linux/fs/proc/inode.c
<<
>>
Prefs
   1/*
   2 *  linux/fs/proc/inode.c
   3 *
   4 *  Copyright (C) 1991, 1992  Linus Torvalds
   5 */
   6
   7#include <linux/time.h>
   8#include <linux/proc_fs.h>
   9#include <linux/kernel.h>
  10#include <linux/pid_namespace.h>
  11#include <linux/mm.h>
  12#include <linux/string.h>
  13#include <linux/stat.h>
  14#include <linux/completion.h>
  15#include <linux/poll.h>
  16#include <linux/printk.h>
  17#include <linux/file.h>
  18#include <linux/limits.h>
  19#include <linux/init.h>
  20#include <linux/module.h>
  21#include <linux/sysctl.h>
  22#include <linux/seq_file.h>
  23#include <linux/slab.h>
  24#include <linux/mount.h>
  25#include <linux/magic.h>
  26
  27#include <asm/uaccess.h>
  28
  29#include "internal.h"
  30
  31static void proc_evict_inode(struct inode *inode)
  32{
  33        struct proc_dir_entry *de;
  34        struct ctl_table_header *head;
  35        const struct proc_ns_operations *ns_ops;
  36        void *ns;
  37
  38        truncate_inode_pages_final(&inode->i_data);
  39        clear_inode(inode);
  40
  41        /* Stop tracking associated processes */
  42        put_pid(PROC_I(inode)->pid);
  43
  44        /* Let go of any associated proc directory entry */
  45        de = PROC_I(inode)->pde;
  46        if (de)
  47                pde_put(de);
  48
  49        head = PROC_I(inode)->sysctl;
  50        if (head) {
  51                rcu_assign_pointer(PROC_I(inode)->sysctl, NULL);
  52                proc_sys_evict_inode(inode, head);
  53        }
  54        /* Release any associated namespace */
  55        ns_ops = PROC_I(inode)->ns.ns_ops;
  56        ns = PROC_I(inode)->ns.ns;
  57        if (ns_ops && ns)
  58                ns_ops->put(ns);
  59}
  60
  61static struct kmem_cache * proc_inode_cachep;
  62
  63static struct inode *proc_alloc_inode(struct super_block *sb)
  64{
  65        struct proc_inode *ei;
  66        struct inode *inode;
  67
  68        ei = (struct proc_inode *)kmem_cache_alloc(proc_inode_cachep, GFP_KERNEL);
  69        if (!ei)
  70                return NULL;
  71        ei->pid = NULL;
  72        ei->fd = 0;
  73        ei->op.proc_get_link = NULL;
  74        ei->pde = NULL;
  75        ei->sysctl = NULL;
  76        ei->sysctl_entry = NULL;
  77        ei->ns.ns = NULL;
  78        ei->ns.ns_ops = NULL;
  79        inode = &ei->vfs_inode;
  80        inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  81        return inode;
  82}
  83
  84static void proc_i_callback(struct rcu_head *head)
  85{
  86        struct inode *inode = container_of(head, struct inode, i_rcu);
  87        kmem_cache_free(proc_inode_cachep, PROC_I(inode));
  88}
  89
  90static void proc_destroy_inode(struct inode *inode)
  91{
  92        call_rcu(&inode->i_rcu, proc_i_callback);
  93}
  94
  95static void init_once(void *foo)
  96{
  97        struct proc_inode *ei = (struct proc_inode *) foo;
  98
  99        inode_init_once(&ei->vfs_inode);
 100}
 101
 102void __init proc_init_inodecache(void)
 103{
 104        proc_inode_cachep = kmem_cache_create("proc_inode_cache",
 105                                             sizeof(struct proc_inode),
 106                                             0, (SLAB_RECLAIM_ACCOUNT|
 107                                                SLAB_MEM_SPREAD|SLAB_ACCOUNT|
 108                                                SLAB_PANIC),
 109                                             init_once);
 110}
 111
 112static int proc_show_options(struct seq_file *seq, struct dentry *root)
 113{
 114        struct super_block *sb = root->d_sb;
 115        struct pid_namespace *pid = sb->s_fs_info;
 116
 117        if (!gid_eq(pid->pid_gid, GLOBAL_ROOT_GID))
 118                seq_printf(seq, ",gid=%u", from_kgid_munged(&init_user_ns, pid->pid_gid));
 119        if (pid->hide_pid != 0)
 120                seq_printf(seq, ",hidepid=%u", pid->hide_pid);
 121
 122        return 0;
 123}
 124
 125static const struct super_operations proc_sops = {
 126        .alloc_inode    = proc_alloc_inode,
 127        .destroy_inode  = proc_destroy_inode,
 128        .drop_inode     = generic_delete_inode,
 129        .evict_inode    = proc_evict_inode,
 130        .statfs         = simple_statfs,
 131        .remount_fs     = proc_remount,
 132        .show_options   = proc_show_options,
 133};
 134
 135enum {BIAS = -1U<<31};
 136
 137static inline int use_pde(struct proc_dir_entry *pde)
 138{
 139        return atomic_inc_unless_negative(&pde->in_use);
 140}
 141
 142static void unuse_pde(struct proc_dir_entry *pde)
 143{
 144        if (atomic_dec_return(&pde->in_use) == BIAS)
 145                complete(pde->pde_unload_completion);
 146}
 147
 148/* pde is locked */
 149static void close_pdeo(struct proc_dir_entry *pde, struct pde_opener *pdeo)
 150{
 151        if (pdeo->closing) {
 152                /* somebody else is doing that, just wait */
 153                DECLARE_COMPLETION_ONSTACK(c);
 154                pdeo->c = &c;
 155                spin_unlock(&pde->pde_unload_lock);
 156                wait_for_completion(&c);
 157                spin_lock(&pde->pde_unload_lock);
 158        } else {
 159                struct file *file;
 160                pdeo->closing = 1;
 161                spin_unlock(&pde->pde_unload_lock);
 162                file = pdeo->file;
 163                pde->proc_fops->release(file_inode(file), file);
 164                spin_lock(&pde->pde_unload_lock);
 165                list_del_init(&pdeo->lh);
 166                if (pdeo->c)
 167                        complete(pdeo->c);
 168                kfree(pdeo);
 169        }
 170}
 171
 172void proc_entry_rundown(struct proc_dir_entry *de)
 173{
 174        DECLARE_COMPLETION_ONSTACK(c);
 175        /* Wait until all existing callers into module are done. */
 176        de->pde_unload_completion = &c;
 177        if (atomic_add_return(BIAS, &de->in_use) != BIAS)
 178                wait_for_completion(&c);
 179
 180        spin_lock(&de->pde_unload_lock);
 181        while (!list_empty(&de->pde_openers)) {
 182                struct pde_opener *pdeo;
 183                pdeo = list_first_entry(&de->pde_openers, struct pde_opener, lh);
 184                close_pdeo(de, pdeo);
 185        }
 186        spin_unlock(&de->pde_unload_lock);
 187}
 188
 189static loff_t proc_reg_llseek(struct file *file, loff_t offset, int whence)
 190{
 191        struct proc_dir_entry *pde = PDE(file_inode(file));
 192        loff_t rv = -EINVAL;
 193        if (use_pde(pde)) {
 194                loff_t (*llseek)(struct file *, loff_t, int);
 195                llseek = pde->proc_fops->llseek;
 196                if (!llseek)
 197                        llseek = default_llseek;
 198                rv = llseek(file, offset, whence);
 199                unuse_pde(pde);
 200        }
 201        return rv;
 202}
 203
 204static ssize_t proc_reg_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
 205{
 206        ssize_t (*read)(struct file *, char __user *, size_t, loff_t *);
 207        struct proc_dir_entry *pde = PDE(file_inode(file));
 208        ssize_t rv = -EIO;
 209        if (use_pde(pde)) {
 210                read = pde->proc_fops->read;
 211                if (read)
 212                        rv = read(file, buf, count, ppos);
 213                unuse_pde(pde);
 214        }
 215        return rv;
 216}
 217
 218static ssize_t proc_reg_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
 219{
 220        ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *);
 221        struct proc_dir_entry *pde = PDE(file_inode(file));
 222        ssize_t rv = -EIO;
 223        if (use_pde(pde)) {
 224                write = pde->proc_fops->write;
 225                if (write)
 226                        rv = write(file, buf, count, ppos);
 227                unuse_pde(pde);
 228        }
 229        return rv;
 230}
 231
 232static unsigned int proc_reg_poll(struct file *file, struct poll_table_struct *pts)
 233{
 234        struct proc_dir_entry *pde = PDE(file_inode(file));
 235        unsigned int rv = DEFAULT_POLLMASK;
 236        unsigned int (*poll)(struct file *, struct poll_table_struct *);
 237        if (use_pde(pde)) {
 238                poll = pde->proc_fops->poll;
 239                if (poll)
 240                        rv = poll(file, pts);
 241                unuse_pde(pde);
 242        }
 243        return rv;
 244}
 245
 246static long proc_reg_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 247{
 248        struct proc_dir_entry *pde = PDE(file_inode(file));
 249        long rv = -ENOTTY;
 250        long (*ioctl)(struct file *, unsigned int, unsigned long);
 251        if (use_pde(pde)) {
 252                ioctl = pde->proc_fops->unlocked_ioctl;
 253                if (ioctl)
 254                        rv = ioctl(file, cmd, arg);
 255                unuse_pde(pde);
 256        }
 257        return rv;
 258}
 259
 260#ifdef CONFIG_COMPAT
 261static long proc_reg_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 262{
 263        struct proc_dir_entry *pde = PDE(file_inode(file));
 264        long rv = -ENOTTY;
 265        long (*compat_ioctl)(struct file *, unsigned int, unsigned long);
 266        if (use_pde(pde)) {
 267                compat_ioctl = pde->proc_fops->compat_ioctl;
 268                if (compat_ioctl)
 269                        rv = compat_ioctl(file, cmd, arg);
 270                unuse_pde(pde);
 271        }
 272        return rv;
 273}
 274#endif
 275
 276static int proc_reg_mmap(struct file *file, struct vm_area_struct *vma)
 277{
 278        struct proc_dir_entry *pde = PDE(file_inode(file));
 279        int rv = -EIO;
 280        int (*mmap)(struct file *, struct vm_area_struct *);
 281        if (use_pde(pde)) {
 282                mmap = pde->proc_fops->mmap;
 283                if (mmap)
 284                        rv = mmap(file, vma);
 285                unuse_pde(pde);
 286        }
 287        return rv;
 288}
 289
 290static int proc_reg_open(struct inode *inode, struct file *file)
 291{
 292        struct proc_dir_entry *pde = PDE(inode);
 293        int rv = 0;
 294        int (*open)(struct inode *, struct file *);
 295        int (*release)(struct inode *, struct file *);
 296        struct pde_opener *pdeo;
 297
 298        /*
 299         * What for, you ask? Well, we can have open, rmmod, remove_proc_entry
 300         * sequence. ->release won't be called because ->proc_fops will be
 301         * cleared. Depending on complexity of ->release, consequences vary.
 302         *
 303         * We can't wait for mercy when close will be done for real, it's
 304         * deadlockable: rmmod foo </proc/foo . So, we're going to do ->release
 305         * by hand in remove_proc_entry(). For this, save opener's credentials
 306         * for later.
 307         */
 308        pdeo = kzalloc(sizeof(struct pde_opener), GFP_KERNEL);
 309        if (!pdeo)
 310                return -ENOMEM;
 311
 312        if (!use_pde(pde)) {
 313                kfree(pdeo);
 314                return -ENOENT;
 315        }
 316        open = pde->proc_fops->open;
 317        release = pde->proc_fops->release;
 318
 319        if (open)
 320                rv = open(inode, file);
 321
 322        if (rv == 0 && release) {
 323                /* To know what to release. */
 324                pdeo->file = file;
 325                /* Strictly for "too late" ->release in proc_reg_release(). */
 326                spin_lock(&pde->pde_unload_lock);
 327                list_add(&pdeo->lh, &pde->pde_openers);
 328                spin_unlock(&pde->pde_unload_lock);
 329        } else
 330                kfree(pdeo);
 331
 332        unuse_pde(pde);
 333        return rv;
 334}
 335
 336static int proc_reg_release(struct inode *inode, struct file *file)
 337{
 338        struct proc_dir_entry *pde = PDE(inode);
 339        struct pde_opener *pdeo;
 340        spin_lock(&pde->pde_unload_lock);
 341        list_for_each_entry(pdeo, &pde->pde_openers, lh) {
 342                if (pdeo->file == file) {
 343                        close_pdeo(pde, pdeo);
 344                        break;
 345                }
 346        }
 347        spin_unlock(&pde->pde_unload_lock);
 348        return 0;
 349}
 350
 351static const struct file_operations proc_reg_file_ops = {
 352        .llseek         = proc_reg_llseek,
 353        .read           = proc_reg_read,
 354        .write          = proc_reg_write,
 355        .poll           = proc_reg_poll,
 356        .unlocked_ioctl = proc_reg_unlocked_ioctl,
 357#ifdef CONFIG_COMPAT
 358        .compat_ioctl   = proc_reg_compat_ioctl,
 359#endif
 360        .mmap           = proc_reg_mmap,
 361        .open           = proc_reg_open,
 362        .release        = proc_reg_release,
 363};
 364
 365#ifdef CONFIG_COMPAT
 366static const struct file_operations proc_reg_file_ops_no_compat = {
 367        .llseek         = proc_reg_llseek,
 368        .read           = proc_reg_read,
 369        .write          = proc_reg_write,
 370        .poll           = proc_reg_poll,
 371        .unlocked_ioctl = proc_reg_unlocked_ioctl,
 372        .mmap           = proc_reg_mmap,
 373        .open           = proc_reg_open,
 374        .release        = proc_reg_release,
 375};
 376#endif
 377
 378struct inode *proc_get_inode(struct super_block *sb, struct proc_dir_entry *de)
 379{
 380        struct inode *inode = new_inode_pseudo(sb);
 381
 382        if (inode) {
 383                inode->i_ino = de->low_ino;
 384                inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
 385                PROC_I(inode)->pde = de;
 386
 387                if (is_empty_pde(de)) {
 388                        make_empty_dir_inode(inode);
 389                        return inode;
 390                }
 391                if (de->mode) {
 392                        inode->i_mode = de->mode;
 393                        inode->i_uid = de->uid;
 394                        inode->i_gid = de->gid;
 395                }
 396                if (de->size)
 397                        inode->i_size = de->size;
 398                if (de->nlink)
 399                        set_nlink(inode, de->nlink);
 400                WARN_ON(!de->proc_iops);
 401                inode->i_op = de->proc_iops;
 402                if (de->proc_fops) {
 403                        if (S_ISREG(inode->i_mode)) {
 404#ifdef CONFIG_COMPAT
 405                                if (!de->proc_fops->compat_ioctl)
 406                                        inode->i_fop =
 407                                                &proc_reg_file_ops_no_compat;
 408                                else
 409#endif
 410                                        inode->i_fop = &proc_reg_file_ops;
 411                        } else {
 412                                inode->i_fop = de->proc_fops;
 413                        }
 414                }
 415        } else
 416               pde_put(de);
 417        return inode;
 418}
 419
 420int proc_fill_super(struct super_block *s, void *data, int silent)
 421{
 422        struct pid_namespace *ns = get_pid_ns(s->s_fs_info);
 423        struct inode *root_inode;
 424
 425        if (!proc_parse_options(data, ns))
 426                return -EINVAL;
 427
 428        /* User space would break if executables or devices appear on proc */
 429        s->s_iflags |= SB_I_USERNS_VISIBLE | SB_I_NOEXEC | SB_I_NODEV;
 430        s->s_flags |= MS_NODIRATIME | MS_NOSUID | MS_NOEXEC;
 431        s->s_blocksize = 1024;
 432        s->s_blocksize_bits = 10;
 433        s->s_magic = PROC_SUPER_MAGIC;
 434        s->s_op = &proc_sops;
 435        s->s_time_gran = 1;
 436        
 437        pde_get(&proc_root);
 438        root_inode = proc_get_inode(s, &proc_root);
 439        if (!root_inode) {
 440                pr_err("proc_fill_super: get root inode failed\n");
 441                return -ENOMEM;
 442        }
 443
 444        s->s_root = d_make_root(root_inode);
 445        if (!s->s_root) {
 446                pr_err("proc_fill_super: allocate dentry failed\n");
 447                return -ENOMEM;
 448        }
 449
 450        return proc_setup_self(s);
 451}
 452