blob: d478629c728beabe160468903fe59ca7500c2f77 [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"
18#include "../fs/mount.h"
19
20#if defined(CONFIG_PROC_FS)
21
22#if defined(CONFIG_INOTIFY_USER) || defined(CONFIG_FANOTIFY)
23
Joe Perchesa3816ab2014-09-29 16:08:25 -070024static void show_fdinfo(struct seq_file *m, struct file *f,
25 void (*show)(struct seq_file *m,
26 struct fsnotify_mark *mark))
Cyrill Gorcunovbe771962012-12-17 16:05:12 -080027{
28 struct fsnotify_group *group = f->private_data;
29 struct fsnotify_mark *mark;
Cyrill Gorcunovbe771962012-12-17 16:05:12 -080030
Linus Torvalds96680d22012-12-20 20:11:52 -080031 mutex_lock(&group->mark_mutex);
Cyrill Gorcunovbe771962012-12-17 16:05:12 -080032 list_for_each_entry(mark, &group->marks_list, g_list) {
Joe Perchesa3816ab2014-09-29 16:08:25 -070033 show(m, mark);
34 if (seq_has_overflowed(m))
Cyrill Gorcunovbe771962012-12-17 16:05:12 -080035 break;
36 }
Linus Torvalds96680d22012-12-20 20:11:52 -080037 mutex_unlock(&group->mark_mutex);
Cyrill Gorcunovbe771962012-12-17 16:05:12 -080038}
39
40#if defined(CONFIG_EXPORTFS)
Joe Perchesa3816ab2014-09-29 16:08:25 -070041static void show_mark_fhandle(struct seq_file *m, struct inode *inode)
Cyrill Gorcunovbe771962012-12-17 16:05:12 -080042{
43 struct {
44 struct file_handle handle;
Andrey Vagin1fc98d12014-09-09 14:51:04 -070045 u8 pad[MAX_HANDLE_SZ];
Cyrill Gorcunovbe771962012-12-17 16:05:12 -080046 } f;
47 int size, ret, i;
48
49 f.handle.handle_bytes = sizeof(f.pad);
50 size = f.handle.handle_bytes >> 2;
51
52 ret = exportfs_encode_inode_fh(inode, (struct fid *)f.handle.f_handle, &size, 0);
Andrey Vagin7e882482014-09-09 14:51:06 -070053 if ((ret == FILEID_INVALID) || (ret < 0)) {
Cyrill Gorcunovbe771962012-12-17 16:05:12 -080054 WARN_ONCE(1, "Can't encode file handler for inotify: %d\n", ret);
Joe Perchesa3816ab2014-09-29 16:08:25 -070055 return;
Cyrill Gorcunovbe771962012-12-17 16:05:12 -080056 }
57
58 f.handle.handle_type = ret;
59 f.handle.handle_bytes = size * sizeof(u32);
60
Joe Perchesa3816ab2014-09-29 16:08:25 -070061 seq_printf(m, "fhandle-bytes:%x fhandle-type:%x f_handle:",
62 f.handle.handle_bytes, f.handle.handle_type);
Cyrill Gorcunovbe771962012-12-17 16:05:12 -080063
64 for (i = 0; i < f.handle.handle_bytes; i++)
Joe Perchesa3816ab2014-09-29 16:08:25 -070065 seq_printf(m, "%02x", (int)f.handle.f_handle[i]);
Cyrill Gorcunovbe771962012-12-17 16:05:12 -080066}
67#else
Joe Perchesa3816ab2014-09-29 16:08:25 -070068static void show_mark_fhandle(struct seq_file *m, struct inode *inode)
Cyrill Gorcunovbe771962012-12-17 16:05:12 -080069{
Cyrill Gorcunovbe771962012-12-17 16:05:12 -080070}
71#endif
72
73#ifdef CONFIG_INOTIFY_USER
74
Joe Perchesa3816ab2014-09-29 16:08:25 -070075static void inotify_fdinfo(struct seq_file *m, struct fsnotify_mark *mark)
Cyrill Gorcunovbe771962012-12-17 16:05:12 -080076{
77 struct inotify_inode_mark *inode_mark;
78 struct inode *inode;
Cyrill Gorcunovbe771962012-12-17 16:05:12 -080079
Jan Kara86ffe242017-03-14 14:29:35 +010080 if (!(mark->connector->flags & FSNOTIFY_OBJ_TYPE_INODE))
Joe Perchesa3816ab2014-09-29 16:08:25 -070081 return;
Cyrill Gorcunovbe771962012-12-17 16:05:12 -080082
83 inode_mark = container_of(mark, struct inotify_inode_mark, fsn_mark);
Jan Kara86ffe242017-03-14 14:29:35 +010084 inode = igrab(mark->connector->inode);
Cyrill Gorcunovbe771962012-12-17 16:05:12 -080085 if (inode) {
Dave Hansen69335992015-11-05 18:43:43 -080086 /*
87 * IN_ALL_EVENTS represents all of the mask bits
88 * that we expose to userspace. There is at
89 * least one bit (FS_EVENT_ON_CHILD) which is
90 * used only internally to the kernel.
91 */
92 u32 mask = mark->mask & IN_ALL_EVENTS;
Joe Perchesa3816ab2014-09-29 16:08:25 -070093 seq_printf(m, "inotify wd:%x ino:%lx sdev:%x mask:%x ignored_mask:%x ",
94 inode_mark->wd, inode->i_ino, inode->i_sb->s_dev,
Dave Hansen69335992015-11-05 18:43:43 -080095 mask, mark->ignored_mask);
Joe Perchesa3816ab2014-09-29 16:08:25 -070096 show_mark_fhandle(m, inode);
97 seq_putc(m, '\n');
Cyrill Gorcunovbe771962012-12-17 16:05:12 -080098 iput(inode);
99 }
Cyrill Gorcunovbe771962012-12-17 16:05:12 -0800100}
101
Joe Perchesa3816ab2014-09-29 16:08:25 -0700102void inotify_show_fdinfo(struct seq_file *m, struct file *f)
Cyrill Gorcunovbe771962012-12-17 16:05:12 -0800103{
Joe Perchesa3816ab2014-09-29 16:08:25 -0700104 show_fdinfo(m, f, inotify_fdinfo);
Cyrill Gorcunovbe771962012-12-17 16:05:12 -0800105}
106
107#endif /* CONFIG_INOTIFY_USER */
108
109#ifdef CONFIG_FANOTIFY
110
Joe Perchesa3816ab2014-09-29 16:08:25 -0700111static void fanotify_fdinfo(struct seq_file *m, struct fsnotify_mark *mark)
Cyrill Gorcunovbe771962012-12-17 16:05:12 -0800112{
Cyrill Gorcunove6dbcaf2012-12-17 16:05:16 -0800113 unsigned int mflags = 0;
Cyrill Gorcunovbe771962012-12-17 16:05:12 -0800114 struct inode *inode;
Cyrill Gorcunovbe771962012-12-17 16:05:12 -0800115
Cyrill Gorcunove6dbcaf2012-12-17 16:05:16 -0800116 if (mark->flags & FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY)
117 mflags |= FAN_MARK_IGNORED_SURV_MODIFY;
118
Jan Kara86ffe242017-03-14 14:29:35 +0100119 if (mark->connector->flags & FSNOTIFY_OBJ_TYPE_INODE) {
120 inode = igrab(mark->connector->inode);
Cyrill Gorcunovbe771962012-12-17 16:05:12 -0800121 if (!inode)
Joe Perchesa3816ab2014-09-29 16:08:25 -0700122 return;
123 seq_printf(m, "fanotify ino:%lx sdev:%x mflags:%x mask:%x ignored_mask:%x ",
124 inode->i_ino, inode->i_sb->s_dev,
125 mflags, mark->mask, mark->ignored_mask);
126 show_mark_fhandle(m, inode);
127 seq_putc(m, '\n');
Cyrill Gorcunovbe771962012-12-17 16:05:12 -0800128 iput(inode);
Jan Kara86ffe242017-03-14 14:29:35 +0100129 } else if (mark->connector->flags & FSNOTIFY_OBJ_TYPE_VFSMOUNT) {
130 struct mount *mnt = real_mount(mark->connector->mnt);
Cyrill Gorcunovbe771962012-12-17 16:05:12 -0800131
Joe Perchesa3816ab2014-09-29 16:08:25 -0700132 seq_printf(m, "fanotify mnt_id:%x mflags:%x mask:%x ignored_mask:%x\n",
133 mnt->mnt_id, mflags, mark->mask, mark->ignored_mask);
Cyrill Gorcunovbe771962012-12-17 16:05:12 -0800134 }
Cyrill Gorcunovbe771962012-12-17 16:05:12 -0800135}
136
Joe Perchesa3816ab2014-09-29 16:08:25 -0700137void fanotify_show_fdinfo(struct seq_file *m, struct file *f)
Cyrill Gorcunovbe771962012-12-17 16:05:12 -0800138{
139 struct fsnotify_group *group = f->private_data;
140 unsigned int flags = 0;
141
142 switch (group->priority) {
143 case FS_PRIO_0:
144 flags |= FAN_CLASS_NOTIF;
145 break;
146 case FS_PRIO_1:
147 flags |= FAN_CLASS_CONTENT;
148 break;
149 case FS_PRIO_2:
150 flags |= FAN_CLASS_PRE_CONTENT;
151 break;
152 }
153
154 if (group->max_events == UINT_MAX)
155 flags |= FAN_UNLIMITED_QUEUE;
156
157 if (group->fanotify_data.max_marks == UINT_MAX)
158 flags |= FAN_UNLIMITED_MARKS;
159
Steve Grubbde8cd832017-10-02 20:21:39 -0400160 if (group->fanotify_data.audit)
161 flags |= FAN_ENABLE_AUDIT;
162
Cyrill Gorcunovbe771962012-12-17 16:05:12 -0800163 seq_printf(m, "fanotify flags:%x event-flags:%x\n",
164 flags, group->fanotify_data.f_flags);
165
Joe Perchesa3816ab2014-09-29 16:08:25 -0700166 show_fdinfo(m, f, fanotify_fdinfo);
Cyrill Gorcunovbe771962012-12-17 16:05:12 -0800167}
168
169#endif /* CONFIG_FANOTIFY */
170
171#endif /* CONFIG_INOTIFY_USER || CONFIG_FANOTIFY */
172
173#endif /* CONFIG_PROC_FS */