blob: 27df635b786ab08077f8c0bfd34bfde2094cbac1 [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>
Oliver Neukum94bebf42006-12-20 10:52:44 +010010#include <asm/semaphore.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
12#include "sysfs.h"
13
14static int object_depth(struct kobject * kobj)
15{
16 struct kobject * p = kobj;
17 int depth = 0;
18 do { depth++; } while ((p = p->parent));
19 return depth;
20}
21
22static int object_path_length(struct kobject * kobj)
23{
24 struct kobject * p = kobj;
25 int length = 1;
26 do {
27 length += strlen(kobject_name(p)) + 1;
28 p = p->parent;
29 } while (p);
30 return length;
31}
32
33static void fill_object_path(struct kobject * kobj, char * buffer, int length)
34{
35 struct kobject * p;
36
37 --length;
38 for (p = kobj; p; p = p->parent) {
39 int cur = strlen(kobject_name(p));
40
41 /* back up enough to print this bus id with '/' */
42 length -= cur;
43 strncpy(buffer + length,kobject_name(p),cur);
44 *(buffer + --length) = '/';
45 }
46}
47
Dmitry Torokhove3a15db2005-04-26 02:31:08 -050048static int sysfs_add_link(struct dentry * parent, const char * name, struct kobject * target)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
50 struct sysfs_dirent * parent_sd = parent->d_fsdata;
Tejun Heoa26cd722007-06-14 03:45:14 +090051 struct sysfs_dirent * sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Tejun Heo3e519032007-06-14 03:45:15 +090053 sd = sysfs_new_dirent(name, S_IFLNK|S_IRWXUGO, SYSFS_KOBJ_LINK);
Tejun Heoa26cd722007-06-14 03:45:14 +090054 if (!sd)
Tejun Heo3e519032007-06-14 03:45:15 +090055 return -ENOMEM;
56
57 sd->s_elem.symlink.target_kobj = kobject_get(target);
Tejun Heoa26cd722007-06-14 03:45:14 +090058 sysfs_attach_dirent(sd, parent_sd, NULL);
Tejun Heodfeb9fb2007-06-14 03:45:14 +090059 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060}
61
62/**
63 * sysfs_create_link - create symlink between two objects.
64 * @kobj: object whose directory we're creating the link in.
65 * @target: object we're pointing to.
66 * @name: name of the symlink.
67 */
Dmitry Torokhove3a15db2005-04-26 02:31:08 -050068int sysfs_create_link(struct kobject * kobj, struct kobject * target, const char * name)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
Greg Kroah-Hartmanceeee1f2002-04-09 12:14:34 -070070 struct dentry *dentry = NULL;
Maneesh Sonic5168652006-03-09 19:40:14 +053071 int error = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Greg Kroah-Hartmanceeee1f2002-04-09 12:14:34 -070073 BUG_ON(!name);
74
75 if (!kobj) {
76 if (sysfs_mount && sysfs_mount->mnt_sb)
77 dentry = sysfs_mount->mnt_sb->s_root;
78 } else
79 dentry = kobj->dentry;
80
81 if (!dentry)
82 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Jes Sorensen1b1dcc12006-01-09 15:59:24 -080084 mutex_lock(&dentry->d_inode->i_mutex);
Maneesh Sonic5168652006-03-09 19:40:14 +053085 if (!sysfs_dirent_exist(dentry->d_fsdata, name))
86 error = sysfs_add_link(dentry, name, target);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -080087 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 return error;
89}
90
91
92/**
93 * sysfs_remove_link - remove symlink in object's directory.
94 * @kobj: object we're acting for.
95 * @name: name of the symlink to remove.
96 */
97
Dmitry Torokhove3a15db2005-04-26 02:31:08 -050098void sysfs_remove_link(struct kobject * kobj, const char * name)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
100 sysfs_hash_and_remove(kobj->dentry,name);
101}
102
103static int sysfs_get_target_path(struct kobject * kobj, struct kobject * target,
Dmitry Torokhove3a15db2005-04-26 02:31:08 -0500104 char *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
106 char * s;
107 int depth, size;
108
109 depth = object_depth(kobj);
110 size = object_path_length(target) + depth * 3 - 1;
111 if (size > PATH_MAX)
112 return -ENAMETOOLONG;
113
114 pr_debug("%s: depth = %d, size = %d\n", __FUNCTION__, depth, size);
115
116 for (s = path; depth--; s += 3)
117 strcpy(s,"../");
118
119 fill_object_path(target, path, size);
120 pr_debug("%s: path = '%s'\n", __FUNCTION__, path);
121
122 return 0;
123}
124
125static int sysfs_getlink(struct dentry *dentry, char * path)
126{
127 struct kobject *kobj, *target_kobj;
128 int error = 0;
129
130 kobj = sysfs_get_kobject(dentry->d_parent);
131 if (!kobj)
132 return -EINVAL;
133
134 target_kobj = sysfs_get_kobject(dentry);
135 if (!target_kobj) {
136 kobject_put(kobj);
137 return -EINVAL;
138 }
139
140 down_read(&sysfs_rename_sem);
141 error = sysfs_get_target_path(kobj, target_kobj, path);
142 up_read(&sysfs_rename_sem);
143
144 kobject_put(kobj);
145 kobject_put(target_kobj);
146 return error;
147
148}
149
Linus Torvaldscc314ee2005-08-19 18:02:56 -0700150static void *sysfs_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
152 int error = -ENOMEM;
153 unsigned long page = get_zeroed_page(GFP_KERNEL);
154 if (page)
155 error = sysfs_getlink(dentry, (char *) page);
156 nd_set_link(nd, error ? ERR_PTR(error) : (char *)page);
Linus Torvaldscc314ee2005-08-19 18:02:56 -0700157 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158}
159
Linus Torvaldscc314ee2005-08-19 18:02:56 -0700160static void sysfs_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
162 char *page = nd_get_link(nd);
163 if (!IS_ERR(page))
164 free_page((unsigned long)page);
165}
166
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800167const struct inode_operations sysfs_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 .readlink = generic_readlink,
169 .follow_link = sysfs_follow_link,
170 .put_link = sysfs_put_link,
171};
172
173
174EXPORT_SYMBOL_GPL(sysfs_create_link);
175EXPORT_SYMBOL_GPL(sysfs_remove_link);