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