Skip to contentSkip to conversation

new flag COPY_FILE_RANGE_FILESIZE for copy_file_range()

PATCH In review Shawn Landden 2 participants 3 messages Apr 13, 2019 – Apr 14, 2019

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

PATCH 1/1 new flag COPY_FILE_RANGE_FILESIZE for copy_file_range()

If flags includes COPY_FILE_RANGE_FILESIZE then the length copied is the length of the file. off_in and off_out are ignored. len must be 0 or the file size.

This implementation saves a call to stat() in the common case of copying files. It does not fix any race conditions, but that is possible in the future with this interface.

EAGAIN: If COPY_FILE_RANGE_FILESIZE was passed and len is not 0 or the file size.

---
 fs/read_write.c           | 14 +++++++++++++-
 include/uapi/linux/stat.h |  4 ++++
 2 files changed, 17 insertions(+), 1 deletion(-)
fs/read_write.c +13 -1
index 61b43ad7608e..6d06361f0856 100644
@@ -1557,7 +1557,7 @@ ssize_t vfs_copy_file_range(struct file *file_in, loff_t pos_in,
 	struct inode *inode_out = file_inode(file_out);
 	ssize_t ret;
 
-	if (flags != 0)
+	if ((flags & ~COPY_FILE_RANGE_FILESIZE) != 0)
 		return -EINVAL;
 
 	if (S_ISDIR(inode_in->i_mode) || S_ISDIR(inode_out->i_mode))
@@ -1565,6 +1565,18 @@ ssize_t vfs_copy_file_range(struct file *file_in, loff_t pos_in,
 	if (!S_ISREG(inode_in->i_mode) || !S_ISREG(inode_out->i_mode))
 		return -EINVAL;
 
+	if (flags & COPY_FILE_RANGE_FILESIZE) {
+		struct kstat stat;
+		int error;
+		error = vfs_getattr(&file_in->f_path, &stat,
+				    STATX_SIZE, 0);
+		if (error < 0)
+			return error;
+		if (!(len == 0 || len == stat.size))
+			return -EAGAIN;
+		len = stat.size;
+	}
+
 	ret = rw_verify_area(READ, file_in, &pos_in, len);
 	if (unlikely(ret))
 		return ret;
include/uapi/linux/stat.h +4 -0
index 7b35e98d3c58..1075aa4666ef 100644
@@ -170,5 +170,9 @@ struct statx {
 
 #define STATX_ATTR_AUTOMOUNT		0x00001000 /* Dir: Automount trigger */
 
+/*
+ * Flags for copy_file_range()
+ */
+#define COPY_FILE_RANGE_FILESIZE	0x00000001 /* Copy the full length of the input file */
 
 #endif /* _UAPI_LINUX_STAT_H */
Signature · 1 line
2.20.1
#1 lore raw
Quoted 8 lines · Shawn Landden

If flags includes COPY_FILE_RANGE_FILESIZE then the length copied is the length of the file. off_in and off_out are ignored. len must be 0 or the file size.

Quoted 6 lines

This implementation saves a call to stat() in the common case of copying files. It does not fix any race conditions, but that is possible in the future with this interface.

EAGAIN: If COPY_FILE_RANGE_FILESIZE was passed and len is not 0 or the file size.

I think you’re asking for trouble here. I assume you have some kind of race prevention in mind here. The trouble is that passing zero means copy the whole thing, but the size-checking behavior is only available for nonzero sizes. This means that anyone who passes their idea of the size needs to account for inconsistent behavior if the size is zero.

Also, what happens if the file size changes mid copy? I assume the result is more or less arbitrary, but you should document what behavior is allowed. The docs should cover the case where you race against an O_APPEND write.

Quoted 57 lines

---
fs/read_write.c | 14 +++++++++++++-
include/uapi/linux/stat.h | 4 ++++

2 files changed, 17 insertions(+), 1 deletion(-)
fs/read_write.c +13 -1
index 61b43ad7608e..6d06361f0856 100644
@@ -1557,7 +1557,7 @@ ssize_t vfs_copy_file_range(struct file *file_in, loff_t pos_in,
   struct inode *inode_out = file_inode(file_out);
   ssize_t ret;
 
-    if (flags != 0)
+    if ((flags & ~COPY_FILE_RANGE_FILESIZE) != 0)
       return -EINVAL;
 
   if (S_ISDIR(inode_in->i_mode) || S_ISDIR(inode_out->i_mode))
@@ -1565,6 +1565,18 @@ ssize_t vfs_copy_file_range(struct file *file_in, loff_t pos_in,
   if (!S_ISREG(inode_in->i_mode) || !S_ISREG(inode_out->i_mode))
       return -EINVAL;
 
+    if (flags & COPY_FILE_RANGE_FILESIZE) {
+        struct kstat stat;
+        int error;
+        error = vfs_getattr(&file_in->f_path, &stat,
+                    STATX_SIZE, 0);
+        if (error < 0)
+            return error;
+        if (!(len == 0 || len == stat.size))
+            return -EAGAIN;
+        len = stat.size;
+    }
+
   ret = rw_verify_area(READ, file_in, &pos_in, len);
   if (unlikely(ret))
       return ret;
include/uapi/linux/stat.h +4 -0
index 7b35e98d3c58..1075aa4666ef 100644
@@ -170,5 +170,9 @@ struct statx {
 
 #define STATX_ATTR_AUTOMOUNT        0x00001000 /* Dir: Automount trigger */
 
+/*
+ * Flags for copy_file_range()
+ */
+#define COPY_FILE_RANGE_FILESIZE    0x00000001 /* Copy the full length of the input file */
 
 #endif /* _UAPI_LINUX_STAT_H */
Signature · 1 line
2.20.1
#2 lore raw

PATCH 1/1 new flag COPY_FILE_RANGE_FILESIZE for copy_file_range()

In reply to Shawn Landden

If flags includes COPY_FILE_RANGE_FILESIZE then the length copied is the length of the file. off_in and off_out are ignored. len must be 0.

This implementation saves a call to stat() in the common case of copying files. It does not fix any race conditions.

EINVAL: If COPY_FILE_RANGE_FILESIZE was passed and len is not 0.

v2: do not accept len == size.

---
 fs/read_write.c           | 14 +++++++++++++-
 include/uapi/linux/stat.h |  4 ++++
 2 files changed, 17 insertions(+), 1 deletion(-)
fs/read_write.c +13 -1
index 61b43ad7608e..f5862849bff7 100644
@@ -1557,7 +1557,7 @@ ssize_t vfs_copy_file_range(struct file *file_in, loff_t pos_in,
 	struct inode *inode_out = file_inode(file_out);
 	ssize_t ret;
 
-	if (flags != 0)
+	if ((flags & ~COPY_FILE_RANGE_FILESIZE) != 0)
 		return -EINVAL;
 
 	if (S_ISDIR(inode_in->i_mode) || S_ISDIR(inode_out->i_mode))
@@ -1565,6 +1565,18 @@ ssize_t vfs_copy_file_range(struct file *file_in, loff_t pos_in,
 	if (!S_ISREG(inode_in->i_mode) || !S_ISREG(inode_out->i_mode))
 		return -EINVAL;
 
+	if (flags & COPY_FILE_RANGE_FILESIZE) {
+		struct kstat stat;
+		int error;
+		if (len != 0)
+			return -EINVAL;
+		error = vfs_getattr(&file_in->f_path, &stat,
+				    STATX_SIZE, 0);
+		if (error < 0)
+			return error;
+		len = stat.size;
+	}
+
 	ret = rw_verify_area(READ, file_in, &pos_in, len);
 	if (unlikely(ret))
 		return ret;
include/uapi/linux/stat.h +4 -0
index 7b35e98d3c58..1075aa4666ef 100644
@@ -170,5 +170,9 @@ struct statx {
 
 #define STATX_ATTR_AUTOMOUNT		0x00001000 /* Dir: Automount trigger */
 
+/*
+ * Flags for copy_file_range()
+ */
+#define COPY_FILE_RANGE_FILESIZE	0x00000001 /* Copy the full length of the input file */
 
 #endif /* _UAPI_LINUX_STAT_H */
Signature · 1 line
2.20.1