diff options
Diffstat (limited to 'fs/debugfs')
| -rw-r--r-- | fs/debugfs/file.c | 299 | ||||
| -rw-r--r-- | fs/debugfs/inode.c | 368 | 
2 files changed, 505 insertions, 162 deletions
diff --git a/fs/debugfs/file.c b/fs/debugfs/file.c index 89d394d8fe2..63146295153 100644 --- a/fs/debugfs/file.c +++ b/fs/debugfs/file.c @@ -15,9 +15,13 @@  #include <linux/module.h>  #include <linux/fs.h> +#include <linux/seq_file.h>  #include <linux/pagemap.h>  #include <linux/namei.h>  #include <linux/debugfs.h> +#include <linux/io.h> +#include <linux/slab.h> +#include <linux/atomic.h>  static ssize_t default_read_file(struct file *file, char __user *buf,  				 size_t count, loff_t *ppos) @@ -31,18 +35,10 @@ static ssize_t default_write_file(struct file *file, const char __user *buf,  	return count;  } -static int default_open(struct inode *inode, struct file *file) -{ -	if (inode->i_private) -		file->private_data = inode->i_private; - -	return 0; -} -  const struct file_operations debugfs_file_operations = {  	.read =		default_read_file,  	.write =	default_write_file, -	.open =		default_open, +	.open =		simple_open,  	.llseek =	noop_llseek,  }; @@ -95,7 +91,7 @@ DEFINE_SIMPLE_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%llu\n");   * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling   * code.   */ -struct dentry *debugfs_create_u8(const char *name, mode_t mode, +struct dentry *debugfs_create_u8(const char *name, umode_t mode,  				 struct dentry *parent, u8 *value)  {  	/* if there are no write bits set, make read only */ @@ -147,7 +143,7 @@ DEFINE_SIMPLE_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%llu\n");   * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling   * code.   */ -struct dentry *debugfs_create_u16(const char *name, mode_t mode, +struct dentry *debugfs_create_u16(const char *name, umode_t mode,  				  struct dentry *parent, u16 *value)  {  	/* if there are no write bits set, make read only */ @@ -199,7 +195,7 @@ DEFINE_SIMPLE_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%llu\n");   * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling   * code.   */ -struct dentry *debugfs_create_u32(const char *name, mode_t mode, +struct dentry *debugfs_create_u32(const char *name, umode_t mode,  				 struct dentry *parent, u32 *value)  {  	/* if there are no write bits set, make read only */ @@ -252,7 +248,7 @@ DEFINE_SIMPLE_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n");   * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling   * code.   */ -struct dentry *debugfs_create_u64(const char *name, mode_t mode, +struct dentry *debugfs_create_u64(const char *name, umode_t mode,  				 struct dentry *parent, u64 *value)  {  	/* if there are no write bits set, make read only */ @@ -298,7 +294,7 @@ DEFINE_SIMPLE_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set, "0x%016llx\n   * @value: a pointer to the variable that the file should read to and write   *         from.   */ -struct dentry *debugfs_create_x8(const char *name, mode_t mode, +struct dentry *debugfs_create_x8(const char *name, umode_t mode,  				 struct dentry *parent, u8 *value)  {  	/* if there are no write bits set, make read only */ @@ -322,7 +318,7 @@ EXPORT_SYMBOL_GPL(debugfs_create_x8);   * @value: a pointer to the variable that the file should read to and write   *         from.   */ -struct dentry *debugfs_create_x16(const char *name, mode_t mode, +struct dentry *debugfs_create_x16(const char *name, umode_t mode,  				 struct dentry *parent, u16 *value)  {  	/* if there are no write bits set, make read only */ @@ -346,7 +342,7 @@ EXPORT_SYMBOL_GPL(debugfs_create_x16);   * @value: a pointer to the variable that the file should read to and write   *         from.   */ -struct dentry *debugfs_create_x32(const char *name, mode_t mode, +struct dentry *debugfs_create_x32(const char *name, umode_t mode,  				 struct dentry *parent, u32 *value)  {  	/* if there are no write bits set, make read only */ @@ -370,7 +366,7 @@ EXPORT_SYMBOL_GPL(debugfs_create_x32);   * @value: a pointer to the variable that the file should read to and write   *         from.   */ -struct dentry *debugfs_create_x64(const char *name, mode_t mode, +struct dentry *debugfs_create_x64(const char *name, umode_t mode,  				 struct dentry *parent, u64 *value)  {  	return debugfs_create_file(name, mode, parent, value, &fops_x64); @@ -401,13 +397,54 @@ DEFINE_SIMPLE_ATTRIBUTE(fops_size_t, debugfs_size_t_get, debugfs_size_t_set,   * @value: a pointer to the variable that the file should read to and write   *         from.   */ -struct dentry *debugfs_create_size_t(const char *name, mode_t mode, +struct dentry *debugfs_create_size_t(const char *name, umode_t mode,  				     struct dentry *parent, size_t *value)  {  	return debugfs_create_file(name, mode, parent, value, &fops_size_t);  }  EXPORT_SYMBOL_GPL(debugfs_create_size_t); +static int debugfs_atomic_t_set(void *data, u64 val) +{ +	atomic_set((atomic_t *)data, val); +	return 0; +} +static int debugfs_atomic_t_get(void *data, u64 *val) +{ +	*val = atomic_read((atomic_t *)data); +	return 0; +} +DEFINE_SIMPLE_ATTRIBUTE(fops_atomic_t, debugfs_atomic_t_get, +			debugfs_atomic_t_set, "%lld\n"); +DEFINE_SIMPLE_ATTRIBUTE(fops_atomic_t_ro, debugfs_atomic_t_get, NULL, "%lld\n"); +DEFINE_SIMPLE_ATTRIBUTE(fops_atomic_t_wo, NULL, debugfs_atomic_t_set, "%lld\n"); + +/** + * debugfs_create_atomic_t - create a debugfs file that is used to read and + * write an atomic_t value + * @name: a pointer to a string containing the name of the file to create. + * @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 parameter is %NULL, then the + *          file will be created in the root of the debugfs filesystem. + * @value: a pointer to the variable that the file should read to and write + *         from. + */ +struct dentry *debugfs_create_atomic_t(const char *name, umode_t mode, +				 struct dentry *parent, atomic_t *value) +{ +	/* if there are no write bits set, make read only */ +	if (!(mode & S_IWUGO)) +		return debugfs_create_file(name, mode, parent, value, +					&fops_atomic_t_ro); +	/* if there are no read bits set, make write only */ +	if (!(mode & S_IRUGO)) +		return debugfs_create_file(name, mode, parent, value, +					&fops_atomic_t_wo); + +	return debugfs_create_file(name, mode, parent, value, &fops_atomic_t); +} +EXPORT_SYMBOL_GPL(debugfs_create_atomic_t);  static ssize_t read_file_bool(struct file *file, char __user *user_buf,  			      size_t count, loff_t *ppos) @@ -428,33 +465,25 @@ static ssize_t write_file_bool(struct file *file, const char __user *user_buf,  			       size_t count, loff_t *ppos)  {  	char buf[32]; -	int buf_size; +	size_t buf_size; +	bool bv;  	u32 *val = file->private_data;  	buf_size = min(count, (sizeof(buf)-1));  	if (copy_from_user(buf, user_buf, buf_size))  		return -EFAULT; -	switch (buf[0]) { -	case 'y': -	case 'Y': -	case '1': -		*val = 1; -		break; -	case 'n': -	case 'N': -	case '0': -		*val = 0; -		break; -	} -	 +	buf[buf_size] = '\0'; +	if (strtobool(buf, &bv) == 0) +		*val = bv; +  	return count;  }  static const struct file_operations fops_bool = {  	.read =		read_file_bool,  	.write =	write_file_bool, -	.open =		default_open, +	.open =		simple_open,  	.llseek =	default_llseek,  }; @@ -482,7 +511,7 @@ static const struct file_operations fops_bool = {   * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling   * code.   */ -struct dentry *debugfs_create_bool(const char *name, mode_t mode, +struct dentry *debugfs_create_bool(const char *name, umode_t mode,  				   struct dentry *parent, u32 *value)  {  	return debugfs_create_file(name, mode, parent, value, &fops_bool); @@ -499,7 +528,7 @@ static ssize_t read_file_blob(struct file *file, char __user *user_buf,  static const struct file_operations fops_blob = {  	.read =		read_file_blob, -	.open =		default_open, +	.open =		simple_open,  	.llseek =	default_llseek,  }; @@ -527,10 +556,208 @@ static const struct file_operations fops_blob = {   * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling   * code.   */ -struct dentry *debugfs_create_blob(const char *name, mode_t mode, +struct dentry *debugfs_create_blob(const char *name, umode_t mode,  				   struct dentry *parent,  				   struct debugfs_blob_wrapper *blob)  {  	return debugfs_create_file(name, mode, parent, blob, &fops_blob);  }  EXPORT_SYMBOL_GPL(debugfs_create_blob); + +struct array_data { +	void *array; +	u32 elements; +}; + +static size_t u32_format_array(char *buf, size_t bufsize, +			       u32 *array, int array_size) +{ +	size_t ret = 0; + +	while (--array_size >= 0) { +		size_t len; +		char term = array_size ? ' ' : '\n'; + +		len = snprintf(buf, bufsize, "%u%c", *array++, term); +		ret += len; + +		buf += len; +		bufsize -= len; +	} +	return ret; +} + +static int u32_array_open(struct inode *inode, struct file *file) +{ +	struct array_data *data = inode->i_private; +	int size, elements = data->elements; +	char *buf; + +	/* +	 * Max size: +	 *  - 10 digits + ' '/'\n' = 11 bytes per number +	 *  - terminating NUL character +	 */ +	size = elements*11; +	buf = kmalloc(size+1, GFP_KERNEL); +	if (!buf) +		return -ENOMEM; +	buf[size] = 0; + +	file->private_data = buf; +	u32_format_array(buf, size, data->array, data->elements); + +	return nonseekable_open(inode, file); +} + +static ssize_t u32_array_read(struct file *file, char __user *buf, size_t len, +			      loff_t *ppos) +{ +	size_t size = strlen(file->private_data); + +	return simple_read_from_buffer(buf, len, ppos, +					file->private_data, size); +} + +static int u32_array_release(struct inode *inode, struct file *file) +{ +	kfree(file->private_data); + +	return 0; +} + +static const struct file_operations u32_array_fops = { +	.owner	 = THIS_MODULE, +	.open	 = u32_array_open, +	.release = u32_array_release, +	.read	 = u32_array_read, +	.llseek  = no_llseek, +}; + +/** + * debugfs_create_u32_array - create a debugfs file that is used to read u32 + * array. + * @name: a pointer to a string containing the name of the file to create. + * @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 parameter is %NULL, then the + *          file will be created in the root of the debugfs filesystem. + * @array: u32 array that provides data. + * @elements: total number of elements in the array. + * + * This function creates a file in debugfs with the given name that exports + * @array as data. If the @mode variable is so set it can be read from. + * Writing is not supported. Seek within the file is also not supported. + * Once array is created its size can not be changed. + * + * The function returns a pointer to dentry on success. If debugfs is not + * enabled in the kernel, the value -%ENODEV will be returned. + */ +struct dentry *debugfs_create_u32_array(const char *name, umode_t mode, +					    struct dentry *parent, +					    u32 *array, u32 elements) +{ +	struct array_data *data = kmalloc(sizeof(*data), GFP_KERNEL); + +	if (data == NULL) +		return NULL; + +	data->array = array; +	data->elements = elements; + +	return debugfs_create_file(name, mode, parent, data, &u32_array_fops); +} +EXPORT_SYMBOL_GPL(debugfs_create_u32_array); + +#ifdef CONFIG_HAS_IOMEM + +/* + * The regset32 stuff is used to print 32-bit registers using the + * seq_file utilities. We offer printing a register set in an already-opened + * sequential file or create a debugfs file that only prints a regset32. + */ + +/** + * debugfs_print_regs32 - use seq_print to describe a set of registers + * @s: the seq_file structure being used to generate output + * @regs: an array if struct debugfs_reg32 structures + * @nregs: the length of the above array + * @base: the base address to be used in reading the registers + * @prefix: a string to be prefixed to every output line + * + * This function outputs a text block describing the current values of + * some 32-bit hardware registers. It is meant to be used within debugfs + * files based on seq_file that need to show registers, intermixed with other + * information. The prefix argument may be used to specify a leading string, + * because some peripherals have several blocks of identical registers, + * for example configuration of dma channels + */ +int debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs, +			   int nregs, void __iomem *base, char *prefix) +{ +	int i, ret = 0; + +	for (i = 0; i < nregs; i++, regs++) { +		if (prefix) +			ret += seq_printf(s, "%s", prefix); +		ret += seq_printf(s, "%s = 0x%08x\n", regs->name, +				  readl(base + regs->offset)); +	} +	return ret; +} +EXPORT_SYMBOL_GPL(debugfs_print_regs32); + +static int debugfs_show_regset32(struct seq_file *s, void *data) +{ +	struct debugfs_regset32 *regset = s->private; + +	debugfs_print_regs32(s, regset->regs, regset->nregs, regset->base, ""); +	return 0; +} + +static int debugfs_open_regset32(struct inode *inode, struct file *file) +{ +	return single_open(file, debugfs_show_regset32, inode->i_private); +} + +static const struct file_operations fops_regset32 = { +	.open =		debugfs_open_regset32, +	.read =		seq_read, +	.llseek =	seq_lseek, +	.release =	single_release, +}; + +/** + * debugfs_create_regset32 - create a debugfs file that returns register values + * @name: a pointer to a string containing the name of the file to create. + * @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 parameter is %NULL, then the + *          file will be created in the root of the debugfs filesystem. + * @regset: a pointer to a struct debugfs_regset32, which contains a pointer + *          to an array of register definitions, the array size and the base + *          address where the register bank is to be found. + * + * This function creates a file in debugfs with the given name that reports + * the names and values of a set of 32-bit registers. If the @mode variable + * is so set it can be read from. Writing is not supported. + * + * This function will return a pointer to a dentry if it succeeds.  This + * pointer must be passed to the debugfs_remove() function when the file is + * to be removed (no automatic cleanup happens if your module is unloaded, + * you are responsible here.)  If an error occurs, %NULL will be returned. + * + * If debugfs is not enabled in the kernel, the value -%ENODEV will be + * returned.  It is not wise to check for this value, but rather, check for + * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling + * code. + */ +struct dentry *debugfs_create_regset32(const char *name, umode_t mode, +				       struct dentry *parent, +				       struct debugfs_regset32 *regset) +{ +	return debugfs_create_file(name, mode, parent, regset, &fops_regset32); +} +EXPORT_SYMBOL_GPL(debugfs_create_regset32); + +#endif /* CONFIG_HAS_IOMEM */ diff --git a/fs/debugfs/inode.c b/fs/debugfs/inode.c index 37a8ca7c122..8c41b52da35 100644 --- a/fs/debugfs/inode.c +++ b/fs/debugfs/inode.c @@ -1,5 +1,5 @@  /* - *  file.c - part of debugfs, a tiny little debug file system + *  inode.c - part of debugfs, a tiny little debug file system   *   *  Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>   *  Copyright (C) 2004 IBM Inc. @@ -13,9 +13,6 @@   *   */ -/* uncomment to get debug messages from the debug filesystem, ah the irony. */ -/* #define DEBUG */ -  #include <linux/module.h>  #include <linux/fs.h>  #include <linux/mount.h> @@ -26,14 +23,18 @@  #include <linux/debugfs.h>  #include <linux/fsnotify.h>  #include <linux/string.h> +#include <linux/seq_file.h> +#include <linux/parser.h>  #include <linux/magic.h>  #include <linux/slab.h> +#define DEBUGFS_DEFAULT_MODE	0700 +  static struct vfsmount *debugfs_mount;  static int debugfs_mount_count;  static bool debugfs_registered; -static struct inode *debugfs_get_inode(struct super_block *sb, int mode, dev_t dev, +static struct inode *debugfs_get_inode(struct super_block *sb, umode_t mode, dev_t dev,  				       void *data, const struct file_operations *fops)  { @@ -53,13 +54,11 @@ static struct inode *debugfs_get_inode(struct super_block *sb, int mode, dev_t d  			break;  		case S_IFLNK:  			inode->i_op = &debugfs_link_operations; -			inode->i_fop = fops;  			inode->i_private = data;  			break;  		case S_IFDIR:  			inode->i_op = &simple_dir_inode_operations; -			inode->i_fop = fops ? fops : &simple_dir_operations; -			inode->i_private = data; +			inode->i_fop = &simple_dir_operations;  			/* directory inodes start off with i_nlink == 2  			 * (for "." entry) */ @@ -72,7 +71,7 @@ static struct inode *debugfs_get_inode(struct super_block *sb, int mode, dev_t d  /* SMP-safe */  static int debugfs_mknod(struct inode *dir, struct dentry *dentry, -			 int mode, dev_t dev, void *data, +			 umode_t mode, dev_t dev, void *data,  			 const struct file_operations *fops)  {  	struct inode *inode; @@ -90,13 +89,12 @@ static int debugfs_mknod(struct inode *dir, struct dentry *dentry,  	return error;  } -static int debugfs_mkdir(struct inode *dir, struct dentry *dentry, int mode, -			 void *data, const struct file_operations *fops) +static int debugfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)  {  	int res;  	mode = (mode & (S_IRWXUGO | S_ISVTX)) | S_IFDIR; -	res = debugfs_mknod(dir, dentry, mode, 0, data, fops); +	res = debugfs_mknod(dir, dentry, mode, 0, NULL, NULL);  	if (!res) {  		inc_nlink(dir);  		fsnotify_mkdir(dir, dentry); @@ -104,14 +102,14 @@ static int debugfs_mkdir(struct inode *dir, struct dentry *dentry, int mode,  	return res;  } -static int debugfs_link(struct inode *dir, struct dentry *dentry, int mode, -			void *data, const struct file_operations *fops) +static int debugfs_link(struct inode *dir, struct dentry *dentry, umode_t mode, +			void *data)  {  	mode = (mode & S_IALLUGO) | S_IFLNK; -	return debugfs_mknod(dir, dentry, mode, 0, data, fops); +	return debugfs_mknod(dir, dentry, mode, 0, data, NULL);  } -static int debugfs_create(struct inode *dir, struct dentry *dentry, int mode, +static int debugfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,  			  void *data, const struct file_operations *fops)  {  	int res; @@ -128,11 +126,165 @@ static inline int debugfs_positive(struct dentry *dentry)  	return dentry->d_inode && !d_unhashed(dentry);  } +struct debugfs_mount_opts { +	kuid_t uid; +	kgid_t gid; +	umode_t mode; +}; + +enum { +	Opt_uid, +	Opt_gid, +	Opt_mode, +	Opt_err +}; + +static const match_table_t tokens = { +	{Opt_uid, "uid=%u"}, +	{Opt_gid, "gid=%u"}, +	{Opt_mode, "mode=%o"}, +	{Opt_err, NULL} +}; + +struct debugfs_fs_info { +	struct debugfs_mount_opts mount_opts; +}; + +static int debugfs_parse_options(char *data, struct debugfs_mount_opts *opts) +{ +	substring_t args[MAX_OPT_ARGS]; +	int option; +	int token; +	kuid_t uid; +	kgid_t gid; +	char *p; + +	opts->mode = DEBUGFS_DEFAULT_MODE; + +	while ((p = strsep(&data, ",")) != NULL) { +		if (!*p) +			continue; + +		token = match_token(p, tokens, args); +		switch (token) { +		case Opt_uid: +			if (match_int(&args[0], &option)) +				return -EINVAL; +			uid = make_kuid(current_user_ns(), option); +			if (!uid_valid(uid)) +				return -EINVAL; +			opts->uid = uid; +			break; +		case Opt_gid: +			if (match_int(&args[0], &option)) +				return -EINVAL; +			gid = make_kgid(current_user_ns(), option); +			if (!gid_valid(gid)) +				return -EINVAL; +			opts->gid = gid; +			break; +		case Opt_mode: +			if (match_octal(&args[0], &option)) +				return -EINVAL; +			opts->mode = option & S_IALLUGO; +			break; +		/* +		 * We might like to report bad mount options here; +		 * but traditionally debugfs has ignored all mount options +		 */ +		} +	} + +	return 0; +} + +static int debugfs_apply_options(struct super_block *sb) +{ +	struct debugfs_fs_info *fsi = sb->s_fs_info; +	struct inode *inode = sb->s_root->d_inode; +	struct debugfs_mount_opts *opts = &fsi->mount_opts; + +	inode->i_mode &= ~S_IALLUGO; +	inode->i_mode |= opts->mode; + +	inode->i_uid = opts->uid; +	inode->i_gid = opts->gid; + +	return 0; +} + +static int debugfs_remount(struct super_block *sb, int *flags, char *data) +{ +	int err; +	struct debugfs_fs_info *fsi = sb->s_fs_info; + +	sync_filesystem(sb); +	err = debugfs_parse_options(data, &fsi->mount_opts); +	if (err) +		goto fail; + +	debugfs_apply_options(sb); + +fail: +	return err; +} + +static int debugfs_show_options(struct seq_file *m, struct dentry *root) +{ +	struct debugfs_fs_info *fsi = root->d_sb->s_fs_info; +	struct debugfs_mount_opts *opts = &fsi->mount_opts; + +	if (!uid_eq(opts->uid, GLOBAL_ROOT_UID)) +		seq_printf(m, ",uid=%u", +			   from_kuid_munged(&init_user_ns, opts->uid)); +	if (!gid_eq(opts->gid, GLOBAL_ROOT_GID)) +		seq_printf(m, ",gid=%u", +			   from_kgid_munged(&init_user_ns, opts->gid)); +	if (opts->mode != DEBUGFS_DEFAULT_MODE) +		seq_printf(m, ",mode=%o", opts->mode); + +	return 0; +} + +static const struct super_operations debugfs_super_operations = { +	.statfs		= simple_statfs, +	.remount_fs	= debugfs_remount, +	.show_options	= debugfs_show_options, +}; +  static int debug_fill_super(struct super_block *sb, void *data, int silent)  {  	static struct tree_descr debug_files[] = {{""}}; +	struct debugfs_fs_info *fsi; +	int err; -	return simple_fill_super(sb, DEBUGFS_MAGIC, debug_files); +	save_mount_options(sb, data); + +	fsi = kzalloc(sizeof(struct debugfs_fs_info), GFP_KERNEL); +	sb->s_fs_info = fsi; +	if (!fsi) { +		err = -ENOMEM; +		goto fail; +	} + +	err = debugfs_parse_options(data, &fsi->mount_opts); +	if (err) +		goto fail; + +	err  =  simple_fill_super(sb, DEBUGFS_MAGIC, debug_files); +	if (err) +		goto fail; + +	sb->s_op = &debugfs_super_operations; + +	debugfs_apply_options(sb); + +	return 0; + +fail: +	kfree(fsi); +	sb->s_fs_info = NULL; +	return err;  }  static struct dentry *debug_mount(struct file_system_type *fs_type, @@ -148,14 +300,21 @@ static struct file_system_type debug_fs_type = {  	.mount =	debug_mount,  	.kill_sb =	kill_litter_super,  }; +MODULE_ALIAS_FS("debugfs"); -static int debugfs_create_by_name(const char *name, mode_t mode, -				  struct dentry *parent, -				  struct dentry **dentry, -				  void *data, -				  const struct file_operations *fops) +static struct dentry *__create_file(const char *name, umode_t mode, +				    struct dentry *parent, void *data, +				    const struct file_operations *fops)  { -	int error = 0; +	struct dentry *dentry = NULL; +	int error; + +	pr_debug("debugfs: creating file '%s'\n",name); + +	error = simple_pin_fs(&debug_fs_type, &debugfs_mount, +			      &debugfs_mount_count); +	if (error) +		goto exit;  	/* 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  @@ -163,32 +322,36 @@ static int debugfs_create_by_name(const char *name, mode_t mode,  	 * have around.  	 */  	if (!parent) -		parent = debugfs_mount->mnt_sb->s_root; +		parent = debugfs_mount->mnt_root; -	*dentry = NULL;  	mutex_lock(&parent->d_inode->i_mutex); -	*dentry = lookup_one_len(name, parent, strlen(name)); -	if (!IS_ERR(*dentry)) { +	dentry = lookup_one_len(name, parent, strlen(name)); +	if (!IS_ERR(dentry)) {  		switch (mode & S_IFMT) {  		case S_IFDIR: -			error = debugfs_mkdir(parent->d_inode, *dentry, mode, -					      data, fops); +			error = debugfs_mkdir(parent->d_inode, dentry, mode); +					        			break;  		case S_IFLNK: -			error = debugfs_link(parent->d_inode, *dentry, mode, -					     data, fops); +			error = debugfs_link(parent->d_inode, dentry, mode, +					     data);  			break;  		default: -			error = debugfs_create(parent->d_inode, *dentry, mode, +			error = debugfs_create(parent->d_inode, dentry, mode,  					       data, fops);  			break;  		} -		dput(*dentry); +		dput(dentry);  	} else -		error = PTR_ERR(*dentry); +		error = PTR_ERR(dentry);  	mutex_unlock(&parent->d_inode->i_mutex); -	return error; +	if (error) { +		dentry = NULL; +		simple_release_fs(&debugfs_mount, &debugfs_mount_count); +	} +exit: +	return dentry;  }  /** @@ -196,7 +359,7 @@ static int debugfs_create_by_name(const char *name, mode_t mode,   * @name: a pointer to a string containing the name of the file to create.   * @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 + *          directory dentry if set.  If this parameter is NULL, then the   *          file will be created in the root of the debugfs filesystem.   * @data: a pointer to something that the caller will want to get to later   *        on.  The inode.i_private pointer will point to this value on @@ -217,29 +380,19 @@ static int debugfs_create_by_name(const char *name, mode_t mode,   * If debugfs is not enabled in the kernel, the value -%ENODEV will be   * returned.   */ -struct dentry *debugfs_create_file(const char *name, mode_t mode, +struct dentry *debugfs_create_file(const char *name, umode_t mode,  				   struct dentry *parent, void *data,  				   const struct file_operations *fops)  { -	struct dentry *dentry = NULL; -	int error; - -	pr_debug("debugfs: creating file '%s'\n",name); - -	error = simple_pin_fs(&debug_fs_type, &debugfs_mount, -			      &debugfs_mount_count); -	if (error) -		goto exit; - -	error = debugfs_create_by_name(name, mode, parent, &dentry, -				       data, fops); -	if (error) { -		dentry = NULL; -		simple_release_fs(&debugfs_mount, &debugfs_mount_count); -		goto exit; +	switch (mode & S_IFMT) { +	case S_IFREG: +	case 0: +		break; +	default: +		BUG();  	} -exit: -	return dentry; + +	return __create_file(name, mode, parent, data, fops);  }  EXPORT_SYMBOL_GPL(debugfs_create_file); @@ -248,7 +401,7 @@ EXPORT_SYMBOL_GPL(debugfs_create_file);   * @name: a pointer to a string containing the name of the directory to   *        create.   * @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 dentry if set.  If this parameter is NULL, then the   *          directory will be created in the root of the debugfs filesystem.   *   * This function creates a directory in debugfs with the given name. @@ -263,8 +416,7 @@ EXPORT_SYMBOL_GPL(debugfs_create_file);   */  struct dentry *debugfs_create_dir(const char *name, struct dentry *parent)  { -	return debugfs_create_file(name,  -				   S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO, +	return __create_file(name, S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO,  				   parent, NULL, NULL);  }  EXPORT_SYMBOL_GPL(debugfs_create_dir); @@ -274,7 +426,7 @@ EXPORT_SYMBOL_GPL(debugfs_create_dir);   * @name: a pointer to a string containing the name of the symbolic link to   *        create.   * @parent: a pointer to the parent dentry for this symbolic link.  This - *          should be a directory dentry if set.  If this paramater is NULL, + *          should be a directory dentry if set.  If this parameter is NULL,   *          then the symbolic link will be created in the root of the debugfs   *          filesystem.   * @target: a pointer to a string containing the path to the target of the @@ -302,15 +454,14 @@ struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent,  	if (!link)  		return NULL; -	result = debugfs_create_file(name, S_IFLNK | S_IRWXUGO, parent, link, -				     NULL); +	result = __create_file(name, S_IFLNK | S_IRWXUGO, parent, link, NULL);  	if (!result)  		kfree(link);  	return result;  }  EXPORT_SYMBOL_GPL(debugfs_create_symlink); -static void __debugfs_remove(struct dentry *dentry, struct dentry *parent) +static int __debugfs_remove(struct dentry *dentry, struct dentry *parent)  {  	int ret = 0; @@ -333,6 +484,7 @@ static void __debugfs_remove(struct dentry *dentry, struct dentry *parent)  			dput(dentry);  		}  	} +	return ret;  }  /** @@ -351,8 +503,9 @@ static void __debugfs_remove(struct dentry *dentry, struct dentry *parent)  void debugfs_remove(struct dentry *dentry)  {  	struct dentry *parent; -	 -	if (!dentry) +	int ret; + +	if (IS_ERR_OR_NULL(dentry))  		return;  	parent = dentry->d_parent; @@ -360,9 +513,10 @@ void debugfs_remove(struct dentry *dentry)  		return;  	mutex_lock(&parent->d_inode->i_mutex); -	__debugfs_remove(dentry, parent); +	ret = __debugfs_remove(dentry, parent);  	mutex_unlock(&parent->d_inode->i_mutex); -	simple_release_fs(&debugfs_mount, &debugfs_mount_count); +	if (!ret) +		simple_release_fs(&debugfs_mount, &debugfs_mount_count);  }  EXPORT_SYMBOL_GPL(debugfs_remove); @@ -380,10 +534,9 @@ EXPORT_SYMBOL_GPL(debugfs_remove);   */  void debugfs_remove_recursive(struct dentry *dentry)  { -	struct dentry *child; -	struct dentry *parent; +	struct dentry *child, *next, *parent; -	if (!dentry) +	if (IS_ERR_OR_NULL(dentry))  		return;  	parent = dentry->d_parent; @@ -391,61 +544,36 @@ void debugfs_remove_recursive(struct dentry *dentry)  		return;  	parent = dentry; + down:  	mutex_lock(&parent->d_inode->i_mutex); +	list_for_each_entry_safe(child, next, &parent->d_subdirs, d_u.d_child) { +		if (!debugfs_positive(child)) +			continue; -	while (1) { -		/* -		 * When all dentries under "parent" has been removed, -		 * walk up the tree until we reach our starting point. -		 */ -		if (list_empty(&parent->d_subdirs)) { -			mutex_unlock(&parent->d_inode->i_mutex); -			if (parent == dentry) -				break; -			parent = parent->d_parent; -			mutex_lock(&parent->d_inode->i_mutex); -		} -		child = list_entry(parent->d_subdirs.next, struct dentry, -				d_u.d_child); - next_sibling: - -		/* -		 * If "child" isn't empty, walk down the tree and -		 * remove all its descendants first. -		 */ +		/* perhaps simple_empty(child) makes more sense */  		if (!list_empty(&child->d_subdirs)) {  			mutex_unlock(&parent->d_inode->i_mutex);  			parent = child; -			mutex_lock(&parent->d_inode->i_mutex); -			continue; +			goto down;  		} -		__debugfs_remove(child, parent); -		if (parent->d_subdirs.next == &child->d_u.d_child) { -			/* -			 * Try the next sibling. -			 */ -			if (child->d_u.d_child.next != &parent->d_subdirs) { -				child = list_entry(child->d_u.d_child.next, -						   struct dentry, -						   d_u.d_child); -				goto next_sibling; -			} - -			/* -			 * Avoid infinite loop if we fail to remove -			 * one dentry. -			 */ -			mutex_unlock(&parent->d_inode->i_mutex); -			break; -		} -		simple_release_fs(&debugfs_mount, &debugfs_mount_count); + up: +		if (!__debugfs_remove(child, parent)) +			simple_release_fs(&debugfs_mount, &debugfs_mount_count);  	} -	parent = dentry->d_parent; +	mutex_unlock(&parent->d_inode->i_mutex); +	child = parent; +	parent = parent->d_parent;  	mutex_lock(&parent->d_inode->i_mutex); -	__debugfs_remove(dentry, parent); + +	if (child != dentry) { +		next = list_next_entry(child, d_u.d_child); +		goto up; +	} + +	if (!__debugfs_remove(child, parent)) +		simple_release_fs(&debugfs_mount, &debugfs_mount_count);  	mutex_unlock(&parent->d_inode->i_mutex); -	simple_release_fs(&debugfs_mount, &debugfs_mount_count);  }  EXPORT_SYMBOL_GPL(debugfs_remove_recursive); @@ -540,17 +668,5 @@ static int __init debugfs_init(void)  	return retval;  } - -static void __exit debugfs_exit(void) -{ -	debugfs_registered = false; - -	simple_release_fs(&debugfs_mount, &debugfs_mount_count); -	unregister_filesystem(&debug_fs_type); -	kobject_put(debug_kobj); -} -  core_initcall(debugfs_init); -module_exit(debugfs_exit); -MODULE_LICENSE("GPL");  | 
