Skip to content
Snippets Groups Projects
namespace.c 71.2 KiB
Newer Older
  • Learn to ignore specific revisions
  • EXPORT_SYMBOL(generic_show_options);
    
    /*
     * If filesystem uses generic_show_options(), this function should be
     * called from the fill_super() callback.
     *
     * The .remount_fs callback usually needs to be handled in a special
     * way, to make sure, that previous options are not overwritten if the
     * remount fails.
     *
     * Also note, that if the filesystem's .remount_fs function doesn't
     * reset all options to their default value, but changes only newly
     * given options, then the displayed options will not reflect reality
     * any more.
     */
    void save_mount_options(struct super_block *sb, char *options)
    {
    
    	BUG_ON(sb->s_options);
    	rcu_assign_pointer(sb->s_options, kstrdup(options, GFP_KERNEL));
    
    }
    EXPORT_SYMBOL(save_mount_options);
    
    
    void replace_mount_options(struct super_block *sb, char *options)
    {
    	char *old = sb->s_options;
    	rcu_assign_pointer(sb->s_options, options);
    	if (old) {
    		synchronize_rcu();
    		kfree(old);
    	}
    }
    EXPORT_SYMBOL(replace_mount_options);
    
    
    /* iterator; we want it to have access to namespace_sem, thus here... */
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    static void *m_start(struct seq_file *m, loff_t *pos)
    {
    
    	struct proc_mounts *p = proc_mounts(m);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	down_read(&namespace_sem);
    
    	return seq_list_start(&p->ns->list, *pos);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    }
    
    static void *m_next(struct seq_file *m, void *v, loff_t *pos)
    {
    
    	struct proc_mounts *p = proc_mounts(m);
    
    	return seq_list_next(v, &p->ns->list, pos);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    }
    
    static void m_stop(struct seq_file *m, void *v)
    {
    
    	up_read(&namespace_sem);
    
    static int m_show(struct seq_file *m, void *v)
    
    	struct proc_mounts *p = proc_mounts(m);
    
    	struct mount *r = list_entry(v, struct mount, mnt_list);
    
    	return p->show(m, &r->mnt);
    
    const struct seq_operations mounts_op = {
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	.start	= m_start,
    	.next	= m_next,
    	.stop	= m_stop,
    
    #endif  /* CONFIG_PROC_FS */
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    /**
     * may_umount_tree - check if a mount tree is busy
     * @mnt: root of mount tree
     *
     * This is called to check if a tree of mounts has any
     * open files, pwds, chroots or sub mounts that are
     * busy.
     */
    
    int may_umount_tree(struct vfsmount *m)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    
    	struct mount *mnt = real_mount(m);
    
    Ram Pai's avatar
    Ram Pai committed
    	int actual_refs = 0;
    	int minimum_refs = 0;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    Nicholas Piggin's avatar
    Nicholas Piggin committed
    	/* write lock needed for mnt_get_count */
    
    	br_write_lock(&vfsmount_lock);
    
    	for (p = mnt; p; p = next_mnt(p, mnt)) {
    
    		actual_refs += mnt_get_count(p);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		minimum_refs += 2;
    	}
    
    	br_write_unlock(&vfsmount_lock);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	if (actual_refs > minimum_refs)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    }
    
    EXPORT_SYMBOL(may_umount_tree);
    
    /**
     * may_umount - check if a mount point is busy
     * @mnt: root of mount
     *
     * This is called to check if a mount point has any
     * open files, pwds, chroots or sub mounts. If the
     * mount has sub mounts this will return busy
     * regardless of whether the sub mounts are busy.
     *
     * Doesn't take quota and stuff into account. IOW, in some cases it will
     * give false negatives. The main reason why it's here is that we need
     * a non-destructive way to look for easily umountable filesystems.
     */
    int may_umount(struct vfsmount *mnt)
    {
    
    	down_read(&namespace_sem);
    
    	br_write_lock(&vfsmount_lock);
    
    	if (propagate_mount_busy(real_mount(mnt), 2))
    
    	br_write_unlock(&vfsmount_lock);
    
    	up_read(&namespace_sem);
    
    	return ret;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    }
    
    EXPORT_SYMBOL(may_umount);
    
    
    static LIST_HEAD(unmounted);	/* protected by namespace_sem */
    
    
    static void namespace_unlock(void)
    
    	LIST_HEAD(head);
    
    	if (likely(list_empty(&unmounted))) {
    		up_write(&namespace_sem);
    		return;
    	}
    
    	list_splice_init(&unmounted, &head);
    	up_write(&namespace_sem);
    
    	while (!list_empty(&head)) {
    		mnt = list_first_entry(&head, struct mount, mnt_hash);
    
    		list_del_init(&mnt->mnt_hash);
    
    		if (mnt_has_parent(mnt)) {
    
    			struct dentry *dentry;
    
    			struct mount *m;
    
    			br_write_lock(&vfsmount_lock);
    
    			dentry = mnt->mnt_mountpoint;
    
    			m = mnt->mnt_parent;
    
    			mnt->mnt_mountpoint = mnt->mnt.mnt_root;
    
    			m->mnt_ghosts--;
    
    			br_write_unlock(&vfsmount_lock);
    
    			dput(dentry);
    
    			mntput(&m->mnt);
    
    static inline void namespace_lock(void)
    
    	down_write(&namespace_sem);
    
    /*
     * vfsmount lock must be held for write
     * namespace_sem must be held for write
     */
    
    void umount_tree(struct mount *mnt, int propagate)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    
    Al Viro's avatar
    Al Viro committed
    	LIST_HEAD(tmp_list);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	for (p = mnt; p; p = next_mnt(p, mnt))
    
    		list_move(&p->mnt_hash, &tmp_list);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	if (propagate)
    
    Al Viro's avatar
    Al Viro committed
    		propagate_umount(&tmp_list);
    
    	list_for_each_entry(p, &tmp_list, mnt_hash) {
    
    		list_del_init(&p->mnt_list);
    
    		__touch_mnt_namespace(p->mnt_ns);
    		p->mnt_ns = NULL;
    
    		list_del_init(&p->mnt_child);
    
    		if (mnt_has_parent(p)) {
    
    			p->mnt_parent->mnt_ghosts++;
    
    			put_mountpoint(p->mnt_mp);
    			p->mnt_mp = NULL;
    
    		change_mnt_propagation(p, MS_PRIVATE);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	}
    
    	list_splice(&tmp_list, &unmounted);
    
    static void shrink_submounts(struct mount *mnt);
    
    static int do_umount(struct mount *mnt, int flags)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    
    	struct super_block *sb = mnt->mnt.mnt_sb;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	int retval;
    
    
    	retval = security_sb_umount(&mnt->mnt, flags);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	if (retval)
    		return retval;
    
    	/*
    	 * Allow userspace to request a mountpoint be expired rather than
    	 * unmounting unconditionally. Unmount only happens if:
    	 *  (1) the mark is already set (the mark is cleared by mntput())
    	 *  (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
    	 */
    	if (flags & MNT_EXPIRE) {
    
    		if (&mnt->mnt == current->fs->root.mnt ||
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		    flags & (MNT_FORCE | MNT_DETACH))
    			return -EINVAL;
    
    
    Nicholas Piggin's avatar
    Nicholas Piggin committed
    		/*
    		 * probably don't strictly need the lock here if we examined
    		 * all race cases, but it's a slowpath.
    		 */
    
    		br_write_lock(&vfsmount_lock);
    
    		if (mnt_get_count(mnt) != 2) {
    
    			br_write_unlock(&vfsmount_lock);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			return -EBUSY;
    
    		br_write_unlock(&vfsmount_lock);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    		if (!xchg(&mnt->mnt_expiry_mark, 1))
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			return -EAGAIN;
    	}
    
    	/*
    	 * If we may have to abort operations to get out of this
    	 * mount, and they will themselves hold resources we must
    	 * allow the fs to do things. In the Unix tradition of
    	 * 'Gee thats tricky lets do it in userspace' the umount_begin
    	 * might fail to complete on the first run through as other tasks
    	 * must return, and the like. Thats for the mount program to worry
    	 * about for the moment.
    	 */
    
    
    	if (flags & MNT_FORCE && sb->s_op->umount_begin) {
    		sb->s_op->umount_begin(sb);
    	}
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	/*
    	 * No sense to grab the lock for this test, but test itself looks
    	 * somewhat bogus. Suggestions for better replacement?
    	 * Ho-hum... In principle, we might treat that as umount + switch
    	 * to rootfs. GC would eventually take care of the old vfsmount.
    	 * Actually it makes sense, especially if rootfs would contain a
    	 * /reboot - static binary that would close all descriptors and
    	 * call reboot(9). Then init(8) could umount root and exec /reboot.
    	 */
    
    	if (&mnt->mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) {
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		/*
    		 * Special case for "unmounting" root ...
    		 * we just try to remount it readonly.
    		 */
    		down_write(&sb->s_umount);
    
    		if (!(sb->s_flags & MS_RDONLY))
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			retval = do_remount_sb(sb, MS_RDONLY, NULL, 0);
    		up_write(&sb->s_umount);
    		return retval;
    	}
    
    
    	namespace_lock();
    
    	br_write_lock(&vfsmount_lock);
    
    	event++;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	if (!(flags & MNT_DETACH))
    
    		shrink_submounts(mnt);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	retval = -EBUSY;
    
    	if (flags & MNT_DETACH || !propagate_mount_busy(mnt, 2)) {
    
    		if (!list_empty(&mnt->mnt_list))
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		retval = 0;
    	}
    
    	br_write_unlock(&vfsmount_lock);
    
    	namespace_unlock();
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	return retval;
    }
    
    
    /* 
     * Is the caller allowed to modify his namespace?
     */
    static inline bool may_mount(void)
    {
    	return ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN);
    }
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    /*
     * Now umount can handle mount points as well as block devices.
     * This is important for filesystems which use unnamed block devices.
     *
     * We now support a flag for forced unmount like the other 'big iron'
     * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
     */
    
    
    SYSCALL_DEFINE2(umount, char __user *, name, int, flags)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    
    	struct path path;
    
    	struct mount *mnt;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	int retval;
    
    	int lookup_flags = 0;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW))
    		return -EINVAL;
    
    
    	if (!may_mount())
    		return -EPERM;
    
    
    	if (!(flags & UMOUNT_NOFOLLOW))
    		lookup_flags |= LOOKUP_FOLLOW;
    
    
    	retval = user_path_mountpoint_at(AT_FDCWD, name, lookup_flags, &path);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	if (retval)
    		goto out;
    
    	mnt = real_mount(path.mnt);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	retval = -EINVAL;
    
    	if (path.dentry != path.mnt->mnt_root)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		goto dput_and_out;
    
    	if (!check_mnt(mnt))
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		goto dput_and_out;
    
    	if (mnt->mnt.mnt_flags & MNT_LOCKED)
    		goto dput_and_out;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	retval = do_umount(mnt, flags);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    dput_and_out:
    
    	/* we mustn't call path_put() as that would clear mnt_expiry_mark */
    
    	dput(path.dentry);
    
    	mntput_no_expire(mnt);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    out:
    	return retval;
    }
    
    #ifdef __ARCH_WANT_SYS_OLDUMOUNT
    
    /*
    
    Ram Pai's avatar
    Ram Pai committed
     *	The 2.0 compatible umount. No flags.
    
    Linus Torvalds's avatar
    Linus Torvalds committed
     */
    
    SYSCALL_DEFINE1(oldumount, char __user *, name)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    
    Ram Pai's avatar
    Ram Pai committed
    	return sys_umount(name, 0);
    
    static bool is_mnt_ns_file(struct dentry *dentry)
    
    	/* Is this a proxy for a mount namespace? */
    	struct inode *inode = dentry->d_inode;
    
    
    	if (!proc_ns_inode(inode))
    		return false;
    
    
    	if (ei->ns_ops != &mntns_operations)
    		return false;
    
    
    	return true;
    }
    
    static bool mnt_ns_loop(struct dentry *dentry)
    {
    	/* Could bind mounting the mount namespace inode cause a
    	 * mount namespace loop?
    	 */
    	struct mnt_namespace *mnt_ns;
    	if (!is_mnt_ns_file(dentry))
    		return false;
    
    	mnt_ns = get_proc_ns(dentry->d_inode)->ns;
    
    	return current->nsproxy->mnt_ns->seq >= mnt_ns->seq;
    }
    
    
    struct mount *copy_tree(struct mount *mnt, struct dentry *dentry,
    
    Ram Pai's avatar
    Ram Pai committed
    					int flag)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    
    	struct mount *res, *p, *q, *r, *parent;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	if (!(flag & CL_COPY_UNBINDABLE) && IS_MNT_UNBINDABLE(mnt))
    		return ERR_PTR(-EINVAL);
    
    	if (!(flag & CL_COPY_MNT_NS_FILE) && is_mnt_ns_file(dentry))
    
    Ram Pai's avatar
    Ram Pai committed
    	res = q = clone_mnt(mnt, dentry, flag);
    
    	q->mnt.mnt_flags &= ~MNT_LOCKED;
    
    	q->mnt_mountpoint = mnt->mnt_mountpoint;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	p = mnt;
    
    	list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
    
    		if (!is_subdir(r->mnt_mountpoint, dentry))
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			continue;
    
    
    		for (s = r; s; s = next_mnt(s, r)) {
    
    			if (!(flag & CL_COPY_UNBINDABLE) &&
    			    IS_MNT_UNBINDABLE(s)) {
    				s = skip_mnt_tree(s);
    				continue;
    			}
    			if (!(flag & CL_COPY_MNT_NS_FILE) &&
    			    is_mnt_ns_file(s->mnt.mnt_root)) {
    
    Ram Pai's avatar
    Ram Pai committed
    				s = skip_mnt_tree(s);
    				continue;
    			}
    
    			while (p != s->mnt_parent) {
    				p = p->mnt_parent;
    				q = q->mnt_parent;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			}
    
    			q = clone_mnt(p, p->mnt.mnt_root, flag);
    
    			br_write_lock(&vfsmount_lock);
    
    			list_add_tail(&q->mnt_list, &res->mnt_list);
    
    			attach_mnt(q, parent, p->mnt_mp);
    
    			br_write_unlock(&vfsmount_lock);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		}
    	}
    	return res;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	if (res) {
    
    		br_write_lock(&vfsmount_lock);
    
    		br_write_unlock(&vfsmount_lock);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	}
    
    /* Caller should check returned pointer for errors */
    
    
    struct vfsmount *collect_mounts(struct path *path)
    
    	struct mount *tree;
    
    	namespace_lock();
    
    	tree = copy_tree(real_mount(path->mnt), path->dentry,
    			 CL_COPY_ALL | CL_PRIVATE);
    
    		return ERR_CAST(tree);
    
    }
    
    void drop_collected_mounts(struct vfsmount *mnt)
    {
    
    	namespace_lock();
    
    	br_write_lock(&vfsmount_lock);
    
    	br_write_unlock(&vfsmount_lock);
    
    	namespace_unlock();
    
    Al Viro's avatar
    Al Viro committed
    int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
    		   struct vfsmount *root)
    {
    
    	struct mount *mnt;
    
    Al Viro's avatar
    Al Viro committed
    	int res = f(root, arg);
    	if (res)
    		return res;
    
    	list_for_each_entry(mnt, &real_mount(root)->mnt_list, mnt_list) {
    		res = f(&mnt->mnt, arg);
    
    Al Viro's avatar
    Al Viro committed
    		if (res)
    			return res;
    	}
    	return 0;
    }
    
    
    static void cleanup_group_ids(struct mount *mnt, struct mount *end)
    
    	for (p = mnt; p != end; p = next_mnt(p, mnt)) {
    
    		if (p->mnt_group_id && !IS_MNT_SHARED(p))
    
    			mnt_release_group_id(p);
    
    static int invent_group_ids(struct mount *mnt, bool recurse)
    
    	for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) {
    
    		if (!p->mnt_group_id && !IS_MNT_SHARED(p)) {
    
    			int err = mnt_alloc_group_id(p);
    
    				cleanup_group_ids(mnt, p);
    
    /*
     *  @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 mount *source_mnt,
    
    			struct mount *dest_mnt,
    			struct mountpoint *dest_mp,
    			struct path *parent_path)
    
    {
    	LIST_HEAD(tree_list);
    
    	struct mount *child, *p;
    
    	if (IS_MNT_SHARED(dest_mnt)) {
    
    		err = invent_group_ids(source_mnt, true);
    
    	err = propagate_mnt(dest_mnt, dest_mp, source_mnt, &tree_list);
    
    	if (err)
    		goto out_cleanup_ids;
    
    	br_write_lock(&vfsmount_lock);
    
    	if (IS_MNT_SHARED(dest_mnt)) {
    
    		for (p = source_mnt; p; p = next_mnt(p, source_mnt))
    
    	if (parent_path) {
    
    		detach_mnt(source_mnt, parent_path);
    
    		attach_mnt(source_mnt, dest_mnt, dest_mp);
    
    		touch_mnt_namespace(source_mnt->mnt_ns);
    
    	} else {
    
    		mnt_set_mountpoint(dest_mnt, dest_mp, 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);
    
    	br_write_unlock(&vfsmount_lock);
    
    	if (IS_MNT_SHARED(dest_mnt))
    
    		cleanup_group_ids(source_mnt, NULL);
    
    static struct mountpoint *lock_mount(struct path *path)
    
    {
    	struct vfsmount *mnt;
    
    	struct dentry *dentry = path->dentry;
    
    	mutex_lock(&dentry->d_inode->i_mutex);
    	if (unlikely(cant_mount(dentry))) {
    		mutex_unlock(&dentry->d_inode->i_mutex);
    		return ERR_PTR(-ENOENT);
    
    	namespace_lock();
    
    	mnt = lookup_mnt(path);
    
    	if (likely(!mnt)) {
    		struct mountpoint *mp = new_mountpoint(dentry);
    		if (IS_ERR(mp)) {
    
    			namespace_unlock();
    
    			mutex_unlock(&dentry->d_inode->i_mutex);
    			return mp;
    		}
    		return mp;
    	}
    
    	namespace_unlock();
    
    	mutex_unlock(&path->dentry->d_inode->i_mutex);
    	path_put(path);
    	path->mnt = mnt;
    
    	dentry = path->dentry = dget(mnt->mnt_root);
    
    static void unlock_mount(struct mountpoint *where)
    
    	struct dentry *dentry = where->m_dentry;
    	put_mountpoint(where);
    
    	mutex_unlock(&dentry->d_inode->i_mutex);
    
    static int graft_tree(struct mount *mnt, struct mount *p, struct mountpoint *mp)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    
    	if (mnt->mnt.mnt_sb->s_flags & MS_NOUSER)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		return -EINVAL;
    
    
    	if (S_ISDIR(mp->m_dentry->d_inode->i_mode) !=
    
    	      S_ISDIR(mnt->mnt.mnt_root->d_inode->i_mode))
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		return -ENOTDIR;
    
    
    	return attach_recursive_mnt(mnt, p, mp, NULL);
    
    /*
     * Sanity check the flags to change_mnt_propagation.
     */
    
    static int flags_to_propagation_type(int flags)
    {
    
    	int type = flags & ~(MS_REC | MS_SILENT);
    
    
    	/* Fail if any non-propagation flags are set */
    	if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
    		return 0;
    	/* Only one propagation flag should be set */
    	if (!is_power_of_2(type))
    		return 0;
    	return type;
    }
    
    
    /*
     * recursively change the type of the mountpoint.
     */
    
    static int do_change_type(struct path *path, int flag)
    
    	struct mount *mnt = real_mount(path->mnt);
    
    	int recurse = flag & MS_REC;
    
    	if (path->dentry != path->mnt->mnt_root)
    
    	type = flags_to_propagation_type(flag);
    	if (!type)
    		return -EINVAL;
    
    
    	namespace_lock();
    
    	if (type == MS_SHARED) {
    		err = invent_group_ids(mnt, recurse);
    		if (err)
    			goto out_unlock;
    	}
    
    
    	br_write_lock(&vfsmount_lock);
    
    	for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL))
    
    		change_mnt_propagation(m, type);
    
    	br_write_unlock(&vfsmount_lock);
    
    	namespace_unlock();
    
    static bool has_locked_children(struct mount *mnt, struct dentry *dentry)
    {
    	struct mount *child;
    	list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
    		if (!is_subdir(child->mnt_mountpoint, dentry))
    			continue;
    
    		if (child->mnt.mnt_flags & MNT_LOCKED)
    			return true;
    	}
    	return false;
    }
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    /*
     * do loopback mount.
     */
    
    Al Viro's avatar
    Al Viro committed
    static int do_loopback(struct path *path, const char *old_name,
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    
    	struct path old_path;
    
    	struct mount *mnt = NULL, *old, *parent;
    	struct mountpoint *mp;
    
    	int err;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	if (!old_name || !*old_name)
    		return -EINVAL;
    
    	err = kern_path(old_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	if (err)
    		return err;
    
    
    	mp = lock_mount(path);
    	err = PTR_ERR(mp);
    	if (IS_ERR(mp))
    
    	old = real_mount(old_path.mnt);
    
    	parent = real_mount(path->mnt);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	err = -EINVAL;
    
    	if (IS_MNT_UNBINDABLE(old))
    
    	if (!check_mnt(parent) || !check_mnt(old))
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	if (!recurse && has_locked_children(old, old_path.dentry))
    		goto out2;
    
    
    		mnt = copy_tree(old, old_path.dentry, CL_COPY_MNT_NS_FILE);
    
    		mnt = clone_mnt(old, old_path.dentry, 0);
    
    	if (IS_ERR(mnt)) {
    		err = PTR_ERR(mnt);
    
    	mnt->mnt.mnt_flags &= ~MNT_LOCKED;
    
    
    	err = graft_tree(mnt, parent, mp);
    
    		br_write_lock(&vfsmount_lock);
    
    		br_write_unlock(&vfsmount_lock);
    
    	unlock_mount(mp);
    
    	path_put(&old_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 (mnt->mnt_flags & MNT_LOCK_READONLY)
    		return -EPERM;
    
    
    		error = mnt_make_readonly(real_mount(mnt));
    
    		__mnt_unmake_readonly(real_mount(mnt));
    
    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.
     */
    
    static int do_remount(struct path *path, int flags, int mnt_flags,
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		      void *data)
    {
    	int err;
    
    	struct super_block *sb = path->mnt->mnt_sb;
    
    	struct mount *mnt = real_mount(path->mnt);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	if (!check_mnt(mnt))
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		return -EINVAL;
    
    
    	if (path->dentry != path->mnt->mnt_root)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		return -EINVAL;
    
    
    	err = security_sb_remount(sb, data);
    	if (err)
    		return err;
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	down_write(&sb->s_umount);
    
    		err = change_mount_flags(path->mnt, flags);
    
    	else if (!capable(CAP_SYS_ADMIN))
    		err = -EPERM;
    
    		err = do_remount_sb(sb, flags, data, 0);
    
    Al Viro's avatar
    Al Viro committed
    	if (!err) {
    
    		br_write_lock(&vfsmount_lock);
    
    		mnt_flags |= mnt->mnt.mnt_flags & MNT_PROPAGATION_MASK;
    		mnt->mnt.mnt_flags = mnt_flags;
    
    		br_write_unlock(&vfsmount_lock);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	up_write(&sb->s_umount);
    
    		br_write_lock(&vfsmount_lock);
    
    		touch_mnt_namespace(mnt->mnt_ns);
    
    		br_write_unlock(&vfsmount_lock);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	return err;
    }
    
    
    static inline int tree_contains_unbindable(struct mount *mnt)
    
    Ram Pai's avatar
    Ram Pai committed
    {
    
    	for (p = mnt; p; p = next_mnt(p, mnt)) {
    
    		if (IS_MNT_UNBINDABLE(p))
    
    Ram Pai's avatar
    Ram Pai committed
    			return 1;
    	}
    	return 0;
    }
    
    
    Al Viro's avatar
    Al Viro committed
    static int do_move_mount(struct path *path, const char *old_name)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    
    	struct path old_path, parent_path;
    
    	struct mount *p;
    
    	struct mount *old;
    
    	struct mountpoint *mp;
    
    	int err;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	if (!old_name || !*old_name)
    		return -EINVAL;
    
    	err = kern_path(old_name, LOOKUP_FOLLOW, &old_path);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	if (err)
    		return err;
    
    
    	mp = lock_mount(path);
    	err = PTR_ERR(mp);
    	if (IS_ERR(mp))
    
    	old = real_mount(old_path.mnt);
    
    	p = real_mount(path->mnt);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	err = -EINVAL;
    
    	if (!check_mnt(p) || !check_mnt(old))
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		goto out1;
    
    
    	if (old->mnt.mnt_flags & MNT_LOCKED)
    		goto out1;
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	err = -EINVAL;
    
    	if (old_path.dentry != old_path.mnt->mnt_root)
    
    		goto out1;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	if (!mnt_has_parent(old))
    
    		goto out1;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	if (S_ISDIR(path->dentry->d_inode->i_mode) !=
    	      S_ISDIR(old_path.dentry->d_inode->i_mode))
    
    		goto out1;
    	/*
    	 * Don't move a mount residing in a shared parent.
    	 */
    
    	if (IS_MNT_SHARED(old->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(p) && tree_contains_unbindable(old))
    
    Ram Pai's avatar
    Ram Pai committed
    		goto out1;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	err = -ELOOP;
    
    	for (; mnt_has_parent(p); p = p->mnt_parent)
    
    		if (p == old)
    
    			goto out1;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	err = attach_recursive_mnt(old, real_mount(path->mnt), mp, &parent_path);
    
    		goto out1;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	/* if the mount is moved, it should no longer be expire
    	 * automatically */
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    out1:
    
    	unlock_mount(mp);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    out:
    	if (!err)
    
    		path_put(&parent_path);
    
    	path_put(&old_path);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	return err;
    }
    
    
    static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype)
    {
    	int err;
    	const char *subtype = strchr(fstype, '.');
    	if (subtype) {
    		subtype++;
    		err = -EINVAL;
    		if (!subtype[0])
    			goto err;
    	} else
    		subtype = "";
    
    	mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL);
    	err = -ENOMEM;
    	if (!mnt->mnt_sb->s_subtype)
    		goto err;
    	return mnt;
    
     err:
    	mntput(mnt);
    	return ERR_PTR(err);
    }
    
    /*
     * add a mount into a namespace's mount tree
     */
    
    static int do_add_mount(struct mount *newmnt, struct path *path, int mnt_flags)
    
    	struct mountpoint *mp;
    	struct mount *parent;
    
    	int err;
    
    	mnt_flags &= ~(MNT_SHARED | MNT_WRITE_HOLD | MNT_INTERNAL);
    
    
    	mp = lock_mount(path);
    	if (IS_ERR(mp))
    		return PTR_ERR(mp);
    
    	parent = real_mount(path->mnt);
    
    	if (unlikely(!check_mnt(parent))) {
    
    Al Viro's avatar
    Al Viro committed
    		/* that's acceptable only for automounts done in private ns */
    		if (!(mnt_flags & MNT_SHRINKABLE))
    			goto unlock;
    		/* ... and for those we'd better have mountpoint still alive */
    
    		if (!parent->mnt_ns)
    
    Al Viro's avatar
    Al Viro committed
    			goto unlock;
    	}
    
    
    	/* Refuse the same filesystem on the same mount point */
    	err = -EBUSY;
    
    	if (path->mnt->mnt_sb == newmnt->mnt.mnt_sb &&
    
    	    path->mnt->mnt_root == path->dentry)
    		goto unlock;
    
    	err = -EINVAL;
    
    	if (S_ISLNK(newmnt->mnt.mnt_root->d_inode->i_mode))