All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v1] erofs-utils: tests: add test for fanotify FAN_PRE_ACCESS event
@ 2026-10-30  9:17 Friendy Su
  2026-10-30  9:22 ` Friendy.Su
  0 siblings, 1 reply; 5+ messages in thread
From: Friendy Su @ 2026-10-30  9:17 UTC (permalink / raw)
  To: xiang, bluce.lee, jnhuang95, hudsonzhu, zhaoyifan28
  Cc: linux-erofs, Friendy Su, Daniel Palmer, Yuezhang Mo

SB_I_ALLOW_HSM is set in kernel for erofs. Add a test to verify 2 cases below.

case 1: with bdev-based device, FAN_PRE_ACCESS event is triggered.

 # SCRATCH_DEV=/dev/pmem1 make check TESTS=erofs/033

PASS: erofs/033
=============
1 test passed
=============

case 2: with file-backed image, FAN_PRE_ACCESS event fails to mark.

 # SCRATCH_DEV=erofs.img make check TESTS=erofs/033

fs-monitor not started
PASS: erofs/033
=============
1 test passed
=============

Signed-off-by: Friendy Su <friendy.su@sony.com>
Reviewed-by: Daniel Palmer <daniel.palmer@sony.com>
Reviewed-by: Yuezhang Mo <Yuezhang.Mo@sony.com>
---
 tests/erofs/033        |  85 +++++++++++++++++++++++
 tests/erofs/033.out    |   2 +
 tests/src/Makefile.am  |   6 +-
 tests/src/fs-monitor.c | 151 +++++++++++++++++++++++++++++++++++++++++
 4 files changed, 243 insertions(+), 1 deletion(-)
 create mode 100755 tests/erofs/033
 create mode 100644 tests/erofs/033.out
 create mode 100644 tests/src/fs-monitor.c

diff --git a/tests/erofs/033 b/tests/erofs/033
new file mode 100755
index 0000000..fab6d56
--- /dev/null
+++ b/tests/erofs/033
@@ -0,0 +1,85 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0+
+#
+# 033 - check FAN_PRE_ACCESS fanotify event
+#
+seq=`basename $0`
+seqres=$RESULT_DIR/$(echo $0 | awk '{print $((NF-1))"/"$NF}' FS="/")
+
+# get standard environment, filters and checks
+. "${srcdir}/common/rc"
+
+cleanup()
+{
+	cd /
+	rm -rf $tmp.*
+}
+
+_require_erofs
+
+# remove previous $seqres.full before test
+rm -f $seqres.full
+
+# real QA test starts here
+echo "QA output created by $seq"
+
+if [ -z $SCRATCH_DEV ]; then
+	SCRATCH_DEV=$tmp/erofs_$seq.img
+	rm -f SCRATCH_DEV
+fi
+
+localdir="$tmp/$seq"
+rm -rf $localdir
+mkdir -p $localdir
+
+# create a file for mmap()
+dd if=/dev/urandom of=$localdir/file-2M bs=2M count=1 >> $seqre.full 2>&1
+
+MKFS_OPTIONS="$MKFS_OPTIONS -x1"
+_scratch_mkfs $localdir >> $seqres.full 2>&1 || _fail "failed to mkfs"
+
+# _scratch_mount always mount with '-o loop', file backed mount
+# can not be tested. So mount with ${MOUNT_PROG} directly.
+#_scratch_mount 2>>$seqres.full
+${MOUNT_PROG} ${SCRATCH_DEV} ${SCRATCH_MNT} 2>>$seqres.full
+
+# start fanotify event monitor
+src/fs-monitor ${SCRATCH_MNT} > $tmp.fsmonitor 2>&1 &
+
+fsmonitor_pid=$!
+sleep 1
+
+ps -p ${fsmonitor_pid} > /dev/null
+
+if [ $? -ne 0 ]; then
+	echo "fs-monitor not started" | tee -a $seqres.full >&2
+	if [ ! -f ${SCRATCH_DEV} ]; then
+		echo "fanotify monitor failed to start on non-file-backed image"
+	fi
+	${UMOUNT_PROG} ${SCRATCH_MNT}
+	echo Silence is golden
+	status=0
+	exit 0
+fi
+
+echo "fs-monitor started" | tee -a $seqres.full >&2
+
+# do mmap(), FAN_PRE_ACCESS event will be triggered
+xfs_io -r -c "mmap -r 0 2M" -c close ${SCRATCH_MNT}/file-2M >> $seqres.full
+
+sleep 1
+
+# kill fs-monitor process anyway
+kill -TERM ${fsmonitor_pid}
+
+cat $tmp.fsmonitor >>$seqres.full
+
+(cat $tmp.fsmonitor | grep -q "FAN_PRE_ACCESS EVENT" > /dev/null) || \
+	echo "No FAN_PRE_ACCESS EVENT"
+
+#_scratch_unmount
+${UMOUNT_PROG} ${SCRATCH_MNT}
+
+echo Silence is golden
+status=0
+exit 0
diff --git a/tests/erofs/033.out b/tests/erofs/033.out
new file mode 100644
index 0000000..c5d2914
--- /dev/null
+++ b/tests/erofs/033.out
@@ -0,0 +1,2 @@
+QA output created by 033
+Silence is golden
diff --git a/tests/src/Makefile.am b/tests/src/Makefile.am
index 16de41a..d645539 100644
--- a/tests/src/Makefile.am
+++ b/tests/src/Makefile.am
@@ -3,7 +3,7 @@
 
 AUTOMAKE_OPTIONS	= foreign
 check_PROGRAMS		=
-noinst_PROGRAMS		= fssum
+noinst_PROGRAMS		= fssum fs-monitor
 
 fssum_CFLAGS = -Wall -I$(top_srcdir)/include
 fssum_LDADD = $(top_builddir)/lib/liberofs.la
@@ -15,3 +15,7 @@ badlz4_SOURCES = badlz4.c
 badlz4_CFLAGS = ${liblz4_CFLAGS}
 badlz4_LDADD = ${liblz4_LIBS}
 endif
+
+check_PROGRAMS += fs-monitor
+fs_monitor_CFLAGS = -Wall -I$(top_srcdir)/include
+fs_monitor_SOURCES = fs-monitor.c
diff --git a/tests/src/fs-monitor.c b/tests/src/fs-monitor.c
new file mode 100644
index 0000000..07b7a2a
--- /dev/null
+++ b/tests/src/fs-monitor.c
@@ -0,0 +1,151 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Partly copied from https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/samples/fanotify/fs-monitor.c
+ */
+
+#include <errno.h>
+#include <err.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <sys/fanotify.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <poll.h>
+
+static void handle_pre_access_event(int fd,
+	       struct fanotify_event_metadata *event)
+{
+	struct fanotify_event_info_header *info;
+	struct fanotify_event_info_range *range;
+	struct fanotify_response response;
+	int off;
+
+
+	for (off = sizeof(*event) ; off < event->event_len;
+	     off += info->len) {
+		info = (struct fanotify_event_info_header *)
+			((char *) event + off);
+
+		switch (info->info_type) {
+		case FAN_EVENT_INFO_TYPE_RANGE:
+			range = (struct fanotify_event_info_range *) info;
+
+			printf("FAN_PRE_ACCESS event range: \n");
+			printf("offset = 0x%llx, count = 0x%llx\n",
+					range->offset, range->count);
+
+			response.fd = event->fd;
+                        response.response = FAN_ALLOW;
+			if (write(fd, &response, sizeof(response)) < 0)
+					perror("Send FAN_PRE_ACCESS response failed");
+
+			break;
+		default:
+			printf("\tUnknown info type=%d len=%d:\n",
+			       info->info_type, info->len);
+			break;
+		}
+
+		fflush(stdout);
+	}
+}
+
+static void handle_notifications(int fd, char *buffer, int len)
+{
+	struct fanotify_event_metadata *event =
+		(struct fanotify_event_metadata *) buffer;
+
+	for (; FAN_EVENT_OK(event, len); event = FAN_EVENT_NEXT(event, len)) {
+
+		if (event->mask == FAN_PRE_ACCESS) {
+
+			/* This msg will be checked in test case */
+			printf("FAN_PRE_ACCESS EVENT (len=%d)\n", event->event_len);
+
+			handle_pre_access_event(fd, event);
+		}
+
+		/* TODO: Add more event handler here */
+
+		printf("---\n\n");
+		fflush(stdout);
+	}
+
+}
+
+int main(int argc, char **argv)
+{
+	int fd, poll_num, opt, nfds;
+	int check = 0;
+	char buffer[BUFSIZ];
+	struct pollfd fds[1];
+
+	if (argc < 2) {
+		printf("Missing path argument\n");
+		return 1;
+	}
+
+	while ((opt = getopt(argc, argv, "c")) != -1) {
+		switch (opt) {
+		case 'c':
+			check = 1;
+			break;
+		default:
+			fprintf(stderr, "Usage: %s [-c] path\n",
+				argv[0]);
+			exit(EXIT_FAILURE);
+		}
+	}
+
+	fd = fanotify_init(FAN_CLOEXEC | FAN_CLASS_PRE_CONTENT | FAN_NONBLOCK,
+                        O_RDONLY);
+	if (check)
+		exit(fd < 0);
+
+	if (fd < 0) {
+		perror("fanotify_init");
+		errx(1, "fanotify_init");
+	}
+
+	if (fanotify_mark(fd, FAN_MARK_ADD | FAN_MARK_FILESYSTEM,
+                          FAN_PRE_ACCESS, AT_FDCWD, argv[optind])) {
+		perror("fanotify_mark");
+		errx(1, "fanotify_mark");
+	}
+
+	printf("fanotify active\n");
+	fflush(stdout);
+
+	fds[0].fd = fd;
+        fds[0].events = POLLIN;
+
+	nfds = 1;
+
+	while (1) {
+	        poll_num = poll(fds, nfds, -1);
+                if (poll_num == -1) {
+                        if (errno == EINTR)	/* Interrupted by a signal */
+                                continue;	/* Restart poll() */
+
+                        perror("poll");		/* Unexpected error */
+                        exit(EXIT_FAILURE);
+                }
+
+		if (poll_num > 0) {
+
+                        if (fds[0].revents & POLLIN) {
+		                int n = read(fd, buffer, sizeof(buffer));
+		                if (n == -1 && errno != EAGAIN) {
+					perror("read");
+					exit(EXIT_FAILURE);
+				}
+
+				handle_notifications(fd, buffer, n);
+			}
+		}
+	}
+
+	return 0;
+}
-- 
2.34.1



^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v1] erofs-utils: tests: add test for fanotify FAN_PRE_ACCESS event
  2026-10-30  9:17 [PATCH v1] erofs-utils: tests: add test for fanotify FAN_PRE_ACCESS event Friendy Su
@ 2026-10-30  9:22 ` Friendy.Su
  2026-11-18 11:35   ` Friendy.Su
  0 siblings, 1 reply; 5+ messages in thread
From: Friendy.Su @ 2026-10-30  9:22 UTC (permalink / raw)
  To: xiang@kernel.org, bluce.lee@aliyun.com, jnhuang95@gmail.com,
	hudsonzhu@tencent.com, zhaoyifan28@huawei.com
  Cc: linux-erofs@lists.ozlabs.org, Daniel.Palmer@sony.com,
	Yuezhang.Mo@sony.com


This is the test for patch "erofs: set SB_I_ALLOW_HSM for bdev-based mounts" in kernel.

________________________________________
From: Su, Friendy <Friendy.Su@sony.com>
Sent: Friday, October 30, 2026 17:17
To: xiang@kernel.org; bluce.lee@aliyun.com; jnhuang95@gmail.com; hudsonzhu@tencent.com; zhaoyifan28@huawei.com
Cc: linux-erofs@lists.ozlabs.org; Su, Friendy; Palmer, Daniel (SGC); Mo, Yuezhang
Subject: [PATCH v1] erofs-utils: tests: add test for fanotify FAN_PRE_ACCESS event

SB_I_ALLOW_HSM is set in kernel for erofs. Add a test to verify 2 cases below.

case 1: with bdev-based device, FAN_PRE_ACCESS event is triggered.

 # SCRATCH_DEV=/dev/pmem1 make check TESTS=erofs/033

PASS: erofs/033
=============
1 test passed
=============

case 2: with file-backed image, FAN_PRE_ACCESS event fails to mark.

 # SCRATCH_DEV=erofs.img make check TESTS=erofs/033

fs-monitor not started
PASS: erofs/033
=============
1 test passed
=============

Signed-off-by: Friendy Su <friendy.su@sony.com>
Reviewed-by: Daniel Palmer <daniel.palmer@sony.com>
Reviewed-by: Yuezhang Mo <Yuezhang.Mo@sony.com>
---
 tests/erofs/033        |  85 +++++++++++++++++++++++
 tests/erofs/033.out    |   2 +
 tests/src/Makefile.am  |   6 +-
 tests/src/fs-monitor.c | 151 +++++++++++++++++++++++++++++++++++++++++
 4 files changed, 243 insertions(+), 1 deletion(-)
 create mode 100755 tests/erofs/033
 create mode 100644 tests/erofs/033.out
 create mode 100644 tests/src/fs-monitor.c

diff --git a/tests/erofs/033 b/tests/erofs/033
new file mode 100755
index 0000000..fab6d56
--- /dev/null
+++ b/tests/erofs/033
@@ -0,0 +1,85 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0+
+#
+# 033 - check FAN_PRE_ACCESS fanotify event
+#
+seq=`basename $0`
+seqres=$RESULT_DIR/$(echo $0 | awk '{print $((NF-1))"/"$NF}' FS="/")
+
+# get standard environment, filters and checks
+. "${srcdir}/common/rc"
+
+cleanup()
+{
+       cd /
+       rm -rf $tmp.*
+}
+
+_require_erofs
+
+# remove previous $seqres.full before test
+rm -f $seqres.full
+
+# real QA test starts here
+echo "QA output created by $seq"
+
+if [ -z $SCRATCH_DEV ]; then
+       SCRATCH_DEV=$tmp/erofs_$seq.img
+       rm -f SCRATCH_DEV
+fi
+
+localdir="$tmp/$seq"
+rm -rf $localdir
+mkdir -p $localdir
+
+# create a file for mmap()
+dd if=/dev/urandom of=$localdir/file-2M bs=2M count=1 >> $seqre.full 2>&1
+
+MKFS_OPTIONS="$MKFS_OPTIONS -x1"
+_scratch_mkfs $localdir >> $seqres.full 2>&1 || _fail "failed to mkfs"
+
+# _scratch_mount always mount with '-o loop', file backed mount
+# can not be tested. So mount with ${MOUNT_PROG} directly.
+#_scratch_mount 2>>$seqres.full
+${MOUNT_PROG} ${SCRATCH_DEV} ${SCRATCH_MNT} 2>>$seqres.full
+
+# start fanotify event monitor
+src/fs-monitor ${SCRATCH_MNT} > $tmp.fsmonitor 2>&1 &
+
+fsmonitor_pid=$!
+sleep 1
+
+ps -p ${fsmonitor_pid} > /dev/null
+
+if [ $? -ne 0 ]; then
+       echo "fs-monitor not started" | tee -a $seqres.full >&2
+       if [ ! -f ${SCRATCH_DEV} ]; then
+               echo "fanotify monitor failed to start on non-file-backed image"
+       fi
+       ${UMOUNT_PROG} ${SCRATCH_MNT}
+       echo Silence is golden
+       status=0
+       exit 0
+fi
+
+echo "fs-monitor started" | tee -a $seqres.full >&2
+
+# do mmap(), FAN_PRE_ACCESS event will be triggered
+xfs_io -r -c "mmap -r 0 2M" -c close ${SCRATCH_MNT}/file-2M >> $seqres.full
+
+sleep 1
+
+# kill fs-monitor process anyway
+kill -TERM ${fsmonitor_pid}
+
+cat $tmp.fsmonitor >>$seqres.full
+
+(cat $tmp.fsmonitor | grep -q "FAN_PRE_ACCESS EVENT" > /dev/null) || \
+       echo "No FAN_PRE_ACCESS EVENT"
+
+#_scratch_unmount
+${UMOUNT_PROG} ${SCRATCH_MNT}
+
+echo Silence is golden
+status=0
+exit 0
diff --git a/tests/erofs/033.out b/tests/erofs/033.out
new file mode 100644
index 0000000..c5d2914
--- /dev/null
+++ b/tests/erofs/033.out
@@ -0,0 +1,2 @@
+QA output created by 033
+Silence is golden
diff --git a/tests/src/Makefile.am b/tests/src/Makefile.am
index 16de41a..d645539 100644
--- a/tests/src/Makefile.am
+++ b/tests/src/Makefile.am
@@ -3,7 +3,7 @@

 AUTOMAKE_OPTIONS       = foreign
 check_PROGRAMS         =
-noinst_PROGRAMS                = fssum
+noinst_PROGRAMS                = fssum fs-monitor

 fssum_CFLAGS = -Wall -I$(top_srcdir)/include
 fssum_LDADD = $(top_builddir)/lib/liberofs.la
@@ -15,3 +15,7 @@ badlz4_SOURCES = badlz4.c
 badlz4_CFLAGS = ${liblz4_CFLAGS}
 badlz4_LDADD = ${liblz4_LIBS}
 endif
+
+check_PROGRAMS += fs-monitor
+fs_monitor_CFLAGS = -Wall -I$(top_srcdir)/include
+fs_monitor_SOURCES = fs-monitor.c
diff --git a/tests/src/fs-monitor.c b/tests/src/fs-monitor.c
new file mode 100644
index 0000000..07b7a2a
--- /dev/null
+++ b/tests/src/fs-monitor.c
@@ -0,0 +1,151 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Partly copied from https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/samples/fanotify/fs-monitor.c
+ */
+
+#include <errno.h>
+#include <err.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <sys/fanotify.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <poll.h>
+
+static void handle_pre_access_event(int fd,
+              struct fanotify_event_metadata *event)
+{
+       struct fanotify_event_info_header *info;
+       struct fanotify_event_info_range *range;
+       struct fanotify_response response;
+       int off;
+
+
+       for (off = sizeof(*event) ; off < event->event_len;
+            off += info->len) {
+               info = (struct fanotify_event_info_header *)
+                       ((char *) event + off);
+
+               switch (info->info_type) {
+               case FAN_EVENT_INFO_TYPE_RANGE:
+                       range = (struct fanotify_event_info_range *) info;
+
+                       printf("FAN_PRE_ACCESS event range: \n");
+                       printf("offset = 0x%llx, count = 0x%llx\n",
+                                       range->offset, range->count);
+
+                       response.fd = event->fd;
+                        response.response = FAN_ALLOW;
+                       if (write(fd, &response, sizeof(response)) < 0)
+                                       perror("Send FAN_PRE_ACCESS response failed");
+
+                       break;
+               default:
+                       printf("\tUnknown info type=%d len=%d:\n",
+                              info->info_type, info->len);
+                       break;
+               }
+
+               fflush(stdout);
+       }
+}
+
+static void handle_notifications(int fd, char *buffer, int len)
+{
+       struct fanotify_event_metadata *event =
+               (struct fanotify_event_metadata *) buffer;
+
+       for (; FAN_EVENT_OK(event, len); event = FAN_EVENT_NEXT(event, len)) {
+
+               if (event->mask == FAN_PRE_ACCESS) {
+
+                       /* This msg will be checked in test case */
+                       printf("FAN_PRE_ACCESS EVENT (len=%d)\n", event->event_len);
+
+                       handle_pre_access_event(fd, event);
+               }
+
+               /* TODO: Add more event handler here */
+
+               printf("---\n\n");
+               fflush(stdout);
+       }
+
+}
+
+int main(int argc, char **argv)
+{
+       int fd, poll_num, opt, nfds;
+       int check = 0;
+       char buffer[BUFSIZ];
+       struct pollfd fds[1];
+
+       if (argc < 2) {
+               printf("Missing path argument\n");
+               return 1;
+       }
+
+       while ((opt = getopt(argc, argv, "c")) != -1) {
+               switch (opt) {
+               case 'c':
+                       check = 1;
+                       break;
+               default:
+                       fprintf(stderr, "Usage: %s [-c] path\n",
+                               argv[0]);
+                       exit(EXIT_FAILURE);
+               }
+       }
+
+       fd = fanotify_init(FAN_CLOEXEC | FAN_CLASS_PRE_CONTENT | FAN_NONBLOCK,
+                        O_RDONLY);
+       if (check)
+               exit(fd < 0);
+
+       if (fd < 0) {
+               perror("fanotify_init");
+               errx(1, "fanotify_init");
+       }
+
+       if (fanotify_mark(fd, FAN_MARK_ADD | FAN_MARK_FILESYSTEM,
+                          FAN_PRE_ACCESS, AT_FDCWD, argv[optind])) {
+               perror("fanotify_mark");
+               errx(1, "fanotify_mark");
+       }
+
+       printf("fanotify active\n");
+       fflush(stdout);
+
+       fds[0].fd = fd;
+        fds[0].events = POLLIN;
+
+       nfds = 1;
+
+       while (1) {
+               poll_num = poll(fds, nfds, -1);
+                if (poll_num == -1) {
+                        if (errno == EINTR)    /* Interrupted by a signal */
+                                continue;      /* Restart poll() */
+
+                        perror("poll");                /* Unexpected error */
+                        exit(EXIT_FAILURE);
+                }
+
+               if (poll_num > 0) {
+
+                        if (fds[0].revents & POLLIN) {
+                               int n = read(fd, buffer, sizeof(buffer));
+                               if (n == -1 && errno != EAGAIN) {
+                                       perror("read");
+                                       exit(EXIT_FAILURE);
+                               }
+
+                               handle_notifications(fd, buffer, n);
+                       }
+               }
+       }
+
+       return 0;
+}
--
2.34.1



^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v1] erofs-utils: tests: add test for fanotify FAN_PRE_ACCESS event
  2026-10-30  9:22 ` Friendy.Su
@ 2026-11-18 11:35   ` Friendy.Su
  2026-11-18 20:41     ` Gao Xiang
  0 siblings, 1 reply; 5+ messages in thread
From: Friendy.Su @ 2026-11-18 11:35 UTC (permalink / raw)
  To: xiang@kernel.org, bluce.lee@aliyun.com, jnhuang95@gmail.com,
	hudsonzhu@tencent.com, zhaoyifan28@huawei.com
  Cc: linux-erofs@lists.ozlabs.org, Daniel.Palmer@sony.com,
	Yuezhang.Mo@sony.com

Hi,

How about this patch ?

________________________________________
From: Su, Friendy <Friendy.Su@sony.com>
Sent: Friday, October 30, 2026 17:22
To: xiang@kernel.org; bluce.lee@aliyun.com; jnhuang95@gmail.com; hudsonzhu@tencent.com; zhaoyifan28@huawei.com
Cc: linux-erofs@lists.ozlabs.org; Palmer, Daniel (SGC); Mo, Yuezhang
Subject: Re: [PATCH v1] erofs-utils: tests: add test for fanotify FAN_PRE_ACCESS event


This is the test for patch "erofs: set SB_I_ALLOW_HSM for bdev-based mounts" in kernel.

________________________________________
From: Su, Friendy <Friendy.Su@sony.com>
Sent: Friday, October 30, 2026 17:17
To: xiang@kernel.org; bluce.lee@aliyun.com; jnhuang95@gmail.com; hudsonzhu@tencent.com; zhaoyifan28@huawei.com
Cc: linux-erofs@lists.ozlabs.org; Su, Friendy; Palmer, Daniel (SGC); Mo, Yuezhang
Subject: [PATCH v1] erofs-utils: tests: add test for fanotify FAN_PRE_ACCESS event

SB_I_ALLOW_HSM is set in kernel for erofs. Add a test to verify 2 cases below.

case 1: with bdev-based device, FAN_PRE_ACCESS event is triggered.

 # SCRATCH_DEV=/dev/pmem1 make check TESTS=erofs/033

PASS: erofs/033
=============
1 test passed
=============

case 2: with file-backed image, FAN_PRE_ACCESS event fails to mark.

 # SCRATCH_DEV=erofs.img make check TESTS=erofs/033

fs-monitor not started
PASS: erofs/033
=============
1 test passed
=============

Signed-off-by: Friendy Su <friendy.su@sony.com>
Reviewed-by: Daniel Palmer <daniel.palmer@sony.com>
Reviewed-by: Yuezhang Mo <Yuezhang.Mo@sony.com>
---
 tests/erofs/033        |  85 +++++++++++++++++++++++
 tests/erofs/033.out    |   2 +
 tests/src/Makefile.am  |   6 +-
 tests/src/fs-monitor.c | 151 +++++++++++++++++++++++++++++++++++++++++
 4 files changed, 243 insertions(+), 1 deletion(-)
 create mode 100755 tests/erofs/033
 create mode 100644 tests/erofs/033.out
 create mode 100644 tests/src/fs-monitor.c

diff --git a/tests/erofs/033 b/tests/erofs/033
new file mode 100755
index 0000000..fab6d56
--- /dev/null
+++ b/tests/erofs/033
@@ -0,0 +1,85 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0+
+#
+# 033 - check FAN_PRE_ACCESS fanotify event
+#
+seq=`basename $0`
+seqres=$RESULT_DIR/$(echo $0 | awk '{print $((NF-1))"/"$NF}' FS="/")
+
+# get standard environment, filters and checks
+. "${srcdir}/common/rc"
+
+cleanup()
+{
+       cd /
+       rm -rf $tmp.*
+}
+
+_require_erofs
+
+# remove previous $seqres.full before test
+rm -f $seqres.full
+
+# real QA test starts here
+echo "QA output created by $seq"
+
+if [ -z $SCRATCH_DEV ]; then
+       SCRATCH_DEV=$tmp/erofs_$seq.img
+       rm -f SCRATCH_DEV
+fi
+
+localdir="$tmp/$seq"
+rm -rf $localdir
+mkdir -p $localdir
+
+# create a file for mmap()
+dd if=/dev/urandom of=$localdir/file-2M bs=2M count=1 >> $seqre.full 2>&1
+
+MKFS_OPTIONS="$MKFS_OPTIONS -x1"
+_scratch_mkfs $localdir >> $seqres.full 2>&1 || _fail "failed to mkfs"
+
+# _scratch_mount always mount with '-o loop', file backed mount
+# can not be tested. So mount with ${MOUNT_PROG} directly.
+#_scratch_mount 2>>$seqres.full
+${MOUNT_PROG} ${SCRATCH_DEV} ${SCRATCH_MNT} 2>>$seqres.full
+
+# start fanotify event monitor
+src/fs-monitor ${SCRATCH_MNT} > $tmp.fsmonitor 2>&1 &
+
+fsmonitor_pid=$!
+sleep 1
+
+ps -p ${fsmonitor_pid} > /dev/null
+
+if [ $? -ne 0 ]; then
+       echo "fs-monitor not started" | tee -a $seqres.full >&2
+       if [ ! -f ${SCRATCH_DEV} ]; then
+               echo "fanotify monitor failed to start on non-file-backed image"
+       fi
+       ${UMOUNT_PROG} ${SCRATCH_MNT}
+       echo Silence is golden
+       status=0
+       exit 0
+fi
+
+echo "fs-monitor started" | tee -a $seqres.full >&2
+
+# do mmap(), FAN_PRE_ACCESS event will be triggered
+xfs_io -r -c "mmap -r 0 2M" -c close ${SCRATCH_MNT}/file-2M >> $seqres.full
+
+sleep 1
+
+# kill fs-monitor process anyway
+kill -TERM ${fsmonitor_pid}
+
+cat $tmp.fsmonitor >>$seqres.full
+
+(cat $tmp.fsmonitor | grep -q "FAN_PRE_ACCESS EVENT" > /dev/null) || \
+       echo "No FAN_PRE_ACCESS EVENT"
+
+#_scratch_unmount
+${UMOUNT_PROG} ${SCRATCH_MNT}
+
+echo Silence is golden
+status=0
+exit 0
diff --git a/tests/erofs/033.out b/tests/erofs/033.out
new file mode 100644
index 0000000..c5d2914
--- /dev/null
+++ b/tests/erofs/033.out
@@ -0,0 +1,2 @@
+QA output created by 033
+Silence is golden
diff --git a/tests/src/Makefile.am b/tests/src/Makefile.am
index 16de41a..d645539 100644
--- a/tests/src/Makefile.am
+++ b/tests/src/Makefile.am
@@ -3,7 +3,7 @@

 AUTOMAKE_OPTIONS       = foreign
 check_PROGRAMS         =
-noinst_PROGRAMS                = fssum
+noinst_PROGRAMS                = fssum fs-monitor

 fssum_CFLAGS = -Wall -I$(top_srcdir)/include
 fssum_LDADD = $(top_builddir)/lib/liberofs.la
@@ -15,3 +15,7 @@ badlz4_SOURCES = badlz4.c
 badlz4_CFLAGS = ${liblz4_CFLAGS}
 badlz4_LDADD = ${liblz4_LIBS}
 endif
+
+check_PROGRAMS += fs-monitor
+fs_monitor_CFLAGS = -Wall -I$(top_srcdir)/include
+fs_monitor_SOURCES = fs-monitor.c
diff --git a/tests/src/fs-monitor.c b/tests/src/fs-monitor.c
new file mode 100644
index 0000000..07b7a2a
--- /dev/null
+++ b/tests/src/fs-monitor.c
@@ -0,0 +1,151 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Partly copied from https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/samples/fanotify/fs-monitor.c
+ */
+
+#include <errno.h>
+#include <err.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <sys/fanotify.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <poll.h>
+
+static void handle_pre_access_event(int fd,
+              struct fanotify_event_metadata *event)
+{
+       struct fanotify_event_info_header *info;
+       struct fanotify_event_info_range *range;
+       struct fanotify_response response;
+       int off;
+
+
+       for (off = sizeof(*event) ; off < event->event_len;
+            off += info->len) {
+               info = (struct fanotify_event_info_header *)
+                       ((char *) event + off);
+
+               switch (info->info_type) {
+               case FAN_EVENT_INFO_TYPE_RANGE:
+                       range = (struct fanotify_event_info_range *) info;
+
+                       printf("FAN_PRE_ACCESS event range: \n");
+                       printf("offset = 0x%llx, count = 0x%llx\n",
+                                       range->offset, range->count);
+
+                       response.fd = event->fd;
+                        response.response = FAN_ALLOW;
+                       if (write(fd, &response, sizeof(response)) < 0)
+                                       perror("Send FAN_PRE_ACCESS response failed");
+
+                       break;
+               default:
+                       printf("\tUnknown info type=%d len=%d:\n",
+                              info->info_type, info->len);
+                       break;
+               }
+
+               fflush(stdout);
+       }
+}
+
+static void handle_notifications(int fd, char *buffer, int len)
+{
+       struct fanotify_event_metadata *event =
+               (struct fanotify_event_metadata *) buffer;
+
+       for (; FAN_EVENT_OK(event, len); event = FAN_EVENT_NEXT(event, len)) {
+
+               if (event->mask == FAN_PRE_ACCESS) {
+
+                       /* This msg will be checked in test case */
+                       printf("FAN_PRE_ACCESS EVENT (len=%d)\n", event->event_len);
+
+                       handle_pre_access_event(fd, event);
+               }
+
+               /* TODO: Add more event handler here */
+
+               printf("---\n\n");
+               fflush(stdout);
+       }
+
+}
+
+int main(int argc, char **argv)
+{
+       int fd, poll_num, opt, nfds;
+       int check = 0;
+       char buffer[BUFSIZ];
+       struct pollfd fds[1];
+
+       if (argc < 2) {
+               printf("Missing path argument\n");
+               return 1;
+       }
+
+       while ((opt = getopt(argc, argv, "c")) != -1) {
+               switch (opt) {
+               case 'c':
+                       check = 1;
+                       break;
+               default:
+                       fprintf(stderr, "Usage: %s [-c] path\n",
+                               argv[0]);
+                       exit(EXIT_FAILURE);
+               }
+       }
+
+       fd = fanotify_init(FAN_CLOEXEC | FAN_CLASS_PRE_CONTENT | FAN_NONBLOCK,
+                        O_RDONLY);
+       if (check)
+               exit(fd < 0);
+
+       if (fd < 0) {
+               perror("fanotify_init");
+               errx(1, "fanotify_init");
+       }
+
+       if (fanotify_mark(fd, FAN_MARK_ADD | FAN_MARK_FILESYSTEM,
+                          FAN_PRE_ACCESS, AT_FDCWD, argv[optind])) {
+               perror("fanotify_mark");
+               errx(1, "fanotify_mark");
+       }
+
+       printf("fanotify active\n");
+       fflush(stdout);
+
+       fds[0].fd = fd;
+        fds[0].events = POLLIN;
+
+       nfds = 1;
+
+       while (1) {
+               poll_num = poll(fds, nfds, -1);
+                if (poll_num == -1) {
+                        if (errno == EINTR)    /* Interrupted by a signal */
+                                continue;      /* Restart poll() */
+
+                        perror("poll");                /* Unexpected error */
+                        exit(EXIT_FAILURE);
+                }
+
+               if (poll_num > 0) {
+
+                        if (fds[0].revents & POLLIN) {
+                               int n = read(fd, buffer, sizeof(buffer));
+                               if (n == -1 && errno != EAGAIN) {
+                                       perror("read");
+                                       exit(EXIT_FAILURE);
+                               }
+
+                               handle_notifications(fd, buffer, n);
+                       }
+               }
+       }
+
+       return 0;
+}
--
2.34.1



^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v1] erofs-utils: tests: add test for fanotify FAN_PRE_ACCESS event
  2026-11-18 11:35   ` Friendy.Su
@ 2026-11-18 20:41     ` Gao Xiang
  2026-11-19  1:58       ` Friendy.Su
  0 siblings, 1 reply; 5+ messages in thread
From: Gao Xiang @ 2026-11-18 20:41 UTC (permalink / raw)
  To: Friendy.Su@sony.com
  Cc: xiang@kernel.org, bluce.lee@aliyun.com, jnhuang95@gmail.com,
	hudsonzhu@tencent.com, zhaoyifan28@huawei.com,
	linux-erofs@lists.ozlabs.org, Daniel.Palmer@sony.com,
	Yuezhang.Mo@sony.com

Hi Friendy,

On Wed, Nov 18, 2026 at 11:35:39AM +0000, Friendy.Su@sony.com wrote:
> Hi,
> 
> How about this patch ?

Thanks for the ping.

I don't forget this one, but let's wait until there is no more
urgent fixes for this cycle, so I can merge this feature then
for the next cycle.

Thanks,
Gao Xiang



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v1] erofs-utils: tests: add test for fanotify FAN_PRE_ACCESS event
  2026-11-18 20:41     ` Gao Xiang
@ 2026-11-19  1:58       ` Friendy.Su
  0 siblings, 0 replies; 5+ messages in thread
From: Friendy.Su @ 2026-11-19  1:58 UTC (permalink / raw)
  To: Gao Xiang
  Cc: bluce.lee@aliyun.com, jnhuang95@gmail.com, hudsonzhu@tencent.com,
	zhaoyifan28@huawei.com, linux-erofs@lists.ozlabs.org,
	Daniel.Palmer@sony.com, Yuezhang.Mo@sony.com

Oh, Thanks. :)


________________________________________
From: Gao Xiang <xiang@kernel.org>
Sent: Thursday, November 19, 2026 4:41
To: Su, Friendy
Cc: xiang@kernel.org; bluce.lee@aliyun.com; jnhuang95@gmail.com; hudsonzhu@tencent.com; zhaoyifan28@huawei.com; linux-erofs@lists.ozlabs.org; Palmer, Daniel (SGC); Mo, Yuezhang
Subject: Re: [PATCH v1] erofs-utils: tests: add test for fanotify FAN_PRE_ACCESS event

Hi Friendy, On Wed, Nov 18, 2026 at 11: 35: 39AM +0000, Friendy. Su@ sony. com wrote: > Hi, > > How about this patch ? Thanks for the ping. I don't forget this one, but let's wait until there is no more urgent fixes for this cycle, so I


Hi Friendy,

On Wed, Nov 18, 2026 at 11:35:39AM +0000, Friendy.Su@sony.com wrote:
> Hi,
>
> How about this patch ?

Thanks for the ping.

I don't forget this one, but let's wait until there is no more
urgent fixes for this cycle, so I can merge this feature then
for the next cycle.

Thanks,
Gao Xiang




^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-11-19  1:59 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-10-30  9:17 [PATCH v1] erofs-utils: tests: add test for fanotify FAN_PRE_ACCESS event Friendy Su
2026-10-30  9:22 ` Friendy.Su
2026-11-18 11:35   ` Friendy.Su
2026-11-18 20:41     ` Gao Xiang
2026-11-19  1:58       ` Friendy.Su

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.