102e83f46SDarrick J. Wong // SPDX-License-Identifier: GPL-2.0-only
202e83f46SDarrick J. Wong #include <linux/slab.h>
302e83f46SDarrick J. Wong #include <linux/stat.h>
402e83f46SDarrick J. Wong #include <linux/sched/xacct.h>
502e83f46SDarrick J. Wong #include <linux/fcntl.h>
602e83f46SDarrick J. Wong #include <linux/file.h>
702e83f46SDarrick J. Wong #include <linux/uio.h>
802e83f46SDarrick J. Wong #include <linux/fsnotify.h>
902e83f46SDarrick J. Wong #include <linux/security.h>
1002e83f46SDarrick J. Wong #include <linux/export.h>
1102e83f46SDarrick J. Wong #include <linux/syscalls.h>
1202e83f46SDarrick J. Wong #include <linux/pagemap.h>
1302e83f46SDarrick J. Wong #include <linux/splice.h>
1402e83f46SDarrick J. Wong #include <linux/compat.h>
1502e83f46SDarrick J. Wong #include <linux/mount.h>
1602e83f46SDarrick J. Wong #include <linux/fs.h>
176f7db389SShiyang Ruan #include <linux/dax.h>
18*b7a9a503SDavid Sterba #include <linux/overflow.h>
1902e83f46SDarrick J. Wong #include "internal.h"
2002e83f46SDarrick J. Wong
2102e83f46SDarrick J. Wong #include <linux/uaccess.h>
2202e83f46SDarrick J. Wong #include <asm/unistd.h>
2302e83f46SDarrick J. Wong
2402e83f46SDarrick J. Wong /*
2502e83f46SDarrick J. Wong * Performs necessary checks before doing a clone.
2602e83f46SDarrick J. Wong *
2702e83f46SDarrick J. Wong * Can adjust amount of bytes to clone via @req_count argument.
2802e83f46SDarrick J. Wong * Returns appropriate error code that caller should return or
2902e83f46SDarrick J. Wong * zero in case the clone should be allowed.
3002e83f46SDarrick J. Wong */
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)311b2c54d6SDarrick J. Wong static int generic_remap_checks(struct file *file_in, loff_t pos_in,
3202e83f46SDarrick J. Wong struct file *file_out, loff_t pos_out,
3302e83f46SDarrick J. Wong loff_t *req_count, unsigned int remap_flags)
3402e83f46SDarrick J. Wong {
3502e83f46SDarrick J. Wong struct inode *inode_in = file_in->f_mapping->host;
3602e83f46SDarrick J. Wong struct inode *inode_out = file_out->f_mapping->host;
3702e83f46SDarrick J. Wong uint64_t count = *req_count;
3802e83f46SDarrick J. Wong uint64_t bcount;
3902e83f46SDarrick J. Wong loff_t size_in, size_out;
4002e83f46SDarrick J. Wong loff_t bs = inode_out->i_sb->s_blocksize;
4102e83f46SDarrick J. Wong int ret;
4202e83f46SDarrick J. Wong
4302e83f46SDarrick J. Wong /* The start of both ranges must be aligned to an fs block. */
4402e83f46SDarrick J. Wong if (!IS_ALIGNED(pos_in, bs) || !IS_ALIGNED(pos_out, bs))
4502e83f46SDarrick J. Wong return -EINVAL;
4602e83f46SDarrick J. Wong
4702e83f46SDarrick J. Wong /* Ensure offsets don't wrap. */
4802e83f46SDarrick J. Wong if (pos_in + count < pos_in || pos_out + count < pos_out)
4902e83f46SDarrick J. Wong return -EINVAL;
5002e83f46SDarrick J. Wong
5102e83f46SDarrick J. Wong size_in = i_size_read(inode_in);
5202e83f46SDarrick J. Wong size_out = i_size_read(inode_out);
5302e83f46SDarrick J. Wong
5402e83f46SDarrick J. Wong /* Dedupe requires both ranges to be within EOF. */
5502e83f46SDarrick J. Wong if ((remap_flags & REMAP_FILE_DEDUP) &&
5602e83f46SDarrick J. Wong (pos_in >= size_in || pos_in + count > size_in ||
5702e83f46SDarrick J. Wong pos_out >= size_out || pos_out + count > size_out))
5802e83f46SDarrick J. Wong return -EINVAL;
5902e83f46SDarrick J. Wong
6002e83f46SDarrick J. Wong /* Ensure the infile range is within the infile. */
6102e83f46SDarrick J. Wong if (pos_in >= size_in)
6202e83f46SDarrick J. Wong return -EINVAL;
6302e83f46SDarrick J. Wong count = min(count, size_in - (uint64_t)pos_in);
6402e83f46SDarrick J. Wong
6502e83f46SDarrick J. Wong ret = generic_write_check_limits(file_out, pos_out, &count);
6602e83f46SDarrick J. Wong if (ret)
6702e83f46SDarrick J. Wong return ret;
6802e83f46SDarrick J. Wong
6902e83f46SDarrick J. Wong /*
7002e83f46SDarrick J. Wong * If the user wanted us to link to the infile's EOF, round up to the
7102e83f46SDarrick J. Wong * next block boundary for this check.
7202e83f46SDarrick J. Wong *
7302e83f46SDarrick J. Wong * Otherwise, make sure the count is also block-aligned, having
7402e83f46SDarrick J. Wong * already confirmed the starting offsets' block alignment.
7502e83f46SDarrick J. Wong */
765750676bSDave Chinner if (pos_in + count == size_in &&
775750676bSDave Chinner (!(remap_flags & REMAP_FILE_DEDUP) || pos_out + count == size_out)) {
7802e83f46SDarrick J. Wong bcount = ALIGN(size_in, bs) - pos_in;
7902e83f46SDarrick J. Wong } else {
8002e83f46SDarrick J. Wong if (!IS_ALIGNED(count, bs))
8102e83f46SDarrick J. Wong count = ALIGN_DOWN(count, bs);
8202e83f46SDarrick J. Wong bcount = count;
8302e83f46SDarrick J. Wong }
8402e83f46SDarrick J. Wong
8502e83f46SDarrick J. Wong /* Don't allow overlapped cloning within the same file. */
8602e83f46SDarrick J. Wong if (inode_in == inode_out &&
8702e83f46SDarrick J. Wong pos_out + bcount > pos_in &&
8802e83f46SDarrick J. Wong pos_out < pos_in + bcount)
8902e83f46SDarrick J. Wong return -EINVAL;
9002e83f46SDarrick J. Wong
9102e83f46SDarrick J. Wong /*
9202e83f46SDarrick J. Wong * We shortened the request but the caller can't deal with that, so
9302e83f46SDarrick J. Wong * bounce the request back to userspace.
9402e83f46SDarrick J. Wong */
9502e83f46SDarrick J. Wong if (*req_count != count && !(remap_flags & REMAP_FILE_CAN_SHORTEN))
9602e83f46SDarrick J. Wong return -EINVAL;
9702e83f46SDarrick J. Wong
9802e83f46SDarrick J. Wong *req_count = count;
9902e83f46SDarrick J. Wong return 0;
10002e83f46SDarrick J. Wong }
1011b2c54d6SDarrick J. Wong
remap_verify_area(struct file * file,loff_t pos,loff_t len,bool write)1021b2c54d6SDarrick J. Wong static int remap_verify_area(struct file *file, loff_t pos, loff_t len,
1031b2c54d6SDarrick J. Wong bool write)
1041b2c54d6SDarrick J. Wong {
105*b7a9a503SDavid Sterba loff_t tmp;
106*b7a9a503SDavid Sterba
1071b2c54d6SDarrick J. Wong if (unlikely(pos < 0 || len < 0))
1081b2c54d6SDarrick J. Wong return -EINVAL;
1091b2c54d6SDarrick J. Wong
110*b7a9a503SDavid Sterba if (unlikely(check_add_overflow(pos, len, &tmp)))
1111b2c54d6SDarrick J. Wong return -EINVAL;
1121b2c54d6SDarrick J. Wong
1131b2c54d6SDarrick J. Wong return security_file_permission(file, write ? MAY_WRITE : MAY_READ);
1141b2c54d6SDarrick J. Wong }
1151b2c54d6SDarrick J. Wong
1161b2c54d6SDarrick J. Wong /*
1171b2c54d6SDarrick J. Wong * Ensure that we don't remap a partial EOF block in the middle of something
1181b2c54d6SDarrick J. Wong * else. Assume that the offsets have already been checked for block
1191b2c54d6SDarrick J. Wong * alignment.
1201b2c54d6SDarrick J. Wong *
1211b2c54d6SDarrick J. Wong * For clone we only link a partial EOF block above or at the destination file's
1221b2c54d6SDarrick J. Wong * EOF. For deduplication we accept a partial EOF block only if it ends at the
1231b2c54d6SDarrick J. Wong * destination file's EOF (can not link it into the middle of a file).
1241b2c54d6SDarrick J. Wong *
1251b2c54d6SDarrick J. Wong * Shorten the request if possible.
1261b2c54d6SDarrick J. Wong */
generic_remap_check_len(struct inode * inode_in,struct inode * inode_out,loff_t pos_out,loff_t * len,unsigned int remap_flags)1271b2c54d6SDarrick J. Wong static int generic_remap_check_len(struct inode *inode_in,
1281b2c54d6SDarrick J. Wong struct inode *inode_out,
1291b2c54d6SDarrick J. Wong loff_t pos_out,
1301b2c54d6SDarrick J. Wong loff_t *len,
1311b2c54d6SDarrick J. Wong unsigned int remap_flags)
1321b2c54d6SDarrick J. Wong {
1331b2c54d6SDarrick J. Wong u64 blkmask = i_blocksize(inode_in) - 1;
1341b2c54d6SDarrick J. Wong loff_t new_len = *len;
1351b2c54d6SDarrick J. Wong
1361b2c54d6SDarrick J. Wong if ((*len & blkmask) == 0)
1371b2c54d6SDarrick J. Wong return 0;
1381b2c54d6SDarrick J. Wong
1391b2c54d6SDarrick J. Wong if (pos_out + *len < i_size_read(inode_out))
1401b2c54d6SDarrick J. Wong new_len &= ~blkmask;
1411b2c54d6SDarrick J. Wong
1421b2c54d6SDarrick J. Wong if (new_len == *len)
1431b2c54d6SDarrick J. Wong return 0;
1441b2c54d6SDarrick J. Wong
1451b2c54d6SDarrick J. Wong if (remap_flags & REMAP_FILE_CAN_SHORTEN) {
1461b2c54d6SDarrick J. Wong *len = new_len;
1471b2c54d6SDarrick J. Wong return 0;
1481b2c54d6SDarrick J. Wong }
1491b2c54d6SDarrick J. Wong
1501b2c54d6SDarrick J. Wong return (remap_flags & REMAP_FILE_DEDUP) ? -EBADE : -EINVAL;
1511b2c54d6SDarrick J. Wong }
1521b2c54d6SDarrick J. Wong
1531b2c54d6SDarrick J. Wong /* Read a page's worth of file data into the page cache. */
vfs_dedupe_get_folio(struct file * file,loff_t pos)1544495a96cSMatthew Wilcox (Oracle) static struct folio *vfs_dedupe_get_folio(struct file *file, loff_t pos)
1551b2c54d6SDarrick J. Wong {
156da028b6bSMatthew Wilcox (Oracle) return read_mapping_folio(file->f_mapping, pos >> PAGE_SHIFT, file);
1571b2c54d6SDarrick J. Wong }
1581b2c54d6SDarrick J. Wong
1591b2c54d6SDarrick J. Wong /*
160338f379cSMatthew Wilcox (Oracle) * Lock two folios, ensuring that we lock in offset order if the folios
161338f379cSMatthew Wilcox (Oracle) * are from the same file.
1621b2c54d6SDarrick J. Wong */
vfs_lock_two_folios(struct folio * folio1,struct folio * folio2)163338f379cSMatthew Wilcox (Oracle) static void vfs_lock_two_folios(struct folio *folio1, struct folio *folio2)
1641b2c54d6SDarrick J. Wong {
1651b2c54d6SDarrick J. Wong /* Always lock in order of increasing index. */
166338f379cSMatthew Wilcox (Oracle) if (folio1->index > folio2->index)
167338f379cSMatthew Wilcox (Oracle) swap(folio1, folio2);
1681b2c54d6SDarrick J. Wong
169338f379cSMatthew Wilcox (Oracle) folio_lock(folio1);
170338f379cSMatthew Wilcox (Oracle) if (folio1 != folio2)
171338f379cSMatthew Wilcox (Oracle) folio_lock(folio2);
1721b2c54d6SDarrick J. Wong }
1731b2c54d6SDarrick J. Wong
174338f379cSMatthew Wilcox (Oracle) /* Unlock two folios, being careful not to unlock the same folio twice. */
vfs_unlock_two_folios(struct folio * folio1,struct folio * folio2)175338f379cSMatthew Wilcox (Oracle) static void vfs_unlock_two_folios(struct folio *folio1, struct folio *folio2)
1761b2c54d6SDarrick J. Wong {
177338f379cSMatthew Wilcox (Oracle) folio_unlock(folio1);
178338f379cSMatthew Wilcox (Oracle) if (folio1 != folio2)
179338f379cSMatthew Wilcox (Oracle) folio_unlock(folio2);
1801b2c54d6SDarrick J. Wong }
1811b2c54d6SDarrick J. Wong
1821b2c54d6SDarrick J. Wong /*
1831b2c54d6SDarrick J. Wong * Compare extents of two files to see if they are the same.
1841b2c54d6SDarrick J. Wong * Caller must have locked both inodes to prevent write races.
1851b2c54d6SDarrick J. Wong */
vfs_dedupe_file_range_compare(struct file * src,loff_t srcoff,struct file * dest,loff_t dstoff,loff_t len,bool * is_same)1864495a96cSMatthew Wilcox (Oracle) static int vfs_dedupe_file_range_compare(struct file *src, loff_t srcoff,
1874495a96cSMatthew Wilcox (Oracle) struct file *dest, loff_t dstoff,
1881b2c54d6SDarrick J. Wong loff_t len, bool *is_same)
1891b2c54d6SDarrick J. Wong {
190338f379cSMatthew Wilcox (Oracle) bool same = true;
191338f379cSMatthew Wilcox (Oracle) int error = -EINVAL;
1921b2c54d6SDarrick J. Wong
1931b2c54d6SDarrick J. Wong while (len) {
194338f379cSMatthew Wilcox (Oracle) struct folio *src_folio, *dst_folio;
195338f379cSMatthew Wilcox (Oracle) void *src_addr, *dst_addr;
196338f379cSMatthew Wilcox (Oracle) loff_t cmp_len = min(PAGE_SIZE - offset_in_page(srcoff),
197338f379cSMatthew Wilcox (Oracle) PAGE_SIZE - offset_in_page(dstoff));
198338f379cSMatthew Wilcox (Oracle)
1991b2c54d6SDarrick J. Wong cmp_len = min(cmp_len, len);
2001b2c54d6SDarrick J. Wong if (cmp_len <= 0)
2011b2c54d6SDarrick J. Wong goto out_error;
2021b2c54d6SDarrick J. Wong
203338f379cSMatthew Wilcox (Oracle) src_folio = vfs_dedupe_get_folio(src, srcoff);
204338f379cSMatthew Wilcox (Oracle) if (IS_ERR(src_folio)) {
205338f379cSMatthew Wilcox (Oracle) error = PTR_ERR(src_folio);
2061b2c54d6SDarrick J. Wong goto out_error;
2071b2c54d6SDarrick J. Wong }
208338f379cSMatthew Wilcox (Oracle) dst_folio = vfs_dedupe_get_folio(dest, dstoff);
209338f379cSMatthew Wilcox (Oracle) if (IS_ERR(dst_folio)) {
210338f379cSMatthew Wilcox (Oracle) error = PTR_ERR(dst_folio);
211338f379cSMatthew Wilcox (Oracle) folio_put(src_folio);
2121b2c54d6SDarrick J. Wong goto out_error;
2131b2c54d6SDarrick J. Wong }
2141b2c54d6SDarrick J. Wong
215338f379cSMatthew Wilcox (Oracle) vfs_lock_two_folios(src_folio, dst_folio);
2161b2c54d6SDarrick J. Wong
2171b2c54d6SDarrick J. Wong /*
218338f379cSMatthew Wilcox (Oracle) * Now that we've locked both folios, make sure they're still
2191b2c54d6SDarrick J. Wong * mapped to the file data we're interested in. If not,
2201b2c54d6SDarrick J. Wong * someone is invalidating pages on us and we lose.
2211b2c54d6SDarrick J. Wong */
222338f379cSMatthew Wilcox (Oracle) if (!folio_test_uptodate(src_folio) || !folio_test_uptodate(dst_folio) ||
2234495a96cSMatthew Wilcox (Oracle) src_folio->mapping != src->f_mapping ||
2244495a96cSMatthew Wilcox (Oracle) dst_folio->mapping != dest->f_mapping) {
2251b2c54d6SDarrick J. Wong same = false;
2261b2c54d6SDarrick J. Wong goto unlock;
2271b2c54d6SDarrick J. Wong }
2281b2c54d6SDarrick J. Wong
229338f379cSMatthew Wilcox (Oracle) src_addr = kmap_local_folio(src_folio,
230338f379cSMatthew Wilcox (Oracle) offset_in_folio(src_folio, srcoff));
231338f379cSMatthew Wilcox (Oracle) dst_addr = kmap_local_folio(dst_folio,
232338f379cSMatthew Wilcox (Oracle) offset_in_folio(dst_folio, dstoff));
2331b2c54d6SDarrick J. Wong
234338f379cSMatthew Wilcox (Oracle) flush_dcache_folio(src_folio);
235338f379cSMatthew Wilcox (Oracle) flush_dcache_folio(dst_folio);
2361b2c54d6SDarrick J. Wong
237338f379cSMatthew Wilcox (Oracle) if (memcmp(src_addr, dst_addr, cmp_len))
2381b2c54d6SDarrick J. Wong same = false;
2391b2c54d6SDarrick J. Wong
240338f379cSMatthew Wilcox (Oracle) kunmap_local(dst_addr);
241338f379cSMatthew Wilcox (Oracle) kunmap_local(src_addr);
2421b2c54d6SDarrick J. Wong unlock:
243338f379cSMatthew Wilcox (Oracle) vfs_unlock_two_folios(src_folio, dst_folio);
244338f379cSMatthew Wilcox (Oracle) folio_put(dst_folio);
245338f379cSMatthew Wilcox (Oracle) folio_put(src_folio);
2461b2c54d6SDarrick J. Wong
2471b2c54d6SDarrick J. Wong if (!same)
2481b2c54d6SDarrick J. Wong break;
2491b2c54d6SDarrick J. Wong
2501b2c54d6SDarrick J. Wong srcoff += cmp_len;
251338f379cSMatthew Wilcox (Oracle) dstoff += cmp_len;
2521b2c54d6SDarrick J. Wong len -= cmp_len;
2531b2c54d6SDarrick J. Wong }
2541b2c54d6SDarrick J. Wong
2551b2c54d6SDarrick J. Wong *is_same = same;
2561b2c54d6SDarrick J. Wong return 0;
2571b2c54d6SDarrick J. Wong
2581b2c54d6SDarrick J. Wong out_error:
2591b2c54d6SDarrick J. Wong return error;
2601b2c54d6SDarrick J. Wong }
2611b2c54d6SDarrick J. Wong
2621b2c54d6SDarrick J. Wong /*
2631b2c54d6SDarrick J. Wong * Check that the two inodes are eligible for cloning, the ranges make
2641b2c54d6SDarrick J. Wong * sense, and then flush all dirty data. Caller must ensure that the
2651b2c54d6SDarrick J. Wong * inodes have been locked against any other modifications.
2661b2c54d6SDarrick J. Wong *
2671b2c54d6SDarrick J. Wong * If there's an error, then the usual negative error code is returned.
2681b2c54d6SDarrick J. Wong * Otherwise returns 0 with *len set to the request length.
2691b2c54d6SDarrick J. Wong */
2706f7db389SShiyang Ruan int
__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,const struct iomap_ops * dax_read_ops)2716f7db389SShiyang Ruan __generic_remap_file_range_prep(struct file *file_in, loff_t pos_in,
2721b2c54d6SDarrick J. Wong struct file *file_out, loff_t pos_out,
2736f7db389SShiyang Ruan loff_t *len, unsigned int remap_flags,
2746f7db389SShiyang Ruan const struct iomap_ops *dax_read_ops)
2751b2c54d6SDarrick J. Wong {
2761b2c54d6SDarrick J. Wong struct inode *inode_in = file_inode(file_in);
2771b2c54d6SDarrick J. Wong struct inode *inode_out = file_inode(file_out);
2781b2c54d6SDarrick J. Wong bool same_inode = (inode_in == inode_out);
2791b2c54d6SDarrick J. Wong int ret;
2801b2c54d6SDarrick J. Wong
2811b2c54d6SDarrick J. Wong /* Don't touch certain kinds of inodes */
2821b2c54d6SDarrick J. Wong if (IS_IMMUTABLE(inode_out))
2831b2c54d6SDarrick J. Wong return -EPERM;
2841b2c54d6SDarrick J. Wong
2851b2c54d6SDarrick J. Wong if (IS_SWAPFILE(inode_in) || IS_SWAPFILE(inode_out))
2861b2c54d6SDarrick J. Wong return -ETXTBSY;
2871b2c54d6SDarrick J. Wong
2881b2c54d6SDarrick J. Wong /* Don't reflink dirs, pipes, sockets... */
2891b2c54d6SDarrick J. Wong if (S_ISDIR(inode_in->i_mode) || S_ISDIR(inode_out->i_mode))
2901b2c54d6SDarrick J. Wong return -EISDIR;
2911b2c54d6SDarrick J. Wong if (!S_ISREG(inode_in->i_mode) || !S_ISREG(inode_out->i_mode))
2921b2c54d6SDarrick J. Wong return -EINVAL;
2931b2c54d6SDarrick J. Wong
2941b2c54d6SDarrick J. Wong /* Zero length dedupe exits immediately; reflink goes to EOF. */
2951b2c54d6SDarrick J. Wong if (*len == 0) {
2961b2c54d6SDarrick J. Wong loff_t isize = i_size_read(inode_in);
2971b2c54d6SDarrick J. Wong
2981b2c54d6SDarrick J. Wong if ((remap_flags & REMAP_FILE_DEDUP) || pos_in == isize)
2991b2c54d6SDarrick J. Wong return 0;
3001b2c54d6SDarrick J. Wong if (pos_in > isize)
3011b2c54d6SDarrick J. Wong return -EINVAL;
3021b2c54d6SDarrick J. Wong *len = isize - pos_in;
3031b2c54d6SDarrick J. Wong if (*len == 0)
3041b2c54d6SDarrick J. Wong return 0;
3051b2c54d6SDarrick J. Wong }
3061b2c54d6SDarrick J. Wong
3071b2c54d6SDarrick J. Wong /* Check that we don't violate system file offset limits. */
3081b2c54d6SDarrick J. Wong ret = generic_remap_checks(file_in, pos_in, file_out, pos_out, len,
3091b2c54d6SDarrick J. Wong remap_flags);
310a79168a0SBrian Foster if (ret || *len == 0)
3111b2c54d6SDarrick J. Wong return ret;
3121b2c54d6SDarrick J. Wong
3131b2c54d6SDarrick J. Wong /* Wait for the completion of any pending IOs on both files */
3141b2c54d6SDarrick J. Wong inode_dio_wait(inode_in);
3151b2c54d6SDarrick J. Wong if (!same_inode)
3161b2c54d6SDarrick J. Wong inode_dio_wait(inode_out);
3171b2c54d6SDarrick J. Wong
3181b2c54d6SDarrick J. Wong ret = filemap_write_and_wait_range(inode_in->i_mapping,
3191b2c54d6SDarrick J. Wong pos_in, pos_in + *len - 1);
3201b2c54d6SDarrick J. Wong if (ret)
3211b2c54d6SDarrick J. Wong return ret;
3221b2c54d6SDarrick J. Wong
3231b2c54d6SDarrick J. Wong ret = filemap_write_and_wait_range(inode_out->i_mapping,
3241b2c54d6SDarrick J. Wong pos_out, pos_out + *len - 1);
3251b2c54d6SDarrick J. Wong if (ret)
3261b2c54d6SDarrick J. Wong return ret;
3271b2c54d6SDarrick J. Wong
3281b2c54d6SDarrick J. Wong /*
3291b2c54d6SDarrick J. Wong * Check that the extents are the same.
3301b2c54d6SDarrick J. Wong */
3311b2c54d6SDarrick J. Wong if (remap_flags & REMAP_FILE_DEDUP) {
3321b2c54d6SDarrick J. Wong bool is_same = false;
3331b2c54d6SDarrick J. Wong
3346f7db389SShiyang Ruan if (!IS_DAX(inode_in))
3354495a96cSMatthew Wilcox (Oracle) ret = vfs_dedupe_file_range_compare(file_in, pos_in,
3364495a96cSMatthew Wilcox (Oracle) file_out, pos_out, *len, &is_same);
3376f7db389SShiyang Ruan else if (dax_read_ops)
3386f7db389SShiyang Ruan ret = dax_dedupe_file_range_compare(inode_in, pos_in,
3396f7db389SShiyang Ruan inode_out, pos_out, *len, &is_same,
3406f7db389SShiyang Ruan dax_read_ops);
3416f7db389SShiyang Ruan else
3426f7db389SShiyang Ruan return -EINVAL;
3431b2c54d6SDarrick J. Wong if (ret)
3441b2c54d6SDarrick J. Wong return ret;
3451b2c54d6SDarrick J. Wong if (!is_same)
3461b2c54d6SDarrick J. Wong return -EBADE;
3471b2c54d6SDarrick J. Wong }
3481b2c54d6SDarrick J. Wong
3491b2c54d6SDarrick J. Wong ret = generic_remap_check_len(inode_in, inode_out, pos_out, len,
3501b2c54d6SDarrick J. Wong remap_flags);
351a79168a0SBrian Foster if (ret || *len == 0)
3521b2c54d6SDarrick J. Wong return ret;
3531b2c54d6SDarrick J. Wong
3541b2c54d6SDarrick J. Wong /* If can't alter the file contents, we're done. */
3551b2c54d6SDarrick J. Wong if (!(remap_flags & REMAP_FILE_DEDUP))
3561b2c54d6SDarrick J. Wong ret = file_modified(file_out);
3571b2c54d6SDarrick J. Wong
3581b2c54d6SDarrick J. Wong return ret;
3591b2c54d6SDarrick J. Wong }
3606f7db389SShiyang Ruan
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)3616f7db389SShiyang Ruan int generic_remap_file_range_prep(struct file *file_in, loff_t pos_in,
3626f7db389SShiyang Ruan struct file *file_out, loff_t pos_out,
3636f7db389SShiyang Ruan loff_t *len, unsigned int remap_flags)
3646f7db389SShiyang Ruan {
3656f7db389SShiyang Ruan return __generic_remap_file_range_prep(file_in, pos_in, file_out,
3666f7db389SShiyang Ruan pos_out, len, remap_flags, NULL);
3676f7db389SShiyang Ruan }
3681b2c54d6SDarrick J. Wong EXPORT_SYMBOL(generic_remap_file_range_prep);
3691b2c54d6SDarrick J. Wong
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)3701b2c54d6SDarrick J. Wong loff_t do_clone_file_range(struct file *file_in, loff_t pos_in,
3711b2c54d6SDarrick J. Wong struct file *file_out, loff_t pos_out,
3721b2c54d6SDarrick J. Wong loff_t len, unsigned int remap_flags)
3731b2c54d6SDarrick J. Wong {
3741b2c54d6SDarrick J. Wong loff_t ret;
3751b2c54d6SDarrick J. Wong
3761b2c54d6SDarrick J. Wong WARN_ON_ONCE(remap_flags & REMAP_FILE_DEDUP);
3771b2c54d6SDarrick J. Wong
3781b2c54d6SDarrick J. Wong if (file_inode(file_in)->i_sb != file_inode(file_out)->i_sb)
3791b2c54d6SDarrick J. Wong return -EXDEV;
3801b2c54d6SDarrick J. Wong
3811b2c54d6SDarrick J. Wong ret = generic_file_rw_checks(file_in, file_out);
3821b2c54d6SDarrick J. Wong if (ret < 0)
3831b2c54d6SDarrick J. Wong return ret;
3841b2c54d6SDarrick J. Wong
3851b2c54d6SDarrick J. Wong if (!file_in->f_op->remap_file_range)
3861b2c54d6SDarrick J. Wong return -EOPNOTSUPP;
3871b2c54d6SDarrick J. Wong
3881b2c54d6SDarrick J. Wong ret = remap_verify_area(file_in, pos_in, len, false);
3891b2c54d6SDarrick J. Wong if (ret)
3901b2c54d6SDarrick J. Wong return ret;
3911b2c54d6SDarrick J. Wong
3921b2c54d6SDarrick J. Wong ret = remap_verify_area(file_out, pos_out, len, true);
3931b2c54d6SDarrick J. Wong if (ret)
3941b2c54d6SDarrick J. Wong return ret;
3951b2c54d6SDarrick J. Wong
3961b2c54d6SDarrick J. Wong ret = file_in->f_op->remap_file_range(file_in, pos_in,
3971b2c54d6SDarrick J. Wong file_out, pos_out, len, remap_flags);
3981b2c54d6SDarrick J. Wong if (ret < 0)
3991b2c54d6SDarrick J. Wong return ret;
4001b2c54d6SDarrick J. Wong
4011b2c54d6SDarrick J. Wong fsnotify_access(file_in);
4021b2c54d6SDarrick J. Wong fsnotify_modify(file_out);
4031b2c54d6SDarrick J. Wong return ret;
4041b2c54d6SDarrick J. Wong }
4051b2c54d6SDarrick J. Wong EXPORT_SYMBOL(do_clone_file_range);
4061b2c54d6SDarrick J. Wong
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)4071b2c54d6SDarrick J. Wong loff_t vfs_clone_file_range(struct file *file_in, loff_t pos_in,
4081b2c54d6SDarrick J. Wong struct file *file_out, loff_t pos_out,
4091b2c54d6SDarrick J. Wong loff_t len, unsigned int remap_flags)
4101b2c54d6SDarrick J. Wong {
4111b2c54d6SDarrick J. Wong loff_t ret;
4121b2c54d6SDarrick J. Wong
4131b2c54d6SDarrick J. Wong file_start_write(file_out);
4141b2c54d6SDarrick J. Wong ret = do_clone_file_range(file_in, pos_in, file_out, pos_out, len,
4151b2c54d6SDarrick J. Wong remap_flags);
4161b2c54d6SDarrick J. Wong file_end_write(file_out);
4171b2c54d6SDarrick J. Wong
4181b2c54d6SDarrick J. Wong return ret;
4191b2c54d6SDarrick J. Wong }
4201b2c54d6SDarrick J. Wong EXPORT_SYMBOL(vfs_clone_file_range);
4211b2c54d6SDarrick J. Wong
4221b2c54d6SDarrick J. Wong /* Check whether we are allowed to dedupe the destination file */
allow_file_dedupe(struct file * file)4231b2c54d6SDarrick J. Wong static bool allow_file_dedupe(struct file *file)
4241b2c54d6SDarrick J. Wong {
4254609e1f1SChristian Brauner struct mnt_idmap *idmap = file_mnt_idmap(file);
4260f5d220bSChristian Brauner struct inode *inode = file_inode(file);
4270f5d220bSChristian Brauner
4281b2c54d6SDarrick J. Wong if (capable(CAP_SYS_ADMIN))
4291b2c54d6SDarrick J. Wong return true;
4301b2c54d6SDarrick J. Wong if (file->f_mode & FMODE_WRITE)
4311b2c54d6SDarrick J. Wong return true;
432e67fe633SChristian Brauner if (vfsuid_eq_kuid(i_uid_into_vfsuid(idmap, inode), current_fsuid()))
4331b2c54d6SDarrick J. Wong return true;
4344609e1f1SChristian Brauner if (!inode_permission(idmap, inode, MAY_WRITE))
4351b2c54d6SDarrick J. Wong return true;
4361b2c54d6SDarrick J. Wong return false;
4371b2c54d6SDarrick J. Wong }
4381b2c54d6SDarrick J. Wong
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)4391b2c54d6SDarrick J. Wong loff_t vfs_dedupe_file_range_one(struct file *src_file, loff_t src_pos,
4401b2c54d6SDarrick J. Wong struct file *dst_file, loff_t dst_pos,
4411b2c54d6SDarrick J. Wong loff_t len, unsigned int remap_flags)
4421b2c54d6SDarrick J. Wong {
4431b2c54d6SDarrick J. Wong loff_t ret;
4441b2c54d6SDarrick J. Wong
4451b2c54d6SDarrick J. Wong WARN_ON_ONCE(remap_flags & ~(REMAP_FILE_DEDUP |
4461b2c54d6SDarrick J. Wong REMAP_FILE_CAN_SHORTEN));
4471b2c54d6SDarrick J. Wong
4481b2c54d6SDarrick J. Wong ret = mnt_want_write_file(dst_file);
4491b2c54d6SDarrick J. Wong if (ret)
4501b2c54d6SDarrick J. Wong return ret;
4511b2c54d6SDarrick J. Wong
4523078d85cSMiklos Szeredi /*
4533078d85cSMiklos Szeredi * This is redundant if called from vfs_dedupe_file_range(), but other
4543078d85cSMiklos Szeredi * callers need it and it's not performance sesitive...
4553078d85cSMiklos Szeredi */
4563078d85cSMiklos Szeredi ret = remap_verify_area(src_file, src_pos, len, false);
4573078d85cSMiklos Szeredi if (ret)
4583078d85cSMiklos Szeredi goto out_drop_write;
4593078d85cSMiklos Szeredi
4601b2c54d6SDarrick J. Wong ret = remap_verify_area(dst_file, dst_pos, len, true);
4613078d85cSMiklos Szeredi if (ret)
4621b2c54d6SDarrick J. Wong goto out_drop_write;
4631b2c54d6SDarrick J. Wong
4641b2c54d6SDarrick J. Wong ret = -EPERM;
4651b2c54d6SDarrick J. Wong if (!allow_file_dedupe(dst_file))
4661b2c54d6SDarrick J. Wong goto out_drop_write;
4671b2c54d6SDarrick J. Wong
4681b2c54d6SDarrick J. Wong ret = -EXDEV;
4699f5710bbSJosef Bacik if (file_inode(src_file)->i_sb != file_inode(dst_file)->i_sb)
4701b2c54d6SDarrick J. Wong goto out_drop_write;
4711b2c54d6SDarrick J. Wong
4721b2c54d6SDarrick J. Wong ret = -EISDIR;
4731b2c54d6SDarrick J. Wong if (S_ISDIR(file_inode(dst_file)->i_mode))
4741b2c54d6SDarrick J. Wong goto out_drop_write;
4751b2c54d6SDarrick J. Wong
4761b2c54d6SDarrick J. Wong ret = -EINVAL;
4771b2c54d6SDarrick J. Wong if (!dst_file->f_op->remap_file_range)
4781b2c54d6SDarrick J. Wong goto out_drop_write;
4791b2c54d6SDarrick J. Wong
4801b2c54d6SDarrick J. Wong if (len == 0) {
4811b2c54d6SDarrick J. Wong ret = 0;
4821b2c54d6SDarrick J. Wong goto out_drop_write;
4831b2c54d6SDarrick J. Wong }
4841b2c54d6SDarrick J. Wong
4851b2c54d6SDarrick J. Wong ret = dst_file->f_op->remap_file_range(src_file, src_pos, dst_file,
4861b2c54d6SDarrick J. Wong dst_pos, len, remap_flags | REMAP_FILE_DEDUP);
4871b2c54d6SDarrick J. Wong out_drop_write:
4881b2c54d6SDarrick J. Wong mnt_drop_write_file(dst_file);
4891b2c54d6SDarrick J. Wong
4901b2c54d6SDarrick J. Wong return ret;
4911b2c54d6SDarrick J. Wong }
4921b2c54d6SDarrick J. Wong EXPORT_SYMBOL(vfs_dedupe_file_range_one);
4931b2c54d6SDarrick J. Wong
vfs_dedupe_file_range(struct file * file,struct file_dedupe_range * same)4941b2c54d6SDarrick J. Wong int vfs_dedupe_file_range(struct file *file, struct file_dedupe_range *same)
4951b2c54d6SDarrick J. Wong {
4961b2c54d6SDarrick J. Wong struct file_dedupe_range_info *info;
4971b2c54d6SDarrick J. Wong struct inode *src = file_inode(file);
4981b2c54d6SDarrick J. Wong u64 off;
4991b2c54d6SDarrick J. Wong u64 len;
5001b2c54d6SDarrick J. Wong int i;
5011b2c54d6SDarrick J. Wong int ret;
5021b2c54d6SDarrick J. Wong u16 count = same->dest_count;
5031b2c54d6SDarrick J. Wong loff_t deduped;
5041b2c54d6SDarrick J. Wong
5051b2c54d6SDarrick J. Wong if (!(file->f_mode & FMODE_READ))
5061b2c54d6SDarrick J. Wong return -EINVAL;
5071b2c54d6SDarrick J. Wong
5081b2c54d6SDarrick J. Wong if (same->reserved1 || same->reserved2)
5091b2c54d6SDarrick J. Wong return -EINVAL;
5101b2c54d6SDarrick J. Wong
5111b2c54d6SDarrick J. Wong off = same->src_offset;
5121b2c54d6SDarrick J. Wong len = same->src_length;
5131b2c54d6SDarrick J. Wong
5141b2c54d6SDarrick J. Wong if (S_ISDIR(src->i_mode))
5151b2c54d6SDarrick J. Wong return -EISDIR;
5161b2c54d6SDarrick J. Wong
5171b2c54d6SDarrick J. Wong if (!S_ISREG(src->i_mode))
5181b2c54d6SDarrick J. Wong return -EINVAL;
5191b2c54d6SDarrick J. Wong
5201b2c54d6SDarrick J. Wong if (!file->f_op->remap_file_range)
5211b2c54d6SDarrick J. Wong return -EOPNOTSUPP;
5221b2c54d6SDarrick J. Wong
5231b2c54d6SDarrick J. Wong ret = remap_verify_area(file, off, len, false);
5241b2c54d6SDarrick J. Wong if (ret < 0)
5251b2c54d6SDarrick J. Wong return ret;
5261b2c54d6SDarrick J. Wong ret = 0;
5271b2c54d6SDarrick J. Wong
5281b2c54d6SDarrick J. Wong if (off + len > i_size_read(src))
5291b2c54d6SDarrick J. Wong return -EINVAL;
5301b2c54d6SDarrick J. Wong
5311b2c54d6SDarrick J. Wong /* Arbitrary 1G limit on a single dedupe request, can be raised. */
5321b2c54d6SDarrick J. Wong len = min_t(u64, len, 1 << 30);
5331b2c54d6SDarrick J. Wong
5341b2c54d6SDarrick J. Wong /* pre-format output fields to sane values */
5351b2c54d6SDarrick J. Wong for (i = 0; i < count; i++) {
5361b2c54d6SDarrick J. Wong same->info[i].bytes_deduped = 0ULL;
5371b2c54d6SDarrick J. Wong same->info[i].status = FILE_DEDUPE_RANGE_SAME;
5381b2c54d6SDarrick J. Wong }
5391b2c54d6SDarrick J. Wong
5401b2c54d6SDarrick J. Wong for (i = 0, info = same->info; i < count; i++, info++) {
5411b2c54d6SDarrick J. Wong struct fd dst_fd = fdget(info->dest_fd);
5421b2c54d6SDarrick J. Wong struct file *dst_file = dst_fd.file;
5431b2c54d6SDarrick J. Wong
5441b2c54d6SDarrick J. Wong if (!dst_file) {
5451b2c54d6SDarrick J. Wong info->status = -EBADF;
5461b2c54d6SDarrick J. Wong goto next_loop;
5471b2c54d6SDarrick J. Wong }
5481b2c54d6SDarrick J. Wong
5491b2c54d6SDarrick J. Wong if (info->reserved) {
5501b2c54d6SDarrick J. Wong info->status = -EINVAL;
5511b2c54d6SDarrick J. Wong goto next_fdput;
5521b2c54d6SDarrick J. Wong }
5531b2c54d6SDarrick J. Wong
5541b2c54d6SDarrick J. Wong deduped = vfs_dedupe_file_range_one(file, off, dst_file,
5551b2c54d6SDarrick J. Wong info->dest_offset, len,
5561b2c54d6SDarrick J. Wong REMAP_FILE_CAN_SHORTEN);
5571b2c54d6SDarrick J. Wong if (deduped == -EBADE)
5581b2c54d6SDarrick J. Wong info->status = FILE_DEDUPE_RANGE_DIFFERS;
5591b2c54d6SDarrick J. Wong else if (deduped < 0)
5601b2c54d6SDarrick J. Wong info->status = deduped;
5611b2c54d6SDarrick J. Wong else
562b926f2adSLinus Torvalds info->bytes_deduped = len;
5631b2c54d6SDarrick J. Wong
5641b2c54d6SDarrick J. Wong next_fdput:
5651b2c54d6SDarrick J. Wong fdput(dst_fd);
5661b2c54d6SDarrick J. Wong next_loop:
5671b2c54d6SDarrick J. Wong if (fatal_signal_pending(current))
5681b2c54d6SDarrick J. Wong break;
5691b2c54d6SDarrick J. Wong }
5701b2c54d6SDarrick J. Wong return ret;
5711b2c54d6SDarrick J. Wong }
5721b2c54d6SDarrick J. Wong EXPORT_SYMBOL(vfs_dedupe_file_range);
573