blob: a3ba217fbe74f4313a1dca88a8cc863e14766341 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Tejun Heo6d66f5c2007-09-20 17:31:38 +09002 * fs/sysfs/symlink.c - sysfs symlink implementation
3 *
4 * Copyright (c) 2001-3 Patrick Mochel
5 * Copyright (c) 2007 SUSE Linux Products GmbH
6 * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
7 *
8 * This file is released under the GPLv2.
9 *
10 * Please see Documentation/filesystems/sysfs.txt for more information.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
13#include <linux/fs.h>
Greg Kroah-Hartmanceeee1f2002-04-09 12:14:34 -070014#include <linux/mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/module.h>
16#include <linux/kobject.h>
17#include <linux/namei.h>
Dave Young869512a2007-07-26 14:53:53 +000018#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20#include "sysfs.h"
21
Cornelia Huck36ce6da2008-06-10 11:09:08 +020022static int sysfs_do_create_link(struct kobject *kobj, struct kobject *target,
23 const char *name, int warn)
Linus Torvalds1da177e2005-04-16 15:20:36 -070024{
Tejun Heo2b29ac22007-06-14 03:45:15 +090025 struct sysfs_dirent *parent_sd = NULL;
26 struct sysfs_dirent *target_sd = NULL;
Tejun Heo3007e992007-06-14 04:27:23 +090027 struct sysfs_dirent *sd = NULL;
Tejun Heofb6896d2007-06-14 04:27:24 +090028 struct sysfs_addrm_cxt acxt;
Tejun Heo3007e992007-06-14 04:27:23 +090029 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Greg Kroah-Hartmanceeee1f2002-04-09 12:14:34 -070031 BUG_ON(!name);
32
Eric W. Biederman7d0c7d62007-08-20 21:36:30 +090033 if (!kobj)
34 parent_sd = &sysfs_root;
35 else
Tejun Heo608e2662007-06-14 04:27:22 +090036 parent_sd = kobj->sd;
Greg Kroah-Hartmanceeee1f2002-04-09 12:14:34 -070037
Tejun Heo3007e992007-06-14 04:27:23 +090038 error = -EFAULT;
Tejun Heo608e2662007-06-14 04:27:22 +090039 if (!parent_sd)
Tejun Heo3007e992007-06-14 04:27:23 +090040 goto out_put;
Tejun Heo2b29ac22007-06-14 03:45:15 +090041
Tejun Heo608e2662007-06-14 04:27:22 +090042 /* target->sd can go away beneath us but is protected with
Tejun Heo5f995322007-06-14 04:27:23 +090043 * sysfs_assoc_lock. Fetch target_sd from it.
Tejun Heo2b29ac22007-06-14 03:45:15 +090044 */
Tejun Heo5f995322007-06-14 04:27:23 +090045 spin_lock(&sysfs_assoc_lock);
Tejun Heo608e2662007-06-14 04:27:22 +090046 if (target->sd)
47 target_sd = sysfs_get(target->sd);
Tejun Heo5f995322007-06-14 04:27:23 +090048 spin_unlock(&sysfs_assoc_lock);
Tejun Heo2b29ac22007-06-14 03:45:15 +090049
Tejun Heo3007e992007-06-14 04:27:23 +090050 error = -ENOENT;
Tejun Heo2b29ac22007-06-14 03:45:15 +090051 if (!target_sd)
Tejun Heo3007e992007-06-14 04:27:23 +090052 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Tejun Heo3007e992007-06-14 04:27:23 +090054 error = -ENOMEM;
55 sd = sysfs_new_dirent(name, S_IFLNK|S_IRWXUGO, SYSFS_KOBJ_LINK);
56 if (!sd)
57 goto out_put;
Tejun Heoa1da4df2007-07-18 16:14:45 +090058
Tejun Heob1fc3d62007-09-20 16:05:11 +090059 sd->s_symlink.target_sd = target_sd;
Tejun Heoa1da4df2007-07-18 16:14:45 +090060 target_sd = NULL; /* reference is now owned by the symlink */
Tejun Heo2b29ac22007-06-14 03:45:15 +090061
Tejun Heofb6896d2007-06-14 04:27:24 +090062 sysfs_addrm_start(&acxt, parent_sd);
Cornelia Huck36ce6da2008-06-10 11:09:08 +020063 if (warn)
64 error = sysfs_add_one(&acxt, sd);
65 else
66 error = __sysfs_add_one(&acxt, sd);
Tejun Heo23dc2792007-08-02 21:38:03 +090067 sysfs_addrm_finish(&acxt);
Tejun Heofb6896d2007-06-14 04:27:24 +090068
Tejun Heo23dc2792007-08-02 21:38:03 +090069 if (error)
Tejun Heo967e35d2007-07-18 16:38:11 +090070 goto out_put;
Tejun Heofb6896d2007-06-14 04:27:24 +090071
Tejun Heo967e35d2007-07-18 16:38:11 +090072 return 0;
73
Tejun Heo3007e992007-06-14 04:27:23 +090074 out_put:
75 sysfs_put(target_sd);
76 sysfs_put(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 return error;
78}
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080/**
Cornelia Huck36ce6da2008-06-10 11:09:08 +020081 * sysfs_create_link - create symlink between two objects.
82 * @kobj: object whose directory we're creating the link in.
83 * @target: object we're pointing to.
84 * @name: name of the symlink.
85 */
86int sysfs_create_link(struct kobject *kobj, struct kobject *target,
87 const char *name)
88{
89 return sysfs_do_create_link(kobj, target, name, 1);
90}
91
92/**
93 * sysfs_create_link_nowarn - create symlink between two objects.
94 * @kobj: object whose directory we're creating the link in.
95 * @target: object we're pointing to.
96 * @name: name of the symlink.
97 *
98 * This function does the same as sysf_create_link(), but it
99 * doesn't warn if the link already exists.
100 */
101int sysfs_create_link_nowarn(struct kobject *kobj, struct kobject *target,
102 const char *name)
103{
104 return sysfs_do_create_link(kobj, target, name, 0);
105}
106
107/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 * 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{
Mark Fasheha839c5a2008-01-29 14:35:18 -0800115 struct sysfs_dirent *parent_sd = NULL;
116
117 if (!kobj)
118 parent_sd = &sysfs_root;
119 else
120 parent_sd = kobj->sd;
121
122 sysfs_hash_and_remove(parent_sd, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
124
Kay Sievers2f90a852007-11-01 20:20:52 +0100125static 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{
Kay Sievers2f90a852007-11-01 20:20:52 +0100128 struct sysfs_dirent *base, *sd;
129 char *s = path;
130 int len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Kay Sievers2f90a852007-11-01 20:20:52 +0100132 /* go up to the root, stop at the base */
133 base = parent_sd;
134 while (base->s_parent) {
135 sd = target_sd->s_parent;
136 while (sd->s_parent && base != sd)
137 sd = sd->s_parent;
138
139 if (base == sd)
140 break;
141
142 strcpy(s, "../");
143 s += 3;
144 base = base->s_parent;
145 }
146
147 /* determine end of target string for reverse fillup */
148 sd = target_sd;
149 while (sd->s_parent && sd != base) {
150 len += strlen(sd->s_name) + 1;
151 sd = sd->s_parent;
152 }
153
154 /* check limits */
155 if (len < 2)
156 return -EINVAL;
157 len--;
158 if ((s - path) + len > PATH_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 return -ENAMETOOLONG;
160
Kay Sievers2f90a852007-11-01 20:20:52 +0100161 /* reverse fillup of target string from target to base */
162 sd = target_sd;
163 while (sd->s_parent && sd != base) {
164 int slen = strlen(sd->s_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Kay Sievers2f90a852007-11-01 20:20:52 +0100166 len -= slen;
167 strncpy(s + len, sd->s_name, slen);
168 if (len)
169 s[--len] = '/';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Kay Sievers2f90a852007-11-01 20:20:52 +0100171 sd = sd->s_parent;
172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 return 0;
175}
176
177static int sysfs_getlink(struct dentry *dentry, char * path)
178{
Tejun Heo2b29ac22007-06-14 03:45:15 +0900179 struct sysfs_dirent *sd = dentry->d_fsdata;
180 struct sysfs_dirent *parent_sd = sd->s_parent;
Tejun Heob1fc3d62007-09-20 16:05:11 +0900181 struct sysfs_dirent *target_sd = sd->s_symlink.target_sd;
Tejun Heo2b29ac22007-06-14 03:45:15 +0900182 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Tejun Heo3007e992007-06-14 04:27:23 +0900184 mutex_lock(&sysfs_mutex);
Tejun Heo2b29ac22007-06-14 03:45:15 +0900185 error = sysfs_get_target_path(parent_sd, target_sd, path);
Tejun Heo3007e992007-06-14 04:27:23 +0900186 mutex_unlock(&sysfs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Tejun Heo2b29ac22007-06-14 03:45:15 +0900188 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189}
190
Linus Torvaldscc314ee2005-08-19 18:02:56 -0700191static void *sysfs_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
193 int error = -ENOMEM;
194 unsigned long page = get_zeroed_page(GFP_KERNEL);
195 if (page)
196 error = sysfs_getlink(dentry, (char *) page);
197 nd_set_link(nd, error ? ERR_PTR(error) : (char *)page);
Linus Torvaldscc314ee2005-08-19 18:02:56 -0700198 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
200
Linus Torvaldscc314ee2005-08-19 18:02:56 -0700201static void sysfs_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
203 char *page = nd_get_link(nd);
204 if (!IS_ERR(page))
205 free_page((unsigned long)page);
206}
207
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800208const struct inode_operations sysfs_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 .readlink = generic_readlink,
210 .follow_link = sysfs_follow_link,
211 .put_link = sysfs_put_link,
212};
213
214
215EXPORT_SYMBOL_GPL(sysfs_create_link);
216EXPORT_SYMBOL_GPL(sysfs_remove_link);