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