1 /*
2 * Copyright (c) 2018 CTERA Networks. All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 or any later of the GNU General Public License
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 * Started by Amir Goldstein <amir73il@gmail.com>
24 *
25 * DESCRIPTION
26 * Check that inotify work for an overlayfs directory after copy up and
27 * drop caches.
28 *
29 * An inotify watch pins the directory inode in cache, but not the dentry.
30 * The watch will not report events on the directory if overlayfs does not
31 * obtain the pinned inode to the new allocated dentry after drop caches.
32 *
33 * The problem has been fixed by commit:
34 * 31747eda41ef "ovl: hash directory inodes for fsnotify"
35 *
36 * ALGORITHM
37 * Add watch on an overlayfs lower directory then chmod directory and drop
38 * dentry and inode caches. Execute operations on directory and child and
39 * expect events to be reported on directory watch.
40 */
41
42 #include "config.h"
43
44 #if defined(HAVE_SYS_INOTIFY_H)
45 # include <sys/inotify.h>
46 #endif
47 #include <stdio.h>
48 #include <sys/stat.h>
49 #include <sys/types.h>
50 #include <fcntl.h>
51 #include <errno.h>
52 #include <string.h>
53 #include <sys/syscall.h>
54 #include <sys/mount.h>
55 #include <limits.h>
56 #include "tst_test.h"
57 #include "inotify.h"
58
59 #if defined(HAVE_SYS_INOTIFY_H)
60
61 #define EVENT_MAX 1024
62 /* size of the event structure, not counting name */
63 #define EVENT_SIZE (sizeof (struct inotify_event))
64 /* reasonable guess as to size of 1024 events */
65 #define EVENT_BUF_LEN (EVENT_MAX * (EVENT_SIZE + 16))
66
67 #define BUF_SIZE 256
68 static int fd_notify, reap_wd;
69 static int wd;
70
71 struct event_t {
72 char name[BUF_SIZE];
73 unsigned int mask;
74 };
75
76 #define OVL_MNT "ovl"
77 #define DIR_NAME "test_dir"
78 #define DIR_PATH OVL_MNT"/"DIR_NAME
79 #define FILE_NAME "test_file"
80 #define FILE_PATH OVL_MNT"/"DIR_NAME"/"FILE_NAME
81
82 static int ovl_mounted;
83
84 static struct event_t event_set[EVENT_MAX];
85
86 static char event_buf[EVENT_BUF_LEN];
87
verify_inotify(void)88 void verify_inotify(void)
89 {
90 int test_cnt = 0;
91
92 /*
93 * generate sequence of events
94 */
95 SAFE_CHMOD(DIR_PATH, 0755);
96 event_set[test_cnt].mask = IN_ISDIR | IN_ATTRIB;
97 strcpy(event_set[test_cnt].name, "");
98 test_cnt++;
99
100 SAFE_TOUCH(FILE_PATH, 0644, NULL);
101 event_set[test_cnt].mask = IN_OPEN;
102 strcpy(event_set[test_cnt].name, FILE_NAME);
103 test_cnt++;
104 event_set[test_cnt].mask = IN_CLOSE_WRITE;
105 strcpy(event_set[test_cnt].name, FILE_NAME);
106 test_cnt++;
107 event_set[test_cnt].mask = IN_ATTRIB;
108 strcpy(event_set[test_cnt].name, FILE_NAME);
109 test_cnt++;
110
111 int len = read(fd_notify, event_buf, EVENT_BUF_LEN);
112 if (len == -1 && errno != EAGAIN) {
113 tst_brk(TBROK | TERRNO,
114 "read(%d, buf, %zu) failed",
115 fd_notify, EVENT_BUF_LEN);
116 }
117
118 int i = 0, test_num = 0;
119 while (i < len) {
120 struct inotify_event *event;
121 event = (struct inotify_event *)&event_buf[i];
122 if (test_num >= test_cnt) {
123 tst_res(TFAIL,
124 "get unnecessary event: "
125 "wd=%d mask=%08x cookie=%-5u len=%-2u"
126 "name=\"%.*s\"", event->wd, event->mask,
127 event->cookie, event->len, event->len,
128 event->name);
129 } else if ((event_set[test_num].mask == event->mask)
130 &&
131 (!strncmp
132 (event_set[test_num].name, event->name,
133 event->len))) {
134 tst_res(TPASS,
135 "get event: wd=%d mask=%08x "
136 "cookie=%-5u len=%-2u name=\"%.*s\"",
137 event->wd, event->mask,
138 event->cookie, event->len,
139 event->len, event->name);
140 } else {
141 tst_res(TFAIL, "get event: wd=%d mask=%08x "
142 "(expected %x) cookie=%-5u len=%-2u "
143 "name=\"%.*s\" (expected \"%s\") %d",
144 event->wd, event->mask,
145 event_set[test_num].mask,
146 event->cookie, event->len, event->len,
147 event->name, event_set[test_num].name,
148 strcmp(event_set[test_num].name,
149 event->name));
150 }
151 test_num++;
152 i += EVENT_SIZE + event->len;
153 }
154
155 for (; test_num < test_cnt; test_num++) {
156 tst_res(TFAIL, "didn't get event: mask=%08x ",
157 event_set[test_num].mask);
158 }
159 }
160
setup(void)161 static void setup(void)
162 {
163 struct stat buf;
164 int ret;
165
166 /* Setup an overlay mount with lower dir and file */
167 SAFE_MKDIR("lower", 0755);
168 SAFE_MKDIR("lower/"DIR_NAME, 0755);
169 SAFE_TOUCH("lower/"DIR_NAME"/"FILE_NAME, 0644, NULL);
170 SAFE_MKDIR("upper", 0755);
171 SAFE_MKDIR("work", 0755);
172 SAFE_MKDIR(OVL_MNT, 0755);
173 ret = mount("overlay", OVL_MNT, "overlay", 0,
174 "lowerdir=lower,upperdir=upper,workdir=work");
175 if (ret < 0) {
176 if (errno == ENODEV) {
177 tst_brk(TCONF,
178 "overlayfs is not configured in this kernel.");
179 } else {
180 tst_brk(TBROK | TERRNO,
181 "overlayfs mount failed");
182 }
183 }
184 ovl_mounted = 1;
185
186 fd_notify = myinotify_init1(O_NONBLOCK);
187 if (fd_notify < 0) {
188 if (errno == ENOSYS) {
189 tst_brk(TCONF,
190 "inotify is not configured in this kernel.");
191 } else {
192 tst_brk(TBROK | TERRNO,
193 "inotify_init () failed");
194 }
195 }
196
197 /* Setup a watch on an overlayfs lower directory */
198 if ((wd = myinotify_add_watch(fd_notify, DIR_PATH, IN_ALL_EVENTS)) < 0) {
199 tst_brk(TBROK | TERRNO,
200 "inotify_add_watch (%d, " DIR_PATH ", IN_ALL_EVENTS) failed",
201 fd_notify);
202 reap_wd = 1;
203 };
204
205 SAFE_STAT(DIR_PATH, &buf);
206 tst_res(TINFO, DIR_PATH " ino=%lu", buf.st_ino);
207
208 /* Drop dentry caches, so overlayfs will allocate a new dentry */
209 SAFE_FILE_PRINTF("/proc/sys/vm/drop_caches", "2");
210
211 /* Copy up directory to make it a merge directory */
212 SAFE_CHMOD(DIR_PATH, 0700);
213
214 /* Lookup directory and see if we got the watched directory inode */
215 SAFE_STAT(DIR_PATH, &buf);
216 tst_res(TINFO, DIR_PATH " ino=%lu", buf.st_ino);
217 }
218
cleanup(void)219 static void cleanup(void)
220 {
221 if (reap_wd && myinotify_rm_watch(fd_notify, wd) < 0) {
222 tst_res(TWARN,
223 "inotify_rm_watch (%d, %d) failed,", fd_notify, wd);
224
225 }
226
227 if (fd_notify > 0)
228 SAFE_CLOSE(fd_notify);
229
230 if (ovl_mounted)
231 SAFE_UMOUNT(OVL_MNT);
232 }
233
234 static struct tst_test test = {
235 .needs_root = 1,
236 .needs_tmpdir = 1,
237 .setup = setup,
238 .cleanup = cleanup,
239 .test_all = verify_inotify,
240 };
241
242 #else
243 TST_TEST_TCONF("system doesn't have required inotify support");
244 #endif
245