]>
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 }, | |
412376ef | 77 | { "log_cluster_size", &set_sb.s_log_cluster_size, 4, parse_int }, |
c6bd0c97 | 78 | { "blocks_per_group", &set_sb.s_blocks_per_group, 4, parse_uint }, |
412376ef | 79 | { "clusters_per_group", &set_sb.s_clusters_per_group, 4, parse_uint }, |
c6bd0c97 | 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 }, | |
1ef4118e | 127 | { "free_blocks_hi", &set_sb.s_free_blocks_hi, 4, parse_uint }, |
007df860 TT |
128 | { "min_extra_isize", &set_sb.s_min_extra_isize, 2, parse_uint }, |
129 | { "want_extra_isize", &set_sb.s_want_extra_isize, 2, parse_uint }, | |
f77704e4 | 130 | { "flags", &set_sb.s_flags, 4, parse_uint }, |
007df860 TT |
131 | { "raid_stride", &set_sb.s_raid_stride, 2, parse_uint }, |
132 | { "min_extra_isize", &set_sb.s_min_extra_isize, 4, parse_uint }, | |
133 | { "mmp_interval", &set_sb.s_mmp_interval, 2, parse_uint }, | |
134 | { "mmp_block", &set_sb.s_mmp_block, 8, parse_uint }, | |
135 | { "raid_stripe_width", &set_sb.s_raid_stripe_width, 4, parse_uint }, | |
494a1daa | 136 | { "log_groups_per_flex", &set_sb.s_log_groups_per_flex, 1, parse_uint }, |
b7c5b403 | 137 | { "kbytes_written", &set_sb.s_kbytes_written, 8, parse_uint }, |
f5448c19 TT |
138 | { "snapshot_inum", &set_sb.s_snapshot_inum, 4, parse_uint }, |
139 | { "snapshot_id", &set_sb.s_snapshot_id, 4, parse_uint }, | |
140 | { "snapshot_r_blocks_count", &set_sb.s_snapshot_r_blocks_count, 8, | |
141 | parse_uint }, | |
142 | { "snapshot_list", &set_sb.s_snapshot_list, 4, parse_uint }, | |
9345f026 | 143 | { "mount_opts", &set_sb.s_mount_opts, 64, parse_string }, |
0edcc270 AK |
144 | { "usr_quota_inum", &set_sb.s_usr_quota_inum, 4, parse_uint }, |
145 | { "grp_quota_inum", &set_sb.s_grp_quota_inum, 4, parse_uint }, | |
4df16182 | 146 | { "overhead_blocks", &set_sb.s_overhead_blocks, 4, parse_uint }, |
89efc88e | 147 | { "checksum", &set_sb.s_checksum, 4, parse_uint }, |
c6bd0c97 TT |
148 | { 0, 0, 0, 0 } |
149 | }; | |
150 | ||
7dec050a TT |
151 | static struct field_set_info inode_fields[] = { |
152 | { "inodes_count", &set_sb.s_inodes_count, 4, parse_uint }, | |
153 | { "mode", &set_inode.i_mode, 2, parse_uint }, | |
154 | { "uid", &set_inode.i_uid, 2, parse_uint }, | |
38d732bb | 155 | { "size", &set_inode.i_size, 4, parse_uint }, |
7dec050a TT |
156 | { "atime", &set_inode.i_atime, 4, parse_time }, |
157 | { "ctime", &set_inode.i_ctime, 4, parse_time }, | |
158 | { "mtime", &set_inode.i_mtime, 4, parse_time }, | |
159 | { "dtime", &set_inode.i_dtime, 4, parse_time }, | |
160 | { "gid", &set_inode.i_gid, 2, parse_uint }, | |
161 | { "links_count", &set_inode.i_links_count, 2, parse_uint }, | |
162 | { "blocks", &set_inode.i_blocks, 4, parse_uint }, | |
163 | { "flags", &set_inode.i_flags, 4, parse_uint }, | |
d362a3fb | 164 | { "version", &set_inode.osd1.linux1.l_i_version, 4, parse_uint }, |
7dec050a | 165 | { "translator", &set_inode.osd1.hurd1.h_i_translator, 4, parse_uint }, |
efc6f628 | 166 | { "block", &set_inode.i_block[0], 4, parse_uint, FLAG_ARRAY, |
08fd3f36 TT |
167 | EXT2_NDIR_BLOCKS }, |
168 | { "block[IND]", &set_inode.i_block[EXT2_IND_BLOCK], 4, parse_uint }, | |
169 | { "block[DIND]", &set_inode.i_block[EXT2_DIND_BLOCK], 4, parse_uint }, | |
170 | { "block[TIND]", &set_inode.i_block[EXT2_TIND_BLOCK], 4, parse_uint }, | |
7dec050a TT |
171 | { "generation", &set_inode.i_generation, 4, parse_uint }, |
172 | { "file_acl", &set_inode.i_file_acl, 4, parse_uint }, | |
fa15820f TT |
173 | { "file_acl_high", &set_inode.osd2.linux2.l_i_file_acl_high, 2, |
174 | parse_uint }, | |
7dec050a | 175 | { "dir_acl", &set_inode.i_dir_acl, 4, parse_uint }, |
007df860 | 176 | { "size_high", &set_inode.i_size_high, 4, parse_uint }, |
7dec050a | 177 | { "faddr", &set_inode.i_faddr, 4, parse_uint }, |
5d17119d TT |
178 | { "blocks_hi", &set_inode.osd2.linux2.l_i_blocks_hi, 2, parse_uint }, |
179 | { "frag", &set_inode.osd2.hurd2.h_i_frag, 1, parse_uint }, | |
180 | { "fsize", &set_inode.osd2.hurd2.h_i_fsize, 1, parse_uint }, | |
caf037a4 TT |
181 | { "uid_high", &set_inode.osd2.linux2.l_i_uid_high, 2, parse_uint }, |
182 | { "gid_high", &set_inode.osd2.linux2.l_i_gid_high, 2, parse_uint }, | |
89efc88e | 183 | { "checksum", &set_inode.osd2.linux2.l_i_checksum_lo, 2, parse_uint }, |
caf037a4 | 184 | { "author", &set_inode.osd2.hurd2.h_i_author, 4, parse_uint }, |
08fd3f36 | 185 | { "bmap", NULL, 4, parse_bmap, FLAG_ARRAY }, |
7dec050a TT |
186 | { 0, 0, 0, 0 } |
187 | }; | |
188 | ||
c7c1209e TT |
189 | static struct field_set_info ext2_bg_fields[] = { |
190 | { "block_bitmap", &set_gd.bg_block_bitmap, 4, parse_uint }, | |
191 | { "inode_bitmap", &set_gd.bg_inode_bitmap, 4, parse_uint }, | |
192 | { "inode_table", &set_gd.bg_inode_table, 4, parse_uint }, | |
193 | { "free_blocks_count", &set_gd.bg_free_blocks_count, 2, parse_uint }, | |
194 | { "free_inodes_count", &set_gd.bg_free_inodes_count, 2, parse_uint }, | |
195 | { "used_dirs_count", &set_gd.bg_used_dirs_count, 2, parse_uint }, | |
196 | { "flags", &set_gd.bg_flags, 2, parse_uint }, | |
c7c1209e | 197 | { "itable_unused", &set_gd.bg_itable_unused, 2, parse_uint }, |
cf96ecf1 | 198 | { "checksum", &set_gd.bg_checksum, 2, parse_gd_csum }, |
c7c1209e TT |
199 | { 0, 0, 0, 0 } |
200 | }; | |
201 | ||
202 | ||
7dec050a TT |
203 | static struct field_set_info *find_field(struct field_set_info *fields, |
204 | char *field) | |
c6bd0c97 | 205 | { |
7dec050a | 206 | struct field_set_info *ss; |
08fd3f36 TT |
207 | const char *prefix; |
208 | char *arg, *delim, *idx, *tmp; | |
c7c1209e | 209 | int prefix_len; |
08fd3f36 TT |
210 | |
211 | if (fields == super_fields) | |
212 | prefix = "s_"; | |
c7c1209e | 213 | else if (fields == inode_fields) |
08fd3f36 | 214 | prefix = "i_"; |
c7c1209e TT |
215 | else |
216 | prefix = "bg_"; | |
217 | prefix_len = strlen(prefix); | |
218 | if (strncmp(field, prefix, prefix_len) == 0) | |
219 | field += prefix_len; | |
08fd3f36 TT |
220 | |
221 | arg = malloc(strlen(field)+1); | |
222 | if (!arg) | |
223 | return NULL; | |
224 | strcpy(arg, field); | |
225 | ||
226 | idx = strchr(arg, '['); | |
227 | if (idx) { | |
228 | *idx++ = 0; | |
229 | delim = idx + strlen(idx) - 1; | |
230 | if (!*idx || *delim != ']') | |
231 | idx = 0; | |
232 | else | |
233 | *delim = 0; | |
234 | } | |
efc6f628 | 235 | /* |
08fd3f36 TT |
236 | * Can we parse the number? |
237 | */ | |
238 | if (idx) { | |
239 | array_idx = strtol(idx, &tmp, 0); | |
240 | if (*tmp) | |
241 | idx = 0; | |
242 | } | |
243 | ||
7dec050a | 244 | for (ss = fields ; ss->name ; ss++) { |
08fd3f36 TT |
245 | if (ss->flags & FLAG_ARRAY) { |
246 | if (!idx || (strcmp(ss->name, arg) != 0)) | |
247 | continue; | |
248 | if (ss->max_idx > 0 && array_idx >= ss->max_idx) | |
249 | continue; | |
250 | } else { | |
251 | if (strcmp(ss->name, field) != 0) | |
252 | continue; | |
253 | } | |
6e9761c2 | 254 | free(arg); |
08fd3f36 | 255 | return ss; |
c6bd0c97 | 256 | } |
6e9761c2 | 257 | free(arg); |
c6bd0c97 TT |
258 | return NULL; |
259 | } | |
260 | ||
7dec050a | 261 | static errcode_t parse_uint(struct field_set_info *info, char *arg) |
c6bd0c97 | 262 | { |
007df860 | 263 | unsigned long long num, limit; |
c6bd0c97 | 264 | char *tmp; |
08fd3f36 | 265 | union { |
007df860 | 266 | __u64 *ptr64; |
08fd3f36 TT |
267 | __u32 *ptr32; |
268 | __u16 *ptr16; | |
269 | __u8 *ptr8; | |
270 | } u; | |
271 | ||
272 | u.ptr8 = (__u8 *) info->ptr; | |
273 | if (info->flags & FLAG_ARRAY) | |
274 | u.ptr8 += array_idx * info->size; | |
c6bd0c97 | 275 | |
007df860 TT |
276 | errno = 0; |
277 | num = STRTOULL(arg, &tmp, 0); | |
278 | if (*tmp || errno) { | |
c6bd0c97 TT |
279 | fprintf(stderr, "Couldn't parse '%s' for field %s.\n", |
280 | arg, info->name); | |
281 | return EINVAL; | |
282 | } | |
007df860 TT |
283 | limit = ~0ULL >> ((8 - info->size) * 8); |
284 | if (num > limit) { | |
285 | fprintf(stderr, "Value '%s' exceeds field %s maximum %llu.\n", | |
286 | arg, info->name, limit); | |
287 | return EINVAL; | |
288 | } | |
c6bd0c97 | 289 | switch (info->size) { |
007df860 TT |
290 | case 8: |
291 | *u.ptr64 = num; | |
292 | break; | |
c6bd0c97 | 293 | case 4: |
08fd3f36 | 294 | *u.ptr32 = num; |
c6bd0c97 TT |
295 | break; |
296 | case 2: | |
08fd3f36 | 297 | *u.ptr16 = num; |
c6bd0c97 TT |
298 | break; |
299 | case 1: | |
08fd3f36 | 300 | *u.ptr8 = num; |
c6bd0c97 TT |
301 | break; |
302 | } | |
303 | return 0; | |
304 | } | |
305 | ||
7dec050a | 306 | static errcode_t parse_int(struct field_set_info *info, char *arg) |
c6bd0c97 TT |
307 | { |
308 | long num; | |
309 | char *tmp; | |
310 | __s32 *ptr32; | |
311 | __s16 *ptr16; | |
312 | __s8 *ptr8; | |
313 | ||
314 | num = strtol(arg, &tmp, 0); | |
315 | if (*tmp) { | |
316 | fprintf(stderr, "Couldn't parse '%s' for field %s.\n", | |
317 | arg, info->name); | |
318 | return EINVAL; | |
319 | } | |
320 | switch (info->size) { | |
321 | case 4: | |
322 | ptr32 = (__s32 *) info->ptr; | |
323 | *ptr32 = num; | |
324 | break; | |
325 | case 2: | |
326 | ptr16 = (__s16 *) info->ptr; | |
327 | *ptr16 = num; | |
328 | break; | |
329 | case 1: | |
330 | ptr8 = (__s8 *) info->ptr; | |
331 | *ptr8 = num; | |
332 | break; | |
333 | } | |
334 | return 0; | |
335 | } | |
336 | ||
7dec050a | 337 | static errcode_t parse_string(struct field_set_info *info, char *arg) |
c6bd0c97 TT |
338 | { |
339 | char *cp = (char *) info->ptr; | |
340 | ||
341 | if (strlen(arg) >= info->size) { | |
342 | fprintf(stderr, "Error maximum size for %s is %d.\n", | |
343 | info->name, info->size); | |
344 | return EINVAL; | |
345 | } | |
346 | strcpy(cp, arg); | |
347 | return 0; | |
348 | } | |
349 | ||
7dec050a TT |
350 | static errcode_t parse_time(struct field_set_info *info, char *arg) |
351 | { | |
e3e92534 TT |
352 | time_t t; |
353 | __u32 *ptr32; | |
7dec050a TT |
354 | |
355 | ptr32 = (__u32 *) info->ptr; | |
356 | ||
4efae606 TT |
357 | t = string_to_time(arg); |
358 | ||
359 | if (t == ((time_t) -1)) { | |
360 | fprintf(stderr, "Couldn't parse '%s' for field %s.\n", | |
361 | arg, info->name); | |
362 | return EINVAL; | |
7dec050a | 363 | } |
4efae606 | 364 | *ptr32 = t; |
7dec050a TT |
365 | return 0; |
366 | } | |
367 | ||
368 | static errcode_t parse_uuid(struct field_set_info *info, char *arg) | |
f61fc0b5 | 369 | { |
3e699064 | 370 | unsigned char * p = (unsigned char *) info->ptr; |
efc6f628 | 371 | |
f61fc0b5 TT |
372 | if ((strcasecmp(arg, "null") == 0) || |
373 | (strcasecmp(arg, "clear") == 0)) { | |
374 | uuid_clear(p); | |
375 | } else if (strcasecmp(arg, "time") == 0) { | |
376 | uuid_generate_time(p); | |
377 | } else if (strcasecmp(arg, "random") == 0) { | |
378 | uuid_generate(p); | |
379 | } else if (uuid_parse(arg, p)) { | |
380 | fprintf(stderr, "Invalid UUID format: %s\n", arg); | |
381 | return EINVAL; | |
382 | } | |
383 | return 0; | |
384 | } | |
385 | ||
7dec050a | 386 | static errcode_t parse_hashalg(struct field_set_info *info, char *arg) |
f61fc0b5 TT |
387 | { |
388 | int hashv; | |
389 | unsigned char *p = (unsigned char *) info->ptr; | |
390 | ||
391 | hashv = e2p_string2hash(arg); | |
392 | if (hashv < 0) { | |
393 | fprintf(stderr, "Invalid hash algorithm: %s\n", arg); | |
394 | return EINVAL; | |
395 | } | |
396 | *p = hashv; | |
397 | return 0; | |
398 | } | |
399 | ||
08fd3f36 TT |
400 | static errcode_t parse_bmap(struct field_set_info *info, char *arg) |
401 | { | |
402 | unsigned long num; | |
403 | blk_t blk; | |
404 | errcode_t retval; | |
405 | char *tmp; | |
406 | ||
407 | num = strtoul(arg, &tmp, 0); | |
408 | if (*tmp) { | |
409 | fprintf(stderr, "Couldn't parse '%s' for field %s.\n", | |
410 | arg, info->name); | |
411 | return EINVAL; | |
412 | } | |
413 | blk = num; | |
414 | ||
efc6f628 | 415 | retval = ext2fs_bmap(current_fs, set_ino, &set_inode, 0, BMAP_SET, |
08fd3f36 TT |
416 | array_idx, &blk); |
417 | if (retval) { | |
418 | com_err("set_inode", retval, "while setting block map"); | |
419 | } | |
619a8ea7 | 420 | return retval; |
08fd3f36 TT |
421 | } |
422 | ||
cf96ecf1 AD |
423 | static errcode_t parse_gd_csum(struct field_set_info *info, char *arg) |
424 | { | |
425 | ||
426 | if (strcmp(arg, "calc") == 0) { | |
427 | ext2fs_group_desc_csum_set(current_fs, set_bg); | |
d7cca6b0 VAH |
428 | memcpy(&set_gd, ext2fs_group_desc(current_fs, |
429 | current_fs->group_desc, | |
430 | set_bg), | |
431 | sizeof(set_gd)); | |
efc6f628 | 432 | printf("Checksum set to 0x%04x\n", |
048786d7 | 433 | ext2fs_bg_checksum(current_fs, set_bg)); |
cf96ecf1 AD |
434 | return 0; |
435 | } | |
436 | ||
437 | return parse_uint(info, arg); | |
438 | } | |
f61fc0b5 | 439 | |
7dec050a | 440 | static void print_possible_fields(struct field_set_info *fields) |
6e96adcb | 441 | { |
7dec050a TT |
442 | struct field_set_info *ss; |
443 | const char *type, *cmd; | |
444 | FILE *f; | |
08fd3f36 | 445 | char name[40], idx[40]; |
6e96adcb | 446 | |
7dec050a TT |
447 | if (fields == super_fields) { |
448 | type = "Superblock"; | |
449 | cmd = "set_super_value"; | |
c7c1209e | 450 | } else if (fields == inode_fields) { |
7dec050a TT |
451 | type = "Inode"; |
452 | cmd = "set_inode"; | |
c7c1209e TT |
453 | } else { |
454 | type = "Block group descriptor"; | |
455 | cmd = "set_block_group"; | |
7dec050a TT |
456 | } |
457 | f = open_pager(); | |
458 | ||
459 | fprintf(f, "%s fields supported by the %s command:\n", type, cmd); | |
460 | ||
461 | for (ss = fields ; ss->name ; ss++) { | |
6e96adcb TT |
462 | type = "unknown"; |
463 | if (ss->func == parse_string) | |
464 | type = "string"; | |
465 | else if (ss->func == parse_int) | |
466 | type = "integer"; | |
467 | else if (ss->func == parse_uint) | |
468 | type = "unsigned integer"; | |
f61fc0b5 TT |
469 | else if (ss->func == parse_uuid) |
470 | type = "UUID"; | |
471 | else if (ss->func == parse_hashalg) | |
472 | type = "hash algorithm"; | |
7dec050a TT |
473 | else if (ss->func == parse_time) |
474 | type = "date/time"; | |
1d3a9511 TT |
475 | else if (ss->func == parse_bmap) |
476 | type = "set physical->logical block map"; | |
08fd3f36 TT |
477 | strcpy(name, ss->name); |
478 | if (ss->flags & FLAG_ARRAY) { | |
efc6f628 | 479 | if (ss->max_idx > 0) |
08fd3f36 TT |
480 | sprintf(idx, "[%d]", ss->max_idx); |
481 | else | |
482 | strcpy(idx, "[]"); | |
483 | strcat(name, idx); | |
484 | } | |
485 | fprintf(f, "\t%-20s\t%s\n", name, type); | |
6e96adcb | 486 | } |
7dec050a | 487 | close_pager(f); |
6e96adcb TT |
488 | } |
489 | ||
490 | ||
c6bd0c97 TT |
491 | void do_set_super(int argc, char *argv[]) |
492 | { | |
e1018eea TT |
493 | const char *usage = "<field> <value>\n" |
494 | "\t\"set_super_value -l\" will list the names of " | |
495 | "superblock fields\n\twhich can be set."; | |
7dec050a | 496 | static struct field_set_info *ss; |
efc6f628 | 497 | |
6e96adcb | 498 | if ((argc == 2) && !strcmp(argv[1], "-l")) { |
7dec050a | 499 | print_possible_fields(super_fields); |
6e96adcb TT |
500 | return; |
501 | } | |
502 | ||
e1018eea TT |
503 | if (common_args_process(argc, argv, 3, 3, "set_super_value", |
504 | usage, CHECK_FS_RW)) | |
c6bd0c97 TT |
505 | return; |
506 | ||
7dec050a | 507 | if ((ss = find_field(super_fields, argv[1])) == 0) { |
c6bd0c97 TT |
508 | com_err(argv[0], 0, "invalid field specifier: %s", argv[1]); |
509 | return; | |
510 | } | |
511 | set_sb = *current_fs->super; | |
512 | if (ss->func(ss, argv[2]) == 0) { | |
513 | *current_fs->super = set_sb; | |
514 | ext2fs_mark_super_dirty(current_fs); | |
515 | } | |
516 | } | |
7dec050a TT |
517 | |
518 | void do_set_inode(int argc, char *argv[]) | |
519 | { | |
520 | const char *usage = "<inode> <field> <value>\n" | |
1d3a9511 | 521 | "\t\"set_inode_field -l\" will list the names of " |
7dec050a TT |
522 | "the fields in an ext2 inode\n\twhich can be set."; |
523 | static struct field_set_info *ss; | |
efc6f628 | 524 | |
7dec050a TT |
525 | if ((argc == 2) && !strcmp(argv[1], "-l")) { |
526 | print_possible_fields(inode_fields); | |
527 | return; | |
528 | } | |
529 | ||
530 | if (common_args_process(argc, argv, 4, 4, "set_inode", | |
531 | usage, CHECK_FS_RW)) | |
532 | return; | |
533 | ||
534 | if ((ss = find_field(inode_fields, argv[2])) == 0) { | |
535 | com_err(argv[0], 0, "invalid field specifier: %s", argv[2]); | |
536 | return; | |
537 | } | |
538 | ||
08fd3f36 TT |
539 | set_ino = string_to_inode(argv[1]); |
540 | if (!set_ino) | |
7dec050a TT |
541 | return; |
542 | ||
08fd3f36 | 543 | if (debugfs_read_inode(set_ino, &set_inode, argv[1])) |
7dec050a TT |
544 | return; |
545 | ||
546 | if (ss->func(ss, argv[3]) == 0) { | |
08fd3f36 | 547 | if (debugfs_write_inode(set_ino, &set_inode, argv[1])) |
7dec050a TT |
548 | return; |
549 | } | |
550 | } | |
c7c1209e TT |
551 | |
552 | void do_set_block_group_descriptor(int argc, char *argv[]) | |
553 | { | |
554 | const char *usage = "<bg number> <field> <value>\n" | |
555 | "\t\"set_block_group_descriptor -l\" will list the names of " | |
556 | "the fields in a block group descriptor\n\twhich can be set."; | |
557 | struct field_set_info *ss; | |
c7c1209e TT |
558 | char *end; |
559 | ||
560 | if ((argc == 2) && !strcmp(argv[1], "-l")) { | |
561 | print_possible_fields(ext2_bg_fields); | |
562 | return; | |
563 | } | |
564 | ||
565 | if (common_args_process(argc, argv, 4, 4, "set_block_group_descriptor", | |
566 | usage, CHECK_FS_RW)) | |
567 | return; | |
568 | ||
569 | set_bg = strtoul(argv[1], &end, 0); | |
570 | if (*end) { | |
571 | com_err(argv[0], 0, "invalid block group number: %s", argv[1]); | |
572 | return; | |
573 | } | |
574 | ||
575 | if (set_bg >= current_fs->group_desc_count) { | |
576 | com_err(argv[0], 0, "block group number too big: %d", set_bg); | |
577 | return; | |
578 | } | |
579 | ||
580 | ||
581 | if ((ss = find_field(ext2_bg_fields, argv[2])) == 0) { | |
582 | com_err(argv[0], 0, "invalid field specifier: %s", argv[2]); | |
583 | return; | |
584 | } | |
585 | ||
d7cca6b0 VAH |
586 | memcpy(&set_gd, ext2fs_group_desc(current_fs, |
587 | current_fs->group_desc, set_bg), | |
588 | sizeof(set_gd)); | |
c7c1209e TT |
589 | |
590 | if (ss->func(ss, argv[3]) == 0) { | |
d7cca6b0 VAH |
591 | memcpy(ext2fs_group_desc(current_fs, |
592 | current_fs->group_desc, set_bg), | |
593 | &set_gd, sizeof(set_gd)); | |
c7c1209e TT |
594 | ext2fs_mark_super_dirty(current_fs); |
595 | } | |
596 | } |