2 * dir.c - Operations for sysfs directories.
8 #include <linux/mount.h>
9 #include <linux/module.h>
10 #include <linux/kobject.h>
11 #include <linux/namei.h>
12 #include <linux/idr.h>
13 #include <linux/completion.h>
14 #include <asm/semaphore.h>
17 DECLARE_RWSEM(sysfs_rename_sem);
18 spinlock_t sysfs_lock = SPIN_LOCK_UNLOCKED;
19 spinlock_t kobj_sysfs_assoc_lock = SPIN_LOCK_UNLOCKED;
21 static spinlock_t sysfs_ino_lock = SPIN_LOCK_UNLOCKED;
22 static DEFINE_IDA(sysfs_ino_ida);
25 * sysfs_link_sibling - link sysfs_dirent into sibling list
26 * @sd: sysfs_dirent of interest
28 * Link @sd into its sibling list which starts from
29 * sd->s_parent->s_children.
32 * mutex_lock(sd->s_parent->dentry->d_inode->i_mutex)
34 static void sysfs_link_sibling(struct sysfs_dirent *sd)
36 struct sysfs_dirent *parent_sd = sd->s_parent;
38 BUG_ON(sd->s_sibling);
39 sd->s_sibling = parent_sd->s_children;
40 parent_sd->s_children = sd;
44 * sysfs_unlink_sibling - unlink sysfs_dirent from sibling list
45 * @sd: sysfs_dirent of interest
47 * Unlink @sd from its sibling list which starts from
48 * sd->s_parent->s_children.
51 * mutex_lock(sd->s_parent->dentry->d_inode->i_mutex)
53 static void sysfs_unlink_sibling(struct sysfs_dirent *sd)
55 struct sysfs_dirent **pos;
57 for (pos = &sd->s_parent->s_children; *pos; pos = &(*pos)->s_sibling) {
67 * sysfs_get_active - get an active reference to sysfs_dirent
68 * @sd: sysfs_dirent to get an active reference to
70 * Get an active reference of @sd. This function is noop if @sd
74 * Pointer to @sd on success, NULL on failure.
76 struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd)
84 v = atomic_read(&sd->s_active);
88 t = atomic_cmpxchg(&sd->s_active, v, v + 1);
99 * sysfs_put_active - put an active reference to sysfs_dirent
100 * @sd: sysfs_dirent to put an active reference to
102 * Put an active reference to @sd. This function is noop if @sd
105 void sysfs_put_active(struct sysfs_dirent *sd)
107 struct completion *cmpl;
113 v = atomic_dec_return(&sd->s_active);
114 if (likely(v != SD_DEACTIVATED_BIAS))
117 /* atomic_dec_return() is a mb(), we'll always see the updated
120 cmpl = (void *)sd->s_sibling;
125 * sysfs_get_active_two - get active references to sysfs_dirent and parent
126 * @sd: sysfs_dirent of interest
128 * Get active reference to @sd and its parent. Parent's active
129 * reference is grabbed first. This function is noop if @sd is
133 * Pointer to @sd on success, NULL on failure.
135 struct sysfs_dirent *sysfs_get_active_two(struct sysfs_dirent *sd)
138 if (sd->s_parent && unlikely(!sysfs_get_active(sd->s_parent)))
140 if (unlikely(!sysfs_get_active(sd))) {
141 sysfs_put_active(sd->s_parent);
149 * sysfs_put_active_two - put active references to sysfs_dirent and parent
150 * @sd: sysfs_dirent of interest
152 * Put active references to @sd and its parent. This function is
153 * noop if @sd is NULL.
155 void sysfs_put_active_two(struct sysfs_dirent *sd)
158 sysfs_put_active(sd);
159 sysfs_put_active(sd->s_parent);
164 * sysfs_deactivate - deactivate sysfs_dirent
165 * @sd: sysfs_dirent to deactivate
167 * Deny new active references and drain existing ones.
169 void sysfs_deactivate(struct sysfs_dirent *sd)
171 DECLARE_COMPLETION_ONSTACK(wait);
174 BUG_ON(sd->s_sibling || !(sd->s_flags & SYSFS_FLAG_REMOVED));
175 sd->s_sibling = (void *)&wait;
177 /* atomic_add_return() is a mb(), put_active() will always see
178 * the updated sd->s_sibling.
180 v = atomic_add_return(SD_DEACTIVATED_BIAS, &sd->s_active);
182 if (v != SD_DEACTIVATED_BIAS)
183 wait_for_completion(&wait);
185 sd->s_sibling = NULL;
188 static int sysfs_alloc_ino(ino_t *pino)
193 spin_lock(&sysfs_ino_lock);
194 rc = ida_get_new_above(&sysfs_ino_ida, 2, &ino);
195 spin_unlock(&sysfs_ino_lock);
198 if (ida_pre_get(&sysfs_ino_ida, GFP_KERNEL))
207 static void sysfs_free_ino(ino_t ino)
209 spin_lock(&sysfs_ino_lock);
210 ida_remove(&sysfs_ino_ida, ino);
211 spin_unlock(&sysfs_ino_lock);
214 void release_sysfs_dirent(struct sysfs_dirent * sd)
216 struct sysfs_dirent *parent_sd;
219 parent_sd = sd->s_parent;
221 if (sysfs_type(sd) == SYSFS_KOBJ_LINK)
222 sysfs_put(sd->s_elem.symlink.target_sd);
223 if (sysfs_type(sd) & SYSFS_COPY_NAME)
226 sysfs_free_ino(sd->s_ino);
227 kmem_cache_free(sysfs_dir_cachep, sd);
230 if (sd && atomic_dec_and_test(&sd->s_count))
234 static void sysfs_d_iput(struct dentry * dentry, struct inode * inode)
236 struct sysfs_dirent * sd = dentry->d_fsdata;
239 /* sd->s_dentry is protected with sysfs_lock. This
240 * allows sysfs_drop_dentry() to dereference it.
242 spin_lock(&sysfs_lock);
244 /* The dentry might have been deleted or another
245 * lookup could have happened updating sd->s_dentry to
246 * point the new dentry. Ignore if it isn't pointing
249 if (sd->s_dentry == dentry)
251 spin_unlock(&sysfs_lock);
257 static struct dentry_operations sysfs_dentry_ops = {
258 .d_iput = sysfs_d_iput,
261 struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type)
263 char *dup_name = NULL;
264 struct sysfs_dirent *sd = NULL;
266 if (type & SYSFS_COPY_NAME) {
267 name = dup_name = kstrdup(name, GFP_KERNEL);
272 sd = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL);
276 if (sysfs_alloc_ino(&sd->s_ino))
279 atomic_set(&sd->s_count, 1);
280 atomic_set(&sd->s_active, 0);
281 atomic_set(&sd->s_event, 1);
291 kmem_cache_free(sysfs_dir_cachep, sd);
295 static void sysfs_attach_dentry(struct sysfs_dirent *sd, struct dentry *dentry)
297 dentry->d_op = &sysfs_dentry_ops;
298 dentry->d_fsdata = sysfs_get(sd);
300 /* protect sd->s_dentry against sysfs_d_iput */
301 spin_lock(&sysfs_lock);
302 sd->s_dentry = dentry;
303 spin_unlock(&sysfs_lock);
308 void sysfs_attach_dirent(struct sysfs_dirent *sd,
309 struct sysfs_dirent *parent_sd, struct dentry *dentry)
312 sysfs_attach_dentry(sd, dentry);
315 sd->s_parent = sysfs_get(parent_sd);
316 sysfs_link_sibling(sd);
321 * sysfs_find_dirent - find sysfs_dirent with the given name
322 * @parent_sd: sysfs_dirent to search under
323 * @name: name to look for
325 * Look for sysfs_dirent with name @name under @parent_sd.
328 * mutex_lock(parent->i_mutex)
331 * Pointer to sysfs_dirent if found, NULL if not.
333 struct sysfs_dirent *sysfs_find_dirent(struct sysfs_dirent *parent_sd,
334 const unsigned char *name)
336 struct sysfs_dirent *sd;
338 for (sd = parent_sd->s_children; sd; sd = sd->s_sibling)
339 if (sysfs_type(sd) && !strcmp(sd->s_name, name))
345 * sysfs_get_dirent - find and get sysfs_dirent with the given name
346 * @parent_sd: sysfs_dirent to search under
347 * @name: name to look for
349 * Look for sysfs_dirent with name @name under @parent_sd and get
353 * Kernel thread context (may sleep)
356 * Pointer to sysfs_dirent if found, NULL if not.
358 struct sysfs_dirent *sysfs_get_dirent(struct sysfs_dirent *parent_sd,
359 const unsigned char *name)
361 struct sysfs_dirent *sd;
363 mutex_lock(&parent_sd->s_dentry->d_inode->i_mutex);
364 sd = sysfs_find_dirent(parent_sd, name);
366 mutex_unlock(&parent_sd->s_dentry->d_inode->i_mutex);
371 static int create_dir(struct kobject *kobj, struct sysfs_dirent *parent_sd,
372 const char *name, struct sysfs_dirent **p_sd)
374 struct dentry *parent = parent_sd->s_dentry;
376 umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
377 struct dentry *dentry;
379 struct sysfs_dirent *sd;
381 mutex_lock(&parent->d_inode->i_mutex);
384 dentry = lookup_one_len(name, parent, strlen(name));
385 if (IS_ERR(dentry)) {
386 error = PTR_ERR(dentry);
395 sd = sysfs_new_dirent(name, mode, SYSFS_DIR);
398 sd->s_elem.dir.kobj = kobj;
400 inode = sysfs_get_inode(sd);
404 if (inode->i_state & I_NEW) {
405 inode->i_op = &sysfs_dir_inode_operations;
406 inode->i_fop = &sysfs_dir_operations;
407 /* directory inodes start off with i_nlink == 2 (for ".") */
413 if (sysfs_find_dirent(parent_sd, name))
416 sysfs_instantiate(dentry, inode);
417 inc_nlink(parent->d_inode);
418 sysfs_attach_dirent(sd, parent_sd, dentry);
422 goto out_unlock; /* pin directory dentry in core */
433 mutex_unlock(&parent->d_inode->i_mutex);
437 int sysfs_create_subdir(struct kobject *kobj, const char *name,
438 struct sysfs_dirent **p_sd)
440 return create_dir(kobj, kobj->sd, name, p_sd);
444 * sysfs_create_dir - create a directory for an object.
445 * @kobj: object we're creating directory for.
446 * @shadow_parent: parent object.
448 int sysfs_create_dir(struct kobject *kobj,
449 struct sysfs_dirent *shadow_parent_sd)
451 struct sysfs_dirent *parent_sd, *sd;
456 if (shadow_parent_sd)
457 parent_sd = shadow_parent_sd;
458 else if (kobj->parent)
459 parent_sd = kobj->parent->sd;
460 else if (sysfs_mount && sysfs_mount->mnt_sb)
461 parent_sd = sysfs_mount->mnt_sb->s_root->d_fsdata;
465 error = create_dir(kobj, parent_sd, kobject_name(kobj), &sd);
471 static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
472 struct nameidata *nd)
474 struct sysfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
475 struct sysfs_dirent * sd;
476 struct bin_attribute *bin_attr;
480 for (sd = parent_sd->s_children; sd; sd = sd->s_sibling) {
481 if ((sysfs_type(sd) & SYSFS_NOT_PINNED) &&
482 !strcmp(sd->s_name, dentry->d_name.name)) {
492 /* attach dentry and inode */
493 inode = sysfs_get_inode(sd);
495 return ERR_PTR(-ENOMEM);
497 if (inode->i_state & I_NEW) {
498 /* initialize inode according to type */
499 switch (sysfs_type(sd)) {
500 case SYSFS_KOBJ_ATTR:
501 inode->i_size = PAGE_SIZE;
502 inode->i_fop = &sysfs_file_operations;
504 case SYSFS_KOBJ_BIN_ATTR:
505 bin_attr = sd->s_elem.bin_attr.bin_attr;
506 inode->i_size = bin_attr->size;
507 inode->i_fop = &bin_fops;
509 case SYSFS_KOBJ_LINK:
510 inode->i_op = &sysfs_symlink_inode_operations;
517 sysfs_instantiate(dentry, inode);
518 sysfs_attach_dentry(sd, dentry);
523 const struct inode_operations sysfs_dir_inode_operations = {
524 .lookup = sysfs_lookup,
525 .setattr = sysfs_setattr,
528 static void remove_dir(struct sysfs_dirent *sd)
530 struct dentry *parent = sd->s_parent->s_dentry;
532 mutex_lock(&parent->d_inode->i_mutex);
534 sysfs_unlink_sibling(sd);
535 sd->s_flags |= SYSFS_FLAG_REMOVED;
537 pr_debug(" o %s removing done\n", sd->s_name);
539 mutex_unlock(&parent->d_inode->i_mutex);
541 sysfs_drop_dentry(sd);
542 sysfs_deactivate(sd);
546 void sysfs_remove_subdir(struct sysfs_dirent *sd)
552 static void __sysfs_remove_dir(struct sysfs_dirent *dir_sd)
554 struct sysfs_dirent *removed = NULL;
555 struct sysfs_dirent **pos;
561 dir = dir_sd->s_dentry;
563 pr_debug("sysfs %s: removing dir\n", dir_sd->s_name);
564 mutex_lock(&dir->d_inode->i_mutex);
565 pos = &dir_sd->s_children;
567 struct sysfs_dirent *sd = *pos;
569 if (sysfs_type(sd) && (sysfs_type(sd) & SYSFS_NOT_PINNED)) {
570 sd->s_flags |= SYSFS_FLAG_REMOVED;
571 *pos = sd->s_sibling;
572 sd->s_sibling = removed;
575 pos = &(*pos)->s_sibling;
577 mutex_unlock(&dir->d_inode->i_mutex);
580 struct sysfs_dirent *sd = removed;
582 removed = sd->s_sibling;
583 sd->s_sibling = NULL;
585 sysfs_drop_dentry(sd);
586 sysfs_deactivate(sd);
594 * sysfs_remove_dir - remove an object's directory.
597 * The only thing special about this is that we remove any files in
598 * the directory before we remove the directory, and we've inlined
599 * what used to be sysfs_rmdir() below, instead of calling separately.
602 void sysfs_remove_dir(struct kobject * kobj)
604 struct sysfs_dirent *sd = kobj->sd;
606 spin_lock(&kobj_sysfs_assoc_lock);
608 spin_unlock(&kobj_sysfs_assoc_lock);
610 __sysfs_remove_dir(sd);
613 int sysfs_rename_dir(struct kobject *kobj, struct sysfs_dirent *new_parent_sd,
614 const char *new_name)
616 struct sysfs_dirent *sd = kobj->sd;
617 struct dentry *new_parent = new_parent_sd->s_dentry;
618 struct dentry *new_dentry;
625 down_write(&sysfs_rename_sem);
626 mutex_lock(&new_parent->d_inode->i_mutex);
628 new_dentry = lookup_one_len(new_name, new_parent, strlen(new_name));
629 if (IS_ERR(new_dentry)) {
630 error = PTR_ERR(new_dentry);
634 /* By allowing two different directories with the same
635 * d_parent we allow this routine to move between different
636 * shadows of the same directory
639 if (sd->s_parent->s_dentry->d_inode != new_parent->d_inode ||
640 new_dentry->d_parent->d_inode != new_parent->d_inode ||
641 new_dentry == sd->s_dentry)
645 if (new_dentry->d_inode)
648 /* rename kobject and sysfs_dirent */
650 new_name = dup_name = kstrdup(new_name, GFP_KERNEL);
654 error = kobject_set_name(kobj, "%s", new_name);
659 sd->s_name = new_name;
661 /* move under the new parent */
662 d_add(new_dentry, NULL);
663 d_move(sd->s_dentry, new_dentry);
665 sysfs_unlink_sibling(sd);
666 sysfs_get(new_parent_sd);
667 sysfs_put(sd->s_parent);
668 sd->s_parent = new_parent_sd;
669 sysfs_link_sibling(sd);
681 mutex_unlock(&new_parent->d_inode->i_mutex);
682 up_write(&sysfs_rename_sem);
686 int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent)
688 struct dentry *old_parent_dentry, *new_parent_dentry, *new_dentry;
689 struct sysfs_dirent *new_parent_sd, *sd;
692 old_parent_dentry = kobj->parent ?
693 kobj->parent->sd->s_dentry : sysfs_mount->mnt_sb->s_root;
694 new_parent_dentry = new_parent ?
695 new_parent->sd->s_dentry : sysfs_mount->mnt_sb->s_root;
697 if (old_parent_dentry->d_inode == new_parent_dentry->d_inode)
698 return 0; /* nothing to move */
700 mutex_lock(&old_parent_dentry->d_inode->i_mutex);
701 if (!mutex_trylock(&new_parent_dentry->d_inode->i_mutex)) {
702 mutex_unlock(&old_parent_dentry->d_inode->i_mutex);
706 new_parent_sd = new_parent_dentry->d_fsdata;
709 new_dentry = lookup_one_len(kobj->name, new_parent_dentry,
711 if (IS_ERR(new_dentry)) {
712 error = PTR_ERR(new_dentry);
716 d_add(new_dentry, NULL);
717 d_move(sd->s_dentry, new_dentry);
720 /* Remove from old parent's list and insert into new parent's list. */
721 sysfs_unlink_sibling(sd);
722 sysfs_get(new_parent_sd);
723 sysfs_put(sd->s_parent);
724 sd->s_parent = new_parent_sd;
725 sysfs_link_sibling(sd);
728 mutex_unlock(&new_parent_dentry->d_inode->i_mutex);
729 mutex_unlock(&old_parent_dentry->d_inode->i_mutex);
734 static int sysfs_dir_open(struct inode *inode, struct file *file)
736 struct dentry * dentry = file->f_path.dentry;
737 struct sysfs_dirent * parent_sd = dentry->d_fsdata;
738 struct sysfs_dirent * sd;
740 mutex_lock(&dentry->d_inode->i_mutex);
741 sd = sysfs_new_dirent("_DIR_", 0, 0);
743 sysfs_attach_dirent(sd, parent_sd, NULL);
744 mutex_unlock(&dentry->d_inode->i_mutex);
746 file->private_data = sd;
747 return sd ? 0 : -ENOMEM;
750 static int sysfs_dir_close(struct inode *inode, struct file *file)
752 struct dentry * dentry = file->f_path.dentry;
753 struct sysfs_dirent * cursor = file->private_data;
755 mutex_lock(&dentry->d_inode->i_mutex);
756 sysfs_unlink_sibling(cursor);
757 mutex_unlock(&dentry->d_inode->i_mutex);
759 release_sysfs_dirent(cursor);
764 /* Relationship between s_mode and the DT_xxx types */
765 static inline unsigned char dt_type(struct sysfs_dirent *sd)
767 return (sd->s_mode >> 12) & 15;
770 static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
772 struct dentry *dentry = filp->f_path.dentry;
773 struct sysfs_dirent * parent_sd = dentry->d_fsdata;
774 struct sysfs_dirent *cursor = filp->private_data;
775 struct sysfs_dirent **pos;
781 ino = parent_sd->s_ino;
782 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
788 if (parent_sd->s_parent)
789 ino = parent_sd->s_parent->s_ino;
791 ino = parent_sd->s_ino;
792 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
798 pos = &parent_sd->s_children;
799 while (*pos != cursor)
800 pos = &(*pos)->s_sibling;
803 *pos = cursor->s_sibling;
805 if (filp->f_pos == 2)
806 pos = &parent_sd->s_children;
808 for ( ; *pos; pos = &(*pos)->s_sibling) {
809 struct sysfs_dirent *next = *pos;
813 if (!sysfs_type(next))
820 if (filldir(dirent, name, len, filp->f_pos, ino,
827 /* put cursor back in */
828 cursor->s_sibling = *pos;
834 static loff_t sysfs_dir_lseek(struct file * file, loff_t offset, int origin)
836 struct dentry * dentry = file->f_path.dentry;
838 mutex_lock(&dentry->d_inode->i_mutex);
841 offset += file->f_pos;
846 mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
849 if (offset != file->f_pos) {
850 file->f_pos = offset;
851 if (file->f_pos >= 2) {
852 struct sysfs_dirent *sd = dentry->d_fsdata;
853 struct sysfs_dirent *cursor = file->private_data;
854 struct sysfs_dirent **pos;
855 loff_t n = file->f_pos - 2;
857 sysfs_unlink_sibling(cursor);
859 pos = &sd->s_children;
861 struct sysfs_dirent *next = *pos;
862 if (sysfs_type(next))
864 pos = &(*pos)->s_sibling;
867 cursor->s_sibling = *pos;
871 mutex_unlock(&dentry->d_inode->i_mutex);
877 * sysfs_make_shadowed_dir - Setup so a directory can be shadowed
878 * @kobj: object we're creating shadow of.
881 int sysfs_make_shadowed_dir(struct kobject *kobj,
882 void * (*follow_link)(struct dentry *, struct nameidata *))
885 struct inode_operations *i_op;
887 inode = kobj->sd->s_dentry->d_inode;
888 if (inode->i_op != &sysfs_dir_inode_operations)
891 i_op = kmalloc(sizeof(*i_op), GFP_KERNEL);
895 memcpy(i_op, &sysfs_dir_inode_operations, sizeof(*i_op));
896 i_op->follow_link = follow_link;
898 /* Locking of inode->i_op?
899 * Since setting i_op is a single word write and they
900 * are atomic we should be ok here.
907 * sysfs_create_shadow_dir - create a shadow directory for an object.
908 * @kobj: object we're creating directory for.
910 * sysfs_make_shadowed_dir must already have been called on this
914 struct sysfs_dirent *sysfs_create_shadow_dir(struct kobject *kobj)
916 struct dentry *dir = kobj->sd->s_dentry;
917 struct inode *inode = dir->d_inode;
918 struct dentry *parent = dir->d_parent;
919 struct sysfs_dirent *parent_sd = parent->d_fsdata;
920 struct dentry *shadow;
921 struct sysfs_dirent *sd;
923 sd = ERR_PTR(-EINVAL);
924 if (!sysfs_is_shadowed_inode(inode))
927 shadow = d_alloc(parent, &dir->d_name);
931 sd = sysfs_new_dirent("_SHADOW_", inode->i_mode, SYSFS_DIR);
934 sd->s_elem.dir.kobj = kobj;
935 /* point to parent_sd but don't attach to it */
936 sd->s_parent = sysfs_get(parent_sd);
937 sysfs_attach_dirent(sd, NULL, shadow);
939 d_instantiate(shadow, igrab(inode));
941 inc_nlink(parent->d_inode);
943 dget(shadow); /* Extra count - pin the dentry in core */
949 sd = ERR_PTR(-ENOMEM);
954 * sysfs_remove_shadow_dir - remove an object's directory.
955 * @shadow_sd: sysfs_dirent of shadow directory
957 * The only thing special about this is that we remove any files in
958 * the directory before we remove the directory, and we've inlined
959 * what used to be sysfs_rmdir() below, instead of calling separately.
962 void sysfs_remove_shadow_dir(struct sysfs_dirent *shadow_sd)
964 __sysfs_remove_dir(shadow_sd);
967 const struct file_operations sysfs_dir_operations = {
968 .open = sysfs_dir_open,
969 .release = sysfs_dir_close,
970 .llseek = sysfs_dir_lseek,
971 .read = generic_read_dir,
972 .readdir = sysfs_readdir,