Skip to contentSkip to conversation

FIEMAP ioctl for ext4

PATCH In review Kalpak Shah 2 participants 2 messages Nov 12, 2007 – Nov 13, 2007

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

PATCH 2/2 FIEMAP ioctl for ext4

Recently there was discussion about an "FIle Extent MAP"(FIEMAP) ioctl for efficiently mapping the extents and holes of a file. This will be many times more efficient than FIBMAP by cutting down the number of ioctls.

This patch adds the FIEMAP ioctl for ext4. The spec for the FIEMAP ioctl was posted earlier by Andreas Dilger and can be found at:
http://www.mail-archive.com/[email protected]/msg03944.html

fs/ext4/ioctl.c +4 -0
Index: linux-2.6.23.1/fs/ext4/ioctl.c
===================================================================
@@ -16,6 +16,7 @@
 #include <linux/compat.h>
 #include <linux/smp_lock.h>
 #include <asm/uaccess.h>
+#include <linux/fiemap.h>
 
 int ext4_ioctl (struct inode * inode, struct file * filp, unsigned int cmd,
 		unsigned long arg)
@@ -248,6 +249,9 @@ flags_err:
 
 		return err;
 	}
+	case EXT4_IOC_FIEMAP: {
+		return ext4_fiemap(inode, filp, cmd, arg);
+	}
 
 	default:
 		return -ENOTTY;
include/linux/ext4_fs.h +9 -2
Index: linux-2.6.23.1/include/linux/ext4_fs.h
===================================================================
@@ -228,15 +228,20 @@ struct ext4_new_group_data {
 #define	EXT4_IOC_SETFLAGS		FS_IOC_SETFLAGS
 #define	EXT4_IOC_GETVERSION		_IOR('f', 3, long)
 #define	EXT4_IOC_SETVERSION		_IOW('f', 4, long)
+#define EXT4_IOC_GETRSVSZ		_IOR('f', 5, long)
+#define EXT4_IOC_SETRSVSZ		_IOW('f', 6, long)
 #define EXT4_IOC_GROUP_EXTEND		_IOW('f', 7, unsigned long)
 #define EXT4_IOC_GROUP_ADD		_IOW('f', 8,struct ext4_new_group_input)
+#define EXT4_IOC_FIEMAP			_IOWR('f', 10, struct fiemap)
 #define	EXT4_IOC_GETVERSION_OLD		FS_IOC_GETVERSION
 #define	EXT4_IOC_SETVERSION_OLD		FS_IOC_SETVERSION
 #ifdef CONFIG_JBD2_DEBUG
 #define EXT4_IOC_WAIT_FOR_READONLY	_IOR('f', 99, long)
 #endif
-#define EXT4_IOC_GETRSVSZ		_IOR('f', 5, long)
-#define EXT4_IOC_SETRSVSZ		_IOW('f', 6, long)
+
+/* ext4 only handles a single LUN, ignore LUN_OFFSET flag */
+#define EXT4_FIEMAP_FLAG_INCOMPAT_UNSUPP (FIEMAP_FLAG_INCOMPAT &	\
+					  ~(FIEMAP_FLAG_LUN_OFFSET))
 
 /*
  * ioctl commands in 32 bit emulation
@@ -1067,6 +1072,8 @@ ext4_get_blocks_wrap(handle_t *handle, s
 	return ext4_get_blocks_handle(handle, inode, block, max_blocks, bh,
 					create, extend_disksize);
 }
+extern int ext4_fiemap(struct inode *inode, struct file *filp, unsigned int cmd,
+		       unsigned long arg);
 
 
 #endif	/* __KERNEL__ */
include/linux/ext4_fs_extents.h +2 -2
Index: linux-2.6.23.1/include/linux/ext4_fs_extents.h
===================================================================
@@ -131,8 +131,8 @@ struct ext4_ext_path {
  * callback must return valid extent (passed or newly created)
  */
 typedef int (*ext_prepare_callback)(struct inode *, struct ext4_ext_path *,
-					struct ext4_ext_cache *,
-					void *);
+				    struct ext4_ext_cache *,
+				    struct ext4_extent *, void *);
 
 #define EXT_CONTINUE	0
 #define EXT_BREAK	1
fs/ext4/extents.c +162 -1

178 lines not on this page · raw message

#1 lore raw
Quoted 4 lines · Kalpak Shah

Recently there was discussion about an "FIle Extent MAP"(FIEMAP) ioctl for efficiently mapping the extents and holes of a file. This will be many times more efficient than FIBMAP by cutting down the number of ioctls.

This patch adds the FIEMAP ioctl for ext4. The spec for the FIEMAP ioctl was posted earlier by Andreas Dilger and can be found at:
http://www.mail-archive.com/[email protected]/msg03944.html

....

Quoted 12 lines
fs/ext4/ioctl.c +3 -0
 	}
+	case EXT4_IOC_FIEMAP: {
+		return ext4_fiemap(inode, filp, cmd, arg);
+	}
 
 	default:
 		return -ENOTTY;

Index: linux-2.6.23.1/include/linux/ext4_fs.h
===================================================================

include/linux/ext4_fs.h struct ext4_new_group_data { · line 235

Quoted 8 lines
include/linux/ext4_fs.h +3 -0
@@ -228,15 +228,20 @@ struct ext4_new_group_data {
 #define	EXT4_IOC_SETFLAGS		FS_IOC_SETFLAGS
 #define	EXT4_IOC_GETVERSION		_IOR('f', 3, long)
 #define	EXT4_IOC_SETVERSION		_IOW('f', 4, long)
+#define EXT4_IOC_GETRSVSZ		_IOR('f', 5, long)
+#define EXT4_IOC_SETRSVSZ		_IOW('f', 6, long)
 #define EXT4_IOC_GROUP_EXTEND		_IOW('f', 7, unsigned long)
 #define EXT4_IOC_GROUP_ADD		_IOW('f', 8,struct ext4_new_group_input)
+#define EXT4_IOC_FIEMAP			_IOWR('f', 10, struct fiemap)

Please make this common - we dont want a new ioctl for every filesystem; we want a single common to all filesystems.

include/linux/ext4_fs.h struct ext4_new_group_data { · line 238

Quoted 3 lines
include/linux/ext4_fs.h +3 -0
+int ext4_fiemap(struct inode *inode, struct file *filp, unsigned int cmd,
+		unsigned long arg)
+{

Most of this function will be common to all IOC_FIEMAP implementations.

include/linux/ext4_fs.h struct ext4_new_group_data { · line 247

Quoted 9 lines
include/linux/ext4_fs.h +9 -0
+	struct fiemap *fiemap_s;
+	struct fiemap_internal fiemap_i;
+	struct fiemap_extent *last_extent;
+	ext4_fsblk_t start_blk;
+	int fm_extent_size = sizeof(struct fiemap_extent);
+	int err = 0;
+
+	if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL))
+		return -EOPNOTSUPP;
	struct address_space *mapping = filp->f_mapping;

	if (!mapping->a_ops->fiemap)
		return -EOPNOTSUPP;

include/linux/ext4_fs.h struct ext4_new_group_data {

Quoted 7 lines
include/linux/ext4_fs.h +7 -0
+
+	fiemap_s = kmalloc(sizeof(*fiemap_s), GFP_KERNEL);
+	if (fiemap_s == NULL)
+		return -ENOMEM;
+	if (copy_from_user(fiemap_s, (struct fiemap __user *)arg,
+			   sizeof(*fiemap_s)))
+		return -EFAULT;

This is common

include/linux/ext4_fs.h

Quoted 6 lines
include/linux/ext4_fs.h +6 -0
+
+	if (fiemap_s->fm_flags & EXT4_FIEMAP_FLAG_INCOMPAT_UNSUPP)
+		return -EOPNOTSUPP;
+
+	if (fiemap_s->fm_flags & FIEMAP_FLAG_SYNC)
+		ext4_sync_file(filp, filp->f_dentry, 1);

The common form is:

	if (fiemap_s->fm_flags & FIEMAP_FLAG_SYNC)
		filemap_write_and_wait(mapping);

include/linux/ext4_fs.h

Quoted 6 lines
include/linux/ext4_fs.h +6 -0
+	start_blk = fiemap_s->fm_start >> inode->i_sb->s_blocksize_bits;
+	fiemap_i.fiemap_s = fiemap_s;
+	fiemap_i.tot_mapping_len = 0;
+	fiemap_i.cur_ext_ptr = (char *)(arg + sizeof(*fiemap_s));
+	fiemap_i.current_extent = 0;
+	fiemap_i.err = 0;

Seems common.

include/linux/ext4_fs.h

Quoted 8 lines
include/linux/ext4_fs.h +8 -0
+
+	/*
+	 * Walk the extent tree gathering extent information
+	 */
+	mutex_lock(&EXT4_I(inode)->truncate_mutex);
+	err = ext4_ext_walk_space(inode, start_blk , EXT_MAX_BLOCK - start_blk,
+				  ext4_ext_fiemap_cb, &fiemap_i);
+	mutex_unlock(&EXT4_I(inode)->truncate_mutex);

This becomes:

	error = mapping->a_ops->fiemap(inode, ....);

and the lock, extent walk, etc becomes ext4_fiemap() which is set up in the a_ops for the filesystem. Any filesystems specific checks go there as well.

Quoted 14 lines
include/linux/ext4_fs.h +14 -0
+	if (err)
+		return err;
+
+	fiemap_s->fm_extent_count = fiemap_i.current_extent;
+	fiemap_s->fm_length = fiemap_i.tot_mapping_len;
+	/*
+	 * Mark last extent as EXTENT_LAST and copy the extent to userspace.
+	 */
+	if (fiemap_i.current_extent != 0 &&
+	    fiemap_i.current_extent < fiemap_s->fm_extent_count &&
+	    !(fiemap_s->fm_flags & FIEMAP_FLAG_NUM_EXTENTS)) {
+		char *dest;
+
+		last_extent = &fiemap_i.fm_extent;

include/linux/ext4_fs.h

Quoted 10 lines
include/linux/ext4_fs.h +10 -0
+		last_extent->fe_flags |= FIEMAP_EXTENT_LAST;
+		dest = (char *)arg + sizeof(*fiemap_s) + fm_extent_size *
+						(fiemap_s->fm_extent_count - 1);
+		err = copy_to_user(dest, last_extent, fm_extent_size);
+		if (err)
+			return err;
+	}
+	err = copy_to_user((void *)arg, fiemap_s, sizeof(*fiemap_s));
+
+	return err;

That's common, too.

I don't want to see this implemented over and over again with minute variations and bugs. The common implementation should be called from in do_file_ioctl() like FIBMAP....

Cheers,

Dave.

Signature · 3 lines
Dave Chinner
Principal Engineer
SGI Australian Software Group