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;
101 int journal_size, journal_flags;
102 char *journal_device;
103 static blk64_t journal_location = ~0LL;
105 static struct list_head blk_move_list;
108 struct list_head list;
114 static const char *please_fsck = N_("Please run e2fsck on the filesystem.\n");
115 static const char *please_dir_fsck =
116 N_("Please run e2fsck -D on the filesystem.\n");
118 #ifdef CONFIG_BUILD_FINDFS
119 void do_findfs(int argc, char **argv);
122 static void usage(void)
125 _("Usage: %s [-c max_mounts_count] [-e errors_behavior] "
127 "\t[-i interval[d|m|w]] [-j] [-J journal_options] [-l]\n"
128 "\t[-m reserved_blocks_percent] "
129 "[-o [^]mount_options[,...]] [-p mmp_update_interval]\n"
130 "\t[-r reserved_blocks_count] [-u user] [-C mount_count] "
131 "[-L volume_label]\n"
132 "\t[-M last_mounted_dir] [-O [^]feature[,...]]\n"
134 "\t[-Q quota_options]\n"
136 "\t[-E extended-option[,...]] [-T last_check_time] "
137 "[-U UUID]\n\t[ -I new_inode_size ] device\n"), program_name);
141 static __u32 ok_features[3] = {
143 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
144 EXT2_FEATURE_COMPAT_DIR_INDEX,
146 EXT2_FEATURE_INCOMPAT_FILETYPE |
147 EXT3_FEATURE_INCOMPAT_EXTENTS |
148 EXT4_FEATURE_INCOMPAT_FLEX_BG |
149 EXT4_FEATURE_INCOMPAT_MMP,
151 EXT2_FEATURE_RO_COMPAT_LARGE_FILE |
152 EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
153 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
154 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
155 EXT4_FEATURE_RO_COMPAT_GDT_CSUM |
156 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER |
158 EXT4_FEATURE_RO_COMPAT_QUOTA |
160 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
163 static __u32 clear_ok_features[3] = {
165 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
166 EXT2_FEATURE_COMPAT_RESIZE_INODE |
167 EXT2_FEATURE_COMPAT_DIR_INDEX,
169 EXT2_FEATURE_INCOMPAT_FILETYPE |
170 EXT4_FEATURE_INCOMPAT_FLEX_BG |
171 EXT4_FEATURE_INCOMPAT_MMP,
173 EXT2_FEATURE_RO_COMPAT_LARGE_FILE |
174 EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
175 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
176 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
177 EXT4_FEATURE_RO_COMPAT_GDT_CSUM |
179 EXT4_FEATURE_RO_COMPAT_QUOTA |
181 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
185 * Try to get journal super block if any
187 static int get_journal_sb(ext2_filsys jfs, char buf[SUPERBLOCK_SIZE])
190 journal_superblock_t *jsb;
192 if (!(jfs->super->s_feature_incompat &
193 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
194 return EXT2_ET_UNSUPP_FEATURE;
197 /* Get the journal superblock */
198 if ((retval = io_channel_read_blk64(jfs->io,
199 ext2fs_journal_sb_start(jfs->blocksize), -SUPERBLOCK_SIZE, buf))) {
200 com_err(program_name, retval, "%s",
201 _("while reading journal superblock"));
205 jsb = (journal_superblock_t *) buf;
206 if ((jsb->s_header.h_magic != (unsigned)ntohl(JFS_MAGIC_NUMBER)) ||
207 (jsb->s_header.h_blocktype != (unsigned)ntohl(JFS_SUPERBLOCK_V2))) {
208 fputs(_("Journal superblock not found!\n"), stderr);
209 return EXT2_ET_BAD_MAGIC;
215 static __u8 *journal_user(__u8 uuid[UUID_SIZE], __u8 s_users[JFS_USERS_SIZE],
219 for (i = 0; i < nr_users; i++) {
220 if (memcmp(uuid, &s_users[i * UUID_SIZE], UUID_SIZE) == 0)
221 return &s_users[i * UUID_SIZE];
228 * Remove an external journal from the filesystem
230 static int remove_journal_device(ext2_filsys fs)
234 char buf[SUPERBLOCK_SIZE];
235 journal_superblock_t *jsb;
238 int commit_remove_journal = 0;
242 commit_remove_journal = 1; /* force removal even if error */
244 uuid_unparse(fs->super->s_journal_uuid, buf);
245 journal_path = blkid_get_devname(NULL, "UUID", buf);
249 ext2fs_find_block_device(fs->super->s_journal_dev);
251 goto no_valid_journal;
254 #ifdef CONFIG_TESTIO_DEBUG
255 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
256 io_ptr = test_io_manager;
257 test_io_backing_manager = unix_io_manager;
260 io_ptr = unix_io_manager;
261 retval = ext2fs_open(journal_path, EXT2_FLAG_RW|
262 EXT2_FLAG_JOURNAL_DEV_OK, 0,
263 fs->blocksize, io_ptr, &jfs);
265 com_err(program_name, retval, "%s",
266 _("while trying to open external journal"));
267 goto no_valid_journal;
270 if ((retval = get_journal_sb(jfs, buf))) {
271 if (retval == EXT2_ET_UNSUPP_FEATURE)
272 fprintf(stderr, _("%s is not a journal device.\n"),
274 goto no_valid_journal;
277 jsb = (journal_superblock_t *) buf;
278 /* Find the filesystem UUID */
279 nr_users = ntohl(jsb->s_nr_users);
281 if (!journal_user(fs->super->s_uuid, jsb->s_users, nr_users)) {
282 fputs(_("Filesystem's UUID not found on journal device.\n"),
284 commit_remove_journal = 1;
285 goto no_valid_journal;
288 for (i = 0; i < nr_users; i++)
289 memcpy(&jsb->s_users[i * 16], &jsb->s_users[(i + 1) * 16], 16);
290 jsb->s_nr_users = htonl(nr_users);
292 /* Write back the journal superblock */
293 retval = io_channel_write_blk64(jfs->io,
294 ext2fs_journal_sb_start(fs->blocksize),
295 -SUPERBLOCK_SIZE, buf);
297 com_err(program_name, retval,
298 "while writing journal superblock.");
299 goto no_valid_journal;
302 commit_remove_journal = 1;
305 if (commit_remove_journal == 0) {
306 fputs(_("Cannot locate journal device. It was NOT removed\n"
307 "Use -f option to remove missing journal device.\n"),
311 fs->super->s_journal_dev = 0;
312 memset(fs->super->s_jnl_blocks, 0, sizeof(fs->super->s_jnl_blocks));
313 uuid_clear(fs->super->s_journal_uuid);
314 ext2fs_mark_super_dirty(fs);
315 fputs(_("Journal removed\n"), stdout);
321 /* Helper function for remove_journal_inode */
322 static int release_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
323 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
324 blk64_t ref_block EXT2FS_ATTR((unused)),
325 int ref_offset EXT2FS_ATTR((unused)),
326 void *private EXT2FS_ATTR((unused)))
332 ext2fs_unmark_block_bitmap2(fs->block_map, block);
333 group = ext2fs_group_of_blk2(fs, block);
334 ext2fs_bg_free_blocks_count_set(fs, group, ext2fs_bg_free_blocks_count(fs, group) + 1);
335 ext2fs_group_desc_csum_set(fs, group);
336 ext2fs_free_blocks_count_add(fs->super, EXT2FS_CLUSTER_RATIO(fs));
341 * Remove the journal inode from the filesystem
343 static errcode_t remove_journal_inode(ext2_filsys fs)
345 struct ext2_inode inode;
347 ino_t ino = fs->super->s_journal_inum;
349 retval = ext2fs_read_inode(fs, ino, &inode);
351 com_err(program_name, retval, "%s",
352 _("while reading journal inode"));
355 if (ino == EXT2_JOURNAL_INO) {
356 retval = ext2fs_read_bitmaps(fs);
358 com_err(program_name, retval, "%s",
359 _("while reading bitmaps"));
362 retval = ext2fs_block_iterate3(fs, ino,
363 BLOCK_FLAG_READ_ONLY, NULL,
364 release_blocks_proc, NULL);
366 com_err(program_name, retval, "%s",
367 _("while clearing journal inode"));
370 memset(&inode, 0, sizeof(inode));
371 ext2fs_mark_bb_dirty(fs);
372 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
374 inode.i_flags &= ~EXT2_IMMUTABLE_FL;
375 retval = ext2fs_write_inode(fs, ino, &inode);
377 com_err(program_name, retval, "%s",
378 _("while writing journal inode"));
381 fs->super->s_journal_inum = 0;
382 memset(fs->super->s_jnl_blocks, 0, sizeof(fs->super->s_jnl_blocks));
383 ext2fs_mark_super_dirty(fs);
389 * Update the default mount options
391 static int update_mntopts(ext2_filsys fs, char *mntopts)
393 struct ext2_super_block *sb = fs->super;
395 if (e2p_edit_mntopts(mntopts, &sb->s_default_mount_opts, ~0)) {
396 fprintf(stderr, _("Invalid mount option set: %s\n"),
400 ext2fs_mark_super_dirty(fs);
405 static int check_fsck_needed(ext2_filsys fs)
407 if (fs->super->s_state & EXT2_VALID_FS)
409 printf("\n%s\n", _(please_fsck));
410 if (mount_flags & EXT2_MF_READONLY)
411 printf("%s", _("(and reboot afterwards!)\n"));
415 static void request_dir_fsck_afterwards(ext2_filsys fs)
417 static int requested;
421 fs->super->s_state &= ~EXT2_VALID_FS;
422 printf("\n%s\n", _(please_dir_fsck));
423 if (mount_flags & EXT2_MF_READONLY)
424 printf("%s", _("(and reboot afterwards!)\n"));
427 static void request_fsck_afterwards(ext2_filsys fs)
429 static int requested = 0;
433 fs->super->s_state &= ~EXT2_VALID_FS;
434 printf("\n%s\n", _(please_fsck));
435 if (mount_flags & EXT2_MF_READONLY)
436 printf("%s", _("(and reboot afterwards!)\n"));
439 /* Rewrite extents */
440 static errcode_t rewrite_extents(ext2_filsys fs, ext2_ino_t ino,
441 struct ext2_inode *inode)
443 ext2_extent_handle_t handle;
444 struct ext2fs_extent extent;
446 struct ext2_extent_info info;
448 if (!(inode->i_flags & EXT4_EXTENTS_FL) ||
449 !EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
450 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
453 errcode = ext2fs_extent_open(fs, ino, &handle);
457 errcode = ext2fs_extent_get(handle, EXT2_EXTENT_ROOT, &extent);
462 errcode = ext2fs_extent_get_info(handle, &info);
467 * If this is the first extent in an extent block that we
468 * haven't visited, rewrite the extent to force the ETB
469 * checksum to be rewritten.
471 if (info.curr_entry == 1 && info.curr_level != 0 &&
472 !(extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT)) {
473 errcode = ext2fs_extent_replace(handle, 0, &extent);
478 /* Skip to the end of a block of leaf nodes */
479 if (extent.e_flags & EXT2_EXTENT_FLAGS_LEAF) {
480 errcode = ext2fs_extent_get(handle,
481 EXT2_EXTENT_LAST_SIB,
487 errcode = ext2fs_extent_get(handle, EXT2_EXTENT_NEXT, &extent);
488 } while (errcode == 0);
491 /* Ok if we run off the end */
492 if (errcode == EXT2_ET_EXTENT_NO_NEXT)
494 ext2fs_extent_free(handle);
499 * Rewrite directory blocks with checksums
501 struct rewrite_dir_context {
508 static int rewrite_dir_block(ext2_filsys fs,
510 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
511 blk64_t ref_block EXT2FS_ATTR((unused)),
512 int ref_offset EXT2FS_ATTR((unused)),
515 struct ext2_dx_countlimit *dcl = NULL;
516 struct rewrite_dir_context *ctx = priv_data;
517 int dcl_offset, changed = 0;
519 ctx->errcode = ext2fs_read_dir_block4(fs, *blocknr, ctx->buf, 0,
524 /* if htree node... */
526 ext2fs_get_dx_countlimit(fs, (struct ext2_dir_entry *)ctx->buf,
529 if (!EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
530 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
531 /* Ensure limit is the max size */
532 int max_entries = (fs->blocksize - dcl_offset) /
533 sizeof(struct ext2_dx_entry);
534 if (ext2fs_le16_to_cpu(dcl->limit) != max_entries) {
536 dcl->limit = ext2fs_cpu_to_le16(max_entries);
539 /* If htree block is full then rebuild the dir */
540 if (ext2fs_le16_to_cpu(dcl->count) ==
541 ext2fs_le16_to_cpu(dcl->limit)) {
542 request_dir_fsck_afterwards(fs);
546 * Ensure dcl->limit is small enough to leave room for
549 int max_entries = (fs->blocksize - (dcl_offset +
550 sizeof(struct ext2_dx_tail))) /
551 sizeof(struct ext2_dx_entry);
552 if (ext2fs_le16_to_cpu(dcl->limit) != max_entries)
553 dcl->limit = ext2fs_cpu_to_le16(max_entries);
554 /* Always rewrite checksum */
558 unsigned int rec_len, name_size;
559 char *top = ctx->buf + fs->blocksize;
560 struct ext2_dir_entry *de = (struct ext2_dir_entry *)ctx->buf;
561 struct ext2_dir_entry *last_de = NULL, *penultimate_de = NULL;
563 /* Find last and penultimate dirent */
564 while ((char *)de < top) {
565 penultimate_de = last_de;
567 ctx->errcode = ext2fs_get_rec_len(fs, de, &rec_len);
568 if (!ctx->errcode && !rec_len)
569 ctx->errcode = EXT2_ET_DIR_CORRUPTED;
572 de = (struct ext2_dir_entry *)(((char *)de) + rec_len);
574 ctx->errcode = ext2fs_get_rec_len(fs, last_de, &rec_len);
577 name_size = ext2fs_dirent_name_len(last_de);
579 if (!EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
580 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
583 if (last_de->inode ||
585 rec_len != sizeof(struct ext2_dir_entry_tail))
588 * The last dirent is unused and the right length to
589 * have stored a checksum. Erase it.
591 ctx->errcode = ext2fs_get_rec_len(fs, penultimate_de,
594 ctx->errcode = EXT2_ET_DIR_CORRUPTED;
597 ext2fs_set_rec_len(fs, rec_len +
598 sizeof(struct ext2_dir_entry_tail),
602 unsigned csum_size = sizeof(struct ext2_dir_entry_tail);
603 struct ext2_dir_entry_tail *t;
606 * If the last dirent looks like the tail, just update
609 if (!last_de->inode &&
610 rec_len == csum_size) {
611 t = (struct ext2_dir_entry_tail *)last_de;
612 t->det_reserved_name_len =
613 EXT2_DIR_NAME_LEN_CSUM;
618 name_size = (name_size & ~3) + 4;
619 /* If there's not enough space for the tail, e2fsck */
620 if (rec_len <= (8 + name_size + csum_size)) {
621 request_dir_fsck_afterwards(fs);
624 /* Shorten that last de and insert the tail */
625 ext2fs_set_rec_len(fs, rec_len - csum_size, last_de);
626 t = EXT2_DIRENT_TAIL(ctx->buf, fs->blocksize);
627 ext2fs_initialize_dirent_tail(fs, t);
629 /* Always update checksum */
638 ctx->errcode = ext2fs_write_dir_block4(fs, *blocknr, ctx->buf,
646 static errcode_t rewrite_directory(ext2_filsys fs, ext2_ino_t dir,
647 struct ext2_inode *inode)
650 struct rewrite_dir_context ctx;
652 retval = ext2fs_get_mem(fs->blocksize, &ctx.buf);
656 ctx.is_htree = (inode->i_flags & EXT2_INDEX_FL);
659 retval = ext2fs_block_iterate3(fs, dir, BLOCK_FLAG_READ_ONLY |
660 BLOCK_FLAG_DATA_ONLY,
661 0, rewrite_dir_block, &ctx);
663 ext2fs_free_mem(&ctx.buf);
671 * Forcibly set checksums in all inodes.
673 static void rewrite_inodes(ext2_filsys fs)
675 int length = EXT2_INODE_SIZE(fs->super);
676 struct ext2_inode *inode, *zero;
678 ext2_inode_scan scan;
681 blk64_t file_acl_block;
684 if (fs->super->s_creator_os != EXT2_OS_LINUX)
687 retval = ext2fs_open_inode_scan(fs, 0, &scan);
689 com_err("set_csum", retval, "while opening inode scan");
693 retval = ext2fs_get_mem(length, &inode);
695 com_err("set_csum", retval, "while allocating memory");
699 retval = ext2fs_get_memzero(length, &zero);
701 com_err("set_csum", retval, "while allocating memory");
705 retval = ext2fs_get_mem(fs->blocksize, &ea_buf);
707 com_err("set_csum", retval, "while allocating memory");
712 retval = ext2fs_get_next_inode_full(scan, &ino, inode, length);
714 com_err("set_csum", retval, "while getting next inode");
719 if (ext2fs_test_inode_bitmap2(fs->inode_map, ino)) {
722 if (memcmp(inode, zero, length) != 0) {
723 memset(inode, 0, length);
731 retval = ext2fs_write_inode_full(fs, ino, inode,
734 com_err("set_csum", retval, "while writing "
740 retval = rewrite_extents(fs, ino, inode);
742 com_err("rewrite_extents", retval,
743 "while rewriting extents");
747 if (LINUX_S_ISDIR(inode->i_mode) &&
748 ext2fs_inode_has_valid_blocks2(fs, inode)) {
749 retval = rewrite_directory(fs, ino, inode);
751 com_err("rewrite_directory", retval,
752 "while rewriting directories");
757 file_acl_block = ext2fs_file_acl_block(fs, inode);
760 retval = ext2fs_read_ext_attr3(fs, file_acl_block, ea_buf, ino);
762 com_err("rewrite_eablock", retval,
763 "while rewriting extended attribute");
766 retval = ext2fs_write_ext_attr3(fs, file_acl_block, ea_buf,
769 com_err("rewrite_eablock", retval,
770 "while rewriting extended attribute");
775 ext2fs_free_mem(&zero);
776 ext2fs_free_mem(&inode);
777 ext2fs_free_mem(&ea_buf);
778 ext2fs_close_inode_scan(scan);
781 static void rewrite_metadata_checksums(ext2_filsys fs)
786 fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
787 ext2fs_init_csum_seed(fs);
788 for (i = 0; i < fs->group_desc_count; i++)
789 ext2fs_group_desc_csum_set(fs, i);
790 retval = ext2fs_read_bitmaps(fs);
792 com_err("rewrite_metadata_checksums", retval,
793 "while reading bitmaps");
797 ext2fs_mark_ib_dirty(fs);
798 ext2fs_mark_bb_dirty(fs);
799 ext2fs_mmp_update2(fs, 1);
800 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
801 fs->flags &= ~EXT2_FLAG_IGNORE_CSUM_ERRORS;
802 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
803 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
804 fs->super->s_checksum_type = EXT2_CRC32C_CHKSUM;
806 fs->super->s_checksum_type = 0;
807 ext2fs_mark_super_dirty(fs);
810 static void enable_uninit_bg(ext2_filsys fs)
812 struct ext2_group_desc *gd;
815 for (i = 0; i < fs->group_desc_count; i++) {
816 gd = ext2fs_group_desc(fs, fs->group_desc, i);
817 gd->bg_itable_unused = 0;
818 gd->bg_flags = EXT2_BG_INODE_ZEROED;
819 ext2fs_group_desc_csum_set(fs, i);
821 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
824 static errcode_t zero_empty_inodes(ext2_filsys fs)
826 int length = EXT2_INODE_SIZE(fs->super);
827 struct ext2_inode *inode = NULL;
828 ext2_inode_scan scan;
832 retval = ext2fs_open_inode_scan(fs, 0, &scan);
836 retval = ext2fs_get_mem(length, &inode);
841 retval = ext2fs_get_next_inode_full(scan, &ino, inode, length);
846 if (!ext2fs_test_inode_bitmap2(fs->inode_map, ino)) {
847 memset(inode, 0, length);
848 retval = ext2fs_write_inode_full(fs, ino, inode,
856 ext2fs_free_mem(&inode);
857 ext2fs_close_inode_scan(scan);
861 static errcode_t disable_uninit_bg(ext2_filsys fs, __u32 csum_feature_flag)
863 struct ext2_group_desc *gd;
868 /* Load bitmaps to ensure that the uninit ones get written out */
869 fs->super->s_feature_ro_compat |= csum_feature_flag;
870 retval = ext2fs_read_bitmaps(fs);
871 fs->super->s_feature_ro_compat &= ~csum_feature_flag;
873 com_err("disable_uninit_bg", retval,
874 "while reading bitmaps");
875 request_fsck_afterwards(fs);
878 ext2fs_mark_ib_dirty(fs);
879 ext2fs_mark_bb_dirty(fs);
881 /* If we're only turning off uninit_bg, zero the inodes */
882 if (csum_feature_flag == EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
883 retval = zero_empty_inodes(fs);
885 com_err("disable_uninit_bg", retval,
886 "while zeroing unused inodes");
887 request_fsck_afterwards(fs);
892 /* The bbitmap is zeroed; we must mark group metadata blocks in use */
893 for (i = 0; i < fs->group_desc_count; i++) {
894 b = ext2fs_block_bitmap_loc(fs, i);
895 ext2fs_mark_block_bitmap2(fs->block_map, b);
896 b = ext2fs_inode_bitmap_loc(fs, i);
897 ext2fs_mark_block_bitmap2(fs->block_map, b);
899 retval = ext2fs_super_and_bgd_loc2(fs, i, &b, &c, &d, NULL);
900 if (retval == 0 && b)
901 ext2fs_mark_block_bitmap2(fs->block_map, b);
902 if (retval == 0 && c)
903 ext2fs_mark_block_bitmap2(fs->block_map, c);
904 if (retval == 0 && d)
905 ext2fs_mark_block_bitmap2(fs->block_map, d);
907 com_err("disable_uninit_bg", retval,
908 "while initializing block bitmaps");
909 request_fsck_afterwards(fs);
912 gd = ext2fs_group_desc(fs, fs->group_desc, i);
913 gd->bg_itable_unused = 0;
915 ext2fs_group_desc_csum_set(fs, i);
917 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
918 ext2fs_mark_super_dirty(fs);
924 * Update the feature set as provided by the user.
926 static int update_feature_set(ext2_filsys fs, char *features)
928 struct ext2_super_block *sb = fs->super;
929 __u32 old_features[3];
931 unsigned int mask_err;
934 #define FEATURE_ON(type, mask) (!(old_features[(type)] & (mask)) && \
935 ((&sb->s_feature_compat)[(type)] & (mask)))
936 #define FEATURE_OFF(type, mask) ((old_features[(type)] & (mask)) && \
937 !((&sb->s_feature_compat)[(type)] & (mask)))
938 #define FEATURE_CHANGED(type, mask) ((mask) & \
939 (old_features[(type)] ^ (&sb->s_feature_compat)[(type)]))
941 old_features[E2P_FEATURE_COMPAT] = sb->s_feature_compat;
942 old_features[E2P_FEATURE_INCOMPAT] = sb->s_feature_incompat;
943 old_features[E2P_FEATURE_RO_INCOMPAT] = sb->s_feature_ro_compat;
945 if (e2p_edit_feature2(features, &sb->s_feature_compat,
946 ok_features, clear_ok_features,
947 &type_err, &mask_err)) {
950 _("Invalid filesystem option set: %s\n"),
952 else if (type_err & E2P_FEATURE_NEGATE_FLAG)
953 fprintf(stderr, _("Clearing filesystem feature '%s' "
955 e2p_feature2string(type_err &
956 E2P_FEATURE_TYPE_MASK,
959 fprintf(stderr, _("Setting filesystem feature '%s' "
961 e2p_feature2string(type_err, mask_err));
965 if (FEATURE_OFF(E2P_FEATURE_COMPAT, EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
966 if ((mount_flags & EXT2_MF_MOUNTED) &&
967 !(mount_flags & EXT2_MF_READONLY)) {
968 fputs(_("The has_journal feature may only be "
969 "cleared when the filesystem is\n"
970 "unmounted or mounted "
971 "read-only.\n"), stderr);
974 if ((sb->s_feature_incompat &
975 EXT3_FEATURE_INCOMPAT_RECOVER) &&
977 fputs(_("The needs_recovery flag is set. "
978 "Please run e2fsck before clearing\n"
979 "the has_journal flag.\n"), stderr);
982 if (sb->s_journal_inum) {
983 if (remove_journal_inode(fs))
986 if (sb->s_journal_dev) {
987 if (remove_journal_device(fs))
992 if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT,
993 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
994 if (sb->s_feature_incompat &
995 EXT2_FEATURE_INCOMPAT_META_BG) {
996 fputs(_("Setting filesystem feature 'sparse_super' "
997 "not supported\nfor filesystems with "
998 "the meta_bg feature enabled.\n"),
1004 if (FEATURE_ON(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_MMP)) {
1007 if ((mount_flags & EXT2_MF_MOUNTED) ||
1008 (mount_flags & EXT2_MF_READONLY)) {
1009 fputs(_("The multiple mount protection feature can't\n"
1010 "be set if the filesystem is mounted or\n"
1011 "read-only.\n"), stderr);
1015 error = ext2fs_mmp_init(fs);
1017 fputs(_("\nError while enabling multiple mount "
1018 "protection feature."), stderr);
1023 * We want to update group desc with the new free blocks count
1025 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
1027 printf(_("Multiple mount protection has been enabled "
1028 "with update interval %ds.\n"),
1029 sb->s_mmp_update_interval);
1032 if (FEATURE_OFF(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_MMP)) {
1035 if (mount_flags & EXT2_MF_READONLY) {
1036 fputs(_("The multiple mount protection feature cannot\n"
1037 "be disabled if the filesystem is readonly.\n"),
1042 error = ext2fs_read_bitmaps(fs);
1044 fputs(_("Error while reading bitmaps\n"), stderr);
1048 error = ext2fs_mmp_read(fs, sb->s_mmp_block, NULL);
1050 struct mmp_struct *mmp_cmp = fs->mmp_cmp;
1052 if (error == EXT2_ET_MMP_MAGIC_INVALID)
1053 printf(_("Magic number in MMP block does not "
1054 "match. expected: %x, actual: %x\n"),
1055 EXT4_MMP_MAGIC, mmp_cmp->mmp_magic);
1057 com_err(program_name, error, "%s",
1058 _("while reading MMP block."));
1062 /* We need to force out the group descriptors as well */
1063 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
1064 ext2fs_block_alloc_stats2(fs, sb->s_mmp_block, -1);
1066 sb->s_mmp_block = 0;
1067 sb->s_mmp_update_interval = 0;
1070 if (FEATURE_ON(E2P_FEATURE_COMPAT, EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
1072 * If adding a journal flag, let the create journal
1073 * code below handle setting the flag and creating the
1074 * journal. We supply a default size if necessary.
1078 sb->s_feature_compat &= ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1081 if (FEATURE_ON(E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_DIR_INDEX)) {
1082 if (!sb->s_def_hash_version)
1083 sb->s_def_hash_version = EXT2_HASH_HALF_MD4;
1084 if (uuid_is_null((unsigned char *) sb->s_hash_seed))
1085 uuid_generate((unsigned char *) sb->s_hash_seed);
1088 if (FEATURE_OFF(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
1089 if (ext2fs_check_desc(fs)) {
1090 fputs(_("Clearing the flex_bg flag would "
1091 "cause the the filesystem to be\n"
1092 "inconsistent.\n"), stderr);
1097 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
1098 EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
1099 if ((mount_flags & EXT2_MF_MOUNTED) &&
1100 !(mount_flags & EXT2_MF_READONLY)) {
1101 fputs(_("The huge_file feature may only be "
1102 "cleared when the filesystem is\n"
1103 "unmounted or mounted "
1104 "read-only.\n"), stderr);
1109 if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT,
1110 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
1111 if (check_fsck_needed(fs))
1113 if (mount_flags & EXT2_MF_MOUNTED)
1114 fputs(_("Cannot enable metadata_csum on a mounted "
1115 "filesystem!\n"), stderr);
1116 rewrite_checksums = 1;
1117 /* metadata_csum supersedes uninit_bg */
1118 fs->super->s_feature_ro_compat &=
1119 ~EXT4_FEATURE_RO_COMPAT_GDT_CSUM;
1121 /* if uninit_bg was previously off, rewrite group desc */
1122 if (!(old_features[E2P_FEATURE_RO_INCOMPAT] &
1123 EXT4_FEATURE_RO_COMPAT_GDT_CSUM))
1124 enable_uninit_bg(fs);
1127 * Since metadata_csum supersedes uninit_bg, pretend like
1128 * uninit_bg has been off all along.
1130 old_features[E2P_FEATURE_RO_INCOMPAT] &=
1131 ~EXT4_FEATURE_RO_COMPAT_GDT_CSUM;
1134 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
1135 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
1136 if (check_fsck_needed(fs))
1138 if (mount_flags & EXT2_MF_MOUNTED)
1139 fputs(_("Cannot disable metadata_csum on a mounted "
1140 "filesystem!\n"), stderr);
1141 rewrite_checksums = 1;
1143 * If we're turning off metadata_csum and not turning on
1144 * uninit_bg, rewrite group desc.
1146 if (!(fs->super->s_feature_ro_compat &
1147 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
1148 err = disable_uninit_bg(fs,
1149 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM);
1154 * metadata_csum previously provided uninit_bg, so if
1155 * we're also setting the uninit_bg feature bit,
1156 * pretend like it was previously enabled. Checksums
1157 * will be rewritten with crc16 later.
1159 old_features[E2P_FEATURE_RO_INCOMPAT] |=
1160 EXT4_FEATURE_RO_COMPAT_GDT_CSUM;
1163 if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT,
1164 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
1165 /* Do not enable uninit_bg when metadata_csum enabled */
1166 if (fs->super->s_feature_ro_compat &
1167 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)
1168 fs->super->s_feature_ro_compat &=
1169 ~EXT4_FEATURE_RO_COMPAT_GDT_CSUM;
1171 enable_uninit_bg(fs);
1174 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
1175 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
1176 err = disable_uninit_bg(fs,
1177 EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
1182 if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT,
1183 EXT4_FEATURE_RO_COMPAT_QUOTA)) {
1185 * Set the Q_flag here and handle the quota options in the code
1190 /* Enable both user quota and group quota by default */
1191 usrquota = QOPT_ENABLE;
1192 grpquota = QOPT_ENABLE;
1194 sb->s_feature_ro_compat &= ~EXT4_FEATURE_RO_COMPAT_QUOTA;
1197 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
1198 EXT4_FEATURE_RO_COMPAT_QUOTA)) {
1200 * Set the Q_flag here and handle the quota options in the code
1204 fputs(_("\nWarning: '^quota' option overrides '-Q'"
1205 "arguments.\n"), stderr);
1207 /* Disable both user quota and group quota by default */
1208 usrquota = QOPT_DISABLE;
1209 grpquota = QOPT_DISABLE;
1212 if (sb->s_rev_level == EXT2_GOOD_OLD_REV &&
1213 (sb->s_feature_compat || sb->s_feature_ro_compat ||
1214 sb->s_feature_incompat))
1215 ext2fs_update_dynamic_rev(fs);
1217 if (FEATURE_CHANGED(E2P_FEATURE_RO_INCOMPAT,
1218 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER) ||
1219 FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
1220 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) ||
1221 FEATURE_CHANGED(E2P_FEATURE_INCOMPAT,
1222 EXT2_FEATURE_INCOMPAT_FILETYPE) ||
1223 FEATURE_CHANGED(E2P_FEATURE_COMPAT,
1224 EXT2_FEATURE_COMPAT_RESIZE_INODE) ||
1225 FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
1226 EXT2_FEATURE_RO_COMPAT_LARGE_FILE))
1227 request_fsck_afterwards(fs);
1229 if ((old_features[E2P_FEATURE_COMPAT] != sb->s_feature_compat) ||
1230 (old_features[E2P_FEATURE_INCOMPAT] != sb->s_feature_incompat) ||
1231 (old_features[E2P_FEATURE_RO_INCOMPAT] != sb->s_feature_ro_compat))
1232 ext2fs_mark_super_dirty(fs);
1238 * Add a journal to the filesystem.
1240 static int add_journal(ext2_filsys fs)
1242 unsigned long journal_blocks;
1247 if (fs->super->s_feature_compat &
1248 EXT3_FEATURE_COMPAT_HAS_JOURNAL) {
1249 fputs(_("The filesystem already has a journal.\n"), stderr);
1252 if (journal_device) {
1253 if (!check_plausibility(journal_device, CHECK_BLOCK_DEV,
1255 proceed_question(-1);
1256 check_mount(journal_device, 0, _("journal"));
1257 #ifdef CONFIG_TESTIO_DEBUG
1258 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
1259 io_ptr = test_io_manager;
1260 test_io_backing_manager = unix_io_manager;
1263 io_ptr = unix_io_manager;
1264 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
1265 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1266 fs->blocksize, io_ptr, &jfs);
1268 com_err(program_name, retval,
1269 _("\n\twhile trying to open journal on %s\n"),
1273 printf(_("Creating journal on device %s: "),
1277 retval = ext2fs_add_journal_device(fs, jfs);
1278 ext2fs_close_free(&jfs);
1280 com_err(program_name, retval,
1281 _("while adding filesystem to journal on %s"),
1285 fputs(_("done\n"), stdout);
1286 } else if (journal_size) {
1287 fputs(_("Creating journal inode: "), stdout);
1289 journal_blocks = figure_journal_size(journal_size, fs);
1291 if (journal_location_string)
1293 parse_num_blocks2(journal_location_string,
1294 fs->super->s_log_block_size);
1295 retval = ext2fs_add_journal_inode2(fs, journal_blocks,
1299 fprintf(stderr, "\n");
1300 com_err(program_name, retval, "%s",
1301 _("\n\twhile trying to create journal file"));
1304 fputs(_("done\n"), stdout);
1306 * If the filesystem wasn't mounted, we need to force
1307 * the block group descriptors out.
1309 if ((mount_flags & EXT2_MF_MOUNTED) == 0)
1310 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
1312 print_check_message(fs->super->s_max_mnt_count,
1313 fs->super->s_checkinterval);
1317 free(journal_device);
1321 static void handle_quota_options(ext2_filsys fs)
1326 if (!usrquota && !grpquota)
1327 /* Nothing to do. */
1330 quota_init_context(&qctx, fs, -1);
1332 if (usrquota == QOPT_ENABLE || grpquota == QOPT_ENABLE)
1333 quota_compute_usage(qctx);
1335 if (usrquota == QOPT_ENABLE && !fs->super->s_usr_quota_inum) {
1336 if ((qf_ino = quota_file_exists(fs, USRQUOTA,
1338 quota_update_limits(qctx, qf_ino, USRQUOTA);
1339 quota_write_inode(qctx, USRQUOTA);
1340 } else if (usrquota == QOPT_DISABLE) {
1341 quota_remove_inode(fs, USRQUOTA);
1344 if (grpquota == QOPT_ENABLE && !fs->super->s_grp_quota_inum) {
1345 if ((qf_ino = quota_file_exists(fs, GRPQUOTA,
1347 quota_update_limits(qctx, qf_ino, GRPQUOTA);
1348 quota_write_inode(qctx, GRPQUOTA);
1349 } else if (grpquota == QOPT_DISABLE) {
1350 quota_remove_inode(fs, GRPQUOTA);
1353 quota_release_context(&qctx);
1355 if ((usrquota == QOPT_ENABLE) || (grpquota == QOPT_ENABLE)) {
1356 fs->super->s_feature_ro_compat |= EXT4_FEATURE_RO_COMPAT_QUOTA;
1357 ext2fs_mark_super_dirty(fs);
1358 } else if (!fs->super->s_usr_quota_inum &&
1359 !fs->super->s_grp_quota_inum) {
1360 fs->super->s_feature_ro_compat &= ~EXT4_FEATURE_RO_COMPAT_QUOTA;
1361 ext2fs_mark_super_dirty(fs);
1368 static void parse_quota_opts(const char *opts)
1370 char *buf, *token, *next, *p;
1374 buf = malloc(len+1);
1376 fputs(_("Couldn't allocate memory to parse quota "
1377 "options!\n"), stderr);
1381 for (token = buf; token && *token; token = next) {
1382 p = strchr(token, ',');
1389 if (strcmp(token, "usrquota") == 0) {
1390 usrquota = QOPT_ENABLE;
1391 } else if (strcmp(token, "^usrquota") == 0) {
1392 usrquota = QOPT_DISABLE;
1393 } else if (strcmp(token, "grpquota") == 0) {
1394 grpquota = QOPT_ENABLE;
1395 } else if (strcmp(token, "^grpquota") == 0) {
1396 grpquota = QOPT_DISABLE;
1398 fputs(_("\nBad quota options specified.\n\n"
1399 "Following valid quota options are available "
1400 "(pass by separating with comma):\n"
1412 static void parse_e2label_options(int argc, char ** argv)
1414 if ((argc < 2) || (argc > 3)) {
1415 fputs(_("Usage: e2label device [newlabel]\n"), stderr);
1418 io_options = strchr(argv[1], '?');
1421 device_name = blkid_get_devname(NULL, argv[1], NULL);
1423 com_err("e2label", 0, _("Unable to resolve '%s'"),
1427 open_flag = EXT2_FLAG_JOURNAL_DEV_OK;
1429 open_flag |= EXT2_FLAG_RW;
1431 new_label = argv[2];
1436 static time_t parse_time(char *str)
1440 if (strcmp(str, "now") == 0) {
1443 memset(&ts, 0, sizeof(ts));
1444 #ifdef HAVE_STRPTIME
1445 strptime(str, "%Y%m%d%H%M%S", &ts);
1447 sscanf(str, "%4d%2d%2d%2d%2d%2d", &ts.tm_year, &ts.tm_mon,
1448 &ts.tm_mday, &ts.tm_hour, &ts.tm_min, &ts.tm_sec);
1451 if (ts.tm_year < 0 || ts.tm_mon < 0 || ts.tm_mon > 11 ||
1452 ts.tm_mday < 0 || ts.tm_mday > 31 || ts.tm_hour > 23 ||
1453 ts.tm_min > 59 || ts.tm_sec > 61)
1456 if (ts.tm_mday == 0) {
1457 com_err(program_name, 0,
1458 _("Couldn't parse date/time specifier: %s"),
1463 return (mktime(&ts));
1466 static void parse_tune2fs_options(int argc, char **argv)
1472 char optstring[100] = "c:e:fg:i:jlm:o:r:s:u:C:E:I:J:L:M:O:T:U:";
1475 strcat(optstring, "Q:");
1479 printf("tune2fs %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
1480 while ((c = getopt(argc, argv, optstring)) != EOF)
1483 max_mount_count = strtol(optarg, &tmp, 0);
1484 if (*tmp || max_mount_count > 16000) {
1485 com_err(program_name, 0,
1486 _("bad mounts count - %s"),
1490 if (max_mount_count == 0)
1491 max_mount_count = -1;
1493 open_flag = EXT2_FLAG_RW;
1496 mount_count = strtoul(optarg, &tmp, 0);
1497 if (*tmp || mount_count > 16000) {
1498 com_err(program_name, 0,
1499 _("bad mounts count - %s"),
1504 open_flag = EXT2_FLAG_RW;
1507 if (strcmp(optarg, "continue") == 0)
1508 errors = EXT2_ERRORS_CONTINUE;
1509 else if (strcmp(optarg, "remount-ro") == 0)
1510 errors = EXT2_ERRORS_RO;
1511 else if (strcmp(optarg, "panic") == 0)
1512 errors = EXT2_ERRORS_PANIC;
1514 com_err(program_name, 0,
1515 _("bad error behavior - %s"),
1520 open_flag = EXT2_FLAG_RW;
1523 extended_cmd = optarg;
1524 open_flag |= EXT2_FLAG_RW;
1526 case 'f': /* Force */
1530 resgid = strtoul(optarg, &tmp, 0);
1532 gr = getgrnam(optarg);
1536 resgid = gr->gr_gid;
1541 com_err(program_name, 0,
1542 _("bad gid/group name - %s"),
1547 open_flag = EXT2_FLAG_RW;
1550 interval = strtoul(optarg, &tmp, 0);
1557 case 'D': /* days */
1563 case 'M': /* months! */
1564 interval *= 86400 * 30;
1568 case 'W': /* weeks */
1569 interval *= 86400 * 7;
1574 com_err(program_name, 0,
1575 _("bad interval - %s"), optarg);
1579 open_flag = EXT2_FLAG_RW;
1584 open_flag = EXT2_FLAG_RW;
1587 parse_journal_opts(optarg);
1588 open_flag = EXT2_FLAG_RW;
1596 open_flag |= EXT2_FLAG_RW |
1597 EXT2_FLAG_JOURNAL_DEV_OK;
1600 reserved_ratio = strtod(optarg, &tmp);
1601 if (*tmp || reserved_ratio > 50 ||
1602 reserved_ratio < 0) {
1603 com_err(program_name, 0,
1604 _("bad reserved block ratio - %s"),
1609 open_flag = EXT2_FLAG_RW;
1612 new_last_mounted = optarg;
1614 open_flag = EXT2_FLAG_RW;
1618 com_err(program_name, 0, "%s",
1619 _("-o may only be specified once"));
1622 mntopts_cmd = optarg;
1623 open_flag = EXT2_FLAG_RW;
1627 com_err(program_name, 0, "%s",
1628 _("-O may only be specified once"));
1631 features_cmd = optarg;
1632 open_flag = EXT2_FLAG_RW;
1637 parse_quota_opts(optarg);
1638 open_flag = EXT2_FLAG_RW;
1642 reserved_blocks = strtoul(optarg, &tmp, 0);
1644 com_err(program_name, 0,
1645 _("bad reserved blocks count - %s"),
1650 open_flag = EXT2_FLAG_RW;
1652 case 's': /* Deprecated */
1653 s_flag = atoi(optarg);
1654 open_flag = EXT2_FLAG_RW;
1658 last_check_time = parse_time(optarg);
1659 open_flag = EXT2_FLAG_RW;
1662 resuid = strtoul(optarg, &tmp, 0);
1664 pw = getpwnam(optarg);
1668 resuid = pw->pw_uid;
1673 com_err(program_name, 0,
1674 _("bad uid/user name - %s"),
1679 open_flag = EXT2_FLAG_RW;
1684 open_flag = EXT2_FLAG_RW |
1685 EXT2_FLAG_JOURNAL_DEV_OK;
1688 new_inode_size = strtoul(optarg, &tmp, 0);
1690 com_err(program_name, 0,
1691 _("bad inode size - %s"),
1695 if (!((new_inode_size &
1696 (new_inode_size - 1)) == 0)) {
1697 com_err(program_name, 0,
1698 _("Inode size must be a "
1699 "power of two- %s"),
1703 open_flag = EXT2_FLAG_RW;
1709 if (optind < argc - 1 || optind == argc)
1711 if (!open_flag && !l_flag)
1713 io_options = strchr(argv[optind], '?');
1716 device_name = blkid_get_devname(NULL, argv[optind], NULL);
1718 com_err(program_name, 0, _("Unable to resolve '%s'"),
1724 #ifdef CONFIG_BUILD_FINDFS
1725 void do_findfs(int argc, char **argv)
1730 (strncmp(argv[1], "LABEL=", 6) && strncmp(argv[1], "UUID=", 5))) {
1731 fprintf(stderr, "Usage: findfs LABEL=<label>|UUID=<uuid>\n");
1734 dev = blkid_get_devname(NULL, argv[1], NULL);
1736 com_err("findfs", 0, _("Unable to resolve '%s'"),
1745 static int parse_extended_opts(ext2_filsys fs, const char *opts)
1747 char *buf, *token, *next, *p, *arg;
1752 buf = malloc(len+1);
1754 fprintf(stderr, "%s",
1755 _("Couldn't allocate memory to parse options!\n"));
1759 for (token = buf; token && *token; token = next) {
1760 p = strchr(token, ',');
1766 arg = strchr(token, '=');
1771 if (strcmp(token, "clear-mmp") == 0 ||
1772 strcmp(token, "clear_mmp") == 0) {
1774 } else if (strcmp(token, "mmp_update_interval") == 0) {
1780 intv = strtoul(arg, &p, 0);
1783 _("Invalid mmp_update_interval: %s\n"),
1789 intv = EXT4_MMP_UPDATE_INTERVAL;
1790 } else if (intv > EXT4_MMP_MAX_UPDATE_INTERVAL) {
1792 _("mmp_update_interval too big: %lu\n"),
1797 printf(P_("Setting multiple mount protection update "
1798 "interval to %lu second\n",
1799 "Setting multiple mount protection update "
1800 "interval to %lu seconds\n", intv),
1802 fs->super->s_mmp_update_interval = intv;
1803 ext2fs_mark_super_dirty(fs);
1804 } else if (!strcmp(token, "test_fs")) {
1805 fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
1806 printf("Setting test filesystem flag\n");
1807 ext2fs_mark_super_dirty(fs);
1808 } else if (!strcmp(token, "^test_fs")) {
1809 fs->super->s_flags &= ~EXT2_FLAGS_TEST_FILESYS;
1810 printf("Clearing test filesystem flag\n");
1811 ext2fs_mark_super_dirty(fs);
1812 } else if (strcmp(token, "stride") == 0) {
1817 stride = strtoul(arg, &p, 0);
1820 _("Invalid RAID stride: %s\n"),
1826 } else if (strcmp(token, "stripe-width") == 0 ||
1827 strcmp(token, "stripe_width") == 0) {
1832 stripe_width = strtoul(arg, &p, 0);
1835 _("Invalid RAID stripe-width: %s\n"),
1840 stripe_width_set = 1;
1841 } else if (strcmp(token, "hash_alg") == 0 ||
1842 strcmp(token, "hash-alg") == 0) {
1847 hash_alg = e2p_string2hash(arg);
1850 _("Invalid hash algorithm: %s\n"),
1855 fs->super->s_def_hash_version = hash_alg;
1856 printf(_("Setting default hash algorithm "
1859 ext2fs_mark_super_dirty(fs);
1860 } else if (!strcmp(token, "mount_opts")) {
1865 if (strlen(arg) >= sizeof(fs->super->s_mount_opts)) {
1867 "Extended mount options too long\n");
1870 ext_mount_opts = strdup(arg);
1875 fprintf(stderr, "%s", _("\nBad options specified.\n\n"
1876 "Extended options are separated by commas, "
1877 "and may take an argument which\n"
1878 "\tis set off by an equals ('=') sign.\n\n"
1879 "Valid extended options are:\n"
1881 "\thash_alg=<hash algorithm>\n"
1882 "\tmount_opts=<extended default mount options>\n"
1883 "\tstride=<RAID per-disk chunk size in blocks>\n"
1884 "\tstripe_width=<RAID stride*data disks in blocks>\n"
1896 * Fill in the block bitmap bmap with the information regarding the
1897 * blocks to be moved
1899 static int get_move_bitmaps(ext2_filsys fs, int new_ino_blks_per_grp,
1900 ext2fs_block_bitmap bmap)
1904 ext2_badblocks_list bb_list = 0;
1905 blk64_t j, needed_blocks = 0;
1906 blk64_t start_blk, end_blk;
1908 retval = ext2fs_read_bb_inode(fs, &bb_list);
1912 for (i = 0; i < fs->group_desc_count; i++) {
1913 start_blk = ext2fs_inode_table_loc(fs, i) +
1914 fs->inode_blocks_per_group;
1916 end_blk = ext2fs_inode_table_loc(fs, i) +
1917 new_ino_blks_per_grp;
1919 for (j = start_blk; j < end_blk; j++) {
1920 if (ext2fs_test_block_bitmap2(fs->block_map, j)) {
1922 * IF the block is a bad block we fail
1924 if (ext2fs_badblocks_list_test(bb_list, j)) {
1925 ext2fs_badblocks_list_free(bb_list);
1929 ext2fs_mark_block_bitmap2(bmap, j);
1932 * We are going to use this block for
1933 * inode table. So mark them used.
1935 ext2fs_mark_block_bitmap2(fs->block_map, j);
1938 needed_blocks += end_blk - start_blk;
1941 ext2fs_badblocks_list_free(bb_list);
1942 if (needed_blocks > ext2fs_free_blocks_count(fs->super))
1948 static int ext2fs_is_meta_block(ext2_filsys fs, blk64_t blk)
1951 group = ext2fs_group_of_blk2(fs, blk);
1952 if (ext2fs_block_bitmap_loc(fs, group) == blk)
1954 if (ext2fs_inode_bitmap_loc(fs, group) == blk)
1959 static int ext2fs_is_block_in_group(ext2_filsys fs, dgrp_t group, blk64_t blk)
1961 blk64_t start_blk, end_blk;
1962 start_blk = fs->super->s_first_data_block +
1963 EXT2_GROUPS_TO_BLOCKS(fs->super, group);
1965 * We cannot get new block beyond end_blk for for the last block group
1966 * so we can check with EXT2_BLOCKS_PER_GROUP even for last block group
1968 end_blk = start_blk + EXT2_BLOCKS_PER_GROUP(fs->super);
1969 if (blk >= start_blk && blk <= end_blk)
1974 static int move_block(ext2_filsys fs, ext2fs_block_bitmap bmap)
1981 blk64_t blk, new_blk, goal;
1982 struct blk_move *bmv;
1984 retval = ext2fs_get_mem(fs->blocksize, &buf);
1988 for (new_blk = blk = fs->super->s_first_data_block;
1989 blk < ext2fs_blocks_count(fs->super); blk++) {
1990 if (!ext2fs_test_block_bitmap2(bmap, blk))
1993 if (ext2fs_is_meta_block(fs, blk)) {
1995 * If the block is mapping a fs meta data block
1996 * like group desc/block bitmap/inode bitmap. We
1997 * should find a block in the same group and fix
1998 * the respective fs metadata pointers. Otherwise
2001 group = ext2fs_group_of_blk2(fs, blk);
2002 goal = ext2fs_group_first_block2(fs, group);
2008 retval = ext2fs_new_block2(fs, goal, NULL, &new_blk);
2012 /* new fs meta data block should be in the same group */
2013 if (meta_data && !ext2fs_is_block_in_group(fs, group, new_blk)) {
2018 /* Mark this block as allocated */
2019 ext2fs_mark_block_bitmap2(fs->block_map, new_blk);
2021 /* Add it to block move list */
2022 retval = ext2fs_get_mem(sizeof(struct blk_move), &bmv);
2027 bmv->new_loc = new_blk;
2029 list_add(&(bmv->list), &blk_move_list);
2031 retval = io_channel_read_blk64(fs->io, blk, 1, buf);
2035 retval = io_channel_write_blk64(fs->io, new_blk, 1, buf);
2041 ext2fs_free_mem(&buf);
2045 static blk64_t translate_block(blk64_t blk)
2047 struct list_head *entry;
2048 struct blk_move *bmv;
2050 list_for_each(entry, &blk_move_list) {
2051 bmv = list_entry(entry, struct blk_move, list);
2052 if (bmv->old_loc == blk)
2053 return bmv->new_loc;
2059 static int process_block(ext2_filsys fs EXT2FS_ATTR((unused)),
2061 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
2062 blk64_t ref_block EXT2FS_ATTR((unused)),
2063 int ref_offset EXT2FS_ATTR((unused)),
2068 ext2fs_block_bitmap bmap = (ext2fs_block_bitmap) priv_data;
2070 if (!ext2fs_test_block_bitmap2(bmap, *block_nr))
2072 new_blk = translate_block(*block_nr);
2074 *block_nr = new_blk;
2076 * This will force the ext2fs_write_inode in the iterator
2078 ret |= BLOCK_CHANGED;
2084 static int inode_scan_and_fix(ext2_filsys fs, ext2fs_block_bitmap bmap)
2086 errcode_t retval = 0;
2089 char *block_buf = 0;
2090 struct ext2_inode inode;
2091 ext2_inode_scan scan = NULL;
2093 retval = ext2fs_get_mem(fs->blocksize * 3, &block_buf);
2097 retval = ext2fs_open_inode_scan(fs, 0, &scan);
2102 retval = ext2fs_get_next_inode(scan, &ino, &inode);
2109 if (inode.i_links_count == 0)
2110 continue; /* inode not in use */
2113 * If we end up modifying the journal inode
2114 * the sb->s_jnl_blocks will differ. But a
2115 * subsequent e2fsck fixes that.
2116 * Do we need to fix this ??
2119 if (ext2fs_file_acl_block(fs, &inode) &&
2120 ext2fs_test_block_bitmap2(bmap,
2121 ext2fs_file_acl_block(fs, &inode))) {
2122 blk = translate_block(ext2fs_file_acl_block(fs,
2127 ext2fs_file_acl_block_set(fs, &inode, blk);
2130 * Write the inode to disk so that inode table
2133 retval = ext2fs_write_inode(fs, ino, &inode);
2138 if (!ext2fs_inode_has_valid_blocks2(fs, &inode))
2141 retval = ext2fs_block_iterate3(fs, ino, 0, block_buf,
2142 process_block, bmap);
2149 ext2fs_free_mem(&block_buf);
2150 ext2fs_close_inode_scan(scan);
2156 * We need to scan for inode and block bitmaps that may need to be
2157 * moved. This can take place if the filesystem was formatted for
2158 * RAID arrays using the mke2fs's extended option "stride".
2160 static int group_desc_scan_and_fix(ext2_filsys fs, ext2fs_block_bitmap bmap)
2163 blk64_t blk, new_blk;
2165 for (i = 0; i < fs->group_desc_count; i++) {
2166 blk = ext2fs_block_bitmap_loc(fs, i);
2167 if (ext2fs_test_block_bitmap2(bmap, blk)) {
2168 new_blk = translate_block(blk);
2171 ext2fs_block_bitmap_loc_set(fs, i, new_blk);
2174 blk = ext2fs_inode_bitmap_loc(fs, i);
2175 if (ext2fs_test_block_bitmap2(bmap, blk)) {
2176 new_blk = translate_block(blk);
2179 ext2fs_inode_bitmap_loc_set(fs, i, new_blk);
2185 static int expand_inode_table(ext2_filsys fs, unsigned long new_ino_size)
2190 int new_ino_blks_per_grp;
2192 char *old_itable = NULL, *new_itable = NULL;
2193 char *tmp_old_itable = NULL, *tmp_new_itable = NULL;
2194 unsigned long old_ino_size;
2195 int old_itable_size, new_itable_size;
2197 old_itable_size = fs->inode_blocks_per_group * fs->blocksize;
2198 old_ino_size = EXT2_INODE_SIZE(fs->super);
2200 new_ino_blks_per_grp = ext2fs_div_ceil(
2201 EXT2_INODES_PER_GROUP(fs->super) *
2205 new_itable_size = new_ino_blks_per_grp * fs->blocksize;
2207 retval = ext2fs_get_mem(old_itable_size, &old_itable);
2211 retval = ext2fs_get_mem(new_itable_size, &new_itable);
2215 tmp_old_itable = old_itable;
2216 tmp_new_itable = new_itable;
2218 for (i = 0; i < fs->group_desc_count; i++) {
2219 blk = ext2fs_inode_table_loc(fs, i);
2220 retval = io_channel_read_blk64(fs->io, blk,
2221 fs->inode_blocks_per_group, old_itable);
2225 for (j = 0; j < EXT2_INODES_PER_GROUP(fs->super); j++) {
2226 memcpy(new_itable, old_itable, old_ino_size);
2228 memset(new_itable+old_ino_size, 0,
2229 new_ino_size - old_ino_size);
2231 new_itable += new_ino_size;
2232 old_itable += old_ino_size;
2235 /* reset the pointer */
2236 old_itable = tmp_old_itable;
2237 new_itable = tmp_new_itable;
2239 retval = io_channel_write_blk64(fs->io, blk,
2240 new_ino_blks_per_grp, new_itable);
2245 /* Update the meta data */
2246 fs->inode_blocks_per_group = new_ino_blks_per_grp;
2247 fs->super->s_inode_size = new_ino_size;
2251 ext2fs_free_mem(&old_itable);
2254 ext2fs_free_mem(&new_itable);
2259 static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs)
2263 unsigned int group = 0;
2264 unsigned int count = 0;
2269 * First calculate the block statistics
2271 for (blk = fs->super->s_first_data_block;
2272 blk < ext2fs_blocks_count(fs->super); blk++) {
2273 if (!ext2fs_fast_test_block_bitmap2(fs->block_map, blk)) {
2278 if ((count == fs->super->s_blocks_per_group) ||
2279 (blk == ext2fs_blocks_count(fs->super)-1)) {
2280 ext2fs_bg_free_blocks_count_set(fs, group++,
2286 total_free = EXT2FS_C2B(fs, total_free);
2287 ext2fs_free_blocks_count_set(fs->super, total_free);
2290 * Next, calculate the inode statistics
2297 /* Protect loop from wrap-around if s_inodes_count maxed */
2298 for (ino = 1; ino <= fs->super->s_inodes_count && ino > 0; ino++) {
2299 if (!ext2fs_fast_test_inode_bitmap2(fs->inode_map, ino)) {
2304 if ((count == fs->super->s_inodes_per_group) ||
2305 (ino == fs->super->s_inodes_count)) {
2306 ext2fs_bg_free_inodes_count_set(fs, group++,
2312 fs->super->s_free_inodes_count = total_free;
2313 ext2fs_mark_super_dirty(fs);
2317 #define list_for_each_safe(pos, pnext, head) \
2318 for (pos = (head)->next, pnext = pos->next; pos != (head); \
2319 pos = pnext, pnext = pos->next)
2321 static void free_blk_move_list(void)
2323 struct list_head *entry, *tmp;
2324 struct blk_move *bmv;
2326 list_for_each_safe(entry, tmp, &blk_move_list) {
2327 bmv = list_entry(entry, struct blk_move, list);
2329 ext2fs_free_mem(&bmv);
2334 static int resize_inode(ext2_filsys fs, unsigned long new_size)
2337 int new_ino_blks_per_grp;
2338 ext2fs_block_bitmap bmap;
2340 retval = ext2fs_read_inode_bitmap(fs);
2342 fputs(_("Failed to read inode bitmap\n"), stderr);
2345 retval = ext2fs_read_block_bitmap(fs);
2347 fputs(_("Failed to read block bitmap\n"), stderr);
2350 INIT_LIST_HEAD(&blk_move_list);
2353 new_ino_blks_per_grp = ext2fs_div_ceil(
2354 EXT2_INODES_PER_GROUP(fs->super)*
2358 /* We may change the file system.
2359 * Mark the file system as invalid so that
2360 * the user is prompted to run fsck.
2362 fs->super->s_state &= ~EXT2_VALID_FS;
2364 retval = ext2fs_allocate_block_bitmap(fs, _("blocks to be moved"),
2367 fputs(_("Failed to allocate block bitmap when "
2368 "increasing inode size\n"), stderr);
2371 retval = get_move_bitmaps(fs, new_ino_blks_per_grp, bmap);
2373 fputs(_("Not enough space to increase inode size \n"), stderr);
2376 retval = move_block(fs, bmap);
2378 fputs(_("Failed to relocate blocks during inode resize \n"),
2382 retval = inode_scan_and_fix(fs, bmap);
2386 retval = group_desc_scan_and_fix(fs, bmap);
2390 retval = expand_inode_table(fs, new_size);
2394 ext2fs_calculate_summary_stats(fs);
2396 fs->super->s_state |= EXT2_VALID_FS;
2397 /* mark super block and block bitmap as dirty */
2398 ext2fs_mark_super_dirty(fs);
2399 ext2fs_mark_bb_dirty(fs);
2402 free_blk_move_list();
2403 ext2fs_free_block_bitmap(bmap);
2408 free_blk_move_list();
2409 ext2fs_free_block_bitmap(bmap);
2410 fputs(_("Error in resizing the inode size.\n"
2411 "Run e2undo to undo the "
2412 "file system changes. \n"), stderr);
2417 static int tune2fs_setup_tdb(const char *name, io_manager *io_ptr)
2419 errcode_t retval = 0;
2420 const char *tdb_dir;
2422 char *dev_name, *tmp_name;
2426 * Configuration via a conf file would be
2429 profile_get_string(profile, "scratch_files",
2433 tmp_name = strdup(name);
2436 com_err(program_name, ENOMEM, "%s",
2437 _("Couldn't allocate memory for tdb filename\n"));
2440 dev_name = basename(tmp_name);
2442 tdb_dir = getenv("E2FSPROGS_UNDO_DIR");
2444 tdb_dir = "/var/lib/e2fsprogs";
2446 if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
2447 access(tdb_dir, W_OK))
2450 tdb_file = malloc(strlen(tdb_dir) + 9 + strlen(dev_name) + 7 + 1);
2453 sprintf(tdb_file, "%s/tune2fs-%s.e2undo", tdb_dir, dev_name);
2455 if ((unlink(tdb_file) < 0) && (errno != ENOENT)) {
2457 com_err(program_name, retval,
2458 _("while trying to delete %s"), tdb_file);
2463 set_undo_io_backing_manager(*io_ptr);
2464 *io_ptr = undo_io_manager;
2465 set_undo_io_backup_file(tdb_file);
2466 printf(_("To undo the tune2fs operation please run "
2467 "the command\n e2undo %s %s\n\n"),
2475 fs_update_journal_user(struct ext2_super_block *sb, __u8 old_uuid[UUID_SIZE])
2477 int retval, nr_users, start;
2478 journal_superblock_t *jsb;
2482 char uuid[UUID_STR_SIZE];
2483 char buf[SUPERBLOCK_SIZE];
2485 if (!(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) ||
2486 uuid_is_null(sb->s_journal_uuid))
2489 uuid_unparse(sb->s_journal_uuid, uuid);
2490 journal_path = blkid_get_devname(NULL, "UUID", uuid);
2494 retval = ext2fs_open2(journal_path, io_options,
2495 EXT2_FLAG_JOURNAL_DEV_OK | EXT2_FLAG_RW,
2496 0, 0, unix_io_manager, &jfs);
2498 com_err(program_name, retval,
2499 _("while trying to open %s"),
2504 retval = get_journal_sb(jfs, buf);
2506 if (retval == EXT2_ET_UNSUPP_FEATURE)
2507 fprintf(stderr, _("%s is not a journal device.\n"),
2512 jsb = (journal_superblock_t *) buf;
2513 /* Find the filesystem UUID */
2514 nr_users = ntohl(jsb->s_nr_users);
2516 j_uuid = journal_user(old_uuid, jsb->s_users, nr_users);
2517 if (j_uuid == NULL) {
2518 fputs(_("Filesystem's UUID not found on journal device.\n"),
2520 return EXT2_ET_LOAD_EXT_JOURNAL;
2523 memcpy(j_uuid, sb->s_uuid, UUID_SIZE);
2525 start = ext2fs_journal_sb_start(jfs->blocksize);
2526 /* Write back the journal superblock */
2527 retval = io_channel_write_blk64(jfs->io, start, -SUPERBLOCK_SIZE, buf);
2529 com_err(program_name, retval,
2530 "while writing journal superblock.");
2539 int main(int argc, char **argv)
2543 struct ext2_super_block *sb;
2544 io_manager io_ptr, io_ptr_orig = NULL;
2548 setlocale(LC_MESSAGES, "");
2549 setlocale(LC_CTYPE, "");
2550 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
2551 textdomain(NLS_CAT_NAME);
2552 set_com_err_gettext(gettext);
2555 program_name = *argv;
2556 add_error_table(&et_ext2_error_table);
2558 #ifdef CONFIG_BUILD_FINDFS
2559 if (strcmp(get_progname(argv[0]), "findfs") == 0)
2560 do_findfs(argc, argv);
2562 if (strcmp(get_progname(argv[0]), "e2label") == 0)
2563 parse_e2label_options(argc, argv);
2565 parse_tune2fs_options(argc, argv);
2567 #ifdef CONFIG_TESTIO_DEBUG
2568 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_DEBUG")) {
2569 io_ptr = test_io_manager;
2570 test_io_backing_manager = unix_io_manager;
2573 io_ptr = unix_io_manager;
2576 if ((open_flag & EXT2_FLAG_RW) == 0 || f_flag)
2577 open_flag |= EXT2_FLAG_SKIP_MMP;
2579 open_flag |= EXT2_FLAG_64BITS | EXT2_FLAG_JOURNAL_DEV_OK;
2581 /* keep the filesystem struct around to dump MMP data */
2582 open_flag |= EXT2_FLAG_NOFREE_ON_ERROR;
2584 retval = ext2fs_open2(device_name, io_options, open_flag,
2587 com_err(program_name, retval,
2588 _("while trying to open %s"),
2590 if (retval == EXT2_ET_MMP_FSCK_ON ||
2591 retval == EXT2_ET_MMP_UNKNOWN_SEQ)
2592 dump_mmp_msg(fs->mmp_buf,
2593 _("If you are sure the filesystem "
2594 "is not in use on any node, run:\n"
2595 "'tune2fs -f -E clear_mmp {device}'\n"));
2596 else if (retval == EXT2_ET_MMP_FAILED)
2597 dump_mmp_msg(fs->mmp_buf, NULL);
2598 else if (retval == EXT2_ET_MMP_MAGIC_INVALID)
2600 _("MMP block magic is bad. Try to fix it by "
2601 "running:\n'e2fsck -f %s'\n"), device_name);
2602 else if (retval == EXT2_ET_BAD_MAGIC)
2603 check_plausibility(device_name, CHECK_FS_EXIST, NULL);
2604 else if (retval != EXT2_ET_MMP_FAILED)
2605 fprintf(stderr, "%s",
2606 _("Couldn't find valid filesystem superblock.\n"));
2611 if (EXT2_HAS_INCOMPAT_FEATURE(fs->super,
2612 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
2613 fprintf(stderr, "%s", _("Cannot modify a journal device.\n"));
2617 fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE;
2619 if (I_flag && !io_ptr_orig) {
2621 * Check the inode size is right so we can issue an
2622 * error message and bail before setting up the tdb
2625 if (new_inode_size == EXT2_INODE_SIZE(fs->super)) {
2626 fprintf(stderr, _("The inode size is already %lu\n"),
2631 if (new_inode_size < EXT2_INODE_SIZE(fs->super)) {
2632 fprintf(stderr, "%s",
2633 _("Shrinking inode size is not supported\n"));
2637 if (new_inode_size > fs->blocksize) {
2638 fprintf(stderr, _("Invalid inode size %lu (max %d)\n"),
2639 new_inode_size, fs->blocksize);
2645 * If inode resize is requested use the
2648 io_ptr_orig = io_ptr;
2649 retval = tune2fs_setup_tdb(device_name, &io_ptr);
2654 if (io_ptr != io_ptr_orig) {
2655 ext2fs_close_free(&fs);
2661 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
2664 /* For e2label emulation */
2665 printf("%.*s\n", (int) sizeof(sb->s_volume_name),
2667 remove_error_table(&et_ext2_error_table);
2671 retval = ext2fs_check_if_mounted(device_name, &mount_flags);
2673 com_err("ext2fs_check_if_mount", retval,
2674 _("while determining whether %s is mounted."),
2679 /* Normally we only need to write out the superblock */
2680 fs->flags |= EXT2_FLAG_SUPER_ONLY;
2683 sb->s_max_mnt_count = max_mount_count;
2684 ext2fs_mark_super_dirty(fs);
2685 printf(_("Setting maximal mount count to %d\n"),
2689 sb->s_mnt_count = mount_count;
2690 ext2fs_mark_super_dirty(fs);
2691 printf(_("Setting current mount count to %d\n"), mount_count);
2694 sb->s_errors = errors;
2695 ext2fs_mark_super_dirty(fs);
2696 printf(_("Setting error behavior to %d\n"), errors);
2699 sb->s_def_resgid = resgid;
2700 ext2fs_mark_super_dirty(fs);
2701 printf(_("Setting reserved blocks gid to %lu\n"), resgid);
2704 if ((unsigned long long)interval >= (1ULL << 32)) {
2705 com_err(program_name, 0,
2706 _("interval between checks is too big (%lu)"),
2711 sb->s_checkinterval = interval;
2712 ext2fs_mark_super_dirty(fs);
2713 printf(_("Setting interval between checks to %lu seconds\n"),
2717 ext2fs_r_blocks_count_set(sb, reserved_ratio *
2718 ext2fs_blocks_count(sb) / 100.0);
2719 ext2fs_mark_super_dirty(fs);
2720 printf (_("Setting reserved blocks percentage to %g%% (%llu blocks)\n"),
2721 reserved_ratio, ext2fs_r_blocks_count(sb));
2724 if (reserved_blocks > ext2fs_blocks_count(sb)/2) {
2725 com_err(program_name, 0,
2726 _("reserved blocks count is too big (%llu)"),
2731 ext2fs_r_blocks_count_set(sb, reserved_blocks);
2732 ext2fs_mark_super_dirty(fs);
2733 printf(_("Setting reserved blocks count to %llu\n"),
2737 if (sb->s_feature_ro_compat &
2738 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER) {
2739 fputs(_("\nThe filesystem already has sparse "
2740 "superblocks.\n"), stderr);
2741 } else if (sb->s_feature_incompat &
2742 EXT2_FEATURE_INCOMPAT_META_BG) {
2743 fputs(_("\nSetting the sparse superblock flag not "
2744 "supported\nfor filesystems with "
2745 "the meta_bg feature enabled.\n"),
2750 sb->s_feature_ro_compat |=
2751 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
2752 sb->s_state &= ~EXT2_VALID_FS;
2753 ext2fs_mark_super_dirty(fs);
2754 printf(_("\nSparse superblock flag set. %s"),
2759 fputs(_("\nClearing the sparse superblock flag not supported.\n"),
2765 sb->s_lastcheck = last_check_time;
2766 ext2fs_mark_super_dirty(fs);
2767 printf(_("Setting time filesystem last checked to %s\n"),
2768 ctime(&last_check_time));
2771 sb->s_def_resuid = resuid;
2772 ext2fs_mark_super_dirty(fs);
2773 printf(_("Setting reserved blocks uid to %lu\n"), resuid);
2776 if (strlen(new_label) > sizeof(sb->s_volume_name))
2777 fputs(_("Warning: label too long, truncating.\n"),
2779 memset(sb->s_volume_name, 0, sizeof(sb->s_volume_name));
2780 strncpy(sb->s_volume_name, new_label,
2781 sizeof(sb->s_volume_name));
2782 ext2fs_mark_super_dirty(fs);
2785 memset(sb->s_last_mounted, 0, sizeof(sb->s_last_mounted));
2786 strncpy(sb->s_last_mounted, new_last_mounted,
2787 sizeof(sb->s_last_mounted));
2788 ext2fs_mark_super_dirty(fs);
2791 rc = update_mntopts(fs, mntopts_cmd);
2796 rc = update_feature_set(fs, features_cmd);
2801 rc = parse_extended_opts(fs, extended_cmd);
2804 if (clear_mmp && !f_flag) {
2805 fputs(_("Error in using clear_mmp. "
2806 "It must be used with -f\n"),
2812 rc = ext2fs_mmp_clear(fs);
2815 if (journal_size || journal_device) {
2816 rc = add_journal(fs);
2822 if (mount_flags & EXT2_MF_MOUNTED) {
2823 fputs(_("The quota feature may only be changed when "
2824 "the filesystem is unmounted.\n"), stderr);
2828 handle_quota_options(fs);
2834 char buf[SUPERBLOCK_SIZE];
2835 __u8 old_uuid[UUID_SIZE];
2837 if (ext2fs_has_group_desc_csum(fs)) {
2839 * Changing the UUID requires rewriting all metadata,
2840 * which can race with a mounted fs. Don't allow that.
2842 if (mount_flags & EXT2_MF_MOUNTED) {
2843 fputs(_("The UUID may only be "
2844 "changed when the filesystem is "
2845 "unmounted.\n"), stderr);
2848 if (check_fsck_needed(fs))
2852 * Determine if the block group checksums are
2853 * correct so we know whether or not to set
2856 for (i = 0; i < fs->group_desc_count; i++)
2857 if (!ext2fs_group_desc_csum_verify(fs, i))
2859 if (i >= fs->group_desc_count)
2863 memcpy(old_uuid, sb->s_uuid, UUID_SIZE);
2864 if ((strcasecmp(new_UUID, "null") == 0) ||
2865 (strcasecmp(new_UUID, "clear") == 0)) {
2866 uuid_clear(sb->s_uuid);
2867 } else if (strcasecmp(new_UUID, "time") == 0) {
2868 uuid_generate_time(sb->s_uuid);
2869 } else if (strcasecmp(new_UUID, "random") == 0) {
2870 uuid_generate(sb->s_uuid);
2871 } else if (uuid_parse(new_UUID, sb->s_uuid)) {
2872 com_err(program_name, 0, "%s",
2873 _("Invalid UUID format\n"));
2877 ext2fs_init_csum_seed(fs);
2879 for (i = 0; i < fs->group_desc_count; i++)
2880 ext2fs_group_desc_csum_set(fs, i);
2881 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
2884 /* If this is a journal dev, we need to copy UUID into jsb */
2885 if (!(rc = get_journal_sb(fs, buf))) {
2886 journal_superblock_t *jsb;
2888 jsb = (journal_superblock_t *) buf;
2889 fputs(_("Need to update journal superblock.\n"), stdout);
2890 memcpy(jsb->s_uuid, sb->s_uuid, sizeof(sb->s_uuid));
2892 /* Writeback the journal superblock */
2893 if ((rc = io_channel_write_blk64(fs->io,
2894 ext2fs_journal_sb_start(fs->blocksize),
2895 -SUPERBLOCK_SIZE, buf)))
2897 } else if (rc != EXT2_ET_UNSUPP_FEATURE)
2900 rc = 0; /** Reset rc to avoid ext2fs_mmp_stop() */
2902 if ((rc = fs_update_journal_user(sb, old_uuid)))
2906 ext2fs_mark_super_dirty(fs);
2907 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
2908 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
2909 rewrite_checksums = 1;
2911 if (rewrite_checksums)
2912 rewrite_metadata_checksums(fs);
2914 if (mount_flags & EXT2_MF_MOUNTED) {
2915 fputs(_("The inode size may only be "
2916 "changed when the filesystem is "
2917 "unmounted.\n"), stderr);
2921 if (fs->super->s_feature_incompat &
2922 EXT4_FEATURE_INCOMPAT_FLEX_BG) {
2923 fputs(_("Changing the inode size not supported for "
2924 "filesystems with the flex_bg\n"
2925 "feature enabled.\n"),
2931 * We want to update group descriptor also
2932 * with the new free inode count
2934 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
2935 if (resize_inode(fs, new_inode_size) == 0) {
2936 printf(_("Setting inode size %lu\n"),
2939 printf("%s", _("Failed to change inode size\n"));
2948 sb->s_raid_stride = stride;
2949 ext2fs_mark_super_dirty(fs);
2950 printf(_("Setting stride size to %d\n"), stride);
2952 if (stripe_width_set) {
2953 sb->s_raid_stripe_width = stripe_width;
2954 ext2fs_mark_super_dirty(fs);
2955 printf(_("Setting stripe width to %d\n"), stripe_width);
2957 if (ext_mount_opts) {
2958 strncpy((char *)(fs->super->s_mount_opts), ext_mount_opts,
2959 sizeof(fs->super->s_mount_opts));
2960 fs->super->s_mount_opts[sizeof(fs->super->s_mount_opts)-1] = 0;
2961 ext2fs_mark_super_dirty(fs);
2962 printf(_("Setting extended default mount options to '%s'\n"),
2964 free(ext_mount_opts);
2967 remove_error_table(&et_ext2_error_table);
2971 ext2fs_mmp_stop(fs);
2975 return (ext2fs_close_free(&fs) ? 1 : 0);