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