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