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>
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>
19 #include <linux/uaccess.h>
20 #include <asm/unistd.h>
23 * Performs necessary checks before doing a clone.
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.
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
)
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
;
37 loff_t size_in
, size_out
;
38 loff_t bs
= inode_out
->i_sb
->s_blocksize
;
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
))
45 /* Ensure offsets don't wrap. */
46 if (pos_in
+ count
< pos_in
|| pos_out
+ count
< pos_out
)
49 size_in
= i_size_read(inode_in
);
50 size_out
= i_size_read(inode_out
);
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
))
58 /* Ensure the infile range is within the infile. */
59 if (pos_in
>= size_in
)
61 count
= min(count
, size_in
- (uint64_t)pos_in
);
63 ret
= generic_write_check_limits(file_out
, pos_out
, &count
);
68 * If the user wanted us to link to the infile's EOF, round up to the
69 * next block boundary for this check.
71 * Otherwise, make sure the count is also block-aligned, having
72 * already confirmed the starting offsets' block alignment.
74 if (pos_in
+ count
== size_in
) {
75 bcount
= ALIGN(size_in
, bs
) - pos_in
;
77 if (!IS_ALIGNED(count
, bs
))
78 count
= ALIGN_DOWN(count
, bs
);
82 /* Don't allow overlapped cloning within the same file. */
83 if (inode_in
== inode_out
&&
84 pos_out
+ bcount
> pos_in
&&
85 pos_out
< pos_in
+ bcount
)
89 * We shortened the request but the caller can't deal with that, so
90 * bounce the request back to userspace.
92 if (*req_count
!= count
&& !(remap_flags
& REMAP_FILE_CAN_SHORTEN
))
99 static int remap_verify_area(struct file
*file
, loff_t pos
, loff_t len
,
102 struct inode
*inode
= file_inode(file
);
104 if (unlikely(pos
< 0 || len
< 0))
107 if (unlikely((loff_t
) (pos
+ len
) < 0))
110 if (unlikely(inode
->i_flctx
&& mandatory_lock(inode
))) {
111 loff_t end
= len
? pos
+ len
- 1 : OFFSET_MAX
;
114 retval
= locks_mandatory_area(inode
, file
, pos
, end
,
115 write
? F_WRLCK
: F_RDLCK
);
120 return security_file_permission(file
, write
? MAY_WRITE
: MAY_READ
);
124 * Ensure that we don't remap a partial EOF block in the middle of something
125 * else. Assume that the offsets have already been checked for block
128 * For clone we only link a partial EOF block above or at the destination file's
129 * EOF. For deduplication we accept a partial EOF block only if it ends at the
130 * destination file's EOF (can not link it into the middle of a file).
132 * Shorten the request if possible.
134 static int generic_remap_check_len(struct inode
*inode_in
,
135 struct inode
*inode_out
,
138 unsigned int remap_flags
)
140 u64 blkmask
= i_blocksize(inode_in
) - 1;
141 loff_t new_len
= *len
;
143 if ((*len
& blkmask
) == 0)
146 if (pos_out
+ *len
< i_size_read(inode_out
))
152 if (remap_flags
& REMAP_FILE_CAN_SHORTEN
) {
157 return (remap_flags
& REMAP_FILE_DEDUP
) ? -EBADE
: -EINVAL
;
160 /* Read a page's worth of file data into the page cache. */
161 static struct page
*vfs_dedupe_get_page(struct inode
*inode
, loff_t offset
)
165 page
= read_mapping_page(inode
->i_mapping
, offset
>> PAGE_SHIFT
, NULL
);
168 if (!PageUptodate(page
)) {
170 return ERR_PTR(-EIO
);
176 * Lock two pages, ensuring that we lock in offset order if the pages are from
179 static void vfs_lock_two_pages(struct page
*page1
, struct page
*page2
)
181 /* Always lock in order of increasing index. */
182 if (page1
->index
> page2
->index
)
190 /* Unlock two pages, being careful not to unlock the same page twice. */
191 static void vfs_unlock_two_pages(struct page
*page1
, struct page
*page2
)
199 * Compare extents of two files to see if they are the same.
200 * Caller must have locked both inodes to prevent write races.
202 static int vfs_dedupe_file_range_compare(struct inode
*src
, loff_t srcoff
,
203 struct inode
*dest
, loff_t destoff
,
204 loff_t len
, bool *is_same
)
210 struct page
*src_page
;
211 struct page
*dest_page
;
219 src_poff
= srcoff
& (PAGE_SIZE
- 1);
220 dest_poff
= destoff
& (PAGE_SIZE
- 1);
221 cmp_len
= min(PAGE_SIZE
- src_poff
,
222 PAGE_SIZE
- dest_poff
);
223 cmp_len
= min(cmp_len
, len
);
227 src_page
= vfs_dedupe_get_page(src
, srcoff
);
228 if (IS_ERR(src_page
)) {
229 error
= PTR_ERR(src_page
);
232 dest_page
= vfs_dedupe_get_page(dest
, destoff
);
233 if (IS_ERR(dest_page
)) {
234 error
= PTR_ERR(dest_page
);
239 vfs_lock_two_pages(src_page
, dest_page
);
242 * Now that we've locked both pages, make sure they're still
243 * mapped to the file data we're interested in. If not,
244 * someone is invalidating pages on us and we lose.
246 if (!PageUptodate(src_page
) || !PageUptodate(dest_page
) ||
247 src_page
->mapping
!= src
->i_mapping
||
248 dest_page
->mapping
!= dest
->i_mapping
) {
253 src_addr
= kmap_atomic(src_page
);
254 dest_addr
= kmap_atomic(dest_page
);
256 flush_dcache_page(src_page
);
257 flush_dcache_page(dest_page
);
259 if (memcmp(src_addr
+ src_poff
, dest_addr
+ dest_poff
, cmp_len
))
262 kunmap_atomic(dest_addr
);
263 kunmap_atomic(src_addr
);
265 vfs_unlock_two_pages(src_page
, dest_page
);
285 * Check that the two inodes are eligible for cloning, the ranges make
286 * sense, and then flush all dirty data. Caller must ensure that the
287 * inodes have been locked against any other modifications.
289 * If there's an error, then the usual negative error code is returned.
290 * Otherwise returns 0 with *len set to the request length.
292 int generic_remap_file_range_prep(struct file
*file_in
, loff_t pos_in
,
293 struct file
*file_out
, loff_t pos_out
,
294 loff_t
*len
, unsigned int remap_flags
)
296 struct inode
*inode_in
= file_inode(file_in
);
297 struct inode
*inode_out
= file_inode(file_out
);
298 bool same_inode
= (inode_in
== inode_out
);
301 /* Don't touch certain kinds of inodes */
302 if (IS_IMMUTABLE(inode_out
))
305 if (IS_SWAPFILE(inode_in
) || IS_SWAPFILE(inode_out
))
308 /* Don't reflink dirs, pipes, sockets... */
309 if (S_ISDIR(inode_in
->i_mode
) || S_ISDIR(inode_out
->i_mode
))
311 if (!S_ISREG(inode_in
->i_mode
) || !S_ISREG(inode_out
->i_mode
))
314 /* Zero length dedupe exits immediately; reflink goes to EOF. */
316 loff_t isize
= i_size_read(inode_in
);
318 if ((remap_flags
& REMAP_FILE_DEDUP
) || pos_in
== isize
)
322 *len
= isize
- pos_in
;
327 /* Check that we don't violate system file offset limits. */
328 ret
= generic_remap_checks(file_in
, pos_in
, file_out
, pos_out
, len
,
333 /* Wait for the completion of any pending IOs on both files */
334 inode_dio_wait(inode_in
);
336 inode_dio_wait(inode_out
);
338 ret
= filemap_write_and_wait_range(inode_in
->i_mapping
,
339 pos_in
, pos_in
+ *len
- 1);
343 ret
= filemap_write_and_wait_range(inode_out
->i_mapping
,
344 pos_out
, pos_out
+ *len
- 1);
349 * Check that the extents are the same.
351 if (remap_flags
& REMAP_FILE_DEDUP
) {
352 bool is_same
= false;
354 ret
= vfs_dedupe_file_range_compare(inode_in
, pos_in
,
355 inode_out
, pos_out
, *len
, &is_same
);
362 ret
= generic_remap_check_len(inode_in
, inode_out
, pos_out
, len
,
367 /* If can't alter the file contents, we're done. */
368 if (!(remap_flags
& REMAP_FILE_DEDUP
))
369 ret
= file_modified(file_out
);
373 EXPORT_SYMBOL(generic_remap_file_range_prep
);
375 loff_t
do_clone_file_range(struct file
*file_in
, loff_t pos_in
,
376 struct file
*file_out
, loff_t pos_out
,
377 loff_t len
, unsigned int remap_flags
)
381 WARN_ON_ONCE(remap_flags
& REMAP_FILE_DEDUP
);
384 * FICLONE/FICLONERANGE ioctls enforce that src and dest files are on
385 * the same mount. Practically, they only need to be on the same file
388 if (file_inode(file_in
)->i_sb
!= file_inode(file_out
)->i_sb
)
391 ret
= generic_file_rw_checks(file_in
, file_out
);
395 if (!file_in
->f_op
->remap_file_range
)
398 ret
= remap_verify_area(file_in
, pos_in
, len
, false);
402 ret
= remap_verify_area(file_out
, pos_out
, len
, true);
406 ret
= file_in
->f_op
->remap_file_range(file_in
, pos_in
,
407 file_out
, pos_out
, len
, remap_flags
);
411 fsnotify_access(file_in
);
412 fsnotify_modify(file_out
);
415 EXPORT_SYMBOL(do_clone_file_range
);
417 loff_t
vfs_clone_file_range(struct file
*file_in
, loff_t pos_in
,
418 struct file
*file_out
, loff_t pos_out
,
419 loff_t len
, unsigned int remap_flags
)
423 file_start_write(file_out
);
424 ret
= do_clone_file_range(file_in
, pos_in
, file_out
, pos_out
, len
,
426 file_end_write(file_out
);
430 EXPORT_SYMBOL(vfs_clone_file_range
);
432 /* Check whether we are allowed to dedupe the destination file */
433 static bool allow_file_dedupe(struct file
*file
)
435 if (capable(CAP_SYS_ADMIN
))
437 if (file
->f_mode
& FMODE_WRITE
)
439 if (uid_eq(current_fsuid(), file_inode(file
)->i_uid
))
441 if (!inode_permission(file_inode(file
), MAY_WRITE
))
446 loff_t
vfs_dedupe_file_range_one(struct file
*src_file
, loff_t src_pos
,
447 struct file
*dst_file
, loff_t dst_pos
,
448 loff_t len
, unsigned int remap_flags
)
452 WARN_ON_ONCE(remap_flags
& ~(REMAP_FILE_DEDUP
|
453 REMAP_FILE_CAN_SHORTEN
));
455 ret
= mnt_want_write_file(dst_file
);
460 * This is redundant if called from vfs_dedupe_file_range(), but other
461 * callers need it and it's not performance sesitive...
463 ret
= remap_verify_area(src_file
, src_pos
, len
, false);
467 ret
= remap_verify_area(dst_file
, dst_pos
, len
, true);
472 if (!allow_file_dedupe(dst_file
))
476 if (src_file
->f_path
.mnt
!= dst_file
->f_path
.mnt
)
480 if (S_ISDIR(file_inode(dst_file
)->i_mode
))
484 if (!dst_file
->f_op
->remap_file_range
)
492 ret
= dst_file
->f_op
->remap_file_range(src_file
, src_pos
, dst_file
,
493 dst_pos
, len
, remap_flags
| REMAP_FILE_DEDUP
);
495 mnt_drop_write_file(dst_file
);
499 EXPORT_SYMBOL(vfs_dedupe_file_range_one
);
501 int vfs_dedupe_file_range(struct file
*file
, struct file_dedupe_range
*same
)
503 struct file_dedupe_range_info
*info
;
504 struct inode
*src
= file_inode(file
);
509 u16 count
= same
->dest_count
;
512 if (!(file
->f_mode
& FMODE_READ
))
515 if (same
->reserved1
|| same
->reserved2
)
518 off
= same
->src_offset
;
519 len
= same
->src_length
;
521 if (S_ISDIR(src
->i_mode
))
524 if (!S_ISREG(src
->i_mode
))
527 if (!file
->f_op
->remap_file_range
)
530 ret
= remap_verify_area(file
, off
, len
, false);
535 if (off
+ len
> i_size_read(src
))
538 /* Arbitrary 1G limit on a single dedupe request, can be raised. */
539 len
= min_t(u64
, len
, 1 << 30);
541 /* pre-format output fields to sane values */
542 for (i
= 0; i
< count
; i
++) {
543 same
->info
[i
].bytes_deduped
= 0ULL;
544 same
->info
[i
].status
= FILE_DEDUPE_RANGE_SAME
;
547 for (i
= 0, info
= same
->info
; i
< count
; i
++, info
++) {
548 struct fd dst_fd
= fdget(info
->dest_fd
);
549 struct file
*dst_file
= dst_fd
.file
;
552 info
->status
= -EBADF
;
556 if (info
->reserved
) {
557 info
->status
= -EINVAL
;
561 deduped
= vfs_dedupe_file_range_one(file
, off
, dst_file
,
562 info
->dest_offset
, len
,
563 REMAP_FILE_CAN_SHORTEN
);
564 if (deduped
== -EBADE
)
565 info
->status
= FILE_DEDUPE_RANGE_DIFFERS
;
566 else if (deduped
< 0)
567 info
->status
= deduped
;
569 info
->bytes_deduped
= len
;
574 if (fatal_signal_pending(current
))
579 EXPORT_SYMBOL(vfs_dedupe_file_range
);