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) 2018 CTERA Networks. All Rights Reserved.
4*49cdfc7eSAndroid Build Coastguard Worker * Author: Amir Goldstein <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker */
6*49cdfc7eSAndroid Build Coastguard Worker
7*49cdfc7eSAndroid Build Coastguard Worker /*\
8*49cdfc7eSAndroid Build Coastguard Worker * [Description]
9*49cdfc7eSAndroid Build Coastguard Worker *
10*49cdfc7eSAndroid Build Coastguard Worker * Check that inotify work for an overlayfs file after copy up and
11*49cdfc7eSAndroid Build Coastguard Worker * drop caches.
12*49cdfc7eSAndroid Build Coastguard Worker *
13*49cdfc7eSAndroid Build Coastguard Worker * An inotify watch pins the file inode in cache, but not the dentry.
14*49cdfc7eSAndroid Build Coastguard Worker * The watch will not report events on the file if overlayfs does not
15*49cdfc7eSAndroid Build Coastguard Worker * obtain the pinned inode to the new allocated dentry after drop caches.
16*49cdfc7eSAndroid Build Coastguard Worker *
17*49cdfc7eSAndroid Build Coastguard Worker * The problem has been fixed by commit:
18*49cdfc7eSAndroid Build Coastguard Worker * 764baba80168 ("ovl: hash non-dir by lower inode for fsnotify").
19*49cdfc7eSAndroid Build Coastguard Worker *
20*49cdfc7eSAndroid Build Coastguard Worker * [Algorithm]
21*49cdfc7eSAndroid Build Coastguard Worker *
22*49cdfc7eSAndroid Build Coastguard Worker * Add watch on an overlayfs lower file then chmod file and drop dentry
23*49cdfc7eSAndroid Build Coastguard Worker * and inode caches. Execute operations on file and expect events to be
24*49cdfc7eSAndroid Build Coastguard Worker * reported on file watch.
25*49cdfc7eSAndroid Build Coastguard Worker */
26*49cdfc7eSAndroid Build Coastguard Worker
27*49cdfc7eSAndroid Build Coastguard Worker #include "config.h"
28*49cdfc7eSAndroid Build Coastguard Worker
29*49cdfc7eSAndroid Build Coastguard Worker #if defined(HAVE_SYS_INOTIFY_H)
30*49cdfc7eSAndroid Build Coastguard Worker # include <sys/inotify.h>
31*49cdfc7eSAndroid Build Coastguard Worker #endif
32*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
33*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
34*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
35*49cdfc7eSAndroid Build Coastguard Worker #include <sys/sysmacros.h>
36*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
37*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
38*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
39*49cdfc7eSAndroid Build Coastguard Worker #include <sys/syscall.h>
40*49cdfc7eSAndroid Build Coastguard Worker #include <sys/mount.h>
41*49cdfc7eSAndroid Build Coastguard Worker #include <limits.h>
42*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
43*49cdfc7eSAndroid Build Coastguard Worker #include "inotify.h"
44*49cdfc7eSAndroid Build Coastguard Worker
45*49cdfc7eSAndroid Build Coastguard Worker #if defined(HAVE_SYS_INOTIFY_H)
46*49cdfc7eSAndroid Build Coastguard Worker
47*49cdfc7eSAndroid Build Coastguard Worker #define EVENT_MAX 1024
48*49cdfc7eSAndroid Build Coastguard Worker /* size of the event structure, not counting name */
49*49cdfc7eSAndroid Build Coastguard Worker #define EVENT_SIZE (sizeof (struct inotify_event))
50*49cdfc7eSAndroid Build Coastguard Worker /* reasonable guess as to size of 1024 events */
51*49cdfc7eSAndroid Build Coastguard Worker #define EVENT_BUF_LEN (EVENT_MAX * (EVENT_SIZE + 16))
52*49cdfc7eSAndroid Build Coastguard Worker
53*49cdfc7eSAndroid Build Coastguard Worker #define BUF_SIZE 256
54*49cdfc7eSAndroid Build Coastguard Worker static int fd_notify, reap_wd;
55*49cdfc7eSAndroid Build Coastguard Worker static int wd;
56*49cdfc7eSAndroid Build Coastguard Worker
57*49cdfc7eSAndroid Build Coastguard Worker struct event_t {
58*49cdfc7eSAndroid Build Coastguard Worker unsigned int mask;
59*49cdfc7eSAndroid Build Coastguard Worker };
60*49cdfc7eSAndroid Build Coastguard Worker
61*49cdfc7eSAndroid Build Coastguard Worker #define FILE_NAME "test_file"
62*49cdfc7eSAndroid Build Coastguard Worker #define FILE_PATH OVL_MNT"/"FILE_NAME
63*49cdfc7eSAndroid Build Coastguard Worker
64*49cdfc7eSAndroid Build Coastguard Worker static const char mntpoint[] = OVL_BASE_MNTPOINT;
65*49cdfc7eSAndroid Build Coastguard Worker static struct event_t event_set[EVENT_MAX];
66*49cdfc7eSAndroid Build Coastguard Worker static char event_buf[EVENT_BUF_LEN];
67*49cdfc7eSAndroid Build Coastguard Worker
verify_inotify(void)68*49cdfc7eSAndroid Build Coastguard Worker void verify_inotify(void)
69*49cdfc7eSAndroid Build Coastguard Worker {
70*49cdfc7eSAndroid Build Coastguard Worker int test_cnt = 0;
71*49cdfc7eSAndroid Build Coastguard Worker
72*49cdfc7eSAndroid Build Coastguard Worker /*
73*49cdfc7eSAndroid Build Coastguard Worker * generate sequence of events
74*49cdfc7eSAndroid Build Coastguard Worker */
75*49cdfc7eSAndroid Build Coastguard Worker SAFE_CHMOD(FILE_PATH, 0644);
76*49cdfc7eSAndroid Build Coastguard Worker event_set[test_cnt].mask = IN_ATTRIB;
77*49cdfc7eSAndroid Build Coastguard Worker test_cnt++;
78*49cdfc7eSAndroid Build Coastguard Worker
79*49cdfc7eSAndroid Build Coastguard Worker SAFE_FILE_PRINTF(FILE_PATH, "1");
80*49cdfc7eSAndroid Build Coastguard Worker
81*49cdfc7eSAndroid Build Coastguard Worker event_set[test_cnt].mask = IN_OPEN;
82*49cdfc7eSAndroid Build Coastguard Worker test_cnt++;
83*49cdfc7eSAndroid Build Coastguard Worker
84*49cdfc7eSAndroid Build Coastguard Worker event_set[test_cnt].mask = IN_CLOSE_WRITE;
85*49cdfc7eSAndroid Build Coastguard Worker test_cnt++;
86*49cdfc7eSAndroid Build Coastguard Worker
87*49cdfc7eSAndroid Build Coastguard Worker /* Make sure events on upper/lower do not show in overlay watch */
88*49cdfc7eSAndroid Build Coastguard Worker SAFE_TOUCH(OVL_LOWER"/"FILE_NAME, 0644, NULL);
89*49cdfc7eSAndroid Build Coastguard Worker SAFE_TOUCH(OVL_UPPER"/"FILE_NAME, 0644, NULL);
90*49cdfc7eSAndroid Build Coastguard Worker
91*49cdfc7eSAndroid Build Coastguard Worker int len = read(fd_notify, event_buf, EVENT_BUF_LEN);
92*49cdfc7eSAndroid Build Coastguard Worker if (len == -1 && errno != EAGAIN) {
93*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK | TERRNO,
94*49cdfc7eSAndroid Build Coastguard Worker "read(%d, buf, %zu) failed",
95*49cdfc7eSAndroid Build Coastguard Worker fd_notify, EVENT_BUF_LEN);
96*49cdfc7eSAndroid Build Coastguard Worker }
97*49cdfc7eSAndroid Build Coastguard Worker
98*49cdfc7eSAndroid Build Coastguard Worker int i = 0, test_num = 0;
99*49cdfc7eSAndroid Build Coastguard Worker while (i < len) {
100*49cdfc7eSAndroid Build Coastguard Worker struct inotify_event *event;
101*49cdfc7eSAndroid Build Coastguard Worker event = (struct inotify_event *)&event_buf[i];
102*49cdfc7eSAndroid Build Coastguard Worker if (test_num >= test_cnt) {
103*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL,
104*49cdfc7eSAndroid Build Coastguard Worker "get unnecessary event: "
105*49cdfc7eSAndroid Build Coastguard Worker "wd=%d mask=%08x cookie=%-5u len=%-2u "
106*49cdfc7eSAndroid Build Coastguard Worker "name=\"%.*s\"", event->wd, event->mask,
107*49cdfc7eSAndroid Build Coastguard Worker event->cookie, event->len, event->len,
108*49cdfc7eSAndroid Build Coastguard Worker event->name);
109*49cdfc7eSAndroid Build Coastguard Worker } else if (event_set[test_num].mask == event->mask &&
110*49cdfc7eSAndroid Build Coastguard Worker !event->len) {
111*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS,
112*49cdfc7eSAndroid Build Coastguard Worker "get event: wd=%d mask=%08x "
113*49cdfc7eSAndroid Build Coastguard Worker "cookie=%-5u len=%-2u",
114*49cdfc7eSAndroid Build Coastguard Worker event->wd, event->mask,
115*49cdfc7eSAndroid Build Coastguard Worker event->cookie, event->len);
116*49cdfc7eSAndroid Build Coastguard Worker } else {
117*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "get event: wd=%d mask=%08x "
118*49cdfc7eSAndroid Build Coastguard Worker "(expected %x) cookie=%-5u len=%-2u "
119*49cdfc7eSAndroid Build Coastguard Worker "name=\"%.*s\" (expected \"\")",
120*49cdfc7eSAndroid Build Coastguard Worker event->wd, event->mask,
121*49cdfc7eSAndroid Build Coastguard Worker event_set[test_num].mask,
122*49cdfc7eSAndroid Build Coastguard Worker event->cookie, event->len, event->len,
123*49cdfc7eSAndroid Build Coastguard Worker event->name);
124*49cdfc7eSAndroid Build Coastguard Worker }
125*49cdfc7eSAndroid Build Coastguard Worker test_num++;
126*49cdfc7eSAndroid Build Coastguard Worker i += EVENT_SIZE + event->len;
127*49cdfc7eSAndroid Build Coastguard Worker }
128*49cdfc7eSAndroid Build Coastguard Worker
129*49cdfc7eSAndroid Build Coastguard Worker for (; test_num < test_cnt; test_num++) {
130*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "didn't get event: mask=%x ",
131*49cdfc7eSAndroid Build Coastguard Worker event_set[test_num].mask);
132*49cdfc7eSAndroid Build Coastguard Worker }
133*49cdfc7eSAndroid Build Coastguard Worker }
134*49cdfc7eSAndroid Build Coastguard Worker
setup(void)135*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
136*49cdfc7eSAndroid Build Coastguard Worker {
137*49cdfc7eSAndroid Build Coastguard Worker struct stat buf;
138*49cdfc7eSAndroid Build Coastguard Worker
139*49cdfc7eSAndroid Build Coastguard Worker /* Setup an overlay mount with lower file */
140*49cdfc7eSAndroid Build Coastguard Worker SAFE_UMOUNT(OVL_MNT);
141*49cdfc7eSAndroid Build Coastguard Worker SAFE_TOUCH(OVL_LOWER"/"FILE_NAME, 0644, NULL);
142*49cdfc7eSAndroid Build Coastguard Worker SAFE_MOUNT_OVERLAY();
143*49cdfc7eSAndroid Build Coastguard Worker
144*49cdfc7eSAndroid Build Coastguard Worker fd_notify = SAFE_MYINOTIFY_INIT1(O_NONBLOCK);
145*49cdfc7eSAndroid Build Coastguard Worker
146*49cdfc7eSAndroid Build Coastguard Worker /* Setup a watch on an overlayfs lower file */
147*49cdfc7eSAndroid Build Coastguard Worker wd = SAFE_MYINOTIFY_ADD_WATCH(fd_notify, FILE_PATH,
148*49cdfc7eSAndroid Build Coastguard Worker IN_ATTRIB | IN_OPEN | IN_CLOSE_WRITE);
149*49cdfc7eSAndroid Build Coastguard Worker reap_wd = 1;
150*49cdfc7eSAndroid Build Coastguard Worker
151*49cdfc7eSAndroid Build Coastguard Worker SAFE_STAT(FILE_PATH, &buf);
152*49cdfc7eSAndroid Build Coastguard Worker tst_res(TINFO, FILE_PATH " ino=%lu, dev=%u:%u", buf.st_ino,
153*49cdfc7eSAndroid Build Coastguard Worker major(buf.st_dev), minor(buf.st_dev));
154*49cdfc7eSAndroid Build Coastguard Worker
155*49cdfc7eSAndroid Build Coastguard Worker /* Drop dentry caches, so overlayfs will allocate a new dentry */
156*49cdfc7eSAndroid Build Coastguard Worker SAFE_FILE_PRINTF("/proc/sys/vm/drop_caches", "2");
157*49cdfc7eSAndroid Build Coastguard Worker
158*49cdfc7eSAndroid Build Coastguard Worker /* Copy up file */
159*49cdfc7eSAndroid Build Coastguard Worker SAFE_CHMOD(FILE_PATH, 0600);
160*49cdfc7eSAndroid Build Coastguard Worker
161*49cdfc7eSAndroid Build Coastguard Worker /* Lookup file and see if we got the watched file inode number */
162*49cdfc7eSAndroid Build Coastguard Worker SAFE_STAT(FILE_PATH, &buf);
163*49cdfc7eSAndroid Build Coastguard Worker tst_res(TINFO, FILE_PATH " ino=%lu, dev=%u:%u", buf.st_ino,
164*49cdfc7eSAndroid Build Coastguard Worker major(buf.st_dev), minor(buf.st_dev));
165*49cdfc7eSAndroid Build Coastguard Worker }
166*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)167*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
168*49cdfc7eSAndroid Build Coastguard Worker {
169*49cdfc7eSAndroid Build Coastguard Worker if (reap_wd && myinotify_rm_watch(fd_notify, wd) < 0) {
170*49cdfc7eSAndroid Build Coastguard Worker tst_res(TWARN,
171*49cdfc7eSAndroid Build Coastguard Worker "inotify_rm_watch (%d, %d) failed,", fd_notify, wd);
172*49cdfc7eSAndroid Build Coastguard Worker }
173*49cdfc7eSAndroid Build Coastguard Worker
174*49cdfc7eSAndroid Build Coastguard Worker if (fd_notify > 0)
175*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd_notify);
176*49cdfc7eSAndroid Build Coastguard Worker }
177*49cdfc7eSAndroid Build Coastguard Worker
178*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
179*49cdfc7eSAndroid Build Coastguard Worker .needs_root = 1,
180*49cdfc7eSAndroid Build Coastguard Worker .mount_device = 1,
181*49cdfc7eSAndroid Build Coastguard Worker .needs_overlay = 1,
182*49cdfc7eSAndroid Build Coastguard Worker .mntpoint = mntpoint,
183*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
184*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup,
185*49cdfc7eSAndroid Build Coastguard Worker .test_all = verify_inotify,
186*49cdfc7eSAndroid Build Coastguard Worker };
187*49cdfc7eSAndroid Build Coastguard Worker
188*49cdfc7eSAndroid Build Coastguard Worker #else
189*49cdfc7eSAndroid Build Coastguard Worker TST_TEST_TCONF("system doesn't have required inotify support");
190*49cdfc7eSAndroid Build Coastguard Worker #endif
191