]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/sysfs/dir.c
sysfs: Make sysfs_mount static
[mirror_ubuntu-bionic-kernel.git] / fs / sysfs / dir.c
CommitLineData
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 17DEFINE_MUTEX(sysfs_mutex);
5f995323 18spinlock_t sysfs_assoc_lock = SPIN_LOCK_UNLOCKED;
1da177e4 19
2b611bb7
TH
20static spinlock_t sysfs_ino_lock = SPIN_LOCK_UNLOCKED;
21static 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 */
41fc1c27 33static 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 */
41fc1c27 52static 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 */
80struct 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;
25328026 133 mutex_lock(&parent_dentry->d_inode->i_mutex);
53e0ae92
TH
134 dentry = lookup_one_len_kern(cur->s_name, parent_dentry,
135 strlen(cur->s_name));
25328026 136 mutex_unlock(&parent_dentry->d_inode->i_mutex);
53e0ae92
TH
137 dput(parent_dentry);
138
139 if (IS_ERR(dentry)) {
140 sysfs_put(cur);
141 return dentry;
142 }
143
144 mutex_lock(&sysfs_mutex);
145 spin_lock(&sysfs_assoc_lock);
146
147 /* This, again, can happen if tree structure has
148 * changed and we looked up the wrong thing. Restart.
149 */
150 if (cur->s_dentry != dentry) {
151 dput(dentry);
152 sysfs_put(cur);
153 goto restart1;
154 }
155
156 spin_unlock(&sysfs_assoc_lock);
157
158 sysfs_put(cur);
159 }
160
161 mutex_unlock(&sysfs_mutex);
162 return dentry;
163}
164
b6b4a439
TH
165/**
166 * sysfs_get_active - get an active reference to sysfs_dirent
167 * @sd: sysfs_dirent to get an active reference to
168 *
169 * Get an active reference of @sd. This function is noop if @sd
170 * is NULL.
171 *
172 * RETURNS:
173 * Pointer to @sd on success, NULL on failure.
174 */
175struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd)
176{
8619f979
TH
177 if (unlikely(!sd))
178 return NULL;
179
180 while (1) {
181 int v, t;
182
183 v = atomic_read(&sd->s_active);
184 if (unlikely(v < 0))
185 return NULL;
186
187 t = atomic_cmpxchg(&sd->s_active, v, v + 1);
188 if (likely(t == v))
189 return sd;
190 if (t < 0)
191 return NULL;
192
193 cpu_relax();
b6b4a439 194 }
b6b4a439
TH
195}
196
197/**
198 * sysfs_put_active - put an active reference to sysfs_dirent
199 * @sd: sysfs_dirent to put an active reference to
200 *
201 * Put an active reference to @sd. This function is noop if @sd
202 * is NULL.
203 */
204void sysfs_put_active(struct sysfs_dirent *sd)
205{
8619f979
TH
206 struct completion *cmpl;
207 int v;
208
209 if (unlikely(!sd))
210 return;
211
212 v = atomic_dec_return(&sd->s_active);
213 if (likely(v != SD_DEACTIVATED_BIAS))
214 return;
215
216 /* atomic_dec_return() is a mb(), we'll always see the updated
0c73f18b 217 * sd->s_sibling.
8619f979 218 */
0c73f18b 219 cmpl = (void *)sd->s_sibling;
8619f979 220 complete(cmpl);
b6b4a439
TH
221}
222
223/**
224 * sysfs_get_active_two - get active references to sysfs_dirent and parent
225 * @sd: sysfs_dirent of interest
226 *
227 * Get active reference to @sd and its parent. Parent's active
228 * reference is grabbed first. This function is noop if @sd is
229 * NULL.
230 *
231 * RETURNS:
232 * Pointer to @sd on success, NULL on failure.
233 */
234struct sysfs_dirent *sysfs_get_active_two(struct sysfs_dirent *sd)
235{
236 if (sd) {
237 if (sd->s_parent && unlikely(!sysfs_get_active(sd->s_parent)))
238 return NULL;
239 if (unlikely(!sysfs_get_active(sd))) {
240 sysfs_put_active(sd->s_parent);
241 return NULL;
242 }
243 }
244 return sd;
245}
246
247/**
248 * sysfs_put_active_two - put active references to sysfs_dirent and parent
249 * @sd: sysfs_dirent of interest
250 *
251 * Put active references to @sd and its parent. This function is
252 * noop if @sd is NULL.
253 */
254void sysfs_put_active_two(struct sysfs_dirent *sd)
255{
256 if (sd) {
257 sysfs_put_active(sd);
258 sysfs_put_active(sd->s_parent);
259 }
260}
261
262/**
263 * sysfs_deactivate - deactivate sysfs_dirent
264 * @sd: sysfs_dirent to deactivate
265 *
8619f979 266 * Deny new active references and drain existing ones.
b6b4a439 267 */
fb6896da 268static void sysfs_deactivate(struct sysfs_dirent *sd)
b6b4a439 269{
8619f979
TH
270 DECLARE_COMPLETION_ONSTACK(wait);
271 int v;
b6b4a439 272
380e6fbb 273 BUG_ON(sd->s_sibling || !(sd->s_flags & SYSFS_FLAG_REMOVED));
0c73f18b 274 sd->s_sibling = (void *)&wait;
8619f979
TH
275
276 /* atomic_add_return() is a mb(), put_active() will always see
0c73f18b 277 * the updated sd->s_sibling.
b6b4a439 278 */
8619f979
TH
279 v = atomic_add_return(SD_DEACTIVATED_BIAS, &sd->s_active);
280
281 if (v != SD_DEACTIVATED_BIAS)
282 wait_for_completion(&wait);
283
0c73f18b 284 sd->s_sibling = NULL;
b6b4a439
TH
285}
286
42b37df6 287static int sysfs_alloc_ino(ino_t *pino)
2b611bb7
TH
288{
289 int ino, rc;
290
291 retry:
292 spin_lock(&sysfs_ino_lock);
293 rc = ida_get_new_above(&sysfs_ino_ida, 2, &ino);
294 spin_unlock(&sysfs_ino_lock);
295
296 if (rc == -EAGAIN) {
297 if (ida_pre_get(&sysfs_ino_ida, GFP_KERNEL))
298 goto retry;
299 rc = -ENOMEM;
300 }
301
302 *pino = ino;
303 return rc;
304}
305
306static void sysfs_free_ino(ino_t ino)
307{
308 spin_lock(&sysfs_ino_lock);
309 ida_remove(&sysfs_ino_ida, ino);
310 spin_unlock(&sysfs_ino_lock);
311}
312
fa7f912a
TH
313void release_sysfs_dirent(struct sysfs_dirent * sd)
314{
13b3086d
TH
315 struct sysfs_dirent *parent_sd;
316
317 repeat:
3007e997
TH
318 /* Moving/renaming is always done while holding reference.
319 * sd->s_parent won't change beneath us.
320 */
13b3086d
TH
321 parent_sd = sd->s_parent;
322
b402d72c 323 if (sysfs_type(sd) == SYSFS_KOBJ_LINK)
2b29ac25 324 sysfs_put(sd->s_elem.symlink.target_sd);
b402d72c 325 if (sysfs_type(sd) & SYSFS_COPY_NAME)
0c096b50 326 kfree(sd->s_name);
fa7f912a 327 kfree(sd->s_iattr);
2b611bb7 328 sysfs_free_ino(sd->s_ino);
fa7f912a 329 kmem_cache_free(sysfs_dir_cachep, sd);
13b3086d
TH
330
331 sd = parent_sd;
332 if (sd && atomic_dec_and_test(&sd->s_count))
333 goto repeat;
fa7f912a
TH
334}
335
1da177e4
LT
336static void sysfs_d_iput(struct dentry * dentry, struct inode * inode)
337{
338 struct sysfs_dirent * sd = dentry->d_fsdata;
339
340 if (sd) {
5f995323
TH
341 /* sd->s_dentry is protected with sysfs_assoc_lock.
342 * This allows sysfs_drop_dentry() to dereference it.
dd14cbc9 343 */
5f995323 344 spin_lock(&sysfs_assoc_lock);
dd14cbc9
TH
345
346 /* The dentry might have been deleted or another
347 * lookup could have happened updating sd->s_dentry to
348 * point the new dentry. Ignore if it isn't pointing
349 * to this dentry.
350 */
351 if (sd->s_dentry == dentry)
352 sd->s_dentry = NULL;
5f995323 353 spin_unlock(&sysfs_assoc_lock);
1da177e4
LT
354 sysfs_put(sd);
355 }
356 iput(inode);
357}
358
359static struct dentry_operations sysfs_dentry_ops = {
360 .d_iput = sysfs_d_iput,
361};
362
3e519038 363struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type)
1da177e4 364{
0c096b50 365 char *dup_name = NULL;
01da2425 366 struct sysfs_dirent *sd;
0c096b50
TH
367
368 if (type & SYSFS_COPY_NAME) {
369 name = dup_name = kstrdup(name, GFP_KERNEL);
370 if (!name)
01da2425 371 return NULL;
0c096b50 372 }
1da177e4 373
c3762229 374 sd = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL);
1da177e4 375 if (!sd)
01da2425 376 goto err_out1;
1da177e4 377
0c096b50 378 if (sysfs_alloc_ino(&sd->s_ino))
01da2425 379 goto err_out2;
2b611bb7 380
1da177e4 381 atomic_set(&sd->s_count, 1);
8619f979 382 atomic_set(&sd->s_active, 0);
eea3f891 383 atomic_set(&sd->s_event, 1);
a26cd722 384
0c096b50 385 sd->s_name = name;
a26cd722 386 sd->s_mode = mode;
b402d72c 387 sd->s_flags = type;
1da177e4
LT
388
389 return sd;
0c096b50 390
01da2425 391 err_out2:
0c096b50 392 kmem_cache_free(sysfs_dir_cachep, sd);
01da2425
AM
393 err_out1:
394 kfree(dup_name);
0c096b50 395 return NULL;
1da177e4
LT
396}
397
3007e997
TH
398/**
399 * sysfs_attach_dentry - associate sysfs_dirent with dentry
400 * @sd: target sysfs_dirent
401 * @dentry: dentry to associate
402 *
403 * Associate @sd with @dentry. This is protected by
404 * sysfs_assoc_lock to avoid race with sysfs_d_iput().
405 *
406 * LOCKING:
407 * mutex_lock(sysfs_mutex)
408 */
198a2a84
TH
409static void sysfs_attach_dentry(struct sysfs_dirent *sd, struct dentry *dentry)
410{
411 dentry->d_op = &sysfs_dentry_ops;
412 dentry->d_fsdata = sysfs_get(sd);
413
414 /* protect sd->s_dentry against sysfs_d_iput */
5f995323 415 spin_lock(&sysfs_assoc_lock);
198a2a84 416 sd->s_dentry = dentry;
5f995323 417 spin_unlock(&sysfs_assoc_lock);
198a2a84
TH
418
419 d_rehash(dentry);
420}
421
fb6896da
TH
422static int sysfs_ilookup_test(struct inode *inode, void *arg)
423{
424 struct sysfs_dirent *sd = arg;
425 return inode->i_ino == sd->s_ino;
426}
427
3007e997 428/**
fb6896da
TH
429 * sysfs_addrm_start - prepare for sysfs_dirent add/remove
430 * @acxt: pointer to sysfs_addrm_cxt to be used
431 * @parent_sd: parent sysfs_dirent
3007e997 432 *
fb6896da
TH
433 * This function is called when the caller is about to add or
434 * remove sysfs_dirent under @parent_sd. This function acquires
435 * sysfs_mutex, grabs inode for @parent_sd if available and lock
436 * i_mutex of it. @acxt is used to keep and pass context to
437 * other addrm functions.
3007e997
TH
438 *
439 * LOCKING:
fb6896da
TH
440 * Kernel thread context (may sleep). sysfs_mutex is locked on
441 * return. i_mutex of parent inode is locked on return if
442 * available.
3007e997 443 */
fb6896da
TH
444void sysfs_addrm_start(struct sysfs_addrm_cxt *acxt,
445 struct sysfs_dirent *parent_sd)
b592fcfe 446{
fb6896da 447 struct inode *inode;
b592fcfe 448
fb6896da
TH
449 memset(acxt, 0, sizeof(*acxt));
450 acxt->parent_sd = parent_sd;
451
452 /* Lookup parent inode. inode initialization and I_NEW
453 * clearing are protected by sysfs_mutex. By grabbing it and
454 * looking up with _nowait variant, inode state can be
455 * determined reliably.
456 */
457 mutex_lock(&sysfs_mutex);
458
459 inode = ilookup5_nowait(sysfs_sb, parent_sd->s_ino, sysfs_ilookup_test,
460 parent_sd);
461
462 if (inode && !(inode->i_state & I_NEW)) {
463 /* parent inode available */
464 acxt->parent_inode = inode;
465
466 /* sysfs_mutex is below i_mutex in lock hierarchy.
467 * First, trylock i_mutex. If fails, unlock
468 * sysfs_mutex and lock them in order.
469 */
470 if (!mutex_trylock(&inode->i_mutex)) {
471 mutex_unlock(&sysfs_mutex);
472 mutex_lock(&inode->i_mutex);
473 mutex_lock(&sysfs_mutex);
474 }
475 } else
476 iput(inode);
477}
478
479/**
480 * sysfs_add_one - add sysfs_dirent to parent
481 * @acxt: addrm context to use
482 * @sd: sysfs_dirent to be added
483 *
484 * Get @acxt->parent_sd and set sd->s_parent to it and increment
485 * nlink of parent inode if @sd is a directory. @sd is NOT
486 * linked into the children list of the parent. The caller
487 * should invoke sysfs_link_sibling() after this function
488 * completes if @sd needs to be on the children list.
489 *
490 * This function should be called between calls to
491 * sysfs_addrm_start() and sysfs_addrm_finish() and should be
492 * passed the same @acxt as passed to sysfs_addrm_start().
493 *
494 * LOCKING:
495 * Determined by sysfs_addrm_start().
23dc2799
TH
496 *
497 * RETURNS:
498 * 0 on success, -EEXIST if entry with the given name already
499 * exists.
fb6896da 500 */
23dc2799 501int sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
fb6896da 502{
23dc2799
TH
503 if (sysfs_find_dirent(acxt->parent_sd, sd->s_name))
504 return -EEXIST;
505
fb6896da
TH
506 sd->s_parent = sysfs_get(acxt->parent_sd);
507
508 if (sysfs_type(sd) == SYSFS_DIR && acxt->parent_inode)
509 inc_nlink(acxt->parent_inode);
510
511 acxt->cnt++;
41fc1c27
TH
512
513 sysfs_link_sibling(sd);
23dc2799
TH
514
515 return 0;
fb6896da
TH
516}
517
518/**
519 * sysfs_remove_one - remove sysfs_dirent from parent
520 * @acxt: addrm context to use
521 * @sd: sysfs_dirent to be added
522 *
523 * Mark @sd removed and drop nlink of parent inode if @sd is a
524 * directory. @sd is NOT unlinked from the children list of the
525 * parent. The caller is repsonsible for removing @sd from the
526 * children list before calling this function.
527 *
528 * This function should be called between calls to
529 * sysfs_addrm_start() and sysfs_addrm_finish() and should be
530 * passed the same @acxt as passed to sysfs_addrm_start().
531 *
532 * LOCKING:
533 * Determined by sysfs_addrm_start().
534 */
535void sysfs_remove_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
536{
41fc1c27
TH
537 BUG_ON(sd->s_flags & SYSFS_FLAG_REMOVED);
538
539 sysfs_unlink_sibling(sd);
fb6896da
TH
540
541 sd->s_flags |= SYSFS_FLAG_REMOVED;
542 sd->s_sibling = acxt->removed;
543 acxt->removed = sd;
544
545 if (sysfs_type(sd) == SYSFS_DIR && acxt->parent_inode)
546 drop_nlink(acxt->parent_inode);
547
548 acxt->cnt++;
549}
550
a0edd7c8
TH
551/**
552 * sysfs_drop_dentry - drop dentry for the specified sysfs_dirent
553 * @sd: target sysfs_dirent
554 *
555 * Drop dentry for @sd. @sd must have been unlinked from its
556 * parent on entry to this function such that it can't be looked
557 * up anymore.
558 *
559 * @sd->s_dentry which is protected with sysfs_assoc_lock points
560 * to the currently associated dentry but we're not holding a
561 * reference to it and racing with dput(). Grab dcache_lock and
562 * verify dentry before dropping it. If @sd->s_dentry is NULL or
563 * dput() beats us, no need to bother.
564 */
565static void sysfs_drop_dentry(struct sysfs_dirent *sd)
566{
567 struct dentry *dentry = NULL;
568 struct inode *inode;
569
570 /* We're not holding a reference to ->s_dentry dentry but the
571 * field will stay valid as long as sysfs_assoc_lock is held.
572 */
573 spin_lock(&sysfs_assoc_lock);
574 spin_lock(&dcache_lock);
575
576 /* drop dentry if it's there and dput() didn't kill it yet */
577 if (sd->s_dentry && sd->s_dentry->d_inode) {
578 dentry = dget_locked(sd->s_dentry);
579 spin_lock(&dentry->d_lock);
580 __d_drop(dentry);
581 spin_unlock(&dentry->d_lock);
582 }
583
584 spin_unlock(&dcache_lock);
585 spin_unlock(&sysfs_assoc_lock);
586
51225039 587 dput(dentry);
a0edd7c8
TH
588
589 /* adjust nlink and update timestamp */
590 inode = ilookup(sysfs_sb, sd->s_ino);
591 if (inode) {
592 mutex_lock(&inode->i_mutex);
593
594 inode->i_ctime = CURRENT_TIME;
595 drop_nlink(inode);
596 if (sysfs_type(sd) == SYSFS_DIR)
597 drop_nlink(inode);
598
599 mutex_unlock(&inode->i_mutex);
600 iput(inode);
601 }
602}
603
fb6896da
TH
604/**
605 * sysfs_addrm_finish - finish up sysfs_dirent add/remove
606 * @acxt: addrm context to finish up
607 *
608 * Finish up sysfs_dirent add/remove. Resources acquired by
609 * sysfs_addrm_start() are released and removed sysfs_dirents are
610 * cleaned up. Timestamps on the parent inode are updated.
611 *
612 * LOCKING:
613 * All mutexes acquired by sysfs_addrm_start() are released.
fb6896da 614 */
990e53f8 615void sysfs_addrm_finish(struct sysfs_addrm_cxt *acxt)
fb6896da
TH
616{
617 /* release resources acquired by sysfs_addrm_start() */
618 mutex_unlock(&sysfs_mutex);
619 if (acxt->parent_inode) {
620 struct inode *inode = acxt->parent_inode;
621
622 /* if added/removed, update timestamps on the parent */
623 if (acxt->cnt)
624 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
625
626 mutex_unlock(&inode->i_mutex);
627 iput(inode);
628 }
629
630 /* kill removed sysfs_dirents */
631 while (acxt->removed) {
632 struct sysfs_dirent *sd = acxt->removed;
633
634 acxt->removed = sd->s_sibling;
635 sd->s_sibling = NULL;
636
637 sysfs_drop_dentry(sd);
638 sysfs_deactivate(sd);
639 sysfs_put(sd);
13b3086d 640 }
b592fcfe
EB
641}
642
f0b0af47
TH
643/**
644 * sysfs_find_dirent - find sysfs_dirent with the given name
645 * @parent_sd: sysfs_dirent to search under
646 * @name: name to look for
647 *
648 * Look for sysfs_dirent with name @name under @parent_sd.
c516865c 649 *
f0b0af47 650 * LOCKING:
3007e997 651 * mutex_lock(sysfs_mutex)
c516865c 652 *
f0b0af47
TH
653 * RETURNS:
654 * Pointer to sysfs_dirent if found, NULL if not.
c516865c 655 */
f0b0af47
TH
656struct sysfs_dirent *sysfs_find_dirent(struct sysfs_dirent *parent_sd,
657 const unsigned char *name)
c516865c 658{
f0b0af47
TH
659 struct sysfs_dirent *sd;
660
661 for (sd = parent_sd->s_children; sd; sd = sd->s_sibling)
662 if (sysfs_type(sd) && !strcmp(sd->s_name, name))
663 return sd;
664 return NULL;
665}
c516865c 666
f0b0af47
TH
667/**
668 * sysfs_get_dirent - find and get sysfs_dirent with the given name
669 * @parent_sd: sysfs_dirent to search under
670 * @name: name to look for
671 *
672 * Look for sysfs_dirent with name @name under @parent_sd and get
673 * it if found.
674 *
675 * LOCKING:
3007e997 676 * Kernel thread context (may sleep). Grabs sysfs_mutex.
f0b0af47
TH
677 *
678 * RETURNS:
679 * Pointer to sysfs_dirent if found, NULL if not.
680 */
681struct sysfs_dirent *sysfs_get_dirent(struct sysfs_dirent *parent_sd,
682 const unsigned char *name)
683{
684 struct sysfs_dirent *sd;
685
3007e997 686 mutex_lock(&sysfs_mutex);
f0b0af47
TH
687 sd = sysfs_find_dirent(parent_sd, name);
688 sysfs_get(sd);
3007e997 689 mutex_unlock(&sysfs_mutex);
f0b0af47
TH
690
691 return sd;
c516865c
MS
692}
693
608e266a
TH
694static int create_dir(struct kobject *kobj, struct sysfs_dirent *parent_sd,
695 const char *name, struct sysfs_dirent **p_sd)
1da177e4 696{
1da177e4 697 umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
51225039 698 struct sysfs_addrm_cxt acxt;
dfeb9fb0 699 struct sysfs_dirent *sd;
23dc2799 700 int rc;
1da177e4 701
fc9f54b9 702 /* allocate */
3e519038 703 sd = sysfs_new_dirent(name, mode, SYSFS_DIR);
a26cd722 704 if (!sd)
51225039 705 return -ENOMEM;
3e519038 706 sd->s_elem.dir.kobj = kobj;
dfeb9fb0 707
fc9f54b9 708 /* link in */
51225039 709 sysfs_addrm_start(&acxt, parent_sd);
23dc2799
TH
710 rc = sysfs_add_one(&acxt, sd);
711 sysfs_addrm_finish(&acxt);
967e35dc 712
23dc2799
TH
713 if (rc == 0)
714 *p_sd = sd;
715 else
967e35dc 716 sysfs_put(sd);
dfeb9fb0 717
23dc2799 718 return rc;
1da177e4
LT
719}
720
608e266a
TH
721int sysfs_create_subdir(struct kobject *kobj, const char *name,
722 struct sysfs_dirent **p_sd)
1da177e4 723{
608e266a 724 return create_dir(kobj, kobj->sd, name, p_sd);
1da177e4
LT
725}
726
727/**
728 * sysfs_create_dir - create a directory for an object.
1da177e4
LT
729 * @kobj: object we're creating directory for.
730 */
90bc6135 731int sysfs_create_dir(struct kobject * kobj)
1da177e4 732{
608e266a 733 struct sysfs_dirent *parent_sd, *sd;
1da177e4
LT
734 int error = 0;
735
736 BUG_ON(!kobj);
737
90bc6135 738 if (kobj->parent)
608e266a 739 parent_sd = kobj->parent->sd;
1da177e4 740 else
7d0c7d67 741 parent_sd = &sysfs_root;
1da177e4 742
608e266a 743 error = create_dir(kobj, parent_sd, kobject_name(kobj), &sd);
1da177e4 744 if (!error)
608e266a 745 kobj->sd = sd;
1da177e4
LT
746 return error;
747}
748
1da177e4
LT
749static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
750 struct nameidata *nd)
751{
6cb52147 752 struct dentry *ret = NULL;
a7a04754
TH
753 struct sysfs_dirent *parent_sd = dentry->d_parent->d_fsdata;
754 struct sysfs_dirent *sd;
fc9f54b9 755 struct inode *inode;
1da177e4 756
6cb52147
TH
757 mutex_lock(&sysfs_mutex);
758
a7a04754
TH
759 for (sd = parent_sd->s_children; sd; sd = sd->s_sibling)
760 if (sysfs_type(sd) && !strcmp(sd->s_name, dentry->d_name.name))
1da177e4 761 break;
1da177e4 762
fc9f54b9 763 /* no such entry */
a7a04754 764 if (!sd)
6cb52147 765 goto out_unlock;
fc9f54b9
TH
766
767 /* attach dentry and inode */
8312a8d7 768 inode = sysfs_get_inode(sd);
6cb52147
TH
769 if (!inode) {
770 ret = ERR_PTR(-ENOMEM);
771 goto out_unlock;
772 }
3007e997 773
119dd52b 774 d_instantiate(dentry, inode);
fc9f54b9
TH
775 sysfs_attach_dentry(sd, dentry);
776
6cb52147 777 out_unlock:
3007e997 778 mutex_unlock(&sysfs_mutex);
6cb52147 779 return ret;
1da177e4
LT
780}
781
c5ef1c42 782const struct inode_operations sysfs_dir_inode_operations = {
1da177e4 783 .lookup = sysfs_lookup,
988d186d 784 .setattr = sysfs_setattr,
1da177e4
LT
785};
786
608e266a 787static void remove_dir(struct sysfs_dirent *sd)
1da177e4 788{
fb6896da 789 struct sysfs_addrm_cxt acxt;
1da177e4 790
fb6896da 791 sysfs_addrm_start(&acxt, sd->s_parent);
fb6896da
TH
792 sysfs_remove_one(&acxt, sd);
793 sysfs_addrm_finish(&acxt);
1da177e4
LT
794}
795
608e266a 796void sysfs_remove_subdir(struct sysfs_dirent *sd)
1da177e4 797{
608e266a 798 remove_dir(sd);
1da177e4
LT
799}
800
801
608e266a 802static void __sysfs_remove_dir(struct sysfs_dirent *dir_sd)
1da177e4 803{
fb6896da 804 struct sysfs_addrm_cxt acxt;
0c73f18b 805 struct sysfs_dirent **pos;
1da177e4 806
608e266a 807 if (!dir_sd)
1da177e4
LT
808 return;
809
608e266a 810 pr_debug("sysfs %s: removing dir\n", dir_sd->s_name);
fb6896da 811 sysfs_addrm_start(&acxt, dir_sd);
608e266a 812 pos = &dir_sd->s_children;
0c73f18b
TH
813 while (*pos) {
814 struct sysfs_dirent *sd = *pos;
815
41fc1c27 816 if (sysfs_type(sd) && sysfs_type(sd) != SYSFS_DIR)
fb6896da 817 sysfs_remove_one(&acxt, sd);
41fc1c27 818 else
0c73f18b 819 pos = &(*pos)->s_sibling;
1da177e4 820 }
fb6896da 821 sysfs_addrm_finish(&acxt);
0ab66088 822
608e266a 823 remove_dir(dir_sd);
b592fcfe
EB
824}
825
826/**
827 * sysfs_remove_dir - remove an object's directory.
828 * @kobj: object.
829 *
830 * The only thing special about this is that we remove any files in
831 * the directory before we remove the directory, and we've inlined
832 * what used to be sysfs_rmdir() below, instead of calling separately.
833 */
834
835void sysfs_remove_dir(struct kobject * kobj)
836{
608e266a 837 struct sysfs_dirent *sd = kobj->sd;
aecdceda 838
5f995323 839 spin_lock(&sysfs_assoc_lock);
608e266a 840 kobj->sd = NULL;
5f995323 841 spin_unlock(&sysfs_assoc_lock);
aecdceda 842
608e266a 843 __sysfs_remove_dir(sd);
1da177e4
LT
844}
845
90bc6135 846int sysfs_rename_dir(struct kobject * kobj, const char *new_name)
1da177e4 847{
90bc6135
EB
848 struct sysfs_dirent *sd;
849 struct dentry *parent = NULL;
51225039
TH
850 struct dentry *old_dentry = NULL, *new_dentry = NULL;
851 const char *dup_name = NULL;
996b7376 852 int error;
1da177e4 853
ff869de7 854 /* get the original dentry */
90bc6135 855 sd = kobj->sd;
51225039
TH
856 old_dentry = sysfs_get_dentry(sd);
857 if (IS_ERR(old_dentry)) {
858 error = PTR_ERR(old_dentry);
859 goto out_dput;
860 }
861
ff869de7 862 parent = old_dentry->d_parent;
1da177e4 863
90bc6135
EB
864 /* lock parent and get dentry for new name */
865 mutex_lock(&parent->d_inode->i_mutex);
1da177e4 866
90bc6135 867 new_dentry = lookup_one_len(new_name, parent, strlen(new_name));
996b7376
TH
868 if (IS_ERR(new_dentry)) {
869 error = PTR_ERR(new_dentry);
870 goto out_unlock;
1da177e4 871 }
996b7376 872
996b7376 873 error = -EINVAL;
90bc6135 874 if (old_dentry == new_dentry)
51225039 875 goto out_unlock;
996b7376
TH
876
877 error = -EEXIST;
878 if (new_dentry->d_inode)
51225039 879 goto out_unlock;
996b7376 880
0c096b50
TH
881 /* rename kobject and sysfs_dirent */
882 error = -ENOMEM;
883 new_name = dup_name = kstrdup(new_name, GFP_KERNEL);
884 if (!new_name)
885 goto out_drop;
886
996b7376
TH
887 error = kobject_set_name(kobj, "%s", new_name);
888 if (error)
51225039 889 goto out_drop;
996b7376 890
6cb52147 891 mutex_lock(&sysfs_mutex);
51225039 892 dup_name = sd->s_name;
0c096b50 893 sd->s_name = new_name;
ff869de7 894 mutex_unlock(&sysfs_mutex);
0c096b50 895
ff869de7 896 /* rename */
996b7376 897 d_add(new_dentry, NULL);
608e266a 898 d_move(sd->s_dentry, new_dentry);
996b7376 899
996b7376
TH
900 error = 0;
901 goto out_unlock;
902
903 out_drop:
904 d_drop(new_dentry);
996b7376 905 out_unlock:
90bc6135 906 mutex_unlock(&parent->d_inode->i_mutex);
51225039
TH
907 out_dput:
908 kfree(dup_name);
51225039
TH
909 dput(old_dentry);
910 dput(new_dentry);
1da177e4
LT
911 return error;
912}
913
51225039 914int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent_kobj)
8a82472f 915{
51225039
TH
916 struct sysfs_dirent *sd = kobj->sd;
917 struct sysfs_dirent *new_parent_sd;
918 struct dentry *old_parent, *new_parent = NULL;
919 struct dentry *old_dentry = NULL, *new_dentry = NULL;
8a82472f
CH
920 int error;
921
51225039
TH
922 BUG_ON(!sd->s_parent);
923 new_parent_sd = new_parent_kobj->sd ? new_parent_kobj->sd : &sysfs_root;
924
925 /* get dentries */
926 old_dentry = sysfs_get_dentry(sd);
927 if (IS_ERR(old_dentry)) {
928 error = PTR_ERR(old_dentry);
929 goto out_dput;
930 }
931 old_parent = sd->s_parent->s_dentry;
932
933 new_parent = sysfs_get_dentry(new_parent_sd);
934 if (IS_ERR(new_parent)) {
935 error = PTR_ERR(new_parent);
936 goto out_dput;
937 }
8a82472f 938
51225039
TH
939 if (old_parent->d_inode == new_parent->d_inode) {
940 error = 0;
941 goto out_dput; /* nothing to move */
942 }
8a82472f 943again:
51225039
TH
944 mutex_lock(&old_parent->d_inode->i_mutex);
945 if (!mutex_trylock(&new_parent->d_inode->i_mutex)) {
946 mutex_unlock(&old_parent->d_inode->i_mutex);
8a82472f
CH
947 goto again;
948 }
949
19c38de8 950 new_dentry = lookup_one_len(kobject_name(kobj), new_parent, strlen(kobject_name(kobj)));
8a82472f
CH
951 if (IS_ERR(new_dentry)) {
952 error = PTR_ERR(new_dentry);
51225039 953 goto out_unlock;
8a82472f
CH
954 } else
955 error = 0;
956 d_add(new_dentry, NULL);
608e266a 957 d_move(sd->s_dentry, new_dentry);
8a82472f
CH
958 dput(new_dentry);
959
960 /* Remove from old parent's list and insert into new parent's list. */
3007e997
TH
961 mutex_lock(&sysfs_mutex);
962
0c73f18b 963 sysfs_unlink_sibling(sd);
7f7cfffe
TH
964 sysfs_get(new_parent_sd);
965 sysfs_put(sd->s_parent);
966 sd->s_parent = new_parent_sd;
0c73f18b 967 sysfs_link_sibling(sd);
8a82472f 968
3007e997 969 mutex_unlock(&sysfs_mutex);
8a82472f 970
51225039
TH
971 out_unlock:
972 mutex_unlock(&new_parent->d_inode->i_mutex);
973 mutex_unlock(&old_parent->d_inode->i_mutex);
974 out_dput:
975 dput(new_parent);
976 dput(old_dentry);
977 dput(new_dentry);
8a82472f
CH
978 return error;
979}
980
1da177e4
LT
981static int sysfs_dir_open(struct inode *inode, struct file *file)
982{
f427f5d5 983 struct dentry * dentry = file->f_path.dentry;
1da177e4 984 struct sysfs_dirent * parent_sd = dentry->d_fsdata;
a26cd722 985 struct sysfs_dirent * sd;
1da177e4 986
3e519038 987 sd = sysfs_new_dirent("_DIR_", 0, 0);
3007e997
TH
988 if (sd) {
989 mutex_lock(&sysfs_mutex);
fb6896da
TH
990 sd->s_parent = sysfs_get(parent_sd);
991 sysfs_link_sibling(sd);
3007e997
TH
992 mutex_unlock(&sysfs_mutex);
993 }
1da177e4 994
a26cd722
TH
995 file->private_data = sd;
996 return sd ? 0 : -ENOMEM;
1da177e4
LT
997}
998
999static int sysfs_dir_close(struct inode *inode, struct file *file)
1000{
1da177e4
LT
1001 struct sysfs_dirent * cursor = file->private_data;
1002
3007e997 1003 mutex_lock(&sysfs_mutex);
0c73f18b 1004 sysfs_unlink_sibling(cursor);
3007e997 1005 mutex_unlock(&sysfs_mutex);
1da177e4
LT
1006
1007 release_sysfs_dirent(cursor);
1008
1009 return 0;
1010}
1011
1012/* Relationship between s_mode and the DT_xxx types */
1013static inline unsigned char dt_type(struct sysfs_dirent *sd)
1014{
1015 return (sd->s_mode >> 12) & 15;
1016}
1017
1018static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
1019{
f427f5d5 1020 struct dentry *dentry = filp->f_path.dentry;
1da177e4
LT
1021 struct sysfs_dirent * parent_sd = dentry->d_fsdata;
1022 struct sysfs_dirent *cursor = filp->private_data;
0c73f18b 1023 struct sysfs_dirent **pos;
1da177e4
LT
1024 ino_t ino;
1025 int i = filp->f_pos;
1026
1027 switch (i) {
1028 case 0:
dc351252 1029 ino = parent_sd->s_ino;
1da177e4
LT
1030 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1031 break;
1032 filp->f_pos++;
1033 i++;
1034 /* fallthrough */
1035 case 1:
13b3086d
TH
1036 if (parent_sd->s_parent)
1037 ino = parent_sd->s_parent->s_ino;
1038 else
1039 ino = parent_sd->s_ino;
1da177e4
LT
1040 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1041 break;
1042 filp->f_pos++;
1043 i++;
1044 /* fallthrough */
1045 default:
3007e997
TH
1046 mutex_lock(&sysfs_mutex);
1047
0c73f18b
TH
1048 pos = &parent_sd->s_children;
1049 while (*pos != cursor)
1050 pos = &(*pos)->s_sibling;
1051
1052 /* unlink cursor */
1053 *pos = cursor->s_sibling;
1054
1bfba4e8 1055 if (filp->f_pos == 2)
0c73f18b 1056 pos = &parent_sd->s_children;
1bfba4e8 1057
0c73f18b
TH
1058 for ( ; *pos; pos = &(*pos)->s_sibling) {
1059 struct sysfs_dirent *next = *pos;
1da177e4
LT
1060 const char * name;
1061 int len;
1062
b402d72c 1063 if (!sysfs_type(next))
1da177e4
LT
1064 continue;
1065
0c096b50 1066 name = next->s_name;
1da177e4 1067 len = strlen(name);
dc351252 1068 ino = next->s_ino;
1da177e4
LT
1069
1070 if (filldir(dirent, name, len, filp->f_pos, ino,
1071 dt_type(next)) < 0)
0c73f18b 1072 break;
1da177e4 1073
1da177e4
LT
1074 filp->f_pos++;
1075 }
0c73f18b
TH
1076
1077 /* put cursor back in */
1078 cursor->s_sibling = *pos;
1079 *pos = cursor;
3007e997
TH
1080
1081 mutex_unlock(&sysfs_mutex);
1da177e4
LT
1082 }
1083 return 0;
1084}
1085
1086static loff_t sysfs_dir_lseek(struct file * file, loff_t offset, int origin)
1087{
f427f5d5 1088 struct dentry * dentry = file->f_path.dentry;
1da177e4 1089
1da177e4
LT
1090 switch (origin) {
1091 case 1:
1092 offset += file->f_pos;
1093 case 0:
1094 if (offset >= 0)
1095 break;
1096 default:
1da177e4
LT
1097 return -EINVAL;
1098 }
1099 if (offset != file->f_pos) {
3007e997
TH
1100 mutex_lock(&sysfs_mutex);
1101
1da177e4
LT
1102 file->f_pos = offset;
1103 if (file->f_pos >= 2) {
1104 struct sysfs_dirent *sd = dentry->d_fsdata;
1105 struct sysfs_dirent *cursor = file->private_data;
0c73f18b 1106 struct sysfs_dirent **pos;
1da177e4
LT
1107 loff_t n = file->f_pos - 2;
1108
0c73f18b
TH
1109 sysfs_unlink_sibling(cursor);
1110
1111 pos = &sd->s_children;
1112 while (n && *pos) {
1113 struct sysfs_dirent *next = *pos;
b402d72c 1114 if (sysfs_type(next))
1da177e4 1115 n--;
0c73f18b 1116 pos = &(*pos)->s_sibling;
1da177e4 1117 }
0c73f18b
TH
1118
1119 cursor->s_sibling = *pos;
1120 *pos = cursor;
1da177e4 1121 }
3007e997
TH
1122
1123 mutex_unlock(&sysfs_mutex);
1da177e4 1124 }
3007e997 1125
1da177e4
LT
1126 return offset;
1127}
1128
4b6f5d20 1129const struct file_operations sysfs_dir_operations = {
1da177e4
LT
1130 .open = sysfs_dir_open,
1131 .release = sysfs_dir_close,
1132 .llseek = sysfs_dir_lseek,
1133 .read = generic_read_dir,
1134 .readdir = sysfs_readdir,
1135};