blob: 2f86e04222907b2c19aab03adf6d2f2cabe092d1 [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;
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;
89 sd->s_elem.symlink.target_sd = target_sd;
Tejun Heo2b29ac22007-06-14 03:45:15 +090090
Tejun Heofb6896d2007-06-14 04:27:24 +090091 sysfs_addrm_start(&acxt, parent_sd);
92
93 if (!sysfs_find_dirent(parent_sd, name)) {
94 sysfs_add_one(&acxt, sd);
95 sysfs_link_sibling(sd);
96 }
97
98 if (sysfs_addrm_finish(&acxt))
99 return 0;
100
Tejun Heo3007e992007-06-14 04:27:23 +0900101 error = -EEXIST;
Tejun Heofb6896d2007-06-14 04:27:24 +0900102 /* fall through */
Tejun Heo3007e992007-06-14 04:27:23 +0900103 out_put:
104 sysfs_put(target_sd);
105 sysfs_put(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 return error;
107}
108
109
110/**
111 * sysfs_remove_link - remove symlink in object's directory.
112 * @kobj: object we're acting for.
113 * @name: name of the symlink to remove.
114 */
115
Dmitry Torokhove3a15db2005-04-26 02:31:08 -0500116void sysfs_remove_link(struct kobject * kobj, const char * name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
Tejun Heo608e2662007-06-14 04:27:22 +0900118 sysfs_hash_and_remove(kobj->sd, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
120
Tejun Heo2b29ac22007-06-14 03:45:15 +0900121static int sysfs_get_target_path(struct sysfs_dirent * parent_sd,
122 struct sysfs_dirent * target_sd, char *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
124 char * s;
125 int depth, size;
126
Tejun Heo2b29ac22007-06-14 03:45:15 +0900127 depth = object_depth(parent_sd);
128 size = object_path_length(target_sd) + depth * 3 - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 if (size > PATH_MAX)
130 return -ENAMETOOLONG;
131
132 pr_debug("%s: depth = %d, size = %d\n", __FUNCTION__, depth, size);
133
134 for (s = path; depth--; s += 3)
135 strcpy(s,"../");
136
Tejun Heo2b29ac22007-06-14 03:45:15 +0900137 fill_object_path(target_sd, path, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 pr_debug("%s: path = '%s'\n", __FUNCTION__, path);
139
140 return 0;
141}
142
143static int sysfs_getlink(struct dentry *dentry, char * path)
144{
Tejun Heo2b29ac22007-06-14 03:45:15 +0900145 struct sysfs_dirent *sd = dentry->d_fsdata;
146 struct sysfs_dirent *parent_sd = sd->s_parent;
147 struct sysfs_dirent *target_sd = sd->s_elem.symlink.target_sd;
148 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Tejun Heo3007e992007-06-14 04:27:23 +0900150 mutex_lock(&sysfs_mutex);
Tejun Heo2b29ac22007-06-14 03:45:15 +0900151 error = sysfs_get_target_path(parent_sd, target_sd, path);
Tejun Heo3007e992007-06-14 04:27:23 +0900152 mutex_unlock(&sysfs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Tejun Heo2b29ac22007-06-14 03:45:15 +0900154 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
156
Linus Torvaldscc314ee2005-08-19 18:02:56 -0700157static void *sysfs_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
159 int error = -ENOMEM;
160 unsigned long page = get_zeroed_page(GFP_KERNEL);
161 if (page)
162 error = sysfs_getlink(dentry, (char *) page);
163 nd_set_link(nd, error ? ERR_PTR(error) : (char *)page);
Linus Torvaldscc314ee2005-08-19 18:02:56 -0700164 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
166
Linus Torvaldscc314ee2005-08-19 18:02:56 -0700167static void sysfs_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
169 char *page = nd_get_link(nd);
170 if (!IS_ERR(page))
171 free_page((unsigned long)page);
172}
173
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800174const struct inode_operations sysfs_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 .readlink = generic_readlink,
176 .follow_link = sysfs_follow_link,
177 .put_link = sysfs_put_link,
178};
179
180
181EXPORT_SYMBOL_GPL(sysfs_create_link);
182EXPORT_SYMBOL_GPL(sysfs_remove_link);