2 * tune2fs.c - Change the file system parameters on an ext2 file system
4 * Copyright (C) 1992, 1993, 1994 Remy Card <card@masi.ibp.fr>
5 * Laboratoire MASI, Institut Blaise Pascal
6 * Universite Pierre et Marie Curie (Paris VI)
8 * Copyright 1995, 1996, 1997, 1998, 1999, 2000 by Theodore Ts'o.
11 * This file may be redistributed under the terms of the GNU Public
19 * 93/10/31 - Added the -c option to change the maximal mount counts
20 * 93/12/14 - Added -l flag to list contents of superblock
21 * M.J.E. Mol (marcel@duteca.et.tudelft.nl)
22 * F.W. ten Wolde (franky@duteca.et.tudelft.nl)
23 * 93/12/29 - Added the -e option to change errors behavior
24 * 94/02/27 - Ported to use the ext2fs library
25 * 94/03/06 - Added the checks interval from Uwe Ohse (uwe@tirka.gun.de)
28 #define _XOPEN_SOURCE 600 /* for inclusion of strptime() */
44 #include <strings.h> /* for strcasecmp() */
46 #define _BSD_SOURCE /* for inclusion of strcasecmp() via <string.h> */
51 #include <sys/types.h>
55 #include "ext2fs/ext2_fs.h"
56 #include "ext2fs/ext2fs.h"
57 #include "et/com_err.h"
58 #include "uuid/uuid.h"
62 #include "plausible.h"
63 #include "blkid/blkid.h"
64 #include "quota/quotaio.h"
66 #include "../version.h"
67 #include "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 usrquota, grpquota;
99 static int rewrite_checksums;
100 static int feature_64bit;
101 static int fsck_requested;
103 int journal_size, journal_flags;
104 char *journal_device;
105 static blk64_t journal_location = ~0LL;
107 static struct list_head blk_move_list;
110 struct list_head list;
116 static const char *please_fsck = N_("Please run e2fsck on the filesystem.\n");
117 static const char *please_dir_fsck =
118 N_("Please run e2fsck -D on the filesystem.\n");
120 #ifdef CONFIG_BUILD_FINDFS
121 void do_findfs(int argc, char **argv);
124 static void usage(void)
127 _("Usage: %s [-c max_mounts_count] [-e errors_behavior] "
129 "\t[-i interval[d|m|w]] [-j] [-J journal_options] [-l]\n"
130 "\t[-m reserved_blocks_percent] "
131 "[-o [^]mount_options[,...]] [-p mmp_update_interval]\n"
132 "\t[-r reserved_blocks_count] [-u user] [-C mount_count] "
133 "[-L volume_label]\n"
134 "\t[-M last_mounted_dir] [-O [^]feature[,...]]\n"
136 "\t[-Q quota_options]\n"
138 "\t[-E extended-option[,...]] [-T last_check_time] "
139 "[-U UUID]\n\t[ -I new_inode_size ] device\n"), program_name);
143 static __u32 ok_features[3] = {
145 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
146 EXT2_FEATURE_COMPAT_DIR_INDEX,
148 EXT2_FEATURE_INCOMPAT_FILETYPE |
149 EXT3_FEATURE_INCOMPAT_EXTENTS |
150 EXT4_FEATURE_INCOMPAT_FLEX_BG |
151 EXT4_FEATURE_INCOMPAT_MMP |
152 EXT4_FEATURE_INCOMPAT_64BIT,
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 |
161 EXT4_FEATURE_RO_COMPAT_QUOTA |
163 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
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 |
183 EXT4_FEATURE_RO_COMPAT_QUOTA |
185 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
189 * Try to get journal super block if any
191 static int get_journal_sb(ext2_filsys jfs, char buf[SUPERBLOCK_SIZE])
194 journal_superblock_t *jsb;
196 if (!(jfs->super->s_feature_incompat &
197 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
198 return EXT2_ET_UNSUPP_FEATURE;
201 /* Get the journal superblock */
202 if ((retval = io_channel_read_blk64(jfs->io,
203 ext2fs_journal_sb_start(jfs->blocksize), -SUPERBLOCK_SIZE, buf))) {
204 com_err(program_name, retval, "%s",
205 _("while reading journal superblock"));
209 jsb = (journal_superblock_t *) buf;
210 if ((jsb->s_header.h_magic != (unsigned)ntohl(JFS_MAGIC_NUMBER)) ||
211 (jsb->s_header.h_blocktype != (unsigned)ntohl(JFS_SUPERBLOCK_V2))) {
212 fputs(_("Journal superblock not found!\n"), stderr);
213 return EXT2_ET_BAD_MAGIC;
219 static __u8 *journal_user(__u8 uuid[UUID_SIZE], __u8 s_users[JFS_USERS_SIZE],
223 for (i = 0; i < nr_users; i++) {
224 if (memcmp(uuid, &s_users[i * UUID_SIZE], UUID_SIZE) == 0)
225 return &s_users[i * UUID_SIZE];
232 * Remove an external journal from the filesystem
234 static int remove_journal_device(ext2_filsys fs)
238 char buf[SUPERBLOCK_SIZE];
239 journal_superblock_t *jsb;
242 int commit_remove_journal = 0;
246 commit_remove_journal = 1; /* force removal even if error */
248 uuid_unparse(fs->super->s_journal_uuid, buf);
249 journal_path = blkid_get_devname(NULL, "UUID", buf);
253 ext2fs_find_block_device(fs->super->s_journal_dev);
255 goto no_valid_journal;
258 #ifdef CONFIG_TESTIO_DEBUG
259 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
260 io_ptr = test_io_manager;
261 test_io_backing_manager = unix_io_manager;
264 io_ptr = unix_io_manager;
265 retval = ext2fs_open(journal_path, EXT2_FLAG_RW|
266 EXT2_FLAG_JOURNAL_DEV_OK, 0,
267 fs->blocksize, io_ptr, &jfs);
269 com_err(program_name, retval, "%s",
270 _("while trying to open external journal"));
271 goto no_valid_journal;
274 if ((retval = get_journal_sb(jfs, buf))) {
275 if (retval == EXT2_ET_UNSUPP_FEATURE)
276 fprintf(stderr, _("%s is not a journal device.\n"),
278 goto no_valid_journal;
281 jsb = (journal_superblock_t *) buf;
282 /* Find the filesystem UUID */
283 nr_users = ntohl(jsb->s_nr_users);
285 if (!journal_user(fs->super->s_uuid, jsb->s_users, nr_users)) {
286 fputs(_("Filesystem's UUID not found on journal device.\n"),
288 commit_remove_journal = 1;
289 goto no_valid_journal;
292 for (i = 0; i < nr_users; i++)
293 memcpy(&jsb->s_users[i * 16], &jsb->s_users[(i + 1) * 16], 16);
294 jsb->s_nr_users = htonl(nr_users);
296 /* Write back the journal superblock */
297 retval = io_channel_write_blk64(jfs->io,
298 ext2fs_journal_sb_start(fs->blocksize),
299 -SUPERBLOCK_SIZE, buf);
301 com_err(program_name, retval,
302 "while writing journal superblock.");
303 goto no_valid_journal;
306 commit_remove_journal = 1;
309 if (commit_remove_journal == 0) {
310 fputs(_("Cannot locate journal device. It was NOT removed\n"
311 "Use -f option to remove missing journal device.\n"),
315 fs->super->s_journal_dev = 0;
316 memset(fs->super->s_jnl_blocks, 0, sizeof(fs->super->s_jnl_blocks));
317 uuid_clear(fs->super->s_journal_uuid);
318 ext2fs_mark_super_dirty(fs);
319 fputs(_("Journal removed\n"), stdout);
325 /* Helper function for remove_journal_inode */
326 static int release_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
327 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
328 blk64_t ref_block EXT2FS_ATTR((unused)),
329 int ref_offset EXT2FS_ATTR((unused)),
330 void *private EXT2FS_ATTR((unused)))
336 ext2fs_unmark_block_bitmap2(fs->block_map, block);
337 group = ext2fs_group_of_blk2(fs, block);
338 ext2fs_bg_free_blocks_count_set(fs, group, ext2fs_bg_free_blocks_count(fs, group) + 1);
339 ext2fs_group_desc_csum_set(fs, group);
340 ext2fs_free_blocks_count_add(fs->super, EXT2FS_CLUSTER_RATIO(fs));
345 * Remove the journal inode from the filesystem
347 static errcode_t remove_journal_inode(ext2_filsys fs)
349 struct ext2_inode inode;
351 ino_t ino = fs->super->s_journal_inum;
353 retval = ext2fs_read_inode(fs, ino, &inode);
355 com_err(program_name, retval, "%s",
356 _("while reading journal inode"));
359 if (ino == EXT2_JOURNAL_INO) {
360 retval = ext2fs_read_bitmaps(fs);
362 com_err(program_name, retval, "%s",
363 _("while reading bitmaps"));
366 retval = ext2fs_block_iterate3(fs, ino,
367 BLOCK_FLAG_READ_ONLY, NULL,
368 release_blocks_proc, NULL);
370 com_err(program_name, retval, "%s",
371 _("while clearing journal inode"));
374 memset(&inode, 0, sizeof(inode));
375 ext2fs_mark_bb_dirty(fs);
376 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
378 inode.i_flags &= ~EXT2_IMMUTABLE_FL;
379 retval = ext2fs_write_inode(fs, ino, &inode);
381 com_err(program_name, retval, "%s",
382 _("while writing journal inode"));
385 fs->super->s_journal_inum = 0;
386 memset(fs->super->s_jnl_blocks, 0, sizeof(fs->super->s_jnl_blocks));
387 ext2fs_mark_super_dirty(fs);
393 * Update the default mount options
395 static int update_mntopts(ext2_filsys fs, char *mntopts)
397 struct ext2_super_block *sb = fs->super;
399 if (e2p_edit_mntopts(mntopts, &sb->s_default_mount_opts, ~0)) {
400 fprintf(stderr, _("Invalid mount option set: %s\n"),
404 ext2fs_mark_super_dirty(fs);
409 static int check_fsck_needed(ext2_filsys fs)
411 if (fs->super->s_state & EXT2_VALID_FS)
413 printf("\n%s\n", _(please_fsck));
414 if (mount_flags & EXT2_MF_READONLY)
415 printf("%s", _("(and reboot afterwards!)\n"));
419 static void request_dir_fsck_afterwards(ext2_filsys fs)
421 static int requested;
426 fs->super->s_state &= ~EXT2_VALID_FS;
427 printf("\n%s\n", _(please_dir_fsck));
428 if (mount_flags & EXT2_MF_READONLY)
429 printf("%s", _("(and reboot afterwards!)\n"));
432 static void request_fsck_afterwards(ext2_filsys fs)
434 static int requested = 0;
439 fs->super->s_state &= ~EXT2_VALID_FS;
440 printf("\n%s\n", _(please_fsck));
441 if (mount_flags & EXT2_MF_READONLY)
442 printf("%s", _("(and reboot afterwards!)\n"));
445 static void convert_64bit(ext2_filsys fs, int direction)
451 * Is resize2fs going to demand a fsck run? Might as well tell the
454 if (!fsck_requested &&
455 ((fs->super->s_state & EXT2_ERROR_FS) ||
456 !(fs->super->s_state & EXT2_VALID_FS) ||
457 fs->super->s_lastcheck < fs->super->s_mtime))
458 request_fsck_afterwards(fs);
460 fprintf(stderr, _("After running e2fsck, please run `resize2fs %s %s"),
461 direction > 0 ? "-b" : "-s", fs->device_name);
463 fprintf(stderr, _("Please run `resize2fs %s %s"),
464 direction > 0 ? "-b" : "-s", fs->device_name);
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 !EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
483 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
486 errcode = ext2fs_extent_open(fs, ino, &handle);
490 errcode = ext2fs_extent_get(handle, EXT2_EXTENT_ROOT, &extent);
495 errcode = ext2fs_extent_get_info(handle, &info);
500 * If this is the first extent in an extent block that we
501 * haven't visited, rewrite the extent to force the ETB
502 * checksum to be rewritten.
504 if (info.curr_entry == 1 && info.curr_level != 0 &&
505 !(extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT)) {
506 errcode = ext2fs_extent_replace(handle, 0, &extent);
511 /* Skip to the end of a block of leaf nodes */
512 if (extent.e_flags & EXT2_EXTENT_FLAGS_LEAF) {
513 errcode = ext2fs_extent_get(handle,
514 EXT2_EXTENT_LAST_SIB,
520 errcode = ext2fs_extent_get(handle, EXT2_EXTENT_NEXT, &extent);
521 } while (errcode == 0);
524 /* Ok if we run off the end */
525 if (errcode == EXT2_ET_EXTENT_NO_NEXT)
527 ext2fs_extent_free(handle);
532 * Rewrite directory blocks with checksums
534 struct rewrite_dir_context {
541 static int rewrite_dir_block(ext2_filsys fs,
543 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
544 blk64_t ref_block EXT2FS_ATTR((unused)),
545 int ref_offset EXT2FS_ATTR((unused)),
548 struct ext2_dx_countlimit *dcl = NULL;
549 struct rewrite_dir_context *ctx = priv_data;
550 int dcl_offset, changed = 0;
552 ctx->errcode = ext2fs_read_dir_block4(fs, *blocknr, ctx->buf, 0,
557 /* if htree node... */
559 ext2fs_get_dx_countlimit(fs, (struct ext2_dir_entry *)ctx->buf,
562 if (!EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
563 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
564 /* Ensure limit is the max size */
565 int max_entries = (fs->blocksize - dcl_offset) /
566 sizeof(struct ext2_dx_entry);
567 if (ext2fs_le16_to_cpu(dcl->limit) != max_entries) {
569 dcl->limit = ext2fs_cpu_to_le16(max_entries);
572 /* If htree block is full then rebuild the dir */
573 if (ext2fs_le16_to_cpu(dcl->count) ==
574 ext2fs_le16_to_cpu(dcl->limit)) {
575 request_dir_fsck_afterwards(fs);
579 * Ensure dcl->limit is small enough to leave room for
582 int max_entries = (fs->blocksize - (dcl_offset +
583 sizeof(struct ext2_dx_tail))) /
584 sizeof(struct ext2_dx_entry);
585 if (ext2fs_le16_to_cpu(dcl->limit) != max_entries)
586 dcl->limit = ext2fs_cpu_to_le16(max_entries);
587 /* Always rewrite checksum */
591 unsigned int rec_len, name_size;
592 char *top = ctx->buf + fs->blocksize;
593 struct ext2_dir_entry *de = (struct ext2_dir_entry *)ctx->buf;
594 struct ext2_dir_entry *last_de = NULL, *penultimate_de = NULL;
596 /* Find last and penultimate dirent */
597 while ((char *)de < top) {
598 penultimate_de = last_de;
600 ctx->errcode = ext2fs_get_rec_len(fs, de, &rec_len);
601 if (!ctx->errcode && !rec_len)
602 ctx->errcode = EXT2_ET_DIR_CORRUPTED;
605 de = (struct ext2_dir_entry *)(((char *)de) + rec_len);
607 ctx->errcode = ext2fs_get_rec_len(fs, last_de, &rec_len);
610 name_size = ext2fs_dirent_name_len(last_de);
612 if (!EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
613 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
616 if (last_de->inode ||
618 rec_len != sizeof(struct ext2_dir_entry_tail))
621 * The last dirent is unused and the right length to
622 * have stored a checksum. Erase it.
624 ctx->errcode = ext2fs_get_rec_len(fs, penultimate_de,
627 ctx->errcode = EXT2_ET_DIR_CORRUPTED;
630 ext2fs_set_rec_len(fs, rec_len +
631 sizeof(struct ext2_dir_entry_tail),
635 unsigned csum_size = sizeof(struct ext2_dir_entry_tail);
636 struct ext2_dir_entry_tail *t;
639 * If the last dirent looks like the tail, just update
642 if (!last_de->inode &&
643 rec_len == csum_size) {
644 t = (struct ext2_dir_entry_tail *)last_de;
645 t->det_reserved_name_len =
646 EXT2_DIR_NAME_LEN_CSUM;
651 name_size = (name_size & ~3) + 4;
652 /* If there's not enough space for the tail, e2fsck */
653 if (rec_len <= (8 + name_size + csum_size)) {
654 request_dir_fsck_afterwards(fs);
657 /* Shorten that last de and insert the tail */
658 ext2fs_set_rec_len(fs, rec_len - csum_size, last_de);
659 t = EXT2_DIRENT_TAIL(ctx->buf, fs->blocksize);
660 ext2fs_initialize_dirent_tail(fs, t);
662 /* Always update checksum */
671 ctx->errcode = ext2fs_write_dir_block4(fs, *blocknr, ctx->buf,
679 static errcode_t rewrite_directory(ext2_filsys fs, ext2_ino_t dir,
680 struct ext2_inode *inode)
683 struct rewrite_dir_context ctx;
685 retval = ext2fs_get_mem(fs->blocksize, &ctx.buf);
689 ctx.is_htree = (inode->i_flags & EXT2_INDEX_FL);
692 retval = ext2fs_block_iterate3(fs, dir, BLOCK_FLAG_READ_ONLY |
693 BLOCK_FLAG_DATA_ONLY,
694 0, rewrite_dir_block, &ctx);
696 ext2fs_free_mem(&ctx.buf);
704 * Forcibly set checksums in all inodes.
706 static void rewrite_inodes(ext2_filsys fs)
708 int length = EXT2_INODE_SIZE(fs->super);
709 struct ext2_inode *inode, *zero;
711 ext2_inode_scan scan;
714 blk64_t file_acl_block;
717 if (fs->super->s_creator_os != EXT2_OS_LINUX)
720 retval = ext2fs_open_inode_scan(fs, 0, &scan);
722 com_err("set_csum", retval, "while opening inode scan");
726 retval = ext2fs_get_mem(length, &inode);
728 com_err("set_csum", retval, "while allocating memory");
732 retval = ext2fs_get_memzero(length, &zero);
734 com_err("set_csum", retval, "while allocating memory");
738 retval = ext2fs_get_mem(fs->blocksize, &ea_buf);
740 com_err("set_csum", retval, "while allocating memory");
745 retval = ext2fs_get_next_inode_full(scan, &ino, inode, length);
747 com_err("set_csum", retval, "while getting next inode");
752 if (ext2fs_test_inode_bitmap2(fs->inode_map, ino)) {
755 if (memcmp(inode, zero, length) != 0) {
756 memset(inode, 0, length);
764 retval = ext2fs_write_inode_full(fs, ino, inode,
767 com_err("set_csum", retval, "while writing "
773 retval = rewrite_extents(fs, ino, inode);
775 com_err("rewrite_extents", retval,
776 "while rewriting extents");
780 if (LINUX_S_ISDIR(inode->i_mode) &&
781 ext2fs_inode_has_valid_blocks2(fs, inode)) {
782 retval = rewrite_directory(fs, ino, inode);
784 com_err("rewrite_directory", retval,
785 "while rewriting directories");
790 file_acl_block = ext2fs_file_acl_block(fs, inode);
793 retval = ext2fs_read_ext_attr3(fs, file_acl_block, ea_buf, ino);
795 com_err("rewrite_eablock", retval,
796 "while rewriting extended attribute");
799 retval = ext2fs_write_ext_attr3(fs, file_acl_block, ea_buf,
802 com_err("rewrite_eablock", retval,
803 "while rewriting extended attribute");
808 ext2fs_free_mem(&zero);
809 ext2fs_free_mem(&inode);
810 ext2fs_free_mem(&ea_buf);
811 ext2fs_close_inode_scan(scan);
814 static void rewrite_metadata_checksums(ext2_filsys fs)
819 fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
820 ext2fs_init_csum_seed(fs);
821 for (i = 0; i < fs->group_desc_count; i++)
822 ext2fs_group_desc_csum_set(fs, i);
823 retval = ext2fs_read_bitmaps(fs);
825 com_err("rewrite_metadata_checksums", retval,
826 "while reading bitmaps");
830 ext2fs_mark_ib_dirty(fs);
831 ext2fs_mark_bb_dirty(fs);
832 ext2fs_mmp_update2(fs, 1);
833 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
834 fs->flags &= ~EXT2_FLAG_IGNORE_CSUM_ERRORS;
835 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
836 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
837 fs->super->s_checksum_type = EXT2_CRC32C_CHKSUM;
839 fs->super->s_checksum_type = 0;
840 ext2fs_mark_super_dirty(fs);
843 static void enable_uninit_bg(ext2_filsys fs)
845 struct ext2_group_desc *gd;
848 for (i = 0; i < fs->group_desc_count; i++) {
849 gd = ext2fs_group_desc(fs, fs->group_desc, i);
850 gd->bg_itable_unused = 0;
851 gd->bg_flags = EXT2_BG_INODE_ZEROED;
852 ext2fs_group_desc_csum_set(fs, i);
854 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
857 static errcode_t zero_empty_inodes(ext2_filsys fs)
859 int length = EXT2_INODE_SIZE(fs->super);
860 struct ext2_inode *inode = NULL;
861 ext2_inode_scan scan;
865 retval = ext2fs_open_inode_scan(fs, 0, &scan);
869 retval = ext2fs_get_mem(length, &inode);
874 retval = ext2fs_get_next_inode_full(scan, &ino, inode, length);
879 if (!ext2fs_test_inode_bitmap2(fs->inode_map, ino)) {
880 memset(inode, 0, length);
881 retval = ext2fs_write_inode_full(fs, ino, inode,
889 ext2fs_free_mem(&inode);
890 ext2fs_close_inode_scan(scan);
894 static errcode_t disable_uninit_bg(ext2_filsys fs, __u32 csum_feature_flag)
896 struct ext2_group_desc *gd;
901 /* Load bitmaps to ensure that the uninit ones get written out */
902 fs->super->s_feature_ro_compat |= csum_feature_flag;
903 retval = ext2fs_read_bitmaps(fs);
904 fs->super->s_feature_ro_compat &= ~csum_feature_flag;
906 com_err("disable_uninit_bg", retval,
907 "while reading bitmaps");
908 request_fsck_afterwards(fs);
911 ext2fs_mark_ib_dirty(fs);
912 ext2fs_mark_bb_dirty(fs);
914 /* If we're only turning off uninit_bg, zero the inodes */
915 if (csum_feature_flag == EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
916 retval = zero_empty_inodes(fs);
918 com_err("disable_uninit_bg", retval,
919 "while zeroing unused inodes");
920 request_fsck_afterwards(fs);
925 /* The bbitmap is zeroed; we must mark group metadata blocks in use */
926 for (i = 0; i < fs->group_desc_count; i++) {
927 b = ext2fs_block_bitmap_loc(fs, i);
928 ext2fs_mark_block_bitmap2(fs->block_map, b);
929 b = ext2fs_inode_bitmap_loc(fs, i);
930 ext2fs_mark_block_bitmap2(fs->block_map, b);
932 retval = ext2fs_super_and_bgd_loc2(fs, i, &b, &c, &d, NULL);
933 if (retval == 0 && b)
934 ext2fs_mark_block_bitmap2(fs->block_map, b);
935 if (retval == 0 && c)
936 ext2fs_mark_block_bitmap2(fs->block_map, c);
937 if (retval == 0 && d)
938 ext2fs_mark_block_bitmap2(fs->block_map, d);
940 com_err("disable_uninit_bg", retval,
941 "while initializing block bitmaps");
942 request_fsck_afterwards(fs);
945 gd = ext2fs_group_desc(fs, fs->group_desc, i);
946 gd->bg_itable_unused = 0;
948 ext2fs_group_desc_csum_set(fs, i);
950 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
951 ext2fs_mark_super_dirty(fs);
957 * Update the feature set as provided by the user.
959 static int update_feature_set(ext2_filsys fs, char *features)
961 struct ext2_super_block *sb = fs->super;
962 __u32 old_features[3];
964 unsigned int mask_err;
967 #define FEATURE_ON(type, mask) (!(old_features[(type)] & (mask)) && \
968 ((&sb->s_feature_compat)[(type)] & (mask)))
969 #define FEATURE_OFF(type, mask) ((old_features[(type)] & (mask)) && \
970 !((&sb->s_feature_compat)[(type)] & (mask)))
971 #define FEATURE_CHANGED(type, mask) ((mask) & \
972 (old_features[(type)] ^ (&sb->s_feature_compat)[(type)]))
974 old_features[E2P_FEATURE_COMPAT] = sb->s_feature_compat;
975 old_features[E2P_FEATURE_INCOMPAT] = sb->s_feature_incompat;
976 old_features[E2P_FEATURE_RO_INCOMPAT] = sb->s_feature_ro_compat;
978 if (e2p_edit_feature2(features, &sb->s_feature_compat,
979 ok_features, clear_ok_features,
980 &type_err, &mask_err)) {
983 _("Invalid filesystem option set: %s\n"),
985 else if (type_err & E2P_FEATURE_NEGATE_FLAG)
986 fprintf(stderr, _("Clearing filesystem feature '%s' "
988 e2p_feature2string(type_err &
989 E2P_FEATURE_TYPE_MASK,
992 fprintf(stderr, _("Setting filesystem feature '%s' "
994 e2p_feature2string(type_err, mask_err));
998 if (FEATURE_OFF(E2P_FEATURE_COMPAT, EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
999 if ((mount_flags & EXT2_MF_MOUNTED) &&
1000 !(mount_flags & EXT2_MF_READONLY)) {
1001 fputs(_("The has_journal feature may only be "
1002 "cleared when the filesystem is\n"
1003 "unmounted or mounted "
1004 "read-only.\n"), stderr);
1007 if ((sb->s_feature_incompat &
1008 EXT3_FEATURE_INCOMPAT_RECOVER) &&
1010 fputs(_("The needs_recovery flag is set. "
1011 "Please run e2fsck before clearing\n"
1012 "the has_journal flag.\n"), stderr);
1015 if (sb->s_journal_inum) {
1016 if (remove_journal_inode(fs))
1019 if (sb->s_journal_dev) {
1020 if (remove_journal_device(fs))
1025 if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT,
1026 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
1027 if (sb->s_feature_incompat &
1028 EXT2_FEATURE_INCOMPAT_META_BG) {
1029 fputs(_("Setting filesystem feature 'sparse_super' "
1030 "not supported\nfor filesystems with "
1031 "the meta_bg feature enabled.\n"),
1037 if (FEATURE_ON(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_MMP)) {
1040 if ((mount_flags & EXT2_MF_MOUNTED) ||
1041 (mount_flags & EXT2_MF_READONLY)) {
1042 fputs(_("The multiple mount protection feature can't\n"
1043 "be set if the filesystem is mounted or\n"
1044 "read-only.\n"), stderr);
1048 error = ext2fs_mmp_init(fs);
1050 fputs(_("\nError while enabling multiple mount "
1051 "protection feature."), stderr);
1056 * We want to update group desc with the new free blocks count
1058 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
1060 printf(_("Multiple mount protection has been enabled "
1061 "with update interval %ds.\n"),
1062 sb->s_mmp_update_interval);
1065 if (FEATURE_OFF(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_MMP)) {
1068 if (mount_flags & EXT2_MF_READONLY) {
1069 fputs(_("The multiple mount protection feature cannot\n"
1070 "be disabled if the filesystem is readonly.\n"),
1075 error = ext2fs_read_bitmaps(fs);
1077 fputs(_("Error while reading bitmaps\n"), stderr);
1081 error = ext2fs_mmp_read(fs, sb->s_mmp_block, NULL);
1083 struct mmp_struct *mmp_cmp = fs->mmp_cmp;
1085 if (error == EXT2_ET_MMP_MAGIC_INVALID)
1086 printf(_("Magic number in MMP block does not "
1087 "match. expected: %x, actual: %x\n"),
1088 EXT4_MMP_MAGIC, mmp_cmp->mmp_magic);
1090 com_err(program_name, error, "%s",
1091 _("while reading MMP block."));
1095 /* We need to force out the group descriptors as well */
1096 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
1097 ext2fs_block_alloc_stats2(fs, sb->s_mmp_block, -1);
1099 sb->s_mmp_block = 0;
1100 sb->s_mmp_update_interval = 0;
1103 if (FEATURE_ON(E2P_FEATURE_COMPAT, EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
1105 * If adding a journal flag, let the create journal
1106 * code below handle setting the flag and creating the
1107 * journal. We supply a default size if necessary.
1111 sb->s_feature_compat &= ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1114 if (FEATURE_ON(E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_DIR_INDEX)) {
1115 if (!sb->s_def_hash_version)
1116 sb->s_def_hash_version = EXT2_HASH_HALF_MD4;
1117 if (uuid_is_null((unsigned char *) sb->s_hash_seed))
1118 uuid_generate((unsigned char *) sb->s_hash_seed);
1121 if (FEATURE_OFF(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
1122 if (ext2fs_check_desc(fs)) {
1123 fputs(_("Clearing the flex_bg flag would "
1124 "cause the the filesystem to be\n"
1125 "inconsistent.\n"), stderr);
1130 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
1131 EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
1132 if ((mount_flags & EXT2_MF_MOUNTED) &&
1133 !(mount_flags & EXT2_MF_READONLY)) {
1134 fputs(_("The huge_file feature may only be "
1135 "cleared when the filesystem is\n"
1136 "unmounted or mounted "
1137 "read-only.\n"), stderr);
1142 if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT,
1143 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
1144 if (check_fsck_needed(fs))
1146 if (mount_flags & EXT2_MF_MOUNTED) {
1147 fputs(_("Cannot enable metadata_csum on a mounted "
1148 "filesystem!\n"), stderr);
1151 if (!EXT2_HAS_INCOMPAT_FEATURE(fs->super,
1152 EXT3_FEATURE_INCOMPAT_EXTENTS))
1154 _("Extents are not enabled. The file extent "
1155 "tree can be checksummed, whereas block maps "
1156 "cannot. Not enabling extents reduces the "
1157 "coverage of metadata checksumming. "
1158 "Re-run with -O extent to rectify.\n"));
1159 if (!EXT2_HAS_INCOMPAT_FEATURE(fs->super,
1160 EXT4_FEATURE_INCOMPAT_64BIT))
1162 _("64-bit filesystem support is not enabled. "
1163 "The larger fields afforded by this feature "
1164 "enable full-strength checksumming. "
1165 "Run resize2fs -b to rectify.\n"));
1166 rewrite_checksums = 1;
1167 /* metadata_csum supersedes uninit_bg */
1168 fs->super->s_feature_ro_compat &=
1169 ~EXT4_FEATURE_RO_COMPAT_GDT_CSUM;
1171 /* if uninit_bg was previously off, rewrite group desc */
1172 if (!(old_features[E2P_FEATURE_RO_INCOMPAT] &
1173 EXT4_FEATURE_RO_COMPAT_GDT_CSUM))
1174 enable_uninit_bg(fs);
1177 * Since metadata_csum supersedes uninit_bg, pretend like
1178 * uninit_bg has been off all along.
1180 old_features[E2P_FEATURE_RO_INCOMPAT] &=
1181 ~EXT4_FEATURE_RO_COMPAT_GDT_CSUM;
1184 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
1185 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
1186 __u32 test_features[3];
1188 if (check_fsck_needed(fs))
1190 if (mount_flags & EXT2_MF_MOUNTED) {
1191 fputs(_("Cannot disable metadata_csum on a mounted "
1192 "filesystem!\n"), stderr);
1195 rewrite_checksums = 1;
1197 /* Enable uninit_bg unless the user expressly turned it off */
1198 memcpy(test_features, old_features, sizeof(test_features));
1199 test_features[E2P_FEATURE_RO_INCOMPAT] |=
1200 EXT4_FEATURE_RO_COMPAT_GDT_CSUM;
1201 e2p_edit_feature2(features, test_features, ok_features,
1202 clear_ok_features, NULL, NULL);
1203 if (test_features[E2P_FEATURE_RO_INCOMPAT] &
1204 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)
1205 fs->super->s_feature_ro_compat |=
1206 EXT4_FEATURE_RO_COMPAT_GDT_CSUM;
1209 * If we're turning off metadata_csum and not turning on
1210 * uninit_bg, rewrite group desc.
1212 if (!(fs->super->s_feature_ro_compat &
1213 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
1214 err = disable_uninit_bg(fs,
1215 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM);
1220 * metadata_csum previously provided uninit_bg, so if
1221 * we're also setting the uninit_bg feature bit,
1222 * pretend like it was previously enabled. Checksums
1223 * will be rewritten with crc16 later.
1225 old_features[E2P_FEATURE_RO_INCOMPAT] |=
1226 EXT4_FEATURE_RO_COMPAT_GDT_CSUM;
1229 if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT,
1230 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
1231 /* Do not enable uninit_bg when metadata_csum enabled */
1232 if (fs->super->s_feature_ro_compat &
1233 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)
1234 fs->super->s_feature_ro_compat &=
1235 ~EXT4_FEATURE_RO_COMPAT_GDT_CSUM;
1237 enable_uninit_bg(fs);
1240 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
1241 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
1242 err = disable_uninit_bg(fs,
1243 EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
1249 * We don't actually toggle 64bit; resize2fs does that. But this
1250 * must come after the metadata_csum feature_on so that it won't
1251 * complain about the lack of 64bit.
1253 if (FEATURE_ON(E2P_FEATURE_INCOMPAT,
1254 EXT4_FEATURE_INCOMPAT_64BIT)) {
1255 if (mount_flags & EXT2_MF_MOUNTED) {
1256 fprintf(stderr, _("Cannot enable 64-bit mode "
1257 "while mounted!\n"));
1260 sb->s_feature_incompat &= ~EXT4_FEATURE_INCOMPAT_64BIT;
1263 if (FEATURE_OFF(E2P_FEATURE_INCOMPAT,
1264 EXT4_FEATURE_INCOMPAT_64BIT)) {
1265 if (mount_flags & EXT2_MF_MOUNTED) {
1266 fprintf(stderr, _("Cannot disable 64-bit mode "
1267 "while mounted!\n"));
1270 sb->s_feature_incompat |= EXT4_FEATURE_INCOMPAT_64BIT;
1274 if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT,
1275 EXT4_FEATURE_RO_COMPAT_QUOTA)) {
1277 * Set the Q_flag here and handle the quota options in the code
1282 /* Enable both user quota and group quota by default */
1283 usrquota = QOPT_ENABLE;
1284 grpquota = QOPT_ENABLE;
1286 sb->s_feature_ro_compat &= ~EXT4_FEATURE_RO_COMPAT_QUOTA;
1289 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
1290 EXT4_FEATURE_RO_COMPAT_QUOTA)) {
1292 * Set the Q_flag here and handle the quota options in the code
1296 fputs(_("\nWarning: '^quota' option overrides '-Q'"
1297 "arguments.\n"), stderr);
1299 /* Disable both user quota and group quota by default */
1300 usrquota = QOPT_DISABLE;
1301 grpquota = QOPT_DISABLE;
1304 if (sb->s_rev_level == EXT2_GOOD_OLD_REV &&
1305 (sb->s_feature_compat || sb->s_feature_ro_compat ||
1306 sb->s_feature_incompat))
1307 ext2fs_update_dynamic_rev(fs);
1309 if (FEATURE_CHANGED(E2P_FEATURE_RO_INCOMPAT,
1310 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER) ||
1311 FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
1312 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) ||
1313 FEATURE_CHANGED(E2P_FEATURE_INCOMPAT,
1314 EXT2_FEATURE_INCOMPAT_FILETYPE) ||
1315 FEATURE_CHANGED(E2P_FEATURE_COMPAT,
1316 EXT2_FEATURE_COMPAT_RESIZE_INODE) ||
1317 FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
1318 EXT2_FEATURE_RO_COMPAT_LARGE_FILE))
1319 request_fsck_afterwards(fs);
1321 if ((old_features[E2P_FEATURE_COMPAT] != sb->s_feature_compat) ||
1322 (old_features[E2P_FEATURE_INCOMPAT] != sb->s_feature_incompat) ||
1323 (old_features[E2P_FEATURE_RO_INCOMPAT] != sb->s_feature_ro_compat))
1324 ext2fs_mark_super_dirty(fs);
1330 * Add a journal to the filesystem.
1332 static int add_journal(ext2_filsys fs)
1334 unsigned long journal_blocks;
1339 if (fs->super->s_feature_compat &
1340 EXT3_FEATURE_COMPAT_HAS_JOURNAL) {
1341 fputs(_("The filesystem already has a journal.\n"), stderr);
1344 if (journal_device) {
1345 if (!check_plausibility(journal_device, CHECK_BLOCK_DEV,
1347 proceed_question(-1);
1348 check_mount(journal_device, 0, _("journal"));
1349 #ifdef CONFIG_TESTIO_DEBUG
1350 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
1351 io_ptr = test_io_manager;
1352 test_io_backing_manager = unix_io_manager;
1355 io_ptr = unix_io_manager;
1356 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
1357 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1358 fs->blocksize, io_ptr, &jfs);
1360 com_err(program_name, retval,
1361 _("\n\twhile trying to open journal on %s\n"),
1365 printf(_("Creating journal on device %s: "),
1369 retval = ext2fs_add_journal_device(fs, jfs);
1370 ext2fs_close_free(&jfs);
1372 com_err(program_name, retval,
1373 _("while adding filesystem to journal on %s"),
1377 fputs(_("done\n"), stdout);
1378 } else if (journal_size) {
1379 fputs(_("Creating journal inode: "), stdout);
1381 journal_blocks = figure_journal_size(journal_size, fs);
1383 if (journal_location_string)
1385 parse_num_blocks2(journal_location_string,
1386 fs->super->s_log_block_size);
1387 retval = ext2fs_add_journal_inode2(fs, journal_blocks,
1391 fprintf(stderr, "\n");
1392 com_err(program_name, retval, "%s",
1393 _("\n\twhile trying to create journal file"));
1396 fputs(_("done\n"), stdout);
1398 * If the filesystem wasn't mounted, we need to force
1399 * the block group descriptors out.
1401 if ((mount_flags & EXT2_MF_MOUNTED) == 0)
1402 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
1404 print_check_message(fs->super->s_max_mnt_count,
1405 fs->super->s_checkinterval);
1409 free(journal_device);
1413 static void handle_quota_options(ext2_filsys fs)
1418 if (!usrquota && !grpquota)
1419 /* Nothing to do. */
1422 quota_init_context(&qctx, fs, -1);
1424 if (usrquota == QOPT_ENABLE || grpquota == QOPT_ENABLE)
1425 quota_compute_usage(qctx);
1427 if (usrquota == QOPT_ENABLE && !fs->super->s_usr_quota_inum) {
1428 if ((qf_ino = quota_file_exists(fs, USRQUOTA,
1430 quota_update_limits(qctx, qf_ino, USRQUOTA);
1431 quota_write_inode(qctx, USRQUOTA);
1432 } else if (usrquota == QOPT_DISABLE) {
1433 quota_remove_inode(fs, USRQUOTA);
1436 if (grpquota == QOPT_ENABLE && !fs->super->s_grp_quota_inum) {
1437 if ((qf_ino = quota_file_exists(fs, GRPQUOTA,
1439 quota_update_limits(qctx, qf_ino, GRPQUOTA);
1440 quota_write_inode(qctx, GRPQUOTA);
1441 } else if (grpquota == QOPT_DISABLE) {
1442 quota_remove_inode(fs, GRPQUOTA);
1445 quota_release_context(&qctx);
1447 if ((usrquota == QOPT_ENABLE) || (grpquota == QOPT_ENABLE)) {
1448 fs->super->s_feature_ro_compat |= EXT4_FEATURE_RO_COMPAT_QUOTA;
1449 ext2fs_mark_super_dirty(fs);
1450 } else if (!fs->super->s_usr_quota_inum &&
1451 !fs->super->s_grp_quota_inum) {
1452 fs->super->s_feature_ro_compat &= ~EXT4_FEATURE_RO_COMPAT_QUOTA;
1453 ext2fs_mark_super_dirty(fs);
1460 static void parse_quota_opts(const char *opts)
1462 char *buf, *token, *next, *p;
1466 buf = malloc(len+1);
1468 fputs(_("Couldn't allocate memory to parse quota "
1469 "options!\n"), stderr);
1473 for (token = buf; token && *token; token = next) {
1474 p = strchr(token, ',');
1481 if (strcmp(token, "usrquota") == 0) {
1482 usrquota = QOPT_ENABLE;
1483 } else if (strcmp(token, "^usrquota") == 0) {
1484 usrquota = QOPT_DISABLE;
1485 } else if (strcmp(token, "grpquota") == 0) {
1486 grpquota = QOPT_ENABLE;
1487 } else if (strcmp(token, "^grpquota") == 0) {
1488 grpquota = QOPT_DISABLE;
1490 fputs(_("\nBad quota options specified.\n\n"
1491 "Following valid quota options are available "
1492 "(pass by separating with comma):\n"
1504 static void parse_e2label_options(int argc, char ** argv)
1506 if ((argc < 2) || (argc > 3)) {
1507 fputs(_("Usage: e2label device [newlabel]\n"), stderr);
1510 io_options = strchr(argv[1], '?');
1513 device_name = blkid_get_devname(NULL, argv[1], NULL);
1515 com_err("e2label", 0, _("Unable to resolve '%s'"),
1519 open_flag = EXT2_FLAG_JOURNAL_DEV_OK;
1521 open_flag |= EXT2_FLAG_RW;
1523 new_label = argv[2];
1528 static time_t parse_time(char *str)
1532 if (strcmp(str, "now") == 0) {
1535 memset(&ts, 0, sizeof(ts));
1536 #ifdef HAVE_STRPTIME
1537 strptime(str, "%Y%m%d%H%M%S", &ts);
1539 sscanf(str, "%4d%2d%2d%2d%2d%2d", &ts.tm_year, &ts.tm_mon,
1540 &ts.tm_mday, &ts.tm_hour, &ts.tm_min, &ts.tm_sec);
1543 if (ts.tm_year < 0 || ts.tm_mon < 0 || ts.tm_mon > 11 ||
1544 ts.tm_mday < 0 || ts.tm_mday > 31 || ts.tm_hour > 23 ||
1545 ts.tm_min > 59 || ts.tm_sec > 61)
1548 if (ts.tm_mday == 0) {
1549 com_err(program_name, 0,
1550 _("Couldn't parse date/time specifier: %s"),
1555 return (mktime(&ts));
1558 static void parse_tune2fs_options(int argc, char **argv)
1564 char optstring[100] = "c:e:fg:i:jlm:o:r:s:u:C:E:I:J:L:M:O:T:U:";
1567 strcat(optstring, "Q:");
1571 printf("tune2fs %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
1572 while ((c = getopt(argc, argv, optstring)) != EOF)
1575 max_mount_count = strtol(optarg, &tmp, 0);
1576 if (*tmp || max_mount_count > 16000) {
1577 com_err(program_name, 0,
1578 _("bad mounts count - %s"),
1582 if (max_mount_count == 0)
1583 max_mount_count = -1;
1585 open_flag = EXT2_FLAG_RW;
1588 mount_count = strtoul(optarg, &tmp, 0);
1589 if (*tmp || mount_count > 16000) {
1590 com_err(program_name, 0,
1591 _("bad mounts count - %s"),
1596 open_flag = EXT2_FLAG_RW;
1599 if (strcmp(optarg, "continue") == 0)
1600 errors = EXT2_ERRORS_CONTINUE;
1601 else if (strcmp(optarg, "remount-ro") == 0)
1602 errors = EXT2_ERRORS_RO;
1603 else if (strcmp(optarg, "panic") == 0)
1604 errors = EXT2_ERRORS_PANIC;
1606 com_err(program_name, 0,
1607 _("bad error behavior - %s"),
1612 open_flag = EXT2_FLAG_RW;
1615 extended_cmd = optarg;
1616 open_flag |= EXT2_FLAG_RW;
1618 case 'f': /* Force */
1622 resgid = strtoul(optarg, &tmp, 0);
1624 gr = getgrnam(optarg);
1628 resgid = gr->gr_gid;
1633 com_err(program_name, 0,
1634 _("bad gid/group name - %s"),
1639 open_flag = EXT2_FLAG_RW;
1642 interval = strtoul(optarg, &tmp, 0);
1649 case 'D': /* days */
1655 case 'M': /* months! */
1656 interval *= 86400 * 30;
1660 case 'W': /* weeks */
1661 interval *= 86400 * 7;
1666 com_err(program_name, 0,
1667 _("bad interval - %s"), optarg);
1671 open_flag = EXT2_FLAG_RW;
1676 open_flag = EXT2_FLAG_RW;
1679 parse_journal_opts(optarg);
1680 open_flag = EXT2_FLAG_RW;
1688 open_flag |= EXT2_FLAG_RW |
1689 EXT2_FLAG_JOURNAL_DEV_OK;
1692 reserved_ratio = strtod(optarg, &tmp);
1693 if (*tmp || reserved_ratio > 50 ||
1694 reserved_ratio < 0) {
1695 com_err(program_name, 0,
1696 _("bad reserved block ratio - %s"),
1701 open_flag = EXT2_FLAG_RW;
1704 new_last_mounted = optarg;
1706 open_flag = EXT2_FLAG_RW;
1710 com_err(program_name, 0, "%s",
1711 _("-o may only be specified once"));
1714 mntopts_cmd = optarg;
1715 open_flag = EXT2_FLAG_RW;
1719 com_err(program_name, 0, "%s",
1720 _("-O may only be specified once"));
1723 features_cmd = optarg;
1724 open_flag = EXT2_FLAG_RW;
1729 parse_quota_opts(optarg);
1730 open_flag = EXT2_FLAG_RW;
1734 reserved_blocks = strtoul(optarg, &tmp, 0);
1736 com_err(program_name, 0,
1737 _("bad reserved blocks count - %s"),
1742 open_flag = EXT2_FLAG_RW;
1744 case 's': /* Deprecated */
1745 s_flag = atoi(optarg);
1746 open_flag = EXT2_FLAG_RW;
1750 last_check_time = parse_time(optarg);
1751 open_flag = EXT2_FLAG_RW;
1754 resuid = strtoul(optarg, &tmp, 0);
1756 pw = getpwnam(optarg);
1760 resuid = pw->pw_uid;
1765 com_err(program_name, 0,
1766 _("bad uid/user name - %s"),
1771 open_flag = EXT2_FLAG_RW;
1776 open_flag = EXT2_FLAG_RW |
1777 EXT2_FLAG_JOURNAL_DEV_OK;
1780 new_inode_size = strtoul(optarg, &tmp, 0);
1782 com_err(program_name, 0,
1783 _("bad inode size - %s"),
1787 if (!((new_inode_size &
1788 (new_inode_size - 1)) == 0)) {
1789 com_err(program_name, 0,
1790 _("Inode size must be a "
1791 "power of two- %s"),
1795 open_flag = EXT2_FLAG_RW;
1801 if (optind < argc - 1 || optind == argc)
1803 if (!open_flag && !l_flag)
1805 io_options = strchr(argv[optind], '?');
1808 device_name = blkid_get_devname(NULL, argv[optind], NULL);
1810 com_err(program_name, 0, _("Unable to resolve '%s'"),
1816 #ifdef CONFIG_BUILD_FINDFS
1817 void do_findfs(int argc, char **argv)
1822 (strncmp(argv[1], "LABEL=", 6) && strncmp(argv[1], "UUID=", 5))) {
1823 fprintf(stderr, "Usage: findfs LABEL=<label>|UUID=<uuid>\n");
1826 dev = blkid_get_devname(NULL, argv[1], NULL);
1828 com_err("findfs", 0, _("Unable to resolve '%s'"),
1837 static int parse_extended_opts(ext2_filsys fs, const char *opts)
1839 char *buf, *token, *next, *p, *arg;
1844 buf = malloc(len+1);
1846 fprintf(stderr, "%s",
1847 _("Couldn't allocate memory to parse options!\n"));
1851 for (token = buf; token && *token; token = next) {
1852 p = strchr(token, ',');
1858 arg = strchr(token, '=');
1863 if (strcmp(token, "clear-mmp") == 0 ||
1864 strcmp(token, "clear_mmp") == 0) {
1866 } else if (strcmp(token, "mmp_update_interval") == 0) {
1872 intv = strtoul(arg, &p, 0);
1875 _("Invalid mmp_update_interval: %s\n"),
1881 intv = EXT4_MMP_UPDATE_INTERVAL;
1882 } else if (intv > EXT4_MMP_MAX_UPDATE_INTERVAL) {
1884 _("mmp_update_interval too big: %lu\n"),
1889 printf(P_("Setting multiple mount protection update "
1890 "interval to %lu second\n",
1891 "Setting multiple mount protection update "
1892 "interval to %lu seconds\n", intv),
1894 fs->super->s_mmp_update_interval = intv;
1895 ext2fs_mark_super_dirty(fs);
1896 } else if (!strcmp(token, "test_fs")) {
1897 fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
1898 printf("Setting test filesystem flag\n");
1899 ext2fs_mark_super_dirty(fs);
1900 } else if (!strcmp(token, "^test_fs")) {
1901 fs->super->s_flags &= ~EXT2_FLAGS_TEST_FILESYS;
1902 printf("Clearing test filesystem flag\n");
1903 ext2fs_mark_super_dirty(fs);
1904 } else if (strcmp(token, "stride") == 0) {
1909 stride = strtoul(arg, &p, 0);
1912 _("Invalid RAID stride: %s\n"),
1918 } else if (strcmp(token, "stripe-width") == 0 ||
1919 strcmp(token, "stripe_width") == 0) {
1924 stripe_width = strtoul(arg, &p, 0);
1927 _("Invalid RAID stripe-width: %s\n"),
1932 stripe_width_set = 1;
1933 } else if (strcmp(token, "hash_alg") == 0 ||
1934 strcmp(token, "hash-alg") == 0) {
1939 hash_alg = e2p_string2hash(arg);
1942 _("Invalid hash algorithm: %s\n"),
1947 fs->super->s_def_hash_version = hash_alg;
1948 printf(_("Setting default hash algorithm "
1951 ext2fs_mark_super_dirty(fs);
1952 } else if (!strcmp(token, "mount_opts")) {
1957 if (strlen(arg) >= sizeof(fs->super->s_mount_opts)) {
1959 "Extended mount options too long\n");
1962 ext_mount_opts = strdup(arg);
1967 fprintf(stderr, "%s", _("\nBad options specified.\n\n"
1968 "Extended options are separated by commas, "
1969 "and may take an argument which\n"
1970 "\tis set off by an equals ('=') sign.\n\n"
1971 "Valid extended options are:\n"
1973 "\thash_alg=<hash algorithm>\n"
1974 "\tmount_opts=<extended default mount options>\n"
1975 "\tstride=<RAID per-disk chunk size in blocks>\n"
1976 "\tstripe_width=<RAID stride*data disks in blocks>\n"
1988 * Fill in the block bitmap bmap with the information regarding the
1989 * blocks to be moved
1991 static int get_move_bitmaps(ext2_filsys fs, int new_ino_blks_per_grp,
1992 ext2fs_block_bitmap bmap)
1996 ext2_badblocks_list bb_list = 0;
1997 blk64_t j, needed_blocks = 0;
1998 blk64_t start_blk, end_blk;
2000 retval = ext2fs_read_bb_inode(fs, &bb_list);
2004 for (i = 0; i < fs->group_desc_count; i++) {
2005 start_blk = ext2fs_inode_table_loc(fs, i) +
2006 fs->inode_blocks_per_group;
2008 end_blk = ext2fs_inode_table_loc(fs, i) +
2009 new_ino_blks_per_grp;
2011 for (j = start_blk; j < end_blk; j++) {
2012 if (ext2fs_test_block_bitmap2(fs->block_map, j)) {
2014 * IF the block is a bad block we fail
2016 if (ext2fs_badblocks_list_test(bb_list, j)) {
2017 ext2fs_badblocks_list_free(bb_list);
2021 ext2fs_mark_block_bitmap2(bmap, j);
2024 * We are going to use this block for
2025 * inode table. So mark them used.
2027 ext2fs_mark_block_bitmap2(fs->block_map, j);
2030 needed_blocks += end_blk - start_blk;
2033 ext2fs_badblocks_list_free(bb_list);
2034 if (needed_blocks > ext2fs_free_blocks_count(fs->super))
2040 static int ext2fs_is_meta_block(ext2_filsys fs, blk64_t blk)
2043 group = ext2fs_group_of_blk2(fs, blk);
2044 if (ext2fs_block_bitmap_loc(fs, group) == blk)
2046 if (ext2fs_inode_bitmap_loc(fs, group) == blk)
2051 static int ext2fs_is_block_in_group(ext2_filsys fs, dgrp_t group, blk64_t blk)
2053 blk64_t start_blk, end_blk;
2054 start_blk = fs->super->s_first_data_block +
2055 EXT2_GROUPS_TO_BLOCKS(fs->super, group);
2057 * We cannot get new block beyond end_blk for for the last block group
2058 * so we can check with EXT2_BLOCKS_PER_GROUP even for last block group
2060 end_blk = start_blk + EXT2_BLOCKS_PER_GROUP(fs->super);
2061 if (blk >= start_blk && blk <= end_blk)
2066 static int move_block(ext2_filsys fs, ext2fs_block_bitmap bmap)
2073 blk64_t blk, new_blk, goal;
2074 struct blk_move *bmv;
2076 retval = ext2fs_get_mem(fs->blocksize, &buf);
2080 for (new_blk = blk = fs->super->s_first_data_block;
2081 blk < ext2fs_blocks_count(fs->super); blk++) {
2082 if (!ext2fs_test_block_bitmap2(bmap, blk))
2085 if (ext2fs_is_meta_block(fs, blk)) {
2087 * If the block is mapping a fs meta data block
2088 * like group desc/block bitmap/inode bitmap. We
2089 * should find a block in the same group and fix
2090 * the respective fs metadata pointers. Otherwise
2093 group = ext2fs_group_of_blk2(fs, blk);
2094 goal = ext2fs_group_first_block2(fs, group);
2100 retval = ext2fs_new_block2(fs, goal, NULL, &new_blk);
2104 /* new fs meta data block should be in the same group */
2105 if (meta_data && !ext2fs_is_block_in_group(fs, group, new_blk)) {
2110 /* Mark this block as allocated */
2111 ext2fs_mark_block_bitmap2(fs->block_map, new_blk);
2113 /* Add it to block move list */
2114 retval = ext2fs_get_mem(sizeof(struct blk_move), &bmv);
2119 bmv->new_loc = new_blk;
2121 list_add(&(bmv->list), &blk_move_list);
2123 retval = io_channel_read_blk64(fs->io, blk, 1, buf);
2127 retval = io_channel_write_blk64(fs->io, new_blk, 1, buf);
2133 ext2fs_free_mem(&buf);
2137 static blk64_t translate_block(blk64_t blk)
2139 struct list_head *entry;
2140 struct blk_move *bmv;
2142 list_for_each(entry, &blk_move_list) {
2143 bmv = list_entry(entry, struct blk_move, list);
2144 if (bmv->old_loc == blk)
2145 return bmv->new_loc;
2151 static int process_block(ext2_filsys fs EXT2FS_ATTR((unused)),
2153 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
2154 blk64_t ref_block EXT2FS_ATTR((unused)),
2155 int ref_offset EXT2FS_ATTR((unused)),
2160 ext2fs_block_bitmap bmap = (ext2fs_block_bitmap) priv_data;
2162 if (!ext2fs_test_block_bitmap2(bmap, *block_nr))
2164 new_blk = translate_block(*block_nr);
2166 *block_nr = new_blk;
2168 * This will force the ext2fs_write_inode in the iterator
2170 ret |= BLOCK_CHANGED;
2176 static int inode_scan_and_fix(ext2_filsys fs, ext2fs_block_bitmap bmap)
2178 errcode_t retval = 0;
2181 char *block_buf = 0;
2182 struct ext2_inode inode;
2183 ext2_inode_scan scan = NULL;
2185 retval = ext2fs_get_mem(fs->blocksize * 3, &block_buf);
2189 retval = ext2fs_open_inode_scan(fs, 0, &scan);
2194 retval = ext2fs_get_next_inode(scan, &ino, &inode);
2201 if (inode.i_links_count == 0)
2202 continue; /* inode not in use */
2205 * If we end up modifying the journal inode
2206 * the sb->s_jnl_blocks will differ. But a
2207 * subsequent e2fsck fixes that.
2208 * Do we need to fix this ??
2211 if (ext2fs_file_acl_block(fs, &inode) &&
2212 ext2fs_test_block_bitmap2(bmap,
2213 ext2fs_file_acl_block(fs, &inode))) {
2214 blk = translate_block(ext2fs_file_acl_block(fs,
2219 ext2fs_file_acl_block_set(fs, &inode, blk);
2222 * Write the inode to disk so that inode table
2225 retval = ext2fs_write_inode(fs, ino, &inode);
2230 if (!ext2fs_inode_has_valid_blocks2(fs, &inode))
2233 retval = ext2fs_block_iterate3(fs, ino, 0, block_buf,
2234 process_block, bmap);
2241 ext2fs_free_mem(&block_buf);
2242 ext2fs_close_inode_scan(scan);
2248 * We need to scan for inode and block bitmaps that may need to be
2249 * moved. This can take place if the filesystem was formatted for
2250 * RAID arrays using the mke2fs's extended option "stride".
2252 static int group_desc_scan_and_fix(ext2_filsys fs, ext2fs_block_bitmap bmap)
2255 blk64_t blk, new_blk;
2257 for (i = 0; i < fs->group_desc_count; i++) {
2258 blk = ext2fs_block_bitmap_loc(fs, i);
2259 if (ext2fs_test_block_bitmap2(bmap, blk)) {
2260 new_blk = translate_block(blk);
2263 ext2fs_block_bitmap_loc_set(fs, i, new_blk);
2266 blk = ext2fs_inode_bitmap_loc(fs, i);
2267 if (ext2fs_test_block_bitmap2(bmap, blk)) {
2268 new_blk = translate_block(blk);
2271 ext2fs_inode_bitmap_loc_set(fs, i, new_blk);
2277 static int expand_inode_table(ext2_filsys fs, unsigned long new_ino_size)
2282 int new_ino_blks_per_grp;
2284 char *old_itable = NULL, *new_itable = NULL;
2285 char *tmp_old_itable = NULL, *tmp_new_itable = NULL;
2286 unsigned long old_ino_size;
2287 int old_itable_size, new_itable_size;
2289 old_itable_size = fs->inode_blocks_per_group * fs->blocksize;
2290 old_ino_size = EXT2_INODE_SIZE(fs->super);
2292 new_ino_blks_per_grp = ext2fs_div_ceil(
2293 EXT2_INODES_PER_GROUP(fs->super) *
2297 new_itable_size = new_ino_blks_per_grp * fs->blocksize;
2299 retval = ext2fs_get_mem(old_itable_size, &old_itable);
2303 retval = ext2fs_get_mem(new_itable_size, &new_itable);
2307 tmp_old_itable = old_itable;
2308 tmp_new_itable = new_itable;
2310 for (i = 0; i < fs->group_desc_count; i++) {
2311 blk = ext2fs_inode_table_loc(fs, i);
2312 retval = io_channel_read_blk64(fs->io, blk,
2313 fs->inode_blocks_per_group, old_itable);
2317 for (j = 0; j < EXT2_INODES_PER_GROUP(fs->super); j++) {
2318 memcpy(new_itable, old_itable, old_ino_size);
2320 memset(new_itable+old_ino_size, 0,
2321 new_ino_size - old_ino_size);
2323 new_itable += new_ino_size;
2324 old_itable += old_ino_size;
2327 /* reset the pointer */
2328 old_itable = tmp_old_itable;
2329 new_itable = tmp_new_itable;
2331 retval = io_channel_write_blk64(fs->io, blk,
2332 new_ino_blks_per_grp, new_itable);
2337 /* Update the meta data */
2338 fs->inode_blocks_per_group = new_ino_blks_per_grp;
2339 ext2fs_free_inode_cache(fs->icache);
2341 fs->super->s_inode_size = new_ino_size;
2345 ext2fs_free_mem(&old_itable);
2348 ext2fs_free_mem(&new_itable);
2353 static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs)
2357 unsigned int group = 0;
2358 unsigned int count = 0;
2363 * First calculate the block statistics
2365 for (blk = fs->super->s_first_data_block;
2366 blk < ext2fs_blocks_count(fs->super); blk++) {
2367 if (!ext2fs_fast_test_block_bitmap2(fs->block_map, blk)) {
2372 if ((count == fs->super->s_blocks_per_group) ||
2373 (blk == ext2fs_blocks_count(fs->super)-1)) {
2374 ext2fs_bg_free_blocks_count_set(fs, group++,
2380 total_free = EXT2FS_C2B(fs, total_free);
2381 ext2fs_free_blocks_count_set(fs->super, total_free);
2384 * Next, calculate the inode statistics
2391 /* Protect loop from wrap-around if s_inodes_count maxed */
2392 for (ino = 1; ino <= fs->super->s_inodes_count && ino > 0; ino++) {
2393 if (!ext2fs_fast_test_inode_bitmap2(fs->inode_map, ino)) {
2398 if ((count == fs->super->s_inodes_per_group) ||
2399 (ino == fs->super->s_inodes_count)) {
2400 ext2fs_bg_free_inodes_count_set(fs, group++,
2406 fs->super->s_free_inodes_count = total_free;
2407 ext2fs_mark_super_dirty(fs);
2411 #define list_for_each_safe(pos, pnext, head) \
2412 for (pos = (head)->next, pnext = pos->next; pos != (head); \
2413 pos = pnext, pnext = pos->next)
2415 static void free_blk_move_list(void)
2417 struct list_head *entry, *tmp;
2418 struct blk_move *bmv;
2420 list_for_each_safe(entry, tmp, &blk_move_list) {
2421 bmv = list_entry(entry, struct blk_move, list);
2423 ext2fs_free_mem(&bmv);
2428 static int resize_inode(ext2_filsys fs, unsigned long new_size)
2431 int new_ino_blks_per_grp;
2432 ext2fs_block_bitmap bmap;
2434 retval = ext2fs_read_inode_bitmap(fs);
2436 fputs(_("Failed to read inode bitmap\n"), stderr);
2439 retval = ext2fs_read_block_bitmap(fs);
2441 fputs(_("Failed to read block bitmap\n"), stderr);
2444 INIT_LIST_HEAD(&blk_move_list);
2447 new_ino_blks_per_grp = ext2fs_div_ceil(
2448 EXT2_INODES_PER_GROUP(fs->super)*
2452 /* We may change the file system.
2453 * Mark the file system as invalid so that
2454 * the user is prompted to run fsck.
2456 fs->super->s_state &= ~EXT2_VALID_FS;
2458 retval = ext2fs_allocate_block_bitmap(fs, _("blocks to be moved"),
2461 fputs(_("Failed to allocate block bitmap when "
2462 "increasing inode size\n"), stderr);
2465 retval = get_move_bitmaps(fs, new_ino_blks_per_grp, bmap);
2467 fputs(_("Not enough space to increase inode size \n"), stderr);
2470 retval = move_block(fs, bmap);
2472 fputs(_("Failed to relocate blocks during inode resize \n"),
2476 retval = inode_scan_and_fix(fs, bmap);
2480 retval = group_desc_scan_and_fix(fs, bmap);
2484 retval = expand_inode_table(fs, new_size);
2488 ext2fs_calculate_summary_stats(fs);
2490 fs->super->s_state |= EXT2_VALID_FS;
2491 /* mark super block and block bitmap as dirty */
2492 ext2fs_mark_super_dirty(fs);
2493 ext2fs_mark_bb_dirty(fs);
2496 free_blk_move_list();
2497 ext2fs_free_block_bitmap(bmap);
2502 free_blk_move_list();
2503 ext2fs_free_block_bitmap(bmap);
2504 fputs(_("Error in resizing the inode size.\n"
2505 "Run e2undo to undo the "
2506 "file system changes. \n"), stderr);
2511 static int tune2fs_setup_tdb(const char *name, io_manager *io_ptr)
2513 errcode_t retval = 0;
2514 const char *tdb_dir;
2516 char *dev_name, *tmp_name;
2520 * Configuration via a conf file would be
2523 profile_get_string(profile, "scratch_files",
2527 tmp_name = strdup(name);
2530 com_err(program_name, ENOMEM, "%s",
2531 _("Couldn't allocate memory for tdb filename\n"));
2534 dev_name = basename(tmp_name);
2536 tdb_dir = getenv("E2FSPROGS_UNDO_DIR");
2538 tdb_dir = "/var/lib/e2fsprogs";
2540 if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
2541 access(tdb_dir, W_OK))
2544 tdb_file = malloc(strlen(tdb_dir) + 9 + strlen(dev_name) + 7 + 1);
2547 sprintf(tdb_file, "%s/tune2fs-%s.e2undo", tdb_dir, dev_name);
2549 if ((unlink(tdb_file) < 0) && (errno != ENOENT)) {
2551 com_err(program_name, retval,
2552 _("while trying to delete %s"), tdb_file);
2557 set_undo_io_backing_manager(*io_ptr);
2558 *io_ptr = undo_io_manager;
2559 set_undo_io_backup_file(tdb_file);
2560 printf(_("To undo the tune2fs operation please run "
2561 "the command\n e2undo %s %s\n\n"),
2569 fs_update_journal_user(struct ext2_super_block *sb, __u8 old_uuid[UUID_SIZE])
2571 int retval, nr_users, start;
2572 journal_superblock_t *jsb;
2576 char uuid[UUID_STR_SIZE];
2577 char buf[SUPERBLOCK_SIZE];
2579 if (!(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) ||
2580 uuid_is_null(sb->s_journal_uuid))
2583 uuid_unparse(sb->s_journal_uuid, uuid);
2584 journal_path = blkid_get_devname(NULL, "UUID", uuid);
2588 retval = ext2fs_open2(journal_path, io_options,
2589 EXT2_FLAG_JOURNAL_DEV_OK | EXT2_FLAG_RW,
2590 0, 0, unix_io_manager, &jfs);
2592 com_err(program_name, retval,
2593 _("while trying to open %s"),
2598 retval = get_journal_sb(jfs, buf);
2600 if (retval == EXT2_ET_UNSUPP_FEATURE)
2601 fprintf(stderr, _("%s is not a journal device.\n"),
2606 jsb = (journal_superblock_t *) buf;
2607 /* Find the filesystem UUID */
2608 nr_users = ntohl(jsb->s_nr_users);
2610 j_uuid = journal_user(old_uuid, jsb->s_users, nr_users);
2611 if (j_uuid == NULL) {
2612 fputs(_("Filesystem's UUID not found on journal device.\n"),
2614 return EXT2_ET_LOAD_EXT_JOURNAL;
2617 memcpy(j_uuid, sb->s_uuid, UUID_SIZE);
2619 start = ext2fs_journal_sb_start(jfs->blocksize);
2620 /* Write back the journal superblock */
2621 retval = io_channel_write_blk64(jfs->io, start, -SUPERBLOCK_SIZE, buf);
2623 com_err(program_name, retval,
2624 "while writing journal superblock.");
2633 int main(int argc, char **argv)
2637 struct ext2_super_block *sb;
2638 io_manager io_ptr, io_ptr_orig = NULL;
2642 setlocale(LC_MESSAGES, "");
2643 setlocale(LC_CTYPE, "");
2644 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
2645 textdomain(NLS_CAT_NAME);
2646 set_com_err_gettext(gettext);
2649 program_name = *argv;
2650 add_error_table(&et_ext2_error_table);
2652 #ifdef CONFIG_BUILD_FINDFS
2653 if (strcmp(get_progname(argv[0]), "findfs") == 0)
2654 do_findfs(argc, argv);
2656 if (strcmp(get_progname(argv[0]), "e2label") == 0)
2657 parse_e2label_options(argc, argv);
2659 parse_tune2fs_options(argc, argv);
2661 #ifdef CONFIG_TESTIO_DEBUG
2662 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_DEBUG")) {
2663 io_ptr = test_io_manager;
2664 test_io_backing_manager = unix_io_manager;
2667 io_ptr = unix_io_manager;
2670 if ((open_flag & EXT2_FLAG_RW) == 0 || f_flag)
2671 open_flag |= EXT2_FLAG_SKIP_MMP;
2673 open_flag |= EXT2_FLAG_64BITS | EXT2_FLAG_JOURNAL_DEV_OK;
2675 /* keep the filesystem struct around to dump MMP data */
2676 open_flag |= EXT2_FLAG_NOFREE_ON_ERROR;
2678 retval = ext2fs_open2(device_name, io_options, open_flag,
2681 com_err(program_name, retval,
2682 _("while trying to open %s"),
2684 if (retval == EXT2_ET_MMP_FSCK_ON ||
2685 retval == EXT2_ET_MMP_UNKNOWN_SEQ)
2686 dump_mmp_msg(fs->mmp_buf,
2687 _("If you are sure the filesystem "
2688 "is not in use on any node, run:\n"
2689 "'tune2fs -f -E clear_mmp {device}'\n"));
2690 else if (retval == EXT2_ET_MMP_FAILED)
2691 dump_mmp_msg(fs->mmp_buf, NULL);
2692 else if (retval == EXT2_ET_MMP_MAGIC_INVALID)
2694 _("MMP block magic is bad. Try to fix it by "
2695 "running:\n'e2fsck -f %s'\n"), device_name);
2696 else if (retval == EXT2_ET_BAD_MAGIC)
2697 check_plausibility(device_name, CHECK_FS_EXIST, NULL);
2698 else if (retval != EXT2_ET_MMP_FAILED)
2699 fprintf(stderr, "%s",
2700 _("Couldn't find valid filesystem superblock.\n"));
2705 if (EXT2_HAS_INCOMPAT_FEATURE(fs->super,
2706 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
2707 fprintf(stderr, "%s", _("Cannot modify a journal device.\n"));
2711 fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE;
2713 if (I_flag && !io_ptr_orig) {
2715 * Check the inode size is right so we can issue an
2716 * error message and bail before setting up the tdb
2719 if (new_inode_size == EXT2_INODE_SIZE(fs->super)) {
2720 fprintf(stderr, _("The inode size is already %lu\n"),
2725 if (new_inode_size < EXT2_INODE_SIZE(fs->super)) {
2726 fprintf(stderr, "%s",
2727 _("Shrinking inode size is not supported\n"));
2731 if (new_inode_size > fs->blocksize) {
2732 fprintf(stderr, _("Invalid inode size %lu (max %d)\n"),
2733 new_inode_size, fs->blocksize);
2739 * If inode resize is requested use the
2742 io_ptr_orig = io_ptr;
2743 retval = tune2fs_setup_tdb(device_name, &io_ptr);
2748 if (io_ptr != io_ptr_orig) {
2749 ext2fs_close_free(&fs);
2755 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
2758 /* For e2label emulation */
2759 printf("%.*s\n", (int) sizeof(sb->s_volume_name),
2761 remove_error_table(&et_ext2_error_table);
2765 retval = ext2fs_check_if_mounted(device_name, &mount_flags);
2767 com_err("ext2fs_check_if_mount", retval,
2768 _("while determining whether %s is mounted."),
2773 /* Normally we only need to write out the superblock */
2774 fs->flags |= EXT2_FLAG_SUPER_ONLY;
2777 sb->s_max_mnt_count = max_mount_count;
2778 ext2fs_mark_super_dirty(fs);
2779 printf(_("Setting maximal mount count to %d\n"),
2783 sb->s_mnt_count = mount_count;
2784 ext2fs_mark_super_dirty(fs);
2785 printf(_("Setting current mount count to %d\n"), mount_count);
2788 sb->s_errors = errors;
2789 ext2fs_mark_super_dirty(fs);
2790 printf(_("Setting error behavior to %d\n"), errors);
2793 sb->s_def_resgid = resgid;
2794 ext2fs_mark_super_dirty(fs);
2795 printf(_("Setting reserved blocks gid to %lu\n"), resgid);
2798 if ((unsigned long long)interval >= (1ULL << 32)) {
2799 com_err(program_name, 0,
2800 _("interval between checks is too big (%lu)"),
2805 sb->s_checkinterval = interval;
2806 ext2fs_mark_super_dirty(fs);
2807 printf(_("Setting interval between checks to %lu seconds\n"),
2811 ext2fs_r_blocks_count_set(sb, reserved_ratio *
2812 ext2fs_blocks_count(sb) / 100.0);
2813 ext2fs_mark_super_dirty(fs);
2814 printf (_("Setting reserved blocks percentage to %g%% (%llu blocks)\n"),
2815 reserved_ratio, ext2fs_r_blocks_count(sb));
2818 if (reserved_blocks > ext2fs_blocks_count(sb)/2) {
2819 com_err(program_name, 0,
2820 _("reserved blocks count is too big (%llu)"),
2825 ext2fs_r_blocks_count_set(sb, reserved_blocks);
2826 ext2fs_mark_super_dirty(fs);
2827 printf(_("Setting reserved blocks count to %llu\n"),
2831 if (sb->s_feature_ro_compat &
2832 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER) {
2833 fputs(_("\nThe filesystem already has sparse "
2834 "superblocks.\n"), stderr);
2835 } else if (sb->s_feature_incompat &
2836 EXT2_FEATURE_INCOMPAT_META_BG) {
2837 fputs(_("\nSetting the sparse superblock flag not "
2838 "supported\nfor filesystems with "
2839 "the meta_bg feature enabled.\n"),
2844 sb->s_feature_ro_compat |=
2845 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
2846 sb->s_state &= ~EXT2_VALID_FS;
2847 ext2fs_mark_super_dirty(fs);
2848 printf(_("\nSparse superblock flag set. %s"),
2853 fputs(_("\nClearing the sparse superblock flag not supported.\n"),
2859 sb->s_lastcheck = last_check_time;
2860 ext2fs_mark_super_dirty(fs);
2861 printf(_("Setting time filesystem last checked to %s\n"),
2862 ctime(&last_check_time));
2865 sb->s_def_resuid = resuid;
2866 ext2fs_mark_super_dirty(fs);
2867 printf(_("Setting reserved blocks uid to %lu\n"), resuid);
2870 if (strlen(new_label) > sizeof(sb->s_volume_name))
2871 fputs(_("Warning: label too long, truncating.\n"),
2873 memset(sb->s_volume_name, 0, sizeof(sb->s_volume_name));
2874 strncpy(sb->s_volume_name, new_label,
2875 sizeof(sb->s_volume_name));
2876 ext2fs_mark_super_dirty(fs);
2879 memset(sb->s_last_mounted, 0, sizeof(sb->s_last_mounted));
2880 strncpy(sb->s_last_mounted, new_last_mounted,
2881 sizeof(sb->s_last_mounted));
2882 ext2fs_mark_super_dirty(fs);
2885 rc = update_mntopts(fs, mntopts_cmd);
2890 rc = update_feature_set(fs, features_cmd);
2895 rc = parse_extended_opts(fs, extended_cmd);
2898 if (clear_mmp && !f_flag) {
2899 fputs(_("Error in using clear_mmp. "
2900 "It must be used with -f\n"),
2906 rc = ext2fs_mmp_clear(fs);
2909 if (journal_size || journal_device) {
2910 rc = add_journal(fs);
2916 if (mount_flags & EXT2_MF_MOUNTED) {
2917 fputs(_("The quota feature may only be changed when "
2918 "the filesystem is unmounted.\n"), stderr);
2922 handle_quota_options(fs);
2928 char buf[SUPERBLOCK_SIZE];
2929 __u8 old_uuid[UUID_SIZE];
2931 if (ext2fs_has_group_desc_csum(fs)) {
2933 * Changing the UUID requires rewriting all metadata,
2934 * which can race with a mounted fs. Don't allow that.
2936 if (mount_flags & EXT2_MF_MOUNTED) {
2937 fputs(_("The UUID may only be "
2938 "changed when the filesystem is "
2939 "unmounted.\n"), stderr);
2942 if (check_fsck_needed(fs))
2946 * Determine if the block group checksums are
2947 * correct so we know whether or not to set
2950 for (i = 0; i < fs->group_desc_count; i++)
2951 if (!ext2fs_group_desc_csum_verify(fs, i))
2953 if (i >= fs->group_desc_count)
2957 memcpy(old_uuid, sb->s_uuid, UUID_SIZE);
2958 if ((strcasecmp(new_UUID, "null") == 0) ||
2959 (strcasecmp(new_UUID, "clear") == 0)) {
2960 uuid_clear(sb->s_uuid);
2961 } else if (strcasecmp(new_UUID, "time") == 0) {
2962 uuid_generate_time(sb->s_uuid);
2963 } else if (strcasecmp(new_UUID, "random") == 0) {
2964 uuid_generate(sb->s_uuid);
2965 } else if (uuid_parse(new_UUID, sb->s_uuid)) {
2966 com_err(program_name, 0, "%s",
2967 _("Invalid UUID format\n"));
2971 ext2fs_init_csum_seed(fs);
2973 for (i = 0; i < fs->group_desc_count; i++)
2974 ext2fs_group_desc_csum_set(fs, i);
2975 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
2978 /* If this is a journal dev, we need to copy UUID into jsb */
2979 if (!(rc = get_journal_sb(fs, buf))) {
2980 journal_superblock_t *jsb;
2982 jsb = (journal_superblock_t *) buf;
2983 fputs(_("Need to update journal superblock.\n"), stdout);
2984 memcpy(jsb->s_uuid, sb->s_uuid, sizeof(sb->s_uuid));
2986 /* Writeback the journal superblock */
2987 if ((rc = io_channel_write_blk64(fs->io,
2988 ext2fs_journal_sb_start(fs->blocksize),
2989 -SUPERBLOCK_SIZE, buf)))
2991 } else if (rc != EXT2_ET_UNSUPP_FEATURE)
2994 rc = 0; /** Reset rc to avoid ext2fs_mmp_stop() */
2996 if ((rc = fs_update_journal_user(sb, old_uuid)))
3000 ext2fs_mark_super_dirty(fs);
3001 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
3002 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
3003 rewrite_checksums = 1;
3007 if (mount_flags & EXT2_MF_MOUNTED) {
3008 fputs(_("The inode size may only be "
3009 "changed when the filesystem is "
3010 "unmounted.\n"), stderr);
3014 if (fs->super->s_feature_incompat &
3015 EXT4_FEATURE_INCOMPAT_FLEX_BG) {
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 remove_error_table(&et_ext2_error_table);
3073 ext2fs_mmp_stop(fs);
3077 convert_64bit(fs, feature_64bit);
3078 return (ext2fs_close_free(&fs) ? 1 : 0);