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 "ext2fs/kernel-jbd.h"
58 #include "et/com_err.h"
59 #include "support/plausible.h"
60 #include "support/quotaio.h"
61 #include "uuid/uuid.h"
64 #include "blkid/blkid.h"
66 #include "../version.h"
67 #include "support/nls-enable.h"
69 #define QOPT_ENABLE (1)
70 #define QOPT_DISABLE (-1)
72 extern int ask_yn(const char *string, int def);
74 const char *program_name = "tune2fs";
76 char *new_label, *new_last_mounted, *new_UUID;
78 static int c_flag, C_flag, e_flag, f_flag, g_flag, i_flag, l_flag, L_flag;
79 static int m_flag, M_flag, Q_flag, r_flag, s_flag = -1, u_flag, U_flag, T_flag;
82 static time_t last_check_time;
83 static int print_label;
84 static int max_mount_count, mount_count, mount_flags;
85 static unsigned long interval;
86 static blk64_t reserved_blocks;
87 static double reserved_ratio;
88 static unsigned long resgid, resuid;
89 static unsigned short errors;
91 static char *features_cmd;
92 static char *mntopts_cmd;
93 static int stride, stripe_width;
94 static int stride_set, stripe_width_set;
95 static char *extended_cmd;
96 static unsigned long new_inode_size;
97 static char *ext_mount_opts;
98 static int quota_enable[MAXQUOTAS];
99 static int rewrite_checksums;
100 static int feature_64bit;
101 static int fsck_requested;
102 static char *undo_file;
104 int journal_size, journal_flags;
105 char *journal_device;
106 static blk64_t journal_location = ~0LL;
108 static struct list_head blk_move_list;
111 struct list_head list;
117 static const char *please_fsck = N_("Please run e2fsck on the filesystem.\n");
118 static const char *please_dir_fsck =
119 N_("Please run e2fsck -D on the filesystem.\n");
121 #ifdef CONFIG_BUILD_FINDFS
122 void do_findfs(int argc, char **argv);
125 static void usage(void)
128 _("Usage: %s [-c max_mounts_count] [-e errors_behavior] "
130 "\t[-i interval[d|m|w]] [-j] [-J journal_options] [-l]\n"
131 "\t[-m reserved_blocks_percent] [-o [^]mount_options[,...]]\n"
132 "\t[-p mmp_update_interval] [-r reserved_blocks_count] "
134 "\t[-C mount_count] [-L volume_label] [-M last_mounted_dir]\n"
135 "\t[-O [^]feature[,...]] [-Q quota_options]\n"
136 "\t[-E extended-option[,...]] [-T last_check_time] "
137 "[-U UUID]\n\t[-I new_inode_size] [-z undo_file] device\n"),
142 static __u32 ok_features[3] = {
144 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
145 EXT2_FEATURE_COMPAT_DIR_INDEX,
147 EXT2_FEATURE_INCOMPAT_FILETYPE |
148 EXT3_FEATURE_INCOMPAT_EXTENTS |
149 EXT4_FEATURE_INCOMPAT_FLEX_BG |
150 EXT4_FEATURE_INCOMPAT_MMP |
151 EXT4_FEATURE_INCOMPAT_64BIT |
152 EXT4_FEATURE_INCOMPAT_ENCRYPT,
154 EXT2_FEATURE_RO_COMPAT_LARGE_FILE |
155 EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
156 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
157 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
158 EXT4_FEATURE_RO_COMPAT_GDT_CSUM |
159 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER |
160 EXT4_FEATURE_RO_COMPAT_QUOTA |
161 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM |
162 EXT4_FEATURE_RO_COMPAT_READONLY |
163 EXT4_FEATURE_RO_COMPAT_PROJECT
166 static __u32 clear_ok_features[3] = {
168 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
169 EXT2_FEATURE_COMPAT_RESIZE_INODE |
170 EXT2_FEATURE_COMPAT_DIR_INDEX,
172 EXT2_FEATURE_INCOMPAT_FILETYPE |
173 EXT4_FEATURE_INCOMPAT_FLEX_BG |
174 EXT4_FEATURE_INCOMPAT_MMP |
175 EXT4_FEATURE_INCOMPAT_64BIT,
177 EXT2_FEATURE_RO_COMPAT_LARGE_FILE |
178 EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
179 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
180 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
181 EXT4_FEATURE_RO_COMPAT_GDT_CSUM |
182 EXT4_FEATURE_RO_COMPAT_QUOTA |
183 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM |
184 EXT4_FEATURE_RO_COMPAT_READONLY
188 * Try to get journal super block if any
190 static int get_journal_sb(ext2_filsys jfs, char buf[SUPERBLOCK_SIZE])
193 journal_superblock_t *jsb;
195 if (!ext2fs_has_feature_journal_dev(jfs->super)) {
196 return EXT2_ET_UNSUPP_FEATURE;
199 /* Get the journal superblock */
200 if ((retval = io_channel_read_blk64(jfs->io,
201 ext2fs_journal_sb_start(jfs->blocksize), -SUPERBLOCK_SIZE, buf))) {
202 com_err(program_name, retval, "%s",
203 _("while reading journal superblock"));
207 jsb = (journal_superblock_t *) buf;
208 if ((jsb->s_header.h_magic != (unsigned)ntohl(JFS_MAGIC_NUMBER)) ||
209 (jsb->s_header.h_blocktype != (unsigned)ntohl(JFS_SUPERBLOCK_V2))) {
210 fputs(_("Journal superblock not found!\n"), stderr);
211 return EXT2_ET_BAD_MAGIC;
217 static __u8 *journal_user(__u8 uuid[UUID_SIZE], __u8 s_users[JFS_USERS_SIZE],
221 for (i = 0; i < nr_users; i++) {
222 if (memcmp(uuid, &s_users[i * UUID_SIZE], UUID_SIZE) == 0)
223 return &s_users[i * UUID_SIZE];
230 * Remove an external journal from the filesystem
232 static int remove_journal_device(ext2_filsys fs)
236 char buf[SUPERBLOCK_SIZE] __attribute__ ((aligned(8)));
237 journal_superblock_t *jsb;
240 int commit_remove_journal = 0;
244 commit_remove_journal = 1; /* force removal even if error */
246 uuid_unparse(fs->super->s_journal_uuid, buf);
247 journal_path = blkid_get_devname(NULL, "UUID", buf);
251 ext2fs_find_block_device(fs->super->s_journal_dev);
253 goto no_valid_journal;
256 #ifdef CONFIG_TESTIO_DEBUG
257 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
258 io_ptr = test_io_manager;
259 test_io_backing_manager = unix_io_manager;
262 io_ptr = unix_io_manager;
263 retval = ext2fs_open(journal_path, EXT2_FLAG_RW|
264 EXT2_FLAG_JOURNAL_DEV_OK, 0,
265 fs->blocksize, io_ptr, &jfs);
267 com_err(program_name, retval, "%s",
268 _("while trying to open external journal"));
269 goto no_valid_journal;
272 if ((retval = get_journal_sb(jfs, buf))) {
273 if (retval == EXT2_ET_UNSUPP_FEATURE)
274 fprintf(stderr, _("%s is not a journal device.\n"),
276 goto no_valid_journal;
279 jsb = (journal_superblock_t *) buf;
280 /* Find the filesystem UUID */
281 nr_users = ntohl(jsb->s_nr_users);
283 if (!journal_user(fs->super->s_uuid, jsb->s_users, nr_users)) {
284 fputs(_("Filesystem's UUID not found on journal device.\n"),
286 commit_remove_journal = 1;
287 goto no_valid_journal;
290 for (i = 0; i < nr_users; i++)
291 memcpy(&jsb->s_users[i * 16], &jsb->s_users[(i + 1) * 16], 16);
292 jsb->s_nr_users = htonl(nr_users);
294 /* Write back the journal superblock */
295 retval = io_channel_write_blk64(jfs->io,
296 ext2fs_journal_sb_start(fs->blocksize),
297 -SUPERBLOCK_SIZE, buf);
299 com_err(program_name, retval,
300 "while writing journal superblock.");
301 goto no_valid_journal;
304 commit_remove_journal = 1;
307 if (commit_remove_journal == 0) {
308 fputs(_("Cannot locate journal device. It was NOT removed\n"
309 "Use -f option to remove missing journal device.\n"),
313 fs->super->s_journal_dev = 0;
314 memset(fs->super->s_jnl_blocks, 0, sizeof(fs->super->s_jnl_blocks));
315 uuid_clear(fs->super->s_journal_uuid);
316 ext2fs_mark_super_dirty(fs);
317 fputs(_("Journal removed\n"), stdout);
323 /* Helper function for remove_journal_inode */
324 static int release_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
325 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
326 blk64_t ref_block EXT2FS_ATTR((unused)),
327 int ref_offset EXT2FS_ATTR((unused)),
328 void *private EXT2FS_ATTR((unused)))
334 ext2fs_unmark_block_bitmap2(fs->block_map, block);
335 group = ext2fs_group_of_blk2(fs, block);
336 ext2fs_bg_free_blocks_count_set(fs, group, ext2fs_bg_free_blocks_count(fs, group) + 1);
337 ext2fs_group_desc_csum_set(fs, group);
338 ext2fs_free_blocks_count_add(fs->super, EXT2FS_CLUSTER_RATIO(fs));
343 * Remove the journal inode from the filesystem
345 static errcode_t remove_journal_inode(ext2_filsys fs)
347 struct ext2_inode inode;
349 ino_t ino = fs->super->s_journal_inum;
351 retval = ext2fs_read_inode(fs, ino, &inode);
353 com_err(program_name, retval, "%s",
354 _("while reading journal inode"));
357 if (ino == EXT2_JOURNAL_INO) {
358 retval = ext2fs_read_bitmaps(fs);
360 com_err(program_name, retval, "%s",
361 _("while reading bitmaps"));
364 retval = ext2fs_block_iterate3(fs, ino,
365 BLOCK_FLAG_READ_ONLY, NULL,
366 release_blocks_proc, NULL);
368 com_err(program_name, retval, "%s",
369 _("while clearing journal inode"));
372 memset(&inode, 0, sizeof(inode));
373 ext2fs_mark_bb_dirty(fs);
374 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
376 inode.i_flags &= ~EXT2_IMMUTABLE_FL;
377 retval = ext2fs_write_inode(fs, ino, &inode);
379 com_err(program_name, retval, "%s",
380 _("while writing journal inode"));
383 fs->super->s_journal_inum = 0;
384 memset(fs->super->s_jnl_blocks, 0, sizeof(fs->super->s_jnl_blocks));
385 ext2fs_mark_super_dirty(fs);
391 * Update the default mount options
393 static int update_mntopts(ext2_filsys fs, char *mntopts)
395 struct ext2_super_block *sb = fs->super;
397 if (e2p_edit_mntopts(mntopts, &sb->s_default_mount_opts, ~0)) {
398 fprintf(stderr, _("Invalid mount option set: %s\n"),
402 ext2fs_mark_super_dirty(fs);
407 static int check_fsck_needed(ext2_filsys fs)
409 if (fs->super->s_state & EXT2_VALID_FS)
411 printf("\n%s\n", _(please_fsck));
412 if (mount_flags & EXT2_MF_READONLY)
413 printf("%s", _("(and reboot afterwards!)\n"));
417 static void request_dir_fsck_afterwards(ext2_filsys fs)
419 static int requested;
424 fs->super->s_state &= ~EXT2_VALID_FS;
425 printf("\n%s\n", _(please_dir_fsck));
426 if (mount_flags & EXT2_MF_READONLY)
427 printf("%s", _("(and reboot afterwards!)\n"));
430 static void request_fsck_afterwards(ext2_filsys fs)
432 static int requested = 0;
437 fs->super->s_state &= ~EXT2_VALID_FS;
438 printf("\n%s\n", _(please_fsck));
439 if (mount_flags & EXT2_MF_READONLY)
440 printf("%s", _("(and reboot afterwards!)\n"));
443 static void convert_64bit(ext2_filsys fs, int direction)
449 * Is resize2fs going to demand a fsck run? Might as well tell the
452 if (!fsck_requested &&
453 ((fs->super->s_state & EXT2_ERROR_FS) ||
454 !(fs->super->s_state & EXT2_VALID_FS) ||
455 fs->super->s_lastcheck < fs->super->s_mtime))
456 request_fsck_afterwards(fs);
458 fprintf(stderr, _("After running e2fsck, please run `resize2fs %s %s"),
459 direction > 0 ? "-b" : "-s", fs->device_name);
461 fprintf(stderr, _("Please run `resize2fs %s %s"),
462 direction > 0 ? "-b" : "-s", fs->device_name);
465 fprintf(stderr, _(" -z \"%s\""), undo_file);
467 fprintf(stderr, _("' to enable 64-bit mode.\n"));
469 fprintf(stderr, _("' to disable 64-bit mode.\n"));
472 /* Rewrite extents */
473 static errcode_t rewrite_extents(ext2_filsys fs, ext2_ino_t ino,
474 struct ext2_inode *inode)
476 ext2_extent_handle_t handle;
477 struct ext2fs_extent extent;
479 struct ext2_extent_info info;
481 if (!(inode->i_flags & EXT4_EXTENTS_FL) ||
482 !ext2fs_has_feature_metadata_csum(fs->super))
485 errcode = ext2fs_extent_open(fs, ino, &handle);
489 errcode = ext2fs_extent_get(handle, EXT2_EXTENT_ROOT, &extent);
494 errcode = ext2fs_extent_get_info(handle, &info);
499 * If this is the first extent in an extent block that we
500 * haven't visited, rewrite the extent to force the ETB
501 * checksum to be rewritten.
503 if (info.curr_entry == 1 && info.curr_level != 0 &&
504 !(extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT)) {
505 errcode = ext2fs_extent_replace(handle, 0, &extent);
510 /* Skip to the end of a block of leaf nodes */
511 if (extent.e_flags & EXT2_EXTENT_FLAGS_LEAF) {
512 errcode = ext2fs_extent_get(handle,
513 EXT2_EXTENT_LAST_SIB,
519 errcode = ext2fs_extent_get(handle, EXT2_EXTENT_NEXT, &extent);
520 } while (errcode == 0);
523 /* Ok if we run off the end */
524 if (errcode == EXT2_ET_EXTENT_NO_NEXT)
526 ext2fs_extent_free(handle);
531 * Rewrite directory blocks with checksums
533 struct rewrite_dir_context {
540 static int rewrite_dir_block(ext2_filsys fs,
542 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
543 blk64_t ref_block EXT2FS_ATTR((unused)),
544 int ref_offset EXT2FS_ATTR((unused)),
547 struct ext2_dx_countlimit *dcl = NULL;
548 struct rewrite_dir_context *ctx = priv_data;
549 int dcl_offset, changed = 0;
551 ctx->errcode = ext2fs_read_dir_block4(fs, *blocknr, ctx->buf, 0,
556 /* if htree node... */
558 ext2fs_get_dx_countlimit(fs, (struct ext2_dir_entry *)ctx->buf,
561 if (!ext2fs_has_feature_metadata_csum(fs->super)) {
562 /* Ensure limit is the max size */
563 int max_entries = (fs->blocksize - dcl_offset) /
564 sizeof(struct ext2_dx_entry);
565 if (ext2fs_le16_to_cpu(dcl->limit) != max_entries) {
567 dcl->limit = ext2fs_cpu_to_le16(max_entries);
570 /* If htree block is full then rebuild the dir */
571 if (ext2fs_le16_to_cpu(dcl->count) ==
572 ext2fs_le16_to_cpu(dcl->limit)) {
573 request_dir_fsck_afterwards(fs);
577 * Ensure dcl->limit is small enough to leave room for
580 int max_entries = (fs->blocksize - (dcl_offset +
581 sizeof(struct ext2_dx_tail))) /
582 sizeof(struct ext2_dx_entry);
583 if (ext2fs_le16_to_cpu(dcl->limit) != max_entries)
584 dcl->limit = ext2fs_cpu_to_le16(max_entries);
585 /* Always rewrite checksum */
589 unsigned int rec_len, name_size;
590 char *top = ctx->buf + fs->blocksize;
591 struct ext2_dir_entry *de = (struct ext2_dir_entry *)ctx->buf;
592 struct ext2_dir_entry *last_de = NULL, *penultimate_de = NULL;
594 /* Find last and penultimate dirent */
595 while ((char *)de < top) {
596 penultimate_de = last_de;
598 ctx->errcode = ext2fs_get_rec_len(fs, de, &rec_len);
599 if (!ctx->errcode && !rec_len)
600 ctx->errcode = EXT2_ET_DIR_CORRUPTED;
603 de = (struct ext2_dir_entry *)(((char *)de) + rec_len);
605 ctx->errcode = ext2fs_get_rec_len(fs, last_de, &rec_len);
608 name_size = ext2fs_dirent_name_len(last_de);
610 if (!ext2fs_has_feature_metadata_csum(fs->super)) {
613 if (last_de->inode ||
615 rec_len != sizeof(struct ext2_dir_entry_tail))
618 * The last dirent is unused and the right length to
619 * have stored a checksum. Erase it.
621 ctx->errcode = ext2fs_get_rec_len(fs, penultimate_de,
624 ctx->errcode = EXT2_ET_DIR_CORRUPTED;
627 ext2fs_set_rec_len(fs, rec_len +
628 sizeof(struct ext2_dir_entry_tail),
632 unsigned csum_size = sizeof(struct ext2_dir_entry_tail);
633 struct ext2_dir_entry_tail *t;
636 * If the last dirent looks like the tail, just update
639 if (!last_de->inode &&
640 rec_len == csum_size) {
641 t = (struct ext2_dir_entry_tail *)last_de;
642 t->det_reserved_name_len =
643 EXT2_DIR_NAME_LEN_CSUM;
648 name_size = (name_size & ~3) + 4;
649 /* If there's not enough space for the tail, e2fsck */
650 if (rec_len <= (8 + name_size + csum_size)) {
651 request_dir_fsck_afterwards(fs);
654 /* Shorten that last de and insert the tail */
655 ext2fs_set_rec_len(fs, rec_len - csum_size, last_de);
656 t = EXT2_DIRENT_TAIL(ctx->buf, fs->blocksize);
657 ext2fs_initialize_dirent_tail(fs, t);
659 /* Always update checksum */
668 ctx->errcode = ext2fs_write_dir_block4(fs, *blocknr, ctx->buf,
676 static errcode_t rewrite_directory(ext2_filsys fs, ext2_ino_t dir,
677 struct ext2_inode *inode)
680 struct rewrite_dir_context ctx;
682 retval = ext2fs_get_mem(fs->blocksize, &ctx.buf);
686 ctx.is_htree = (inode->i_flags & EXT2_INDEX_FL);
689 retval = ext2fs_block_iterate3(fs, dir, BLOCK_FLAG_READ_ONLY |
690 BLOCK_FLAG_DATA_ONLY,
691 0, rewrite_dir_block, &ctx);
693 ext2fs_free_mem(&ctx.buf);
701 * Forcibly set checksums in all inodes.
703 static void rewrite_inodes(ext2_filsys fs)
705 int length = EXT2_INODE_SIZE(fs->super);
706 struct ext2_inode *inode, *zero;
708 ext2_inode_scan scan;
711 blk64_t file_acl_block;
714 if (fs->super->s_creator_os != EXT2_OS_LINUX)
717 retval = ext2fs_open_inode_scan(fs, 0, &scan);
719 com_err("set_csum", retval, "while opening inode scan");
723 retval = ext2fs_get_mem(length, &inode);
725 com_err("set_csum", retval, "while allocating memory");
729 retval = ext2fs_get_memzero(length, &zero);
731 com_err("set_csum", retval, "while allocating memory");
735 retval = ext2fs_get_mem(fs->blocksize, &ea_buf);
737 com_err("set_csum", retval, "while allocating memory");
742 retval = ext2fs_get_next_inode_full(scan, &ino, inode, length);
744 com_err("set_csum", retval, "while getting next inode");
749 if (ext2fs_test_inode_bitmap2(fs->inode_map, ino)) {
752 if (memcmp(inode, zero, length) != 0) {
753 memset(inode, 0, length);
761 retval = ext2fs_write_inode_full(fs, ino, inode,
764 com_err("set_csum", retval, "while writing "
770 retval = rewrite_extents(fs, ino, inode);
772 com_err("rewrite_extents", retval,
773 "while rewriting extents");
777 if (LINUX_S_ISDIR(inode->i_mode) &&
778 ext2fs_inode_has_valid_blocks2(fs, inode)) {
779 retval = rewrite_directory(fs, ino, inode);
781 com_err("rewrite_directory", retval,
782 "while rewriting directories");
787 file_acl_block = ext2fs_file_acl_block(fs, inode);
790 retval = ext2fs_read_ext_attr3(fs, file_acl_block, ea_buf, ino);
792 com_err("rewrite_eablock", retval,
793 "while rewriting extended attribute");
796 retval = ext2fs_write_ext_attr3(fs, file_acl_block, ea_buf,
799 com_err("rewrite_eablock", retval,
800 "while rewriting extended attribute");
805 ext2fs_free_mem(&zero);
806 ext2fs_free_mem(&inode);
807 ext2fs_free_mem(&ea_buf);
808 ext2fs_close_inode_scan(scan);
811 static void rewrite_metadata_checksums(ext2_filsys fs)
816 fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
817 ext2fs_init_csum_seed(fs);
818 for (i = 0; i < fs->group_desc_count; i++)
819 ext2fs_group_desc_csum_set(fs, i);
820 retval = ext2fs_read_bitmaps(fs);
822 com_err("rewrite_metadata_checksums", retval,
823 "while reading bitmaps");
827 ext2fs_mark_ib_dirty(fs);
828 ext2fs_mark_bb_dirty(fs);
829 ext2fs_mmp_update2(fs, 1);
830 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
831 fs->flags &= ~EXT2_FLAG_IGNORE_CSUM_ERRORS;
832 if (ext2fs_has_feature_metadata_csum(fs->super))
833 fs->super->s_checksum_type = EXT2_CRC32C_CHKSUM;
835 fs->super->s_checksum_type = 0;
836 ext2fs_mark_super_dirty(fs);
839 static void enable_uninit_bg(ext2_filsys fs)
841 struct ext2_group_desc *gd;
844 for (i = 0; i < fs->group_desc_count; i++) {
845 gd = ext2fs_group_desc(fs, fs->group_desc, i);
846 gd->bg_itable_unused = 0;
847 gd->bg_flags = EXT2_BG_INODE_ZEROED;
848 ext2fs_group_desc_csum_set(fs, i);
850 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
853 static errcode_t zero_empty_inodes(ext2_filsys fs)
855 int length = EXT2_INODE_SIZE(fs->super);
856 struct ext2_inode *inode = NULL;
857 ext2_inode_scan scan;
861 retval = ext2fs_open_inode_scan(fs, 0, &scan);
865 retval = ext2fs_get_mem(length, &inode);
870 retval = ext2fs_get_next_inode_full(scan, &ino, inode, length);
875 if (!ext2fs_test_inode_bitmap2(fs->inode_map, ino)) {
876 memset(inode, 0, length);
877 retval = ext2fs_write_inode_full(fs, ino, inode,
885 ext2fs_free_mem(&inode);
886 ext2fs_close_inode_scan(scan);
890 static errcode_t disable_uninit_bg(ext2_filsys fs, __u32 csum_feature_flag)
892 struct ext2_group_desc *gd;
897 /* Load bitmaps to ensure that the uninit ones get written out */
898 fs->super->s_feature_ro_compat |= csum_feature_flag;
899 retval = ext2fs_read_bitmaps(fs);
900 fs->super->s_feature_ro_compat &= ~csum_feature_flag;
902 com_err("disable_uninit_bg", retval,
903 "while reading bitmaps");
904 request_fsck_afterwards(fs);
907 ext2fs_mark_ib_dirty(fs);
908 ext2fs_mark_bb_dirty(fs);
910 /* If we're only turning off uninit_bg, zero the inodes */
911 if (csum_feature_flag == EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
912 retval = zero_empty_inodes(fs);
914 com_err("disable_uninit_bg", retval,
915 "while zeroing unused inodes");
916 request_fsck_afterwards(fs);
921 /* The bbitmap is zeroed; we must mark group metadata blocks in use */
922 for (i = 0; i < fs->group_desc_count; i++) {
923 b = ext2fs_block_bitmap_loc(fs, i);
924 ext2fs_mark_block_bitmap2(fs->block_map, b);
925 b = ext2fs_inode_bitmap_loc(fs, i);
926 ext2fs_mark_block_bitmap2(fs->block_map, b);
928 retval = ext2fs_super_and_bgd_loc2(fs, i, &b, &c, &d, NULL);
929 if (retval == 0 && b)
930 ext2fs_mark_block_bitmap2(fs->block_map, b);
931 if (retval == 0 && c)
932 ext2fs_mark_block_bitmap2(fs->block_map, c);
933 if (retval == 0 && d)
934 ext2fs_mark_block_bitmap2(fs->block_map, d);
936 com_err("disable_uninit_bg", retval,
937 "while initializing block bitmaps");
938 request_fsck_afterwards(fs);
941 gd = ext2fs_group_desc(fs, fs->group_desc, i);
942 gd->bg_itable_unused = 0;
944 ext2fs_group_desc_csum_set(fs, i);
946 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
947 ext2fs_mark_super_dirty(fs);
953 * Update the feature set as provided by the user.
955 static int update_feature_set(ext2_filsys fs, char *features)
957 struct ext2_super_block *sb = fs->super;
958 __u32 old_features[3];
960 unsigned int mask_err;
962 enum quota_type qtype;
964 #define FEATURE_ON(type, mask) (!(old_features[(type)] & (mask)) && \
965 ((&sb->s_feature_compat)[(type)] & (mask)))
966 #define FEATURE_OFF(type, mask) ((old_features[(type)] & (mask)) && \
967 !((&sb->s_feature_compat)[(type)] & (mask)))
968 #define FEATURE_CHANGED(type, mask) ((mask) & \
969 (old_features[(type)] ^ (&sb->s_feature_compat)[(type)]))
971 old_features[E2P_FEATURE_COMPAT] = sb->s_feature_compat;
972 old_features[E2P_FEATURE_INCOMPAT] = sb->s_feature_incompat;
973 old_features[E2P_FEATURE_RO_INCOMPAT] = sb->s_feature_ro_compat;
975 if (e2p_edit_feature2(features, &sb->s_feature_compat,
976 ok_features, clear_ok_features,
977 &type_err, &mask_err)) {
980 _("Invalid filesystem option set: %s\n"),
982 else if (type_err & E2P_FEATURE_NEGATE_FLAG)
983 fprintf(stderr, _("Clearing filesystem feature '%s' "
985 e2p_feature2string(type_err &
986 E2P_FEATURE_TYPE_MASK,
989 fprintf(stderr, _("Setting filesystem feature '%s' "
991 e2p_feature2string(type_err, mask_err));
995 if (FEATURE_OFF(E2P_FEATURE_COMPAT, EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
996 if ((mount_flags & EXT2_MF_MOUNTED) &&
997 !(mount_flags & EXT2_MF_READONLY)) {
998 fputs(_("The has_journal feature may only be "
999 "cleared when the filesystem is\n"
1000 "unmounted or mounted "
1001 "read-only.\n"), stderr);
1004 if (ext2fs_has_feature_journal_needs_recovery(sb) &&
1006 fputs(_("The needs_recovery flag is set. "
1007 "Please run e2fsck before clearing\n"
1008 "the has_journal flag.\n"), stderr);
1011 if (sb->s_journal_inum) {
1012 if (remove_journal_inode(fs))
1015 if (sb->s_journal_dev) {
1016 if (remove_journal_device(fs))
1021 if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT,
1022 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
1023 if (ext2fs_has_feature_meta_bg(sb)) {
1024 fputs(_("Setting filesystem feature 'sparse_super' "
1025 "not supported\nfor filesystems with "
1026 "the meta_bg feature enabled.\n"),
1032 if (FEATURE_ON(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_MMP)) {
1035 if ((mount_flags & EXT2_MF_MOUNTED) ||
1036 (mount_flags & EXT2_MF_READONLY)) {
1037 fputs(_("The multiple mount protection feature can't\n"
1038 "be set if the filesystem is mounted or\n"
1039 "read-only.\n"), stderr);
1043 error = ext2fs_mmp_init(fs);
1045 fputs(_("\nError while enabling multiple mount "
1046 "protection feature."), stderr);
1051 * We want to update group desc with the new free blocks count
1053 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
1055 printf(_("Multiple mount protection has been enabled "
1056 "with update interval %ds.\n"),
1057 sb->s_mmp_update_interval);
1060 if (FEATURE_OFF(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_MMP)) {
1063 if (mount_flags & EXT2_MF_READONLY) {
1064 fputs(_("The multiple mount protection feature cannot\n"
1065 "be disabled if the filesystem is readonly.\n"),
1070 error = ext2fs_read_bitmaps(fs);
1072 fputs(_("Error while reading bitmaps\n"), stderr);
1076 error = ext2fs_mmp_read(fs, sb->s_mmp_block, NULL);
1078 struct mmp_struct *mmp_cmp = fs->mmp_cmp;
1080 if (error == EXT2_ET_MMP_MAGIC_INVALID)
1081 printf(_("Magic number in MMP block does not "
1082 "match. expected: %x, actual: %x\n"),
1083 EXT4_MMP_MAGIC, mmp_cmp->mmp_magic);
1085 com_err(program_name, error, "%s",
1086 _("while reading MMP block."));
1090 /* We need to force out the group descriptors as well */
1091 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
1092 ext2fs_block_alloc_stats2(fs, sb->s_mmp_block, -1);
1094 sb->s_mmp_block = 0;
1095 sb->s_mmp_update_interval = 0;
1098 if (FEATURE_ON(E2P_FEATURE_COMPAT, EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
1100 * If adding a journal flag, let the create journal
1101 * code below handle setting the flag and creating the
1102 * journal. We supply a default size if necessary.
1106 ext2fs_clear_feature_journal(sb);
1109 if (FEATURE_ON(E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_DIR_INDEX)) {
1110 if (!sb->s_def_hash_version)
1111 sb->s_def_hash_version = EXT2_HASH_HALF_MD4;
1112 if (uuid_is_null((unsigned char *) sb->s_hash_seed))
1113 uuid_generate((unsigned char *) sb->s_hash_seed);
1116 if (FEATURE_OFF(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
1117 if (ext2fs_check_desc(fs)) {
1118 fputs(_("Clearing the flex_bg flag would "
1119 "cause the the filesystem to be\n"
1120 "inconsistent.\n"), stderr);
1125 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
1126 EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
1127 if ((mount_flags & EXT2_MF_MOUNTED) &&
1128 !(mount_flags & EXT2_MF_READONLY)) {
1129 fputs(_("The huge_file feature may only be "
1130 "cleared when the filesystem is\n"
1131 "unmounted or mounted "
1132 "read-only.\n"), stderr);
1137 if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT,
1138 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
1139 if (check_fsck_needed(fs))
1141 if (mount_flags & EXT2_MF_MOUNTED) {
1142 fputs(_("Cannot enable metadata_csum on a mounted "
1143 "filesystem!\n"), stderr);
1146 if (!ext2fs_has_feature_extents(fs->super))
1148 _("Extents are not enabled. The file extent "
1149 "tree can be checksummed, whereas block maps "
1150 "cannot. Not enabling extents reduces the "
1151 "coverage of metadata checksumming. "
1152 "Re-run with -O extent to rectify.\n"));
1153 if (!ext2fs_has_feature_64bit(fs->super))
1155 _("64-bit filesystem support is not enabled. "
1156 "The larger fields afforded by this feature "
1157 "enable full-strength checksumming. "
1158 "Run resize2fs -b to rectify.\n"));
1159 rewrite_checksums = 1;
1160 /* metadata_csum supersedes uninit_bg */
1161 ext2fs_clear_feature_gdt_csum(fs->super);
1163 /* if uninit_bg was previously off, rewrite group desc */
1164 if (!(old_features[E2P_FEATURE_RO_INCOMPAT] &
1165 EXT4_FEATURE_RO_COMPAT_GDT_CSUM))
1166 enable_uninit_bg(fs);
1169 * Since metadata_csum supersedes uninit_bg, pretend like
1170 * uninit_bg has been off all along.
1172 old_features[E2P_FEATURE_RO_INCOMPAT] &=
1173 ~EXT4_FEATURE_RO_COMPAT_GDT_CSUM;
1176 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
1177 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
1178 __u32 test_features[3];
1180 if (check_fsck_needed(fs))
1182 if (mount_flags & EXT2_MF_MOUNTED) {
1183 fputs(_("Cannot disable metadata_csum on a mounted "
1184 "filesystem!\n"), stderr);
1187 rewrite_checksums = 1;
1189 /* Enable uninit_bg unless the user expressly turned it off */
1190 memcpy(test_features, old_features, sizeof(test_features));
1191 test_features[E2P_FEATURE_RO_INCOMPAT] |=
1192 EXT4_FEATURE_RO_COMPAT_GDT_CSUM;
1193 e2p_edit_feature2(features, test_features, ok_features,
1194 clear_ok_features, NULL, NULL);
1195 if (test_features[E2P_FEATURE_RO_INCOMPAT] &
1196 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)
1197 ext2fs_set_feature_gdt_csum(fs->super);
1200 * If we're turning off metadata_csum and not turning on
1201 * uninit_bg, rewrite group desc.
1203 if (!ext2fs_has_feature_gdt_csum(fs->super)) {
1204 err = disable_uninit_bg(fs,
1205 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM);
1210 * metadata_csum previously provided uninit_bg, so if
1211 * we're also setting the uninit_bg feature bit,
1212 * pretend like it was previously enabled. Checksums
1213 * will be rewritten with crc16 later.
1215 old_features[E2P_FEATURE_RO_INCOMPAT] |=
1216 EXT4_FEATURE_RO_COMPAT_GDT_CSUM;
1219 if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT,
1220 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
1221 /* Do not enable uninit_bg when metadata_csum enabled */
1222 if (ext2fs_has_feature_metadata_csum(fs->super))
1223 ext2fs_clear_feature_gdt_csum(fs->super);
1225 enable_uninit_bg(fs);
1228 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
1229 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
1230 err = disable_uninit_bg(fs,
1231 EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
1237 * We don't actually toggle 64bit; resize2fs does that. But this
1238 * must come after the metadata_csum feature_on so that it won't
1239 * complain about the lack of 64bit.
1241 if (FEATURE_ON(E2P_FEATURE_INCOMPAT,
1242 EXT4_FEATURE_INCOMPAT_64BIT)) {
1243 if (mount_flags & EXT2_MF_MOUNTED) {
1244 fprintf(stderr, _("Cannot enable 64-bit mode "
1245 "while mounted!\n"));
1248 ext2fs_clear_feature_64bit(sb);
1251 if (FEATURE_OFF(E2P_FEATURE_INCOMPAT,
1252 EXT4_FEATURE_INCOMPAT_64BIT)) {
1253 if (mount_flags & EXT2_MF_MOUNTED) {
1254 fprintf(stderr, _("Cannot disable 64-bit mode "
1255 "while mounted!\n"));
1258 ext2fs_set_feature_64bit(sb);
1262 if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT,
1263 EXT4_FEATURE_RO_COMPAT_QUOTA)) {
1265 * Set the Q_flag here and handle the quota options in the code
1270 /* Enable all quota by default */
1271 for (qtype = 0; qtype < MAXQUOTAS; qtype++)
1272 quota_enable[qtype] = QOPT_ENABLE;
1274 ext2fs_clear_feature_quota(sb);
1277 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
1278 EXT4_FEATURE_RO_COMPAT_QUOTA)) {
1280 * Set the Q_flag here and handle the quota options in the code
1284 fputs(_("\nWarning: '^quota' option overrides '-Q'"
1285 "arguments.\n"), stderr);
1287 /* Disable all quota by default */
1288 for (qtype = 0; qtype < MAXQUOTAS; qtype++)
1289 quota_enable[qtype] = QOPT_DISABLE;
1292 if (FEATURE_ON(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_ENCRYPT)) {
1293 fs->super->s_encrypt_algos[0] =
1294 EXT4_ENCRYPTION_MODE_AES_256_XTS;
1295 fs->super->s_encrypt_algos[1] =
1296 EXT4_ENCRYPTION_MODE_AES_256_CTS;
1299 if (sb->s_rev_level == EXT2_GOOD_OLD_REV &&
1300 (sb->s_feature_compat || sb->s_feature_ro_compat ||
1301 sb->s_feature_incompat))
1302 ext2fs_update_dynamic_rev(fs);
1304 if (FEATURE_CHANGED(E2P_FEATURE_RO_INCOMPAT,
1305 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER) ||
1306 FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
1307 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) ||
1308 FEATURE_CHANGED(E2P_FEATURE_INCOMPAT,
1309 EXT2_FEATURE_INCOMPAT_FILETYPE) ||
1310 FEATURE_CHANGED(E2P_FEATURE_COMPAT,
1311 EXT2_FEATURE_COMPAT_RESIZE_INODE) ||
1312 FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
1313 EXT2_FEATURE_RO_COMPAT_LARGE_FILE))
1314 request_fsck_afterwards(fs);
1316 if ((old_features[E2P_FEATURE_COMPAT] != sb->s_feature_compat) ||
1317 (old_features[E2P_FEATURE_INCOMPAT] != sb->s_feature_incompat) ||
1318 (old_features[E2P_FEATURE_RO_INCOMPAT] != sb->s_feature_ro_compat))
1319 ext2fs_mark_super_dirty(fs);
1325 * Add a journal to the filesystem.
1327 static int add_journal(ext2_filsys fs)
1329 unsigned long journal_blocks;
1334 if (ext2fs_has_feature_journal(fs->super)) {
1335 fputs(_("The filesystem already has a journal.\n"), stderr);
1338 if (journal_device) {
1339 if (!check_plausibility(journal_device, CHECK_BLOCK_DEV,
1341 proceed_question(-1);
1342 check_mount(journal_device, 0, _("journal"));
1343 #ifdef CONFIG_TESTIO_DEBUG
1344 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
1345 io_ptr = test_io_manager;
1346 test_io_backing_manager = unix_io_manager;
1349 io_ptr = unix_io_manager;
1350 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
1351 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1352 fs->blocksize, io_ptr, &jfs);
1354 com_err(program_name, retval,
1355 _("\n\twhile trying to open journal on %s\n"),
1359 printf(_("Creating journal on device %s: "),
1363 retval = ext2fs_add_journal_device(fs, jfs);
1364 ext2fs_close_free(&jfs);
1366 com_err(program_name, retval,
1367 _("while adding filesystem to journal on %s"),
1371 fputs(_("done\n"), stdout);
1372 } else if (journal_size) {
1373 fputs(_("Creating journal inode: "), stdout);
1375 journal_blocks = figure_journal_size(journal_size, fs);
1377 if (journal_location_string)
1379 parse_num_blocks2(journal_location_string,
1380 fs->super->s_log_block_size);
1381 retval = ext2fs_add_journal_inode2(fs, journal_blocks,
1385 fprintf(stderr, "\n");
1386 com_err(program_name, retval, "%s",
1387 _("\n\twhile trying to create journal file"));
1390 fputs(_("done\n"), stdout);
1392 * If the filesystem wasn't mounted, we need to force
1393 * the block group descriptors out.
1395 if ((mount_flags & EXT2_MF_MOUNTED) == 0)
1396 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
1398 print_check_message(fs->super->s_max_mnt_count,
1399 fs->super->s_checkinterval);
1403 free(journal_device);
1407 static void handle_quota_options(ext2_filsys fs)
1411 enum quota_type qtype;
1414 for (qtype = 0 ; qtype < MAXQUOTAS; qtype++)
1415 if (quota_enable[qtype] != 0)
1417 if (qtype == MAXQUOTAS)
1418 /* Nothing to do. */
1421 quota_init_context(&qctx, fs, QUOTA_ALL_BIT);
1422 for (qtype = 0 ; qtype < MAXQUOTAS; qtype++) {
1423 if (quota_enable[qtype] == QOPT_ENABLE) {
1429 quota_compute_usage(qctx);
1431 for (qtype = 0 ; qtype < MAXQUOTAS; qtype++) {
1432 if (quota_enable[qtype] == QOPT_ENABLE &&
1433 *quota_sb_inump(fs->super, qtype) == 0) {
1434 if ((qf_ino = quota_file_exists(fs, qtype)) > 0)
1435 quota_update_limits(qctx, qf_ino, qtype);
1436 quota_write_inode(qctx, 1 << qtype);
1437 } else if (quota_enable[qtype] == QOPT_DISABLE) {
1438 quota_remove_inode(fs, qtype);
1442 quota_release_context(&qctx);
1445 ext2fs_set_feature_quota(fs->super);
1446 ext2fs_mark_super_dirty(fs);
1448 for (qtype = 0 ; qtype < MAXQUOTAS; qtype++)
1449 if (*quota_sb_inump(fs->super, qtype) != 0)
1451 if (qtype == MAXQUOTAS) {
1452 fs->super->s_feature_ro_compat &=
1453 ~EXT4_FEATURE_RO_COMPAT_QUOTA;
1454 ext2fs_mark_super_dirty(fs);
1461 static int option_handle_function(char *token, void *data)
1463 if (strncmp(token, "usr", 3) == 0) {
1464 quota_enable[USRQUOTA] = QOPT_ENABLE;
1465 } else if (strncmp(token, "^usr", 4) == 0) {
1466 quota_enable[USRQUOTA] = QOPT_DISABLE;
1467 } else if (strncmp(token, "grp", 3) == 0) {
1468 quota_enable[GRPQUOTA] = QOPT_ENABLE;
1469 } else if (strncmp(token, "^grp", 4) == 0) {
1470 quota_enable[GRPQUOTA] = QOPT_DISABLE;
1472 fputs(_("\nBad quota options specified.\n\n"
1473 "Following valid quota options are available "
1474 "(pass by separating with comma):\n"
1483 static void parse_e2label_options(int argc, char ** argv)
1485 if ((argc < 2) || (argc > 3)) {
1486 fputs(_("Usage: e2label device [newlabel]\n"), stderr);
1489 io_options = strchr(argv[1], '?');
1492 device_name = blkid_get_devname(NULL, argv[1], NULL);
1494 com_err("e2label", 0, _("Unable to resolve '%s'"),
1498 open_flag = EXT2_FLAG_JOURNAL_DEV_OK;
1500 open_flag |= EXT2_FLAG_RW;
1502 new_label = argv[2];
1507 static time_t parse_time(char *str)
1511 if (strcmp(str, "now") == 0) {
1514 memset(&ts, 0, sizeof(ts));
1515 #ifdef HAVE_STRPTIME
1516 strptime(str, "%Y%m%d%H%M%S", &ts);
1518 sscanf(str, "%4d%2d%2d%2d%2d%2d", &ts.tm_year, &ts.tm_mon,
1519 &ts.tm_mday, &ts.tm_hour, &ts.tm_min, &ts.tm_sec);
1522 if (ts.tm_year < 0 || ts.tm_mon < 0 || ts.tm_mon > 11 ||
1523 ts.tm_mday < 0 || ts.tm_mday > 31 || ts.tm_hour > 23 ||
1524 ts.tm_min > 59 || ts.tm_sec > 61)
1527 if (ts.tm_mday == 0) {
1528 com_err(program_name, 0,
1529 _("Couldn't parse date/time specifier: %s"),
1534 return (mktime(&ts));
1537 static void parse_tune2fs_options(int argc, char **argv)
1544 char optstring[100] = "c:e:fg:i:jlm:o:r:s:u:C:E:I:J:L:M:O:T:U:z:Q:";
1547 printf("tune2fs %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
1548 while ((c = getopt(argc, argv, optstring)) != EOF)
1551 max_mount_count = strtol(optarg, &tmp, 0);
1552 if (*tmp || max_mount_count > 16000) {
1553 com_err(program_name, 0,
1554 _("bad mounts count - %s"),
1558 if (max_mount_count == 0)
1559 max_mount_count = -1;
1561 open_flag = EXT2_FLAG_RW;
1564 mount_count = strtoul(optarg, &tmp, 0);
1565 if (*tmp || mount_count > 16000) {
1566 com_err(program_name, 0,
1567 _("bad mounts count - %s"),
1572 open_flag = EXT2_FLAG_RW;
1575 if (strcmp(optarg, "continue") == 0)
1576 errors = EXT2_ERRORS_CONTINUE;
1577 else if (strcmp(optarg, "remount-ro") == 0)
1578 errors = EXT2_ERRORS_RO;
1579 else if (strcmp(optarg, "panic") == 0)
1580 errors = EXT2_ERRORS_PANIC;
1582 com_err(program_name, 0,
1583 _("bad error behavior - %s"),
1588 open_flag = EXT2_FLAG_RW;
1591 extended_cmd = optarg;
1592 open_flag |= EXT2_FLAG_RW;
1594 case 'f': /* Force */
1598 resgid = strtoul(optarg, &tmp, 0);
1600 gr = getgrnam(optarg);
1604 resgid = gr->gr_gid;
1609 com_err(program_name, 0,
1610 _("bad gid/group name - %s"),
1615 open_flag = EXT2_FLAG_RW;
1618 interval = strtoul(optarg, &tmp, 0);
1625 case 'D': /* days */
1631 case 'M': /* months! */
1632 interval *= 86400 * 30;
1636 case 'W': /* weeks */
1637 interval *= 86400 * 7;
1642 com_err(program_name, 0,
1643 _("bad interval - %s"), optarg);
1647 open_flag = EXT2_FLAG_RW;
1652 open_flag = EXT2_FLAG_RW;
1655 parse_journal_opts(optarg);
1656 open_flag = EXT2_FLAG_RW;
1664 open_flag |= EXT2_FLAG_RW |
1665 EXT2_FLAG_JOURNAL_DEV_OK;
1668 reserved_ratio = strtod(optarg, &tmp);
1669 if (*tmp || reserved_ratio > 50 ||
1670 reserved_ratio < 0) {
1671 com_err(program_name, 0,
1672 _("bad reserved block ratio - %s"),
1677 open_flag = EXT2_FLAG_RW;
1680 new_last_mounted = optarg;
1682 open_flag = EXT2_FLAG_RW;
1686 com_err(program_name, 0, "%s",
1687 _("-o may only be specified once"));
1690 mntopts_cmd = optarg;
1691 open_flag = EXT2_FLAG_RW;
1695 com_err(program_name, 0, "%s",
1696 _("-O may only be specified once"));
1699 features_cmd = optarg;
1700 open_flag = EXT2_FLAG_RW;
1704 ret = parse_quota_opts(optarg, option_handle_function,
1708 open_flag = EXT2_FLAG_RW;
1711 reserved_blocks = strtoul(optarg, &tmp, 0);
1713 com_err(program_name, 0,
1714 _("bad reserved blocks count - %s"),
1719 open_flag = EXT2_FLAG_RW;
1721 case 's': /* Deprecated */
1722 s_flag = atoi(optarg);
1723 open_flag = EXT2_FLAG_RW;
1727 last_check_time = parse_time(optarg);
1728 open_flag = EXT2_FLAG_RW;
1731 resuid = strtoul(optarg, &tmp, 0);
1733 pw = getpwnam(optarg);
1737 resuid = pw->pw_uid;
1742 com_err(program_name, 0,
1743 _("bad uid/user name - %s"),
1748 open_flag = EXT2_FLAG_RW;
1753 open_flag = EXT2_FLAG_RW |
1754 EXT2_FLAG_JOURNAL_DEV_OK;
1757 new_inode_size = strtoul(optarg, &tmp, 0);
1759 com_err(program_name, 0,
1760 _("bad inode size - %s"),
1764 if (!((new_inode_size &
1765 (new_inode_size - 1)) == 0)) {
1766 com_err(program_name, 0,
1767 _("Inode size must be a "
1768 "power of two- %s"),
1772 open_flag = EXT2_FLAG_RW;
1781 if (optind < argc - 1 || optind == argc)
1783 if (!open_flag && !l_flag)
1785 io_options = strchr(argv[optind], '?');
1788 device_name = blkid_get_devname(NULL, argv[optind], NULL);
1790 com_err(program_name, 0, _("Unable to resolve '%s'"),
1796 #ifdef CONFIG_BUILD_FINDFS
1797 void do_findfs(int argc, char **argv)
1802 (strncmp(argv[1], "LABEL=", 6) && strncmp(argv[1], "UUID=", 5))) {
1803 fprintf(stderr, "Usage: findfs LABEL=<label>|UUID=<uuid>\n");
1806 dev = blkid_get_devname(NULL, argv[1], NULL);
1808 com_err("findfs", 0, _("Unable to resolve '%s'"),
1817 static int parse_extended_opts(ext2_filsys fs, const char *opts)
1819 char *buf, *token, *next, *p, *arg;
1824 buf = malloc(len+1);
1826 fprintf(stderr, "%s",
1827 _("Couldn't allocate memory to parse options!\n"));
1831 for (token = buf; token && *token; token = next) {
1832 p = strchr(token, ',');
1838 arg = strchr(token, '=');
1843 if (strcmp(token, "clear-mmp") == 0 ||
1844 strcmp(token, "clear_mmp") == 0) {
1846 } else if (strcmp(token, "mmp_update_interval") == 0) {
1852 intv = strtoul(arg, &p, 0);
1855 _("Invalid mmp_update_interval: %s\n"),
1861 intv = EXT4_MMP_UPDATE_INTERVAL;
1862 } else if (intv > EXT4_MMP_MAX_UPDATE_INTERVAL) {
1864 _("mmp_update_interval too big: %lu\n"),
1869 printf(P_("Setting multiple mount protection update "
1870 "interval to %lu second\n",
1871 "Setting multiple mount protection update "
1872 "interval to %lu seconds\n", intv),
1874 fs->super->s_mmp_update_interval = intv;
1875 ext2fs_mark_super_dirty(fs);
1876 } else if (!strcmp(token, "test_fs")) {
1877 fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
1878 printf("Setting test filesystem flag\n");
1879 ext2fs_mark_super_dirty(fs);
1880 } else if (!strcmp(token, "^test_fs")) {
1881 fs->super->s_flags &= ~EXT2_FLAGS_TEST_FILESYS;
1882 printf("Clearing test filesystem flag\n");
1883 ext2fs_mark_super_dirty(fs);
1884 } else if (strcmp(token, "stride") == 0) {
1889 stride = strtoul(arg, &p, 0);
1892 _("Invalid RAID stride: %s\n"),
1898 } else if (strcmp(token, "stripe-width") == 0 ||
1899 strcmp(token, "stripe_width") == 0) {
1904 stripe_width = strtoul(arg, &p, 0);
1907 _("Invalid RAID stripe-width: %s\n"),
1912 stripe_width_set = 1;
1913 } else if (strcmp(token, "hash_alg") == 0 ||
1914 strcmp(token, "hash-alg") == 0) {
1919 hash_alg = e2p_string2hash(arg);
1922 _("Invalid hash algorithm: %s\n"),
1927 fs->super->s_def_hash_version = hash_alg;
1928 printf(_("Setting default hash algorithm "
1931 ext2fs_mark_super_dirty(fs);
1932 } else if (!strcmp(token, "mount_opts")) {
1937 if (strlen(arg) >= sizeof(fs->super->s_mount_opts)) {
1939 "Extended mount options too long\n");
1942 ext_mount_opts = strdup(arg);
1947 fprintf(stderr, "%s", _("\nBad options specified.\n\n"
1948 "Extended options are separated by commas, "
1949 "and may take an argument which\n"
1950 "\tis set off by an equals ('=') sign.\n\n"
1951 "Valid extended options are:\n"
1953 "\thash_alg=<hash algorithm>\n"
1954 "\tmount_opts=<extended default mount options>\n"
1955 "\tstride=<RAID per-disk chunk size in blocks>\n"
1956 "\tstripe_width=<RAID stride*data disks in blocks>\n"
1968 * Fill in the block bitmap bmap with the information regarding the
1969 * blocks to be moved
1971 static int get_move_bitmaps(ext2_filsys fs, int new_ino_blks_per_grp,
1972 ext2fs_block_bitmap bmap)
1976 ext2_badblocks_list bb_list = 0;
1977 blk64_t j, needed_blocks = 0;
1978 blk64_t start_blk, end_blk;
1980 retval = ext2fs_read_bb_inode(fs, &bb_list);
1984 for (i = 0; i < fs->group_desc_count; i++) {
1985 start_blk = ext2fs_inode_table_loc(fs, i) +
1986 fs->inode_blocks_per_group;
1988 end_blk = ext2fs_inode_table_loc(fs, i) +
1989 new_ino_blks_per_grp;
1991 for (j = start_blk; j < end_blk; j++) {
1992 if (ext2fs_test_block_bitmap2(fs->block_map, j)) {
1994 * IF the block is a bad block we fail
1996 if (ext2fs_badblocks_list_test(bb_list, j)) {
1997 ext2fs_badblocks_list_free(bb_list);
2001 ext2fs_mark_block_bitmap2(bmap, j);
2004 * We are going to use this block for
2005 * inode table. So mark them used.
2007 ext2fs_mark_block_bitmap2(fs->block_map, j);
2010 needed_blocks += end_blk - start_blk;
2013 ext2fs_badblocks_list_free(bb_list);
2014 if (needed_blocks > ext2fs_free_blocks_count(fs->super))
2020 static int ext2fs_is_meta_block(ext2_filsys fs, blk64_t blk)
2023 group = ext2fs_group_of_blk2(fs, blk);
2024 if (ext2fs_block_bitmap_loc(fs, group) == blk)
2026 if (ext2fs_inode_bitmap_loc(fs, group) == blk)
2031 static int ext2fs_is_block_in_group(ext2_filsys fs, dgrp_t group, blk64_t blk)
2033 blk64_t start_blk, end_blk;
2034 start_blk = fs->super->s_first_data_block +
2035 EXT2_GROUPS_TO_BLOCKS(fs->super, group);
2037 * We cannot get new block beyond end_blk for for the last block group
2038 * so we can check with EXT2_BLOCKS_PER_GROUP even for last block group
2040 end_blk = start_blk + EXT2_BLOCKS_PER_GROUP(fs->super);
2041 if (blk >= start_blk && blk <= end_blk)
2046 static int move_block(ext2_filsys fs, ext2fs_block_bitmap bmap)
2053 blk64_t blk, new_blk, goal;
2054 struct blk_move *bmv;
2056 retval = ext2fs_get_mem(fs->blocksize, &buf);
2060 for (new_blk = blk = fs->super->s_first_data_block;
2061 blk < ext2fs_blocks_count(fs->super); blk++) {
2062 if (!ext2fs_test_block_bitmap2(bmap, blk))
2065 if (ext2fs_is_meta_block(fs, blk)) {
2067 * If the block is mapping a fs meta data block
2068 * like group desc/block bitmap/inode bitmap. We
2069 * should find a block in the same group and fix
2070 * the respective fs metadata pointers. Otherwise
2073 group = ext2fs_group_of_blk2(fs, blk);
2074 goal = ext2fs_group_first_block2(fs, group);
2080 retval = ext2fs_new_block2(fs, goal, NULL, &new_blk);
2084 /* new fs meta data block should be in the same group */
2085 if (meta_data && !ext2fs_is_block_in_group(fs, group, new_blk)) {
2090 /* Mark this block as allocated */
2091 ext2fs_mark_block_bitmap2(fs->block_map, new_blk);
2093 /* Add it to block move list */
2094 retval = ext2fs_get_mem(sizeof(struct blk_move), &bmv);
2099 bmv->new_loc = new_blk;
2101 list_add(&(bmv->list), &blk_move_list);
2103 retval = io_channel_read_blk64(fs->io, blk, 1, buf);
2107 retval = io_channel_write_blk64(fs->io, new_blk, 1, buf);
2113 ext2fs_free_mem(&buf);
2117 static blk64_t translate_block(blk64_t blk)
2119 struct list_head *entry;
2120 struct blk_move *bmv;
2122 list_for_each(entry, &blk_move_list) {
2123 bmv = list_entry(entry, struct blk_move, list);
2124 if (bmv->old_loc == blk)
2125 return bmv->new_loc;
2131 static int process_block(ext2_filsys fs EXT2FS_ATTR((unused)),
2133 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
2134 blk64_t ref_block EXT2FS_ATTR((unused)),
2135 int ref_offset EXT2FS_ATTR((unused)),
2140 ext2fs_block_bitmap bmap = (ext2fs_block_bitmap) priv_data;
2142 if (!ext2fs_test_block_bitmap2(bmap, *block_nr))
2144 new_blk = translate_block(*block_nr);
2146 *block_nr = new_blk;
2148 * This will force the ext2fs_write_inode in the iterator
2150 ret |= BLOCK_CHANGED;
2156 static int inode_scan_and_fix(ext2_filsys fs, ext2fs_block_bitmap bmap)
2158 errcode_t retval = 0;
2161 char *block_buf = 0;
2162 struct ext2_inode inode;
2163 ext2_inode_scan scan = NULL;
2165 retval = ext2fs_get_mem(fs->blocksize * 3, &block_buf);
2169 retval = ext2fs_open_inode_scan(fs, 0, &scan);
2174 retval = ext2fs_get_next_inode(scan, &ino, &inode);
2181 if (inode.i_links_count == 0)
2182 continue; /* inode not in use */
2185 * If we end up modifying the journal inode
2186 * the sb->s_jnl_blocks will differ. But a
2187 * subsequent e2fsck fixes that.
2188 * Do we need to fix this ??
2191 if (ext2fs_file_acl_block(fs, &inode) &&
2192 ext2fs_test_block_bitmap2(bmap,
2193 ext2fs_file_acl_block(fs, &inode))) {
2194 blk = translate_block(ext2fs_file_acl_block(fs,
2199 ext2fs_file_acl_block_set(fs, &inode, blk);
2202 * Write the inode to disk so that inode table
2205 retval = ext2fs_write_inode(fs, ino, &inode);
2210 if (!ext2fs_inode_has_valid_blocks2(fs, &inode))
2213 retval = ext2fs_block_iterate3(fs, ino, 0, block_buf,
2214 process_block, bmap);
2221 ext2fs_free_mem(&block_buf);
2222 ext2fs_close_inode_scan(scan);
2228 * We need to scan for inode and block bitmaps that may need to be
2229 * moved. This can take place if the filesystem was formatted for
2230 * RAID arrays using the mke2fs's extended option "stride".
2232 static int group_desc_scan_and_fix(ext2_filsys fs, ext2fs_block_bitmap bmap)
2235 blk64_t blk, new_blk;
2237 for (i = 0; i < fs->group_desc_count; i++) {
2238 blk = ext2fs_block_bitmap_loc(fs, i);
2239 if (ext2fs_test_block_bitmap2(bmap, blk)) {
2240 new_blk = translate_block(blk);
2243 ext2fs_block_bitmap_loc_set(fs, i, new_blk);
2246 blk = ext2fs_inode_bitmap_loc(fs, i);
2247 if (ext2fs_test_block_bitmap2(bmap, blk)) {
2248 new_blk = translate_block(blk);
2251 ext2fs_inode_bitmap_loc_set(fs, i, new_blk);
2257 static int expand_inode_table(ext2_filsys fs, unsigned long new_ino_size)
2262 int new_ino_blks_per_grp;
2264 char *old_itable = NULL, *new_itable = NULL;
2265 char *tmp_old_itable = NULL, *tmp_new_itable = NULL;
2266 unsigned long old_ino_size;
2267 int old_itable_size, new_itable_size;
2269 old_itable_size = fs->inode_blocks_per_group * fs->blocksize;
2270 old_ino_size = EXT2_INODE_SIZE(fs->super);
2272 new_ino_blks_per_grp = ext2fs_div_ceil(
2273 EXT2_INODES_PER_GROUP(fs->super) *
2277 new_itable_size = new_ino_blks_per_grp * fs->blocksize;
2279 retval = ext2fs_get_mem(old_itable_size, &old_itable);
2283 retval = ext2fs_get_mem(new_itable_size, &new_itable);
2287 tmp_old_itable = old_itable;
2288 tmp_new_itable = new_itable;
2290 for (i = 0; i < fs->group_desc_count; i++) {
2291 blk = ext2fs_inode_table_loc(fs, i);
2292 retval = io_channel_read_blk64(fs->io, blk,
2293 fs->inode_blocks_per_group, old_itable);
2297 for (j = 0; j < EXT2_INODES_PER_GROUP(fs->super); j++) {
2298 memcpy(new_itable, old_itable, old_ino_size);
2300 memset(new_itable+old_ino_size, 0,
2301 new_ino_size - old_ino_size);
2303 new_itable += new_ino_size;
2304 old_itable += old_ino_size;
2307 /* reset the pointer */
2308 old_itable = tmp_old_itable;
2309 new_itable = tmp_new_itable;
2311 retval = io_channel_write_blk64(fs->io, blk,
2312 new_ino_blks_per_grp, new_itable);
2317 /* Update the meta data */
2318 fs->inode_blocks_per_group = new_ino_blks_per_grp;
2319 ext2fs_free_inode_cache(fs->icache);
2321 fs->super->s_inode_size = new_ino_size;
2325 ext2fs_free_mem(&old_itable);
2328 ext2fs_free_mem(&new_itable);
2333 static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs)
2337 unsigned int group = 0;
2338 unsigned int count = 0;
2343 * First calculate the block statistics
2345 for (blk = fs->super->s_first_data_block;
2346 blk < ext2fs_blocks_count(fs->super); blk++) {
2347 if (!ext2fs_fast_test_block_bitmap2(fs->block_map, blk)) {
2352 if ((count == fs->super->s_blocks_per_group) ||
2353 (blk == ext2fs_blocks_count(fs->super)-1)) {
2354 ext2fs_bg_free_blocks_count_set(fs, group++,
2360 total_free = EXT2FS_C2B(fs, total_free);
2361 ext2fs_free_blocks_count_set(fs->super, total_free);
2364 * Next, calculate the inode statistics
2371 /* Protect loop from wrap-around if s_inodes_count maxed */
2372 for (ino = 1; ino <= fs->super->s_inodes_count && ino > 0; ino++) {
2373 if (!ext2fs_fast_test_inode_bitmap2(fs->inode_map, ino)) {
2378 if ((count == fs->super->s_inodes_per_group) ||
2379 (ino == fs->super->s_inodes_count)) {
2380 ext2fs_bg_free_inodes_count_set(fs, group++,
2386 fs->super->s_free_inodes_count = total_free;
2387 ext2fs_mark_super_dirty(fs);
2391 #define list_for_each_safe(pos, pnext, head) \
2392 for (pos = (head)->next, pnext = pos->next; pos != (head); \
2393 pos = pnext, pnext = pos->next)
2395 static void free_blk_move_list(void)
2397 struct list_head *entry, *tmp;
2398 struct blk_move *bmv;
2400 list_for_each_safe(entry, tmp, &blk_move_list) {
2401 bmv = list_entry(entry, struct blk_move, list);
2403 ext2fs_free_mem(&bmv);
2408 static int resize_inode(ext2_filsys fs, unsigned long new_size)
2411 int new_ino_blks_per_grp;
2412 ext2fs_block_bitmap bmap;
2414 retval = ext2fs_read_inode_bitmap(fs);
2416 fputs(_("Failed to read inode bitmap\n"), stderr);
2419 retval = ext2fs_read_block_bitmap(fs);
2421 fputs(_("Failed to read block bitmap\n"), stderr);
2424 INIT_LIST_HEAD(&blk_move_list);
2427 new_ino_blks_per_grp = ext2fs_div_ceil(
2428 EXT2_INODES_PER_GROUP(fs->super)*
2432 /* We may change the file system.
2433 * Mark the file system as invalid so that
2434 * the user is prompted to run fsck.
2436 fs->super->s_state &= ~EXT2_VALID_FS;
2438 retval = ext2fs_allocate_block_bitmap(fs, _("blocks to be moved"),
2441 fputs(_("Failed to allocate block bitmap when "
2442 "increasing inode size\n"), stderr);
2445 retval = get_move_bitmaps(fs, new_ino_blks_per_grp, bmap);
2447 fputs(_("Not enough space to increase inode size \n"), stderr);
2450 retval = move_block(fs, bmap);
2452 fputs(_("Failed to relocate blocks during inode resize \n"),
2456 retval = inode_scan_and_fix(fs, bmap);
2460 retval = group_desc_scan_and_fix(fs, bmap);
2464 retval = expand_inode_table(fs, new_size);
2468 ext2fs_calculate_summary_stats(fs);
2470 fs->super->s_state |= EXT2_VALID_FS;
2471 /* mark super block and block bitmap as dirty */
2472 ext2fs_mark_super_dirty(fs);
2473 ext2fs_mark_bb_dirty(fs);
2476 free_blk_move_list();
2477 ext2fs_free_block_bitmap(bmap);
2482 free_blk_move_list();
2483 ext2fs_free_block_bitmap(bmap);
2484 fputs(_("Error in resizing the inode size.\n"
2485 "Run e2undo to undo the "
2486 "file system changes. \n"), stderr);
2491 static int tune2fs_setup_tdb(const char *name, io_manager *io_ptr)
2493 errcode_t retval = 0;
2494 const char *tdb_dir;
2495 char *tdb_file = NULL;
2496 char *dev_name, *tmp_name;
2498 /* (re)open a specific undo file */
2499 if (undo_file && undo_file[0] != 0) {
2500 retval = set_undo_io_backing_manager(*io_ptr);
2503 *io_ptr = undo_io_manager;
2504 retval = set_undo_io_backup_file(undo_file);
2507 printf(_("Overwriting existing filesystem; this can be undone "
2508 "using the command:\n"
2509 " e2undo %s %s\n\n"),
2515 * Configuration via a conf file would be
2518 tdb_dir = getenv("E2FSPROGS_UNDO_DIR");
2520 tdb_dir = "/var/lib/e2fsprogs";
2522 if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
2523 access(tdb_dir, W_OK))
2526 tmp_name = strdup(name);
2529 dev_name = basename(tmp_name);
2530 tdb_file = malloc(strlen(tdb_dir) + 9 + strlen(dev_name) + 7 + 1);
2535 sprintf(tdb_file, "%s/tune2fs-%s.e2undo", tdb_dir, dev_name);
2538 if ((unlink(tdb_file) < 0) && (errno != ENOENT)) {
2540 com_err(program_name, retval,
2541 _("while trying to delete %s"), tdb_file);
2545 retval = set_undo_io_backing_manager(*io_ptr);
2548 *io_ptr = undo_io_manager;
2549 retval = set_undo_io_backup_file(tdb_file);
2552 printf(_("Overwriting existing filesystem; this can be undone "
2553 "using the command:\n"
2554 " e2undo %s %s\n\n"),
2562 com_err("tune2fs", retval, "while trying to setup undo file\n");
2567 fs_update_journal_user(struct ext2_super_block *sb, __u8 old_uuid[UUID_SIZE])
2569 int retval, nr_users, start;
2570 journal_superblock_t *jsb;
2574 char uuid[UUID_STR_SIZE];
2575 char buf[SUPERBLOCK_SIZE] __attribute__ ((aligned(8)));
2577 if (!ext2fs_has_feature_journal(sb) || uuid_is_null(sb->s_journal_uuid))
2580 uuid_unparse(sb->s_journal_uuid, uuid);
2581 journal_path = blkid_get_devname(NULL, "UUID", uuid);
2585 retval = ext2fs_open2(journal_path, io_options,
2586 EXT2_FLAG_JOURNAL_DEV_OK | EXT2_FLAG_RW,
2587 0, 0, unix_io_manager, &jfs);
2589 com_err(program_name, retval,
2590 _("while trying to open %s"),
2595 retval = get_journal_sb(jfs, buf);
2597 if (retval == EXT2_ET_UNSUPP_FEATURE)
2598 fprintf(stderr, _("%s is not a journal device.\n"),
2603 jsb = (journal_superblock_t *) buf;
2604 /* Find the filesystem UUID */
2605 nr_users = ntohl(jsb->s_nr_users);
2607 j_uuid = journal_user(old_uuid, jsb->s_users, nr_users);
2608 if (j_uuid == NULL) {
2609 fputs(_("Filesystem's UUID not found on journal device.\n"),
2611 return EXT2_ET_LOAD_EXT_JOURNAL;
2614 memcpy(j_uuid, sb->s_uuid, UUID_SIZE);
2616 start = ext2fs_journal_sb_start(jfs->blocksize);
2617 /* Write back the journal superblock */
2618 retval = io_channel_write_blk64(jfs->io, start, -SUPERBLOCK_SIZE, buf);
2620 com_err(program_name, retval,
2621 "while writing journal superblock.");
2630 #ifndef BUILD_AS_LIB
2631 int main(int argc, char **argv)
2633 int tune2fs_main(int argc, char **argv)
2634 #endif /* BUILD_AS_LIB */
2638 struct ext2_super_block *sb;
2639 io_manager io_ptr, io_ptr_orig = NULL;
2641 char default_undo_file[1] = { 0 };
2644 setlocale(LC_MESSAGES, "");
2645 setlocale(LC_CTYPE, "");
2646 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
2647 textdomain(NLS_CAT_NAME);
2648 set_com_err_gettext(gettext);
2651 program_name = *argv;
2652 add_error_table(&et_ext2_error_table);
2654 #ifdef CONFIG_BUILD_FINDFS
2655 if (strcmp(get_progname(argv[0]), "findfs") == 0)
2656 do_findfs(argc, argv);
2658 if (strcmp(get_progname(argv[0]), "e2label") == 0)
2659 parse_e2label_options(argc, argv);
2661 parse_tune2fs_options(argc, argv);
2663 #ifdef CONFIG_TESTIO_DEBUG
2664 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_DEBUG")) {
2665 io_ptr = test_io_manager;
2666 test_io_backing_manager = unix_io_manager;
2669 io_ptr = unix_io_manager;
2672 if ((open_flag & EXT2_FLAG_RW) == 0 || f_flag)
2673 open_flag |= EXT2_FLAG_SKIP_MMP;
2675 open_flag |= EXT2_FLAG_64BITS | EXT2_FLAG_JOURNAL_DEV_OK;
2677 /* keep the filesystem struct around to dump MMP data */
2678 open_flag |= EXT2_FLAG_NOFREE_ON_ERROR;
2680 retval = ext2fs_open2(device_name, io_options, open_flag,
2683 com_err(program_name, retval,
2684 _("while trying to open %s"),
2686 if (retval == EXT2_ET_MMP_FSCK_ON ||
2687 retval == EXT2_ET_MMP_UNKNOWN_SEQ)
2688 dump_mmp_msg(fs->mmp_buf,
2689 _("If you are sure the filesystem "
2690 "is not in use on any node, run:\n"
2691 "'tune2fs -f -E clear_mmp {device}'\n"));
2692 else if (retval == EXT2_ET_MMP_FAILED)
2693 dump_mmp_msg(fs->mmp_buf, NULL);
2694 else if (retval == EXT2_ET_MMP_MAGIC_INVALID)
2696 _("MMP block magic is bad. Try to fix it by "
2697 "running:\n'e2fsck -f %s'\n"), device_name);
2698 else if (retval == EXT2_ET_BAD_MAGIC)
2699 check_plausibility(device_name, CHECK_FS_EXIST, NULL);
2700 else if (retval != EXT2_ET_MMP_FAILED)
2701 fprintf(stderr, "%s",
2702 _("Couldn't find valid filesystem superblock.\n"));
2707 if (ext2fs_has_feature_journal_dev(fs->super)) {
2708 fprintf(stderr, "%s", _("Cannot modify a journal device.\n"));
2712 fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE;
2716 * Check the inode size is right so we can issue an
2717 * error message and bail before setting up the tdb
2720 if (new_inode_size == EXT2_INODE_SIZE(fs->super)) {
2721 fprintf(stderr, _("The inode size is already %lu\n"),
2726 if (new_inode_size < EXT2_INODE_SIZE(fs->super)) {
2727 fprintf(stderr, "%s",
2728 _("Shrinking inode size is not supported\n"));
2732 if (new_inode_size > fs->blocksize) {
2733 fprintf(stderr, _("Invalid inode size %lu (max %d)\n"),
2734 new_inode_size, fs->blocksize);
2739 * If inode resize is requested use the
2742 undo_file = default_undo_file;
2745 /* Set up an undo file */
2746 if (undo_file && io_ptr_orig == NULL) {
2747 io_ptr_orig = io_ptr;
2748 retval = tune2fs_setup_tdb(device_name, &io_ptr);
2753 if (io_ptr != io_ptr_orig) {
2754 ext2fs_close_free(&fs);
2760 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
2763 /* For e2label emulation */
2764 printf("%.*s\n", (int) sizeof(sb->s_volume_name),
2766 remove_error_table(&et_ext2_error_table);
2770 retval = ext2fs_check_if_mounted(device_name, &mount_flags);
2772 com_err("ext2fs_check_if_mount", retval,
2773 _("while determining whether %s is mounted."),
2778 /* Normally we only need to write out the superblock */
2779 fs->flags |= EXT2_FLAG_SUPER_ONLY;
2782 sb->s_max_mnt_count = max_mount_count;
2783 ext2fs_mark_super_dirty(fs);
2784 printf(_("Setting maximal mount count to %d\n"),
2788 sb->s_mnt_count = mount_count;
2789 ext2fs_mark_super_dirty(fs);
2790 printf(_("Setting current mount count to %d\n"), mount_count);
2793 sb->s_errors = errors;
2794 ext2fs_mark_super_dirty(fs);
2795 printf(_("Setting error behavior to %d\n"), errors);
2798 sb->s_def_resgid = resgid;
2799 ext2fs_mark_super_dirty(fs);
2800 printf(_("Setting reserved blocks gid to %lu\n"), resgid);
2803 if ((unsigned long long)interval >= (1ULL << 32)) {
2804 com_err(program_name, 0,
2805 _("interval between checks is too big (%lu)"),
2810 sb->s_checkinterval = interval;
2811 ext2fs_mark_super_dirty(fs);
2812 printf(_("Setting interval between checks to %lu seconds\n"),
2816 ext2fs_r_blocks_count_set(sb, reserved_ratio *
2817 ext2fs_blocks_count(sb) / 100.0);
2818 ext2fs_mark_super_dirty(fs);
2819 printf (_("Setting reserved blocks percentage to %g%% (%llu blocks)\n"),
2820 reserved_ratio, ext2fs_r_blocks_count(sb));
2823 if (reserved_blocks > ext2fs_blocks_count(sb)/2) {
2824 com_err(program_name, 0,
2825 _("reserved blocks count is too big (%llu)"),
2830 ext2fs_r_blocks_count_set(sb, reserved_blocks);
2831 ext2fs_mark_super_dirty(fs);
2832 printf(_("Setting reserved blocks count to %llu\n"),
2836 if (ext2fs_has_feature_sparse_super(sb)) {
2837 fputs(_("\nThe filesystem already has sparse "
2838 "superblocks.\n"), stderr);
2839 } else if (ext2fs_has_feature_meta_bg(sb)) {
2840 fputs(_("\nSetting the sparse superblock flag not "
2841 "supported\nfor filesystems with "
2842 "the meta_bg feature enabled.\n"),
2847 ext2fs_set_feature_sparse_super(sb);
2848 sb->s_state &= ~EXT2_VALID_FS;
2849 ext2fs_mark_super_dirty(fs);
2850 printf(_("\nSparse superblock flag set. %s"),
2855 fputs(_("\nClearing the sparse superblock flag not supported.\n"),
2861 sb->s_lastcheck = last_check_time;
2862 ext2fs_mark_super_dirty(fs);
2863 printf(_("Setting time filesystem last checked to %s\n"),
2864 ctime(&last_check_time));
2867 sb->s_def_resuid = resuid;
2868 ext2fs_mark_super_dirty(fs);
2869 printf(_("Setting reserved blocks uid to %lu\n"), resuid);
2872 if (strlen(new_label) > sizeof(sb->s_volume_name))
2873 fputs(_("Warning: label too long, truncating.\n"),
2875 memset(sb->s_volume_name, 0, sizeof(sb->s_volume_name));
2876 strncpy(sb->s_volume_name, new_label,
2877 sizeof(sb->s_volume_name));
2878 ext2fs_mark_super_dirty(fs);
2881 memset(sb->s_last_mounted, 0, sizeof(sb->s_last_mounted));
2882 strncpy(sb->s_last_mounted, new_last_mounted,
2883 sizeof(sb->s_last_mounted));
2884 ext2fs_mark_super_dirty(fs);
2887 rc = update_mntopts(fs, mntopts_cmd);
2892 rc = update_feature_set(fs, features_cmd);
2897 rc = parse_extended_opts(fs, extended_cmd);
2900 if (clear_mmp && !f_flag) {
2901 fputs(_("Error in using clear_mmp. "
2902 "It must be used with -f\n"),
2908 rc = ext2fs_mmp_clear(fs);
2911 if (journal_size || journal_device) {
2912 rc = add_journal(fs);
2918 if (mount_flags & EXT2_MF_MOUNTED) {
2919 fputs(_("The quota feature may only be changed when "
2920 "the filesystem is unmounted.\n"), stderr);
2924 handle_quota_options(fs);
2930 char buf[SUPERBLOCK_SIZE] __attribute__ ((aligned(8)));
2931 __u8 old_uuid[UUID_SIZE];
2933 if (ext2fs_has_group_desc_csum(fs)) {
2935 * Changing the UUID requires rewriting all metadata,
2936 * which can race with a mounted fs. Don't allow that.
2938 if ((mount_flags & EXT2_MF_MOUNTED) && !f_flag) {
2939 fputs(_("The UUID may only be "
2940 "changed when the filesystem is "
2941 "unmounted.\n"), stderr);
2944 if (check_fsck_needed(fs))
2948 * Determine if the block group checksums are
2949 * correct so we know whether or not to set
2952 for (i = 0; i < fs->group_desc_count; i++)
2953 if (!ext2fs_group_desc_csum_verify(fs, i))
2955 if (i >= fs->group_desc_count)
2959 memcpy(old_uuid, sb->s_uuid, UUID_SIZE);
2960 if ((strcasecmp(new_UUID, "null") == 0) ||
2961 (strcasecmp(new_UUID, "clear") == 0)) {
2962 uuid_clear(sb->s_uuid);
2963 } else if (strcasecmp(new_UUID, "time") == 0) {
2964 uuid_generate_time(sb->s_uuid);
2965 } else if (strcasecmp(new_UUID, "random") == 0) {
2966 uuid_generate(sb->s_uuid);
2967 } else if (uuid_parse(new_UUID, sb->s_uuid)) {
2968 com_err(program_name, 0, "%s",
2969 _("Invalid UUID format\n"));
2973 ext2fs_init_csum_seed(fs);
2975 for (i = 0; i < fs->group_desc_count; i++)
2976 ext2fs_group_desc_csum_set(fs, i);
2977 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
2980 /* If this is a journal dev, we need to copy UUID into jsb */
2981 if (!(rc = get_journal_sb(fs, buf))) {
2982 journal_superblock_t *jsb;
2984 jsb = (journal_superblock_t *) buf;
2985 fputs(_("Need to update journal superblock.\n"), stdout);
2986 memcpy(jsb->s_uuid, sb->s_uuid, sizeof(sb->s_uuid));
2988 /* Writeback the journal superblock */
2989 if ((rc = io_channel_write_blk64(fs->io,
2990 ext2fs_journal_sb_start(fs->blocksize),
2991 -SUPERBLOCK_SIZE, buf)))
2993 } else if (rc != EXT2_ET_UNSUPP_FEATURE)
2996 rc = 0; /** Reset rc to avoid ext2fs_mmp_stop() */
2998 if ((rc = fs_update_journal_user(sb, old_uuid)))
3002 ext2fs_mark_super_dirty(fs);
3003 if (ext2fs_has_feature_metadata_csum(fs->super))
3004 rewrite_checksums = 1;
3008 if (mount_flags & EXT2_MF_MOUNTED) {
3009 fputs(_("The inode size may only be "
3010 "changed when the filesystem is "
3011 "unmounted.\n"), stderr);
3015 if (ext2fs_has_feature_flex_bg(fs->super)) {
3016 fputs(_("Changing the inode size not supported for "
3017 "filesystems with the flex_bg\n"
3018 "feature enabled.\n"),
3024 * We want to update group descriptor also
3025 * with the new free inode count
3027 if (rewrite_checksums)
3028 fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
3029 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
3030 retval = resize_inode(fs, new_inode_size);
3031 if (rewrite_checksums)
3032 fs->flags &= ~EXT2_FLAG_IGNORE_CSUM_ERRORS;
3034 printf(_("Setting inode size %lu\n"),
3036 rewrite_checksums = 1;
3038 printf("%s", _("Failed to change inode size\n"));
3044 if (rewrite_checksums)
3045 rewrite_metadata_checksums(fs);
3050 sb->s_raid_stride = stride;
3051 ext2fs_mark_super_dirty(fs);
3052 printf(_("Setting stride size to %d\n"), stride);
3054 if (stripe_width_set) {
3055 sb->s_raid_stripe_width = stripe_width;
3056 ext2fs_mark_super_dirty(fs);
3057 printf(_("Setting stripe width to %d\n"), stripe_width);
3059 if (ext_mount_opts) {
3060 strncpy((char *)(fs->super->s_mount_opts), ext_mount_opts,
3061 sizeof(fs->super->s_mount_opts));
3062 fs->super->s_mount_opts[sizeof(fs->super->s_mount_opts)-1] = 0;
3063 ext2fs_mark_super_dirty(fs);
3064 printf(_("Setting extended default mount options to '%s'\n"),
3066 free(ext_mount_opts);
3069 /* Warn if file system needs recovery and it is opened for writing. */
3070 if ((open_flag & EXT2_FLAG_RW) && !(mount_flags & EXT2_MF_MOUNTED) &&
3071 (sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
3072 (sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER)) {
3074 _("Warning: The journal is dirty. You may wish to replay the journal like:\n\n"
3075 "\te2fsck -E journal_only %s\n\n"
3076 "then rerun this command. Otherwise, any changes made may be overwritten\n"
3077 "by journal recovery.\n"), device_name);
3081 remove_error_table(&et_ext2_error_table);
3085 ext2fs_mmp_stop(fs);
3086 #ifndef BUILD_AS_LIB
3091 convert_64bit(fs, feature_64bit);
3092 return (ext2fs_close_free(&fs) ? 1 : 0);