[LTP] [PATCH v2] fanotify13: fix test failure when running iterations

AnonymeMeow anonymemeow@gmail.com
Wed Jun 3 03:58:41 CEST 2026


The FAN_DELETE_SELF test case removes test files, causing later
iterations to crash. Recreate the removed files after the test to
restore the expected initial state. And adjust the overlayfs mount
timing to avoid creating files directly in the filesystem underneath
the already mounted overlayfs.

Signed-off-by: AnonymeMeow <anonymemeow@gmail.com>
---

Hi, Amir,

I tested your suggested solution, but the overlayfs mount failed with:

fanotify13.c:165: TBROK: overlayfs mount failed: ESTALE (116)

So I kept clean_upper_dir() in restore_objects() to restore the expected
initial state.

With Best Regards,
AnonymeMeow

---
 .../kernel/syscalls/fanotify/fanotify13.c     | 49 ++++++++++++++++---
 1 file changed, 43 insertions(+), 6 deletions(-)

diff --git a/testcases/kernel/syscalls/fanotify/fanotify13.c b/testcases/kernel/syscalls/fanotify/fanotify13.c
index 76d40eaf7..182858470 100644
--- a/testcases/kernel/syscalls/fanotify/fanotify13.c
+++ b/testcases/kernel/syscalls/fanotify/fanotify13.c
@@ -53,6 +53,9 @@
 #define FILE_PATH_ONE MOUNT_PATH"/"FILE_ONE
 #define FILE_PATH_TWO MOUNT_PATH"/"FILE_TWO
 
+#define TST_VARIANT_OVL_LOWER (tst_variant & 1)
+#define TST_VARIANT_OVL_WATCH (tst_variant > 2)
+
 #if defined(HAVE_NAME_TO_HANDLE_AT)
 struct event_t {
 	unsigned long long expected_mask;
@@ -137,6 +140,22 @@ static void delete_objects(void)
 	}
 }
 
+static void clean_upper_dir(void)
+{
+	unsigned int i;
+
+	SAFE_UMOUNT(MOUNT_PATH);
+	SAFE_UMOUNT(OVL_MNT);
+
+	SAFE_MOUNT(OVL_UPPER, MOUNT_PATH, "none", MS_BIND, NULL);
+	for (i = 0; i < ARRAY_SIZE(objects); i++)
+		SAFE_UNLINK(objects[i].path);
+	SAFE_UMOUNT(MOUNT_PATH);
+
+	SAFE_MOUNT_OVERLAY();
+	SAFE_MOUNT(OVL_MNT, MOUNT_PATH, "none", MS_BIND, NULL);
+}
+
 static void get_object_stats(void)
 {
 	unsigned int i;
@@ -147,6 +166,17 @@ static void get_object_stats(void)
 	}
 }
 
+static void restore_objects(void)
+{
+	if (TST_VARIANT_OVL_LOWER)
+		clean_upper_dir();
+	else
+		create_objects();
+
+	/* Get the re-created object ids */
+	get_object_stats();
+}
+
 static int setup_marks(unsigned int fd, struct test_case_t *tc)
 {
 	unsigned int i;
@@ -340,6 +370,10 @@ static void do_test(unsigned int number)
 			"Did not get an expected event (expected: %llx)",
 			event_set[i].expected_mask);
 	}
+
+	/* Restore to state before delete_objects() */
+	if (tc->mask & FAN_DELETE_SELF)
+		restore_objects();
 out:
 	SAFE_CLOSE(fanotify_fd);
 }
@@ -367,14 +401,11 @@ static void do_setup(void)
 	 */
 	if (tst_variant) {
 		REQUIRE_HANDLE_TYPE_SUPPORTED_BY_KERNEL(AT_HANDLE_FID);
-		ovl_mounted = TST_MOUNT_OVERLAY();
-		if (!ovl_mounted)
-			return;
+		tst_create_overlay_dirs();
 
-		mnt = tst_variant & 1 ? OVL_LOWER : OVL_UPPER;
+		mnt = TST_VARIANT_OVL_LOWER ? OVL_LOWER : OVL_UPPER;
 	} else {
 		mnt = OVL_BASE_MNTPOINT;
-
 	}
 	REQUIRE_FANOTIFY_INIT_FLAGS_SUPPORTED_ON_FS(FAN_REPORT_FID, mnt);
 	SAFE_MKDIR(MOUNT_PATH, 0755);
@@ -385,8 +416,14 @@ static void do_setup(void)
 
 	/* Create file and directory objects for testing on base fs */
 	create_objects();
+	
+	if (tst_variant) {
+		ovl_mounted = TST_MOUNT_OVERLAY();
+		if (!ovl_mounted)
+			return;
+	}
 
-	if (tst_variant > 2) {
+	if (TST_VARIANT_OVL_WATCH) {
 		/* Setup watches on overlayfs */
 		SAFE_MOUNT(OVL_MNT, MOUNT_PATH, "none", MS_BIND, NULL);
 		ovl_bind_mounted = 1;
-- 
2.54.0



More information about the ltp mailing list