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