btrfs-progs: Rename OPEN_CTREE_FS_PARTIAL to OPEN_CTREE_TEMPORARY_SUPER
[platform/upstream/btrfs-progs.git] / btrfs-sb-mod.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public
4  * License v2 as published by the Free Software Foundation.
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
9  * General Public License for more details.
10  *
11  * You should have received a copy of the GNU General Public
12  * License along with this program; if not, write to the
13  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
14  * Boston, MA 021110-1307, USA.
15  */
16
17 #include "kerncompat.h"
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <sys/fcntl.h>
22 #include <sys/stat.h>
23 #include <inttypes.h>
24 #include <string.h>
25 #include <limits.h>
26 #include <byteswap.h>
27 #include <kernel-lib/crc32c.h>
28 #include "disk-io.h"
29
30 #define BLOCKSIZE (4096)
31 static char buf[BLOCKSIZE];
32 static int csum_size;
33
34 static int check_csum_superblock(void *sb)
35 {
36         u8 result[csum_size];
37         u32 crc = ~(u32)0;
38
39         crc = btrfs_csum_data((char *)sb + BTRFS_CSUM_SIZE,
40                                 crc, BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
41         btrfs_csum_final(crc, result);
42
43         return !memcmp(sb, &result, csum_size);
44 }
45
46 static void update_block_csum(void *block, int is_sb)
47 {
48         u8 result[csum_size];
49         struct btrfs_header *hdr;
50         u32 crc = ~(u32)0;
51
52         if (is_sb) {
53                 crc = btrfs_csum_data((char *)block + BTRFS_CSUM_SIZE, crc,
54                                 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
55         } else {
56                 crc = btrfs_csum_data((char *)block + BTRFS_CSUM_SIZE, crc,
57                                 BLOCKSIZE - BTRFS_CSUM_SIZE);
58         }
59         btrfs_csum_final(crc, result);
60         memset(block, 0, BTRFS_CSUM_SIZE);
61         hdr = (struct btrfs_header *)block;
62         memcpy(&hdr->csum, result, csum_size);
63 }
64
65 static u64 arg_strtou64(const char *str)
66 {
67         u64 value;
68         char *ptr_parse_end = NULL;
69
70         value = strtoull(str, &ptr_parse_end, 0);
71         if (ptr_parse_end && *ptr_parse_end != '\0') {
72                 fprintf(stderr, "ERROR: %s is not a valid numeric value.\n",
73                         str);
74                 exit(1);
75         }
76
77         /*
78          * if we pass a negative number to strtoull, it will return an
79          * unexpected number to us, so let's do the check ourselves.
80          */
81         if (str[0] == '-') {
82                 fprintf(stderr, "ERROR: %s: negative value is invalid.\n",
83                         str);
84                 exit(1);
85         }
86         if (value == ULLONG_MAX) {
87                 fprintf(stderr, "ERROR: %s is too large.\n", str);
88                 exit(1);
89         }
90         return value;
91 }
92
93
94 enum field_op {
95         OP_GET,
96         OP_SET,
97         OP_ADD,
98         OP_SUB,
99         OP_XOR,
100         OP_NAND,        /* broken */
101         OP_BSWAP,
102 };
103
104 struct fspec {
105         const char *name;
106         enum field_op fop;
107         u64 value;
108 };
109
110 enum field_type {
111         TYPE_UNKNOWN,
112         TYPE_U64,
113         TYPE_U16,
114 };
115
116 struct sb_field {
117         const char *name;
118         enum field_type type;
119 } known_fields[] = {
120         { .name = "total_bytes", .type = TYPE_U64 },
121         { .name = "root", .type = TYPE_U64 },
122         { .name = "generation", .type = TYPE_U64 },
123         { .name = "chunk_root", .type = TYPE_U64 },
124         { .name = "chunk_root_generation", .type = TYPE_U64 },
125         { .name = "cache_generation", .type = TYPE_U64 },
126         { .name = "uuid_tree_generation", .type = TYPE_U64 },
127         { .name = "compat_flags", .type = TYPE_U64 },
128         { .name = "compat_ro_flags", .type = TYPE_U64 },
129         { .name = "incompat_flags", .type = TYPE_U64 },
130         { .name = "csum_type", .type = TYPE_U16 },
131 };
132
133 #define MOD_FIELD_XX(fname, set, val, bits, f_dec, f_hex, f_type)       \
134         else if (strcmp(name, #fname) == 0) {                           \
135                 if (set) {                                              \
136                         printf("SET: "#fname" "f_dec" (0x"f_hex")\n", \
137                         (f_type)*val, (f_type)*val);                            \
138                         sb->fname = cpu_to_le##bits(*val);                      \
139                 } else {                                                        \
140                         *val = le##bits##_to_cpu(sb->fname);                    \
141                         printf("GET: "#fname" "f_dec" (0x"f_hex")\n",   \
142                         (f_type)*val, (f_type)*val);                    \
143                 }                                                       \
144         }
145
146 #define MOD_FIELD64(fname, set, val)                                    \
147         MOD_FIELD_XX(fname, set, val, 64, "%llu", "%llx", unsigned long long)
148
149 /* Alias for u64 */
150 #define MOD_FIELD(fname, set, val)      MOD_FIELD64(fname, set, val)
151
152 /*
153  * Support only GET and SET properly, ADD and SUB may work
154  */
155 #define MOD_FIELD32(fname, set, val)                                    \
156         MOD_FIELD_XX(fname, set, val, 32, "%u", "%x", unsigned int)
157
158 #define MOD_FIELD16(fname, set, val)                                    \
159         MOD_FIELD_XX(fname, set, val, 16, "%hu", "%hx", unsigned short int)
160
161 #define MOD_FIELD8(fname, set, val)                                     \
162         MOD_FIELD_XX(fname, set, val, 8, "%hhu", "%hhx", unsigned char)
163
164 static void mod_field_by_name(struct btrfs_super_block *sb, int set, const char *name,
165                 u64 *val)
166 {
167         if (0) { }
168                 MOD_FIELD(total_bytes, set, val)
169                 MOD_FIELD(root, set, val)
170                 MOD_FIELD(generation, set, val)
171                 MOD_FIELD(chunk_root, set, val)
172                 MOD_FIELD(chunk_root_generation, set, val)
173                 MOD_FIELD(cache_generation, set, val)
174                 MOD_FIELD(uuid_tree_generation, set, val)
175                 MOD_FIELD(compat_flags, set, val)
176                 MOD_FIELD(compat_ro_flags, set, val)
177                 MOD_FIELD(incompat_flags, set, val)
178                 MOD_FIELD16(csum_type, set, val)
179         else {
180                 printf("ERROR: unhandled field: %s\n", name);
181                 exit(1);
182         }
183 }
184
185 static int sb_edit(struct btrfs_super_block *sb, struct fspec *fsp)
186 {
187         u64 val;
188         u64 newval;
189
190         mod_field_by_name(sb, 0, fsp->name, &val);
191         switch (fsp->fop) {
192         case OP_GET: newval = val; break;
193         case OP_SET: newval = fsp->value; break;
194         case OP_ADD: newval = val + fsp->value; break;
195         case OP_SUB: newval = val - fsp->value; break;
196         case OP_XOR: newval = val ^ fsp->value; break;
197         case OP_NAND: newval = val & (~fsp->value); break;
198         case OP_BSWAP: newval = bswap_64(val); break;
199         default: printf("ERROR: unhandled operation: %d\n", fsp->fop); exit(1);
200         }
201
202         mod_field_by_name(sb, 1, fsp->name, &newval);
203
204         return 0;
205 }
206
207 static int is_known_field(const char *f)
208 {
209         int i;
210
211         for (i = 0; i < ARRAY_SIZE(known_fields); i++)
212                 if (strcmp(f, known_fields[i].name) == 0)
213                         return 1;
214         return 0;
215 }
216
217 static int arg_to_op_value(const char *arg, enum field_op *op, u64 *val)
218 {
219         switch (arg[0]) {
220         case 0: return -1;
221         case '.':
222         case '?': *op = OP_GET; *val = 0; break;
223         case '=': *op = OP_SET; *val = arg_strtou64(arg + 1); break;
224         case '+': *op = OP_ADD; *val = arg_strtou64(arg + 1); break;
225         case '-': *op = OP_SUB; *val = arg_strtou64(arg + 1); break;
226         case '^': *op = OP_XOR; *val = arg_strtou64(arg + 1); break;
227         case '~': *op = OP_NAND; *val = arg_strtou64(arg + 1); break;
228         case '@': *op = OP_BSWAP; *val = arg_strtou64(arg + 1); break;
229         default:
230                   printf("ERROR: unknown op: %c\n", arg[0]);
231                   return -1;
232         }
233
234         return 0;
235 }
236
237 int main(int argc, char **argv) {
238         int fd;
239         loff_t off;
240         int ret;
241         struct btrfs_header *hdr;
242         struct btrfs_super_block *sb;
243         int i;
244         struct fspec spec[128];
245         int specidx;
246         int changed;
247
248         memset(spec, 0, sizeof(spec));
249         if (argc <= 2) {
250                 printf("Usage: %s image [fieldspec...]\n", argv[0]);
251                 exit(1);
252         }
253         fd = open(argv[1], O_RDWR | O_EXCL);
254         if (fd == -1) {
255                 perror("open()");
256                 exit(1);
257         }
258
259         /* verify superblock */
260         csum_size = btrfs_csum_sizes[BTRFS_CSUM_TYPE_CRC32];
261         off = BTRFS_SUPER_INFO_OFFSET;
262
263         ret = pread(fd, buf, BLOCKSIZE, off);
264         if (ret <= 0) {
265                 printf("pread error %d at offset %llu\n",
266                                 ret, (unsigned long long)off);
267                 exit(1);
268         }
269         if (ret != BLOCKSIZE) {
270                 printf("pread error at offset %llu: read only %d bytes\n",
271                                 (unsigned long long)off, ret);
272                 exit(1);
273         }
274         hdr = (struct btrfs_header *)buf;
275         /* verify checksum */
276         if (!check_csum_superblock(&hdr->csum)) {
277                 printf("super block checksum does not match at offset %llu, will be corrected\n",
278                                 (unsigned long long)off);
279         } else {
280                 printf("super block checksum is ok\n");
281         }
282         sb = (struct btrfs_super_block *)buf;
283         changed = 0;
284
285         specidx = 0;
286         for (i = 2; i < argc; i++) {
287                 struct fspec *f;
288
289                 if (i + 1 >= argc) {
290                         printf("ERROR: bad argument count\n");
291                         ret = 1;
292                         goto out;
293                 }
294
295                 if (!is_known_field(argv[i])) {
296                         printf("ERROR: unknown filed: %s\n", argv[i]);
297                         ret = 1;
298                         goto out;
299                 }
300                 f = &spec[specidx];
301                 specidx++;
302                 f->name = strdup(argv[i]);
303                 i++;
304                 if (arg_to_op_value(argv[i], &f->fop, &f->value)) {
305                         ret = 1;
306                         goto out;
307                 }
308         }
309
310         for (i = 0; i < specidx; i++) {
311                 sb_edit(sb, &spec[i]);
312                 changed = 1;
313         }
314
315         if (changed) {
316                 printf("Update csum\n");
317                 update_block_csum(buf, 1);
318                 ret = pwrite(fd, buf, BLOCKSIZE, off);
319                 if (ret <= 0) {
320                         printf("pwrite error %d at offset %llu\n",
321                                         ret, (unsigned long long)off);
322                         exit(1);
323                 }
324                 if (ret != BLOCKSIZE) {
325                         printf("pwrite error at offset %llu: written only %d bytes\n",
326                                         (unsigned long long)off, ret);
327                         exit(1);
328                 }
329                 fsync(fd);
330         } else {
331                 printf("Nothing changed\n");
332         }
333         ret = 0;
334 out:
335         close(fd);
336         return ret;
337 }