Whamcloud - gitweb
Convert to use block group accessor functions
[tools/e2fsprogs.git] / debugfs / set_fields.c
1 /*
2  * set_fields.c --- set a superblock value
3  *
4  * Copyright (C) 2000, 2001, 2002, 2003, 2004 by Theodore Ts'o.
5  *
6  * %Begin-Header%
7  * This file may be redistributed under the terms of the GNU Public
8  * License.
9  * %End-Header%
10  */
11
12 #define _XOPEN_SOURCE 600 /* for inclusion of strptime() and strtoull */
13
14 #ifdef HAVE_STRTOULL
15 #define STRTOULL strtoull
16 #else
17 #define STRTOULL strtoul
18 #endif
19
20 #include <stdio.h>
21 #include <unistd.h>
22 #include <stdlib.h>
23 #include <ctype.h>
24 #include <string.h>
25 #include <strings.h>
26 #include <time.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #ifdef HAVE_ERRNO_H
30 #include <errno.h>
31 #endif
32 #if HAVE_STRINGS_H
33 #include <strings.h>
34 #endif
35 #include <fcntl.h>
36 #include <utime.h>
37
38 #include "debugfs.h"
39 #include "uuid/uuid.h"
40 #include "e2p/e2p.h"
41
42 static struct ext2_super_block set_sb;
43 static struct ext2_inode set_inode;
44 static struct ext2_group_desc set_gd;
45 static dgrp_t set_bg;
46 static ext2_ino_t set_ino;
47 static int array_idx;
48
49 #define FLAG_ARRAY      0x0001
50
51 struct field_set_info {
52         const char      *name;
53         void    *ptr;
54         unsigned int    size;
55         errcode_t (*func)(struct field_set_info *info, char *arg);
56         int flags;
57         int max_idx;
58 };
59
60 static errcode_t parse_uint(struct field_set_info *info, char *arg);
61 static errcode_t parse_int(struct field_set_info *info, char *arg);
62 static errcode_t parse_string(struct field_set_info *info, char *arg);
63 static errcode_t parse_uuid(struct field_set_info *info, char *arg);
64 static errcode_t parse_hashalg(struct field_set_info *info, char *arg);
65 static errcode_t parse_time(struct field_set_info *info, char *arg);
66 static errcode_t parse_bmap(struct field_set_info *info, char *arg);
67 static errcode_t parse_gd_csum(struct field_set_info *info, char *arg);
68
69 static struct field_set_info super_fields[] = {
70         { "inodes_count", &set_sb.s_inodes_count, 4, parse_uint },
71         { "blocks_count", &set_sb.s_blocks_count, 4, parse_uint },
72         { "r_blocks_count", &set_sb.s_r_blocks_count, 4, parse_uint },
73         { "free_blocks_count", &set_sb.s_free_blocks_count, 4, parse_uint },
74         { "free_inodes_count", &set_sb.s_free_inodes_count, 4, parse_uint },
75         { "first_data_block", &set_sb.s_first_data_block, 4, parse_uint },
76         { "log_block_size", &set_sb.s_log_block_size, 4, parse_uint },
77         { "log_frag_size", &set_sb.s_log_frag_size, 4, parse_int },
78         { "blocks_per_group", &set_sb.s_blocks_per_group, 4, parse_uint },
79         { "frags_per_group", &set_sb.s_frags_per_group, 4, parse_uint },
80         { "inodes_per_group", &set_sb.s_inodes_per_group, 4, parse_uint },
81         { "mtime", &set_sb.s_mtime, 4, parse_time },
82         { "wtime", &set_sb.s_wtime, 4, parse_time },
83         { "mnt_count", &set_sb.s_mnt_count, 2, parse_uint },
84         { "max_mnt_count", &set_sb.s_max_mnt_count, 2, parse_int },
85         /* s_magic */
86         { "state", &set_sb.s_state, 2, parse_uint },
87         { "errors", &set_sb.s_errors, 2, parse_uint },
88         { "minor_rev_level", &set_sb.s_minor_rev_level, 2, parse_uint },
89         { "lastcheck", &set_sb.s_lastcheck, 4, parse_time },
90         { "checkinterval", &set_sb.s_checkinterval, 4, parse_uint },
91         { "creator_os", &set_sb.s_creator_os, 4, parse_uint },
92         { "rev_level", &set_sb.s_rev_level, 4, parse_uint },
93         { "def_resuid", &set_sb.s_def_resuid, 2, parse_uint },
94         { "def_resgid", &set_sb.s_def_resgid, 2, parse_uint },
95         { "first_ino", &set_sb.s_first_ino, 4, parse_uint },
96         { "inode_size", &set_sb.s_inode_size, 2, parse_uint },
97         { "block_group_nr", &set_sb.s_block_group_nr, 2, parse_uint },
98         { "feature_compat", &set_sb.s_feature_compat, 4, parse_uint },
99         { "feature_incompat", &set_sb.s_feature_incompat, 4, parse_uint },
100         { "feature_ro_compat", &set_sb.s_feature_ro_compat, 4, parse_uint },
101         { "uuid", &set_sb.s_uuid, 16, parse_uuid },
102         { "volume_name",  &set_sb.s_volume_name, 16, parse_string },
103         { "last_mounted",  &set_sb.s_last_mounted, 64, parse_string },
104         { "lastcheck",  &set_sb.s_lastcheck, 4, parse_uint },
105         { "algorithm_usage_bitmap", &set_sb.s_algorithm_usage_bitmap,
106                   4, parse_uint },
107         { "prealloc_blocks", &set_sb.s_prealloc_blocks, 1, parse_uint },
108         { "prealloc_dir_blocks", &set_sb.s_prealloc_dir_blocks, 1,
109                   parse_uint },
110         { "reserved_gdt_blocks", &set_sb.s_reserved_gdt_blocks, 2,
111                   parse_uint },
112         { "journal_uuid", &set_sb.s_journal_uuid, 16, parse_uuid },
113         { "journal_inum", &set_sb.s_journal_inum, 4, parse_uint },
114         { "journal_dev", &set_sb.s_journal_dev, 4, parse_uint },
115         { "last_orphan", &set_sb.s_last_orphan, 4, parse_uint },
116         { "hash_seed", &set_sb.s_hash_seed, 16, parse_uuid },
117         { "def_hash_version", &set_sb.s_def_hash_version, 1, parse_hashalg },
118         { "jnl_backup_type", &set_sb.s_jnl_backup_type, 1, parse_uint },
119         { "desc_size", &set_sb.s_desc_size, 2, parse_uint },
120         { "default_mount_opts", &set_sb.s_default_mount_opts, 4, parse_uint },
121         { "first_meta_bg", &set_sb.s_first_meta_bg, 4, parse_uint },
122         { "mkfs_time", &set_sb.s_mkfs_time, 4, parse_time },
123         { "jnl_blocks", &set_sb.s_jnl_blocks[0], 4, parse_uint, FLAG_ARRAY,
124           17 },
125         { "blocks_count_hi", &set_sb.s_blocks_count_hi, 4, parse_uint },
126         { "r_blocks_count_hi", &set_sb.s_r_blocks_count_hi, 4, parse_uint },
127         { "free_blocks_hi", &set_sb.s_free_blocks_hi, 4, parse_uint },
128         { "min_extra_isize", &set_sb.s_min_extra_isize, 2, parse_uint },
129         { "want_extra_isize", &set_sb.s_want_extra_isize, 2, parse_uint },
130         { "flags", &set_sb.s_flags, 4, parse_uint },
131         { "raid_stride", &set_sb.s_raid_stride, 2, parse_uint },
132         { "min_extra_isize", &set_sb.s_min_extra_isize, 4, parse_uint },
133         { "mmp_interval", &set_sb.s_mmp_interval, 2, parse_uint },
134         { "mmp_block", &set_sb.s_mmp_block, 8, parse_uint },
135         { "raid_stripe_width", &set_sb.s_raid_stripe_width, 4, parse_uint },
136         { "log_groups_per_flex", &set_sb.s_log_groups_per_flex, 1, parse_uint },
137         { "kbytes_written", &set_sb.s_kbytes_written, 8, parse_uint },
138         { 0, 0, 0, 0 }
139 };
140
141 static struct field_set_info inode_fields[] = {
142         { "inodes_count", &set_sb.s_inodes_count, 4, parse_uint },
143         { "mode", &set_inode.i_mode, 2, parse_uint },
144         { "uid", &set_inode.i_uid, 2, parse_uint },
145         { "size", &set_inode.i_size, 4, parse_uint },
146         { "atime", &set_inode.i_atime, 4, parse_time },
147         { "ctime", &set_inode.i_ctime, 4, parse_time },
148         { "mtime", &set_inode.i_mtime, 4, parse_time },
149         { "dtime", &set_inode.i_dtime, 4, parse_time },
150         { "gid", &set_inode.i_gid, 2, parse_uint },
151         { "links_count", &set_inode.i_links_count, 2, parse_uint },
152         { "blocks", &set_inode.i_blocks, 4, parse_uint },
153         { "flags", &set_inode.i_flags, 4, parse_uint },
154         { "version", &set_inode.osd1.linux1.l_i_version, 4, parse_uint },
155         { "translator", &set_inode.osd1.hurd1.h_i_translator, 4, parse_uint },
156         { "block", &set_inode.i_block[0], 4, parse_uint, FLAG_ARRAY,
157           EXT2_NDIR_BLOCKS },
158         { "block[IND]", &set_inode.i_block[EXT2_IND_BLOCK], 4, parse_uint },
159         { "block[DIND]", &set_inode.i_block[EXT2_DIND_BLOCK], 4, parse_uint },
160         { "block[TIND]", &set_inode.i_block[EXT2_TIND_BLOCK], 4, parse_uint },
161         { "generation", &set_inode.i_generation, 4, parse_uint },
162         { "file_acl", &set_inode.i_file_acl, 4, parse_uint },
163         { "file_acl_high", &set_inode.osd2.linux2.l_i_file_acl_high, 2,
164           parse_uint },
165         { "dir_acl", &set_inode.i_dir_acl, 4, parse_uint },
166         { "size_high", &set_inode.i_size_high, 4, parse_uint },
167         { "faddr", &set_inode.i_faddr, 4, parse_uint },
168         { "blocks_hi", &set_inode.osd2.linux2.l_i_blocks_hi, 2, parse_uint },
169         { "frag", &set_inode.osd2.hurd2.h_i_frag, 1, parse_uint },
170         { "fsize", &set_inode.osd2.hurd2.h_i_fsize, 1, parse_uint },
171         { "uid_high", &set_inode.osd2.linux2.l_i_uid_high, 2, parse_uint },
172         { "gid_high", &set_inode.osd2.linux2.l_i_gid_high, 2, parse_uint },
173         { "author", &set_inode.osd2.hurd2.h_i_author, 4, parse_uint },
174         { "bmap", NULL, 4, parse_bmap, FLAG_ARRAY },
175         { 0, 0, 0, 0 }
176 };
177
178 static struct field_set_info ext2_bg_fields[] = {
179         { "block_bitmap", &set_gd.bg_block_bitmap, 4, parse_uint },
180         { "inode_bitmap", &set_gd.bg_inode_bitmap, 4, parse_uint },
181         { "inode_table", &set_gd.bg_inode_table, 4, parse_uint },
182         { "free_blocks_count", &set_gd.bg_free_blocks_count, 2, parse_uint },
183         { "free_inodes_count", &set_gd.bg_free_inodes_count, 2, parse_uint },
184         { "used_dirs_count", &set_gd.bg_used_dirs_count, 2, parse_uint },
185         { "flags", &set_gd.bg_flags, 2, parse_uint },
186         { "reserved", &set_gd.bg_reserved, 2, parse_uint, FLAG_ARRAY, 2 },
187         { "itable_unused", &set_gd.bg_itable_unused, 2, parse_uint },
188         { "checksum", &set_gd.bg_checksum, 2, parse_gd_csum },
189         { 0, 0, 0, 0 }
190 };
191
192
193 static struct field_set_info *find_field(struct field_set_info *fields,
194                                          char *field)
195 {
196         struct field_set_info *ss;
197         const char      *prefix;
198         char            *arg, *delim, *idx, *tmp;
199         int             prefix_len;
200
201         if (fields == super_fields)
202                 prefix = "s_";
203         else if (fields == inode_fields)
204                 prefix = "i_";
205         else
206                 prefix = "bg_";
207         prefix_len = strlen(prefix);
208         if (strncmp(field, prefix, prefix_len) == 0)
209                 field += prefix_len;
210
211         arg = malloc(strlen(field)+1);
212         if (!arg)
213                 return NULL;
214         strcpy(arg, field);
215
216         idx = strchr(arg, '[');
217         if (idx) {
218                 *idx++ = 0;
219                 delim = idx + strlen(idx) - 1;
220                 if (!*idx || *delim != ']')
221                         idx = 0;
222                 else
223                         *delim = 0;
224         }
225         /*
226          * Can we parse the number?
227          */
228         if (idx) {
229                 array_idx = strtol(idx, &tmp, 0);
230                 if (*tmp)
231                         idx = 0;
232         }
233
234         for (ss = fields ; ss->name ; ss++) {
235                 if (ss->flags & FLAG_ARRAY) {
236                         if (!idx || (strcmp(ss->name, arg) != 0))
237                                 continue;
238                         if (ss->max_idx > 0 && array_idx >= ss->max_idx)
239                                 continue;
240                 } else {
241                         if (strcmp(ss->name, field) != 0)
242                                 continue;
243                 }
244                 free(arg);
245                 return ss;
246         }
247         free(arg);
248         return NULL;
249 }
250
251 static errcode_t parse_uint(struct field_set_info *info, char *arg)
252 {
253         unsigned long long num, limit;
254         char *tmp;
255         union {
256                 __u64   *ptr64;
257                 __u32   *ptr32;
258                 __u16   *ptr16;
259                 __u8    *ptr8;
260         } u;
261
262         u.ptr8 = (__u8 *) info->ptr;
263         if (info->flags & FLAG_ARRAY)
264                 u.ptr8 += array_idx * info->size;
265
266         errno = 0;
267         num = STRTOULL(arg, &tmp, 0);
268         if (*tmp || errno) {
269                 fprintf(stderr, "Couldn't parse '%s' for field %s.\n",
270                         arg, info->name);
271                 return EINVAL;
272         }
273         limit = ~0ULL >> ((8 - info->size) * 8);
274         if (num > limit) {
275                 fprintf(stderr, "Value '%s' exceeds field %s maximum %llu.\n",
276                         arg, info->name, limit);
277                 return EINVAL;
278         }
279         switch (info->size) {
280         case 8:
281                 *u.ptr64 = num;
282                 break;
283         case 4:
284                 *u.ptr32 = num;
285                 break;
286         case 2:
287                 *u.ptr16 = num;
288                 break;
289         case 1:
290                 *u.ptr8 = num;
291                 break;
292         }
293         return 0;
294 }
295
296 static errcode_t parse_int(struct field_set_info *info, char *arg)
297 {
298         long    num;
299         char *tmp;
300         __s32   *ptr32;
301         __s16   *ptr16;
302         __s8    *ptr8;
303
304         num = strtol(arg, &tmp, 0);
305         if (*tmp) {
306                 fprintf(stderr, "Couldn't parse '%s' for field %s.\n",
307                         arg, info->name);
308                 return EINVAL;
309         }
310         switch (info->size) {
311         case 4:
312                 ptr32 = (__s32 *) info->ptr;
313                 *ptr32 = num;
314                 break;
315         case 2:
316                 ptr16 = (__s16 *) info->ptr;
317                 *ptr16 = num;
318                 break;
319         case 1:
320                 ptr8 = (__s8 *) info->ptr;
321                 *ptr8 = num;
322                 break;
323         }
324         return 0;
325 }
326
327 static errcode_t parse_string(struct field_set_info *info, char *arg)
328 {
329         char    *cp = (char *) info->ptr;
330
331         if (strlen(arg) >= info->size) {
332                 fprintf(stderr, "Error maximum size for %s is %d.\n",
333                         info->name, info->size);
334                 return EINVAL;
335         }
336         strcpy(cp, arg);
337         return 0;
338 }
339
340 static errcode_t parse_time(struct field_set_info *info, char *arg)
341 {
342         time_t          t;
343         __u32           *ptr32;
344
345         ptr32 = (__u32 *) info->ptr;
346
347         t = string_to_time(arg);
348
349         if (t == ((time_t) -1)) {
350                 fprintf(stderr, "Couldn't parse '%s' for field %s.\n",
351                         arg, info->name);
352                 return EINVAL;
353         }
354         *ptr32 = t;
355         return 0;
356 }
357
358 static errcode_t parse_uuid(struct field_set_info *info, char *arg)
359 {
360         unsigned char * p = (unsigned char *) info->ptr;
361
362         if ((strcasecmp(arg, "null") == 0) ||
363             (strcasecmp(arg, "clear") == 0)) {
364                 uuid_clear(p);
365         } else if (strcasecmp(arg, "time") == 0) {
366                 uuid_generate_time(p);
367         } else if (strcasecmp(arg, "random") == 0) {
368                 uuid_generate(p);
369         } else if (uuid_parse(arg, p)) {
370                 fprintf(stderr, "Invalid UUID format: %s\n", arg);
371                 return EINVAL;
372         }
373         return 0;
374 }
375
376 static errcode_t parse_hashalg(struct field_set_info *info, char *arg)
377 {
378         int     hashv;
379         unsigned char   *p = (unsigned char *) info->ptr;
380
381         hashv = e2p_string2hash(arg);
382         if (hashv < 0) {
383                 fprintf(stderr, "Invalid hash algorithm: %s\n", arg);
384                 return EINVAL;
385         }
386         *p = hashv;
387         return 0;
388 }
389
390 static errcode_t parse_bmap(struct field_set_info *info, char *arg)
391 {
392         unsigned long   num;
393         blk_t           blk;
394         errcode_t       retval;
395         char            *tmp;
396
397         num = strtoul(arg, &tmp, 0);
398         if (*tmp) {
399                 fprintf(stderr, "Couldn't parse '%s' for field %s.\n",
400                         arg, info->name);
401                 return EINVAL;
402         }
403         blk = num;
404
405         retval = ext2fs_bmap(current_fs, set_ino, &set_inode, 0, BMAP_SET,
406                              array_idx, &blk);
407         if (retval) {
408                 com_err("set_inode", retval, "while setting block map");
409         }
410         return retval;
411 }
412
413 static errcode_t parse_gd_csum(struct field_set_info *info, char *arg)
414 {
415
416         if (strcmp(arg, "calc") == 0) {
417                 ext2fs_group_desc_csum_set(current_fs, set_bg);
418                 memcpy(&set_gd, ext2fs_group_desc(current_fs,
419                                         current_fs->group_desc,
420                                         set_bg),
421                         sizeof(set_gd));
422                 printf("Checksum set to 0x%04x\n",
423                        ext2fs_bg_checksum(current_fs, set_bg));
424                 return 0;
425         }
426
427         return parse_uint(info, arg);
428 }
429
430 static void print_possible_fields(struct field_set_info *fields)
431 {
432         struct field_set_info *ss;
433         const char      *type, *cmd;
434         FILE *f;
435         char name[40], idx[40];
436
437         if (fields == super_fields) {
438                 type = "Superblock";
439                 cmd = "set_super_value";
440         } else if (fields == inode_fields) {
441                 type = "Inode";
442                 cmd = "set_inode";
443         } else {
444                 type = "Block group descriptor";
445                 cmd = "set_block_group";
446         }
447         f = open_pager();
448
449         fprintf(f, "%s fields supported by the %s command:\n", type, cmd);
450
451         for (ss = fields ; ss->name ; ss++) {
452                 type = "unknown";
453                 if (ss->func == parse_string)
454                         type = "string";
455                 else if (ss->func == parse_int)
456                         type = "integer";
457                 else if (ss->func == parse_uint)
458                         type = "unsigned integer";
459                 else if (ss->func == parse_uuid)
460                         type = "UUID";
461                 else if (ss->func == parse_hashalg)
462                         type = "hash algorithm";
463                 else if (ss->func == parse_time)
464                         type = "date/time";
465                 else if (ss->func == parse_bmap)
466                         type = "set physical->logical block map";
467                 strcpy(name, ss->name);
468                 if (ss->flags & FLAG_ARRAY) {
469                         if (ss->max_idx > 0)
470                                 sprintf(idx, "[%d]", ss->max_idx);
471                         else
472                                 strcpy(idx, "[]");
473                         strcat(name, idx);
474                 }
475                 fprintf(f, "\t%-20s\t%s\n", name, type);
476         }
477         close_pager(f);
478 }
479
480
481 void do_set_super(int argc, char *argv[])
482 {
483         const char *usage = "<field> <value>\n"
484                 "\t\"set_super_value -l\" will list the names of "
485                 "superblock fields\n\twhich can be set.";
486         static struct field_set_info *ss;
487
488         if ((argc == 2) && !strcmp(argv[1], "-l")) {
489                 print_possible_fields(super_fields);
490                 return;
491         }
492
493         if (common_args_process(argc, argv, 3, 3, "set_super_value",
494                                 usage, CHECK_FS_RW))
495                 return;
496
497         if ((ss = find_field(super_fields, argv[1])) == 0) {
498                 com_err(argv[0], 0, "invalid field specifier: %s", argv[1]);
499                 return;
500         }
501         set_sb = *current_fs->super;
502         if (ss->func(ss, argv[2]) == 0) {
503                 *current_fs->super = set_sb;
504                 ext2fs_mark_super_dirty(current_fs);
505         }
506 }
507
508 void do_set_inode(int argc, char *argv[])
509 {
510         const char *usage = "<inode> <field> <value>\n"
511                 "\t\"set_inode_field -l\" will list the names of "
512                 "the fields in an ext2 inode\n\twhich can be set.";
513         static struct field_set_info *ss;
514
515         if ((argc == 2) && !strcmp(argv[1], "-l")) {
516                 print_possible_fields(inode_fields);
517                 return;
518         }
519
520         if (common_args_process(argc, argv, 4, 4, "set_inode",
521                                 usage, CHECK_FS_RW))
522                 return;
523
524         if ((ss = find_field(inode_fields, argv[2])) == 0) {
525                 com_err(argv[0], 0, "invalid field specifier: %s", argv[2]);
526                 return;
527         }
528
529         set_ino = string_to_inode(argv[1]);
530         if (!set_ino)
531                 return;
532
533         if (debugfs_read_inode(set_ino, &set_inode, argv[1]))
534                 return;
535
536         if (ss->func(ss, argv[3]) == 0) {
537                 if (debugfs_write_inode(set_ino, &set_inode, argv[1]))
538                         return;
539         }
540 }
541
542 void do_set_block_group_descriptor(int argc, char *argv[])
543 {
544         const char *usage = "<bg number> <field> <value>\n"
545                 "\t\"set_block_group_descriptor -l\" will list the names of "
546                 "the fields in a block group descriptor\n\twhich can be set.";
547         struct field_set_info   *ss;
548         char                    *end;
549
550         if ((argc == 2) && !strcmp(argv[1], "-l")) {
551                 print_possible_fields(ext2_bg_fields);
552                 return;
553         }
554
555         if (common_args_process(argc, argv, 4, 4, "set_block_group_descriptor",
556                                 usage, CHECK_FS_RW))
557                 return;
558
559         set_bg = strtoul(argv[1], &end, 0);
560         if (*end) {
561                 com_err(argv[0], 0, "invalid block group number: %s", argv[1]);
562                 return;
563         }
564
565         if (set_bg >= current_fs->group_desc_count) {
566                 com_err(argv[0], 0, "block group number too big: %d", set_bg);
567                 return;
568         }
569
570
571         if ((ss = find_field(ext2_bg_fields, argv[2])) == 0) {
572                 com_err(argv[0], 0, "invalid field specifier: %s", argv[2]);
573                 return;
574         }
575
576         memcpy(&set_gd, ext2fs_group_desc(current_fs,
577                                 current_fs->group_desc, set_bg),
578                 sizeof(set_gd));
579
580         if (ss->func(ss, argv[3]) == 0) {
581                 memcpy(ext2fs_group_desc(current_fs,
582                                 current_fs->group_desc, set_bg),
583                        &set_gd, sizeof(set_gd));
584                 ext2fs_mark_super_dirty(current_fs);
585         }
586 }