2 * tune2fs.c - Change the file system parameters on an ext2 file system
4 * Copyright (C) 1992, 1993, 1994 Remy Card <card@masi.ibp.fr>
5 * Laboratoire MASI, Institut Blaise Pascal
6 * Universite Pierre et Marie Curie (Paris VI)
8 * Copyright 1995, 1996, 1997, 1998, 1999, 2000 by Theodore Ts'o.
11 * This file may be redistributed under the terms of the GNU Public
19 * 93/10/31 - Added the -c option to change the maximal mount counts
20 * 93/12/14 - Added -l flag to list contents of superblock
21 * M.J.E. Mol (marcel@duteca.et.tudelft.nl)
22 * F.W. ten Wolde (franky@duteca.et.tudelft.nl)
23 * 93/12/29 - Added the -e option to change errors behavior
24 * 94/02/27 - Ported to use the ext2fs library
25 * 94/03/06 - Added the checks interval from Uwe Ohse (uwe@tirka.gun.de)
28 #define _XOPEN_SOURCE 600 /* for inclusion of strptime() */
44 #include <strings.h> /* for strcasecmp() */
46 #define _BSD_SOURCE /* for inclusion of strcasecmp() via <string.h> */
51 #include <sys/types.h>
55 #include "ext2fs/ext2_fs.h"
56 #include "ext2fs/ext2fs.h"
57 #include "et/com_err.h"
58 #include "uuid/uuid.h"
62 #include "plausible.h"
63 #include "blkid/blkid.h"
64 #include "quota/quotaio.h"
66 #include "../version.h"
67 #include "nls-enable.h"
69 #define QOPT_ENABLE (1)
70 #define QOPT_DISABLE (-1)
72 extern int ask_yn(const char *string, int def);
74 const char *program_name = "tune2fs";
76 char *new_label, *new_last_mounted, *new_UUID;
78 static int c_flag, C_flag, e_flag, f_flag, g_flag, i_flag, l_flag, L_flag;
79 static int m_flag, M_flag, Q_flag, r_flag, s_flag = -1, u_flag, U_flag, T_flag;
82 static time_t last_check_time;
83 static int print_label;
84 static int max_mount_count, mount_count, mount_flags;
85 static unsigned long interval;
86 static blk64_t reserved_blocks;
87 static double reserved_ratio;
88 static unsigned long resgid, resuid;
89 static unsigned short errors;
91 static char *features_cmd;
92 static char *mntopts_cmd;
93 static int stride, stripe_width;
94 static int stride_set, stripe_width_set;
95 static char *extended_cmd;
96 static unsigned long new_inode_size;
97 static char *ext_mount_opts;
98 static int usrquota, grpquota;
99 static int rewrite_checksums;
100 static int feature_64bit;
101 static int fsck_requested;
103 int journal_size, journal_flags;
104 char *journal_device;
105 static blk64_t journal_location = ~0LL;
107 static struct list_head blk_move_list;
110 struct list_head list;
116 static const char *please_fsck = N_("Please run e2fsck on the filesystem.\n");
117 static const char *please_dir_fsck =
118 N_("Please run e2fsck -D on the filesystem.\n");
120 #ifdef CONFIG_BUILD_FINDFS
121 void do_findfs(int argc, char **argv);
124 static void usage(void)
127 _("Usage: %s [-c max_mounts_count] [-e errors_behavior] "
129 "\t[-i interval[d|m|w]] [-j] [-J journal_options] [-l]\n"
130 "\t[-m reserved_blocks_percent] "
131 "[-o [^]mount_options[,...]] [-p mmp_update_interval]\n"
132 "\t[-r reserved_blocks_count] [-u user] [-C mount_count] "
133 "[-L volume_label]\n"
134 "\t[-M last_mounted_dir] [-O [^]feature[,...]]\n"
136 "\t[-Q quota_options]\n"
138 "\t[-E extended-option[,...]] [-T last_check_time] "
139 "[-U UUID]\n\t[ -I new_inode_size ] device\n"), program_name);
143 static __u32 ok_features[3] = {
145 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
146 EXT2_FEATURE_COMPAT_DIR_INDEX,
148 EXT2_FEATURE_INCOMPAT_FILETYPE |
149 EXT3_FEATURE_INCOMPAT_EXTENTS |
150 EXT4_FEATURE_INCOMPAT_FLEX_BG |
151 EXT4_FEATURE_INCOMPAT_MMP |
152 EXT4_FEATURE_INCOMPAT_64BIT |
153 EXT4_FEATURE_INCOMPAT_ENCRYPT,
155 EXT2_FEATURE_RO_COMPAT_LARGE_FILE |
156 EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
157 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
158 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
159 EXT4_FEATURE_RO_COMPAT_GDT_CSUM |
160 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER |
162 EXT4_FEATURE_RO_COMPAT_QUOTA |
164 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM |
165 EXT4_FEATURE_RO_COMPAT_READONLY
168 static __u32 clear_ok_features[3] = {
170 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
171 EXT2_FEATURE_COMPAT_RESIZE_INODE |
172 EXT2_FEATURE_COMPAT_DIR_INDEX,
174 EXT2_FEATURE_INCOMPAT_FILETYPE |
175 EXT4_FEATURE_INCOMPAT_FLEX_BG |
176 EXT4_FEATURE_INCOMPAT_MMP |
177 EXT4_FEATURE_INCOMPAT_64BIT,
179 EXT2_FEATURE_RO_COMPAT_LARGE_FILE |
180 EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
181 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
182 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
183 EXT4_FEATURE_RO_COMPAT_GDT_CSUM |
185 EXT4_FEATURE_RO_COMPAT_QUOTA |
187 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM |
188 EXT4_FEATURE_RO_COMPAT_READONLY
192 * Try to get journal super block if any
194 static int get_journal_sb(ext2_filsys jfs, char buf[SUPERBLOCK_SIZE])
197 journal_superblock_t *jsb;
199 if (!(jfs->super->s_feature_incompat &
200 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
201 return EXT2_ET_UNSUPP_FEATURE;
204 /* Get the journal superblock */
205 if ((retval = io_channel_read_blk64(jfs->io,
206 ext2fs_journal_sb_start(jfs->blocksize), -SUPERBLOCK_SIZE, buf))) {
207 com_err(program_name, retval, "%s",
208 _("while reading journal superblock"));
212 jsb = (journal_superblock_t *) buf;
213 if ((jsb->s_header.h_magic != (unsigned)ntohl(JFS_MAGIC_NUMBER)) ||
214 (jsb->s_header.h_blocktype != (unsigned)ntohl(JFS_SUPERBLOCK_V2))) {
215 fputs(_("Journal superblock not found!\n"), stderr);
216 return EXT2_ET_BAD_MAGIC;
222 static __u8 *journal_user(__u8 uuid[UUID_SIZE], __u8 s_users[JFS_USERS_SIZE],
226 for (i = 0; i < nr_users; i++) {
227 if (memcmp(uuid, &s_users[i * UUID_SIZE], UUID_SIZE) == 0)
228 return &s_users[i * UUID_SIZE];
235 * Remove an external journal from the filesystem
237 static int remove_journal_device(ext2_filsys fs)
241 char buf[SUPERBLOCK_SIZE];
242 journal_superblock_t *jsb;
245 int commit_remove_journal = 0;
249 commit_remove_journal = 1; /* force removal even if error */
251 uuid_unparse(fs->super->s_journal_uuid, buf);
252 journal_path = blkid_get_devname(NULL, "UUID", buf);
256 ext2fs_find_block_device(fs->super->s_journal_dev);
258 goto no_valid_journal;
261 #ifdef CONFIG_TESTIO_DEBUG
262 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
263 io_ptr = test_io_manager;
264 test_io_backing_manager = unix_io_manager;
267 io_ptr = unix_io_manager;
268 retval = ext2fs_open(journal_path, EXT2_FLAG_RW|
269 EXT2_FLAG_JOURNAL_DEV_OK, 0,
270 fs->blocksize, io_ptr, &jfs);
272 com_err(program_name, retval, "%s",
273 _("while trying to open external journal"));
274 goto no_valid_journal;
277 if ((retval = get_journal_sb(jfs, buf))) {
278 if (retval == EXT2_ET_UNSUPP_FEATURE)
279 fprintf(stderr, _("%s is not a journal device.\n"),
281 goto no_valid_journal;
284 jsb = (journal_superblock_t *) buf;
285 /* Find the filesystem UUID */
286 nr_users = ntohl(jsb->s_nr_users);
288 if (!journal_user(fs->super->s_uuid, jsb->s_users, nr_users)) {
289 fputs(_("Filesystem's UUID not found on journal device.\n"),
291 commit_remove_journal = 1;
292 goto no_valid_journal;
295 for (i = 0; i < nr_users; i++)
296 memcpy(&jsb->s_users[i * 16], &jsb->s_users[(i + 1) * 16], 16);
297 jsb->s_nr_users = htonl(nr_users);
299 /* Write back the journal superblock */
300 retval = io_channel_write_blk64(jfs->io,
301 ext2fs_journal_sb_start(fs->blocksize),
302 -SUPERBLOCK_SIZE, buf);
304 com_err(program_name, retval,
305 "while writing journal superblock.");
306 goto no_valid_journal;
309 commit_remove_journal = 1;
312 if (commit_remove_journal == 0) {
313 fputs(_("Cannot locate journal device. It was NOT removed\n"
314 "Use -f option to remove missing journal device.\n"),
318 fs->super->s_journal_dev = 0;
319 memset(fs->super->s_jnl_blocks, 0, sizeof(fs->super->s_jnl_blocks));
320 uuid_clear(fs->super->s_journal_uuid);
321 ext2fs_mark_super_dirty(fs);
322 fputs(_("Journal removed\n"), stdout);
328 /* Helper function for remove_journal_inode */
329 static int release_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
330 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
331 blk64_t ref_block EXT2FS_ATTR((unused)),
332 int ref_offset EXT2FS_ATTR((unused)),
333 void *private EXT2FS_ATTR((unused)))
339 ext2fs_unmark_block_bitmap2(fs->block_map, block);
340 group = ext2fs_group_of_blk2(fs, block);
341 ext2fs_bg_free_blocks_count_set(fs, group, ext2fs_bg_free_blocks_count(fs, group) + 1);
342 ext2fs_group_desc_csum_set(fs, group);
343 ext2fs_free_blocks_count_add(fs->super, EXT2FS_CLUSTER_RATIO(fs));
348 * Remove the journal inode from the filesystem
350 static errcode_t remove_journal_inode(ext2_filsys fs)
352 struct ext2_inode inode;
354 ino_t ino = fs->super->s_journal_inum;
356 retval = ext2fs_read_inode(fs, ino, &inode);
358 com_err(program_name, retval, "%s",
359 _("while reading journal inode"));
362 if (ino == EXT2_JOURNAL_INO) {
363 retval = ext2fs_read_bitmaps(fs);
365 com_err(program_name, retval, "%s",
366 _("while reading bitmaps"));
369 retval = ext2fs_block_iterate3(fs, ino,
370 BLOCK_FLAG_READ_ONLY, NULL,
371 release_blocks_proc, NULL);
373 com_err(program_name, retval, "%s",
374 _("while clearing journal inode"));
377 memset(&inode, 0, sizeof(inode));
378 ext2fs_mark_bb_dirty(fs);
379 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
381 inode.i_flags &= ~EXT2_IMMUTABLE_FL;
382 retval = ext2fs_write_inode(fs, ino, &inode);
384 com_err(program_name, retval, "%s",
385 _("while writing journal inode"));
388 fs->super->s_journal_inum = 0;
389 memset(fs->super->s_jnl_blocks, 0, sizeof(fs->super->s_jnl_blocks));
390 ext2fs_mark_super_dirty(fs);
396 * Update the default mount options
398 static int update_mntopts(ext2_filsys fs, char *mntopts)
400 struct ext2_super_block *sb = fs->super;
402 if (e2p_edit_mntopts(mntopts, &sb->s_default_mount_opts, ~0)) {
403 fprintf(stderr, _("Invalid mount option set: %s\n"),
407 ext2fs_mark_super_dirty(fs);
412 static int check_fsck_needed(ext2_filsys fs)
414 if (fs->super->s_state & EXT2_VALID_FS)
416 printf("\n%s\n", _(please_fsck));
417 if (mount_flags & EXT2_MF_READONLY)
418 printf("%s", _("(and reboot afterwards!)\n"));
422 static void request_dir_fsck_afterwards(ext2_filsys fs)
424 static int requested;
429 fs->super->s_state &= ~EXT2_VALID_FS;
430 printf("\n%s\n", _(please_dir_fsck));
431 if (mount_flags & EXT2_MF_READONLY)
432 printf("%s", _("(and reboot afterwards!)\n"));
435 static void request_fsck_afterwards(ext2_filsys fs)
437 static int requested = 0;
442 fs->super->s_state &= ~EXT2_VALID_FS;
443 printf("\n%s\n", _(please_fsck));
444 if (mount_flags & EXT2_MF_READONLY)
445 printf("%s", _("(and reboot afterwards!)\n"));
448 static void convert_64bit(ext2_filsys fs, int direction)
454 * Is resize2fs going to demand a fsck run? Might as well tell the
457 if (!fsck_requested &&
458 ((fs->super->s_state & EXT2_ERROR_FS) ||
459 !(fs->super->s_state & EXT2_VALID_FS) ||
460 fs->super->s_lastcheck < fs->super->s_mtime))
461 request_fsck_afterwards(fs);
463 fprintf(stderr, _("After running e2fsck, please run `resize2fs %s %s"),
464 direction > 0 ? "-b" : "-s", fs->device_name);
466 fprintf(stderr, _("Please run `resize2fs %s %s"),
467 direction > 0 ? "-b" : "-s", fs->device_name);
470 fprintf(stderr, _("' to enable 64-bit mode.\n"));
472 fprintf(stderr, _("' to disable 64-bit mode.\n"));
475 /* Rewrite extents */
476 static errcode_t rewrite_extents(ext2_filsys fs, ext2_ino_t ino,
477 struct ext2_inode *inode)
479 ext2_extent_handle_t handle;
480 struct ext2fs_extent extent;
482 struct ext2_extent_info info;
484 if (!(inode->i_flags & EXT4_EXTENTS_FL) ||
485 !EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
486 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
489 errcode = ext2fs_extent_open(fs, ino, &handle);
493 errcode = ext2fs_extent_get(handle, EXT2_EXTENT_ROOT, &extent);
498 errcode = ext2fs_extent_get_info(handle, &info);
503 * If this is the first extent in an extent block that we
504 * haven't visited, rewrite the extent to force the ETB
505 * checksum to be rewritten.
507 if (info.curr_entry == 1 && info.curr_level != 0 &&
508 !(extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT)) {
509 errcode = ext2fs_extent_replace(handle, 0, &extent);
514 /* Skip to the end of a block of leaf nodes */
515 if (extent.e_flags & EXT2_EXTENT_FLAGS_LEAF) {
516 errcode = ext2fs_extent_get(handle,
517 EXT2_EXTENT_LAST_SIB,
523 errcode = ext2fs_extent_get(handle, EXT2_EXTENT_NEXT, &extent);
524 } while (errcode == 0);
527 /* Ok if we run off the end */
528 if (errcode == EXT2_ET_EXTENT_NO_NEXT)
530 ext2fs_extent_free(handle);
535 * Rewrite directory blocks with checksums
537 struct rewrite_dir_context {
544 static int rewrite_dir_block(ext2_filsys fs,
546 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
547 blk64_t ref_block EXT2FS_ATTR((unused)),
548 int ref_offset EXT2FS_ATTR((unused)),
551 struct ext2_dx_countlimit *dcl = NULL;
552 struct rewrite_dir_context *ctx = priv_data;
553 int dcl_offset, changed = 0;
555 ctx->errcode = ext2fs_read_dir_block4(fs, *blocknr, ctx->buf, 0,
560 /* if htree node... */
562 ext2fs_get_dx_countlimit(fs, (struct ext2_dir_entry *)ctx->buf,
565 if (!EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
566 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
567 /* Ensure limit is the max size */
568 int max_entries = (fs->blocksize - dcl_offset) /
569 sizeof(struct ext2_dx_entry);
570 if (ext2fs_le16_to_cpu(dcl->limit) != max_entries) {
572 dcl->limit = ext2fs_cpu_to_le16(max_entries);
575 /* If htree block is full then rebuild the dir */
576 if (ext2fs_le16_to_cpu(dcl->count) ==
577 ext2fs_le16_to_cpu(dcl->limit)) {
578 request_dir_fsck_afterwards(fs);
582 * Ensure dcl->limit is small enough to leave room for
585 int max_entries = (fs->blocksize - (dcl_offset +
586 sizeof(struct ext2_dx_tail))) /
587 sizeof(struct ext2_dx_entry);
588 if (ext2fs_le16_to_cpu(dcl->limit) != max_entries)
589 dcl->limit = ext2fs_cpu_to_le16(max_entries);
590 /* Always rewrite checksum */
594 unsigned int rec_len, name_size;
595 char *top = ctx->buf + fs->blocksize;
596 struct ext2_dir_entry *de = (struct ext2_dir_entry *)ctx->buf;
597 struct ext2_dir_entry *last_de = NULL, *penultimate_de = NULL;
599 /* Find last and penultimate dirent */
600 while ((char *)de < top) {
601 penultimate_de = last_de;
603 ctx->errcode = ext2fs_get_rec_len(fs, de, &rec_len);
604 if (!ctx->errcode && !rec_len)
605 ctx->errcode = EXT2_ET_DIR_CORRUPTED;
608 de = (struct ext2_dir_entry *)(((char *)de) + rec_len);
610 ctx->errcode = ext2fs_get_rec_len(fs, last_de, &rec_len);
613 name_size = ext2fs_dirent_name_len(last_de);
615 if (!EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
616 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
619 if (last_de->inode ||
621 rec_len != sizeof(struct ext2_dir_entry_tail))
624 * The last dirent is unused and the right length to
625 * have stored a checksum. Erase it.
627 ctx->errcode = ext2fs_get_rec_len(fs, penultimate_de,
630 ctx->errcode = EXT2_ET_DIR_CORRUPTED;
633 ext2fs_set_rec_len(fs, rec_len +
634 sizeof(struct ext2_dir_entry_tail),
638 unsigned csum_size = sizeof(struct ext2_dir_entry_tail);
639 struct ext2_dir_entry_tail *t;
642 * If the last dirent looks like the tail, just update
645 if (!last_de->inode &&
646 rec_len == csum_size) {
647 t = (struct ext2_dir_entry_tail *)last_de;
648 t->det_reserved_name_len =
649 EXT2_DIR_NAME_LEN_CSUM;
654 name_size = (name_size & ~3) + 4;
655 /* If there's not enough space for the tail, e2fsck */
656 if (rec_len <= (8 + name_size + csum_size)) {
657 request_dir_fsck_afterwards(fs);
660 /* Shorten that last de and insert the tail */
661 ext2fs_set_rec_len(fs, rec_len - csum_size, last_de);
662 t = EXT2_DIRENT_TAIL(ctx->buf, fs->blocksize);
663 ext2fs_initialize_dirent_tail(fs, t);
665 /* Always update checksum */
674 ctx->errcode = ext2fs_write_dir_block4(fs, *blocknr, ctx->buf,
682 static errcode_t rewrite_directory(ext2_filsys fs, ext2_ino_t dir,
683 struct ext2_inode *inode)
686 struct rewrite_dir_context ctx;
688 retval = ext2fs_get_mem(fs->blocksize, &ctx.buf);
692 ctx.is_htree = (inode->i_flags & EXT2_INDEX_FL);
695 retval = ext2fs_block_iterate3(fs, dir, BLOCK_FLAG_READ_ONLY |
696 BLOCK_FLAG_DATA_ONLY,
697 0, rewrite_dir_block, &ctx);
699 ext2fs_free_mem(&ctx.buf);
707 * Forcibly set checksums in all inodes.
709 static void rewrite_inodes(ext2_filsys fs)
711 int length = EXT2_INODE_SIZE(fs->super);
712 struct ext2_inode *inode, *zero;
714 ext2_inode_scan scan;
717 blk64_t file_acl_block;
720 if (fs->super->s_creator_os != EXT2_OS_LINUX)
723 retval = ext2fs_open_inode_scan(fs, 0, &scan);
725 com_err("set_csum", retval, "while opening inode scan");
729 retval = ext2fs_get_mem(length, &inode);
731 com_err("set_csum", retval, "while allocating memory");
735 retval = ext2fs_get_memzero(length, &zero);
737 com_err("set_csum", retval, "while allocating memory");
741 retval = ext2fs_get_mem(fs->blocksize, &ea_buf);
743 com_err("set_csum", retval, "while allocating memory");
748 retval = ext2fs_get_next_inode_full(scan, &ino, inode, length);
750 com_err("set_csum", retval, "while getting next inode");
755 if (ext2fs_test_inode_bitmap2(fs->inode_map, ino)) {
758 if (memcmp(inode, zero, length) != 0) {
759 memset(inode, 0, length);
767 retval = ext2fs_write_inode_full(fs, ino, inode,
770 com_err("set_csum", retval, "while writing "
776 retval = rewrite_extents(fs, ino, inode);
778 com_err("rewrite_extents", retval,
779 "while rewriting extents");
783 if (LINUX_S_ISDIR(inode->i_mode) &&
784 ext2fs_inode_has_valid_blocks2(fs, inode)) {
785 retval = rewrite_directory(fs, ino, inode);
787 com_err("rewrite_directory", retval,
788 "while rewriting directories");
793 file_acl_block = ext2fs_file_acl_block(fs, inode);
796 retval = ext2fs_read_ext_attr3(fs, file_acl_block, ea_buf, ino);
798 com_err("rewrite_eablock", retval,
799 "while rewriting extended attribute");
802 retval = ext2fs_write_ext_attr3(fs, file_acl_block, ea_buf,
805 com_err("rewrite_eablock", retval,
806 "while rewriting extended attribute");
811 ext2fs_free_mem(&zero);
812 ext2fs_free_mem(&inode);
813 ext2fs_free_mem(&ea_buf);
814 ext2fs_close_inode_scan(scan);
817 static void rewrite_metadata_checksums(ext2_filsys fs)
822 fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
823 ext2fs_init_csum_seed(fs);
824 for (i = 0; i < fs->group_desc_count; i++)
825 ext2fs_group_desc_csum_set(fs, i);
826 retval = ext2fs_read_bitmaps(fs);
828 com_err("rewrite_metadata_checksums", retval,
829 "while reading bitmaps");
833 ext2fs_mark_ib_dirty(fs);
834 ext2fs_mark_bb_dirty(fs);
835 ext2fs_mmp_update2(fs, 1);
836 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
837 fs->flags &= ~EXT2_FLAG_IGNORE_CSUM_ERRORS;
838 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
839 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
840 fs->super->s_checksum_type = EXT2_CRC32C_CHKSUM;
842 fs->super->s_checksum_type = 0;
843 ext2fs_mark_super_dirty(fs);
846 static void enable_uninit_bg(ext2_filsys fs)
848 struct ext2_group_desc *gd;
851 for (i = 0; i < fs->group_desc_count; i++) {
852 gd = ext2fs_group_desc(fs, fs->group_desc, i);
853 gd->bg_itable_unused = 0;
854 gd->bg_flags = EXT2_BG_INODE_ZEROED;
855 ext2fs_group_desc_csum_set(fs, i);
857 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
860 static errcode_t zero_empty_inodes(ext2_filsys fs)
862 int length = EXT2_INODE_SIZE(fs->super);
863 struct ext2_inode *inode = NULL;
864 ext2_inode_scan scan;
868 retval = ext2fs_open_inode_scan(fs, 0, &scan);
872 retval = ext2fs_get_mem(length, &inode);
877 retval = ext2fs_get_next_inode_full(scan, &ino, inode, length);
882 if (!ext2fs_test_inode_bitmap2(fs->inode_map, ino)) {
883 memset(inode, 0, length);
884 retval = ext2fs_write_inode_full(fs, ino, inode,
892 ext2fs_free_mem(&inode);
893 ext2fs_close_inode_scan(scan);
897 static errcode_t disable_uninit_bg(ext2_filsys fs, __u32 csum_feature_flag)
899 struct ext2_group_desc *gd;
904 /* Load bitmaps to ensure that the uninit ones get written out */
905 fs->super->s_feature_ro_compat |= csum_feature_flag;
906 retval = ext2fs_read_bitmaps(fs);
907 fs->super->s_feature_ro_compat &= ~csum_feature_flag;
909 com_err("disable_uninit_bg", retval,
910 "while reading bitmaps");
911 request_fsck_afterwards(fs);
914 ext2fs_mark_ib_dirty(fs);
915 ext2fs_mark_bb_dirty(fs);
917 /* If we're only turning off uninit_bg, zero the inodes */
918 if (csum_feature_flag == EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
919 retval = zero_empty_inodes(fs);
921 com_err("disable_uninit_bg", retval,
922 "while zeroing unused inodes");
923 request_fsck_afterwards(fs);
928 /* The bbitmap is zeroed; we must mark group metadata blocks in use */
929 for (i = 0; i < fs->group_desc_count; i++) {
930 b = ext2fs_block_bitmap_loc(fs, i);
931 ext2fs_mark_block_bitmap2(fs->block_map, b);
932 b = ext2fs_inode_bitmap_loc(fs, i);
933 ext2fs_mark_block_bitmap2(fs->block_map, b);
935 retval = ext2fs_super_and_bgd_loc2(fs, i, &b, &c, &d, NULL);
936 if (retval == 0 && b)
937 ext2fs_mark_block_bitmap2(fs->block_map, b);
938 if (retval == 0 && c)
939 ext2fs_mark_block_bitmap2(fs->block_map, c);
940 if (retval == 0 && d)
941 ext2fs_mark_block_bitmap2(fs->block_map, d);
943 com_err("disable_uninit_bg", retval,
944 "while initializing block bitmaps");
945 request_fsck_afterwards(fs);
948 gd = ext2fs_group_desc(fs, fs->group_desc, i);
949 gd->bg_itable_unused = 0;
951 ext2fs_group_desc_csum_set(fs, i);
953 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
954 ext2fs_mark_super_dirty(fs);
960 * Update the feature set as provided by the user.
962 static int update_feature_set(ext2_filsys fs, char *features)
964 struct ext2_super_block *sb = fs->super;
965 __u32 old_features[3];
967 unsigned int mask_err;
970 #define FEATURE_ON(type, mask) (!(old_features[(type)] & (mask)) && \
971 ((&sb->s_feature_compat)[(type)] & (mask)))
972 #define FEATURE_OFF(type, mask) ((old_features[(type)] & (mask)) && \
973 !((&sb->s_feature_compat)[(type)] & (mask)))
974 #define FEATURE_CHANGED(type, mask) ((mask) & \
975 (old_features[(type)] ^ (&sb->s_feature_compat)[(type)]))
977 old_features[E2P_FEATURE_COMPAT] = sb->s_feature_compat;
978 old_features[E2P_FEATURE_INCOMPAT] = sb->s_feature_incompat;
979 old_features[E2P_FEATURE_RO_INCOMPAT] = sb->s_feature_ro_compat;
981 if (e2p_edit_feature2(features, &sb->s_feature_compat,
982 ok_features, clear_ok_features,
983 &type_err, &mask_err)) {
986 _("Invalid filesystem option set: %s\n"),
988 else if (type_err & E2P_FEATURE_NEGATE_FLAG)
989 fprintf(stderr, _("Clearing filesystem feature '%s' "
991 e2p_feature2string(type_err &
992 E2P_FEATURE_TYPE_MASK,
995 fprintf(stderr, _("Setting filesystem feature '%s' "
997 e2p_feature2string(type_err, mask_err));
1001 if (FEATURE_OFF(E2P_FEATURE_COMPAT, EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
1002 if ((mount_flags & EXT2_MF_MOUNTED) &&
1003 !(mount_flags & EXT2_MF_READONLY)) {
1004 fputs(_("The has_journal feature may only be "
1005 "cleared when the filesystem is\n"
1006 "unmounted or mounted "
1007 "read-only.\n"), stderr);
1010 if ((sb->s_feature_incompat &
1011 EXT3_FEATURE_INCOMPAT_RECOVER) &&
1013 fputs(_("The needs_recovery flag is set. "
1014 "Please run e2fsck before clearing\n"
1015 "the has_journal flag.\n"), stderr);
1018 if (sb->s_journal_inum) {
1019 if (remove_journal_inode(fs))
1022 if (sb->s_journal_dev) {
1023 if (remove_journal_device(fs))
1028 if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT,
1029 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
1030 if (sb->s_feature_incompat &
1031 EXT2_FEATURE_INCOMPAT_META_BG) {
1032 fputs(_("Setting filesystem feature 'sparse_super' "
1033 "not supported\nfor filesystems with "
1034 "the meta_bg feature enabled.\n"),
1040 if (FEATURE_ON(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_MMP)) {
1043 if ((mount_flags & EXT2_MF_MOUNTED) ||
1044 (mount_flags & EXT2_MF_READONLY)) {
1045 fputs(_("The multiple mount protection feature can't\n"
1046 "be set if the filesystem is mounted or\n"
1047 "read-only.\n"), stderr);
1051 error = ext2fs_mmp_init(fs);
1053 fputs(_("\nError while enabling multiple mount "
1054 "protection feature."), stderr);
1059 * We want to update group desc with the new free blocks count
1061 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
1063 printf(_("Multiple mount protection has been enabled "
1064 "with update interval %ds.\n"),
1065 sb->s_mmp_update_interval);
1068 if (FEATURE_OFF(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_MMP)) {
1071 if (mount_flags & EXT2_MF_READONLY) {
1072 fputs(_("The multiple mount protection feature cannot\n"
1073 "be disabled if the filesystem is readonly.\n"),
1078 error = ext2fs_read_bitmaps(fs);
1080 fputs(_("Error while reading bitmaps\n"), stderr);
1084 error = ext2fs_mmp_read(fs, sb->s_mmp_block, NULL);
1086 struct mmp_struct *mmp_cmp = fs->mmp_cmp;
1088 if (error == EXT2_ET_MMP_MAGIC_INVALID)
1089 printf(_("Magic number in MMP block does not "
1090 "match. expected: %x, actual: %x\n"),
1091 EXT4_MMP_MAGIC, mmp_cmp->mmp_magic);
1093 com_err(program_name, error, "%s",
1094 _("while reading MMP block."));
1098 /* We need to force out the group descriptors as well */
1099 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
1100 ext2fs_block_alloc_stats2(fs, sb->s_mmp_block, -1);
1102 sb->s_mmp_block = 0;
1103 sb->s_mmp_update_interval = 0;
1106 if (FEATURE_ON(E2P_FEATURE_COMPAT, EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
1108 * If adding a journal flag, let the create journal
1109 * code below handle setting the flag and creating the
1110 * journal. We supply a default size if necessary.
1114 sb->s_feature_compat &= ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1117 if (FEATURE_ON(E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_DIR_INDEX)) {
1118 if (!sb->s_def_hash_version)
1119 sb->s_def_hash_version = EXT2_HASH_HALF_MD4;
1120 if (uuid_is_null((unsigned char *) sb->s_hash_seed))
1121 uuid_generate((unsigned char *) sb->s_hash_seed);
1124 if (FEATURE_OFF(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
1125 if (ext2fs_check_desc(fs)) {
1126 fputs(_("Clearing the flex_bg flag would "
1127 "cause the the filesystem to be\n"
1128 "inconsistent.\n"), stderr);
1133 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
1134 EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
1135 if ((mount_flags & EXT2_MF_MOUNTED) &&
1136 !(mount_flags & EXT2_MF_READONLY)) {
1137 fputs(_("The huge_file feature may only be "
1138 "cleared when the filesystem is\n"
1139 "unmounted or mounted "
1140 "read-only.\n"), stderr);
1145 if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT,
1146 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
1147 if (check_fsck_needed(fs))
1149 if (mount_flags & EXT2_MF_MOUNTED) {
1150 fputs(_("Cannot enable metadata_csum on a mounted "
1151 "filesystem!\n"), stderr);
1154 if (!EXT2_HAS_INCOMPAT_FEATURE(fs->super,
1155 EXT3_FEATURE_INCOMPAT_EXTENTS))
1157 _("Extents are not enabled. The file extent "
1158 "tree can be checksummed, whereas block maps "
1159 "cannot. Not enabling extents reduces the "
1160 "coverage of metadata checksumming. "
1161 "Re-run with -O extent to rectify.\n"));
1162 if (!EXT2_HAS_INCOMPAT_FEATURE(fs->super,
1163 EXT4_FEATURE_INCOMPAT_64BIT))
1165 _("64-bit filesystem support is not enabled. "
1166 "The larger fields afforded by this feature "
1167 "enable full-strength checksumming. "
1168 "Run resize2fs -b to rectify.\n"));
1169 rewrite_checksums = 1;
1170 /* metadata_csum supersedes uninit_bg */
1171 fs->super->s_feature_ro_compat &=
1172 ~EXT4_FEATURE_RO_COMPAT_GDT_CSUM;
1174 /* if uninit_bg was previously off, rewrite group desc */
1175 if (!(old_features[E2P_FEATURE_RO_INCOMPAT] &
1176 EXT4_FEATURE_RO_COMPAT_GDT_CSUM))
1177 enable_uninit_bg(fs);
1180 * Since metadata_csum supersedes uninit_bg, pretend like
1181 * uninit_bg has been off all along.
1183 old_features[E2P_FEATURE_RO_INCOMPAT] &=
1184 ~EXT4_FEATURE_RO_COMPAT_GDT_CSUM;
1187 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
1188 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
1189 __u32 test_features[3];
1191 if (check_fsck_needed(fs))
1193 if (mount_flags & EXT2_MF_MOUNTED) {
1194 fputs(_("Cannot disable metadata_csum on a mounted "
1195 "filesystem!\n"), stderr);
1198 rewrite_checksums = 1;
1200 /* Enable uninit_bg unless the user expressly turned it off */
1201 memcpy(test_features, old_features, sizeof(test_features));
1202 test_features[E2P_FEATURE_RO_INCOMPAT] |=
1203 EXT4_FEATURE_RO_COMPAT_GDT_CSUM;
1204 e2p_edit_feature2(features, test_features, ok_features,
1205 clear_ok_features, NULL, NULL);
1206 if (test_features[E2P_FEATURE_RO_INCOMPAT] &
1207 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)
1208 fs->super->s_feature_ro_compat |=
1209 EXT4_FEATURE_RO_COMPAT_GDT_CSUM;
1212 * If we're turning off metadata_csum and not turning on
1213 * uninit_bg, rewrite group desc.
1215 if (!(fs->super->s_feature_ro_compat &
1216 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
1217 err = disable_uninit_bg(fs,
1218 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM);
1223 * metadata_csum previously provided uninit_bg, so if
1224 * we're also setting the uninit_bg feature bit,
1225 * pretend like it was previously enabled. Checksums
1226 * will be rewritten with crc16 later.
1228 old_features[E2P_FEATURE_RO_INCOMPAT] |=
1229 EXT4_FEATURE_RO_COMPAT_GDT_CSUM;
1232 if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT,
1233 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
1234 /* Do not enable uninit_bg when metadata_csum enabled */
1235 if (fs->super->s_feature_ro_compat &
1236 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)
1237 fs->super->s_feature_ro_compat &=
1238 ~EXT4_FEATURE_RO_COMPAT_GDT_CSUM;
1240 enable_uninit_bg(fs);
1243 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
1244 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
1245 err = disable_uninit_bg(fs,
1246 EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
1252 * We don't actually toggle 64bit; resize2fs does that. But this
1253 * must come after the metadata_csum feature_on so that it won't
1254 * complain about the lack of 64bit.
1256 if (FEATURE_ON(E2P_FEATURE_INCOMPAT,
1257 EXT4_FEATURE_INCOMPAT_64BIT)) {
1258 if (mount_flags & EXT2_MF_MOUNTED) {
1259 fprintf(stderr, _("Cannot enable 64-bit mode "
1260 "while mounted!\n"));
1263 sb->s_feature_incompat &= ~EXT4_FEATURE_INCOMPAT_64BIT;
1266 if (FEATURE_OFF(E2P_FEATURE_INCOMPAT,
1267 EXT4_FEATURE_INCOMPAT_64BIT)) {
1268 if (mount_flags & EXT2_MF_MOUNTED) {
1269 fprintf(stderr, _("Cannot disable 64-bit mode "
1270 "while mounted!\n"));
1273 sb->s_feature_incompat |= EXT4_FEATURE_INCOMPAT_64BIT;
1277 if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT,
1278 EXT4_FEATURE_RO_COMPAT_QUOTA)) {
1280 * Set the Q_flag here and handle the quota options in the code
1285 /* Enable both user quota and group quota by default */
1286 usrquota = QOPT_ENABLE;
1287 grpquota = QOPT_ENABLE;
1289 sb->s_feature_ro_compat &= ~EXT4_FEATURE_RO_COMPAT_QUOTA;
1292 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
1293 EXT4_FEATURE_RO_COMPAT_QUOTA)) {
1295 * Set the Q_flag here and handle the quota options in the code
1299 fputs(_("\nWarning: '^quota' option overrides '-Q'"
1300 "arguments.\n"), stderr);
1302 /* Disable both user quota and group quota by default */
1303 usrquota = QOPT_DISABLE;
1304 grpquota = QOPT_DISABLE;
1307 if (FEATURE_ON(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_ENCRYPT)) {
1308 fs->super->s_encrypt_algos[0] =
1309 EXT4_ENCRYPTION_MODE_AES_256_XTS;
1310 fs->super->s_encrypt_algos[1] =
1311 EXT4_ENCRYPTION_MODE_AES_256_CTS;
1314 if (sb->s_rev_level == EXT2_GOOD_OLD_REV &&
1315 (sb->s_feature_compat || sb->s_feature_ro_compat ||
1316 sb->s_feature_incompat))
1317 ext2fs_update_dynamic_rev(fs);
1319 if (FEATURE_CHANGED(E2P_FEATURE_RO_INCOMPAT,
1320 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER) ||
1321 FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
1322 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) ||
1323 FEATURE_CHANGED(E2P_FEATURE_INCOMPAT,
1324 EXT2_FEATURE_INCOMPAT_FILETYPE) ||
1325 FEATURE_CHANGED(E2P_FEATURE_COMPAT,
1326 EXT2_FEATURE_COMPAT_RESIZE_INODE) ||
1327 FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
1328 EXT2_FEATURE_RO_COMPAT_LARGE_FILE))
1329 request_fsck_afterwards(fs);
1331 if ((old_features[E2P_FEATURE_COMPAT] != sb->s_feature_compat) ||
1332 (old_features[E2P_FEATURE_INCOMPAT] != sb->s_feature_incompat) ||
1333 (old_features[E2P_FEATURE_RO_INCOMPAT] != sb->s_feature_ro_compat))
1334 ext2fs_mark_super_dirty(fs);
1340 * Add a journal to the filesystem.
1342 static int add_journal(ext2_filsys fs)
1344 unsigned long journal_blocks;
1349 if (fs->super->s_feature_compat &
1350 EXT3_FEATURE_COMPAT_HAS_JOURNAL) {
1351 fputs(_("The filesystem already has a journal.\n"), stderr);
1354 if (journal_device) {
1355 if (!check_plausibility(journal_device, CHECK_BLOCK_DEV,
1357 proceed_question(-1);
1358 check_mount(journal_device, 0, _("journal"));
1359 #ifdef CONFIG_TESTIO_DEBUG
1360 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
1361 io_ptr = test_io_manager;
1362 test_io_backing_manager = unix_io_manager;
1365 io_ptr = unix_io_manager;
1366 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
1367 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1368 fs->blocksize, io_ptr, &jfs);
1370 com_err(program_name, retval,
1371 _("\n\twhile trying to open journal on %s\n"),
1375 printf(_("Creating journal on device %s: "),
1379 retval = ext2fs_add_journal_device(fs, jfs);
1380 ext2fs_close_free(&jfs);
1382 com_err(program_name, retval,
1383 _("while adding filesystem to journal on %s"),
1387 fputs(_("done\n"), stdout);
1388 } else if (journal_size) {
1389 fputs(_("Creating journal inode: "), stdout);
1391 journal_blocks = figure_journal_size(journal_size, fs);
1393 if (journal_location_string)
1395 parse_num_blocks2(journal_location_string,
1396 fs->super->s_log_block_size);
1397 retval = ext2fs_add_journal_inode2(fs, journal_blocks,
1401 fprintf(stderr, "\n");
1402 com_err(program_name, retval, "%s",
1403 _("\n\twhile trying to create journal file"));
1406 fputs(_("done\n"), stdout);
1408 * If the filesystem wasn't mounted, we need to force
1409 * the block group descriptors out.
1411 if ((mount_flags & EXT2_MF_MOUNTED) == 0)
1412 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
1414 print_check_message(fs->super->s_max_mnt_count,
1415 fs->super->s_checkinterval);
1419 free(journal_device);
1423 static void handle_quota_options(ext2_filsys fs)
1428 if (!usrquota && !grpquota)
1429 /* Nothing to do. */
1432 quota_init_context(&qctx, fs, -1);
1434 if (usrquota == QOPT_ENABLE || grpquota == QOPT_ENABLE)
1435 quota_compute_usage(qctx);
1437 if (usrquota == QOPT_ENABLE && !fs->super->s_usr_quota_inum) {
1438 if ((qf_ino = quota_file_exists(fs, USRQUOTA,
1440 quota_update_limits(qctx, qf_ino, USRQUOTA);
1441 quota_write_inode(qctx, USRQUOTA);
1442 } else if (usrquota == QOPT_DISABLE) {
1443 quota_remove_inode(fs, USRQUOTA);
1446 if (grpquota == QOPT_ENABLE && !fs->super->s_grp_quota_inum) {
1447 if ((qf_ino = quota_file_exists(fs, GRPQUOTA,
1449 quota_update_limits(qctx, qf_ino, GRPQUOTA);
1450 quota_write_inode(qctx, GRPQUOTA);
1451 } else if (grpquota == QOPT_DISABLE) {
1452 quota_remove_inode(fs, GRPQUOTA);
1455 quota_release_context(&qctx);
1457 if ((usrquota == QOPT_ENABLE) || (grpquota == QOPT_ENABLE)) {
1458 fs->super->s_feature_ro_compat |= EXT4_FEATURE_RO_COMPAT_QUOTA;
1459 ext2fs_mark_super_dirty(fs);
1460 } else if (!fs->super->s_usr_quota_inum &&
1461 !fs->super->s_grp_quota_inum) {
1462 fs->super->s_feature_ro_compat &= ~EXT4_FEATURE_RO_COMPAT_QUOTA;
1463 ext2fs_mark_super_dirty(fs);
1470 static void parse_quota_opts(const char *opts)
1472 char *buf, *token, *next, *p;
1476 buf = malloc(len+1);
1478 fputs(_("Couldn't allocate memory to parse quota "
1479 "options!\n"), stderr);
1483 for (token = buf; token && *token; token = next) {
1484 p = strchr(token, ',');
1491 if (strcmp(token, "usrquota") == 0) {
1492 usrquota = QOPT_ENABLE;
1493 } else if (strcmp(token, "^usrquota") == 0) {
1494 usrquota = QOPT_DISABLE;
1495 } else if (strcmp(token, "grpquota") == 0) {
1496 grpquota = QOPT_ENABLE;
1497 } else if (strcmp(token, "^grpquota") == 0) {
1498 grpquota = QOPT_DISABLE;
1500 fputs(_("\nBad quota options specified.\n\n"
1501 "Following valid quota options are available "
1502 "(pass by separating with comma):\n"
1514 static void parse_e2label_options(int argc, char ** argv)
1516 if ((argc < 2) || (argc > 3)) {
1517 fputs(_("Usage: e2label device [newlabel]\n"), stderr);
1520 io_options = strchr(argv[1], '?');
1523 device_name = blkid_get_devname(NULL, argv[1], NULL);
1525 com_err("e2label", 0, _("Unable to resolve '%s'"),
1529 open_flag = EXT2_FLAG_JOURNAL_DEV_OK;
1531 open_flag |= EXT2_FLAG_RW;
1533 new_label = argv[2];
1538 static time_t parse_time(char *str)
1542 if (strcmp(str, "now") == 0) {
1545 memset(&ts, 0, sizeof(ts));
1546 #ifdef HAVE_STRPTIME
1547 strptime(str, "%Y%m%d%H%M%S", &ts);
1549 sscanf(str, "%4d%2d%2d%2d%2d%2d", &ts.tm_year, &ts.tm_mon,
1550 &ts.tm_mday, &ts.tm_hour, &ts.tm_min, &ts.tm_sec);
1553 if (ts.tm_year < 0 || ts.tm_mon < 0 || ts.tm_mon > 11 ||
1554 ts.tm_mday < 0 || ts.tm_mday > 31 || ts.tm_hour > 23 ||
1555 ts.tm_min > 59 || ts.tm_sec > 61)
1558 if (ts.tm_mday == 0) {
1559 com_err(program_name, 0,
1560 _("Couldn't parse date/time specifier: %s"),
1565 return (mktime(&ts));
1568 static void parse_tune2fs_options(int argc, char **argv)
1574 char optstring[100] = "c:e:fg:i:jlm:o:r:s:u:C:E:I:J:L:M:O:T:U:";
1577 strcat(optstring, "Q:");
1581 printf("tune2fs %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
1582 while ((c = getopt(argc, argv, optstring)) != EOF)
1585 max_mount_count = strtol(optarg, &tmp, 0);
1586 if (*tmp || max_mount_count > 16000) {
1587 com_err(program_name, 0,
1588 _("bad mounts count - %s"),
1592 if (max_mount_count == 0)
1593 max_mount_count = -1;
1595 open_flag = EXT2_FLAG_RW;
1598 mount_count = strtoul(optarg, &tmp, 0);
1599 if (*tmp || mount_count > 16000) {
1600 com_err(program_name, 0,
1601 _("bad mounts count - %s"),
1606 open_flag = EXT2_FLAG_RW;
1609 if (strcmp(optarg, "continue") == 0)
1610 errors = EXT2_ERRORS_CONTINUE;
1611 else if (strcmp(optarg, "remount-ro") == 0)
1612 errors = EXT2_ERRORS_RO;
1613 else if (strcmp(optarg, "panic") == 0)
1614 errors = EXT2_ERRORS_PANIC;
1616 com_err(program_name, 0,
1617 _("bad error behavior - %s"),
1622 open_flag = EXT2_FLAG_RW;
1625 extended_cmd = optarg;
1626 open_flag |= EXT2_FLAG_RW;
1628 case 'f': /* Force */
1632 resgid = strtoul(optarg, &tmp, 0);
1634 gr = getgrnam(optarg);
1638 resgid = gr->gr_gid;
1643 com_err(program_name, 0,
1644 _("bad gid/group name - %s"),
1649 open_flag = EXT2_FLAG_RW;
1652 interval = strtoul(optarg, &tmp, 0);
1659 case 'D': /* days */
1665 case 'M': /* months! */
1666 interval *= 86400 * 30;
1670 case 'W': /* weeks */
1671 interval *= 86400 * 7;
1676 com_err(program_name, 0,
1677 _("bad interval - %s"), optarg);
1681 open_flag = EXT2_FLAG_RW;
1686 open_flag = EXT2_FLAG_RW;
1689 parse_journal_opts(optarg);
1690 open_flag = EXT2_FLAG_RW;
1698 open_flag |= EXT2_FLAG_RW |
1699 EXT2_FLAG_JOURNAL_DEV_OK;
1702 reserved_ratio = strtod(optarg, &tmp);
1703 if (*tmp || reserved_ratio > 50 ||
1704 reserved_ratio < 0) {
1705 com_err(program_name, 0,
1706 _("bad reserved block ratio - %s"),
1711 open_flag = EXT2_FLAG_RW;
1714 new_last_mounted = optarg;
1716 open_flag = EXT2_FLAG_RW;
1720 com_err(program_name, 0, "%s",
1721 _("-o may only be specified once"));
1724 mntopts_cmd = optarg;
1725 open_flag = EXT2_FLAG_RW;
1729 com_err(program_name, 0, "%s",
1730 _("-O may only be specified once"));
1733 features_cmd = optarg;
1734 open_flag = EXT2_FLAG_RW;
1739 parse_quota_opts(optarg);
1740 open_flag = EXT2_FLAG_RW;
1744 reserved_blocks = strtoul(optarg, &tmp, 0);
1746 com_err(program_name, 0,
1747 _("bad reserved blocks count - %s"),
1752 open_flag = EXT2_FLAG_RW;
1754 case 's': /* Deprecated */
1755 s_flag = atoi(optarg);
1756 open_flag = EXT2_FLAG_RW;
1760 last_check_time = parse_time(optarg);
1761 open_flag = EXT2_FLAG_RW;
1764 resuid = strtoul(optarg, &tmp, 0);
1766 pw = getpwnam(optarg);
1770 resuid = pw->pw_uid;
1775 com_err(program_name, 0,
1776 _("bad uid/user name - %s"),
1781 open_flag = EXT2_FLAG_RW;
1786 open_flag = EXT2_FLAG_RW |
1787 EXT2_FLAG_JOURNAL_DEV_OK;
1790 new_inode_size = strtoul(optarg, &tmp, 0);
1792 com_err(program_name, 0,
1793 _("bad inode size - %s"),
1797 if (!((new_inode_size &
1798 (new_inode_size - 1)) == 0)) {
1799 com_err(program_name, 0,
1800 _("Inode size must be a "
1801 "power of two- %s"),
1805 open_flag = EXT2_FLAG_RW;
1811 if (optind < argc - 1 || optind == argc)
1813 if (!open_flag && !l_flag)
1815 io_options = strchr(argv[optind], '?');
1818 device_name = blkid_get_devname(NULL, argv[optind], NULL);
1820 com_err(program_name, 0, _("Unable to resolve '%s'"),
1826 #ifdef CONFIG_BUILD_FINDFS
1827 void do_findfs(int argc, char **argv)
1832 (strncmp(argv[1], "LABEL=", 6) && strncmp(argv[1], "UUID=", 5))) {
1833 fprintf(stderr, "Usage: findfs LABEL=<label>|UUID=<uuid>\n");
1836 dev = blkid_get_devname(NULL, argv[1], NULL);
1838 com_err("findfs", 0, _("Unable to resolve '%s'"),
1847 static int parse_extended_opts(ext2_filsys fs, const char *opts)
1849 char *buf, *token, *next, *p, *arg;
1854 buf = malloc(len+1);
1856 fprintf(stderr, "%s",
1857 _("Couldn't allocate memory to parse options!\n"));
1861 for (token = buf; token && *token; token = next) {
1862 p = strchr(token, ',');
1868 arg = strchr(token, '=');
1873 if (strcmp(token, "clear-mmp") == 0 ||
1874 strcmp(token, "clear_mmp") == 0) {
1876 } else if (strcmp(token, "mmp_update_interval") == 0) {
1882 intv = strtoul(arg, &p, 0);
1885 _("Invalid mmp_update_interval: %s\n"),
1891 intv = EXT4_MMP_UPDATE_INTERVAL;
1892 } else if (intv > EXT4_MMP_MAX_UPDATE_INTERVAL) {
1894 _("mmp_update_interval too big: %lu\n"),
1899 printf(P_("Setting multiple mount protection update "
1900 "interval to %lu second\n",
1901 "Setting multiple mount protection update "
1902 "interval to %lu seconds\n", intv),
1904 fs->super->s_mmp_update_interval = intv;
1905 ext2fs_mark_super_dirty(fs);
1906 } else if (!strcmp(token, "test_fs")) {
1907 fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
1908 printf("Setting test filesystem flag\n");
1909 ext2fs_mark_super_dirty(fs);
1910 } else if (!strcmp(token, "^test_fs")) {
1911 fs->super->s_flags &= ~EXT2_FLAGS_TEST_FILESYS;
1912 printf("Clearing test filesystem flag\n");
1913 ext2fs_mark_super_dirty(fs);
1914 } else if (strcmp(token, "stride") == 0) {
1919 stride = strtoul(arg, &p, 0);
1922 _("Invalid RAID stride: %s\n"),
1928 } else if (strcmp(token, "stripe-width") == 0 ||
1929 strcmp(token, "stripe_width") == 0) {
1934 stripe_width = strtoul(arg, &p, 0);
1937 _("Invalid RAID stripe-width: %s\n"),
1942 stripe_width_set = 1;
1943 } else if (strcmp(token, "hash_alg") == 0 ||
1944 strcmp(token, "hash-alg") == 0) {
1949 hash_alg = e2p_string2hash(arg);
1952 _("Invalid hash algorithm: %s\n"),
1957 fs->super->s_def_hash_version = hash_alg;
1958 printf(_("Setting default hash algorithm "
1961 ext2fs_mark_super_dirty(fs);
1962 } else if (!strcmp(token, "mount_opts")) {
1967 if (strlen(arg) >= sizeof(fs->super->s_mount_opts)) {
1969 "Extended mount options too long\n");
1972 ext_mount_opts = strdup(arg);
1977 fprintf(stderr, "%s", _("\nBad options specified.\n\n"
1978 "Extended options are separated by commas, "
1979 "and may take an argument which\n"
1980 "\tis set off by an equals ('=') sign.\n\n"
1981 "Valid extended options are:\n"
1983 "\thash_alg=<hash algorithm>\n"
1984 "\tmount_opts=<extended default mount options>\n"
1985 "\tstride=<RAID per-disk chunk size in blocks>\n"
1986 "\tstripe_width=<RAID stride*data disks in blocks>\n"
1998 * Fill in the block bitmap bmap with the information regarding the
1999 * blocks to be moved
2001 static int get_move_bitmaps(ext2_filsys fs, int new_ino_blks_per_grp,
2002 ext2fs_block_bitmap bmap)
2006 ext2_badblocks_list bb_list = 0;
2007 blk64_t j, needed_blocks = 0;
2008 blk64_t start_blk, end_blk;
2010 retval = ext2fs_read_bb_inode(fs, &bb_list);
2014 for (i = 0; i < fs->group_desc_count; i++) {
2015 start_blk = ext2fs_inode_table_loc(fs, i) +
2016 fs->inode_blocks_per_group;
2018 end_blk = ext2fs_inode_table_loc(fs, i) +
2019 new_ino_blks_per_grp;
2021 for (j = start_blk; j < end_blk; j++) {
2022 if (ext2fs_test_block_bitmap2(fs->block_map, j)) {
2024 * IF the block is a bad block we fail
2026 if (ext2fs_badblocks_list_test(bb_list, j)) {
2027 ext2fs_badblocks_list_free(bb_list);
2031 ext2fs_mark_block_bitmap2(bmap, j);
2034 * We are going to use this block for
2035 * inode table. So mark them used.
2037 ext2fs_mark_block_bitmap2(fs->block_map, j);
2040 needed_blocks += end_blk - start_blk;
2043 ext2fs_badblocks_list_free(bb_list);
2044 if (needed_blocks > ext2fs_free_blocks_count(fs->super))
2050 static int ext2fs_is_meta_block(ext2_filsys fs, blk64_t blk)
2053 group = ext2fs_group_of_blk2(fs, blk);
2054 if (ext2fs_block_bitmap_loc(fs, group) == blk)
2056 if (ext2fs_inode_bitmap_loc(fs, group) == blk)
2061 static int ext2fs_is_block_in_group(ext2_filsys fs, dgrp_t group, blk64_t blk)
2063 blk64_t start_blk, end_blk;
2064 start_blk = fs->super->s_first_data_block +
2065 EXT2_GROUPS_TO_BLOCKS(fs->super, group);
2067 * We cannot get new block beyond end_blk for for the last block group
2068 * so we can check with EXT2_BLOCKS_PER_GROUP even for last block group
2070 end_blk = start_blk + EXT2_BLOCKS_PER_GROUP(fs->super);
2071 if (blk >= start_blk && blk <= end_blk)
2076 static int move_block(ext2_filsys fs, ext2fs_block_bitmap bmap)
2083 blk64_t blk, new_blk, goal;
2084 struct blk_move *bmv;
2086 retval = ext2fs_get_mem(fs->blocksize, &buf);
2090 for (new_blk = blk = fs->super->s_first_data_block;
2091 blk < ext2fs_blocks_count(fs->super); blk++) {
2092 if (!ext2fs_test_block_bitmap2(bmap, blk))
2095 if (ext2fs_is_meta_block(fs, blk)) {
2097 * If the block is mapping a fs meta data block
2098 * like group desc/block bitmap/inode bitmap. We
2099 * should find a block in the same group and fix
2100 * the respective fs metadata pointers. Otherwise
2103 group = ext2fs_group_of_blk2(fs, blk);
2104 goal = ext2fs_group_first_block2(fs, group);
2110 retval = ext2fs_new_block2(fs, goal, NULL, &new_blk);
2114 /* new fs meta data block should be in the same group */
2115 if (meta_data && !ext2fs_is_block_in_group(fs, group, new_blk)) {
2120 /* Mark this block as allocated */
2121 ext2fs_mark_block_bitmap2(fs->block_map, new_blk);
2123 /* Add it to block move list */
2124 retval = ext2fs_get_mem(sizeof(struct blk_move), &bmv);
2129 bmv->new_loc = new_blk;
2131 list_add(&(bmv->list), &blk_move_list);
2133 retval = io_channel_read_blk64(fs->io, blk, 1, buf);
2137 retval = io_channel_write_blk64(fs->io, new_blk, 1, buf);
2143 ext2fs_free_mem(&buf);
2147 static blk64_t translate_block(blk64_t blk)
2149 struct list_head *entry;
2150 struct blk_move *bmv;
2152 list_for_each(entry, &blk_move_list) {
2153 bmv = list_entry(entry, struct blk_move, list);
2154 if (bmv->old_loc == blk)
2155 return bmv->new_loc;
2161 static int process_block(ext2_filsys fs EXT2FS_ATTR((unused)),
2163 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
2164 blk64_t ref_block EXT2FS_ATTR((unused)),
2165 int ref_offset EXT2FS_ATTR((unused)),
2170 ext2fs_block_bitmap bmap = (ext2fs_block_bitmap) priv_data;
2172 if (!ext2fs_test_block_bitmap2(bmap, *block_nr))
2174 new_blk = translate_block(*block_nr);
2176 *block_nr = new_blk;
2178 * This will force the ext2fs_write_inode in the iterator
2180 ret |= BLOCK_CHANGED;
2186 static int inode_scan_and_fix(ext2_filsys fs, ext2fs_block_bitmap bmap)
2188 errcode_t retval = 0;
2191 char *block_buf = 0;
2192 struct ext2_inode inode;
2193 ext2_inode_scan scan = NULL;
2195 retval = ext2fs_get_mem(fs->blocksize * 3, &block_buf);
2199 retval = ext2fs_open_inode_scan(fs, 0, &scan);
2204 retval = ext2fs_get_next_inode(scan, &ino, &inode);
2211 if (inode.i_links_count == 0)
2212 continue; /* inode not in use */
2215 * If we end up modifying the journal inode
2216 * the sb->s_jnl_blocks will differ. But a
2217 * subsequent e2fsck fixes that.
2218 * Do we need to fix this ??
2221 if (ext2fs_file_acl_block(fs, &inode) &&
2222 ext2fs_test_block_bitmap2(bmap,
2223 ext2fs_file_acl_block(fs, &inode))) {
2224 blk = translate_block(ext2fs_file_acl_block(fs,
2229 ext2fs_file_acl_block_set(fs, &inode, blk);
2232 * Write the inode to disk so that inode table
2235 retval = ext2fs_write_inode(fs, ino, &inode);
2240 if (!ext2fs_inode_has_valid_blocks2(fs, &inode))
2243 retval = ext2fs_block_iterate3(fs, ino, 0, block_buf,
2244 process_block, bmap);
2251 ext2fs_free_mem(&block_buf);
2252 ext2fs_close_inode_scan(scan);
2258 * We need to scan for inode and block bitmaps that may need to be
2259 * moved. This can take place if the filesystem was formatted for
2260 * RAID arrays using the mke2fs's extended option "stride".
2262 static int group_desc_scan_and_fix(ext2_filsys fs, ext2fs_block_bitmap bmap)
2265 blk64_t blk, new_blk;
2267 for (i = 0; i < fs->group_desc_count; i++) {
2268 blk = ext2fs_block_bitmap_loc(fs, i);
2269 if (ext2fs_test_block_bitmap2(bmap, blk)) {
2270 new_blk = translate_block(blk);
2273 ext2fs_block_bitmap_loc_set(fs, i, new_blk);
2276 blk = ext2fs_inode_bitmap_loc(fs, i);
2277 if (ext2fs_test_block_bitmap2(bmap, blk)) {
2278 new_blk = translate_block(blk);
2281 ext2fs_inode_bitmap_loc_set(fs, i, new_blk);
2287 static int expand_inode_table(ext2_filsys fs, unsigned long new_ino_size)
2292 int new_ino_blks_per_grp;
2294 char *old_itable = NULL, *new_itable = NULL;
2295 char *tmp_old_itable = NULL, *tmp_new_itable = NULL;
2296 unsigned long old_ino_size;
2297 int old_itable_size, new_itable_size;
2299 old_itable_size = fs->inode_blocks_per_group * fs->blocksize;
2300 old_ino_size = EXT2_INODE_SIZE(fs->super);
2302 new_ino_blks_per_grp = ext2fs_div_ceil(
2303 EXT2_INODES_PER_GROUP(fs->super) *
2307 new_itable_size = new_ino_blks_per_grp * fs->blocksize;
2309 retval = ext2fs_get_mem(old_itable_size, &old_itable);
2313 retval = ext2fs_get_mem(new_itable_size, &new_itable);
2317 tmp_old_itable = old_itable;
2318 tmp_new_itable = new_itable;
2320 for (i = 0; i < fs->group_desc_count; i++) {
2321 blk = ext2fs_inode_table_loc(fs, i);
2322 retval = io_channel_read_blk64(fs->io, blk,
2323 fs->inode_blocks_per_group, old_itable);
2327 for (j = 0; j < EXT2_INODES_PER_GROUP(fs->super); j++) {
2328 memcpy(new_itable, old_itable, old_ino_size);
2330 memset(new_itable+old_ino_size, 0,
2331 new_ino_size - old_ino_size);
2333 new_itable += new_ino_size;
2334 old_itable += old_ino_size;
2337 /* reset the pointer */
2338 old_itable = tmp_old_itable;
2339 new_itable = tmp_new_itable;
2341 retval = io_channel_write_blk64(fs->io, blk,
2342 new_ino_blks_per_grp, new_itable);
2347 /* Update the meta data */
2348 fs->inode_blocks_per_group = new_ino_blks_per_grp;
2349 ext2fs_free_inode_cache(fs->icache);
2351 fs->super->s_inode_size = new_ino_size;
2355 ext2fs_free_mem(&old_itable);
2358 ext2fs_free_mem(&new_itable);
2363 static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs)
2367 unsigned int group = 0;
2368 unsigned int count = 0;
2373 * First calculate the block statistics
2375 for (blk = fs->super->s_first_data_block;
2376 blk < ext2fs_blocks_count(fs->super); blk++) {
2377 if (!ext2fs_fast_test_block_bitmap2(fs->block_map, blk)) {
2382 if ((count == fs->super->s_blocks_per_group) ||
2383 (blk == ext2fs_blocks_count(fs->super)-1)) {
2384 ext2fs_bg_free_blocks_count_set(fs, group++,
2390 total_free = EXT2FS_C2B(fs, total_free);
2391 ext2fs_free_blocks_count_set(fs->super, total_free);
2394 * Next, calculate the inode statistics
2401 /* Protect loop from wrap-around if s_inodes_count maxed */
2402 for (ino = 1; ino <= fs->super->s_inodes_count && ino > 0; ino++) {
2403 if (!ext2fs_fast_test_inode_bitmap2(fs->inode_map, ino)) {
2408 if ((count == fs->super->s_inodes_per_group) ||
2409 (ino == fs->super->s_inodes_count)) {
2410 ext2fs_bg_free_inodes_count_set(fs, group++,
2416 fs->super->s_free_inodes_count = total_free;
2417 ext2fs_mark_super_dirty(fs);
2421 #define list_for_each_safe(pos, pnext, head) \
2422 for (pos = (head)->next, pnext = pos->next; pos != (head); \
2423 pos = pnext, pnext = pos->next)
2425 static void free_blk_move_list(void)
2427 struct list_head *entry, *tmp;
2428 struct blk_move *bmv;
2430 list_for_each_safe(entry, tmp, &blk_move_list) {
2431 bmv = list_entry(entry, struct blk_move, list);
2433 ext2fs_free_mem(&bmv);
2438 static int resize_inode(ext2_filsys fs, unsigned long new_size)
2441 int new_ino_blks_per_grp;
2442 ext2fs_block_bitmap bmap;
2444 retval = ext2fs_read_inode_bitmap(fs);
2446 fputs(_("Failed to read inode bitmap\n"), stderr);
2449 retval = ext2fs_read_block_bitmap(fs);
2451 fputs(_("Failed to read block bitmap\n"), stderr);
2454 INIT_LIST_HEAD(&blk_move_list);
2457 new_ino_blks_per_grp = ext2fs_div_ceil(
2458 EXT2_INODES_PER_GROUP(fs->super)*
2462 /* We may change the file system.
2463 * Mark the file system as invalid so that
2464 * the user is prompted to run fsck.
2466 fs->super->s_state &= ~EXT2_VALID_FS;
2468 retval = ext2fs_allocate_block_bitmap(fs, _("blocks to be moved"),
2471 fputs(_("Failed to allocate block bitmap when "
2472 "increasing inode size\n"), stderr);
2475 retval = get_move_bitmaps(fs, new_ino_blks_per_grp, bmap);
2477 fputs(_("Not enough space to increase inode size \n"), stderr);
2480 retval = move_block(fs, bmap);
2482 fputs(_("Failed to relocate blocks during inode resize \n"),
2486 retval = inode_scan_and_fix(fs, bmap);
2490 retval = group_desc_scan_and_fix(fs, bmap);
2494 retval = expand_inode_table(fs, new_size);
2498 ext2fs_calculate_summary_stats(fs);
2500 fs->super->s_state |= EXT2_VALID_FS;
2501 /* mark super block and block bitmap as dirty */
2502 ext2fs_mark_super_dirty(fs);
2503 ext2fs_mark_bb_dirty(fs);
2506 free_blk_move_list();
2507 ext2fs_free_block_bitmap(bmap);
2512 free_blk_move_list();
2513 ext2fs_free_block_bitmap(bmap);
2514 fputs(_("Error in resizing the inode size.\n"
2515 "Run e2undo to undo the "
2516 "file system changes. \n"), stderr);
2521 static int tune2fs_setup_tdb(const char *name, io_manager *io_ptr)
2523 errcode_t retval = 0;
2524 const char *tdb_dir;
2526 char *dev_name, *tmp_name;
2530 * Configuration via a conf file would be
2533 profile_get_string(profile, "scratch_files",
2537 tmp_name = strdup(name);
2540 com_err(program_name, ENOMEM, "%s",
2541 _("Couldn't allocate memory for tdb filename\n"));
2544 dev_name = basename(tmp_name);
2546 tdb_dir = getenv("E2FSPROGS_UNDO_DIR");
2548 tdb_dir = "/var/lib/e2fsprogs";
2550 if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
2551 access(tdb_dir, W_OK))
2554 tdb_file = malloc(strlen(tdb_dir) + 9 + strlen(dev_name) + 7 + 1);
2557 sprintf(tdb_file, "%s/tune2fs-%s.e2undo", tdb_dir, dev_name);
2559 if ((unlink(tdb_file) < 0) && (errno != ENOENT)) {
2561 com_err(program_name, retval,
2562 _("while trying to delete %s"), tdb_file);
2567 set_undo_io_backing_manager(*io_ptr);
2568 *io_ptr = undo_io_manager;
2569 set_undo_io_backup_file(tdb_file);
2570 printf(_("To undo the tune2fs operation please run "
2571 "the command\n e2undo %s %s\n\n"),
2579 fs_update_journal_user(struct ext2_super_block *sb, __u8 old_uuid[UUID_SIZE])
2581 int retval, nr_users, start;
2582 journal_superblock_t *jsb;
2586 char uuid[UUID_STR_SIZE];
2587 char buf[SUPERBLOCK_SIZE];
2589 if (!(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) ||
2590 uuid_is_null(sb->s_journal_uuid))
2593 uuid_unparse(sb->s_journal_uuid, uuid);
2594 journal_path = blkid_get_devname(NULL, "UUID", uuid);
2598 retval = ext2fs_open2(journal_path, io_options,
2599 EXT2_FLAG_JOURNAL_DEV_OK | EXT2_FLAG_RW,
2600 0, 0, unix_io_manager, &jfs);
2602 com_err(program_name, retval,
2603 _("while trying to open %s"),
2608 retval = get_journal_sb(jfs, buf);
2610 if (retval == EXT2_ET_UNSUPP_FEATURE)
2611 fprintf(stderr, _("%s is not a journal device.\n"),
2616 jsb = (journal_superblock_t *) buf;
2617 /* Find the filesystem UUID */
2618 nr_users = ntohl(jsb->s_nr_users);
2620 j_uuid = journal_user(old_uuid, jsb->s_users, nr_users);
2621 if (j_uuid == NULL) {
2622 fputs(_("Filesystem's UUID not found on journal device.\n"),
2624 return EXT2_ET_LOAD_EXT_JOURNAL;
2627 memcpy(j_uuid, sb->s_uuid, UUID_SIZE);
2629 start = ext2fs_journal_sb_start(jfs->blocksize);
2630 /* Write back the journal superblock */
2631 retval = io_channel_write_blk64(jfs->io, start, -SUPERBLOCK_SIZE, buf);
2633 com_err(program_name, retval,
2634 "while writing journal superblock.");
2643 int main(int argc, char **argv)
2647 struct ext2_super_block *sb;
2648 io_manager io_ptr, io_ptr_orig = NULL;
2652 setlocale(LC_MESSAGES, "");
2653 setlocale(LC_CTYPE, "");
2654 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
2655 textdomain(NLS_CAT_NAME);
2656 set_com_err_gettext(gettext);
2659 program_name = *argv;
2660 add_error_table(&et_ext2_error_table);
2662 #ifdef CONFIG_BUILD_FINDFS
2663 if (strcmp(get_progname(argv[0]), "findfs") == 0)
2664 do_findfs(argc, argv);
2666 if (strcmp(get_progname(argv[0]), "e2label") == 0)
2667 parse_e2label_options(argc, argv);
2669 parse_tune2fs_options(argc, argv);
2671 #ifdef CONFIG_TESTIO_DEBUG
2672 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_DEBUG")) {
2673 io_ptr = test_io_manager;
2674 test_io_backing_manager = unix_io_manager;
2677 io_ptr = unix_io_manager;
2680 if ((open_flag & EXT2_FLAG_RW) == 0 || f_flag)
2681 open_flag |= EXT2_FLAG_SKIP_MMP;
2683 open_flag |= EXT2_FLAG_64BITS | EXT2_FLAG_JOURNAL_DEV_OK;
2685 /* keep the filesystem struct around to dump MMP data */
2686 open_flag |= EXT2_FLAG_NOFREE_ON_ERROR;
2688 retval = ext2fs_open2(device_name, io_options, open_flag,
2691 com_err(program_name, retval,
2692 _("while trying to open %s"),
2694 if (retval == EXT2_ET_MMP_FSCK_ON ||
2695 retval == EXT2_ET_MMP_UNKNOWN_SEQ)
2696 dump_mmp_msg(fs->mmp_buf,
2697 _("If you are sure the filesystem "
2698 "is not in use on any node, run:\n"
2699 "'tune2fs -f -E clear_mmp {device}'\n"));
2700 else if (retval == EXT2_ET_MMP_FAILED)
2701 dump_mmp_msg(fs->mmp_buf, NULL);
2702 else if (retval == EXT2_ET_MMP_MAGIC_INVALID)
2704 _("MMP block magic is bad. Try to fix it by "
2705 "running:\n'e2fsck -f %s'\n"), device_name);
2706 else if (retval == EXT2_ET_BAD_MAGIC)
2707 check_plausibility(device_name, CHECK_FS_EXIST, NULL);
2708 else if (retval != EXT2_ET_MMP_FAILED)
2709 fprintf(stderr, "%s",
2710 _("Couldn't find valid filesystem superblock.\n"));
2715 if (EXT2_HAS_INCOMPAT_FEATURE(fs->super,
2716 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
2717 fprintf(stderr, "%s", _("Cannot modify a journal device.\n"));
2721 fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE;
2723 if (I_flag && !io_ptr_orig) {
2725 * Check the inode size is right so we can issue an
2726 * error message and bail before setting up the tdb
2729 if (new_inode_size == EXT2_INODE_SIZE(fs->super)) {
2730 fprintf(stderr, _("The inode size is already %lu\n"),
2735 if (new_inode_size < EXT2_INODE_SIZE(fs->super)) {
2736 fprintf(stderr, "%s",
2737 _("Shrinking inode size is not supported\n"));
2741 if (new_inode_size > fs->blocksize) {
2742 fprintf(stderr, _("Invalid inode size %lu (max %d)\n"),
2743 new_inode_size, fs->blocksize);
2749 * If inode resize is requested use the
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 (sb->s_feature_ro_compat &
2842 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER) {
2843 fputs(_("\nThe filesystem already has sparse "
2844 "superblocks.\n"), stderr);
2845 } else if (sb->s_feature_incompat &
2846 EXT2_FEATURE_INCOMPAT_META_BG) {
2847 fputs(_("\nSetting the sparse superblock flag not "
2848 "supported\nfor filesystems with "
2849 "the meta_bg feature enabled.\n"),
2854 sb->s_feature_ro_compat |=
2855 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
2856 sb->s_state &= ~EXT2_VALID_FS;
2857 ext2fs_mark_super_dirty(fs);
2858 printf(_("\nSparse superblock flag set. %s"),
2863 fputs(_("\nClearing the sparse superblock flag not supported.\n"),
2869 sb->s_lastcheck = last_check_time;
2870 ext2fs_mark_super_dirty(fs);
2871 printf(_("Setting time filesystem last checked to %s\n"),
2872 ctime(&last_check_time));
2875 sb->s_def_resuid = resuid;
2876 ext2fs_mark_super_dirty(fs);
2877 printf(_("Setting reserved blocks uid to %lu\n"), resuid);
2880 if (strlen(new_label) > sizeof(sb->s_volume_name))
2881 fputs(_("Warning: label too long, truncating.\n"),
2883 memset(sb->s_volume_name, 0, sizeof(sb->s_volume_name));
2884 strncpy(sb->s_volume_name, new_label,
2885 sizeof(sb->s_volume_name));
2886 ext2fs_mark_super_dirty(fs);
2889 memset(sb->s_last_mounted, 0, sizeof(sb->s_last_mounted));
2890 strncpy(sb->s_last_mounted, new_last_mounted,
2891 sizeof(sb->s_last_mounted));
2892 ext2fs_mark_super_dirty(fs);
2895 rc = update_mntopts(fs, mntopts_cmd);
2900 rc = update_feature_set(fs, features_cmd);
2905 rc = parse_extended_opts(fs, extended_cmd);
2908 if (clear_mmp && !f_flag) {
2909 fputs(_("Error in using clear_mmp. "
2910 "It must be used with -f\n"),
2916 rc = ext2fs_mmp_clear(fs);
2919 if (journal_size || journal_device) {
2920 rc = add_journal(fs);
2926 if (mount_flags & EXT2_MF_MOUNTED) {
2927 fputs(_("The quota feature may only be changed when "
2928 "the filesystem is unmounted.\n"), stderr);
2932 handle_quota_options(fs);
2938 char buf[SUPERBLOCK_SIZE];
2939 __u8 old_uuid[UUID_SIZE];
2941 if (ext2fs_has_group_desc_csum(fs)) {
2943 * Changing the UUID requires rewriting all metadata,
2944 * which can race with a mounted fs. Don't allow that.
2946 if (mount_flags & EXT2_MF_MOUNTED) {
2947 fputs(_("The UUID may only be "
2948 "changed when the filesystem is "
2949 "unmounted.\n"), stderr);
2952 if (check_fsck_needed(fs))
2956 * Determine if the block group checksums are
2957 * correct so we know whether or not to set
2960 for (i = 0; i < fs->group_desc_count; i++)
2961 if (!ext2fs_group_desc_csum_verify(fs, i))
2963 if (i >= fs->group_desc_count)
2967 memcpy(old_uuid, sb->s_uuid, UUID_SIZE);
2968 if ((strcasecmp(new_UUID, "null") == 0) ||
2969 (strcasecmp(new_UUID, "clear") == 0)) {
2970 uuid_clear(sb->s_uuid);
2971 } else if (strcasecmp(new_UUID, "time") == 0) {
2972 uuid_generate_time(sb->s_uuid);
2973 } else if (strcasecmp(new_UUID, "random") == 0) {
2974 uuid_generate(sb->s_uuid);
2975 } else if (uuid_parse(new_UUID, sb->s_uuid)) {
2976 com_err(program_name, 0, "%s",
2977 _("Invalid UUID format\n"));
2981 ext2fs_init_csum_seed(fs);
2983 for (i = 0; i < fs->group_desc_count; i++)
2984 ext2fs_group_desc_csum_set(fs, i);
2985 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
2988 /* If this is a journal dev, we need to copy UUID into jsb */
2989 if (!(rc = get_journal_sb(fs, buf))) {
2990 journal_superblock_t *jsb;
2992 jsb = (journal_superblock_t *) buf;
2993 fputs(_("Need to update journal superblock.\n"), stdout);
2994 memcpy(jsb->s_uuid, sb->s_uuid, sizeof(sb->s_uuid));
2996 /* Writeback the journal superblock */
2997 if ((rc = io_channel_write_blk64(fs->io,
2998 ext2fs_journal_sb_start(fs->blocksize),
2999 -SUPERBLOCK_SIZE, buf)))
3001 } else if (rc != EXT2_ET_UNSUPP_FEATURE)
3004 rc = 0; /** Reset rc to avoid ext2fs_mmp_stop() */
3006 if ((rc = fs_update_journal_user(sb, old_uuid)))
3010 ext2fs_mark_super_dirty(fs);
3011 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
3012 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
3013 rewrite_checksums = 1;
3017 if (mount_flags & EXT2_MF_MOUNTED) {
3018 fputs(_("The inode size may only be "
3019 "changed when the filesystem is "
3020 "unmounted.\n"), stderr);
3024 if (fs->super->s_feature_incompat &
3025 EXT4_FEATURE_INCOMPAT_FLEX_BG) {
3026 fputs(_("Changing the inode size not supported for "
3027 "filesystems with the flex_bg\n"
3028 "feature enabled.\n"),
3034 * We want to update group descriptor also
3035 * with the new free inode count
3037 if (rewrite_checksums)
3038 fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
3039 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
3040 retval = resize_inode(fs, new_inode_size);
3041 if (rewrite_checksums)
3042 fs->flags &= ~EXT2_FLAG_IGNORE_CSUM_ERRORS;
3044 printf(_("Setting inode size %lu\n"),
3046 rewrite_checksums = 1;
3048 printf("%s", _("Failed to change inode size\n"));
3054 if (rewrite_checksums)
3055 rewrite_metadata_checksums(fs);
3060 sb->s_raid_stride = stride;
3061 ext2fs_mark_super_dirty(fs);
3062 printf(_("Setting stride size to %d\n"), stride);
3064 if (stripe_width_set) {
3065 sb->s_raid_stripe_width = stripe_width;
3066 ext2fs_mark_super_dirty(fs);
3067 printf(_("Setting stripe width to %d\n"), stripe_width);
3069 if (ext_mount_opts) {
3070 strncpy((char *)(fs->super->s_mount_opts), ext_mount_opts,
3071 sizeof(fs->super->s_mount_opts));
3072 fs->super->s_mount_opts[sizeof(fs->super->s_mount_opts)-1] = 0;
3073 ext2fs_mark_super_dirty(fs);
3074 printf(_("Setting extended default mount options to '%s'\n"),
3076 free(ext_mount_opts);
3079 remove_error_table(&et_ext2_error_table);
3083 ext2fs_mmp_stop(fs);
3087 convert_64bit(fs, feature_64bit);
3088 return (ext2fs_close_free(&fs) ? 1 : 0);