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