git: de8042811810 - main - inotify: Open target the directory when adding a watch

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Mon, 19 Oct 2026 15:29:28 UTC
The branch main has been updated by markj:

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

commit de80428118101624aaeaae02d59665d1027ff10e
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2026-10-19 13:44:41 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2026-10-19 15:29:13 +0000

    inotify: Open target the directory when adding a watch
    
    When an application uses inotify to watch a directory,
    vn_inotify_add_watch() reads the directory entries and does a name
    lookup so as to induce the filesystem to create namecache entries.
    Some filesystems expect the directory to be opened before VOP_READDIR
    can be called, so do that.
    
    PR:             299239
    Reviewed by:    kib
    MFC after:      1 week
    Differential Revision:  https://reviews.freebsd.org/D60782
---
 sys/kern/vfs_inotify.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/sys/kern/vfs_inotify.c b/sys/kern/vfs_inotify.c
index 2cf13a7485eb..13555c2692f9 100644
--- a/sys/kern/vfs_inotify.c
+++ b/sys/kern/vfs_inotify.c
@@ -785,6 +785,14 @@ vn_inotify_add_watch(struct vnode *vp, struct inotify_softc *sc, uint32_t mask,
 		size_t buflen, len;
 		int eof, error;
 
+		/*
+		 * Some filesystems, at least fuse, require that the directory
+		 * be opened before reading it.
+		 */
+		error = VOP_OPEN(vp, FREAD, td->td_ucred, td, NULL);
+		if (error != 0)
+			return (error);
+
 		buflen = 128 * sizeof(struct dirent);
 		buf = malloc(buflen, M_TEMP, M_WAITOK);
 
@@ -821,6 +829,7 @@ vn_inotify_add_watch(struct vnode *vp, struct inotify_softc *sc, uint32_t mask,
 			vrele(nd.ni_vp);
 		}
 		free(buf, M_TEMP);
+		VOP_CLOSE(vp, FREAD, td->td_ucred, td);
 		if (error != 0)
 			return (error);
 	}