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