2005-04-17 07:20:36 +09:00
|
|
|
/*
|
2007-09-20 17:31:38 +09:00
|
|
|
* fs/sysfs/symlink.c - operations for initializing and mounting sysfs
|
|
|
|
*
|
|
|
|
* Copyright (c) 2001-3 Patrick Mochel
|
|
|
|
* Copyright (c) 2007 SUSE Linux Products GmbH
|
|
|
|
* Copyright (c) 2007 Tejun Heo <teheo@suse.de>
|
|
|
|
*
|
|
|
|
* This file is released under the GPLv2.
|
|
|
|
*
|
|
|
|
* Please see Documentation/filesystems/sysfs.txt for more information.
|
2005-04-17 07:20:36 +09:00
|
|
|
*/
|
|
|
|
|
2012-12-06 18:08:56 +09:00
|
|
|
#define DEBUG
|
2005-04-17 07:20:36 +09:00
|
|
|
|
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <linux/mount.h>
|
|
|
|
#include <linux/pagemap.h>
|
|
|
|
#include <linux/init.h>
|
2008-07-16 07:58:04 +09:00
|
|
|
#include <linux/module.h>
|
2009-01-14 16:45:13 +09:00
|
|
|
#include <linux/magic.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 17:04:11 +09:00
|
|
|
#include <linux/slab.h>
|
2013-03-25 06:28:27 +09:00
|
|
|
#include <linux/user_namespace.h>
|
2005-04-17 07:20:36 +09:00
|
|
|
|
|
|
|
#include "sysfs.h"
|
|
|
|
|
|
|
|
|
2006-12-07 13:33:20 +09:00
|
|
|
struct kmem_cache *sysfs_dir_cachep;
|
2005-04-17 07:20:36 +09:00
|
|
|
|
2007-02-12 17:55:41 +09:00
|
|
|
static const struct super_operations sysfs_ops = {
|
2005-04-17 07:20:36 +09:00
|
|
|
.statfs = simple_statfs,
|
2007-07-31 19:15:08 +09:00
|
|
|
.drop_inode = generic_delete_inode,
|
2010-06-05 11:21:54 +09:00
|
|
|
.evict_inode = sysfs_evict_inode,
|
2005-04-17 07:20:36 +09:00
|
|
|
};
|
|
|
|
|
2013-11-29 04:54:40 +09:00
|
|
|
static struct kernfs_root *sysfs_root;
|
|
|
|
struct sysfs_dirent *sysfs_root_sd;
|
2013-11-29 04:54:39 +09:00
|
|
|
|
2013-11-29 04:54:36 +09:00
|
|
|
static int sysfs_fill_super(struct super_block *sb)
|
2005-04-17 07:20:36 +09:00
|
|
|
{
|
2013-11-29 04:54:42 +09:00
|
|
|
struct sysfs_super_info *info = sysfs_info(sb);
|
2005-04-17 07:20:36 +09:00
|
|
|
struct inode *inode;
|
|
|
|
struct dentry *root;
|
|
|
|
|
|
|
|
sb->s_blocksize = PAGE_CACHE_SIZE;
|
|
|
|
sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
|
|
|
|
sb->s_magic = SYSFS_MAGIC;
|
|
|
|
sb->s_op = &sysfs_ops;
|
|
|
|
sb->s_time_gran = 1;
|
|
|
|
|
2007-07-18 14:29:06 +09:00
|
|
|
/* get root inode, initialize and unlock it */
|
2009-01-22 04:55:11 +09:00
|
|
|
mutex_lock(&sysfs_mutex);
|
2013-11-29 04:54:42 +09:00
|
|
|
inode = sysfs_get_inode(sb, info->root->sd);
|
2009-01-22 04:55:11 +09:00
|
|
|
mutex_unlock(&sysfs_mutex);
|
2007-06-14 03:45:17 +09:00
|
|
|
if (!inode) {
|
2005-04-17 07:20:36 +09:00
|
|
|
pr_debug("sysfs: could not get root inode\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2007-07-18 14:29:06 +09:00
|
|
|
/* instantiate and link root dentry */
|
2012-01-09 12:15:13 +09:00
|
|
|
root = d_make_root(inode);
|
2005-04-17 07:20:36 +09:00
|
|
|
if (!root) {
|
2013-08-22 08:28:26 +09:00
|
|
|
pr_debug("%s: could not get root dentry!\n", __func__);
|
2005-04-17 07:20:36 +09:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
2013-11-29 04:54:42 +09:00
|
|
|
kernfs_get(info->root->sd);
|
|
|
|
root->d_fsdata = info->root->sd;
|
2005-04-17 07:20:36 +09:00
|
|
|
sb->s_root = root;
|
2012-06-08 09:51:39 +09:00
|
|
|
sb->s_d_op = &sysfs_dentry_ops;
|
2005-04-17 07:20:36 +09:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-03-31 03:31:24 +09:00
|
|
|
static int sysfs_test_super(struct super_block *sb, void *data)
|
|
|
|
{
|
|
|
|
struct sysfs_super_info *sb_info = sysfs_info(sb);
|
|
|
|
struct sysfs_super_info *info = data;
|
2010-03-31 03:31:26 +09:00
|
|
|
|
2013-11-29 04:54:42 +09:00
|
|
|
return sb_info->root == info->root && sb_info->ns == info->ns;
|
2010-03-31 03:31:24 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
static int sysfs_set_super(struct super_block *sb, void *data)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
error = set_anon_super(sb, data);
|
|
|
|
if (!error)
|
|
|
|
sb->s_fs_info = data;
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2013-11-29 04:54:43 +09:00
|
|
|
/**
|
|
|
|
* kernfs_super_ns - determine the namespace tag of a kernfs super_block
|
|
|
|
* @sb: super_block of interest
|
|
|
|
*
|
|
|
|
* Return the namespace tag associated with kernfs super_block @sb.
|
|
|
|
*/
|
|
|
|
const void *kernfs_super_ns(struct super_block *sb)
|
2011-06-09 10:13:01 +09:00
|
|
|
{
|
2013-11-29 04:54:43 +09:00
|
|
|
struct sysfs_super_info *info = sysfs_info(sb);
|
|
|
|
|
|
|
|
return info->ns;
|
2011-06-09 10:13:01 +09:00
|
|
|
}
|
|
|
|
|
2010-07-26 18:30:36 +09:00
|
|
|
static struct dentry *sysfs_mount(struct file_system_type *fs_type,
|
|
|
|
int flags, const char *dev_name, void *data)
|
2005-04-17 07:20:36 +09:00
|
|
|
{
|
2013-11-29 04:54:43 +09:00
|
|
|
struct dentry *root;
|
|
|
|
void *ns;
|
2010-03-31 03:31:24 +09:00
|
|
|
|
2013-03-26 12:07:01 +09:00
|
|
|
if (!(flags & MS_KERNMOUNT)) {
|
|
|
|
if (!capable(CAP_SYS_ADMIN) && !fs_fully_visible(fs_type))
|
|
|
|
return ERR_PTR(-EPERM);
|
|
|
|
|
2013-11-24 08:01:46 +09:00
|
|
|
if (!kobj_ns_current_may_mount(KOBJ_NS_TYPE_NET))
|
|
|
|
return ERR_PTR(-EPERM);
|
2013-03-26 12:07:01 +09:00
|
|
|
}
|
2013-03-25 06:28:27 +09:00
|
|
|
|
2013-11-29 04:54:43 +09:00
|
|
|
ns = kobj_ns_grab_current(KOBJ_NS_TYPE_NET);
|
|
|
|
root = kernfs_mount_ns(fs_type, flags, sysfs_root, ns);
|
|
|
|
if (IS_ERR(root))
|
|
|
|
kobj_ns_drop(KOBJ_NS_TYPE_NET, ns);
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* kernfs_mount_ns - kernfs mount helper
|
|
|
|
* @fs_type: file_system_type of the fs being mounted
|
|
|
|
* @flags: mount flags specified for the mount
|
|
|
|
* @root: kernfs_root of the hierarchy being mounted
|
|
|
|
* @ns: optional namespace tag of the mount
|
|
|
|
*
|
|
|
|
* This is to be called from each kernfs user's file_system_type->mount()
|
|
|
|
* implementation, which should pass through the specified @fs_type and
|
|
|
|
* @flags, and specify the hierarchy and namespace tag to mount via @root
|
|
|
|
* and @ns, respectively.
|
|
|
|
*
|
|
|
|
* The return value can be passed to the vfs layer verbatim.
|
|
|
|
*/
|
|
|
|
struct dentry *kernfs_mount_ns(struct file_system_type *fs_type, int flags,
|
|
|
|
struct kernfs_root *root, const void *ns)
|
|
|
|
{
|
|
|
|
struct super_block *sb;
|
|
|
|
struct sysfs_super_info *info;
|
|
|
|
int error;
|
|
|
|
|
2010-03-31 03:31:24 +09:00
|
|
|
info = kzalloc(sizeof(*info), GFP_KERNEL);
|
|
|
|
if (!info)
|
2010-07-26 18:30:36 +09:00
|
|
|
return ERR_PTR(-ENOMEM);
|
2010-03-31 03:31:26 +09:00
|
|
|
|
2013-11-29 04:54:43 +09:00
|
|
|
info->root = root;
|
|
|
|
info->ns = ns;
|
2010-03-31 03:31:26 +09:00
|
|
|
|
2012-06-25 20:55:37 +09:00
|
|
|
sb = sget(fs_type, sysfs_test_super, sysfs_set_super, flags, info);
|
2010-03-31 03:31:24 +09:00
|
|
|
if (IS_ERR(sb) || sb->s_fs_info != info)
|
2013-11-29 04:54:43 +09:00
|
|
|
kfree(info);
|
2010-07-26 18:30:36 +09:00
|
|
|
if (IS_ERR(sb))
|
|
|
|
return ERR_CAST(sb);
|
2010-03-31 03:31:24 +09:00
|
|
|
if (!sb->s_root) {
|
2013-11-29 04:54:36 +09:00
|
|
|
error = sysfs_fill_super(sb);
|
2010-03-31 03:31:24 +09:00
|
|
|
if (error) {
|
|
|
|
deactivate_locked_super(sb);
|
2010-07-26 18:30:36 +09:00
|
|
|
return ERR_PTR(error);
|
2010-03-31 03:31:24 +09:00
|
|
|
}
|
|
|
|
sb->s_flags |= MS_ACTIVE;
|
|
|
|
}
|
|
|
|
|
2010-07-26 18:30:36 +09:00
|
|
|
return dget(sb->s_root);
|
2010-03-31 03:31:24 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
static void sysfs_kill_sb(struct super_block *sb)
|
2013-11-29 04:54:43 +09:00
|
|
|
{
|
|
|
|
kernfs_kill_sb(sb);
|
|
|
|
kobj_ns_drop(KOBJ_NS_TYPE_NET, (void *)kernfs_super_ns(sb));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* kernfs_kill_sb - kill_sb for kernfs
|
|
|
|
* @sb: super_block being killed
|
|
|
|
*
|
|
|
|
* This can be used directly for file_system_type->kill_sb(). If a kernfs
|
|
|
|
* user needs extra cleanup, it can implement its own kill_sb() and call
|
|
|
|
* this function at the end.
|
|
|
|
*/
|
|
|
|
void kernfs_kill_sb(struct super_block *sb)
|
2010-03-31 03:31:24 +09:00
|
|
|
{
|
|
|
|
struct sysfs_super_info *info = sysfs_info(sb);
|
2013-11-29 04:54:40 +09:00
|
|
|
struct sysfs_dirent *root_sd = sb->s_root->d_fsdata;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Remove the superblock from fs_supers/s_instances
|
2010-05-19 04:58:33 +09:00
|
|
|
* so we can't find it, before freeing sysfs_super_info.
|
|
|
|
*/
|
2010-03-31 03:31:24 +09:00
|
|
|
kill_anon_super(sb);
|
2013-11-29 04:54:43 +09:00
|
|
|
kfree(info);
|
2013-11-29 04:54:40 +09:00
|
|
|
kernfs_put(root_sd);
|
2005-04-17 07:20:36 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct file_system_type sysfs_fs_type = {
|
|
|
|
.name = "sysfs",
|
2010-07-26 18:30:36 +09:00
|
|
|
.mount = sysfs_mount,
|
2010-03-31 03:31:24 +09:00
|
|
|
.kill_sb = sysfs_kill_sb,
|
2012-07-27 21:56:48 +09:00
|
|
|
.fs_flags = FS_USERNS_MOUNT,
|
2005-04-17 07:20:36 +09:00
|
|
|
};
|
|
|
|
|
2013-11-29 04:54:43 +09:00
|
|
|
void __init kernfs_init(void)
|
2005-04-17 07:20:36 +09:00
|
|
|
{
|
|
|
|
sysfs_dir_cachep = kmem_cache_create("sysfs_dir_cache",
|
|
|
|
sizeof(struct sysfs_dirent),
|
2013-11-29 04:54:43 +09:00
|
|
|
0, SLAB_PANIC, NULL);
|
|
|
|
sysfs_inode_init();
|
|
|
|
}
|
2005-04-17 07:20:36 +09:00
|
|
|
|
2013-11-29 04:54:43 +09:00
|
|
|
int __init sysfs_init(void)
|
|
|
|
{
|
|
|
|
int err;
|
2007-10-17 15:25:46 +09:00
|
|
|
|
2013-11-29 04:54:40 +09:00
|
|
|
sysfs_root = kernfs_create_root(NULL);
|
2013-11-29 04:54:43 +09:00
|
|
|
if (IS_ERR(sysfs_root))
|
|
|
|
return PTR_ERR(sysfs_root);
|
|
|
|
|
2013-11-29 04:54:40 +09:00
|
|
|
sysfs_root_sd = sysfs_root->sd;
|
|
|
|
|
2005-04-17 07:20:36 +09:00
|
|
|
err = register_filesystem(&sysfs_fs_type);
|
2013-11-29 04:54:43 +09:00
|
|
|
if (err) {
|
|
|
|
kernfs_destroy_root(sysfs_root);
|
|
|
|
return err;
|
|
|
|
}
|
2013-11-29 04:54:38 +09:00
|
|
|
|
|
|
|
return 0;
|
2005-04-17 07:20:36 +09:00
|
|
|
}
|