aboutsummaryrefslogtreecommitdiffstats
path: root/fs/cifs/ioctl.c
diff options
context:
space:
mode:
authorSteve French <smfrench@gmail.com>2013-11-14 01:05:36 -0500
committerSteve French <smfrench@gmail.com>2013-11-14 01:05:36 -0500
commit41c1358e9181ab1ebd773905b3fa8039b61aa0e9 (patch)
tree144d08d6758ac33212c200421a973120910c6fc8 /fs/cifs/ioctl.c
parent2c957ddf30897787e39462ac56cdc4bf21eb0465 (diff)
CIFS: SMB2/SMB3 Copy offload support (refcopy) phase 1
This first patch adds the ability for us to do a server side copy (ie fast copy offloaded to the server to perform, aka refcopy) "cp --reflink" of one file to another located on the same server. This is much faster than traditional copy (which requires reading and writing over the network and extra memcpys). This first version is not going to be copy files larger than about 1MB (to Samba) until I add support for multiple chunks and for autoconfiguring the chunksize. It includes: 1) processing of the ioctl 2) marshalling and sending the SMB2/SMB3 fsctl over the network 3) simple parsing of the response It does not include yet (these will be in followon patches to come soon): 1) support for multiple chunks 2) support for autoconfiguring and remembering the chunksize 3) Support for the older style copychunk which Samba 4.1 server supports (because this requires write permission on the target file, which cp does not give you, apparently per-posix). This may require a distinct tool (other than cp) and other ioctl to implement. Reviewed-by: Pavel Shilovsky <piastry@etersoft.ru> Signed-off-by: Steve French <smfrench@gmail.com>
Diffstat (limited to 'fs/cifs/ioctl.c')
-rw-r--r--fs/cifs/ioctl.c111
1 files changed, 111 insertions, 0 deletions
diff --git a/fs/cifs/ioctl.c b/fs/cifs/ioctl.c
index ba54bf6ab116..409b45eefe70 100644
--- a/fs/cifs/ioctl.c
+++ b/fs/cifs/ioctl.c
@@ -22,12 +22,120 @@
22 */ 22 */
23 23
24#include <linux/fs.h> 24#include <linux/fs.h>
25#include <linux/file.h>
26#include <linux/mount.h>
27#include <linux/mm.h>
28#include <linux/pagemap.h>
29#include <linux/btrfs.h>
25#include "cifspdu.h" 30#include "cifspdu.h"
26#include "cifsglob.h" 31#include "cifsglob.h"
27#include "cifsproto.h" 32#include "cifsproto.h"
28#include "cifs_debug.h" 33#include "cifs_debug.h"
29#include "cifsfs.h" 34#include "cifsfs.h"
30 35
36static long cifs_ioctl_clone(unsigned int xid, struct file *dst_file,
37 unsigned long srcfd, u64 off, u64 len, u64 destoff)
38{
39 int rc;
40 struct cifsFileInfo *smb_file_target = dst_file->private_data;
41 struct inode *target_inode = file_inode(dst_file);
42 struct cifs_tcon *target_tcon;
43 struct fd src_file;
44 struct cifsFileInfo *smb_file_src;
45 struct inode *src_inode;
46 struct cifs_tcon *src_tcon;
47
48 cifs_dbg(FYI, "ioctl clone range\n");
49 /* the destination must be opened for writing */
50 if (!(dst_file->f_mode & FMODE_WRITE)) {
51 cifs_dbg(FYI, "file target not open for write\n");
52 return -EINVAL;
53 }
54
55 /* check if target volume is readonly and take reference */
56 rc = mnt_want_write_file(dst_file);
57 if (rc) {
58 cifs_dbg(FYI, "mnt_want_write failed with rc %d\n", rc);
59 return rc;
60 }
61
62 src_file = fdget(srcfd);
63 if (!src_file.file) {
64 rc = -EBADF;
65 goto out_drop_write;
66 }
67
68 if ((!src_file.file->private_data) || (!dst_file->private_data)) {
69 rc = -EBADF;
70 cifs_dbg(VFS, "missing cifsFileInfo on copy range src file\n");
71 goto out_fput;
72 }
73
74 rc = -EXDEV;
75 smb_file_target = dst_file->private_data;
76 smb_file_src = src_file.file->private_data;
77 src_tcon = tlink_tcon(smb_file_src->tlink);
78 target_tcon = tlink_tcon(smb_file_target->tlink);
79
80 /* check if source and target are on same tree connection */
81 if (src_tcon != target_tcon) {
82 cifs_dbg(VFS, "file copy src and target on different volume\n");
83 goto out_fput;
84 }
85
86 src_inode = src_file.file->f_dentry->d_inode;
87
88 /*
89 * Note: cifs case is easier than btrfs since server responsible for
90 * checks for proper open modes and file type and if it wants
91 * server could even support copy of range where source = target
92 */
93
94 /* so we do not deadlock racing two ioctls on same files */
95 if (target_inode < src_inode) {
96 mutex_lock_nested(&target_inode->i_mutex, I_MUTEX_PARENT);
97 mutex_lock_nested(&src_inode->i_mutex, I_MUTEX_CHILD);
98 } else {
99 mutex_lock_nested(&src_inode->i_mutex, I_MUTEX_PARENT);
100 mutex_lock_nested(&target_inode->i_mutex, I_MUTEX_CHILD);
101 }
102
103 /* determine range to clone */
104 rc = -EINVAL;
105 if (off + len > src_inode->i_size || off + len < off)
106 goto out_unlock;
107 if (len == 0)
108 len = src_inode->i_size - off;
109
110 cifs_dbg(FYI, "about to flush pages\n");
111 /* should we flush first and last page first */
112 truncate_inode_pages_range(&target_inode->i_data, destoff,
113 PAGE_CACHE_ALIGN(destoff + len)-1);
114
115 if (target_tcon->ses->server->ops->clone_range)
116 rc = target_tcon->ses->server->ops->clone_range(xid,
117 smb_file_src, smb_file_target, off, len, destoff);
118
119 /* force revalidate of size and timestamps of target file now
120 that target is updated on the server */
121 CIFS_I(target_inode)->time = 0;
122out_unlock:
123 /* although unlocking in the reverse order from locking is not
124 strictly necessary here it is a little cleaner to be consistent */
125 if (target_inode < src_inode) {
126 mutex_unlock(&src_inode->i_mutex);
127 mutex_unlock(&target_inode->i_mutex);
128 } else {
129 mutex_unlock(&target_inode->i_mutex);
130 mutex_unlock(&src_inode->i_mutex);
131 }
132out_fput:
133 fdput(src_file);
134out_drop_write:
135 mnt_drop_write_file(dst_file);
136 return rc;
137}
138
31long cifs_ioctl(struct file *filep, unsigned int command, unsigned long arg) 139long cifs_ioctl(struct file *filep, unsigned int command, unsigned long arg)
32{ 140{
33 struct inode *inode = file_inode(filep); 141 struct inode *inode = file_inode(filep);
@@ -105,6 +213,9 @@ long cifs_ioctl(struct file *filep, unsigned int command, unsigned long arg)
105 cifs_dbg(FYI, "set compress flag rc %d\n", rc); 213 cifs_dbg(FYI, "set compress flag rc %d\n", rc);
106 } 214 }
107 break; 215 break;
216 case BTRFS_IOC_CLONE:
217 rc = cifs_ioctl_clone(xid, filep, arg, 0, 0, 0);
218 break;
108 default: 219 default:
109 cifs_dbg(FYI, "unsupported ioctl\n"); 220 cifs_dbg(FYI, "unsupported ioctl\n");
110 break; 221 break;