Skip to content
Snippets Groups Projects
namespace.c 46.8 KiB
Newer Older
  • Learn to ignore specific revisions
  • Linus Torvalds's avatar
    Linus Torvalds committed
    	err = -EINVAL;
    	if (!check_mnt(nd->mnt) || !check_mnt(old_nd.mnt))
    		goto out;
    
    	err = -ENOENT;
    
    	mutex_lock(&nd->dentry->d_inode->i_mutex);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	if (IS_DEADDIR(nd->dentry->d_inode))
    		goto out1;
    
    	if (!IS_ROOT(nd->dentry) && d_unhashed(nd->dentry))
    
    		goto out1;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	err = -EINVAL;
    	if (old_nd.dentry != old_nd.mnt->mnt_root)
    
    		goto out1;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	if (old_nd.mnt == old_nd.mnt->mnt_parent)
    
    		goto out1;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	if (S_ISDIR(nd->dentry->d_inode->i_mode) !=
    	      S_ISDIR(old_nd.dentry->d_inode->i_mode))
    
    		goto out1;
    	/*
    	 * Don't move a mount residing in a shared parent.
    	 */
    	if (old_nd.mnt->mnt_parent && IS_MNT_SHARED(old_nd.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->mnt) && tree_contains_unbindable(old_nd.mnt))
    		goto out1;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	err = -ELOOP;
    
    Ram Pai's avatar
    Ram Pai committed
    	for (p = nd->mnt; p->mnt_parent != p; p = p->mnt_parent)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		if (p == old_nd.mnt)
    
    			goto out1;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	if ((err = attach_recursive_mnt(old_nd.mnt, nd, &parent_nd)))
    		goto out1;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	spin_lock(&vfsmount_lock);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	/* if the mount is moved, it should no longer be expire
    	 * automatically */
    
    	list_del_init(&old_nd.mnt->mnt_expire);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	spin_unlock(&vfsmount_lock);
    out1:
    
    	mutex_unlock(&nd->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_release(&parent_nd);
    	path_release(&old_nd);
    	return err;
    }
    
    /*
     * create a new mount for userspace and request it to be added into the
     * namespace's tree
     */
    static int do_new_mount(struct nameidata *nd, char *type, int flags,
    			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 */
    
    Ram Pai's avatar
    Ram Pai committed
    	while (d_mountpoint(nd->dentry) && follow_down(&nd->mnt, &nd->dentry))
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		;
    	err = -EINVAL;
    	if (!check_mnt(nd->mnt))
    		goto unlock;
    
    	/* Refuse the same filesystem on the same mount point */
    	err = -EBUSY;
    	if (nd->mnt->mnt_sb == newmnt->mnt_sb &&
    	    nd->mnt->mnt_root == nd->dentry)
    		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
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		/* add to the specified expiration list */
    		spin_lock(&vfsmount_lock);
    
    		list_add_tail(&newmnt->mnt_expire, fslist);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		spin_unlock(&vfsmount_lock);
    	}
    
    	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);
    
    
    static void expire_mount(struct vfsmount *mnt, struct list_head *mounts,
    				struct list_head *umounts)
    
    	/*
    	 * Check if mount is still attached, if not, let whoever holds it deal
    	 * with the sucker
    	 */
    	if (mnt->mnt_parent == mnt) {
    		spin_unlock(&vfsmount_lock);
    		return;
    	}
    
    
    	/*
    	 * Check that it is still dead: the count should now be 2 - as
    	 * contributed by the vfsmount parent and the mntget above
    	 */
    
    	if (!propagate_mount_busy(mnt, 2)) {
    
    		/* delete from the namespace */
    
    		touch_namespace(mnt->mnt_namespace);
    
    		list_del_init(&mnt->mnt_list);
    
    		umount_tree(mnt, 1, umounts);
    
    		spin_unlock(&vfsmount_lock);
    	} else {
    		/*
    		 * Someone brought it back to life whilst we didn't have any
    		 * locks held so return it to the expiration list
    		 */
    
    		list_add_tail(&mnt->mnt_expire, mounts);
    
    /*
     * go through the vfsmounts we've just consigned to the graveyard to
     * - check that they're still dead
     * - delete the vfsmount from the appropriate namespace under lock
     * - dispose of the corpse
     */
    static void expire_mount_list(struct list_head *graveyard, struct list_head *mounts)
    {
    	struct namespace *namespace;
    	struct vfsmount *mnt;
    
    	while (!list_empty(graveyard)) {
    		LIST_HEAD(umounts);
    		mnt = list_entry(graveyard->next, struct vfsmount, mnt_expire);
    		list_del_init(&mnt->mnt_expire);
    
    		/* don't do anything if the namespace is dead - all the
    		 * vfsmounts from it are going away anyway */
    		namespace = mnt->mnt_namespace;
    		if (!namespace || !namespace->root)
    			continue;
    		get_namespace(namespace);
    
    		spin_unlock(&vfsmount_lock);
    		down_write(&namespace_sem);
    		expire_mount(mnt, mounts, &umounts);
    		up_write(&namespace_sem);
    		release_mounts(&umounts);
    		mntput(mnt);
    		put_namespace(namespace);
    		spin_lock(&vfsmount_lock);
    	}
    }
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    /*
     * 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);
    
    	if (list_empty(mounts))
    		return;
    
    	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) ||
    		    atomic_read(&mnt->mnt_count) != 1)
    			continue;
    
    		mntget(mnt);
    
    		list_move(&mnt->mnt_expire, &graveyard);
    
    	expire_mount_list(&graveyard, mounts);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	spin_unlock(&vfsmount_lock);
    }
    
    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)) {
    			mntget(mnt);
    			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
     */
    void shrink_submounts(struct vfsmount *mountpoint, struct list_head *mounts)
    {
    	LIST_HEAD(graveyard);
    	int found;
    
    	spin_lock(&vfsmount_lock);
    
    	/* extract submounts of 'mountpoint' from the expiration list */
    	while ((found = select_submounts(mountpoint, &graveyard)) != 0)
    		expire_mount_list(&graveyard, mounts);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	spin_unlock(&vfsmount_lock);
    }
    
    
    EXPORT_SYMBOL_GPL(shrink_submounts);
    
    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;
    
    	flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE |
    		   MS_NOATIME | MS_NODIRATIME);
    
    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, type_page, flags, data_page);
    	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:
    	path_release(&nd);
    	return retval;
    }
    
    
    /*
     * Allocate a new namespace structure and populate it with contents
     * copied from the namespace of the passed in task structure.
     */
    struct namespace *dup_namespace(struct task_struct *tsk, struct fs_struct *fs)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    	struct namespace *namespace = tsk->namespace;
    	struct namespace *new_ns;
    	struct vfsmount *rootmnt = NULL, *pwdmnt = NULL, *altrootmnt = NULL;
    	struct vfsmount *p, *q;
    
    	new_ns = kmalloc(sizeof(struct namespace), GFP_KERNEL);
    	if (!new_ns)
    
    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 */
    
    Ram Pai's avatar
    Ram Pai committed
    	new_ns->root = copy_tree(namespace->root, namespace->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);
    
    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.
    	 */
    	p = namespace->root;
    	q = new_ns->root;
    	while (p) {
    		q->mnt_namespace = new_ns;
    		if (fs) {
    			if (p == fs->rootmnt) {
    				rootmnt = p;
    				fs->rootmnt = mntget(q);
    			}
    			if (p == fs->pwdmnt) {
    				pwdmnt = p;
    				fs->pwdmnt = mntget(q);
    			}
    			if (p == fs->altrootmnt) {
    				altrootmnt = p;
    				fs->altrootmnt = mntget(q);
    			}
    		}
    		p = next_mnt(p, namespace->root);
    		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);
    
    
    	return new_ns;
    }
    
    int copy_namespace(int flags, struct task_struct *tsk)
    {
    	struct namespace *namespace = tsk->namespace;
    	struct namespace *new_ns;
    	int err = 0;
    
    	if (!namespace)
    		return 0;
    
    	get_namespace(namespace);
    
    	if (!(flags & CLONE_NEWNS))
    		return 0;
    
    	if (!capable(CAP_SYS_ADMIN)) {
    		err = -EPERM;
    		goto out;
    	}
    
    	new_ns = dup_namespace(tsk, tsk->fs);
    	if (!new_ns) {
    		err = -ENOMEM;
    		goto out;
    	}
    
    	tsk->namespace = new_ns;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    out:
    	put_namespace(namespace);
    
    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 vfsmount *mnt,
    		 struct dentry *dentry)
    {
    	struct dentry *old_root;
    	struct vfsmount *old_rootmnt;
    	write_lock(&fs->lock);
    	old_root = fs->root;
    	old_rootmnt = fs->rootmnt;
    	fs->rootmnt = mntget(mnt);
    	fs->root = dget(dentry);
    	write_unlock(&fs->lock);
    	if (old_root) {
    		dput(old_root);
    		mntput(old_rootmnt);
    	}
    }
    
    /*
     * 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 vfsmount *mnt,
    		struct dentry *dentry)
    {
    	struct dentry *old_pwd;
    	struct vfsmount *old_pwdmnt;
    
    	write_lock(&fs->lock);
    	old_pwd = fs->pwd;
    	old_pwdmnt = fs->pwdmnt;
    	fs->pwdmnt = mntget(mnt);
    	fs->pwd = dget(dentry);
    	write_unlock(&fs->lock);
    
    	if (old_pwd) {
    		dput(old_pwd);
    		mntput(old_pwdmnt);
    	}
    }
    
    static void chroot_fs_refs(struct nameidata *old_nd, struct nameidata *new_nd)
    {
    	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);
    
    Ram Pai's avatar
    Ram Pai committed
    			if (fs->root == old_nd->dentry
    			    && fs->rootmnt == old_nd->mnt)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    				set_fs_root(fs, new_nd->mnt, new_nd->dentry);
    
    Ram Pai's avatar
    Ram Pai committed
    			if (fs->pwd == old_nd->dentry
    			    && fs->pwdmnt == old_nd->mnt)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    				set_fs_pwd(fs, new_nd->mnt, new_nd->dentry);
    			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, parent_nd, root_parent, user_nd;
    	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);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	if (error)
    		goto out0;
    	error = -EINVAL;
    	if (!check_mnt(new_nd.mnt))
    		goto out1;
    
    
    Ram Pai's avatar
    Ram Pai committed
    	error = __user_walk(put_old, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &old_nd);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	if (error)
    		goto out1;
    
    	error = security_sb_pivotroot(&old_nd, &new_nd);
    	if (error) {
    		path_release(&old_nd);
    		goto out1;
    	}
    
    	read_lock(&current->fs->lock);
    	user_nd.mnt = mntget(current->fs->rootmnt);
    	user_nd.dentry = dget(current->fs->root);
    	read_unlock(&current->fs->lock);
    
    	down_write(&namespace_sem);
    
    	mutex_lock(&old_nd.dentry->d_inode->i_mutex);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	error = -EINVAL;
    
    	if (IS_MNT_SHARED(old_nd.mnt) ||
    		IS_MNT_SHARED(new_nd.mnt->mnt_parent) ||
    		IS_MNT_SHARED(user_nd.mnt->mnt_parent))
    		goto out2;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	if (!check_mnt(user_nd.mnt))
    		goto out2;
    	error = -ENOENT;
    	if (IS_DEADDIR(new_nd.dentry->d_inode))
    		goto out2;
    	if (d_unhashed(new_nd.dentry) && !IS_ROOT(new_nd.dentry))
    		goto out2;
    	if (d_unhashed(old_nd.dentry) && !IS_ROOT(old_nd.dentry))
    		goto out2;
    	error = -EBUSY;
    	if (new_nd.mnt == user_nd.mnt || old_nd.mnt == user_nd.mnt)
    		goto out2; /* loop, on the same file system  */
    	error = -EINVAL;
    	if (user_nd.mnt->mnt_root != user_nd.dentry)
    		goto out2; /* not a mountpoint */
    
    	if (user_nd.mnt->mnt_parent == user_nd.mnt)
    		goto out2; /* not attached */
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	if (new_nd.mnt->mnt_root != new_nd.dentry)
    		goto out2; /* not a mountpoint */
    
    	if (new_nd.mnt->mnt_parent == new_nd.mnt)
    		goto out2; /* not attached */
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	tmp = old_nd.mnt; /* make sure we can reach put_old from new_root */
    	spin_lock(&vfsmount_lock);
    	if (tmp != new_nd.mnt) {
    		for (;;) {
    			if (tmp->mnt_parent == tmp)
    				goto out3; /* already mounted on put_old */
    			if (tmp->mnt_parent == new_nd.mnt)
    				break;
    			tmp = tmp->mnt_parent;
    		}
    		if (!is_subdir(tmp->mnt_mountpoint, new_nd.dentry))
    			goto out3;
    	} else if (!is_subdir(old_nd.dentry, new_nd.dentry))
    		goto out3;
    	detach_mnt(new_nd.mnt, &parent_nd);
    	detach_mnt(user_nd.mnt, &root_parent);
    	attach_mnt(user_nd.mnt, &old_nd);     /* mount old root on put_old */
    	attach_mnt(new_nd.mnt, &root_parent); /* mount new_root on / */
    
    	touch_namespace(current->namespace);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	spin_unlock(&vfsmount_lock);
    	chroot_fs_refs(&user_nd, &new_nd);
    	security_sb_post_pivotroot(&user_nd, &new_nd);
    	error = 0;
    	path_release(&root_parent);
    	path_release(&parent_nd);
    out2:
    
    	mutex_unlock(&old_nd.dentry->d_inode->i_mutex);
    
    	up_write(&namespace_sem);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	path_release(&user_nd);
    	path_release(&old_nd);
    out1:
    	path_release(&new_nd);
    out0:
    	unlock_kernel();
    	return error;
    out3:
    	spin_unlock(&vfsmount_lock);
    	goto out2;
    }
    
    static void __init init_mount_tree(void)
    {
    	struct vfsmount *mnt;
    	struct namespace *namespace;
    	struct task_struct *g, *p;
    
    	mnt = do_kern_mount("rootfs", 0, "rootfs", NULL);
    	if (IS_ERR(mnt))
    		panic("Can't create rootfs");
    	namespace = kmalloc(sizeof(*namespace), GFP_KERNEL);
    	if (!namespace)
    		panic("Can't allocate initial namespace");
    	atomic_set(&namespace->count, 1);
    	INIT_LIST_HEAD(&namespace->list);
    
    	init_waitqueue_head(&namespace->poll);
    	namespace->event = 0;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	list_add(&mnt->mnt_list, &namespace->list);
    	namespace->root = mnt;
    	mnt->mnt_namespace = namespace;
    
    	init_task.namespace = namespace;
    	read_lock(&tasklist_lock);
    	do_each_thread(g, p) {
    		get_namespace(namespace);
    		p->namespace = namespace;
    	} while_each_thread(g, p);
    	read_unlock(&tasklist_lock);
    
    	set_fs_pwd(current->fs, namespace->root, namespace->root->mnt_root);
    	set_fs_root(current->fs, namespace->root, namespace->root->mnt_root);
    }
    
    void __init mnt_init(unsigned long mempages)
    {
    	struct list_head *d;
    	unsigned int nr_hash;
    	int i;
    
    
    	init_rwsem(&namespace_sem);
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct vfsmount),
    
    Ram Pai's avatar
    Ram Pai committed
    			0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL, NULL);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    Ram Pai's avatar
    Ram Pai committed
    	mount_hashtable = (struct list_head *)__get_free_page(GFP_ATOMIC);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	if (!mount_hashtable)
    		panic("Failed to allocate mount hash table\n");
    
    	/*
    	 * Find the power-of-two list-heads that can fit into the allocation..
    	 * We don't guarantee that "sizeof(struct list_head)" is necessarily
    	 * a power-of-two.
    	 */
    	nr_hash = PAGE_SIZE / sizeof(struct list_head);
    	hash_bits = 0;
    	do {
    		hash_bits++;
    	} while ((nr_hash >> hash_bits) != 0);
    	hash_bits--;
    
    	/*
    	 * Re-calculate the actual number of entries and the mask
    	 * from the number of bits we can fit.
    	 */
    	nr_hash = 1UL << hash_bits;
    
    Ram Pai's avatar
    Ram Pai committed
    	hash_mask = nr_hash - 1;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	printk("Mount-cache hash table entries: %d\n", nr_hash);
    
    	/* And initialize the newly allocated array */
    	d = mount_hashtable;
    	i = nr_hash;
    	do {
    		INIT_LIST_HEAD(d);
    		d++;
    		i--;
    	} while (i);
    	sysfs_init();
    
    Miklos Szeredi's avatar
    Miklos Szeredi committed
    	subsystem_register(&fs_subsys);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	init_rootfs();
    	init_mount_tree();
    }
    
    void __put_namespace(struct namespace *namespace)
    {
    
    	struct vfsmount *root = namespace->root;
    
    	LIST_HEAD(umount_list);
    
    	namespace->root = NULL;
    	spin_unlock(&vfsmount_lock);
    
    	down_write(&namespace_sem);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	spin_lock(&vfsmount_lock);
    
    	umount_tree(root, 0, &umount_list);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	spin_unlock(&vfsmount_lock);
    
    	up_write(&namespace_sem);
    
    	release_mounts(&umount_list);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	kfree(namespace);
    }