Skip to contentSkip to conversation

udf: extent cache implementation for manipulating block map

PATCH In review Namjae Jeon 2 participants 5 messages Aug 31, 2012 – Sep 4, 2012 UDF filesystem

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 udf: extent cache implementation for manipulating block map

From: Namjae Jeon <[email protected]>

While mapping logical blocks of a file to physical blocks on the partition, everytime UDF read file metadata from the begining which decrease preformance. The drawback of this scheme is more prominent while reading large files. For example, while reading a large file of ~5GB, read speed will gradually become less as we near the end of file because of the time taken in calculating the corresponding physical block.

This patch implements caching and remembers the location of the last read extent. Instead of reading file metadata from begining, start from the cached location.

---
 fs/udf/ialloc.c  |    2 ++
 fs/udf/inode.c   |   77 +++++++++++++++++++++++++++++++++++++++++++++++++-----
 fs/udf/udf_i.h   |   17 ++++++++++++
 fs/udf/udfdecl.h |   10 +++----
 4 files changed, 94 insertions(+), 12 deletions(-)
fs/udf/ialloc.c +2 -0
index 7e5aae4..7dd86a4 100644
@@ -117,6 +117,8 @@ struct inode *udf_new_inode(struct inode *dir, umode_t mode, int *err)
 	iinfo->i_lenAlloc = 0;
 	iinfo->i_use = 0;
 	iinfo->i_checkpoint = 1;
+	memset(&iinfo->cached_extent, 0, sizeof(struct udf_ext_cache));
+	mutex_init(&(iinfo->i_extent_cache_lock));
 	if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_AD_IN_ICB))
 		iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
 	else if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
fs/udf/inode.c +70 -7

129 lines not on this page · raw message

fs/udf/udf_i.h +17 -0
index bb8309d..ec3878a 100644
@@ -1,6 +1,20 @@
 #ifndef _UDF_I_H
 #define _UDF_I_H
 
+struct extent_position {
+	struct buffer_head *bh;
+	uint32_t offset;
+	struct kernel_lb_addr block;
+};
+
+struct udf_ext_cache {
+	/* Extent position */
+	struct extent_position epos;
+	/* Start logical block */
+	loff_t eblock;
+	int8_t sanity;
+};
+
 /*
  * The i_data_sem and i_mutex serve for protection of allocation information
  * of a regular files and symlinks. This includes all extents belonging to
@@ -35,6 +49,9 @@ struct udf_inode_info {
 		__u8		*i_data;
 	} i_ext;
 	struct rw_semaphore	i_data_sem;
+	struct udf_ext_cache cached_extent;
+	/* Mutex for protecting extent cache */
+	struct mutex i_extent_cache_lock;
 	struct inode vfs_inode;
 };
 
fs/udf/udfdecl.h +5 -5
index de038da..46eadd3 100644
@@ -113,11 +113,6 @@ struct ustr {
 	uint8_t u_len;
 };
 
-struct extent_position {
-	struct buffer_head *bh;
-	uint32_t offset;
-	struct kernel_lb_addr block;
-};
 
 /* super.c */
 
@@ -164,6 +159,11 @@ extern int8_t udf_next_aext(struct inode *, struct extent_position *,
 			    struct kernel_lb_addr *, uint32_t *, int);
 extern int8_t udf_current_aext(struct inode *, struct extent_position *,
 			       struct kernel_lb_addr *, uint32_t *, int);
+int udf_read_extent_cache(struct inode *inode, loff_t bcount, loff_t *lbcount,
+			  struct extent_position *pos);
+void udf_update_extent_cache(struct inode *inode, loff_t estart,
+			     struct extent_position *pos);
+void udf_clear_extent_cache(struct udf_inode_info *iinfo);
 
 /* misc.c */
 extern struct buffer_head *udf_tgetblk(struct super_block *, int);
Signature · 1 line
1.7.9.5
#1 lore raw

Hello,

Quoted 9 lines · Namjae Jeon

From: Namjae Jeon <[email protected]>

While mapping logical blocks of a file to physical blocks on the partition, everytime UDF read file metadata from the begining which decrease preformance. The drawback of this scheme is more prominent while reading large files. For example, while reading a large file of ~5GB, read speed will gradually become less as we near the end of file because of the time taken in calculating the corresponding physical block.

Quoted 3 lines

This patch implements caching and remembers the location of the last read extent. Instead of reading file metadata from begining, start from the cached location.

Thanks for the patch. I like this much better than the previous one. Some
comments are below:

Quoted 8 lines
fs/udf/inode.c +0 -0
index 1a0588e..f4cc4e0 100644
@@ -357,8 +359,14 @@ static int udf_get_block(struct inode *inode, sector_t block,
 	if (!phys)
 		goto abort;
 

fs/udf/inode.c static int udf_get_block(struct inode *inode, sector_t block, · line 370

Quoted 10 lines
fs/udf/inode.c +7 -1
-	if (new)
+	if (new) {
+		unsigned char lshift = inode->i_sb->s_blocksize_bits;
 		set_buffer_new(bh_result);
+		if (iinfo->cached_extent.sanity &&
+		    (iinfo->cached_extent.eblock > (block << lshift)))
+			/* Block allocated for hole, invalidate cache */
+			udf_clear_extent_cache(iinfo);
+	}
 	map_bh(bh_result, inode->i_sb, phys);

Instead of this, you should change inode_getblk() to also use the extent
cache (for initialization of prev_epos when appropriate) and then update the extent cache to the just found / created extent when the function is done.

Quoted 6 lines
fs/udf/inode.c +0 -1
 abort:
@@ -2132,11 +2144,12 @@ int8_t inode_bmap(struct inode *inode, sector_t block,
 	struct udf_inode_info *iinfo;
 
 	iinfo = UDF_I(inode);
-	pos->offset = 0;

fs/udf/inode.c int8_t inode_bmap(struct inode *inode, sector_t block, · line 2152

Quoted 10 lines
fs/udf/inode.c +6 -4
-	pos->block = iinfo->i_location;
-	pos->bh = NULL;
-	*elen = 0;
-
+	if (!udf_read_extent_cache(inode, bcount, &lbcount, pos)) {
+		pos->offset = 0;
+		pos->block = iinfo->i_location;
+		pos->bh = NULL;
+	}
+		*elen = 0;

There's TAB added here which doesn't make sense.

Quoted 3 lines
fs/udf/inode.c +0 -0
 	do {
 		etype = udf_next_aext(inode, pos, eloc, elen, 1);
 		if (etype == -1) {

...

Quoted 9 lines
fs/udf/inode.c +5 -0
@@ -2176,3 +2190,52 @@ long udf_block_map(struct inode *inode, sector_t block)
 	else
 		return ret;
 }
+int udf_read_extent_cache(struct inode *inode, loff_t bcount,
+			  loff_t *lbcount, struct extent_position *pos)
+{
+	struct udf_inode_info *iinfo = UDF_I(inode);
+	mutex_lock(&iinfo->i_extent_cache_lock);

fs/udf/inode.c long udf_block_map(struct inode *inode, sector_t block) · line 2207

Quoted 10 lines
fs/udf/inode.c +10 -0
+	if ((iinfo->cached_extent.eblock <= bcount) &&
+	    (iinfo->cached_extent.sanity)) {
+		/* Cache hit */
+		*lbcount = iinfo->cached_extent.eblock;
+		memcpy(pos, &iinfo->cached_extent.epos,
+		       sizeof(struct extent_position));
+		mutex_unlock(&iinfo->i_extent_cache_lock);
+		return 1;
+	} else
+		udf_clear_extent_cache(iinfo);

It would be less confusing, if udf_read_extent_cache() didn't clear the
cache in case of a miss. Then in udf_update_extent_cache() just clear the cache if it is still valid.

Quoted 24 lines
fs/udf/inode.c +24 -0
+
+	mutex_unlock(&iinfo->i_extent_cache_lock);
+	return 0;
+}
+void udf_update_extent_cache(struct inode *inode, loff_t estart,
+			     struct extent_position *pos)
+{
+	struct udf_inode_info *iinfo = UDF_I(inode);
+	mutex_lock(&iinfo->i_extent_cache_lock);
+	if (pos->bh != NULL)
+		/* Increase ref count */
+		get_bh(pos->bh);
+	memcpy(&iinfo->cached_extent.epos, pos,
+	       sizeof(struct extent_position));
+	iinfo->cached_extent.eblock = estart;
+	iinfo->cached_extent.sanity = 1;
+	switch (iinfo->i_alloc_type) {
+	case ICBTAG_FLAG_AD_SHORT:
+		iinfo->cached_extent.epos.offset -= sizeof(struct short_ad);
+		break;
+	case ICBTAG_FLAG_AD_LONG:
+		iinfo->cached_extent.epos.offset -= sizeof(struct long_ad);
+	}
+	mutex_unlock(&iinfo->i_extent_cache_lock);

fs/udf/inode.c long udf_block_map(struct inode *inode, sector_t block) · line 2241

Quoted 10 lines
fs/udf/inode.c +10 -0
+}
+
+void udf_clear_extent_cache(struct udf_inode_info *iinfo)
+{
+	if (iinfo->cached_extent.sanity) {
+		brelse(iinfo->cached_extent.epos.bh);
+		memset(&iinfo->cached_extent, 0, sizeof(struct udf_ext_cache));
+	}
+}
+

I think udf_clear_entent_cache() should take i_extent_cache_lock. Or if
you are sure it's not needed, you need a good documentation why.

Quoted 9 lines
fs/udf/udf_i.h +1 -0
index bb8309d..ec3878a 100644
@@ -1,6 +1,20 @@
 #ifndef _UDF_I_H
 #define _UDF_I_H
 
+struct extent_position {

fs/udf/udf_i.h line 14

Quoted 10 lines
fs/udf/udf_i.h +10 -0
+	struct buffer_head *bh;
+	uint32_t offset;
+	struct kernel_lb_addr block;
+};
+
+struct udf_ext_cache {
+	/* Extent position */
+	struct extent_position epos;
+	/* Start logical block */
+	loff_t eblock;

Well, this is really in bytes not blocks, isn't it? Then call it like
'lstart' and add a comment that it's in bytes.

fs/udf/udf_i.h line 15

Quoted 1 line
fs/udf/udf_i.h +1 -0
+	int8_t sanity;

Make this 'bool' and call it 'valid'. That's more common.

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#2 lore raw

2012/9/3, Jan Kara <[email protected]>:

Quoted 17 lines

Hello,

Quoted 14 lines · Namjae Jeon

From: Namjae Jeon <[email protected]>

While mapping logical blocks of a file to physical blocks on the partition,
everytime UDF read file metadata from the begining which decrease preformance.
The drawback of this scheme is more prominent while reading large files. For example, while reading a large file of ~5GB, read speed will gradually become less as we near the end of file because of the time taken in calculating the corresponding physical block.

This patch implements caching and remembers the location of the last read extent. Instead of reading file metadata from begining, start from the cached location.

Hi. Jan.

Quoted 2 lines

Thanks for the patch. I like this much better than the previous one. Some
comments are below:

Thank you~

Quoted 10 lines
Quoted 10 lines
fs/udf/inode.c +0 -0
index 1a0588e..f4cc4e0 100644
@@ -357,8 +359,14 @@ static int udf_get_block(struct inode *inode,
 sector_t block,
 	if (!phys)
 		goto abort;
 
Quoted 14 lines

fs/udf/inode.c static int udf_get_block(struct inode *inode, sector_t block, · line 370

Quoted 10 lines
fs/udf/inode.c +7 -1
-	if (new)
+	if (new) {
+		unsigned char lshift = inode->i_sb->s_blocksize_bits;
 		set_buffer_new(bh_result);
+		if (iinfo->cached_extent.sanity &&
+		    (iinfo->cached_extent.eblock > (block << lshift)))
+			/* Block allocated for hole, invalidate cache */
+			udf_clear_extent_cache(iinfo);
+	}
 	map_bh(bh_result, inode->i_sb, phys);

Instead of this, you should change inode_getblk() to also use the extent
cache (for initialization of prev_epos when appropriate) and then update the extent cache to the just found / created extent when the function is done.

Okay, I will check about your suggestion.

Quoted 39 lines
Quoted 8 lines
fs/udf/inode.c +0 -1
 abort:
@@ -2132,11 +2144,12 @@ int8_t inode_bmap(struct inode *inode, sector_t
 block,
 	struct udf_inode_info *iinfo;
 
 	iinfo = UDF_I(inode);
-	pos->offset = 0;

fs/udf/inode.c int8_t inode_bmap(struct inode *inode, sector_t · line 2153

Quoted 10 lines
fs/udf/inode.c +6 -4
-	pos->block = iinfo->i_location;
-	pos->bh = NULL;
-	*elen = 0;
-
+	if (!udf_read_extent_cache(inode, bcount, &lbcount, pos)) {
+		pos->offset = 0;
+		pos->block = iinfo->i_location;
+		pos->bh = NULL;
+	}
+		*elen = 0;

There's TAB added here which doesn't make sense.

Quoted 3 lines
fs/udf/inode.c +0 -0
 	do {
 		etype = udf_next_aext(inode, pos, eloc, elen, 1);

if (etype == -1) {

...

Quoted 15 lines
fs/udf/inode.c +10 -0
@@ -2176,3 +2190,52 @@ long udf_block_map(struct inode *inode, sector_t
 block)
 	else
 		return ret;
 }
+int udf_read_extent_cache(struct inode *inode, loff_t bcount,
+			  loff_t *lbcount, struct extent_position *pos)
+{
+	struct udf_inode_info *iinfo = UDF_I(inode);
+	mutex_lock(&iinfo->i_extent_cache_lock);
+	if ((iinfo->cached_extent.eblock <= bcount) &&
+	    (iinfo->cached_extent.sanity)) {
+		/* Cache hit */
+		*lbcount = iinfo->cached_extent.eblock;
+		memcpy(pos, &iinfo->cached_extent.epos,
Quoted 8 lines

fs/udf/inode.c long udf_block_map(struct inode *inode, sector_t block) · line 2207

Quoted 5 lines
fs/udf/inode.c +5 -0
+		       sizeof(struct extent_position));
+		mutex_unlock(&iinfo->i_extent_cache_lock);
+		return 1;
+	} else
+		udf_clear_extent_cache(iinfo);

It would be less confusing, if udf_read_extent_cache() didn't clear the
cache in case of a miss. Then in udf_update_extent_cache() just clear the cache if it is still valid.

Okay, It is more effective! I will change ~

Quoted 29 lines
Quoted 29 lines
fs/udf/inode.c +28 -0
+
+	mutex_unlock(&iinfo->i_extent_cache_lock);
+	return 0;
+}
+void udf_update_extent_cache(struct inode *inode, loff_t estart,
+			     struct extent_position *pos)
+{
+	struct udf_inode_info *iinfo = UDF_I(inode);
+	mutex_lock(&iinfo->i_extent_cache_lock);
+	if (pos->bh != NULL)
+		/* Increase ref count */
+		get_bh(pos->bh);
+	memcpy(&iinfo->cached_extent.epos, pos,
+	       sizeof(struct extent_position));
+	iinfo->cached_extent.eblock = estart;
+	iinfo->cached_extent.sanity = 1;
+	switch (iinfo->i_alloc_type) {
+	case ICBTAG_FLAG_AD_SHORT:
+		iinfo->cached_extent.epos.offset -= sizeof(struct short_ad);
+		break;
+	case ICBTAG_FLAG_AD_LONG:
+		iinfo->cached_extent.epos.offset -= sizeof(struct long_ad);
+	}
+	mutex_unlock(&iinfo->i_extent_cache_lock);
+}
+
+void udf_clear_extent_cache(struct udf_inode_info *iinfo)
+{
Quoted 8 lines

fs/udf/inode.c long udf_block_map(struct inode *inode, sector_t block) · line 2241

Quoted 6 lines
fs/udf/inode.c +6 -0
+	if (iinfo->cached_extent.sanity) {
+		brelse(iinfo->cached_extent.epos.bh);
+		memset(&iinfo->cached_extent, 0, sizeof(struct udf_ext_cache));
+	}
+}
+

I think udf_clear_entent_cache() should take i_extent_cache_lock. Or if
you are sure it's not needed, you need a good documentation why.

Documentation ? I am a little confusing.. It means udf.txt is in Documentation/filesystem/ ?
or comment about clear extent function ?

Quoted 9 lines
Quoted 9 lines
fs/udf/udf_i.h +0 -0
index bb8309d..ec3878a 100644
@@ -1,6 +1,20 @@
 #ifndef _UDF_I_H
 #define _UDF_I_H
 
Quoted 13 lines

fs/udf/udf_i.h line 14

Quoted 11 lines
fs/udf/udf_i.h +11 -0
+struct extent_position {
+	struct buffer_head *bh;
+	uint32_t offset;
+	struct kernel_lb_addr block;
+};
+
+struct udf_ext_cache {
+	/* Extent position */
+	struct extent_position epos;
+	/* Start logical block */
+	loff_t eblock;

Well, this is really in bytes not blocks, isn't it? Then call it like
'lstart' and add a comment that it's in bytes.

Yes, It is in bytes. And I will add comment.

Quoted 3 lines

fs/udf/udf_i.h line 15

Quoted 2 lines
fs/udf/udf_i.h +1 -0
+	int8_t sanity;

Make this 'bool' and call it 'valid'. That's more common.

Okay, I will modify bool.

Thanks a lot. Jan!

Quoted 6 lines

Honza
--
Jan Kara <[email protected]>
SUSE Labs, CR

#3 lore raw
Quoted 11 lines · Namjae Jeon

2012/9/3, Jan Kara <[email protected]>:

Quoted 10 lines

fs/udf/inode.c long udf_block_map(struct inode *inode, sector_t block) · line 2241

Quoted 8 lines
fs/udf/inode.c +8 -0
+void udf_clear_extent_cache(struct udf_inode_info *iinfo)
+{
+	if (iinfo->cached_extent.sanity) {
+		brelse(iinfo->cached_extent.epos.bh);
+		memset(&iinfo->cached_extent, 0, sizeof(struct udf_ext_cache));
+	}
+}
+

I think udf_clear_entent_cache() should take i_extent_cache_lock. Or if
you are sure it's not needed, you need a good documentation why.

Quoted 3 lines

Documentation ? I am a little confusing.. It means udf.txt is in Documentation/filesystem/ ?
or comment about clear extent function ?

I meant in a comment before udf_clear_extent_cache() function...

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#4 lore raw

2012/9/4 Jan Kara <[email protected]>:

Quoted 12 lines · Namjae Jeon
Quoted 11 lines

2012/9/3, Jan Kara <[email protected]>:

Quoted 10 lines

fs/udf/inode.c long udf_block_map(struct inode *inode, sector_t block) · line 2241

Quoted 8 lines
fs/udf/inode.c +8 -0
+void udf_clear_extent_cache(struct udf_inode_info *iinfo)
+{
+  if (iinfo->cached_extent.sanity) {
+          brelse(iinfo->cached_extent.epos.bh);
+          memset(&iinfo->cached_extent, 0, sizeof(struct udf_ext_cache));
+  }
+}
+

I think udf_clear_entent_cache() should take i_extent_cache_lock. Or if
you are sure it's not needed, you need a good documentation why.

Quoted 4 lines
Quoted 3 lines

Documentation ? I am a little confusing.. It means udf.txt is in Documentation/filesystem/ ?
or comment about clear extent function ?

I meant in a comment before udf_clear_extent_cache() function...

Okay, I understood.
Thanks a lot.

Quoted 5 lines

Honza
--
Jan Kara <[email protected]>
SUSE Labs, CR