blob: 683316f0aa967a786f04bd322f97573eeb748774 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * symlink.c - operations for sysfs symlinks.
3 */
4
5#include <linux/fs.h>
Greg Kroah-Hartmanceeee1f2002-04-09 12:14:34 -07006#include <linux/mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/module.h>
8#include <linux/kobject.h>
9#include <linux/namei.h>
Oliver Neukum94bebf42006-12-20 10:52:44 +010010#include <asm/semaphore.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
12#include "sysfs.h"
13
Tejun Heo2b29ac22007-06-14 03:45:15 +090014static int object_depth(struct sysfs_dirent *sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015{
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 int depth = 0;
Tejun Heo2b29ac22007-06-14 03:45:15 +090017
18 for (; sd->s_parent; sd = sd->s_parent)
19 depth++;
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 return depth;
22}
23
Tejun Heo2b29ac22007-06-14 03:45:15 +090024static int object_path_length(struct sysfs_dirent * sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -070025{
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 int length = 1;
Tejun Heo2b29ac22007-06-14 03:45:15 +090027
28 for (; sd->s_parent; sd = sd->s_parent)
29 length += strlen(sd->s_name) + 1;
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 return length;
32}
33
Tejun Heo2b29ac22007-06-14 03:45:15 +090034static void fill_object_path(struct sysfs_dirent *sd, char *buffer, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035{
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 --length;
Tejun Heo2b29ac22007-06-14 03:45:15 +090037 for (; sd->s_parent; sd = sd->s_parent) {
38 int cur = strlen(sd->s_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40 /* back up enough to print this bus id with '/' */
41 length -= cur;
Tejun Heo2b29ac22007-06-14 03:45:15 +090042 strncpy(buffer + length, sd->s_name, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 *(buffer + --length) = '/';
44 }
45}
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047/**
48 * sysfs_create_link - create symlink between two objects.
49 * @kobj: object whose directory we're creating the link in.
50 * @target: object we're pointing to.
51 * @name: name of the symlink.
52 */
Dmitry Torokhove3a15db2005-04-26 02:31:08 -050053int sysfs_create_link(struct kobject * kobj, struct kobject * target, const char * name)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
Tejun Heo2b29ac22007-06-14 03:45:15 +090055 struct sysfs_dirent *parent_sd = NULL;
56 struct sysfs_dirent *target_sd = NULL;
Tejun Heo3007e992007-06-14 04:27:23 +090057 struct sysfs_dirent *sd = NULL;
58 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Greg Kroah-Hartmanceeee1f2002-04-09 12:14:34 -070060 BUG_ON(!name);
61
62 if (!kobj) {
63 if (sysfs_mount && sysfs_mount->mnt_sb)
Tejun Heo608e2662007-06-14 04:27:22 +090064 parent_sd = sysfs_mount->mnt_sb->s_root->d_fsdata;
Greg Kroah-Hartmanceeee1f2002-04-09 12:14:34 -070065 } else
Tejun Heo608e2662007-06-14 04:27:22 +090066 parent_sd = kobj->sd;
Greg Kroah-Hartmanceeee1f2002-04-09 12:14:34 -070067
Tejun Heo3007e992007-06-14 04:27:23 +090068 error = -EFAULT;
Tejun Heo608e2662007-06-14 04:27:22 +090069 if (!parent_sd)
Tejun Heo3007e992007-06-14 04:27:23 +090070 goto out_put;
Tejun Heo2b29ac22007-06-14 03:45:15 +090071
Tejun Heo608e2662007-06-14 04:27:22 +090072 /* target->sd can go away beneath us but is protected with
Tejun Heo5f995322007-06-14 04:27:23 +090073 * sysfs_assoc_lock. Fetch target_sd from it.
Tejun Heo2b29ac22007-06-14 03:45:15 +090074 */
Tejun Heo5f995322007-06-14 04:27:23 +090075 spin_lock(&sysfs_assoc_lock);
Tejun Heo608e2662007-06-14 04:27:22 +090076 if (target->sd)
77 target_sd = sysfs_get(target->sd);
Tejun Heo5f995322007-06-14 04:27:23 +090078 spin_unlock(&sysfs_assoc_lock);
Tejun Heo2b29ac22007-06-14 03:45:15 +090079
Tejun Heo3007e992007-06-14 04:27:23 +090080 error = -ENOENT;
Tejun Heo2b29ac22007-06-14 03:45:15 +090081 if (!target_sd)
Tejun Heo3007e992007-06-14 04:27:23 +090082 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Tejun Heo3007e992007-06-14 04:27:23 +090084 error = -ENOMEM;
85 sd = sysfs_new_dirent(name, S_IFLNK|S_IRWXUGO, SYSFS_KOBJ_LINK);
86 if (!sd)
87 goto out_put;
88 sd->s_elem.symlink.target_sd = target_sd;
Tejun Heo2b29ac22007-06-14 03:45:15 +090089
Tejun Heo3007e992007-06-14 04:27:23 +090090 mutex_lock(&sysfs_mutex);
91 error = -EEXIST;
92 if (sysfs_find_dirent(parent_sd, name))
93 goto out_unlock;
94 sysfs_attach_dirent(sd, parent_sd, NULL);
95 mutex_unlock(&sysfs_mutex);
Tejun Heo2b29ac22007-06-14 03:45:15 +090096
Tejun Heo3007e992007-06-14 04:27:23 +090097 return 0;
98
99 out_unlock:
100 mutex_unlock(&sysfs_mutex);
101 out_put:
102 sysfs_put(target_sd);
103 sysfs_put(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 return error;
105}
106
107
108/**
109 * sysfs_remove_link - remove symlink in object's directory.
110 * @kobj: object we're acting for.
111 * @name: name of the symlink to remove.
112 */
113
Dmitry Torokhove3a15db2005-04-26 02:31:08 -0500114void sysfs_remove_link(struct kobject * kobj, const char * name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
Tejun Heo608e2662007-06-14 04:27:22 +0900116 sysfs_hash_and_remove(kobj->sd, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
118
Tejun Heo2b29ac22007-06-14 03:45:15 +0900119static int sysfs_get_target_path(struct sysfs_dirent * parent_sd,
120 struct sysfs_dirent * target_sd, char *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
122 char * s;
123 int depth, size;
124
Tejun Heo2b29ac22007-06-14 03:45:15 +0900125 depth = object_depth(parent_sd);
126 size = object_path_length(target_sd) + depth * 3 - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 if (size > PATH_MAX)
128 return -ENAMETOOLONG;
129
130 pr_debug("%s: depth = %d, size = %d\n", __FUNCTION__, depth, size);
131
132 for (s = path; depth--; s += 3)
133 strcpy(s,"../");
134
Tejun Heo2b29ac22007-06-14 03:45:15 +0900135 fill_object_path(target_sd, path, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 pr_debug("%s: path = '%s'\n", __FUNCTION__, path);
137
138 return 0;
139}
140
141static int sysfs_getlink(struct dentry *dentry, char * path)
142{
Tejun Heo2b29ac22007-06-14 03:45:15 +0900143 struct sysfs_dirent *sd = dentry->d_fsdata;
144 struct sysfs_dirent *parent_sd = sd->s_parent;
145 struct sysfs_dirent *target_sd = sd->s_elem.symlink.target_sd;
146 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Tejun Heo3007e992007-06-14 04:27:23 +0900148 mutex_lock(&sysfs_mutex);
Tejun Heo2b29ac22007-06-14 03:45:15 +0900149 error = sysfs_get_target_path(parent_sd, target_sd, path);
Tejun Heo3007e992007-06-14 04:27:23 +0900150 mutex_unlock(&sysfs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Tejun Heo2b29ac22007-06-14 03:45:15 +0900152 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
154
Linus Torvaldscc314ee2005-08-19 18:02:56 -0700155static void *sysfs_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
157 int error = -ENOMEM;
158 unsigned long page = get_zeroed_page(GFP_KERNEL);
159 if (page)
160 error = sysfs_getlink(dentry, (char *) page);
161 nd_set_link(nd, error ? ERR_PTR(error) : (char *)page);
Linus Torvaldscc314ee2005-08-19 18:02:56 -0700162 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
164
Linus Torvaldscc314ee2005-08-19 18:02:56 -0700165static void sysfs_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
167 char *page = nd_get_link(nd);
168 if (!IS_ERR(page))
169 free_page((unsigned long)page);
170}
171
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800172const struct inode_operations sysfs_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 .readlink = generic_readlink,
174 .follow_link = sysfs_follow_link,
175 .put_link = sysfs_put_link,
176};
177
178
179EXPORT_SYMBOL_GPL(sysfs_create_link);
180EXPORT_SYMBOL_GPL(sysfs_remove_link);