Whamcloud - gitweb
Reserve the codepoints for the new INCOMPAT feature ENCRYPT
[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         { "checksum_type", &set_sb.s_checksum_type, NULL, 1, parse_uint },
157         { "error_count", &set_sb.s_error_count, NULL, 4, parse_uint },
158         { "first_error_time", &set_sb.s_first_error_time, NULL, 4, parse_time },
159         { "first_error_ino", &set_sb.s_first_error_ino, NULL, 4, parse_uint },
160         { "first_error_block", &set_sb.s_first_error_block, NULL, 8, parse_uint },
161         { "first_error_func", &set_sb.s_first_error_func, NULL, 32, parse_string },
162         { "first_error_line", &set_sb.s_first_error_ino, NULL, 4, parse_uint },
163         { "last_error_time", &set_sb.s_last_error_time, NULL, 4, parse_time },
164         { "last_error_ino", &set_sb.s_last_error_ino, NULL, 4, parse_uint },
165         { "last_error_block", &set_sb.s_last_error_block, NULL, 8, parse_uint },
166         { "last_error_func", &set_sb.s_last_error_func, NULL, 32, parse_string },
167         { "last_error_line", &set_sb.s_last_error_ino, NULL, 4, parse_uint },
168         { "encrypt_algos", &set_sb.s_encrypt_algos, NULL, 1, parse_uint,
169           FLAG_ARRAY, 4 },
170         { 0, 0, 0, 0 }
171 };
172
173 static struct field_set_info inode_fields[] = {
174         { "inodes_count", &set_sb.s_inodes_count, NULL, 4, parse_uint },
175         { "mode", &set_inode.i_mode, NULL, 2, parse_uint },
176         { "uid", &set_inode.i_uid, &set_inode.osd2.linux2.l_i_uid_high,
177                 2, parse_uint },
178         { "size", &set_inode.i_size, &set_inode.i_size_high, 4, parse_uint },
179         { "atime", &set_inode.i_atime, NULL, 4, parse_time },
180         { "ctime", &set_inode.i_ctime, NULL, 4, parse_time },
181         { "mtime", &set_inode.i_mtime, NULL, 4, parse_time },
182         { "dtime", &set_inode.i_dtime, NULL, 4, parse_time },
183         { "gid", &set_inode.i_gid, &set_inode.osd2.linux2.l_i_gid_high,
184                 2, parse_uint },
185         { "links_count", &set_inode.i_links_count, NULL, 2, parse_uint },
186         /* Special case: i_blocks is 4 bytes, i_blocks_high is 2 bytes */
187         { "blocks", &set_inode.i_blocks, &set_inode.osd2.linux2.l_i_blocks_hi,
188                 6, parse_uint },
189         { "flags", &set_inode.i_flags, NULL, 4, parse_uint },
190         { "version", &set_inode.osd1.linux1.l_i_version,
191                 &set_inode.i_version_hi, 4, parse_uint },
192         { "translator", &set_inode.osd1.hurd1.h_i_translator, NULL, 4, parse_uint },
193         { "block", &set_inode.i_block[0], NULL, 4, parse_uint, FLAG_ARRAY,
194           EXT2_NDIR_BLOCKS },
195         { "block[IND]", &set_inode.i_block[EXT2_IND_BLOCK], NULL, 4, parse_uint },
196         { "block[DIND]", &set_inode.i_block[EXT2_DIND_BLOCK], NULL, 4, parse_uint },
197         { "block[TIND]", &set_inode.i_block[EXT2_TIND_BLOCK], NULL, 4, parse_uint },
198         { "generation", &set_inode.i_generation, NULL, 4, parse_uint },
199         /* Special case: i_file_acl_high is 2 bytes */
200         { "file_acl", &set_inode.i_file_acl, 
201                 &set_inode.osd2.linux2.l_i_file_acl_high, 6, parse_uint },
202         { "dir_acl", &set_inode.i_dir_acl, NULL, 4, parse_uint },
203         { "faddr", &set_inode.i_faddr, NULL, 4, parse_uint },
204         { "frag", &set_inode.osd2.hurd2.h_i_frag, NULL, 1, parse_uint },
205         { "fsize", &set_inode.osd2.hurd2.h_i_fsize, NULL, 1, parse_uint },
206         { "checksum", &set_inode.osd2.linux2.l_i_checksum_lo, 
207                 &set_inode.i_checksum_hi, 2, parse_uint },
208         { "author", &set_inode.osd2.hurd2.h_i_author, NULL,
209                 4, parse_uint },
210         { "extra_isize", &set_inode.i_extra_isize, NULL,
211                 2, parse_uint },
212         { "ctime_extra", &set_inode.i_ctime_extra, NULL,
213                 4, parse_uint },
214         { "mtime_extra", &set_inode.i_mtime_extra, NULL,
215                 4, parse_uint },
216         { "atime_extra", &set_inode.i_atime_extra, NULL,
217                 4, parse_uint },
218         { "crtime", &set_inode.i_crtime, NULL, 4, parse_uint },
219         { "crtime_extra", &set_inode.i_crtime_extra, NULL,
220                 4, parse_uint },
221         { "bmap", NULL, NULL, 4, parse_bmap, FLAG_ARRAY },
222         { 0, 0, 0, 0 }
223 };
224
225 static struct field_set_info ext2_bg_fields[] = {
226         { "block_bitmap", &set_gd.bg_block_bitmap, NULL, 4, parse_uint },
227         { "inode_bitmap", &set_gd.bg_inode_bitmap, NULL, 4, parse_uint },
228         { "inode_table", &set_gd.bg_inode_table, NULL, 4, parse_uint },
229         { "free_blocks_count", &set_gd.bg_free_blocks_count, NULL, 2, parse_uint },
230         { "free_inodes_count", &set_gd.bg_free_inodes_count, NULL, 2, parse_uint },
231         { "used_dirs_count", &set_gd.bg_used_dirs_count, NULL, 2, parse_uint },
232         { "flags", &set_gd.bg_flags, NULL, 2, parse_uint },
233         { "itable_unused", &set_gd.bg_itable_unused, NULL, 2, parse_uint },
234         { "checksum", &set_gd.bg_checksum, NULL, 2, parse_gd_csum },
235         { 0, 0, 0, 0 }
236 };
237
238 static struct field_set_info ext4_bg_fields[] = {
239         { "block_bitmap", &set_gd4.bg_block_bitmap,
240                 &set_gd4.bg_block_bitmap_hi, 4, parse_uint },
241         { "inode_bitmap", &set_gd4.bg_inode_bitmap,
242                 &set_gd4.bg_inode_bitmap_hi, 4, parse_uint },
243         { "inode_table", &set_gd4.bg_inode_table,
244                 &set_gd4.bg_inode_table_hi, 4, parse_uint },
245         { "free_blocks_count", &set_gd4.bg_free_blocks_count,
246                 &set_gd4.bg_free_blocks_count_hi, 2, parse_uint },
247         { "free_inodes_count", &set_gd4.bg_free_inodes_count,
248                 &set_gd4.bg_free_inodes_count_hi, 2, parse_uint },
249         { "used_dirs_count", &set_gd4.bg_used_dirs_count,
250                 &set_gd4.bg_used_dirs_count_hi, 2, parse_uint },
251         { "flags", &set_gd4.bg_flags, NULL, 2, parse_uint },
252         { "exclude_bitmap", &set_gd4.bg_exclude_bitmap_lo,
253                 &set_gd4.bg_exclude_bitmap_hi, 4, parse_uint },
254         { "block_bitmap_csum", &set_gd4.bg_block_bitmap_csum_lo,
255                 &set_gd4.bg_block_bitmap_csum_hi, 2, parse_uint },
256         { "inode_bitmap_csum", &set_gd4.bg_inode_bitmap_csum_lo,
257                 &set_gd4.bg_inode_bitmap_csum_hi, 2, parse_uint },
258         { "itable_unused", &set_gd4.bg_itable_unused,
259                 &set_gd4.bg_itable_unused_hi, 2, parse_uint },
260         { "checksum", &set_gd4.bg_checksum, NULL, 2, parse_gd_csum },
261         { 0, 0, 0, 0 }
262 };
263
264 static struct field_set_info mmp_fields[] = {
265         { "clear", &set_mmp.mmp_magic, NULL, sizeof(set_mmp), parse_mmp_clear },
266         { "magic", &set_mmp.mmp_magic, NULL, 4, parse_uint },
267         { "seq", &set_mmp.mmp_seq, NULL, 4, parse_uint },
268         { "time", &set_mmp.mmp_time, NULL, 8, parse_uint },
269         { "nodename", &set_mmp.mmp_nodename, NULL, sizeof(set_mmp.mmp_nodename),
270                 parse_string },
271         { "bdevname", &set_mmp.mmp_bdevname, NULL, sizeof(set_mmp.mmp_bdevname),
272                 parse_string },
273         { "check_interval", &set_mmp.mmp_check_interval, NULL, 2, parse_uint },
274         { "checksum", &set_mmp.mmp_checksum, NULL, 4, parse_uint },
275 };
276
277 static int check_suffix(const char *field)
278 {
279         int len = strlen(field);
280
281         if (len <= 3)
282                 return 0;
283         field += len-3;
284         if (!strcmp(field, "_lo"))
285                 return 1;
286         if (!strcmp(field, "_hi"))
287                 return 2;
288         return 0;
289 }
290
291 static struct field_set_info *find_field(struct field_set_info *fields,
292                                          char *field)
293 {
294         struct field_set_info *ss;
295         const char      *prefix;
296         char            *arg, *delim, *idx, *tmp;
297         int             suffix, prefix_len;
298
299         if (fields == super_fields)
300                 prefix = "s_";
301         else if (fields == inode_fields)
302                 prefix = "i_";
303         else
304                 prefix = "bg_";
305         prefix_len = strlen(prefix);
306         if (strncmp(field, prefix, prefix_len) == 0)
307                 field += prefix_len;
308
309         arg = malloc(strlen(field)+1);
310         if (!arg)
311                 return NULL;
312         strcpy(arg, field);
313
314         idx = strchr(arg, '[');
315         if (idx) {
316                 *idx++ = 0;
317                 delim = idx + strlen(idx) - 1;
318                 if (!*idx || *delim != ']')
319                         idx = 0;
320                 else
321                         *delim = 0;
322         }
323         /*
324          * Can we parse the number?
325          */
326         if (idx) {
327                 array_idx = strtol(idx, &tmp, 0);
328                 if (*tmp) {
329                         *(--idx) = '[';
330                         *delim = ']';
331                         idx = 0;
332                 }
333         }
334
335         /*
336          * If there is a valid _hi or a _lo suffix, strip it off
337          */
338         suffix = check_suffix(arg);
339         if (suffix > 0)
340                 arg[strlen(arg)-3] = 0;
341
342         for (ss = fields ; ss->name ; ss++) {
343                 if (suffix && ss->ptr2 == 0)
344                         continue;
345                 if (ss->flags & FLAG_ARRAY) {
346                         if (!idx || (strcmp(ss->name, arg) != 0))
347                                 continue;
348                         if (ss->max_idx > 0 && array_idx >= ss->max_idx)
349                                 continue;
350                 } else {
351                         if (strcmp(ss->name, arg) != 0)
352                                 continue;
353                 }
354                 free(arg);
355                 return ss;
356         }
357         free(arg);
358         return NULL;
359 }
360
361 /*
362  * Note: info->size == 6 is special; this means a base size 4 bytes,
363  * and secondiory (high) size of 2 bytes.  This is needed for the
364  * special case of i_blocks_high and i_file_acl_high.
365  */
366 static errcode_t parse_uint(struct field_set_info *info, char *field,
367                             char *arg)
368 {
369         unsigned long long n, num, mask, limit;
370         int suffix = check_suffix(field);
371         char *tmp;
372         void *field1 = info->ptr, *field2 = info->ptr2;
373         int size = (info->size == 6) ? 4 : info->size;
374         union {
375                 __u64   *ptr64;
376                 __u32   *ptr32;
377                 __u16   *ptr16;
378                 __u8    *ptr8;
379         } u;
380
381         if (suffix == 1)
382                 field2 = 0;
383         if (suffix == 2) {
384                 field1 = field2;
385                 field2 = 0;
386         }
387
388         u.ptr8 = (__u8 *) field1;
389         if (info->flags & FLAG_ARRAY)
390                 u.ptr8 += array_idx * info->size;
391
392         errno = 0;
393         num = STRTOULL(arg, &tmp, 0);
394         if (*tmp || errno) {
395                 fprintf(stderr, "Couldn't parse '%s' for field %s.\n",
396                         arg, info->name);
397                 return EINVAL;
398         }
399         mask = ~0ULL >> ((8 - size) * 8);
400         limit = ~0ULL >> ((8 - info->size) * 8);
401         if (field2 && info->size != 6)
402                 limit = ~0ULL >> ((8 - info->size*2) * 8);
403
404         if (num > limit) {
405                 fprintf(stderr, "Value '%s' exceeds field %s maximum %llu.\n",
406                         arg, info->name, limit);
407                 return EINVAL;
408         }
409         n = num & mask;
410         switch (size) {
411         case 8:
412                 /* Should never get here */
413                 fprintf(stderr, "64-bit field %s has a second 64-bit field\n"
414                         "defined; BUG?!?\n", info->name);
415                 *u.ptr64 = 0;
416                 break;
417         case 4:
418                 *u.ptr32 = n;
419                 break;
420         case 2:
421                 *u.ptr16 = n;
422                 break;
423         case 1:
424                 *u.ptr8 = n;
425                 break;
426         }
427         if (!field2)
428                 return 0;
429         n = num >> (size*8);
430         u.ptr8 = (__u8 *) field2;
431         if (info->size == 6)
432                 size = 2;
433         switch (size) {
434         case 8:
435                 *u.ptr64 = n;
436                 break;
437         case 4:
438                 *u.ptr32 = n;
439                 break;
440         case 2:
441                 *u.ptr16 = n;
442                 break;
443         case 1:
444                 *u.ptr8 = n;
445                 break;
446         }
447         return 0;
448 }
449
450 static errcode_t parse_int(struct field_set_info *info,
451                            char *field EXT2FS_ATTR((unused)), char *arg)
452 {
453         long    num;
454         char *tmp;
455         __s32   *ptr32;
456         __s16   *ptr16;
457         __s8    *ptr8;
458
459         num = strtol(arg, &tmp, 0);
460         if (*tmp) {
461                 fprintf(stderr, "Couldn't parse '%s' for field %s.\n",
462                         arg, info->name);
463                 return EINVAL;
464         }
465         switch (info->size) {
466         case 4:
467                 ptr32 = (__s32 *) info->ptr;
468                 *ptr32 = num;
469                 break;
470         case 2:
471                 ptr16 = (__s16 *) info->ptr;
472                 *ptr16 = num;
473                 break;
474         case 1:
475                 ptr8 = (__s8 *) info->ptr;
476                 *ptr8 = num;
477                 break;
478         }
479         return 0;
480 }
481
482 static errcode_t parse_string(struct field_set_info *info,
483                               char *field EXT2FS_ATTR((unused)), char *arg)
484 {
485         char    *cp = (char *) info->ptr;
486
487         if (strlen(arg) >= info->size) {
488                 fprintf(stderr, "Error maximum size for %s is %d.\n",
489                         info->name, info->size);
490                 return EINVAL;
491         }
492         strcpy(cp, arg);
493         return 0;
494 }
495
496 static errcode_t parse_time(struct field_set_info *info,
497                             char *field EXT2FS_ATTR((unused)), char *arg)
498 {
499         time_t          t;
500         __u32           *ptr32;
501
502         ptr32 = (__u32 *) info->ptr;
503
504         t = string_to_time(arg);
505
506         if (t == ((time_t) -1)) {
507                 fprintf(stderr, "Couldn't parse '%s' for field %s.\n",
508                         arg, info->name);
509                 return EINVAL;
510         }
511         *ptr32 = t;
512         return 0;
513 }
514
515 static errcode_t parse_uuid(struct field_set_info *info,
516                             char *field EXT2FS_ATTR((unused)), char *arg)
517 {
518         unsigned char * p = (unsigned char *) info->ptr;
519
520         if ((strcasecmp(arg, "null") == 0) ||
521             (strcasecmp(arg, "clear") == 0)) {
522                 uuid_clear(p);
523         } else if (strcasecmp(arg, "time") == 0) {
524                 uuid_generate_time(p);
525         } else if (strcasecmp(arg, "random") == 0) {
526                 uuid_generate(p);
527         } else if (uuid_parse(arg, p)) {
528                 fprintf(stderr, "Invalid UUID format: %s\n", arg);
529                 return EINVAL;
530         }
531         return 0;
532 }
533
534 static errcode_t parse_hashalg(struct field_set_info *info,
535                                char *field EXT2FS_ATTR((unused)), char *arg)
536 {
537         int     hashv;
538         unsigned char   *p = (unsigned char *) info->ptr;
539
540         hashv = e2p_string2hash(arg);
541         if (hashv < 0) {
542                 fprintf(stderr, "Invalid hash algorithm: %s\n", arg);
543                 return EINVAL;
544         }
545         *p = hashv;
546         return 0;
547 }
548
549 static errcode_t parse_bmap(struct field_set_info *info,
550                             char *field EXT2FS_ATTR((unused)), char *arg)
551 {
552         blk64_t         blk;
553         errcode_t       retval;
554         char            *tmp;
555
556         blk = strtoull(arg, &tmp, 0);
557         if (*tmp) {
558                 fprintf(stderr, "Couldn't parse '%s' for field %s.\n",
559                         arg, info->name);
560                 return EINVAL;
561         }
562
563         retval = ext2fs_bmap2(current_fs, set_ino,
564                               (struct ext2_inode *) &set_inode,
565                               NULL, BMAP_ALLOC | BMAP_SET, array_idx, NULL,
566                               &blk);
567         if (retval) {
568                 com_err("set_inode", retval, "while setting block map");
569         }
570         return retval;
571 }
572
573 static errcode_t parse_gd_csum(struct field_set_info *info, char *field,
574                                char *arg)
575 {
576
577         if (strcmp(arg, "calc") == 0) {
578                 ext2fs_group_desc_csum_set(current_fs, set_bg);
579                 memcpy(&set_gd, ext2fs_group_desc(current_fs,
580                                         current_fs->group_desc,
581                                         set_bg),
582                         sizeof(set_gd));
583                 printf("Checksum set to 0x%04x\n",
584                        ext2fs_bg_checksum(current_fs, set_bg));
585                 return 0;
586         }
587
588         return parse_uint(info, field, arg);
589 }
590
591 static void print_possible_fields(struct field_set_info *fields)
592 {
593         struct field_set_info *ss;
594         const char      *type, *cmd;
595         FILE *f;
596         char name[40], idx[40];
597
598         if (fields == super_fields) {
599                 type = "Superblock";
600                 cmd = "set_super_value";
601         } else if (fields == inode_fields) {
602                 type = "Inode";
603                 cmd = "set_inode";
604         } else if (fields == mmp_fields) {
605                 type = "MMP";
606                 cmd = "set_mmp_value";
607         } else {
608                 type = "Block group descriptor";
609                 cmd = "set_block_group";
610         }
611         f = open_pager();
612
613         fprintf(f, "%s fields supported by the %s command:\n", type, cmd);
614
615         for (ss = fields ; ss->name ; ss++) {
616                 type = "unknown";
617                 if (ss->func == parse_string)
618                         type = "string";
619                 else if (ss->func == parse_int)
620                         type = "integer";
621                 else if (ss->func == parse_uint)
622                         type = "unsigned integer";
623                 else if (ss->func == parse_uuid)
624                         type = "UUID";
625                 else if (ss->func == parse_hashalg)
626                         type = "hash algorithm";
627                 else if (ss->func == parse_time)
628                         type = "date/time";
629                 else if (ss->func == parse_bmap)
630                         type = "set physical->logical block map";
631                 else if (ss->func == parse_gd_csum)
632                         type = "unsigned integer OR \"calc\"";
633                 strcpy(name, ss->name);
634                 if (ss->flags & FLAG_ARRAY) {
635                         if (ss->max_idx > 0)
636                                 sprintf(idx, "[%d]", ss->max_idx);
637                         else
638                                 strcpy(idx, "[]");
639                         strcat(name, idx);
640                 }
641                 if (ss->ptr2)
642                         strcat(name, "[_hi|_lo]");
643                 fprintf(f, "\t%-25s\t%s\n", name, type);
644         }
645         close_pager(f);
646 }
647
648
649 void do_set_super(int argc, char *argv[])
650 {
651         const char *usage = "<field> <value>\n"
652                 "\t\"set_super_value -l\" will list the names of "
653                 "superblock fields\n\twhich can be set.";
654         static struct field_set_info *ss;
655
656         if ((argc == 2) && !strcmp(argv[1], "-l")) {
657                 print_possible_fields(super_fields);
658                 return;
659         }
660
661         if (common_args_process(argc, argv, 3, 3, "set_super_value",
662                                 usage, CHECK_FS_RW))
663                 return;
664
665         if ((ss = find_field(super_fields, argv[1])) == 0) {
666                 com_err(argv[0], 0, "invalid field specifier: %s", argv[1]);
667                 return;
668         }
669         set_sb = *current_fs->super;
670         if (ss->func(ss, argv[1], argv[2]) == 0) {
671                 *current_fs->super = set_sb;
672                 ext2fs_mark_super_dirty(current_fs);
673         }
674 }
675
676 void do_set_inode(int argc, char *argv[])
677 {
678         const char *usage = "<inode> <field> <value>\n"
679                 "\t\"set_inode_field -l\" will list the names of "
680                 "the fields in an ext2 inode\n\twhich can be set.";
681         static struct field_set_info *ss;
682
683         if ((argc == 2) && !strcmp(argv[1], "-l")) {
684                 print_possible_fields(inode_fields);
685                 return;
686         }
687
688         if (common_args_process(argc, argv, 4, 4, "set_inode",
689                                 usage, CHECK_FS_RW))
690                 return;
691
692         if ((ss = find_field(inode_fields, argv[2])) == 0) {
693                 com_err(argv[0], 0, "invalid field specifier: %s", argv[2]);
694                 return;
695         }
696
697         set_ino = string_to_inode(argv[1]);
698         if (!set_ino)
699                 return;
700
701         if (debugfs_read_inode_full(set_ino,
702                         (struct ext2_inode *) &set_inode, argv[1],
703                                     sizeof(set_inode)))
704                 return;
705
706         if (ss->func(ss, argv[2], argv[3]) == 0) {
707                 if (debugfs_write_inode_full(set_ino, 
708                              (struct ext2_inode *) &set_inode,
709                              argv[1], sizeof(set_inode)))
710                         return;
711         }
712 }
713
714 void do_set_block_group_descriptor(int argc, char *argv[])
715 {
716         const char *usage = "<bg number> <field> <value>\n"
717                 "\t\"set_block_group_descriptor -l\" will list the names of "
718                 "the fields in a block group descriptor\n\twhich can be set.";
719         struct field_set_info   *table;
720         struct field_set_info   *ss;
721         char                    *end;
722         void                    *edit, *target;
723         int                     size;
724
725         /*
726          * Determine whether we are editing an ext2 or ext4 block group
727          * descriptor.  Descriptors larger than ext4_group_desc cannot
728          * have their fields edited yet, because they do not have any
729          * names assigned.  When that happens, this function needs to
730          * be updated for the new descriptor struct and fields.
731          */
732         if (current_fs &&
733             EXT2_DESC_SIZE(current_fs->super) >= EXT2_MIN_DESC_SIZE_64BIT) {
734                 table = ext4_bg_fields;
735                 edit = &set_gd4;
736                 size = sizeof(set_gd4);
737         } else {
738                 table = ext2_bg_fields;
739                 edit = &set_gd;
740                 size = sizeof(set_gd);
741         }
742
743         if ((argc == 2) && !strcmp(argv[1], "-l")) {
744                 print_possible_fields(table);
745                 return;
746         }
747
748         if (common_args_process(argc, argv, 4, 4, "set_block_group_descriptor",
749                                 usage, CHECK_FS_RW))
750                 return;
751
752         set_bg = strtoul(argv[1], &end, 0);
753         if (*end) {
754                 com_err(argv[0], 0, "invalid block group number: %s", argv[1]);
755                 return;
756         }
757
758         if (set_bg >= current_fs->group_desc_count) {
759                 com_err(argv[0], 0, "block group number too big: %d", set_bg);
760                 return;
761         }
762
763         if ((ss = find_field(table, argv[2])) == 0) {
764                 com_err(argv[0], 0, "invalid field specifier: %s", argv[2]);
765                 return;
766         }
767
768         target = ext2fs_group_desc(current_fs, current_fs->group_desc, set_bg);
769         memcpy(edit, target, size);
770         if (ss->func(ss, argv[2], argv[3]) == 0) {
771                 memcpy(target, edit, size);
772                 ext2fs_mark_super_dirty(current_fs);
773         }
774 }
775
776 static errcode_t parse_mmp_clear(struct field_set_info *info,
777                                  char *field EXT2FS_ATTR((unused)),
778                                  char *arg EXT2FS_ATTR((unused)))
779 {
780         errcode_t retval;
781
782         retval = ext2fs_mmp_clear(current_fs);
783         if (retval != 0)
784                 com_err("set_mmp_value", retval, "while clearing MMP block\n");
785         else
786                 memcpy(info->ptr, current_fs->mmp_buf, info->size);
787
788         return 1; /* we don't need the MMP block written again */
789 }
790
791 void do_set_mmp_value(int argc, char *argv[])
792 {
793 #ifdef CONFIG_MMP
794         const char *usage = "<field> <value>\n"
795                 "\t\"set_mmp_value -l\" will list the names of "
796                 "MMP fields\n\twhich can be set.";
797         static struct field_set_info *smmp;
798         struct mmp_struct *mmp_s;
799         errcode_t retval;
800
801         if (argc == 2 && strcmp(argv[1], "-l") == 0) {
802                 print_possible_fields(mmp_fields);
803                 return;
804         }
805
806         if (check_fs_open(argv[0]))
807                 return;
808
809         if (current_fs->super->s_mmp_block == 0) {
810                 com_err(argv[0], 0, "no MMP block allocated\n");
811                 return;
812         }
813
814         if (common_args_process(argc, argv, 2, 3, "set_mmp_value",
815                                 usage, CHECK_FS_RW))
816                 return;
817
818         mmp_s = current_fs->mmp_buf;
819         if (mmp_s == NULL) {
820                 retval = ext2fs_get_mem(current_fs->blocksize, &mmp_s);
821                 if (retval) {
822                         com_err(argv[0], retval, "allocating MMP buffer\n");
823                         return;
824                 }
825                 retval = ext2fs_mmp_read(current_fs,
826                                          current_fs->super->s_mmp_block, mmp_s);
827                 if (retval) {
828                         com_err(argv[0], retval, "reading MMP block %llu.\n",
829                                 (long long)current_fs->super->s_mmp_block);
830                         ext2fs_free_mem(&mmp_s);
831                         return;
832                 }
833                 current_fs->mmp_buf = mmp_s;
834         }
835
836         smmp = find_field(mmp_fields, argv[1]);
837         if (smmp == 0) {
838                 com_err(argv[0], 0, "invalid field specifier: %s", argv[1]);
839                 return;
840         }
841
842         set_mmp = *mmp_s;
843         if (smmp->func(smmp, argv[1], argv[2]) == 0) {
844                 ext2fs_mmp_write(current_fs, current_fs->super->s_mmp_block,
845                                  &set_mmp);
846                 *mmp_s = set_mmp;
847         }
848 #else
849         fprintf(stdout, "MMP is unsupported, please recompile with "
850                         "--enable-mmp\n");
851 #endif
852 }
853