Blame SOURCES/e2fsprogs-1.45.6-tune2fs-update-dir-checksums-when-clearing-dir_index.patch

a77133
From fb4a6ed596f6a9f6b1c6d3d9307ef8259d988c04 Mon Sep 17 00:00:00 2001
a77133
From: Jan Kara <jack@suse.cz>
a77133
Date: Thu, 13 Feb 2020 11:16:02 +0100
a77133
Subject: [PATCH 06/46] tune2fs: update dir checksums when clearing dir_index
a77133
 feature
a77133
Content-Type: text/plain
a77133
a77133
When clearing dir_index feature while metadata_csum is enabled, we have
a77133
to rewrite checksums of all indexed directories to update checksums of
a77133
internal tree nodes.
a77133
a77133
Signed-off-by: Jan Kara <jack@suse.cz>
a77133
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
a77133
Signed-off-by: Lukas Czerner <lczerner@redhat.com>
a77133
---
a77133
 misc/tune2fs.c | 143 ++++++++++++++++++++++++++++++++-----------------
a77133
 1 file changed, 95 insertions(+), 48 deletions(-)
a77133
a77133
diff --git a/misc/tune2fs.c b/misc/tune2fs.c
a77133
index 39cf8587..a7a779b8 100644
a77133
--- a/misc/tune2fs.c
a77133
+++ b/misc/tune2fs.c
a77133
@@ -504,7 +504,8 @@ struct rewrite_dir_context {
a77133
 	char *buf;
a77133
 	errcode_t errcode;
a77133
 	ext2_ino_t dir;
a77133
-	int is_htree;
a77133
+	int is_htree:1;
a77133
+	int clear_htree:1;
a77133
 };
a77133
 
a77133
 static int rewrite_dir_block(ext2_filsys fs,
a77133
@@ -523,8 +524,13 @@ static int rewrite_dir_block(ext2_filsys fs,
a77133
 	if (ctx->errcode)
a77133
 		return BLOCK_ABORT;
a77133
 
a77133
-	/* if htree node... */
a77133
-	if (ctx->is_htree)
a77133
+	/*
a77133
+	 * if htree node... Note that if we are clearing htree structures from
a77133
+	 * the directory, we treat the htree internal block as an ordinary leaf.
a77133
+	 * The code below will do the right thing and make space for checksum
a77133
+	 * there.
a77133
+	 */
a77133
+	if (ctx->is_htree && !ctx->clear_htree)
a77133
 		ext2fs_get_dx_countlimit(fs, (struct ext2_dir_entry *)ctx->buf,
a77133
 					 &dcl, &dcl_offset);
a77133
 	if (dcl) {
a77133
@@ -653,7 +659,8 @@ static errcode_t rewrite_directory(ext2_filsys fs, ext2_ino_t dir,
a77133
 	if (retval)
a77133
 		return retval;
a77133
 
a77133
-	ctx.is_htree = (inode->i_flags & EXT2_INDEX_FL);
a77133
+	ctx.is_htree = !!(inode->i_flags & EXT2_INDEX_FL);
a77133
+	ctx.clear_htree = !ext2fs_has_feature_dir_index(fs->super);
a77133
 	ctx.dir = dir;
a77133
 	ctx.errcode = 0;
a77133
 	retval = ext2fs_block_iterate3(fs, dir, BLOCK_FLAG_READ_ONLY |
a77133
@@ -664,6 +671,13 @@ static errcode_t rewrite_directory(ext2_filsys fs, ext2_ino_t dir,
a77133
 	if (retval)
a77133
 		return retval;
a77133
 
a77133
+	if (ctx.is_htree && ctx.clear_htree) {
a77133
+		inode->i_flags &= ~EXT2_INDEX_FL;
a77133
+		retval = ext2fs_write_inode(fs, dir, inode);
a77133
+		if (retval)
a77133
+			return retval;
a77133
+	}
a77133
+
a77133
 	return ctx.errcode;
a77133
 }
a77133
 
a77133
@@ -818,28 +832,67 @@ static void rewrite_one_inode(struct rewrite_context *ctx, ext2_ino_t ino,
a77133
 		fatal_err(retval, "while rewriting extended attribute");
a77133
 }
a77133
 
a77133
-/*
a77133
- * Forcibly set checksums in all inodes.
a77133
- */
a77133
-static void rewrite_inodes(ext2_filsys fs)
a77133
+#define REWRITE_EA_FL		0x01	/* Rewrite EA inodes */
a77133
+#define REWRITE_DIR_FL		0x02	/* Rewrite directories */
a77133
+#define REWRITE_NONDIR_FL	0x04	/* Rewrite other inodes */
a77133
+#define REWRITE_ALL (REWRITE_EA_FL | REWRITE_DIR_FL | REWRITE_NONDIR_FL)
a77133
+
a77133
+static void rewrite_inodes_pass(struct rewrite_context *ctx, unsigned int flags)
a77133
 {
a77133
 	ext2_inode_scan	scan;
a77133
 	errcode_t	retval;
a77133
 	ext2_ino_t	ino;
a77133
 	struct ext2_inode *inode;
a77133
-	int pass;
a77133
+	int rewrite;
a77133
+
a77133
+	retval = ext2fs_get_mem(ctx->inode_size, &inode;;
a77133
+	if (retval)
a77133
+		fatal_err(retval, "while allocating memory");
a77133
+
a77133
+	retval = ext2fs_open_inode_scan(ctx->fs, 0, &scan;;
a77133
+	if (retval)
a77133
+		fatal_err(retval, "while opening inode scan");
a77133
+
a77133
+	do {
a77133
+		retval = ext2fs_get_next_inode_full(scan, &ino, inode,
a77133
+						    ctx->inode_size);
a77133
+		if (retval)
a77133
+			fatal_err(retval, "while getting next inode");
a77133
+		if (!ino)
a77133
+			break;
a77133
+
a77133
+		rewrite = 0;
a77133
+		if (inode->i_flags & EXT4_EA_INODE_FL) {
a77133
+			if (flags & REWRITE_EA_FL)
a77133
+				rewrite = 1;
a77133
+		} else if (LINUX_S_ISDIR(inode->i_mode)) {
a77133
+			if (flags & REWRITE_DIR_FL)
a77133
+				rewrite = 1;
a77133
+		} else {
a77133
+			if (flags & REWRITE_NONDIR_FL)
a77133
+				rewrite = 1;
a77133
+		}
a77133
+		if (rewrite)
a77133
+			rewrite_one_inode(ctx, ino, inode);
a77133
+	} while (ino);
a77133
+	ext2fs_close_inode_scan(scan);
a77133
+	ext2fs_free_mem(&inode;;
a77133
+}
a77133
+
a77133
+/*
a77133
+ * Forcibly rewrite checksums in inodes specified by 'flags'
a77133
+ */
a77133
+static void rewrite_inodes(ext2_filsys fs, unsigned int flags)
a77133
+{
a77133
 	struct rewrite_context ctx = {
a77133
 		.fs = fs,
a77133
 		.inode_size = EXT2_INODE_SIZE(fs->super),
a77133
 	};
a77133
+	errcode_t retval;
a77133
 
a77133
 	if (fs->super->s_creator_os == EXT2_OS_HURD)
a77133
 		return;
a77133
 
a77133
-	retval = ext2fs_get_mem(ctx.inode_size, &inode;;
a77133
-	if (retval)
a77133
-		fatal_err(retval, "while allocating memory");
a77133
-
a77133
 	retval = ext2fs_get_memzero(ctx.inode_size, &ctx.zero_inode);
a77133
 	if (retval)
a77133
 		fatal_err(retval, "while allocating memory");
a77133
@@ -858,39 +911,16 @@ static void rewrite_inodes(ext2_filsys fs)
a77133
 	 *
a77133
 	 * pass 2: go over other inodes to update their checksums.
a77133
 	 */
a77133
-	if (ext2fs_has_feature_ea_inode(fs->super))
a77133
-		pass = 1;
a77133
-	else
a77133
-		pass = 2;
a77133
-	for (;pass <= 2; pass++) {
a77133
-		retval = ext2fs_open_inode_scan(fs, 0, &scan;;
a77133
-		if (retval)
a77133
-			fatal_err(retval, "while opening inode scan");
a77133
-
a77133
-		do {
a77133
-			retval = ext2fs_get_next_inode_full(scan, &ino, inode,
a77133
-							    ctx.inode_size);
a77133
-			if (retval)
a77133
-				fatal_err(retval, "while getting next inode");
a77133
-			if (!ino)
a77133
-				break;
a77133
-
a77133
-			if (((pass == 1) &&
a77133
-			     (inode->i_flags & EXT4_EA_INODE_FL)) ||
a77133
-			    ((pass == 2) &&
a77133
-			     !(inode->i_flags & EXT4_EA_INODE_FL)))
a77133
-				rewrite_one_inode(&ctx, ino, inode);
a77133
-		} while (ino);
a77133
-
a77133
-		ext2fs_close_inode_scan(scan);
a77133
-	}
a77133
+	if (ext2fs_has_feature_ea_inode(fs->super) && (flags & REWRITE_EA_FL))
a77133
+		rewrite_inodes_pass(&ctx, REWRITE_EA_FL);
a77133
+	flags &= ~REWRITE_EA_FL;
a77133
+	rewrite_inodes_pass(&ctx, flags);
a77133
 
a77133
 	ext2fs_free_mem(&ctx.zero_inode);
a77133
 	ext2fs_free_mem(&ctx.ea_buf);
a77133
-	ext2fs_free_mem(&inode;;
a77133
 }
a77133
 
a77133
-static void rewrite_metadata_checksums(ext2_filsys fs)
a77133
+static void rewrite_metadata_checksums(ext2_filsys fs, unsigned int flags)
a77133
 {
a77133
 	errcode_t retval;
a77133
 	dgrp_t i;
a77133
@@ -902,7 +932,7 @@ static void rewrite_metadata_checksums(ext2_filsys fs)
a77133
 	retval = ext2fs_read_bitmaps(fs);
a77133
 	if (retval)
a77133
 		fatal_err(retval, "while reading bitmaps");
a77133
-	rewrite_inodes(fs);
a77133
+	rewrite_inodes(fs, flags);
a77133
 	ext2fs_mark_ib_dirty(fs);
a77133
 	ext2fs_mark_bb_dirty(fs);
a77133
 	ext2fs_mmp_update2(fs, 1);
a77133
@@ -1201,6 +1231,23 @@ mmp_error:
a77133
 			uuid_generate((unsigned char *) sb->s_hash_seed);
a77133
 	}
a77133
 
a77133
+	if (FEATURE_OFF(E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_DIR_INDEX) &&
a77133
+	    ext2fs_has_feature_metadata_csum(sb)) {
a77133
+		check_fsck_needed(fs,
a77133
+			_("Disabling directory index on filesystem with "
a77133
+			  "checksums could take some time."));
a77133
+		if (mount_flags & EXT2_MF_MOUNTED) {
a77133
+			fputs(_("Cannot disable dir_index on a mounted "
a77133
+				"filesystem!\n"), stderr);
a77133
+			exit(1);
a77133
+		}
a77133
+		/*
a77133
+		 * Clearing dir_index on checksummed filesystem requires
a77133
+		 * rewriting all directories to update checksums.
a77133
+		 */
a77133
+		rewrite_checksums |= REWRITE_DIR_FL;
a77133
+	}
a77133
+
a77133
 	if (FEATURE_OFF(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
a77133
 		if (ext2fs_check_desc(fs)) {
a77133
 			fputs(_("Clearing the flex_bg flag would "
a77133
@@ -1244,7 +1291,7 @@ mmp_error:
a77133
 				 "The larger fields afforded by this feature "
a77133
 				 "enable full-strength checksumming.  "
a77133
 				 "Run resize2fs -b to rectify.\n"));
a77133
-		rewrite_checksums = 1;
a77133
+		rewrite_checksums = REWRITE_ALL;
a77133
 		/* metadata_csum supersedes uninit_bg */
a77133
 		ext2fs_clear_feature_gdt_csum(fs->super);
a77133
 
a77133
@@ -1272,7 +1319,7 @@ mmp_error:
a77133
 				"filesystem!\n"), stderr);
a77133
 			exit(1);
a77133
 		}
a77133
-		rewrite_checksums = 1;
a77133
+		rewrite_checksums = REWRITE_ALL;
a77133
 
a77133
 		/* Enable uninit_bg unless the user expressly turned it off */
a77133
 		memcpy(test_features, old_features, sizeof(test_features));
a77133
@@ -1454,7 +1501,7 @@ mmp_error:
a77133
 			}
a77133
 			check_fsck_needed(fs, _("Recalculating checksums "
a77133
 						"could take some time."));
a77133
-			rewrite_checksums = 1;
a77133
+			rewrite_checksums = REWRITE_ALL;
a77133
 		}
a77133
 	}
a77133
 
a77133
@@ -3191,7 +3238,7 @@ _("Warning: The journal is dirty. You may wish to replay the journal like:\n\n"
a77133
 			check_fsck_needed(fs,
a77133
 				_("Setting the UUID on this "
a77133
 				  "filesystem could take some time."));
a77133
-			rewrite_checksums = 1;
a77133
+			rewrite_checksums = REWRITE_ALL;
a77133
 		}
a77133
 
a77133
 		if (ext2fs_has_group_desc_csum(fs)) {
a77133
@@ -3302,7 +3349,7 @@ _("Warning: The journal is dirty. You may wish to replay the journal like:\n\n"
a77133
 		if (retval == 0) {
a77133
 			printf(_("Setting inode size %lu\n"),
a77133
 							new_inode_size);
a77133
-			rewrite_checksums = 1;
a77133
+			rewrite_checksums = REWRITE_ALL;
a77133
 		} else {
a77133
 			printf("%s", _("Failed to change inode size\n"));
a77133
 			rc = 1;
a77133
@@ -3311,7 +3358,7 @@ _("Warning: The journal is dirty. You may wish to replay the journal like:\n\n"
a77133
 	}
a77133
 
a77133
 	if (rewrite_checksums)
a77133
-		rewrite_metadata_checksums(fs);
a77133
+		rewrite_metadata_checksums(fs, rewrite_checksums);
a77133
 
a77133
 	if (l_flag)
a77133
 		list_super(sb);
a77133
-- 
a77133
2.35.1
a77133