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