blob: 57f0d5d9f934efecf793335386c09da4cfba1c09 [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>
Cyrill Gorcunovbe771962012-12-17 16:05:12 -080014#include <linux/exportfs.h>
15
16#include "inotify/inotify.h"
Eric Biggersd6f7aa92018-11-14 16:12:14 -080017#include "fdinfo.h"
Amir Goldstein36f10f52018-06-23 17:54:49 +030018#include "fsnotify.h"
Cyrill Gorcunovbe771962012-12-17 16:05:12 -080019
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
Ben Dooks (Codethink)ddd06c32019-10-16 10:59:55 +010052 ret = exportfs_encode_inode_fh(inode, (struct fid *)f.handle.f_handle, &size, NULL);
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
Amir Goldsteind6f7b982018-04-20 16:10:49 -070080 if (mark->connector->type != 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);
Amir Goldstein36f10f52018-06-23 17:54:49 +030084 inode = igrab(fsnotify_conn_inode(mark->connector));
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
Amir Goldsteind6f7b982018-04-20 16:10:49 -0700119 if (mark->connector->type == FSNOTIFY_OBJ_TYPE_INODE) {
Amir Goldstein36f10f52018-06-23 17:54:49 +0300120 inode = igrab(fsnotify_conn_inode(mark->connector));
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);
Amir Goldsteind6f7b982018-04-20 16:10:49 -0700129 } else if (mark->connector->type == FSNOTIFY_OBJ_TYPE_VFSMOUNT) {
Amir Goldstein36f10f52018-06-23 17:54:49 +0300130 struct mount *mnt = fsnotify_conn_mount(mark->connector);
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);
Amir Goldstein1e6cb722018-09-01 10:41:11 +0300134 } else if (mark->connector->type == FSNOTIFY_OBJ_TYPE_SB) {
135 struct super_block *sb = fsnotify_conn_sb(mark->connector);
136
137 seq_printf(m, "fanotify sdev:%x mflags:%x mask:%x ignored_mask:%x\n",
138 sb->s_dev, mflags, mark->mask, mark->ignored_mask);
Cyrill Gorcunovbe771962012-12-17 16:05:12 -0800139 }
Cyrill Gorcunovbe771962012-12-17 16:05:12 -0800140}
141
Joe Perchesa3816ab2014-09-29 16:08:25 -0700142void fanotify_show_fdinfo(struct seq_file *m, struct file *f)
Cyrill Gorcunovbe771962012-12-17 16:05:12 -0800143{
144 struct fsnotify_group *group = f->private_data;
Steve Grubbde8cd832017-10-02 20:21:39 -0400145
Cyrill Gorcunovbe771962012-12-17 16:05:12 -0800146 seq_printf(m, "fanotify flags:%x event-flags:%x\n",
Amir Goldsteina8b98c82021-05-24 16:53:21 +0300147 group->fanotify_data.flags & FANOTIFY_INIT_FLAGS,
Amir Goldstein7cea2a32021-03-04 13:29:21 +0200148 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 */