git: 828b4f1476c2 - stable/14 - zfsctl_root_readdir: if there were no dirents to copy out, return EINVAL

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Mon, 25 Aug 2025 08:57:18 UTC
The branch stable/14 has been updated by kib:

URL: https://cgit.FreeBSD.org/src/commit/?id=828b4f1476c24ee3026d7944646363c49484d9f6

commit 828b4f1476c24ee3026d7944646363c49484d9f6
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2025-08-16 02:40:17 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2025-08-25 08:42:16 +0000

    zfsctl_root_readdir: if there were no dirents to copy out, return EINVAL
    
    (cherry picked from commit cead6157cc1b748df29b32072f492d4f6afae65a)
---
 .../openzfs/module/os/freebsd/zfs/zfs_ctldir.c     | 40 ++++++++++++++--------
 1 file changed, 26 insertions(+), 14 deletions(-)

diff --git a/sys/contrib/openzfs/module/os/freebsd/zfs/zfs_ctldir.c b/sys/contrib/openzfs/module/os/freebsd/zfs/zfs_ctldir.c
index 3f955b296f1e..0c0112c29ae7 100644
--- a/sys/contrib/openzfs/module/os/freebsd/zfs/zfs_ctldir.c
+++ b/sys/contrib/openzfs/module/os/freebsd/zfs/zfs_ctldir.c
@@ -673,6 +673,7 @@ zfsctl_root_readdir(struct vop_readdir_args *ap)
 	zfs_uio_t uio;
 	int *eofp = ap->a_eofflag;
 	off_t dots_offset;
+	ssize_t orig_resid;
 	int error;
 
 	zfs_uio_init(&uio, ap->a_uio);
@@ -692,13 +693,11 @@ zfsctl_root_readdir(struct vop_readdir_args *ap)
 		return (0);
 	}
 
+	orig_resid = zfs_uio_resid(&uio);
 	error = sfs_readdir_common(zfsvfs->z_root, ZFSCTL_INO_ROOT, ap, &uio,
 	    &dots_offset);
-	if (error != 0) {
-		if (error == ENAMETOOLONG) /* ran out of destination space */
-			error = 0;
-		return (error);
-	}
+	if (error != 0)
+		goto err;
 	if (zfs_uio_offset(&uio) != dots_offset)
 		return (SET_ERROR(EINVAL));
 
@@ -711,8 +710,11 @@ zfsctl_root_readdir(struct vop_readdir_args *ap)
 	entry.d_reclen = sizeof (entry);
 	error = vfs_read_dirent(ap, &entry, zfs_uio_offset(&uio));
 	if (error != 0) {
-		if (error == ENAMETOOLONG)
-			error = 0;
+err:
+		if (error == ENAMETOOLONG) {
+			error = orig_resid == zfs_uio_resid(&uio) ?
+			    EINVAL : 0;
+		}
 		return (SET_ERROR(error));
 	}
 	if (eofp != NULL)
@@ -1057,17 +1059,21 @@ zfsctl_snapdir_readdir(struct vop_readdir_args *ap)
 	zfs_uio_t uio;
 	int *eofp = ap->a_eofflag;
 	off_t dots_offset;
+	ssize_t orig_resid;
 	int error;
 
 	zfs_uio_init(&uio, ap->a_uio);
+	orig_resid = zfs_uio_resid(&uio);
 
 	ASSERT3S(vp->v_type, ==, VDIR);
 
 	error = sfs_readdir_common(ZFSCTL_INO_ROOT, ZFSCTL_INO_SNAPDIR, ap,
 	    &uio, &dots_offset);
 	if (error != 0) {
-		if (error == ENAMETOOLONG) /* ran out of destination space */
-			error = 0;
+		if (error == ENAMETOOLONG) { /* ran out of destination space */
+			error = orig_resid == zfs_uio_resid(&uio) ?
+			    EINVAL : 0;
+		}
 		return (error);
 	}
 
@@ -1085,9 +1091,13 @@ zfsctl_snapdir_readdir(struct vop_readdir_args *ap)
 		dsl_pool_config_exit(dmu_objset_pool(zfsvfs->z_os), FTAG);
 		if (error != 0) {
 			if (error == ENOENT) {
-				if (eofp != NULL)
-					*eofp = 1;
-				error = 0;
+				if (orig_resid == zfs_uio_resid(&uio)) {
+					error = EINVAL;
+				} else {
+					error = 0;
+					if (eofp != NULL)
+						*eofp = 1;
+				}
 			}
 			zfs_exit(zfsvfs, FTAG);
 			return (error);
@@ -1100,8 +1110,10 @@ zfsctl_snapdir_readdir(struct vop_readdir_args *ap)
 		entry.d_reclen = sizeof (entry);
 		error = vfs_read_dirent(ap, &entry, zfs_uio_offset(&uio));
 		if (error != 0) {
-			if (error == ENAMETOOLONG)
-				error = 0;
+			if (error == ENAMETOOLONG) {
+				error = orig_resid == zfs_uio_resid(&uio) ?
+				    EINVAL : 0;
+			}
 			zfs_exit(zfsvfs, FTAG);
 			return (SET_ERROR(error));
 		}