Skip to content
Snippets Groups Projects
namespace.c 55 KiB
Newer Older
  • Learn to ignore specific revisions
  • Ram Pai's avatar
    Ram Pai committed
     *	The 2.0 compatible umount. No flags.
    
    Linus Torvalds's avatar
    Linus Torvalds committed
     */
    asmlinkage long sys_oldumount(char __user * name)
    {
    
    Ram Pai's avatar
    Ram Pai committed
    	return sys_umount(name, 0);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    }
    
    #endif
    
    static int mount_is_safe(struct nameidata *nd)
    {
    	if (capable(CAP_SYS_ADMIN))
    		return 0;
    	return -EPERM;
    #ifdef notyet
    
    	if (S_ISLNK(nd->path.dentry->d_inode->i_mode))
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		return -EPERM;
    
    	if (nd->path.dentry->d_inode->i_mode & S_ISVTX) {
    		if (current->uid != nd->path.dentry->d_inode->i_uid)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			return -EPERM;
    	}
    
    	if (vfs_permission(nd, MAY_WRITE))
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		return -EPERM;
    	return 0;
    #endif
    }
    
    
    Ram Pai's avatar
    Ram Pai committed
    static int lives_below_in_same_fs(struct dentry *d, struct dentry *dentry)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    	while (1) {
    		if (d == dentry)
    			return 1;
    		if (d == NULL || d == d->d_parent)
    			return 0;
    		d = d->d_parent;
    	}
    }
    
    
    struct vfsmount *copy_tree(struct vfsmount *mnt, struct dentry *dentry,
    
    Ram Pai's avatar
    Ram Pai committed
    					int flag)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    	struct vfsmount *res, *p, *q, *r, *s;
    
    	struct path path;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    Ram Pai's avatar
    Ram Pai committed
    	if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(mnt))
    		return NULL;
    
    
    Ram Pai's avatar
    Ram Pai committed
    	res = q = clone_mnt(mnt, dentry, flag);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	if (!q)
    		goto Enomem;
    	q->mnt_mountpoint = mnt->mnt_mountpoint;
    
    	p = mnt;
    
    	list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		if (!lives_below_in_same_fs(r->mnt_mountpoint, dentry))
    			continue;
    
    		for (s = r; s; s = next_mnt(s, r)) {
    
    Ram Pai's avatar
    Ram Pai committed
    			if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(s)) {
    				s = skip_mnt_tree(s);
    				continue;
    			}
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			while (p != s->mnt_parent) {
    				p = p->mnt_parent;
    				q = q->mnt_parent;
    			}
    			p = s;
    
    			path.mnt = q;
    			path.dentry = p->mnt_mountpoint;
    
    Ram Pai's avatar
    Ram Pai committed
    			q = clone_mnt(p, p->mnt_root, flag);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			if (!q)
    				goto Enomem;
    			spin_lock(&vfsmount_lock);
    			list_add_tail(&q->mnt_list, &res->mnt_list);
    
    			attach_mnt(q, &path);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			spin_unlock(&vfsmount_lock);
    		}
    	}
    	return res;
    
    Ram Pai's avatar
    Ram Pai committed
    Enomem:
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	if (res) {
    
    		LIST_HEAD(umount_list);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		spin_lock(&vfsmount_lock);
    
    		umount_tree(res, 0, &umount_list);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		spin_unlock(&vfsmount_lock);
    
    		release_mounts(&umount_list);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	}
    	return NULL;
    }
    
    
    struct vfsmount *collect_mounts(struct vfsmount *mnt, struct dentry *dentry)
    {
    	struct vfsmount *tree;
    
    	tree = copy_tree(mnt, dentry, CL_COPY_ALL | CL_PRIVATE);
    
    	return tree;
    }
    
    void drop_collected_mounts(struct vfsmount *mnt)
    {
    	LIST_HEAD(umount_list);
    
    	spin_lock(&vfsmount_lock);
    	umount_tree(mnt, 0, &umount_list);
    	spin_unlock(&vfsmount_lock);
    
    /*
     *  @source_mnt : mount tree to be attached
    
     *  @nd         : place the mount tree @source_mnt is attached
     *  @parent_nd  : if non-null, detach the source_mnt from its parent and
     *  		   store the parent mount and mountpoint dentry.
     *  		   (done when source_mnt is moved)
    
     *
     *  NOTE: in the table below explains the semantics when a source mount
     *  of a given type is attached to a destination mount of a given type.
    
    Ram Pai's avatar
    Ram Pai committed
     * ---------------------------------------------------------------------------
     * |         BIND MOUNT OPERATION                                            |
     * |**************************************************************************
     * | source-->| shared        |       private  |       slave    | unbindable |
     * | dest     |               |                |                |            |
     * |   |      |               |                |                |            |
     * |   v      |               |                |                |            |
     * |**************************************************************************
     * |  shared  | shared (++)   |     shared (+) |     shared(+++)|  invalid   |
     * |          |               |                |                |            |
     * |non-shared| shared (+)    |      private   |      slave (*) |  invalid   |
     * ***************************************************************************
    
     * A bind operation clones the source mount and mounts the clone on the
     * destination mount.
     *
     * (++)  the cloned mount is propagated to all the mounts in the propagation
     * 	 tree of the destination mount and the cloned mount is added to
     * 	 the peer group of the source mount.
     * (+)   the cloned mount is created under the destination mount and is marked
     *       as shared. The cloned mount is added to the peer group of the source
     *       mount.
    
     * (+++) the mount is propagated to all the mounts in the propagation tree
     *       of the destination mount and the cloned mount is made slave
     *       of the same master as that of the source mount. The cloned mount
     *       is marked as 'shared and slave'.
     * (*)   the cloned mount is made a slave of the same master as that of the
     * 	 source mount.
     *
    
    Ram Pai's avatar
    Ram Pai committed
     * ---------------------------------------------------------------------------
     * |         		MOVE MOUNT OPERATION                                 |
     * |**************************************************************************
     * | source-->| shared        |       private  |       slave    | unbindable |
     * | dest     |               |                |                |            |
     * |   |      |               |                |                |            |
     * |   v      |               |                |                |            |
     * |**************************************************************************
     * |  shared  | shared (+)    |     shared (+) |    shared(+++) |  invalid   |
     * |          |               |                |                |            |
     * |non-shared| shared (+*)   |      private   |    slave (*)   | unbindable |
     * ***************************************************************************
    
     *
     * (+)  the mount is moved to the destination. And is then propagated to
     * 	all the mounts in the propagation tree of the destination mount.
    
     * (+*)  the mount is moved to the destination.
    
     * (+++)  the mount is moved to the destination and is then propagated to
     * 	all the mounts belonging to the destination mount's propagation tree.
     * 	the mount is marked as 'shared and slave'.
     * (*)	the mount continues to be a slave at the new location.
    
     *
     * if the source mount is a tree, the operations explained above is
     * applied to each mount in the tree.
     * Must be called without spinlocks held, since this function can sleep
     * in allocations.
     */
    static int attach_recursive_mnt(struct vfsmount *source_mnt,
    
    			struct path *path, struct path *parent_path)
    
    {
    	LIST_HEAD(tree_list);
    
    	struct vfsmount *dest_mnt = path->mnt;
    	struct dentry *dest_dentry = path->dentry;
    
    	struct vfsmount *child, *p;
    
    	if (propagate_mnt(dest_mnt, dest_dentry, source_mnt, &tree_list))
    		return -EINVAL;
    
    	if (IS_MNT_SHARED(dest_mnt)) {
    		for (p = source_mnt; p; p = next_mnt(p, source_mnt))
    			set_mnt_shared(p);
    	}
    
    	spin_lock(&vfsmount_lock);
    
    	if (parent_path) {
    		detach_mnt(source_mnt, parent_path);
    		attach_mnt(source_mnt, path);
    
    		touch_mnt_namespace(current->nsproxy->mnt_ns);
    
    	} else {
    		mnt_set_mountpoint(dest_mnt, dest_dentry, source_mnt);
    		commit_tree(source_mnt);
    	}
    
    
    	list_for_each_entry_safe(child, p, &tree_list, mnt_hash) {
    		list_del_init(&child->mnt_hash);
    		commit_tree(child);
    	}
    	spin_unlock(&vfsmount_lock);
    	return 0;
    }
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    static int graft_tree(struct vfsmount *mnt, struct nameidata *nd)
    {
    	int err;
    	if (mnt->mnt_sb->s_flags & MS_NOUSER)
    		return -EINVAL;
    
    
    	if (S_ISDIR(nd->path.dentry->d_inode->i_mode) !=
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	      S_ISDIR(mnt->mnt_root->d_inode->i_mode))
    		return -ENOTDIR;
    
    	err = -ENOENT;
    
    	mutex_lock(&nd->path.dentry->d_inode->i_mutex);
    	if (IS_DEADDIR(nd->path.dentry->d_inode))
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		goto out_unlock;
    
    
    	err = security_sb_check_sb(mnt, &nd->path);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	if (err)
    		goto out_unlock;
    
    	err = -ENOENT;
    
    	if (IS_ROOT(nd->path.dentry) || !d_unhashed(nd->path.dentry))
    
    		err = attach_recursive_mnt(mnt, &nd->path, NULL);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    out_unlock:
    
    	mutex_unlock(&nd->path.dentry->d_inode->i_mutex);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	if (!err)
    
    		security_sb_post_addmount(mnt, &nd->path);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	return err;
    }
    
    
    /*
     * recursively change the type of the mountpoint.
    
     * noinline this do_mount helper to save do_mount stack space.
    
    static noinline int do_change_type(struct nameidata *nd, int flag)
    
    	struct vfsmount *m, *mnt = nd->path.mnt;
    
    	int recurse = flag & MS_REC;
    	int type = flag & ~MS_REC;
    
    
    	if (!capable(CAP_SYS_ADMIN))
    		return -EPERM;
    
    
    	if (nd->path.dentry != nd->path.mnt->mnt_root)
    
    		return -EINVAL;
    
    	down_write(&namespace_sem);
    	spin_lock(&vfsmount_lock);
    	for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL))
    		change_mnt_propagation(m, type);
    	spin_unlock(&vfsmount_lock);
    	up_write(&namespace_sem);
    	return 0;
    }
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    /*
     * do loopback mount.
    
     * noinline this do_mount helper to save do_mount stack space.
    
    Linus Torvalds's avatar
    Linus Torvalds committed
     */
    
    static noinline int do_loopback(struct nameidata *nd, char *old_name,
    				int recurse)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    	struct nameidata old_nd;
    	struct vfsmount *mnt = NULL;
    	int err = mount_is_safe(nd);
    	if (err)
    		return err;
    	if (!old_name || !*old_name)
    		return -EINVAL;
    	err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd);
    	if (err)
    		return err;
    
    
    	down_write(&namespace_sem);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	err = -EINVAL;
    
    	if (IS_MNT_UNBINDABLE(old_nd.path.mnt))
    		goto out;
    
    	if (!check_mnt(nd->path.mnt) || !check_mnt(old_nd.path.mnt))
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	err = -ENOMEM;
    	if (recurse)
    
    		mnt = copy_tree(old_nd.path.mnt, old_nd.path.dentry, 0);
    
    		mnt = clone_mnt(old_nd.path.mnt, old_nd.path.dentry, 0);
    
    
    	if (!mnt)
    		goto out;
    
    	err = graft_tree(mnt, nd);
    	if (err) {
    
    		LIST_HEAD(umount_list);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		spin_lock(&vfsmount_lock);
    
    		umount_tree(mnt, 0, &umount_list);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		spin_unlock(&vfsmount_lock);
    
    		release_mounts(&umount_list);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	up_write(&namespace_sem);
    
    Jan Blunck's avatar
    Jan Blunck committed
    	path_put(&old_nd.path);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	return err;
    }
    
    
    static int change_mount_flags(struct vfsmount *mnt, int ms_flags)
    {
    	int error = 0;
    	int readonly_request = 0;
    
    	if (ms_flags & MS_RDONLY)
    		readonly_request = 1;
    	if (readonly_request == __mnt_is_readonly(mnt))
    		return 0;
    
    	if (readonly_request)
    		error = mnt_make_readonly(mnt);
    	else
    		__mnt_unmake_readonly(mnt);
    	return error;
    }
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    /*
     * change filesystem flags. dir should be a physical root of filesystem.
     * If you've mounted a non-root directory somewhere and want to do remount
     * on it - tough luck.
    
     * noinline this do_mount helper to save do_mount stack space.
    
    Linus Torvalds's avatar
    Linus Torvalds committed
     */
    
    static noinline int do_remount(struct nameidata *nd, int flags, int mnt_flags,
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		      void *data)
    {
    	int err;
    
    	struct super_block *sb = nd->path.mnt->mnt_sb;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	if (!capable(CAP_SYS_ADMIN))
    		return -EPERM;
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		return -EINVAL;
    
    
    	if (nd->path.dentry != nd->path.mnt->mnt_root)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		return -EINVAL;
    
    	down_write(&sb->s_umount);
    
    	if (flags & MS_BIND)
    		err = change_mount_flags(nd->path.mnt, flags);
    	else
    		err = do_remount_sb(sb, flags, data, 0);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	if (!err)
    
    		nd->path.mnt->mnt_flags = mnt_flags;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	up_write(&sb->s_umount);
    	if (!err)
    
    		security_sb_post_remount(nd->path.mnt, flags, data);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	return err;
    }
    
    
    Ram Pai's avatar
    Ram Pai committed
    static inline int tree_contains_unbindable(struct vfsmount *mnt)
    {
    	struct vfsmount *p;
    	for (p = mnt; p; p = next_mnt(p, mnt)) {
    		if (IS_MNT_UNBINDABLE(p))
    			return 1;
    	}
    	return 0;
    }
    
    
    /*
     * noinline this do_mount helper to save do_mount stack space.
     */
    static noinline int do_move_mount(struct nameidata *nd, char *old_name)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    
    	struct nameidata old_nd;
    	struct path parent_path;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	struct vfsmount *p;
    	int err = 0;
    	if (!capable(CAP_SYS_ADMIN))
    		return -EPERM;
    	if (!old_name || !*old_name)
    		return -EINVAL;
    	err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd);
    	if (err)
    		return err;
    
    
    	down_write(&namespace_sem);
    
    	while (d_mountpoint(nd->path.dentry) &&
    	       follow_down(&nd->path.mnt, &nd->path.dentry))
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		;
    	err = -EINVAL;
    
    	if (!check_mnt(nd->path.mnt) || !check_mnt(old_nd.path.mnt))
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		goto out;
    
    	err = -ENOENT;
    
    	mutex_lock(&nd->path.dentry->d_inode->i_mutex);
    	if (IS_DEADDIR(nd->path.dentry->d_inode))
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		goto out1;
    
    
    	if (!IS_ROOT(nd->path.dentry) && d_unhashed(nd->path.dentry))
    
    		goto out1;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	err = -EINVAL;
    
    	if (old_nd.path.dentry != old_nd.path.mnt->mnt_root)
    
    		goto out1;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	if (old_nd.path.mnt == old_nd.path.mnt->mnt_parent)
    
    		goto out1;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	if (S_ISDIR(nd->path.dentry->d_inode->i_mode) !=
    	      S_ISDIR(old_nd.path.dentry->d_inode->i_mode))
    
    		goto out1;
    	/*
    	 * Don't move a mount residing in a shared parent.
    	 */
    
    	if (old_nd.path.mnt->mnt_parent &&
    	    IS_MNT_SHARED(old_nd.path.mnt->mnt_parent))
    
    		goto out1;
    
    Ram Pai's avatar
    Ram Pai committed
    	/*
    	 * Don't move a mount tree containing unbindable mounts to a destination
    	 * mount which is shared.
    	 */
    
    	if (IS_MNT_SHARED(nd->path.mnt) &&
    	    tree_contains_unbindable(old_nd.path.mnt))
    
    Ram Pai's avatar
    Ram Pai committed
    		goto out1;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	err = -ELOOP;
    
    	for (p = nd->path.mnt; p->mnt_parent != p; p = p->mnt_parent)
    		if (p == old_nd.path.mnt)
    
    			goto out1;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	err = attach_recursive_mnt(old_nd.path.mnt, &nd->path, &parent_path);
    
    		goto out1;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	/* if the mount is moved, it should no longer be expire
    	 * automatically */
    
    	list_del_init(&old_nd.path.mnt->mnt_expire);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    out1:
    
    	mutex_unlock(&nd->path.dentry->d_inode->i_mutex);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    out:
    
    	up_write(&namespace_sem);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	if (!err)
    
    		path_put(&parent_path);
    
    Jan Blunck's avatar
    Jan Blunck committed
    	path_put(&old_nd.path);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	return err;
    }
    
    /*
     * create a new mount for userspace and request it to be added into the
     * namespace's tree
    
     * noinline this do_mount helper to save do_mount stack space.
    
    Linus Torvalds's avatar
    Linus Torvalds committed
     */
    
    static noinline int do_new_mount(struct nameidata *nd, char *type, int flags,
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			int mnt_flags, char *name, void *data)
    {
    	struct vfsmount *mnt;
    
    	if (!type || !memchr(type, 0, PAGE_SIZE))
    		return -EINVAL;
    
    	/* we need capabilities... */
    	if (!capable(CAP_SYS_ADMIN))
    		return -EPERM;
    
    	mnt = do_kern_mount(type, flags, name, data);
    	if (IS_ERR(mnt))
    		return PTR_ERR(mnt);
    
    	return do_add_mount(mnt, nd, mnt_flags, NULL);
    }
    
    /*
     * add a mount into a namespace's mount tree
     * - provide the option of adding the new mount to an expiration list
     */
    int do_add_mount(struct vfsmount *newmnt, struct nameidata *nd,
    		 int mnt_flags, struct list_head *fslist)
    {
    	int err;
    
    
    	down_write(&namespace_sem);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	/* Something was mounted here while we slept */
    
    	while (d_mountpoint(nd->path.dentry) &&
    	       follow_down(&nd->path.mnt, &nd->path.dentry))
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		;
    	err = -EINVAL;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		goto unlock;
    
    	/* Refuse the same filesystem on the same mount point */
    	err = -EBUSY;
    
    	if (nd->path.mnt->mnt_sb == newmnt->mnt_sb &&
    	    nd->path.mnt->mnt_root == nd->path.dentry)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		goto unlock;
    
    	err = -EINVAL;
    	if (S_ISLNK(newmnt->mnt_root->d_inode->i_mode))
    		goto unlock;
    
    	newmnt->mnt_flags = mnt_flags;
    
    	if ((err = graft_tree(newmnt, nd)))
    		goto unlock;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	if (fslist) /* add to the specified expiration list */
    
    		list_add_tail(&newmnt->mnt_expire, fslist);
    
    	up_write(&namespace_sem);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    unlock:
    
    	up_write(&namespace_sem);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	mntput(newmnt);
    	return err;
    }
    
    EXPORT_SYMBOL_GPL(do_add_mount);
    
    /*
     * process a list of expirable mountpoints with the intent of discarding any
     * mountpoints that aren't in use and haven't been touched since last we came
     * here
     */
    void mark_mounts_for_expiry(struct list_head *mounts)
    {
    	struct vfsmount *mnt, *next;
    	LIST_HEAD(graveyard);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	if (list_empty(mounts))
    		return;
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	spin_lock(&vfsmount_lock);
    
    	/* extract from the expiration list every vfsmount that matches the
    	 * following criteria:
    	 * - only referenced by its parent vfsmount
    	 * - still marked for expiry (marked on the last call here; marks are
    	 *   cleared by mntput())
    	 */
    
    	list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		if (!xchg(&mnt->mnt_expiry_mark, 1) ||
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			continue;
    
    		list_move(&mnt->mnt_expire, &graveyard);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	}
    
    	while (!list_empty(&graveyard)) {
    		mnt = list_first_entry(&graveyard, struct vfsmount, mnt_expire);
    		touch_mnt_namespace(mnt->mnt_ns);
    		umount_tree(mnt, 1, &umounts);
    	}
    
    	spin_unlock(&vfsmount_lock);
    
    	up_write(&namespace_sem);
    
    	release_mounts(&umounts);
    
    }
    
    EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
    
    /*
     * Ripoff of 'select_parent()'
     *
     * search the list of submounts for a given mountpoint, and move any
     * shrinkable submounts to the 'graveyard' list.
     */
    static int select_submounts(struct vfsmount *parent, struct list_head *graveyard)
    {
    	struct vfsmount *this_parent = parent;
    	struct list_head *next;
    	int found = 0;
    
    repeat:
    	next = this_parent->mnt_mounts.next;
    resume:
    	while (next != &this_parent->mnt_mounts) {
    		struct list_head *tmp = next;
    		struct vfsmount *mnt = list_entry(tmp, struct vfsmount, mnt_child);
    
    		next = tmp->next;
    		if (!(mnt->mnt_flags & MNT_SHRINKABLE))
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			continue;
    
    		/*
    		 * Descend a level if the d_mounts list is non-empty.
    		 */
    		if (!list_empty(&mnt->mnt_mounts)) {
    			this_parent = mnt;
    			goto repeat;
    		}
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    		if (!propagate_mount_busy(mnt, 1)) {
    			list_move_tail(&mnt->mnt_expire, graveyard);
    			found++;
    		}
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	}
    
    	/*
    	 * All done at this level ... ascend and resume the search
    	 */
    	if (this_parent != parent) {
    		next = this_parent->mnt_child.next;
    		this_parent = this_parent->mnt_parent;
    		goto resume;
    	}
    	return found;
    }
    
    /*
     * process a list of expirable mountpoints with the intent of discarding any
     * submounts of a specific parent mountpoint
     */
    
    static void shrink_submounts(struct vfsmount *mnt, struct list_head *umounts)
    
    {
    	LIST_HEAD(graveyard);
    
    	struct vfsmount *m;
    
    
    	/* extract submounts of 'mountpoint' from the expiration list */
    
    	while (select_submounts(mnt, &graveyard)) {
    
    		while (!list_empty(&graveyard)) {
    
    			m = list_first_entry(&graveyard, struct vfsmount,
    
    						mnt_expire);
    			touch_mnt_namespace(mnt->mnt_ns);
    
    			umount_tree(mnt, 1, umounts);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    }
    
    /*
     * Some copy_from_user() implementations do not return the exact number of
     * bytes remaining to copy on a fault.  But copy_mount_options() requires that.
     * Note that this function differs from copy_from_user() in that it will oops
     * on bad values of `to', rather than returning a short copy.
     */
    
    Ram Pai's avatar
    Ram Pai committed
    static long exact_copy_from_user(void *to, const void __user * from,
    				 unsigned long n)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    	char *t = to;
    	const char __user *f = from;
    	char c;
    
    	if (!access_ok(VERIFY_READ, from, n))
    		return n;
    
    	while (n) {
    		if (__get_user(c, f)) {
    			memset(t, 0, n);
    			break;
    		}
    		*t++ = c;
    		f++;
    		n--;
    	}
    	return n;
    }
    
    
    Ram Pai's avatar
    Ram Pai committed
    int copy_mount_options(const void __user * data, unsigned long *where)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    	int i;
    	unsigned long page;
    	unsigned long size;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	*where = 0;
    	if (!data)
    		return 0;
    
    	if (!(page = __get_free_page(GFP_KERNEL)))
    		return -ENOMEM;
    
    	/* We only care that *some* data at the address the user
    	 * gave us is valid.  Just in case, we'll zero
    	 * the remainder of the page.
    	 */
    	/* copy_from_user cannot cross TASK_SIZE ! */
    	size = TASK_SIZE - (unsigned long)data;
    	if (size > PAGE_SIZE)
    		size = PAGE_SIZE;
    
    	i = size - exact_copy_from_user((void *)page, data, size);
    	if (!i) {
    
    Ram Pai's avatar
    Ram Pai committed
    		free_page(page);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		return -EFAULT;
    	}
    	if (i != PAGE_SIZE)
    		memset((char *)page + i, 0, PAGE_SIZE - i);
    	*where = page;
    	return 0;
    }
    
    /*
     * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
     * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
     *
     * data is a (void *) that can point to any structure up to
     * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
     * information (or be NULL).
     *
     * Pre-0.97 versions of mount() didn't have a flags word.
     * When the flags word was introduced its top half was required
     * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
     * Therefore, if this magic number is present, it carries no information
     * and must be discarded.
     */
    
    Ram Pai's avatar
    Ram Pai committed
    long do_mount(char *dev_name, char *dir_name, char *type_page,
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		  unsigned long flags, void *data_page)
    {
    	struct nameidata nd;
    	int retval = 0;
    	int mnt_flags = 0;
    
    	/* Discard magic */
    	if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
    		flags &= ~MS_MGC_MSK;
    
    	/* Basic sanity checks */
    
    	if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE))
    		return -EINVAL;
    	if (dev_name && !memchr(dev_name, 0, PAGE_SIZE))
    		return -EINVAL;
    
    	if (data_page)
    		((char *)data_page)[PAGE_SIZE - 1] = 0;
    
    	/* Separate the per-mountpoint flags */
    	if (flags & MS_NOSUID)
    		mnt_flags |= MNT_NOSUID;
    	if (flags & MS_NODEV)
    		mnt_flags |= MNT_NODEV;
    	if (flags & MS_NOEXEC)
    		mnt_flags |= MNT_NOEXEC;
    
    	if (flags & MS_NOATIME)
    		mnt_flags |= MNT_NOATIME;
    	if (flags & MS_NODIRATIME)
    		mnt_flags |= MNT_NODIRATIME;
    
    Valerie Henson's avatar
    Valerie Henson committed
    	if (flags & MS_RELATIME)
    		mnt_flags |= MNT_RELATIME;
    
    	if (flags & MS_RDONLY)
    		mnt_flags |= MNT_READONLY;
    
    
    	flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE |
    
    		   MS_NOATIME | MS_NODIRATIME | MS_RELATIME| MS_KERNMOUNT);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	/* ... and get the mountpoint */
    	retval = path_lookup(dir_name, LOOKUP_FOLLOW, &nd);
    	if (retval)
    		return retval;
    
    
    	retval = security_sb_mount(dev_name, &nd.path,
    				   type_page, flags, data_page);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	if (retval)
    		goto dput_out;
    
    	if (flags & MS_REMOUNT)
    		retval = do_remount(&nd, flags & ~MS_REMOUNT, mnt_flags,
    				    data_page);
    	else if (flags & MS_BIND)
    
    		retval = do_loopback(&nd, dev_name, flags & MS_REC);
    
    Ram Pai's avatar
    Ram Pai committed
    	else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
    
    		retval = do_change_type(&nd, flags);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	else if (flags & MS_MOVE)
    		retval = do_move_mount(&nd, dev_name);
    	else
    		retval = do_new_mount(&nd, type_page, flags, mnt_flags,
    				      dev_name, data_page);
    dput_out:
    
    Jan Blunck's avatar
    Jan Blunck committed
    	path_put(&nd.path);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	return retval;
    }
    
    
    /*
     * Allocate a new namespace structure and populate it with contents
     * copied from the namespace of the passed in task structure.
     */
    
    static struct mnt_namespace *dup_mnt_ns(struct mnt_namespace *mnt_ns,
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    
    	struct mnt_namespace *new_ns;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	struct vfsmount *rootmnt = NULL, *pwdmnt = NULL, *altrootmnt = NULL;
    	struct vfsmount *p, *q;
    
    
    	new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	if (!new_ns)
    
    		return ERR_PTR(-ENOMEM);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	atomic_set(&new_ns->count, 1);
    	INIT_LIST_HEAD(&new_ns->list);
    
    	init_waitqueue_head(&new_ns->poll);
    	new_ns->event = 0;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	down_write(&namespace_sem);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	/* First pass: copy the tree topology */
    
    	new_ns->root = copy_tree(mnt_ns->root, mnt_ns->root->mnt_root,
    
    Ram Pai's avatar
    Ram Pai committed
    					CL_COPY_ALL | CL_EXPIRE);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	if (!new_ns->root) {
    
    		up_write(&namespace_sem);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		kfree(new_ns);
    
    		return ERR_PTR(-ENOMEM);;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	}
    	spin_lock(&vfsmount_lock);
    	list_add_tail(&new_ns->list, &new_ns->root->mnt_list);
    	spin_unlock(&vfsmount_lock);
    
    	/*
    	 * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
    	 * as belonging to new namespace.  We have already acquired a private
    	 * fs_struct, so tsk->fs->lock is not needed.
    	 */
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	q = new_ns->root;
    	while (p) {
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		if (fs) {
    
    Jan Blunck's avatar
    Jan Blunck committed
    			if (p == fs->root.mnt) {
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    				rootmnt = p;
    
    Jan Blunck's avatar
    Jan Blunck committed
    				fs->root.mnt = mntget(q);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			}
    
    Jan Blunck's avatar
    Jan Blunck committed
    			if (p == fs->pwd.mnt) {
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    				pwdmnt = p;
    
    Jan Blunck's avatar
    Jan Blunck committed
    				fs->pwd.mnt = mntget(q);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			}
    
    Jan Blunck's avatar
    Jan Blunck committed
    			if (p == fs->altroot.mnt) {
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    				altrootmnt = p;
    
    Jan Blunck's avatar
    Jan Blunck committed
    				fs->altroot.mnt = mntget(q);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			}
    		}
    
    		p = next_mnt(p, mnt_ns->root);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		q = next_mnt(q, new_ns->root);
    	}
    
    	up_write(&namespace_sem);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	if (rootmnt)
    		mntput(rootmnt);
    	if (pwdmnt)
    		mntput(pwdmnt);
    	if (altrootmnt)
    		mntput(altrootmnt);
    
    
    struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns,
    
    	struct mnt_namespace *new_ns;
    
    
    	if (!(flags & CLONE_NEWNS))
    
    	new_ns = dup_mnt_ns(ns, new_fs);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    }
    
    asmlinkage long sys_mount(char __user * dev_name, char __user * dir_name,
    			  char __user * type, unsigned long flags,
    			  void __user * data)
    {
    	int retval;
    	unsigned long data_page;
    	unsigned long type_page;
    	unsigned long dev_page;
    	char *dir_page;
    
    
    Ram Pai's avatar
    Ram Pai committed
    	retval = copy_mount_options(type, &type_page);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	if (retval < 0)
    		return retval;
    
    	dir_page = getname(dir_name);
    	retval = PTR_ERR(dir_page);
    	if (IS_ERR(dir_page))
    		goto out1;
    
    
    Ram Pai's avatar
    Ram Pai committed
    	retval = copy_mount_options(dev_name, &dev_page);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	if (retval < 0)
    		goto out2;
    
    
    Ram Pai's avatar
    Ram Pai committed
    	retval = copy_mount_options(data, &data_page);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	if (retval < 0)
    		goto out3;
    
    	lock_kernel();
    
    Ram Pai's avatar
    Ram Pai committed
    	retval = do_mount((char *)dev_page, dir_page, (char *)type_page,
    			  flags, (void *)data_page);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	unlock_kernel();
    	free_page(data_page);
    
    out3:
    	free_page(dev_page);
    out2:
    	putname(dir_page);
    out1:
    	free_page(type_page);
    	return retval;
    }
    
    /*
     * Replace the fs->{rootmnt,root} with {mnt,dentry}. Put the old values.
     * It can block. Requires the big lock held.
     */
    
    void set_fs_root(struct fs_struct *fs, struct path *path)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    
    Jan Blunck's avatar
    Jan Blunck committed
    	struct path old_root;
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	write_lock(&fs->lock);
    	old_root = fs->root;
    
    	fs->root = *path;
    	path_get(path);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	write_unlock(&fs->lock);
    
    Jan Blunck's avatar
    Jan Blunck committed
    	if (old_root.dentry)
    		path_put(&old_root);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    }
    
    /*
     * Replace the fs->{pwdmnt,pwd} with {mnt,dentry}. Put the old values.
     * It can block. Requires the big lock held.
     */
    
    void set_fs_pwd(struct fs_struct *fs, struct path *path)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    
    Jan Blunck's avatar
    Jan Blunck committed
    	struct path old_pwd;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	write_lock(&fs->lock);
    	old_pwd = fs->pwd;
    
    	fs->pwd = *path;
    	path_get(path);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	write_unlock(&fs->lock);
    
    
    Jan Blunck's avatar
    Jan Blunck committed
    	if (old_pwd.dentry)
    		path_put(&old_pwd);
    
    static void chroot_fs_refs(struct path *old_root, struct path *new_root)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    	struct task_struct *g, *p;
    	struct fs_struct *fs;
    
    	read_lock(&tasklist_lock);
    	do_each_thread(g, p) {
    		task_lock(p);
    		fs = p->fs;
    		if (fs) {
    			atomic_inc(&fs->count);
    			task_unlock(p);
    
    			if (fs->root.dentry == old_root->dentry
    			    && fs->root.mnt == old_root->mnt)
    				set_fs_root(fs, new_root);
    			if (fs->pwd.dentry == old_root->dentry
    			    && fs->pwd.mnt == old_root->mnt)
    				set_fs_pwd(fs, new_root);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			put_fs_struct(fs);
    		} else
    			task_unlock(p);
    	} while_each_thread(g, p);
    	read_unlock(&tasklist_lock);
    }
    
    /*
     * pivot_root Semantics:
     * Moves the root file system of the current process to the directory put_old,
     * makes new_root as the new root file system of the current process, and sets
     * root/cwd of all processes which had them on the current root to new_root.
     *
     * Restrictions:
     * The new_root and put_old must be directories, and  must not be on the
     * same file  system as the current process root. The put_old  must  be
     * underneath new_root,  i.e. adding a non-zero number of /.. to the string
     * pointed to by put_old must yield the same directory as new_root. No other
     * file system may be mounted on put_old. After all, new_root is a mountpoint.
     *
    
     * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem.
     * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives
     * in this situation.
     *
    
    Linus Torvalds's avatar
    Linus Torvalds committed
     * Notes:
     *  - we don't move root/cwd if they are not at the root (reason: if something
     *    cared enough to change them, it's probably wrong to force them elsewhere)
     *  - it's okay to pick a root that isn't the root of a file system, e.g.
     *    /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
     *    though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
     *    first.
     */
    
    Ram Pai's avatar
    Ram Pai committed
    asmlinkage long sys_pivot_root(const char __user * new_root,
    			       const char __user * put_old)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    	struct vfsmount *tmp;
    
    	struct nameidata new_nd, old_nd, user_nd;
    	struct path parent_path, root_parent;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	int error;
    
    	if (!capable(CAP_SYS_ADMIN))
    		return -EPERM;
    
    	lock_kernel();
    
    
    Ram Pai's avatar
    Ram Pai committed
    	error = __user_walk(new_root, LOOKUP_FOLLOW | LOOKUP_DIRECTORY,
    			    &new_nd);