]>
Commit | Line | Data |
---|---|---|
c6bd0c97 | 1 | /* |
7dec050a | 2 | * set_fields.c --- set a superblock value |
efc6f628 | 3 | * |
7dec050a | 4 | * Copyright (C) 2000, 2001, 2002, 2003, 2004 by Theodore Ts'o. |
efc6f628 | 5 | * |
7dec050a TT |
6 | * %Begin-Header% |
7 | * This file may be redistributed under the terms of the GNU Public | |
8 | * License. | |
9 | * %End-Header% | |
c6bd0c97 TT |
10 | */ |
11 | ||
007df860 TT |
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 | |
619a8ea7 | 19 | |
c6bd0c97 TT |
20 | #include <stdio.h> |
21 | #include <unistd.h> | |
22 | #include <stdlib.h> | |
23 | #include <ctype.h> | |
24 | #include <string.h> | |
8192c633 | 25 | #include <strings.h> |
c6bd0c97 TT |
26 | #include <time.h> |
27 | #include <sys/types.h> | |
28 | #include <sys/stat.h> | |
29 | #ifdef HAVE_ERRNO_H | |
30 | #include <errno.h> | |
31 | #endif | |
a98b51e9 TT |
32 | #if HAVE_STRINGS_H |
33 | #include <strings.h> | |
34 | #endif | |
c6bd0c97 TT |
35 | #include <fcntl.h> |
36 | #include <utime.h> | |
37 | ||
38 | #include "debugfs.h" | |
3e699064 TT |
39 | #include "uuid/uuid.h" |
40 | #include "e2p/e2p.h" | |
c6bd0c97 TT |
41 | |
42 | static struct ext2_super_block set_sb; | |
7dec050a | 43 | static struct ext2_inode set_inode; |
c7c1209e | 44 | static struct ext2_group_desc set_gd; |
cf96ecf1 | 45 | static dgrp_t set_bg; |
08fd3f36 TT |
46 | static ext2_ino_t set_ino; |
47 | static int array_idx; | |
48 | ||
49 | #define FLAG_ARRAY 0x0001 | |
c6bd0c97 | 50 | |
7dec050a | 51 | struct field_set_info { |
b044c2e0 | 52 | const char *name; |
c6bd0c97 | 53 | void *ptr; |
54434927 | 54 | unsigned int size; |
7dec050a | 55 | errcode_t (*func)(struct field_set_info *info, char *arg); |
08fd3f36 TT |
56 | int flags; |
57 | int max_idx; | |
c6bd0c97 TT |
58 | }; |
59 | ||
7dec050a TT |
60 | static errcode_t parse_uint(struct field_set_info *info, char *arg); |
61 | static errcode_t parse_int(struct field_set_info *info, char *arg); | |
62 | static errcode_t parse_string(struct field_set_info *info, char *arg); | |
63 | static errcode_t parse_uuid(struct field_set_info *info, char *arg); | |
64 | static errcode_t parse_hashalg(struct field_set_info *info, char *arg); | |
65 | static errcode_t parse_time(struct field_set_info *info, char *arg); | |
08fd3f36 | 66 | static errcode_t parse_bmap(struct field_set_info *info, char *arg); |
cf96ecf1 | 67 | static errcode_t parse_gd_csum(struct field_set_info *info, char *arg); |
c6bd0c97 | 68 | |
7dec050a | 69 | static struct field_set_info super_fields[] = { |
c6bd0c97 TT |
70 | { "inodes_count", &set_sb.s_inodes_count, 4, parse_uint }, |
71 | { "blocks_count", &set_sb.s_blocks_count, 4, parse_uint }, | |
72 | { "r_blocks_count", &set_sb.s_r_blocks_count, 4, parse_uint }, | |
73 | { "free_blocks_count", &set_sb.s_free_blocks_count, 4, parse_uint }, | |
74 | { "free_inodes_count", &set_sb.s_free_inodes_count, 4, parse_uint }, | |
75 | { "first_data_block", &set_sb.s_first_data_block, 4, parse_uint }, | |
76 | { "log_block_size", &set_sb.s_log_block_size, 4, parse_uint }, | |
77 | { "log_frag_size", &set_sb.s_log_frag_size, 4, parse_int }, | |
78 | { "blocks_per_group", &set_sb.s_blocks_per_group, 4, parse_uint }, | |
79 | { "frags_per_group", &set_sb.s_frags_per_group, 4, parse_uint }, | |
80 | { "inodes_per_group", &set_sb.s_inodes_per_group, 4, parse_uint }, | |
7dec050a TT |
81 | { "mtime", &set_sb.s_mtime, 4, parse_time }, |
82 | { "wtime", &set_sb.s_wtime, 4, parse_time }, | |
c6bd0c97 TT |
83 | { "mnt_count", &set_sb.s_mnt_count, 2, parse_uint }, |
84 | { "max_mnt_count", &set_sb.s_max_mnt_count, 2, parse_int }, | |
85 | /* s_magic */ | |
86 | { "state", &set_sb.s_state, 2, parse_uint }, | |
87 | { "errors", &set_sb.s_errors, 2, parse_uint }, | |
88 | { "minor_rev_level", &set_sb.s_minor_rev_level, 2, parse_uint }, | |
7dec050a | 89 | { "lastcheck", &set_sb.s_lastcheck, 4, parse_time }, |
c6bd0c97 TT |
90 | { "checkinterval", &set_sb.s_checkinterval, 4, parse_uint }, |
91 | { "creator_os", &set_sb.s_creator_os, 4, parse_uint }, | |
92 | { "rev_level", &set_sb.s_rev_level, 4, parse_uint }, | |
93 | { "def_resuid", &set_sb.s_def_resuid, 2, parse_uint }, | |
94 | { "def_resgid", &set_sb.s_def_resgid, 2, parse_uint }, | |
95 | { "first_ino", &set_sb.s_first_ino, 4, parse_uint }, | |
426d734f | 96 | { "inode_size", &set_sb.s_inode_size, 2, parse_uint }, |
c6bd0c97 TT |
97 | { "block_group_nr", &set_sb.s_block_group_nr, 2, parse_uint }, |
98 | { "feature_compat", &set_sb.s_feature_compat, 4, parse_uint }, | |
99 | { "feature_incompat", &set_sb.s_feature_incompat, 4, parse_uint }, | |
efc6f628 | 100 | { "feature_ro_compat", &set_sb.s_feature_ro_compat, 4, parse_uint }, |
f61fc0b5 | 101 | { "uuid", &set_sb.s_uuid, 16, parse_uuid }, |
c6bd0c97 TT |
102 | { "volume_name", &set_sb.s_volume_name, 16, parse_string }, |
103 | { "last_mounted", &set_sb.s_last_mounted, 64, parse_string }, | |
364a373e | 104 | { "lastcheck", &set_sb.s_lastcheck, 4, parse_uint }, |
efc6f628 | 105 | { "algorithm_usage_bitmap", &set_sb.s_algorithm_usage_bitmap, |
c6bd0c97 TT |
106 | 4, parse_uint }, |
107 | { "prealloc_blocks", &set_sb.s_prealloc_blocks, 1, parse_uint }, | |
108 | { "prealloc_dir_blocks", &set_sb.s_prealloc_dir_blocks, 1, | |
109 | parse_uint }, | |
a06bd026 TT |
110 | { "reserved_gdt_blocks", &set_sb.s_reserved_gdt_blocks, 2, |
111 | parse_uint }, | |
f61fc0b5 | 112 | { "journal_uuid", &set_sb.s_journal_uuid, 16, parse_uuid }, |
c6bd0c97 TT |
113 | { "journal_inum", &set_sb.s_journal_inum, 4, parse_uint }, |
114 | { "journal_dev", &set_sb.s_journal_dev, 4, parse_uint }, | |
115 | { "last_orphan", &set_sb.s_last_orphan, 4, parse_uint }, | |
f61fc0b5 TT |
116 | { "hash_seed", &set_sb.s_hash_seed, 16, parse_uuid }, |
117 | { "def_hash_version", &set_sb.s_def_hash_version, 1, parse_hashalg }, | |
a06bd026 | 118 | { "jnl_backup_type", &set_sb.s_jnl_backup_type, 1, parse_uint }, |
007df860 | 119 | { "desc_size", &set_sb.s_desc_size, 2, parse_uint }, |
a06bd026 TT |
120 | { "default_mount_opts", &set_sb.s_default_mount_opts, 4, parse_uint }, |
121 | { "first_meta_bg", &set_sb.s_first_meta_bg, 4, parse_uint }, | |
7dec050a | 122 | { "mkfs_time", &set_sb.s_mkfs_time, 4, parse_time }, |
efc6f628 | 123 | { "jnl_blocks", &set_sb.s_jnl_blocks[0], 4, parse_uint, FLAG_ARRAY, |
89c80156 | 124 | 17 }, |
007df860 TT |
125 | { "blocks_count_hi", &set_sb.s_blocks_count_hi, 4, parse_uint }, |
126 | { "r_blocks_count_hi", &set_sb.s_r_blocks_count_hi, 4, parse_uint }, | |
127 | { "min_extra_isize", &set_sb.s_min_extra_isize, 2, parse_uint }, | |
128 | { "want_extra_isize", &set_sb.s_want_extra_isize, 2, parse_uint }, | |
f77704e4 | 129 | { "flags", &set_sb.s_flags, 4, parse_uint }, |
007df860 TT |
130 | { "raid_stride", &set_sb.s_raid_stride, 2, parse_uint }, |
131 | { "min_extra_isize", &set_sb.s_min_extra_isize, 4, parse_uint }, | |
132 | { "mmp_interval", &set_sb.s_mmp_interval, 2, parse_uint }, | |
133 | { "mmp_block", &set_sb.s_mmp_block, 8, parse_uint }, | |
134 | { "raid_stripe_width", &set_sb.s_raid_stripe_width, 4, parse_uint }, | |
494a1daa | 135 | { "log_groups_per_flex", &set_sb.s_log_groups_per_flex, 1, parse_uint }, |
c6bd0c97 TT |
136 | { 0, 0, 0, 0 } |
137 | }; | |
138 | ||
7dec050a TT |
139 | static struct field_set_info inode_fields[] = { |
140 | { "inodes_count", &set_sb.s_inodes_count, 4, parse_uint }, | |
141 | { "mode", &set_inode.i_mode, 2, parse_uint }, | |
142 | { "uid", &set_inode.i_uid, 2, parse_uint }, | |
38d732bb | 143 | { "size", &set_inode.i_size, 4, parse_uint }, |
7dec050a TT |
144 | { "atime", &set_inode.i_atime, 4, parse_time }, |
145 | { "ctime", &set_inode.i_ctime, 4, parse_time }, | |
146 | { "mtime", &set_inode.i_mtime, 4, parse_time }, | |
147 | { "dtime", &set_inode.i_dtime, 4, parse_time }, | |
148 | { "gid", &set_inode.i_gid, 2, parse_uint }, | |
149 | { "links_count", &set_inode.i_links_count, 2, parse_uint }, | |
150 | { "blocks", &set_inode.i_blocks, 4, parse_uint }, | |
151 | { "flags", &set_inode.i_flags, 4, parse_uint }, | |
d362a3fb | 152 | { "version", &set_inode.osd1.linux1.l_i_version, 4, parse_uint }, |
7dec050a | 153 | { "translator", &set_inode.osd1.hurd1.h_i_translator, 4, parse_uint }, |
efc6f628 | 154 | { "block", &set_inode.i_block[0], 4, parse_uint, FLAG_ARRAY, |
08fd3f36 TT |
155 | EXT2_NDIR_BLOCKS }, |
156 | { "block[IND]", &set_inode.i_block[EXT2_IND_BLOCK], 4, parse_uint }, | |
157 | { "block[DIND]", &set_inode.i_block[EXT2_DIND_BLOCK], 4, parse_uint }, | |
158 | { "block[TIND]", &set_inode.i_block[EXT2_TIND_BLOCK], 4, parse_uint }, | |
7dec050a TT |
159 | { "generation", &set_inode.i_generation, 4, parse_uint }, |
160 | { "file_acl", &set_inode.i_file_acl, 4, parse_uint }, | |
161 | { "dir_acl", &set_inode.i_dir_acl, 4, parse_uint }, | |
007df860 | 162 | { "size_high", &set_inode.i_size_high, 4, parse_uint }, |
7dec050a | 163 | { "faddr", &set_inode.i_faddr, 4, parse_uint }, |
5d17119d TT |
164 | { "blocks_hi", &set_inode.osd2.linux2.l_i_blocks_hi, 2, parse_uint }, |
165 | { "frag", &set_inode.osd2.hurd2.h_i_frag, 1, parse_uint }, | |
166 | { "fsize", &set_inode.osd2.hurd2.h_i_fsize, 1, parse_uint }, | |
caf037a4 TT |
167 | { "uid_high", &set_inode.osd2.linux2.l_i_uid_high, 2, parse_uint }, |
168 | { "gid_high", &set_inode.osd2.linux2.l_i_gid_high, 2, parse_uint }, | |
169 | { "author", &set_inode.osd2.hurd2.h_i_author, 4, parse_uint }, | |
08fd3f36 | 170 | { "bmap", NULL, 4, parse_bmap, FLAG_ARRAY }, |
7dec050a TT |
171 | { 0, 0, 0, 0 } |
172 | }; | |
173 | ||
c7c1209e TT |
174 | static struct field_set_info ext2_bg_fields[] = { |
175 | { "block_bitmap", &set_gd.bg_block_bitmap, 4, parse_uint }, | |
176 | { "inode_bitmap", &set_gd.bg_inode_bitmap, 4, parse_uint }, | |
177 | { "inode_table", &set_gd.bg_inode_table, 4, parse_uint }, | |
178 | { "free_blocks_count", &set_gd.bg_free_blocks_count, 2, parse_uint }, | |
179 | { "free_inodes_count", &set_gd.bg_free_inodes_count, 2, parse_uint }, | |
180 | { "used_dirs_count", &set_gd.bg_used_dirs_count, 2, parse_uint }, | |
181 | { "flags", &set_gd.bg_flags, 2, parse_uint }, | |
182 | { "reserved", &set_gd.bg_reserved, 2, parse_uint, FLAG_ARRAY, 2 }, | |
183 | { "itable_unused", &set_gd.bg_itable_unused, 2, parse_uint }, | |
cf96ecf1 | 184 | { "checksum", &set_gd.bg_checksum, 2, parse_gd_csum }, |
c7c1209e TT |
185 | { 0, 0, 0, 0 } |
186 | }; | |
187 | ||
188 | ||
7dec050a TT |
189 | static struct field_set_info *find_field(struct field_set_info *fields, |
190 | char *field) | |
c6bd0c97 | 191 | { |
7dec050a | 192 | struct field_set_info *ss; |
08fd3f36 TT |
193 | const char *prefix; |
194 | char *arg, *delim, *idx, *tmp; | |
c7c1209e | 195 | int prefix_len; |
08fd3f36 TT |
196 | |
197 | if (fields == super_fields) | |
198 | prefix = "s_"; | |
c7c1209e | 199 | else if (fields == inode_fields) |
08fd3f36 | 200 | prefix = "i_"; |
c7c1209e TT |
201 | else |
202 | prefix = "bg_"; | |
203 | prefix_len = strlen(prefix); | |
204 | if (strncmp(field, prefix, prefix_len) == 0) | |
205 | field += prefix_len; | |
08fd3f36 TT |
206 | |
207 | arg = malloc(strlen(field)+1); | |
208 | if (!arg) | |
209 | return NULL; | |
210 | strcpy(arg, field); | |
211 | ||
212 | idx = strchr(arg, '['); | |
213 | if (idx) { | |
214 | *idx++ = 0; | |
215 | delim = idx + strlen(idx) - 1; | |
216 | if (!*idx || *delim != ']') | |
217 | idx = 0; | |
218 | else | |
219 | *delim = 0; | |
220 | } | |
efc6f628 | 221 | /* |
08fd3f36 TT |
222 | * Can we parse the number? |
223 | */ | |
224 | if (idx) { | |
225 | array_idx = strtol(idx, &tmp, 0); | |
226 | if (*tmp) | |
227 | idx = 0; | |
228 | } | |
229 | ||
7dec050a | 230 | for (ss = fields ; ss->name ; ss++) { |
08fd3f36 TT |
231 | if (ss->flags & FLAG_ARRAY) { |
232 | if (!idx || (strcmp(ss->name, arg) != 0)) | |
233 | continue; | |
234 | if (ss->max_idx > 0 && array_idx >= ss->max_idx) | |
235 | continue; | |
236 | } else { | |
237 | if (strcmp(ss->name, field) != 0) | |
238 | continue; | |
239 | } | |
240 | return ss; | |
c6bd0c97 | 241 | } |
08fd3f36 | 242 | |
c6bd0c97 TT |
243 | return NULL; |
244 | } | |
245 | ||
7dec050a | 246 | static errcode_t parse_uint(struct field_set_info *info, char *arg) |
c6bd0c97 | 247 | { |
007df860 | 248 | unsigned long long num, limit; |
c6bd0c97 | 249 | char *tmp; |
08fd3f36 | 250 | union { |
007df860 | 251 | __u64 *ptr64; |
08fd3f36 TT |
252 | __u32 *ptr32; |
253 | __u16 *ptr16; | |
254 | __u8 *ptr8; | |
255 | } u; | |
256 | ||
257 | u.ptr8 = (__u8 *) info->ptr; | |
258 | if (info->flags & FLAG_ARRAY) | |
259 | u.ptr8 += array_idx * info->size; | |
c6bd0c97 | 260 | |
007df860 TT |
261 | errno = 0; |
262 | num = STRTOULL(arg, &tmp, 0); | |
263 | if (*tmp || errno) { | |
c6bd0c97 TT |
264 | fprintf(stderr, "Couldn't parse '%s' for field %s.\n", |
265 | arg, info->name); | |
266 | return EINVAL; | |
267 | } | |
007df860 TT |
268 | limit = ~0ULL >> ((8 - info->size) * 8); |
269 | if (num > limit) { | |
270 | fprintf(stderr, "Value '%s' exceeds field %s maximum %llu.\n", | |
271 | arg, info->name, limit); | |
272 | return EINVAL; | |
273 | } | |
c6bd0c97 | 274 | switch (info->size) { |
007df860 TT |
275 | case 8: |
276 | *u.ptr64 = num; | |
277 | break; | |
c6bd0c97 | 278 | case 4: |
08fd3f36 | 279 | *u.ptr32 = num; |
c6bd0c97 TT |
280 | break; |
281 | case 2: | |
08fd3f36 | 282 | *u.ptr16 = num; |
c6bd0c97 TT |
283 | break; |
284 | case 1: | |
08fd3f36 | 285 | *u.ptr8 = num; |
c6bd0c97 TT |
286 | break; |
287 | } | |
288 | return 0; | |
289 | } | |
290 | ||
7dec050a | 291 | static errcode_t parse_int(struct field_set_info *info, char *arg) |
c6bd0c97 TT |
292 | { |
293 | long num; | |
294 | char *tmp; | |
295 | __s32 *ptr32; | |
296 | __s16 *ptr16; | |
297 | __s8 *ptr8; | |
298 | ||
299 | num = strtol(arg, &tmp, 0); | |
300 | if (*tmp) { | |
301 | fprintf(stderr, "Couldn't parse '%s' for field %s.\n", | |
302 | arg, info->name); | |
303 | return EINVAL; | |
304 | } | |
305 | switch (info->size) { | |
306 | case 4: | |
307 | ptr32 = (__s32 *) info->ptr; | |
308 | *ptr32 = num; | |
309 | break; | |
310 | case 2: | |
311 | ptr16 = (__s16 *) info->ptr; | |
312 | *ptr16 = num; | |
313 | break; | |
314 | case 1: | |
315 | ptr8 = (__s8 *) info->ptr; | |
316 | *ptr8 = num; | |
317 | break; | |
318 | } | |
319 | return 0; | |
320 | } | |
321 | ||
7dec050a | 322 | static errcode_t parse_string(struct field_set_info *info, char *arg) |
c6bd0c97 TT |
323 | { |
324 | char *cp = (char *) info->ptr; | |
325 | ||
326 | if (strlen(arg) >= info->size) { | |
327 | fprintf(stderr, "Error maximum size for %s is %d.\n", | |
328 | info->name, info->size); | |
329 | return EINVAL; | |
330 | } | |
331 | strcpy(cp, arg); | |
332 | return 0; | |
333 | } | |
334 | ||
7dec050a TT |
335 | static errcode_t parse_time(struct field_set_info *info, char *arg) |
336 | { | |
e3e92534 TT |
337 | time_t t; |
338 | __u32 *ptr32; | |
7dec050a TT |
339 | |
340 | ptr32 = (__u32 *) info->ptr; | |
341 | ||
4efae606 TT |
342 | t = string_to_time(arg); |
343 | ||
344 | if (t == ((time_t) -1)) { | |
345 | fprintf(stderr, "Couldn't parse '%s' for field %s.\n", | |
346 | arg, info->name); | |
347 | return EINVAL; | |
7dec050a | 348 | } |
4efae606 | 349 | *ptr32 = t; |
7dec050a TT |
350 | return 0; |
351 | } | |
352 | ||
353 | static errcode_t parse_uuid(struct field_set_info *info, char *arg) | |
f61fc0b5 | 354 | { |
3e699064 | 355 | unsigned char * p = (unsigned char *) info->ptr; |
efc6f628 | 356 | |
f61fc0b5 TT |
357 | if ((strcasecmp(arg, "null") == 0) || |
358 | (strcasecmp(arg, "clear") == 0)) { | |
359 | uuid_clear(p); | |
360 | } else if (strcasecmp(arg, "time") == 0) { | |
361 | uuid_generate_time(p); | |
362 | } else if (strcasecmp(arg, "random") == 0) { | |
363 | uuid_generate(p); | |
364 | } else if (uuid_parse(arg, p)) { | |
365 | fprintf(stderr, "Invalid UUID format: %s\n", arg); | |
366 | return EINVAL; | |
367 | } | |
368 | return 0; | |
369 | } | |
370 | ||
7dec050a | 371 | static errcode_t parse_hashalg(struct field_set_info *info, char *arg) |
f61fc0b5 TT |
372 | { |
373 | int hashv; | |
374 | unsigned char *p = (unsigned char *) info->ptr; | |
375 | ||
376 | hashv = e2p_string2hash(arg); | |
377 | if (hashv < 0) { | |
378 | fprintf(stderr, "Invalid hash algorithm: %s\n", arg); | |
379 | return EINVAL; | |
380 | } | |
381 | *p = hashv; | |
382 | return 0; | |
383 | } | |
384 | ||
08fd3f36 TT |
385 | static errcode_t parse_bmap(struct field_set_info *info, char *arg) |
386 | { | |
387 | unsigned long num; | |
388 | blk_t blk; | |
389 | errcode_t retval; | |
390 | char *tmp; | |
391 | ||
392 | num = strtoul(arg, &tmp, 0); | |
393 | if (*tmp) { | |
394 | fprintf(stderr, "Couldn't parse '%s' for field %s.\n", | |
395 | arg, info->name); | |
396 | return EINVAL; | |
397 | } | |
398 | blk = num; | |
399 | ||
efc6f628 | 400 | retval = ext2fs_bmap(current_fs, set_ino, &set_inode, 0, BMAP_SET, |
08fd3f36 TT |
401 | array_idx, &blk); |
402 | if (retval) { | |
403 | com_err("set_inode", retval, "while setting block map"); | |
404 | } | |
619a8ea7 | 405 | return retval; |
08fd3f36 TT |
406 | } |
407 | ||
cf96ecf1 AD |
408 | static errcode_t parse_gd_csum(struct field_set_info *info, char *arg) |
409 | { | |
410 | ||
411 | if (strcmp(arg, "calc") == 0) { | |
412 | ext2fs_group_desc_csum_set(current_fs, set_bg); | |
413 | set_gd = current_fs->group_desc[set_bg]; | |
efc6f628 | 414 | printf("Checksum set to 0x%04x\n", |
cf96ecf1 AD |
415 | current_fs->group_desc[set_bg].bg_checksum); |
416 | return 0; | |
417 | } | |
418 | ||
419 | return parse_uint(info, arg); | |
420 | } | |
f61fc0b5 | 421 | |
7dec050a | 422 | static void print_possible_fields(struct field_set_info *fields) |
6e96adcb | 423 | { |
7dec050a TT |
424 | struct field_set_info *ss; |
425 | const char *type, *cmd; | |
426 | FILE *f; | |
08fd3f36 | 427 | char name[40], idx[40]; |
6e96adcb | 428 | |
7dec050a TT |
429 | if (fields == super_fields) { |
430 | type = "Superblock"; | |
431 | cmd = "set_super_value"; | |
c7c1209e | 432 | } else if (fields == inode_fields) { |
7dec050a TT |
433 | type = "Inode"; |
434 | cmd = "set_inode"; | |
c7c1209e TT |
435 | } else { |
436 | type = "Block group descriptor"; | |
437 | cmd = "set_block_group"; | |
7dec050a TT |
438 | } |
439 | f = open_pager(); | |
440 | ||
441 | fprintf(f, "%s fields supported by the %s command:\n", type, cmd); | |
442 | ||
443 | for (ss = fields ; ss->name ; ss++) { | |
6e96adcb TT |
444 | type = "unknown"; |
445 | if (ss->func == parse_string) | |
446 | type = "string"; | |
447 | else if (ss->func == parse_int) | |
448 | type = "integer"; | |
449 | else if (ss->func == parse_uint) | |
450 | type = "unsigned integer"; | |
f61fc0b5 TT |
451 | else if (ss->func == parse_uuid) |
452 | type = "UUID"; | |
453 | else if (ss->func == parse_hashalg) | |
454 | type = "hash algorithm"; | |
7dec050a TT |
455 | else if (ss->func == parse_time) |
456 | type = "date/time"; | |
1d3a9511 TT |
457 | else if (ss->func == parse_bmap) |
458 | type = "set physical->logical block map"; | |
08fd3f36 TT |
459 | strcpy(name, ss->name); |
460 | if (ss->flags & FLAG_ARRAY) { | |
efc6f628 | 461 | if (ss->max_idx > 0) |
08fd3f36 TT |
462 | sprintf(idx, "[%d]", ss->max_idx); |
463 | else | |
464 | strcpy(idx, "[]"); | |
465 | strcat(name, idx); | |
466 | } | |
467 | fprintf(f, "\t%-20s\t%s\n", name, type); | |
6e96adcb | 468 | } |
7dec050a | 469 | close_pager(f); |
6e96adcb TT |
470 | } |
471 | ||
472 | ||
c6bd0c97 TT |
473 | void do_set_super(int argc, char *argv[]) |
474 | { | |
e1018eea TT |
475 | const char *usage = "<field> <value>\n" |
476 | "\t\"set_super_value -l\" will list the names of " | |
477 | "superblock fields\n\twhich can be set."; | |
7dec050a | 478 | static struct field_set_info *ss; |
efc6f628 | 479 | |
6e96adcb | 480 | if ((argc == 2) && !strcmp(argv[1], "-l")) { |
7dec050a | 481 | print_possible_fields(super_fields); |
6e96adcb TT |
482 | return; |
483 | } | |
484 | ||
e1018eea TT |
485 | if (common_args_process(argc, argv, 3, 3, "set_super_value", |
486 | usage, CHECK_FS_RW)) | |
c6bd0c97 TT |
487 | return; |
488 | ||
7dec050a | 489 | if ((ss = find_field(super_fields, argv[1])) == 0) { |
c6bd0c97 TT |
490 | com_err(argv[0], 0, "invalid field specifier: %s", argv[1]); |
491 | return; | |
492 | } | |
493 | set_sb = *current_fs->super; | |
494 | if (ss->func(ss, argv[2]) == 0) { | |
495 | *current_fs->super = set_sb; | |
496 | ext2fs_mark_super_dirty(current_fs); | |
497 | } | |
498 | } | |
7dec050a TT |
499 | |
500 | void do_set_inode(int argc, char *argv[]) | |
501 | { | |
502 | const char *usage = "<inode> <field> <value>\n" | |
1d3a9511 | 503 | "\t\"set_inode_field -l\" will list the names of " |
7dec050a TT |
504 | "the fields in an ext2 inode\n\twhich can be set."; |
505 | static struct field_set_info *ss; | |
efc6f628 | 506 | |
7dec050a TT |
507 | if ((argc == 2) && !strcmp(argv[1], "-l")) { |
508 | print_possible_fields(inode_fields); | |
509 | return; | |
510 | } | |
511 | ||
512 | if (common_args_process(argc, argv, 4, 4, "set_inode", | |
513 | usage, CHECK_FS_RW)) | |
514 | return; | |
515 | ||
516 | if ((ss = find_field(inode_fields, argv[2])) == 0) { | |
517 | com_err(argv[0], 0, "invalid field specifier: %s", argv[2]); | |
518 | return; | |
519 | } | |
520 | ||
08fd3f36 TT |
521 | set_ino = string_to_inode(argv[1]); |
522 | if (!set_ino) | |
7dec050a TT |
523 | return; |
524 | ||
08fd3f36 | 525 | if (debugfs_read_inode(set_ino, &set_inode, argv[1])) |
7dec050a TT |
526 | return; |
527 | ||
528 | if (ss->func(ss, argv[3]) == 0) { | |
08fd3f36 | 529 | if (debugfs_write_inode(set_ino, &set_inode, argv[1])) |
7dec050a TT |
530 | return; |
531 | } | |
532 | } | |
c7c1209e TT |
533 | |
534 | void do_set_block_group_descriptor(int argc, char *argv[]) | |
535 | { | |
536 | const char *usage = "<bg number> <field> <value>\n" | |
537 | "\t\"set_block_group_descriptor -l\" will list the names of " | |
538 | "the fields in a block group descriptor\n\twhich can be set."; | |
539 | struct field_set_info *ss; | |
c7c1209e TT |
540 | char *end; |
541 | ||
542 | if ((argc == 2) && !strcmp(argv[1], "-l")) { | |
543 | print_possible_fields(ext2_bg_fields); | |
544 | return; | |
545 | } | |
546 | ||
547 | if (common_args_process(argc, argv, 4, 4, "set_block_group_descriptor", | |
548 | usage, CHECK_FS_RW)) | |
549 | return; | |
550 | ||
551 | set_bg = strtoul(argv[1], &end, 0); | |
552 | if (*end) { | |
553 | com_err(argv[0], 0, "invalid block group number: %s", argv[1]); | |
554 | return; | |
555 | } | |
556 | ||
557 | if (set_bg >= current_fs->group_desc_count) { | |
558 | com_err(argv[0], 0, "block group number too big: %d", set_bg); | |
559 | return; | |
560 | } | |
561 | ||
562 | ||
563 | if ((ss = find_field(ext2_bg_fields, argv[2])) == 0) { | |
564 | com_err(argv[0], 0, "invalid field specifier: %s", argv[2]); | |
565 | return; | |
566 | } | |
567 | ||
568 | set_gd = current_fs->group_desc[set_bg]; | |
569 | ||
570 | if (ss->func(ss, argv[3]) == 0) { | |
571 | current_fs->group_desc[set_bg] = set_gd; | |
572 | ext2fs_mark_super_dirty(current_fs); | |
573 | } | |
574 | } |