blob: 800247a256c9395b54fbb4f0d25b3fb72c6a3948 [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
Alexey Dobriyanb4884f22018-04-10 16:31:52 -070011#include <linux/cache.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/errno.h>
13#include <linux/time.h>
14#include <linux/proc_fs.h>
15#include <linux/stat.h>
Christoph Hellwig10257742010-06-04 11:30:02 +020016#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Andrew Morton87ebdc02013-02-27 17:03:16 -080019#include <linux/printk.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/init.h>
22#include <linux/idr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#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 Torvalds7c0f6ba2016-12-24 11:46:01 -080026#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Adrian Bunkfee781e2006-01-08 01:04:16 -080028#include "internal.h"
29
Waiman Longecf1a3d2015-09-09 15:35:57 -070030static DEFINE_RWLOCK(proc_subdir_lock);
Steven Rostedt64a07bd2006-03-26 01:36:55 -080031
Alexey Dobriyanb4884f22018-04-10 16:31:52 -070032struct kmem_cache *proc_dir_entry_cache __ro_after_init;
33
34void pde_free(struct proc_dir_entry *pde)
35{
36 if (S_ISLNK(pde->mode))
37 kfree(pde->data);
38 if (pde->name != pde->inline_name)
39 kfree(pde->name);
40 kmem_cache_free(proc_dir_entry_cache, pde);
41}
42
Alexey Dobriyan93ad5bc2018-02-06 15:37:31 -080043static int proc_match(const char *name, struct proc_dir_entry *de, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
Nicolas Dichtel710585d2014-12-10 15:45:01 -080045 if (len < de->namelen)
46 return -1;
47 if (len > de->namelen)
48 return 1;
49
50 return memcmp(name, de->name, len);
51}
52
53static struct proc_dir_entry *pde_subdir_first(struct proc_dir_entry *dir)
54{
Davidlohr Bueso410bd5e2017-09-08 16:15:15 -070055 return rb_entry_safe(rb_first_cached(&dir->subdir),
56 struct proc_dir_entry, subdir_node);
Nicolas Dichtel710585d2014-12-10 15:45:01 -080057}
58
59static struct proc_dir_entry *pde_subdir_next(struct proc_dir_entry *dir)
60{
Nicolas Dichtel2fc1e942014-12-10 15:45:07 -080061 return rb_entry_safe(rb_next(&dir->subdir_node), struct proc_dir_entry,
62 subdir_node);
Nicolas Dichtel710585d2014-12-10 15:45:01 -080063}
64
65static struct proc_dir_entry *pde_subdir_find(struct proc_dir_entry *dir,
66 const char *name,
67 unsigned int len)
68{
Davidlohr Bueso410bd5e2017-09-08 16:15:15 -070069 struct rb_node *node = dir->subdir.rb_root.rb_node;
Nicolas Dichtel710585d2014-12-10 15:45:01 -080070
71 while (node) {
Geliang Tang4e4a7fb2017-02-24 15:00:17 -080072 struct proc_dir_entry *de = rb_entry(node,
73 struct proc_dir_entry,
74 subdir_node);
Alexey Dobriyan93ad5bc2018-02-06 15:37:31 -080075 int result = proc_match(name, de, len);
Nicolas Dichtel710585d2014-12-10 15:45:01 -080076
77 if (result < 0)
78 node = node->rb_left;
79 else if (result > 0)
80 node = node->rb_right;
81 else
82 return de;
83 }
84 return NULL;
85}
86
87static bool pde_subdir_insert(struct proc_dir_entry *dir,
88 struct proc_dir_entry *de)
89{
Davidlohr Bueso410bd5e2017-09-08 16:15:15 -070090 struct rb_root_cached *root = &dir->subdir;
91 struct rb_node **new = &root->rb_root.rb_node, *parent = NULL;
92 bool leftmost = true;
Nicolas Dichtel710585d2014-12-10 15:45:01 -080093
94 /* Figure out where to put new node */
95 while (*new) {
Geliang Tang4e4a7fb2017-02-24 15:00:17 -080096 struct proc_dir_entry *this = rb_entry(*new,
97 struct proc_dir_entry,
98 subdir_node);
Alexey Dobriyan93ad5bc2018-02-06 15:37:31 -080099 int result = proc_match(de->name, this, de->namelen);
Nicolas Dichtel710585d2014-12-10 15:45:01 -0800100
101 parent = *new;
102 if (result < 0)
103 new = &(*new)->rb_left;
Davidlohr Bueso410bd5e2017-09-08 16:15:15 -0700104 else if (result > 0) {
Nicolas Dichtel710585d2014-12-10 15:45:01 -0800105 new = &(*new)->rb_right;
Davidlohr Bueso410bd5e2017-09-08 16:15:15 -0700106 leftmost = false;
107 } else
Nicolas Dichtel710585d2014-12-10 15:45:01 -0800108 return false;
109 }
110
111 /* Add new node and rebalance tree. */
112 rb_link_node(&de->subdir_node, parent, new);
Davidlohr Bueso410bd5e2017-09-08 16:15:15 -0700113 rb_insert_color_cached(&de->subdir_node, root, leftmost);
Nicolas Dichtel710585d2014-12-10 15:45:01 -0800114 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117static int proc_notify_change(struct dentry *dentry, struct iattr *iattr)
118{
David Howells2b0143b2015-03-17 22:25:59 +0000119 struct inode *inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 struct proc_dir_entry *de = PDE(inode);
121 int error;
122
Jan Kara31051c82016-05-26 16:55:18 +0200123 error = setattr_prepare(dentry, iattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 if (error)
Christoph Hellwig10257742010-06-04 11:30:02 +0200125 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Christoph Hellwig10257742010-06-04 11:30:02 +0200127 setattr_copy(inode, iattr);
128 mark_inode_dirty(inode);
Marco Stornelli46f69552012-12-15 11:48:48 +0100129
Rui Xiangcdf7e8d2014-01-23 15:55:41 -0800130 proc_set_user(de, inode->i_uid, inode->i_gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 de->mode = inode->i_mode;
Christoph Hellwig10257742010-06-04 11:30:02 +0200132 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
David Howellsa528d352017-01-31 16:46:22 +0000135static int proc_getattr(const struct path *path, struct kstat *stat,
136 u32 request_mask, unsigned int query_flags)
Miklos Szeredi2b579be2005-09-06 15:17:18 -0700137{
David Howellsa528d352017-01-31 16:46:22 +0000138 struct inode *inode = d_inode(path->dentry);
Alexander Kuleshov6bee55f2015-02-12 15:01:03 -0800139 struct proc_dir_entry *de = PDE(inode);
Miklos Szeredi2b579be2005-09-06 15:17:18 -0700140 if (de && de->nlink)
Miklos Szeredibfe86842011-10-28 14:13:29 +0200141 set_nlink(inode, de->nlink);
Miklos Szeredi2b579be2005-09-06 15:17:18 -0700142
143 generic_fillattr(inode, stat);
144 return 0;
145}
146
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800147static const struct inode_operations proc_file_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 .setattr = proc_notify_change,
149};
150
151/*
152 * This function parses a name such as "tty/driver/serial", and
153 * returns the struct proc_dir_entry for "/proc/tty/driver", and
154 * returns "serial" in residual.
155 */
Alexey Dobriyane17a5762010-03-05 13:43:59 -0800156static int __xlate_proc_name(const char *name, struct proc_dir_entry **ret,
157 const char **residual)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
159 const char *cp = name, *next;
160 struct proc_dir_entry *de;
Alexey Dobriyan312ec7e2011-03-23 16:42:52 -0700161 unsigned int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Alexey Dobriyan7cee4e02008-04-29 01:01:40 -0700163 de = *ret;
164 if (!de)
165 de = &proc_root;
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 while (1) {
168 next = strchr(cp, '/');
169 if (!next)
170 break;
171
172 len = next - cp;
Nicolas Dichtel710585d2014-12-10 15:45:01 -0800173 de = pde_subdir_find(de, cp, len);
Alexey Dobriyan12bac0d2010-03-05 13:44:00 -0800174 if (!de) {
175 WARN(1, "name '%s'\n", name);
Alexey Dobriyane17a5762010-03-05 13:43:59 -0800176 return -ENOENT;
Alexey Dobriyan12bac0d2010-03-05 13:44:00 -0800177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 cp += len + 1;
179 }
180 *residual = cp;
181 *ret = de;
Alexey Dobriyane17a5762010-03-05 13:43:59 -0800182 return 0;
183}
184
185static int xlate_proc_name(const char *name, struct proc_dir_entry **ret,
186 const char **residual)
187{
188 int rv;
189
Waiman Longecf1a3d2015-09-09 15:35:57 -0700190 read_lock(&proc_subdir_lock);
Alexey Dobriyane17a5762010-03-05 13:43:59 -0800191 rv = __xlate_proc_name(name, ret, residual);
Waiman Longecf1a3d2015-09-09 15:35:57 -0700192 read_unlock(&proc_subdir_lock);
Alexey Dobriyane17a5762010-03-05 13:43:59 -0800193 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
Alexey Dobriyan9a185402008-07-26 11:21:37 +0400196static DEFINE_IDA(proc_inum_ida);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Alexey Dobriyan67935df2008-07-26 11:18:28 +0400198#define PROC_DYNAMIC_FIRST 0xF0000000U
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200/*
201 * Return an inode number between PROC_DYNAMIC_FIRST and
202 * 0xffffffff, or zero on failure.
203 */
Eric W. Biederman33d6dce2011-06-17 13:33:20 -0700204int proc_alloc_inum(unsigned int *inum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
Heiner Kallweitcde1b692017-07-10 15:50:52 -0700206 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Heiner Kallweitcde1b692017-07-10 15:50:52 -0700208 i = ida_simple_get(&proc_inum_ida, 0, UINT_MAX - PROC_DYNAMIC_FIRST + 1,
209 GFP_KERNEL);
210 if (i < 0)
211 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Heiner Kallweitcde1b692017-07-10 15:50:52 -0700213 *inum = PROC_DYNAMIC_FIRST + (unsigned int)i;
Eric W. Biederman33d6dce2011-06-17 13:33:20 -0700214 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
216
Eric W. Biederman33d6dce2011-06-17 13:33:20 -0700217void proc_free_inum(unsigned int inum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
Heiner Kallweitcde1b692017-07-10 15:50:52 -0700219 ida_simple_remove(&proc_inum_ida, inum - PROC_DYNAMIC_FIRST);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 * Don't create negative dentries here, return -ENOENT by hand
224 * instead.
225 */
Alexey Dobriyan93ad5bc2018-02-06 15:37:31 -0800226struct dentry *proc_lookup_de(struct inode *dir, struct dentry *dentry,
227 struct proc_dir_entry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
Al Virod3d009c2013-01-25 20:11:22 -0500229 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Waiman Longecf1a3d2015-09-09 15:35:57 -0700231 read_lock(&proc_subdir_lock);
Nicolas Dichtel710585d2014-12-10 15:45:01 -0800232 de = pde_subdir_find(de, dentry->d_name.name, dentry->d_name.len);
233 if (de) {
234 pde_get(de);
Waiman Longecf1a3d2015-09-09 15:35:57 -0700235 read_unlock(&proc_subdir_lock);
Nicolas Dichtel710585d2014-12-10 15:45:01 -0800236 inode = proc_get_inode(dir->i_sb, de);
237 if (!inode)
238 return ERR_PTR(-ENOMEM);
239 d_set_d_op(dentry, &simple_dentry_operations);
240 d_add(dentry, inode);
241 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 }
Waiman Longecf1a3d2015-09-09 15:35:57 -0700243 read_unlock(&proc_subdir_lock);
Al Virod3d009c2013-01-25 20:11:22 -0500244 return ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245}
246
Pavel Emelyanove9720ac2008-03-07 11:08:40 -0800247struct dentry *proc_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -0400248 unsigned int flags)
Pavel Emelyanove9720ac2008-03-07 11:08:40 -0800249{
Alexey Dobriyan93ad5bc2018-02-06 15:37:31 -0800250 return proc_lookup_de(dir, dentry, PDE(dir));
Pavel Emelyanove9720ac2008-03-07 11:08:40 -0800251}
252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253/*
254 * This returns non-zero if at EOF, so that the /proc
255 * root directory can use this and check if it should
256 * continue with the <pid> entries..
257 *
258 * Note that the VFS-layer doesn't care about the return
259 * value of the readdir() call, as long as it's non-negative
260 * for success..
261 */
Alexey Dobriyan93ad5bc2018-02-06 15:37:31 -0800262int proc_readdir_de(struct file *file, struct dir_context *ctx,
263 struct proc_dir_entry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Al Virof0c3b502013-05-16 12:07:31 -0400267 if (!dir_emit_dots(file, ctx))
268 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Waiman Longecf1a3d2015-09-09 15:35:57 -0700270 read_lock(&proc_subdir_lock);
Nicolas Dichtel710585d2014-12-10 15:45:01 -0800271 de = pde_subdir_first(de);
Al Virof0c3b502013-05-16 12:07:31 -0400272 i = ctx->pos - 2;
273 for (;;) {
274 if (!de) {
Waiman Longecf1a3d2015-09-09 15:35:57 -0700275 read_unlock(&proc_subdir_lock);
Al Virof0c3b502013-05-16 12:07:31 -0400276 return 0;
277 }
278 if (!i)
279 break;
Nicolas Dichtel710585d2014-12-10 15:45:01 -0800280 de = pde_subdir_next(de);
Al Virof0c3b502013-05-16 12:07:31 -0400281 i--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 }
Al Virof0c3b502013-05-16 12:07:31 -0400283
284 do {
285 struct proc_dir_entry *next;
286 pde_get(de);
Waiman Longecf1a3d2015-09-09 15:35:57 -0700287 read_unlock(&proc_subdir_lock);
Al Virof0c3b502013-05-16 12:07:31 -0400288 if (!dir_emit(ctx, de->name, de->namelen,
289 de->low_ino, de->mode >> 12)) {
290 pde_put(de);
291 return 0;
292 }
Waiman Longecf1a3d2015-09-09 15:35:57 -0700293 read_lock(&proc_subdir_lock);
Al Virof0c3b502013-05-16 12:07:31 -0400294 ctx->pos++;
Nicolas Dichtel710585d2014-12-10 15:45:01 -0800295 next = pde_subdir_next(de);
Al Virof0c3b502013-05-16 12:07:31 -0400296 pde_put(de);
297 de = next;
298 } while (de);
Waiman Longecf1a3d2015-09-09 15:35:57 -0700299 read_unlock(&proc_subdir_lock);
Linus Torvaldsfd3930f2013-08-19 16:26:12 -0700300 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301}
302
Al Virof0c3b502013-05-16 12:07:31 -0400303int proc_readdir(struct file *file, struct dir_context *ctx)
Pavel Emelyanove9720ac2008-03-07 11:08:40 -0800304{
Al Virof0c3b502013-05-16 12:07:31 -0400305 struct inode *inode = file_inode(file);
Pavel Emelyanove9720ac2008-03-07 11:08:40 -0800306
Alexey Dobriyan93ad5bc2018-02-06 15:37:31 -0800307 return proc_readdir_de(file, ctx, PDE(inode));
Pavel Emelyanove9720ac2008-03-07 11:08:40 -0800308}
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310/*
311 * These are the generic /proc directory operations. They
312 * use the in-memory "struct proc_dir_entry" tree to parse
313 * the /proc directory.
314 */
Arjan van de Ven00977a52007-02-12 00:55:34 -0800315static const struct file_operations proc_dir_operations = {
Alexey Dobriyanb4df2b92008-10-27 22:48:36 +0300316 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -0400318 .iterate_shared = proc_readdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319};
320
321/*
322 * proc directories can do almost nothing..
323 */
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800324static const struct inode_operations proc_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 .lookup = proc_lookup,
Miklos Szeredi2b579be2005-09-06 15:17:18 -0700326 .getattr = proc_getattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 .setattr = proc_notify_change,
328};
329
330static int proc_register(struct proc_dir_entry * dir, struct proc_dir_entry * dp)
331{
Eric W. Biederman33d6dce2011-06-17 13:33:20 -0700332 int ret;
Nicolas Dichtel710585d2014-12-10 15:45:01 -0800333
Eric W. Biederman33d6dce2011-06-17 13:33:20 -0700334 ret = proc_alloc_inum(&dp->low_ino);
335 if (ret)
336 return ret;
Steven Rostedt64a07bd2006-03-26 01:36:55 -0800337
Waiman Longecf1a3d2015-09-09 15:35:57 -0700338 write_lock(&proc_subdir_lock);
Changli Gao99fc06d2007-07-15 23:40:09 -0700339 dp->parent = dir;
Debabrata Banerjeeb208d542014-12-10 15:45:04 -0800340 if (pde_subdir_insert(dir, dp) == false) {
Nicolas Dichtel710585d2014-12-10 15:45:01 -0800341 WARN(1, "proc_dir_entry '%s/%s' already registered\n",
342 dir->name, dp->name);
Waiman Longecf1a3d2015-09-09 15:35:57 -0700343 write_unlock(&proc_subdir_lock);
Debabrata Banerjeeb208d542014-12-10 15:45:04 -0800344 proc_free_inum(dp->low_ino);
345 return -EEXIST;
346 }
Waiman Longecf1a3d2015-09-09 15:35:57 -0700347 write_unlock(&proc_subdir_lock);
Changli Gao99fc06d2007-07-15 23:40:09 -0700348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 return 0;
350}
351
Alexey Dobriyan2d3a4e32008-02-08 04:18:37 -0800352static struct proc_dir_entry *__proc_create(struct proc_dir_entry **parent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 const char *name,
Al Virod161a132011-07-24 03:36:29 -0400354 umode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 nlink_t nlink)
356{
357 struct proc_dir_entry *ent = NULL;
Alexey Dobriyandbcdb502014-08-08 14:21:25 -0700358 const char *fn;
359 struct qstr qstr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Alexey Dobriyan7cee4e02008-04-29 01:01:40 -0700361 if (xlate_proc_name(name, parent, &fn) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 goto out;
Alexey Dobriyandbcdb502014-08-08 14:21:25 -0700363 qstr.name = fn;
364 qstr.len = strlen(fn);
365 if (qstr.len == 0 || qstr.len >= 256) {
366 WARN(1, "name len %u\n", qstr.len);
367 return NULL;
368 }
369 if (*parent == &proc_root && name_to_int(&qstr) != ~0U) {
370 WARN(1, "create '/proc/%s' by hand\n", qstr.name);
371 return NULL;
372 }
Eric W. Biedermaneb6d38d2015-05-11 16:44:25 -0500373 if (is_empty_pde(*parent)) {
374 WARN(1, "attempt to add to permanently empty directory");
375 return NULL;
376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Alexey Dobriyanb4884f22018-04-10 16:31:52 -0700378 ent = kmem_cache_zalloc(proc_dir_entry_cache, GFP_KERNEL);
yan17baa2a2012-10-04 17:15:43 -0700379 if (!ent)
380 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Alexey Dobriyanb4884f22018-04-10 16:31:52 -0700382 if (qstr.len + 1 <= sizeof(ent->inline_name)) {
383 ent->name = ent->inline_name;
384 } else {
385 ent->name = kmalloc(qstr.len + 1, GFP_KERNEL);
386 if (!ent->name) {
387 pde_free(ent);
388 return NULL;
389 }
390 }
391
Alexey Dobriyandbcdb502014-08-08 14:21:25 -0700392 memcpy(ent->name, fn, qstr.len + 1);
393 ent->namelen = qstr.len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 ent->mode = mode;
395 ent->nlink = nlink;
Davidlohr Bueso410bd5e2017-09-08 16:15:15 -0700396 ent->subdir = RB_ROOT_CACHED;
Alexey Dobriyan5a622f22007-12-04 23:45:28 -0800397 atomic_set(&ent->count, 1);
Alexey Dobriyan786d7e12007-07-15 23:39:00 -0700398 spin_lock_init(&ent->pde_unload_lock);
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700399 INIT_LIST_HEAD(&ent->pde_openers);
Dmitry Torokhovc1104862016-08-10 14:36:01 -0700400 proc_set_user(ent, (*parent)->uid, (*parent)->gid);
401
yan17baa2a2012-10-04 17:15:43 -0700402out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 return ent;
404}
405
406struct proc_dir_entry *proc_symlink(const char *name,
407 struct proc_dir_entry *parent, const char *dest)
408{
409 struct proc_dir_entry *ent;
410
Alexey Dobriyan2d3a4e32008-02-08 04:18:37 -0800411 ent = __proc_create(&parent, name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 (S_IFLNK | S_IRUGO | S_IWUGO | S_IXUGO),1);
413
414 if (ent) {
415 ent->data = kmalloc((ent->size=strlen(dest))+1, GFP_KERNEL);
416 if (ent->data) {
417 strcpy((char*)ent->data,dest);
Al Virod443b9f2014-12-25 16:47:49 -0500418 ent->proc_iops = &proc_link_inode_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 if (proc_register(parent, ent) < 0) {
Alexey Dobriyanb4884f22018-04-10 16:31:52 -0700420 pde_free(ent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 ent = NULL;
422 }
423 } else {
Alexey Dobriyanb4884f22018-04-10 16:31:52 -0700424 pde_free(ent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 ent = NULL;
426 }
427 }
428 return ent;
429}
Helight.Xu587d4a12009-12-30 13:24:41 +0800430EXPORT_SYMBOL(proc_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
David Howells270b5ac2013-04-12 02:48:30 +0100432struct proc_dir_entry *proc_mkdir_data(const char *name, umode_t mode,
433 struct proc_dir_entry *parent, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
435 struct proc_dir_entry *ent;
436
David Howells270b5ac2013-04-12 02:48:30 +0100437 if (mode == 0)
438 mode = S_IRUGO | S_IXUGO;
439
Alexey Dobriyan2d3a4e32008-02-08 04:18:37 -0800440 ent = __proc_create(&parent, name, S_IFDIR | mode, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 if (ent) {
David Howells270b5ac2013-04-12 02:48:30 +0100442 ent->data = data;
Al Virod443b9f2014-12-25 16:47:49 -0500443 ent->proc_fops = &proc_dir_operations;
444 ent->proc_iops = &proc_dir_inode_operations;
445 parent->nlink++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 if (proc_register(parent, ent) < 0) {
Alexey Dobriyanb4884f22018-04-10 16:31:52 -0700447 pde_free(ent);
Al Virod443b9f2014-12-25 16:47:49 -0500448 parent->nlink--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 ent = NULL;
450 }
451 }
452 return ent;
453}
David Howells270b5ac2013-04-12 02:48:30 +0100454EXPORT_SYMBOL_GPL(proc_mkdir_data);
455
456struct proc_dir_entry *proc_mkdir_mode(const char *name, umode_t mode,
457 struct proc_dir_entry *parent)
458{
459 return proc_mkdir_data(name, mode, parent, NULL);
460}
Alexey Dobriyan011159a2011-05-14 00:12:48 +0300461EXPORT_SYMBOL(proc_mkdir_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463struct proc_dir_entry *proc_mkdir(const char *name,
464 struct proc_dir_entry *parent)
465{
David Howells270b5ac2013-04-12 02:48:30 +0100466 return proc_mkdir_data(name, 0, parent, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467}
Helight.Xu587d4a12009-12-30 13:24:41 +0800468EXPORT_SYMBOL(proc_mkdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Eric W. Biedermaneb6d38d2015-05-11 16:44:25 -0500470struct proc_dir_entry *proc_create_mount_point(const char *name)
471{
472 umode_t mode = S_IFDIR | S_IRUGO | S_IXUGO;
473 struct proc_dir_entry *ent, *parent = NULL;
474
475 ent = __proc_create(&parent, name, mode, 2);
476 if (ent) {
477 ent->data = NULL;
478 ent->proc_fops = NULL;
479 ent->proc_iops = NULL;
Takashi Iwaid66bb162017-04-28 15:00:15 +0200480 parent->nlink++;
Eric W. Biedermaneb6d38d2015-05-11 16:44:25 -0500481 if (proc_register(parent, ent) < 0) {
Alexey Dobriyanb4884f22018-04-10 16:31:52 -0700482 pde_free(ent);
Eric W. Biedermaneb6d38d2015-05-11 16:44:25 -0500483 parent->nlink--;
484 ent = NULL;
485 }
486 }
487 return ent;
488}
Seth Forsheef97df702016-11-14 11:12:56 +0000489EXPORT_SYMBOL(proc_create_mount_point);
Eric W. Biedermaneb6d38d2015-05-11 16:44:25 -0500490
Al Virod161a132011-07-24 03:36:29 -0400491struct proc_dir_entry *proc_create_data(const char *name, umode_t mode,
Denis V. Lunev59b74352008-04-29 01:02:00 -0700492 struct proc_dir_entry *parent,
493 const struct file_operations *proc_fops,
494 void *data)
Alexey Dobriyan2d3a4e32008-02-08 04:18:37 -0800495{
496 struct proc_dir_entry *pde;
Al Virob6cdc732013-03-30 21:20:14 -0400497 if ((mode & S_IFMT) == 0)
498 mode |= S_IFREG;
Alexey Dobriyan2d3a4e32008-02-08 04:18:37 -0800499
Al Virob6cdc732013-03-30 21:20:14 -0400500 if (!S_ISREG(mode)) {
501 WARN_ON(1); /* use proc_mkdir() */
502 return NULL;
Alexey Dobriyan2d3a4e32008-02-08 04:18:37 -0800503 }
504
Al Virod443b9f2014-12-25 16:47:49 -0500505 BUG_ON(proc_fops == NULL);
506
Al Virob6cdc732013-03-30 21:20:14 -0400507 if ((mode & S_IALLUGO) == 0)
508 mode |= S_IRUGO;
509 pde = __proc_create(&parent, name, mode, 1);
Alexey Dobriyan2d3a4e32008-02-08 04:18:37 -0800510 if (!pde)
511 goto out;
512 pde->proc_fops = proc_fops;
Denis V. Lunev59b74352008-04-29 01:02:00 -0700513 pde->data = data;
Al Virod443b9f2014-12-25 16:47:49 -0500514 pde->proc_iops = &proc_file_inode_operations;
Alexey Dobriyan2d3a4e32008-02-08 04:18:37 -0800515 if (proc_register(parent, pde) < 0)
516 goto out_free;
517 return pde;
518out_free:
Alexey Dobriyanb4884f22018-04-10 16:31:52 -0700519 pde_free(pde);
Alexey Dobriyan2d3a4e32008-02-08 04:18:37 -0800520out:
521 return NULL;
522}
Helight.Xu587d4a12009-12-30 13:24:41 +0800523EXPORT_SYMBOL(proc_create_data);
David Howells271a15e2013-04-12 00:38:51 +0100524
Alexey Dobriyan855d9762017-09-08 16:13:38 -0700525struct proc_dir_entry *proc_create(const char *name, umode_t mode,
526 struct proc_dir_entry *parent,
527 const struct file_operations *proc_fops)
528{
529 return proc_create_data(name, mode, parent, proc_fops, NULL);
530}
531EXPORT_SYMBOL(proc_create);
532
David Howells271a15e2013-04-12 00:38:51 +0100533void proc_set_size(struct proc_dir_entry *de, loff_t size)
534{
535 de->size = size;
536}
537EXPORT_SYMBOL(proc_set_size);
538
539void proc_set_user(struct proc_dir_entry *de, kuid_t uid, kgid_t gid)
540{
541 de->uid = uid;
542 de->gid = gid;
543}
544EXPORT_SYMBOL(proc_set_user);
Alexey Dobriyan2d3a4e32008-02-08 04:18:37 -0800545
Alexey Dobriyan135d5652009-12-15 16:45:39 -0800546void pde_put(struct proc_dir_entry *pde)
547{
Alexey Dobriyanb4884f22018-04-10 16:31:52 -0700548 if (atomic_dec_and_test(&pde->count)) {
549 proc_free_inum(pde->low_ino);
550 pde_free(pde);
551 }
Alexey Dobriyan135d5652009-12-15 16:45:39 -0800552}
553
Al Viro8ce584c2013-03-30 20:13:46 -0400554/*
555 * Remove a /proc entry and free it if it's not currently in use.
556 */
557void remove_proc_entry(const char *name, struct proc_dir_entry *parent)
558{
Al Viro8ce584c2013-03-30 20:13:46 -0400559 struct proc_dir_entry *de = NULL;
560 const char *fn = name;
561 unsigned int len;
562
Waiman Longecf1a3d2015-09-09 15:35:57 -0700563 write_lock(&proc_subdir_lock);
Al Viro8ce584c2013-03-30 20:13:46 -0400564 if (__xlate_proc_name(name, &parent, &fn) != 0) {
Waiman Longecf1a3d2015-09-09 15:35:57 -0700565 write_unlock(&proc_subdir_lock);
Al Viro8ce584c2013-03-30 20:13:46 -0400566 return;
567 }
568 len = strlen(fn);
569
Nicolas Dichtel710585d2014-12-10 15:45:01 -0800570 de = pde_subdir_find(parent, fn, len);
571 if (de)
Davidlohr Bueso410bd5e2017-09-08 16:15:15 -0700572 rb_erase_cached(&de->subdir_node, &parent->subdir);
Waiman Longecf1a3d2015-09-09 15:35:57 -0700573 write_unlock(&proc_subdir_lock);
Al Viro8ce584c2013-03-30 20:13:46 -0400574 if (!de) {
575 WARN(1, "name '%s'\n", name);
576 return;
577 }
578
Al Viro866ad9a72013-04-03 19:07:30 -0400579 proc_entry_rundown(de);
Alexey Dobriyan881adb82008-07-25 01:48:29 -0700580
Alexey Dobriyanf649d6d2008-04-29 01:01:39 -0700581 if (S_ISDIR(de->mode))
582 parent->nlink--;
583 de->nlink = 0;
Nicolas Dichtel710585d2014-12-10 15:45:01 -0800584 WARN(pde_subdir_first(de),
585 "%s: removing non-empty directory '%s/%s', leaking at least '%s'\n",
586 __func__, de->parent->name, de->name, pde_subdir_first(de)->name);
Alexey Dobriyan135d5652009-12-15 16:45:39 -0800587 pde_put(de);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588}
Helight.Xu587d4a12009-12-30 13:24:41 +0800589EXPORT_SYMBOL(remove_proc_entry);
Al Viro8ce584c2013-03-30 20:13:46 -0400590
591int remove_proc_subtree(const char *name, struct proc_dir_entry *parent)
592{
Al Viro8ce584c2013-03-30 20:13:46 -0400593 struct proc_dir_entry *root = NULL, *de, *next;
594 const char *fn = name;
595 unsigned int len;
596
Waiman Longecf1a3d2015-09-09 15:35:57 -0700597 write_lock(&proc_subdir_lock);
Al Viro8ce584c2013-03-30 20:13:46 -0400598 if (__xlate_proc_name(name, &parent, &fn) != 0) {
Waiman Longecf1a3d2015-09-09 15:35:57 -0700599 write_unlock(&proc_subdir_lock);
Al Viro8ce584c2013-03-30 20:13:46 -0400600 return -ENOENT;
601 }
602 len = strlen(fn);
603
Nicolas Dichtel710585d2014-12-10 15:45:01 -0800604 root = pde_subdir_find(parent, fn, len);
Al Viro8ce584c2013-03-30 20:13:46 -0400605 if (!root) {
Waiman Longecf1a3d2015-09-09 15:35:57 -0700606 write_unlock(&proc_subdir_lock);
Al Viro8ce584c2013-03-30 20:13:46 -0400607 return -ENOENT;
608 }
Davidlohr Bueso410bd5e2017-09-08 16:15:15 -0700609 rb_erase_cached(&root->subdir_node, &parent->subdir);
Nicolas Dichtel710585d2014-12-10 15:45:01 -0800610
Al Viro8ce584c2013-03-30 20:13:46 -0400611 de = root;
612 while (1) {
Nicolas Dichtel710585d2014-12-10 15:45:01 -0800613 next = pde_subdir_first(de);
Al Viro8ce584c2013-03-30 20:13:46 -0400614 if (next) {
Davidlohr Bueso410bd5e2017-09-08 16:15:15 -0700615 rb_erase_cached(&next->subdir_node, &de->subdir);
Al Viro8ce584c2013-03-30 20:13:46 -0400616 de = next;
617 continue;
618 }
Waiman Longecf1a3d2015-09-09 15:35:57 -0700619 write_unlock(&proc_subdir_lock);
Al Viro8ce584c2013-03-30 20:13:46 -0400620
Al Viro866ad9a72013-04-03 19:07:30 -0400621 proc_entry_rundown(de);
Al Viro8ce584c2013-03-30 20:13:46 -0400622 next = de->parent;
623 if (S_ISDIR(de->mode))
624 next->nlink--;
625 de->nlink = 0;
626 if (de == root)
627 break;
628 pde_put(de);
629
Waiman Longecf1a3d2015-09-09 15:35:57 -0700630 write_lock(&proc_subdir_lock);
Al Viro8ce584c2013-03-30 20:13:46 -0400631 de = next;
632 }
633 pde_put(root);
634 return 0;
635}
636EXPORT_SYMBOL(remove_proc_subtree);
David Howells4a520d22013-04-12 14:06:01 +0100637
638void *proc_get_parent_data(const struct inode *inode)
639{
640 struct proc_dir_entry *de = PDE(inode);
641 return de->parent->data;
642}
643EXPORT_SYMBOL_GPL(proc_get_parent_data);
David Howellsa8ca16e2013-04-12 17:27:28 +0100644
645void proc_remove(struct proc_dir_entry *de)
646{
647 if (de)
648 remove_proc_subtree(de->name, de->parent);
649}
650EXPORT_SYMBOL(proc_remove);
David Howellsc30480b2013-04-12 18:03:36 +0100651
652void *PDE_DATA(const struct inode *inode)
653{
654 return __PDE_DATA(inode);
655}
656EXPORT_SYMBOL(PDE_DATA);