Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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