Skip to content
Snippets Groups Projects
namespace.c 63.2 KiB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
/*
 *  linux/fs/namespace.c
 *
 * (C) Copyright Al Viro 2000, 2001
 *	Released under GPL v2.
 *
 * Based on code from fs/super.c, copyright Linus Torvalds and others.
 * Heavily rewritten.
 */

#include <linux/syscalls.h>
Al Viro's avatar
Al Viro committed
#include <linux/export.h>
#include <linux/capability.h>
#include <linux/mnt_namespace.h>
Linus Torvalds's avatar
Linus Torvalds committed
#include <linux/namei.h>
#include <linux/security.h>
#include <linux/idr.h>
Al Viro's avatar
Al Viro committed
#include <linux/acct.h>		/* acct_auto_close_mnt */
#include <linux/ramfs.h>	/* init_rootfs */
#include <linux/fs_struct.h>	/* get_fs_root et.al. */
#include <linux/fsnotify.h>	/* fsnotify_vfsmount_delete */
#include <linux/uaccess.h>
#include "pnode.h"
#include "internal.h"
Linus Torvalds's avatar
Linus Torvalds committed

#define HASH_SHIFT ilog2(PAGE_SIZE / sizeof(struct list_head))
#define HASH_SIZE (1UL << HASH_SHIFT)

static int event;
static DEFINE_IDA(mnt_id_ida);
static DEFINE_IDA(mnt_group_ida);
static DEFINE_SPINLOCK(mnt_id_lock);
static int mnt_id_start = 0;
static int mnt_group_start = 1;
Linus Torvalds's avatar
Linus Torvalds committed

static struct list_head *mount_hashtable __read_mostly;
static struct kmem_cache *mnt_cache __read_mostly;
static struct rw_semaphore namespace_sem;
Linus Torvalds's avatar
Linus Torvalds committed

Miklos Szeredi's avatar
Miklos Szeredi committed
/* /sys/fs */
struct kobject *fs_kobj;
EXPORT_SYMBOL_GPL(fs_kobj);
Miklos Szeredi's avatar
Miklos Szeredi committed

/*
 * vfsmount lock may be taken for read to prevent changes to the
 * vfsmount hash, ie. during mountpoint lookups or walking back
 * up the tree.
 *
 * It should be taken for write in all cases where the vfsmount
 * tree or hash is modified or when a vfsmount structure is modified.
 */
DEFINE_BRLOCK(vfsmount_lock);

Linus Torvalds's avatar
Linus Torvalds committed
static inline unsigned long hash(struct vfsmount *mnt, struct dentry *dentry)
{
Ram Pai's avatar
Ram Pai committed
	unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES);
	tmp += ((unsigned long)dentry / L1_CACHE_BYTES);
	tmp = tmp + (tmp >> HASH_SHIFT);
	return tmp & (HASH_SIZE - 1);
Linus Torvalds's avatar
Linus Torvalds committed
}

#define MNT_WRITER_UNDERFLOW_LIMIT -(1<<16)

/*
 * allocation is serialized by namespace_sem, but we need the spinlock to
 * serialize with freeing.
 */
static int mnt_alloc_id(struct mount *mnt)
{
	int res;

retry:
	ida_pre_get(&mnt_id_ida, GFP_KERNEL);
	spin_lock(&mnt_id_lock);
Al Viro's avatar
Al Viro committed
	res = ida_get_new_above(&mnt_id_ida, mnt_id_start, &mnt->mnt_id);
Al Viro's avatar
Al Viro committed
		mnt_id_start = mnt->mnt_id + 1;
	spin_unlock(&mnt_id_lock);
	if (res == -EAGAIN)
		goto retry;

	return res;
}

static void mnt_free_id(struct mount *mnt)
Al Viro's avatar
Al Viro committed
	int id = mnt->mnt_id;
	spin_lock(&mnt_id_lock);
	ida_remove(&mnt_id_ida, id);
	if (mnt_id_start > id)
		mnt_id_start = id;
	spin_unlock(&mnt_id_lock);
/*
 * Allocate a new peer group ID
 *
 * mnt_group_ida is protected by namespace_sem
 */
static int mnt_alloc_group_id(struct mount *mnt)
	if (!ida_pre_get(&mnt_group_ida, GFP_KERNEL))
		return -ENOMEM;

	res = ida_get_new_above(&mnt_group_ida,
				mnt_group_start,
Al Viro's avatar
Al Viro committed
				&mnt->mnt_group_id);
Al Viro's avatar
Al Viro committed
		mnt_group_start = mnt->mnt_group_id + 1;
void mnt_release_group_id(struct mount *mnt)
Al Viro's avatar
Al Viro committed
	int id = mnt->mnt_group_id;
	ida_remove(&mnt_group_ida, id);
	if (mnt_group_start > id)
		mnt_group_start = id;
Al Viro's avatar
Al Viro committed
	mnt->mnt_group_id = 0;
Nicholas Piggin's avatar
Nicholas Piggin committed
/*
 * vfsmount lock must be held for read
 */
static inline void mnt_add_count(struct mount *mnt, int n)
Nicholas Piggin's avatar
Nicholas Piggin committed
{
#ifdef CONFIG_SMP
	this_cpu_add(mnt->mnt_pcp->mnt_count, n);
Nicholas Piggin's avatar
Nicholas Piggin committed
#else
	preempt_disable();
	mnt->mnt_count += n;
Nicholas Piggin's avatar
Nicholas Piggin committed
	preempt_enable();
#endif
}

/*
 * vfsmount lock must be held for write
 */
unsigned int mnt_get_count(struct mount *mnt)
Nicholas Piggin's avatar
Nicholas Piggin committed
{
#ifdef CONFIG_SMP
	unsigned int count = 0;
Nicholas Piggin's avatar
Nicholas Piggin committed
	int cpu;

	for_each_possible_cpu(cpu) {
		count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_count;
Nicholas Piggin's avatar
Nicholas Piggin committed
	}

	return count;
#else
	return mnt->mnt_count;
static struct mount *alloc_vfsmnt(const char *name)
Linus Torvalds's avatar
Linus Torvalds committed
{
	struct mount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL);
	if (mnt) {
		err = mnt_alloc_id(mnt);
		if (err)
			goto out_free_cache;

		if (name) {
			mnt->mnt_devname = kstrdup(name, GFP_KERNEL);
			if (!mnt->mnt_devname)
Nicholas Piggin's avatar
Nicholas Piggin committed
#ifdef CONFIG_SMP
		mnt->mnt_pcp = alloc_percpu(struct mnt_pcp);
		if (!mnt->mnt_pcp)
Nicholas Piggin's avatar
Nicholas Piggin committed
			goto out_free_devname;

		this_cpu_add(mnt->mnt_pcp->mnt_count, 1);
Nicholas Piggin's avatar
Nicholas Piggin committed
#else
		mnt->mnt_count = 1;
		mnt->mnt_writers = 0;
		INIT_LIST_HEAD(&mnt->mnt_hash);
		INIT_LIST_HEAD(&mnt->mnt_child);
		INIT_LIST_HEAD(&mnt->mnt_mounts);
		INIT_LIST_HEAD(&mnt->mnt_list);
		INIT_LIST_HEAD(&mnt->mnt_expire);
		INIT_LIST_HEAD(&mnt->mnt_share);
		INIT_LIST_HEAD(&mnt->mnt_slave_list);
		INIT_LIST_HEAD(&mnt->mnt_slave);
#ifdef CONFIG_FSNOTIFY
		INIT_HLIST_HEAD(&mnt->mnt_fsnotify_marks);
#endif
Linus Torvalds's avatar
Linus Torvalds committed
	}
	return mnt;
Loading
Loading full blame...