Whamcloud - gitweb
e2scan: a tool for fast namespace/inode scanning
[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 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 "config.h"
21 #include <stdio.h>
22 #include <unistd.h>
23 #include <stdlib.h>
24 #include <ctype.h>
25 #include <string.h>
26 #include <strings.h>
27 #include <time.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #ifdef HAVE_ERRNO_H
31 #include <errno.h>
32 #endif
33 #if HAVE_STRINGS_H
34 #include <strings.h>
35 #endif
36 #include <fcntl.h>
37 #include <utime.h>
38
39 #include "debugfs.h"
40 #include "uuid/uuid.h"
41 #include "e2p/e2p.h"
42
43 static struct ext2_super_block set_sb;
44 static struct ext2_inode_large set_inode;
45 static struct ext2_group_desc set_gd;
46 static struct ext4_group_desc set_gd4;
47 static struct mmp_struct set_mmp;
48 static dgrp_t set_bg;
49 static ext2_ino_t set_ino;
50 static int array_idx;
51
52 #define FLAG_ARRAY      0x0001
53
54 struct field_set_info {
55         const char      *name;
56         void    *ptr;
57         void    *ptr2;
58         unsigned int    size;
59         errcode_t (*func)(struct field_set_info *info, char *field, char *arg);
60         int flags;
61         int max_idx;
62 };
63
64 static errcode_t parse_uint(struct field_set_info *info, char *field, char *arg);
65 static errcode_t parse_int(struct field_set_info *info, char *field, char *arg);
66 static errcode_t parse_string(struct field_set_info *info, char *field, char *arg);
67 static errcode_t parse_uuid(struct field_set_info *info, char *field, char *arg);
68 static errcode_t parse_hashalg(struct field_set_info *info, char *field, char *arg);
69 static errcode_t parse_time(struct field_set_info *info, char *field, char *arg);
70 static errcode_t parse_bmap(struct field_set_info *info, char *field, char *arg);
71 static errcode_t parse_gd_csum(struct field_set_info *info, char *field, char *arg);
72 static errcode_t parse_mmp_clear(struct field_set_info *info, char *field,
73                                  char *arg);
74
75 static struct field_set_info super_fields[] = {
76         { "inodes_count", &set_sb.s_inodes_count, NULL, 4, parse_uint },
77         { "blocks_count", &set_sb.s_blocks_count, &set_sb.s_blocks_count_hi,
78                 4, parse_uint },
79         { "r_blocks_count", &set_sb.s_r_blocks_count,
80                 &set_sb.s_r_blocks_count_hi, 4, parse_uint },
81         { "free_blocks_count", &set_sb.s_free_blocks_count,
82                 &set_sb.s_free_blocks_hi, 4, parse_uint },
83         { "free_inodes_count", &set_sb.s_free_inodes_count, NULL, 4, parse_uint },
84         { "first_data_block", &set_sb.s_first_data_block, NULL, 4, parse_uint },
85         { "log_block_size", &set_sb.s_log_block_size, NULL, 4, parse_uint },
86         { "log_cluster_size", &set_sb.s_log_cluster_size, NULL, 4, parse_int },
87         { "blocks_per_group", &set_sb.s_blocks_per_group, NULL, 4, parse_uint },
88         { "clusters_per_group", &set_sb.s_clusters_per_group, NULL, 4, parse_uint },
89         { "inodes_per_group", &set_sb.s_inodes_per_group, NULL, 4, parse_uint },
90         { "mtime", &set_sb.s_mtime, NULL, 4, parse_time },
91         { "wtime", &set_sb.s_wtime, NULL, 4, parse_time },
92         { "mnt_count", &set_sb.s_mnt_count, NULL, 2, parse_uint },
93         { "max_mnt_count", &set_sb.s_max_mnt_count, NULL, 2, parse_int },
94         /* s_magic */
95         { "state", &set_sb.s_state, NULL, 2, parse_uint },
96         { "errors", &set_sb.s_errors, NULL, 2, parse_uint },
97         { "minor_rev_level", &set_sb.s_minor_rev_level, NULL, 2, parse_uint },
98         { "lastcheck", &set_sb.s_lastcheck, NULL, 4, parse_time },
99         { "checkinterval", &set_sb.s_checkinterval, NULL, 4, parse_uint },
100         { "creator_os", &set_sb.s_creator_os, NULL, 4, parse_uint },
101         { "rev_level", &set_sb.s_rev_level, NULL, 4, parse_uint },
102         { "def_resuid", &set_sb.s_def_resuid, NULL, 2, parse_uint },
103         { "def_resgid", &set_sb.s_def_resgid, NULL, 2, parse_uint },
104         { "first_ino", &set_sb.s_first_ino, NULL, 4, parse_uint },
105         { "inode_size", &set_sb.s_inode_size, NULL, 2, parse_uint },
106         { "block_group_nr", &set_sb.s_block_group_nr, NULL, 2, parse_uint },
107         { "feature_compat", &set_sb.s_feature_compat, NULL, 4, parse_uint },
108         { "feature_incompat", &set_sb.s_feature_incompat, NULL, 4, parse_uint },
109         { "feature_ro_compat", &set_sb.s_feature_ro_compat, NULL, 4, parse_uint },
110         { "uuid", &set_sb.s_uuid, NULL, 16, parse_uuid },
111         { "volume_name",  &set_sb.s_volume_name, NULL, 16, parse_string },
112         { "last_mounted",  &set_sb.s_last_mounted, NULL, 64, parse_string },
113         { "lastcheck",  &set_sb.s_lastcheck, NULL, 4, parse_uint },
114         { "algorithm_usage_bitmap", &set_sb.s_algorithm_usage_bitmap, NULL,
115                   4, parse_uint },
116         { "prealloc_blocks", &set_sb.s_prealloc_blocks, NULL, 1, parse_uint },
117         { "prealloc_dir_blocks", &set_sb.s_prealloc_dir_blocks, NULL, 1,
118                   parse_uint },
119         { "reserved_gdt_blocks", &set_sb.s_reserved_gdt_blocks, NULL, 2,
120                   parse_uint },
121         { "journal_uuid", &set_sb.s_journal_uuid, NULL, 16, parse_uuid },
122         { "journal_inum", &set_sb.s_journal_inum, NULL, 4, parse_uint },
123         { "journal_dev", &set_sb.s_journal_dev, NULL, 4, parse_uint },
124         { "last_orphan", &set_sb.s_last_orphan, NULL, 4, parse_uint },
125         { "hash_seed", &set_sb.s_hash_seed, NULL, 16, parse_uuid },
126         { "def_hash_version", &set_sb.s_def_hash_version, NULL, 1, parse_hashalg },
127         { "jnl_backup_type", &set_sb.s_jnl_backup_type, NULL, 1, parse_uint },
128         { "desc_size", &set_sb.s_desc_size, NULL, 2, parse_uint },
129         { "default_mount_opts", &set_sb.s_default_mount_opts, NULL, 4, parse_uint },
130         { "first_meta_bg", &set_sb.s_first_meta_bg, NULL, 4, parse_uint },
131         { "mkfs_time", &set_sb.s_mkfs_time, NULL, 4, parse_time },
132         { "jnl_blocks", &set_sb.s_jnl_blocks[0], NULL, 4, parse_uint, FLAG_ARRAY,
133           17 },
134         { "min_extra_isize", &set_sb.s_min_extra_isize, NULL, 2, parse_uint },
135         { "want_extra_isize", &set_sb.s_want_extra_isize, NULL, 2, parse_uint },
136         { "flags", &set_sb.s_flags, NULL, 4, parse_uint },
137         { "raid_stride", &set_sb.s_raid_stride, NULL, 2, parse_uint },
138         { "min_extra_isize", &set_sb.s_min_extra_isize, NULL, 4, parse_uint },
139         { "mmp_interval", &set_sb.s_mmp_update_interval, NULL, 2, parse_uint },
140         { "mmp_block", &set_sb.s_mmp_block, NULL, 8, parse_uint },
141         { "raid_stripe_width", &set_sb.s_raid_stripe_width, NULL, 4, parse_uint },
142         { "log_groups_per_flex", &set_sb.s_log_groups_per_flex, NULL, 1, parse_uint },
143         { "kbytes_written", &set_sb.s_kbytes_written, NULL, 8, parse_uint },
144         { "snapshot_inum", &set_sb.s_snapshot_inum, NULL, 4, parse_uint },
145         { "snapshot_id", &set_sb.s_snapshot_id, NULL, 4, parse_uint },
146         { "snapshot_r_blocks_count", &set_sb.s_snapshot_r_blocks_count,
147           NULL, 8, parse_uint },
148         { "snapshot_list", &set_sb.s_snapshot_list, NULL, 4, parse_uint },
149         { "mount_opts",  &set_sb.s_mount_opts, NULL, 64, parse_string },
150         { "usr_quota_inum", &set_sb.s_usr_quota_inum, NULL, 4, parse_uint },
151         { "grp_quota_inum", &set_sb.s_grp_quota_inum, NULL, 4, parse_uint },
152         { "overhead_blocks", &set_sb.s_overhead_blocks, NULL, 4, parse_uint },
153         { "backup_bgs", &set_sb.s_backup_bgs[0], NULL, 4, parse_uint,
154           FLAG_ARRAY, 2 },
155         { "checksum", &set_sb.s_checksum, NULL, 4, parse_uint },
156         { "error_count", &set_sb.s_error_count, NULL, 4, parse_uint },
157         { "first_error_time", &set_sb.s_first_error_time, NULL, 4, parse_time },
158         { "first_error_ino", &set_sb.s_first_error_ino, NULL, 4, parse_uint },
159         { "first_error_block", &set_sb.s_first_error_block, NULL, 8, parse_uint },
160         { "first_error_func", &set_sb.s_first_error_func, NULL, 32, parse_string },
161         { "first_error_line", &set_sb.s_first_error_ino, NULL, 4, parse_uint },
162         { "last_error_time", &set_sb.s_last_error_time, NULL, 4, parse_time },
163         { "last_error_ino", &set_sb.s_last_error_ino, NULL, 4, parse_uint },
164         { "last_error_block", &set_sb.s_last_error_block, NULL, 8, parse_uint },
165         { "last_error_func", &set_sb.s_last_error_func, NULL, 32, parse_string },
166         { "last_error_line", &set_sb.s_last_error_ino, NULL, 4, parse_uint },
167         { 0, 0, 0, 0 }
168 };
169
170 static struct field_set_info inode_fields[] = {
171         { "inodes_count", &set_sb.s_inodes_count, NULL, 4, parse_uint },
172         { "mode", &set_inode.i_mode, NULL, 2, parse_uint },
173         { "uid", &set_inode.i_uid, &set_inode.osd2.linux2.l_i_uid_high,
174                 2, parse_uint },
175         { "size", &set_inode.i_size, &set_inode.i_size_high, 4, parse_uint },
176         { "atime", &set_inode.i_atime, NULL, 4, parse_time },
177         { "ctime", &set_inode.i_ctime, NULL, 4, parse_time },
178         { "mtime", &set_inode.i_mtime, NULL, 4, parse_time },
179         { "dtime", &set_inode.i_dtime, NULL, 4, parse_time },
180         { "gid", &set_inode.i_gid, &set_inode.osd2.linux2.l_i_gid_high,
181                 2, parse_uint },
182         { "links_count", &set_inode.i_links_count, NULL, 2, parse_uint },
183         /* Special case: i_blocks is 4 bytes, i_blocks_high is 2 bytes */
184         { "blocks", &set_inode.i_blocks, &set_inode.osd2.linux2.l_i_blocks_hi,
185                 6, parse_uint },
186         { "flags", &set_inode.i_flags, NULL, 4, parse_uint },
187         { "version", &set_inode.osd1.linux1.l_i_version,
188                 &set_inode.i_version_hi, 4, parse_uint },
189         { "translator", &set_inode.osd1.hurd1.h_i_translator, NULL, 4, parse_uint },
190         { "block", &set_inode.i_block[0], NULL, 4, parse_uint, FLAG_ARRAY,
191           EXT2_NDIR_BLOCKS },
192         { "block[IND]", &set_inode.i_block[EXT2_IND_BLOCK], NULL, 4, parse_uint },
193         { "block[DIND]", &set_inode.i_block[EXT2_DIND_BLOCK], NULL, 4, parse_uint },
194         { "block[TIND]", &set_inode.i_block[EXT2_TIND_BLOCK], NULL, 4, parse_uint },
195         { "generation", &set_inode.i_generation, NULL, 4, parse_uint },
196         /* Special case: i_file_acl_high is 2 bytes */
197         { "file_acl", &set_inode.i_file_acl, 
198                 &set_inode.osd2.linux2.l_i_file_acl_high, 6, parse_uint },
199         { "dir_acl", &set_inode.i_dir_acl, NULL, 4, parse_uint },
200         { "faddr", &set_inode.i_faddr, NULL, 4, parse_uint },
201         { "frag", &set_inode.osd2.hurd2.h_i_frag, NULL, 1, parse_uint },
202         { "fsize", &set_inode.osd2.hurd2.h_i_fsize, NULL, 1, parse_uint },
203         { "checksum", &set_inode.osd2.linux2.l_i_checksum_lo, 
204                 &set_inode.i_checksum_hi, 2, parse_uint },
205         { "author", &set_inode.osd2.hurd2.h_i_author, NULL,
206                 4, parse_uint },
207         { "extra_isize", &set_inode.i_extra_isize, NULL,
208                 2, parse_uint },
209         { "ctime_extra", &set_inode.i_ctime_extra, NULL,
210                 4, parse_uint },
211         { "mtime_extra", &set_inode.i_mtime_extra, NULL,
212                 4, parse_uint },
213         { "atime_extra", &set_inode.i_atime_extra, NULL,
214                 4, parse_uint },
215         { "crtime", &set_inode.i_crtime, NULL, 4, parse_uint },
216         { "crtime_extra", &set_inode.i_crtime_extra, NULL,
217                 4, parse_uint },
218         { "bmap", NULL, NULL, 4, parse_bmap, FLAG_ARRAY },
219         { 0, 0, 0, 0 }
220 };
221
222 static struct field_set_info ext2_bg_fields[] = {
223         { "block_bitmap", &set_gd.bg_block_bitmap, NULL, 4, parse_uint },
224         { "inode_bitmap", &set_gd.bg_inode_bitmap, NULL, 4, parse_uint },
225         { "inode_table", &set_gd.bg_inode_table, NULL, 4, parse_uint },
226         { "free_blocks_count", &set_gd.bg_free_blocks_count, NULL, 2, parse_uint },
227         { "free_inodes_count", &set_gd.bg_free_inodes_count, NULL, 2, parse_uint },
228         { "used_dirs_count", &set_gd.bg_used_dirs_count, NULL, 2, parse_uint },
229         { "flags", &set_gd.bg_flags, NULL, 2, parse_uint },
230         { "itable_unused", &set_gd.bg_itable_unused, NULL, 2, parse_uint },
231         { "checksum", &set_gd.bg_checksum, NULL, 2, parse_gd_csum },
232         { 0, 0, 0, 0 }
233 };
234
235 static struct field_set_info ext4_bg_fields[] = {
236         { "block_bitmap", &set_gd4.bg_block_bitmap,
237                 &set_gd4.bg_block_bitmap_hi, 4, parse_uint },
238         { "inode_bitmap", &set_gd4.bg_inode_bitmap,
239                 &set_gd4.bg_inode_bitmap_hi, 4, parse_uint },
240         { "inode_table", &set_gd4.bg_inode_table,
241                 &set_gd4.bg_inode_table_hi, 4, parse_uint },
242         { "free_blocks_count", &set_gd4.bg_free_blocks_count,
243                 &set_gd4.bg_free_blocks_count_hi, 2, parse_uint },
244         { "free_inodes_count", &set_gd4.bg_free_inodes_count,
245                 &set_gd4.bg_free_inodes_count_hi, 2, parse_uint },
246         { "used_dirs_count", &set_gd4.bg_used_dirs_count,
247                 &set_gd4.bg_used_dirs_count_hi, 2, parse_uint },
248         { "flags", &set_gd4.bg_flags, NULL, 2, parse_uint },
249         { "exclude_bitmap", &set_gd4.bg_exclude_bitmap_lo,
250                 &set_gd4.bg_exclude_bitmap_hi, 4, parse_uint },
251         { "block_bitmap_csum", &set_gd4.bg_block_bitmap_csum_lo,
252                 &set_gd4.bg_block_bitmap_csum_hi, 2, parse_uint },
253         { "inode_bitmap_csum", &set_gd4.bg_inode_bitmap_csum_lo,
254                 &set_gd4.bg_inode_bitmap_csum_hi, 2, parse_uint },
255         { "itable_unused", &set_gd4.bg_itable_unused,
256                 &set_gd4.bg_itable_unused_hi, 2, parse_uint },
257         { "checksum", &set_gd4.bg_checksum, NULL, 2, parse_gd_csum },
258         { 0, 0, 0, 0 }
259 };
260
261 static struct field_set_info mmp_fields[] = {
262         { "clear", &set_mmp.mmp_magic, NULL, sizeof(set_mmp), parse_mmp_clear },
263         { "magic", &set_mmp.mmp_magic, NULL, 4, parse_uint },
264         { "seq", &set_mmp.mmp_seq, NULL, 4, parse_uint },
265         { "time", &set_mmp.mmp_time, NULL, 8, parse_uint },
266         { "nodename", &set_mmp.mmp_nodename, NULL, sizeof(set_mmp.mmp_nodename),
267                 parse_string },
268         { "bdevname", &set_mmp.mmp_bdevname, NULL, sizeof(set_mmp.mmp_bdevname),
269                 parse_string },
270         { "check_interval", &set_mmp.mmp_check_interval, NULL, 2, parse_uint },
271 };
272
273 static int check_suffix(const char *field)
274 {
275         int len = strlen(field);
276
277         if (len <= 3)
278                 return 0;
279         field += len-3;
280         if (!strcmp(field, "_lo"))
281                 return 1;
282         if (!strcmp(field, "_hi"))
283                 return 2;
284         return 0;
285 }
286
287 static struct field_set_info *find_field(struct field_set_info *fields,
288                                          char *field)
289 {
290         struct field_set_info *ss;
291         const char      *prefix;
292         char            *arg, *delim, *idx, *tmp;
293         int             suffix, prefix_len;
294
295         if (fields == super_fields)
296                 prefix = "s_";
297         else if (fields == inode_fields)
298                 prefix = "i_";
299         else
300                 prefix = "bg_";
301         prefix_len = strlen(prefix);
302         if (strncmp(field, prefix, prefix_len) == 0)
303                 field += prefix_len;
304
305         arg = malloc(strlen(field)+1);
306         if (!arg)
307                 return NULL;
308         strcpy(arg, field);
309
310         idx = strchr(arg, '[');
311         if (idx) {
312                 *idx++ = 0;
313                 delim = idx + strlen(idx) - 1;
314                 if (!*idx || *delim != ']')
315                         idx = 0;
316                 else
317                         *delim = 0;
318         }
319         /*
320          * Can we parse the number?
321          */
322         if (idx) {
323                 array_idx = strtol(idx, &tmp, 0);
324                 if (*tmp) {
325                         *(--idx) = '[';
326                         *delim = ']';
327                         idx = 0;
328                 }
329         }
330
331         /*
332          * If there is a valid _hi or a _lo suffix, strip it off
333          */
334         suffix = check_suffix(arg);
335         if (suffix > 0)
336                 arg[strlen(arg)-3] = 0;
337
338         for (ss = fields ; ss->name ; ss++) {
339                 if (suffix && ss->ptr2 == 0)
340                         continue;
341                 if (ss->flags & FLAG_ARRAY) {
342                         if (!idx || (strcmp(ss->name, arg) != 0))
343                                 continue;
344                         if (ss->max_idx > 0 && array_idx >= ss->max_idx)
345                                 continue;
346                 } else {
347                         if (strcmp(ss->name, arg) != 0)
348                                 continue;
349                 }
350                 free(arg);
351                 return ss;
352         }
353         free(arg);
354         return NULL;
355 }
356
357 /*
358  * Note: info->size == 6 is special; this means a base size 4 bytes,
359  * and secondiory (high) size of 2 bytes.  This is needed for the
360  * special case of i_blocks_high and i_file_acl_high.
361  */
362 static errcode_t parse_uint(struct field_set_info *info, char *field,
363                             char *arg)
364 {
365         unsigned long long n, num, mask, limit;
366         int suffix = check_suffix(field);
367         char *tmp;
368         void *field1 = info->ptr, *field2 = info->ptr2;
369         int size = (info->size == 6) ? 4 : info->size;
370         union {
371                 __u64   *ptr64;
372                 __u32   *ptr32;
373                 __u16   *ptr16;
374                 __u8    *ptr8;
375         } u;
376
377         if (suffix == 1)
378                 field2 = 0;
379         if (suffix == 2) {
380                 field1 = field2;
381                 field2 = 0;
382         }
383
384         u.ptr8 = (__u8 *) field1;
385         if (info->flags & FLAG_ARRAY)
386                 u.ptr8 += array_idx * info->size;
387
388         errno = 0;
389         num = STRTOULL(arg, &tmp, 0);
390         if (*tmp || errno) {
391                 fprintf(stderr, "Couldn't parse '%s' for field %s.\n",
392                         arg, info->name);
393                 return EINVAL;
394         }
395         mask = ~0ULL >> ((8 - size) * 8);
396         limit = ~0ULL >> ((8 - info->size) * 8);
397         if (field2 && info->size != 6)
398                 limit = ~0ULL >> ((8 - info->size*2) * 8);
399
400         if (num > limit) {
401                 fprintf(stderr, "Value '%s' exceeds field %s maximum %llu.\n",
402                         arg, info->name, limit);
403                 return EINVAL;
404         }
405         n = num & mask;
406         switch (size) {
407         case 8:
408                 /* Should never get here */
409                 fprintf(stderr, "64-bit field %s has a second 64-bit field\n"
410                         "defined; BUG?!?\n", info->name);
411                 *u.ptr64 = 0;
412                 break;
413         case 4:
414                 *u.ptr32 = n;
415                 break;
416         case 2:
417                 *u.ptr16 = n;
418                 break;
419         case 1:
420                 *u.ptr8 = n;
421                 break;
422         }
423         if (!field2)
424                 return 0;
425         n = num >> (size*8);
426         u.ptr8 = (__u8 *) field2;
427         if (info->size == 6)
428                 size = 2;
429         switch (size) {
430         case 8:
431                 *u.ptr64 = n;
432                 break;
433         case 4:
434                 *u.ptr32 = n;
435                 break;
436         case 2:
437                 *u.ptr16 = n;
438                 break;
439         case 1:
440                 *u.ptr8 = n;
441                 break;
442         }
443         return 0;
444 }
445
446 static errcode_t parse_int(struct field_set_info *info,
447                            char *field EXT2FS_ATTR((unused)), char *arg)
448 {
449         long    num;
450         char *tmp;
451         __s32   *ptr32;
452         __s16   *ptr16;
453         __s8    *ptr8;
454
455         num = strtol(arg, &tmp, 0);
456         if (*tmp) {
457                 fprintf(stderr, "Couldn't parse '%s' for field %s.\n",
458                         arg, info->name);
459                 return EINVAL;
460         }
461         switch (info->size) {
462         case 4:
463                 ptr32 = (__s32 *) info->ptr;
464                 *ptr32 = num;
465                 break;
466         case 2:
467                 ptr16 = (__s16 *) info->ptr;
468                 *ptr16 = num;
469                 break;
470         case 1:
471                 ptr8 = (__s8 *) info->ptr;
472                 *ptr8 = num;
473                 break;
474         }
475         return 0;
476 }
477
478 static errcode_t parse_string(struct field_set_info *info,
479                               char *field EXT2FS_ATTR((unused)), char *arg)
480 {
481         char    *cp = (char *) info->ptr;
482
483         if (strlen(arg) >= info->size) {
484                 fprintf(stderr, "Error maximum size for %s is %d.\n",
485                         info->name, info->size);
486                 return EINVAL;
487         }
488         strcpy(cp, arg);
489         return 0;
490 }
491
492 static errcode_t parse_time(struct field_set_info *info,
493                             char *field EXT2FS_ATTR((unused)), char *arg)
494 {
495         time_t          t;
496         __u32           *ptr32;
497
498         ptr32 = (__u32 *) info->ptr;
499
500         t = string_to_time(arg);
501
502         if (t == ((time_t) -1)) {
503                 fprintf(stderr, "Couldn't parse '%s' for field %s.\n",
504                         arg, info->name);
505                 return EINVAL;
506         }
507         *ptr32 = t;
508         return 0;
509 }
510
511 static errcode_t parse_uuid(struct field_set_info *info,
512                             char *field EXT2FS_ATTR((unused)), char *arg)
513 {
514         unsigned char * p = (unsigned char *) info->ptr;
515
516         if ((strcasecmp(arg, "null") == 0) ||
517             (strcasecmp(arg, "clear") == 0)) {
518                 uuid_clear(p);
519         } else if (strcasecmp(arg, "time") == 0) {
520                 uuid_generate_time(p);
521         } else if (strcasecmp(arg, "random") == 0) {
522                 uuid_generate(p);
523         } else if (uuid_parse(arg, p)) {
524                 fprintf(stderr, "Invalid UUID format: %s\n", arg);
525                 return EINVAL;
526         }
527         return 0;
528 }
529
530 static errcode_t parse_hashalg(struct field_set_info *info,
531                                char *field EXT2FS_ATTR((unused)), char *arg)
532 {
533         int     hashv;
534         unsigned char   *p = (unsigned char *) info->ptr;
535
536         hashv = e2p_string2hash(arg);
537         if (hashv < 0) {
538                 fprintf(stderr, "Invalid hash algorithm: %s\n", arg);
539                 return EINVAL;
540         }
541         *p = hashv;
542         return 0;
543 }
544
545 static errcode_t parse_bmap(struct field_set_info *info,
546                             char *field EXT2FS_ATTR((unused)), char *arg)
547 {
548         blk64_t         blk;
549         errcode_t       retval;
550         char            *tmp;
551
552         blk = strtoull(arg, &tmp, 0);
553         if (*tmp) {
554                 fprintf(stderr, "Couldn't parse '%s' for field %s.\n",
555                         arg, info->name);
556                 return EINVAL;
557         }
558
559         retval = ext2fs_bmap2(current_fs, set_ino,
560                               (struct ext2_inode *) &set_inode,
561                               NULL, BMAP_ALLOC | BMAP_SET, array_idx, NULL,
562                               &blk);
563         if (retval) {
564                 com_err("set_inode", retval, "while setting block map");
565         }
566         return retval;
567 }
568
569 static errcode_t parse_gd_csum(struct field_set_info *info, char *field,
570                                char *arg)
571 {
572
573         if (strcmp(arg, "calc") == 0) {
574                 ext2fs_group_desc_csum_set(current_fs, set_bg);
575                 memcpy(&set_gd, ext2fs_group_desc(current_fs,
576                                         current_fs->group_desc,
577                                         set_bg),
578                         sizeof(set_gd));
579                 printf("Checksum set to 0x%04x\n",
580                        ext2fs_bg_checksum(current_fs, set_bg));
581                 return 0;
582         }
583
584         return parse_uint(info, field, arg);
585 }
586
587 static void print_possible_fields(struct field_set_info *fields)
588 {
589         struct field_set_info *ss;
590         const char      *type, *cmd;
591         FILE *f;
592         char name[40], idx[40];
593
594         if (fields == super_fields) {
595                 type = "Superblock";
596                 cmd = "set_super_value";
597         } else if (fields == inode_fields) {
598                 type = "Inode";
599                 cmd = "set_inode";
600         } else if (fields == mmp_fields) {
601                 type = "MMP";
602                 cmd = "set_mmp_value";
603         } else {
604                 type = "Block group descriptor";
605                 cmd = "set_block_group";
606         }
607         f = open_pager();
608
609         fprintf(f, "%s fields supported by the %s command:\n", type, cmd);
610
611         for (ss = fields ; ss->name ; ss++) {
612                 type = "unknown";
613                 if (ss->func == parse_string)
614                         type = "string";
615                 else if (ss->func == parse_int)
616                         type = "integer";
617                 else if (ss->func == parse_uint)
618                         type = "unsigned integer";
619                 else if (ss->func == parse_uuid)
620                         type = "UUID";
621                 else if (ss->func == parse_hashalg)
622                         type = "hash algorithm";
623                 else if (ss->func == parse_time)
624                         type = "date/time";
625                 else if (ss->func == parse_bmap)
626                         type = "set physical->logical block map";
627                 else if (ss->func == parse_gd_csum)
628                         type = "unsigned integer OR \"calc\"";
629                 strcpy(name, ss->name);
630                 if (ss->flags & FLAG_ARRAY) {
631                         if (ss->max_idx > 0)
632                                 sprintf(idx, "[%d]", ss->max_idx);
633                         else
634                                 strcpy(idx, "[]");
635                         strcat(name, idx);
636                 }
637                 if (ss->ptr2)
638                         strcat(name, "[_hi|_lo]");
639                 fprintf(f, "\t%-25s\t%s\n", name, type);
640         }
641         close_pager(f);
642 }
643
644
645 void do_set_super(int argc, char *argv[])
646 {
647         const char *usage = "<field> <value>\n"
648                 "\t\"set_super_value -l\" will list the names of "
649                 "superblock fields\n\twhich can be set.";
650         static struct field_set_info *ss;
651
652         if ((argc == 2) && !strcmp(argv[1], "-l")) {
653                 print_possible_fields(super_fields);
654                 return;
655         }
656
657         if (common_args_process(argc, argv, 3, 3, "set_super_value",
658                                 usage, CHECK_FS_RW))
659                 return;
660
661         if ((ss = find_field(super_fields, argv[1])) == 0) {
662                 com_err(argv[0], 0, "invalid field specifier: %s", argv[1]);
663                 return;
664         }
665         set_sb = *current_fs->super;
666         if (ss->func(ss, argv[1], argv[2]) == 0) {
667                 *current_fs->super = set_sb;
668                 ext2fs_mark_super_dirty(current_fs);
669         }
670 }
671
672 void do_set_inode(int argc, char *argv[])
673 {
674         const char *usage = "<inode> <field> <value>\n"
675                 "\t\"set_inode_field -l\" will list the names of "
676                 "the fields in an ext2 inode\n\twhich can be set.";
677         static struct field_set_info *ss;
678
679         if ((argc == 2) && !strcmp(argv[1], "-l")) {
680                 print_possible_fields(inode_fields);
681                 return;
682         }
683
684         if (common_args_process(argc, argv, 4, 4, "set_inode",
685                                 usage, CHECK_FS_RW))
686                 return;
687
688         if ((ss = find_field(inode_fields, argv[2])) == 0) {
689                 com_err(argv[0], 0, "invalid field specifier: %s", argv[2]);
690                 return;
691         }
692
693         set_ino = string_to_inode(argv[1]);
694         if (!set_ino)
695                 return;
696
697         if (debugfs_read_inode_full(set_ino,
698                         (struct ext2_inode *) &set_inode, argv[1],
699                                     sizeof(set_inode)))
700                 return;
701
702         if (ss->func(ss, argv[2], argv[3]) == 0) {
703                 if (debugfs_write_inode_full(set_ino, 
704                              (struct ext2_inode *) &set_inode,
705                              argv[1], sizeof(set_inode)))
706                         return;
707         }
708 }
709
710 void do_set_block_group_descriptor(int argc, char *argv[])
711 {
712         const char *usage = "<bg number> <field> <value>\n"
713                 "\t\"set_block_group_descriptor -l\" will list the names of "
714                 "the fields in a block group descriptor\n\twhich can be set.";
715         struct field_set_info   *table;
716         struct field_set_info   *ss;
717         char                    *end;
718         void                    *edit, *target;
719         int                     size;
720
721         /*
722          * Determine whether we are editing an ext2 or ext4 block group
723          * descriptor.  Descriptors larger than ext4_group_desc cannot
724          * have their fields edited yet, because they do not have any
725          * names assigned.  When that happens, this function needs to
726          * be updated for the new descriptor struct and fields.
727          */
728         if (current_fs &&
729             EXT2_DESC_SIZE(current_fs->super) >= EXT2_MIN_DESC_SIZE_64BIT) {
730                 table = ext4_bg_fields;
731                 edit = &set_gd4;
732                 size = sizeof(set_gd4);
733         } else {
734                 table = ext2_bg_fields;
735                 edit = &set_gd;
736                 size = sizeof(set_gd);
737         }
738
739         if ((argc == 2) && !strcmp(argv[1], "-l")) {
740                 print_possible_fields(table);
741                 return;
742         }
743
744         if (common_args_process(argc, argv, 4, 4, "set_block_group_descriptor",
745                                 usage, CHECK_FS_RW))
746                 return;
747
748         set_bg = strtoul(argv[1], &end, 0);
749         if (*end) {
750                 com_err(argv[0], 0, "invalid block group number: %s", argv[1]);
751                 return;
752         }
753
754         if (set_bg >= current_fs->group_desc_count) {
755                 com_err(argv[0], 0, "block group number too big: %d", set_bg);
756                 return;
757         }
758
759         if ((ss = find_field(table, argv[2])) == 0) {
760                 com_err(argv[0], 0, "invalid field specifier: %s", argv[2]);
761                 return;
762         }
763
764         target = ext2fs_group_desc(current_fs, current_fs->group_desc, set_bg);
765         memcpy(edit, target, size);
766         if (ss->func(ss, argv[2], argv[3]) == 0) {
767                 memcpy(target, edit, size);
768                 ext2fs_mark_super_dirty(current_fs);
769         }
770 }
771
772 static errcode_t parse_mmp_clear(struct field_set_info *info,
773                                  char *field EXT2FS_ATTR((unused)),
774                                  char *arg EXT2FS_ATTR((unused)))
775 {
776         errcode_t retval;
777
778         retval = ext2fs_mmp_clear(current_fs);
779         if (retval != 0)
780                 com_err("set_mmp_value", retval, "while clearing MMP block\n");
781         else
782                 memcpy(info->ptr, current_fs->mmp_buf, info->size);
783
784         return 1; /* we don't need the MMP block written again */
785 }
786
787 void do_set_mmp_value(int argc, char *argv[])
788 {
789         const char *usage = "<field> <value>\n"
790                 "\t\"set_mmp_value -l\" will list the names of "
791                 "MMP fields\n\twhich can be set.";
792         static struct field_set_info *smmp;
793         struct mmp_struct *mmp_s;
794         errcode_t retval;
795
796         if (argc == 2 && strcmp(argv[1], "-l") == 0) {
797                 print_possible_fields(mmp_fields);
798                 return;
799         }
800
801         if (check_fs_open(argv[0]))
802                 return;
803
804         if (current_fs->super->s_mmp_block == 0) {
805                 com_err(argv[0], 0, "no MMP block allocated\n");
806                 return;
807         }
808
809         if (common_args_process(argc, argv, 2, 3, "set_mmp_value",
810                                 usage, CHECK_FS_RW))
811                 return;
812
813         mmp_s = current_fs->mmp_buf;
814         if (mmp_s == NULL) {
815                 retval = ext2fs_get_mem(current_fs->blocksize, &mmp_s);
816                 if (retval) {
817                         com_err(argv[0], retval, "allocating MMP buffer\n");
818                         return;
819                 }
820                 retval = ext2fs_mmp_read(current_fs,
821                                          current_fs->super->s_mmp_block, mmp_s);
822                 if (retval) {
823                         com_err(argv[0], retval, "reading MMP block %llu.\n",
824                                 (long long)current_fs->super->s_mmp_block);
825                         ext2fs_free_mem(&mmp_s);
826                         return;
827                 }
828                 current_fs->mmp_buf = mmp_s;
829         }
830
831         smmp = find_field(mmp_fields, argv[1]);
832         if (smmp == 0) {
833                 com_err(argv[0], 0, "invalid field specifier: %s", argv[1]);
834                 return;
835         }
836
837         set_mmp = *mmp_s;
838         if (smmp->func(smmp, argv[1], argv[2]) == 0) {
839                 ext2fs_mmp_write(current_fs, current_fs->super->s_mmp_block,
840                                  &set_mmp);
841                 *mmp_s = set_mmp;
842         }
843 }
844