Skip to contentSkip to conversation

ext4: Add new abstraction ext4_map_blocks() underneath ext4_get_blocks()

RFC PATCH In review Theodore Ts'o 2 participants 5 messages May 3, 2010 – May 5, 2010 ext4 file system

lore
Share & tools
lore
author maintainer
#1 lore raw

RFC PATCH 2/2 ext4: Convert callers of ext4_get_blocks() to use ext4_map_blocks()

In reply to Theodore Ts'o

This saves a huge amount of stack space by avoiding unnecesary struct buffer_head's from being allocated on the stack.

In addition, to make the code easier to understand, collapse and refactor ext4_get_block(), ext4_get_block_write(), noalloc_get_block_write(), into a single function.

---
 fs/ext4/dir.c     |   12 +-
 fs/ext4/ext4.h    |    2 +-
 fs/ext4/extents.c |   40 +++----
 fs/ext4/inode.c   |  342 +++++++++++++++++++++--------------------------------
 4 files changed, 158 insertions(+), 238 deletions(-)
fs/ext4/dir.c +6 -6
index 86cb6d8..e3f2700 100644
@@ -128,14 +128,14 @@ static int ext4_readdir(struct file *filp,
 	offset = filp->f_pos & (sb->s_blocksize - 1);
 
 	while (!error && !stored && filp->f_pos < inode->i_size) {
-		ext4_lblk_t blk = filp->f_pos >> EXT4_BLOCK_SIZE_BITS(sb);
-		struct buffer_head map_bh;
+		struct ext4_map_blocks map;
 		struct buffer_head *bh = NULL;
 
-		map_bh.b_state = 0;
-		err = ext4_get_blocks(NULL, inode, blk, 1, &map_bh, 0);
+		map.m_lblk = filp->f_pos >> EXT4_BLOCK_SIZE_BITS(sb);
+		map.m_len = 1;
+		err = ext4_map_blocks(NULL, inode, &map, 0);
 		if (err > 0) {
-			pgoff_t index = map_bh.b_blocknr >>
+			pgoff_t index = map.m_pblk >>
 					(PAGE_CACHE_SHIFT - inode->i_blkbits);
 			if (!ra_has_index(&filp->f_ra, index))
 				page_cache_sync_readahead(
@@ -143,7 +143,7 @@ static int ext4_readdir(struct file *filp,
 					&filp->f_ra, filp,
 					index, 1);
 			filp->f_ra.prev_pos = (loff_t)index << PAGE_CACHE_SHIFT;
-			bh = ext4_bread(NULL, inode, blk, 0, &err);
+			bh = ext4_bread(NULL, inode, map.m_lblk, 0, &err);
 		}
 
 		/*
fs/ext4/ext4.h +1 -1
index 8dfc70b..08f541b 100644
@@ -139,7 +139,7 @@ struct ext4_allocation_request {
 #define EXT4_MAP_UNINIT		(1 << BH_Uninit)
 #define EXT4_MAP_FLAGS		(EXT4_MAP_NEW | EXT4_MAP_MAPPED |\
 				 EXT4_MAP_UNWRITTEN | EXT4_MAP_BOUNDARY |\
-				 EXT4_MAP_UNINIT);
+				 EXT4_MAP_UNINIT)
 
 struct ext4_map_blocks {
 	ext4_fsblk_t m_pblk;
fs/ext4/extents.c +17 -23
index 005dce9..2282f40 100644
@@ -3667,13 +3667,12 @@ static void ext4_falloc_update_inode(struct inode *inode,
 long ext4_fallocate(struct inode *inode, int mode, loff_t offset, loff_t len)
 {
 	handle_t *handle;
-	ext4_lblk_t block;
 	loff_t new_size;
 	unsigned int max_blocks;
 	int ret = 0;
 	int ret2 = 0;
 	int retries = 0;
-	struct buffer_head map_bh;
+	struct ext4_map_blocks map;
 	unsigned int credits, blkbits = inode->i_blkbits;
 
 	/*
@@ -3687,13 +3686,13 @@ long ext4_fallocate(struct inode *inode, int mode, loff_t offset, loff_t len)
 	if (S_ISDIR(inode->i_mode))
 		return -ENODEV;
 
-	block = offset >> blkbits;
+	map.m_lblk = offset >> blkbits;
 	/*
 	 * We can't just convert len to max_blocks because
 	 * If blocksize = 4096 offset = 3072 and len = 2048
 	 */
 	max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits)
-							- block;
+		- map.m_lblk;
 	/*
 	 * credits to insert 1 extent into extent tree
 	 */
@@ -3701,16 +3700,14 @@ long ext4_fallocate(struct inode *inode, int mode, loff_t offset, loff_t len)
 	mutex_lock(&inode->i_mutex);
 retry:
 	while (ret >= 0 && ret < max_blocks) {
-		block = block + ret;
-		max_blocks = max_blocks - ret;
+		map.m_lblk = map.m_lblk + ret;
+		map.m_len = max_blocks = max_blocks - ret;
 		handle = ext4_journal_start(inode, credits);
 		if (IS_ERR(handle)) {
 			ret = PTR_ERR(handle);
 			break;
 		}
-		map_bh.b_state = 0;
-		ret = ext4_get_blocks(handle, inode, block,
-				      max_blocks, &map_bh,
+		ret = ext4_map_blocks(handle, inode, &map,
 				      EXT4_GET_BLOCKS_CREATE_UNINIT_EXT);
 		if (ret <= 0) {
 #ifdef EXT4FS_DEBUG
@@ -3724,14 +3721,14 @@ retry:
 			ret2 = ext4_journal_stop(handle);
 			break;
 		}
-		if ((block + ret) >= (EXT4_BLOCK_ALIGN(offset + len,
+		if ((map.m_lblk + ret) >= (EXT4_BLOCK_ALIGN(offset + len,
 						blkbits) >> blkbits))
 			new_size = offset + len;
 		else
-			new_size = (block + ret) << blkbits;
+			new_size = (map.m_lblk + ret) << blkbits;
 
 		ext4_falloc_update_inode(inode, mode, new_size,
-						buffer_new(&map_bh));
+					 (map.m_flags & EXT4_MAP_NEW));
 		ext4_mark_inode_dirty(handle, inode);
 		ret2 = ext4_journal_stop(handle);
 		if (ret2)
@@ -3760,42 +3757,39 @@ int ext4_convert_unwritten_extents(struct inode *inode, loff_t offset,
 				    ssize_t len)
 {
 	handle_t *handle;
-	ext4_lblk_t block;
 	unsigned int max_blocks;
 	int ret = 0;
 	int ret2 = 0;
-	struct buffer_head map_bh;
+	struct ext4_map_blocks map;
 	unsigned int credits, blkbits = inode->i_blkbits;
 
-	block = offset >> blkbits;
+	map.m_lblk = offset >> blkbits;
 	/*
 	 * We can't just convert len to max_blocks because
 	 * If blocksize = 4096 offset = 3072 and len = 2048
 	 */
-	max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits)
-							- block;
+	max_blocks = ((EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits) -
+		      map.m_lblk);
 	/*
 	 * credits to insert 1 extent into extent tree
 	 */
 	credits = ext4_chunk_trans_blocks(inode, max_blocks);
 	while (ret >= 0 && ret < max_blocks) {
-		block = block + ret;
-		max_blocks = max_blocks - ret;
+		map.m_lblk += ret;
+		map.m_len = (max_blocks -= ret);
 		handle = ext4_journal_start(inode, credits);
 		if (IS_ERR(handle)) {
 			ret = PTR_ERR(handle);
 			break;
 		}
-		map_bh.b_state = 0;
-		ret = ext4_get_blocks(handle, inode, block,
-				      max_blocks, &map_bh,
+		ret = ext4_map_blocks(handle, inode, &map,
 				      EXT4_GET_BLOCKS_IO_CONVERT_EXT);
 		if (ret <= 0) {
 			WARN_ON(ret <= 0);
 			printk(KERN_ERR "%s: ext4_ext_map_blocks "
 				    "returned error inode#%lu, block=%u, "
 				    "max_blocks=%u", __func__,
-				    inode->i_ino, block, max_blocks);
+				    inode->i_ino, map.m_lblk, map.m_len);
 		}
 		ext4_mark_inode_dirty(handle, inode);
 		ret2 = ext4_journal_stop(handle);
fs/ext4/inode.c +131 -196

482 lines not on this page · raw message

Theodore Ts'o

author maintainer
#0 lore raw

RFC PATCH 1/2 ext4: Add new abstraction ext4_map_blocks() underneath ext4_get_blocks()

Jack up ext4_get_blocks() and add a new function, ext4_map_blocks() which uses a much smaller structure, struct ext4_map_blocks which is 20 bytes, as opposed to a struct buffer_head, which nearly 5 times bigger on an x86_64 machine. By switching things to use ext4_map_blocks(), we can save stack space by using ext4_map_blocks() since we can avoid allocating a struct buffer_head on the stack.

---
 fs/ext4/ext4.h    |   30 ++++++-
 fs/ext4/extents.c |  233 +++++++++++++++++++++++++---------------------------
 fs/ext4/inode.c   |  102 +++++++++++++----------
 3 files changed, 197 insertions(+), 168 deletions(-)
fs/ext4/ext4.h +27 -3
index bf938cf..8dfc70b 100644
@@ -126,6 +126,29 @@ struct ext4_allocation_request {
 };
 
 /*
+ * Logical to physical block mapping, used by ext4_map_blocks()
+ *
+ * This structure is used to pass requests into ext4_map_blocks() as
+ * well as to store the information returned by ext4_map_blocks().  It
+ * takes less room on the stack than a struct buffer_head.
+ */
+#define EXT4_MAP_NEW		(1 << BH_New)
+#define EXT4_MAP_MAPPED		(1 << BH_Mapped)
+#define EXT4_MAP_UNWRITTEN	(1 << BH_Unwritten)
+#define EXT4_MAP_BOUNDARY	(1 << BH_Boundary)
+#define EXT4_MAP_UNINIT		(1 << BH_Uninit)
+#define EXT4_MAP_FLAGS		(EXT4_MAP_NEW | EXT4_MAP_MAPPED |\
+				 EXT4_MAP_UNWRITTEN | EXT4_MAP_BOUNDARY |\
+				 EXT4_MAP_UNINIT);
+
+struct ext4_map_blocks {
+	ext4_fsblk_t m_pblk;
+	ext4_lblk_t m_lblk;
+	unsigned int m_len;
+	unsigned int m_flags;
+};
+
+/*
  * For delayed allocation tracking
  */
 struct mpage_da_data {
@@ -1772,9 +1795,8 @@ extern int ext4_ext_tree_init(handle_t *handle, struct inode *);
 extern int ext4_ext_writepage_trans_blocks(struct inode *, int);
 extern int ext4_ext_index_trans_blocks(struct inode *inode, int nrblocks,
 				       int chunk);
-extern int ext4_ext_get_blocks(handle_t *handle, struct inode *inode,
-			       ext4_lblk_t iblock, unsigned int max_blocks,
-			       struct buffer_head *bh_result, int flags);
+extern int ext4_ext_map_blocks(handle_t *handle, struct inode *inode,
+			       struct ext4_map_blocks *map, int flags);
 extern void ext4_ext_truncate(struct inode *);
 extern void ext4_ext_init(struct super_block *);
 extern void ext4_ext_release(struct super_block *);
@@ -1782,6 +1804,8 @@ extern long ext4_fallocate(struct inode *inode, int mode, loff_t offset,
 			  loff_t len);
 extern int ext4_convert_unwritten_extents(struct inode *inode, loff_t offset,
 			  ssize_t len);
+extern int ext4_map_blocks(handle_t *handle, struct inode *inode,
+			   struct ext4_map_blocks *map, int flags);
 extern int ext4_get_blocks(handle_t *handle, struct inode *inode,
 			   sector_t block, unsigned int max_blocks,
 			   struct buffer_head *bh, int flags);
fs/ext4/extents.c +112 -121

598 lines not on this page · raw message

fs/ext4/inode.c +58 -44

236 lines not on this page · raw message

Signature · 1 line
1.6.6.1.1.g974db.dirty
#2 lore raw

Re: [PATCH, RFC 2/2] ext4: Convert callers of ext4_get_blocks() to use ext4_map_blocks()

In reply to Theodore Ts'o
Quoted 34 lines · Theodore Ts'o
fs/ext4/inode.c +4 -20
@@ -2560,45 +2522,50 @@ static int ext4_da_get_block_prep(struct inode *inode, sector_t iblock,
 		invalid_block = ~0;
 
 	BUG_ON(create == 0);
-	BUG_ON(bh_result->b_size != inode->i_sb->s_blocksize);
+	BUG_ON(bh->b_size != inode->i_sb->s_blocksize);
+
+	map.m_lblk = iblock;
+	map.m_len = 1;
 
 	/*
 	 * first, we need to know whether the block is allocated already
 	 * preallocated blocks are unmapped but should treated
 	 * the same as allocated blocks.
 	 */
-	ret = ext4_get_blocks(NULL, inode, iblock, 1,  bh_result, 0);
-	if ((ret == 0) && !buffer_delay(bh_result)) {
-		/* the block isn't (pre)allocated yet, let's reserve space */
-		/*
-		 * XXX: __block_prepare_write() unmaps passed block,
-		 * is it OK?
-		 */
-		ret = ext4_da_reserve_space(inode, iblock);
-		if (ret)
-			/* not enough space to reserve */
-			return ret;
-
-		map_bh(bh_result, inode->i_sb, invalid_block);
-		set_buffer_new(bh_result);
-		set_buffer_delay(bh_result);
-	} else if (ret > 0) {
-		bh_result->b_size = (ret << inode->i_blkbits);
-		if (buffer_unwritten(bh_result)) {
-			/* A delayed write to unwritten bh should

fs/ext4/inode.c static int ext4_da_get_block_prep(struct inode *inode, sector_t iblock, · line 2539

Quoted 10 lines
fs/ext4/inode.c +5 -5
-			 * be marked new and mapped.  Mapped ensures
-			 * that we don't do get_block multiple times
-			 * when we write to the same offset and new
-			 * ensures that we do proper zero out for
-			 * partial write.
+	ret = ext4_map_blocks(NULL, inode, &map, 0);
+	if (ret < 0)
+		return ret;
+	if (ret == 0) {
+		if (!buffer_delay(bh)) {

bh flags are not set here. This check should be based on map.m_flags.

Quoted 8 lines
fs/ext4/inode.c +5 -2
+			/*
+			 * XXX: __block_prepare_write() unmaps passed
+			 * block, is it OK?
 			 */
-			set_buffer_new(bh_result);
-			set_buffer_mapped(bh_result);
+			ret = ext4_da_reserve_space(inode, iblock);
+			if (ret)

fs/ext4/inode.c static int ext4_da_get_block_prep(struct inode *inode, sector_t iblock, · line 2554

Quoted 10 lines
fs/ext4/inode.c +6 -1
+				return ret; /* not enough space to reserve */
+
+			map_bh(bh, inode->i_sb, invalid_block);
+			set_buffer_new(bh);
+			set_buffer_delay(bh);
 		}
-		ret = 0;
+		return 0;
 	}
 

Only thing i am worried about is we were modifying bh_flags in all possible confusing ways. We may want to make sure we get the update correct. I am still going through the patch after applying it to the tree. So may take more time to look at the full changeset.

-aneesh

author maintainer
#3 lore raw

Re: [PATCH, RFC 2/2] ext4: Convert callers of ext4_get_blocks() to use ext4_map_blocks()

Quoted 2 lines · Aneesh Kumar K. V

bh flags are not set here. This check should be based on map.m_flags.

Good catch, thanks.

Quoted 4 lines

Only thing i am worried about is we were modifying bh_flags in all possible confusing ways. We may want to make sure we get the update correct. I am still going through the patch after applying it to the tree. So may take more time to look at the full changeset.

I'm concerned about that as well; in fact I'm not sure what we had before was completely correct, and I *still* have trouble reasoning about how all the flags work and why we do some of the things that we do based on how the flags are set --- and that scares me.

XFS doesn't jump through *nearly* as many hoops as we do --- and given XFS's reputation for complexity, that's saying a lot! --- and I suspect some of the things we do are mandated by the fs/buffer.c and fs/direct_io.c, the latter of which does some very strange and unnatural things with buffer_heads --- and some of the things we do are based on our own, ext4-specific logic and how we route state through the many layers of callback functions. I think I've figured some of this out, but it's gotten very crufty over the course of ext4's development.

One of the reasons I had for doing this cleanup, in addition to reducing stack usage (which I have measured as around 120 bytes or so on an 32-bit system, and I'm sure it's got to be more on a 64-bit system) was to make explicit how we were modifying the bh_flags. At least now we can grep for EXT4_MAP_* and see on the places where we were mucking with things.

- Ted

author maintainer
#4 lore raw

Re: [PATCH, RFC 2/2] ext4: Convert callers of ext4_get_blocks() to use ext4_map_blocks()

In reply to Aneesh Kumar K. V
Quoted 7 lines · Aneesh Kumar K. V

fs/ext4/inode.c static int ext4_da_get_block_prep(struct inode *inode, sector_t iblock, · line 2539

Quoted 5 lines
fs/ext4/inode.c +5 -0
+	ret = ext4_map_blocks(NULL, inode, &map, 0);
+	if (ret < 0)
+		return ret;
+	if (ret == 0) {
+		if (!buffer_delay(bh)) {

bh flags are not set here. This check should be based on map.m_flags.

Actually, I looked more closely at this again today. ext4_get_blocks() never messed with BH_DELAY, so this flag would only be set or not set if it was originally set in the buffer_head before it had been passed into ext4_get_blocks().

BTW, this is one of the reasons why I really hated the old system. It was never clear which flags were set by whom, and whether the flags that had been in the struct bh before it had been passed into ext4_get_blocks() was random stack garbage, or whether it had meaning, etc.

I'm still not sure we have it all right, but it's clearly better to use !buffer_delay(bh), since the buffer_delay flag is one that had been never touched by ext4_get_blocks().

- Ted