2 * tune2fs.c - Change the file system parameters on an ext2 file system
4 * Copyright (C) 1992, 1993, 1994 Remy Card <card@masi.ibp.fr>
5 * Laboratoire MASI, Institut Blaise Pascal
6 * Universite Pierre et Marie Curie (Paris VI)
8 * Copyright 1995, 1996, 1997, 1998, 1999, 2000 by Theodore Ts'o.
11 * This file may be redistributed under the terms of the GNU Public
19 * 93/10/31 - Added the -c option to change the maximal mount counts
20 * 93/12/14 - Added -l flag to list contents of superblock
21 * M.J.E. Mol (marcel@duteca.et.tudelft.nl)
22 * F.W. ten Wolde (franky@duteca.et.tudelft.nl)
23 * 93/12/29 - Added the -e option to change errors behavior
24 * 94/02/27 - Ported to use the ext2fs library
25 * 94/03/06 - Added the checks interval from Uwe Ohse (uwe@tirka.gun.de)
28 #define _XOPEN_SOURCE 600 /* for inclusion of strptime() */
44 #include <strings.h> /* for strcasecmp() */
46 #define _BSD_SOURCE /* for inclusion of strcasecmp() via <string.h> */
51 #include <sys/types.h>
55 #include "ext2fs/ext2_fs.h"
56 #include "ext2fs/ext2fs.h"
57 #include "et/com_err.h"
58 #include "uuid/uuid.h"
62 #include "blkid/blkid.h"
63 #include "quota/quotaio.h"
65 #include "../version.h"
66 #include "nls-enable.h"
68 #define QOPT_ENABLE (1)
69 #define QOPT_DISABLE (-1)
71 extern int ask_yn(const char *string, int def);
73 const char *program_name = "tune2fs";
75 char *new_label, *new_last_mounted, *new_UUID;
77 static int c_flag, C_flag, e_flag, f_flag, g_flag, i_flag, l_flag, L_flag;
78 static int m_flag, M_flag, Q_flag, r_flag, s_flag = -1, u_flag, U_flag, T_flag;
81 static time_t last_check_time;
82 static int print_label;
83 static int max_mount_count, mount_count, mount_flags;
84 static unsigned long interval;
85 static blk64_t reserved_blocks;
86 static double reserved_ratio;
87 static unsigned long resgid, resuid;
88 static unsigned short errors;
90 static char *features_cmd;
91 static char *mntopts_cmd;
92 static int stride, stripe_width;
93 static int stride_set, stripe_width_set;
94 static char *extended_cmd;
95 static unsigned long new_inode_size;
96 static char *ext_mount_opts;
97 static int usrquota, grpquota;
98 static int rewrite_checksums;
100 int journal_size, journal_flags;
101 char *journal_device;
102 static blk64_t journal_location = ~0LL;
104 static struct list_head blk_move_list;
107 struct list_head list;
113 static const char *please_fsck = N_("Please run e2fsck on the filesystem.\n");
114 static const char *please_dir_fsck =
115 N_("Please run e2fsck -D on the filesystem.\n");
117 #ifdef CONFIG_BUILD_FINDFS
118 void do_findfs(int argc, char **argv);
121 static void usage(void)
124 _("Usage: %s [-c max_mounts_count] [-e errors_behavior] "
126 "\t[-i interval[d|m|w]] [-j] [-J journal_options] [-l]\n"
127 "\t[-m reserved_blocks_percent] "
128 "[-o [^]mount_options[,...]] [-p mmp_update_interval]\n"
129 "\t[-r reserved_blocks_count] [-u user] [-C mount_count] "
130 "[-L volume_label]\n"
131 "\t[-M last_mounted_dir] [-O [^]feature[,...]]\n"
133 "\t[-Q quota_options]\n"
135 "\t[-E extended-option[,...]] [-T last_check_time] "
136 "[-U UUID]\n\t[ -I new_inode_size ] device\n"), program_name);
140 static __u32 ok_features[3] = {
142 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
143 EXT2_FEATURE_COMPAT_DIR_INDEX,
145 EXT2_FEATURE_INCOMPAT_FILETYPE |
146 EXT3_FEATURE_INCOMPAT_EXTENTS |
147 EXT4_FEATURE_INCOMPAT_FLEX_BG |
148 EXT4_FEATURE_INCOMPAT_MMP,
150 EXT2_FEATURE_RO_COMPAT_LARGE_FILE |
151 EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
152 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
153 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
154 EXT4_FEATURE_RO_COMPAT_GDT_CSUM |
155 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER |
157 EXT4_FEATURE_RO_COMPAT_QUOTA |
159 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
162 static __u32 clear_ok_features[3] = {
164 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
165 EXT2_FEATURE_COMPAT_RESIZE_INODE |
166 EXT2_FEATURE_COMPAT_DIR_INDEX,
168 EXT2_FEATURE_INCOMPAT_FILETYPE |
169 EXT4_FEATURE_INCOMPAT_FLEX_BG |
170 EXT4_FEATURE_INCOMPAT_MMP,
172 EXT2_FEATURE_RO_COMPAT_LARGE_FILE |
173 EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
174 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
175 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
176 EXT4_FEATURE_RO_COMPAT_GDT_CSUM |
178 EXT4_FEATURE_RO_COMPAT_QUOTA |
180 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
184 * Try to get journal super block if any
186 static int get_journal_sb(ext2_filsys jfs, char buf[SUPERBLOCK_SIZE])
189 journal_superblock_t *jsb;
191 if (!(jfs->super->s_feature_incompat &
192 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
193 return EXT2_ET_UNSUPP_FEATURE;
196 /* Get the journal superblock */
197 if ((retval = io_channel_read_blk64(jfs->io,
198 ext2fs_journal_sb_start(jfs->blocksize), -SUPERBLOCK_SIZE, buf))) {
199 com_err(program_name, retval, "%s",
200 _("while reading journal superblock"));
204 jsb = (journal_superblock_t *) buf;
205 if ((jsb->s_header.h_magic != (unsigned)ntohl(JFS_MAGIC_NUMBER)) ||
206 (jsb->s_header.h_blocktype != (unsigned)ntohl(JFS_SUPERBLOCK_V2))) {
207 fputs(_("Journal superblock not found!\n"), stderr);
208 return EXT2_ET_BAD_MAGIC;
214 static __u8 *journal_user(__u8 uuid[UUID_SIZE], __u8 s_users[JFS_USERS_SIZE],
218 for (i = 0; i < nr_users; i++) {
219 if (memcmp(uuid, &s_users[i * UUID_SIZE], UUID_SIZE) == 0)
220 return &s_users[i * UUID_SIZE];
227 * Remove an external journal from the filesystem
229 static int remove_journal_device(ext2_filsys fs)
233 char buf[SUPERBLOCK_SIZE];
234 journal_superblock_t *jsb;
237 int commit_remove_journal = 0;
241 commit_remove_journal = 1; /* force removal even if error */
243 uuid_unparse(fs->super->s_journal_uuid, buf);
244 journal_path = blkid_get_devname(NULL, "UUID", buf);
248 ext2fs_find_block_device(fs->super->s_journal_dev);
250 goto no_valid_journal;
253 #ifdef CONFIG_TESTIO_DEBUG
254 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
255 io_ptr = test_io_manager;
256 test_io_backing_manager = unix_io_manager;
259 io_ptr = unix_io_manager;
260 retval = ext2fs_open(journal_path, EXT2_FLAG_RW|
261 EXT2_FLAG_JOURNAL_DEV_OK, 0,
262 fs->blocksize, io_ptr, &jfs);
264 com_err(program_name, retval, "%s",
265 _("while trying to open external journal"));
266 goto no_valid_journal;
269 if ((retval = get_journal_sb(jfs, buf))) {
270 if (retval == EXT2_ET_UNSUPP_FEATURE)
271 fprintf(stderr, _("%s is not a journal device.\n"),
273 goto no_valid_journal;
276 jsb = (journal_superblock_t *) buf;
277 /* Find the filesystem UUID */
278 nr_users = ntohl(jsb->s_nr_users);
280 if (!journal_user(fs->super->s_uuid, jsb->s_users, nr_users)) {
281 fputs(_("Filesystem's UUID not found on journal device.\n"),
283 commit_remove_journal = 1;
284 goto no_valid_journal;
287 for (i = 0; i < nr_users; i++)
288 memcpy(&jsb->s_users[i * 16], &jsb->s_users[(i + 1) * 16], 16);
289 jsb->s_nr_users = htonl(nr_users);
291 /* Write back the journal superblock */
292 retval = io_channel_write_blk64(jfs->io,
293 ext2fs_journal_sb_start(fs->blocksize),
294 -SUPERBLOCK_SIZE, buf);
296 com_err(program_name, retval,
297 "while writing journal superblock.");
298 goto no_valid_journal;
301 commit_remove_journal = 1;
304 if (commit_remove_journal == 0) {
305 fputs(_("Cannot locate journal device. It was NOT removed\n"
306 "Use -f option to remove missing journal device.\n"),
310 fs->super->s_journal_dev = 0;
311 uuid_clear(fs->super->s_journal_uuid);
312 ext2fs_mark_super_dirty(fs);
313 fputs(_("Journal removed\n"), stdout);
319 /* Helper function for remove_journal_inode */
320 static int release_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
321 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
322 blk64_t ref_block EXT2FS_ATTR((unused)),
323 int ref_offset EXT2FS_ATTR((unused)),
324 void *private EXT2FS_ATTR((unused)))
330 ext2fs_unmark_block_bitmap2(fs->block_map, block);
331 group = ext2fs_group_of_blk2(fs, block);
332 ext2fs_bg_free_blocks_count_set(fs, group, ext2fs_bg_free_blocks_count(fs, group) + 1);
333 ext2fs_group_desc_csum_set(fs, group);
334 ext2fs_free_blocks_count_add(fs->super, EXT2FS_CLUSTER_RATIO(fs));
339 * Remove the journal inode from the filesystem
341 static errcode_t remove_journal_inode(ext2_filsys fs)
343 struct ext2_inode inode;
345 ino_t ino = fs->super->s_journal_inum;
347 retval = ext2fs_read_inode(fs, ino, &inode);
349 com_err(program_name, retval, "%s",
350 _("while reading journal inode"));
353 if (ino == EXT2_JOURNAL_INO) {
354 retval = ext2fs_read_bitmaps(fs);
356 com_err(program_name, retval, "%s",
357 _("while reading bitmaps"));
360 retval = ext2fs_block_iterate3(fs, ino,
361 BLOCK_FLAG_READ_ONLY, NULL,
362 release_blocks_proc, NULL);
364 com_err(program_name, retval, "%s",
365 _("while clearing journal inode"));
368 memset(&inode, 0, sizeof(inode));
369 ext2fs_mark_bb_dirty(fs);
370 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
372 inode.i_flags &= ~EXT2_IMMUTABLE_FL;
373 retval = ext2fs_write_inode(fs, ino, &inode);
375 com_err(program_name, retval, "%s",
376 _("while writing journal inode"));
379 fs->super->s_journal_inum = 0;
380 memset(fs->super->s_jnl_blocks, 0, sizeof(fs->super->s_jnl_blocks));
381 ext2fs_mark_super_dirty(fs);
387 * Update the default mount options
389 static int update_mntopts(ext2_filsys fs, char *mntopts)
391 struct ext2_super_block *sb = fs->super;
393 if (e2p_edit_mntopts(mntopts, &sb->s_default_mount_opts, ~0)) {
394 fprintf(stderr, _("Invalid mount option set: %s\n"),
398 ext2fs_mark_super_dirty(fs);
403 static int check_fsck_needed(ext2_filsys fs)
405 if (fs->super->s_state & EXT2_VALID_FS)
407 printf("\n%s\n", _(please_fsck));
408 if (mount_flags & EXT2_MF_READONLY)
409 printf("%s", _("(and reboot afterwards!)\n"));
413 static void request_dir_fsck_afterwards(ext2_filsys fs)
415 static int requested;
419 fs->super->s_state &= ~EXT2_VALID_FS;
420 printf("\n%s\n", _(please_dir_fsck));
421 if (mount_flags & EXT2_MF_READONLY)
422 printf("%s", _("(and reboot afterwards!)\n"));
425 static void request_fsck_afterwards(ext2_filsys fs)
427 static int requested = 0;
431 fs->super->s_state &= ~EXT2_VALID_FS;
432 printf("\n%s\n", _(please_fsck));
433 if (mount_flags & EXT2_MF_READONLY)
434 printf("%s", _("(and reboot afterwards!)\n"));
437 /* Rewrite extents */
438 static errcode_t rewrite_extents(ext2_filsys fs, ext2_ino_t ino,
439 struct ext2_inode *inode)
441 ext2_extent_handle_t handle;
442 struct ext2fs_extent extent;
443 int op = EXT2_EXTENT_ROOT;
446 if (!(inode->i_flags & EXT4_EXTENTS_FL))
449 errcode = ext2fs_extent_open(fs, ino, &handle);
454 errcode = ext2fs_extent_get(handle, op, &extent);
458 /* Root node is in the separately checksummed inode */
459 if (op == EXT2_EXTENT_ROOT) {
460 op = EXT2_EXTENT_NEXT;
463 op = EXT2_EXTENT_NEXT;
465 /* Only visit the first extent in each extent block */
466 if (extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT)
468 errcode = ext2fs_extent_replace(handle, 0, &extent);
473 /* Ok if we run off the end */
474 if (errcode == EXT2_ET_EXTENT_NO_NEXT)
480 * Rewrite directory blocks with checksums
482 struct rewrite_dir_context {
489 static int rewrite_dir_block(ext2_filsys fs,
491 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
492 blk64_t ref_block EXT2FS_ATTR((unused)),
493 int ref_offset EXT2FS_ATTR((unused)),
496 struct ext2_dx_countlimit *dcl = NULL;
497 struct rewrite_dir_context *ctx = priv_data;
498 int dcl_offset, changed = 0;
500 ctx->errcode = ext2fs_read_dir_block4(fs, *blocknr, ctx->buf, 0,
505 /* if htree node... */
507 ext2fs_get_dx_countlimit(fs, (struct ext2_dir_entry *)ctx->buf,
510 if (!EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
511 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
512 /* Ensure limit is the max size */
513 int max_entries = (fs->blocksize - dcl_offset) /
514 sizeof(struct ext2_dx_entry);
515 if (ext2fs_le16_to_cpu(dcl->limit) != max_entries) {
517 dcl->limit = ext2fs_cpu_to_le16(max_entries);
520 /* If htree block is full then rebuild the dir */
521 if (ext2fs_le16_to_cpu(dcl->count) ==
522 ext2fs_le16_to_cpu(dcl->limit)) {
523 request_dir_fsck_afterwards(fs);
527 * Ensure dcl->limit is small enough to leave room for
530 int max_entries = (fs->blocksize - (dcl_offset +
531 sizeof(struct ext2_dx_tail))) /
532 sizeof(struct ext2_dx_entry);
533 if (ext2fs_le16_to_cpu(dcl->limit) != max_entries)
534 dcl->limit = ext2fs_cpu_to_le16(max_entries);
535 /* Always rewrite checksum */
539 unsigned int rec_len, name_size;
540 char *top = ctx->buf + fs->blocksize;
541 struct ext2_dir_entry *de = (struct ext2_dir_entry *)ctx->buf;
542 struct ext2_dir_entry *last_de = NULL, *penultimate_de = NULL;
544 /* Find last and penultimate dirent */
545 while ((char *)de < top) {
546 penultimate_de = last_de;
548 ctx->errcode = ext2fs_get_rec_len(fs, de, &rec_len);
549 if (!ctx->errcode && !rec_len)
550 ctx->errcode = EXT2_ET_DIR_CORRUPTED;
553 de = (struct ext2_dir_entry *)(((char *)de) + rec_len);
555 ctx->errcode = ext2fs_get_rec_len(fs, last_de, &rec_len);
558 name_size = ext2fs_dirent_name_len(last_de);
560 if (!EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
561 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
564 if (last_de->inode ||
566 rec_len != sizeof(struct ext2_dir_entry_tail))
569 * The last dirent is unused and the right length to
570 * have stored a checksum. Erase it.
572 ctx->errcode = ext2fs_get_rec_len(fs, penultimate_de,
575 ctx->errcode = EXT2_ET_DIR_CORRUPTED;
578 ext2fs_set_rec_len(fs, rec_len +
579 sizeof(struct ext2_dir_entry_tail),
583 unsigned csum_size = sizeof(struct ext2_dir_entry_tail);
584 struct ext2_dir_entry_tail *t;
587 * If the last dirent looks like the tail, just update
590 if (!last_de->inode &&
591 rec_len == csum_size) {
592 t = (struct ext2_dir_entry_tail *)last_de;
593 t->det_reserved_name_len =
594 EXT2_DIR_NAME_LEN_CSUM;
599 name_size = (name_size & ~3) + 4;
600 /* If there's not enough space for the tail, e2fsck */
601 if (rec_len <= (8 + name_size + csum_size)) {
602 request_dir_fsck_afterwards(fs);
605 /* Shorten that last de and insert the tail */
606 ext2fs_set_rec_len(fs, rec_len - csum_size, last_de);
607 t = EXT2_DIRENT_TAIL(ctx->buf, fs->blocksize);
608 ext2fs_initialize_dirent_tail(fs, t);
610 /* Always update checksum */
619 ctx->errcode = ext2fs_write_dir_block4(fs, *blocknr, ctx->buf,
627 static errcode_t rewrite_directory(ext2_filsys fs, ext2_ino_t dir,
628 struct ext2_inode *inode)
631 struct rewrite_dir_context ctx;
633 retval = ext2fs_get_mem(fs->blocksize, &ctx.buf);
637 ctx.is_htree = (inode->i_flags & EXT2_INDEX_FL);
640 retval = ext2fs_block_iterate3(fs, dir, BLOCK_FLAG_READ_ONLY |
641 BLOCK_FLAG_DATA_ONLY,
642 0, rewrite_dir_block, &ctx);
644 ext2fs_free_mem(&ctx.buf);
652 * Forcibly set checksums in all inodes.
654 static void rewrite_inodes(ext2_filsys fs)
656 int length = EXT2_INODE_SIZE(fs->super);
657 struct ext2_inode *inode, *zero;
659 ext2_inode_scan scan;
662 blk64_t file_acl_block;
665 if (fs->super->s_creator_os != EXT2_OS_LINUX)
668 retval = ext2fs_open_inode_scan(fs, 0, &scan);
670 com_err("set_csum", retval, "while opening inode scan");
674 retval = ext2fs_get_mem(length, &inode);
676 com_err("set_csum", retval, "while allocating memory");
680 retval = ext2fs_get_memzero(length, &zero);
682 com_err("set_csum", retval, "while allocating memory");
686 retval = ext2fs_get_mem(fs->blocksize, &ea_buf);
688 com_err("set_csum", retval, "while allocating memory");
693 retval = ext2fs_get_next_inode_full(scan, &ino, inode, length);
695 com_err("set_csum", retval, "while getting next inode");
700 if (ext2fs_test_inode_bitmap2(fs->inode_map, ino)) {
703 if (memcmp(inode, zero, length) != 0) {
704 memset(inode, 0, length);
712 retval = ext2fs_write_inode_full(fs, ino, inode,
715 com_err("set_csum", retval, "while writing "
721 retval = rewrite_extents(fs, ino, inode);
723 com_err("rewrite_extents", retval,
724 "while rewriting extents");
728 if (LINUX_S_ISDIR(inode->i_mode) &&
729 ext2fs_inode_has_valid_blocks2(fs, inode)) {
730 retval = rewrite_directory(fs, ino, inode);
732 com_err("rewrite_directory", retval,
733 "while rewriting directories");
738 file_acl_block = ext2fs_file_acl_block(fs, inode);
741 retval = ext2fs_read_ext_attr3(fs, file_acl_block, ea_buf, ino);
743 com_err("rewrite_eablock", retval,
744 "while rewriting extended attribute");
747 retval = ext2fs_write_ext_attr3(fs, file_acl_block, ea_buf,
750 com_err("rewrite_eablock", retval,
751 "while rewriting extended attribute");
756 ext2fs_free_mem(&zero);
757 ext2fs_free_mem(&inode);
758 ext2fs_free_mem(&ea_buf);
759 ext2fs_close_inode_scan(scan);
762 static void rewrite_metadata_checksums(ext2_filsys fs)
767 fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
768 ext2fs_init_csum_seed(fs);
769 for (i = 0; i < fs->group_desc_count; i++)
770 ext2fs_group_desc_csum_set(fs, i);
771 retval = ext2fs_read_bitmaps(fs);
773 com_err("rewrite_metadata_checksums", retval,
774 "while reading bitmaps");
778 ext2fs_mark_ib_dirty(fs);
779 ext2fs_mark_bb_dirty(fs);
780 ext2fs_mmp_update2(fs, 1);
781 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
782 fs->flags &= ~EXT2_FLAG_IGNORE_CSUM_ERRORS;
783 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
784 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
785 fs->super->s_checksum_type = EXT2_CRC32C_CHKSUM;
787 fs->super->s_checksum_type = 0;
788 ext2fs_mark_super_dirty(fs);
791 static void enable_uninit_bg(ext2_filsys fs)
793 struct ext2_group_desc *gd;
796 for (i = 0; i < fs->group_desc_count; i++) {
797 gd = ext2fs_group_desc(fs, fs->group_desc, i);
798 gd->bg_itable_unused = 0;
799 gd->bg_flags = EXT2_BG_INODE_ZEROED;
800 ext2fs_group_desc_csum_set(fs, i);
802 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
805 static errcode_t zero_empty_inodes(ext2_filsys fs)
807 int length = EXT2_INODE_SIZE(fs->super);
808 struct ext2_inode *inode = NULL;
809 ext2_inode_scan scan;
813 retval = ext2fs_open_inode_scan(fs, 0, &scan);
817 retval = ext2fs_get_mem(length, &inode);
822 retval = ext2fs_get_next_inode_full(scan, &ino, inode, length);
827 if (!ext2fs_test_inode_bitmap2(fs->inode_map, ino)) {
828 memset(inode, 0, length);
829 retval = ext2fs_write_inode_full(fs, ino, inode,
837 ext2fs_free_mem(&inode);
838 ext2fs_close_inode_scan(scan);
842 static void disable_uninit_bg(ext2_filsys fs, __u32 csum_feature_flag)
844 struct ext2_group_desc *gd;
849 /* Load bitmaps to ensure that the uninit ones get written out */
850 fs->super->s_feature_ro_compat |= csum_feature_flag;
851 retval = ext2fs_read_bitmaps(fs);
853 com_err("disable_uninit_bg", retval,
854 "while reading bitmaps");
857 ext2fs_mark_ib_dirty(fs);
858 ext2fs_mark_bb_dirty(fs);
859 fs->super->s_feature_ro_compat &= ~csum_feature_flag;
861 /* If we're only turning off uninit_bg, zero the inodes */
862 if (csum_feature_flag == EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
863 retval = zero_empty_inodes(fs);
865 com_err("disable_uninit_bg", retval,
866 "while zeroing unused inodes");
867 request_fsck_afterwards(fs);
871 /* The bbitmap is zeroed; we must mark group metadata blocks in use */
872 for (i = 0; i < fs->group_desc_count; i++) {
873 b = ext2fs_block_bitmap_loc(fs, i);
874 ext2fs_mark_block_bitmap2(fs->block_map, b);
875 b = ext2fs_inode_bitmap_loc(fs, i);
876 ext2fs_mark_block_bitmap2(fs->block_map, b);
878 retval = ext2fs_super_and_bgd_loc2(fs, i, &b, &c, &d, NULL);
879 if (retval == 0 && b)
880 ext2fs_mark_block_bitmap2(fs->block_map, b);
881 if (retval == 0 && c)
882 ext2fs_mark_block_bitmap2(fs->block_map, c);
883 if (retval == 0 && d)
884 ext2fs_mark_block_bitmap2(fs->block_map, d);
886 com_err("disable_uninit_bg", retval,
887 "while initializing block bitmaps");
888 request_fsck_afterwards(fs);
891 gd = ext2fs_group_desc(fs, fs->group_desc, i);
892 gd->bg_itable_unused = 0;
894 ext2fs_group_desc_csum_set(fs, i);
896 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
897 ext2fs_mark_super_dirty(fs);
901 * Update the feature set as provided by the user.
903 static int update_feature_set(ext2_filsys fs, char *features)
905 struct ext2_super_block *sb = fs->super;
906 __u32 old_features[3];
908 unsigned int mask_err;
910 #define FEATURE_ON(type, mask) (!(old_features[(type)] & (mask)) && \
911 ((&sb->s_feature_compat)[(type)] & (mask)))
912 #define FEATURE_OFF(type, mask) ((old_features[(type)] & (mask)) && \
913 !((&sb->s_feature_compat)[(type)] & (mask)))
914 #define FEATURE_CHANGED(type, mask) ((mask) & \
915 (old_features[(type)] ^ (&sb->s_feature_compat)[(type)]))
917 old_features[E2P_FEATURE_COMPAT] = sb->s_feature_compat;
918 old_features[E2P_FEATURE_INCOMPAT] = sb->s_feature_incompat;
919 old_features[E2P_FEATURE_RO_INCOMPAT] = sb->s_feature_ro_compat;
921 if (e2p_edit_feature2(features, &sb->s_feature_compat,
922 ok_features, clear_ok_features,
923 &type_err, &mask_err)) {
926 _("Invalid filesystem option set: %s\n"),
928 else if (type_err & E2P_FEATURE_NEGATE_FLAG)
929 fprintf(stderr, _("Clearing filesystem feature '%s' "
931 e2p_feature2string(type_err &
932 E2P_FEATURE_TYPE_MASK,
935 fprintf(stderr, _("Setting filesystem feature '%s' "
937 e2p_feature2string(type_err, mask_err));
941 if (FEATURE_OFF(E2P_FEATURE_COMPAT, EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
942 if ((mount_flags & EXT2_MF_MOUNTED) &&
943 !(mount_flags & EXT2_MF_READONLY)) {
944 fputs(_("The has_journal feature may only be "
945 "cleared when the filesystem is\n"
946 "unmounted or mounted "
947 "read-only.\n"), stderr);
950 if ((sb->s_feature_incompat &
951 EXT3_FEATURE_INCOMPAT_RECOVER) &&
953 fputs(_("The needs_recovery flag is set. "
954 "Please run e2fsck before clearing\n"
955 "the has_journal flag.\n"), stderr);
958 if (sb->s_journal_inum) {
959 if (remove_journal_inode(fs))
962 if (sb->s_journal_dev) {
963 if (remove_journal_device(fs))
968 if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT,
969 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
970 if (sb->s_feature_incompat &
971 EXT2_FEATURE_INCOMPAT_META_BG) {
972 fputs(_("Setting filesystem feature 'sparse_super' "
973 "not supported\nfor filesystems with "
974 "the meta_bg feature enabled.\n"),
980 if (FEATURE_ON(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_MMP)) {
983 if ((mount_flags & EXT2_MF_MOUNTED) ||
984 (mount_flags & EXT2_MF_READONLY)) {
985 fputs(_("The multiple mount protection feature can't\n"
986 "be set if the filesystem is mounted or\n"
987 "read-only.\n"), stderr);
991 error = ext2fs_mmp_init(fs);
993 fputs(_("\nError while enabling multiple mount "
994 "protection feature."), stderr);
999 * We want to update group desc with the new free blocks count
1001 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
1003 printf(_("Multiple mount protection has been enabled "
1004 "with update interval %ds.\n"),
1005 sb->s_mmp_update_interval);
1008 if (FEATURE_OFF(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_MMP)) {
1011 if (mount_flags & EXT2_MF_READONLY) {
1012 fputs(_("The multiple mount protection feature cannot\n"
1013 "be disabled if the filesystem is readonly.\n"),
1018 error = ext2fs_read_bitmaps(fs);
1020 fputs(_("Error while reading bitmaps\n"), stderr);
1024 error = ext2fs_mmp_read(fs, sb->s_mmp_block, NULL);
1026 struct mmp_struct *mmp_cmp = fs->mmp_cmp;
1028 if (error == EXT2_ET_MMP_MAGIC_INVALID)
1029 printf(_("Magic number in MMP block does not "
1030 "match. expected: %x, actual: %x\n"),
1031 EXT4_MMP_MAGIC, mmp_cmp->mmp_magic);
1033 com_err(program_name, error, "%s",
1034 _("while reading MMP block."));
1038 /* We need to force out the group descriptors as well */
1039 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
1040 ext2fs_block_alloc_stats2(fs, sb->s_mmp_block, -1);
1042 sb->s_mmp_block = 0;
1043 sb->s_mmp_update_interval = 0;
1046 if (FEATURE_ON(E2P_FEATURE_COMPAT, EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
1048 * If adding a journal flag, let the create journal
1049 * code below handle setting the flag and creating the
1050 * journal. We supply a default size if necessary.
1054 sb->s_feature_compat &= ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1057 if (FEATURE_ON(E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_DIR_INDEX)) {
1058 if (!sb->s_def_hash_version)
1059 sb->s_def_hash_version = EXT2_HASH_HALF_MD4;
1060 if (uuid_is_null((unsigned char *) sb->s_hash_seed))
1061 uuid_generate((unsigned char *) sb->s_hash_seed);
1064 if (FEATURE_OFF(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
1065 if (ext2fs_check_desc(fs)) {
1066 fputs(_("Clearing the flex_bg flag would "
1067 "cause the the filesystem to be\n"
1068 "inconsistent.\n"), stderr);
1073 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
1074 EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
1075 if ((mount_flags & EXT2_MF_MOUNTED) &&
1076 !(mount_flags & EXT2_MF_READONLY)) {
1077 fputs(_("The huge_file feature may only be "
1078 "cleared when the filesystem is\n"
1079 "unmounted or mounted "
1080 "read-only.\n"), stderr);
1085 if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT,
1086 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
1087 if (check_fsck_needed(fs))
1089 rewrite_checksums = 1;
1090 /* metadata_csum supersedes uninit_bg */
1091 fs->super->s_feature_ro_compat &=
1092 ~EXT4_FEATURE_RO_COMPAT_GDT_CSUM;
1094 /* if uninit_bg was previously off, rewrite group desc */
1095 if (!(old_features[E2P_FEATURE_RO_INCOMPAT] &
1096 EXT4_FEATURE_RO_COMPAT_GDT_CSUM))
1097 enable_uninit_bg(fs);
1100 * Since metadata_csum supersedes uninit_bg, pretend like
1101 * uninit_bg has been off all along.
1103 old_features[E2P_FEATURE_RO_INCOMPAT] &=
1104 ~EXT4_FEATURE_RO_COMPAT_GDT_CSUM;
1107 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
1108 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
1109 if (check_fsck_needed(fs))
1111 rewrite_checksums = 1;
1113 * If we're turning off metadata_csum and not turning on
1114 * uninit_bg, rewrite group desc.
1116 if (!(fs->super->s_feature_ro_compat &
1117 EXT4_FEATURE_RO_COMPAT_GDT_CSUM))
1118 disable_uninit_bg(fs,
1119 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM);
1122 * metadata_csum previously provided uninit_bg, so if
1123 * we're also setting the uninit_bg feature bit,
1124 * pretend like it was previously enabled. Checksums
1125 * will be rewritten with crc16 later.
1127 old_features[E2P_FEATURE_RO_INCOMPAT] |=
1128 EXT4_FEATURE_RO_COMPAT_GDT_CSUM;
1131 if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT,
1132 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
1133 /* Do not enable uninit_bg when metadata_csum enabled */
1134 if (fs->super->s_feature_ro_compat &
1135 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)
1136 fs->super->s_feature_ro_compat &=
1137 ~EXT4_FEATURE_RO_COMPAT_GDT_CSUM;
1139 enable_uninit_bg(fs);
1142 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
1143 EXT4_FEATURE_RO_COMPAT_GDT_CSUM))
1144 disable_uninit_bg(fs,
1145 EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
1147 if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT,
1148 EXT4_FEATURE_RO_COMPAT_QUOTA)) {
1150 * Set the Q_flag here and handle the quota options in the code
1155 /* Enable both user quota and group quota by default */
1156 usrquota = QOPT_ENABLE;
1157 grpquota = QOPT_ENABLE;
1159 sb->s_feature_ro_compat &= ~EXT4_FEATURE_RO_COMPAT_QUOTA;
1162 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
1163 EXT4_FEATURE_RO_COMPAT_QUOTA)) {
1165 * Set the Q_flag here and handle the quota options in the code
1169 fputs(_("\nWarning: '^quota' option overrides '-Q'"
1170 "arguments.\n"), stderr);
1172 /* Disable both user quota and group quota by default */
1173 usrquota = QOPT_DISABLE;
1174 grpquota = QOPT_DISABLE;
1177 if (sb->s_rev_level == EXT2_GOOD_OLD_REV &&
1178 (sb->s_feature_compat || sb->s_feature_ro_compat ||
1179 sb->s_feature_incompat))
1180 ext2fs_update_dynamic_rev(fs);
1182 if (FEATURE_CHANGED(E2P_FEATURE_RO_INCOMPAT,
1183 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER) ||
1184 FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
1185 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) ||
1186 FEATURE_CHANGED(E2P_FEATURE_INCOMPAT,
1187 EXT2_FEATURE_INCOMPAT_FILETYPE) ||
1188 FEATURE_CHANGED(E2P_FEATURE_COMPAT,
1189 EXT2_FEATURE_COMPAT_RESIZE_INODE) ||
1190 FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
1191 EXT2_FEATURE_RO_COMPAT_LARGE_FILE))
1192 request_fsck_afterwards(fs);
1194 if ((old_features[E2P_FEATURE_COMPAT] != sb->s_feature_compat) ||
1195 (old_features[E2P_FEATURE_INCOMPAT] != sb->s_feature_incompat) ||
1196 (old_features[E2P_FEATURE_RO_INCOMPAT] != sb->s_feature_ro_compat))
1197 ext2fs_mark_super_dirty(fs);
1203 * Add a journal to the filesystem.
1205 static int add_journal(ext2_filsys fs)
1207 unsigned long journal_blocks;
1212 if (fs->super->s_feature_compat &
1213 EXT3_FEATURE_COMPAT_HAS_JOURNAL) {
1214 fputs(_("The filesystem already has a journal.\n"), stderr);
1217 if (journal_device) {
1218 if (!check_plausibility(journal_device, CHECK_BLOCK_DEV,
1220 proceed_question(-1);
1221 check_mount(journal_device, 0, _("journal"));
1222 #ifdef CONFIG_TESTIO_DEBUG
1223 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
1224 io_ptr = test_io_manager;
1225 test_io_backing_manager = unix_io_manager;
1228 io_ptr = unix_io_manager;
1229 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
1230 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1231 fs->blocksize, io_ptr, &jfs);
1233 com_err(program_name, retval,
1234 _("\n\twhile trying to open journal on %s\n"),
1238 printf(_("Creating journal on device %s: "),
1242 retval = ext2fs_add_journal_device(fs, jfs);
1243 ext2fs_close_free(&jfs);
1245 com_err(program_name, retval,
1246 _("while adding filesystem to journal on %s"),
1250 fputs(_("done\n"), stdout);
1251 } else if (journal_size) {
1252 fputs(_("Creating journal inode: "), stdout);
1254 journal_blocks = figure_journal_size(journal_size, fs);
1256 if (journal_location_string)
1258 parse_num_blocks2(journal_location_string,
1259 fs->super->s_log_block_size);
1260 retval = ext2fs_add_journal_inode2(fs, journal_blocks,
1264 fprintf(stderr, "\n");
1265 com_err(program_name, retval, "%s",
1266 _("\n\twhile trying to create journal file"));
1269 fputs(_("done\n"), stdout);
1271 * If the filesystem wasn't mounted, we need to force
1272 * the block group descriptors out.
1274 if ((mount_flags & EXT2_MF_MOUNTED) == 0)
1275 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
1277 print_check_message(fs->super->s_max_mnt_count,
1278 fs->super->s_checkinterval);
1282 free(journal_device);
1286 static void handle_quota_options(ext2_filsys fs)
1291 if (!usrquota && !grpquota)
1292 /* Nothing to do. */
1295 quota_init_context(&qctx, fs, -1);
1297 if (usrquota == QOPT_ENABLE || grpquota == QOPT_ENABLE)
1298 quota_compute_usage(qctx);
1300 if (usrquota == QOPT_ENABLE && !fs->super->s_usr_quota_inum) {
1301 if ((qf_ino = quota_file_exists(fs, USRQUOTA,
1303 quota_update_limits(qctx, qf_ino, USRQUOTA);
1304 quota_write_inode(qctx, USRQUOTA);
1305 } else if (usrquota == QOPT_DISABLE) {
1306 quota_remove_inode(fs, USRQUOTA);
1309 if (grpquota == QOPT_ENABLE && !fs->super->s_grp_quota_inum) {
1310 if ((qf_ino = quota_file_exists(fs, GRPQUOTA,
1312 quota_update_limits(qctx, qf_ino, GRPQUOTA);
1313 quota_write_inode(qctx, GRPQUOTA);
1314 } else if (grpquota == QOPT_DISABLE) {
1315 quota_remove_inode(fs, GRPQUOTA);
1318 quota_release_context(&qctx);
1320 if ((usrquota == QOPT_ENABLE) || (grpquota == QOPT_ENABLE)) {
1321 fs->super->s_feature_ro_compat |= EXT4_FEATURE_RO_COMPAT_QUOTA;
1322 ext2fs_mark_super_dirty(fs);
1323 } else if (!fs->super->s_usr_quota_inum &&
1324 !fs->super->s_grp_quota_inum) {
1325 fs->super->s_feature_ro_compat &= ~EXT4_FEATURE_RO_COMPAT_QUOTA;
1326 ext2fs_mark_super_dirty(fs);
1333 static void parse_quota_opts(const char *opts)
1335 char *buf, *token, *next, *p;
1339 buf = malloc(len+1);
1341 fputs(_("Couldn't allocate memory to parse quota "
1342 "options!\n"), stderr);
1346 for (token = buf; token && *token; token = next) {
1347 p = strchr(token, ',');
1354 if (strcmp(token, "usrquota") == 0) {
1355 usrquota = QOPT_ENABLE;
1356 } else if (strcmp(token, "^usrquota") == 0) {
1357 usrquota = QOPT_DISABLE;
1358 } else if (strcmp(token, "grpquota") == 0) {
1359 grpquota = QOPT_ENABLE;
1360 } else if (strcmp(token, "^grpquota") == 0) {
1361 grpquota = QOPT_DISABLE;
1363 fputs(_("\nBad quota options specified.\n\n"
1364 "Following valid quota options are available "
1365 "(pass by separating with comma):\n"
1377 static void parse_e2label_options(int argc, char ** argv)
1379 if ((argc < 2) || (argc > 3)) {
1380 fputs(_("Usage: e2label device [newlabel]\n"), stderr);
1383 io_options = strchr(argv[1], '?');
1386 device_name = blkid_get_devname(NULL, argv[1], NULL);
1388 com_err("e2label", 0, _("Unable to resolve '%s'"),
1392 open_flag = EXT2_FLAG_JOURNAL_DEV_OK;
1394 open_flag |= EXT2_FLAG_RW;
1396 new_label = argv[2];
1401 static time_t parse_time(char *str)
1405 if (strcmp(str, "now") == 0) {
1408 memset(&ts, 0, sizeof(ts));
1409 #ifdef HAVE_STRPTIME
1410 strptime(str, "%Y%m%d%H%M%S", &ts);
1412 sscanf(str, "%4d%2d%2d%2d%2d%2d", &ts.tm_year, &ts.tm_mon,
1413 &ts.tm_mday, &ts.tm_hour, &ts.tm_min, &ts.tm_sec);
1416 if (ts.tm_year < 0 || ts.tm_mon < 0 || ts.tm_mon > 11 ||
1417 ts.tm_mday < 0 || ts.tm_mday > 31 || ts.tm_hour > 23 ||
1418 ts.tm_min > 59 || ts.tm_sec > 61)
1421 if (ts.tm_mday == 0) {
1422 com_err(program_name, 0,
1423 _("Couldn't parse date/time specifier: %s"),
1428 return (mktime(&ts));
1431 static void parse_tune2fs_options(int argc, char **argv)
1437 char optstring[100] = "c:e:fg:i:jlm:o:r:s:u:C:E:I:J:L:M:O:T:U:";
1440 strcat(optstring, "Q:");
1444 printf("tune2fs %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
1445 while ((c = getopt(argc, argv, optstring)) != EOF)
1448 max_mount_count = strtol(optarg, &tmp, 0);
1449 if (*tmp || max_mount_count > 16000) {
1450 com_err(program_name, 0,
1451 _("bad mounts count - %s"),
1455 if (max_mount_count == 0)
1456 max_mount_count = -1;
1458 open_flag = EXT2_FLAG_RW;
1461 mount_count = strtoul(optarg, &tmp, 0);
1462 if (*tmp || mount_count > 16000) {
1463 com_err(program_name, 0,
1464 _("bad mounts count - %s"),
1469 open_flag = EXT2_FLAG_RW;
1472 if (strcmp(optarg, "continue") == 0)
1473 errors = EXT2_ERRORS_CONTINUE;
1474 else if (strcmp(optarg, "remount-ro") == 0)
1475 errors = EXT2_ERRORS_RO;
1476 else if (strcmp(optarg, "panic") == 0)
1477 errors = EXT2_ERRORS_PANIC;
1479 com_err(program_name, 0,
1480 _("bad error behavior - %s"),
1485 open_flag = EXT2_FLAG_RW;
1488 extended_cmd = optarg;
1489 open_flag |= EXT2_FLAG_RW;
1491 case 'f': /* Force */
1495 resgid = strtoul(optarg, &tmp, 0);
1497 gr = getgrnam(optarg);
1501 resgid = gr->gr_gid;
1506 com_err(program_name, 0,
1507 _("bad gid/group name - %s"),
1512 open_flag = EXT2_FLAG_RW;
1515 interval = strtoul(optarg, &tmp, 0);
1522 case 'D': /* days */
1528 case 'M': /* months! */
1529 interval *= 86400 * 30;
1533 case 'W': /* weeks */
1534 interval *= 86400 * 7;
1539 com_err(program_name, 0,
1540 _("bad interval - %s"), optarg);
1544 open_flag = EXT2_FLAG_RW;
1549 open_flag = EXT2_FLAG_RW;
1552 parse_journal_opts(optarg);
1553 open_flag = EXT2_FLAG_RW;
1561 open_flag |= EXT2_FLAG_RW |
1562 EXT2_FLAG_JOURNAL_DEV_OK;
1565 reserved_ratio = strtod(optarg, &tmp);
1566 if (*tmp || reserved_ratio > 50 ||
1567 reserved_ratio < 0) {
1568 com_err(program_name, 0,
1569 _("bad reserved block ratio - %s"),
1574 open_flag = EXT2_FLAG_RW;
1577 new_last_mounted = optarg;
1579 open_flag = EXT2_FLAG_RW;
1583 com_err(program_name, 0, "%s",
1584 _("-o may only be specified once"));
1587 mntopts_cmd = optarg;
1588 open_flag = EXT2_FLAG_RW;
1592 com_err(program_name, 0, "%s",
1593 _("-O may only be specified once"));
1596 features_cmd = optarg;
1597 open_flag = EXT2_FLAG_RW;
1602 parse_quota_opts(optarg);
1603 open_flag = EXT2_FLAG_RW;
1607 reserved_blocks = strtoul(optarg, &tmp, 0);
1609 com_err(program_name, 0,
1610 _("bad reserved blocks count - %s"),
1615 open_flag = EXT2_FLAG_RW;
1617 case 's': /* Deprecated */
1618 s_flag = atoi(optarg);
1619 open_flag = EXT2_FLAG_RW;
1623 last_check_time = parse_time(optarg);
1624 open_flag = EXT2_FLAG_RW;
1627 resuid = strtoul(optarg, &tmp, 0);
1629 pw = getpwnam(optarg);
1633 resuid = pw->pw_uid;
1638 com_err(program_name, 0,
1639 _("bad uid/user name - %s"),
1644 open_flag = EXT2_FLAG_RW;
1649 open_flag = EXT2_FLAG_RW |
1650 EXT2_FLAG_JOURNAL_DEV_OK;
1653 new_inode_size = strtoul(optarg, &tmp, 0);
1655 com_err(program_name, 0,
1656 _("bad inode size - %s"),
1660 if (!((new_inode_size &
1661 (new_inode_size - 1)) == 0)) {
1662 com_err(program_name, 0,
1663 _("Inode size must be a "
1664 "power of two- %s"),
1668 open_flag = EXT2_FLAG_RW;
1674 if (optind < argc - 1 || optind == argc)
1676 if (!open_flag && !l_flag)
1678 io_options = strchr(argv[optind], '?');
1681 device_name = blkid_get_devname(NULL, argv[optind], NULL);
1683 com_err(program_name, 0, _("Unable to resolve '%s'"),
1689 #ifdef CONFIG_BUILD_FINDFS
1690 void do_findfs(int argc, char **argv)
1695 (strncmp(argv[1], "LABEL=", 6) && strncmp(argv[1], "UUID=", 5))) {
1696 fprintf(stderr, "Usage: findfs LABEL=<label>|UUID=<uuid>\n");
1699 dev = blkid_get_devname(NULL, argv[1], NULL);
1701 com_err("findfs", 0, _("Unable to resolve '%s'"),
1710 static int parse_extended_opts(ext2_filsys fs, const char *opts)
1712 char *buf, *token, *next, *p, *arg;
1717 buf = malloc(len+1);
1719 fprintf(stderr, "%s",
1720 _("Couldn't allocate memory to parse options!\n"));
1724 for (token = buf; token && *token; token = next) {
1725 p = strchr(token, ',');
1731 arg = strchr(token, '=');
1736 if (strcmp(token, "clear-mmp") == 0 ||
1737 strcmp(token, "clear_mmp") == 0) {
1739 } else if (strcmp(token, "mmp_update_interval") == 0) {
1745 intv = strtoul(arg, &p, 0);
1748 _("Invalid mmp_update_interval: %s\n"),
1754 intv = EXT4_MMP_UPDATE_INTERVAL;
1755 } else if (intv > EXT4_MMP_MAX_UPDATE_INTERVAL) {
1757 _("mmp_update_interval too big: %lu\n"),
1762 printf(P_("Setting multiple mount protection update "
1763 "interval to %lu second\n",
1764 "Setting multiple mount protection update "
1765 "interval to %lu seconds\n", intv),
1767 fs->super->s_mmp_update_interval = intv;
1768 ext2fs_mark_super_dirty(fs);
1769 } else if (!strcmp(token, "test_fs")) {
1770 fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
1771 printf("Setting test filesystem flag\n");
1772 ext2fs_mark_super_dirty(fs);
1773 } else if (!strcmp(token, "^test_fs")) {
1774 fs->super->s_flags &= ~EXT2_FLAGS_TEST_FILESYS;
1775 printf("Clearing test filesystem flag\n");
1776 ext2fs_mark_super_dirty(fs);
1777 } else if (strcmp(token, "stride") == 0) {
1782 stride = strtoul(arg, &p, 0);
1785 _("Invalid RAID stride: %s\n"),
1791 } else if (strcmp(token, "stripe-width") == 0 ||
1792 strcmp(token, "stripe_width") == 0) {
1797 stripe_width = strtoul(arg, &p, 0);
1800 _("Invalid RAID stripe-width: %s\n"),
1805 stripe_width_set = 1;
1806 } else if (strcmp(token, "hash_alg") == 0 ||
1807 strcmp(token, "hash-alg") == 0) {
1812 hash_alg = e2p_string2hash(arg);
1815 _("Invalid hash algorithm: %s\n"),
1820 fs->super->s_def_hash_version = hash_alg;
1821 printf(_("Setting default hash algorithm "
1824 ext2fs_mark_super_dirty(fs);
1825 } else if (!strcmp(token, "mount_opts")) {
1830 if (strlen(arg) >= sizeof(fs->super->s_mount_opts)) {
1832 "Extended mount options too long\n");
1835 ext_mount_opts = strdup(arg);
1840 fprintf(stderr, "%s", _("\nBad options specified.\n\n"
1841 "Extended options are separated by commas, "
1842 "and may take an argument which\n"
1843 "\tis set off by an equals ('=') sign.\n\n"
1844 "Valid extended options are:\n"
1846 "\thash_alg=<hash algorithm>\n"
1847 "\tmount_opts=<extended default mount options>\n"
1848 "\tstride=<RAID per-disk chunk size in blocks>\n"
1849 "\tstripe_width=<RAID stride*data disks in blocks>\n"
1861 * Fill in the block bitmap bmap with the information regarding the
1862 * blocks to be moved
1864 static int get_move_bitmaps(ext2_filsys fs, int new_ino_blks_per_grp,
1865 ext2fs_block_bitmap bmap)
1869 ext2_badblocks_list bb_list = 0;
1870 blk64_t j, needed_blocks = 0;
1871 blk64_t start_blk, end_blk;
1873 retval = ext2fs_read_bb_inode(fs, &bb_list);
1877 for (i = 0; i < fs->group_desc_count; i++) {
1878 start_blk = ext2fs_inode_table_loc(fs, i) +
1879 fs->inode_blocks_per_group;
1881 end_blk = ext2fs_inode_table_loc(fs, i) +
1882 new_ino_blks_per_grp;
1884 for (j = start_blk; j < end_blk; j++) {
1885 if (ext2fs_test_block_bitmap2(fs->block_map, j)) {
1887 * IF the block is a bad block we fail
1889 if (ext2fs_badblocks_list_test(bb_list, j)) {
1890 ext2fs_badblocks_list_free(bb_list);
1894 ext2fs_mark_block_bitmap2(bmap, j);
1897 * We are going to use this block for
1898 * inode table. So mark them used.
1900 ext2fs_mark_block_bitmap2(fs->block_map, j);
1903 needed_blocks += end_blk - start_blk;
1906 ext2fs_badblocks_list_free(bb_list);
1907 if (needed_blocks > ext2fs_free_blocks_count(fs->super))
1913 static int ext2fs_is_meta_block(ext2_filsys fs, blk64_t blk)
1916 group = ext2fs_group_of_blk2(fs, blk);
1917 if (ext2fs_block_bitmap_loc(fs, group) == blk)
1919 if (ext2fs_inode_bitmap_loc(fs, group) == blk)
1924 static int ext2fs_is_block_in_group(ext2_filsys fs, dgrp_t group, blk64_t blk)
1926 blk64_t start_blk, end_blk;
1927 start_blk = fs->super->s_first_data_block +
1928 EXT2_GROUPS_TO_BLOCKS(fs->super, group);
1930 * We cannot get new block beyond end_blk for for the last block group
1931 * so we can check with EXT2_BLOCKS_PER_GROUP even for last block group
1933 end_blk = start_blk + EXT2_BLOCKS_PER_GROUP(fs->super);
1934 if (blk >= start_blk && blk <= end_blk)
1939 static int move_block(ext2_filsys fs, ext2fs_block_bitmap bmap)
1946 blk64_t blk, new_blk, goal;
1947 struct blk_move *bmv;
1949 retval = ext2fs_get_mem(fs->blocksize, &buf);
1953 for (new_blk = blk = fs->super->s_first_data_block;
1954 blk < ext2fs_blocks_count(fs->super); blk++) {
1955 if (!ext2fs_test_block_bitmap2(bmap, blk))
1958 if (ext2fs_is_meta_block(fs, blk)) {
1960 * If the block is mapping a fs meta data block
1961 * like group desc/block bitmap/inode bitmap. We
1962 * should find a block in the same group and fix
1963 * the respective fs metadata pointers. Otherwise
1966 group = ext2fs_group_of_blk2(fs, blk);
1967 goal = ext2fs_group_first_block2(fs, group);
1973 retval = ext2fs_new_block2(fs, goal, NULL, &new_blk);
1977 /* new fs meta data block should be in the same group */
1978 if (meta_data && !ext2fs_is_block_in_group(fs, group, new_blk)) {
1983 /* Mark this block as allocated */
1984 ext2fs_mark_block_bitmap2(fs->block_map, new_blk);
1986 /* Add it to block move list */
1987 retval = ext2fs_get_mem(sizeof(struct blk_move), &bmv);
1992 bmv->new_loc = new_blk;
1994 list_add(&(bmv->list), &blk_move_list);
1996 retval = io_channel_read_blk64(fs->io, blk, 1, buf);
2000 retval = io_channel_write_blk64(fs->io, new_blk, 1, buf);
2006 ext2fs_free_mem(&buf);
2010 static blk64_t translate_block(blk64_t blk)
2012 struct list_head *entry;
2013 struct blk_move *bmv;
2015 list_for_each(entry, &blk_move_list) {
2016 bmv = list_entry(entry, struct blk_move, list);
2017 if (bmv->old_loc == blk)
2018 return bmv->new_loc;
2024 static int process_block(ext2_filsys fs EXT2FS_ATTR((unused)),
2026 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
2027 blk64_t ref_block EXT2FS_ATTR((unused)),
2028 int ref_offset EXT2FS_ATTR((unused)),
2033 ext2fs_block_bitmap bmap = (ext2fs_block_bitmap) priv_data;
2035 if (!ext2fs_test_block_bitmap2(bmap, *block_nr))
2037 new_blk = translate_block(*block_nr);
2039 *block_nr = new_blk;
2041 * This will force the ext2fs_write_inode in the iterator
2043 ret |= BLOCK_CHANGED;
2049 static int inode_scan_and_fix(ext2_filsys fs, ext2fs_block_bitmap bmap)
2051 errcode_t retval = 0;
2054 char *block_buf = 0;
2055 struct ext2_inode inode;
2056 ext2_inode_scan scan = NULL;
2058 retval = ext2fs_get_mem(fs->blocksize * 3, &block_buf);
2062 retval = ext2fs_open_inode_scan(fs, 0, &scan);
2067 retval = ext2fs_get_next_inode(scan, &ino, &inode);
2074 if (inode.i_links_count == 0)
2075 continue; /* inode not in use */
2078 * If we end up modifying the journal inode
2079 * the sb->s_jnl_blocks will differ. But a
2080 * subsequent e2fsck fixes that.
2081 * Do we need to fix this ??
2084 if (ext2fs_file_acl_block(fs, &inode) &&
2085 ext2fs_test_block_bitmap2(bmap,
2086 ext2fs_file_acl_block(fs, &inode))) {
2087 blk = translate_block(ext2fs_file_acl_block(fs,
2092 ext2fs_file_acl_block_set(fs, &inode, blk);
2095 * Write the inode to disk so that inode table
2098 retval = ext2fs_write_inode(fs, ino, &inode);
2103 if (!ext2fs_inode_has_valid_blocks2(fs, &inode))
2106 retval = ext2fs_block_iterate3(fs, ino, 0, block_buf,
2107 process_block, bmap);
2114 ext2fs_free_mem(&block_buf);
2120 * We need to scan for inode and block bitmaps that may need to be
2121 * moved. This can take place if the filesystem was formatted for
2122 * RAID arrays using the mke2fs's extended option "stride".
2124 static int group_desc_scan_and_fix(ext2_filsys fs, ext2fs_block_bitmap bmap)
2127 blk64_t blk, new_blk;
2129 for (i = 0; i < fs->group_desc_count; i++) {
2130 blk = ext2fs_block_bitmap_loc(fs, i);
2131 if (ext2fs_test_block_bitmap2(bmap, blk)) {
2132 new_blk = translate_block(blk);
2135 ext2fs_block_bitmap_loc_set(fs, i, new_blk);
2138 blk = ext2fs_inode_bitmap_loc(fs, i);
2139 if (ext2fs_test_block_bitmap2(bmap, blk)) {
2140 new_blk = translate_block(blk);
2143 ext2fs_inode_bitmap_loc_set(fs, i, new_blk);
2149 static int expand_inode_table(ext2_filsys fs, unsigned long new_ino_size)
2154 int new_ino_blks_per_grp;
2156 char *old_itable = NULL, *new_itable = NULL;
2157 char *tmp_old_itable = NULL, *tmp_new_itable = NULL;
2158 unsigned long old_ino_size;
2159 int old_itable_size, new_itable_size;
2161 old_itable_size = fs->inode_blocks_per_group * fs->blocksize;
2162 old_ino_size = EXT2_INODE_SIZE(fs->super);
2164 new_ino_blks_per_grp = ext2fs_div_ceil(
2165 EXT2_INODES_PER_GROUP(fs->super) *
2169 new_itable_size = new_ino_blks_per_grp * fs->blocksize;
2171 retval = ext2fs_get_mem(old_itable_size, &old_itable);
2175 retval = ext2fs_get_mem(new_itable_size, &new_itable);
2179 tmp_old_itable = old_itable;
2180 tmp_new_itable = new_itable;
2182 for (i = 0; i < fs->group_desc_count; i++) {
2183 blk = ext2fs_inode_table_loc(fs, i);
2184 retval = io_channel_read_blk64(fs->io, blk,
2185 fs->inode_blocks_per_group, old_itable);
2189 for (j = 0; j < EXT2_INODES_PER_GROUP(fs->super); j++) {
2190 memcpy(new_itable, old_itable, old_ino_size);
2192 memset(new_itable+old_ino_size, 0,
2193 new_ino_size - old_ino_size);
2195 new_itable += new_ino_size;
2196 old_itable += old_ino_size;
2199 /* reset the pointer */
2200 old_itable = tmp_old_itable;
2201 new_itable = tmp_new_itable;
2203 retval = io_channel_write_blk64(fs->io, blk,
2204 new_ino_blks_per_grp, new_itable);
2209 /* Update the meta data */
2210 fs->inode_blocks_per_group = new_ino_blks_per_grp;
2211 fs->super->s_inode_size = new_ino_size;
2215 ext2fs_free_mem(&old_itable);
2218 ext2fs_free_mem(&new_itable);
2223 static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs)
2227 unsigned int group = 0;
2228 unsigned int count = 0;
2233 * First calculate the block statistics
2235 for (blk = fs->super->s_first_data_block;
2236 blk < ext2fs_blocks_count(fs->super); blk++) {
2237 if (!ext2fs_fast_test_block_bitmap2(fs->block_map, blk)) {
2242 if ((count == fs->super->s_blocks_per_group) ||
2243 (blk == ext2fs_blocks_count(fs->super)-1)) {
2244 ext2fs_bg_free_blocks_count_set(fs, group++,
2250 total_free = EXT2FS_C2B(fs, total_free);
2251 ext2fs_free_blocks_count_set(fs->super, total_free);
2254 * Next, calculate the inode statistics
2261 /* Protect loop from wrap-around if s_inodes_count maxed */
2262 for (ino = 1; ino <= fs->super->s_inodes_count && ino > 0; ino++) {
2263 if (!ext2fs_fast_test_inode_bitmap2(fs->inode_map, ino)) {
2268 if ((count == fs->super->s_inodes_per_group) ||
2269 (ino == fs->super->s_inodes_count)) {
2270 ext2fs_bg_free_inodes_count_set(fs, group++,
2276 fs->super->s_free_inodes_count = total_free;
2277 ext2fs_mark_super_dirty(fs);
2281 #define list_for_each_safe(pos, pnext, head) \
2282 for (pos = (head)->next, pnext = pos->next; pos != (head); \
2283 pos = pnext, pnext = pos->next)
2285 static void free_blk_move_list(void)
2287 struct list_head *entry, *tmp;
2288 struct blk_move *bmv;
2290 list_for_each_safe(entry, tmp, &blk_move_list) {
2291 bmv = list_entry(entry, struct blk_move, list);
2293 ext2fs_free_mem(&bmv);
2298 static int resize_inode(ext2_filsys fs, unsigned long new_size)
2301 int new_ino_blks_per_grp;
2302 ext2fs_block_bitmap bmap;
2304 retval = ext2fs_read_inode_bitmap(fs);
2306 fputs(_("Failed to read inode bitmap\n"), stderr);
2309 retval = ext2fs_read_block_bitmap(fs);
2311 fputs(_("Failed to read block bitmap\n"), stderr);
2314 INIT_LIST_HEAD(&blk_move_list);
2317 new_ino_blks_per_grp = ext2fs_div_ceil(
2318 EXT2_INODES_PER_GROUP(fs->super)*
2322 /* We may change the file system.
2323 * Mark the file system as invalid so that
2324 * the user is prompted to run fsck.
2326 fs->super->s_state &= ~EXT2_VALID_FS;
2328 retval = ext2fs_allocate_block_bitmap(fs, _("blocks to be moved"),
2331 fputs(_("Failed to allocate block bitmap when "
2332 "increasing inode size\n"), stderr);
2335 retval = get_move_bitmaps(fs, new_ino_blks_per_grp, bmap);
2337 fputs(_("Not enough space to increase inode size \n"), stderr);
2340 retval = move_block(fs, bmap);
2342 fputs(_("Failed to relocate blocks during inode resize \n"),
2346 retval = inode_scan_and_fix(fs, bmap);
2350 retval = group_desc_scan_and_fix(fs, bmap);
2354 retval = expand_inode_table(fs, new_size);
2358 ext2fs_calculate_summary_stats(fs);
2360 fs->super->s_state |= EXT2_VALID_FS;
2361 /* mark super block and block bitmap as dirty */
2362 ext2fs_mark_super_dirty(fs);
2363 ext2fs_mark_bb_dirty(fs);
2366 free_blk_move_list();
2367 ext2fs_free_block_bitmap(bmap);
2372 free_blk_move_list();
2373 ext2fs_free_block_bitmap(bmap);
2374 fputs(_("Error in resizing the inode size.\n"
2375 "Run e2undo to undo the "
2376 "file system changes. \n"), stderr);
2381 static int tune2fs_setup_tdb(const char *name, io_manager *io_ptr)
2383 errcode_t retval = 0;
2384 const char *tdb_dir;
2386 char *dev_name, *tmp_name;
2390 * Configuration via a conf file would be
2393 profile_get_string(profile, "scratch_files",
2397 tmp_name = strdup(name);
2400 com_err(program_name, ENOMEM, "%s",
2401 _("Couldn't allocate memory for tdb filename\n"));
2404 dev_name = basename(tmp_name);
2406 tdb_dir = getenv("E2FSPROGS_UNDO_DIR");
2408 tdb_dir = "/var/lib/e2fsprogs";
2410 if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
2411 access(tdb_dir, W_OK))
2414 tdb_file = malloc(strlen(tdb_dir) + 9 + strlen(dev_name) + 7 + 1);
2417 sprintf(tdb_file, "%s/tune2fs-%s.e2undo", tdb_dir, dev_name);
2419 if ((unlink(tdb_file) < 0) && (errno != ENOENT)) {
2421 com_err(program_name, retval,
2422 _("while trying to delete %s"), tdb_file);
2427 set_undo_io_backing_manager(*io_ptr);
2428 *io_ptr = undo_io_manager;
2429 set_undo_io_backup_file(tdb_file);
2430 printf(_("To undo the tune2fs operation please run "
2431 "the command\n e2undo %s %s\n\n"),
2439 fs_update_journal_user(struct ext2_super_block *sb, __u8 old_uuid[UUID_SIZE])
2441 int retval, nr_users, start;
2442 journal_superblock_t *jsb;
2446 char uuid[UUID_STR_SIZE];
2447 char buf[SUPERBLOCK_SIZE];
2449 if (!(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) ||
2450 uuid_is_null(sb->s_journal_uuid))
2453 uuid_unparse(sb->s_journal_uuid, uuid);
2454 journal_path = blkid_get_devname(NULL, "UUID", uuid);
2458 retval = ext2fs_open2(journal_path, io_options,
2459 EXT2_FLAG_JOURNAL_DEV_OK | EXT2_FLAG_RW,
2460 0, 0, unix_io_manager, &jfs);
2462 com_err(program_name, retval,
2463 _("while trying to open %s"),
2468 retval = get_journal_sb(jfs, buf);
2470 if (retval == EXT2_ET_UNSUPP_FEATURE)
2471 fprintf(stderr, _("%s is not a journal device.\n"),
2476 jsb = (journal_superblock_t *) buf;
2477 /* Find the filesystem UUID */
2478 nr_users = ntohl(jsb->s_nr_users);
2480 j_uuid = journal_user(old_uuid, jsb->s_users, nr_users);
2481 if (j_uuid == NULL) {
2482 fputs(_("Filesystem's UUID not found on journal device.\n"),
2484 return EXT2_ET_LOAD_EXT_JOURNAL;
2487 memcpy(j_uuid, sb->s_uuid, UUID_SIZE);
2489 start = ext2fs_journal_sb_start(jfs->blocksize);
2490 /* Write back the journal superblock */
2491 retval = io_channel_write_blk64(jfs->io, start, -SUPERBLOCK_SIZE, buf);
2493 com_err(program_name, retval,
2494 "while writing journal superblock.");
2503 int main(int argc, char **argv)
2507 struct ext2_super_block *sb;
2508 io_manager io_ptr, io_ptr_orig = NULL;
2512 setlocale(LC_MESSAGES, "");
2513 setlocale(LC_CTYPE, "");
2514 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
2515 textdomain(NLS_CAT_NAME);
2516 set_com_err_gettext(gettext);
2519 program_name = *argv;
2520 add_error_table(&et_ext2_error_table);
2522 #ifdef CONFIG_BUILD_FINDFS
2523 if (strcmp(get_progname(argv[0]), "findfs") == 0)
2524 do_findfs(argc, argv);
2526 if (strcmp(get_progname(argv[0]), "e2label") == 0)
2527 parse_e2label_options(argc, argv);
2529 parse_tune2fs_options(argc, argv);
2531 #ifdef CONFIG_TESTIO_DEBUG
2532 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_DEBUG")) {
2533 io_ptr = test_io_manager;
2534 test_io_backing_manager = unix_io_manager;
2537 io_ptr = unix_io_manager;
2540 if ((open_flag & EXT2_FLAG_RW) == 0 || f_flag)
2541 open_flag |= EXT2_FLAG_SKIP_MMP;
2543 open_flag |= EXT2_FLAG_64BITS | EXT2_FLAG_JOURNAL_DEV_OK;
2545 /* keep the filesystem struct around to dump MMP data */
2546 open_flag |= EXT2_FLAG_NOFREE_ON_ERROR;
2548 retval = ext2fs_open2(device_name, io_options, open_flag,
2551 com_err(program_name, retval,
2552 _("while trying to open %s"),
2554 if (retval == EXT2_ET_MMP_FSCK_ON ||
2555 retval == EXT2_ET_MMP_UNKNOWN_SEQ)
2556 dump_mmp_msg(fs->mmp_buf,
2557 _("If you are sure the filesystem "
2558 "is not in use on any node, run:\n"
2559 "'tune2fs -f -E clear_mmp {device}'\n"));
2560 else if (retval == EXT2_ET_MMP_FAILED)
2561 dump_mmp_msg(fs->mmp_buf, NULL);
2562 else if (retval == EXT2_ET_MMP_MAGIC_INVALID)
2564 _("MMP block magic is bad. Try to fix it by "
2565 "running:\n'e2fsck -f %s'\n"), device_name);
2566 else if (retval != EXT2_ET_MMP_FAILED)
2567 fprintf(stderr, "%s",
2568 _("Couldn't find valid filesystem superblock.\n"));
2573 if (EXT2_HAS_INCOMPAT_FEATURE(fs->super,
2574 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
2575 fprintf(stderr, "%s", _("Cannot modify a journal device.\n"));
2579 fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE;
2581 if (I_flag && !io_ptr_orig) {
2583 * Check the inode size is right so we can issue an
2584 * error message and bail before setting up the tdb
2587 if (new_inode_size == EXT2_INODE_SIZE(fs->super)) {
2588 fprintf(stderr, _("The inode size is already %lu\n"),
2593 if (new_inode_size < EXT2_INODE_SIZE(fs->super)) {
2594 fprintf(stderr, "%s",
2595 _("Shrinking inode size is not supported\n"));
2599 if (new_inode_size > fs->blocksize) {
2600 fprintf(stderr, _("Invalid inode size %lu (max %d)\n"),
2601 new_inode_size, fs->blocksize);
2607 * If inode resize is requested use the
2610 io_ptr_orig = io_ptr;
2611 retval = tune2fs_setup_tdb(device_name, &io_ptr);
2616 if (io_ptr != io_ptr_orig) {
2617 ext2fs_close_free(&fs);
2623 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
2626 /* For e2label emulation */
2627 printf("%.*s\n", (int) sizeof(sb->s_volume_name),
2629 remove_error_table(&et_ext2_error_table);
2633 retval = ext2fs_check_if_mounted(device_name, &mount_flags);
2635 com_err("ext2fs_check_if_mount", retval,
2636 _("while determining whether %s is mounted."),
2641 /* Normally we only need to write out the superblock */
2642 fs->flags |= EXT2_FLAG_SUPER_ONLY;
2645 sb->s_max_mnt_count = max_mount_count;
2646 ext2fs_mark_super_dirty(fs);
2647 printf(_("Setting maximal mount count to %d\n"),
2651 sb->s_mnt_count = mount_count;
2652 ext2fs_mark_super_dirty(fs);
2653 printf(_("Setting current mount count to %d\n"), mount_count);
2656 sb->s_errors = errors;
2657 ext2fs_mark_super_dirty(fs);
2658 printf(_("Setting error behavior to %d\n"), errors);
2661 sb->s_def_resgid = resgid;
2662 ext2fs_mark_super_dirty(fs);
2663 printf(_("Setting reserved blocks gid to %lu\n"), resgid);
2666 if ((unsigned long long)interval >= (1ULL << 32)) {
2667 com_err(program_name, 0,
2668 _("interval between checks is too big (%lu)"),
2673 sb->s_checkinterval = interval;
2674 ext2fs_mark_super_dirty(fs);
2675 printf(_("Setting interval between checks to %lu seconds\n"),
2679 ext2fs_r_blocks_count_set(sb, reserved_ratio *
2680 ext2fs_blocks_count(sb) / 100.0);
2681 ext2fs_mark_super_dirty(fs);
2682 printf (_("Setting reserved blocks percentage to %g%% (%llu blocks)\n"),
2683 reserved_ratio, ext2fs_r_blocks_count(sb));
2686 if (reserved_blocks > ext2fs_blocks_count(sb)/2) {
2687 com_err(program_name, 0,
2688 _("reserved blocks count is too big (%llu)"),
2693 ext2fs_r_blocks_count_set(sb, reserved_blocks);
2694 ext2fs_mark_super_dirty(fs);
2695 printf(_("Setting reserved blocks count to %llu\n"),
2699 if (sb->s_feature_ro_compat &
2700 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER) {
2701 fputs(_("\nThe filesystem already has sparse "
2702 "superblocks.\n"), stderr);
2703 } else if (sb->s_feature_incompat &
2704 EXT2_FEATURE_INCOMPAT_META_BG) {
2705 fputs(_("\nSetting the sparse superblock flag not "
2706 "supported\nfor filesystems with "
2707 "the meta_bg feature enabled.\n"),
2712 sb->s_feature_ro_compat |=
2713 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
2714 sb->s_state &= ~EXT2_VALID_FS;
2715 ext2fs_mark_super_dirty(fs);
2716 printf(_("\nSparse superblock flag set. %s"),
2721 fputs(_("\nClearing the sparse superblock flag not supported.\n"),
2727 sb->s_lastcheck = last_check_time;
2728 ext2fs_mark_super_dirty(fs);
2729 printf(_("Setting time filesystem last checked to %s\n"),
2730 ctime(&last_check_time));
2733 sb->s_def_resuid = resuid;
2734 ext2fs_mark_super_dirty(fs);
2735 printf(_("Setting reserved blocks uid to %lu\n"), resuid);
2738 if (strlen(new_label) > sizeof(sb->s_volume_name))
2739 fputs(_("Warning: label too long, truncating.\n"),
2741 memset(sb->s_volume_name, 0, sizeof(sb->s_volume_name));
2742 strncpy(sb->s_volume_name, new_label,
2743 sizeof(sb->s_volume_name));
2744 ext2fs_mark_super_dirty(fs);
2747 memset(sb->s_last_mounted, 0, sizeof(sb->s_last_mounted));
2748 strncpy(sb->s_last_mounted, new_last_mounted,
2749 sizeof(sb->s_last_mounted));
2750 ext2fs_mark_super_dirty(fs);
2753 rc = update_mntopts(fs, mntopts_cmd);
2758 rc = update_feature_set(fs, features_cmd);
2763 rc = parse_extended_opts(fs, extended_cmd);
2766 if (clear_mmp && !f_flag) {
2767 fputs(_("Error in using clear_mmp. "
2768 "It must be used with -f\n"),
2774 rc = ext2fs_mmp_clear(fs);
2777 if (journal_size || journal_device) {
2778 rc = add_journal(fs);
2784 if (mount_flags & EXT2_MF_MOUNTED) {
2785 fputs(_("The quota feature may only be changed when "
2786 "the filesystem is unmounted.\n"), stderr);
2790 handle_quota_options(fs);
2796 char buf[SUPERBLOCK_SIZE];
2797 __u8 old_uuid[UUID_SIZE];
2799 if (ext2fs_has_group_desc_csum(fs)) {
2801 * Changing the UUID requires rewriting all metadata,
2802 * which can race with a mounted fs. Don't allow that.
2804 if (mount_flags & EXT2_MF_MOUNTED) {
2805 fputs(_("The UUID may only be "
2806 "changed when the filesystem is "
2807 "unmounted.\n"), stderr);
2810 if (check_fsck_needed(fs))
2814 * Determine if the block group checksums are
2815 * correct so we know whether or not to set
2818 for (i = 0; i < fs->group_desc_count; i++)
2819 if (!ext2fs_group_desc_csum_verify(fs, i))
2821 if (i >= fs->group_desc_count)
2825 memcpy(old_uuid, sb->s_uuid, UUID_SIZE);
2826 if ((strcasecmp(new_UUID, "null") == 0) ||
2827 (strcasecmp(new_UUID, "clear") == 0)) {
2828 uuid_clear(sb->s_uuid);
2829 } else if (strcasecmp(new_UUID, "time") == 0) {
2830 uuid_generate_time(sb->s_uuid);
2831 } else if (strcasecmp(new_UUID, "random") == 0) {
2832 uuid_generate(sb->s_uuid);
2833 } else if (uuid_parse(new_UUID, sb->s_uuid)) {
2834 com_err(program_name, 0, "%s",
2835 _("Invalid UUID format\n"));
2839 ext2fs_init_csum_seed(fs);
2841 for (i = 0; i < fs->group_desc_count; i++)
2842 ext2fs_group_desc_csum_set(fs, i);
2843 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
2846 /* If this is a journal dev, we need to copy UUID into jsb */
2847 if (!(rc = get_journal_sb(fs, buf))) {
2848 journal_superblock_t *jsb;
2850 jsb = (journal_superblock_t *) buf;
2851 fputs(_("Need to update journal superblock.\n"), stdout);
2852 memcpy(jsb->s_uuid, sb->s_uuid, sizeof(sb->s_uuid));
2854 /* Writeback the journal superblock */
2855 if ((rc = io_channel_write_blk64(fs->io,
2856 ext2fs_journal_sb_start(fs->blocksize),
2857 -SUPERBLOCK_SIZE, buf)))
2859 } else if (rc != EXT2_ET_UNSUPP_FEATURE)
2862 rc = 0; /** Reset rc to avoid ext2fs_mmp_stop() */
2864 if ((rc = fs_update_journal_user(sb, old_uuid)))
2868 ext2fs_mark_super_dirty(fs);
2869 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
2870 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
2871 rewrite_checksums = 1;
2873 if (rewrite_checksums)
2874 rewrite_metadata_checksums(fs);
2876 if (mount_flags & EXT2_MF_MOUNTED) {
2877 fputs(_("The inode size may only be "
2878 "changed when the filesystem is "
2879 "unmounted.\n"), stderr);
2883 if (fs->super->s_feature_incompat &
2884 EXT4_FEATURE_INCOMPAT_FLEX_BG) {
2885 fputs(_("Changing the inode size not supported for "
2886 "filesystems with the flex_bg\n"
2887 "feature enabled.\n"),
2893 * We want to update group descriptor also
2894 * with the new free inode count
2896 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
2897 if (resize_inode(fs, new_inode_size) == 0) {
2898 printf(_("Setting inode size %lu\n"),
2901 printf("%s", _("Failed to change inode size\n"));
2910 sb->s_raid_stride = stride;
2911 ext2fs_mark_super_dirty(fs);
2912 printf(_("Setting stride size to %d\n"), stride);
2914 if (stripe_width_set) {
2915 sb->s_raid_stripe_width = stripe_width;
2916 ext2fs_mark_super_dirty(fs);
2917 printf(_("Setting stripe width to %d\n"), stripe_width);
2919 if (ext_mount_opts) {
2920 strncpy((char *)(fs->super->s_mount_opts), ext_mount_opts,
2921 sizeof(fs->super->s_mount_opts));
2922 fs->super->s_mount_opts[sizeof(fs->super->s_mount_opts)-1] = 0;
2923 ext2fs_mark_super_dirty(fs);
2924 printf(_("Setting extended default mount options to '%s'\n"),
2926 free(ext_mount_opts);
2929 remove_error_table(&et_ext2_error_table);
2933 ext2fs_mmp_stop(fs);
2937 return (ext2fs_close_free(&fs) ? 1 : 0);