xref: /openbmc/linux/fs/proc/fd.c (revision 0c08b92f)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
23f07c014SIngo Molnar #include <linux/sched/signal.h>
3faf60af1SCyrill Gorcunov #include <linux/errno.h>
4faf60af1SCyrill Gorcunov #include <linux/dcache.h>
5faf60af1SCyrill Gorcunov #include <linux/path.h>
6faf60af1SCyrill Gorcunov #include <linux/fdtable.h>
7faf60af1SCyrill Gorcunov #include <linux/namei.h>
8faf60af1SCyrill Gorcunov #include <linux/pid.h>
97bc3fa01SKalesh Singh #include <linux/ptrace.h>
10f1f1f256SIvan Babrou #include <linux/bitmap.h>
11faf60af1SCyrill Gorcunov #include <linux/security.h>
12ddd3e077SCyrill Gorcunov #include <linux/file.h>
13ddd3e077SCyrill Gorcunov #include <linux/seq_file.h>
146c8c9031SAndrey Vagin #include <linux/fs.h>
155970e15dSJeff Layton #include <linux/filelock.h>
16faf60af1SCyrill Gorcunov 
17faf60af1SCyrill Gorcunov #include <linux/proc_fs.h>
18faf60af1SCyrill Gorcunov 
1949d063cbSAndrey Vagin #include "../mount.h"
20faf60af1SCyrill Gorcunov #include "internal.h"
21faf60af1SCyrill Gorcunov #include "fd.h"
22faf60af1SCyrill Gorcunov 
seq_show(struct seq_file * m,void * v)23ddd3e077SCyrill Gorcunov static int seq_show(struct seq_file *m, void *v)
24faf60af1SCyrill Gorcunov {
25faf60af1SCyrill Gorcunov 	struct files_struct *files = NULL;
26ddd3e077SCyrill Gorcunov 	int f_flags = 0, ret = -ENOENT;
27ddd3e077SCyrill Gorcunov 	struct file *file = NULL;
28ddd3e077SCyrill Gorcunov 	struct task_struct *task;
29faf60af1SCyrill Gorcunov 
30ddd3e077SCyrill Gorcunov 	task = get_proc_task(m->private);
31ddd3e077SCyrill Gorcunov 	if (!task)
32ddd3e077SCyrill Gorcunov 		return -ENOENT;
33ddd3e077SCyrill Gorcunov 
34775e0656SEric W. Biederman 	task_lock(task);
35775e0656SEric W. Biederman 	files = task->files;
36faf60af1SCyrill Gorcunov 	if (files) {
37771187d6SAlexey Dobriyan 		unsigned int fd = proc_fd(m->private);
38ddd3e077SCyrill Gorcunov 
39faf60af1SCyrill Gorcunov 		spin_lock(&files->file_lock);
40120ce2b0SEric W. Biederman 		file = files_lookup_fd_locked(files, fd);
41faf60af1SCyrill Gorcunov 		if (file) {
42ddd3e077SCyrill Gorcunov 			struct fdtable *fdt = files_fdtable(files);
43faf60af1SCyrill Gorcunov 
44c6f3d811SAl Viro 			f_flags = file->f_flags;
45faf60af1SCyrill Gorcunov 			if (close_on_exec(fd, fdt))
46faf60af1SCyrill Gorcunov 				f_flags |= O_CLOEXEC;
47faf60af1SCyrill Gorcunov 
48ddd3e077SCyrill Gorcunov 			get_file(file);
49ddd3e077SCyrill Gorcunov 			ret = 0;
50faf60af1SCyrill Gorcunov 		}
51faf60af1SCyrill Gorcunov 		spin_unlock(&files->file_lock);
52faf60af1SCyrill Gorcunov 	}
53775e0656SEric W. Biederman 	task_unlock(task);
54775e0656SEric W. Biederman 	put_task_struct(task);
55ddd3e077SCyrill Gorcunov 
566c8c9031SAndrey Vagin 	if (ret)
576c8c9031SAndrey Vagin 		return ret;
586c8c9031SAndrey Vagin 
593845f256SKalesh Singh 	seq_printf(m, "pos:\t%lli\nflags:\t0%o\nmnt_id:\t%i\nino:\t%lu\n",
6049d063cbSAndrey Vagin 		   (long long)file->f_pos, f_flags,
613845f256SKalesh Singh 		   real_mount(file->f_path.mnt)->mnt_id,
623845f256SKalesh Singh 		   file_inode(file)->i_ino);
636c8c9031SAndrey Vagin 
64775e0656SEric W. Biederman 	/* show_fd_locks() never deferences files so a stale value is safe */
656c8c9031SAndrey Vagin 	show_fd_locks(m, file, files);
666c8c9031SAndrey Vagin 	if (seq_has_overflowed(m))
676c8c9031SAndrey Vagin 		goto out;
686c8c9031SAndrey Vagin 
6955985dd7SCyrill Gorcunov 	if (file->f_op->show_fdinfo)
70a3816ab0SJoe Perches 		file->f_op->show_fdinfo(m, file);
71faf60af1SCyrill Gorcunov 
726c8c9031SAndrey Vagin out:
736c8c9031SAndrey Vagin 	fput(file);
746c8c9031SAndrey Vagin 	return 0;
75ddd3e077SCyrill Gorcunov }
76ddd3e077SCyrill Gorcunov 
seq_fdinfo_open(struct inode * inode,struct file * file)770c08b92fSTyler Hicks (Microsoft) static int seq_fdinfo_open(struct inode *inode, struct file *file)
780c08b92fSTyler Hicks (Microsoft) {
790c08b92fSTyler Hicks (Microsoft) 	return single_open(file, seq_show, inode);
800c08b92fSTyler Hicks (Microsoft) }
810c08b92fSTyler Hicks (Microsoft) 
820c08b92fSTyler Hicks (Microsoft) /**
830c08b92fSTyler Hicks (Microsoft)  * Shared /proc/pid/fdinfo and /proc/pid/fdinfo/fd permission helper to ensure
840c08b92fSTyler Hicks (Microsoft)  * that the current task has PTRACE_MODE_READ in addition to the normal
850c08b92fSTyler Hicks (Microsoft)  * POSIX-like checks.
860c08b92fSTyler Hicks (Microsoft)  */
proc_fdinfo_permission(struct mnt_idmap * idmap,struct inode * inode,int mask)870c08b92fSTyler Hicks (Microsoft) static int proc_fdinfo_permission(struct mnt_idmap *idmap, struct inode *inode,
880c08b92fSTyler Hicks (Microsoft) 				  int mask)
89ddd3e077SCyrill Gorcunov {
907bc3fa01SKalesh Singh 	bool allowed = false;
917bc3fa01SKalesh Singh 	struct task_struct *task = get_proc_task(inode);
927bc3fa01SKalesh Singh 
937bc3fa01SKalesh Singh 	if (!task)
947bc3fa01SKalesh Singh 		return -ESRCH;
957bc3fa01SKalesh Singh 
967bc3fa01SKalesh Singh 	allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
977bc3fa01SKalesh Singh 	put_task_struct(task);
987bc3fa01SKalesh Singh 
997bc3fa01SKalesh Singh 	if (!allowed)
1007bc3fa01SKalesh Singh 		return -EACCES;
1017bc3fa01SKalesh Singh 
1020c08b92fSTyler Hicks (Microsoft) 	return generic_permission(idmap, inode, mask);
1031927e498SKalesh Singh }
1041927e498SKalesh Singh 
1050c08b92fSTyler Hicks (Microsoft) static const struct inode_operations proc_fdinfo_file_inode_operations = {
1060c08b92fSTyler Hicks (Microsoft) 	.permission	= proc_fdinfo_permission,
1070c08b92fSTyler Hicks (Microsoft) 	.setattr	= proc_setattr,
1080c08b92fSTyler Hicks (Microsoft) };
109ddd3e077SCyrill Gorcunov 
110ddd3e077SCyrill Gorcunov static const struct file_operations proc_fdinfo_file_operations = {
111ddd3e077SCyrill Gorcunov 	.open		= seq_fdinfo_open,
112ddd3e077SCyrill Gorcunov 	.read		= seq_read,
113ddd3e077SCyrill Gorcunov 	.llseek		= seq_lseek,
114ddd3e077SCyrill Gorcunov 	.release	= single_release,
115ddd3e077SCyrill Gorcunov };
116ddd3e077SCyrill Gorcunov 
tid_fd_mode(struct task_struct * task,unsigned fd,fmode_t * mode)1171ae9bd8bSAl Viro static bool tid_fd_mode(struct task_struct *task, unsigned fd, fmode_t *mode)
1181ae9bd8bSAl Viro {
1191ae9bd8bSAl Viro 	struct file *file;
1201ae9bd8bSAl Viro 
1211ae9bd8bSAl Viro 	rcu_read_lock();
12264eb661fSEric W. Biederman 	file = task_lookup_fd_rcu(task, fd);
1231ae9bd8bSAl Viro 	if (file)
1241ae9bd8bSAl Viro 		*mode = file->f_mode;
1251ae9bd8bSAl Viro 	rcu_read_unlock();
1261ae9bd8bSAl Viro 	return !!file;
1271ae9bd8bSAl Viro }
1281ae9bd8bSAl Viro 
tid_fd_update_inode(struct task_struct * task,struct inode * inode,fmode_t f_mode)12998836386SAl Viro static void tid_fd_update_inode(struct task_struct *task, struct inode *inode,
13098836386SAl Viro 				fmode_t f_mode)
131faf60af1SCyrill Gorcunov {
13268eb94f1SEric W. Biederman 	task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
133faf60af1SCyrill Gorcunov 
134faf60af1SCyrill Gorcunov 	if (S_ISLNK(inode->i_mode)) {
135faf60af1SCyrill Gorcunov 		unsigned i_mode = S_IFLNK;
136faf60af1SCyrill Gorcunov 		if (f_mode & FMODE_READ)
137faf60af1SCyrill Gorcunov 			i_mode |= S_IRUSR | S_IXUSR;
138faf60af1SCyrill Gorcunov 		if (f_mode & FMODE_WRITE)
139faf60af1SCyrill Gorcunov 			i_mode |= S_IWUSR | S_IXUSR;
140faf60af1SCyrill Gorcunov 		inode->i_mode = i_mode;
141faf60af1SCyrill Gorcunov 	}
142faf60af1SCyrill Gorcunov 	security_task_to_inode(task, inode);
14398836386SAl Viro }
14498836386SAl Viro 
tid_fd_revalidate(struct dentry * dentry,unsigned int flags)14598836386SAl Viro static int tid_fd_revalidate(struct dentry *dentry, unsigned int flags)
14698836386SAl Viro {
14798836386SAl Viro 	struct task_struct *task;
14898836386SAl Viro 	struct inode *inode;
14998836386SAl Viro 	unsigned int fd;
15098836386SAl Viro 
15198836386SAl Viro 	if (flags & LOOKUP_RCU)
15298836386SAl Viro 		return -ECHILD;
15398836386SAl Viro 
15498836386SAl Viro 	inode = d_inode(dentry);
15598836386SAl Viro 	task = get_proc_task(inode);
15698836386SAl Viro 	fd = proc_fd(inode);
15798836386SAl Viro 
15898836386SAl Viro 	if (task) {
15998836386SAl Viro 		fmode_t f_mode;
16098836386SAl Viro 		if (tid_fd_mode(task, fd, &f_mode)) {
16198836386SAl Viro 			tid_fd_update_inode(task, inode, f_mode);
162faf60af1SCyrill Gorcunov 			put_task_struct(task);
163faf60af1SCyrill Gorcunov 			return 1;
164faf60af1SCyrill Gorcunov 		}
165faf60af1SCyrill Gorcunov 		put_task_struct(task);
166faf60af1SCyrill Gorcunov 	}
167faf60af1SCyrill Gorcunov 	return 0;
168faf60af1SCyrill Gorcunov }
169faf60af1SCyrill Gorcunov 
170faf60af1SCyrill Gorcunov static const struct dentry_operations tid_fd_dentry_operations = {
171faf60af1SCyrill Gorcunov 	.d_revalidate	= tid_fd_revalidate,
172faf60af1SCyrill Gorcunov 	.d_delete	= pid_delete_dentry,
173faf60af1SCyrill Gorcunov };
174faf60af1SCyrill Gorcunov 
proc_fd_link(struct dentry * dentry,struct path * path)175faf60af1SCyrill Gorcunov static int proc_fd_link(struct dentry *dentry, struct path *path)
176faf60af1SCyrill Gorcunov {
177ddd3e077SCyrill Gorcunov 	struct task_struct *task;
178ddd3e077SCyrill Gorcunov 	int ret = -ENOENT;
179ddd3e077SCyrill Gorcunov 
1802b0143b5SDavid Howells 	task = get_proc_task(d_inode(dentry));
181ddd3e077SCyrill Gorcunov 	if (task) {
182771187d6SAlexey Dobriyan 		unsigned int fd = proc_fd(d_inode(dentry));
183ddd3e077SCyrill Gorcunov 		struct file *fd_file;
184ddd3e077SCyrill Gorcunov 
185439be326SEric W. Biederman 		fd_file = fget_task(task, fd);
186ddd3e077SCyrill Gorcunov 		if (fd_file) {
187ddd3e077SCyrill Gorcunov 			*path = fd_file->f_path;
188ddd3e077SCyrill Gorcunov 			path_get(&fd_file->f_path);
189ddd3e077SCyrill Gorcunov 			ret = 0;
190439be326SEric W. Biederman 			fput(fd_file);
191ddd3e077SCyrill Gorcunov 		}
192439be326SEric W. Biederman 		put_task_struct(task);
193ddd3e077SCyrill Gorcunov 	}
194ddd3e077SCyrill Gorcunov 
195ddd3e077SCyrill Gorcunov 	return ret;
196faf60af1SCyrill Gorcunov }
197faf60af1SCyrill Gorcunov 
19898836386SAl Viro struct fd_data {
19998836386SAl Viro 	fmode_t mode;
20098836386SAl Viro 	unsigned fd;
20198836386SAl Viro };
20298836386SAl Viro 
proc_fd_instantiate(struct dentry * dentry,struct task_struct * task,const void * ptr)2030168b9e3SAl Viro static struct dentry *proc_fd_instantiate(struct dentry *dentry,
204faf60af1SCyrill Gorcunov 	struct task_struct *task, const void *ptr)
205faf60af1SCyrill Gorcunov {
20698836386SAl Viro 	const struct fd_data *data = ptr;
207faf60af1SCyrill Gorcunov 	struct proc_inode *ei;
208faf60af1SCyrill Gorcunov 	struct inode *inode;
209faf60af1SCyrill Gorcunov 
2100168b9e3SAl Viro 	inode = proc_pid_make_inode(dentry->d_sb, task, S_IFLNK);
211faf60af1SCyrill Gorcunov 	if (!inode)
2120168b9e3SAl Viro 		return ERR_PTR(-ENOENT);
213faf60af1SCyrill Gorcunov 
214faf60af1SCyrill Gorcunov 	ei = PROC_I(inode);
21598836386SAl Viro 	ei->fd = data->fd;
216faf60af1SCyrill Gorcunov 
217faf60af1SCyrill Gorcunov 	inode->i_op = &proc_pid_link_inode_operations;
218faf60af1SCyrill Gorcunov 	inode->i_size = 64;
219faf60af1SCyrill Gorcunov 
220faf60af1SCyrill Gorcunov 	ei->op.proc_get_link = proc_fd_link;
22198836386SAl Viro 	tid_fd_update_inode(task, inode, data->mode);
222faf60af1SCyrill Gorcunov 
223faf60af1SCyrill Gorcunov 	d_set_d_op(dentry, &tid_fd_dentry_operations);
2240168b9e3SAl Viro 	return d_splice_alias(inode, dentry);
225faf60af1SCyrill Gorcunov }
226faf60af1SCyrill Gorcunov 
proc_lookupfd_common(struct inode * dir,struct dentry * dentry,instantiate_t instantiate)227faf60af1SCyrill Gorcunov static struct dentry *proc_lookupfd_common(struct inode *dir,
228faf60af1SCyrill Gorcunov 					   struct dentry *dentry,
229faf60af1SCyrill Gorcunov 					   instantiate_t instantiate)
230faf60af1SCyrill Gorcunov {
231faf60af1SCyrill Gorcunov 	struct task_struct *task = get_proc_task(dir);
23298836386SAl Viro 	struct fd_data data = {.fd = name_to_int(&dentry->d_name)};
2330168b9e3SAl Viro 	struct dentry *result = ERR_PTR(-ENOENT);
234faf60af1SCyrill Gorcunov 
235faf60af1SCyrill Gorcunov 	if (!task)
236faf60af1SCyrill Gorcunov 		goto out_no_task;
23798836386SAl Viro 	if (data.fd == ~0U)
238faf60af1SCyrill Gorcunov 		goto out;
23998836386SAl Viro 	if (!tid_fd_mode(task, data.fd, &data.mode))
2401ae9bd8bSAl Viro 		goto out;
241faf60af1SCyrill Gorcunov 
2420168b9e3SAl Viro 	result = instantiate(dentry, task, &data);
243faf60af1SCyrill Gorcunov out:
244faf60af1SCyrill Gorcunov 	put_task_struct(task);
245faf60af1SCyrill Gorcunov out_no_task:
2460168b9e3SAl Viro 	return result;
247faf60af1SCyrill Gorcunov }
248faf60af1SCyrill Gorcunov 
proc_readfd_common(struct file * file,struct dir_context * ctx,instantiate_t instantiate)249f0c3b509SAl Viro static int proc_readfd_common(struct file *file, struct dir_context *ctx,
250f0c3b509SAl Viro 			      instantiate_t instantiate)
251faf60af1SCyrill Gorcunov {
252f0c3b509SAl Viro 	struct task_struct *p = get_proc_task(file_inode(file));
253f0c3b509SAl Viro 	unsigned int fd;
254faf60af1SCyrill Gorcunov 
255faf60af1SCyrill Gorcunov 	if (!p)
256f0c3b509SAl Viro 		return -ENOENT;
257faf60af1SCyrill Gorcunov 
258f0c3b509SAl Viro 	if (!dir_emit_dots(file, ctx))
259faf60af1SCyrill Gorcunov 		goto out;
260f0c3b509SAl Viro 
261faf60af1SCyrill Gorcunov 	rcu_read_lock();
2625b17b618SEric W. Biederman 	for (fd = ctx->pos - 2;; fd++) {
26398836386SAl Viro 		struct file *f;
26498836386SAl Viro 		struct fd_data data;
265e3912ac3SAlexey Dobriyan 		char name[10 + 1];
266a4ef3895SAlexey Dobriyan 		unsigned int len;
267faf60af1SCyrill Gorcunov 
2685b17b618SEric W. Biederman 		f = task_lookup_next_fd_rcu(p, &fd);
2695b17b618SEric W. Biederman 		ctx->pos = fd + 2LL;
27098836386SAl Viro 		if (!f)
2715b17b618SEric W. Biederman 			break;
27298836386SAl Viro 		data.mode = f->f_mode;
273faf60af1SCyrill Gorcunov 		rcu_read_unlock();
27498836386SAl Viro 		data.fd = fd;
275faf60af1SCyrill Gorcunov 
276771187d6SAlexey Dobriyan 		len = snprintf(name, sizeof(name), "%u", fd);
277f0c3b509SAl Viro 		if (!proc_fill_cache(file, ctx,
278faf60af1SCyrill Gorcunov 				     name, len, instantiate, p,
27998836386SAl Viro 				     &data))
2805b17b618SEric W. Biederman 			goto out;
2813cc4a84eSEric Dumazet 		cond_resched();
282faf60af1SCyrill Gorcunov 		rcu_read_lock();
283faf60af1SCyrill Gorcunov 	}
284faf60af1SCyrill Gorcunov 	rcu_read_unlock();
285faf60af1SCyrill Gorcunov out:
286faf60af1SCyrill Gorcunov 	put_task_struct(p);
287f0c3b509SAl Viro 	return 0;
288faf60af1SCyrill Gorcunov }
289faf60af1SCyrill Gorcunov 
proc_readfd_count(struct inode * inode,loff_t * count)290f1f1f256SIvan Babrou static int proc_readfd_count(struct inode *inode, loff_t *count)
291f1f1f256SIvan Babrou {
292f1f1f256SIvan Babrou 	struct task_struct *p = get_proc_task(inode);
293f1f1f256SIvan Babrou 	struct fdtable *fdt;
294f1f1f256SIvan Babrou 
295f1f1f256SIvan Babrou 	if (!p)
296f1f1f256SIvan Babrou 		return -ENOENT;
297f1f1f256SIvan Babrou 
298f1f1f256SIvan Babrou 	task_lock(p);
299f1f1f256SIvan Babrou 	if (p->files) {
300f1f1f256SIvan Babrou 		rcu_read_lock();
301f1f1f256SIvan Babrou 
302f1f1f256SIvan Babrou 		fdt = files_fdtable(p->files);
303f1f1f256SIvan Babrou 		*count = bitmap_weight(fdt->open_fds, fdt->max_fds);
304f1f1f256SIvan Babrou 
305f1f1f256SIvan Babrou 		rcu_read_unlock();
306f1f1f256SIvan Babrou 	}
307f1f1f256SIvan Babrou 	task_unlock(p);
308f1f1f256SIvan Babrou 
309f1f1f256SIvan Babrou 	put_task_struct(p);
310f1f1f256SIvan Babrou 
311f1f1f256SIvan Babrou 	return 0;
312f1f1f256SIvan Babrou }
313f1f1f256SIvan Babrou 
proc_readfd(struct file * file,struct dir_context * ctx)314f0c3b509SAl Viro static int proc_readfd(struct file *file, struct dir_context *ctx)
315faf60af1SCyrill Gorcunov {
316f0c3b509SAl Viro 	return proc_readfd_common(file, ctx, proc_fd_instantiate);
317faf60af1SCyrill Gorcunov }
318faf60af1SCyrill Gorcunov 
319faf60af1SCyrill Gorcunov const struct file_operations proc_fd_operations = {
320faf60af1SCyrill Gorcunov 	.read		= generic_read_dir,
321f50752eaSAl Viro 	.iterate_shared	= proc_readfd,
322f50752eaSAl Viro 	.llseek		= generic_file_llseek,
323faf60af1SCyrill Gorcunov };
324faf60af1SCyrill Gorcunov 
proc_lookupfd(struct inode * dir,struct dentry * dentry,unsigned int flags)325faf60af1SCyrill Gorcunov static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
326faf60af1SCyrill Gorcunov 				    unsigned int flags)
327faf60af1SCyrill Gorcunov {
328faf60af1SCyrill Gorcunov 	return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
329faf60af1SCyrill Gorcunov }
330faf60af1SCyrill Gorcunov 
331faf60af1SCyrill Gorcunov /*
332faf60af1SCyrill Gorcunov  * /proc/pid/fd needs a special permission handler so that a process can still
333faf60af1SCyrill Gorcunov  * access /proc/self/fd after it has executed a setuid().
334faf60af1SCyrill Gorcunov  */
proc_fd_permission(struct mnt_idmap * idmap,struct inode * inode,int mask)3354609e1f1SChristian Brauner int proc_fd_permission(struct mnt_idmap *idmap,
336549c7297SChristian Brauner 		       struct inode *inode, int mask)
337faf60af1SCyrill Gorcunov {
33854708d28SOleg Nesterov 	struct task_struct *p;
33954708d28SOleg Nesterov 	int rv;
34054708d28SOleg Nesterov 
3414609e1f1SChristian Brauner 	rv = generic_permission(&nop_mnt_idmap, inode, mask);
342faf60af1SCyrill Gorcunov 	if (rv == 0)
34354708d28SOleg Nesterov 		return rv;
34454708d28SOleg Nesterov 
34554708d28SOleg Nesterov 	rcu_read_lock();
34654708d28SOleg Nesterov 	p = pid_task(proc_pid(inode), PIDTYPE_PID);
34754708d28SOleg Nesterov 	if (p && same_thread_group(p, current))
348faf60af1SCyrill Gorcunov 		rv = 0;
34954708d28SOleg Nesterov 	rcu_read_unlock();
35054708d28SOleg Nesterov 
351faf60af1SCyrill Gorcunov 	return rv;
352faf60af1SCyrill Gorcunov }
353faf60af1SCyrill Gorcunov 
proc_fd_getattr(struct mnt_idmap * idmap,const struct path * path,struct kstat * stat,u32 request_mask,unsigned int query_flags)354b74d24f7SChristian Brauner static int proc_fd_getattr(struct mnt_idmap *idmap,
355f1f1f256SIvan Babrou 			const struct path *path, struct kstat *stat,
356f1f1f256SIvan Babrou 			u32 request_mask, unsigned int query_flags)
357f1f1f256SIvan Babrou {
358f1f1f256SIvan Babrou 	struct inode *inode = d_inode(path->dentry);
359f1f1f256SIvan Babrou 	int rv = 0;
360f1f1f256SIvan Babrou 
3610d72b928SJeff Layton 	generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
362f1f1f256SIvan Babrou 
363f1f1f256SIvan Babrou 	/* If it's a directory, put the number of open fds there */
364f1f1f256SIvan Babrou 	if (S_ISDIR(inode->i_mode)) {
365f1f1f256SIvan Babrou 		rv = proc_readfd_count(inode, &stat->size);
366f1f1f256SIvan Babrou 		if (rv < 0)
367f1f1f256SIvan Babrou 			return rv;
368f1f1f256SIvan Babrou 	}
369f1f1f256SIvan Babrou 
370f1f1f256SIvan Babrou 	return rv;
371f1f1f256SIvan Babrou }
372f1f1f256SIvan Babrou 
373faf60af1SCyrill Gorcunov const struct inode_operations proc_fd_inode_operations = {
374faf60af1SCyrill Gorcunov 	.lookup		= proc_lookupfd,
375faf60af1SCyrill Gorcunov 	.permission	= proc_fd_permission,
376f1f1f256SIvan Babrou 	.getattr	= proc_fd_getattr,
377faf60af1SCyrill Gorcunov 	.setattr	= proc_setattr,
378faf60af1SCyrill Gorcunov };
379faf60af1SCyrill Gorcunov 
proc_fdinfo_instantiate(struct dentry * dentry,struct task_struct * task,const void * ptr)3800168b9e3SAl Viro static struct dentry *proc_fdinfo_instantiate(struct dentry *dentry,
381faf60af1SCyrill Gorcunov 	struct task_struct *task, const void *ptr)
382faf60af1SCyrill Gorcunov {
38398836386SAl Viro 	const struct fd_data *data = ptr;
384faf60af1SCyrill Gorcunov 	struct proc_inode *ei;
385faf60af1SCyrill Gorcunov 	struct inode *inode;
386faf60af1SCyrill Gorcunov 
3877bc3fa01SKalesh Singh 	inode = proc_pid_make_inode(dentry->d_sb, task, S_IFREG | S_IRUGO);
388faf60af1SCyrill Gorcunov 	if (!inode)
3890168b9e3SAl Viro 		return ERR_PTR(-ENOENT);
390faf60af1SCyrill Gorcunov 
391faf60af1SCyrill Gorcunov 	ei = PROC_I(inode);
39298836386SAl Viro 	ei->fd = data->fd;
393faf60af1SCyrill Gorcunov 
3940c08b92fSTyler Hicks (Microsoft) 	inode->i_op = &proc_fdinfo_file_inode_operations;
3950c08b92fSTyler Hicks (Microsoft) 
396faf60af1SCyrill Gorcunov 	inode->i_fop = &proc_fdinfo_file_operations;
39798836386SAl Viro 	tid_fd_update_inode(task, inode, 0);
398faf60af1SCyrill Gorcunov 
399faf60af1SCyrill Gorcunov 	d_set_d_op(dentry, &tid_fd_dentry_operations);
4000168b9e3SAl Viro 	return d_splice_alias(inode, dentry);
401faf60af1SCyrill Gorcunov }
402faf60af1SCyrill Gorcunov 
403faf60af1SCyrill Gorcunov static struct dentry *
proc_lookupfdinfo(struct inode * dir,struct dentry * dentry,unsigned int flags)404faf60af1SCyrill Gorcunov proc_lookupfdinfo(struct inode *dir, struct dentry *dentry, unsigned int flags)
405faf60af1SCyrill Gorcunov {
406faf60af1SCyrill Gorcunov 	return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
407faf60af1SCyrill Gorcunov }
408faf60af1SCyrill Gorcunov 
proc_readfdinfo(struct file * file,struct dir_context * ctx)409f0c3b509SAl Viro static int proc_readfdinfo(struct file *file, struct dir_context *ctx)
410faf60af1SCyrill Gorcunov {
411f0c3b509SAl Viro 	return proc_readfd_common(file, ctx,
412faf60af1SCyrill Gorcunov 				  proc_fdinfo_instantiate);
413faf60af1SCyrill Gorcunov }
414faf60af1SCyrill Gorcunov 
415faf60af1SCyrill Gorcunov const struct inode_operations proc_fdinfo_inode_operations = {
416faf60af1SCyrill Gorcunov 	.lookup		= proc_lookupfdinfo,
4170c08b92fSTyler Hicks (Microsoft) 	.permission	= proc_fdinfo_permission,
418faf60af1SCyrill Gorcunov 	.setattr	= proc_setattr,
419faf60af1SCyrill Gorcunov };
420faf60af1SCyrill Gorcunov 
421faf60af1SCyrill Gorcunov const struct file_operations proc_fdinfo_operations = {
422faf60af1SCyrill Gorcunov 	.read		= generic_read_dir,
423f50752eaSAl Viro 	.iterate_shared	= proc_readfdinfo,
424f50752eaSAl Viro 	.llseek		= generic_file_llseek,
425faf60af1SCyrill Gorcunov };
426