blob: 9f8fa1e5e8aa82b02318862ed8558252ec2c7a4e [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);
372 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 }
Changli Gao99fc06d2007-07-15 23:40:09 -0700374
375 spin_lock(&proc_subdir_lock);
Changli Gao99fc06d2007-07-15 23:40:09 -0700376 dp->parent = dir;
Nicolas Dichtel710585d2014-12-10 15:45:01 -0800377 if (pde_subdir_insert(dir, dp) == false)
378 WARN(1, "proc_dir_entry '%s/%s' already registered\n",
379 dir->name, dp->name);
Changli Gao99fc06d2007-07-15 23:40:09 -0700380 spin_unlock(&proc_subdir_lock);
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 return 0;
383}
384
Alexey Dobriyan2d3a4e32008-02-08 04:18:37 -0800385static struct proc_dir_entry *__proc_create(struct proc_dir_entry **parent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 const char *name,
Al Virod161a132011-07-24 03:36:29 -0400387 umode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 nlink_t nlink)
389{
390 struct proc_dir_entry *ent = NULL;
Alexey Dobriyandbcdb502014-08-08 14:21:25 -0700391 const char *fn;
392 struct qstr qstr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Alexey Dobriyan7cee4e02008-04-29 01:01:40 -0700394 if (xlate_proc_name(name, parent, &fn) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 goto out;
Alexey Dobriyandbcdb502014-08-08 14:21:25 -0700396 qstr.name = fn;
397 qstr.len = strlen(fn);
398 if (qstr.len == 0 || qstr.len >= 256) {
399 WARN(1, "name len %u\n", qstr.len);
400 return NULL;
401 }
402 if (*parent == &proc_root && name_to_int(&qstr) != ~0U) {
403 WARN(1, "create '/proc/%s' by hand\n", qstr.name);
404 return NULL;
405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Alexey Dobriyandbcdb502014-08-08 14:21:25 -0700407 ent = kzalloc(sizeof(struct proc_dir_entry) + qstr.len + 1, GFP_KERNEL);
yan17baa2a2012-10-04 17:15:43 -0700408 if (!ent)
409 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Alexey Dobriyandbcdb502014-08-08 14:21:25 -0700411 memcpy(ent->name, fn, qstr.len + 1);
412 ent->namelen = qstr.len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 ent->mode = mode;
414 ent->nlink = nlink;
Nicolas Dichtel710585d2014-12-10 15:45:01 -0800415 ent->subdir = RB_ROOT;
Alexey Dobriyan5a622f22007-12-04 23:45:28 -0800416 atomic_set(&ent->count, 1);
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700417 spin_lock_init(&ent->pde_unload_lock);
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700418 INIT_LIST_HEAD(&ent->pde_openers);
yan17baa2a2012-10-04 17:15:43 -0700419out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 return ent;
421}
422
423struct proc_dir_entry *proc_symlink(const char *name,
424 struct proc_dir_entry *parent, const char *dest)
425{
426 struct proc_dir_entry *ent;
427
Alexey Dobriyan2d3a4e32008-02-08 04:18:37 -0800428 ent = __proc_create(&parent, name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 (S_IFLNK | S_IRUGO | S_IWUGO | S_IXUGO),1);
430
431 if (ent) {
432 ent->data = kmalloc((ent->size=strlen(dest))+1, GFP_KERNEL);
433 if (ent->data) {
434 strcpy((char*)ent->data,dest);
435 if (proc_register(parent, ent) < 0) {
436 kfree(ent->data);
437 kfree(ent);
438 ent = NULL;
439 }
440 } else {
441 kfree(ent);
442 ent = NULL;
443 }
444 }
445 return ent;
446}
Helight.Xu587d4a12009-12-30 13:24:41 +0800447EXPORT_SYMBOL(proc_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
David Howells270b5ac2013-04-12 02:48:30 +0100449struct proc_dir_entry *proc_mkdir_data(const char *name, umode_t mode,
450 struct proc_dir_entry *parent, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
452 struct proc_dir_entry *ent;
453
David Howells270b5ac2013-04-12 02:48:30 +0100454 if (mode == 0)
455 mode = S_IRUGO | S_IXUGO;
456
Alexey Dobriyan2d3a4e32008-02-08 04:18:37 -0800457 ent = __proc_create(&parent, name, S_IFDIR | mode, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 if (ent) {
David Howells270b5ac2013-04-12 02:48:30 +0100459 ent->data = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 if (proc_register(parent, ent) < 0) {
461 kfree(ent);
462 ent = NULL;
463 }
464 }
465 return ent;
466}
David Howells270b5ac2013-04-12 02:48:30 +0100467EXPORT_SYMBOL_GPL(proc_mkdir_data);
468
469struct proc_dir_entry *proc_mkdir_mode(const char *name, umode_t mode,
470 struct proc_dir_entry *parent)
471{
472 return proc_mkdir_data(name, mode, parent, NULL);
473}
Alexey Dobriyan011159a2011-05-14 00:12:48 +0300474EXPORT_SYMBOL(proc_mkdir_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476struct proc_dir_entry *proc_mkdir(const char *name,
477 struct proc_dir_entry *parent)
478{
David Howells270b5ac2013-04-12 02:48:30 +0100479 return proc_mkdir_data(name, 0, parent, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480}
Helight.Xu587d4a12009-12-30 13:24:41 +0800481EXPORT_SYMBOL(proc_mkdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Al Virod161a132011-07-24 03:36:29 -0400483struct proc_dir_entry *proc_create_data(const char *name, umode_t mode,
Denis V. Lunev59b74352008-04-29 01:02:00 -0700484 struct proc_dir_entry *parent,
485 const struct file_operations *proc_fops,
486 void *data)
Alexey Dobriyan2d3a4e32008-02-08 04:18:37 -0800487{
488 struct proc_dir_entry *pde;
Al Virob6cdc732013-03-30 21:20:14 -0400489 if ((mode & S_IFMT) == 0)
490 mode |= S_IFREG;
Alexey Dobriyan2d3a4e32008-02-08 04:18:37 -0800491
Al Virob6cdc732013-03-30 21:20:14 -0400492 if (!S_ISREG(mode)) {
493 WARN_ON(1); /* use proc_mkdir() */
494 return NULL;
Alexey Dobriyan2d3a4e32008-02-08 04:18:37 -0800495 }
496
Al Virob6cdc732013-03-30 21:20:14 -0400497 if ((mode & S_IALLUGO) == 0)
498 mode |= S_IRUGO;
499 pde = __proc_create(&parent, name, mode, 1);
Alexey Dobriyan2d3a4e32008-02-08 04:18:37 -0800500 if (!pde)
501 goto out;
502 pde->proc_fops = proc_fops;
Denis V. Lunev59b74352008-04-29 01:02:00 -0700503 pde->data = data;
Alexey Dobriyan2d3a4e32008-02-08 04:18:37 -0800504 if (proc_register(parent, pde) < 0)
505 goto out_free;
506 return pde;
507out_free:
508 kfree(pde);
509out:
510 return NULL;
511}
Helight.Xu587d4a12009-12-30 13:24:41 +0800512EXPORT_SYMBOL(proc_create_data);
David Howells271a15e2013-04-12 00:38:51 +0100513
514void proc_set_size(struct proc_dir_entry *de, loff_t size)
515{
516 de->size = size;
517}
518EXPORT_SYMBOL(proc_set_size);
519
520void proc_set_user(struct proc_dir_entry *de, kuid_t uid, kgid_t gid)
521{
522 de->uid = uid;
523 de->gid = gid;
524}
525EXPORT_SYMBOL(proc_set_user);
Alexey Dobriyan2d3a4e32008-02-08 04:18:37 -0800526
Alexey Dobriyan135d5652009-12-15 16:45:39 -0800527static void free_proc_entry(struct proc_dir_entry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
Eric W. Biederman33d6dce2011-06-17 13:33:20 -0700529 proc_free_inum(de->low_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Alexey Dobriyanfd2cbe42008-02-08 04:18:28 -0800531 if (S_ISLNK(de->mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 kfree(de->data);
533 kfree(de);
534}
535
Alexey Dobriyan135d5652009-12-15 16:45:39 -0800536void pde_put(struct proc_dir_entry *pde)
537{
538 if (atomic_dec_and_test(&pde->count))
539 free_proc_entry(pde);
540}
541
Al Viro8ce584c2013-03-30 20:13:46 -0400542/*
543 * Remove a /proc entry and free it if it's not currently in use.
544 */
545void remove_proc_entry(const char *name, struct proc_dir_entry *parent)
546{
Al Viro8ce584c2013-03-30 20:13:46 -0400547 struct proc_dir_entry *de = NULL;
548 const char *fn = name;
549 unsigned int len;
550
551 spin_lock(&proc_subdir_lock);
552 if (__xlate_proc_name(name, &parent, &fn) != 0) {
553 spin_unlock(&proc_subdir_lock);
554 return;
555 }
556 len = strlen(fn);
557
Nicolas Dichtel710585d2014-12-10 15:45:01 -0800558 de = pde_subdir_find(parent, fn, len);
559 if (de)
560 rb_erase(&de->subdir_node, &parent->subdir);
Al Viro8ce584c2013-03-30 20:13:46 -0400561 spin_unlock(&proc_subdir_lock);
562 if (!de) {
563 WARN(1, "name '%s'\n", name);
564 return;
565 }
566
Al Viro866ad9a72013-04-03 19:07:30 -0400567 proc_entry_rundown(de);
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700568
Alexey Dobriyanf649d6d2008-04-29 01:01:39 -0700569 if (S_ISDIR(de->mode))
570 parent->nlink--;
571 de->nlink = 0;
Nicolas Dichtel710585d2014-12-10 15:45:01 -0800572 WARN(pde_subdir_first(de),
573 "%s: removing non-empty directory '%s/%s', leaking at least '%s'\n",
574 __func__, de->parent->name, de->name, pde_subdir_first(de)->name);
Alexey Dobriyan135d5652009-12-15 16:45:39 -0800575 pde_put(de);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576}
Helight.Xu587d4a12009-12-30 13:24:41 +0800577EXPORT_SYMBOL(remove_proc_entry);
Al Viro8ce584c2013-03-30 20:13:46 -0400578
579int remove_proc_subtree(const char *name, struct proc_dir_entry *parent)
580{
Al Viro8ce584c2013-03-30 20:13:46 -0400581 struct proc_dir_entry *root = NULL, *de, *next;
582 const char *fn = name;
583 unsigned int len;
584
585 spin_lock(&proc_subdir_lock);
586 if (__xlate_proc_name(name, &parent, &fn) != 0) {
587 spin_unlock(&proc_subdir_lock);
588 return -ENOENT;
589 }
590 len = strlen(fn);
591
Nicolas Dichtel710585d2014-12-10 15:45:01 -0800592 root = pde_subdir_find(parent, fn, len);
Al Viro8ce584c2013-03-30 20:13:46 -0400593 if (!root) {
594 spin_unlock(&proc_subdir_lock);
595 return -ENOENT;
596 }
Nicolas Dichtel710585d2014-12-10 15:45:01 -0800597 rb_erase(&root->subdir_node, &parent->subdir);
598
Al Viro8ce584c2013-03-30 20:13:46 -0400599 de = root;
600 while (1) {
Nicolas Dichtel710585d2014-12-10 15:45:01 -0800601 next = pde_subdir_first(de);
Al Viro8ce584c2013-03-30 20:13:46 -0400602 if (next) {
Nicolas Dichtel710585d2014-12-10 15:45:01 -0800603 rb_erase(&next->subdir_node, &de->subdir);
Al Viro8ce584c2013-03-30 20:13:46 -0400604 de = next;
605 continue;
606 }
607 spin_unlock(&proc_subdir_lock);
608
Al Viro866ad9a72013-04-03 19:07:30 -0400609 proc_entry_rundown(de);
Al Viro8ce584c2013-03-30 20:13:46 -0400610 next = de->parent;
611 if (S_ISDIR(de->mode))
612 next->nlink--;
613 de->nlink = 0;
614 if (de == root)
615 break;
616 pde_put(de);
617
618 spin_lock(&proc_subdir_lock);
619 de = next;
620 }
621 pde_put(root);
622 return 0;
623}
624EXPORT_SYMBOL(remove_proc_subtree);
David Howells4a520d22013-04-12 14:06:01 +0100625
626void *proc_get_parent_data(const struct inode *inode)
627{
628 struct proc_dir_entry *de = PDE(inode);
629 return de->parent->data;
630}
631EXPORT_SYMBOL_GPL(proc_get_parent_data);
David Howellsa8ca16e2013-04-12 17:27:28 +0100632
633void proc_remove(struct proc_dir_entry *de)
634{
635 if (de)
636 remove_proc_subtree(de->name, de->parent);
637}
638EXPORT_SYMBOL(proc_remove);
David Howellsc30480b2013-04-12 18:03:36 +0100639
640void *PDE_DATA(const struct inode *inode)
641{
642 return __PDE_DATA(inode);
643}
644EXPORT_SYMBOL(PDE_DATA);