Skip to contentSkip to conversation

ext4: implement ext4_fiemap

PATCH In review Mark Fasheh 4 participants 4 messages Jun 25, 2008 – Jul 4, 2008

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 4/4 ext4: implement ext4_fiemap

From: Eric Sandeen <[email protected]>

Hook ext4 to the vfs fiemap interface.

ext4_ext_walk_space() was reinstated to be used for iterating over file extents with a callback; it is used by the ext4 fiemap implementation.

---
 fs/ext4/ext4.h         |    2 +
 fs/ext4/ext4_extents.h |   15 +++
 fs/ext4/extents.c      |  248 ++++++++++++++++++++++++++++++++++++++++++++++++
 fs/ext4/file.c         |    4 +
 fs/ext4/inode.c        |    2 +-
 5 files changed, 270 insertions(+), 1 deletions(-)
fs/ext4/ext4.h +2 -0
index 8158083..99e1c44 100644
@@ -1025,6 +1025,8 @@ struct buffer_head *ext4_getblk(handle_t *, struct inode *,
 						ext4_lblk_t, int, int *);
 struct buffer_head *ext4_bread(handle_t *, struct inode *,
 						ext4_lblk_t, int, int *);
+int ext4_get_block(struct inode *inode, sector_t iblock,
+				struct buffer_head *bh_result, int create);
 int ext4_get_blocks_handle(handle_t *handle, struct inode *inode,
 				ext4_lblk_t iblock, unsigned long maxblocks,
 				struct buffer_head *bh_result,
fs/ext4/ext4_extents.h +15 -0
index 75333b5..c9fabb0 100644
@@ -124,6 +124,19 @@ struct ext4_ext_path {
 #define EXT4_EXT_CACHE_GAP	1
 #define EXT4_EXT_CACHE_EXTENT	2
 
+/*
+ * to be called by ext4_ext_walk_space()
+ * negative retcode - error
+ * positive retcode - signal for ext4_ext_walk_space(), see below
+ * callback must return valid extent (passed or newly created)
+ */
+typedef int (*ext_prepare_callback)(struct inode *, struct ext4_ext_path *,
+					struct ext4_ext_cache *,
+					struct ext4_extent *, void *);
+
+#define EXT_CONTINUE   0
+#define EXT_BREAK      1
+#define EXT_REPEAT     2
 
 #define EXT_MAX_BLOCK	0xffffffff
 
@@ -221,6 +234,8 @@ extern int ext4_ext_try_to_merge(struct inode *inode,
 				 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_walk_space(struct inode *, ext4_lblk_t, ext4_lblk_t,
+							ext_prepare_callback, void *);
 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/extents.c +248 -0

263 lines not on this page · raw message

fs/ext4/file.c +4 -0
index 4159be6..99bde24 100644
@@ -123,6 +123,9 @@ force_commit:
 	return ret;
 }
 
+extern int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
+		__u64 start, __u64 len);
+
 const struct file_operations ext4_file_operations = {
 	.llseek		= generic_file_llseek,
 	.read		= do_sync_read,
@@ -152,5 +155,6 @@ const struct inode_operations ext4_file_inode_operations = {
 #endif
 	.permission	= ext4_permission,
 	.fallocate	= ext4_fallocate,
+	.fiemap		= ext4_fiemap,
 };
 
fs/ext4/inode.c +1 -1
index 8d97077..59ecbbc 100644
@@ -1000,7 +1000,7 @@ int ext4_get_blocks_wrap(handle_t *handle, struct inode *inode, sector_t block,
 	return retval;
 }
 
-static int ext4_get_block(struct inode *inode, sector_t iblock,
+int ext4_get_block(struct inode *inode, sector_t iblock,
 			struct buffer_head *bh_result, int create)
 {
 	handle_t *handle = ext4_journal_current_handle();
Signature · 1 line
1.5.4.1
#1 lore raw
Quoted 27 lines · Mark Fasheh

From: Eric Sandeen <[email protected]>

(quoted diff) +26 -0
+/* fiemap flags we can handle specified here */
+#define EXT4_FIEMAP_FLAGS	(FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
+
+int ext4_xattr_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo)
+{
+	__u64 physical = 0;
+	__u64 length;
+	__u32 flags = FIEMAP_EXTENT_LAST;
+	int blockbits = inode->i_sb->s_blocksize_bits;
+	int error = 0;
+
+	/* in-inode? */
+	if (EXT4_I(inode)->i_state & EXT4_STATE_XATTR) {
+		struct ext4_iloc iloc;
+		int offset;	/* offset of xattr in inode */
+
+		error = ext4_get_inode_loc(inode, &iloc);
+		if (error)
+			return error;
+		physical = iloc.bh->b_blocknr << blockbits;
+		offset = EXT4_GOOD_OLD_INODE_SIZE +
+				EXT4_I(inode)->i_extra_isize;
+		physical += offset;
+		length = EXT4_SB(inode->i_sb)->s_inode_size - offset;
+		flags |= FIEMAP_EXTENT_DATA_INLINE;
+	} else { /* external block */

Review

Quoted 10 lines
(quoted diff) +10 -0
+		physical = EXT4_I(inode)->i_file_acl << blockbits;
+		length = inode->i_sb->s_blocksize;
+	}
+
+	if (physical)
+		error = fiemap_fill_next_extent(fieinfo, 0, physical,
+					length, flags, inode->i_sb->s_dev);
+	return (error < 0 ? error : 0);
+}
+

Extended attributes can be present in the inode as well as the external block at the same time and we should be returning extents for both them in that case. Below patch fixes this problem:

fs/ext4/extents.c +16 -6
Index: linux-2.6/fs/ext4/extents.c
===================================================================
@@ -3130,7 +3130,7 @@ int ext4_xattr_fiemap(struct inode *inod
 {
 	__u64 physical = 0;
 	__u64 length;
-	__u32 flags = FIEMAP_EXTENT_LAST;
+	__u32 flags = 0;
 	int blockbits = inode->i_sb->s_blocksize_bits;
 	int error = 0;
 
@@ -3148,14 +3148,24 @@ int ext4_xattr_fiemap(struct inode *inod
 		physical += offset;
 		length = EXT4_SB(inode->i_sb)->s_inode_size - offset;
 		flags |= FIEMAP_EXTENT_DATA_INLINE;
-	} else { /* external block */
-		physical = EXT4_I(inode)->i_file_acl << blockbits;
-		length = inode->i_sb->s_blocksize;
+		if (EXT4_I(inode)->i_file_acl == 0)
+			flags |= FIEMAP_EXTENT_LAST;
+
+		error = fiemap_fill_next_extent(fieinfo, 0, physical, length,
+						flags, inode->i_sb->s_dev);
+		if (error)
+			goto out;
 	}
 
+	/* external block */
+	flags = FIEMAP_EXTENT_LAST;
+	physical = EXT4_I(inode)->i_file_acl << blockbits;
+	length = inode->i_sb->s_blocksize;
 	if (physical)
-		error = fiemap_fill_next_extent(fieinfo, 0, physical,
-					length, flags, inode->i_sb->s_dev);
+		error = fiemap_fill_next_extent(fieinfo, 0, physical, length,
+						flags, inode->i_sb->s_dev);
+
+out:
 	return (error < 0 ? error : 0);
 }
 

Thanks,
Kalpak

#2 lore raw
Quoted 1 line · Kalpak Shah

Extended attributes can be present in the inode as well as the external block at the same time and we should be returning extents for both them in that case. Below patch fixes this problem:

Thanks Kalpak, was on my todo list but not done. :)

-Eric

Quoted 50 lines

Index: linux-2.6/fs/ext4/extents.c
===================================================================

fs/ext4/extents.c +16 -6
@@ -3130,7 +3130,7 @@ int ext4_xattr_fiemap(struct inode *inod
 {
 	__u64 physical = 0;
 	__u64 length;
-	__u32 flags = FIEMAP_EXTENT_LAST;
+	__u32 flags = 0;
 	int blockbits = inode->i_sb->s_blocksize_bits;
 	int error = 0;
 
@@ -3148,14 +3148,24 @@ int ext4_xattr_fiemap(struct inode *inod
 		physical += offset;
 		length = EXT4_SB(inode->i_sb)->s_inode_size - offset;
 		flags |= FIEMAP_EXTENT_DATA_INLINE;
-	} else { /* external block */
-		physical = EXT4_I(inode)->i_file_acl << blockbits;
-		length = inode->i_sb->s_blocksize;
+		if (EXT4_I(inode)->i_file_acl == 0)
+			flags |= FIEMAP_EXTENT_LAST;
+
+		error = fiemap_fill_next_extent(fieinfo, 0, physical, length,
+						flags, inode->i_sb->s_dev);
+		if (error)
+			goto out;
 	}
 
+	/* external block */
+	flags = FIEMAP_EXTENT_LAST;
+	physical = EXT4_I(inode)->i_file_acl << blockbits;
+	length = inode->i_sb->s_blocksize;
 	if (physical)
-		error = fiemap_fill_next_extent(fieinfo, 0, physical,
-					length, flags, inode->i_sb->s_dev);
+		error = fiemap_fill_next_extent(fieinfo, 0, physical, length,
+						flags, inode->i_sb->s_dev);
+
+out:
 	return (error < 0 ? error : 0);
 }
 

Thanks,
Kalpak

#3 lore raw
In reply to Kalpak Shah
Quoted 30 lines · Kalpak Shah
Quoted 10 lines · Mark Fasheh
(quoted diff) +10 -0
+int ext4_xattr_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo)
+{
+	/* in-inode? */
+	if (EXT4_I(inode)->i_state & EXT4_STATE_XATTR) {
+		struct ext4_iloc iloc;
+		int offset;	/* offset of xattr in inode */
+
+		physical = iloc.bh->b_blocknr << blockbits;
+		offset = EXT4_GOOD_OLD_INODE_SIZE +
+				EXT4_I(inode)->i_extra_isize;
fs/ext4/extents.c +9 -3
@@ -3148,14 +3148,24 @@ int ext4_xattr_fiemap(struct inode *inod
 		physical += offset;
 		length = EXT4_SB(inode->i_sb)->s_inode_size - offset;
 		flags |= FIEMAP_EXTENT_DATA_INLINE;
-	} else { /* external block */
-		physical = EXT4_I(inode)->i_file_acl << blockbits;
-		length = inode->i_sb->s_blocksize;
+		if (EXT4_I(inode)->i_file_acl == 0)
+			flags |= FIEMAP_EXTENT_LAST;
+
+		error = fiemap_fill_next_extent(fieinfo, 0, physical, length,
+						flags, inode->i_sb->s_dev);
+		if (error)
+			goto out;
 	}
 
+	/* external block */
+	flags = FIEMAP_EXTENT_LAST;

fs/ext4/extents.c int ext4_xattr_fiemap(struct inode *inod · line 3169

Quoted 10 lines
fs/ext4/extents.c +6 -2
+	physical = EXT4_I(inode)->i_file_acl << blockbits;
+	length = inode->i_sb->s_blocksize;
 	if (physical)
-		error = fiemap_fill_next_extent(fieinfo, 0, physical,
-					length, flags, inode->i_sb->s_dev);
+		error = fiemap_fill_next_extent(fieinfo, 0, physical, length,
+						flags, inode->i_sb->s_dev);
+
+out:
 	return (error < 0 ? error : 0);

It seems a bit unpleasant to have both logical offsets be 0 in this case. I'd suggest having the in-inode logical offset be "offset", and the external EA logical offset be "s_inode_size" or "s_blocksize".

Cheers, Andreas
--
Andreas Dilger
Sr. Staff Engineer, Lustre Group
Sun Microsystems of Canada, Inc.