File tune2fs-update-dir-checksums-when-clearing-dir_index.patch of Package e2fsprogs.19068

From 514fde0296c2085fb11685820f3d396d5c41026e Mon Sep 17 00:00:00 2001
From: Jan Kara <jack@suse.cz>
Date: Thu, 13 Feb 2020 11:16:02 +0100
Subject: [PATCH 5/5] tune2fs: update dir checksums when clearing dir_index
 feature
References: bsc#1160979

When clearing dir_index feature while metadata_csum is enabled, we have
to rewrite checksums of all indexed directories to update checksums of
internal tree nodes.

Signed-off-by: Jan Kara <jack@suse.cz>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
Acked-by: Jan Kara <jack@suse.cz>
---
 misc/tune2fs.c | 143 ++++++++++++++++++++++++++++++++++++++-------------------
 1 file changed, 95 insertions(+), 48 deletions(-)

Index: e2fsprogs-1.43.8/misc/tune2fs.c
===================================================================
--- e2fsprogs-1.43.8.orig/misc/tune2fs.c
+++ e2fsprogs-1.43.8/misc/tune2fs.c
@@ -553,7 +553,8 @@ struct rewrite_dir_context {
 	char *buf;
 	errcode_t errcode;
 	ext2_ino_t dir;
-	int is_htree;
+	int is_htree:1;
+	int clear_htree:1;
 };
 
 static int rewrite_dir_block(ext2_filsys fs,
@@ -572,8 +573,13 @@ static int rewrite_dir_block(ext2_filsys
 	if (ctx->errcode)
 		return BLOCK_ABORT;
 
-	/* if htree node... */
-	if (ctx->is_htree)
+	/*
+	 * if htree node... Note that if we are clearing htree structures from
+	 * the directory, we treat the htree internal block as an ordinary leaf.
+	 * The code below will do the right thing and make space for checksum
+	 * there.
+	 */
+	if (ctx->is_htree && !ctx->clear_htree)
 		ext2fs_get_dx_countlimit(fs, (struct ext2_dir_entry *)ctx->buf,
 					 &dcl, &dcl_offset);
 	if (dcl) {
@@ -702,7 +708,8 @@ static errcode_t rewrite_directory(ext2_
 	if (retval)
 		return retval;
 
-	ctx.is_htree = (inode->i_flags & EXT2_INDEX_FL);
+	ctx.is_htree = !!(inode->i_flags & EXT2_INDEX_FL);
+	ctx.clear_htree = !ext2fs_has_feature_dir_index(fs->super);
 	ctx.dir = dir;
 	ctx.errcode = 0;
 	retval = ext2fs_block_iterate3(fs, dir, BLOCK_FLAG_READ_ONLY |
@@ -713,121 +720,169 @@ static errcode_t rewrite_directory(ext2_
 	if (retval)
 		return retval;
 
+	if (ctx.is_htree && ctx.clear_htree) {
+		inode->i_flags &= ~EXT2_INDEX_FL;
+		retval = ext2fs_write_inode(fs, dir, inode);
+		if (retval)
+			return retval;
+	}
+
 	return ctx.errcode;
 }
 
-/*
- * Forcibly set checksums in all inodes.
- */
-static void rewrite_inodes(ext2_filsys fs)
+struct rewrite_context {
+	ext2_filsys fs;
+	struct ext2_inode *zero_inode;
+	char *ea_buf;
+	int inode_size;
+};
+
+static void rewrite_one_inode(struct rewrite_context *ctx, ext2_ino_t ino,
+			      struct ext2_inode *inode)
 {
-	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;
+	ext2_filsys fs = ctx->fs;
+	int inode_dirty;
+	errcode_t retval;
+	blk64_t	file_acl_block;
 
-	if (fs->super->s_creator_os == EXT2_OS_HURD)
-		return;
+	if (ext2fs_test_inode_bitmap2(fs->inode_map, ino)) {
+		inode_dirty = 1;
+	} else {
+		if (memcmp(inode, ctx->zero_inode, ctx->inode_size) != 0) {
+			memset(inode, 0, ctx->inode_size);
+			inode_dirty = 1;
+		} else {
+			inode_dirty = 0;
+		}
+	}
 
-	retval = ext2fs_open_inode_scan(fs, 0, &scan);
+	if (inode_dirty) {
+		retval = ext2fs_write_inode_full(fs, ino, inode,
+						 ctx->inode_size);
+		if (retval) {
+			com_err("set_csum", retval, "while writing "
+				"inode");
+			exit(1);
+		}
+	}
+
+	retval = rewrite_extents(fs, ino, inode);
 	if (retval) {
-		com_err("set_csum", retval, "while opening inode scan");
+		com_err("rewrite_extents", retval,
+			"while rewriting extents");
 		exit(1);
 	}
 
-	retval = ext2fs_get_mem(length, &inode);
+	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)
+		return;
+	retval = ext2fs_read_ext_attr3(fs, file_acl_block, ctx->ea_buf, ino);
 	if (retval) {
-		com_err("set_csum", retval, "while allocating memory");
+		com_err("rewrite_eablock", retval,
+			"while rewriting extended attribute");
+		exit(1);
+	}
+	retval = ext2fs_write_ext_attr3(fs, file_acl_block, ctx->ea_buf,
+					ino);
+	if (retval) {
+		com_err("rewrite_eablock", retval,
+			"while rewriting extended attribute");
 		exit(1);
 	}
+}
+
+#define REWRITE_DIR_FL		0x02	/* Rewrite directories */
+#define REWRITE_NONDIR_FL	0x04	/* Rewrite other inodes */
+#define REWRITE_ALL (REWRITE_DIR_FL | REWRITE_NONDIR_FL)
+
+static void rewrite_inodes_pass(struct rewrite_context *ctx, unsigned int flags)
+{
+	ext2_filsys fs = ctx->fs;
+	struct ext2_inode *inode;
+	ext2_inode_scan	scan;
+	errcode_t	retval;
+	ext2_ino_t	ino;
+	int rewrite;
 
-	retval = ext2fs_get_memzero(length, &zero);
+	if (fs->super->s_creator_os == EXT2_OS_HURD)
+		return;
+
+	retval = ext2fs_open_inode_scan(fs, 0, &scan);
 	if (retval) {
-		com_err("set_csum", retval, "while allocating memory");
+		com_err("set_csum", retval, "while opening inode scan");
 		exit(1);
 	}
 
-	retval = ext2fs_get_mem(fs->blocksize, &ea_buf);
+	retval = ext2fs_get_mem(ctx->inode_size, &inode);
 	if (retval) {
 		com_err("set_csum", retval, "while allocating memory");
 		exit(1);
 	}
 
 	do {
-		retval = ext2fs_get_next_inode_full(scan, &ino, inode, length);
+		retval = ext2fs_get_next_inode_full(scan, &ino, inode, ctx->inode_size);
 		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;
+		rewrite = 0;
+		if (LINUX_S_ISDIR(inode->i_mode)) {
+			if (flags & REWRITE_DIR_FL)
+				rewrite = 1;
 		} else {
-			if (memcmp(inode, zero, length) != 0) {
-				memset(inode, 0, length);
-				inode_dirty = 1;
-			} else {
-				inode_dirty = 0;
-			}
+			if (flags & REWRITE_NONDIR_FL)
+				rewrite = 1;
 		}
+		if (rewrite)
+			rewrite_one_inode(ctx, ino, inode);
+	} while (ino);
 
-		if (inode_dirty) {
-			retval = ext2fs_write_inode_full(fs, ino, inode,
-							 length);
-			if (retval) {
-				com_err("set_csum", retval, "while writing "
-					"inode");
-				exit(1);
-			}
-		}
+	ext2fs_free_mem(&inode);
+	ext2fs_close_inode_scan(scan);
+}
 
-		retval = rewrite_extents(fs, ino, inode);
-		if (retval) {
-			com_err("rewrite_extents", retval,
-				"while rewriting extents");
-			exit(1);
-		}
+/*
+ * Forcibly rewrite checksums in inodes specified by 'flags'
+ */
+static void rewrite_inodes(ext2_filsys fs, unsigned int flags)
+{
+	struct rewrite_context ctx = {
+		.fs = fs,
+		.inode_size = EXT2_INODE_SIZE(fs->super),
+	};
+	errcode_t retval;
 
-		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);
-			}
-		}
+	retval = ext2fs_get_memzero(ctx.inode_size, &ctx.zero_inode);
+	if (retval) {
+		com_err("set_csum", retval, "while allocating memory");
+		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);
+	retval = ext2fs_get_mem(fs->blocksize, &ctx.ea_buf);
+	if (retval) {
+		com_err("set_csum", retval, "while allocating memory");
+		exit(1);
+	}
 
-	ext2fs_free_mem(&zero);
-	ext2fs_free_mem(&inode);
-	ext2fs_free_mem(&ea_buf);
-	ext2fs_close_inode_scan(scan);
+	rewrite_inodes_pass(&ctx, flags);
+
+	ext2fs_free_mem(&ctx.zero_inode);
+	ext2fs_free_mem(&ctx.ea_buf);
 }
 
-static void rewrite_metadata_checksums(ext2_filsys fs)
+static void rewrite_metadata_checksums(ext2_filsys fs, unsigned int flags)
 {
 	errcode_t retval;
 	dgrp_t i;
@@ -842,7 +897,7 @@ static void rewrite_metadata_checksums(e
 			"while reading bitmaps");
 		exit(1);
 	}
-	rewrite_inodes(fs);
+	rewrite_inodes(fs, flags);
 	ext2fs_mark_ib_dirty(fs);
 	ext2fs_mark_bb_dirty(fs);
 	ext2fs_mmp_update2(fs, 1);
@@ -1141,6 +1196,23 @@ mmp_error:
 			uuid_generate((unsigned char *) sb->s_hash_seed);
 	}
 
+	if (FEATURE_OFF(E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_DIR_INDEX) &&
+	    ext2fs_has_feature_metadata_csum(sb)) {
+		check_fsck_needed(fs,
+			_("Disabling directory index on filesystem with "
+			  "checksums could take some time."));
+		if (mount_flags & EXT2_MF_MOUNTED) {
+			fputs(_("Cannot disable dir_index on a mounted "
+				"filesystem!\n"), stderr);
+			exit(1);
+		}
+		/*
+		 * Clearing dir_index on checksummed filesystem requires
+		 * rewriting all directories to update checksums.
+		 */
+		rewrite_checksums |= REWRITE_DIR_FL;
+	}
+
 	if (FEATURE_OFF(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
 		if (ext2fs_check_desc(fs)) {
 			fputs(_("Clearing the flex_bg flag would "
@@ -1184,7 +1256,7 @@ mmp_error:
 				 "The larger fields afforded by this feature "
 				 "enable full-strength checksumming.  "
 				 "Run resize2fs -b to rectify.\n"));
-		rewrite_checksums = 1;
+		rewrite_checksums = REWRITE_ALL;
 		/* metadata_csum supersedes uninit_bg */
 		ext2fs_clear_feature_gdt_csum(fs->super);
 
@@ -1212,7 +1284,7 @@ mmp_error:
 				"filesystem!\n"), stderr);
 			exit(1);
 		}
-		rewrite_checksums = 1;
+		rewrite_checksums = REWRITE_ALL;
 
 		/* Enable uninit_bg unless the user expressly turned it off */
 		memcpy(test_features, old_features, sizeof(test_features));
@@ -1375,7 +1447,7 @@ mmp_error:
 			return 1;
 		}
 
-		rewrite_checksums = 1;
+		rewrite_checksums = REWRITE_ALL;
 	}
 
 	if (sb->s_rev_level == EXT2_GOOD_OLD_REV &&
@@ -3148,7 +3220,7 @@ retry_open:
 		ext2fs_mark_super_dirty(fs);
 		if (ext2fs_has_feature_metadata_csum(fs->super) &&
 		    !ext2fs_has_feature_csum_seed(fs->super))
-			rewrite_checksums = 1;
+			rewrite_checksums = REWRITE_ALL;
 	}
 
 	if (I_flag) {
@@ -3180,7 +3252,7 @@ retry_open:
 		if (retval == 0) {
 			printf(_("Setting inode size %lu\n"),
 							new_inode_size);
-			rewrite_checksums = 1;
+			rewrite_checksums = REWRITE_ALL;
 		} else {
 			printf("%s", _("Failed to change inode size\n"));
 			rc = 1;
@@ -3189,7 +3261,7 @@ retry_open:
 	}
 
 	if (rewrite_checksums)
-		rewrite_metadata_checksums(fs);
+		rewrite_metadata_checksums(fs, rewrite_checksums);
 
 	if (l_flag)
 		list_super(sb);
openSUSE Build Service is sponsored by