set_fields.c revision 0edcc2702106ef8ea0d4ab60ef06c0c38b0b87ee
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
42static struct ext2_super_block set_sb;
43static struct ext2_inode set_inode;
44static struct ext2_group_desc set_gd;
45static dgrp_t set_bg;
46static ext2_ino_t set_ino;
47static int array_idx;
48
49#define FLAG_ARRAY	0x0001
50
51struct field_set_info {
52	const char	*name;
53	void	*ptr;
54	unsigned int	size;
55	errcode_t (*func)(struct field_set_info *info, char *arg);
56	int flags;
57	int max_idx;
58};
59
60static errcode_t parse_uint(struct field_set_info *info, char *arg);
61static errcode_t parse_int(struct field_set_info *info, char *arg);
62static errcode_t parse_string(struct field_set_info *info, char *arg);
63static errcode_t parse_uuid(struct field_set_info *info, char *arg);
64static errcode_t parse_hashalg(struct field_set_info *info, char *arg);
65static errcode_t parse_time(struct field_set_info *info, char *arg);
66static errcode_t parse_bmap(struct field_set_info *info, char *arg);
67static errcode_t parse_gd_csum(struct field_set_info *info, char *arg);
68
69static struct field_set_info super_fields[] = {
70	{ "inodes_count", &set_sb.s_inodes_count, 4, parse_uint },
71	{ "blocks_count", &set_sb.s_blocks_count, 4, parse_uint },
72	{ "r_blocks_count", &set_sb.s_r_blocks_count, 4, parse_uint },
73	{ "free_blocks_count", &set_sb.s_free_blocks_count, 4, parse_uint },
74	{ "free_inodes_count", &set_sb.s_free_inodes_count, 4, parse_uint },
75	{ "first_data_block", &set_sb.s_first_data_block, 4, parse_uint },
76	{ "log_block_size", &set_sb.s_log_block_size, 4, parse_uint },
77	{ "log_frag_size", &set_sb.s_log_frag_size, 4, parse_int },
78	{ "blocks_per_group", &set_sb.s_blocks_per_group, 4, parse_uint },
79	{ "frags_per_group", &set_sb.s_frags_per_group, 4, parse_uint },
80	{ "inodes_per_group", &set_sb.s_inodes_per_group, 4, parse_uint },
81	{ "mtime", &set_sb.s_mtime, 4, parse_time },
82	{ "wtime", &set_sb.s_wtime, 4, parse_time },
83	{ "mnt_count", &set_sb.s_mnt_count, 2, parse_uint },
84	{ "max_mnt_count", &set_sb.s_max_mnt_count, 2, parse_int },
85	/* s_magic */
86	{ "state", &set_sb.s_state, 2, parse_uint },
87	{ "errors", &set_sb.s_errors, 2, parse_uint },
88	{ "minor_rev_level", &set_sb.s_minor_rev_level, 2, parse_uint },
89	{ "lastcheck", &set_sb.s_lastcheck, 4, parse_time },
90	{ "checkinterval", &set_sb.s_checkinterval, 4, parse_uint },
91	{ "creator_os", &set_sb.s_creator_os, 4, parse_uint },
92	{ "rev_level", &set_sb.s_rev_level, 4, parse_uint },
93	{ "def_resuid", &set_sb.s_def_resuid, 2, parse_uint },
94	{ "def_resgid", &set_sb.s_def_resgid, 2, parse_uint },
95	{ "first_ino", &set_sb.s_first_ino, 4, parse_uint },
96	{ "inode_size", &set_sb.s_inode_size, 2, parse_uint },
97	{ "block_group_nr", &set_sb.s_block_group_nr, 2, parse_uint },
98	{ "feature_compat", &set_sb.s_feature_compat, 4, parse_uint },
99	{ "feature_incompat", &set_sb.s_feature_incompat, 4, parse_uint },
100	{ "feature_ro_compat", &set_sb.s_feature_ro_compat, 4, parse_uint },
101	{ "uuid", &set_sb.s_uuid, 16, parse_uuid },
102	{ "volume_name",  &set_sb.s_volume_name, 16, parse_string },
103	{ "last_mounted",  &set_sb.s_last_mounted, 64, parse_string },
104	{ "lastcheck",  &set_sb.s_lastcheck, 4, parse_uint },
105	{ "algorithm_usage_bitmap", &set_sb.s_algorithm_usage_bitmap,
106		  4, parse_uint },
107	{ "prealloc_blocks", &set_sb.s_prealloc_blocks, 1, parse_uint },
108	{ "prealloc_dir_blocks", &set_sb.s_prealloc_dir_blocks, 1,
109		  parse_uint },
110	{ "reserved_gdt_blocks", &set_sb.s_reserved_gdt_blocks, 2,
111		  parse_uint },
112	{ "journal_uuid", &set_sb.s_journal_uuid, 16, parse_uuid },
113	{ "journal_inum", &set_sb.s_journal_inum, 4, parse_uint },
114	{ "journal_dev", &set_sb.s_journal_dev, 4, parse_uint },
115	{ "last_orphan", &set_sb.s_last_orphan, 4, parse_uint },
116	{ "hash_seed", &set_sb.s_hash_seed, 16, parse_uuid },
117	{ "def_hash_version", &set_sb.s_def_hash_version, 1, parse_hashalg },
118	{ "jnl_backup_type", &set_sb.s_jnl_backup_type, 1, parse_uint },
119	{ "desc_size", &set_sb.s_desc_size, 2, parse_uint },
120	{ "default_mount_opts", &set_sb.s_default_mount_opts, 4, parse_uint },
121	{ "first_meta_bg", &set_sb.s_first_meta_bg, 4, parse_uint },
122	{ "mkfs_time", &set_sb.s_mkfs_time, 4, parse_time },
123	{ "jnl_blocks", &set_sb.s_jnl_blocks[0], 4, parse_uint, FLAG_ARRAY,
124	  17 },
125	{ "blocks_count_hi", &set_sb.s_blocks_count_hi, 4, parse_uint },
126	{ "r_blocks_count_hi", &set_sb.s_r_blocks_count_hi, 4, parse_uint },
127	{ "min_extra_isize", &set_sb.s_min_extra_isize, 2, parse_uint },
128	{ "want_extra_isize", &set_sb.s_want_extra_isize, 2, parse_uint },
129	{ "flags", &set_sb.s_flags, 4, parse_uint },
130	{ "raid_stride", &set_sb.s_raid_stride, 2, parse_uint },
131	{ "min_extra_isize", &set_sb.s_min_extra_isize, 4, parse_uint },
132	{ "mmp_interval", &set_sb.s_mmp_interval, 2, parse_uint },
133	{ "mmp_block", &set_sb.s_mmp_block, 8, parse_uint },
134	{ "raid_stripe_width", &set_sb.s_raid_stripe_width, 4, parse_uint },
135	{ "log_groups_per_flex", &set_sb.s_log_groups_per_flex, 1, parse_uint },
136	{ "kbytes_written", &set_sb.s_kbytes_written, 8, parse_uint },
137	{ "snapshot_inum", &set_sb.s_snapshot_inum, 4, parse_uint },
138	{ "snapshot_id", &set_sb.s_snapshot_id, 4, parse_uint },
139	{ "snapshot_r_blocks_count", &set_sb.s_snapshot_r_blocks_count, 8,
140	  parse_uint },
141	{ "snapshot_list", &set_sb.s_snapshot_list, 4, parse_uint },
142	{ "mount_opts",  &set_sb.s_mount_opts, 64, parse_string },
143	{ "usr_quota_inum", &set_sb.s_usr_quota_inum, 4, parse_uint },
144	{ "grp_quota_inum", &set_sb.s_grp_quota_inum, 4, parse_uint },
145	{ 0, 0, 0, 0 }
146};
147
148static struct field_set_info inode_fields[] = {
149	{ "inodes_count", &set_sb.s_inodes_count, 4, parse_uint },
150	{ "mode", &set_inode.i_mode, 2, parse_uint },
151	{ "uid", &set_inode.i_uid, 2, parse_uint },
152	{ "size", &set_inode.i_size, 4, parse_uint },
153	{ "atime", &set_inode.i_atime, 4, parse_time },
154	{ "ctime", &set_inode.i_ctime, 4, parse_time },
155	{ "mtime", &set_inode.i_mtime, 4, parse_time },
156	{ "dtime", &set_inode.i_dtime, 4, parse_time },
157	{ "gid", &set_inode.i_gid, 2, parse_uint },
158	{ "links_count", &set_inode.i_links_count, 2, parse_uint },
159	{ "blocks", &set_inode.i_blocks, 4, parse_uint },
160	{ "flags", &set_inode.i_flags, 4, parse_uint },
161	{ "version", &set_inode.osd1.linux1.l_i_version, 4, parse_uint },
162	{ "translator", &set_inode.osd1.hurd1.h_i_translator, 4, parse_uint },
163	{ "block", &set_inode.i_block[0], 4, parse_uint, FLAG_ARRAY,
164	  EXT2_NDIR_BLOCKS },
165	{ "block[IND]", &set_inode.i_block[EXT2_IND_BLOCK], 4, parse_uint },
166	{ "block[DIND]", &set_inode.i_block[EXT2_DIND_BLOCK], 4, parse_uint },
167	{ "block[TIND]", &set_inode.i_block[EXT2_TIND_BLOCK], 4, parse_uint },
168	{ "generation", &set_inode.i_generation, 4, parse_uint },
169	{ "file_acl", &set_inode.i_file_acl, 4, parse_uint },
170	{ "file_acl_high", &set_inode.osd2.linux2.l_i_file_acl_high, 2,
171	  parse_uint },
172	{ "dir_acl", &set_inode.i_dir_acl, 4, parse_uint },
173	{ "size_high", &set_inode.i_size_high, 4, parse_uint },
174	{ "faddr", &set_inode.i_faddr, 4, parse_uint },
175	{ "blocks_hi", &set_inode.osd2.linux2.l_i_blocks_hi, 2, parse_uint },
176	{ "frag", &set_inode.osd2.hurd2.h_i_frag, 1, parse_uint },
177	{ "fsize", &set_inode.osd2.hurd2.h_i_fsize, 1, parse_uint },
178	{ "uid_high", &set_inode.osd2.linux2.l_i_uid_high, 2, parse_uint },
179	{ "gid_high", &set_inode.osd2.linux2.l_i_gid_high, 2, parse_uint },
180	{ "author", &set_inode.osd2.hurd2.h_i_author, 4, parse_uint },
181	{ "bmap", NULL, 4, parse_bmap, FLAG_ARRAY },
182	{ 0, 0, 0, 0 }
183};
184
185static struct field_set_info ext2_bg_fields[] = {
186	{ "block_bitmap", &set_gd.bg_block_bitmap, 4, parse_uint },
187	{ "inode_bitmap", &set_gd.bg_inode_bitmap, 4, parse_uint },
188	{ "inode_table", &set_gd.bg_inode_table, 4, parse_uint },
189	{ "free_blocks_count", &set_gd.bg_free_blocks_count, 2, parse_uint },
190	{ "free_inodes_count", &set_gd.bg_free_inodes_count, 2, parse_uint },
191	{ "used_dirs_count", &set_gd.bg_used_dirs_count, 2, parse_uint },
192	{ "flags", &set_gd.bg_flags, 2, parse_uint },
193	{ "reserved", &set_gd.bg_reserved, 2, parse_uint, FLAG_ARRAY, 2 },
194	{ "itable_unused", &set_gd.bg_itable_unused, 2, parse_uint },
195	{ "checksum", &set_gd.bg_checksum, 2, parse_gd_csum },
196	{ 0, 0, 0, 0 }
197};
198
199
200static struct field_set_info *find_field(struct field_set_info *fields,
201					 char *field)
202{
203	struct field_set_info *ss;
204	const char	*prefix;
205	char		*arg, *delim, *idx, *tmp;
206	int		prefix_len;
207
208	if (fields == super_fields)
209		prefix = "s_";
210	else if (fields == inode_fields)
211		prefix = "i_";
212	else
213		prefix = "bg_";
214	prefix_len = strlen(prefix);
215	if (strncmp(field, prefix, prefix_len) == 0)
216		field += prefix_len;
217
218	arg = malloc(strlen(field)+1);
219	if (!arg)
220		return NULL;
221	strcpy(arg, field);
222
223	idx = strchr(arg, '[');
224	if (idx) {
225		*idx++ = 0;
226		delim = idx + strlen(idx) - 1;
227		if (!*idx || *delim != ']')
228			idx = 0;
229		else
230			*delim = 0;
231	}
232	/*
233	 * Can we parse the number?
234	 */
235	if (idx) {
236		array_idx = strtol(idx, &tmp, 0);
237		if (*tmp)
238			idx = 0;
239	}
240
241	for (ss = fields ; ss->name ; ss++) {
242		if (ss->flags & FLAG_ARRAY) {
243			if (!idx || (strcmp(ss->name, arg) != 0))
244				continue;
245			if (ss->max_idx > 0 && array_idx >= ss->max_idx)
246				continue;
247		} else {
248			if (strcmp(ss->name, field) != 0)
249				continue;
250		}
251		free(arg);
252		return ss;
253	}
254	free(arg);
255	return NULL;
256}
257
258static errcode_t parse_uint(struct field_set_info *info, char *arg)
259{
260	unsigned long long num, limit;
261	char *tmp;
262	union {
263		__u64	*ptr64;
264		__u32	*ptr32;
265		__u16	*ptr16;
266		__u8	*ptr8;
267	} u;
268
269	u.ptr8 = (__u8 *) info->ptr;
270	if (info->flags & FLAG_ARRAY)
271		u.ptr8 += array_idx * info->size;
272
273	errno = 0;
274	num = STRTOULL(arg, &tmp, 0);
275	if (*tmp || errno) {
276		fprintf(stderr, "Couldn't parse '%s' for field %s.\n",
277			arg, info->name);
278		return EINVAL;
279	}
280	limit = ~0ULL >> ((8 - info->size) * 8);
281	if (num > limit) {
282		fprintf(stderr, "Value '%s' exceeds field %s maximum %llu.\n",
283			arg, info->name, limit);
284		return EINVAL;
285	}
286	switch (info->size) {
287	case 8:
288		*u.ptr64 = num;
289		break;
290	case 4:
291		*u.ptr32 = num;
292		break;
293	case 2:
294		*u.ptr16 = num;
295		break;
296	case 1:
297		*u.ptr8 = num;
298		break;
299	}
300	return 0;
301}
302
303static errcode_t parse_int(struct field_set_info *info, char *arg)
304{
305	long	num;
306	char *tmp;
307	__s32	*ptr32;
308	__s16	*ptr16;
309	__s8	*ptr8;
310
311	num = strtol(arg, &tmp, 0);
312	if (*tmp) {
313		fprintf(stderr, "Couldn't parse '%s' for field %s.\n",
314			arg, info->name);
315		return EINVAL;
316	}
317	switch (info->size) {
318	case 4:
319		ptr32 = (__s32 *) info->ptr;
320		*ptr32 = num;
321		break;
322	case 2:
323		ptr16 = (__s16 *) info->ptr;
324		*ptr16 = num;
325		break;
326	case 1:
327		ptr8 = (__s8 *) info->ptr;
328		*ptr8 = num;
329		break;
330	}
331	return 0;
332}
333
334static errcode_t parse_string(struct field_set_info *info, char *arg)
335{
336	char	*cp = (char *) info->ptr;
337
338	if (strlen(arg) >= info->size) {
339		fprintf(stderr, "Error maximum size for %s is %d.\n",
340			info->name, info->size);
341		return EINVAL;
342	}
343	strcpy(cp, arg);
344	return 0;
345}
346
347static errcode_t parse_time(struct field_set_info *info, char *arg)
348{
349	time_t		t;
350	__u32		*ptr32;
351
352	ptr32 = (__u32 *) info->ptr;
353
354	t = string_to_time(arg);
355
356	if (t == ((time_t) -1)) {
357		fprintf(stderr, "Couldn't parse '%s' for field %s.\n",
358			arg, info->name);
359		return EINVAL;
360	}
361	*ptr32 = t;
362	return 0;
363}
364
365static errcode_t parse_uuid(struct field_set_info *info, char *arg)
366{
367	unsigned char *	p = (unsigned char *) info->ptr;
368
369	if ((strcasecmp(arg, "null") == 0) ||
370	    (strcasecmp(arg, "clear") == 0)) {
371		uuid_clear(p);
372	} else if (strcasecmp(arg, "time") == 0) {
373		uuid_generate_time(p);
374	} else if (strcasecmp(arg, "random") == 0) {
375		uuid_generate(p);
376	} else if (uuid_parse(arg, p)) {
377		fprintf(stderr, "Invalid UUID format: %s\n", arg);
378		return EINVAL;
379	}
380	return 0;
381}
382
383static errcode_t parse_hashalg(struct field_set_info *info, char *arg)
384{
385	int	hashv;
386	unsigned char	*p = (unsigned char *) info->ptr;
387
388	hashv = e2p_string2hash(arg);
389	if (hashv < 0) {
390		fprintf(stderr, "Invalid hash algorithm: %s\n", arg);
391		return EINVAL;
392	}
393	*p = hashv;
394	return 0;
395}
396
397static errcode_t parse_bmap(struct field_set_info *info, char *arg)
398{
399	unsigned long	num;
400	blk_t		blk;
401	errcode_t	retval;
402	char		*tmp;
403
404	num = strtoul(arg, &tmp, 0);
405	if (*tmp) {
406		fprintf(stderr, "Couldn't parse '%s' for field %s.\n",
407			arg, info->name);
408		return EINVAL;
409	}
410	blk = num;
411
412	retval = ext2fs_bmap(current_fs, set_ino, &set_inode, 0, BMAP_SET,
413			     array_idx, &blk);
414	if (retval) {
415		com_err("set_inode", retval, "while setting block map");
416	}
417	return retval;
418}
419
420static errcode_t parse_gd_csum(struct field_set_info *info, char *arg)
421{
422
423	if (strcmp(arg, "calc") == 0) {
424		ext2fs_group_desc_csum_set(current_fs, set_bg);
425		set_gd = current_fs->group_desc[set_bg];
426		printf("Checksum set to 0x%04x\n",
427		       current_fs->group_desc[set_bg].bg_checksum);
428		return 0;
429	}
430
431	return parse_uint(info, arg);
432}
433
434static void print_possible_fields(struct field_set_info *fields)
435{
436	struct field_set_info *ss;
437	const char	*type, *cmd;
438	FILE *f;
439	char name[40], idx[40];
440
441	if (fields == super_fields) {
442		type = "Superblock";
443		cmd = "set_super_value";
444	} else if (fields == inode_fields) {
445		type = "Inode";
446		cmd = "set_inode";
447	} else {
448		type = "Block group descriptor";
449		cmd = "set_block_group";
450	}
451	f = open_pager();
452
453	fprintf(f, "%s fields supported by the %s command:\n", type, cmd);
454
455	for (ss = fields ; ss->name ; ss++) {
456		type = "unknown";
457		if (ss->func == parse_string)
458			type = "string";
459		else if (ss->func == parse_int)
460			type = "integer";
461		else if (ss->func == parse_uint)
462			type = "unsigned integer";
463		else if (ss->func == parse_uuid)
464			type = "UUID";
465		else if (ss->func == parse_hashalg)
466			type = "hash algorithm";
467		else if (ss->func == parse_time)
468			type = "date/time";
469		else if (ss->func == parse_bmap)
470			type = "set physical->logical block map";
471		strcpy(name, ss->name);
472		if (ss->flags & FLAG_ARRAY) {
473			if (ss->max_idx > 0)
474				sprintf(idx, "[%d]", ss->max_idx);
475			else
476				strcpy(idx, "[]");
477			strcat(name, idx);
478		}
479		fprintf(f, "\t%-20s\t%s\n", name, type);
480	}
481	close_pager(f);
482}
483
484
485void do_set_super(int argc, char *argv[])
486{
487	const char *usage = "<field> <value>\n"
488		"\t\"set_super_value -l\" will list the names of "
489		"superblock fields\n\twhich can be set.";
490	static struct field_set_info *ss;
491
492	if ((argc == 2) && !strcmp(argv[1], "-l")) {
493		print_possible_fields(super_fields);
494		return;
495	}
496
497	if (common_args_process(argc, argv, 3, 3, "set_super_value",
498				usage, CHECK_FS_RW))
499		return;
500
501	if ((ss = find_field(super_fields, argv[1])) == 0) {
502		com_err(argv[0], 0, "invalid field specifier: %s", argv[1]);
503		return;
504	}
505	set_sb = *current_fs->super;
506	if (ss->func(ss, argv[2]) == 0) {
507		*current_fs->super = set_sb;
508		ext2fs_mark_super_dirty(current_fs);
509	}
510}
511
512void do_set_inode(int argc, char *argv[])
513{
514	const char *usage = "<inode> <field> <value>\n"
515		"\t\"set_inode_field -l\" will list the names of "
516		"the fields in an ext2 inode\n\twhich can be set.";
517	static struct field_set_info *ss;
518
519	if ((argc == 2) && !strcmp(argv[1], "-l")) {
520		print_possible_fields(inode_fields);
521		return;
522	}
523
524	if (common_args_process(argc, argv, 4, 4, "set_inode",
525				usage, CHECK_FS_RW))
526		return;
527
528	if ((ss = find_field(inode_fields, argv[2])) == 0) {
529		com_err(argv[0], 0, "invalid field specifier: %s", argv[2]);
530		return;
531	}
532
533	set_ino = string_to_inode(argv[1]);
534	if (!set_ino)
535		return;
536
537	if (debugfs_read_inode(set_ino, &set_inode, argv[1]))
538		return;
539
540	if (ss->func(ss, argv[3]) == 0) {
541		if (debugfs_write_inode(set_ino, &set_inode, argv[1]))
542			return;
543	}
544}
545
546void do_set_block_group_descriptor(int argc, char *argv[])
547{
548	const char *usage = "<bg number> <field> <value>\n"
549		"\t\"set_block_group_descriptor -l\" will list the names of "
550		"the fields in a block group descriptor\n\twhich can be set.";
551	struct field_set_info	*ss;
552	char			*end;
553
554	if ((argc == 2) && !strcmp(argv[1], "-l")) {
555		print_possible_fields(ext2_bg_fields);
556		return;
557	}
558
559	if (common_args_process(argc, argv, 4, 4, "set_block_group_descriptor",
560				usage, CHECK_FS_RW))
561		return;
562
563	set_bg = strtoul(argv[1], &end, 0);
564	if (*end) {
565		com_err(argv[0], 0, "invalid block group number: %s", argv[1]);
566		return;
567	}
568
569	if (set_bg >= current_fs->group_desc_count) {
570		com_err(argv[0], 0, "block group number too big: %d", set_bg);
571		return;
572	}
573
574
575	if ((ss = find_field(ext2_bg_fields, argv[2])) == 0) {
576		com_err(argv[0], 0, "invalid field specifier: %s", argv[2]);
577		return;
578	}
579
580	set_gd = current_fs->group_desc[set_bg];
581
582	if (ss->func(ss, argv[3]) == 0) {
583		current_fs->group_desc[set_bg] = set_gd;
584		ext2fs_mark_super_dirty(current_fs);
585	}
586}
587