Merge tag 'sched_ext-for-6.12-rc1-fixes-1' of git://git.kernel.org/pub/scm/linux...
[linux-block.git] / fs / notify / fdinfo.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
be77196b
CG
2#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>
be77196b
CG
14#include <linux/exportfs.h>
15
16#include "inotify/inotify.h"
4adce25c 17#include "fanotify/fanotify.h"
d6f7aa98 18#include "fdinfo.h"
36f10f55 19#include "fsnotify.h"
be77196b
CG
20
21#if defined(CONFIG_PROC_FS)
22
23#if defined(CONFIG_INOTIFY_USER) || defined(CONFIG_FANOTIFY)
24
a3816ab0
JP
25static void show_fdinfo(struct seq_file *m, struct file *f,
26 void (*show)(struct seq_file *m,
27 struct fsnotify_mark *mark))
be77196b
CG
28{
29 struct fsnotify_group *group = f->private_data;
30 struct fsnotify_mark *mark;
be77196b 31
43b245a7 32 fsnotify_group_lock(group);
be77196b 33 list_for_each_entry(mark, &group->marks_list, g_list) {
a3816ab0
JP
34 show(m, mark);
35 if (seq_has_overflowed(m))
be77196b
CG
36 break;
37 }
43b245a7 38 fsnotify_group_unlock(group);
be77196b
CG
39}
40
41#if defined(CONFIG_EXPORTFS)
a3816ab0 42static void show_mark_fhandle(struct seq_file *m, struct inode *inode)
be77196b 43{
4d69c58e 44 DEFINE_FLEX(struct file_handle, f, f_handle, handle_bytes, MAX_HANDLE_SZ);
be77196b
CG
45 int size, ret, i;
46
4d69c58e 47 size = f->handle_bytes >> 2;
be77196b 48
4d69c58e 49 ret = exportfs_encode_fid(inode, (struct fid *)f->f_handle, &size);
7e882481 50 if ((ret == FILEID_INVALID) || (ret < 0)) {
be77196b 51 WARN_ONCE(1, "Can't encode file handler for inotify: %d\n", ret);
a3816ab0 52 return;
be77196b
CG
53 }
54
4d69c58e
GS
55 f->handle_type = ret;
56 f->handle_bytes = size * sizeof(u32);
be77196b 57
a3816ab0 58 seq_printf(m, "fhandle-bytes:%x fhandle-type:%x f_handle:",
4d69c58e 59 f->handle_bytes, f->handle_type);
be77196b 60
4d69c58e
GS
61 for (i = 0; i < f->handle_bytes; i++)
62 seq_printf(m, "%02x", (int)f->f_handle[i]);
be77196b
CG
63}
64#else
a3816ab0 65static void show_mark_fhandle(struct seq_file *m, struct inode *inode)
be77196b 66{
be77196b
CG
67}
68#endif
69
70#ifdef CONFIG_INOTIFY_USER
71
a3816ab0 72static void inotify_fdinfo(struct seq_file *m, struct fsnotify_mark *mark)
be77196b
CG
73{
74 struct inotify_inode_mark *inode_mark;
75 struct inode *inode;
be77196b 76
d6f7b98b 77 if (mark->connector->type != FSNOTIFY_OBJ_TYPE_INODE)
a3816ab0 78 return;
be77196b
CG
79
80 inode_mark = container_of(mark, struct inotify_inode_mark, fsn_mark);
36f10f55 81 inode = igrab(fsnotify_conn_inode(mark->connector));
be77196b 82 if (inode) {
a32e697c 83 seq_printf(m, "inotify wd:%x ino:%lx sdev:%x mask:%x ignored_mask:0 ",
a3816ab0 84 inode_mark->wd, inode->i_ino, inode->i_sb->s_dev,
a32e697c 85 inotify_mark_user_mask(mark));
a3816ab0
JP
86 show_mark_fhandle(m, inode);
87 seq_putc(m, '\n');
be77196b
CG
88 iput(inode);
89 }
be77196b
CG
90}
91
a3816ab0 92void inotify_show_fdinfo(struct seq_file *m, struct file *f)
be77196b 93{
a3816ab0 94 show_fdinfo(m, f, inotify_fdinfo);
be77196b
CG
95}
96
97#endif /* CONFIG_INOTIFY_USER */
98
99#ifdef CONFIG_FANOTIFY
100
a3816ab0 101static void fanotify_fdinfo(struct seq_file *m, struct fsnotify_mark *mark)
be77196b 102{
4adce25c 103 unsigned int mflags = fanotify_mark_user_flags(mark);
be77196b 104 struct inode *inode;
be77196b 105
d6f7b98b 106 if (mark->connector->type == FSNOTIFY_OBJ_TYPE_INODE) {
36f10f55 107 inode = igrab(fsnotify_conn_inode(mark->connector));
be77196b 108 if (!inode)
a3816ab0
JP
109 return;
110 seq_printf(m, "fanotify ino:%lx sdev:%x mflags:%x mask:%x ignored_mask:%x ",
111 inode->i_ino, inode->i_sb->s_dev,
31a371e4 112 mflags, mark->mask, mark->ignore_mask);
a3816ab0
JP
113 show_mark_fhandle(m, inode);
114 seq_putc(m, '\n');
be77196b 115 iput(inode);
d6f7b98b 116 } else if (mark->connector->type == FSNOTIFY_OBJ_TYPE_VFSMOUNT) {
36f10f55 117 struct mount *mnt = fsnotify_conn_mount(mark->connector);
be77196b 118
a3816ab0 119 seq_printf(m, "fanotify mnt_id:%x mflags:%x mask:%x ignored_mask:%x\n",
31a371e4 120 mnt->mnt_id, mflags, mark->mask, mark->ignore_mask);
1e6cb723
AG
121 } else if (mark->connector->type == FSNOTIFY_OBJ_TYPE_SB) {
122 struct super_block *sb = fsnotify_conn_sb(mark->connector);
123
124 seq_printf(m, "fanotify sdev:%x mflags:%x mask:%x ignored_mask:%x\n",
31a371e4 125 sb->s_dev, mflags, mark->mask, mark->ignore_mask);
be77196b 126 }
be77196b
CG
127}
128
a3816ab0 129void fanotify_show_fdinfo(struct seq_file *m, struct file *f)
be77196b
CG
130{
131 struct fsnotify_group *group = f->private_data;
de8cd83e 132
be77196b 133 seq_printf(m, "fanotify flags:%x event-flags:%x\n",
a8b98c80 134 group->fanotify_data.flags & FANOTIFY_INIT_FLAGS,
7cea2a3c 135 group->fanotify_data.f_flags);
be77196b 136
a3816ab0 137 show_fdinfo(m, f, fanotify_fdinfo);
be77196b
CG
138}
139
140#endif /* CONFIG_FANOTIFY */
141
142#endif /* CONFIG_INOTIFY_USER || CONFIG_FANOTIFY */
143
144#endif /* CONFIG_PROC_FS */