blob: a6b13f12b0e73c5a3275aab2301ee993f57b3bad [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>
Dave Young869512a2007-07-26 14:53:53 +000010#include <linux/mutex.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;
Tejun Heofb6896d2007-06-14 04:27:24 +090058 struct sysfs_addrm_cxt acxt;
Tejun Heo3007e992007-06-14 04:27:23 +090059 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Greg Kroah-Hartmanceeee1f2002-04-09 12:14:34 -070061 BUG_ON(!name);
62
63 if (!kobj) {
64 if (sysfs_mount && sysfs_mount->mnt_sb)
Tejun Heo608e2662007-06-14 04:27:22 +090065 parent_sd = sysfs_mount->mnt_sb->s_root->d_fsdata;
Greg Kroah-Hartmanceeee1f2002-04-09 12:14:34 -070066 } else
Tejun Heo608e2662007-06-14 04:27:22 +090067 parent_sd = kobj->sd;
Greg Kroah-Hartmanceeee1f2002-04-09 12:14:34 -070068
Tejun Heo3007e992007-06-14 04:27:23 +090069 error = -EFAULT;
Tejun Heo608e2662007-06-14 04:27:22 +090070 if (!parent_sd)
Tejun Heo3007e992007-06-14 04:27:23 +090071 goto out_put;
Tejun Heo2b29ac22007-06-14 03:45:15 +090072
Tejun Heo608e2662007-06-14 04:27:22 +090073 /* target->sd can go away beneath us but is protected with
Tejun Heo5f995322007-06-14 04:27:23 +090074 * sysfs_assoc_lock. Fetch target_sd from it.
Tejun Heo2b29ac22007-06-14 03:45:15 +090075 */
Tejun Heo5f995322007-06-14 04:27:23 +090076 spin_lock(&sysfs_assoc_lock);
Tejun Heo608e2662007-06-14 04:27:22 +090077 if (target->sd)
78 target_sd = sysfs_get(target->sd);
Tejun Heo5f995322007-06-14 04:27:23 +090079 spin_unlock(&sysfs_assoc_lock);
Tejun Heo2b29ac22007-06-14 03:45:15 +090080
Tejun Heo3007e992007-06-14 04:27:23 +090081 error = -ENOENT;
Tejun Heo2b29ac22007-06-14 03:45:15 +090082 if (!target_sd)
Tejun Heo3007e992007-06-14 04:27:23 +090083 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Tejun Heo3007e992007-06-14 04:27:23 +090085 error = -ENOMEM;
86 sd = sysfs_new_dirent(name, S_IFLNK|S_IRWXUGO, SYSFS_KOBJ_LINK);
87 if (!sd)
88 goto out_put;
Tejun Heoa1da4df2007-07-18 16:14:45 +090089
Tejun Heo3007e992007-06-14 04:27:23 +090090 sd->s_elem.symlink.target_sd = target_sd;
Tejun Heoa1da4df2007-07-18 16:14:45 +090091 target_sd = NULL; /* reference is now owned by the symlink */
Tejun Heo2b29ac22007-06-14 03:45:15 +090092
Tejun Heofb6896d2007-06-14 04:27:24 +090093 sysfs_addrm_start(&acxt, parent_sd);
Tejun Heo23dc2792007-08-02 21:38:03 +090094 error = sysfs_add_one(&acxt, sd);
95 sysfs_addrm_finish(&acxt);
Tejun Heofb6896d2007-06-14 04:27:24 +090096
Tejun Heo23dc2792007-08-02 21:38:03 +090097 if (error)
Tejun Heo967e35d2007-07-18 16:38:11 +090098 goto out_put;
Tejun Heofb6896d2007-06-14 04:27:24 +090099
Tejun Heo967e35d2007-07-18 16:38:11 +0900100 return 0;
101
Tejun Heo3007e992007-06-14 04:27:23 +0900102 out_put:
103 sysfs_put(target_sd);
104 sysfs_put(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 return error;
106}
107
108
109/**
110 * sysfs_remove_link - remove symlink in object's directory.
111 * @kobj: object we're acting for.
112 * @name: name of the symlink to remove.
113 */
114
Dmitry Torokhove3a15db2005-04-26 02:31:08 -0500115void sysfs_remove_link(struct kobject * kobj, const char * name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
Tejun Heo608e2662007-06-14 04:27:22 +0900117 sysfs_hash_and_remove(kobj->sd, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
119
Tejun Heo2b29ac22007-06-14 03:45:15 +0900120static int sysfs_get_target_path(struct sysfs_dirent * parent_sd,
121 struct sysfs_dirent * target_sd, char *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
123 char * s;
124 int depth, size;
125
Tejun Heo2b29ac22007-06-14 03:45:15 +0900126 depth = object_depth(parent_sd);
127 size = object_path_length(target_sd) + depth * 3 - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 if (size > PATH_MAX)
129 return -ENAMETOOLONG;
130
131 pr_debug("%s: depth = %d, size = %d\n", __FUNCTION__, depth, size);
132
133 for (s = path; depth--; s += 3)
134 strcpy(s,"../");
135
Tejun Heo2b29ac22007-06-14 03:45:15 +0900136 fill_object_path(target_sd, path, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 pr_debug("%s: path = '%s'\n", __FUNCTION__, path);
138
139 return 0;
140}
141
142static int sysfs_getlink(struct dentry *dentry, char * path)
143{
Tejun Heo2b29ac22007-06-14 03:45:15 +0900144 struct sysfs_dirent *sd = dentry->d_fsdata;
145 struct sysfs_dirent *parent_sd = sd->s_parent;
146 struct sysfs_dirent *target_sd = sd->s_elem.symlink.target_sd;
147 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Tejun Heo3007e992007-06-14 04:27:23 +0900149 mutex_lock(&sysfs_mutex);
Tejun Heo2b29ac22007-06-14 03:45:15 +0900150 error = sysfs_get_target_path(parent_sd, target_sd, path);
Tejun Heo3007e992007-06-14 04:27:23 +0900151 mutex_unlock(&sysfs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Tejun Heo2b29ac22007-06-14 03:45:15 +0900153 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154}
155
Linus Torvaldscc314ee2005-08-19 18:02:56 -0700156static void *sysfs_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
158 int error = -ENOMEM;
159 unsigned long page = get_zeroed_page(GFP_KERNEL);
160 if (page)
161 error = sysfs_getlink(dentry, (char *) page);
162 nd_set_link(nd, error ? ERR_PTR(error) : (char *)page);
Linus Torvaldscc314ee2005-08-19 18:02:56 -0700163 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}
165
Linus Torvaldscc314ee2005-08-19 18:02:56 -0700166static void sysfs_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
168 char *page = nd_get_link(nd);
169 if (!IS_ERR(page))
170 free_page((unsigned long)page);
171}
172
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800173const struct inode_operations sysfs_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 .readlink = generic_readlink,
175 .follow_link = sysfs_follow_link,
176 .put_link = sysfs_put_link,
177};
178
179
180EXPORT_SYMBOL_GPL(sysfs_create_link);
181EXPORT_SYMBOL_GPL(sysfs_remove_link);