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 500 /* for inclusion of strptime() */
29 #define _BSD_SOURCE /* for inclusion of strcasecmp() */
44 #include <sys/types.h>
47 #include "ext2fs/ext2_fs.h"
48 #include "ext2fs/ext2fs.h"
49 #include "et/com_err.h"
50 #include "uuid/uuid.h"
54 #include "blkid/blkid.h"
56 #include "../version.h"
57 #include "nls-enable.h"
60 * Tune2fs supports these features in addition to the standard features.
62 #define EXT2_TUNE2FS_INCOMPAT (EXT3_FEATURE_INCOMPAT_EXTENTS)
63 #define EXT2_TUNE2FS_RO_COMPAT (EXT4_FEATURE_RO_COMPAT_HUGE_FILE|\
64 EXT4_FEATURE_RO_COMPAT_GDT_CSUM| \
65 EXT4_FEATURE_RO_COMPAT_DIR_NLINK| \
66 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)
69 const char * program_name = "tune2fs";
71 char * new_label, *new_last_mounted, *new_UUID;
73 static int c_flag, C_flag, e_flag, f_flag, g_flag, i_flag, l_flag, L_flag;
74 static int m_flag, M_flag, r_flag, s_flag = -1, u_flag, U_flag, T_flag;
76 static time_t last_check_time;
77 static int print_label;
78 static int max_mount_count, mount_count, mount_flags;
79 static unsigned long interval, 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;
91 int journal_size, journal_flags;
94 static struct list_head blk_move_list;
97 struct list_head list;
103 static const char *please_fsck = N_("Please run e2fsck on the filesystem.\n");
105 void do_findfs(int argc, char **argv);
107 static void usage(void)
110 _("Usage: %s [-c max_mounts_count] [-e errors_behavior] "
112 "\t[-i interval[d|m|w]] [-j] [-J journal_options] [-l]\n"
113 "\t[-m reserved_blocks_percent] "
114 "[-o [^]mount_options[,...]] \n"
115 "\t[-r reserved_blocks_count] [-u user] [-C mount_count] "
116 "[-L volume_label]\n"
117 "\t[-M last_mounted_dir] [-O [^]feature[,...]]\n"
118 "\t[-E extended-option[,...]] [-T last_check_time] "
119 "[-U UUID]\n\t[ -I new_inode_size ] device\n"), program_name);
123 static __u32 ok_features[3] = {
125 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
126 EXT2_FEATURE_COMPAT_DIR_INDEX,
128 EXT2_FEATURE_INCOMPAT_FILETYPE |
129 EXT3_FEATURE_INCOMPAT_EXTENTS |
130 EXT4_FEATURE_INCOMPAT_FLEX_BG,
132 EXT2_FEATURE_RO_COMPAT_LARGE_FILE |
133 EXT4_FEATURE_RO_COMPAT_GDT_CSUM |
134 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
137 static __u32 clear_ok_features[3] = {
139 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
140 EXT2_FEATURE_COMPAT_RESIZE_INODE |
141 EXT2_FEATURE_COMPAT_DIR_INDEX,
143 EXT2_FEATURE_INCOMPAT_FILETYPE |
144 EXT4_FEATURE_INCOMPAT_FLEX_BG,
146 EXT2_FEATURE_RO_COMPAT_LARGE_FILE |
147 EXT4_FEATURE_RO_COMPAT_GDT_CSUM
151 * Remove an external journal from the filesystem
153 static void remove_journal_device(ext2_filsys fs)
158 journal_superblock_t *jsb;
161 int commit_remove_journal = 0;
165 commit_remove_journal = 1; /* force removal even if error */
167 uuid_unparse(fs->super->s_journal_uuid, buf);
168 journal_path = blkid_get_devname(NULL, "UUID", buf);
172 ext2fs_find_block_device(fs->super->s_journal_dev);
177 #ifdef CONFIG_TESTIO_DEBUG
178 io_ptr = test_io_manager;
179 test_io_backing_manager = unix_io_manager;
181 io_ptr = unix_io_manager;
183 retval = ext2fs_open(journal_path, EXT2_FLAG_RW|
184 EXT2_FLAG_JOURNAL_DEV_OK, 0,
185 fs->blocksize, io_ptr, &jfs);
187 com_err(program_name, retval,
188 _("while trying to open external journal"));
189 goto no_valid_journal;
191 if (!(jfs->super->s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
192 fprintf(stderr, _("%s is not a journal device.\n"),
194 goto no_valid_journal;
197 /* Get the journal superblock */
198 if ((retval = io_channel_read_blk(jfs->io, 1, -1024, buf))) {
199 com_err(program_name, retval,
200 _("while reading journal superblock"));
201 goto no_valid_journal;
204 jsb = (journal_superblock_t *) buf;
205 if ((jsb->s_header.h_magic != (unsigned) ntohl(JFS_MAGIC_NUMBER)) ||
206 (jsb->s_header.h_blocktype != (unsigned) ntohl(JFS_SUPERBLOCK_V2))) {
207 fputs(_("Journal superblock not found!\n"), stderr);
208 goto no_valid_journal;
211 /* Find the filesystem UUID */
212 nr_users = ntohl(jsb->s_nr_users);
213 for (i=0; i < nr_users; i++) {
214 if (memcmp(fs->super->s_uuid,
215 &jsb->s_users[i*16], 16) == 0)
219 fputs(_("Filesystem's UUID not found on journal device.\n"),
221 commit_remove_journal = 1;
222 goto no_valid_journal;
225 for (i=0; i < nr_users; i++)
226 memcpy(&jsb->s_users[i*16], &jsb->s_users[(i+1)*16], 16);
227 jsb->s_nr_users = htonl(nr_users);
229 /* Write back the journal superblock */
230 if ((retval = io_channel_write_blk(jfs->io, 1, -1024, buf))) {
231 com_err(program_name, retval,
232 "while writing journal superblock.");
233 goto no_valid_journal;
236 commit_remove_journal = 1;
239 if (commit_remove_journal == 0) {
240 fputs(_("Journal NOT removed\n"), stderr);
243 fs->super->s_journal_dev = 0;
244 uuid_clear(fs->super->s_journal_uuid);
245 ext2fs_mark_super_dirty(fs);
246 fputs(_("Journal removed\n"), stdout);
250 /* Helper function for remove_journal_inode */
251 static int release_blocks_proc(ext2_filsys fs, blk_t *blocknr,
252 int blockcnt EXT2FS_ATTR((unused)),
253 void *private EXT2FS_ATTR((unused)))
259 ext2fs_unmark_block_bitmap(fs->block_map,block);
260 group = ext2fs_group_of_blk(fs, block);
261 fs->group_desc[group].bg_free_blocks_count++;
262 ext2fs_group_desc_csum_set(fs, group);
263 fs->super->s_free_blocks_count++;
268 * Remove the journal inode from the filesystem
270 static void remove_journal_inode(ext2_filsys fs)
272 struct ext2_inode inode;
274 ino_t ino = fs->super->s_journal_inum;
276 retval = ext2fs_read_inode(fs, ino, &inode);
278 com_err(program_name, retval,
279 _("while reading journal inode"));
282 if (ino == EXT2_JOURNAL_INO) {
283 retval = ext2fs_read_bitmaps(fs);
285 com_err(program_name, retval,
286 _("while reading bitmaps"));
289 retval = ext2fs_block_iterate(fs, ino,
290 BLOCK_FLAG_READ_ONLY, NULL,
291 release_blocks_proc, NULL);
293 com_err(program_name, retval,
294 _("while clearing journal inode"));
297 memset(&inode, 0, sizeof(inode));
298 ext2fs_mark_bb_dirty(fs);
299 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
301 inode.i_flags &= ~EXT2_IMMUTABLE_FL;
302 retval = ext2fs_write_inode(fs, ino, &inode);
304 com_err(program_name, retval,
305 _("while writing journal inode"));
308 fs->super->s_journal_inum = 0;
309 ext2fs_mark_super_dirty(fs);
313 * Update the default mount options
315 static void update_mntopts(ext2_filsys fs, char *mntopts)
317 struct ext2_super_block *sb= fs->super;
319 if (e2p_edit_mntopts(mntopts, &sb->s_default_mount_opts, ~0)) {
320 fprintf(stderr, _("Invalid mount option set: %s\n"),
324 ext2fs_mark_super_dirty(fs);
328 * Update the feature set as provided by the user.
330 static void update_feature_set(ext2_filsys fs, char *features)
332 struct ext2_super_block *sb= fs->super;
333 __u32 old_features[3];
335 unsigned int mask_err;
337 #define FEATURE_ON(type, mask) (!(old_features[(type)] & (mask)) && \
338 ((&sb->s_feature_compat)[(type)] & (mask)))
339 #define FEATURE_OFF(type, mask) ((old_features[(type)] & (mask)) && \
340 !((&sb->s_feature_compat)[(type)] & (mask)))
341 #define FEATURE_CHANGED(type, mask) ((mask) & \
342 (old_features[(type)] ^ (&sb->s_feature_compat)[(type)]))
344 old_features[E2P_FEATURE_COMPAT] = sb->s_feature_compat;
345 old_features[E2P_FEATURE_INCOMPAT] = sb->s_feature_incompat;
346 old_features[E2P_FEATURE_RO_INCOMPAT] = sb->s_feature_ro_compat;
348 if (e2p_edit_feature2(features, &sb->s_feature_compat,
349 ok_features, clear_ok_features,
350 &type_err, &mask_err)) {
353 _("Invalid filesystem option set: %s\n"),
355 else if (type_err & E2P_FEATURE_NEGATE_FLAG)
356 fprintf(stderr, _("Clearing filesystem feature '%s' "
358 e2p_feature2string(type_err &
359 E2P_FEATURE_TYPE_MASK,
362 fprintf(stderr, _("Setting filesystem feature '%s' "
364 e2p_feature2string(type_err, mask_err));
368 if (FEATURE_OFF(E2P_FEATURE_COMPAT, EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
369 if ((mount_flags & EXT2_MF_MOUNTED) &&
370 !(mount_flags & EXT2_MF_READONLY)) {
371 fputs(_("The has_journal flag may only be "
372 "cleared when the filesystem is\n"
373 "unmounted or mounted "
374 "read-only.\n"), stderr);
377 if (sb->s_feature_incompat &
378 EXT3_FEATURE_INCOMPAT_RECOVER) {
379 fputs(_("The needs_recovery flag is set. "
380 "Please run e2fsck before clearing\n"
381 "the has_journal flag.\n"), stderr);
384 if (sb->s_journal_inum) {
385 remove_journal_inode(fs);
387 if (sb->s_journal_dev) {
388 remove_journal_device(fs);
392 if (FEATURE_ON(E2P_FEATURE_COMPAT, EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
394 * If adding a journal flag, let the create journal
395 * code below handle creating setting the flag and
396 * creating the journal. We supply a default size if
401 sb->s_feature_compat &= ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
404 if (FEATURE_ON(E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_DIR_INDEX)) {
405 if (!sb->s_def_hash_version)
406 sb->s_def_hash_version = EXT2_HASH_TEA;
407 if (uuid_is_null((unsigned char *) sb->s_hash_seed))
408 uuid_generate((unsigned char *) sb->s_hash_seed);
411 if (FEATURE_OFF(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
412 if (ext2fs_check_desc(fs)) {
413 fputs(_("Clearing the flex_bg flag would "
414 "cause the the filesystem to be\n"
415 "inconsistent.\n"), stderr);
420 if (sb->s_rev_level == EXT2_GOOD_OLD_REV &&
421 (sb->s_feature_compat || sb->s_feature_ro_compat ||
422 sb->s_feature_incompat))
423 ext2fs_update_dynamic_rev(fs);
425 if (FEATURE_CHANGED(E2P_FEATURE_RO_INCOMPAT,
426 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER) ||
427 FEATURE_CHANGED(E2P_FEATURE_RO_INCOMPAT,
428 EXT4_FEATURE_RO_COMPAT_GDT_CSUM) ||
429 FEATURE_CHANGED(E2P_FEATURE_INCOMPAT,
430 EXT2_FEATURE_INCOMPAT_FILETYPE) ||
431 FEATURE_CHANGED(E2P_FEATURE_COMPAT,
432 EXT2_FEATURE_COMPAT_RESIZE_INODE) ||
433 FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
434 EXT2_FEATURE_RO_COMPAT_LARGE_FILE)) {
435 sb->s_state &= ~EXT2_VALID_FS;
436 printf("\n%s\n", _(please_fsck));
439 if ((old_features[E2P_FEATURE_COMPAT] != sb->s_feature_compat) ||
440 (old_features[E2P_FEATURE_INCOMPAT] != sb->s_feature_incompat) ||
441 (old_features[E2P_FEATURE_RO_INCOMPAT] != sb->s_feature_ro_compat))
442 ext2fs_mark_super_dirty(fs);
446 * Add a journal to the filesystem.
448 static void add_journal(ext2_filsys fs)
450 unsigned long journal_blocks;
455 if (fs->super->s_feature_compat &
456 EXT3_FEATURE_COMPAT_HAS_JOURNAL) {
457 fputs(_("The filesystem already has a journal.\n"), stderr);
460 if (journal_device) {
461 check_plausibility(journal_device);
462 check_mount(journal_device, 0, _("journal"));
463 #ifdef CONFIG_TESTIO_DEBUG
464 io_ptr = test_io_manager;
465 test_io_backing_manager = unix_io_manager;
467 io_ptr = unix_io_manager;
469 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
470 EXT2_FLAG_JOURNAL_DEV_OK, 0,
471 fs->blocksize, io_ptr, &jfs);
473 com_err(program_name, retval,
474 _("\n\twhile trying to open journal on %s\n"),
478 printf(_("Creating journal on device %s: "),
482 retval = ext2fs_add_journal_device(fs, jfs);
485 com_err (program_name, retval,
486 _("while adding filesystem to journal on %s"),
490 fputs(_("done\n"), stdout);
491 } else if (journal_size) {
492 fputs(_("Creating journal inode: "), stdout);
494 journal_blocks = figure_journal_size(journal_size, fs);
496 retval = ext2fs_add_journal_inode(fs, journal_blocks,
499 fprintf(stderr, "\n");
500 com_err(program_name, retval,
501 _("\n\twhile trying to create journal file"));
504 fputs(_("done\n"), stdout);
506 * If the filesystem wasn't mounted, we need to force
507 * the block group descriptors out.
509 if ((mount_flags & EXT2_MF_MOUNTED) == 0)
510 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
512 print_check_message(fs);
517 free(journal_device);
522 static void parse_e2label_options(int argc, char ** argv)
524 if ((argc < 2) || (argc > 3)) {
525 fputs(_("Usage: e2label device [newlabel]\n"), stderr);
528 io_options = strchr(argv[1], '?');
531 device_name = blkid_get_devname(NULL, argv[1], NULL);
533 com_err("e2label", 0, _("Unable to resolve '%s'"),
537 open_flag = EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_JOURNAL_DEV_OK;
539 open_flag |= EXT2_FLAG_RW;
546 static time_t parse_time(char *str)
550 if (strcmp(str, "now") == 0) {
553 memset(&ts, 0, sizeof(ts));
555 strptime(str, "%Y%m%d%H%M%S", &ts);
557 sscanf(str, "%4d%2d%2d%2d%2d%2d", &ts.tm_year, &ts.tm_mon,
558 &ts.tm_mday, &ts.tm_hour, &ts.tm_min, &ts.tm_sec);
561 if (ts.tm_year < 0 || ts.tm_mon < 0 || ts.tm_mon > 11 ||
562 ts.tm_mday < 0 || ts.tm_mday > 31 || ts.tm_hour > 23 ||
563 ts.tm_min > 59 || ts.tm_sec > 61)
566 if (ts.tm_mday == 0) {
567 com_err(program_name, 0,
568 _("Couldn't parse date/time specifier: %s"),
573 return (mktime(&ts));
576 static void parse_tune2fs_options(int argc, char **argv)
583 open_flag = EXT2_FLAG_SOFTSUPP_FEATURES;
585 printf("tune2fs %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
586 while ((c = getopt(argc, argv, "c:e:fg:i:jlm:o:r:s:u:C:E:I:J:L:M:O:T:U:")) != EOF)
590 max_mount_count = strtol (optarg, &tmp, 0);
591 if (*tmp || max_mount_count > 16000) {
592 com_err (program_name, 0,
593 _("bad mounts count - %s"),
597 if (max_mount_count == 0)
598 max_mount_count = -1;
600 open_flag = EXT2_FLAG_RW;
603 mount_count = strtoul (optarg, &tmp, 0);
604 if (*tmp || mount_count > 16000) {
605 com_err (program_name, 0,
606 _("bad mounts count - %s"),
611 open_flag = EXT2_FLAG_RW;
614 if (strcmp (optarg, "continue") == 0)
615 errors = EXT2_ERRORS_CONTINUE;
616 else if (strcmp (optarg, "remount-ro") == 0)
617 errors = EXT2_ERRORS_RO;
618 else if (strcmp (optarg, "panic") == 0)
619 errors = EXT2_ERRORS_PANIC;
621 com_err (program_name, 0,
622 _("bad error behavior - %s"),
627 open_flag = EXT2_FLAG_RW;
630 extended_cmd = optarg;
631 open_flag |= EXT2_FLAG_RW;
633 case 'f': /* Force */
637 resgid = strtoul (optarg, &tmp, 0);
639 gr = getgrnam (optarg);
648 com_err (program_name, 0,
649 _("bad gid/group name - %s"),
654 open_flag = EXT2_FLAG_RW;
657 interval = strtoul (optarg, &tmp, 0);
670 case 'M': /* months! */
671 interval *= 86400 * 30;
675 case 'W': /* weeks */
676 interval *= 86400 * 7;
681 com_err (program_name, 0,
682 _("bad interval - %s"), optarg);
686 open_flag = EXT2_FLAG_RW;
691 open_flag = EXT2_FLAG_RW;
694 parse_journal_opts(optarg);
695 open_flag = EXT2_FLAG_RW;
703 open_flag |= EXT2_FLAG_RW |
704 EXT2_FLAG_JOURNAL_DEV_OK;
707 reserved_ratio = strtod(optarg, &tmp);
708 if (*tmp || reserved_ratio > 50) {
709 com_err (program_name, 0,
710 _("bad reserved block ratio - %s"),
715 open_flag = EXT2_FLAG_RW;
718 new_last_mounted = optarg;
720 open_flag = EXT2_FLAG_RW;
724 com_err (program_name, 0,
725 _("-o may only be specified once"));
728 mntopts_cmd = optarg;
729 open_flag = EXT2_FLAG_RW;
734 com_err (program_name, 0,
735 _("-O may only be specified once"));
738 features_cmd = optarg;
739 open_flag = EXT2_FLAG_RW;
742 reserved_blocks = strtoul (optarg, &tmp, 0);
744 com_err (program_name, 0,
745 _("bad reserved blocks count - %s"),
750 open_flag = EXT2_FLAG_RW;
752 case 's': /* Deprecated */
753 s_flag = atoi(optarg);
754 open_flag = EXT2_FLAG_RW;
758 last_check_time = parse_time(optarg);
759 open_flag = EXT2_FLAG_RW;
762 resuid = strtoul (optarg, &tmp, 0);
764 pw = getpwnam (optarg);
773 com_err (program_name, 0,
774 _("bad uid/user name - %s"),
779 open_flag = EXT2_FLAG_RW;
784 open_flag = EXT2_FLAG_RW |
785 EXT2_FLAG_JOURNAL_DEV_OK;
788 new_inode_size = strtoul (optarg, &tmp, 0);
790 com_err (program_name, 0,
791 _("bad inode size - %s"),
795 if (!((new_inode_size &
796 (new_inode_size - 1)) == 0)) {
797 com_err (program_name, 0,
798 _("Inode size must be a "
803 open_flag = EXT2_FLAG_RW;
809 if (optind < argc - 1 || optind == argc)
811 if (!open_flag && !l_flag)
813 io_options = strchr(argv[optind], '?');
816 device_name = blkid_get_devname(NULL, argv[optind], NULL);
818 com_err("tune2fs", 0, _("Unable to resolve '%s'"),
824 void do_findfs(int argc, char **argv)
829 (strncmp(argv[1], "LABEL=", 6) && strncmp(argv[1], "UUID=", 5))) {
830 fprintf(stderr, "Usage: findfs LABEL=<label>|UUID=<uuid>\n");
833 dev = blkid_get_devname(NULL, argv[1], NULL);
835 com_err("findfs", 0, _("Unable to resolve '%s'"),
844 * Note! If any extended options are incompatible with the
845 * intersection of the SOFTSUPP features and those features explicitly
846 * enabled for tune2fs, there needs to be an explicit test for them
849 static void parse_extended_opts(ext2_filsys fs, const char *opts)
851 char *buf, *token, *next, *p, *arg;
859 _("Couldn't allocate memory to parse options!\n"));
863 for (token = buf; token && *token; token = next) {
864 p = strchr(token, ',');
870 arg = strchr(token, '=');
875 if (!strcmp(token, "test_fs")) {
876 fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
877 printf("Setting test filesystem flag\n");
878 ext2fs_mark_super_dirty(fs);
879 } else if (!strcmp(token, "^test_fs")) {
880 fs->super->s_flags &= ~EXT2_FLAGS_TEST_FILESYS;
881 printf("Clearing test filesystem flag\n");
882 ext2fs_mark_super_dirty(fs);
883 } else if (strcmp(token, "stride") == 0) {
888 stride = strtoul(arg, &p, 0);
889 if (*p || (stride == 0)) {
891 _("Invalid RAID stride: %s\n"),
897 } else if (strcmp(token, "stripe-width") == 0 ||
898 strcmp(token, "stripe_width") == 0) {
903 stripe_width = strtoul(arg, &p, 0);
904 if (*p || (stripe_width == 0)) {
906 _("Invalid RAID stripe-width: %s\n"),
911 stripe_width_set = 1;
916 fprintf(stderr, _("\nBad options specified.\n\n"
917 "Extended options are separated by commas, "
918 "and may take an argument which\n"
919 "\tis set off by an equals ('=') sign.\n\n"
920 "Valid extended options are:\n"
921 "\tstride=<RAID per-disk chunk size in blocks>\n"
922 "\tstripe-width=<RAID stride*data disks in blocks>\n"
931 static int get_move_bitmap(ext2_filsys fs, int new_ino_blks_per_grp,
932 ext2fs_block_bitmap bmap)
935 blk_t j, needed_blocks = 0;
936 blk_t start_blk, end_blk;
938 for (i = 0; i < fs->group_desc_count; i++) {
940 start_blk = fs->group_desc[i].bg_inode_table +
941 fs->inode_blocks_per_group;
943 end_blk = fs->group_desc[i].bg_inode_table +
944 new_ino_blks_per_grp;
946 for (j = start_blk; j < end_blk; j++) {
948 if (ext2fs_test_block_bitmap(fs->block_map, j)) {
950 * What happens if the block is marked
953 ext2fs_mark_block_bitmap(bmap, j);
957 * We are going to use this block for
958 * inode table. So mark them used.
960 ext2fs_mark_block_bitmap(fs->block_map, j);
965 if (needed_blocks > fs->super->s_free_blocks_count ) {
972 static int move_block(ext2_filsys fs, ext2fs_block_bitmap bmap)
977 struct blk_move *bmv;
980 retval = ext2fs_get_mem(fs->blocksize, &buf);
984 for (blk = fs->super->s_first_data_block;
985 blk < fs->super->s_blocks_count; blk++) {
987 if (!ext2fs_test_block_bitmap(bmap, blk))
990 retval = ext2fs_new_block(fs, blk, NULL, &new_blk);
994 /* Mark this block as allocated */
995 ext2fs_mark_block_bitmap(fs->block_map, new_blk);
997 /* Add it to block move list */
998 retval = ext2fs_get_mem(sizeof(struct blk_move), &bmv);
1003 bmv->new_loc = new_blk;
1005 list_add(&(bmv->list), &blk_move_list);
1007 retval = io_channel_read_blk(fs->io, blk, 1, buf);
1011 retval = io_channel_write_blk(fs->io, new_blk, 1, buf);
1017 ext2fs_free_mem(&buf);
1021 static blk_t transalate_block(blk_t blk)
1023 struct list_head *entry;
1024 struct blk_move *bmv;
1026 list_for_each(entry, &blk_move_list) {
1028 bmv = list_entry(entry, struct blk_move, list);
1029 if (bmv->old_loc == blk)
1030 return bmv->new_loc;
1036 static int process_block(ext2_filsys fs EXT2FS_ATTR((unused)),
1038 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1039 blk_t ref_block EXT2FS_ATTR((unused)),
1040 int ref_offset EXT2FS_ATTR((unused)),
1041 void *priv_data EXT2FS_ATTR((unused)))
1047 new_blk = transalate_block(*block_nr);
1049 *block_nr = new_blk;
1051 * This will force the ext2fs_write_inode in the iterator
1053 ret |= BLOCK_CHANGED;
1059 static int inode_scan_and_fix(ext2_filsys fs)
1061 errcode_t retval = 0;
1064 char *block_buf = 0;
1065 struct ext2_inode inode;
1066 ext2_inode_scan scan = NULL;
1068 retval = ext2fs_get_mem(fs->blocksize * 3, &block_buf);
1072 retval = ext2fs_open_inode_scan(fs, 0, &scan);
1078 retval = ext2fs_get_next_inode(scan, &ino, &inode);
1085 if (inode.i_links_count == 0)
1086 continue; /* inode not in use */
1089 * If we end up modifying the journal inode
1090 * the sb->s_jnl_blocks will differ. But a
1091 * subsequent e2fsck fixes that.
1092 * Do we need to fix this ??
1095 if (inode.i_file_acl) {
1097 blk = transalate_block(inode.i_file_acl);
1101 inode.i_file_acl = blk;
1104 * Write the inode to disk so that inode table
1107 retval = ext2fs_write_inode(fs, ino, &inode);
1112 if (!ext2fs_inode_has_valid_blocks(&inode))
1115 retval = ext2fs_block_iterate2(fs, ino, 0,
1116 block_buf, process_block,
1124 ext2fs_free_mem(&block_buf);
1130 static int expand_inode_table(ext2_filsys fs, unsigned long new_ino_size)
1135 int new_ino_blks_per_grp;
1137 char *old_itable = NULL, *new_itable = NULL;
1138 char *tmp_old_itable = NULL, *tmp_new_itable = NULL;
1139 unsigned long old_ino_size;
1140 int old_itable_size, new_itable_size;
1142 old_itable_size = fs->inode_blocks_per_group * fs->blocksize;
1143 old_ino_size = EXT2_INODE_SIZE(fs->super);
1145 new_ino_blks_per_grp = ext2fs_div_ceil(
1146 EXT2_INODES_PER_GROUP(fs->super) *
1150 new_itable_size = new_ino_blks_per_grp * fs->blocksize;
1152 retval = ext2fs_get_mem(old_itable_size, &old_itable);
1156 retval = ext2fs_get_mem(new_itable_size, &new_itable);
1160 tmp_old_itable = old_itable;
1161 tmp_new_itable = new_itable;
1163 for (i = 0; i < fs->group_desc_count; i++) {
1165 blk = fs->group_desc[i].bg_inode_table;
1166 retval = io_channel_read_blk(fs->io, blk,
1167 fs->inode_blocks_per_group, old_itable);
1171 for (j = 0; j < EXT2_INODES_PER_GROUP(fs->super); j++) {
1173 memcpy(new_itable, old_itable, old_ino_size);
1175 memset(new_itable+old_ino_size, 0,
1176 new_ino_size - old_ino_size);
1178 new_itable += new_ino_size;
1179 old_itable += old_ino_size;
1182 /* reset the pointer */
1183 old_itable = tmp_old_itable;
1184 new_itable = tmp_new_itable;
1186 retval = io_channel_write_blk(fs->io, blk,
1187 new_ino_blks_per_grp, new_itable);
1192 /* Update the meta data */
1193 fs->inode_blocks_per_group = new_ino_blks_per_grp;
1194 fs->super->s_inode_size = new_ino_size;
1198 ext2fs_free_mem(&old_itable);
1201 ext2fs_free_mem(&new_itable);
1206 static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs)
1210 unsigned int group = 0;
1211 unsigned int count = 0;
1216 * First calculate the block statistics
1218 for (blk = fs->super->s_first_data_block;
1219 blk < fs->super->s_blocks_count; blk++) {
1220 if (!ext2fs_fast_test_block_bitmap(fs->block_map, blk)) {
1225 if ((count == fs->super->s_blocks_per_group) ||
1226 (blk == fs->super->s_blocks_count-1)) {
1227 fs->group_desc[group++].bg_free_blocks_count =
1233 fs->super->s_free_blocks_count = total_free;
1236 * Next, calculate the inode statistics
1243 /* Protect loop from wrap-around if s_inodes_count maxed */
1244 for (ino = 1; ino <= fs->super->s_inodes_count && ino > 0; ino++) {
1245 if (!ext2fs_fast_test_inode_bitmap(fs->inode_map, ino)) {
1250 if ((count == fs->super->s_inodes_per_group) ||
1251 (ino == fs->super->s_inodes_count)) {
1252 fs->group_desc[group++].bg_free_inodes_count =
1258 fs->super->s_free_inodes_count = total_free;
1259 ext2fs_mark_super_dirty(fs);
1263 #define list_for_each_safe(pos, pnext, head) \
1264 for (pos = (head)->next, pnext = pos->next; pos != (head); \
1265 pos = pnext, pnext = pos->next)
1267 static void free_blk_move_list(void)
1269 struct list_head *entry, *tmp;
1270 struct blk_move *bmv;
1272 list_for_each_safe(entry, tmp, &blk_move_list) {
1274 bmv = list_entry(entry, struct blk_move, list);
1276 ext2fs_free_mem(&bmv);
1282 static int resize_inode(ext2_filsys fs, unsigned long new_size)
1285 int new_ino_blks_per_grp;
1286 ext2fs_block_bitmap bmap;
1288 if (new_size <= EXT2_INODE_SIZE(fs->super)) {
1289 fprintf(stderr, _("New inode size too small\n"));
1290 return EXT2_ET_INVALID_ARGUMENT;
1293 ext2fs_read_inode_bitmap(fs);
1294 ext2fs_read_block_bitmap(fs);
1295 INIT_LIST_HEAD(&blk_move_list);
1298 new_ino_blks_per_grp = ext2fs_div_ceil(
1299 EXT2_INODES_PER_GROUP(fs->super)*
1303 /* We may change the file system.
1304 * Mark the file system as invalid so that
1305 * the user is prompted to run fsck.
1307 fs->super->s_state &= ~EXT2_VALID_FS;
1309 retval = ext2fs_allocate_block_bitmap(fs, _("blocks to be moved"),
1314 retval = get_move_bitmap(fs, new_ino_blks_per_grp, bmap);
1318 retval = move_block(fs, bmap);
1322 retval = inode_scan_and_fix(fs);
1326 retval = expand_inode_table(fs, new_size);
1330 ext2fs_calculate_summary_stats(fs);
1332 fs->super->s_state |= EXT2_VALID_FS;
1333 /* mark super block and block bitmap as dirty */
1334 ext2fs_mark_super_dirty(fs);
1335 ext2fs_mark_bb_dirty(fs);
1338 free_blk_move_list();
1339 ext2fs_free_block_bitmap(bmap);
1344 static int tune2fs_setup_tdb(const char *name, io_manager *io_ptr)
1346 errcode_t retval = 0;
1347 const char *tdb_dir;
1348 char tdb_file[PATH_MAX];
1349 char *dev_name, *tmp_name;
1353 * Configuration via a conf file would be
1356 profile_get_string(profile, "scratch_files",
1360 tmp_name = strdup(name);
1361 dev_name = basename(tmp_name);
1363 tdb_dir = getenv("E2FSPROGS_UNDO_DIR");
1365 tdb_dir="/var/lib/e2fsprogs";
1367 if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
1368 access(tdb_dir, W_OK))
1371 sprintf(tdb_file, "%s/tune2fs-%s.e2undo", tdb_dir, dev_name);
1373 if (!access(tdb_file, F_OK)) {
1374 if (unlink(tdb_file) < 0) {
1376 com_err(program_name, retval,
1377 _("while trying to delete %s"),
1383 set_undo_io_backing_manager(*io_ptr);
1384 *io_ptr = undo_io_manager;
1385 set_undo_io_backup_file(tdb_file);
1386 printf(_("To undo the tune2fs operations please run "
1387 "the command\n undoe2fs %s %s\n\n"),
1393 int main (int argc, char ** argv)
1397 struct ext2_super_block *sb;
1401 setlocale(LC_MESSAGES, "");
1402 setlocale(LC_CTYPE, "");
1403 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1404 textdomain(NLS_CAT_NAME);
1407 program_name = *argv;
1408 add_error_table(&et_ext2_error_table);
1410 if (strcmp(get_progname(argv[0]), "findfs") == 0)
1411 do_findfs(argc, argv);
1412 if (strcmp(get_progname(argv[0]), "e2label") == 0)
1413 parse_e2label_options(argc, argv);
1415 parse_tune2fs_options(argc, argv);
1417 #ifdef CONFIG_TESTIO_DEBUG
1418 io_ptr = test_io_manager;
1419 test_io_backing_manager = unix_io_manager;
1421 io_ptr = unix_io_manager;
1426 * If inode resize is requested use the
1429 retval = tune2fs_setup_tdb(device_name, &io_ptr);
1434 retval = ext2fs_open2(device_name, io_options, open_flag,
1437 com_err (program_name, retval, _("while trying to open %s"),
1440 _("Couldn't find valid filesystem superblock.\n"));
1444 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
1445 if ((sb->s_feature_incompat & !EXT2_TUNE2FS_INCOMPAT) ||
1446 (sb->s_feature_ro_compat & !EXT2_TUNE2FS_RO_COMPAT)) {
1448 _("Filesystem %s has unsupported features enabled.\n"),
1453 /* For e2label emulation */
1454 printf("%.*s\n", (int) sizeof(sb->s_volume_name),
1456 remove_error_table(&et_ext2_error_table);
1459 retval = ext2fs_check_if_mounted(device_name, &mount_flags);
1461 com_err("ext2fs_check_if_mount", retval,
1462 _("while determining whether %s is mounted."),
1466 /* Normally we only need to write out the superblock */
1467 fs->flags |= EXT2_FLAG_SUPER_ONLY;
1470 sb->s_max_mnt_count = max_mount_count;
1471 ext2fs_mark_super_dirty(fs);
1472 printf (_("Setting maximal mount count to %d\n"),
1476 sb->s_mnt_count = mount_count;
1477 ext2fs_mark_super_dirty(fs);
1478 printf (_("Setting current mount count to %d\n"), mount_count);
1481 sb->s_errors = errors;
1482 ext2fs_mark_super_dirty(fs);
1483 printf (_("Setting error behavior to %d\n"), errors);
1486 sb->s_def_resgid = resgid;
1487 ext2fs_mark_super_dirty(fs);
1488 printf (_("Setting reserved blocks gid to %lu\n"), resgid);
1491 sb->s_checkinterval = interval;
1492 ext2fs_mark_super_dirty(fs);
1493 printf (_("Setting interval between checks to %lu seconds\n"), interval);
1496 sb->s_r_blocks_count = e2p_percent(reserved_ratio,
1497 sb->s_blocks_count);
1498 ext2fs_mark_super_dirty(fs);
1499 printf (_("Setting reserved blocks percentage to %g%% (%u blocks)\n"),
1500 reserved_ratio, sb->s_r_blocks_count);
1503 if (reserved_blocks >= sb->s_blocks_count/2) {
1504 com_err (program_name, 0,
1505 _("reserved blocks count is too big (%lu)"),
1509 sb->s_r_blocks_count = reserved_blocks;
1510 ext2fs_mark_super_dirty(fs);
1511 printf (_("Setting reserved blocks count to %lu\n"),
1515 if (sb->s_feature_ro_compat &
1516 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)
1517 fputs(_("\nThe filesystem already has sparse "
1518 "superblocks.\n"), stderr);
1520 sb->s_feature_ro_compat |=
1521 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1522 sb->s_state &= ~EXT2_VALID_FS;
1523 ext2fs_mark_super_dirty(fs);
1524 printf(_("\nSparse superblock flag set. %s"),
1529 fputs(_("\nClearing the sparse superflag not supported.\n"),
1534 sb->s_lastcheck = last_check_time;
1535 ext2fs_mark_super_dirty(fs);
1536 printf(_("Setting time filesystem last checked to %s\n"),
1537 ctime(&last_check_time));
1540 sb->s_def_resuid = resuid;
1541 ext2fs_mark_super_dirty(fs);
1542 printf (_("Setting reserved blocks uid to %lu\n"), resuid);
1545 if (strlen(new_label) > sizeof(sb->s_volume_name))
1546 fputs(_("Warning: label too long, truncating.\n"),
1548 memset(sb->s_volume_name, 0, sizeof(sb->s_volume_name));
1549 strncpy(sb->s_volume_name, new_label,
1550 sizeof(sb->s_volume_name));
1551 ext2fs_mark_super_dirty(fs);
1554 memset(sb->s_last_mounted, 0, sizeof(sb->s_last_mounted));
1555 strncpy(sb->s_last_mounted, new_last_mounted,
1556 sizeof(sb->s_last_mounted));
1557 ext2fs_mark_super_dirty(fs);
1560 update_mntopts(fs, mntopts_cmd);
1562 update_feature_set(fs, features_cmd);
1564 parse_extended_opts(fs, extended_cmd);
1565 if (journal_size || journal_device)
1569 if ((strcasecmp(new_UUID, "null") == 0) ||
1570 (strcasecmp(new_UUID, "clear") == 0)) {
1571 uuid_clear(sb->s_uuid);
1572 } else if (strcasecmp(new_UUID, "time") == 0) {
1573 uuid_generate_time(sb->s_uuid);
1574 } else if (strcasecmp(new_UUID, "random") == 0) {
1575 uuid_generate(sb->s_uuid);
1576 } else if (uuid_parse(new_UUID, sb->s_uuid)) {
1577 com_err(program_name, 0, _("Invalid UUID format\n"));
1580 ext2fs_mark_super_dirty(fs);
1583 if (mount_flags & EXT2_MF_MOUNTED) {
1584 fputs(_("The inode size may only be "
1585 "changed when the filesystem is "
1586 "unmounted.\n"), stderr);
1590 * We want to update group descriptor also
1591 * with the new free inode count
1593 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
1594 if (resize_inode(fs, new_inode_size)) {
1595 fputs(_("Error in resizing the inode size.\n"
1596 "Run undoe2fs to undo the "
1597 "file system changes. \n"), stderr);
1599 printf (_("Setting inode size %lu\n"),
1607 sb->s_raid_stride = stride;
1608 ext2fs_mark_super_dirty(fs);
1609 printf(_("Setting stride size to %d\n"), stride);
1611 if (stripe_width_set) {
1612 sb->s_raid_stripe_width = stripe_width;
1613 ext2fs_mark_super_dirty(fs);
1614 printf(_("Setting stripe width to %d\n"), stripe_width);
1616 remove_error_table(&et_ext2_error_table);
1617 return (ext2fs_close (fs) ? 1 : 0);