1 // SPDX-License-Identifier: GPL-2.0
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>
14 #include <linux/exportfs.h>
15 
16 #include "inotify/inotify.h"
17 #include "fdinfo.h"
18 #include "fsnotify.h"
19 
20 #if defined(CONFIG_PROC_FS)
21 
22 #if defined(CONFIG_INOTIFY_USER) || defined(CONFIG_FANOTIFY)
23 
show_fdinfo(struct seq_file * m,struct file * f,void (* show)(struct seq_file * m,struct fsnotify_mark * mark))24 static void show_fdinfo(struct seq_file *m, struct file *f,
25 			void (*show)(struct seq_file *m,
26 				     struct fsnotify_mark *mark))
27 {
28 	struct fsnotify_group *group = f->private_data;
29 	struct fsnotify_mark *mark;
30 
31 	mutex_lock(&group->mark_mutex);
32 	list_for_each_entry(mark, &group->marks_list, g_list) {
33 		show(m, mark);
34 		if (seq_has_overflowed(m))
35 			break;
36 	}
37 	mutex_unlock(&group->mark_mutex);
38 }
39 
40 #if defined(CONFIG_EXPORTFS)
show_mark_fhandle(struct seq_file * m,struct inode * inode)41 static void show_mark_fhandle(struct seq_file *m, struct inode *inode)
42 {
43 	struct {
44 		struct file_handle handle;
45 		u8 pad[MAX_HANDLE_SZ];
46 	} 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, NULL);
53 	if ((ret == FILEID_INVALID) || (ret < 0)) {
54 		WARN_ONCE(1, "Can't encode file handler for inotify: %d\n", ret);
55 		return;
56 	}
57 
58 	f.handle.handle_type = ret;
59 	f.handle.handle_bytes = size * sizeof(u32);
60 
61 	seq_printf(m, "fhandle-bytes:%x fhandle-type:%x f_handle:",
62 		   f.handle.handle_bytes, f.handle.handle_type);
63 
64 	for (i = 0; i < f.handle.handle_bytes; i++)
65 		seq_printf(m, "%02x", (int)f.handle.f_handle[i]);
66 }
67 #else
show_mark_fhandle(struct seq_file * m,struct inode * inode)68 static void show_mark_fhandle(struct seq_file *m, struct inode *inode)
69 {
70 }
71 #endif
72 
73 #ifdef CONFIG_INOTIFY_USER
74 
inotify_fdinfo(struct seq_file * m,struct fsnotify_mark * mark)75 static void inotify_fdinfo(struct seq_file *m, struct fsnotify_mark *mark)
76 {
77 	struct inotify_inode_mark *inode_mark;
78 	struct inode *inode;
79 
80 	if (mark->connector->type != FSNOTIFY_OBJ_TYPE_INODE)
81 		return;
82 
83 	inode_mark = container_of(mark, struct inotify_inode_mark, fsn_mark);
84 	inode = igrab(fsnotify_conn_inode(mark->connector));
85 	if (inode) {
86 		/*
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;
93 		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,
95 			   mask, mark->ignored_mask);
96 		show_mark_fhandle(m, inode);
97 		seq_putc(m, '\n');
98 		iput(inode);
99 	}
100 }
101 
inotify_show_fdinfo(struct seq_file * m,struct file * f)102 void inotify_show_fdinfo(struct seq_file *m, struct file *f)
103 {
104 	show_fdinfo(m, f, inotify_fdinfo);
105 }
106 
107 #endif /* CONFIG_INOTIFY_USER */
108 
109 #ifdef CONFIG_FANOTIFY
110 
fanotify_fdinfo(struct seq_file * m,struct fsnotify_mark * mark)111 static void fanotify_fdinfo(struct seq_file *m, struct fsnotify_mark *mark)
112 {
113 	unsigned int mflags = 0;
114 	struct inode *inode;
115 
116 	if (mark->flags & FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY)
117 		mflags |= FAN_MARK_IGNORED_SURV_MODIFY;
118 
119 	if (mark->connector->type == FSNOTIFY_OBJ_TYPE_INODE) {
120 		inode = igrab(fsnotify_conn_inode(mark->connector));
121 		if (!inode)
122 			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');
128 		iput(inode);
129 	} else if (mark->connector->type == FSNOTIFY_OBJ_TYPE_VFSMOUNT) {
130 		struct mount *mnt = fsnotify_conn_mount(mark->connector);
131 
132 		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);
134 	} 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);
139 	}
140 }
141 
fanotify_show_fdinfo(struct seq_file * m,struct file * f)142 void fanotify_show_fdinfo(struct seq_file *m, struct file *f)
143 {
144 	struct fsnotify_group *group = f->private_data;
145 
146 	seq_printf(m, "fanotify flags:%x event-flags:%x\n",
147 		   group->fanotify_data.flags,
148 		   group->fanotify_data.f_flags);
149 
150 	show_fdinfo(m, f, fanotify_fdinfo);
151 }
152 
153 #endif /* CONFIG_FANOTIFY */
154 
155 #endif /* CONFIG_INOTIFY_USER || CONFIG_FANOTIFY */
156 
157 #endif /* CONFIG_PROC_FS */
158