]> git.ipfire.org Git - thirdparty/linux.git/blame - fs/proc/generic.c
Merge tag 'pm-5.8-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm
[thirdparty/linux.git] / fs / proc / generic.c
CommitLineData
457c8996 1// SPDX-License-Identifier: GPL-2.0-only
1da177e4
LT
2/*
3 * proc/fs/generic.c --- generic routines for the proc-fs
4 *
5 * This file contains generic proc-fs routines for handling
6 * directories and files.
7 *
8 * Copyright (C) 1991, 1992 Linus Torvalds.
9 * Copyright (C) 1997 Theodore Ts'o
10 */
11
b4884f23 12#include <linux/cache.h>
1da177e4
LT
13#include <linux/errno.h>
14#include <linux/time.h>
15#include <linux/proc_fs.h>
16#include <linux/stat.h>
1025774c 17#include <linux/mm.h>
1da177e4 18#include <linux/module.h>
1da4d377 19#include <linux/namei.h>
5a0e3ad6 20#include <linux/slab.h>
87ebdc00 21#include <linux/printk.h>
1da177e4 22#include <linux/mount.h>
1da177e4
LT
23#include <linux/init.h>
24#include <linux/idr.h>
1da177e4 25#include <linux/bitops.h>
64a07bd8 26#include <linux/spinlock.h>
786d7e16 27#include <linux/completion.h>
7c0f6ba6 28#include <linux/uaccess.h>
fddda2b7 29#include <linux/seq_file.h>
1da177e4 30
fee781e6
AB
31#include "internal.h"
32
ecf1a3df 33static DEFINE_RWLOCK(proc_subdir_lock);
64a07bd8 34
b4884f23
AD
35struct kmem_cache *proc_dir_entry_cache __ro_after_init;
36
37void pde_free(struct proc_dir_entry *pde)
38{
39 if (S_ISLNK(pde->mode))
40 kfree(pde->data);
41 if (pde->name != pde->inline_name)
42 kfree(pde->name);
43 kmem_cache_free(proc_dir_entry_cache, pde);
44}
45
93ad5bc6 46static int proc_match(const char *name, struct proc_dir_entry *de, unsigned int len)
1da177e4 47{
710585d4
ND
48 if (len < de->namelen)
49 return -1;
50 if (len > de->namelen)
51 return 1;
52
53 return memcmp(name, de->name, len);
54}
55
56static struct proc_dir_entry *pde_subdir_first(struct proc_dir_entry *dir)
57{
4f113437
AD
58 return rb_entry_safe(rb_first(&dir->subdir), struct proc_dir_entry,
59 subdir_node);
710585d4
ND
60}
61
62static struct proc_dir_entry *pde_subdir_next(struct proc_dir_entry *dir)
63{
2fc1e948
ND
64 return rb_entry_safe(rb_next(&dir->subdir_node), struct proc_dir_entry,
65 subdir_node);
710585d4
ND
66}
67
68static struct proc_dir_entry *pde_subdir_find(struct proc_dir_entry *dir,
69 const char *name,
70 unsigned int len)
71{
4f113437 72 struct rb_node *node = dir->subdir.rb_node;
710585d4
ND
73
74 while (node) {
4e4a7fb7
GT
75 struct proc_dir_entry *de = rb_entry(node,
76 struct proc_dir_entry,
77 subdir_node);
93ad5bc6 78 int result = proc_match(name, de, len);
710585d4
ND
79
80 if (result < 0)
81 node = node->rb_left;
82 else if (result > 0)
83 node = node->rb_right;
84 else
85 return de;
86 }
87 return NULL;
88}
89
90static bool pde_subdir_insert(struct proc_dir_entry *dir,
91 struct proc_dir_entry *de)
92{
4f113437
AD
93 struct rb_root *root = &dir->subdir;
94 struct rb_node **new = &root->rb_node, *parent = NULL;
710585d4
ND
95
96 /* Figure out where to put new node */
97 while (*new) {
4e4a7fb7
GT
98 struct proc_dir_entry *this = rb_entry(*new,
99 struct proc_dir_entry,
100 subdir_node);
93ad5bc6 101 int result = proc_match(de->name, this, de->namelen);
710585d4
ND
102
103 parent = *new;
104 if (result < 0)
105 new = &(*new)->rb_left;
4f113437 106 else if (result > 0)
710585d4 107 new = &(*new)->rb_right;
4f113437 108 else
710585d4
ND
109 return false;
110 }
111
112 /* Add new node and rebalance tree. */
113 rb_link_node(&de->subdir_node, parent, new);
4f113437 114 rb_insert_color(&de->subdir_node, root);
710585d4 115 return true;
1da177e4
LT
116}
117
1da177e4
LT
118static int proc_notify_change(struct dentry *dentry, struct iattr *iattr)
119{
2b0143b5 120 struct inode *inode = d_inode(dentry);
1da177e4
LT
121 struct proc_dir_entry *de = PDE(inode);
122 int error;
123
31051c85 124 error = setattr_prepare(dentry, iattr);
1da177e4 125 if (error)
1025774c 126 return error;
1da177e4 127
1025774c
CH
128 setattr_copy(inode, iattr);
129 mark_inode_dirty(inode);
46f69557 130
cdf7e8dd 131 proc_set_user(de, inode->i_uid, inode->i_gid);
1da177e4 132 de->mode = inode->i_mode;
1025774c 133 return 0;
1da177e4
LT
134}
135
a528d35e
DH
136static int proc_getattr(const struct path *path, struct kstat *stat,
137 u32 request_mask, unsigned int query_flags)
2b579bee 138{
a528d35e 139 struct inode *inode = d_inode(path->dentry);
6bee55f9 140 struct proc_dir_entry *de = PDE(inode);
e06689bf
AD
141 if (de) {
142 nlink_t nlink = READ_ONCE(de->nlink);
143 if (nlink > 0) {
144 set_nlink(inode, nlink);
145 }
146 }
2b579bee
MS
147
148 generic_fillattr(inode, stat);
149 return 0;
150}
151
c5ef1c42 152static const struct inode_operations proc_file_inode_operations = {
1da177e4
LT
153 .setattr = proc_notify_change,
154};
155
156/*
157 * This function parses a name such as "tty/driver/serial", and
158 * returns the struct proc_dir_entry for "/proc/tty/driver", and
159 * returns "serial" in residual.
160 */
e17a5765
AD
161static int __xlate_proc_name(const char *name, struct proc_dir_entry **ret,
162 const char **residual)
1da177e4
LT
163{
164 const char *cp = name, *next;
165 struct proc_dir_entry *de;
1da177e4 166
7cee4e00
AD
167 de = *ret;
168 if (!de)
169 de = &proc_root;
170
1da177e4
LT
171 while (1) {
172 next = strchr(cp, '/');
173 if (!next)
174 break;
175
5f6354ea 176 de = pde_subdir_find(de, cp, next - cp);
12bac0d9
AD
177 if (!de) {
178 WARN(1, "name '%s'\n", name);
e17a5765 179 return -ENOENT;
12bac0d9 180 }
5f6354ea 181 cp = next + 1;
1da177e4
LT
182 }
183 *residual = cp;
184 *ret = de;
e17a5765
AD
185 return 0;
186}
187
188static int xlate_proc_name(const char *name, struct proc_dir_entry **ret,
189 const char **residual)
190{
191 int rv;
192
ecf1a3df 193 read_lock(&proc_subdir_lock);
e17a5765 194 rv = __xlate_proc_name(name, ret, residual);
ecf1a3df 195 read_unlock(&proc_subdir_lock);
e17a5765 196 return rv;
1da177e4
LT
197}
198
9a185409 199static DEFINE_IDA(proc_inum_ida);
1da177e4 200
67935df4 201#define PROC_DYNAMIC_FIRST 0xF0000000U
1da177e4
LT
202
203/*
204 * Return an inode number between PROC_DYNAMIC_FIRST and
205 * 0xffffffff, or zero on failure.
206 */
33d6dce6 207int proc_alloc_inum(unsigned int *inum)
1da177e4 208{
cde1b693 209 int i;
1da177e4 210
cde1b693
HK
211 i = ida_simple_get(&proc_inum_ida, 0, UINT_MAX - PROC_DYNAMIC_FIRST + 1,
212 GFP_KERNEL);
213 if (i < 0)
214 return i;
1da177e4 215
cde1b693 216 *inum = PROC_DYNAMIC_FIRST + (unsigned int)i;
33d6dce6 217 return 0;
1da177e4
LT
218}
219
33d6dce6 220void proc_free_inum(unsigned int inum)
1da177e4 221{
cde1b693 222 ida_simple_remove(&proc_inum_ida, inum - PROC_DYNAMIC_FIRST);
1da177e4
LT
223}
224
1da4d377
AD
225static int proc_misc_d_revalidate(struct dentry *dentry, unsigned int flags)
226{
227 if (flags & LOOKUP_RCU)
228 return -ECHILD;
229
230 if (atomic_read(&PDE(d_inode(dentry))->in_use) < 0)
231 return 0; /* revalidate */
232 return 1;
233}
234
235static int proc_misc_d_delete(const struct dentry *dentry)
236{
237 return atomic_read(&PDE(d_inode(dentry))->in_use) < 0;
238}
239
240static const struct dentry_operations proc_misc_dentry_ops = {
241 .d_revalidate = proc_misc_d_revalidate,
242 .d_delete = proc_misc_d_delete,
243};
244
1da177e4
LT
245/*
246 * Don't create negative dentries here, return -ENOENT by hand
247 * instead.
248 */
93ad5bc6
AD
249struct dentry *proc_lookup_de(struct inode *dir, struct dentry *dentry,
250 struct proc_dir_entry *de)
1da177e4 251{
d3d009cb 252 struct inode *inode;
1da177e4 253
ecf1a3df 254 read_lock(&proc_subdir_lock);
710585d4
ND
255 de = pde_subdir_find(de, dentry->d_name.name, dentry->d_name.len);
256 if (de) {
257 pde_get(de);
ecf1a3df 258 read_unlock(&proc_subdir_lock);
710585d4
ND
259 inode = proc_get_inode(dir->i_sb, de);
260 if (!inode)
261 return ERR_PTR(-ENOMEM);
1fde6f21 262 d_set_d_op(dentry, de->proc_dops);
888e2b03 263 return d_splice_alias(inode, dentry);
1da177e4 264 }
ecf1a3df 265 read_unlock(&proc_subdir_lock);
d3d009cb 266 return ERR_PTR(-ENOENT);
1da177e4
LT
267}
268
e9720acd 269struct dentry *proc_lookup(struct inode *dir, struct dentry *dentry,
00cd8dd3 270 unsigned int flags)
e9720acd 271{
93ad5bc6 272 return proc_lookup_de(dir, dentry, PDE(dir));
e9720acd
PE
273}
274
1da177e4
LT
275/*
276 * This returns non-zero if at EOF, so that the /proc
277 * root directory can use this and check if it should
278 * continue with the <pid> entries..
279 *
280 * Note that the VFS-layer doesn't care about the return
281 * value of the readdir() call, as long as it's non-negative
282 * for success..
283 */
93ad5bc6
AD
284int proc_readdir_de(struct file *file, struct dir_context *ctx,
285 struct proc_dir_entry *de)
1da177e4 286{
1da177e4 287 int i;
f0c3b509
AV
288
289 if (!dir_emit_dots(file, ctx))
290 return 0;
291
8d48b2e0 292 i = ctx->pos - 2;
ecf1a3df 293 read_lock(&proc_subdir_lock);
710585d4 294 de = pde_subdir_first(de);
f0c3b509
AV
295 for (;;) {
296 if (!de) {
ecf1a3df 297 read_unlock(&proc_subdir_lock);
f0c3b509
AV
298 return 0;
299 }
300 if (!i)
301 break;
710585d4 302 de = pde_subdir_next(de);
f0c3b509 303 i--;
1da177e4 304 }
f0c3b509
AV
305
306 do {
307 struct proc_dir_entry *next;
308 pde_get(de);
ecf1a3df 309 read_unlock(&proc_subdir_lock);
f0c3b509
AV
310 if (!dir_emit(ctx, de->name, de->namelen,
311 de->low_ino, de->mode >> 12)) {
312 pde_put(de);
313 return 0;
314 }
f0c3b509 315 ctx->pos++;
8d48b2e0 316 read_lock(&proc_subdir_lock);
710585d4 317 next = pde_subdir_next(de);
f0c3b509
AV
318 pde_put(de);
319 de = next;
320 } while (de);
ecf1a3df 321 read_unlock(&proc_subdir_lock);
fd3930f7 322 return 1;
1da177e4
LT
323}
324
f0c3b509 325int proc_readdir(struct file *file, struct dir_context *ctx)
e9720acd 326{
f0c3b509 327 struct inode *inode = file_inode(file);
e9720acd 328
93ad5bc6 329 return proc_readdir_de(file, ctx, PDE(inode));
e9720acd
PE
330}
331
1da177e4
LT
332/*
333 * These are the generic /proc directory operations. They
334 * use the in-memory "struct proc_dir_entry" tree to parse
335 * the /proc directory.
336 */
00977a59 337static const struct file_operations proc_dir_operations = {
b4df2b92 338 .llseek = generic_file_llseek,
1da177e4 339 .read = generic_read_dir,
f50752ea 340 .iterate_shared = proc_readdir,
1da177e4
LT
341};
342
343/*
344 * proc directories can do almost nothing..
345 */
c5ef1c42 346static const struct inode_operations proc_dir_inode_operations = {
1da177e4 347 .lookup = proc_lookup,
2b579bee 348 .getattr = proc_getattr,
1da177e4
LT
349 .setattr = proc_notify_change,
350};
351
61172eae
CH
352/* returns the registered entry, or frees dp and returns NULL on failure */
353struct proc_dir_entry *proc_register(struct proc_dir_entry *dir,
354 struct proc_dir_entry *dp)
1da177e4 355{
61172eae
CH
356 if (proc_alloc_inum(&dp->low_ino))
357 goto out_free_entry;
64a07bd8 358
ecf1a3df 359 write_lock(&proc_subdir_lock);
99fc06df 360 dp->parent = dir;
b208d54b 361 if (pde_subdir_insert(dir, dp) == false) {
710585d4
ND
362 WARN(1, "proc_dir_entry '%s/%s' already registered\n",
363 dir->name, dp->name);
ecf1a3df 364 write_unlock(&proc_subdir_lock);
61172eae 365 goto out_free_inum;
b208d54b 366 }
e06689bf 367 dir->nlink++;
ecf1a3df 368 write_unlock(&proc_subdir_lock);
99fc06df 369
61172eae
CH
370 return dp;
371out_free_inum:
372 proc_free_inum(dp->low_ino);
373out_free_entry:
374 pde_free(dp);
375 return NULL;
1da177e4
LT
376}
377
2d3a4e36 378static struct proc_dir_entry *__proc_create(struct proc_dir_entry **parent,
1da177e4 379 const char *name,
d161a13f 380 umode_t mode,
1da177e4
LT
381 nlink_t nlink)
382{
383 struct proc_dir_entry *ent = NULL;
dbcdb504
AD
384 const char *fn;
385 struct qstr qstr;
1da177e4 386
7cee4e00 387 if (xlate_proc_name(name, parent, &fn) != 0)
1da177e4 388 goto out;
dbcdb504
AD
389 qstr.name = fn;
390 qstr.len = strlen(fn);
391 if (qstr.len == 0 || qstr.len >= 256) {
392 WARN(1, "name len %u\n", qstr.len);
393 return NULL;
394 }
b77d70db
AD
395 if (qstr.len == 1 && fn[0] == '.') {
396 WARN(1, "name '.'\n");
397 return NULL;
398 }
399 if (qstr.len == 2 && fn[0] == '.' && fn[1] == '.') {
400 WARN(1, "name '..'\n");
401 return NULL;
402 }
dbcdb504
AD
403 if (*parent == &proc_root && name_to_int(&qstr) != ~0U) {
404 WARN(1, "create '/proc/%s' by hand\n", qstr.name);
405 return NULL;
406 }
eb6d38d5
EB
407 if (is_empty_pde(*parent)) {
408 WARN(1, "attempt to add to permanently empty directory");
409 return NULL;
410 }
1da177e4 411
b4884f23 412 ent = kmem_cache_zalloc(proc_dir_entry_cache, GFP_KERNEL);
17baa2a2 413 if (!ent)
414 goto out;
1da177e4 415
24074a35 416 if (qstr.len + 1 <= SIZEOF_PDE_INLINE_NAME) {
b4884f23
AD
417 ent->name = ent->inline_name;
418 } else {
419 ent->name = kmalloc(qstr.len + 1, GFP_KERNEL);
420 if (!ent->name) {
421 pde_free(ent);
422 return NULL;
423 }
424 }
425
dbcdb504
AD
426 memcpy(ent->name, fn, qstr.len + 1);
427 ent->namelen = qstr.len;
1da177e4
LT
428 ent->mode = mode;
429 ent->nlink = nlink;
4f113437 430 ent->subdir = RB_ROOT;
9cdd83e3 431 refcount_set(&ent->refcnt, 1);
786d7e16 432 spin_lock_init(&ent->pde_unload_lock);
881adb85 433 INIT_LIST_HEAD(&ent->pde_openers);
c110486f
DT
434 proc_set_user(ent, (*parent)->uid, (*parent)->gid);
435
1fde6f21
AD
436 ent->proc_dops = &proc_misc_dentry_ops;
437
17baa2a2 438out:
1da177e4
LT
439 return ent;
440}
441
442struct proc_dir_entry *proc_symlink(const char *name,
443 struct proc_dir_entry *parent, const char *dest)
444{
445 struct proc_dir_entry *ent;
446
2d3a4e36 447 ent = __proc_create(&parent, name,
1da177e4
LT
448 (S_IFLNK | S_IRUGO | S_IWUGO | S_IXUGO),1);
449
450 if (ent) {
451 ent->data = kmalloc((ent->size=strlen(dest))+1, GFP_KERNEL);
452 if (ent->data) {
453 strcpy((char*)ent->data,dest);
d443b9fd 454 ent->proc_iops = &proc_link_inode_operations;
61172eae 455 ent = proc_register(parent, ent);
1da177e4 456 } else {
b4884f23 457 pde_free(ent);
1da177e4
LT
458 ent = NULL;
459 }
460 }
461 return ent;
462}
587d4a17 463EXPORT_SYMBOL(proc_symlink);
1da177e4 464
270b5ac2
DH
465struct proc_dir_entry *proc_mkdir_data(const char *name, umode_t mode,
466 struct proc_dir_entry *parent, void *data)
1da177e4
LT
467{
468 struct proc_dir_entry *ent;
469
270b5ac2
DH
470 if (mode == 0)
471 mode = S_IRUGO | S_IXUGO;
472
2d3a4e36 473 ent = __proc_create(&parent, name, S_IFDIR | mode, 2);
1da177e4 474 if (ent) {
270b5ac2 475 ent->data = data;
d56c0d45 476 ent->proc_dir_ops = &proc_dir_operations;
d443b9fd 477 ent->proc_iops = &proc_dir_inode_operations;
61172eae 478 ent = proc_register(parent, ent);
1da177e4
LT
479 }
480 return ent;
481}
270b5ac2 482EXPORT_SYMBOL_GPL(proc_mkdir_data);
1da177e4 483
270b5ac2
DH
484struct proc_dir_entry *proc_mkdir_mode(const char *name, umode_t mode,
485 struct proc_dir_entry *parent)
78e92b99 486{
270b5ac2 487 return proc_mkdir_data(name, mode, parent, NULL);
78e92b99 488}
270b5ac2 489EXPORT_SYMBOL(proc_mkdir_mode);
78e92b99 490
1da177e4
LT
491struct proc_dir_entry *proc_mkdir(const char *name,
492 struct proc_dir_entry *parent)
493{
270b5ac2 494 return proc_mkdir_data(name, 0, parent, NULL);
1da177e4 495}
587d4a17 496EXPORT_SYMBOL(proc_mkdir);
1da177e4 497
eb6d38d5
EB
498struct proc_dir_entry *proc_create_mount_point(const char *name)
499{
500 umode_t mode = S_IFDIR | S_IRUGO | S_IXUGO;
501 struct proc_dir_entry *ent, *parent = NULL;
502
503 ent = __proc_create(&parent, name, mode, 2);
504 if (ent) {
505 ent->data = NULL;
d56c0d45 506 ent->proc_dir_ops = NULL;
eb6d38d5 507 ent->proc_iops = NULL;
61172eae 508 ent = proc_register(parent, ent);
eb6d38d5
EB
509 }
510 return ent;
511}
f97df70b 512EXPORT_SYMBOL(proc_create_mount_point);
eb6d38d5 513
7aed53d1
CH
514struct proc_dir_entry *proc_create_reg(const char *name, umode_t mode,
515 struct proc_dir_entry **parent, void *data)
2d3a4e36 516{
7aed53d1
CH
517 struct proc_dir_entry *p;
518
b6cdc731
AV
519 if ((mode & S_IFMT) == 0)
520 mode |= S_IFREG;
7aed53d1
CH
521 if ((mode & S_IALLUGO) == 0)
522 mode |= S_IRUGO;
523 if (WARN_ON_ONCE(!S_ISREG(mode)))
b6cdc731 524 return NULL;
7aed53d1
CH
525
526 p = __proc_create(parent, name, mode, 1);
527 if (p) {
528 p->proc_iops = &proc_file_inode_operations;
529 p->data = data;
2d3a4e36 530 }
7aed53d1
CH
531 return p;
532}
533
d919b33d
AD
534static inline void pde_set_flags(struct proc_dir_entry *pde)
535{
536 if (pde->proc_ops->proc_flags & PROC_ENTRY_PERMANENT)
537 pde->flags |= PROC_ENTRY_PERMANENT;
538}
539
7aed53d1
CH
540struct proc_dir_entry *proc_create_data(const char *name, umode_t mode,
541 struct proc_dir_entry *parent,
d56c0d45 542 const struct proc_ops *proc_ops, void *data)
7aed53d1
CH
543{
544 struct proc_dir_entry *p;
2d3a4e36 545
7aed53d1
CH
546 p = proc_create_reg(name, mode, &parent, data);
547 if (!p)
548 return NULL;
d56c0d45 549 p->proc_ops = proc_ops;
d919b33d 550 pde_set_flags(p);
7aed53d1 551 return proc_register(parent, p);
2d3a4e36 552}
587d4a17 553EXPORT_SYMBOL(proc_create_data);
271a15ea 554
855d9765
AD
555struct proc_dir_entry *proc_create(const char *name, umode_t mode,
556 struct proc_dir_entry *parent,
d56c0d45 557 const struct proc_ops *proc_ops)
855d9765 558{
d56c0d45 559 return proc_create_data(name, mode, parent, proc_ops, NULL);
855d9765
AD
560}
561EXPORT_SYMBOL(proc_create);
562
fddda2b7
CH
563static int proc_seq_open(struct inode *inode, struct file *file)
564{
565 struct proc_dir_entry *de = PDE(inode);
566
44414d82
CH
567 if (de->state_size)
568 return seq_open_private(file, de->seq_ops, de->state_size);
fddda2b7
CH
569 return seq_open(file, de->seq_ops);
570}
571
877f919e
CH
572static int proc_seq_release(struct inode *inode, struct file *file)
573{
574 struct proc_dir_entry *de = PDE(inode);
575
576 if (de->state_size)
577 return seq_release_private(inode, file);
578 return seq_release(inode, file);
579}
580
d56c0d45 581static const struct proc_ops proc_seq_ops = {
d919b33d 582 /* not permanent -- can call into arbitrary seq_operations */
d56c0d45
AD
583 .proc_open = proc_seq_open,
584 .proc_read = seq_read,
585 .proc_lseek = seq_lseek,
586 .proc_release = proc_seq_release,
fddda2b7
CH
587};
588
44414d82 589struct proc_dir_entry *proc_create_seq_private(const char *name, umode_t mode,
fddda2b7 590 struct proc_dir_entry *parent, const struct seq_operations *ops,
44414d82 591 unsigned int state_size, void *data)
fddda2b7
CH
592{
593 struct proc_dir_entry *p;
594
595 p = proc_create_reg(name, mode, &parent, data);
596 if (!p)
597 return NULL;
d56c0d45 598 p->proc_ops = &proc_seq_ops;
fddda2b7 599 p->seq_ops = ops;
44414d82 600 p->state_size = state_size;
fddda2b7
CH
601 return proc_register(parent, p);
602}
44414d82 603EXPORT_SYMBOL(proc_create_seq_private);
fddda2b7 604
3f3942ac
CH
605static int proc_single_open(struct inode *inode, struct file *file)
606{
607 struct proc_dir_entry *de = PDE(inode);
608
609 return single_open(file, de->single_show, de->data);
610}
611
d56c0d45 612static const struct proc_ops proc_single_ops = {
d919b33d 613 /* not permanent -- can call into arbitrary ->single_show */
d56c0d45
AD
614 .proc_open = proc_single_open,
615 .proc_read = seq_read,
616 .proc_lseek = seq_lseek,
617 .proc_release = single_release,
3f3942ac
CH
618};
619
620struct proc_dir_entry *proc_create_single_data(const char *name, umode_t mode,
621 struct proc_dir_entry *parent,
622 int (*show)(struct seq_file *, void *), void *data)
623{
624 struct proc_dir_entry *p;
625
626 p = proc_create_reg(name, mode, &parent, data);
627 if (!p)
628 return NULL;
d56c0d45 629 p->proc_ops = &proc_single_ops;
3f3942ac
CH
630 p->single_show = show;
631 return proc_register(parent, p);
632}
633EXPORT_SYMBOL(proc_create_single_data);
634
271a15ea
DH
635void proc_set_size(struct proc_dir_entry *de, loff_t size)
636{
637 de->size = size;
638}
639EXPORT_SYMBOL(proc_set_size);
640
641void proc_set_user(struct proc_dir_entry *de, kuid_t uid, kgid_t gid)
642{
643 de->uid = uid;
644 de->gid = gid;
645}
646EXPORT_SYMBOL(proc_set_user);
2d3a4e36 647
135d5655
AD
648void pde_put(struct proc_dir_entry *pde)
649{
9cdd83e3 650 if (refcount_dec_and_test(&pde->refcnt)) {
b4884f23
AD
651 proc_free_inum(pde->low_ino);
652 pde_free(pde);
653 }
135d5655
AD
654}
655
8ce584c7
AV
656/*
657 * Remove a /proc entry and free it if it's not currently in use.
658 */
659void remove_proc_entry(const char *name, struct proc_dir_entry *parent)
660{
8ce584c7
AV
661 struct proc_dir_entry *de = NULL;
662 const char *fn = name;
663 unsigned int len;
664
ecf1a3df 665 write_lock(&proc_subdir_lock);
8ce584c7 666 if (__xlate_proc_name(name, &parent, &fn) != 0) {
ecf1a3df 667 write_unlock(&proc_subdir_lock);
8ce584c7
AV
668 return;
669 }
670 len = strlen(fn);
671
710585d4 672 de = pde_subdir_find(parent, fn, len);
e06689bf 673 if (de) {
d919b33d
AD
674 if (unlikely(pde_is_permanent(de))) {
675 WARN(1, "removing permanent /proc entry '%s'", de->name);
676 de = NULL;
677 } else {
678 rb_erase(&de->subdir_node, &parent->subdir);
679 if (S_ISDIR(de->mode))
680 parent->nlink--;
e06689bf
AD
681 }
682 }
ecf1a3df 683 write_unlock(&proc_subdir_lock);
8ce584c7
AV
684 if (!de) {
685 WARN(1, "name '%s'\n", name);
686 return;
687 }
688
866ad9a7 689 proc_entry_rundown(de);
881adb85 690
710585d4
ND
691 WARN(pde_subdir_first(de),
692 "%s: removing non-empty directory '%s/%s', leaking at least '%s'\n",
693 __func__, de->parent->name, de->name, pde_subdir_first(de)->name);
135d5655 694 pde_put(de);
1da177e4 695}
587d4a17 696EXPORT_SYMBOL(remove_proc_entry);
8ce584c7
AV
697
698int remove_proc_subtree(const char *name, struct proc_dir_entry *parent)
699{
8ce584c7
AV
700 struct proc_dir_entry *root = NULL, *de, *next;
701 const char *fn = name;
702 unsigned int len;
703
ecf1a3df 704 write_lock(&proc_subdir_lock);
8ce584c7 705 if (__xlate_proc_name(name, &parent, &fn) != 0) {
ecf1a3df 706 write_unlock(&proc_subdir_lock);
8ce584c7
AV
707 return -ENOENT;
708 }
709 len = strlen(fn);
710
710585d4 711 root = pde_subdir_find(parent, fn, len);
8ce584c7 712 if (!root) {
ecf1a3df 713 write_unlock(&proc_subdir_lock);
8ce584c7
AV
714 return -ENOENT;
715 }
d919b33d
AD
716 if (unlikely(pde_is_permanent(root))) {
717 write_unlock(&proc_subdir_lock);
718 WARN(1, "removing permanent /proc entry '%s/%s'",
719 root->parent->name, root->name);
720 return -EINVAL;
721 }
4f113437 722 rb_erase(&root->subdir_node, &parent->subdir);
710585d4 723
8ce584c7
AV
724 de = root;
725 while (1) {
710585d4 726 next = pde_subdir_first(de);
8ce584c7 727 if (next) {
d919b33d
AD
728 if (unlikely(pde_is_permanent(root))) {
729 write_unlock(&proc_subdir_lock);
730 WARN(1, "removing permanent /proc entry '%s/%s'",
731 next->parent->name, next->name);
732 return -EINVAL;
733 }
4f113437 734 rb_erase(&next->subdir_node, &de->subdir);
8ce584c7
AV
735 de = next;
736 continue;
737 }
8ce584c7
AV
738 next = de->parent;
739 if (S_ISDIR(de->mode))
740 next->nlink--;
e06689bf
AD
741 write_unlock(&proc_subdir_lock);
742
743 proc_entry_rundown(de);
8ce584c7
AV
744 if (de == root)
745 break;
746 pde_put(de);
747
ecf1a3df 748 write_lock(&proc_subdir_lock);
8ce584c7
AV
749 de = next;
750 }
751 pde_put(root);
752 return 0;
753}
754EXPORT_SYMBOL(remove_proc_subtree);
4a520d27
DH
755
756void *proc_get_parent_data(const struct inode *inode)
757{
758 struct proc_dir_entry *de = PDE(inode);
759 return de->parent->data;
760}
761EXPORT_SYMBOL_GPL(proc_get_parent_data);
a8ca16ea
DH
762
763void proc_remove(struct proc_dir_entry *de)
764{
765 if (de)
766 remove_proc_subtree(de->name, de->parent);
767}
768EXPORT_SYMBOL(proc_remove);
c30480b9
DH
769
770void *PDE_DATA(const struct inode *inode)
771{
772 return __PDE_DATA(inode);
773}
774EXPORT_SYMBOL(PDE_DATA);
564def71
DH
775
776/*
777 * Pull a user buffer into memory and pass it to the file's write handler if
778 * one is supplied. The ->write() method is permitted to modify the
779 * kernel-side buffer.
780 */
781ssize_t proc_simple_write(struct file *f, const char __user *ubuf, size_t size,
782 loff_t *_pos)
783{
784 struct proc_dir_entry *pde = PDE(file_inode(f));
785 char *buf;
786 int ret;
787
788 if (!pde->write)
789 return -EACCES;
790 if (size == 0 || size > PAGE_SIZE - 1)
791 return -EINVAL;
792 buf = memdup_user_nul(ubuf, size);
793 if (IS_ERR(buf))
794 return PTR_ERR(buf);
795 ret = pde->write(f, buf, size);
796 kfree(buf);
797 return ret == 0 ? size : ret;
798}