Skip to contentSkip to conversation

e2fsck: update quota when optimizing the extent tree

PATCH In review Theodore Ts'o 3 participants 11 messages Apr 15, 2017 – Apr 22, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 e2fsck: update quota when optimizing the extent tree

If quota is enabled, optimizing the extent tree wouldn't update the in-memory quota statistics, so that a subsequent e2fsck run would show that the quota usage statistics on disk were incorrect.

Google-Bug-Id: 36391645

---
 e2fsck/e2fsck.c  |  1 +
 e2fsck/e2fsck.h  |  1 +
 e2fsck/extents.c | 23 ++++++++++++++++++-----
 e2fsck/pass1.c   |  1 +
 4 files changed, 21 insertions(+), 5 deletions(-)
e2fsck/e2fsck.c +1 -0
index 0f9da46a..4baedbac 100644
@@ -169,6 +169,7 @@ errcode_t e2fsck_reset_context(e2fsck_t ctx)
 	ctx->fs_fragmented = 0;
 	ctx->fs_fragmented_dir = 0;
 	ctx->large_files = 0;
+	ctx->clusters_allocated = 0;
 
 	for (i=0; i < MAX_EXTENT_DEPTH_COUNT; i++)
 		ctx->extent_depth_count[i] = 0;
e2fsck/e2fsck.h +1 -0
index f3568106..ecd3b1e1 100644
@@ -372,6 +372,7 @@ struct e2fsck_struct {
 	profile_t	profile;
 	int blocks_per_page;
 	ext2_u32_list encrypted_dirs;
+	__u32 clusters_allocated;
 
 	/* Reserve blocks for root and l+f re-creation */
 	blk64_t root_repair_block, lnf_repair_block;
e2fsck/extents.c +18 -5
index c4167e16..1df31d71 100644
@@ -208,17 +208,21 @@ static int find_blocks(ext2_filsys fs, blk64_t *blocknr, e2_blkcnt_t blockcnt,
 static errcode_t rebuild_extent_tree(e2fsck_t ctx, struct extent_list *list,
 				     ext2_ino_t ino)
 {
-	struct ext2_inode	inode;
+	struct ext2_inode_large	inode;
+	struct ext2_inode	*inode_ptr;
 	errcode_t		retval;
 	ext2_extent_handle_t	handle;
 	unsigned int		i, ext_written;
 	struct ext2fs_extent	*ex, extent;
+	__u32			start_val;
 
 	list->count = 0;
 	list->blocks_freed = 0;
 	list->ino = ino;
 	list->ext_read = 0;
-	e2fsck_read_inode(ctx, ino, &inode, "rebuild_extents");
+	inode_ptr = (struct ext2_inode *) &inode;
+	e2fsck_read_inode_full(ctx, ino, inode_ptr, sizeof(inode),
+			       "rebuild_extents");
 
 	/* Skip deleted inodes and inline data files */
 	if (inode.i_links_count == 0 ||
@@ -248,16 +252,19 @@ extents_loaded:
 	memset(inode.i_block, 0, sizeof(inode.i_block));
 
 	/* Make a note of freed blocks */
-	retval = ext2fs_iblk_sub_blocks(ctx->fs, &inode, list->blocks_freed);
+	quota_data_sub(ctx->qctx, &inode, ino,
+		       list->blocks_freed * ctx->fs->blocksize);
+	retval = ext2fs_iblk_sub_blocks(ctx->fs, inode_ptr, list->blocks_freed);
 	if (retval)
 		goto err;
 
 	/* Now stuff extents into the file */
-	retval = ext2fs_extent_open2(ctx->fs, ino, &inode, &handle);
+	retval = ext2fs_extent_open2(ctx->fs, ino, inode_ptr, &handle);
 	if (retval)
 		goto err;
 
 	ext_written = 0;
+	start_val = ctx->clusters_allocated;
 	for (i = 0, ex = list->extents; i < list->count; i++, ex++) {
 		memcpy(&extent, ex, sizeof(struct ext2fs_extent));
 		extent.e_flags &= EXT2_EXTENT_FLAGS_UNINIT;
@@ -295,11 +302,17 @@ extents_loaded:
 		ext_written++;
 	}
 
+	if (start_val != ctx->clusters_allocated) {
+		__u32 num = ctx->clusters_allocated - start_val;
+		quota_data_add(ctx->qctx, &inode, ino,
+			       num * ctx->fs->blocksize);
+	}
+
 #if defined(DEBUG) || defined(DEBUG_SUMMARY)
 	printf("rebuild: ino=%d extents=%d->%d\n", ino, list->ext_read,
 	       ext_written);
 #endif
-	e2fsck_write_inode(ctx, ino, &inode, "rebuild_extents");
+	e2fsck_write_inode(ctx, ino, inode_ptr, "rebuild_extents");
 
 err2:
 	ext2fs_extent_free(handle);
e2fsck/pass1.c +1 -0
index 188cc562..99e8f66a 100644
@@ -4017,6 +4017,7 @@ static errcode_t e2fsck_get_alloc_block(ext2_filsys fs, blk64_t goal,
 	}
 
 	*ret = new_block;
+	ctx->clusters_allocated++;
 	return (0);
 }
 
Signature · 1 line
2.11.0.rc0.7.gbe5a750
#1 lore raw

PATCH 2/3 tests: add new test f_quota_extent_opt

Add a test to validate the changes in commit 403bcb668e4f: "e2fsck:
update quota when optimizing the extent tree".

---
 tests/f_quota_extent_opt/expect.1 |  15 +++++++++++++++
 tests/f_quota_extent_opt/expect.2 |   7 +++++++
 tests/f_quota_extent_opt/image.gz | Bin 0 -> 9018 bytes
 tests/f_quota_extent_opt/name     |   1 +
 4 files changed, 23 insertions(+)
 create mode 100644 tests/f_quota_extent_opt/expect.1
 create mode 100644 tests/f_quota_extent_opt/expect.2
 create mode 100644 tests/f_quota_extent_opt/image.gz
 create mode 100644 tests/f_quota_extent_opt/name
tests/f_quota_extent_opt/expect.1 new file +15 -0
new file mode 100644
index 00000000..1b966213
@@ -0,0 +1,15 @@
+Pass 1: Checking inodes, blocks, and sizes
+Inode 12 extent tree (at level 1) could be narrower.  Fix? yes
+
+Pass 1E: Optimizing extent trees
+Pass 2: Checking directory structure
+Pass 3: Checking directory connectivity
+Pass 4: Checking reference counts
+Pass 5: Checking group summary information
+[QUOTA WARNING] Usage inconsistent for ID 0:actual (147456, 3) != expected (148480, 3)
+Update quota info for quota type 0? yes
+
+
+test_filesys: ***** FILE SYSTEM WAS MODIFIED *****
+test_filesys: 12/1024 files (25.0% non-contiguous), 1339/4096 blocks
+Exit status is 1
tests/f_quota_extent_opt/expect.2 new file +7 -0
new file mode 100644
index 00000000..0bd4632c
@@ -0,0 +1,7 @@
+Pass 1: Checking inodes, blocks, and sizes
+Pass 2: Checking directory structure
+Pass 3: Checking directory connectivity
+Pass 4: Checking reference counts
+Pass 5: Checking group summary information
+test_filesys: 12/1024 files (25.0% non-contiguous), 1339/4096 blocks
+Exit status is 0
tests/f_quota_extent_opt/image.gz new file binary +0 -0
new file mode 100644
index 0000000000000000000000000000000000000000..21dcfbdfeb150a46a6746de21ad874a9eff12185
GIT binary patch
tests/f_quota_extent_opt/name new file +1 -0
new file mode 100644
index 00000000..8c5d7a90
@@ -0,0 +1 @@
+extent optimization with quota
Signature · 1 line
2.11.0.rc0.7.gbe5a750
#2 lore raw

PATCH 3/3 e2fsck: allow extent tree optimization to be disabled

In reply to Theodore Ts'o

Add an extended option, -E no_optimize_extents, as well as a e2fsck.conf profile option, to disable extent tree optimization.

---
 e2fsck/e2fsck.8.in      | 4 ++++
 e2fsck/e2fsck.conf.5.in | 4 ++++
 e2fsck/e2fsck.h         | 1 +
 e2fsck/extents.c        | 3 +++
 e2fsck/unix.c           | 8 ++++++++
 5 files changed, 20 insertions(+)
e2fsck/e2fsck.8.in +4 -0
index 915273d8..4ad575f4 100644
@@ -226,6 +226,10 @@ option may prevent you from further manual data recovery.
 Do not attempt to discard free blocks and unused inode blocks. This option is
 exactly the opposite of discard option. This is set as default.
 .TP
+.BI no_optimize_extents
+Do not offer to optimize the extent tree by eliminating unnecessary
+width or depth.
+.TP
 .BI readahead_kb
 Use this many KiB of memory to pre-fetch metadata in the hopes of reducing
 e2fsck runtime.  By default, this is set to the size of two block groups' inode
e2fsck/e2fsck.conf.5.in +4 -0
index 0bfc76ab..94525baf 100644
@@ -205,6 +205,10 @@ of that type are squelched.  This can be useful if the console is slow
 (i.e., connected to a serial port) and so a large amount of output could
 end up delaying the boot process for a long time (potentially hours).
 .TP
+.I no_optimize_extents
+Do not offer to optimize the extent tree by eliminating unnecessary
+width or depth.
+.TP
 .I readahead_mem_pct
 Use this percentage of memory to try to read in metadata blocks ahead of the
 main e2fsck thread.  This should reduce run times, depending on the speed of
e2fsck/e2fsck.h +1 -0
index ecd3b1e1..a4b5fb24 100644
@@ -169,6 +169,7 @@ struct resource_track {
 #define E2F_OPT_DISCARD		0x2000
 #define E2F_OPT_CONVERT_BMAP	0x4000 /* convert blockmap to extent */
 #define E2F_OPT_FIXES_ONLY	0x8000 /* skip all optimizations */
+#define E2F_OPT_NOOPT_EXTENTS	0x10000 /* don't optimize extents */
 
 /*
  * E2fsck flags
e2fsck/extents.c +3 -0
index 1df31d71..d9410028 100644
@@ -518,6 +518,9 @@ errcode_t e2fsck_should_rebuild_extents(e2fsck_t ctx,
 	if (eti->force_rebuild)
 		goto rebuild;
 
+	if (ctx->options & E2F_OPT_NOOPT_EXTENTS)
+		return 0;
+
 	extents_per_block = (ctx->fs->blocksize -
 			     sizeof(struct ext3_extent_header)) /
 			    sizeof(struct ext3_extent);
e2fsck/unix.c +8 -0
index b7322bc6..b6025535 100644
@@ -709,6 +709,9 @@ static void parse_extended_opts(e2fsck_t ctx, const char *opts)
 		} else if (strcmp(token, "nodiscard") == 0) {
 			ctx->options &= ~E2F_OPT_DISCARD;
 			continue;
+		} else if (strcmp(token, "no_optimize_extents") == 0) {
+			ctx->options |= E2F_OPT_NOOPT_EXTENTS;
+			continue;
 		} else if (strcmp(token, "log_filename") == 0) {
 			if (!arg)
 				extended_usage++;
@@ -1007,6 +1010,11 @@ static errcode_t PRS(int argc, char *argv[], e2fsck_t *ret_ctx)
 	if (c)
 		verbose = 1;
 
+	profile_get_boolean(ctx->profile, "options", "no_optimize_extents",
+			    0, 0, &c);
+	if (c)
+		ctx->options |= E2F_OPT_NOOPT_EXTENTS;
+
 	if (ctx->readahead_kb == ~0ULL) {
 		profile_get_integer(ctx->profile, "options",
 				    "readahead_mem_pct", 0, -1, &c);
Signature · 1 line
2.11.0.rc0.7.gbe5a750
#5 lore raw
In reply to Theodore Ts'o
Quoted 89 lines · Theodore Ts'o

If quota is enabled, optimizing the extent tree wouldn't update the in-memory quota statistics, so that a subsequent e2fsck run would show that the quota usage statistics on disk were incorrect.

Google-Bug-Id: 36391645

---
 e2fsck/e2fsck.c  |  1 +
 e2fsck/e2fsck.h  |  1 +
 e2fsck/extents.c | 23 ++++++++++++++++++-----
 e2fsck/pass1.c   |  1 +
 4 files changed, 21 insertions(+), 5 deletions(-)
e2fsck/e2fsck.c +1 -0
index 0f9da46a..4baedbac 100644
@@ -169,6 +169,7 @@ errcode_t e2fsck_reset_context(e2fsck_t ctx)
 	ctx->fs_fragmented = 0;
 	ctx->fs_fragmented_dir = 0;
 	ctx->large_files = 0;
+	ctx->clusters_allocated = 0;
 
 	for (i=0; i < MAX_EXTENT_DEPTH_COUNT; i++)
 		ctx->extent_depth_count[i] = 0;
e2fsck/e2fsck.h +1 -0
index f3568106..ecd3b1e1 100644
@@ -372,6 +372,7 @@ struct e2fsck_struct {
 	profile_t	profile;
 	int blocks_per_page;
 	ext2_u32_list encrypted_dirs;
+	__u32 clusters_allocated;
 
 	/* Reserve blocks for root and l+f re-creation */
 	blk64_t root_repair_block, lnf_repair_block;
e2fsck/extents.c +11 -4
index c4167e16..1df31d71 100644
@@ -208,17 +208,21 @@ static int find_blocks(ext2_filsys fs, blk64_t *blocknr, e2_blkcnt_t blockcnt,
 static errcode_t rebuild_extent_tree(e2fsck_t ctx, struct extent_list *list,
 				     ext2_ino_t ino)
 {
-	struct ext2_inode	inode;
+	struct ext2_inode_large	inode;
+	struct ext2_inode	*inode_ptr;
 	errcode_t		retval;
 	ext2_extent_handle_t	handle;
 	unsigned int		i, ext_written;
 	struct ext2fs_extent	*ex, extent;
+	__u32			start_val;
 
 	list->count = 0;
 	list->blocks_freed = 0;
 	list->ino = ino;
 	list->ext_read = 0;
-	e2fsck_read_inode(ctx, ino, &inode, "rebuild_extents");
+	inode_ptr = (struct ext2_inode *) &inode;
+	e2fsck_read_inode_full(ctx, ino, inode_ptr, sizeof(inode),
+			       "rebuild_extents");
 
 	/* Skip deleted inodes and inline data files */
 	if (inode.i_links_count == 0 ||
@@ -248,16 +252,19 @@ extents_loaded:
 	memset(inode.i_block, 0, sizeof(inode.i_block));
 
 	/* Make a note of freed blocks */
-	retval = ext2fs_iblk_sub_blocks(ctx->fs, &inode, list->blocks_freed);
+	quota_data_sub(ctx->qctx, &inode, ino,
+		       list->blocks_freed * ctx->fs->blocksize);
+	retval = ext2fs_iblk_sub_blocks(ctx->fs, inode_ptr, list->blocks_freed);
 	if (retval)
 		goto err;
 
 	/* Now stuff extents into the file */
-	retval = ext2fs_extent_open2(ctx->fs, ino, &inode, &handle);
+	retval = ext2fs_extent_open2(ctx->fs, ino, inode_ptr, &handle);
 	if (retval)
 		goto err;
 
 	ext_written = 0;
+	start_val = ctx->clusters_allocated;
 	for (i = 0, ex = list->extents; i < list->count; i++, ex++) {
 		memcpy(&extent, ex, sizeof(struct ext2fs_extent));
 		extent.e_flags &= EXT2_EXTENT_FLAGS_UNINIT;

e2fsck/extents.c extents_loaded: · line 309

Quoted 8 lines
e2fsck/extents.c +5 -0
@@ -295,11 +302,17 @@ extents_loaded:
 		ext_written++;
 	}
 
+	if (start_val != ctx->clusters_allocated) {
+		__u32 num = ctx->clusters_allocated - start_val;
+		quota_data_add(ctx->qctx, &inode, ino,
+			       num * ctx->fs->blocksize);
+	}

Why not calculate the change in quota use by comparing the inode's i_blocks in between the ext2fs_iblk_sub_blocks call and the end of the ext2fs_extent_insert loop, rather than adding a counter to the global e2fsck state?

--D

Quoted 25 lines
e2fsck/extents.c +2 -1
+
 #if defined(DEBUG) || defined(DEBUG_SUMMARY)
 	printf("rebuild: ino=%d extents=%d->%d\n", ino, list->ext_read,
 	       ext_written);
 #endif
-	e2fsck_write_inode(ctx, ino, &inode, "rebuild_extents");
+	e2fsck_write_inode(ctx, ino, inode_ptr, "rebuild_extents");
 
 err2:
 	ext2fs_extent_free(handle);
e2fsck/pass1.c +1 -0
index 188cc562..99e8f66a 100644
@@ -4017,6 +4017,7 @@ static errcode_t e2fsck_get_alloc_block(ext2_filsys fs, blk64_t goal,
 	}
 
 	*ret = new_block;
+	ctx->clusters_allocated++;
 	return (0);
 }
 
Signature · 1 line
2.11.0.rc0.7.gbe5a750
Reviewed-by on v1 3/3
#3 lore raw

Re: [PATCH 3/3] e2fsck: allow extent tree optimization to be disabled

In reply to Theodore Ts'o
Quoted 32 lines · Theodore Ts'o

Add an extended option, -E no_optimize_extents, as well as a e2fsck.conf profile option, to disable extent tree optimization.

---
 e2fsck/e2fsck.8.in      | 4 ++++
 e2fsck/e2fsck.conf.5.in | 4 ++++
 e2fsck/e2fsck.h         | 1 +
 e2fsck/extents.c        | 3 +++
 e2fsck/unix.c           | 8 ++++++++
 5 files changed, 20 insertions(+)
e2fsck/e2fsck.8.in +4 -0
index 915273d8..4ad575f4 100644
@@ -226,6 +226,10 @@ option may prevent you from further manual data recovery.
 Do not attempt to discard free blocks and unused inode blocks. This option is
 exactly the opposite of discard option. This is set as default.
 .TP
+.BI no_optimize_extents
+Do not offer to optimize the extent tree by eliminating unnecessary
+width or depth.
+.TP
 .BI readahead_kb
 Use this many KiB of memory to pre-fetch metadata in the hopes of reducing
 e2fsck runtime.  By default, this is set to the size of two block groups' inode
e2fsck/e2fsck.conf.5.in +0 -0
index 0bfc76ab..94525baf 100644

e2fsck/e2fsck.conf.5.in of that type are squelched. This can be useful if the console is slow · line 210

Quoted 6 lines
e2fsck/e2fsck.conf.5.in +3 -0
@@ -205,6 +205,10 @@ of that type are squelched.  This can be useful if the console is slow
 (i.e., connected to a serial port) and so a large amount of output could
 end up delaying the boot process for a long time (potentially hours).
 .TP
+.I no_optimize_extents
+Do not offer to optimize the extent tree by eliminating unnecessary
+width or depth.

Looks ok,

/me wonders if there's a way to detect that we're being run by initscripts/systemd/whatever on a device that is mounted ro, and skip the optimization step to avoid the reboot.

OTOH I wonder why the initramfs (if available) doesn't just build in e2fsck and fsck the root device before mounting the fs...

--D

Quoted 59 lines
e2fsck/e2fsck.conf.5.in +1 -0
+.TP
 .I readahead_mem_pct
 Use this percentage of memory to try to read in metadata blocks ahead of the
 main e2fsck thread.  This should reduce run times, depending on the speed of
e2fsck/e2fsck.h +1 -0
index ecd3b1e1..a4b5fb24 100644
@@ -169,6 +169,7 @@ struct resource_track {
 #define E2F_OPT_DISCARD		0x2000
 #define E2F_OPT_CONVERT_BMAP	0x4000 /* convert blockmap to extent */
 #define E2F_OPT_FIXES_ONLY	0x8000 /* skip all optimizations */
+#define E2F_OPT_NOOPT_EXTENTS	0x10000 /* don't optimize extents */
 
 /*
  * E2fsck flags
e2fsck/extents.c +3 -0
index 1df31d71..d9410028 100644
@@ -518,6 +518,9 @@ errcode_t e2fsck_should_rebuild_extents(e2fsck_t ctx,
 	if (eti->force_rebuild)
 		goto rebuild;
 
+	if (ctx->options & E2F_OPT_NOOPT_EXTENTS)
+		return 0;
+
 	extents_per_block = (ctx->fs->blocksize -
 			     sizeof(struct ext3_extent_header)) /
 			    sizeof(struct ext3_extent);
e2fsck/unix.c +8 -0
index b7322bc6..b6025535 100644
@@ -709,6 +709,9 @@ static void parse_extended_opts(e2fsck_t ctx, const char *opts)
 		} else if (strcmp(token, "nodiscard") == 0) {
 			ctx->options &= ~E2F_OPT_DISCARD;
 			continue;
+		} else if (strcmp(token, "no_optimize_extents") == 0) {
+			ctx->options |= E2F_OPT_NOOPT_EXTENTS;
+			continue;
 		} else if (strcmp(token, "log_filename") == 0) {
 			if (!arg)
 				extended_usage++;
@@ -1007,6 +1010,11 @@ static errcode_t PRS(int argc, char *argv[], e2fsck_t *ret_ctx)
 	if (c)
 		verbose = 1;
 
+	profile_get_boolean(ctx->profile, "options", "no_optimize_extents",
+			    0, 0, &c);
+	if (c)
+		ctx->options |= E2F_OPT_NOOPT_EXTENTS;
+
 	if (ctx->readahead_kb == ~0ULL) {
 		profile_get_integer(ctx->profile, "options",
 				    "readahead_mem_pct", 0, -1, &c);
Signature · 1 line
2.11.0.rc0.7.gbe5a750
#4 lore raw

Re: [PATCH 3/3] e2fsck: allow extent tree optimization to be disabled

Quoted 4 lines · Darrick J. Wong

/me wonders if there's a way to detect that we're being run by initscripts/systemd/whatever on a device that is mounted ro, and skip the optimization step to avoid the reboot.

That wasn't actually the reason for this option; but adding feature where which does this for the root file system mounted read-only, the optimization is avoided would make sense, yes.

Quoted 2 lines

OTOH I wonder why the initramfs (if available) doesn't just build in e2fsck and fsck the root device before mounting the fs...

Debian does this already. RHEL is just behind the times. :-)

- Ted

#6 lore raw
In reply to Darrick J. Wong
Quoted 5 lines · Darrick J. Wong

Why not calculate the change in quota use by comparing the inode's i_blocks in between the ext2fs_iblk_sub_blocks call and the end of the ext2fs_extent_insert loop, rather than adding a counter to the global e2fsck state?

Good point. That's a much simpler patch. I now have:

Quoted 1 line

From 403bcb668e4f3d11baa5a503aff17fdaee5bf3c8 Mon Sep 17 00:00:00 2001

From: Theodore Ts'o <[email protected]>
Date: Sat, 15 Apr 2017 00:29:46 -0400
Subject: [PATCH 1/3] e2fsck: update quota when optimizing the extent tree

If quota is enabled, optimizing the extent tree wouldn't update the in-memory quota statistics, so that a subsequent e2fsck run would show that the quota usage statistics on disk were incorrect.

Google-Bug-Id: 36391645

---
 e2fsck/e2fsck.c  |  1 +
 e2fsck/e2fsck.h  |  1 +
 e2fsck/extents.c | 23 ++++++++++++++++++-----
 e2fsck/pass1.c   |  1 +
 4 files changed, 21 insertions(+), 5 deletions(-)
e2fsck/e2fsck.c +1 -0
index 0f9da46a..4baedbac 100644
@@ -169,6 +169,7 @@ errcode_t e2fsck_reset_context(e2fsck_t ctx)
 	ctx->fs_fragmented = 0;
 	ctx->fs_fragmented_dir = 0;
 	ctx->large_files = 0;
+	ctx->clusters_allocated = 0;
 
 	for (i=0; i < MAX_EXTENT_DEPTH_COUNT; i++)
 		ctx->extent_depth_count[i] = 0;
e2fsck/e2fsck.h +1 -0
index f3568106..ecd3b1e1 100644
@@ -372,6 +372,7 @@ struct e2fsck_struct {
 	profile_t	profile;
 	int blocks_per_page;
 	ext2_u32_list encrypted_dirs;
+	__u32 clusters_allocated;
 
 	/* Reserve blocks for root and l+f re-creation */
 	blk64_t root_repair_block, lnf_repair_block;
e2fsck/extents.c +18 -5
index c4167e16..1df31d71 100644
@@ -208,17 +208,21 @@ static int find_blocks(ext2_filsys fs, blk64_t *blocknr, e2_blkcnt_t blockcnt,
 static errcode_t rebuild_extent_tree(e2fsck_t ctx, struct extent_list *list,
 				     ext2_ino_t ino)
 {
-	struct ext2_inode	inode;
+	struct ext2_inode_large	inode;
+	struct ext2_inode	*inode_ptr;
 	errcode_t		retval;
 	ext2_extent_handle_t	handle;
 	unsigned int		i, ext_written;
 	struct ext2fs_extent	*ex, extent;
+	__u32			start_val;
 
 	list->count = 0;
 	list->blocks_freed = 0;
 	list->ino = ino;
 	list->ext_read = 0;
-	e2fsck_read_inode(ctx, ino, &inode, "rebuild_extents");
+	inode_ptr = (struct ext2_inode *) &inode;
+	e2fsck_read_inode_full(ctx, ino, inode_ptr, sizeof(inode),
+			       "rebuild_extents");
 
 	/* Skip deleted inodes and inline data files */
 	if (inode.i_links_count == 0 ||
@@ -248,16 +252,19 @@ extents_loaded:
 	memset(inode.i_block, 0, sizeof(inode.i_block));
 
 	/* Make a note of freed blocks */
-	retval = ext2fs_iblk_sub_blocks(ctx->fs, &inode, list->blocks_freed);
+	quota_data_sub(ctx->qctx, &inode, ino,
+		       list->blocks_freed * ctx->fs->blocksize);
+	retval = ext2fs_iblk_sub_blocks(ctx->fs, inode_ptr, list->blocks_freed);
 	if (retval)
 		goto err;
 
 	/* Now stuff extents into the file */
-	retval = ext2fs_extent_open2(ctx->fs, ino, &inode, &handle);
+	retval = ext2fs_extent_open2(ctx->fs, ino, inode_ptr, &handle);
 	if (retval)
 		goto err;
 
 	ext_written = 0;
+	start_val = ctx->clusters_allocated;
 	for (i = 0, ex = list->extents; i < list->count; i++, ex++) {
 		memcpy(&extent, ex, sizeof(struct ext2fs_extent));
 		extent.e_flags &= EXT2_EXTENT_FLAGS_UNINIT;
@@ -295,11 +302,17 @@ extents_loaded:
 		ext_written++;
 	}
 
+	if (start_val != ctx->clusters_allocated) {
+		__u32 num = ctx->clusters_allocated - start_val;
+		quota_data_add(ctx->qctx, &inode, ino,
+			       num * ctx->fs->blocksize);
+	}
+
 #if defined(DEBUG) || defined(DEBUG_SUMMARY)
 	printf("rebuild: ino=%d extents=%d->%d\n", ino, list->ext_read,
 	       ext_written);
 #endif
-	e2fsck_write_inode(ctx, ino, &inode, "rebuild_extents");
+	e2fsck_write_inode(ctx, ino, inode_ptr, "rebuild_extents");
 
 err2:
 	ext2fs_extent_free(handle);
e2fsck/pass1.c +1 -0
index 188cc562..99e8f66a 100644
@@ -4017,6 +4017,7 @@ static errcode_t e2fsck_get_alloc_block(ext2_filsys fs, blk64_t goal,
 	}
 
 	*ret = new_block;
+	ctx->clusters_allocated++;
 	return (0);
 }
 
Signature · 1 line
2.11.0.rc0.7.gbe5a750
#7 lore raw
Quoted 33 lines · Theodore Ts'o
Quoted 5 lines · Darrick J. Wong

Why not calculate the change in quota use by comparing the inode's i_blocks in between the ext2fs_iblk_sub_blocks call and the end of the ext2fs_extent_insert loop, rather than adding a counter to the global e2fsck state?

Good point. That's a much simpler patch. I now have:

From 403bcb668e4f3d11baa5a503aff17fdaee5bf3c8 Mon Sep 17 00:00:00 2001
From: Theodore Ts'o <[email protected]>
Date: Sat, 15 Apr 2017 00:29:46 -0400
Subject: [PATCH 1/3] e2fsck: update quota when optimizing the extent tree

If quota is enabled, optimizing the extent tree wouldn't update the in-memory quota statistics, so that a subsequent e2fsck run would show that the quota usage statistics on disk were incorrect.

Google-Bug-Id: 36391645

---
 e2fsck/e2fsck.c  |  1 +
 e2fsck/e2fsck.h  |  1 +
 e2fsck/extents.c | 23 ++++++++++++++++++-----
 e2fsck/pass1.c   |  1 +
 4 files changed, 21 insertions(+), 5 deletions(-)
e2fsck/e2fsck.c +0 -0
index 0f9da46a..4baedbac 100644

e2fsck/e2fsck.c errcode_t e2fsck_reset_context(e2fsck_t ctx) · line 172

Quoted 4 lines
e2fsck/e2fsck.c +1 -0
@@ -169,6 +169,7 @@ errcode_t e2fsck_reset_context(e2fsck_t ctx)
 	ctx->fs_fragmented = 0;
 	ctx->fs_fragmented_dir = 0;
 	ctx->large_files = 0;
+	ctx->clusters_allocated = 0;

Uh... isn't this the same patch as before?

--D

Quoted 99 lines
e2fsck/e2fsck.c +0 -0
 	for (i=0; i < MAX_EXTENT_DEPTH_COUNT; i++)
 		ctx->extent_depth_count[i] = 0;
e2fsck/e2fsck.h +1 -0
index f3568106..ecd3b1e1 100644
@@ -372,6 +372,7 @@ struct e2fsck_struct {
 	profile_t	profile;
 	int blocks_per_page;
 	ext2_u32_list encrypted_dirs;
+	__u32 clusters_allocated;
 
 	/* Reserve blocks for root and l+f re-creation */
 	blk64_t root_repair_block, lnf_repair_block;
e2fsck/extents.c +18 -5
index c4167e16..1df31d71 100644
@@ -208,17 +208,21 @@ static int find_blocks(ext2_filsys fs, blk64_t *blocknr, e2_blkcnt_t blockcnt,
 static errcode_t rebuild_extent_tree(e2fsck_t ctx, struct extent_list *list,
 				     ext2_ino_t ino)
 {
-	struct ext2_inode	inode;
+	struct ext2_inode_large	inode;
+	struct ext2_inode	*inode_ptr;
 	errcode_t		retval;
 	ext2_extent_handle_t	handle;
 	unsigned int		i, ext_written;
 	struct ext2fs_extent	*ex, extent;
+	__u32			start_val;
 
 	list->count = 0;
 	list->blocks_freed = 0;
 	list->ino = ino;
 	list->ext_read = 0;
-	e2fsck_read_inode(ctx, ino, &inode, "rebuild_extents");
+	inode_ptr = (struct ext2_inode *) &inode;
+	e2fsck_read_inode_full(ctx, ino, inode_ptr, sizeof(inode),
+			       "rebuild_extents");
 
 	/* Skip deleted inodes and inline data files */
 	if (inode.i_links_count == 0 ||
@@ -248,16 +252,19 @@ extents_loaded:
 	memset(inode.i_block, 0, sizeof(inode.i_block));
 
 	/* Make a note of freed blocks */
-	retval = ext2fs_iblk_sub_blocks(ctx->fs, &inode, list->blocks_freed);
+	quota_data_sub(ctx->qctx, &inode, ino,
+		       list->blocks_freed * ctx->fs->blocksize);
+	retval = ext2fs_iblk_sub_blocks(ctx->fs, inode_ptr, list->blocks_freed);
 	if (retval)
 		goto err;
 
 	/* Now stuff extents into the file */
-	retval = ext2fs_extent_open2(ctx->fs, ino, &inode, &handle);
+	retval = ext2fs_extent_open2(ctx->fs, ino, inode_ptr, &handle);
 	if (retval)
 		goto err;
 
 	ext_written = 0;
+	start_val = ctx->clusters_allocated;
 	for (i = 0, ex = list->extents; i < list->count; i++, ex++) {
 		memcpy(&extent, ex, sizeof(struct ext2fs_extent));
 		extent.e_flags &= EXT2_EXTENT_FLAGS_UNINIT;
@@ -295,11 +302,17 @@ extents_loaded:
 		ext_written++;
 	}
 
+	if (start_val != ctx->clusters_allocated) {
+		__u32 num = ctx->clusters_allocated - start_val;
+		quota_data_add(ctx->qctx, &inode, ino,
+			       num * ctx->fs->blocksize);
+	}
+
 #if defined(DEBUG) || defined(DEBUG_SUMMARY)
 	printf("rebuild: ino=%d extents=%d->%d\n", ino, list->ext_read,
 	       ext_written);
 #endif
-	e2fsck_write_inode(ctx, ino, &inode, "rebuild_extents");
+	e2fsck_write_inode(ctx, ino, inode_ptr, "rebuild_extents");
 
 err2:
 	ext2fs_extent_free(handle);
e2fsck/pass1.c +1 -0
index 188cc562..99e8f66a 100644
@@ -4017,6 +4017,7 @@ static errcode_t e2fsck_get_alloc_block(ext2_filsys fs, blk64_t goal,
 	}
 
 	*ret = new_block;
+	ctx->clusters_allocated++;
 	return (0);
 }
 
Signature · 1 line
2.11.0.rc0.7.gbe5a750
#8 lore raw

Oops, Sorry. Now with the corret version of the patch.

- Ted

Quoted 1 line

From bc177d425d4fe33b0ae774b218b055465a840ca1 Mon Sep 17 00:00:00 2001

From: Theodore Ts'o <[email protected]>
Date: Sat, 15 Apr 2017 00:29:46 -0400
Subject: [PATCH] e2fsck: update quota when optimizing the extent tree

If quota is enabled, optimizing the extent tree wouldn't update the in-memory quota statistics, so that a subsequent e2fsck run would show that the quota usage statistics on disk were incorrect.

Google-Bug-Id: 36391645

---
 e2fsck/extents.c | 26 +++++++++++++++++++++-----
 1 file changed, 21 insertions(+), 5 deletions(-)
e2fsck/extents.c +21 -5
index c4167e16..7f28e6dd 100644
@@ -208,17 +208,19 @@ static int find_blocks(ext2_filsys fs, blk64_t *blocknr, e2_blkcnt_t blockcnt,
 static errcode_t rebuild_extent_tree(e2fsck_t ctx, struct extent_list *list,
 				     ext2_ino_t ino)
 {
-	struct ext2_inode	inode;
+	struct ext2_inode_large	inode;
 	errcode_t		retval;
 	ext2_extent_handle_t	handle;
 	unsigned int		i, ext_written;
 	struct ext2fs_extent	*ex, extent;
+	blk64_t			start_val, delta;
 
 	list->count = 0;
 	list->blocks_freed = 0;
 	list->ino = ino;
 	list->ext_read = 0;
-	e2fsck_read_inode(ctx, ino, &inode, "rebuild_extents");
+	e2fsck_read_inode_full(ctx, ino, EXT2_INODE(&inode), sizeof(inode),
+			       "rebuild_extents");
 
 	/* Skip deleted inodes and inline data files */
 	if (inode.i_links_count == 0 ||
@@ -248,16 +250,20 @@ extents_loaded:
 	memset(inode.i_block, 0, sizeof(inode.i_block));
 
 	/* Make a note of freed blocks */
-	retval = ext2fs_iblk_sub_blocks(ctx->fs, &inode, list->blocks_freed);
+	quota_data_sub(ctx->qctx, &inode, ino,
+		       list->blocks_freed * ctx->fs->blocksize);
+	retval = ext2fs_iblk_sub_blocks(ctx->fs, EXT2_INODE(&inode),
+					list->blocks_freed);
 	if (retval)
 		goto err;
 
 	/* Now stuff extents into the file */
-	retval = ext2fs_extent_open2(ctx->fs, ino, &inode, &handle);
+	retval = ext2fs_extent_open2(ctx->fs, ino, EXT2_INODE(&inode), &handle);
 	if (retval)
 		goto err;
 
 	ext_written = 0;
+	start_val = ext2fs_inode_i_blocks(ctx->fs, EXT2_INODE(&inode));
 	for (i = 0, ex = list->extents; i < list->count; i++, ex++) {
 		memcpy(&extent, ex, sizeof(struct ext2fs_extent));
 		extent.e_flags &= EXT2_EXTENT_FLAGS_UNINIT;
@@ -295,11 +301,21 @@ extents_loaded:
 		ext_written++;
 	}
 
+	delta = ext2fs_inode_i_blocks(ctx->fs, EXT2_INODE(&inode)) - start_val;
+	if (delta) {
+		if (!ext2fs_has_feature_huge_file(ctx->fs->super) ||
+		    !(inode.i_flags & EXT4_HUGE_FILE_FL))
+			delta <<= 9;
+		else
+			delta *= ctx->fs->blocksize;
+		quota_data_add(ctx->qctx, &inode, ino, delta);
+	}
+
 #if defined(DEBUG) || defined(DEBUG_SUMMARY)
 	printf("rebuild: ino=%d extents=%d->%d\n", ino, list->ext_read,
 	       ext_written);
 #endif
-	e2fsck_write_inode(ctx, ino, &inode, "rebuild_extents");
+	e2fsck_write_inode(ctx, ino, EXT2_INODE(&inode), "rebuild_extents");
 
 err2:
 	ext2fs_extent_free(handle);
Signature · 1 line
2.11.0.rc0.7.gbe5a750
#9 lore raw
Quoted 30 lines · Theodore Ts'o

From bc177d425d4fe33b0ae774b218b055465a840ca1 Mon Sep 17 00:00:00 2001
From: Theodore Ts'o <[email protected]>
Date: Sat, 15 Apr 2017 00:29:46 -0400
Subject: [PATCH] e2fsck: update quota when optimizing the extent tree

If quota is enabled, optimizing the extent tree wouldn't update the in-memory quota statistics, so that a subsequent e2fsck run would show that the quota usage statistics on disk were incorrect.

Google-Bug-Id: 36391645

---
e2fsck/extents.c | 26 +++++++++++++++++++++-----

1 file changed, 21 insertions(+), 5 deletions(-)
e2fsck/extents.c +1 -1
index c4167e16..7f28e6dd 100644
@@ -208,17 +208,19 @@ static int find_blocks(ext2_filsys fs, blk64_t *blocknr, e2_blkcnt_t blockcnt,
 static errcode_t rebuild_extent_tree(e2fsck_t ctx, struct extent_list *list,
 				     ext2_ino_t ino)
 {
-	struct ext2_inode	inode;
+	struct ext2_inode_large	inode;
 	errcode_t		retval;
 	ext2_extent_handle_t	handle;
 	unsigned int		i, ext_written;

e2fsck/extents.c static int find_blocks(ext2_filsys fs, blk64_t *blocknr, e2_blkcnt_t blockcnt, · line 223

Quoted 10 lines
e2fsck/extents.c +3 -1
 	struct ext2fs_extent	*ex, extent;
+	blk64_t			start_val, delta;
 
 	list->count = 0;
 	list->blocks_freed = 0;
 	list->ino = ino;
 	list->ext_read = 0;
-	e2fsck_read_inode(ctx, ino, &inode, "rebuild_extents");
+	e2fsck_read_inode_full(ctx, ino, EXT2_INODE(&inode), sizeof(inode),
+			       "rebuild_extents");

I'm always a bit puzzled why e2fsck_read_inode_full() takes ext2_inode as an argument instead of ext2_inode_large, but that isn't the fault of this patch.

e2fsck/extents.c extents_loaded: · line 254

Quoted 10 lines
e2fsck/extents.c +2 -1
 	/* Skip deleted inodes and inline data files */
 	if (inode.i_links_count == 0 ||
@@ -248,16 +250,20 @@ extents_loaded:
 	memset(inode.i_block, 0, sizeof(inode.i_block));
 
 	/* Make a note of freed blocks */
-	retval = ext2fs_iblk_sub_blocks(ctx->fs, &inode, list->blocks_freed);
+	quota_data_sub(ctx->qctx, &inode, ino,
+		       list->blocks_freed * ctx->fs->blocksize);

Should this quota_data_sub() call be after ext2fs_iblk_sub_blocks() has completed successfully, or are the blocks already freed at this point and only the inode->i_blocks counter would be outdated?

e2fsck/extents.c extents_loaded: · line 266

Quoted 13 lines
e2fsck/extents.c +4 -1
+	retval = ext2fs_iblk_sub_blocks(ctx->fs, EXT2_INODE(&inode),
+					list->blocks_freed);
 	if (retval)
 		goto err;
 
 	/* Now stuff extents into the file */
-	retval = ext2fs_extent_open2(ctx->fs, ino, &inode, &handle);
+	retval = ext2fs_extent_open2(ctx->fs, ino, EXT2_INODE(&inode), &handle);
 	if (retval)
 		goto err;
 
 	ext_written = 0;
+	start_val = ext2fs_inode_i_blocks(ctx->fs, EXT2_INODE(&inode));

In theory this could go at the start before any changes are made to the inode, and then there wouldn't be the need for the intermediate call to quota_data_sub() above? That avoids a small amount of overhead, and also avoids the more complicated issue of the quota file being modified even if this operation hits an error and is skipped (i.e. any "goto err" case).

Cheers, Andreas

Quoted 25 lines
e2fsck/extents.c +11 -1
 	for (i = 0, ex = list->extents; i < list->count; i++, ex++) {
 		memcpy(&extent, ex, sizeof(struct ext2fs_extent));
 		extent.e_flags &= EXT2_EXTENT_FLAGS_UNINIT;
@@ -295,11 +301,21 @@ extents_loaded:
 		ext_written++;
 	}
 
+	delta = ext2fs_inode_i_blocks(ctx->fs, EXT2_INODE(&inode)) - start_val;
+	if (delta) {
+		if (!ext2fs_has_feature_huge_file(ctx->fs->super) ||
+		    !(inode.i_flags & EXT4_HUGE_FILE_FL))
+			delta <<= 9;
+		else
+			delta *= ctx->fs->blocksize;
+		quota_data_add(ctx->qctx, &inode, ino, delta);
+	}
+
 #if defined(DEBUG) || defined(DEBUG_SUMMARY)
 	printf("rebuild: ino=%d extents=%d->%d\n", ino, list->ext_read,
 	       ext_written);
 #endif
-	e2fsck_write_inode(ctx, ino, &inode, "rebuild_extents");
+	e2fsck_write_inode(ctx, ino, EXT2_INODE(&inode), "rebuild_extents");
 

err2:

Quoted 4 lines
	ext2fs_extent_free(handle);

--
2.11.0.rc0.7.gbe5a750

Cheers, Andreas

#10 lore raw
Quoted 4 lines · Andreas Dilger

Should this quota_data_sub() call be after ext2fs_iblk_sub_blocks() has completed successfully, or are the blocks already freed at this point and only the inode->i_blocks counter would be outdated?

The blocks are already freed at this point.

Quoted 6 lines

In theory this could go at the start before any changes are made to the inode, and then there wouldn't be the need for the intermediate call to quota_data_sub() above? That avoids a small amount of overhead, and also avoids the more complicated issue of the quota file being modified even if this operation hits an error and is skipped (i.e. any "goto err" case).

We're actually not modifying the quota file at this point; we're just modifying the in-memory "correct" quota data numbers.

Also, if we fail in the middle, given that the extent tree blocks have been freed and potentially partially reused, the fact that the quota file has been modified is the least of our problems. In theory we could save the list of old extent tree index blocks, and try to use new blocks for the new extent tree index blocks as much as possible. But that's not true today.

- Ted