]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blob - debugfs/set_fields.c
Merge branch 'maint' into next
[thirdparty/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 { 0, 0, 0, 0 }
158 };
159
160 static struct field_set_info inode_fields[] = {
161 { "inodes_count", &set_sb.s_inodes_count, NULL, 4, parse_uint },
162 { "mode", &set_inode.i_mode, NULL, 2, parse_uint },
163 { "uid", &set_inode.i_uid, &set_inode.osd2.linux2.l_i_uid_high,
164 2, parse_uint },
165 { "size", &set_inode.i_size, &set_inode.i_size_high, 4, parse_uint },
166 { "atime", &set_inode.i_atime, NULL, 4, parse_time },
167 { "ctime", &set_inode.i_ctime, NULL, 4, parse_time },
168 { "mtime", &set_inode.i_mtime, NULL, 4, parse_time },
169 { "dtime", &set_inode.i_dtime, NULL, 4, parse_time },
170 { "gid", &set_inode.i_gid, &set_inode.osd2.linux2.l_i_gid_high,
171 2, parse_uint },
172 { "links_count", &set_inode.i_links_count, NULL, 2, parse_uint },
173 /* Special case: i_blocks is 4 bytes, i_blocks_high is 2 bytes */
174 { "blocks", &set_inode.i_blocks, &set_inode.osd2.linux2.l_i_blocks_hi,
175 6, parse_uint },
176 { "flags", &set_inode.i_flags, NULL, 4, parse_uint },
177 { "version", &set_inode.osd1.linux1.l_i_version,
178 &set_inode.i_version_hi, 4, parse_uint },
179 { "translator", &set_inode.osd1.hurd1.h_i_translator, NULL, 4, parse_uint },
180 { "block", &set_inode.i_block[0], NULL, 4, parse_uint, FLAG_ARRAY,
181 EXT2_NDIR_BLOCKS },
182 { "block[IND]", &set_inode.i_block[EXT2_IND_BLOCK], NULL, 4, parse_uint },
183 { "block[DIND]", &set_inode.i_block[EXT2_DIND_BLOCK], NULL, 4, parse_uint },
184 { "block[TIND]", &set_inode.i_block[EXT2_TIND_BLOCK], NULL, 4, parse_uint },
185 { "generation", &set_inode.i_generation, NULL, 4, parse_uint },
186 /* Special case: i_file_acl_high is 2 bytes */
187 { "file_acl", &set_inode.i_file_acl,
188 &set_inode.osd2.linux2.l_i_file_acl_high, 6, parse_uint },
189 { "dir_acl", &set_inode.i_dir_acl, NULL, 4, parse_uint },
190 { "faddr", &set_inode.i_faddr, NULL, 4, parse_uint },
191 { "frag", &set_inode.osd2.hurd2.h_i_frag, NULL, 1, parse_uint },
192 { "fsize", &set_inode.osd2.hurd2.h_i_fsize, NULL, 1, parse_uint },
193 { "checksum", &set_inode.osd2.linux2.l_i_checksum_lo,
194 &set_inode.i_checksum_hi, 2, parse_uint },
195 { "author", &set_inode.osd2.hurd2.h_i_author, NULL,
196 4, parse_uint },
197 { "extra_isize", &set_inode.i_extra_isize, NULL,
198 2, parse_uint },
199 { "ctime_extra", &set_inode.i_ctime_extra, NULL,
200 4, parse_uint },
201 { "mtime_extra", &set_inode.i_mtime_extra, NULL,
202 4, parse_uint },
203 { "atime_extra", &set_inode.i_atime_extra, NULL,
204 4, parse_uint },
205 { "crtime", &set_inode.i_crtime, NULL, 4, parse_uint },
206 { "crtime_extra", &set_inode.i_crtime_extra, NULL,
207 4, parse_uint },
208 { "bmap", NULL, NULL, 4, parse_bmap, FLAG_ARRAY },
209 { 0, 0, 0, 0 }
210 };
211
212 static struct field_set_info ext2_bg_fields[] = {
213 { "block_bitmap", &set_gd.bg_block_bitmap, NULL, 4, parse_uint },
214 { "inode_bitmap", &set_gd.bg_inode_bitmap, NULL, 4, parse_uint },
215 { "inode_table", &set_gd.bg_inode_table, NULL, 4, parse_uint },
216 { "free_blocks_count", &set_gd.bg_free_blocks_count, NULL, 2, parse_uint },
217 { "free_inodes_count", &set_gd.bg_free_inodes_count, NULL, 2, parse_uint },
218 { "used_dirs_count", &set_gd.bg_used_dirs_count, NULL, 2, parse_uint },
219 { "flags", &set_gd.bg_flags, NULL, 2, parse_uint },
220 { "itable_unused", &set_gd.bg_itable_unused, NULL, 2, parse_uint },
221 { "checksum", &set_gd.bg_checksum, NULL, 2, parse_gd_csum },
222 { 0, 0, 0, 0 }
223 };
224
225 static struct field_set_info ext4_bg_fields[] = {
226 { "block_bitmap", &set_gd4.bg_block_bitmap,
227 &set_gd4.bg_block_bitmap_hi, 4, parse_uint },
228 { "inode_bitmap", &set_gd4.bg_inode_bitmap,
229 &set_gd4.bg_inode_bitmap_hi, 4, parse_uint },
230 { "inode_table", &set_gd4.bg_inode_table,
231 &set_gd4.bg_inode_table_hi, 4, parse_uint },
232 { "free_blocks_count", &set_gd4.bg_free_blocks_count,
233 &set_gd4.bg_free_blocks_count_hi, 2, parse_uint },
234 { "free_inodes_count", &set_gd4.bg_free_inodes_count,
235 &set_gd4.bg_free_inodes_count_hi, 2, parse_uint },
236 { "used_dirs_count", &set_gd4.bg_used_dirs_count,
237 &set_gd4.bg_used_dirs_count_hi, 2, parse_uint },
238 { "flags", &set_gd4.bg_flags, NULL, 2, parse_uint },
239 { "exclude_bitmap", &set_gd4.bg_exclude_bitmap_lo,
240 &set_gd4.bg_exclude_bitmap_hi, 4, parse_uint },
241 { "block_bitmap_csum", &set_gd4.bg_block_bitmap_csum_lo,
242 &set_gd4.bg_block_bitmap_csum_hi, 2, parse_uint },
243 { "inode_bitmap_csum", &set_gd4.bg_inode_bitmap_csum_lo,
244 &set_gd4.bg_inode_bitmap_csum_hi, 2, parse_uint },
245 { "itable_unused", &set_gd4.bg_itable_unused,
246 &set_gd4.bg_itable_unused_hi, 2, parse_uint },
247 { "checksum", &set_gd4.bg_checksum, NULL, 2, parse_gd_csum },
248 { 0, 0, 0, 0 }
249 };
250
251 static struct field_set_info mmp_fields[] = {
252 { "clear", &set_mmp.mmp_magic, NULL, sizeof(set_mmp), parse_mmp_clear },
253 { "magic", &set_mmp.mmp_magic, NULL, 4, parse_uint },
254 { "seq", &set_mmp.mmp_seq, NULL, 4, parse_uint },
255 { "time", &set_mmp.mmp_time, NULL, 8, parse_uint },
256 { "nodename", &set_mmp.mmp_nodename, NULL, sizeof(set_mmp.mmp_nodename),
257 parse_string },
258 { "bdevname", &set_mmp.mmp_bdevname, NULL, sizeof(set_mmp.mmp_bdevname),
259 parse_string },
260 { "check_interval", &set_mmp.mmp_check_interval, NULL, 2, parse_uint },
261 { "checksum", &set_mmp.mmp_checksum, NULL, 4, parse_uint },
262 };
263
264 static int check_suffix(const char *field)
265 {
266 int len = strlen(field);
267
268 if (len <= 3)
269 return 0;
270 field += len-3;
271 if (!strcmp(field, "_lo"))
272 return 1;
273 if (!strcmp(field, "_hi"))
274 return 2;
275 return 0;
276 }
277
278 static struct field_set_info *find_field(struct field_set_info *fields,
279 char *field)
280 {
281 struct field_set_info *ss;
282 const char *prefix;
283 char *arg, *delim, *idx, *tmp;
284 int suffix, prefix_len;
285
286 if (fields == super_fields)
287 prefix = "s_";
288 else if (fields == inode_fields)
289 prefix = "i_";
290 else
291 prefix = "bg_";
292 prefix_len = strlen(prefix);
293 if (strncmp(field, prefix, prefix_len) == 0)
294 field += prefix_len;
295
296 arg = malloc(strlen(field)+1);
297 if (!arg)
298 return NULL;
299 strcpy(arg, field);
300
301 idx = strchr(arg, '[');
302 if (idx) {
303 *idx++ = 0;
304 delim = idx + strlen(idx) - 1;
305 if (!*idx || *delim != ']')
306 idx = 0;
307 else
308 *delim = 0;
309 }
310 /*
311 * Can we parse the number?
312 */
313 if (idx) {
314 array_idx = strtol(idx, &tmp, 0);
315 if (*tmp)
316 idx = 0;
317 }
318
319 /*
320 * If there is a valid _hi or a _lo suffix, strip it off
321 */
322 suffix = check_suffix(arg);
323 if (suffix > 0)
324 arg[strlen(arg)-3] = 0;
325
326 for (ss = fields ; ss->name ; ss++) {
327 if (suffix && ss->ptr2 == 0)
328 continue;
329 if (ss->flags & FLAG_ARRAY) {
330 if (!idx || (strcmp(ss->name, arg) != 0))
331 continue;
332 if (ss->max_idx > 0 && array_idx >= ss->max_idx)
333 continue;
334 } else {
335 if (strcmp(ss->name, arg) != 0)
336 continue;
337 }
338 free(arg);
339 return ss;
340 }
341 free(arg);
342 return NULL;
343 }
344
345 /*
346 * Note: info->size == 6 is special; this means a base size 4 bytes,
347 * and secondiory (high) size of 2 bytes. This is needed for the
348 * special case of i_blocks_high and i_file_acl_high.
349 */
350 static errcode_t parse_uint(struct field_set_info *info, char *field,
351 char *arg)
352 {
353 unsigned long long n, num, mask, limit;
354 int suffix = check_suffix(field);
355 char *tmp;
356 void *field1 = info->ptr, *field2 = info->ptr2;
357 int size = (info->size == 6) ? 4 : info->size;
358 union {
359 __u64 *ptr64;
360 __u32 *ptr32;
361 __u16 *ptr16;
362 __u8 *ptr8;
363 } u;
364
365 if (suffix == 1)
366 field2 = 0;
367 if (suffix == 2) {
368 field1 = field2;
369 field2 = 0;
370 }
371
372 u.ptr8 = (__u8 *) field1;
373 if (info->flags & FLAG_ARRAY)
374 u.ptr8 += array_idx * info->size;
375
376 errno = 0;
377 num = STRTOULL(arg, &tmp, 0);
378 if (*tmp || errno) {
379 fprintf(stderr, "Couldn't parse '%s' for field %s.\n",
380 arg, info->name);
381 return EINVAL;
382 }
383 mask = ~0ULL >> ((8 - size) * 8);
384 limit = ~0ULL >> ((8 - info->size) * 8);
385 if (field2 && info->size != 6)
386 limit = ~0ULL >> ((8 - info->size*2) * 8);
387
388 if (num > limit) {
389 fprintf(stderr, "Value '%s' exceeds field %s maximum %llu.\n",
390 arg, info->name, limit);
391 return EINVAL;
392 }
393 n = num & mask;
394 switch (size) {
395 case 8:
396 /* Should never get here */
397 fprintf(stderr, "64-bit field %s has a second 64-bit field\n"
398 "defined; BUG?!?\n", info->name);
399 *u.ptr64 = 0;
400 break;
401 case 4:
402 *u.ptr32 = n;
403 break;
404 case 2:
405 *u.ptr16 = n;
406 break;
407 case 1:
408 *u.ptr8 = n;
409 break;
410 }
411 if (!field2)
412 return 0;
413 n = num >> (size*8);
414 u.ptr8 = (__u8 *) field2;
415 if (info->size == 6)
416 size = 2;
417 switch (size) {
418 case 8:
419 *u.ptr64 = n;
420 break;
421 case 4:
422 *u.ptr32 = n;
423 break;
424 case 2:
425 *u.ptr16 = n;
426 break;
427 case 1:
428 *u.ptr8 = n;
429 break;
430 }
431 return 0;
432 }
433
434 static errcode_t parse_int(struct field_set_info *info,
435 char *field EXT2FS_ATTR((unused)), char *arg)
436 {
437 long num;
438 char *tmp;
439 __s32 *ptr32;
440 __s16 *ptr16;
441 __s8 *ptr8;
442
443 num = strtol(arg, &tmp, 0);
444 if (*tmp) {
445 fprintf(stderr, "Couldn't parse '%s' for field %s.\n",
446 arg, info->name);
447 return EINVAL;
448 }
449 switch (info->size) {
450 case 4:
451 ptr32 = (__s32 *) info->ptr;
452 *ptr32 = num;
453 break;
454 case 2:
455 ptr16 = (__s16 *) info->ptr;
456 *ptr16 = num;
457 break;
458 case 1:
459 ptr8 = (__s8 *) info->ptr;
460 *ptr8 = num;
461 break;
462 }
463 return 0;
464 }
465
466 static errcode_t parse_string(struct field_set_info *info,
467 char *field EXT2FS_ATTR((unused)), char *arg)
468 {
469 char *cp = (char *) info->ptr;
470
471 if (strlen(arg) >= info->size) {
472 fprintf(stderr, "Error maximum size for %s is %d.\n",
473 info->name, info->size);
474 return EINVAL;
475 }
476 strcpy(cp, arg);
477 return 0;
478 }
479
480 static errcode_t parse_time(struct field_set_info *info,
481 char *field EXT2FS_ATTR((unused)), char *arg)
482 {
483 time_t t;
484 __u32 *ptr32;
485
486 ptr32 = (__u32 *) info->ptr;
487
488 t = string_to_time(arg);
489
490 if (t == ((time_t) -1)) {
491 fprintf(stderr, "Couldn't parse '%s' for field %s.\n",
492 arg, info->name);
493 return EINVAL;
494 }
495 *ptr32 = t;
496 return 0;
497 }
498
499 static errcode_t parse_uuid(struct field_set_info *info,
500 char *field EXT2FS_ATTR((unused)), char *arg)
501 {
502 unsigned char * p = (unsigned char *) info->ptr;
503
504 if ((strcasecmp(arg, "null") == 0) ||
505 (strcasecmp(arg, "clear") == 0)) {
506 uuid_clear(p);
507 } else if (strcasecmp(arg, "time") == 0) {
508 uuid_generate_time(p);
509 } else if (strcasecmp(arg, "random") == 0) {
510 uuid_generate(p);
511 } else if (uuid_parse(arg, p)) {
512 fprintf(stderr, "Invalid UUID format: %s\n", arg);
513 return EINVAL;
514 }
515 return 0;
516 }
517
518 static errcode_t parse_hashalg(struct field_set_info *info,
519 char *field EXT2FS_ATTR((unused)), char *arg)
520 {
521 int hashv;
522 unsigned char *p = (unsigned char *) info->ptr;
523
524 hashv = e2p_string2hash(arg);
525 if (hashv < 0) {
526 fprintf(stderr, "Invalid hash algorithm: %s\n", arg);
527 return EINVAL;
528 }
529 *p = hashv;
530 return 0;
531 }
532
533 static errcode_t parse_bmap(struct field_set_info *info,
534 char *field EXT2FS_ATTR((unused)), char *arg)
535 {
536 blk64_t blk;
537 errcode_t retval;
538 char *tmp;
539
540 blk = strtoull(arg, &tmp, 0);
541 if (*tmp) {
542 fprintf(stderr, "Couldn't parse '%s' for field %s.\n",
543 arg, info->name);
544 return EINVAL;
545 }
546
547 retval = ext2fs_bmap2(current_fs, set_ino,
548 (struct ext2_inode *) &set_inode,
549 NULL, BMAP_SET, array_idx, NULL, &blk);
550 if (retval) {
551 com_err("set_inode", retval, "while setting block map");
552 }
553 return retval;
554 }
555
556 static errcode_t parse_gd_csum(struct field_set_info *info, char *field,
557 char *arg)
558 {
559
560 if (strcmp(arg, "calc") == 0) {
561 ext2fs_group_desc_csum_set(current_fs, set_bg);
562 memcpy(&set_gd, ext2fs_group_desc(current_fs,
563 current_fs->group_desc,
564 set_bg),
565 sizeof(set_gd));
566 printf("Checksum set to 0x%04x\n",
567 ext2fs_bg_checksum(current_fs, set_bg));
568 return 0;
569 }
570
571 return parse_uint(info, field, arg);
572 }
573
574 static void print_possible_fields(struct field_set_info *fields)
575 {
576 struct field_set_info *ss;
577 const char *type, *cmd;
578 FILE *f;
579 char name[40], idx[40];
580
581 if (fields == super_fields) {
582 type = "Superblock";
583 cmd = "set_super_value";
584 } else if (fields == inode_fields) {
585 type = "Inode";
586 cmd = "set_inode";
587 } else if (fields == mmp_fields) {
588 type = "MMP";
589 cmd = "set_mmp_value";
590 } else {
591 type = "Block group descriptor";
592 cmd = "set_block_group";
593 }
594 f = open_pager();
595
596 fprintf(f, "%s fields supported by the %s command:\n", type, cmd);
597
598 for (ss = fields ; ss->name ; ss++) {
599 type = "unknown";
600 if (ss->func == parse_string)
601 type = "string";
602 else if (ss->func == parse_int)
603 type = "integer";
604 else if (ss->func == parse_uint)
605 type = "unsigned integer";
606 else if (ss->func == parse_uuid)
607 type = "UUID";
608 else if (ss->func == parse_hashalg)
609 type = "hash algorithm";
610 else if (ss->func == parse_time)
611 type = "date/time";
612 else if (ss->func == parse_bmap)
613 type = "set physical->logical block map";
614 else if (ss->func == parse_gd_csum)
615 type = "unsigned integer OR \"calc\"";
616 strcpy(name, ss->name);
617 if (ss->flags & FLAG_ARRAY) {
618 if (ss->max_idx > 0)
619 sprintf(idx, "[%d]", ss->max_idx);
620 else
621 strcpy(idx, "[]");
622 strcat(name, idx);
623 }
624 if (ss->ptr2)
625 strcat(name, "[_hi|_lo]");
626 fprintf(f, "\t%-25s\t%s\n", name, type);
627 }
628 close_pager(f);
629 }
630
631
632 void do_set_super(int argc, char *argv[])
633 {
634 const char *usage = "<field> <value>\n"
635 "\t\"set_super_value -l\" will list the names of "
636 "superblock fields\n\twhich can be set.";
637 static struct field_set_info *ss;
638
639 if ((argc == 2) && !strcmp(argv[1], "-l")) {
640 print_possible_fields(super_fields);
641 return;
642 }
643
644 if (common_args_process(argc, argv, 3, 3, "set_super_value",
645 usage, CHECK_FS_RW))
646 return;
647
648 if ((ss = find_field(super_fields, argv[1])) == 0) {
649 com_err(argv[0], 0, "invalid field specifier: %s", argv[1]);
650 return;
651 }
652 set_sb = *current_fs->super;
653 if (ss->func(ss, argv[1], argv[2]) == 0) {
654 *current_fs->super = set_sb;
655 ext2fs_mark_super_dirty(current_fs);
656 }
657 }
658
659 void do_set_inode(int argc, char *argv[])
660 {
661 const char *usage = "<inode> <field> <value>\n"
662 "\t\"set_inode_field -l\" will list the names of "
663 "the fields in an ext2 inode\n\twhich can be set.";
664 static struct field_set_info *ss;
665
666 if ((argc == 2) && !strcmp(argv[1], "-l")) {
667 print_possible_fields(inode_fields);
668 return;
669 }
670
671 if (common_args_process(argc, argv, 4, 4, "set_inode",
672 usage, CHECK_FS_RW))
673 return;
674
675 if ((ss = find_field(inode_fields, argv[2])) == 0) {
676 com_err(argv[0], 0, "invalid field specifier: %s", argv[2]);
677 return;
678 }
679
680 set_ino = string_to_inode(argv[1]);
681 if (!set_ino)
682 return;
683
684 if (debugfs_read_inode_full(set_ino,
685 (struct ext2_inode *) &set_inode, argv[1],
686 sizeof(set_inode)))
687 return;
688
689 if (ss->func(ss, argv[2], argv[3]) == 0) {
690 if (debugfs_write_inode_full(set_ino,
691 (struct ext2_inode *) &set_inode,
692 argv[1], sizeof(set_inode)))
693 return;
694 }
695 }
696
697 void do_set_block_group_descriptor(int argc, char *argv[])
698 {
699 const char *usage = "<bg number> <field> <value>\n"
700 "\t\"set_block_group_descriptor -l\" will list the names of "
701 "the fields in a block group descriptor\n\twhich can be set.";
702 struct field_set_info *table;
703 struct field_set_info *ss;
704 char *end;
705 void *edit, *target;
706 int size;
707
708 /*
709 * Determine whether we are editing an ext2 or ext4 block group
710 * descriptor. Descriptors larger than ext4_group_desc cannot
711 * have their fields edited yet, because they do not have any
712 * names assigned. When that happens, this function needs to
713 * be updated for the new descriptor struct and fields.
714 */
715 if (current_fs &&
716 EXT2_DESC_SIZE(current_fs->super) >= EXT2_MIN_DESC_SIZE_64BIT) {
717 table = ext4_bg_fields;
718 edit = &set_gd4;
719 size = sizeof(set_gd4);
720 } else {
721 table = ext2_bg_fields;
722 edit = &set_gd;
723 size = sizeof(set_gd);
724 }
725
726 if ((argc == 2) && !strcmp(argv[1], "-l")) {
727 print_possible_fields(table);
728 return;
729 }
730
731 if (common_args_process(argc, argv, 4, 4, "set_block_group_descriptor",
732 usage, CHECK_FS_RW))
733 return;
734
735 set_bg = strtoul(argv[1], &end, 0);
736 if (*end) {
737 com_err(argv[0], 0, "invalid block group number: %s", argv[1]);
738 return;
739 }
740
741 if (set_bg >= current_fs->group_desc_count) {
742 com_err(argv[0], 0, "block group number too big: %d", set_bg);
743 return;
744 }
745
746 if ((ss = find_field(table, argv[2])) == 0) {
747 com_err(argv[0], 0, "invalid field specifier: %s", argv[2]);
748 return;
749 }
750
751 target = ext2fs_group_desc(current_fs, current_fs->group_desc, set_bg);
752 memcpy(edit, target, size);
753 if (ss->func(ss, argv[2], argv[3]) == 0) {
754 memcpy(target, edit, size);
755 ext2fs_mark_super_dirty(current_fs);
756 }
757 }
758
759 static errcode_t parse_mmp_clear(struct field_set_info *info,
760 char *field EXT2FS_ATTR((unused)),
761 char *arg EXT2FS_ATTR((unused)))
762 {
763 errcode_t retval;
764
765 retval = ext2fs_mmp_clear(current_fs);
766 if (retval != 0)
767 com_err("set_mmp_value", retval, "while clearing MMP block\n");
768 else
769 memcpy(info->ptr, current_fs->mmp_buf, info->size);
770
771 return 1; /* we don't need the MMP block written again */
772 }
773
774 void do_set_mmp_value(int argc, char *argv[])
775 {
776 #ifdef CONFIG_MMP
777 const char *usage = "<field> <value>\n"
778 "\t\"set_mmp_value -l\" will list the names of "
779 "MMP fields\n\twhich can be set.";
780 static struct field_set_info *smmp;
781 struct mmp_struct *mmp_s;
782 errcode_t retval;
783
784 if (argc == 2 && strcmp(argv[1], "-l") == 0) {
785 print_possible_fields(mmp_fields);
786 return;
787 }
788
789 if (check_fs_open(argv[0]))
790 return;
791
792 if (current_fs->super->s_mmp_block == 0) {
793 com_err(argv[0], 0, "no MMP block allocated\n");
794 return;
795 }
796
797 if (common_args_process(argc, argv, 2, 3, "set_mmp_value",
798 usage, CHECK_FS_RW))
799 return;
800
801 mmp_s = current_fs->mmp_buf;
802 if (mmp_s == NULL) {
803 retval = ext2fs_get_mem(current_fs->blocksize, &mmp_s);
804 if (retval) {
805 com_err(argv[0], retval, "allocating MMP buffer\n");
806 return;
807 }
808 retval = ext2fs_mmp_read(current_fs,
809 current_fs->super->s_mmp_block, mmp_s);
810 if (retval) {
811 com_err(argv[0], retval, "reading MMP block %llu.\n",
812 (long long)current_fs->super->s_mmp_block);
813 ext2fs_free_mem(&mmp_s);
814 return;
815 }
816 current_fs->mmp_buf = mmp_s;
817 }
818
819 smmp = find_field(mmp_fields, argv[1]);
820 if (smmp == 0) {
821 com_err(argv[0], 0, "invalid field specifier: %s", argv[1]);
822 return;
823 }
824
825 set_mmp = *mmp_s;
826 if (smmp->func(smmp, argv[1], argv[2]) == 0) {
827 ext2fs_mmp_write(current_fs, current_fs->super->s_mmp_block,
828 &set_mmp);
829 *mmp_s = set_mmp;
830 }
831 #else
832 fprintf(stdout, "MMP is unsupported, please recompile with "
833 "--enable-mmp\n");
834 #endif
835 }
836