svn commit: r354564 - head/sys/kern

Rick Macklem rmacklem at FreeBSD.org
Fri Nov 8 23:39:18 UTC 2019


Author: rmacklem
Date: Fri Nov  8 23:39:17 2019
New Revision: 354564
URL: https://svnweb.freebsd.org/changeset/base/354564

Log:
  Update copy_file_range(2) to be Linux5 compatible.
  
  The current linux man page and testing done on a fairly recent linux5.n
  kernel have identified two changes to the semantics of the linux
  copy_file_range system call.
  Since the copy_file_range(2) system call is intended to be linux compatible
  and is only currently in head/current and not used by any commands,
  it seems appropriate to update the system call to be compatible with
  the current linux one.
  The old linux man page stated that, if the
  offset + len exceeded file_size for the input file, EINVAL should be returned.
  Now, the semantics is to copy up to at most file_size bytes and return that
  number of bytes copied. If the offset is at or beyond file_size, a return
  of 0 bytes is done.
  This patch modifies copy_file_range(2) to be linux compatible for this
  semantic change.
  A separate patch will change copy_file_range(2) for the other semantic
  change, which allows the infd and outfd to refer to the same file, so
  long as the byte ranges do not overlap.

Modified:
  head/sys/kern/vfs_vnops.c

Modified: head/sys/kern/vfs_vnops.c
==============================================================================
--- head/sys/kern/vfs_vnops.c	Fri Nov  8 20:53:56 2019	(r354563)
+++ head/sys/kern/vfs_vnops.c	Fri Nov  8 23:39:17 2019	(r354564)
@@ -2679,7 +2679,6 @@ vn_copy_file_range(struct vnode *invp, off_t *inoffp, 
     off_t *outoffp, size_t *lenp, unsigned int flags, struct ucred *incred,
     struct ucred *outcred, struct thread *fsize_td)
 {
-	struct vattr va;
 	int error;
 	size_t len;
 	uint64_t uvalin, uvalout;
@@ -2705,17 +2704,6 @@ vn_copy_file_range(struct vnode *invp, off_t *inoffp, 
 	if (error != 0)
 		goto out;
 
-	error = vn_lock(invp, LK_SHARED);
-	if (error != 0)
-		goto out;
-	/* Check that the offset + len does not go past EOF of invp. */
-	error = VOP_GETATTR(invp, &va, incred);
-	if (error == 0 && va.va_size < *inoffp + len)
-		error = EINVAL;
-	VOP_UNLOCK(invp, 0);
-	if (error != 0)
-		goto out;
-
 	/*
 	 * If the two vnode are for the same file system, call
 	 * VOP_COPY_FILE_RANGE(), otherwise call vn_generic_copy_file_range()
@@ -2917,7 +2905,7 @@ vn_generic_copy_file_range(struct vnode *invp, off_t *
 	off_t startoff, endoff, xfer, xfer2;
 	u_long blksize;
 	int error;
-	bool cantseek, readzeros;
+	bool cantseek, readzeros, eof, lastblock;
 	ssize_t aresid;
 	size_t copylen, len, savlen;
 	char *dat;
@@ -3004,7 +2992,8 @@ vn_generic_copy_file_range(struct vnode *invp, off_t *
 	 * Note that some file systems such as NFSv3, NFSv4.0 and NFSv4.1 may
 	 * support holes on the server, but do not support FIOSEEKHOLE.
 	 */
-	while (len > 0 && error == 0) {
+	eof = false;
+	while (len > 0 && error == 0 && !eof) {
 		endoff = 0;			/* To shut up compilers. */
 		cantseek = true;
 		startoff = *inoffp;
@@ -3086,7 +3075,7 @@ vn_generic_copy_file_range(struct vnode *invp, off_t *
 			xfer -= (*inoffp % blksize);
 		}
 		/* Loop copying the data block. */
-		while (copylen > 0 && error == 0) {
+		while (copylen > 0 && error == 0 && !eof) {
 			if (copylen < xfer)
 				xfer = copylen;
 			error = vn_lock(invp, LK_SHARED);
@@ -3097,12 +3086,13 @@ vn_generic_copy_file_range(struct vnode *invp, off_t *
 			    curthread->td_ucred, incred, &aresid,
 			    curthread);
 			VOP_UNLOCK(invp, 0);
-			/*
-			 * Linux considers a range that exceeds EOF to
-			 * be an error, so we will too.
-			 */
-			if (error == 0 && aresid > 0)
-				error = EINVAL;
+			lastblock = false;
+			if (error == 0 && aresid > 0) {
+				/* Stop the copy at EOF on the input file. */
+				xfer -= aresid;
+				eof = true;
+				lastblock = true;
+			}
 			if (error == 0) {
 				/*
 				 * Skip the write for holes past the initial EOF
@@ -3111,11 +3101,13 @@ vn_generic_copy_file_range(struct vnode *invp, off_t *
 				 */
 				readzeros = cantseek ? mem_iszero(dat, xfer) :
 				    false;
+				if (xfer == len)
+					lastblock = true;
 				if (!cantseek || *outoffp < va.va_size ||
-				    xfer == len || !readzeros)
+				    lastblock || !readzeros)
 					error = vn_write_outvp(outvp, dat,
 					    *outoffp, xfer, blksize,
-					    readzeros && xfer == len &&
+					    readzeros && lastblock &&
 					    *outoffp >= va.va_size, false,
 					    outcred);
 				if (error == 0) {


More information about the svn-src-head mailing list