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