Merge tag 'hid-for-linus-2025070502' of git://git.kernel.org/pub/scm/linux/kernel...
[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);
974e3fe0 50 if ((ret == FILEID_INVALID) || (ret < 0))
a3816ab0 51 return;
be77196b 52
4d69c58e
GS
53 f->handle_type = ret;
54 f->handle_bytes = size * sizeof(u32);
be77196b 55
a3816ab0 56 seq_printf(m, "fhandle-bytes:%x fhandle-type:%x f_handle:",
4d69c58e 57 f->handle_bytes, f->handle_type);
be77196b 58
4d69c58e
GS
59 for (i = 0; i < f->handle_bytes; i++)
60 seq_printf(m, "%02x", (int)f->f_handle[i]);
be77196b
CG
61}
62#else
a3816ab0 63static void show_mark_fhandle(struct seq_file *m, struct inode *inode)
be77196b 64{
be77196b
CG
65}
66#endif
67
68#ifdef CONFIG_INOTIFY_USER
69
a3816ab0 70static void inotify_fdinfo(struct seq_file *m, struct fsnotify_mark *mark)
be77196b
CG
71{
72 struct inotify_inode_mark *inode_mark;
73 struct inode *inode;
be77196b 74
d6f7b98b 75 if (mark->connector->type != FSNOTIFY_OBJ_TYPE_INODE)
a3816ab0 76 return;
be77196b
CG
77
78 inode_mark = container_of(mark, struct inotify_inode_mark, fsn_mark);
36f10f55 79 inode = igrab(fsnotify_conn_inode(mark->connector));
be77196b 80 if (inode) {
a32e697c 81 seq_printf(m, "inotify wd:%x ino:%lx sdev:%x mask:%x ignored_mask:0 ",
a3816ab0 82 inode_mark->wd, inode->i_ino, inode->i_sb->s_dev,
a32e697c 83 inotify_mark_user_mask(mark));
a3816ab0
JP
84 show_mark_fhandle(m, inode);
85 seq_putc(m, '\n');
be77196b
CG
86 iput(inode);
87 }
be77196b
CG
88}
89
a3816ab0 90void inotify_show_fdinfo(struct seq_file *m, struct file *f)
be77196b 91{
a3816ab0 92 show_fdinfo(m, f, inotify_fdinfo);
be77196b
CG
93}
94
95#endif /* CONFIG_INOTIFY_USER */
96
97#ifdef CONFIG_FANOTIFY
98
a3816ab0 99static void fanotify_fdinfo(struct seq_file *m, struct fsnotify_mark *mark)
be77196b 100{
4adce25c 101 unsigned int mflags = fanotify_mark_user_flags(mark);
be77196b 102 struct inode *inode;
be77196b 103
d6f7b98b 104 if (mark->connector->type == FSNOTIFY_OBJ_TYPE_INODE) {
36f10f55 105 inode = igrab(fsnotify_conn_inode(mark->connector));
be77196b 106 if (!inode)
a3816ab0
JP
107 return;
108 seq_printf(m, "fanotify ino:%lx sdev:%x mflags:%x mask:%x ignored_mask:%x ",
109 inode->i_ino, inode->i_sb->s_dev,
31a371e4 110 mflags, mark->mask, mark->ignore_mask);
a3816ab0
JP
111 show_mark_fhandle(m, inode);
112 seq_putc(m, '\n');
be77196b 113 iput(inode);
d6f7b98b 114 } else if (mark->connector->type == FSNOTIFY_OBJ_TYPE_VFSMOUNT) {
36f10f55 115 struct mount *mnt = fsnotify_conn_mount(mark->connector);
be77196b 116
a3816ab0 117 seq_printf(m, "fanotify mnt_id:%x mflags:%x mask:%x ignored_mask:%x\n",
31a371e4 118 mnt->mnt_id, mflags, mark->mask, mark->ignore_mask);
1e6cb723
AG
119 } else if (mark->connector->type == FSNOTIFY_OBJ_TYPE_SB) {
120 struct super_block *sb = fsnotify_conn_sb(mark->connector);
121
122 seq_printf(m, "fanotify sdev:%x mflags:%x mask:%x ignored_mask:%x\n",
31a371e4 123 sb->s_dev, mflags, mark->mask, mark->ignore_mask);
0f46d81f
MS
124 } else if (mark->connector->type == FSNOTIFY_OBJ_TYPE_MNTNS) {
125 struct mnt_namespace *mnt_ns = fsnotify_conn_mntns(mark->connector);
126
127 seq_printf(m, "fanotify mnt_ns:%u mflags:%x mask:%x ignored_mask:%x\n",
128 mnt_ns->ns.inum, mflags, mark->mask, mark->ignore_mask);
be77196b 129 }
be77196b
CG
130}
131
a3816ab0 132void fanotify_show_fdinfo(struct seq_file *m, struct file *f)
be77196b
CG
133{
134 struct fsnotify_group *group = f->private_data;
de8cd83e 135
be77196b 136 seq_printf(m, "fanotify flags:%x event-flags:%x\n",
a8b98c80 137 group->fanotify_data.flags & FANOTIFY_INIT_FLAGS,
7cea2a3c 138 group->fanotify_data.f_flags);
be77196b 139
a3816ab0 140 show_fdinfo(m, f, fanotify_fdinfo);
be77196b
CG
141}
142
143#endif /* CONFIG_FANOTIFY */
144
145#endif /* CONFIG_INOTIFY_USER || CONFIG_FANOTIFY */
146
147#endif /* CONFIG_PROC_FS */