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