Skip to contentSkip to conversation

vfs: hoist reflink/dedupe ioctls to the VFS

RFC In review Darrick J. Wong 5 participants 19 messages Dec 19, 2015 – Aug 7, 2016

Subsystems (9)
x86 VFS Btrfs file system NFS client +5 more subsystems
lore
Share & tools
lore
#0 lore raw

v4 cover vfs: hoist reflink/dedupe ioctls to the VFS

Hi all,

This patch set goes along with the fourth revision of an RFC adding to XFS kernel support for tracking reverse-mappings of physical blocks to file and metadata; and support for mapping multiple file logical blocks to the same physical block, more commonly known as reflinking.

The first four patches are taken verbatim from Anna Schumaker's patches adding a physical copy call to the kernel. The next two patches are from Christoph Hellwig, and hoist the clone and clone_range ioctls into the VFS. These patches are a bit old at this point; they're in here solely to demonstrate how this (long) patchset diverges from upstream.

The third patch fixes some bugs in the first four patches, and the fourth patch hoists the extent_same ioctl into the VFS as the dedupe ioctl.

The patch set is based on the current (4.4-rc5) upstream kernel.

If you're going to start using this mess, you probably ought to just pull from my github trees for kernel[1], xfsprogs[2], and xfstests[3]. See also the xfs-docs[4] and manpage[5] updates.

This is an extraordinary way to eat your data. Enjoy!

Comments and questions are, as always, welcome.

--D

[1] https://github.com/djwong/linux/tree/for-dave
[2] https://github.com/djwong/xfsprogs/tree/for-dave
[3] https://github.com/djwong/xfstests/tree/for-dave
[4] https://github.com/djwong/xfs-documentation/tree/for-dave
[5] https://github.com/djwong/man-pages/commits/for-mtk

#1 lore raw

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

Quoted 2 lines

From : Anna Schumaker <[email protected]> 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,
Change flags parameter from int to unsigned int, Add function to include/linux/syscalls.h, Check copy len after file open mode, Don't forbid ranges inside the same file, Use rw_verify_area() to veriy ranges, Use file_out rather than file_in,
Add COPY_FR_REFLINK flag]

---
 fs/read_write.c                   |  120 +++++++++++++++++++++++++++++++++++++
 include/linux/fs.h                |    3 +
 include/linux/syscalls.h          |    3 +
 include/uapi/asm-generic/unistd.h |    4 +
 kernel/sys_ni.c                   |    1 
 5 files changed, 130 insertions(+), 1 deletion(-)
fs/read_write.c +120 -0
index 819ef3f..97c15ca 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,122 @@ 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, unsigned int flags)
+{
+	struct inode *inode_in = file_inode(file_in);
+	struct inode *inode_out = file_inode(file_out);
+	ssize_t ret;
+
+	if (flags != 0)
+		return -EINVAL;
+
+	/* 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_out->f_op->copy_file_range)
+		return -EBADF;
+
+	/* this could be relaxed once a method supports cross-fs copies */
+	if (inode_in->i_sb != inode_out->i_sb)
+		return -EXDEV;
+
+	if (len == 0)
+		return 0;
+
+	ret = mnt_want_write_file(file_out);
+	if (ret)
+		return ret;
+
+	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);
+		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 = -EBADF;
+
+	f_in = fdget(fd_in);
+	if (!f_in.file)
+		goto out2;
+
+	f_out = fdget(fd_out);
+	if (!f_out.file)
+		goto out1;
+
+	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);
+out1:
+	fdput(f_out);
+out2:
+	return ret;
+}
include/linux/fs.h +3 -0
index 3aa5142..e8a7362 100644
@@ -1629,6 +1629,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, unsigned int);
 };
 
 struct inode_operations {
@@ -1680,6 +1681,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, unsigned int);
 
 struct super_operations {
    	struct inode *(*alloc_inode)(struct super_block *sb);
include/linux/syscalls.h +3 -0
index c2b66a2..185815c 100644
@@ -886,6 +886,9 @@ asmlinkage long sys_execveat(int dfd, const char __user *filename,
 			const char __user *const __user *envp, int flags);
 
 asmlinkage long sys_membarrier(int cmd, int flags);
+asmlinkage long sys_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);
 
 asmlinkage long sys_mlock2(unsigned long start, size_t len, int flags);
 
include/uapi/asm-generic/unistd.h +3 -1
index 1324b02..2622b33 100644
@@ -715,9 +715,11 @@ __SYSCALL(__NR_userfaultfd, sys_userfaultfd)
 __SYSCALL(__NR_membarrier, sys_membarrier)
 #define __NR_mlock2 284
 __SYSCALL(__NR_mlock2, sys_mlock2)
+#define __NR_copy_file_range 285
+__SYSCALL(__NR_copy_file_range, sys_copy_file_range)
 
 #undef __NR_syscalls
-#define __NR_syscalls 285
+#define __NR_syscalls 286
 
 /*
  * All syscalls below here should go away really,
kernel/sys_ni.c +1 -0
index 0623787..2c5e3a8 100644
@@ -174,6 +174,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);
#2 lore raw

PATCH v4 2/9 x86: add sys_copy_file_range to syscall tables

In reply to Darrick J. Wong

Add sys_copy_file_range to the x86 syscall tables.

Quoted 2 lines

From : Anna Schumaker <[email protected]>
From : Zach Brown <[email protected]>

[Anna Schumaker: Update syscall number in syscall_32.tbl]
---
 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 f17705e..cb713df 100644
@@ -383,3 +383,4 @@
 374	i386	userfaultfd		sys_userfaultfd
 375	i386	membarrier		sys_membarrier
 376	i386	mlock2			sys_mlock2
+377	i386	copy_file_range		sys_copy_file_range
arch/x86/entry/syscalls/syscall_64.tbl +1 -0
index 314a90b..dc1040a 100644
@@ -332,6 +332,7 @@
 323	common	userfaultfd		sys_userfaultfd
 324	common	membarrier		sys_membarrier
 325	common	mlock2			sys_mlock2
+326	common	copy_file_range		sys_copy_file_range
 
 #
 # x32-specific system call numbers start at 512 to avoid cache impact

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs

#3 lore raw

PATCH v4 3/9 btrfs: add .copy_file_range file operation

In reply to Darrick J. Wong
Quoted 2 lines

From : Anna Schumaker <[email protected]> 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.

[Anna Schumaker: Make flags an unsigned int,
Check for COPY_FR_REFLINK]

---
 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 35489e7..ede7277 100644
@@ -4055,6 +4055,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, unsigned int flags);
 
 /* tree-defrag.c */
 int btrfs_defrag_leaves(struct btrfs_trans_handle *trans,
fs/btrfs/file.c +1 -0
index 72e7346..e67fe6a 100644
@@ -2924,6 +2924,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

#4 lore raw

PATCH v4 4/9 vfs: Add vfs_copy_file_range() support for pagecache copies

In reply to Darrick J. Wong
Quoted 1 line

From : Anna Schumaker <[email protected]>

This allows us to have an in-kernel copy mechanism that avoids frequent switches between kernel and user space. This is especially useful so NFSD can support server-side copies.

The default (flags=0) means to first attempt copy acceleration, but use the pagecache if that fails.

I moved the rw_verify_area() calls into the fallback code since some filesystems can handle reflinking a large range.

---
 fs/read_write.c |   13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
fs/read_write.c +9 -4
index 97c15ca..6f73af1 100644
@@ -1354,8 +1354,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->copy_file_range)
+	    (file_out->f_flags & O_APPEND))
 		return -EBADF;
 
 	/* this could be relaxed once a method supports cross-fs copies */
@@ -1369,8 +1368,14 @@ 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 == -EOPNOTSUPP)
+		ret = do_splice_direct(file_in, &pos_in, file_out, &pos_out,
+				len > MAX_RW_COUNT ? MAX_RW_COUNT : len, 0);
+
 	if (ret > 0) {
 		fsnotify_access(file_in);
 		add_rchar(current, ret);
#5 lore raw

PATCH v4 5/9 locks: new locks_mandatory_area calling convention

In reply to Darrick J. Wong
Quoted 1 line

From : Christoph Hellwig <[email protected]>

Pass a loff_t end for the last byte instead of the 32-bit count parameter to allow full file clones even on 32-bit architectures. While we're at it also drop the pointless inode argument and simplify the read/write selection.

---
 fs/locks.c         |   22 +++++++++-------------
 fs/read_write.c    |    5 ++---
 include/linux/fs.h |   28 +++++++++++++---------------
 3 files changed, 24 insertions(+), 31 deletions(-)
fs/locks.c +9 -13
index 0d2b326..ab2ea2e 100644
@@ -1227,21 +1227,17 @@ int locks_mandatory_locked(struct file *file)
 
 /**
  * locks_mandatory_area - Check for a conflicting lock
- * @read_write: %FLOCK_VERIFY_WRITE for exclusive access, %FLOCK_VERIFY_READ
- *		for shared
- * @inode:      the file to check
  * @filp:       how the file was opened (if it was)
- * @offset:     start of area to check
- * @count:      length of area to check
+ * @start:	first byte in the file to check
+ * @end:	lastbyte in the file to check
+ * @type:	%F_WRLCK for a write lock, else %F_RDLCK
  *
  * Searches the inode's list of locks to find any POSIX locks which conflict.
- * This function is called from rw_verify_area() and
- * locks_verify_truncate().
  */
-int locks_mandatory_area(int read_write, struct inode *inode,
-			 struct file *filp, loff_t offset,
-			 size_t count)
+int locks_mandatory_area(struct file *filp, loff_t start, loff_t end,
+		unsigned char type)
 {
+	struct inode *inode = file_inode(filp);
 	struct file_lock fl;
 	int error;
 	bool sleep = false;
@@ -1252,9 +1248,9 @@ int locks_mandatory_area(int read_write, struct inode *inode,
 	fl.fl_flags = FL_POSIX | FL_ACCESS;
 	if (filp && !(filp->f_flags & O_NONBLOCK))
 		sleep = true;
-	fl.fl_type = (read_write == FLOCK_VERIFY_WRITE) ? F_WRLCK : F_RDLCK;
-	fl.fl_start = offset;
-	fl.fl_end = offset + count - 1;
+	fl.fl_type = type;
+	fl.fl_start = start;
+	fl.fl_end = end;
 
 	for (;;) {
 		if (filp) {
fs/read_write.c +2 -3
index 6f73af1..e4c09dc 100644
@@ -396,9 +396,8 @@ int rw_verify_area(int read_write, struct file *file, const loff_t *ppos, size_t
 	}
 
 	if (unlikely(inode->i_flctx && mandatory_lock(inode))) {
-		retval = locks_mandatory_area(
-			read_write == READ ? FLOCK_VERIFY_READ : FLOCK_VERIFY_WRITE,
-			inode, file, pos, count);
+		retval = locks_mandatory_area(file, pos, pos + count - 1,
+				read_write == READ ? F_RDLCK : F_WRLCK);
 		if (retval < 0)
 			return retval;
 	}
include/linux/fs.h +13 -15
index e8a7362..73874cd 100644
@@ -2030,12 +2030,9 @@ extern struct kobject *fs_kobj;
 
 #define MAX_RW_COUNT (INT_MAX & PAGE_CACHE_MASK)
 
-#define FLOCK_VERIFY_READ  1
-#define FLOCK_VERIFY_WRITE 2
-
 #ifdef CONFIG_FILE_LOCKING
 extern int locks_mandatory_locked(struct file *);
-extern int locks_mandatory_area(int, struct inode *, struct file *, loff_t, size_t);
+extern int locks_mandatory_area(struct file *, loff_t, loff_t, unsigned char);
 
 /*
  * Candidates for mandatory locking have the setgid bit set
@@ -2068,14 +2065,16 @@ static inline int locks_verify_truncate(struct inode *inode,
 				    struct file *filp,
 				    loff_t size)
 {
-	if (inode->i_flctx && mandatory_lock(inode))
-		return locks_mandatory_area(
-			FLOCK_VERIFY_WRITE, inode, filp,
-			size < inode->i_size ? size : inode->i_size,
-			(size < inode->i_size ? inode->i_size - size
-			 : size - inode->i_size)
-		);
-	return 0;
+	if (!inode->i_flctx || !mandatory_lock(inode))
+		return 0;
+
+	if (size < inode->i_size) {
+		return locks_mandatory_area(filp, size, inode->i_size - 1,
+				F_WRLCK);
+	} else {
+		return locks_mandatory_area(filp, inode->i_size, size - 1,
+				F_WRLCK);
+	}
 }
 
 static inline int break_lease(struct inode *inode, unsigned int mode)
@@ -2144,9 +2143,8 @@ static inline int locks_mandatory_locked(struct file *file)
 	return 0;
 }
 
-static inline int locks_mandatory_area(int rw, struct inode *inode,
-				       struct file *filp, loff_t offset,
-				       size_t count)
+static inline int locks_mandatory_area(struct file *filp, loff_t start,
+		loff_t end, unsigned char type)
 {
 	return 0;
 }
#6 lore raw

PATCH v4 6/9 vfs: pull btrfs clone API to vfs layer

In reply to Darrick J. Wong
Quoted 1 line

From : Christoph Hellwig <[email protected]>

The btrfs clone ioctls are now adopted by other file systems, with NFS and CIFS already having support for them, and XFS being under active development. To avoid growth of various slightly incompatible implementations, add one to the VFS. Note that clones are different from file copies in several ways:

- they are atomic vs other writers
- they support whole file clones
- they support 64-bit legth clones
- they do not allow partial success (aka short writes)
- clones are expected to be a fast metadata operation

Because of that it would be rather cumbersome to try to piggyback them on top of the recent clone_file_range infrastructure. The converse isn't true and the clone_file_range system call could try clone file range as a first attempt to copy, something that further patches will enable.

Based on earlier work from Peng Tao.

---
 fs/btrfs/ctree.h        |    3 +
 fs/btrfs/file.c         |    1 
 fs/btrfs/ioctl.c        |   49 +-----------------
 fs/cifs/cifsfs.c        |   63 ++++++++++++++++++++++++
 fs/cifs/cifsfs.h        |    1 
 fs/cifs/ioctl.c         |  126 ++++++++++++++++++++++-------------------------
 fs/ioctl.c              |   29 +++++++++++
 fs/nfs/nfs4file.c       |   87 ++++----------------------------
 fs/read_write.c         |   72 +++++++++++++++++++++++++++
 include/linux/fs.h      |    7 ++-
 include/uapi/linux/fs.h |    9 +++
 11 files changed, 254 insertions(+), 193 deletions(-)
fs/btrfs/ctree.h +2 -1
index ede7277..dd4733f 100644
@@ -4025,7 +4025,6 @@ void btrfs_get_block_group_info(struct list_head *groups_list,
 void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
 			       struct btrfs_ioctl_balance_args *bargs);
 
-
 /* file.c */
 int btrfs_auto_defrag_init(void);
 void btrfs_auto_defrag_exit(void);
@@ -4058,6 +4057,8 @@ 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, unsigned int flags);
+int btrfs_clone_file_range(struct file *file_in, loff_t pos_in,
+			   struct file *file_out, loff_t pos_out, u64 len);
 
 /* tree-defrag.c */
 int btrfs_defrag_leaves(struct btrfs_trans_handle *trans,
fs/btrfs/file.c +1 -0
index e67fe6a..232e300 100644
@@ -2925,6 +2925,7 @@ const struct file_operations btrfs_file_operations = {
 	.compat_ioctl	= btrfs_ioctl,
 #endif
 	.copy_file_range = btrfs_copy_file_range,
+	.clone_file_range = btrfs_clone_file_range,
 };
 
 void btrfs_auto_defrag_exit(void)
fs/btrfs/ioctl.c +3 -46

62 lines not on this page · raw message

fs/cifs/cifsfs.c +63 -0

111 lines not on this page · raw message

fs/cifs/cifsfs.h +0 -1
index c3cc160..c399513 100644
@@ -131,7 +131,6 @@ extern int	cifs_setxattr(struct dentry *, const char *, const void *,
 extern ssize_t	cifs_getxattr(struct dentry *, const char *, void *, size_t);
 extern ssize_t	cifs_listxattr(struct dentry *, char *, size_t);
 extern long cifs_ioctl(struct file *filep, unsigned int cmd, unsigned long arg);
-
 #ifdef CONFIG_CIFS_NFSD_EXPORT
 extern const struct export_operations cifs_export_ops;
 #endif /* CONFIG_CIFS_NFSD_EXPORT */
fs/cifs/ioctl.c +59 -67

176 lines not on this page · raw message

fs/ioctl.c +29 -0
index 5d01d26..84c6e79 100644
@@ -215,6 +215,29 @@ static int ioctl_fiemap(struct file *filp, unsigned long arg)
 	return error;
 }
 
+static long ioctl_file_clone(struct file *dst_file, unsigned long srcfd,
+			     u64 off, u64 olen, u64 destoff)
+{
+	struct fd src_file = fdget(srcfd);
+	int ret;
+
+	if (!src_file.file)
+		return -EBADF;
+	ret = vfs_clone_file_range(src_file.file, off, dst_file, destoff, olen);
+	fdput(src_file);
+	return ret;
+}
+
+static long ioctl_file_clone_range(struct file *file, void __user *argp)
+{
+	struct file_clone_range args;
+
+	if (copy_from_user(&args, argp, sizeof(args)))
+		return -EFAULT;
+	return ioctl_file_clone(file, args.src_fd, args.src_offset,
+				args.src_length, args.dest_offset);
+}
+
 #ifdef CONFIG_BLOCK
 
 static inline sector_t logical_to_blk(struct inode *inode, loff_t offset)
@@ -600,6 +623,12 @@ int do_vfs_ioctl(struct file *filp, unsigned int fd, unsigned int cmd,
 	case FIGETBSZ:
 		return put_user(inode->i_sb->s_blocksize, argp);
 
+	case FICLONE:
+		return ioctl_file_clone(filp, arg, 0, 0, 0);
+
+	case FICLONERANGE:
+		return ioctl_file_clone_range(filp, argp);
+
 	default:
 		if (S_ISREG(inode->i_mode))
 			error = file_ioctl(filp, cmd, arg);
fs/nfs/nfs4file.c +10 -77

127 lines not on this page · raw message

fs/read_write.c +72 -0

75 lines not on this page · raw message

include/linux/fs.h +6 -1
index 73874cd..26b7607 100644
@@ -1629,7 +1629,10 @@ 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, unsigned int);
+	ssize_t (*copy_file_range)(struct file *, loff_t, struct file *,
+			loff_t, size_t, unsigned int);
+	int (*clone_file_range)(struct file *, loff_t, struct file *, loff_t,
+			u64);
 };
 
 struct inode_operations {
@@ -1683,6 +1686,8 @@ 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, unsigned int);
+extern int vfs_clone_file_range(struct file *file_in, loff_t pos_in,
+		struct file *file_out, loff_t pos_out, u64 len);
 
 struct super_operations {
    	struct inode *(*alloc_inode)(struct super_block *sb);
include/uapi/linux/fs.h +9 -0
index 6decbac..ad60359 100644
@@ -39,6 +39,13 @@
 #define RENAME_EXCHANGE		(1 << 1)	/* Exchange source and dest */
 #define RENAME_WHITEOUT		(1 << 2)	/* Whiteout source */
 
+struct file_clone_range {
+	__s64 src_fd;
+	__u64 src_offset;
+	__u64 src_length;
+	__u64 dest_offset;
+};
+
 struct fstrim_range {
 	__u64 start;
 	__u64 len;
@@ -168,6 +175,8 @@ struct blkzeroout2 {
 #define FIFREEZE	_IOWR('X', 119, int)	/* Freeze */
 #define FITHAW		_IOWR('X', 120, int)	/* Thaw */
 #define FITRIM		_IOWR('X', 121, struct fstrim_range)	/* Trim */
+#define FICLONE		_IOW(0x94, 9, int)
+#define FICLONERANGE	_IOW(0x94, 13, struct file_clone_range)
 
 #define	FS_IOC_GETFLAGS			_IOR('f', 1, long)
 #define	FS_IOC_SETFLAGS			_IOW('f', 2, long)
#7 lore raw

PATCH v4 7/9 vfs: wire up compat ioctl for CLONE/CLONE_RANGE

In reply to Darrick J. Wong
---
 fs/compat_ioctl.c |    4 ++++
 fs/read_write.c   |    2 +-
 2 files changed, 5 insertions(+), 1 deletion(-)
fs/compat_ioctl.c +4 -0
index dcf2653..70d4b10 100644
@@ -1580,6 +1580,10 @@ COMPAT_SYSCALL_DEFINE3(ioctl, unsigned int, fd, unsigned int, cmd,
 		goto out_fput;
 #endif
 
+	case FICLONE:
+	case FICLONERANGE:
+		goto do_ioctl;
+
 	case FIBMAP:
 	case FIGETBSZ:
 	case FIONREAD:
fs/read_write.c +1 -1
index 6395c733..0713e28 100644
@@ -1489,7 +1489,7 @@ int vfs_clone_file_range(struct file *file_in, loff_t pos_in,
 	if (S_ISDIR(inode_in->i_mode) || S_ISDIR(inode_out->i_mode))
 		return -EISDIR;
 	if (!S_ISREG(inode_in->i_mode) || !S_ISREG(inode_out->i_mode))
-		return -EOPNOTSUPP;
+		return -EINVAL;
 
 	if (!(file_in->f_mode & FMODE_READ) ||
 	    !(file_out->f_mode & FMODE_WRITE) ||
#8 lore raw

PATCH v4 8/9 vfs: hoist the btrfs deduplication ioctl to the vfs

In reply to Darrick J. Wong

Hoist the btrfs EXTENT_SAME ioctl up to the VFS and make the name more systematic (FIDEDUPERANGE).

---
 fs/compat_ioctl.c       |    1 
 fs/ioctl.c              |   38 ++++++++++++++++++
 fs/read_write.c         |  100 +++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/fs.h      |    4 ++
 include/uapi/linux/fs.h |   30 ++++++++++++++
 5 files changed, 173 insertions(+)
fs/compat_ioctl.c +1 -0
index 70d4b10..eab31e7 100644
@@ -1582,6 +1582,7 @@ COMPAT_SYSCALL_DEFINE3(ioctl, unsigned int, fd, unsigned int, cmd,
 
 	case FICLONE:
 	case FICLONERANGE:
+	case FIDEDUPERANGE:
 		goto do_ioctl;
 
 	case FIBMAP:
fs/ioctl.c +38 -0
index 84c6e79..fcdd33b 100644
@@ -568,6 +568,41 @@ static int ioctl_fsthaw(struct file *filp)
 	return thaw_super(sb);
 }
 
+static long ioctl_file_dedupe_range(struct file *file, void __user *arg)
+{
+	struct file_dedupe_range __user *argp = arg;
+	struct file_dedupe_range *same = NULL;
+	int ret;
+	unsigned long size;
+	u16 count;
+
+	if (get_user(count, &argp->dest_count)) {
+		ret = -EFAULT;
+		goto out;
+	}
+
+	size = offsetof(struct file_dedupe_range __user, info[count]);
+
+	same = memdup_user(argp, size);
+	if (IS_ERR(same)) {
+		ret = PTR_ERR(same);
+		same = NULL;
+		goto out;
+	}
+
+	ret = vfs_dedupe_file_range(file, same);
+	if (ret)
+		goto out;
+
+	ret = copy_to_user(argp, same, size);
+	if (ret)
+		ret = -EFAULT;
+
+out:
+	kfree(same);
+	return ret;
+}
+
 /*
  * When you add any new common ioctls to the switches above and below
  * please update compat_sys_ioctl() too.
@@ -629,6 +664,9 @@ int do_vfs_ioctl(struct file *filp, unsigned int fd, unsigned int cmd,
 	case FICLONERANGE:
 		return ioctl_file_clone_range(filp, argp);
 
+	case FIDEDUPERANGE:
+		return ioctl_file_dedupe_range(filp, argp);
+
 	default:
 		if (S_ISREG(inode->i_mode))
 			error = file_ioctl(filp, cmd, arg);
fs/read_write.c +100 -0

103 lines not on this page · raw message

include/linux/fs.h +4 -0
index 26b7607..bcfa23d 100644
@@ -1633,6 +1633,8 @@ struct file_operations {
 			loff_t, size_t, unsigned int);
 	int (*clone_file_range)(struct file *, loff_t, struct file *, loff_t,
 			u64);
+	ssize_t (*dedupe_file_range)(struct file *, u64, u64, struct file *,
+			u64);
 };
 
 struct inode_operations {
@@ -1688,6 +1690,8 @@ extern ssize_t vfs_copy_file_range(struct file *, loff_t , struct file *,
 				   loff_t, size_t, unsigned int);
 extern int vfs_clone_file_range(struct file *file_in, loff_t pos_in,
 		struct file *file_out, loff_t pos_out, u64 len);
+extern int vfs_dedupe_file_range(struct file *file,
+				 struct file_dedupe_range *same);
 
 struct super_operations {
    	struct inode *(*alloc_inode)(struct super_block *sb);
include/uapi/linux/fs.h +30 -0
index ad60359..801986e 100644
@@ -52,6 +52,35 @@ struct fstrim_range {
 	__u64 minlen;
 };
 
+/* extent-same (dedupe) ioctls; these MUST match the btrfs ioctl definitions */
+#define FILE_DEDUPE_RANGE_SAME		0
+#define FILE_DEDUPE_RANGE_DIFFERS	1
+
+/* from struct btrfs_ioctl_file_extent_same_info */
+struct file_dedupe_range_info {
+	__s64 dest_fd;		/* in - destination file */
+	__u64 dest_offset;	/* in - start of extent in destination */
+	__u64 bytes_deduped;	/* out - total # of bytes we were able
+				 * to dedupe from this file. */
+	/* status of this dedupe operation:
+	 * < 0 for error
+	 * == FILE_DEDUPE_RANGE_SAME if dedupe succeeds
+	 * == FILE_DEDUPE_RANGE_DIFFERS if data differs
+	 */
+	__s32 status;		/* out - see above description */
+	__u32 reserved;		/* must be zero */
+};
+
+/* from struct btrfs_ioctl_file_extent_same_args */
+struct file_dedupe_range {
+	__u64 src_offset;	/* in - start of extent in source */
+	__u64 src_length;	/* in - length of extent */
+	__u16 dest_count;	/* in - total elements in info array */
+	__u16 reserved1;	/* must be zero */
+	__u32 reserved2;	/* must be zero */
+	struct file_dedupe_range_info info[0];
+};
+
 /* And dynamically-tunable limits and defaults: */
 struct files_stat_struct {
 	unsigned long nr_files;		/* read only */
@@ -177,6 +206,7 @@ struct blkzeroout2 {
 #define FITRIM		_IOWR('X', 121, struct fstrim_range)	/* Trim */
 #define FICLONE		_IOW(0x94, 9, int)
 #define FICLONERANGE	_IOW(0x94, 13, struct file_clone_range)
+#define FIDEDUPERANGE	_IOWR(0x94, 54, struct file_dedupe_range)
 
 #define	FS_IOC_GETFLAGS			_IOR('f', 1, long)
 #define	FS_IOC_SETFLAGS			_IOW('f', 2, long)
#17 lore raw

PATCH v4 9/9 btrfs: use new dedupe data function pointer

In reply to Darrick J. Wong

Now that the VFS encapsulates the dedupe ioctl, wire up btrfs to it.

---
 fs/btrfs/ctree.h |    2 +
 fs/btrfs/file.c  |    1 
 fs/btrfs/ioctl.c |  110 ++++++------------------------------------------------
 3 files changed, 16 insertions(+), 97 deletions(-)
fs/btrfs/ctree.h +2 -0
index dd4733f..b7e4e34 100644
@@ -4024,6 +4024,8 @@ void btrfs_get_block_group_info(struct list_head *groups_list,
 				struct btrfs_ioctl_space_info *space);
 void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
 			       struct btrfs_ioctl_balance_args *bargs);
+ssize_t btrfs_dedupe_file_range(struct file *src_file, u64 loff, u64 olen,
+			   struct file *dst_file, u64 dst_loff);
 
 /* file.c */
 int btrfs_auto_defrag_init(void);
fs/btrfs/file.c +1 -0
index 232e300..d012e0a 100644
@@ -2926,6 +2926,7 @@ const struct file_operations btrfs_file_operations = {
 #endif
 	.copy_file_range = btrfs_copy_file_range,
 	.clone_file_range = btrfs_clone_file_range,
+	.dedupe_file_range = btrfs_dedupe_file_range,
 };
 
 void btrfs_auto_defrag_exit(void)
fs/btrfs/ioctl.c +13 -97

139 lines not on this page · raw message

#9 lore raw

Re: [PATCH 8/9] vfs: hoist the btrfs deduplication ioctl to the vfs

In reply to Darrick J. Wong

Some feedback on the VFS portion of the FIDEDUPERANGE ioctl and its man page... (note: I realize the patch is mostly just moving the code that already existed in btrfs, but in the VFS it deserves a more thorough review):

At high level, I am confused about what is meant by the "source" and "destination" files. I understand that with more than two files, you effectively have to choose one file to treat specially and dedupe with all the other files (an NxN comparison isn't realistic). But with just two files, a deduplication operation should be completely symmetric, should it not? The end result should be that the data is deduplicated, regardless of the order in which I gave the file descriptors. So why is there some non-symmetric behavior? There are several examples but one is that the VFS is checking !S_ISREG() on the "source" file descriptor but not on the "destination" file descriptor. Another is that different permissions are required on the source versus on the destination. If there are good reasons for the nonsymmetry then this needs to be clearly explained in the man page; otherwise it may not be clear what to use as the "source" and what to use as the "destination".

It seems odd to be adding "copy" as a system call but then have "dedupe" and "clone" as ioctls rather than system calls... it seems that they should all be one or the other (at least, if we put aside the fact that the ioctls already exist in btrfs).

The range checking in clone_verify_area() appears incomplete. Someone could provide len=UINT64_MAX and all the checks would still pass even though 'pos+len' would overflow.

Should the ioctl be interruptible? Right now it always goes through *all* the 'struct file_dedupe_range_info's you passed in --- potentially up to 65535 of them.

Why 'info->bytes_deduped += deduped' rather than 'info->bytes_deduped = deduped'? 'bytes_deduped' is per file descriptor, not for the operation as a whole.

What permissions do you need on the destination file descriptors? The man page implies they must be open for writing and not appending. The implementation
differs: it requires FMODE_WRITE only for non-admin users, and it doesn't check for O_APPEND at all. The man page also says you get EPERM if "dest_fd is immutable" and ETXTBSY if "one of the files is a swap file", which I don't see actually happening in the implementation; it seems those error codes perhaps exist at all for this ioctl but rather be left to open(..., O_WRONLY).

If the filesystem doesn't support deduplication, or I pass in a strange file descriptor such as one for a named pipe, do I get EINVAL or EOPNOTSUPP? The man page isn't clear.

Under what circumstances will 'bytes_deduped' differ from the count that was passed in? If short counts are allowed, what will be the 'status' be in that
case: FILE_DEDUP_RANGE_DIFFERS, FILE_DEDUPE_RANGE_SAME, or something else? Can data be deduped even if only a prefix of the data region matches?

The man page doesn't mention FILE_DEDUPE_RANGE_SAME at all, instead calling it 0; it only mentions FILE_DEDUPE_RANGE_DIFFERS.

The man page isn't clear about whether the ioctl stops early if an error occurs or always processes all the 'struct file_dedupe_range_info's you pass in. And if it were, hypothetically, to stop early, how is the user meant to know on which file it stopped?

The man page says "logical_offset" but in the struct it is called "dest_offset".

There are some variables named "same" which don't really make sense now that the ioctl is called FIDEDUPERANGE instead of EXTENT_SAME.

Eric

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs

#10 lore raw

Re: [PATCH 8/9] vfs: hoist the btrfs deduplication ioctl to the vfs

[adding btrfs to the cc since we're talking about a whole new dedupe interface]

Quoted 3 lines · Eric Biggers

Some feedback on the VFS portion of the FIDEDUPERANGE ioctl and its man page... (note: I realize the patch is mostly just moving the code that already existed in btrfs, but in the VFS it deserves a more thorough review):

Wheee. :)

Yes, let's discuss the concerns about the btrfs extent same ioctl.

I believe Christoph dislikes about the odd return mechanism (i.e. status and bytes_deduped) and doubts that the vectorization is really necessary. There's not a lot of documentation to go on aside from "Do whatever the BTRFS ioctl does". I suspect that will leave my explanations lackng, since I neither designed the btrfs interface nor know all that much about the decisions made to arrive at what we have now.

(I agree with both of hch's complaints.)

Really, the best argument for keeping this ioctl is to avoid breaking duperemove. Even then, given that current duperemove checks for btrfs before trying to use BTRFS_IOC_EXTENT_SAME, we could very well design a new dedupe ioctl for the VFS, hook the new dedupers (XFS) into the new VFS ioctl leaving the old btrfs ioctl intact, and train duperemove to try the new ioctl and fall back on the btrfs one if the VFS ioctl isn't supported.

Frankly, I also wouldn't mind changing the VFS dedupe ioctl that to something that resembles the clone_range interface:

int ioctl(int dest_fd, FIDEDUPERANGE, struct file_dedupe_range * arg);

struct file_dedupe_range {
    __s64 src_fd;
    __u64 src_offset;
    __u64 length;
    __u64 dest_offset;
    __u64 flags;
};

"See if the byte range src_offset:length in src_fd matches all of dest_offset:length in dest_fd; if so, share src_fd's physical storage with dest_fd. Both fds must be files; if they are the same file the ranges cannot overlap; src_fd must be readable; dest_fd must be writable or append-only. Offsets and lengths probably need to be block-aligned, but that is filesystem dependent."

The error conditions would be superset of the ones we know about today. I'd return EOVERFLOW or something if length is longer than the FS wants to deal with.

Now all the vectorization problems go away, and since it's a new VFS interface we can define everything from the start.

Christoph, if this new interface solves your complaints I think I'd like to get started on the code/docs soon.

Quoted 6 lines

At high level, I am confused about what is meant by the "source" and "destination" files. I understand that with more than two files, you effectively have to choose one file to treat specially and dedupe with all the other files (an NxN comparison isn't realistic). But with just two files, a deduplication operation should be completely symmetric, should it not? The end

Not sure what you mean by 'symmetric', but in any case the convention seems to be that src_fd's storage is shared with dest_fd if there's a match.

Quoted 5 lines

result should be that the data is deduplicated, regardless of the order in which I gave the file descriptors. So why is there some non-symmetric behavior? There are several examples but one is that the VFS is checking !S_ISREG() on the "source" file descriptor but not on the "destination" file descriptor.

The dedupe_range function pointer should only be supplied for regular files.

Quoted 9 lines

Another is that different permissions are required on the source versus on the destination. If there are good reasons for the nonsymmetry then this needs to be clearly explained in the man page; otherwise it may not be clear what to use as the "source" and what to use as the "destination".

It seems odd to be adding "copy" as a system call but then have "dedupe" and "clone" as ioctls rather than system calls... it seems that they should all be one or the other (at least, if we put aside the fact that the ioctls already exist in btrfs).

We can't put the clone ioctl aside; coreutils has already started using it.

I'm not sure if clone_range or extent_same are all that popular, though. AFAIK duperemove is the only program using extent_same, and I don't know of anything using clone_range.

(Well, xfs_io does...)

Quoted 3 lines

The range checking in clone_verify_area() appears incomplete. Someone could provide len=UINT64_MAX and all the checks would still pass even though 'pos+len' would overflow.

Yeah...

Quoted 3 lines

Should the ioctl be interruptible? Right now it always goes through *all* the 'struct file_dedupe_range_info's you passed in --- potentially up to 65535 of them.

There probably ought to be explicit signal checks, or we could just get rid of the vectorization entirely. :)

Quoted 3 lines

Why 'info->bytes_deduped += deduped' rather than 'info->bytes_deduped = deduped'? 'bytes_deduped' is per file descriptor, not for the operation as a whole.

Right, because bytes_deduped is a part of file_dedup_range_info, not file_dedupe_range.

(Note the bytes_deduped = 0 earlier in the function.)

Quoted 4 lines

What permissions do you need on the destination file descriptors? The man page implies they must be open for writing and not appending. The implementation differs: it requires FMODE_WRITE only for non-admin users, and it doesn't check for O_APPEND at all.

I think the result of an earlier discussion was that src_fd must be readable, and dest_fd must be writable or appendable.

Quoted 4 lines

The man page also says you get EPERM if "dest_fd is immutable" and ETXTBSY if "one of the files is a swap file", which I don't see actually happening in the implementation; it seems those error codes perhaps exist at all for this ioctl but rather be left to open(..., O_WRONLY).

Those could be hoisted to the VFS (from the XFS implementation), I think.

Quoted 3 lines

If the filesystem doesn't support deduplication, or I pass in a strange file descriptor such as one for a named pipe, do I get EINVAL or EOPNOTSUPP? The man page isn't clear.

Should be EOPNOTSUPP if dest_fd isn't a regular file; EISDIR if either are directories; and EINVAL for any other kind of non-file fd. I suspect the clone* manpages don't make this too clear either.

Quoted 2 lines

Under what circumstances will 'bytes_deduped' differ from the count that was passed in?

btrfs/xfs will only compare the first 16MB. Not documented anywhere. :(

Quoted 2 lines

If short counts are allowed, what will be the 'status' be in that case:
FILE_DEDUP_RANGE_DIFFERS, FILE_DEDUPE_RANGE_SAME, or something else?

One of those two.

Quoted 1 line

Can data be deduped even if only a prefix of the data region matches?

No.

Quoted 2 lines

The man page doesn't mention FILE_DEDUPE_RANGE_SAME at all, instead calling it 0; it only mentions FILE_DEDUPE_RANGE_DIFFERS.

Oops, good catch. :(

Quoted 4 lines

The man page isn't clear about whether the ioctl stops early if an error occurs or always processes all the 'struct file_dedupe_range_info's you pass in. And if it were, hypothetically, to stop early, how is the user meant to know on which file it stopped?

I don't know if this should be the official behavior, but it stopped at whichever file_dedupe_range_info has both status and bytes_deduped set to zero.

Quoted 2 lines

The man page says "logical_offset" but in the struct it is called "dest_offset".

Oops.

Quoted 2 lines

There are some variables named "same" which don't really make sense now that the ioctl is called FIDEDUPERANGE instead of EXTENT_SAME.

Perhaps not.

I'll later take a look at how many of these issues apply to clone/clone_range.

--D

Quoted 6 lines

Eric
--
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

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs

#11 lore raw

Re: [PATCH 8/9] vfs: hoist the btrfs deduplication ioctl to the vfs

Quoted 5 lines · Darrick J. Wong

Frankly, I also wouldn't mind changing the VFS dedupe ioctl that to something that resembles the clone_range interface:

int ioctl(int dest_fd, FIDEDUPERANGE, struct file_dedupe_range * arg);

Getting rid of the vectorization certainly avoids a lot of API complexity. I don't know precisely why it was made vectorized in the first place, but to me it seems it shouldn't be vectorized unless there's a strong reason for it to be.

Quoted 5 lines

Not sure what you mean by 'symmetric', but in any case the convention seems to be that src_fd's storage is shared with dest_fd if there's a match.
...
I think the result of an earlier discussion was that src_fd must be readable, and dest_fd must be writable or appendable.

Deduplication is a strange operation since you're not "reading" or "writing" in the traditional sense. All file data stays exactly the same from a user perspective.

So rather than arbitrarily say that you're "reading" from one file and "writing" to the other, one possibility is to have an API where you submit two files and the implementation decides how to best deduplicate them. That could involve sharing existing extents in file 1 with file 2; or sharing existing extents in file 2 with file 1; or creating a new extent and referencing it from each file. It would be up to the implementation to choose the most efficient approach.

But I take it that the existing btrfs ioctl doesn't work that way, and it always will share the existing extents in file 1 with file 2.

That's probably a perfectly fine way to do it, but I was wondering whether it really makes sense to hard-code that detail in the API, since an API could be defined more generally.

And from a practical perpective, people using the ioctl to deduplicate two files need to know which one to submit to the API as the "source" and which as the "destination", if it matters.

Related to this are the permissions you need on each file descriptor. Since deduplication isn't "reading" or "writing" in the traditional sense it could, theoretically, be argued that no permissions at all should be required: neither FMODE_READ nor FMODE_WRITE.

Most likely are concerns that would make that a bad idea, though. Perhaps someone could create mischief by causing fragmentation in system files.

If this was previously discussed, can you point me to that discussion?

The proposed man page is also very brief on permissions, only mentioning them indirectly in the error codes section.

Quoted 6 lines
Quoted 4 lines

The man page also says you get EPERM if "dest_fd is immutable" and ETXTBSY if "one of the files is a swap file", which I don't see actually happening in the implementation; it seems those error codes perhaps exist at all for this ioctl but rather be left to open(..., O_WRONLY).

Those could be hoisted to the VFS (from the XFS implementation), I think.

If the destination file has to already be open for writing then it can't be immutable. So I don't think that error code is needed. Checking for an active swapfile, on the other hand, may be valid, although I don't see such a check anywhere in the version of the code I'm looking at.

Quoted 1 line

I'll later take a look at how many of these issues apply to clone/clone_range.

clone and clone_range seem to avoid the two biggest issues I saw: they aren't vectorized, and there's a natural distinction between the "source" and the "destination" of the operation. They definitely need careful consideration as well, though.

#12 lore raw

Re: [PATCH 8/9] vfs: hoist the btrfs deduplication ioctl to the vfs

Quoted 10 lines · Eric Biggers
Quoted 5 lines · Darrick J. Wong

Frankly, I also wouldn't mind changing the VFS dedupe ioctl that to something that resembles the clone_range interface:

int ioctl(int dest_fd, FIDEDUPERANGE, struct file_dedupe_range * arg);

Getting rid of the vectorization certainly avoids a lot of API complexity. I don't know precisely why it was made vectorized in the first place, but to me it seems it shouldn't be vectorized unless there's a strong reason for it to be.

I suppose the idea was that you could ask the kernel to dedupe a bunch of files "atomically" and that locks would be held (at least on the source file) during the entire operation...?

Ick. Given that you can have 65536 vectors of 16MB apiece, you could be reading up to 1TB of data into the pagecache. That's a lot for a single ioctl, though I guess we unlock all the inodes between each vector.

(Welll... we don't check for pending signals. Urk.)

Quoted 10 lines
Quoted 5 lines

Not sure what you mean by 'symmetric', but in any case the convention seems to be that src_fd's storage is shared with dest_fd if there's a match.
...
I think the result of an earlier discussion was that src_fd must be readable, and dest_fd must be writable or appendable.

Deduplication is a strange operation since you're not "reading" or "writing" in the traditional sense. All file data stays exactly the same from a user perspective.

Quoted 6 lines

So rather than arbitrarily say that you're "reading" from one file and "writing" to the other, one possibility is to have an API where you submit two files and the implementation decides how to best deduplicate them. That could involve sharing existing extents in file 1 with file 2; or sharing existing extents in file 2 with file 1; or creating a new extent and referencing it from each file. It would be up to the implementation to choose the most efficient approach.

That's a very good point! We needn't limit the API to whatever the first implementer does; all of the above are valid interpretations of what dedupe could involve. I might even argue that this is the use-case for the vectorized dedupe call -- analyze all these proposed deduplications and figure out the best plan to make it happen.

(Ofc the current implementation doesn't allow this, but we can change the code when the singularity happens, etc.)

Quoted 7 lines

But I take it that the existing btrfs ioctl doesn't work that way, and it always will share the existing extents in file 1 with file 2.

That's probably a perfectly fine way to do it, but I was wondering whether it really makes sense to hard-code that detail in the API, since an API could be defined more generally.

Quoted 8 lines

And from a practical perpective, people using the ioctl to deduplicate two files need to know which one to submit to the API as the "source" and which as the "destination", if it matters.

Related to this are the permissions you need on each file descriptor. Since deduplication isn't "reading" or "writing" in the traditional sense it could, theoretically, be argued that no permissions at all should be required: neither FMODE_READ nor FMODE_WRITE.

I don't like the idea of being able to make major changes to a file that I've opened with O_RDONLY. :)

I suppose if you're going to allow the FS to figure out its own strategy and all that, then by the above reasoning one ought to have all the files opened as writable.

Quoted 4 lines

Most likely are concerns that would make that a bad idea, though. Perhaps someone could create mischief by causing fragmentation in system files.

If this was previously discussed, can you point me to that discussion?

I don't know if there was a previous discussion, alas.

Quoted 2 lines

The proposed man page is also very brief on permissions, only mentioning them indirectly in the error codes section.

There's a lot of unfortunate hand-waving in that manpage -- I wasn't involved in designing the initial ioctl, so for the most part I'm simply sniffing my way through based on observed behaviors (of duperemove) and guessing based on the source code as I write the XFS version. :)

Quoted 11 lines
Quoted 6 lines
Quoted 4 lines

The man page also says you get EPERM if "dest_fd is immutable" and ETXTBSY if "one of the files is a swap file", which I don't see actually happening in the implementation; it seems those error codes perhaps exist at all for this ioctl but rather be left to open(..., O_WRONLY).

Those could be hoisted to the VFS (from the XFS implementation), I think.

If the destination file has to already be open for writing then it can't be immutable. So I don't think that error code is needed. Checking for an active swapfile, on the other hand, may be valid, although I don't see such a check anywhere in the version of the code I'm looking at.

It's at the top of xfs_file_share_range() in the more recent RFCs of XFS reflink.

Quoted 6 lines
Quoted 1 line

I'll later take a look at how many of these issues apply to clone/clone_range.

clone and clone_range seem to avoid the two biggest issues I saw: they aren't vectorized, and there's a natural distinction between the "source" and the "destination" of the operation. They definitely need careful consideration as well, though.

Indeed. I appreciate your review of the manpages/patches!

--D

Quoted 4 lines

--
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

#14 lore raw

Re: [PATCH 8/9] vfs: hoist the btrfs deduplication ioctl to the vfs

In reply to Darrick J. Wong
Quoted 37 lines · Darrick J. Wong

Hoist the btrfs EXTENT_SAME ioctl up to the VFS and make the name more systematic (FIDEDUPERANGE).

---
 fs/compat_ioctl.c       |    1 
 fs/ioctl.c              |   38 ++++++++++++++++++
 fs/read_write.c         |  100 +++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/fs.h      |    4 ++
 include/uapi/linux/fs.h |   30 ++++++++++++++
 5 files changed, 173 insertions(+)
fs/compat_ioctl.c +1 -0
index 70d4b10..eab31e7 100644
@@ -1582,6 +1582,7 @@ COMPAT_SYSCALL_DEFINE3(ioctl, unsigned int, fd, unsigned int, cmd,
 
 	case FICLONE:
 	case FICLONERANGE:
+	case FIDEDUPERANGE:
 		goto do_ioctl;
 
 	case FIBMAP:
fs/ioctl.c +4 -0
index 84c6e79..fcdd33b 100644
@@ -568,6 +568,41 @@ static int ioctl_fsthaw(struct file *filp)
 	return thaw_super(sb);
 }
 
+static long ioctl_file_dedupe_range(struct file *file, void __user *arg)
+{
+	struct file_dedupe_range __user *argp = arg;
+	struct file_dedupe_range *same = NULL;

fs/ioctl.c static int ioctl_fsthaw(struct file *filp) · line 584

Quoted 10 lines
fs/ioctl.c +10 -0
+	int ret;
+	unsigned long size;
+	u16 count;
+
+	if (get_user(count, &argp->dest_count)) {
+		ret = -EFAULT;
+		goto out;
+	}
+
+	size = offsetof(struct file_dedupe_range __user, info[count]);

Vlastimil triggered this during fuzzing:

http://paste.opensuse.org/view/raw/99203426

High order allocation without __GFP_NOWARN + fallback. That's not good.

Basically, we don't have any sanity check of 'dest_count' here. This u16 comes directly from userspace. And we call memdup_user() based on it.

Here's a program which makes kernel allocate order-9 page:

https://gist.github.com/kiryl/2b344b51da1fd2725be420a996b10d22

Should we put some reasonable upper limit for the 'dest_count'? What is typical 'dest_count'?

Quoted 21 lines
fs/ioctl.c +21 -0
+
+	same = memdup_user(argp, size);
+	if (IS_ERR(same)) {
+		ret = PTR_ERR(same);
+		same = NULL;
+		goto out;
+	}
+
+	ret = vfs_dedupe_file_range(file, same);
+	if (ret)
+		goto out;
+
+	ret = copy_to_user(argp, same, size);
+	if (ret)
+		ret = -EFAULT;
+
+out:
+	kfree(same);
+	return ret;
+}
+
Signature · 1 line
 Kirill A. Shutemov
#15 lore raw

Re: [PATCH 8/9] vfs: hoist the btrfs deduplication ioctl to the vfs

Quoted 34 lines · Kirill A. Shutemov
Quoted 33 lines · Darrick J. Wong

Hoist the btrfs EXTENT_SAME ioctl up to the VFS and make the name more systematic (FIDEDUPERANGE).

---
 fs/compat_ioctl.c       |    1 
 fs/ioctl.c              |   38 ++++++++++++++++++
 fs/read_write.c         |  100 +++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/fs.h      |    4 ++
 include/uapi/linux/fs.h |   30 ++++++++++++++
 5 files changed, 173 insertions(+)
fs/compat_ioctl.c +1 -0
index 70d4b10..eab31e7 100644
@@ -1582,6 +1582,7 @@ COMPAT_SYSCALL_DEFINE3(ioctl, unsigned int, fd, unsigned int, cmd,
 
 	case FICLONE:
 	case FICLONERANGE:
+	case FIDEDUPERANGE:
 		goto do_ioctl;
 
 	case FIBMAP:
fs/ioctl.c +0 -0
index 84c6e79..fcdd33b 100644
@@ -568,6 +568,41 @@ static int ioctl_fsthaw(struct file *filp)
 	return thaw_super(sb);
 }
 
Quoted 14 lines
Quoted 4 lines
fs/ioctl.c +4 -0
+static long ioctl_file_dedupe_range(struct file *file, void __user *arg)
+{
+	struct file_dedupe_range __user *argp = arg;
+	struct file_dedupe_range *same = NULL;

fs/ioctl.c static int ioctl_fsthaw(struct file *filp) · line 584

Quoted 10 lines
fs/ioctl.c +10 -0
+	int ret;
+	unsigned long size;
+	u16 count;
+
+	if (get_user(count, &argp->dest_count)) {
+		ret = -EFAULT;
+		goto out;
+	}
+
+	size = offsetof(struct file_dedupe_range __user, info[count]);

(I still hate this interface.)

Quoted 9 lines

Vlastimil triggered this during fuzzing:

http://paste.opensuse.org/view/raw/99203426

High order allocation without __GFP_NOWARN + fallback. That's not good.

Basically, we don't have any sanity check of 'dest_count' here. This u16 comes directly from userspace. And we call memdup_user() based on it.

Quoted 6 lines

Here's a program which makes kernel allocate order-9 page:

https://gist.github.com/kiryl/2b344b51da1fd2725be420a996b10d22

Should we put some reasonable upper limit for the 'dest_count'? What is typical 'dest_count'?

There are two userland programs I know of that call this ioctl. The first is xfs_io, which always sets dest_count = 1.

The other is duperemove, which seems capable of setting dest_count to however many fragments it finds, up to a max of 120. Capping size to x86's 4k page size yields 127 entries. On bigger machines with 64k pages, that increases to 2047. I think that's enough for anybody.

(Honestly, 127 dedupe candidates * max 16M extent length is already 2GB of IO for a single call.)

--D

Quoted 25 lines
Quoted 22 lines
fs/ioctl.c +21 -0
+
+	same = memdup_user(argp, size);
+	if (IS_ERR(same)) {
+		ret = PTR_ERR(same);
+		same = NULL;
+		goto out;
+	}
+
+	ret = vfs_dedupe_file_range(file, same);
+	if (ret)
+		goto out;
+
+	ret = copy_to_user(argp, same, size);
+	if (ret)
+		ret = -EFAULT;
+
+out:
+	kfree(same);
+	return ret;
+}
+
Signature · 1 line
 Kirill A. Shutemov
#16 lore raw

Re: [PATCH 8/9] vfs: hoist the btrfs deduplication ioctl to the vfs

In reply to Kirill A. Shutemov
Quoted 53 lines · Kirill A. Shutemov
Quoted 37 lines · Darrick J. Wong

Hoist the btrfs EXTENT_SAME ioctl up to the VFS and make the name more systematic (FIDEDUPERANGE).

---
 fs/compat_ioctl.c       |    1 
 fs/ioctl.c              |   38 ++++++++++++++++++
 fs/read_write.c         |  100 +++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/fs.h      |    4 ++
 include/uapi/linux/fs.h |   30 ++++++++++++++
 5 files changed, 173 insertions(+)
fs/compat_ioctl.c +1 -0
index 70d4b10..eab31e7 100644
@@ -1582,6 +1582,7 @@ COMPAT_SYSCALL_DEFINE3(ioctl, unsigned int, fd, unsigned int, cmd,
 
 	case FICLONE:
 	case FICLONERANGE:
+	case FIDEDUPERANGE:
 		goto do_ioctl;
 
 	case FIBMAP:
fs/ioctl.c +4 -0
index 84c6e79..fcdd33b 100644
@@ -568,6 +568,41 @@ static int ioctl_fsthaw(struct file *filp)
 	return thaw_super(sb);
 }
 
+static long ioctl_file_dedupe_range(struct file *file, void __user *arg)
+{
+	struct file_dedupe_range __user *argp = arg;
+	struct file_dedupe_range *same = NULL;

fs/ioctl.c static int ioctl_fsthaw(struct file *filp) · line 584

Quoted 10 lines
fs/ioctl.c +10 -0
+	int ret;
+	unsigned long size;
+	u16 count;
+
+	if (get_user(count, &argp->dest_count)) {
+		ret = -EFAULT;
+		goto out;
+	}
+
+	size = offsetof(struct file_dedupe_range __user, info[count]);

Vlastimil triggered this during fuzzing:

http://paste.opensuse.org/view/raw/99203426

Quoted 8 lines

High order allocation without __GFP_NOWARN + fallback. That's not good.

Basically, we don't have any sanity check of 'dest_count' here. This u16 comes directly from userspace. And we call memdup_user() based on it.

Here's a program which makes kernel allocate order-9 page:

https://gist.github.com/kiryl/2b344b51da1fd2725be420a996b10d22

I forgot to say, please wrap this up as an xfstest so we can check for future regressions. After the patch, the ioctl should return ENOMEM to signal that the caller asked for more dedupe_count than we want to allocate memory for.

--D

Quoted 31 lines

Should we put some reasonable upper limit for the 'dest_count'? What is typical 'dest_count'?

Quoted 21 lines
fs/ioctl.c +21 -0
+
+	same = memdup_user(argp, size);
+	if (IS_ERR(same)) {
+		ret = PTR_ERR(same);
+		same = NULL;
+		goto out;
+	}
+
+	ret = vfs_dedupe_file_range(file, same);
+	if (ret)
+		goto out;
+
+	ret = copy_to_user(argp, same, size);
+	if (ret)
+		ret = -EFAULT;
+
+out:
+	kfree(same);
+	return ret;
+}
+
Signature · 5 lines
 Kirill A. Shutemov
--
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
#13 lore raw

Re: [PATCH 8/9] vfs: hoist the btrfs deduplication ioctl to the vfs

In reply to Darrick J. Wong

Hi Darrick,

Quoted 1 line · Darrick J. Wong

[adding btrfs to the cc since we're talking about a whole new dedupe interface]

In the discussion below, many points of possible improvement were notedfor the man page.... Would you be willing to put together a patch please?

Thanks,

Michael

Quoted 188 lines · Eric Biggers
Quoted 3 lines

Some feedback on the VFS portion of the FIDEDUPERANGE ioctl and its man page... (note: I realize the patch is mostly just moving the code that already existed in btrfs, but in the VFS it deserves a more thorough review):

Wheee. :)

Yes, let's discuss the concerns about the btrfs extent same ioctl.

I believe Christoph dislikes about the odd return mechanism (i.e. status and bytes_deduped) and doubts that the vectorization is really necessary. There's not a lot of documentation to go on aside from "Do whatever the BTRFS ioctl does". I suspect that will leave my explanations lackng, since I neither designed the btrfs interface nor know all that much about the decisions made to arrive at what we have now.

(I agree with both of hch's complaints.)

Really, the best argument for keeping this ioctl is to avoid breaking duperemove. Even then, given that current duperemove checks for btrfs before trying to use BTRFS_IOC_EXTENT_SAME, we could very well design a new dedupe ioctl for the VFS, hook the new dedupers (XFS) into the new VFS ioctl leaving the old btrfs ioctl intact, and train duperemove to try the new ioctl and fall back on the btrfs one if the VFS ioctl isn't supported.

Frankly, I also wouldn't mind changing the VFS dedupe ioctl that to something that resembles the clone_range interface:

int ioctl(int dest_fd, FIDEDUPERANGE, struct file_dedupe_range * arg);

struct file_dedupe_range {
    __s64 src_fd;
    __u64 src_offset;
    __u64 length;
    __u64 dest_offset;
    __u64 flags;
};

"See if the byte range src_offset:length in src_fd matches all of dest_offset:length in dest_fd; if so, share src_fd's physical storage with dest_fd. Both fds must be files; if they are the same file the ranges cannot overlap; src_fd must be readable; dest_fd must be writable or append-only. Offsets and lengths probably need to be block-aligned, but that is filesystem dependent."

The error conditions would be superset of the ones we know about today. I'd return EOVERFLOW or something if length is longer than the FS wants to deal with.

Now all the vectorization problems go away, and since it's a new VFS interface we can define everything from the start.

Christoph, if this new interface solves your complaints I think I'd like to get started on the code/docs soon.

Quoted 6 lines

At high level, I am confused about what is meant by the "source" and "destination" files. I understand that with more than two files, you effectively have to choose one file to treat specially and dedupe with all the other files (an NxN comparison isn't realistic). But with just two files, a deduplication operation should be completely symmetric, should it not? The end

Not sure what you mean by 'symmetric', but in any case the convention seems to be that src_fd's storage is shared with dest_fd if there's a match.

Quoted 5 lines

result should be that the data is deduplicated, regardless of the order in which I gave the file descriptors. So why is there some non-symmetric behavior? There are several examples but one is that the VFS is checking !S_ISREG() on the "source" file descriptor but not on the "destination" file descriptor.

The dedupe_range function pointer should only be supplied for regular files.

Quoted 9 lines

Another is that different permissions are required on the source versus on the destination. If there are good reasons for the nonsymmetry then this needs to be clearly explained in the man page; otherwise it may not be clear what to use as the "source" and what to use as the "destination".

It seems odd to be adding "copy" as a system call but then have "dedupe" and "clone" as ioctls rather than system calls... it seems that they should all be one or the other (at least, if we put aside the fact that the ioctls already exist in btrfs).

We can't put the clone ioctl aside; coreutils has already started using it.

I'm not sure if clone_range or extent_same are all that popular, though. AFAIK duperemove is the only program using extent_same, and I don't know of anything using clone_range.

(Well, xfs_io does...)

Quoted 3 lines

The range checking in clone_verify_area() appears incomplete. Someone could provide len=UINT64_MAX and all the checks would still pass even though 'pos+len' would overflow.

Yeah...

Quoted 3 lines

Should the ioctl be interruptible? Right now it always goes through *all* the 'struct file_dedupe_range_info's you passed in --- potentially up to 65535 of them.

There probably ought to be explicit signal checks, or we could just get rid of the vectorization entirely. :)

Quoted 3 lines

Why 'info->bytes_deduped += deduped' rather than 'info->bytes_deduped = deduped'? 'bytes_deduped' is per file descriptor, not for the operation as a whole.

Right, because bytes_deduped is a part of file_dedup_range_info, not file_dedupe_range.

(Note the bytes_deduped = 0 earlier in the function.)

Quoted 4 lines

What permissions do you need on the destination file descriptors? The man page implies they must be open for writing and not appending. The implementation differs: it requires FMODE_WRITE only for non-admin users, and it doesn't check for O_APPEND at all.

I think the result of an earlier discussion was that src_fd must be readable, and dest_fd must be writable or appendable.

Quoted 4 lines

The man page also says you get EPERM if "dest_fd is immutable" and ETXTBSY if "one of the files is a swap file", which I don't see actually happening in the implementation; it seems those error codes perhaps exist at all for this ioctl but rather be left to open(..., O_WRONLY).

Those could be hoisted to the VFS (from the XFS implementation), I think.

Quoted 3 lines

If the filesystem doesn't support deduplication, or I pass in a strange file descriptor such as one for a named pipe, do I get EINVAL or EOPNOTSUPP? The man page isn't clear.

Should be EOPNOTSUPP if dest_fd isn't a regular file; EISDIR if either are directories; and EINVAL for any other kind of non-file fd. I suspect the clone* manpages don't make this too clear either.

Quoted 2 lines

Under what circumstances will 'bytes_deduped' differ from the count that was passed in?

btrfs/xfs will only compare the first 16MB. Not documented anywhere. :(

Quoted 2 lines

If short counts are allowed, what will be the 'status' be in that case:
FILE_DEDUP_RANGE_DIFFERS, FILE_DEDUPE_RANGE_SAME, or something else?

One of those two.

Quoted 1 line

Can data be deduped even if only a prefix of the data region matches?

No.

Quoted 2 lines

The man page doesn't mention FILE_DEDUPE_RANGE_SAME at all, instead calling it 0; it only mentions FILE_DEDUPE_RANGE_DIFFERS.

Oops, good catch. :(

Quoted 4 lines

The man page isn't clear about whether the ioctl stops early if an error occurs or always processes all the 'struct file_dedupe_range_info's you pass in. And if it were, hypothetically, to stop early, how is the user meant to know on which file it stopped?

I don't know if this should be the official behavior, but it stopped at whichever file_dedupe_range_info has both status and bytes_deduped set to zero.

Quoted 2 lines

The man page says "logical_offset" but in the struct it is called "dest_offset".

Oops.

Quoted 2 lines

There are some variables named "same" which don't really make sense now that the ioctl is called FIDEDUPERANGE instead of EXTENT_SAME.

Perhaps not.

I'll later take a look at how many of these issues apply to clone/clone_range.

--D

Quoted 6 lines

Eric
--
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-api" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Signature · 3 lines
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/