Skip to contentSkip to conversation

VFS: In-kernel copy system call

PATCH Superseded Anna Schumaker 18 participants 59 messages Sep 4, 2015 – Sep 14, 2015 24 messages not in the archive

Subsystems (8)
x86 VFS Btrfs file system NFS client +4 more subsystems

v1 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

cover VFS: In-kernel copy system call

Copy system calls came up during Plumbers a couple of weeks ago, because several filesystems (including NFS and XFS) are currently working on copy acceleration implementations. We haven't heard from Zach Brown in a while, so I volunteered to push his patches upstream so individual filesystems don't need to keep writing their own ioctls.

The first three patches are a simple reposting of Zach's patches from several months ago, with one minor error code fix. The remaining patches add in a fallback mechanism when filesystems don't provide a copy function. This is especially useful when doing a server-side copy on NFS (using the new COPY operation in NFS v4.2). This fallback can be disabled by passing the flag COPY_REFLINK to the system call.

The last patch is a man page patch documenting this new system call, including an example program.

I tested the fallback option by using /dev/urandom to generate files of varying sizes and copying them. I compared the time to copy against that of cp just to see if there is a noticable difference. I found that runtimes are roughly the same, but in-kernel copy tends to use less of the cpu. Values in the tables below are averages across multiple trials.

 /usr/bin/cp |   512 MB  |   1024 MB |   1536 MB |   2048 MB
-------------|-----------|-----------|-----------|-----------
       user  |   0.00s   |   0.00s   |   0.00s   |   0.00s
     system  |   0.32s   |   0.52s   |   1.04s   |   1.04s
        cpu  |     73%   |     69%   |     62%   |     62%
      total  |   0.446   |   0.757   |   1.197   |   1.667
   VFS copy  |   512 MB  |   1024 MB |   1536 MB |   2048 MB
-------------|-----------|-----------|-----------|-----------
       user  |   0.00s   |   0.00s   |   0.00s   |  0.00s
     system  |   0.33s   |   0.49s   |   0.76s   |  0.99s
        cpu  |     77%   |     62%   |     60%   |    59%
      total  |   0.422   |   0.777   |   1.267   |  1.655

Questions? Comments? Thoughts?

Anna

Anna Schumaker (5):
btrfs: Add mountpoint checking during btrfs_copy_file_range
vfs: Remove copy_file_range mountpoint checks
vfs: Copy should check len after file open mode
vfs: Copy should use file_out rather than file_in
vfs: Fall back on splice if no copy function defined

Zach Brown (3):
vfs: add copy_file_range syscall and vfs helper
x86: add sys_copy_file_range to syscall tables
btrfs: add .copy_file_range file operation

 arch/x86/entry/syscalls/syscall_32.tbl |   1 +
 arch/x86/entry/syscalls/syscall_64.tbl |   1 +
 fs/btrfs/ctree.h                       |   3 +
 fs/btrfs/file.c                        |   1 +
 fs/btrfs/ioctl.c                       |  95 ++++++++++++++----------
 fs/read_write.c                        | 132 +++++++++++++++++++++++++++++++++
 include/linux/copy.h                   |   6 ++
 include/linux/fs.h                     |   3 +
 include/uapi/asm-generic/unistd.h      |   4 +-
 include/uapi/linux/Kbuild              |   1 +
 include/uapi/linux/copy.h              |   6 ++
 kernel/sys_ni.c                        |   1 +
 12 files changed, 214 insertions(+), 40 deletions(-)
 create mode 100644 include/linux/copy.h
 create mode 100644 include/uapi/linux/copy.h
Signature · 6 lines
2.5.1

--
To unsubscribe from this list: send the line "unsubscribe linux-nfs" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): 1441397823-1203-1-git-send-email-Anna.Schumaker-ZwjVKphTwtPQT0dZR+AlfA@public.gmane.org

#2 lore raw

PATCH 1/9 vfs: add copy_file_range syscall and vfs helper

From: Zach Brown <[email protected]>

Add a copy_file_range() system call for offloading copies between regular files.

This gives an interface to underlying layers of the storage stack which can copy without reading and writing all the data. There are a few candidates that should support copy offloading in the nearer term:

- btrfs shares extent references with its clone ioctl
- NFS has patches to add a COPY command which copies on the server
- SCSI has a family of XCOPY commands which copy in the device

This system call avoids the complexity of also accelerating the creation of the destination file by operating on an existing destination file descriptor, not a path.

Currently the high level vfs entry point limits copy offloading to files on the same mount and super (and not in the same file). This can be relaxed if we get implementations which can copy between file systems safely.

[Anna Schumaker: Change -EINVAL to -EBADF during file verification]
---
 fs/read_write.c                   | 129 ++++++++++++++++++++++++++++++++++++++
 include/linux/fs.h                |   3 +
 include/uapi/asm-generic/unistd.h |   4 +-
 kernel/sys_ni.c                   |   1 +
 4 files changed, 136 insertions(+), 1 deletion(-)
fs/read_write.c +129 -0
index 819ef3f..82c4933 100644
@@ -16,6 +16,7 @@
 #include <linux/pagemap.h>
 #include <linux/splice.h>
 #include <linux/compat.h>
+#include <linux/mount.h>
 #include "internal.h"
 
 #include <asm/uaccess.h>
@@ -1327,3 +1328,131 @@ COMPAT_SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd,
 	return do_sendfile(out_fd, in_fd, NULL, count, 0);
 }
 #endif
+
+/*
+ * copy_file_range() differs from regular file read and write in that it
+ * specifically allows return partial success.  When it does so is up to
+ * the copy_file_range method.
+ */
+ssize_t vfs_copy_file_range(struct file *file_in, loff_t pos_in,
+			    struct file *file_out, loff_t pos_out,
+			    size_t len, int flags)
+{
+	struct inode *inode_in;
+	struct inode *inode_out;
+	ssize_t ret;
+
+	if (flags)
+		return -EINVAL;
+
+	if (len == 0)
+		return 0;
+
+	/* copy_file_range allows full ssize_t len, ignoring MAX_RW_COUNT  */
+	ret = rw_verify_area(READ, file_in, &pos_in, len);
+	if (ret >= 0)
+		ret = rw_verify_area(WRITE, file_out, &pos_out, len);
+	if (ret < 0)
+		return ret;
+
+	if (!(file_in->f_mode & FMODE_READ) ||
+	    !(file_out->f_mode & FMODE_WRITE) ||
+	    (file_out->f_flags & O_APPEND) ||
+	    !file_in->f_op || !file_in->f_op->copy_file_range)
+		return -EBADF;
+
+	inode_in = file_inode(file_in);
+	inode_out = file_inode(file_out);
+
+	/* make sure offsets don't wrap and the input is inside i_size */
+	if (pos_in + len < pos_in || pos_out + len < pos_out ||
+	    pos_in + len > i_size_read(inode_in))
+		return -EINVAL;
+
+	/* this could be relaxed once a method supports cross-fs copies */
+	if (inode_in->i_sb != inode_out->i_sb ||
+	    file_in->f_path.mnt != file_out->f_path.mnt)
+		return -EXDEV;
+
+	/* forbid ranges in the same file */
+	if (inode_in == inode_out)
+		return -EINVAL;
+
+	ret = mnt_want_write_file(file_out);
+	if (ret)
+		return ret;
+
+	ret = file_in->f_op->copy_file_range(file_in, pos_in, file_out, pos_out,
+					     len, flags);
+	if (ret > 0) {
+		fsnotify_access(file_in);
+		add_rchar(current, ret);
+		fsnotify_modify(file_out);
+		add_wchar(current, ret);
+	}
+	inc_syscr(current);
+	inc_syscw(current);
+
+	mnt_drop_write_file(file_out);
+
+	return ret;
+}
+EXPORT_SYMBOL(vfs_copy_file_range);
+
+SYSCALL_DEFINE6(copy_file_range, int, fd_in, loff_t __user *, off_in,
+		int, fd_out, loff_t __user *, off_out,
+		size_t, len, unsigned int, flags)
+{
+	loff_t pos_in;
+	loff_t pos_out;
+	struct fd f_in;
+	struct fd f_out;
+	ssize_t ret;
+
+	f_in = fdget(fd_in);
+	f_out = fdget(fd_out);
+	if (!f_in.file || !f_out.file) {
+		ret = -EBADF;
+		goto out;
+	}
+
+	ret = -EFAULT;
+	if (off_in) {
+		if (copy_from_user(&pos_in, off_in, sizeof(loff_t)))
+			goto out;
+	} else {
+		pos_in = f_in.file->f_pos;
+	}
+
+	if (off_out) {
+		if (copy_from_user(&pos_out, off_out, sizeof(loff_t)))
+			goto out;
+	} else {
+		pos_out = f_out.file->f_pos;
+	}
+
+	ret = vfs_copy_file_range(f_in.file, pos_in, f_out.file, pos_out, len,
+				  flags);
+	if (ret > 0) {
+		pos_in += ret;
+		pos_out += ret;
+
+		if (off_in) {
+			if (copy_to_user(off_in, &pos_in, sizeof(loff_t)))
+				ret = -EFAULT;
+		} else {
+			f_in.file->f_pos = pos_in;
+		}
+
+		if (off_out) {
+			if (copy_to_user(off_out, &pos_out, sizeof(loff_t)))
+				ret = -EFAULT;
+		} else {
+			f_out.file->f_pos = pos_out;
+		}
+	}
+out:
+	fdput(f_in);
+	fdput(f_out);
+	return ret;
+}
include/linux/fs.h +3 -0
index cc008c3..c97aed8 100644
@@ -1631,6 +1631,7 @@ struct file_operations {
 #ifndef CONFIG_MMU
 	unsigned (*mmap_capabilities)(struct file *);
 #endif
+	ssize_t (*copy_file_range)(struct file *, loff_t, struct file *, loff_t, size_t, int);
 };
 
 struct inode_operations {
@@ -1684,6 +1685,8 @@ extern ssize_t vfs_readv(struct file *, const struct iovec __user *,
 		unsigned long, loff_t *);
 extern ssize_t vfs_writev(struct file *, const struct iovec __user *,
 		unsigned long, loff_t *);
+extern ssize_t vfs_copy_file_range(struct file *, loff_t , struct file *,
+				   loff_t, size_t, int);
 
 struct super_operations {
    	struct inode *(*alloc_inode)(struct super_block *sb);
include/uapi/asm-generic/unistd.h +3 -1
index e016bd9..2b60f0c 100644
@@ -709,9 +709,11 @@ __SYSCALL(__NR_memfd_create, sys_memfd_create)
 __SYSCALL(__NR_bpf, sys_bpf)
 #define __NR_execveat 281
 __SC_COMP(__NR_execveat, sys_execveat, compat_sys_execveat)
+#define __NR_copy_file_range 282
+__SYSCALL(__NR_copy_file_range, sys_copy_file_range)
 
 #undef __NR_syscalls
-#define __NR_syscalls 282
+#define __NR_syscalls 283
 
 /*
  * All syscalls below here should go away really,
kernel/sys_ni.c +1 -0
index 7995ef5..4e01cd9 100644
@@ -173,6 +173,7 @@ cond_syscall(sys_setfsuid);
 cond_syscall(sys_setfsgid);
 cond_syscall(sys_capget);
 cond_syscall(sys_capset);
+cond_syscall(sys_copy_file_range);
 
 /* arch-specific weak syscall entries */
 cond_syscall(sys_pciconfig_read);
Signature · 1 line
2.5.1
#3 lore raw

Re: [PATCH v1 1/9] vfs: add copy_file_range syscall and vfs helper

Quoted 87 lines · Anna Schumaker

From: Zach Brown <[email protected]>

Add a copy_file_range() system call for offloading copies between regular files.

This gives an interface to underlying layers of the storage stack which can copy without reading and writing all the data. There are a few candidates that should support copy offloading in the nearer term:

- btrfs shares extent references with its clone ioctl
- NFS has patches to add a COPY command which copies on the server
- SCSI has a family of XCOPY commands which copy in the device

This system call avoids the complexity of also accelerating the creation of the destination file by operating on an existing destination file descriptor, not a path.

Currently the high level vfs entry point limits copy offloading to files on the same mount and super (and not in the same file). This can be relaxed if we get implementations which can copy between file systems safely.

[Anna Schumaker: Change -EINVAL to -EBADF during file verification]
---
 fs/read_write.c                   | 129 ++++++++++++++++++++++++++++++++++++++
 include/linux/fs.h                |   3 +
 include/uapi/asm-generic/unistd.h |   4 +-
 kernel/sys_ni.c                   |   1 +
 4 files changed, 136 insertions(+), 1 deletion(-)
fs/read_write.c +40 -0
index 819ef3f..82c4933 100644
@@ -16,6 +16,7 @@
 #include <linux/pagemap.h>
 #include <linux/splice.h>
 #include <linux/compat.h>
+#include <linux/mount.h>
 #include "internal.h"
 
 #include <asm/uaccess.h>
@@ -1327,3 +1328,131 @@ COMPAT_SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd,
 	return do_sendfile(out_fd, in_fd, NULL, count, 0);
 }
 #endif
+
+/*
+ * copy_file_range() differs from regular file read and write in that it
+ * specifically allows return partial success.  When it does so is up to
+ * the copy_file_range method.
+ */
+ssize_t vfs_copy_file_range(struct file *file_in, loff_t pos_in,
+			    struct file *file_out, loff_t pos_out,
+			    size_t len, int flags)
+{
+	struct inode *inode_in;
+	struct inode *inode_out;
+	ssize_t ret;
+
+	if (flags)
+		return -EINVAL;
+
+	if (len == 0)
+		return 0;
+
+	/* copy_file_range allows full ssize_t len, ignoring MAX_RW_COUNT  */
+	ret = rw_verify_area(READ, file_in, &pos_in, len);
+	if (ret >= 0)
+		ret = rw_verify_area(WRITE, file_out, &pos_out, len);
+	if (ret < 0)
+		return ret;
+
+	if (!(file_in->f_mode & FMODE_READ) ||
+	    !(file_out->f_mode & FMODE_WRITE) ||
+	    (file_out->f_flags & O_APPEND) ||
+	    !file_in->f_op || !file_in->f_op->copy_file_range)
+		return -EBADF;
+
+	inode_in = file_inode(file_in);
+	inode_out = file_inode(file_out);
+
+	/* make sure offsets don't wrap and the input is inside i_size */
+	if (pos_in + len < pos_in || pos_out + len < pos_out ||
+	    pos_in + len > i_size_read(inode_in))

fs/read_write.c COMPAT_SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd, · line 1379

Quoted 10 lines
fs/read_write.c +10 -0
+		return -EINVAL;
+
+	/* this could be relaxed once a method supports cross-fs copies */
+	if (inode_in->i_sb != inode_out->i_sb ||
+	    file_in->f_path.mnt != file_out->f_path.mnt)
+		return -EXDEV;
+
+	/* forbid ranges in the same file */
+	if (inode_in == inode_out)
+		return -EINVAL;

btrfs does and XFS will support the case of a file sharing blocks with itself.

--D

Quoted 136 lines
fs/read_write.c +79 -0
+
+	ret = mnt_want_write_file(file_out);
+	if (ret)
+		return ret;
+
+	ret = file_in->f_op->copy_file_range(file_in, pos_in, file_out, pos_out,
+					     len, flags);
+	if (ret > 0) {
+		fsnotify_access(file_in);
+		add_rchar(current, ret);
+		fsnotify_modify(file_out);
+		add_wchar(current, ret);
+	}
+	inc_syscr(current);
+	inc_syscw(current);
+
+	mnt_drop_write_file(file_out);
+
+	return ret;
+}
+EXPORT_SYMBOL(vfs_copy_file_range);
+
+SYSCALL_DEFINE6(copy_file_range, int, fd_in, loff_t __user *, off_in,
+		int, fd_out, loff_t __user *, off_out,
+		size_t, len, unsigned int, flags)
+{
+	loff_t pos_in;
+	loff_t pos_out;
+	struct fd f_in;
+	struct fd f_out;
+	ssize_t ret;
+
+	f_in = fdget(fd_in);
+	f_out = fdget(fd_out);
+	if (!f_in.file || !f_out.file) {
+		ret = -EBADF;
+		goto out;
+	}
+
+	ret = -EFAULT;
+	if (off_in) {
+		if (copy_from_user(&pos_in, off_in, sizeof(loff_t)))
+			goto out;
+	} else {
+		pos_in = f_in.file->f_pos;
+	}
+
+	if (off_out) {
+		if (copy_from_user(&pos_out, off_out, sizeof(loff_t)))
+			goto out;
+	} else {
+		pos_out = f_out.file->f_pos;
+	}
+
+	ret = vfs_copy_file_range(f_in.file, pos_in, f_out.file, pos_out, len,
+				  flags);
+	if (ret > 0) {
+		pos_in += ret;
+		pos_out += ret;
+
+		if (off_in) {
+			if (copy_to_user(off_in, &pos_in, sizeof(loff_t)))
+				ret = -EFAULT;
+		} else {
+			f_in.file->f_pos = pos_in;
+		}
+
+		if (off_out) {
+			if (copy_to_user(off_out, &pos_out, sizeof(loff_t)))
+				ret = -EFAULT;
+		} else {
+			f_out.file->f_pos = pos_out;
+		}
+	}
+out:
+	fdput(f_in);
+	fdput(f_out);
+	return ret;
+}
include/linux/fs.h +3 -0
index cc008c3..c97aed8 100644
@@ -1631,6 +1631,7 @@ struct file_operations {
 #ifndef CONFIG_MMU
 	unsigned (*mmap_capabilities)(struct file *);
 #endif
+	ssize_t (*copy_file_range)(struct file *, loff_t, struct file *, loff_t, size_t, int);
 };
 
 struct inode_operations {
@@ -1684,6 +1685,8 @@ extern ssize_t vfs_readv(struct file *, const struct iovec __user *,
 		unsigned long, loff_t *);
 extern ssize_t vfs_writev(struct file *, const struct iovec __user *,
 		unsigned long, loff_t *);
+extern ssize_t vfs_copy_file_range(struct file *, loff_t , struct file *,
+				   loff_t, size_t, int);
 
 struct super_operations {
    	struct inode *(*alloc_inode)(struct super_block *sb);
include/uapi/asm-generic/unistd.h +3 -1
index e016bd9..2b60f0c 100644
@@ -709,9 +709,11 @@ __SYSCALL(__NR_memfd_create, sys_memfd_create)
 __SYSCALL(__NR_bpf, sys_bpf)
 #define __NR_execveat 281
 __SC_COMP(__NR_execveat, sys_execveat, compat_sys_execveat)
+#define __NR_copy_file_range 282
+__SYSCALL(__NR_copy_file_range, sys_copy_file_range)
 
 #undef __NR_syscalls
-#define __NR_syscalls 282
+#define __NR_syscalls 283
 
 /*
  * All syscalls below here should go away really,
kernel/sys_ni.c +1 -0
index 7995ef5..4e01cd9 100644
@@ -173,6 +173,7 @@ cond_syscall(sys_setfsuid);
 cond_syscall(sys_setfsgid);
 cond_syscall(sys_capget);
 cond_syscall(sys_capset);
+cond_syscall(sys_copy_file_range);
 
 /* arch-specific weak syscall entries */
 cond_syscall(sys_pciconfig_read);
Signature · 6 lines
2.5.1

--
To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#4 lore raw

PATCH 4/8 btrfs: Add mountpoint checking during btrfs_copy_file_range

In reply to a message not in the archive

We need to verify that both the source and the destination files are part of the same filesystem, otherwise we can't create a reflink.

---
 fs/btrfs/ioctl.c | 4 ++++
 1 file changed, 4 insertions(+)
fs/btrfs/ioctl.c +4 -0
index 62ae286..9c0c955 100644
@@ -3856,6 +3856,10 @@ ssize_t btrfs_copy_file_range(struct file *file_in, loff_t pos_in,
 {
 	ssize_t ret;
 
+	if (inode_in->i_sb != inode_out->i_sb ||
+	    file_in->f_path.mnt != file_out->f_path.mnt)
+		return -EXDEV;
+
 	ret = btrfs_clone_files(file_out, file_in, pos_in, len, pos_out);
 	if (ret == 0)
 		ret = len;
Signature · 6 lines
2.5.1

--
To unsubscribe from this list: send the line "unsubscribe linux-nfs" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#5 lore raw

Re: [PATCH v1 4/8] btrfs: Add mountpoint checking during btrfs_copy_file_range

Quoted 13 lines · Anna Schumaker

We need to verify that both the source and the destination files are part of the same filesystem, otherwise we can't create a reflink.

---
 fs/btrfs/ioctl.c | 4 ++++
 1 file changed, 4 insertions(+)
fs/btrfs/ioctl.c +0 -0
index 62ae286..9c0c955 100644

fs/btrfs/ioctl.c ssize_t btrfs_copy_file_range(struct file *file_in, loff_t pos_in, · line 3861

Quoted 6 lines
fs/btrfs/ioctl.c +3 -0
@@ -3856,6 +3856,10 @@ ssize_t btrfs_copy_file_range(struct file *file_in, loff_t pos_in,
 {
 	ssize_t ret;
 
+	if (inode_in->i_sb != inode_out->i_sb ||
+	    file_in->f_path.mnt != file_out->f_path.mnt)
+		return -EXDEV;

The same check exists in btrfs_clone_files (added in the previous patch), what's the reason to add it here again?

Quoted 2 lines
fs/btrfs/ioctl.c +1 -0
+
 	ret = btrfs_clone_files(file_out, file_in, pos_in, len, pos_out);
#6 lore raw

Re: [PATCH v1 4/8] btrfs: Add mountpoint checking during btrfs_copy_file_range

Quoted 17 lines · David Sterba
Quoted 16 lines · Anna Schumaker

We need to verify that both the source and the destination files are part of the same filesystem, otherwise we can't create a reflink.

---
 fs/btrfs/ioctl.c | 4 ++++
 1 file changed, 4 insertions(+)
fs/btrfs/ioctl.c +0 -0
index 62ae286..9c0c955 100644
@@ -3856,6 +3856,10 @@ ssize_t btrfs_copy_file_range(struct file *file_in, loff_t pos_in,
 {
 	ssize_t ret;
 
Quoted 6 lines

fs/btrfs/ioctl.c ssize_t btrfs_copy_file_range(struct file *file_in, loff_t pos_in, · line 3861

Quoted 3 lines
fs/btrfs/ioctl.c +3 -0
+	if (inode_in->i_sb != inode_out->i_sb ||
+	    file_in->f_path.mnt != file_out->f_path.mnt)
+		return -EXDEV;

The same check exists in btrfs_clone_files (added in the previous patch), what's the reason to add it here again?

To be really, really sure that it's not a cross-device copy? :)

In reality, it was an oversight and I'll drop the patch. Thanks for letting me know!

Anna

Quoted 3 lines
Quoted 3 lines
fs/btrfs/ioctl.c +1 -0
+
 	ret = btrfs_clone_files(file_out, file_in, pos_in, len, pos_out);
#7 lore raw

PATCH 5/8 vfs: Remove copy_file_range mountpoint checks

In reply to a message not in the archive

I still want to do an in-kernel copy even if the files are on different mountpoints, and NFS has a "server to server" copy that expects two files on different mountpoints. Let's have individual filesystems implement this check instead.

---
 fs/read_write.c | 5 -----
 1 file changed, 5 deletions(-)
fs/read_write.c +0 -5
index 82c4933..922ca58 100644
@@ -1369,11 +1369,6 @@ ssize_t vfs_copy_file_range(struct file *file_in, loff_t pos_in,
 	    pos_in + len > i_size_read(inode_in))
 		return -EINVAL;
 
-	/* this could be relaxed once a method supports cross-fs copies */
-	if (inode_in->i_sb != inode_out->i_sb ||
-	    file_in->f_path.mnt != file_out->f_path.mnt)
-		return -EXDEV;
-
 	/* forbid ranges in the same file */
 	if (inode_in == inode_out)
 		return -EINVAL;
Signature · 6 lines
2.5.1

--
To unsubscribe from this list: send the line "unsubscribe linux-nfs" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#8 lore raw

PATCH 6/8 vfs: Copy should check len after file open mode

In reply to a message not in the archive

I don't think it makes sense to report that a copy succeeded if the files aren't open properly.

---
 fs/read_write.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
fs/read_write.c +3 -3
index 922ca58..59637ed 100644
@@ -1345,9 +1345,6 @@ ssize_t vfs_copy_file_range(struct file *file_in, loff_t pos_in,
 	if (flags)
 		return -EINVAL;
 
-	if (len == 0)
-		return 0;
-
 	/* copy_file_range allows full ssize_t len, ignoring MAX_RW_COUNT  */
 	ret = rw_verify_area(READ, file_in, &pos_in, len);
 	if (ret >= 0)
@@ -1373,6 +1370,9 @@ ssize_t vfs_copy_file_range(struct file *file_in, loff_t pos_in,
 	if (inode_in == inode_out)
 		return -EINVAL;
 
+	if (len == 0)
+		return 0;
+
 	ret = mnt_want_write_file(file_out);
 	if (ret)
 		return ret;
Signature · 6 lines
2.5.1

--
To unsubscribe from this list: send the line "unsubscribe linux-nfs" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#9 lore raw

PATCH 8/8 vfs: Fall back on splice if no copy function defined

In reply to a message not in the archive

The NFS server will need a fallback for filesystems that don't have any kind of copy acceleration yet, and it should be generally useful to have an in-kernel copy to avoid lots of switches between kernel and user space. Users who only want a reflink can pass the COPY_REFLINK flag to disable the fallback.

---
 fs/read_write.c           | 16 ++++++++++++----
 include/linux/copy.h      |  6 ++++++
 include/uapi/linux/Kbuild |  1 +
 include/uapi/linux/copy.h |  6 ++++++
 4 files changed, 25 insertions(+), 4 deletions(-)
 create mode 100644 include/linux/copy.h
 create mode 100644 include/uapi/linux/copy.h
fs/read_write.c +12 -4
index 9dafb7f..bd7e7e2 100644
@@ -7,6 +7,7 @@
 #include <linux/slab.h> 
 #include <linux/stat.h>
 #include <linux/fcntl.h>
+#include <linux/copy.h>
 #include <linux/file.h>
 #include <linux/uio.h>
 #include <linux/fsnotify.h>
@@ -1342,7 +1343,7 @@ ssize_t vfs_copy_file_range(struct file *file_in, loff_t pos_in,
 	struct inode *inode_out;
 	ssize_t ret;
 
-	if (flags)
+	if (!((flags == 0) || (flags == COPY_REFLINK)))
 		return -EINVAL;
 
 	/* copy_file_range allows full ssize_t len, ignoring MAX_RW_COUNT  */
@@ -1355,7 +1356,7 @@ ssize_t vfs_copy_file_range(struct file *file_in, loff_t pos_in,
 	if (!(file_in->f_mode & FMODE_READ) ||
 	    !(file_out->f_mode & FMODE_WRITE) ||
 	    (file_out->f_flags & O_APPEND) ||
-	    !file_out->f_op || !file_out->f_op->copy_file_range)
+	    !file_in->f_op)
 		return -EBADF;
 
 	inode_in = file_inode(file_in);
@@ -1377,8 +1378,15 @@ ssize_t vfs_copy_file_range(struct file *file_in, loff_t pos_in,
 	if (ret)
 		return ret;
 
-	ret = file_out->f_op->copy_file_range(file_in, pos_in, file_out, pos_out,
-					      len, flags);
+	ret = -EOPNOTSUPP;
+	if (file_out->f_op->copy_file_range)
+		ret = file_out->f_op->copy_file_range(file_in, pos_in, file_out,
+						      pos_out, len, flags);
+	if ((ret < 0) && !(flags & COPY_REFLINK)) {
+		file_start_write(file_out);
+		ret = do_splice_direct(file_in, &pos_in, file_out, &pos_out, len, 0);
+		file_end_write(file_out);
+	}
 	if (ret > 0) {
 		fsnotify_access(file_in);
 		add_rchar(current, ret);
include/linux/copy.h new file +6 -0
new file mode 100644
index 0000000..fd54543
@@ -0,0 +1,6 @@
+#ifndef _LINUX_COPY_H
+#define _LINUX_COPY_H
+
+#include <uapi/linux/copy.h>
+
+#endif /* _LINUX_COPY_H */
include/uapi/linux/Kbuild +1 -0
index 1ff9942..b6109f3 100644
@@ -90,6 +90,7 @@ header-y += coda_psdev.h
 header-y += coff.h
 header-y += connector.h
 header-y += const.h
+header-y += copy.h
 header-y += cramfs_fs.h
 header-y += cuda.h
 header-y += cyclades.h
include/uapi/linux/copy.h new file +6 -0
new file mode 100644
index 0000000..68f3d65
@@ -0,0 +1,6 @@
+#ifndef _UAPI_LINUX_COPY_H
+#define _UAPI_LINUX_COPY_H
+
+#define COPY_REFLINK	1	/* only perform a reflink */
+
+#endif /* _UAPI_LINUX_COPY_H */
Signature · 6 lines
2.5.1

--
To unsubscribe from this list: send the line "unsubscribe linux-nfs" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#10 lore raw

Re: [PATCH v1 8/8] vfs: Fall back on splice if no copy function defined

Quoted 29 lines · Anna Schumaker

The NFS server will need a fallback for filesystems that don't have any kind of copy acceleration yet, and it should be generally useful to have an in-kernel copy to avoid lots of switches between kernel and user space. Users who only want a reflink can pass the COPY_REFLINK flag to disable the fallback.

---
 fs/read_write.c           | 16 ++++++++++++----
 include/linux/copy.h      |  6 ++++++
 include/uapi/linux/Kbuild |  1 +
 include/uapi/linux/copy.h |  6 ++++++
 4 files changed, 25 insertions(+), 4 deletions(-)
 create mode 100644 include/linux/copy.h
 create mode 100644 include/uapi/linux/copy.h
fs/read_write.c +1 -0
index 9dafb7f..bd7e7e2 100644
@@ -7,6 +7,7 @@
 #include <linux/slab.h> 
 #include <linux/stat.h>
 #include <linux/fcntl.h>
+#include <linux/copy.h>
 #include <linux/file.h>
 #include <linux/uio.h>
 #include <linux/fsnotify.h>

fs/read_write.c ssize_t vfs_copy_file_range(struct file *file_in, loff_t pos_in, · line 1346

Quoted 5 lines
fs/read_write.c +1 -1
@@ -1342,7 +1343,7 @@ ssize_t vfs_copy_file_range(struct file *file_in, loff_t pos_in,
 	struct inode *inode_out;
 	ssize_t ret;
 
-	if (flags)
+	if (!((flags == 0) || (flags == COPY_REFLINK)))

I think it's a good idea for the copy_file_range flags to have the name of the syscall in them at the beginning, both to try to avoid namespace collisions and also to make it clear where the flag comes from. So, could we prefix the values with "COPY_FILE_RANGE_" instead of just "COPY_"?

Also... I'm confused by what the structure of check implies about 'flags' -- are each of 'flags's values supposed to be mutually exclusive, or is it a straight bitset like flags arguments tend to be?

Say we want to add dedupe and ponies as potential behavioral variants. Then we'd end up with something like:

/* raw flags */
#define COPY_FILE_RANGE_SHARE_BLOCKS (1)
#define COPY_FILE_RANGE_CHECK_SAME (2)
#define COPY_FILE_RANGE_PONIES (4)

/* syntactic sugar */
#define COPY_FILE_RANGE_DEDUPE (COPY_FILE_RANGE_SHARE_BLOCKS | \
COPY_FILE_RANGE_CHECK_SAME)

#define COPY_FILE_RANGE_ALL		(COPY_FILE_RANGE_SHARE_BLOCKS | \
					 COPY_FILE_RANGE_CHECK_SAME | \
					 COPY_FILE_RANGE_PONIES)

and in copy_file_range():

if (flags & ~COPY_FILE_RANGE_ALL)

	return -EINVAL;

if ((flags & COPY_FILE_RANGE_CHECK_SAME) &&
!(flags & COPY_FILE_RANGE_SHARE_BLOCKS))
return -EINVAL; /* or is it return 0? */

if (flags & COPY_FILE_RANGE_PONIES)

	panic();
if (flags & COPY_FILE_RANGE_CHECK_SAME)
	check_same_contents(...);
err = vfs_copy_range(...);
if (err < 0 && !(flags & COPY_FILE_RANGE_SHARE_BLOCKS))
	err = splice(...);

One hard part is figuring out just what actions we want userland to be able to ask for. I can think of three: "share blocks via remapping" (i.e. reflink), "share blocks via remapping but only if they're identical" (i.e. dedupe), and "classic copy via pagecache". I lean towards giving each variant its own bit and only allowing through the cases that make sense, rather than giving each valid combination its own unique number, but maybe I misinterpreted the intent behind the code.

(I guess there could also be 'do it with hardware assist' but that's digging myself a pretty deep hole.)

--D

Quoted 69 lines
fs/read_write.c +10 -3
 		return -EINVAL;
 
 	/* copy_file_range allows full ssize_t len, ignoring MAX_RW_COUNT  */
@@ -1355,7 +1356,7 @@ ssize_t vfs_copy_file_range(struct file *file_in, loff_t pos_in,
 	if (!(file_in->f_mode & FMODE_READ) ||
 	    !(file_out->f_mode & FMODE_WRITE) ||
 	    (file_out->f_flags & O_APPEND) ||
-	    !file_out->f_op || !file_out->f_op->copy_file_range)
+	    !file_in->f_op)
 		return -EBADF;
 
 	inode_in = file_inode(file_in);
@@ -1377,8 +1378,15 @@ ssize_t vfs_copy_file_range(struct file *file_in, loff_t pos_in,
 	if (ret)
 		return ret;
 
-	ret = file_out->f_op->copy_file_range(file_in, pos_in, file_out, pos_out,
-					      len, flags);
+	ret = -EOPNOTSUPP;
+	if (file_out->f_op->copy_file_range)
+		ret = file_out->f_op->copy_file_range(file_in, pos_in, file_out,
+						      pos_out, len, flags);
+	if ((ret < 0) && !(flags & COPY_REFLINK)) {
+		file_start_write(file_out);
+		ret = do_splice_direct(file_in, &pos_in, file_out, &pos_out, len, 0);
+		file_end_write(file_out);
+	}
 	if (ret > 0) {
 		fsnotify_access(file_in);
 		add_rchar(current, ret);
include/linux/copy.h new file +6 -0
new file mode 100644
index 0000000..fd54543
@@ -0,0 +1,6 @@
+#ifndef _LINUX_COPY_H
+#define _LINUX_COPY_H
+
+#include <uapi/linux/copy.h>
+
+#endif /* _LINUX_COPY_H */
include/uapi/linux/Kbuild +1 -0
index 1ff9942..b6109f3 100644
@@ -90,6 +90,7 @@ header-y += coda_psdev.h
 header-y += coff.h
 header-y += connector.h
 header-y += const.h
+header-y += copy.h
 header-y += cramfs_fs.h
 header-y += cuda.h
 header-y += cyclades.h
include/uapi/linux/copy.h new file +6 -0
new file mode 100644
index 0000000..68f3d65
@@ -0,0 +1,6 @@
+#ifndef _UAPI_LINUX_COPY_H
+#define _UAPI_LINUX_COPY_H
+
+#define COPY_REFLINK	1	/* only perform a reflink */
+
+#endif /* _UAPI_LINUX_COPY_H */
Signature · 1 line
2.5.1

A message that is not in the archive (sent off-list or not yet received): [email protected]

#12 lore raw

Re: [PATCH v1 8/8] vfs: Fall back on splice if no copy function defined

Quoted 33 lines · Darrick J. Wong
Quoted 32 lines · Anna Schumaker

The NFS server will need a fallback for filesystems that don't have any kind of copy acceleration yet, and it should be generally useful to have an in-kernel copy to avoid lots of switches between kernel and user space. Users who only want a reflink can pass the COPY_REFLINK flag to disable the fallback.

---
 fs/read_write.c           | 16 ++++++++++++----
 include/linux/copy.h      |  6 ++++++
 include/uapi/linux/Kbuild |  1 +
 include/uapi/linux/copy.h |  6 ++++++
 4 files changed, 25 insertions(+), 4 deletions(-)
 create mode 100644 include/linux/copy.h
 create mode 100644 include/uapi/linux/copy.h
fs/read_write.c +1 -0
index 9dafb7f..bd7e7e2 100644
@@ -7,6 +7,7 @@
 #include <linux/slab.h> 
 #include <linux/stat.h>
 #include <linux/fcntl.h>
+#include <linux/copy.h>
 #include <linux/file.h>
 #include <linux/uio.h>
 #include <linux/fsnotify.h>
@@ -1342,7 +1343,7 @@ ssize_t vfs_copy_file_range(struct file *file_in, loff_t pos_in,
 	struct inode *inode_out;
 	ssize_t ret;
 
Quoted 7 lines

fs/read_write.c ssize_t vfs_copy_file_range(struct file *file_in, loff_t pos_in, · line 1346

Quoted 2 lines
fs/read_write.c +1 -1
-	if (flags)
+	if (!((flags == 0) || (flags == COPY_REFLINK)))

I think it's a good idea for the copy_file_range flags to have the name of the syscall in them at the beginning, both to try to avoid namespace collisions and also to make it clear where the flag comes from. So, could we prefix the values with "COPY_FILE_RANGE_" instead of just "COPY_"?

Sure, that makes sense!

Quoted 4 lines

Also... I'm confused by what the structure of check implies about 'flags' -- are each of 'flags's values supposed to be mutually exclusive, or is it a straight bitset like flags arguments tend to be?

I was expecting this to be a bitset, but that's not the easiest to communicate when there is (currently) only one flag. I'll take a look at other functions to see how I should be validating this!

Thanks for the comments,
Anna

Quoted 114 lines

Say we want to add dedupe and ponies as potential behavioral variants. Then we'd end up with something like:

/* raw flags */
#define COPY_FILE_RANGE_SHARE_BLOCKS (1)
#define COPY_FILE_RANGE_CHECK_SAME (2)
#define COPY_FILE_RANGE_PONIES (4)

/* syntactic sugar */
#define COPY_FILE_RANGE_DEDUPE (COPY_FILE_RANGE_SHARE_BLOCKS | \
COPY_FILE_RANGE_CHECK_SAME)

#define COPY_FILE_RANGE_ALL		(COPY_FILE_RANGE_SHARE_BLOCKS | \
					 COPY_FILE_RANGE_CHECK_SAME | \
					 COPY_FILE_RANGE_PONIES)

and in copy_file_range():

if (flags & ~COPY_FILE_RANGE_ALL)

	return -EINVAL;

if ((flags & COPY_FILE_RANGE_CHECK_SAME) &&
!(flags & COPY_FILE_RANGE_SHARE_BLOCKS))
return -EINVAL; /* or is it return 0? */

if (flags & COPY_FILE_RANGE_PONIES)

	panic();
if (flags & COPY_FILE_RANGE_CHECK_SAME)
	check_same_contents(...);
err = vfs_copy_range(...);
if (err < 0 && !(flags & COPY_FILE_RANGE_SHARE_BLOCKS))
	err = splice(...);

One hard part is figuring out just what actions we want userland to be able to ask for. I can think of three: "share blocks via remapping" (i.e. reflink), "share blocks via remapping but only if they're identical" (i.e. dedupe), and "classic copy via pagecache". I lean towards giving each variant its own bit and only allowing through the cases that make sense, rather than giving each valid combination its own unique number, but maybe I misinterpreted the intent behind the code.

(I guess there could also be 'do it with hardware assist' but that's digging myself a pretty deep hole.)

--D

Quoted 69 lines
fs/read_write.c +10 -3
 		return -EINVAL;
 
 	/* copy_file_range allows full ssize_t len, ignoring MAX_RW_COUNT  */
@@ -1355,7 +1356,7 @@ ssize_t vfs_copy_file_range(struct file *file_in, loff_t pos_in,
 	if (!(file_in->f_mode & FMODE_READ) ||
 	    !(file_out->f_mode & FMODE_WRITE) ||
 	    (file_out->f_flags & O_APPEND) ||
-	    !file_out->f_op || !file_out->f_op->copy_file_range)
+	    !file_in->f_op)
 		return -EBADF;
 
 	inode_in = file_inode(file_in);
@@ -1377,8 +1378,15 @@ ssize_t vfs_copy_file_range(struct file *file_in, loff_t pos_in,
 	if (ret)
 		return ret;
 
-	ret = file_out->f_op->copy_file_range(file_in, pos_in, file_out, pos_out,
-					      len, flags);
+	ret = -EOPNOTSUPP;
+	if (file_out->f_op->copy_file_range)
+		ret = file_out->f_op->copy_file_range(file_in, pos_in, file_out,
+						      pos_out, len, flags);
+	if ((ret < 0) && !(flags & COPY_REFLINK)) {
+		file_start_write(file_out);
+		ret = do_splice_direct(file_in, &pos_in, file_out, &pos_out, len, 0);
+		file_end_write(file_out);
+	}
 	if (ret > 0) {
 		fsnotify_access(file_in);
 		add_rchar(current, ret);
include/linux/copy.h new file +6 -0
new file mode 100644
index 0000000..fd54543
@@ -0,0 +1,6 @@
+#ifndef _LINUX_COPY_H
+#define _LINUX_COPY_H
+
+#include <uapi/linux/copy.h>
+
+#endif /* _LINUX_COPY_H */
include/uapi/linux/Kbuild +1 -0
index 1ff9942..b6109f3 100644
@@ -90,6 +90,7 @@ header-y += coda_psdev.h
 header-y += coff.h
 header-y += connector.h
 header-y += const.h
+header-y += copy.h
 header-y += cramfs_fs.h
 header-y += cuda.h
 header-y += cyclades.h
include/uapi/linux/copy.h new file +6 -0
new file mode 100644
index 0000000..68f3d65
@@ -0,0 +1,6 @@
+#ifndef _UAPI_LINUX_COPY_H
+#define _UAPI_LINUX_COPY_H
+
+#define COPY_REFLINK	1	/* only perform a reflink */
+
+#endif /* _UAPI_LINUX_COPY_H */
Signature · 1 line
2.5.1
#13 lore raw

[PATCH v1 9/8] copy_file_range.2: New page documenting copy_file_range()

In reply to a message not in the archive

copy_file_range() is a new system call for copying ranges of data completely in the kernel. This gives filesystems an opportunity to implement some kind of "copy acceleration", such as reflinks or server-side-copy (in the case of NFS).

---
 man2/copy_file_range.2 | 168 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 168 insertions(+)
 create mode 100644 man2/copy_file_range.2
man2/copy_file_range.2 new file +168 -0
new file mode 100644
index 0000000..4a4cb73
@@ -0,0 +1,168 @@
+.\"This manpage is Copyright (C) 2015 Anna Schumaker <[email protected]>
+.TH COPY 2 2015-8-31 "Linux" "Linux Programmer's Manual"
+.SH NAME
+copy_file_range \- Copy a range of data from one file to another
+.SH SYNOPSIS
+.nf
+.B #include <linux/copy.h>
+.B #include <sys/syscall.h>
+.B #include <unistd.h>
+
+.BI "ssize_t syscall(__NR_copy_file_range, int " fd_in ", loff_t * " off_in ",
+.BI "                int " fd_out ", loff_t * " off_out ", size_t " len ",
+.BI "                unsigned int " flags );
+.fi
+.SH DESCRIPTION
+The
+.BR copy_file_range ()
+system call performs an in-kernel copy between two file descriptors
+without all that tedious mucking about in userspace.
+It copies up to
+.I len
+bytes of data from file descriptor
+.I fd_in
+to file descriptor
+.I fd_out
+at
+.IR off_out .
+The file descriptors must not refer to the same file.
+
+The following semantics apply for
+.IR fd_in ,
+and similar statements apply to
+.IR off_out :
+.IP * 3
+If
+.I off_in
+is NULL, then bytes are read from
+.I fd_in
+starting from the current file offset and the current
+file offset is adjusted appropriately.
+.IP *
+If
+.I off_in
+is not NULL, then
+.I off_in
+must point to a buffer that specifies the starting
+offset where bytes from
+.I fd_in
+will be read.  The current file offset of
+.I fd_in
+is not changed, but
+.I off_in
+is adjusted appropriately.
+.PP
+The default behavior of
+.BR copy_file_range ()
+is filesystem specific, and might result in creating a
+copy-on-write reflink.
+In the event that a given filesystem does not implement
+any form of copy acceleration, the kernel will perform
+a deep copy of the requested range by reading bytes from
+.I fd_in
+and writing them to
+.IR fd_out .
+
+Currently, Linux only supports the following flag:
+.TP 1.9i
+.B COPY_REFLINK
+Only perform the copy if the filesystem can do it as a reflink.
+Do not fall back on performing a deep copy.
+.SH RETURN VALUE
+Upon successful completion,
+.BR copy_file_range ()
+will return the number of bytes copied between files.
+This could be less than the length originally requested.
+
+On error,
+.BR copy_file_range ()
+returns \-1 and
+.I errno
+is set to indicate the error.
+.SH ERRORS
+.TP
+.B EBADF
+One or more file descriptors are not valid,
+or do not have proper read-write mode.
+.TP
+.B EINVAL
+Requested range extends beyond the end of the file;
+.I flags
+argument is set to an invalid value.
+.TP
+.B EOPNOTSUPP
+.B COPY_REFLINK
+was specified in
+.IR flags ,
+but the target filesystem does not support reflinks.
+.TP
+.B EXDEV
+Target filesystem doesn't support cross-filesystem copies.
+.SH VERSIONS
+The
+.BR copy_file_range ()
+system call first appeared in Linux 4.3.
+.SH CONFORMING TO
+The
+.BR copy_file_range ()
+system call is a nonstandard Linux extension.
+.SH EXAMPLE
+.nf
+
+#define _GNU_SOURCE
+#include <fcntl.h>
+#include <linux/copy.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <sys/syscall.h>
+#include <unistd.h>
+
+
+int main(int argc, char **argv)
+{
+    int fd_in, fd_out;
+    struct stat stat;
+    loff_t len, ret;
+
+    if (argc != 3) {
+        fprintf(stderr, "Usage: %s <pathname> <pathname>\n", argv[0]);
+        exit(EXIT_FAILURE);
+    }
+
+    fd_in = open(argv[1], O_RDONLY);
+    if (fd_in == -1) {
+        perror("open (argv[1])");
+        exit(EXIT_FAILURE);
+    }
+
+    if (fstat(fd_in, &stat) == -1) {
+        perror("fstat");
+        exit(EXIT_FAILURE);
+    }
+    len = stat.st_size;
+
+    fd_out = open(argv[2], O_WRONLY | O_CREAT, 0644);
+    if (fd_out == -1) {
+        perror("open (argv[2])");
+        exit(EXIT_FAILURE);
+    }
+
+    do {
+        ret = syscall(__NR_copy_file_range, fd_in, NULL,
+                      fd_out, NULL, len, 0);
+        if (ret == -1) {
+            perror("copy_file_range");
+            exit(EXIT_FAILURE);
+        }
+
+        len -= ret;
+    } while (len > 0);
+
+    close(fd_in);
+    close(fd_out);
+    exit(EXIT_SUCCESS);
+}
+.fi
+.SH SEE ALSO
+.BR splice (2)
Signature · 6 lines
2.5.1

--
To unsubscribe from this list: send the line "unsubscribe linux-nfs" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#14 lore raw

Re: [PATCH v1 9/8] copy_file_range.2: New page documenting copy_file_range()

Quoted 26 lines · Anna Schumaker

copy_file_range() is a new system call for copying ranges of data completely in the kernel. This gives filesystems an opportunity to implement some kind of "copy acceleration", such as reflinks or server-side-copy (in the case of NFS).

---
 man2/copy_file_range.2 | 168 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 168 insertions(+)
 create mode 100644 man2/copy_file_range.2
man2/copy_file_range.2 new file +9 -0
new file mode 100644
index 0000000..4a4cb73
@@ -0,0 +1,168 @@
+.\"This manpage is Copyright (C) 2015 Anna Schumaker <[email protected]>
+.TH COPY 2 2015-8-31 "Linux" "Linux Programmer's Manual"
+.SH NAME
+copy_file_range \- Copy a range of data from one file to another
+.SH SYNOPSIS
+.nf
+.B #include <linux/copy.h>
+.B #include <sys/syscall.h>
+.B #include <unistd.h>

man2/copy_file_range.2 line 19

Quoted 10 lines
man2/copy_file_range.2 new file +10 -0
+
+.BI "ssize_t syscall(__NR_copy_file_range, int " fd_in ", loff_t * " off_in ",
+.BI "                int " fd_out ", loff_t * " off_out ", size_t " len ",
+.BI "                unsigned int " flags );
+.fi
+.SH DESCRIPTION
+The
+.BR copy_file_range ()
+system call performs an in-kernel copy between two file descriptors
+without all that tedious mucking about in userspace.

;)

man2/copy_file_range.2 line 28

Quoted 9 lines
man2/copy_file_range.2 new file +9 -0
+It copies up to
+.I len
+bytes of data from file descriptor
+.I fd_in
+to file descriptor
+.I fd_out
+at
+.IR off_out .
+The file descriptors must not refer to the same file.

Why? btrfs (and XFS) reflink can handle the case of a file sharing blocks with itself.

Quoted 23 lines
man2/copy_file_range.2 new file +23 -0
+
+The following semantics apply for
+.IR fd_in ,
+and similar statements apply to
+.IR off_out :
+.IP * 3
+If
+.I off_in
+is NULL, then bytes are read from
+.I fd_in
+starting from the current file offset and the current
+file offset is adjusted appropriately.
+.IP *
+If
+.I off_in
+is not NULL, then
+.I off_in
+must point to a buffer that specifies the starting
+offset where bytes from
+.I fd_in
+will be read.  The current file offset of
+.I fd_in
+is not changed, but

man2/copy_file_range.2 line 61

Quoted 10 lines
man2/copy_file_range.2 new file +10 -0
+.I off_in
+is adjusted appropriately.
+.PP
+The default behavior of
+.BR copy_file_range ()
+is filesystem specific, and might result in creating a
+copy-on-write reflink.
+In the event that a given filesystem does not implement
+any form of copy acceleration, the kernel will perform
+a deep copy of the requested range by reading bytes from

I wonder if it's wise to allow deep copies -- what happens if len == 1T? Will this syscall just block for a really long time?

man2/copy_file_range.2 line 64

Quoted 3 lines
man2/copy_file_range.2 new file +3 -0
+.I fd_in
+and writing them to
+.IR fd_out .

"...if COPY_REFLINK is not set in flags."

Quoted 12 lines
man2/copy_file_range.2 new file +12 -0
+
+Currently, Linux only supports the following flag:
+.TP 1.9i
+.B COPY_REFLINK
+Only perform the copy if the filesystem can do it as a reflink.
+Do not fall back on performing a deep copy.
+.SH RETURN VALUE
+Upon successful completion,
+.BR copy_file_range ()
+will return the number of bytes copied between files.
+This could be less than the length originally requested.
+

man2/copy_file_range.2 line 86

Quoted 10 lines
man2/copy_file_range.2 new file +10 -0
+On error,
+.BR copy_file_range ()
+returns \-1 and
+.I errno
+is set to indicate the error.
+.SH ERRORS
+.TP
+.B EBADF
+One or more file descriptors are not valid,
+or do not have proper read-write mode.

"or fd_out is not opened for writing"?

Quoted 5 lines
man2/copy_file_range.2 new file +5 -0
+.TP
+.B EINVAL
+Requested range extends beyond the end of the file;
+.I flags
+argument is set to an invalid value.

man2/copy_file_range.2 line 101

Quoted 10 lines
man2/copy_file_range.2 new file +10 -0
+.TP
+.B EOPNOTSUPP
+.B COPY_REFLINK
+was specified in
+.IR flags ,
+but the target filesystem does not support reflinks.
+.TP
+.B EXDEV
+Target filesystem doesn't support cross-filesystem copies.
+.SH VERSIONS

Perhaps this ought to list a few more errors (EIO, ENOSPC, ENOSYS, EPERM...) that can be returned? (I was looking at the fallocate manpage.)

--D

Quoted 74 lines
man2/copy_file_range.2 new file +67 -0
+The
+.BR copy_file_range ()
+system call first appeared in Linux 4.3.
+.SH CONFORMING TO
+The
+.BR copy_file_range ()
+system call is a nonstandard Linux extension.
+.SH EXAMPLE
+.nf
+
+#define _GNU_SOURCE
+#include <fcntl.h>
+#include <linux/copy.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <sys/syscall.h>
+#include <unistd.h>
+
+
+int main(int argc, char **argv)
+{
+    int fd_in, fd_out;
+    struct stat stat;
+    loff_t len, ret;
+
+    if (argc != 3) {
+        fprintf(stderr, "Usage: %s <pathname> <pathname>\n", argv[0]);
+        exit(EXIT_FAILURE);
+    }
+
+    fd_in = open(argv[1], O_RDONLY);
+    if (fd_in == -1) {
+        perror("open (argv[1])");
+        exit(EXIT_FAILURE);
+    }
+
+    if (fstat(fd_in, &stat) == -1) {
+        perror("fstat");
+        exit(EXIT_FAILURE);
+    }
+    len = stat.st_size;
+
+    fd_out = open(argv[2], O_WRONLY | O_CREAT, 0644);
+    if (fd_out == -1) {
+        perror("open (argv[2])");
+        exit(EXIT_FAILURE);
+    }
+
+    do {
+        ret = syscall(__NR_copy_file_range, fd_in, NULL,
+                      fd_out, NULL, len, 0);
+        if (ret == -1) {
+            perror("copy_file_range");
+            exit(EXIT_FAILURE);
+        }
+
+        len -= ret;
+    } while (len > 0);
+
+    close(fd_in);
+    close(fd_out);
+    exit(EXIT_SUCCESS);
+}
+.fi
+.SH SEE ALSO
+.BR splice (2)
Signature · 6 lines
2.5.1

--
To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): [email protected]

#16 lore raw

Re: [PATCH v1 9/8] copy_file_range.2: New page documenting copy_file_range()

Quoted 82 lines · Darrick J. Wong
Quoted 26 lines · Anna Schumaker

copy_file_range() is a new system call for copying ranges of data completely in the kernel. This gives filesystems an opportunity to implement some kind of "copy acceleration", such as reflinks or server-side-copy (in the case of NFS).

---
man2/copy_file_range.2 | 168 +++++++++++++++++++++++++++++++++++++++++++++++++

1 file changed, 168 insertions(+)

create mode 100644 man2/copy_file_range.2

man2/copy_file_range.2 new file +9 -0
new file mode 100644
index 0000000..4a4cb73
@@ -0,0 +1,168 @@
+.\"This manpage is Copyright (C) 2015 Anna Schumaker <[email protected]>
+.TH COPY 2 2015-8-31 "Linux" "Linux Programmer's Manual"
+.SH NAME
+copy_file_range \- Copy a range of data from one file to another
+.SH SYNOPSIS
+.nf
+.B #include <linux/copy.h>
+.B #include <sys/syscall.h>
+.B #include <unistd.h>

man2/copy_file_range.2 line 19

Quoted 10 lines
man2/copy_file_range.2 new file +10 -0
+
+.BI "ssize_t syscall(__NR_copy_file_range, int " fd_in ", loff_t * " off_in ",
+.BI "                int " fd_out ", loff_t * " off_out ", size_t " len ",
+.BI "                unsigned int " flags );
+.fi
+.SH DESCRIPTION
+The
+.BR copy_file_range ()
+system call performs an in-kernel copy between two file descriptors
+without all that tedious mucking about in userspace.

;)

man2/copy_file_range.2 line 28

Quoted 9 lines
man2/copy_file_range.2 new file +9 -0
+It copies up to
+.I len
+bytes of data from file descriptor
+.I fd_in
+to file descriptor
+.I fd_out
+at
+.IR off_out .
+The file descriptors must not refer to the same file.

Why? btrfs (and XFS) reflink can handle the case of a file sharing blocks with itself.

Quoted 28 lines
man2/copy_file_range.2 new file +28 -0
+
+The following semantics apply for
+.IR fd_in ,
+and similar statements apply to
+.IR off_out :
+.IP * 3
+If
+.I off_in
+is NULL, then bytes are read from
+.I fd_in
+starting from the current file offset and the current
+file offset is adjusted appropriately.
+.IP *
+If
+.I off_in
+is not NULL, then
+.I off_in
+must point to a buffer that specifies the starting
+offset where bytes from
+.I fd_in
+will be read.  The current file offset of
+.I fd_in
+is not changed, but
+.I off_in
+is adjusted appropriately.
+.PP
+The default behavior of
+.BR copy_file_range ()
Quoted 8 lines

man2/copy_file_range.2 line 61

Quoted 5 lines
man2/copy_file_range.2 new file +5 -0
+is filesystem specific, and might result in creating a
+copy-on-write reflink.
+In the event that a given filesystem does not implement
+any form of copy acceleration, the kernel will perform
+a deep copy of the requested range by reading bytes from

I wonder if it's wise to allow deep copies -- what happens if len == 1T? Will this syscall just block for a really long time?

It should be interruptible, and return the length of the number of bytes copied so far, just like read() and write(). That allows the caller to continue where it left off, or abort and delete the target file, or whatever it wants to do.

Cheers, Andreas

Quoted 122 lines

man2/copy_file_range.2 line 64

Quoted 3 lines
man2/copy_file_range.2 new file +3 -0
+.I fd_in
+and writing them to
+.IR fd_out .

"...if COPY_REFLINK is not set in flags."

Quoted 12 lines
man2/copy_file_range.2 new file +12 -0
+
+Currently, Linux only supports the following flag:
+.TP 1.9i
+.B COPY_REFLINK
+Only perform the copy if the filesystem can do it as a reflink.
+Do not fall back on performing a deep copy.
+.SH RETURN VALUE
+Upon successful completion,
+.BR copy_file_range ()
+will return the number of bytes copied between files.
+This could be less than the length originally requested.
+

man2/copy_file_range.2 line 86

Quoted 10 lines
man2/copy_file_range.2 new file +10 -0
+On error,
+.BR copy_file_range ()
+returns \-1 and
+.I errno
+is set to indicate the error.
+.SH ERRORS
+.TP
+.B EBADF
+One or more file descriptors are not valid,
+or do not have proper read-write mode.

"or fd_out is not opened for writing"?

Quoted 5 lines
man2/copy_file_range.2 new file +5 -0
+.TP
+.B EINVAL
+Requested range extends beyond the end of the file;
+.I flags
+argument is set to an invalid value.

man2/copy_file_range.2 line 101

Quoted 10 lines
man2/copy_file_range.2 new file +10 -0
+.TP
+.B EOPNOTSUPP
+.B COPY_REFLINK
+was specified in
+.IR flags ,
+but the target filesystem does not support reflinks.
+.TP
+.B EXDEV
+Target filesystem doesn't support cross-filesystem copies.
+.SH VERSIONS

Perhaps this ought to list a few more errors (EIO, ENOSPC, ENOSYS, EPERM...) that can be returned? (I was looking at the fallocate manpage.)

--D

Quoted 70 lines
man2/copy_file_range.2 new file +67 -0
+The
+.BR copy_file_range ()
+system call first appeared in Linux 4.3.
+.SH CONFORMING TO
+The
+.BR copy_file_range ()
+system call is a nonstandard Linux extension.
+.SH EXAMPLE
+.nf
+
+#define _GNU_SOURCE
+#include <fcntl.h>
+#include <linux/copy.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <sys/syscall.h>
+#include <unistd.h>
+
+
+int main(int argc, char **argv)
+{
+    int fd_in, fd_out;
+    struct stat stat;
+    loff_t len, ret;
+
+    if (argc != 3) {
+        fprintf(stderr, "Usage: %s <pathname> <pathname>\n", argv[0]);
+        exit(EXIT_FAILURE);
+    }
+
+    fd_in = open(argv[1], O_RDONLY);
+    if (fd_in == -1) {
+        perror("open (argv[1])");
+        exit(EXIT_FAILURE);
+    }
+
+    if (fstat(fd_in, &stat) == -1) {
+        perror("fstat");
+        exit(EXIT_FAILURE);
+    }
+    len = stat.st_size;
+
+    fd_out = open(argv[2], O_WRONLY | O_CREAT, 0644);
+    if (fd_out == -1) {
+        perror("open (argv[2])");
+        exit(EXIT_FAILURE);
+    }
+
+    do {
+        ret = syscall(__NR_copy_file_range, fd_in, NULL,
+                      fd_out, NULL, len, 0);
+        if (ret == -1) {
+            perror("copy_file_range");
+            exit(EXIT_FAILURE);
+        }
+
+        len -= ret;
+    } while (len > 0);
+
+    close(fd_in);
+    close(fd_out);
+    exit(EXIT_SUCCESS);
+}
+.fi
+.SH SEE ALSO
+.BR splice (2)
Signature · 1 line
2.5.1
Quoted 8 lines
Quoted 4 lines

--
To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

--
To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

Cheers, Andreas

--
To unsubscribe from this list: send the line "unsubscribe linux-nfs" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): 95674806-645C-410C-8A4B-A46F03AFFE20-m1MBpc4rdrD3fQ9qLvQP4Q@public.gmane.org

#18 lore raw

Re: [PATCH v1 9/8] copy_file_range.2: New page documenting copy_file_range()

Quoted 92 lines · Andreas Dilger
Quoted 82 lines · Darrick J. Wong
Quoted 26 lines · Anna Schumaker

copy_file_range() is a new system call for copying ranges of data completely in the kernel. This gives filesystems an opportunity to implement some kind of "copy acceleration", such as reflinks or server-side-copy (in the case of NFS).

---
man2/copy_file_range.2 | 168 +++++++++++++++++++++++++++++++++++++++++++++++++

1 file changed, 168 insertions(+)

create mode 100644 man2/copy_file_range.2

man2/copy_file_range.2 new file +9 -0
new file mode 100644
index 0000000..4a4cb73
@@ -0,0 +1,168 @@
+.\"This manpage is Copyright (C) 2015 Anna Schumaker <[email protected]>
+.TH COPY 2 2015-8-31 "Linux" "Linux Programmer's Manual"
+.SH NAME
+copy_file_range \- Copy a range of data from one file to another
+.SH SYNOPSIS
+.nf
+.B #include <linux/copy.h>
+.B #include <sys/syscall.h>
+.B #include <unistd.h>

man2/copy_file_range.2 line 19

Quoted 10 lines
man2/copy_file_range.2 new file +10 -0
+
+.BI "ssize_t syscall(__NR_copy_file_range, int " fd_in ", loff_t * " off_in ",
+.BI "                int " fd_out ", loff_t * " off_out ", size_t " len ",
+.BI "                unsigned int " flags );
+.fi
+.SH DESCRIPTION
+The
+.BR copy_file_range ()
+system call performs an in-kernel copy between two file descriptors
+without all that tedious mucking about in userspace.

;)

man2/copy_file_range.2 line 28

Quoted 9 lines
man2/copy_file_range.2 new file +9 -0
+It copies up to
+.I len
+bytes of data from file descriptor
+.I fd_in
+to file descriptor
+.I fd_out
+at
+.IR off_out .
+The file descriptors must not refer to the same file.

Why? btrfs (and XFS) reflink can handle the case of a file sharing blocks with itself.

Quoted 28 lines
man2/copy_file_range.2 new file +28 -0
+
+The following semantics apply for
+.IR fd_in ,
+and similar statements apply to
+.IR off_out :
+.IP * 3
+If
+.I off_in
+is NULL, then bytes are read from
+.I fd_in
+starting from the current file offset and the current
+file offset is adjusted appropriately.
+.IP *
+If
+.I off_in
+is not NULL, then
+.I off_in
+must point to a buffer that specifies the starting
+offset where bytes from
+.I fd_in
+will be read.  The current file offset of
+.I fd_in
+is not changed, but
+.I off_in
+is adjusted appropriately.
+.PP
+The default behavior of
+.BR copy_file_range ()
Quoted 8 lines

man2/copy_file_range.2 line 61

Quoted 5 lines
man2/copy_file_range.2 new file +5 -0
+is filesystem specific, and might result in creating a
+copy-on-write reflink.
+In the event that a given filesystem does not implement
+any form of copy acceleration, the kernel will perform
+a deep copy of the requested range by reading bytes from

I wonder if it's wise to allow deep copies -- what happens if len == 1T? Will this syscall just block for a really long time?

Quoted 4 lines

It should be interruptible, and return the length of the number of bytes copied so far, just like read() and write(). That allows the caller to continue where it left off, or abort and delete the target file, or whatever it wants to do.

We already return the number of bytes copied so far, so I'll look into making it interruptable!

Thanks,
Anna

Quoted 135 lines

Cheers, Andreas

Quoted 122 lines

man2/copy_file_range.2 line 64

Quoted 3 lines
man2/copy_file_range.2 new file +3 -0
+.I fd_in
+and writing them to
+.IR fd_out .

"...if COPY_REFLINK is not set in flags."

Quoted 12 lines
man2/copy_file_range.2 new file +12 -0
+
+Currently, Linux only supports the following flag:
+.TP 1.9i
+.B COPY_REFLINK
+Only perform the copy if the filesystem can do it as a reflink.
+Do not fall back on performing a deep copy.
+.SH RETURN VALUE
+Upon successful completion,
+.BR copy_file_range ()
+will return the number of bytes copied between files.
+This could be less than the length originally requested.
+

man2/copy_file_range.2 line 86

Quoted 10 lines
man2/copy_file_range.2 new file +10 -0
+On error,
+.BR copy_file_range ()
+returns \-1 and
+.I errno
+is set to indicate the error.
+.SH ERRORS
+.TP
+.B EBADF
+One or more file descriptors are not valid,
+or do not have proper read-write mode.

"or fd_out is not opened for writing"?

Quoted 5 lines
man2/copy_file_range.2 new file +5 -0
+.TP
+.B EINVAL
+Requested range extends beyond the end of the file;
+.I flags
+argument is set to an invalid value.

man2/copy_file_range.2 line 101

Quoted 10 lines
man2/copy_file_range.2 new file +10 -0
+.TP
+.B EOPNOTSUPP
+.B COPY_REFLINK
+was specified in
+.IR flags ,
+but the target filesystem does not support reflinks.
+.TP
+.B EXDEV
+Target filesystem doesn't support cross-filesystem copies.
+.SH VERSIONS

Perhaps this ought to list a few more errors (EIO, ENOSPC, ENOSYS, EPERM...) that can be returned? (I was looking at the fallocate manpage.)

--D

Quoted 70 lines
man2/copy_file_range.2 new file +67 -0
+The
+.BR copy_file_range ()
+system call first appeared in Linux 4.3.
+.SH CONFORMING TO
+The
+.BR copy_file_range ()
+system call is a nonstandard Linux extension.
+.SH EXAMPLE
+.nf
+
+#define _GNU_SOURCE
+#include <fcntl.h>
+#include <linux/copy.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <sys/syscall.h>
+#include <unistd.h>
+
+
+int main(int argc, char **argv)
+{
+    int fd_in, fd_out;
+    struct stat stat;
+    loff_t len, ret;
+
+    if (argc != 3) {
+        fprintf(stderr, "Usage: %s <pathname> <pathname>\n", argv[0]);
+        exit(EXIT_FAILURE);
+    }
+
+    fd_in = open(argv[1], O_RDONLY);
+    if (fd_in == -1) {
+        perror("open (argv[1])");
+        exit(EXIT_FAILURE);
+    }
+
+    if (fstat(fd_in, &stat) == -1) {
+        perror("fstat");
+        exit(EXIT_FAILURE);
+    }
+    len = stat.st_size;
+
+    fd_out = open(argv[2], O_WRONLY | O_CREAT, 0644);
+    if (fd_out == -1) {
+        perror("open (argv[2])");
+        exit(EXIT_FAILURE);
+    }
+
+    do {
+        ret = syscall(__NR_copy_file_range, fd_in, NULL,
+                      fd_out, NULL, len, 0);
+        if (ret == -1) {
+            perror("copy_file_range");
+            exit(EXIT_FAILURE);
+        }
+
+        len -= ret;
+    } while (len > 0);
+
+    close(fd_in);
+    close(fd_out);
+    exit(EXIT_SUCCESS);
+}
+.fi
+.SH SEE ALSO
+.BR splice (2)
Signature · 1 line
2.5.1
Quoted 8 lines
Quoted 4 lines

--
To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

--
To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

Quoted 6 lines

Cheers, Andreas

--
To unsubscribe from this list: send the line "unsubscribe linux-nfs" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#19 lore raw

Re: [PATCH v1 9/8] copy_file_range.2: New page documenting copy_file_range()

In reply to a message not in the archive
Quoted 40 lines · Darrick J. Wong
Quoted 26 lines · Anna Schumaker

copy_file_range() is a new system call for copying ranges of data completely in the kernel. This gives filesystems an opportunity to implement some kind of "copy acceleration", such as reflinks or server-side-copy (in the case of NFS).

---
 man2/copy_file_range.2 | 168 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 168 insertions(+)
 create mode 100644 man2/copy_file_range.2
man2/copy_file_range.2 new file +9 -0
new file mode 100644
index 0000000..4a4cb73
@@ -0,0 +1,168 @@
+.\"This manpage is Copyright (C) 2015 Anna Schumaker <[email protected]>
+.TH COPY 2 2015-8-31 "Linux" "Linux Programmer's Manual"
+.SH NAME
+copy_file_range \- Copy a range of data from one file to another
+.SH SYNOPSIS
+.nf
+.B #include <linux/copy.h>
+.B #include <sys/syscall.h>
+.B #include <unistd.h>

man2/copy_file_range.2 line 19

Quoted 10 lines
man2/copy_file_range.2 new file +10 -0
+
+.BI "ssize_t syscall(__NR_copy_file_range, int " fd_in ", loff_t * " off_in ",
+.BI "                int " fd_out ", loff_t * " off_out ", size_t " len ",
+.BI "                unsigned int " flags );
+.fi
+.SH DESCRIPTION
+The
+.BR copy_file_range ()
+system call performs an in-kernel copy between two file descriptors
+without all that tedious mucking about in userspace.

;)

Quoted 12 lines

man2/copy_file_range.2 line 28

Quoted 9 lines
man2/copy_file_range.2 new file +9 -0
+It copies up to
+.I len
+bytes of data from file descriptor
+.I fd_in
+to file descriptor
+.I fd_out
+at
+.IR off_out .
+The file descriptors must not refer to the same file.

Why? btrfs (and XFS) reflink can handle the case of a file sharing blocks with itself.

I've never really thought about it... Zach had that in his initial submission, so mentioned it in the man page. Should I remove that bit?

Quoted 29 lines
Quoted 29 lines
man2/copy_file_range.2 new file +28 -0
+
+The following semantics apply for
+.IR fd_in ,
+and similar statements apply to
+.IR off_out :
+.IP * 3
+If
+.I off_in
+is NULL, then bytes are read from
+.I fd_in
+starting from the current file offset and the current
+file offset is adjusted appropriately.
+.IP *
+If
+.I off_in
+is not NULL, then
+.I off_in
+must point to a buffer that specifies the starting
+offset where bytes from
+.I fd_in
+will be read.  The current file offset of
+.I fd_in
+is not changed, but
+.I off_in
+is adjusted appropriately.
+.PP
+The default behavior of
+.BR copy_file_range ()
Quoted 8 lines

man2/copy_file_range.2 line 61

Quoted 5 lines
man2/copy_file_range.2 new file +5 -0
+is filesystem specific, and might result in creating a
+copy-on-write reflink.
+In the event that a given filesystem does not implement
+any form of copy acceleration, the kernel will perform
+a deep copy of the requested range by reading bytes from

I wonder if it's wise to allow deep copies -- what happens if len == 1T? Will this syscall just block for a really long time?

We use rw_verify_area(), (similar to read and write) so we won't allow a value of len that long. I can mention this in an updated version of this man page!

Quoted 6 lines

man2/copy_file_range.2 line 64

Quoted 4 lines
man2/copy_file_range.2 new file +3 -0
+.I fd_in
+and writing them to
+.IR fd_out .

"...if COPY_REFLINK is not set in flags."

Sure.

Quoted 17 lines
Quoted 17 lines
man2/copy_file_range.2 new file +16 -0
+
+Currently, Linux only supports the following flag:
+.TP 1.9i
+.B COPY_REFLINK
+Only perform the copy if the filesystem can do it as a reflink.
+Do not fall back on performing a deep copy.
+.SH RETURN VALUE
+Upon successful completion,
+.BR copy_file_range ()
+will return the number of bytes copied between files.
+This could be less than the length originally requested.
+
+On error,
+.BR copy_file_range ()
+returns \-1 and
+.I errno
Quoted 8 lines

man2/copy_file_range.2 line 86

Quoted 6 lines
man2/copy_file_range.2 new file +6 -0
+is set to indicate the error.
+.SH ERRORS
+.TP
+.B EBADF
+One or more file descriptors are not valid,
+or do not have proper read-write mode.

"or fd_out is not opened for writing"?

I'll add that.

Quoted 11 lines
Quoted 11 lines
man2/copy_file_range.2 new file +10 -0
+.TP
+.B EINVAL
+Requested range extends beyond the end of the file;
+.I flags
+argument is set to an invalid value.
+.TP
+.B EOPNOTSUPP
+.B COPY_REFLINK
+was specified in
+.IR flags ,
Quoted 8 lines

man2/copy_file_range.2 line 101

Quoted 5 lines
man2/copy_file_range.2 new file +5 -0
+but the target filesystem does not support reflinks.
+.TP
+.B EXDEV
+Target filesystem doesn't support cross-filesystem copies.
+.SH VERSIONS

Perhaps this ought to list a few more errors (EIO, ENOSPC, ENOSYS, EPERM...) that can be returned? (I was looking at the fallocate manpage.)

Okay. I'll poke around for what else could be returned!

Thanks,
Anna

Quoted 77 lines

--D

Quoted 74 lines
man2/copy_file_range.2 new file +67 -0
+The
+.BR copy_file_range ()
+system call first appeared in Linux 4.3.
+.SH CONFORMING TO
+The
+.BR copy_file_range ()
+system call is a nonstandard Linux extension.
+.SH EXAMPLE
+.nf
+
+#define _GNU_SOURCE
+#include <fcntl.h>
+#include <linux/copy.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <sys/syscall.h>
+#include <unistd.h>
+
+
+int main(int argc, char **argv)
+{
+    int fd_in, fd_out;
+    struct stat stat;
+    loff_t len, ret;
+
+    if (argc != 3) {
+        fprintf(stderr, "Usage: %s <pathname> <pathname>\n", argv[0]);
+        exit(EXIT_FAILURE);
+    }
+
+    fd_in = open(argv[1], O_RDONLY);
+    if (fd_in == -1) {
+        perror("open (argv[1])");
+        exit(EXIT_FAILURE);
+    }
+
+    if (fstat(fd_in, &stat) == -1) {
+        perror("fstat");
+        exit(EXIT_FAILURE);
+    }
+    len = stat.st_size;
+
+    fd_out = open(argv[2], O_WRONLY | O_CREAT, 0644);
+    if (fd_out == -1) {
+        perror("open (argv[2])");
+        exit(EXIT_FAILURE);
+    }
+
+    do {
+        ret = syscall(__NR_copy_file_range, fd_in, NULL,
+                      fd_out, NULL, len, 0);
+        if (ret == -1) {
+            perror("copy_file_range");
+            exit(EXIT_FAILURE);
+        }
+
+        len -= ret;
+    } while (len > 0);
+
+    close(fd_in);
+    close(fd_out);
+    exit(EXIT_SUCCESS);
+}
+.fi
+.SH SEE ALSO
+.BR splice (2)
Signature · 6 lines
2.5.1

--
To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#20 lore raw

Re: [PATCH v1 9/8] copy_file_range.2: New page documenting copy_file_range()

Quoted 54 lines · Anna Schumaker
Quoted 40 lines · Darrick J. Wong
Quoted 26 lines · Anna Schumaker

copy_file_range() is a new system call for copying ranges of data completely in the kernel. This gives filesystems an opportunity to implement some kind of "copy acceleration", such as reflinks or server-side-copy (in the case of NFS).

---
 man2/copy_file_range.2 | 168 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 168 insertions(+)
 create mode 100644 man2/copy_file_range.2
man2/copy_file_range.2 new file +9 -0
new file mode 100644
index 0000000..4a4cb73
@@ -0,0 +1,168 @@
+.\"This manpage is Copyright (C) 2015 Anna Schumaker <[email protected]>
+.TH COPY 2 2015-8-31 "Linux" "Linux Programmer's Manual"
+.SH NAME
+copy_file_range \- Copy a range of data from one file to another
+.SH SYNOPSIS
+.nf
+.B #include <linux/copy.h>
+.B #include <sys/syscall.h>
+.B #include <unistd.h>

man2/copy_file_range.2 line 19

Quoted 10 lines
man2/copy_file_range.2 new file +10 -0
+
+.BI "ssize_t syscall(__NR_copy_file_range, int " fd_in ", loff_t * " off_in ",
+.BI "                int " fd_out ", loff_t * " off_out ", size_t " len ",
+.BI "                unsigned int " flags );
+.fi
+.SH DESCRIPTION
+The
+.BR copy_file_range ()
+system call performs an in-kernel copy between two file descriptors
+without all that tedious mucking about in userspace.

;)

Quoted 12 lines

man2/copy_file_range.2 line 28

Quoted 9 lines
man2/copy_file_range.2 new file +9 -0
+It copies up to
+.I len
+bytes of data from file descriptor
+.I fd_in
+to file descriptor
+.I fd_out
+at
+.IR off_out .
+The file descriptors must not refer to the same file.

Why? btrfs (and XFS) reflink can handle the case of a file sharing blocks with itself.

Quoted 2 lines

I've never really thought about it... Zach had that in his initial submission, so mentioned it in the man page. Should I remove that bit?

Yes, please!

I could be wrong, but I think btrfs only started supporting files that share blocks with themselves relatively recently(?)

I'm not sure why zab added this; was hoping he'd speak up. ;)

Quoted 39 lines
Quoted 30 lines
Quoted 30 lines
man2/copy_file_range.2 new file +28 -0
+
+The following semantics apply for
+.IR fd_in ,
+and similar statements apply to
+.IR off_out :
+.IP * 3
+If
+.I off_in
+is NULL, then bytes are read from
+.I fd_in
+starting from the current file offset and the current
+file offset is adjusted appropriately.
+.IP *
+If
+.I off_in
+is not NULL, then
+.I off_in
+must point to a buffer that specifies the starting
+offset where bytes from
+.I fd_in
+will be read.  The current file offset of
+.I fd_in
+is not changed, but
+.I off_in
+is adjusted appropriately.
+.PP
+The default behavior of
+.BR copy_file_range ()
Quoted 8 lines

man2/copy_file_range.2 line 61

Quoted 5 lines
man2/copy_file_range.2 new file +5 -0
+is filesystem specific, and might result in creating a
+copy-on-write reflink.
+In the event that a given filesystem does not implement
+any form of copy acceleration, the kernel will perform
+a deep copy of the requested range by reading bytes from

I wonder if it's wise to allow deep copies -- what happens if len == 1T? Will this syscall just block for a really long time?

Quoted 3 lines

We use rw_verify_area(), (similar to read and write) so we won't allow a value of len that long. I can mention this in an updated version of this man page!

Ok. I guess MAX_RW_COUNT limits us to about 4G at once, which for a splice copy is probably reasonable.

The reason why I asked about len == 1T specifically is that I can (with somewhat long delays) reflink about 260 million extents at a time on XFS, which is about 1TB. Given that locks get held for the duration, it's probably not a bad thing to limit userspace to 4G at a time.

(But hey, it's fun to stress-test once in a while. :))

--D

Quoted 142 lines
Quoted 8 lines

man2/copy_file_range.2 line 64

Quoted 6 lines
man2/copy_file_range.2 new file +3 -0
+.I fd_in
+and writing them to
+.IR fd_out .

"...if COPY_REFLINK is not set in flags."

Sure.

Quoted 17 lines
Quoted 17 lines
man2/copy_file_range.2 new file +16 -0
+
+Currently, Linux only supports the following flag:
+.TP 1.9i
+.B COPY_REFLINK
+Only perform the copy if the filesystem can do it as a reflink.
+Do not fall back on performing a deep copy.
+.SH RETURN VALUE
+Upon successful completion,
+.BR copy_file_range ()
+will return the number of bytes copied between files.
+This could be less than the length originally requested.
+
+On error,
+.BR copy_file_range ()
+returns \-1 and
+.I errno
Quoted 8 lines

man2/copy_file_range.2 line 86

Quoted 6 lines
man2/copy_file_range.2 new file +6 -0
+is set to indicate the error.
+.SH ERRORS
+.TP
+.B EBADF
+One or more file descriptors are not valid,
+or do not have proper read-write mode.

"or fd_out is not opened for writing"?

I'll add that.

Quoted 11 lines
Quoted 11 lines
man2/copy_file_range.2 new file +10 -0
+.TP
+.B EINVAL
+Requested range extends beyond the end of the file;
+.I flags
+argument is set to an invalid value.
+.TP
+.B EOPNOTSUPP
+.B COPY_REFLINK
+was specified in
+.IR flags ,
Quoted 8 lines

man2/copy_file_range.2 line 101

Quoted 5 lines
man2/copy_file_range.2 new file +5 -0
+but the target filesystem does not support reflinks.
+.TP
+.B EXDEV
+Target filesystem doesn't support cross-filesystem copies.
+.SH VERSIONS

Perhaps this ought to list a few more errors (EIO, ENOSPC, ENOSYS, EPERM...) that can be returned? (I was looking at the fallocate manpage.)

Okay. I'll poke around for what else could be returned!

Thanks,
Anna

Quoted 77 lines

--D

Quoted 74 lines
man2/copy_file_range.2 new file +67 -0
+The
+.BR copy_file_range ()
+system call first appeared in Linux 4.3.
+.SH CONFORMING TO
+The
+.BR copy_file_range ()
+system call is a nonstandard Linux extension.
+.SH EXAMPLE
+.nf
+
+#define _GNU_SOURCE
+#include <fcntl.h>
+#include <linux/copy.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <sys/syscall.h>
+#include <unistd.h>
+
+
+int main(int argc, char **argv)
+{
+    int fd_in, fd_out;
+    struct stat stat;
+    loff_t len, ret;
+
+    if (argc != 3) {
+        fprintf(stderr, "Usage: %s <pathname> <pathname>\n", argv[0]);
+        exit(EXIT_FAILURE);
+    }
+
+    fd_in = open(argv[1], O_RDONLY);
+    if (fd_in == -1) {
+        perror("open (argv[1])");
+        exit(EXIT_FAILURE);
+    }
+
+    if (fstat(fd_in, &stat) == -1) {
+        perror("fstat");
+        exit(EXIT_FAILURE);
+    }
+    len = stat.st_size;
+
+    fd_out = open(argv[2], O_WRONLY | O_CREAT, 0644);
+    if (fd_out == -1) {
+        perror("open (argv[2])");
+        exit(EXIT_FAILURE);
+    }
+
+    do {
+        ret = syscall(__NR_copy_file_range, fd_in, NULL,
+                      fd_out, NULL, len, 0);
+        if (ret == -1) {
+            perror("copy_file_range");
+            exit(EXIT_FAILURE);
+        }
+
+        len -= ret;
+    } while (len > 0);
+
+    close(fd_in);
+    close(fd_out);
+    exit(EXIT_SUCCESS);
+}
+.fi
+.SH SEE ALSO
+.BR splice (2)
Signature · 6 lines
2.5.1

--
To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#21 lore raw

Re: [PATCH v1 9/8] copy_file_range.2: New page documenting copy_file_range()

Quoted 5 lines · Darrick J. Wong
Quoted 5 lines
Quoted 4 lines
Quoted 1 line

+The file descriptors must not refer to the same file.

Why? btrfs (and XFS) reflink can handle the case of a file sharing blocks with itself.

Quoted 7 lines
Quoted 2 lines

I've never really thought about it... Zach had that in his initial submission, so mentioned it in the man page. Should I remove that bit?

Yes, please!

I could be wrong, but I think btrfs only started supporting files that share blocks with themselves relatively recently(?)

The support was added into the cloning ioctl itself, otherwise same-file clones were always possible with a sidestep using another file.

A message that is not in the archive (sent off-list or not yet received): [email protected]

#23 lore raw

Re: [PATCH v1 9/8] copy_file_range.2: New page documenting copy_file_range()

Quoted 108 lines · Darrick J. Wong
Quoted 54 lines · Anna Schumaker
Quoted 40 lines · Darrick J. Wong
Quoted 26 lines · Anna Schumaker

copy_file_range() is a new system call for copying ranges of data completely in the kernel. This gives filesystems an opportunity to implement some kind of "copy acceleration", such as reflinks or server-side-copy (in the case of NFS).

---
  man2/copy_file_range.2 | 168 +++++++++++++++++++++++++++++++++++++++++++++++++
  1 file changed, 168 insertions(+)

create mode 100644 man2/copy_file_range.2

man2/copy_file_range.2 new file +9 -0
new file mode 100644
index 0000000..4a4cb73
@@ -0,0 +1,168 @@
+.\"This manpage is Copyright (C) 2015 Anna Schumaker <[email protected]>
+.TH COPY 2 2015-8-31 "Linux" "Linux Programmer's Manual"
+.SH NAME
+copy_file_range \- Copy a range of data from one file to another
+.SH SYNOPSIS
+.nf
+.B #include <linux/copy.h>
+.B #include <sys/syscall.h>
+.B #include <unistd.h>

man2/copy_file_range.2 line 19

Quoted 10 lines
man2/copy_file_range.2 new file +10 -0
+
+.BI "ssize_t syscall(__NR_copy_file_range, int " fd_in ", loff_t * " off_in ",
+.BI "                int " fd_out ", loff_t * " off_out ", size_t " len ",
+.BI "                unsigned int " flags );
+.fi
+.SH DESCRIPTION
+The
+.BR copy_file_range ()
+system call performs an in-kernel copy between two file descriptors
+without all that tedious mucking about in userspace.

;)

Quoted 12 lines

man2/copy_file_range.2 line 28

Quoted 9 lines
man2/copy_file_range.2 new file +9 -0
+It copies up to
+.I len
+bytes of data from file descriptor
+.I fd_in
+to file descriptor
+.I fd_out
+at
+.IR off_out .
+The file descriptors must not refer to the same file.

Why? btrfs (and XFS) reflink can handle the case of a file sharing blocks with itself.

Quoted 2 lines

I've never really thought about it... Zach had that in his initial submission, so mentioned it in the man page. Should I remove that bit?

Yes, please!

I could be wrong, but I think btrfs only started supporting files that share blocks with themselves relatively recently(?)

I'm not sure why zab added this; was hoping he'd speak up. ;)

Quoted 39 lines
Quoted 30 lines
Quoted 30 lines
man2/copy_file_range.2 new file +28 -0
+
+The following semantics apply for
+.IR fd_in ,
+and similar statements apply to
+.IR off_out :
+.IP * 3
+If
+.I off_in
+is NULL, then bytes are read from
+.I fd_in
+starting from the current file offset and the current
+file offset is adjusted appropriately.
+.IP *
+If
+.I off_in
+is not NULL, then
+.I off_in
+must point to a buffer that specifies the starting
+offset where bytes from
+.I fd_in
+will be read.  The current file offset of
+.I fd_in
+is not changed, but
+.I off_in
+is adjusted appropriately.
+.PP
+The default behavior of
+.BR copy_file_range ()
Quoted 8 lines

man2/copy_file_range.2 line 61

Quoted 5 lines
man2/copy_file_range.2 new file +5 -0
+is filesystem specific, and might result in creating a
+copy-on-write reflink.
+In the event that a given filesystem does not implement
+any form of copy acceleration, the kernel will perform
+a deep copy of the requested range by reading bytes from

I wonder if it's wise to allow deep copies -- what happens if len == 1T? Will this syscall just block for a really long time?

Quoted 3 lines

We use rw_verify_area(), (similar to read and write) so we won't allow a value of len that long. I can mention this in an updated version of this man page!

Quoted 7 lines

Ok. I guess MAX_RW_COUNT limits us to about 4G at once, which for a splice copy is probably reasonable.

The reason why I asked about len == 1T specifically is that I can (with somewhat long delays) reflink about 260 million extents at a time on XFS, which is about 1TB. Given that locks get held for the duration, it's probably not a bad thing to limit userspace to 4G at a time.

I'd personally love to see that be tunable by a sysctl (kind of like how you can control the maximum number of AIO requests in flight), and for that matter we might want to be able to limit the number of in-progress copies going on.

Quoted 152 lines

(But hey, it's fun to stress-test once in a while. :))

--D

Quoted 137 lines
Quoted 8 lines

man2/copy_file_range.2 line 64

Quoted 6 lines
man2/copy_file_range.2 new file +3 -0
+.I fd_in
+and writing them to
+.IR fd_out .

"...if COPY_REFLINK is not set in flags."

Sure.

Quoted 17 lines
Quoted 17 lines
man2/copy_file_range.2 new file +16 -0
+
+Currently, Linux only supports the following flag:
+.TP 1.9i
+.B COPY_REFLINK
+Only perform the copy if the filesystem can do it as a reflink.
+Do not fall back on performing a deep copy.
+.SH RETURN VALUE
+Upon successful completion,
+.BR copy_file_range ()
+will return the number of bytes copied between files.
+This could be less than the length originally requested.
+
+On error,
+.BR copy_file_range ()
+returns \-1 and
+.I errno
Quoted 8 lines

man2/copy_file_range.2 line 86

Quoted 6 lines
man2/copy_file_range.2 new file +6 -0
+is set to indicate the error.
+.SH ERRORS
+.TP
+.B EBADF
+One or more file descriptors are not valid,
+or do not have proper read-write mode.

"or fd_out is not opened for writing"?

I'll add that.

Quoted 11 lines
Quoted 11 lines
man2/copy_file_range.2 new file +10 -0
+.TP
+.B EINVAL
+Requested range extends beyond the end of the file;
+.I flags
+argument is set to an invalid value.
+.TP
+.B EOPNOTSUPP
+.B COPY_REFLINK
+was specified in
+.IR flags ,
Quoted 8 lines

man2/copy_file_range.2 line 101

Quoted 5 lines
man2/copy_file_range.2 new file +5 -0
+but the target filesystem does not support reflinks.
+.TP
+.B EXDEV
+Target filesystem doesn't support cross-filesystem copies.
+.SH VERSIONS

Perhaps this ought to list a few more errors (EIO, ENOSPC, ENOSYS, EPERM...) that can be returned? (I was looking at the fallocate manpage.)

Okay. I'll poke around for what else could be returned!

Thanks,
Anna

Quoted 73 lines

--D

Quoted 70 lines
man2/copy_file_range.2 new file +67 -1
+The
+.BR copy_file_range ()
+system call first appeared in Linux 4.3.
+.SH CONFORMING TO
+The
+.BR copy_file_range ()
+system call is a nonstandard Linux extension.
+.SH EXAMPLE
+.nf
+
+#define _GNU_SOURCE
+#include <fcntl.h>
+#include <linux/copy.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <sys/syscall.h>
+#include <unistd.h>
+
+
+int main(int argc, char **argv)
+{
+    int fd_in, fd_out;
+    struct stat stat;
+    loff_t len, ret;
+
+    if (argc != 3) {
+        fprintf(stderr, "Usage: %s <pathname> <pathname>\n", argv[0]);
+        exit(EXIT_FAILURE);
+    }
+
+    fd_in = open(argv[1], O_RDONLY);
+    if (fd_in == -1) {
+        perror("open (argv[1])");
+        exit(EXIT_FAILURE);
+    }
+
+    if (fstat(fd_in, &stat) == -1) {
+        perror("fstat");
+        exit(EXIT_FAILURE);
+    }
+    len = stat.st_size;
+
+    fd_out = open(argv[2], O_WRONLY | O_CREAT, 0644);
+    if (fd_out == -1) {
+        perror("open (argv[2])");
+        exit(EXIT_FAILURE);
+    }
+
+    do {
+        ret = syscall(__NR_copy_file_range, fd_in, NULL,
+                      fd_out, NULL, len, 0);
+        if (ret == -1) {
+            perror("copy_file_range");
+            exit(EXIT_FAILURE);
+        }
+
+        len -= ret;
+    } while (len > 0);
+
+    close(fd_in);
+    close(fd_out);
+    exit(EXIT_SUCCESS);
+}
+.fi
+.SH SEE ALSO
+.BR splice (2)
--

2.5.1

Quoted 5 lines
Quoted 4 lines
Quoted 4 lines

--
To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#24 lore raw

Re: [PATCH v1 9/8] copy_file_range.2: New page documenting copy_file_range()

Quoted 105 lines · Austin S Hemmelgarn
Quoted 104 lines · Darrick J. Wong
Quoted 54 lines · Anna Schumaker
Quoted 40 lines · Darrick J. Wong
Quoted 26 lines · Anna Schumaker

copy_file_range() is a new system call for copying ranges of data completely in the kernel. This gives filesystems an opportunity to implement some kind of "copy acceleration", such as reflinks or server-side-copy (in the case of NFS).

---
 man2/copy_file_range.2 | 168 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 168 insertions(+)
 create mode 100644 man2/copy_file_range.2
man2/copy_file_range.2 new file +9 -0
new file mode 100644
index 0000000..4a4cb73
@@ -0,0 +1,168 @@
+.\"This manpage is Copyright (C) 2015 Anna Schumaker <[email protected]>
+.TH COPY 2 2015-8-31 "Linux" "Linux Programmer's Manual"
+.SH NAME
+copy_file_range \- Copy a range of data from one file to another
+.SH SYNOPSIS
+.nf
+.B #include <linux/copy.h>
+.B #include <sys/syscall.h>
+.B #include <unistd.h>

man2/copy_file_range.2 line 19

Quoted 10 lines
man2/copy_file_range.2 new file +10 -0
+
+.BI "ssize_t syscall(__NR_copy_file_range, int " fd_in ", loff_t * " off_in ",
+.BI "                int " fd_out ", loff_t * " off_out ", size_t " len ",
+.BI "                unsigned int " flags );
+.fi
+.SH DESCRIPTION
+The
+.BR copy_file_range ()
+system call performs an in-kernel copy between two file descriptors
+without all that tedious mucking about in userspace.

;)

Quoted 12 lines

man2/copy_file_range.2 line 28

Quoted 9 lines
man2/copy_file_range.2 new file +9 -0
+It copies up to
+.I len
+bytes of data from file descriptor
+.I fd_in
+to file descriptor
+.I fd_out
+at
+.IR off_out .
+The file descriptors must not refer to the same file.

Why? btrfs (and XFS) reflink can handle the case of a file sharing blocks with itself.

Quoted 2 lines

I've never really thought about it... Zach had that in his initial submission, so mentioned it in the man page. Should I remove that bit?

Yes, please!

I could be wrong, but I think btrfs only started supporting files that share blocks with themselves relatively recently(?)

I'm not sure why zab added this; was hoping he'd speak up. ;)

Quoted 39 lines
Quoted 38 lines
Quoted 25 lines
man2/copy_file_range.2 new file +23 -0
+
+The following semantics apply for
+.IR fd_in ,
+and similar statements apply to
+.IR off_out :
+.IP * 3
+If
+.I off_in
+is NULL, then bytes are read from
+.I fd_in
+starting from the current file offset and the current
+file offset is adjusted appropriately.
+.IP *
+If
+.I off_in
+is not NULL, then
+.I off_in
+must point to a buffer that specifies the starting
+offset where bytes from
+.I fd_in
+will be read.  The current file offset of
+.I fd_in
+is not changed, but

man2/copy_file_range.2 line 61

Quoted 10 lines
man2/copy_file_range.2 new file +10 -0
+.I off_in
+is adjusted appropriately.
+.PP
+The default behavior of
+.BR copy_file_range ()
+is filesystem specific, and might result in creating a
+copy-on-write reflink.
+In the event that a given filesystem does not implement
+any form of copy acceleration, the kernel will perform
+a deep copy of the requested range by reading bytes from

I wonder if it's wise to allow deep copies -- what happens if len == 1T? Will this syscall just block for a really long time?

Quoted 5 lines
Quoted 5 lines
Quoted 3 lines

We use rw_verify_area(), (similar to read and write) so we won't allow a value of len that long. I can mention this in an updated version of this man page!

Ok. I guess MAX_RW_COUNT limits us to about 4G at once, which for a splice

Heh, INT_MAX, so 2GB at once.

Quoted 11 lines
Quoted 6 lines

copy is probably reasonable.

The reason why I asked about len == 1T specifically is that I can (with somewhat long delays) reflink about 260 million extents at a time on XFS, which is about 1TB. Given that locks get held for the duration, it's probably not a bad thing to limit userspace to 4G at a time.

I'd personally love to see that be tunable by a sysctl (kind of like how you can control the maximum number of AIO requests in flight), and for that matter we might want to be able to limit the number of in-progress copies going on.

Now that I think about it, btrfs' reflink ioctl doesn't seem to have any particular limit on how much you can reflink in a single call. XFS doesn't have a limit either. Given that reflink should create a tiny amount of IO compared to the number of bytes being manipulated, should we allow a higher limit when ssize_t is large enough?

Copy-through-the-pagecache should stick to MAX_RW_COUNT.

I noticed that btrfs won't dedupe more than 16M per call. Any thoughts?

--D

Quoted 154 lines
Quoted 152 lines

(But hey, it's fun to stress-test once in a while. :))

--D

Quoted 137 lines
Quoted 8 lines

man2/copy_file_range.2 line 64

Quoted 6 lines
man2/copy_file_range.2 new file +3 -0
+.I fd_in
+and writing them to
+.IR fd_out .

"...if COPY_REFLINK is not set in flags."

Sure.

Quoted 17 lines
Quoted 17 lines
man2/copy_file_range.2 new file +16 -0
+
+Currently, Linux only supports the following flag:
+.TP 1.9i
+.B COPY_REFLINK
+Only perform the copy if the filesystem can do it as a reflink.
+Do not fall back on performing a deep copy.
+.SH RETURN VALUE
+Upon successful completion,
+.BR copy_file_range ()
+will return the number of bytes copied between files.
+This could be less than the length originally requested.
+
+On error,
+.BR copy_file_range ()
+returns \-1 and
+.I errno
Quoted 8 lines

man2/copy_file_range.2 line 86

Quoted 6 lines
man2/copy_file_range.2 new file +6 -0
+is set to indicate the error.
+.SH ERRORS
+.TP
+.B EBADF
+One or more file descriptors are not valid,
+or do not have proper read-write mode.

"or fd_out is not opened for writing"?

I'll add that.

Quoted 11 lines
Quoted 11 lines
man2/copy_file_range.2 new file +10 -0
+.TP
+.B EINVAL
+Requested range extends beyond the end of the file;
+.I flags
+argument is set to an invalid value.
+.TP
+.B EOPNOTSUPP
+.B COPY_REFLINK
+was specified in
+.IR flags ,
Quoted 8 lines

man2/copy_file_range.2 line 101

Quoted 5 lines
man2/copy_file_range.2 new file +5 -0
+but the target filesystem does not support reflinks.
+.TP
+.B EXDEV
+Target filesystem doesn't support cross-filesystem copies.
+.SH VERSIONS

Perhaps this ought to list a few more errors (EIO, ENOSPC, ENOSYS, EPERM...) that can be returned? (I was looking at the fallocate manpage.)

Okay. I'll poke around for what else could be returned!

Thanks,
Anna

Quoted 73 lines

--D

Quoted 70 lines
man2/copy_file_range.2 new file +67 -1
+The
+.BR copy_file_range ()
+system call first appeared in Linux 4.3.
+.SH CONFORMING TO
+The
+.BR copy_file_range ()
+system call is a nonstandard Linux extension.
+.SH EXAMPLE
+.nf
+
+#define _GNU_SOURCE
+#include <fcntl.h>
+#include <linux/copy.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <sys/syscall.h>
+#include <unistd.h>
+
+
+int main(int argc, char **argv)
+{
+    int fd_in, fd_out;
+    struct stat stat;
+    loff_t len, ret;
+
+    if (argc != 3) {
+        fprintf(stderr, "Usage: %s <pathname> <pathname>\n", argv[0]);
+        exit(EXIT_FAILURE);
+    }
+
+    fd_in = open(argv[1], O_RDONLY);
+    if (fd_in == -1) {
+        perror("open (argv[1])");
+        exit(EXIT_FAILURE);
+    }
+
+    if (fstat(fd_in, &stat) == -1) {
+        perror("fstat");
+        exit(EXIT_FAILURE);
+    }
+    len = stat.st_size;
+
+    fd_out = open(argv[2], O_WRONLY | O_CREAT, 0644);
+    if (fd_out == -1) {
+        perror("open (argv[2])");
+        exit(EXIT_FAILURE);
+    }
+
+    do {
+        ret = syscall(__NR_copy_file_range, fd_in, NULL,
+                      fd_out, NULL, len, 0);
+        if (ret == -1) {
+            perror("copy_file_range");
+            exit(EXIT_FAILURE);
+        }
+
+        len -= ret;
+    } while (len > 0);
+
+    close(fd_in);
+    close(fd_out);
+    exit(EXIT_SUCCESS);
+}
+.fi
+.SH SEE ALSO
+.BR splice (2)
--

2.5.1

Quoted 5 lines
Quoted 4 lines
Quoted 4 lines

--
To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): [email protected]

#26 lore raw

Re: [PATCH v1 9/8] copy_file_range.2: New page documenting copy_file_range()

Quoted 126 lines · Darrick J. Wong
Quoted 105 lines · Austin S Hemmelgarn
Quoted 104 lines · Darrick J. Wong
Quoted 54 lines · Anna Schumaker
Quoted 40 lines · Darrick J. Wong
Quoted 26 lines · Anna Schumaker

copy_file_range() is a new system call for copying ranges of data completely in the kernel. This gives filesystems an opportunity to implement some kind of "copy acceleration", such as reflinks or server-side-copy (in the case of NFS).

---
 man2/copy_file_range.2 | 168 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 168 insertions(+)
 create mode 100644 man2/copy_file_range.2
man2/copy_file_range.2 new file +9 -0
new file mode 100644
index 0000000..4a4cb73
@@ -0,0 +1,168 @@
+.\"This manpage is Copyright (C) 2015 Anna Schumaker <[email protected]>
+.TH COPY 2 2015-8-31 "Linux" "Linux Programmer's Manual"
+.SH NAME
+copy_file_range \- Copy a range of data from one file to another
+.SH SYNOPSIS
+.nf
+.B #include <linux/copy.h>
+.B #include <sys/syscall.h>
+.B #include <unistd.h>

man2/copy_file_range.2 line 19

Quoted 10 lines
man2/copy_file_range.2 new file +10 -0
+
+.BI "ssize_t syscall(__NR_copy_file_range, int " fd_in ", loff_t * " off_in ",
+.BI "                int " fd_out ", loff_t * " off_out ", size_t " len ",
+.BI "                unsigned int " flags );
+.fi
+.SH DESCRIPTION
+The
+.BR copy_file_range ()
+system call performs an in-kernel copy between two file descriptors
+without all that tedious mucking about in userspace.

;)

Quoted 12 lines

man2/copy_file_range.2 line 28

Quoted 9 lines
man2/copy_file_range.2 new file +9 -0
+It copies up to
+.I len
+bytes of data from file descriptor
+.I fd_in
+to file descriptor
+.I fd_out
+at
+.IR off_out .
+The file descriptors must not refer to the same file.

Why? btrfs (and XFS) reflink can handle the case of a file sharing blocks with itself.

Quoted 2 lines

I've never really thought about it... Zach had that in his initial submission, so mentioned it in the man page. Should I remove that bit?

Yes, please!

I could be wrong, but I think btrfs only started supporting files that share blocks with themselves relatively recently(?)

I'm not sure why zab added this; was hoping he'd speak up. ;)

Quoted 39 lines
Quoted 38 lines
Quoted 25 lines
man2/copy_file_range.2 new file +23 -0
+
+The following semantics apply for
+.IR fd_in ,
+and similar statements apply to
+.IR off_out :
+.IP * 3
+If
+.I off_in
+is NULL, then bytes are read from
+.I fd_in
+starting from the current file offset and the current
+file offset is adjusted appropriately.
+.IP *
+If
+.I off_in
+is not NULL, then
+.I off_in
+must point to a buffer that specifies the starting
+offset where bytes from
+.I fd_in
+will be read.  The current file offset of
+.I fd_in
+is not changed, but

man2/copy_file_range.2 line 61

Quoted 10 lines
man2/copy_file_range.2 new file +10 -0
+.I off_in
+is adjusted appropriately.
+.PP
+The default behavior of
+.BR copy_file_range ()
+is filesystem specific, and might result in creating a
+copy-on-write reflink.
+In the event that a given filesystem does not implement
+any form of copy acceleration, the kernel will perform
+a deep copy of the requested range by reading bytes from

I wonder if it's wise to allow deep copies -- what happens if len == 1T? Will this syscall just block for a really long time?

Quoted 5 lines
Quoted 5 lines
Quoted 3 lines

We use rw_verify_area(), (similar to read and write) so we won't allow a value of len that long. I can mention this in an updated version of this man page!

Ok. I guess MAX_RW_COUNT limits us to about 4G at once, which for a splice

Heh, INT_MAX, so 2GB at once.

Quoted 11 lines
Quoted 6 lines

copy is probably reasonable.

The reason why I asked about len == 1T specifically is that I can (with somewhat long delays) reflink about 260 million extents at a time on XFS, which is about 1TB. Given that locks get held for the duration, it's probably not a bad thing to limit userspace to 4G at a time.

I'd personally love to see that be tunable by a sysctl (kind of like how you can control the maximum number of AIO requests in flight), and for that matter we might want to be able to limit the number of in-progress copies going on.

Quoted 7 lines

Now that I think about it, btrfs' reflink ioctl doesn't seem to have any particular limit on how much you can reflink in a single call. XFS doesn't have a limit either. Given that reflink should create a tiny amount of IO compared to the number of bytes being manipulated, should we allow a higher limit when ssize_t is large enough?

Copy-through-the-pagecache should stick to MAX_RW_COUNT.

Should I keep rejecting pagecache copies if len > MAX_RW_COUNT? Or would it be okay to change the value of len to MAX_RW_COUNT in this case?

Anna

Quoted 161 lines

I noticed that btrfs won't dedupe more than 16M per call. Any thoughts?

--D

Quoted 147 lines
Quoted 147 lines

(But hey, it's fun to stress-test once in a while. :))

--D

Quoted 142 lines
Quoted 8 lines

man2/copy_file_range.2 line 64

Quoted 6 lines
man2/copy_file_range.2 new file +3 -0
+.I fd_in
+and writing them to
+.IR fd_out .

"...if COPY_REFLINK is not set in flags."

Sure.

Quoted 17 lines
Quoted 17 lines
man2/copy_file_range.2 new file +16 -0
+
+Currently, Linux only supports the following flag:
+.TP 1.9i
+.B COPY_REFLINK
+Only perform the copy if the filesystem can do it as a reflink.
+Do not fall back on performing a deep copy.
+.SH RETURN VALUE
+Upon successful completion,
+.BR copy_file_range ()
+will return the number of bytes copied between files.
+This could be less than the length originally requested.
+
+On error,
+.BR copy_file_range ()
+returns \-1 and
+.I errno
Quoted 8 lines

man2/copy_file_range.2 line 86

Quoted 6 lines
man2/copy_file_range.2 new file +6 -0
+is set to indicate the error.
+.SH ERRORS
+.TP
+.B EBADF
+One or more file descriptors are not valid,
+or do not have proper read-write mode.

"or fd_out is not opened for writing"?

I'll add that.

Quoted 11 lines
Quoted 11 lines
man2/copy_file_range.2 new file +10 -0
+.TP
+.B EINVAL
+Requested range extends beyond the end of the file;
+.I flags
+argument is set to an invalid value.
+.TP
+.B EOPNOTSUPP
+.B COPY_REFLINK
+was specified in
+.IR flags ,
Quoted 8 lines

man2/copy_file_range.2 line 101

Quoted 5 lines
man2/copy_file_range.2 new file +5 -0
+but the target filesystem does not support reflinks.
+.TP
+.B EXDEV
+Target filesystem doesn't support cross-filesystem copies.
+.SH VERSIONS

Perhaps this ought to list a few more errors (EIO, ENOSPC, ENOSYS, EPERM...) that can be returned? (I was looking at the fallocate manpage.)

Okay. I'll poke around for what else could be returned!

Thanks,
Anna

Quoted 77 lines

--D

Quoted 74 lines
man2/copy_file_range.2 new file +67 -1
+The
+.BR copy_file_range ()
+system call first appeared in Linux 4.3.
+.SH CONFORMING TO
+The
+.BR copy_file_range ()
+system call is a nonstandard Linux extension.
+.SH EXAMPLE
+.nf
+
+#define _GNU_SOURCE
+#include <fcntl.h>
+#include <linux/copy.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <sys/syscall.h>
+#include <unistd.h>
+
+
+int main(int argc, char **argv)
+{
+    int fd_in, fd_out;
+    struct stat stat;
+    loff_t len, ret;
+
+    if (argc != 3) {
+        fprintf(stderr, "Usage: %s <pathname> <pathname>\n", argv[0]);
+        exit(EXIT_FAILURE);
+    }
+
+    fd_in = open(argv[1], O_RDONLY);
+    if (fd_in == -1) {
+        perror("open (argv[1])");
+        exit(EXIT_FAILURE);
+    }
+
+    if (fstat(fd_in, &stat) == -1) {
+        perror("fstat");
+        exit(EXIT_FAILURE);
+    }
+    len = stat.st_size;
+
+    fd_out = open(argv[2], O_WRONLY | O_CREAT, 0644);
+    if (fd_out == -1) {
+        perror("open (argv[2])");
+        exit(EXIT_FAILURE);
+    }
+
+    do {
+        ret = syscall(__NR_copy_file_range, fd_in, NULL,
+                      fd_out, NULL, len, 0);
+        if (ret == -1) {
+            perror("copy_file_range");
+            exit(EXIT_FAILURE);
+        }
+
+        len -= ret;
+    } while (len > 0);
+
+    close(fd_in);
+    close(fd_out);
+    exit(EXIT_SUCCESS);
+}
+.fi
+.SH SEE ALSO
+.BR splice (2)
--

2.5.1

--
To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

Quoted 7 lines
Quoted 5 lines

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

Quoted 5 lines

--
To unsubscribe from this list: send the line "unsubscribe linux-nfs" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

--
To unsubscribe from this list: send the line "unsubscribe linux-nfs" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): [email protected]

#28 lore raw

Re: [PATCH v1 9/8] copy_file_range.2: New page documenting copy_file_range()

Quoted 133 lines · Anna Schumaker
Quoted 132 lines · Darrick J. Wong
Quoted 105 lines · Austin S Hemmelgarn
Quoted 104 lines · Darrick J. Wong
Quoted 54 lines · Anna Schumaker
Quoted 40 lines · Darrick J. Wong
Quoted 26 lines · Anna Schumaker

copy_file_range() is a new system call for copying ranges of data completely in the kernel. This gives filesystems an opportunity to implement some kind of "copy acceleration", such as reflinks or server-side-copy (in the case of NFS).

---
 man2/copy_file_range.2 | 168 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 168 insertions(+)
 create mode 100644 man2/copy_file_range.2
man2/copy_file_range.2 new file +9 -0
new file mode 100644
index 0000000..4a4cb73
@@ -0,0 +1,168 @@
+.\"This manpage is Copyright (C) 2015 Anna Schumaker <[email protected]>
+.TH COPY 2 2015-8-31 "Linux" "Linux Programmer's Manual"
+.SH NAME
+copy_file_range \- Copy a range of data from one file to another
+.SH SYNOPSIS
+.nf
+.B #include <linux/copy.h>
+.B #include <sys/syscall.h>
+.B #include <unistd.h>

man2/copy_file_range.2 line 19

Quoted 10 lines
man2/copy_file_range.2 new file +10 -0
+
+.BI "ssize_t syscall(__NR_copy_file_range, int " fd_in ", loff_t * " off_in ",
+.BI "                int " fd_out ", loff_t * " off_out ", size_t " len ",
+.BI "                unsigned int " flags );
+.fi
+.SH DESCRIPTION
+The
+.BR copy_file_range ()
+system call performs an in-kernel copy between two file descriptors
+without all that tedious mucking about in userspace.

;)

Quoted 12 lines

man2/copy_file_range.2 line 28

Quoted 9 lines
man2/copy_file_range.2 new file +9 -0
+It copies up to
+.I len
+bytes of data from file descriptor
+.I fd_in
+to file descriptor
+.I fd_out
+at
+.IR off_out .
+The file descriptors must not refer to the same file.

Why? btrfs (and XFS) reflink can handle the case of a file sharing blocks with itself.

Quoted 2 lines

I've never really thought about it... Zach had that in his initial submission, so mentioned it in the man page. Should I remove that bit?

Yes, please!

I could be wrong, but I think btrfs only started supporting files that share blocks with themselves relatively recently(?)

I'm not sure why zab added this; was hoping he'd speak up. ;)

Quoted 39 lines
Quoted 38 lines
Quoted 25 lines
man2/copy_file_range.2 new file +23 -0
+
+The following semantics apply for
+.IR fd_in ,
+and similar statements apply to
+.IR off_out :
+.IP * 3
+If
+.I off_in
+is NULL, then bytes are read from
+.I fd_in
+starting from the current file offset and the current
+file offset is adjusted appropriately.
+.IP *
+If
+.I off_in
+is not NULL, then
+.I off_in
+must point to a buffer that specifies the starting
+offset where bytes from
+.I fd_in
+will be read.  The current file offset of
+.I fd_in
+is not changed, but

man2/copy_file_range.2 line 61

Quoted 10 lines
man2/copy_file_range.2 new file +10 -0
+.I off_in
+is adjusted appropriately.
+.PP
+The default behavior of
+.BR copy_file_range ()
+is filesystem specific, and might result in creating a
+copy-on-write reflink.
+In the event that a given filesystem does not implement
+any form of copy acceleration, the kernel will perform
+a deep copy of the requested range by reading bytes from

I wonder if it's wise to allow deep copies -- what happens if len == 1T? Will this syscall just block for a really long time?

Quoted 5 lines
Quoted 5 lines
Quoted 3 lines

We use rw_verify_area(), (similar to read and write) so we won't allow a value of len that long. I can mention this in an updated version of this man page!

Ok. I guess MAX_RW_COUNT limits us to about 4G at once, which for a splice

Heh, INT_MAX, so 2GB at once.

Quoted 11 lines
Quoted 6 lines

copy is probably reasonable.

The reason why I asked about len == 1T specifically is that I can (with somewhat long delays) reflink about 260 million extents at a time on XFS, which is about 1TB. Given that locks get held for the duration, it's probably not a bad thing to limit userspace to 4G at a time.

I'd personally love to see that be tunable by a sysctl (kind of like how you can control the maximum number of AIO requests in flight), and for that matter we might want to be able to limit the number of in-progress copies going on.

Now that I think about it, btrfs' reflink ioctl doesn't seem to have any particular limit on how much you can reflink in a single call. XFS doesn't have a limit either. Given that reflink should create a tiny amount of IO compared to the number of bytes being manipulated, should we allow a higher limit when ssize_t is large enough?

Quoted 4 lines
Quoted 1 line

Copy-through-the-pagecache should stick to MAX_RW_COUNT.

Should I keep rejecting pagecache copies if len > MAX_RW_COUNT? Or would it be okay to change the value of len to MAX_RW_COUNT in this case?

OH. Heh.

rw_verify_area returns either an error code or a len that's been clamped to MAX_RW_COUNT. However, the syscall code only checks for errors, and otherwise ignores the clamp. So I guess the length has never been clamped.

Since the syscall returns ssize_t, I think it's fine to keep around the return value from rw_verify_area and use it to clamp len if we have to fall back on pagecache copy. Otherwise we'll let each FS' copy routine decide its maximum.

--D

Quoted 170 lines

Anna

Quoted 166 lines

I noticed that btrfs won't dedupe more than 16M per call. Any thoughts?

--D

Quoted 147 lines
Quoted 147 lines

(But hey, it's fun to stress-test once in a while. :))

--D

Quoted 142 lines
Quoted 8 lines

man2/copy_file_range.2 line 64

Quoted 6 lines
man2/copy_file_range.2 new file +3 -0
+.I fd_in
+and writing them to
+.IR fd_out .

"...if COPY_REFLINK is not set in flags."

Sure.

Quoted 17 lines
Quoted 17 lines
man2/copy_file_range.2 new file +16 -0
+
+Currently, Linux only supports the following flag:
+.TP 1.9i
+.B COPY_REFLINK
+Only perform the copy if the filesystem can do it as a reflink.
+Do not fall back on performing a deep copy.
+.SH RETURN VALUE
+Upon successful completion,
+.BR copy_file_range ()
+will return the number of bytes copied between files.
+This could be less than the length originally requested.
+
+On error,
+.BR copy_file_range ()
+returns \-1 and
+.I errno
Quoted 8 lines

man2/copy_file_range.2 line 86

Quoted 6 lines
man2/copy_file_range.2 new file +6 -0
+is set to indicate the error.
+.SH ERRORS
+.TP
+.B EBADF
+One or more file descriptors are not valid,
+or do not have proper read-write mode.

"or fd_out is not opened for writing"?

I'll add that.

Quoted 11 lines
Quoted 11 lines
man2/copy_file_range.2 new file +10 -0
+.TP
+.B EINVAL
+Requested range extends beyond the end of the file;
+.I flags
+argument is set to an invalid value.
+.TP
+.B EOPNOTSUPP
+.B COPY_REFLINK
+was specified in
+.IR flags ,
Quoted 8 lines

man2/copy_file_range.2 line 101

Quoted 5 lines
man2/copy_file_range.2 new file +5 -0
+but the target filesystem does not support reflinks.
+.TP
+.B EXDEV
+Target filesystem doesn't support cross-filesystem copies.
+.SH VERSIONS

Perhaps this ought to list a few more errors (EIO, ENOSPC, ENOSYS, EPERM...) that can be returned? (I was looking at the fallocate manpage.)

Okay. I'll poke around for what else could be returned!

Thanks,
Anna

Quoted 77 lines

--D

Quoted 74 lines
man2/copy_file_range.2 new file +67 -1
+The
+.BR copy_file_range ()
+system call first appeared in Linux 4.3.
+.SH CONFORMING TO
+The
+.BR copy_file_range ()
+system call is a nonstandard Linux extension.
+.SH EXAMPLE
+.nf
+
+#define _GNU_SOURCE
+#include <fcntl.h>
+#include <linux/copy.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <sys/syscall.h>
+#include <unistd.h>
+
+
+int main(int argc, char **argv)
+{
+    int fd_in, fd_out;
+    struct stat stat;
+    loff_t len, ret;
+
+    if (argc != 3) {
+        fprintf(stderr, "Usage: %s <pathname> <pathname>\n", argv[0]);
+        exit(EXIT_FAILURE);
+    }
+
+    fd_in = open(argv[1], O_RDONLY);
+    if (fd_in == -1) {
+        perror("open (argv[1])");
+        exit(EXIT_FAILURE);
+    }
+
+    if (fstat(fd_in, &stat) == -1) {
+        perror("fstat");
+        exit(EXIT_FAILURE);
+    }
+    len = stat.st_size;
+
+    fd_out = open(argv[2], O_WRONLY | O_CREAT, 0644);
+    if (fd_out == -1) {
+        perror("open (argv[2])");
+        exit(EXIT_FAILURE);
+    }
+
+    do {
+        ret = syscall(__NR_copy_file_range, fd_in, NULL,
+                      fd_out, NULL, len, 0);
+        if (ret == -1) {
+            perror("copy_file_range");
+            exit(EXIT_FAILURE);
+        }
+
+        len -= ret;
+    } while (len > 0);
+
+    close(fd_in);
+    close(fd_out);
+    exit(EXIT_SUCCESS);
+}
+.fi
+.SH SEE ALSO
+.BR splice (2)
--

2.5.1

--
To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

Quoted 7 lines
Quoted 5 lines

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

--
To unsubscribe from this list: send the line "unsubscribe linux-nfs" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#29 lore raw

Re: [PATCH v1 9/8] copy_file_range.2: New page documenting copy_file_range()

Quoted 141 lines · Darrick J. Wong
Quoted 133 lines · Anna Schumaker
Quoted 132 lines · Darrick J. Wong
Quoted 105 lines · Austin S Hemmelgarn
Quoted 104 lines · Darrick J. Wong
Quoted 54 lines · Anna Schumaker
Quoted 40 lines · Darrick J. Wong
Quoted 26 lines · Anna Schumaker

copy_file_range() is a new system call for copying ranges of data completely in the kernel. This gives filesystems an opportunity to implement some kind of "copy acceleration", such as reflinks or server-side-copy (in the case of NFS).

---
 man2/copy_file_range.2 | 168 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 168 insertions(+)
 create mode 100644 man2/copy_file_range.2
man2/copy_file_range.2 new file +9 -0
new file mode 100644
index 0000000..4a4cb73
@@ -0,0 +1,168 @@
+.\"This manpage is Copyright (C) 2015 Anna Schumaker <[email protected]>
+.TH COPY 2 2015-8-31 "Linux" "Linux Programmer's Manual"
+.SH NAME
+copy_file_range \- Copy a range of data from one file to another
+.SH SYNOPSIS
+.nf
+.B #include <linux/copy.h>
+.B #include <sys/syscall.h>
+.B #include <unistd.h>

man2/copy_file_range.2 line 19

Quoted 10 lines
man2/copy_file_range.2 new file +10 -0
+
+.BI "ssize_t syscall(__NR_copy_file_range, int " fd_in ", loff_t * " off_in ",
+.BI "                int " fd_out ", loff_t * " off_out ", size_t " len ",
+.BI "                unsigned int " flags );
+.fi
+.SH DESCRIPTION
+The
+.BR copy_file_range ()
+system call performs an in-kernel copy between two file descriptors
+without all that tedious mucking about in userspace.

;)

Quoted 12 lines

man2/copy_file_range.2 line 28

Quoted 9 lines
man2/copy_file_range.2 new file +9 -0
+It copies up to
+.I len
+bytes of data from file descriptor
+.I fd_in
+to file descriptor
+.I fd_out
+at
+.IR off_out .
+The file descriptors must not refer to the same file.

Why? btrfs (and XFS) reflink can handle the case of a file sharing blocks with itself.

Quoted 2 lines

I've never really thought about it... Zach had that in his initial submission, so mentioned it in the man page. Should I remove that bit?

Yes, please!

I could be wrong, but I think btrfs only started supporting files that share blocks with themselves relatively recently(?)

I'm not sure why zab added this; was hoping he'd speak up. ;)

Quoted 39 lines
Quoted 38 lines
Quoted 25 lines
man2/copy_file_range.2 new file +23 -0
+
+The following semantics apply for
+.IR fd_in ,
+and similar statements apply to
+.IR off_out :
+.IP * 3
+If
+.I off_in
+is NULL, then bytes are read from
+.I fd_in
+starting from the current file offset and the current
+file offset is adjusted appropriately.
+.IP *
+If
+.I off_in
+is not NULL, then
+.I off_in
+must point to a buffer that specifies the starting
+offset where bytes from
+.I fd_in
+will be read.  The current file offset of
+.I fd_in
+is not changed, but

man2/copy_file_range.2 line 61

Quoted 10 lines
man2/copy_file_range.2 new file +10 -0
+.I off_in
+is adjusted appropriately.
+.PP
+The default behavior of
+.BR copy_file_range ()
+is filesystem specific, and might result in creating a
+copy-on-write reflink.
+In the event that a given filesystem does not implement
+any form of copy acceleration, the kernel will perform
+a deep copy of the requested range by reading bytes from

I wonder if it's wise to allow deep copies -- what happens if len == 1T? Will this syscall just block for a really long time?

Quoted 5 lines
Quoted 5 lines
Quoted 3 lines

We use rw_verify_area(), (similar to read and write) so we won't allow a value of len that long. I can mention this in an updated version of this man page!

Ok. I guess MAX_RW_COUNT limits us to about 4G at once, which for a splice

Heh, INT_MAX, so 2GB at once.

Quoted 11 lines
Quoted 6 lines

copy is probably reasonable.

The reason why I asked about len == 1T specifically is that I can (with somewhat long delays) reflink about 260 million extents at a time on XFS, which is about 1TB. Given that locks get held for the duration, it's probably not a bad thing to limit userspace to 4G at a time.

I'd personally love to see that be tunable by a sysctl (kind of like how you can control the maximum number of AIO requests in flight), and for that matter we might want to be able to limit the number of in-progress copies going on.

Now that I think about it, btrfs' reflink ioctl doesn't seem to have any particular limit on how much you can reflink in a single call. XFS doesn't have a limit either. Given that reflink should create a tiny amount of IO compared to the number of bytes being manipulated, should we allow a higher limit when ssize_t is large enough?

Quoted 4 lines
Quoted 1 line

Copy-through-the-pagecache should stick to MAX_RW_COUNT.

Should I keep rejecting pagecache copies if len > MAX_RW_COUNT? Or would it be okay to change the value of len to MAX_RW_COUNT in this case?

OH. Heh.

Quoted 7 lines

rw_verify_area returns either an error code or a len that's been clamped to MAX_RW_COUNT. However, the syscall code only checks for errors, and otherwise ignores the clamp. So I guess the length has never been clamped.

Since the syscall returns ssize_t, I think it's fine to keep around the return value from rw_verify_area and use it to clamp len if we have to fall back on pagecache copy. Otherwise we'll let each FS' copy routine decide its maximum.

Okay. I'll use the return value to clamp the copy length, but move this code so that it's only for pagecache copies. Thanks!

Anna

Quoted 173 lines

--D

Quoted 170 lines

Anna

Quoted 166 lines

I noticed that btrfs won't dedupe more than 16M per call. Any thoughts?

--D

Quoted 147 lines
Quoted 147 lines

(But hey, it's fun to stress-test once in a while. :))

--D

Quoted 142 lines
Quoted 8 lines

man2/copy_file_range.2 line 64

Quoted 6 lines
man2/copy_file_range.2 new file +3 -0
+.I fd_in
+and writing them to
+.IR fd_out .

"...if COPY_REFLINK is not set in flags."

Sure.

Quoted 17 lines
Quoted 17 lines
man2/copy_file_range.2 new file +16 -0
+
+Currently, Linux only supports the following flag:
+.TP 1.9i
+.B COPY_REFLINK
+Only perform the copy if the filesystem can do it as a reflink.
+Do not fall back on performing a deep copy.
+.SH RETURN VALUE
+Upon successful completion,
+.BR copy_file_range ()
+will return the number of bytes copied between files.
+This could be less than the length originally requested.
+
+On error,
+.BR copy_file_range ()
+returns \-1 and
+.I errno
Quoted 8 lines

man2/copy_file_range.2 line 86

Quoted 6 lines
man2/copy_file_range.2 new file +6 -0
+is set to indicate the error.
+.SH ERRORS
+.TP
+.B EBADF
+One or more file descriptors are not valid,
+or do not have proper read-write mode.

"or fd_out is not opened for writing"?

I'll add that.

Quoted 11 lines
Quoted 11 lines
man2/copy_file_range.2 new file +10 -0
+.TP
+.B EINVAL
+Requested range extends beyond the end of the file;
+.I flags
+argument is set to an invalid value.
+.TP
+.B EOPNOTSUPP
+.B COPY_REFLINK
+was specified in
+.IR flags ,
Quoted 8 lines

man2/copy_file_range.2 line 101

Quoted 5 lines
man2/copy_file_range.2 new file +5 -0
+but the target filesystem does not support reflinks.
+.TP
+.B EXDEV
+Target filesystem doesn't support cross-filesystem copies.
+.SH VERSIONS

Perhaps this ought to list a few more errors (EIO, ENOSPC, ENOSYS, EPERM...) that can be returned? (I was looking at the fallocate manpage.)

Okay. I'll poke around for what else could be returned!

Thanks,
Anna

Quoted 77 lines

--D

Quoted 74 lines
man2/copy_file_range.2 new file +67 -1
+The
+.BR copy_file_range ()
+system call first appeared in Linux 4.3.
+.SH CONFORMING TO
+The
+.BR copy_file_range ()
+system call is a nonstandard Linux extension.
+.SH EXAMPLE
+.nf
+
+#define _GNU_SOURCE
+#include <fcntl.h>
+#include <linux/copy.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <sys/syscall.h>
+#include <unistd.h>
+
+
+int main(int argc, char **argv)
+{
+    int fd_in, fd_out;
+    struct stat stat;
+    loff_t len, ret;
+
+    if (argc != 3) {
+        fprintf(stderr, "Usage: %s <pathname> <pathname>\n", argv[0]);
+        exit(EXIT_FAILURE);
+    }
+
+    fd_in = open(argv[1], O_RDONLY);
+    if (fd_in == -1) {
+        perror("open (argv[1])");
+        exit(EXIT_FAILURE);
+    }
+
+    if (fstat(fd_in, &stat) == -1) {
+        perror("fstat");
+        exit(EXIT_FAILURE);
+    }
+    len = stat.st_size;
+
+    fd_out = open(argv[2], O_WRONLY | O_CREAT, 0644);
+    if (fd_out == -1) {
+        perror("open (argv[2])");
+        exit(EXIT_FAILURE);
+    }
+
+    do {
+        ret = syscall(__NR_copy_file_range, fd_in, NULL,
+                      fd_out, NULL, len, 0);
+        if (ret == -1) {
+            perror("copy_file_range");
+            exit(EXIT_FAILURE);
+        }
+
+        len -= ret;
+    } while (len > 0);
+
+    close(fd_in);
+    close(fd_out);
+    exit(EXIT_SUCCESS);
+}
+.fi
+.SH SEE ALSO
+.BR splice (2)
--

2.5.1

--
To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Quoted 7 lines
Quoted 5 lines

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

--
To unsubscribe from this list: send the line "unsubscribe linux-nfs" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#30 lore raw

Re: [PATCH v1 9/8] copy_file_range.2: New page documenting copy_file_range()

In reply to a message not in the archive
Quoted 1 line · Darrick J. Wong

I noticed that btrfs won't dedupe more than 16M per call. Any thoughts?

btrfs_ioctl_file_extent_same:

3138 /*

3139          * Limit the total length we will dedupe for each operation.
3140          * This is intended to bound the total time spent in this
3141          * ioctl to something sane.
3142          */

3143 if (len > BTRFS_MAX_DEDUPE_LEN)
3144 len = BTRFS_MAX_DEDUPE_LEN;

The deduplication compares the source and destination blocks and does not use the checksum based approach (btrfs_cmp_data()). The 16M limit is artifical, I don't have an estimate whether the value is ok or not. The longer dedupe chunk the lower the chance to find more matching extents, so the practially used chunk sizes are in range of hundreds of kilobytes. But this obviously depends on data and many-megabyte-sized chunks could fit some usecases easily.

A message that is not in the archive (sent off-list or not yet received): [email protected]

#32 lore raw

Re: [PATCH v1 9/8] copy_file_range.2: New page documenting copy_file_range()

Quoted 13 lines · David Sterba
Quoted 1 line · Darrick J. Wong

I noticed that btrfs won't dedupe more than 16M per call. Any thoughts?

btrfs_ioctl_file_extent_same:

3138 /*

3139          * Limit the total length we will dedupe for each operation.
3140          * This is intended to bound the total time spent in this
3141          * ioctl to something sane.
3142          */

3143 if (len > BTRFS_MAX_DEDUPE_LEN)
3144 len = BTRFS_MAX_DEDUPE_LEN;

Quoted 7 lines

The deduplication compares the source and destination blocks and does not use the checksum based approach (btrfs_cmp_data()). The 16M limit is artifical, I don't have an estimate whether the value is ok or not. The longer dedupe chunk the lower the chance to find more matching extents, so the practially used chunk sizes are in range of hundreds of kilobytes. But this obviously depends on data and many-megabyte-sized chunks could fit some usecases easily.

I guessed that 16M was a 'reasonable default maximum' since the semantics seem to be "link these two ranges together if all block contents match", not "I think these ranges match, link together any blocks which actually /do/ match". Personally, I doubt that it'll often be the case that a dedupe tool finds >16M chunks to dedupe *and* for whatever reason can't just call iteratively.

Internally it could do some fadvise-like magic to avoid polluting the page cache with the compares, but I agree that not letting the call take forever is a good thing.

Oh well. It /could/ be a per-fs tunable if anyone yells loudly.

--D

#33 lore raw
In reply to a message not in the archive
Quoted 35 lines · Anna Schumaker

Copy system calls came up during Plumbers a couple of weeks ago, because several filesystems (including NFS and XFS) are currently working on copy acceleration implementations. We haven't heard from Zach Brown in a while, so I volunteered to push his patches upstream so individual filesystems don't need to keep writing their own ioctls.

The first three patches are a simple reposting of Zach's patches from several months ago, with one minor error code fix. The remaining patches add in a fallback mechanism when filesystems don't provide a copy function. This is especially useful when doing a server-side copy on NFS (using the new COPY operation in NFS v4.2). This fallback can be disabled by passing the flag COPY_REFLINK to the system call.

The last patch is a man page patch documenting this new system call, including an example program.

I tested the fallback option by using /dev/urandom to generate files of varying sizes and copying them. I compared the time to copy against that of cp just to see if there is a noticable difference. I found that runtimes are roughly the same, but in-kernel copy tends to use less of the cpu. Values in the tables below are averages across multiple trials.

/usr/bin/cp | 512 MB | 1024 MB | 1536 MB | 2048 MB
-------------|-----------|-----------|-----------|-----------

      user  |   0.00s   |   0.00s   |   0.00s   |   0.00s
    system  |   0.32s   |   0.52s   |   1.04s   |   1.04s
       cpu  |     73%   |     69%   |     62%   |     62%
     total  |   0.446   |   0.757   |   1.197   |   1.667

VFS copy | 512 MB | 1024 MB | 1536 MB | 2048 MB
-------------|-----------|-----------|-----------|-----------

Quoted 7 lines
      user  |   0.00s   |   0.00s   |   0.00s   |  0.00s
    system  |   0.33s   |   0.49s   |   0.76s   |  0.99s
       cpu  |     77%   |     62%   |     60%   |    59%
     total  |   0.422   |   0.777   |   1.267   |  1.655

Questions? Comments? Thoughts?

This is a bit of a surprising result, since in my testing in the past, copy_{to/from}_user() is a major consumer of CPU time (50% of a CPU core at 1GB/s). What backing filesystem did you test on?

In theory, the VFS copy routines should save at least 50% of the CPU usage since it only needs to make one copy (src->dest) instead of two (kernel->user, user->kernel). Ideally it wouldn't make any data copies at all and just pass page references from the source to the target.

Cheers, Andreas

Quoted 29 lines

Anna

Anna Schumaker (5):
btrfs: Add mountpoint checking during btrfs_copy_file_range
vfs: Remove copy_file_range mountpoint checks
vfs: Copy should check len after file open mode
vfs: Copy should use file_out rather than file_in
vfs: Fall back on splice if no copy function defined

Zach Brown (3):
vfs: add copy_file_range syscall and vfs helper
x86: add sys_copy_file_range to syscall tables
btrfs: add .copy_file_range file operation

arch/x86/entry/syscalls/syscall_32.tbl |   1 +
arch/x86/entry/syscalls/syscall_64.tbl |   1 +
fs/btrfs/ctree.h                       |   3 +
fs/btrfs/file.c                        |   1 +
fs/btrfs/ioctl.c                       |  95 ++++++++++++++----------
fs/read_write.c                        | 132 +++++++++++++++++++++++++++++++++
include/linux/copy.h                   |   6 ++
include/linux/fs.h                     |   3 +
include/uapi/asm-generic/unistd.h      |   4 +-
include/uapi/linux/Kbuild              |   1 +
include/uapi/linux/copy.h              |   6 ++
kernel/sys_ni.c                        |   1 +
12 files changed, 214 insertions(+), 40 deletions(-)
Quoted 10 lines

create mode 100644 include/linux/copy.h
create mode 100644 include/uapi/linux/copy.h

Signature · 6 lines
2.5.1

--
To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Cheers, Andreas

A message that is not in the archive (sent off-list or not yet received): 4B41043F-5D85-42D6-8F20-2DCC45930EF4-m1MBpc4rdrD3fQ9qLvQP4Q@public.gmane.org

#35 lore raw
Quoted 3 lines · Andreas Dilger

This is a bit of a surprising result, since in my testing in the past, copy_{to/from}_user() is a major consumer of CPU time (50% of a CPU core at 1GB/s). What backing filesystem did you test on?

While we are at it, was cp(1) using read(2)/write(2) loop or was it using something else (sendfile(2), for example)?

A message that is not in the archive (sent off-list or not yet received): 20150905083342.GG22011-3bDd1+5oDREiFSDQTTA3OLVCufUGDwFn@public.gmane.org

#37 lore raw
Quoted 8 lines · Al Viro
Quoted 3 lines · Andreas Dilger

This is a bit of a surprising result, since in my testing in the past, copy_{to/from}_user() is a major consumer of CPU time (50% of a CPU core at 1GB/s). What backing filesystem did you test on?

While we are at it, was cp(1) using read(2)/write(2) loop or was it using something else (sendfile(2), for example)?

cp uses a read / write loop, and has some heuristics for guessing an optimum buffer size.

Anna

Quoted 1 line
#38 lore raw
Quoted 11 lines · Anna Schumaker
Quoted 8 lines · Al Viro
Quoted 3 lines · Andreas Dilger

This is a bit of a surprising result, since in my testing in the past, copy_{to/from}_user() is a major consumer of CPU time (50% of a CPU core at 1GB/s). What backing filesystem did you test on?

While we are at it, was cp(1) using read(2)/write(2) loop or was it using something else (sendfile(2), for example)?

cp uses a read / write loop, and has some heuristics for guessing an optimum buffer size.

..but afaict cp doesn't fsync at the end, which means it's possible that the destination file's blocks are still delalloc and nothing's been flushed to disk yet. What happens if you time (cp /tmp/a /tmp/b ; sync) ?

2048M / 1.667s = ~1200MB/s.

--D

Quoted 5 lines

Anna

Quoted 1 line

A message that is not in the archive (sent off-list or not yet received): [email protected]

#40 lore raw
Quoted 8 lines · Darrick J. Wong
Quoted 7 lines · Anna Schumaker
Quoted 6 lines · Al Viro
Quoted 3 lines · Andreas Dilger

This is a bit of a surprising result, since in my testing in the past, copy_{to/from}_user() is a major consumer of CPU time (50% of a CPU core at 1GB/s). What backing filesystem did you test on?

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

While we are at it, was cp(1) using read(2)/write(2) loop or was it using something else (sendfile(2), for example)?

cp uses a read / write loop, and has some heuristics for guessing an optimum buffer size.

..but afaict cp doesn't fsync at the end, which means it's possible that the destination file's blocks are still delalloc and nothing's been flushed to disk yet. What happens if you time (cp /tmp/a /tmp/b ; sync) ?

That's already how I was using cp :). The example program in my man page also doesn't fsync at the end, so the extra sync at the end is needed for both.

Anna

Quoted 10 lines

2048M / 1.667s = ~1200MB/s.

--D

Quoted 5 lines

Anna

Quoted 1 line

--
To unsubscribe from this list: send the line "unsubscribe linux-nfs" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#41 lore raw
In reply to Andreas Dilger
Quoted 42 lines · Andreas Dilger
Quoted 41 lines · Anna Schumaker

Copy system calls came up during Plumbers a couple of weeks ago, because several filesystems (including NFS and XFS) are currently working on copy acceleration implementations. We haven't heard from Zach Brown in a while, so I volunteered to push his patches upstream so individual filesystems don't need to keep writing their own ioctls.

The first three patches are a simple reposting of Zach's patches from several months ago, with one minor error code fix. The remaining patches add in a fallback mechanism when filesystems don't provide a copy function. This is especially useful when doing a server-side copy on NFS (using the new COPY operation in NFS v4.2). This fallback can be disabled by passing the flag COPY_REFLINK to the system call.

The last patch is a man page patch documenting this new system call, including an example program.

I tested the fallback option by using /dev/urandom to generate files of varying sizes and copying them. I compared the time to copy against that of cp just to see if there is a noticable difference. I found that runtimes are roughly the same, but in-kernel copy tends to use less of the cpu. Values in the tables below are averages across multiple trials.

/usr/bin/cp | 512 MB | 1024 MB | 1536 MB | 2048 MB
-------------|-----------|-----------|-----------|-----------

      user  |   0.00s   |   0.00s   |   0.00s   |   0.00s
    system  |   0.32s   |   0.52s   |   1.04s   |   1.04s
       cpu  |     73%   |     69%   |     62%   |     62%
     total  |   0.446   |   0.757   |   1.197   |   1.667

VFS copy | 512 MB | 1024 MB | 1536 MB | 2048 MB
-------------|-----------|-----------|-----------|-----------

      user  |   0.00s   |   0.00s   |   0.00s   |  0.00s
    system  |   0.33s   |   0.49s   |   0.76s   |  0.99s
       cpu  |     77%   |     62%   |     60%   |    59%
     total  |   0.422   |   0.777   |   1.267   |  1.655
Quoted 5 lines
Quoted 1 line

Questions? Comments? Thoughts?

This is a bit of a surprising result, since in my testing in the past, copy_{to/from}_user() is a major consumer of CPU time (50% of a CPU core at 1GB/s). What backing filesystem did you test on?

I tested using XFS against two KVM guests. Maybe something there is adding the extra cpu cycles?

Anna

Quoted 55 lines

In theory, the VFS copy routines should save at least 50% of the CPU usage since it only needs to make one copy (src->dest) instead of two (kernel->user, user->kernel). Ideally it wouldn't make any data copies at all and just pass page references from the source to the target.

Cheers, Andreas

Quoted 29 lines

Anna

Anna Schumaker (5):
btrfs: Add mountpoint checking during btrfs_copy_file_range
vfs: Remove copy_file_range mountpoint checks
vfs: Copy should check len after file open mode
vfs: Copy should use file_out rather than file_in
vfs: Fall back on splice if no copy function defined

Zach Brown (3):
vfs: add copy_file_range syscall and vfs helper
x86: add sys_copy_file_range to syscall tables
btrfs: add .copy_file_range file operation

arch/x86/entry/syscalls/syscall_32.tbl |   1 +
arch/x86/entry/syscalls/syscall_64.tbl |   1 +
fs/btrfs/ctree.h                       |   3 +
fs/btrfs/file.c                        |   1 +
fs/btrfs/ioctl.c                       |  95 ++++++++++++++----------
fs/read_write.c                        | 132 +++++++++++++++++++++++++++++++++
include/linux/copy.h                   |   6 ++
include/linux/fs.h                     |   3 +
include/uapi/asm-generic/unistd.h      |   4 +-
include/uapi/linux/Kbuild              |   1 +
include/uapi/linux/copy.h              |   6 ++
kernel/sys_ni.c                        |   1 +
12 files changed, 214 insertions(+), 40 deletions(-)
Quoted 10 lines

create mode 100644 include/linux/copy.h
create mode 100644 include/uapi/linux/copy.h

Signature · 6 lines
2.5.1

--
To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Cheers, Andreas

#42 lore raw
In reply to a message not in the archive
Quoted 5 lines · Anna Schumaker

Copy system calls came up during Plumbers a couple of weeks ago, because several filesystems (including NFS and XFS) are currently working on copy acceleration implementations. We haven't heard from Zach Brown in a while, so I volunteered to push his patches upstream so individual filesystems don't need to keep writing their own ioctls.

Just mentioning that this is just pertaining to the data, not the metadata. Providing metadata copying facilities would be _very_ useful, as
most file system specific details relate to the metadata, and having VFS operations for that would avoid the plethora of details in each userspace tool, and theoretically support translations between disparate metadata.

Quoted 6 lines

The first three patches are a simple reposting of Zach's patches from several months ago, with one minor error code fix. The remaining patches add in a fallback mechanism when filesystems don't provide a copy function. This is especially useful when doing a server-side copy on NFS (using the new COPY operation in NFS v4.2). This fallback can be disabled by passing the flag COPY_REFLINK to the system call.

I see copy_file_range() is a reflink() on BTRFS? That's a bit surprising, as it avoids the copy completely. cp(1) for example considered doing a BTRFS clone by default, but didn't due to expectations that users actually wanted the data duplicated on disk for resilience reasons, and for performance reasons so that write latencies were restricted to the copy operation, rather than being introduced at usage time as the dest file is CoW'd.

If reflink() is a possibility for copy_file_range() then could it be done optionally with a flag?

thanks,
Pádraig

A message that is not in the archive (sent off-list or not yet received): [email protected]

#44 lore raw
Quoted 7 lines · Pádraig Brady
Quoted 5 lines · Anna Schumaker

Copy system calls came up during Plumbers a couple of weeks ago, because several filesystems (including NFS and XFS) are currently working on copy acceleration implementations. We haven't heard from Zach Brown in a while, so I volunteered to push his patches upstream so individual filesystems don't need to keep writing their own ioctls.

Quoted 5 lines

Just mentioning that this is just pertaining to the data, not the metadata. Providing metadata copying facilities would be _very_ useful, as
most file system specific details relate to the metadata, and having VFS operations for that would avoid the plethora of details in each userspace tool, and theoretically support translations between disparate metadata.

Metadata copying could be added later if somebody really wants it.

Quoted 17 lines
Quoted 7 lines

The first three patches are a simple reposting of Zach's patches from several months ago, with one minor error code fix. The remaining patches add in a fallback mechanism when filesystems don't provide a copy function. This is especially useful when doing a server-side copy on NFS (using the new COPY operation in NFS v4.2). This fallback can be disabled by passing the flag COPY_REFLINK to the system call.

I see copy_file_range() is a reflink() on BTRFS? That's a bit surprising, as it avoids the copy completely. cp(1) for example considered doing a BTRFS clone by default, but didn't due to expectations that users actually wanted the data duplicated on disk for resilience reasons, and for performance reasons so that write latencies were restricted to the copy operation, rather than being introduced at usage time as the dest file is CoW'd.

Quoted 2 lines

If reflink() is a possibility for copy_file_range() then could it be done optionally with a flag?

The idea is that filesystems get to choose how to handle copies in the default case. BTRFS could do a reflink, but NFS could do a server side copy instead. I can change the default behavior to only do a data copy (unless the reflink flag is specified) instead, if that is desirable.

What does everybody think?

Anna

Quoted 4 lines

thanks,
Pádraig

A message that is not in the archive (sent off-list or not yet received): [email protected]

#46 lore raw
Quoted 10 lines · Anna Schumaker
Quoted 9 lines · Pádraig Brady

I see copy_file_range() is a reflink() on BTRFS? That's a bit surprising, as it avoids the copy completely. cp(1) for example considered doing a BTRFS clone by default, but didn't due to expectations that users actually wanted the data duplicated on disk for resilience reasons, and for performance reasons so that write latencies were restricted to the copy operation, rather than being introduced at usage time as the dest file is CoW'd.

Quoted 6 lines
Quoted 2 lines

If reflink() is a possibility for copy_file_range() then could it be done optionally with a flag?

The idea is that filesystems get to choose how to handle copies in the default case. BTRFS could do a reflink, but NFS could do a server side copy instead. I can change the default behavior to only do a data copy (unless the reflink flag is specified) instead, if that is desirable.

What does everybody think?

I think the best you could do is to have a hint asking politely for the data to be deep-copied. After all, some filesystems reserve the right to transparently deduplicate.

Also, on a true COW filesystem (e.g. btrfs sometimes), there may be no advantage to deep copying unless you actually want two copies for locality reasons.

--Andy

A message that is not in the archive (sent off-list or not yet received): CALCETrXxRB-LXVb+=nkwfj0zEjWuXXTctkSAc9Oec0fgyOQ5Yg-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

#48 lore raw
Quoted 19 lines · Andy Lutomirski
Quoted 10 lines · Anna Schumaker
Quoted 9 lines · Pádraig Brady

I see copy_file_range() is a reflink() on BTRFS? That's a bit surprising, as it avoids the copy completely. cp(1) for example considered doing a BTRFS clone by default, but didn't due to expectations that users actually wanted the data duplicated on disk for resilience reasons, and for performance reasons so that write latencies were restricted to the copy operation, rather than being introduced at usage time as the dest file is CoW'd.

Quoted 6 lines
Quoted 2 lines

If reflink() is a possibility for copy_file_range() then could it be done optionally with a flag?

The idea is that filesystems get to choose how to handle copies in the default case. BTRFS could do a reflink, but NFS could do a server side copy instead. I can change the default behavior to only do a data copy (unless the reflink flag is specified) instead, if that is desirable.

What does everybody think?

Quoted 7 lines

I think the best you could do is to have a hint asking politely for the data to be deep-copied. After all, some filesystems reserve the right to transparently deduplicate.

Also, on a true COW filesystem (e.g. btrfs sometimes), there may be no advantage to deep copying unless you actually want two copies for locality reasons.

Agreed. The relink and server side copy are separate things. There's no advantage to not doing a server side copy,
but as mentioned there may be advantages to doing deep copies on BTRFS (another reason not previous mentioned in this thread, would be to avoid ENOSPC errors at some time in the future).

So having control over the deep copy seems useful.
It's debatable whether ALLOW_REFLINK should be on/off by default for copy_file_range(). I'd be inclined to have such a setting off by default, but cp(1) at least will work with whatever is chosen.

thanks,
Pádraig.

A message that is not in the archive (sent off-list or not yet received): [email protected]

#50 lore raw
Quoted 13 lines · Pádraig Brady
Quoted 12 lines · Andy Lutomirski
Quoted 10 lines · Anna Schumaker
Quoted 9 lines · Pádraig Brady

I see copy_file_range() is a reflink() on BTRFS? That's a bit surprising, as it avoids the copy completely. cp(1) for example considered doing a BTRFS clone by default, but didn't due to expectations that users actually wanted the data duplicated on disk for resilience reasons, and for performance reasons so that write latencies were restricted to the copy operation, rather than being introduced at usage time as the dest file is CoW'd.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines

If reflink() is a possibility for copy_file_range() then could it be done optionally with a flag?

The idea is that filesystems get to choose how to handle copies in the default case. BTRFS could do a reflink, but NFS could do a server side

Eww, different default behaviors depending on the filesystem. :)

Quoted 19 lines
Quoted 5 lines
Quoted 4 lines

copy instead. I can change the default behavior to only do a data copy (unless the reflink flag is specified) instead, if that is desirable.

What does everybody think?

Quoted 7 lines

I think the best you could do is to have a hint asking politely for the data to be deep-copied. After all, some filesystems reserve the right to transparently deduplicate.

Also, on a true COW filesystem (e.g. btrfs sometimes), there may be no advantage to deep copying unless you actually want two copies for locality reasons.

Agreed. The relink and server side copy are separate things. There's no advantage to not doing a server side copy,
but as mentioned there may be advantages to doing deep copies on BTRFS (another reason not previous mentioned in this thread, would be to avoid ENOSPC errors at some time in the future).

Quoted 4 lines

So having control over the deep copy seems useful.
It's debatable whether ALLOW_REFLINK should be on/off by default for copy_file_range(). I'd be inclined to have such a setting off by default, but cp(1) at least will work with whatever is chosen.

So far it looks like people are interested in at least these "make data appear in this other place" filesystem operations:

1. reflink
2. reflink, but only if the contents are the same (dedupe)
3. regular copy
4. regular copy, but make the hardware do it for us
5. regular copy, but require a second copy on the media (no-dedupe)
6. regular copy, but don't CoW (eatmyothercopies) (joke)

(Please add whatever ops I missed.)

I think I can see a case for letting (4) fall back to (3) since (4) is an optimization of (3).

However, I particularly don't like the idea of (1) falling back to (3-5). Either the kernel can satisfy a request or it can't, but let's not just assume that we should transmogrify one type of request into another. Userspace should decide if a reflink failure should turn into one of the copy variants, depending on whether the user wants to spread allocation costs over rewrites or pay it all up front. Also, if we allow reflink to fall back to copy, how do programs find out what actually took place? Or do we simply not allow them to find out?

Also, programs that expect reflink either to finish or fail quickly might be surprised if it's possible for reflink to take a longer time than usual and with the side effect that a deep(er) copy was made.

I guess if someone asks for both (1) and (3) we can do the fallback in the kernel, like how we handle it right now.

--D

Quoted 3 lines

thanks,
Pádraig.

#51 lore raw
Quoted 46 lines · Darrick J. Wong
Quoted 13 lines · Pádraig Brady
Quoted 12 lines · Andy Lutomirski
Quoted 10 lines · Anna Schumaker
Quoted 9 lines · Pádraig Brady

I see copy_file_range() is a reflink() on BTRFS? That's a bit surprising, as it avoids the copy completely. cp(1) for example considered doing a BTRFS clone by default, but didn't due to expectations that users actually wanted the data duplicated on disk for resilience reasons, and for performance reasons so that write latencies were restricted to the copy operation, rather than being introduced at usage time as the dest file is CoW'd.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines

If reflink() is a possibility for copy_file_range() then could it be done optionally with a flag?

The idea is that filesystems get to choose how to handle copies in the default case. BTRFS could do a reflink, but NFS could do a server side

Eww, different default behaviors depending on the filesystem. :)

Quoted 19 lines
Quoted 5 lines
Quoted 4 lines

copy instead. I can change the default behavior to only do a data copy (unless the reflink flag is specified) instead, if that is desirable.

What does everybody think?

Quoted 7 lines

I think the best you could do is to have a hint asking politely for the data to be deep-copied. After all, some filesystems reserve the right to transparently deduplicate.

Also, on a true COW filesystem (e.g. btrfs sometimes), there may be no advantage to deep copying unless you actually want two copies for locality reasons.

Agreed. The relink and server side copy are separate things. There's no advantage to not doing a server side copy,
but as mentioned there may be advantages to doing deep copies on BTRFS (another reason not previous mentioned in this thread, would be to avoid ENOSPC errors at some time in the future).

Quoted 4 lines

So having control over the deep copy seems useful.
It's debatable whether ALLOW_REFLINK should be on/off by default for copy_file_range(). I'd be inclined to have such a setting off by default, but cp(1) at least will work with whatever is chosen.

Quoted 5 lines

So far it looks like people are interested in at least these "make data appear in this other place" filesystem operations:

1. reflink
2. reflink, but only if the contents are the same (dedupe)

What I meant by this was: if you ask for "regular copy", you may end up with a reflink anyway. Anyway, how can you reflink a range and have the contents *not* be the same?

Quoted 3 lines

3. regular copy
4. regular copy, but make the hardware do it for us
5. regular copy, but require a second copy on the media (no-dedupe)

If this comes from me, I have no desire to ever use this as a flag. If someone wants to use chattr or some new operation to say "make this range of this file belong just to me for purpose of optimizing future writes", then sure, go for it, with the understanding that there are plenty of filesystems for which that doesn't even make sense.

Quoted 16 lines

6. regular copy, but don't CoW (eatmyothercopies) (joke)

(Please add whatever ops I missed.)

I think I can see a case for letting (4) fall back to (3) since (4) is an optimization of (3).

However, I particularly don't like the idea of (1) falling back to (3-5). Either the kernel can satisfy a request or it can't, but let's not just assume that we should transmogrify one type of request into another. Userspace should decide if a reflink failure should turn into one of the copy variants, depending on whether the user wants to spread allocation costs over rewrites or pay it all up front. Also, if we allow reflink to fall back to copy, how do programs find out what actually took place? Or do we simply not allow them to find out?

Quoted 7 lines

Also, programs that expect reflink either to finish or fail quickly might be surprised if it's possible for reflink to take a longer time than usual and with the side effect that a deep(er) copy was made.

I guess if someone asks for both (1) and (3) we can do the fallback in the kernel, like how we handle it right now.

I think we should focus on what the actual legit use cases might be. Certainly we want to support a mode that's "reflink or fail". We could have these flags:

COPY_FILE_RANGE_ALLOW_REFLINK
COPY_FILE_RANGE_ALLOW_COPY

Setting neither gets -EINVAL. Setting both works as is. Setting just ALLOW_REFLINK will fail if a reflink can't be supported. Setting just ALLOW_COPY will make a best-effort attempt not to reflink but expressly permits reflinking in cases where either (a) plain old write(2) might also result in a reflink or (b) there is no advantage to not reflinking.

An example of (b) would be a filesystem backed by deduped thinly-provisioned storage that can't do anything about ENOSPC because it doesn't control it in the first place.

Another option would be to split up the copy case into "I expect to overwrite a lot of the target file soon, so (c) try to commit space for that or (d) try to make it time-efficient". Of course, (d) is irrelevant on filesystems with no random access (nvdimms, for example).

I guess the tl;dr is that I'm highly skeptical of any use for disallowing reflinking other than forcibly committing space in cases where committing space actually means something.
--
To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#52 lore raw
Quoted 50 lines · Andy Lutomirski
Quoted 49 lines · Darrick J. Wong
Quoted 13 lines · Pádraig Brady
Quoted 12 lines · Andy Lutomirski
Quoted 10 lines · Anna Schumaker
Quoted 9 lines · Pádraig Brady

I see copy_file_range() is a reflink() on BTRFS? That's a bit surprising, as it avoids the copy completely. cp(1) for example considered doing a BTRFS clone by default, but didn't due to expectations that users actually wanted the data duplicated on disk for resilience reasons, and for performance reasons so that write latencies were restricted to the copy operation, rather than being introduced at usage time as the dest file is CoW'd.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines

If reflink() is a possibility for copy_file_range() then could it be done optionally with a flag?

The idea is that filesystems get to choose how to handle copies in the default case. BTRFS could do a reflink, but NFS could do a server side

Eww, different default behaviors depending on the filesystem. :)

Quoted 19 lines
Quoted 5 lines
Quoted 4 lines

copy instead. I can change the default behavior to only do a data copy (unless the reflink flag is specified) instead, if that is desirable.

What does everybody think?

Quoted 7 lines

I think the best you could do is to have a hint asking politely for the data to be deep-copied. After all, some filesystems reserve the right to transparently deduplicate.

Also, on a true COW filesystem (e.g. btrfs sometimes), there may be no advantage to deep copying unless you actually want two copies for locality reasons.

Agreed. The relink and server side copy are separate things. There's no advantage to not doing a server side copy,
but as mentioned there may be advantages to doing deep copies on BTRFS (another reason not previous mentioned in this thread, would be to avoid ENOSPC errors at some time in the future).

Quoted 4 lines

So having control over the deep copy seems useful.
It's debatable whether ALLOW_REFLINK should be on/off by default for copy_file_range(). I'd be inclined to have such a setting off by default, but cp(1) at least will work with whatever is chosen.

So far it looks like people are interested in at least these "make data appear in this other place" filesystem operations:

Quoted 6 lines
Quoted 2 lines

1. reflink
2. reflink, but only if the contents are the same (dedupe)

What I meant by this was: if you ask for "regular copy", you may end up with a reflink anyway. Anyway, how can you reflink a range and have the contents *not* be the same?

reflink forcibly remaps fd_dest's range to fd_src's range. If they didn't match before, they will afterwards.

dedupe remaps fd_dest's range to fd_src's range only if they match, of course.

Perhaps I should have said "...if the contents are the same before the call"?

Quoted 6 lines
Quoted 4 lines

3. regular copy
4. regular copy, but make the hardware do it for us
5. regular copy, but require a second copy on the media (no-dedupe)

If this comes from me, I have no desire to ever use this as a flag.

I meant (5) as a "disable auto-dedupe for this operation" flag, not as a "reallocate all the shared blocks now" op...

Quoted 4 lines

If someone wants to use chattr or some new operation to say "make this range of this file belong just to me for purpose of optimizing future writes", then sure, go for it, with the understanding that there are plenty of filesystems for which that doesn't even make sense.

"Unshare these blocks" sounds more like something fallocate could do.

So far in my XFS reflink playground, it seems that using the defrag tool to un-cow a file makes most sense. AFAICT the XFS and ext4 defraggers copy a fragmented file's data to a second file and use a 'swap extents' operation, after which the donor file is unlinked.

Hey, if this syscall turns into a more generic "do something involving two (fd:off:len) (fd:off:len) tuples" call, I guess we could throw in "swap extents" as a 7th operation, to refactor the ioctls. <smirk>

Quoted 32 lines
Quoted 17 lines

6. regular copy, but don't CoW (eatmyothercopies) (joke)

(Please add whatever ops I missed.)

I think I can see a case for letting (4) fall back to (3) since (4) is an optimization of (3).

However, I particularly don't like the idea of (1) falling back to (3-5). Either the kernel can satisfy a request or it can't, but let's not just assume that we should transmogrify one type of request into another. Userspace should decide if a reflink failure should turn into one of the copy variants, depending on whether the user wants to spread allocation costs over rewrites or pay it all up front. Also, if we allow reflink to fall back to copy, how do programs find out what actually took place? Or do we simply not allow them to find out?

Quoted 7 lines

Also, programs that expect reflink either to finish or fail quickly might be surprised if it's possible for reflink to take a longer time than usual and with the side effect that a deep(er) copy was made.

I guess if someone asks for both (1) and (3) we can do the fallback in the kernel, like how we handle it right now.

I think we should focus on what the actual legit use cases might be. Certainly we want to support a mode that's "reflink or fail". We could have these flags:

COPY_FILE_RANGE_ALLOW_REFLINK
COPY_FILE_RANGE_ALLOW_COPY

Quoted 6 lines

Setting neither gets -EINVAL. Setting both works as is. Setting just ALLOW_REFLINK will fail if a reflink can't be supported. Setting just ALLOW_COPY will make a best-effort attempt not to reflink but expressly permits reflinking in cases where either (a) plain old write(2) might also result in a reflink or (b) there is no advantage to not reflinking.

I don't agree with having a 'copy' flag that can reflink when we also have a 'reflink' flag. I guess I just don't like having a flag with different meanings depending on context.

Users should be able to get the default behavior by passing '0' for flags, so provide FORBID_REFLINK and FORBID_COPY flags to turn off those behaviors, with an admonishment that one should only use them if they have a goooood reason. Passing neither gets you reflink-xor-copy, which is what I think we both want in the general case.

FORBID_REFLINK = 1
FORBID_COPY = 2
CHECK_SAME = 4
HW_COPY = 8

DEDUPE = (FORBID_COPY | CHECK_SAME)

What do you say to that?

Quoted 10 lines

An example of (b) would be a filesystem backed by deduped thinly-provisioned storage that can't do anything about ENOSPC because it doesn't control it in the first place.

Another option would be to split up the copy case into "I expect to overwrite a lot of the target file soon, so (c) try to commit space for that or (d) try to make it time-efficient". Of course, (d) is irrelevant on filesystems with no random access (nvdimms, for example).

Quoted 3 lines

I guess the tl;dr is that I'm highly skeptical of any use for disallowing reflinking other than forcibly committing space in cases where committing space actually means something.

That's more or less where I was going too. :)

--D
--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#53 lore raw
Quoted 58 lines · Darrick J. Wong
Quoted 50 lines · Andy Lutomirski
Quoted 49 lines · Darrick J. Wong
Quoted 13 lines · Pádraig Brady
Quoted 12 lines · Andy Lutomirski
Quoted 10 lines · Anna Schumaker
Quoted 9 lines · Pádraig Brady

I see copy_file_range() is a reflink() on BTRFS? That's a bit surprising, as it avoids the copy completely. cp(1) for example considered doing a BTRFS clone by default, but didn't due to expectations that users actually wanted the data duplicated on disk for resilience reasons, and for performance reasons so that write latencies were restricted to the copy operation, rather than being introduced at usage time as the dest file is CoW'd.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines

If reflink() is a possibility for copy_file_range() then could it be done optionally with a flag?

The idea is that filesystems get to choose how to handle copies in the default case. BTRFS could do a reflink, but NFS could do a server side

Eww, different default behaviors depending on the filesystem. :)

Quoted 19 lines
Quoted 5 lines
Quoted 4 lines

copy instead. I can change the default behavior to only do a data copy (unless the reflink flag is specified) instead, if that is desirable.

What does everybody think?

Quoted 7 lines

I think the best you could do is to have a hint asking politely for the data to be deep-copied. After all, some filesystems reserve the right to transparently deduplicate.

Also, on a true COW filesystem (e.g. btrfs sometimes), there may be no advantage to deep copying unless you actually want two copies for locality reasons.

Agreed. The relink and server side copy are separate things. There's no advantage to not doing a server side copy,
but as mentioned there may be advantages to doing deep copies on BTRFS (another reason not previous mentioned in this thread, would be to avoid ENOSPC errors at some time in the future).

Quoted 4 lines

So having control over the deep copy seems useful.
It's debatable whether ALLOW_REFLINK should be on/off by default for copy_file_range(). I'd be inclined to have such a setting off by default, but cp(1) at least will work with whatever is chosen.

So far it looks like people are interested in at least these "make data appear in this other place" filesystem operations:

Quoted 6 lines
Quoted 2 lines

1. reflink
2. reflink, but only if the contents are the same (dedupe)

What I meant by this was: if you ask for "regular copy", you may end up with a reflink anyway. Anyway, how can you reflink a range and have the contents *not* be the same?

Quoted 7 lines

reflink forcibly remaps fd_dest's range to fd_src's range. If they didn't match before, they will afterwards.

dedupe remaps fd_dest's range to fd_src's range only if they match, of course.

Perhaps I should have said "...if the contents are the same before the call"?

Oh, I see.

Can we have a clean way to figure out whether two file ranges are the same in a way that allows false negatives? I.e. return 1 if the ranges are reflinks of each other and 0 if not? Pretty please? I've implemented that in the past on btrfs by syncing the ranges and then comparing FIEMAP output, but that's hideous.

Quoted 9 lines
Quoted 6 lines
Quoted 4 lines

3. regular copy
4. regular copy, but make the hardware do it for us
5. regular copy, but require a second copy on the media (no-dedupe)

If this comes from me, I have no desire to ever use this as a flag.

I meant (5) as a "disable auto-dedupe for this operation" flag, not as a "reallocate all the shared blocks now" op...

Hmm, interesting. What effect does it have on systems that do deferred auto-dedupe?

Quoted 25 lines
Quoted 8 lines

I think we should focus on what the actual legit use cases might be. Certainly we want to support a mode that's "reflink or fail". We could have these flags:

COPY_FILE_RANGE_ALLOW_REFLINK
COPY_FILE_RANGE_ALLOW_COPY

Quoted 6 lines

Setting neither gets -EINVAL. Setting both works as is. Setting just ALLOW_REFLINK will fail if a reflink can't be supported. Setting just ALLOW_COPY will make a best-effort attempt not to reflink but expressly permits reflinking in cases where either (a) plain old write(2) might also result in a reflink or (b) there is no advantage to not reflinking.

I don't agree with having a 'copy' flag that can reflink when we also have a 'reflink' flag. I guess I just don't like having a flag with different meanings depending on context.

Users should be able to get the default behavior by passing '0' for flags, so provide FORBID_REFLINK and FORBID_COPY flags to turn off those behaviors, with an admonishment that one should only use them if they have a goooood reason. Passing neither gets you reflink-xor-copy, which is what I think we both want in the general case.

Quoted 8 lines

FORBID_REFLINK = 1
FORBID_COPY = 2
CHECK_SAME = 4
HW_COPY = 8

DEDUPE = (FORBID_COPY | CHECK_SAME)

What do you say to that?

What does HW_COPY mean?

If we have enough weird combinations, maybe having a mode instead of flags makes sense.

--Andy
--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#54 lore raw
Quoted 67 lines · Andy Lutomirski
Quoted 58 lines · Darrick J. Wong
Quoted 50 lines · Andy Lutomirski
Quoted 49 lines · Darrick J. Wong
Quoted 13 lines · Pádraig Brady
Quoted 12 lines · Andy Lutomirski
Quoted 10 lines · Anna Schumaker
Quoted 9 lines · Pádraig Brady

I see copy_file_range() is a reflink() on BTRFS? That's a bit surprising, as it avoids the copy completely. cp(1) for example considered doing a BTRFS clone by default, but didn't due to expectations that users actually wanted the data duplicated on disk for resilience reasons, and for performance reasons so that write latencies were restricted to the copy operation, rather than being introduced at usage time as the dest file is CoW'd.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines

If reflink() is a possibility for copy_file_range() then could it be done optionally with a flag?

The idea is that filesystems get to choose how to handle copies in the default case. BTRFS could do a reflink, but NFS could do a server side

Eww, different default behaviors depending on the filesystem. :)

Quoted 19 lines
Quoted 5 lines
Quoted 4 lines

copy instead. I can change the default behavior to only do a data copy (unless the reflink flag is specified) instead, if that is desirable.

What does everybody think?

Quoted 7 lines

I think the best you could do is to have a hint asking politely for the data to be deep-copied. After all, some filesystems reserve the right to transparently deduplicate.

Also, on a true COW filesystem (e.g. btrfs sometimes), there may be no advantage to deep copying unless you actually want two copies for locality reasons.

Agreed. The relink and server side copy are separate things. There's no advantage to not doing a server side copy,
but as mentioned there may be advantages to doing deep copies on BTRFS (another reason not previous mentioned in this thread, would be to avoid ENOSPC errors at some time in the future).

Quoted 4 lines

So having control over the deep copy seems useful.
It's debatable whether ALLOW_REFLINK should be on/off by default for copy_file_range(). I'd be inclined to have such a setting off by default, but cp(1) at least will work with whatever is chosen.

So far it looks like people are interested in at least these "make data appear in this other place" filesystem operations:

Quoted 6 lines
Quoted 2 lines

1. reflink
2. reflink, but only if the contents are the same (dedupe)

What I meant by this was: if you ask for "regular copy", you may end up with a reflink anyway. Anyway, how can you reflink a range and have the contents *not* be the same?

Quoted 7 lines

reflink forcibly remaps fd_dest's range to fd_src's range. If they didn't match before, they will afterwards.

dedupe remaps fd_dest's range to fd_src's range only if they match, of course.

Perhaps I should have said "...if the contents are the same before the call"?

Quoted 7 lines

Oh, I see.

Can we have a clean way to figure out whether two file ranges are the same in a way that allows false negatives? I.e. return 1 if the ranges are reflinks of each other and 0 if not? Pretty please? I've implemented that in the past on btrfs by syncing the ranges and then comparing FIEMAP output, but that's hideous.

Another mode for this call... :)

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

3. regular copy
4. regular copy, but make the hardware do it for us
5. regular copy, but require a second copy on the media (no-dedupe)

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

If this comes from me, I have no desire to ever use this as a flag.

I meant (5) as a "disable auto-dedupe for this operation" flag, not as a "reallocate all the shared blocks now" op...

Hmm, interesting. What effect does it have on systems that do deferred auto-dedupe?

If it's a userspace deferred auto-dedupe, then hopefully the program coordinates with the dedupe program.

Otherwise, it's only effective with a dedupe that runs in the write-path.

Quoted 30 lines
Quoted 30 lines
Quoted 8 lines

I think we should focus on what the actual legit use cases might be. Certainly we want to support a mode that's "reflink or fail". We could have these flags:

COPY_FILE_RANGE_ALLOW_REFLINK
COPY_FILE_RANGE_ALLOW_COPY

Quoted 6 lines

Setting neither gets -EINVAL. Setting both works as is. Setting just ALLOW_REFLINK will fail if a reflink can't be supported. Setting just ALLOW_COPY will make a best-effort attempt not to reflink but expressly permits reflinking in cases where either (a) plain old write(2) might also result in a reflink or (b) there is no advantage to not reflinking.

I don't agree with having a 'copy' flag that can reflink when we also have a 'reflink' flag. I guess I just don't like having a flag with different meanings depending on context.

Users should be able to get the default behavior by passing '0' for flags, so provide FORBID_REFLINK and FORBID_COPY flags to turn off those behaviors, with an admonishment that one should only use them if they have a goooood reason. Passing neither gets you reflink-xor-copy, which is what I think we both want in the general case.

FORBID_REFLINK = 1
FORBID_COPY = 2
CHECK_SAME = 4
HW_COPY = 8

Quoted 5 lines
Quoted 3 lines

DEDUPE = (FORBID_COPY | CHECK_SAME)

What do you say to that?

What does HW_COPY mean?

It /probably/ means that the FS tells the storage device to copy the block rather than streaming it through the page cache. Your autodedupe thinp device, for example, would "copy" block X to Y by mapping both X and Y to the same piece of media.

(Effectively the same thing as FS reflink/dedupe, but in the storage dev.)

Quoted 3 lines

If we have enough weird combinations, maybe having a mode instead of flags makes sense.

Let's hope not. :)

--D

Quoted 2 lines

--Andy

--
To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#55 lore raw
In reply to Andy Lutomirski
Quoted 14 lines · Andy Lutomirski
Quoted 5 lines · Darrick J. Wong
Quoted 3 lines · Andy Lutomirski

What I meant by this was: if you ask for "regular copy", you may end up with a reflink anyway. Anyway, how can you reflink a range and have the contents *not* be the same?

Quoted 7 lines

reflink forcibly remaps fd_dest's range to fd_src's range. If they didn't match before, they will afterwards.

dedupe remaps fd_dest's range to fd_src's range only if they match, of course.

Perhaps I should have said "...if the contents are the same before the call"?

Quoted 7 lines

Oh, I see.

Can we have a clean way to figure out whether two file ranges are the same in a way that allows false negatives? I.e. return 1 if the ranges are reflinks of each other and 0 if not? Pretty please? I've implemented that in the past on btrfs by syncing the ranges and then comparing FIEMAP output, but that's hideous.

I'd almost rather have a separate call, maybe unshare_file_range()?

Is that the end goal to the sharing check?

-chris

A message that is not in the archive (sent off-list or not yet received): 20150909200921.GD9511-DzB2rL6jT1BHfPKRx072akEOCMrvLtNR@public.gmane.org

#57 lore raw
Quoted 17 lines · Chris Mason
Quoted 16 lines · Andy Lutomirski
Quoted 5 lines · Darrick J. Wong
Quoted 3 lines · Andy Lutomirski

What I meant by this was: if you ask for "regular copy", you may end up with a reflink anyway. Anyway, how can you reflink a range and have the contents *not* be the same?

Quoted 7 lines

reflink forcibly remaps fd_dest's range to fd_src's range. If they didn't match before, they will afterwards.

dedupe remaps fd_dest's range to fd_src's range only if they match, of course.

Perhaps I should have said "...if the contents are the same before the call"?

Oh, I see.

Quoted 8 lines
Quoted 5 lines

Can we have a clean way to figure out whether two file ranges are the same in a way that allows false negatives? I.e. return 1 if the ranges are reflinks of each other and 0 if not? Pretty please? I've implemented that in the past on btrfs by syncing the ranges and then comparing FIEMAP output, but that's hideous.

I'd almost rather have a separate call, maybe unshare_file_range()?

Doesn't it make more sense to put that functionality in fallocate()?

Cheers
Trond

A message that is not in the archive (sent off-list or not yet received): CAHQdGtTSZ1beMMF4DJv=OuA1j2ww0xzJj3+9HMRAf3UpCCLaZg-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

#59 lore raw
Quoted 24 lines · Trond Myklebust
Quoted 23 lines · Chris Mason
Quoted 21 lines · Andy Lutomirski
Quoted 5 lines · Darrick J. Wong
Quoted 3 lines · Andy Lutomirski

What I meant by this was: if you ask for "regular copy", you may end up with a reflink anyway. Anyway, how can you reflink a range and have the contents *not* be the same?

Quoted 7 lines

reflink forcibly remaps fd_dest's range to fd_src's range. If they didn't match before, they will afterwards.

dedupe remaps fd_dest's range to fd_src's range only if they match, of course.

Perhaps I should have said "...if the contents are the same before the call"?

Oh, I see.

Can we have a clean way to figure out whether two file ranges are the same in a way that allows false negatives? I.e. return 1 if the ranges are reflinks of each other and 0 if not? Pretty please? I've implemented that in the past on btrfs by syncing the ranges and then comparing FIEMAP output, but that's hideous.

Quoted 4 lines
Quoted 2 lines

I'd almost rather have a separate call, maybe unshare_file_range()?

Doesn't it make more sense to put that functionality in fallocate()?

That works too, I'm just hoping to keep the copy_file_range stuff simple.

-chris
--
To unsubscribe from this list: send the line "unsubscribe linux-nfs" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): 20150909203805.GE9511-DzB2rL6jT1BHfPKRx072akEOCMrvLtNR@public.gmane.org

#61 lore raw
Quoted 25 lines · Chris Mason
Quoted 24 lines · Trond Myklebust
Quoted 23 lines · Chris Mason
Quoted 21 lines · Andy Lutomirski
Quoted 5 lines · Darrick J. Wong
Quoted 3 lines · Andy Lutomirski

What I meant by this was: if you ask for "regular copy", you may end up with a reflink anyway. Anyway, how can you reflink a range and have the contents *not* be the same?

Quoted 7 lines

reflink forcibly remaps fd_dest's range to fd_src's range. If they didn't match before, they will afterwards.

dedupe remaps fd_dest's range to fd_src's range only if they match, of course.

Perhaps I should have said "...if the contents are the same before the call"?

Oh, I see.

Can we have a clean way to figure out whether two file ranges are the same in a way that allows false negatives? I.e. return 1 if the ranges are reflinks of each other and 0 if not? Pretty please? I've implemented that in the past on btrfs by syncing the ranges and then comparing FIEMAP output, but that's hideous.

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

I'd almost rather have a separate call, maybe unshare_file_range()?

Doesn't it make more sense to put that functionality in fallocate()?

That works too, I'm just hoping to keep the copy_file_range stuff simple.

I agree with keeping copy_file_range() simple, especially for the initial merge. Extra stuff can always be added in later :)

Anna

Quoted 3 lines

-chris

--
To unsubscribe from this list: send the line "unsubscribe linux-nfs" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): [email protected]

#63 lore raw
Quoted 26 lines · Anna Schumaker
Quoted 25 lines · Chris Mason
Quoted 24 lines · Trond Myklebust
Quoted 23 lines · Chris Mason
Quoted 21 lines · Andy Lutomirski
Quoted 5 lines · Darrick J. Wong
Quoted 3 lines · Andy Lutomirski

What I meant by this was: if you ask for "regular copy", you may end up with a reflink anyway. Anyway, how can you reflink a range and have the contents *not* be the same?

Quoted 7 lines

reflink forcibly remaps fd_dest's range to fd_src's range. If they didn't match before, they will afterwards.

dedupe remaps fd_dest's range to fd_src's range only if they match, of course.

Perhaps I should have said "...if the contents are the same before the call"?

Oh, I see.

Can we have a clean way to figure out whether two file ranges are the same in a way that allows false negatives? I.e. return 1 if the ranges are reflinks of each other and 0 if not? Pretty please? I've implemented that in the past on btrfs by syncing the ranges and then comparing FIEMAP output, but that's hideous.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 2 lines

I'd almost rather have a separate call, maybe unshare_file_range()?

Doesn't it make more sense to put that functionality in fallocate()?

[slightly off-topic]

How about FALLOC_FL_UNSHARE_RANGE? I've been looking for a place to land an unshare op that isn't chattr +C, and fallocate seems like a better fit anyway.

--D

Quoted 10 lines
Quoted 3 lines

That works too, I'm just hoping to keep the copy_file_range stuff simple.

I agree with keeping copy_file_range() simple, especially for the initial merge. Extra stuff can always be added in later :)

Anna

Quoted 1 line
Quoted 7 lines
Quoted 2 lines

-chris

--
To unsubscribe from this list: send the line "unsubscribe linux-api" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

--
To unsubscribe from this list: send the line "unsubscribe linux-nfs" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#64 lore raw
In reply to a message not in the archive
Quoted 23 lines · Chris Mason
Quoted 14 lines · Andy Lutomirski
Quoted 5 lines · Darrick J. Wong
Quoted 3 lines · Andy Lutomirski

What I meant by this was: if you ask for "regular copy", you may end up with a reflink anyway. Anyway, how can you reflink a range and have the contents *not* be the same?

Quoted 7 lines

reflink forcibly remaps fd_dest's range to fd_src's range. If they didn't match before, they will afterwards.

dedupe remaps fd_dest's range to fd_src's range only if they match, of course.

Perhaps I should have said "...if the contents are the same before the call"?

Quoted 7 lines

Oh, I see.

Can we have a clean way to figure out whether two file ranges are the same in a way that allows false negatives? I.e. return 1 if the ranges are reflinks of each other and 0 if not? Pretty please? I've implemented that in the past on btrfs by syncing the ranges and then comparing FIEMAP output, but that's hideous.

Quoted 3 lines

I'd almost rather have a separate call, maybe unshare_file_range()?

Is that the end goal to the sharing check?

My use case was archival. I can reflink data between a working copy and some archived copy and then I can very efficiently tell if the working copy has been changed by checking if the reflink is still linked.

It would be even better if I could enumerate which parts of one file match which parts of another file.

--Andy
--
To unsubscribe from this list: send the line "unsubscribe linux-nfs" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): CALCETrXPcxHWGwqhtkGStVabWDOsRbBy+VzrN+XxVZA_F9O0qA-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

#66 lore raw
Quoted 28 lines · Andy Lutomirski
Quoted 23 lines · Chris Mason
Quoted 14 lines · Andy Lutomirski
Quoted 5 lines · Darrick J. Wong
Quoted 3 lines · Andy Lutomirski

What I meant by this was: if you ask for "regular copy", you may end up with a reflink anyway. Anyway, how can you reflink a range and have the contents *not* be the same?

Quoted 7 lines

reflink forcibly remaps fd_dest's range to fd_src's range. If they didn't match before, they will afterwards.

dedupe remaps fd_dest's range to fd_src's range only if they match, of course.

Perhaps I should have said "...if the contents are the same before the call"?

Quoted 7 lines

Oh, I see.

Can we have a clean way to figure out whether two file ranges are the same in a way that allows false negatives? I.e. return 1 if the ranges are reflinks of each other and 0 if not? Pretty please? I've implemented that in the past on btrfs by syncing the ranges and then comparing FIEMAP output, but that's hideous.

Quoted 3 lines

I'd almost rather have a separate call, maybe unshare_file_range()?

Is that the end goal to the sharing check?

Quoted 7 lines

My use case was archival. I can reflink data between a working copy and some archived copy and then I can very efficiently tell if the working copy has been changed by checking if the reflink is still linked.

It would be even better if I could enumerate which parts of one file match which parts of another file.

Oh ok, we can do that pretty quickly with the btrfs searching ioctl (just walk the items, really fast), but that's root only.

For a real interface maybe a btrfs specific ioctl to compare file ranges.

-chris
--
To unsubscribe from this list: send the line "unsubscribe linux-nfs" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): CALCETrVsWBdqvAgwxHcG=gbcWRNPG2ZziWUg1g=siKDrDu7S2Q-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

#68 lore raw
Quoted 5 lines · Andy Lutomirski

Can we have a clean way to figure out whether two file ranges are the same in a way that allows false negatives? I.e. return 1 if the ranges are reflinks of each other and 0 if not? Pretty please? I've implemented that in the past on btrfs by syncing the ranges and then comparing FIEMAP output, but that's hideous.

That fundamentally doesn't work for userspace, because the moment the filesystem drops it's locks on the inodes in the kernel after doing the comparison the mappings can change. IOWs, by the time the information gets back to userspace, it's already wrong. e.g. cp made this mistake by trying to use FIEMAP to optimise hole detection in files and ended up with corrupt copies.

It really doesn't matter what the syscall/ioctl interface is, trying to make application logic decisions based on inode block mappings from userspace is racy and not safe and will go wrong...

Cheers,

Dave.

Signature · 2 lines
Dave Chinner
[email protected]
#69 lore raw
Quoted 15 lines · Dave Chinner
Quoted 5 lines · Andy Lutomirski

Can we have a clean way to figure out whether two file ranges are the same in a way that allows false negatives? I.e. return 1 if the ranges are reflinks of each other and 0 if not? Pretty please? I've implemented that in the past on btrfs by syncing the ranges and then comparing FIEMAP output, but that's hideous.

That fundamentally doesn't work for userspace, because the moment the filesystem drops it's locks on the inodes in the kernel after doing the comparison the mappings can change. IOWs, by the time the information gets back to userspace, it's already wrong. e.g. cp made this mistake by trying to use FIEMAP to optimise hole detection in files and ended up with corrupt copies.

Quoted 4 lines

It really doesn't matter what the syscall/ioctl interface is, trying to make application logic decisions based on inode block mappings from userspace is racy and not safe and will go wrong...

I agree, and that thing was just an experiment. I'd love to see a sane and correct interface, though.

--Andy
--
To unsubscribe from this list: send the line "unsubscribe linux-nfs" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#70 lore raw
In reply to Darrick J. Wong
Quoted 134 lines · Darrick J. Wong
Quoted 50 lines · Andy Lutomirski
Quoted 49 lines · Darrick J. Wong
Quoted 13 lines · Pádraig Brady
Quoted 12 lines · Andy Lutomirski
Quoted 10 lines · Anna Schumaker
Quoted 9 lines · Pádraig Brady

I see copy_file_range() is a reflink() on BTRFS? That's a bit surprising, as it avoids the copy completely. cp(1) for example considered doing a BTRFS clone by default, but didn't due to expectations that users actually wanted the data duplicated on disk for resilience reasons, and for performance reasons so that write latencies were restricted to the copy operation, rather than being introduced at usage time as the dest file is CoW'd.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines

If reflink() is a possibility for copy_file_range() then could it be done optionally with a flag?

The idea is that filesystems get to choose how to handle copies in the default case. BTRFS could do a reflink, but NFS could do a server side

Eww, different default behaviors depending on the filesystem. :)

Quoted 19 lines
Quoted 5 lines
Quoted 4 lines

copy instead. I can change the default behavior to only do a data copy (unless the reflink flag is specified) instead, if that is desirable.

What does everybody think?

Quoted 7 lines

I think the best you could do is to have a hint asking politely for the data to be deep-copied. After all, some filesystems reserve the right to transparently deduplicate.

Also, on a true COW filesystem (e.g. btrfs sometimes), there may be no advantage to deep copying unless you actually want two copies for locality reasons.

Agreed. The relink and server side copy are separate things. There's no advantage to not doing a server side copy,
but as mentioned there may be advantages to doing deep copies on BTRFS (another reason not previous mentioned in this thread, would be to avoid ENOSPC errors at some time in the future).

Quoted 4 lines

So having control over the deep copy seems useful.
It's debatable whether ALLOW_REFLINK should be on/off by default for copy_file_range(). I'd be inclined to have such a setting off by default, but cp(1) at least will work with whatever is chosen.

So far it looks like people are interested in at least these "make data appear in this other place" filesystem operations:

Quoted 6 lines
Quoted 2 lines

1. reflink
2. reflink, but only if the contents are the same (dedupe)

What I meant by this was: if you ask for "regular copy", you may end up with a reflink anyway. Anyway, how can you reflink a range and have the contents *not* be the same?

reflink forcibly remaps fd_dest's range to fd_src's range. If they didn't match before, they will afterwards.

dedupe remaps fd_dest's range to fd_src's range only if they match, of course.

Perhaps I should have said "...if the contents are the same before the call"?

Quoted 6 lines
Quoted 4 lines

3. regular copy
4. regular copy, but make the hardware do it for us
5. regular copy, but require a second copy on the media (no-dedupe)

If this comes from me, I have no desire to ever use this as a flag.

I meant (5) as a "disable auto-dedupe for this operation" flag, not as a "reallocate all the shared blocks now" op...

Quoted 4 lines

If someone wants to use chattr or some new operation to say "make this range of this file belong just to me for purpose of optimizing future writes", then sure, go for it, with the understanding that there are plenty of filesystems for which that doesn't even make sense.

"Unshare these blocks" sounds more like something fallocate could do.

So far in my XFS reflink playground, it seems that using the defrag tool to un-cow a file makes most sense. AFAICT the XFS and ext4 defraggers copy a fragmented file's data to a second file and use a 'swap extents' operation, after which the donor file is unlinked.

Hey, if this syscall turns into a more generic "do something involving two (fd:off:len) (fd:off:len) tuples" call, I guess we could throw in "swap extents" as a 7th operation, to refactor the ioctls. <smirk>

Quoted 32 lines
Quoted 17 lines

6. regular copy, but don't CoW (eatmyothercopies) (joke)

(Please add whatever ops I missed.)

I think I can see a case for letting (4) fall back to (3) since (4) is an optimization of (3).

However, I particularly don't like the idea of (1) falling back to (3-5). Either the kernel can satisfy a request or it can't, but let's not just assume that we should transmogrify one type of request into another. Userspace should decide if a reflink failure should turn into one of the copy variants, depending on whether the user wants to spread allocation costs over rewrites or pay it all up front. Also, if we allow reflink to fall back to copy, how do programs find out what actually took place? Or do we simply not allow them to find out?

Quoted 7 lines

Also, programs that expect reflink either to finish or fail quickly might be surprised if it's possible for reflink to take a longer time than usual and with the side effect that a deep(er) copy was made.

I guess if someone asks for both (1) and (3) we can do the fallback in the kernel, like how we handle it right now.

I think we should focus on what the actual legit use cases might be. Certainly we want to support a mode that's "reflink or fail". We could have these flags:

COPY_FILE_RANGE_ALLOW_REFLINK
COPY_FILE_RANGE_ALLOW_COPY

Quoted 6 lines

Setting neither gets -EINVAL. Setting both works as is. Setting just ALLOW_REFLINK will fail if a reflink can't be supported. Setting just ALLOW_COPY will make a best-effort attempt not to reflink but expressly permits reflinking in cases where either (a) plain old write(2) might also result in a reflink or (b) there is no advantage to not reflinking.

I don't agree with having a 'copy' flag that can reflink when we also have a 'reflink' flag. I guess I just don't like having a flag with different meanings depending on context.

Quoted 5 lines

Users should be able to get the default behavior by passing '0' for flags, so provide FORBID_REFLINK and FORBID_COPY flags to turn off those behaviors, with an admonishment that one should only use them if they have a goooood reason. Passing neither gets you reflink-xor-copy, which is what I think we both want in the general case.

I agree here that 0 for flags should do something useful, and I wanted to double check if reflink-xor-copy is a good default behavior.

Quoted 3 lines

FORBID_REFLINK = 1
FORBID_COPY = 2

I don't like the idea of using flags to forbid behavior. I think it would be more straightforward to have flags like REFLINK_ONLY or COPY_ONLY so users can tell us what they want, instead of what they don't want.

While I'm thinking about flags, COPY_FILE_RANGE_REFLINK_ONLY would be a bit of a mouthful. Does anybody have suggestions for ways that I could make this shorter?

Thanks,
Anna

Quoted 17 lines

CHECK_SAME = 4
HW_COPY = 8

DEDUPE = (FORBID_COPY | CHECK_SAME)

What do you say to that?

Quoted 10 lines

An example of (b) would be a filesystem backed by deduped thinly-provisioned storage that can't do anything about ENOSPC because it doesn't control it in the first place.

Another option would be to split up the copy case into "I expect to overwrite a lot of the target file soon, so (c) try to commit space for that or (d) try to make it time-efficient". Of course, (d) is irrelevant on filesystems with no random access (nvdimms, for example).

Quoted 8 lines
Quoted 3 lines

I guess the tl;dr is that I'm highly skeptical of any use for disallowing reflinking other than forcibly committing space in cases where committing space actually means something.

That's more or less where I was going too. :)

--D

--
To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): [email protected]

#72 lore raw
Quoted 135 lines · Anna Schumaker
Quoted 134 lines · Darrick J. Wong
Quoted 50 lines · Andy Lutomirski
Quoted 49 lines · Darrick J. Wong
Quoted 13 lines · Pádraig Brady
Quoted 12 lines · Andy Lutomirski
Quoted 10 lines · Anna Schumaker
Quoted 9 lines · Pádraig Brady

I see copy_file_range() is a reflink() on BTRFS? That's a bit surprising, as it avoids the copy completely. cp(1) for example considered doing a BTRFS clone by default, but didn't due to expectations that users actually wanted the data duplicated on disk for resilience reasons, and for performance reasons so that write latencies were restricted to the copy operation, rather than being introduced at usage time as the dest file is CoW'd.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines

If reflink() is a possibility for copy_file_range() then could it be done optionally with a flag?

The idea is that filesystems get to choose how to handle copies in the default case. BTRFS could do a reflink, but NFS could do a server side

Eww, different default behaviors depending on the filesystem. :)

Quoted 19 lines
Quoted 5 lines
Quoted 4 lines

copy instead. I can change the default behavior to only do a data copy (unless the reflink flag is specified) instead, if that is desirable.

What does everybody think?

Quoted 7 lines

I think the best you could do is to have a hint asking politely for the data to be deep-copied. After all, some filesystems reserve the right to transparently deduplicate.

Also, on a true COW filesystem (e.g. btrfs sometimes), there may be no advantage to deep copying unless you actually want two copies for locality reasons.

Agreed. The relink and server side copy are separate things. There's no advantage to not doing a server side copy,
but as mentioned there may be advantages to doing deep copies on BTRFS (another reason not previous mentioned in this thread, would be to avoid ENOSPC errors at some time in the future).

Quoted 4 lines

So having control over the deep copy seems useful.
It's debatable whether ALLOW_REFLINK should be on/off by default for copy_file_range(). I'd be inclined to have such a setting off by default, but cp(1) at least will work with whatever is chosen.

So far it looks like people are interested in at least these "make data appear in this other place" filesystem operations:

Quoted 6 lines
Quoted 2 lines

1. reflink
2. reflink, but only if the contents are the same (dedupe)

What I meant by this was: if you ask for "regular copy", you may end up with a reflink anyway. Anyway, how can you reflink a range and have the contents *not* be the same?

reflink forcibly remaps fd_dest's range to fd_src's range. If they didn't match before, they will afterwards.

dedupe remaps fd_dest's range to fd_src's range only if they match, of course.

Perhaps I should have said "...if the contents are the same before the call"?

Quoted 6 lines
Quoted 4 lines

3. regular copy
4. regular copy, but make the hardware do it for us
5. regular copy, but require a second copy on the media (no-dedupe)

If this comes from me, I have no desire to ever use this as a flag.

I meant (5) as a "disable auto-dedupe for this operation" flag, not as a "reallocate all the shared blocks now" op...

Quoted 4 lines

If someone wants to use chattr or some new operation to say "make this range of this file belong just to me for purpose of optimizing future writes", then sure, go for it, with the understanding that there are plenty of filesystems for which that doesn't even make sense.

"Unshare these blocks" sounds more like something fallocate could do.

So far in my XFS reflink playground, it seems that using the defrag tool to un-cow a file makes most sense. AFAICT the XFS and ext4 defraggers copy a fragmented file's data to a second file and use a 'swap extents' operation, after which the donor file is unlinked.

Hey, if this syscall turns into a more generic "do something involving two (fd:off:len) (fd:off:len) tuples" call, I guess we could throw in "swap extents" as a 7th operation, to refactor the ioctls. <smirk>

Quoted 32 lines
Quoted 17 lines

6. regular copy, but don't CoW (eatmyothercopies) (joke)

(Please add whatever ops I missed.)

I think I can see a case for letting (4) fall back to (3) since (4) is an optimization of (3).

However, I particularly don't like the idea of (1) falling back to (3-5). Either the kernel can satisfy a request or it can't, but let's not just assume that we should transmogrify one type of request into another. Userspace should decide if a reflink failure should turn into one of the copy variants, depending on whether the user wants to spread allocation costs over rewrites or pay it all up front. Also, if we allow reflink to fall back to copy, how do programs find out what actually took place? Or do we simply not allow them to find out?

Quoted 7 lines

Also, programs that expect reflink either to finish or fail quickly might be surprised if it's possible for reflink to take a longer time than usual and with the side effect that a deep(er) copy was made.

I guess if someone asks for both (1) and (3) we can do the fallback in the kernel, like how we handle it right now.

I think we should focus on what the actual legit use cases might be. Certainly we want to support a mode that's "reflink or fail". We could have these flags:

COPY_FILE_RANGE_ALLOW_REFLINK
COPY_FILE_RANGE_ALLOW_COPY

Quoted 6 lines

Setting neither gets -EINVAL. Setting both works as is. Setting just ALLOW_REFLINK will fail if a reflink can't be supported. Setting just ALLOW_COPY will make a best-effort attempt not to reflink but expressly permits reflinking in cases where either (a) plain old write(2) might also result in a reflink or (b) there is no advantage to not reflinking.

I don't agree with having a 'copy' flag that can reflink when we also have a 'reflink' flag. I guess I just don't like having a flag with different meanings depending on context.

Quoted 8 lines
Quoted 5 lines

Users should be able to get the default behavior by passing '0' for flags, so provide FORBID_REFLINK and FORBID_COPY flags to turn off those behaviors, with an admonishment that one should only use them if they have a goooood reason. Passing neither gets you reflink-xor-copy, which is what I think we both want in the general case.

I agree here that 0 for flags should do something useful, and I wanted to double check if reflink-xor-copy is a good default behavior.

Ok.

Quoted 7 lines
Quoted 3 lines

FORBID_REFLINK = 1
FORBID_COPY = 2

I don't like the idea of using flags to forbid behavior. I think it would be more straightforward to have flags like REFLINK_ONLY or COPY_ONLY so users can tell us what they want, instead of what they don't want.

Seems fine to me.

Quoted 3 lines

While I'm thinking about flags, COPY_FILE_RANGE_REFLINK_ONLY would be a bit of a mouthful. Does anybody have suggestions for ways that I could make this shorter?

CFR_REFLINK_ONLY?

--D

Quoted 34 lines

Thanks,
Anna

Quoted 17 lines

CHECK_SAME = 4
HW_COPY = 8

DEDUPE = (FORBID_COPY | CHECK_SAME)

What do you say to that?

Quoted 10 lines

An example of (b) would be a filesystem backed by deduped thinly-provisioned storage that can't do anything about ENOSPC because it doesn't control it in the first place.

Another option would be to split up the copy case into "I expect to overwrite a lot of the target file soon, so (c) try to commit space for that or (d) try to make it time-efficient". Of course, (d) is irrelevant on filesystems with no random access (nvdimms, for example).

Quoted 8 lines
Quoted 3 lines

I guess the tl;dr is that I'm highly skeptical of any use for disallowing reflinking other than forcibly committing space in cases where committing space actually means something.

That's more or less where I was going too. :)

--D

--
To unsubscribe from this list: send the line "unsubscribe linux-api" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#73 lore raw
Quoted 155 lines · Darrick J. Wong
Quoted 135 lines · Anna Schumaker
Quoted 134 lines · Darrick J. Wong
Quoted 50 lines · Andy Lutomirski
Quoted 49 lines · Darrick J. Wong
Quoted 13 lines · Pádraig Brady
Quoted 12 lines · Andy Lutomirski
Quoted 10 lines · Anna Schumaker
Quoted 9 lines · Pádraig Brady

I see copy_file_range() is a reflink() on BTRFS? That's a bit surprising, as it avoids the copy completely. cp(1) for example considered doing a BTRFS clone by default, but didn't due to expectations that users actually wanted the data duplicated on disk for resilience reasons, and for performance reasons so that write latencies were restricted to the copy operation, rather than being introduced at usage time as the dest file is CoW'd.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines

If reflink() is a possibility for copy_file_range() then could it be done optionally with a flag?

The idea is that filesystems get to choose how to handle copies in the default case. BTRFS could do a reflink, but NFS could do a server side

Eww, different default behaviors depending on the filesystem. :)

Quoted 19 lines
Quoted 5 lines
Quoted 4 lines

copy instead. I can change the default behavior to only do a data copy (unless the reflink flag is specified) instead, if that is desirable.

What does everybody think?

Quoted 7 lines

I think the best you could do is to have a hint asking politely for the data to be deep-copied. After all, some filesystems reserve the right to transparently deduplicate.

Also, on a true COW filesystem (e.g. btrfs sometimes), there may be no advantage to deep copying unless you actually want two copies for locality reasons.

Agreed. The relink and server side copy are separate things. There's no advantage to not doing a server side copy,
but as mentioned there may be advantages to doing deep copies on BTRFS (another reason not previous mentioned in this thread, would be to avoid ENOSPC errors at some time in the future).

Quoted 4 lines

So having control over the deep copy seems useful.
It's debatable whether ALLOW_REFLINK should be on/off by default for copy_file_range(). I'd be inclined to have such a setting off by default, but cp(1) at least will work with whatever is chosen.

So far it looks like people are interested in at least these "make data appear in this other place" filesystem operations:

Quoted 6 lines
Quoted 2 lines

1. reflink
2. reflink, but only if the contents are the same (dedupe)

What I meant by this was: if you ask for "regular copy", you may end up with a reflink anyway. Anyway, how can you reflink a range and have the contents *not* be the same?

reflink forcibly remaps fd_dest's range to fd_src's range. If they didn't match before, they will afterwards.

dedupe remaps fd_dest's range to fd_src's range only if they match, of course.

Perhaps I should have said "...if the contents are the same before the call"?

Quoted 6 lines
Quoted 4 lines

3. regular copy
4. regular copy, but make the hardware do it for us
5. regular copy, but require a second copy on the media (no-dedupe)

If this comes from me, I have no desire to ever use this as a flag.

I meant (5) as a "disable auto-dedupe for this operation" flag, not as a "reallocate all the shared blocks now" op...

Quoted 4 lines

If someone wants to use chattr or some new operation to say "make this range of this file belong just to me for purpose of optimizing future writes", then sure, go for it, with the understanding that there are plenty of filesystems for which that doesn't even make sense.

"Unshare these blocks" sounds more like something fallocate could do.

So far in my XFS reflink playground, it seems that using the defrag tool to un-cow a file makes most sense. AFAICT the XFS and ext4 defraggers copy a fragmented file's data to a second file and use a 'swap extents' operation, after which the donor file is unlinked.

Hey, if this syscall turns into a more generic "do something involving two (fd:off:len) (fd:off:len) tuples" call, I guess we could throw in "swap extents" as a 7th operation, to refactor the ioctls. <smirk>

Quoted 32 lines
Quoted 17 lines

6. regular copy, but don't CoW (eatmyothercopies) (joke)

(Please add whatever ops I missed.)

I think I can see a case for letting (4) fall back to (3) since (4) is an optimization of (3).

However, I particularly don't like the idea of (1) falling back to (3-5). Either the kernel can satisfy a request or it can't, but let's not just assume that we should transmogrify one type of request into another. Userspace should decide if a reflink failure should turn into one of the copy variants, depending on whether the user wants to spread allocation costs over rewrites or pay it all up front. Also, if we allow reflink to fall back to copy, how do programs find out what actually took place? Or do we simply not allow them to find out?

Quoted 7 lines

Also, programs that expect reflink either to finish or fail quickly might be surprised if it's possible for reflink to take a longer time than usual and with the side effect that a deep(er) copy was made.

I guess if someone asks for both (1) and (3) we can do the fallback in the kernel, like how we handle it right now.

I think we should focus on what the actual legit use cases might be. Certainly we want to support a mode that's "reflink or fail". We could have these flags:

COPY_FILE_RANGE_ALLOW_REFLINK
COPY_FILE_RANGE_ALLOW_COPY

Quoted 6 lines

Setting neither gets -EINVAL. Setting both works as is. Setting just ALLOW_REFLINK will fail if a reflink can't be supported. Setting just ALLOW_COPY will make a best-effort attempt not to reflink but expressly permits reflinking in cases where either (a) plain old write(2) might also result in a reflink or (b) there is no advantage to not reflinking.

I don't agree with having a 'copy' flag that can reflink when we also have a 'reflink' flag. I guess I just don't like having a flag with different meanings depending on context.

Quoted 8 lines
Quoted 5 lines

Users should be able to get the default behavior by passing '0' for flags, so provide FORBID_REFLINK and FORBID_COPY flags to turn off those behaviors, with an admonishment that one should only use them if they have a goooood reason. Passing neither gets you reflink-xor-copy, which is what I think we both want in the general case.

I agree here that 0 for flags should do something useful, and I wanted to double check if reflink-xor-copy is a good default behavior.

Ok.

Quoted 7 lines
Quoted 3 lines

FORBID_REFLINK = 1
FORBID_COPY = 2

I don't like the idea of using flags to forbid behavior. I think it would be more straightforward to have flags like REFLINK_ONLY or COPY_ONLY so users can tell us what they want, instead of what they don't want.

Quoted 7 lines

Seems fine to me.

Quoted 3 lines

While I'm thinking about flags, COPY_FILE_RANGE_REFLINK_ONLY would be a bit of a mouthful. Does anybody have suggestions for ways that I could make this shorter?

CFR_REFLINK_ONLY?

That could work! Although I might do as Austin suggests and drop the _ONLY part, and then make the man page clear about what's going on.

Would you expect to trigger a NFS server side copy by passing the pagecache copy flag? Or would that only happen if I pass flags=0?

Anna

Quoted 37 lines

--D

Quoted 34 lines

Thanks,
Anna

Quoted 17 lines

CHECK_SAME = 4
HW_COPY = 8

DEDUPE = (FORBID_COPY | CHECK_SAME)

What do you say to that?

Quoted 10 lines

An example of (b) would be a filesystem backed by deduped thinly-provisioned storage that can't do anything about ENOSPC because it doesn't control it in the first place.

Another option would be to split up the copy case into "I expect to overwrite a lot of the target file soon, so (c) try to commit space for that or (d) try to make it time-efficient". Of course, (d) is irrelevant on filesystems with no random access (nvdimms, for example).

Quoted 8 lines
Quoted 3 lines

I guess the tl;dr is that I'm highly skeptical of any use for disallowing reflinking other than forcibly committing space in cases where committing space actually means something.

That's more or less where I was going too. :)

--D

--
To unsubscribe from this list: send the line "unsubscribe linux-api" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): [email protected]

#75 lore raw
Quoted 162 lines · Anna Schumaker
Quoted 161 lines · Darrick J. Wong
Quoted 135 lines · Anna Schumaker
Quoted 134 lines · Darrick J. Wong
Quoted 50 lines · Andy Lutomirski
Quoted 49 lines · Darrick J. Wong
Quoted 13 lines · Pádraig Brady
Quoted 12 lines · Andy Lutomirski
Quoted 10 lines · Anna Schumaker
Quoted 9 lines · Pádraig Brady

I see copy_file_range() is a reflink() on BTRFS? That's a bit surprising, as it avoids the copy completely. cp(1) for example considered doing a BTRFS clone by default, but didn't due to expectations that users actually wanted the data duplicated on disk for resilience reasons, and for performance reasons so that write latencies were restricted to the copy operation, rather than being introduced at usage time as the dest file is CoW'd.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines

If reflink() is a possibility for copy_file_range() then could it be done optionally with a flag?

The idea is that filesystems get to choose how to handle copies in the default case. BTRFS could do a reflink, but NFS could do a server side

Eww, different default behaviors depending on the filesystem. :)

Quoted 19 lines
Quoted 5 lines
Quoted 4 lines

copy instead. I can change the default behavior to only do a data copy (unless the reflink flag is specified) instead, if that is desirable.

What does everybody think?

Quoted 7 lines

I think the best you could do is to have a hint asking politely for the data to be deep-copied. After all, some filesystems reserve the right to transparently deduplicate.

Also, on a true COW filesystem (e.g. btrfs sometimes), there may be no advantage to deep copying unless you actually want two copies for locality reasons.

Agreed. The relink and server side copy are separate things. There's no advantage to not doing a server side copy,
but as mentioned there may be advantages to doing deep copies on BTRFS (another reason not previous mentioned in this thread, would be to avoid ENOSPC errors at some time in the future).

Quoted 4 lines

So having control over the deep copy seems useful.
It's debatable whether ALLOW_REFLINK should be on/off by default for copy_file_range(). I'd be inclined to have such a setting off by default, but cp(1) at least will work with whatever is chosen.

So far it looks like people are interested in at least these "make data appear in this other place" filesystem operations:

Quoted 6 lines
Quoted 2 lines

1. reflink
2. reflink, but only if the contents are the same (dedupe)

What I meant by this was: if you ask for "regular copy", you may end up with a reflink anyway. Anyway, how can you reflink a range and have the contents *not* be the same?

reflink forcibly remaps fd_dest's range to fd_src's range. If they didn't match before, they will afterwards.

dedupe remaps fd_dest's range to fd_src's range only if they match, of course.

Perhaps I should have said "...if the contents are the same before the call"?

Quoted 6 lines
Quoted 4 lines

3. regular copy
4. regular copy, but make the hardware do it for us
5. regular copy, but require a second copy on the media (no-dedupe)

If this comes from me, I have no desire to ever use this as a flag.

I meant (5) as a "disable auto-dedupe for this operation" flag, not as a "reallocate all the shared blocks now" op...

Quoted 4 lines

If someone wants to use chattr or some new operation to say "make this range of this file belong just to me for purpose of optimizing future writes", then sure, go for it, with the understanding that there are plenty of filesystems for which that doesn't even make sense.

"Unshare these blocks" sounds more like something fallocate could do.

So far in my XFS reflink playground, it seems that using the defrag tool to un-cow a file makes most sense. AFAICT the XFS and ext4 defraggers copy a fragmented file's data to a second file and use a 'swap extents' operation, after which the donor file is unlinked.

Hey, if this syscall turns into a more generic "do something involving two (fd:off:len) (fd:off:len) tuples" call, I guess we could throw in "swap extents" as a 7th operation, to refactor the ioctls. <smirk>

Quoted 32 lines
Quoted 17 lines

6. regular copy, but don't CoW (eatmyothercopies) (joke)

(Please add whatever ops I missed.)

I think I can see a case for letting (4) fall back to (3) since (4) is an optimization of (3).

However, I particularly don't like the idea of (1) falling back to (3-5). Either the kernel can satisfy a request or it can't, but let's not just assume that we should transmogrify one type of request into another. Userspace should decide if a reflink failure should turn into one of the copy variants, depending on whether the user wants to spread allocation costs over rewrites or pay it all up front. Also, if we allow reflink to fall back to copy, how do programs find out what actually took place? Or do we simply not allow them to find out?

Quoted 7 lines

Also, programs that expect reflink either to finish or fail quickly might be surprised if it's possible for reflink to take a longer time than usual and with the side effect that a deep(er) copy was made.

I guess if someone asks for both (1) and (3) we can do the fallback in the kernel, like how we handle it right now.

I think we should focus on what the actual legit use cases might be. Certainly we want to support a mode that's "reflink or fail". We could have these flags:

COPY_FILE_RANGE_ALLOW_REFLINK
COPY_FILE_RANGE_ALLOW_COPY

Quoted 6 lines

Setting neither gets -EINVAL. Setting both works as is. Setting just ALLOW_REFLINK will fail if a reflink can't be supported. Setting just ALLOW_COPY will make a best-effort attempt not to reflink but expressly permits reflinking in cases where either (a) plain old write(2) might also result in a reflink or (b) there is no advantage to not reflinking.

I don't agree with having a 'copy' flag that can reflink when we also have a 'reflink' flag. I guess I just don't like having a flag with different meanings depending on context.

Quoted 8 lines
Quoted 5 lines

Users should be able to get the default behavior by passing '0' for flags, so provide FORBID_REFLINK and FORBID_COPY flags to turn off those behaviors, with an admonishment that one should only use them if they have a goooood reason. Passing neither gets you reflink-xor-copy, which is what I think we both want in the general case.

I agree here that 0 for flags should do something useful, and I wanted to double check if reflink-xor-copy is a good default behavior.

Ok.

Quoted 7 lines
Quoted 3 lines

FORBID_REFLINK = 1
FORBID_COPY = 2

I don't like the idea of using flags to forbid behavior. I think it would be more straightforward to have flags like REFLINK_ONLY or COPY_ONLY so users can tell us what they want, instead of what they don't want.

Seems fine to me.

Quoted 3 lines

While I'm thinking about flags, COPY_FILE_RANGE_REFLINK_ONLY would be a bit of a mouthful. Does anybody have suggestions for ways that I could make this shorter?

Quoted 5 lines
Quoted 1 line

CFR_REFLINK_ONLY?

That could work! Although I might do as Austin suggests and drop the _ONLY part, and then make the man page clear about what's going on.

Would you expect to trigger a NFS server side copy by passing the pagecache copy flag? Or would that only happen if I pass flags=0?

Personally, I would think that an NFS server side copy could be counted under the 'hardware assisted' flag. From the point of view of an NFS client, the NFS server is a (usually) opaque piece of storage hardware, similar to a local disk drive in that you pass commands to it and get responses, the only real difference is that NFS is a much higher level protocol than for example SCSI.

Quoted 38 lines
Quoted 37 lines

--D

Quoted 34 lines

Thanks,
Anna

Quoted 17 lines

CHECK_SAME = 4
HW_COPY = 8

DEDUPE = (FORBID_COPY | CHECK_SAME)

What do you say to that?

Quoted 10 lines

An example of (b) would be a filesystem backed by deduped thinly-provisioned storage that can't do anything about ENOSPC because it doesn't control it in the first place.

Another option would be to split up the copy case into "I expect to overwrite a lot of the target file soon, so (c) try to commit space for that or (d) try to make it time-efficient". Of course, (d) is irrelevant on filesystems with no random access (nvdimms, for example).

Quoted 8 lines
Quoted 3 lines

I guess the tl;dr is that I'm highly skeptical of any use for disallowing reflinking other than forcibly committing space in cases where committing space actually means something.

That's more or less where I was going too. :)

--D

--
To unsubscribe from this list: send the line "unsubscribe linux-api" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#76 lore raw
In reply to Anna Schumaker
Quoted 144 lines · Anna Schumaker
Quoted 134 lines · Darrick J. Wong
Quoted 50 lines · Andy Lutomirski
Quoted 49 lines · Darrick J. Wong
Quoted 13 lines · Pádraig Brady
Quoted 12 lines · Andy Lutomirski
Quoted 10 lines · Anna Schumaker
Quoted 9 lines · Pádraig Brady

I see copy_file_range() is a reflink() on BTRFS? That's a bit surprising, as it avoids the copy completely. cp(1) for example considered doing a BTRFS clone by default, but didn't due to expectations that users actually wanted the data duplicated on disk for resilience reasons, and for performance reasons so that write latencies were restricted to the copy operation, rather than being introduced at usage time as the dest file is CoW'd.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines

If reflink() is a possibility for copy_file_range() then could it be done optionally with a flag?

The idea is that filesystems get to choose how to handle copies in the default case. BTRFS could do a reflink, but NFS could do a server side

Eww, different default behaviors depending on the filesystem. :)

Quoted 19 lines
Quoted 5 lines
Quoted 4 lines

copy instead. I can change the default behavior to only do a data copy (unless the reflink flag is specified) instead, if that is desirable.

What does everybody think?

Quoted 7 lines

I think the best you could do is to have a hint asking politely for the data to be deep-copied. After all, some filesystems reserve the right to transparently deduplicate.

Also, on a true COW filesystem (e.g. btrfs sometimes), there may be no advantage to deep copying unless you actually want two copies for locality reasons.

Agreed. The relink and server side copy are separate things. There's no advantage to not doing a server side copy,
but as mentioned there may be advantages to doing deep copies on BTRFS (another reason not previous mentioned in this thread, would be to avoid ENOSPC errors at some time in the future).

Quoted 4 lines

So having control over the deep copy seems useful.
It's debatable whether ALLOW_REFLINK should be on/off by default for copy_file_range(). I'd be inclined to have such a setting off by default, but cp(1) at least will work with whatever is chosen.

So far it looks like people are interested in at least these "make data appear in this other place" filesystem operations:

Quoted 6 lines
Quoted 2 lines

1. reflink
2. reflink, but only if the contents are the same (dedupe)

What I meant by this was: if you ask for "regular copy", you may end up with a reflink anyway. Anyway, how can you reflink a range and have the contents *not* be the same?

reflink forcibly remaps fd_dest's range to fd_src's range. If they didn't match before, they will afterwards.

dedupe remaps fd_dest's range to fd_src's range only if they match, of course.

Perhaps I should have said "...if the contents are the same before the call"?

Quoted 6 lines
Quoted 4 lines

3. regular copy
4. regular copy, but make the hardware do it for us
5. regular copy, but require a second copy on the media (no-dedupe)

If this comes from me, I have no desire to ever use this as a flag.

I meant (5) as a "disable auto-dedupe for this operation" flag, not as a "reallocate all the shared blocks now" op...

Quoted 4 lines

If someone wants to use chattr or some new operation to say "make this range of this file belong just to me for purpose of optimizing future writes", then sure, go for it, with the understanding that there are plenty of filesystems for which that doesn't even make sense.

"Unshare these blocks" sounds more like something fallocate could do.

So far in my XFS reflink playground, it seems that using the defrag tool to un-cow a file makes most sense. AFAICT the XFS and ext4 defraggers copy a fragmented file's data to a second file and use a 'swap extents' operation, after which the donor file is unlinked.

Hey, if this syscall turns into a more generic "do something involving two (fd:off:len) (fd:off:len) tuples" call, I guess we could throw in "swap extents" as a 7th operation, to refactor the ioctls. <smirk>

Quoted 32 lines
Quoted 17 lines

6. regular copy, but don't CoW (eatmyothercopies) (joke)

(Please add whatever ops I missed.)

I think I can see a case for letting (4) fall back to (3) since (4) is an optimization of (3).

However, I particularly don't like the idea of (1) falling back to (3-5). Either the kernel can satisfy a request or it can't, but let's not just assume that we should transmogrify one type of request into another. Userspace should decide if a reflink failure should turn into one of the copy variants, depending on whether the user wants to spread allocation costs over rewrites or pay it all up front. Also, if we allow reflink to fall back to copy, how do programs find out what actually took place? Or do we simply not allow them to find out?

Quoted 7 lines

Also, programs that expect reflink either to finish or fail quickly might be surprised if it's possible for reflink to take a longer time than usual and with the side effect that a deep(er) copy was made.

I guess if someone asks for both (1) and (3) we can do the fallback in the kernel, like how we handle it right now.

I think we should focus on what the actual legit use cases might be. Certainly we want to support a mode that's "reflink or fail". We could have these flags:

COPY_FILE_RANGE_ALLOW_REFLINK
COPY_FILE_RANGE_ALLOW_COPY

Quoted 6 lines

Setting neither gets -EINVAL. Setting both works as is. Setting just ALLOW_REFLINK will fail if a reflink can't be supported. Setting just ALLOW_COPY will make a best-effort attempt not to reflink but expressly permits reflinking in cases where either (a) plain old write(2) might also result in a reflink or (b) there is no advantage to not reflinking.

I don't agree with having a 'copy' flag that can reflink when we also have a 'reflink' flag. I guess I just don't like having a flag with different meanings depending on context.

Quoted 5 lines

Users should be able to get the default behavior by passing '0' for flags, so provide FORBID_REFLINK and FORBID_COPY flags to turn off those behaviors, with an admonishment that one should only use them if they have a goooood reason. Passing neither gets you reflink-xor-copy, which is what I think we both want in the general case.

I agree here that 0 for flags should do something useful, and I wanted to double check if reflink-xor-copy is a good default behavior.

Quoted 1 line
Quoted 7 lines
Quoted 2 lines

FORBID_REFLINK = 1
FORBID_COPY = 2

I don't like the idea of using flags to forbid behavior. I think it would be more straightforward to have flags like REFLINK_ONLY or COPY_ONLY so users can tell us what they want, instead of what they don't want.

While I'm thinking about flags, COPY_FILE_RANGE_REFLINK_ONLY would be a bit of a mouthful. Does anybody have suggestions for ways that I could make this shorter?

If (and only if) it's _very_ well documented, you could probably drop the _ONLY part.

Quoted 32 lines
Quoted 18 lines

CHECK_SAME = 4
HW_COPY = 8

DEDUPE = (FORBID_COPY | CHECK_SAME)

What do you say to that?

Quoted 10 lines

An example of (b) would be a filesystem backed by deduped thinly-provisioned storage that can't do anything about ENOSPC because it doesn't control it in the first place.

Another option would be to split up the copy case into "I expect to overwrite a lot of the target file soon, so (c) try to commit space for that or (d) try to make it time-efficient". Of course, (d) is irrelevant on filesystems with no random access (nvdimms, for example).

Quoted 8 lines
Quoted 3 lines

I guess the tl;dr is that I'm highly skeptical of any use for disallowing reflinking other than forcibly committing space in cases where committing space actually means something.

That's more or less where I was going too. :)

--D

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#77 lore raw

PATCH 2/8 x86: add sys_copy_file_range to syscall tables

In reply to Anna Schumaker

From: Zach Brown <[email protected]>

Add sys_copy_file_range to the x86 syscall tables.

---
 arch/x86/entry/syscalls/syscall_32.tbl | 1 +
 arch/x86/entry/syscalls/syscall_64.tbl | 1 +
 2 files changed, 2 insertions(+)
arch/x86/entry/syscalls/syscall_32.tbl +1 -0
index ef8187f..2f5e1e0 100644
@@ -365,3 +365,4 @@
 356	i386	memfd_create		sys_memfd_create
 357	i386	bpf			sys_bpf
 358	i386	execveat		sys_execveat			stub32_execveat
+359	i386	copy_file_range		sys_copy_file_range
arch/x86/entry/syscalls/syscall_64.tbl +1 -0
index 9ef32d5..b2101de 100644
@@ -329,6 +329,7 @@
 320	common	kexec_file_load		sys_kexec_file_load
 321	common	bpf			sys_bpf
 322	64	execveat		stub_execveat
+323	common	copy_file_range		sys_copy_file_range
 
 #
 # x32-specific system call numbers start at 512 to avoid cache impact
Signature · 1 line
2.5.1
#78 lore raw

PATCH 3/8 btrfs: add .copy_file_range file operation

In reply to Anna Schumaker

From: Zach Brown <[email protected]>

This rearranges the existing COPY_RANGE ioctl implementation so that the .copy_file_range file operation can call the core loop that copies file data extent items.

The extent copying loop is lifted up into its own function. It retains the core btrfs error checks that should be shared.

---
 fs/btrfs/ctree.h |  3 ++
 fs/btrfs/file.c  |  1 +
 fs/btrfs/ioctl.c | 91 ++++++++++++++++++++++++++++++++------------------------
 3 files changed, 56 insertions(+), 39 deletions(-)
fs/btrfs/ctree.h +3 -0
index aac314e..e09d4e2 100644
@@ -4000,6 +4000,9 @@ int btrfs_dirty_pages(struct btrfs_root *root, struct inode *inode,
 		      loff_t pos, size_t write_bytes,
 		      struct extent_state **cached);
 int btrfs_fdatawrite_range(struct inode *inode, loff_t start, loff_t end);
+ssize_t btrfs_copy_file_range(struct file *file_in, loff_t pos_in,
+			      struct file *file_out, loff_t pos_out,
+			      size_t len, int flags);
 
 /* tree-defrag.c */
 int btrfs_defrag_leaves(struct btrfs_trans_handle *trans,
fs/btrfs/file.c +1 -0
index b823fac..b05449c 100644
@@ -2816,6 +2816,7 @@ const struct file_operations btrfs_file_operations = {
 #ifdef CONFIG_COMPAT
 	.compat_ioctl	= btrfs_ioctl,
 #endif
+	.copy_file_range = btrfs_copy_file_range,
 };
 
 void btrfs_auto_defrag_exit(void)
fs/btrfs/ioctl.c +52 -39

124 lines not on this page · raw message

Signature · 1 line
2.5.1

A message that is not in the archive (sent off-list or not yet received): 1441397823-1203-4-git-send-email-Anna.Schumaker-ZwjVKphTwtPQT0dZR+AlfA@public.gmane.org

Reviewed-by
#80 lore raw

Re: [PATCH v1 3/8] btrfs: add .copy_file_range file operation

Quoted 11 lines · Anna Schumaker

From: Zach Brown <[email protected]>

This rearranges the existing COPY_RANGE ioctl implementation so that the .copy_file_range file operation can call the core loop that copies file data extent items.

The extent copying loop is lifted up into its own function. It retains the core btrfs error checks that should be shared.

This bit looks fine to me,

Thanks,

Josef

Reviewed-by on v1 3/8
#81 lore raw

Re: [PATCH v1 3/8] btrfs: add .copy_file_range file operation

In reply to Anna Schumaker
Quoted 11 lines · Anna Schumaker

From: Zach Brown <[email protected]>

This rearranges the existing COPY_RANGE ioctl implementation so that the .copy_file_range file operation can call the core loop that copies file data extent items.

The extent copying loop is lifted up into its own function. It retains the core btrfs error checks that should be shared.

#82 lore raw

PATCH 7/8 vfs: Copy should use file_out rather than file_in

In reply to Anna Schumaker

The way to think about this is that the destination filesystem reads the data from the source file and processes it accordingly. This is especially important to avoid an infinate loop when doing a "server to server" copy on NFS.

---
 fs/read_write.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
fs/read_write.c +3 -3
index 59637ed..9dafb7f 100644
@@ -1355,7 +1355,7 @@ ssize_t vfs_copy_file_range(struct file *file_in, loff_t pos_in,
 	if (!(file_in->f_mode & FMODE_READ) ||
 	    !(file_out->f_mode & FMODE_WRITE) ||
 	    (file_out->f_flags & O_APPEND) ||
-	    !file_in->f_op || !file_in->f_op->copy_file_range)
+	    !file_out->f_op || !file_out->f_op->copy_file_range)
 		return -EBADF;
 
 	inode_in = file_inode(file_in);
@@ -1377,8 +1377,8 @@ ssize_t vfs_copy_file_range(struct file *file_in, loff_t pos_in,
 	if (ret)
 		return ret;
 
-	ret = file_in->f_op->copy_file_range(file_in, pos_in, file_out, pos_out,
-					     len, flags);
+	ret = file_out->f_op->copy_file_range(file_in, pos_in, file_out, pos_out,
+					      len, flags);
 	if (ret > 0) {
 		fsnotify_access(file_in);
 		add_rchar(current, ret);
Signature · 1 line
2.5.1