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() */
44 #include <strings.h> /* for strcasecmp() */
46 #define _BSD_SOURCE /* for inclusion of strcasecmp() via <string.h> */
51 #include <sys/types.h>
55 #include "ext2fs/ext2_fs.h"
56 #include "ext2fs/ext2fs.h"
57 #include "et/com_err.h"
58 #include "uuid/uuid.h"
62 #include "blkid/blkid.h"
63 #include "quota/mkquota.h"
65 #include "../version.h"
66 #include "nls-enable.h"
68 #define QOPT_ENABLE (1)
69 #define QOPT_DISABLE (-1)
71 extern int ask_yn(const char *string
, int def
);
73 const char *program_name
= "tune2fs";
75 char *new_label
, *new_last_mounted
, *new_UUID
;
77 static int c_flag
, C_flag
, e_flag
, f_flag
, g_flag
, i_flag
, l_flag
, L_flag
;
78 static int m_flag
, M_flag
, Q_flag
, r_flag
, s_flag
= -1, u_flag
, U_flag
, T_flag
;
81 static time_t last_check_time
;
82 static int print_label
;
83 static int max_mount_count
, mount_count
, mount_flags
;
84 static unsigned long interval
;
85 static blk64_t reserved_blocks
;
86 static double reserved_ratio
;
87 static unsigned long resgid
, resuid
;
88 static unsigned short errors
;
90 static char *features_cmd
;
91 static char *mntopts_cmd
;
92 static int stride
, stripe_width
;
93 static int stride_set
, stripe_width_set
;
94 static char *extended_cmd
;
95 static unsigned long new_inode_size
;
96 static char *ext_mount_opts
;
97 static int usrquota
, grpquota
;
99 int journal_size
, journal_flags
;
100 char *journal_device
;
101 static blk64_t journal_location
= ~0LL;
103 static struct list_head blk_move_list
;
106 struct list_head list
;
112 static const char *please_fsck
= N_("Please run e2fsck on the filesystem.\n");
114 #ifdef CONFIG_BUILD_FINDFS
115 void do_findfs(int argc
, char **argv
);
118 static void usage(void)
121 _("Usage: %s [-c max_mounts_count] [-e errors_behavior] "
123 "\t[-i interval[d|m|w]] [-j] [-J journal_options] [-l]\n"
124 "\t[-m reserved_blocks_percent] "
125 "[-o [^]mount_options[,...]] [-p mmp_update_interval]\n"
126 "\t[-r reserved_blocks_count] [-u user] [-C mount_count] "
127 "[-L volume_label]\n"
128 "\t[-M last_mounted_dir] [-O [^]feature[,...]]\n"
130 "\t[-Q quota_options]\n"
132 "\t[-E extended-option[,...]] [-T last_check_time] "
133 "[-U UUID]\n\t[ -I new_inode_size ] device\n"), program_name
);
137 static __u32 ok_features
[3] = {
139 EXT3_FEATURE_COMPAT_HAS_JOURNAL
|
140 EXT2_FEATURE_COMPAT_DIR_INDEX
,
142 EXT2_FEATURE_INCOMPAT_FILETYPE
|
143 EXT3_FEATURE_INCOMPAT_EXTENTS
|
144 EXT4_FEATURE_INCOMPAT_FLEX_BG
|
145 EXT4_FEATURE_INCOMPAT_MMP
,
147 EXT2_FEATURE_RO_COMPAT_LARGE_FILE
|
148 EXT4_FEATURE_RO_COMPAT_HUGE_FILE
|
149 EXT4_FEATURE_RO_COMPAT_DIR_NLINK
|
150 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE
|
151 EXT4_FEATURE_RO_COMPAT_GDT_CSUM
|
153 EXT4_FEATURE_RO_COMPAT_QUOTA
|
155 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
158 static __u32 clear_ok_features
[3] = {
160 EXT3_FEATURE_COMPAT_HAS_JOURNAL
|
161 EXT2_FEATURE_COMPAT_RESIZE_INODE
|
162 EXT2_FEATURE_COMPAT_DIR_INDEX
,
164 EXT2_FEATURE_INCOMPAT_FILETYPE
|
165 EXT4_FEATURE_INCOMPAT_FLEX_BG
|
166 EXT4_FEATURE_INCOMPAT_MMP
,
168 EXT2_FEATURE_RO_COMPAT_LARGE_FILE
|
169 EXT4_FEATURE_RO_COMPAT_HUGE_FILE
|
170 EXT4_FEATURE_RO_COMPAT_DIR_NLINK
|
171 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE
|
173 EXT4_FEATURE_RO_COMPAT_QUOTA
|
175 EXT4_FEATURE_RO_COMPAT_GDT_CSUM
179 * Remove an external journal from the filesystem
181 static int remove_journal_device(ext2_filsys fs
)
186 journal_superblock_t
*jsb
;
189 int commit_remove_journal
= 0;
193 commit_remove_journal
= 1; /* force removal even if error */
195 uuid_unparse(fs
->super
->s_journal_uuid
, buf
);
196 journal_path
= blkid_get_devname(NULL
, "UUID", buf
);
200 ext2fs_find_block_device(fs
->super
->s_journal_dev
);
202 goto no_valid_journal
;
205 #ifdef CONFIG_TESTIO_DEBUG
206 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
207 io_ptr
= test_io_manager
;
208 test_io_backing_manager
= unix_io_manager
;
211 io_ptr
= unix_io_manager
;
212 retval
= ext2fs_open(journal_path
, EXT2_FLAG_RW
|
213 EXT2_FLAG_JOURNAL_DEV_OK
, 0,
214 fs
->blocksize
, io_ptr
, &jfs
);
216 com_err(program_name
, retval
, "%s",
217 _("while trying to open external journal"));
218 goto no_valid_journal
;
220 if (!(jfs
->super
->s_feature_incompat
&
221 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV
)) {
222 fprintf(stderr
, _("%s is not a journal device.\n"),
224 goto no_valid_journal
;
227 /* Get the journal superblock */
228 if ((retval
= io_channel_read_blk64(jfs
->io
, 1, -1024, buf
))) {
229 com_err(program_name
, retval
, "%s",
230 _("while reading journal superblock"));
231 goto no_valid_journal
;
234 jsb
= (journal_superblock_t
*) buf
;
235 if ((jsb
->s_header
.h_magic
!= (unsigned)ntohl(JFS_MAGIC_NUMBER
)) ||
236 (jsb
->s_header
.h_blocktype
!= (unsigned)ntohl(JFS_SUPERBLOCK_V2
))) {
237 fputs(_("Journal superblock not found!\n"), stderr
);
238 goto no_valid_journal
;
241 /* Find the filesystem UUID */
242 nr_users
= ntohl(jsb
->s_nr_users
);
243 for (i
= 0; i
< nr_users
; i
++) {
244 if (memcmp(fs
->super
->s_uuid
, &jsb
->s_users
[i
* 16], 16) == 0)
248 fputs(_("Filesystem's UUID not found on journal device.\n"),
250 commit_remove_journal
= 1;
251 goto no_valid_journal
;
254 for (i
= 0; i
< nr_users
; i
++)
255 memcpy(&jsb
->s_users
[i
* 16], &jsb
->s_users
[(i
+ 1) * 16], 16);
256 jsb
->s_nr_users
= htonl(nr_users
);
258 /* Write back the journal superblock */
259 if ((retval
= io_channel_write_blk64(jfs
->io
, 1, -1024, buf
))) {
260 com_err(program_name
, retval
,
261 "while writing journal superblock.");
262 goto no_valid_journal
;
265 commit_remove_journal
= 1;
268 if (commit_remove_journal
== 0) {
269 fputs(_("Cannot locate journal device. It was NOT removed\n"
270 "Use -f option to remove missing journal device.\n"),
274 fs
->super
->s_journal_dev
= 0;
275 uuid_clear(fs
->super
->s_journal_uuid
);
276 ext2fs_mark_super_dirty(fs
);
277 fputs(_("Journal removed\n"), stdout
);
283 /* Helper function for remove_journal_inode */
284 static int release_blocks_proc(ext2_filsys fs
, blk64_t
*blocknr
,
285 e2_blkcnt_t blockcnt
EXT2FS_ATTR((unused
)),
286 blk64_t ref_block
EXT2FS_ATTR((unused
)),
287 int ref_offset
EXT2FS_ATTR((unused
)),
288 void *private EXT2FS_ATTR((unused
)))
294 ext2fs_unmark_block_bitmap2(fs
->block_map
, block
);
295 group
= ext2fs_group_of_blk2(fs
, block
);
296 ext2fs_bg_free_blocks_count_set(fs
, group
, ext2fs_bg_free_blocks_count(fs
, group
) + 1);
297 ext2fs_group_desc_csum_set(fs
, group
);
298 ext2fs_free_blocks_count_add(fs
->super
, EXT2FS_CLUSTER_RATIO(fs
));
303 * Remove the journal inode from the filesystem
305 static errcode_t
remove_journal_inode(ext2_filsys fs
)
307 struct ext2_inode inode
;
309 ino_t ino
= fs
->super
->s_journal_inum
;
311 retval
= ext2fs_read_inode(fs
, ino
, &inode
);
313 com_err(program_name
, retval
, "%s",
314 _("while reading journal inode"));
317 if (ino
== EXT2_JOURNAL_INO
) {
318 retval
= ext2fs_read_bitmaps(fs
);
320 com_err(program_name
, retval
, "%s",
321 _("while reading bitmaps"));
324 retval
= ext2fs_block_iterate3(fs
, ino
,
325 BLOCK_FLAG_READ_ONLY
, NULL
,
326 release_blocks_proc
, NULL
);
328 com_err(program_name
, retval
, "%s",
329 _("while clearing journal inode"));
332 memset(&inode
, 0, sizeof(inode
));
333 ext2fs_mark_bb_dirty(fs
);
334 fs
->flags
&= ~EXT2_FLAG_SUPER_ONLY
;
336 inode
.i_flags
&= ~EXT2_IMMUTABLE_FL
;
337 retval
= ext2fs_write_inode(fs
, ino
, &inode
);
339 com_err(program_name
, retval
, "%s",
340 _("while writing journal inode"));
343 fs
->super
->s_journal_inum
= 0;
344 ext2fs_mark_super_dirty(fs
);
350 * Update the default mount options
352 static int update_mntopts(ext2_filsys fs
, char *mntopts
)
354 struct ext2_super_block
*sb
= fs
->super
;
356 if (e2p_edit_mntopts(mntopts
, &sb
->s_default_mount_opts
, ~0)) {
357 fprintf(stderr
, _("Invalid mount option set: %s\n"),
361 ext2fs_mark_super_dirty(fs
);
366 static int check_fsck_needed(ext2_filsys fs
)
368 if (fs
->super
->s_state
& EXT2_VALID_FS
)
370 printf("\n%s\n", _(please_fsck
));
371 if (mount_flags
& EXT2_MF_READONLY
)
372 printf(_("(and reboot afterwards!)\n"));
376 static void request_fsck_afterwards(ext2_filsys fs
)
378 static int requested
= 0;
382 fs
->super
->s_state
&= ~EXT2_VALID_FS
;
383 printf("\n%s\n", _(please_fsck
));
384 if (mount_flags
& EXT2_MF_READONLY
)
385 printf("%s", _("(and reboot afterwards!)\n"));
389 * Update the feature set as provided by the user.
391 static int update_feature_set(ext2_filsys fs
, char *features
)
393 struct ext2_super_block
*sb
= fs
->super
;
394 struct ext2_group_desc
*gd
;
395 __u32 old_features
[3];
398 unsigned int mask_err
;
400 #define FEATURE_ON(type, mask) (!(old_features[(type)] & (mask)) && \
401 ((&sb->s_feature_compat)[(type)] & (mask)))
402 #define FEATURE_OFF(type, mask) ((old_features[(type)] & (mask)) && \
403 !((&sb->s_feature_compat)[(type)] & (mask)))
404 #define FEATURE_CHANGED(type, mask) ((mask) & \
405 (old_features[(type)] ^ (&sb->s_feature_compat)[(type)]))
407 old_features
[E2P_FEATURE_COMPAT
] = sb
->s_feature_compat
;
408 old_features
[E2P_FEATURE_INCOMPAT
] = sb
->s_feature_incompat
;
409 old_features
[E2P_FEATURE_RO_INCOMPAT
] = sb
->s_feature_ro_compat
;
411 if (e2p_edit_feature2(features
, &sb
->s_feature_compat
,
412 ok_features
, clear_ok_features
,
413 &type_err
, &mask_err
)) {
416 _("Invalid filesystem option set: %s\n"),
418 else if (type_err
& E2P_FEATURE_NEGATE_FLAG
)
419 fprintf(stderr
, _("Clearing filesystem feature '%s' "
421 e2p_feature2string(type_err
&
422 E2P_FEATURE_TYPE_MASK
,
425 fprintf(stderr
, _("Setting filesystem feature '%s' "
427 e2p_feature2string(type_err
, mask_err
));
431 if (FEATURE_OFF(E2P_FEATURE_COMPAT
, EXT3_FEATURE_COMPAT_HAS_JOURNAL
)) {
432 if ((mount_flags
& EXT2_MF_MOUNTED
) &&
433 !(mount_flags
& EXT2_MF_READONLY
)) {
434 fputs(_("The has_journal feature may only be "
435 "cleared when the filesystem is\n"
436 "unmounted or mounted "
437 "read-only.\n"), stderr
);
440 if (sb
->s_feature_incompat
&
441 EXT3_FEATURE_INCOMPAT_RECOVER
) {
442 fputs(_("The needs_recovery flag is set. "
443 "Please run e2fsck before clearing\n"
444 "the has_journal flag.\n"), stderr
);
447 if (sb
->s_journal_inum
) {
448 if (remove_journal_inode(fs
))
451 if (sb
->s_journal_dev
) {
452 if (remove_journal_device(fs
))
457 if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT
,
458 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
)) {
459 if (sb
->s_feature_incompat
&
460 EXT2_FEATURE_INCOMPAT_META_BG
) {
461 fputs(_("Setting filesystem feature 'sparse_super' "
462 "not supported\nfor filesystems with "
463 "the meta_bg feature enabled.\n"),
469 if (FEATURE_ON(E2P_FEATURE_INCOMPAT
, EXT4_FEATURE_INCOMPAT_MMP
)) {
472 if ((mount_flags
& EXT2_MF_MOUNTED
) ||
473 (mount_flags
& EXT2_MF_READONLY
)) {
474 fputs(_("The multiple mount protection feature can't\n"
475 "be set if the filesystem is mounted or\n"
476 "read-only.\n"), stderr
);
480 error
= ext2fs_mmp_init(fs
);
482 fputs(_("\nError while enabling multiple mount "
483 "protection feature."), stderr
);
488 * We want to update group desc with the new free blocks count
490 fs
->flags
&= ~EXT2_FLAG_SUPER_ONLY
;
492 printf(_("Multiple mount protection has been enabled "
493 "with update interval %ds.\n"),
494 sb
->s_mmp_update_interval
);
497 if (FEATURE_OFF(E2P_FEATURE_INCOMPAT
, EXT4_FEATURE_INCOMPAT_MMP
)) {
500 if (mount_flags
& EXT2_MF_READONLY
) {
501 fputs(_("The multiple mount protection feature cannot\n"
502 "be disabled if the filesystem is readonly.\n"),
507 error
= ext2fs_read_bitmaps(fs
);
509 fputs(_("Error while reading bitmaps\n"), stderr
);
513 error
= ext2fs_mmp_read(fs
, sb
->s_mmp_block
, NULL
);
515 struct mmp_struct
*mmp_cmp
= fs
->mmp_cmp
;
517 if (error
== EXT2_ET_MMP_MAGIC_INVALID
)
518 printf(_("Magic number in MMP block does not "
519 "match. expected: %x, actual: %x\n"),
520 EXT4_MMP_MAGIC
, mmp_cmp
->mmp_magic
);
522 com_err(program_name
, error
, "%s",
523 _("while reading MMP block."));
527 /* We need to force out the group descriptors as well */
528 fs
->flags
&= ~EXT2_FLAG_SUPER_ONLY
;
529 ext2fs_block_alloc_stats2(fs
, sb
->s_mmp_block
, -1);
532 sb
->s_mmp_update_interval
= 0;
535 if (FEATURE_ON(E2P_FEATURE_COMPAT
, EXT3_FEATURE_COMPAT_HAS_JOURNAL
)) {
537 * If adding a journal flag, let the create journal
538 * code below handle setting the flag and creating the
539 * journal. We supply a default size if necessary.
543 sb
->s_feature_compat
&= ~EXT3_FEATURE_COMPAT_HAS_JOURNAL
;
546 if (FEATURE_ON(E2P_FEATURE_COMPAT
, EXT2_FEATURE_COMPAT_DIR_INDEX
)) {
547 if (!sb
->s_def_hash_version
)
548 sb
->s_def_hash_version
= EXT2_HASH_HALF_MD4
;
549 if (uuid_is_null((unsigned char *) sb
->s_hash_seed
))
550 uuid_generate((unsigned char *) sb
->s_hash_seed
);
553 if (FEATURE_OFF(E2P_FEATURE_INCOMPAT
, EXT4_FEATURE_INCOMPAT_FLEX_BG
)) {
554 if (ext2fs_check_desc(fs
)) {
555 fputs(_("Clearing the flex_bg flag would "
556 "cause the the filesystem to be\n"
557 "inconsistent.\n"), stderr
);
562 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT
,
563 EXT4_FEATURE_RO_COMPAT_HUGE_FILE
)) {
564 if ((mount_flags
& EXT2_MF_MOUNTED
) &&
565 !(mount_flags
& EXT2_MF_READONLY
)) {
566 fputs(_("The huge_file feature may only be "
567 "cleared when the filesystem is\n"
568 "unmounted or mounted "
569 "read-only.\n"), stderr
);
574 if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT
,
575 EXT4_FEATURE_RO_COMPAT_GDT_CSUM
)) {
576 for (i
= 0; i
< fs
->group_desc_count
; i
++) {
577 gd
= ext2fs_group_desc(fs
, fs
->group_desc
, i
);
578 gd
->bg_itable_unused
= 0;
579 gd
->bg_flags
= EXT2_BG_INODE_ZEROED
;
580 ext2fs_group_desc_csum_set(fs
, i
);
582 fs
->flags
&= ~EXT2_FLAG_SUPER_ONLY
;
585 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT
,
586 EXT4_FEATURE_RO_COMPAT_GDT_CSUM
)) {
587 for (i
= 0; i
< fs
->group_desc_count
; i
++) {
588 gd
= ext2fs_group_desc(fs
, fs
->group_desc
, i
);
589 if ((gd
->bg_flags
& EXT2_BG_INODE_ZEROED
) == 0) {
591 * XXX what we really should do is zap
592 * uninitialized inode tables instead.
594 request_fsck_afterwards(fs
);
597 gd
->bg_itable_unused
= 0;
601 fs
->flags
&= ~EXT2_FLAG_SUPER_ONLY
;
604 if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT
,
605 EXT4_FEATURE_RO_COMPAT_QUOTA
)) {
607 * Set the Q_flag here and handle the quota options in the code
612 /* Enable both user quota and group quota by default */
613 usrquota
= QOPT_ENABLE
;
614 grpquota
= QOPT_ENABLE
;
616 sb
->s_feature_ro_compat
&= ~EXT4_FEATURE_RO_COMPAT_QUOTA
;
619 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT
,
620 EXT4_FEATURE_RO_COMPAT_QUOTA
)) {
622 * Set the Q_flag here and handle the quota options in the code
626 fputs(_("\nWarning: '^quota' option overrides '-Q'"
627 "arguments.\n"), stderr
);
629 /* Disable both user quota and group quota by default */
630 usrquota
= QOPT_DISABLE
;
631 grpquota
= QOPT_DISABLE
;
634 if (sb
->s_rev_level
== EXT2_GOOD_OLD_REV
&&
635 (sb
->s_feature_compat
|| sb
->s_feature_ro_compat
||
636 sb
->s_feature_incompat
))
637 ext2fs_update_dynamic_rev(fs
);
639 if (FEATURE_CHANGED(E2P_FEATURE_RO_INCOMPAT
,
640 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
) ||
641 FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT
,
642 EXT4_FEATURE_RO_COMPAT_HUGE_FILE
) ||
643 FEATURE_CHANGED(E2P_FEATURE_INCOMPAT
,
644 EXT2_FEATURE_INCOMPAT_FILETYPE
) ||
645 FEATURE_CHANGED(E2P_FEATURE_COMPAT
,
646 EXT2_FEATURE_COMPAT_RESIZE_INODE
) ||
647 FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT
,
648 EXT2_FEATURE_RO_COMPAT_LARGE_FILE
))
649 request_fsck_afterwards(fs
);
651 if ((old_features
[E2P_FEATURE_COMPAT
] != sb
->s_feature_compat
) ||
652 (old_features
[E2P_FEATURE_INCOMPAT
] != sb
->s_feature_incompat
) ||
653 (old_features
[E2P_FEATURE_RO_INCOMPAT
] != sb
->s_feature_ro_compat
))
654 ext2fs_mark_super_dirty(fs
);
660 * Add a journal to the filesystem.
662 static int add_journal(ext2_filsys fs
)
664 unsigned long journal_blocks
;
669 if (fs
->super
->s_feature_compat
&
670 EXT3_FEATURE_COMPAT_HAS_JOURNAL
) {
671 fputs(_("The filesystem already has a journal.\n"), stderr
);
674 if (journal_device
) {
675 check_plausibility(journal_device
);
676 check_mount(journal_device
, 0, _("journal"));
677 #ifdef CONFIG_TESTIO_DEBUG
678 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
679 io_ptr
= test_io_manager
;
680 test_io_backing_manager
= unix_io_manager
;
683 io_ptr
= unix_io_manager
;
684 retval
= ext2fs_open(journal_device
, EXT2_FLAG_RW
|
685 EXT2_FLAG_JOURNAL_DEV_OK
, 0,
686 fs
->blocksize
, io_ptr
, &jfs
);
688 com_err(program_name
, retval
,
689 _("\n\twhile trying to open journal on %s\n"),
693 printf(_("Creating journal on device %s: "),
697 retval
= ext2fs_add_journal_device(fs
, jfs
);
700 com_err(program_name
, retval
,
701 _("while adding filesystem to journal on %s"),
705 fputs(_("done\n"), stdout
);
706 } else if (journal_size
) {
707 fputs(_("Creating journal inode: "), stdout
);
709 journal_blocks
= figure_journal_size(journal_size
, fs
);
711 if (journal_location_string
)
713 parse_num_blocks2(journal_location_string
,
714 fs
->super
->s_log_block_size
);
715 retval
= ext2fs_add_journal_inode2(fs
, journal_blocks
,
719 fprintf(stderr
, "\n");
720 com_err(program_name
, retval
, "%s",
721 _("\n\twhile trying to create journal file"));
724 fputs(_("done\n"), stdout
);
726 * If the filesystem wasn't mounted, we need to force
727 * the block group descriptors out.
729 if ((mount_flags
& EXT2_MF_MOUNTED
) == 0)
730 fs
->flags
&= ~EXT2_FLAG_SUPER_ONLY
;
732 print_check_message(fs
->super
->s_max_mnt_count
,
733 fs
->super
->s_checkinterval
);
737 free(journal_device
);
741 static void handle_quota_options(ext2_filsys fs
)
746 if (!usrquota
&& !grpquota
)
750 quota_init_context(&qctx
, fs
, -1);
752 if (usrquota
== QOPT_ENABLE
|| grpquota
== QOPT_ENABLE
)
753 quota_compute_usage(qctx
);
755 if (usrquota
== QOPT_ENABLE
&& !fs
->super
->s_usr_quota_inum
) {
756 if ((qf_ino
= quota_file_exists(fs
, USRQUOTA
,
758 quota_update_limits(qctx
, qf_ino
, USRQUOTA
);
759 quota_write_inode(qctx
, USRQUOTA
);
760 } else if (usrquota
== QOPT_DISABLE
) {
761 quota_remove_inode(fs
, USRQUOTA
);
764 if (grpquota
== QOPT_ENABLE
&& !fs
->super
->s_grp_quota_inum
) {
765 if ((qf_ino
= quota_file_exists(fs
, GRPQUOTA
,
767 quota_update_limits(qctx
, qf_ino
, GRPQUOTA
);
768 quota_write_inode(qctx
, GRPQUOTA
);
769 } else if (grpquota
== QOPT_DISABLE
) {
770 quota_remove_inode(fs
, GRPQUOTA
);
773 quota_release_context(&qctx
);
775 if ((usrquota
== QOPT_ENABLE
) || (grpquota
== QOPT_ENABLE
)) {
776 fprintf(stderr
, "%s", _("\nWarning: the quota feature is still "
777 "under development\n"
778 "See https://ext4.wiki.kernel.org/"
779 "index.php/Quota for more information\n\n"));
780 fs
->super
->s_feature_ro_compat
|= EXT4_FEATURE_RO_COMPAT_QUOTA
;
781 ext2fs_mark_super_dirty(fs
);
782 } else if (!fs
->super
->s_usr_quota_inum
&&
783 !fs
->super
->s_grp_quota_inum
) {
784 fs
->super
->s_feature_ro_compat
&= ~EXT4_FEATURE_RO_COMPAT_QUOTA
;
785 ext2fs_mark_super_dirty(fs
);
792 static void parse_quota_opts(const char *opts
)
794 char *buf
, *token
, *next
, *p
;
800 fputs(_("Couldn't allocate memory to parse quota "
801 "options!\n"), stderr
);
805 for (token
= buf
; token
&& *token
; token
= next
) {
806 p
= strchr(token
, ',');
813 if (strcmp(token
, "usrquota") == 0) {
814 usrquota
= QOPT_ENABLE
;
815 } else if (strcmp(token
, "^usrquota") == 0) {
816 usrquota
= QOPT_DISABLE
;
817 } else if (strcmp(token
, "grpquota") == 0) {
818 grpquota
= QOPT_ENABLE
;
819 } else if (strcmp(token
, "^grpquota") == 0) {
820 grpquota
= QOPT_DISABLE
;
822 fputs(_("\nBad quota options specified.\n\n"
823 "Following valid quota options are available "
824 "(pass by separating with comma):\n"
836 static void parse_e2label_options(int argc
, char ** argv
)
838 if ((argc
< 2) || (argc
> 3)) {
839 fputs(_("Usage: e2label device [newlabel]\n"), stderr
);
842 io_options
= strchr(argv
[1], '?');
845 device_name
= blkid_get_devname(NULL
, argv
[1], NULL
);
847 com_err("e2label", 0, _("Unable to resolve '%s'"),
851 open_flag
= EXT2_FLAG_JOURNAL_DEV_OK
;
853 open_flag
|= EXT2_FLAG_RW
;
860 static time_t parse_time(char *str
)
864 if (strcmp(str
, "now") == 0) {
867 memset(&ts
, 0, sizeof(ts
));
869 strptime(str
, "%Y%m%d%H%M%S", &ts
);
871 sscanf(str
, "%4d%2d%2d%2d%2d%2d", &ts
.tm_year
, &ts
.tm_mon
,
872 &ts
.tm_mday
, &ts
.tm_hour
, &ts
.tm_min
, &ts
.tm_sec
);
875 if (ts
.tm_year
< 0 || ts
.tm_mon
< 0 || ts
.tm_mon
> 11 ||
876 ts
.tm_mday
< 0 || ts
.tm_mday
> 31 || ts
.tm_hour
> 23 ||
877 ts
.tm_min
> 59 || ts
.tm_sec
> 61)
880 if (ts
.tm_mday
== 0) {
881 com_err(program_name
, 0,
882 _("Couldn't parse date/time specifier: %s"),
887 return (mktime(&ts
));
890 static void parse_tune2fs_options(int argc
, char **argv
)
896 char optstring
[100] = "c:e:fg:i:jlm:o:r:s:u:C:E:I:J:L:M:O:T:U:";
899 strcat(optstring
, "Q:");
903 printf("tune2fs %s (%s)\n", E2FSPROGS_VERSION
, E2FSPROGS_DATE
);
904 while ((c
= getopt(argc
, argv
, optstring
)) != EOF
)
907 max_mount_count
= strtol(optarg
, &tmp
, 0);
908 if (*tmp
|| max_mount_count
> 16000) {
909 com_err(program_name
, 0,
910 _("bad mounts count - %s"),
914 if (max_mount_count
== 0)
915 max_mount_count
= -1;
917 open_flag
= EXT2_FLAG_RW
;
920 mount_count
= strtoul(optarg
, &tmp
, 0);
921 if (*tmp
|| mount_count
> 16000) {
922 com_err(program_name
, 0,
923 _("bad mounts count - %s"),
928 open_flag
= EXT2_FLAG_RW
;
931 if (strcmp(optarg
, "continue") == 0)
932 errors
= EXT2_ERRORS_CONTINUE
;
933 else if (strcmp(optarg
, "remount-ro") == 0)
934 errors
= EXT2_ERRORS_RO
;
935 else if (strcmp(optarg
, "panic") == 0)
936 errors
= EXT2_ERRORS_PANIC
;
938 com_err(program_name
, 0,
939 _("bad error behavior - %s"),
944 open_flag
= EXT2_FLAG_RW
;
947 extended_cmd
= optarg
;
948 open_flag
|= EXT2_FLAG_RW
;
950 case 'f': /* Force */
954 resgid
= strtoul(optarg
, &tmp
, 0);
956 gr
= getgrnam(optarg
);
965 com_err(program_name
, 0,
966 _("bad gid/group name - %s"),
971 open_flag
= EXT2_FLAG_RW
;
974 interval
= strtoul(optarg
, &tmp
, 0);
987 case 'M': /* months! */
988 interval
*= 86400 * 30;
992 case 'W': /* weeks */
993 interval
*= 86400 * 7;
998 com_err(program_name
, 0,
999 _("bad interval - %s"), optarg
);
1003 open_flag
= EXT2_FLAG_RW
;
1008 open_flag
= EXT2_FLAG_RW
;
1011 parse_journal_opts(optarg
);
1012 open_flag
= EXT2_FLAG_RW
;
1020 open_flag
|= EXT2_FLAG_RW
|
1021 EXT2_FLAG_JOURNAL_DEV_OK
;
1024 reserved_ratio
= strtod(optarg
, &tmp
);
1025 if (*tmp
|| reserved_ratio
> 50 ||
1026 reserved_ratio
< 0) {
1027 com_err(program_name
, 0,
1028 _("bad reserved block ratio - %s"),
1033 open_flag
= EXT2_FLAG_RW
;
1036 new_last_mounted
= optarg
;
1038 open_flag
= EXT2_FLAG_RW
;
1042 com_err(program_name
, 0, "%s",
1043 _("-o may only be specified once"));
1046 mntopts_cmd
= optarg
;
1047 open_flag
= EXT2_FLAG_RW
;
1051 com_err(program_name
, 0, "%s",
1052 _("-O may only be specified once"));
1055 features_cmd
= optarg
;
1056 open_flag
= EXT2_FLAG_RW
;
1061 parse_quota_opts(optarg
);
1062 open_flag
= EXT2_FLAG_RW
;
1066 reserved_blocks
= strtoul(optarg
, &tmp
, 0);
1068 com_err(program_name
, 0,
1069 _("bad reserved blocks count - %s"),
1074 open_flag
= EXT2_FLAG_RW
;
1076 case 's': /* Deprecated */
1077 s_flag
= atoi(optarg
);
1078 open_flag
= EXT2_FLAG_RW
;
1082 last_check_time
= parse_time(optarg
);
1083 open_flag
= EXT2_FLAG_RW
;
1086 resuid
= strtoul(optarg
, &tmp
, 0);
1088 pw
= getpwnam(optarg
);
1092 resuid
= pw
->pw_uid
;
1097 com_err(program_name
, 0,
1098 _("bad uid/user name - %s"),
1103 open_flag
= EXT2_FLAG_RW
;
1108 open_flag
= EXT2_FLAG_RW
|
1109 EXT2_FLAG_JOURNAL_DEV_OK
;
1112 new_inode_size
= strtoul(optarg
, &tmp
, 0);
1114 com_err(program_name
, 0,
1115 _("bad inode size - %s"),
1119 if (!((new_inode_size
&
1120 (new_inode_size
- 1)) == 0)) {
1121 com_err(program_name
, 0,
1122 _("Inode size must be a "
1123 "power of two- %s"),
1127 open_flag
= EXT2_FLAG_RW
;
1133 if (optind
< argc
- 1 || optind
== argc
)
1135 if (!open_flag
&& !l_flag
)
1137 io_options
= strchr(argv
[optind
], '?');
1140 device_name
= blkid_get_devname(NULL
, argv
[optind
], NULL
);
1142 com_err(program_name
, 0, _("Unable to resolve '%s'"),
1148 #ifdef CONFIG_BUILD_FINDFS
1149 void do_findfs(int argc
, char **argv
)
1154 (strncmp(argv
[1], "LABEL=", 6) && strncmp(argv
[1], "UUID=", 5))) {
1155 fprintf(stderr
, "Usage: findfs LABEL=<label>|UUID=<uuid>\n");
1158 dev
= blkid_get_devname(NULL
, argv
[1], NULL
);
1160 com_err("findfs", 0, _("Unable to resolve '%s'"),
1169 static int parse_extended_opts(ext2_filsys fs
, const char *opts
)
1171 char *buf
, *token
, *next
, *p
, *arg
;
1176 buf
= malloc(len
+1);
1178 fprintf(stderr
, "%s",
1179 _("Couldn't allocate memory to parse options!\n"));
1183 for (token
= buf
; token
&& *token
; token
= next
) {
1184 p
= strchr(token
, ',');
1190 arg
= strchr(token
, '=');
1195 if (strcmp(token
, "clear-mmp") == 0 ||
1196 strcmp(token
, "clear_mmp") == 0) {
1198 } else if (strcmp(token
, "mmp_update_interval") == 0) {
1204 intv
= strtoul(arg
, &p
, 0);
1207 _("Invalid mmp_update_interval: %s\n"),
1213 intv
= EXT4_MMP_UPDATE_INTERVAL
;
1214 } else if (intv
> EXT4_MMP_MAX_UPDATE_INTERVAL
) {
1216 _("mmp_update_interval too big: %lu\n"),
1221 printf(P_("Setting multiple mount protection update "
1222 "interval to %lu second\n",
1223 "Setting multiple mount protection update "
1224 "interval to %lu seconds\n", intv
),
1226 fs
->super
->s_mmp_update_interval
= intv
;
1227 ext2fs_mark_super_dirty(fs
);
1228 } else if (!strcmp(token
, "test_fs")) {
1229 fs
->super
->s_flags
|= EXT2_FLAGS_TEST_FILESYS
;
1230 printf("Setting test filesystem flag\n");
1231 ext2fs_mark_super_dirty(fs
);
1232 } else if (!strcmp(token
, "^test_fs")) {
1233 fs
->super
->s_flags
&= ~EXT2_FLAGS_TEST_FILESYS
;
1234 printf("Clearing test filesystem flag\n");
1235 ext2fs_mark_super_dirty(fs
);
1236 } else if (strcmp(token
, "stride") == 0) {
1241 stride
= strtoul(arg
, &p
, 0);
1244 _("Invalid RAID stride: %s\n"),
1250 } else if (strcmp(token
, "stripe-width") == 0 ||
1251 strcmp(token
, "stripe_width") == 0) {
1256 stripe_width
= strtoul(arg
, &p
, 0);
1259 _("Invalid RAID stripe-width: %s\n"),
1264 stripe_width_set
= 1;
1265 } else if (strcmp(token
, "hash_alg") == 0 ||
1266 strcmp(token
, "hash-alg") == 0) {
1271 hash_alg
= e2p_string2hash(arg
);
1274 _("Invalid hash algorithm: %s\n"),
1279 fs
->super
->s_def_hash_version
= hash_alg
;
1280 printf(_("Setting default hash algorithm "
1283 ext2fs_mark_super_dirty(fs
);
1284 } else if (!strcmp(token
, "mount_opts")) {
1289 if (strlen(arg
) >= sizeof(fs
->super
->s_mount_opts
)) {
1291 "Extended mount options too long\n");
1294 ext_mount_opts
= strdup(arg
);
1299 fprintf(stderr
, "%s", _("\nBad options specified.\n\n"
1300 "Extended options are separated by commas, "
1301 "and may take an argument which\n"
1302 "\tis set off by an equals ('=') sign.\n\n"
1303 "Valid extended options are:\n"
1305 "\thash_alg=<hash algorithm>\n"
1306 "\tmount_opts=<extended default mount options>\n"
1307 "\tstride=<RAID per-disk chunk size in blocks>\n"
1308 "\tstripe_width=<RAID stride*data disks in blocks>\n"
1320 * Fill in the block bitmap bmap with the information regarding the
1321 * blocks to be moved
1323 static int get_move_bitmaps(ext2_filsys fs
, int new_ino_blks_per_grp
,
1324 ext2fs_block_bitmap bmap
)
1328 ext2_badblocks_list bb_list
= 0;
1329 blk64_t j
, needed_blocks
= 0;
1330 blk64_t start_blk
, end_blk
;
1332 retval
= ext2fs_read_bb_inode(fs
, &bb_list
);
1336 for (i
= 0; i
< fs
->group_desc_count
; i
++) {
1337 start_blk
= ext2fs_inode_table_loc(fs
, i
) +
1338 fs
->inode_blocks_per_group
;
1340 end_blk
= ext2fs_inode_table_loc(fs
, i
) +
1341 new_ino_blks_per_grp
;
1343 for (j
= start_blk
; j
< end_blk
; j
++) {
1344 if (ext2fs_test_block_bitmap2(fs
->block_map
, j
)) {
1346 * IF the block is a bad block we fail
1348 if (ext2fs_badblocks_list_test(bb_list
, j
)) {
1349 ext2fs_badblocks_list_free(bb_list
);
1353 ext2fs_mark_block_bitmap2(bmap
, j
);
1356 * We are going to use this block for
1357 * inode table. So mark them used.
1359 ext2fs_mark_block_bitmap2(fs
->block_map
, j
);
1362 needed_blocks
+= end_blk
- start_blk
;
1365 ext2fs_badblocks_list_free(bb_list
);
1366 if (needed_blocks
> ext2fs_free_blocks_count(fs
->super
))
1372 static int ext2fs_is_meta_block(ext2_filsys fs
, blk64_t blk
)
1375 group
= ext2fs_group_of_blk2(fs
, blk
);
1376 if (ext2fs_block_bitmap_loc(fs
, group
) == blk
)
1378 if (ext2fs_inode_bitmap_loc(fs
, group
) == blk
)
1383 static int ext2fs_is_block_in_group(ext2_filsys fs
, dgrp_t group
, blk64_t blk
)
1385 blk64_t start_blk
, end_blk
;
1386 start_blk
= fs
->super
->s_first_data_block
+
1387 EXT2_BLOCKS_PER_GROUP(fs
->super
) * group
;
1389 * We cannot get new block beyond end_blk for for the last block group
1390 * so we can check with EXT2_BLOCKS_PER_GROUP even for last block group
1392 end_blk
= start_blk
+ EXT2_BLOCKS_PER_GROUP(fs
->super
);
1393 if (blk
>= start_blk
&& blk
<= end_blk
)
1398 static int move_block(ext2_filsys fs
, ext2fs_block_bitmap bmap
)
1405 blk64_t blk
, new_blk
, goal
;
1406 struct blk_move
*bmv
;
1408 retval
= ext2fs_get_mem(fs
->blocksize
, &buf
);
1412 for (new_blk
= blk
= fs
->super
->s_first_data_block
;
1413 blk
< ext2fs_blocks_count(fs
->super
); blk
++) {
1414 if (!ext2fs_test_block_bitmap2(bmap
, blk
))
1417 if (ext2fs_is_meta_block(fs
, blk
)) {
1419 * If the block is mapping a fs meta data block
1420 * like group desc/block bitmap/inode bitmap. We
1421 * should find a block in the same group and fix
1422 * the respective fs metadata pointers. Otherwise
1425 group
= ext2fs_group_of_blk2(fs
, blk
);
1426 goal
= ext2fs_group_first_block2(fs
, group
);
1432 retval
= ext2fs_new_block2(fs
, goal
, NULL
, &new_blk
);
1436 /* new fs meta data block should be in the same group */
1437 if (meta_data
&& !ext2fs_is_block_in_group(fs
, group
, new_blk
)) {
1442 /* Mark this block as allocated */
1443 ext2fs_mark_block_bitmap2(fs
->block_map
, new_blk
);
1445 /* Add it to block move list */
1446 retval
= ext2fs_get_mem(sizeof(struct blk_move
), &bmv
);
1451 bmv
->new_loc
= new_blk
;
1453 list_add(&(bmv
->list
), &blk_move_list
);
1455 retval
= io_channel_read_blk64(fs
->io
, blk
, 1, buf
);
1459 retval
= io_channel_write_blk64(fs
->io
, new_blk
, 1, buf
);
1465 ext2fs_free_mem(&buf
);
1469 static blk64_t
translate_block(blk64_t blk
)
1471 struct list_head
*entry
;
1472 struct blk_move
*bmv
;
1474 list_for_each(entry
, &blk_move_list
) {
1475 bmv
= list_entry(entry
, struct blk_move
, list
);
1476 if (bmv
->old_loc
== blk
)
1477 return bmv
->new_loc
;
1483 static int process_block(ext2_filsys fs
EXT2FS_ATTR((unused
)),
1485 e2_blkcnt_t blockcnt
EXT2FS_ATTR((unused
)),
1486 blk64_t ref_block
EXT2FS_ATTR((unused
)),
1487 int ref_offset
EXT2FS_ATTR((unused
)),
1492 ext2fs_block_bitmap bmap
= (ext2fs_block_bitmap
) priv_data
;
1494 if (!ext2fs_test_block_bitmap2(bmap
, *block_nr
))
1496 new_blk
= translate_block(*block_nr
);
1498 *block_nr
= new_blk
;
1500 * This will force the ext2fs_write_inode in the iterator
1502 ret
|= BLOCK_CHANGED
;
1508 static int inode_scan_and_fix(ext2_filsys fs
, ext2fs_block_bitmap bmap
)
1510 errcode_t retval
= 0;
1513 char *block_buf
= 0;
1514 struct ext2_inode inode
;
1515 ext2_inode_scan scan
= NULL
;
1517 retval
= ext2fs_get_mem(fs
->blocksize
* 3, &block_buf
);
1521 retval
= ext2fs_open_inode_scan(fs
, 0, &scan
);
1526 retval
= ext2fs_get_next_inode(scan
, &ino
, &inode
);
1533 if (inode
.i_links_count
== 0)
1534 continue; /* inode not in use */
1537 * If we end up modifying the journal inode
1538 * the sb->s_jnl_blocks will differ. But a
1539 * subsequent e2fsck fixes that.
1540 * Do we need to fix this ??
1543 if (ext2fs_file_acl_block(fs
, &inode
) &&
1544 ext2fs_test_block_bitmap2(bmap
,
1545 ext2fs_file_acl_block(fs
, &inode
))) {
1546 blk
= translate_block(ext2fs_file_acl_block(fs
,
1551 ext2fs_file_acl_block_set(fs
, &inode
, blk
);
1554 * Write the inode to disk so that inode table
1557 retval
= ext2fs_write_inode(fs
, ino
, &inode
);
1562 if (!ext2fs_inode_has_valid_blocks2(fs
, &inode
))
1565 retval
= ext2fs_block_iterate3(fs
, ino
, 0, block_buf
,
1566 process_block
, bmap
);
1573 ext2fs_free_mem(&block_buf
);
1579 * We need to scan for inode and block bitmaps that may need to be
1580 * moved. This can take place if the filesystem was formatted for
1581 * RAID arrays using the mke2fs's extended option "stride".
1583 static int group_desc_scan_and_fix(ext2_filsys fs
, ext2fs_block_bitmap bmap
)
1586 blk64_t blk
, new_blk
;
1588 for (i
= 0; i
< fs
->group_desc_count
; i
++) {
1589 blk
= ext2fs_block_bitmap_loc(fs
, i
);
1590 if (ext2fs_test_block_bitmap2(bmap
, blk
)) {
1591 new_blk
= translate_block(blk
);
1594 ext2fs_block_bitmap_loc_set(fs
, i
, new_blk
);
1597 blk
= ext2fs_inode_bitmap_loc(fs
, i
);
1598 if (ext2fs_test_block_bitmap2(bmap
, blk
)) {
1599 new_blk
= translate_block(blk
);
1602 ext2fs_inode_bitmap_loc_set(fs
, i
, new_blk
);
1608 static int expand_inode_table(ext2_filsys fs
, unsigned long new_ino_size
)
1613 int new_ino_blks_per_grp
;
1615 char *old_itable
= NULL
, *new_itable
= NULL
;
1616 char *tmp_old_itable
= NULL
, *tmp_new_itable
= NULL
;
1617 unsigned long old_ino_size
;
1618 int old_itable_size
, new_itable_size
;
1620 old_itable_size
= fs
->inode_blocks_per_group
* fs
->blocksize
;
1621 old_ino_size
= EXT2_INODE_SIZE(fs
->super
);
1623 new_ino_blks_per_grp
= ext2fs_div_ceil(
1624 EXT2_INODES_PER_GROUP(fs
->super
) *
1628 new_itable_size
= new_ino_blks_per_grp
* fs
->blocksize
;
1630 retval
= ext2fs_get_mem(old_itable_size
, &old_itable
);
1634 retval
= ext2fs_get_mem(new_itable_size
, &new_itable
);
1638 tmp_old_itable
= old_itable
;
1639 tmp_new_itable
= new_itable
;
1641 for (i
= 0; i
< fs
->group_desc_count
; i
++) {
1642 blk
= ext2fs_inode_table_loc(fs
, i
);
1643 retval
= io_channel_read_blk64(fs
->io
, blk
,
1644 fs
->inode_blocks_per_group
, old_itable
);
1648 for (j
= 0; j
< EXT2_INODES_PER_GROUP(fs
->super
); j
++) {
1649 memcpy(new_itable
, old_itable
, old_ino_size
);
1651 memset(new_itable
+old_ino_size
, 0,
1652 new_ino_size
- old_ino_size
);
1654 new_itable
+= new_ino_size
;
1655 old_itable
+= old_ino_size
;
1658 /* reset the pointer */
1659 old_itable
= tmp_old_itable
;
1660 new_itable
= tmp_new_itable
;
1662 retval
= io_channel_write_blk64(fs
->io
, blk
,
1663 new_ino_blks_per_grp
, new_itable
);
1668 /* Update the meta data */
1669 fs
->inode_blocks_per_group
= new_ino_blks_per_grp
;
1670 fs
->super
->s_inode_size
= new_ino_size
;
1674 ext2fs_free_mem(&old_itable
);
1677 ext2fs_free_mem(&new_itable
);
1682 static errcode_t
ext2fs_calculate_summary_stats(ext2_filsys fs
)
1686 unsigned int group
= 0;
1687 unsigned int count
= 0;
1692 * First calculate the block statistics
1694 for (blk
= fs
->super
->s_first_data_block
;
1695 blk
< ext2fs_blocks_count(fs
->super
); blk
++) {
1696 if (!ext2fs_fast_test_block_bitmap2(fs
->block_map
, blk
)) {
1701 if ((count
== fs
->super
->s_blocks_per_group
) ||
1702 (blk
== ext2fs_blocks_count(fs
->super
)-1)) {
1703 ext2fs_bg_free_blocks_count_set(fs
, group
++,
1709 total_free
= EXT2FS_C2B(fs
, total_free
);
1710 ext2fs_free_blocks_count_set(fs
->super
, total_free
);
1713 * Next, calculate the inode statistics
1720 /* Protect loop from wrap-around if s_inodes_count maxed */
1721 for (ino
= 1; ino
<= fs
->super
->s_inodes_count
&& ino
> 0; ino
++) {
1722 if (!ext2fs_fast_test_inode_bitmap2(fs
->inode_map
, ino
)) {
1727 if ((count
== fs
->super
->s_inodes_per_group
) ||
1728 (ino
== fs
->super
->s_inodes_count
)) {
1729 ext2fs_bg_free_inodes_count_set(fs
, group
++,
1735 fs
->super
->s_free_inodes_count
= total_free
;
1736 ext2fs_mark_super_dirty(fs
);
1740 #define list_for_each_safe(pos, pnext, head) \
1741 for (pos = (head)->next, pnext = pos->next; pos != (head); \
1742 pos = pnext, pnext = pos->next)
1744 static void free_blk_move_list(void)
1746 struct list_head
*entry
, *tmp
;
1747 struct blk_move
*bmv
;
1749 list_for_each_safe(entry
, tmp
, &blk_move_list
) {
1750 bmv
= list_entry(entry
, struct blk_move
, list
);
1752 ext2fs_free_mem(&bmv
);
1757 static int resize_inode(ext2_filsys fs
, unsigned long new_size
)
1760 int new_ino_blks_per_grp
;
1761 ext2fs_block_bitmap bmap
;
1763 retval
= ext2fs_read_inode_bitmap(fs
);
1765 fputs(_("Failed to read inode bitmap\n"), stderr
);
1768 retval
= ext2fs_read_block_bitmap(fs
);
1770 fputs(_("Failed to read block bitmap\n"), stderr
);
1773 INIT_LIST_HEAD(&blk_move_list
);
1776 new_ino_blks_per_grp
= ext2fs_div_ceil(
1777 EXT2_INODES_PER_GROUP(fs
->super
)*
1781 /* We may change the file system.
1782 * Mark the file system as invalid so that
1783 * the user is prompted to run fsck.
1785 fs
->super
->s_state
&= ~EXT2_VALID_FS
;
1787 retval
= ext2fs_allocate_block_bitmap(fs
, _("blocks to be moved"),
1790 fputs(_("Failed to allocate block bitmap when "
1791 "increasing inode size\n"), stderr
);
1794 retval
= get_move_bitmaps(fs
, new_ino_blks_per_grp
, bmap
);
1796 fputs(_("Not enough space to increase inode size \n"), stderr
);
1799 retval
= move_block(fs
, bmap
);
1801 fputs(_("Failed to relocate blocks during inode resize \n"),
1805 retval
= inode_scan_and_fix(fs
, bmap
);
1809 retval
= group_desc_scan_and_fix(fs
, bmap
);
1813 retval
= expand_inode_table(fs
, new_size
);
1817 ext2fs_calculate_summary_stats(fs
);
1819 fs
->super
->s_state
|= EXT2_VALID_FS
;
1820 /* mark super block and block bitmap as dirty */
1821 ext2fs_mark_super_dirty(fs
);
1822 ext2fs_mark_bb_dirty(fs
);
1825 free_blk_move_list();
1826 ext2fs_free_block_bitmap(bmap
);
1831 free_blk_move_list();
1832 ext2fs_free_block_bitmap(bmap
);
1833 fputs(_("Error in resizing the inode size.\n"
1834 "Run e2undo to undo the "
1835 "file system changes. \n"), stderr
);
1840 static int tune2fs_setup_tdb(const char *name
, io_manager
*io_ptr
)
1842 errcode_t retval
= 0;
1843 const char *tdb_dir
;
1845 char *dev_name
, *tmp_name
;
1849 * Configuration via a conf file would be
1852 profile_get_string(profile
, "scratch_files",
1856 tmp_name
= strdup(name
);
1859 com_err(program_name
, ENOMEM
, "%s",
1860 _("Couldn't allocate memory for tdb filename\n"));
1863 dev_name
= basename(tmp_name
);
1865 tdb_dir
= getenv("E2FSPROGS_UNDO_DIR");
1867 tdb_dir
= "/var/lib/e2fsprogs";
1869 if (!strcmp(tdb_dir
, "none") || (tdb_dir
[0] == 0) ||
1870 access(tdb_dir
, W_OK
))
1873 tdb_file
= malloc(strlen(tdb_dir
) + 9 + strlen(dev_name
) + 7 + 1);
1876 sprintf(tdb_file
, "%s/tune2fs-%s.e2undo", tdb_dir
, dev_name
);
1878 if ((unlink(tdb_file
) < 0) && (errno
!= ENOENT
)) {
1880 com_err(program_name
, retval
,
1881 _("while trying to delete %s"), tdb_file
);
1886 set_undo_io_backing_manager(*io_ptr
);
1887 *io_ptr
= undo_io_manager
;
1888 set_undo_io_backup_file(tdb_file
);
1889 printf(_("To undo the tune2fs operation please run "
1890 "the command\n e2undo %s %s\n\n"),
1897 int main(int argc
, char **argv
)
1901 struct ext2_super_block
*sb
;
1902 io_manager io_ptr
, io_ptr_orig
= NULL
;
1906 setlocale(LC_MESSAGES
, "");
1907 setlocale(LC_CTYPE
, "");
1908 bindtextdomain(NLS_CAT_NAME
, LOCALEDIR
);
1909 textdomain(NLS_CAT_NAME
);
1910 set_com_err_gettext(gettext
);
1913 program_name
= *argv
;
1914 add_error_table(&et_ext2_error_table
);
1916 #ifdef CONFIG_BUILD_FINDFS
1917 if (strcmp(get_progname(argv
[0]), "findfs") == 0)
1918 do_findfs(argc
, argv
);
1920 if (strcmp(get_progname(argv
[0]), "e2label") == 0)
1921 parse_e2label_options(argc
, argv
);
1923 parse_tune2fs_options(argc
, argv
);
1925 #ifdef CONFIG_TESTIO_DEBUG
1926 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_DEBUG")) {
1927 io_ptr
= test_io_manager
;
1928 test_io_backing_manager
= unix_io_manager
;
1931 io_ptr
= unix_io_manager
;
1934 if ((open_flag
& EXT2_FLAG_RW
) == 0 || f_flag
)
1935 open_flag
|= EXT2_FLAG_SKIP_MMP
;
1937 open_flag
|= EXT2_FLAG_64BITS
;
1939 /* keep the filesystem struct around to dump MMP data */
1940 open_flag
|= EXT2_FLAG_NOFREE_ON_ERROR
;
1942 retval
= ext2fs_open2(device_name
, io_options
, open_flag
,
1945 com_err(program_name
, retval
,
1946 _("while trying to open %s"),
1948 if (retval
== EXT2_ET_MMP_FSCK_ON
||
1949 retval
== EXT2_ET_MMP_UNKNOWN_SEQ
)
1950 dump_mmp_msg(fs
->mmp_buf
,
1951 _("If you are sure the filesystem "
1952 "is not in use on any node, run:\n"
1953 "'tune2fs -f -E clear_mmp {device}'\n"));
1954 else if (retval
== EXT2_ET_MMP_FAILED
)
1955 dump_mmp_msg(fs
->mmp_buf
, NULL
);
1956 else if (retval
== EXT2_ET_MMP_MAGIC_INVALID
)
1958 _("MMP block magic is bad. Try to fix it by "
1959 "running:\n'e2fsck -f %s'\n"), device_name
);
1960 else if (retval
!= EXT2_ET_MMP_FAILED
)
1961 fprintf(stderr
, "%s",
1962 _("Couldn't find valid filesystem superblock.\n"));
1967 fs
->default_bitmap_type
= EXT2FS_BMAP64_RBTREE
;
1969 if (I_flag
&& !io_ptr_orig
) {
1971 * Check the inode size is right so we can issue an
1972 * error message and bail before setting up the tdb
1975 if (new_inode_size
== EXT2_INODE_SIZE(fs
->super
)) {
1976 fprintf(stderr
, _("The inode size is already %lu\n"),
1981 if (new_inode_size
< EXT2_INODE_SIZE(fs
->super
)) {
1982 fprintf(stderr
, "%s",
1983 _("Shrinking inode size is not supported\n"));
1987 if (new_inode_size
> fs
->blocksize
) {
1988 fprintf(stderr
, _("Invalid inode size %lu (max %d)\n"),
1989 new_inode_size
, fs
->blocksize
);
1995 * If inode resize is requested use the
1998 io_ptr_orig
= io_ptr
;
1999 retval
= tune2fs_setup_tdb(device_name
, &io_ptr
);
2004 if (io_ptr
!= io_ptr_orig
) {
2011 fs
->flags
&= ~EXT2_FLAG_MASTER_SB_ONLY
;
2014 /* For e2label emulation */
2015 printf("%.*s\n", (int) sizeof(sb
->s_volume_name
),
2017 remove_error_table(&et_ext2_error_table
);
2021 retval
= ext2fs_check_if_mounted(device_name
, &mount_flags
);
2023 com_err("ext2fs_check_if_mount", retval
,
2024 _("while determining whether %s is mounted."),
2029 /* Normally we only need to write out the superblock */
2030 fs
->flags
|= EXT2_FLAG_SUPER_ONLY
;
2033 sb
->s_max_mnt_count
= max_mount_count
;
2034 ext2fs_mark_super_dirty(fs
);
2035 printf(_("Setting maximal mount count to %d\n"),
2039 sb
->s_mnt_count
= mount_count
;
2040 ext2fs_mark_super_dirty(fs
);
2041 printf(_("Setting current mount count to %d\n"), mount_count
);
2044 sb
->s_errors
= errors
;
2045 ext2fs_mark_super_dirty(fs
);
2046 printf(_("Setting error behavior to %d\n"), errors
);
2049 sb
->s_def_resgid
= resgid
;
2050 ext2fs_mark_super_dirty(fs
);
2051 printf(_("Setting reserved blocks gid to %lu\n"), resgid
);
2054 if (interval
>= (1ULL << 32)) {
2055 com_err(program_name
, 0,
2056 _("interval between checks is too big (%lu)"),
2061 sb
->s_checkinterval
= interval
;
2062 ext2fs_mark_super_dirty(fs
);
2063 printf(_("Setting interval between checks to %lu seconds\n"),
2067 ext2fs_r_blocks_count_set(sb
, reserved_ratio
*
2068 ext2fs_blocks_count(sb
) / 100.0);
2069 ext2fs_mark_super_dirty(fs
);
2070 printf (_("Setting reserved blocks percentage to %g%% (%llu blocks)\n"),
2071 reserved_ratio
, ext2fs_r_blocks_count(sb
));
2074 if (reserved_blocks
> ext2fs_blocks_count(sb
)/2) {
2075 com_err(program_name
, 0,
2076 _("reserved blocks count is too big (%llu)"),
2081 ext2fs_r_blocks_count_set(sb
, reserved_blocks
);
2082 ext2fs_mark_super_dirty(fs
);
2083 printf(_("Setting reserved blocks count to %llu\n"),
2087 if (sb
->s_feature_ro_compat
&
2088 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
) {
2089 fputs(_("\nThe filesystem already has sparse "
2090 "superblocks.\n"), stderr
);
2091 } else if (sb
->s_feature_incompat
&
2092 EXT2_FEATURE_INCOMPAT_META_BG
) {
2093 fputs(_("\nSetting the sparse superblock flag not "
2094 "supported\nfor filesystems with "
2095 "the meta_bg feature enabled.\n"),
2100 sb
->s_feature_ro_compat
|=
2101 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
;
2102 sb
->s_state
&= ~EXT2_VALID_FS
;
2103 ext2fs_mark_super_dirty(fs
);
2104 printf(_("\nSparse superblock flag set. %s"),
2109 fputs(_("\nClearing the sparse superblock flag not supported.\n"),
2115 sb
->s_lastcheck
= last_check_time
;
2116 ext2fs_mark_super_dirty(fs
);
2117 printf(_("Setting time filesystem last checked to %s\n"),
2118 ctime(&last_check_time
));
2121 sb
->s_def_resuid
= resuid
;
2122 ext2fs_mark_super_dirty(fs
);
2123 printf(_("Setting reserved blocks uid to %lu\n"), resuid
);
2126 if (strlen(new_label
) > sizeof(sb
->s_volume_name
))
2127 fputs(_("Warning: label too long, truncating.\n"),
2129 memset(sb
->s_volume_name
, 0, sizeof(sb
->s_volume_name
));
2130 strncpy(sb
->s_volume_name
, new_label
,
2131 sizeof(sb
->s_volume_name
));
2132 ext2fs_mark_super_dirty(fs
);
2135 memset(sb
->s_last_mounted
, 0, sizeof(sb
->s_last_mounted
));
2136 strncpy(sb
->s_last_mounted
, new_last_mounted
,
2137 sizeof(sb
->s_last_mounted
));
2138 ext2fs_mark_super_dirty(fs
);
2141 rc
= update_mntopts(fs
, mntopts_cmd
);
2146 rc
= update_feature_set(fs
, features_cmd
);
2151 rc
= parse_extended_opts(fs
, extended_cmd
);
2154 if (clear_mmp
&& !f_flag
) {
2155 fputs(_("Error in using clear_mmp. "
2156 "It must be used with -f\n"),
2162 rc
= ext2fs_mmp_clear(fs
);
2165 if (journal_size
|| journal_device
) {
2166 rc
= add_journal(fs
);
2172 if (mount_flags
& EXT2_MF_MOUNTED
) {
2173 fputs(_("The quota feature may only be changed when "
2174 "the filesystem is unmounted.\n"), stderr
);
2178 handle_quota_options(fs
);
2185 if (sb
->s_feature_ro_compat
&
2186 EXT4_FEATURE_RO_COMPAT_GDT_CSUM
) {
2188 * Changing the UUID requires rewriting all metadata,
2189 * which can race with a mounted fs. Don't allow that.
2191 if (mount_flags
& EXT2_MF_MOUNTED
) {
2192 fputs(_("The UUID may only be "
2193 "changed when the filesystem is "
2194 "unmounted.\n"), stderr
);
2197 if (check_fsck_needed(fs
))
2201 * Determine if the block group checksums are
2202 * correct so we know whether or not to set
2205 for (i
= 0; i
< fs
->group_desc_count
; i
++)
2206 if (!ext2fs_group_desc_csum_verify(fs
, i
))
2208 if (i
>= fs
->group_desc_count
)
2211 if ((strcasecmp(new_UUID
, "null") == 0) ||
2212 (strcasecmp(new_UUID
, "clear") == 0)) {
2213 uuid_clear(sb
->s_uuid
);
2214 } else if (strcasecmp(new_UUID
, "time") == 0) {
2215 uuid_generate_time(sb
->s_uuid
);
2216 } else if (strcasecmp(new_UUID
, "random") == 0) {
2217 uuid_generate(sb
->s_uuid
);
2218 } else if (uuid_parse(new_UUID
, sb
->s_uuid
)) {
2219 com_err(program_name
, 0, "%s",
2220 _("Invalid UUID format\n"));
2225 for (i
= 0; i
< fs
->group_desc_count
; i
++)
2226 ext2fs_group_desc_csum_set(fs
, i
);
2227 fs
->flags
&= ~EXT2_FLAG_SUPER_ONLY
;
2229 ext2fs_mark_super_dirty(fs
);
2232 if (mount_flags
& EXT2_MF_MOUNTED
) {
2233 fputs(_("The inode size may only be "
2234 "changed when the filesystem is "
2235 "unmounted.\n"), stderr
);
2239 if (fs
->super
->s_feature_incompat
&
2240 EXT4_FEATURE_INCOMPAT_FLEX_BG
) {
2241 fputs(_("Changing the inode size not supported for "
2242 "filesystems with the flex_bg\n"
2243 "feature enabled.\n"),
2249 * We want to update group descriptor also
2250 * with the new free inode count
2252 fs
->flags
&= ~EXT2_FLAG_SUPER_ONLY
;
2253 if (resize_inode(fs
, new_inode_size
) == 0) {
2254 printf(_("Setting inode size %lu\n"),
2257 printf("%s", _("Failed to change inode size\n"));
2266 sb
->s_raid_stride
= stride
;
2267 ext2fs_mark_super_dirty(fs
);
2268 printf(_("Setting stride size to %d\n"), stride
);
2270 if (stripe_width_set
) {
2271 sb
->s_raid_stripe_width
= stripe_width
;
2272 ext2fs_mark_super_dirty(fs
);
2273 printf(_("Setting stripe width to %d\n"), stripe_width
);
2275 if (ext_mount_opts
) {
2276 strncpy((char *)(fs
->super
->s_mount_opts
), ext_mount_opts
,
2277 sizeof(fs
->super
->s_mount_opts
));
2278 fs
->super
->s_mount_opts
[sizeof(fs
->super
->s_mount_opts
)-1] = 0;
2279 ext2fs_mark_super_dirty(fs
);
2280 printf(_("Setting extended default mount options to '%s'\n"),
2282 free(ext_mount_opts
);
2285 remove_error_table(&et_ext2_error_table
);
2289 ext2fs_mmp_stop(fs
);
2293 return (ext2fs_close(fs
) ? 1 : 0);