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