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