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