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