1143 lines · 38.2 KB · C
RawDownload
/* *  file.c - part of debugfs, a tiny little debug file system * *  Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com> *  Copyright (C) 2004 IBM Inc. * *	This program is free software; you can redistribute it and/or *	modify it under the terms of the GNU General Public License version *	2 as published by the Free Software Foundation. * *  debugfs is for people to use instead of /proc or /sys. *  See Documentation/filesystems/ for more details. * */ #include <linux/module.h>#include <linux/fs.h>#include <linux/seq_file.h>#include <linux/pagemap.h>#include <linux/debugfs.h>#include <linux/io.h>#include <linux/slab.h>#include <linux/atomic.h>#include <linux/device.h>#include <linux/srcu.h>#include <asm/poll.h> #include "internal.h" struct poll_table_struct; static ssize_t default_read_file(struct file *file, char __user *buf,				 size_t count, loff_t *ppos){	return 0;} static ssize_t default_write_file(struct file *file, const char __user *buf,				   size_t count, loff_t *ppos){	return count;} const struct file_operations debugfs_noop_file_operations = {	.read =		default_read_file,	.write =	default_write_file,	.open =		simple_open,	.llseek =	noop_llseek,}; /** * debugfs_use_file_start - mark the beginning of file data access * @dentry: the dentry object whose data is being accessed. * @srcu_idx: a pointer to some memory to store a SRCU index in. * * Up to a matching call to debugfs_use_file_finish(), any * successive call into the file removing functions debugfs_remove() * and debugfs_remove_recursive() will block. Since associated private * file data may only get freed after a successful return of any of * the removal functions, you may safely access it after a successful * call to debugfs_use_file_start() without worrying about * lifetime issues. * * If -%EIO is returned, the file has already been removed and thus, * it is not safe to access any of its data. If, on the other hand, * it is allowed to access the file data, zero is returned. * * Regardless of the return code, any call to * debugfs_use_file_start() must be followed by a matching call * to debugfs_use_file_finish(). */int debugfs_use_file_start(const struct dentry *dentry, int *srcu_idx)	__acquires(&debugfs_srcu){	*srcu_idx = srcu_read_lock(&debugfs_srcu);	barrier();	if (d_unlinked(dentry))		return -EIO;	return 0;}EXPORT_SYMBOL_GPL(debugfs_use_file_start); /** * debugfs_use_file_finish - mark the end of file data access * @srcu_idx: the SRCU index "created" by a former call to *            debugfs_use_file_start(). * * Allow any ongoing concurrent call into debugfs_remove() or * debugfs_remove_recursive() blocked by a former call to * debugfs_use_file_start() to proceed and return to its caller. */void debugfs_use_file_finish(int srcu_idx) __releases(&debugfs_srcu){	srcu_read_unlock(&debugfs_srcu, srcu_idx);}EXPORT_SYMBOL_GPL(debugfs_use_file_finish); #define F_DENTRY(filp) ((filp)->f_path.dentry) static int open_proxy_open(struct inode *inode, struct file *filp){	const struct dentry *dentry = F_DENTRY(filp);	const struct file_operations *real_fops = NULL;	int srcu_idx, r; 	r = debugfs_use_file_start(dentry, &srcu_idx);	if (r) {		r = -ENOENT;		goto out;	} 	real_fops = debugfs_real_fops(filp);	real_fops = fops_get(real_fops);	if (!real_fops) {		/* Huh? Module did not clean up after itself at exit? */		WARN(1, "debugfs file owner did not clean up at exit: %pd",			dentry);		r = -ENXIO;		goto out;	}	replace_fops(filp, real_fops); 	if (real_fops->open)		r = real_fops->open(inode, filp); out:	debugfs_use_file_finish(srcu_idx);	return r;} const struct file_operations debugfs_open_proxy_file_operations = {	.open = open_proxy_open,}; #define PROTO(args...) args#define ARGS(args...) args #define FULL_PROXY_FUNC(name, ret_type, filp, proto, args)		\static ret_type full_proxy_ ## name(proto)				\{									\	const struct dentry *dentry = F_DENTRY(filp);			\	const struct file_operations *real_fops =			\		debugfs_real_fops(filp);				\	int srcu_idx;							\	ret_type r;							\									\	r = debugfs_use_file_start(dentry, &srcu_idx);			\	if (likely(!r))						\		r = real_fops->name(args);				\	debugfs_use_file_finish(srcu_idx);				\	return r;							\} FULL_PROXY_FUNC(llseek, loff_t, filp,		PROTO(struct file *filp, loff_t offset, int whence),		ARGS(filp, offset, whence)); FULL_PROXY_FUNC(read, ssize_t, filp,		PROTO(struct file *filp, char __user *buf, size_t size,			loff_t *ppos),		ARGS(filp, buf, size, ppos)); FULL_PROXY_FUNC(write, ssize_t, filp,		PROTO(struct file *filp, const char __user *buf, size_t size,			loff_t *ppos),		ARGS(filp, buf, size, ppos)); FULL_PROXY_FUNC(unlocked_ioctl, long, filp,		PROTO(struct file *filp, unsigned int cmd, unsigned long arg),		ARGS(filp, cmd, arg)); static unsigned int full_proxy_poll(struct file *filp,				struct poll_table_struct *wait){	const struct dentry *dentry = F_DENTRY(filp);	const struct file_operations *real_fops = debugfs_real_fops(filp);	int srcu_idx;	unsigned int r = 0; 	if (debugfs_use_file_start(dentry, &srcu_idx)) {		debugfs_use_file_finish(srcu_idx);		return POLLHUP;	} 	r = real_fops->poll(filp, wait);	debugfs_use_file_finish(srcu_idx);	return r;} static int full_proxy_release(struct inode *inode, struct file *filp){	const struct dentry *dentry = F_DENTRY(filp);	const struct file_operations *real_fops = debugfs_real_fops(filp);	const struct file_operations *proxy_fops = filp->f_op;	int r = 0; 	/*	 * We must not protect this against removal races here: the	 * original releaser should be called unconditionally in order	 * not to leak any resources. Releasers must not assume that	 * ->i_private is still being meaningful here.	 */	if (real_fops->release)		r = real_fops->release(inode, filp); 	replace_fops(filp, d_inode(dentry)->i_fop);	kfree((void *)proxy_fops);	fops_put(real_fops);	return r;} static void __full_proxy_fops_init(struct file_operations *proxy_fops,				const struct file_operations *real_fops){	proxy_fops->release = full_proxy_release;	if (real_fops->llseek)		proxy_fops->llseek = full_proxy_llseek;	if (real_fops->read)		proxy_fops->read = full_proxy_read;	if (real_fops->write)		proxy_fops->write = full_proxy_write;	if (real_fops->poll)		proxy_fops->poll = full_proxy_poll;	if (real_fops->unlocked_ioctl)		proxy_fops->unlocked_ioctl = full_proxy_unlocked_ioctl;} static int full_proxy_open(struct inode *inode, struct file *filp){	const struct dentry *dentry = F_DENTRY(filp);	const struct file_operations *real_fops = NULL;	struct file_operations *proxy_fops = NULL;	int srcu_idx, r; 	r = debugfs_use_file_start(dentry, &srcu_idx);	if (r) {		r = -ENOENT;		goto out;	} 	real_fops = debugfs_real_fops(filp);	real_fops = fops_get(real_fops);	if (!real_fops) {		/* Huh? Module did not cleanup after itself at exit? */		WARN(1, "debugfs file owner did not clean up at exit: %pd",			dentry);		r = -ENXIO;		goto out;	} 	proxy_fops = kzalloc(sizeof(*proxy_fops), GFP_KERNEL);	if (!proxy_fops) {		r = -ENOMEM;		goto free_proxy;	}	__full_proxy_fops_init(proxy_fops, real_fops);	replace_fops(filp, proxy_fops); 	if (real_fops->open) {		r = real_fops->open(inode, filp);		if (r) {			replace_fops(filp, d_inode(dentry)->i_fop);			goto free_proxy;		} else if (filp->f_op != proxy_fops) {			/* No protection against file removal anymore. */			WARN(1, "debugfs file owner replaced proxy fops: %pd",				dentry);			goto free_proxy;		}	} 	goto out;free_proxy:	kfree(proxy_fops);	fops_put(real_fops);out:	debugfs_use_file_finish(srcu_idx);	return r;} const struct file_operations debugfs_full_proxy_file_operations = {	.open = full_proxy_open,}; ssize_t debugfs_attr_read(struct file *file, char __user *buf,			size_t len, loff_t *ppos){	ssize_t ret;	int srcu_idx; 	ret = debugfs_use_file_start(F_DENTRY(file), &srcu_idx);	if (likely(!ret))		ret = simple_attr_read(file, buf, len, ppos);	debugfs_use_file_finish(srcu_idx);	return ret;}EXPORT_SYMBOL_GPL(debugfs_attr_read); ssize_t debugfs_attr_write(struct file *file, const char __user *buf,			 size_t len, loff_t *ppos){	ssize_t ret;	int srcu_idx; 	ret = debugfs_use_file_start(F_DENTRY(file), &srcu_idx);	if (likely(!ret))		ret = simple_attr_write(file, buf, len, ppos);	debugfs_use_file_finish(srcu_idx);	return ret;}EXPORT_SYMBOL_GPL(debugfs_attr_write); static struct dentry *debugfs_create_mode_unsafe(const char *name, umode_t mode,					struct dentry *parent, void *value,					const struct file_operations *fops,					const struct file_operations *fops_ro,					const struct file_operations *fops_wo){	/* if there are no write bits set, make read only */	if (!(mode & S_IWUGO))		return debugfs_create_file_unsafe(name, mode, parent, value,						fops_ro);	/* if there are no read bits set, make write only */	if (!(mode & S_IRUGO))		return debugfs_create_file_unsafe(name, mode, parent, value,						fops_wo); 	return debugfs_create_file_unsafe(name, mode, parent, value, fops);} static int debugfs_u8_set(void *data, u64 val){	*(u8 *)data = val;	return 0;}static int debugfs_u8_get(void *data, u64 *val){	*val = *(u8 *)data;	return 0;}DEFINE_DEBUGFS_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n");DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_ro, debugfs_u8_get, NULL, "%llu\n");DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%llu\n"); /** * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit 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. * * This function creates a file in debugfs with the given name that * contains the value of the variable @value.  If the @mode variable is so * set, it can be read from, and written to. * * 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_u8(const char *name, umode_t mode,				 struct dentry *parent, u8 *value){	return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u8,				   &fops_u8_ro, &fops_u8_wo);}EXPORT_SYMBOL_GPL(debugfs_create_u8); static int debugfs_u16_set(void *data, u64 val){	*(u16 *)data = val;	return 0;}static int debugfs_u16_get(void *data, u64 *val){	*val = *(u16 *)data;	return 0;}DEFINE_DEBUGFS_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n");DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_ro, debugfs_u16_get, NULL, "%llu\n");DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%llu\n"); /** * debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-bit 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. * * This function creates a file in debugfs with the given name that * contains the value of the variable @value.  If the @mode variable is so * set, it can be read from, and written to. * * 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_u16(const char *name, umode_t mode,				  struct dentry *parent, u16 *value){	return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u16,				   &fops_u16_ro, &fops_u16_wo);}EXPORT_SYMBOL_GPL(debugfs_create_u16); static int debugfs_u32_set(void *data, u64 val){	*(u32 *)data = val;	return 0;}static int debugfs_u32_get(void *data, u64 *val){	*val = *(u32 *)data;	return 0;}DEFINE_DEBUGFS_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n");DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%llu\n");DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%llu\n"); /** * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit 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. * * This function creates a file in debugfs with the given name that * contains the value of the variable @value.  If the @mode variable is so * set, it can be read from, and written to. * * 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_u32(const char *name, umode_t mode,				 struct dentry *parent, u32 *value){	return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u32,				   &fops_u32_ro, &fops_u32_wo);}EXPORT_SYMBOL_GPL(debugfs_create_u32); static int debugfs_u64_set(void *data, u64 val){	*(u64 *)data = val;	return 0;} static int debugfs_u64_get(void *data, u64 *val){	*val = *(u64 *)data;	return 0;}DEFINE_DEBUGFS_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n");DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%llu\n");DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n"); /** * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit 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. * * This function creates a file in debugfs with the given name that * contains the value of the variable @value.  If the @mode variable is so * set, it can be read from, and written to. * * 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,

Lines 1–500 of 1143.