1 /* 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> 15 #include <linux/module.h> 16 #include <linux/slab.h> 17 #include <linux/mount.h> 18 #include <linux/init.h> 19 #include <linux/idr.h> 20 #include <linux/namei.h> 21 #include <linux/bitops.h> 22 #include <linux/spinlock.h> 23 #include <linux/completion.h> 24 #include <asm/uaccess.h> 25 26 #include "internal.h" 27 28 DEFINE_SPINLOCK(proc_subdir_lock); 29 30 static int proc_match(int len, const char *name, struct proc_dir_entry *de) 31 { 32 if (de->namelen != len) 33 return 0; 34 return !memcmp(name, de->name, len); 35 } 36 37 /* buffer size is one page but our output routines use some slack for overruns */ 38 #define PROC_BLOCK_SIZE (PAGE_SIZE - 1024) 39 40 static ssize_t 41 __proc_file_read(struct file *file, char __user *buf, size_t nbytes, 42 loff_t *ppos) 43 { 44 struct inode * inode = file->f_path.dentry->d_inode; 45 char *page; 46 ssize_t retval=0; 47 int eof=0; 48 ssize_t n, count; 49 char *start; 50 struct proc_dir_entry * dp; 51 unsigned long long pos; 52 53 /* 54 * Gaah, please just use "seq_file" instead. The legacy /proc 55 * interfaces cut loff_t down to off_t for reads, and ignore 56 * the offset entirely for writes.. 57 */ 58 pos = *ppos; 59 if (pos > MAX_NON_LFS) 60 return 0; 61 if (nbytes > MAX_NON_LFS - pos) 62 nbytes = MAX_NON_LFS - pos; 63 64 dp = PDE(inode); 65 if (!(page = (char*) __get_free_page(GFP_TEMPORARY))) 66 return -ENOMEM; 67 68 while ((nbytes > 0) && !eof) { 69 count = min_t(size_t, PROC_BLOCK_SIZE, nbytes); 70 71 start = NULL; 72 if (dp->read_proc) { 73 /* 74 * How to be a proc read function 75 * ------------------------------ 76 * Prototype: 77 * int f(char *buffer, char **start, off_t offset, 78 * int count, int *peof, void *dat) 79 * 80 * Assume that the buffer is "count" bytes in size. 81 * 82 * If you know you have supplied all the data you 83 * have, set *peof. 84 * 85 * You have three ways to return data: 86 * 0) Leave *start = NULL. (This is the default.) 87 * Put the data of the requested offset at that 88 * offset within the buffer. Return the number (n) 89 * of bytes there are from the beginning of the 90 * buffer up to the last byte of data. If the 91 * number of supplied bytes (= n - offset) is 92 * greater than zero and you didn't signal eof 93 * and the reader is prepared to take more data 94 * you will be called again with the requested 95 * offset advanced by the number of bytes 96 * absorbed. This interface is useful for files 97 * no larger than the buffer. 98 * 1) Set *start = an unsigned long value less than 99 * the buffer address but greater than zero. 100 * Put the data of the requested offset at the 101 * beginning of the buffer. Return the number of 102 * bytes of data placed there. If this number is 103 * greater than zero and you didn't signal eof 104 * and the reader is prepared to take more data 105 * you will be called again with the requested 106 * offset advanced by *start. This interface is 107 * useful when you have a large file consisting 108 * of a series of blocks which you want to count 109 * and return as wholes. 110 * (Hack by Paul.Russell@rustcorp.com.au) 111 * 2) Set *start = an address within the buffer. 112 * Put the data of the requested offset at *start. 113 * Return the number of bytes of data placed there. 114 * If this number is greater than zero and you 115 * didn't signal eof and the reader is prepared to 116 * take more data you will be called again with the 117 * requested offset advanced by the number of bytes 118 * absorbed. 119 */ 120 n = dp->read_proc(page, &start, *ppos, 121 count, &eof, dp->data); 122 } else 123 break; 124 125 if (n == 0) /* end of file */ 126 break; 127 if (n < 0) { /* error */ 128 if (retval == 0) 129 retval = n; 130 break; 131 } 132 133 if (start == NULL) { 134 if (n > PAGE_SIZE) { 135 printk(KERN_ERR 136 "proc_file_read: Apparent buffer overflow!\n"); 137 n = PAGE_SIZE; 138 } 139 n -= *ppos; 140 if (n <= 0) 141 break; 142 if (n > count) 143 n = count; 144 start = page + *ppos; 145 } else if (start < page) { 146 if (n > PAGE_SIZE) { 147 printk(KERN_ERR 148 "proc_file_read: Apparent buffer overflow!\n"); 149 n = PAGE_SIZE; 150 } 151 if (n > count) { 152 /* 153 * Don't reduce n because doing so might 154 * cut off part of a data block. 155 */ 156 printk(KERN_WARNING 157 "proc_file_read: Read count exceeded\n"); 158 } 159 } else /* start >= page */ { 160 unsigned long startoff = (unsigned long)(start - page); 161 if (n > (PAGE_SIZE - startoff)) { 162 printk(KERN_ERR 163 "proc_file_read: Apparent buffer overflow!\n"); 164 n = PAGE_SIZE - startoff; 165 } 166 if (n > count) 167 n = count; 168 } 169 170 n -= copy_to_user(buf, start < page ? page : start, n); 171 if (n == 0) { 172 if (retval == 0) 173 retval = -EFAULT; 174 break; 175 } 176 177 *ppos += start < page ? (unsigned long)start : n; 178 nbytes -= n; 179 buf += n; 180 retval += n; 181 } 182 free_page((unsigned long) page); 183 return retval; 184 } 185 186 static ssize_t 187 proc_file_read(struct file *file, char __user *buf, size_t nbytes, 188 loff_t *ppos) 189 { 190 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode); 191 ssize_t rv = -EIO; 192 193 spin_lock(&pde->pde_unload_lock); 194 if (!pde->proc_fops) { 195 spin_unlock(&pde->pde_unload_lock); 196 return rv; 197 } 198 pde->pde_users++; 199 spin_unlock(&pde->pde_unload_lock); 200 201 rv = __proc_file_read(file, buf, nbytes, ppos); 202 203 pde_users_dec(pde); 204 return rv; 205 } 206 207 static ssize_t 208 proc_file_write(struct file *file, const char __user *buffer, 209 size_t count, loff_t *ppos) 210 { 211 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode); 212 ssize_t rv = -EIO; 213 214 if (pde->write_proc) { 215 spin_lock(&pde->pde_unload_lock); 216 if (!pde->proc_fops) { 217 spin_unlock(&pde->pde_unload_lock); 218 return rv; 219 } 220 pde->pde_users++; 221 spin_unlock(&pde->pde_unload_lock); 222 223 /* FIXME: does this routine need ppos? probably... */ 224 rv = pde->write_proc(file, buffer, count, pde->data); 225 pde_users_dec(pde); 226 } 227 return rv; 228 } 229 230 231 static loff_t 232 proc_file_lseek(struct file *file, loff_t offset, int orig) 233 { 234 loff_t retval = -EINVAL; 235 switch (orig) { 236 case 1: 237 offset += file->f_pos; 238 /* fallthrough */ 239 case 0: 240 if (offset < 0 || offset > MAX_NON_LFS) 241 break; 242 file->f_pos = retval = offset; 243 } 244 return retval; 245 } 246 247 static const struct file_operations proc_file_operations = { 248 .llseek = proc_file_lseek, 249 .read = proc_file_read, 250 .write = proc_file_write, 251 }; 252 253 static int proc_notify_change(struct dentry *dentry, struct iattr *iattr) 254 { 255 struct inode *inode = dentry->d_inode; 256 struct proc_dir_entry *de = PDE(inode); 257 int error; 258 259 error = inode_change_ok(inode, iattr); 260 if (error) 261 goto out; 262 263 error = inode_setattr(inode, iattr); 264 if (error) 265 goto out; 266 267 de->uid = inode->i_uid; 268 de->gid = inode->i_gid; 269 de->mode = inode->i_mode; 270 out: 271 return error; 272 } 273 274 static int proc_getattr(struct vfsmount *mnt, struct dentry *dentry, 275 struct kstat *stat) 276 { 277 struct inode *inode = dentry->d_inode; 278 struct proc_dir_entry *de = PROC_I(inode)->pde; 279 if (de && de->nlink) 280 inode->i_nlink = de->nlink; 281 282 generic_fillattr(inode, stat); 283 return 0; 284 } 285 286 static const struct inode_operations proc_file_inode_operations = { 287 .setattr = proc_notify_change, 288 }; 289 290 /* 291 * This function parses a name such as "tty/driver/serial", and 292 * returns the struct proc_dir_entry for "/proc/tty/driver", and 293 * returns "serial" in residual. 294 */ 295 static int __xlate_proc_name(const char *name, struct proc_dir_entry **ret, 296 const char **residual) 297 { 298 const char *cp = name, *next; 299 struct proc_dir_entry *de; 300 int len; 301 302 de = *ret; 303 if (!de) 304 de = &proc_root; 305 306 while (1) { 307 next = strchr(cp, '/'); 308 if (!next) 309 break; 310 311 len = next - cp; 312 for (de = de->subdir; de ; de = de->next) { 313 if (proc_match(len, cp, de)) 314 break; 315 } 316 if (!de) { 317 WARN(1, "name '%s'\n", name); 318 return -ENOENT; 319 } 320 cp += len + 1; 321 } 322 *residual = cp; 323 *ret = de; 324 return 0; 325 } 326 327 static int xlate_proc_name(const char *name, struct proc_dir_entry **ret, 328 const char **residual) 329 { 330 int rv; 331 332 spin_lock(&proc_subdir_lock); 333 rv = __xlate_proc_name(name, ret, residual); 334 spin_unlock(&proc_subdir_lock); 335 return rv; 336 } 337 338 static DEFINE_IDA(proc_inum_ida); 339 static DEFINE_SPINLOCK(proc_inum_lock); /* protects the above */ 340 341 #define PROC_DYNAMIC_FIRST 0xF0000000U 342 343 /* 344 * Return an inode number between PROC_DYNAMIC_FIRST and 345 * 0xffffffff, or zero on failure. 346 * 347 * Current inode allocations in the proc-fs (hex-numbers): 348 * 349 * 00000000 reserved 350 * 00000001-00000fff static entries (goners) 351 * 001 root-ino 352 * 353 * 00001000-00001fff unused 354 * 0001xxxx-7fffxxxx pid-dir entries for pid 1-7fff 355 * 80000000-efffffff unused 356 * f0000000-ffffffff dynamic entries 357 * 358 * Goal: 359 * Once we split the thing into several virtual filesystems, 360 * we will get rid of magical ranges (and this comment, BTW). 361 */ 362 static unsigned int get_inode_number(void) 363 { 364 unsigned int i; 365 int error; 366 367 retry: 368 if (ida_pre_get(&proc_inum_ida, GFP_KERNEL) == 0) 369 return 0; 370 371 spin_lock(&proc_inum_lock); 372 error = ida_get_new(&proc_inum_ida, &i); 373 spin_unlock(&proc_inum_lock); 374 if (error == -EAGAIN) 375 goto retry; 376 else if (error) 377 return 0; 378 379 if (i > UINT_MAX - PROC_DYNAMIC_FIRST) { 380 spin_lock(&proc_inum_lock); 381 ida_remove(&proc_inum_ida, i); 382 spin_unlock(&proc_inum_lock); 383 return 0; 384 } 385 return PROC_DYNAMIC_FIRST + i; 386 } 387 388 static void release_inode_number(unsigned int inum) 389 { 390 spin_lock(&proc_inum_lock); 391 ida_remove(&proc_inum_ida, inum - PROC_DYNAMIC_FIRST); 392 spin_unlock(&proc_inum_lock); 393 } 394 395 static void *proc_follow_link(struct dentry *dentry, struct nameidata *nd) 396 { 397 nd_set_link(nd, PDE(dentry->d_inode)->data); 398 return NULL; 399 } 400 401 static const struct inode_operations proc_link_inode_operations = { 402 .readlink = generic_readlink, 403 .follow_link = proc_follow_link, 404 }; 405 406 /* 407 * As some entries in /proc are volatile, we want to 408 * get rid of unused dentries. This could be made 409 * smarter: we could keep a "volatile" flag in the 410 * inode to indicate which ones to keep. 411 */ 412 static int proc_delete_dentry(struct dentry * dentry) 413 { 414 return 1; 415 } 416 417 static const struct dentry_operations proc_dentry_operations = 418 { 419 .d_delete = proc_delete_dentry, 420 }; 421 422 /* 423 * Don't create negative dentries here, return -ENOENT by hand 424 * instead. 425 */ 426 struct dentry *proc_lookup_de(struct proc_dir_entry *de, struct inode *dir, 427 struct dentry *dentry) 428 { 429 struct inode *inode = NULL; 430 int error = -ENOENT; 431 432 spin_lock(&proc_subdir_lock); 433 for (de = de->subdir; de ; de = de->next) { 434 if (de->namelen != dentry->d_name.len) 435 continue; 436 if (!memcmp(dentry->d_name.name, de->name, de->namelen)) { 437 unsigned int ino; 438 439 ino = de->low_ino; 440 pde_get(de); 441 spin_unlock(&proc_subdir_lock); 442 error = -EINVAL; 443 inode = proc_get_inode(dir->i_sb, ino, de); 444 goto out_unlock; 445 } 446 } 447 spin_unlock(&proc_subdir_lock); 448 out_unlock: 449 450 if (inode) { 451 dentry->d_op = &proc_dentry_operations; 452 d_add(dentry, inode); 453 return NULL; 454 } 455 if (de) 456 pde_put(de); 457 return ERR_PTR(error); 458 } 459 460 struct dentry *proc_lookup(struct inode *dir, struct dentry *dentry, 461 struct nameidata *nd) 462 { 463 return proc_lookup_de(PDE(dir), dir, dentry); 464 } 465 466 /* 467 * This returns non-zero if at EOF, so that the /proc 468 * root directory can use this and check if it should 469 * continue with the <pid> entries.. 470 * 471 * Note that the VFS-layer doesn't care about the return 472 * value of the readdir() call, as long as it's non-negative 473 * for success.. 474 */ 475 int proc_readdir_de(struct proc_dir_entry *de, struct file *filp, void *dirent, 476 filldir_t filldir) 477 { 478 unsigned int ino; 479 int i; 480 struct inode *inode = filp->f_path.dentry->d_inode; 481 int ret = 0; 482 483 ino = inode->i_ino; 484 i = filp->f_pos; 485 switch (i) { 486 case 0: 487 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0) 488 goto out; 489 i++; 490 filp->f_pos++; 491 /* fall through */ 492 case 1: 493 if (filldir(dirent, "..", 2, i, 494 parent_ino(filp->f_path.dentry), 495 DT_DIR) < 0) 496 goto out; 497 i++; 498 filp->f_pos++; 499 /* fall through */ 500 default: 501 spin_lock(&proc_subdir_lock); 502 de = de->subdir; 503 i -= 2; 504 for (;;) { 505 if (!de) { 506 ret = 1; 507 spin_unlock(&proc_subdir_lock); 508 goto out; 509 } 510 if (!i) 511 break; 512 de = de->next; 513 i--; 514 } 515 516 do { 517 struct proc_dir_entry *next; 518 519 /* filldir passes info to user space */ 520 pde_get(de); 521 spin_unlock(&proc_subdir_lock); 522 if (filldir(dirent, de->name, de->namelen, filp->f_pos, 523 de->low_ino, de->mode >> 12) < 0) { 524 pde_put(de); 525 goto out; 526 } 527 spin_lock(&proc_subdir_lock); 528 filp->f_pos++; 529 next = de->next; 530 pde_put(de); 531 de = next; 532 } while (de); 533 spin_unlock(&proc_subdir_lock); 534 } 535 ret = 1; 536 out: 537 return ret; 538 } 539 540 int proc_readdir(struct file *filp, void *dirent, filldir_t filldir) 541 { 542 struct inode *inode = filp->f_path.dentry->d_inode; 543 544 return proc_readdir_de(PDE(inode), filp, dirent, filldir); 545 } 546 547 /* 548 * These are the generic /proc directory operations. They 549 * use the in-memory "struct proc_dir_entry" tree to parse 550 * the /proc directory. 551 */ 552 static const struct file_operations proc_dir_operations = { 553 .llseek = generic_file_llseek, 554 .read = generic_read_dir, 555 .readdir = proc_readdir, 556 }; 557 558 /* 559 * proc directories can do almost nothing.. 560 */ 561 static const struct inode_operations proc_dir_inode_operations = { 562 .lookup = proc_lookup, 563 .getattr = proc_getattr, 564 .setattr = proc_notify_change, 565 }; 566 567 static int proc_register(struct proc_dir_entry * dir, struct proc_dir_entry * dp) 568 { 569 unsigned int i; 570 struct proc_dir_entry *tmp; 571 572 i = get_inode_number(); 573 if (i == 0) 574 return -EAGAIN; 575 dp->low_ino = i; 576 577 if (S_ISDIR(dp->mode)) { 578 if (dp->proc_iops == NULL) { 579 dp->proc_fops = &proc_dir_operations; 580 dp->proc_iops = &proc_dir_inode_operations; 581 } 582 dir->nlink++; 583 } else if (S_ISLNK(dp->mode)) { 584 if (dp->proc_iops == NULL) 585 dp->proc_iops = &proc_link_inode_operations; 586 } else if (S_ISREG(dp->mode)) { 587 if (dp->proc_fops == NULL) 588 dp->proc_fops = &proc_file_operations; 589 if (dp->proc_iops == NULL) 590 dp->proc_iops = &proc_file_inode_operations; 591 } 592 593 spin_lock(&proc_subdir_lock); 594 595 for (tmp = dir->subdir; tmp; tmp = tmp->next) 596 if (strcmp(tmp->name, dp->name) == 0) { 597 WARN(1, KERN_WARNING "proc_dir_entry '%s/%s' already registered\n", 598 dir->name, dp->name); 599 break; 600 } 601 602 dp->next = dir->subdir; 603 dp->parent = dir; 604 dir->subdir = dp; 605 spin_unlock(&proc_subdir_lock); 606 607 return 0; 608 } 609 610 static struct proc_dir_entry *__proc_create(struct proc_dir_entry **parent, 611 const char *name, 612 mode_t mode, 613 nlink_t nlink) 614 { 615 struct proc_dir_entry *ent = NULL; 616 const char *fn = name; 617 int len; 618 619 /* make sure name is valid */ 620 if (!name || !strlen(name)) goto out; 621 622 if (xlate_proc_name(name, parent, &fn) != 0) 623 goto out; 624 625 /* At this point there must not be any '/' characters beyond *fn */ 626 if (strchr(fn, '/')) 627 goto out; 628 629 len = strlen(fn); 630 631 ent = kmalloc(sizeof(struct proc_dir_entry) + len + 1, GFP_KERNEL); 632 if (!ent) goto out; 633 634 memset(ent, 0, sizeof(struct proc_dir_entry)); 635 memcpy(((char *) ent) + sizeof(struct proc_dir_entry), fn, len + 1); 636 ent->name = ((char *) ent) + sizeof(*ent); 637 ent->namelen = len; 638 ent->mode = mode; 639 ent->nlink = nlink; 640 atomic_set(&ent->count, 1); 641 ent->pde_users = 0; 642 spin_lock_init(&ent->pde_unload_lock); 643 ent->pde_unload_completion = NULL; 644 INIT_LIST_HEAD(&ent->pde_openers); 645 out: 646 return ent; 647 } 648 649 struct proc_dir_entry *proc_symlink(const char *name, 650 struct proc_dir_entry *parent, const char *dest) 651 { 652 struct proc_dir_entry *ent; 653 654 ent = __proc_create(&parent, name, 655 (S_IFLNK | S_IRUGO | S_IWUGO | S_IXUGO),1); 656 657 if (ent) { 658 ent->data = kmalloc((ent->size=strlen(dest))+1, GFP_KERNEL); 659 if (ent->data) { 660 strcpy((char*)ent->data,dest); 661 if (proc_register(parent, ent) < 0) { 662 kfree(ent->data); 663 kfree(ent); 664 ent = NULL; 665 } 666 } else { 667 kfree(ent); 668 ent = NULL; 669 } 670 } 671 return ent; 672 } 673 EXPORT_SYMBOL(proc_symlink); 674 675 struct proc_dir_entry *proc_mkdir_mode(const char *name, mode_t mode, 676 struct proc_dir_entry *parent) 677 { 678 struct proc_dir_entry *ent; 679 680 ent = __proc_create(&parent, name, S_IFDIR | mode, 2); 681 if (ent) { 682 if (proc_register(parent, ent) < 0) { 683 kfree(ent); 684 ent = NULL; 685 } 686 } 687 return ent; 688 } 689 690 struct proc_dir_entry *proc_net_mkdir(struct net *net, const char *name, 691 struct proc_dir_entry *parent) 692 { 693 struct proc_dir_entry *ent; 694 695 ent = __proc_create(&parent, name, S_IFDIR | S_IRUGO | S_IXUGO, 2); 696 if (ent) { 697 ent->data = net; 698 if (proc_register(parent, ent) < 0) { 699 kfree(ent); 700 ent = NULL; 701 } 702 } 703 return ent; 704 } 705 EXPORT_SYMBOL_GPL(proc_net_mkdir); 706 707 struct proc_dir_entry *proc_mkdir(const char *name, 708 struct proc_dir_entry *parent) 709 { 710 return proc_mkdir_mode(name, S_IRUGO | S_IXUGO, parent); 711 } 712 EXPORT_SYMBOL(proc_mkdir); 713 714 struct proc_dir_entry *create_proc_entry(const char *name, mode_t mode, 715 struct proc_dir_entry *parent) 716 { 717 struct proc_dir_entry *ent; 718 nlink_t nlink; 719 720 if (S_ISDIR(mode)) { 721 if ((mode & S_IALLUGO) == 0) 722 mode |= S_IRUGO | S_IXUGO; 723 nlink = 2; 724 } else { 725 if ((mode & S_IFMT) == 0) 726 mode |= S_IFREG; 727 if ((mode & S_IALLUGO) == 0) 728 mode |= S_IRUGO; 729 nlink = 1; 730 } 731 732 ent = __proc_create(&parent, name, mode, nlink); 733 if (ent) { 734 if (proc_register(parent, ent) < 0) { 735 kfree(ent); 736 ent = NULL; 737 } 738 } 739 return ent; 740 } 741 EXPORT_SYMBOL(create_proc_entry); 742 743 struct proc_dir_entry *proc_create_data(const char *name, mode_t mode, 744 struct proc_dir_entry *parent, 745 const struct file_operations *proc_fops, 746 void *data) 747 { 748 struct proc_dir_entry *pde; 749 nlink_t nlink; 750 751 if (S_ISDIR(mode)) { 752 if ((mode & S_IALLUGO) == 0) 753 mode |= S_IRUGO | S_IXUGO; 754 nlink = 2; 755 } else { 756 if ((mode & S_IFMT) == 0) 757 mode |= S_IFREG; 758 if ((mode & S_IALLUGO) == 0) 759 mode |= S_IRUGO; 760 nlink = 1; 761 } 762 763 pde = __proc_create(&parent, name, mode, nlink); 764 if (!pde) 765 goto out; 766 pde->proc_fops = proc_fops; 767 pde->data = data; 768 if (proc_register(parent, pde) < 0) 769 goto out_free; 770 return pde; 771 out_free: 772 kfree(pde); 773 out: 774 return NULL; 775 } 776 EXPORT_SYMBOL(proc_create_data); 777 778 static void free_proc_entry(struct proc_dir_entry *de) 779 { 780 unsigned int ino = de->low_ino; 781 782 if (ino < PROC_DYNAMIC_FIRST) 783 return; 784 785 release_inode_number(ino); 786 787 if (S_ISLNK(de->mode)) 788 kfree(de->data); 789 kfree(de); 790 } 791 792 void pde_put(struct proc_dir_entry *pde) 793 { 794 if (atomic_dec_and_test(&pde->count)) 795 free_proc_entry(pde); 796 } 797 798 /* 799 * Remove a /proc entry and free it if it's not currently in use. 800 */ 801 void remove_proc_entry(const char *name, struct proc_dir_entry *parent) 802 { 803 struct proc_dir_entry **p; 804 struct proc_dir_entry *de = NULL; 805 const char *fn = name; 806 int len; 807 808 spin_lock(&proc_subdir_lock); 809 if (__xlate_proc_name(name, &parent, &fn) != 0) { 810 spin_unlock(&proc_subdir_lock); 811 return; 812 } 813 len = strlen(fn); 814 815 for (p = &parent->subdir; *p; p=&(*p)->next ) { 816 if (proc_match(len, fn, *p)) { 817 de = *p; 818 *p = de->next; 819 de->next = NULL; 820 break; 821 } 822 } 823 spin_unlock(&proc_subdir_lock); 824 if (!de) { 825 WARN(1, "name '%s'\n", name); 826 return; 827 } 828 829 spin_lock(&de->pde_unload_lock); 830 /* 831 * Stop accepting new callers into module. If you're 832 * dynamically allocating ->proc_fops, save a pointer somewhere. 833 */ 834 de->proc_fops = NULL; 835 /* Wait until all existing callers into module are done. */ 836 if (de->pde_users > 0) { 837 DECLARE_COMPLETION_ONSTACK(c); 838 839 if (!de->pde_unload_completion) 840 de->pde_unload_completion = &c; 841 842 spin_unlock(&de->pde_unload_lock); 843 844 wait_for_completion(de->pde_unload_completion); 845 846 goto continue_removing; 847 } 848 spin_unlock(&de->pde_unload_lock); 849 850 continue_removing: 851 spin_lock(&de->pde_unload_lock); 852 while (!list_empty(&de->pde_openers)) { 853 struct pde_opener *pdeo; 854 855 pdeo = list_first_entry(&de->pde_openers, struct pde_opener, lh); 856 list_del(&pdeo->lh); 857 spin_unlock(&de->pde_unload_lock); 858 pdeo->release(pdeo->inode, pdeo->file); 859 kfree(pdeo); 860 spin_lock(&de->pde_unload_lock); 861 } 862 spin_unlock(&de->pde_unload_lock); 863 864 if (S_ISDIR(de->mode)) 865 parent->nlink--; 866 de->nlink = 0; 867 WARN(de->subdir, KERN_WARNING "%s: removing non-empty directory " 868 "'%s/%s', leaking at least '%s'\n", __func__, 869 de->parent->name, de->name, de->subdir->name); 870 pde_put(de); 871 } 872 EXPORT_SYMBOL(remove_proc_entry); 873