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