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() */
47 #include <sys/types.h>
51 #include "ext2fs/ext2_fs.h"
52 #include "ext2fs/ext2fs.h"
53 #include "et/com_err.h"
54 #include "uuid/uuid.h"
58 #include "blkid/blkid.h"
59 #include "quota/mkquota.h"
61 #include "../version.h"
62 #include "nls-enable.h"
64 #define QOPT_ENABLE (1)
65 #define QOPT_DISABLE (-1)
67 extern int ask_yn(const char *string
, int def
);
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
, Q_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
;
80 static blk64_t reserved_blocks
;
81 static double reserved_ratio
;
82 static unsigned long resgid
, resuid
;
83 static unsigned short errors
;
85 static char *features_cmd
;
86 static char *mntopts_cmd
;
87 static int stride
, stripe_width
;
88 static int stride_set
, stripe_width_set
;
89 static char *extended_cmd
;
90 static unsigned long new_inode_size
;
91 static char *ext_mount_opts
;
92 static int usrquota
, grpquota
;
94 int journal_size
, journal_flags
;
97 static struct list_head blk_move_list
;
100 struct list_head list
;
106 static const char *please_fsck
= N_("Please run e2fsck on the filesystem.\n");
108 #ifdef CONFIG_BUILD_FINDFS
109 void do_findfs(int argc
, char **argv
);
112 static void usage(void)
115 _("Usage: %s [-c max_mounts_count] [-e errors_behavior] "
117 "\t[-i interval[d|m|w]] [-j] [-J journal_options] [-l]\n"
118 "\t[-m reserved_blocks_percent] "
119 "[-o [^]mount_options[,...]] \n"
120 "\t[-r reserved_blocks_count] [-u user] [-C mount_count] "
121 "[-L volume_label]\n"
122 "\t[-M last_mounted_dir] [-O [^]feature[,...]]\n"
123 "\t[-E extended-option[,...]] [-T last_check_time] "
124 "[-U UUID]\n\t[ -I new_inode_size ] device\n"), program_name
);
128 static __u32 ok_features
[3] = {
130 EXT3_FEATURE_COMPAT_HAS_JOURNAL
|
131 EXT2_FEATURE_COMPAT_DIR_INDEX
,
133 EXT2_FEATURE_INCOMPAT_FILETYPE
|
134 EXT3_FEATURE_INCOMPAT_EXTENTS
|
135 EXT4_FEATURE_INCOMPAT_FLEX_BG
,
137 EXT2_FEATURE_RO_COMPAT_LARGE_FILE
|
138 EXT4_FEATURE_RO_COMPAT_HUGE_FILE
|
139 EXT4_FEATURE_RO_COMPAT_DIR_NLINK
|
140 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE
|
141 EXT4_FEATURE_RO_COMPAT_GDT_CSUM
|
142 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
|
143 EXT4_FEATURE_RO_COMPAT_QUOTA
146 static __u32 clear_ok_features
[3] = {
148 EXT3_FEATURE_COMPAT_HAS_JOURNAL
|
149 EXT2_FEATURE_COMPAT_RESIZE_INODE
|
150 EXT2_FEATURE_COMPAT_DIR_INDEX
,
152 EXT2_FEATURE_INCOMPAT_FILETYPE
|
153 EXT4_FEATURE_INCOMPAT_FLEX_BG
,
155 EXT2_FEATURE_RO_COMPAT_LARGE_FILE
|
156 EXT4_FEATURE_RO_COMPAT_HUGE_FILE
|
157 EXT4_FEATURE_RO_COMPAT_DIR_NLINK
|
158 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE
|
159 EXT4_FEATURE_RO_COMPAT_GDT_CSUM
|
160 EXT4_FEATURE_RO_COMPAT_QUOTA
164 * Remove an external journal from the filesystem
166 static void remove_journal_device(ext2_filsys fs
)
171 journal_superblock_t
*jsb
;
174 int commit_remove_journal
= 0;
178 commit_remove_journal
= 1; /* force removal even if error */
180 uuid_unparse(fs
->super
->s_journal_uuid
, buf
);
181 journal_path
= blkid_get_devname(NULL
, "UUID", buf
);
185 ext2fs_find_block_device(fs
->super
->s_journal_dev
);
190 #ifdef CONFIG_TESTIO_DEBUG
191 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
192 io_ptr
= test_io_manager
;
193 test_io_backing_manager
= unix_io_manager
;
196 io_ptr
= unix_io_manager
;
197 retval
= ext2fs_open(journal_path
, EXT2_FLAG_RW
|
198 EXT2_FLAG_JOURNAL_DEV_OK
, 0,
199 fs
->blocksize
, io_ptr
, &jfs
);
201 com_err(program_name
, retval
,
202 _("while trying to open external journal"));
203 goto no_valid_journal
;
205 if (!(jfs
->super
->s_feature_incompat
& EXT3_FEATURE_INCOMPAT_JOURNAL_DEV
)) {
206 fprintf(stderr
, _("%s is not a journal device.\n"),
208 goto no_valid_journal
;
211 /* Get the journal superblock */
212 if ((retval
= io_channel_read_blk64(jfs
->io
, 1, -1024, buf
))) {
213 com_err(program_name
, retval
,
214 _("while reading journal superblock"));
215 goto no_valid_journal
;
218 jsb
= (journal_superblock_t
*) buf
;
219 if ((jsb
->s_header
.h_magic
!= (unsigned) ntohl(JFS_MAGIC_NUMBER
)) ||
220 (jsb
->s_header
.h_blocktype
!= (unsigned) ntohl(JFS_SUPERBLOCK_V2
))) {
221 fputs(_("Journal superblock not found!\n"), stderr
);
222 goto no_valid_journal
;
225 /* Find the filesystem UUID */
226 nr_users
= ntohl(jsb
->s_nr_users
);
227 for (i
= 0; i
< nr_users
; i
++) {
228 if (memcmp(fs
->super
->s_uuid
,
229 &jsb
->s_users
[i
*16], 16) == 0)
233 fputs(_("Filesystem's UUID not found on journal device.\n"),
235 commit_remove_journal
= 1;
236 goto no_valid_journal
;
239 for (i
= 0; i
< nr_users
; i
++)
240 memcpy(&jsb
->s_users
[i
*16], &jsb
->s_users
[(i
+1)*16], 16);
241 jsb
->s_nr_users
= htonl(nr_users
);
243 /* Write back the journal superblock */
244 if ((retval
= io_channel_write_blk64(jfs
->io
, 1, -1024, buf
))) {
245 com_err(program_name
, retval
,
246 "while writing journal superblock.");
247 goto no_valid_journal
;
250 commit_remove_journal
= 1;
253 if (commit_remove_journal
== 0) {
254 fputs(_("Journal NOT removed\n"), stderr
);
257 fs
->super
->s_journal_dev
= 0;
258 uuid_clear(fs
->super
->s_journal_uuid
);
259 ext2fs_mark_super_dirty(fs
);
260 fputs(_("Journal removed\n"), stdout
);
264 /* Helper function for remove_journal_inode */
265 static int release_blocks_proc(ext2_filsys fs
, blk64_t
*blocknr
,
266 e2_blkcnt_t blockcnt
EXT2FS_ATTR((unused
)),
267 blk64_t ref_block
EXT2FS_ATTR((unused
)),
268 int ref_offset
EXT2FS_ATTR((unused
)),
269 void *private EXT2FS_ATTR((unused
)))
275 ext2fs_unmark_block_bitmap2(fs
->block_map
, block
);
276 group
= ext2fs_group_of_blk2(fs
, block
);
277 ext2fs_bg_free_blocks_count_set(fs
, group
, ext2fs_bg_free_blocks_count(fs
, group
) + 1);
278 ext2fs_group_desc_csum_set(fs
, group
);
279 ext2fs_free_blocks_count_add(fs
->super
, EXT2FS_CLUSTER_RATIO(fs
));
284 * Remove the journal inode from the filesystem
286 static void remove_journal_inode(ext2_filsys fs
)
288 struct ext2_inode inode
;
290 ino_t ino
= fs
->super
->s_journal_inum
;
292 retval
= ext2fs_read_inode(fs
, ino
, &inode
);
294 com_err(program_name
, retval
,
295 _("while reading journal inode"));
298 if (ino
== EXT2_JOURNAL_INO
) {
299 retval
= ext2fs_read_bitmaps(fs
);
301 com_err(program_name
, retval
,
302 _("while reading bitmaps"));
305 retval
= ext2fs_block_iterate3(fs
, ino
,
306 BLOCK_FLAG_READ_ONLY
, NULL
,
307 release_blocks_proc
, NULL
);
309 com_err(program_name
, retval
,
310 _("while clearing journal inode"));
313 memset(&inode
, 0, sizeof(inode
));
314 ext2fs_mark_bb_dirty(fs
);
315 fs
->flags
&= ~EXT2_FLAG_SUPER_ONLY
;
317 inode
.i_flags
&= ~EXT2_IMMUTABLE_FL
;
318 retval
= ext2fs_write_inode(fs
, ino
, &inode
);
320 com_err(program_name
, retval
,
321 _("while writing journal inode"));
324 fs
->super
->s_journal_inum
= 0;
325 ext2fs_mark_super_dirty(fs
);
329 * Update the default mount options
331 static void update_mntopts(ext2_filsys fs
, char *mntopts
)
333 struct ext2_super_block
*sb
= fs
->super
;
335 if (e2p_edit_mntopts(mntopts
, &sb
->s_default_mount_opts
, ~0)) {
336 fprintf(stderr
, _("Invalid mount option set: %s\n"),
340 ext2fs_mark_super_dirty(fs
);
343 static void request_fsck_afterwards(ext2_filsys fs
)
345 static int requested
= 0;
349 fs
->super
->s_state
&= ~EXT2_VALID_FS
;
350 printf("\n%s\n", _(please_fsck
));
351 if (mount_flags
& EXT2_MF_READONLY
)
352 printf(_("(and reboot afterwards!)\n"));
356 * Update the feature set as provided by the user.
358 static void update_feature_set(ext2_filsys fs
, char *features
)
360 struct ext2_super_block
*sb
= fs
->super
;
361 struct ext2_group_desc
*gd
;
362 __u32 old_features
[3];
364 unsigned int mask_err
;
366 #define FEATURE_ON(type, mask) (!(old_features[(type)] & (mask)) && \
367 ((&sb->s_feature_compat)[(type)] & (mask)))
368 #define FEATURE_OFF(type, mask) ((old_features[(type)] & (mask)) && \
369 !((&sb->s_feature_compat)[(type)] & (mask)))
370 #define FEATURE_CHANGED(type, mask) ((mask) & \
371 (old_features[(type)] ^ (&sb->s_feature_compat)[(type)]))
373 old_features
[E2P_FEATURE_COMPAT
] = sb
->s_feature_compat
;
374 old_features
[E2P_FEATURE_INCOMPAT
] = sb
->s_feature_incompat
;
375 old_features
[E2P_FEATURE_RO_INCOMPAT
] = sb
->s_feature_ro_compat
;
377 if (e2p_edit_feature2(features
, &sb
->s_feature_compat
,
378 ok_features
, clear_ok_features
,
379 &type_err
, &mask_err
)) {
382 _("Invalid filesystem option set: %s\n"),
384 else if (type_err
& E2P_FEATURE_NEGATE_FLAG
)
385 fprintf(stderr
, _("Clearing filesystem feature '%s' "
387 e2p_feature2string(type_err
&
388 E2P_FEATURE_TYPE_MASK
,
391 fprintf(stderr
, _("Setting filesystem feature '%s' "
393 e2p_feature2string(type_err
, mask_err
));
397 if (FEATURE_OFF(E2P_FEATURE_COMPAT
, EXT3_FEATURE_COMPAT_HAS_JOURNAL
)) {
398 if ((mount_flags
& EXT2_MF_MOUNTED
) &&
399 !(mount_flags
& EXT2_MF_READONLY
)) {
400 fputs(_("The has_journal feature may only be "
401 "cleared when the filesystem is\n"
402 "unmounted or mounted "
403 "read-only.\n"), stderr
);
406 if (sb
->s_feature_incompat
&
407 EXT3_FEATURE_INCOMPAT_RECOVER
) {
408 fputs(_("The needs_recovery flag is set. "
409 "Please run e2fsck before clearing\n"
410 "the has_journal flag.\n"), stderr
);
413 if (sb
->s_journal_inum
) {
414 remove_journal_inode(fs
);
416 if (sb
->s_journal_dev
) {
417 remove_journal_device(fs
);
421 if (FEATURE_ON(E2P_FEATURE_COMPAT
, EXT3_FEATURE_COMPAT_HAS_JOURNAL
)) {
423 * If adding a journal flag, let the create journal
424 * code below handle setting the flag and creating the
425 * journal. We supply a default size if necessary.
429 sb
->s_feature_compat
&= ~EXT3_FEATURE_COMPAT_HAS_JOURNAL
;
432 if (FEATURE_ON(E2P_FEATURE_COMPAT
, EXT2_FEATURE_COMPAT_DIR_INDEX
)) {
433 if (!sb
->s_def_hash_version
)
434 sb
->s_def_hash_version
= EXT2_HASH_HALF_MD4
;
435 if (uuid_is_null((unsigned char *) sb
->s_hash_seed
))
436 uuid_generate((unsigned char *) sb
->s_hash_seed
);
439 if (FEATURE_OFF(E2P_FEATURE_INCOMPAT
, EXT4_FEATURE_INCOMPAT_FLEX_BG
)) {
440 if (ext2fs_check_desc(fs
)) {
441 fputs(_("Clearing the flex_bg flag would "
442 "cause the the filesystem to be\n"
443 "inconsistent.\n"), stderr
);
448 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT
,
449 EXT4_FEATURE_RO_COMPAT_HUGE_FILE
)) {
450 if ((mount_flags
& EXT2_MF_MOUNTED
) &&
451 !(mount_flags
& EXT2_MF_READONLY
)) {
452 fputs(_("The huge_file feature may only be "
453 "cleared when the filesystem is\n"
454 "unmounted or mounted "
455 "read-only.\n"), stderr
);
460 if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT
,
461 EXT4_FEATURE_RO_COMPAT_GDT_CSUM
)) {
462 for (i
= 0; i
< fs
->group_desc_count
; i
++) {
463 gd
= ext2fs_group_desc(fs
, fs
->group_desc
, i
);
464 gd
->bg_itable_unused
= 0;
465 gd
->bg_flags
= EXT2_BG_INODE_ZEROED
;
466 ext2fs_group_desc_csum_set(fs
, i
);
468 fs
->flags
&= ~EXT2_FLAG_SUPER_ONLY
;
471 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT
,
472 EXT4_FEATURE_RO_COMPAT_GDT_CSUM
)) {
473 for (i
= 0; i
< fs
->group_desc_count
; i
++) {
474 gd
= ext2fs_group_desc(fs
, fs
->group_desc
, i
);
475 if ((gd
->bg_flags
& EXT2_BG_INODE_ZEROED
) == 0) {
477 * XXX what we really should do is zap
478 * uninitialized inode tables instead.
480 request_fsck_afterwards(fs
);
483 gd
->bg_itable_unused
= 0;
487 fs
->flags
&= ~EXT2_FLAG_SUPER_ONLY
;
490 if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT
,
491 EXT4_FEATURE_RO_COMPAT_QUOTA
)) {
493 * Set the Q_flag here and handle the quota options in the code
498 /* Enable both user quota and group quota by default */
499 usrquota
= QOPT_ENABLE
;
500 grpquota
= QOPT_ENABLE
;
502 sb
->s_feature_ro_compat
&= ~EXT4_FEATURE_RO_COMPAT_QUOTA
;
505 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT
,
506 EXT4_FEATURE_RO_COMPAT_QUOTA
)) {
508 * Set the Q_flag here and handle the quota options in the code
512 fputs(_("\nWarning: '^quota' option overrides '-Q'"
513 "arguments.\n"), stderr
);
515 /* Disable both user quota and group quota by default */
516 usrquota
= QOPT_DISABLE
;
517 grpquota
= QOPT_DISABLE
;
520 if (sb
->s_rev_level
== EXT2_GOOD_OLD_REV
&&
521 (sb
->s_feature_compat
|| sb
->s_feature_ro_compat
||
522 sb
->s_feature_incompat
))
523 ext2fs_update_dynamic_rev(fs
);
525 if (FEATURE_CHANGED(E2P_FEATURE_RO_INCOMPAT
,
526 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
) ||
527 FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT
,
528 EXT4_FEATURE_RO_COMPAT_HUGE_FILE
) ||
529 FEATURE_CHANGED(E2P_FEATURE_INCOMPAT
,
530 EXT2_FEATURE_INCOMPAT_FILETYPE
) ||
531 FEATURE_CHANGED(E2P_FEATURE_COMPAT
,
532 EXT2_FEATURE_COMPAT_RESIZE_INODE
) ||
533 FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT
,
534 EXT2_FEATURE_RO_COMPAT_LARGE_FILE
))
535 request_fsck_afterwards(fs
);
537 if ((old_features
[E2P_FEATURE_COMPAT
] != sb
->s_feature_compat
) ||
538 (old_features
[E2P_FEATURE_INCOMPAT
] != sb
->s_feature_incompat
) ||
539 (old_features
[E2P_FEATURE_RO_INCOMPAT
] != sb
->s_feature_ro_compat
))
540 ext2fs_mark_super_dirty(fs
);
544 * Add a journal to the filesystem.
546 static void add_journal(ext2_filsys fs
)
548 unsigned long journal_blocks
;
553 if (fs
->super
->s_feature_compat
&
554 EXT3_FEATURE_COMPAT_HAS_JOURNAL
) {
555 fputs(_("The filesystem already has a journal.\n"), stderr
);
558 if (journal_device
) {
559 check_plausibility(journal_device
);
560 check_mount(journal_device
, 0, _("journal"));
561 #ifdef CONFIG_TESTIO_DEBUG
562 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
563 io_ptr
= test_io_manager
;
564 test_io_backing_manager
= unix_io_manager
;
567 io_ptr
= unix_io_manager
;
568 retval
= ext2fs_open(journal_device
, EXT2_FLAG_RW
|
569 EXT2_FLAG_JOURNAL_DEV_OK
, 0,
570 fs
->blocksize
, io_ptr
, &jfs
);
572 com_err(program_name
, retval
,
573 _("\n\twhile trying to open journal on %s\n"),
577 printf(_("Creating journal on device %s: "),
581 retval
= ext2fs_add_journal_device(fs
, jfs
);
584 com_err(program_name
, retval
,
585 _("while adding filesystem to journal on %s"),
589 fputs(_("done\n"), stdout
);
590 } else if (journal_size
) {
591 fputs(_("Creating journal inode: "), stdout
);
593 journal_blocks
= figure_journal_size(journal_size
, fs
);
595 retval
= ext2fs_add_journal_inode(fs
, journal_blocks
,
598 fprintf(stderr
, "\n");
599 com_err(program_name
, retval
,
600 _("\n\twhile trying to create journal file"));
603 fputs(_("done\n"), stdout
);
605 * If the filesystem wasn't mounted, we need to force
606 * the block group descriptors out.
608 if ((mount_flags
& EXT2_MF_MOUNTED
) == 0)
609 fs
->flags
&= ~EXT2_FLAG_SUPER_ONLY
;
611 print_check_message(fs
->super
->s_max_mnt_count
,
612 fs
->super
->s_checkinterval
);
616 free(journal_device
);
620 void handle_quota_options(ext2_filsys fs
)
626 if (!usrquota
&& !grpquota
)
630 init_quota_context(&qctx
, fs
, -1);
632 if (usrquota
== QOPT_ENABLE
&& !fs
->super
->s_usr_quota_inum
) {
633 if ((qf_ino
= quota_file_exists(fs
, USRQUOTA
, QFMT_VFS_V1
)) > 0)
634 set_sb_quota_inum(fs
, qf_ino
, USRQUOTA
);
636 write_quota_inode(qctx
, USRQUOTA
);
637 } else if (usrquota
== QOPT_DISABLE
) {
638 remove_quota_inode(fs
, USRQUOTA
);
641 if (grpquota
== QOPT_ENABLE
&& !fs
->super
->s_grp_quota_inum
) {
642 if ((qf_ino
= quota_file_exists(fs
, GRPQUOTA
, QFMT_VFS_V1
)) > 0)
643 set_sb_quota_inum(fs
, qf_ino
, GRPQUOTA
);
645 write_quota_inode(qctx
, GRPQUOTA
);
646 } else if (grpquota
== QOPT_DISABLE
) {
647 remove_quota_inode(fs
, GRPQUOTA
);
650 release_quota_context(&qctx
);
652 if ((usrquota
== QOPT_ENABLE
) || (grpquota
== QOPT_ENABLE
)) {
653 fs
->super
->s_feature_ro_compat
|= EXT4_FEATURE_RO_COMPAT_QUOTA
;
654 ext2fs_mark_super_dirty(fs
);
655 } else if ((usrquota
== QOPT_DISABLE
) && (grpquota
== QOPT_DISABLE
)) {
656 fs
->super
->s_feature_ro_compat
&= ~EXT4_FEATURE_RO_COMPAT_QUOTA
;
657 ext2fs_mark_super_dirty(fs
);
663 void parse_quota_opts(const char *opts
)
665 char *buf
, *token
, *next
, *p
, *arg
;
671 fputs(_("Couldn't allocate memory to parse quota "
672 "options!\n"), stderr
);
676 for (token
= buf
; token
&& *token
; token
= next
) {
677 p
= strchr(token
, ',');
684 if (strcmp(token
, "usrquota") == 0) {
685 usrquota
= QOPT_ENABLE
;
686 } else if (strcmp(token
, "^usrquota") == 0) {
687 usrquota
= QOPT_DISABLE
;
688 } else if (strcmp(token
, "grpquota") == 0) {
689 grpquota
= QOPT_ENABLE
;
690 } else if (strcmp(token
, "^grpquota") == 0) {
691 grpquota
= QOPT_DISABLE
;
693 fputs(_("\nBad quota options specified.\n\n"
694 "Following valid quota options are available "
695 "(pass by separating with comma):\n"
708 static void parse_e2label_options(int argc
, char ** argv
)
710 if ((argc
< 2) || (argc
> 3)) {
711 fputs(_("Usage: e2label device [newlabel]\n"), stderr
);
714 io_options
= strchr(argv
[1], '?');
717 device_name
= blkid_get_devname(NULL
, argv
[1], NULL
);
719 com_err("e2label", 0, _("Unable to resolve '%s'"),
723 open_flag
= EXT2_FLAG_JOURNAL_DEV_OK
;
725 open_flag
|= EXT2_FLAG_RW
;
732 static time_t parse_time(char *str
)
736 if (strcmp(str
, "now") == 0) {
739 memset(&ts
, 0, sizeof(ts
));
741 strptime(str
, "%Y%m%d%H%M%S", &ts
);
743 sscanf(str
, "%4d%2d%2d%2d%2d%2d", &ts
.tm_year
, &ts
.tm_mon
,
744 &ts
.tm_mday
, &ts
.tm_hour
, &ts
.tm_min
, &ts
.tm_sec
);
747 if (ts
.tm_year
< 0 || ts
.tm_mon
< 0 || ts
.tm_mon
> 11 ||
748 ts
.tm_mday
< 0 || ts
.tm_mday
> 31 || ts
.tm_hour
> 23 ||
749 ts
.tm_min
> 59 || ts
.tm_sec
> 61)
752 if (ts
.tm_mday
== 0) {
753 com_err(program_name
, 0,
754 _("Couldn't parse date/time specifier: %s"),
759 return (mktime(&ts
));
762 static void parse_tune2fs_options(int argc
, char **argv
)
771 printf("tune2fs %s (%s)\n", E2FSPROGS_VERSION
, E2FSPROGS_DATE
);
772 while ((c
= getopt(argc
, argv
, "c:e:fg:i:jlm:o:r:s:u:C:E:I:J:L:M:O:Q:T:U:")) != EOF
)
775 max_mount_count
= strtol(optarg
, &tmp
, 0);
776 if (*tmp
|| max_mount_count
> 16000) {
777 com_err(program_name
, 0,
778 _("bad mounts count - %s"),
782 if (max_mount_count
== 0)
783 max_mount_count
= -1;
785 open_flag
= EXT2_FLAG_RW
;
788 mount_count
= strtoul(optarg
, &tmp
, 0);
789 if (*tmp
|| mount_count
> 16000) {
790 com_err(program_name
, 0,
791 _("bad mounts count - %s"),
796 open_flag
= EXT2_FLAG_RW
;
799 if (strcmp(optarg
, "continue") == 0)
800 errors
= EXT2_ERRORS_CONTINUE
;
801 else if (strcmp(optarg
, "remount-ro") == 0)
802 errors
= EXT2_ERRORS_RO
;
803 else if (strcmp(optarg
, "panic") == 0)
804 errors
= EXT2_ERRORS_PANIC
;
806 com_err(program_name
, 0,
807 _("bad error behavior - %s"),
812 open_flag
= EXT2_FLAG_RW
;
815 extended_cmd
= optarg
;
816 open_flag
|= EXT2_FLAG_RW
;
818 case 'f': /* Force */
822 resgid
= strtoul(optarg
, &tmp
, 0);
824 gr
= getgrnam(optarg
);
833 com_err(program_name
, 0,
834 _("bad gid/group name - %s"),
839 open_flag
= EXT2_FLAG_RW
;
842 interval
= strtoul(optarg
, &tmp
, 0);
855 case 'M': /* months! */
856 interval
*= 86400 * 30;
860 case 'W': /* weeks */
861 interval
*= 86400 * 7;
866 com_err(program_name
, 0,
867 _("bad interval - %s"), optarg
);
871 open_flag
= EXT2_FLAG_RW
;
876 open_flag
= EXT2_FLAG_RW
;
879 parse_journal_opts(optarg
);
880 open_flag
= EXT2_FLAG_RW
;
888 open_flag
|= EXT2_FLAG_RW
|
889 EXT2_FLAG_JOURNAL_DEV_OK
;
892 reserved_ratio
= strtod(optarg
, &tmp
);
893 if (*tmp
|| reserved_ratio
> 50 ||
894 reserved_ratio
< 0) {
895 com_err(program_name
, 0,
896 _("bad reserved block ratio - %s"),
901 open_flag
= EXT2_FLAG_RW
;
904 new_last_mounted
= optarg
;
906 open_flag
= EXT2_FLAG_RW
;
910 com_err(program_name
, 0,
911 _("-o may only be specified once"));
914 mntopts_cmd
= optarg
;
915 open_flag
= EXT2_FLAG_RW
;
920 com_err(program_name
, 0,
921 _("-O may only be specified once"));
924 features_cmd
= optarg
;
925 open_flag
= EXT2_FLAG_RW
;
929 parse_quota_opts(optarg
);
930 open_flag
= EXT2_FLAG_RW
;
933 reserved_blocks
= strtoul(optarg
, &tmp
, 0);
935 com_err(program_name
, 0,
936 _("bad reserved blocks count - %s"),
941 open_flag
= EXT2_FLAG_RW
;
943 case 's': /* Deprecated */
944 s_flag
= atoi(optarg
);
945 open_flag
= EXT2_FLAG_RW
;
949 last_check_time
= parse_time(optarg
);
950 open_flag
= EXT2_FLAG_RW
;
953 resuid
= strtoul(optarg
, &tmp
, 0);
955 pw
= getpwnam(optarg
);
964 com_err(program_name
, 0,
965 _("bad uid/user name - %s"),
970 open_flag
= EXT2_FLAG_RW
;
975 open_flag
= EXT2_FLAG_RW
|
976 EXT2_FLAG_JOURNAL_DEV_OK
;
979 new_inode_size
= strtoul(optarg
, &tmp
, 0);
981 com_err(program_name
, 0,
982 _("bad inode size - %s"),
986 if (!((new_inode_size
&
987 (new_inode_size
- 1)) == 0)) {
988 com_err(program_name
, 0,
989 _("Inode size must be a "
994 open_flag
= EXT2_FLAG_RW
;
1000 if (optind
< argc
- 1 || optind
== argc
)
1002 if (!open_flag
&& !l_flag
)
1004 io_options
= strchr(argv
[optind
], '?');
1007 device_name
= blkid_get_devname(NULL
, argv
[optind
], NULL
);
1009 com_err("tune2fs", 0, _("Unable to resolve '%s'"),
1015 #ifdef CONFIG_BUILD_FINDFS
1016 void do_findfs(int argc
, char **argv
)
1021 (strncmp(argv
[1], "LABEL=", 6) && strncmp(argv
[1], "UUID=", 5))) {
1022 fprintf(stderr
, "Usage: findfs LABEL=<label>|UUID=<uuid>\n");
1025 dev
= blkid_get_devname(NULL
, argv
[1], NULL
);
1027 com_err("findfs", 0, _("Unable to resolve '%s'"),
1036 static void parse_extended_opts(ext2_filsys fs
, const char *opts
)
1038 char *buf
, *token
, *next
, *p
, *arg
;
1043 buf
= malloc(len
+1);
1046 _("Couldn't allocate memory to parse options!\n"));
1050 for (token
= buf
; token
&& *token
; token
= next
) {
1051 p
= strchr(token
, ',');
1057 arg
= strchr(token
, '=');
1062 if (!strcmp(token
, "test_fs")) {
1063 fs
->super
->s_flags
|= EXT2_FLAGS_TEST_FILESYS
;
1064 printf("Setting test filesystem flag\n");
1065 ext2fs_mark_super_dirty(fs
);
1066 } else if (!strcmp(token
, "^test_fs")) {
1067 fs
->super
->s_flags
&= ~EXT2_FLAGS_TEST_FILESYS
;
1068 printf("Clearing test filesystem flag\n");
1069 ext2fs_mark_super_dirty(fs
);
1070 } else if (strcmp(token
, "stride") == 0) {
1075 stride
= strtoul(arg
, &p
, 0);
1078 _("Invalid RAID stride: %s\n"),
1084 } else if (strcmp(token
, "stripe-width") == 0 ||
1085 strcmp(token
, "stripe_width") == 0) {
1090 stripe_width
= strtoul(arg
, &p
, 0);
1093 _("Invalid RAID stripe-width: %s\n"),
1098 stripe_width_set
= 1;
1099 } else if (strcmp(token
, "hash_alg") == 0 ||
1100 strcmp(token
, "hash-alg") == 0) {
1105 hash_alg
= e2p_string2hash(arg
);
1108 _("Invalid hash algorithm: %s\n"),
1113 fs
->super
->s_def_hash_version
= hash_alg
;
1114 printf(_("Setting default hash algorithm "
1117 ext2fs_mark_super_dirty(fs
);
1118 } else if (!strcmp(token
, "mount_opts")) {
1123 if (strlen(arg
) >= sizeof(fs
->super
->s_mount_opts
)) {
1125 "Extended mount options too long\n");
1128 ext_mount_opts
= strdup(arg
);
1133 fprintf(stderr
, _("\nBad options specified.\n\n"
1134 "Extended options are separated by commas, "
1135 "and may take an argument which\n"
1136 "\tis set off by an equals ('=') sign.\n\n"
1137 "Valid extended options are:\n"
1138 "\thash_alg=<hash algorithm>\n"
1139 "\tmount_opts=<extended default mount options>\n"
1140 "\tstride=<RAID per-disk chunk size in blocks>\n"
1141 "\tstripe_width=<RAID stride*data disks in blocks>\n"
1151 * Fill in the block bitmap bmap with the information regarding the
1152 * blocks to be moved
1154 static int get_move_bitmaps(ext2_filsys fs
, int new_ino_blks_per_grp
,
1155 ext2fs_block_bitmap bmap
)
1159 ext2_badblocks_list bb_list
= 0;
1160 blk64_t j
, needed_blocks
= 0;
1161 blk64_t start_blk
, end_blk
;
1163 retval
= ext2fs_read_bb_inode(fs
, &bb_list
);
1167 for (i
= 0; i
< fs
->group_desc_count
; i
++) {
1168 start_blk
= ext2fs_inode_table_loc(fs
, i
) +
1169 fs
->inode_blocks_per_group
;
1171 end_blk
= ext2fs_inode_table_loc(fs
, i
) +
1172 new_ino_blks_per_grp
;
1174 for (j
= start_blk
; j
< end_blk
; j
++) {
1175 if (ext2fs_test_block_bitmap2(fs
->block_map
, j
)) {
1177 * IF the block is a bad block we fail
1179 if (ext2fs_badblocks_list_test(bb_list
, j
)) {
1180 ext2fs_badblocks_list_free(bb_list
);
1184 ext2fs_mark_block_bitmap2(bmap
, j
);
1187 * We are going to use this block for
1188 * inode table. So mark them used.
1190 ext2fs_mark_block_bitmap2(fs
->block_map
, j
);
1193 needed_blocks
+= end_blk
- start_blk
;
1196 ext2fs_badblocks_list_free(bb_list
);
1197 if (needed_blocks
> ext2fs_free_blocks_count(fs
->super
))
1203 static int ext2fs_is_meta_block(ext2_filsys fs
, blk_t blk
)
1206 group
= ext2fs_group_of_blk(fs
, blk
);
1207 if (ext2fs_block_bitmap_loc(fs
, group
) == blk
)
1209 if (ext2fs_inode_bitmap_loc(fs
, group
) == blk
)
1214 static int ext2fs_is_block_in_group(ext2_filsys fs
, dgrp_t group
, blk_t blk
)
1216 blk_t start_blk
, end_blk
;
1217 start_blk
= fs
->super
->s_first_data_block
+
1218 EXT2_BLOCKS_PER_GROUP(fs
->super
) * group
;
1220 * We cannot get new block beyond end_blk for for the last block group
1221 * so we can check with EXT2_BLOCKS_PER_GROUP even for last block group
1223 end_blk
= start_blk
+ EXT2_BLOCKS_PER_GROUP(fs
->super
);
1224 if (blk
>= start_blk
&& blk
<= end_blk
)
1229 static int move_block(ext2_filsys fs
, ext2fs_block_bitmap bmap
)
1236 blk64_t blk
, new_blk
, goal
;
1237 struct blk_move
*bmv
;
1239 retval
= ext2fs_get_mem(fs
->blocksize
, &buf
);
1243 for (new_blk
= blk
= fs
->super
->s_first_data_block
;
1244 blk
< ext2fs_blocks_count(fs
->super
); blk
++) {
1245 if (!ext2fs_test_block_bitmap2(bmap
, blk
))
1248 if (ext2fs_is_meta_block(fs
, blk
)) {
1250 * If the block is mapping a fs meta data block
1251 * like group desc/block bitmap/inode bitmap. We
1252 * should find a block in the same group and fix
1253 * the respective fs metadata pointers. Otherwise
1256 group
= ext2fs_group_of_blk(fs
, blk
);
1257 goal
= ext2fs_group_first_block2(fs
, group
);
1263 retval
= ext2fs_new_block2(fs
, goal
, NULL
, &new_blk
);
1267 /* new fs meta data block should be in the same group */
1268 if (meta_data
&& !ext2fs_is_block_in_group(fs
, group
, new_blk
)) {
1273 /* Mark this block as allocated */
1274 ext2fs_mark_block_bitmap2(fs
->block_map
, new_blk
);
1276 /* Add it to block move list */
1277 retval
= ext2fs_get_mem(sizeof(struct blk_move
), &bmv
);
1282 bmv
->new_loc
= new_blk
;
1284 list_add(&(bmv
->list
), &blk_move_list
);
1286 retval
= io_channel_read_blk64(fs
->io
, blk
, 1, buf
);
1290 retval
= io_channel_write_blk64(fs
->io
, new_blk
, 1, buf
);
1296 ext2fs_free_mem(&buf
);
1300 static blk64_t
translate_block(blk64_t blk
)
1302 struct list_head
*entry
;
1303 struct blk_move
*bmv
;
1305 list_for_each(entry
, &blk_move_list
) {
1306 bmv
= list_entry(entry
, struct blk_move
, list
);
1307 if (bmv
->old_loc
== blk
)
1308 return bmv
->new_loc
;
1314 static int process_block(ext2_filsys fs
EXT2FS_ATTR((unused
)),
1316 e2_blkcnt_t blockcnt
EXT2FS_ATTR((unused
)),
1317 blk64_t ref_block
EXT2FS_ATTR((unused
)),
1318 int ref_offset
EXT2FS_ATTR((unused
)),
1323 ext2fs_block_bitmap bmap
= (ext2fs_block_bitmap
) priv_data
;
1325 if (!ext2fs_test_block_bitmap2(bmap
, *block_nr
))
1327 new_blk
= translate_block(*block_nr
);
1329 *block_nr
= new_blk
;
1331 * This will force the ext2fs_write_inode in the iterator
1333 ret
|= BLOCK_CHANGED
;
1339 static int inode_scan_and_fix(ext2_filsys fs
, ext2fs_block_bitmap bmap
)
1341 errcode_t retval
= 0;
1344 char *block_buf
= 0;
1345 struct ext2_inode inode
;
1346 ext2_inode_scan scan
= NULL
;
1348 retval
= ext2fs_get_mem(fs
->blocksize
* 3, &block_buf
);
1352 retval
= ext2fs_open_inode_scan(fs
, 0, &scan
);
1357 retval
= ext2fs_get_next_inode(scan
, &ino
, &inode
);
1364 if (inode
.i_links_count
== 0)
1365 continue; /* inode not in use */
1368 * If we end up modifying the journal inode
1369 * the sb->s_jnl_blocks will differ. But a
1370 * subsequent e2fsck fixes that.
1371 * Do we need to fix this ??
1374 if (ext2fs_file_acl_block(&inode
) &&
1375 ext2fs_test_block_bitmap2(bmap
,
1376 ext2fs_file_acl_block(&inode
))) {
1377 blk
= translate_block(ext2fs_file_acl_block(&inode
));
1381 ext2fs_file_acl_block_set(&inode
, blk
);
1384 * Write the inode to disk so that inode table
1387 retval
= ext2fs_write_inode(fs
, ino
, &inode
);
1392 if (!ext2fs_inode_has_valid_blocks(&inode
))
1395 retval
= ext2fs_block_iterate3(fs
, ino
, 0, block_buf
,
1396 process_block
, bmap
);
1403 ext2fs_free_mem(&block_buf
);
1409 * We need to scan for inode and block bitmaps that may need to be
1410 * moved. This can take place if the filesystem was formatted for
1411 * RAID arrays using the mke2fs's extended option "stride".
1413 static int group_desc_scan_and_fix(ext2_filsys fs
, ext2fs_block_bitmap bmap
)
1418 for (i
= 0; i
< fs
->group_desc_count
; i
++) {
1419 blk
= ext2fs_block_bitmap_loc(fs
, i
);
1420 if (ext2fs_test_block_bitmap2(bmap
, blk
)) {
1421 new_blk
= translate_block(blk
);
1424 ext2fs_block_bitmap_loc_set(fs
, i
, new_blk
);
1427 blk
= ext2fs_inode_bitmap_loc(fs
, i
);
1428 if (ext2fs_test_block_bitmap2(bmap
, blk
)) {
1429 new_blk
= translate_block(blk
);
1432 ext2fs_inode_bitmap_loc_set(fs
, i
, new_blk
);
1438 static int expand_inode_table(ext2_filsys fs
, unsigned long new_ino_size
)
1443 int new_ino_blks_per_grp
;
1445 char *old_itable
= NULL
, *new_itable
= NULL
;
1446 char *tmp_old_itable
= NULL
, *tmp_new_itable
= NULL
;
1447 unsigned long old_ino_size
;
1448 int old_itable_size
, new_itable_size
;
1450 old_itable_size
= fs
->inode_blocks_per_group
* fs
->blocksize
;
1451 old_ino_size
= EXT2_INODE_SIZE(fs
->super
);
1453 new_ino_blks_per_grp
= ext2fs_div_ceil(
1454 EXT2_INODES_PER_GROUP(fs
->super
) *
1458 new_itable_size
= new_ino_blks_per_grp
* fs
->blocksize
;
1460 retval
= ext2fs_get_mem(old_itable_size
, &old_itable
);
1464 retval
= ext2fs_get_mem(new_itable_size
, &new_itable
);
1468 tmp_old_itable
= old_itable
;
1469 tmp_new_itable
= new_itable
;
1471 for (i
= 0; i
< fs
->group_desc_count
; i
++) {
1472 blk
= ext2fs_inode_table_loc(fs
, i
);
1473 retval
= io_channel_read_blk64(fs
->io
, blk
,
1474 fs
->inode_blocks_per_group
, old_itable
);
1478 for (j
= 0; j
< EXT2_INODES_PER_GROUP(fs
->super
); j
++) {
1479 memcpy(new_itable
, old_itable
, old_ino_size
);
1481 memset(new_itable
+old_ino_size
, 0,
1482 new_ino_size
- old_ino_size
);
1484 new_itable
+= new_ino_size
;
1485 old_itable
+= old_ino_size
;
1488 /* reset the pointer */
1489 old_itable
= tmp_old_itable
;
1490 new_itable
= tmp_new_itable
;
1492 retval
= io_channel_write_blk64(fs
->io
, blk
,
1493 new_ino_blks_per_grp
, new_itable
);
1498 /* Update the meta data */
1499 fs
->inode_blocks_per_group
= new_ino_blks_per_grp
;
1500 fs
->super
->s_inode_size
= new_ino_size
;
1504 ext2fs_free_mem(&old_itable
);
1507 ext2fs_free_mem(&new_itable
);
1512 static errcode_t
ext2fs_calculate_summary_stats(ext2_filsys fs
)
1516 unsigned int group
= 0;
1517 unsigned int count
= 0;
1522 * First calculate the block statistics
1524 for (blk
= fs
->super
->s_first_data_block
;
1525 blk
< ext2fs_blocks_count(fs
->super
); blk
++) {
1526 if (!ext2fs_fast_test_block_bitmap2(fs
->block_map
, blk
)) {
1531 if ((count
== fs
->super
->s_blocks_per_group
) ||
1532 (blk
== ext2fs_blocks_count(fs
->super
)-1)) {
1533 ext2fs_bg_free_blocks_count_set(fs
, group
++,
1539 total_free
= EXT2FS_C2B(fs
, total_free
);
1540 ext2fs_free_blocks_count_set(fs
->super
, total_free
);
1543 * Next, calculate the inode statistics
1550 /* Protect loop from wrap-around if s_inodes_count maxed */
1551 for (ino
= 1; ino
<= fs
->super
->s_inodes_count
&& ino
> 0; ino
++) {
1552 if (!ext2fs_fast_test_inode_bitmap2(fs
->inode_map
, ino
)) {
1557 if ((count
== fs
->super
->s_inodes_per_group
) ||
1558 (ino
== fs
->super
->s_inodes_count
)) {
1559 ext2fs_bg_free_inodes_count_set(fs
, group
++,
1565 fs
->super
->s_free_inodes_count
= total_free
;
1566 ext2fs_mark_super_dirty(fs
);
1570 #define list_for_each_safe(pos, pnext, head) \
1571 for (pos = (head)->next, pnext = pos->next; pos != (head); \
1572 pos = pnext, pnext = pos->next)
1574 static void free_blk_move_list(void)
1576 struct list_head
*entry
, *tmp
;
1577 struct blk_move
*bmv
;
1579 list_for_each_safe(entry
, tmp
, &blk_move_list
) {
1580 bmv
= list_entry(entry
, struct blk_move
, list
);
1582 ext2fs_free_mem(&bmv
);
1587 static int resize_inode(ext2_filsys fs
, unsigned long new_size
)
1590 int new_ino_blks_per_grp
;
1591 ext2fs_block_bitmap bmap
;
1593 retval
= ext2fs_read_inode_bitmap(fs
);
1595 fputs(_("Failed to read inode bitmap\n"), stderr
);
1598 retval
= ext2fs_read_block_bitmap(fs
);
1600 fputs(_("Failed to read blockbitmap\n"), stderr
);
1603 INIT_LIST_HEAD(&blk_move_list
);
1606 new_ino_blks_per_grp
= ext2fs_div_ceil(
1607 EXT2_INODES_PER_GROUP(fs
->super
)*
1611 /* We may change the file system.
1612 * Mark the file system as invalid so that
1613 * the user is prompted to run fsck.
1615 fs
->super
->s_state
&= ~EXT2_VALID_FS
;
1617 retval
= ext2fs_allocate_block_bitmap(fs
, _("blocks to be moved"),
1620 fputs(_("Failed to allocate block bitmap when "
1621 "increasing inode size\n"), stderr
);
1624 retval
= get_move_bitmaps(fs
, new_ino_blks_per_grp
, bmap
);
1626 fputs(_("Not enough space to increase inode size \n"), stderr
);
1629 retval
= move_block(fs
, bmap
);
1631 fputs(_("Failed to relocate blocks during inode resize \n"),
1635 retval
= inode_scan_and_fix(fs
, bmap
);
1639 retval
= group_desc_scan_and_fix(fs
, bmap
);
1643 retval
= expand_inode_table(fs
, new_size
);
1647 ext2fs_calculate_summary_stats(fs
);
1649 fs
->super
->s_state
|= EXT2_VALID_FS
;
1650 /* mark super block and block bitmap as dirty */
1651 ext2fs_mark_super_dirty(fs
);
1652 ext2fs_mark_bb_dirty(fs
);
1655 free_blk_move_list();
1656 ext2fs_free_block_bitmap(bmap
);
1661 free_blk_move_list();
1662 ext2fs_free_block_bitmap(bmap
);
1663 fputs(_("Error in resizing the inode size.\n"
1664 "Run e2undo to undo the "
1665 "file system changes. \n"), stderr
);
1670 static int tune2fs_setup_tdb(const char *name
, io_manager
*io_ptr
)
1672 errcode_t retval
= 0;
1673 const char *tdb_dir
;
1675 char *dev_name
, *tmp_name
;
1679 * Configuration via a conf file would be
1682 profile_get_string(profile
, "scratch_files",
1686 tmp_name
= strdup(name
);
1689 com_err(program_name
, ENOMEM
,
1690 _("Couldn't allocate memory for tdb filename\n"));
1693 dev_name
= basename(tmp_name
);
1695 tdb_dir
= getenv("E2FSPROGS_UNDO_DIR");
1697 tdb_dir
= "/var/lib/e2fsprogs";
1699 if (!strcmp(tdb_dir
, "none") || (tdb_dir
[0] == 0) ||
1700 access(tdb_dir
, W_OK
))
1703 tdb_file
= malloc(strlen(tdb_dir
) + 9 + strlen(dev_name
) + 7 + 1);
1706 sprintf(tdb_file
, "%s/tune2fs-%s.e2undo", tdb_dir
, dev_name
);
1708 if (!access(tdb_file
, F_OK
)) {
1709 if (unlink(tdb_file
) < 0) {
1711 com_err(program_name
, retval
,
1712 _("while trying to delete %s"),
1719 set_undo_io_backing_manager(*io_ptr
);
1720 *io_ptr
= undo_io_manager
;
1721 set_undo_io_backup_file(tdb_file
);
1722 printf(_("To undo the tune2fs operation please run "
1723 "the command\n e2undo %s %s\n\n"),
1730 int main(int argc
, char **argv
)
1734 struct ext2_super_block
*sb
;
1735 io_manager io_ptr
, io_ptr_orig
= NULL
;
1738 setlocale(LC_MESSAGES
, "");
1739 setlocale(LC_CTYPE
, "");
1740 bindtextdomain(NLS_CAT_NAME
, LOCALEDIR
);
1741 textdomain(NLS_CAT_NAME
);
1744 program_name
= *argv
;
1745 add_error_table(&et_ext2_error_table
);
1747 #ifdef CONFIG_BUILD_FINDFS
1748 if (strcmp(get_progname(argv
[0]), "findfs") == 0)
1749 do_findfs(argc
, argv
);
1751 if (strcmp(get_progname(argv
[0]), "e2label") == 0)
1752 parse_e2label_options(argc
, argv
);
1754 parse_tune2fs_options(argc
, argv
);
1756 #ifdef CONFIG_TESTIO_DEBUG
1757 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_DEBUG")) {
1758 io_ptr
= test_io_manager
;
1759 test_io_backing_manager
= unix_io_manager
;
1762 io_ptr
= unix_io_manager
;
1765 retval
= ext2fs_open2(device_name
, io_options
, open_flag
,
1768 com_err(program_name
, retval
,
1769 _("while trying to open %s"),
1772 _("Couldn't find valid filesystem superblock.\n"));
1776 if (I_flag
&& !io_ptr_orig
) {
1778 * Check the inode size is right so we can issue an
1779 * error message and bail before setting up the tdb
1782 if (new_inode_size
== EXT2_INODE_SIZE(fs
->super
)) {
1783 fprintf(stderr
, _("The inode size is already %lu\n"),
1787 if (new_inode_size
< EXT2_INODE_SIZE(fs
->super
)) {
1788 fprintf(stderr
, _("Shrinking the inode size is "
1789 "not supported\n"));
1794 * If inode resize is requested use the
1797 io_ptr_orig
= io_ptr
;
1798 retval
= tune2fs_setup_tdb(device_name
, &io_ptr
);
1801 if (io_ptr
!= io_ptr_orig
) {
1808 fs
->flags
&= ~EXT2_FLAG_MASTER_SB_ONLY
;
1811 /* For e2label emulation */
1812 printf("%.*s\n", (int) sizeof(sb
->s_volume_name
),
1814 remove_error_table(&et_ext2_error_table
);
1818 retval
= ext2fs_check_if_mounted(device_name
, &mount_flags
);
1820 com_err("ext2fs_check_if_mount", retval
,
1821 _("while determining whether %s is mounted."),
1825 /* Normally we only need to write out the superblock */
1826 fs
->flags
|= EXT2_FLAG_SUPER_ONLY
;
1829 sb
->s_max_mnt_count
= max_mount_count
;
1830 ext2fs_mark_super_dirty(fs
);
1831 printf(_("Setting maximal mount count to %d\n"),
1835 sb
->s_mnt_count
= mount_count
;
1836 ext2fs_mark_super_dirty(fs
);
1837 printf(_("Setting current mount count to %d\n"), mount_count
);
1840 sb
->s_errors
= errors
;
1841 ext2fs_mark_super_dirty(fs
);
1842 printf(_("Setting error behavior to %d\n"), errors
);
1845 sb
->s_def_resgid
= resgid
;
1846 ext2fs_mark_super_dirty(fs
);
1847 printf(_("Setting reserved blocks gid to %lu\n"), resgid
);
1850 if (interval
>= (1ULL << 32)) {
1851 com_err(program_name
, 0,
1852 _("interval between checks is too big (%lu)"),
1856 sb
->s_checkinterval
= interval
;
1857 ext2fs_mark_super_dirty(fs
);
1858 printf(_("Setting interval between checks to %lu seconds\n"),
1862 ext2fs_r_blocks_count_set(sb
, reserved_ratio
*
1863 ext2fs_blocks_count(sb
) / 100.0);
1864 ext2fs_mark_super_dirty(fs
);
1865 printf (_("Setting reserved blocks percentage to %g%% (%llu blocks)\n"),
1866 reserved_ratio
, ext2fs_r_blocks_count(sb
));
1869 if (reserved_blocks
> ext2fs_blocks_count(sb
)/2) {
1870 com_err(program_name
, 0,
1871 _("reserved blocks count is too big (%llu)"),
1875 ext2fs_r_blocks_count_set(sb
, reserved_blocks
);
1876 ext2fs_mark_super_dirty(fs
);
1877 printf(_("Setting reserved blocks count to %llu\n"),
1881 if (sb
->s_feature_ro_compat
&
1882 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
)
1883 fputs(_("\nThe filesystem already has sparse "
1884 "superblocks.\n"), stderr
);
1886 sb
->s_feature_ro_compat
|=
1887 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
;
1888 sb
->s_state
&= ~EXT2_VALID_FS
;
1889 ext2fs_mark_super_dirty(fs
);
1890 printf(_("\nSparse superblock flag set. %s"),
1895 fputs(_("\nClearing the sparse superflag not supported.\n"),
1900 sb
->s_lastcheck
= last_check_time
;
1901 ext2fs_mark_super_dirty(fs
);
1902 printf(_("Setting time filesystem last checked to %s\n"),
1903 ctime(&last_check_time
));
1906 sb
->s_def_resuid
= resuid
;
1907 ext2fs_mark_super_dirty(fs
);
1908 printf(_("Setting reserved blocks uid to %lu\n"), resuid
);
1911 if (strlen(new_label
) > sizeof(sb
->s_volume_name
))
1912 fputs(_("Warning: label too long, truncating.\n"),
1914 memset(sb
->s_volume_name
, 0, sizeof(sb
->s_volume_name
));
1915 strncpy(sb
->s_volume_name
, new_label
,
1916 sizeof(sb
->s_volume_name
));
1917 ext2fs_mark_super_dirty(fs
);
1920 memset(sb
->s_last_mounted
, 0, sizeof(sb
->s_last_mounted
));
1921 strncpy(sb
->s_last_mounted
, new_last_mounted
,
1922 sizeof(sb
->s_last_mounted
));
1923 ext2fs_mark_super_dirty(fs
);
1926 update_mntopts(fs
, mntopts_cmd
);
1928 update_feature_set(fs
, features_cmd
);
1930 parse_extended_opts(fs
, extended_cmd
);
1931 if (journal_size
|| journal_device
)
1935 if (mount_flags
& EXT2_MF_MOUNTED
) {
1936 fputs(_("The quota feature may only be changed when "
1937 "the filesystem is unmounted.\n"), stderr
);
1940 handle_quota_options(fs
);
1947 if (sb
->s_feature_ro_compat
&
1948 EXT4_FEATURE_RO_COMPAT_GDT_CSUM
) {
1950 * Determine if the block group checksums are
1951 * correct so we know whether or not to set
1954 for (i
= 0; i
< fs
->group_desc_count
; i
++)
1955 if (!ext2fs_group_desc_csum_verify(fs
, i
))
1957 if (i
>= fs
->group_desc_count
)
1960 if ((strcasecmp(new_UUID
, "null") == 0) ||
1961 (strcasecmp(new_UUID
, "clear") == 0)) {
1962 uuid_clear(sb
->s_uuid
);
1963 } else if (strcasecmp(new_UUID
, "time") == 0) {
1964 uuid_generate_time(sb
->s_uuid
);
1965 } else if (strcasecmp(new_UUID
, "random") == 0) {
1966 uuid_generate(sb
->s_uuid
);
1967 } else if (uuid_parse(new_UUID
, sb
->s_uuid
)) {
1968 com_err(program_name
, 0, _("Invalid UUID format\n"));
1972 for (i
= 0; i
< fs
->group_desc_count
; i
++)
1973 ext2fs_group_desc_csum_set(fs
, i
);
1974 fs
->flags
&= ~EXT2_FLAG_SUPER_ONLY
;
1976 ext2fs_mark_super_dirty(fs
);
1979 if (mount_flags
& EXT2_MF_MOUNTED
) {
1980 fputs(_("The inode size may only be "
1981 "changed when the filesystem is "
1982 "unmounted.\n"), stderr
);
1985 if (fs
->super
->s_feature_incompat
&
1986 EXT4_FEATURE_INCOMPAT_FLEX_BG
) {
1987 fputs(_("Changing the inode size not supported for "
1988 "filesystems with the flex_bg\n"
1989 "feature enabled.\n"),
1994 * We want to update group descriptor also
1995 * with the new free inode count
1997 fs
->flags
&= ~EXT2_FLAG_SUPER_ONLY
;
1998 if (resize_inode(fs
, new_inode_size
) == 0) {
1999 printf(_("Setting inode size %lu\n"),
2002 printf(_("Failed to change inode size\n"));
2010 sb
->s_raid_stride
= stride
;
2011 ext2fs_mark_super_dirty(fs
);
2012 printf(_("Setting stride size to %d\n"), stride
);
2014 if (stripe_width_set
) {
2015 sb
->s_raid_stripe_width
= stripe_width
;
2016 ext2fs_mark_super_dirty(fs
);
2017 printf(_("Setting stripe width to %d\n"), stripe_width
);
2019 if (ext_mount_opts
) {
2020 strncpy((char *)(fs
->super
->s_mount_opts
), ext_mount_opts
,
2021 sizeof(fs
->super
->s_mount_opts
));
2022 fs
->super
->s_mount_opts
[sizeof(fs
->super
->s_mount_opts
)-1] = 0;
2023 ext2fs_mark_super_dirty(fs
);
2024 printf(_("Setting extended default mount options to '%s'\n"),
2026 free(ext_mount_opts
);
2029 remove_error_table(&et_ext2_error_table
);
2030 return (ext2fs_close(fs
) ? 1 : 0);