Subject: linux-next: manual merge of the vfs tree with the xfs tree

From: Stephen Rothwell <[email-masked]>
Date: Wed, 31 Oct 2018 11:52:47 +1100

Hi all,

[I don't understand why all this new work turned up in the xfs tree
during the merge window ...]

Today's linux-next merge of the vfs tree got a conflict in:

  fs/read_write.c

between commits:

  42ec3d4c0218 ("vfs: make remap_file_range functions take and return bytes completed")
  eca3654e3cc7 ("vfs: enable remap callers that can handle short operations")

from the xfs tree and commit:

  5de4480ae7f8 ("vfs: allow dedupe of user owned read-only files")

from the vfs tree.

I fixed it up (see below) and can carry the fix as necessary. This
is now fixed as far as linux-next is concerned, but any non trivial
conflicts should be mentioned to your upstream maintainer when your tree
is submitted for merging.  You may also want to consider cooperating
with the maintainer of the conflicting tree to minimise any particularly
complex conflicts.

-- 
Cheers,
Stephen Rothwell

diff --cc fs/read_write.c
index 50680b900b52,10f9bed985f4..000000000000
--- a/fs/read_write.c
+++ b/fs/read_write.c
@@@ -2014,14 -1880,120 +2013,28 @@@ loff_t vfs_clone_file_range(struct fil
  }
  EXPORT_SYMBOL(vfs_clone_file_range);
  
 -/*
 - * Read a page's worth of file data into the page cache.  Return the page
 - * locked.
 - */
 -static struct page *vfs_dedupe_get_page(struct inode *inode, loff_t offset)
 -{
 -	struct address_space *mapping;
 -	struct page *page;
 -	pgoff_t n;
 -
 -	n = offset >> PAGE_SHIFT;
 -	mapping = inode->i_mapping;
 -	page = read_mapping_page(mapping, n, NULL);
 -	if (IS_ERR(page))
 -		return page;
 -	if (!PageUptodate(page)) {
 -		put_page(page);
 -		return ERR_PTR(-EIO);
 -	}
 -	lock_page(page);
 -	return page;
 -}
 -
 -/*
 - * Compare extents of two files to see if they are the same.
 - * Caller must have locked both inodes to prevent write races.
 - */
 -int vfs_dedupe_file_range_compare(struct inode *src, loff_t srcoff,
 -				  struct inode *dest, loff_t destoff,
 -				  loff_t len, bool *is_same)
 -{
 -	loff_t src_poff;
 -	loff_t dest_poff;
 -	void *src_addr;
 -	void *dest_addr;
 -	struct page *src_page;
 -	struct page *dest_page;
 -	loff_t cmp_len;
 -	bool same;
 -	int error;
 -
 -	error = -EINVAL;
 -	same = true;
 -	while (len) {
 -		src_poff = srcoff & (PAGE_SIZE - 1);
 -		dest_poff = destoff & (PAGE_SIZE - 1);
 -		cmp_len = min(PAGE_SIZE - src_poff,
 -			      PAGE_SIZE - dest_poff);
 -		cmp_len = min(cmp_len, len);
 -		if (cmp_len <= 0)
 -			goto out_error;
 -
 -		src_page = vfs_dedupe_get_page(src, srcoff);
 -		if (IS_ERR(src_page)) {
 -			error = PTR_ERR(src_page);
 -			goto out_error;
 -		}
 -		dest_page = vfs_dedupe_get_page(dest, destoff);
 -		if (IS_ERR(dest_page)) {
 -			error = PTR_ERR(dest_page);
 -			unlock_page(src_page);
 -			put_page(src_page);
 -			goto out_error;
 -		}
 -		src_addr = kmap_atomic(src_page);
 -		dest_addr = kmap_atomic(dest_page);
 -
 -		flush_dcache_page(src_page);
 -		flush_dcache_page(dest_page);
 -
 -		if (memcmp(src_addr + src_poff, dest_addr + dest_poff, cmp_len))
 -			same = false;
 -
 -		kunmap_atomic(dest_addr);
 -		kunmap_atomic(src_addr);
 -		unlock_page(dest_page);
 -		unlock_page(src_page);
 -		put_page(dest_page);
 -		put_page(src_page);
 -
 -		if (!same)
 -			break;
 -
 -		srcoff += cmp_len;
 -		destoff += cmp_len;
 -		len -= cmp_len;
 -	}
 -
 -	*is_same = same;
 -	return 0;
 -
 -out_error:
 -	return error;
 -}
 -EXPORT_SYMBOL(vfs_dedupe_file_range_compare);
 -
+ /* Check whether we are allowed to dedupe the destination file */
+ static bool allow_file_dedupe(struct file *file)
+ {
+ 	if (capable(CAP_SYS_ADMIN))
+ 		return true;
+ 	if (file->f_mode & FMODE_WRITE)
+ 		return true;
+ 	if (uid_eq(current_fsuid(), file_inode(file)->i_uid))
+ 		return true;
+ 	if (!inode_permission(file_inode(file), MAY_WRITE))
+ 		return true;
+ 	return false;
+ }
+ 
 -int vfs_dedupe_file_range_one(struct file *src_file, loff_t src_pos,
 -			      struct file *dst_file, loff_t dst_pos, u64 len)
 +loff_t 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)
  {
 -	s64 ret;
 +	loff_t ret;
 +
 +	WARN_ON_ONCE(remap_flags & ~(REMAP_FILE_DEDUP |
 +				     REMAP_FILE_CAN_SHORTEN));
  
  	ret = mnt_want_write_file(dst_file);
  	if (ret)