2 * tune2fs.c - Change the file system parameters on an ext2 file system
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)
8 * Copyright 1995, 1996, 1997, 1998, 1999, 2000 by Theodore Ts'o.
11 * This file may be redistributed under the terms of the GNU Public
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)
28 #define _XOPEN_SOURCE 600 /* for inclusion of strptime() */
29 #define _BSD_SOURCE /* for inclusion of strcasecmp() */
46 #include <sys/types.h>
50 #include "ext2fs/ext2_fs.h"
51 #include "ext2fs/ext2fs.h"
52 #include "et/com_err.h"
53 #include "uuid/uuid.h"
57 #include "blkid/blkid.h"
58 #include "quota/mkquota.h"
60 #include "../version.h"
61 #include "nls-enable.h"
63 #define QOPT_ENABLE (1)
64 #define QOPT_DISABLE (-1)
66 extern int ask_yn(const char *string, int def);
68 const char *program_name = "tune2fs";
70 char *new_label, *new_last_mounted, *new_UUID;
72 static int c_flag, C_flag, e_flag, f_flag, g_flag, i_flag, l_flag, L_flag;
73 static int m_flag, M_flag, Q_flag, r_flag, s_flag = -1, u_flag, U_flag, T_flag;
75 static time_t last_check_time;
76 static int print_label;
77 static int max_mount_count, mount_count, mount_flags;
78 static unsigned long interval;
79 static blk64_t reserved_blocks;
80 static double reserved_ratio;
81 static unsigned long resgid, resuid;
82 static unsigned short errors;
84 static char *features_cmd;
85 static char *mntopts_cmd;
86 static int stride, stripe_width;
87 static int stride_set, stripe_width_set;
88 static char *extended_cmd;
89 static unsigned long new_inode_size;
90 static char *ext_mount_opts;
91 static int usrquota, grpquota;
93 int journal_size, journal_flags;
96 static struct list_head blk_move_list;
99 struct list_head list;
105 static const char *please_fsck = N_("Please run e2fsck on the filesystem.\n");
107 #ifdef CONFIG_BUILD_FINDFS
108 void do_findfs(int argc, char **argv);
111 static void usage(void)
114 _("Usage: %s [-c max_mounts_count] [-e errors_behavior] "
116 "\t[-i interval[d|m|w]] [-j] [-J journal_options] [-l]\n"
117 "\t[-m reserved_blocks_percent] "
118 "[-o [^]mount_options[,...]] \n"
119 "\t[-r reserved_blocks_count] [-u user] [-C mount_count] "
120 "[-L volume_label]\n"
121 "\t[-M last_mounted_dir] [-O [^]feature[,...]]\n"
122 "\t[-E extended-option[,...]] [-T last_check_time] "
123 "[-U UUID]\n\t[ -I new_inode_size ] device\n"), program_name);
127 static __u32 ok_features[3] = {
129 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
130 EXT2_FEATURE_COMPAT_DIR_INDEX,
132 EXT2_FEATURE_INCOMPAT_FILETYPE |
133 EXT3_FEATURE_INCOMPAT_EXTENTS |
134 EXT4_FEATURE_INCOMPAT_FLEX_BG,
136 EXT2_FEATURE_RO_COMPAT_LARGE_FILE |
137 EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
138 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
139 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
140 EXT4_FEATURE_RO_COMPAT_GDT_CSUM |
141 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER |
142 EXT4_FEATURE_RO_COMPAT_QUOTA
145 static __u32 clear_ok_features[3] = {
147 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
148 EXT2_FEATURE_COMPAT_RESIZE_INODE |
149 EXT2_FEATURE_COMPAT_DIR_INDEX,
151 EXT2_FEATURE_INCOMPAT_FILETYPE |
152 EXT4_FEATURE_INCOMPAT_FLEX_BG,
154 EXT2_FEATURE_RO_COMPAT_LARGE_FILE |
155 EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
156 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
157 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
158 EXT4_FEATURE_RO_COMPAT_GDT_CSUM |
159 EXT4_FEATURE_RO_COMPAT_QUOTA
163 * Remove an external journal from the filesystem
165 static void remove_journal_device(ext2_filsys fs)
170 journal_superblock_t *jsb;
173 int commit_remove_journal = 0;
177 commit_remove_journal = 1; /* force removal even if error */
179 uuid_unparse(fs->super->s_journal_uuid, buf);
180 journal_path = blkid_get_devname(NULL, "UUID", buf);
184 ext2fs_find_block_device(fs->super->s_journal_dev);
189 #ifdef CONFIG_TESTIO_DEBUG
190 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
191 io_ptr = test_io_manager;
192 test_io_backing_manager = unix_io_manager;
195 io_ptr = unix_io_manager;
196 retval = ext2fs_open(journal_path, EXT2_FLAG_RW|
197 EXT2_FLAG_JOURNAL_DEV_OK, 0,
198 fs->blocksize, io_ptr, &jfs);
200 com_err(program_name, retval,
201 _("while trying to open external journal"));
202 goto no_valid_journal;
204 if (!(jfs->super->s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
205 fprintf(stderr, _("%s is not a journal device.\n"),
207 goto no_valid_journal;
210 /* Get the journal superblock */
211 if ((retval = io_channel_read_blk64(jfs->io, 1, -1024, buf))) {
212 com_err(program_name, retval,
213 _("while reading journal superblock"));
214 goto no_valid_journal;
217 jsb = (journal_superblock_t *) buf;
218 if ((jsb->s_header.h_magic != (unsigned) ntohl(JFS_MAGIC_NUMBER)) ||
219 (jsb->s_header.h_blocktype != (unsigned) ntohl(JFS_SUPERBLOCK_V2))) {
220 fputs(_("Journal superblock not found!\n"), stderr);
221 goto no_valid_journal;
224 /* Find the filesystem UUID */
225 nr_users = ntohl(jsb->s_nr_users);
226 for (i = 0; i < nr_users; i++) {
227 if (memcmp(fs->super->s_uuid,
228 &jsb->s_users[i*16], 16) == 0)
232 fputs(_("Filesystem's UUID not found on journal device.\n"),
234 commit_remove_journal = 1;
235 goto no_valid_journal;
238 for (i = 0; i < nr_users; i++)
239 memcpy(&jsb->s_users[i*16], &jsb->s_users[(i+1)*16], 16);
240 jsb->s_nr_users = htonl(nr_users);
242 /* Write back the journal superblock */
243 if ((retval = io_channel_write_blk64(jfs->io, 1, -1024, buf))) {
244 com_err(program_name, retval,
245 "while writing journal superblock.");
246 goto no_valid_journal;
249 commit_remove_journal = 1;
252 if (commit_remove_journal == 0) {
253 fputs(_("Journal NOT removed\n"), stderr);
256 fs->super->s_journal_dev = 0;
257 uuid_clear(fs->super->s_journal_uuid);
258 ext2fs_mark_super_dirty(fs);
259 fputs(_("Journal removed\n"), stdout);
263 /* Helper function for remove_journal_inode */
264 static int release_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
265 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
266 blk64_t ref_block EXT2FS_ATTR((unused)),
267 int ref_offset EXT2FS_ATTR((unused)),
268 void *private EXT2FS_ATTR((unused)))
274 ext2fs_unmark_block_bitmap2(fs->block_map, block);
275 group = ext2fs_group_of_blk2(fs, block);
276 ext2fs_bg_free_blocks_count_set(fs, group, ext2fs_bg_free_blocks_count(fs, group) + 1);
277 ext2fs_group_desc_csum_set(fs, group);
278 ext2fs_free_blocks_count_add(fs->super, EXT2FS_CLUSTER_RATIO(fs));
283 * Remove the journal inode from the filesystem
285 static void remove_journal_inode(ext2_filsys fs)
287 struct ext2_inode inode;
289 ino_t ino = fs->super->s_journal_inum;
291 retval = ext2fs_read_inode(fs, ino, &inode);
293 com_err(program_name, retval,
294 _("while reading journal inode"));
297 if (ino == EXT2_JOURNAL_INO) {
298 retval = ext2fs_read_bitmaps(fs);
300 com_err(program_name, retval,
301 _("while reading bitmaps"));
304 retval = ext2fs_block_iterate3(fs, ino,
305 BLOCK_FLAG_READ_ONLY, NULL,
306 release_blocks_proc, NULL);
308 com_err(program_name, retval,
309 _("while clearing journal inode"));
312 memset(&inode, 0, sizeof(inode));
313 ext2fs_mark_bb_dirty(fs);
314 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
316 inode.i_flags &= ~EXT2_IMMUTABLE_FL;
317 retval = ext2fs_write_inode(fs, ino, &inode);
319 com_err(program_name, retval,
320 _("while writing journal inode"));
323 fs->super->s_journal_inum = 0;
324 ext2fs_mark_super_dirty(fs);
328 * Update the default mount options
330 static void update_mntopts(ext2_filsys fs, char *mntopts)
332 struct ext2_super_block *sb = fs->super;
334 if (e2p_edit_mntopts(mntopts, &sb->s_default_mount_opts, ~0)) {
335 fprintf(stderr, _("Invalid mount option set: %s\n"),
339 ext2fs_mark_super_dirty(fs);
342 static void request_fsck_afterwards(ext2_filsys fs)
344 static int requested = 0;
348 fs->super->s_state &= ~EXT2_VALID_FS;
349 printf("\n%s\n", _(please_fsck));
350 if (mount_flags & EXT2_MF_READONLY)
351 printf(_("(and reboot afterwards!)\n"));
355 * Update the feature set as provided by the user.
357 static void update_feature_set(ext2_filsys fs, char *features)
359 struct ext2_super_block *sb = fs->super;
360 struct ext2_group_desc *gd;
361 __u32 old_features[3];
363 unsigned int mask_err;
365 #define FEATURE_ON(type, mask) (!(old_features[(type)] & (mask)) && \
366 ((&sb->s_feature_compat)[(type)] & (mask)))
367 #define FEATURE_OFF(type, mask) ((old_features[(type)] & (mask)) && \
368 !((&sb->s_feature_compat)[(type)] & (mask)))
369 #define FEATURE_CHANGED(type, mask) ((mask) & \
370 (old_features[(type)] ^ (&sb->s_feature_compat)[(type)]))
372 old_features[E2P_FEATURE_COMPAT] = sb->s_feature_compat;
373 old_features[E2P_FEATURE_INCOMPAT] = sb->s_feature_incompat;
374 old_features[E2P_FEATURE_RO_INCOMPAT] = sb->s_feature_ro_compat;
376 if (e2p_edit_feature2(features, &sb->s_feature_compat,
377 ok_features, clear_ok_features,
378 &type_err, &mask_err)) {
381 _("Invalid filesystem option set: %s\n"),
383 else if (type_err & E2P_FEATURE_NEGATE_FLAG)
384 fprintf(stderr, _("Clearing filesystem feature '%s' "
386 e2p_feature2string(type_err &
387 E2P_FEATURE_TYPE_MASK,
390 fprintf(stderr, _("Setting filesystem feature '%s' "
392 e2p_feature2string(type_err, mask_err));
396 if (FEATURE_OFF(E2P_FEATURE_COMPAT, EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
397 if ((mount_flags & EXT2_MF_MOUNTED) &&
398 !(mount_flags & EXT2_MF_READONLY)) {
399 fputs(_("The has_journal feature may only be "
400 "cleared when the filesystem is\n"
401 "unmounted or mounted "
402 "read-only.\n"), stderr);
405 if (sb->s_feature_incompat &
406 EXT3_FEATURE_INCOMPAT_RECOVER) {
407 fputs(_("The needs_recovery flag is set. "
408 "Please run e2fsck before clearing\n"
409 "the has_journal flag.\n"), stderr);
412 if (sb->s_journal_inum) {
413 remove_journal_inode(fs);
415 if (sb->s_journal_dev) {
416 remove_journal_device(fs);
420 if (FEATURE_ON(E2P_FEATURE_COMPAT, EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
422 * If adding a journal flag, let the create journal
423 * code below handle setting the flag and creating the
424 * journal. We supply a default size if necessary.
428 sb->s_feature_compat &= ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
431 if (FEATURE_ON(E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_DIR_INDEX)) {
432 if (!sb->s_def_hash_version)
433 sb->s_def_hash_version = EXT2_HASH_HALF_MD4;
434 if (uuid_is_null((unsigned char *) sb->s_hash_seed))
435 uuid_generate((unsigned char *) sb->s_hash_seed);
438 if (FEATURE_OFF(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
439 if (ext2fs_check_desc(fs)) {
440 fputs(_("Clearing the flex_bg flag would "
441 "cause the the filesystem to be\n"
442 "inconsistent.\n"), stderr);
447 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
448 EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
449 if ((mount_flags & EXT2_MF_MOUNTED) &&
450 !(mount_flags & EXT2_MF_READONLY)) {
451 fputs(_("The huge_file feature may only be "
452 "cleared when the filesystem is\n"
453 "unmounted or mounted "
454 "read-only.\n"), stderr);
459 if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT,
460 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
461 for (i = 0; i < fs->group_desc_count; i++) {
462 gd = ext2fs_group_desc(fs, fs->group_desc, i);
463 gd->bg_itable_unused = 0;
464 gd->bg_flags = EXT2_BG_INODE_ZEROED;
465 ext2fs_group_desc_csum_set(fs, i);
467 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
470 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
471 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
472 for (i = 0; i < fs->group_desc_count; i++) {
473 gd = ext2fs_group_desc(fs, fs->group_desc, i);
474 if ((gd->bg_flags & EXT2_BG_INODE_ZEROED) == 0) {
476 * XXX what we really should do is zap
477 * uninitialized inode tables instead.
479 request_fsck_afterwards(fs);
482 gd->bg_itable_unused = 0;
486 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
489 if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT,
490 EXT4_FEATURE_RO_COMPAT_QUOTA)) {
492 * Set the Q_flag here and handle the quota options in the code
497 /* Enable both user quota and group quota by default */
498 usrquota = QOPT_ENABLE;
499 grpquota = QOPT_ENABLE;
501 sb->s_feature_ro_compat &= ~EXT4_FEATURE_RO_COMPAT_QUOTA;
504 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
505 EXT4_FEATURE_RO_COMPAT_QUOTA)) {
507 * Set the Q_flag here and handle the quota options in the code
511 fputs(_("\nWarning: '^quota' option overrides '-Q'"
512 "arguments.\n"), stderr);
514 /* Disable both user quota and group quota by default */
515 usrquota = QOPT_DISABLE;
516 grpquota = QOPT_DISABLE;
519 if (sb->s_rev_level == EXT2_GOOD_OLD_REV &&
520 (sb->s_feature_compat || sb->s_feature_ro_compat ||
521 sb->s_feature_incompat))
522 ext2fs_update_dynamic_rev(fs);
524 if (FEATURE_CHANGED(E2P_FEATURE_RO_INCOMPAT,
525 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER) ||
526 FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
527 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) ||
528 FEATURE_CHANGED(E2P_FEATURE_INCOMPAT,
529 EXT2_FEATURE_INCOMPAT_FILETYPE) ||
530 FEATURE_CHANGED(E2P_FEATURE_COMPAT,
531 EXT2_FEATURE_COMPAT_RESIZE_INODE) ||
532 FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
533 EXT2_FEATURE_RO_COMPAT_LARGE_FILE))
534 request_fsck_afterwards(fs);
536 if ((old_features[E2P_FEATURE_COMPAT] != sb->s_feature_compat) ||
537 (old_features[E2P_FEATURE_INCOMPAT] != sb->s_feature_incompat) ||
538 (old_features[E2P_FEATURE_RO_INCOMPAT] != sb->s_feature_ro_compat))
539 ext2fs_mark_super_dirty(fs);
543 * Add a journal to the filesystem.
545 static void add_journal(ext2_filsys fs)
547 unsigned long journal_blocks;
552 if (fs->super->s_feature_compat &
553 EXT3_FEATURE_COMPAT_HAS_JOURNAL) {
554 fputs(_("The filesystem already has a journal.\n"), stderr);
557 if (journal_device) {
558 check_plausibility(journal_device);
559 check_mount(journal_device, 0, _("journal"));
560 #ifdef CONFIG_TESTIO_DEBUG
561 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
562 io_ptr = test_io_manager;
563 test_io_backing_manager = unix_io_manager;
566 io_ptr = unix_io_manager;
567 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
568 EXT2_FLAG_JOURNAL_DEV_OK, 0,
569 fs->blocksize, io_ptr, &jfs);
571 com_err(program_name, retval,
572 _("\n\twhile trying to open journal on %s\n"),
576 printf(_("Creating journal on device %s: "),
580 retval = ext2fs_add_journal_device(fs, jfs);
583 com_err(program_name, retval,
584 _("while adding filesystem to journal on %s"),
588 fputs(_("done\n"), stdout);
589 } else if (journal_size) {
590 fputs(_("Creating journal inode: "), stdout);
592 journal_blocks = figure_journal_size(journal_size, fs);
594 retval = ext2fs_add_journal_inode(fs, journal_blocks,
597 fprintf(stderr, "\n");
598 com_err(program_name, retval,
599 _("\n\twhile trying to create journal file"));
602 fputs(_("done\n"), stdout);
604 * If the filesystem wasn't mounted, we need to force
605 * the block group descriptors out.
607 if ((mount_flags & EXT2_MF_MOUNTED) == 0)
608 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
610 print_check_message(fs);
614 free(journal_device);
618 void handle_quota_options(ext2_filsys fs)
624 if (!usrquota && !grpquota)
628 init_quota_context(&qctx, fs, -1);
630 if (usrquota == QOPT_ENABLE && !fs->super->s_usr_quota_inum) {
631 if ((qf_ino = quota_file_exists(fs, USRQUOTA, QFMT_VFS_V1)) > 0)
632 set_sb_quota_inum(fs, qf_ino, USRQUOTA);
634 write_quota_inode(qctx, USRQUOTA);
635 } else if (usrquota == QOPT_DISABLE) {
636 remove_quota_inode(fs, USRQUOTA);
639 if (grpquota == QOPT_ENABLE && !fs->super->s_grp_quota_inum) {
640 if ((qf_ino = quota_file_exists(fs, GRPQUOTA, QFMT_VFS_V1)) > 0)
641 set_sb_quota_inum(fs, qf_ino, GRPQUOTA);
643 write_quota_inode(qctx, GRPQUOTA);
644 } else if (grpquota == QOPT_DISABLE) {
645 remove_quota_inode(fs, GRPQUOTA);
648 release_quota_context(&qctx);
650 if ((usrquota == QOPT_ENABLE) || (grpquota == QOPT_ENABLE)) {
651 fs->super->s_feature_ro_compat |= EXT4_FEATURE_RO_COMPAT_QUOTA;
652 ext2fs_mark_super_dirty(fs);
653 } else if ((usrquota == QOPT_DISABLE) && (grpquota == QOPT_DISABLE)) {
654 fs->super->s_feature_ro_compat &= ~EXT4_FEATURE_RO_COMPAT_QUOTA;
655 ext2fs_mark_super_dirty(fs);
661 void parse_quota_opts(const char *opts)
663 char *buf, *token, *next, *p, *arg;
669 fputs(_("Couldn't allocate memory to parse quota "
670 "options!\n"), stderr);
674 for (token = buf; token && *token; token = next) {
675 p = strchr(token, ',');
682 if (strcmp(token, "usrquota") == 0) {
683 usrquota = QOPT_ENABLE;
684 } else if (strcmp(token, "^usrquota") == 0) {
685 usrquota = QOPT_DISABLE;
686 } else if (strcmp(token, "grpquota") == 0) {
687 grpquota = QOPT_ENABLE;
688 } else if (strcmp(token, "^grpquota") == 0) {
689 grpquota = QOPT_DISABLE;
691 fputs(_("\nBad quota options specified.\n\n"
692 "Following valid quota options are available "
693 "(pass by separating with comma):\n"
706 static void parse_e2label_options(int argc, char ** argv)
708 if ((argc < 2) || (argc > 3)) {
709 fputs(_("Usage: e2label device [newlabel]\n"), stderr);
712 io_options = strchr(argv[1], '?');
715 device_name = blkid_get_devname(NULL, argv[1], NULL);
717 com_err("e2label", 0, _("Unable to resolve '%s'"),
721 open_flag = EXT2_FLAG_JOURNAL_DEV_OK;
723 open_flag |= EXT2_FLAG_RW;
730 static time_t parse_time(char *str)
734 if (strcmp(str, "now") == 0) {
737 memset(&ts, 0, sizeof(ts));
739 strptime(str, "%Y%m%d%H%M%S", &ts);
741 sscanf(str, "%4d%2d%2d%2d%2d%2d", &ts.tm_year, &ts.tm_mon,
742 &ts.tm_mday, &ts.tm_hour, &ts.tm_min, &ts.tm_sec);
745 if (ts.tm_year < 0 || ts.tm_mon < 0 || ts.tm_mon > 11 ||
746 ts.tm_mday < 0 || ts.tm_mday > 31 || ts.tm_hour > 23 ||
747 ts.tm_min > 59 || ts.tm_sec > 61)
750 if (ts.tm_mday == 0) {
751 com_err(program_name, 0,
752 _("Couldn't parse date/time specifier: %s"),
757 return (mktime(&ts));
760 static void parse_tune2fs_options(int argc, char **argv)
769 printf("tune2fs %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
770 while ((c = getopt(argc, argv, "c:e:fg:i:jlm:o:r:s:u:C:E:I:J:L:M:O:Q:T:U:")) != EOF)
773 max_mount_count = strtol(optarg, &tmp, 0);
774 if (*tmp || max_mount_count > 16000) {
775 com_err(program_name, 0,
776 _("bad mounts count - %s"),
780 if (max_mount_count == 0)
781 max_mount_count = -1;
783 open_flag = EXT2_FLAG_RW;
786 mount_count = strtoul(optarg, &tmp, 0);
787 if (*tmp || mount_count > 16000) {
788 com_err(program_name, 0,
789 _("bad mounts count - %s"),
794 open_flag = EXT2_FLAG_RW;
797 if (strcmp(optarg, "continue") == 0)
798 errors = EXT2_ERRORS_CONTINUE;
799 else if (strcmp(optarg, "remount-ro") == 0)
800 errors = EXT2_ERRORS_RO;
801 else if (strcmp(optarg, "panic") == 0)
802 errors = EXT2_ERRORS_PANIC;
804 com_err(program_name, 0,
805 _("bad error behavior - %s"),
810 open_flag = EXT2_FLAG_RW;
813 extended_cmd = optarg;
814 open_flag |= EXT2_FLAG_RW;
816 case 'f': /* Force */
820 resgid = strtoul(optarg, &tmp, 0);
822 gr = getgrnam(optarg);
831 com_err(program_name, 0,
832 _("bad gid/group name - %s"),
837 open_flag = EXT2_FLAG_RW;
840 interval = strtoul(optarg, &tmp, 0);
853 case 'M': /* months! */
854 interval *= 86400 * 30;
858 case 'W': /* weeks */
859 interval *= 86400 * 7;
864 com_err(program_name, 0,
865 _("bad interval - %s"), optarg);
869 open_flag = EXT2_FLAG_RW;
874 open_flag = EXT2_FLAG_RW;
877 parse_journal_opts(optarg);
878 open_flag = EXT2_FLAG_RW;
886 open_flag |= EXT2_FLAG_RW |
887 EXT2_FLAG_JOURNAL_DEV_OK;
890 reserved_ratio = strtod(optarg, &tmp);
891 if (*tmp || reserved_ratio > 50 ||
892 reserved_ratio < 0) {
893 com_err(program_name, 0,
894 _("bad reserved block ratio - %s"),
899 open_flag = EXT2_FLAG_RW;
902 new_last_mounted = optarg;
904 open_flag = EXT2_FLAG_RW;
908 com_err(program_name, 0,
909 _("-o may only be specified once"));
912 mntopts_cmd = optarg;
913 open_flag = EXT2_FLAG_RW;
918 com_err(program_name, 0,
919 _("-O may only be specified once"));
922 features_cmd = optarg;
923 open_flag = EXT2_FLAG_RW;
927 parse_quota_opts(optarg);
928 open_flag = EXT2_FLAG_RW;
931 reserved_blocks = strtoul(optarg, &tmp, 0);
933 com_err(program_name, 0,
934 _("bad reserved blocks count - %s"),
939 open_flag = EXT2_FLAG_RW;
941 case 's': /* Deprecated */
942 s_flag = atoi(optarg);
943 open_flag = EXT2_FLAG_RW;
947 last_check_time = parse_time(optarg);
948 open_flag = EXT2_FLAG_RW;
951 resuid = strtoul(optarg, &tmp, 0);
953 pw = getpwnam(optarg);
962 com_err(program_name, 0,
963 _("bad uid/user name - %s"),
968 open_flag = EXT2_FLAG_RW;
973 open_flag = EXT2_FLAG_RW |
974 EXT2_FLAG_JOURNAL_DEV_OK;
977 new_inode_size = strtoul(optarg, &tmp, 0);
979 com_err(program_name, 0,
980 _("bad inode size - %s"),
984 if (!((new_inode_size &
985 (new_inode_size - 1)) == 0)) {
986 com_err(program_name, 0,
987 _("Inode size must be a "
992 open_flag = EXT2_FLAG_RW;
998 if (optind < argc - 1 || optind == argc)
1000 if (!open_flag && !l_flag)
1002 io_options = strchr(argv[optind], '?');
1005 device_name = blkid_get_devname(NULL, argv[optind], NULL);
1007 com_err("tune2fs", 0, _("Unable to resolve '%s'"),
1013 #ifdef CONFIG_BUILD_FINDFS
1014 void do_findfs(int argc, char **argv)
1019 (strncmp(argv[1], "LABEL=", 6) && strncmp(argv[1], "UUID=", 5))) {
1020 fprintf(stderr, "Usage: findfs LABEL=<label>|UUID=<uuid>\n");
1023 dev = blkid_get_devname(NULL, argv[1], NULL);
1025 com_err("findfs", 0, _("Unable to resolve '%s'"),
1034 static void parse_extended_opts(ext2_filsys fs, const char *opts)
1036 char *buf, *token, *next, *p, *arg;
1041 buf = malloc(len+1);
1044 _("Couldn't allocate memory to parse options!\n"));
1048 for (token = buf; token && *token; token = next) {
1049 p = strchr(token, ',');
1055 arg = strchr(token, '=');
1060 if (!strcmp(token, "test_fs")) {
1061 fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
1062 printf("Setting test filesystem flag\n");
1063 ext2fs_mark_super_dirty(fs);
1064 } else if (!strcmp(token, "^test_fs")) {
1065 fs->super->s_flags &= ~EXT2_FLAGS_TEST_FILESYS;
1066 printf("Clearing test filesystem flag\n");
1067 ext2fs_mark_super_dirty(fs);
1068 } else if (strcmp(token, "stride") == 0) {
1073 stride = strtoul(arg, &p, 0);
1076 _("Invalid RAID stride: %s\n"),
1082 } else if (strcmp(token, "stripe-width") == 0 ||
1083 strcmp(token, "stripe_width") == 0) {
1088 stripe_width = strtoul(arg, &p, 0);
1091 _("Invalid RAID stripe-width: %s\n"),
1096 stripe_width_set = 1;
1097 } else if (strcmp(token, "hash_alg") == 0 ||
1098 strcmp(token, "hash-alg") == 0) {
1103 hash_alg = e2p_string2hash(arg);
1106 _("Invalid hash algorithm: %s\n"),
1111 fs->super->s_def_hash_version = hash_alg;
1112 printf(_("Setting default hash algorithm "
1115 ext2fs_mark_super_dirty(fs);
1116 } else if (!strcmp(token, "mount_opts")) {
1121 if (strlen(arg) >= sizeof(fs->super->s_mount_opts)) {
1123 "Extended mount options too long\n");
1126 ext_mount_opts = strdup(arg);
1131 fprintf(stderr, _("\nBad options specified.\n\n"
1132 "Extended options are separated by commas, "
1133 "and may take an argument which\n"
1134 "\tis set off by an equals ('=') sign.\n\n"
1135 "Valid extended options are:\n"
1136 "\thash_alg=<hash algorithm>\n"
1137 "\tmount_opts=<extended default mount options>\n"
1138 "\tstride=<RAID per-disk chunk size in blocks>\n"
1139 "\tstripe_width=<RAID stride*data disks in blocks>\n"
1149 * Fill in the block bitmap bmap with the information regarding the
1150 * blocks to be moved
1152 static int get_move_bitmaps(ext2_filsys fs, int new_ino_blks_per_grp,
1153 ext2fs_block_bitmap bmap)
1157 ext2_badblocks_list bb_list = 0;
1158 blk64_t j, needed_blocks = 0;
1159 blk64_t start_blk, end_blk;
1161 retval = ext2fs_read_bb_inode(fs, &bb_list);
1165 for (i = 0; i < fs->group_desc_count; i++) {
1166 start_blk = ext2fs_inode_table_loc(fs, i) +
1167 fs->inode_blocks_per_group;
1169 end_blk = ext2fs_inode_table_loc(fs, i) +
1170 new_ino_blks_per_grp;
1172 for (j = start_blk; j < end_blk; j++) {
1173 if (ext2fs_test_block_bitmap2(fs->block_map, j)) {
1175 * IF the block is a bad block we fail
1177 if (ext2fs_badblocks_list_test(bb_list, j)) {
1178 ext2fs_badblocks_list_free(bb_list);
1182 ext2fs_mark_block_bitmap2(bmap, j);
1185 * We are going to use this block for
1186 * inode table. So mark them used.
1188 ext2fs_mark_block_bitmap2(fs->block_map, j);
1191 needed_blocks += end_blk - start_blk;
1194 ext2fs_badblocks_list_free(bb_list);
1195 if (needed_blocks > ext2fs_free_blocks_count(fs->super))
1201 static int ext2fs_is_meta_block(ext2_filsys fs, blk_t blk)
1204 group = ext2fs_group_of_blk(fs, blk);
1205 if (ext2fs_block_bitmap_loc(fs, group) == blk)
1207 if (ext2fs_inode_bitmap_loc(fs, group) == blk)
1212 static int ext2fs_is_block_in_group(ext2_filsys fs, dgrp_t group, blk_t blk)
1214 blk_t start_blk, end_blk;
1215 start_blk = fs->super->s_first_data_block +
1216 EXT2_BLOCKS_PER_GROUP(fs->super) * group;
1218 * We cannot get new block beyond end_blk for for the last block group
1219 * so we can check with EXT2_BLOCKS_PER_GROUP even for last block group
1221 end_blk = start_blk + EXT2_BLOCKS_PER_GROUP(fs->super);
1222 if (blk >= start_blk && blk <= end_blk)
1227 static int move_block(ext2_filsys fs, ext2fs_block_bitmap bmap)
1234 blk64_t blk, new_blk, goal;
1235 struct blk_move *bmv;
1237 retval = ext2fs_get_mem(fs->blocksize, &buf);
1241 for (new_blk = blk = fs->super->s_first_data_block;
1242 blk < ext2fs_blocks_count(fs->super); blk++) {
1243 if (!ext2fs_test_block_bitmap2(bmap, blk))
1246 if (ext2fs_is_meta_block(fs, blk)) {
1248 * If the block is mapping a fs meta data block
1249 * like group desc/block bitmap/inode bitmap. We
1250 * should find a block in the same group and fix
1251 * the respective fs metadata pointers. Otherwise
1254 group = ext2fs_group_of_blk(fs, blk);
1255 goal = ext2fs_group_first_block2(fs, group);
1261 retval = ext2fs_new_block2(fs, goal, NULL, &new_blk);
1265 /* new fs meta data block should be in the same group */
1266 if (meta_data && !ext2fs_is_block_in_group(fs, group, new_blk)) {
1271 /* Mark this block as allocated */
1272 ext2fs_mark_block_bitmap2(fs->block_map, new_blk);
1274 /* Add it to block move list */
1275 retval = ext2fs_get_mem(sizeof(struct blk_move), &bmv);
1280 bmv->new_loc = new_blk;
1282 list_add(&(bmv->list), &blk_move_list);
1284 retval = io_channel_read_blk64(fs->io, blk, 1, buf);
1288 retval = io_channel_write_blk64(fs->io, new_blk, 1, buf);
1294 ext2fs_free_mem(&buf);
1298 static blk64_t translate_block(blk64_t blk)
1300 struct list_head *entry;
1301 struct blk_move *bmv;
1303 list_for_each(entry, &blk_move_list) {
1304 bmv = list_entry(entry, struct blk_move, list);
1305 if (bmv->old_loc == blk)
1306 return bmv->new_loc;
1312 static int process_block(ext2_filsys fs EXT2FS_ATTR((unused)),
1314 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1315 blk64_t ref_block EXT2FS_ATTR((unused)),
1316 int ref_offset EXT2FS_ATTR((unused)),
1321 ext2fs_block_bitmap bmap = (ext2fs_block_bitmap) priv_data;
1323 if (!ext2fs_test_block_bitmap2(bmap, *block_nr))
1325 new_blk = translate_block(*block_nr);
1327 *block_nr = new_blk;
1329 * This will force the ext2fs_write_inode in the iterator
1331 ret |= BLOCK_CHANGED;
1337 static int inode_scan_and_fix(ext2_filsys fs, ext2fs_block_bitmap bmap)
1339 errcode_t retval = 0;
1342 char *block_buf = 0;
1343 struct ext2_inode inode;
1344 ext2_inode_scan scan = NULL;
1346 retval = ext2fs_get_mem(fs->blocksize * 3, &block_buf);
1350 retval = ext2fs_open_inode_scan(fs, 0, &scan);
1355 retval = ext2fs_get_next_inode(scan, &ino, &inode);
1362 if (inode.i_links_count == 0)
1363 continue; /* inode not in use */
1366 * If we end up modifying the journal inode
1367 * the sb->s_jnl_blocks will differ. But a
1368 * subsequent e2fsck fixes that.
1369 * Do we need to fix this ??
1372 if (ext2fs_file_acl_block(&inode) &&
1373 ext2fs_test_block_bitmap2(bmap,
1374 ext2fs_file_acl_block(&inode))) {
1375 blk = translate_block(ext2fs_file_acl_block(&inode));
1379 ext2fs_file_acl_block_set(&inode, blk);
1382 * Write the inode to disk so that inode table
1385 retval = ext2fs_write_inode(fs, ino, &inode);
1390 if (!ext2fs_inode_has_valid_blocks(&inode))
1393 retval = ext2fs_block_iterate3(fs, ino, 0, block_buf,
1394 process_block, bmap);
1401 ext2fs_free_mem(&block_buf);
1407 * We need to scan for inode and block bitmaps that may need to be
1408 * moved. This can take place if the filesystem was formatted for
1409 * RAID arrays using the mke2fs's extended option "stride".
1411 static int group_desc_scan_and_fix(ext2_filsys fs, ext2fs_block_bitmap bmap)
1416 for (i = 0; i < fs->group_desc_count; i++) {
1417 blk = ext2fs_block_bitmap_loc(fs, i);
1418 if (ext2fs_test_block_bitmap2(bmap, blk)) {
1419 new_blk = translate_block(blk);
1422 ext2fs_block_bitmap_loc_set(fs, i, new_blk);
1425 blk = ext2fs_inode_bitmap_loc(fs, i);
1426 if (ext2fs_test_block_bitmap2(bmap, blk)) {
1427 new_blk = translate_block(blk);
1430 ext2fs_inode_bitmap_loc_set(fs, i, new_blk);
1436 static int expand_inode_table(ext2_filsys fs, unsigned long new_ino_size)
1441 int new_ino_blks_per_grp;
1443 char *old_itable = NULL, *new_itable = NULL;
1444 char *tmp_old_itable = NULL, *tmp_new_itable = NULL;
1445 unsigned long old_ino_size;
1446 int old_itable_size, new_itable_size;
1448 old_itable_size = fs->inode_blocks_per_group * fs->blocksize;
1449 old_ino_size = EXT2_INODE_SIZE(fs->super);
1451 new_ino_blks_per_grp = ext2fs_div_ceil(
1452 EXT2_INODES_PER_GROUP(fs->super) *
1456 new_itable_size = new_ino_blks_per_grp * fs->blocksize;
1458 retval = ext2fs_get_mem(old_itable_size, &old_itable);
1462 retval = ext2fs_get_mem(new_itable_size, &new_itable);
1466 tmp_old_itable = old_itable;
1467 tmp_new_itable = new_itable;
1469 for (i = 0; i < fs->group_desc_count; i++) {
1470 blk = ext2fs_inode_table_loc(fs, i);
1471 retval = io_channel_read_blk64(fs->io, blk,
1472 fs->inode_blocks_per_group, old_itable);
1476 for (j = 0; j < EXT2_INODES_PER_GROUP(fs->super); j++) {
1477 memcpy(new_itable, old_itable, old_ino_size);
1479 memset(new_itable+old_ino_size, 0,
1480 new_ino_size - old_ino_size);
1482 new_itable += new_ino_size;
1483 old_itable += old_ino_size;
1486 /* reset the pointer */
1487 old_itable = tmp_old_itable;
1488 new_itable = tmp_new_itable;
1490 retval = io_channel_write_blk64(fs->io, blk,
1491 new_ino_blks_per_grp, new_itable);
1496 /* Update the meta data */
1497 fs->inode_blocks_per_group = new_ino_blks_per_grp;
1498 fs->super->s_inode_size = new_ino_size;
1502 ext2fs_free_mem(&old_itable);
1505 ext2fs_free_mem(&new_itable);
1510 static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs)
1514 unsigned int group = 0;
1515 unsigned int count = 0;
1520 * First calculate the block statistics
1522 for (blk = fs->super->s_first_data_block;
1523 blk < ext2fs_blocks_count(fs->super); blk++) {
1524 if (!ext2fs_fast_test_block_bitmap2(fs->block_map, blk)) {
1529 if ((count == fs->super->s_blocks_per_group) ||
1530 (blk == ext2fs_blocks_count(fs->super)-1)) {
1531 ext2fs_bg_free_blocks_count_set(fs, group++,
1537 total_free = EXT2FS_C2B(fs, total_free);
1538 ext2fs_free_blocks_count_set(fs->super, total_free);
1541 * Next, calculate the inode statistics
1548 /* Protect loop from wrap-around if s_inodes_count maxed */
1549 for (ino = 1; ino <= fs->super->s_inodes_count && ino > 0; ino++) {
1550 if (!ext2fs_fast_test_inode_bitmap2(fs->inode_map, ino)) {
1555 if ((count == fs->super->s_inodes_per_group) ||
1556 (ino == fs->super->s_inodes_count)) {
1557 ext2fs_bg_free_inodes_count_set(fs, group++,
1563 fs->super->s_free_inodes_count = total_free;
1564 ext2fs_mark_super_dirty(fs);
1568 #define list_for_each_safe(pos, pnext, head) \
1569 for (pos = (head)->next, pnext = pos->next; pos != (head); \
1570 pos = pnext, pnext = pos->next)
1572 static void free_blk_move_list(void)
1574 struct list_head *entry, *tmp;
1575 struct blk_move *bmv;
1577 list_for_each_safe(entry, tmp, &blk_move_list) {
1578 bmv = list_entry(entry, struct blk_move, list);
1580 ext2fs_free_mem(&bmv);
1585 static int resize_inode(ext2_filsys fs, unsigned long new_size)
1588 int new_ino_blks_per_grp;
1589 ext2fs_block_bitmap bmap;
1591 ext2fs_read_inode_bitmap(fs);
1592 ext2fs_read_block_bitmap(fs);
1593 INIT_LIST_HEAD(&blk_move_list);
1596 new_ino_blks_per_grp = ext2fs_div_ceil(
1597 EXT2_INODES_PER_GROUP(fs->super)*
1601 /* We may change the file system.
1602 * Mark the file system as invalid so that
1603 * the user is prompted to run fsck.
1605 fs->super->s_state &= ~EXT2_VALID_FS;
1607 retval = ext2fs_allocate_block_bitmap(fs, _("blocks to be moved"),
1610 fputs(_("Failed to allocate block bitmap when "
1611 "increasing inode size\n"), stderr);
1614 retval = get_move_bitmaps(fs, new_ino_blks_per_grp, bmap);
1616 fputs(_("Not enough space to increase inode size \n"), stderr);
1619 retval = move_block(fs, bmap);
1621 fputs(_("Failed to relocate blocks during inode resize \n"),
1625 retval = inode_scan_and_fix(fs, bmap);
1629 retval = group_desc_scan_and_fix(fs, bmap);
1633 retval = expand_inode_table(fs, new_size);
1637 ext2fs_calculate_summary_stats(fs);
1639 fs->super->s_state |= EXT2_VALID_FS;
1640 /* mark super block and block bitmap as dirty */
1641 ext2fs_mark_super_dirty(fs);
1642 ext2fs_mark_bb_dirty(fs);
1645 free_blk_move_list();
1646 ext2fs_free_block_bitmap(bmap);
1651 free_blk_move_list();
1652 ext2fs_free_block_bitmap(bmap);
1653 fputs(_("Error in resizing the inode size.\n"
1654 "Run e2undo to undo the "
1655 "file system changes. \n"), stderr);
1660 static int tune2fs_setup_tdb(const char *name, io_manager *io_ptr)
1662 errcode_t retval = 0;
1663 const char *tdb_dir;
1665 char *dev_name, *tmp_name;
1669 * Configuration via a conf file would be
1672 profile_get_string(profile, "scratch_files",
1676 tmp_name = strdup(name);
1679 com_err(program_name, ENOMEM,
1680 _("Couldn't allocate memory for tdb filename\n"));
1683 dev_name = basename(tmp_name);
1685 tdb_dir = getenv("E2FSPROGS_UNDO_DIR");
1687 tdb_dir = "/var/lib/e2fsprogs";
1689 if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
1690 access(tdb_dir, W_OK))
1693 tdb_file = malloc(strlen(tdb_dir) + 9 + strlen(dev_name) + 7 + 1);
1696 sprintf(tdb_file, "%s/tune2fs-%s.e2undo", tdb_dir, dev_name);
1698 if (!access(tdb_file, F_OK)) {
1699 if (unlink(tdb_file) < 0) {
1701 com_err(program_name, retval,
1702 _("while trying to delete %s"),
1709 set_undo_io_backing_manager(*io_ptr);
1710 *io_ptr = undo_io_manager;
1711 set_undo_io_backup_file(tdb_file);
1712 printf(_("To undo the tune2fs operation please run "
1713 "the command\n e2undo %s %s\n\n"),
1720 int main(int argc, char **argv)
1724 struct ext2_super_block *sb;
1725 io_manager io_ptr, io_ptr_orig = NULL;
1728 setlocale(LC_MESSAGES, "");
1729 setlocale(LC_CTYPE, "");
1730 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1731 textdomain(NLS_CAT_NAME);
1734 program_name = *argv;
1735 add_error_table(&et_ext2_error_table);
1737 #ifdef CONFIG_BUILD_FINDFS
1738 if (strcmp(get_progname(argv[0]), "findfs") == 0)
1739 do_findfs(argc, argv);
1741 if (strcmp(get_progname(argv[0]), "e2label") == 0)
1742 parse_e2label_options(argc, argv);
1744 parse_tune2fs_options(argc, argv);
1746 #ifdef CONFIG_TESTIO_DEBUG
1747 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_DEBUG")) {
1748 io_ptr = test_io_manager;
1749 test_io_backing_manager = unix_io_manager;
1752 io_ptr = unix_io_manager;
1755 retval = ext2fs_open2(device_name, io_options, open_flag,
1758 com_err(program_name, retval,
1759 _("while trying to open %s"),
1762 _("Couldn't find valid filesystem superblock.\n"));
1766 if (I_flag && !io_ptr_orig) {
1768 * Check the inode size is right so we can issue an
1769 * error message and bail before setting up the tdb
1772 if (new_inode_size == EXT2_INODE_SIZE(fs->super)) {
1773 fprintf(stderr, _("The inode size is already %lu\n"),
1777 if (new_inode_size < EXT2_INODE_SIZE(fs->super)) {
1778 fprintf(stderr, _("Shrinking the inode size is "
1779 "not supported\n"));
1784 * If inode resize is requested use the
1787 io_ptr_orig = io_ptr;
1788 retval = tune2fs_setup_tdb(device_name, &io_ptr);
1791 if (io_ptr != io_ptr_orig) {
1798 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
1801 /* For e2label emulation */
1802 printf("%.*s\n", (int) sizeof(sb->s_volume_name),
1804 remove_error_table(&et_ext2_error_table);
1808 retval = ext2fs_check_if_mounted(device_name, &mount_flags);
1810 com_err("ext2fs_check_if_mount", retval,
1811 _("while determining whether %s is mounted."),
1815 /* Normally we only need to write out the superblock */
1816 fs->flags |= EXT2_FLAG_SUPER_ONLY;
1819 sb->s_max_mnt_count = max_mount_count;
1820 ext2fs_mark_super_dirty(fs);
1821 printf(_("Setting maximal mount count to %d\n"),
1825 sb->s_mnt_count = mount_count;
1826 ext2fs_mark_super_dirty(fs);
1827 printf(_("Setting current mount count to %d\n"), mount_count);
1830 sb->s_errors = errors;
1831 ext2fs_mark_super_dirty(fs);
1832 printf(_("Setting error behavior to %d\n"), errors);
1835 sb->s_def_resgid = resgid;
1836 ext2fs_mark_super_dirty(fs);
1837 printf(_("Setting reserved blocks gid to %lu\n"), resgid);
1840 if (interval >= (1ULL << 32)) {
1841 com_err(program_name, 0,
1842 _("interval between checks is too big (%lu)"),
1846 sb->s_checkinterval = interval;
1847 ext2fs_mark_super_dirty(fs);
1848 printf(_("Setting interval between checks to %lu seconds\n"),
1852 ext2fs_r_blocks_count_set(sb, reserved_ratio *
1853 ext2fs_blocks_count(sb) / 100.0);
1854 ext2fs_mark_super_dirty(fs);
1855 printf (_("Setting reserved blocks percentage to %g%% (%llu blocks)\n"),
1856 reserved_ratio, ext2fs_r_blocks_count(sb));
1859 if (reserved_blocks > ext2fs_blocks_count(sb)/2) {
1860 com_err(program_name, 0,
1861 _("reserved blocks count is too big (%llu)"),
1865 ext2fs_r_blocks_count_set(sb, reserved_blocks);
1866 ext2fs_mark_super_dirty(fs);
1867 printf(_("Setting reserved blocks count to %llu\n"),
1871 if (sb->s_feature_ro_compat &
1872 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)
1873 fputs(_("\nThe filesystem already has sparse "
1874 "superblocks.\n"), stderr);
1876 sb->s_feature_ro_compat |=
1877 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1878 sb->s_state &= ~EXT2_VALID_FS;
1879 ext2fs_mark_super_dirty(fs);
1880 printf(_("\nSparse superblock flag set. %s"),
1885 fputs(_("\nClearing the sparse superflag not supported.\n"),
1890 sb->s_lastcheck = last_check_time;
1891 ext2fs_mark_super_dirty(fs);
1892 printf(_("Setting time filesystem last checked to %s\n"),
1893 ctime(&last_check_time));
1896 sb->s_def_resuid = resuid;
1897 ext2fs_mark_super_dirty(fs);
1898 printf(_("Setting reserved blocks uid to %lu\n"), resuid);
1901 if (strlen(new_label) > sizeof(sb->s_volume_name))
1902 fputs(_("Warning: label too long, truncating.\n"),
1904 memset(sb->s_volume_name, 0, sizeof(sb->s_volume_name));
1905 strncpy(sb->s_volume_name, new_label,
1906 sizeof(sb->s_volume_name));
1907 ext2fs_mark_super_dirty(fs);
1910 memset(sb->s_last_mounted, 0, sizeof(sb->s_last_mounted));
1911 strncpy(sb->s_last_mounted, new_last_mounted,
1912 sizeof(sb->s_last_mounted));
1913 ext2fs_mark_super_dirty(fs);
1916 update_mntopts(fs, mntopts_cmd);
1918 update_feature_set(fs, features_cmd);
1920 parse_extended_opts(fs, extended_cmd);
1921 if (journal_size || journal_device)
1925 if (mount_flags & EXT2_MF_MOUNTED) {
1926 fputs(_("The quota feature may only be changed when "
1927 "the filesystem is unmounted.\n"), stderr);
1930 handle_quota_options(fs);
1937 if (sb->s_feature_ro_compat &
1938 EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
1940 * Determine if the block group checksums are
1941 * correct so we know whether or not to set
1944 for (i = 0; i < fs->group_desc_count; i++)
1945 if (!ext2fs_group_desc_csum_verify(fs, i))
1947 if (i >= fs->group_desc_count)
1950 if ((strcasecmp(new_UUID, "null") == 0) ||
1951 (strcasecmp(new_UUID, "clear") == 0)) {
1952 uuid_clear(sb->s_uuid);
1953 } else if (strcasecmp(new_UUID, "time") == 0) {
1954 uuid_generate_time(sb->s_uuid);
1955 } else if (strcasecmp(new_UUID, "random") == 0) {
1956 uuid_generate(sb->s_uuid);
1957 } else if (uuid_parse(new_UUID, sb->s_uuid)) {
1958 com_err(program_name, 0, _("Invalid UUID format\n"));
1962 for (i = 0; i < fs->group_desc_count; i++)
1963 ext2fs_group_desc_csum_set(fs, i);
1964 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
1966 ext2fs_mark_super_dirty(fs);
1969 if (mount_flags & EXT2_MF_MOUNTED) {
1970 fputs(_("The inode size may only be "
1971 "changed when the filesystem is "
1972 "unmounted.\n"), stderr);
1975 if (fs->super->s_feature_incompat &
1976 EXT4_FEATURE_INCOMPAT_FLEX_BG) {
1977 fputs(_("Changing the inode size not supported for "
1978 "filesystems with the flex_bg\n"
1979 "feature enabled.\n"),
1984 * We want to update group descriptor also
1985 * with the new free inode count
1987 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
1988 if (resize_inode(fs, new_inode_size) == 0) {
1989 printf(_("Setting inode size %lu\n"),
1997 sb->s_raid_stride = stride;
1998 ext2fs_mark_super_dirty(fs);
1999 printf(_("Setting stride size to %d\n"), stride);
2001 if (stripe_width_set) {
2002 sb->s_raid_stripe_width = stripe_width;
2003 ext2fs_mark_super_dirty(fs);
2004 printf(_("Setting stripe width to %d\n"), stripe_width);
2006 if (ext_mount_opts) {
2007 strncpy(fs->super->s_mount_opts, ext_mount_opts,
2008 sizeof(fs->super->s_mount_opts));
2009 fs->super->s_mount_opts[sizeof(fs->super->s_mount_opts)-1] = 0;
2010 ext2fs_mark_super_dirty(fs);
2011 printf(_("Setting extended default mount options to '%s'\n"),
2013 free(ext_mount_opts);
2016 remove_error_table(&et_ext2_error_table);
2017 return (ext2fs_close(fs) ? 1 : 0);