Skip to contentSkip to conversation

Implement fallocate

PATCH In review David Chinner 3 participants 4 messages Oct 29, 2007 – Nov 1, 2007 XFS filesystem

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Implement fallocate

XFS fallocate() callout.

Allocate the range requested as unwritten extents. Atomically change the file size if requested.

---
 fs/xfs/linux-2.6/xfs_iops.c |   45 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)
fs/xfs/linux-2.6/xfs_iops.c +45 -0
Index: 2.6.x-xfs-new/fs/xfs/linux-2.6/xfs_iops.c
===================================================================
@@ -52,6 +52,7 @@
 #include <linux/xattr.h>
 #include <linux/namei.h>
 #include <linux/security.h>
+#include <linux/falloc.h>
 
 /*
  * Bring the atime in the XFS inode uptodate.
@@ -796,6 +797,49 @@ xfs_vn_removexattr(
 	return namesp->attr_remove(vp, attr, xflags);
 }
 
+/*
+ * generic space allocation vector.
+ */
+STATIC long
+xfs_vn_fallocate(
+	struct inode	*inode,
+	int		mode,
+	loff_t		offset,
+	loff_t		len)
+{
+	long		error;
+	loff_t		new_size = 0;
+	xfs_flock64_t	bf;
+
+	/* preallocation on directories not yet supported */
+	error = -ENODEV;
+	if (S_ISDIR(inode->i_mode))
+		goto out_error;
+
+	bf.l_whence = 0;
+	bf.l_start = offset;
+	bf.l_len = len;
+
+	xfs_ilock(XFS_I(inode), XFS_IOLOCK_EXCL);
+	error = xfs_change_file_space(XFS_I(inode), XFS_IOC_RESVSP, &bf,
+						0, NULL, ATTR_NOLOCK);
+	if (!error && !(mode & FALLOC_FL_KEEP_SIZE) &&
+	    offset + len > i_size_read(inode))
+		new_size = offset + len;
+
+	/* Change file size if needed */
+	if (new_size) {
+		bhv_vattr_t	va;
+
+		va.va_mask = XFS_AT_SIZE;
+		va.va_size = new_size;
+		error = xfs_setattr(XFS_I(inode), &va, ATTR_NOLOCK, NULL);
+	}
+
+	xfs_iunlock(XFS_I(inode), XFS_IOLOCK_EXCL);
+out_error:
+	return error;
+}
 
 const struct inode_operations xfs_inode_operations = {
 	.permission		= xfs_vn_permission,
@@ -806,6 +850,7 @@ const struct inode_operations xfs_inode_
 	.getxattr		= xfs_vn_getxattr,
 	.listxattr		= xfs_vn_listxattr,
 	.removexattr		= xfs_vn_removexattr,
+	.fallocate		= xfs_vn_fallocate,
 };
 
 const struct inode_operations xfs_dir_inode_operations = {
#1 lore raw
Quoted 3 lines · David Chinner

+/*
+ * generic space allocation vector.
+ */

This comment is rather non-sensical. But I think you can just kill it as inode operations don't really need comments.

Quoted 13 lines
fs/xfs/linux-2.6/xfs_iops.c +13 -0
+STATIC long
+xfs_vn_fallocate(
+	struct inode	*inode,
+	int		mode,
+	loff_t		offset,
+	loff_t		len)
+{
+	long		error;
+	loff_t		new_size = 0;
+	xfs_flock64_t	bf;
+
+	/* preallocation on directories not yet supported */
+	error = -ENODEV;

fs/xfs/linux-2.6/xfs_iops.c xfs_vn_removexattr( · line 825

Quoted 10 lines
fs/xfs/linux-2.6/xfs_iops.c +10 -0
+	if (S_ISDIR(inode->i_mode))
+		goto out_error;
+
+	bf.l_whence = 0;
+	bf.l_start = offset;
+	bf.l_len = len;
+
+	xfs_ilock(XFS_I(inode), XFS_IOLOCK_EXCL);
+	error = xfs_change_file_space(XFS_I(inode), XFS_IOC_RESVSP, &bf,
+						0, NULL, ATTR_NOLOCK);

please add a

	struct xfs_inode *ip = XFS_I(inode);

to the beginning of the function and use it subsequently. That'll make the fun ction a little more readable.

Otherwise this looks fine to me.

#2 lore raw
In reply to David Chinner
Quoted 54 lines · David Chinner

XFS fallocate() callout.

Allocate the range requested as unwritten extents. Atomically change the file size if requested.

---
 fs/xfs/linux-2.6/xfs_iops.c |   45 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)

Index: 2.6.x-xfs-new/fs/xfs/linux-2.6/xfs_iops.c
===================================================================

fs/xfs/linux-2.6/xfs_iops.c +29 -0
@@ -52,6 +52,7 @@
 #include <linux/xattr.h>
 #include <linux/namei.h>
 #include <linux/security.h>
+#include <linux/falloc.h>
 
 /*
  * Bring the atime in the XFS inode uptodate.
@@ -796,6 +797,49 @@ xfs_vn_removexattr(
 	return namesp->attr_remove(vp, attr, xflags);
 }
 
+/*
+ * generic space allocation vector.
+ */
+STATIC long
+xfs_vn_fallocate(
+	struct inode	*inode,
+	int		mode,
+	loff_t		offset,
+	loff_t		len)
+{
+	long		error;
+	loff_t		new_size = 0;
+	xfs_flock64_t	bf;
+
+	/* preallocation on directories not yet supported */
+	error = -ENODEV;
+	if (S_ISDIR(inode->i_mode))
+		goto out_error;
+
+	bf.l_whence = 0;
+	bf.l_start = offset;
+	bf.l_len = len;
+
+	xfs_ilock(XFS_I(inode), XFS_IOLOCK_EXCL);
+	error = xfs_change_file_space(XFS_I(inode), XFS_IOC_RESVSP, &bf,
+						0, NULL, ATTR_NOLOCK);
+	if (!error && !(mode & FALLOC_FL_KEEP_SIZE) &&
+	    offset + len > i_size_read(inode))

fs/xfs/linux-2.6/xfs_iops.c xfs_vn_removexattr( · line 837

Quoted 10 lines
fs/xfs/linux-2.6/xfs_iops.c +10 -0
+		new_size = offset + len;
+
+	/* Change file size if needed */
+	if (new_size) {
+		bhv_vattr_t	va;
+
+		va.va_mask = XFS_AT_SIZE;
+		va.va_size = new_size;
+		error = xfs_setattr(XFS_I(inode), &va, ATTR_NOLOCK, NULL);
+	}

Is it necessary to call xfs_setattr() here? Could we just do an explicit call to xfs_zero_eof(), set the new size, set i_update_core/size and mark the inode dirty? Hmmm, then again, that approach wouldn't be as clean as above.

Quoted 17 lines
fs/xfs/linux-2.6/xfs_iops.c +6 -0
+
+	xfs_iunlock(XFS_I(inode), XFS_IOLOCK_EXCL);
+out_error:
+	return error;
+}
 
 const struct inode_operations xfs_inode_operations = {
 	.permission		= xfs_vn_permission,
@@ -806,6 +850,7 @@ const struct inode_operations xfs_inode_
 	.getxattr		= xfs_vn_getxattr,
 	.listxattr		= xfs_vn_listxattr,
 	.removexattr		= xfs_vn_removexattr,
+	.fallocate		= xfs_vn_fallocate,
 };
 
 const struct inode_operations xfs_dir_inode_operations = {
#3 lore raw
Quoted 12 lines · Lachlan McIlroy
Quoted 12 lines
fs/xfs/linux-2.6/xfs_iops.c +12 -0
+	xfs_ilock(XFS_I(inode), XFS_IOLOCK_EXCL);
+	error = xfs_change_file_space(XFS_I(inode), XFS_IOC_RESVSP, &bf,
+						0, NULL, ATTR_NOLOCK);
+	if (!error && !(mode & FALLOC_FL_KEEP_SIZE) &&
+	    offset + len > i_size_read(inode))
+		new_size = offset + len;
+
+	/* Change file size if needed */
+	if (new_size) {
+		bhv_vattr_t	va;
+
+		va.va_mask = XFS_AT_SIZE;
Quoted 8 lines

fs/xfs/linux-2.6/xfs_iops.c xfs_vn_removexattr( · line 837

Quoted 3 lines
fs/xfs/linux-2.6/xfs_iops.c +3 -0
+		va.va_size = new_size;
+		error = xfs_setattr(XFS_I(inode), &va, ATTR_NOLOCK, NULL);
+	}

Is it necessary to call xfs_setattr() here? Could we just do an explicit call to xfs_zero_eof(), set the new size, set i_update_core/size and mark the inode dirty? Hmmm, then again, that approach wouldn't be as clean as above.

And it also violates the atomicity that posix_fallocate is supposed to provide. i.e. if it returns success, the change of file size must be permanent. i.e. the change of size needs to be recorded in a transaction. Hence we need to call xfs_setattr....

Cheers,

Dave.

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