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->super->s_max_mnt_count,
611 fs->super->s_checkinterval);
615 free(journal_device);
619 void handle_quota_options(ext2_filsys fs)
625 if (!usrquota && !grpquota)
629 init_quota_context(&qctx, fs, -1);
631 if (usrquota == QOPT_ENABLE && !fs->super->s_usr_quota_inum) {
632 if ((qf_ino = quota_file_exists(fs, USRQUOTA, QFMT_VFS_V1)) > 0)
633 set_sb_quota_inum(fs, qf_ino, USRQUOTA);
635 write_quota_inode(qctx, USRQUOTA);
636 } else if (usrquota == QOPT_DISABLE) {
637 remove_quota_inode(fs, USRQUOTA);
640 if (grpquota == QOPT_ENABLE && !fs->super->s_grp_quota_inum) {
641 if ((qf_ino = quota_file_exists(fs, GRPQUOTA, QFMT_VFS_V1)) > 0)
642 set_sb_quota_inum(fs, qf_ino, GRPQUOTA);
644 write_quota_inode(qctx, GRPQUOTA);
645 } else if (grpquota == QOPT_DISABLE) {
646 remove_quota_inode(fs, GRPQUOTA);
649 release_quota_context(&qctx);
651 if ((usrquota == QOPT_ENABLE) || (grpquota == QOPT_ENABLE)) {
652 fs->super->s_feature_ro_compat |= EXT4_FEATURE_RO_COMPAT_QUOTA;
653 ext2fs_mark_super_dirty(fs);
654 } else if ((usrquota == QOPT_DISABLE) && (grpquota == QOPT_DISABLE)) {
655 fs->super->s_feature_ro_compat &= ~EXT4_FEATURE_RO_COMPAT_QUOTA;
656 ext2fs_mark_super_dirty(fs);
662 void parse_quota_opts(const char *opts)
664 char *buf, *token, *next, *p, *arg;
670 fputs(_("Couldn't allocate memory to parse quota "
671 "options!\n"), stderr);
675 for (token = buf; token && *token; token = next) {
676 p = strchr(token, ',');
683 if (strcmp(token, "usrquota") == 0) {
684 usrquota = QOPT_ENABLE;
685 } else if (strcmp(token, "^usrquota") == 0) {
686 usrquota = QOPT_DISABLE;
687 } else if (strcmp(token, "grpquota") == 0) {
688 grpquota = QOPT_ENABLE;
689 } else if (strcmp(token, "^grpquota") == 0) {
690 grpquota = QOPT_DISABLE;
692 fputs(_("\nBad quota options specified.\n\n"
693 "Following valid quota options are available "
694 "(pass by separating with comma):\n"
707 static void parse_e2label_options(int argc, char ** argv)
709 if ((argc < 2) || (argc > 3)) {
710 fputs(_("Usage: e2label device [newlabel]\n"), stderr);
713 io_options = strchr(argv[1], '?');
716 device_name = blkid_get_devname(NULL, argv[1], NULL);
718 com_err("e2label", 0, _("Unable to resolve '%s'"),
722 open_flag = EXT2_FLAG_JOURNAL_DEV_OK;
724 open_flag |= EXT2_FLAG_RW;
731 static time_t parse_time(char *str)
735 if (strcmp(str, "now") == 0) {
738 memset(&ts, 0, sizeof(ts));
740 strptime(str, "%Y%m%d%H%M%S", &ts);
742 sscanf(str, "%4d%2d%2d%2d%2d%2d", &ts.tm_year, &ts.tm_mon,
743 &ts.tm_mday, &ts.tm_hour, &ts.tm_min, &ts.tm_sec);
746 if (ts.tm_year < 0 || ts.tm_mon < 0 || ts.tm_mon > 11 ||
747 ts.tm_mday < 0 || ts.tm_mday > 31 || ts.tm_hour > 23 ||
748 ts.tm_min > 59 || ts.tm_sec > 61)
751 if (ts.tm_mday == 0) {
752 com_err(program_name, 0,
753 _("Couldn't parse date/time specifier: %s"),
758 return (mktime(&ts));
761 static void parse_tune2fs_options(int argc, char **argv)
770 printf("tune2fs %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
771 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)
774 max_mount_count = strtol(optarg, &tmp, 0);
775 if (*tmp || max_mount_count > 16000) {
776 com_err(program_name, 0,
777 _("bad mounts count - %s"),
781 if (max_mount_count == 0)
782 max_mount_count = -1;
784 open_flag = EXT2_FLAG_RW;
787 mount_count = strtoul(optarg, &tmp, 0);
788 if (*tmp || mount_count > 16000) {
789 com_err(program_name, 0,
790 _("bad mounts count - %s"),
795 open_flag = EXT2_FLAG_RW;
798 if (strcmp(optarg, "continue") == 0)
799 errors = EXT2_ERRORS_CONTINUE;
800 else if (strcmp(optarg, "remount-ro") == 0)
801 errors = EXT2_ERRORS_RO;
802 else if (strcmp(optarg, "panic") == 0)
803 errors = EXT2_ERRORS_PANIC;
805 com_err(program_name, 0,
806 _("bad error behavior - %s"),
811 open_flag = EXT2_FLAG_RW;
814 extended_cmd = optarg;
815 open_flag |= EXT2_FLAG_RW;
817 case 'f': /* Force */
821 resgid = strtoul(optarg, &tmp, 0);
823 gr = getgrnam(optarg);
832 com_err(program_name, 0,
833 _("bad gid/group name - %s"),
838 open_flag = EXT2_FLAG_RW;
841 interval = strtoul(optarg, &tmp, 0);
854 case 'M': /* months! */
855 interval *= 86400 * 30;
859 case 'W': /* weeks */
860 interval *= 86400 * 7;
865 com_err(program_name, 0,
866 _("bad interval - %s"), optarg);
870 open_flag = EXT2_FLAG_RW;
875 open_flag = EXT2_FLAG_RW;
878 parse_journal_opts(optarg);
879 open_flag = EXT2_FLAG_RW;
887 open_flag |= EXT2_FLAG_RW |
888 EXT2_FLAG_JOURNAL_DEV_OK;
891 reserved_ratio = strtod(optarg, &tmp);
892 if (*tmp || reserved_ratio > 50 ||
893 reserved_ratio < 0) {
894 com_err(program_name, 0,
895 _("bad reserved block ratio - %s"),
900 open_flag = EXT2_FLAG_RW;
903 new_last_mounted = optarg;
905 open_flag = EXT2_FLAG_RW;
909 com_err(program_name, 0,
910 _("-o may only be specified once"));
913 mntopts_cmd = optarg;
914 open_flag = EXT2_FLAG_RW;
919 com_err(program_name, 0,
920 _("-O may only be specified once"));
923 features_cmd = optarg;
924 open_flag = EXT2_FLAG_RW;
928 parse_quota_opts(optarg);
929 open_flag = EXT2_FLAG_RW;
932 reserved_blocks = strtoul(optarg, &tmp, 0);
934 com_err(program_name, 0,
935 _("bad reserved blocks count - %s"),
940 open_flag = EXT2_FLAG_RW;
942 case 's': /* Deprecated */
943 s_flag = atoi(optarg);
944 open_flag = EXT2_FLAG_RW;
948 last_check_time = parse_time(optarg);
949 open_flag = EXT2_FLAG_RW;
952 resuid = strtoul(optarg, &tmp, 0);
954 pw = getpwnam(optarg);
963 com_err(program_name, 0,
964 _("bad uid/user name - %s"),
969 open_flag = EXT2_FLAG_RW;
974 open_flag = EXT2_FLAG_RW |
975 EXT2_FLAG_JOURNAL_DEV_OK;
978 new_inode_size = strtoul(optarg, &tmp, 0);
980 com_err(program_name, 0,
981 _("bad inode size - %s"),
985 if (!((new_inode_size &
986 (new_inode_size - 1)) == 0)) {
987 com_err(program_name, 0,
988 _("Inode size must be a "
993 open_flag = EXT2_FLAG_RW;
999 if (optind < argc - 1 || optind == argc)
1001 if (!open_flag && !l_flag)
1003 io_options = strchr(argv[optind], '?');
1006 device_name = blkid_get_devname(NULL, argv[optind], NULL);
1008 com_err("tune2fs", 0, _("Unable to resolve '%s'"),
1014 #ifdef CONFIG_BUILD_FINDFS
1015 void do_findfs(int argc, char **argv)
1020 (strncmp(argv[1], "LABEL=", 6) && strncmp(argv[1], "UUID=", 5))) {
1021 fprintf(stderr, "Usage: findfs LABEL=<label>|UUID=<uuid>\n");
1024 dev = blkid_get_devname(NULL, argv[1], NULL);
1026 com_err("findfs", 0, _("Unable to resolve '%s'"),
1035 static void parse_extended_opts(ext2_filsys fs, const char *opts)
1037 char *buf, *token, *next, *p, *arg;
1042 buf = malloc(len+1);
1045 _("Couldn't allocate memory to parse options!\n"));
1049 for (token = buf; token && *token; token = next) {
1050 p = strchr(token, ',');
1056 arg = strchr(token, '=');
1061 if (!strcmp(token, "test_fs")) {
1062 fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
1063 printf("Setting test filesystem flag\n");
1064 ext2fs_mark_super_dirty(fs);
1065 } else if (!strcmp(token, "^test_fs")) {
1066 fs->super->s_flags &= ~EXT2_FLAGS_TEST_FILESYS;
1067 printf("Clearing test filesystem flag\n");
1068 ext2fs_mark_super_dirty(fs);
1069 } else if (strcmp(token, "stride") == 0) {
1074 stride = strtoul(arg, &p, 0);
1077 _("Invalid RAID stride: %s\n"),
1083 } else if (strcmp(token, "stripe-width") == 0 ||
1084 strcmp(token, "stripe_width") == 0) {
1089 stripe_width = strtoul(arg, &p, 0);
1092 _("Invalid RAID stripe-width: %s\n"),
1097 stripe_width_set = 1;
1098 } else if (strcmp(token, "hash_alg") == 0 ||
1099 strcmp(token, "hash-alg") == 0) {
1104 hash_alg = e2p_string2hash(arg);
1107 _("Invalid hash algorithm: %s\n"),
1112 fs->super->s_def_hash_version = hash_alg;
1113 printf(_("Setting default hash algorithm "
1116 ext2fs_mark_super_dirty(fs);
1117 } else if (!strcmp(token, "mount_opts")) {
1122 if (strlen(arg) >= sizeof(fs->super->s_mount_opts)) {
1124 "Extended mount options too long\n");
1127 ext_mount_opts = strdup(arg);
1132 fprintf(stderr, _("\nBad options specified.\n\n"
1133 "Extended options are separated by commas, "
1134 "and may take an argument which\n"
1135 "\tis set off by an equals ('=') sign.\n\n"
1136 "Valid extended options are:\n"
1137 "\thash_alg=<hash algorithm>\n"
1138 "\tmount_opts=<extended default mount options>\n"
1139 "\tstride=<RAID per-disk chunk size in blocks>\n"
1140 "\tstripe_width=<RAID stride*data disks in blocks>\n"
1150 * Fill in the block bitmap bmap with the information regarding the
1151 * blocks to be moved
1153 static int get_move_bitmaps(ext2_filsys fs, int new_ino_blks_per_grp,
1154 ext2fs_block_bitmap bmap)
1158 ext2_badblocks_list bb_list = 0;
1159 blk64_t j, needed_blocks = 0;
1160 blk64_t start_blk, end_blk;
1162 retval = ext2fs_read_bb_inode(fs, &bb_list);
1166 for (i = 0; i < fs->group_desc_count; i++) {
1167 start_blk = ext2fs_inode_table_loc(fs, i) +
1168 fs->inode_blocks_per_group;
1170 end_blk = ext2fs_inode_table_loc(fs, i) +
1171 new_ino_blks_per_grp;
1173 for (j = start_blk; j < end_blk; j++) {
1174 if (ext2fs_test_block_bitmap2(fs->block_map, j)) {
1176 * IF the block is a bad block we fail
1178 if (ext2fs_badblocks_list_test(bb_list, j)) {
1179 ext2fs_badblocks_list_free(bb_list);
1183 ext2fs_mark_block_bitmap2(bmap, j);
1186 * We are going to use this block for
1187 * inode table. So mark them used.
1189 ext2fs_mark_block_bitmap2(fs->block_map, j);
1192 needed_blocks += end_blk - start_blk;
1195 ext2fs_badblocks_list_free(bb_list);
1196 if (needed_blocks > ext2fs_free_blocks_count(fs->super))
1202 static int ext2fs_is_meta_block(ext2_filsys fs, blk_t blk)
1205 group = ext2fs_group_of_blk(fs, blk);
1206 if (ext2fs_block_bitmap_loc(fs, group) == blk)
1208 if (ext2fs_inode_bitmap_loc(fs, group) == blk)
1213 static int ext2fs_is_block_in_group(ext2_filsys fs, dgrp_t group, blk_t blk)
1215 blk_t start_blk, end_blk;
1216 start_blk = fs->super->s_first_data_block +
1217 EXT2_BLOCKS_PER_GROUP(fs->super) * group;
1219 * We cannot get new block beyond end_blk for for the last block group
1220 * so we can check with EXT2_BLOCKS_PER_GROUP even for last block group
1222 end_blk = start_blk + EXT2_BLOCKS_PER_GROUP(fs->super);
1223 if (blk >= start_blk && blk <= end_blk)
1228 static int move_block(ext2_filsys fs, ext2fs_block_bitmap bmap)
1235 blk64_t blk, new_blk, goal;
1236 struct blk_move *bmv;
1238 retval = ext2fs_get_mem(fs->blocksize, &buf);
1242 for (new_blk = blk = fs->super->s_first_data_block;
1243 blk < ext2fs_blocks_count(fs->super); blk++) {
1244 if (!ext2fs_test_block_bitmap2(bmap, blk))
1247 if (ext2fs_is_meta_block(fs, blk)) {
1249 * If the block is mapping a fs meta data block
1250 * like group desc/block bitmap/inode bitmap. We
1251 * should find a block in the same group and fix
1252 * the respective fs metadata pointers. Otherwise
1255 group = ext2fs_group_of_blk(fs, blk);
1256 goal = ext2fs_group_first_block2(fs, group);
1262 retval = ext2fs_new_block2(fs, goal, NULL, &new_blk);
1266 /* new fs meta data block should be in the same group */
1267 if (meta_data && !ext2fs_is_block_in_group(fs, group, new_blk)) {
1272 /* Mark this block as allocated */
1273 ext2fs_mark_block_bitmap2(fs->block_map, new_blk);
1275 /* Add it to block move list */
1276 retval = ext2fs_get_mem(sizeof(struct blk_move), &bmv);
1281 bmv->new_loc = new_blk;
1283 list_add(&(bmv->list), &blk_move_list);
1285 retval = io_channel_read_blk64(fs->io, blk, 1, buf);
1289 retval = io_channel_write_blk64(fs->io, new_blk, 1, buf);
1295 ext2fs_free_mem(&buf);
1299 static blk64_t translate_block(blk64_t blk)
1301 struct list_head *entry;
1302 struct blk_move *bmv;
1304 list_for_each(entry, &blk_move_list) {
1305 bmv = list_entry(entry, struct blk_move, list);
1306 if (bmv->old_loc == blk)
1307 return bmv->new_loc;
1313 static int process_block(ext2_filsys fs EXT2FS_ATTR((unused)),
1315 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1316 blk64_t ref_block EXT2FS_ATTR((unused)),
1317 int ref_offset EXT2FS_ATTR((unused)),
1322 ext2fs_block_bitmap bmap = (ext2fs_block_bitmap) priv_data;
1324 if (!ext2fs_test_block_bitmap2(bmap, *block_nr))
1326 new_blk = translate_block(*block_nr);
1328 *block_nr = new_blk;
1330 * This will force the ext2fs_write_inode in the iterator
1332 ret |= BLOCK_CHANGED;
1338 static int inode_scan_and_fix(ext2_filsys fs, ext2fs_block_bitmap bmap)
1340 errcode_t retval = 0;
1343 char *block_buf = 0;
1344 struct ext2_inode inode;
1345 ext2_inode_scan scan = NULL;
1347 retval = ext2fs_get_mem(fs->blocksize * 3, &block_buf);
1351 retval = ext2fs_open_inode_scan(fs, 0, &scan);
1356 retval = ext2fs_get_next_inode(scan, &ino, &inode);
1363 if (inode.i_links_count == 0)
1364 continue; /* inode not in use */
1367 * If we end up modifying the journal inode
1368 * the sb->s_jnl_blocks will differ. But a
1369 * subsequent e2fsck fixes that.
1370 * Do we need to fix this ??
1373 if (ext2fs_file_acl_block(&inode) &&
1374 ext2fs_test_block_bitmap2(bmap,
1375 ext2fs_file_acl_block(&inode))) {
1376 blk = translate_block(ext2fs_file_acl_block(&inode));
1380 ext2fs_file_acl_block_set(&inode, blk);
1383 * Write the inode to disk so that inode table
1386 retval = ext2fs_write_inode(fs, ino, &inode);
1391 if (!ext2fs_inode_has_valid_blocks(&inode))
1394 retval = ext2fs_block_iterate3(fs, ino, 0, block_buf,
1395 process_block, bmap);
1402 ext2fs_free_mem(&block_buf);
1408 * We need to scan for inode and block bitmaps that may need to be
1409 * moved. This can take place if the filesystem was formatted for
1410 * RAID arrays using the mke2fs's extended option "stride".
1412 static int group_desc_scan_and_fix(ext2_filsys fs, ext2fs_block_bitmap bmap)
1417 for (i = 0; i < fs->group_desc_count; i++) {
1418 blk = ext2fs_block_bitmap_loc(fs, i);
1419 if (ext2fs_test_block_bitmap2(bmap, blk)) {
1420 new_blk = translate_block(blk);
1423 ext2fs_block_bitmap_loc_set(fs, i, new_blk);
1426 blk = ext2fs_inode_bitmap_loc(fs, i);
1427 if (ext2fs_test_block_bitmap2(bmap, blk)) {
1428 new_blk = translate_block(blk);
1431 ext2fs_inode_bitmap_loc_set(fs, i, new_blk);
1437 static int expand_inode_table(ext2_filsys fs, unsigned long new_ino_size)
1442 int new_ino_blks_per_grp;
1444 char *old_itable = NULL, *new_itable = NULL;
1445 char *tmp_old_itable = NULL, *tmp_new_itable = NULL;
1446 unsigned long old_ino_size;
1447 int old_itable_size, new_itable_size;
1449 old_itable_size = fs->inode_blocks_per_group * fs->blocksize;
1450 old_ino_size = EXT2_INODE_SIZE(fs->super);
1452 new_ino_blks_per_grp = ext2fs_div_ceil(
1453 EXT2_INODES_PER_GROUP(fs->super) *
1457 new_itable_size = new_ino_blks_per_grp * fs->blocksize;
1459 retval = ext2fs_get_mem(old_itable_size, &old_itable);
1463 retval = ext2fs_get_mem(new_itable_size, &new_itable);
1467 tmp_old_itable = old_itable;
1468 tmp_new_itable = new_itable;
1470 for (i = 0; i < fs->group_desc_count; i++) {
1471 blk = ext2fs_inode_table_loc(fs, i);
1472 retval = io_channel_read_blk64(fs->io, blk,
1473 fs->inode_blocks_per_group, old_itable);
1477 for (j = 0; j < EXT2_INODES_PER_GROUP(fs->super); j++) {
1478 memcpy(new_itable, old_itable, old_ino_size);
1480 memset(new_itable+old_ino_size, 0,
1481 new_ino_size - old_ino_size);
1483 new_itable += new_ino_size;
1484 old_itable += old_ino_size;
1487 /* reset the pointer */
1488 old_itable = tmp_old_itable;
1489 new_itable = tmp_new_itable;
1491 retval = io_channel_write_blk64(fs->io, blk,
1492 new_ino_blks_per_grp, new_itable);
1497 /* Update the meta data */
1498 fs->inode_blocks_per_group = new_ino_blks_per_grp;
1499 fs->super->s_inode_size = new_ino_size;
1503 ext2fs_free_mem(&old_itable);
1506 ext2fs_free_mem(&new_itable);
1511 static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs)
1515 unsigned int group = 0;
1516 unsigned int count = 0;
1521 * First calculate the block statistics
1523 for (blk = fs->super->s_first_data_block;
1524 blk < ext2fs_blocks_count(fs->super); blk++) {
1525 if (!ext2fs_fast_test_block_bitmap2(fs->block_map, blk)) {
1530 if ((count == fs->super->s_blocks_per_group) ||
1531 (blk == ext2fs_blocks_count(fs->super)-1)) {
1532 ext2fs_bg_free_blocks_count_set(fs, group++,
1538 total_free = EXT2FS_C2B(fs, total_free);
1539 ext2fs_free_blocks_count_set(fs->super, total_free);
1542 * Next, calculate the inode statistics
1549 /* Protect loop from wrap-around if s_inodes_count maxed */
1550 for (ino = 1; ino <= fs->super->s_inodes_count && ino > 0; ino++) {
1551 if (!ext2fs_fast_test_inode_bitmap2(fs->inode_map, ino)) {
1556 if ((count == fs->super->s_inodes_per_group) ||
1557 (ino == fs->super->s_inodes_count)) {
1558 ext2fs_bg_free_inodes_count_set(fs, group++,
1564 fs->super->s_free_inodes_count = total_free;
1565 ext2fs_mark_super_dirty(fs);
1569 #define list_for_each_safe(pos, pnext, head) \
1570 for (pos = (head)->next, pnext = pos->next; pos != (head); \
1571 pos = pnext, pnext = pos->next)
1573 static void free_blk_move_list(void)
1575 struct list_head *entry, *tmp;
1576 struct blk_move *bmv;
1578 list_for_each_safe(entry, tmp, &blk_move_list) {
1579 bmv = list_entry(entry, struct blk_move, list);
1581 ext2fs_free_mem(&bmv);
1586 static int resize_inode(ext2_filsys fs, unsigned long new_size)
1589 int new_ino_blks_per_grp;
1590 ext2fs_block_bitmap bmap;
1592 ext2fs_read_inode_bitmap(fs);
1593 ext2fs_read_block_bitmap(fs);
1594 INIT_LIST_HEAD(&blk_move_list);
1597 new_ino_blks_per_grp = ext2fs_div_ceil(
1598 EXT2_INODES_PER_GROUP(fs->super)*
1602 /* We may change the file system.
1603 * Mark the file system as invalid so that
1604 * the user is prompted to run fsck.
1606 fs->super->s_state &= ~EXT2_VALID_FS;
1608 retval = ext2fs_allocate_block_bitmap(fs, _("blocks to be moved"),
1611 fputs(_("Failed to allocate block bitmap when "
1612 "increasing inode size\n"), stderr);
1615 retval = get_move_bitmaps(fs, new_ino_blks_per_grp, bmap);
1617 fputs(_("Not enough space to increase inode size \n"), stderr);
1620 retval = move_block(fs, bmap);
1622 fputs(_("Failed to relocate blocks during inode resize \n"),
1626 retval = inode_scan_and_fix(fs, bmap);
1630 retval = group_desc_scan_and_fix(fs, bmap);
1634 retval = expand_inode_table(fs, new_size);
1638 ext2fs_calculate_summary_stats(fs);
1640 fs->super->s_state |= EXT2_VALID_FS;
1641 /* mark super block and block bitmap as dirty */
1642 ext2fs_mark_super_dirty(fs);
1643 ext2fs_mark_bb_dirty(fs);
1646 free_blk_move_list();
1647 ext2fs_free_block_bitmap(bmap);
1652 free_blk_move_list();
1653 ext2fs_free_block_bitmap(bmap);
1654 fputs(_("Error in resizing the inode size.\n"
1655 "Run e2undo to undo the "
1656 "file system changes. \n"), stderr);
1661 static int tune2fs_setup_tdb(const char *name, io_manager *io_ptr)
1663 errcode_t retval = 0;
1664 const char *tdb_dir;
1666 char *dev_name, *tmp_name;
1670 * Configuration via a conf file would be
1673 profile_get_string(profile, "scratch_files",
1677 tmp_name = strdup(name);
1680 com_err(program_name, ENOMEM,
1681 _("Couldn't allocate memory for tdb filename\n"));
1684 dev_name = basename(tmp_name);
1686 tdb_dir = getenv("E2FSPROGS_UNDO_DIR");
1688 tdb_dir = "/var/lib/e2fsprogs";
1690 if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
1691 access(tdb_dir, W_OK))
1694 tdb_file = malloc(strlen(tdb_dir) + 9 + strlen(dev_name) + 7 + 1);
1697 sprintf(tdb_file, "%s/tune2fs-%s.e2undo", tdb_dir, dev_name);
1699 if (!access(tdb_file, F_OK)) {
1700 if (unlink(tdb_file) < 0) {
1702 com_err(program_name, retval,
1703 _("while trying to delete %s"),
1710 set_undo_io_backing_manager(*io_ptr);
1711 *io_ptr = undo_io_manager;
1712 set_undo_io_backup_file(tdb_file);
1713 printf(_("To undo the tune2fs operation please run "
1714 "the command\n e2undo %s %s\n\n"),
1721 int main(int argc, char **argv)
1725 struct ext2_super_block *sb;
1726 io_manager io_ptr, io_ptr_orig = NULL;
1729 setlocale(LC_MESSAGES, "");
1730 setlocale(LC_CTYPE, "");
1731 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1732 textdomain(NLS_CAT_NAME);
1735 program_name = *argv;
1736 add_error_table(&et_ext2_error_table);
1738 #ifdef CONFIG_BUILD_FINDFS
1739 if (strcmp(get_progname(argv[0]), "findfs") == 0)
1740 do_findfs(argc, argv);
1742 if (strcmp(get_progname(argv[0]), "e2label") == 0)
1743 parse_e2label_options(argc, argv);
1745 parse_tune2fs_options(argc, argv);
1747 #ifdef CONFIG_TESTIO_DEBUG
1748 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_DEBUG")) {
1749 io_ptr = test_io_manager;
1750 test_io_backing_manager = unix_io_manager;
1753 io_ptr = unix_io_manager;
1756 retval = ext2fs_open2(device_name, io_options, open_flag,
1759 com_err(program_name, retval,
1760 _("while trying to open %s"),
1763 _("Couldn't find valid filesystem superblock.\n"));
1767 if (I_flag && !io_ptr_orig) {
1769 * Check the inode size is right so we can issue an
1770 * error message and bail before setting up the tdb
1773 if (new_inode_size == EXT2_INODE_SIZE(fs->super)) {
1774 fprintf(stderr, _("The inode size is already %lu\n"),
1778 if (new_inode_size < EXT2_INODE_SIZE(fs->super)) {
1779 fprintf(stderr, _("Shrinking the inode size is "
1780 "not supported\n"));
1785 * If inode resize is requested use the
1788 io_ptr_orig = io_ptr;
1789 retval = tune2fs_setup_tdb(device_name, &io_ptr);
1792 if (io_ptr != io_ptr_orig) {
1799 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
1802 /* For e2label emulation */
1803 printf("%.*s\n", (int) sizeof(sb->s_volume_name),
1805 remove_error_table(&et_ext2_error_table);
1809 retval = ext2fs_check_if_mounted(device_name, &mount_flags);
1811 com_err("ext2fs_check_if_mount", retval,
1812 _("while determining whether %s is mounted."),
1816 /* Normally we only need to write out the superblock */
1817 fs->flags |= EXT2_FLAG_SUPER_ONLY;
1820 sb->s_max_mnt_count = max_mount_count;
1821 ext2fs_mark_super_dirty(fs);
1822 printf(_("Setting maximal mount count to %d\n"),
1826 sb->s_mnt_count = mount_count;
1827 ext2fs_mark_super_dirty(fs);
1828 printf(_("Setting current mount count to %d\n"), mount_count);
1831 sb->s_errors = errors;
1832 ext2fs_mark_super_dirty(fs);
1833 printf(_("Setting error behavior to %d\n"), errors);
1836 sb->s_def_resgid = resgid;
1837 ext2fs_mark_super_dirty(fs);
1838 printf(_("Setting reserved blocks gid to %lu\n"), resgid);
1841 if (interval >= (1ULL << 32)) {
1842 com_err(program_name, 0,
1843 _("interval between checks is too big (%lu)"),
1847 sb->s_checkinterval = interval;
1848 ext2fs_mark_super_dirty(fs);
1849 printf(_("Setting interval between checks to %lu seconds\n"),
1853 ext2fs_r_blocks_count_set(sb, reserved_ratio *
1854 ext2fs_blocks_count(sb) / 100.0);
1855 ext2fs_mark_super_dirty(fs);
1856 printf (_("Setting reserved blocks percentage to %g%% (%llu blocks)\n"),
1857 reserved_ratio, ext2fs_r_blocks_count(sb));
1860 if (reserved_blocks > ext2fs_blocks_count(sb)/2) {
1861 com_err(program_name, 0,
1862 _("reserved blocks count is too big (%llu)"),
1866 ext2fs_r_blocks_count_set(sb, reserved_blocks);
1867 ext2fs_mark_super_dirty(fs);
1868 printf(_("Setting reserved blocks count to %llu\n"),
1872 if (sb->s_feature_ro_compat &
1873 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)
1874 fputs(_("\nThe filesystem already has sparse "
1875 "superblocks.\n"), stderr);
1877 sb->s_feature_ro_compat |=
1878 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1879 sb->s_state &= ~EXT2_VALID_FS;
1880 ext2fs_mark_super_dirty(fs);
1881 printf(_("\nSparse superblock flag set. %s"),
1886 fputs(_("\nClearing the sparse superflag not supported.\n"),
1891 sb->s_lastcheck = last_check_time;
1892 ext2fs_mark_super_dirty(fs);
1893 printf(_("Setting time filesystem last checked to %s\n"),
1894 ctime(&last_check_time));
1897 sb->s_def_resuid = resuid;
1898 ext2fs_mark_super_dirty(fs);
1899 printf(_("Setting reserved blocks uid to %lu\n"), resuid);
1902 if (strlen(new_label) > sizeof(sb->s_volume_name))
1903 fputs(_("Warning: label too long, truncating.\n"),
1905 memset(sb->s_volume_name, 0, sizeof(sb->s_volume_name));
1906 strncpy(sb->s_volume_name, new_label,
1907 sizeof(sb->s_volume_name));
1908 ext2fs_mark_super_dirty(fs);
1911 memset(sb->s_last_mounted, 0, sizeof(sb->s_last_mounted));
1912 strncpy(sb->s_last_mounted, new_last_mounted,
1913 sizeof(sb->s_last_mounted));
1914 ext2fs_mark_super_dirty(fs);
1917 update_mntopts(fs, mntopts_cmd);
1919 update_feature_set(fs, features_cmd);
1921 parse_extended_opts(fs, extended_cmd);
1922 if (journal_size || journal_device)
1926 if (mount_flags & EXT2_MF_MOUNTED) {
1927 fputs(_("The quota feature may only be changed when "
1928 "the filesystem is unmounted.\n"), stderr);
1931 handle_quota_options(fs);
1938 if (sb->s_feature_ro_compat &
1939 EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
1941 * Determine if the block group checksums are
1942 * correct so we know whether or not to set
1945 for (i = 0; i < fs->group_desc_count; i++)
1946 if (!ext2fs_group_desc_csum_verify(fs, i))
1948 if (i >= fs->group_desc_count)
1951 if ((strcasecmp(new_UUID, "null") == 0) ||
1952 (strcasecmp(new_UUID, "clear") == 0)) {
1953 uuid_clear(sb->s_uuid);
1954 } else if (strcasecmp(new_UUID, "time") == 0) {
1955 uuid_generate_time(sb->s_uuid);
1956 } else if (strcasecmp(new_UUID, "random") == 0) {
1957 uuid_generate(sb->s_uuid);
1958 } else if (uuid_parse(new_UUID, sb->s_uuid)) {
1959 com_err(program_name, 0, _("Invalid UUID format\n"));
1963 for (i = 0; i < fs->group_desc_count; i++)
1964 ext2fs_group_desc_csum_set(fs, i);
1965 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
1967 ext2fs_mark_super_dirty(fs);
1970 if (mount_flags & EXT2_MF_MOUNTED) {
1971 fputs(_("The inode size may only be "
1972 "changed when the filesystem is "
1973 "unmounted.\n"), stderr);
1976 if (fs->super->s_feature_incompat &
1977 EXT4_FEATURE_INCOMPAT_FLEX_BG) {
1978 fputs(_("Changing the inode size not supported for "
1979 "filesystems with the flex_bg\n"
1980 "feature enabled.\n"),
1985 * We want to update group descriptor also
1986 * with the new free inode count
1988 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
1989 if (resize_inode(fs, new_inode_size) == 0) {
1990 printf(_("Setting inode size %lu\n"),
1998 sb->s_raid_stride = stride;
1999 ext2fs_mark_super_dirty(fs);
2000 printf(_("Setting stride size to %d\n"), stride);
2002 if (stripe_width_set) {
2003 sb->s_raid_stripe_width = stripe_width;
2004 ext2fs_mark_super_dirty(fs);
2005 printf(_("Setting stripe width to %d\n"), stripe_width);
2007 if (ext_mount_opts) {
2008 strncpy(fs->super->s_mount_opts, ext_mount_opts,
2009 sizeof(fs->super->s_mount_opts));
2010 fs->super->s_mount_opts[sizeof(fs->super->s_mount_opts)-1] = 0;
2011 ext2fs_mark_super_dirty(fs);
2012 printf(_("Setting extended default mount options to '%s'\n"),
2014 free(ext_mount_opts);
2017 remove_error_table(&et_ext2_error_table);
2018 return (ext2fs_close(fs) ? 1 : 0);