set_fields.c revision 9d92a201deec7bbb1911e6e5ee98abf3c83882d4
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 <stdio.h> 21#include <unistd.h> 22#include <stdlib.h> 23#include <ctype.h> 24#include <string.h> 25#include <strings.h> 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 32#if HAVE_STRINGS_H 33#include <strings.h> 34#endif 35#include <fcntl.h> 36#include <utime.h> 37 38#include "debugfs.h" 39#include "uuid/uuid.h" 40#include "e2p/e2p.h" 41 42static struct ext2_super_block set_sb; 43static struct ext2_inode set_inode; 44static struct ext2_group_desc set_gd; 45static dgrp_t set_bg; 46static ext2_ino_t set_ino; 47static int array_idx; 48 49#define FLAG_ARRAY 0x0001 50 51struct field_set_info { 52 const char *name; 53 void *ptr; 54 unsigned int size; 55 errcode_t (*func)(struct field_set_info *info, char *arg); 56 int flags; 57 int max_idx; 58}; 59 60static errcode_t parse_uint(struct field_set_info *info, char *arg); 61static errcode_t parse_int(struct field_set_info *info, char *arg); 62static errcode_t parse_string(struct field_set_info *info, char *arg); 63static errcode_t parse_uuid(struct field_set_info *info, char *arg); 64static errcode_t parse_hashalg(struct field_set_info *info, char *arg); 65static errcode_t parse_time(struct field_set_info *info, char *arg); 66static errcode_t parse_bmap(struct field_set_info *info, char *arg); 67static errcode_t parse_gd_csum(struct field_set_info *info, char *arg); 68 69static struct field_set_info super_fields[] = { 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 }, 81 { "mtime", &set_sb.s_mtime, 4, parse_time }, 82 { "wtime", &set_sb.s_wtime, 4, parse_time }, 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 }, 89 { "lastcheck", &set_sb.s_lastcheck, 4, parse_time }, 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 }, 96 { "inode_size", &set_sb.s_inode_size, 2, parse_uint }, 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 }, 100 { "feature_ro_compat", &set_sb.s_feature_ro_compat, 4, parse_uint }, 101 { "uuid", &set_sb.s_uuid, 16, parse_uuid }, 102 { "volume_name", &set_sb.s_volume_name, 16, parse_string }, 103 { "last_mounted", &set_sb.s_last_mounted, 64, parse_string }, 104 { "lastcheck", &set_sb.s_lastcheck, 4, parse_uint }, 105 { "algorithm_usage_bitmap", &set_sb.s_algorithm_usage_bitmap, 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 }, 110 { "reserved_gdt_blocks", &set_sb.s_reserved_gdt_blocks, 2, 111 parse_uint }, 112 { "journal_uuid", &set_sb.s_journal_uuid, 16, parse_uuid }, 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 }, 116 { "hash_seed", &set_sb.s_hash_seed, 16, parse_uuid }, 117 { "def_hash_version", &set_sb.s_def_hash_version, 1, parse_hashalg }, 118 { "jnl_backup_type", &set_sb.s_jnl_backup_type, 1, parse_uint }, 119 { "desc_size", &set_sb.s_desc_size, 2, parse_uint }, 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 }, 122 { "mkfs_time", &set_sb.s_mkfs_time, 4, parse_time }, 123 { "jnl_blocks", &set_sb.s_jnl_blocks[0], 4, parse_uint, FLAG_ARRAY, 124 17 }, 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 { "free_blocks_hi", &set_sb.s_free_blocks_hi, 4, parse_uint }, 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 }, 130 { "flags", &set_sb.s_flags, 4, parse_uint }, 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 }, 136 { "log_groups_per_flex", &set_sb.s_log_groups_per_flex, 1, parse_uint }, 137 { "kbytes_written", &set_sb.s_kbytes_written, 8, parse_uint }, 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 }, 143 { "mount_opts", &set_sb.s_mount_opts, 64, parse_string }, 144 { 0, 0, 0, 0 } 145}; 146 147static struct field_set_info inode_fields[] = { 148 { "inodes_count", &set_sb.s_inodes_count, 4, parse_uint }, 149 { "mode", &set_inode.i_mode, 2, parse_uint }, 150 { "uid", &set_inode.i_uid, 2, parse_uint }, 151 { "size", &set_inode.i_size, 4, parse_uint }, 152 { "atime", &set_inode.i_atime, 4, parse_time }, 153 { "ctime", &set_inode.i_ctime, 4, parse_time }, 154 { "mtime", &set_inode.i_mtime, 4, parse_time }, 155 { "dtime", &set_inode.i_dtime, 4, parse_time }, 156 { "gid", &set_inode.i_gid, 2, parse_uint }, 157 { "links_count", &set_inode.i_links_count, 2, parse_uint }, 158 { "blocks", &set_inode.i_blocks, 4, parse_uint }, 159 { "flags", &set_inode.i_flags, 4, parse_uint }, 160 { "version", &set_inode.osd1.linux1.l_i_version, 4, parse_uint }, 161 { "translator", &set_inode.osd1.hurd1.h_i_translator, 4, parse_uint }, 162 { "block", &set_inode.i_block[0], 4, parse_uint, FLAG_ARRAY, 163 EXT2_NDIR_BLOCKS }, 164 { "block[IND]", &set_inode.i_block[EXT2_IND_BLOCK], 4, parse_uint }, 165 { "block[DIND]", &set_inode.i_block[EXT2_DIND_BLOCK], 4, parse_uint }, 166 { "block[TIND]", &set_inode.i_block[EXT2_TIND_BLOCK], 4, parse_uint }, 167 { "generation", &set_inode.i_generation, 4, parse_uint }, 168 { "file_acl", &set_inode.i_file_acl, 4, parse_uint }, 169 { "file_acl_high", &set_inode.osd2.linux2.l_i_file_acl_high, 2, 170 parse_uint }, 171 { "dir_acl", &set_inode.i_dir_acl, 4, parse_uint }, 172 { "size_high", &set_inode.i_size_high, 4, parse_uint }, 173 { "faddr", &set_inode.i_faddr, 4, parse_uint }, 174 { "blocks_hi", &set_inode.osd2.linux2.l_i_blocks_hi, 2, parse_uint }, 175 { "frag", &set_inode.osd2.hurd2.h_i_frag, 1, parse_uint }, 176 { "fsize", &set_inode.osd2.hurd2.h_i_fsize, 1, parse_uint }, 177 { "uid_high", &set_inode.osd2.linux2.l_i_uid_high, 2, parse_uint }, 178 { "gid_high", &set_inode.osd2.linux2.l_i_gid_high, 2, parse_uint }, 179 { "author", &set_inode.osd2.hurd2.h_i_author, 4, parse_uint }, 180 { "bmap", NULL, 4, parse_bmap, FLAG_ARRAY }, 181 { 0, 0, 0, 0 } 182}; 183 184static struct field_set_info ext2_bg_fields[] = { 185 { "block_bitmap", &set_gd.bg_block_bitmap, 4, parse_uint }, 186 { "inode_bitmap", &set_gd.bg_inode_bitmap, 4, parse_uint }, 187 { "inode_table", &set_gd.bg_inode_table, 4, parse_uint }, 188 { "free_blocks_count", &set_gd.bg_free_blocks_count, 2, parse_uint }, 189 { "free_inodes_count", &set_gd.bg_free_inodes_count, 2, parse_uint }, 190 { "used_dirs_count", &set_gd.bg_used_dirs_count, 2, parse_uint }, 191 { "flags", &set_gd.bg_flags, 2, parse_uint }, 192 { "reserved", &set_gd.bg_reserved, 2, parse_uint, FLAG_ARRAY, 2 }, 193 { "itable_unused", &set_gd.bg_itable_unused, 2, parse_uint }, 194 { "checksum", &set_gd.bg_checksum, 2, parse_gd_csum }, 195 { 0, 0, 0, 0 } 196}; 197 198 199static struct field_set_info *find_field(struct field_set_info *fields, 200 char *field) 201{ 202 struct field_set_info *ss; 203 const char *prefix; 204 char *arg, *delim, *idx, *tmp; 205 int prefix_len; 206 207 if (fields == super_fields) 208 prefix = "s_"; 209 else if (fields == inode_fields) 210 prefix = "i_"; 211 else 212 prefix = "bg_"; 213 prefix_len = strlen(prefix); 214 if (strncmp(field, prefix, prefix_len) == 0) 215 field += prefix_len; 216 217 arg = malloc(strlen(field)+1); 218 if (!arg) 219 return NULL; 220 strcpy(arg, field); 221 222 idx = strchr(arg, '['); 223 if (idx) { 224 *idx++ = 0; 225 delim = idx + strlen(idx) - 1; 226 if (!*idx || *delim != ']') 227 idx = 0; 228 else 229 *delim = 0; 230 } 231 /* 232 * Can we parse the number? 233 */ 234 if (idx) { 235 array_idx = strtol(idx, &tmp, 0); 236 if (*tmp) 237 idx = 0; 238 } 239 240 for (ss = fields ; ss->name ; ss++) { 241 if (ss->flags & FLAG_ARRAY) { 242 if (!idx || (strcmp(ss->name, arg) != 0)) 243 continue; 244 if (ss->max_idx > 0 && array_idx >= ss->max_idx) 245 continue; 246 } else { 247 if (strcmp(ss->name, field) != 0) 248 continue; 249 } 250 free(arg); 251 return ss; 252 } 253 free(arg); 254 return NULL; 255} 256 257static errcode_t parse_uint(struct field_set_info *info, char *arg) 258{ 259 unsigned long long num, limit; 260 char *tmp; 261 union { 262 __u64 *ptr64; 263 __u32 *ptr32; 264 __u16 *ptr16; 265 __u8 *ptr8; 266 } u; 267 268 u.ptr8 = (__u8 *) info->ptr; 269 if (info->flags & FLAG_ARRAY) 270 u.ptr8 += array_idx * info->size; 271 272 errno = 0; 273 num = STRTOULL(arg, &tmp, 0); 274 if (*tmp || errno) { 275 fprintf(stderr, "Couldn't parse '%s' for field %s.\n", 276 arg, info->name); 277 return EINVAL; 278 } 279 limit = ~0ULL >> ((8 - info->size) * 8); 280 if (num > limit) { 281 fprintf(stderr, "Value '%s' exceeds field %s maximum %llu.\n", 282 arg, info->name, limit); 283 return EINVAL; 284 } 285 switch (info->size) { 286 case 8: 287 *u.ptr64 = num; 288 break; 289 case 4: 290 *u.ptr32 = num; 291 break; 292 case 2: 293 *u.ptr16 = num; 294 break; 295 case 1: 296 *u.ptr8 = num; 297 break; 298 } 299 return 0; 300} 301 302static errcode_t parse_int(struct field_set_info *info, char *arg) 303{ 304 long num; 305 char *tmp; 306 __s32 *ptr32; 307 __s16 *ptr16; 308 __s8 *ptr8; 309 310 num = strtol(arg, &tmp, 0); 311 if (*tmp) { 312 fprintf(stderr, "Couldn't parse '%s' for field %s.\n", 313 arg, info->name); 314 return EINVAL; 315 } 316 switch (info->size) { 317 case 4: 318 ptr32 = (__s32 *) info->ptr; 319 *ptr32 = num; 320 break; 321 case 2: 322 ptr16 = (__s16 *) info->ptr; 323 *ptr16 = num; 324 break; 325 case 1: 326 ptr8 = (__s8 *) info->ptr; 327 *ptr8 = num; 328 break; 329 } 330 return 0; 331} 332 333static errcode_t parse_string(struct field_set_info *info, char *arg) 334{ 335 char *cp = (char *) info->ptr; 336 337 if (strlen(arg) >= info->size) { 338 fprintf(stderr, "Error maximum size for %s is %d.\n", 339 info->name, info->size); 340 return EINVAL; 341 } 342 strcpy(cp, arg); 343 return 0; 344} 345 346static errcode_t parse_time(struct field_set_info *info, char *arg) 347{ 348 time_t t; 349 __u32 *ptr32; 350 351 ptr32 = (__u32 *) info->ptr; 352 353 t = string_to_time(arg); 354 355 if (t == ((time_t) -1)) { 356 fprintf(stderr, "Couldn't parse '%s' for field %s.\n", 357 arg, info->name); 358 return EINVAL; 359 } 360 *ptr32 = t; 361 return 0; 362} 363 364static errcode_t parse_uuid(struct field_set_info *info, char *arg) 365{ 366 unsigned char * p = (unsigned char *) info->ptr; 367 368 if ((strcasecmp(arg, "null") == 0) || 369 (strcasecmp(arg, "clear") == 0)) { 370 uuid_clear(p); 371 } else if (strcasecmp(arg, "time") == 0) { 372 uuid_generate_time(p); 373 } else if (strcasecmp(arg, "random") == 0) { 374 uuid_generate(p); 375 } else if (uuid_parse(arg, p)) { 376 fprintf(stderr, "Invalid UUID format: %s\n", arg); 377 return EINVAL; 378 } 379 return 0; 380} 381 382static errcode_t parse_hashalg(struct field_set_info *info, char *arg) 383{ 384 int hashv; 385 unsigned char *p = (unsigned char *) info->ptr; 386 387 hashv = e2p_string2hash(arg); 388 if (hashv < 0) { 389 fprintf(stderr, "Invalid hash algorithm: %s\n", arg); 390 return EINVAL; 391 } 392 *p = hashv; 393 return 0; 394} 395 396static errcode_t parse_bmap(struct field_set_info *info, char *arg) 397{ 398 unsigned long num; 399 blk_t blk; 400 errcode_t retval; 401 char *tmp; 402 403 num = strtoul(arg, &tmp, 0); 404 if (*tmp) { 405 fprintf(stderr, "Couldn't parse '%s' for field %s.\n", 406 arg, info->name); 407 return EINVAL; 408 } 409 blk = num; 410 411 retval = ext2fs_bmap(current_fs, set_ino, &set_inode, 0, BMAP_SET, 412 array_idx, &blk); 413 if (retval) { 414 com_err("set_inode", retval, "while setting block map"); 415 } 416 return retval; 417} 418 419static errcode_t parse_gd_csum(struct field_set_info *info, char *arg) 420{ 421 422 if (strcmp(arg, "calc") == 0) { 423 ext2fs_group_desc_csum_set(current_fs, set_bg); 424 memcpy(&set_gd, ext2fs_group_desc(current_fs, 425 current_fs->group_desc, 426 set_bg), 427 sizeof(set_gd)); 428 printf("Checksum set to 0x%04x\n", 429 ext2fs_bg_checksum(current_fs, set_bg)); 430 return 0; 431 } 432 433 return parse_uint(info, arg); 434} 435 436static void print_possible_fields(struct field_set_info *fields) 437{ 438 struct field_set_info *ss; 439 const char *type, *cmd; 440 FILE *f; 441 char name[40], idx[40]; 442 443 if (fields == super_fields) { 444 type = "Superblock"; 445 cmd = "set_super_value"; 446 } else if (fields == inode_fields) { 447 type = "Inode"; 448 cmd = "set_inode"; 449 } else { 450 type = "Block group descriptor"; 451 cmd = "set_block_group"; 452 } 453 f = open_pager(); 454 455 fprintf(f, "%s fields supported by the %s command:\n", type, cmd); 456 457 for (ss = fields ; ss->name ; ss++) { 458 type = "unknown"; 459 if (ss->func == parse_string) 460 type = "string"; 461 else if (ss->func == parse_int) 462 type = "integer"; 463 else if (ss->func == parse_uint) 464 type = "unsigned integer"; 465 else if (ss->func == parse_uuid) 466 type = "UUID"; 467 else if (ss->func == parse_hashalg) 468 type = "hash algorithm"; 469 else if (ss->func == parse_time) 470 type = "date/time"; 471 else if (ss->func == parse_bmap) 472 type = "set physical->logical block map"; 473 strcpy(name, ss->name); 474 if (ss->flags & FLAG_ARRAY) { 475 if (ss->max_idx > 0) 476 sprintf(idx, "[%d]", ss->max_idx); 477 else 478 strcpy(idx, "[]"); 479 strcat(name, idx); 480 } 481 fprintf(f, "\t%-20s\t%s\n", name, type); 482 } 483 close_pager(f); 484} 485 486 487void do_set_super(int argc, char *argv[]) 488{ 489 const char *usage = "<field> <value>\n" 490 "\t\"set_super_value -l\" will list the names of " 491 "superblock fields\n\twhich can be set."; 492 static struct field_set_info *ss; 493 494 if ((argc == 2) && !strcmp(argv[1], "-l")) { 495 print_possible_fields(super_fields); 496 return; 497 } 498 499 if (common_args_process(argc, argv, 3, 3, "set_super_value", 500 usage, CHECK_FS_RW)) 501 return; 502 503 if ((ss = find_field(super_fields, argv[1])) == 0) { 504 com_err(argv[0], 0, "invalid field specifier: %s", argv[1]); 505 return; 506 } 507 set_sb = *current_fs->super; 508 if (ss->func(ss, argv[2]) == 0) { 509 *current_fs->super = set_sb; 510 ext2fs_mark_super_dirty(current_fs); 511 } 512} 513 514void do_set_inode(int argc, char *argv[]) 515{ 516 const char *usage = "<inode> <field> <value>\n" 517 "\t\"set_inode_field -l\" will list the names of " 518 "the fields in an ext2 inode\n\twhich can be set."; 519 static struct field_set_info *ss; 520 521 if ((argc == 2) && !strcmp(argv[1], "-l")) { 522 print_possible_fields(inode_fields); 523 return; 524 } 525 526 if (common_args_process(argc, argv, 4, 4, "set_inode", 527 usage, CHECK_FS_RW)) 528 return; 529 530 if ((ss = find_field(inode_fields, argv[2])) == 0) { 531 com_err(argv[0], 0, "invalid field specifier: %s", argv[2]); 532 return; 533 } 534 535 set_ino = string_to_inode(argv[1]); 536 if (!set_ino) 537 return; 538 539 if (debugfs_read_inode(set_ino, &set_inode, argv[1])) 540 return; 541 542 if (ss->func(ss, argv[3]) == 0) { 543 if (debugfs_write_inode(set_ino, &set_inode, argv[1])) 544 return; 545 } 546} 547 548void do_set_block_group_descriptor(int argc, char *argv[]) 549{ 550 const char *usage = "<bg number> <field> <value>\n" 551 "\t\"set_block_group_descriptor -l\" will list the names of " 552 "the fields in a block group descriptor\n\twhich can be set."; 553 struct field_set_info *ss; 554 char *end; 555 556 if ((argc == 2) && !strcmp(argv[1], "-l")) { 557 print_possible_fields(ext2_bg_fields); 558 return; 559 } 560 561 if (common_args_process(argc, argv, 4, 4, "set_block_group_descriptor", 562 usage, CHECK_FS_RW)) 563 return; 564 565 set_bg = strtoul(argv[1], &end, 0); 566 if (*end) { 567 com_err(argv[0], 0, "invalid block group number: %s", argv[1]); 568 return; 569 } 570 571 if (set_bg >= current_fs->group_desc_count) { 572 com_err(argv[0], 0, "block group number too big: %d", set_bg); 573 return; 574 } 575 576 577 if ((ss = find_field(ext2_bg_fields, argv[2])) == 0) { 578 com_err(argv[0], 0, "invalid field specifier: %s", argv[2]); 579 return; 580 } 581 582 memcpy(&set_gd, ext2fs_group_desc(current_fs, 583 current_fs->group_desc, set_bg), 584 sizeof(set_gd)); 585 586 if (ss->func(ss, argv[3]) == 0) { 587 memcpy(ext2fs_group_desc(current_fs, 588 current_fs->group_desc, set_bg), 589 &set_gd, sizeof(set_gd)); 590 ext2fs_mark_super_dirty(current_fs); 591 } 592} 593