blob: 90484533801e6daa8237a44cd60d0b2d9179f1f1 [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);
94
95 if (!sysfs_find_dirent(parent_sd, name)) {
96 sysfs_add_one(&acxt, sd);
97 sysfs_link_sibling(sd);
98 }
99
Tejun Heo967e35d2007-07-18 16:38:11 +0900100 if (!sysfs_addrm_finish(&acxt)) {
101 error = -EEXIST;
102 goto out_put;
103 }
Tejun Heofb6896d2007-06-14 04:27:24 +0900104
Tejun Heo967e35d2007-07-18 16:38:11 +0900105 return 0;
106
Tejun Heo3007e992007-06-14 04:27:23 +0900107 out_put:
108 sysfs_put(target_sd);
109 sysfs_put(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 return error;
111}
112
113
114/**
115 * sysfs_remove_link - remove symlink in object's directory.
116 * @kobj: object we're acting for.
117 * @name: name of the symlink to remove.
118 */
119
Dmitry Torokhove3a15db2005-04-26 02:31:08 -0500120void sysfs_remove_link(struct kobject * kobj, const char * name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
Tejun Heo608e2662007-06-14 04:27:22 +0900122 sysfs_hash_and_remove(kobj->sd, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
124
Tejun Heo2b29ac22007-06-14 03:45:15 +0900125static int sysfs_get_target_path(struct sysfs_dirent * parent_sd,
126 struct sysfs_dirent * target_sd, char *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
128 char * s;
129 int depth, size;
130
Tejun Heo2b29ac22007-06-14 03:45:15 +0900131 depth = object_depth(parent_sd);
132 size = object_path_length(target_sd) + depth * 3 - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 if (size > PATH_MAX)
134 return -ENAMETOOLONG;
135
136 pr_debug("%s: depth = %d, size = %d\n", __FUNCTION__, depth, size);
137
138 for (s = path; depth--; s += 3)
139 strcpy(s,"../");
140
Tejun Heo2b29ac22007-06-14 03:45:15 +0900141 fill_object_path(target_sd, path, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 pr_debug("%s: path = '%s'\n", __FUNCTION__, path);
143
144 return 0;
145}
146
147static int sysfs_getlink(struct dentry *dentry, char * path)
148{
Tejun Heo2b29ac22007-06-14 03:45:15 +0900149 struct sysfs_dirent *sd = dentry->d_fsdata;
150 struct sysfs_dirent *parent_sd = sd->s_parent;
151 struct sysfs_dirent *target_sd = sd->s_elem.symlink.target_sd;
152 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Tejun Heo3007e992007-06-14 04:27:23 +0900154 mutex_lock(&sysfs_mutex);
Tejun Heo2b29ac22007-06-14 03:45:15 +0900155 error = sysfs_get_target_path(parent_sd, target_sd, path);
Tejun Heo3007e992007-06-14 04:27:23 +0900156 mutex_unlock(&sysfs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Tejun Heo2b29ac22007-06-14 03:45:15 +0900158 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160
Linus Torvaldscc314ee2005-08-19 18:02:56 -0700161static void *sysfs_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
163 int error = -ENOMEM;
164 unsigned long page = get_zeroed_page(GFP_KERNEL);
165 if (page)
166 error = sysfs_getlink(dentry, (char *) page);
167 nd_set_link(nd, error ? ERR_PTR(error) : (char *)page);
Linus Torvaldscc314ee2005-08-19 18:02:56 -0700168 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
170
Linus Torvaldscc314ee2005-08-19 18:02:56 -0700171static void sysfs_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
173 char *page = nd_get_link(nd);
174 if (!IS_ERR(page))
175 free_page((unsigned long)page);
176}
177
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800178const struct inode_operations sysfs_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 .readlink = generic_readlink,
180 .follow_link = sysfs_follow_link,
181 .put_link = sysfs_put_link,
182};
183
184
185EXPORT_SYMBOL_GPL(sysfs_create_link);
186EXPORT_SYMBOL_GPL(sysfs_remove_link);