2005-04-16 16:20:36 -06:00
|
|
|
/*
|
2007-09-20 02:31:38 -06: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-16 16:20:36 -06:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/fs.h>
|
2002-04-09 13:14:34 -06:00
|
|
|
#include <linux/mount.h>
|
2005-04-16 16:20:36 -06:00
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/kobject.h>
|
|
|
|
#include <linux/namei.h>
|
2007-07-26 08:53:53 -06:00
|
|
|
#include <linux/mutex.h>
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
#include "sysfs.h"
|
|
|
|
|
2007-06-13 12:45:15 -06:00
|
|
|
static int object_depth(struct sysfs_dirent *sd)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
|
|
|
int depth = 0;
|
2007-06-13 12:45:15 -06:00
|
|
|
|
|
|
|
for (; sd->s_parent; sd = sd->s_parent)
|
|
|
|
depth++;
|
|
|
|
|
2005-04-16 16:20:36 -06:00
|
|
|
return depth;
|
|
|
|
}
|
|
|
|
|
2007-06-13 12:45:15 -06:00
|
|
|
static int object_path_length(struct sysfs_dirent * sd)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
|
|
|
int length = 1;
|
2007-06-13 12:45:15 -06:00
|
|
|
|
|
|
|
for (; sd->s_parent; sd = sd->s_parent)
|
|
|
|
length += strlen(sd->s_name) + 1;
|
|
|
|
|
2005-04-16 16:20:36 -06:00
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2007-06-13 12:45:15 -06:00
|
|
|
static void fill_object_path(struct sysfs_dirent *sd, char *buffer, int length)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
|
|
|
--length;
|
2007-06-13 12:45:15 -06:00
|
|
|
for (; sd->s_parent; sd = sd->s_parent) {
|
|
|
|
int cur = strlen(sd->s_name);
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
/* back up enough to print this bus id with '/' */
|
|
|
|
length -= cur;
|
2007-06-13 12:45:15 -06:00
|
|
|
strncpy(buffer + length, sd->s_name, cur);
|
2005-04-16 16:20:36 -06:00
|
|
|
*(buffer + --length) = '/';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2005-04-26 01:31:08 -06:00
|
|
|
int sysfs_create_link(struct kobject * kobj, struct kobject * target, const char * name)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2007-06-13 12:45:15 -06:00
|
|
|
struct sysfs_dirent *parent_sd = NULL;
|
|
|
|
struct sysfs_dirent *target_sd = NULL;
|
2007-06-13 13:27:23 -06:00
|
|
|
struct sysfs_dirent *sd = NULL;
|
2007-06-13 13:27:24 -06:00
|
|
|
struct sysfs_addrm_cxt acxt;
|
2007-06-13 13:27:23 -06:00
|
|
|
int error;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2002-04-09 13:14:34 -06:00
|
|
|
BUG_ON(!name);
|
|
|
|
|
2007-08-20 06:36:30 -06:00
|
|
|
if (!kobj)
|
|
|
|
parent_sd = &sysfs_root;
|
|
|
|
else
|
2007-06-13 13:27:22 -06:00
|
|
|
parent_sd = kobj->sd;
|
2002-04-09 13:14:34 -06:00
|
|
|
|
2007-06-13 13:27:23 -06:00
|
|
|
error = -EFAULT;
|
2007-06-13 13:27:22 -06:00
|
|
|
if (!parent_sd)
|
2007-06-13 13:27:23 -06:00
|
|
|
goto out_put;
|
2007-06-13 12:45:15 -06:00
|
|
|
|
2007-06-13 13:27:22 -06:00
|
|
|
/* target->sd can go away beneath us but is protected with
|
2007-06-13 13:27:23 -06:00
|
|
|
* sysfs_assoc_lock. Fetch target_sd from it.
|
2007-06-13 12:45:15 -06:00
|
|
|
*/
|
2007-06-13 13:27:23 -06:00
|
|
|
spin_lock(&sysfs_assoc_lock);
|
2007-06-13 13:27:22 -06:00
|
|
|
if (target->sd)
|
|
|
|
target_sd = sysfs_get(target->sd);
|
2007-06-13 13:27:23 -06:00
|
|
|
spin_unlock(&sysfs_assoc_lock);
|
2007-06-13 12:45:15 -06:00
|
|
|
|
2007-06-13 13:27:23 -06:00
|
|
|
error = -ENOENT;
|
2007-06-13 12:45:15 -06:00
|
|
|
if (!target_sd)
|
2007-06-13 13:27:23 -06: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 01:14:45 -06:00
|
|
|
|
2007-09-20 01:05:11 -06:00
|
|
|
sd->s_symlink.target_sd = target_sd;
|
2007-07-18 01:14:45 -06:00
|
|
|
target_sd = NULL; /* reference is now owned by the symlink */
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2007-06-13 13:27:24 -06:00
|
|
|
sysfs_addrm_start(&acxt, parent_sd);
|
2007-08-02 06:38:03 -06:00
|
|
|
error = sysfs_add_one(&acxt, sd);
|
|
|
|
sysfs_addrm_finish(&acxt);
|
2007-06-13 12:45:15 -06:00
|
|
|
|
2007-08-02 06:38:03 -06:00
|
|
|
if (error)
|
2007-07-18 01:38:11 -06:00
|
|
|
goto out_put;
|
|
|
|
|
|
|
|
return 0;
|
2007-06-13 13:27:24 -06:00
|
|
|
|
2007-06-13 13:27:23 -06:00
|
|
|
out_put:
|
|
|
|
sysfs_put(target_sd);
|
|
|
|
sysfs_put(sd);
|
2005-04-16 16:20:36 -06:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sysfs_remove_link - remove symlink in object's directory.
|
|
|
|
* @kobj: object we're acting for.
|
|
|
|
* @name: name of the symlink to remove.
|
|
|
|
*/
|
|
|
|
|
2005-04-26 01:31:08 -06:00
|
|
|
void sysfs_remove_link(struct kobject * kobj, const char * name)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2007-06-13 13:27:22 -06:00
|
|
|
sysfs_hash_and_remove(kobj->sd, name);
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
2007-06-13 12:45:15 -06:00
|
|
|
static int sysfs_get_target_path(struct sysfs_dirent * parent_sd,
|
|
|
|
struct sysfs_dirent * target_sd, char *path)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
|
|
|
char * s;
|
|
|
|
int depth, size;
|
|
|
|
|
2007-06-13 12:45:15 -06:00
|
|
|
depth = object_depth(parent_sd);
|
|
|
|
size = object_path_length(target_sd) + depth * 3 - 1;
|
2005-04-16 16:20:36 -06:00
|
|
|
if (size > PATH_MAX)
|
|
|
|
return -ENAMETOOLONG;
|
|
|
|
|
|
|
|
pr_debug("%s: depth = %d, size = %d\n", __FUNCTION__, depth, size);
|
|
|
|
|
|
|
|
for (s = path; depth--; s += 3)
|
|
|
|
strcpy(s,"../");
|
|
|
|
|
2007-06-13 12:45:15 -06:00
|
|
|
fill_object_path(target_sd, path, size);
|
2005-04-16 16:20:36 -06:00
|
|
|
pr_debug("%s: path = '%s'\n", __FUNCTION__, path);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sysfs_getlink(struct dentry *dentry, char * path)
|
|
|
|
{
|
2007-06-13 12:45:15 -06:00
|
|
|
struct sysfs_dirent *sd = dentry->d_fsdata;
|
|
|
|
struct sysfs_dirent *parent_sd = sd->s_parent;
|
2007-09-20 01:05:11 -06:00
|
|
|
struct sysfs_dirent *target_sd = sd->s_symlink.target_sd;
|
2007-06-13 12:45:15 -06:00
|
|
|
int error;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2007-06-13 13:27:23 -06:00
|
|
|
mutex_lock(&sysfs_mutex);
|
2007-06-13 12:45:15 -06:00
|
|
|
error = sysfs_get_target_path(parent_sd, target_sd, path);
|
2007-06-13 13:27:23 -06:00
|
|
|
mutex_unlock(&sysfs_mutex);
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2007-06-13 12:45:15 -06:00
|
|
|
return error;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
2005-08-19 19:02:56 -06:00
|
|
|
static void *sysfs_follow_link(struct dentry *dentry, struct nameidata *nd)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
|
|
|
int error = -ENOMEM;
|
|
|
|
unsigned long page = get_zeroed_page(GFP_KERNEL);
|
|
|
|
if (page)
|
|
|
|
error = sysfs_getlink(dentry, (char *) page);
|
|
|
|
nd_set_link(nd, error ? ERR_PTR(error) : (char *)page);
|
2005-08-19 19:02:56 -06:00
|
|
|
return NULL;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
2005-08-19 19:02:56 -06:00
|
|
|
static void sysfs_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
|
|
|
char *page = nd_get_link(nd);
|
|
|
|
if (!IS_ERR(page))
|
|
|
|
free_page((unsigned long)page);
|
|
|
|
}
|
|
|
|
|
2007-02-12 01:55:40 -07:00
|
|
|
const struct inode_operations sysfs_symlink_inode_operations = {
|
2005-04-16 16:20:36 -06:00
|
|
|
.readlink = generic_readlink,
|
|
|
|
.follow_link = sysfs_follow_link,
|
|
|
|
.put_link = sysfs_put_link,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
EXPORT_SYMBOL_GPL(sysfs_create_link);
|
|
|
|
EXPORT_SYMBOL_GPL(sysfs_remove_link);
|