Whamcloud - gitweb
Fix typo'ed version number in Debian changelog
[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_size, 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, 1, parse_uint },
140         { "fsize", &set_inode.osd2.linux2.l_i_fsize, 1, parse_uint },
141         { "uid_high", &set_inode.osd2.linux2.l_i_uid_high, 2, parse_uint },
142         { "gid_high", &set_inode.osd2.linux2.l_i_gid_high, 2, parse_uint },
143         { "author", &set_inode.osd2.hurd2.h_i_author, 4, 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         __u32           *ptr32, t;
282
283         ptr32 = (__u32 *) info->ptr;
284
285         t = string_to_time(arg);
286
287         if (t == ((time_t) -1)) {
288                 fprintf(stderr, "Couldn't parse '%s' for field %s.\n",
289                         arg, info->name);
290                 return EINVAL;
291         }
292         *ptr32 = t;
293         return 0;
294 }
295
296 static errcode_t parse_uuid(struct field_set_info *info, char *arg)
297 {
298         unsigned char * p = (unsigned char *) info->ptr;
299         
300         if ((strcasecmp(arg, "null") == 0) ||
301             (strcasecmp(arg, "clear") == 0)) {
302                 uuid_clear(p);
303         } else if (strcasecmp(arg, "time") == 0) {
304                 uuid_generate_time(p);
305         } else if (strcasecmp(arg, "random") == 0) {
306                 uuid_generate(p);
307         } else if (uuid_parse(arg, p)) {
308                 fprintf(stderr, "Invalid UUID format: %s\n", arg);
309                 return EINVAL;
310         }
311         return 0;
312 }
313
314 static errcode_t parse_hashalg(struct field_set_info *info, char *arg)
315 {
316         int     hashv;
317         unsigned char   *p = (unsigned char *) info->ptr;
318
319         hashv = e2p_string2hash(arg);
320         if (hashv < 0) {
321                 fprintf(stderr, "Invalid hash algorithm: %s\n", arg);
322                 return EINVAL;
323         }
324         *p = hashv;
325         return 0;
326 }
327
328 static errcode_t parse_bmap(struct field_set_info *info, char *arg)
329 {
330         unsigned long   num;
331         blk_t           blk;
332         errcode_t       retval;
333         char            *tmp;
334
335         num = strtoul(arg, &tmp, 0);
336         if (*tmp) {
337                 fprintf(stderr, "Couldn't parse '%s' for field %s.\n",
338                         arg, info->name);
339                 return EINVAL;
340         }
341         blk = num;
342
343         retval = ext2fs_bmap(current_fs, set_ino, &set_inode, 0, BMAP_SET, 
344                              array_idx, &blk);
345         if (retval) {
346                 com_err("set_inode", retval, "while setting block map");
347         }
348         return retval;
349 }
350
351
352 static void print_possible_fields(struct field_set_info *fields)
353 {
354         struct field_set_info *ss;
355         const char      *type, *cmd;
356         FILE *f;
357         char name[40], idx[40];
358
359         if (fields == super_fields) {
360                 type = "Superblock";
361                 cmd = "set_super_value";
362         } else {
363                 type = "Inode";
364                 cmd = "set_inode";
365         }
366         f = open_pager();
367
368         fprintf(f, "%s fields supported by the %s command:\n", type, cmd);
369
370         for (ss = fields ; ss->name ; ss++) {
371                 type = "unknown";
372                 if (ss->func == parse_string)
373                         type = "string";
374                 else if (ss->func == parse_int)
375                         type = "integer";
376                 else if (ss->func == parse_uint)
377                         type = "unsigned integer";
378                 else if (ss->func == parse_uuid)
379                         type = "UUID";
380                 else if (ss->func == parse_hashalg)
381                         type = "hash algorithm";
382                 else if (ss->func == parse_time)
383                         type = "date/time";
384                 else if (ss->func == parse_bmap)
385                         type = "set physical->logical block map";
386                 strcpy(name, ss->name);
387                 if (ss->flags & FLAG_ARRAY) {
388                         if (ss->max_idx > 0) 
389                                 sprintf(idx, "[%d]", ss->max_idx);
390                         else
391                                 strcpy(idx, "[]");
392                         strcat(name, idx);
393                 }
394                 fprintf(f, "\t%-20s\t%s\n", name, type);
395         }
396         close_pager(f);
397 }
398
399
400 void do_set_super(int argc, char *argv[])
401 {
402         const char *usage = "<field> <value>\n"
403                 "\t\"set_super_value -l\" will list the names of "
404                 "superblock fields\n\twhich can be set.";
405         static struct field_set_info *ss;
406         
407         if ((argc == 2) && !strcmp(argv[1], "-l")) {
408                 print_possible_fields(super_fields);
409                 return;
410         }
411
412         if (common_args_process(argc, argv, 3, 3, "set_super_value",
413                                 usage, CHECK_FS_RW))
414                 return;
415
416         if ((ss = find_field(super_fields, argv[1])) == 0) {
417                 com_err(argv[0], 0, "invalid field specifier: %s", argv[1]);
418                 return;
419         }
420         set_sb = *current_fs->super;
421         if (ss->func(ss, argv[2]) == 0) {
422                 *current_fs->super = set_sb;
423                 ext2fs_mark_super_dirty(current_fs);
424         }
425 }
426
427 void do_set_inode(int argc, char *argv[])
428 {
429         const char *usage = "<inode> <field> <value>\n"
430                 "\t\"set_inode_field -l\" will list the names of "
431                 "the fields in an ext2 inode\n\twhich can be set.";
432         static struct field_set_info *ss;
433         
434         if ((argc == 2) && !strcmp(argv[1], "-l")) {
435                 print_possible_fields(inode_fields);
436                 return;
437         }
438
439         if (common_args_process(argc, argv, 4, 4, "set_inode",
440                                 usage, CHECK_FS_RW))
441                 return;
442
443         if ((ss = find_field(inode_fields, argv[2])) == 0) {
444                 com_err(argv[0], 0, "invalid field specifier: %s", argv[2]);
445                 return;
446         }
447
448         set_ino = string_to_inode(argv[1]);
449         if (!set_ino)
450                 return;
451
452         if (debugfs_read_inode(set_ino, &set_inode, argv[1]))
453                 return;
454
455         if (ss->func(ss, argv[3]) == 0) {
456                 if (debugfs_write_inode(set_ino, &set_inode, argv[1]))
457                         return;
458         }
459 }