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);
322 * Return -EEXIST if there is already a sysfs element with the same name for
325 * called with parent inode's i_mutex held
327 int sysfs_dirent_exist(struct sysfs_dirent *parent_sd,
328 const unsigned char *new)
330 struct sysfs_dirent * sd;
332 for (sd = parent_sd->s_children; sd; sd = sd->s_sibling) {
333 if (sysfs_type(sd)) {
334 if (strcmp(sd->s_name, new))
344 static int create_dir(struct kobject *kobj, struct dentry *parent,
345 const char *name, struct dentry **p_dentry)
348 umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
349 struct dentry *dentry;
351 struct sysfs_dirent *sd;
353 mutex_lock(&parent->d_inode->i_mutex);
356 dentry = lookup_one_len(name, parent, strlen(name));
357 if (IS_ERR(dentry)) {
358 error = PTR_ERR(dentry);
367 sd = sysfs_new_dirent(name, mode, SYSFS_DIR);
370 sd->s_elem.dir.kobj = kobj;
372 inode = sysfs_get_inode(sd);
376 if (inode->i_state & I_NEW) {
377 inode->i_op = &sysfs_dir_inode_operations;
378 inode->i_fop = &sysfs_dir_operations;
379 /* directory inodes start off with i_nlink == 2 (for ".") */
385 if (sysfs_dirent_exist(parent->d_fsdata, name))
388 sysfs_instantiate(dentry, inode);
389 inc_nlink(parent->d_inode);
390 sysfs_attach_dirent(sd, parent->d_fsdata, dentry);
394 goto out_unlock; /* pin directory dentry in core */
405 mutex_unlock(&parent->d_inode->i_mutex);
410 int sysfs_create_subdir(struct kobject * k, const char * n, struct dentry ** d)
412 return create_dir(k,k->dentry,n,d);
416 * sysfs_create_dir - create a directory for an object.
417 * @kobj: object we're creating directory for.
418 * @shadow_parent: parent parent object.
421 int sysfs_create_dir(struct kobject * kobj, struct dentry *shadow_parent)
423 struct dentry * dentry = NULL;
424 struct dentry * parent;
430 parent = shadow_parent;
431 else if (kobj->parent)
432 parent = kobj->parent->dentry;
433 else if (sysfs_mount && sysfs_mount->mnt_sb)
434 parent = sysfs_mount->mnt_sb->s_root;
438 error = create_dir(kobj,parent,kobject_name(kobj),&dentry);
440 kobj->dentry = dentry;
444 static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
445 struct nameidata *nd)
447 struct sysfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
448 struct sysfs_dirent * sd;
449 struct bin_attribute *bin_attr;
453 for (sd = parent_sd->s_children; sd; sd = sd->s_sibling) {
454 if ((sysfs_type(sd) & SYSFS_NOT_PINNED) &&
455 !strcmp(sd->s_name, dentry->d_name.name)) {
465 /* attach dentry and inode */
466 inode = sysfs_get_inode(sd);
468 return ERR_PTR(-ENOMEM);
470 if (inode->i_state & I_NEW) {
471 /* initialize inode according to type */
472 switch (sysfs_type(sd)) {
473 case SYSFS_KOBJ_ATTR:
474 inode->i_size = PAGE_SIZE;
475 inode->i_fop = &sysfs_file_operations;
477 case SYSFS_KOBJ_BIN_ATTR:
478 bin_attr = sd->s_elem.bin_attr.bin_attr;
479 inode->i_size = bin_attr->size;
480 inode->i_fop = &bin_fops;
482 case SYSFS_KOBJ_LINK:
483 inode->i_op = &sysfs_symlink_inode_operations;
490 sysfs_instantiate(dentry, inode);
491 sysfs_attach_dentry(sd, dentry);
496 const struct inode_operations sysfs_dir_inode_operations = {
497 .lookup = sysfs_lookup,
498 .setattr = sysfs_setattr,
501 static void remove_dir(struct dentry * d)
503 struct dentry *parent = d->d_parent;
504 struct sysfs_dirent *sd = d->d_fsdata;
506 mutex_lock(&parent->d_inode->i_mutex);
508 sysfs_unlink_sibling(sd);
509 sd->s_flags |= SYSFS_FLAG_REMOVED;
511 pr_debug(" o %s removing done (%d)\n",d->d_name.name,
512 atomic_read(&d->d_count));
514 mutex_unlock(&parent->d_inode->i_mutex);
516 sysfs_drop_dentry(sd);
517 sysfs_deactivate(sd);
521 void sysfs_remove_subdir(struct dentry * d)
527 static void __sysfs_remove_dir(struct dentry *dentry)
529 struct sysfs_dirent *removed = NULL;
530 struct sysfs_dirent *parent_sd;
531 struct sysfs_dirent **pos;
536 pr_debug("sysfs %s: removing dir\n",dentry->d_name.name);
537 mutex_lock(&dentry->d_inode->i_mutex);
538 parent_sd = dentry->d_fsdata;
539 pos = &parent_sd->s_children;
541 struct sysfs_dirent *sd = *pos;
543 if (sysfs_type(sd) && (sysfs_type(sd) & SYSFS_NOT_PINNED)) {
544 sd->s_flags |= SYSFS_FLAG_REMOVED;
545 *pos = sd->s_sibling;
546 sd->s_sibling = removed;
549 pos = &(*pos)->s_sibling;
551 mutex_unlock(&dentry->d_inode->i_mutex);
554 struct sysfs_dirent *sd = removed;
556 removed = sd->s_sibling;
557 sd->s_sibling = NULL;
559 sysfs_drop_dentry(sd);
560 sysfs_deactivate(sd);
568 * sysfs_remove_dir - remove an object's directory.
571 * The only thing special about this is that we remove any files in
572 * the directory before we remove the directory, and we've inlined
573 * what used to be sysfs_rmdir() below, instead of calling separately.
576 void sysfs_remove_dir(struct kobject * kobj)
578 struct dentry *d = kobj->dentry;
580 spin_lock(&kobj_sysfs_assoc_lock);
582 spin_unlock(&kobj_sysfs_assoc_lock);
584 __sysfs_remove_dir(d);
587 int sysfs_rename_dir(struct kobject * kobj, struct dentry *new_parent,
588 const char *new_name)
590 struct sysfs_dirent *sd = kobj->dentry->d_fsdata;
591 struct sysfs_dirent *parent_sd = new_parent->d_fsdata;
592 struct dentry *new_dentry;
599 down_write(&sysfs_rename_sem);
600 mutex_lock(&new_parent->d_inode->i_mutex);
602 new_dentry = lookup_one_len(new_name, new_parent, strlen(new_name));
603 if (IS_ERR(new_dentry)) {
604 error = PTR_ERR(new_dentry);
608 /* By allowing two different directories with the same
609 * d_parent we allow this routine to move between different
610 * shadows of the same directory
613 if (kobj->dentry->d_parent->d_inode != new_parent->d_inode ||
614 new_dentry->d_parent->d_inode != new_parent->d_inode ||
615 new_dentry == kobj->dentry)
619 if (new_dentry->d_inode)
622 /* rename kobject and sysfs_dirent */
624 new_name = dup_name = kstrdup(new_name, GFP_KERNEL);
628 error = kobject_set_name(kobj, "%s", new_name);
633 sd->s_name = new_name;
635 /* move under the new parent */
636 d_add(new_dentry, NULL);
637 d_move(kobj->dentry, new_dentry);
639 sysfs_unlink_sibling(sd);
640 sysfs_get(parent_sd);
641 sysfs_put(sd->s_parent);
642 sd->s_parent = parent_sd;
643 sysfs_link_sibling(sd);
655 mutex_unlock(&new_parent->d_inode->i_mutex);
656 up_write(&sysfs_rename_sem);
660 int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent)
662 struct dentry *old_parent_dentry, *new_parent_dentry, *new_dentry;
663 struct sysfs_dirent *new_parent_sd, *sd;
666 old_parent_dentry = kobj->parent ?
667 kobj->parent->dentry : sysfs_mount->mnt_sb->s_root;
668 new_parent_dentry = new_parent ?
669 new_parent->dentry : sysfs_mount->mnt_sb->s_root;
671 if (old_parent_dentry->d_inode == new_parent_dentry->d_inode)
672 return 0; /* nothing to move */
674 mutex_lock(&old_parent_dentry->d_inode->i_mutex);
675 if (!mutex_trylock(&new_parent_dentry->d_inode->i_mutex)) {
676 mutex_unlock(&old_parent_dentry->d_inode->i_mutex);
680 new_parent_sd = new_parent_dentry->d_fsdata;
681 sd = kobj->dentry->d_fsdata;
683 new_dentry = lookup_one_len(kobj->name, new_parent_dentry,
685 if (IS_ERR(new_dentry)) {
686 error = PTR_ERR(new_dentry);
690 d_add(new_dentry, NULL);
691 d_move(kobj->dentry, new_dentry);
694 /* Remove from old parent's list and insert into new parent's list. */
695 sysfs_unlink_sibling(sd);
696 sysfs_get(new_parent_sd);
697 sysfs_put(sd->s_parent);
698 sd->s_parent = new_parent_sd;
699 sysfs_link_sibling(sd);
702 mutex_unlock(&new_parent_dentry->d_inode->i_mutex);
703 mutex_unlock(&old_parent_dentry->d_inode->i_mutex);
708 static int sysfs_dir_open(struct inode *inode, struct file *file)
710 struct dentry * dentry = file->f_path.dentry;
711 struct sysfs_dirent * parent_sd = dentry->d_fsdata;
712 struct sysfs_dirent * sd;
714 mutex_lock(&dentry->d_inode->i_mutex);
715 sd = sysfs_new_dirent("_DIR_", 0, 0);
717 sysfs_attach_dirent(sd, parent_sd, NULL);
718 mutex_unlock(&dentry->d_inode->i_mutex);
720 file->private_data = sd;
721 return sd ? 0 : -ENOMEM;
724 static int sysfs_dir_close(struct inode *inode, struct file *file)
726 struct dentry * dentry = file->f_path.dentry;
727 struct sysfs_dirent * cursor = file->private_data;
729 mutex_lock(&dentry->d_inode->i_mutex);
730 sysfs_unlink_sibling(cursor);
731 mutex_unlock(&dentry->d_inode->i_mutex);
733 release_sysfs_dirent(cursor);
738 /* Relationship between s_mode and the DT_xxx types */
739 static inline unsigned char dt_type(struct sysfs_dirent *sd)
741 return (sd->s_mode >> 12) & 15;
744 static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
746 struct dentry *dentry = filp->f_path.dentry;
747 struct sysfs_dirent * parent_sd = dentry->d_fsdata;
748 struct sysfs_dirent *cursor = filp->private_data;
749 struct sysfs_dirent **pos;
755 ino = parent_sd->s_ino;
756 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
762 if (parent_sd->s_parent)
763 ino = parent_sd->s_parent->s_ino;
765 ino = parent_sd->s_ino;
766 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
772 pos = &parent_sd->s_children;
773 while (*pos != cursor)
774 pos = &(*pos)->s_sibling;
777 *pos = cursor->s_sibling;
779 if (filp->f_pos == 2)
780 pos = &parent_sd->s_children;
782 for ( ; *pos; pos = &(*pos)->s_sibling) {
783 struct sysfs_dirent *next = *pos;
787 if (!sysfs_type(next))
794 if (filldir(dirent, name, len, filp->f_pos, ino,
801 /* put cursor back in */
802 cursor->s_sibling = *pos;
808 static loff_t sysfs_dir_lseek(struct file * file, loff_t offset, int origin)
810 struct dentry * dentry = file->f_path.dentry;
812 mutex_lock(&dentry->d_inode->i_mutex);
815 offset += file->f_pos;
820 mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
823 if (offset != file->f_pos) {
824 file->f_pos = offset;
825 if (file->f_pos >= 2) {
826 struct sysfs_dirent *sd = dentry->d_fsdata;
827 struct sysfs_dirent *cursor = file->private_data;
828 struct sysfs_dirent **pos;
829 loff_t n = file->f_pos - 2;
831 sysfs_unlink_sibling(cursor);
833 pos = &sd->s_children;
835 struct sysfs_dirent *next = *pos;
836 if (sysfs_type(next))
838 pos = &(*pos)->s_sibling;
841 cursor->s_sibling = *pos;
845 mutex_unlock(&dentry->d_inode->i_mutex);
851 * sysfs_make_shadowed_dir - Setup so a directory can be shadowed
852 * @kobj: object we're creating shadow of.
855 int sysfs_make_shadowed_dir(struct kobject *kobj,
856 void * (*follow_link)(struct dentry *, struct nameidata *))
859 struct inode_operations *i_op;
861 inode = kobj->dentry->d_inode;
862 if (inode->i_op != &sysfs_dir_inode_operations)
865 i_op = kmalloc(sizeof(*i_op), GFP_KERNEL);
869 memcpy(i_op, &sysfs_dir_inode_operations, sizeof(*i_op));
870 i_op->follow_link = follow_link;
872 /* Locking of inode->i_op?
873 * Since setting i_op is a single word write and they
874 * are atomic we should be ok here.
881 * sysfs_create_shadow_dir - create a shadow directory for an object.
882 * @kobj: object we're creating directory for.
884 * sysfs_make_shadowed_dir must already have been called on this
888 struct dentry *sysfs_create_shadow_dir(struct kobject *kobj)
890 struct dentry *dir = kobj->dentry;
891 struct inode *inode = dir->d_inode;
892 struct dentry *parent = dir->d_parent;
893 struct sysfs_dirent *parent_sd = parent->d_fsdata;
894 struct dentry *shadow;
895 struct sysfs_dirent *sd;
897 shadow = ERR_PTR(-EINVAL);
898 if (!sysfs_is_shadowed_inode(inode))
901 shadow = d_alloc(parent, &dir->d_name);
905 sd = sysfs_new_dirent("_SHADOW_", inode->i_mode, SYSFS_DIR);
908 sd->s_elem.dir.kobj = kobj;
909 /* point to parent_sd but don't attach to it */
910 sd->s_parent = sysfs_get(parent_sd);
911 sysfs_attach_dirent(sd, NULL, shadow);
913 d_instantiate(shadow, igrab(inode));
915 inc_nlink(parent->d_inode);
917 dget(shadow); /* Extra count - pin the dentry in core */
923 shadow = ERR_PTR(-ENOMEM);
928 * sysfs_remove_shadow_dir - remove an object's directory.
929 * @shadow: dentry of shadow directory
931 * The only thing special about this is that we remove any files in
932 * the directory before we remove the directory, and we've inlined
933 * what used to be sysfs_rmdir() below, instead of calling separately.
936 void sysfs_remove_shadow_dir(struct dentry *shadow)
938 __sysfs_remove_dir(shadow);
941 const struct file_operations sysfs_dir_operations = {
942 .open = sysfs_dir_open,
943 .release = sysfs_dir_close,
944 .llseek = sysfs_dir_lseek,
945 .read = generic_read_dir,
946 .readdir = sysfs_readdir,