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