]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/sysfs/symlink.c
driver core: Make the dev_*() family of macros in device.h complete
[net-next-2.6.git] / fs / sysfs / symlink.c
CommitLineData
1da177e4 1/*
6d66f5cd
TH
2 * 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.
1da177e4
LT
11 */
12
13#include <linux/fs.h>
ceeee1fb 14#include <linux/mount.h>
1da177e4
LT
15#include <linux/module.h>
16#include <linux/kobject.h>
17#include <linux/namei.h>
869512ab 18#include <linux/mutex.h>
1da177e4
LT
19
20#include "sysfs.h"
21
2b29ac25 22static int object_depth(struct sysfs_dirent *sd)
1da177e4 23{
1da177e4 24 int depth = 0;
2b29ac25
TH
25
26 for (; sd->s_parent; sd = sd->s_parent)
27 depth++;
28
1da177e4
LT
29 return depth;
30}
31
2b29ac25 32static int object_path_length(struct sysfs_dirent * sd)
1da177e4 33{
1da177e4 34 int length = 1;
2b29ac25
TH
35
36 for (; sd->s_parent; sd = sd->s_parent)
37 length += strlen(sd->s_name) + 1;
38
1da177e4
LT
39 return length;
40}
41
2b29ac25 42static void fill_object_path(struct sysfs_dirent *sd, char *buffer, int length)
1da177e4 43{
1da177e4 44 --length;
2b29ac25
TH
45 for (; sd->s_parent; sd = sd->s_parent) {
46 int cur = strlen(sd->s_name);
1da177e4
LT
47
48 /* back up enough to print this bus id with '/' */
49 length -= cur;
2b29ac25 50 strncpy(buffer + length, sd->s_name, cur);
1da177e4
LT
51 *(buffer + --length) = '/';
52 }
53}
54
1da177e4
LT
55/**
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 */
e3a15db2 61int sysfs_create_link(struct kobject * kobj, struct kobject * target, const char * name)
1da177e4 62{
2b29ac25
TH
63 struct sysfs_dirent *parent_sd = NULL;
64 struct sysfs_dirent *target_sd = NULL;
3007e997 65 struct sysfs_dirent *sd = NULL;
fb6896da 66 struct sysfs_addrm_cxt acxt;
3007e997 67 int error;
1da177e4 68
ceeee1fb
GKH
69 BUG_ON(!name);
70
7d0c7d67
EB
71 if (!kobj)
72 parent_sd = &sysfs_root;
73 else
608e266a 74 parent_sd = kobj->sd;
ceeee1fb 75
3007e997 76 error = -EFAULT;
608e266a 77 if (!parent_sd)
3007e997 78 goto out_put;
2b29ac25 79
608e266a 80 /* target->sd can go away beneath us but is protected with
5f995323 81 * sysfs_assoc_lock. Fetch target_sd from it.
2b29ac25 82 */
5f995323 83 spin_lock(&sysfs_assoc_lock);
608e266a
TH
84 if (target->sd)
85 target_sd = sysfs_get(target->sd);
5f995323 86 spin_unlock(&sysfs_assoc_lock);
2b29ac25 87
3007e997 88 error = -ENOENT;
2b29ac25 89 if (!target_sd)
3007e997
TH
90 goto out_put;
91
92 error = -ENOMEM;
93 sd = sysfs_new_dirent(name, S_IFLNK|S_IRWXUGO, SYSFS_KOBJ_LINK);
94 if (!sd)
95 goto out_put;
a1da4dfe 96
b1fc3d61 97 sd->s_symlink.target_sd = target_sd;
a1da4dfe 98 target_sd = NULL; /* reference is now owned by the symlink */
1da177e4 99
fb6896da 100 sysfs_addrm_start(&acxt, parent_sd);
23dc2799
TH
101 error = sysfs_add_one(&acxt, sd);
102 sysfs_addrm_finish(&acxt);
2b29ac25 103
23dc2799 104 if (error)
967e35dc 105 goto out_put;
967e35dc
TH
106
107 return 0;
fb6896da 108
3007e997
TH
109 out_put:
110 sysfs_put(target_sd);
111 sysfs_put(sd);
1da177e4
LT
112 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
e3a15db2 122void sysfs_remove_link(struct kobject * kobj, const char * name)
1da177e4 123{
608e266a 124 sysfs_hash_and_remove(kobj->sd, name);
1da177e4
LT
125}
126
2b29ac25
TH
127static int sysfs_get_target_path(struct sysfs_dirent * parent_sd,
128 struct sysfs_dirent * target_sd, char *path)
1da177e4
LT
129{
130 char * s;
131 int depth, size;
132
2b29ac25
TH
133 depth = object_depth(parent_sd);
134 size = object_path_length(target_sd) + depth * 3 - 1;
1da177e4
LT
135 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
2b29ac25 143 fill_object_path(target_sd, path, size);
1da177e4
LT
144 pr_debug("%s: path = '%s'\n", __FUNCTION__, path);
145
146 return 0;
147}
148
149static int sysfs_getlink(struct dentry *dentry, char * path)
150{
2b29ac25
TH
151 struct sysfs_dirent *sd = dentry->d_fsdata;
152 struct sysfs_dirent *parent_sd = sd->s_parent;
b1fc3d61 153 struct sysfs_dirent *target_sd = sd->s_symlink.target_sd;
2b29ac25 154 int error;
1da177e4 155
3007e997 156 mutex_lock(&sysfs_mutex);
2b29ac25 157 error = sysfs_get_target_path(parent_sd, target_sd, path);
3007e997 158 mutex_unlock(&sysfs_mutex);
1da177e4 159
2b29ac25 160 return error;
1da177e4
LT
161}
162
cc314eef 163static void *sysfs_follow_link(struct dentry *dentry, struct nameidata *nd)
1da177e4
LT
164{
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);
cc314eef 170 return NULL;
1da177e4
LT
171}
172
cc314eef 173static void sysfs_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
1da177e4
LT
174{
175 char *page = nd_get_link(nd);
176 if (!IS_ERR(page))
177 free_page((unsigned long)page);
178}
179
c5ef1c42 180const struct inode_operations sysfs_symlink_inode_operations = {
1da177e4
LT
181 .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);