Skip to contentSkip to conversation

ext4: block reservations to handle delalloc ENOSPC error

RFC New Mingming Cao 1 participant 1 message Feb 22, 2008 ext4 file system

lore
Share & tools
lore
#0 lore raw

RFC 1/1 ext4: block reservations to handle delalloc ENOSPC error

In delayed allocation, blocks to be allocated need to be reserved before user buffers being copied to memory, otherwise later at page writeout time we could hit ENOSPC error.

In this patch, blocks(data and metadata) are reserved at da_write_begin() time, the free blocks counter is updated by then, and the number of reserved blocks is store in inode structure in memory.

Then later, when new_blocks() is being called, if the caller is already reserved the blocks for allocation, the free blocks_counter is not being updated, otherwise, in DIO case, which could call get_blocks() without the blocks being reserved, proper accounting still need to be done.

At the writepage() time, the unused reserved blocks are returned back.

---
 fs/ext4/balloc.c                |   25 +++++++--
 fs/ext4/dir.c                   |    3 -
 fs/ext4/extents.c               |   78 +++++++++++++++++++++++-------
 fs/ext4/inode.c                 |  102 +++++++++++++++++++++++++++++++++-------
 fs/ext4/mballoc.c               |   11 +++-
 fs/ext4/migrate.c               |    2 
 fs/ext4/super.c                 |    3 +
 fs/ext4/xattr.c                 |    2 
 include/linux/ext4_fs.h         |   17 ++++--
 include/linux/ext4_fs_extents.h |    4 -
 include/linux/ext4_fs_i.h       |    4 +
 11 files changed, 195 insertions(+), 56 deletions(-)
fs/ext4/super.c +3 -0
Index: linux-2.6.25-rc2/fs/ext4/super.c
===================================================================
@@ -573,6 +573,8 @@ static struct inode *ext4_alloc_inode(st
 	memset(&ei->i_cached_extent, 0, sizeof(struct ext4_ext_cache));
 	INIT_LIST_HEAD(&ei->i_prealloc_list);
 	spin_lock_init(&ei->i_prealloc_lock);
+	ei->i_reserved_data_blocks = 0;
+	ei->i_reserved_meta_blocks = 0;
 	return &ei->vfs_inode;
 }
 
@@ -2190,6 +2192,7 @@ static int ext4_fill_super (struct super
 		err = percpu_counter_init(&sbi->s_dirs_counter,
 				ext4_count_dirs(sb));
 	}
+
 	if (err) {
 		printk(KERN_ERR "EXT4-fs: insufficient memory\n");
 		goto failed_mount3;
fs/ext4/inode.c +85 -17

214 lines not on this page · raw message

include/linux/ext4_fs_i.h +4 -0
Index: linux-2.6.25-rc2/include/linux/ext4_fs_i.h
===================================================================
@@ -100,6 +100,10 @@ struct ext4_inode_info {
 	/* block reservation info */
 	struct ext4_block_alloc_info *i_block_alloc_info;
 
+	/* allocation reservation info for delalloc */
+	unsigned long i_reserved_data_blocks;
+	unsigned long i_reserved_meta_blocks;
+
 	ext4_lblk_t		i_dir_start_lookup;
 #ifdef CONFIG_EXT4DEV_FS_XATTR
 	/*
fs/ext4/extents.c +59 -19

196 lines not on this page · raw message

include/linux/ext4_fs_extents.h +2 -2
Index: linux-2.6.25-rc2/include/linux/ext4_fs_extents.h
===================================================================
@@ -211,7 +211,7 @@ static inline int ext4_ext_get_actual_le
 		le16_to_cpu(ext->ee_len) :
 		(le16_to_cpu(ext->ee_len) - EXT_INIT_MAX_LEN));
 }
-
+extern int ext4_ext_calc_metadata_amount(struct inode *inode, int blocks);
 extern ext4_fsblk_t idx_pblock(struct ext4_extent_idx *);
 extern void ext4_ext_store_pblock(struct ext4_extent *, ext4_fsblk_t);
 extern int ext4_extent_tree_init(handle_t *, struct inode *);
@@ -220,7 +220,7 @@ extern int ext4_ext_try_to_merge(struct 
 				 struct ext4_ext_path *path,
 				 struct ext4_extent *);
 extern unsigned int ext4_ext_check_overlap(struct inode *, struct ext4_extent *, struct ext4_ext_path *);
-extern int ext4_ext_insert_extent(handle_t *, struct inode *, struct ext4_ext_path *, struct ext4_extent *);
+extern int ext4_ext_insert_extent(handle_t *, struct inode *, struct ext4_ext_path *, struct ext4_extent *, int);
 extern struct ext4_ext_path *ext4_ext_find_extent(struct inode *, ext4_lblk_t,
 							struct ext4_ext_path *);
 extern int ext4_ext_search_left(struct inode *, struct ext4_ext_path *,
fs/ext4/balloc.c +18 -7
Index: linux-2.6.25-rc2/fs/ext4/balloc.c
===================================================================
@@ -1609,7 +1609,8 @@ int ext4_should_retry_alloc(struct super
  *
  */
 ext4_fsblk_t ext4_new_blocks_old(handle_t *handle, struct inode *inode,
-			ext4_fsblk_t goal, unsigned long *count, int *errp)
+			ext4_fsblk_t goal, unsigned long *count, int reserved,
+			int *errp)
 {
 	struct buffer_head *bitmap_bh = NULL;
 	struct buffer_head *gdp_bh;
@@ -1662,7 +1663,11 @@ ext4_fsblk_t ext4_new_blocks_old(handle_
 	if (block_i && ((windowsz = block_i->rsv_window_node.rsv_goal_size) > 0))
 		my_rsv = &block_i->rsv_window_node;
 
-	if (!ext4_has_free_blocks(sbi)) {
+	if (reserved)
+		BUG_ON(EXT4_I(inode)->i_reserved_data_blocks +
+			EXT4_I(inode)->i_reserved_meta_blocks < num);
+
+	if (!reserved && !ext4_has_free_blocks(sbi)) {
 		*errp = -ENOSPC;
 		goto out;
 	}
@@ -1833,7 +1838,8 @@ allocated:
 	le16_add_cpu(&gdp->bg_free_blocks_count, -num);
 	gdp->bg_checksum = ext4_group_desc_csum(sbi, group_no, gdp);
 	spin_unlock(sb_bgl_lock(sbi, group_no));
-	percpu_counter_sub(&sbi->s_freeblocks_counter, num);
+	if (!reserved)
+		percpu_counter_sub(&sbi->s_freeblocks_counter, num);
 
 	if (sbi->s_log_groups_per_flex) {
 		ext4_group_t flex_group = ext4_flex_group(sbi, group_no);
@@ -1874,14 +1880,15 @@ out:
 }
 
 ext4_fsblk_t ext4_new_block(handle_t *handle, struct inode *inode,
-		ext4_fsblk_t goal, int *errp)
+		ext4_fsblk_t goal, int reserved, int *errp)
 {
 	struct ext4_allocation_request ar;
 	ext4_fsblk_t ret;
 
 	if (!test_opt(inode->i_sb, MBALLOC)) {
 		unsigned long count = 1;
-		ret = ext4_new_blocks_old(handle, inode, goal, &count, errp);
+		ret = ext4_new_blocks_old(handle, inode, goal, &count,
+			reserved, errp);
 		return ret;
 	}
 
@@ -1889,18 +1896,21 @@ ext4_fsblk_t ext4_new_block(handle_t *ha
 	ar.inode = inode;
 	ar.goal = goal;
 	ar.len = 1;
+	ar.flags |= EXT4_MB_DELALLOC_RESERVED;
 	ret = ext4_mb_new_blocks(handle, &ar, errp);
 	return ret;
 }
 
 ext4_fsblk_t ext4_new_blocks(handle_t *handle, struct inode *inode,
-		ext4_fsblk_t goal, unsigned long *count, int *errp)
+		ext4_fsblk_t goal, unsigned long *count, int reserved,
+		int *errp)
 {
 	struct ext4_allocation_request ar;
 	ext4_fsblk_t ret;
 
 	if (!test_opt(inode->i_sb, MBALLOC)) {
-		ret = ext4_new_blocks_old(handle, inode, goal, count, errp);
+		ret = ext4_new_blocks_old(handle, inode, goal, count,
+						reserved, errp);
 		return ret;
 	}
 
@@ -1908,6 +1918,7 @@ ext4_fsblk_t ext4_new_blocks(handle_t *h
 	ar.inode = inode;
 	ar.goal = goal;
 	ar.len = *count;
+	ar.flags |= EXT4_MB_DELALLOC_RESERVED;
 	ret = ext4_mb_new_blocks(handle, &ar, errp);
 	*count = ar.len;
 	return ret;
fs/ext4/mballoc.c +9 -2
Index: linux-2.6.25-rc2/fs/ext4/mballoc.c
===================================================================
@@ -3106,7 +3106,9 @@ ext4_mb_mark_diskspace_used(struct ext4_
 	le16_add_cpu(&gdp->bg_free_blocks_count, -ac->ac_b_ex.fe_len);
 	gdp->bg_checksum = ext4_group_desc_csum(sbi, ac->ac_b_ex.fe_group, gdp);
 	spin_unlock(sb_bgl_lock(sbi, ac->ac_b_ex.fe_group));
-	percpu_counter_sub(&sbi->s_freeblocks_counter, ac->ac_b_ex.fe_len);
+	if (!ac->ac_flags & EXT4_MB_DELALLOC_RESERVED)
+		percpu_counter_sub(&sbi->s_freeblocks_counter,
+						ac->ac_b_ex.fe_len);
 
 	err = ext4_journal_dirty_metadata(handle, bitmap_bh);
 	if (err)
@@ -3328,6 +3330,9 @@ ext4_mb_normalize_request(struct ext4_al
 		ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
 	}
 
+	if (ar->flags & EXT4_MB_DELALLOC_RESERVED)
+		ac->ac_flags |= EXT4_MB_DELALLOC_RESERVED;
+
 	mb_debug("goal: %u(was %u) blocks at %u\n", (unsigned) size,
 		(unsigned) orig_size, (unsigned) start);
 }
@@ -4292,13 +4297,15 @@ ext4_fsblk_t ext4_mb_new_blocks(handle_t
 	ext4_fsblk_t block = 0;
 	int freed;
 	int inquota;
+	int res_flag = 0;
 
 	sb = ar->inode->i_sb;
 	sbi = EXT4_SB(sb);
 
+	res_flag = ar->flags & EXT4_MB_HINT_NOPREALLOC;
 	if (!test_opt(sb, MBALLOC)) {
 		block = ext4_new_blocks_old(handle, ar->inode, ar->goal,
-					    &(ar->len), errp);
+					    &(ar->len), res_flag, errp);
 		return block;
 	}
 
include/linux/ext4_fs.h +11 -6
Index: linux-2.6.25-rc2/include/linux/ext4_fs.h
===================================================================
@@ -75,6 +75,8 @@
 #define EXT4_MB_HINT_GOAL_ONLY		256
 /* goal is meaningful */
 #define EXT4_MB_HINT_TRY_GOAL		512
+/* blocks already pre-reserved by delayed allocation */
+#define EXT4_MB_DELALLOC_RESERVED	1024
 
 struct ext4_allocation_request {
 	/* target inode for block we're allocating */
@@ -973,11 +975,13 @@ extern int ext4_bg_has_super(struct supe
 extern unsigned long ext4_bg_num_gdb(struct super_block *sb,
 			ext4_group_t group);
 extern ext4_fsblk_t ext4_new_block (handle_t *handle, struct inode *inode,
-			ext4_fsblk_t goal, int *errp);
+			ext4_fsblk_t goal, int reserved, int *errp);
 extern ext4_fsblk_t ext4_new_blocks (handle_t *handle, struct inode *inode,
-			ext4_fsblk_t goal, unsigned long *count, int *errp);
+			ext4_fsblk_t goal, unsigned long *count,
+			int reserved, int *errp);
 extern ext4_fsblk_t ext4_new_blocks_old(handle_t *handle, struct inode *inode,
-			ext4_fsblk_t goal, unsigned long *count, int *errp);
+			ext4_fsblk_t goal, unsigned long *count,
+			int reserved, int *errp);
 extern void ext4_free_blocks (handle_t *handle, struct inode *inode,
 			ext4_fsblk_t block, unsigned long count, int metadata);
 extern void ext4_free_blocks_sb (handle_t *handle, struct super_block *sb,
@@ -1042,7 +1046,8 @@ struct buffer_head *ext4_bread(handle_t 
 int ext4_get_blocks_handle(handle_t *handle, struct inode *inode,
 				ext4_lblk_t iblock, unsigned long maxblocks,
 				struct buffer_head *bh_result,
-				int create, int extend_disksize);
+				int create, int extend_disksize,
+				int reserved);
 
 extern struct inode *ext4_iget(struct super_block *, unsigned long);
 extern int  ext4_write_inode (struct inode *, int);
@@ -1216,7 +1221,7 @@ extern int ext4_ext_writepage_trans_bloc
 extern int ext4_ext_get_blocks(handle_t *handle, struct inode *inode,
 			ext4_lblk_t iblock,
 			unsigned long max_blocks, struct buffer_head *bh_result,
-			int create, int extend_disksize);
+			int create, int extend_disksize, int reserved);
 extern void ext4_ext_truncate(struct inode *, struct page *);
 extern void ext4_ext_init(struct super_block *);
 extern void ext4_ext_release(struct super_block *);
@@ -1225,7 +1230,7 @@ extern long ext4_fallocate(struct inode 
 extern int ext4_get_blocks_wrap(handle_t *handle, struct inode *inode,
 			sector_t block, unsigned long max_blocks,
 			struct buffer_head *bh, int create,
-			int extend_disksize);
+			int extend_disksize, int reserved);
 #endif	/* __KERNEL__ */
 
 #endif	/* _LINUX_EXT4_FS_H */
fs/ext4/dir.c +2 -1
Index: linux-2.6.25-rc2/fs/ext4/dir.c
===================================================================
@@ -129,7 +129,8 @@ static int ext4_readdir(struct file * fi
 		struct buffer_head *bh = NULL;
 
 		map_bh.b_state = 0;
-		err = ext4_get_blocks_wrap(NULL, inode, blk, 1, &map_bh, 0, 0);
+		err = ext4_get_blocks_wrap(NULL, inode, blk, 1, &map_bh,
+					0, 0, 0);
 		if (err > 0) {
 			pgoff_t index = map_bh.b_blocknr >>
 					(PAGE_CACHE_SHIFT - inode->i_blkbits);
fs/ext4/migrate.c +1 -1
Index: linux-2.6.25-rc2/fs/ext4/migrate.c
===================================================================
@@ -73,7 +73,7 @@ static int finish_range(handle_t *handle
 				goto err_out;
 		}
 	}
-	retval = ext4_ext_insert_extent(handle, inode, path, &newext);
+	retval = ext4_ext_insert_extent(handle, inode, path, &newext, 0);
 err_out:
 	if (path) {
 		ext4_ext_drop_refs(path);
fs/ext4/xattr.c +1 -1
Index: linux-2.6.25-rc2/fs/ext4/xattr.c
===================================================================
@@ -809,7 +809,7 @@ inserted:
 			ext4_fsblk_t goal = ext4_group_first_block_no(sb,
 						EXT4_I(inode)->i_block_group);
 			ext4_fsblk_t block = ext4_new_block(handle, inode,
-							goal, &error);
+							goal, 0, &error);
 			if (error)
 				goto cleanup;
 			ea_idebug(inode, "creating block %d", block);