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> | |
869512ab | 10 | #include <linux/mutex.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 | ||
1da177e4 LT |
47 | /** |
48 | * sysfs_create_link - create symlink between two objects. | |
49 | * @kobj: object whose directory we're creating the link in. | |
50 | * @target: object we're pointing to. | |
51 | * @name: name of the symlink. | |
52 | */ | |
e3a15db2 | 53 | int sysfs_create_link(struct kobject * kobj, struct kobject * target, const char * name) |
1da177e4 | 54 | { |
2b29ac25 TH |
55 | struct sysfs_dirent *parent_sd = NULL; |
56 | struct sysfs_dirent *target_sd = NULL; | |
3007e997 | 57 | struct sysfs_dirent *sd = NULL; |
fb6896da | 58 | struct sysfs_addrm_cxt acxt; |
3007e997 | 59 | int error; |
1da177e4 | 60 | |
ceeee1fb GKH |
61 | BUG_ON(!name); |
62 | ||
63 | if (!kobj) { | |
64 | if (sysfs_mount && sysfs_mount->mnt_sb) | |
608e266a | 65 | parent_sd = sysfs_mount->mnt_sb->s_root->d_fsdata; |
ceeee1fb | 66 | } else |
608e266a | 67 | parent_sd = kobj->sd; |
ceeee1fb | 68 | |
3007e997 | 69 | error = -EFAULT; |
608e266a | 70 | if (!parent_sd) |
3007e997 | 71 | goto out_put; |
2b29ac25 | 72 | |
608e266a | 73 | /* target->sd can go away beneath us but is protected with |
5f995323 | 74 | * sysfs_assoc_lock. Fetch target_sd from it. |
2b29ac25 | 75 | */ |
5f995323 | 76 | spin_lock(&sysfs_assoc_lock); |
608e266a TH |
77 | if (target->sd) |
78 | target_sd = sysfs_get(target->sd); | |
5f995323 | 79 | spin_unlock(&sysfs_assoc_lock); |
2b29ac25 | 80 | |
3007e997 | 81 | error = -ENOENT; |
2b29ac25 | 82 | if (!target_sd) |
3007e997 TH |
83 | goto out_put; |
84 | ||
85 | error = -ENOMEM; | |
86 | sd = sysfs_new_dirent(name, S_IFLNK|S_IRWXUGO, SYSFS_KOBJ_LINK); | |
87 | if (!sd) | |
88 | goto out_put; | |
a1da4dfe | 89 | |
3007e997 | 90 | sd->s_elem.symlink.target_sd = target_sd; |
a1da4dfe | 91 | target_sd = NULL; /* reference is now owned by the symlink */ |
1da177e4 | 92 | |
fb6896da | 93 | sysfs_addrm_start(&acxt, parent_sd); |
2b29ac25 | 94 | |
41fc1c27 | 95 | if (!sysfs_find_dirent(parent_sd, name)) |
fb6896da | 96 | sysfs_add_one(&acxt, sd); |
2b29ac25 | 97 | |
967e35dc TH |
98 | if (!sysfs_addrm_finish(&acxt)) { |
99 | error = -EEXIST; | |
100 | goto out_put; | |
101 | } | |
102 | ||
103 | return 0; | |
fb6896da | 104 | |
3007e997 TH |
105 | out_put: |
106 | sysfs_put(target_sd); | |
107 | sysfs_put(sd); | |
1da177e4 LT |
108 | return error; |
109 | } | |
110 | ||
111 | ||
112 | /** | |
113 | * sysfs_remove_link - remove symlink in object's directory. | |
114 | * @kobj: object we're acting for. | |
115 | * @name: name of the symlink to remove. | |
116 | */ | |
117 | ||
e3a15db2 | 118 | void sysfs_remove_link(struct kobject * kobj, const char * name) |
1da177e4 | 119 | { |
608e266a | 120 | sysfs_hash_and_remove(kobj->sd, name); |
1da177e4 LT |
121 | } |
122 | ||
2b29ac25 TH |
123 | static int sysfs_get_target_path(struct sysfs_dirent * parent_sd, |
124 | struct sysfs_dirent * target_sd, char *path) | |
1da177e4 LT |
125 | { |
126 | char * s; | |
127 | int depth, size; | |
128 | ||
2b29ac25 TH |
129 | depth = object_depth(parent_sd); |
130 | size = object_path_length(target_sd) + depth * 3 - 1; | |
1da177e4 LT |
131 | if (size > PATH_MAX) |
132 | return -ENAMETOOLONG; | |
133 | ||
134 | pr_debug("%s: depth = %d, size = %d\n", __FUNCTION__, depth, size); | |
135 | ||
136 | for (s = path; depth--; s += 3) | |
137 | strcpy(s,"../"); | |
138 | ||
2b29ac25 | 139 | fill_object_path(target_sd, path, size); |
1da177e4 LT |
140 | pr_debug("%s: path = '%s'\n", __FUNCTION__, path); |
141 | ||
142 | return 0; | |
143 | } | |
144 | ||
145 | static int sysfs_getlink(struct dentry *dentry, char * path) | |
146 | { | |
2b29ac25 TH |
147 | struct sysfs_dirent *sd = dentry->d_fsdata; |
148 | struct sysfs_dirent *parent_sd = sd->s_parent; | |
149 | struct sysfs_dirent *target_sd = sd->s_elem.symlink.target_sd; | |
150 | int error; | |
1da177e4 | 151 | |
3007e997 | 152 | mutex_lock(&sysfs_mutex); |
2b29ac25 | 153 | error = sysfs_get_target_path(parent_sd, target_sd, path); |
3007e997 | 154 | mutex_unlock(&sysfs_mutex); |
1da177e4 | 155 | |
2b29ac25 | 156 | return error; |
1da177e4 LT |
157 | } |
158 | ||
cc314eef | 159 | static void *sysfs_follow_link(struct dentry *dentry, struct nameidata *nd) |
1da177e4 LT |
160 | { |
161 | int error = -ENOMEM; | |
162 | unsigned long page = get_zeroed_page(GFP_KERNEL); | |
163 | if (page) | |
164 | error = sysfs_getlink(dentry, (char *) page); | |
165 | nd_set_link(nd, error ? ERR_PTR(error) : (char *)page); | |
cc314eef | 166 | return NULL; |
1da177e4 LT |
167 | } |
168 | ||
cc314eef | 169 | static void sysfs_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie) |
1da177e4 LT |
170 | { |
171 | char *page = nd_get_link(nd); | |
172 | if (!IS_ERR(page)) | |
173 | free_page((unsigned long)page); | |
174 | } | |
175 | ||
c5ef1c42 | 176 | const struct inode_operations sysfs_symlink_inode_operations = { |
1da177e4 LT |
177 | .readlink = generic_readlink, |
178 | .follow_link = sysfs_follow_link, | |
179 | .put_link = sysfs_put_link, | |
180 | }; | |
181 | ||
182 | ||
183 | EXPORT_SYMBOL_GPL(sysfs_create_link); | |
184 | EXPORT_SYMBOL_GPL(sysfs_remove_link); |