Skip to contentSkip to conversation

DEBUGFS: Automatically create parents for debugfs files v2

PATCH In review Andi Kleen 2 participants 4 messages Dec 2, 2011 – Dec 12, 2011

Subsystems (2)
lore
Share & tools
lore

Andi Kleen

author
#0 lore raw

PATCH 1/3 DEBUGFS: Automatically create parents for debugfs files v2

From: Andi Kleen <[email protected]>

Allow passing path names to debugfs_create* and automatically create the parents. This makes it much simpler for the caller to create hierarchies.

The way the reference counts are handled is admittedly a bit ugly.

There is no way to clean them up currently other than to delete the tree, but that doesn't seem like a big problem for debugfs to leave behind a few empty directories.

v2: Comment fixes based on review comments.

---
 fs/debugfs/inode.c |   62 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 60 insertions(+), 2 deletions(-)
fs/debugfs/inode.c +60 -2
index f3a257d..b66ab79 100644
@@ -146,6 +146,55 @@ static struct file_system_type debug_fs_type = {
 	.kill_sb =	kill_litter_super,
 };
 
+/*
+ * Created parents stay around later. Sorry, but it shouldn't be a big issue.
+ */
+static const char *create_parents(const char *name, struct dentry *parent,
+				  struct dentry **pp, int *ref)
+{
+	char fn[strlen(name) + 1];
+	char *tail;
+	char *s, *p;
+	struct dentry *new_parent, *dentry, *old_parent = NULL;
+
+	strcpy(fn, name);
+	tail = strrchr(fn, '/');
+	if (!tail)
+		return name;
+	tail++;
+
+	s = fn;
+	while ((p = strsep(&s, "/")) != NULL && p != tail) {
+		/* 
+ 		 * When the parent already exists, this will error out.
+ 		 * Ignore that error. We'll just look the dentry up below.
+ 		 */
+		dentry = debugfs_create_dir(p, parent);
+
+		/* Is it there now? */
+		mutex_lock(&parent->d_inode->i_mutex);
+		new_parent = lookup_one_len(p, parent, strlen(p));
+		mutex_unlock(&parent->d_inode->i_mutex);
+
+		if (!PTR_ERR(dentry))
+			dput(dentry);
+		if (IS_ERR(new_parent))
+			return NULL;
+		if (old_parent)
+			dput(old_parent);
+		if (parent != *pp)
+			old_parent = parent;
+		parent = new_parent;
+	}
+	if (parent) {
+		*ref = 1;
+		*pp = parent;
+	}
+	if (old_parent)
+		dput(old_parent);
+	return strrchr(name, '/') + 1;
+}
+
 static int debugfs_create_by_name(const char *name, mode_t mode,
 				  struct dentry *parent,
 				  struct dentry **dentry,
@@ -153,6 +202,7 @@ static int debugfs_create_by_name(const char *name, mode_t mode,
 				  const struct file_operations *fops)
 {
 	int error = 0;
+	int ref = 0;
 
 	/* If the parent is not specified, we create it in the root.
 	 * We need the root dentry to do this, which is in the super 
@@ -161,6 +211,7 @@ static int debugfs_create_by_name(const char *name, mode_t mode,
 	 */
 	if (!parent)
 		parent = debugfs_mount->mnt_sb->s_root;
+	name = create_parents(name, parent, &parent, &ref);
 
 	*dentry = NULL;
 	mutex_lock(&parent->d_inode->i_mutex);
@@ -185,12 +236,17 @@ static int debugfs_create_by_name(const char *name, mode_t mode,
 		error = PTR_ERR(*dentry);
 	mutex_unlock(&parent->d_inode->i_mutex);
 
+	if (ref)
+		dput(parent);
+
 	return error;
 }
 
 /**
  * debugfs_create_file - create a file in the debugfs filesystem
  * @name: a pointer to a string containing the name of the file to create.
+ *        This can also contain directories (dir/file), in this case missing
+ *        directories will be automatically created.
  * @mode: the permission that the file should have.
  * @parent: a pointer to the parent dentry for this file.  This should be a
  *          directory dentry if set.  If this paramater is NULL, then the
@@ -243,7 +299,8 @@ EXPORT_SYMBOL_GPL(debugfs_create_file);
 /**
  * debugfs_create_dir - create a directory in the debugfs filesystem
  * @name: a pointer to a string containing the name of the directory to
- *        create.
+ *        create. This can also contain directories (dir/file), in this case 
+ *        missing directories will be automatically created.
  * @parent: a pointer to the parent dentry for this file.  This should be a
  *          directory dentry if set.  If this paramater is NULL, then the
  *          directory will be created in the root of the debugfs filesystem.
@@ -269,7 +326,8 @@ EXPORT_SYMBOL_GPL(debugfs_create_dir);
 /**
  * debugfs_create_symlink- create a symbolic link in the debugfs filesystem
  * @name: a pointer to a string containing the name of the symbolic link to
- *        create.
+ *        create.  This can also contain directories (dir/file), in this case 
+ *        missing directories will be automatically created
  * @parent: a pointer to the parent dentry for this symbolic link.  This
  *          should be a directory dentry if set.  If this paramater is NULL,
  *          then the symbolic link will be created in the root of the debugfs
Signature · 1 line
1.7.4.4
author
#1 lore raw

PATCH 2/3 DEBUGFS: Add per cpu counters

From: Andi Kleen <[email protected]>

Add a simple way to export per cpu counters in debugfs.

This is done using a section, so that they can be declared using a simple macro with minimal typing.

OPEN: modules are not supported yet.

---
 fs/debugfs/Makefile               |    2 +-
 fs/debugfs/counter.c              |   73 +++++++++++++++++++++++++++++++++++++
 include/asm-generic/vmlinux.lds.h |    6 +++
 include/linux/debugfs.h           |   27 ++++++++++++++
 4 files changed, 107 insertions(+), 1 deletions(-)
 create mode 100644 fs/debugfs/counter.c
fs/debugfs/Makefile +1 -1
index 840c456..21be8d3 100644
@@ -1,4 +1,4 @@
-debugfs-objs	:= inode.o file.o
+debugfs-objs	:= inode.o file.o counter.o
 
 obj-$(CONFIG_DEBUG_FS)	+= debugfs.o
 
fs/debugfs/counter.c new file +73 -0

73 lines not on this page · raw message

include/asm-generic/vmlinux.lds.h +6 -0
index b5e2e4c..fbfb95b 100644
@@ -272,6 +272,12 @@
 									\
 	TRACEDATA							\
 									\
+	__debugfs  	: AT(ADDR(__debugfs) - LOAD_OFFSET) {		\
+		VMLINUX_SYMBOL(__start___debugfs) = .;			\
+		*(__debugfs)						\
+		VMLINUX_SYMBOL(__stop___debugfs) = .;			\
+	}								\
+									\
 	/* Kernel symbol table: Normal symbols */			\
 	__ksymtab         : AT(ADDR(__ksymtab) - LOAD_OFFSET) {		\
 		VMLINUX_SYMBOL(__start___ksymtab) = .;			\
include/linux/debugfs.h +27 -0
index e7d9b20..a243b05 100644
@@ -18,6 +18,7 @@
 #include <linux/fs.h>
 
 #include <linux/types.h>
+#include <linux/percpu.h>
 
 struct file_operations;
 
@@ -76,6 +77,24 @@ struct dentry *debugfs_create_blob(const char *name, mode_t mode,
 
 bool debugfs_initialized(void);
 
+struct debugfs_counter {
+	unsigned __percpu *ptr;
+	const char *fn;
+	const char *name;
+} __attribute__((aligned(sizeof(char *))));
+
+/* Note: static doesn't work unlike DEFINE_PERCPU. Sorry. */
+#define DEFINE_DEBUGFS_COUNTER(name_, file)				\
+	DEFINE_PER_CPU(unsigned, name_ ## _counter);			\
+	struct debugfs_counter name_ ## _pcpu_counter __used		\
+	__attribute__((aligned(sizeof(char *)),section("__debugfs"),unused)) \
+	= { .ptr = &name_ ## _counter, .fn = file, .name = #name_ }; \
+
+#define DECLARE_DEBUGFS_COUNTER(name)			\
+	struct debugfs_counter name ## _pcpu_counter
+
+#define debugfs_counter_inc(name) this_cpu_inc(name ## _counter)
+
 #else
 
 #include <linux/err.h>
@@ -193,6 +212,14 @@ static inline bool debugfs_initialized(void)
 	return false;
 }
 
+
+struct debugfs_counter {
+};
+
+#define DEFINE_DEBUGFS_COUNTER(name, file)
+#define DECLARE_DEBUGFS_COUNTER(name)
+#define debugfs_counter_inc(name) do {} while(0)
+
 #endif
 
 #endif
Signature · 1 line
1.7.4.4
#2 lore raw

Re: [PATCH 2/3] DEBUGFS: Add per cpu counters

Quoted 10 lines · Andi Kleen

From: Andi Kleen <[email protected]>

Add a simple way to export per cpu counters in debugfs.

This is done using a section, so that they can be declared using a simple macro with minimal typing.

OPEN: modules are not supported yet.

After applying the previous patch, this one still needs "fuzz" which makes git not happy, care to redo this one as well when you redo the 1/3 patch?

And I'm guessing that I don't need 3/3?

thanks,

greg k-h

#3 lore raw
In reply to Andi Kleen
Quoted 5 lines · Andi Kleen

From: Andi Kleen <[email protected]>

Allow passing path names to debugfs_create* and automatically create the parents. This makes it much simpler for the caller to create hierarchies.

Quoted 8 lines

The way the reference counts are handled is admittedly a bit ugly.

There is no way to clean them up currently other than to delete the tree, but that doesn't seem like a big problem for debugfs to leave behind a few empty directories.

v2: Comment fixes based on review comments.

git, and checkpatch.pl complain a lot about this patch, care to fix up the issues and resend it?

thanks,

greg k-h