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