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