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