git: 84235bcd4bde - stable/14 - inotify: Avoid resetting the cookie

Mark Johnston <[email protected]>
Newsgroups gmane.os.freebsd.devel.cvs.src
Message-ID <6a7353e1.45e3a.297c5958__19574.3389822141$1785943292$gmane$org@gitrepo.freebsd.org>
The branch stable/14 has been updated by markj:

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

commit 84235bcd4bde587c134820763c2880fd8e653717
Author:     Mark Johnston <[email protected]>
AuthorDate: 2025-12-21 23:49:26 +0000
Commit:     Mark Johnston <[email protected]>
CommitDate: 2026-08-05 15:15:16 +0000

    inotify: Avoid resetting the cookie
    
    The IN_MOVED_FROM and _TO events only apply to names in a watched
    directory, never to a watched directory itself.  So, the cookie value
    there is always zero, and in particular we should not reset the
    caller-provided cookie value, as it may be used later.
    
    Add a regression test.
    
    Reported by:    arrowd
    MFC after:      1 week
    
    (cherry picked from commit da8ab13249420e85935b89794f333f0755e56385)
---
 sys/kern/vfs_inotify.c        |  7 ++-----
 tests/sys/kern/inotify_test.c | 31 +++++++++++++++++++++++++++++++
 2 files changed, 33 insertions(+), 5 deletions(-)

diff --git a/sys/kern/vfs_inotify.c b/sys/kern/vfs_inotify.c
index 6b82d5e9ab96..e003759348a3 100644
--- a/sys/kern/vfs_inotify.c
+++ b/sys/kern/vfs_inotify.c
@@ -719,7 +719,6 @@ vn_inotify(struct vnode *vp, struct vnode *dvp, struct componentname *cnp,
 				}
 				break;
 			case IN_MOVED_FROM:
-				cookie = 0;
 				selfevent = IN_MOVE_SELF;
 				break;
 			case _IN_ATTRIB_LINKCOUNT:
@@ -730,10 +729,8 @@ vn_inotify(struct vnode *vp, struct vnode *dvp, struct componentname *cnp,
 				break;
 			}
 
-			if ((selfevent & ~_IN_DIR_EVENTS) != 0) {
-				inotify_log(vp, NULL, 0, selfevent | isdir,
-				    cookie);
-			}
+			if ((selfevent & ~_IN_DIR_EVENTS) != 0)
+				inotify_log(vp, NULL, 0, selfevent | isdir, 0);
 		}
 
 		/*
diff --git a/tests/sys/kern/inotify_test.c b/tests/sys/kern/inotify_test.c
index 968c0f83fc8b..b4fa21862063 100644
--- a/tests/sys/kern/inotify_test.c
+++ b/tests/sys/kern/inotify_test.c
@@ -908,6 +908,36 @@ ATF_TC_BODY(inotify_event_move, tc)
 	close_inotify(ifd);
 }
 
+ATF_TC_WITHOUT_HEAD(inotify_event_move_dir);
+ATF_TC_BODY(inotify_event_move_dir, tc)
+{
+	char dir[PATH_MAX], subdir1[PATH_MAX], subdir2[PATH_MAX];
+	uint32_t cookie1, cookie2;
+	int error, ifd, wd1, wd2;
+
+	ifd = inotify(IN_NONBLOCK);
+
+	wd1 = watch_dir(ifd, IN_MOVE, dir);
+	snprintf(subdir1, sizeof(subdir1), "%s/subdir", dir);
+	error = mkdir(subdir1, 0755);
+	ATF_REQUIRE(error == 0);
+	wd2 = inotify_add_watch(ifd, subdir1, IN_MOVE);
+	ATF_REQUIRE(wd2 != -1);
+
+	snprintf(subdir2, sizeof(subdir2), "%s/newsubdir", dir);
+	error = rename(subdir1, subdir2);
+	ATF_REQUIRE(error == 0);
+
+	cookie1 = consume_event_cookie(ifd, wd1, IN_MOVED_FROM, IN_ISDIR,
+	    "subdir");
+	cookie2 = consume_event_cookie(ifd, wd1, IN_MOVED_TO, IN_ISDIR,
+	    "newsubdir");
+	ATF_REQUIRE_MSG(cookie1 == cookie2,
+	    "expected cookie %u, got %u", cookie1, cookie2);
+
+	close_inotify(ifd);
+}
+
 ATF_TC_WITHOUT_HEAD(inotify_event_open);
 ATF_TC_BODY(inotify_event_open, tc)
 {
@@ -1007,6 +1037,7 @@ ATF_TP_ADD_TCS(tp)
 	ATF_TP_ADD_TC(tp, inotify_event_create);
 	ATF_TP_ADD_TC(tp, inotify_event_delete);
 	ATF_TP_ADD_TC(tp, inotify_event_move);
+	ATF_TP_ADD_TC(tp, inotify_event_move_dir);
 	ATF_TP_ADD_TC(tp, inotify_event_open);
 	ATF_TP_ADD_TC(tp, inotify_event_unmount);
 	return (atf_no_error());
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.