Skip to contentSkip to conversation

fiemap support for ext3

PATCH In review Josef Bacik 3 participants 9 messages Apr 23, 2008 – Apr 24, 2008

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

PATCH 1/1 fiemap support for ext3

Hello,

Here is my reworked fiemap patch for ext3. The generic fiemap handler takes the get_block_t from the filesystem and does the fiemap that way, so then adding ext2 support should be a snap, as well as any other fs that wants to use this. Thanks much,

fs/ext3/file.c +1 -0
Index: linux-2.6.25/fs/ext3/file.c
===================================================================
@@ -134,5 +134,6 @@ const struct inode_operations ext3_file_
 	.removexattr	= generic_removexattr,
 #endif
 	.permission	= ext3_permission,
+	.fiemap		= ext3_fiemap,
 };
 
fs/ext3/inode.c +6 -0
Index: linux-2.6.25/fs/ext3/inode.c
===================================================================
@@ -36,6 +36,7 @@
 #include <linux/mpage.h>
 #include <linux/uio.h>
 #include <linux/bio.h>
+#include <linux/fiemap.h>
 #include "xattr.h"
 #include "acl.h"
 
@@ -981,6 +982,11 @@ out:
 	return ret;
 }
 
+int ext3_fiemap(struct inode *inode, unsigned long arg)
+{
+	return generic_block_fiemap(inode, arg, ext3_get_block);
+}
+
 /*
  * `handle' can be NULL if create is zero
  */
include/linux/ext3_fs.h +1 -0
Index: linux-2.6.25/include/linux/ext3_fs.h
===================================================================
@@ -836,6 +836,7 @@ extern void ext3_truncate (struct inode 
 extern void ext3_set_inode_flags(struct inode *);
 extern void ext3_get_inode_flags(struct ext3_inode_info *);
 extern void ext3_set_aops(struct inode *inode);
+extern int ext3_fiemap(struct inode *inode, unsigned long arg);
 
 /* ioctl.c */
 extern int ext3_ioctl (struct inode *, struct file *, unsigned int,
fs/ioctl.c +162 -0

174 lines not on this page · raw message

include/linux/fs.h +2 -0
Index: linux-2.6.25/include/linux/fs.h
===================================================================
@@ -1925,6 +1925,8 @@ extern int vfs_fstat(unsigned int, struc
 extern long vfs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
 extern int do_vfs_ioctl(struct file *filp, unsigned int fd, unsigned int cmd,
 		    unsigned long arg);
+extern int generic_block_fiemap(struct inode *inode, unsigned long arg,
+				get_block_t *get_block);
 
 extern void get_filesystem(struct file_system_type *fs);
 extern void put_filesystem(struct file_system_type *fs);
#1 lore raw
Quoted 3 lines · Josef Bacik

Here is my reworked fiemap patch for ext3. The generic fiemap handler takes the get_block_t from the filesystem and does the fiemap that way, so then adding ext2 support should be a snap, as well as any other fs that wants to use this.

The generic mechanism is really good, some minor issues still exist.

fs/ioctl.c static int ioctl_fiemap(struct file *fil · line 132

Quoted 10 lines
fs/ioctl.c +10 -0
+int generic_block_fiemap(struct inode *inode, unsigned long arg,
+			 get_block_t *get_block)
+{
+	/*
+	 * if fm_start is in the middle of the current block, get the next
+	 * block so we don't end up returning a start thats before the given
+	 * fm_start
+	 */
+	start_blk = (fiemap_s->fm_start + (1 << inode->i_blkbits) - 1) >>
+		inode->i_blkbits;

This should return the start of the current block (so the requested fm_start is included in the returned mapping), instead of the start of the next block.

fs/ioctl.c static int ioctl_fiemap(struct file *fil · line 155

Quoted 8 lines
fs/ioctl.c +8 -0
+	/*
+	 * we want the comparisons to be unsigned, in case somebody passes -1,
+	 * meaning they want they want the entire file, but the result has to be
+	 * signed so we can handle the case where we get more blocks than the
+	 * size of the file
+	 */
+	length = (long)min((unsigned long)fiemap_s->fm_length,
+			   (unsigned long)i_size_read(inode));

This might be written as:

	length = (long)min_t(unsigned long,fiemap_s->fm_len,i_size_read(inode));

Also, what about files that have blocks mapped after i_size?

fs/ioctl.c static int ioctl_fiemap(struct file *fil · line 179

Quoted 3 lines
fs/ioctl.c +3 -0
+		if (!tmp.b_blocknr) {
+
+			if (!hole) {

No empty line above "if (!hole)".

fs/ioctl.c static int ioctl_fiemap(struct file *fil · line 214

Quoted 2 lines
fs/ioctl.c +2 -0
+			fiemap_e.fe_offset = tmp.b_blocknr <<
+				inode->i_blkbits;

This doesn't need to wrap.

fs/ioctl.c static int ioctl_fiemap(struct file *fil · line 218

Quoted 4 lines
fs/ioctl.c +4 -0
+			fiemap_e.fe_length = tmp.b_size;
+
+			if (length <= 0)
+				fiemap_e.fe_flags |= FIEMAP_EXTENT_LAST;

Should the exit condition should be the first hole (block == 0) after i_size?

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

#2 lore raw
Quoted 13 lines · Andreas Dilger

fs/ioctl.c static int ioctl_fiemap(struct file *fil · line 155

Quoted 8 lines · Josef Bacik
fs/ioctl.c +8 -0
+	/*
+	 * we want the comparisons to be unsigned, in case somebody passes -1,
+	 * meaning they want they want the entire file, but the result has to be
+	 * signed so we can handle the case where we get more blocks than the
+	 * size of the file
+	 */
+	length = (long)min((unsigned long)fiemap_s->fm_length,
+			   (unsigned long)i_size_read(inode));

This might be written as:

Quoted 3 lines
	length = (long)min_t(unsigned long,fiemap_s->fm_len,i_size_read(inode));

Also, what about files that have blocks mapped after i_size?

That'll be tough for ext3, though I guess for a generic interface it could happen, so I guess it needs to be handled. Maybe check i_blocks against i_size, see if i_blocks indicates blocks past EOF? Hm, I guess that's not going to work in general; you could be completely sparse up to an EOF at 100G and have 100M of blocks past that...

I don't know how you'd handle this in the generic case...?

-Eric

#3 lore raw
Quoted 13 lines · Eric Sandeen
Quoted 12 lines · Andreas Dilger

fs/ioctl.c static int ioctl_fiemap(struct file *fil · line 155

Quoted 8 lines · Josef Bacik
fs/ioctl.c +8 -0
+	/*
+	 * we want the comparisons to be unsigned, in case somebody passes -1,
+	 * meaning they want they want the entire file, but the result has to be
+	 * signed so we can handle the case where we get more blocks than the
+	 * size of the file
+	 */
+	length = (long)min((unsigned long)fiemap_s->fm_length,
+			   (unsigned long)i_size_read(inode));

This might be written as:

Quoted 6 lines
Quoted 3 lines
	length = (long)min_t(unsigned long,fiemap_s->fm_len,i_size_read(inode));

Also, what about files that have blocks mapped after i_size?

That'll be tough for ext3, though I guess for a generic interface it could happen, so I guess it needs to be handled.

Right, because some filesystems may preallocate blocks beyond i_size to avoid fragmentation.

Quoted 4 lines

Maybe check i_blocks
against i_size, see if i_blocks indicates blocks past EOF? Hm, I guess that's not going to work in general; you could be completely sparse up to an EOF at 100G and have 100M of blocks past that...

...and there are also indirect blocks, and EA blocks that are not counted toward i_size. The issue is that getblock() doesn't have any way of reporting that it is beyond EOF. If it was an ext2/ext3-specific mechanism then it could check in the i_block[] array and in the end of the {t,d,}indirect blocks to know conclusively whether there are any blocks beyond EOF.

That said, I don't think the generic interface can know everything about each filesystem. My suggestion was that blocks beyond i_size continue to be mapped until a hole (block == 0) is returned. It isn't perfect, but would likely cover 99.9% of the cases where some small number of blocks (<= 64kB or whatever) were allocated beyond EOF to avoid fragmentation.

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

#4 lore raw
In reply to Andreas Dilger
Quoted 4 lines · Josef Bacik

Here is my reworked fiemap patch for ext3. The generic fiemap handler takes the get_block_t from the filesystem and does the fiemap that way, so then adding ext2 support should be a snap, as well as any other fs that wants to use this.

Ah, one other point is that this should also be used by ext4 to map inodes that are not in extents format, replacing the -EOPNOTSUPP return in that case.

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

#5 lore raw
Quoted 10 lines · Andreas Dilger
Quoted 4 lines · Josef Bacik

Here is my reworked fiemap patch for ext3. The generic fiemap handler takes the get_block_t from the filesystem and does the fiemap that way, so then adding ext2 support should be a snap, as well as any other fs that wants to use this.

Ah, one other point is that this should also be used by ext4 to map inodes that are not in extents format, replacing the -EOPNOTSUPP return in that case.

Sounds good, but I'm wondering if we should add a flag to the FIEMAP stuff to specify wether the inode you are mapping does truly have extents or if its just more of a block map. Do you think something like that would be usefull? Thank you,

Josef

#6 lore raw
Quoted 11 lines · Josef Bacik
Quoted 9 lines · Andreas Dilger
Quoted 4 lines · Josef Bacik

Here is my reworked fiemap patch for ext3. The generic fiemap handler takes the get_block_t from the filesystem and does the fiemap that way, so then adding ext2 support should be a snap, as well as any other fs that wants to use this.

Ah, one other point is that this should also be used by ext4 to map inodes that are not in extents format, replacing the -EOPNOTSUPP return in that case.

Quoted 6 lines

Sounds good, but I'm wondering if we should add a flag to the FIEMAP stuff to specify wether the inode you are mapping does truly have extents or if its just more of a block map. Do you think something like that would be usefull? Thank you,

Josef

I don't see that in itself as useful; you care about file layout, but why would you care about the filesystem's mechanism for tracking that layout?

-Eric

#7 lore raw
Quoted 17 lines · Eric Sandeen
Quoted 16 lines · Josef Bacik
Quoted 9 lines · Andreas Dilger
Quoted 4 lines · Josef Bacik

Here is my reworked fiemap patch for ext3. The generic fiemap handler takes the get_block_t from the filesystem and does the fiemap that way, so then adding ext2 support should be a snap, as well as any other fs that wants to use this.

Ah, one other point is that this should also be used by ext4 to map inodes that are not in extents format, replacing the -EOPNOTSUPP return in that case.

Sounds good, but I'm wondering if we should add a flag to the FIEMAP stuff to specify wether the inode you are mapping does truly have extents or if its just more of a block map. Do you think something like that would be usefull? Thank you,

Quoted 6 lines
Quoted 1 line

Josef

I don't see that in itself as useful; you care about file layout, but why would you care about the filesystem's mechanism for tracking that layout?

idk, just thought it may be usefull in the case of ext4 where you potentially have both formats. Mostly thinking out loud while we can still change things. Thanks,

Josef

#8 lore raw
In reply to Josef Bacik
Quoted 3 lines · Josef Bacik

I'm wondering if we should add a flag to the FIEMAP stuff to specify wether the inode you are mapping does truly have extents or if its just more of a block map. Do you think something like that would be usefull?

You can already do lsattr (EXT*_IOC_GETFLAGS) on a file to determine if it is using extents:

# lsattr /mnt/tmp/O/0/d0/459808 
-------------e /mnt/tmp/O/0/d0/459808

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