1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/slab.h>
3 #include <linux/stat.h>
4 #include <linux/sched/xacct.h>
5 #include <linux/fcntl.h>
6 #include <linux/file.h>
7 #include <linux/uio.h>
8 #include <linux/fsnotify.h>
9 #include <linux/security.h>
10 #include <linux/export.h>
11 #include <linux/syscalls.h>
12 #include <linux/pagemap.h>
13 #include <linux/splice.h>
14 #include <linux/compat.h>
15 #include <linux/mount.h>
16 #include <linux/fs.h>
17 #include "internal.h"
18
19 #include <linux/uaccess.h>
20 #include <asm/unistd.h>
21
22 /*
23 * Performs necessary checks before doing a clone.
24 *
25 * Can adjust amount of bytes to clone via @req_count argument.
26 * Returns appropriate error code that caller should return or
27 * zero in case the clone should be allowed.
28 */
generic_remap_checks(struct file * file_in,loff_t pos_in,struct file * file_out,loff_t pos_out,loff_t * req_count,unsigned int remap_flags)29 static int generic_remap_checks(struct file *file_in, loff_t pos_in,
30 struct file *file_out, loff_t pos_out,
31 loff_t *req_count, unsigned int remap_flags)
32 {
33 struct inode *inode_in = file_in->f_mapping->host;
34 struct inode *inode_out = file_out->f_mapping->host;
35 uint64_t count = *req_count;
36 uint64_t bcount;
37 loff_t size_in, size_out;
38 loff_t bs = inode_out->i_sb->s_blocksize;
39 int ret;
40
41 /* The start of both ranges must be aligned to an fs block. */
42 if (!IS_ALIGNED(pos_in, bs) || !IS_ALIGNED(pos_out, bs))
43 return -EINVAL;
44
45 /* Ensure offsets don't wrap. */
46 if (pos_in + count < pos_in || pos_out + count < pos_out)
47 return -EINVAL;
48
49 size_in = i_size_read(inode_in);
50 size_out = i_size_read(inode_out);
51
52 /* Dedupe requires both ranges to be within EOF. */
53 if ((remap_flags & REMAP_FILE_DEDUP) &&
54 (pos_in >= size_in || pos_in + count > size_in ||
55 pos_out >= size_out || pos_out + count > size_out))
56 return -EINVAL;
57
58 /* Ensure the infile range is within the infile. */
59 if (pos_in >= size_in)
60 return -EINVAL;
61 count = min(count, size_in - (uint64_t)pos_in);
62
63 ret = generic_write_check_limits(file_out, pos_out, &count);
64 if (ret)
65 return ret;
66
67 /*
68 * If the user wanted us to link to the infile's EOF, round up to the
69 * next block boundary for this check.
70 *
71 * Otherwise, make sure the count is also block-aligned, having
72 * already confirmed the starting offsets' block alignment.
73 */
74 if (pos_in + count == size_in &&
75 (!(remap_flags & REMAP_FILE_DEDUP) || pos_out + count == size_out)) {
76 bcount = ALIGN(size_in, bs) - pos_in;
77 } else {
78 if (!IS_ALIGNED(count, bs))
79 count = ALIGN_DOWN(count, bs);
80 bcount = count;
81 }
82
83 /* Don't allow overlapped cloning within the same file. */
84 if (inode_in == inode_out &&
85 pos_out + bcount > pos_in &&
86 pos_out < pos_in + bcount)
87 return -EINVAL;
88
89 /*
90 * We shortened the request but the caller can't deal with that, so
91 * bounce the request back to userspace.
92 */
93 if (*req_count != count && !(remap_flags & REMAP_FILE_CAN_SHORTEN))
94 return -EINVAL;
95
96 *req_count = count;
97 return 0;
98 }
99
remap_verify_area(struct file * file,loff_t pos,loff_t len,bool write)100 static int remap_verify_area(struct file *file, loff_t pos, loff_t len,
101 bool write)
102 {
103 if (unlikely(pos < 0 || len < 0))
104 return -EINVAL;
105
106 if (unlikely((loff_t) (pos + len) < 0))
107 return -EINVAL;
108
109 return security_file_permission(file, write ? MAY_WRITE : MAY_READ);
110 }
111
112 /*
113 * Ensure that we don't remap a partial EOF block in the middle of something
114 * else. Assume that the offsets have already been checked for block
115 * alignment.
116 *
117 * For clone we only link a partial EOF block above or at the destination file's
118 * EOF. For deduplication we accept a partial EOF block only if it ends at the
119 * destination file's EOF (can not link it into the middle of a file).
120 *
121 * Shorten the request if possible.
122 */
generic_remap_check_len(struct inode * inode_in,struct inode * inode_out,loff_t pos_out,loff_t * len,unsigned int remap_flags)123 static int generic_remap_check_len(struct inode *inode_in,
124 struct inode *inode_out,
125 loff_t pos_out,
126 loff_t *len,
127 unsigned int remap_flags)
128 {
129 u64 blkmask = i_blocksize(inode_in) - 1;
130 loff_t new_len = *len;
131
132 if ((*len & blkmask) == 0)
133 return 0;
134
135 if (pos_out + *len < i_size_read(inode_out))
136 new_len &= ~blkmask;
137
138 if (new_len == *len)
139 return 0;
140
141 if (remap_flags & REMAP_FILE_CAN_SHORTEN) {
142 *len = new_len;
143 return 0;
144 }
145
146 return (remap_flags & REMAP_FILE_DEDUP) ? -EBADE : -EINVAL;
147 }
148
149 /* Read a page's worth of file data into the page cache. */
vfs_dedupe_get_page(struct inode * inode,loff_t offset)150 static struct page *vfs_dedupe_get_page(struct inode *inode, loff_t offset)
151 {
152 struct page *page;
153
154 page = read_mapping_page(inode->i_mapping, offset >> PAGE_SHIFT, NULL);
155 if (IS_ERR(page))
156 return page;
157 if (!PageUptodate(page)) {
158 put_page(page);
159 return ERR_PTR(-EIO);
160 }
161 return page;
162 }
163
164 /*
165 * Lock two pages, ensuring that we lock in offset order if the pages are from
166 * the same file.
167 */
vfs_lock_two_pages(struct page * page1,struct page * page2)168 static void vfs_lock_two_pages(struct page *page1, struct page *page2)
169 {
170 /* Always lock in order of increasing index. */
171 if (page1->index > page2->index)
172 swap(page1, page2);
173
174 lock_page(page1);
175 if (page1 != page2)
176 lock_page(page2);
177 }
178
179 /* Unlock two pages, being careful not to unlock the same page twice. */
vfs_unlock_two_pages(struct page * page1,struct page * page2)180 static void vfs_unlock_two_pages(struct page *page1, struct page *page2)
181 {
182 unlock_page(page1);
183 if (page1 != page2)
184 unlock_page(page2);
185 }
186
187 /*
188 * Compare extents of two files to see if they are the same.
189 * Caller must have locked both inodes to prevent write races.
190 */
vfs_dedupe_file_range_compare(struct inode * src,loff_t srcoff,struct inode * dest,loff_t destoff,loff_t len,bool * is_same)191 static int vfs_dedupe_file_range_compare(struct inode *src, loff_t srcoff,
192 struct inode *dest, loff_t destoff,
193 loff_t len, bool *is_same)
194 {
195 loff_t src_poff;
196 loff_t dest_poff;
197 void *src_addr;
198 void *dest_addr;
199 struct page *src_page;
200 struct page *dest_page;
201 loff_t cmp_len;
202 bool same;
203 int error;
204
205 error = -EINVAL;
206 same = true;
207 while (len) {
208 src_poff = srcoff & (PAGE_SIZE - 1);
209 dest_poff = destoff & (PAGE_SIZE - 1);
210 cmp_len = min(PAGE_SIZE - src_poff,
211 PAGE_SIZE - dest_poff);
212 cmp_len = min(cmp_len, len);
213 if (cmp_len <= 0)
214 goto out_error;
215
216 src_page = vfs_dedupe_get_page(src, srcoff);
217 if (IS_ERR(src_page)) {
218 error = PTR_ERR(src_page);
219 goto out_error;
220 }
221 dest_page = vfs_dedupe_get_page(dest, destoff);
222 if (IS_ERR(dest_page)) {
223 error = PTR_ERR(dest_page);
224 put_page(src_page);
225 goto out_error;
226 }
227
228 vfs_lock_two_pages(src_page, dest_page);
229
230 /*
231 * Now that we've locked both pages, make sure they're still
232 * mapped to the file data we're interested in. If not,
233 * someone is invalidating pages on us and we lose.
234 */
235 if (!PageUptodate(src_page) || !PageUptodate(dest_page) ||
236 src_page->mapping != src->i_mapping ||
237 dest_page->mapping != dest->i_mapping) {
238 same = false;
239 goto unlock;
240 }
241
242 src_addr = kmap_atomic(src_page);
243 dest_addr = kmap_atomic(dest_page);
244
245 flush_dcache_page(src_page);
246 flush_dcache_page(dest_page);
247
248 if (memcmp(src_addr + src_poff, dest_addr + dest_poff, cmp_len))
249 same = false;
250
251 kunmap_atomic(dest_addr);
252 kunmap_atomic(src_addr);
253 unlock:
254 vfs_unlock_two_pages(src_page, dest_page);
255 put_page(dest_page);
256 put_page(src_page);
257
258 if (!same)
259 break;
260
261 srcoff += cmp_len;
262 destoff += cmp_len;
263 len -= cmp_len;
264 }
265
266 *is_same = same;
267 return 0;
268
269 out_error:
270 return error;
271 }
272
273 /*
274 * Check that the two inodes are eligible for cloning, the ranges make
275 * sense, and then flush all dirty data. Caller must ensure that the
276 * inodes have been locked against any other modifications.
277 *
278 * If there's an error, then the usual negative error code is returned.
279 * Otherwise returns 0 with *len set to the request length.
280 */
generic_remap_file_range_prep(struct file * file_in,loff_t pos_in,struct file * file_out,loff_t pos_out,loff_t * len,unsigned int remap_flags)281 int generic_remap_file_range_prep(struct file *file_in, loff_t pos_in,
282 struct file *file_out, loff_t pos_out,
283 loff_t *len, unsigned int remap_flags)
284 {
285 struct inode *inode_in = file_inode(file_in);
286 struct inode *inode_out = file_inode(file_out);
287 bool same_inode = (inode_in == inode_out);
288 int ret;
289
290 /* Don't touch certain kinds of inodes */
291 if (IS_IMMUTABLE(inode_out))
292 return -EPERM;
293
294 if (IS_SWAPFILE(inode_in) || IS_SWAPFILE(inode_out))
295 return -ETXTBSY;
296
297 /* Don't reflink dirs, pipes, sockets... */
298 if (S_ISDIR(inode_in->i_mode) || S_ISDIR(inode_out->i_mode))
299 return -EISDIR;
300 if (!S_ISREG(inode_in->i_mode) || !S_ISREG(inode_out->i_mode))
301 return -EINVAL;
302
303 /* Zero length dedupe exits immediately; reflink goes to EOF. */
304 if (*len == 0) {
305 loff_t isize = i_size_read(inode_in);
306
307 if ((remap_flags & REMAP_FILE_DEDUP) || pos_in == isize)
308 return 0;
309 if (pos_in > isize)
310 return -EINVAL;
311 *len = isize - pos_in;
312 if (*len == 0)
313 return 0;
314 }
315
316 /* Check that we don't violate system file offset limits. */
317 ret = generic_remap_checks(file_in, pos_in, file_out, pos_out, len,
318 remap_flags);
319 if (ret)
320 return ret;
321
322 /* Wait for the completion of any pending IOs on both files */
323 inode_dio_wait(inode_in);
324 if (!same_inode)
325 inode_dio_wait(inode_out);
326
327 ret = filemap_write_and_wait_range(inode_in->i_mapping,
328 pos_in, pos_in + *len - 1);
329 if (ret)
330 return ret;
331
332 ret = filemap_write_and_wait_range(inode_out->i_mapping,
333 pos_out, pos_out + *len - 1);
334 if (ret)
335 return ret;
336
337 /*
338 * Check that the extents are the same.
339 */
340 if (remap_flags & REMAP_FILE_DEDUP) {
341 bool is_same = false;
342
343 ret = vfs_dedupe_file_range_compare(inode_in, pos_in,
344 inode_out, pos_out, *len, &is_same);
345 if (ret)
346 return ret;
347 if (!is_same)
348 return -EBADE;
349 }
350
351 ret = generic_remap_check_len(inode_in, inode_out, pos_out, len,
352 remap_flags);
353 if (ret)
354 return ret;
355
356 /* If can't alter the file contents, we're done. */
357 if (!(remap_flags & REMAP_FILE_DEDUP))
358 ret = file_modified(file_out);
359
360 return ret;
361 }
362 EXPORT_SYMBOL(generic_remap_file_range_prep);
363
do_clone_file_range(struct file * file_in,loff_t pos_in,struct file * file_out,loff_t pos_out,loff_t len,unsigned int remap_flags)364 loff_t do_clone_file_range(struct file *file_in, loff_t pos_in,
365 struct file *file_out, loff_t pos_out,
366 loff_t len, unsigned int remap_flags)
367 {
368 loff_t ret;
369
370 WARN_ON_ONCE(remap_flags & REMAP_FILE_DEDUP);
371
372 /*
373 * FICLONE/FICLONERANGE ioctls enforce that src and dest files are on
374 * the same mount. Practically, they only need to be on the same file
375 * system.
376 */
377 if (file_inode(file_in)->i_sb != file_inode(file_out)->i_sb)
378 return -EXDEV;
379
380 ret = generic_file_rw_checks(file_in, file_out);
381 if (ret < 0)
382 return ret;
383
384 if (!file_in->f_op->remap_file_range)
385 return -EOPNOTSUPP;
386
387 ret = remap_verify_area(file_in, pos_in, len, false);
388 if (ret)
389 return ret;
390
391 ret = remap_verify_area(file_out, pos_out, len, true);
392 if (ret)
393 return ret;
394
395 ret = file_in->f_op->remap_file_range(file_in, pos_in,
396 file_out, pos_out, len, remap_flags);
397 if (ret < 0)
398 return ret;
399
400 fsnotify_access(file_in);
401 fsnotify_modify(file_out);
402 return ret;
403 }
404 EXPORT_SYMBOL(do_clone_file_range);
405
vfs_clone_file_range(struct file * file_in,loff_t pos_in,struct file * file_out,loff_t pos_out,loff_t len,unsigned int remap_flags)406 loff_t vfs_clone_file_range(struct file *file_in, loff_t pos_in,
407 struct file *file_out, loff_t pos_out,
408 loff_t len, unsigned int remap_flags)
409 {
410 loff_t ret;
411
412 file_start_write(file_out);
413 ret = do_clone_file_range(file_in, pos_in, file_out, pos_out, len,
414 remap_flags);
415 file_end_write(file_out);
416
417 return ret;
418 }
419 EXPORT_SYMBOL(vfs_clone_file_range);
420
421 /* Check whether we are allowed to dedupe the destination file */
allow_file_dedupe(struct file * file)422 static bool allow_file_dedupe(struct file *file)
423 {
424 struct user_namespace *mnt_userns = file_mnt_user_ns(file);
425 struct inode *inode = file_inode(file);
426
427 if (capable(CAP_SYS_ADMIN))
428 return true;
429 if (file->f_mode & FMODE_WRITE)
430 return true;
431 if (uid_eq(current_fsuid(), i_uid_into_mnt(mnt_userns, inode)))
432 return true;
433 if (!inode_permission(mnt_userns, inode, MAY_WRITE))
434 return true;
435 return false;
436 }
437
vfs_dedupe_file_range_one(struct file * src_file,loff_t src_pos,struct file * dst_file,loff_t dst_pos,loff_t len,unsigned int remap_flags)438 loff_t vfs_dedupe_file_range_one(struct file *src_file, loff_t src_pos,
439 struct file *dst_file, loff_t dst_pos,
440 loff_t len, unsigned int remap_flags)
441 {
442 loff_t ret;
443
444 WARN_ON_ONCE(remap_flags & ~(REMAP_FILE_DEDUP |
445 REMAP_FILE_CAN_SHORTEN));
446
447 ret = mnt_want_write_file(dst_file);
448 if (ret)
449 return ret;
450
451 /*
452 * This is redundant if called from vfs_dedupe_file_range(), but other
453 * callers need it and it's not performance sesitive...
454 */
455 ret = remap_verify_area(src_file, src_pos, len, false);
456 if (ret)
457 goto out_drop_write;
458
459 ret = remap_verify_area(dst_file, dst_pos, len, true);
460 if (ret)
461 goto out_drop_write;
462
463 ret = -EPERM;
464 if (!allow_file_dedupe(dst_file))
465 goto out_drop_write;
466
467 ret = -EXDEV;
468 if (src_file->f_path.mnt != dst_file->f_path.mnt)
469 goto out_drop_write;
470
471 ret = -EISDIR;
472 if (S_ISDIR(file_inode(dst_file)->i_mode))
473 goto out_drop_write;
474
475 ret = -EINVAL;
476 if (!dst_file->f_op->remap_file_range)
477 goto out_drop_write;
478
479 if (len == 0) {
480 ret = 0;
481 goto out_drop_write;
482 }
483
484 ret = dst_file->f_op->remap_file_range(src_file, src_pos, dst_file,
485 dst_pos, len, remap_flags | REMAP_FILE_DEDUP);
486 out_drop_write:
487 mnt_drop_write_file(dst_file);
488
489 return ret;
490 }
491 EXPORT_SYMBOL(vfs_dedupe_file_range_one);
492
vfs_dedupe_file_range(struct file * file,struct file_dedupe_range * same)493 int vfs_dedupe_file_range(struct file *file, struct file_dedupe_range *same)
494 {
495 struct file_dedupe_range_info *info;
496 struct inode *src = file_inode(file);
497 u64 off;
498 u64 len;
499 int i;
500 int ret;
501 u16 count = same->dest_count;
502 loff_t deduped;
503
504 if (!(file->f_mode & FMODE_READ))
505 return -EINVAL;
506
507 if (same->reserved1 || same->reserved2)
508 return -EINVAL;
509
510 off = same->src_offset;
511 len = same->src_length;
512
513 if (S_ISDIR(src->i_mode))
514 return -EISDIR;
515
516 if (!S_ISREG(src->i_mode))
517 return -EINVAL;
518
519 if (!file->f_op->remap_file_range)
520 return -EOPNOTSUPP;
521
522 ret = remap_verify_area(file, off, len, false);
523 if (ret < 0)
524 return ret;
525 ret = 0;
526
527 if (off + len > i_size_read(src))
528 return -EINVAL;
529
530 /* Arbitrary 1G limit on a single dedupe request, can be raised. */
531 len = min_t(u64, len, 1 << 30);
532
533 /* pre-format output fields to sane values */
534 for (i = 0; i < count; i++) {
535 same->info[i].bytes_deduped = 0ULL;
536 same->info[i].status = FILE_DEDUPE_RANGE_SAME;
537 }
538
539 for (i = 0, info = same->info; i < count; i++, info++) {
540 struct fd dst_fd = fdget(info->dest_fd);
541 struct file *dst_file = dst_fd.file;
542
543 if (!dst_file) {
544 info->status = -EBADF;
545 goto next_loop;
546 }
547
548 if (info->reserved) {
549 info->status = -EINVAL;
550 goto next_fdput;
551 }
552
553 deduped = vfs_dedupe_file_range_one(file, off, dst_file,
554 info->dest_offset, len,
555 REMAP_FILE_CAN_SHORTEN);
556 if (deduped == -EBADE)
557 info->status = FILE_DEDUPE_RANGE_DIFFERS;
558 else if (deduped < 0)
559 info->status = deduped;
560 else
561 info->bytes_deduped = len;
562
563 next_fdput:
564 fdput(dst_fd);
565 next_loop:
566 if (fatal_signal_pending(current))
567 break;
568 }
569 return ret;
570 }
571 EXPORT_SYMBOL(vfs_dedupe_file_range);
572