Whamcloud - gitweb
Merge branch 'maint' into next
[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 #include <assert.h>
34 #if HAVE_STRINGS_H
35 #include <strings.h>
36 #endif
37 #include <fcntl.h>
38 #include <utime.h>
39
40 #include "debugfs.h"
41 #include "uuid/uuid.h"
42 #include "e2p/e2p.h"
43
44 static struct ext2_super_block set_sb;
45 static struct ext2_inode_large set_inode;
46 static struct ext2_group_desc set_gd;
47 static struct ext4_group_desc set_gd4;
48 static struct mmp_struct set_mmp;
49 static dgrp_t set_bg;
50 static ext2_ino_t set_ino;
51 static int array_idx;
52
53 #define FLAG_ARRAY      0x0001
54 #define FLAG_ALIAS      0x0002  /* Data intersects with other field */
55
56 struct field_set_info {
57         const char      *name;
58         void    *ptr;
59         void    *ptr2;
60         unsigned int    size;
61         errcode_t (*func)(struct field_set_info *info, char *field, char *arg);
62         int flags;
63         int max_idx;
64 };
65
66 static errcode_t parse_uint(struct field_set_info *info, char *field, char *arg);
67 static errcode_t parse_int(struct field_set_info *info, char *field, char *arg);
68 static errcode_t parse_string(struct field_set_info *info, char *field, char *arg);
69 static errcode_t parse_uuid(struct field_set_info *info, char *field, char *arg);
70 static errcode_t parse_hashalg(struct field_set_info *info, char *field, char *arg);
71 static errcode_t parse_time(struct field_set_info *info, char *field, char *arg);
72 static errcode_t parse_bmap(struct field_set_info *info, char *field, char *arg);
73 static errcode_t parse_gd_csum(struct field_set_info *info, char *field, char *arg);
74 static errcode_t parse_mmp_clear(struct field_set_info *info, char *field,
75                                  char *arg);
76
77 static struct field_set_info super_fields[] = {
78         { "inodes_count", &set_sb.s_inodes_count, NULL, 4, parse_uint },
79         { "blocks_count", &set_sb.s_blocks_count, &set_sb.s_blocks_count_hi,
80                 4, parse_uint },
81         { "r_blocks_count", &set_sb.s_r_blocks_count,
82                 &set_sb.s_r_blocks_count_hi, 4, parse_uint },
83         { "free_blocks_count", &set_sb.s_free_blocks_count,
84                 &set_sb.s_free_blocks_hi, 4, parse_uint },
85         { "free_inodes_count", &set_sb.s_free_inodes_count, NULL, 4, parse_uint },
86         { "first_data_block", &set_sb.s_first_data_block, NULL, 4, parse_uint },
87         { "log_block_size", &set_sb.s_log_block_size, NULL, 4, parse_uint },
88         { "log_cluster_size", &set_sb.s_log_cluster_size, NULL, 4, parse_int },
89         { "blocks_per_group", &set_sb.s_blocks_per_group, NULL, 4, parse_uint },
90         { "clusters_per_group", &set_sb.s_clusters_per_group, NULL, 4, parse_uint },
91         { "inodes_per_group", &set_sb.s_inodes_per_group, NULL, 4, parse_uint },
92         { "mtime", &set_sb.s_mtime, NULL, 4, parse_time },
93         { "wtime", &set_sb.s_wtime, NULL, 4, parse_time },
94         { "mnt_count", &set_sb.s_mnt_count, NULL, 2, parse_uint },
95         { "max_mnt_count", &set_sb.s_max_mnt_count, NULL, 2, parse_int },
96         /* s_magic */
97         { "state", &set_sb.s_state, NULL, 2, parse_uint },
98         { "errors", &set_sb.s_errors, NULL, 2, parse_uint },
99         { "minor_rev_level", &set_sb.s_minor_rev_level, NULL, 2, parse_uint },
100         { "lastcheck", &set_sb.s_lastcheck, NULL, 4, parse_time },
101         { "checkinterval", &set_sb.s_checkinterval, NULL, 4, parse_uint },
102         { "creator_os", &set_sb.s_creator_os, NULL, 4, parse_uint },
103         { "rev_level", &set_sb.s_rev_level, NULL, 4, parse_uint },
104         { "def_resuid", &set_sb.s_def_resuid, NULL, 2, parse_uint },
105         { "def_resgid", &set_sb.s_def_resgid, NULL, 2, parse_uint },
106         { "first_ino", &set_sb.s_first_ino, NULL, 4, parse_uint },
107         { "inode_size", &set_sb.s_inode_size, NULL, 2, parse_uint },
108         { "block_group_nr", &set_sb.s_block_group_nr, NULL, 2, parse_uint },
109         { "feature_compat", &set_sb.s_feature_compat, NULL, 4, parse_uint },
110         { "feature_incompat", &set_sb.s_feature_incompat, NULL, 4, parse_uint },
111         { "feature_ro_compat", &set_sb.s_feature_ro_compat, NULL, 4, parse_uint },
112         { "uuid", &set_sb.s_uuid, NULL, 16, parse_uuid },
113         { "volume_name",  &set_sb.s_volume_name, NULL, 16, parse_string },
114         { "last_mounted",  &set_sb.s_last_mounted, NULL, 64, parse_string },
115         { "algorithm_usage_bitmap", &set_sb.s_algorithm_usage_bitmap, NULL,
116                   4, parse_uint },
117         { "prealloc_blocks", &set_sb.s_prealloc_blocks, NULL, 1, parse_uint },
118         { "prealloc_dir_blocks", &set_sb.s_prealloc_dir_blocks, NULL, 1,
119                   parse_uint },
120         { "reserved_gdt_blocks", &set_sb.s_reserved_gdt_blocks, NULL, 2,
121                   parse_uint },
122         { "journal_uuid", &set_sb.s_journal_uuid, NULL, 16, parse_uuid },
123         { "journal_inum", &set_sb.s_journal_inum, NULL, 4, parse_uint },
124         { "journal_dev", &set_sb.s_journal_dev, NULL, 4, parse_uint },
125         { "last_orphan", &set_sb.s_last_orphan, NULL, 4, parse_uint },
126         { "hash_seed", &set_sb.s_hash_seed, NULL, 16, parse_uuid },
127         { "def_hash_version", &set_sb.s_def_hash_version, NULL, 1, parse_hashalg },
128         { "jnl_backup_type", &set_sb.s_jnl_backup_type, NULL, 1, parse_uint },
129         { "desc_size", &set_sb.s_desc_size, NULL, 2, parse_uint },
130         { "default_mount_opts", &set_sb.s_default_mount_opts, NULL, 4, parse_uint },
131         { "first_meta_bg", &set_sb.s_first_meta_bg, NULL, 4, parse_uint },
132         { "mkfs_time", &set_sb.s_mkfs_time, NULL, 4, parse_time },
133         { "jnl_blocks", &set_sb.s_jnl_blocks[0], NULL, 4, parse_uint, FLAG_ARRAY,
134           17 },
135         { "min_extra_isize", &set_sb.s_min_extra_isize, NULL, 2, parse_uint },
136         { "want_extra_isize", &set_sb.s_want_extra_isize, NULL, 2, parse_uint },
137         { "flags", &set_sb.s_flags, NULL, 4, parse_uint },
138         { "raid_stride", &set_sb.s_raid_stride, NULL, 2, 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_line, 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_line, 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         { "mode", &set_inode.i_mode, NULL, 2, parse_uint },
178         { "uid", &set_inode.i_uid, &set_inode.osd2.linux2.l_i_uid_high,
179                 2, parse_uint },
180         { "size", &set_inode.i_size, &set_inode.i_size_high, 4, parse_uint },
181         { "atime", &set_inode.i_atime, NULL, 4, parse_time },
182         { "ctime", &set_inode.i_ctime, NULL, 4, parse_time },
183         { "mtime", &set_inode.i_mtime, NULL, 4, parse_time },
184         { "dtime", &set_inode.i_dtime, NULL, 4, parse_time },
185         { "gid", &set_inode.i_gid, &set_inode.osd2.linux2.l_i_gid_high,
186                 2, parse_uint },
187         { "links_count", &set_inode.i_links_count, NULL, 2, parse_uint },
188         /* Special case: i_blocks is 4 bytes, i_blocks_high is 2 bytes */
189         { "blocks", &set_inode.i_blocks, &set_inode.osd2.linux2.l_i_blocks_hi,
190                 6, parse_uint },
191         { "flags", &set_inode.i_flags, NULL, 4, parse_uint },
192         { "version", &set_inode.osd1.linux1.l_i_version,
193                 &set_inode.i_version_hi, 4, parse_uint },
194         { "translator", &set_inode.osd1.hurd1.h_i_translator, NULL,
195                 4, parse_uint, FLAG_ALIAS },
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, FLAG_ALIAS },
206         { "faddr", &set_inode.i_faddr, NULL, 4, parse_uint },
207         { "frag", &set_inode.osd2.hurd2.h_i_frag, NULL, 1, parse_uint, FLAG_ALIAS },
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, FLAG_ALIAS },
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),
269                 parse_mmp_clear, FLAG_ALIAS },
270         { "magic", &set_mmp.mmp_magic, NULL, 4, parse_uint },
271         { "seq", &set_mmp.mmp_seq, NULL, 4, parse_uint },
272         { "time", &set_mmp.mmp_time, NULL, 8, parse_uint },
273         { "nodename", &set_mmp.mmp_nodename, NULL, sizeof(set_mmp.mmp_nodename),
274                 parse_string },
275         { "bdevname", &set_mmp.mmp_bdevname, NULL, sizeof(set_mmp.mmp_bdevname),
276                 parse_string },
277         { "check_interval", &set_mmp.mmp_check_interval, NULL, 2, parse_uint },
278         { "checksum", &set_mmp.mmp_checksum, NULL, 4, parse_uint },
279         { 0, 0, 0, 0 }
280 };
281
282 #ifdef UNITTEST
283
284 static void do_verify_field_set_info(struct field_set_info *fields,
285                 const void *data, size_t size)
286 {
287         struct field_set_info *ss, *ss2;
288         const char *begin = (char *)data;
289         const char *end = begin + size;
290
291         for (ss = fields ; ss->name ; ss++) {
292                 const char *ptr;
293
294                 /* Check pointers */
295                 ptr = ss->ptr;
296                 assert(!ptr || (ptr >= begin && ptr < end));
297                 ptr = ss->ptr2;
298                 assert(!ptr || (ptr >= begin && ptr < end));
299
300                 /* Check function */
301                 assert(ss->func);
302
303                 for (ss2 = fields ; ss2 != ss ; ss2++) {
304                         /* Check duplicate names */
305                         assert(strcmp(ss->name, ss2->name));
306
307                         if (ss->flags & FLAG_ALIAS || ss2->flags & FLAG_ALIAS)
308                                 continue;
309                         /* Check false aliases, might be copy-n-paste error */
310                         assert(!ss->ptr || (ss->ptr != ss2->ptr &&
311                                             ss->ptr != ss2->ptr2));
312                         assert(!ss->ptr2 || (ss->ptr2 != ss2->ptr &&
313                                              ss->ptr2 != ss2->ptr2));
314                 }
315         }
316 }
317
318 int main(int argc, char **argv)
319 {
320         do_verify_field_set_info(super_fields, &set_sb, sizeof(set_sb));
321         do_verify_field_set_info(inode_fields, &set_inode, sizeof(set_inode));
322         do_verify_field_set_info(ext2_bg_fields, &set_gd, sizeof(set_gd));
323         do_verify_field_set_info(ext4_bg_fields, &set_gd4, sizeof(set_gd4));
324         do_verify_field_set_info(mmp_fields, &set_mmp, sizeof(set_mmp));
325         return 0;
326 }
327
328 ext2_filsys current_fs;
329 ext2_ino_t root, cwd;
330
331 #endif /* UNITTEST */
332
333 static int check_suffix(const char *field)
334 {
335         int len = strlen(field);
336
337         if (len <= 3)
338                 return 0;
339         field += len-3;
340         if (!strcmp(field, "_lo"))
341                 return 1;
342         if (!strcmp(field, "_hi"))
343                 return 2;
344         return 0;
345 }
346
347 static struct field_set_info *find_field(struct field_set_info *fields,
348                                          char *field)
349 {
350         struct field_set_info *ss;
351         const char      *prefix;
352         char            *arg, *delim, *idx, *tmp;
353         int             suffix, prefix_len;
354
355         if (fields == super_fields)
356                 prefix = "s_";
357         else if (fields == inode_fields)
358                 prefix = "i_";
359         else
360                 prefix = "bg_";
361         prefix_len = strlen(prefix);
362         if (strncmp(field, prefix, prefix_len) == 0)
363                 field += prefix_len;
364
365         arg = malloc(strlen(field)+1);
366         if (!arg)
367                 return NULL;
368         strcpy(arg, field);
369
370         idx = strchr(arg, '[');
371         if (idx) {
372                 *idx++ = 0;
373                 delim = idx + strlen(idx) - 1;
374                 if (!*idx || *delim != ']')
375                         idx = 0;
376                 else
377                         *delim = 0;
378         }
379         /*
380          * Can we parse the number?
381          */
382         if (idx) {
383                 array_idx = strtol(idx, &tmp, 0);
384                 if (*tmp) {
385                         *(--idx) = '[';
386                         *delim = ']';
387                         idx = 0;
388                 }
389         }
390
391         /*
392          * If there is a valid _hi or a _lo suffix, strip it off
393          */
394         suffix = check_suffix(arg);
395         if (suffix > 0)
396                 arg[strlen(arg)-3] = 0;
397
398         for (ss = fields ; ss->name ; ss++) {
399                 if (suffix && ss->ptr2 == 0)
400                         continue;
401                 if (ss->flags & FLAG_ARRAY) {
402                         if (!idx || (strcmp(ss->name, arg) != 0))
403                                 continue;
404                         if (ss->max_idx > 0 && array_idx >= ss->max_idx)
405                                 continue;
406                 } else {
407                         if (strcmp(ss->name, arg) != 0)
408                                 continue;
409                 }
410                 free(arg);
411                 return ss;
412         }
413         free(arg);
414         return NULL;
415 }
416
417 /*
418  * Note: info->size == 6 is special; this means a base size 4 bytes,
419  * and secondiory (high) size of 2 bytes.  This is needed for the
420  * special case of i_blocks_high and i_file_acl_high.
421  */
422 static errcode_t parse_uint(struct field_set_info *info, char *field,
423                             char *arg)
424 {
425         unsigned long long n, num, mask, limit;
426         int suffix = check_suffix(field);
427         char *tmp;
428         void *field1 = info->ptr, *field2 = info->ptr2;
429         int size = (info->size == 6) ? 4 : info->size;
430         union {
431                 __u64   *ptr64;
432                 __u32   *ptr32;
433                 __u16   *ptr16;
434                 __u8    *ptr8;
435         } u;
436
437         if (suffix == 1)
438                 field2 = 0;
439         if (suffix == 2) {
440                 field1 = field2;
441                 field2 = 0;
442         }
443
444         u.ptr8 = (__u8 *) field1;
445         if (info->flags & FLAG_ARRAY)
446                 u.ptr8 += array_idx * info->size;
447
448         errno = 0;
449         num = STRTOULL(arg, &tmp, 0);
450         if (*tmp || errno) {
451                 fprintf(stderr, "Couldn't parse '%s' for field %s.\n",
452                         arg, info->name);
453                 return EINVAL;
454         }
455         mask = ~0ULL >> ((8 - size) * 8);
456         limit = ~0ULL >> ((8 - info->size) * 8);
457         if (field2 && info->size != 6)
458                 limit = ~0ULL >> ((8 - info->size*2) * 8);
459
460         if (num > limit) {
461                 fprintf(stderr, "Value '%s' exceeds field %s maximum %llu.\n",
462                         arg, info->name, limit);
463                 return EINVAL;
464         }
465         n = num & mask;
466         switch (size) {
467         case 8:
468                 /* Should never get here */
469                 fprintf(stderr, "64-bit field %s has a second 64-bit field\n"
470                         "defined; BUG?!?\n", info->name);
471                 *u.ptr64 = 0;
472                 break;
473         case 4:
474                 *u.ptr32 = n;
475                 break;
476         case 2:
477                 *u.ptr16 = n;
478                 break;
479         case 1:
480                 *u.ptr8 = n;
481                 break;
482         }
483         if (!field2)
484                 return 0;
485         n = num >> (size*8);
486         u.ptr8 = (__u8 *) field2;
487         if (info->size == 6)
488                 size = 2;
489         switch (size) {
490         case 8:
491                 *u.ptr64 = n;
492                 break;
493         case 4:
494                 *u.ptr32 = n;
495                 break;
496         case 2:
497                 *u.ptr16 = n;
498                 break;
499         case 1:
500                 *u.ptr8 = n;
501                 break;
502         }
503         return 0;
504 }
505
506 static errcode_t parse_int(struct field_set_info *info,
507                            char *field EXT2FS_ATTR((unused)), char *arg)
508 {
509         long    num;
510         char *tmp;
511         __s32   *ptr32;
512         __s16   *ptr16;
513         __s8    *ptr8;
514
515         num = strtol(arg, &tmp, 0);
516         if (*tmp) {
517                 fprintf(stderr, "Couldn't parse '%s' for field %s.\n",
518                         arg, info->name);
519                 return EINVAL;
520         }
521         switch (info->size) {
522         case 4:
523                 ptr32 = (__s32 *) info->ptr;
524                 *ptr32 = num;
525                 break;
526         case 2:
527                 ptr16 = (__s16 *) info->ptr;
528                 *ptr16 = num;
529                 break;
530         case 1:
531                 ptr8 = (__s8 *) info->ptr;
532                 *ptr8 = num;
533                 break;
534         }
535         return 0;
536 }
537
538 static errcode_t parse_string(struct field_set_info *info,
539                               char *field EXT2FS_ATTR((unused)), char *arg)
540 {
541         char    *cp = (char *) info->ptr;
542
543         if (strlen(arg) >= info->size) {
544                 fprintf(stderr, "Error maximum size for %s is %d.\n",
545                         info->name, info->size);
546                 return EINVAL;
547         }
548         strcpy(cp, arg);
549         return 0;
550 }
551
552 static errcode_t parse_time(struct field_set_info *info,
553                             char *field EXT2FS_ATTR((unused)), char *arg)
554 {
555         time_t          t;
556         __u32           *ptr32;
557
558         ptr32 = (__u32 *) info->ptr;
559
560         t = string_to_time(arg);
561
562         if (t == ((time_t) -1)) {
563                 fprintf(stderr, "Couldn't parse '%s' for field %s.\n",
564                         arg, info->name);
565                 return EINVAL;
566         }
567         *ptr32 = t;
568         return 0;
569 }
570
571 static errcode_t parse_uuid(struct field_set_info *info,
572                             char *field EXT2FS_ATTR((unused)), char *arg)
573 {
574         unsigned char * p = (unsigned char *) info->ptr;
575
576         if ((strcasecmp(arg, "null") == 0) ||
577             (strcasecmp(arg, "clear") == 0)) {
578                 uuid_clear(p);
579         } else if (strcasecmp(arg, "time") == 0) {
580                 uuid_generate_time(p);
581         } else if (strcasecmp(arg, "random") == 0) {
582                 uuid_generate(p);
583         } else if (uuid_parse(arg, p)) {
584                 fprintf(stderr, "Invalid UUID format: %s\n", arg);
585                 return EINVAL;
586         }
587         return 0;
588 }
589
590 static errcode_t parse_hashalg(struct field_set_info *info,
591                                char *field EXT2FS_ATTR((unused)), char *arg)
592 {
593         int     hashv;
594         unsigned char   *p = (unsigned char *) info->ptr;
595
596         hashv = e2p_string2hash(arg);
597         if (hashv < 0) {
598                 fprintf(stderr, "Invalid hash algorithm: %s\n", arg);
599                 return EINVAL;
600         }
601         *p = hashv;
602         return 0;
603 }
604
605 static errcode_t parse_bmap(struct field_set_info *info,
606                             char *field EXT2FS_ATTR((unused)), char *arg)
607 {
608         blk64_t         blk;
609         errcode_t       retval;
610         char            *tmp;
611
612         blk = strtoull(arg, &tmp, 0);
613         if (*tmp) {
614                 fprintf(stderr, "Couldn't parse '%s' for field %s.\n",
615                         arg, info->name);
616                 return EINVAL;
617         }
618
619         retval = ext2fs_bmap2(current_fs, set_ino,
620                               (struct ext2_inode *) &set_inode,
621                               NULL, BMAP_ALLOC | BMAP_SET, array_idx, NULL,
622                               &blk);
623         if (retval) {
624                 com_err("set_inode", retval, "while setting block map");
625         }
626         return retval;
627 }
628
629 static errcode_t parse_gd_csum(struct field_set_info *info, char *field,
630                                char *arg)
631 {
632
633         if (strcmp(arg, "calc") == 0) {
634                 ext2fs_group_desc_csum_set(current_fs, set_bg);
635                 memcpy(&set_gd, ext2fs_group_desc(current_fs,
636                                         current_fs->group_desc,
637                                         set_bg),
638                         sizeof(set_gd));
639                 printf("Checksum set to 0x%04x\n",
640                        ext2fs_bg_checksum(current_fs, set_bg));
641                 return 0;
642         }
643
644         return parse_uint(info, field, arg);
645 }
646
647 static void print_possible_fields(struct field_set_info *fields)
648 {
649         struct field_set_info *ss;
650         const char      *type, *cmd;
651         FILE *f;
652         char name[40], idx[40];
653
654         if (fields == super_fields) {
655                 type = "Superblock";
656                 cmd = "set_super_value";
657         } else if (fields == inode_fields) {
658                 type = "Inode";
659                 cmd = "set_inode";
660         } else if (fields == mmp_fields) {
661                 type = "MMP";
662                 cmd = "set_mmp_value";
663         } else {
664                 type = "Block group descriptor";
665                 cmd = "set_block_group";
666         }
667         f = open_pager();
668
669         fprintf(f, "%s fields supported by the %s command:\n", type, cmd);
670
671         for (ss = fields ; ss->name ; ss++) {
672                 type = "unknown";
673                 if (ss->func == parse_string)
674                         type = "string";
675                 else if (ss->func == parse_int)
676                         type = "integer";
677                 else if (ss->func == parse_uint)
678                         type = "unsigned integer";
679                 else if (ss->func == parse_uuid)
680                         type = "UUID";
681                 else if (ss->func == parse_hashalg)
682                         type = "hash algorithm";
683                 else if (ss->func == parse_time)
684                         type = "date/time";
685                 else if (ss->func == parse_bmap)
686                         type = "set physical->logical block map";
687                 else if (ss->func == parse_gd_csum)
688                         type = "unsigned integer OR \"calc\"";
689                 strcpy(name, ss->name);
690                 if (ss->flags & FLAG_ARRAY) {
691                         if (ss->max_idx > 0)
692                                 sprintf(idx, "[%d]", ss->max_idx);
693                         else
694                                 strcpy(idx, "[]");
695                         strcat(name, idx);
696                 }
697                 if (ss->ptr2)
698                         strcat(name, "[_hi|_lo]");
699                 fprintf(f, "\t%-25s\t%s\n", name, type);
700         }
701         close_pager(f);
702 }
703
704
705 void do_set_super(int argc, char *argv[])
706 {
707         const char *usage = "<field> <value>\n"
708                 "\t\"set_super_value -l\" will list the names of "
709                 "superblock fields\n\twhich can be set.";
710         static struct field_set_info *ss;
711
712         if ((argc == 2) && !strcmp(argv[1], "-l")) {
713                 print_possible_fields(super_fields);
714                 return;
715         }
716
717         if (common_args_process(argc, argv, 3, 3, "set_super_value",
718                                 usage, CHECK_FS_RW))
719                 return;
720
721         if ((ss = find_field(super_fields, argv[1])) == 0) {
722                 com_err(argv[0], 0, "invalid field specifier: %s", argv[1]);
723                 return;
724         }
725         set_sb = *current_fs->super;
726         if (ss->func(ss, argv[1], argv[2]) == 0) {
727                 *current_fs->super = set_sb;
728                 ext2fs_mark_super_dirty(current_fs);
729         }
730 }
731
732 void do_set_inode(int argc, char *argv[])
733 {
734         const char *usage = "<inode> <field> <value>\n"
735                 "\t\"set_inode_field -l\" will list the names of "
736                 "the fields in an ext2 inode\n\twhich can be set.";
737         static struct field_set_info *ss;
738
739         if ((argc == 2) && !strcmp(argv[1], "-l")) {
740                 print_possible_fields(inode_fields);
741                 return;
742         }
743
744         if (common_args_process(argc, argv, 4, 4, "set_inode",
745                                 usage, CHECK_FS_RW))
746                 return;
747
748         if ((ss = find_field(inode_fields, argv[2])) == 0) {
749                 com_err(argv[0], 0, "invalid field specifier: %s", argv[2]);
750                 return;
751         }
752
753         set_ino = string_to_inode(argv[1]);
754         if (!set_ino)
755                 return;
756
757         if (debugfs_read_inode_full(set_ino,
758                         (struct ext2_inode *) &set_inode, argv[1],
759                                     sizeof(set_inode)))
760                 return;
761
762         if (ss->func(ss, argv[2], argv[3]) == 0) {
763                 if (debugfs_write_inode_full(set_ino, 
764                              (struct ext2_inode *) &set_inode,
765                              argv[1], sizeof(set_inode)))
766                         return;
767         }
768 }
769
770 void do_set_block_group_descriptor(int argc, char *argv[])
771 {
772         const char *usage = "<bg number> <field> <value>\n"
773                 "\t\"set_block_group_descriptor -l\" will list the names of "
774                 "the fields in a block group descriptor\n\twhich can be set.";
775         struct field_set_info   *table;
776         struct field_set_info   *ss;
777         char                    *end;
778         void                    *edit, *target;
779         int                     size;
780
781         /*
782          * Determine whether we are editing an ext2 or ext4 block group
783          * descriptor.  Descriptors larger than ext4_group_desc cannot
784          * have their fields edited yet, because they do not have any
785          * names assigned.  When that happens, this function needs to
786          * be updated for the new descriptor struct and fields.
787          */
788         if (current_fs &&
789             EXT2_DESC_SIZE(current_fs->super) >= EXT2_MIN_DESC_SIZE_64BIT) {
790                 table = ext4_bg_fields;
791                 edit = &set_gd4;
792                 size = sizeof(set_gd4);
793         } else {
794                 table = ext2_bg_fields;
795                 edit = &set_gd;
796                 size = sizeof(set_gd);
797         }
798
799         if ((argc == 2) && !strcmp(argv[1], "-l")) {
800                 print_possible_fields(table);
801                 return;
802         }
803
804         if (common_args_process(argc, argv, 4, 4, "set_block_group_descriptor",
805                                 usage, CHECK_FS_RW))
806                 return;
807
808         set_bg = strtoul(argv[1], &end, 0);
809         if (*end) {
810                 com_err(argv[0], 0, "invalid block group number: %s", argv[1]);
811                 return;
812         }
813
814         if (set_bg >= current_fs->group_desc_count) {
815                 com_err(argv[0], 0, "block group number too big: %d", set_bg);
816                 return;
817         }
818
819         if ((ss = find_field(table, argv[2])) == 0) {
820                 com_err(argv[0], 0, "invalid field specifier: %s", argv[2]);
821                 return;
822         }
823
824         target = ext2fs_group_desc(current_fs, current_fs->group_desc, set_bg);
825         memcpy(edit, target, size);
826         if (ss->func(ss, argv[2], argv[3]) == 0) {
827                 memcpy(target, edit, size);
828                 ext2fs_mark_super_dirty(current_fs);
829         }
830 }
831
832 static errcode_t parse_mmp_clear(struct field_set_info *info,
833                                  char *field EXT2FS_ATTR((unused)),
834                                  char *arg EXT2FS_ATTR((unused)))
835 {
836         errcode_t retval;
837
838         retval = ext2fs_mmp_clear(current_fs);
839         if (retval != 0)
840                 com_err("set_mmp_value", retval, "while clearing MMP block\n");
841         else
842                 memcpy(info->ptr, current_fs->mmp_buf, info->size);
843
844         return 1; /* we don't need the MMP block written again */
845 }
846
847 void do_set_mmp_value(int argc, char *argv[])
848 {
849 #ifdef CONFIG_MMP
850         const char *usage = "<field> <value>\n"
851                 "\t\"set_mmp_value -l\" will list the names of "
852                 "MMP fields\n\twhich can be set.";
853         static struct field_set_info *smmp;
854         struct mmp_struct *mmp_s;
855         errcode_t retval;
856
857         if (argc == 2 && strcmp(argv[1], "-l") == 0) {
858                 print_possible_fields(mmp_fields);
859                 return;
860         }
861
862         if (check_fs_open(argv[0]))
863                 return;
864
865         if (current_fs->super->s_mmp_block == 0) {
866                 com_err(argv[0], 0, "no MMP block allocated\n");
867                 return;
868         }
869
870         if (common_args_process(argc, argv, 2, 3, "set_mmp_value",
871                                 usage, CHECK_FS_RW))
872                 return;
873
874         mmp_s = current_fs->mmp_buf;
875         if (mmp_s == NULL) {
876                 retval = ext2fs_get_mem(current_fs->blocksize, &mmp_s);
877                 if (retval) {
878                         com_err(argv[0], retval, "allocating MMP buffer\n");
879                         return;
880                 }
881                 retval = ext2fs_mmp_read(current_fs,
882                                          current_fs->super->s_mmp_block, mmp_s);
883                 if (retval) {
884                         com_err(argv[0], retval, "reading MMP block %llu.\n",
885                                 (long long)current_fs->super->s_mmp_block);
886                         ext2fs_free_mem(&mmp_s);
887                         return;
888                 }
889                 current_fs->mmp_buf = mmp_s;
890         }
891
892         smmp = find_field(mmp_fields, argv[1]);
893         if (smmp == 0) {
894                 com_err(argv[0], 0, "invalid field specifier: %s", argv[1]);
895                 return;
896         }
897
898         set_mmp = *mmp_s;
899         if (smmp->func(smmp, argv[1], argv[2]) == 0) {
900                 ext2fs_mmp_write(current_fs, current_fs->super->s_mmp_block,
901                                  &set_mmp);
902                 *mmp_s = set_mmp;
903         }
904 #else
905         fprintf(stdout, "MMP is unsupported, please recompile with "
906                         "--enable-mmp\n");
907 #endif
908 }
909