blob: ef83f4020554f1b45c273e6a5a1afab38828d86a [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Cyrill Gorcunovbe771962012-12-17 16:05:12 -08002#include <linux/file.h>
3#include <linux/fs.h>
4#include <linux/fsnotify_backend.h>
5#include <linux/idr.h>
6#include <linux/init.h>
7#include <linux/inotify.h>
8#include <linux/fanotify.h>
9#include <linux/kernel.h>
10#include <linux/namei.h>
11#include <linux/sched.h>
12#include <linux/types.h>
13#include <linux/seq_file.h>
14#include <linux/proc_fs.h>
15#include <linux/exportfs.h>
16
17#include "inotify/inotify.h"
Eric Biggersd6f7aa92018-11-14 16:12:14 -080018#include "fdinfo.h"
Amir Goldstein36f10f52018-06-23 17:54:49 +030019#include "fsnotify.h"
Cyrill Gorcunovbe771962012-12-17 16:05:12 -080020
21#if defined(CONFIG_PROC_FS)
22
23#if defined(CONFIG_INOTIFY_USER) || defined(CONFIG_FANOTIFY)
24
Joe Perchesa3816ab2014-09-29 16:08:25 -070025static void show_fdinfo(struct seq_file *m, struct file *f,
26 void (*show)(struct seq_file *m,
27 struct fsnotify_mark *mark))
Cyrill Gorcunovbe771962012-12-17 16:05:12 -080028{
29 struct fsnotify_group *group = f->private_data;
30 struct fsnotify_mark *mark;
Cyrill Gorcunovbe771962012-12-17 16:05:12 -080031
Linus Torvalds96680d22012-12-20 20:11:52 -080032 mutex_lock(&group->mark_mutex);
Cyrill Gorcunovbe771962012-12-17 16:05:12 -080033 list_for_each_entry(mark, &group->marks_list, g_list) {
Joe Perchesa3816ab2014-09-29 16:08:25 -070034 show(m, mark);
35 if (seq_has_overflowed(m))
Cyrill Gorcunovbe771962012-12-17 16:05:12 -080036 break;
37 }
Linus Torvalds96680d22012-12-20 20:11:52 -080038 mutex_unlock(&group->mark_mutex);
Cyrill Gorcunovbe771962012-12-17 16:05:12 -080039}
40
41#if defined(CONFIG_EXPORTFS)
Joe Perchesa3816ab2014-09-29 16:08:25 -070042static void show_mark_fhandle(struct seq_file *m, struct inode *inode)
Cyrill Gorcunovbe771962012-12-17 16:05:12 -080043{
44 struct {
45 struct file_handle handle;
Andrey Vagin1fc98d12014-09-09 14:51:04 -070046 u8 pad[MAX_HANDLE_SZ];
Cyrill Gorcunovbe771962012-12-17 16:05:12 -080047 } f;
48 int size, ret, i;
49
50 f.handle.handle_bytes = sizeof(f.pad);
51 size = f.handle.handle_bytes >> 2;
52
Ben Dooks (Codethink)ddd06c32019-10-16 10:59:55 +010053 ret = exportfs_encode_inode_fh(inode, (struct fid *)f.handle.f_handle, &size, NULL);
Andrey Vagin7e882482014-09-09 14:51:06 -070054 if ((ret == FILEID_INVALID) || (ret < 0)) {
Cyrill Gorcunovbe771962012-12-17 16:05:12 -080055 WARN_ONCE(1, "Can't encode file handler for inotify: %d\n", ret);
Joe Perchesa3816ab2014-09-29 16:08:25 -070056 return;
Cyrill Gorcunovbe771962012-12-17 16:05:12 -080057 }
58
59 f.handle.handle_type = ret;
60 f.handle.handle_bytes = size * sizeof(u32);
61
Joe Perchesa3816ab2014-09-29 16:08:25 -070062 seq_printf(m, "fhandle-bytes:%x fhandle-type:%x f_handle:",
63 f.handle.handle_bytes, f.handle.handle_type);
Cyrill Gorcunovbe771962012-12-17 16:05:12 -080064
65 for (i = 0; i < f.handle.handle_bytes; i++)
Joe Perchesa3816ab2014-09-29 16:08:25 -070066 seq_printf(m, "%02x", (int)f.handle.f_handle[i]);
Cyrill Gorcunovbe771962012-12-17 16:05:12 -080067}
68#else
Joe Perchesa3816ab2014-09-29 16:08:25 -070069static void show_mark_fhandle(struct seq_file *m, struct inode *inode)
Cyrill Gorcunovbe771962012-12-17 16:05:12 -080070{
Cyrill Gorcunovbe771962012-12-17 16:05:12 -080071}
72#endif
73
74#ifdef CONFIG_INOTIFY_USER
75
Joe Perchesa3816ab2014-09-29 16:08:25 -070076static void inotify_fdinfo(struct seq_file *m, struct fsnotify_mark *mark)
Cyrill Gorcunovbe771962012-12-17 16:05:12 -080077{
78 struct inotify_inode_mark *inode_mark;
79 struct inode *inode;
Cyrill Gorcunovbe771962012-12-17 16:05:12 -080080
Amir Goldsteind6f7b982018-04-20 16:10:49 -070081 if (mark->connector->type != FSNOTIFY_OBJ_TYPE_INODE)
Joe Perchesa3816ab2014-09-29 16:08:25 -070082 return;
Cyrill Gorcunovbe771962012-12-17 16:05:12 -080083
84 inode_mark = container_of(mark, struct inotify_inode_mark, fsn_mark);
Amir Goldstein36f10f52018-06-23 17:54:49 +030085 inode = igrab(fsnotify_conn_inode(mark->connector));
Cyrill Gorcunovbe771962012-12-17 16:05:12 -080086 if (inode) {
Dave Hansen69335992015-11-05 18:43:43 -080087 /*
88 * IN_ALL_EVENTS represents all of the mask bits
89 * that we expose to userspace. There is at
90 * least one bit (FS_EVENT_ON_CHILD) which is
91 * used only internally to the kernel.
92 */
93 u32 mask = mark->mask & IN_ALL_EVENTS;
Joe Perchesa3816ab2014-09-29 16:08:25 -070094 seq_printf(m, "inotify wd:%x ino:%lx sdev:%x mask:%x ignored_mask:%x ",
95 inode_mark->wd, inode->i_ino, inode->i_sb->s_dev,
Dave Hansen69335992015-11-05 18:43:43 -080096 mask, mark->ignored_mask);
Joe Perchesa3816ab2014-09-29 16:08:25 -070097 show_mark_fhandle(m, inode);
98 seq_putc(m, '\n');
Cyrill Gorcunovbe771962012-12-17 16:05:12 -080099 iput(inode);
100 }
Cyrill Gorcunovbe771962012-12-17 16:05:12 -0800101}
102
Joe Perchesa3816ab2014-09-29 16:08:25 -0700103void inotify_show_fdinfo(struct seq_file *m, struct file *f)
Cyrill Gorcunovbe771962012-12-17 16:05:12 -0800104{
Joe Perchesa3816ab2014-09-29 16:08:25 -0700105 show_fdinfo(m, f, inotify_fdinfo);
Cyrill Gorcunovbe771962012-12-17 16:05:12 -0800106}
107
108#endif /* CONFIG_INOTIFY_USER */
109
110#ifdef CONFIG_FANOTIFY
111
Joe Perchesa3816ab2014-09-29 16:08:25 -0700112static void fanotify_fdinfo(struct seq_file *m, struct fsnotify_mark *mark)
Cyrill Gorcunovbe771962012-12-17 16:05:12 -0800113{
Cyrill Gorcunove6dbcaf2012-12-17 16:05:16 -0800114 unsigned int mflags = 0;
Cyrill Gorcunovbe771962012-12-17 16:05:12 -0800115 struct inode *inode;
Cyrill Gorcunovbe771962012-12-17 16:05:12 -0800116
Cyrill Gorcunove6dbcaf2012-12-17 16:05:16 -0800117 if (mark->flags & FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY)
118 mflags |= FAN_MARK_IGNORED_SURV_MODIFY;
119
Amir Goldsteind6f7b982018-04-20 16:10:49 -0700120 if (mark->connector->type == FSNOTIFY_OBJ_TYPE_INODE) {
Amir Goldstein36f10f52018-06-23 17:54:49 +0300121 inode = igrab(fsnotify_conn_inode(mark->connector));
Cyrill Gorcunovbe771962012-12-17 16:05:12 -0800122 if (!inode)
Joe Perchesa3816ab2014-09-29 16:08:25 -0700123 return;
124 seq_printf(m, "fanotify ino:%lx sdev:%x mflags:%x mask:%x ignored_mask:%x ",
125 inode->i_ino, inode->i_sb->s_dev,
126 mflags, mark->mask, mark->ignored_mask);
127 show_mark_fhandle(m, inode);
128 seq_putc(m, '\n');
Cyrill Gorcunovbe771962012-12-17 16:05:12 -0800129 iput(inode);
Amir Goldsteind6f7b982018-04-20 16:10:49 -0700130 } else if (mark->connector->type == FSNOTIFY_OBJ_TYPE_VFSMOUNT) {
Amir Goldstein36f10f52018-06-23 17:54:49 +0300131 struct mount *mnt = fsnotify_conn_mount(mark->connector);
Cyrill Gorcunovbe771962012-12-17 16:05:12 -0800132
Joe Perchesa3816ab2014-09-29 16:08:25 -0700133 seq_printf(m, "fanotify mnt_id:%x mflags:%x mask:%x ignored_mask:%x\n",
134 mnt->mnt_id, mflags, mark->mask, mark->ignored_mask);
Amir Goldstein1e6cb722018-09-01 10:41:11 +0300135 } else if (mark->connector->type == FSNOTIFY_OBJ_TYPE_SB) {
136 struct super_block *sb = fsnotify_conn_sb(mark->connector);
137
138 seq_printf(m, "fanotify sdev:%x mflags:%x mask:%x ignored_mask:%x\n",
139 sb->s_dev, mflags, mark->mask, mark->ignored_mask);
Cyrill Gorcunovbe771962012-12-17 16:05:12 -0800140 }
Cyrill Gorcunovbe771962012-12-17 16:05:12 -0800141}
142
Joe Perchesa3816ab2014-09-29 16:08:25 -0700143void fanotify_show_fdinfo(struct seq_file *m, struct file *f)
Cyrill Gorcunovbe771962012-12-17 16:05:12 -0800144{
145 struct fsnotify_group *group = f->private_data;
Steve Grubbde8cd832017-10-02 20:21:39 -0400146
Cyrill Gorcunovbe771962012-12-17 16:05:12 -0800147 seq_printf(m, "fanotify flags:%x event-flags:%x\n",
Amir Goldstein96a71f22018-09-21 21:20:30 +0300148 group->fanotify_data.flags, group->fanotify_data.f_flags);
Cyrill Gorcunovbe771962012-12-17 16:05:12 -0800149
Joe Perchesa3816ab2014-09-29 16:08:25 -0700150 show_fdinfo(m, f, fanotify_fdinfo);
Cyrill Gorcunovbe771962012-12-17 16:05:12 -0800151}
152
153#endif /* CONFIG_FANOTIFY */
154
155#endif /* CONFIG_INOTIFY_USER || CONFIG_FANOTIFY */
156
157#endif /* CONFIG_PROC_FS */