Make Ext3 fsck way faster [2.6.24-rc6 -mm patch]
Thanks a lot for the feedback Andrew.
I've addressed your comments, details follow. I've also retested this change and implemented a minor optimization after which sequential read with metaclustering is slightly better than normal.
Quoted 6 lines · Andrew Morton
Quoted 2 lines · On Mon, 14 Jan 2008 08:39:01 -0500 Abhishek Rai <[email protected]> wrote:
This is the patch for 2.6.24-rc6 -mm tree, please let me know if anyone would like a patch against another recent kernel. Ingo Molnar has already posted a patch for 2.6.24-rc7.
Quoted 7 lines
Please always retain and maintain the full changelog with each version of a patch. The changelog in your earlier "Clustering indirect blocks in Ext3" patch was good. I retained that (after renaming it to "ext3: indirect block clustering" rather than this dopey name) and then, err, dropped the patch ;)
Please cc linux-ext4 on ext2/3/4-related work.
OK
Quoted 2 lines
Please update
Documentation/filesystems/ext3.txtwhen adding new mount options.
Added.
Quoted 17 lines
Here's a quick low-levelish nitpickingish implementation review. I haven't thought about the design-level things yet.
This code will need careful checking regarding the journalling side of things - to verify that if the machine crashes at any stage, recovery will produce a consistent and secure fs. It is all-nigh impossible to spot bugs in this area via the usual kernel bug reporting process and it is very hard to effectively runtime test recovery even in a development situation. Careful review unusually important here.
hm, so
ext3_count_free()hits prime time.
ext3_count_free()should be converted to use the standardhweight8(). Really it should be converted tohweight_long()orhweight32()or something
- it is presently probably inefficient.
Quoted 5 lines
This function appears to be called frequently and it calls the slow-looking
ext3_count_free()under spinlock. This might have scalability problems on the right machine running the right workload.Can we address that before we hit it?
Thanks for pointing this out. ext3_count_free() now uses hweight*() functions.
Quoted 2 lines
Far too large to inline. Please just remove all inlines from the patch. The compiler will sort it out.
Removed 'inline' keyword from all newly added functions except one (allow_mc_alloc()) which will most definitely get inlined.
Quoted 19 lines
Quoted 6 lines
(quoted diff) +6
-0+static ext3_grpblk_t +bitmap_find_prev_zero_bit(char *map, ext3_grpblk_t start, ext3_grpblk_t lowest) +{ + ext3_grpblk_t k, blk; + + k = start & ~7;Review
Quoted 10 lines
(quoted diff) +10
-0+ while (lowest <= k) { + if (map[k/8] != '\255' && + (blk = ext3_find_next_zero_bit(map, k + 8, k)) + < (k + 8)) + return blk; + + k -= 8; + } + return -1; +}Please rename `k' to something meaningful.
Quoted 5 lines
The logic in here looks odd. If (map[k/8] !=
0xff) then theext3_find_next_zero_bit()cannot fail. So why do we test for it in this fashion?Perhaps some commnetary is needed here.
Rewrote this function to search for a zero bit in strictly decreasing order of block numbers. The above code was doing the search in staircase pattern which was resulting in some unwanted fragmentation. The new code also improves sequential read performance slightly. Please take a look now.
Quoted 9 lines
Quoted 9 lines
(quoted diff) +9
-0+static ext3_grpblk_t +bitmap_search_prev_usable_block(ext3_grpblk_t start, struct buffer_head *bh, + ext3_grpblk_t lowest) +{ + ext3_grpblk_t next; + struct journal_head *jh = bh2jh(bh); + + /* + * The bitmap search --- search backward alternately through the actual
Quoted 8 lines
Review
Quoted 4 lines
(quoted diff) +4
-0+ * bitmap and the last-committed copy until we find a bit free in + * both + */ + while (start >= lowest) {Little style nit: in
bitmap_find_prev_zero_bit()you used (lowest < k) which is a little surprising but I assumeed that you were following the (a < b < c) layout. But here you're not doing that.
As mentioned above, this code is gone now, new code doesn't have this inconsistency.
Quoted 4 lines
Whoa. The first ever use of
cpu_relaxin a filesystem (apart from sysfs, but that's hardy endorsement).What are you trying to do here? Why was this needed?
Agreed, it was not very useful / was an overkill, so nuked it.
Quoted 58 lines
Quoted 58 lines
(quoted diff) +57
-0+ } + if (allocated < indirect_blks) + blk = bitmap_search_prev_usable_block(blk, bitmap_bh, + mc_start); + } + return allocated; +} + +/* + * check_allocated_blocks: + * Helper function for ext3_new_blocks. Checks newly allocated block + * numbers. + */ +int check_allocated_blocks(ext3_fsblk_t blk, unsigned long num, + struct super_block *sb, int group_no, + struct ext3_group_desc *gdp, + struct buffer_head *bitmap_bh) +{ + struct ext3_super_block *es = EXT3_SB(sb)->s_es; + struct ext3_sb_info *sbi = EXT3_SB(sb); + ext3_fsblk_t grp_blk = blk - ext3_group_first_block_no(sb, group_no); + + if (in_range(le32_to_cpu(gdp->bg_block_bitmap), blk, num) || + in_range(le32_to_cpu(gdp->bg_inode_bitmap), blk, num) || + in_range(blk, le32_to_cpu(gdp->bg_inode_table), + EXT3_SB(sb)->s_itb_per_group) || + in_range(blk + num - 1, le32_to_cpu(gdp->bg_inode_table), + EXT3_SB(sb)->s_itb_per_group)) { + ext3_error(sb, "ext3_new_blocks", + "Allocating block in system zone - " + "blocks from "E3FSBLK", length %lu", + blk, num); + return 1; + } + +#ifdef CONFIG_JBD_DEBUG + { + struct buffer_head *debug_bh; + + /* Record bitmap buffer state in the newly allocated block */ + debug_bh = sb_find_get_block(sb, blk); + if (debug_bh) { + BUFFER_TRACE(debug_bh, "state when allocated"); + BUFFER_TRACE2(debug_bh, bitmap_bh, "bitmap state"); + brelse(debug_bh); + } + } + jbd_lock_bh_state(bitmap_bh); + spin_lock(sb_bgl_lock(sbi, group_no)); + if (buffer_jbd(bitmap_bh) && bh2jh(bitmap_bh)->b_committed_data) { + int i; + + for (i = 0; i < num; i++) { + if (ext3_test_bit(grp_blk+i, + bh2jh(bitmap_bh)->b_committed_data)) + printk(KERN_ERR "%s: block was unexpectedly set" + " in b_committed_data\n", __FUNCTION__);
Quoted 8 lines
Review
Quoted 6 lines
(quoted diff) +6
-0+ } + } + ext3_debug("found bit %d\n", grp_blk); + spin_unlock(sb_bgl_lock(sbi, group_no)); + jbd_unlock_bh_state(bitmap_bh); +#endifHas the
CONFIG_JBD_DEBUG=ycode been tested?
This piece of code was simply a reshuffle of existing code. In particular, the new check_allocated_blocks() function contains only what the old ext3_new_blocks() used to have for checking newly allocated blocks. So I'd think we don't need explicit testing of these cases as such. But if you think I should test it, please let me know and I'll be glad to do so.
Quoted 4 lines
Quoted 1 line
+ /* Should this be called before
ext3_journal_dirty_metadata? */If you're concerned here I'd suggest that you formulate a question for the ext3/4 maintainers to think about.
Thanks for the suggestion, I've emailed linux-ext4.
Quoted 3 lines
Quoted 1 line
+ * waier can unblock.
typo.
Fixed
Quoted 11 lines
Review
Quoted 6 lines
+ */
(quoted diff) +5
-0+static void ext3_ind_read_end_bio(struct bio *bio, int err) +{ + struct ext3_ind_read_info *read_info = bio->bi_private; + struct buffer_head *bh; + int uptodate = !err && test_bit(BIO_UPTODATE, &bio->bi_flags);It's pretty regrettable that ext3 now starts using bios directly. All of jbd and ext3 has thus far avoided this additional coupling.
Why was this necessary?
...
Quoted 7 lines
Again, I really don't see why we needed to dive into the BIO layer for this. Why not use
submit_bh()and friends for all of this?Also, why is it necessary to do block sorting at the filesystem level? The block layer does that.
There are two main factors which are responsible for the way this patch was using bios.
(1) I initially started with what seemed like the most obvious choice to me, which is also what you suggested: issue read to each indirect block individually using submit_bh(). However, in my experiments I observed that coalescing of requests was sometimes suboptimal. I tried to investigate this behavior but gave up and started directly invoking bio layer to ensure maximum coalescing.
(2) The second problem I had was that bio layer would sometimes not behave correctly when invoked with a physically-discontiguous range of blocks, even though the interface suggests that this should work without any problems. Someone told me that it's "common understanding" that submit_bio() should only be used for a physically contiguous range. So I had to break the discontiguous requests into smaller contiguous requests which would each be sorted to be contiguous before invoking submit_bio.
I was not aware that Ext3 developers have put in a lot of effort to avoid directly accessing the bio layer. But now that you pointed this out, I went back and retried using submit_bh() for each buffer and turns out it's working fine now. So probably there was something amiss in my blktrace-based analysis previously which led me to believe that the IO stream going to disk is fragmented. The new patch uses submit_bh() here which makes it smaller by almost 300 lines of code.
Quoted 2 lines
This is far too large to be inlined.
Removed 'inline'.
Quoted 4 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ max_read = min_t(unsigned long, remaining_ind_blks_in_dind, + remaining_ind_blks_before_eof);Can use plain old
min()here.
Done
Quoted 8 lines
Quoted 1 line
+
BUG_ON(max_read < 1);Please prefer to use the (much) friendlier
ext3_error()over the user-hostile BUG. Where it makes sense.This is especially the case where the assertion could be triggered by corrupted disk contents - doing a
BUG()in response to that is a coding bug.
Done
Quoted 4 lines
Quoted 4 lines
(quoted diff) +4
-0+static void ind_info_swap(void *a, void *b, int size) +{ + struct ind_block_info *info_a = (struct ind_block_info *)a; + struct ind_block_info *info_b = (struct ind_block_info *)b;
Quoted 8 lines
Review
Quoted 6 lines
(quoted diff) +6
-0+ struct ind_block_info tmp; + + tmp = *info_a; + *info_a = *info_b; + *info_b = tmp; +}Unneeded (and undesirable) casts of void*.
Fixed
Quoted 23 lines
Quoted 18 lines
+/*
+ *ext3_read_indblocks_async--+ * @sb: super block + * @ind_blocks[]: array of indirect block numbers on disk + * @count: maximum number of indirect blocks to read + * @first_bh: buffer_head for indirect block ind_blocks[0], may be + * NULL + * @seq_prefetch: if this is part of a sequential prefetch and buffers' + * prefetch bit must be set. + * @blocks_done: number of blocks considered for prefetching. + * + * Issue a single bio request to read upto count buffers identified in + * ind_blocks[]. Fewer than count buffers may be read in some cases: + * - If a buffer is found to be uptodate and it's prefetch bit is set, we + * don't look at any more buffers as they will most likely be in the cache. + * - We skip buffers we cannot lock without blocking (except for first_bh + * if specified). + * - We skip buffers beyond a certain range on disk.Quoted 4 lines
+ *
+ * This function must issue read onfirst_bhif specified unless of course
+ * it's already uptodate.
+ */
Quoted 2 lines
This comment is almost-but-not-quite in kerneldoc format. Please review the /** comments in ext3 and convert newly-added commetns to match.
Fixed all comments in the patch.
Quoted 10 lines
Quoted 1 line
+ count =
min_t(unsigned long, count, EXT3_IND_READ_MAX);Both count and
EXT3_IND_READ_MAXare plain old int. Forcing them to ulong for this comparison is a little odd.Please check whether those two things have appropriate types - can they ever legitimately go negative? I doubt it, in which case they should have unsigned types.
Please review all newly-added min_t's for these things.
Checked all newly added min and min_t's and changed some according to your suggestion.
Quoted 5 lines
Review
Quoted 3 lines
(quoted diff) +3
-0+ *blocks_done = 0UL; + + if (count == 1 && first_bh) {This comparison could do with a comment explaining what's happening?
Added comment.
Quoted 5 lines
Quoted 1 line
+ brelse(bh);
brelse()is an old-fashioned thing which checks for a NULL arg. Unless you really have a bh* which might legitimately be NULL, please preferput_bh().
Fixed.
Quoted 5 lines
I'm wondering about the interaction between this code and the
buffer_boundary()logic. I guess we should disable thebuffer_boundary()handling when this code is in effect. Have you reviewed and tested that aspect?
Thanks for pointing this out, I had totally missed this issue in my change. I've now made the call to set_buffer_boundary() in ext3_get_blocks_handle() subject to metacluster option being set.
Quoted 14 lines
Review static int ext3_fill_super (struct super · line 1708
Quoted 12 lines
(quoted diff) +7
-0@@ -1692,6 +1699,13 @@ static int ext3_fill_super (struct super } sbi->s_frags_per_block = 1; sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group); + if (test_opt(sb, METACLUSTER)) { + sbi->s_nonmc_blocks_per_group = sbi->s_blocks_per_group - + sbi->s_blocks_per_group / 12; + sbi->s_nonmc_blocks_per_group &= ~7; + } else + sbi->s_nonmc_blocks_per_group = sbi->s_blocks_per_group; +hm, magic numbers.
Added comments explaining the magic numbers.
Thanks
Updated patch for linux-2.6.24-rc8 follows.
Documentation/filesystems/ext3.txt +2 -0
@@ -121,6 +121,8 @@ nobh (a) cache disk block mapping info "nobh" option tries to avoid associating buffer heads (supported only for "writeback" mode). +metacluster Enable metaclustering for indirect block allocation. + Specification =============
Binary files linux-2.6.24-rc8-clean/fs/ext3/.balloc.c.swp and linux-2.6.24-rc8-ext3mc/fs/ext3/.balloc.c.swp differ Binary files linux-2.6.24-rc8-clean/fs/ext3/.bitmap.c.swp and linux-2.6.24-rc8-ext3mc/fs/ext3/.bitmap.c.swp differ Binary files linux-2.6.24-rc8-clean/fs/ext3/.inode.c.swp and linux-2.6.24-rc8-ext3mc/fs/ext3/.inode.c.swp differ
fs/ext3/balloc.c +544 -189
916 lines not on this page · raw message
fs/ext3/bitmap.c +5 -13
@@ -11,22 +11,14 @@#include <linux/jbd.h> #include <linux/ext3_fs.h> -#ifdef EXT3FS_DEBUG - -static const int nibblemap[] = {4 ,3 ,3 ,2 ,3 ,2 ,2 ,1 ,3 ,2 ,2 ,1 ,2 ,1 ,1 ,0 }; -unsigned long ext3_count_free (struct buffer_head * map,unsigned int numchars) {unsigned int i;unsigned long sum =0 ; -if (!map) -return (0 ); -for (i =0 ; i < numchars; i++) - sum += nibblemap[map->b_data[i] &0xf ] + - nibblemap[(map->b_data[i] >>4 ) &0xf ]; -return (sum); +for (i =0 ; i < (numchars & ~0x3 ); i +=4 ) + sum +=hweight32 (~(*((unsigned int *)&map->b_data[i]))); +for (; i < numchars; i++) + sum +=hweight8 (~((unsigned char )map->b_data[i])); +return sum; } - -#endif /* EXT3FS_DEBUG */ -
fs/ext3/inode.c +445 -84
636 lines not on this page · raw message
fs/ext3/super.c +43 -8
124 lines not on this page · raw message
include/linux/ext3_fs.h +28 -2
@@ -380,6 +380,7 @@ struct ext3_inode {#define EXT3_MOUNT_QUOTA 0x80000/* Some quota option set */ #define EXT3_MOUNT_USRQUOTA 0x100000/* "old" user quota */ #define EXT3_MOUNT_GRPQUOTA 0x200000/* "old" group quota */ +#define EXT3_MOUNT_METACLUSTER 0x400000/* Indirect block clustering */ /* Compatibility, for having both ext2_fs.h and ext3_fs.h included at once */ #ifndef _LINUX_EXT2_FS_H
@@ -493,6 +494,7 @@ struct ext3_super_block {#ifdef __KERNEL__#include <linux/ext3_fs_i.h> #include <linux/ext3_fs_sb.h> +#include <linux/buffer_head.h> static inline struct ext3_sb_info *EXT3_SB (struct super_block *sb) {return sb->s_fs_info;
@@ -742,6 +744,11 @@ struct dir_private_info {__u32 next_hash; }; +/* Special bh flag used by the metacluster readahead logic. */ +enum ext3_bh_state_bits { +EXT3_BH_PREFETCH = BH_JBD_Sentinel, +}; +/* calculate the first block number of the group */ static inline ext3_fsblk_t ext3_group_first_block_no (struct super_block *sb,unsigned long group_no)
@@ -750,6 +757,24 @@ ext3_group_first_block_no(struct super_ble32_to_cpu (EXT3_SB (sb)->s_es->s_first_data_block); } +static inline void +ext3_set_buffer_prefetch (struct buffer_head *bh) +{ +set_bit (EXT3_BH_PREFETCH , &bh->b_state); +} + +static inline void +ext3_clear_buffer_prefetch (struct buffer_head *bh) +{ +clear_bit (EXT3_BH_PREFETCH , &bh->b_state); +} + +static inline int +ext3_buffer_prefetch (struct buffer_head *bh) +{ +return test_bit (EXT3_BH_PREFETCH , &bh->b_state); +} +/* * Special error return code only used by dx_probe() and its callers. */
@@ -772,8 +797,9 @@ extern int ext3_bg_has_super(struct supeextern unsigned long ext3_bg_num_gdb (struct super_block *sb,int group);extern ext3_fsblk_t ext3_new_block (handle_t *handle,struct inode *inode,ext3_fsblk_t goal,int *errp); -extern ext3_fsblk_t ext3_new_blocks (handle_t *handle,struct inode *inode, -ext3_fsblk_t goal,unsigned long *count,int *errp); +extern int ext3_new_blocks (handle_t *handle,struct inode *inode, +ext3_fsblk_t goal,int indirect_blks,int blks, +ext3_fsblk_t new_blocks[],int *errp);extern void ext3_free_blocks (handle_t *handle,struct inode *inode,ext3_fsblk_t block,unsigned long count); extern void ext3_free_blocks_sb (handle_t *handle, struct super_block *sb,
include/linux/ext3_fs_sb.h +13 -0
@@ -24,6 +24,8 @@#endif #include <linux/rbtree.h> +struct ext3_bg_info ; +/* * third extended-fs super-block data in memory */
@@ -33,6 +35,7 @@ struct ext3_sb_info {unsigned long s_inodes_per_block;/* Number of inodes per block */ unsigned long s_frags_per_group;/* Number of fragments in a group */ unsigned long s_blocks_per_group;/* Number of blocks in a group */ +unsigned long s_nonmc_blocks_per_group;/* Number of non-metacluster blocks in a group */ unsigned long s_inodes_per_group;/* Number of inodes in a group */ unsigned long s_itb_per_group;/* Number of inode table blocks per group */ unsigned long s_gdb_count;/* Number of group descriptor blocks */
@@ -67,6 +70,9 @@ struct ext3_sb_info {struct rb_root s_rsv_window_root;struct ext3_reserve_window_node s_rsv_window_head; +/* array of per-bg in-memory info */ +struct ext3_bg_info *s_bginfo; +/* Journaling */ struct inode * s_journal_inode;struct journal_s * s_journal;
@@ -83,4 +89,11 @@ struct ext3_sb_info { #endif }; +/* +* in-memory data associated with each block group. +*/ +struct ext3_bg_info { +int bgi_free_nonmc_blocks_count;/* Number of free non-metacluster blocks in group */ +}; +#endif /* _LINUX_EXT3_FS_SB */
include/linux/jbd.h +1 -0
@@ -295,6 +295,7 @@ enum jbd_state_bits { BH_State,/* Pins most journal_head state */ BH_JournalHead,/* Pins bh->b_private and jh->b_bh */ BH_Unshadow,/* Dummy bit, for BJ_Shadow wakeup filtering */ + BH_JBD_Sentinel,/* Start bit for clients of jbd */ };BUFFER_FNS (JBD , jbd)
Quoted 8 lines · On Wed, 23 Jan 2008 04:12:16 -0500 Abhishek Rai <[email protected]> wrote:
Quoted 4 lines
I'm wondering about the interaction between this code and the
buffer_boundary()logic. I guess we should disable thebuffer_boundary()handling when this code is in effect. Have you reviewed and tested that aspect?Thanks for pointing this out, I had totally missed this issue in my change. I've now made the call to
set_buffer_boundary()inext3_get_blocks_handle()subject to metacluster option being set.
Did it make any performance difference? iirc the buffer_boundary stuff was worth around 10% on a single linear read of a large, well-laid-out file.
No, it didn't. I measured read from a 10GB sequentially laid out file with standard benchmarking practices (cold cache, multiple runs, low std. deviation in results, etc.) and here are the results:
File created by vanilla Ext3 being read by vanilla Ext3:
Total: 3m16.1s
User: 0.0.5s
Sys: 13.9
File created by mc Ext3 being read by mc Ext3 (with the buffer boundary logic disabled):
Total: 3m15.5s
User: 0.05s
Sys: 13.6s
Thanks,
Abhishek
Quoted 12 lines · Andrew Morton
Quoted 8 lines · On Wed, 23 Jan 2008 04:12:16 -0500 Abhishek Rai <[email protected]> wrote:
Quoted 4 lines
I'm wondering about the interaction between this code and the
buffer_boundary()logic. I guess we should disable thebuffer_boundary()handling when this code is in effect. Have you reviewed and tested that aspect?Thanks for pointing this out, I had totally missed this issue in my change. I've now made the call to
set_buffer_boundary()inext3_get_blocks_handle()subject to metacluster option being set.Did it make any performance difference? iirc the
buffer_boundarystuff was worth around 10% on a single linear read of a large, well-laid-out file.