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