@@ -240,10 +240,31 @@ static void sysfs_free_ino(unsigned int ino)
240240 spin_unlock (& sysfs_ino_lock );
241241}
242242
243- void release_sysfs_dirent (struct sysfs_dirent * sd )
243+ /**
244+ * kernfs_get - get a reference count on a sysfs_dirent
245+ * @sd: the target sysfs_dirent
246+ */
247+ void kernfs_get (struct sysfs_dirent * sd )
248+ {
249+ if (sd ) {
250+ WARN_ON (!atomic_read (& sd -> s_count ));
251+ atomic_inc (& sd -> s_count );
252+ }
253+ }
254+ EXPORT_SYMBOL_GPL (kernfs_get );
255+
256+ /**
257+ * kernfs_put - put a reference count on a sysfs_dirent
258+ * @sd: the target sysfs_dirent
259+ *
260+ * Put a reference count of @sd and destroy it if it reached zero.
261+ */
262+ void kernfs_put (struct sysfs_dirent * sd )
244263{
245264 struct sysfs_dirent * parent_sd ;
246265
266+ if (!sd || !atomic_dec_and_test (& sd -> s_count ))
267+ return ;
247268 repeat :
248269 /* Moving/renaming is always done while holding reference.
249270 * sd->s_parent won't change beneath us.
@@ -255,7 +276,7 @@ void release_sysfs_dirent(struct sysfs_dirent *sd)
255276 parent_sd ? parent_sd -> s_name : "" , sd -> s_name );
256277
257278 if (sysfs_type (sd ) == SYSFS_KOBJ_LINK )
258- sysfs_put (sd -> s_symlink .target_sd );
279+ kernfs_put (sd -> s_symlink .target_sd );
259280 if (sysfs_type (sd ) & SYSFS_COPY_NAME )
260281 kfree (sd -> s_name );
261282 if (sd -> s_iattr && sd -> s_iattr -> ia_secdata )
@@ -269,6 +290,7 @@ void release_sysfs_dirent(struct sysfs_dirent *sd)
269290 if (sd && atomic_dec_and_test (& sd -> s_count ))
270291 goto repeat ;
271292}
293+ EXPORT_SYMBOL_GPL (kernfs_put );
272294
273295static int sysfs_dentry_delete (const struct dentry * dentry )
274296{
@@ -331,7 +353,7 @@ static int sysfs_dentry_revalidate(struct dentry *dentry, unsigned int flags)
331353
332354static void sysfs_dentry_release (struct dentry * dentry )
333355{
334- sysfs_put (dentry -> d_fsdata );
356+ kernfs_put (dentry -> d_fsdata );
335357}
336358
337359const struct dentry_operations sysfs_dentry_ops = {
@@ -433,7 +455,8 @@ int sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd,
433455 return - EINVAL ;
434456
435457 sd -> s_hash = sysfs_name_hash (sd -> s_name , sd -> s_ns );
436- sd -> s_parent = sysfs_get (parent_sd );
458+ sd -> s_parent = parent_sd ;
459+ kernfs_get (parent_sd );
437460
438461 ret = sysfs_link_sibling (sd );
439462 if (ret )
@@ -553,36 +576,33 @@ void sysfs_addrm_finish(struct sysfs_addrm_cxt *acxt)
553576
554577 sysfs_deactivate (sd );
555578 sysfs_unmap_bin_file (sd );
556- sysfs_put (sd );
579+ kernfs_put (sd );
557580 }
558581}
559582
560583/**
561- * sysfs_find_dirent - find sysfs_dirent with the given name
562- * @parent_sd: sysfs_dirent to search under
563- * @name: name to look for
564- * @ns: the namespace tag to use
565- *
566- * Look for sysfs_dirent with name @name under @parent_sd.
567- *
568- * LOCKING:
569- * mutex_lock(sysfs_mutex)
584+ * kernfs_find_ns - find sysfs_dirent with the given name
585+ * @parent: sysfs_dirent to search under
586+ * @name: name to look for
587+ * @ns: the namespace tag to use
570588 *
571- * RETURNS:
572- * Pointer to sysfs_dirent if found, NULL if not .
589+ * Look for sysfs_dirent with name @name under @parent. Returns pointer to
590+ * the found sysfs_dirent on success, % NULL on failure .
573591 */
574- struct sysfs_dirent * sysfs_find_dirent (struct sysfs_dirent * parent_sd ,
575- const unsigned char * name ,
576- const void * ns )
592+ static struct sysfs_dirent * kernfs_find_ns (struct sysfs_dirent * parent ,
593+ const unsigned char * name ,
594+ const void * ns )
577595{
578- struct rb_node * node = parent_sd -> s_dir .children .rb_node ;
579- bool has_ns = parent_sd -> s_flags & SYSFS_FLAG_NS ;
596+ struct rb_node * node = parent -> s_dir .children .rb_node ;
597+ bool has_ns = parent -> s_flags & SYSFS_FLAG_NS ;
580598 unsigned int hash ;
581599
600+ lockdep_assert_held (& sysfs_mutex );
601+
582602 if (has_ns != (bool )ns ) {
583603 WARN (1 , KERN_WARNING "sysfs: ns %s in '%s' for '%s'\n" ,
584604 has_ns ? "required" : "invalid" ,
585- parent_sd -> s_name , name );
605+ parent -> s_name , name );
586606 return NULL ;
587607 }
588608
@@ -604,34 +624,28 @@ struct sysfs_dirent *sysfs_find_dirent(struct sysfs_dirent *parent_sd,
604624}
605625
606626/**
607- * sysfs_get_dirent_ns - find and get sysfs_dirent with the given name
608- * @parent_sd: sysfs_dirent to search under
609- * @name: name to look for
610- * @ns: the namespace tag to use
611- *
612- * Look for sysfs_dirent with name @name under @parent_sd and get
613- * it if found.
614- *
615- * LOCKING:
616- * Kernel thread context (may sleep). Grabs sysfs_mutex.
627+ * kernfs_find_and_get_ns - find and get sysfs_dirent with the given name
628+ * @parent: sysfs_dirent to search under
629+ * @name: name to look for
630+ * @ns: the namespace tag to use
617631 *
618- * RETURNS:
619- * Pointer to sysfs_dirent if found, NULL if not.
632+ * Look for sysfs_dirent with name @name under @parent and get a reference
633+ * if found. This function may sleep and returns pointer to the found
634+ * sysfs_dirent on success, %NULL on failure.
620635 */
621- struct sysfs_dirent * sysfs_get_dirent_ns (struct sysfs_dirent * parent_sd ,
622- const unsigned char * name ,
623- const void * ns )
636+ struct sysfs_dirent * kernfs_find_and_get_ns (struct sysfs_dirent * parent ,
637+ const char * name , const void * ns )
624638{
625639 struct sysfs_dirent * sd ;
626640
627641 mutex_lock (& sysfs_mutex );
628- sd = sysfs_find_dirent ( parent_sd , name , ns );
629- sysfs_get (sd );
642+ sd = kernfs_find_ns ( parent , name , ns );
643+ kernfs_get (sd );
630644 mutex_unlock (& sysfs_mutex );
631645
632646 return sd ;
633647}
634- EXPORT_SYMBOL_GPL (sysfs_get_dirent_ns );
648+ EXPORT_SYMBOL_GPL (kernfs_find_and_get_ns );
635649
636650/**
637651 * kernfs_create_dir_ns - create a directory
@@ -667,7 +681,7 @@ struct sysfs_dirent *kernfs_create_dir_ns(struct sysfs_dirent *parent,
667681 if (!rc )
668682 return sd ;
669683
670- sysfs_put (sd );
684+ kernfs_put (sd );
671685 return ERR_PTR (rc );
672686}
673687
@@ -716,14 +730,15 @@ static struct dentry *sysfs_lookup(struct inode *dir, struct dentry *dentry,
716730 if (parent_sd -> s_flags & SYSFS_FLAG_NS )
717731 ns = sysfs_info (dir -> i_sb )-> ns ;
718732
719- sd = sysfs_find_dirent (parent_sd , dentry -> d_name .name , ns );
733+ sd = kernfs_find_ns (parent_sd , dentry -> d_name .name , ns );
720734
721735 /* no such entry */
722736 if (!sd ) {
723737 ret = ERR_PTR (- ENOENT );
724738 goto out_unlock ;
725739 }
726- dentry -> d_fsdata = sysfs_get (sd );
740+ kernfs_get (sd );
741+ dentry -> d_fsdata = sd ;
727742
728743 /* attach dentry and inode */
729744 inode = sysfs_get_inode (dir -> i_sb , sd );
@@ -859,7 +874,7 @@ int kernfs_remove_by_name_ns(struct sysfs_dirent *dir_sd, const char *name,
859874
860875 sysfs_addrm_start (& acxt );
861876
862- sd = sysfs_find_dirent (dir_sd , name , ns );
877+ sd = kernfs_find_ns (dir_sd , name , ns );
863878 if (sd )
864879 __kernfs_remove (& acxt , sd );
865880
@@ -925,7 +940,7 @@ int kernfs_rename_ns(struct sysfs_dirent *sd, struct sysfs_dirent *new_parent,
925940 goto out ; /* nothing to rename */
926941
927942 error = - EEXIST ;
928- if (sysfs_find_dirent (new_parent , new_name , new_ns ))
943+ if (kernfs_find_ns (new_parent , new_name , new_ns ))
929944 goto out ;
930945
931946 /* rename sysfs_dirent */
@@ -943,8 +958,8 @@ int kernfs_rename_ns(struct sysfs_dirent *sd, struct sysfs_dirent *new_parent,
943958 * Move to the appropriate place in the appropriate directories rbtree.
944959 */
945960 sysfs_unlink_sibling (sd );
946- sysfs_get (new_parent );
947- sysfs_put (sd -> s_parent );
961+ kernfs_get (new_parent );
962+ kernfs_put (sd -> s_parent );
948963 sd -> s_ns = new_ns ;
949964 sd -> s_hash = sysfs_name_hash (sd -> s_name , sd -> s_ns );
950965 sd -> s_parent = new_parent ;
@@ -1000,7 +1015,7 @@ static inline unsigned char dt_type(struct sysfs_dirent *sd)
10001015
10011016static int sysfs_dir_release (struct inode * inode , struct file * filp )
10021017{
1003- sysfs_put (filp -> private_data );
1018+ kernfs_put (filp -> private_data );
10041019 return 0 ;
10051020}
10061021
@@ -1011,7 +1026,7 @@ static struct sysfs_dirent *sysfs_dir_pos(const void *ns,
10111026 int valid = !(pos -> s_flags & SYSFS_FLAG_REMOVED ) &&
10121027 pos -> s_parent == parent_sd &&
10131028 hash == pos -> s_hash ;
1014- sysfs_put (pos );
1029+ kernfs_put (pos );
10151030 if (!valid )
10161031 pos = NULL ;
10171032 }
@@ -1075,8 +1090,10 @@ static int sysfs_readdir(struct file *file, struct dir_context *ctx)
10751090 unsigned int type = dt_type (pos );
10761091 int len = strlen (name );
10771092 ino_t ino = pos -> s_ino ;
1093+
10781094 ctx -> pos = pos -> s_hash ;
1079- file -> private_data = sysfs_get (pos );
1095+ file -> private_data = pos ;
1096+ kernfs_get (pos );
10801097
10811098 mutex_unlock (& sysfs_mutex );
10821099 if (!dir_emit (ctx , name , len , ino , type ))
0 commit comments