*/
#define _XOPEN_SOURCE 600 /* for inclusion of strptime() */
-#define _BSD_SOURCE /* for inclusion of strcasecmp() */
#include "config.h"
#include <fcntl.h>
#include <grp.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
+#ifdef HAVE_STRINGS_H
+#include <strings.h> /* for strcasecmp() */
+#else
+#define _BSD_SOURCE /* for inclusion of strcasecmp() via <string.h> */
+#endif
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "jfs_user.h"
#include "util.h"
#include "blkid/blkid.h"
-#include "quota/mkquota.h"
+#include "quota/quotaio.h"
#include "../version.h"
#include "nls-enable.h"
static unsigned long new_inode_size;
static char *ext_mount_opts;
static int usrquota, grpquota;
+static int rewrite_checksums;
int journal_size, journal_flags;
char *journal_device;
+static blk64_t journal_location = ~0LL;
static struct list_head blk_move_list;
static const char *please_fsck = N_("Please run e2fsck on the filesystem.\n");
+static const char *please_dir_fsck =
+ N_("Please run e2fsck -D on the filesystem.\n");
#ifdef CONFIG_BUILD_FINDFS
void do_findfs(int argc, char **argv);
"\t[-r reserved_blocks_count] [-u user] [-C mount_count] "
"[-L volume_label]\n"
"\t[-M last_mounted_dir] [-O [^]feature[,...]]\n"
+#ifdef CONFIG_QUOTA
+ "\t[-Q quota_options]\n"
+#endif
"\t[-E extended-option[,...]] [-T last_check_time] "
"[-U UUID]\n\t[ -I new_inode_size ] device\n"), program_name);
exit(1);
EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
EXT4_FEATURE_RO_COMPAT_GDT_CSUM |
+ EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER |
#ifdef CONFIG_QUOTA
EXT4_FEATURE_RO_COMPAT_QUOTA |
#endif
- EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
+ EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
};
static __u32 clear_ok_features[3] = {
EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
+ EXT4_FEATURE_RO_COMPAT_GDT_CSUM |
#ifdef CONFIG_QUOTA
EXT4_FEATURE_RO_COMPAT_QUOTA |
#endif
- EXT4_FEATURE_RO_COMPAT_GDT_CSUM
+ EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
};
/*
EXT2_FLAG_JOURNAL_DEV_OK, 0,
fs->blocksize, io_ptr, &jfs);
if (retval) {
- com_err(program_name, retval,
+ com_err(program_name, retval, "%s",
_("while trying to open external journal"));
goto no_valid_journal;
}
/* Get the journal superblock */
if ((retval = io_channel_read_blk64(jfs->io, 1, -1024, buf))) {
- com_err(program_name, retval,
+ com_err(program_name, retval, "%s",
_("while reading journal superblock"));
goto no_valid_journal;
}
retval = ext2fs_read_inode(fs, ino, &inode);
if (retval) {
- com_err(program_name, retval,
+ com_err(program_name, retval, "%s",
_("while reading journal inode"));
return retval;
}
if (ino == EXT2_JOURNAL_INO) {
retval = ext2fs_read_bitmaps(fs);
if (retval) {
- com_err(program_name, retval,
+ com_err(program_name, retval, "%s",
_("while reading bitmaps"));
return retval;
}
BLOCK_FLAG_READ_ONLY, NULL,
release_blocks_proc, NULL);
if (retval) {
- com_err(program_name, retval,
+ com_err(program_name, retval, "%s",
_("while clearing journal inode"));
return retval;
}
inode.i_flags &= ~EXT2_IMMUTABLE_FL;
retval = ext2fs_write_inode(fs, ino, &inode);
if (retval) {
- com_err(program_name, retval,
+ com_err(program_name, retval, "%s",
_("while writing journal inode"));
return retval;
}
return 0;
}
+static int check_fsck_needed(ext2_filsys fs)
+{
+ if (fs->super->s_state & EXT2_VALID_FS)
+ return 0;
+ printf("\n%s\n", _(please_fsck));
+ if (mount_flags & EXT2_MF_READONLY)
+ printf("%s", _("(and reboot afterwards!)\n"));
+ return 1;
+}
+
+static void request_dir_fsck_afterwards(ext2_filsys fs)
+{
+ static int requested;
+
+ if (requested++)
+ return;
+ fs->super->s_state &= ~EXT2_VALID_FS;
+ printf("\n%s\n", _(please_dir_fsck));
+ if (mount_flags & EXT2_MF_READONLY)
+ printf("%s", _("(and reboot afterwards!)\n"));
+}
+
static void request_fsck_afterwards(ext2_filsys fs)
{
static int requested = 0;
fs->super->s_state &= ~EXT2_VALID_FS;
printf("\n%s\n", _(please_fsck));
if (mount_flags & EXT2_MF_READONLY)
- printf(_("(and reboot afterwards!)\n"));
+ printf("%s", _("(and reboot afterwards!)\n"));
+}
+
+/* Rewrite extents */
+static errcode_t rewrite_extents(ext2_filsys fs, ext2_ino_t ino,
+ struct ext2_inode *inode)
+{
+ ext2_extent_handle_t handle;
+ struct ext2fs_extent extent;
+ int op = EXT2_EXTENT_ROOT;
+ errcode_t errcode;
+
+ if (!(inode->i_flags & EXT4_EXTENTS_FL))
+ return 0;
+
+ errcode = ext2fs_extent_open(fs, ino, &handle);
+ if (errcode)
+ return errcode;
+
+ while (1) {
+ errcode = ext2fs_extent_get(handle, op, &extent);
+ if (errcode)
+ break;
+
+ /* Root node is in the separately checksummed inode */
+ if (op == EXT2_EXTENT_ROOT) {
+ op = EXT2_EXTENT_NEXT;
+ continue;
+ }
+ op = EXT2_EXTENT_NEXT;
+
+ /* Only visit the first extent in each extent block */
+ if (extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT)
+ continue;
+ errcode = ext2fs_extent_replace(handle, 0, &extent);
+ if (errcode)
+ break;
+ }
+
+ /* Ok if we run off the end */
+ if (errcode == EXT2_ET_EXTENT_NO_NEXT)
+ errcode = 0;
+ return errcode;
+}
+
+/*
+ * Rewrite directory blocks with checksums
+ */
+struct rewrite_dir_context {
+ char *buf;
+ errcode_t errcode;
+ ext2_ino_t dir;
+ int is_htree;
+};
+
+static int rewrite_dir_block(ext2_filsys fs,
+ blk64_t *blocknr,
+ e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
+ blk64_t ref_block EXT2FS_ATTR((unused)),
+ int ref_offset EXT2FS_ATTR((unused)),
+ void *priv_data)
+{
+ struct ext2_dx_countlimit *dcl = NULL;
+ struct rewrite_dir_context *ctx = priv_data;
+ int dcl_offset, changed = 0;
+
+ ctx->errcode = ext2fs_read_dir_block4(fs, *blocknr, ctx->buf, 0,
+ ctx->dir);
+ if (ctx->errcode)
+ return BLOCK_ABORT;
+
+ /* if htree node... */
+ if (ctx->is_htree)
+ ext2fs_get_dx_countlimit(fs, (struct ext2_dir_entry *)ctx->buf,
+ &dcl, &dcl_offset);
+ if (dcl) {
+ if (!EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
+ EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
+ /* Ensure limit is the max size */
+ int max_entries = (fs->blocksize - dcl_offset) /
+ sizeof(struct ext2_dx_entry);
+ if (ext2fs_le16_to_cpu(dcl->limit) != max_entries) {
+ changed = 1;
+ dcl->limit = ext2fs_cpu_to_le16(max_entries);
+ }
+ } else {
+ /* If htree block is full then rebuild the dir */
+ if (ext2fs_le16_to_cpu(dcl->count) ==
+ ext2fs_le16_to_cpu(dcl->limit)) {
+ request_dir_fsck_afterwards(fs);
+ return 0;
+ }
+ /*
+ * Ensure dcl->limit is small enough to leave room for
+ * the checksum tail.
+ */
+ int max_entries = (fs->blocksize - (dcl_offset +
+ sizeof(struct ext2_dx_tail))) /
+ sizeof(struct ext2_dx_entry);
+ if (ext2fs_le16_to_cpu(dcl->limit) != max_entries)
+ dcl->limit = ext2fs_cpu_to_le16(max_entries);
+ /* Always rewrite checksum */
+ changed = 1;
+ }
+ } else {
+ unsigned int rec_len, name_size;
+ char *top = ctx->buf + fs->blocksize;
+ struct ext2_dir_entry *de = (struct ext2_dir_entry *)ctx->buf;
+ struct ext2_dir_entry *last_de = NULL, *penultimate_de = NULL;
+
+ /* Find last and penultimate dirent */
+ while ((char *)de < top) {
+ penultimate_de = last_de;
+ last_de = de;
+ ctx->errcode = ext2fs_get_rec_len(fs, de, &rec_len);
+ if (!ctx->errcode && !rec_len)
+ ctx->errcode = EXT2_ET_DIR_CORRUPTED;
+ if (ctx->errcode)
+ return BLOCK_ABORT;
+ de = (struct ext2_dir_entry *)(((char *)de) + rec_len);
+ }
+ ctx->errcode = ext2fs_get_rec_len(fs, last_de, &rec_len);
+ if (ctx->errcode)
+ return BLOCK_ABORT;
+ name_size = ext2fs_dirent_name_len(last_de);
+
+ if (!EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
+ EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
+ if (!penultimate_de)
+ return 0;
+ if (last_de->inode ||
+ name_size ||
+ rec_len != sizeof(struct ext2_dir_entry_tail))
+ return 0;
+ /*
+ * The last dirent is unused and the right length to
+ * have stored a checksum. Erase it.
+ */
+ ctx->errcode = ext2fs_get_rec_len(fs, penultimate_de,
+ &rec_len);
+ if (!rec_len)
+ ctx->errcode = EXT2_ET_DIR_CORRUPTED;
+ if (ctx->errcode)
+ return BLOCK_ABORT;
+ ext2fs_set_rec_len(fs, rec_len +
+ sizeof(struct ext2_dir_entry_tail),
+ penultimate_de);
+ changed = 1;
+ } else {
+ unsigned csum_size = sizeof(struct ext2_dir_entry_tail);
+ struct ext2_dir_entry_tail *t;
+
+ /*
+ * If the last dirent looks like the tail, just update
+ * the checksum.
+ */
+ if (!last_de->inode &&
+ rec_len == csum_size) {
+ t = (struct ext2_dir_entry_tail *)last_de;
+ t->det_reserved_name_len =
+ EXT2_DIR_NAME_LEN_CSUM;
+ changed = 1;
+ goto out;
+ }
+ if (name_size & 3)
+ name_size = (name_size & ~3) + 4;
+ /* If there's not enough space for the tail, e2fsck */
+ if (rec_len <= (8 + name_size + csum_size)) {
+ request_dir_fsck_afterwards(fs);
+ return 0;
+ }
+ /* Shorten that last de and insert the tail */
+ ext2fs_set_rec_len(fs, rec_len - csum_size, last_de);
+ t = EXT2_DIRENT_TAIL(ctx->buf, fs->blocksize);
+ ext2fs_initialize_dirent_tail(fs, t);
+
+ /* Always update checksum */
+ changed = 1;
+ }
+ }
+
+out:
+ if (!changed)
+ return 0;
+
+ ctx->errcode = ext2fs_write_dir_block4(fs, *blocknr, ctx->buf,
+ 0, ctx->dir);
+ if (ctx->errcode)
+ return BLOCK_ABORT;
+
+ return 0;
+}
+
+static errcode_t rewrite_directory(ext2_filsys fs, ext2_ino_t dir,
+ struct ext2_inode *inode)
+{
+ errcode_t retval;
+ struct rewrite_dir_context ctx;
+
+ retval = ext2fs_get_mem(fs->blocksize, &ctx.buf);
+ if (retval)
+ return retval;
+
+ ctx.is_htree = (inode->i_flags & EXT2_INDEX_FL);
+ ctx.dir = dir;
+ ctx.errcode = 0;
+ retval = ext2fs_block_iterate3(fs, dir, BLOCK_FLAG_READ_ONLY |
+ BLOCK_FLAG_DATA_ONLY,
+ 0, rewrite_dir_block, &ctx);
+
+ ext2fs_free_mem(&ctx.buf);
+ if (retval)
+ return retval;
+
+ return ctx.errcode;
+}
+
+/*
+ * Forcibly set checksums in all inodes.
+ */
+static void rewrite_inodes(ext2_filsys fs)
+{
+ int length = EXT2_INODE_SIZE(fs->super);
+ struct ext2_inode *inode, *zero;
+ char *ea_buf;
+ ext2_inode_scan scan;
+ errcode_t retval;
+ ext2_ino_t ino;
+ blk64_t file_acl_block;
+ int inode_dirty;
+
+ if (fs->super->s_creator_os != EXT2_OS_LINUX)
+ return;
+
+ retval = ext2fs_open_inode_scan(fs, 0, &scan);
+ if (retval) {
+ com_err("set_csum", retval, "while opening inode scan");
+ exit(1);
+ }
+
+ retval = ext2fs_get_mem(length, &inode);
+ if (retval) {
+ com_err("set_csum", retval, "while allocating memory");
+ exit(1);
+ }
+
+ retval = ext2fs_get_memzero(length, &zero);
+ if (retval) {
+ com_err("set_csum", retval, "while allocating memory");
+ exit(1);
+ }
+
+ retval = ext2fs_get_mem(fs->blocksize, &ea_buf);
+ if (retval) {
+ com_err("set_csum", retval, "while allocating memory");
+ exit(1);
+ }
+
+ do {
+ retval = ext2fs_get_next_inode_full(scan, &ino, inode, length);
+ if (retval) {
+ com_err("set_csum", retval, "while getting next inode");
+ exit(1);
+ }
+ if (!ino)
+ break;
+ if (ext2fs_test_inode_bitmap2(fs->inode_map, ino)) {
+ inode_dirty = 1;
+ } else {
+ if (memcmp(inode, zero, length) != 0) {
+ memset(inode, 0, length);
+ inode_dirty = 1;
+ } else {
+ inode_dirty = 0;
+ }
+ }
+
+ if (inode_dirty) {
+ retval = ext2fs_write_inode_full(fs, ino, inode,
+ length);
+ if (retval) {
+ com_err("set_csum", retval, "while writing "
+ "inode");
+ exit(1);
+ }
+ }
+
+ retval = rewrite_extents(fs, ino, inode);
+ if (retval) {
+ com_err("rewrite_extents", retval,
+ "while rewriting extents");
+ exit(1);
+ }
+
+ if (LINUX_S_ISDIR(inode->i_mode) &&
+ ext2fs_inode_has_valid_blocks2(fs, inode)) {
+ retval = rewrite_directory(fs, ino, inode);
+ if (retval) {
+ com_err("rewrite_directory", retval,
+ "while rewriting directories");
+ exit(1);
+ }
+ }
+
+ file_acl_block = ext2fs_file_acl_block(fs, inode);
+ if (!file_acl_block)
+ continue;
+ retval = ext2fs_read_ext_attr3(fs, file_acl_block, ea_buf, ino);
+ if (retval) {
+ com_err("rewrite_eablock", retval,
+ "while rewriting extended attribute");
+ exit(1);
+ }
+ retval = ext2fs_write_ext_attr3(fs, file_acl_block, ea_buf,
+ ino);
+ if (retval) {
+ com_err("rewrite_eablock", retval,
+ "while rewriting extended attribute");
+ exit(1);
+ }
+ } while (ino);
+
+ ext2fs_free_mem(&zero);
+ ext2fs_free_mem(&inode);
+ ext2fs_free_mem(&ea_buf);
+ ext2fs_close_inode_scan(scan);
+}
+
+static void rewrite_metadata_checksums(ext2_filsys fs)
+{
+ errcode_t retval;
+ dgrp_t i;
+
+ fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
+ ext2fs_init_csum_seed(fs);
+ for (i = 0; i < fs->group_desc_count; i++)
+ ext2fs_group_desc_csum_set(fs, i);
+ retval = ext2fs_read_bitmaps(fs);
+ if (retval) {
+ com_err("rewrite_metadata_checksums", retval,
+ "while reading bitmaps");
+ exit(1);
+ }
+ rewrite_inodes(fs);
+ ext2fs_mark_ib_dirty(fs);
+ ext2fs_mark_bb_dirty(fs);
+ ext2fs_mmp_update2(fs, 1);
+ fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
+ fs->flags &= ~EXT2_FLAG_IGNORE_CSUM_ERRORS;
+ if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
+ EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
+ fs->super->s_checksum_type = EXT2_CRC32C_CHKSUM;
+ else
+ fs->super->s_checksum_type = 0;
+ ext2fs_mark_super_dirty(fs);
+}
+
+static void enable_uninit_bg(ext2_filsys fs)
+{
+ struct ext2_group_desc *gd;
+ dgrp_t i;
+
+ for (i = 0; i < fs->group_desc_count; i++) {
+ gd = ext2fs_group_desc(fs, fs->group_desc, i);
+ gd->bg_itable_unused = 0;
+ gd->bg_flags = EXT2_BG_INODE_ZEROED;
+ ext2fs_group_desc_csum_set(fs, i);
+ }
+ fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
+}
+
+static errcode_t zero_empty_inodes(ext2_filsys fs)
+{
+ int length = EXT2_INODE_SIZE(fs->super);
+ struct ext2_inode *inode;
+ ext2_inode_scan scan;
+ errcode_t retval;
+ ext2_ino_t ino;
+
+ retval = ext2fs_open_inode_scan(fs, 0, &scan);
+ if (retval)
+ goto out;
+
+ retval = ext2fs_get_mem(length, &inode);
+ if (retval)
+ goto out;
+
+ do {
+ retval = ext2fs_get_next_inode_full(scan, &ino, inode, length);
+ if (retval)
+ goto out;
+ if (!ino)
+ break;
+ if (!ext2fs_test_inode_bitmap2(fs->inode_map, ino)) {
+ memset(inode, 0, length);
+ retval = ext2fs_write_inode_full(fs, ino, inode,
+ length);
+ if (retval)
+ goto out;
+ }
+ } while (1);
+
+out:
+ ext2fs_free_mem(&inode);
+ ext2fs_close_inode_scan(scan);
+ return retval;
+}
+
+static void disable_uninit_bg(ext2_filsys fs, __u32 csum_feature_flag)
+{
+ struct ext2_group_desc *gd;
+ dgrp_t i;
+ errcode_t retval;
+ blk64_t b, c, d;
+ int has_super;
+
+ /* Load bitmaps to ensure that the uninit ones get written out */
+ fs->super->s_feature_ro_compat |= csum_feature_flag;
+ retval = ext2fs_read_bitmaps(fs);
+ if (retval) {
+ com_err("disable_uninit_bg", retval,
+ "while reading bitmaps");
+ return;
+ }
+ ext2fs_mark_ib_dirty(fs);
+ ext2fs_mark_bb_dirty(fs);
+ fs->super->s_feature_ro_compat &= ~csum_feature_flag;
+
+ /* If we're only turning off uninit_bg, zero the inodes */
+ if (csum_feature_flag == EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
+ retval = zero_empty_inodes(fs);
+ if (retval) {
+ com_err("disable_uninit_bg", retval,
+ "while zeroing unused inodes");
+ request_fsck_afterwards(fs);
+ }
+ }
+
+ /* The bbitmap is zeroed; we must mark group metadata blocks in use */
+ for (i = 0; i < fs->group_desc_count; i++) {
+ b = ext2fs_block_bitmap_loc(fs, i);
+ ext2fs_mark_block_bitmap2(fs->block_map, b);
+ b = ext2fs_inode_bitmap_loc(fs, i);
+ ext2fs_mark_block_bitmap2(fs->block_map, b);
+
+ retval = ext2fs_super_and_bgd_loc2(fs, i, &b, &c, &d, NULL);
+ if (retval == 0 && b)
+ ext2fs_mark_block_bitmap2(fs->block_map, b);
+ if (retval == 0 && c)
+ ext2fs_mark_block_bitmap2(fs->block_map, c);
+ if (retval == 0 && d)
+ ext2fs_mark_block_bitmap2(fs->block_map, d);
+ if (retval) {
+ com_err("disable_uninit_bg", retval,
+ "while initializing block bitmaps");
+ request_fsck_afterwards(fs);
+ }
+
+ gd = ext2fs_group_desc(fs, fs->group_desc, i);
+ gd->bg_itable_unused = 0;
+ gd->bg_flags = 0;
+ ext2fs_group_desc_csum_set(fs, i);
+ }
+ fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
+ ext2fs_mark_super_dirty(fs);
}
/*
static int update_feature_set(ext2_filsys fs, char *features)
{
struct ext2_super_block *sb = fs->super;
- struct ext2_group_desc *gd;
__u32 old_features[3];
- int i, type_err;
+ int type_err;
unsigned int mask_err;
#define FEATURE_ON(type, mask) (!(old_features[(type)] & (mask)) && \
"read-only.\n"), stderr);
return 1;
}
- if (sb->s_feature_incompat &
- EXT3_FEATURE_INCOMPAT_RECOVER) {
+ if ((sb->s_feature_incompat &
+ EXT3_FEATURE_INCOMPAT_RECOVER) &&
+ f_flag < 2) {
fputs(_("The needs_recovery flag is set. "
"Please run e2fsck before clearing\n"
"the has_journal flag.\n"), stderr);
return 1;
}
}
+
+ if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT,
+ EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
+ if (sb->s_feature_incompat &
+ EXT2_FEATURE_INCOMPAT_META_BG) {
+ fputs(_("Setting filesystem feature 'sparse_super' "
+ "not supported\nfor filesystems with "
+ "the meta_bg feature enabled.\n"),
+ stderr);
+ return 1;
+ }
+ }
+
if (FEATURE_ON(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_MMP)) {
int error;
"match. expected: %x, actual: %x\n"),
EXT4_MMP_MAGIC, mmp_cmp->mmp_magic);
else
- com_err(program_name, error,
+ com_err(program_name, error, "%s",
_("while reading MMP block."));
goto mmp_error;
}
/* We need to force out the group descriptors as well */
fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
- ext2fs_block_alloc_stats(fs, sb->s_mmp_block, -1);
+ ext2fs_block_alloc_stats2(fs, sb->s_mmp_block, -1);
mmp_error:
sb->s_mmp_block = 0;
sb->s_mmp_update_interval = 0;
}
if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT,
- EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
- for (i = 0; i < fs->group_desc_count; i++) {
- gd = ext2fs_group_desc(fs, fs->group_desc, i);
- gd->bg_itable_unused = 0;
- gd->bg_flags = EXT2_BG_INODE_ZEROED;
- ext2fs_group_desc_csum_set(fs, i);
- }
- fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
+ EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
+ if (check_fsck_needed(fs))
+ exit(1);
+ rewrite_checksums = 1;
+ /* metadata_csum supersedes uninit_bg */
+ fs->super->s_feature_ro_compat &=
+ ~EXT4_FEATURE_RO_COMPAT_GDT_CSUM;
+
+ /* if uninit_bg was previously off, rewrite group desc */
+ if (!(old_features[E2P_FEATURE_RO_INCOMPAT] &
+ EXT4_FEATURE_RO_COMPAT_GDT_CSUM))
+ enable_uninit_bg(fs);
+
+ /*
+ * Since metadata_csum supersedes uninit_bg, pretend like
+ * uninit_bg has been off all along.
+ */
+ old_features[E2P_FEATURE_RO_INCOMPAT] &=
+ ~EXT4_FEATURE_RO_COMPAT_GDT_CSUM;
}
if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
- EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
- for (i = 0; i < fs->group_desc_count; i++) {
- gd = ext2fs_group_desc(fs, fs->group_desc, i);
- if ((gd->bg_flags & EXT2_BG_INODE_ZEROED) == 0) {
- /*
- * XXX what we really should do is zap
- * uninitialized inode tables instead.
- */
- request_fsck_afterwards(fs);
- break;
- }
- gd->bg_itable_unused = 0;
- gd->bg_flags = 0;
- gd->bg_checksum = 0;
- }
- fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
+ EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
+ if (check_fsck_needed(fs))
+ exit(1);
+ rewrite_checksums = 1;
+ /*
+ * If we're turning off metadata_csum and not turning on
+ * uninit_bg, rewrite group desc.
+ */
+ if (!(fs->super->s_feature_ro_compat &
+ EXT4_FEATURE_RO_COMPAT_GDT_CSUM))
+ disable_uninit_bg(fs,
+ EXT4_FEATURE_RO_COMPAT_METADATA_CSUM);
+ else
+ /*
+ * metadata_csum previously provided uninit_bg, so if
+ * we're also setting the uninit_bg feature bit,
+ * pretend like it was previously enabled. Checksums
+ * will be rewritten with crc16 later.
+ */
+ old_features[E2P_FEATURE_RO_INCOMPAT] |=
+ EXT4_FEATURE_RO_COMPAT_GDT_CSUM;
+ }
+
+ if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT,
+ EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
+ /* Do not enable uninit_bg when metadata_csum enabled */
+ if (fs->super->s_feature_ro_compat &
+ EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)
+ fs->super->s_feature_ro_compat &=
+ ~EXT4_FEATURE_RO_COMPAT_GDT_CSUM;
+ else
+ enable_uninit_bg(fs);
}
+ if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
+ EXT4_FEATURE_RO_COMPAT_GDT_CSUM))
+ disable_uninit_bg(fs,
+ EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
+
if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT,
EXT4_FEATURE_RO_COMPAT_QUOTA)) {
/*
goto err;
}
if (journal_device) {
- check_plausibility(journal_device);
+ if (!check_plausibility(journal_device, CHECK_BLOCK_DEV,
+ NULL))
+ proceed_question(-1);
check_mount(journal_device, 0, _("journal"));
#ifdef CONFIG_TESTIO_DEBUG
if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
fflush(stdout);
retval = ext2fs_add_journal_device(fs, jfs);
- ext2fs_close(jfs);
+ ext2fs_close_free(&jfs);
if (retval) {
com_err(program_name, retval,
_("while adding filesystem to journal on %s"),
fflush(stdout);
journal_blocks = figure_journal_size(journal_size, fs);
- retval = ext2fs_add_journal_inode(fs, journal_blocks,
- journal_flags);
+ if (journal_location_string)
+ journal_location =
+ parse_num_blocks2(journal_location_string,
+ fs->super->s_log_block_size);
+ retval = ext2fs_add_journal_inode2(fs, journal_blocks,
+ journal_location,
+ journal_flags);
if (retval) {
fprintf(stderr, "\n");
- com_err(program_name, retval,
+ com_err(program_name, retval, "%s",
_("\n\twhile trying to create journal file"));
return retval;
} else
return 1;
}
-void handle_quota_options(ext2_filsys fs)
+static void handle_quota_options(ext2_filsys fs)
{
quota_ctx_t qctx;
ext2_ino_t qf_ino;
quota_release_context(&qctx);
if ((usrquota == QOPT_ENABLE) || (grpquota == QOPT_ENABLE)) {
- fprintf(stderr, _("\nWarning: the quota feature is still "
- "under development\n"
- "See https://ext4.wiki.kernel.org/"
- "index.php/Quota for more information\n\n"));
fs->super->s_feature_ro_compat |= EXT4_FEATURE_RO_COMPAT_QUOTA;
ext2fs_mark_super_dirty(fs);
} else if (!fs->super->s_usr_quota_inum &&
return;
}
-void parse_quota_opts(const char *opts)
+#ifdef CONFIG_QUOTA
+static void parse_quota_opts(const char *opts)
{
char *buf, *token, *next, *p;
int len;
}
free(buf);
}
-
-
+#endif
static void parse_e2label_options(int argc, char ** argv)
{
char *tmp;
struct group *gr;
struct passwd *pw;
+ char optstring[100] = "c:e:fg:i:jlm:o:r:s:u:C:E:I:J:L:M:O:T:U:";
+#ifdef CONFIG_QUOTA
+ strcat(optstring, "Q:");
+#endif
open_flag = 0;
printf("tune2fs %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
- while ((c = getopt(argc, argv, "c:e:fg:i:jlm:o:r:s:u:C:E:I:J:L:M:O:Q:T:U:")) != EOF)
+ while ((c = getopt(argc, argv, optstring)) != EOF)
switch (c) {
case 'c':
max_mount_count = strtol(optarg, &tmp, 0);
open_flag |= EXT2_FLAG_RW;
break;
case 'f': /* Force */
- f_flag = 1;
+ f_flag++;
break;
case 'g':
resgid = strtoul(optarg, &tmp, 0);
break;
case 'o':
if (mntopts_cmd) {
- com_err(program_name, 0,
+ com_err(program_name, 0, "%s",
_("-o may only be specified once"));
usage();
}
break;
case 'O':
if (features_cmd) {
- com_err(program_name, 0,
+ com_err(program_name, 0, "%s",
_("-O may only be specified once"));
usage();
}
features_cmd = optarg;
open_flag = EXT2_FLAG_RW;
break;
+#ifdef CONFIG_QUOTA
case 'Q':
Q_flag = 1;
parse_quota_opts(optarg);
open_flag = EXT2_FLAG_RW;
break;
+#endif
case 'r':
reserved_blocks = strtoul(optarg, &tmp, 0);
if (*tmp) {
len = strlen(opts);
buf = malloc(len+1);
if (!buf) {
- fprintf(stderr,
+ fprintf(stderr, "%s",
_("Couldn't allocate memory to parse options!\n"));
return 1;
}
strcmp(token, "clear_mmp") == 0) {
clear_mmp = 1;
} else if (strcmp(token, "mmp_update_interval") == 0) {
- unsigned long interval;
+ unsigned long intv;
if (!arg) {
r_usage++;
continue;
}
- interval = strtoul(arg, &p, 0);
+ intv = strtoul(arg, &p, 0);
if (*p) {
fprintf(stderr,
_("Invalid mmp_update_interval: %s\n"),
r_usage++;
continue;
}
- if (interval == 0) {
- interval = EXT4_MMP_UPDATE_INTERVAL;
- } else if (interval > EXT4_MMP_MAX_UPDATE_INTERVAL) {
+ if (intv == 0) {
+ intv = EXT4_MMP_UPDATE_INTERVAL;
+ } else if (intv > EXT4_MMP_MAX_UPDATE_INTERVAL) {
fprintf(stderr,
_("mmp_update_interval too big: %lu\n"),
- interval);
+ intv);
r_usage++;
continue;
}
printf(P_("Setting multiple mount protection update "
"interval to %lu second\n",
"Setting multiple mount protection update "
- "interval to %lu seconds\n", interval),
- interval);
- fs->super->s_mmp_update_interval = interval;
+ "interval to %lu seconds\n", intv),
+ intv);
+ fs->super->s_mmp_update_interval = intv;
ext2fs_mark_super_dirty(fs);
} else if (!strcmp(token, "test_fs")) {
fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
r_usage++;
}
if (r_usage) {
- fprintf(stderr, _("\nBad options specified.\n\n"
+ fprintf(stderr, "%s", _("\nBad options specified.\n\n"
"Extended options are separated by commas, "
"and may take an argument which\n"
"\tis set off by an equals ('=') sign.\n\n"
return 0;
}
-static int ext2fs_is_meta_block(ext2_filsys fs, blk_t blk)
+static int ext2fs_is_meta_block(ext2_filsys fs, blk64_t blk)
{
dgrp_t group;
- group = ext2fs_group_of_blk(fs, blk);
+ group = ext2fs_group_of_blk2(fs, blk);
if (ext2fs_block_bitmap_loc(fs, group) == blk)
return 1;
if (ext2fs_inode_bitmap_loc(fs, group) == blk)
return 0;
}
-static int ext2fs_is_block_in_group(ext2_filsys fs, dgrp_t group, blk_t blk)
+static int ext2fs_is_block_in_group(ext2_filsys fs, dgrp_t group, blk64_t blk)
{
- blk_t start_blk, end_blk;
+ blk64_t start_blk, end_blk;
start_blk = fs->super->s_first_data_block +
- EXT2_BLOCKS_PER_GROUP(fs->super) * group;
+ EXT2_GROUPS_TO_BLOCKS(fs->super, group);
/*
* We cannot get new block beyond end_blk for for the last block group
* so we can check with EXT2_BLOCKS_PER_GROUP even for last block group
* the respective fs metadata pointers. Otherwise
* fail
*/
- group = ext2fs_group_of_blk(fs, blk);
+ group = ext2fs_group_of_blk2(fs, blk);
goal = ext2fs_group_first_block2(fs, group);
meta_data = 1;
static int group_desc_scan_and_fix(ext2_filsys fs, ext2fs_block_bitmap bmap)
{
dgrp_t i;
- blk_t blk, new_blk;
+ blk64_t blk, new_blk;
for (i = 0; i < fs->group_desc_count; i++) {
blk = ext2fs_block_bitmap_loc(fs, i);
tmp_name = strdup(name);
if (!tmp_name) {
alloc_fn_fail:
- com_err(program_name, ENOMEM,
+ com_err(program_name, ENOMEM, "%s",
_("Couldn't allocate memory for tdb filename\n"));
return ENOMEM;
}
goto alloc_fn_fail;
sprintf(tdb_file, "%s/tune2fs-%s.e2undo", tdb_dir, dev_name);
- if (!access(tdb_file, F_OK)) {
- if (unlink(tdb_file) < 0) {
- retval = errno;
- com_err(program_name, retval,
- _("while trying to delete %s"),
- tdb_file);
- free(tdb_file);
- return retval;
- }
+ if ((unlink(tdb_file) < 0) && (errno != ENOENT)) {
+ retval = errno;
+ com_err(program_name, retval,
+ _("while trying to delete %s"), tdb_file);
+ free(tdb_file);
+ return retval;
}
set_undo_io_backing_manager(*io_ptr);
_("MMP block magic is bad. Try to fix it by "
"running:\n'e2fsck -f %s'\n"), device_name);
else if (retval != EXT2_ET_MMP_FAILED)
- fprintf(stderr,
+ fprintf(stderr, "%s",
_("Couldn't find valid filesystem superblock.\n"));
ext2fs_free(fs);
goto closefs;
}
if (new_inode_size < EXT2_INODE_SIZE(fs->super)) {
- fprintf(stderr, _("Shrinking the inode size is "
- "not supported\n"));
+ fprintf(stderr, "%s",
+ _("Shrinking inode size is not supported\n"));
+ rc = 1;
+ goto closefs;
+ }
+ if (new_inode_size > fs->blocksize) {
+ fprintf(stderr, _("Invalid inode size %lu (max %d)\n"),
+ new_inode_size, fs->blocksize);
rc = 1;
goto closefs;
}
goto closefs;
}
if (io_ptr != io_ptr_orig) {
- ext2fs_close(fs);
+ ext2fs_close_free(&fs);
goto retry_open;
}
}
printf(_("Setting reserved blocks gid to %lu\n"), resgid);
}
if (i_flag) {
- if (interval >= (1ULL << 32)) {
+ if ((unsigned long long)interval >= (1ULL << 32)) {
com_err(program_name, 0,
_("interval between checks is too big (%lu)"),
interval);
}
if (s_flag == 1) {
if (sb->s_feature_ro_compat &
- EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)
+ EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER) {
fputs(_("\nThe filesystem already has sparse "
"superblocks.\n"), stderr);
- else {
+ } else if (sb->s_feature_incompat &
+ EXT2_FEATURE_INCOMPAT_META_BG) {
+ fputs(_("\nSetting the sparse superblock flag not "
+ "supported\nfor filesystems with "
+ "the meta_bg feature enabled.\n"),
+ stderr);
+ rc = 1;
+ goto closefs;
+ } else {
sb->s_feature_ro_compat |=
EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
sb->s_state &= ~EXT2_VALID_FS;
}
}
if (s_flag == 0) {
- fputs(_("\nClearing the sparse superflag not supported.\n"),
+ fputs(_("\nClearing the sparse superblock flag not supported.\n"),
stderr);
rc = 1;
goto closefs;
int set_csum = 0;
dgrp_t i;
- if (sb->s_feature_ro_compat &
- EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
+ if (ext2fs_has_group_desc_csum(fs)) {
+ /*
+ * Changing the UUID requires rewriting all metadata,
+ * which can race with a mounted fs. Don't allow that.
+ */
+ if (mount_flags & EXT2_MF_MOUNTED) {
+ fputs(_("The UUID may only be "
+ "changed when the filesystem is "
+ "unmounted.\n"), stderr);
+ exit(1);
+ }
+ if (check_fsck_needed(fs))
+ exit(1);
+
/*
* Determine if the block group checksums are
* correct so we know whether or not to set
} else if (strcasecmp(new_UUID, "random") == 0) {
uuid_generate(sb->s_uuid);
} else if (uuid_parse(new_UUID, sb->s_uuid)) {
- com_err(program_name, 0, _("Invalid UUID format\n"));
+ com_err(program_name, 0, "%s",
+ _("Invalid UUID format\n"));
rc = 1;
goto closefs;
}
+ ext2fs_init_csum_seed(fs);
if (set_csum) {
for (i = 0; i < fs->group_desc_count; i++)
ext2fs_group_desc_csum_set(fs, i);
fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
}
ext2fs_mark_super_dirty(fs);
+ if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
+ EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
+ rewrite_checksums = 1;
}
+ if (rewrite_checksums)
+ rewrite_metadata_checksums(fs);
if (I_flag) {
if (mount_flags & EXT2_MF_MOUNTED) {
fputs(_("The inode size may only be "
printf(_("Setting inode size %lu\n"),
new_inode_size);
} else {
- printf(_("Failed to change inode size\n"));
+ printf("%s", _("Failed to change inode size\n"));
rc = 1;
goto closefs;
}
exit(1);
}
- return (ext2fs_close(fs) ? 1 : 0);
+ return (ext2fs_close_free(&fs) ? 1 : 0);
}