tune2fs.c revision d82445e903be682e09c41607ed5f7b338cb8c1b0
1/* 2 * tune2fs.c - Change the file system parameters on an ext2 file system 3 * 4 * Copyright (C) 1992, 1993, 1994 Remy Card <card@masi.ibp.fr> 5 * Laboratoire MASI, Institut Blaise Pascal 6 * Universite Pierre et Marie Curie (Paris VI) 7 * 8 * Copyright 1995, 1996, 1997, 1998, 1999, 2000 by Theodore Ts'o. 9 * 10 * %Begin-Header% 11 * This file may be redistributed under the terms of the GNU Public 12 * License. 13 * %End-Header% 14 */ 15 16/* 17 * History: 18 * 93/06/01 - Creation 19 * 93/10/31 - Added the -c option to change the maximal mount counts 20 * 93/12/14 - Added -l flag to list contents of superblock 21 * M.J.E. Mol (marcel@duteca.et.tudelft.nl) 22 * F.W. ten Wolde (franky@duteca.et.tudelft.nl) 23 * 93/12/29 - Added the -e option to change errors behavior 24 * 94/02/27 - Ported to use the ext2fs library 25 * 94/03/06 - Added the checks interval from Uwe Ohse (uwe@tirka.gun.de) 26 */ 27 28#define _XOPEN_SOURCE 600 /* for inclusion of strptime() */ 29#define _BSD_SOURCE /* for inclusion of strcasecmp() */ 30#include "config.h" 31#include <fcntl.h> 32#include <grp.h> 33#ifdef HAVE_GETOPT_H 34#include <getopt.h> 35#else 36extern char *optarg; 37extern int optind; 38#endif 39#include <pwd.h> 40#include <stdio.h> 41#ifdef HAVE_STDLIB_H 42#include <stdlib.h> 43#endif 44#include <string.h> 45#include <time.h> 46#include <unistd.h> 47#include <sys/types.h> 48#include <libgen.h> 49#include <limits.h> 50 51#include "ext2fs/ext2_fs.h" 52#include "ext2fs/ext2fs.h" 53#include "et/com_err.h" 54#include "uuid/uuid.h" 55#include "e2p/e2p.h" 56#include "jfs_user.h" 57#include "util.h" 58#include "blkid/blkid.h" 59#include "quota/mkquota.h" 60 61#include "../version.h" 62#include "nls-enable.h" 63 64#define QOPT_ENABLE (1) 65#define QOPT_DISABLE (-1) 66 67extern int ask_yn(const char *string, int def); 68 69const char *program_name = "tune2fs"; 70char *device_name; 71char *new_label, *new_last_mounted, *new_UUID; 72char *io_options; 73static int c_flag, C_flag, e_flag, f_flag, g_flag, i_flag, l_flag, L_flag; 74static int m_flag, M_flag, Q_flag, r_flag, s_flag = -1, u_flag, U_flag, T_flag; 75static int I_flag; 76static int clear_mmp; 77static time_t last_check_time; 78static int print_label; 79static int max_mount_count, mount_count, mount_flags; 80static unsigned long interval; 81static blk64_t reserved_blocks; 82static double reserved_ratio; 83static unsigned long resgid, resuid; 84static unsigned short errors; 85static int open_flag; 86static char *features_cmd; 87static char *mntopts_cmd; 88static int stride, stripe_width; 89static int stride_set, stripe_width_set; 90static char *extended_cmd; 91static unsigned long new_inode_size; 92static char *ext_mount_opts; 93static int usrquota, grpquota; 94 95int journal_size, journal_flags; 96char *journal_device; 97 98static struct list_head blk_move_list; 99 100struct blk_move { 101 struct list_head list; 102 blk64_t old_loc; 103 blk64_t new_loc; 104}; 105 106 107static const char *please_fsck = N_("Please run e2fsck on the filesystem.\n"); 108 109#ifdef CONFIG_BUILD_FINDFS 110void do_findfs(int argc, char **argv); 111#endif 112 113static void usage(void) 114{ 115 fprintf(stderr, 116 _("Usage: %s [-c max_mounts_count] [-e errors_behavior] " 117 "[-g group]\n" 118 "\t[-i interval[d|m|w]] [-j] [-J journal_options] [-l]\n" 119 "\t[-m reserved_blocks_percent] " 120 "[-o [^]mount_options[,...]] [-p mmp_update_interval]\n" 121 "\t[-r reserved_blocks_count] [-u user] [-C mount_count] " 122 "[-L volume_label]\n" 123 "\t[-M last_mounted_dir] [-O [^]feature[,...]]\n" 124#ifdef CONFIG_QUOTA 125 "\t[-Q quota_options]\n" 126#endif 127 "\t[-E extended-option[,...]] [-T last_check_time] " 128 "[-U UUID]\n\t[ -I new_inode_size ] device\n"), program_name); 129 exit(1); 130} 131 132static __u32 ok_features[3] = { 133 /* Compat */ 134 EXT3_FEATURE_COMPAT_HAS_JOURNAL | 135 EXT2_FEATURE_COMPAT_DIR_INDEX, 136 /* Incompat */ 137 EXT2_FEATURE_INCOMPAT_FILETYPE | 138 EXT3_FEATURE_INCOMPAT_EXTENTS | 139 EXT4_FEATURE_INCOMPAT_FLEX_BG | 140 EXT4_FEATURE_INCOMPAT_MMP, 141 /* R/O compat */ 142 EXT2_FEATURE_RO_COMPAT_LARGE_FILE | 143 EXT4_FEATURE_RO_COMPAT_HUGE_FILE| 144 EXT4_FEATURE_RO_COMPAT_DIR_NLINK| 145 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE| 146 EXT4_FEATURE_RO_COMPAT_GDT_CSUM | 147#ifdef CONFIG_QUOTA 148 EXT4_FEATURE_RO_COMPAT_QUOTA | 149#endif 150 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER 151}; 152 153static __u32 clear_ok_features[3] = { 154 /* Compat */ 155 EXT3_FEATURE_COMPAT_HAS_JOURNAL | 156 EXT2_FEATURE_COMPAT_RESIZE_INODE | 157 EXT2_FEATURE_COMPAT_DIR_INDEX, 158 /* Incompat */ 159 EXT2_FEATURE_INCOMPAT_FILETYPE | 160 EXT4_FEATURE_INCOMPAT_FLEX_BG | 161 EXT4_FEATURE_INCOMPAT_MMP, 162 /* R/O compat */ 163 EXT2_FEATURE_RO_COMPAT_LARGE_FILE | 164 EXT4_FEATURE_RO_COMPAT_HUGE_FILE| 165 EXT4_FEATURE_RO_COMPAT_DIR_NLINK| 166 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE| 167#ifdef CONFIG_QUOTA 168 EXT4_FEATURE_RO_COMPAT_QUOTA | 169#endif 170 EXT4_FEATURE_RO_COMPAT_GDT_CSUM 171}; 172 173/* 174 * Remove an external journal from the filesystem 175 */ 176static int remove_journal_device(ext2_filsys fs) 177{ 178 char *journal_path; 179 ext2_filsys jfs; 180 char buf[1024]; 181 journal_superblock_t *jsb; 182 int i, nr_users; 183 errcode_t retval; 184 int commit_remove_journal = 0; 185 io_manager io_ptr; 186 187 if (f_flag) 188 commit_remove_journal = 1; /* force removal even if error */ 189 190 uuid_unparse(fs->super->s_journal_uuid, buf); 191 journal_path = blkid_get_devname(NULL, "UUID", buf); 192 193 if (!journal_path) { 194 journal_path = 195 ext2fs_find_block_device(fs->super->s_journal_dev); 196 if (!journal_path) 197 goto no_valid_journal; 198 } 199 200#ifdef CONFIG_TESTIO_DEBUG 201 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) { 202 io_ptr = test_io_manager; 203 test_io_backing_manager = unix_io_manager; 204 } else 205#endif 206 io_ptr = unix_io_manager; 207 retval = ext2fs_open(journal_path, EXT2_FLAG_RW| 208 EXT2_FLAG_JOURNAL_DEV_OK, 0, 209 fs->blocksize, io_ptr, &jfs); 210 if (retval) { 211 com_err(program_name, retval, 212 _("while trying to open external journal")); 213 goto no_valid_journal; 214 } 215 if (!(jfs->super->s_feature_incompat & 216 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) { 217 fprintf(stderr, _("%s is not a journal device.\n"), 218 journal_path); 219 goto no_valid_journal; 220 } 221 222 /* Get the journal superblock */ 223 if ((retval = io_channel_read_blk64(jfs->io, 1, -1024, buf))) { 224 com_err(program_name, retval, 225 _("while reading journal superblock")); 226 goto no_valid_journal; 227 } 228 229 jsb = (journal_superblock_t *) buf; 230 if ((jsb->s_header.h_magic != (unsigned)ntohl(JFS_MAGIC_NUMBER)) || 231 (jsb->s_header.h_blocktype != (unsigned)ntohl(JFS_SUPERBLOCK_V2))) { 232 fputs(_("Journal superblock not found!\n"), stderr); 233 goto no_valid_journal; 234 } 235 236 /* Find the filesystem UUID */ 237 nr_users = ntohl(jsb->s_nr_users); 238 for (i = 0; i < nr_users; i++) { 239 if (memcmp(fs->super->s_uuid, &jsb->s_users[i * 16], 16) == 0) 240 break; 241 } 242 if (i >= nr_users) { 243 fputs(_("Filesystem's UUID not found on journal device.\n"), 244 stderr); 245 commit_remove_journal = 1; 246 goto no_valid_journal; 247 } 248 nr_users--; 249 for (i = 0; i < nr_users; i++) 250 memcpy(&jsb->s_users[i * 16], &jsb->s_users[(i + 1) * 16], 16); 251 jsb->s_nr_users = htonl(nr_users); 252 253 /* Write back the journal superblock */ 254 if ((retval = io_channel_write_blk64(jfs->io, 1, -1024, buf))) { 255 com_err(program_name, retval, 256 "while writing journal superblock."); 257 goto no_valid_journal; 258 } 259 260 commit_remove_journal = 1; 261 262no_valid_journal: 263 if (commit_remove_journal == 0) { 264 fputs(_("Cannot locate journal device. It was NOT removed\n" 265 "Use -f option to remove missing journal device.\n"), 266 stderr); 267 return 1; 268 } 269 fs->super->s_journal_dev = 0; 270 uuid_clear(fs->super->s_journal_uuid); 271 ext2fs_mark_super_dirty(fs); 272 fputs(_("Journal removed\n"), stdout); 273 free(journal_path); 274 275 return 0; 276} 277 278/* Helper function for remove_journal_inode */ 279static int release_blocks_proc(ext2_filsys fs, blk64_t *blocknr, 280 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)), 281 blk64_t ref_block EXT2FS_ATTR((unused)), 282 int ref_offset EXT2FS_ATTR((unused)), 283 void *private EXT2FS_ATTR((unused))) 284{ 285 blk64_t block; 286 int group; 287 288 block = *blocknr; 289 ext2fs_unmark_block_bitmap2(fs->block_map, block); 290 group = ext2fs_group_of_blk2(fs, block); 291 ext2fs_bg_free_blocks_count_set(fs, group, ext2fs_bg_free_blocks_count(fs, group) + 1); 292 ext2fs_group_desc_csum_set(fs, group); 293 ext2fs_free_blocks_count_add(fs->super, EXT2FS_CLUSTER_RATIO(fs)); 294 return 0; 295} 296 297/* 298 * Remove the journal inode from the filesystem 299 */ 300static errcode_t remove_journal_inode(ext2_filsys fs) 301{ 302 struct ext2_inode inode; 303 errcode_t retval; 304 ino_t ino = fs->super->s_journal_inum; 305 306 retval = ext2fs_read_inode(fs, ino, &inode); 307 if (retval) { 308 com_err(program_name, retval, 309 _("while reading journal inode")); 310 return retval; 311 } 312 if (ino == EXT2_JOURNAL_INO) { 313 retval = ext2fs_read_bitmaps(fs); 314 if (retval) { 315 com_err(program_name, retval, 316 _("while reading bitmaps")); 317 return retval; 318 } 319 retval = ext2fs_block_iterate3(fs, ino, 320 BLOCK_FLAG_READ_ONLY, NULL, 321 release_blocks_proc, NULL); 322 if (retval) { 323 com_err(program_name, retval, 324 _("while clearing journal inode")); 325 return retval; 326 } 327 memset(&inode, 0, sizeof(inode)); 328 ext2fs_mark_bb_dirty(fs); 329 fs->flags &= ~EXT2_FLAG_SUPER_ONLY; 330 } else 331 inode.i_flags &= ~EXT2_IMMUTABLE_FL; 332 retval = ext2fs_write_inode(fs, ino, &inode); 333 if (retval) { 334 com_err(program_name, retval, 335 _("while writing journal inode")); 336 return retval; 337 } 338 fs->super->s_journal_inum = 0; 339 ext2fs_mark_super_dirty(fs); 340 341 return 0; 342} 343 344/* 345 * Update the default mount options 346 */ 347static int update_mntopts(ext2_filsys fs, char *mntopts) 348{ 349 struct ext2_super_block *sb = fs->super; 350 351 if (e2p_edit_mntopts(mntopts, &sb->s_default_mount_opts, ~0)) { 352 fprintf(stderr, _("Invalid mount option set: %s\n"), 353 mntopts); 354 return 1; 355 } 356 ext2fs_mark_super_dirty(fs); 357 358 return 0; 359} 360 361static void request_fsck_afterwards(ext2_filsys fs) 362{ 363 static int requested = 0; 364 365 if (requested++) 366 return; 367 fs->super->s_state &= ~EXT2_VALID_FS; 368 printf("\n%s\n", _(please_fsck)); 369 if (mount_flags & EXT2_MF_READONLY) 370 printf(_("(and reboot afterwards!)\n")); 371} 372 373/* 374 * Update the feature set as provided by the user. 375 */ 376static int update_feature_set(ext2_filsys fs, char *features) 377{ 378 struct ext2_super_block *sb = fs->super; 379 struct ext2_group_desc *gd; 380 __u32 old_features[3]; 381 dgrp_t i; 382 int type_err; 383 unsigned int mask_err; 384 385#define FEATURE_ON(type, mask) (!(old_features[(type)] & (mask)) && \ 386 ((&sb->s_feature_compat)[(type)] & (mask))) 387#define FEATURE_OFF(type, mask) ((old_features[(type)] & (mask)) && \ 388 !((&sb->s_feature_compat)[(type)] & (mask))) 389#define FEATURE_CHANGED(type, mask) ((mask) & \ 390 (old_features[(type)] ^ (&sb->s_feature_compat)[(type)])) 391 392 old_features[E2P_FEATURE_COMPAT] = sb->s_feature_compat; 393 old_features[E2P_FEATURE_INCOMPAT] = sb->s_feature_incompat; 394 old_features[E2P_FEATURE_RO_INCOMPAT] = sb->s_feature_ro_compat; 395 396 if (e2p_edit_feature2(features, &sb->s_feature_compat, 397 ok_features, clear_ok_features, 398 &type_err, &mask_err)) { 399 if (!mask_err) 400 fprintf(stderr, 401 _("Invalid filesystem option set: %s\n"), 402 features); 403 else if (type_err & E2P_FEATURE_NEGATE_FLAG) 404 fprintf(stderr, _("Clearing filesystem feature '%s' " 405 "not supported.\n"), 406 e2p_feature2string(type_err & 407 E2P_FEATURE_TYPE_MASK, 408 mask_err)); 409 else 410 fprintf(stderr, _("Setting filesystem feature '%s' " 411 "not supported.\n"), 412 e2p_feature2string(type_err, mask_err)); 413 return 1; 414 } 415 416 if (FEATURE_OFF(E2P_FEATURE_COMPAT, EXT3_FEATURE_COMPAT_HAS_JOURNAL)) { 417 if ((mount_flags & EXT2_MF_MOUNTED) && 418 !(mount_flags & EXT2_MF_READONLY)) { 419 fputs(_("The has_journal feature may only be " 420 "cleared when the filesystem is\n" 421 "unmounted or mounted " 422 "read-only.\n"), stderr); 423 return 1; 424 } 425 if (sb->s_feature_incompat & 426 EXT3_FEATURE_INCOMPAT_RECOVER) { 427 fputs(_("The needs_recovery flag is set. " 428 "Please run e2fsck before clearing\n" 429 "the has_journal flag.\n"), stderr); 430 return 1; 431 } 432 if (sb->s_journal_inum) { 433 if (remove_journal_inode(fs)) 434 return 1; 435 } 436 if (sb->s_journal_dev) { 437 if (remove_journal_device(fs)) 438 return 1; 439 } 440 } 441 if (FEATURE_ON(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_MMP)) { 442 int error; 443 444 if ((mount_flags & EXT2_MF_MOUNTED) || 445 (mount_flags & EXT2_MF_READONLY)) { 446 fputs(_("The multiple mount protection feature can't\n" 447 "be set if the filesystem is mounted or\n" 448 "read-only.\n"), stderr); 449 return 1; 450 } 451 452 error = ext2fs_mmp_init(fs); 453 if (error) { 454 fputs(_("\nError while enabling multiple mount " 455 "protection feature."), stderr); 456 return 1; 457 } 458 459 /* 460 * We want to update group desc with the new free blocks count 461 */ 462 fs->flags &= ~EXT2_FLAG_SUPER_ONLY; 463 464 printf(_("Multiple mount protection has been enabled " 465 "with update interval %ds.\n"), 466 sb->s_mmp_update_interval); 467 } 468 469 if (FEATURE_OFF(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_MMP)) { 470 int error; 471 472 if (mount_flags & EXT2_MF_READONLY) { 473 fputs(_("The multiple mount protection feature cannot\n" 474 "be disabled if the filesystem is readonly.\n"), 475 stderr); 476 return 1; 477 } 478 479 error = ext2fs_read_bitmaps(fs); 480 if (error) { 481 fputs(_("Error while reading bitmaps\n"), stderr); 482 return 1; 483 } 484 485 error = ext2fs_mmp_read(fs, sb->s_mmp_block, NULL); 486 if (error) { 487 struct mmp_struct *mmp_cmp = fs->mmp_cmp; 488 489 if (error == EXT2_ET_MMP_MAGIC_INVALID) 490 printf(_("Magic number in MMP block does not " 491 "match. expected: %x, actual: %x\n"), 492 EXT4_MMP_MAGIC, mmp_cmp->mmp_magic); 493 else 494 com_err(program_name, error, 495 _("while reading MMP block.")); 496 goto mmp_error; 497 } 498 499 /* We need to force out the group descriptors as well */ 500 fs->flags &= ~EXT2_FLAG_SUPER_ONLY; 501 ext2fs_block_alloc_stats2(fs, sb->s_mmp_block, -1); 502mmp_error: 503 sb->s_mmp_block = 0; 504 sb->s_mmp_update_interval = 0; 505 } 506 507 if (FEATURE_ON(E2P_FEATURE_COMPAT, EXT3_FEATURE_COMPAT_HAS_JOURNAL)) { 508 /* 509 * If adding a journal flag, let the create journal 510 * code below handle setting the flag and creating the 511 * journal. We supply a default size if necessary. 512 */ 513 if (!journal_size) 514 journal_size = -1; 515 sb->s_feature_compat &= ~EXT3_FEATURE_COMPAT_HAS_JOURNAL; 516 } 517 518 if (FEATURE_ON(E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_DIR_INDEX)) { 519 if (!sb->s_def_hash_version) 520 sb->s_def_hash_version = EXT2_HASH_HALF_MD4; 521 if (uuid_is_null((unsigned char *) sb->s_hash_seed)) 522 uuid_generate((unsigned char *) sb->s_hash_seed); 523 } 524 525 if (FEATURE_OFF(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_FLEX_BG)) { 526 if (ext2fs_check_desc(fs)) { 527 fputs(_("Clearing the flex_bg flag would " 528 "cause the the filesystem to be\n" 529 "inconsistent.\n"), stderr); 530 return 1; 531 } 532 } 533 534 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT, 535 EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) { 536 if ((mount_flags & EXT2_MF_MOUNTED) && 537 !(mount_flags & EXT2_MF_READONLY)) { 538 fputs(_("The huge_file feature may only be " 539 "cleared when the filesystem is\n" 540 "unmounted or mounted " 541 "read-only.\n"), stderr); 542 return 1; 543 } 544 } 545 546 if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT, 547 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) { 548 for (i = 0; i < fs->group_desc_count; i++) { 549 gd = ext2fs_group_desc(fs, fs->group_desc, i); 550 gd->bg_itable_unused = 0; 551 gd->bg_flags = EXT2_BG_INODE_ZEROED; 552 ext2fs_group_desc_csum_set(fs, i); 553 } 554 fs->flags &= ~EXT2_FLAG_SUPER_ONLY; 555 } 556 557 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT, 558 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) { 559 for (i = 0; i < fs->group_desc_count; i++) { 560 gd = ext2fs_group_desc(fs, fs->group_desc, i); 561 if ((gd->bg_flags & EXT2_BG_INODE_ZEROED) == 0) { 562 /* 563 * XXX what we really should do is zap 564 * uninitialized inode tables instead. 565 */ 566 request_fsck_afterwards(fs); 567 break; 568 } 569 gd->bg_itable_unused = 0; 570 gd->bg_flags = 0; 571 gd->bg_checksum = 0; 572 } 573 fs->flags &= ~EXT2_FLAG_SUPER_ONLY; 574 } 575 576 if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT, 577 EXT4_FEATURE_RO_COMPAT_QUOTA)) { 578 /* 579 * Set the Q_flag here and handle the quota options in the code 580 * below. 581 */ 582 if (!Q_flag) { 583 Q_flag = 1; 584 /* Enable both user quota and group quota by default */ 585 usrquota = QOPT_ENABLE; 586 grpquota = QOPT_ENABLE; 587 } 588 sb->s_feature_ro_compat &= ~EXT4_FEATURE_RO_COMPAT_QUOTA; 589 } 590 591 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT, 592 EXT4_FEATURE_RO_COMPAT_QUOTA)) { 593 /* 594 * Set the Q_flag here and handle the quota options in the code 595 * below. 596 */ 597 if (Q_flag) 598 fputs(_("\nWarning: '^quota' option overrides '-Q'" 599 "arguments.\n"), stderr); 600 Q_flag = 1; 601 /* Disable both user quota and group quota by default */ 602 usrquota = QOPT_DISABLE; 603 grpquota = QOPT_DISABLE; 604 } 605 606 if (sb->s_rev_level == EXT2_GOOD_OLD_REV && 607 (sb->s_feature_compat || sb->s_feature_ro_compat || 608 sb->s_feature_incompat)) 609 ext2fs_update_dynamic_rev(fs); 610 611 if (FEATURE_CHANGED(E2P_FEATURE_RO_INCOMPAT, 612 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER) || 613 FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT, 614 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) || 615 FEATURE_CHANGED(E2P_FEATURE_INCOMPAT, 616 EXT2_FEATURE_INCOMPAT_FILETYPE) || 617 FEATURE_CHANGED(E2P_FEATURE_COMPAT, 618 EXT2_FEATURE_COMPAT_RESIZE_INODE) || 619 FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT, 620 EXT2_FEATURE_RO_COMPAT_LARGE_FILE)) 621 request_fsck_afterwards(fs); 622 623 if ((old_features[E2P_FEATURE_COMPAT] != sb->s_feature_compat) || 624 (old_features[E2P_FEATURE_INCOMPAT] != sb->s_feature_incompat) || 625 (old_features[E2P_FEATURE_RO_INCOMPAT] != sb->s_feature_ro_compat)) 626 ext2fs_mark_super_dirty(fs); 627 628 return 0; 629} 630 631/* 632 * Add a journal to the filesystem. 633 */ 634static int add_journal(ext2_filsys fs) 635{ 636 unsigned long journal_blocks; 637 errcode_t retval; 638 ext2_filsys jfs; 639 io_manager io_ptr; 640 641 if (fs->super->s_feature_compat & 642 EXT3_FEATURE_COMPAT_HAS_JOURNAL) { 643 fputs(_("The filesystem already has a journal.\n"), stderr); 644 goto err; 645 } 646 if (journal_device) { 647 check_plausibility(journal_device); 648 check_mount(journal_device, 0, _("journal")); 649#ifdef CONFIG_TESTIO_DEBUG 650 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) { 651 io_ptr = test_io_manager; 652 test_io_backing_manager = unix_io_manager; 653 } else 654#endif 655 io_ptr = unix_io_manager; 656 retval = ext2fs_open(journal_device, EXT2_FLAG_RW| 657 EXT2_FLAG_JOURNAL_DEV_OK, 0, 658 fs->blocksize, io_ptr, &jfs); 659 if (retval) { 660 com_err(program_name, retval, 661 _("\n\twhile trying to open journal on %s\n"), 662 journal_device); 663 goto err; 664 } 665 printf(_("Creating journal on device %s: "), 666 journal_device); 667 fflush(stdout); 668 669 retval = ext2fs_add_journal_device(fs, jfs); 670 ext2fs_close(jfs); 671 if (retval) { 672 com_err(program_name, retval, 673 _("while adding filesystem to journal on %s"), 674 journal_device); 675 goto err; 676 } 677 fputs(_("done\n"), stdout); 678 } else if (journal_size) { 679 fputs(_("Creating journal inode: "), stdout); 680 fflush(stdout); 681 journal_blocks = figure_journal_size(journal_size, fs); 682 683 retval = ext2fs_add_journal_inode(fs, journal_blocks, 684 journal_flags); 685 if (retval) { 686 fprintf(stderr, "\n"); 687 com_err(program_name, retval, 688 _("\n\twhile trying to create journal file")); 689 return retval; 690 } else 691 fputs(_("done\n"), stdout); 692 /* 693 * If the filesystem wasn't mounted, we need to force 694 * the block group descriptors out. 695 */ 696 if ((mount_flags & EXT2_MF_MOUNTED) == 0) 697 fs->flags &= ~EXT2_FLAG_SUPER_ONLY; 698 } 699 print_check_message(fs->super->s_max_mnt_count, 700 fs->super->s_checkinterval); 701 return 0; 702 703err: 704 free(journal_device); 705 return 1; 706} 707 708static void handle_quota_options(ext2_filsys fs) 709{ 710 quota_ctx_t qctx; 711 ext2_ino_t qf_ino; 712 713 if (!usrquota && !grpquota) 714 /* Nothing to do. */ 715 return; 716 717 quota_init_context(&qctx, fs, -1); 718 719 if (usrquota == QOPT_ENABLE || grpquota == QOPT_ENABLE) 720 quota_compute_usage(qctx); 721 722 if (usrquota == QOPT_ENABLE && !fs->super->s_usr_quota_inum) { 723 if ((qf_ino = quota_file_exists(fs, USRQUOTA, 724 QFMT_VFS_V1)) > 0) 725 quota_update_limits(qctx, qf_ino, USRQUOTA); 726 quota_write_inode(qctx, USRQUOTA); 727 } else if (usrquota == QOPT_DISABLE) { 728 quota_remove_inode(fs, USRQUOTA); 729 } 730 731 if (grpquota == QOPT_ENABLE && !fs->super->s_grp_quota_inum) { 732 if ((qf_ino = quota_file_exists(fs, GRPQUOTA, 733 QFMT_VFS_V1)) > 0) 734 quota_update_limits(qctx, qf_ino, GRPQUOTA); 735 quota_write_inode(qctx, GRPQUOTA); 736 } else if (grpquota == QOPT_DISABLE) { 737 quota_remove_inode(fs, GRPQUOTA); 738 } 739 740 quota_release_context(&qctx); 741 742 if ((usrquota == QOPT_ENABLE) || (grpquota == QOPT_ENABLE)) { 743 fprintf(stderr, _("\nWarning: the quota feature is still " 744 "under development\n" 745 "See https://ext4.wiki.kernel.org/" 746 "index.php/Quota for more information\n\n")); 747 fs->super->s_feature_ro_compat |= EXT4_FEATURE_RO_COMPAT_QUOTA; 748 ext2fs_mark_super_dirty(fs); 749 } else if (!fs->super->s_usr_quota_inum && 750 !fs->super->s_grp_quota_inum) { 751 fs->super->s_feature_ro_compat &= ~EXT4_FEATURE_RO_COMPAT_QUOTA; 752 ext2fs_mark_super_dirty(fs); 753 } 754 755 return; 756} 757 758static void parse_quota_opts(const char *opts) 759{ 760 char *buf, *token, *next, *p; 761 int len; 762 763 len = strlen(opts); 764 buf = malloc(len+1); 765 if (!buf) { 766 fputs(_("Couldn't allocate memory to parse quota " 767 "options!\n"), stderr); 768 exit(1); 769 } 770 strcpy(buf, opts); 771 for (token = buf; token && *token; token = next) { 772 p = strchr(token, ','); 773 next = 0; 774 if (p) { 775 *p = 0; 776 next = p+1; 777 } 778 779 if (strcmp(token, "usrquota") == 0) { 780 usrquota = QOPT_ENABLE; 781 } else if (strcmp(token, "^usrquota") == 0) { 782 usrquota = QOPT_DISABLE; 783 } else if (strcmp(token, "grpquota") == 0) { 784 grpquota = QOPT_ENABLE; 785 } else if (strcmp(token, "^grpquota") == 0) { 786 grpquota = QOPT_DISABLE; 787 } else { 788 fputs(_("\nBad quota options specified.\n\n" 789 "Following valid quota options are available " 790 "(pass by separating with comma):\n" 791 "\t[^]usrquota\n" 792 "\t[^]grpquota\n" 793 "\n\n"), stderr); 794 free(buf); 795 exit(1); 796 } 797 } 798 free(buf); 799} 800 801 802 803static void parse_e2label_options(int argc, char ** argv) 804{ 805 if ((argc < 2) || (argc > 3)) { 806 fputs(_("Usage: e2label device [newlabel]\n"), stderr); 807 exit(1); 808 } 809 io_options = strchr(argv[1], '?'); 810 if (io_options) 811 *io_options++ = 0; 812 device_name = blkid_get_devname(NULL, argv[1], NULL); 813 if (!device_name) { 814 com_err("e2label", 0, _("Unable to resolve '%s'"), 815 argv[1]); 816 exit(1); 817 } 818 open_flag = EXT2_FLAG_JOURNAL_DEV_OK; 819 if (argc == 3) { 820 open_flag |= EXT2_FLAG_RW; 821 L_flag = 1; 822 new_label = argv[2]; 823 } else 824 print_label++; 825} 826 827static time_t parse_time(char *str) 828{ 829 struct tm ts; 830 831 if (strcmp(str, "now") == 0) { 832 return (time(0)); 833 } 834 memset(&ts, 0, sizeof(ts)); 835#ifdef HAVE_STRPTIME 836 strptime(str, "%Y%m%d%H%M%S", &ts); 837#else 838 sscanf(str, "%4d%2d%2d%2d%2d%2d", &ts.tm_year, &ts.tm_mon, 839 &ts.tm_mday, &ts.tm_hour, &ts.tm_min, &ts.tm_sec); 840 ts.tm_year -= 1900; 841 ts.tm_mon -= 1; 842 if (ts.tm_year < 0 || ts.tm_mon < 0 || ts.tm_mon > 11 || 843 ts.tm_mday < 0 || ts.tm_mday > 31 || ts.tm_hour > 23 || 844 ts.tm_min > 59 || ts.tm_sec > 61) 845 ts.tm_mday = 0; 846#endif 847 if (ts.tm_mday == 0) { 848 com_err(program_name, 0, 849 _("Couldn't parse date/time specifier: %s"), 850 str); 851 usage(); 852 } 853 ts.tm_isdst = -1; 854 return (mktime(&ts)); 855} 856 857static void parse_tune2fs_options(int argc, char **argv) 858{ 859 int c; 860 char *tmp; 861 struct group *gr; 862 struct passwd *pw; 863 char optstring[100] = "c:e:fg:i:jlm:o:r:s:u:C:E:I:J:L:M:O:T:U:"; 864 865#ifdef CONFIG_QUOTA 866 strcat(optstring, "Q:"); 867#endif 868 open_flag = 0; 869 870 printf("tune2fs %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE); 871 while ((c = getopt(argc, argv, optstring)) != EOF) 872 switch (c) { 873 case 'c': 874 max_mount_count = strtol(optarg, &tmp, 0); 875 if (*tmp || max_mount_count > 16000) { 876 com_err(program_name, 0, 877 _("bad mounts count - %s"), 878 optarg); 879 usage(); 880 } 881 if (max_mount_count == 0) 882 max_mount_count = -1; 883 c_flag = 1; 884 open_flag = EXT2_FLAG_RW; 885 break; 886 case 'C': 887 mount_count = strtoul(optarg, &tmp, 0); 888 if (*tmp || mount_count > 16000) { 889 com_err(program_name, 0, 890 _("bad mounts count - %s"), 891 optarg); 892 usage(); 893 } 894 C_flag = 1; 895 open_flag = EXT2_FLAG_RW; 896 break; 897 case 'e': 898 if (strcmp(optarg, "continue") == 0) 899 errors = EXT2_ERRORS_CONTINUE; 900 else if (strcmp(optarg, "remount-ro") == 0) 901 errors = EXT2_ERRORS_RO; 902 else if (strcmp(optarg, "panic") == 0) 903 errors = EXT2_ERRORS_PANIC; 904 else { 905 com_err(program_name, 0, 906 _("bad error behavior - %s"), 907 optarg); 908 usage(); 909 } 910 e_flag = 1; 911 open_flag = EXT2_FLAG_RW; 912 break; 913 case 'E': 914 extended_cmd = optarg; 915 open_flag |= EXT2_FLAG_RW; 916 break; 917 case 'f': /* Force */ 918 f_flag = 1; 919 break; 920 case 'g': 921 resgid = strtoul(optarg, &tmp, 0); 922 if (*tmp) { 923 gr = getgrnam(optarg); 924 if (gr == NULL) 925 tmp = optarg; 926 else { 927 resgid = gr->gr_gid; 928 *tmp = 0; 929 } 930 } 931 if (*tmp) { 932 com_err(program_name, 0, 933 _("bad gid/group name - %s"), 934 optarg); 935 usage(); 936 } 937 g_flag = 1; 938 open_flag = EXT2_FLAG_RW; 939 break; 940 case 'i': 941 interval = strtoul(optarg, &tmp, 0); 942 switch (*tmp) { 943 case 's': 944 tmp++; 945 break; 946 case '\0': 947 case 'd': 948 case 'D': /* days */ 949 interval *= 86400; 950 if (*tmp != '\0') 951 tmp++; 952 break; 953 case 'm': 954 case 'M': /* months! */ 955 interval *= 86400 * 30; 956 tmp++; 957 break; 958 case 'w': 959 case 'W': /* weeks */ 960 interval *= 86400 * 7; 961 tmp++; 962 break; 963 } 964 if (*tmp) { 965 com_err(program_name, 0, 966 _("bad interval - %s"), optarg); 967 usage(); 968 } 969 i_flag = 1; 970 open_flag = EXT2_FLAG_RW; 971 break; 972 case 'j': 973 if (!journal_size) 974 journal_size = -1; 975 open_flag = EXT2_FLAG_RW; 976 break; 977 case 'J': 978 parse_journal_opts(optarg); 979 open_flag = EXT2_FLAG_RW; 980 break; 981 case 'l': 982 l_flag = 1; 983 break; 984 case 'L': 985 new_label = optarg; 986 L_flag = 1; 987 open_flag |= EXT2_FLAG_RW | 988 EXT2_FLAG_JOURNAL_DEV_OK; 989 break; 990 case 'm': 991 reserved_ratio = strtod(optarg, &tmp); 992 if (*tmp || reserved_ratio > 50 || 993 reserved_ratio < 0) { 994 com_err(program_name, 0, 995 _("bad reserved block ratio - %s"), 996 optarg); 997 usage(); 998 } 999 m_flag = 1; 1000 open_flag = EXT2_FLAG_RW; 1001 break; 1002 case 'M': 1003 new_last_mounted = optarg; 1004 M_flag = 1; 1005 open_flag = EXT2_FLAG_RW; 1006 break; 1007 case 'o': 1008 if (mntopts_cmd) { 1009 com_err(program_name, 0, 1010 _("-o may only be specified once")); 1011 usage(); 1012 } 1013 mntopts_cmd = optarg; 1014 open_flag = EXT2_FLAG_RW; 1015 break; 1016 case 'O': 1017 if (features_cmd) { 1018 com_err(program_name, 0, 1019 _("-O may only be specified once")); 1020 usage(); 1021 } 1022 features_cmd = optarg; 1023 open_flag = EXT2_FLAG_RW; 1024 break; 1025#ifdef CONFIG_QUOTA 1026 case 'Q': 1027 Q_flag = 1; 1028 parse_quota_opts(optarg); 1029 open_flag = EXT2_FLAG_RW; 1030 break; 1031#endif 1032 case 'r': 1033 reserved_blocks = strtoul(optarg, &tmp, 0); 1034 if (*tmp) { 1035 com_err(program_name, 0, 1036 _("bad reserved blocks count - %s"), 1037 optarg); 1038 usage(); 1039 } 1040 r_flag = 1; 1041 open_flag = EXT2_FLAG_RW; 1042 break; 1043 case 's': /* Deprecated */ 1044 s_flag = atoi(optarg); 1045 open_flag = EXT2_FLAG_RW; 1046 break; 1047 case 'T': 1048 T_flag = 1; 1049 last_check_time = parse_time(optarg); 1050 open_flag = EXT2_FLAG_RW; 1051 break; 1052 case 'u': 1053 resuid = strtoul(optarg, &tmp, 0); 1054 if (*tmp) { 1055 pw = getpwnam(optarg); 1056 if (pw == NULL) 1057 tmp = optarg; 1058 else { 1059 resuid = pw->pw_uid; 1060 *tmp = 0; 1061 } 1062 } 1063 if (*tmp) { 1064 com_err(program_name, 0, 1065 _("bad uid/user name - %s"), 1066 optarg); 1067 usage(); 1068 } 1069 u_flag = 1; 1070 open_flag = EXT2_FLAG_RW; 1071 break; 1072 case 'U': 1073 new_UUID = optarg; 1074 U_flag = 1; 1075 open_flag = EXT2_FLAG_RW | 1076 EXT2_FLAG_JOURNAL_DEV_OK; 1077 break; 1078 case 'I': 1079 new_inode_size = strtoul(optarg, &tmp, 0); 1080 if (*tmp) { 1081 com_err(program_name, 0, 1082 _("bad inode size - %s"), 1083 optarg); 1084 usage(); 1085 } 1086 if (!((new_inode_size & 1087 (new_inode_size - 1)) == 0)) { 1088 com_err(program_name, 0, 1089 _("Inode size must be a " 1090 "power of two- %s"), 1091 optarg); 1092 usage(); 1093 } 1094 open_flag = EXT2_FLAG_RW; 1095 I_flag = 1; 1096 break; 1097 default: 1098 usage(); 1099 } 1100 if (optind < argc - 1 || optind == argc) 1101 usage(); 1102 if (!open_flag && !l_flag) 1103 usage(); 1104 io_options = strchr(argv[optind], '?'); 1105 if (io_options) 1106 *io_options++ = 0; 1107 device_name = blkid_get_devname(NULL, argv[optind], NULL); 1108 if (!device_name) { 1109 com_err(program_name, 0, _("Unable to resolve '%s'"), 1110 argv[optind]); 1111 exit(1); 1112 } 1113} 1114 1115#ifdef CONFIG_BUILD_FINDFS 1116void do_findfs(int argc, char **argv) 1117{ 1118 char *dev; 1119 1120 if ((argc != 2) || 1121 (strncmp(argv[1], "LABEL=", 6) && strncmp(argv[1], "UUID=", 5))) { 1122 fprintf(stderr, "Usage: findfs LABEL=<label>|UUID=<uuid>\n"); 1123 exit(2); 1124 } 1125 dev = blkid_get_devname(NULL, argv[1], NULL); 1126 if (!dev) { 1127 com_err("findfs", 0, _("Unable to resolve '%s'"), 1128 argv[1]); 1129 exit(1); 1130 } 1131 puts(dev); 1132 exit(0); 1133} 1134#endif 1135 1136static int parse_extended_opts(ext2_filsys fs, const char *opts) 1137{ 1138 char *buf, *token, *next, *p, *arg; 1139 int len, hash_alg; 1140 int r_usage = 0; 1141 1142 len = strlen(opts); 1143 buf = malloc(len+1); 1144 if (!buf) { 1145 fprintf(stderr, 1146 _("Couldn't allocate memory to parse options!\n")); 1147 return 1; 1148 } 1149 strcpy(buf, opts); 1150 for (token = buf; token && *token; token = next) { 1151 p = strchr(token, ','); 1152 next = 0; 1153 if (p) { 1154 *p = 0; 1155 next = p+1; 1156 } 1157 arg = strchr(token, '='); 1158 if (arg) { 1159 *arg = 0; 1160 arg++; 1161 } 1162 if (strcmp(token, "clear-mmp") == 0 || 1163 strcmp(token, "clear_mmp") == 0) { 1164 clear_mmp = 1; 1165 } else if (strcmp(token, "mmp_update_interval") == 0) { 1166 unsigned long intv; 1167 if (!arg) { 1168 r_usage++; 1169 continue; 1170 } 1171 intv = strtoul(arg, &p, 0); 1172 if (*p) { 1173 fprintf(stderr, 1174 _("Invalid mmp_update_interval: %s\n"), 1175 arg); 1176 r_usage++; 1177 continue; 1178 } 1179 if (intv == 0) { 1180 intv = EXT4_MMP_UPDATE_INTERVAL; 1181 } else if (intv > EXT4_MMP_MAX_UPDATE_INTERVAL) { 1182 fprintf(stderr, 1183 _("mmp_update_interval too big: %lu\n"), 1184 intv); 1185 r_usage++; 1186 continue; 1187 } 1188 printf(P_("Setting multiple mount protection update " 1189 "interval to %lu second\n", 1190 "Setting multiple mount protection update " 1191 "interval to %lu seconds\n", intv), 1192 intv); 1193 fs->super->s_mmp_update_interval = intv; 1194 ext2fs_mark_super_dirty(fs); 1195 } else if (!strcmp(token, "test_fs")) { 1196 fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS; 1197 printf("Setting test filesystem flag\n"); 1198 ext2fs_mark_super_dirty(fs); 1199 } else if (!strcmp(token, "^test_fs")) { 1200 fs->super->s_flags &= ~EXT2_FLAGS_TEST_FILESYS; 1201 printf("Clearing test filesystem flag\n"); 1202 ext2fs_mark_super_dirty(fs); 1203 } else if (strcmp(token, "stride") == 0) { 1204 if (!arg) { 1205 r_usage++; 1206 continue; 1207 } 1208 stride = strtoul(arg, &p, 0); 1209 if (*p) { 1210 fprintf(stderr, 1211 _("Invalid RAID stride: %s\n"), 1212 arg); 1213 r_usage++; 1214 continue; 1215 } 1216 stride_set = 1; 1217 } else if (strcmp(token, "stripe-width") == 0 || 1218 strcmp(token, "stripe_width") == 0) { 1219 if (!arg) { 1220 r_usage++; 1221 continue; 1222 } 1223 stripe_width = strtoul(arg, &p, 0); 1224 if (*p) { 1225 fprintf(stderr, 1226 _("Invalid RAID stripe-width: %s\n"), 1227 arg); 1228 r_usage++; 1229 continue; 1230 } 1231 stripe_width_set = 1; 1232 } else if (strcmp(token, "hash_alg") == 0 || 1233 strcmp(token, "hash-alg") == 0) { 1234 if (!arg) { 1235 r_usage++; 1236 continue; 1237 } 1238 hash_alg = e2p_string2hash(arg); 1239 if (hash_alg < 0) { 1240 fprintf(stderr, 1241 _("Invalid hash algorithm: %s\n"), 1242 arg); 1243 r_usage++; 1244 continue; 1245 } 1246 fs->super->s_def_hash_version = hash_alg; 1247 printf(_("Setting default hash algorithm " 1248 "to %s (%d)\n"), 1249 arg, hash_alg); 1250 ext2fs_mark_super_dirty(fs); 1251 } else if (!strcmp(token, "mount_opts")) { 1252 if (!arg) { 1253 r_usage++; 1254 continue; 1255 } 1256 if (strlen(arg) >= sizeof(fs->super->s_mount_opts)) { 1257 fprintf(stderr, 1258 "Extended mount options too long\n"); 1259 continue; 1260 } 1261 ext_mount_opts = strdup(arg); 1262 } else 1263 r_usage++; 1264 } 1265 if (r_usage) { 1266 fprintf(stderr, _("\nBad options specified.\n\n" 1267 "Extended options are separated by commas, " 1268 "and may take an argument which\n" 1269 "\tis set off by an equals ('=') sign.\n\n" 1270 "Valid extended options are:\n" 1271 "\tclear_mmp\n" 1272 "\thash_alg=<hash algorithm>\n" 1273 "\tmount_opts=<extended default mount options>\n" 1274 "\tstride=<RAID per-disk chunk size in blocks>\n" 1275 "\tstripe_width=<RAID stride*data disks in blocks>\n" 1276 "\ttest_fs\n" 1277 "\t^test_fs\n")); 1278 free(buf); 1279 return 1; 1280 } 1281 free(buf); 1282 1283 return 0; 1284} 1285 1286/* 1287 * Fill in the block bitmap bmap with the information regarding the 1288 * blocks to be moved 1289 */ 1290static int get_move_bitmaps(ext2_filsys fs, int new_ino_blks_per_grp, 1291 ext2fs_block_bitmap bmap) 1292{ 1293 dgrp_t i; 1294 int retval; 1295 ext2_badblocks_list bb_list = 0; 1296 blk64_t j, needed_blocks = 0; 1297 blk64_t start_blk, end_blk; 1298 1299 retval = ext2fs_read_bb_inode(fs, &bb_list); 1300 if (retval) 1301 return retval; 1302 1303 for (i = 0; i < fs->group_desc_count; i++) { 1304 start_blk = ext2fs_inode_table_loc(fs, i) + 1305 fs->inode_blocks_per_group; 1306 1307 end_blk = ext2fs_inode_table_loc(fs, i) + 1308 new_ino_blks_per_grp; 1309 1310 for (j = start_blk; j < end_blk; j++) { 1311 if (ext2fs_test_block_bitmap2(fs->block_map, j)) { 1312 /* 1313 * IF the block is a bad block we fail 1314 */ 1315 if (ext2fs_badblocks_list_test(bb_list, j)) { 1316 ext2fs_badblocks_list_free(bb_list); 1317 return ENOSPC; 1318 } 1319 1320 ext2fs_mark_block_bitmap2(bmap, j); 1321 } else { 1322 /* 1323 * We are going to use this block for 1324 * inode table. So mark them used. 1325 */ 1326 ext2fs_mark_block_bitmap2(fs->block_map, j); 1327 } 1328 } 1329 needed_blocks += end_blk - start_blk; 1330 } 1331 1332 ext2fs_badblocks_list_free(bb_list); 1333 if (needed_blocks > ext2fs_free_blocks_count(fs->super)) 1334 return ENOSPC; 1335 1336 return 0; 1337} 1338 1339static int ext2fs_is_meta_block(ext2_filsys fs, blk64_t blk) 1340{ 1341 dgrp_t group; 1342 group = ext2fs_group_of_blk2(fs, blk); 1343 if (ext2fs_block_bitmap_loc(fs, group) == blk) 1344 return 1; 1345 if (ext2fs_inode_bitmap_loc(fs, group) == blk) 1346 return 1; 1347 return 0; 1348} 1349 1350static int ext2fs_is_block_in_group(ext2_filsys fs, dgrp_t group, blk64_t blk) 1351{ 1352 blk64_t start_blk, end_blk; 1353 start_blk = fs->super->s_first_data_block + 1354 EXT2_BLOCKS_PER_GROUP(fs->super) * group; 1355 /* 1356 * We cannot get new block beyond end_blk for for the last block group 1357 * so we can check with EXT2_BLOCKS_PER_GROUP even for last block group 1358 */ 1359 end_blk = start_blk + EXT2_BLOCKS_PER_GROUP(fs->super); 1360 if (blk >= start_blk && blk <= end_blk) 1361 return 1; 1362 return 0; 1363} 1364 1365static int move_block(ext2_filsys fs, ext2fs_block_bitmap bmap) 1366{ 1367 1368 char *buf; 1369 dgrp_t group = 0; 1370 errcode_t retval; 1371 int meta_data = 0; 1372 blk64_t blk, new_blk, goal; 1373 struct blk_move *bmv; 1374 1375 retval = ext2fs_get_mem(fs->blocksize, &buf); 1376 if (retval) 1377 return retval; 1378 1379 for (new_blk = blk = fs->super->s_first_data_block; 1380 blk < ext2fs_blocks_count(fs->super); blk++) { 1381 if (!ext2fs_test_block_bitmap2(bmap, blk)) 1382 continue; 1383 1384 if (ext2fs_is_meta_block(fs, blk)) { 1385 /* 1386 * If the block is mapping a fs meta data block 1387 * like group desc/block bitmap/inode bitmap. We 1388 * should find a block in the same group and fix 1389 * the respective fs metadata pointers. Otherwise 1390 * fail 1391 */ 1392 group = ext2fs_group_of_blk2(fs, blk); 1393 goal = ext2fs_group_first_block2(fs, group); 1394 meta_data = 1; 1395 1396 } else { 1397 goal = new_blk; 1398 } 1399 retval = ext2fs_new_block2(fs, goal, NULL, &new_blk); 1400 if (retval) 1401 goto err_out; 1402 1403 /* new fs meta data block should be in the same group */ 1404 if (meta_data && !ext2fs_is_block_in_group(fs, group, new_blk)) { 1405 retval = ENOSPC; 1406 goto err_out; 1407 } 1408 1409 /* Mark this block as allocated */ 1410 ext2fs_mark_block_bitmap2(fs->block_map, new_blk); 1411 1412 /* Add it to block move list */ 1413 retval = ext2fs_get_mem(sizeof(struct blk_move), &bmv); 1414 if (retval) 1415 goto err_out; 1416 1417 bmv->old_loc = blk; 1418 bmv->new_loc = new_blk; 1419 1420 list_add(&(bmv->list), &blk_move_list); 1421 1422 retval = io_channel_read_blk64(fs->io, blk, 1, buf); 1423 if (retval) 1424 goto err_out; 1425 1426 retval = io_channel_write_blk64(fs->io, new_blk, 1, buf); 1427 if (retval) 1428 goto err_out; 1429 } 1430 1431err_out: 1432 ext2fs_free_mem(&buf); 1433 return retval; 1434} 1435 1436static blk64_t translate_block(blk64_t blk) 1437{ 1438 struct list_head *entry; 1439 struct blk_move *bmv; 1440 1441 list_for_each(entry, &blk_move_list) { 1442 bmv = list_entry(entry, struct blk_move, list); 1443 if (bmv->old_loc == blk) 1444 return bmv->new_loc; 1445 } 1446 1447 return 0; 1448} 1449 1450static int process_block(ext2_filsys fs EXT2FS_ATTR((unused)), 1451 blk64_t *block_nr, 1452 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)), 1453 blk64_t ref_block EXT2FS_ATTR((unused)), 1454 int ref_offset EXT2FS_ATTR((unused)), 1455 void *priv_data) 1456{ 1457 int ret = 0; 1458 blk64_t new_blk; 1459 ext2fs_block_bitmap bmap = (ext2fs_block_bitmap) priv_data; 1460 1461 if (!ext2fs_test_block_bitmap2(bmap, *block_nr)) 1462 return 0; 1463 new_blk = translate_block(*block_nr); 1464 if (new_blk) { 1465 *block_nr = new_blk; 1466 /* 1467 * This will force the ext2fs_write_inode in the iterator 1468 */ 1469 ret |= BLOCK_CHANGED; 1470 } 1471 1472 return ret; 1473} 1474 1475static int inode_scan_and_fix(ext2_filsys fs, ext2fs_block_bitmap bmap) 1476{ 1477 errcode_t retval = 0; 1478 ext2_ino_t ino; 1479 blk64_t blk; 1480 char *block_buf = 0; 1481 struct ext2_inode inode; 1482 ext2_inode_scan scan = NULL; 1483 1484 retval = ext2fs_get_mem(fs->blocksize * 3, &block_buf); 1485 if (retval) 1486 return retval; 1487 1488 retval = ext2fs_open_inode_scan(fs, 0, &scan); 1489 if (retval) 1490 goto err_out; 1491 1492 while (1) { 1493 retval = ext2fs_get_next_inode(scan, &ino, &inode); 1494 if (retval) 1495 goto err_out; 1496 1497 if (!ino) 1498 break; 1499 1500 if (inode.i_links_count == 0) 1501 continue; /* inode not in use */ 1502 1503 /* FIXME!! 1504 * If we end up modifying the journal inode 1505 * the sb->s_jnl_blocks will differ. But a 1506 * subsequent e2fsck fixes that. 1507 * Do we need to fix this ?? 1508 */ 1509 1510 if (ext2fs_file_acl_block(fs, &inode) && 1511 ext2fs_test_block_bitmap2(bmap, 1512 ext2fs_file_acl_block(fs, &inode))) { 1513 blk = translate_block(ext2fs_file_acl_block(fs, 1514 &inode)); 1515 if (!blk) 1516 continue; 1517 1518 ext2fs_file_acl_block_set(fs, &inode, blk); 1519 1520 /* 1521 * Write the inode to disk so that inode table 1522 * resizing can work 1523 */ 1524 retval = ext2fs_write_inode(fs, ino, &inode); 1525 if (retval) 1526 goto err_out; 1527 } 1528 1529 if (!ext2fs_inode_has_valid_blocks2(fs, &inode)) 1530 continue; 1531 1532 retval = ext2fs_block_iterate3(fs, ino, 0, block_buf, 1533 process_block, bmap); 1534 if (retval) 1535 goto err_out; 1536 1537 } 1538 1539err_out: 1540 ext2fs_free_mem(&block_buf); 1541 1542 return retval; 1543} 1544 1545/* 1546 * We need to scan for inode and block bitmaps that may need to be 1547 * moved. This can take place if the filesystem was formatted for 1548 * RAID arrays using the mke2fs's extended option "stride". 1549 */ 1550static int group_desc_scan_and_fix(ext2_filsys fs, ext2fs_block_bitmap bmap) 1551{ 1552 dgrp_t i; 1553 blk64_t blk, new_blk; 1554 1555 for (i = 0; i < fs->group_desc_count; i++) { 1556 blk = ext2fs_block_bitmap_loc(fs, i); 1557 if (ext2fs_test_block_bitmap2(bmap, blk)) { 1558 new_blk = translate_block(blk); 1559 if (!new_blk) 1560 continue; 1561 ext2fs_block_bitmap_loc_set(fs, i, new_blk); 1562 } 1563 1564 blk = ext2fs_inode_bitmap_loc(fs, i); 1565 if (ext2fs_test_block_bitmap2(bmap, blk)) { 1566 new_blk = translate_block(blk); 1567 if (!new_blk) 1568 continue; 1569 ext2fs_inode_bitmap_loc_set(fs, i, new_blk); 1570 } 1571 } 1572 return 0; 1573} 1574 1575static int expand_inode_table(ext2_filsys fs, unsigned long new_ino_size) 1576{ 1577 dgrp_t i; 1578 blk64_t blk; 1579 errcode_t retval; 1580 int new_ino_blks_per_grp; 1581 unsigned int j; 1582 char *old_itable = NULL, *new_itable = NULL; 1583 char *tmp_old_itable = NULL, *tmp_new_itable = NULL; 1584 unsigned long old_ino_size; 1585 int old_itable_size, new_itable_size; 1586 1587 old_itable_size = fs->inode_blocks_per_group * fs->blocksize; 1588 old_ino_size = EXT2_INODE_SIZE(fs->super); 1589 1590 new_ino_blks_per_grp = ext2fs_div_ceil( 1591 EXT2_INODES_PER_GROUP(fs->super) * 1592 new_ino_size, 1593 fs->blocksize); 1594 1595 new_itable_size = new_ino_blks_per_grp * fs->blocksize; 1596 1597 retval = ext2fs_get_mem(old_itable_size, &old_itable); 1598 if (retval) 1599 return retval; 1600 1601 retval = ext2fs_get_mem(new_itable_size, &new_itable); 1602 if (retval) 1603 goto err_out; 1604 1605 tmp_old_itable = old_itable; 1606 tmp_new_itable = new_itable; 1607 1608 for (i = 0; i < fs->group_desc_count; i++) { 1609 blk = ext2fs_inode_table_loc(fs, i); 1610 retval = io_channel_read_blk64(fs->io, blk, 1611 fs->inode_blocks_per_group, old_itable); 1612 if (retval) 1613 goto err_out; 1614 1615 for (j = 0; j < EXT2_INODES_PER_GROUP(fs->super); j++) { 1616 memcpy(new_itable, old_itable, old_ino_size); 1617 1618 memset(new_itable+old_ino_size, 0, 1619 new_ino_size - old_ino_size); 1620 1621 new_itable += new_ino_size; 1622 old_itable += old_ino_size; 1623 } 1624 1625 /* reset the pointer */ 1626 old_itable = tmp_old_itable; 1627 new_itable = tmp_new_itable; 1628 1629 retval = io_channel_write_blk64(fs->io, blk, 1630 new_ino_blks_per_grp, new_itable); 1631 if (retval) 1632 goto err_out; 1633 } 1634 1635 /* Update the meta data */ 1636 fs->inode_blocks_per_group = new_ino_blks_per_grp; 1637 fs->super->s_inode_size = new_ino_size; 1638 1639err_out: 1640 if (old_itable) 1641 ext2fs_free_mem(&old_itable); 1642 1643 if (new_itable) 1644 ext2fs_free_mem(&new_itable); 1645 1646 return retval; 1647} 1648 1649static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs) 1650{ 1651 blk64_t blk; 1652 ext2_ino_t ino; 1653 unsigned int group = 0; 1654 unsigned int count = 0; 1655 int total_free = 0; 1656 int group_free = 0; 1657 1658 /* 1659 * First calculate the block statistics 1660 */ 1661 for (blk = fs->super->s_first_data_block; 1662 blk < ext2fs_blocks_count(fs->super); blk++) { 1663 if (!ext2fs_fast_test_block_bitmap2(fs->block_map, blk)) { 1664 group_free++; 1665 total_free++; 1666 } 1667 count++; 1668 if ((count == fs->super->s_blocks_per_group) || 1669 (blk == ext2fs_blocks_count(fs->super)-1)) { 1670 ext2fs_bg_free_blocks_count_set(fs, group++, 1671 group_free); 1672 count = 0; 1673 group_free = 0; 1674 } 1675 } 1676 total_free = EXT2FS_C2B(fs, total_free); 1677 ext2fs_free_blocks_count_set(fs->super, total_free); 1678 1679 /* 1680 * Next, calculate the inode statistics 1681 */ 1682 group_free = 0; 1683 total_free = 0; 1684 count = 0; 1685 group = 0; 1686 1687 /* Protect loop from wrap-around if s_inodes_count maxed */ 1688 for (ino = 1; ino <= fs->super->s_inodes_count && ino > 0; ino++) { 1689 if (!ext2fs_fast_test_inode_bitmap2(fs->inode_map, ino)) { 1690 group_free++; 1691 total_free++; 1692 } 1693 count++; 1694 if ((count == fs->super->s_inodes_per_group) || 1695 (ino == fs->super->s_inodes_count)) { 1696 ext2fs_bg_free_inodes_count_set(fs, group++, 1697 group_free); 1698 count = 0; 1699 group_free = 0; 1700 } 1701 } 1702 fs->super->s_free_inodes_count = total_free; 1703 ext2fs_mark_super_dirty(fs); 1704 return 0; 1705} 1706 1707#define list_for_each_safe(pos, pnext, head) \ 1708 for (pos = (head)->next, pnext = pos->next; pos != (head); \ 1709 pos = pnext, pnext = pos->next) 1710 1711static void free_blk_move_list(void) 1712{ 1713 struct list_head *entry, *tmp; 1714 struct blk_move *bmv; 1715 1716 list_for_each_safe(entry, tmp, &blk_move_list) { 1717 bmv = list_entry(entry, struct blk_move, list); 1718 list_del(entry); 1719 ext2fs_free_mem(&bmv); 1720 } 1721 return; 1722} 1723 1724static int resize_inode(ext2_filsys fs, unsigned long new_size) 1725{ 1726 errcode_t retval; 1727 int new_ino_blks_per_grp; 1728 ext2fs_block_bitmap bmap; 1729 1730 retval = ext2fs_read_inode_bitmap(fs); 1731 if (retval) { 1732 fputs(_("Failed to read inode bitmap\n"), stderr); 1733 return retval; 1734 } 1735 retval = ext2fs_read_block_bitmap(fs); 1736 if (retval) { 1737 fputs(_("Failed to read block bitmap\n"), stderr); 1738 return retval; 1739 } 1740 INIT_LIST_HEAD(&blk_move_list); 1741 1742 1743 new_ino_blks_per_grp = ext2fs_div_ceil( 1744 EXT2_INODES_PER_GROUP(fs->super)* 1745 new_size, 1746 fs->blocksize); 1747 1748 /* We may change the file system. 1749 * Mark the file system as invalid so that 1750 * the user is prompted to run fsck. 1751 */ 1752 fs->super->s_state &= ~EXT2_VALID_FS; 1753 1754 retval = ext2fs_allocate_block_bitmap(fs, _("blocks to be moved"), 1755 &bmap); 1756 if (retval) { 1757 fputs(_("Failed to allocate block bitmap when " 1758 "increasing inode size\n"), stderr); 1759 return retval; 1760 } 1761 retval = get_move_bitmaps(fs, new_ino_blks_per_grp, bmap); 1762 if (retval) { 1763 fputs(_("Not enough space to increase inode size \n"), stderr); 1764 goto err_out; 1765 } 1766 retval = move_block(fs, bmap); 1767 if (retval) { 1768 fputs(_("Failed to relocate blocks during inode resize \n"), 1769 stderr); 1770 goto err_out; 1771 } 1772 retval = inode_scan_and_fix(fs, bmap); 1773 if (retval) 1774 goto err_out_undo; 1775 1776 retval = group_desc_scan_and_fix(fs, bmap); 1777 if (retval) 1778 goto err_out_undo; 1779 1780 retval = expand_inode_table(fs, new_size); 1781 if (retval) 1782 goto err_out_undo; 1783 1784 ext2fs_calculate_summary_stats(fs); 1785 1786 fs->super->s_state |= EXT2_VALID_FS; 1787 /* mark super block and block bitmap as dirty */ 1788 ext2fs_mark_super_dirty(fs); 1789 ext2fs_mark_bb_dirty(fs); 1790 1791err_out: 1792 free_blk_move_list(); 1793 ext2fs_free_block_bitmap(bmap); 1794 1795 return retval; 1796 1797err_out_undo: 1798 free_blk_move_list(); 1799 ext2fs_free_block_bitmap(bmap); 1800 fputs(_("Error in resizing the inode size.\n" 1801 "Run e2undo to undo the " 1802 "file system changes. \n"), stderr); 1803 1804 return retval; 1805} 1806 1807static int tune2fs_setup_tdb(const char *name, io_manager *io_ptr) 1808{ 1809 errcode_t retval = 0; 1810 const char *tdb_dir; 1811 char *tdb_file; 1812 char *dev_name, *tmp_name; 1813 1814#if 0 /* FIXME!! */ 1815 /* 1816 * Configuration via a conf file would be 1817 * nice 1818 */ 1819 profile_get_string(profile, "scratch_files", 1820 "directory", 0, 0, 1821 &tdb_dir); 1822#endif 1823 tmp_name = strdup(name); 1824 if (!tmp_name) { 1825 alloc_fn_fail: 1826 com_err(program_name, ENOMEM, 1827 _("Couldn't allocate memory for tdb filename\n")); 1828 return ENOMEM; 1829 } 1830 dev_name = basename(tmp_name); 1831 1832 tdb_dir = getenv("E2FSPROGS_UNDO_DIR"); 1833 if (!tdb_dir) 1834 tdb_dir = "/var/lib/e2fsprogs"; 1835 1836 if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) || 1837 access(tdb_dir, W_OK)) 1838 return 0; 1839 1840 tdb_file = malloc(strlen(tdb_dir) + 9 + strlen(dev_name) + 7 + 1); 1841 if (!tdb_file) 1842 goto alloc_fn_fail; 1843 sprintf(tdb_file, "%s/tune2fs-%s.e2undo", tdb_dir, dev_name); 1844 1845 if (!access(tdb_file, F_OK)) { 1846 if (unlink(tdb_file) < 0) { 1847 retval = errno; 1848 com_err(program_name, retval, 1849 _("while trying to delete %s"), 1850 tdb_file); 1851 free(tdb_file); 1852 return retval; 1853 } 1854 } 1855 1856 set_undo_io_backing_manager(*io_ptr); 1857 *io_ptr = undo_io_manager; 1858 set_undo_io_backup_file(tdb_file); 1859 printf(_("To undo the tune2fs operation please run " 1860 "the command\n e2undo %s %s\n\n"), 1861 tdb_file, name); 1862 free(tdb_file); 1863 free(tmp_name); 1864 return retval; 1865} 1866 1867int main(int argc, char **argv) 1868{ 1869 errcode_t retval; 1870 ext2_filsys fs; 1871 struct ext2_super_block *sb; 1872 io_manager io_ptr, io_ptr_orig = NULL; 1873 int rc = 0; 1874 1875#ifdef ENABLE_NLS 1876 setlocale(LC_MESSAGES, ""); 1877 setlocale(LC_CTYPE, ""); 1878 bindtextdomain(NLS_CAT_NAME, LOCALEDIR); 1879 textdomain(NLS_CAT_NAME); 1880 set_com_err_gettext(gettext); 1881#endif 1882 if (argc && *argv) 1883 program_name = *argv; 1884 add_error_table(&et_ext2_error_table); 1885 1886#ifdef CONFIG_BUILD_FINDFS 1887 if (strcmp(get_progname(argv[0]), "findfs") == 0) 1888 do_findfs(argc, argv); 1889#endif 1890 if (strcmp(get_progname(argv[0]), "e2label") == 0) 1891 parse_e2label_options(argc, argv); 1892 else 1893 parse_tune2fs_options(argc, argv); 1894 1895#ifdef CONFIG_TESTIO_DEBUG 1896 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_DEBUG")) { 1897 io_ptr = test_io_manager; 1898 test_io_backing_manager = unix_io_manager; 1899 } else 1900#endif 1901 io_ptr = unix_io_manager; 1902 1903retry_open: 1904 if ((open_flag & EXT2_FLAG_RW) == 0 || f_flag) 1905 open_flag |= EXT2_FLAG_SKIP_MMP; 1906 1907 open_flag |= EXT2_FLAG_64BITS; 1908 1909 /* keep the filesystem struct around to dump MMP data */ 1910 open_flag |= EXT2_FLAG_NOFREE_ON_ERROR; 1911 1912 retval = ext2fs_open2(device_name, io_options, open_flag, 1913 0, 0, io_ptr, &fs); 1914 if (retval) { 1915 com_err(program_name, retval, 1916 _("while trying to open %s"), 1917 device_name); 1918 if (retval == EXT2_ET_MMP_FSCK_ON || 1919 retval == EXT2_ET_MMP_UNKNOWN_SEQ) 1920 dump_mmp_msg(fs->mmp_buf, 1921 _("If you are sure the filesystem " 1922 "is not in use on any node, run:\n" 1923 "'tune2fs -f -E clear_mmp {device}'\n")); 1924 else if (retval == EXT2_ET_MMP_FAILED) 1925 dump_mmp_msg(fs->mmp_buf, NULL); 1926 else if (retval == EXT2_ET_MMP_MAGIC_INVALID) 1927 fprintf(stderr, 1928 _("MMP block magic is bad. Try to fix it by " 1929 "running:\n'e2fsck -f %s'\n"), device_name); 1930 else if (retval != EXT2_ET_MMP_FAILED) 1931 fprintf(stderr, 1932 _("Couldn't find valid filesystem superblock.\n")); 1933 1934 ext2fs_free(fs); 1935 exit(1); 1936 } 1937 fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE; 1938 1939 if (I_flag && !io_ptr_orig) { 1940 /* 1941 * Check the inode size is right so we can issue an 1942 * error message and bail before setting up the tdb 1943 * file. 1944 */ 1945 if (new_inode_size == EXT2_INODE_SIZE(fs->super)) { 1946 fprintf(stderr, _("The inode size is already %lu\n"), 1947 new_inode_size); 1948 rc = 1; 1949 goto closefs; 1950 } 1951 if (new_inode_size < EXT2_INODE_SIZE(fs->super)) { 1952 fprintf(stderr, _("Shrinking the inode size is " 1953 "not supported\n")); 1954 rc = 1; 1955 goto closefs; 1956 } 1957 if (new_inode_size > fs->blocksize) { 1958 fprintf(stderr, _("Invalid inode size %lu (max %d)\n"), 1959 new_inode_size, fs->blocksize); 1960 rc = 1; 1961 goto closefs; 1962 } 1963 1964 /* 1965 * If inode resize is requested use the 1966 * Undo I/O manager 1967 */ 1968 io_ptr_orig = io_ptr; 1969 retval = tune2fs_setup_tdb(device_name, &io_ptr); 1970 if (retval) { 1971 rc = 1; 1972 goto closefs; 1973 } 1974 if (io_ptr != io_ptr_orig) { 1975 ext2fs_close(fs); 1976 goto retry_open; 1977 } 1978 } 1979 1980 sb = fs->super; 1981 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY; 1982 1983 if (print_label) { 1984 /* For e2label emulation */ 1985 printf("%.*s\n", (int) sizeof(sb->s_volume_name), 1986 sb->s_volume_name); 1987 remove_error_table(&et_ext2_error_table); 1988 goto closefs; 1989 } 1990 1991 retval = ext2fs_check_if_mounted(device_name, &mount_flags); 1992 if (retval) { 1993 com_err("ext2fs_check_if_mount", retval, 1994 _("while determining whether %s is mounted."), 1995 device_name); 1996 rc = 1; 1997 goto closefs; 1998 } 1999 /* Normally we only need to write out the superblock */ 2000 fs->flags |= EXT2_FLAG_SUPER_ONLY; 2001 2002 if (c_flag) { 2003 sb->s_max_mnt_count = max_mount_count; 2004 ext2fs_mark_super_dirty(fs); 2005 printf(_("Setting maximal mount count to %d\n"), 2006 max_mount_count); 2007 } 2008 if (C_flag) { 2009 sb->s_mnt_count = mount_count; 2010 ext2fs_mark_super_dirty(fs); 2011 printf(_("Setting current mount count to %d\n"), mount_count); 2012 } 2013 if (e_flag) { 2014 sb->s_errors = errors; 2015 ext2fs_mark_super_dirty(fs); 2016 printf(_("Setting error behavior to %d\n"), errors); 2017 } 2018 if (g_flag) { 2019 sb->s_def_resgid = resgid; 2020 ext2fs_mark_super_dirty(fs); 2021 printf(_("Setting reserved blocks gid to %lu\n"), resgid); 2022 } 2023 if (i_flag) { 2024 if (interval >= (1ULL << 32)) { 2025 com_err(program_name, 0, 2026 _("interval between checks is too big (%lu)"), 2027 interval); 2028 rc = 1; 2029 goto closefs; 2030 } 2031 sb->s_checkinterval = interval; 2032 ext2fs_mark_super_dirty(fs); 2033 printf(_("Setting interval between checks to %lu seconds\n"), 2034 interval); 2035 } 2036 if (m_flag) { 2037 ext2fs_r_blocks_count_set(sb, reserved_ratio * 2038 ext2fs_blocks_count(sb) / 100.0); 2039 ext2fs_mark_super_dirty(fs); 2040 printf (_("Setting reserved blocks percentage to %g%% (%llu blocks)\n"), 2041 reserved_ratio, ext2fs_r_blocks_count(sb)); 2042 } 2043 if (r_flag) { 2044 if (reserved_blocks > ext2fs_blocks_count(sb)/2) { 2045 com_err(program_name, 0, 2046 _("reserved blocks count is too big (%llu)"), 2047 reserved_blocks); 2048 rc = 1; 2049 goto closefs; 2050 } 2051 ext2fs_r_blocks_count_set(sb, reserved_blocks); 2052 ext2fs_mark_super_dirty(fs); 2053 printf(_("Setting reserved blocks count to %llu\n"), 2054 reserved_blocks); 2055 } 2056 if (s_flag == 1) { 2057 if (sb->s_feature_ro_compat & 2058 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER) 2059 fputs(_("\nThe filesystem already has sparse " 2060 "superblocks.\n"), stderr); 2061 else { 2062 sb->s_feature_ro_compat |= 2063 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER; 2064 sb->s_state &= ~EXT2_VALID_FS; 2065 ext2fs_mark_super_dirty(fs); 2066 printf(_("\nSparse superblock flag set. %s"), 2067 _(please_fsck)); 2068 } 2069 } 2070 if (s_flag == 0) { 2071 fputs(_("\nClearing the sparse superflag not supported.\n"), 2072 stderr); 2073 rc = 1; 2074 goto closefs; 2075 } 2076 if (T_flag) { 2077 sb->s_lastcheck = last_check_time; 2078 ext2fs_mark_super_dirty(fs); 2079 printf(_("Setting time filesystem last checked to %s\n"), 2080 ctime(&last_check_time)); 2081 } 2082 if (u_flag) { 2083 sb->s_def_resuid = resuid; 2084 ext2fs_mark_super_dirty(fs); 2085 printf(_("Setting reserved blocks uid to %lu\n"), resuid); 2086 } 2087 if (L_flag) { 2088 if (strlen(new_label) > sizeof(sb->s_volume_name)) 2089 fputs(_("Warning: label too long, truncating.\n"), 2090 stderr); 2091 memset(sb->s_volume_name, 0, sizeof(sb->s_volume_name)); 2092 strncpy(sb->s_volume_name, new_label, 2093 sizeof(sb->s_volume_name)); 2094 ext2fs_mark_super_dirty(fs); 2095 } 2096 if (M_flag) { 2097 memset(sb->s_last_mounted, 0, sizeof(sb->s_last_mounted)); 2098 strncpy(sb->s_last_mounted, new_last_mounted, 2099 sizeof(sb->s_last_mounted)); 2100 ext2fs_mark_super_dirty(fs); 2101 } 2102 if (mntopts_cmd) { 2103 rc = update_mntopts(fs, mntopts_cmd); 2104 if (rc) 2105 goto closefs; 2106 } 2107 if (features_cmd) { 2108 rc = update_feature_set(fs, features_cmd); 2109 if (rc) 2110 goto closefs; 2111 } 2112 if (extended_cmd) { 2113 rc = parse_extended_opts(fs, extended_cmd); 2114 if (rc) 2115 goto closefs; 2116 if (clear_mmp && !f_flag) { 2117 fputs(_("Error in using clear_mmp. " 2118 "It must be used with -f\n"), 2119 stderr); 2120 goto closefs; 2121 } 2122 } 2123 if (clear_mmp) { 2124 rc = ext2fs_mmp_clear(fs); 2125 goto closefs; 2126 } 2127 if (journal_size || journal_device) { 2128 rc = add_journal(fs); 2129 if (rc) 2130 goto closefs; 2131 } 2132 2133 if (Q_flag) { 2134 if (mount_flags & EXT2_MF_MOUNTED) { 2135 fputs(_("The quota feature may only be changed when " 2136 "the filesystem is unmounted.\n"), stderr); 2137 rc = 1; 2138 goto closefs; 2139 } 2140 handle_quota_options(fs); 2141 } 2142 2143 if (U_flag) { 2144 int set_csum = 0; 2145 dgrp_t i; 2146 2147 if (sb->s_feature_ro_compat & 2148 EXT4_FEATURE_RO_COMPAT_GDT_CSUM) { 2149 /* 2150 * Determine if the block group checksums are 2151 * correct so we know whether or not to set 2152 * them later on. 2153 */ 2154 for (i = 0; i < fs->group_desc_count; i++) 2155 if (!ext2fs_group_desc_csum_verify(fs, i)) 2156 break; 2157 if (i >= fs->group_desc_count) 2158 set_csum = 1; 2159 } 2160 if ((strcasecmp(new_UUID, "null") == 0) || 2161 (strcasecmp(new_UUID, "clear") == 0)) { 2162 uuid_clear(sb->s_uuid); 2163 } else if (strcasecmp(new_UUID, "time") == 0) { 2164 uuid_generate_time(sb->s_uuid); 2165 } else if (strcasecmp(new_UUID, "random") == 0) { 2166 uuid_generate(sb->s_uuid); 2167 } else if (uuid_parse(new_UUID, sb->s_uuid)) { 2168 com_err(program_name, 0, _("Invalid UUID format\n")); 2169 rc = 1; 2170 goto closefs; 2171 } 2172 if (set_csum) { 2173 for (i = 0; i < fs->group_desc_count; i++) 2174 ext2fs_group_desc_csum_set(fs, i); 2175 fs->flags &= ~EXT2_FLAG_SUPER_ONLY; 2176 } 2177 ext2fs_mark_super_dirty(fs); 2178 } 2179 if (I_flag) { 2180 if (mount_flags & EXT2_MF_MOUNTED) { 2181 fputs(_("The inode size may only be " 2182 "changed when the filesystem is " 2183 "unmounted.\n"), stderr); 2184 rc = 1; 2185 goto closefs; 2186 } 2187 if (fs->super->s_feature_incompat & 2188 EXT4_FEATURE_INCOMPAT_FLEX_BG) { 2189 fputs(_("Changing the inode size not supported for " 2190 "filesystems with the flex_bg\n" 2191 "feature enabled.\n"), 2192 stderr); 2193 rc = 1; 2194 goto closefs; 2195 } 2196 /* 2197 * We want to update group descriptor also 2198 * with the new free inode count 2199 */ 2200 fs->flags &= ~EXT2_FLAG_SUPER_ONLY; 2201 if (resize_inode(fs, new_inode_size) == 0) { 2202 printf(_("Setting inode size %lu\n"), 2203 new_inode_size); 2204 } else { 2205 printf(_("Failed to change inode size\n")); 2206 rc = 1; 2207 goto closefs; 2208 } 2209 } 2210 2211 if (l_flag) 2212 list_super(sb); 2213 if (stride_set) { 2214 sb->s_raid_stride = stride; 2215 ext2fs_mark_super_dirty(fs); 2216 printf(_("Setting stride size to %d\n"), stride); 2217 } 2218 if (stripe_width_set) { 2219 sb->s_raid_stripe_width = stripe_width; 2220 ext2fs_mark_super_dirty(fs); 2221 printf(_("Setting stripe width to %d\n"), stripe_width); 2222 } 2223 if (ext_mount_opts) { 2224 strncpy((char *)(fs->super->s_mount_opts), ext_mount_opts, 2225 sizeof(fs->super->s_mount_opts)); 2226 fs->super->s_mount_opts[sizeof(fs->super->s_mount_opts)-1] = 0; 2227 ext2fs_mark_super_dirty(fs); 2228 printf(_("Setting extended default mount options to '%s'\n"), 2229 ext_mount_opts); 2230 free(ext_mount_opts); 2231 } 2232 free(device_name); 2233 remove_error_table(&et_ext2_error_table); 2234 2235closefs: 2236 if (rc) { 2237 ext2fs_mmp_stop(fs); 2238 exit(1); 2239 } 2240 2241 return (ext2fs_close(fs) ? 1 : 0); 2242} 2243