blob: f50e3cc2ded8d76e00d203829108b40647f1fc63 [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>
10
11#include "sysfs.h"
12
13static int object_depth(struct kobject * kobj)
14{
15 struct kobject * p = kobj;
16 int depth = 0;
17 do { depth++; } while ((p = p->parent));
18 return depth;
19}
20
21static int object_path_length(struct kobject * kobj)
22{
23 struct kobject * p = kobj;
24 int length = 1;
25 do {
26 length += strlen(kobject_name(p)) + 1;
27 p = p->parent;
28 } while (p);
29 return length;
30}
31
32static void fill_object_path(struct kobject * kobj, char * buffer, int length)
33{
34 struct kobject * p;
35
36 --length;
37 for (p = kobj; p; p = p->parent) {
38 int cur = strlen(kobject_name(p));
39
40 /* back up enough to print this bus id with '/' */
41 length -= cur;
42 strncpy(buffer + length,kobject_name(p),cur);
43 *(buffer + --length) = '/';
44 }
45}
46
Dmitry Torokhove3a15db2005-04-26 02:31:08 -050047static int sysfs_add_link(struct dentry * parent, const char * name, struct kobject * target)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
49 struct sysfs_dirent * parent_sd = parent->d_fsdata;
50 struct sysfs_symlink * sl;
51 int error = 0;
52
53 error = -ENOMEM;
54 sl = kmalloc(sizeof(*sl), GFP_KERNEL);
55 if (!sl)
56 goto exit1;
57
58 sl->link_name = kmalloc(strlen(name) + 1, GFP_KERNEL);
59 if (!sl->link_name)
60 goto exit2;
61
62 strcpy(sl->link_name, name);
63 sl->target_kobj = kobject_get(target);
64
65 error = sysfs_make_dirent(parent_sd, NULL, sl, S_IFLNK|S_IRWXUGO,
66 SYSFS_KOBJ_LINK);
67 if (!error)
68 return 0;
69
Greg Kroah-Hartmanb3229082006-03-16 15:44:26 -080070 kobject_put(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 kfree(sl->link_name);
72exit2:
73 kfree(sl);
74exit1:
75 return error;
76}
77
78/**
79 * sysfs_create_link - create symlink between two objects.
80 * @kobj: object whose directory we're creating the link in.
81 * @target: object we're pointing to.
82 * @name: name of the symlink.
83 */
Dmitry Torokhove3a15db2005-04-26 02:31:08 -050084int sysfs_create_link(struct kobject * kobj, struct kobject * target, const char * name)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
Greg Kroah-Hartmanceeee1f2002-04-09 12:14:34 -070086 struct dentry *dentry = NULL;
Maneesh Sonic5168652006-03-09 19:40:14 +053087 int error = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Greg Kroah-Hartmanceeee1f2002-04-09 12:14:34 -070089 BUG_ON(!name);
90
91 if (!kobj) {
92 if (sysfs_mount && sysfs_mount->mnt_sb)
93 dentry = sysfs_mount->mnt_sb->s_root;
94 } else
95 dentry = kobj->dentry;
96
97 if (!dentry)
98 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800100 mutex_lock(&dentry->d_inode->i_mutex);
Maneesh Sonic5168652006-03-09 19:40:14 +0530101 if (!sysfs_dirent_exist(dentry->d_fsdata, name))
102 error = sysfs_add_link(dentry, name, target);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800103 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 return error;
105}
106
107
108/**
109 * sysfs_remove_link - remove symlink in object's directory.
110 * @kobj: object we're acting for.
111 * @name: name of the symlink to remove.
112 */
113
Dmitry Torokhove3a15db2005-04-26 02:31:08 -0500114void sysfs_remove_link(struct kobject * kobj, const char * name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
116 sysfs_hash_and_remove(kobj->dentry,name);
117}
118
119static int sysfs_get_target_path(struct kobject * kobj, struct kobject * target,
Dmitry Torokhove3a15db2005-04-26 02:31:08 -0500120 char *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
122 char * s;
123 int depth, size;
124
125 depth = object_depth(kobj);
126 size = object_path_length(target) + depth * 3 - 1;
127 if (size > PATH_MAX)
128 return -ENAMETOOLONG;
129
130 pr_debug("%s: depth = %d, size = %d\n", __FUNCTION__, depth, size);
131
132 for (s = path; depth--; s += 3)
133 strcpy(s,"../");
134
135 fill_object_path(target, path, size);
136 pr_debug("%s: path = '%s'\n", __FUNCTION__, path);
137
138 return 0;
139}
140
141static int sysfs_getlink(struct dentry *dentry, char * path)
142{
143 struct kobject *kobj, *target_kobj;
144 int error = 0;
145
146 kobj = sysfs_get_kobject(dentry->d_parent);
147 if (!kobj)
148 return -EINVAL;
149
150 target_kobj = sysfs_get_kobject(dentry);
151 if (!target_kobj) {
152 kobject_put(kobj);
153 return -EINVAL;
154 }
155
156 down_read(&sysfs_rename_sem);
157 error = sysfs_get_target_path(kobj, target_kobj, path);
158 up_read(&sysfs_rename_sem);
159
160 kobject_put(kobj);
161 kobject_put(target_kobj);
162 return error;
163
164}
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
183struct inode_operations sysfs_symlink_inode_operations = {
184 .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);