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