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