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 (!(remap_flags & REMAP_FILE_DEDUP) || pos_out + count == size_out)) {
76 bcount = ALIGN(size_in, bs) - pos_in;
78 if (!IS_ALIGNED(count, bs))
79 count = ALIGN_DOWN(count, bs);
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)
90 * We shortened the request but the caller can't deal with that, so
91 * bounce the request back to userspace.
93 if (*req_count != count && !(remap_flags & REMAP_FILE_CAN_SHORTEN))
100 static int remap_verify_area(struct file *file, loff_t pos, loff_t len,
103 if (unlikely(pos < 0 || len < 0))
106 if (unlikely((loff_t) (pos + len) < 0))
109 return security_file_permission(file, write ? MAY_WRITE : MAY_READ);
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
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).
121 * Shorten the request if possible.
123 static int generic_remap_check_len(struct inode *inode_in,
124 struct inode *inode_out,
127 unsigned int remap_flags)
129 u64 blkmask = i_blocksize(inode_in) - 1;
130 loff_t new_len = *len;
132 if ((*len & blkmask) == 0)
135 if (pos_out + *len < i_size_read(inode_out))
141 if (remap_flags & REMAP_FILE_CAN_SHORTEN) {
146 return (remap_flags & REMAP_FILE_DEDUP) ? -EBADE : -EINVAL;
149 /* Read a page's worth of file data into the page cache. */
150 static struct folio *vfs_dedupe_get_folio(struct file *file, loff_t pos)
152 return read_mapping_folio(file->f_mapping, pos >> PAGE_SHIFT, file);
156 * Lock two folios, ensuring that we lock in offset order if the folios
157 * are from the same file.
159 static void vfs_lock_two_folios(struct folio *folio1, struct folio *folio2)
161 /* Always lock in order of increasing index. */
162 if (folio1->index > folio2->index)
163 swap(folio1, folio2);
166 if (folio1 != folio2)
170 /* Unlock two folios, being careful not to unlock the same folio twice. */
171 static void vfs_unlock_two_folios(struct folio *folio1, struct folio *folio2)
173 folio_unlock(folio1);
174 if (folio1 != folio2)
175 folio_unlock(folio2);
179 * Compare extents of two files to see if they are the same.
180 * Caller must have locked both inodes to prevent write races.
182 static int vfs_dedupe_file_range_compare(struct file *src, loff_t srcoff,
183 struct file *dest, loff_t dstoff,
184 loff_t len, bool *is_same)
190 struct folio *src_folio, *dst_folio;
191 void *src_addr, *dst_addr;
192 loff_t cmp_len = min(PAGE_SIZE - offset_in_page(srcoff),
193 PAGE_SIZE - offset_in_page(dstoff));
195 cmp_len = min(cmp_len, len);
199 src_folio = vfs_dedupe_get_folio(src, srcoff);
200 if (IS_ERR(src_folio)) {
201 error = PTR_ERR(src_folio);
204 dst_folio = vfs_dedupe_get_folio(dest, dstoff);
205 if (IS_ERR(dst_folio)) {
206 error = PTR_ERR(dst_folio);
207 folio_put(src_folio);
211 vfs_lock_two_folios(src_folio, dst_folio);
214 * Now that we've locked both folios, make sure they're still
215 * mapped to the file data we're interested in. If not,
216 * someone is invalidating pages on us and we lose.
218 if (!folio_test_uptodate(src_folio) || !folio_test_uptodate(dst_folio) ||
219 src_folio->mapping != src->f_mapping ||
220 dst_folio->mapping != dest->f_mapping) {
225 src_addr = kmap_local_folio(src_folio,
226 offset_in_folio(src_folio, srcoff));
227 dst_addr = kmap_local_folio(dst_folio,
228 offset_in_folio(dst_folio, dstoff));
230 flush_dcache_folio(src_folio);
231 flush_dcache_folio(dst_folio);
233 if (memcmp(src_addr, dst_addr, cmp_len))
236 kunmap_local(dst_addr);
237 kunmap_local(src_addr);
239 vfs_unlock_two_folios(src_folio, dst_folio);
240 folio_put(dst_folio);
241 folio_put(src_folio);
259 * Check that the two inodes are eligible for cloning, the ranges make
260 * sense, and then flush all dirty data. Caller must ensure that the
261 * inodes have been locked against any other modifications.
263 * If there's an error, then the usual negative error code is returned.
264 * Otherwise returns 0 with *len set to the request length.
266 int generic_remap_file_range_prep(struct file *file_in, loff_t pos_in,
267 struct file *file_out, loff_t pos_out,
268 loff_t *len, unsigned int remap_flags)
270 struct inode *inode_in = file_inode(file_in);
271 struct inode *inode_out = file_inode(file_out);
272 bool same_inode = (inode_in == inode_out);
275 /* Don't touch certain kinds of inodes */
276 if (IS_IMMUTABLE(inode_out))
279 if (IS_SWAPFILE(inode_in) || IS_SWAPFILE(inode_out))
282 /* Don't reflink dirs, pipes, sockets... */
283 if (S_ISDIR(inode_in->i_mode) || S_ISDIR(inode_out->i_mode))
285 if (!S_ISREG(inode_in->i_mode) || !S_ISREG(inode_out->i_mode))
288 /* Zero length dedupe exits immediately; reflink goes to EOF. */
290 loff_t isize = i_size_read(inode_in);
292 if ((remap_flags & REMAP_FILE_DEDUP) || pos_in == isize)
296 *len = isize - pos_in;
301 /* Check that we don't violate system file offset limits. */
302 ret = generic_remap_checks(file_in, pos_in, file_out, pos_out, len,
307 /* Wait for the completion of any pending IOs on both files */
308 inode_dio_wait(inode_in);
310 inode_dio_wait(inode_out);
312 ret = filemap_write_and_wait_range(inode_in->i_mapping,
313 pos_in, pos_in + *len - 1);
317 ret = filemap_write_and_wait_range(inode_out->i_mapping,
318 pos_out, pos_out + *len - 1);
323 * Check that the extents are the same.
325 if (remap_flags & REMAP_FILE_DEDUP) {
326 bool is_same = false;
328 ret = vfs_dedupe_file_range_compare(file_in, pos_in,
329 file_out, pos_out, *len, &is_same);
336 ret = generic_remap_check_len(inode_in, inode_out, pos_out, len,
341 /* If can't alter the file contents, we're done. */
342 if (!(remap_flags & REMAP_FILE_DEDUP))
343 ret = file_modified(file_out);
347 EXPORT_SYMBOL(generic_remap_file_range_prep);
349 loff_t do_clone_file_range(struct file *file_in, loff_t pos_in,
350 struct file *file_out, loff_t pos_out,
351 loff_t len, unsigned int remap_flags)
355 WARN_ON_ONCE(remap_flags & REMAP_FILE_DEDUP);
357 if (file_inode(file_in)->i_sb != file_inode(file_out)->i_sb)
360 ret = generic_file_rw_checks(file_in, file_out);
364 if (!file_in->f_op->remap_file_range)
367 ret = remap_verify_area(file_in, pos_in, len, false);
371 ret = remap_verify_area(file_out, pos_out, len, true);
375 ret = file_in->f_op->remap_file_range(file_in, pos_in,
376 file_out, pos_out, len, remap_flags);
380 fsnotify_access(file_in);
381 fsnotify_modify(file_out);
384 EXPORT_SYMBOL(do_clone_file_range);
386 loff_t vfs_clone_file_range(struct file *file_in, loff_t pos_in,
387 struct file *file_out, loff_t pos_out,
388 loff_t len, unsigned int remap_flags)
392 file_start_write(file_out);
393 ret = do_clone_file_range(file_in, pos_in, file_out, pos_out, len,
395 file_end_write(file_out);
399 EXPORT_SYMBOL(vfs_clone_file_range);
401 /* Check whether we are allowed to dedupe the destination file */
402 static bool allow_file_dedupe(struct file *file)
404 struct user_namespace *mnt_userns = file_mnt_user_ns(file);
405 struct inode *inode = file_inode(file);
407 if (capable(CAP_SYS_ADMIN))
409 if (file->f_mode & FMODE_WRITE)
411 if (uid_eq(current_fsuid(), i_uid_into_mnt(mnt_userns, inode)))
413 if (!inode_permission(mnt_userns, inode, MAY_WRITE))
418 loff_t vfs_dedupe_file_range_one(struct file *src_file, loff_t src_pos,
419 struct file *dst_file, loff_t dst_pos,
420 loff_t len, unsigned int remap_flags)
424 WARN_ON_ONCE(remap_flags & ~(REMAP_FILE_DEDUP |
425 REMAP_FILE_CAN_SHORTEN));
427 ret = mnt_want_write_file(dst_file);
432 * This is redundant if called from vfs_dedupe_file_range(), but other
433 * callers need it and it's not performance sesitive...
435 ret = remap_verify_area(src_file, src_pos, len, false);
439 ret = remap_verify_area(dst_file, dst_pos, len, true);
444 if (!allow_file_dedupe(dst_file))
448 if (file_inode(src_file)->i_sb != file_inode(dst_file)->i_sb)
452 if (S_ISDIR(file_inode(dst_file)->i_mode))
456 if (!dst_file->f_op->remap_file_range)
464 ret = dst_file->f_op->remap_file_range(src_file, src_pos, dst_file,
465 dst_pos, len, remap_flags | REMAP_FILE_DEDUP);
467 mnt_drop_write_file(dst_file);
471 EXPORT_SYMBOL(vfs_dedupe_file_range_one);
473 int vfs_dedupe_file_range(struct file *file, struct file_dedupe_range *same)
475 struct file_dedupe_range_info *info;
476 struct inode *src = file_inode(file);
481 u16 count = same->dest_count;
484 if (!(file->f_mode & FMODE_READ))
487 if (same->reserved1 || same->reserved2)
490 off = same->src_offset;
491 len = same->src_length;
493 if (S_ISDIR(src->i_mode))
496 if (!S_ISREG(src->i_mode))
499 if (!file->f_op->remap_file_range)
502 ret = remap_verify_area(file, off, len, false);
507 if (off + len > i_size_read(src))
510 /* Arbitrary 1G limit on a single dedupe request, can be raised. */
511 len = min_t(u64, len, 1 << 30);
513 /* pre-format output fields to sane values */
514 for (i = 0; i < count; i++) {
515 same->info[i].bytes_deduped = 0ULL;
516 same->info[i].status = FILE_DEDUPE_RANGE_SAME;
519 for (i = 0, info = same->info; i < count; i++, info++) {
520 struct fd dst_fd = fdget(info->dest_fd);
521 struct file *dst_file = dst_fd.file;
524 info->status = -EBADF;
528 if (info->reserved) {
529 info->status = -EINVAL;
533 deduped = vfs_dedupe_file_range_one(file, off, dst_file,
534 info->dest_offset, len,
535 REMAP_FILE_CAN_SHORTEN);
536 if (deduped == -EBADE)
537 info->status = FILE_DEDUPE_RANGE_DIFFERS;
538 else if (deduped < 0)
539 info->status = deduped;
541 info->bytes_deduped = len;
546 if (fatal_signal_pending(current))
551 EXPORT_SYMBOL(vfs_dedupe_file_range);