2005-04-17 06:20:36 +08:00
|
|
|
/*
|
2007-09-20 16:31:38 +08:00
|
|
|
* fs/sysfs/symlink.c - sysfs symlink implementation
|
|
|
|
*
|
|
|
|
* 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 06:20:36 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/fs.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 16:04:11 +08:00
|
|
|
#include <linux/gfp.h>
|
2002-04-10 03:14:34 +08:00
|
|
|
#include <linux/mount.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/kobject.h>
|
|
|
|
#include <linux/namei.h>
|
2007-07-26 22:53:53 +08:00
|
|
|
#include <linux/mutex.h>
|
2009-09-10 02:25:37 +08:00
|
|
|
#include <linux/security.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
#include "sysfs.h"
|
|
|
|
|
2013-01-26 04:51:13 +08:00
|
|
|
static int sysfs_do_create_link_sd(struct sysfs_dirent *parent_sd,
|
|
|
|
struct kobject *target,
|
|
|
|
const char *name, int warn)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2007-06-14 02:45:15 +08:00
|
|
|
struct sysfs_dirent *target_sd = NULL;
|
2007-06-14 03:27:23 +08:00
|
|
|
struct sysfs_dirent *sd = NULL;
|
2007-06-14 03:27:24 +08:00
|
|
|
struct sysfs_addrm_cxt acxt;
|
2007-06-14 03:27:23 +08:00
|
|
|
int error;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2013-01-26 04:51:13 +08:00
|
|
|
BUG_ON(!name || !parent_sd);
|
2007-06-14 02:45:15 +08:00
|
|
|
|
2013-10-30 22:28:36 +08:00
|
|
|
/*
|
|
|
|
* We don't own @target and it may be removed at any time.
|
|
|
|
* Synchronize using sysfs_symlink_target_lock. See
|
|
|
|
* sysfs_remove_dir() for details.
|
2007-06-14 02:45:15 +08:00
|
|
|
*/
|
2013-10-30 22:28:36 +08:00
|
|
|
spin_lock(&sysfs_symlink_target_lock);
|
2007-06-14 03:27:22 +08:00
|
|
|
if (target->sd)
|
|
|
|
target_sd = sysfs_get(target->sd);
|
2013-10-30 22:28:36 +08:00
|
|
|
spin_unlock(&sysfs_symlink_target_lock);
|
2007-06-14 02:45:15 +08:00
|
|
|
|
2007-06-14 03:27:23 +08:00
|
|
|
error = -ENOENT;
|
2007-06-14 02:45:15 +08:00
|
|
|
if (!target_sd)
|
2007-06-14 03:27:23 +08:00
|
|
|
goto out_put;
|
|
|
|
|
|
|
|
error = -ENOMEM;
|
|
|
|
sd = sysfs_new_dirent(name, S_IFLNK|S_IRWXUGO, SYSFS_KOBJ_LINK);
|
|
|
|
if (!sd)
|
|
|
|
goto out_put;
|
2007-07-18 15:14:45 +08:00
|
|
|
|
2013-11-24 07:01:46 +08:00
|
|
|
if (parent_sd->s_flags & SYSFS_FLAG_NS)
|
2013-11-07 19:47:28 +08:00
|
|
|
sd->s_ns = target_sd->s_ns;
|
2007-09-20 15:05:11 +08:00
|
|
|
sd->s_symlink.target_sd = target_sd;
|
2007-07-18 15:14:45 +08:00
|
|
|
target_sd = NULL; /* reference is now owned by the symlink */
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2013-09-19 05:15:35 +08:00
|
|
|
sysfs_addrm_start(&acxt);
|
2013-11-24 07:01:46 +08:00
|
|
|
if (warn)
|
|
|
|
error = sysfs_add_one(&acxt, sd, parent_sd);
|
|
|
|
else
|
|
|
|
error = __sysfs_add_one(&acxt, sd, parent_sd);
|
2007-08-02 20:38:03 +08:00
|
|
|
sysfs_addrm_finish(&acxt);
|
2007-06-14 02:45:15 +08:00
|
|
|
|
2007-08-02 20:38:03 +08:00
|
|
|
if (error)
|
2007-07-18 15:38:11 +08:00
|
|
|
goto out_put;
|
|
|
|
|
|
|
|
return 0;
|
2007-06-14 03:27:24 +08:00
|
|
|
|
2007-06-14 03:27:23 +08:00
|
|
|
out_put:
|
|
|
|
sysfs_put(target_sd);
|
|
|
|
sysfs_put(sd);
|
2005-04-17 06:20:36 +08:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2013-01-26 04:51:13 +08:00
|
|
|
/**
|
|
|
|
* sysfs_create_link_sd - create symlink to a given object.
|
|
|
|
* @sd: directory we're creating the link in.
|
|
|
|
* @target: object we're pointing to.
|
|
|
|
* @name: name of the symlink.
|
|
|
|
*/
|
|
|
|
int sysfs_create_link_sd(struct sysfs_dirent *sd, struct kobject *target,
|
|
|
|
const char *name)
|
|
|
|
{
|
|
|
|
return sysfs_do_create_link_sd(sd, target, name, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sysfs_do_create_link(struct kobject *kobj, struct kobject *target,
|
|
|
|
const char *name, int warn)
|
|
|
|
{
|
|
|
|
struct sysfs_dirent *parent_sd = NULL;
|
|
|
|
|
|
|
|
if (!kobj)
|
|
|
|
parent_sd = &sysfs_root;
|
|
|
|
else
|
|
|
|
parent_sd = kobj->sd;
|
|
|
|
|
|
|
|
if (!parent_sd)
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
return sysfs_do_create_link_sd(parent_sd, target, name, warn);
|
|
|
|
}
|
|
|
|
|
2008-06-10 17:09:08 +08:00
|
|
|
/**
|
|
|
|
* sysfs_create_link - create symlink between two objects.
|
|
|
|
* @kobj: object whose directory we're creating the link in.
|
|
|
|
* @target: object we're pointing to.
|
|
|
|
* @name: name of the symlink.
|
|
|
|
*/
|
|
|
|
int sysfs_create_link(struct kobject *kobj, struct kobject *target,
|
|
|
|
const char *name)
|
|
|
|
{
|
|
|
|
return sysfs_do_create_link(kobj, target, name, 1);
|
|
|
|
}
|
2013-08-22 07:17:47 +08:00
|
|
|
EXPORT_SYMBOL_GPL(sysfs_create_link);
|
2008-06-10 17:09:08 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* sysfs_create_link_nowarn - create symlink between two objects.
|
|
|
|
* @kobj: object whose directory we're creating the link in.
|
|
|
|
* @target: object we're pointing to.
|
|
|
|
* @name: name of the symlink.
|
|
|
|
*
|
2012-09-04 19:23:35 +08:00
|
|
|
* This function does the same as sysfs_create_link(), but it
|
2008-06-10 17:09:08 +08:00
|
|
|
* doesn't warn if the link already exists.
|
|
|
|
*/
|
|
|
|
int sysfs_create_link_nowarn(struct kobject *kobj, struct kobject *target,
|
|
|
|
const char *name)
|
|
|
|
{
|
|
|
|
return sysfs_do_create_link(kobj, target, name, 0);
|
|
|
|
}
|
|
|
|
|
2010-03-31 02:31:28 +08:00
|
|
|
/**
|
|
|
|
* sysfs_delete_link - remove symlink in object's directory.
|
|
|
|
* @kobj: object we're acting for.
|
|
|
|
* @targ: object we're pointing to.
|
|
|
|
* @name: name of the symlink to remove.
|
|
|
|
*
|
|
|
|
* Unlike sysfs_remove_link sysfs_delete_link has enough information
|
|
|
|
* to successfully delete symlinks in tagged directories.
|
|
|
|
*/
|
|
|
|
void sysfs_delete_link(struct kobject *kobj, struct kobject *targ,
|
|
|
|
const char *name)
|
|
|
|
{
|
|
|
|
const void *ns = NULL;
|
2013-10-30 22:28:36 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We don't own @target and it may be removed at any time.
|
|
|
|
* Synchronize using sysfs_symlink_target_lock. See
|
|
|
|
* sysfs_remove_dir() for details.
|
|
|
|
*/
|
|
|
|
spin_lock(&sysfs_symlink_target_lock);
|
2013-11-24 07:01:46 +08:00
|
|
|
if (targ->sd && (kobj->sd->s_flags & SYSFS_FLAG_NS))
|
2010-03-31 02:31:28 +08:00
|
|
|
ns = targ->sd->s_ns;
|
2013-10-30 22:28:36 +08:00
|
|
|
spin_unlock(&sysfs_symlink_target_lock);
|
2013-09-12 10:29:09 +08:00
|
|
|
sysfs_hash_and_remove(kobj->sd, name, ns);
|
2010-03-31 02:31:28 +08:00
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/**
|
|
|
|
* sysfs_remove_link - remove symlink in object's directory.
|
|
|
|
* @kobj: object we're acting for.
|
|
|
|
* @name: name of the symlink to remove.
|
|
|
|
*/
|
2013-08-22 07:28:26 +08:00
|
|
|
void sysfs_remove_link(struct kobject *kobj, const char *name)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2008-01-30 06:35:18 +08:00
|
|
|
struct sysfs_dirent *parent_sd = NULL;
|
|
|
|
|
|
|
|
if (!kobj)
|
|
|
|
parent_sd = &sysfs_root;
|
|
|
|
else
|
|
|
|
parent_sd = kobj->sd;
|
|
|
|
|
2013-09-12 10:29:09 +08:00
|
|
|
sysfs_hash_and_remove(parent_sd, name, NULL);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2013-08-22 07:17:47 +08:00
|
|
|
EXPORT_SYMBOL_GPL(sysfs_remove_link);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2010-02-13 11:22:25 +08:00
|
|
|
/**
|
2013-09-12 10:29:06 +08:00
|
|
|
* sysfs_rename_link_ns - rename symlink in object's directory.
|
2010-02-13 11:22:25 +08:00
|
|
|
* @kobj: object we're acting for.
|
|
|
|
* @targ: object we're pointing to.
|
|
|
|
* @old: previous name of the symlink.
|
|
|
|
* @new: new name of the symlink.
|
2013-09-12 10:29:06 +08:00
|
|
|
* @new_ns: new namespace of the symlink.
|
2010-02-13 11:22:25 +08:00
|
|
|
*
|
|
|
|
* A helper function for the common rename symlink idiom.
|
|
|
|
*/
|
2013-09-12 10:29:06 +08:00
|
|
|
int sysfs_rename_link_ns(struct kobject *kobj, struct kobject *targ,
|
|
|
|
const char *old, const char *new, const void *new_ns)
|
2010-02-13 11:22:25 +08:00
|
|
|
{
|
|
|
|
struct sysfs_dirent *parent_sd, *sd = NULL;
|
2013-09-12 10:29:06 +08:00
|
|
|
const void *old_ns = NULL;
|
2010-02-13 11:22:25 +08:00
|
|
|
int result;
|
|
|
|
|
|
|
|
if (!kobj)
|
|
|
|
parent_sd = &sysfs_root;
|
|
|
|
else
|
|
|
|
parent_sd = kobj->sd;
|
|
|
|
|
2010-03-31 02:31:26 +08:00
|
|
|
if (targ->sd)
|
|
|
|
old_ns = targ->sd->s_ns;
|
|
|
|
|
2010-02-13 11:22:25 +08:00
|
|
|
result = -ENOENT;
|
2013-09-12 11:19:13 +08:00
|
|
|
sd = sysfs_get_dirent_ns(parent_sd, old, old_ns);
|
2010-02-13 11:22:25 +08:00
|
|
|
if (!sd)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
result = -EINVAL;
|
|
|
|
if (sysfs_type(sd) != SYSFS_KOBJ_LINK)
|
|
|
|
goto out;
|
|
|
|
if (sd->s_symlink.target_sd->s_dir.kobj != targ)
|
|
|
|
goto out;
|
|
|
|
|
2013-09-12 10:29:09 +08:00
|
|
|
result = sysfs_rename(sd, parent_sd, new, new_ns);
|
2010-02-13 11:22:25 +08:00
|
|
|
|
|
|
|
out:
|
|
|
|
sysfs_put(sd);
|
|
|
|
return result;
|
|
|
|
}
|
2013-09-12 10:29:06 +08:00
|
|
|
EXPORT_SYMBOL_GPL(sysfs_rename_link_ns);
|
2010-02-13 11:22:25 +08:00
|
|
|
|
2007-11-02 03:20:52 +08:00
|
|
|
static int sysfs_get_target_path(struct sysfs_dirent *parent_sd,
|
|
|
|
struct sysfs_dirent *target_sd, char *path)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2007-11-02 03:20:52 +08:00
|
|
|
struct sysfs_dirent *base, *sd;
|
|
|
|
char *s = path;
|
|
|
|
int len = 0;
|
|
|
|
|
|
|
|
/* go up to the root, stop at the base */
|
|
|
|
base = parent_sd;
|
|
|
|
while (base->s_parent) {
|
|
|
|
sd = target_sd->s_parent;
|
|
|
|
while (sd->s_parent && base != sd)
|
|
|
|
sd = sd->s_parent;
|
|
|
|
|
|
|
|
if (base == sd)
|
|
|
|
break;
|
|
|
|
|
|
|
|
strcpy(s, "../");
|
|
|
|
s += 3;
|
|
|
|
base = base->s_parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* determine end of target string for reverse fillup */
|
|
|
|
sd = target_sd;
|
|
|
|
while (sd->s_parent && sd != base) {
|
|
|
|
len += strlen(sd->s_name) + 1;
|
|
|
|
sd = sd->s_parent;
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-11-02 03:20:52 +08:00
|
|
|
/* check limits */
|
|
|
|
if (len < 2)
|
|
|
|
return -EINVAL;
|
|
|
|
len--;
|
|
|
|
if ((s - path) + len > PATH_MAX)
|
2005-04-17 06:20:36 +08:00
|
|
|
return -ENAMETOOLONG;
|
|
|
|
|
2007-11-02 03:20:52 +08:00
|
|
|
/* reverse fillup of target string from target to base */
|
|
|
|
sd = target_sd;
|
|
|
|
while (sd->s_parent && sd != base) {
|
|
|
|
int slen = strlen(sd->s_name);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-11-02 03:20:52 +08:00
|
|
|
len -= slen;
|
|
|
|
strncpy(s + len, sd->s_name, slen);
|
|
|
|
if (len)
|
|
|
|
s[--len] = '/';
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-11-02 03:20:52 +08:00
|
|
|
sd = sd->s_parent;
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-08-22 07:28:26 +08:00
|
|
|
static int sysfs_getlink(struct dentry *dentry, char *path)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2007-06-14 02:45:15 +08:00
|
|
|
struct sysfs_dirent *sd = dentry->d_fsdata;
|
|
|
|
struct sysfs_dirent *parent_sd = sd->s_parent;
|
2007-09-20 15:05:11 +08:00
|
|
|
struct sysfs_dirent *target_sd = sd->s_symlink.target_sd;
|
2007-06-14 02:45:15 +08:00
|
|
|
int error;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-06-14 03:27:23 +08:00
|
|
|
mutex_lock(&sysfs_mutex);
|
2007-06-14 02:45:15 +08:00
|
|
|
error = sysfs_get_target_path(parent_sd, target_sd, path);
|
2007-06-14 03:27:23 +08:00
|
|
|
mutex_unlock(&sysfs_mutex);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-06-14 02:45:15 +08:00
|
|
|
return error;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2005-08-20 09:02:56 +08:00
|
|
|
static void *sysfs_follow_link(struct dentry *dentry, struct nameidata *nd)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
int error = -ENOMEM;
|
|
|
|
unsigned long page = get_zeroed_page(GFP_KERNEL);
|
2009-04-30 01:29:59 +08:00
|
|
|
if (page) {
|
2013-08-22 07:21:17 +08:00
|
|
|
error = sysfs_getlink(dentry, (char *) page);
|
2009-04-30 01:29:59 +08:00
|
|
|
if (error < 0)
|
|
|
|
free_page((unsigned long)page);
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
nd_set_link(nd, error ? ERR_PTR(error) : (char *)page);
|
2005-08-20 09:02:56 +08:00
|
|
|
return NULL;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2013-08-22 07:33:34 +08:00
|
|
|
static void sysfs_put_link(struct dentry *dentry, struct nameidata *nd,
|
|
|
|
void *cookie)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
char *page = nd_get_link(nd);
|
|
|
|
if (!IS_ERR(page))
|
|
|
|
free_page((unsigned long)page);
|
|
|
|
}
|
|
|
|
|
2007-02-12 16:55:40 +08:00
|
|
|
const struct inode_operations sysfs_symlink_inode_operations = {
|
2009-11-21 08:08:52 +08:00
|
|
|
.setxattr = sysfs_setxattr,
|
|
|
|
.readlink = generic_readlink,
|
|
|
|
.follow_link = sysfs_follow_link,
|
|
|
|
.put_link = sysfs_put_link,
|
2009-11-21 08:08:53 +08:00
|
|
|
.setattr = sysfs_setattr,
|
|
|
|
.getattr = sysfs_getattr,
|
|
|
|
.permission = sysfs_permission,
|
2005-04-17 06:20:36 +08:00
|
|
|
};
|