blob: be39c6feb3e56c5f12bd1afab9c7b1867959b77c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * proc/fs/generic.c --- generic routines for the proc-fs
3 *
4 * This file contains generic proc-fs routines for handling
5 * directories and files.
6 *
7 * Copyright (C) 1991, 1992 Linus Torvalds.
8 * Copyright (C) 1997 Theodore Ts'o
9 */
10
11#include <linux/errno.h>
12#include <linux/time.h>
13#include <linux/proc_fs.h>
14#include <linux/stat.h>
Christoph Hellwig10257742010-06-04 11:30:02 +020015#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Andrew Morton87ebdc02013-02-27 17:03:16 -080018#include <linux/printk.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/init.h>
21#include <linux/idr.h>
22#include <linux/namei.h>
23#include <linux/bitops.h>
Steven Rostedt64a07bd2006-03-26 01:36:55 -080024#include <linux/spinlock.h>
Alexey Dobriyan786d7e12007-07-15 23:39:00 -070025#include <linux/completion.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <asm/uaccess.h>
27
Adrian Bunkfee781e2006-01-08 01:04:16 -080028#include "internal.h"
29
Alexey Dobriyan4dcc03f2014-08-08 14:21:29 -070030static DEFINE_SPINLOCK(proc_subdir_lock);
Steven Rostedt64a07bd2006-03-26 01:36:55 -080031
Alexey Dobriyan312ec7e2011-03-23 16:42:52 -070032static int proc_match(unsigned int len, const char *name, struct proc_dir_entry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033{
Nicolas Dichtel710585d2014-12-10 15:45:01 -080034 if (len < de->namelen)
35 return -1;
36 if (len > de->namelen)
37 return 1;
38
39 return memcmp(name, de->name, len);
40}
41
42static struct proc_dir_entry *pde_subdir_first(struct proc_dir_entry *dir)
43{
44 struct rb_node *node = rb_first(&dir->subdir);
45
46 if (node == NULL)
47 return NULL;
48
49 return rb_entry(node, struct proc_dir_entry, subdir_node);
50}
51
52static struct proc_dir_entry *pde_subdir_next(struct proc_dir_entry *dir)
53{
54 struct rb_node *node = rb_next(&dir->subdir_node);
55
56 if (node == NULL)
57 return NULL;
58
59 return rb_entry(node, struct proc_dir_entry, subdir_node);
60}
61
62static struct proc_dir_entry *pde_subdir_find(struct proc_dir_entry *dir,
63 const char *name,
64 unsigned int len)
65{
66 struct rb_node *node = dir->subdir.rb_node;
67
68 while (node) {
69 struct proc_dir_entry *de = container_of(node,
70 struct proc_dir_entry,
71 subdir_node);
72 int result = proc_match(len, name, de);
73
74 if (result < 0)
75 node = node->rb_left;
76 else if (result > 0)
77 node = node->rb_right;
78 else
79 return de;
80 }
81 return NULL;
82}
83
84static bool pde_subdir_insert(struct proc_dir_entry *dir,
85 struct proc_dir_entry *de)
86{
87 struct rb_root *root = &dir->subdir;
88 struct rb_node **new = &root->rb_node, *parent = NULL;
89
90 /* Figure out where to put new node */
91 while (*new) {
92 struct proc_dir_entry *this =
93 container_of(*new, struct proc_dir_entry, subdir_node);
94 int result = proc_match(de->namelen, de->name, this);
95
96 parent = *new;
97 if (result < 0)
98 new = &(*new)->rb_left;
99 else if (result > 0)
100 new = &(*new)->rb_right;
101 else
102 return false;
103 }
104
105 /* Add new node and rebalance tree. */
106 rb_link_node(&de->subdir_node, parent, new);
107 rb_insert_color(&de->subdir_node, root);
108 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109}
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111static int proc_notify_change(struct dentry *dentry, struct iattr *iattr)
112{
113 struct inode *inode = dentry->d_inode;
114 struct proc_dir_entry *de = PDE(inode);
115 int error;
116
117 error = inode_change_ok(inode, iattr);
118 if (error)
Christoph Hellwig10257742010-06-04 11:30:02 +0200119 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Christoph Hellwig10257742010-06-04 11:30:02 +0200121 setattr_copy(inode, iattr);
122 mark_inode_dirty(inode);
Marco Stornelli46f69552012-12-15 11:48:48 +0100123
Rui Xiangcdf7e8d2014-01-23 15:55:41 -0800124 proc_set_user(de, inode->i_uid, inode->i_gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 de->mode = inode->i_mode;
Christoph Hellwig10257742010-06-04 11:30:02 +0200126 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127}
128
Miklos Szeredi2b579be2005-09-06 15:17:18 -0700129static int proc_getattr(struct vfsmount *mnt, struct dentry *dentry,
130 struct kstat *stat)
131{
132 struct inode *inode = dentry->d_inode;
133 struct proc_dir_entry *de = PROC_I(inode)->pde;
134 if (de && de->nlink)
Miklos Szeredibfe86842011-10-28 14:13:29 +0200135 set_nlink(inode, de->nlink);
Miklos Szeredi2b579be2005-09-06 15:17:18 -0700136
137 generic_fillattr(inode, stat);
138 return 0;
139}
140
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800141static const struct inode_operations proc_file_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 .setattr = proc_notify_change,
143};
144
145/*
146 * This function parses a name such as "tty/driver/serial", and
147 * returns the struct proc_dir_entry for "/proc/tty/driver", and
148 * returns "serial" in residual.
149 */
Alexey Dobriyane17a5762010-03-05 13:43:59 -0800150static int __xlate_proc_name(const char *name, struct proc_dir_entry **ret,
151 const char **residual)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
153 const char *cp = name, *next;
154 struct proc_dir_entry *de;
Alexey Dobriyan312ec7e2011-03-23 16:42:52 -0700155 unsigned int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Alexey Dobriyan7cee4e02008-04-29 01:01:40 -0700157 de = *ret;
158 if (!de)
159 de = &proc_root;
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 while (1) {
162 next = strchr(cp, '/');
163 if (!next)
164 break;
165
166 len = next - cp;
Nicolas Dichtel710585d2014-12-10 15:45:01 -0800167 de = pde_subdir_find(de, cp, len);
Alexey Dobriyan12bac0d2010-03-05 13:44:00 -0800168 if (!de) {
169 WARN(1, "name '%s'\n", name);
Alexey Dobriyane17a5762010-03-05 13:43:59 -0800170 return -ENOENT;
Alexey Dobriyan12bac0d2010-03-05 13:44:00 -0800171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 cp += len + 1;
173 }
174 *residual = cp;
175 *ret = de;
Alexey Dobriyane17a5762010-03-05 13:43:59 -0800176 return 0;
177}
178
179static int xlate_proc_name(const char *name, struct proc_dir_entry **ret,
180 const char **residual)
181{
182 int rv;
183
184 spin_lock(&proc_subdir_lock);
185 rv = __xlate_proc_name(name, ret, residual);
Steven Rostedt64a07bd2006-03-26 01:36:55 -0800186 spin_unlock(&proc_subdir_lock);
Alexey Dobriyane17a5762010-03-05 13:43:59 -0800187 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
189
Alexey Dobriyan9a185402008-07-26 11:21:37 +0400190static DEFINE_IDA(proc_inum_ida);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191static DEFINE_SPINLOCK(proc_inum_lock); /* protects the above */
192
Alexey Dobriyan67935df2008-07-26 11:18:28 +0400193#define PROC_DYNAMIC_FIRST 0xF0000000U
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195/*
196 * Return an inode number between PROC_DYNAMIC_FIRST and
197 * 0xffffffff, or zero on failure.
198 */
Eric W. Biederman33d6dce2011-06-17 13:33:20 -0700199int proc_alloc_inum(unsigned int *inum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Alexey Dobriyan67935df2008-07-26 11:18:28 +0400201 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 int error;
203
204retry:
Eric W. Biederman33d6dce2011-06-17 13:33:20 -0700205 if (!ida_pre_get(&proc_inum_ida, GFP_KERNEL))
206 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Eric W. Biedermandfb2ea42012-12-21 20:38:00 -0800208 spin_lock_irq(&proc_inum_lock);
Alexey Dobriyan9a185402008-07-26 11:21:37 +0400209 error = ida_get_new(&proc_inum_ida, &i);
Eric W. Biedermandfb2ea42012-12-21 20:38:00 -0800210 spin_unlock_irq(&proc_inum_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 if (error == -EAGAIN)
212 goto retry;
213 else if (error)
Eric W. Biederman33d6dce2011-06-17 13:33:20 -0700214 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Alexey Dobriyan67935df2008-07-26 11:18:28 +0400216 if (i > UINT_MAX - PROC_DYNAMIC_FIRST) {
Eric W. Biedermandfb2ea42012-12-21 20:38:00 -0800217 spin_lock_irq(&proc_inum_lock);
Alexey Dobriyan9a185402008-07-26 11:21:37 +0400218 ida_remove(&proc_inum_ida, i);
Eric W. Biedermandfb2ea42012-12-21 20:38:00 -0800219 spin_unlock_irq(&proc_inum_lock);
Eric W. Biederman33d6dce2011-06-17 13:33:20 -0700220 return -ENOSPC;
Alexey Dobriyan67935df2008-07-26 11:18:28 +0400221 }
Eric W. Biederman33d6dce2011-06-17 13:33:20 -0700222 *inum = PROC_DYNAMIC_FIRST + i;
223 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224}
225
Eric W. Biederman33d6dce2011-06-17 13:33:20 -0700226void proc_free_inum(unsigned int inum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
Eric W. Biedermandfb2ea42012-12-21 20:38:00 -0800228 unsigned long flags;
229 spin_lock_irqsave(&proc_inum_lock, flags);
Alexey Dobriyan9a185402008-07-26 11:21:37 +0400230 ida_remove(&proc_inum_ida, inum - PROC_DYNAMIC_FIRST);
Eric W. Biedermandfb2ea42012-12-21 20:38:00 -0800231 spin_unlock_irqrestore(&proc_inum_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
Al Viro008b1502005-08-20 00:17:39 +0100234static void *proc_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
David Howellsc30480b2013-04-12 18:03:36 +0100236 nd_set_link(nd, __PDE_DATA(dentry->d_inode));
Al Viro008b1502005-08-20 00:17:39 +0100237 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
239
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800240static const struct inode_operations proc_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 .readlink = generic_readlink,
242 .follow_link = proc_follow_link,
243};
244
245/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 * Don't create negative dentries here, return -ENOENT by hand
247 * instead.
248 */
Pavel Emelyanove9720ac2008-03-07 11:08:40 -0800249struct dentry *proc_lookup_de(struct proc_dir_entry *de, struct inode *dir,
250 struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
Al Virod3d009c2013-01-25 20:11:22 -0500252 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Steven Rostedt64a07bd2006-03-26 01:36:55 -0800254 spin_lock(&proc_subdir_lock);
Nicolas Dichtel710585d2014-12-10 15:45:01 -0800255 de = pde_subdir_find(de, dentry->d_name.name, dentry->d_name.len);
256 if (de) {
257 pde_get(de);
258 spin_unlock(&proc_subdir_lock);
259 inode = proc_get_inode(dir->i_sb, de);
260 if (!inode)
261 return ERR_PTR(-ENOMEM);
262 d_set_d_op(dentry, &simple_dentry_operations);
263 d_add(dentry, inode);
264 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 }
Steven Rostedt64a07bd2006-03-26 01:36:55 -0800266 spin_unlock(&proc_subdir_lock);
Al Virod3d009c2013-01-25 20:11:22 -0500267 return ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268}
269
Pavel Emelyanove9720ac2008-03-07 11:08:40 -0800270struct dentry *proc_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -0400271 unsigned int flags)
Pavel Emelyanove9720ac2008-03-07 11:08:40 -0800272{
273 return proc_lookup_de(PDE(dir), dir, dentry);
274}
275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276/*
277 * This returns non-zero if at EOF, so that the /proc
278 * root directory can use this and check if it should
279 * continue with the <pid> entries..
280 *
281 * Note that the VFS-layer doesn't care about the return
282 * value of the readdir() call, as long as it's non-negative
283 * for success..
284 */
Al Virof0c3b502013-05-16 12:07:31 -0400285int proc_readdir_de(struct proc_dir_entry *de, struct file *file,
286 struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Al Virof0c3b502013-05-16 12:07:31 -0400290 if (!dir_emit_dots(file, ctx))
291 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Al Virof0c3b502013-05-16 12:07:31 -0400293 spin_lock(&proc_subdir_lock);
Nicolas Dichtel710585d2014-12-10 15:45:01 -0800294 de = pde_subdir_first(de);
Al Virof0c3b502013-05-16 12:07:31 -0400295 i = ctx->pos - 2;
296 for (;;) {
297 if (!de) {
Steven Rostedt64a07bd2006-03-26 01:36:55 -0800298 spin_unlock(&proc_subdir_lock);
Al Virof0c3b502013-05-16 12:07:31 -0400299 return 0;
300 }
301 if (!i)
302 break;
Nicolas Dichtel710585d2014-12-10 15:45:01 -0800303 de = pde_subdir_next(de);
Al Virof0c3b502013-05-16 12:07:31 -0400304 i--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 }
Al Virof0c3b502013-05-16 12:07:31 -0400306
307 do {
308 struct proc_dir_entry *next;
309 pde_get(de);
310 spin_unlock(&proc_subdir_lock);
311 if (!dir_emit(ctx, de->name, de->namelen,
312 de->low_ino, de->mode >> 12)) {
313 pde_put(de);
314 return 0;
315 }
316 spin_lock(&proc_subdir_lock);
317 ctx->pos++;
Nicolas Dichtel710585d2014-12-10 15:45:01 -0800318 next = pde_subdir_next(de);
Al Virof0c3b502013-05-16 12:07:31 -0400319 pde_put(de);
320 de = next;
321 } while (de);
322 spin_unlock(&proc_subdir_lock);
Linus Torvaldsfd3930f2013-08-19 16:26:12 -0700323 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324}
325
Al Virof0c3b502013-05-16 12:07:31 -0400326int proc_readdir(struct file *file, struct dir_context *ctx)
Pavel Emelyanove9720ac2008-03-07 11:08:40 -0800327{
Al Virof0c3b502013-05-16 12:07:31 -0400328 struct inode *inode = file_inode(file);
Pavel Emelyanove9720ac2008-03-07 11:08:40 -0800329
Al Virof0c3b502013-05-16 12:07:31 -0400330 return proc_readdir_de(PDE(inode), file, ctx);
Pavel Emelyanove9720ac2008-03-07 11:08:40 -0800331}
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333/*
334 * These are the generic /proc directory operations. They
335 * use the in-memory "struct proc_dir_entry" tree to parse
336 * the /proc directory.
337 */
Arjan van de Ven00977a52007-02-12 00:55:34 -0800338static const struct file_operations proc_dir_operations = {
Alexey Dobriyanb4df2b92008-10-27 22:48:36 +0300339 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 .read = generic_read_dir,
Al Virof0c3b502013-05-16 12:07:31 -0400341 .iterate = proc_readdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342};
343
344/*
345 * proc directories can do almost nothing..
346 */
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800347static const struct inode_operations proc_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 .lookup = proc_lookup,
Miklos Szeredi2b579be2005-09-06 15:17:18 -0700349 .getattr = proc_getattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 .setattr = proc_notify_change,
351};
352
353static int proc_register(struct proc_dir_entry * dir, struct proc_dir_entry * dp)
354{
Eric W. Biederman33d6dce2011-06-17 13:33:20 -0700355 int ret;
Nicolas Dichtel710585d2014-12-10 15:45:01 -0800356
Eric W. Biederman33d6dce2011-06-17 13:33:20 -0700357 ret = proc_alloc_inum(&dp->low_ino);
358 if (ret)
359 return ret;
Steven Rostedt64a07bd2006-03-26 01:36:55 -0800360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 if (S_ISDIR(dp->mode)) {
Al Virob6cdc732013-03-30 21:20:14 -0400362 dp->proc_fops = &proc_dir_operations;
363 dp->proc_iops = &proc_dir_inode_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 dir->nlink++;
365 } else if (S_ISLNK(dp->mode)) {
Al Virob6cdc732013-03-30 21:20:14 -0400366 dp->proc_iops = &proc_link_inode_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 } else if (S_ISREG(dp->mode)) {
David Howells3cb5bf12013-04-11 03:20:50 +0100368 BUG_ON(dp->proc_fops == NULL);
Al Virob6cdc732013-03-30 21:20:14 -0400369 dp->proc_iops = &proc_file_inode_operations;
370 } else {
371 WARN_ON(1);
Debabrata Banerjeeb208d542014-12-10 15:45:04 -0800372 proc_free_inum(dp->low_ino);
Al Virob6cdc732013-03-30 21:20:14 -0400373 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 }
Changli Gao99fc06d2007-07-15 23:40:09 -0700375
376 spin_lock(&proc_subdir_lock);
Changli Gao99fc06d2007-07-15 23:40:09 -0700377 dp->parent = dir;
Debabrata Banerjeeb208d542014-12-10 15:45:04 -0800378 if (pde_subdir_insert(dir, dp) == false) {
Nicolas Dichtel710585d2014-12-10 15:45:01 -0800379 WARN(1, "proc_dir_entry '%s/%s' already registered\n",
380 dir->name, dp->name);
Debabrata Banerjeeb208d542014-12-10 15:45:04 -0800381 spin_unlock(&proc_subdir_lock);
382 if (S_ISDIR(dp->mode))
383 dir->nlink--;
384 proc_free_inum(dp->low_ino);
385 return -EEXIST;
386 }
Changli Gao99fc06d2007-07-15 23:40:09 -0700387 spin_unlock(&proc_subdir_lock);
388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 return 0;
390}
391
Alexey Dobriyan2d3a4e32008-02-08 04:18:37 -0800392static struct proc_dir_entry *__proc_create(struct proc_dir_entry **parent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 const char *name,
Al Virod161a132011-07-24 03:36:29 -0400394 umode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 nlink_t nlink)
396{
397 struct proc_dir_entry *ent = NULL;
Alexey Dobriyandbcdb502014-08-08 14:21:25 -0700398 const char *fn;
399 struct qstr qstr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Alexey Dobriyan7cee4e02008-04-29 01:01:40 -0700401 if (xlate_proc_name(name, parent, &fn) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 goto out;
Alexey Dobriyandbcdb502014-08-08 14:21:25 -0700403 qstr.name = fn;
404 qstr.len = strlen(fn);
405 if (qstr.len == 0 || qstr.len >= 256) {
406 WARN(1, "name len %u\n", qstr.len);
407 return NULL;
408 }
409 if (*parent == &proc_root && name_to_int(&qstr) != ~0U) {
410 WARN(1, "create '/proc/%s' by hand\n", qstr.name);
411 return NULL;
412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Alexey Dobriyandbcdb502014-08-08 14:21:25 -0700414 ent = kzalloc(sizeof(struct proc_dir_entry) + qstr.len + 1, GFP_KERNEL);
yan17baa2a2012-10-04 17:15:43 -0700415 if (!ent)
416 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Alexey Dobriyandbcdb502014-08-08 14:21:25 -0700418 memcpy(ent->name, fn, qstr.len + 1);
419 ent->namelen = qstr.len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 ent->mode = mode;
421 ent->nlink = nlink;
Nicolas Dichtel710585d2014-12-10 15:45:01 -0800422 ent->subdir = RB_ROOT;
Alexey Dobriyan5a622f22007-12-04 23:45:28 -0800423 atomic_set(&ent->count, 1);
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700424 spin_lock_init(&ent->pde_unload_lock);
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700425 INIT_LIST_HEAD(&ent->pde_openers);
yan17baa2a2012-10-04 17:15:43 -0700426out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 return ent;
428}
429
430struct proc_dir_entry *proc_symlink(const char *name,
431 struct proc_dir_entry *parent, const char *dest)
432{
433 struct proc_dir_entry *ent;
434
Alexey Dobriyan2d3a4e32008-02-08 04:18:37 -0800435 ent = __proc_create(&parent, name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 (S_IFLNK | S_IRUGO | S_IWUGO | S_IXUGO),1);
437
438 if (ent) {
439 ent->data = kmalloc((ent->size=strlen(dest))+1, GFP_KERNEL);
440 if (ent->data) {
441 strcpy((char*)ent->data,dest);
442 if (proc_register(parent, ent) < 0) {
443 kfree(ent->data);
444 kfree(ent);
445 ent = NULL;
446 }
447 } else {
448 kfree(ent);
449 ent = NULL;
450 }
451 }
452 return ent;
453}
Helight.Xu587d4a12009-12-30 13:24:41 +0800454EXPORT_SYMBOL(proc_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
David Howells270b5ac2013-04-12 02:48:30 +0100456struct proc_dir_entry *proc_mkdir_data(const char *name, umode_t mode,
457 struct proc_dir_entry *parent, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
459 struct proc_dir_entry *ent;
460
David Howells270b5ac2013-04-12 02:48:30 +0100461 if (mode == 0)
462 mode = S_IRUGO | S_IXUGO;
463
Alexey Dobriyan2d3a4e32008-02-08 04:18:37 -0800464 ent = __proc_create(&parent, name, S_IFDIR | mode, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 if (ent) {
David Howells270b5ac2013-04-12 02:48:30 +0100466 ent->data = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 if (proc_register(parent, ent) < 0) {
468 kfree(ent);
469 ent = NULL;
470 }
471 }
472 return ent;
473}
David Howells270b5ac2013-04-12 02:48:30 +0100474EXPORT_SYMBOL_GPL(proc_mkdir_data);
475
476struct proc_dir_entry *proc_mkdir_mode(const char *name, umode_t mode,
477 struct proc_dir_entry *parent)
478{
479 return proc_mkdir_data(name, mode, parent, NULL);
480}
Alexey Dobriyan011159a2011-05-14 00:12:48 +0300481EXPORT_SYMBOL(proc_mkdir_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483struct proc_dir_entry *proc_mkdir(const char *name,
484 struct proc_dir_entry *parent)
485{
David Howells270b5ac2013-04-12 02:48:30 +0100486 return proc_mkdir_data(name, 0, parent, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487}
Helight.Xu587d4a12009-12-30 13:24:41 +0800488EXPORT_SYMBOL(proc_mkdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Al Virod161a132011-07-24 03:36:29 -0400490struct proc_dir_entry *proc_create_data(const char *name, umode_t mode,
Denis V. Lunev59b74352008-04-29 01:02:00 -0700491 struct proc_dir_entry *parent,
492 const struct file_operations *proc_fops,
493 void *data)
Alexey Dobriyan2d3a4e32008-02-08 04:18:37 -0800494{
495 struct proc_dir_entry *pde;
Al Virob6cdc732013-03-30 21:20:14 -0400496 if ((mode & S_IFMT) == 0)
497 mode |= S_IFREG;
Alexey Dobriyan2d3a4e32008-02-08 04:18:37 -0800498
Al Virob6cdc732013-03-30 21:20:14 -0400499 if (!S_ISREG(mode)) {
500 WARN_ON(1); /* use proc_mkdir() */
501 return NULL;
Alexey Dobriyan2d3a4e32008-02-08 04:18:37 -0800502 }
503
Al Virob6cdc732013-03-30 21:20:14 -0400504 if ((mode & S_IALLUGO) == 0)
505 mode |= S_IRUGO;
506 pde = __proc_create(&parent, name, mode, 1);
Alexey Dobriyan2d3a4e32008-02-08 04:18:37 -0800507 if (!pde)
508 goto out;
509 pde->proc_fops = proc_fops;
Denis V. Lunev59b74352008-04-29 01:02:00 -0700510 pde->data = data;
Alexey Dobriyan2d3a4e32008-02-08 04:18:37 -0800511 if (proc_register(parent, pde) < 0)
512 goto out_free;
513 return pde;
514out_free:
515 kfree(pde);
516out:
517 return NULL;
518}
Helight.Xu587d4a12009-12-30 13:24:41 +0800519EXPORT_SYMBOL(proc_create_data);
David Howells271a15e2013-04-12 00:38:51 +0100520
521void proc_set_size(struct proc_dir_entry *de, loff_t size)
522{
523 de->size = size;
524}
525EXPORT_SYMBOL(proc_set_size);
526
527void proc_set_user(struct proc_dir_entry *de, kuid_t uid, kgid_t gid)
528{
529 de->uid = uid;
530 de->gid = gid;
531}
532EXPORT_SYMBOL(proc_set_user);
Alexey Dobriyan2d3a4e32008-02-08 04:18:37 -0800533
Alexey Dobriyan135d5652009-12-15 16:45:39 -0800534static void free_proc_entry(struct proc_dir_entry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
Eric W. Biederman33d6dce2011-06-17 13:33:20 -0700536 proc_free_inum(de->low_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Alexey Dobriyanfd2cbe42008-02-08 04:18:28 -0800538 if (S_ISLNK(de->mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 kfree(de->data);
540 kfree(de);
541}
542
Alexey Dobriyan135d5652009-12-15 16:45:39 -0800543void pde_put(struct proc_dir_entry *pde)
544{
545 if (atomic_dec_and_test(&pde->count))
546 free_proc_entry(pde);
547}
548
Al Viro8ce584c2013-03-30 20:13:46 -0400549/*
550 * Remove a /proc entry and free it if it's not currently in use.
551 */
552void remove_proc_entry(const char *name, struct proc_dir_entry *parent)
553{
Al Viro8ce584c2013-03-30 20:13:46 -0400554 struct proc_dir_entry *de = NULL;
555 const char *fn = name;
556 unsigned int len;
557
558 spin_lock(&proc_subdir_lock);
559 if (__xlate_proc_name(name, &parent, &fn) != 0) {
560 spin_unlock(&proc_subdir_lock);
561 return;
562 }
563 len = strlen(fn);
564
Nicolas Dichtel710585d2014-12-10 15:45:01 -0800565 de = pde_subdir_find(parent, fn, len);
566 if (de)
567 rb_erase(&de->subdir_node, &parent->subdir);
Al Viro8ce584c2013-03-30 20:13:46 -0400568 spin_unlock(&proc_subdir_lock);
569 if (!de) {
570 WARN(1, "name '%s'\n", name);
571 return;
572 }
573
Al Viro866ad9a72013-04-03 19:07:30 -0400574 proc_entry_rundown(de);
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700575
Alexey Dobriyanf649d6d2008-04-29 01:01:39 -0700576 if (S_ISDIR(de->mode))
577 parent->nlink--;
578 de->nlink = 0;
Nicolas Dichtel710585d2014-12-10 15:45:01 -0800579 WARN(pde_subdir_first(de),
580 "%s: removing non-empty directory '%s/%s', leaking at least '%s'\n",
581 __func__, de->parent->name, de->name, pde_subdir_first(de)->name);
Alexey Dobriyan135d5652009-12-15 16:45:39 -0800582 pde_put(de);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583}
Helight.Xu587d4a12009-12-30 13:24:41 +0800584EXPORT_SYMBOL(remove_proc_entry);
Al Viro8ce584c2013-03-30 20:13:46 -0400585
586int remove_proc_subtree(const char *name, struct proc_dir_entry *parent)
587{
Al Viro8ce584c2013-03-30 20:13:46 -0400588 struct proc_dir_entry *root = NULL, *de, *next;
589 const char *fn = name;
590 unsigned int len;
591
592 spin_lock(&proc_subdir_lock);
593 if (__xlate_proc_name(name, &parent, &fn) != 0) {
594 spin_unlock(&proc_subdir_lock);
595 return -ENOENT;
596 }
597 len = strlen(fn);
598
Nicolas Dichtel710585d2014-12-10 15:45:01 -0800599 root = pde_subdir_find(parent, fn, len);
Al Viro8ce584c2013-03-30 20:13:46 -0400600 if (!root) {
601 spin_unlock(&proc_subdir_lock);
602 return -ENOENT;
603 }
Nicolas Dichtel710585d2014-12-10 15:45:01 -0800604 rb_erase(&root->subdir_node, &parent->subdir);
605
Al Viro8ce584c2013-03-30 20:13:46 -0400606 de = root;
607 while (1) {
Nicolas Dichtel710585d2014-12-10 15:45:01 -0800608 next = pde_subdir_first(de);
Al Viro8ce584c2013-03-30 20:13:46 -0400609 if (next) {
Nicolas Dichtel710585d2014-12-10 15:45:01 -0800610 rb_erase(&next->subdir_node, &de->subdir);
Al Viro8ce584c2013-03-30 20:13:46 -0400611 de = next;
612 continue;
613 }
614 spin_unlock(&proc_subdir_lock);
615
Al Viro866ad9a72013-04-03 19:07:30 -0400616 proc_entry_rundown(de);
Al Viro8ce584c2013-03-30 20:13:46 -0400617 next = de->parent;
618 if (S_ISDIR(de->mode))
619 next->nlink--;
620 de->nlink = 0;
621 if (de == root)
622 break;
623 pde_put(de);
624
625 spin_lock(&proc_subdir_lock);
626 de = next;
627 }
628 pde_put(root);
629 return 0;
630}
631EXPORT_SYMBOL(remove_proc_subtree);
David Howells4a520d22013-04-12 14:06:01 +0100632
633void *proc_get_parent_data(const struct inode *inode)
634{
635 struct proc_dir_entry *de = PDE(inode);
636 return de->parent->data;
637}
638EXPORT_SYMBOL_GPL(proc_get_parent_data);
David Howellsa8ca16e2013-04-12 17:27:28 +0100639
640void proc_remove(struct proc_dir_entry *de)
641{
642 if (de)
643 remove_proc_subtree(de->name, de->parent);
644}
645EXPORT_SYMBOL(proc_remove);
David Howellsc30480b2013-04-12 18:03:36 +0100646
647void *PDE_DATA(const struct inode *inode)
648{
649 return __PDE_DATA(inode);
650}
651EXPORT_SYMBOL(PDE_DATA);