]>
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); |
5f995323 | 18 | spinlock_t sysfs_assoc_lock = SPIN_LOCK_UNLOCKED; |
1da177e4 | 19 | |
2b611bb7 TH |
20 | static spinlock_t sysfs_ino_lock = SPIN_LOCK_UNLOCKED; |
21 | static DEFINE_IDA(sysfs_ino_ida); | |
22 | ||
0c73f18b TH |
23 | /** |
24 | * sysfs_link_sibling - link sysfs_dirent into sibling list | |
25 | * @sd: sysfs_dirent of interest | |
26 | * | |
27 | * Link @sd into its sibling list which starts from | |
28 | * sd->s_parent->s_children. | |
29 | * | |
30 | * Locking: | |
3007e997 | 31 | * mutex_lock(sysfs_mutex) |
0c73f18b | 32 | */ |
fb6896da | 33 | void sysfs_link_sibling(struct sysfs_dirent *sd) |
0c73f18b TH |
34 | { |
35 | struct sysfs_dirent *parent_sd = sd->s_parent; | |
36 | ||
37 | BUG_ON(sd->s_sibling); | |
38 | sd->s_sibling = parent_sd->s_children; | |
39 | parent_sd->s_children = sd; | |
40 | } | |
41 | ||
42 | /** | |
43 | * sysfs_unlink_sibling - unlink sysfs_dirent from sibling list | |
44 | * @sd: sysfs_dirent of interest | |
45 | * | |
46 | * Unlink @sd from its sibling list which starts from | |
47 | * sd->s_parent->s_children. | |
48 | * | |
49 | * Locking: | |
3007e997 | 50 | * mutex_lock(sysfs_mutex) |
0c73f18b | 51 | */ |
fb6896da | 52 | void sysfs_unlink_sibling(struct sysfs_dirent *sd) |
0c73f18b TH |
53 | { |
54 | struct sysfs_dirent **pos; | |
55 | ||
56 | for (pos = &sd->s_parent->s_children; *pos; pos = &(*pos)->s_sibling) { | |
57 | if (*pos == sd) { | |
58 | *pos = sd->s_sibling; | |
59 | sd->s_sibling = NULL; | |
60 | break; | |
61 | } | |
62 | } | |
63 | } | |
64 | ||
53e0ae92 TH |
65 | /** |
66 | * sysfs_get_dentry - get dentry for the given sysfs_dirent | |
67 | * @sd: sysfs_dirent of interest | |
68 | * | |
69 | * Get dentry for @sd. Dentry is looked up if currently not | |
70 | * present. This function climbs sysfs_dirent tree till it | |
71 | * reaches a sysfs_dirent with valid dentry attached and descends | |
72 | * down from there looking up dentry for each step. | |
73 | * | |
74 | * LOCKING: | |
75 | * Kernel thread context (may sleep) | |
76 | * | |
77 | * RETURNS: | |
78 | * Pointer to found dentry on success, ERR_PTR() value on error. | |
79 | */ | |
80 | struct dentry *sysfs_get_dentry(struct sysfs_dirent *sd) | |
81 | { | |
82 | struct sysfs_dirent *cur; | |
83 | struct dentry *parent_dentry, *dentry; | |
84 | int i, depth; | |
85 | ||
86 | /* Find the first parent which has valid s_dentry and get the | |
87 | * dentry. | |
88 | */ | |
89 | mutex_lock(&sysfs_mutex); | |
90 | restart0: | |
91 | spin_lock(&sysfs_assoc_lock); | |
92 | restart1: | |
93 | spin_lock(&dcache_lock); | |
94 | ||
95 | dentry = NULL; | |
96 | depth = 0; | |
97 | cur = sd; | |
98 | while (!cur->s_dentry || !cur->s_dentry->d_inode) { | |
99 | if (cur->s_flags & SYSFS_FLAG_REMOVED) { | |
100 | dentry = ERR_PTR(-ENOENT); | |
101 | depth = 0; | |
102 | break; | |
103 | } | |
104 | cur = cur->s_parent; | |
105 | depth++; | |
106 | } | |
107 | if (!IS_ERR(dentry)) | |
108 | dentry = dget_locked(cur->s_dentry); | |
109 | ||
110 | spin_unlock(&dcache_lock); | |
111 | spin_unlock(&sysfs_assoc_lock); | |
112 | ||
113 | /* from the found dentry, look up depth times */ | |
114 | while (depth--) { | |
115 | /* find and get depth'th ancestor */ | |
116 | for (cur = sd, i = 0; cur && i < depth; i++) | |
117 | cur = cur->s_parent; | |
118 | ||
119 | /* This can happen if tree structure was modified due | |
120 | * to move/rename. Restart. | |
121 | */ | |
122 | if (i != depth) { | |
123 | dput(dentry); | |
124 | goto restart0; | |
125 | } | |
126 | ||
127 | sysfs_get(cur); | |
128 | ||
129 | mutex_unlock(&sysfs_mutex); | |
130 | ||
131 | /* look it up */ | |
132 | parent_dentry = dentry; | |
133 | dentry = lookup_one_len_kern(cur->s_name, parent_dentry, | |
134 | strlen(cur->s_name)); | |
135 | dput(parent_dentry); | |
136 | ||
137 | if (IS_ERR(dentry)) { | |
138 | sysfs_put(cur); | |
139 | return dentry; | |
140 | } | |
141 | ||
142 | mutex_lock(&sysfs_mutex); | |
143 | spin_lock(&sysfs_assoc_lock); | |
144 | ||
145 | /* This, again, can happen if tree structure has | |
146 | * changed and we looked up the wrong thing. Restart. | |
147 | */ | |
148 | if (cur->s_dentry != dentry) { | |
149 | dput(dentry); | |
150 | sysfs_put(cur); | |
151 | goto restart1; | |
152 | } | |
153 | ||
154 | spin_unlock(&sysfs_assoc_lock); | |
155 | ||
156 | sysfs_put(cur); | |
157 | } | |
158 | ||
159 | mutex_unlock(&sysfs_mutex); | |
160 | return dentry; | |
161 | } | |
162 | ||
b6b4a439 TH |
163 | /** |
164 | * sysfs_get_active - get an active reference to sysfs_dirent | |
165 | * @sd: sysfs_dirent to get an active reference to | |
166 | * | |
167 | * Get an active reference of @sd. This function is noop if @sd | |
168 | * is NULL. | |
169 | * | |
170 | * RETURNS: | |
171 | * Pointer to @sd on success, NULL on failure. | |
172 | */ | |
173 | struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd) | |
174 | { | |
8619f979 TH |
175 | if (unlikely(!sd)) |
176 | return NULL; | |
177 | ||
178 | while (1) { | |
179 | int v, t; | |
180 | ||
181 | v = atomic_read(&sd->s_active); | |
182 | if (unlikely(v < 0)) | |
183 | return NULL; | |
184 | ||
185 | t = atomic_cmpxchg(&sd->s_active, v, v + 1); | |
186 | if (likely(t == v)) | |
187 | return sd; | |
188 | if (t < 0) | |
189 | return NULL; | |
190 | ||
191 | cpu_relax(); | |
b6b4a439 | 192 | } |
b6b4a439 TH |
193 | } |
194 | ||
195 | /** | |
196 | * sysfs_put_active - put an active reference to sysfs_dirent | |
197 | * @sd: sysfs_dirent to put an active reference to | |
198 | * | |
199 | * Put an active reference to @sd. This function is noop if @sd | |
200 | * is NULL. | |
201 | */ | |
202 | void sysfs_put_active(struct sysfs_dirent *sd) | |
203 | { | |
8619f979 TH |
204 | struct completion *cmpl; |
205 | int v; | |
206 | ||
207 | if (unlikely(!sd)) | |
208 | return; | |
209 | ||
210 | v = atomic_dec_return(&sd->s_active); | |
211 | if (likely(v != SD_DEACTIVATED_BIAS)) | |
212 | return; | |
213 | ||
214 | /* atomic_dec_return() is a mb(), we'll always see the updated | |
0c73f18b | 215 | * sd->s_sibling. |
8619f979 | 216 | */ |
0c73f18b | 217 | cmpl = (void *)sd->s_sibling; |
8619f979 | 218 | complete(cmpl); |
b6b4a439 TH |
219 | } |
220 | ||
221 | /** | |
222 | * sysfs_get_active_two - get active references to sysfs_dirent and parent | |
223 | * @sd: sysfs_dirent of interest | |
224 | * | |
225 | * Get active reference to @sd and its parent. Parent's active | |
226 | * reference is grabbed first. This function is noop if @sd is | |
227 | * NULL. | |
228 | * | |
229 | * RETURNS: | |
230 | * Pointer to @sd on success, NULL on failure. | |
231 | */ | |
232 | struct sysfs_dirent *sysfs_get_active_two(struct sysfs_dirent *sd) | |
233 | { | |
234 | if (sd) { | |
235 | if (sd->s_parent && unlikely(!sysfs_get_active(sd->s_parent))) | |
236 | return NULL; | |
237 | if (unlikely(!sysfs_get_active(sd))) { | |
238 | sysfs_put_active(sd->s_parent); | |
239 | return NULL; | |
240 | } | |
241 | } | |
242 | return sd; | |
243 | } | |
244 | ||
245 | /** | |
246 | * sysfs_put_active_two - put active references to sysfs_dirent and parent | |
247 | * @sd: sysfs_dirent of interest | |
248 | * | |
249 | * Put active references to @sd and its parent. This function is | |
250 | * noop if @sd is NULL. | |
251 | */ | |
252 | void sysfs_put_active_two(struct sysfs_dirent *sd) | |
253 | { | |
254 | if (sd) { | |
255 | sysfs_put_active(sd); | |
256 | sysfs_put_active(sd->s_parent); | |
257 | } | |
258 | } | |
259 | ||
260 | /** | |
261 | * sysfs_deactivate - deactivate sysfs_dirent | |
262 | * @sd: sysfs_dirent to deactivate | |
263 | * | |
8619f979 | 264 | * Deny new active references and drain existing ones. |
b6b4a439 | 265 | */ |
fb6896da | 266 | static void sysfs_deactivate(struct sysfs_dirent *sd) |
b6b4a439 | 267 | { |
8619f979 TH |
268 | DECLARE_COMPLETION_ONSTACK(wait); |
269 | int v; | |
b6b4a439 | 270 | |
380e6fbb | 271 | BUG_ON(sd->s_sibling || !(sd->s_flags & SYSFS_FLAG_REMOVED)); |
0c73f18b | 272 | sd->s_sibling = (void *)&wait; |
8619f979 TH |
273 | |
274 | /* atomic_add_return() is a mb(), put_active() will always see | |
0c73f18b | 275 | * the updated sd->s_sibling. |
b6b4a439 | 276 | */ |
8619f979 TH |
277 | v = atomic_add_return(SD_DEACTIVATED_BIAS, &sd->s_active); |
278 | ||
279 | if (v != SD_DEACTIVATED_BIAS) | |
280 | wait_for_completion(&wait); | |
281 | ||
0c73f18b | 282 | sd->s_sibling = NULL; |
b6b4a439 TH |
283 | } |
284 | ||
42b37df6 | 285 | static int sysfs_alloc_ino(ino_t *pino) |
2b611bb7 TH |
286 | { |
287 | int ino, rc; | |
288 | ||
289 | retry: | |
290 | spin_lock(&sysfs_ino_lock); | |
291 | rc = ida_get_new_above(&sysfs_ino_ida, 2, &ino); | |
292 | spin_unlock(&sysfs_ino_lock); | |
293 | ||
294 | if (rc == -EAGAIN) { | |
295 | if (ida_pre_get(&sysfs_ino_ida, GFP_KERNEL)) | |
296 | goto retry; | |
297 | rc = -ENOMEM; | |
298 | } | |
299 | ||
300 | *pino = ino; | |
301 | return rc; | |
302 | } | |
303 | ||
304 | static void sysfs_free_ino(ino_t ino) | |
305 | { | |
306 | spin_lock(&sysfs_ino_lock); | |
307 | ida_remove(&sysfs_ino_ida, ino); | |
308 | spin_unlock(&sysfs_ino_lock); | |
309 | } | |
310 | ||
fa7f912a TH |
311 | void release_sysfs_dirent(struct sysfs_dirent * sd) |
312 | { | |
13b3086d TH |
313 | struct sysfs_dirent *parent_sd; |
314 | ||
315 | repeat: | |
3007e997 TH |
316 | /* Moving/renaming is always done while holding reference. |
317 | * sd->s_parent won't change beneath us. | |
318 | */ | |
13b3086d TH |
319 | parent_sd = sd->s_parent; |
320 | ||
b402d72c | 321 | if (sysfs_type(sd) == SYSFS_KOBJ_LINK) |
2b29ac25 | 322 | sysfs_put(sd->s_elem.symlink.target_sd); |
b402d72c | 323 | if (sysfs_type(sd) & SYSFS_COPY_NAME) |
0c096b50 | 324 | kfree(sd->s_name); |
fa7f912a | 325 | kfree(sd->s_iattr); |
2b611bb7 | 326 | sysfs_free_ino(sd->s_ino); |
fa7f912a | 327 | kmem_cache_free(sysfs_dir_cachep, sd); |
13b3086d TH |
328 | |
329 | sd = parent_sd; | |
330 | if (sd && atomic_dec_and_test(&sd->s_count)) | |
331 | goto repeat; | |
fa7f912a TH |
332 | } |
333 | ||
1da177e4 LT |
334 | static void sysfs_d_iput(struct dentry * dentry, struct inode * inode) |
335 | { | |
336 | struct sysfs_dirent * sd = dentry->d_fsdata; | |
337 | ||
338 | if (sd) { | |
5f995323 TH |
339 | /* sd->s_dentry is protected with sysfs_assoc_lock. |
340 | * This allows sysfs_drop_dentry() to dereference it. | |
dd14cbc9 | 341 | */ |
5f995323 | 342 | spin_lock(&sysfs_assoc_lock); |
dd14cbc9 TH |
343 | |
344 | /* The dentry might have been deleted or another | |
345 | * lookup could have happened updating sd->s_dentry to | |
346 | * point the new dentry. Ignore if it isn't pointing | |
347 | * to this dentry. | |
348 | */ | |
349 | if (sd->s_dentry == dentry) | |
350 | sd->s_dentry = NULL; | |
5f995323 | 351 | spin_unlock(&sysfs_assoc_lock); |
1da177e4 LT |
352 | sysfs_put(sd); |
353 | } | |
354 | iput(inode); | |
355 | } | |
356 | ||
357 | static struct dentry_operations sysfs_dentry_ops = { | |
358 | .d_iput = sysfs_d_iput, | |
359 | }; | |
360 | ||
3e519038 | 361 | struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type) |
1da177e4 | 362 | { |
0c096b50 | 363 | char *dup_name = NULL; |
01da2425 | 364 | struct sysfs_dirent *sd; |
0c096b50 TH |
365 | |
366 | if (type & SYSFS_COPY_NAME) { | |
367 | name = dup_name = kstrdup(name, GFP_KERNEL); | |
368 | if (!name) | |
01da2425 | 369 | return NULL; |
0c096b50 | 370 | } |
1da177e4 | 371 | |
c3762229 | 372 | sd = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL); |
1da177e4 | 373 | if (!sd) |
01da2425 | 374 | goto err_out1; |
1da177e4 | 375 | |
0c096b50 | 376 | if (sysfs_alloc_ino(&sd->s_ino)) |
01da2425 | 377 | goto err_out2; |
2b611bb7 | 378 | |
1da177e4 | 379 | atomic_set(&sd->s_count, 1); |
8619f979 | 380 | atomic_set(&sd->s_active, 0); |
eea3f891 | 381 | atomic_set(&sd->s_event, 1); |
a26cd722 | 382 | |
0c096b50 | 383 | sd->s_name = name; |
a26cd722 | 384 | sd->s_mode = mode; |
b402d72c | 385 | sd->s_flags = type; |
1da177e4 LT |
386 | |
387 | return sd; | |
0c096b50 | 388 | |
01da2425 | 389 | err_out2: |
0c096b50 | 390 | kmem_cache_free(sysfs_dir_cachep, sd); |
01da2425 AM |
391 | err_out1: |
392 | kfree(dup_name); | |
0c096b50 | 393 | return NULL; |
1da177e4 LT |
394 | } |
395 | ||
3007e997 TH |
396 | /** |
397 | * sysfs_attach_dentry - associate sysfs_dirent with dentry | |
398 | * @sd: target sysfs_dirent | |
399 | * @dentry: dentry to associate | |
400 | * | |
401 | * Associate @sd with @dentry. This is protected by | |
402 | * sysfs_assoc_lock to avoid race with sysfs_d_iput(). | |
403 | * | |
404 | * LOCKING: | |
405 | * mutex_lock(sysfs_mutex) | |
406 | */ | |
198a2a84 TH |
407 | static void sysfs_attach_dentry(struct sysfs_dirent *sd, struct dentry *dentry) |
408 | { | |
409 | dentry->d_op = &sysfs_dentry_ops; | |
410 | dentry->d_fsdata = sysfs_get(sd); | |
411 | ||
412 | /* protect sd->s_dentry against sysfs_d_iput */ | |
5f995323 | 413 | spin_lock(&sysfs_assoc_lock); |
198a2a84 | 414 | sd->s_dentry = dentry; |
5f995323 | 415 | spin_unlock(&sysfs_assoc_lock); |
198a2a84 TH |
416 | |
417 | d_rehash(dentry); | |
418 | } | |
419 | ||
fb6896da TH |
420 | static int sysfs_ilookup_test(struct inode *inode, void *arg) |
421 | { | |
422 | struct sysfs_dirent *sd = arg; | |
423 | return inode->i_ino == sd->s_ino; | |
424 | } | |
425 | ||
3007e997 | 426 | /** |
fb6896da TH |
427 | * sysfs_addrm_start - prepare for sysfs_dirent add/remove |
428 | * @acxt: pointer to sysfs_addrm_cxt to be used | |
429 | * @parent_sd: parent sysfs_dirent | |
3007e997 | 430 | * |
fb6896da TH |
431 | * This function is called when the caller is about to add or |
432 | * remove sysfs_dirent under @parent_sd. This function acquires | |
433 | * sysfs_mutex, grabs inode for @parent_sd if available and lock | |
434 | * i_mutex of it. @acxt is used to keep and pass context to | |
435 | * other addrm functions. | |
3007e997 TH |
436 | * |
437 | * LOCKING: | |
fb6896da TH |
438 | * Kernel thread context (may sleep). sysfs_mutex is locked on |
439 | * return. i_mutex of parent inode is locked on return if | |
440 | * available. | |
3007e997 | 441 | */ |
fb6896da TH |
442 | void sysfs_addrm_start(struct sysfs_addrm_cxt *acxt, |
443 | struct sysfs_dirent *parent_sd) | |
b592fcfe | 444 | { |
fb6896da | 445 | struct inode *inode; |
b592fcfe | 446 | |
fb6896da TH |
447 | memset(acxt, 0, sizeof(*acxt)); |
448 | acxt->parent_sd = parent_sd; | |
449 | ||
450 | /* Lookup parent inode. inode initialization and I_NEW | |
451 | * clearing are protected by sysfs_mutex. By grabbing it and | |
452 | * looking up with _nowait variant, inode state can be | |
453 | * determined reliably. | |
454 | */ | |
455 | mutex_lock(&sysfs_mutex); | |
456 | ||
457 | inode = ilookup5_nowait(sysfs_sb, parent_sd->s_ino, sysfs_ilookup_test, | |
458 | parent_sd); | |
459 | ||
460 | if (inode && !(inode->i_state & I_NEW)) { | |
461 | /* parent inode available */ | |
462 | acxt->parent_inode = inode; | |
463 | ||
464 | /* sysfs_mutex is below i_mutex in lock hierarchy. | |
465 | * First, trylock i_mutex. If fails, unlock | |
466 | * sysfs_mutex and lock them in order. | |
467 | */ | |
468 | if (!mutex_trylock(&inode->i_mutex)) { | |
469 | mutex_unlock(&sysfs_mutex); | |
470 | mutex_lock(&inode->i_mutex); | |
471 | mutex_lock(&sysfs_mutex); | |
472 | } | |
473 | } else | |
474 | iput(inode); | |
475 | } | |
476 | ||
477 | /** | |
478 | * sysfs_add_one - add sysfs_dirent to parent | |
479 | * @acxt: addrm context to use | |
480 | * @sd: sysfs_dirent to be added | |
481 | * | |
482 | * Get @acxt->parent_sd and set sd->s_parent to it and increment | |
483 | * nlink of parent inode if @sd is a directory. @sd is NOT | |
484 | * linked into the children list of the parent. The caller | |
485 | * should invoke sysfs_link_sibling() after this function | |
486 | * completes if @sd needs to be on the children list. | |
487 | * | |
488 | * This function should be called between calls to | |
489 | * sysfs_addrm_start() and sysfs_addrm_finish() and should be | |
490 | * passed the same @acxt as passed to sysfs_addrm_start(). | |
491 | * | |
492 | * LOCKING: | |
493 | * Determined by sysfs_addrm_start(). | |
494 | */ | |
495 | void sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd) | |
496 | { | |
497 | sd->s_parent = sysfs_get(acxt->parent_sd); | |
498 | ||
499 | if (sysfs_type(sd) == SYSFS_DIR && acxt->parent_inode) | |
500 | inc_nlink(acxt->parent_inode); | |
501 | ||
502 | acxt->cnt++; | |
503 | } | |
504 | ||
505 | /** | |
506 | * sysfs_remove_one - remove sysfs_dirent from parent | |
507 | * @acxt: addrm context to use | |
508 | * @sd: sysfs_dirent to be added | |
509 | * | |
510 | * Mark @sd removed and drop nlink of parent inode if @sd is a | |
511 | * directory. @sd is NOT unlinked from the children list of the | |
512 | * parent. The caller is repsonsible for removing @sd from the | |
513 | * children list before calling this function. | |
514 | * | |
515 | * This function should be called between calls to | |
516 | * sysfs_addrm_start() and sysfs_addrm_finish() and should be | |
517 | * passed the same @acxt as passed to sysfs_addrm_start(). | |
518 | * | |
519 | * LOCKING: | |
520 | * Determined by sysfs_addrm_start(). | |
521 | */ | |
522 | void sysfs_remove_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd) | |
523 | { | |
524 | BUG_ON(sd->s_sibling || (sd->s_flags & SYSFS_FLAG_REMOVED)); | |
525 | ||
526 | sd->s_flags |= SYSFS_FLAG_REMOVED; | |
527 | sd->s_sibling = acxt->removed; | |
528 | acxt->removed = sd; | |
529 | ||
530 | if (sysfs_type(sd) == SYSFS_DIR && acxt->parent_inode) | |
531 | drop_nlink(acxt->parent_inode); | |
532 | ||
533 | acxt->cnt++; | |
534 | } | |
535 | ||
a0edd7c8 TH |
536 | /** |
537 | * sysfs_drop_dentry - drop dentry for the specified sysfs_dirent | |
538 | * @sd: target sysfs_dirent | |
539 | * | |
540 | * Drop dentry for @sd. @sd must have been unlinked from its | |
541 | * parent on entry to this function such that it can't be looked | |
542 | * up anymore. | |
543 | * | |
544 | * @sd->s_dentry which is protected with sysfs_assoc_lock points | |
545 | * to the currently associated dentry but we're not holding a | |
546 | * reference to it and racing with dput(). Grab dcache_lock and | |
547 | * verify dentry before dropping it. If @sd->s_dentry is NULL or | |
548 | * dput() beats us, no need to bother. | |
549 | */ | |
550 | static void sysfs_drop_dentry(struct sysfs_dirent *sd) | |
551 | { | |
552 | struct dentry *dentry = NULL; | |
553 | struct inode *inode; | |
554 | ||
555 | /* We're not holding a reference to ->s_dentry dentry but the | |
556 | * field will stay valid as long as sysfs_assoc_lock is held. | |
557 | */ | |
558 | spin_lock(&sysfs_assoc_lock); | |
559 | spin_lock(&dcache_lock); | |
560 | ||
561 | /* drop dentry if it's there and dput() didn't kill it yet */ | |
562 | if (sd->s_dentry && sd->s_dentry->d_inode) { | |
563 | dentry = dget_locked(sd->s_dentry); | |
564 | spin_lock(&dentry->d_lock); | |
565 | __d_drop(dentry); | |
566 | spin_unlock(&dentry->d_lock); | |
567 | } | |
568 | ||
569 | spin_unlock(&dcache_lock); | |
570 | spin_unlock(&sysfs_assoc_lock); | |
571 | ||
51225039 TH |
572 | /* dentries for shadowed inodes are pinned, unpin */ |
573 | if (dentry && sysfs_is_shadowed_inode(dentry->d_inode)) | |
a0edd7c8 | 574 | dput(dentry); |
51225039 | 575 | dput(dentry); |
a0edd7c8 TH |
576 | |
577 | /* adjust nlink and update timestamp */ | |
578 | inode = ilookup(sysfs_sb, sd->s_ino); | |
579 | if (inode) { | |
580 | mutex_lock(&inode->i_mutex); | |
581 | ||
582 | inode->i_ctime = CURRENT_TIME; | |
583 | drop_nlink(inode); | |
584 | if (sysfs_type(sd) == SYSFS_DIR) | |
585 | drop_nlink(inode); | |
586 | ||
587 | mutex_unlock(&inode->i_mutex); | |
588 | iput(inode); | |
589 | } | |
590 | } | |
591 | ||
fb6896da TH |
592 | /** |
593 | * sysfs_addrm_finish - finish up sysfs_dirent add/remove | |
594 | * @acxt: addrm context to finish up | |
595 | * | |
596 | * Finish up sysfs_dirent add/remove. Resources acquired by | |
597 | * sysfs_addrm_start() are released and removed sysfs_dirents are | |
598 | * cleaned up. Timestamps on the parent inode are updated. | |
599 | * | |
600 | * LOCKING: | |
601 | * All mutexes acquired by sysfs_addrm_start() are released. | |
602 | * | |
603 | * RETURNS: | |
604 | * Number of added/removed sysfs_dirents since sysfs_addrm_start(). | |
605 | */ | |
606 | int sysfs_addrm_finish(struct sysfs_addrm_cxt *acxt) | |
607 | { | |
608 | /* release resources acquired by sysfs_addrm_start() */ | |
609 | mutex_unlock(&sysfs_mutex); | |
610 | if (acxt->parent_inode) { | |
611 | struct inode *inode = acxt->parent_inode; | |
612 | ||
613 | /* if added/removed, update timestamps on the parent */ | |
614 | if (acxt->cnt) | |
615 | inode->i_ctime = inode->i_mtime = CURRENT_TIME; | |
616 | ||
617 | mutex_unlock(&inode->i_mutex); | |
618 | iput(inode); | |
619 | } | |
620 | ||
621 | /* kill removed sysfs_dirents */ | |
622 | while (acxt->removed) { | |
623 | struct sysfs_dirent *sd = acxt->removed; | |
624 | ||
625 | acxt->removed = sd->s_sibling; | |
626 | sd->s_sibling = NULL; | |
627 | ||
628 | sysfs_drop_dentry(sd); | |
629 | sysfs_deactivate(sd); | |
630 | sysfs_put(sd); | |
13b3086d | 631 | } |
fb6896da TH |
632 | |
633 | return acxt->cnt; | |
b592fcfe EB |
634 | } |
635 | ||
f0b0af47 TH |
636 | /** |
637 | * sysfs_find_dirent - find sysfs_dirent with the given name | |
638 | * @parent_sd: sysfs_dirent to search under | |
639 | * @name: name to look for | |
640 | * | |
641 | * Look for sysfs_dirent with name @name under @parent_sd. | |
c516865c | 642 | * |
f0b0af47 | 643 | * LOCKING: |
3007e997 | 644 | * mutex_lock(sysfs_mutex) |
c516865c | 645 | * |
f0b0af47 TH |
646 | * RETURNS: |
647 | * Pointer to sysfs_dirent if found, NULL if not. | |
c516865c | 648 | */ |
f0b0af47 TH |
649 | struct sysfs_dirent *sysfs_find_dirent(struct sysfs_dirent *parent_sd, |
650 | const unsigned char *name) | |
c516865c | 651 | { |
f0b0af47 TH |
652 | struct sysfs_dirent *sd; |
653 | ||
654 | for (sd = parent_sd->s_children; sd; sd = sd->s_sibling) | |
655 | if (sysfs_type(sd) && !strcmp(sd->s_name, name)) | |
656 | return sd; | |
657 | return NULL; | |
658 | } | |
c516865c | 659 | |
f0b0af47 TH |
660 | /** |
661 | * sysfs_get_dirent - find and get sysfs_dirent with the given name | |
662 | * @parent_sd: sysfs_dirent to search under | |
663 | * @name: name to look for | |
664 | * | |
665 | * Look for sysfs_dirent with name @name under @parent_sd and get | |
666 | * it if found. | |
667 | * | |
668 | * LOCKING: | |
3007e997 | 669 | * Kernel thread context (may sleep). Grabs sysfs_mutex. |
f0b0af47 TH |
670 | * |
671 | * RETURNS: | |
672 | * Pointer to sysfs_dirent if found, NULL if not. | |
673 | */ | |
674 | struct sysfs_dirent *sysfs_get_dirent(struct sysfs_dirent *parent_sd, | |
675 | const unsigned char *name) | |
676 | { | |
677 | struct sysfs_dirent *sd; | |
678 | ||
3007e997 | 679 | mutex_lock(&sysfs_mutex); |
f0b0af47 TH |
680 | sd = sysfs_find_dirent(parent_sd, name); |
681 | sysfs_get(sd); | |
3007e997 | 682 | mutex_unlock(&sysfs_mutex); |
f0b0af47 TH |
683 | |
684 | return sd; | |
c516865c MS |
685 | } |
686 | ||
608e266a TH |
687 | static int create_dir(struct kobject *kobj, struct sysfs_dirent *parent_sd, |
688 | const char *name, struct sysfs_dirent **p_sd) | |
1da177e4 | 689 | { |
1da177e4 | 690 | umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO; |
51225039 | 691 | struct sysfs_addrm_cxt acxt; |
dfeb9fb0 | 692 | struct sysfs_dirent *sd; |
1da177e4 | 693 | |
fc9f54b9 | 694 | /* allocate */ |
3e519038 | 695 | sd = sysfs_new_dirent(name, mode, SYSFS_DIR); |
a26cd722 | 696 | if (!sd) |
51225039 | 697 | return -ENOMEM; |
3e519038 | 698 | sd->s_elem.dir.kobj = kobj; |
dfeb9fb0 | 699 | |
fc9f54b9 | 700 | /* link in */ |
51225039 | 701 | sysfs_addrm_start(&acxt, parent_sd); |
967e35dc | 702 | |
51225039 TH |
703 | if (!sysfs_find_dirent(parent_sd, name)) { |
704 | sysfs_add_one(&acxt, sd); | |
705 | sysfs_link_sibling(sd); | |
706 | } | |
967e35dc TH |
707 | |
708 | if (!sysfs_addrm_finish(&acxt)) { | |
709 | sysfs_put(sd); | |
710 | return -EEXIST; | |
51225039 | 711 | } |
dfeb9fb0 | 712 | |
967e35dc TH |
713 | *p_sd = sd; |
714 | return 0; | |
1da177e4 LT |
715 | } |
716 | ||
608e266a TH |
717 | int sysfs_create_subdir(struct kobject *kobj, const char *name, |
718 | struct sysfs_dirent **p_sd) | |
1da177e4 | 719 | { |
608e266a | 720 | return create_dir(kobj, kobj->sd, name, p_sd); |
1da177e4 LT |
721 | } |
722 | ||
723 | /** | |
724 | * sysfs_create_dir - create a directory for an object. | |
1da177e4 | 725 | * @kobj: object we're creating directory for. |
608e266a | 726 | * @shadow_parent: parent object. |
1da177e4 | 727 | */ |
608e266a TH |
728 | int sysfs_create_dir(struct kobject *kobj, |
729 | struct sysfs_dirent *shadow_parent_sd) | |
1da177e4 | 730 | { |
608e266a | 731 | struct sysfs_dirent *parent_sd, *sd; |
1da177e4 LT |
732 | int error = 0; |
733 | ||
734 | BUG_ON(!kobj); | |
735 | ||
608e266a TH |
736 | if (shadow_parent_sd) |
737 | parent_sd = shadow_parent_sd; | |
b592fcfe | 738 | else if (kobj->parent) |
608e266a | 739 | parent_sd = kobj->parent->sd; |
1da177e4 | 740 | else if (sysfs_mount && sysfs_mount->mnt_sb) |
608e266a | 741 | parent_sd = sysfs_mount->mnt_sb->s_root->d_fsdata; |
1da177e4 LT |
742 | else |
743 | return -EFAULT; | |
744 | ||
608e266a | 745 | error = create_dir(kobj, parent_sd, kobject_name(kobj), &sd); |
1da177e4 | 746 | if (!error) |
608e266a | 747 | kobj->sd = sd; |
1da177e4 LT |
748 | return error; |
749 | } | |
750 | ||
51225039 TH |
751 | static int sysfs_count_nlink(struct sysfs_dirent *sd) |
752 | { | |
753 | struct sysfs_dirent *child; | |
754 | int nr = 0; | |
755 | ||
756 | for (child = sd->s_children; child; child = child->s_sibling) | |
757 | if (sysfs_type(child) == SYSFS_DIR) | |
758 | nr++; | |
759 | return nr + 2; | |
760 | } | |
761 | ||
1da177e4 LT |
762 | static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry, |
763 | struct nameidata *nd) | |
764 | { | |
6cb52147 | 765 | struct dentry *ret = NULL; |
1da177e4 LT |
766 | struct sysfs_dirent * parent_sd = dentry->d_parent->d_fsdata; |
767 | struct sysfs_dirent * sd; | |
b402d72c | 768 | struct bin_attribute *bin_attr; |
fc9f54b9 TH |
769 | struct inode *inode; |
770 | int found = 0; | |
1da177e4 | 771 | |
6cb52147 TH |
772 | mutex_lock(&sysfs_mutex); |
773 | ||
0c73f18b | 774 | for (sd = parent_sd->s_children; sd; sd = sd->s_sibling) { |
51225039 | 775 | if (sysfs_type(sd) && |
fc9f54b9 TH |
776 | !strcmp(sd->s_name, dentry->d_name.name)) { |
777 | found = 1; | |
1da177e4 LT |
778 | break; |
779 | } | |
780 | } | |
781 | ||
fc9f54b9 TH |
782 | /* no such entry */ |
783 | if (!found) | |
6cb52147 | 784 | goto out_unlock; |
fc9f54b9 TH |
785 | |
786 | /* attach dentry and inode */ | |
8312a8d7 | 787 | inode = sysfs_get_inode(sd); |
6cb52147 TH |
788 | if (!inode) { |
789 | ret = ERR_PTR(-ENOMEM); | |
790 | goto out_unlock; | |
791 | } | |
3007e997 | 792 | |
8312a8d7 TH |
793 | if (inode->i_state & I_NEW) { |
794 | /* initialize inode according to type */ | |
b402d72c | 795 | switch (sysfs_type(sd)) { |
51225039 TH |
796 | case SYSFS_DIR: |
797 | inode->i_op = &sysfs_dir_inode_operations; | |
798 | inode->i_fop = &sysfs_dir_operations; | |
799 | inode->i_nlink = sysfs_count_nlink(sd); | |
800 | break; | |
b402d72c | 801 | case SYSFS_KOBJ_ATTR: |
8312a8d7 TH |
802 | inode->i_size = PAGE_SIZE; |
803 | inode->i_fop = &sysfs_file_operations; | |
b402d72c TH |
804 | break; |
805 | case SYSFS_KOBJ_BIN_ATTR: | |
806 | bin_attr = sd->s_elem.bin_attr.bin_attr; | |
8312a8d7 TH |
807 | inode->i_size = bin_attr->size; |
808 | inode->i_fop = &bin_fops; | |
b402d72c TH |
809 | break; |
810 | case SYSFS_KOBJ_LINK: | |
8312a8d7 | 811 | inode->i_op = &sysfs_symlink_inode_operations; |
b402d72c TH |
812 | break; |
813 | default: | |
814 | BUG(); | |
815 | } | |
8312a8d7 | 816 | } |
fc9f54b9 TH |
817 | |
818 | sysfs_instantiate(dentry, inode); | |
819 | sysfs_attach_dentry(sd, dentry); | |
820 | ||
6cb52147 | 821 | out_unlock: |
3007e997 | 822 | mutex_unlock(&sysfs_mutex); |
6cb52147 | 823 | return ret; |
1da177e4 LT |
824 | } |
825 | ||
c5ef1c42 | 826 | const struct inode_operations sysfs_dir_inode_operations = { |
1da177e4 | 827 | .lookup = sysfs_lookup, |
988d186d | 828 | .setattr = sysfs_setattr, |
1da177e4 LT |
829 | }; |
830 | ||
608e266a | 831 | static void remove_dir(struct sysfs_dirent *sd) |
1da177e4 | 832 | { |
fb6896da | 833 | struct sysfs_addrm_cxt acxt; |
1da177e4 | 834 | |
fb6896da TH |
835 | sysfs_addrm_start(&acxt, sd->s_parent); |
836 | sysfs_unlink_sibling(sd); | |
837 | sysfs_remove_one(&acxt, sd); | |
838 | sysfs_addrm_finish(&acxt); | |
1da177e4 LT |
839 | } |
840 | ||
608e266a | 841 | void sysfs_remove_subdir(struct sysfs_dirent *sd) |
1da177e4 | 842 | { |
608e266a | 843 | remove_dir(sd); |
1da177e4 LT |
844 | } |
845 | ||
846 | ||
608e266a | 847 | static void __sysfs_remove_dir(struct sysfs_dirent *dir_sd) |
1da177e4 | 848 | { |
fb6896da | 849 | struct sysfs_addrm_cxt acxt; |
0c73f18b | 850 | struct sysfs_dirent **pos; |
1da177e4 | 851 | |
608e266a | 852 | if (!dir_sd) |
1da177e4 LT |
853 | return; |
854 | ||
608e266a | 855 | pr_debug("sysfs %s: removing dir\n", dir_sd->s_name); |
fb6896da | 856 | sysfs_addrm_start(&acxt, dir_sd); |
608e266a | 857 | pos = &dir_sd->s_children; |
0c73f18b TH |
858 | while (*pos) { |
859 | struct sysfs_dirent *sd = *pos; | |
860 | ||
51225039 | 861 | if (sysfs_type(sd) && sysfs_type(sd) != SYSFS_DIR) { |
0c73f18b | 862 | *pos = sd->s_sibling; |
fb6896da TH |
863 | sd->s_sibling = NULL; |
864 | sysfs_remove_one(&acxt, sd); | |
0c73f18b TH |
865 | } else |
866 | pos = &(*pos)->s_sibling; | |
1da177e4 | 867 | } |
fb6896da | 868 | sysfs_addrm_finish(&acxt); |
0ab66088 | 869 | |
608e266a | 870 | remove_dir(dir_sd); |
b592fcfe EB |
871 | } |
872 | ||
873 | /** | |
874 | * sysfs_remove_dir - remove an object's directory. | |
875 | * @kobj: object. | |
876 | * | |
877 | * The only thing special about this is that we remove any files in | |
878 | * the directory before we remove the directory, and we've inlined | |
879 | * what used to be sysfs_rmdir() below, instead of calling separately. | |
880 | */ | |
881 | ||
882 | void sysfs_remove_dir(struct kobject * kobj) | |
883 | { | |
608e266a | 884 | struct sysfs_dirent *sd = kobj->sd; |
aecdceda | 885 | |
5f995323 | 886 | spin_lock(&sysfs_assoc_lock); |
608e266a | 887 | kobj->sd = NULL; |
5f995323 | 888 | spin_unlock(&sysfs_assoc_lock); |
aecdceda | 889 | |
608e266a | 890 | __sysfs_remove_dir(sd); |
1da177e4 LT |
891 | } |
892 | ||
608e266a | 893 | int sysfs_rename_dir(struct kobject *kobj, struct sysfs_dirent *new_parent_sd, |
b592fcfe | 894 | const char *new_name) |
1da177e4 | 895 | { |
608e266a | 896 | struct sysfs_dirent *sd = kobj->sd; |
51225039 TH |
897 | struct dentry *new_parent = NULL; |
898 | struct dentry *old_dentry = NULL, *new_dentry = NULL; | |
899 | const char *dup_name = NULL; | |
996b7376 | 900 | int error; |
1da177e4 | 901 | |
51225039 TH |
902 | /* get dentries */ |
903 | old_dentry = sysfs_get_dentry(sd); | |
904 | if (IS_ERR(old_dentry)) { | |
905 | error = PTR_ERR(old_dentry); | |
906 | goto out_dput; | |
907 | } | |
908 | ||
909 | new_parent = sysfs_get_dentry(new_parent_sd); | |
910 | if (IS_ERR(new_parent)) { | |
911 | error = PTR_ERR(new_parent); | |
912 | goto out_dput; | |
913 | } | |
1da177e4 | 914 | |
51225039 | 915 | /* lock new_parent and get dentry for new name */ |
b592fcfe | 916 | mutex_lock(&new_parent->d_inode->i_mutex); |
1da177e4 | 917 | |
b592fcfe | 918 | new_dentry = lookup_one_len(new_name, new_parent, strlen(new_name)); |
996b7376 TH |
919 | if (IS_ERR(new_dentry)) { |
920 | error = PTR_ERR(new_dentry); | |
921 | goto out_unlock; | |
1da177e4 | 922 | } |
996b7376 TH |
923 | |
924 | /* By allowing two different directories with the same | |
925 | * d_parent we allow this routine to move between different | |
926 | * shadows of the same directory | |
927 | */ | |
928 | error = -EINVAL; | |
51225039 | 929 | if (old_dentry->d_parent->d_inode != new_parent->d_inode || |
996b7376 | 930 | new_dentry->d_parent->d_inode != new_parent->d_inode || |
51225039 TH |
931 | old_dentry == new_dentry) |
932 | goto out_unlock; | |
996b7376 TH |
933 | |
934 | error = -EEXIST; | |
935 | if (new_dentry->d_inode) | |
51225039 | 936 | goto out_unlock; |
996b7376 | 937 | |
0c096b50 TH |
938 | /* rename kobject and sysfs_dirent */ |
939 | error = -ENOMEM; | |
940 | new_name = dup_name = kstrdup(new_name, GFP_KERNEL); | |
941 | if (!new_name) | |
942 | goto out_drop; | |
943 | ||
996b7376 TH |
944 | error = kobject_set_name(kobj, "%s", new_name); |
945 | if (error) | |
51225039 | 946 | goto out_drop; |
996b7376 | 947 | |
6cb52147 TH |
948 | mutex_lock(&sysfs_mutex); |
949 | ||
51225039 | 950 | dup_name = sd->s_name; |
0c096b50 TH |
951 | sd->s_name = new_name; |
952 | ||
953 | /* move under the new parent */ | |
996b7376 | 954 | d_add(new_dentry, NULL); |
608e266a | 955 | d_move(sd->s_dentry, new_dentry); |
996b7376 | 956 | |
0c73f18b | 957 | sysfs_unlink_sibling(sd); |
608e266a | 958 | sysfs_get(new_parent_sd); |
7f7cfffe | 959 | sysfs_put(sd->s_parent); |
608e266a | 960 | sd->s_parent = new_parent_sd; |
0c73f18b | 961 | sysfs_link_sibling(sd); |
996b7376 | 962 | |
3007e997 TH |
963 | mutex_unlock(&sysfs_mutex); |
964 | ||
996b7376 TH |
965 | error = 0; |
966 | goto out_unlock; | |
967 | ||
968 | out_drop: | |
969 | d_drop(new_dentry); | |
996b7376 | 970 | out_unlock: |
b592fcfe | 971 | mutex_unlock(&new_parent->d_inode->i_mutex); |
51225039 TH |
972 | out_dput: |
973 | kfree(dup_name); | |
974 | dput(new_parent); | |
975 | dput(old_dentry); | |
976 | dput(new_dentry); | |
1da177e4 LT |
977 | return error; |
978 | } | |
979 | ||
51225039 | 980 | int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent_kobj) |
8a82472f | 981 | { |
51225039 TH |
982 | struct sysfs_dirent *sd = kobj->sd; |
983 | struct sysfs_dirent *new_parent_sd; | |
984 | struct dentry *old_parent, *new_parent = NULL; | |
985 | struct dentry *old_dentry = NULL, *new_dentry = NULL; | |
8a82472f CH |
986 | int error; |
987 | ||
51225039 TH |
988 | BUG_ON(!sd->s_parent); |
989 | new_parent_sd = new_parent_kobj->sd ? new_parent_kobj->sd : &sysfs_root; | |
990 | ||
991 | /* get dentries */ | |
992 | old_dentry = sysfs_get_dentry(sd); | |
993 | if (IS_ERR(old_dentry)) { | |
994 | error = PTR_ERR(old_dentry); | |
995 | goto out_dput; | |
996 | } | |
997 | old_parent = sd->s_parent->s_dentry; | |
998 | ||
999 | new_parent = sysfs_get_dentry(new_parent_sd); | |
1000 | if (IS_ERR(new_parent)) { | |
1001 | error = PTR_ERR(new_parent); | |
1002 | goto out_dput; | |
1003 | } | |
8a82472f | 1004 | |
51225039 TH |
1005 | if (old_parent->d_inode == new_parent->d_inode) { |
1006 | error = 0; | |
1007 | goto out_dput; /* nothing to move */ | |
1008 | } | |
8a82472f | 1009 | again: |
51225039 TH |
1010 | mutex_lock(&old_parent->d_inode->i_mutex); |
1011 | if (!mutex_trylock(&new_parent->d_inode->i_mutex)) { | |
1012 | mutex_unlock(&old_parent->d_inode->i_mutex); | |
8a82472f CH |
1013 | goto again; |
1014 | } | |
1015 | ||
19c38de8 | 1016 | new_dentry = lookup_one_len(kobject_name(kobj), new_parent, strlen(kobject_name(kobj))); |
8a82472f CH |
1017 | if (IS_ERR(new_dentry)) { |
1018 | error = PTR_ERR(new_dentry); | |
51225039 | 1019 | goto out_unlock; |
8a82472f CH |
1020 | } else |
1021 | error = 0; | |
1022 | d_add(new_dentry, NULL); | |
608e266a | 1023 | d_move(sd->s_dentry, new_dentry); |
8a82472f CH |
1024 | dput(new_dentry); |
1025 | ||
1026 | /* Remove from old parent's list and insert into new parent's list. */ | |
3007e997 TH |
1027 | mutex_lock(&sysfs_mutex); |
1028 | ||
0c73f18b | 1029 | sysfs_unlink_sibling(sd); |
7f7cfffe TH |
1030 | sysfs_get(new_parent_sd); |
1031 | sysfs_put(sd->s_parent); | |
1032 | sd->s_parent = new_parent_sd; | |
0c73f18b | 1033 | sysfs_link_sibling(sd); |
8a82472f | 1034 | |
3007e997 | 1035 | mutex_unlock(&sysfs_mutex); |
8a82472f | 1036 | |
51225039 TH |
1037 | out_unlock: |
1038 | mutex_unlock(&new_parent->d_inode->i_mutex); | |
1039 | mutex_unlock(&old_parent->d_inode->i_mutex); | |
1040 | out_dput: | |
1041 | dput(new_parent); | |
1042 | dput(old_dentry); | |
1043 | dput(new_dentry); | |
8a82472f CH |
1044 | return error; |
1045 | } | |
1046 | ||
1da177e4 LT |
1047 | static int sysfs_dir_open(struct inode *inode, struct file *file) |
1048 | { | |
f427f5d5 | 1049 | struct dentry * dentry = file->f_path.dentry; |
1da177e4 | 1050 | struct sysfs_dirent * parent_sd = dentry->d_fsdata; |
a26cd722 | 1051 | struct sysfs_dirent * sd; |
1da177e4 | 1052 | |
3e519038 | 1053 | sd = sysfs_new_dirent("_DIR_", 0, 0); |
3007e997 TH |
1054 | if (sd) { |
1055 | mutex_lock(&sysfs_mutex); | |
fb6896da TH |
1056 | sd->s_parent = sysfs_get(parent_sd); |
1057 | sysfs_link_sibling(sd); | |
3007e997 TH |
1058 | mutex_unlock(&sysfs_mutex); |
1059 | } | |
1da177e4 | 1060 | |
a26cd722 TH |
1061 | file->private_data = sd; |
1062 | return sd ? 0 : -ENOMEM; | |
1da177e4 LT |
1063 | } |
1064 | ||
1065 | static int sysfs_dir_close(struct inode *inode, struct file *file) | |
1066 | { | |
1da177e4 LT |
1067 | struct sysfs_dirent * cursor = file->private_data; |
1068 | ||
3007e997 | 1069 | mutex_lock(&sysfs_mutex); |
0c73f18b | 1070 | sysfs_unlink_sibling(cursor); |
3007e997 | 1071 | mutex_unlock(&sysfs_mutex); |
1da177e4 LT |
1072 | |
1073 | release_sysfs_dirent(cursor); | |
1074 | ||
1075 | return 0; | |
1076 | } | |
1077 | ||
1078 | /* Relationship between s_mode and the DT_xxx types */ | |
1079 | static inline unsigned char dt_type(struct sysfs_dirent *sd) | |
1080 | { | |
1081 | return (sd->s_mode >> 12) & 15; | |
1082 | } | |
1083 | ||
1084 | static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir) | |
1085 | { | |
f427f5d5 | 1086 | struct dentry *dentry = filp->f_path.dentry; |
1da177e4 LT |
1087 | struct sysfs_dirent * parent_sd = dentry->d_fsdata; |
1088 | struct sysfs_dirent *cursor = filp->private_data; | |
0c73f18b | 1089 | struct sysfs_dirent **pos; |
1da177e4 LT |
1090 | ino_t ino; |
1091 | int i = filp->f_pos; | |
1092 | ||
1093 | switch (i) { | |
1094 | case 0: | |
dc351252 | 1095 | ino = parent_sd->s_ino; |
1da177e4 LT |
1096 | if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0) |
1097 | break; | |
1098 | filp->f_pos++; | |
1099 | i++; | |
1100 | /* fallthrough */ | |
1101 | case 1: | |
13b3086d TH |
1102 | if (parent_sd->s_parent) |
1103 | ino = parent_sd->s_parent->s_ino; | |
1104 | else | |
1105 | ino = parent_sd->s_ino; | |
1da177e4 LT |
1106 | if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0) |
1107 | break; | |
1108 | filp->f_pos++; | |
1109 | i++; | |
1110 | /* fallthrough */ | |
1111 | default: | |
3007e997 TH |
1112 | mutex_lock(&sysfs_mutex); |
1113 | ||
0c73f18b TH |
1114 | pos = &parent_sd->s_children; |
1115 | while (*pos != cursor) | |
1116 | pos = &(*pos)->s_sibling; | |
1117 | ||
1118 | /* unlink cursor */ | |
1119 | *pos = cursor->s_sibling; | |
1120 | ||
1bfba4e8 | 1121 | if (filp->f_pos == 2) |
0c73f18b | 1122 | pos = &parent_sd->s_children; |
1bfba4e8 | 1123 | |
0c73f18b TH |
1124 | for ( ; *pos; pos = &(*pos)->s_sibling) { |
1125 | struct sysfs_dirent *next = *pos; | |
1da177e4 LT |
1126 | const char * name; |
1127 | int len; | |
1128 | ||
b402d72c | 1129 | if (!sysfs_type(next)) |
1da177e4 LT |
1130 | continue; |
1131 | ||
0c096b50 | 1132 | name = next->s_name; |
1da177e4 | 1133 | len = strlen(name); |
dc351252 | 1134 | ino = next->s_ino; |
1da177e4 LT |
1135 | |
1136 | if (filldir(dirent, name, len, filp->f_pos, ino, | |
1137 | dt_type(next)) < 0) | |
0c73f18b | 1138 | break; |
1da177e4 | 1139 | |
1da177e4 LT |
1140 | filp->f_pos++; |
1141 | } | |
0c73f18b TH |
1142 | |
1143 | /* put cursor back in */ | |
1144 | cursor->s_sibling = *pos; | |
1145 | *pos = cursor; | |
3007e997 TH |
1146 | |
1147 | mutex_unlock(&sysfs_mutex); | |
1da177e4 LT |
1148 | } |
1149 | return 0; | |
1150 | } | |
1151 | ||
1152 | static loff_t sysfs_dir_lseek(struct file * file, loff_t offset, int origin) | |
1153 | { | |
f427f5d5 | 1154 | struct dentry * dentry = file->f_path.dentry; |
1da177e4 | 1155 | |
1da177e4 LT |
1156 | switch (origin) { |
1157 | case 1: | |
1158 | offset += file->f_pos; | |
1159 | case 0: | |
1160 | if (offset >= 0) | |
1161 | break; | |
1162 | default: | |
1da177e4 LT |
1163 | return -EINVAL; |
1164 | } | |
1165 | if (offset != file->f_pos) { | |
3007e997 TH |
1166 | mutex_lock(&sysfs_mutex); |
1167 | ||
1da177e4 LT |
1168 | file->f_pos = offset; |
1169 | if (file->f_pos >= 2) { | |
1170 | struct sysfs_dirent *sd = dentry->d_fsdata; | |
1171 | struct sysfs_dirent *cursor = file->private_data; | |
0c73f18b | 1172 | struct sysfs_dirent **pos; |
1da177e4 LT |
1173 | loff_t n = file->f_pos - 2; |
1174 | ||
0c73f18b TH |
1175 | sysfs_unlink_sibling(cursor); |
1176 | ||
1177 | pos = &sd->s_children; | |
1178 | while (n && *pos) { | |
1179 | struct sysfs_dirent *next = *pos; | |
b402d72c | 1180 | if (sysfs_type(next)) |
1da177e4 | 1181 | n--; |
0c73f18b | 1182 | pos = &(*pos)->s_sibling; |
1da177e4 | 1183 | } |
0c73f18b TH |
1184 | |
1185 | cursor->s_sibling = *pos; | |
1186 | *pos = cursor; | |
1da177e4 | 1187 | } |
3007e997 TH |
1188 | |
1189 | mutex_unlock(&sysfs_mutex); | |
1da177e4 | 1190 | } |
3007e997 | 1191 | |
1da177e4 LT |
1192 | return offset; |
1193 | } | |
1194 | ||
b592fcfe EB |
1195 | |
1196 | /** | |
1197 | * sysfs_make_shadowed_dir - Setup so a directory can be shadowed | |
1198 | * @kobj: object we're creating shadow of. | |
1199 | */ | |
1200 | ||
1201 | int sysfs_make_shadowed_dir(struct kobject *kobj, | |
1202 | void * (*follow_link)(struct dentry *, struct nameidata *)) | |
1203 | { | |
51225039 | 1204 | struct dentry *dentry; |
b592fcfe EB |
1205 | struct inode *inode; |
1206 | struct inode_operations *i_op; | |
1207 | ||
51225039 TH |
1208 | /* get dentry for @kobj->sd, dentry of a shadowed dir is pinned */ |
1209 | dentry = sysfs_get_dentry(kobj->sd); | |
1210 | if (IS_ERR(dentry)) | |
1211 | return PTR_ERR(dentry); | |
1212 | ||
1213 | inode = dentry->d_inode; | |
1214 | if (inode->i_op != &sysfs_dir_inode_operations) { | |
1215 | dput(dentry); | |
b592fcfe | 1216 | return -EINVAL; |
51225039 | 1217 | } |
b592fcfe EB |
1218 | |
1219 | i_op = kmalloc(sizeof(*i_op), GFP_KERNEL); | |
1220 | if (!i_op) | |
1221 | return -ENOMEM; | |
1222 | ||
1223 | memcpy(i_op, &sysfs_dir_inode_operations, sizeof(*i_op)); | |
1224 | i_op->follow_link = follow_link; | |
1225 | ||
1226 | /* Locking of inode->i_op? | |
1227 | * Since setting i_op is a single word write and they | |
1228 | * are atomic we should be ok here. | |
1229 | */ | |
1230 | inode->i_op = i_op; | |
1231 | return 0; | |
1232 | } | |
1233 | ||
1234 | /** | |
1235 | * sysfs_create_shadow_dir - create a shadow directory for an object. | |
1236 | * @kobj: object we're creating directory for. | |
1237 | * | |
1238 | * sysfs_make_shadowed_dir must already have been called on this | |
1239 | * directory. | |
1240 | */ | |
1241 | ||
608e266a | 1242 | struct sysfs_dirent *sysfs_create_shadow_dir(struct kobject *kobj) |
b592fcfe | 1243 | { |
51225039 TH |
1244 | struct sysfs_dirent *parent_sd = kobj->sd->s_parent; |
1245 | struct dentry *dir, *parent, *shadow; | |
1246 | struct inode *inode; | |
b592fcfe | 1247 | struct sysfs_dirent *sd; |
fb6896da | 1248 | struct sysfs_addrm_cxt acxt; |
b592fcfe | 1249 | |
51225039 TH |
1250 | dir = sysfs_get_dentry(kobj->sd); |
1251 | if (IS_ERR(dir)) { | |
1252 | sd = (void *)dir; | |
1253 | goto out; | |
1254 | } | |
1255 | parent = dir->d_parent; | |
1256 | ||
1257 | inode = dir->d_inode; | |
608e266a | 1258 | sd = ERR_PTR(-EINVAL); |
b592fcfe | 1259 | if (!sysfs_is_shadowed_inode(inode)) |
51225039 | 1260 | goto out_dput; |
b592fcfe EB |
1261 | |
1262 | shadow = d_alloc(parent, &dir->d_name); | |
1263 | if (!shadow) | |
1264 | goto nomem; | |
1265 | ||
3e519038 | 1266 | sd = sysfs_new_dirent("_SHADOW_", inode->i_mode, SYSFS_DIR); |
b592fcfe EB |
1267 | if (!sd) |
1268 | goto nomem; | |
3e519038 | 1269 | sd->s_elem.dir.kobj = kobj; |
b592fcfe | 1270 | |
fb6896da TH |
1271 | sysfs_addrm_start(&acxt, parent_sd); |
1272 | ||
1273 | /* add but don't link into children list */ | |
1274 | sysfs_add_one(&acxt, sd); | |
1275 | ||
1276 | /* attach and instantiate dentry */ | |
1277 | sysfs_attach_dentry(sd, shadow); | |
b592fcfe | 1278 | d_instantiate(shadow, igrab(inode)); |
fb6896da TH |
1279 | inc_nlink(inode); /* tj: synchronization? */ |
1280 | ||
1281 | sysfs_addrm_finish(&acxt); | |
b592fcfe EB |
1282 | |
1283 | dget(shadow); /* Extra count - pin the dentry in core */ | |
1284 | ||
51225039 TH |
1285 | goto out_dput; |
1286 | ||
1287 | nomem: | |
b592fcfe | 1288 | dput(shadow); |
608e266a | 1289 | sd = ERR_PTR(-ENOMEM); |
51225039 TH |
1290 | out_dput: |
1291 | dput(dir); | |
1292 | out: | |
1293 | return sd; | |
b592fcfe EB |
1294 | } |
1295 | ||
1296 | /** | |
1297 | * sysfs_remove_shadow_dir - remove an object's directory. | |
608e266a | 1298 | * @shadow_sd: sysfs_dirent of shadow directory |
b592fcfe EB |
1299 | * |
1300 | * The only thing special about this is that we remove any files in | |
1301 | * the directory before we remove the directory, and we've inlined | |
1302 | * what used to be sysfs_rmdir() below, instead of calling separately. | |
1303 | */ | |
1304 | ||
608e266a | 1305 | void sysfs_remove_shadow_dir(struct sysfs_dirent *shadow_sd) |
b592fcfe | 1306 | { |
608e266a | 1307 | __sysfs_remove_dir(shadow_sd); |
b592fcfe EB |
1308 | } |
1309 | ||
4b6f5d20 | 1310 | const struct file_operations sysfs_dir_operations = { |
1da177e4 LT |
1311 | .open = sysfs_dir_open, |
1312 | .release = sysfs_dir_close, | |
1313 | .llseek = sysfs_dir_lseek, | |
1314 | .read = generic_read_dir, | |
1315 | .readdir = sysfs_readdir, | |
1316 | }; |