blob: 43cc5222f136bcce5c0e1549f281875d7478f21b [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
Tejun Heo2b29ac22007-06-14 03:45:15 +090047static int sysfs_add_link(struct sysfs_dirent * parent_sd, const char * name,
48 struct sysfs_dirent * target_sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
Tejun Heoa26cd722007-06-14 03:45:14 +090050 struct sysfs_dirent * sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Tejun Heo3e519032007-06-14 03:45:15 +090052 sd = sysfs_new_dirent(name, S_IFLNK|S_IRWXUGO, SYSFS_KOBJ_LINK);
Tejun Heoa26cd722007-06-14 03:45:14 +090053 if (!sd)
Tejun Heo3e519032007-06-14 03:45:15 +090054 return -ENOMEM;
55
Tejun Heo2b29ac22007-06-14 03:45:15 +090056 sd->s_elem.symlink.target_sd = target_sd;
Tejun Heoa26cd722007-06-14 03:45:14 +090057 sysfs_attach_dirent(sd, parent_sd, NULL);
Tejun Heodfeb9fb2007-06-14 03:45:14 +090058 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059}
60
61/**
62 * sysfs_create_link - create symlink between two objects.
63 * @kobj: object whose directory we're creating the link in.
64 * @target: object we're pointing to.
65 * @name: name of the symlink.
66 */
Dmitry Torokhove3a15db2005-04-26 02:31:08 -050067int sysfs_create_link(struct kobject * kobj, struct kobject * target, const char * name)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
Tejun Heo2b29ac22007-06-14 03:45:15 +090069 struct sysfs_dirent *parent_sd = NULL;
70 struct sysfs_dirent *target_sd = NULL;
Maneesh Sonic5168652006-03-09 19:40:14 +053071 int error = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Greg Kroah-Hartmanceeee1f2002-04-09 12:14:34 -070073 BUG_ON(!name);
74
75 if (!kobj) {
76 if (sysfs_mount && sysfs_mount->mnt_sb)
Tejun Heo608e2662007-06-14 04:27:22 +090077 parent_sd = sysfs_mount->mnt_sb->s_root->d_fsdata;
Greg Kroah-Hartmanceeee1f2002-04-09 12:14:34 -070078 } else
Tejun Heo608e2662007-06-14 04:27:22 +090079 parent_sd = kobj->sd;
Greg Kroah-Hartmanceeee1f2002-04-09 12:14:34 -070080
Tejun Heo608e2662007-06-14 04:27:22 +090081 if (!parent_sd)
Greg Kroah-Hartmanceeee1f2002-04-09 12:14:34 -070082 return -EFAULT;
Tejun Heo2b29ac22007-06-14 03:45:15 +090083
Tejun Heo608e2662007-06-14 04:27:22 +090084 /* target->sd can go away beneath us but is protected with
Tejun Heo2b29ac22007-06-14 03:45:15 +090085 * kobj_sysfs_assoc_lock. Fetch target_sd from it.
86 */
87 spin_lock(&kobj_sysfs_assoc_lock);
Tejun Heo608e2662007-06-14 04:27:22 +090088 if (target->sd)
89 target_sd = sysfs_get(target->sd);
Tejun Heo2b29ac22007-06-14 03:45:15 +090090 spin_unlock(&kobj_sysfs_assoc_lock);
91
92 if (!target_sd)
93 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Tejun Heo608e2662007-06-14 04:27:22 +090095 mutex_lock(&parent_sd->s_dentry->d_inode->i_mutex);
96 if (!sysfs_find_dirent(parent_sd, name))
Tejun Heo2b29ac22007-06-14 03:45:15 +090097 error = sysfs_add_link(parent_sd, name, target_sd);
Tejun Heo608e2662007-06-14 04:27:22 +090098 mutex_unlock(&parent_sd->s_dentry->d_inode->i_mutex);
Tejun Heo2b29ac22007-06-14 03:45:15 +090099
100 if (error)
101 sysfs_put(target_sd);
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 return error;
104}
105
106
107/**
108 * sysfs_remove_link - remove symlink in object's directory.
109 * @kobj: object we're acting for.
110 * @name: name of the symlink to remove.
111 */
112
Dmitry Torokhove3a15db2005-04-26 02:31:08 -0500113void sysfs_remove_link(struct kobject * kobj, const char * name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
Tejun Heo608e2662007-06-14 04:27:22 +0900115 sysfs_hash_and_remove(kobj->sd, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
Tejun Heo2b29ac22007-06-14 03:45:15 +0900118static int sysfs_get_target_path(struct sysfs_dirent * parent_sd,
119 struct sysfs_dirent * target_sd, char *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
121 char * s;
122 int depth, size;
123
Tejun Heo2b29ac22007-06-14 03:45:15 +0900124 depth = object_depth(parent_sd);
125 size = object_path_length(target_sd) + depth * 3 - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 if (size > PATH_MAX)
127 return -ENAMETOOLONG;
128
129 pr_debug("%s: depth = %d, size = %d\n", __FUNCTION__, depth, size);
130
131 for (s = path; depth--; s += 3)
132 strcpy(s,"../");
133
Tejun Heo2b29ac22007-06-14 03:45:15 +0900134 fill_object_path(target_sd, path, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 pr_debug("%s: path = '%s'\n", __FUNCTION__, path);
136
137 return 0;
138}
139
140static int sysfs_getlink(struct dentry *dentry, char * path)
141{
Tejun Heo2b29ac22007-06-14 03:45:15 +0900142 struct sysfs_dirent *sd = dentry->d_fsdata;
143 struct sysfs_dirent *parent_sd = sd->s_parent;
144 struct sysfs_dirent *target_sd = sd->s_elem.symlink.target_sd;
145 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147 down_read(&sysfs_rename_sem);
Tejun Heo2b29ac22007-06-14 03:45:15 +0900148 error = sysfs_get_target_path(parent_sd, target_sd, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 up_read(&sysfs_rename_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Tejun Heo2b29ac22007-06-14 03:45:15 +0900151 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152}
153
Linus Torvaldscc314ee2005-08-19 18:02:56 -0700154static void *sysfs_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
156 int error = -ENOMEM;
157 unsigned long page = get_zeroed_page(GFP_KERNEL);
158 if (page)
159 error = sysfs_getlink(dentry, (char *) page);
160 nd_set_link(nd, error ? ERR_PTR(error) : (char *)page);
Linus Torvaldscc314ee2005-08-19 18:02:56 -0700161 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
163
Linus Torvaldscc314ee2005-08-19 18:02:56 -0700164static void sysfs_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
166 char *page = nd_get_link(nd);
167 if (!IS_ERR(page))
168 free_page((unsigned long)page);
169}
170
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800171const struct inode_operations sysfs_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 .readlink = generic_readlink,
173 .follow_link = sysfs_follow_link,
174 .put_link = sysfs_put_link,
175};
176
177
178EXPORT_SYMBOL_GPL(sysfs_create_link);
179EXPORT_SYMBOL_GPL(sysfs_remove_link);