blob: 817f5966edcac2c2e36033902f2ac2ce0f89f626 [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{
Mark Fasheha839c5a2008-01-29 14:35:18 -080090 struct sysfs_dirent *parent_sd = NULL;
91
92 if (!kobj)
93 parent_sd = &sysfs_root;
94 else
95 parent_sd = kobj->sd;
96
97 sysfs_hash_and_remove(parent_sd, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098}
99
Kay Sievers2f90a852007-11-01 20:20:52 +0100100static int sysfs_get_target_path(struct sysfs_dirent *parent_sd,
101 struct sysfs_dirent *target_sd, char *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
Kay Sievers2f90a852007-11-01 20:20:52 +0100103 struct sysfs_dirent *base, *sd;
104 char *s = path;
105 int len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Kay Sievers2f90a852007-11-01 20:20:52 +0100107 /* go up to the root, stop at the base */
108 base = parent_sd;
109 while (base->s_parent) {
110 sd = target_sd->s_parent;
111 while (sd->s_parent && base != sd)
112 sd = sd->s_parent;
113
114 if (base == sd)
115 break;
116
117 strcpy(s, "../");
118 s += 3;
119 base = base->s_parent;
120 }
121
122 /* determine end of target string for reverse fillup */
123 sd = target_sd;
124 while (sd->s_parent && sd != base) {
125 len += strlen(sd->s_name) + 1;
126 sd = sd->s_parent;
127 }
128
129 /* check limits */
130 if (len < 2)
131 return -EINVAL;
132 len--;
133 if ((s - path) + len > PATH_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 return -ENAMETOOLONG;
135
Kay Sievers2f90a852007-11-01 20:20:52 +0100136 /* reverse fillup of target string from target to base */
137 sd = target_sd;
138 while (sd->s_parent && sd != base) {
139 int slen = strlen(sd->s_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Kay Sievers2f90a852007-11-01 20:20:52 +0100141 len -= slen;
142 strncpy(s + len, sd->s_name, slen);
143 if (len)
144 s[--len] = '/';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Kay Sievers2f90a852007-11-01 20:20:52 +0100146 sd = sd->s_parent;
147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149 return 0;
150}
151
152static int sysfs_getlink(struct dentry *dentry, char * path)
153{
Tejun Heo2b29ac22007-06-14 03:45:15 +0900154 struct sysfs_dirent *sd = dentry->d_fsdata;
155 struct sysfs_dirent *parent_sd = sd->s_parent;
Tejun Heob1fc3d62007-09-20 16:05:11 +0900156 struct sysfs_dirent *target_sd = sd->s_symlink.target_sd;
Tejun Heo2b29ac22007-06-14 03:45:15 +0900157 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Tejun Heo3007e992007-06-14 04:27:23 +0900159 mutex_lock(&sysfs_mutex);
Tejun Heo2b29ac22007-06-14 03:45:15 +0900160 error = sysfs_get_target_path(parent_sd, target_sd, path);
Tejun Heo3007e992007-06-14 04:27:23 +0900161 mutex_unlock(&sysfs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Tejun Heo2b29ac22007-06-14 03:45:15 +0900163 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}
165
Linus Torvaldscc314ee2005-08-19 18:02:56 -0700166static void *sysfs_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
168 int error = -ENOMEM;
169 unsigned long page = get_zeroed_page(GFP_KERNEL);
170 if (page)
171 error = sysfs_getlink(dentry, (char *) page);
172 nd_set_link(nd, error ? ERR_PTR(error) : (char *)page);
Linus Torvaldscc314ee2005-08-19 18:02:56 -0700173 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174}
175
Linus Torvaldscc314ee2005-08-19 18:02:56 -0700176static void sysfs_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
178 char *page = nd_get_link(nd);
179 if (!IS_ERR(page))
180 free_page((unsigned long)page);
181}
182
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800183const struct inode_operations sysfs_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 .readlink = generic_readlink,
185 .follow_link = sysfs_follow_link,
186 .put_link = sysfs_put_link,
187};
188
189
190EXPORT_SYMBOL_GPL(sysfs_create_link);
191EXPORT_SYMBOL_GPL(sysfs_remove_link);