Skip to content
Snippets Groups Projects
namespace.c 67 KiB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
/*
 * create a new mount for userspace and request it to be added into the
 * namespace's tree
 */
static int do_new_mount(struct path *path, char *type, int flags,
Linus Torvalds's avatar
Linus Torvalds committed
			int mnt_flags, char *name, void *data)
{
	struct vfsmount *mnt;
Linus Torvalds's avatar
Linus Torvalds committed

Linus Torvalds's avatar
Linus Torvalds committed
		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);

	err = do_add_mount(mnt, path, mnt_flags);
	if (err)
		mntput(mnt);
	return err;
int finish_automount(struct vfsmount *m, struct path *path)
{
	int err;
	/* The new mount record should have at least 2 refs to prevent it being
	 * expired before we get a chance to add it
	 */
	BUG_ON(mnt_get_count(m) < 2);

	if (m->mnt_sb == path->mnt->mnt_sb &&
	    m->mnt_root == path->dentry) {
		err = -ELOOP;
		goto fail;
	}

	err = do_add_mount(m, path, path->mnt->mnt_flags | MNT_SHRINKABLE);
	if (!err)
		return 0;
fail:
	/* remove m from any expiration list it may be on */
	if (!list_empty(&m->mnt_expire)) {
		down_write(&namespace_sem);
		br_write_lock(vfsmount_lock);
		list_del_init(&m->mnt_expire);
		br_write_unlock(vfsmount_lock);
		up_write(&namespace_sem);
	mntput(m);
	mntput(m);
/**
 * mnt_set_expiry - Put a mount on an expiration list
 * @mnt: The mount to list.
 * @expiry_list: The list to add the mount to.
 */
void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list)
{
	down_write(&namespace_sem);
	br_write_lock(vfsmount_lock);

	list_add_tail(&mnt->mnt_expire, expiry_list);

	br_write_unlock(vfsmount_lock);
	up_write(&namespace_sem);
}
EXPORT_SYMBOL(mnt_set_expiry);

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 mount *mnt, *next;
Linus Torvalds's avatar
Linus Torvalds committed
	LIST_HEAD(graveyard);
Linus Torvalds's avatar
Linus Torvalds committed

	if (list_empty(mounts))
		return;

	br_write_lock(vfsmount_lock);
Linus Torvalds's avatar
Linus Torvalds committed

	/* 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.mnt_expire) {
		if (!xchg(&mnt->mnt.mnt_expiry_mark, 1) ||
			propagate_mount_busy(&mnt->mnt, 1))
Linus Torvalds's avatar
Linus Torvalds committed
			continue;
		list_move(&mnt->mnt.mnt_expire, &graveyard);
Linus Torvalds's avatar
Linus Torvalds committed
	}
	while (!list_empty(&graveyard)) {
		mnt = list_first_entry(&graveyard, struct mount, mnt.mnt_expire);
		touch_mnt_namespace(mnt->mnt.mnt_ns);
	br_write_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 mount *parent, struct list_head *graveyard)
	struct mount *this_parent = parent;
	struct list_head *next;
	int found = 0;

repeat:
	next = this_parent->mnt.mnt_mounts.next;
resume:
	while (next != &this_parent->mnt.mnt_mounts) {
		struct list_head *tmp = next;
		struct mount *mnt = list_entry(tmp, struct mount, mnt.mnt_child);

		next = tmp->next;
		if (!(mnt->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.mnt_mounts)) {
			this_parent = mnt;
			goto repeat;
		}
Linus Torvalds's avatar
Linus Torvalds committed

		if (!propagate_mount_busy(&mnt->mnt, 1)) {
			list_move_tail(&mnt->mnt.mnt_expire, graveyard);
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.mnt_child.next;
		this_parent = real_mount(this_parent->mnt.mnt_parent);
		goto resume;
	}
	return found;
}

/*
 * process a list of expirable mountpoints with the intent of discarding any
 * submounts of a specific parent mountpoint
 *
 * vfsmount_lock must be held for write
static void shrink_submounts(struct mount *mnt, struct list_head *umounts)
{
	LIST_HEAD(graveyard);
	struct mount *m;

	/* extract submounts of 'mountpoint' from the expiration list */
	while (select_submounts(mnt, &graveyard)) {
		while (!list_empty(&graveyard)) {
			m = list_first_entry(&graveyard, struct mount,
						mnt.mnt_expire);
			touch_mnt_namespace(m->mnt.mnt_ns);
			umount_tree(m, 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;
}

int copy_mount_string(const void __user *data, char **where)
{
	char *tmp;

	if (!data) {
		*where = NULL;
		return 0;
	}

	tmp = strndup_user(data, PAGE_SIZE);
	if (IS_ERR(tmp))
		return PTR_ERR(tmp);

	*where = tmp;
	return 0;
}

Linus Torvalds's avatar
Linus Torvalds committed
/*
 * 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 path path;
Linus Torvalds's avatar
Linus Torvalds committed
	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 (data_page)
		((char *)data_page)[PAGE_SIZE - 1] = 0;

	/* ... and get the mountpoint */
	retval = kern_path(dir_name, LOOKUP_FOLLOW, &path);
	if (retval)
		return retval;

	retval = security_sb_mount(dev_name, &path,
				   type_page, flags, data_page);
	if (retval)
		goto dput_out;

	/* Default to relatime unless overriden */
	if (!(flags & MS_NOATIME))
		mnt_flags |= MNT_RELATIME;
Linus Torvalds's avatar
Linus Torvalds committed
	/* 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;
	if (flags & MS_STRICTATIME)
		mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME);
	if (flags & MS_RDONLY)
		mnt_flags |= MNT_READONLY;
	flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE | MS_BORN |
		   MS_NOATIME | MS_NODIRATIME | MS_RELATIME| MS_KERNMOUNT |
		   MS_STRICTATIME);
Linus Torvalds's avatar
Linus Torvalds committed

	if (flags & MS_REMOUNT)
		retval = do_remount(&path, flags & ~MS_REMOUNT, mnt_flags,
Linus Torvalds's avatar
Linus Torvalds committed
				    data_page);
	else if (flags & MS_BIND)
		retval = do_loopback(&path, 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(&path, flags);
Linus Torvalds's avatar
Linus Torvalds committed
	else if (flags & MS_MOVE)
		retval = do_move_mount(&path, dev_name);
Linus Torvalds's avatar
Linus Torvalds committed
	else
		retval = do_new_mount(&path, type_page, flags, mnt_flags,
Linus Torvalds's avatar
Linus Torvalds committed
				      dev_name, data_page);
dput_out:
Linus Torvalds's avatar
Linus Torvalds committed
	return retval;
}

static struct mnt_namespace *alloc_mnt_ns(void)
{
	struct mnt_namespace *new_ns;

	new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL);
	if (!new_ns)
		return ERR_PTR(-ENOMEM);
	atomic_set(&new_ns->count, 1);
	new_ns->root = NULL;
	INIT_LIST_HEAD(&new_ns->list);
	init_waitqueue_head(&new_ns->poll);
	new_ns->event = 0;
	return new_ns;
}

void mnt_make_longterm(struct vfsmount *mnt)
{
	__mnt_make_longterm(mnt);
}

void mnt_make_shortterm(struct vfsmount *mnt)
{
#ifdef CONFIG_SMP
	if (atomic_add_unless(&mnt->mnt_longterm, -1, 1))
		return;
	br_write_lock(vfsmount_lock);
	atomic_dec(&mnt->mnt_longterm);
	br_write_unlock(vfsmount_lock);
/*
 * 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;
Al Viro's avatar
Al Viro committed
	struct vfsmount *rootmnt = NULL, *pwdmnt = NULL;
Linus Torvalds's avatar
Linus Torvalds committed

	new_ns = alloc_mnt_ns();
	if (IS_ERR(new_ns))
		return new_ns;
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 = copy_tree(real_mount(mnt_ns->root), mnt_ns->root->mnt_root,
Ram Pai's avatar
Ram Pai committed
					CL_COPY_ALL | CL_EXPIRE);
		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
	}
	new_ns->root = &new->mnt;
	br_write_lock(vfsmount_lock);
Linus Torvalds's avatar
Linus Torvalds committed
	list_add_tail(&new_ns->list, &new_ns->root->mnt_list);
	br_write_unlock(vfsmount_lock);
Linus Torvalds's avatar
Linus Torvalds committed

	/*
	 * 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 = real_mount(mnt_ns->root);
Linus Torvalds's avatar
Linus Torvalds committed
	while (p) {
		q->mnt.mnt_ns = new_ns;
		__mnt_make_longterm(&q->mnt);
Linus Torvalds's avatar
Linus Torvalds committed
		if (fs) {
			if (&p->mnt == fs->root.mnt) {
				fs->root.mnt = mntget(&q->mnt);
				__mnt_make_longterm(&q->mnt);
				mnt_make_shortterm(&p->mnt);
				rootmnt = &p->mnt;
Linus Torvalds's avatar
Linus Torvalds committed
			}
			if (&p->mnt == fs->pwd.mnt) {
				fs->pwd.mnt = mntget(&q->mnt);
				__mnt_make_longterm(&q->mnt);
				mnt_make_shortterm(&p->mnt);
				pwdmnt = &p->mnt;
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);
Linus Torvalds's avatar
Linus Torvalds committed
	if (pwdmnt)
		mntput(pwdmnt);
Linus Torvalds's avatar
Linus Torvalds committed

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);
/**
 * create_mnt_ns - creates a private namespace and adds a root filesystem
 * @mnt: pointer to the new root filesystem mountpoint
 */
static struct mnt_namespace *create_mnt_ns(struct vfsmount *mnt)
{
	struct mnt_namespace *new_ns;

	new_ns = alloc_mnt_ns();
	if (!IS_ERR(new_ns)) {
		mnt->mnt_ns = new_ns;
		__mnt_make_longterm(mnt);
		new_ns->root = mnt;
		list_add(&new_ns->list, &new_ns->root->mnt_list);
Al Viro's avatar
Al Viro committed
struct dentry *mount_subtree(struct vfsmount *mnt, const char *name)
{
	struct mnt_namespace *ns;
	struct super_block *s;
Al Viro's avatar
Al Viro committed
	struct path path;
	int err;

	ns = create_mnt_ns(mnt);
	if (IS_ERR(ns))
		return ERR_CAST(ns);

	err = vfs_path_lookup(mnt->mnt_root, mnt,
			name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path);

	put_mnt_ns(ns);

	if (err)
		return ERR_PTR(err);

	/* trade a vfsmount reference for active sb one */
	s = path.mnt->mnt_sb;
	atomic_inc(&s->s_active);
Al Viro's avatar
Al Viro committed
	mntput(path.mnt);
	/* lock the sucker */
	down_write(&s->s_umount);
Al Viro's avatar
Al Viro committed
	/* ... and return the root of (sub)tree on it */
	return path.dentry;
}
EXPORT_SYMBOL(mount_subtree);

SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name,
		char __user *, type, unsigned long, flags, void __user *, data)
Linus Torvalds's avatar
Linus Torvalds committed
{
	int ret;
	char *kernel_type;
	char *kernel_dir;
	char *kernel_dev;
Linus Torvalds's avatar
Linus Torvalds committed
	unsigned long data_page;

	ret = copy_mount_string(type, &kernel_type);
	if (ret < 0)
		goto out_type;
Linus Torvalds's avatar
Linus Torvalds committed

	kernel_dir = getname(dir_name);
	if (IS_ERR(kernel_dir)) {
		ret = PTR_ERR(kernel_dir);
		goto out_dir;
	}
Linus Torvalds's avatar
Linus Torvalds committed

	ret = copy_mount_string(dev_name, &kernel_dev);
	if (ret < 0)
		goto out_dev;
Linus Torvalds's avatar
Linus Torvalds committed

	ret = copy_mount_options(data, &data_page);
	if (ret < 0)
		goto out_data;
Linus Torvalds's avatar
Linus Torvalds committed

	ret = do_mount(kernel_dev, kernel_dir, kernel_type, flags,
		(void *) data_page);
Linus Torvalds's avatar
Linus Torvalds committed

	free_page(data_page);
out_data:
	kfree(kernel_dev);
out_dev:
	putname(kernel_dir);
out_dir:
	kfree(kernel_type);
out_type:
	return ret;
Al Viro's avatar
Al Viro committed
/*
 * Return true if path is reachable from root
 *
 * namespace_sem or vfsmount_lock is held
 */
bool is_path_reachable(struct vfsmount *mnt, struct dentry *dentry,
			 const struct path *root)
{
	while (mnt != root->mnt && mnt_has_parent(mnt)) {
		dentry = mnt->mnt_mountpoint;
		mnt = mnt->mnt_parent;
	}
	return mnt == root->mnt && is_subdir(dentry, root->dentry);
}

int path_is_under(struct path *path1, struct path *path2)
{
	int res;
	br_read_lock(vfsmount_lock);
	res = is_path_reachable(path1->mnt, path1->dentry, path2);
	br_read_unlock(vfsmount_lock);
	return res;
}
EXPORT_SYMBOL(path_is_under);

Linus Torvalds's avatar
Linus Torvalds committed
/*
 * 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.
 */
SYSCALL_DEFINE2(pivot_root, const char __user *, new_root,
		const char __user *, put_old)
Linus Torvalds's avatar
Linus Torvalds committed
{
	struct path new, old, parent_path, root_parent, root;
	struct mount *new_mnt, *root_mnt;
Linus Torvalds's avatar
Linus Torvalds committed
	int error;

	if (!capable(CAP_SYS_ADMIN))
		return -EPERM;

	error = user_path_dir(new_root, &new);
Linus Torvalds's avatar
Linus Torvalds committed
	if (error)
		goto out0;

	error = user_path_dir(put_old, &old);
Linus Torvalds's avatar
Linus Torvalds committed
	if (error)
		goto out1;

	error = security_sb_pivotroot(&old, &new);
	if (error)
		goto out2;
Linus Torvalds's avatar
Linus Torvalds committed

	get_fs_root(current->fs, &root);
	error = lock_mount(&old);
	if (error)
		goto out3;

Linus Torvalds's avatar
Linus Torvalds committed
	error = -EINVAL;
	new_mnt = real_mount(new.mnt);
	root_mnt = real_mount(root.mnt);
	if (IS_MNT_SHARED(old.mnt) ||
		IS_MNT_SHARED(new.mnt->mnt_parent) ||
		IS_MNT_SHARED(root.mnt->mnt_parent))
Al Viro's avatar
Al Viro committed
	if (!check_mnt(root.mnt) || !check_mnt(new.mnt))
Linus Torvalds's avatar
Linus Torvalds committed
	error = -ENOENT;
	if (d_unlinked(new.dentry))
	if (d_unlinked(old.dentry))
Linus Torvalds's avatar
Linus Torvalds committed
	error = -EBUSY;
	if (new.mnt == root.mnt ||
	    old.mnt == root.mnt)
		goto out4; /* loop, on the same file system  */
Linus Torvalds's avatar
Linus Torvalds committed
	error = -EINVAL;
	if (root.mnt->mnt_root != root.dentry)
		goto out4; /* not a mountpoint */
	if (!mnt_has_parent(root.mnt))
		goto out4; /* not attached */
	if (new.mnt->mnt_root != new.dentry)
		goto out4; /* not a mountpoint */
	if (!mnt_has_parent(new.mnt))
		goto out4; /* not attached */
	/* make sure we can reach put_old from new_root */
Al Viro's avatar
Al Viro committed
	if (!is_path_reachable(old.mnt, old.dentry, &new))
Al Viro's avatar
Al Viro committed
	br_write_lock(vfsmount_lock);
	detach_mnt(new_mnt, &parent_path);
	detach_mnt(root_mnt, &root_parent);
	/* mount old root on put_old */
	attach_mnt(root_mnt, &old);
	attach_mnt(new_mnt, &root_parent);
	touch_mnt_namespace(current->nsproxy->mnt_ns);
	br_write_unlock(vfsmount_lock);
	chroot_fs_refs(&root, &new);
Linus Torvalds's avatar
Linus Torvalds committed
	error = 0;
out4:
	unlock_mount(&old);
	if (!error) {
		path_put(&root_parent);
		path_put(&parent_path);
	}
out3:
	path_put(&old);
Linus Torvalds's avatar
Linus Torvalds committed
out1:
	path_put(&new);
Linus Torvalds's avatar
Linus Torvalds committed
out0:
	return error;
}

static void __init init_mount_tree(void)
{
	struct vfsmount *mnt;
	struct mnt_namespace *ns;
	struct path root;
Linus Torvalds's avatar
Linus Torvalds committed

	mnt = do_kern_mount("rootfs", 0, "rootfs", NULL);
	if (IS_ERR(mnt))
		panic("Can't create rootfs");
	ns = create_mnt_ns(mnt);
	if (IS_ERR(ns))
Linus Torvalds's avatar
Linus Torvalds committed
		panic("Can't allocate initial namespace");

	init_task.nsproxy->mnt_ns = ns;
	get_mnt_ns(ns);

	root.mnt = ns->root;
	root.dentry = ns->root->mnt_root;

	set_fs_pwd(current->fs, &root);
	set_fs_root(current->fs, &root);
void __init mnt_init(void)
Linus Torvalds's avatar
Linus Torvalds committed
{
	unsigned u;
Linus Torvalds's avatar
Linus Torvalds committed

	init_rwsem(&namespace_sem);

	mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount),
			0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, 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");

	printk(KERN_INFO "Mount-cache hash table entries: %lu\n", HASH_SIZE);

	for (u = 0; u < HASH_SIZE; u++)
		INIT_LIST_HEAD(&mount_hashtable[u]);
Linus Torvalds's avatar
Linus Torvalds committed

	br_lock_init(vfsmount_lock);

	err = sysfs_init();
	if (err)
		printk(KERN_WARNING "%s: sysfs_init error: %d\n",
	fs_kobj = kobject_create_and_add("fs", NULL);
	if (!fs_kobj)
		printk(KERN_WARNING "%s: kobj create error\n", __func__);
Linus Torvalds's avatar
Linus Torvalds committed
	init_rootfs();
	init_mount_tree();
}

void put_mnt_ns(struct mnt_namespace *ns)
Linus Torvalds's avatar
Linus Torvalds committed
{
	LIST_HEAD(umount_list);
	if (!atomic_dec_and_test(&ns->count))
	down_write(&namespace_sem);
	br_write_lock(vfsmount_lock);
	umount_tree(real_mount(ns->root), 0, &umount_list);
	br_write_unlock(vfsmount_lock);
	up_write(&namespace_sem);
	release_mounts(&umount_list);
Linus Torvalds's avatar
Linus Torvalds committed
}

struct vfsmount *kern_mount_data(struct file_system_type *type, void *data)
{
	struct vfsmount *mnt;
	mnt = vfs_kern_mount(type, MS_KERNMOUNT, type->name, data);
	if (!IS_ERR(mnt)) {
		/*
		 * it is a longterm mount, don't release mnt until
		 * we unmount before file sys is unregistered
		*/
		mnt_make_longterm(mnt);
	}
	return mnt;
}
EXPORT_SYMBOL_GPL(kern_mount_data);

void kern_unmount(struct vfsmount *mnt)
{
	/* release long term mount so mount point can be released */
	if (!IS_ERR_OR_NULL(mnt)) {
		mnt_make_shortterm(mnt);
		mntput(mnt);
	}
}
EXPORT_SYMBOL(kern_unmount);

bool our_mnt(struct vfsmount *mnt)
{
	return check_mnt(mnt);
}