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