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 600 /* for inclusion of strptime() and strtoull */
15 #define STRTOULL strtoull
17 #define STRTOULL strtoul
28 #include <sys/types.h>
41 #include "uuid/uuid.h"
43 #include "support/quotaio.h"
45 static struct ext2_super_block set_sb;
46 static struct ext2_inode_large set_inode;
47 static struct ext2_group_desc set_gd;
48 static struct ext4_group_desc set_gd4;
49 static struct mmp_struct set_mmp;
51 static ext2_ino_t set_ino;
54 #define FLAG_ARRAY 0x0001
55 #define FLAG_ALIAS 0x0002 /* Data intersects with other field */
56 #define FLAG_CSUM 0x0004
58 struct field_set_info {
63 errcode_t (*func)(struct field_set_info *info, char *field, char *arg);
68 static errcode_t parse_uint(struct field_set_info *info, char *field, char *arg);
69 static errcode_t parse_int(struct field_set_info *info, char *field, char *arg);
70 static errcode_t parse_string(struct field_set_info *info, char *field, char *arg);
71 static errcode_t parse_uuid(struct field_set_info *info, char *field, char *arg);
72 static errcode_t parse_hashalg(struct field_set_info *info, char *field, char *arg);
73 static errcode_t parse_encoding(struct field_set_info *info, char *field, char *arg);
74 static errcode_t parse_time(struct field_set_info *info, char *field, char *arg);
75 static errcode_t parse_bmap(struct field_set_info *info, char *field, char *arg);
76 static errcode_t parse_gd_csum(struct field_set_info *info, char *field, char *arg);
77 static errcode_t parse_inode_csum(struct field_set_info *info, char *field,
79 static errcode_t parse_mmp_clear(struct field_set_info *info, char *field,
82 #if __GNUC_PREREQ (4, 6) || defined(__clang__)
83 #pragma GCC diagnostic push
84 #pragma GCC diagnostic ignored "-Wmissing-field-initializers"
87 static struct field_set_info super_fields[] = {
88 { "inodes_count", &set_sb.s_inodes_count, NULL, 4, parse_uint },
89 { "blocks_count", &set_sb.s_blocks_count, &set_sb.s_blocks_count_hi,
91 { "r_blocks_count", &set_sb.s_r_blocks_count,
92 &set_sb.s_r_blocks_count_hi, 4, parse_uint },
93 { "free_blocks_count", &set_sb.s_free_blocks_count,
94 &set_sb.s_free_blocks_hi, 4, parse_uint },
95 { "free_inodes_count", &set_sb.s_free_inodes_count, NULL, 4, parse_uint },
96 { "first_data_block", &set_sb.s_first_data_block, NULL, 4, parse_uint },
97 { "log_block_size", &set_sb.s_log_block_size, NULL, 4, parse_uint },
98 { "log_cluster_size", &set_sb.s_log_cluster_size, NULL, 4, parse_int },
99 { "blocks_per_group", &set_sb.s_blocks_per_group, NULL, 4, parse_uint },
100 { "clusters_per_group", &set_sb.s_clusters_per_group, NULL, 4, parse_uint },
101 { "inodes_per_group", &set_sb.s_inodes_per_group, NULL, 4, parse_uint },
102 { "mtime", &set_sb.s_mtime, &set_sb.s_mtime_hi, 5, parse_time },
103 { "wtime", &set_sb.s_wtime, &set_sb.s_wtime_hi, 5, parse_time },
104 { "mnt_count", &set_sb.s_mnt_count, NULL, 2, parse_uint },
105 { "max_mnt_count", &set_sb.s_max_mnt_count, NULL, 2, parse_int },
107 { "state", &set_sb.s_state, NULL, 2, parse_uint },
108 { "errors", &set_sb.s_errors, NULL, 2, parse_uint },
109 { "minor_rev_level", &set_sb.s_minor_rev_level, NULL, 2, parse_uint },
110 { "lastcheck", &set_sb.s_lastcheck, &set_sb.s_lastcheck_hi, 5,
112 { "checkinterval", &set_sb.s_checkinterval, NULL, 4, parse_uint },
113 { "creator_os", &set_sb.s_creator_os, NULL, 4, parse_uint },
114 { "rev_level", &set_sb.s_rev_level, NULL, 4, parse_uint },
115 { "def_resuid", &set_sb.s_def_resuid, NULL, 2, parse_uint },
116 { "def_resgid", &set_sb.s_def_resgid, NULL, 2, parse_uint },
117 { "first_ino", &set_sb.s_first_ino, NULL, 4, parse_uint },
118 { "inode_size", &set_sb.s_inode_size, NULL, 2, parse_uint },
119 { "block_group_nr", &set_sb.s_block_group_nr, NULL, 2, parse_uint },
120 { "feature_compat", &set_sb.s_feature_compat, NULL, 4, parse_uint },
121 { "feature_incompat", &set_sb.s_feature_incompat, NULL, 4, parse_uint },
122 { "feature_ro_compat", &set_sb.s_feature_ro_compat, NULL, 4, parse_uint },
123 { "uuid", &set_sb.s_uuid, NULL, 16, parse_uuid },
124 { "volume_name", &set_sb.s_volume_name, NULL, 16, parse_string },
125 { "last_mounted", &set_sb.s_last_mounted, NULL, 64, parse_string },
126 { "algorithm_usage_bitmap", &set_sb.s_algorithm_usage_bitmap, NULL,
128 { "prealloc_blocks", &set_sb.s_prealloc_blocks, NULL, 1, parse_uint },
129 { "prealloc_dir_blocks", &set_sb.s_prealloc_dir_blocks, NULL, 1,
131 { "reserved_gdt_blocks", &set_sb.s_reserved_gdt_blocks, NULL, 2,
133 { "journal_uuid", &set_sb.s_journal_uuid, NULL, 16, parse_uuid },
134 { "journal_inum", &set_sb.s_journal_inum, NULL, 4, parse_uint },
135 { "journal_dev", &set_sb.s_journal_dev, NULL, 4, parse_uint },
136 { "last_orphan", &set_sb.s_last_orphan, NULL, 4, parse_uint },
137 { "hash_seed", &set_sb.s_hash_seed, NULL, 16, parse_uuid },
138 { "def_hash_version", &set_sb.s_def_hash_version, NULL, 1, parse_hashalg },
139 { "jnl_backup_type", &set_sb.s_jnl_backup_type, NULL, 1, parse_uint },
140 { "desc_size", &set_sb.s_desc_size, NULL, 2, parse_uint },
141 { "default_mount_opts", &set_sb.s_default_mount_opts, NULL, 4, parse_uint },
142 { "first_meta_bg", &set_sb.s_first_meta_bg, NULL, 4, parse_uint },
143 { "mkfs_time", &set_sb.s_mkfs_time, &set_sb.s_mkfs_time_hi, 5,
145 { "jnl_blocks", &set_sb.s_jnl_blocks[0], NULL, 4, parse_uint, FLAG_ARRAY,
147 { "min_extra_isize", &set_sb.s_min_extra_isize, NULL, 2, parse_uint },
148 { "want_extra_isize", &set_sb.s_want_extra_isize, NULL, 2, parse_uint },
149 { "flags", &set_sb.s_flags, NULL, 4, parse_uint },
150 { "raid_stride", &set_sb.s_raid_stride, NULL, 2, parse_uint },
151 { "mmp_interval", &set_sb.s_mmp_update_interval, NULL, 2, parse_uint },
152 { "mmp_block", &set_sb.s_mmp_block, NULL, 8, parse_uint },
153 { "raid_stripe_width", &set_sb.s_raid_stripe_width, NULL, 4, parse_uint },
154 { "log_groups_per_flex", &set_sb.s_log_groups_per_flex, NULL, 1, parse_uint },
155 { "kbytes_written", &set_sb.s_kbytes_written, NULL, 8, parse_uint },
156 { "snapshot_inum", &set_sb.s_snapshot_inum, NULL, 4, parse_uint },
157 { "snapshot_id", &set_sb.s_snapshot_id, NULL, 4, parse_uint },
158 { "snapshot_r_blocks_count", &set_sb.s_snapshot_r_blocks_count,
159 NULL, 8, parse_uint },
160 { "snapshot_list", &set_sb.s_snapshot_list, NULL, 4, parse_uint },
161 { "mount_opts", &set_sb.s_mount_opts, NULL, 64, parse_string },
162 { "usr_quota_inum", &set_sb.s_usr_quota_inum, NULL, 4, parse_uint },
163 { "grp_quota_inum", &set_sb.s_grp_quota_inum, NULL, 4, parse_uint },
164 { "prj_quota_inum", &set_sb.s_prj_quota_inum, NULL, 4, parse_uint },
165 { "overhead_clusters", &set_sb.s_overhead_clusters, NULL, 4, parse_uint },
166 { "backup_bgs", &set_sb.s_backup_bgs[0], NULL, 4, parse_uint,
168 { "checksum", &set_sb.s_checksum, NULL, 4, parse_uint },
169 { "checksum_type", &set_sb.s_checksum_type, NULL, 1, parse_uint },
170 { "encryption_level", &set_sb.s_encryption_level, NULL, 1, parse_uint },
171 { "error_count", &set_sb.s_error_count, NULL, 4, parse_uint },
172 { "first_error_time", &set_sb.s_first_error_time,
173 &set_sb.s_first_error_time_hi, 5, parse_time },
174 { "first_error_ino", &set_sb.s_first_error_ino, NULL, 4, parse_uint },
175 { "first_error_block", &set_sb.s_first_error_block, NULL, 8, parse_uint },
176 { "first_error_func", &set_sb.s_first_error_func, NULL, 32, parse_string },
177 { "first_error_line", &set_sb.s_first_error_line, NULL, 4, parse_uint },
178 { "last_error_time", &set_sb.s_last_error_time,
179 &set_sb.s_last_error_time_hi, 5, parse_time },
180 { "last_error_ino", &set_sb.s_last_error_ino, NULL, 4, parse_uint },
181 { "last_error_block", &set_sb.s_last_error_block, NULL, 8, parse_uint },
182 { "last_error_func", &set_sb.s_last_error_func, NULL, 32, parse_string },
183 { "last_error_line", &set_sb.s_last_error_line, NULL, 4, parse_uint },
184 { "encrypt_algos", &set_sb.s_encrypt_algos, NULL, 1, parse_uint,
186 { "encrypt_pw_salt", &set_sb.s_encrypt_pw_salt, NULL, 16, parse_uuid },
187 { "lpf_ino", &set_sb.s_lpf_ino, NULL, 4, parse_uint },
188 { "checksum_seed", &set_sb.s_checksum_seed, NULL, 4, parse_uint },
189 { "encoding", &set_sb.s_encoding, NULL, 2, parse_encoding },
190 { "orphan_file_inum", &set_sb.s_orphan_file_inum, NULL, 4, parse_uint },
194 static struct field_set_info inode_fields[] = {
195 { "mode", &set_inode.i_mode, NULL, 2, parse_uint },
196 { "uid", &set_inode.i_uid, &set_inode.osd2.linux2.l_i_uid_high,
198 { "size", &set_inode.i_size, &set_inode.i_size_high, 4, parse_uint },
199 { "atime", &set_inode.i_atime, &set_inode.i_atime_extra,
201 { "ctime", &set_inode.i_ctime, &set_inode.i_ctime_extra,
203 { "mtime", &set_inode.i_mtime, &set_inode.i_mtime_extra,
205 { "dtime", &set_inode.i_dtime, NULL,
207 { "gid", &set_inode.i_gid, &set_inode.osd2.linux2.l_i_gid_high,
209 { "links_count", &set_inode.i_links_count, NULL, 2, parse_uint },
210 /* Special case: i_blocks is 4 bytes, i_blocks_high is 2 bytes */
211 { "blocks", &set_inode.i_blocks, &set_inode.osd2.linux2.l_i_blocks_hi,
213 { "flags", &set_inode.i_flags, NULL, 4, parse_uint },
214 { "version", &set_inode.osd1.linux1.l_i_version,
215 &set_inode.i_version_hi, 4, parse_uint },
216 { "translator", &set_inode.osd1.hurd1.h_i_translator, NULL,
217 4, parse_uint, FLAG_ALIAS },
218 { "block", &set_inode.i_block[0], NULL, 4, parse_uint, FLAG_ARRAY,
220 { "block[IND]", &set_inode.i_block[EXT2_IND_BLOCK], NULL, 4, parse_uint },
221 { "block[DIND]", &set_inode.i_block[EXT2_DIND_BLOCK], NULL, 4, parse_uint },
222 { "block[TIND]", &set_inode.i_block[EXT2_TIND_BLOCK], NULL, 4, parse_uint },
223 { "generation", &set_inode.i_generation, NULL, 4, parse_uint },
224 /* Special case: i_file_acl_high is 2 bytes */
225 { "file_acl", &set_inode.i_file_acl,
226 &set_inode.osd2.linux2.l_i_file_acl_high, 6, parse_uint },
227 { "faddr", &set_inode.i_faddr, NULL, 4, parse_uint },
228 { "frag", &set_inode.osd2.hurd2.h_i_frag, NULL, 1, parse_uint, FLAG_ALIAS },
229 { "fsize", &set_inode.osd2.hurd2.h_i_fsize, NULL, 1, parse_uint },
230 { "checksum", &set_inode.osd2.linux2.l_i_checksum_lo,
231 &set_inode.i_checksum_hi, 2, parse_inode_csum, FLAG_CSUM },
232 { "author", &set_inode.osd2.hurd2.h_i_author, NULL,
233 4, parse_uint, FLAG_ALIAS },
234 { "extra_isize", &set_inode.i_extra_isize, NULL,
236 { "ctime_extra", &set_inode.i_ctime_extra, NULL,
237 4, parse_uint, FLAG_ALIAS },
238 { "mtime_extra", &set_inode.i_mtime_extra, NULL,
239 4, parse_uint, FLAG_ALIAS },
240 { "atime_extra", &set_inode.i_atime_extra, NULL,
241 4, parse_uint, FLAG_ALIAS },
242 { "crtime", &set_inode.i_crtime, &set_inode.i_crtime_extra,
244 { "crtime_extra", &set_inode.i_crtime_extra, NULL,
245 4, parse_uint, FLAG_ALIAS },
246 { "projid", &set_inode.i_projid, NULL, 4, parse_uint },
247 { "bmap", NULL, NULL, 4, parse_bmap, FLAG_ARRAY },
251 static struct field_set_info ext2_bg_fields[] = {
252 { "block_bitmap", &set_gd.bg_block_bitmap, NULL, 4, parse_uint },
253 { "inode_bitmap", &set_gd.bg_inode_bitmap, NULL, 4, parse_uint },
254 { "inode_table", &set_gd.bg_inode_table, NULL, 4, parse_uint },
255 { "free_blocks_count", &set_gd.bg_free_blocks_count, NULL, 2, parse_uint },
256 { "free_inodes_count", &set_gd.bg_free_inodes_count, NULL, 2, parse_uint },
257 { "used_dirs_count", &set_gd.bg_used_dirs_count, NULL, 2, parse_uint },
258 { "flags", &set_gd.bg_flags, NULL, 2, parse_uint },
259 { "itable_unused", &set_gd.bg_itable_unused, NULL, 2, parse_uint },
260 { "checksum", &set_gd.bg_checksum, NULL, 2, parse_gd_csum },
264 static struct field_set_info ext4_bg_fields[] = {
265 { "block_bitmap", &set_gd4.bg_block_bitmap,
266 &set_gd4.bg_block_bitmap_hi, 4, parse_uint },
267 { "inode_bitmap", &set_gd4.bg_inode_bitmap,
268 &set_gd4.bg_inode_bitmap_hi, 4, parse_uint },
269 { "inode_table", &set_gd4.bg_inode_table,
270 &set_gd4.bg_inode_table_hi, 4, parse_uint },
271 { "free_blocks_count", &set_gd4.bg_free_blocks_count,
272 &set_gd4.bg_free_blocks_count_hi, 2, parse_uint },
273 { "free_inodes_count", &set_gd4.bg_free_inodes_count,
274 &set_gd4.bg_free_inodes_count_hi, 2, parse_uint },
275 { "used_dirs_count", &set_gd4.bg_used_dirs_count,
276 &set_gd4.bg_used_dirs_count_hi, 2, parse_uint },
277 { "flags", &set_gd4.bg_flags, NULL, 2, parse_uint },
278 { "exclude_bitmap", &set_gd4.bg_exclude_bitmap_lo,
279 &set_gd4.bg_exclude_bitmap_hi, 4, parse_uint },
280 { "block_bitmap_csum", &set_gd4.bg_block_bitmap_csum_lo,
281 &set_gd4.bg_block_bitmap_csum_hi, 2, parse_uint },
282 { "inode_bitmap_csum", &set_gd4.bg_inode_bitmap_csum_lo,
283 &set_gd4.bg_inode_bitmap_csum_hi, 2, parse_uint },
284 { "itable_unused", &set_gd4.bg_itable_unused,
285 &set_gd4.bg_itable_unused_hi, 2, parse_uint },
286 { "checksum", &set_gd4.bg_checksum, NULL, 2, parse_gd_csum },
290 static struct field_set_info mmp_fields[] = {
291 { "clear", &set_mmp.mmp_magic, NULL, sizeof(set_mmp),
292 parse_mmp_clear, FLAG_ALIAS },
293 { "magic", &set_mmp.mmp_magic, NULL, 4, parse_uint },
294 { "seq", &set_mmp.mmp_seq, NULL, 4, parse_uint },
295 { "time", &set_mmp.mmp_time, NULL, 8, parse_uint },
296 { "nodename", &set_mmp.mmp_nodename, NULL, sizeof(set_mmp.mmp_nodename),
298 { "bdevname", &set_mmp.mmp_bdevname, NULL, sizeof(set_mmp.mmp_bdevname),
300 { "check_interval", &set_mmp.mmp_check_interval, NULL, 2, parse_uint },
301 { "checksum", &set_mmp.mmp_checksum, NULL, 4, parse_uint },
304 #if __GNUC_PREREQ (4, 6)
305 #pragma GCC diagnostic pop
311 static void do_verify_field_set_info(struct field_set_info *fields,
312 const void *data, size_t size)
314 struct field_set_info *ss, *ss2;
315 const char *begin = (char *)data;
316 const char *end = begin + size;
318 for (ss = fields ; ss->name ; ss++) {
323 assert(!ptr || (ptr >= begin && ptr < end));
325 assert(!ptr || (ptr >= begin && ptr < end));
330 for (ss2 = fields ; ss2 != ss ; ss2++) {
331 /* Check duplicate names */
332 assert(strcmp(ss->name, ss2->name));
334 if (ss->flags & FLAG_ALIAS || ss2->flags & FLAG_ALIAS)
336 /* Check false aliases, might be copy-n-paste error */
337 assert(!ss->ptr || (ss->ptr != ss2->ptr &&
338 ss->ptr != ss2->ptr2));
339 assert(!ss->ptr2 || (ss->ptr2 != ss2->ptr &&
340 ss->ptr2 != ss2->ptr2));
345 int main(int argc, char **argv)
347 do_verify_field_set_info(super_fields, &set_sb, sizeof(set_sb));
348 do_verify_field_set_info(inode_fields, &set_inode, sizeof(set_inode));
349 do_verify_field_set_info(ext2_bg_fields, &set_gd, sizeof(set_gd));
350 do_verify_field_set_info(ext4_bg_fields, &set_gd4, sizeof(set_gd4));
351 do_verify_field_set_info(mmp_fields, &set_mmp, sizeof(set_mmp));
355 ext2_filsys current_fs;
356 ext2_ino_t root, cwd;
358 #endif /* UNITTEST */
360 static int check_suffix(const char *field)
362 int len = strlen(field);
367 if (!strcmp(field, "_lo"))
369 if (!strcmp(field, "_hi"))
374 static struct field_set_info *find_field(struct field_set_info *fields,
377 struct field_set_info *ss;
379 char *arg, *delim, *idx, *tmp;
380 int suffix, prefix_len;
382 if (fields == super_fields)
384 else if (fields == inode_fields)
388 prefix_len = strlen(prefix);
389 if (strncmp(field, prefix, prefix_len) == 0)
392 arg = malloc(strlen(field)+1);
397 idx = strchr(arg, '[');
400 delim = idx + strlen(idx) - 1;
401 if (!*idx || *delim != ']')
407 * Can we parse the number?
410 array_idx = strtol(idx, &tmp, 0);
419 * If there is a valid _hi or a _lo suffix, strip it off
421 suffix = check_suffix(arg);
423 arg[strlen(arg)-3] = 0;
425 for (ss = fields ; ss->name ; ss++) {
426 if (suffix && ss->ptr2 == 0)
428 if (ss->flags & FLAG_ARRAY) {
429 if (!idx || (strcmp(ss->name, arg) != 0))
431 if (ss->max_idx > 0 && array_idx >= ss->max_idx)
434 if (strcmp(ss->name, arg) != 0)
445 * Note: info->size == 6 is special; this means a base size 4 bytes,
446 * and secondary (high) size of 2 bytes. This is needed for the
447 * special case of i_blocks_high and i_file_acl_high.
449 * Similarly, info->size == 5 is for superblock timestamps, which have
450 * a 4-byte primary field and a 1-byte _hi field.
452 static errcode_t parse_uint(struct field_set_info *info, char *field,
455 unsigned long long n, num, mask, limit;
456 int suffix = check_suffix(field);
458 void *field1 = info->ptr, *field2 = info->ptr2;
459 unsigned int size = (info->size == 6 || info->size == 5) ? 4 :
475 u.ptr8 = (__u8 *) field1;
476 if (info->flags & FLAG_ARRAY)
477 u.ptr8 += array_idx * info->size;
480 num = STRTOULL(arg, &tmp, 0);
482 fprintf(stderr, "Couldn't parse '%s' for field %s.\n",
486 mask = ~0ULL >> ((8 - size) * 8);
487 limit = ~0ULL >> ((8 - info->size) * 8);
488 if (field2 && (info->size != 6 && info->size != 5))
489 limit = ~0ULL >> ((8 - info->size*2) * 8);
492 fprintf(stderr, "Value '%s' exceeds field %s maximum %llu.\n",
493 arg, info->name, limit);
513 n = (size == 8) ? 0 : (num >> (size*8));
514 u.ptr8 = (__u8 *) field2;
515 if (info->size > size)
516 size = info->size - size;
519 /* Should never get here */
521 "64-bit field %s has a second 64-bit field defined; BUG?!?\n",
538 static errcode_t parse_int(struct field_set_info *info,
539 char *field EXT2FS_ATTR((unused)), char *arg)
547 num = strtol(arg, &tmp, 0);
549 fprintf(stderr, "Couldn't parse '%s' for field %s.\n",
553 switch (info->size) {
555 ptr32 = (__s32 *) info->ptr;
559 ptr16 = (__s16 *) info->ptr;
563 ptr8 = (__s8 *) info->ptr;
570 static errcode_t parse_string(struct field_set_info *info,
571 char *field EXT2FS_ATTR((unused)), char *arg)
573 char *cp = (char *) info->ptr;
575 if (strlen(arg) >= info->size) {
576 fprintf(stderr, "Error maximum size for %s is %d.\n",
577 info->name, info->size);
584 static errcode_t parse_time(struct field_set_info *info,
585 char *field, char *arg)
589 __u32 *ptr_low, *ptr_high;
591 if (check_suffix(field))
592 return parse_uint(info, field, arg);
594 ptr_low = (__u32 *) info->ptr;
595 ptr_high = (__u32 *) info->ptr2;
597 t = string_to_time(arg);
600 fprintf(stderr, "Couldn't parse '%s' for field %s.\n",
605 t_high = ((t - (__s32)t) >> 32) & EXT4_EPOCH_MASK;
608 *ptr_high = (*ptr_high & ~EXT4_EPOCH_MASK) | t_high;
612 static errcode_t parse_uuid(struct field_set_info *info,
613 char *field EXT2FS_ATTR((unused)), char *arg)
615 unsigned char * p = (unsigned char *) info->ptr;
617 if ((strcasecmp(arg, "null") == 0) ||
618 (strcasecmp(arg, "clear") == 0)) {
620 } else if (strcasecmp(arg, "time") == 0) {
621 uuid_generate_time(p);
622 } else if (strcasecmp(arg, "random") == 0) {
624 } else if (uuid_parse(arg, p)) {
625 fprintf(stderr, "Invalid UUID format: %s\n", arg);
631 static errcode_t parse_hashalg(struct field_set_info *info,
632 char *field EXT2FS_ATTR((unused)), char *arg)
635 unsigned char *p = (unsigned char *) info->ptr;
637 hashv = e2p_string2hash(arg);
639 fprintf(stderr, "Invalid hash algorithm: %s\n", arg);
646 static errcode_t parse_encoding(struct field_set_info *info,
647 char *field EXT2FS_ATTR((unused)), char *arg)
650 unsigned char *p = (unsigned char *) info->ptr;
652 encoding = e2p_str2encoding(arg);
654 return parse_uint(info, field, arg);
659 static errcode_t parse_bmap(struct field_set_info *info,
660 char *field EXT2FS_ATTR((unused)), char *arg)
666 blk = strtoull(arg, &tmp, 0);
668 fprintf(stderr, "Couldn't parse '%s' for field %s.\n",
673 retval = ext2fs_bmap2(current_fs, set_ino,
674 (struct ext2_inode *) &set_inode,
675 NULL, BMAP_ALLOC | BMAP_SET, array_idx, NULL,
678 com_err("set_inode", retval, "while setting block map");
683 static errcode_t parse_gd_csum(struct field_set_info *info, char *field,
686 __u16 *checksum = info->ptr;
688 if (strcmp(arg, "calc") == 0) {
689 *checksum = ext2fs_group_desc_csum(current_fs, set_bg);
690 printf("Checksum set to 0x%04x\n", *checksum);
693 return parse_uint(info, field, arg);
696 static errcode_t parse_inode_csum(struct field_set_info *info, char *field,
699 errcode_t retval = 0;
701 int is_large_inode = 0;
703 if (strcmp(arg, "calc") == 0) {
704 size_t sz = EXT2_INODE_SIZE(current_fs->super);
705 struct ext2_inode_large *tmp_inode = NULL;
707 retval = ext2fs_get_mem(sz, &tmp_inode);
711 retval = ext2fs_read_inode_full(current_fs, set_ino,
712 (struct ext2_inode *) tmp_inode,
717 #ifdef WORDS_BIGENDIAN
718 ext2fs_swap_inode_full(current_fs, tmp_inode,
722 if (sz > EXT2_GOOD_OLD_INODE_SIZE)
725 retval = ext2fs_inode_csum_set(current_fs, set_ino,
729 #ifdef WORDS_BIGENDIAN
730 crc = set_inode.i_checksum_lo =
731 ext2fs_swab16(tmp_inode->i_checksum_lo);
734 crc = set_inode.i_checksum_lo = tmp_inode->i_checksum_lo;
736 if (is_large_inode &&
737 set_inode.i_extra_isize >=
738 (offsetof(struct ext2_inode_large,
740 EXT2_GOOD_OLD_INODE_SIZE)) {
741 #ifdef WORDS_BIGENDIAN
742 set_inode.i_checksum_lo =
743 ext2fs_swab16(tmp_inode->i_checksum_lo);
745 set_inode.i_checksum_hi = tmp_inode->i_checksum_hi;
747 crc |= ((__u32)set_inode.i_checksum_hi) << 16;
749 printf("Checksum set to 0x%08x\n", crc);
751 ext2fs_free_mem(&tmp_inode);
754 return parse_uint(info, field, arg);
757 static void print_possible_fields(struct field_set_info *fields)
759 struct field_set_info *ss;
760 const char *type, *cmd;
762 char name[40], idx[40];
764 if (fields == super_fields) {
766 cmd = "set_super_value";
767 } else if (fields == inode_fields) {
770 } else if (fields == mmp_fields) {
772 cmd = "set_mmp_value";
774 type = "Block group descriptor";
775 cmd = "set_block_group";
779 fprintf(f, "%s fields supported by the %s command:\n", type, cmd);
781 for (ss = fields ; ss->name ; ss++) {
783 if (ss->func == parse_string)
785 else if (ss->func == parse_int)
787 else if (ss->func == parse_uint)
788 type = "unsigned integer";
789 else if (ss->func == parse_uuid)
791 else if (ss->func == parse_hashalg)
792 type = "hash algorithm";
793 else if (ss->func == parse_time)
795 else if (ss->func == parse_bmap)
796 type = "set physical->logical block map";
797 else if (ss->func == parse_gd_csum)
798 type = "unsigned integer OR \"calc\"";
799 strcpy(name, ss->name);
800 if (ss->flags & FLAG_ARRAY) {
802 sprintf(idx, "[%d]", ss->max_idx);
808 strcat(name, "[_hi|_lo]");
809 fprintf(f, "\t%-25s\t%s\n", name, type);
815 void do_set_super(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
816 void *infop EXT2FS_ATTR((unused)))
818 const char *usage = "<field> <value>\n"
819 "\t\"set_super_value -l\" will list the names of "
820 "superblock fields\n\twhich can be set.";
821 static struct field_set_info *ss;
823 if ((argc == 2) && !strcmp(argv[1], "-l")) {
824 print_possible_fields(super_fields);
828 if (common_args_process(argc, argv, 3, 3, "set_super_value",
832 if ((ss = find_field(super_fields, argv[1])) == 0) {
833 com_err(argv[0], 0, "invalid field specifier: %s", argv[1]);
836 set_sb = *current_fs->super;
837 if (ss->func(ss, argv[1], argv[2]) == 0) {
838 *current_fs->super = set_sb;
839 ext2fs_mark_super_dirty(current_fs);
843 void do_set_inode(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
844 void *infop EXT2FS_ATTR((unused)))
846 const char *usage = "<inode> <field> <value>\n"
847 "\t\"set_inode_field -l\" will list the names of "
848 "the fields in an ext2 inode\n\twhich can be set.";
849 static struct field_set_info *ss;
851 if ((argc == 2) && !strcmp(argv[1], "-l")) {
852 print_possible_fields(inode_fields);
856 if (common_args_process(argc, argv, 4, 4, "set_inode",
860 if ((ss = find_field(inode_fields, argv[2])) == 0) {
861 com_err(argv[0], 0, "invalid field specifier: %s", argv[2]);
865 set_ino = string_to_inode(argv[1]);
869 if (debugfs_read_inode2(set_ino,
870 (struct ext2_inode *) &set_inode, argv[1],
872 (ss->flags & FLAG_CSUM) ?
873 READ_INODE_NOCSUM : 0))
876 if (ss->func(ss, argv[2], argv[3]) == 0) {
877 debugfs_write_inode2(set_ino,
878 (struct ext2_inode *) &set_inode,
879 argv[1], sizeof(set_inode),
880 (ss->flags & FLAG_CSUM) ?
881 WRITE_INODE_NOCSUM : 0);
885 void do_set_block_group_descriptor(int argc, char *argv[],
886 int sci_idx EXT2FS_ATTR((unused)),
887 void *infop EXT2FS_ATTR((unused)))
889 const char *usage = "<bg number> <field> <value>\n"
890 "\t\"set_block_group -l\" will list the names of "
891 "the fields in a block group descriptor\n\twhich can be set.";
892 struct field_set_info *table;
893 struct field_set_info *ss;
899 * Determine whether we are editing an ext2 or ext4 block group
900 * descriptor. Descriptors larger than ext4_group_desc cannot
901 * have their fields edited yet, because they do not have any
902 * names assigned. When that happens, this function needs to
903 * be updated for the new descriptor struct and fields.
906 EXT2_DESC_SIZE(current_fs->super) >= EXT2_MIN_DESC_SIZE_64BIT) {
907 table = ext4_bg_fields;
909 size = sizeof(set_gd4);
911 table = ext2_bg_fields;
913 size = sizeof(set_gd);
916 if ((argc == 2) && !strcmp(argv[1], "-l")) {
917 print_possible_fields(table);
921 if (common_args_process(argc, argv, 4, 4, "set_block_group",
925 set_bg = strtoul(argv[1], &end, 0);
927 com_err(argv[0], 0, "invalid block group number: %s", argv[1]);
931 if (set_bg >= current_fs->group_desc_count) {
932 com_err(argv[0], 0, "block group number too big: %d", set_bg);
936 if ((ss = find_field(table, argv[2])) == 0) {
937 com_err(argv[0], 0, "invalid field specifier: %s", argv[2]);
941 target = ext2fs_group_desc(current_fs, current_fs->group_desc, set_bg);
942 memcpy(edit, target, size);
943 if (ss->func(ss, argv[2], argv[3]) == 0) {
944 memcpy(target, edit, size);
945 ext2fs_mark_super_dirty(current_fs);
949 static errcode_t parse_mmp_clear(struct field_set_info *info,
950 char *field EXT2FS_ATTR((unused)),
951 char *arg EXT2FS_ATTR((unused)))
955 retval = ext2fs_mmp_clear(current_fs);
957 com_err("set_mmp_value", retval, "while clearing MMP block\n");
959 memcpy(info->ptr, current_fs->mmp_buf, info->size);
961 return 1; /* we don't need the MMP block written again */
965 void do_set_mmp_value(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
966 void *infop EXT2FS_ATTR((unused)))
968 const char *usage = "<field> <value>\n"
969 "\t\"set_mmp_value -l\" will list the names of "
970 "MMP fields\n\twhich can be set.";
971 static struct field_set_info *smmp;
972 struct mmp_struct *mmp_s;
975 if (argc == 2 && strcmp(argv[1], "-l") == 0) {
976 print_possible_fields(mmp_fields);
980 if (check_fs_open(argv[0]))
983 if (current_fs->super->s_mmp_block == 0) {
984 com_err(argv[0], 0, "no MMP block allocated\n");
988 if (common_args_process(argc, argv, 2, 3, "set_mmp_value",
992 mmp_s = current_fs->mmp_buf;
994 retval = ext2fs_get_mem(current_fs->blocksize, &mmp_s);
996 com_err(argv[0], retval, "allocating MMP buffer\n");
999 retval = ext2fs_mmp_read(current_fs,
1000 current_fs->super->s_mmp_block, mmp_s);
1002 com_err(argv[0], retval, "reading MMP block %llu.\n",
1003 (long long)current_fs->super->s_mmp_block);
1004 ext2fs_free_mem(&mmp_s);
1007 current_fs->mmp_buf = mmp_s;
1010 smmp = find_field(mmp_fields, argv[1]);
1012 com_err(argv[0], 0, "invalid field specifier: %s", argv[1]);
1017 if (smmp->func(smmp, argv[1], argv[2]) == 0) {
1018 ext2fs_mmp_write(current_fs, current_fs->super->s_mmp_block,
1024 void do_set_mmp_value(int argc EXT2FS_ATTR((unused)),
1025 char *argv[] EXT2FS_ATTR((unused)),
1026 int sci_idx EXT2FS_ATTR((unused)),
1027 void *infop EXT2FS_ATTR((unused)))
1029 fprintf(stdout, "MMP is unsupported, please recompile with "