blob: 5f66c446615176eff46e1864a7343f228bac19e6 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070022/**
23 * sysfs_create_link - create symlink between two objects.
24 * @kobj: object whose directory we're creating the link in.
25 * @target: object we're pointing to.
26 * @name: name of the symlink.
27 */
Dmitry Torokhove3a15db2005-04-26 02:31:08 -050028int sysfs_create_link(struct kobject * kobj, struct kobject * target, const char * name)
Linus Torvalds1da177e2005-04-16 15:20:36 -070029{
Tejun Heo2b29ac22007-06-14 03:45:15 +090030 struct sysfs_dirent *parent_sd = NULL;
31 struct sysfs_dirent *target_sd = NULL;
Tejun Heo3007e992007-06-14 04:27:23 +090032 struct sysfs_dirent *sd = NULL;
Tejun Heofb6896d2007-06-14 04:27:24 +090033 struct sysfs_addrm_cxt acxt;
Tejun Heo3007e992007-06-14 04:27:23 +090034 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Greg Kroah-Hartmanceeee1f2002-04-09 12:14:34 -070036 BUG_ON(!name);
37
Eric W. Biederman7d0c7d62007-08-20 21:36:30 +090038 if (!kobj)
39 parent_sd = &sysfs_root;
40 else
Tejun Heo608e2662007-06-14 04:27:22 +090041 parent_sd = kobj->sd;
Greg Kroah-Hartmanceeee1f2002-04-09 12:14:34 -070042
Tejun Heo3007e992007-06-14 04:27:23 +090043 error = -EFAULT;
Tejun Heo608e2662007-06-14 04:27:22 +090044 if (!parent_sd)
Tejun Heo3007e992007-06-14 04:27:23 +090045 goto out_put;
Tejun Heo2b29ac22007-06-14 03:45:15 +090046
Tejun Heo608e2662007-06-14 04:27:22 +090047 /* target->sd can go away beneath us but is protected with
Tejun Heo5f995322007-06-14 04:27:23 +090048 * sysfs_assoc_lock. Fetch target_sd from it.
Tejun Heo2b29ac22007-06-14 03:45:15 +090049 */
Tejun Heo5f995322007-06-14 04:27:23 +090050 spin_lock(&sysfs_assoc_lock);
Tejun Heo608e2662007-06-14 04:27:22 +090051 if (target->sd)
52 target_sd = sysfs_get(target->sd);
Tejun Heo5f995322007-06-14 04:27:23 +090053 spin_unlock(&sysfs_assoc_lock);
Tejun Heo2b29ac22007-06-14 03:45:15 +090054
Tejun Heo3007e992007-06-14 04:27:23 +090055 error = -ENOENT;
Tejun Heo2b29ac22007-06-14 03:45:15 +090056 if (!target_sd)
Tejun Heo3007e992007-06-14 04:27:23 +090057 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Tejun Heo3007e992007-06-14 04:27:23 +090059 error = -ENOMEM;
60 sd = sysfs_new_dirent(name, S_IFLNK|S_IRWXUGO, SYSFS_KOBJ_LINK);
61 if (!sd)
62 goto out_put;
Tejun Heoa1da4df2007-07-18 16:14:45 +090063
Tejun Heob1fc3d62007-09-20 16:05:11 +090064 sd->s_symlink.target_sd = target_sd;
Tejun Heoa1da4df2007-07-18 16:14:45 +090065 target_sd = NULL; /* reference is now owned by the symlink */
Tejun Heo2b29ac22007-06-14 03:45:15 +090066
Tejun Heofb6896d2007-06-14 04:27:24 +090067 sysfs_addrm_start(&acxt, parent_sd);
Tejun Heo23dc2792007-08-02 21:38:03 +090068 error = sysfs_add_one(&acxt, sd);
69 sysfs_addrm_finish(&acxt);
Tejun Heofb6896d2007-06-14 04:27:24 +090070
Tejun Heo23dc2792007-08-02 21:38:03 +090071 if (error)
Tejun Heo967e35d2007-07-18 16:38:11 +090072 goto out_put;
Tejun Heofb6896d2007-06-14 04:27:24 +090073
Tejun Heo967e35d2007-07-18 16:38:11 +090074 return 0;
75
Tejun Heo3007e992007-06-14 04:27:23 +090076 out_put:
77 sysfs_put(target_sd);
78 sysfs_put(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 return error;
80}
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082/**
83 * sysfs_remove_link - remove symlink in object's directory.
84 * @kobj: object we're acting for.
85 * @name: name of the symlink to remove.
86 */
87
Dmitry Torokhove3a15db2005-04-26 02:31:08 -050088void sysfs_remove_link(struct kobject * kobj, const char * name)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
Tejun Heo608e2662007-06-14 04:27:22 +090090 sysfs_hash_and_remove(kobj->sd, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
92
Kay Sievers2f90a852007-11-01 20:20:52 +010093static int sysfs_get_target_path(struct sysfs_dirent *parent_sd,
94 struct sysfs_dirent *target_sd, char *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
Kay Sievers2f90a852007-11-01 20:20:52 +010096 struct sysfs_dirent *base, *sd;
97 char *s = path;
98 int len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Kay Sievers2f90a852007-11-01 20:20:52 +0100100 /* go up to the root, stop at the base */
101 base = parent_sd;
102 while (base->s_parent) {
103 sd = target_sd->s_parent;
104 while (sd->s_parent && base != sd)
105 sd = sd->s_parent;
106
107 if (base == sd)
108 break;
109
110 strcpy(s, "../");
111 s += 3;
112 base = base->s_parent;
113 }
114
115 /* determine end of target string for reverse fillup */
116 sd = target_sd;
117 while (sd->s_parent && sd != base) {
118 len += strlen(sd->s_name) + 1;
119 sd = sd->s_parent;
120 }
121
122 /* check limits */
123 if (len < 2)
124 return -EINVAL;
125 len--;
126 if ((s - path) + len > PATH_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 return -ENAMETOOLONG;
128
Kay Sievers2f90a852007-11-01 20:20:52 +0100129 /* reverse fillup of target string from target to base */
130 sd = target_sd;
131 while (sd->s_parent && sd != base) {
132 int slen = strlen(sd->s_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Kay Sievers2f90a852007-11-01 20:20:52 +0100134 len -= slen;
135 strncpy(s + len, sd->s_name, slen);
136 if (len)
137 s[--len] = '/';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Kay Sievers2f90a852007-11-01 20:20:52 +0100139 sd = sd->s_parent;
140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142 return 0;
143}
144
145static int sysfs_getlink(struct dentry *dentry, char * path)
146{
Tejun Heo2b29ac22007-06-14 03:45:15 +0900147 struct sysfs_dirent *sd = dentry->d_fsdata;
148 struct sysfs_dirent *parent_sd = sd->s_parent;
Tejun Heob1fc3d62007-09-20 16:05:11 +0900149 struct sysfs_dirent *target_sd = sd->s_symlink.target_sd;
Tejun Heo2b29ac22007-06-14 03:45:15 +0900150 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Tejun Heo3007e992007-06-14 04:27:23 +0900152 mutex_lock(&sysfs_mutex);
Tejun Heo2b29ac22007-06-14 03:45:15 +0900153 error = sysfs_get_target_path(parent_sd, target_sd, path);
Tejun Heo3007e992007-06-14 04:27:23 +0900154 mutex_unlock(&sysfs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Tejun Heo2b29ac22007-06-14 03:45:15 +0900156 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157}
158
Linus Torvaldscc314ee2005-08-19 18:02:56 -0700159static void *sysfs_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
161 int error = -ENOMEM;
162 unsigned long page = get_zeroed_page(GFP_KERNEL);
163 if (page)
164 error = sysfs_getlink(dentry, (char *) page);
165 nd_set_link(nd, error ? ERR_PTR(error) : (char *)page);
Linus Torvaldscc314ee2005-08-19 18:02:56 -0700166 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
168
Linus Torvaldscc314ee2005-08-19 18:02:56 -0700169static void sysfs_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
171 char *page = nd_get_link(nd);
172 if (!IS_ERR(page))
173 free_page((unsigned long)page);
174}
175
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800176const struct inode_operations sysfs_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 .readlink = generic_readlink,
178 .follow_link = sysfs_follow_link,
179 .put_link = sysfs_put_link,
180};
181
182
183EXPORT_SYMBOL_GPL(sysfs_create_link);
184EXPORT_SYMBOL_GPL(sysfs_remove_link);