1 // SPDX-License-Identifier: GPL-2.0-only 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 12 #include <linux/cache.h> 13 #include <linux/errno.h> 14 #include <linux/time.h> 15 #include <linux/proc_fs.h> 16 #include <linux/stat.h> 17 #include <linux/mm.h> 18 #include <linux/module.h> 19 #include <linux/namei.h> 20 #include <linux/slab.h> 21 #include <linux/printk.h> 22 #include <linux/mount.h> 23 #include <linux/init.h> 24 #include <linux/idr.h> 25 #include <linux/bitops.h> 26 #include <linux/spinlock.h> 27 #include <linux/completion.h> 28 #include <linux/uaccess.h> 29 #include <linux/seq_file.h> 30 31 #include "internal.h" 32 33 static DEFINE_RWLOCK(proc_subdir_lock); 34 35 struct kmem_cache *proc_dir_entry_cache __ro_after_init; 36 37 void 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 46 static int proc_match(const char *name, struct proc_dir_entry *de, unsigned int len) 47 { 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 56 static struct proc_dir_entry *pde_subdir_first(struct proc_dir_entry *dir) 57 { 58 return rb_entry_safe(rb_first(&dir->subdir), struct proc_dir_entry, 59 subdir_node); 60 } 61 62 static struct proc_dir_entry *pde_subdir_next(struct proc_dir_entry *dir) 63 { 64 return rb_entry_safe(rb_next(&dir->subdir_node), struct proc_dir_entry, 65 subdir_node); 66 } 67 68 static struct proc_dir_entry *pde_subdir_find(struct proc_dir_entry *dir, 69 const char *name, 70 unsigned int len) 71 { 72 struct rb_node *node = dir->subdir.rb_node; 73 74 while (node) { 75 struct proc_dir_entry *de = rb_entry(node, 76 struct proc_dir_entry, 77 subdir_node); 78 int result = proc_match(name, de, len); 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 90 static bool pde_subdir_insert(struct proc_dir_entry *dir, 91 struct proc_dir_entry *de) 92 { 93 struct rb_root *root = &dir->subdir; 94 struct rb_node **new = &root->rb_node, *parent = NULL; 95 96 /* Figure out where to put new node */ 97 while (*new) { 98 struct proc_dir_entry *this = rb_entry(*new, 99 struct proc_dir_entry, 100 subdir_node); 101 int result = proc_match(de->name, this, de->namelen); 102 103 parent = *new; 104 if (result < 0) 105 new = &(*new)->rb_left; 106 else if (result > 0) 107 new = &(*new)->rb_right; 108 else 109 return false; 110 } 111 112 /* Add new node and rebalance tree. */ 113 rb_link_node(&de->subdir_node, parent, new); 114 rb_insert_color(&de->subdir_node, root); 115 if (S_ISDIR(de->mode)) 116 dir->nlink++; 117 return true; 118 } 119 120 static int proc_setattr(const struct mnt_idmap *idmap, struct dentry *dentry, 121 struct iattr *iattr) 122 { 123 struct inode *inode = d_inode(dentry); 124 struct proc_dir_entry *de = PDE(inode); 125 int error; 126 127 error = setattr_prepare(&nop_mnt_idmap, dentry, iattr); 128 if (error) 129 return error; 130 131 setattr_copy(&nop_mnt_idmap, inode, iattr); 132 133 proc_set_user(de, inode->i_uid, inode->i_gid); 134 de->mode = inode->i_mode; 135 return 0; 136 } 137 138 static int proc_getattr(const struct mnt_idmap *idmap, 139 const struct path *path, struct kstat *stat, 140 u32 request_mask, unsigned int query_flags) 141 { 142 struct inode *inode = d_inode(path->dentry); 143 struct proc_dir_entry *de = PDE(inode); 144 if (de) { 145 nlink_t nlink = READ_ONCE(de->nlink); 146 if (nlink > 0) { 147 set_nlink(inode, nlink); 148 } 149 } 150 151 generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat); 152 return 0; 153 } 154 155 static const struct inode_operations proc_file_inode_operations = { 156 .setattr = proc_setattr, 157 }; 158 159 /* 160 * This function parses a name such as "tty/driver/serial", and 161 * returns the struct proc_dir_entry for "/proc/tty/driver", and 162 * returns "serial" in residual. 163 */ 164 static int __xlate_proc_name(const char *name, struct proc_dir_entry **ret, 165 const char **residual) 166 { 167 const char *cp = name, *next; 168 struct proc_dir_entry *de; 169 170 de = *ret ?: &proc_root; 171 while ((next = strchr(cp, '/')) != NULL) { 172 de = pde_subdir_find(de, cp, next - cp); 173 if (!de) { 174 WARN(1, "name '%s'\n", name); 175 return -ENOENT; 176 } 177 cp = next + 1; 178 } 179 *residual = cp; 180 *ret = de; 181 return 0; 182 } 183 184 static int xlate_proc_name(const char *name, struct proc_dir_entry **ret, 185 const char **residual) 186 { 187 int rv; 188 189 read_lock(&proc_subdir_lock); 190 rv = __xlate_proc_name(name, ret, residual); 191 read_unlock(&proc_subdir_lock); 192 return rv; 193 } 194 195 static DEFINE_IDA(proc_inum_ida); 196 197 #define PROC_DYNAMIC_FIRST 0xF0000000U 198 199 /* 200 * Return an inode number between PROC_DYNAMIC_FIRST and 201 * 0xffffffff, or zero on failure. 202 */ 203 int proc_alloc_inum(unsigned int *inum) 204 { 205 int i; 206 207 i = ida_alloc_max(&proc_inum_ida, UINT_MAX - PROC_DYNAMIC_FIRST, 208 GFP_KERNEL); 209 if (i < 0) 210 return i; 211 212 *inum = PROC_DYNAMIC_FIRST + (unsigned int)i; 213 return 0; 214 } 215 216 void proc_free_inum(unsigned int inum) 217 { 218 ida_free(&proc_inum_ida, inum - PROC_DYNAMIC_FIRST); 219 } 220 221 static int proc_misc_d_revalidate(struct inode *dir, const struct qstr *name, 222 struct dentry *dentry, unsigned int flags) 223 { 224 if (flags & LOOKUP_RCU) 225 return -ECHILD; 226 227 if (atomic_read(&PDE(d_inode(dentry))->in_use) < 0) 228 return 0; /* revalidate */ 229 return 1; 230 } 231 232 static int proc_misc_d_delete(const struct dentry *dentry) 233 { 234 return atomic_read(&PDE(d_inode(dentry))->in_use) < 0; 235 } 236 237 static const struct dentry_operations proc_misc_dentry_ops = { 238 .d_revalidate = proc_misc_d_revalidate, 239 .d_delete = proc_misc_d_delete, 240 }; 241 242 /* 243 * Don't create negative dentries here, return -ENOENT by hand 244 * instead. 245 */ 246 struct dentry *proc_lookup_de(struct inode *dir, struct dentry *dentry, 247 struct proc_dir_entry *de) 248 { 249 struct inode *inode; 250 251 read_lock(&proc_subdir_lock); 252 de = pde_subdir_find(de, dentry->d_name.name, dentry->d_name.len); 253 if (de) { 254 pde_get(de); 255 read_unlock(&proc_subdir_lock); 256 inode = proc_get_inode(dir->i_sb, de); 257 if (!inode) 258 return ERR_PTR(-ENOMEM); 259 /* never reused and so never a mountpoint for long */ 260 if (de->flags & PROC_ENTRY_FORCE_LOOKUP) 261 return proc_splice_unmountable(inode, dentry, 262 &proc_net_dentry_ops); 263 return d_splice_alias_ops(inode, dentry, 264 &proc_misc_dentry_ops); 265 } 266 read_unlock(&proc_subdir_lock); 267 return ERR_PTR(-ENOENT); 268 } 269 270 struct dentry *proc_lookup(struct inode *dir, struct dentry *dentry, 271 unsigned int flags) 272 { 273 struct proc_fs_info *fs_info = proc_sb_info(dir->i_sb); 274 275 if (fs_info->pidonly == PROC_PIDONLY_ON) 276 return ERR_PTR(-ENOENT); 277 278 return proc_lookup_de(dir, dentry, PDE(dir)); 279 } 280 281 /* 282 * This returns non-zero if at EOF, so that the /proc 283 * root directory can use this and check if it should 284 * continue with the <pid> entries.. 285 * 286 * Note that the VFS-layer doesn't care about the return 287 * value of the readdir() call, as long as it's non-negative 288 * for success.. 289 */ 290 int proc_readdir_de(struct file *file, struct dir_context *ctx, 291 struct proc_dir_entry *de) 292 { 293 int i; 294 295 if (!dir_emit_dots(file, ctx)) 296 return 0; 297 298 i = ctx->pos - 2; 299 read_lock(&proc_subdir_lock); 300 de = pde_subdir_first(de); 301 for (;;) { 302 if (!de) { 303 read_unlock(&proc_subdir_lock); 304 return 0; 305 } 306 if (!i) 307 break; 308 de = pde_subdir_next(de); 309 i--; 310 } 311 312 do { 313 struct proc_dir_entry *next; 314 pde_get(de); 315 read_unlock(&proc_subdir_lock); 316 if (!dir_emit(ctx, de->name, de->namelen, 317 de->low_ino, de->mode >> 12)) { 318 pde_put(de); 319 return 0; 320 } 321 ctx->pos++; 322 read_lock(&proc_subdir_lock); 323 next = pde_subdir_next(de); 324 pde_put(de); 325 de = next; 326 } while (de); 327 read_unlock(&proc_subdir_lock); 328 return 1; 329 } 330 331 int proc_readdir(struct file *file, struct dir_context *ctx) 332 { 333 struct inode *inode = file_inode(file); 334 struct proc_fs_info *fs_info = proc_sb_info(inode->i_sb); 335 336 if (fs_info->pidonly == PROC_PIDONLY_ON) 337 return 1; 338 339 return proc_readdir_de(file, ctx, PDE(inode)); 340 } 341 342 /* 343 * These are the generic /proc directory operations. They 344 * use the in-memory "struct proc_dir_entry" tree to parse 345 * the /proc directory. 346 */ 347 static const struct file_operations proc_dir_operations = { 348 .llseek = generic_file_llseek, 349 .read = generic_read_dir, 350 .iterate_shared = proc_readdir, 351 }; 352 353 static int proc_net_d_revalidate(struct inode *dir, const struct qstr *name, 354 struct dentry *dentry, unsigned int flags) 355 { 356 return 0; 357 } 358 359 const struct dentry_operations proc_net_dentry_ops = { 360 .d_revalidate = proc_net_d_revalidate, 361 .d_delete = always_delete_dentry, 362 }; 363 364 /* 365 * proc directories can do almost nothing.. 366 */ 367 static const struct inode_operations proc_dir_inode_operations = { 368 .lookup = proc_lookup, 369 .getattr = proc_getattr, 370 .setattr = proc_setattr, 371 }; 372 373 static void pde_set_flags(struct proc_dir_entry *pde) 374 { 375 const struct proc_ops *proc_ops = pde->proc_ops; 376 377 if (!proc_ops) 378 return; 379 380 if (proc_ops->proc_flags & PROC_ENTRY_PERMANENT) 381 pde->flags |= PROC_ENTRY_PERMANENT; 382 if (proc_ops->proc_read_iter) 383 pde->flags |= PROC_ENTRY_proc_read_iter; 384 #ifdef CONFIG_COMPAT 385 if (proc_ops->proc_compat_ioctl) 386 pde->flags |= PROC_ENTRY_proc_compat_ioctl; 387 #endif 388 if (proc_ops->proc_lseek) 389 pde->flags |= PROC_ENTRY_proc_lseek; 390 } 391 392 /* returns the registered entry, or frees dp and returns NULL on failure */ 393 struct proc_dir_entry *proc_register(struct proc_dir_entry *dir, 394 struct proc_dir_entry *dp) 395 { 396 if (proc_alloc_inum(&dp->low_ino)) 397 goto out_free_entry; 398 399 if (!S_ISDIR(dp->mode)) 400 pde_set_flags(dp); 401 402 write_lock(&proc_subdir_lock); 403 dp->parent = dir; 404 if (pde_subdir_insert(dir, dp) == false) { 405 WARN(1, "proc_dir_entry '%s/%s' already registered\n", 406 dir->name, dp->name); 407 write_unlock(&proc_subdir_lock); 408 goto out_free_inum; 409 } 410 write_unlock(&proc_subdir_lock); 411 412 return dp; 413 out_free_inum: 414 proc_free_inum(dp->low_ino); 415 out_free_entry: 416 pde_free(dp); 417 return NULL; 418 } 419 420 static struct proc_dir_entry *__proc_create(struct proc_dir_entry **parent, 421 const char *name, 422 umode_t mode, 423 nlink_t nlink) 424 { 425 struct proc_dir_entry *ent = NULL; 426 const char *fn; 427 struct qstr qstr; 428 429 if (xlate_proc_name(name, parent, &fn) != 0) 430 goto out; 431 qstr.name = fn; 432 qstr.len = strnlen(fn, NAME_MAX + 1); 433 if (qstr.len == 0) { 434 WARN(1, "empty name\n"); 435 return NULL; 436 } 437 if (qstr.len > NAME_MAX) { 438 WARN(1, "name too long\n"); 439 return NULL; 440 } 441 if (qstr.len == 1 && fn[0] == '.') { 442 WARN(1, "name '.'\n"); 443 return NULL; 444 } 445 if (qstr.len == 2 && fn[0] == '.' && fn[1] == '.') { 446 WARN(1, "name '..'\n"); 447 return NULL; 448 } 449 if (*parent == &proc_root && name_to_int(&qstr) != ~0U) { 450 WARN(1, "create '/proc/%s' by hand\n", qstr.name); 451 return NULL; 452 } 453 if (is_empty_pde(*parent)) { 454 WARN(1, "attempt to add to permanently empty directory"); 455 return NULL; 456 } 457 458 ent = kmem_cache_zalloc(proc_dir_entry_cache, GFP_KERNEL); 459 if (!ent) 460 goto out; 461 462 if (qstr.len + 1 <= SIZEOF_PDE_INLINE_NAME) { 463 ent->name = ent->inline_name; 464 } else { 465 ent->name = kmalloc(qstr.len + 1, GFP_KERNEL); 466 if (!ent->name) { 467 pde_free(ent); 468 return NULL; 469 } 470 } 471 472 memcpy(ent->name, fn, qstr.len + 1); 473 ent->namelen = qstr.len; 474 ent->mode = mode; 475 ent->nlink = nlink; 476 ent->subdir = RB_ROOT; 477 refcount_set(&ent->refcnt, 1); 478 spin_lock_init(&ent->pde_unload_lock); 479 INIT_LIST_HEAD(&ent->pde_openers); 480 proc_set_user(ent, (*parent)->uid, (*parent)->gid); 481 482 /* Revalidate everything under /proc/${pid}/net */ 483 if ((*parent)->flags & PROC_ENTRY_FORCE_LOOKUP) 484 pde_force_lookup(ent); 485 486 out: 487 return ent; 488 } 489 490 struct proc_dir_entry *proc_symlink(const char *name, 491 struct proc_dir_entry *parent, const char *dest) 492 { 493 struct proc_dir_entry *ent; 494 495 ent = __proc_create(&parent, name, 496 (S_IFLNK | S_IRUGO | S_IWUGO | S_IXUGO),1); 497 498 if (ent) { 499 ent->size = strlen(dest); 500 ent->data = kmemdup(dest, ent->size + 1, GFP_KERNEL); 501 if (ent->data) { 502 ent->proc_iops = &proc_link_inode_operations; 503 ent = proc_register(parent, ent); 504 } else { 505 pde_free(ent); 506 ent = NULL; 507 } 508 } 509 return ent; 510 } 511 EXPORT_SYMBOL(proc_symlink); 512 513 struct proc_dir_entry *_proc_mkdir(const char *name, umode_t mode, 514 struct proc_dir_entry *parent, void *data, bool force_lookup) 515 { 516 struct proc_dir_entry *ent; 517 518 if (mode == 0) 519 mode = S_IRUGO | S_IXUGO; 520 521 ent = __proc_create(&parent, name, S_IFDIR | mode, 2); 522 if (ent) { 523 ent->data = data; 524 ent->proc_dir_ops = &proc_dir_operations; 525 ent->proc_iops = &proc_dir_inode_operations; 526 if (force_lookup) { 527 pde_force_lookup(ent); 528 } 529 ent = proc_register(parent, ent); 530 } 531 return ent; 532 } 533 EXPORT_SYMBOL_GPL(_proc_mkdir); 534 535 struct proc_dir_entry *proc_mkdir_data(const char *name, umode_t mode, 536 struct proc_dir_entry *parent, void *data) 537 { 538 return _proc_mkdir(name, mode, parent, data, false); 539 } 540 EXPORT_SYMBOL_GPL(proc_mkdir_data); 541 542 struct proc_dir_entry *proc_mkdir_mode(const char *name, umode_t mode, 543 struct proc_dir_entry *parent) 544 { 545 return proc_mkdir_data(name, mode, parent, NULL); 546 } 547 EXPORT_SYMBOL(proc_mkdir_mode); 548 549 struct proc_dir_entry *proc_mkdir(const char *name, 550 struct proc_dir_entry *parent) 551 { 552 return proc_mkdir_data(name, 0, parent, NULL); 553 } 554 EXPORT_SYMBOL(proc_mkdir); 555 556 struct proc_dir_entry *proc_create_mount_point(const char *name) 557 { 558 umode_t mode = S_IFDIR | S_IRUGO | S_IXUGO; 559 struct proc_dir_entry *ent, *parent = NULL; 560 561 ent = __proc_create(&parent, name, mode, 2); 562 if (ent) { 563 ent->data = NULL; 564 ent->proc_dir_ops = NULL; 565 ent->proc_iops = NULL; 566 ent = proc_register(parent, ent); 567 } 568 return ent; 569 } 570 EXPORT_SYMBOL(proc_create_mount_point); 571 572 struct proc_dir_entry *proc_create_reg(const char *name, umode_t mode, 573 struct proc_dir_entry **parent, void *data) 574 { 575 struct proc_dir_entry *p; 576 577 if ((mode & S_IFMT) == 0) 578 mode |= S_IFREG; 579 if ((mode & S_IALLUGO) == 0) 580 mode |= S_IRUGO; 581 if (WARN_ON_ONCE(!S_ISREG(mode))) 582 return NULL; 583 584 p = __proc_create(parent, name, mode, 1); 585 if (p) { 586 p->proc_iops = &proc_file_inode_operations; 587 p->data = data; 588 } 589 return p; 590 } 591 592 struct proc_dir_entry *proc_create_data(const char *name, umode_t mode, 593 struct proc_dir_entry *parent, 594 const struct proc_ops *proc_ops, void *data) 595 { 596 struct proc_dir_entry *p; 597 598 p = proc_create_reg(name, mode, &parent, data); 599 if (!p) 600 return NULL; 601 p->proc_ops = proc_ops; 602 return proc_register(parent, p); 603 } 604 EXPORT_SYMBOL(proc_create_data); 605 606 struct proc_dir_entry *proc_create(const char *name, umode_t mode, 607 struct proc_dir_entry *parent, 608 const struct proc_ops *proc_ops) 609 { 610 return proc_create_data(name, mode, parent, proc_ops, NULL); 611 } 612 EXPORT_SYMBOL(proc_create); 613 614 static int proc_seq_open(struct inode *inode, struct file *file) 615 { 616 struct proc_dir_entry *de = PDE(inode); 617 618 if (de->state_size) 619 return seq_open_private(file, de->seq_ops, de->state_size); 620 return seq_open(file, de->seq_ops); 621 } 622 623 static int proc_seq_release(struct inode *inode, struct file *file) 624 { 625 struct proc_dir_entry *de = PDE(inode); 626 627 if (de->state_size) 628 return seq_release_private(inode, file); 629 return seq_release(inode, file); 630 } 631 632 static const struct proc_ops proc_seq_ops = { 633 /* not permanent -- can call into arbitrary seq_operations */ 634 .proc_open = proc_seq_open, 635 .proc_read_iter = seq_read_iter, 636 .proc_lseek = seq_lseek, 637 .proc_release = proc_seq_release, 638 }; 639 640 struct proc_dir_entry *proc_create_seq_private(const char *name, umode_t mode, 641 struct proc_dir_entry *parent, const struct seq_operations *ops, 642 unsigned int state_size, void *data) 643 { 644 struct proc_dir_entry *p; 645 646 p = proc_create_reg(name, mode, &parent, data); 647 if (!p) 648 return NULL; 649 p->proc_ops = &proc_seq_ops; 650 p->seq_ops = ops; 651 p->state_size = state_size; 652 return proc_register(parent, p); 653 } 654 EXPORT_SYMBOL(proc_create_seq_private); 655 656 static int proc_single_open(struct inode *inode, struct file *file) 657 { 658 struct proc_dir_entry *de = PDE(inode); 659 660 return single_open(file, de->single_show, de->data); 661 } 662 663 static const struct proc_ops proc_single_ops = { 664 /* not permanent -- can call into arbitrary ->single_show */ 665 .proc_open = proc_single_open, 666 .proc_read_iter = seq_read_iter, 667 .proc_lseek = seq_lseek, 668 .proc_release = single_release, 669 }; 670 671 struct proc_dir_entry *proc_create_single_data(const char *name, umode_t mode, 672 struct proc_dir_entry *parent, 673 int (*show)(struct seq_file *, void *), void *data) 674 { 675 struct proc_dir_entry *p; 676 677 p = proc_create_reg(name, mode, &parent, data); 678 if (!p) 679 return NULL; 680 p->proc_ops = &proc_single_ops; 681 p->single_show = show; 682 return proc_register(parent, p); 683 } 684 EXPORT_SYMBOL(proc_create_single_data); 685 686 void proc_set_size(struct proc_dir_entry *de, loff_t size) 687 { 688 de->size = size; 689 } 690 EXPORT_SYMBOL(proc_set_size); 691 692 void proc_set_user(struct proc_dir_entry *de, kuid_t uid, kgid_t gid) 693 { 694 de->uid = uid; 695 de->gid = gid; 696 } 697 EXPORT_SYMBOL(proc_set_user); 698 699 void pde_put(struct proc_dir_entry *pde) 700 { 701 if (refcount_dec_and_test(&pde->refcnt)) { 702 proc_free_inum(pde->low_ino); 703 pde_free(pde); 704 } 705 } 706 707 static void pde_erase(struct proc_dir_entry *pde, struct proc_dir_entry *parent) 708 { 709 rb_erase(&pde->subdir_node, &parent->subdir); 710 RB_CLEAR_NODE(&pde->subdir_node); 711 if (S_ISDIR(pde->mode)) 712 parent->nlink--; 713 } 714 715 /* 716 * Remove a /proc entry and free it if it's not currently in use. 717 */ 718 void remove_proc_entry(const char *name, struct proc_dir_entry *parent) 719 { 720 struct proc_dir_entry *de = NULL; 721 const char *fn = name; 722 unsigned int len; 723 724 write_lock(&proc_subdir_lock); 725 if (__xlate_proc_name(name, &parent, &fn) != 0) { 726 write_unlock(&proc_subdir_lock); 727 return; 728 } 729 len = strlen(fn); 730 731 de = pde_subdir_find(parent, fn, len); 732 if (de) { 733 if (unlikely(pde_is_permanent(de))) { 734 WARN(1, "removing permanent /proc entry '%s'", de->name); 735 de = NULL; 736 } else { 737 pde_erase(de, parent); 738 } 739 } 740 write_unlock(&proc_subdir_lock); 741 if (!de) { 742 WARN(1, "name '%s'\n", name); 743 return; 744 } 745 746 proc_entry_rundown(de); 747 748 WARN(pde_subdir_first(de), 749 "%s: removing non-empty directory '%s/%s', leaking at least '%s'\n", 750 __func__, de->parent->name, de->name, pde_subdir_first(de)->name); 751 pde_put(de); 752 } 753 EXPORT_SYMBOL(remove_proc_entry); 754 755 int remove_proc_subtree(const char *name, struct proc_dir_entry *parent) 756 { 757 struct proc_dir_entry *root = NULL, *de, *next; 758 const char *fn = name; 759 unsigned int len; 760 761 write_lock(&proc_subdir_lock); 762 if (__xlate_proc_name(name, &parent, &fn) != 0) { 763 write_unlock(&proc_subdir_lock); 764 return -ENOENT; 765 } 766 len = strlen(fn); 767 768 root = pde_subdir_find(parent, fn, len); 769 if (!root) { 770 write_unlock(&proc_subdir_lock); 771 return -ENOENT; 772 } 773 if (unlikely(pde_is_permanent(root))) { 774 write_unlock(&proc_subdir_lock); 775 WARN(1, "removing permanent /proc entry '%s/%s'", 776 root->parent->name, root->name); 777 return -EINVAL; 778 } 779 pde_erase(root, parent); 780 781 de = root; 782 while (1) { 783 next = pde_subdir_first(de); 784 if (next) { 785 if (unlikely(pde_is_permanent(next))) { 786 write_unlock(&proc_subdir_lock); 787 WARN(1, "removing permanent /proc entry '%s/%s'", 788 next->parent->name, next->name); 789 return -EINVAL; 790 } 791 pde_erase(next, de); 792 de = next; 793 continue; 794 } 795 next = de->parent; 796 write_unlock(&proc_subdir_lock); 797 798 proc_entry_rundown(de); 799 if (de == root) 800 break; 801 pde_put(de); 802 803 write_lock(&proc_subdir_lock); 804 de = next; 805 } 806 pde_put(root); 807 return 0; 808 } 809 EXPORT_SYMBOL(remove_proc_subtree); 810 811 void *proc_get_parent_data(const struct inode *inode) 812 { 813 struct proc_dir_entry *de = PDE(inode); 814 return de->parent->data; 815 } 816 EXPORT_SYMBOL_GPL(proc_get_parent_data); 817 818 void proc_remove(struct proc_dir_entry *de) 819 { 820 if (de) 821 remove_proc_subtree(de->name, de->parent); 822 } 823 EXPORT_SYMBOL(proc_remove); 824 825 /* 826 * Pull a user buffer into memory and pass it to the file's write handler if 827 * one is supplied. The ->write() method is permitted to modify the 828 * kernel-side buffer. 829 */ 830 ssize_t proc_simple_write(struct file *f, const char __user *ubuf, size_t size, 831 loff_t *_pos) 832 { 833 struct proc_dir_entry *pde = PDE(file_inode(f)); 834 char *buf; 835 int ret; 836 837 if (!pde->write) 838 return -EACCES; 839 if (size == 0 || size > PAGE_SIZE - 1) 840 return -EINVAL; 841 buf = memdup_user_nul(ubuf, size); 842 if (IS_ERR(buf)) 843 return PTR_ERR(buf); 844 ret = pde->write(f, buf, size); 845 kfree(buf); 846 return ret == 0 ? size : ret; 847 } 848 849 /* 850 * Not exported to modules: 851 * modules' /proc files aren't permanent because modules aren't permanent. 852 */ 853 void impl_proc_make_permanent(struct proc_dir_entry *pde) 854 { 855 if (pde) 856 pde_make_permanent(pde); 857 } 858