2 * set_fields.c --- set a superblock value
4 * Copyright (C) 2000, 2001, 2002, 2003, 2004 by Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Public
12 #define _XOPEN_SOURCE 500 /* for inclusion of strptime() */
20 #include <sys/types.h>
29 #include "uuid/uuid.h"
32 static struct ext2_super_block set_sb;
33 static struct ext2_inode set_inode;
34 static ext2_ino_t set_ino;
37 #define FLAG_ARRAY 0x0001
39 struct field_set_info {
43 errcode_t (*func)(struct field_set_info *info, char *arg);
48 static errcode_t parse_uint(struct field_set_info *info, char *arg);
49 static errcode_t parse_int(struct field_set_info *info, char *arg);
50 static errcode_t parse_string(struct field_set_info *info, char *arg);
51 static errcode_t parse_uuid(struct field_set_info *info, char *arg);
52 static errcode_t parse_hashalg(struct field_set_info *info, char *arg);
53 static errcode_t parse_time(struct field_set_info *info, char *arg);
54 static errcode_t parse_bmap(struct field_set_info *info, char *arg);
56 static struct field_set_info super_fields[] = {
57 { "inodes_count", &set_sb.s_inodes_count, 4, parse_uint },
58 { "blocks_count", &set_sb.s_blocks_count, 4, parse_uint },
59 { "r_blocks_count", &set_sb.s_r_blocks_count, 4, parse_uint },
60 { "free_blocks_count", &set_sb.s_free_blocks_count, 4, parse_uint },
61 { "free_inodes_count", &set_sb.s_free_inodes_count, 4, parse_uint },
62 { "first_data_block", &set_sb.s_first_data_block, 4, parse_uint },
63 { "log_block_size", &set_sb.s_log_block_size, 4, parse_uint },
64 { "log_frag_size", &set_sb.s_log_frag_size, 4, parse_int },
65 { "blocks_per_group", &set_sb.s_blocks_per_group, 4, parse_uint },
66 { "frags_per_group", &set_sb.s_frags_per_group, 4, parse_uint },
67 { "inodes_per_group", &set_sb.s_inodes_per_group, 4, parse_uint },
68 { "mtime", &set_sb.s_mtime, 4, parse_time },
69 { "wtime", &set_sb.s_wtime, 4, parse_time },
70 { "mnt_count", &set_sb.s_mnt_count, 2, parse_uint },
71 { "max_mnt_count", &set_sb.s_max_mnt_count, 2, parse_int },
73 { "state", &set_sb.s_state, 2, parse_uint },
74 { "errors", &set_sb.s_errors, 2, parse_uint },
75 { "minor_rev_level", &set_sb.s_minor_rev_level, 2, parse_uint },
76 { "lastcheck", &set_sb.s_lastcheck, 4, parse_time },
77 { "checkinterval", &set_sb.s_checkinterval, 4, parse_uint },
78 { "creator_os", &set_sb.s_creator_os, 4, parse_uint },
79 { "rev_level", &set_sb.s_rev_level, 4, parse_uint },
80 { "def_resuid", &set_sb.s_def_resuid, 2, parse_uint },
81 { "def_resgid", &set_sb.s_def_resgid, 2, parse_uint },
82 { "first_ino", &set_sb.s_first_ino, 4, parse_uint },
83 { "inode_size", &set_sb.s_inode_size, 2, parse_uint },
84 { "block_group_nr", &set_sb.s_block_group_nr, 2, parse_uint },
85 { "feature_compat", &set_sb.s_feature_compat, 4, parse_uint },
86 { "feature_incompat", &set_sb.s_feature_incompat, 4, parse_uint },
87 { "feature_ro_compat", &set_sb.s_feature_ro_compat, 4, parse_uint },
88 { "uuid", &set_sb.s_uuid, 16, parse_uuid },
89 { "volume_name", &set_sb.s_volume_name, 16, parse_string },
90 { "last_mounted", &set_sb.s_last_mounted, 64, parse_string },
91 { "lastcheck", &set_sb.s_lastcheck, 4, parse_uint },
92 { "algorithm_usage_bitmap", &set_sb.s_algorithm_usage_bitmap,
94 { "prealloc_blocks", &set_sb.s_prealloc_blocks, 1, parse_uint },
95 { "prealloc_dir_blocks", &set_sb.s_prealloc_dir_blocks, 1,
97 { "reserved_gdt_blocks", &set_sb.s_reserved_gdt_blocks, 2,
100 { "journal_uuid", &set_sb.s_journal_uuid, 16, parse_uuid },
101 { "journal_inum", &set_sb.s_journal_inum, 4, parse_uint },
102 { "journal_dev", &set_sb.s_journal_dev, 4, parse_uint },
103 { "last_orphan", &set_sb.s_last_orphan, 4, parse_uint },
104 { "hash_seed", &set_sb.s_hash_seed, 16, parse_uuid },
105 { "def_hash_version", &set_sb.s_def_hash_version, 1, parse_hashalg },
106 { "jnl_backup_type", &set_sb.s_jnl_backup_type, 1, parse_uint },
107 /* s_reserved_word_pad */
108 { "default_mount_opts", &set_sb.s_default_mount_opts, 4, parse_uint },
109 { "first_meta_bg", &set_sb.s_first_meta_bg, 4, parse_uint },
110 { "mkfs_time", &set_sb.s_mkfs_time, 4, parse_time },
111 { "jnl_blocks", &set_sb.s_jnl_blocks[0], 4, parse_uint, FLAG_ARRAY,
116 static struct field_set_info inode_fields[] = {
117 { "inodes_count", &set_sb.s_inodes_count, 4, parse_uint },
118 { "mode", &set_inode.i_mode, 2, parse_uint },
119 { "uid", &set_inode.i_uid, 2, parse_uint },
120 { "size", &set_inode.i_uid, 4, parse_uint },
121 { "atime", &set_inode.i_atime, 4, parse_time },
122 { "ctime", &set_inode.i_ctime, 4, parse_time },
123 { "mtime", &set_inode.i_mtime, 4, parse_time },
124 { "dtime", &set_inode.i_dtime, 4, parse_time },
125 { "gid", &set_inode.i_gid, 2, parse_uint },
126 { "links_count", &set_inode.i_links_count, 2, parse_uint },
127 { "blocks", &set_inode.i_blocks, 4, parse_uint },
128 { "flags", &set_inode.i_flags, 4, parse_uint },
129 { "translator", &set_inode.osd1.hurd1.h_i_translator, 4, parse_uint },
130 { "block", &set_inode.i_block[0], 4, parse_uint, FLAG_ARRAY,
132 { "block[IND]", &set_inode.i_block[EXT2_IND_BLOCK], 4, parse_uint },
133 { "block[DIND]", &set_inode.i_block[EXT2_DIND_BLOCK], 4, parse_uint },
134 { "block[TIND]", &set_inode.i_block[EXT2_TIND_BLOCK], 4, parse_uint },
135 { "generation", &set_inode.i_generation, 4, parse_uint },
136 { "file_acl", &set_inode.i_file_acl, 4, parse_uint },
137 { "dir_acl", &set_inode.i_dir_acl, 4, parse_uint },
138 { "faddr", &set_inode.i_faddr, 4, parse_uint },
139 { "frag", &set_inode.osd2.linux2.l_i_frag, 8, parse_uint },
140 { "fsize", &set_inode.osd2.linux2.l_i_fsize, 8, parse_uint },
141 { "uid_high", &set_inode.osd2.linux2.l_i_uid_high, 8, parse_uint },
142 { "gid_high", &set_inode.osd2.linux2.l_i_gid_high, 8, parse_uint },
143 { "author", &set_inode.osd2.hurd2.h_i_author, 8, parse_uint },
144 { "bmap", NULL, 4, parse_bmap, FLAG_ARRAY },
148 static struct field_set_info *find_field(struct field_set_info *fields,
151 struct field_set_info *ss;
153 char *arg, *delim, *idx, *tmp;
155 if (fields == super_fields)
159 if (strncmp(field, prefix, 2) == 0)
162 arg = malloc(strlen(field)+1);
167 idx = strchr(arg, '[');
170 delim = idx + strlen(idx) - 1;
171 if (!*idx || *delim != ']')
177 * Can we parse the number?
180 array_idx = strtol(idx, &tmp, 0);
185 for (ss = fields ; ss->name ; ss++) {
186 if (ss->flags & FLAG_ARRAY) {
187 if (!idx || (strcmp(ss->name, arg) != 0))
189 if (ss->max_idx > 0 && array_idx >= ss->max_idx)
192 if (strcmp(ss->name, field) != 0)
201 static errcode_t parse_uint(struct field_set_info *info, char *arg)
211 u.ptr8 = (__u8 *) info->ptr;
212 if (info->flags & FLAG_ARRAY)
213 u.ptr8 += array_idx * info->size;
215 num = strtoul(arg, &tmp, 0);
217 fprintf(stderr, "Couldn't parse '%s' for field %s.\n",
221 switch (info->size) {
235 static errcode_t parse_int(struct field_set_info *info, char *arg)
243 num = strtol(arg, &tmp, 0);
245 fprintf(stderr, "Couldn't parse '%s' for field %s.\n",
249 switch (info->size) {
251 ptr32 = (__s32 *) info->ptr;
255 ptr16 = (__s16 *) info->ptr;
259 ptr8 = (__s8 *) info->ptr;
266 static errcode_t parse_string(struct field_set_info *info, char *arg)
268 char *cp = (char *) info->ptr;
270 if (strlen(arg) >= info->size) {
271 fprintf(stderr, "Error maximum size for %s is %d.\n",
272 info->name, info->size);
279 static errcode_t parse_time(struct field_set_info *info, char *arg)
284 ptr32 = (__u32 *) info->ptr;
286 if (strcmp(arg, "now") == 0) {
290 memset(&ts, 0, sizeof(ts));
292 strptime(arg, "%Y%m%d%H%M%S", &ts);
294 sscanf(arg, "%4d%2d%2d%2d%2d%2d", &ts.tm_year, &ts.tm_mon,
295 &ts.tm_mday, &ts.tm_hour, &ts.tm_min, &ts.tm_sec);
298 if (ts.tm_year < 0 || ts.tm_mon < 0 || ts.tm_mon > 11 ||
299 ts.tm_mday < 0 || ts.tm_mday > 31 || ts.tm_hour > 23 ||
300 ts.tm_min > 59 || ts.tm_sec > 61)
303 if (ts.tm_mday == 0) {
304 /* Try it as an integer... */
305 return parse_uint(info, arg);
307 *ptr32 = mktime(&ts);
311 static errcode_t parse_uuid(struct field_set_info *info, char *arg)
313 unsigned char * p = (unsigned char *) info->ptr;
315 if ((strcasecmp(arg, "null") == 0) ||
316 (strcasecmp(arg, "clear") == 0)) {
318 } else if (strcasecmp(arg, "time") == 0) {
319 uuid_generate_time(p);
320 } else if (strcasecmp(arg, "random") == 0) {
322 } else if (uuid_parse(arg, p)) {
323 fprintf(stderr, "Invalid UUID format: %s\n", arg);
329 static errcode_t parse_hashalg(struct field_set_info *info, char *arg)
332 unsigned char *p = (unsigned char *) info->ptr;
334 hashv = e2p_string2hash(arg);
336 fprintf(stderr, "Invalid hash algorithm: %s\n", arg);
343 static errcode_t parse_bmap(struct field_set_info *info, char *arg)
350 num = strtoul(arg, &tmp, 0);
352 fprintf(stderr, "Couldn't parse '%s' for field %s.\n",
358 retval = ext2fs_bmap(current_fs, set_ino, &set_inode, 0, BMAP_SET,
361 com_err("set_inode", retval, "while setting block map");
367 static void print_possible_fields(struct field_set_info *fields)
369 struct field_set_info *ss;
370 const char *type, *cmd;
372 char name[40], idx[40];
374 if (fields == super_fields) {
376 cmd = "set_super_value";
383 fprintf(f, "%s fields supported by the %s command:\n", type, cmd);
385 for (ss = fields ; ss->name ; ss++) {
387 if (ss->func == parse_string)
389 else if (ss->func == parse_int)
391 else if (ss->func == parse_uint)
392 type = "unsigned integer";
393 else if (ss->func == parse_uuid)
395 else if (ss->func == parse_hashalg)
396 type = "hash algorithm";
397 else if (ss->func == parse_time)
399 else if (ss->func == parse_bmap)
400 type = "set physical->logical block map";
401 strcpy(name, ss->name);
402 if (ss->flags & FLAG_ARRAY) {
404 sprintf(idx, "[%d]", ss->max_idx);
409 fprintf(f, "\t%-20s\t%s\n", name, type);
415 void do_set_super(int argc, char *argv[])
417 const char *usage = "<field> <value>\n"
418 "\t\"set_super_value -l\" will list the names of "
419 "superblock fields\n\twhich can be set.";
420 static struct field_set_info *ss;
422 if ((argc == 2) && !strcmp(argv[1], "-l")) {
423 print_possible_fields(super_fields);
427 if (common_args_process(argc, argv, 3, 3, "set_super_value",
431 if ((ss = find_field(super_fields, argv[1])) == 0) {
432 com_err(argv[0], 0, "invalid field specifier: %s", argv[1]);
435 set_sb = *current_fs->super;
436 if (ss->func(ss, argv[2]) == 0) {
437 *current_fs->super = set_sb;
438 ext2fs_mark_super_dirty(current_fs);
442 void do_set_inode(int argc, char *argv[])
444 const char *usage = "<inode> <field> <value>\n"
445 "\t\"set_inode_field -l\" will list the names of "
446 "the fields in an ext2 inode\n\twhich can be set.";
447 static struct field_set_info *ss;
449 if ((argc == 2) && !strcmp(argv[1], "-l")) {
450 print_possible_fields(inode_fields);
454 if (common_args_process(argc, argv, 4, 4, "set_inode",
458 if ((ss = find_field(inode_fields, argv[2])) == 0) {
459 com_err(argv[0], 0, "invalid field specifier: %s", argv[2]);
463 set_ino = string_to_inode(argv[1]);
467 if (debugfs_read_inode(set_ino, &set_inode, argv[1]))
470 if (ss->func(ss, argv[3]) == 0) {
471 if (debugfs_write_inode(set_ino, &set_inode, argv[1]))