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