Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * proc/fs/generic.c --- generic routines for the proc-fs
0004  *
0005  * This file contains generic proc-fs routines for handling
0006  * directories and files.
0007  * 
0008  * Copyright (C) 1991, 1992 Linus Torvalds.
0009  * Copyright (C) 1997 Theodore Ts'o
0010  */
0011 
0012 #include <linux/cache.h>
0013 #include <linux/errno.h>
0014 #include <linux/time.h>
0015 #include <linux/proc_fs.h>
0016 #include <linux/stat.h>
0017 #include <linux/mm.h>
0018 #include <linux/module.h>
0019 #include <linux/namei.h>
0020 #include <linux/slab.h>
0021 #include <linux/printk.h>
0022 #include <linux/mount.h>
0023 #include <linux/init.h>
0024 #include <linux/idr.h>
0025 #include <linux/bitops.h>
0026 #include <linux/spinlock.h>
0027 #include <linux/completion.h>
0028 #include <linux/uaccess.h>
0029 #include <linux/seq_file.h>
0030 
0031 #include "internal.h"
0032 
0033 static DEFINE_RWLOCK(proc_subdir_lock);
0034 
0035 struct kmem_cache *proc_dir_entry_cache __ro_after_init;
0036 
0037 void pde_free(struct proc_dir_entry *pde)
0038 {
0039     if (S_ISLNK(pde->mode))
0040         kfree(pde->data);
0041     if (pde->name != pde->inline_name)
0042         kfree(pde->name);
0043     kmem_cache_free(proc_dir_entry_cache, pde);
0044 }
0045 
0046 static int proc_match(const char *name, struct proc_dir_entry *de, unsigned int len)
0047 {
0048     if (len < de->namelen)
0049         return -1;
0050     if (len > de->namelen)
0051         return 1;
0052 
0053     return memcmp(name, de->name, len);
0054 }
0055 
0056 static struct proc_dir_entry *pde_subdir_first(struct proc_dir_entry *dir)
0057 {
0058     return rb_entry_safe(rb_first(&dir->subdir), struct proc_dir_entry,
0059                  subdir_node);
0060 }
0061 
0062 static struct proc_dir_entry *pde_subdir_next(struct proc_dir_entry *dir)
0063 {
0064     return rb_entry_safe(rb_next(&dir->subdir_node), struct proc_dir_entry,
0065                  subdir_node);
0066 }
0067 
0068 static struct proc_dir_entry *pde_subdir_find(struct proc_dir_entry *dir,
0069                           const char *name,
0070                           unsigned int len)
0071 {
0072     struct rb_node *node = dir->subdir.rb_node;
0073 
0074     while (node) {
0075         struct proc_dir_entry *de = rb_entry(node,
0076                              struct proc_dir_entry,
0077                              subdir_node);
0078         int result = proc_match(name, de, len);
0079 
0080         if (result < 0)
0081             node = node->rb_left;
0082         else if (result > 0)
0083             node = node->rb_right;
0084         else
0085             return de;
0086     }
0087     return NULL;
0088 }
0089 
0090 static bool pde_subdir_insert(struct proc_dir_entry *dir,
0091                   struct proc_dir_entry *de)
0092 {
0093     struct rb_root *root = &dir->subdir;
0094     struct rb_node **new = &root->rb_node, *parent = NULL;
0095 
0096     /* Figure out where to put new node */
0097     while (*new) {
0098         struct proc_dir_entry *this = rb_entry(*new,
0099                                struct proc_dir_entry,
0100                                subdir_node);
0101         int result = proc_match(de->name, this, de->namelen);
0102 
0103         parent = *new;
0104         if (result < 0)
0105             new = &(*new)->rb_left;
0106         else if (result > 0)
0107             new = &(*new)->rb_right;
0108         else
0109             return false;
0110     }
0111 
0112     /* Add new node and rebalance tree. */
0113     rb_link_node(&de->subdir_node, parent, new);
0114     rb_insert_color(&de->subdir_node, root);
0115     return true;
0116 }
0117 
0118 static int proc_notify_change(struct user_namespace *mnt_userns,
0119                   struct dentry *dentry, struct iattr *iattr)
0120 {
0121     struct inode *inode = d_inode(dentry);
0122     struct proc_dir_entry *de = PDE(inode);
0123     int error;
0124 
0125     error = setattr_prepare(&init_user_ns, dentry, iattr);
0126     if (error)
0127         return error;
0128 
0129     setattr_copy(&init_user_ns, inode, iattr);
0130     mark_inode_dirty(inode);
0131 
0132     proc_set_user(de, inode->i_uid, inode->i_gid);
0133     de->mode = inode->i_mode;
0134     return 0;
0135 }
0136 
0137 static int proc_getattr(struct user_namespace *mnt_userns,
0138             const struct path *path, struct kstat *stat,
0139             u32 request_mask, unsigned int query_flags)
0140 {
0141     struct inode *inode = d_inode(path->dentry);
0142     struct proc_dir_entry *de = PDE(inode);
0143     if (de) {
0144         nlink_t nlink = READ_ONCE(de->nlink);
0145         if (nlink > 0) {
0146             set_nlink(inode, nlink);
0147         }
0148     }
0149 
0150     generic_fillattr(&init_user_ns, inode, stat);
0151     return 0;
0152 }
0153 
0154 static const struct inode_operations proc_file_inode_operations = {
0155     .setattr    = proc_notify_change,
0156 };
0157 
0158 /*
0159  * This function parses a name such as "tty/driver/serial", and
0160  * returns the struct proc_dir_entry for "/proc/tty/driver", and
0161  * returns "serial" in residual.
0162  */
0163 static int __xlate_proc_name(const char *name, struct proc_dir_entry **ret,
0164                  const char **residual)
0165 {
0166     const char          *cp = name, *next;
0167     struct proc_dir_entry   *de;
0168 
0169     de = *ret ?: &proc_root;
0170     while ((next = strchr(cp, '/')) != NULL) {
0171         de = pde_subdir_find(de, cp, next - cp);
0172         if (!de) {
0173             WARN(1, "name '%s'\n", name);
0174             return -ENOENT;
0175         }
0176         cp = next + 1;
0177     }
0178     *residual = cp;
0179     *ret = de;
0180     return 0;
0181 }
0182 
0183 static int xlate_proc_name(const char *name, struct proc_dir_entry **ret,
0184                const char **residual)
0185 {
0186     int rv;
0187 
0188     read_lock(&proc_subdir_lock);
0189     rv = __xlate_proc_name(name, ret, residual);
0190     read_unlock(&proc_subdir_lock);
0191     return rv;
0192 }
0193 
0194 static DEFINE_IDA(proc_inum_ida);
0195 
0196 #define PROC_DYNAMIC_FIRST 0xF0000000U
0197 
0198 /*
0199  * Return an inode number between PROC_DYNAMIC_FIRST and
0200  * 0xffffffff, or zero on failure.
0201  */
0202 int proc_alloc_inum(unsigned int *inum)
0203 {
0204     int i;
0205 
0206     i = ida_simple_get(&proc_inum_ida, 0, UINT_MAX - PROC_DYNAMIC_FIRST + 1,
0207                GFP_KERNEL);
0208     if (i < 0)
0209         return i;
0210 
0211     *inum = PROC_DYNAMIC_FIRST + (unsigned int)i;
0212     return 0;
0213 }
0214 
0215 void proc_free_inum(unsigned int inum)
0216 {
0217     ida_simple_remove(&proc_inum_ida, inum - PROC_DYNAMIC_FIRST);
0218 }
0219 
0220 static int proc_misc_d_revalidate(struct dentry *dentry, unsigned int flags)
0221 {
0222     if (flags & LOOKUP_RCU)
0223         return -ECHILD;
0224 
0225     if (atomic_read(&PDE(d_inode(dentry))->in_use) < 0)
0226         return 0; /* revalidate */
0227     return 1;
0228 }
0229 
0230 static int proc_misc_d_delete(const struct dentry *dentry)
0231 {
0232     return atomic_read(&PDE(d_inode(dentry))->in_use) < 0;
0233 }
0234 
0235 static const struct dentry_operations proc_misc_dentry_ops = {
0236     .d_revalidate   = proc_misc_d_revalidate,
0237     .d_delete   = proc_misc_d_delete,
0238 };
0239 
0240 /*
0241  * Don't create negative dentries here, return -ENOENT by hand
0242  * instead.
0243  */
0244 struct dentry *proc_lookup_de(struct inode *dir, struct dentry *dentry,
0245                   struct proc_dir_entry *de)
0246 {
0247     struct inode *inode;
0248 
0249     read_lock(&proc_subdir_lock);
0250     de = pde_subdir_find(de, dentry->d_name.name, dentry->d_name.len);
0251     if (de) {
0252         pde_get(de);
0253         read_unlock(&proc_subdir_lock);
0254         inode = proc_get_inode(dir->i_sb, de);
0255         if (!inode)
0256             return ERR_PTR(-ENOMEM);
0257         d_set_d_op(dentry, de->proc_dops);
0258         return d_splice_alias(inode, dentry);
0259     }
0260     read_unlock(&proc_subdir_lock);
0261     return ERR_PTR(-ENOENT);
0262 }
0263 
0264 struct dentry *proc_lookup(struct inode *dir, struct dentry *dentry,
0265         unsigned int flags)
0266 {
0267     struct proc_fs_info *fs_info = proc_sb_info(dir->i_sb);
0268 
0269     if (fs_info->pidonly == PROC_PIDONLY_ON)
0270         return ERR_PTR(-ENOENT);
0271 
0272     return proc_lookup_de(dir, dentry, PDE(dir));
0273 }
0274 
0275 /*
0276  * This returns non-zero if at EOF, so that the /proc
0277  * root directory can use this and check if it should
0278  * continue with the <pid> entries..
0279  *
0280  * Note that the VFS-layer doesn't care about the return
0281  * value of the readdir() call, as long as it's non-negative
0282  * for success..
0283  */
0284 int proc_readdir_de(struct file *file, struct dir_context *ctx,
0285             struct proc_dir_entry *de)
0286 {
0287     int i;
0288 
0289     if (!dir_emit_dots(file, ctx))
0290         return 0;
0291 
0292     i = ctx->pos - 2;
0293     read_lock(&proc_subdir_lock);
0294     de = pde_subdir_first(de);
0295     for (;;) {
0296         if (!de) {
0297             read_unlock(&proc_subdir_lock);
0298             return 0;
0299         }
0300         if (!i)
0301             break;
0302         de = pde_subdir_next(de);
0303         i--;
0304     }
0305 
0306     do {
0307         struct proc_dir_entry *next;
0308         pde_get(de);
0309         read_unlock(&proc_subdir_lock);
0310         if (!dir_emit(ctx, de->name, de->namelen,
0311                 de->low_ino, de->mode >> 12)) {
0312             pde_put(de);
0313             return 0;
0314         }
0315         ctx->pos++;
0316         read_lock(&proc_subdir_lock);
0317         next = pde_subdir_next(de);
0318         pde_put(de);
0319         de = next;
0320     } while (de);
0321     read_unlock(&proc_subdir_lock);
0322     return 1;
0323 }
0324 
0325 int proc_readdir(struct file *file, struct dir_context *ctx)
0326 {
0327     struct inode *inode = file_inode(file);
0328     struct proc_fs_info *fs_info = proc_sb_info(inode->i_sb);
0329 
0330     if (fs_info->pidonly == PROC_PIDONLY_ON)
0331         return 1;
0332 
0333     return proc_readdir_de(file, ctx, PDE(inode));
0334 }
0335 
0336 /*
0337  * These are the generic /proc directory operations. They
0338  * use the in-memory "struct proc_dir_entry" tree to parse
0339  * the /proc directory.
0340  */
0341 static const struct file_operations proc_dir_operations = {
0342     .llseek         = generic_file_llseek,
0343     .read           = generic_read_dir,
0344     .iterate_shared     = proc_readdir,
0345 };
0346 
0347 static int proc_net_d_revalidate(struct dentry *dentry, unsigned int flags)
0348 {
0349     return 0;
0350 }
0351 
0352 const struct dentry_operations proc_net_dentry_ops = {
0353     .d_revalidate   = proc_net_d_revalidate,
0354     .d_delete   = always_delete_dentry,
0355 };
0356 
0357 /*
0358  * proc directories can do almost nothing..
0359  */
0360 static const struct inode_operations proc_dir_inode_operations = {
0361     .lookup     = proc_lookup,
0362     .getattr    = proc_getattr,
0363     .setattr    = proc_notify_change,
0364 };
0365 
0366 /* returns the registered entry, or frees dp and returns NULL on failure */
0367 struct proc_dir_entry *proc_register(struct proc_dir_entry *dir,
0368         struct proc_dir_entry *dp)
0369 {
0370     if (proc_alloc_inum(&dp->low_ino))
0371         goto out_free_entry;
0372 
0373     write_lock(&proc_subdir_lock);
0374     dp->parent = dir;
0375     if (pde_subdir_insert(dir, dp) == false) {
0376         WARN(1, "proc_dir_entry '%s/%s' already registered\n",
0377              dir->name, dp->name);
0378         write_unlock(&proc_subdir_lock);
0379         goto out_free_inum;
0380     }
0381     dir->nlink++;
0382     write_unlock(&proc_subdir_lock);
0383 
0384     return dp;
0385 out_free_inum:
0386     proc_free_inum(dp->low_ino);
0387 out_free_entry:
0388     pde_free(dp);
0389     return NULL;
0390 }
0391 
0392 static struct proc_dir_entry *__proc_create(struct proc_dir_entry **parent,
0393                       const char *name,
0394                       umode_t mode,
0395                       nlink_t nlink)
0396 {
0397     struct proc_dir_entry *ent = NULL;
0398     const char *fn;
0399     struct qstr qstr;
0400 
0401     if (xlate_proc_name(name, parent, &fn) != 0)
0402         goto out;
0403     qstr.name = fn;
0404     qstr.len = strlen(fn);
0405     if (qstr.len == 0 || qstr.len >= 256) {
0406         WARN(1, "name len %u\n", qstr.len);
0407         return NULL;
0408     }
0409     if (qstr.len == 1 && fn[0] == '.') {
0410         WARN(1, "name '.'\n");
0411         return NULL;
0412     }
0413     if (qstr.len == 2 && fn[0] == '.' && fn[1] == '.') {
0414         WARN(1, "name '..'\n");
0415         return NULL;
0416     }
0417     if (*parent == &proc_root && name_to_int(&qstr) != ~0U) {
0418         WARN(1, "create '/proc/%s' by hand\n", qstr.name);
0419         return NULL;
0420     }
0421     if (is_empty_pde(*parent)) {
0422         WARN(1, "attempt to add to permanently empty directory");
0423         return NULL;
0424     }
0425 
0426     ent = kmem_cache_zalloc(proc_dir_entry_cache, GFP_KERNEL);
0427     if (!ent)
0428         goto out;
0429 
0430     if (qstr.len + 1 <= SIZEOF_PDE_INLINE_NAME) {
0431         ent->name = ent->inline_name;
0432     } else {
0433         ent->name = kmalloc(qstr.len + 1, GFP_KERNEL);
0434         if (!ent->name) {
0435             pde_free(ent);
0436             return NULL;
0437         }
0438     }
0439 
0440     memcpy(ent->name, fn, qstr.len + 1);
0441     ent->namelen = qstr.len;
0442     ent->mode = mode;
0443     ent->nlink = nlink;
0444     ent->subdir = RB_ROOT;
0445     refcount_set(&ent->refcnt, 1);
0446     spin_lock_init(&ent->pde_unload_lock);
0447     INIT_LIST_HEAD(&ent->pde_openers);
0448     proc_set_user(ent, (*parent)->uid, (*parent)->gid);
0449 
0450     ent->proc_dops = &proc_misc_dentry_ops;
0451     /* Revalidate everything under /proc/${pid}/net */
0452     if ((*parent)->proc_dops == &proc_net_dentry_ops)
0453         pde_force_lookup(ent);
0454 
0455 out:
0456     return ent;
0457 }
0458 
0459 struct proc_dir_entry *proc_symlink(const char *name,
0460         struct proc_dir_entry *parent, const char *dest)
0461 {
0462     struct proc_dir_entry *ent;
0463 
0464     ent = __proc_create(&parent, name,
0465               (S_IFLNK | S_IRUGO | S_IWUGO | S_IXUGO),1);
0466 
0467     if (ent) {
0468         ent->data = kmalloc((ent->size=strlen(dest))+1, GFP_KERNEL);
0469         if (ent->data) {
0470             strcpy((char*)ent->data,dest);
0471             ent->proc_iops = &proc_link_inode_operations;
0472             ent = proc_register(parent, ent);
0473         } else {
0474             pde_free(ent);
0475             ent = NULL;
0476         }
0477     }
0478     return ent;
0479 }
0480 EXPORT_SYMBOL(proc_symlink);
0481 
0482 struct proc_dir_entry *_proc_mkdir(const char *name, umode_t mode,
0483         struct proc_dir_entry *parent, void *data, bool force_lookup)
0484 {
0485     struct proc_dir_entry *ent;
0486 
0487     if (mode == 0)
0488         mode = S_IRUGO | S_IXUGO;
0489 
0490     ent = __proc_create(&parent, name, S_IFDIR | mode, 2);
0491     if (ent) {
0492         ent->data = data;
0493         ent->proc_dir_ops = &proc_dir_operations;
0494         ent->proc_iops = &proc_dir_inode_operations;
0495         if (force_lookup) {
0496             pde_force_lookup(ent);
0497         }
0498         ent = proc_register(parent, ent);
0499     }
0500     return ent;
0501 }
0502 EXPORT_SYMBOL_GPL(_proc_mkdir);
0503 
0504 struct proc_dir_entry *proc_mkdir_data(const char *name, umode_t mode,
0505         struct proc_dir_entry *parent, void *data)
0506 {
0507     return _proc_mkdir(name, mode, parent, data, false);
0508 }
0509 EXPORT_SYMBOL_GPL(proc_mkdir_data);
0510 
0511 struct proc_dir_entry *proc_mkdir_mode(const char *name, umode_t mode,
0512                        struct proc_dir_entry *parent)
0513 {
0514     return proc_mkdir_data(name, mode, parent, NULL);
0515 }
0516 EXPORT_SYMBOL(proc_mkdir_mode);
0517 
0518 struct proc_dir_entry *proc_mkdir(const char *name,
0519         struct proc_dir_entry *parent)
0520 {
0521     return proc_mkdir_data(name, 0, parent, NULL);
0522 }
0523 EXPORT_SYMBOL(proc_mkdir);
0524 
0525 struct proc_dir_entry *proc_create_mount_point(const char *name)
0526 {
0527     umode_t mode = S_IFDIR | S_IRUGO | S_IXUGO;
0528     struct proc_dir_entry *ent, *parent = NULL;
0529 
0530     ent = __proc_create(&parent, name, mode, 2);
0531     if (ent) {
0532         ent->data = NULL;
0533         ent->proc_dir_ops = NULL;
0534         ent->proc_iops = NULL;
0535         ent = proc_register(parent, ent);
0536     }
0537     return ent;
0538 }
0539 EXPORT_SYMBOL(proc_create_mount_point);
0540 
0541 struct proc_dir_entry *proc_create_reg(const char *name, umode_t mode,
0542         struct proc_dir_entry **parent, void *data)
0543 {
0544     struct proc_dir_entry *p;
0545 
0546     if ((mode & S_IFMT) == 0)
0547         mode |= S_IFREG;
0548     if ((mode & S_IALLUGO) == 0)
0549         mode |= S_IRUGO;
0550     if (WARN_ON_ONCE(!S_ISREG(mode)))
0551         return NULL;
0552 
0553     p = __proc_create(parent, name, mode, 1);
0554     if (p) {
0555         p->proc_iops = &proc_file_inode_operations;
0556         p->data = data;
0557     }
0558     return p;
0559 }
0560 
0561 static inline void pde_set_flags(struct proc_dir_entry *pde)
0562 {
0563     if (pde->proc_ops->proc_flags & PROC_ENTRY_PERMANENT)
0564         pde->flags |= PROC_ENTRY_PERMANENT;
0565 }
0566 
0567 struct proc_dir_entry *proc_create_data(const char *name, umode_t mode,
0568         struct proc_dir_entry *parent,
0569         const struct proc_ops *proc_ops, void *data)
0570 {
0571     struct proc_dir_entry *p;
0572 
0573     p = proc_create_reg(name, mode, &parent, data);
0574     if (!p)
0575         return NULL;
0576     p->proc_ops = proc_ops;
0577     pde_set_flags(p);
0578     return proc_register(parent, p);
0579 }
0580 EXPORT_SYMBOL(proc_create_data);
0581  
0582 struct proc_dir_entry *proc_create(const char *name, umode_t mode,
0583                    struct proc_dir_entry *parent,
0584                    const struct proc_ops *proc_ops)
0585 {
0586     return proc_create_data(name, mode, parent, proc_ops, NULL);
0587 }
0588 EXPORT_SYMBOL(proc_create);
0589 
0590 static int proc_seq_open(struct inode *inode, struct file *file)
0591 {
0592     struct proc_dir_entry *de = PDE(inode);
0593 
0594     if (de->state_size)
0595         return seq_open_private(file, de->seq_ops, de->state_size);
0596     return seq_open(file, de->seq_ops);
0597 }
0598 
0599 static int proc_seq_release(struct inode *inode, struct file *file)
0600 {
0601     struct proc_dir_entry *de = PDE(inode);
0602 
0603     if (de->state_size)
0604         return seq_release_private(inode, file);
0605     return seq_release(inode, file);
0606 }
0607 
0608 static const struct proc_ops proc_seq_ops = {
0609     /* not permanent -- can call into arbitrary seq_operations */
0610     .proc_open  = proc_seq_open,
0611     .proc_read_iter = seq_read_iter,
0612     .proc_lseek = seq_lseek,
0613     .proc_release   = proc_seq_release,
0614 };
0615 
0616 struct proc_dir_entry *proc_create_seq_private(const char *name, umode_t mode,
0617         struct proc_dir_entry *parent, const struct seq_operations *ops,
0618         unsigned int state_size, void *data)
0619 {
0620     struct proc_dir_entry *p;
0621 
0622     p = proc_create_reg(name, mode, &parent, data);
0623     if (!p)
0624         return NULL;
0625     p->proc_ops = &proc_seq_ops;
0626     p->seq_ops = ops;
0627     p->state_size = state_size;
0628     return proc_register(parent, p);
0629 }
0630 EXPORT_SYMBOL(proc_create_seq_private);
0631 
0632 static int proc_single_open(struct inode *inode, struct file *file)
0633 {
0634     struct proc_dir_entry *de = PDE(inode);
0635 
0636     return single_open(file, de->single_show, de->data);
0637 }
0638 
0639 static const struct proc_ops proc_single_ops = {
0640     /* not permanent -- can call into arbitrary ->single_show */
0641     .proc_open  = proc_single_open,
0642     .proc_read_iter = seq_read_iter,
0643     .proc_lseek = seq_lseek,
0644     .proc_release   = single_release,
0645 };
0646 
0647 struct proc_dir_entry *proc_create_single_data(const char *name, umode_t mode,
0648         struct proc_dir_entry *parent,
0649         int (*show)(struct seq_file *, void *), void *data)
0650 {
0651     struct proc_dir_entry *p;
0652 
0653     p = proc_create_reg(name, mode, &parent, data);
0654     if (!p)
0655         return NULL;
0656     p->proc_ops = &proc_single_ops;
0657     p->single_show = show;
0658     return proc_register(parent, p);
0659 }
0660 EXPORT_SYMBOL(proc_create_single_data);
0661 
0662 void proc_set_size(struct proc_dir_entry *de, loff_t size)
0663 {
0664     de->size = size;
0665 }
0666 EXPORT_SYMBOL(proc_set_size);
0667 
0668 void proc_set_user(struct proc_dir_entry *de, kuid_t uid, kgid_t gid)
0669 {
0670     de->uid = uid;
0671     de->gid = gid;
0672 }
0673 EXPORT_SYMBOL(proc_set_user);
0674 
0675 void pde_put(struct proc_dir_entry *pde)
0676 {
0677     if (refcount_dec_and_test(&pde->refcnt)) {
0678         proc_free_inum(pde->low_ino);
0679         pde_free(pde);
0680     }
0681 }
0682 
0683 /*
0684  * Remove a /proc entry and free it if it's not currently in use.
0685  */
0686 void remove_proc_entry(const char *name, struct proc_dir_entry *parent)
0687 {
0688     struct proc_dir_entry *de = NULL;
0689     const char *fn = name;
0690     unsigned int len;
0691 
0692     write_lock(&proc_subdir_lock);
0693     if (__xlate_proc_name(name, &parent, &fn) != 0) {
0694         write_unlock(&proc_subdir_lock);
0695         return;
0696     }
0697     len = strlen(fn);
0698 
0699     de = pde_subdir_find(parent, fn, len);
0700     if (de) {
0701         if (unlikely(pde_is_permanent(de))) {
0702             WARN(1, "removing permanent /proc entry '%s'", de->name);
0703             de = NULL;
0704         } else {
0705             rb_erase(&de->subdir_node, &parent->subdir);
0706             if (S_ISDIR(de->mode))
0707                 parent->nlink--;
0708         }
0709     }
0710     write_unlock(&proc_subdir_lock);
0711     if (!de) {
0712         WARN(1, "name '%s'\n", name);
0713         return;
0714     }
0715 
0716     proc_entry_rundown(de);
0717 
0718     WARN(pde_subdir_first(de),
0719          "%s: removing non-empty directory '%s/%s', leaking at least '%s'\n",
0720          __func__, de->parent->name, de->name, pde_subdir_first(de)->name);
0721     pde_put(de);
0722 }
0723 EXPORT_SYMBOL(remove_proc_entry);
0724 
0725 int remove_proc_subtree(const char *name, struct proc_dir_entry *parent)
0726 {
0727     struct proc_dir_entry *root = NULL, *de, *next;
0728     const char *fn = name;
0729     unsigned int len;
0730 
0731     write_lock(&proc_subdir_lock);
0732     if (__xlate_proc_name(name, &parent, &fn) != 0) {
0733         write_unlock(&proc_subdir_lock);
0734         return -ENOENT;
0735     }
0736     len = strlen(fn);
0737 
0738     root = pde_subdir_find(parent, fn, len);
0739     if (!root) {
0740         write_unlock(&proc_subdir_lock);
0741         return -ENOENT;
0742     }
0743     if (unlikely(pde_is_permanent(root))) {
0744         write_unlock(&proc_subdir_lock);
0745         WARN(1, "removing permanent /proc entry '%s/%s'",
0746             root->parent->name, root->name);
0747         return -EINVAL;
0748     }
0749     rb_erase(&root->subdir_node, &parent->subdir);
0750 
0751     de = root;
0752     while (1) {
0753         next = pde_subdir_first(de);
0754         if (next) {
0755             if (unlikely(pde_is_permanent(next))) {
0756                 write_unlock(&proc_subdir_lock);
0757                 WARN(1, "removing permanent /proc entry '%s/%s'",
0758                     next->parent->name, next->name);
0759                 return -EINVAL;
0760             }
0761             rb_erase(&next->subdir_node, &de->subdir);
0762             de = next;
0763             continue;
0764         }
0765         next = de->parent;
0766         if (S_ISDIR(de->mode))
0767             next->nlink--;
0768         write_unlock(&proc_subdir_lock);
0769 
0770         proc_entry_rundown(de);
0771         if (de == root)
0772             break;
0773         pde_put(de);
0774 
0775         write_lock(&proc_subdir_lock);
0776         de = next;
0777     }
0778     pde_put(root);
0779     return 0;
0780 }
0781 EXPORT_SYMBOL(remove_proc_subtree);
0782 
0783 void *proc_get_parent_data(const struct inode *inode)
0784 {
0785     struct proc_dir_entry *de = PDE(inode);
0786     return de->parent->data;
0787 }
0788 EXPORT_SYMBOL_GPL(proc_get_parent_data);
0789 
0790 void proc_remove(struct proc_dir_entry *de)
0791 {
0792     if (de)
0793         remove_proc_subtree(de->name, de->parent);
0794 }
0795 EXPORT_SYMBOL(proc_remove);
0796 
0797 /*
0798  * Pull a user buffer into memory and pass it to the file's write handler if
0799  * one is supplied.  The ->write() method is permitted to modify the
0800  * kernel-side buffer.
0801  */
0802 ssize_t proc_simple_write(struct file *f, const char __user *ubuf, size_t size,
0803               loff_t *_pos)
0804 {
0805     struct proc_dir_entry *pde = PDE(file_inode(f));
0806     char *buf;
0807     int ret;
0808 
0809     if (!pde->write)
0810         return -EACCES;
0811     if (size == 0 || size > PAGE_SIZE - 1)
0812         return -EINVAL;
0813     buf = memdup_user_nul(ubuf, size);
0814     if (IS_ERR(buf))
0815         return PTR_ERR(buf);
0816     ret = pde->write(f, buf, size);
0817     kfree(buf);
0818     return ret == 0 ? size : ret;
0819 }