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"
59 #include "../version.h"
60 #include "nls-enable.h"
62 const char *program_name = "tune2fs";
64 char *new_label, *new_last_mounted, *new_UUID;
66 static int c_flag, C_flag, e_flag, f_flag, g_flag, i_flag, l_flag, L_flag;
67 static int m_flag, M_flag, r_flag, s_flag = -1, u_flag, U_flag, T_flag;
69 static time_t last_check_time;
70 static int print_label;
71 static int max_mount_count, mount_count, mount_flags;
72 static unsigned long interval, reserved_blocks;
73 static double reserved_ratio;
74 static unsigned long resgid, resuid;
75 static unsigned short errors;
77 static char *features_cmd;
78 static char *mntopts_cmd;
79 static int stride, stripe_width;
80 static int stride_set, stripe_width_set;
81 static char *extended_cmd;
82 static unsigned long new_inode_size;
84 int journal_size, journal_flags;
87 static struct list_head blk_move_list;
90 struct list_head list;
96 static const char *please_fsck = N_("Please run e2fsck on the filesystem.\n");
98 #ifdef CONFIG_BUILD_FINDFS
99 void do_findfs(int argc, char **argv);
102 static void usage(void)
105 _("Usage: %s [-c max_mounts_count] [-e errors_behavior] "
107 "\t[-i interval[d|m|w]] [-j] [-J journal_options] [-l]\n"
108 "\t[-m reserved_blocks_percent] "
109 "[-o [^]mount_options[,...]] \n"
110 "\t[-r reserved_blocks_count] [-u user] [-C mount_count] "
111 "[-L volume_label]\n"
112 "\t[-M last_mounted_dir] [-O [^]feature[,...]]\n"
113 "\t[-E extended-option[,...]] [-T last_check_time] "
114 "[-U UUID]\n\t[ -I new_inode_size ] device\n"), program_name);
118 static __u32 ok_features[3] = {
120 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
121 EXT2_FEATURE_COMPAT_DIR_INDEX,
123 EXT2_FEATURE_INCOMPAT_FILETYPE |
124 EXT3_FEATURE_INCOMPAT_EXTENTS |
125 EXT4_FEATURE_INCOMPAT_FLEX_BG,
127 EXT2_FEATURE_RO_COMPAT_LARGE_FILE |
128 EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
129 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
130 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
131 EXT4_FEATURE_RO_COMPAT_GDT_CSUM |
132 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
135 static __u32 clear_ok_features[3] = {
137 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
138 EXT2_FEATURE_COMPAT_RESIZE_INODE |
139 EXT2_FEATURE_COMPAT_DIR_INDEX,
141 EXT2_FEATURE_INCOMPAT_FILETYPE |
142 EXT4_FEATURE_INCOMPAT_FLEX_BG,
144 EXT2_FEATURE_RO_COMPAT_LARGE_FILE |
145 EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
146 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
147 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
148 EXT4_FEATURE_RO_COMPAT_GDT_CSUM
152 * Remove an external journal from the filesystem
154 static void remove_journal_device(ext2_filsys fs)
159 journal_superblock_t *jsb;
162 int commit_remove_journal = 0;
166 commit_remove_journal = 1; /* force removal even if error */
168 uuid_unparse(fs->super->s_journal_uuid, buf);
169 journal_path = blkid_get_devname(NULL, "UUID", buf);
173 ext2fs_find_block_device(fs->super->s_journal_dev);
178 #ifdef CONFIG_TESTIO_DEBUG
179 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
180 io_ptr = test_io_manager;
181 test_io_backing_manager = unix_io_manager;
184 io_ptr = unix_io_manager;
185 retval = ext2fs_open(journal_path, EXT2_FLAG_RW|
186 EXT2_FLAG_JOURNAL_DEV_OK, 0,
187 fs->blocksize, io_ptr, &jfs);
189 com_err(program_name, retval,
190 _("while trying to open external journal"));
191 goto no_valid_journal;
193 if (!(jfs->super->s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
194 fprintf(stderr, _("%s is not a journal device.\n"),
196 goto no_valid_journal;
199 /* Get the journal superblock */
200 if ((retval = io_channel_read_blk(jfs->io, 1, -1024, buf))) {
201 com_err(program_name, retval,
202 _("while reading journal superblock"));
203 goto no_valid_journal;
206 jsb = (journal_superblock_t *) buf;
207 if ((jsb->s_header.h_magic != (unsigned) ntohl(JFS_MAGIC_NUMBER)) ||
208 (jsb->s_header.h_blocktype != (unsigned) ntohl(JFS_SUPERBLOCK_V2))) {
209 fputs(_("Journal superblock not found!\n"), stderr);
210 goto no_valid_journal;
213 /* Find the filesystem UUID */
214 nr_users = ntohl(jsb->s_nr_users);
215 for (i = 0; i < nr_users; i++) {
216 if (memcmp(fs->super->s_uuid,
217 &jsb->s_users[i*16], 16) == 0)
221 fputs(_("Filesystem's UUID not found on journal device.\n"),
223 commit_remove_journal = 1;
224 goto no_valid_journal;
227 for (i = 0; i < nr_users; i++)
228 memcpy(&jsb->s_users[i*16], &jsb->s_users[(i+1)*16], 16);
229 jsb->s_nr_users = htonl(nr_users);
231 /* Write back the journal superblock */
232 if ((retval = io_channel_write_blk(jfs->io, 1, -1024, buf))) {
233 com_err(program_name, retval,
234 "while writing journal superblock.");
235 goto no_valid_journal;
238 commit_remove_journal = 1;
241 if (commit_remove_journal == 0) {
242 fputs(_("Journal NOT removed\n"), stderr);
245 fs->super->s_journal_dev = 0;
246 uuid_clear(fs->super->s_journal_uuid);
247 ext2fs_mark_super_dirty(fs);
248 fputs(_("Journal removed\n"), stdout);
252 /* Helper function for remove_journal_inode */
253 static int release_blocks_proc(ext2_filsys fs, blk_t *blocknr,
254 int blockcnt EXT2FS_ATTR((unused)),
255 void *private EXT2FS_ATTR((unused)))
261 ext2fs_unmark_block_bitmap(fs->block_map, block);
262 group = ext2fs_group_of_blk(fs, block);
263 fs->group_desc[group].bg_free_blocks_count++;
264 ext2fs_group_desc_csum_set(fs, group);
265 fs->super->s_free_blocks_count++;
270 * Remove the journal inode from the filesystem
272 static void remove_journal_inode(ext2_filsys fs)
274 struct ext2_inode inode;
276 ino_t ino = fs->super->s_journal_inum;
278 retval = ext2fs_read_inode(fs, ino, &inode);
280 com_err(program_name, retval,
281 _("while reading journal inode"));
284 if (ino == EXT2_JOURNAL_INO) {
285 retval = ext2fs_read_bitmaps(fs);
287 com_err(program_name, retval,
288 _("while reading bitmaps"));
291 retval = ext2fs_block_iterate(fs, ino,
292 BLOCK_FLAG_READ_ONLY, NULL,
293 release_blocks_proc, NULL);
295 com_err(program_name, retval,
296 _("while clearing journal inode"));
299 memset(&inode, 0, sizeof(inode));
300 ext2fs_mark_bb_dirty(fs);
301 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
303 inode.i_flags &= ~EXT2_IMMUTABLE_FL;
304 retval = ext2fs_write_inode(fs, ino, &inode);
306 com_err(program_name, retval,
307 _("while writing journal inode"));
310 fs->super->s_journal_inum = 0;
311 ext2fs_mark_super_dirty(fs);
315 * Update the default mount options
317 static void update_mntopts(ext2_filsys fs, char *mntopts)
319 struct ext2_super_block *sb = fs->super;
321 if (e2p_edit_mntopts(mntopts, &sb->s_default_mount_opts, ~0)) {
322 fprintf(stderr, _("Invalid mount option set: %s\n"),
326 ext2fs_mark_super_dirty(fs);
330 * Update the feature set as provided by the user.
332 static void update_feature_set(ext2_filsys fs, char *features)
334 struct ext2_super_block *sb = fs->super;
335 __u32 old_features[3];
337 unsigned int mask_err;
339 #define FEATURE_ON(type, mask) (!(old_features[(type)] & (mask)) && \
340 ((&sb->s_feature_compat)[(type)] & (mask)))
341 #define FEATURE_OFF(type, mask) ((old_features[(type)] & (mask)) && \
342 !((&sb->s_feature_compat)[(type)] & (mask)))
343 #define FEATURE_CHANGED(type, mask) ((mask) & \
344 (old_features[(type)] ^ (&sb->s_feature_compat)[(type)]))
346 old_features[E2P_FEATURE_COMPAT] = sb->s_feature_compat;
347 old_features[E2P_FEATURE_INCOMPAT] = sb->s_feature_incompat;
348 old_features[E2P_FEATURE_RO_INCOMPAT] = sb->s_feature_ro_compat;
350 if (e2p_edit_feature2(features, &sb->s_feature_compat,
351 ok_features, clear_ok_features,
352 &type_err, &mask_err)) {
355 _("Invalid filesystem option set: %s\n"),
357 else if (type_err & E2P_FEATURE_NEGATE_FLAG)
358 fprintf(stderr, _("Clearing filesystem feature '%s' "
360 e2p_feature2string(type_err &
361 E2P_FEATURE_TYPE_MASK,
364 fprintf(stderr, _("Setting filesystem feature '%s' "
366 e2p_feature2string(type_err, mask_err));
370 if (FEATURE_OFF(E2P_FEATURE_COMPAT, EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
371 if ((mount_flags & EXT2_MF_MOUNTED) &&
372 !(mount_flags & EXT2_MF_READONLY)) {
373 fputs(_("The has_journal feature may only be "
374 "cleared when the filesystem is\n"
375 "unmounted or mounted "
376 "read-only.\n"), stderr);
379 if (sb->s_feature_incompat &
380 EXT3_FEATURE_INCOMPAT_RECOVER) {
381 fputs(_("The needs_recovery flag is set. "
382 "Please run e2fsck before clearing\n"
383 "the has_journal flag.\n"), stderr);
386 if (sb->s_journal_inum) {
387 remove_journal_inode(fs);
389 if (sb->s_journal_dev) {
390 remove_journal_device(fs);
394 if (FEATURE_ON(E2P_FEATURE_COMPAT, EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
396 * If adding a journal flag, let the create journal
397 * code below handle setting the flag and creating the
398 * journal. We supply a default size if necessary.
402 sb->s_feature_compat &= ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
405 if (FEATURE_ON(E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_DIR_INDEX)) {
406 if (!sb->s_def_hash_version)
407 sb->s_def_hash_version = EXT2_HASH_HALF_MD4;
408 if (uuid_is_null((unsigned char *) sb->s_hash_seed))
409 uuid_generate((unsigned char *) sb->s_hash_seed);
412 if (FEATURE_OFF(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
413 if (ext2fs_check_desc(fs)) {
414 fputs(_("Clearing the flex_bg flag would "
415 "cause the the filesystem to be\n"
416 "inconsistent.\n"), stderr);
421 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
422 EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
423 if ((mount_flags & EXT2_MF_MOUNTED) &&
424 !(mount_flags & EXT2_MF_READONLY)) {
425 fputs(_("The huge_file feature may only be "
426 "cleared when the filesystem is\n"
427 "unmounted or mounted "
428 "read-only.\n"), stderr);
433 if (sb->s_rev_level == EXT2_GOOD_OLD_REV &&
434 (sb->s_feature_compat || sb->s_feature_ro_compat ||
435 sb->s_feature_incompat))
436 ext2fs_update_dynamic_rev(fs);
438 if (FEATURE_CHANGED(E2P_FEATURE_RO_INCOMPAT,
439 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER) ||
440 FEATURE_CHANGED(E2P_FEATURE_RO_INCOMPAT,
441 EXT4_FEATURE_RO_COMPAT_GDT_CSUM) ||
442 FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
443 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) ||
444 FEATURE_CHANGED(E2P_FEATURE_INCOMPAT,
445 EXT2_FEATURE_INCOMPAT_FILETYPE) ||
446 FEATURE_CHANGED(E2P_FEATURE_COMPAT,
447 EXT2_FEATURE_COMPAT_RESIZE_INODE) ||
448 FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
449 EXT2_FEATURE_RO_COMPAT_LARGE_FILE)) {
450 sb->s_state &= ~EXT2_VALID_FS;
451 printf("\n%s\n", _(please_fsck));
452 if (mount_flags & EXT2_MF_READONLY)
453 printf(_("(and reboot afterwards!)\n"));
456 if ((old_features[E2P_FEATURE_COMPAT] != sb->s_feature_compat) ||
457 (old_features[E2P_FEATURE_INCOMPAT] != sb->s_feature_incompat) ||
458 (old_features[E2P_FEATURE_RO_INCOMPAT] != sb->s_feature_ro_compat))
459 ext2fs_mark_super_dirty(fs);
463 * Add a journal to the filesystem.
465 static void add_journal(ext2_filsys fs)
467 unsigned long journal_blocks;
472 if (fs->super->s_feature_compat &
473 EXT3_FEATURE_COMPAT_HAS_JOURNAL) {
474 fputs(_("The filesystem already has a journal.\n"), stderr);
477 if (journal_device) {
478 check_plausibility(journal_device);
479 check_mount(journal_device, 0, _("journal"));
480 #ifdef CONFIG_TESTIO_DEBUG
481 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
482 io_ptr = test_io_manager;
483 test_io_backing_manager = unix_io_manager;
486 io_ptr = unix_io_manager;
487 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
488 EXT2_FLAG_JOURNAL_DEV_OK, 0,
489 fs->blocksize, io_ptr, &jfs);
491 com_err(program_name, retval,
492 _("\n\twhile trying to open journal on %s\n"),
496 printf(_("Creating journal on device %s: "),
500 retval = ext2fs_add_journal_device(fs, jfs);
503 com_err(program_name, retval,
504 _("while adding filesystem to journal on %s"),
508 fputs(_("done\n"), stdout);
509 } else if (journal_size) {
510 fputs(_("Creating journal inode: "), stdout);
512 journal_blocks = figure_journal_size(journal_size, fs);
514 retval = ext2fs_add_journal_inode(fs, journal_blocks,
517 fprintf(stderr, "\n");
518 com_err(program_name, retval,
519 _("\n\twhile trying to create journal file"));
522 fputs(_("done\n"), stdout);
524 * If the filesystem wasn't mounted, we need to force
525 * the block group descriptors out.
527 if ((mount_flags & EXT2_MF_MOUNTED) == 0)
528 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
530 print_check_message(fs);
534 free(journal_device);
539 static void parse_e2label_options(int argc, char ** argv)
541 if ((argc < 2) || (argc > 3)) {
542 fputs(_("Usage: e2label device [newlabel]\n"), stderr);
545 io_options = strchr(argv[1], '?');
548 device_name = blkid_get_devname(NULL, argv[1], NULL);
550 com_err("e2label", 0, _("Unable to resolve '%s'"),
554 open_flag = EXT2_FLAG_JOURNAL_DEV_OK;
556 open_flag |= EXT2_FLAG_RW;
563 static time_t parse_time(char *str)
567 if (strcmp(str, "now") == 0) {
570 memset(&ts, 0, sizeof(ts));
572 strptime(str, "%Y%m%d%H%M%S", &ts);
574 sscanf(str, "%4d%2d%2d%2d%2d%2d", &ts.tm_year, &ts.tm_mon,
575 &ts.tm_mday, &ts.tm_hour, &ts.tm_min, &ts.tm_sec);
578 if (ts.tm_year < 0 || ts.tm_mon < 0 || ts.tm_mon > 11 ||
579 ts.tm_mday < 0 || ts.tm_mday > 31 || ts.tm_hour > 23 ||
580 ts.tm_min > 59 || ts.tm_sec > 61)
583 if (ts.tm_mday == 0) {
584 com_err(program_name, 0,
585 _("Couldn't parse date/time specifier: %s"),
590 return (mktime(&ts));
593 static void parse_tune2fs_options(int argc, char **argv)
602 printf("tune2fs %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
603 while ((c = getopt(argc, argv, "c:e:fg:i:jlm:o:r:s:u:C:E:I:J:L:M:O:T:U:")) != EOF)
606 max_mount_count = strtol(optarg, &tmp, 0);
607 if (*tmp || max_mount_count > 16000) {
608 com_err(program_name, 0,
609 _("bad mounts count - %s"),
613 if (max_mount_count == 0)
614 max_mount_count = -1;
616 open_flag = EXT2_FLAG_RW;
619 mount_count = strtoul(optarg, &tmp, 0);
620 if (*tmp || mount_count > 16000) {
621 com_err(program_name, 0,
622 _("bad mounts count - %s"),
627 open_flag = EXT2_FLAG_RW;
630 if (strcmp(optarg, "continue") == 0)
631 errors = EXT2_ERRORS_CONTINUE;
632 else if (strcmp(optarg, "remount-ro") == 0)
633 errors = EXT2_ERRORS_RO;
634 else if (strcmp(optarg, "panic") == 0)
635 errors = EXT2_ERRORS_PANIC;
637 com_err(program_name, 0,
638 _("bad error behavior - %s"),
643 open_flag = EXT2_FLAG_RW;
646 extended_cmd = optarg;
647 open_flag |= EXT2_FLAG_RW;
649 case 'f': /* Force */
653 resgid = strtoul(optarg, &tmp, 0);
655 gr = getgrnam(optarg);
664 com_err(program_name, 0,
665 _("bad gid/group name - %s"),
670 open_flag = EXT2_FLAG_RW;
673 interval = strtoul(optarg, &tmp, 0);
686 case 'M': /* months! */
687 interval *= 86400 * 30;
691 case 'W': /* weeks */
692 interval *= 86400 * 7;
697 com_err(program_name, 0,
698 _("bad interval - %s"), optarg);
702 open_flag = EXT2_FLAG_RW;
707 open_flag = EXT2_FLAG_RW;
710 parse_journal_opts(optarg);
711 open_flag = EXT2_FLAG_RW;
719 open_flag |= EXT2_FLAG_RW |
720 EXT2_FLAG_JOURNAL_DEV_OK;
723 reserved_ratio = strtod(optarg, &tmp);
724 if (*tmp || reserved_ratio > 50 ||
725 reserved_ratio < 0) {
726 com_err(program_name, 0,
727 _("bad reserved block ratio - %s"),
732 open_flag = EXT2_FLAG_RW;
735 new_last_mounted = optarg;
737 open_flag = EXT2_FLAG_RW;
741 com_err(program_name, 0,
742 _("-o may only be specified once"));
745 mntopts_cmd = optarg;
746 open_flag = EXT2_FLAG_RW;
751 com_err(program_name, 0,
752 _("-O may only be specified once"));
755 features_cmd = optarg;
756 open_flag = EXT2_FLAG_RW;
759 reserved_blocks = strtoul(optarg, &tmp, 0);
761 com_err(program_name, 0,
762 _("bad reserved blocks count - %s"),
767 open_flag = EXT2_FLAG_RW;
769 case 's': /* Deprecated */
770 s_flag = atoi(optarg);
771 open_flag = EXT2_FLAG_RW;
775 last_check_time = parse_time(optarg);
776 open_flag = EXT2_FLAG_RW;
779 resuid = strtoul(optarg, &tmp, 0);
781 pw = getpwnam(optarg);
790 com_err(program_name, 0,
791 _("bad uid/user name - %s"),
796 open_flag = EXT2_FLAG_RW;
801 open_flag = EXT2_FLAG_RW |
802 EXT2_FLAG_JOURNAL_DEV_OK;
805 new_inode_size = strtoul(optarg, &tmp, 0);
807 com_err(program_name, 0,
808 _("bad inode size - %s"),
812 if (!((new_inode_size &
813 (new_inode_size - 1)) == 0)) {
814 com_err(program_name, 0,
815 _("Inode size must be a "
820 open_flag = EXT2_FLAG_RW;
826 if (optind < argc - 1 || optind == argc)
828 if (!open_flag && !l_flag)
830 io_options = strchr(argv[optind], '?');
833 device_name = blkid_get_devname(NULL, argv[optind], NULL);
835 com_err("tune2fs", 0, _("Unable to resolve '%s'"),
841 #ifdef CONFIG_BUILD_FINDFS
842 void do_findfs(int argc, char **argv)
847 (strncmp(argv[1], "LABEL=", 6) && strncmp(argv[1], "UUID=", 5))) {
848 fprintf(stderr, "Usage: findfs LABEL=<label>|UUID=<uuid>\n");
851 dev = blkid_get_devname(NULL, argv[1], NULL);
853 com_err("findfs", 0, _("Unable to resolve '%s'"),
862 static void parse_extended_opts(ext2_filsys fs, const char *opts)
864 char *buf, *token, *next, *p, *arg;
872 _("Couldn't allocate memory to parse options!\n"));
876 for (token = buf; token && *token; token = next) {
877 p = strchr(token, ',');
883 arg = strchr(token, '=');
888 if (!strcmp(token, "test_fs")) {
889 fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
890 printf("Setting test filesystem flag\n");
891 ext2fs_mark_super_dirty(fs);
892 } else if (!strcmp(token, "^test_fs")) {
893 fs->super->s_flags &= ~EXT2_FLAGS_TEST_FILESYS;
894 printf("Clearing test filesystem flag\n");
895 ext2fs_mark_super_dirty(fs);
896 } else if (strcmp(token, "stride") == 0) {
901 stride = strtoul(arg, &p, 0);
902 if (*p || (stride == 0)) {
904 _("Invalid RAID stride: %s\n"),
910 } else if (strcmp(token, "stripe-width") == 0 ||
911 strcmp(token, "stripe_width") == 0) {
916 stripe_width = strtoul(arg, &p, 0);
917 if (*p || (stripe_width == 0)) {
919 _("Invalid RAID stripe-width: %s\n"),
924 stripe_width_set = 1;
925 } else if (strcmp(token, "hash_alg") == 0 ||
926 strcmp(token, "hash-alg") == 0) {
931 hash_alg = e2p_string2hash(arg);
934 _("Invalid hash algorithm: %s\n"),
939 fs->super->s_def_hash_version = hash_alg;
940 printf(_("Setting default hash algorithm "
943 ext2fs_mark_super_dirty(fs);
948 fprintf(stderr, _("\nBad options specified.\n\n"
949 "Extended options are separated by commas, "
950 "and may take an argument which\n"
951 "\tis set off by an equals ('=') sign.\n\n"
952 "Valid extended options are:\n"
953 "\tstride=<RAID per-disk chunk size in blocks>\n"
954 "\tstripe_width=<RAID stride*data disks in blocks>\n"
955 "\thash_alg=<hash algorithm>\n"
965 * Fill in the block bitmap bmap with the information regarding the
968 static int get_move_bitmaps(ext2_filsys fs, int new_ino_blks_per_grp,
969 ext2fs_block_bitmap bmap)
972 blk_t j, needed_blocks = 0;
973 blk_t start_blk, end_blk;
975 for (i = 0; i < fs->group_desc_count; i++) {
976 start_blk = fs->group_desc[i].bg_inode_table +
977 fs->inode_blocks_per_group;
979 end_blk = fs->group_desc[i].bg_inode_table +
980 new_ino_blks_per_grp;
982 for (j = start_blk; j < end_blk; j++) {
983 if (ext2fs_test_block_bitmap(fs->block_map, j)) {
985 * What happens if the block is marked
988 ext2fs_mark_block_bitmap(bmap, j);
991 * We are going to use this block for
992 * inode table. So mark them used.
994 ext2fs_mark_block_bitmap(fs->block_map, j);
997 needed_blocks += end_blk - start_blk;
1000 if (needed_blocks > fs->super->s_free_blocks_count)
1006 static int move_block(ext2_filsys fs, ext2fs_block_bitmap bmap)
1010 blk_t blk, new_blk, goal;
1011 struct blk_move *bmv;
1013 retval = ext2fs_get_mem(fs->blocksize, &buf);
1017 for (new_blk = blk = fs->super->s_first_data_block;
1018 blk < fs->super->s_blocks_count; blk++) {
1019 if (!ext2fs_test_block_bitmap(bmap, blk))
1023 retval = ext2fs_new_block(fs, goal, NULL, &new_blk);
1027 /* Mark this block as allocated */
1028 ext2fs_mark_block_bitmap(fs->block_map, new_blk);
1030 /* Add it to block move list */
1031 retval = ext2fs_get_mem(sizeof(struct blk_move), &bmv);
1036 bmv->new_loc = new_blk;
1038 list_add(&(bmv->list), &blk_move_list);
1040 retval = io_channel_read_blk(fs->io, blk, 1, buf);
1044 retval = io_channel_write_blk(fs->io, new_blk, 1, buf);
1050 ext2fs_free_mem(&buf);
1054 static blk_t translate_block(blk_t blk)
1056 struct list_head *entry;
1057 struct blk_move *bmv;
1059 list_for_each(entry, &blk_move_list) {
1060 bmv = list_entry(entry, struct blk_move, list);
1061 if (bmv->old_loc == blk)
1062 return bmv->new_loc;
1068 static int process_block(ext2_filsys fs EXT2FS_ATTR((unused)),
1070 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1071 blk_t ref_block EXT2FS_ATTR((unused)),
1072 int ref_offset EXT2FS_ATTR((unused)),
1077 ext2fs_block_bitmap bmap = (ext2fs_block_bitmap) priv_data;
1079 if (!ext2fs_test_block_bitmap(bmap, *block_nr))
1081 new_blk = translate_block(*block_nr);
1083 *block_nr = new_blk;
1085 * This will force the ext2fs_write_inode in the iterator
1087 ret |= BLOCK_CHANGED;
1093 static int inode_scan_and_fix(ext2_filsys fs, ext2fs_block_bitmap bmap)
1095 errcode_t retval = 0;
1098 char *block_buf = 0;
1099 struct ext2_inode inode;
1100 ext2_inode_scan scan = NULL;
1102 retval = ext2fs_get_mem(fs->blocksize * 3, &block_buf);
1106 retval = ext2fs_open_inode_scan(fs, 0, &scan);
1111 retval = ext2fs_get_next_inode(scan, &ino, &inode);
1118 if (inode.i_links_count == 0)
1119 continue; /* inode not in use */
1122 * If we end up modifying the journal inode
1123 * the sb->s_jnl_blocks will differ. But a
1124 * subsequent e2fsck fixes that.
1125 * Do we need to fix this ??
1128 if (inode.i_file_acl &&
1129 ext2fs_test_block_bitmap(bmap, inode.i_file_acl)) {
1130 blk = translate_block(inode.i_file_acl);
1134 inode.i_file_acl = blk;
1137 * Write the inode to disk so that inode table
1140 retval = ext2fs_write_inode(fs, ino, &inode);
1145 if (!ext2fs_inode_has_valid_blocks(&inode))
1148 retval = ext2fs_block_iterate2(fs, ino, 0, block_buf,
1149 process_block, bmap);
1156 ext2fs_free_mem(&block_buf);
1161 static int expand_inode_table(ext2_filsys fs, unsigned long new_ino_size)
1166 int new_ino_blks_per_grp;
1168 char *old_itable = NULL, *new_itable = NULL;
1169 char *tmp_old_itable = NULL, *tmp_new_itable = NULL;
1170 unsigned long old_ino_size;
1171 int old_itable_size, new_itable_size;
1173 old_itable_size = fs->inode_blocks_per_group * fs->blocksize;
1174 old_ino_size = EXT2_INODE_SIZE(fs->super);
1176 new_ino_blks_per_grp = ext2fs_div_ceil(
1177 EXT2_INODES_PER_GROUP(fs->super) *
1181 new_itable_size = new_ino_blks_per_grp * fs->blocksize;
1183 retval = ext2fs_get_mem(old_itable_size, &old_itable);
1187 retval = ext2fs_get_mem(new_itable_size, &new_itable);
1191 tmp_old_itable = old_itable;
1192 tmp_new_itable = new_itable;
1194 for (i = 0; i < fs->group_desc_count; i++) {
1195 blk = fs->group_desc[i].bg_inode_table;
1196 retval = io_channel_read_blk(fs->io, blk,
1197 fs->inode_blocks_per_group, old_itable);
1201 for (j = 0; j < EXT2_INODES_PER_GROUP(fs->super); j++) {
1202 memcpy(new_itable, old_itable, old_ino_size);
1204 memset(new_itable+old_ino_size, 0,
1205 new_ino_size - old_ino_size);
1207 new_itable += new_ino_size;
1208 old_itable += old_ino_size;
1211 /* reset the pointer */
1212 old_itable = tmp_old_itable;
1213 new_itable = tmp_new_itable;
1215 retval = io_channel_write_blk(fs->io, blk,
1216 new_ino_blks_per_grp, new_itable);
1221 /* Update the meta data */
1222 fs->inode_blocks_per_group = new_ino_blks_per_grp;
1223 fs->super->s_inode_size = new_ino_size;
1227 ext2fs_free_mem(&old_itable);
1230 ext2fs_free_mem(&new_itable);
1235 static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs)
1239 unsigned int group = 0;
1240 unsigned int count = 0;
1245 * First calculate the block statistics
1247 for (blk = fs->super->s_first_data_block;
1248 blk < fs->super->s_blocks_count; blk++) {
1249 if (!ext2fs_fast_test_block_bitmap(fs->block_map, blk)) {
1254 if ((count == fs->super->s_blocks_per_group) ||
1255 (blk == fs->super->s_blocks_count-1)) {
1256 fs->group_desc[group++].bg_free_blocks_count =
1262 fs->super->s_free_blocks_count = total_free;
1265 * Next, calculate the inode statistics
1272 /* Protect loop from wrap-around if s_inodes_count maxed */
1273 for (ino = 1; ino <= fs->super->s_inodes_count && ino > 0; ino++) {
1274 if (!ext2fs_fast_test_inode_bitmap(fs->inode_map, ino)) {
1279 if ((count == fs->super->s_inodes_per_group) ||
1280 (ino == fs->super->s_inodes_count)) {
1281 fs->group_desc[group++].bg_free_inodes_count =
1287 fs->super->s_free_inodes_count = total_free;
1288 ext2fs_mark_super_dirty(fs);
1292 #define list_for_each_safe(pos, pnext, head) \
1293 for (pos = (head)->next, pnext = pos->next; pos != (head); \
1294 pos = pnext, pnext = pos->next)
1296 static void free_blk_move_list(void)
1298 struct list_head *entry, *tmp;
1299 struct blk_move *bmv;
1301 list_for_each_safe(entry, tmp, &blk_move_list) {
1302 bmv = list_entry(entry, struct blk_move, list);
1304 ext2fs_free_mem(&bmv);
1309 static int resize_inode(ext2_filsys fs, unsigned long new_size)
1312 int new_ino_blks_per_grp;
1313 ext2fs_block_bitmap bmap;
1315 ext2fs_read_inode_bitmap(fs);
1316 ext2fs_read_block_bitmap(fs);
1317 INIT_LIST_HEAD(&blk_move_list);
1320 new_ino_blks_per_grp = ext2fs_div_ceil(
1321 EXT2_INODES_PER_GROUP(fs->super)*
1325 /* We may change the file system.
1326 * Mark the file system as invalid so that
1327 * the user is prompted to run fsck.
1329 fs->super->s_state &= ~EXT2_VALID_FS;
1331 retval = ext2fs_allocate_block_bitmap(fs, _("blocks to be moved"),
1334 fputs(_("Failed to allocate block bitmap when "
1335 "increasing inode size\n"), stderr);
1338 retval = get_move_bitmaps(fs, new_ino_blks_per_grp, bmap);
1340 fputs(_("Not enough space to increase inode size \n"), stderr);
1343 retval = move_block(fs, bmap);
1345 fputs(_("Failed to relocate blocks during inode resize \n"),
1349 retval = inode_scan_and_fix(fs, bmap);
1353 retval = expand_inode_table(fs, new_size);
1357 ext2fs_calculate_summary_stats(fs);
1359 fs->super->s_state |= EXT2_VALID_FS;
1360 /* mark super block and block bitmap as dirty */
1361 ext2fs_mark_super_dirty(fs);
1362 ext2fs_mark_bb_dirty(fs);
1365 free_blk_move_list();
1366 ext2fs_free_block_bitmap(bmap);
1371 free_blk_move_list();
1372 ext2fs_free_block_bitmap(bmap);
1373 fputs(_("Error in resizing the inode size.\n"
1374 "Run e2undo to undo the "
1375 "file system changes. \n"), stderr);
1380 static int tune2fs_setup_tdb(const char *name, io_manager *io_ptr)
1382 errcode_t retval = 0;
1383 const char *tdb_dir;
1385 char *dev_name, *tmp_name;
1389 * Configuration via a conf file would be
1392 profile_get_string(profile, "scratch_files",
1396 tmp_name = strdup(name);
1399 com_err(program_name, ENOMEM,
1400 _("Couldn't allocate memory for tdb filename\n"));
1403 dev_name = basename(tmp_name);
1405 tdb_dir = getenv("E2FSPROGS_UNDO_DIR");
1407 tdb_dir = "/var/lib/e2fsprogs";
1409 if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
1410 access(tdb_dir, W_OK))
1413 tdb_file = malloc(strlen(tdb_dir) + 9 + strlen(dev_name) + 7 + 1);
1416 sprintf(tdb_file, "%s/tune2fs-%s.e2undo", tdb_dir, dev_name);
1418 if (!access(tdb_file, F_OK)) {
1419 if (unlink(tdb_file) < 0) {
1421 com_err(program_name, retval,
1422 _("while trying to delete %s"),
1429 set_undo_io_backing_manager(*io_ptr);
1430 *io_ptr = undo_io_manager;
1431 set_undo_io_backup_file(tdb_file);
1432 printf(_("To undo the tune2fs operation please run "
1433 "the command\n e2undo %s %s\n\n"),
1440 int main(int argc, char **argv)
1444 struct ext2_super_block *sb;
1445 io_manager io_ptr, io_ptr_orig = NULL;
1448 setlocale(LC_MESSAGES, "");
1449 setlocale(LC_CTYPE, "");
1450 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1451 textdomain(NLS_CAT_NAME);
1454 program_name = *argv;
1455 add_error_table(&et_ext2_error_table);
1457 #ifdef CONFIG_BUILD_FINDFS
1458 if (strcmp(get_progname(argv[0]), "findfs") == 0)
1459 do_findfs(argc, argv);
1461 if (strcmp(get_progname(argv[0]), "e2label") == 0)
1462 parse_e2label_options(argc, argv);
1464 parse_tune2fs_options(argc, argv);
1466 #ifdef CONFIG_TESTIO_DEBUG
1467 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_DEBUG")) {
1468 io_ptr = test_io_manager;
1469 test_io_backing_manager = unix_io_manager;
1472 io_ptr = unix_io_manager;
1475 retval = ext2fs_open2(device_name, io_options, open_flag,
1478 com_err(program_name, retval,
1479 _("while trying to open %s"),
1482 _("Couldn't find valid filesystem superblock.\n"));
1486 if (I_flag && !io_ptr_orig) {
1488 * Check the inode size is right so we can issue an
1489 * error message and bail before setting up the tdb
1492 if (new_inode_size == EXT2_INODE_SIZE(fs->super)) {
1493 fprintf(stderr, _("The inode size is already %lu\n"),
1497 if (new_inode_size < EXT2_INODE_SIZE(fs->super)) {
1498 fprintf(stderr, _("Shrinking the inode size is "
1499 "not supported\n"));
1504 * If inode resize is requested use the
1507 io_ptr_orig = io_ptr;
1508 retval = tune2fs_setup_tdb(device_name, &io_ptr);
1511 if (io_ptr != io_ptr_orig) {
1518 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
1521 /* For e2label emulation */
1522 printf("%.*s\n", (int) sizeof(sb->s_volume_name),
1524 remove_error_table(&et_ext2_error_table);
1528 retval = ext2fs_check_if_mounted(device_name, &mount_flags);
1530 com_err("ext2fs_check_if_mount", retval,
1531 _("while determining whether %s is mounted."),
1535 /* Normally we only need to write out the superblock */
1536 fs->flags |= EXT2_FLAG_SUPER_ONLY;
1539 sb->s_max_mnt_count = max_mount_count;
1540 ext2fs_mark_super_dirty(fs);
1541 printf(_("Setting maximal mount count to %d\n"),
1545 sb->s_mnt_count = mount_count;
1546 ext2fs_mark_super_dirty(fs);
1547 printf(_("Setting current mount count to %d\n"), mount_count);
1550 sb->s_errors = errors;
1551 ext2fs_mark_super_dirty(fs);
1552 printf(_("Setting error behavior to %d\n"), errors);
1555 sb->s_def_resgid = resgid;
1556 ext2fs_mark_super_dirty(fs);
1557 printf(_("Setting reserved blocks gid to %lu\n"), resgid);
1560 sb->s_checkinterval = interval;
1561 ext2fs_mark_super_dirty(fs);
1562 printf(_("Setting interval between checks to %lu seconds\n"),
1566 sb->s_r_blocks_count = (unsigned int) (reserved_ratio *
1567 sb->s_blocks_count / 100.0);
1568 ext2fs_mark_super_dirty(fs);
1569 printf(_("Setting reserved blocks percentage to %g%% "
1571 reserved_ratio, sb->s_r_blocks_count);
1574 if (reserved_blocks >= sb->s_blocks_count/2) {
1575 com_err(program_name, 0,
1576 _("reserved blocks count is too big (%lu)"),
1580 sb->s_r_blocks_count = reserved_blocks;
1581 ext2fs_mark_super_dirty(fs);
1582 printf(_("Setting reserved blocks count to %lu\n"),
1586 if (sb->s_feature_ro_compat &
1587 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)
1588 fputs(_("\nThe filesystem already has sparse "
1589 "superblocks.\n"), stderr);
1591 sb->s_feature_ro_compat |=
1592 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1593 sb->s_state &= ~EXT2_VALID_FS;
1594 ext2fs_mark_super_dirty(fs);
1595 printf(_("\nSparse superblock flag set. %s"),
1600 fputs(_("\nClearing the sparse superflag not supported.\n"),
1605 sb->s_lastcheck = last_check_time;
1606 ext2fs_mark_super_dirty(fs);
1607 printf(_("Setting time filesystem last checked to %s\n"),
1608 ctime(&last_check_time));
1611 sb->s_def_resuid = resuid;
1612 ext2fs_mark_super_dirty(fs);
1613 printf(_("Setting reserved blocks uid to %lu\n"), resuid);
1616 if (strlen(new_label) > sizeof(sb->s_volume_name))
1617 fputs(_("Warning: label too long, truncating.\n"),
1619 memset(sb->s_volume_name, 0, sizeof(sb->s_volume_name));
1620 strncpy(sb->s_volume_name, new_label,
1621 sizeof(sb->s_volume_name));
1622 ext2fs_mark_super_dirty(fs);
1625 memset(sb->s_last_mounted, 0, sizeof(sb->s_last_mounted));
1626 strncpy(sb->s_last_mounted, new_last_mounted,
1627 sizeof(sb->s_last_mounted));
1628 ext2fs_mark_super_dirty(fs);
1631 update_mntopts(fs, mntopts_cmd);
1633 update_feature_set(fs, features_cmd);
1635 parse_extended_opts(fs, extended_cmd);
1636 if (journal_size || journal_device)
1643 if (sb->s_feature_ro_compat &
1644 EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
1646 * Determine if the block group checksums are
1647 * correct so we know whether or not to set
1650 for (i = 0; i < fs->group_desc_count; i++)
1651 if (!ext2fs_group_desc_csum_verify(fs, i))
1653 if (i >= fs->group_desc_count)
1656 if ((strcasecmp(new_UUID, "null") == 0) ||
1657 (strcasecmp(new_UUID, "clear") == 0)) {
1658 uuid_clear(sb->s_uuid);
1659 } else if (strcasecmp(new_UUID, "time") == 0) {
1660 uuid_generate_time(sb->s_uuid);
1661 } else if (strcasecmp(new_UUID, "random") == 0) {
1662 uuid_generate(sb->s_uuid);
1663 } else if (uuid_parse(new_UUID, sb->s_uuid)) {
1664 com_err(program_name, 0, _("Invalid UUID format\n"));
1668 for (i = 0; i < fs->group_desc_count; i++)
1669 ext2fs_group_desc_csum_set(fs, i);
1670 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
1672 ext2fs_mark_super_dirty(fs);
1675 if (mount_flags & EXT2_MF_MOUNTED) {
1676 fputs(_("The inode size may only be "
1677 "changed when the filesystem is "
1678 "unmounted.\n"), stderr);
1681 if (fs->super->s_feature_incompat &
1682 EXT4_FEATURE_INCOMPAT_FLEX_BG) {
1683 fputs(_("Changing the inode size not supported for "
1684 "filesystems with the flex_bg\n"
1685 "feature enabled.\n"),
1690 * We want to update group descriptor also
1691 * with the new free inode count
1693 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
1694 if (resize_inode(fs, new_inode_size) == 0) {
1695 printf(_("Setting inode size %lu\n"),
1703 sb->s_raid_stride = stride;
1704 ext2fs_mark_super_dirty(fs);
1705 printf(_("Setting stride size to %d\n"), stride);
1707 if (stripe_width_set) {
1708 sb->s_raid_stripe_width = stripe_width;
1709 ext2fs_mark_super_dirty(fs);
1710 printf(_("Setting stripe width to %d\n"), stripe_width);
1713 remove_error_table(&et_ext2_error_table);
1714 return (ext2fs_close(fs) ? 1 : 0);