From patchwork Wed Mar 7 00:00:19 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Darrick J. Wong" X-Patchwork-Id: 145082 X-Patchwork-Delegate: tytso@mit.edu Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id C50C2B6EF1 for ; Wed, 7 Mar 2012 11:00:49 +1100 (EST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1031604Ab2CGAAc (ORCPT ); Tue, 6 Mar 2012 19:00:32 -0500 Received: from e37.co.us.ibm.com ([32.97.110.158]:54349 "EHLO e37.co.us.ibm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1031468Ab2CGAAb (ORCPT ); Tue, 6 Mar 2012 19:00:31 -0500 Received: from /spool/local by e37.co.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Tue, 6 Mar 2012 17:00:29 -0700 Received: from d03dlp03.boulder.ibm.com (9.17.202.179) by e37.co.us.ibm.com (192.168.1.137) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Tue, 6 Mar 2012 17:00:28 -0700 Received: from d03relay03.boulder.ibm.com (d03relay03.boulder.ibm.com [9.17.195.228]) by d03dlp03.boulder.ibm.com (Postfix) with ESMTP id 34BD619D804C for ; Tue, 6 Mar 2012 17:00:22 -0700 (MST) Received: from d03av04.boulder.ibm.com (d03av04.boulder.ibm.com [9.17.195.170]) by d03relay03.boulder.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id q2700QJg176108 for ; Tue, 6 Mar 2012 17:00:27 -0700 Received: from d03av04.boulder.ibm.com (loopback [127.0.0.1]) by d03av04.boulder.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id q2700NwO022061 for ; Tue, 6 Mar 2012 17:00:25 -0700 Received: from elm3b70.beaverton.ibm.com (elm3b70.beaverton.ibm.com [9.47.67.70]) by d03av04.boulder.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id q2700L7i021963; Tue, 6 Mar 2012 17:00:22 -0700 Subject: [PATCH 27/54] tune2fs: Rebuild and checksum directories when toggling metadata_csum or changing UUID To: Andreas Dilger , Theodore Tso , "Darrick J. Wong" From: "Darrick J. Wong" Cc: Sunil Mushran , Amir Goldstein , Andi Kleen , Mingming Cao , Joel Becker , linux-ext4@vger.kernel.org, Coly Li Date: Tue, 06 Mar 2012 16:00:19 -0800 Message-ID: <20120307000019.11945.3808.stgit@elm3b70.beaverton.ibm.com> In-Reply-To: <20120306235720.11945.30629.stgit@elm3b70.beaverton.ibm.com> References: <20120306235720.11945.30629.stgit@elm3b70.beaverton.ibm.com> User-Agent: StGit/0.15 MIME-Version: 1.0 X-Content-Scanned: Fidelis XPS MAILER x-cbid: 12030700-7408-0000-0000-0000034204CE Sender: linux-ext4-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org Since all the metadata checksums depend on the fs UUID, tune2fs must be able to rewrite the checksums of _all_ metadata. It's not that hard to add in the bits to resize the directory block structures at the same time. Signed-off-by: Darrick J. Wong --- misc/tune2fs.c | 194 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 194 insertions(+), 0 deletions(-) -- To unsubscribe from this list: send the line "unsubscribe linux-ext4" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/misc/tune2fs.c b/misc/tune2fs.c index 784df9a..801e6ea 100644 --- a/misc/tune2fs.c +++ b/misc/tune2fs.c @@ -106,6 +106,8 @@ struct blk_move { 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); @@ -364,6 +366,18 @@ static int check_fsck_needed(ext2_filsys fs) 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(_("(and reboot afterwards!)\n")); +} + static void request_fsck_afterwards(ext2_filsys fs) { static int requested = 0; @@ -419,6 +433,177 @@ static errcode_t rewrite_extents(ext2_filsys fs, ext2_ino_t ino, } /* + * 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, + 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 *)(((void *)de) + rec_len); + } + ctx->errcode = ext2fs_get_rec_len(fs, last_de, &rec_len); + if (ctx->errcode) + return BLOCK_ABORT; + name_size = last_de->name_len & 0xFF; + + 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 { + int 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; +} + +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; + 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) @@ -465,6 +650,15 @@ static void rewrite_inodes(ext2_filsys fs) "while rewriting extents"); exit(1); } + + if (LINUX_S_ISDIR(inode->i_mode)) { + retval = rewrite_directory(fs, ino, inode); + if (retval) { + com_err("rewrite_directory", retval, + "while rewriting directories"); + exit(1); + } + } } while (ino); ext2fs_free_mem(&inode);