2 * tune2fs.c - Change the file system parameters on an ext2 file system
4 * Copyright (C) 1992, 1993, 1994 Remy Card <card@masi.ibp.fr>
5 * Laboratoire MASI, Institut Blaise Pascal
6 * Universite Pierre et Marie Curie (Paris VI)
8 * Copyright 1995, 1996, 1997, 1998, 1999, 2000 by Theodore Ts'o.
11 * This file may be redistributed under the terms of the GNU Public
19 * 93/10/31 - Added the -c option to change the maximal mount counts
20 * 93/12/14 - Added -l flag to list contents of superblock
21 * M.J.E. Mol (marcel@duteca.et.tudelft.nl)
22 * F.W. ten Wolde (franky@duteca.et.tudelft.nl)
23 * 93/12/29 - Added the -e option to change errors behavior
24 * 94/02/27 - Ported to use the ext2fs library
25 * 94/03/06 - Added the checks interval from Uwe Ohse (uwe@tirka.gun.de)
28 #define _XOPEN_SOURCE 600 /* for inclusion of strptime() */
44 #include <strings.h> /* for strcasecmp() */
46 #define _BSD_SOURCE /* for inclusion of strcasecmp() via <string.h> */
51 #include <sys/types.h>
55 #include "ext2fs/ext2_fs.h"
56 #include "ext2fs/ext2fs.h"
57 #include "ext2fs/kernel-jbd.h"
58 #include "et/com_err.h"
59 #include "support/plausible.h"
60 #include "support/quotaio.h"
61 #include "uuid/uuid.h"
64 #include "blkid/blkid.h"
66 #include "../version.h"
67 #include "support/nls-enable.h"
69 #define QOPT_ENABLE (1)
70 #define QOPT_DISABLE (-1)
72 extern int ask_yn(const char *string, int def);
74 const char *program_name = "tune2fs";
76 char *new_label, *new_last_mounted, *new_UUID;
78 static int c_flag, C_flag, e_flag, f_flag, g_flag, i_flag, l_flag, L_flag;
79 static int m_flag, M_flag, Q_flag, r_flag, s_flag = -1, u_flag, U_flag, T_flag;
82 static time_t last_check_time;
83 static int print_label;
84 static int max_mount_count, mount_count, mount_flags;
85 static unsigned long interval;
86 static blk64_t reserved_blocks;
87 static double reserved_ratio;
88 static unsigned long resgid, resuid;
89 static unsigned short errors;
91 static char *features_cmd;
92 static char *mntopts_cmd;
93 static int stride, stripe_width;
94 static int stride_set, stripe_width_set;
95 static char *extended_cmd;
96 static unsigned long new_inode_size;
97 static char *ext_mount_opts;
98 static int usrquota, grpquota;
99 static int rewrite_checksums;
100 static int feature_64bit;
101 static int fsck_requested;
102 static char *undo_file;
104 int journal_size, journal_flags;
105 char *journal_device;
106 static blk64_t journal_location = ~0LL;
108 static struct list_head blk_move_list;
111 struct list_head list;
117 static const char *please_fsck = N_("Please run e2fsck on the filesystem.\n");
118 static const char *please_dir_fsck =
119 N_("Please run e2fsck -D on the filesystem.\n");
121 #ifdef CONFIG_BUILD_FINDFS
122 void do_findfs(int argc, char **argv);
125 static void usage(void)
128 _("Usage: %s [-c max_mounts_count] [-e errors_behavior] "
130 "\t[-i interval[d|m|w]] [-j] [-J journal_options] [-l]\n"
131 "\t[-m reserved_blocks_percent] [-o [^]mount_options[,...]]\n"
132 "\t[-p mmp_update_interval] [-r reserved_blocks_count] "
134 "\t[-C mount_count] [-L volume_label] [-M last_mounted_dir]\n"
135 "\t[-O [^]feature[,...]] [-Q quota_options]\n"
136 "\t[-E extended-option[,...]] [-T last_check_time] "
137 "[-U UUID]\n\t[-I new_inode_size] [-z undo_file] device\n"),
142 static __u32 ok_features[3] = {
144 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
145 EXT2_FEATURE_COMPAT_DIR_INDEX,
147 EXT2_FEATURE_INCOMPAT_FILETYPE |
148 EXT3_FEATURE_INCOMPAT_EXTENTS |
149 EXT4_FEATURE_INCOMPAT_FLEX_BG |
150 EXT4_FEATURE_INCOMPAT_MMP |
151 EXT4_FEATURE_INCOMPAT_64BIT |
152 EXT4_FEATURE_INCOMPAT_ENCRYPT,
154 EXT2_FEATURE_RO_COMPAT_LARGE_FILE |
155 EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
156 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
157 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
158 EXT4_FEATURE_RO_COMPAT_GDT_CSUM |
159 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER |
160 EXT4_FEATURE_RO_COMPAT_QUOTA |
161 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM |
162 EXT4_FEATURE_RO_COMPAT_READONLY
165 static __u32 clear_ok_features[3] = {
167 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
168 EXT2_FEATURE_COMPAT_RESIZE_INODE |
169 EXT2_FEATURE_COMPAT_DIR_INDEX,
171 EXT2_FEATURE_INCOMPAT_FILETYPE |
172 EXT4_FEATURE_INCOMPAT_FLEX_BG |
173 EXT4_FEATURE_INCOMPAT_MMP |
174 EXT4_FEATURE_INCOMPAT_64BIT,
176 EXT2_FEATURE_RO_COMPAT_LARGE_FILE |
177 EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
178 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
179 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
180 EXT4_FEATURE_RO_COMPAT_GDT_CSUM |
181 EXT4_FEATURE_RO_COMPAT_QUOTA |
182 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM |
183 EXT4_FEATURE_RO_COMPAT_READONLY
187 * Try to get journal super block if any
189 static int get_journal_sb(ext2_filsys jfs, char buf[SUPERBLOCK_SIZE])
192 journal_superblock_t *jsb;
194 if (!ext2fs_has_feature_journal_dev(jfs->super)) {
195 return EXT2_ET_UNSUPP_FEATURE;
198 /* Get the journal superblock */
199 if ((retval = io_channel_read_blk64(jfs->io,
200 ext2fs_journal_sb_start(jfs->blocksize), -SUPERBLOCK_SIZE, buf))) {
201 com_err(program_name, retval, "%s",
202 _("while reading journal superblock"));
206 jsb = (journal_superblock_t *) buf;
207 if ((jsb->s_header.h_magic != (unsigned)ntohl(JFS_MAGIC_NUMBER)) ||
208 (jsb->s_header.h_blocktype != (unsigned)ntohl(JFS_SUPERBLOCK_V2))) {
209 fputs(_("Journal superblock not found!\n"), stderr);
210 return EXT2_ET_BAD_MAGIC;
216 static __u8 *journal_user(__u8 uuid[UUID_SIZE], __u8 s_users[JFS_USERS_SIZE],
220 for (i = 0; i < nr_users; i++) {
221 if (memcmp(uuid, &s_users[i * UUID_SIZE], UUID_SIZE) == 0)
222 return &s_users[i * UUID_SIZE];
229 * Remove an external journal from the filesystem
231 static int remove_journal_device(ext2_filsys fs)
235 char buf[SUPERBLOCK_SIZE] __attribute__ ((aligned(8)));
236 journal_superblock_t *jsb;
239 int commit_remove_journal = 0;
243 commit_remove_journal = 1; /* force removal even if error */
245 uuid_unparse(fs->super->s_journal_uuid, buf);
246 journal_path = blkid_get_devname(NULL, "UUID", buf);
250 ext2fs_find_block_device(fs->super->s_journal_dev);
252 goto no_valid_journal;
255 #ifdef CONFIG_TESTIO_DEBUG
256 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
257 io_ptr = test_io_manager;
258 test_io_backing_manager = unix_io_manager;
261 io_ptr = unix_io_manager;
262 retval = ext2fs_open(journal_path, EXT2_FLAG_RW|
263 EXT2_FLAG_JOURNAL_DEV_OK, 0,
264 fs->blocksize, io_ptr, &jfs);
266 com_err(program_name, retval, "%s",
267 _("while trying to open external journal"));
268 goto no_valid_journal;
271 if ((retval = get_journal_sb(jfs, buf))) {
272 if (retval == EXT2_ET_UNSUPP_FEATURE)
273 fprintf(stderr, _("%s is not a journal device.\n"),
275 goto no_valid_journal;
278 jsb = (journal_superblock_t *) buf;
279 /* Find the filesystem UUID */
280 nr_users = ntohl(jsb->s_nr_users);
282 if (!journal_user(fs->super->s_uuid, jsb->s_users, nr_users)) {
283 fputs(_("Filesystem's UUID not found on journal device.\n"),
285 commit_remove_journal = 1;
286 goto no_valid_journal;
289 for (i = 0; i < nr_users; i++)
290 memcpy(&jsb->s_users[i * 16], &jsb->s_users[(i + 1) * 16], 16);
291 jsb->s_nr_users = htonl(nr_users);
293 /* Write back the journal superblock */
294 retval = io_channel_write_blk64(jfs->io,
295 ext2fs_journal_sb_start(fs->blocksize),
296 -SUPERBLOCK_SIZE, buf);
298 com_err(program_name, retval,
299 "while writing journal superblock.");
300 goto no_valid_journal;
303 commit_remove_journal = 1;
306 if (commit_remove_journal == 0) {
307 fputs(_("Cannot locate journal device. It was NOT removed\n"
308 "Use -f option to remove missing journal device.\n"),
312 fs->super->s_journal_dev = 0;
313 memset(fs->super->s_jnl_blocks, 0, sizeof(fs->super->s_jnl_blocks));
314 uuid_clear(fs->super->s_journal_uuid);
315 ext2fs_mark_super_dirty(fs);
316 fputs(_("Journal removed\n"), stdout);
322 /* Helper function for remove_journal_inode */
323 static int release_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
324 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
325 blk64_t ref_block EXT2FS_ATTR((unused)),
326 int ref_offset EXT2FS_ATTR((unused)),
327 void *private EXT2FS_ATTR((unused)))
333 ext2fs_unmark_block_bitmap2(fs->block_map, block);
334 group = ext2fs_group_of_blk2(fs, block);
335 ext2fs_bg_free_blocks_count_set(fs, group, ext2fs_bg_free_blocks_count(fs, group) + 1);
336 ext2fs_group_desc_csum_set(fs, group);
337 ext2fs_free_blocks_count_add(fs->super, EXT2FS_CLUSTER_RATIO(fs));
342 * Remove the journal inode from the filesystem
344 static errcode_t remove_journal_inode(ext2_filsys fs)
346 struct ext2_inode inode;
348 ino_t ino = fs->super->s_journal_inum;
350 retval = ext2fs_read_inode(fs, ino, &inode);
352 com_err(program_name, retval, "%s",
353 _("while reading journal inode"));
356 if (ino == EXT2_JOURNAL_INO) {
357 retval = ext2fs_read_bitmaps(fs);
359 com_err(program_name, retval, "%s",
360 _("while reading bitmaps"));
363 retval = ext2fs_block_iterate3(fs, ino,
364 BLOCK_FLAG_READ_ONLY, NULL,
365 release_blocks_proc, NULL);
367 com_err(program_name, retval, "%s",
368 _("while clearing journal inode"));
371 memset(&inode, 0, sizeof(inode));
372 ext2fs_mark_bb_dirty(fs);
373 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
375 inode.i_flags &= ~EXT2_IMMUTABLE_FL;
376 retval = ext2fs_write_inode(fs, ino, &inode);
378 com_err(program_name, retval, "%s",
379 _("while writing journal inode"));
382 fs->super->s_journal_inum = 0;
383 memset(fs->super->s_jnl_blocks, 0, sizeof(fs->super->s_jnl_blocks));
384 ext2fs_mark_super_dirty(fs);
390 * Update the default mount options
392 static int update_mntopts(ext2_filsys fs, char *mntopts)
394 struct ext2_super_block *sb = fs->super;
396 if (e2p_edit_mntopts(mntopts, &sb->s_default_mount_opts, ~0)) {
397 fprintf(stderr, _("Invalid mount option set: %s\n"),
401 ext2fs_mark_super_dirty(fs);
406 static int check_fsck_needed(ext2_filsys fs)
408 if (fs->super->s_state & EXT2_VALID_FS)
410 printf("\n%s\n", _(please_fsck));
411 if (mount_flags & EXT2_MF_READONLY)
412 printf("%s", _("(and reboot afterwards!)\n"));
416 static void request_dir_fsck_afterwards(ext2_filsys fs)
418 static int requested;
423 fs->super->s_state &= ~EXT2_VALID_FS;
424 printf("\n%s\n", _(please_dir_fsck));
425 if (mount_flags & EXT2_MF_READONLY)
426 printf("%s", _("(and reboot afterwards!)\n"));
429 static void request_fsck_afterwards(ext2_filsys fs)
431 static int requested = 0;
436 fs->super->s_state &= ~EXT2_VALID_FS;
437 printf("\n%s\n", _(please_fsck));
438 if (mount_flags & EXT2_MF_READONLY)
439 printf("%s", _("(and reboot afterwards!)\n"));
442 static void convert_64bit(ext2_filsys fs, int direction)
448 * Is resize2fs going to demand a fsck run? Might as well tell the
451 if (!fsck_requested &&
452 ((fs->super->s_state & EXT2_ERROR_FS) ||
453 !(fs->super->s_state & EXT2_VALID_FS) ||
454 fs->super->s_lastcheck < fs->super->s_mtime))
455 request_fsck_afterwards(fs);
457 fprintf(stderr, _("After running e2fsck, please run `resize2fs %s %s"),
458 direction > 0 ? "-b" : "-s", fs->device_name);
460 fprintf(stderr, _("Please run `resize2fs %s %s"),
461 direction > 0 ? "-b" : "-s", fs->device_name);
464 fprintf(stderr, _(" -z \"%s\""), undo_file);
466 fprintf(stderr, _("' to enable 64-bit mode.\n"));
468 fprintf(stderr, _("' to disable 64-bit mode.\n"));
471 /* Rewrite extents */
472 static errcode_t rewrite_extents(ext2_filsys fs, ext2_ino_t ino,
473 struct ext2_inode *inode)
475 ext2_extent_handle_t handle;
476 struct ext2fs_extent extent;
478 struct ext2_extent_info info;
480 if (!(inode->i_flags & EXT4_EXTENTS_FL) ||
481 !ext2fs_has_feature_metadata_csum(fs->super))
484 errcode = ext2fs_extent_open(fs, ino, &handle);
488 errcode = ext2fs_extent_get(handle, EXT2_EXTENT_ROOT, &extent);
493 errcode = ext2fs_extent_get_info(handle, &info);
498 * If this is the first extent in an extent block that we
499 * haven't visited, rewrite the extent to force the ETB
500 * checksum to be rewritten.
502 if (info.curr_entry == 1 && info.curr_level != 0 &&
503 !(extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT)) {
504 errcode = ext2fs_extent_replace(handle, 0, &extent);
509 /* Skip to the end of a block of leaf nodes */
510 if (extent.e_flags & EXT2_EXTENT_FLAGS_LEAF) {
511 errcode = ext2fs_extent_get(handle,
512 EXT2_EXTENT_LAST_SIB,
518 errcode = ext2fs_extent_get(handle, EXT2_EXTENT_NEXT, &extent);
519 } while (errcode == 0);
522 /* Ok if we run off the end */
523 if (errcode == EXT2_ET_EXTENT_NO_NEXT)
525 ext2fs_extent_free(handle);
530 * Rewrite directory blocks with checksums
532 struct rewrite_dir_context {
539 static int rewrite_dir_block(ext2_filsys fs,
541 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
542 blk64_t ref_block EXT2FS_ATTR((unused)),
543 int ref_offset EXT2FS_ATTR((unused)),
546 struct ext2_dx_countlimit *dcl = NULL;
547 struct rewrite_dir_context *ctx = priv_data;
548 int dcl_offset, changed = 0;
550 ctx->errcode = ext2fs_read_dir_block4(fs, *blocknr, ctx->buf, 0,
555 /* if htree node... */
557 ext2fs_get_dx_countlimit(fs, (struct ext2_dir_entry *)ctx->buf,
560 if (!ext2fs_has_feature_metadata_csum(fs->super)) {
561 /* Ensure limit is the max size */
562 int max_entries = (fs->blocksize - dcl_offset) /
563 sizeof(struct ext2_dx_entry);
564 if (ext2fs_le16_to_cpu(dcl->limit) != max_entries) {
566 dcl->limit = ext2fs_cpu_to_le16(max_entries);
569 /* If htree block is full then rebuild the dir */
570 if (ext2fs_le16_to_cpu(dcl->count) ==
571 ext2fs_le16_to_cpu(dcl->limit)) {
572 request_dir_fsck_afterwards(fs);
576 * Ensure dcl->limit is small enough to leave room for
579 int max_entries = (fs->blocksize - (dcl_offset +
580 sizeof(struct ext2_dx_tail))) /
581 sizeof(struct ext2_dx_entry);
582 if (ext2fs_le16_to_cpu(dcl->limit) != max_entries)
583 dcl->limit = ext2fs_cpu_to_le16(max_entries);
584 /* Always rewrite checksum */
588 unsigned int rec_len, name_size;
589 char *top = ctx->buf + fs->blocksize;
590 struct ext2_dir_entry *de = (struct ext2_dir_entry *)ctx->buf;
591 struct ext2_dir_entry *last_de = NULL, *penultimate_de = NULL;
593 /* Find last and penultimate dirent */
594 while ((char *)de < top) {
595 penultimate_de = last_de;
597 ctx->errcode = ext2fs_get_rec_len(fs, de, &rec_len);
598 if (!ctx->errcode && !rec_len)
599 ctx->errcode = EXT2_ET_DIR_CORRUPTED;
602 de = (struct ext2_dir_entry *)(((char *)de) + rec_len);
604 ctx->errcode = ext2fs_get_rec_len(fs, last_de, &rec_len);
607 name_size = ext2fs_dirent_name_len(last_de);
609 if (!ext2fs_has_feature_metadata_csum(fs->super)) {
612 if (last_de->inode ||
614 rec_len != sizeof(struct ext2_dir_entry_tail))
617 * The last dirent is unused and the right length to
618 * have stored a checksum. Erase it.
620 ctx->errcode = ext2fs_get_rec_len(fs, penultimate_de,
623 ctx->errcode = EXT2_ET_DIR_CORRUPTED;
626 ext2fs_set_rec_len(fs, rec_len +
627 sizeof(struct ext2_dir_entry_tail),
631 unsigned csum_size = sizeof(struct ext2_dir_entry_tail);
632 struct ext2_dir_entry_tail *t;
635 * If the last dirent looks like the tail, just update
638 if (!last_de->inode &&
639 rec_len == csum_size) {
640 t = (struct ext2_dir_entry_tail *)last_de;
641 t->det_reserved_name_len =
642 EXT2_DIR_NAME_LEN_CSUM;
647 name_size = (name_size & ~3) + 4;
648 /* If there's not enough space for the tail, e2fsck */
649 if (rec_len <= (8 + name_size + csum_size)) {
650 request_dir_fsck_afterwards(fs);
653 /* Shorten that last de and insert the tail */
654 ext2fs_set_rec_len(fs, rec_len - csum_size, last_de);
655 t = EXT2_DIRENT_TAIL(ctx->buf, fs->blocksize);
656 ext2fs_initialize_dirent_tail(fs, t);
658 /* Always update checksum */
667 ctx->errcode = ext2fs_write_dir_block4(fs, *blocknr, ctx->buf,
675 static errcode_t rewrite_directory(ext2_filsys fs, ext2_ino_t dir,
676 struct ext2_inode *inode)
679 struct rewrite_dir_context ctx;
681 retval = ext2fs_get_mem(fs->blocksize, &ctx.buf);
685 ctx.is_htree = (inode->i_flags & EXT2_INDEX_FL);
688 retval = ext2fs_block_iterate3(fs, dir, BLOCK_FLAG_READ_ONLY |
689 BLOCK_FLAG_DATA_ONLY,
690 0, rewrite_dir_block, &ctx);
692 ext2fs_free_mem(&ctx.buf);
700 * Forcibly set checksums in all inodes.
702 static void rewrite_inodes(ext2_filsys fs)
704 int length = EXT2_INODE_SIZE(fs->super);
705 struct ext2_inode *inode, *zero;
707 ext2_inode_scan scan;
710 blk64_t file_acl_block;
713 if (fs->super->s_creator_os != EXT2_OS_LINUX)
716 retval = ext2fs_open_inode_scan(fs, 0, &scan);
718 com_err("set_csum", retval, "while opening inode scan");
722 retval = ext2fs_get_mem(length, &inode);
724 com_err("set_csum", retval, "while allocating memory");
728 retval = ext2fs_get_memzero(length, &zero);
730 com_err("set_csum", retval, "while allocating memory");
734 retval = ext2fs_get_mem(fs->blocksize, &ea_buf);
736 com_err("set_csum", retval, "while allocating memory");
741 retval = ext2fs_get_next_inode_full(scan, &ino, inode, length);
743 com_err("set_csum", retval, "while getting next inode");
748 if (ext2fs_test_inode_bitmap2(fs->inode_map, ino)) {
751 if (memcmp(inode, zero, length) != 0) {
752 memset(inode, 0, length);
760 retval = ext2fs_write_inode_full(fs, ino, inode,
763 com_err("set_csum", retval, "while writing "
769 retval = rewrite_extents(fs, ino, inode);
771 com_err("rewrite_extents", retval,
772 "while rewriting extents");
776 if (LINUX_S_ISDIR(inode->i_mode) &&
777 ext2fs_inode_has_valid_blocks2(fs, inode)) {
778 retval = rewrite_directory(fs, ino, inode);
780 com_err("rewrite_directory", retval,
781 "while rewriting directories");
786 file_acl_block = ext2fs_file_acl_block(fs, inode);
789 retval = ext2fs_read_ext_attr3(fs, file_acl_block, ea_buf, ino);
791 com_err("rewrite_eablock", retval,
792 "while rewriting extended attribute");
795 retval = ext2fs_write_ext_attr3(fs, file_acl_block, ea_buf,
798 com_err("rewrite_eablock", retval,
799 "while rewriting extended attribute");
804 ext2fs_free_mem(&zero);
805 ext2fs_free_mem(&inode);
806 ext2fs_free_mem(&ea_buf);
807 ext2fs_close_inode_scan(scan);
810 static void rewrite_metadata_checksums(ext2_filsys fs)
815 fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
816 ext2fs_init_csum_seed(fs);
817 for (i = 0; i < fs->group_desc_count; i++)
818 ext2fs_group_desc_csum_set(fs, i);
819 retval = ext2fs_read_bitmaps(fs);
821 com_err("rewrite_metadata_checksums", retval,
822 "while reading bitmaps");
826 ext2fs_mark_ib_dirty(fs);
827 ext2fs_mark_bb_dirty(fs);
828 ext2fs_mmp_update2(fs, 1);
829 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
830 fs->flags &= ~EXT2_FLAG_IGNORE_CSUM_ERRORS;
831 if (ext2fs_has_feature_metadata_csum(fs->super))
832 fs->super->s_checksum_type = EXT2_CRC32C_CHKSUM;
834 fs->super->s_checksum_type = 0;
835 ext2fs_mark_super_dirty(fs);
838 static void enable_uninit_bg(ext2_filsys fs)
840 struct ext2_group_desc *gd;
843 for (i = 0; i < fs->group_desc_count; i++) {
844 gd = ext2fs_group_desc(fs, fs->group_desc, i);
845 gd->bg_itable_unused = 0;
846 gd->bg_flags = EXT2_BG_INODE_ZEROED;
847 ext2fs_group_desc_csum_set(fs, i);
849 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
852 static errcode_t zero_empty_inodes(ext2_filsys fs)
854 int length = EXT2_INODE_SIZE(fs->super);
855 struct ext2_inode *inode = NULL;
856 ext2_inode_scan scan;
860 retval = ext2fs_open_inode_scan(fs, 0, &scan);
864 retval = ext2fs_get_mem(length, &inode);
869 retval = ext2fs_get_next_inode_full(scan, &ino, inode, length);
874 if (!ext2fs_test_inode_bitmap2(fs->inode_map, ino)) {
875 memset(inode, 0, length);
876 retval = ext2fs_write_inode_full(fs, ino, inode,
884 ext2fs_free_mem(&inode);
885 ext2fs_close_inode_scan(scan);
889 static errcode_t disable_uninit_bg(ext2_filsys fs, __u32 csum_feature_flag)
891 struct ext2_group_desc *gd;
896 /* Load bitmaps to ensure that the uninit ones get written out */
897 fs->super->s_feature_ro_compat |= csum_feature_flag;
898 retval = ext2fs_read_bitmaps(fs);
899 fs->super->s_feature_ro_compat &= ~csum_feature_flag;
901 com_err("disable_uninit_bg", retval,
902 "while reading bitmaps");
903 request_fsck_afterwards(fs);
906 ext2fs_mark_ib_dirty(fs);
907 ext2fs_mark_bb_dirty(fs);
909 /* If we're only turning off uninit_bg, zero the inodes */
910 if (csum_feature_flag == EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
911 retval = zero_empty_inodes(fs);
913 com_err("disable_uninit_bg", retval,
914 "while zeroing unused inodes");
915 request_fsck_afterwards(fs);
920 /* The bbitmap is zeroed; we must mark group metadata blocks in use */
921 for (i = 0; i < fs->group_desc_count; i++) {
922 b = ext2fs_block_bitmap_loc(fs, i);
923 ext2fs_mark_block_bitmap2(fs->block_map, b);
924 b = ext2fs_inode_bitmap_loc(fs, i);
925 ext2fs_mark_block_bitmap2(fs->block_map, b);
927 retval = ext2fs_super_and_bgd_loc2(fs, i, &b, &c, &d, NULL);
928 if (retval == 0 && b)
929 ext2fs_mark_block_bitmap2(fs->block_map, b);
930 if (retval == 0 && c)
931 ext2fs_mark_block_bitmap2(fs->block_map, c);
932 if (retval == 0 && d)
933 ext2fs_mark_block_bitmap2(fs->block_map, d);
935 com_err("disable_uninit_bg", retval,
936 "while initializing block bitmaps");
937 request_fsck_afterwards(fs);
940 gd = ext2fs_group_desc(fs, fs->group_desc, i);
941 gd->bg_itable_unused = 0;
943 ext2fs_group_desc_csum_set(fs, i);
945 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
946 ext2fs_mark_super_dirty(fs);
952 * Update the feature set as provided by the user.
954 static int update_feature_set(ext2_filsys fs, char *features)
956 struct ext2_super_block *sb = fs->super;
957 __u32 old_features[3];
959 unsigned int mask_err;
962 #define FEATURE_ON(type, mask) (!(old_features[(type)] & (mask)) && \
963 ((&sb->s_feature_compat)[(type)] & (mask)))
964 #define FEATURE_OFF(type, mask) ((old_features[(type)] & (mask)) && \
965 !((&sb->s_feature_compat)[(type)] & (mask)))
966 #define FEATURE_CHANGED(type, mask) ((mask) & \
967 (old_features[(type)] ^ (&sb->s_feature_compat)[(type)]))
969 old_features[E2P_FEATURE_COMPAT] = sb->s_feature_compat;
970 old_features[E2P_FEATURE_INCOMPAT] = sb->s_feature_incompat;
971 old_features[E2P_FEATURE_RO_INCOMPAT] = sb->s_feature_ro_compat;
973 if (e2p_edit_feature2(features, &sb->s_feature_compat,
974 ok_features, clear_ok_features,
975 &type_err, &mask_err)) {
978 _("Invalid filesystem option set: %s\n"),
980 else if (type_err & E2P_FEATURE_NEGATE_FLAG)
981 fprintf(stderr, _("Clearing filesystem feature '%s' "
983 e2p_feature2string(type_err &
984 E2P_FEATURE_TYPE_MASK,
987 fprintf(stderr, _("Setting filesystem feature '%s' "
989 e2p_feature2string(type_err, mask_err));
993 if (FEATURE_OFF(E2P_FEATURE_COMPAT, EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
994 if ((mount_flags & EXT2_MF_MOUNTED) &&
995 !(mount_flags & EXT2_MF_READONLY)) {
996 fputs(_("The has_journal feature may only be "
997 "cleared when the filesystem is\n"
998 "unmounted or mounted "
999 "read-only.\n"), stderr);
1002 if (ext2fs_has_feature_journal_needs_recovery(sb) &&
1004 fputs(_("The needs_recovery flag is set. "
1005 "Please run e2fsck before clearing\n"
1006 "the has_journal flag.\n"), stderr);
1009 if (sb->s_journal_inum) {
1010 if (remove_journal_inode(fs))
1013 if (sb->s_journal_dev) {
1014 if (remove_journal_device(fs))
1019 if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT,
1020 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
1021 if (ext2fs_has_feature_meta_bg(sb)) {
1022 fputs(_("Setting filesystem feature 'sparse_super' "
1023 "not supported\nfor filesystems with "
1024 "the meta_bg feature enabled.\n"),
1030 if (FEATURE_ON(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_MMP)) {
1033 if ((mount_flags & EXT2_MF_MOUNTED) ||
1034 (mount_flags & EXT2_MF_READONLY)) {
1035 fputs(_("The multiple mount protection feature can't\n"
1036 "be set if the filesystem is mounted or\n"
1037 "read-only.\n"), stderr);
1041 error = ext2fs_mmp_init(fs);
1043 fputs(_("\nError while enabling multiple mount "
1044 "protection feature."), stderr);
1049 * We want to update group desc with the new free blocks count
1051 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
1053 printf(_("Multiple mount protection has been enabled "
1054 "with update interval %ds.\n"),
1055 sb->s_mmp_update_interval);
1058 if (FEATURE_OFF(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_MMP)) {
1061 if (mount_flags & EXT2_MF_READONLY) {
1062 fputs(_("The multiple mount protection feature cannot\n"
1063 "be disabled if the filesystem is readonly.\n"),
1068 error = ext2fs_read_bitmaps(fs);
1070 fputs(_("Error while reading bitmaps\n"), stderr);
1074 error = ext2fs_mmp_read(fs, sb->s_mmp_block, NULL);
1076 struct mmp_struct *mmp_cmp = fs->mmp_cmp;
1078 if (error == EXT2_ET_MMP_MAGIC_INVALID)
1079 printf(_("Magic number in MMP block does not "
1080 "match. expected: %x, actual: %x\n"),
1081 EXT4_MMP_MAGIC, mmp_cmp->mmp_magic);
1083 com_err(program_name, error, "%s",
1084 _("while reading MMP block."));
1088 /* We need to force out the group descriptors as well */
1089 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
1090 ext2fs_block_alloc_stats2(fs, sb->s_mmp_block, -1);
1092 sb->s_mmp_block = 0;
1093 sb->s_mmp_update_interval = 0;
1096 if (FEATURE_ON(E2P_FEATURE_COMPAT, EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
1098 * If adding a journal flag, let the create journal
1099 * code below handle setting the flag and creating the
1100 * journal. We supply a default size if necessary.
1104 ext2fs_clear_feature_journal(sb);
1107 if (FEATURE_ON(E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_DIR_INDEX)) {
1108 if (!sb->s_def_hash_version)
1109 sb->s_def_hash_version = EXT2_HASH_HALF_MD4;
1110 if (uuid_is_null((unsigned char *) sb->s_hash_seed))
1111 uuid_generate((unsigned char *) sb->s_hash_seed);
1114 if (FEATURE_OFF(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
1115 if (ext2fs_check_desc(fs)) {
1116 fputs(_("Clearing the flex_bg flag would "
1117 "cause the the filesystem to be\n"
1118 "inconsistent.\n"), stderr);
1123 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
1124 EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
1125 if ((mount_flags & EXT2_MF_MOUNTED) &&
1126 !(mount_flags & EXT2_MF_READONLY)) {
1127 fputs(_("The huge_file feature may only be "
1128 "cleared when the filesystem is\n"
1129 "unmounted or mounted "
1130 "read-only.\n"), stderr);
1135 if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT,
1136 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
1137 if (check_fsck_needed(fs))
1139 if (mount_flags & EXT2_MF_MOUNTED) {
1140 fputs(_("Cannot enable metadata_csum on a mounted "
1141 "filesystem!\n"), stderr);
1144 if (!ext2fs_has_feature_extents(fs->super))
1146 _("Extents are not enabled. The file extent "
1147 "tree can be checksummed, whereas block maps "
1148 "cannot. Not enabling extents reduces the "
1149 "coverage of metadata checksumming. "
1150 "Re-run with -O extent to rectify.\n"));
1151 if (!ext2fs_has_feature_64bit(fs->super))
1153 _("64-bit filesystem support is not enabled. "
1154 "The larger fields afforded by this feature "
1155 "enable full-strength checksumming. "
1156 "Run resize2fs -b to rectify.\n"));
1157 rewrite_checksums = 1;
1158 /* metadata_csum supersedes uninit_bg */
1159 ext2fs_clear_feature_gdt_csum(fs->super);
1161 /* if uninit_bg was previously off, rewrite group desc */
1162 if (!(old_features[E2P_FEATURE_RO_INCOMPAT] &
1163 EXT4_FEATURE_RO_COMPAT_GDT_CSUM))
1164 enable_uninit_bg(fs);
1167 * Since metadata_csum supersedes uninit_bg, pretend like
1168 * uninit_bg has been off all along.
1170 old_features[E2P_FEATURE_RO_INCOMPAT] &=
1171 ~EXT4_FEATURE_RO_COMPAT_GDT_CSUM;
1174 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
1175 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
1176 __u32 test_features[3];
1178 if (check_fsck_needed(fs))
1180 if (mount_flags & EXT2_MF_MOUNTED) {
1181 fputs(_("Cannot disable metadata_csum on a mounted "
1182 "filesystem!\n"), stderr);
1185 rewrite_checksums = 1;
1187 /* Enable uninit_bg unless the user expressly turned it off */
1188 memcpy(test_features, old_features, sizeof(test_features));
1189 test_features[E2P_FEATURE_RO_INCOMPAT] |=
1190 EXT4_FEATURE_RO_COMPAT_GDT_CSUM;
1191 e2p_edit_feature2(features, test_features, ok_features,
1192 clear_ok_features, NULL, NULL);
1193 if (test_features[E2P_FEATURE_RO_INCOMPAT] &
1194 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)
1195 ext2fs_set_feature_gdt_csum(fs->super);
1198 * If we're turning off metadata_csum and not turning on
1199 * uninit_bg, rewrite group desc.
1201 if (!ext2fs_has_feature_gdt_csum(fs->super)) {
1202 err = disable_uninit_bg(fs,
1203 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM);
1208 * metadata_csum previously provided uninit_bg, so if
1209 * we're also setting the uninit_bg feature bit,
1210 * pretend like it was previously enabled. Checksums
1211 * will be rewritten with crc16 later.
1213 old_features[E2P_FEATURE_RO_INCOMPAT] |=
1214 EXT4_FEATURE_RO_COMPAT_GDT_CSUM;
1217 if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT,
1218 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
1219 /* Do not enable uninit_bg when metadata_csum enabled */
1220 if (ext2fs_has_feature_metadata_csum(fs->super))
1221 ext2fs_clear_feature_gdt_csum(fs->super);
1223 enable_uninit_bg(fs);
1226 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
1227 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
1228 err = disable_uninit_bg(fs,
1229 EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
1235 * We don't actually toggle 64bit; resize2fs does that. But this
1236 * must come after the metadata_csum feature_on so that it won't
1237 * complain about the lack of 64bit.
1239 if (FEATURE_ON(E2P_FEATURE_INCOMPAT,
1240 EXT4_FEATURE_INCOMPAT_64BIT)) {
1241 if (mount_flags & EXT2_MF_MOUNTED) {
1242 fprintf(stderr, _("Cannot enable 64-bit mode "
1243 "while mounted!\n"));
1246 ext2fs_clear_feature_64bit(sb);
1249 if (FEATURE_OFF(E2P_FEATURE_INCOMPAT,
1250 EXT4_FEATURE_INCOMPAT_64BIT)) {
1251 if (mount_flags & EXT2_MF_MOUNTED) {
1252 fprintf(stderr, _("Cannot disable 64-bit mode "
1253 "while mounted!\n"));
1256 ext2fs_set_feature_64bit(sb);
1260 if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT,
1261 EXT4_FEATURE_RO_COMPAT_QUOTA)) {
1263 * Set the Q_flag here and handle the quota options in the code
1268 /* Enable both user quota and group quota by default */
1269 usrquota = QOPT_ENABLE;
1270 grpquota = QOPT_ENABLE;
1272 ext2fs_clear_feature_quota(sb);
1275 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
1276 EXT4_FEATURE_RO_COMPAT_QUOTA)) {
1278 * Set the Q_flag here and handle the quota options in the code
1282 fputs(_("\nWarning: '^quota' option overrides '-Q'"
1283 "arguments.\n"), stderr);
1285 /* Disable both user quota and group quota by default */
1286 usrquota = QOPT_DISABLE;
1287 grpquota = QOPT_DISABLE;
1290 if (FEATURE_ON(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_ENCRYPT)) {
1291 fs->super->s_encrypt_algos[0] =
1292 EXT4_ENCRYPTION_MODE_AES_256_XTS;
1293 fs->super->s_encrypt_algos[1] =
1294 EXT4_ENCRYPTION_MODE_AES_256_CTS;
1297 if (sb->s_rev_level == EXT2_GOOD_OLD_REV &&
1298 (sb->s_feature_compat || sb->s_feature_ro_compat ||
1299 sb->s_feature_incompat))
1300 ext2fs_update_dynamic_rev(fs);
1302 if (FEATURE_CHANGED(E2P_FEATURE_RO_INCOMPAT,
1303 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER) ||
1304 FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
1305 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) ||
1306 FEATURE_CHANGED(E2P_FEATURE_INCOMPAT,
1307 EXT2_FEATURE_INCOMPAT_FILETYPE) ||
1308 FEATURE_CHANGED(E2P_FEATURE_COMPAT,
1309 EXT2_FEATURE_COMPAT_RESIZE_INODE) ||
1310 FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
1311 EXT2_FEATURE_RO_COMPAT_LARGE_FILE))
1312 request_fsck_afterwards(fs);
1314 if ((old_features[E2P_FEATURE_COMPAT] != sb->s_feature_compat) ||
1315 (old_features[E2P_FEATURE_INCOMPAT] != sb->s_feature_incompat) ||
1316 (old_features[E2P_FEATURE_RO_INCOMPAT] != sb->s_feature_ro_compat))
1317 ext2fs_mark_super_dirty(fs);
1323 * Add a journal to the filesystem.
1325 static int add_journal(ext2_filsys fs)
1327 unsigned long journal_blocks;
1332 if (ext2fs_has_feature_journal(fs->super)) {
1333 fputs(_("The filesystem already has a journal.\n"), stderr);
1336 if (journal_device) {
1337 if (!check_plausibility(journal_device, CHECK_BLOCK_DEV,
1339 proceed_question(-1);
1340 check_mount(journal_device, 0, _("journal"));
1341 #ifdef CONFIG_TESTIO_DEBUG
1342 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
1343 io_ptr = test_io_manager;
1344 test_io_backing_manager = unix_io_manager;
1347 io_ptr = unix_io_manager;
1348 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
1349 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1350 fs->blocksize, io_ptr, &jfs);
1352 com_err(program_name, retval,
1353 _("\n\twhile trying to open journal on %s\n"),
1357 printf(_("Creating journal on device %s: "),
1361 retval = ext2fs_add_journal_device(fs, jfs);
1362 ext2fs_close_free(&jfs);
1364 com_err(program_name, retval,
1365 _("while adding filesystem to journal on %s"),
1369 fputs(_("done\n"), stdout);
1370 } else if (journal_size) {
1371 fputs(_("Creating journal inode: "), stdout);
1373 journal_blocks = figure_journal_size(journal_size, fs);
1375 if (journal_location_string)
1377 parse_num_blocks2(journal_location_string,
1378 fs->super->s_log_block_size);
1379 retval = ext2fs_add_journal_inode2(fs, journal_blocks,
1383 fprintf(stderr, "\n");
1384 com_err(program_name, retval, "%s",
1385 _("\n\twhile trying to create journal file"));
1388 fputs(_("done\n"), stdout);
1390 * If the filesystem wasn't mounted, we need to force
1391 * the block group descriptors out.
1393 if ((mount_flags & EXT2_MF_MOUNTED) == 0)
1394 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
1396 print_check_message(fs->super->s_max_mnt_count,
1397 fs->super->s_checkinterval);
1401 free(journal_device);
1405 static void handle_quota_options(ext2_filsys fs)
1410 if (!usrquota && !grpquota)
1411 /* Nothing to do. */
1414 quota_init_context(&qctx, fs, -1);
1416 if (usrquota == QOPT_ENABLE || grpquota == QOPT_ENABLE)
1417 quota_compute_usage(qctx);
1419 if (usrquota == QOPT_ENABLE && !fs->super->s_usr_quota_inum) {
1420 if ((qf_ino = quota_file_exists(fs, USRQUOTA)) > 0)
1421 quota_update_limits(qctx, qf_ino, USRQUOTA);
1422 quota_write_inode(qctx, USRQUOTA);
1423 } else if (usrquota == QOPT_DISABLE) {
1424 quota_remove_inode(fs, USRQUOTA);
1427 if (grpquota == QOPT_ENABLE && !fs->super->s_grp_quota_inum) {
1428 if ((qf_ino = quota_file_exists(fs, GRPQUOTA)) > 0)
1429 quota_update_limits(qctx, qf_ino, GRPQUOTA);
1430 quota_write_inode(qctx, GRPQUOTA);
1431 } else if (grpquota == QOPT_DISABLE) {
1432 quota_remove_inode(fs, GRPQUOTA);
1435 quota_release_context(&qctx);
1437 if ((usrquota == QOPT_ENABLE) || (grpquota == QOPT_ENABLE)) {
1438 ext2fs_set_feature_quota(fs->super);
1439 ext2fs_mark_super_dirty(fs);
1440 } else if (!fs->super->s_usr_quota_inum &&
1441 !fs->super->s_grp_quota_inum) {
1442 ext2fs_clear_feature_quota(fs->super);
1443 ext2fs_mark_super_dirty(fs);
1449 static void parse_quota_opts(const char *opts)
1451 char *buf, *token, *next, *p;
1455 buf = malloc(len+1);
1457 fputs(_("Couldn't allocate memory to parse quota "
1458 "options!\n"), stderr);
1462 for (token = buf; token && *token; token = next) {
1463 p = strchr(token, ',');
1470 if (strcmp(token, "usrquota") == 0) {
1471 usrquota = QOPT_ENABLE;
1472 } else if (strcmp(token, "^usrquota") == 0) {
1473 usrquota = QOPT_DISABLE;
1474 } else if (strcmp(token, "grpquota") == 0) {
1475 grpquota = QOPT_ENABLE;
1476 } else if (strcmp(token, "^grpquota") == 0) {
1477 grpquota = QOPT_DISABLE;
1479 fputs(_("\nBad quota options specified.\n\n"
1480 "Following valid quota options are available "
1481 "(pass by separating with comma):\n"
1492 static void parse_e2label_options(int argc, char ** argv)
1494 if ((argc < 2) || (argc > 3)) {
1495 fputs(_("Usage: e2label device [newlabel]\n"), stderr);
1498 io_options = strchr(argv[1], '?');
1501 device_name = blkid_get_devname(NULL, argv[1], NULL);
1503 com_err("e2label", 0, _("Unable to resolve '%s'"),
1507 open_flag = EXT2_FLAG_JOURNAL_DEV_OK;
1509 open_flag |= EXT2_FLAG_RW;
1511 new_label = argv[2];
1516 static time_t parse_time(char *str)
1520 if (strcmp(str, "now") == 0) {
1523 memset(&ts, 0, sizeof(ts));
1524 #ifdef HAVE_STRPTIME
1525 strptime(str, "%Y%m%d%H%M%S", &ts);
1527 sscanf(str, "%4d%2d%2d%2d%2d%2d", &ts.tm_year, &ts.tm_mon,
1528 &ts.tm_mday, &ts.tm_hour, &ts.tm_min, &ts.tm_sec);
1531 if (ts.tm_year < 0 || ts.tm_mon < 0 || ts.tm_mon > 11 ||
1532 ts.tm_mday < 0 || ts.tm_mday > 31 || ts.tm_hour > 23 ||
1533 ts.tm_min > 59 || ts.tm_sec > 61)
1536 if (ts.tm_mday == 0) {
1537 com_err(program_name, 0,
1538 _("Couldn't parse date/time specifier: %s"),
1543 return (mktime(&ts));
1546 static void parse_tune2fs_options(int argc, char **argv)
1552 char optstring[100] = "c:e:fg:i:jlm:o:r:s:u:C:E:I:J:L:M:O:T:U:z:Q:";
1555 printf("tune2fs %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
1556 while ((c = getopt(argc, argv, optstring)) != EOF)
1559 max_mount_count = strtol(optarg, &tmp, 0);
1560 if (*tmp || max_mount_count > 16000) {
1561 com_err(program_name, 0,
1562 _("bad mounts count - %s"),
1566 if (max_mount_count == 0)
1567 max_mount_count = -1;
1569 open_flag = EXT2_FLAG_RW;
1572 mount_count = strtoul(optarg, &tmp, 0);
1573 if (*tmp || mount_count > 16000) {
1574 com_err(program_name, 0,
1575 _("bad mounts count - %s"),
1580 open_flag = EXT2_FLAG_RW;
1583 if (strcmp(optarg, "continue") == 0)
1584 errors = EXT2_ERRORS_CONTINUE;
1585 else if (strcmp(optarg, "remount-ro") == 0)
1586 errors = EXT2_ERRORS_RO;
1587 else if (strcmp(optarg, "panic") == 0)
1588 errors = EXT2_ERRORS_PANIC;
1590 com_err(program_name, 0,
1591 _("bad error behavior - %s"),
1596 open_flag = EXT2_FLAG_RW;
1599 extended_cmd = optarg;
1600 open_flag |= EXT2_FLAG_RW;
1602 case 'f': /* Force */
1606 resgid = strtoul(optarg, &tmp, 0);
1608 gr = getgrnam(optarg);
1612 resgid = gr->gr_gid;
1617 com_err(program_name, 0,
1618 _("bad gid/group name - %s"),
1623 open_flag = EXT2_FLAG_RW;
1626 interval = strtoul(optarg, &tmp, 0);
1633 case 'D': /* days */
1639 case 'M': /* months! */
1640 interval *= 86400 * 30;
1644 case 'W': /* weeks */
1645 interval *= 86400 * 7;
1650 com_err(program_name, 0,
1651 _("bad interval - %s"), optarg);
1655 open_flag = EXT2_FLAG_RW;
1660 open_flag = EXT2_FLAG_RW;
1663 parse_journal_opts(optarg);
1664 open_flag = EXT2_FLAG_RW;
1672 open_flag |= EXT2_FLAG_RW |
1673 EXT2_FLAG_JOURNAL_DEV_OK;
1676 reserved_ratio = strtod(optarg, &tmp);
1677 if (*tmp || reserved_ratio > 50 ||
1678 reserved_ratio < 0) {
1679 com_err(program_name, 0,
1680 _("bad reserved block ratio - %s"),
1685 open_flag = EXT2_FLAG_RW;
1688 new_last_mounted = optarg;
1690 open_flag = EXT2_FLAG_RW;
1694 com_err(program_name, 0, "%s",
1695 _("-o may only be specified once"));
1698 mntopts_cmd = optarg;
1699 open_flag = EXT2_FLAG_RW;
1703 com_err(program_name, 0, "%s",
1704 _("-O may only be specified once"));
1707 features_cmd = optarg;
1708 open_flag = EXT2_FLAG_RW;
1712 parse_quota_opts(optarg);
1713 open_flag = EXT2_FLAG_RW;
1716 reserved_blocks = strtoul(optarg, &tmp, 0);
1718 com_err(program_name, 0,
1719 _("bad reserved blocks count - %s"),
1724 open_flag = EXT2_FLAG_RW;
1726 case 's': /* Deprecated */
1727 s_flag = atoi(optarg);
1728 open_flag = EXT2_FLAG_RW;
1732 last_check_time = parse_time(optarg);
1733 open_flag = EXT2_FLAG_RW;
1736 resuid = strtoul(optarg, &tmp, 0);
1738 pw = getpwnam(optarg);
1742 resuid = pw->pw_uid;
1747 com_err(program_name, 0,
1748 _("bad uid/user name - %s"),
1753 open_flag = EXT2_FLAG_RW;
1758 open_flag = EXT2_FLAG_RW |
1759 EXT2_FLAG_JOURNAL_DEV_OK;
1762 new_inode_size = strtoul(optarg, &tmp, 0);
1764 com_err(program_name, 0,
1765 _("bad inode size - %s"),
1769 if (!((new_inode_size &
1770 (new_inode_size - 1)) == 0)) {
1771 com_err(program_name, 0,
1772 _("Inode size must be a "
1773 "power of two- %s"),
1777 open_flag = EXT2_FLAG_RW;
1786 if (optind < argc - 1 || optind == argc)
1788 if (!open_flag && !l_flag)
1790 io_options = strchr(argv[optind], '?');
1793 device_name = blkid_get_devname(NULL, argv[optind], NULL);
1795 com_err(program_name, 0, _("Unable to resolve '%s'"),
1801 #ifdef CONFIG_BUILD_FINDFS
1802 void do_findfs(int argc, char **argv)
1807 (strncmp(argv[1], "LABEL=", 6) && strncmp(argv[1], "UUID=", 5))) {
1808 fprintf(stderr, "Usage: findfs LABEL=<label>|UUID=<uuid>\n");
1811 dev = blkid_get_devname(NULL, argv[1], NULL);
1813 com_err("findfs", 0, _("Unable to resolve '%s'"),
1822 static int parse_extended_opts(ext2_filsys fs, const char *opts)
1824 char *buf, *token, *next, *p, *arg;
1829 buf = malloc(len+1);
1831 fprintf(stderr, "%s",
1832 _("Couldn't allocate memory to parse options!\n"));
1836 for (token = buf; token && *token; token = next) {
1837 p = strchr(token, ',');
1843 arg = strchr(token, '=');
1848 if (strcmp(token, "clear-mmp") == 0 ||
1849 strcmp(token, "clear_mmp") == 0) {
1851 } else if (strcmp(token, "mmp_update_interval") == 0) {
1857 intv = strtoul(arg, &p, 0);
1860 _("Invalid mmp_update_interval: %s\n"),
1866 intv = EXT4_MMP_UPDATE_INTERVAL;
1867 } else if (intv > EXT4_MMP_MAX_UPDATE_INTERVAL) {
1869 _("mmp_update_interval too big: %lu\n"),
1874 printf(P_("Setting multiple mount protection update "
1875 "interval to %lu second\n",
1876 "Setting multiple mount protection update "
1877 "interval to %lu seconds\n", intv),
1879 fs->super->s_mmp_update_interval = intv;
1880 ext2fs_mark_super_dirty(fs);
1881 } else if (!strcmp(token, "test_fs")) {
1882 fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
1883 printf("Setting test filesystem flag\n");
1884 ext2fs_mark_super_dirty(fs);
1885 } else if (!strcmp(token, "^test_fs")) {
1886 fs->super->s_flags &= ~EXT2_FLAGS_TEST_FILESYS;
1887 printf("Clearing test filesystem flag\n");
1888 ext2fs_mark_super_dirty(fs);
1889 } else if (strcmp(token, "stride") == 0) {
1894 stride = strtoul(arg, &p, 0);
1897 _("Invalid RAID stride: %s\n"),
1903 } else if (strcmp(token, "stripe-width") == 0 ||
1904 strcmp(token, "stripe_width") == 0) {
1909 stripe_width = strtoul(arg, &p, 0);
1912 _("Invalid RAID stripe-width: %s\n"),
1917 stripe_width_set = 1;
1918 } else if (strcmp(token, "hash_alg") == 0 ||
1919 strcmp(token, "hash-alg") == 0) {
1924 hash_alg = e2p_string2hash(arg);
1927 _("Invalid hash algorithm: %s\n"),
1932 fs->super->s_def_hash_version = hash_alg;
1933 printf(_("Setting default hash algorithm "
1936 ext2fs_mark_super_dirty(fs);
1937 } else if (!strcmp(token, "mount_opts")) {
1942 if (strlen(arg) >= sizeof(fs->super->s_mount_opts)) {
1944 "Extended mount options too long\n");
1947 ext_mount_opts = strdup(arg);
1952 fprintf(stderr, "%s", _("\nBad options specified.\n\n"
1953 "Extended options are separated by commas, "
1954 "and may take an argument which\n"
1955 "\tis set off by an equals ('=') sign.\n\n"
1956 "Valid extended options are:\n"
1958 "\thash_alg=<hash algorithm>\n"
1959 "\tmount_opts=<extended default mount options>\n"
1960 "\tstride=<RAID per-disk chunk size in blocks>\n"
1961 "\tstripe_width=<RAID stride*data disks in blocks>\n"
1973 * Fill in the block bitmap bmap with the information regarding the
1974 * blocks to be moved
1976 static int get_move_bitmaps(ext2_filsys fs, int new_ino_blks_per_grp,
1977 ext2fs_block_bitmap bmap)
1981 ext2_badblocks_list bb_list = 0;
1982 blk64_t j, needed_blocks = 0;
1983 blk64_t start_blk, end_blk;
1985 retval = ext2fs_read_bb_inode(fs, &bb_list);
1989 for (i = 0; i < fs->group_desc_count; i++) {
1990 start_blk = ext2fs_inode_table_loc(fs, i) +
1991 fs->inode_blocks_per_group;
1993 end_blk = ext2fs_inode_table_loc(fs, i) +
1994 new_ino_blks_per_grp;
1996 for (j = start_blk; j < end_blk; j++) {
1997 if (ext2fs_test_block_bitmap2(fs->block_map, j)) {
1999 * IF the block is a bad block we fail
2001 if (ext2fs_badblocks_list_test(bb_list, j)) {
2002 ext2fs_badblocks_list_free(bb_list);
2006 ext2fs_mark_block_bitmap2(bmap, j);
2009 * We are going to use this block for
2010 * inode table. So mark them used.
2012 ext2fs_mark_block_bitmap2(fs->block_map, j);
2015 needed_blocks += end_blk - start_blk;
2018 ext2fs_badblocks_list_free(bb_list);
2019 if (needed_blocks > ext2fs_free_blocks_count(fs->super))
2025 static int ext2fs_is_meta_block(ext2_filsys fs, blk64_t blk)
2028 group = ext2fs_group_of_blk2(fs, blk);
2029 if (ext2fs_block_bitmap_loc(fs, group) == blk)
2031 if (ext2fs_inode_bitmap_loc(fs, group) == blk)
2036 static int ext2fs_is_block_in_group(ext2_filsys fs, dgrp_t group, blk64_t blk)
2038 blk64_t start_blk, end_blk;
2039 start_blk = fs->super->s_first_data_block +
2040 EXT2_GROUPS_TO_BLOCKS(fs->super, group);
2042 * We cannot get new block beyond end_blk for for the last block group
2043 * so we can check with EXT2_BLOCKS_PER_GROUP even for last block group
2045 end_blk = start_blk + EXT2_BLOCKS_PER_GROUP(fs->super);
2046 if (blk >= start_blk && blk <= end_blk)
2051 static int move_block(ext2_filsys fs, ext2fs_block_bitmap bmap)
2058 blk64_t blk, new_blk, goal;
2059 struct blk_move *bmv;
2061 retval = ext2fs_get_mem(fs->blocksize, &buf);
2065 for (new_blk = blk = fs->super->s_first_data_block;
2066 blk < ext2fs_blocks_count(fs->super); blk++) {
2067 if (!ext2fs_test_block_bitmap2(bmap, blk))
2070 if (ext2fs_is_meta_block(fs, blk)) {
2072 * If the block is mapping a fs meta data block
2073 * like group desc/block bitmap/inode bitmap. We
2074 * should find a block in the same group and fix
2075 * the respective fs metadata pointers. Otherwise
2078 group = ext2fs_group_of_blk2(fs, blk);
2079 goal = ext2fs_group_first_block2(fs, group);
2085 retval = ext2fs_new_block2(fs, goal, NULL, &new_blk);
2089 /* new fs meta data block should be in the same group */
2090 if (meta_data && !ext2fs_is_block_in_group(fs, group, new_blk)) {
2095 /* Mark this block as allocated */
2096 ext2fs_mark_block_bitmap2(fs->block_map, new_blk);
2098 /* Add it to block move list */
2099 retval = ext2fs_get_mem(sizeof(struct blk_move), &bmv);
2104 bmv->new_loc = new_blk;
2106 list_add(&(bmv->list), &blk_move_list);
2108 retval = io_channel_read_blk64(fs->io, blk, 1, buf);
2112 retval = io_channel_write_blk64(fs->io, new_blk, 1, buf);
2118 ext2fs_free_mem(&buf);
2122 static blk64_t translate_block(blk64_t blk)
2124 struct list_head *entry;
2125 struct blk_move *bmv;
2127 list_for_each(entry, &blk_move_list) {
2128 bmv = list_entry(entry, struct blk_move, list);
2129 if (bmv->old_loc == blk)
2130 return bmv->new_loc;
2136 static int process_block(ext2_filsys fs EXT2FS_ATTR((unused)),
2138 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
2139 blk64_t ref_block EXT2FS_ATTR((unused)),
2140 int ref_offset EXT2FS_ATTR((unused)),
2145 ext2fs_block_bitmap bmap = (ext2fs_block_bitmap) priv_data;
2147 if (!ext2fs_test_block_bitmap2(bmap, *block_nr))
2149 new_blk = translate_block(*block_nr);
2151 *block_nr = new_blk;
2153 * This will force the ext2fs_write_inode in the iterator
2155 ret |= BLOCK_CHANGED;
2161 static int inode_scan_and_fix(ext2_filsys fs, ext2fs_block_bitmap bmap)
2163 errcode_t retval = 0;
2166 char *block_buf = 0;
2167 struct ext2_inode inode;
2168 ext2_inode_scan scan = NULL;
2170 retval = ext2fs_get_mem(fs->blocksize * 3, &block_buf);
2174 retval = ext2fs_open_inode_scan(fs, 0, &scan);
2179 retval = ext2fs_get_next_inode(scan, &ino, &inode);
2186 if (inode.i_links_count == 0)
2187 continue; /* inode not in use */
2190 * If we end up modifying the journal inode
2191 * the sb->s_jnl_blocks will differ. But a
2192 * subsequent e2fsck fixes that.
2193 * Do we need to fix this ??
2196 if (ext2fs_file_acl_block(fs, &inode) &&
2197 ext2fs_test_block_bitmap2(bmap,
2198 ext2fs_file_acl_block(fs, &inode))) {
2199 blk = translate_block(ext2fs_file_acl_block(fs,
2204 ext2fs_file_acl_block_set(fs, &inode, blk);
2207 * Write the inode to disk so that inode table
2210 retval = ext2fs_write_inode(fs, ino, &inode);
2215 if (!ext2fs_inode_has_valid_blocks2(fs, &inode))
2218 retval = ext2fs_block_iterate3(fs, ino, 0, block_buf,
2219 process_block, bmap);
2226 ext2fs_free_mem(&block_buf);
2227 ext2fs_close_inode_scan(scan);
2233 * We need to scan for inode and block bitmaps that may need to be
2234 * moved. This can take place if the filesystem was formatted for
2235 * RAID arrays using the mke2fs's extended option "stride".
2237 static int group_desc_scan_and_fix(ext2_filsys fs, ext2fs_block_bitmap bmap)
2240 blk64_t blk, new_blk;
2242 for (i = 0; i < fs->group_desc_count; i++) {
2243 blk = ext2fs_block_bitmap_loc(fs, i);
2244 if (ext2fs_test_block_bitmap2(bmap, blk)) {
2245 new_blk = translate_block(blk);
2248 ext2fs_block_bitmap_loc_set(fs, i, new_blk);
2251 blk = ext2fs_inode_bitmap_loc(fs, i);
2252 if (ext2fs_test_block_bitmap2(bmap, blk)) {
2253 new_blk = translate_block(blk);
2256 ext2fs_inode_bitmap_loc_set(fs, i, new_blk);
2262 static int expand_inode_table(ext2_filsys fs, unsigned long new_ino_size)
2267 int new_ino_blks_per_grp;
2269 char *old_itable = NULL, *new_itable = NULL;
2270 char *tmp_old_itable = NULL, *tmp_new_itable = NULL;
2271 unsigned long old_ino_size;
2272 int old_itable_size, new_itable_size;
2274 old_itable_size = fs->inode_blocks_per_group * fs->blocksize;
2275 old_ino_size = EXT2_INODE_SIZE(fs->super);
2277 new_ino_blks_per_grp = ext2fs_div_ceil(
2278 EXT2_INODES_PER_GROUP(fs->super) *
2282 new_itable_size = new_ino_blks_per_grp * fs->blocksize;
2284 retval = ext2fs_get_mem(old_itable_size, &old_itable);
2288 retval = ext2fs_get_mem(new_itable_size, &new_itable);
2292 tmp_old_itable = old_itable;
2293 tmp_new_itable = new_itable;
2295 for (i = 0; i < fs->group_desc_count; i++) {
2296 blk = ext2fs_inode_table_loc(fs, i);
2297 retval = io_channel_read_blk64(fs->io, blk,
2298 fs->inode_blocks_per_group, old_itable);
2302 for (j = 0; j < EXT2_INODES_PER_GROUP(fs->super); j++) {
2303 memcpy(new_itable, old_itable, old_ino_size);
2305 memset(new_itable+old_ino_size, 0,
2306 new_ino_size - old_ino_size);
2308 new_itable += new_ino_size;
2309 old_itable += old_ino_size;
2312 /* reset the pointer */
2313 old_itable = tmp_old_itable;
2314 new_itable = tmp_new_itable;
2316 retval = io_channel_write_blk64(fs->io, blk,
2317 new_ino_blks_per_grp, new_itable);
2322 /* Update the meta data */
2323 fs->inode_blocks_per_group = new_ino_blks_per_grp;
2324 ext2fs_free_inode_cache(fs->icache);
2326 fs->super->s_inode_size = new_ino_size;
2330 ext2fs_free_mem(&old_itable);
2333 ext2fs_free_mem(&new_itable);
2338 static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs)
2342 unsigned int group = 0;
2343 unsigned int count = 0;
2348 * First calculate the block statistics
2350 for (blk = fs->super->s_first_data_block;
2351 blk < ext2fs_blocks_count(fs->super); blk++) {
2352 if (!ext2fs_fast_test_block_bitmap2(fs->block_map, blk)) {
2357 if ((count == fs->super->s_blocks_per_group) ||
2358 (blk == ext2fs_blocks_count(fs->super)-1)) {
2359 ext2fs_bg_free_blocks_count_set(fs, group++,
2365 total_free = EXT2FS_C2B(fs, total_free);
2366 ext2fs_free_blocks_count_set(fs->super, total_free);
2369 * Next, calculate the inode statistics
2376 /* Protect loop from wrap-around if s_inodes_count maxed */
2377 for (ino = 1; ino <= fs->super->s_inodes_count && ino > 0; ino++) {
2378 if (!ext2fs_fast_test_inode_bitmap2(fs->inode_map, ino)) {
2383 if ((count == fs->super->s_inodes_per_group) ||
2384 (ino == fs->super->s_inodes_count)) {
2385 ext2fs_bg_free_inodes_count_set(fs, group++,
2391 fs->super->s_free_inodes_count = total_free;
2392 ext2fs_mark_super_dirty(fs);
2396 #define list_for_each_safe(pos, pnext, head) \
2397 for (pos = (head)->next, pnext = pos->next; pos != (head); \
2398 pos = pnext, pnext = pos->next)
2400 static void free_blk_move_list(void)
2402 struct list_head *entry, *tmp;
2403 struct blk_move *bmv;
2405 list_for_each_safe(entry, tmp, &blk_move_list) {
2406 bmv = list_entry(entry, struct blk_move, list);
2408 ext2fs_free_mem(&bmv);
2413 static int resize_inode(ext2_filsys fs, unsigned long new_size)
2416 int new_ino_blks_per_grp;
2417 ext2fs_block_bitmap bmap;
2419 retval = ext2fs_read_inode_bitmap(fs);
2421 fputs(_("Failed to read inode bitmap\n"), stderr);
2424 retval = ext2fs_read_block_bitmap(fs);
2426 fputs(_("Failed to read block bitmap\n"), stderr);
2429 INIT_LIST_HEAD(&blk_move_list);
2432 new_ino_blks_per_grp = ext2fs_div_ceil(
2433 EXT2_INODES_PER_GROUP(fs->super)*
2437 /* We may change the file system.
2438 * Mark the file system as invalid so that
2439 * the user is prompted to run fsck.
2441 fs->super->s_state &= ~EXT2_VALID_FS;
2443 retval = ext2fs_allocate_block_bitmap(fs, _("blocks to be moved"),
2446 fputs(_("Failed to allocate block bitmap when "
2447 "increasing inode size\n"), stderr);
2450 retval = get_move_bitmaps(fs, new_ino_blks_per_grp, bmap);
2452 fputs(_("Not enough space to increase inode size \n"), stderr);
2455 retval = move_block(fs, bmap);
2457 fputs(_("Failed to relocate blocks during inode resize \n"),
2461 retval = inode_scan_and_fix(fs, bmap);
2465 retval = group_desc_scan_and_fix(fs, bmap);
2469 retval = expand_inode_table(fs, new_size);
2473 ext2fs_calculate_summary_stats(fs);
2475 fs->super->s_state |= EXT2_VALID_FS;
2476 /* mark super block and block bitmap as dirty */
2477 ext2fs_mark_super_dirty(fs);
2478 ext2fs_mark_bb_dirty(fs);
2481 free_blk_move_list();
2482 ext2fs_free_block_bitmap(bmap);
2487 free_blk_move_list();
2488 ext2fs_free_block_bitmap(bmap);
2489 fputs(_("Error in resizing the inode size.\n"
2490 "Run e2undo to undo the "
2491 "file system changes. \n"), stderr);
2496 static int tune2fs_setup_tdb(const char *name, io_manager *io_ptr)
2498 errcode_t retval = 0;
2499 const char *tdb_dir;
2500 char *tdb_file = NULL;
2501 char *dev_name, *tmp_name;
2503 /* (re)open a specific undo file */
2504 if (undo_file && undo_file[0] != 0) {
2505 retval = set_undo_io_backing_manager(*io_ptr);
2508 *io_ptr = undo_io_manager;
2509 retval = set_undo_io_backup_file(undo_file);
2512 printf(_("Overwriting existing filesystem; this can be undone "
2513 "using the command:\n"
2514 " e2undo %s %s\n\n"),
2520 * Configuration via a conf file would be
2523 tdb_dir = getenv("E2FSPROGS_UNDO_DIR");
2525 tdb_dir = "/var/lib/e2fsprogs";
2527 if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
2528 access(tdb_dir, W_OK))
2531 tmp_name = strdup(name);
2534 dev_name = basename(tmp_name);
2535 tdb_file = malloc(strlen(tdb_dir) + 9 + strlen(dev_name) + 7 + 1);
2540 sprintf(tdb_file, "%s/tune2fs-%s.e2undo", tdb_dir, dev_name);
2543 if ((unlink(tdb_file) < 0) && (errno != ENOENT)) {
2545 com_err(program_name, retval,
2546 _("while trying to delete %s"), tdb_file);
2550 retval = set_undo_io_backing_manager(*io_ptr);
2553 *io_ptr = undo_io_manager;
2554 retval = set_undo_io_backup_file(tdb_file);
2557 printf(_("Overwriting existing filesystem; this can be undone "
2558 "using the command:\n"
2559 " e2undo %s %s\n\n"),
2567 com_err("tune2fs", retval, "while trying to setup undo file\n");
2572 fs_update_journal_user(struct ext2_super_block *sb, __u8 old_uuid[UUID_SIZE])
2574 int retval, nr_users, start;
2575 journal_superblock_t *jsb;
2579 char uuid[UUID_STR_SIZE];
2580 char buf[SUPERBLOCK_SIZE] __attribute__ ((aligned(8)));
2582 if (!ext2fs_has_feature_journal(sb) || uuid_is_null(sb->s_journal_uuid))
2585 uuid_unparse(sb->s_journal_uuid, uuid);
2586 journal_path = blkid_get_devname(NULL, "UUID", uuid);
2590 retval = ext2fs_open2(journal_path, io_options,
2591 EXT2_FLAG_JOURNAL_DEV_OK | EXT2_FLAG_RW,
2592 0, 0, unix_io_manager, &jfs);
2594 com_err(program_name, retval,
2595 _("while trying to open %s"),
2600 retval = get_journal_sb(jfs, buf);
2602 if (retval == EXT2_ET_UNSUPP_FEATURE)
2603 fprintf(stderr, _("%s is not a journal device.\n"),
2608 jsb = (journal_superblock_t *) buf;
2609 /* Find the filesystem UUID */
2610 nr_users = ntohl(jsb->s_nr_users);
2612 j_uuid = journal_user(old_uuid, jsb->s_users, nr_users);
2613 if (j_uuid == NULL) {
2614 fputs(_("Filesystem's UUID not found on journal device.\n"),
2616 return EXT2_ET_LOAD_EXT_JOURNAL;
2619 memcpy(j_uuid, sb->s_uuid, UUID_SIZE);
2621 start = ext2fs_journal_sb_start(jfs->blocksize);
2622 /* Write back the journal superblock */
2623 retval = io_channel_write_blk64(jfs->io, start, -SUPERBLOCK_SIZE, buf);
2625 com_err(program_name, retval,
2626 "while writing journal superblock.");
2635 #ifndef BUILD_AS_LIB
2636 int main(int argc, char **argv)
2638 int tune2fs_main(int argc, char **argv)
2639 #endif /* BUILD_AS_LIB */
2643 struct ext2_super_block *sb;
2644 io_manager io_ptr, io_ptr_orig = NULL;
2646 char default_undo_file[1] = { 0 };
2649 setlocale(LC_MESSAGES, "");
2650 setlocale(LC_CTYPE, "");
2651 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
2652 textdomain(NLS_CAT_NAME);
2653 set_com_err_gettext(gettext);
2656 program_name = *argv;
2657 add_error_table(&et_ext2_error_table);
2659 #ifdef CONFIG_BUILD_FINDFS
2660 if (strcmp(get_progname(argv[0]), "findfs") == 0)
2661 do_findfs(argc, argv);
2663 if (strcmp(get_progname(argv[0]), "e2label") == 0)
2664 parse_e2label_options(argc, argv);
2666 parse_tune2fs_options(argc, argv);
2668 #ifdef CONFIG_TESTIO_DEBUG
2669 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_DEBUG")) {
2670 io_ptr = test_io_manager;
2671 test_io_backing_manager = unix_io_manager;
2674 io_ptr = unix_io_manager;
2677 if ((open_flag & EXT2_FLAG_RW) == 0 || f_flag)
2678 open_flag |= EXT2_FLAG_SKIP_MMP;
2680 open_flag |= EXT2_FLAG_64BITS | EXT2_FLAG_JOURNAL_DEV_OK;
2682 /* keep the filesystem struct around to dump MMP data */
2683 open_flag |= EXT2_FLAG_NOFREE_ON_ERROR;
2685 retval = ext2fs_open2(device_name, io_options, open_flag,
2688 com_err(program_name, retval,
2689 _("while trying to open %s"),
2691 if (retval == EXT2_ET_MMP_FSCK_ON ||
2692 retval == EXT2_ET_MMP_UNKNOWN_SEQ)
2693 dump_mmp_msg(fs->mmp_buf,
2694 _("If you are sure the filesystem "
2695 "is not in use on any node, run:\n"
2696 "'tune2fs -f -E clear_mmp {device}'\n"));
2697 else if (retval == EXT2_ET_MMP_FAILED)
2698 dump_mmp_msg(fs->mmp_buf, NULL);
2699 else if (retval == EXT2_ET_MMP_MAGIC_INVALID)
2701 _("MMP block magic is bad. Try to fix it by "
2702 "running:\n'e2fsck -f %s'\n"), device_name);
2703 else if (retval == EXT2_ET_BAD_MAGIC)
2704 check_plausibility(device_name, CHECK_FS_EXIST, NULL);
2705 else if (retval != EXT2_ET_MMP_FAILED)
2706 fprintf(stderr, "%s",
2707 _("Couldn't find valid filesystem superblock.\n"));
2712 if (ext2fs_has_feature_journal_dev(fs->super)) {
2713 fprintf(stderr, "%s", _("Cannot modify a journal device.\n"));
2717 fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE;
2721 * Check the inode size is right so we can issue an
2722 * error message and bail before setting up the tdb
2725 if (new_inode_size == EXT2_INODE_SIZE(fs->super)) {
2726 fprintf(stderr, _("The inode size is already %lu\n"),
2731 if (new_inode_size < EXT2_INODE_SIZE(fs->super)) {
2732 fprintf(stderr, "%s",
2733 _("Shrinking inode size is not supported\n"));
2737 if (new_inode_size > fs->blocksize) {
2738 fprintf(stderr, _("Invalid inode size %lu (max %d)\n"),
2739 new_inode_size, fs->blocksize);
2744 * If inode resize is requested use the
2747 undo_file = default_undo_file;
2750 /* Set up an undo file */
2751 if (undo_file && io_ptr_orig == NULL) {
2752 io_ptr_orig = io_ptr;
2753 retval = tune2fs_setup_tdb(device_name, &io_ptr);
2758 if (io_ptr != io_ptr_orig) {
2759 ext2fs_close_free(&fs);
2765 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
2768 /* For e2label emulation */
2769 printf("%.*s\n", (int) sizeof(sb->s_volume_name),
2771 remove_error_table(&et_ext2_error_table);
2775 retval = ext2fs_check_if_mounted(device_name, &mount_flags);
2777 com_err("ext2fs_check_if_mount", retval,
2778 _("while determining whether %s is mounted."),
2783 /* Normally we only need to write out the superblock */
2784 fs->flags |= EXT2_FLAG_SUPER_ONLY;
2787 sb->s_max_mnt_count = max_mount_count;
2788 ext2fs_mark_super_dirty(fs);
2789 printf(_("Setting maximal mount count to %d\n"),
2793 sb->s_mnt_count = mount_count;
2794 ext2fs_mark_super_dirty(fs);
2795 printf(_("Setting current mount count to %d\n"), mount_count);
2798 sb->s_errors = errors;
2799 ext2fs_mark_super_dirty(fs);
2800 printf(_("Setting error behavior to %d\n"), errors);
2803 sb->s_def_resgid = resgid;
2804 ext2fs_mark_super_dirty(fs);
2805 printf(_("Setting reserved blocks gid to %lu\n"), resgid);
2808 if ((unsigned long long)interval >= (1ULL << 32)) {
2809 com_err(program_name, 0,
2810 _("interval between checks is too big (%lu)"),
2815 sb->s_checkinterval = interval;
2816 ext2fs_mark_super_dirty(fs);
2817 printf(_("Setting interval between checks to %lu seconds\n"),
2821 ext2fs_r_blocks_count_set(sb, reserved_ratio *
2822 ext2fs_blocks_count(sb) / 100.0);
2823 ext2fs_mark_super_dirty(fs);
2824 printf (_("Setting reserved blocks percentage to %g%% (%llu blocks)\n"),
2825 reserved_ratio, ext2fs_r_blocks_count(sb));
2828 if (reserved_blocks > ext2fs_blocks_count(sb)/2) {
2829 com_err(program_name, 0,
2830 _("reserved blocks count is too big (%llu)"),
2835 ext2fs_r_blocks_count_set(sb, reserved_blocks);
2836 ext2fs_mark_super_dirty(fs);
2837 printf(_("Setting reserved blocks count to %llu\n"),
2841 if (ext2fs_has_feature_sparse_super(sb)) {
2842 fputs(_("\nThe filesystem already has sparse "
2843 "superblocks.\n"), stderr);
2844 } else if (ext2fs_has_feature_meta_bg(sb)) {
2845 fputs(_("\nSetting the sparse superblock flag not "
2846 "supported\nfor filesystems with "
2847 "the meta_bg feature enabled.\n"),
2852 ext2fs_set_feature_sparse_super(sb);
2853 sb->s_state &= ~EXT2_VALID_FS;
2854 ext2fs_mark_super_dirty(fs);
2855 printf(_("\nSparse superblock flag set. %s"),
2860 fputs(_("\nClearing the sparse superblock flag not supported.\n"),
2866 sb->s_lastcheck = last_check_time;
2867 ext2fs_mark_super_dirty(fs);
2868 printf(_("Setting time filesystem last checked to %s\n"),
2869 ctime(&last_check_time));
2872 sb->s_def_resuid = resuid;
2873 ext2fs_mark_super_dirty(fs);
2874 printf(_("Setting reserved blocks uid to %lu\n"), resuid);
2877 if (strlen(new_label) > sizeof(sb->s_volume_name))
2878 fputs(_("Warning: label too long, truncating.\n"),
2880 memset(sb->s_volume_name, 0, sizeof(sb->s_volume_name));
2881 strncpy(sb->s_volume_name, new_label,
2882 sizeof(sb->s_volume_name));
2883 ext2fs_mark_super_dirty(fs);
2886 memset(sb->s_last_mounted, 0, sizeof(sb->s_last_mounted));
2887 strncpy(sb->s_last_mounted, new_last_mounted,
2888 sizeof(sb->s_last_mounted));
2889 ext2fs_mark_super_dirty(fs);
2892 rc = update_mntopts(fs, mntopts_cmd);
2897 rc = update_feature_set(fs, features_cmd);
2902 rc = parse_extended_opts(fs, extended_cmd);
2905 if (clear_mmp && !f_flag) {
2906 fputs(_("Error in using clear_mmp. "
2907 "It must be used with -f\n"),
2913 rc = ext2fs_mmp_clear(fs);
2916 if (journal_size || journal_device) {
2917 rc = add_journal(fs);
2923 if (mount_flags & EXT2_MF_MOUNTED) {
2924 fputs(_("The quota feature may only be changed when "
2925 "the filesystem is unmounted.\n"), stderr);
2929 handle_quota_options(fs);
2935 char buf[SUPERBLOCK_SIZE] __attribute__ ((aligned(8)));
2936 __u8 old_uuid[UUID_SIZE];
2938 if (ext2fs_has_group_desc_csum(fs)) {
2940 * Changing the UUID requires rewriting all metadata,
2941 * which can race with a mounted fs. Don't allow that.
2943 if ((mount_flags & EXT2_MF_MOUNTED) && !f_flag) {
2944 fputs(_("The UUID may only be "
2945 "changed when the filesystem is "
2946 "unmounted.\n"), stderr);
2949 if (check_fsck_needed(fs))
2953 * Determine if the block group checksums are
2954 * correct so we know whether or not to set
2957 for (i = 0; i < fs->group_desc_count; i++)
2958 if (!ext2fs_group_desc_csum_verify(fs, i))
2960 if (i >= fs->group_desc_count)
2964 memcpy(old_uuid, sb->s_uuid, UUID_SIZE);
2965 if ((strcasecmp(new_UUID, "null") == 0) ||
2966 (strcasecmp(new_UUID, "clear") == 0)) {
2967 uuid_clear(sb->s_uuid);
2968 } else if (strcasecmp(new_UUID, "time") == 0) {
2969 uuid_generate_time(sb->s_uuid);
2970 } else if (strcasecmp(new_UUID, "random") == 0) {
2971 uuid_generate(sb->s_uuid);
2972 } else if (uuid_parse(new_UUID, sb->s_uuid)) {
2973 com_err(program_name, 0, "%s",
2974 _("Invalid UUID format\n"));
2978 ext2fs_init_csum_seed(fs);
2980 for (i = 0; i < fs->group_desc_count; i++)
2981 ext2fs_group_desc_csum_set(fs, i);
2982 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
2985 /* If this is a journal dev, we need to copy UUID into jsb */
2986 if (!(rc = get_journal_sb(fs, buf))) {
2987 journal_superblock_t *jsb;
2989 jsb = (journal_superblock_t *) buf;
2990 fputs(_("Need to update journal superblock.\n"), stdout);
2991 memcpy(jsb->s_uuid, sb->s_uuid, sizeof(sb->s_uuid));
2993 /* Writeback the journal superblock */
2994 if ((rc = io_channel_write_blk64(fs->io,
2995 ext2fs_journal_sb_start(fs->blocksize),
2996 -SUPERBLOCK_SIZE, buf)))
2998 } else if (rc != EXT2_ET_UNSUPP_FEATURE)
3001 rc = 0; /** Reset rc to avoid ext2fs_mmp_stop() */
3003 if ((rc = fs_update_journal_user(sb, old_uuid)))
3007 ext2fs_mark_super_dirty(fs);
3008 if (ext2fs_has_feature_metadata_csum(fs->super))
3009 rewrite_checksums = 1;
3013 if (mount_flags & EXT2_MF_MOUNTED) {
3014 fputs(_("The inode size may only be "
3015 "changed when the filesystem is "
3016 "unmounted.\n"), stderr);
3020 if (ext2fs_has_feature_flex_bg(fs->super)) {
3021 fputs(_("Changing the inode size not supported for "
3022 "filesystems with the flex_bg\n"
3023 "feature enabled.\n"),
3029 * We want to update group descriptor also
3030 * with the new free inode count
3032 if (rewrite_checksums)
3033 fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
3034 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
3035 retval = resize_inode(fs, new_inode_size);
3036 if (rewrite_checksums)
3037 fs->flags &= ~EXT2_FLAG_IGNORE_CSUM_ERRORS;
3039 printf(_("Setting inode size %lu\n"),
3041 rewrite_checksums = 1;
3043 printf("%s", _("Failed to change inode size\n"));
3049 if (rewrite_checksums)
3050 rewrite_metadata_checksums(fs);
3055 sb->s_raid_stride = stride;
3056 ext2fs_mark_super_dirty(fs);
3057 printf(_("Setting stride size to %d\n"), stride);
3059 if (stripe_width_set) {
3060 sb->s_raid_stripe_width = stripe_width;
3061 ext2fs_mark_super_dirty(fs);
3062 printf(_("Setting stripe width to %d\n"), stripe_width);
3064 if (ext_mount_opts) {
3065 strncpy((char *)(fs->super->s_mount_opts), ext_mount_opts,
3066 sizeof(fs->super->s_mount_opts));
3067 fs->super->s_mount_opts[sizeof(fs->super->s_mount_opts)-1] = 0;
3068 ext2fs_mark_super_dirty(fs);
3069 printf(_("Setting extended default mount options to '%s'\n"),
3071 free(ext_mount_opts);
3074 remove_error_table(&et_ext2_error_table);
3078 ext2fs_mmp_stop(fs);
3079 #ifndef BUILD_AS_LIB
3084 convert_64bit(fs, feature_64bit);
3085 return (ext2fs_close_free(&fs) ? 1 : 0);