blob: 3eac20c63c419d45cc666a53251be208ebf1296a [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
Tejun Heo2b29ac22007-06-14 03:45:15 +090022static int object_depth(struct sysfs_dirent *sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -070023{
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 int depth = 0;
Tejun Heo2b29ac22007-06-14 03:45:15 +090025
26 for (; sd->s_parent; sd = sd->s_parent)
27 depth++;
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 return depth;
30}
31
Tejun Heo2b29ac22007-06-14 03:45:15 +090032static int object_path_length(struct sysfs_dirent * sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033{
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 int length = 1;
Tejun Heo2b29ac22007-06-14 03:45:15 +090035
36 for (; sd->s_parent; sd = sd->s_parent)
37 length += strlen(sd->s_name) + 1;
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 return length;
40}
41
Tejun Heo2b29ac22007-06-14 03:45:15 +090042static void fill_object_path(struct sysfs_dirent *sd, char *buffer, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 --length;
Tejun Heo2b29ac22007-06-14 03:45:15 +090045 for (; sd->s_parent; sd = sd->s_parent) {
46 int cur = strlen(sd->s_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48 /* back up enough to print this bus id with '/' */
49 length -= cur;
Tejun Heo2b29ac22007-06-14 03:45:15 +090050 strncpy(buffer + length, sd->s_name, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 *(buffer + --length) = '/';
52 }
53}
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055/**
56 * sysfs_create_link - create symlink between two objects.
57 * @kobj: object whose directory we're creating the link in.
58 * @target: object we're pointing to.
59 * @name: name of the symlink.
60 */
Dmitry Torokhove3a15db2005-04-26 02:31:08 -050061int sysfs_create_link(struct kobject * kobj, struct kobject * target, const char * name)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
Tejun Heo2b29ac22007-06-14 03:45:15 +090063 struct sysfs_dirent *parent_sd = NULL;
64 struct sysfs_dirent *target_sd = NULL;
Tejun Heo3007e992007-06-14 04:27:23 +090065 struct sysfs_dirent *sd = NULL;
Tejun Heofb6896d2007-06-14 04:27:24 +090066 struct sysfs_addrm_cxt acxt;
Tejun Heo3007e992007-06-14 04:27:23 +090067 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Greg Kroah-Hartmanceeee1f2002-04-09 12:14:34 -070069 BUG_ON(!name);
70
Eric W. Biederman7d0c7d62007-08-20 21:36:30 +090071 if (!kobj)
72 parent_sd = &sysfs_root;
73 else
Tejun Heo608e2662007-06-14 04:27:22 +090074 parent_sd = kobj->sd;
Greg Kroah-Hartmanceeee1f2002-04-09 12:14:34 -070075
Tejun Heo3007e992007-06-14 04:27:23 +090076 error = -EFAULT;
Tejun Heo608e2662007-06-14 04:27:22 +090077 if (!parent_sd)
Tejun Heo3007e992007-06-14 04:27:23 +090078 goto out_put;
Tejun Heo2b29ac22007-06-14 03:45:15 +090079
Tejun Heo608e2662007-06-14 04:27:22 +090080 /* target->sd can go away beneath us but is protected with
Tejun Heo5f995322007-06-14 04:27:23 +090081 * sysfs_assoc_lock. Fetch target_sd from it.
Tejun Heo2b29ac22007-06-14 03:45:15 +090082 */
Tejun Heo5f995322007-06-14 04:27:23 +090083 spin_lock(&sysfs_assoc_lock);
Tejun Heo608e2662007-06-14 04:27:22 +090084 if (target->sd)
85 target_sd = sysfs_get(target->sd);
Tejun Heo5f995322007-06-14 04:27:23 +090086 spin_unlock(&sysfs_assoc_lock);
Tejun Heo2b29ac22007-06-14 03:45:15 +090087
Tejun Heo3007e992007-06-14 04:27:23 +090088 error = -ENOENT;
Tejun Heo2b29ac22007-06-14 03:45:15 +090089 if (!target_sd)
Tejun Heo3007e992007-06-14 04:27:23 +090090 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Tejun Heo3007e992007-06-14 04:27:23 +090092 error = -ENOMEM;
93 sd = sysfs_new_dirent(name, S_IFLNK|S_IRWXUGO, SYSFS_KOBJ_LINK);
94 if (!sd)
95 goto out_put;
Tejun Heoa1da4df2007-07-18 16:14:45 +090096
Tejun Heob1fc3d62007-09-20 16:05:11 +090097 sd->s_symlink.target_sd = target_sd;
Tejun Heoa1da4df2007-07-18 16:14:45 +090098 target_sd = NULL; /* reference is now owned by the symlink */
Tejun Heo2b29ac22007-06-14 03:45:15 +090099
Tejun Heofb6896d2007-06-14 04:27:24 +0900100 sysfs_addrm_start(&acxt, parent_sd);
Tejun Heo23dc2792007-08-02 21:38:03 +0900101 error = sysfs_add_one(&acxt, sd);
102 sysfs_addrm_finish(&acxt);
Tejun Heofb6896d2007-06-14 04:27:24 +0900103
Tejun Heo23dc2792007-08-02 21:38:03 +0900104 if (error)
Tejun Heo967e35d2007-07-18 16:38:11 +0900105 goto out_put;
Tejun Heofb6896d2007-06-14 04:27:24 +0900106
Tejun Heo967e35d2007-07-18 16:38:11 +0900107 return 0;
108
Tejun Heo3007e992007-06-14 04:27:23 +0900109 out_put:
110 sysfs_put(target_sd);
111 sysfs_put(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 return error;
113}
114
115
116/**
117 * sysfs_remove_link - remove symlink in object's directory.
118 * @kobj: object we're acting for.
119 * @name: name of the symlink to remove.
120 */
121
Dmitry Torokhove3a15db2005-04-26 02:31:08 -0500122void sysfs_remove_link(struct kobject * kobj, const char * name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
Tejun Heo608e2662007-06-14 04:27:22 +0900124 sysfs_hash_and_remove(kobj->sd, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
126
Tejun Heo2b29ac22007-06-14 03:45:15 +0900127static int sysfs_get_target_path(struct sysfs_dirent * parent_sd,
128 struct sysfs_dirent * target_sd, char *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
130 char * s;
131 int depth, size;
132
Tejun Heo2b29ac22007-06-14 03:45:15 +0900133 depth = object_depth(parent_sd);
134 size = object_path_length(target_sd) + depth * 3 - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 if (size > PATH_MAX)
136 return -ENAMETOOLONG;
137
138 pr_debug("%s: depth = %d, size = %d\n", __FUNCTION__, depth, size);
139
140 for (s = path; depth--; s += 3)
141 strcpy(s,"../");
142
Tejun Heo2b29ac22007-06-14 03:45:15 +0900143 fill_object_path(target_sd, path, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 pr_debug("%s: path = '%s'\n", __FUNCTION__, path);
145
146 return 0;
147}
148
149static int sysfs_getlink(struct dentry *dentry, char * path)
150{
Tejun Heo2b29ac22007-06-14 03:45:15 +0900151 struct sysfs_dirent *sd = dentry->d_fsdata;
152 struct sysfs_dirent *parent_sd = sd->s_parent;
Tejun Heob1fc3d62007-09-20 16:05:11 +0900153 struct sysfs_dirent *target_sd = sd->s_symlink.target_sd;
Tejun Heo2b29ac22007-06-14 03:45:15 +0900154 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Tejun Heo3007e992007-06-14 04:27:23 +0900156 mutex_lock(&sysfs_mutex);
Tejun Heo2b29ac22007-06-14 03:45:15 +0900157 error = sysfs_get_target_path(parent_sd, target_sd, path);
Tejun Heo3007e992007-06-14 04:27:23 +0900158 mutex_unlock(&sysfs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Tejun Heo2b29ac22007-06-14 03:45:15 +0900160 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162
Linus Torvaldscc314ee2005-08-19 18:02:56 -0700163static void *sysfs_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
165 int error = -ENOMEM;
166 unsigned long page = get_zeroed_page(GFP_KERNEL);
167 if (page)
168 error = sysfs_getlink(dentry, (char *) page);
169 nd_set_link(nd, error ? ERR_PTR(error) : (char *)page);
Linus Torvaldscc314ee2005-08-19 18:02:56 -0700170 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
Linus Torvaldscc314ee2005-08-19 18:02:56 -0700173static void sysfs_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
175 char *page = nd_get_link(nd);
176 if (!IS_ERR(page))
177 free_page((unsigned long)page);
178}
179
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800180const struct inode_operations sysfs_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 .readlink = generic_readlink,
182 .follow_link = sysfs_follow_link,
183 .put_link = sysfs_put_link,
184};
185
186
187EXPORT_SYMBOL_GPL(sysfs_create_link);
188EXPORT_SYMBOL_GPL(sysfs_remove_link);