linux-bk/fs/namespace.c
<<
>>
Prefs
   1/*
   2 *  linux/fs/namespace.c
   3 *
   4 * (C) Copyright Al Viro 2000, 2001
   5 *      Released under GPL v2.
   6 *
   7 * Based on code from fs/super.c, copyright Linus Torvalds and others.
   8 * Heavily rewritten.
   9 */
  10
  11#include <linux/config.h>
  12#include <linux/slab.h>
  13#include <linux/sched.h>
  14#include <linux/smp_lock.h>
  15#include <linux/init.h>
  16#include <linux/quotaops.h>
  17#include <linux/acct.h>
  18#include <linux/module.h>
  19#include <linux/seq_file.h>
  20#include <linux/namespace.h>
  21#include <linux/namei.h>
  22#include <linux/security.h>
  23#include <linux/mount.h>
  24#include <asm/uaccess.h>
  25
  26extern int __init init_rootfs(void);
  27extern int __init sysfs_init(void);
  28
  29/* spinlock for vfsmount related operations, inplace of dcache_lock */
  30spinlock_t vfsmount_lock __cacheline_aligned_in_smp = SPIN_LOCK_UNLOCKED;
  31static struct list_head *mount_hashtable;
  32static int hash_mask, hash_bits;
  33static kmem_cache_t *mnt_cache; 
  34
  35static inline unsigned long hash(struct vfsmount *mnt, struct dentry *dentry)
  36{
  37        unsigned long tmp = ((unsigned long) mnt / L1_CACHE_BYTES);
  38        tmp += ((unsigned long) dentry / L1_CACHE_BYTES);
  39        tmp = tmp + (tmp >> hash_bits);
  40        return tmp & hash_mask;
  41}
  42
  43struct vfsmount *alloc_vfsmnt(const char *name)
  44{
  45        struct vfsmount *mnt = kmem_cache_alloc(mnt_cache, GFP_KERNEL); 
  46        if (mnt) {
  47                memset(mnt, 0, sizeof(struct vfsmount));
  48                atomic_set(&mnt->mnt_count,1);
  49                INIT_LIST_HEAD(&mnt->mnt_hash);
  50                INIT_LIST_HEAD(&mnt->mnt_child);
  51                INIT_LIST_HEAD(&mnt->mnt_mounts);
  52                INIT_LIST_HEAD(&mnt->mnt_list);
  53                if (name) {
  54                        int size = strlen(name)+1;
  55                        char *newname = kmalloc(size, GFP_KERNEL);
  56                        if (newname) {
  57                                memcpy(newname, name, size);
  58                                mnt->mnt_devname = newname;
  59                        }
  60                }
  61        }
  62        return mnt;
  63}
  64
  65void free_vfsmnt(struct vfsmount *mnt)
  66{
  67        kfree(mnt->mnt_devname);
  68        kmem_cache_free(mnt_cache, mnt);
  69}
  70
  71/*
  72 * Now, lookup_mnt increments the ref count before returning
  73 * the vfsmount struct.
  74 */
  75struct vfsmount *lookup_mnt(struct vfsmount *mnt, struct dentry *dentry)
  76{
  77        struct list_head * head = mount_hashtable + hash(mnt, dentry);
  78        struct list_head * tmp = head;
  79        struct vfsmount *p, *found = NULL;
  80
  81        spin_lock(&vfsmount_lock);
  82        for (;;) {
  83                tmp = tmp->next;
  84                p = NULL;
  85                if (tmp == head)
  86                        break;
  87                p = list_entry(tmp, struct vfsmount, mnt_hash);
  88                if (p->mnt_parent == mnt && p->mnt_mountpoint == dentry) {
  89                        found = mntget(p);
  90                        break;
  91                }
  92        }
  93        spin_unlock(&vfsmount_lock);
  94        return found;
  95}
  96
  97EXPORT_SYMBOL(lookup_mnt);
  98
  99static int check_mnt(struct vfsmount *mnt)
 100{
 101        spin_lock(&vfsmount_lock);
 102        while (mnt->mnt_parent != mnt)
 103                mnt = mnt->mnt_parent;
 104        spin_unlock(&vfsmount_lock);
 105        return mnt == current->namespace->root;
 106}
 107
 108static void detach_mnt(struct vfsmount *mnt, struct nameidata *old_nd)
 109{
 110        old_nd->dentry = mnt->mnt_mountpoint;
 111        old_nd->mnt = mnt->mnt_parent;
 112        mnt->mnt_parent = mnt;
 113        mnt->mnt_mountpoint = mnt->mnt_root;
 114        list_del_init(&mnt->mnt_child);
 115        list_del_init(&mnt->mnt_hash);
 116        old_nd->dentry->d_mounted--;
 117}
 118
 119static void attach_mnt(struct vfsmount *mnt, struct nameidata *nd)
 120{
 121        mnt->mnt_parent = mntget(nd->mnt);
 122        mnt->mnt_mountpoint = dget(nd->dentry);
 123        list_add(&mnt->mnt_hash, mount_hashtable+hash(nd->mnt, nd->dentry));
 124        list_add_tail(&mnt->mnt_child, &nd->mnt->mnt_mounts);
 125        nd->dentry->d_mounted++;
 126}
 127
 128static struct vfsmount *next_mnt(struct vfsmount *p, struct vfsmount *root)
 129{
 130        struct list_head *next = p->mnt_mounts.next;
 131        if (next == &p->mnt_mounts) {
 132                while (1) {
 133                        if (p == root)
 134                                return NULL;
 135                        next = p->mnt_child.next;
 136                        if (next != &p->mnt_parent->mnt_mounts)
 137                                break;
 138                        p = p->mnt_parent;
 139                }
 140        }
 141        return list_entry(next, struct vfsmount, mnt_child);
 142}
 143
 144static struct vfsmount *
 145clone_mnt(struct vfsmount *old, struct dentry *root)
 146{
 147        struct super_block *sb = old->mnt_sb;
 148        struct vfsmount *mnt = alloc_vfsmnt(old->mnt_devname);
 149
 150        if (mnt) {
 151                mnt->mnt_flags = old->mnt_flags;
 152                atomic_inc(&sb->s_active);
 153                mnt->mnt_sb = sb;
 154                mnt->mnt_root = dget(root);
 155                mnt->mnt_mountpoint = mnt->mnt_root;
 156                mnt->mnt_parent = mnt;
 157        }
 158        return mnt;
 159}
 160
 161void __mntput(struct vfsmount *mnt)
 162{
 163        struct super_block *sb = mnt->mnt_sb;
 164        dput(mnt->mnt_root);
 165        free_vfsmnt(mnt);
 166        deactivate_super(sb);
 167}
 168
 169EXPORT_SYMBOL(__mntput);
 170
 171/* iterator */
 172static void *m_start(struct seq_file *m, loff_t *pos)
 173{
 174        struct namespace *n = m->private;
 175        struct list_head *p;
 176        loff_t l = *pos;
 177
 178        down_read(&n->sem);
 179        list_for_each(p, &n->list)
 180                if (!l--)
 181                        return list_entry(p, struct vfsmount, mnt_list);
 182        return NULL;
 183}
 184
 185static void *m_next(struct seq_file *m, void *v, loff_t *pos)
 186{
 187        struct namespace *n = m->private;
 188        struct list_head *p = ((struct vfsmount *)v)->mnt_list.next;
 189        (*pos)++;
 190        return p==&n->list ? NULL : list_entry(p, struct vfsmount, mnt_list);
 191}
 192
 193static void m_stop(struct seq_file *m, void *v)
 194{
 195        struct namespace *n = m->private;
 196        up_read(&n->sem);
 197}
 198
 199static inline void mangle(struct seq_file *m, const char *s)
 200{
 201        seq_escape(m, s, " \t\n\\");
 202}
 203
 204static int show_vfsmnt(struct seq_file *m, void *v)
 205{
 206        struct vfsmount *mnt = v;
 207        int err = 0;
 208        static struct proc_fs_info {
 209                int flag;
 210                char *str;
 211        } fs_info[] = {
 212                { MS_SYNCHRONOUS, ",sync" },
 213                { MS_DIRSYNC, ",dirsync" },
 214                { MS_MANDLOCK, ",mand" },
 215                { MS_NOATIME, ",noatime" },
 216                { MS_NODIRATIME, ",nodiratime" },
 217                { 0, NULL }
 218        };
 219        static struct proc_fs_info mnt_info[] = {
 220                { MNT_NOSUID, ",nosuid" },
 221                { MNT_NODEV, ",nodev" },
 222                { MNT_NOEXEC, ",noexec" },
 223                { 0, NULL }
 224        };
 225        struct proc_fs_info *fs_infop;
 226
 227        mangle(m, mnt->mnt_devname ? mnt->mnt_devname : "none");
 228        seq_putc(m, ' ');
 229        seq_path(m, mnt, mnt->mnt_root, " \t\n\\");
 230        seq_putc(m, ' ');
 231        mangle(m, mnt->mnt_sb->s_type->name);
 232        seq_puts(m, mnt->mnt_sb->s_flags & MS_RDONLY ? " ro" : " rw");
 233        for (fs_infop = fs_info; fs_infop->flag; fs_infop++) {
 234                if (mnt->mnt_sb->s_flags & fs_infop->flag)
 235                        seq_puts(m, fs_infop->str);
 236        }
 237        for (fs_infop = mnt_info; fs_infop->flag; fs_infop++) {
 238                if (mnt->mnt_flags & fs_infop->flag)
 239                        seq_puts(m, fs_infop->str);
 240        }
 241        if (mnt->mnt_sb->s_op->show_options)
 242                err = mnt->mnt_sb->s_op->show_options(m, mnt);
 243        seq_puts(m, " 0 0\n");
 244        return err;
 245}
 246
 247struct seq_operations mounts_op = {
 248        .start  = m_start,
 249        .next   = m_next,
 250        .stop   = m_stop,
 251        .show   = show_vfsmnt
 252};
 253
 254/*
 255 * Doesn't take quota and stuff into account. IOW, in some cases it will
 256 * give false negatives. The main reason why it's here is that we need
 257 * a non-destructive way to look for easily umountable filesystems.
 258 */
 259int may_umount(struct vfsmount *mnt)
 260{
 261        if (atomic_read(&mnt->mnt_count) > 2)
 262                return -EBUSY;
 263        return 0;
 264}
 265
 266EXPORT_SYMBOL(may_umount);
 267
 268void umount_tree(struct vfsmount *mnt)
 269{
 270        struct vfsmount *p;
 271        LIST_HEAD(kill);
 272
 273        for (p = mnt; p; p = next_mnt(p, mnt)) {
 274                list_del(&p->mnt_list);
 275                list_add(&p->mnt_list, &kill);
 276        }
 277
 278        while (!list_empty(&kill)) {
 279                mnt = list_entry(kill.next, struct vfsmount, mnt_list);
 280                list_del_init(&mnt->mnt_list);
 281                if (mnt->mnt_parent == mnt) {
 282                        spin_unlock(&vfsmount_lock);
 283                } else {
 284                        struct nameidata old_nd;
 285                        detach_mnt(mnt, &old_nd);
 286                        spin_unlock(&vfsmount_lock);
 287                        path_release(&old_nd);
 288                }
 289                mntput(mnt);
 290                spin_lock(&vfsmount_lock);
 291        }
 292}
 293
 294static int do_umount(struct vfsmount *mnt, int flags)
 295{
 296        struct super_block * sb = mnt->mnt_sb;
 297        int retval;
 298
 299        retval = security_sb_umount(mnt, flags);
 300        if (retval)
 301                return retval;
 302
 303        /*
 304         * If we may have to abort operations to get out of this
 305         * mount, and they will themselves hold resources we must
 306         * allow the fs to do things. In the Unix tradition of
 307         * 'Gee thats tricky lets do it in userspace' the umount_begin
 308         * might fail to complete on the first run through as other tasks
 309         * must return, and the like. Thats for the mount program to worry
 310         * about for the moment.
 311         */
 312
 313        lock_kernel();
 314        if( (flags&MNT_FORCE) && sb->s_op->umount_begin)
 315                sb->s_op->umount_begin(sb);
 316        unlock_kernel();
 317
 318        /*
 319         * No sense to grab the lock for this test, but test itself looks
 320         * somewhat bogus. Suggestions for better replacement?
 321         * Ho-hum... In principle, we might treat that as umount + switch
 322         * to rootfs. GC would eventually take care of the old vfsmount.
 323         * Actually it makes sense, especially if rootfs would contain a
 324         * /reboot - static binary that would close all descriptors and
 325         * call reboot(9). Then init(8) could umount root and exec /reboot.
 326         */
 327        if (mnt == current->fs->rootmnt && !(flags & MNT_DETACH)) {
 328                /*
 329                 * Special case for "unmounting" root ...
 330                 * we just try to remount it readonly.
 331                 */
 332                down_write(&sb->s_umount);
 333                if (!(sb->s_flags & MS_RDONLY)) {
 334                        lock_kernel();
 335                        retval = do_remount_sb(sb, MS_RDONLY, 0, 0);
 336                        unlock_kernel();
 337                }
 338                up_write(&sb->s_umount);
 339                return retval;
 340        }
 341
 342        down_write(&current->namespace->sem);
 343        spin_lock(&vfsmount_lock);
 344
 345        if (atomic_read(&sb->s_active) == 1) {
 346                /* last instance - try to be smart */
 347                spin_unlock(&vfsmount_lock);
 348                lock_kernel();
 349                DQUOT_OFF(sb);
 350                acct_auto_close(sb);
 351                unlock_kernel();
 352                security_sb_umount_close(mnt);
 353                spin_lock(&vfsmount_lock);
 354        }
 355        retval = -EBUSY;
 356        if (atomic_read(&mnt->mnt_count) == 2 || flags & MNT_DETACH) {
 357                if (!list_empty(&mnt->mnt_list))
 358                        umount_tree(mnt);
 359                retval = 0;
 360        }
 361        spin_unlock(&vfsmount_lock);
 362        if (retval)
 363                security_sb_umount_busy(mnt);
 364        up_write(&current->namespace->sem);
 365        return retval;
 366}
 367
 368/*
 369 * Now umount can handle mount points as well as block devices.
 370 * This is important for filesystems which use unnamed block devices.
 371 *
 372 * We now support a flag for forced unmount like the other 'big iron'
 373 * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
 374 */
 375
 376asmlinkage long sys_umount(char __user * name, int flags)
 377{
 378        struct nameidata nd;
 379        int retval;
 380
 381        retval = __user_walk(name, LOOKUP_FOLLOW, &nd);
 382        if (retval)
 383                goto out;
 384        retval = -EINVAL;
 385        if (nd.dentry != nd.mnt->mnt_root)
 386                goto dput_and_out;
 387        if (!check_mnt(nd.mnt))
 388                goto dput_and_out;
 389
 390        retval = -EPERM;
 391        if (!capable(CAP_SYS_ADMIN))
 392                goto dput_and_out;
 393
 394        retval = do_umount(nd.mnt, flags);
 395dput_and_out:
 396        path_release(&nd);
 397out:
 398        return retval;
 399}
 400
 401/*
 402 *      The 2.0 compatible umount. No flags. 
 403 */
 404 
 405asmlinkage long sys_oldumount(char __user * name)
 406{
 407        return sys_umount(name,0);
 408}
 409
 410static int mount_is_safe(struct nameidata *nd)
 411{
 412        if (capable(CAP_SYS_ADMIN))
 413                return 0;
 414        return -EPERM;
 415#ifdef notyet
 416        if (S_ISLNK(nd->dentry->d_inode->i_mode))
 417                return -EPERM;
 418        if (nd->dentry->d_inode->i_mode & S_ISVTX) {
 419                if (current->uid != nd->dentry->d_inode->i_uid)
 420                        return -EPERM;
 421        }
 422        if (permission(nd->dentry->d_inode, MAY_WRITE, nd))
 423                return -EPERM;
 424        return 0;
 425#endif
 426}
 427
 428static int
 429lives_below_in_same_fs(struct dentry *d, struct dentry *dentry)
 430{
 431        while (1) {
 432                if (d == dentry)
 433                        return 1;
 434                if (d == NULL || d == d->d_parent)
 435                        return 0;
 436                d = d->d_parent;
 437        }
 438}
 439
 440static struct vfsmount *copy_tree(struct vfsmount *mnt, struct dentry *dentry)
 441{
 442        struct vfsmount *res, *p, *q, *r, *s;
 443        struct list_head *h;
 444        struct nameidata nd;
 445
 446        res = q = clone_mnt(mnt, dentry);
 447        if (!q)
 448                goto Enomem;
 449        q->mnt_mountpoint = mnt->mnt_mountpoint;
 450
 451        p = mnt;
 452        for (h = mnt->mnt_mounts.next; h != &mnt->mnt_mounts; h = h->next) {
 453                r = list_entry(h, struct vfsmount, mnt_child);
 454                if (!lives_below_in_same_fs(r->mnt_mountpoint, dentry))
 455                        continue;
 456
 457                for (s = r; s; s = next_mnt(s, r)) {
 458                        while (p != s->mnt_parent) {
 459                                p = p->mnt_parent;
 460                                q = q->mnt_parent;
 461                        }
 462                        p = s;
 463                        nd.mnt = q;
 464                        nd.dentry = p->mnt_mountpoint;
 465                        q = clone_mnt(p, p->mnt_root);
 466                        if (!q)
 467                                goto Enomem;
 468                        spin_lock(&vfsmount_lock);
 469                        list_add_tail(&q->mnt_list, &res->mnt_list);
 470                        attach_mnt(q, &nd);
 471                        spin_unlock(&vfsmount_lock);
 472                }
 473        }
 474        return res;
 475 Enomem:
 476        if (res) {
 477                spin_lock(&vfsmount_lock);
 478                umount_tree(res);
 479                spin_unlock(&vfsmount_lock);
 480        }
 481        return NULL;
 482}
 483
 484static int graft_tree(struct vfsmount *mnt, struct nameidata *nd)
 485{
 486        int err;
 487        if (mnt->mnt_sb->s_flags & MS_NOUSER)
 488                return -EINVAL;
 489
 490        if (S_ISDIR(nd->dentry->d_inode->i_mode) !=
 491              S_ISDIR(mnt->mnt_root->d_inode->i_mode))
 492                return -ENOTDIR;
 493
 494        err = -ENOENT;
 495        down(&nd->dentry->d_inode->i_sem);
 496        if (IS_DEADDIR(nd->dentry->d_inode))
 497                goto out_unlock;
 498
 499        err = security_sb_check_sb(mnt, nd);
 500        if (err)
 501                goto out_unlock;
 502
 503        err = -ENOENT;
 504        spin_lock(&vfsmount_lock);
 505        if (IS_ROOT(nd->dentry) || !d_unhashed(nd->dentry)) {
 506                struct list_head head;
 507
 508                attach_mnt(mnt, nd);
 509                list_add_tail(&head, &mnt->mnt_list);
 510                list_splice(&head, current->namespace->list.prev);
 511                mntget(mnt);
 512                err = 0;
 513        }
 514        spin_unlock(&vfsmount_lock);
 515out_unlock:
 516        up(&nd->dentry->d_inode->i_sem);
 517        if (!err)
 518                security_sb_post_addmount(mnt, nd);
 519        return err;
 520}
 521
 522/*
 523 * do loopback mount.
 524 */
 525static int do_loopback(struct nameidata *nd, char *old_name, int recurse)
 526{
 527        struct nameidata old_nd;
 528        struct vfsmount *mnt = NULL;
 529        int err = mount_is_safe(nd);
 530        if (err)
 531                return err;
 532        if (!old_name || !*old_name)
 533                return -EINVAL;
 534        err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd);
 535        if (err)
 536                return err;
 537
 538        down_write(&current->namespace->sem);
 539        err = -EINVAL;
 540        if (check_mnt(nd->mnt) && (!recurse || check_mnt(old_nd.mnt))) {
 541                err = -ENOMEM;
 542                if (recurse)
 543                        mnt = copy_tree(old_nd.mnt, old_nd.dentry);
 544                else
 545                        mnt = clone_mnt(old_nd.mnt, old_nd.dentry);
 546        }
 547
 548        if (mnt) {
 549                err = graft_tree(mnt, nd);
 550                if (err) {
 551                        spin_lock(&vfsmount_lock);
 552                        umount_tree(mnt);
 553                        spin_unlock(&vfsmount_lock);
 554                } else
 555                        mntput(mnt);
 556        }
 557
 558        up_write(&current->namespace->sem);
 559        path_release(&old_nd);
 560        return err;
 561}
 562
 563/*
 564 * change filesystem flags. dir should be a physical root of filesystem.
 565 * If you've mounted a non-root directory somewhere and want to do remount
 566 * on it - tough luck.
 567 */
 568
 569static int do_remount(struct nameidata *nd,int flags,int mnt_flags,void *data)
 570{
 571        int err;
 572        struct super_block * sb = nd->mnt->mnt_sb;
 573
 574        if (!capable(CAP_SYS_ADMIN))
 575                return -EPERM;
 576
 577        if (!check_mnt(nd->mnt))
 578                return -EINVAL;
 579
 580        if (nd->dentry != nd->mnt->mnt_root)
 581                return -EINVAL;
 582
 583        down_write(&sb->s_umount);
 584        err = do_remount_sb(sb, flags, data, 0);
 585        if (!err)
 586                nd->mnt->mnt_flags=mnt_flags;
 587        up_write(&sb->s_umount);
 588        if (!err)
 589                security_sb_post_remount(nd->mnt, flags, data);
 590        return err;
 591}
 592
 593static int do_move_mount(struct nameidata *nd, char *old_name)
 594{
 595        struct nameidata old_nd, parent_nd;
 596        struct vfsmount *p;
 597        int err = 0;
 598        if (!capable(CAP_SYS_ADMIN))
 599                return -EPERM;
 600        if (!old_name || !*old_name)
 601                return -EINVAL;
 602        err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd);
 603        if (err)
 604                return err;
 605
 606        down_write(&current->namespace->sem);
 607        while(d_mountpoint(nd->dentry) && follow_down(&nd->mnt, &nd->dentry))
 608                ;
 609        err = -EINVAL;
 610        if (!check_mnt(nd->mnt) || !check_mnt(old_nd.mnt))
 611                goto out;
 612
 613        err = -ENOENT;
 614        down(&nd->dentry->d_inode->i_sem);
 615        if (IS_DEADDIR(nd->dentry->d_inode))
 616                goto out1;
 617
 618        spin_lock(&vfsmount_lock);
 619        if (!IS_ROOT(nd->dentry) && d_unhashed(nd->dentry))
 620                goto out2;
 621
 622        err = -EINVAL;
 623        if (old_nd.dentry != old_nd.mnt->mnt_root)
 624                goto out2;
 625
 626        if (old_nd.mnt == old_nd.mnt->mnt_parent)
 627                goto out2;
 628
 629        if (S_ISDIR(nd->dentry->d_inode->i_mode) !=
 630              S_ISDIR(old_nd.dentry->d_inode->i_mode))
 631                goto out2;
 632
 633        err = -ELOOP;
 634        for (p = nd->mnt; p->mnt_parent!=p; p = p->mnt_parent)
 635                if (p == old_nd.mnt)
 636                        goto out2;
 637        err = 0;
 638
 639        detach_mnt(old_nd.mnt, &parent_nd);
 640        attach_mnt(old_nd.mnt, nd);
 641out2:
 642        spin_unlock(&vfsmount_lock);
 643out1:
 644        up(&nd->dentry->d_inode->i_sem);
 645out:
 646        up_write(&current->namespace->sem);
 647        if (!err)
 648                path_release(&parent_nd);
 649        path_release(&old_nd);
 650        return err;
 651}
 652
 653static int do_add_mount(struct nameidata *nd, char *type, int flags,
 654                        int mnt_flags, char *name, void *data)
 655{
 656        struct vfsmount *mnt;
 657        int err;
 658
 659        if (!type || !memchr(type, 0, PAGE_SIZE))
 660                return -EINVAL;
 661
 662        /* we need capabilities... */
 663        if (!capable(CAP_SYS_ADMIN))
 664                return -EPERM;
 665
 666        mnt = do_kern_mount(type, flags, name, data);
 667        err = PTR_ERR(mnt);
 668        if (IS_ERR(mnt))
 669                goto out;
 670
 671        down_write(&current->namespace->sem);
 672        /* Something was mounted here while we slept */
 673        while(d_mountpoint(nd->dentry) && follow_down(&nd->mnt, &nd->dentry))
 674                ;
 675        err = -EINVAL;
 676        if (!check_mnt(nd->mnt))
 677                goto unlock;
 678
 679        /* Refuse the same filesystem on the same mount point */
 680        err = -EBUSY;
 681        if (nd->mnt->mnt_sb == mnt->mnt_sb && nd->mnt->mnt_root == nd->dentry)
 682                goto unlock;
 683
 684        mnt->mnt_flags = mnt_flags;
 685        err = graft_tree(mnt, nd);
 686unlock:
 687        up_write(&current->namespace->sem);
 688        mntput(mnt);
 689out:
 690        return err;
 691}
 692
 693static int copy_mount_options (const void __user *data, unsigned long *where)
 694{
 695        int i;
 696        unsigned long page;
 697        unsigned long size;
 698        
 699        *where = 0;
 700        if (!data)
 701                return 0;
 702
 703        if (!(page = __get_free_page(GFP_KERNEL)))
 704                return -ENOMEM;
 705
 706        /* We only care that *some* data at the address the user
 707         * gave us is valid.  Just in case, we'll zero
 708         * the remainder of the page.
 709         */
 710        /* copy_from_user cannot cross TASK_SIZE ! */
 711        size = TASK_SIZE - (unsigned long)data;
 712        if (size > PAGE_SIZE)
 713                size = PAGE_SIZE;
 714
 715        i = size - copy_from_user((void *)page, data, size);
 716        if (!i) {
 717                free_page(page); 
 718                return -EFAULT;
 719        }
 720        if (i != PAGE_SIZE)
 721                memset((char *)page + i, 0, PAGE_SIZE - i);
 722        *where = page;
 723        return 0;
 724}
 725
 726/*
 727 * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
 728 * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
 729 *
 730 * data is a (void *) that can point to any structure up to
 731 * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
 732 * information (or be NULL).
 733 *
 734 * Pre-0.97 versions of mount() didn't have a flags word.
 735 * When the flags word was introduced its top half was required
 736 * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
 737 * Therefore, if this magic number is present, it carries no information
 738 * and must be discarded.
 739 */
 740long do_mount(char * dev_name, char * dir_name, char *type_page,
 741                  unsigned long flags, void *data_page)
 742{
 743        struct nameidata nd;
 744        int retval = 0;
 745        int mnt_flags = 0;
 746
 747        /* Discard magic */
 748        if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
 749                flags &= ~MS_MGC_MSK;
 750
 751        /* Basic sanity checks */
 752
 753        if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE))
 754                return -EINVAL;
 755        if (dev_name && !memchr(dev_name, 0, PAGE_SIZE))
 756                return -EINVAL;
 757
 758        /* Separate the per-mountpoint flags */
 759        if (flags & MS_NOSUID)
 760                mnt_flags |= MNT_NOSUID;
 761        if (flags & MS_NODEV)
 762                mnt_flags |= MNT_NODEV;
 763        if (flags & MS_NOEXEC)
 764                mnt_flags |= MNT_NOEXEC;
 765        flags &= ~(MS_NOSUID|MS_NOEXEC|MS_NODEV);
 766
 767        /* ... and get the mountpoint */
 768        retval = path_lookup(dir_name, LOOKUP_FOLLOW, &nd);
 769        if (retval)
 770                return retval;
 771
 772        retval = security_sb_mount(dev_name, &nd, type_page, flags, data_page);
 773        if (retval)
 774                goto dput_out;
 775
 776        if (flags & MS_REMOUNT)
 777                retval = do_remount(&nd, flags & ~MS_REMOUNT, mnt_flags,
 778                                    data_page);
 779        else if (flags & MS_BIND)
 780                retval = do_loopback(&nd, dev_name, flags & MS_REC);
 781        else if (flags & MS_MOVE)
 782                retval = do_move_mount(&nd, dev_name);
 783        else
 784                retval = do_add_mount(&nd, type_page, flags, mnt_flags,
 785                                      dev_name, data_page);
 786dput_out:
 787        path_release(&nd);
 788        return retval;
 789}
 790
 791int copy_namespace(int flags, struct task_struct *tsk)
 792{
 793        struct namespace *namespace = tsk->namespace;
 794        struct namespace *new_ns;
 795        struct vfsmount *rootmnt = NULL, *pwdmnt = NULL, *altrootmnt = NULL;
 796        struct fs_struct *fs = tsk->fs;
 797
 798        if (!namespace)
 799                return 0;
 800
 801        get_namespace(namespace);
 802
 803        if (!(flags & CLONE_NEWNS))
 804                return 0;
 805
 806        if (!capable(CAP_SYS_ADMIN)) {
 807                put_namespace(namespace);
 808                return -EPERM;
 809        }
 810
 811        new_ns = kmalloc(sizeof(struct namespace), GFP_KERNEL);
 812        if (!new_ns)
 813                goto out;
 814
 815        atomic_set(&new_ns->count, 1);
 816        init_rwsem(&new_ns->sem);
 817        new_ns->root = NULL;
 818        INIT_LIST_HEAD(&new_ns->list);
 819
 820        down_write(&tsk->namespace->sem);
 821        /* First pass: copy the tree topology */
 822        new_ns->root = copy_tree(namespace->root, namespace->root->mnt_root);
 823        spin_lock(&vfsmount_lock);
 824        list_add_tail(&new_ns->list, &new_ns->root->mnt_list);
 825        spin_unlock(&vfsmount_lock);
 826
 827        /* Second pass: switch the tsk->fs->* elements */
 828        if (fs) {
 829                struct vfsmount *p, *q;
 830                write_lock(&fs->lock);
 831
 832                p = namespace->root;
 833                q = new_ns->root;
 834                while (p) {
 835                        if (p == fs->rootmnt) {
 836                                rootmnt = p;
 837                                fs->rootmnt = mntget(q);
 838                        }
 839                        if (p == fs->pwdmnt) {
 840                                pwdmnt = p;
 841                                fs->pwdmnt = mntget(q);
 842                        }
 843                        if (p == fs->altrootmnt) {
 844                                altrootmnt = p;
 845                                fs->altrootmnt = mntget(q);
 846                        }
 847                        p = next_mnt(p, namespace->root);
 848                        q = next_mnt(q, new_ns->root);
 849                }
 850                write_unlock(&fs->lock);
 851        }
 852        up_write(&tsk->namespace->sem);
 853
 854        tsk->namespace = new_ns;
 855
 856        if (rootmnt)
 857                mntput(rootmnt);
 858        if (pwdmnt)
 859                mntput(pwdmnt);
 860        if (altrootmnt)
 861                mntput(altrootmnt);
 862
 863        put_namespace(namespace);
 864        return 0;
 865
 866out:
 867        put_namespace(namespace);
 868        return -ENOMEM;
 869}
 870
 871asmlinkage long sys_mount(char __user * dev_name, char __user * dir_name,
 872                          char __user * type, unsigned long flags,
 873                          void __user * data)
 874{
 875        int retval;
 876        unsigned long data_page;
 877        unsigned long type_page;
 878        unsigned long dev_page;
 879        char *dir_page;
 880
 881        retval = copy_mount_options (type, &type_page);
 882        if (retval < 0)
 883                return retval;
 884
 885        dir_page = getname(dir_name);
 886        retval = PTR_ERR(dir_page);
 887        if (IS_ERR(dir_page))
 888                goto out1;
 889
 890        retval = copy_mount_options (dev_name, &dev_page);
 891        if (retval < 0)
 892                goto out2;
 893
 894        retval = copy_mount_options (data, &data_page);
 895        if (retval < 0)
 896                goto out3;
 897
 898        lock_kernel();
 899        retval = do_mount((char*)dev_page, dir_page, (char*)type_page,
 900                          flags, (void*)data_page);
 901        unlock_kernel();
 902        free_page(data_page);
 903
 904out3:
 905        free_page(dev_page);
 906out2:
 907        putname(dir_page);
 908out1:
 909        free_page(type_page);
 910        return retval;
 911}
 912
 913/*
 914 * Replace the fs->{rootmnt,root} with {mnt,dentry}. Put the old values.
 915 * It can block. Requires the big lock held.
 916 */
 917void set_fs_root(struct fs_struct *fs, struct vfsmount *mnt,
 918                 struct dentry *dentry)
 919{
 920        struct dentry *old_root;
 921        struct vfsmount *old_rootmnt;
 922        write_lock(&fs->lock);
 923        old_root = fs->root;
 924        old_rootmnt = fs->rootmnt;
 925        fs->rootmnt = mntget(mnt);
 926        fs->root = dget(dentry);
 927        write_unlock(&fs->lock);
 928        if (old_root) {
 929                dput(old_root);
 930                mntput(old_rootmnt);
 931        }
 932}
 933
 934EXPORT_SYMBOL(set_fs_root);
 935
 936/*
 937 * Replace the fs->{pwdmnt,pwd} with {mnt,dentry}. Put the old values.
 938 * It can block. Requires the big lock held.
 939 */
 940void set_fs_pwd(struct fs_struct *fs, struct vfsmount *mnt,
 941                struct dentry *dentry)
 942{
 943        struct dentry *old_pwd;
 944        struct vfsmount *old_pwdmnt;
 945
 946        write_lock(&fs->lock);
 947        old_pwd = fs->pwd;
 948        old_pwdmnt = fs->pwdmnt;
 949        fs->pwdmnt = mntget(mnt);
 950        fs->pwd = dget(dentry);
 951        write_unlock(&fs->lock);
 952
 953        if (old_pwd) {
 954                dput(old_pwd);
 955                mntput(old_pwdmnt);
 956        }
 957}
 958
 959EXPORT_SYMBOL(set_fs_pwd);
 960
 961static void chroot_fs_refs(struct nameidata *old_nd, struct nameidata *new_nd)
 962{
 963        struct task_struct *g, *p;
 964        struct fs_struct *fs;
 965
 966        read_lock(&tasklist_lock);
 967        do_each_thread(g, p) {
 968                task_lock(p);
 969                fs = p->fs;
 970                if (fs) {
 971                        atomic_inc(&fs->count);
 972                        task_unlock(p);
 973                        if (fs->root==old_nd->dentry&&fs->rootmnt==old_nd->mnt)
 974                                set_fs_root(fs, new_nd->mnt, new_nd->dentry);
 975                        if (fs->pwd==old_nd->dentry&&fs->pwdmnt==old_nd->mnt)
 976                                set_fs_pwd(fs, new_nd->mnt, new_nd->dentry);
 977                        put_fs_struct(fs);
 978                } else
 979                        task_unlock(p);
 980        } while_each_thread(g, p);
 981        read_unlock(&tasklist_lock);
 982}
 983
 984/*
 985 * Moves the current root to put_root, and sets root/cwd of all processes
 986 * which had them on the old root to new_root.
 987 *
 988 * Note:
 989 *  - we don't move root/cwd if they are not at the root (reason: if something
 990 *    cared enough to change them, it's probably wrong to force them elsewhere)
 991 *  - it's okay to pick a root that isn't the root of a file system, e.g.
 992 *    /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
 993 *    though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
 994 *    first.
 995 */
 996
 997asmlinkage long sys_pivot_root(const char __user *new_root, const char __user *put_old)
 998{
 999        struct vfsmount *tmp;
1000        struct nameidata new_nd, old_nd, parent_nd, root_parent, user_nd;
1001        int error;
1002
1003        if (!capable(CAP_SYS_ADMIN))
1004                return -EPERM;
1005
1006        lock_kernel();
1007
1008        error = __user_walk(new_root, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &new_nd);
1009        if (error)
1010                goto out0;
1011        error = -EINVAL;
1012        if (!check_mnt(new_nd.mnt))
1013                goto out1;
1014
1015        error = __user_walk(put_old, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &old_nd);
1016        if (error)
1017                goto out1;
1018
1019        error = security_sb_pivotroot(&old_nd, &new_nd);
1020        if (error) {
1021                path_release(&old_nd);
1022                goto out1;
1023        }
1024
1025        read_lock(&current->fs->lock);
1026        user_nd.mnt = mntget(current->fs->rootmnt);
1027        user_nd.dentry = dget(current->fs->root);
1028        read_unlock(&current->fs->lock);
1029        down_write(&current->namespace->sem);
1030        down(&old_nd.dentry->d_inode->i_sem);
1031        error = -EINVAL;
1032        if (!check_mnt(user_nd.mnt))
1033                goto out2;
1034        error = -ENOENT;
1035        if (IS_DEADDIR(new_nd.dentry->d_inode))
1036                goto out2;
1037        if (d_unhashed(new_nd.dentry) && !IS_ROOT(new_nd.dentry))
1038                goto out2;
1039        if (d_unhashed(old_nd.dentry) && !IS_ROOT(old_nd.dentry))
1040                goto out2;
1041        error = -EBUSY;
1042        if (new_nd.mnt == user_nd.mnt || old_nd.mnt == user_nd.mnt)
1043                goto out2; /* loop */
1044        error = -EINVAL;
1045        if (user_nd.mnt->mnt_root != user_nd.dentry)
1046                goto out2;
1047        if (new_nd.mnt->mnt_root != new_nd.dentry)
1048                goto out2; /* not a mountpoint */
1049        tmp = old_nd.mnt; /* make sure we can reach put_old from new_root */
1050        spin_lock(&vfsmount_lock);
1051        if (tmp != new_nd.mnt) {
1052                for (;;) {
1053                        if (tmp->mnt_parent == tmp)
1054                                goto out3;
1055                        if (tmp->mnt_parent == new_nd.mnt)
1056                                break;
1057                        tmp = tmp->mnt_parent;
1058                }
1059                if (!is_subdir(tmp->mnt_mountpoint, new_nd.dentry))
1060                        goto out3;
1061        } else if (!is_subdir(old_nd.dentry, new_nd.dentry))
1062                goto out3;
1063        detach_mnt(new_nd.mnt, &parent_nd);
1064        detach_mnt(user_nd.mnt, &root_parent);
1065        attach_mnt(user_nd.mnt, &old_nd);
1066        attach_mnt(new_nd.mnt, &root_parent);
1067        spin_unlock(&vfsmount_lock);
1068        chroot_fs_refs(&user_nd, &new_nd);
1069        security_sb_post_pivotroot(&user_nd, &new_nd);
1070        error = 0;
1071        path_release(&root_parent);
1072        path_release(&parent_nd);
1073out2:
1074        up(&old_nd.dentry->d_inode->i_sem);
1075        up_write(&current->namespace->sem);
1076        path_release(&user_nd);
1077        path_release(&old_nd);
1078out1:
1079        path_release(&new_nd);
1080out0:
1081        unlock_kernel();
1082        return error;
1083out3:
1084        spin_unlock(&vfsmount_lock);
1085        goto out2;
1086}
1087
1088static void __init init_mount_tree(void)
1089{
1090        struct vfsmount *mnt;
1091        struct namespace *namespace;
1092        struct task_struct *g, *p;
1093
1094        mnt = do_kern_mount("rootfs", 0, "rootfs", NULL);
1095        if (IS_ERR(mnt))
1096                panic("Can't create rootfs");
1097        namespace = kmalloc(sizeof(*namespace), GFP_KERNEL);
1098        if (!namespace)
1099                panic("Can't allocate initial namespace");
1100        atomic_set(&namespace->count, 1);
1101        INIT_LIST_HEAD(&namespace->list);
1102        init_rwsem(&namespace->sem);
1103        list_add(&mnt->mnt_list, &namespace->list);
1104        namespace->root = mnt;
1105
1106        init_task.namespace = namespace;
1107        read_lock(&tasklist_lock);
1108        do_each_thread(g, p) {
1109                get_namespace(namespace);
1110                p->namespace = namespace;
1111        } while_each_thread(g, p);
1112        read_unlock(&tasklist_lock);
1113
1114        set_fs_pwd(current->fs, namespace->root, namespace->root->mnt_root);
1115        set_fs_root(current->fs, namespace->root, namespace->root->mnt_root);
1116}
1117
1118void __init mnt_init(unsigned long mempages)
1119{
1120        struct list_head *d;
1121        unsigned long order;
1122        unsigned int nr_hash;
1123        int i;
1124
1125        mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct vfsmount),
1126                                        0, SLAB_HWCACHE_ALIGN, NULL, NULL);
1127        if (!mnt_cache)
1128                panic("Cannot create vfsmount cache");
1129
1130        order = 0; 
1131        mount_hashtable = (struct list_head *)
1132                __get_free_pages(GFP_ATOMIC, order);
1133
1134        if (!mount_hashtable)
1135                panic("Failed to allocate mount hash table\n");
1136
1137        /*
1138         * Find the power-of-two list-heads that can fit into the allocation..
1139         * We don't guarantee that "sizeof(struct list_head)" is necessarily
1140         * a power-of-two.
1141         */
1142        nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct list_head);
1143        hash_bits = 0;
1144        do {
1145                hash_bits++;
1146        } while ((nr_hash >> hash_bits) != 0);
1147        hash_bits--;
1148
1149        /*
1150         * Re-calculate the actual number of entries and the mask
1151         * from the number of bits we can fit.
1152         */
1153        nr_hash = 1UL << hash_bits;
1154        hash_mask = nr_hash-1;
1155
1156        printk("Mount-cache hash table entries: %d (order: %ld, %ld bytes)\n",
1157                        nr_hash, order, (PAGE_SIZE << order));
1158
1159        /* And initialize the newly allocated array */
1160        d = mount_hashtable;
1161        i = nr_hash;
1162        do {
1163                INIT_LIST_HEAD(d);
1164                d++;
1165                i--;
1166        } while (i);
1167        sysfs_init();
1168        init_rootfs();
1169        init_mount_tree();
1170}
1171
1172void __put_namespace(struct namespace *namespace)
1173{
1174        down_write(&namespace->sem);
1175        spin_lock(&vfsmount_lock);
1176        umount_tree(namespace->root);
1177        spin_unlock(&vfsmount_lock);
1178        up_write(&namespace->sem);
1179        kfree(namespace);
1180}
1181
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.