ext4: Use new buffer_head flag to check uninit group bitmaps initialization
PATCH 1/4 ext4: Use new buffer_head flag to check uninit group bitmaps initialization
For uninit block group, the ondisk bitmap is not initialized. That implies we cannot depend on the uptodate flag on the bitmap buffer_head to find bitmap validity. Use a new buffer_head flag which would be set after we properly initialize the bitmap. This also prevent the initializing the uninit group bitmap initialization every time we do a ext4_read_block_bitmap.
--- fs/ext4/balloc.c | 25 +++++++++++++++++++++++-- fs/ext4/ext4.h | 18 ++++++++++++++++++ fs/ext4/ialloc.c | 24 ++++++++++++++++++++++-- fs/ext4/mballoc.c | 24 ++++++++++++++++++++++-- 4 files changed, 85 insertions(+), 6 deletions(-)
fs/ext4/balloc.c +23 -2
@@ -320,20 +320,41 @@ ext4_read_block_bitmap(struct super_block *sb, ext4_group_t block_group) block_group, bitmap_blk);return NULL ; } -if (buffer_uptodate (bh) && - !(desc->bg_flags &cpu_to_le16 (EXT4_BG_BLOCK_UNINIT ))) + +if (bitmap_uptodate (bh))return bh;lock_buffer (bh); +if (bitmap_uptodate (bh)) { +unlock_buffer (bh); +return bh; + }spin_lock (sb_bgl_lock (EXT4_SB (sb), block_group));if (desc->bg_flags &cpu_to_le16 (EXT4_BG_BLOCK_UNINIT )) {ext4_init_block_bitmap (sb, bh, block_group, desc); +set_bitmap_uptodate (bh);set_buffer_uptodate (bh);spin_unlock (sb_bgl_lock (EXT4_SB (sb), block_group));unlock_buffer (bh);return bh; }spin_unlock (sb_bgl_lock (EXT4_SB (sb), block_group)); +if (buffer_uptodate (bh)) { +/* +* if not uninit if bh is uptodate, +* bitmap is also uptodate +*/ +set_bitmap_uptodate (bh); +unlock_buffer (bh); +return bh; + } +/* +* submit the buffer_head for read. We can +* safely mark the bitmap as uptodate now. +* We do it here so the bitmap uptodate bit +* get set with buffer lock held. +*/ +set_bitmap_uptodate (bh);if (bh_submit_read (bh) <0 ) {put_bh (bh); ext4_error(sb, __func__,
fs/ext4/ext4.h +18 -0
@@ -19,6 +19,7 @@#include <linux/types.h> #include <linux/blkdev.h> #include <linux/magic.h> +#include <linux/jbd2.h> #include "ext4_i.h" /*
@@ -1360,6 +1361,23 @@ extern int ext4_get_blocks_wrap(handle_t *handle, struct inode *inode,extern int ext4_fiemap (struct inode *inode,struct fiemap_extent_info *fieinfo,__u64 start,__u64 len); +/* +* Add new method to test wether block and inode bitmaps are properly +* initialized. With uninit_bg reading the block from disk is not enough +* to mark the bitmap uptodate. We need to also zero-out the bitmap +*/ +#define BH_BITMAP_UPTODATE BH_JBDPrivateStart + +static inline int bitmap_uptodate (struct buffer_head *bh) +{ +return (buffer_uptodate (bh) && +test_bit (BH_BITMAP_UPTODATE , &(bh)->b_state)); +} +static inline void set_bitmap_uptodate (struct buffer_head *bh) +{ +set_bit (BH_BITMAP_UPTODATE , &(bh)->b_state); +} +#endif /* __KERNEL__ */ #endif /* _EXT4_H */
fs/ext4/ialloc.c +22 -2
@@ -115,20 +115,40 @@ ext4_read_inode_bitmap(struct super_block *sb, ext4_group_t block_group) block_group, bitmap_blk);return NULL ; } -if (buffer_uptodate (bh) && - !(desc->bg_flags &cpu_to_le16 (EXT4_BG_INODE_UNINIT ))) +if (bitmap_uptodate (bh))return bh;lock_buffer (bh); +if (bitmap_uptodate (bh)) { +unlock_buffer (bh); +return bh; + }spin_lock (sb_bgl_lock (EXT4_SB (sb), block_group));if (desc->bg_flags &cpu_to_le16 (EXT4_BG_INODE_UNINIT )) {ext4_init_inode_bitmap (sb, bh, block_group, desc); +set_bitmap_uptodate (bh);set_buffer_uptodate (bh);spin_unlock (sb_bgl_lock (EXT4_SB (sb), block_group));unlock_buffer (bh);return bh; }spin_unlock (sb_bgl_lock (EXT4_SB (sb), block_group)); +if (buffer_uptodate (bh)) { +/* +* if not uninit if bh is uptodate, +* bitmap is also uptodate +*/ +set_bitmap_uptodate (bh); +unlock_buffer (bh); +return bh; + } +/* +* submit the buffer_head for read. We can +* safely mark the bitmap as uptodate now. +* We do it here so the bitmap uptodate bit +* get set with buffer lock held. +*/ +set_bitmap_uptodate (bh);if (bh_submit_read (bh) <0 ) {put_bh (bh); ext4_error(sb, __func__,
fs/ext4/mballoc.c +22 -2
@@ -794,22 +794,42 @@ static int ext4_mb_init_cache(struct page *page, char *incore)if (bh[i] ==NULL ) goto out; -if (buffer_uptodate (bh[i]) && - !(desc->bg_flags &cpu_to_le16 (EXT4_BG_BLOCK_UNINIT ))) +if (bitmap_uptodate (bh[i]))continue ;lock_buffer (bh[i]); +if (bitmap_uptodate (bh[i])) { +unlock_buffer (bh[i]); +continue ; + }spin_lock (sb_bgl_lock (EXT4_SB (sb), first_group + i));if (desc->bg_flags &cpu_to_le16 (EXT4_BG_BLOCK_UNINIT )) {ext4_init_block_bitmap (sb, bh[i], first_group + i, desc); +set_bitmap_uptodate (bh[i]);set_buffer_uptodate (bh[i]);spin_unlock (sb_bgl_lock (EXT4_SB (sb), first_group + i));unlock_buffer (bh[i]);continue ; }spin_unlock (sb_bgl_lock (EXT4_SB (sb), first_group + i)); +if (buffer_uptodate (bh[i])) { +/* +* if not uninit if bh is uptodate, +* bitmap is also uptodate +*/ +set_bitmap_uptodate (bh[i]); +unlock_buffer (bh[i]); +continue ; + }get_bh (bh[i]); +/* +* submit the buffer_head for read. We can +* safely mark the bitmap as uptodate now. +* We do it here so the bitmap uptodate bit +* get set with buffer lock held. +*/ +set_bitmap_uptodate (bh[i]); bh[i]->b_end_io = end_buffer_read_sync;submit_bh (READ , bh[i]);mb_debug ("read bitmap for group %u\n" , first_group + i);
Signature · 1 line
1.6.0.4.735.gea4f
PATCH 2/4 ext4: mark the blocks/inode bitmap beyond end of group as used.
We need to mark the block/inode bitmap beyond end of grou with '1'
--- fs/ext4/ialloc.c | 2 +- fs/ext4/mballoc.c | 4 ++-- fs/ext4/resize.c | 6 ++---- 3 files changed, 5 insertions(+), 7 deletions(-)
fs/ext4/ialloc.c +1 -1
@@ -84,7 +84,7 @@ unsigned ext4_init_inode_bitmap(struct super_block *sb, struct buffer_head *bh, }memset (bh->b_data,0 , (EXT4_INODES_PER_GROUP (sb) +7 ) /8 ); -mark_bitmap_end (EXT4_INODES_PER_GROUP (sb),EXT4_BLOCKS_PER_GROUP (sb), +mark_bitmap_end (EXT4_INODES_PER_GROUP (sb), sb->s_blocksize *8 , bh->b_data);return EXT4_INODES_PER_GROUP (sb);
fs/ext4/mballoc.c +2 -2
@@ -3044,8 +3044,8 @@ ext4_mb_mark_diskspace_used(struct ext4_allocation_context *ac, in_range(block + len - 1, ext4_inode_table(sb, gdp), EXT4_SB(sb)->s_itb_per_group)) {ext4_error (sb, __func__, -"Allocating block in system zone - block = %llu" , - block); +"Allocating block %llu in system zone of %d group\n" , + block, ac->ac_b_ex.fe_group);/* File system mounted not to panic on error * Fix the bitmap and repeat the block allocation * We leak some of the blocks here.
fs/ext4/resize.c +1 -2
@@ -284,11 +284,9 @@ static int setup_new_group_blocks(struct super_block *sb,if ((err =extend_or_restart_transaction (handle,2 , bh))) goto exit_bh; -mark_bitmap_end (input->blocks_count,EXT4_BLOCKS_PER_GROUP (sb), - bh->b_data); +mark_bitmap_end (input->blocks_count, sb->s_blocksize *8 , bh->b_data);ext4_journal_dirty_metadata (handle, bh);brelse (bh);
PATCH 3/4 ext4: Don't allow new groups to be added during block allocation
After we mark the blocks in the buddy cache as allocated, we should ensure that we don't reinit the buddy cache untill the block bitmap is updated. The patch achieve this by holding the group_info alloc_semaphore till ext4_mb_release_context
--- fs/ext4/mballoc.c | 10 ++++++++-- fs/ext4/mballoc.h | 5 +++++ 2 files changed, 13 insertions(+), 2 deletions(-)
fs/ext4/mballoc.c +8 -2
@@ -1055,7 +1055,8 @@ __releases(e4b->alloc_semp)if (e4b->bd_buddy_page)page_cache_release (e4b->bd_buddy_page);/* Done with the buddy cache */ -up_read (e4b->alloc_semp); +if (e4b->alloc_semp) +up_read (e4b->alloc_semp);__release (e4b->alloc_semp); }
@@ -1375,7 +1376,9 @@ static void ext4_mb_use_best_found(struct ext4_allocation_context *ac,get_page (ac->ac_bitmap_page); ac->ac_buddy_page = e4b->bd_buddy_page;get_page (ac->ac_buddy_page); - +/* on allocation we use ac to track the held semaphore */ + ac->alloc_semp = e4b->alloc_semp; + e4b->alloc_semp =NULL ;/* store last allocated for subsequent stream allocation */ if ((ac->ac_flags &EXT4_MB_HINT_DATA )) {spin_lock (&sbi->s_md_lock);
@@ -4297,6 +4300,7 @@ ext4_mb_initialize_context(struct ext4_allocation_context *ac, ac->ac_pa =NULL ; ac->ac_bitmap_page =NULL ; ac->ac_buddy_page =NULL ; + ac->alloc_semp =NULL ; ac->ac_lg =NULL ;/* we have to define context: we'll we work with a file or
@@ -4478,6 +4482,8 @@ static int ext4_mb_release_context(struct ext4_allocation_context *ac) }ext4_mb_put_pa (ac, ac->ac_sb, pa); } +if (ac->alloc_semp) +up_read (ac->alloc_semp);if (ac->ac_bitmap_page)page_cache_release (ac->ac_bitmap_page);if (ac->ac_buddy_page)
fs/ext4/mballoc.h +5 -0
@@ -195,6 +195,11 @@ struct ext4_allocation_context {__u8 ac_op;/* operation, for history only */ struct page *ac_bitmap_page;struct page *ac_buddy_page; +/* +* pointer to the held semaphore upon successfull +* block allocation +*/ +struct rw_semaphore *alloc_semp;struct ext4_prealloc_space *ac_pa;struct ext4_locality_group *ac_lg; };
Signature · 1 line
1.6.0.4.735.gea4f
PATCH 4/4 ext4: Init the complete page while building buddy cache
We need to init the complete page during buddy cache init by setting the contents to '1'. This ensures that during resize when we increment the s_groups_count we don't hit the erros as below.
EXT4-fs error (device sdb1): ext4_mb_mark_diskspace_used:
Allocating block 1040385 in system zone of 127 group
--- fs/ext4/mballoc.c | 2 ++ 1 files changed, 2 insertions(+), 0 deletions(-)
fs/ext4/mballoc.c +2 -0
@@ -846,6 +846,8 @@ static int ext4_mb_init_cache(struct page *page, char *incore) err =0 ; first_block = page->index * blocks_per_page; +/* init the page */ +memset (page_address (page),0xff ,PAGE_CACHE_SIZE );for (i =0 ; i < blocks_per_page; i++) {int group;struct ext4_group_info *grinfo;
Signature · 1 line
1.6.0.4.735.gea4f
Hi,
All the 4 patches get applied after akpm-jbd2-locking-fix in the patch queue.
-aneesh