1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) 2022 CTERA Networks. All Rights Reserved.
4*49cdfc7eSAndroid Build Coastguard Worker *
5*49cdfc7eSAndroid Build Coastguard Worker * Author: Amir Goldstein <[email protected]>
6*49cdfc7eSAndroid Build Coastguard Worker */
7*49cdfc7eSAndroid Build Coastguard Worker
8*49cdfc7eSAndroid Build Coastguard Worker /*\
9*49cdfc7eSAndroid Build Coastguard Worker * [Description]
10*49cdfc7eSAndroid Build Coastguard Worker * Check evictable fanotify inode marks.
11*49cdfc7eSAndroid Build Coastguard Worker */
12*49cdfc7eSAndroid Build Coastguard Worker
13*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
14*49cdfc7eSAndroid Build Coastguard Worker #include "config.h"
15*49cdfc7eSAndroid Build Coastguard Worker
16*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
17*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
18*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
19*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
20*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
21*49cdfc7eSAndroid Build Coastguard Worker #include <sys/syscall.h>
22*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
23*49cdfc7eSAndroid Build Coastguard Worker
24*49cdfc7eSAndroid Build Coastguard Worker #ifdef HAVE_SYS_FANOTIFY_H
25*49cdfc7eSAndroid Build Coastguard Worker #include "fanotify.h"
26*49cdfc7eSAndroid Build Coastguard Worker
27*49cdfc7eSAndroid Build Coastguard Worker #define EVENT_MAX 1024
28*49cdfc7eSAndroid Build Coastguard Worker /* size of the event structure, not counting name */
29*49cdfc7eSAndroid Build Coastguard Worker #define EVENT_SIZE (sizeof(struct fanotify_event_metadata))
30*49cdfc7eSAndroid Build Coastguard Worker /* reasonable guess as to size of 1024 events */
31*49cdfc7eSAndroid Build Coastguard Worker #define EVENT_BUF_LEN (EVENT_MAX * EVENT_SIZE)
32*49cdfc7eSAndroid Build Coastguard Worker
33*49cdfc7eSAndroid Build Coastguard Worker #define MOUNT_PATH "fs_mnt"
34*49cdfc7eSAndroid Build Coastguard Worker #define TEST_FILE MOUNT_PATH "/testfile"
35*49cdfc7eSAndroid Build Coastguard Worker
36*49cdfc7eSAndroid Build Coastguard Worker #define DROP_CACHES_FILE "/proc/sys/vm/drop_caches"
37*49cdfc7eSAndroid Build Coastguard Worker #define CACHE_PRESSURE_FILE "/proc/sys/vm/vfs_cache_pressure"
38*49cdfc7eSAndroid Build Coastguard Worker
39*49cdfc7eSAndroid Build Coastguard Worker static int old_cache_pressure;
40*49cdfc7eSAndroid Build Coastguard Worker static int fd_notify;
41*49cdfc7eSAndroid Build Coastguard Worker
42*49cdfc7eSAndroid Build Coastguard Worker static unsigned long long event_set[EVENT_MAX];
43*49cdfc7eSAndroid Build Coastguard Worker
44*49cdfc7eSAndroid Build Coastguard Worker static char event_buf[EVENT_BUF_LEN];
45*49cdfc7eSAndroid Build Coastguard Worker
fsync_file(const char * path)46*49cdfc7eSAndroid Build Coastguard Worker static void fsync_file(const char *path)
47*49cdfc7eSAndroid Build Coastguard Worker {
48*49cdfc7eSAndroid Build Coastguard Worker int fd = SAFE_OPEN(path, O_RDONLY);
49*49cdfc7eSAndroid Build Coastguard Worker
50*49cdfc7eSAndroid Build Coastguard Worker SAFE_FSYNC(fd);
51*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd);
52*49cdfc7eSAndroid Build Coastguard Worker }
53*49cdfc7eSAndroid Build Coastguard Worker
54*49cdfc7eSAndroid Build Coastguard Worker /* Flush out all pending dirty inodes and destructing marks */
mount_cycle(void)55*49cdfc7eSAndroid Build Coastguard Worker static void mount_cycle(void)
56*49cdfc7eSAndroid Build Coastguard Worker {
57*49cdfc7eSAndroid Build Coastguard Worker SAFE_UMOUNT(MOUNT_PATH);
58*49cdfc7eSAndroid Build Coastguard Worker SAFE_MOUNT(tst_device->dev, MOUNT_PATH, tst_device->fs_type, 0, NULL);
59*49cdfc7eSAndroid Build Coastguard Worker }
60*49cdfc7eSAndroid Build Coastguard Worker
verify_mark_removed(const char * path,const char * when)61*49cdfc7eSAndroid Build Coastguard Worker static int verify_mark_removed(const char *path, const char *when)
62*49cdfc7eSAndroid Build Coastguard Worker {
63*49cdfc7eSAndroid Build Coastguard Worker int ret;
64*49cdfc7eSAndroid Build Coastguard Worker
65*49cdfc7eSAndroid Build Coastguard Worker /*
66*49cdfc7eSAndroid Build Coastguard Worker * We know that inode with evictable mark was evicted when a
67*49cdfc7eSAndroid Build Coastguard Worker * bogus call remove ACCESS from event mask returns ENOENT.
68*49cdfc7eSAndroid Build Coastguard Worker */
69*49cdfc7eSAndroid Build Coastguard Worker errno = 0;
70*49cdfc7eSAndroid Build Coastguard Worker ret = fanotify_mark(fd_notify, FAN_MARK_REMOVE,
71*49cdfc7eSAndroid Build Coastguard Worker FAN_ACCESS, AT_FDCWD, path);
72*49cdfc7eSAndroid Build Coastguard Worker if (ret == -1 && errno == ENOENT) {
73*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS,
74*49cdfc7eSAndroid Build Coastguard Worker "FAN_MARK_REMOVE failed with ENOENT as expected"
75*49cdfc7eSAndroid Build Coastguard Worker " %s", when);
76*49cdfc7eSAndroid Build Coastguard Worker return 1;
77*49cdfc7eSAndroid Build Coastguard Worker }
78*49cdfc7eSAndroid Build Coastguard Worker
79*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL | TERRNO,
80*49cdfc7eSAndroid Build Coastguard Worker "FAN_MARK_REMOVE did not fail with ENOENT as expected"
81*49cdfc7eSAndroid Build Coastguard Worker " %s", when);
82*49cdfc7eSAndroid Build Coastguard Worker
83*49cdfc7eSAndroid Build Coastguard Worker return 0;
84*49cdfc7eSAndroid Build Coastguard Worker }
85*49cdfc7eSAndroid Build Coastguard Worker
test_fanotify(void)86*49cdfc7eSAndroid Build Coastguard Worker static void test_fanotify(void)
87*49cdfc7eSAndroid Build Coastguard Worker {
88*49cdfc7eSAndroid Build Coastguard Worker int ret, len, test_num = 0;
89*49cdfc7eSAndroid Build Coastguard Worker struct fanotify_event_metadata *event;
90*49cdfc7eSAndroid Build Coastguard Worker int tst_count = 0;
91*49cdfc7eSAndroid Build Coastguard Worker
92*49cdfc7eSAndroid Build Coastguard Worker fd_notify = SAFE_FANOTIFY_INIT(FAN_CLASS_NOTIF | FAN_REPORT_FID |
93*49cdfc7eSAndroid Build Coastguard Worker FAN_NONBLOCK, O_RDONLY);
94*49cdfc7eSAndroid Build Coastguard Worker
95*49cdfc7eSAndroid Build Coastguard Worker /*
96*49cdfc7eSAndroid Build Coastguard Worker * Verify that evictable mark can be upgraded to non-evictable
97*49cdfc7eSAndroid Build Coastguard Worker * and cannot be downgraded to evictable.
98*49cdfc7eSAndroid Build Coastguard Worker */
99*49cdfc7eSAndroid Build Coastguard Worker SAFE_FANOTIFY_MARK(fd_notify, FAN_MARK_ADD | FAN_MARK_EVICTABLE,
100*49cdfc7eSAndroid Build Coastguard Worker FAN_ACCESS,
101*49cdfc7eSAndroid Build Coastguard Worker AT_FDCWD, TEST_FILE);
102*49cdfc7eSAndroid Build Coastguard Worker SAFE_FANOTIFY_MARK(fd_notify, FAN_MARK_ADD,
103*49cdfc7eSAndroid Build Coastguard Worker FAN_ACCESS,
104*49cdfc7eSAndroid Build Coastguard Worker AT_FDCWD, TEST_FILE);
105*49cdfc7eSAndroid Build Coastguard Worker errno = 0;
106*49cdfc7eSAndroid Build Coastguard Worker ret = fanotify_mark(fd_notify, FAN_MARK_ADD | FAN_MARK_EVICTABLE,
107*49cdfc7eSAndroid Build Coastguard Worker FAN_ACCESS,
108*49cdfc7eSAndroid Build Coastguard Worker AT_FDCWD, TEST_FILE);
109*49cdfc7eSAndroid Build Coastguard Worker if (ret == -1 && errno == EEXIST) {
110*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS,
111*49cdfc7eSAndroid Build Coastguard Worker "FAN_MARK_ADD failed with EEXIST as expected"
112*49cdfc7eSAndroid Build Coastguard Worker " when trying to downgrade to evictable mark");
113*49cdfc7eSAndroid Build Coastguard Worker } else {
114*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL | TERRNO,
115*49cdfc7eSAndroid Build Coastguard Worker "FAN_MARK_ADD did not fail with EEXIST as expected"
116*49cdfc7eSAndroid Build Coastguard Worker " when trying to downgrade to evictable mark");
117*49cdfc7eSAndroid Build Coastguard Worker }
118*49cdfc7eSAndroid Build Coastguard Worker SAFE_FANOTIFY_MARK(fd_notify, FAN_MARK_REMOVE,
119*49cdfc7eSAndroid Build Coastguard Worker FAN_ACCESS,
120*49cdfc7eSAndroid Build Coastguard Worker AT_FDCWD, TEST_FILE);
121*49cdfc7eSAndroid Build Coastguard Worker verify_mark_removed(TEST_FILE, "after empty mask");
122*49cdfc7eSAndroid Build Coastguard Worker
123*49cdfc7eSAndroid Build Coastguard Worker
124*49cdfc7eSAndroid Build Coastguard Worker /*
125*49cdfc7eSAndroid Build Coastguard Worker * Watch ATTRIB events on entire mount
126*49cdfc7eSAndroid Build Coastguard Worker */
127*49cdfc7eSAndroid Build Coastguard Worker SAFE_FANOTIFY_MARK(fd_notify, FAN_MARK_ADD | FAN_MARK_FILESYSTEM,
128*49cdfc7eSAndroid Build Coastguard Worker FAN_ATTRIB, AT_FDCWD, MOUNT_PATH);
129*49cdfc7eSAndroid Build Coastguard Worker
130*49cdfc7eSAndroid Build Coastguard Worker /*
131*49cdfc7eSAndroid Build Coastguard Worker * Generate events
132*49cdfc7eSAndroid Build Coastguard Worker */
133*49cdfc7eSAndroid Build Coastguard Worker SAFE_CHMOD(TEST_FILE, 0600);
134*49cdfc7eSAndroid Build Coastguard Worker event_set[tst_count] = FAN_ATTRIB;
135*49cdfc7eSAndroid Build Coastguard Worker tst_count++;
136*49cdfc7eSAndroid Build Coastguard Worker
137*49cdfc7eSAndroid Build Coastguard Worker /* Read events so far */
138*49cdfc7eSAndroid Build Coastguard Worker ret = SAFE_READ(0, fd_notify, event_buf, EVENT_BUF_LEN);
139*49cdfc7eSAndroid Build Coastguard Worker len = ret;
140*49cdfc7eSAndroid Build Coastguard Worker
141*49cdfc7eSAndroid Build Coastguard Worker /*
142*49cdfc7eSAndroid Build Coastguard Worker * Evictable mark on file ignores ATTRIB events
143*49cdfc7eSAndroid Build Coastguard Worker */
144*49cdfc7eSAndroid Build Coastguard Worker SAFE_FANOTIFY_MARK(fd_notify, FAN_MARK_ADD | FAN_MARK_EVICTABLE |
145*49cdfc7eSAndroid Build Coastguard Worker FAN_MARK_IGNORED_MASK | FAN_MARK_IGNORED_SURV_MODIFY,
146*49cdfc7eSAndroid Build Coastguard Worker FAN_ATTRIB, AT_FDCWD, TEST_FILE);
147*49cdfc7eSAndroid Build Coastguard Worker
148*49cdfc7eSAndroid Build Coastguard Worker /* ATTRIB event should be ignored */
149*49cdfc7eSAndroid Build Coastguard Worker SAFE_CHMOD(TEST_FILE, 0600);
150*49cdfc7eSAndroid Build Coastguard Worker
151*49cdfc7eSAndroid Build Coastguard Worker /*
152*49cdfc7eSAndroid Build Coastguard Worker * Read events to verify event was ignored
153*49cdfc7eSAndroid Build Coastguard Worker */
154*49cdfc7eSAndroid Build Coastguard Worker ret = read(fd_notify, event_buf + len, EVENT_BUF_LEN - len);
155*49cdfc7eSAndroid Build Coastguard Worker if (ret < 0 && errno == EAGAIN) {
156*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "Got no events as expected");
157*49cdfc7eSAndroid Build Coastguard Worker } else {
158*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "Got expected events");
159*49cdfc7eSAndroid Build Coastguard Worker len += ret;
160*49cdfc7eSAndroid Build Coastguard Worker }
161*49cdfc7eSAndroid Build Coastguard Worker
162*49cdfc7eSAndroid Build Coastguard Worker /*
163*49cdfc7eSAndroid Build Coastguard Worker * drop_caches should evict inode from cache and remove evictable mark.
164*49cdfc7eSAndroid Build Coastguard Worker * We call drop_caches twice as once the dentries will just cycle
165*49cdfc7eSAndroid Build Coastguard Worker * through the LRU without being reclaimed and if there are no other
166*49cdfc7eSAndroid Build Coastguard Worker * objects to reclaim, the slab reclaim will just stop instead of
167*49cdfc7eSAndroid Build Coastguard Worker * retrying. Note that this relies on how reclaim of fs objects work
168*49cdfc7eSAndroid Build Coastguard Worker * for the filesystem but this test is restricted to ext2...
169*49cdfc7eSAndroid Build Coastguard Worker */
170*49cdfc7eSAndroid Build Coastguard Worker fsync_file(TEST_FILE);
171*49cdfc7eSAndroid Build Coastguard Worker SAFE_FILE_PRINTF(DROP_CACHES_FILE, "3");
172*49cdfc7eSAndroid Build Coastguard Worker SAFE_FILE_PRINTF(DROP_CACHES_FILE, "3");
173*49cdfc7eSAndroid Build Coastguard Worker
174*49cdfc7eSAndroid Build Coastguard Worker verify_mark_removed(TEST_FILE, "after drop_caches");
175*49cdfc7eSAndroid Build Coastguard Worker
176*49cdfc7eSAndroid Build Coastguard Worker SAFE_CHMOD(TEST_FILE, 0600);
177*49cdfc7eSAndroid Build Coastguard Worker event_set[tst_count] = FAN_ATTRIB;
178*49cdfc7eSAndroid Build Coastguard Worker tst_count++;
179*49cdfc7eSAndroid Build Coastguard Worker
180*49cdfc7eSAndroid Build Coastguard Worker /* Read events to verify ATTRIB event was properly generated */
181*49cdfc7eSAndroid Build Coastguard Worker ret = SAFE_READ(0, fd_notify, event_buf + len, EVENT_BUF_LEN - len);
182*49cdfc7eSAndroid Build Coastguard Worker len += ret;
183*49cdfc7eSAndroid Build Coastguard Worker
184*49cdfc7eSAndroid Build Coastguard Worker /*
185*49cdfc7eSAndroid Build Coastguard Worker * Check events
186*49cdfc7eSAndroid Build Coastguard Worker */
187*49cdfc7eSAndroid Build Coastguard Worker event = (struct fanotify_event_metadata *)event_buf;
188*49cdfc7eSAndroid Build Coastguard Worker
189*49cdfc7eSAndroid Build Coastguard Worker /* Iterate over and validate events against expected result set */
190*49cdfc7eSAndroid Build Coastguard Worker while (FAN_EVENT_OK(event, len) && test_num < tst_count) {
191*49cdfc7eSAndroid Build Coastguard Worker if (!(event->mask & event_set[test_num])) {
192*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL,
193*49cdfc7eSAndroid Build Coastguard Worker "got event: mask=%llx (expected %llx)",
194*49cdfc7eSAndroid Build Coastguard Worker (unsigned long long)event->mask,
195*49cdfc7eSAndroid Build Coastguard Worker event_set[test_num]);
196*49cdfc7eSAndroid Build Coastguard Worker } else {
197*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS,
198*49cdfc7eSAndroid Build Coastguard Worker "got event: mask=%llx",
199*49cdfc7eSAndroid Build Coastguard Worker (unsigned long long)event->mask);
200*49cdfc7eSAndroid Build Coastguard Worker }
201*49cdfc7eSAndroid Build Coastguard Worker /*
202*49cdfc7eSAndroid Build Coastguard Worker * Close fd and invalidate it so that we don't check it again
203*49cdfc7eSAndroid Build Coastguard Worker * unnecessarily
204*49cdfc7eSAndroid Build Coastguard Worker */
205*49cdfc7eSAndroid Build Coastguard Worker if (event->fd >= 0)
206*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(event->fd);
207*49cdfc7eSAndroid Build Coastguard Worker event->fd = FAN_NOFD;
208*49cdfc7eSAndroid Build Coastguard Worker event->mask &= ~event_set[test_num];
209*49cdfc7eSAndroid Build Coastguard Worker /* No events left in current mask? Go for next event */
210*49cdfc7eSAndroid Build Coastguard Worker if (event->mask == 0)
211*49cdfc7eSAndroid Build Coastguard Worker event = FAN_EVENT_NEXT(event, len);
212*49cdfc7eSAndroid Build Coastguard Worker test_num++;
213*49cdfc7eSAndroid Build Coastguard Worker }
214*49cdfc7eSAndroid Build Coastguard Worker
215*49cdfc7eSAndroid Build Coastguard Worker while (FAN_EVENT_OK(event, len)) {
216*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL,
217*49cdfc7eSAndroid Build Coastguard Worker "got unnecessary event: mask=%llx",
218*49cdfc7eSAndroid Build Coastguard Worker (unsigned long long)event->mask);
219*49cdfc7eSAndroid Build Coastguard Worker if (event->fd != FAN_NOFD)
220*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(event->fd);
221*49cdfc7eSAndroid Build Coastguard Worker event = FAN_EVENT_NEXT(event, len);
222*49cdfc7eSAndroid Build Coastguard Worker }
223*49cdfc7eSAndroid Build Coastguard Worker
224*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd_notify);
225*49cdfc7eSAndroid Build Coastguard Worker /* Flush out all pending dirty inodes and destructing marks */
226*49cdfc7eSAndroid Build Coastguard Worker mount_cycle();
227*49cdfc7eSAndroid Build Coastguard Worker }
228*49cdfc7eSAndroid Build Coastguard Worker
setup(void)229*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
230*49cdfc7eSAndroid Build Coastguard Worker {
231*49cdfc7eSAndroid Build Coastguard Worker SAFE_TOUCH(TEST_FILE, 0666, NULL);
232*49cdfc7eSAndroid Build Coastguard Worker
233*49cdfc7eSAndroid Build Coastguard Worker REQUIRE_MARK_TYPE_SUPPORTED_ON_FS(FAN_MARK_EVICTABLE, ".");
234*49cdfc7eSAndroid Build Coastguard Worker REQUIRE_FANOTIFY_EVENTS_SUPPORTED_ON_FS(FAN_CLASS_NOTIF|FAN_REPORT_FID,
235*49cdfc7eSAndroid Build Coastguard Worker FAN_MARK_FILESYSTEM,
236*49cdfc7eSAndroid Build Coastguard Worker FAN_ATTRIB, ".");
237*49cdfc7eSAndroid Build Coastguard Worker
238*49cdfc7eSAndroid Build Coastguard Worker SAFE_FILE_SCANF(CACHE_PRESSURE_FILE, "%d", &old_cache_pressure);
239*49cdfc7eSAndroid Build Coastguard Worker /* Set high priority for evicting inodes */
240*49cdfc7eSAndroid Build Coastguard Worker SAFE_FILE_PRINTF(CACHE_PRESSURE_FILE, "500");
241*49cdfc7eSAndroid Build Coastguard Worker }
242*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)243*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
244*49cdfc7eSAndroid Build Coastguard Worker {
245*49cdfc7eSAndroid Build Coastguard Worker if (fd_notify > 0)
246*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd_notify);
247*49cdfc7eSAndroid Build Coastguard Worker
248*49cdfc7eSAndroid Build Coastguard Worker SAFE_FILE_PRINTF(CACHE_PRESSURE_FILE, "%d", old_cache_pressure);
249*49cdfc7eSAndroid Build Coastguard Worker }
250*49cdfc7eSAndroid Build Coastguard Worker
251*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
252*49cdfc7eSAndroid Build Coastguard Worker .test_all = test_fanotify,
253*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
254*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup,
255*49cdfc7eSAndroid Build Coastguard Worker .needs_root = 1,
256*49cdfc7eSAndroid Build Coastguard Worker .mount_device = 1,
257*49cdfc7eSAndroid Build Coastguard Worker .mntpoint = MOUNT_PATH,
258*49cdfc7eSAndroid Build Coastguard Worker /* Shrinkers on other fs do not work reliably enough to guarantee mark eviction on drop_caches */
259*49cdfc7eSAndroid Build Coastguard Worker .dev_fs_type = "ext2",
260*49cdfc7eSAndroid Build Coastguard Worker };
261*49cdfc7eSAndroid Build Coastguard Worker
262*49cdfc7eSAndroid Build Coastguard Worker #else
263*49cdfc7eSAndroid Build Coastguard Worker TST_TEST_TCONF("system doesn't have required fanotify support");
264*49cdfc7eSAndroid Build Coastguard Worker #endif
265