[PATCH net v2 1/3] class: add class_has_file_ns() helper function

From: Taehee Yoo
Date: Sat Apr 04 2020 - 10:19:03 EST


The new helper function is to check whether the class file is existing
or not. This function will be used by networking stack to
check "/sys/class/net/*" file.

Reported-by: syzbot+830c6dbfc71edc4f0b8f@xxxxxxxxxxxxxxxxxxxxxxxxx
Fixes: b76cdba9cdb2 ("[PATCH] bonding: add sysfs functionality to bonding (large)")
Signed-off-by: Taehee Yoo <ap420073@xxxxxxxxx>
---

v1 -> v2:
- Implement class_has_file_ns() instead of class_find_and_get_file_ns().
- Change headline.
- Add kernel documentation comment.

drivers/base/class.c | 22 ++++++++++++++++++++++
include/linux/device/class.h | 3 ++-
2 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/drivers/base/class.c b/drivers/base/class.c
index bcd410e6d70a..a2f2787f6aa7 100644
--- a/drivers/base/class.c
+++ b/drivers/base/class.c
@@ -105,6 +105,28 @@ void class_remove_file_ns(struct class *cls, const struct class_attribute *attr,
sysfs_remove_file_ns(&cls->p->subsys.kobj, &attr->attr, ns);
}

+/**
+ * class_has_file_ns - check whether file is existing or not
+ * @cls: the compatibility class
+ * @name: name to look for
+ * @ns: the namespace tag to use
+ */
+bool class_has_file_ns(struct class *cls, const char *name,
+ const void *ns)
+{
+ struct kernfs_node *kn = NULL;
+
+ if (cls) {
+ kn = kernfs_find_and_get_ns(cls->p->subsys.kobj.sd, name, ns);
+ if (kn) {
+ kernfs_put(kn);
+ return true;
+ }
+ }
+ return false;
+}
+EXPORT_SYMBOL_GPL(class_has_file_ns);
+
static struct class *class_get(struct class *cls)
{
if (cls)
diff --git a/include/linux/device/class.h b/include/linux/device/class.h
index e8d470c457d1..b3d43658b201 100644
--- a/include/linux/device/class.h
+++ b/include/linux/device/class.h
@@ -209,7 +209,8 @@ extern int __must_check class_create_file_ns(struct class *class,
extern void class_remove_file_ns(struct class *class,
const struct class_attribute *attr,
const void *ns);
-
+bool class_has_file_ns(struct class *cls, const char *name,
+ const void *ns);
static inline int __must_check class_create_file(struct class *class,
const struct class_attribute *attr)
{
--
2.17.1