]> git.proxmox.com Git - mirror_zfs.git/blob - module/os/linux/zfs/zpl_file_range.c
3065d54fa9da5f290e289da45d0df15b4a62b42e
[mirror_zfs.git] / module / os / linux / zfs / zpl_file_range.c
1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or https://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21 /*
22 * Copyright (c) 2023, Klara Inc.
23 */
24
25 #ifdef CONFIG_COMPAT
26 #include <linux/compat.h>
27 #endif
28 #include <linux/fs.h>
29 #include <sys/file.h>
30 #include <sys/zfs_znode.h>
31 #include <sys/zfs_vnops.h>
32 #include <sys/zfeature.h>
33
34 /*
35 * Clone part of a file via block cloning.
36 *
37 * Note that we are not required to update file offsets; the kernel will take
38 * care of that depending on how it was called.
39 */
40 static ssize_t
41 zpl_clone_file_range_impl(struct file *src_file, loff_t src_off,
42 struct file *dst_file, loff_t dst_off, size_t len)
43 {
44 struct inode *src_i = file_inode(src_file);
45 struct inode *dst_i = file_inode(dst_file);
46 uint64_t src_off_o = (uint64_t)src_off;
47 uint64_t dst_off_o = (uint64_t)dst_off;
48 uint64_t len_o = (uint64_t)len;
49 cred_t *cr = CRED();
50 fstrans_cookie_t cookie;
51 int err;
52
53 if (!zfs_bclone_enabled)
54 return (-EOPNOTSUPP);
55
56 if (!spa_feature_is_enabled(
57 dmu_objset_spa(ITOZSB(dst_i)->z_os), SPA_FEATURE_BLOCK_CLONING))
58 return (-EOPNOTSUPP);
59
60 if (src_i != dst_i)
61 spl_inode_lock_shared(src_i);
62 spl_inode_lock(dst_i);
63
64 crhold(cr);
65 cookie = spl_fstrans_mark();
66
67 err = -zfs_clone_range(ITOZ(src_i), &src_off_o, ITOZ(dst_i),
68 &dst_off_o, &len_o, cr);
69
70 spl_fstrans_unmark(cookie);
71 crfree(cr);
72
73 spl_inode_unlock(dst_i);
74 if (src_i != dst_i)
75 spl_inode_unlock_shared(src_i);
76
77 if (err < 0)
78 return (err);
79
80 return ((ssize_t)len_o);
81 }
82
83 #if defined(HAVE_VFS_COPY_FILE_RANGE) || \
84 defined(HAVE_VFS_FILE_OPERATIONS_EXTEND)
85 /*
86 * Entry point for copy_file_range(). Copy len bytes from src_off in src_file
87 * to dst_off in dst_file. We are permitted to do this however we like, so we
88 * try to just clone the blocks, and if we can't support it, fall back to the
89 * kernel's generic byte copy function.
90 */
91 ssize_t
92 zpl_copy_file_range(struct file *src_file, loff_t src_off,
93 struct file *dst_file, loff_t dst_off, size_t len, unsigned int flags)
94 {
95 ssize_t ret;
96
97 /* Flags is reserved for future extensions and must be zero. */
98 if (flags != 0)
99 return (-EINVAL);
100
101 /* Try to do it via zfs_clone_range() and allow shortening. */
102 ret = zpl_clone_file_range_impl(src_file, src_off,
103 dst_file, dst_off, len);
104
105 #ifdef HAVE_VFS_GENERIC_COPY_FILE_RANGE
106 /*
107 * Since Linux 5.3 the filesystem driver is responsible for executing
108 * an appropriate fallback, and a generic fallback function is provided.
109 */
110 if (ret == -EOPNOTSUPP || ret == -EINVAL || ret == -EXDEV ||
111 ret == -EAGAIN)
112 ret = generic_copy_file_range(src_file, src_off, dst_file,
113 dst_off, len, flags);
114 #else
115 /*
116 * Before Linux 5.3 the filesystem has to return -EOPNOTSUPP to signal
117 * to the kernel that it should fallback to a content copy.
118 */
119 if (ret == -EINVAL || ret == -EXDEV || ret == -EAGAIN)
120 ret = -EOPNOTSUPP;
121 #endif /* HAVE_VFS_GENERIC_COPY_FILE_RANGE */
122
123 return (ret);
124 }
125 #endif /* HAVE_VFS_COPY_FILE_RANGE || HAVE_VFS_FILE_OPERATIONS_EXTEND */
126
127 #ifdef HAVE_VFS_REMAP_FILE_RANGE
128 /*
129 * Entry point for FICLONE/FICLONERANGE/FIDEDUPERANGE.
130 *
131 * FICLONE and FICLONERANGE are basically the same as copy_file_range(), except
132 * that they must clone - they cannot fall back to copying. FICLONE is exactly
133 * FICLONERANGE, for the entire file. We don't need to try to tell them apart;
134 * the kernel will sort that out for us.
135 *
136 * FIDEDUPERANGE is for turning a non-clone into a clone, that is, compare the
137 * range in both files and if they're the same, arrange for them to be backed
138 * by the same storage.
139 *
140 * REMAP_FILE_CAN_SHORTEN lets us know we can clone less than the given range
141 * if we want. It's designed for filesystems that may need to shorten the
142 * length for alignment, EOF, or any other requirement. ZFS may shorten the
143 * request when there is outstanding dirty data which hasn't been written.
144 */
145 loff_t
146 zpl_remap_file_range(struct file *src_file, loff_t src_off,
147 struct file *dst_file, loff_t dst_off, loff_t len, unsigned int flags)
148 {
149 if (flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_CAN_SHORTEN))
150 return (-EINVAL);
151
152 /* No support for dedup yet */
153 if (flags & REMAP_FILE_DEDUP)
154 return (-EOPNOTSUPP);
155
156 /* Zero length means to clone everything to the end of the file */
157 if (len == 0)
158 len = i_size_read(file_inode(src_file)) - src_off;
159
160 ssize_t ret = zpl_clone_file_range_impl(src_file, src_off,
161 dst_file, dst_off, len);
162
163 if (!(flags & REMAP_FILE_CAN_SHORTEN) && ret >= 0 && ret != len)
164 ret = -EINVAL;
165
166 return (ret);
167 }
168 #endif /* HAVE_VFS_REMAP_FILE_RANGE */
169
170 #if defined(HAVE_VFS_CLONE_FILE_RANGE) || \
171 defined(HAVE_VFS_FILE_OPERATIONS_EXTEND)
172 /*
173 * Entry point for FICLONE and FICLONERANGE, before Linux 4.20.
174 */
175 int
176 zpl_clone_file_range(struct file *src_file, loff_t src_off,
177 struct file *dst_file, loff_t dst_off, uint64_t len)
178 {
179 /* Zero length means to clone everything to the end of the file */
180 if (len == 0)
181 len = i_size_read(file_inode(src_file)) - src_off;
182
183 /* The entire length must be cloned or this is an error. */
184 ssize_t ret = zpl_clone_file_range_impl(src_file, src_off,
185 dst_file, dst_off, len);
186
187 if (ret >= 0 && ret != len)
188 ret = -EINVAL;
189
190 return (ret);
191 }
192 #endif /* HAVE_VFS_CLONE_FILE_RANGE || HAVE_VFS_FILE_OPERATIONS_EXTEND */
193
194 #ifdef HAVE_VFS_DEDUPE_FILE_RANGE
195 /*
196 * Entry point for FIDEDUPERANGE, before Linux 4.20.
197 */
198 int
199 zpl_dedupe_file_range(struct file *src_file, loff_t src_off,
200 struct file *dst_file, loff_t dst_off, uint64_t len)
201 {
202 /* No support for dedup yet */
203 return (-EOPNOTSUPP);
204 }
205 #endif /* HAVE_VFS_DEDUPE_FILE_RANGE */
206
207 /* Entry point for FICLONE, before Linux 4.5. */
208 long
209 zpl_ioctl_ficlone(struct file *dst_file, void *arg)
210 {
211 unsigned long sfd = (unsigned long)arg;
212
213 struct file *src_file = fget(sfd);
214 if (src_file == NULL)
215 return (-EBADF);
216
217 if (dst_file->f_op != src_file->f_op) {
218 fput(src_file);
219 return (-EXDEV);
220 }
221
222 size_t len = i_size_read(file_inode(src_file));
223
224 ssize_t ret = zpl_clone_file_range_impl(src_file, 0, dst_file, 0, len);
225
226 fput(src_file);
227
228 if (ret < 0) {
229 if (ret == -EOPNOTSUPP)
230 return (-ENOTTY);
231 return (ret);
232 }
233
234 if (ret != len)
235 return (-EINVAL);
236
237 return (0);
238 }
239
240 /* Entry point for FICLONERANGE, before Linux 4.5. */
241 long
242 zpl_ioctl_ficlonerange(struct file *dst_file, void __user *arg)
243 {
244 zfs_ioc_compat_file_clone_range_t fcr;
245
246 if (copy_from_user(&fcr, arg, sizeof (fcr)))
247 return (-EFAULT);
248
249 struct file *src_file = fget(fcr.fcr_src_fd);
250 if (src_file == NULL)
251 return (-EBADF);
252
253 if (dst_file->f_op != src_file->f_op) {
254 fput(src_file);
255 return (-EXDEV);
256 }
257
258 size_t len = fcr.fcr_src_length;
259 if (len == 0)
260 len = i_size_read(file_inode(src_file)) - fcr.fcr_src_offset;
261
262 ssize_t ret = zpl_clone_file_range_impl(src_file, fcr.fcr_src_offset,
263 dst_file, fcr.fcr_dest_offset, len);
264
265 fput(src_file);
266
267 if (ret < 0) {
268 if (ret == -EOPNOTSUPP)
269 return (-ENOTTY);
270 return (ret);
271 }
272
273 if (ret != len)
274 return (-EINVAL);
275
276 return (0);
277 }
278
279 /* Entry point for FIDEDUPERANGE, before Linux 4.5. */
280 long
281 zpl_ioctl_fideduperange(struct file *filp, void *arg)
282 {
283 (void) arg;
284
285 /* No support for dedup yet */
286 return (-ENOTTY);
287 }