[PATCH 05/22] sysfs: implement sysfs_find_child()

Tejun Heo
2007-09-20T08:11:25Z

Implement sysfs_find_child() which finds a child of a sysfs_dirent by
name. This function does not grab reference of the found child. The
caller is supposed to have reference if the child exists. This
function is useful for callers which own the sysfs_dirent to be looked
up but don't wanna keep a pointer to it.

Signed-off-by: Tejun Heo <htejun@gmail.com>
---
fs/sysfs/dir.c | 33 +++++++++++++++++++++++++++++++++
include/linux/sysfs.h | 9 +++++++++
2 files changed, 42 insertions(+), 0 deletions(-)

diff --git a/fs/sysfs/dir.c b/fs/sysfs/dir.c
index 584f17c..7a6be9a 100644
--- a/fs/sysfs/dir.c
+++ b/fs/sysfs/dir.c
@@ -647,6 +647,39 @@ struct sysfs_dirent *sysfs_get_dirent(struct sysfs_dirent *parent_sd,
return sd;
}

+/**
+ * sysfs_find_child - find sysfs_dirent with the given name
+ * @parent: sysfs_dirent to search under
+ * @name: name to look for
+ *
+ * This is exported version of sysfs_find_dirent(). This
+ * function doesn't grab reference to the found dirent. The
+ * caller must already have a reference to it. This function is
+ * useful for callers which own the sysfs_dirent to be looked up
+ * but don't wanna keep a pointer to it.
+ *
+ * LOCKING:
+ * Kernel thread context (may sleep).
+ *
+ * RETURNS:
+ * Pointer to the looked up sysfs_dirent on success, NULL if
+ * there's no such entry, ERR_PTR(-EBADF) is @parent is ERR_PTR()
+ * value.
+ *
+ */
+struct sysfs_dirent *sysfs_find_child(struct sysfs_dirent *parent,
+ const char *name)
+{
+ struct sysfs_dirent *sd;
+
+ mutex_lock(&sysfs_mutex);
+ sd = sysfs_find_dirent(parent, name);
+ mutex_unlock(&sysfs_mutex);
+
+ return sd;
+}
+EXPORT_SYMBOL_GPL(sysfs_find_child);
+
static int create_dir(struct kobject *kobj, struct sysfs_dirent *parent_sd,
const char *name, struct sysfs_dirent **p_sd)
{
diff --git a/include/linux/sysfs.h b/include/linux/sysfs.h
index 5646e56..f030dc6 100644
--- a/include/linux/sysfs.h
+++ b/include/linux/sysfs.h
@@ -32,10 +32,19 @@ struct vm_area_struct;

#ifdef CONFIG_SYSFS

+struct sysfs_dirent *sysfs_find_child(struct sysfs_dirent *parent,
+ const char *name);
+
int __must_check sysfs_init(void);

#else /* CONFIG_SYSFS */

+static inline struct sysfs_dirent *sysfs_find_child(struct sysfs_dirent *parent,
+ const char *name)
+{
+ return NULL;
+}
+
static inline int __must_check sysfs_init(void)
{
return 0;
--
1.5.0.3