Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * dir.c - Operations for sysfs directories. | |
3 | */ | |
4 | ||
5 | #undef DEBUG | |
6 | ||
7 | #include <linux/fs.h> | |
8 | #include <linux/mount.h> | |
9 | #include <linux/module.h> | |
10 | #include <linux/kobject.h> | |
5f45f1a7 | 11 | #include <linux/namei.h> |
2b611bb7 | 12 | #include <linux/idr.h> |
8619f979 | 13 | #include <linux/completion.h> |
869512ab | 14 | #include <linux/mutex.h> |
1da177e4 LT |
15 | #include "sysfs.h" |
16 | ||
3007e997 | 17 | DEFINE_MUTEX(sysfs_mutex); |
932ea2e3 | 18 | DEFINE_MUTEX(sysfs_rename_mutex); |
5f995323 | 19 | spinlock_t sysfs_assoc_lock = SPIN_LOCK_UNLOCKED; |
1da177e4 | 20 | |
2b611bb7 TH |
21 | static spinlock_t sysfs_ino_lock = SPIN_LOCK_UNLOCKED; |
22 | static DEFINE_IDA(sysfs_ino_ida); | |
23 | ||
0c73f18b TH |
24 | /** |
25 | * sysfs_link_sibling - link sysfs_dirent into sibling list | |
26 | * @sd: sysfs_dirent of interest | |
27 | * | |
28 | * Link @sd into its sibling list which starts from | |
29 | * sd->s_parent->s_children. | |
30 | * | |
31 | * Locking: | |
3007e997 | 32 | * mutex_lock(sysfs_mutex) |
0c73f18b | 33 | */ |
41fc1c27 | 34 | static void sysfs_link_sibling(struct sysfs_dirent *sd) |
0c73f18b TH |
35 | { |
36 | struct sysfs_dirent *parent_sd = sd->s_parent; | |
3efa65b9 | 37 | struct sysfs_dirent **pos; |
0c73f18b TH |
38 | |
39 | BUG_ON(sd->s_sibling); | |
3efa65b9 EB |
40 | |
41 | /* Store directory entries in order by ino. This allows | |
42 | * readdir to properly restart without having to add a | |
43 | * cursor into the s_children list. | |
44 | */ | |
45 | for (pos = &parent_sd->s_children; *pos; pos = &(*pos)->s_sibling) { | |
46 | if (sd->s_ino < (*pos)->s_ino) | |
47 | break; | |
48 | } | |
49 | sd->s_sibling = *pos; | |
50 | *pos = sd; | |
0c73f18b TH |
51 | } |
52 | ||
53 | /** | |
54 | * sysfs_unlink_sibling - unlink sysfs_dirent from sibling list | |
55 | * @sd: sysfs_dirent of interest | |
56 | * | |
57 | * Unlink @sd from its sibling list which starts from | |
58 | * sd->s_parent->s_children. | |
59 | * | |
60 | * Locking: | |
3007e997 | 61 | * mutex_lock(sysfs_mutex) |
0c73f18b | 62 | */ |
41fc1c27 | 63 | static void sysfs_unlink_sibling(struct sysfs_dirent *sd) |
0c73f18b TH |
64 | { |
65 | struct sysfs_dirent **pos; | |
66 | ||
67 | for (pos = &sd->s_parent->s_children; *pos; pos = &(*pos)->s_sibling) { | |
68 | if (*pos == sd) { | |
69 | *pos = sd->s_sibling; | |
70 | sd->s_sibling = NULL; | |
71 | break; | |
72 | } | |
73 | } | |
74 | } | |
75 | ||
53e0ae92 TH |
76 | /** |
77 | * sysfs_get_dentry - get dentry for the given sysfs_dirent | |
78 | * @sd: sysfs_dirent of interest | |
79 | * | |
80 | * Get dentry for @sd. Dentry is looked up if currently not | |
e0712bbf TH |
81 | * present. This function descends from the root looking up |
82 | * dentry for each step. | |
53e0ae92 TH |
83 | * |
84 | * LOCKING: | |
932ea2e3 | 85 | * mutex_lock(sysfs_rename_mutex) |
53e0ae92 TH |
86 | * |
87 | * RETURNS: | |
88 | * Pointer to found dentry on success, ERR_PTR() value on error. | |
89 | */ | |
90 | struct dentry *sysfs_get_dentry(struct sysfs_dirent *sd) | |
91 | { | |
e0712bbf | 92 | struct dentry *dentry = dget(sysfs_sb->s_root); |
53e0ae92 | 93 | |
e0712bbf TH |
94 | while (dentry->d_fsdata != sd) { |
95 | struct sysfs_dirent *cur; | |
96 | struct dentry *parent; | |
53e0ae92 | 97 | |
e0712bbf TH |
98 | /* find the first ancestor which hasn't been looked up */ |
99 | cur = sd; | |
100 | while (cur->s_parent != dentry->d_fsdata) | |
53e0ae92 TH |
101 | cur = cur->s_parent; |
102 | ||
53e0ae92 | 103 | /* look it up */ |
e0712bbf TH |
104 | parent = dentry; |
105 | mutex_lock(&parent->d_inode->i_mutex); | |
106 | dentry = lookup_one_len_kern(cur->s_name, parent, | |
53e0ae92 | 107 | strlen(cur->s_name)); |
e0712bbf TH |
108 | mutex_unlock(&parent->d_inode->i_mutex); |
109 | dput(parent); | |
53e0ae92 | 110 | |
e0712bbf TH |
111 | if (IS_ERR(dentry)) |
112 | break; | |
53e0ae92 | 113 | } |
53e0ae92 TH |
114 | return dentry; |
115 | } | |
116 | ||
b6b4a439 TH |
117 | /** |
118 | * sysfs_get_active - get an active reference to sysfs_dirent | |
119 | * @sd: sysfs_dirent to get an active reference to | |
120 | * | |
121 | * Get an active reference of @sd. This function is noop if @sd | |
122 | * is NULL. | |
123 | * | |
124 | * RETURNS: | |
125 | * Pointer to @sd on success, NULL on failure. | |
126 | */ | |
127 | struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd) | |
128 | { | |
8619f979 TH |
129 | if (unlikely(!sd)) |
130 | return NULL; | |
131 | ||
132 | while (1) { | |
133 | int v, t; | |
134 | ||
135 | v = atomic_read(&sd->s_active); | |
136 | if (unlikely(v < 0)) | |
137 | return NULL; | |
138 | ||
139 | t = atomic_cmpxchg(&sd->s_active, v, v + 1); | |
140 | if (likely(t == v)) | |
141 | return sd; | |
142 | if (t < 0) | |
143 | return NULL; | |
144 | ||
145 | cpu_relax(); | |
b6b4a439 | 146 | } |
b6b4a439 TH |
147 | } |
148 | ||
149 | /** | |
150 | * sysfs_put_active - put an active reference to sysfs_dirent | |
151 | * @sd: sysfs_dirent to put an active reference to | |
152 | * | |
153 | * Put an active reference to @sd. This function is noop if @sd | |
154 | * is NULL. | |
155 | */ | |
156 | void sysfs_put_active(struct sysfs_dirent *sd) | |
157 | { | |
8619f979 TH |
158 | struct completion *cmpl; |
159 | int v; | |
160 | ||
161 | if (unlikely(!sd)) | |
162 | return; | |
163 | ||
164 | v = atomic_dec_return(&sd->s_active); | |
165 | if (likely(v != SD_DEACTIVATED_BIAS)) | |
166 | return; | |
167 | ||
168 | /* atomic_dec_return() is a mb(), we'll always see the updated | |
0c73f18b | 169 | * sd->s_sibling. |
8619f979 | 170 | */ |
0c73f18b | 171 | cmpl = (void *)sd->s_sibling; |
8619f979 | 172 | complete(cmpl); |
b6b4a439 TH |
173 | } |
174 | ||
175 | /** | |
176 | * sysfs_get_active_two - get active references to sysfs_dirent and parent | |
177 | * @sd: sysfs_dirent of interest | |
178 | * | |
179 | * Get active reference to @sd and its parent. Parent's active | |
180 | * reference is grabbed first. This function is noop if @sd is | |
181 | * NULL. | |
182 | * | |
183 | * RETURNS: | |
184 | * Pointer to @sd on success, NULL on failure. | |
185 | */ | |
186 | struct sysfs_dirent *sysfs_get_active_two(struct sysfs_dirent *sd) | |
187 | { | |
188 | if (sd) { | |
189 | if (sd->s_parent && unlikely(!sysfs_get_active(sd->s_parent))) | |
190 | return NULL; | |
191 | if (unlikely(!sysfs_get_active(sd))) { | |
192 | sysfs_put_active(sd->s_parent); | |
193 | return NULL; | |
194 | } | |
195 | } | |
196 | return sd; | |
197 | } | |
198 | ||
199 | /** | |
200 | * sysfs_put_active_two - put active references to sysfs_dirent and parent | |
201 | * @sd: sysfs_dirent of interest | |
202 | * | |
203 | * Put active references to @sd and its parent. This function is | |
204 | * noop if @sd is NULL. | |
205 | */ | |
206 | void sysfs_put_active_two(struct sysfs_dirent *sd) | |
207 | { | |
208 | if (sd) { | |
209 | sysfs_put_active(sd); | |
210 | sysfs_put_active(sd->s_parent); | |
211 | } | |
212 | } | |
213 | ||
214 | /** | |
215 | * sysfs_deactivate - deactivate sysfs_dirent | |
216 | * @sd: sysfs_dirent to deactivate | |
217 | * | |
8619f979 | 218 | * Deny new active references and drain existing ones. |
b6b4a439 | 219 | */ |
fb6896da | 220 | static void sysfs_deactivate(struct sysfs_dirent *sd) |
b6b4a439 | 221 | { |
8619f979 TH |
222 | DECLARE_COMPLETION_ONSTACK(wait); |
223 | int v; | |
b6b4a439 | 224 | |
380e6fbb | 225 | BUG_ON(sd->s_sibling || !(sd->s_flags & SYSFS_FLAG_REMOVED)); |
0c73f18b | 226 | sd->s_sibling = (void *)&wait; |
8619f979 TH |
227 | |
228 | /* atomic_add_return() is a mb(), put_active() will always see | |
0c73f18b | 229 | * the updated sd->s_sibling. |
b6b4a439 | 230 | */ |
8619f979 TH |
231 | v = atomic_add_return(SD_DEACTIVATED_BIAS, &sd->s_active); |
232 | ||
233 | if (v != SD_DEACTIVATED_BIAS) | |
234 | wait_for_completion(&wait); | |
235 | ||
0c73f18b | 236 | sd->s_sibling = NULL; |
b6b4a439 TH |
237 | } |
238 | ||
42b37df6 | 239 | static int sysfs_alloc_ino(ino_t *pino) |
2b611bb7 TH |
240 | { |
241 | int ino, rc; | |
242 | ||
243 | retry: | |
244 | spin_lock(&sysfs_ino_lock); | |
245 | rc = ida_get_new_above(&sysfs_ino_ida, 2, &ino); | |
246 | spin_unlock(&sysfs_ino_lock); | |
247 | ||
248 | if (rc == -EAGAIN) { | |
249 | if (ida_pre_get(&sysfs_ino_ida, GFP_KERNEL)) | |
250 | goto retry; | |
251 | rc = -ENOMEM; | |
252 | } | |
253 | ||
254 | *pino = ino; | |
255 | return rc; | |
256 | } | |
257 | ||
258 | static void sysfs_free_ino(ino_t ino) | |
259 | { | |
260 | spin_lock(&sysfs_ino_lock); | |
261 | ida_remove(&sysfs_ino_ida, ino); | |
262 | spin_unlock(&sysfs_ino_lock); | |
263 | } | |
264 | ||
fa7f912a TH |
265 | void release_sysfs_dirent(struct sysfs_dirent * sd) |
266 | { | |
13b3086d TH |
267 | struct sysfs_dirent *parent_sd; |
268 | ||
269 | repeat: | |
3007e997 TH |
270 | /* Moving/renaming is always done while holding reference. |
271 | * sd->s_parent won't change beneath us. | |
272 | */ | |
13b3086d TH |
273 | parent_sd = sd->s_parent; |
274 | ||
b402d72c | 275 | if (sysfs_type(sd) == SYSFS_KOBJ_LINK) |
2b29ac25 | 276 | sysfs_put(sd->s_elem.symlink.target_sd); |
b402d72c | 277 | if (sysfs_type(sd) & SYSFS_COPY_NAME) |
0c096b50 | 278 | kfree(sd->s_name); |
fa7f912a | 279 | kfree(sd->s_iattr); |
2b611bb7 | 280 | sysfs_free_ino(sd->s_ino); |
fa7f912a | 281 | kmem_cache_free(sysfs_dir_cachep, sd); |
13b3086d TH |
282 | |
283 | sd = parent_sd; | |
284 | if (sd && atomic_dec_and_test(&sd->s_count)) | |
285 | goto repeat; | |
fa7f912a TH |
286 | } |
287 | ||
1da177e4 LT |
288 | static void sysfs_d_iput(struct dentry * dentry, struct inode * inode) |
289 | { | |
290 | struct sysfs_dirent * sd = dentry->d_fsdata; | |
291 | ||
5a26b79c | 292 | sysfs_put(sd); |
1da177e4 LT |
293 | iput(inode); |
294 | } | |
295 | ||
296 | static struct dentry_operations sysfs_dentry_ops = { | |
297 | .d_iput = sysfs_d_iput, | |
298 | }; | |
299 | ||
3e519038 | 300 | struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type) |
1da177e4 | 301 | { |
0c096b50 | 302 | char *dup_name = NULL; |
01da2425 | 303 | struct sysfs_dirent *sd; |
0c096b50 TH |
304 | |
305 | if (type & SYSFS_COPY_NAME) { | |
306 | name = dup_name = kstrdup(name, GFP_KERNEL); | |
307 | if (!name) | |
01da2425 | 308 | return NULL; |
0c096b50 | 309 | } |
1da177e4 | 310 | |
c3762229 | 311 | sd = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL); |
1da177e4 | 312 | if (!sd) |
01da2425 | 313 | goto err_out1; |
1da177e4 | 314 | |
0c096b50 | 315 | if (sysfs_alloc_ino(&sd->s_ino)) |
01da2425 | 316 | goto err_out2; |
2b611bb7 | 317 | |
1da177e4 | 318 | atomic_set(&sd->s_count, 1); |
8619f979 | 319 | atomic_set(&sd->s_active, 0); |