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