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