Commit | Line | Data |
---|---|---|
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 | ||
1da177e4 LT |
22 | /** |
23 | * sysfs_create_link - create symlink between two objects. | |
24 | * @kobj: object whose directory we're creating the link in. | |
25 | * @target: object we're pointing to. | |
26 | * @name: name of the symlink. | |
27 | */ | |
e3a15db2 | 28 | int sysfs_create_link(struct kobject * kobj, struct kobject * target, const char * name) |
1da177e4 | 29 | { |
2b29ac25 TH |
30 | struct sysfs_dirent *parent_sd = NULL; |
31 | struct sysfs_dirent *target_sd = NULL; | |
3007e997 | 32 | struct sysfs_dirent *sd = NULL; |
fb6896da | 33 | struct sysfs_addrm_cxt acxt; |
3007e997 | 34 | int error; |
1da177e4 | 35 | |
ceeee1fb GKH |
36 | BUG_ON(!name); |
37 | ||
7d0c7d67 EB |
38 | if (!kobj) |
39 | parent_sd = &sysfs_root; | |
40 | else | |
608e266a | 41 | parent_sd = kobj->sd; |
ceeee1fb | 42 | |
3007e997 | 43 | error = -EFAULT; |
608e266a | 44 | if (!parent_sd) |
3007e997 | 45 | goto out_put; |
2b29ac25 | 46 | |
608e266a | 47 | /* target->sd can go away beneath us but is protected with |
5f995323 | 48 | * sysfs_assoc_lock. Fetch target_sd from it. |
2b29ac25 | 49 | */ |
5f995323 | 50 | spin_lock(&sysfs_assoc_lock); |
608e266a TH |
51 | if (target->sd) |
52 | target_sd = sysfs_get(target->sd); | |
5f995323 | 53 | spin_unlock(&sysfs_assoc_lock); |
2b29ac25 | 54 | |
3007e997 | 55 | error = -ENOENT; |
2b29ac25 | 56 | if (!target_sd) |
3007e997 TH |
57 | goto out_put; |
58 | ||
59 | error = -ENOMEM; | |
60 | sd = sysfs_new_dirent(name, S_IFLNK|S_IRWXUGO, SYSFS_KOBJ_LINK); | |
61 | if (!sd) | |
62 | goto out_put; | |
a1da4dfe | 63 | |
b1fc3d61 | 64 | sd->s_symlink.target_sd = target_sd; |
a1da4dfe | 65 | target_sd = NULL; /* reference is now owned by the symlink */ |
1da177e4 | 66 | |
fb6896da | 67 | sysfs_addrm_start(&acxt, parent_sd); |
23dc2799 TH |
68 | error = sysfs_add_one(&acxt, sd); |
69 | sysfs_addrm_finish(&acxt); | |
2b29ac25 | 70 | |
23dc2799 | 71 | if (error) |
967e35dc | 72 | goto out_put; |
967e35dc TH |
73 | |
74 | return 0; | |
fb6896da | 75 | |
3007e997 TH |
76 | out_put: |
77 | sysfs_put(target_sd); | |
78 | sysfs_put(sd); | |
1da177e4 LT |
79 | return error; |
80 | } | |
81 | ||
1da177e4 LT |
82 | /** |
83 | * sysfs_remove_link - remove symlink in object's directory. | |
84 | * @kobj: object we're acting for. | |
85 | * @name: name of the symlink to remove. | |
86 | */ | |
87 | ||
e3a15db2 | 88 | void sysfs_remove_link(struct kobject * kobj, const char * name) |
1da177e4 | 89 | { |
a839c5af MF |
90 | struct sysfs_dirent *parent_sd = NULL; |
91 | ||
92 | if (!kobj) | |
93 | parent_sd = &sysfs_root; | |
94 | else | |
95 | parent_sd = kobj->sd; | |
96 | ||
97 | sysfs_hash_and_remove(parent_sd, name); | |
1da177e4 LT |
98 | } |
99 | ||
2f90a851 KS |
100 | static int sysfs_get_target_path(struct sysfs_dirent *parent_sd, |
101 | struct sysfs_dirent *target_sd, char *path) | |
1da177e4 | 102 | { |
2f90a851 KS |
103 | struct sysfs_dirent *base, *sd; |
104 | char *s = path; | |
105 | int len = 0; | |
106 | ||
107 | /* go up to the root, stop at the base */ | |
108 | base = parent_sd; | |
109 | while (base->s_parent) { | |
110 | sd = target_sd->s_parent; | |
111 | while (sd->s_parent && base != sd) | |
112 | sd = sd->s_parent; | |
113 | ||
114 | if (base == sd) | |
115 | break; | |
116 | ||
117 | strcpy(s, "../"); | |
118 | s += 3; | |
119 | base = base->s_parent; | |
120 | } | |
121 | ||
122 | /* determine end of target string for reverse fillup */ | |
123 | sd = target_sd; | |
124 | while (sd->s_parent && sd != base) { | |
125 | len += strlen(sd->s_name) + 1; | |
126 | sd = sd->s_parent; | |
127 | } | |
1da177e4 | 128 | |
2f90a851 KS |
129 | /* check limits */ |
130 | if (len < 2) | |
131 | return -EINVAL; | |
132 | len--; | |
133 | if ((s - path) + len > PATH_MAX) | |
1da177e4 LT |
134 | return -ENAMETOOLONG; |
135 | ||
2f90a851 KS |
136 | /* reverse fillup of target string from target to base */ |
137 | sd = target_sd; | |
138 | while (sd->s_parent && sd != base) { | |
139 | int slen = strlen(sd->s_name); | |
1da177e4 | 140 | |
2f90a851 KS |
141 | len -= slen; |
142 | strncpy(s + len, sd->s_name, slen); | |
143 | if (len) | |
144 | s[--len] = '/'; | |
1da177e4 | 145 | |
2f90a851 KS |
146 | sd = sd->s_parent; |
147 | } | |
1da177e4 LT |
148 | |
149 | return 0; | |
150 | } | |
151 | ||
152 | static int sysfs_getlink(struct dentry *dentry, char * path) | |
153 | { | |
2b29ac25 TH |
154 | struct sysfs_dirent *sd = dentry->d_fsdata; |
155 | struct sysfs_dirent *parent_sd = sd->s_parent; | |
b1fc3d61 | 156 | struct sysfs_dirent *target_sd = sd->s_symlink.target_sd; |
2b29ac25 | 157 | int error; |
1da177e4 | 158 | |
3007e997 | 159 | mutex_lock(&sysfs_mutex); |
2b29ac25 | 160 | error = sysfs_get_target_path(parent_sd, target_sd, path); |
3007e997 | 161 | mutex_unlock(&sysfs_mutex); |
1da177e4 | 162 | |
2b29ac25 | 163 | return error; |
1da177e4 LT |
164 | } |
165 | ||
cc314eef | 166 | static void *sysfs_follow_link(struct dentry *dentry, struct nameidata *nd) |
1da177e4 LT |
167 | { |
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); | |
cc314eef | 173 | return NULL; |
1da177e4 LT |
174 | } |
175 | ||
cc314eef | 176 | static void sysfs_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie) |
1da177e4 LT |
177 | { |
178 | char *page = nd_get_link(nd); | |
179 | if (!IS_ERR(page)) | |
180 | free_page((unsigned long)page); | |
181 | } | |
182 | ||
c5ef1c42 | 183 | const struct inode_operations sysfs_symlink_inode_operations = { |
1da177e4 LT |
184 | .readlink = generic_readlink, |
185 | .follow_link = sysfs_follow_link, | |
186 | .put_link = sysfs_put_link, | |
187 | }; | |
188 | ||
189 | ||
190 | EXPORT_SYMBOL_GPL(sysfs_create_link); | |
191 | EXPORT_SYMBOL_GPL(sysfs_remove_link); |