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