Skip to contentSkip to conversation

udf: extent cache implementation for manipulating block map

PATCH RESEND Superseded Namjae Jeon 2 participants 5 messages Aug 18, 2012 – Aug 22, 2012 UDF filesystem

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH RESEND 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   |   83 ++++++++++++++++++++++++++++++++++++++++++++++++++----
 fs/udf/udf_i.h   |   13 +++++++++
 fs/udf/udfdecl.h |   13 +++++++++
 4 files changed, 105 insertions(+), 6 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 +77 -6

133 lines not on this page · raw message

fs/udf/udf_i.h +13 -0
index bb8309d..ec168a9 100644
@@ -1,6 +1,16 @@
 #ifndef _UDF_I_H
 #define _UDF_I_H
 
+struct udf_ext_cache {
+	struct kernel_lb_addr epos;
+	uint32_t offset;
+	uint32_t p_block_nr;
+	struct kernel_lb_addr eloc;
+	uint32_t elen;
+	int8_t etype;
+	loff_t last_block;
+};
+
 /*
  * 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 +45,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 +13 -0
index de038da..b3d9f50 100644
@@ -44,6 +44,7 @@ extern __printf(3, 4) void _udf_warn(struct super_block *sb,
 
 #define udf_fixed_to_variable(x) ( ( ( (x) >> 5 ) * 39 ) + ( (x) & 0x0000001F ) )
 #define udf_variable_to_fixed(x) ( ( ( (x) / 39 ) << 5 ) + ( (x) % 39 ) )
+#define in_range(last, len, b) ((b) >= (last - len) && (b) < last)
 
 #define UDF_EXTENT_LENGTH_MASK	0x3FFFFFFF
 #define UDF_EXTENT_FLAG_MASK	0xC0000000
@@ -52,6 +53,11 @@ extern __printf(3, 4) void _udf_warn(struct super_block *sb,
 #define UDF_NAME_LEN		256
 #define UDF_PATH_LEN		1023
 
+static inline void udf_clear_extent_cache(struct udf_inode_info *iinfo)
+{
+	iinfo->cached_extent.last_block = 0;
+}
+
 static inline size_t udf_file_entry_alloc_offset(struct inode *inode)
 {
 	struct udf_inode_info *iinfo = UDF_I(inode);
@@ -165,6 +171,13 @@ extern int8_t udf_next_aext(struct inode *, struct extent_position *,
 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,
+		uint32_t *elen, struct extent_position *pos,
+		struct kernel_lb_addr *eloc, int8_t *etype);
+void udf_update_extent_cache(struct inode *inode, uint32_t *elen,
+		struct extent_position *pos, loff_t *lbcount,
+		struct kernel_lb_addr *eloc, int8_t *etype);
+
 /* misc.c */
 extern struct buffer_head *udf_tgetblk(struct super_block *, int);
 extern struct buffer_head *udf_tread(struct super_block *, int);
Signature · 1 line
1.7.9.5
#1 lore raw

Hello,

first, I'm sorry for a late reply. I was on a long vacation and then it
took me a while to catch up with stuff.

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.

I agree this functionality is useful. Thanks for implementing it! I have
some comments regarding the implementation below:

Quoted 28 lines
---
 fs/udf/ialloc.c  |    2 ++
 fs/udf/inode.c   |   83 ++++++++++++++++++++++++++++++++++++++++++++++++++----
 fs/udf/udf_i.h   |   13 +++++++++
 fs/udf/udfdecl.h |   13 +++++++++
 4 files changed, 105 insertions(+), 6 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 +0 -0
index fafaad7..cf34dec 100644

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

Quoted 8 lines
fs/udf/inode.c +1 -1
@@ -345,7 +345,7 @@ static int udf_get_block(struct inode *inode, sector_t block,
 		iinfo->i_next_alloc_goal++;
 	}
 
-
+	udf_clear_extent_cache(iinfo);
 	phys = inode_getblk(inode, block, &err, &new);
 	if (!phys)
 		goto abort;

This is certainly the easiest thing to do. But it nags me that simple
appending writes are not able to use the extent cache. It even needn't be that complicated if we carefully pick the things to cache - see below.

...

Quoted 8 lines
fs/udf/udf_i.h +0 -0
index bb8309d..ec168a9 100644
@@ -1,6 +1,16 @@
 #ifndef _UDF_I_H
 #define _UDF_I_H
 

fs/udf/udf_i.h line 13

Quoted 10 lines
fs/udf/udf_i.h +10 -0
+struct udf_ext_cache {
+	struct kernel_lb_addr epos;
+	uint32_t offset;
+	uint32_t p_block_nr;
+	struct kernel_lb_addr eloc;
+	uint32_t elen;
+	int8_t etype;
+	loff_t last_block;
+};
+

Umm, I think caching things slightly differently might make things
easier. I'd do:

struct udf_ext_cache {
	/*
	 * Buffer head with extent or NULL if the extent is in inode
	 * (need to have buffer reference via get_bh!)
	 */
	struct buffer_head *extent_bh;
	/*
	 * Extent position - this is somewhat redundant since we have the
	 * bh but code in block mapping functions expects to have this
	 */
	struct kernel_lb_addr epos;
	/* Offset of cached extent in the buffer head / inode */
	uint32_t offset;
	/* Logical block where cached extent starts */
	sector_t block;
};

So you cache extent position, bh, and it's logical position within inode. udf_read_extent_cache() would check whether queried logical block is after our cached logical block. If yes, we would decode extent from our bh / inode and proceed as you did.

The advantage I see in this approach is that we cache a bit less, don't have to lookup the buffer head (although that is pinned in memory now so total memory consumption might be higher in some cases), and we don't have to invalidate the cache when the cached logical block is before the block we write to (which nicely catches also extending writes).

What do you think?

fs/udf/udf_i.h struct udf_inode_info { · line 52

Quoted 12 lines
fs/udf/udf_i.h +3 -0
 /*
  * 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 +45,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;
 };

Honza

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

2012/8/21, Jan Kara <[email protected]>:

Quoted 109 lines

Hello,

first, I'm sorry for a late reply. I was on a long vacation and then it
took me a while to catch up with stuff.

Quoted 11 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.

I agree this functionality is useful. Thanks for implementing it! I have
some comments regarding the implementation below:

Quoted 38 lines
---
 fs/udf/ialloc.c  |    2 ++
 fs/udf/inode.c   |   83
++++++++++++++++++++++++++++++++++++++++++++++++++----
 fs/udf/udf_i.h   |   13 +++++++++
 fs/udf/udfdecl.h |   13 +++++++++
 4 files changed, 105 insertions(+), 6 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;
fs/udf/ialloc.c +0 -0
 	else if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
fs/udf/inode.c +1 -1
index fafaad7..cf34dec 100644
@@ -345,7 +345,7 @@ static int udf_get_block(struct inode *inode, sector_t
 block,
 		iinfo->i_next_alloc_goal++;
 	}
 
-
+	udf_clear_extent_cache(iinfo);
 	phys = inode_getblk(inode, block, &err, &new);
 	if (!phys)
Quoted 1 line

goto abort;

This is certainly the easiest thing to do. But it nags me that simple
appending writes are not able to use the extent cache. It even needn't be that complicated if we carefully pick the things to cache - see below.

...

Quoted 8 lines
fs/udf/udf_i.h +0 -0
index bb8309d..ec168a9 100644
@@ -1,6 +1,16 @@
 #ifndef _UDF_I_H
 #define _UDF_I_H
 

fs/udf/udf_i.h line 13

Quoted 10 lines
fs/udf/udf_i.h +10 -0
+struct udf_ext_cache {
+	struct kernel_lb_addr epos;
+	uint32_t offset;
+	uint32_t p_block_nr;
+	struct kernel_lb_addr eloc;
+	uint32_t elen;
+	int8_t etype;
+	loff_t last_block;
+};
+

Umm, I think caching things slightly differently might make things
easier. I'd do:

struct udf_ext_cache {
	/*
	 * Buffer head with extent or NULL if the extent is in inode
	 * (need to have buffer reference via get_bh!)
	 */
	struct buffer_head *extent_bh;
	/*
	 * Extent position - this is somewhat redundant since we have the
	 * bh but code in block mapping functions expects to have this
	 */
	struct kernel_lb_addr epos;
	/* Offset of cached extent in the buffer head / inode */
	uint32_t offset;
	/* Logical block where cached extent starts */
	sector_t block;
};

So you cache extent position, bh, and it's logical position within inode. udf_read_extent_cache() would check whether queried logical block is after our cached logical block. If yes, we would decode extent from our bh / inode and proceed as you did.

Quoted 7 lines

The advantage I see in this approach is that we cache a bit less, don't have to lookup the buffer head (although that is pinned in memory now so total memory consumption might be higher in some cases), and we don't have to invalidate the cache when the cached logical block is before the block we write to (which nicely catches also extending writes).

What do you think?

Hi. Jan.
Okay, We are trying to do it from your comment.
1. Change udf_ext_cache structure to following which would also include *bh. struct udf_ext_cache {

/* Position of the cached extent */
struct extent_position epos;

/* Logical block where cached extent starts */
sector_t block;
};

2. Remove call to brelse(epos.bh) from all the callers of inode_bmap() and move it to udf_evict_inode()

3. As now we are not caching elen, etype and eloc, we have to change the cache_hit logic in inode_bmap.
The call to function udf_next_aext is now necessary from inode_bmap.

4. Remove call to udf_clear_extent_cache() from udf_get_block as with new scheme, it is not required.

Please let us know in case of any sugguestion or queries.

Thanks.

Quoted 21 lines
Quoted 7 lines
fs/udf/udf_i.h +0 -0
 /*
  * 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

fs/udf/udf_i.h struct udf_inode_info { · line 52

Quoted 8 lines
fs/udf/udf_i.h +3 -0
@@ -35,6 +45,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;
 };

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

#3 lore raw
Quoted 6 lines · Namjae Jeon

2012/8/21, Jan Kara <[email protected]>:
Hi. Jan.
Okay, We are trying to do it from your comment.
1. Change udf_ext_cache structure to following which would also include *bh. struct udf_ext_cache {

Quoted 6 lines

/* Position of the cached extent */
struct extent_position epos;

/* Logical block where cached extent starts */
sector_t block;
};

OK.

Quoted 2 lines

2. Remove call to brelse(epos.bh) from all the callers of inode_bmap() and move it to udf_evict_inode()

It might be easier to keep brelse() where it is and add get_bh() to
udf_add_extent_cache() and brelse() to udf_clear_extent_cache(). It is then easier to audit we don't leak bh references...

Quoted 3 lines

3. As now we are not caching elen, etype and eloc, we have to change the cache_hit logic in inode_bmap.
The call to function udf_next_aext is now necessary from inode_bmap.

Yes.

Quoted 2 lines

4. Remove call to udf_clear_extent_cache() from udf_get_block as with new scheme, it is not required.

You still need this when you write before the cached location (e.g. when
the file has holes, and you write into them, extents will shift).

Honza

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

2012/8/22, Jan Kara <[email protected]>:

Quoted 14 lines · Namjae Jeon
Quoted 7 lines

2012/8/21, Jan Kara <[email protected]>:
Hi. Jan.
Okay, We are trying to do it from your comment.
1. Change udf_ext_cache structure to following which would also include *bh.
struct udf_ext_cache {

Quoted 6 lines

/* Position of the cached extent */
struct extent_position epos;

/* Logical block where cached extent starts */
sector_t block;
};

Quoted 7 lines

OK.

Quoted 2 lines

2. Remove call to brelse(epos.bh) from all the callers of inode_bmap() and move it to udf_evict_inode()

It might be easier to keep brelse() where it is and add get_bh() to
udf_add_extent_cache() and brelse() to udf_clear_extent_cache(). It is then easier to audit we don't leak bh references...

Good point. I will~

Quoted 10 lines
Quoted 4 lines

3. As now we are not caching elen, etype and eloc, we have to change the cache_hit logic in inode_bmap.
The call to function udf_next_aext is now necessary from inode_bmap.

Yes.

Quoted 2 lines

4. Remove call to udf_clear_extent_cache() from udf_get_block as with new scheme, it is not required.

You still need this when you write before the cached location (e.g. when
the file has holes, and you write into them, extents will shift).

Okay, I see.
I will send v2 patch soon.
Thanks for your advice.

Quoted 7 lines

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