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 DEFINE_MUTEX(sysfs_mutex);
18 spinlock_t sysfs_assoc_lock = SPIN_LOCK_UNLOCKED;
20 static spinlock_t sysfs_ino_lock = SPIN_LOCK_UNLOCKED;
21 static DEFINE_IDA(sysfs_ino_ida);
24 * sysfs_link_sibling - link sysfs_dirent into sibling list
25 * @sd: sysfs_dirent of interest
27 * Link @sd into its sibling list which starts from
28 * sd->s_parent->s_children.
31 * mutex_lock(sysfs_mutex)
33 void sysfs_link_sibling(struct sysfs_dirent *sd)
35 struct sysfs_dirent *parent_sd = sd->s_parent;
37 BUG_ON(sd->s_sibling);
38 sd->s_sibling = parent_sd->s_children;
39 parent_sd->s_children = sd;
43 * sysfs_unlink_sibling - unlink sysfs_dirent from sibling list
44 * @sd: sysfs_dirent of interest
46 * Unlink @sd from its sibling list which starts from
47 * sd->s_parent->s_children.
50 * mutex_lock(sysfs_mutex)
52 void sysfs_unlink_sibling(struct sysfs_dirent *sd)
54 struct sysfs_dirent **pos;
56 for (pos = &sd->s_parent->s_children; *pos; pos = &(*pos)->s_sibling) {
66 * sysfs_get_active - get an active reference to sysfs_dirent
67 * @sd: sysfs_dirent to get an active reference to
69 * Get an active reference of @sd. This function is noop if @sd
73 * Pointer to @sd on success, NULL on failure.
75 struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd)
83 v = atomic_read(&sd->s_active);
87 t = atomic_cmpxchg(&sd->s_active, v, v + 1);
98 * sysfs_put_active - put an active reference to sysfs_dirent
99 * @sd: sysfs_dirent to put an active reference to
101 * Put an active reference to @sd. This function is noop if @sd
104 void sysfs_put_active(struct sysfs_dirent *sd)
106 struct completion *cmpl;
112 v = atomic_dec_return(&sd->s_active);
113 if (likely(v != SD_DEACTIVATED_BIAS))
116 /* atomic_dec_return() is a mb(), we'll always see the updated
119 cmpl = (void *)sd->s_sibling;
124 * sysfs_get_active_two - get active references to sysfs_dirent and parent
125 * @sd: sysfs_dirent of interest
127 * Get active reference to @sd and its parent. Parent's active
128 * reference is grabbed first. This function is noop if @sd is
132 * Pointer to @sd on success, NULL on failure.
134 struct sysfs_dirent *sysfs_get_active_two(struct sysfs_dirent *sd)
137 if (sd->s_parent && unlikely(!sysfs_get_active(sd->s_parent)))
139 if (unlikely(!sysfs_get_active(sd))) {
140 sysfs_put_active(sd->s_parent);
148 * sysfs_put_active_two - put active references to sysfs_dirent and parent
149 * @sd: sysfs_dirent of interest
151 * Put active references to @sd and its parent. This function is
152 * noop if @sd is NULL.
154 void sysfs_put_active_two(struct sysfs_dirent *sd)
157 sysfs_put_active(sd);
158 sysfs_put_active(sd->s_parent);
163 * sysfs_deactivate - deactivate sysfs_dirent
164 * @sd: sysfs_dirent to deactivate
166 * Deny new active references and drain existing ones.
168 static void sysfs_deactivate(struct sysfs_dirent *sd)
170 DECLARE_COMPLETION_ONSTACK(wait);
173 BUG_ON(sd->s_sibling || !(sd->s_flags & SYSFS_FLAG_REMOVED));
174 sd->s_sibling = (void *)&wait;
176 /* atomic_add_return() is a mb(), put_active() will always see
177 * the updated sd->s_sibling.
179 v = atomic_add_return(SD_DEACTIVATED_BIAS, &sd->s_active);
181 if (v != SD_DEACTIVATED_BIAS)
182 wait_for_completion(&wait);
184 sd->s_sibling = NULL;
187 static int sysfs_alloc_ino(ino_t *pino)
192 spin_lock(&sysfs_ino_lock);
193 rc = ida_get_new_above(&sysfs_ino_ida, 2, &ino);
194 spin_unlock(&sysfs_ino_lock);
197 if (ida_pre_get(&sysfs_ino_ida, GFP_KERNEL))
206 static void sysfs_free_ino(ino_t ino)
208 spin_lock(&sysfs_ino_lock);
209 ida_remove(&sysfs_ino_ida, ino);
210 spin_unlock(&sysfs_ino_lock);
213 void release_sysfs_dirent(struct sysfs_dirent * sd)
215 struct sysfs_dirent *parent_sd;
218 /* Moving/renaming is always done while holding reference.
219 * sd->s_parent won't change beneath us.
221 parent_sd = sd->s_parent;
223 if (sysfs_type(sd) == SYSFS_KOBJ_LINK)
224 sysfs_put(sd->s_elem.symlink.target_sd);
225 if (sysfs_type(sd) & SYSFS_COPY_NAME)
228 sysfs_free_ino(sd->s_ino);
229 kmem_cache_free(sysfs_dir_cachep, sd);
232 if (sd && atomic_dec_and_test(&sd->s_count))
236 static void sysfs_d_iput(struct dentry * dentry, struct inode * inode)
238 struct sysfs_dirent * sd = dentry->d_fsdata;
241 /* sd->s_dentry is protected with sysfs_assoc_lock.
242 * This allows sysfs_drop_dentry() to dereference it.
244 spin_lock(&sysfs_assoc_lock);
246 /* The dentry might have been deleted or another
247 * lookup could have happened updating sd->s_dentry to
248 * point the new dentry. Ignore if it isn't pointing
251 if (sd->s_dentry == dentry)
253 spin_unlock(&sysfs_assoc_lock);
259 static struct dentry_operations sysfs_dentry_ops = {
260 .d_iput = sysfs_d_iput,
263 struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type)
265 char *dup_name = NULL;
266 struct sysfs_dirent *sd = NULL;
268 if (type & SYSFS_COPY_NAME) {
269 name = dup_name = kstrdup(name, GFP_KERNEL);
274 sd = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL);
278 if (sysfs_alloc_ino(&sd->s_ino))
281 atomic_set(&sd->s_count, 1);
282 atomic_set(&sd->s_active, 0);
283 atomic_set(&sd->s_event, 1);
293 kmem_cache_free(sysfs_dir_cachep, sd);
298 * sysfs_attach_dentry - associate sysfs_dirent with dentry
299 * @sd: target sysfs_dirent
300 * @dentry: dentry to associate
302 * Associate @sd with @dentry. This is protected by
303 * sysfs_assoc_lock to avoid race with sysfs_d_iput().
306 * mutex_lock(sysfs_mutex)
308 static void sysfs_attach_dentry(struct sysfs_dirent *sd, struct dentry *dentry)
310 dentry->d_op = &sysfs_dentry_ops;
311 dentry->d_fsdata = sysfs_get(sd);
313 /* protect sd->s_dentry against sysfs_d_iput */
314 spin_lock(&sysfs_assoc_lock);
315 sd->s_dentry = dentry;
316 spin_unlock(&sysfs_assoc_lock);
321 static int sysfs_ilookup_test(struct inode *inode, void *arg)
323 struct sysfs_dirent *sd = arg;
324 return inode->i_ino == sd->s_ino;
328 * sysfs_addrm_start - prepare for sysfs_dirent add/remove
329 * @acxt: pointer to sysfs_addrm_cxt to be used
330 * @parent_sd: parent sysfs_dirent
332 * This function is called when the caller is about to add or
333 * remove sysfs_dirent under @parent_sd. This function acquires
334 * sysfs_mutex, grabs inode for @parent_sd if available and lock
335 * i_mutex of it. @acxt is used to keep and pass context to
336 * other addrm functions.
339 * Kernel thread context (may sleep). sysfs_mutex is locked on
340 * return. i_mutex of parent inode is locked on return if
343 void sysfs_addrm_start(struct sysfs_addrm_cxt *acxt,
344 struct sysfs_dirent *parent_sd)
348 memset(acxt, 0, sizeof(*acxt));
349 acxt->parent_sd = parent_sd;
351 /* Lookup parent inode. inode initialization and I_NEW
352 * clearing are protected by sysfs_mutex. By grabbing it and
353 * looking up with _nowait variant, inode state can be
354 * determined reliably.
356 mutex_lock(&sysfs_mutex);
358 inode = ilookup5_nowait(sysfs_sb, parent_sd->s_ino, sysfs_ilookup_test,
361 if (inode && !(inode->i_state & I_NEW)) {
362 /* parent inode available */
363 acxt->parent_inode = inode;
365 /* sysfs_mutex is below i_mutex in lock hierarchy.
366 * First, trylock i_mutex. If fails, unlock
367 * sysfs_mutex and lock them in order.
369 if (!mutex_trylock(&inode->i_mutex)) {
370 mutex_unlock(&sysfs_mutex);
371 mutex_lock(&inode->i_mutex);
372 mutex_lock(&sysfs_mutex);
379 * sysfs_add_one - add sysfs_dirent to parent
380 * @acxt: addrm context to use
381 * @sd: sysfs_dirent to be added
383 * Get @acxt->parent_sd and set sd->s_parent to it and increment
384 * nlink of parent inode if @sd is a directory. @sd is NOT
385 * linked into the children list of the parent. The caller
386 * should invoke sysfs_link_sibling() after this function
387 * completes if @sd needs to be on the children list.
389 * This function should be called between calls to
390 * sysfs_addrm_start() and sysfs_addrm_finish() and should be
391 * passed the same @acxt as passed to sysfs_addrm_start().
394 * Determined by sysfs_addrm_start().
396 void sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
398 sd->s_parent = sysfs_get(acxt->parent_sd);
400 if (sysfs_type(sd) == SYSFS_DIR && acxt->parent_inode)
401 inc_nlink(acxt->parent_inode);
407 * sysfs_remove_one - remove sysfs_dirent from parent
408 * @acxt: addrm context to use
409 * @sd: sysfs_dirent to be added
411 * Mark @sd removed and drop nlink of parent inode if @sd is a
412 * directory. @sd is NOT unlinked from the children list of the
413 * parent. The caller is repsonsible for removing @sd from the
414 * children list before calling this function.
416 * This function should be called between calls to
417 * sysfs_addrm_start() and sysfs_addrm_finish() and should be
418 * passed the same @acxt as passed to sysfs_addrm_start().
421 * Determined by sysfs_addrm_start().
423 void sysfs_remove_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
425 BUG_ON(sd->s_sibling || (sd->s_flags & SYSFS_FLAG_REMOVED));
427 sd->s_flags |= SYSFS_FLAG_REMOVED;
428 sd->s_sibling = acxt->removed;
431 if (sysfs_type(sd) == SYSFS_DIR && acxt->parent_inode)
432 drop_nlink(acxt->parent_inode);
438 * sysfs_drop_dentry - drop dentry for the specified sysfs_dirent
439 * @sd: target sysfs_dirent
441 * Drop dentry for @sd. @sd must have been unlinked from its
442 * parent on entry to this function such that it can't be looked
445 * @sd->s_dentry which is protected with sysfs_assoc_lock points
446 * to the currently associated dentry but we're not holding a
447 * reference to it and racing with dput(). Grab dcache_lock and
448 * verify dentry before dropping it. If @sd->s_dentry is NULL or
449 * dput() beats us, no need to bother.
451 static void sysfs_drop_dentry(struct sysfs_dirent *sd)
453 struct dentry *dentry = NULL;
456 /* We're not holding a reference to ->s_dentry dentry but the
457 * field will stay valid as long as sysfs_assoc_lock is held.
459 spin_lock(&sysfs_assoc_lock);
460 spin_lock(&dcache_lock);
462 /* drop dentry if it's there and dput() didn't kill it yet */
463 if (sd->s_dentry && sd->s_dentry->d_inode) {
464 dentry = dget_locked(sd->s_dentry);
465 spin_lock(&dentry->d_lock);
467 spin_unlock(&dentry->d_lock);
470 spin_unlock(&dcache_lock);
471 spin_unlock(&sysfs_assoc_lock);
474 /* XXX: unpin if directory, this will go away soon */
475 if (sysfs_type(sd) == SYSFS_DIR)
478 /* adjust nlink and update timestamp */
479 inode = ilookup(sysfs_sb, sd->s_ino);
481 mutex_lock(&inode->i_mutex);
483 inode->i_ctime = CURRENT_TIME;
485 if (sysfs_type(sd) == SYSFS_DIR)
488 mutex_unlock(&inode->i_mutex);
494 * sysfs_addrm_finish - finish up sysfs_dirent add/remove
495 * @acxt: addrm context to finish up
497 * Finish up sysfs_dirent add/remove. Resources acquired by
498 * sysfs_addrm_start() are released and removed sysfs_dirents are
499 * cleaned up. Timestamps on the parent inode are updated.
502 * All mutexes acquired by sysfs_addrm_start() are released.
505 * Number of added/removed sysfs_dirents since sysfs_addrm_start().
507 int sysfs_addrm_finish(struct sysfs_addrm_cxt *acxt)
509 /* release resources acquired by sysfs_addrm_start() */
510 mutex_unlock(&sysfs_mutex);
511 if (acxt->parent_inode) {
512 struct inode *inode = acxt->parent_inode;
514 /* if added/removed, update timestamps on the parent */
516 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
518 mutex_unlock(&inode->i_mutex);
522 /* kill removed sysfs_dirents */
523 while (acxt->removed) {
524 struct sysfs_dirent *sd = acxt->removed;
526 acxt->removed = sd->s_sibling;
527 sd->s_sibling = NULL;
529 sysfs_drop_dentry(sd);
530 sysfs_deactivate(sd);
538 * sysfs_find_dirent - find sysfs_dirent with the given name
539 * @parent_sd: sysfs_dirent to search under
540 * @name: name to look for
542 * Look for sysfs_dirent with name @name under @parent_sd.
545 * mutex_lock(sysfs_mutex)
548 * Pointer to sysfs_dirent if found, NULL if not.
550 struct sysfs_dirent *sysfs_find_dirent(struct sysfs_dirent *parent_sd,
551 const unsigned char *name)
553 struct sysfs_dirent *sd;
555 for (sd = parent_sd->s_children; sd; sd = sd->s_sibling)
556 if (sysfs_type(sd) && !strcmp(sd->s_name, name))
562 * sysfs_get_dirent - find and get sysfs_dirent with the given name
563 * @parent_sd: sysfs_dirent to search under
564 * @name: name to look for
566 * Look for sysfs_dirent with name @name under @parent_sd and get
570 * Kernel thread context (may sleep). Grabs sysfs_mutex.
573 * Pointer to sysfs_dirent if found, NULL if not.
575 struct sysfs_dirent *sysfs_get_dirent(struct sysfs_dirent *parent_sd,
576 const unsigned char *name)
578 struct sysfs_dirent *sd;
580 mutex_lock(&sysfs_mutex);
581 sd = sysfs_find_dirent(parent_sd, name);
583 mutex_unlock(&sysfs_mutex);
588 static int create_dir(struct kobject *kobj, struct sysfs_dirent *parent_sd,
589 const char *name, struct sysfs_dirent **p_sd)
591 struct dentry *parent = parent_sd->s_dentry;
592 struct sysfs_addrm_cxt acxt;
594 umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
595 struct dentry *dentry;
597 struct sysfs_dirent *sd;
599 sysfs_addrm_start(&acxt, parent_sd);
602 dentry = lookup_one_len(name, parent, strlen(name));
603 if (IS_ERR(dentry)) {
604 error = PTR_ERR(dentry);
613 sd = sysfs_new_dirent(name, mode, SYSFS_DIR);
616 sd->s_elem.dir.kobj = kobj;
618 inode = sysfs_get_inode(sd);
622 if (inode->i_state & I_NEW) {
623 inode->i_op = &sysfs_dir_inode_operations;
624 inode->i_fop = &sysfs_dir_operations;
625 /* directory inodes start off with i_nlink == 2 (for ".") */
631 if (sysfs_find_dirent(parent_sd, name))
634 sysfs_add_one(&acxt, sd);
635 sysfs_link_sibling(sd);
636 sysfs_instantiate(dentry, inode);
637 sysfs_attach_dentry(sd, dentry);
641 goto out_finish; /* pin directory dentry in core */
652 sysfs_addrm_finish(&acxt);
656 int sysfs_create_subdir(struct kobject *kobj, const char *name,
657 struct sysfs_dirent **p_sd)
659 return create_dir(kobj, kobj->sd, name, p_sd);
663 * sysfs_create_dir - create a directory for an object.
664 * @kobj: object we're creating directory for.
665 * @shadow_parent: parent object.
667 int sysfs_create_dir(struct kobject *kobj,
668 struct sysfs_dirent *shadow_parent_sd)
670 struct sysfs_dirent *parent_sd, *sd;
675 if (shadow_parent_sd)
676 parent_sd = shadow_parent_sd;
677 else if (kobj->parent)
678 parent_sd = kobj->parent->sd;
679 else if (sysfs_mount && sysfs_mount->mnt_sb)
680 parent_sd = sysfs_mount->mnt_sb->s_root->d_fsdata;
684 error = create_dir(kobj, parent_sd, kobject_name(kobj), &sd);
690 static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
691 struct nameidata *nd)
693 struct sysfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
694 struct sysfs_dirent * sd;
695 struct bin_attribute *bin_attr;
699 for (sd = parent_sd->s_children; sd; sd = sd->s_sibling) {
700 if ((sysfs_type(sd) & SYSFS_NOT_PINNED) &&
701 !strcmp(sd->s_name, dentry->d_name.name)) {
711 /* attach dentry and inode */
712 inode = sysfs_get_inode(sd);
714 return ERR_PTR(-ENOMEM);
716 mutex_lock(&sysfs_mutex);
718 if (inode->i_state & I_NEW) {
719 /* initialize inode according to type */
720 switch (sysfs_type(sd)) {
721 case SYSFS_KOBJ_ATTR:
722 inode->i_size = PAGE_SIZE;
723 inode->i_fop = &sysfs_file_operations;
725 case SYSFS_KOBJ_BIN_ATTR:
726 bin_attr = sd->s_elem.bin_attr.bin_attr;
727 inode->i_size = bin_attr->size;
728 inode->i_fop = &bin_fops;
730 case SYSFS_KOBJ_LINK:
731 inode->i_op = &sysfs_symlink_inode_operations;
738 sysfs_instantiate(dentry, inode);
739 sysfs_attach_dentry(sd, dentry);
741 mutex_unlock(&sysfs_mutex);
746 const struct inode_operations sysfs_dir_inode_operations = {
747 .lookup = sysfs_lookup,
748 .setattr = sysfs_setattr,
751 static void remove_dir(struct sysfs_dirent *sd)
753 struct sysfs_addrm_cxt acxt;
755 sysfs_addrm_start(&acxt, sd->s_parent);
756 sysfs_unlink_sibling(sd);
757 sysfs_remove_one(&acxt, sd);
758 sysfs_addrm_finish(&acxt);
761 void sysfs_remove_subdir(struct sysfs_dirent *sd)
767 static void __sysfs_remove_dir(struct sysfs_dirent *dir_sd)
769 struct sysfs_addrm_cxt acxt;
770 struct sysfs_dirent **pos;
775 pr_debug("sysfs %s: removing dir\n", dir_sd->s_name);
776 sysfs_addrm_start(&acxt, dir_sd);
777 pos = &dir_sd->s_children;
779 struct sysfs_dirent *sd = *pos;
781 if (sysfs_type(sd) && (sysfs_type(sd) & SYSFS_NOT_PINNED)) {
782 *pos = sd->s_sibling;
783 sd->s_sibling = NULL;
784 sysfs_remove_one(&acxt, sd);
786 pos = &(*pos)->s_sibling;
788 sysfs_addrm_finish(&acxt);
794 * sysfs_remove_dir - remove an object's directory.
797 * The only thing special about this is that we remove any files in
798 * the directory before we remove the directory, and we've inlined
799 * what used to be sysfs_rmdir() below, instead of calling separately.
802 void sysfs_remove_dir(struct kobject * kobj)
804 struct sysfs_dirent *sd = kobj->sd;
806 spin_lock(&sysfs_assoc_lock);
808 spin_unlock(&sysfs_assoc_lock);
810 __sysfs_remove_dir(sd);
813 int sysfs_rename_dir(struct kobject *kobj, struct sysfs_dirent *new_parent_sd,
814 const char *new_name)
816 struct sysfs_dirent *sd = kobj->sd;
817 struct dentry *new_parent = new_parent_sd->s_dentry;
818 struct dentry *new_dentry;
825 mutex_lock(&new_parent->d_inode->i_mutex);
827 new_dentry = lookup_one_len(new_name, new_parent, strlen(new_name));
828 if (IS_ERR(new_dentry)) {
829 error = PTR_ERR(new_dentry);
833 /* By allowing two different directories with the same
834 * d_parent we allow this routine to move between different
835 * shadows of the same directory
838 if (sd->s_parent->s_dentry->d_inode != new_parent->d_inode ||
839 new_dentry->d_parent->d_inode != new_parent->d_inode ||
840 new_dentry == sd->s_dentry)
844 if (new_dentry->d_inode)
847 /* rename kobject and sysfs_dirent */
849 new_name = dup_name = kstrdup(new_name, GFP_KERNEL);
853 error = kobject_set_name(kobj, "%s", new_name);
858 sd->s_name = new_name;
860 /* move under the new parent */
861 d_add(new_dentry, NULL);
862 d_move(sd->s_dentry, new_dentry);
864 mutex_lock(&sysfs_mutex);
866 sysfs_unlink_sibling(sd);
867 sysfs_get(new_parent_sd);
868 sysfs_put(sd->s_parent);
869 sd->s_parent = new_parent_sd;
870 sysfs_link_sibling(sd);
872 mutex_unlock(&sysfs_mutex);
884 mutex_unlock(&new_parent->d_inode->i_mutex);
888 int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent)
890 struct dentry *old_parent_dentry, *new_parent_dentry, *new_dentry;
891 struct sysfs_dirent *new_parent_sd, *sd;
894 old_parent_dentry = kobj->parent ?
895 kobj->parent->sd->s_dentry : sysfs_mount->mnt_sb->s_root;
896 new_parent_dentry = new_parent ?
897 new_parent->sd->s_dentry : sysfs_mount->mnt_sb->s_root;
899 if (old_parent_dentry->d_inode == new_parent_dentry->d_inode)
900 return 0; /* nothing to move */
902 mutex_lock(&old_parent_dentry->d_inode->i_mutex);
903 if (!mutex_trylock(&new_parent_dentry->d_inode->i_mutex)) {
904 mutex_unlock(&old_parent_dentry->d_inode->i_mutex);
908 new_parent_sd = new_parent_dentry->d_fsdata;
911 new_dentry = lookup_one_len(kobj->name, new_parent_dentry,
913 if (IS_ERR(new_dentry)) {
914 error = PTR_ERR(new_dentry);
918 d_add(new_dentry, NULL);
919 d_move(sd->s_dentry, new_dentry);
922 /* Remove from old parent's list and insert into new parent's list. */
923 mutex_lock(&sysfs_mutex);
925 sysfs_unlink_sibling(sd);
926 sysfs_get(new_parent_sd);
927 sysfs_put(sd->s_parent);
928 sd->s_parent = new_parent_sd;
929 sysfs_link_sibling(sd);
931 mutex_unlock(&sysfs_mutex);
933 mutex_unlock(&new_parent_dentry->d_inode->i_mutex);
934 mutex_unlock(&old_parent_dentry->d_inode->i_mutex);
939 static int sysfs_dir_open(struct inode *inode, struct file *file)
941 struct dentry * dentry = file->f_path.dentry;
942 struct sysfs_dirent * parent_sd = dentry->d_fsdata;
943 struct sysfs_dirent * sd;
945 sd = sysfs_new_dirent("_DIR_", 0, 0);
947 mutex_lock(&sysfs_mutex);
948 sd->s_parent = sysfs_get(parent_sd);
949 sysfs_link_sibling(sd);
950 mutex_unlock(&sysfs_mutex);
953 file->private_data = sd;
954 return sd ? 0 : -ENOMEM;
957 static int sysfs_dir_close(struct inode *inode, struct file *file)
959 struct sysfs_dirent * cursor = file->private_data;
961 mutex_lock(&sysfs_mutex);
962 sysfs_unlink_sibling(cursor);
963 mutex_unlock(&sysfs_mutex);
965 release_sysfs_dirent(cursor);
970 /* Relationship between s_mode and the DT_xxx types */
971 static inline unsigned char dt_type(struct sysfs_dirent *sd)
973 return (sd->s_mode >> 12) & 15;
976 static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
978 struct dentry *dentry = filp->f_path.dentry;
979 struct sysfs_dirent * parent_sd = dentry->d_fsdata;
980 struct sysfs_dirent *cursor = filp->private_data;
981 struct sysfs_dirent **pos;
987 ino = parent_sd->s_ino;
988 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
994 if (parent_sd->s_parent)
995 ino = parent_sd->s_parent->s_ino;
997 ino = parent_sd->s_ino;
998 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1004 mutex_lock(&sysfs_mutex);
1006 pos = &parent_sd->s_children;
1007 while (*pos != cursor)
1008 pos = &(*pos)->s_sibling;
1011 *pos = cursor->s_sibling;
1013 if (filp->f_pos == 2)
1014 pos = &parent_sd->s_children;
1016 for ( ; *pos; pos = &(*pos)->s_sibling) {
1017 struct sysfs_dirent *next = *pos;
1021 if (!sysfs_type(next))
1024 name = next->s_name;
1028 if (filldir(dirent, name, len, filp->f_pos, ino,
1035 /* put cursor back in */
1036 cursor->s_sibling = *pos;
1039 mutex_unlock(&sysfs_mutex);
1044 static loff_t sysfs_dir_lseek(struct file * file, loff_t offset, int origin)
1046 struct dentry * dentry = file->f_path.dentry;
1050 offset += file->f_pos;
1057 if (offset != file->f_pos) {
1058 mutex_lock(&sysfs_mutex);
1060 file->f_pos = offset;
1061 if (file->f_pos >= 2) {
1062 struct sysfs_dirent *sd = dentry->d_fsdata;
1063 struct sysfs_dirent *cursor = file->private_data;
1064 struct sysfs_dirent **pos;
1065 loff_t n = file->f_pos - 2;
1067 sysfs_unlink_sibling(cursor);
1069 pos = &sd->s_children;
1071 struct sysfs_dirent *next = *pos;
1072 if (sysfs_type(next))
1074 pos = &(*pos)->s_sibling;
1077 cursor->s_sibling = *pos;
1081 mutex_unlock(&sysfs_mutex);
1089 * sysfs_make_shadowed_dir - Setup so a directory can be shadowed
1090 * @kobj: object we're creating shadow of.
1093 int sysfs_make_shadowed_dir(struct kobject *kobj,
1094 void * (*follow_link)(struct dentry *, struct nameidata *))
1096 struct inode *inode;
1097 struct inode_operations *i_op;
1099 inode = kobj->sd->s_dentry->d_inode;
1100 if (inode->i_op != &sysfs_dir_inode_operations)
1103 i_op = kmalloc(sizeof(*i_op), GFP_KERNEL);
1107 memcpy(i_op, &sysfs_dir_inode_operations, sizeof(*i_op));
1108 i_op->follow_link = follow_link;
1110 /* Locking of inode->i_op?
1111 * Since setting i_op is a single word write and they
1112 * are atomic we should be ok here.
1119 * sysfs_create_shadow_dir - create a shadow directory for an object.
1120 * @kobj: object we're creating directory for.
1122 * sysfs_make_shadowed_dir must already have been called on this
1126 struct sysfs_dirent *sysfs_create_shadow_dir(struct kobject *kobj)
1128 struct dentry *dir = kobj->sd->s_dentry;
1129 struct inode *inode = dir->d_inode;
1130 struct dentry *parent = dir->d_parent;
1131 struct sysfs_dirent *parent_sd = parent->d_fsdata;
1132 struct dentry *shadow;
1133 struct sysfs_dirent *sd;
1134 struct sysfs_addrm_cxt acxt;
1136 sd = ERR_PTR(-EINVAL);
1137 if (!sysfs_is_shadowed_inode(inode))
1140 shadow = d_alloc(parent, &dir->d_name);
1144 sd = sysfs_new_dirent("_SHADOW_", inode->i_mode, SYSFS_DIR);
1147 sd->s_elem.dir.kobj = kobj;
1149 sysfs_addrm_start(&acxt, parent_sd);
1151 /* add but don't link into children list */
1152 sysfs_add_one(&acxt, sd);
1154 /* attach and instantiate dentry */
1155 sysfs_attach_dentry(sd, shadow);
1156 d_instantiate(shadow, igrab(inode));
1157 inc_nlink(inode); /* tj: synchronization? */
1159 sysfs_addrm_finish(&acxt);
1161 dget(shadow); /* Extra count - pin the dentry in core */
1167 sd = ERR_PTR(-ENOMEM);
1172 * sysfs_remove_shadow_dir - remove an object's directory.
1173 * @shadow_sd: sysfs_dirent of shadow directory
1175 * The only thing special about this is that we remove any files in
1176 * the directory before we remove the directory, and we've inlined
1177 * what used to be sysfs_rmdir() below, instead of calling separately.
1180 void sysfs_remove_shadow_dir(struct sysfs_dirent *shadow_sd)
1182 __sysfs_remove_dir(shadow_sd);
1185 const struct file_operations sysfs_dir_operations = {
1186 .open = sysfs_dir_open,
1187 .release = sysfs_dir_close,
1188 .llseek = sysfs_dir_lseek,
1189 .read = generic_read_dir,
1190 .readdir = sysfs_readdir,