Home | History | Annotate | Download | only in debugfs
      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_large set_inode;
     44 static struct ext2_group_desc set_gd;
     45 static struct ext4_group_desc set_gd4;
     46 static struct mmp_struct set_mmp;
     47 static dgrp_t set_bg;
     48 static ext2_ino_t set_ino;
     49 static int array_idx;
     50 
     51 #define FLAG_ARRAY	0x0001
     52 
     53 struct field_set_info {
     54 	const char	*name;
     55 	void	*ptr;
     56 	void	*ptr2;
     57 	unsigned int	size;
     58 	errcode_t (*func)(struct field_set_info *info, char *field, char *arg);
     59 	int flags;
     60 	int max_idx;
     61 };
     62 
     63 static errcode_t parse_uint(struct field_set_info *info, char *field, char *arg);
     64 static errcode_t parse_int(struct field_set_info *info, char *field, char *arg);
     65 static errcode_t parse_string(struct field_set_info *info, char *field, char *arg);
     66 static errcode_t parse_uuid(struct field_set_info *info, char *field, char *arg);
     67 static errcode_t parse_hashalg(struct field_set_info *info, char *field, char *arg);
     68 static errcode_t parse_time(struct field_set_info *info, char *field, char *arg);
     69 static errcode_t parse_bmap(struct field_set_info *info, char *field, char *arg);
     70 static errcode_t parse_gd_csum(struct field_set_info *info, char *field, char *arg);
     71 static errcode_t parse_mmp_clear(struct field_set_info *info, char *field,
     72 				 char *arg);
     73 
     74 static struct field_set_info super_fields[] = {
     75 	{ "inodes_count", &set_sb.s_inodes_count, NULL, 4, parse_uint },
     76 	{ "blocks_count", &set_sb.s_blocks_count, &set_sb.s_blocks_count_hi,
     77 		4, parse_uint },
     78 	{ "r_blocks_count", &set_sb.s_r_blocks_count,
     79 		&set_sb.s_r_blocks_count_hi, 4, parse_uint },
     80 	{ "free_blocks_count", &set_sb.s_free_blocks_count,
     81 		&set_sb.s_free_blocks_hi, 4, parse_uint },
     82 	{ "free_inodes_count", &set_sb.s_free_inodes_count, NULL, 4, parse_uint },
     83 	{ "first_data_block", &set_sb.s_first_data_block, NULL, 4, parse_uint },
     84 	{ "log_block_size", &set_sb.s_log_block_size, NULL, 4, parse_uint },
     85 	{ "log_cluster_size", &set_sb.s_log_cluster_size, NULL, 4, parse_int },
     86 	{ "blocks_per_group", &set_sb.s_blocks_per_group, NULL, 4, parse_uint },
     87 	{ "clusters_per_group", &set_sb.s_clusters_per_group, NULL, 4, parse_uint },
     88 	{ "inodes_per_group", &set_sb.s_inodes_per_group, NULL, 4, parse_uint },
     89 	{ "mtime", &set_sb.s_mtime, NULL, 4, parse_time },
     90 	{ "wtime", &set_sb.s_wtime, NULL, 4, parse_time },
     91 	{ "mnt_count", &set_sb.s_mnt_count, NULL, 2, parse_uint },
     92 	{ "max_mnt_count", &set_sb.s_max_mnt_count, NULL, 2, parse_int },
     93 	/* s_magic */
     94 	{ "state", &set_sb.s_state, NULL, 2, parse_uint },
     95 	{ "errors", &set_sb.s_errors, NULL, 2, parse_uint },
     96 	{ "minor_rev_level", &set_sb.s_minor_rev_level, NULL, 2, parse_uint },
     97 	{ "lastcheck", &set_sb.s_lastcheck, NULL, 4, parse_time },
     98 	{ "checkinterval", &set_sb.s_checkinterval, NULL, 4, parse_uint },
     99 	{ "creator_os", &set_sb.s_creator_os, NULL, 4, parse_uint },
    100 	{ "rev_level", &set_sb.s_rev_level, NULL, 4, parse_uint },
    101 	{ "def_resuid", &set_sb.s_def_resuid, NULL, 2, parse_uint },
    102 	{ "def_resgid", &set_sb.s_def_resgid, NULL, 2, parse_uint },
    103 	{ "first_ino", &set_sb.s_first_ino, NULL, 4, parse_uint },
    104 	{ "inode_size", &set_sb.s_inode_size, NULL, 2, parse_uint },
    105 	{ "block_group_nr", &set_sb.s_block_group_nr, NULL, 2, parse_uint },
    106 	{ "feature_compat", &set_sb.s_feature_compat, NULL, 4, parse_uint },
    107 	{ "feature_incompat", &set_sb.s_feature_incompat, NULL, 4, parse_uint },
    108 	{ "feature_ro_compat", &set_sb.s_feature_ro_compat, NULL, 4, parse_uint },
    109 	{ "uuid", &set_sb.s_uuid, NULL, 16, parse_uuid },
    110 	{ "volume_name",  &set_sb.s_volume_name, NULL, 16, parse_string },
    111 	{ "last_mounted",  &set_sb.s_last_mounted, NULL, 64, parse_string },
    112 	{ "lastcheck",  &set_sb.s_lastcheck, NULL, 4, parse_uint },
    113 	{ "algorithm_usage_bitmap", &set_sb.s_algorithm_usage_bitmap, NULL,
    114 		  4, parse_uint },
    115 	{ "prealloc_blocks", &set_sb.s_prealloc_blocks, NULL, 1, parse_uint },
    116 	{ "prealloc_dir_blocks", &set_sb.s_prealloc_dir_blocks, NULL, 1,
    117 		  parse_uint },
    118 	{ "reserved_gdt_blocks", &set_sb.s_reserved_gdt_blocks, NULL, 2,
    119 		  parse_uint },
    120 	{ "journal_uuid", &set_sb.s_journal_uuid, NULL, 16, parse_uuid },
    121 	{ "journal_inum", &set_sb.s_journal_inum, NULL, 4, parse_uint },
    122 	{ "journal_dev", &set_sb.s_journal_dev, NULL, 4, parse_uint },
    123 	{ "last_orphan", &set_sb.s_last_orphan, NULL, 4, parse_uint },
    124 	{ "hash_seed", &set_sb.s_hash_seed, NULL, 16, parse_uuid },
    125 	{ "def_hash_version", &set_sb.s_def_hash_version, NULL, 1, parse_hashalg },
    126 	{ "jnl_backup_type", &set_sb.s_jnl_backup_type, NULL, 1, parse_uint },
    127 	{ "desc_size", &set_sb.s_desc_size, NULL, 2, parse_uint },
    128 	{ "default_mount_opts", &set_sb.s_default_mount_opts, NULL, 4, parse_uint },
    129 	{ "first_meta_bg", &set_sb.s_first_meta_bg, NULL, 4, parse_uint },
    130 	{ "mkfs_time", &set_sb.s_mkfs_time, NULL, 4, parse_time },
    131 	{ "jnl_blocks", &set_sb.s_jnl_blocks[0], NULL, 4, parse_uint, FLAG_ARRAY,
    132 	  17 },
    133 	{ "min_extra_isize", &set_sb.s_min_extra_isize, NULL, 2, parse_uint },
    134 	{ "want_extra_isize", &set_sb.s_want_extra_isize, NULL, 2, parse_uint },
    135 	{ "flags", &set_sb.s_flags, NULL, 4, parse_uint },
    136 	{ "raid_stride", &set_sb.s_raid_stride, NULL, 2, parse_uint },
    137 	{ "min_extra_isize", &set_sb.s_min_extra_isize, NULL, 4, parse_uint },
    138 	{ "mmp_interval", &set_sb.s_mmp_update_interval, NULL, 2, parse_uint },
    139 	{ "mmp_block", &set_sb.s_mmp_block, NULL, 8, parse_uint },
    140 	{ "raid_stripe_width", &set_sb.s_raid_stripe_width, NULL, 4, parse_uint },
    141 	{ "log_groups_per_flex", &set_sb.s_log_groups_per_flex, NULL, 1, parse_uint },
    142 	{ "kbytes_written", &set_sb.s_kbytes_written, NULL, 8, parse_uint },
    143 	{ "snapshot_inum", &set_sb.s_snapshot_inum, NULL, 4, parse_uint },
    144 	{ "snapshot_id", &set_sb.s_snapshot_id, NULL, 4, parse_uint },
    145 	{ "snapshot_r_blocks_count", &set_sb.s_snapshot_r_blocks_count,
    146 	  NULL, 8, parse_uint },
    147 	{ "snapshot_list", &set_sb.s_snapshot_list, NULL, 4, parse_uint },
    148 	{ "mount_opts",  &set_sb.s_mount_opts, NULL, 64, parse_string },
    149 	{ "usr_quota_inum", &set_sb.s_usr_quota_inum, NULL, 4, parse_uint },
    150 	{ "grp_quota_inum", &set_sb.s_grp_quota_inum, NULL, 4, parse_uint },
    151 	{ "overhead_blocks", &set_sb.s_overhead_blocks, NULL, 4, parse_uint },
    152 	{ "checksum", &set_sb.s_checksum, NULL, 4, parse_uint },
    153 	{ 0, 0, 0, 0 }
    154 };
    155 
    156 static struct field_set_info inode_fields[] = {
    157 	{ "inodes_count", &set_sb.s_inodes_count, NULL, 4, parse_uint },
    158 	{ "mode", &set_inode.i_mode, NULL, 2, parse_uint },
    159 	{ "uid", &set_inode.i_uid, &set_inode.osd2.linux2.l_i_uid_high,
    160 		2, parse_uint },
    161 	{ "size", &set_inode.i_size, &set_inode.i_size_high, 4, parse_uint },
    162 	{ "atime", &set_inode.i_atime, NULL, 4, parse_time },
    163 	{ "ctime", &set_inode.i_ctime, NULL, 4, parse_time },
    164 	{ "mtime", &set_inode.i_mtime, NULL, 4, parse_time },
    165 	{ "dtime", &set_inode.i_dtime, NULL, 4, parse_time },
    166 	{ "gid", &set_inode.i_gid, &set_inode.osd2.linux2.l_i_gid_high,
    167 		2, parse_uint },
    168 	{ "links_count", &set_inode.i_links_count, NULL, 2, parse_uint },
    169 	/* Special case: i_blocks is 4 bytes, i_blocks_high is 2 bytes */
    170 	{ "blocks", &set_inode.i_blocks, &set_inode.osd2.linux2.l_i_blocks_hi,
    171 		6, parse_uint },
    172 	{ "flags", &set_inode.i_flags, NULL, 4, parse_uint },
    173 	{ "version", &set_inode.osd1.linux1.l_i_version,
    174 		&set_inode.i_version_hi, 4, parse_uint },
    175 	{ "translator", &set_inode.osd1.hurd1.h_i_translator, NULL, 4, parse_uint },
    176 	{ "block", &set_inode.i_block[0], NULL, 4, parse_uint, FLAG_ARRAY,
    177 	  EXT2_NDIR_BLOCKS },
    178 	{ "block[IND]", &set_inode.i_block[EXT2_IND_BLOCK], NULL, 4, parse_uint },
    179 	{ "block[DIND]", &set_inode.i_block[EXT2_DIND_BLOCK], NULL, 4, parse_uint },
    180 	{ "block[TIND]", &set_inode.i_block[EXT2_TIND_BLOCK], NULL, 4, parse_uint },
    181 	{ "generation", &set_inode.i_generation, NULL, 4, parse_uint },
    182 	/* Special case: i_file_acl_high is 2 bytes */
    183 	{ "file_acl", &set_inode.i_file_acl,
    184 		&set_inode.osd2.linux2.l_i_file_acl_high, 6, parse_uint },
    185 	{ "dir_acl", &set_inode.i_dir_acl, NULL, 4, parse_uint },
    186 	{ "faddr", &set_inode.i_faddr, NULL, 4, parse_uint },
    187 	{ "frag", &set_inode.osd2.hurd2.h_i_frag, NULL, 1, parse_uint },
    188 	{ "fsize", &set_inode.osd2.hurd2.h_i_fsize, NULL, 1, parse_uint },
    189 	{ "checksum", &set_inode.osd2.linux2.l_i_checksum_lo,
    190 		&set_inode.i_checksum_hi, 2, parse_uint },
    191 	{ "author", &set_inode.osd2.hurd2.h_i_author, NULL,
    192 		4, parse_uint },
    193 	{ "extra_isize", &set_inode.i_extra_isize, NULL,
    194 		2, parse_uint },
    195 	{ "ctime_extra", &set_inode.i_ctime_extra, NULL,
    196 		4, parse_uint },
    197 	{ "mtime_extra", &set_inode.i_mtime_extra, NULL,
    198 		4, parse_uint },
    199 	{ "atime_extra", &set_inode.i_atime_extra, NULL,
    200 		4, parse_uint },
    201 	{ "crtime", &set_inode.i_crtime, NULL, 4, parse_uint },
    202 	{ "crtime_extra", &set_inode.i_crtime_extra, NULL,
    203 		4, parse_uint },
    204 	{ "bmap", NULL, NULL, 4, parse_bmap, FLAG_ARRAY },
    205 	{ 0, 0, 0, 0 }
    206 };
    207 
    208 static struct field_set_info ext2_bg_fields[] = {
    209 	{ "block_bitmap", &set_gd.bg_block_bitmap, NULL, 4, parse_uint },
    210 	{ "inode_bitmap", &set_gd.bg_inode_bitmap, NULL, 4, parse_uint },
    211 	{ "inode_table", &set_gd.bg_inode_table, NULL, 4, parse_uint },
    212 	{ "free_blocks_count", &set_gd.bg_free_blocks_count, NULL, 2, parse_uint },
    213 	{ "free_inodes_count", &set_gd.bg_free_inodes_count, NULL, 2, parse_uint },
    214 	{ "used_dirs_count", &set_gd.bg_used_dirs_count, NULL, 2, parse_uint },
    215 	{ "flags", &set_gd.bg_flags, NULL, 2, parse_uint },
    216 	{ "itable_unused", &set_gd.bg_itable_unused, NULL, 2, parse_uint },
    217 	{ "checksum", &set_gd.bg_checksum, NULL, 2, parse_gd_csum },
    218 	{ 0, 0, 0, 0 }
    219 };
    220 
    221 static struct field_set_info ext4_bg_fields[] = {
    222 	{ "block_bitmap", &set_gd4.bg_block_bitmap,
    223 		&set_gd4.bg_block_bitmap_hi, 4, parse_uint },
    224 	{ "inode_bitmap", &set_gd4.bg_inode_bitmap,
    225 		&set_gd4.bg_inode_bitmap_hi, 4, parse_uint },
    226 	{ "inode_table", &set_gd4.bg_inode_table,
    227 		&set_gd4.bg_inode_table_hi, 4, parse_uint },
    228 	{ "free_blocks_count", &set_gd4.bg_free_blocks_count,
    229 		&set_gd4.bg_free_blocks_count_hi, 2, parse_uint },
    230 	{ "free_inodes_count", &set_gd4.bg_free_inodes_count,
    231 		&set_gd4.bg_free_inodes_count_hi, 2, parse_uint },
    232 	{ "used_dirs_count", &set_gd4.bg_used_dirs_count,
    233 		&set_gd4.bg_used_dirs_count_hi, 2, parse_uint },
    234 	{ "flags", &set_gd4.bg_flags, NULL, 2, parse_uint },
    235 	{ "exclude_bitmap", &set_gd4.bg_exclude_bitmap_lo,
    236 		&set_gd4.bg_exclude_bitmap_hi, 4, parse_uint },
    237 	{ "block_bitmap_csum", &set_gd4.bg_block_bitmap_csum_lo,
    238 		&set_gd4.bg_block_bitmap_csum_hi, 2, parse_uint },
    239 	{ "inode_bitmap_csum", &set_gd4.bg_inode_bitmap_csum_lo,
    240 		&set_gd4.bg_inode_bitmap_csum_hi, 2, parse_uint },
    241 	{ "itable_unused", &set_gd4.bg_itable_unused,
    242 		&set_gd4.bg_itable_unused_hi, 2, parse_uint },
    243 	{ "checksum", &set_gd4.bg_checksum, NULL, 2, parse_gd_csum },
    244 	{ 0, 0, 0, 0 }
    245 };
    246 
    247 static struct field_set_info mmp_fields[] = {
    248 	{ "clear", &set_mmp.mmp_magic, NULL, sizeof(set_mmp), parse_mmp_clear },
    249 	{ "magic", &set_mmp.mmp_magic, NULL, 4, parse_uint },
    250 	{ "seq", &set_mmp.mmp_seq, NULL, 4, parse_uint },
    251 	{ "time", &set_mmp.mmp_time, NULL, 8, parse_uint },
    252 	{ "nodename", &set_mmp.mmp_nodename, NULL, sizeof(set_mmp.mmp_nodename),
    253 		parse_string },
    254 	{ "bdevname", &set_mmp.mmp_bdevname, NULL, sizeof(set_mmp.mmp_bdevname),
    255 		parse_string },
    256 	{ "check_interval", &set_mmp.mmp_check_interval, NULL, 2, parse_uint },
    257 };
    258 
    259 static int check_suffix(const char *field)
    260 {
    261 	int len = strlen(field);
    262 
    263 	if (len <= 3)
    264 		return 0;
    265 	field += len-3;
    266 	if (!strcmp(field, "_lo"))
    267 		return 1;
    268 	if (!strcmp(field, "_hi"))
    269 		return 2;
    270 	return 0;
    271 }
    272 
    273 static struct field_set_info *find_field(struct field_set_info *fields,
    274 					 char *field)
    275 {
    276 	struct field_set_info *ss;
    277 	const char	*prefix;
    278 	char		*arg, *delim, *idx, *tmp;
    279 	int		suffix, prefix_len;
    280 
    281 	if (fields == super_fields)
    282 		prefix = "s_";
    283 	else if (fields == inode_fields)
    284 		prefix = "i_";
    285 	else
    286 		prefix = "bg_";
    287 	prefix_len = strlen(prefix);
    288 	if (strncmp(field, prefix, prefix_len) == 0)
    289 		field += prefix_len;
    290 
    291 	arg = malloc(strlen(field)+1);
    292 	if (!arg)
    293 		return NULL;
    294 	strcpy(arg, field);
    295 
    296 	idx = strchr(arg, '[');
    297 	if (idx) {
    298 		*idx++ = 0;
    299 		delim = idx + strlen(idx) - 1;
    300 		if (!*idx || *delim != ']')
    301 			idx = 0;
    302 		else
    303 			*delim = 0;
    304 	}
    305 	/*
    306 	 * Can we parse the number?
    307 	 */
    308 	if (idx) {
    309 		array_idx = strtol(idx, &tmp, 0);
    310 		if (*tmp)
    311 			idx = 0;
    312 	}
    313 
    314 	/*
    315 	 * If there is a valid _hi or a _lo suffix, strip it off
    316 	 */
    317 	suffix = check_suffix(arg);
    318 	if (suffix > 0)
    319 		arg[strlen(arg)-3] = 0;
    320 
    321 	for (ss = fields ; ss->name ; ss++) {
    322 		if (suffix && ss->ptr2 == 0)
    323 			continue;
    324 		if (ss->flags & FLAG_ARRAY) {
    325 			if (!idx || (strcmp(ss->name, arg) != 0))
    326 				continue;
    327 			if (ss->max_idx > 0 && array_idx >= ss->max_idx)
    328 				continue;
    329 		} else {
    330 			if (strcmp(ss->name, arg) != 0)
    331 				continue;
    332 		}
    333 		free(arg);
    334 		return ss;
    335 	}
    336 	free(arg);
    337 	return NULL;
    338 }
    339 
    340 /*
    341  * Note: info->size == 6 is special; this means a base size 4 bytes,
    342  * and secondiory (high) size of 2 bytes.  This is needed for the
    343  * special case of i_blocks_high and i_file_acl_high.
    344  */
    345 static errcode_t parse_uint(struct field_set_info *info, char *field,
    346 			    char *arg)
    347 {
    348 	unsigned long long n, num, mask, limit;
    349 	int suffix = check_suffix(field);
    350 	char *tmp;
    351 	void *field1 = info->ptr, *field2 = info->ptr2;
    352 	int size = (info->size == 6) ? 4 : info->size;
    353 	union {
    354 		__u64	*ptr64;
    355 		__u32	*ptr32;
    356 		__u16	*ptr16;
    357 		__u8	*ptr8;
    358 	} u;
    359 
    360 	if (suffix == 1)
    361 		field2 = 0;
    362 	if (suffix == 2) {
    363 		field1 = field2;
    364 		field2 = 0;
    365 	}
    366 
    367 	u.ptr8 = (__u8 *) field1;
    368 	if (info->flags & FLAG_ARRAY)
    369 		u.ptr8 += array_idx * info->size;
    370 
    371 	errno = 0;
    372 	num = STRTOULL(arg, &tmp, 0);
    373 	if (*tmp || errno) {
    374 		fprintf(stderr, "Couldn't parse '%s' for field %s.\n",
    375 			arg, info->name);
    376 		return EINVAL;
    377 	}
    378 	mask = ~0ULL >> ((8 - size) * 8);
    379 	limit = ~0ULL >> ((8 - info->size) * 8);
    380 	if (field2 && info->size != 6)
    381 		limit = ~0ULL >> ((8 - info->size*2) * 8);
    382 
    383 	if (num > limit) {
    384 		fprintf(stderr, "Value '%s' exceeds field %s maximum %llu.\n",
    385 			arg, info->name, limit);
    386 		return EINVAL;
    387 	}
    388 	n = num & mask;
    389 	switch (size) {
    390 	case 8:
    391 		/* Should never get here */
    392 		fprintf(stderr, "64-bit field %s has a second 64-bit field\n"
    393 			"defined; BUG?!?\n", info->name);
    394 		*u.ptr64 = 0;
    395 		break;
    396 	case 4:
    397 		*u.ptr32 = n;
    398 		break;
    399 	case 2:
    400 		*u.ptr16 = n;
    401 		break;
    402 	case 1:
    403 		*u.ptr8 = n;
    404 		break;
    405 	}
    406 	if (!field2)
    407 		return 0;
    408 	n = num >> (size*8);
    409 	u.ptr8 = (__u8 *) field2;
    410 	if (info->size == 6)
    411 		size = 2;
    412 	switch (size) {
    413 	case 8:
    414 		*u.ptr64 = n;
    415 		break;
    416 	case 4:
    417 		*u.ptr32 = n;
    418 		break;
    419 	case 2:
    420 		*u.ptr16 = n;
    421 		break;
    422 	case 1:
    423 		*u.ptr8 = n;
    424 		break;
    425 	}
    426 	return 0;
    427 }
    428 
    429 static errcode_t parse_int(struct field_set_info *info,
    430 			   char *field EXT2FS_ATTR((unused)), char *arg)
    431 {
    432 	long	num;
    433 	char *tmp;
    434 	__s32	*ptr32;
    435 	__s16	*ptr16;
    436 	__s8	*ptr8;
    437 
    438 	num = strtol(arg, &tmp, 0);
    439 	if (*tmp) {
    440 		fprintf(stderr, "Couldn't parse '%s' for field %s.\n",
    441 			arg, info->name);
    442 		return EINVAL;
    443 	}
    444 	switch (info->size) {
    445 	case 4:
    446 		ptr32 = (__s32 *) info->ptr;
    447 		*ptr32 = num;
    448 		break;
    449 	case 2:
    450 		ptr16 = (__s16 *) info->ptr;
    451 		*ptr16 = num;
    452 		break;
    453 	case 1:
    454 		ptr8 = (__s8 *) info->ptr;
    455 		*ptr8 = num;
    456 		break;
    457 	}
    458 	return 0;
    459 }
    460 
    461 static errcode_t parse_string(struct field_set_info *info,
    462 			      char *field EXT2FS_ATTR((unused)), char *arg)
    463 {
    464 	char	*cp = (char *) info->ptr;
    465 
    466 	if (strlen(arg) >= info->size) {
    467 		fprintf(stderr, "Error maximum size for %s is %d.\n",
    468 			info->name, info->size);
    469 		return EINVAL;
    470 	}
    471 	strcpy(cp, arg);
    472 	return 0;
    473 }
    474 
    475 static errcode_t parse_time(struct field_set_info *info,
    476 			    char *field EXT2FS_ATTR((unused)), char *arg)
    477 {
    478 	time_t		t;
    479 	__u32		*ptr32;
    480 
    481 	ptr32 = (__u32 *) info->ptr;
    482 
    483 	t = string_to_time(arg);
    484 
    485 	if (t == ((time_t) -1)) {
    486 		fprintf(stderr, "Couldn't parse '%s' for field %s.\n",
    487 			arg, info->name);
    488 		return EINVAL;
    489 	}
    490 	*ptr32 = t;
    491 	return 0;
    492 }
    493 
    494 static errcode_t parse_uuid(struct field_set_info *info,
    495 			    char *field EXT2FS_ATTR((unused)), char *arg)
    496 {
    497 	unsigned char *	p = (unsigned char *) info->ptr;
    498 
    499 	if ((strcasecmp(arg, "null") == 0) ||
    500 	    (strcasecmp(arg, "clear") == 0)) {
    501 		uuid_clear(p);
    502 	} else if (strcasecmp(arg, "time") == 0) {
    503 		uuid_generate_time(p);
    504 	} else if (strcasecmp(arg, "random") == 0) {
    505 		uuid_generate(p);
    506 	} else if (uuid_parse(arg, p)) {
    507 		fprintf(stderr, "Invalid UUID format: %s\n", arg);
    508 		return EINVAL;
    509 	}
    510 	return 0;
    511 }
    512 
    513 static errcode_t parse_hashalg(struct field_set_info *info,
    514 			       char *field EXT2FS_ATTR((unused)), char *arg)
    515 {
    516 	int	hashv;
    517 	unsigned char	*p = (unsigned char *) info->ptr;
    518 
    519 	hashv = e2p_string2hash(arg);
    520 	if (hashv < 0) {
    521 		fprintf(stderr, "Invalid hash algorithm: %s\n", arg);
    522 		return EINVAL;
    523 	}
    524 	*p = hashv;
    525 	return 0;
    526 }
    527 
    528 static errcode_t parse_bmap(struct field_set_info *info,
    529 			    char *field EXT2FS_ATTR((unused)), char *arg)
    530 {
    531 	blk64_t		blk;
    532 	errcode_t	retval;
    533 	char		*tmp;
    534 
    535 	blk = strtoull(arg, &tmp, 0);
    536 	if (*tmp) {
    537 		fprintf(stderr, "Couldn't parse '%s' for field %s.\n",
    538 			arg, info->name);
    539 		return EINVAL;
    540 	}
    541 
    542 	retval = ext2fs_bmap2(current_fs, set_ino,
    543 			     (struct ext2_inode *) &set_inode,
    544 			     NULL, BMAP_SET, array_idx, NULL, &blk);
    545 	if (retval) {
    546 		com_err("set_inode", retval, "while setting block map");
    547 	}
    548 	return retval;
    549 }
    550 
    551 static errcode_t parse_gd_csum(struct field_set_info *info, char *field,
    552 			       char *arg)
    553 {
    554 
    555 	if (strcmp(arg, "calc") == 0) {
    556 		ext2fs_group_desc_csum_set(current_fs, set_bg);
    557 		memcpy(&set_gd, ext2fs_group_desc(current_fs,
    558 					current_fs->group_desc,
    559 					set_bg),
    560 			sizeof(set_gd));
    561 		printf("Checksum set to 0x%04x\n",
    562 		       ext2fs_bg_checksum(current_fs, set_bg));
    563 		return 0;
    564 	}
    565 
    566 	return parse_uint(info, field, arg);
    567 }
    568 
    569 static void print_possible_fields(struct field_set_info *fields)
    570 {
    571 	struct field_set_info *ss;
    572 	const char	*type, *cmd;
    573 	FILE *f;
    574 	char name[40], idx[40];
    575 
    576 	if (fields == super_fields) {
    577 		type = "Superblock";
    578 		cmd = "set_super_value";
    579 	} else if (fields == inode_fields) {
    580 		type = "Inode";
    581 		cmd = "set_inode";
    582 	} else if (fields == mmp_fields) {
    583 		type = "MMP";
    584 		cmd = "set_mmp_value";
    585 	} else {
    586 		type = "Block group descriptor";
    587 		cmd = "set_block_group";
    588 	}
    589 	f = open_pager();
    590 
    591 	fprintf(f, "%s fields supported by the %s command:\n", type, cmd);
    592 
    593 	for (ss = fields ; ss->name ; ss++) {
    594 		type = "unknown";
    595 		if (ss->func == parse_string)
    596 			type = "string";
    597 		else if (ss->func == parse_int)
    598 			type = "integer";
    599 		else if (ss->func == parse_uint)
    600 			type = "unsigned integer";
    601 		else if (ss->func == parse_uuid)
    602 			type = "UUID";
    603 		else if (ss->func == parse_hashalg)
    604 			type = "hash algorithm";
    605 		else if (ss->func == parse_time)
    606 			type = "date/time";
    607 		else if (ss->func == parse_bmap)
    608 			type = "set physical->logical block map";
    609 		else if (ss->func == parse_gd_csum)
    610 			type = "unsigned integer OR \"calc\"";
    611 		strcpy(name, ss->name);
    612 		if (ss->flags & FLAG_ARRAY) {
    613 			if (ss->max_idx > 0)
    614 				sprintf(idx, "[%d]", ss->max_idx);
    615 			else
    616 				strcpy(idx, "[]");
    617 			strcat(name, idx);
    618 		}
    619 		if (ss->ptr2)
    620 			strcat(name, "[_hi|_lo]");
    621 		fprintf(f, "\t%-25s\t%s\n", name, type);
    622 	}
    623 	close_pager(f);
    624 }
    625 
    626 
    627 void do_set_super(int argc, char *argv[])
    628 {
    629 	const char *usage = "<field> <value>\n"
    630 		"\t\"set_super_value -l\" will list the names of "
    631 		"superblock fields\n\twhich can be set.";
    632 	static struct field_set_info *ss;
    633 
    634 	if ((argc == 2) && !strcmp(argv[1], "-l")) {
    635 		print_possible_fields(super_fields);
    636 		return;
    637 	}
    638 
    639 	if (common_args_process(argc, argv, 3, 3, "set_super_value",
    640 				usage, CHECK_FS_RW))
    641 		return;
    642 
    643 	if ((ss = find_field(super_fields, argv[1])) == 0) {
    644 		com_err(argv[0], 0, "invalid field specifier: %s", argv[1]);
    645 		return;
    646 	}
    647 	set_sb = *current_fs->super;
    648 	if (ss->func(ss, argv[1], argv[2]) == 0) {
    649 		*current_fs->super = set_sb;
    650 		ext2fs_mark_super_dirty(current_fs);
    651 	}
    652 }
    653 
    654 void do_set_inode(int argc, char *argv[])
    655 {
    656 	const char *usage = "<inode> <field> <value>\n"
    657 		"\t\"set_inode_field -l\" will list the names of "
    658 		"the fields in an ext2 inode\n\twhich can be set.";
    659 	static struct field_set_info *ss;
    660 
    661 	if ((argc == 2) && !strcmp(argv[1], "-l")) {
    662 		print_possible_fields(inode_fields);
    663 		return;
    664 	}
    665 
    666 	if (common_args_process(argc, argv, 4, 4, "set_inode",
    667 				usage, CHECK_FS_RW))
    668 		return;
    669 
    670 	if ((ss = find_field(inode_fields, argv[2])) == 0) {
    671 		com_err(argv[0], 0, "invalid field specifier: %s", argv[2]);
    672 		return;
    673 	}
    674 
    675 	set_ino = string_to_inode(argv[1]);
    676 	if (!set_ino)
    677 		return;
    678 
    679 	if (debugfs_read_inode_full(set_ino,
    680 			(struct ext2_inode *) &set_inode, argv[1],
    681 				    sizeof(set_inode)))
    682 		return;
    683 
    684 	if (ss->func(ss, argv[2], argv[3]) == 0) {
    685 		if (debugfs_write_inode_full(set_ino,
    686 			     (struct ext2_inode *) &set_inode,
    687 			     argv[1], sizeof(set_inode)))
    688 			return;
    689 	}
    690 }
    691 
    692 void do_set_block_group_descriptor(int argc, char *argv[])
    693 {
    694 	const char *usage = "<bg number> <field> <value>\n"
    695 		"\t\"set_block_group_descriptor -l\" will list the names of "
    696 		"the fields in a block group descriptor\n\twhich can be set.";
    697 	struct field_set_info	*table;
    698 	struct field_set_info	*ss;
    699 	char			*end;
    700 	void			*edit, *target;
    701 	int			size;
    702 
    703 	/*
    704 	 * Determine whether we are editing an ext2 or ext4 block group
    705 	 * descriptor.  Descriptors larger than ext4_group_desc cannot
    706 	 * have their fields edited yet, because they do not have any
    707 	 * names assigned.  When that happens, this function needs to
    708 	 * be updated for the new descriptor struct and fields.
    709 	 */
    710 	if (current_fs &&
    711 	    EXT2_DESC_SIZE(current_fs->super) >= EXT2_MIN_DESC_SIZE_64BIT) {
    712 		table = ext4_bg_fields;
    713 		edit = &set_gd4;
    714 		size = sizeof(set_gd4);
    715 	} else {
    716 		table = ext2_bg_fields;
    717 		edit = &set_gd;
    718 		size = sizeof(set_gd);
    719 	}
    720 
    721 	if ((argc == 2) && !strcmp(argv[1], "-l")) {
    722 		print_possible_fields(table);
    723 		return;
    724 	}
    725 
    726 	if (common_args_process(argc, argv, 4, 4, "set_block_group_descriptor",
    727 				usage, CHECK_FS_RW))
    728 		return;
    729 
    730 	set_bg = strtoul(argv[1], &end, 0);
    731 	if (*end) {
    732 		com_err(argv[0], 0, "invalid block group number: %s", argv[1]);
    733 		return;
    734 	}
    735 
    736 	if (set_bg >= current_fs->group_desc_count) {
    737 		com_err(argv[0], 0, "block group number too big: %d", set_bg);
    738 		return;
    739 	}
    740 
    741 	if ((ss = find_field(table, argv[2])) == 0) {
    742 		com_err(argv[0], 0, "invalid field specifier: %s", argv[2]);
    743 		return;
    744 	}
    745 
    746 	target = ext2fs_group_desc(current_fs, current_fs->group_desc, set_bg);
    747 	memcpy(edit, target, size);
    748 	if (ss->func(ss, argv[2], argv[3]) == 0) {
    749 		memcpy(target, edit, size);
    750 		ext2fs_mark_super_dirty(current_fs);
    751 	}
    752 }
    753 
    754 static errcode_t parse_mmp_clear(struct field_set_info *info,
    755 				 char *field EXT2FS_ATTR((unused)),
    756 				 char *arg EXT2FS_ATTR((unused)))
    757 {
    758 	errcode_t retval;
    759 
    760 	retval = ext2fs_mmp_clear(current_fs);
    761 	if (retval != 0)
    762 		com_err("set_mmp_value", retval, "while clearing MMP block\n");
    763 	else
    764 		memcpy(info->ptr, current_fs->mmp_buf, info->size);
    765 
    766 	return 1; /* we don't need the MMP block written again */
    767 }
    768 
    769 void do_set_mmp_value(int argc, char *argv[])
    770 {
    771 	const char *usage = "<field> <value>\n"
    772 		"\t\"set_mmp_value -l\" will list the names of "
    773 		"MMP fields\n\twhich can be set.";
    774 	static struct field_set_info *smmp;
    775 	struct mmp_struct *mmp_s;
    776 	errcode_t retval;
    777 
    778 	if (argc == 2 && strcmp(argv[1], "-l") == 0) {
    779 		print_possible_fields(mmp_fields);
    780 		return;
    781 	}
    782 
    783 	if (check_fs_open(argv[0]))
    784 		return;
    785 
    786 	if (current_fs->super->s_mmp_block == 0) {
    787 		com_err(argv[0], 0, "no MMP block allocated\n");
    788 		return;
    789 	}
    790 
    791 	if (common_args_process(argc, argv, 2, 3, "set_mmp_value",
    792 				usage, CHECK_FS_RW))
    793 		return;
    794 
    795 	mmp_s = current_fs->mmp_buf;
    796 	if (mmp_s == NULL) {
    797 		retval = ext2fs_get_mem(current_fs->blocksize, &mmp_s);
    798 		if (retval) {
    799 			com_err(argv[0], retval, "allocating MMP buffer\n");
    800 			return;
    801 		}
    802 		retval = ext2fs_mmp_read(current_fs,
    803 					 current_fs->super->s_mmp_block, mmp_s);
    804 		if (retval) {
    805 			com_err(argv[0], retval, "reading MMP block %llu.\n",
    806 				(long long)current_fs->super->s_mmp_block);
    807 			ext2fs_free_mem(&mmp_s);
    808 			return;
    809 		}
    810 		current_fs->mmp_buf = mmp_s;
    811 	}
    812 
    813 	smmp = find_field(mmp_fields, argv[1]);
    814 	if (smmp == 0) {
    815 		com_err(argv[0], 0, "invalid field specifier: %s", argv[1]);
    816 		return;
    817 	}
    818 
    819 	set_mmp = *mmp_s;
    820 	if (smmp->func(smmp, argv[1], argv[2]) == 0) {
    821 		ext2fs_mmp_write(current_fs, current_fs->super->s_mmp_block,
    822 				 &set_mmp);
    823 		*mmp_s = set_mmp;
    824 	}
    825 }
    826 
    827