block: also probe loop devices
[project/fstools.git] / libfstools / snapshot.h
1 /*
2  * Copyright (C) 2014 John Crispin <blogic@openwrt.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License version 2.1
6  * as published by the Free Software Foundation
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  */
13
14 #ifndef _SNAPSHOT_H__
15 #define _SNAPSHOT_H__
16
17 #define PATH_MAX        256
18 #define OWRT            0x4f575254
19 #define DATA            0x44415441
20 #define CONF            0x434f4e46
21
22 struct file_header {
23         uint32_t magic;
24         uint32_t type;
25         uint32_t seq;
26         uint32_t length;
27         uint32_t md5[4];
28 };
29
30 static inline int
31 is_config(struct file_header *h)
32 {
33         return ((h->magic == OWRT) && (h->type == CONF));
34 }
35
36 static inline int
37 valid_file_size(int fs)
38 {
39         if ((fs > 8 * 1024 * 1204) || (fs <= 0))
40                 return -1;
41
42         return 0;
43 }
44
45 static inline void
46 hdr_to_be32(struct file_header *hdr)
47 {
48         uint32_t *h = (uint32_t *) hdr;
49         int i;
50
51         for (i = 0; i < sizeof(struct file_header) / sizeof(uint32_t); i++)
52                 h[i] = cpu_to_be32(h[i]);
53 }
54
55 static inline void
56 be32_to_hdr(struct file_header *hdr)
57 {
58         uint32_t *h = (uint32_t *) hdr;
59         int i;
60
61         for (i = 0; i < sizeof(struct file_header) / sizeof(uint32_t); i++)
62                 h[i] = be32_to_cpu(h[i]);
63 }
64
65 static inline int
66 pad_file_size(struct volume *v, int size)
67 {
68         int mod;
69
70         size += sizeof(struct file_header);
71         mod = size % v->block_size;
72         if (mod) {
73                 size -= mod;
74                 size += v->block_size;
75         }
76
77         return size;
78 }
79
80 int verify_file_hash(char *file, uint32_t *hash);
81 int snapshot_next_free(struct volume *v, uint32_t *seq);
82 int config_find(struct volume *v, struct file_header *conf, struct file_header *sentinel);
83 int snapshot_write_file(struct volume *v, int block, char *file, uint32_t seq, uint32_t type);
84 int snapshot_read_file(struct volume *v, int block, char *file, uint32_t type);
85 int sentinel_write(struct volume *v, uint32_t _seq);
86 int volatile_write(struct volume *v, uint32_t _seq);
87
88 #endif