b24413180f560 (Greg Kroah-Hartman 2017-11-01 15:07:57 +0100 1) // SPDX-License-Identifier: GPL-2.0
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 2) #include <linux/file.h>
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 3) #include <linux/fs.h>
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 4) #include <linux/fsnotify_backend.h>
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 5) #include <linux/idr.h>
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 6) #include <linux/init.h>
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 7) #include <linux/inotify.h>
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 8) #include <linux/fanotify.h>
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 9) #include <linux/kernel.h>
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 10) #include <linux/namei.h>
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 11) #include <linux/sched.h>
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 12) #include <linux/types.h>
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 13) #include <linux/seq_file.h>
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 14) #include <linux/exportfs.h>
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 15)
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 16) #include "inotify/inotify.h"
d6f7aa9820f21 (Eric Biggers 2018-11-14 16:12:14 -0800 17) #include "fdinfo.h"
36f10f55ff1d2 (Amir Goldstein 2018-06-23 17:54:49 +0300 18) #include "fsnotify.h"
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 19)
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 20) #if defined(CONFIG_PROC_FS)
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 21)
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 22) #if defined(CONFIG_INOTIFY_USER) || defined(CONFIG_FANOTIFY)
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 23)
a3816ab0e8fe5 (Joe Perches 2014-09-29 16:08:25 -0700 24) static void show_fdinfo(struct seq_file *m, struct file *f,
a3816ab0e8fe5 (Joe Perches 2014-09-29 16:08:25 -0700 25) void (*show)(struct seq_file *m,
a3816ab0e8fe5 (Joe Perches 2014-09-29 16:08:25 -0700 26) struct fsnotify_mark *mark))
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 27) {
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 28) struct fsnotify_group *group = f->private_data;
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 29) struct fsnotify_mark *mark;
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 30)
96680d2b91746 (Linus Torvalds 2012-12-20 20:11:52 -0800 31) mutex_lock(&group->mark_mutex);
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 32) list_for_each_entry(mark, &group->marks_list, g_list) {
a3816ab0e8fe5 (Joe Perches 2014-09-29 16:08:25 -0700 33) show(m, mark);
a3816ab0e8fe5 (Joe Perches 2014-09-29 16:08:25 -0700 34) if (seq_has_overflowed(m))
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 35) break;
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 36) }
96680d2b91746 (Linus Torvalds 2012-12-20 20:11:52 -0800 37) mutex_unlock(&group->mark_mutex);
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 38) }
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 39)
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 40) #if defined(CONFIG_EXPORTFS)
a3816ab0e8fe5 (Joe Perches 2014-09-29 16:08:25 -0700 41) static void show_mark_fhandle(struct seq_file *m, struct inode *inode)
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 42) {
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 43) struct {
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 44) struct file_handle handle;
1fc98d11cac6d (Andrey Vagin 2014-09-09 14:51:04 -0700 45) u8 pad[MAX_HANDLE_SZ];
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 46) } f;
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 47) int size, ret, i;
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 48)
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 49) f.handle.handle_bytes = sizeof(f.pad);
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 50) size = f.handle.handle_bytes >> 2;
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 51)
ddd06c36bdb3c (Ben Dooks (Codethink) 2019-10-16 10:59:55 +0100 52) ret = exportfs_encode_inode_fh(inode, (struct fid *)f.handle.f_handle, &size, NULL);
7e8824816bda1 (Andrey Vagin 2014-09-09 14:51:06 -0700 53) if ((ret == FILEID_INVALID) || (ret < 0)) {
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 54) WARN_ONCE(1, "Can't encode file handler for inotify: %d\n", ret);
a3816ab0e8fe5 (Joe Perches 2014-09-29 16:08:25 -0700 55) return;
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 56) }
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 57)
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 58) f.handle.handle_type = ret;
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 59) f.handle.handle_bytes = size * sizeof(u32);
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 60)
a3816ab0e8fe5 (Joe Perches 2014-09-29 16:08:25 -0700 61) seq_printf(m, "fhandle-bytes:%x fhandle-type:%x f_handle:",
a3816ab0e8fe5 (Joe Perches 2014-09-29 16:08:25 -0700 62) f.handle.handle_bytes, f.handle.handle_type);
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 63)
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 64) for (i = 0; i < f.handle.handle_bytes; i++)
a3816ab0e8fe5 (Joe Perches 2014-09-29 16:08:25 -0700 65) seq_printf(m, "%02x", (int)f.handle.f_handle[i]);
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 66) }
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 67) #else
a3816ab0e8fe5 (Joe Perches 2014-09-29 16:08:25 -0700 68) static void show_mark_fhandle(struct seq_file *m, struct inode *inode)
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 69) {
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 70) }
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 71) #endif
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 72)
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 73) #ifdef CONFIG_INOTIFY_USER
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 74)
a3816ab0e8fe5 (Joe Perches 2014-09-29 16:08:25 -0700 75) static void inotify_fdinfo(struct seq_file *m, struct fsnotify_mark *mark)
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 76) {
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 77) struct inotify_inode_mark *inode_mark;
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 78) struct inode *inode;
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 79)
d6f7b98bc8147 (Amir Goldstein 2018-04-20 16:10:49 -0700 80) if (mark->connector->type != FSNOTIFY_OBJ_TYPE_INODE)
a3816ab0e8fe5 (Joe Perches 2014-09-29 16:08:25 -0700 81) return;
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 82)
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 83) inode_mark = container_of(mark, struct inotify_inode_mark, fsn_mark);
36f10f55ff1d2 (Amir Goldstein 2018-06-23 17:54:49 +0300 84) inode = igrab(fsnotify_conn_inode(mark->connector));
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 85) if (inode) {
6933599697c96 (Dave Hansen 2015-11-05 18:43:43 -0800 86) /*
6933599697c96 (Dave Hansen 2015-11-05 18:43:43 -0800 87) * IN_ALL_EVENTS represents all of the mask bits
6933599697c96 (Dave Hansen 2015-11-05 18:43:43 -0800 88) * that we expose to userspace. There is at
6933599697c96 (Dave Hansen 2015-11-05 18:43:43 -0800 89) * least one bit (FS_EVENT_ON_CHILD) which is
6933599697c96 (Dave Hansen 2015-11-05 18:43:43 -0800 90) * used only internally to the kernel.
6933599697c96 (Dave Hansen 2015-11-05 18:43:43 -0800 91) */
6933599697c96 (Dave Hansen 2015-11-05 18:43:43 -0800 92) u32 mask = mark->mask & IN_ALL_EVENTS;
a3816ab0e8fe5 (Joe Perches 2014-09-29 16:08:25 -0700 93) seq_printf(m, "inotify wd:%x ino:%lx sdev:%x mask:%x ignored_mask:%x ",
a3816ab0e8fe5 (Joe Perches 2014-09-29 16:08:25 -0700 94) inode_mark->wd, inode->i_ino, inode->i_sb->s_dev,
6933599697c96 (Dave Hansen 2015-11-05 18:43:43 -0800 95) mask, mark->ignored_mask);
a3816ab0e8fe5 (Joe Perches 2014-09-29 16:08:25 -0700 96) show_mark_fhandle(m, inode);
a3816ab0e8fe5 (Joe Perches 2014-09-29 16:08:25 -0700 97) seq_putc(m, '\n');
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 98) iput(inode);
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 99) }
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 100) }
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 101)
a3816ab0e8fe5 (Joe Perches 2014-09-29 16:08:25 -0700 102) void inotify_show_fdinfo(struct seq_file *m, struct file *f)
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 103) {
a3816ab0e8fe5 (Joe Perches 2014-09-29 16:08:25 -0700 104) show_fdinfo(m, f, inotify_fdinfo);
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 105) }
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 106)
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 107) #endif /* CONFIG_INOTIFY_USER */
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 108)
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 109) #ifdef CONFIG_FANOTIFY
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 110)
a3816ab0e8fe5 (Joe Perches 2014-09-29 16:08:25 -0700 111) static void fanotify_fdinfo(struct seq_file *m, struct fsnotify_mark *mark)
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 112) {
e6dbcafb744ab (Cyrill Gorcunov 2012-12-17 16:05:16 -0800 113) unsigned int mflags = 0;
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 114) struct inode *inode;
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 115)
e6dbcafb744ab (Cyrill Gorcunov 2012-12-17 16:05:16 -0800 116) if (mark->flags & FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY)
e6dbcafb744ab (Cyrill Gorcunov 2012-12-17 16:05:16 -0800 117) mflags |= FAN_MARK_IGNORED_SURV_MODIFY;
e6dbcafb744ab (Cyrill Gorcunov 2012-12-17 16:05:16 -0800 118)
d6f7b98bc8147 (Amir Goldstein 2018-04-20 16:10:49 -0700 119) if (mark->connector->type == FSNOTIFY_OBJ_TYPE_INODE) {
36f10f55ff1d2 (Amir Goldstein 2018-06-23 17:54:49 +0300 120) inode = igrab(fsnotify_conn_inode(mark->connector));
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 121) if (!inode)
a3816ab0e8fe5 (Joe Perches 2014-09-29 16:08:25 -0700 122) return;
a3816ab0e8fe5 (Joe Perches 2014-09-29 16:08:25 -0700 123) seq_printf(m, "fanotify ino:%lx sdev:%x mflags:%x mask:%x ignored_mask:%x ",
a3816ab0e8fe5 (Joe Perches 2014-09-29 16:08:25 -0700 124) inode->i_ino, inode->i_sb->s_dev,
a3816ab0e8fe5 (Joe Perches 2014-09-29 16:08:25 -0700 125) mflags, mark->mask, mark->ignored_mask);
a3816ab0e8fe5 (Joe Perches 2014-09-29 16:08:25 -0700 126) show_mark_fhandle(m, inode);
a3816ab0e8fe5 (Joe Perches 2014-09-29 16:08:25 -0700 127) seq_putc(m, '\n');
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 128) iput(inode);
d6f7b98bc8147 (Amir Goldstein 2018-04-20 16:10:49 -0700 129) } else if (mark->connector->type == FSNOTIFY_OBJ_TYPE_VFSMOUNT) {
36f10f55ff1d2 (Amir Goldstein 2018-06-23 17:54:49 +0300 130) struct mount *mnt = fsnotify_conn_mount(mark->connector);
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 131)
a3816ab0e8fe5 (Joe Perches 2014-09-29 16:08:25 -0700 132) seq_printf(m, "fanotify mnt_id:%x mflags:%x mask:%x ignored_mask:%x\n",
a3816ab0e8fe5 (Joe Perches 2014-09-29 16:08:25 -0700 133) mnt->mnt_id, mflags, mark->mask, mark->ignored_mask);
1e6cb72399fd5 (Amir Goldstein 2018-09-01 10:41:11 +0300 134) } else if (mark->connector->type == FSNOTIFY_OBJ_TYPE_SB) {
1e6cb72399fd5 (Amir Goldstein 2018-09-01 10:41:11 +0300 135) struct super_block *sb = fsnotify_conn_sb(mark->connector);
1e6cb72399fd5 (Amir Goldstein 2018-09-01 10:41:11 +0300 136)
1e6cb72399fd5 (Amir Goldstein 2018-09-01 10:41:11 +0300 137) seq_printf(m, "fanotify sdev:%x mflags:%x mask:%x ignored_mask:%x\n",
1e6cb72399fd5 (Amir Goldstein 2018-09-01 10:41:11 +0300 138) sb->s_dev, mflags, mark->mask, mark->ignored_mask);
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 139) }
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 140) }
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 141)
a3816ab0e8fe5 (Joe Perches 2014-09-29 16:08:25 -0700 142) void fanotify_show_fdinfo(struct seq_file *m, struct file *f)
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 143) {
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 144) struct fsnotify_group *group = f->private_data;
de8cd83e91bc3 (Steve Grubb 2017-10-02 20:21:39 -0400 145)
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 146) seq_printf(m, "fanotify flags:%x event-flags:%x\n",
a8b98c808eab3 (Amir Goldstein 2021-05-24 16:53:21 +0300 147) group->fanotify_data.flags & FANOTIFY_INIT_FLAGS,
7cea2a3c505e8 (Amir Goldstein 2021-03-04 13:29:21 +0200 148) group->fanotify_data.f_flags);
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 149)
a3816ab0e8fe5 (Joe Perches 2014-09-29 16:08:25 -0700 150) show_fdinfo(m, f, fanotify_fdinfo);
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 151) }
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 152)
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 153) #endif /* CONFIG_FANOTIFY */
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 154)
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 155) #endif /* CONFIG_INOTIFY_USER || CONFIG_FANOTIFY */
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 156)
be77196b809cd (Cyrill Gorcunov 2012-12-17 16:05:12 -0800 157) #endif /* CONFIG_PROC_FS */