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