Skip to contentSkip to conversation

export, rename fill_kobj_path and get_kobj_path_length

PATCH New Robert Love 2 participants 6 messages Aug 6, 2004

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 export, rename fill_kobj_path and get_kobj_path_length

Greg,

Per our discussion, the following patch exports fill_kobj_path and get_kobj_path_length and adds them to kobject.h.

I also renamed them to kobject_get_path_len and kobject_fill_path, respectively, to match the name space.

I need these for a kobject-based kevent. Others may find them fascinating as well.

Robert Love

Export some internal kobject helpers.

 include/linux/kobject.h |    3 +++
 lib/kobject.c           |   12 +++++++-----
 2 files changed, 10 insertions(+), 5 deletions(-)
include/linux/kobject.h +3 -0
diff -urN linux-2.6.8-rc3-mm1/include/linux/kobject.h linux-rml/include/linux/kobject.h
@@ -58,6 +58,9 @@
 
 extern void kobject_hotplug(const char *action, struct kobject *);
 
+extern int kobject_get_path_len(struct kset *, struct kobject *);
+extern void kobject_fill_path(struct kset *, struct kobject *, char *, int);
+
 struct kobj_type {
 	void (*release)(struct kobject *);
 	struct sysfs_ops	* sysfs_ops;
lib/kobject.c +7 -5
diff -urN linux-2.6.8-rc3-mm1/lib/kobject.c linux-rml/lib/kobject.c
@@ -66,7 +66,7 @@
 
 
 #ifdef CONFIG_HOTPLUG
-static int get_kobj_path_length(struct kset *kset, struct kobject *kobj)
+int kobject_get_path_len(struct kset *kset, struct kobject *kobj)
 {
 	int length = 1;
 	struct kobject * parent = kobj;
@@ -82,7 +82,8 @@
 	return length;
 }
 
-static void fill_kobj_path(struct kset *kset, struct kobject *kobj, char *path, int length)
+void kobject_fill_path(struct kset *kset, struct kobject *kobj, char *path,
+		       int length)
 {
 	struct kobject * parent;
 
@@ -163,12 +164,12 @@
 	envp [i++] = scratch;
 	scratch += sprintf(scratch, "SEQNUM=%ld", seq) + 1;
 
-	kobj_path_length = get_kobj_path_length (kset, kobj);
+	kobj_path_length = kobject_get_path_len (kset, kobj);
 	kobj_path = kmalloc (kobj_path_length, GFP_KERNEL);
 	if (!kobj_path)
 		goto exit;
 	memset (kobj_path, 0x00, kobj_path_length);
-	fill_kobj_path (kset, kobj, kobj_path, kobj_path_length);
+	kobject_fill_path (kset, kobj, kobj_path, kobj_path_length);
 
 	envp [i++] = scratch;
 	scratch += sprintf (scratch, "DEVPATH=%s", kobj_path) + 1;
@@ -626,7 +627,8 @@
 	}
 }
 
-
+EXPORT_SYMBOL(kobject_get_path_len);
+EXPORT_SYMBOL(kobject_fill_path);
 EXPORT_SYMBOL(kobject_init);
 EXPORT_SYMBOL(kobject_register);
 EXPORT_SYMBOL(kobject_unregister);
#1 lore raw

PATCH 1/1 add kobject_get_path

Greg -

Hey, here is an alternative to exporting those two functions.

This patch, in lieu, creates a new function that performs the same steps as in kset_hotplug() and presumably what every single user of the previously exported functions would do: get the length, allocate memory, and then build the path.

So just export a single function to do it right. And use it in kset_hotplug(). The function is prototyped as

char * kobject_get_path(struct kset *kset, struct kobject *kobj,
int gfp_mask)

I like this approach better.

Patch is against 2.6.8-rc3.

Robert Love

Create a new kobject_get_path() helper function, which is super duper.

 include/linux/kobject.h |    2 ++
 lib/kobject.c           |   31 +++++++++++++++++++++++++------
 2 files changed, 27 insertions(+), 6 deletions(-)
include/linux/kobject.h +2 -0
diff -urN linux-2.6.8-rc3-mm1/include/linux/kobject.h linux/include/linux/kobject.h
@@ -58,6 +58,8 @@
 
 extern void kobject_hotplug(const char *action, struct kobject *);
 
+extern char * kobject_get_path(struct kset *, struct kobject *, int);
+
 struct kobj_type {
 	void (*release)(struct kobject *);
 	struct sysfs_ops	* sysfs_ops;
lib/kobject.c +25 -6
diff -urN linux-2.6.8-rc3-mm1/lib/kobject.c linux/lib/kobject.c
@@ -103,6 +103,29 @@
 static unsigned long sequence_num;
 static spinlock_t sequence_lock = SPIN_LOCK_UNLOCKED;
 
+/**
+ * kobject_get_path - generate and return the path associated with a given kobj
+ * and kset pair.  The result must be freed by the caller with kfree().
+ *
+ * @kset:	kset in question, with which to build the path
+ * @kobj:	kobject in question, with which to build the path
+ * @gfp_mask:	the allocation type used to allocate the path
+ */
+char * kobject_get_path(struct kset *kset, struct kobject *kobj, int gfp_mask)
+{
+	char *path;
+	int len;
+
+	len = get_kobj_path_length (kset, kobj);
+	path = kmalloc (len, gfp_mask);
+	if (!path)
+		return NULL;
+	memset (path, 0x00, len);
+	fill_kobj_path (kset, kobj, path, len);
+
+	return path;
+}
+
 static void kset_hotplug(const char *action, struct kset *kset,
 			 struct kobject *kobj)
 {
@@ -112,7 +135,6 @@
 	char *scratch;
 	int i = 0;
 	int retval;
-	int kobj_path_length;
 	char *kobj_path = NULL;
 	char *name = NULL;
 	unsigned long seq;
@@ -163,12 +185,9 @@
 	envp [i++] = scratch;
 	scratch += sprintf(scratch, "SEQNUM=%ld", seq) + 1;
 
-	kobj_path_length = get_kobj_path_length (kset, kobj);
-	kobj_path = kmalloc (kobj_path_length, GFP_KERNEL);
+	kobj_path = kobject_get_path (kset, kobj, GFP_KERNEL);
 	if (!kobj_path)
 		goto exit;
-	memset (kobj_path, 0x00, kobj_path_length);
-	fill_kobj_path (kset, kobj, kobj_path, kobj_path_length);
 
 	envp [i++] = scratch;
 	scratch += sprintf (scratch, "DEVPATH=%s", kobj_path) + 1;
@@ -626,7 +645,7 @@
 	}
 }
 
-
+EXPORT_SYMBOL(kobject_get_path);
 EXPORT_SYMBOL(kobject_init);
 EXPORT_SYMBOL(kobject_register);
 EXPORT_SYMBOL(kobject_unregister);
#2 lore raw

Re: [patch] add kobject_get_path

Quoted 3 lines · Robert Love

Greg -

Hey, here is an alternative to exporting those two functions.

Ah, good timing, I was just about to apply your other patch :)

Quoted 6 lines

This patch, in lieu, creates a new function that performs the same steps as in kset_hotplug() and presumably what every single user of the previously exported functions would do: get the length, allocate memory, and then build the path.

Quoted 7 lines

So just export a single function to do it right. And use it in kset_hotplug(). The function is prototyped as

char * kobject_get_path(struct kset *kset, struct kobject *kobj,
int gfp_mask)

I like this approach better.

I do too. One problem though, get_kobj_path_length and fill_kobj_path are only built if CONFIG_HOTPLUG is enabled. Care to respin this patch by moving the functions outside of that #define?

thanks,

greg k-h

#3 lore raw

Re: [patch] add kobject_get_path

Quoted 3 lines · Greg KH

I do too. One problem though, get_kobj_path_length and fill_kobj_path are only built if CONFIG_HOTPLUG is enabled. Care to respin this patch by moving the functions outside of that #define?

Hrm, did not notice that. kobject_get_path() was inside there, too.

I presume these functions are still meaningful if !CONFIG_HOTPLUG?

Here we go. Thanks,

Robert Love

Add a new kobject helper, kobject_get_path(), which is the greatest function ever.

 include/linux/kobject.h |    2 ++
 lib/kobject.c           |   36 +++++++++++++++++++++++++++---------
 2 files changed, 29 insertions(+), 9 deletions(-)
include/linux/kobject.h +2 -0
diff -urN linux-2.6.8-rc3-mm1/include/linux/kobject.h linux/include/linux/kobject.h
@@ -58,6 +58,8 @@
 
 extern void kobject_hotplug(const char *action, struct kobject *);
 
+extern char * kobject_get_path(struct kset *, struct kobject *, int);
+
 struct kobj_type {
 	void (*release)(struct kobject *);
 	struct sysfs_ops	* sysfs_ops;
lib/kobject.c +27 -9
diff -urN linux-2.6.8-rc3-mm1/lib/kobject.c linux/lib/kobject.c
@@ -58,14 +58,11 @@
 	return error;
 }
 
-
 static inline struct kobject * to_kobj(struct list_head * entry)
 {
 	return container_of(entry,struct kobject,entry);
 }
 
-
-#ifdef CONFIG_HOTPLUG
 static int get_kobj_path_length(struct kset *kset, struct kobject *kobj)
 {
 	int length = 1;
@@ -98,6 +95,31 @@
 	pr_debug("%s: path = '%s'\n",__FUNCTION__,path);
 }
 
+/**
+ * kobject_get_path - generate and return the path associated with a given kobj
+ * and kset pair.  The result must be freed by the caller with kfree().
+ *
+ * @kset:	kset in question, with which to build the path
+ * @kobj:	kobject in question, with which to build the path
+ * @gfp_mask:	the allocation type used to allocate the path
+ */
+char * kobject_get_path(struct kset *kset, struct kobject *kobj, int gfp_mask)
+{
+	char *path;
+	int len;
+
+	len = get_kobj_path_length (kset, kobj);
+	path = kmalloc (len, gfp_mask);
+	if (!path)
+		return NULL;
+	memset (path, 0x00, len);
+	fill_kobj_path (kset, kobj, path, len);
+
+	return path;
+}
+
+#ifdef CONFIG_HOTPLUG
+
 #define BUFFER_SIZE	1024	/* should be enough memory for the env */
 #define NUM_ENVP	32	/* number of env pointers */
 static unsigned long sequence_num;
@@ -112,7 +134,6 @@
 	char *scratch;
 	int i = 0;
 	int retval;
-	int kobj_path_length;
 	char *kobj_path = NULL;
 	char *name = NULL;
 	unsigned long seq;
@@ -163,12 +184,9 @@
 	envp [i++] = scratch;
 	scratch += sprintf(scratch, "SEQNUM=%ld", seq) + 1;
 
-	kobj_path_length = get_kobj_path_length (kset, kobj);
-	kobj_path = kmalloc (kobj_path_length, GFP_KERNEL);
+	kobj_path = kobject_get_path (kset, kobj, GFP_KERNEL);
 	if (!kobj_path)
 		goto exit;
-	memset (kobj_path, 0x00, kobj_path_length);
-	fill_kobj_path (kset, kobj, kobj_path, kobj_path_length);
 
 	envp [i++] = scratch;
 	scratch += sprintf (scratch, "DEVPATH=%s", kobj_path) + 1;
@@ -626,7 +644,7 @@
 	}
 }
 
-
+EXPORT_SYMBOL(kobject_get_path);
 EXPORT_SYMBOL(kobject_init);
 EXPORT_SYMBOL(kobject_register);
 EXPORT_SYMBOL(kobject_unregister);
#4 lore raw

Re: [patch] add kobject_get_path

Quoted 1 line · Robert Love

I presume these functions are still meaningful if !CONFIG_HOTPLUG?

Compiles, at least. Just tested.

Robert Love

Applied
#5 lore raw

Re: [patch] add kobject_get_path

In reply to Robert Love
Quoted 9 lines · Robert Love
Quoted 3 lines · Greg KH

I do too. One problem though, get_kobj_path_length and fill_kobj_path are only built if CONFIG_HOTPLUG is enabled. Care to respin this patch by moving the functions outside of that #define?

Hrm, did not notice that. kobject_get_path() was inside there, too.

I presume these functions are still meaningful if !CONFIG_HOTPLUG?

Well, they weren't until your function needed them :)

Quoted 1 line

Here we go. Thanks,

Applied, thanks.

greg k-h