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