2 * fs/sysfs/dir.c - sysfs core and dir operation implementation
4 * Copyright (c) 2001-3 Patrick Mochel
5 * Copyright (c) 2007 SUSE Linux Products GmbH
6 * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
8 * This file is released under the GPLv2.
10 * Please see Documentation/filesystems/sysfs.txt for more information.
16 #include <linux/mount.h>
17 #include <linux/module.h>
18 #include <linux/kobject.h>
19 #include <linux/namei.h>
20 #include <linux/idr.h>
21 #include <linux/completion.h>
22 #include <linux/mutex.h>
23 #include <linux/slab.h>
26 DEFINE_MUTEX(sysfs_mutex);
27 DEFINE_MUTEX(sysfs_rename_mutex);
28 DEFINE_SPINLOCK(sysfs_assoc_lock);
30 static DEFINE_SPINLOCK(sysfs_ino_lock);
31 static DEFINE_IDA(sysfs_ino_ida);
34 * sysfs_link_sibling - link sysfs_dirent into sibling list
35 * @sd: sysfs_dirent of interest
37 * Link @sd into its sibling list which starts from
38 * sd->s_parent->s_dir.children.
41 * mutex_lock(sysfs_mutex)
43 static void sysfs_link_sibling(struct sysfs_dirent *sd)
45 struct sysfs_dirent *parent_sd = sd->s_parent;
46 struct sysfs_dirent **pos;
48 BUG_ON(sd->s_sibling);
50 /* Store directory entries in order by ino. This allows
51 * readdir to properly restart without having to add a
52 * cursor into the s_dir.children list.
54 for (pos = &parent_sd->s_dir.children; *pos; pos = &(*pos)->s_sibling) {
55 if (sd->s_ino < (*pos)->s_ino)
63 * sysfs_unlink_sibling - unlink sysfs_dirent from sibling list
64 * @sd: sysfs_dirent of interest
66 * Unlink @sd from its sibling list which starts from
67 * sd->s_parent->s_dir.children.
70 * mutex_lock(sysfs_mutex)
72 static void sysfs_unlink_sibling(struct sysfs_dirent *sd)
74 struct sysfs_dirent **pos;
76 for (pos = &sd->s_parent->s_dir.children; *pos;
77 pos = &(*pos)->s_sibling) {
87 * sysfs_get_dentry - get dentry for the given sysfs_dirent
88 * @sd: sysfs_dirent of interest
90 * Get dentry for @sd. Dentry is looked up if currently not
91 * present. This function descends from the root looking up
92 * dentry for each step.
95 * mutex_lock(sysfs_rename_mutex)
98 * Pointer to found dentry on success, ERR_PTR() value on error.
100 struct dentry *sysfs_get_dentry(struct sysfs_dirent *sd)
102 struct dentry *dentry = dget(sysfs_sb->s_root);
104 while (dentry->d_fsdata != sd) {
105 struct sysfs_dirent *cur;
106 struct dentry *parent;
108 /* find the first ancestor which hasn't been looked up */
110 while (cur->s_parent != dentry->d_fsdata)
115 mutex_lock(&parent->d_inode->i_mutex);
116 dentry = lookup_one_noperm(cur->s_name, parent);
117 mutex_unlock(&parent->d_inode->i_mutex);
127 * sysfs_get_active - get an active reference to sysfs_dirent
128 * @sd: sysfs_dirent to get an active reference to
130 * Get an active reference of @sd. This function is noop if @sd
134 * Pointer to @sd on success, NULL on failure.
136 static struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd)
144 v = atomic_read(&sd->s_active);
148 t = atomic_cmpxchg(&sd->s_active, v, v + 1);
159 * sysfs_put_active - put an active reference to sysfs_dirent
160 * @sd: sysfs_dirent to put an active reference to
162 * Put an active reference to @sd. This function is noop if @sd
165 static void sysfs_put_active(struct sysfs_dirent *sd)
167 struct completion *cmpl;
173 v = atomic_dec_return(&sd->s_active);
174 if (likely(v != SD_DEACTIVATED_BIAS))
177 /* atomic_dec_return() is a mb(), we'll always see the updated
180 cmpl = (void *)sd->s_sibling;
185 * sysfs_get_active_two - get active references to sysfs_dirent and parent
186 * @sd: sysfs_dirent of interest
188 * Get active reference to @sd and its parent. Parent's active
189 * reference is grabbed first. This function is noop if @sd is
193 * Pointer to @sd on success, NULL on failure.
195 struct sysfs_dirent *sysfs_get_active_two(struct sysfs_dirent *sd)
198 if (sd->s_parent && unlikely(!sysfs_get_active(sd->s_parent)))
200 if (unlikely(!sysfs_get_active(sd))) {
201 sysfs_put_active(sd->s_parent);
209 * sysfs_put_active_two - put active references to sysfs_dirent and parent
210 * @sd: sysfs_dirent of interest
212 * Put active references to @sd and its parent. This function is
213 * noop if @sd is NULL.
215 void sysfs_put_active_two(struct sysfs_dirent *sd)
218 sysfs_put_active(sd);
219 sysfs_put_active(sd->s_parent);
224 * sysfs_deactivate - deactivate sysfs_dirent
225 * @sd: sysfs_dirent to deactivate
227 * Deny new active references and drain existing ones.
229 static void sysfs_deactivate(struct sysfs_dirent *sd)
231 DECLARE_COMPLETION_ONSTACK(wait);
234 BUG_ON(sd->s_sibling || !(sd->s_flags & SYSFS_FLAG_REMOVED));
235 sd->s_sibling = (void *)&wait;
237 /* atomic_add_return() is a mb(), put_active() will always see
238 * the updated sd->s_sibling.
240 v = atomic_add_return(SD_DEACTIVATED_BIAS, &sd->s_active);
242 if (v != SD_DEACTIVATED_BIAS)
243 wait_for_completion(&wait);
245 sd->s_sibling = NULL;
248 static int sysfs_alloc_ino(ino_t *pino)
253 spin_lock(&sysfs_ino_lock);
254 rc = ida_get_new_above(&sysfs_ino_ida, 2, &ino);
255 spin_unlock(&sysfs_ino_lock);
258 if (ida_pre_get(&sysfs_ino_ida, GFP_KERNEL))
267 static void sysfs_free_ino(ino_t ino)
269 spin_lock(&sysfs_ino_lock);
270 ida_remove(&sysfs_ino_ida, ino);
271 spin_unlock(&sysfs_ino_lock);
274 void release_sysfs_dirent(struct sysfs_dirent * sd)
276 struct sysfs_dirent *parent_sd;
279 /* Moving/renaming is always done while holding reference.
280 * sd->s_parent won't change beneath us.
282 parent_sd = sd->s_parent;
284 if (sysfs_type(sd) == SYSFS_KOBJ_LINK)
285 sysfs_put(sd->s_symlink.target_sd);
286 if (sysfs_type(sd) & SYSFS_COPY_NAME)
289 sysfs_free_ino(sd->s_ino);
290 kmem_cache_free(sysfs_dir_cachep, sd);
293 if (sd && atomic_dec_and_test(&sd->s_count))
297 static void sysfs_d_iput(struct dentry * dentry, struct inode * inode)
299 struct sysfs_dirent * sd = dentry->d_fsdata;
305 static struct dentry_operations sysfs_dentry_ops = {
306 .d_iput = sysfs_d_iput,
309 struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type)
311 char *dup_name = NULL;
312 struct sysfs_dirent *sd;
314 if (type & SYSFS_COPY_NAME) {
315 name = dup_name = kstrdup(name, GFP_KERNEL);
320 sd = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL);
324 if (sysfs_alloc_ino(&sd->s_ino))
327 atomic_set(&sd->s_count, 1);
328 atomic_set(&sd->s_active, 0);
337 kmem_cache_free(sysfs_dir_cachep, sd);
343 static int sysfs_ilookup_test(struct inode *inode, void *arg)
345 struct sysfs_dirent *sd = arg;
346 return inode->i_ino == sd->s_ino;
350 * sysfs_addrm_start - prepare for sysfs_dirent add/remove
351 * @acxt: pointer to sysfs_addrm_cxt to be used
352 * @parent_sd: parent sysfs_dirent
354 * This function is called when the caller is about to add or
355 * remove sysfs_dirent under @parent_sd. This function acquires
356 * sysfs_mutex, grabs inode for @parent_sd if available and lock
357 * i_mutex of it. @acxt is used to keep and pass context to
358 * other addrm functions.
361 * Kernel thread context (may sleep). sysfs_mutex is locked on
362 * return. i_mutex of parent inode is locked on return if
365 void sysfs_addrm_start(struct sysfs_addrm_cxt *acxt,
366 struct sysfs_dirent *parent_sd)
370 memset(acxt, 0, sizeof(*acxt));
371 acxt->parent_sd = parent_sd;
373 /* Lookup parent inode. inode initialization and I_NEW
374 * clearing are protected by sysfs_mutex. By grabbing it and
375 * looking up with _nowait variant, inode state can be
376 * determined reliably.
378 mutex_lock(&sysfs_mutex);
380 inode = ilookup5_nowait(sysfs_sb, parent_sd->s_ino, sysfs_ilookup_test,
383 if (inode && !(inode->i_state & I_NEW)) {
384 /* parent inode available */
385 acxt->parent_inode = inode;
387 /* sysfs_mutex is below i_mutex in lock hierarchy.
388 * First, trylock i_mutex. If fails, unlock
389 * sysfs_mutex and lock them in order.
391 if (!mutex_trylock(&inode->i_mutex)) {
392 mutex_unlock(&sysfs_mutex);
393 mutex_lock(&inode->i_mutex);
394 mutex_lock(&sysfs_mutex);
401 * sysfs_add_one - add sysfs_dirent to parent
402 * @acxt: addrm context to use
403 * @sd: sysfs_dirent to be added
405 * Get @acxt->parent_sd and set sd->s_parent to it and increment
406 * nlink of parent inode if @sd is a directory and link into the
407 * children list of the parent.
409 * This function should be called between calls to
410 * sysfs_addrm_start() and sysfs_addrm_finish() and should be
411 * passed the same @acxt as passed to sysfs_addrm_start().
414 * Determined by sysfs_addrm_start().
417 * 0 on success, -EEXIST if entry with the given name already
420 int sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
422 if (sysfs_find_dirent(acxt->parent_sd, sd->s_name))
425 sd->s_parent = sysfs_get(acxt->parent_sd);
427 if (sysfs_type(sd) == SYSFS_DIR && acxt->parent_inode)
428 inc_nlink(acxt->parent_inode);
432 sysfs_link_sibling(sd);
438 * sysfs_remove_one - remove sysfs_dirent from parent
439 * @acxt: addrm context to use
440 * @sd: sysfs_dirent to be removed
442 * Mark @sd removed and drop nlink of parent inode if @sd is a
443 * directory. @sd is unlinked from the children list.
445 * This function should be called between calls to
446 * sysfs_addrm_start() and sysfs_addrm_finish() and should be
447 * passed the same @acxt as passed to sysfs_addrm_start().
450 * Determined by sysfs_addrm_start().
452 void sysfs_remove_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
454 BUG_ON(sd->s_flags & SYSFS_FLAG_REMOVED);
456 sysfs_unlink_sibling(sd);
458 sd->s_flags |= SYSFS_FLAG_REMOVED;
459 sd->s_sibling = acxt->removed;
462 if (sysfs_type(sd) == SYSFS_DIR && acxt->parent_inode)
463 drop_nlink(acxt->parent_inode);
469 * sysfs_drop_dentry - drop dentry for the specified sysfs_dirent
470 * @sd: target sysfs_dirent
472 * Drop dentry for @sd. @sd must have been unlinked from its
473 * parent on entry to this function such that it can't be looked
476 static void sysfs_drop_dentry(struct sysfs_dirent *sd)
479 struct dentry *dentry;
481 inode = ilookup(sysfs_sb, sd->s_ino);
485 /* Drop any existing dentries associated with sd.
487 * For the dentry to be properly freed we need to grab a
488 * reference to the dentry under the dcache lock, unhash it,
489 * and then put it. The playing with the dentry count allows
490 * dput to immediately free the dentry if it is not in use.
493 spin_lock(&dcache_lock);
494 list_for_each_entry(dentry, &inode->i_dentry, d_alias) {
495 if (d_unhashed(dentry))
498 spin_lock(&dentry->d_lock);
500 spin_unlock(&dentry->d_lock);
501 spin_unlock(&dcache_lock);
505 spin_unlock(&dcache_lock);
507 /* adjust nlink and update timestamp */
508 mutex_lock(&inode->i_mutex);
510 inode->i_ctime = CURRENT_TIME;
512 if (sysfs_type(sd) == SYSFS_DIR)
515 mutex_unlock(&inode->i_mutex);
521 * sysfs_addrm_finish - finish up sysfs_dirent add/remove
522 * @acxt: addrm context to finish up
524 * Finish up sysfs_dirent add/remove. Resources acquired by
525 * sysfs_addrm_start() are released and removed sysfs_dirents are
526 * cleaned up. Timestamps on the parent inode are updated.
529 * All mutexes acquired by sysfs_addrm_start() are released.
531 void sysfs_addrm_finish(struct sysfs_addrm_cxt *acxt)
533 /* release resources acquired by sysfs_addrm_start() */
534 mutex_unlock(&sysfs_mutex);
535 if (acxt->parent_inode) {
536 struct inode *inode = acxt->parent_inode;
538 /* if added/removed, update timestamps on the parent */
540 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
542 mutex_unlock(&inode->i_mutex);
546 /* kill removed sysfs_dirents */
547 while (acxt->removed) {
548 struct sysfs_dirent *sd = acxt->removed;
550 acxt->removed = sd->s_sibling;
551 sd->s_sibling = NULL;
553 sysfs_drop_dentry(sd);
554 sysfs_deactivate(sd);
560 * sysfs_find_dirent - find sysfs_dirent with the given name
561 * @parent_sd: sysfs_dirent to search under
562 * @name: name to look for
564 * Look for sysfs_dirent with name @name under @parent_sd.
567 * mutex_lock(sysfs_mutex)
570 * Pointer to sysfs_dirent if found, NULL if not.
572 struct sysfs_dirent *sysfs_find_dirent(struct sysfs_dirent *parent_sd,
573 const unsigned char *name)
575 struct sysfs_dirent *sd;
577 for (sd = parent_sd->s_dir.children; sd; sd = sd->s_sibling)
578 if (!strcmp(sd->s_name, name))
584 * sysfs_get_dirent - find and get sysfs_dirent with the given name
585 * @parent_sd: sysfs_dirent to search under
586 * @name: name to look for
588 * Look for sysfs_dirent with name @name under @parent_sd and get
592 * Kernel thread context (may sleep). Grabs sysfs_mutex.
595 * Pointer to sysfs_dirent if found, NULL if not.
597 struct sysfs_dirent *sysfs_get_dirent(struct sysfs_dirent *parent_sd,
598 const unsigned char *name)
600 struct sysfs_dirent *sd;
602 mutex_lock(&sysfs_mutex);
603 sd = sysfs_find_dirent(parent_sd, name);
605 mutex_unlock(&sysfs_mutex);
610 static int create_dir(struct kobject *kobj, struct sysfs_dirent *parent_sd,
611 const char *name, struct sysfs_dirent **p_sd)
613 umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
614 struct sysfs_addrm_cxt acxt;
615 struct sysfs_dirent *sd;
619 sd = sysfs_new_dirent(name, mode, SYSFS_DIR);
622 sd->s_dir.kobj = kobj;
625 sysfs_addrm_start(&acxt, parent_sd);
626 rc = sysfs_add_one(&acxt, sd);
627 sysfs_addrm_finish(&acxt);
637 int sysfs_create_subdir(struct kobject *kobj, const char *name,
638 struct sysfs_dirent **p_sd)
640 return create_dir(kobj, kobj->sd, name, p_sd);
644 * sysfs_create_dir - create a directory for an object.
645 * @kobj: object we're creating directory for.
647 int sysfs_create_dir(struct kobject * kobj)
649 struct sysfs_dirent *parent_sd, *sd;
655 parent_sd = kobj->parent->sd;
657 parent_sd = &sysfs_root;
659 error = create_dir(kobj, parent_sd, kobject_name(kobj), &sd);
665 static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
666 struct nameidata *nd)
668 struct dentry *ret = NULL;
669 struct sysfs_dirent *parent_sd = dentry->d_parent->d_fsdata;
670 struct sysfs_dirent *sd;
673 mutex_lock(&sysfs_mutex);
675 sd = sysfs_find_dirent(parent_sd, dentry->d_name.name);
679 ret = ERR_PTR(-ENOENT);
683 /* attach dentry and inode */
684 inode = sysfs_get_inode(sd);
686 ret = ERR_PTR(-ENOMEM);
690 /* instantiate and hash dentry */
691 dentry->d_op = &sysfs_dentry_ops;
692 dentry->d_fsdata = sysfs_get(sd);
693 d_instantiate(dentry, inode);
697 mutex_unlock(&sysfs_mutex);
701 const struct inode_operations sysfs_dir_inode_operations = {
702 .lookup = sysfs_lookup,
703 .setattr = sysfs_setattr,
706 static void remove_dir(struct sysfs_dirent *sd)
708 struct sysfs_addrm_cxt acxt;
710 sysfs_addrm_start(&acxt, sd->s_parent);
711 sysfs_remove_one(&acxt, sd);
712 sysfs_addrm_finish(&acxt);
715 void sysfs_remove_subdir(struct sysfs_dirent *sd)
721 static void __sysfs_remove_dir(struct sysfs_dirent *dir_sd)
723 struct sysfs_addrm_cxt acxt;
724 struct sysfs_dirent **pos;
729 pr_debug("sysfs %s: removing dir\n", dir_sd->s_name);
730 sysfs_addrm_start(&acxt, dir_sd);
731 pos = &dir_sd->s_dir.children;
733 struct sysfs_dirent *sd = *pos;
735 if (sysfs_type(sd) != SYSFS_DIR)
736 sysfs_remove_one(&acxt, sd);
738 pos = &(*pos)->s_sibling;
740 sysfs_addrm_finish(&acxt);
746 * sysfs_remove_dir - remove an object's directory.
749 * The only thing special about this is that we remove any files in
750 * the directory before we remove the directory, and we've inlined
751 * what used to be sysfs_rmdir() below, instead of calling separately.
754 void sysfs_remove_dir(struct kobject * kobj)
756 struct sysfs_dirent *sd = kobj->sd;
758 spin_lock(&sysfs_assoc_lock);
760 spin_unlock(&sysfs_assoc_lock);
762 __sysfs_remove_dir(sd);
765 int sysfs_rename_dir(struct kobject * kobj, const char *new_name)
767 struct sysfs_dirent *sd = kobj->sd;
768 struct dentry *parent = NULL;
769 struct dentry *old_dentry = NULL, *new_dentry = NULL;
770 const char *dup_name = NULL;
773 mutex_lock(&sysfs_rename_mutex);
776 if (strcmp(sd->s_name, new_name) == 0)
777 goto out; /* nothing to rename */
779 /* get the original dentry */
780 old_dentry = sysfs_get_dentry(sd);
781 if (IS_ERR(old_dentry)) {
782 error = PTR_ERR(old_dentry);
787 parent = old_dentry->d_parent;
789 /* lock parent and get dentry for new name */
790 mutex_lock(&parent->d_inode->i_mutex);
791 mutex_lock(&sysfs_mutex);
794 if (sysfs_find_dirent(sd->s_parent, new_name))
798 new_dentry = d_alloc_name(parent, new_name);
802 /* rename kobject and sysfs_dirent */
804 new_name = dup_name = kstrdup(new_name, GFP_KERNEL);
808 error = kobject_set_name(kobj, "%s", new_name);
812 dup_name = sd->s_name;
813 sd->s_name = new_name;
816 d_add(new_dentry, NULL);
817 d_move(old_dentry, new_dentry);
821 mutex_unlock(&sysfs_mutex);
822 mutex_unlock(&parent->d_inode->i_mutex);
827 mutex_unlock(&sysfs_rename_mutex);
831 int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent_kobj)
833 struct sysfs_dirent *sd = kobj->sd;
834 struct sysfs_dirent *new_parent_sd;
835 struct dentry *old_parent, *new_parent = NULL;
836 struct dentry *old_dentry = NULL, *new_dentry = NULL;
839 mutex_lock(&sysfs_rename_mutex);
840 BUG_ON(!sd->s_parent);
841 new_parent_sd = new_parent_kobj->sd ? new_parent_kobj->sd : &sysfs_root;
844 if (sd->s_parent == new_parent_sd)
845 goto out; /* nothing to move */
848 old_dentry = sysfs_get_dentry(sd);
849 if (IS_ERR(old_dentry)) {
850 error = PTR_ERR(old_dentry);
854 old_parent = old_dentry->d_parent;
856 new_parent = sysfs_get_dentry(new_parent_sd);
857 if (IS_ERR(new_parent)) {
858 error = PTR_ERR(new_parent);
864 mutex_lock(&old_parent->d_inode->i_mutex);
865 if (!mutex_trylock(&new_parent->d_inode->i_mutex)) {
866 mutex_unlock(&old_parent->d_inode->i_mutex);
869 mutex_lock(&sysfs_mutex);
872 if (sysfs_find_dirent(new_parent_sd, sd->s_name))
876 new_dentry = d_alloc_name(new_parent, sd->s_name);
881 d_add(new_dentry, NULL);
882 d_move(old_dentry, new_dentry);
884 /* Remove from old parent's list and insert into new parent's list. */
885 sysfs_unlink_sibling(sd);
886 sysfs_get(new_parent_sd);
887 sysfs_put(sd->s_parent);
888 sd->s_parent = new_parent_sd;
889 sysfs_link_sibling(sd);
892 mutex_unlock(&sysfs_mutex);
893 mutex_unlock(&new_parent->d_inode->i_mutex);
894 mutex_unlock(&old_parent->d_inode->i_mutex);
899 mutex_unlock(&sysfs_rename_mutex);
903 /* Relationship between s_mode and the DT_xxx types */
904 static inline unsigned char dt_type(struct sysfs_dirent *sd)
906 return (sd->s_mode >> 12) & 15;
909 static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
911 struct dentry *dentry = filp->f_path.dentry;
912 struct sysfs_dirent * parent_sd = dentry->d_fsdata;
913 struct sysfs_dirent *pos;
916 if (filp->f_pos == 0) {
917 ino = parent_sd->s_ino;
918 if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) == 0)
921 if (filp->f_pos == 1) {
922 if (parent_sd->s_parent)
923 ino = parent_sd->s_parent->s_ino;
925 ino = parent_sd->s_ino;
926 if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) == 0)
929 if ((filp->f_pos > 1) && (filp->f_pos < INT_MAX)) {
930 mutex_lock(&sysfs_mutex);
932 /* Skip the dentries we have already reported */
933 pos = parent_sd->s_dir.children;
934 while (pos && (filp->f_pos > pos->s_ino))
935 pos = pos->s_sibling;
937 for ( ; pos; pos = pos->s_sibling) {
943 filp->f_pos = ino = pos->s_ino;
945 if (filldir(dirent, name, len, filp->f_pos, ino,
950 filp->f_pos = INT_MAX;
951 mutex_unlock(&sysfs_mutex);
957 const struct file_operations sysfs_dir_operations = {
958 .read = generic_read_dir,
959 .readdir = sysfs_readdir,