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() */
44 #include <sys/types.h>
48 #include "ext2fs/ext2_fs.h"
49 #include "ext2fs/ext2fs.h"
50 #include "et/com_err.h"
51 #include "uuid/uuid.h"
55 #include "blkid/blkid.h"
57 #include "../version.h"
58 #include "nls-enable.h"
60 const char * program_name = "tune2fs";
62 char * new_label, *new_last_mounted, *new_UUID;
64 static int c_flag, C_flag, e_flag, f_flag, g_flag, i_flag, l_flag, L_flag;
65 static int m_flag, M_flag, r_flag, s_flag = -1, u_flag, U_flag, T_flag;
67 static time_t last_check_time;
68 static int print_label;
69 static int max_mount_count, mount_count, mount_flags;
70 static unsigned long interval, reserved_blocks;
71 static double reserved_ratio;
72 static unsigned long resgid, resuid;
73 static unsigned short errors;
75 static char *features_cmd;
76 static char *mntopts_cmd;
77 static int stride, stripe_width;
78 static int stride_set, stripe_width_set;
79 static char *extended_cmd;
80 static unsigned long new_inode_size;
82 int journal_size, journal_flags;
85 static struct list_head blk_move_list;
88 struct list_head list;
94 static const char *please_fsck = N_("Please run e2fsck on the filesystem.\n");
96 void do_findfs(int argc, char **argv);
98 static void usage(void)
101 _("Usage: %s [-c max_mounts_count] [-e errors_behavior] "
103 "\t[-i interval[d|m|w]] [-j] [-J journal_options] [-l]\n"
104 "\t[-m reserved_blocks_percent] "
105 "[-o [^]mount_options[,...]] \n"
106 "\t[-r reserved_blocks_count] [-u user] [-C mount_count] "
107 "[-L volume_label]\n"
108 "\t[-M last_mounted_dir] [-O [^]feature[,...]]\n"
109 "\t[-E extended-option[,...]] [-T last_check_time] "
110 "[-U UUID]\n\t[ -I new_inode_size ] device\n"), program_name);
114 static __u32 ok_features[3] = {
116 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
117 EXT2_FEATURE_COMPAT_DIR_INDEX,
119 EXT2_FEATURE_INCOMPAT_FILETYPE |
120 EXT3_FEATURE_INCOMPAT_EXTENTS |
121 EXT4_FEATURE_INCOMPAT_FLEX_BG,
123 EXT2_FEATURE_RO_COMPAT_LARGE_FILE |
124 EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
125 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
126 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
127 EXT4_FEATURE_RO_COMPAT_GDT_CSUM |
128 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
131 static __u32 clear_ok_features[3] = {
133 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
134 EXT2_FEATURE_COMPAT_RESIZE_INODE |
135 EXT2_FEATURE_COMPAT_DIR_INDEX,
137 EXT2_FEATURE_INCOMPAT_FILETYPE |
138 EXT4_FEATURE_INCOMPAT_FLEX_BG,
140 EXT2_FEATURE_RO_COMPAT_LARGE_FILE |
141 EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
142 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
143 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
144 EXT4_FEATURE_RO_COMPAT_GDT_CSUM
148 * Remove an external journal from the filesystem
150 static void remove_journal_device(ext2_filsys fs)
155 journal_superblock_t *jsb;
158 int commit_remove_journal = 0;
162 commit_remove_journal = 1; /* force removal even if error */
164 uuid_unparse(fs->super->s_journal_uuid, buf);
165 journal_path = blkid_get_devname(NULL, "UUID", buf);
169 ext2fs_find_block_device(fs->super->s_journal_dev);
174 #ifdef CONFIG_TESTIO_DEBUG
175 io_ptr = test_io_manager;
176 test_io_backing_manager = unix_io_manager;
178 io_ptr = unix_io_manager;
180 retval = ext2fs_open(journal_path, EXT2_FLAG_RW|
181 EXT2_FLAG_JOURNAL_DEV_OK, 0,
182 fs->blocksize, io_ptr, &jfs);
184 com_err(program_name, retval,
185 _("while trying to open external journal"));
186 goto no_valid_journal;
188 if (!(jfs->super->s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
189 fprintf(stderr, _("%s is not a journal device.\n"),
191 goto no_valid_journal;
194 /* Get the journal superblock */
195 if ((retval = io_channel_read_blk(jfs->io, 1, -1024, buf))) {
196 com_err(program_name, retval,
197 _("while reading journal superblock"));
198 goto no_valid_journal;
201 jsb = (journal_superblock_t *) buf;
202 if ((jsb->s_header.h_magic != (unsigned) ntohl(JFS_MAGIC_NUMBER)) ||
203 (jsb->s_header.h_blocktype != (unsigned) ntohl(JFS_SUPERBLOCK_V2))) {
204 fputs(_("Journal superblock not found!\n"), stderr);
205 goto no_valid_journal;
208 /* Find the filesystem UUID */
209 nr_users = ntohl(jsb->s_nr_users);
210 for (i=0; i < nr_users; i++) {
211 if (memcmp(fs->super->s_uuid,
212 &jsb->s_users[i*16], 16) == 0)
216 fputs(_("Filesystem's UUID not found on journal device.\n"),
218 commit_remove_journal = 1;
219 goto no_valid_journal;
222 for (i=0; i < nr_users; i++)
223 memcpy(&jsb->s_users[i*16], &jsb->s_users[(i+1)*16], 16);
224 jsb->s_nr_users = htonl(nr_users);
226 /* Write back the journal superblock */
227 if ((retval = io_channel_write_blk(jfs->io, 1, -1024, buf))) {
228 com_err(program_name, retval,
229 "while writing journal superblock.");
230 goto no_valid_journal;
233 commit_remove_journal = 1;
236 if (commit_remove_journal == 0) {
237 fputs(_("Journal NOT removed\n"), stderr);
240 fs->super->s_journal_dev = 0;
241 uuid_clear(fs->super->s_journal_uuid);
242 ext2fs_mark_super_dirty(fs);
243 fputs(_("Journal removed\n"), stdout);
247 /* Helper function for remove_journal_inode */
248 static int release_blocks_proc(ext2_filsys fs, blk_t *blocknr,
249 int blockcnt EXT2FS_ATTR((unused)),
250 void *private EXT2FS_ATTR((unused)))
256 ext2fs_unmark_block_bitmap(fs->block_map,block);
257 group = ext2fs_group_of_blk(fs, block);
258 fs->group_desc[group].bg_free_blocks_count++;
259 ext2fs_group_desc_csum_set(fs, group);
260 fs->super->s_free_blocks_count++;
265 * Remove the journal inode from the filesystem
267 static void remove_journal_inode(ext2_filsys fs)
269 struct ext2_inode inode;
271 ino_t ino = fs->super->s_journal_inum;
273 retval = ext2fs_read_inode(fs, ino, &inode);
275 com_err(program_name, retval,
276 _("while reading journal inode"));
279 if (ino == EXT2_JOURNAL_INO) {
280 retval = ext2fs_read_bitmaps(fs);
282 com_err(program_name, retval,
283 _("while reading bitmaps"));
286 retval = ext2fs_block_iterate(fs, ino,
287 BLOCK_FLAG_READ_ONLY, NULL,
288 release_blocks_proc, NULL);
290 com_err(program_name, retval,
291 _("while clearing journal inode"));
294 memset(&inode, 0, sizeof(inode));
295 ext2fs_mark_bb_dirty(fs);
296 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
298 inode.i_flags &= ~EXT2_IMMUTABLE_FL;
299 retval = ext2fs_write_inode(fs, ino, &inode);
301 com_err(program_name, retval,
302 _("while writing journal inode"));
305 fs->super->s_journal_inum = 0;
306 ext2fs_mark_super_dirty(fs);
310 * Update the default mount options
312 static void update_mntopts(ext2_filsys fs, char *mntopts)
314 struct ext2_super_block *sb= fs->super;
316 if (e2p_edit_mntopts(mntopts, &sb->s_default_mount_opts, ~0)) {
317 fprintf(stderr, _("Invalid mount option set: %s\n"),
321 ext2fs_mark_super_dirty(fs);
325 * Update the feature set as provided by the user.
327 static void update_feature_set(ext2_filsys fs, char *features)
329 struct ext2_super_block *sb= fs->super;
330 __u32 old_features[3];
332 unsigned int mask_err;
334 #define FEATURE_ON(type, mask) (!(old_features[(type)] & (mask)) && \
335 ((&sb->s_feature_compat)[(type)] & (mask)))
336 #define FEATURE_OFF(type, mask) ((old_features[(type)] & (mask)) && \
337 !((&sb->s_feature_compat)[(type)] & (mask)))
338 #define FEATURE_CHANGED(type, mask) ((mask) & \
339 (old_features[(type)] ^ (&sb->s_feature_compat)[(type)]))
341 old_features[E2P_FEATURE_COMPAT] = sb->s_feature_compat;
342 old_features[E2P_FEATURE_INCOMPAT] = sb->s_feature_incompat;
343 old_features[E2P_FEATURE_RO_INCOMPAT] = sb->s_feature_ro_compat;
345 if (e2p_edit_feature2(features, &sb->s_feature_compat,
346 ok_features, clear_ok_features,
347 &type_err, &mask_err)) {
350 _("Invalid filesystem option set: %s\n"),
352 else if (type_err & E2P_FEATURE_NEGATE_FLAG)
353 fprintf(stderr, _("Clearing filesystem feature '%s' "
355 e2p_feature2string(type_err &
356 E2P_FEATURE_TYPE_MASK,
359 fprintf(stderr, _("Setting filesystem feature '%s' "
361 e2p_feature2string(type_err, mask_err));
365 if (FEATURE_OFF(E2P_FEATURE_COMPAT, EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
366 if ((mount_flags & EXT2_MF_MOUNTED) &&
367 !(mount_flags & EXT2_MF_READONLY)) {
368 fputs(_("The has_journal feature may only be "
369 "cleared when the filesystem is\n"
370 "unmounted or mounted "
371 "read-only.\n"), stderr);
374 if (sb->s_feature_incompat &
375 EXT3_FEATURE_INCOMPAT_RECOVER) {
376 fputs(_("The needs_recovery flag is set. "
377 "Please run e2fsck before clearing\n"
378 "the has_journal flag.\n"), stderr);
381 if (sb->s_journal_inum) {
382 remove_journal_inode(fs);
384 if (sb->s_journal_dev) {
385 remove_journal_device(fs);
389 if (FEATURE_ON(E2P_FEATURE_COMPAT, EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
391 * If adding a journal flag, let the create journal
392 * code below handle setting the flag and creating the
393 * journal. We supply a default size if necessary.
397 sb->s_feature_compat &= ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
400 if (FEATURE_ON(E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_DIR_INDEX)) {
401 if (!sb->s_def_hash_version)
402 sb->s_def_hash_version = EXT2_HASH_HALF_MD4;
403 if (uuid_is_null((unsigned char *) sb->s_hash_seed))
404 uuid_generate((unsigned char *) sb->s_hash_seed);
407 if (FEATURE_OFF(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
408 if (ext2fs_check_desc(fs)) {
409 fputs(_("Clearing the flex_bg flag would "
410 "cause the the filesystem to be\n"
411 "inconsistent.\n"), stderr);
416 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
417 EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
418 if ((mount_flags & EXT2_MF_MOUNTED) &&
419 !(mount_flags & EXT2_MF_READONLY)) {
420 fputs(_("The huge_file feature may only be "
421 "cleared when the filesystem is\n"
422 "unmounted or mounted "
423 "read-only.\n"), stderr);
428 if (sb->s_rev_level == EXT2_GOOD_OLD_REV &&
429 (sb->s_feature_compat || sb->s_feature_ro_compat ||
430 sb->s_feature_incompat))
431 ext2fs_update_dynamic_rev(fs);
433 if (FEATURE_CHANGED(E2P_FEATURE_RO_INCOMPAT,
434 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER) ||
435 FEATURE_CHANGED(E2P_FEATURE_RO_INCOMPAT,
436 EXT4_FEATURE_RO_COMPAT_GDT_CSUM) ||
437 FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
438 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) ||
439 FEATURE_CHANGED(E2P_FEATURE_INCOMPAT,
440 EXT2_FEATURE_INCOMPAT_FILETYPE) ||
441 FEATURE_CHANGED(E2P_FEATURE_COMPAT,
442 EXT2_FEATURE_COMPAT_RESIZE_INODE) ||
443 FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
444 EXT2_FEATURE_RO_COMPAT_LARGE_FILE)) {
445 sb->s_state &= ~EXT2_VALID_FS;
446 printf("\n%s\n", _(please_fsck));
447 if (mount_flags & EXT2_MF_READONLY)
448 printf(_("(and reboot afterwards!)\n"));
451 if ((old_features[E2P_FEATURE_COMPAT] != sb->s_feature_compat) ||
452 (old_features[E2P_FEATURE_INCOMPAT] != sb->s_feature_incompat) ||
453 (old_features[E2P_FEATURE_RO_INCOMPAT] != sb->s_feature_ro_compat))
454 ext2fs_mark_super_dirty(fs);
458 * Add a journal to the filesystem.
460 static void add_journal(ext2_filsys fs)
462 unsigned long journal_blocks;
467 if (fs->super->s_feature_compat &
468 EXT3_FEATURE_COMPAT_HAS_JOURNAL) {
469 fputs(_("The filesystem already has a journal.\n"), stderr);
472 if (journal_device) {
473 check_plausibility(journal_device);
474 check_mount(journal_device, 0, _("journal"));
475 #ifdef CONFIG_TESTIO_DEBUG
476 io_ptr = test_io_manager;
477 test_io_backing_manager = unix_io_manager;
479 io_ptr = unix_io_manager;
481 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
482 EXT2_FLAG_JOURNAL_DEV_OK, 0,
483 fs->blocksize, io_ptr, &jfs);
485 com_err(program_name, retval,
486 _("\n\twhile trying to open journal on %s\n"),
490 printf(_("Creating journal on device %s: "),
494 retval = ext2fs_add_journal_device(fs, jfs);
497 com_err (program_name, retval,
498 _("while adding filesystem to journal on %s"),
502 fputs(_("done\n"), stdout);
503 } else if (journal_size) {
504 fputs(_("Creating journal inode: "), stdout);
506 journal_blocks = figure_journal_size(journal_size, fs);
508 retval = ext2fs_add_journal_inode(fs, journal_blocks,
511 fprintf(stderr, "\n");
512 com_err(program_name, retval,
513 _("\n\twhile trying to create journal file"));
516 fputs(_("done\n"), stdout);
518 * If the filesystem wasn't mounted, we need to force
519 * the block group descriptors out.
521 if ((mount_flags & EXT2_MF_MOUNTED) == 0)
522 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
524 print_check_message(fs);
529 free(journal_device);
534 static void parse_e2label_options(int argc, char ** argv)
536 if ((argc < 2) || (argc > 3)) {
537 fputs(_("Usage: e2label device [newlabel]\n"), stderr);
540 io_options = strchr(argv[1], '?');
543 device_name = blkid_get_devname(NULL, argv[1], NULL);
545 com_err("e2label", 0, _("Unable to resolve '%s'"),
549 open_flag = EXT2_FLAG_JOURNAL_DEV_OK;
551 open_flag |= EXT2_FLAG_RW;
558 static time_t parse_time(char *str)
562 if (strcmp(str, "now") == 0) {
565 memset(&ts, 0, sizeof(ts));
567 strptime(str, "%Y%m%d%H%M%S", &ts);
569 sscanf(str, "%4d%2d%2d%2d%2d%2d", &ts.tm_year, &ts.tm_mon,
570 &ts.tm_mday, &ts.tm_hour, &ts.tm_min, &ts.tm_sec);
573 if (ts.tm_year < 0 || ts.tm_mon < 0 || ts.tm_mon > 11 ||
574 ts.tm_mday < 0 || ts.tm_mday > 31 || ts.tm_hour > 23 ||
575 ts.tm_min > 59 || ts.tm_sec > 61)
578 if (ts.tm_mday == 0) {
579 com_err(program_name, 0,
580 _("Couldn't parse date/time specifier: %s"),
585 return (mktime(&ts));
588 static void parse_tune2fs_options(int argc, char **argv)
597 printf("tune2fs %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
598 while ((c = getopt(argc, argv, "c:e:fg:i:jlm:o:r:s:u:C:E:I:J:L:M:O:T:U:")) != EOF)
602 max_mount_count = strtol (optarg, &tmp, 0);
603 if (*tmp || max_mount_count > 16000) {
604 com_err (program_name, 0,
605 _("bad mounts count - %s"),
609 if (max_mount_count == 0)
610 max_mount_count = -1;
612 open_flag = EXT2_FLAG_RW;
615 mount_count = strtoul (optarg, &tmp, 0);
616 if (*tmp || mount_count > 16000) {
617 com_err (program_name, 0,
618 _("bad mounts count - %s"),
623 open_flag = EXT2_FLAG_RW;
626 if (strcmp (optarg, "continue") == 0)
627 errors = EXT2_ERRORS_CONTINUE;
628 else if (strcmp (optarg, "remount-ro") == 0)
629 errors = EXT2_ERRORS_RO;
630 else if (strcmp (optarg, "panic") == 0)
631 errors = EXT2_ERRORS_PANIC;
633 com_err (program_name, 0,
634 _("bad error behavior - %s"),
639 open_flag = EXT2_FLAG_RW;
642 extended_cmd = optarg;
643 open_flag |= EXT2_FLAG_RW;
645 case 'f': /* Force */
649 resgid = strtoul (optarg, &tmp, 0);
651 gr = getgrnam (optarg);
660 com_err (program_name, 0,
661 _("bad gid/group name - %s"),
666 open_flag = EXT2_FLAG_RW;
669 interval = strtoul (optarg, &tmp, 0);
682 case 'M': /* months! */
683 interval *= 86400 * 30;
687 case 'W': /* weeks */
688 interval *= 86400 * 7;
693 com_err (program_name, 0,
694 _("bad interval - %s"), optarg);
698 open_flag = EXT2_FLAG_RW;
703 open_flag = EXT2_FLAG_RW;
706 parse_journal_opts(optarg);
707 open_flag = EXT2_FLAG_RW;
715 open_flag |= EXT2_FLAG_RW |
716 EXT2_FLAG_JOURNAL_DEV_OK;
719 reserved_ratio = strtod(optarg, &tmp);
720 if (*tmp || reserved_ratio > 50) {
721 com_err (program_name, 0,
722 _("bad reserved block ratio - %s"),
727 open_flag = EXT2_FLAG_RW;
730 new_last_mounted = optarg;
732 open_flag = EXT2_FLAG_RW;
736 com_err (program_name, 0,
737 _("-o may only be specified once"));
740 mntopts_cmd = optarg;
741 open_flag = EXT2_FLAG_RW;
746 com_err (program_name, 0,
747 _("-O may only be specified once"));
750 features_cmd = optarg;
751 open_flag = EXT2_FLAG_RW;
754 reserved_blocks = strtoul (optarg, &tmp, 0);
756 com_err (program_name, 0,
757 _("bad reserved blocks count - %s"),
762 open_flag = EXT2_FLAG_RW;
764 case 's': /* Deprecated */
765 s_flag = atoi(optarg);
766 open_flag = EXT2_FLAG_RW;
770 last_check_time = parse_time(optarg);
771 open_flag = EXT2_FLAG_RW;
774 resuid = strtoul (optarg, &tmp, 0);
776 pw = getpwnam (optarg);
785 com_err (program_name, 0,
786 _("bad uid/user name - %s"),
791 open_flag = EXT2_FLAG_RW;
796 open_flag = EXT2_FLAG_RW |
797 EXT2_FLAG_JOURNAL_DEV_OK;
800 new_inode_size = strtoul (optarg, &tmp, 0);
802 com_err (program_name, 0,
803 _("bad inode size - %s"),
807 if (!((new_inode_size &
808 (new_inode_size - 1)) == 0)) {
809 com_err (program_name, 0,
810 _("Inode size must be a "
815 open_flag = EXT2_FLAG_RW;
821 if (optind < argc - 1 || optind == argc)
823 if (!open_flag && !l_flag)
825 io_options = strchr(argv[optind], '?');
828 device_name = blkid_get_devname(NULL, argv[optind], NULL);
830 com_err("tune2fs", 0, _("Unable to resolve '%s'"),
836 void do_findfs(int argc, char **argv)
841 (strncmp(argv[1], "LABEL=", 6) && strncmp(argv[1], "UUID=", 5))) {
842 fprintf(stderr, "Usage: findfs LABEL=<label>|UUID=<uuid>\n");
845 dev = blkid_get_devname(NULL, argv[1], NULL);
847 com_err("findfs", 0, _("Unable to resolve '%s'"),
855 static void parse_extended_opts(ext2_filsys fs, const char *opts)
857 char *buf, *token, *next, *p, *arg;
865 _("Couldn't allocate memory to parse options!\n"));
869 for (token = buf; token && *token; token = next) {
870 p = strchr(token, ',');
876 arg = strchr(token, '=');
881 if (!strcmp(token, "test_fs")) {
882 fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
883 printf("Setting test filesystem flag\n");
884 ext2fs_mark_super_dirty(fs);
885 } else if (!strcmp(token, "^test_fs")) {
886 fs->super->s_flags &= ~EXT2_FLAGS_TEST_FILESYS;
887 printf("Clearing test filesystem flag\n");
888 ext2fs_mark_super_dirty(fs);
889 } else if (strcmp(token, "stride") == 0) {
894 stride = strtoul(arg, &p, 0);
895 if (*p || (stride == 0)) {
897 _("Invalid RAID stride: %s\n"),
903 } else if (strcmp(token, "stripe-width") == 0 ||
904 strcmp(token, "stripe_width") == 0) {
909 stripe_width = strtoul(arg, &p, 0);
910 if (*p || (stripe_width == 0)) {
912 _("Invalid RAID stripe-width: %s\n"),
917 stripe_width_set = 1;
922 fprintf(stderr, _("\nBad options specified.\n\n"
923 "Extended options are separated by commas, "
924 "and may take an argument which\n"
925 "\tis set off by an equals ('=') sign.\n\n"
926 "Valid extended options are:\n"
927 "\tstride=<RAID per-disk chunk size in blocks>\n"
928 "\tstripe-width=<RAID stride*data disks in blocks>\n"
937 static int get_move_bitmap(ext2_filsys fs, int new_ino_blks_per_grp,
938 ext2fs_block_bitmap bmap)
941 blk_t j, needed_blocks = 0;
942 blk_t start_blk, end_blk;
944 for (i = 0; i < fs->group_desc_count; i++) {
946 start_blk = fs->group_desc[i].bg_inode_table +
947 fs->inode_blocks_per_group;
949 end_blk = fs->group_desc[i].bg_inode_table +
950 new_ino_blks_per_grp;
952 for (j = start_blk; j < end_blk; j++) {
954 if (ext2fs_test_block_bitmap(fs->block_map, j)) {
956 * What happens if the block is marked
959 ext2fs_mark_block_bitmap(bmap, j);
963 * We are going to use this block for
964 * inode table. So mark them used.
966 ext2fs_mark_block_bitmap(fs->block_map, j);
971 if (needed_blocks > fs->super->s_free_blocks_count ) {
978 static int move_block(ext2_filsys fs, ext2fs_block_bitmap bmap)
983 struct blk_move *bmv;
986 retval = ext2fs_get_mem(fs->blocksize, &buf);
990 for (blk = fs->super->s_first_data_block;
991 blk < fs->super->s_blocks_count; blk++) {
993 if (!ext2fs_test_block_bitmap(bmap, blk))
996 retval = ext2fs_new_block(fs, blk, NULL, &new_blk);
1000 /* Mark this block as allocated */
1001 ext2fs_mark_block_bitmap(fs->block_map, new_blk);
1003 /* Add it to block move list */
1004 retval = ext2fs_get_mem(sizeof(struct blk_move), &bmv);
1009 bmv->new_loc = new_blk;
1011 list_add(&(bmv->list), &blk_move_list);
1013 retval = io_channel_read_blk(fs->io, blk, 1, buf);
1017 retval = io_channel_write_blk(fs->io, new_blk, 1, buf);
1023 ext2fs_free_mem(&buf);
1027 static blk_t transalate_block(blk_t blk)
1029 struct list_head *entry;
1030 struct blk_move *bmv;
1032 list_for_each(entry, &blk_move_list) {
1034 bmv = list_entry(entry, struct blk_move, list);
1035 if (bmv->old_loc == blk)
1036 return bmv->new_loc;
1042 static int process_block(ext2_filsys fs EXT2FS_ATTR((unused)),
1044 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1045 blk_t ref_block EXT2FS_ATTR((unused)),
1046 int ref_offset EXT2FS_ATTR((unused)),
1047 void *priv_data EXT2FS_ATTR((unused)))
1053 new_blk = transalate_block(*block_nr);
1055 *block_nr = new_blk;
1057 * This will force the ext2fs_write_inode in the iterator
1059 ret |= BLOCK_CHANGED;
1065 static int inode_scan_and_fix(ext2_filsys fs)
1067 errcode_t retval = 0;
1070 char *block_buf = 0;
1071 struct ext2_inode inode;
1072 ext2_inode_scan scan = NULL;
1074 retval = ext2fs_get_mem(fs->blocksize * 3, &block_buf);
1078 retval = ext2fs_open_inode_scan(fs, 0, &scan);
1084 retval = ext2fs_get_next_inode(scan, &ino, &inode);
1091 if (inode.i_links_count == 0)
1092 continue; /* inode not in use */
1095 * If we end up modifying the journal inode
1096 * the sb->s_jnl_blocks will differ. But a
1097 * subsequent e2fsck fixes that.
1098 * Do we need to fix this ??
1101 if (inode.i_file_acl) {
1103 blk = transalate_block(inode.i_file_acl);
1107 inode.i_file_acl = blk;
1110 * Write the inode to disk so that inode table
1113 retval = ext2fs_write_inode(fs, ino, &inode);
1118 if (!ext2fs_inode_has_valid_blocks(&inode))
1121 retval = ext2fs_block_iterate2(fs, ino, 0,
1122 block_buf, process_block,
1130 ext2fs_free_mem(&block_buf);
1136 static int expand_inode_table(ext2_filsys fs, unsigned long new_ino_size)
1141 int new_ino_blks_per_grp;
1143 char *old_itable = NULL, *new_itable = NULL;
1144 char *tmp_old_itable = NULL, *tmp_new_itable = NULL;
1145 unsigned long old_ino_size;
1146 int old_itable_size, new_itable_size;
1148 old_itable_size = fs->inode_blocks_per_group * fs->blocksize;
1149 old_ino_size = EXT2_INODE_SIZE(fs->super);
1151 new_ino_blks_per_grp = ext2fs_div_ceil(
1152 EXT2_INODES_PER_GROUP(fs->super) *
1156 new_itable_size = new_ino_blks_per_grp * fs->blocksize;
1158 retval = ext2fs_get_mem(old_itable_size, &old_itable);
1162 retval = ext2fs_get_mem(new_itable_size, &new_itable);
1166 tmp_old_itable = old_itable;
1167 tmp_new_itable = new_itable;
1169 for (i = 0; i < fs->group_desc_count; i++) {
1171 blk = fs->group_desc[i].bg_inode_table;
1172 retval = io_channel_read_blk(fs->io, blk,
1173 fs->inode_blocks_per_group, old_itable);
1177 for (j = 0; j < EXT2_INODES_PER_GROUP(fs->super); j++) {
1179 memcpy(new_itable, old_itable, old_ino_size);
1181 memset(new_itable+old_ino_size, 0,
1182 new_ino_size - old_ino_size);
1184 new_itable += new_ino_size;
1185 old_itable += old_ino_size;
1188 /* reset the pointer */
1189 old_itable = tmp_old_itable;
1190 new_itable = tmp_new_itable;
1192 retval = io_channel_write_blk(fs->io, blk,
1193 new_ino_blks_per_grp, new_itable);
1198 /* Update the meta data */
1199 fs->inode_blocks_per_group = new_ino_blks_per_grp;
1200 fs->super->s_inode_size = new_ino_size;
1204 ext2fs_free_mem(&old_itable);
1207 ext2fs_free_mem(&new_itable);
1212 static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs)
1216 unsigned int group = 0;
1217 unsigned int count = 0;
1222 * First calculate the block statistics
1224 for (blk = fs->super->s_first_data_block;
1225 blk < fs->super->s_blocks_count; blk++) {
1226 if (!ext2fs_fast_test_block_bitmap(fs->block_map, blk)) {
1231 if ((count == fs->super->s_blocks_per_group) ||
1232 (blk == fs->super->s_blocks_count-1)) {
1233 fs->group_desc[group++].bg_free_blocks_count =
1239 fs->super->s_free_blocks_count = total_free;
1242 * Next, calculate the inode statistics
1249 /* Protect loop from wrap-around if s_inodes_count maxed */
1250 for (ino = 1; ino <= fs->super->s_inodes_count && ino > 0; ino++) {
1251 if (!ext2fs_fast_test_inode_bitmap(fs->inode_map, ino)) {
1256 if ((count == fs->super->s_inodes_per_group) ||
1257 (ino == fs->super->s_inodes_count)) {
1258 fs->group_desc[group++].bg_free_inodes_count =
1264 fs->super->s_free_inodes_count = total_free;
1265 ext2fs_mark_super_dirty(fs);
1269 #define list_for_each_safe(pos, pnext, head) \
1270 for (pos = (head)->next, pnext = pos->next; pos != (head); \
1271 pos = pnext, pnext = pos->next)
1273 static void free_blk_move_list(void)
1275 struct list_head *entry, *tmp;
1276 struct blk_move *bmv;
1278 list_for_each_safe(entry, tmp, &blk_move_list) {
1280 bmv = list_entry(entry, struct blk_move, list);
1282 ext2fs_free_mem(&bmv);
1288 static int resize_inode(ext2_filsys fs, unsigned long new_size)
1291 int new_ino_blks_per_grp;
1292 ext2fs_block_bitmap bmap;
1294 ext2fs_read_inode_bitmap(fs);
1295 ext2fs_read_block_bitmap(fs);
1296 INIT_LIST_HEAD(&blk_move_list);
1299 new_ino_blks_per_grp = ext2fs_div_ceil(
1300 EXT2_INODES_PER_GROUP(fs->super)*
1304 /* We may change the file system.
1305 * Mark the file system as invalid so that
1306 * the user is prompted to run fsck.
1308 fs->super->s_state &= ~EXT2_VALID_FS;
1310 retval = ext2fs_allocate_block_bitmap(fs, _("blocks to be moved"),
1315 retval = get_move_bitmap(fs, new_ino_blks_per_grp, bmap);
1319 retval = move_block(fs, bmap);
1323 retval = inode_scan_and_fix(fs);
1327 retval = expand_inode_table(fs, new_size);
1331 ext2fs_calculate_summary_stats(fs);
1333 fs->super->s_state |= EXT2_VALID_FS;
1334 /* mark super block and block bitmap as dirty */
1335 ext2fs_mark_super_dirty(fs);
1336 ext2fs_mark_bb_dirty(fs);
1339 free_blk_move_list();
1340 ext2fs_free_block_bitmap(bmap);
1345 static int tune2fs_setup_tdb(const char *name, io_manager *io_ptr)
1347 errcode_t retval = 0;
1348 const char *tdb_dir;
1349 char tdb_file[PATH_MAX];
1350 char *dev_name, *tmp_name;
1354 * Configuration via a conf file would be
1357 profile_get_string(profile, "scratch_files",
1361 tmp_name = strdup(name);
1362 dev_name = basename(tmp_name);
1364 tdb_dir = getenv("E2FSPROGS_UNDO_DIR");
1366 tdb_dir="/var/lib/e2fsprogs";
1368 if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
1369 access(tdb_dir, W_OK))
1372 sprintf(tdb_file, "%s/tune2fs-%s.e2undo", tdb_dir, dev_name);
1374 if (!access(tdb_file, F_OK)) {
1375 if (unlink(tdb_file) < 0) {
1377 com_err(program_name, retval,
1378 _("while trying to delete %s"),
1384 set_undo_io_backing_manager(*io_ptr);
1385 *io_ptr = undo_io_manager;
1386 set_undo_io_backup_file(tdb_file);
1387 printf(_("To undo the tune2fs operations please run "
1388 "the command\n e2undo %s %s\n\n"),
1394 int main (int argc, char ** argv)
1398 struct ext2_super_block *sb;
1399 io_manager io_ptr, io_ptr_orig = NULL;
1402 setlocale(LC_MESSAGES, "");
1403 setlocale(LC_CTYPE, "");
1404 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1405 textdomain(NLS_CAT_NAME);
1408 program_name = *argv;
1409 add_error_table(&et_ext2_error_table);
1411 if (strcmp(get_progname(argv[0]), "findfs") == 0)
1412 do_findfs(argc, argv);
1413 if (strcmp(get_progname(argv[0]), "e2label") == 0)
1414 parse_e2label_options(argc, argv);
1416 parse_tune2fs_options(argc, argv);
1418 #ifdef CONFIG_TESTIO_DEBUG
1419 io_ptr = test_io_manager;
1420 test_io_backing_manager = unix_io_manager;
1422 io_ptr = unix_io_manager;
1426 retval = ext2fs_open2(device_name, io_options, open_flag,
1429 com_err (program_name, retval, _("while trying to open %s"),
1432 _("Couldn't find valid filesystem superblock.\n"));
1436 if (I_flag && !io_ptr_orig) {
1438 * Check the inode size is right so we can issue an
1439 * error message and bail before setting up the tdb
1442 if (new_inode_size == EXT2_INODE_SIZE(fs->super)) {
1443 fprintf(stderr, _("The inode size is already %d\n"),
1447 if (new_inode_size < EXT2_INODE_SIZE(fs->super)) {
1448 fprintf(stderr, _("Shrinking the inode size is "
1449 "not supported\n"));
1454 * If inode resize is requested use the
1457 io_ptr_orig = io_ptr;
1458 retval = tune2fs_setup_tdb(device_name, &io_ptr);
1461 if (io_ptr != io_ptr_orig) {
1468 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
1471 /* For e2label emulation */
1472 printf("%.*s\n", (int) sizeof(sb->s_volume_name),
1474 remove_error_table(&et_ext2_error_table);
1478 retval = ext2fs_check_if_mounted(device_name, &mount_flags);
1480 com_err("ext2fs_check_if_mount", retval,
1481 _("while determining whether %s is mounted."),
1485 /* Normally we only need to write out the superblock */
1486 fs->flags |= EXT2_FLAG_SUPER_ONLY;
1489 sb->s_max_mnt_count = max_mount_count;
1490 ext2fs_mark_super_dirty(fs);
1491 printf (_("Setting maximal mount count to %d\n"),
1495 sb->s_mnt_count = mount_count;
1496 ext2fs_mark_super_dirty(fs);
1497 printf (_("Setting current mount count to %d\n"), mount_count);
1500 sb->s_errors = errors;
1501 ext2fs_mark_super_dirty(fs);
1502 printf (_("Setting error behavior to %d\n"), errors);
1505 sb->s_def_resgid = resgid;
1506 ext2fs_mark_super_dirty(fs);
1507 printf (_("Setting reserved blocks gid to %lu\n"), resgid);
1510 sb->s_checkinterval = interval;
1511 ext2fs_mark_super_dirty(fs);
1512 printf (_("Setting interval between checks to %lu seconds\n"), interval);
1515 sb->s_r_blocks_count = (unsigned int) (reserved_ratio *
1516 sb->s_blocks_count / 100.0);
1517 ext2fs_mark_super_dirty(fs);
1518 printf (_("Setting reserved blocks percentage to %g%% (%u blocks)\n"),
1519 reserved_ratio, sb->s_r_blocks_count);
1522 if (reserved_blocks >= sb->s_blocks_count/2) {
1523 com_err (program_name, 0,
1524 _("reserved blocks count is too big (%lu)"),
1528 sb->s_r_blocks_count = reserved_blocks;
1529 ext2fs_mark_super_dirty(fs);
1530 printf (_("Setting reserved blocks count to %lu\n"),
1534 if (sb->s_feature_ro_compat &
1535 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)
1536 fputs(_("\nThe filesystem already has sparse "
1537 "superblocks.\n"), stderr);
1539 sb->s_feature_ro_compat |=
1540 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1541 sb->s_state &= ~EXT2_VALID_FS;
1542 ext2fs_mark_super_dirty(fs);
1543 printf(_("\nSparse superblock flag set. %s"),
1548 fputs(_("\nClearing the sparse superflag not supported.\n"),
1553 sb->s_lastcheck = last_check_time;
1554 ext2fs_mark_super_dirty(fs);
1555 printf(_("Setting time filesystem last checked to %s\n"),
1556 ctime(&last_check_time));
1559 sb->s_def_resuid = resuid;
1560 ext2fs_mark_super_dirty(fs);
1561 printf (_("Setting reserved blocks uid to %lu\n"), resuid);
1564 if (strlen(new_label) > sizeof(sb->s_volume_name))
1565 fputs(_("Warning: label too long, truncating.\n"),
1567 memset(sb->s_volume_name, 0, sizeof(sb->s_volume_name));
1568 strncpy(sb->s_volume_name, new_label,
1569 sizeof(sb->s_volume_name));
1570 ext2fs_mark_super_dirty(fs);
1573 memset(sb->s_last_mounted, 0, sizeof(sb->s_last_mounted));
1574 strncpy(sb->s_last_mounted, new_last_mounted,
1575 sizeof(sb->s_last_mounted));
1576 ext2fs_mark_super_dirty(fs);
1579 update_mntopts(fs, mntopts_cmd);
1581 update_feature_set(fs, features_cmd);
1583 parse_extended_opts(fs, extended_cmd);
1584 if (journal_size || journal_device)
1588 if ((strcasecmp(new_UUID, "null") == 0) ||
1589 (strcasecmp(new_UUID, "clear") == 0)) {
1590 uuid_clear(sb->s_uuid);
1591 } else if (strcasecmp(new_UUID, "time") == 0) {
1592 uuid_generate_time(sb->s_uuid);
1593 } else if (strcasecmp(new_UUID, "random") == 0) {
1594 uuid_generate(sb->s_uuid);
1595 } else if (uuid_parse(new_UUID, sb->s_uuid)) {
1596 com_err(program_name, 0, _("Invalid UUID format\n"));
1599 ext2fs_mark_super_dirty(fs);
1602 if (mount_flags & EXT2_MF_MOUNTED) {
1603 fputs(_("The inode size may only be "
1604 "changed when the filesystem is "
1605 "unmounted.\n"), stderr);
1609 * We want to update group descriptor also
1610 * with the new free inode count
1612 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
1613 if (resize_inode(fs, new_inode_size)) {
1614 fputs(_("Error in resizing the inode size.\n"
1615 "Run e2undo to undo the "
1616 "file system changes. \n"), stderr);
1618 printf (_("Setting inode size %lu\n"),
1626 sb->s_raid_stride = stride;
1627 ext2fs_mark_super_dirty(fs);
1628 printf(_("Setting stride size to %d\n"), stride);
1630 if (stripe_width_set) {
1631 sb->s_raid_stripe_width = stripe_width;
1632 ext2fs_mark_super_dirty(fs);
1633 printf(_("Setting stripe width to %d\n"), stripe_width);
1635 remove_error_table(&et_ext2_error_table);
1636 return (ext2fs_close (fs) ? 1 : 0);