Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * symlink.c - operations for sysfs symlinks. | |
3 | */ | |
4 | ||
5 | #include <linux/fs.h> | |
ceeee1fb | 6 | #include <linux/mount.h> |
1da177e4 LT |
7 | #include <linux/module.h> |
8 | #include <linux/kobject.h> | |
9 | #include <linux/namei.h> | |
94bebf4d | 10 | #include <asm/semaphore.h> |
1da177e4 LT |
11 | |
12 | #include "sysfs.h" | |
13 | ||
2b29ac25 | 14 | static int object_depth(struct sysfs_dirent *sd) |
1da177e4 | 15 | { |
1da177e4 | 16 | int depth = 0; |
2b29ac25 TH |
17 | |
18 | for (; sd->s_parent; sd = sd->s_parent) | |
19 | depth++; | |
20 | ||
1da177e4 LT |
21 | return depth; |
22 | } | |
23 | ||
2b29ac25 | 24 | static int object_path_length(struct sysfs_dirent * sd) |
1da177e4 | 25 | { |
1da177e4 | 26 | int length = 1; |
2b29ac25 TH |
27 | |
28 | for (; sd->s_parent; sd = sd->s_parent) | |
29 | length += strlen(sd->s_name) + 1; | |
30 | ||
1da177e4 LT |
31 | return length; |
32 | } | |
33 | ||
2b29ac25 | 34 | static void fill_object_path(struct sysfs_dirent *sd, char *buffer, int length) |
1da177e4 | 35 | { |
1da177e4 | 36 | --length; |
2b29ac25 TH |
37 | for (; sd->s_parent; sd = sd->s_parent) { |
38 | int cur = strlen(sd->s_name); | |
1da177e4 LT |
39 | |
40 | /* back up enough to print this bus id with '/' */ | |
41 | length -= cur; | |
2b29ac25 | 42 | strncpy(buffer + length, sd->s_name, cur); |
1da177e4 LT |
43 | *(buffer + --length) = '/'; |
44 | } | |
45 | } | |
46 | ||
2b29ac25 TH |
47 | static int sysfs_add_link(struct sysfs_dirent * parent_sd, const char * name, |
48 | struct sysfs_dirent * target_sd) | |
1da177e4 | 49 | { |
a26cd722 | 50 | struct sysfs_dirent * sd; |
1da177e4 | 51 | |
3e519038 | 52 | sd = sysfs_new_dirent(name, S_IFLNK|S_IRWXUGO, SYSFS_KOBJ_LINK); |
a26cd722 | 53 | if (!sd) |
3e519038 | 54 | return -ENOMEM; |
dfeb9fb0 | 55 | |
2b29ac25 | 56 | sd->s_elem.symlink.target_sd = target_sd; |
3e519038 | 57 | sysfs_attach_dirent(sd, parent_sd, NULL); |
dfeb9fb0 | 58 | return 0; |
1da177e4 LT |
59 | } |
60 | ||
61 | /** | |
62 | * sysfs_create_link - create symlink between two objects. | |
63 | * @kobj: object whose directory we're creating the link in. | |
64 | * @target: object we're pointing to. | |
65 | * @name: name of the symlink. | |
66 | */ | |
e3a15db2 | 67 | int sysfs_create_link(struct kobject * kobj, struct kobject * target, const char * name) |
1da177e4 | 68 | { |
2b29ac25 TH |
69 | struct sysfs_dirent *parent_sd = NULL; |
70 | struct sysfs_dirent *target_sd = NULL; | |
c516865c | 71 | int error = -EEXIST; |
1da177e4 | 72 | |
ceeee1fb GKH |
73 | BUG_ON(!name); |
74 | ||
75 | if (!kobj) { | |
76 | if (sysfs_mount && sysfs_mount->mnt_sb) | |
608e266a | 77 | parent_sd = sysfs_mount->mnt_sb->s_root->d_fsdata; |
ceeee1fb | 78 | } else |
608e266a | 79 | parent_sd = kobj->sd; |
ceeee1fb | 80 | |
608e266a | 81 | if (!parent_sd) |
ceeee1fb | 82 | return -EFAULT; |
2b29ac25 | 83 | |
608e266a | 84 | /* target->sd can go away beneath us but is protected with |
5f995323 | 85 | * sysfs_assoc_lock. Fetch target_sd from it. |
2b29ac25 | 86 | */ |
5f995323 | 87 | spin_lock(&sysfs_assoc_lock); |
608e266a TH |
88 | if (target->sd) |
89 | target_sd = sysfs_get(target->sd); | |
5f995323 | 90 | spin_unlock(&sysfs_assoc_lock); |
2b29ac25 TH |
91 | |
92 | if (!target_sd) | |
93 | return -ENOENT; | |
1da177e4 | 94 | |
608e266a TH |
95 | mutex_lock(&parent_sd->s_dentry->d_inode->i_mutex); |
96 | if (!sysfs_find_dirent(parent_sd, name)) | |
2b29ac25 | 97 | error = sysfs_add_link(parent_sd, name, target_sd); |
608e266a | 98 | mutex_unlock(&parent_sd->s_dentry->d_inode->i_mutex); |
2b29ac25 TH |
99 | |
100 | if (error) | |
101 | sysfs_put(target_sd); | |
102 | ||
1da177e4 LT |
103 | return error; |
104 | } | |
105 | ||
106 | ||
107 | /** | |
108 | * sysfs_remove_link - remove symlink in object's directory. | |
109 | * @kobj: object we're acting for. | |
110 | * @name: name of the symlink to remove. | |
111 | */ | |
112 | ||
e3a15db2 | 113 | void sysfs_remove_link(struct kobject * kobj, const char * name) |
1da177e4 | 114 | { |
608e266a | 115 | sysfs_hash_and_remove(kobj->sd, name); |
1da177e4 LT |
116 | } |
117 | ||
2b29ac25 TH |
118 | static int sysfs_get_target_path(struct sysfs_dirent * parent_sd, |
119 | struct sysfs_dirent * target_sd, char *path) | |
1da177e4 LT |
120 | { |
121 | char * s; | |
122 | int depth, size; | |
123 | ||
2b29ac25 TH |
124 | depth = object_depth(parent_sd); |
125 | size = object_path_length(target_sd) + depth * 3 - 1; | |
1da177e4 LT |
126 | if (size > PATH_MAX) |
127 | return -ENAMETOOLONG; | |
128 | ||
129 | pr_debug("%s: depth = %d, size = %d\n", __FUNCTION__, depth, size); | |
130 | ||
131 | for (s = path; depth--; s += 3) | |
132 | strcpy(s,"../"); | |
133 | ||
2b29ac25 | 134 | fill_object_path(target_sd, path, size); |
1da177e4 LT |
135 | pr_debug("%s: path = '%s'\n", __FUNCTION__, path); |
136 | ||
137 | return 0; | |
138 | } | |
139 | ||
140 | static int sysfs_getlink(struct dentry *dentry, char * path) | |
141 | { | |
2b29ac25 TH |
142 | struct sysfs_dirent *sd = dentry->d_fsdata; |
143 | struct sysfs_dirent *parent_sd = sd->s_parent; | |
144 | struct sysfs_dirent *target_sd = sd->s_elem.symlink.target_sd; | |
145 | int error; | |
1da177e4 LT |
146 | |
147 | down_read(&sysfs_rename_sem); | |
2b29ac25 | 148 | error = sysfs_get_target_path(parent_sd, target_sd, path); |
1da177e4 | 149 | up_read(&sysfs_rename_sem); |
1da177e4 | 150 | |
2b29ac25 | 151 | return error; |
1da177e4 LT |
152 | } |
153 | ||
cc314eef | 154 | static void *sysfs_follow_link(struct dentry *dentry, struct nameidata *nd) |
1da177e4 LT |
155 | { |
156 | int error = -ENOMEM; | |
157 | unsigned long page = get_zeroed_page(GFP_KERNEL); | |
158 | if (page) | |
159 | error = sysfs_getlink(dentry, (char *) page); | |
160 | nd_set_link(nd, error ? ERR_PTR(error) : (char *)page); | |
cc314eef | 161 | return NULL; |
1da177e4 LT |
162 | } |
163 | ||
cc314eef | 164 | static void sysfs_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie) |
1da177e4 LT |
165 | { |
166 | char *page = nd_get_link(nd); | |
167 | if (!IS_ERR(page)) | |
168 | free_page((unsigned long)page); | |
169 | } | |
170 | ||
c5ef1c42 | 171 | const struct inode_operations sysfs_symlink_inode_operations = { |
1da177e4 LT |
172 | .readlink = generic_readlink, |
173 | .follow_link = sysfs_follow_link, | |
174 | .put_link = sysfs_put_link, | |
175 | }; | |
176 | ||
177 | ||
178 | EXPORT_SYMBOL_GPL(sysfs_create_link); | |
179 | EXPORT_SYMBOL_GPL(sysfs_remove_link); |