public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Arnd Bergmann <arnd@arndb.de>
To: linux-kernel@vger.kernel.org
Cc: linux-fsdevel@vger.kernel.org,
	Alexander Viro <viro@zeniv.linux.org.uk>,
	Christoph Hellwig <hch@lst.de>, Greg KH <gregkh@suse.de>,
	David Howells <dhowells@redhat.com>
Subject: [RFC 01/11] add generic versions of debugfs file operations
Date: Tue, 19 Feb 2008 05:04:36 +0100	[thread overview]
Message-ID: <20080219040828.415218298@arndb.de> (raw)
In-Reply-To: 20080219040435.825494460@arndb.de

[-- Attachment #1: introduce-libfs-files.diff --]
[-- Type: text/plain, Size: 6352 bytes --]

The file operations in debugfs are rather generic and can
be used by other file systems, so it can be interesting to
include them in libfs, with more generic names, and exported
to modules.

This patch adds a new copy of these operations to libfs,
so that the debugfs version can later be cut down.

Signed-off-by: Arnd Bergmann <arnd@arndb.de>

Index: linux-2.6/fs/Makefile
===================================================================
--- linux-2.6.orig/fs/Makefile
+++ linux-2.6/fs/Makefile
@@ -13,6 +13,8 @@ obj-y :=	open.o read_write.o file_table.
 		pnode.o drop_caches.o splice.o sync.o utimes.o \
 		stack.o
 
+obj-$(CONFIG_LIBFS) += libfs/
+
 ifeq ($(CONFIG_BLOCK),y)
 obj-y +=	buffer.o bio.o block_dev.o direct-io.o mpage.o ioprio.o
 else
Index: linux-2.6/include/linux/libfs.h
===================================================================
--- /dev/null
+++ linux-2.6/include/linux/libfs.h
@@ -0,0 +1,21 @@
+#ifndef __LIBFS_H__
+#define __LIBFS_H__
+
+#include <linux/fs.h>
+
+extern const struct file_operations simple_fops_u8;
+extern const struct file_operations simple_fops_x8;
+extern const struct file_operations simple_fops_u16;
+extern const struct file_operations simple_fops_x16;
+extern const struct file_operations simple_fops_u32;
+extern const struct file_operations simple_fops_x32;
+extern const struct file_operations simple_fops_u64;
+extern const struct file_operations simple_fops_bool;
+extern const struct file_operations simple_fops_blob;
+
+struct simple_blob_wrapper {
+	void *data;
+	unsigned long size;
+};
+
+#endif /* __LIBFS_H__ */
Index: linux-2.6/fs/libfs/Makefile
===================================================================
--- /dev/null
+++ linux-2.6/fs/libfs/Makefile
@@ -0,0 +1,3 @@
+libfs-y += file.o
+
+obj-$(CONFIG_LIBFS) += libfs.o
Index: linux-2.6/fs/libfs/file.c
===================================================================
--- /dev/null
+++ linux-2.6/fs/libfs/file.c
@@ -0,0 +1,126 @@
+/*
+ *	fs/libfs/file.c
+ *	Library for filesystems writers.
+ */
+
+#include <linux/fs.h>
+#include <linux/libfs.h>
+#include <linux/module.h>
+
+#include <asm/uaccess.h>
+
+/* commonly used attribute file operations */
+static int simple_u8_set(void *data, u64 val)
+{
+	*(u8 *)data = val;
+	return 0;
+}
+static int simple_u8_get(void *data, u64 *val)
+{
+	*val = *(u8 *)data;
+	return 0;
+}
+DEFINE_SIMPLE_EXPORTED_ATTRIBUTE(simple_fops_u8, simple_u8_get, simple_u8_set, "%llu\n");
+DEFINE_SIMPLE_EXPORTED_ATTRIBUTE(simple_fops_x8, simple_u8_get, simple_u8_set, "0x%02llx\n");
+
+static int simple_u16_set(void *data, u64 val)
+{
+	*(u16 *)data = val;
+	return 0;
+}
+static int simple_u16_get(void *data, u64 *val)
+{
+	*val = *(u16 *)data;
+	return 0;
+}
+DEFINE_SIMPLE_EXPORTED_ATTRIBUTE(simple_fops_u16, simple_u16_get, simple_u16_set, "%llu\n");
+DEFINE_SIMPLE_EXPORTED_ATTRIBUTE(simple_fops_x16, simple_u16_get, simple_u16_set, "0x%04llx\n");
+
+static int simple_u32_set(void *data, u64 val)
+{
+	*(u32 *)data = val;
+	return 0;
+}
+static int simple_u32_get(void *data, u64 *val)
+{
+	*val = *(u32 *)data;
+	return 0;
+}
+DEFINE_SIMPLE_EXPORTED_ATTRIBUTE(simple_fops_u32, simple_u32_get, simple_u32_set, "%llu\n");
+DEFINE_SIMPLE_EXPORTED_ATTRIBUTE(simple_fops_x32, simple_u32_get, simple_u32_set, "0x%08llx\n");
+
+static int simple_u64_set(void *data, u64 val)
+{
+	*(u64 *)data = val;
+	return 0;
+}
+
+static int simple_u64_get(void *data, u64 *val)
+{
+	return *(u64 *)data;
+	return 0;
+}
+DEFINE_SIMPLE_EXPORTED_ATTRIBUTE(simple_fops_u64, simple_u64_get, simple_u64_set, "%llu\n");
+
+static ssize_t read_file_bool(struct file *file, char __user *user_buf,
+			      size_t count, loff_t *ppos)
+{
+	char buf[3];
+	u32 *val = file->private_data;
+
+	if (*val)
+		buf[0] = 'Y';
+	else
+		buf[0] = 'N';
+	buf[1] = '\n';
+	buf[2] = 0x00;
+	return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
+}
+
+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;
+	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;
+	}
+
+	return count;
+}
+
+const struct file_operations simple_fops_bool = {
+	.read =		read_file_bool,
+	.write =	write_file_bool,
+	.open =		simple_open,
+};
+EXPORT_SYMBOL_GPL(simple_fops_bool);
+
+static ssize_t read_file_blob(struct file *file, char __user *user_buf,
+			      size_t count, loff_t *ppos)
+{
+	struct simple_blob_wrapper *blob = file->private_data;
+	return simple_read_from_buffer(user_buf, count, ppos, blob->data,
+			blob->size);
+}
+
+const struct file_operations simple_fops_blob = {
+	.read =		read_file_blob,
+	.open =		simple_open,
+};
+EXPORT_SYMBOL_GPL(simple_fops_blob);
Index: linux-2.6/include/linux/fs.h
===================================================================
--- linux-2.6.orig/include/linux/fs.h
+++ linux-2.6/include/linux/fs.h
@@ -2042,19 +2042,26 @@ static inline void simple_transaction_se
  * All attributes contain a text representation of a numeric value
  * that are accessed with the get() and set() functions.
  */
-#define DEFINE_SIMPLE_ATTRIBUTE(__fops, __get, __set, __fmt)		\
+#define __DEFINE_SIMPLE_ATTRIBUTE(__static, __fops, __get, __set, __fmt)\
 static int __fops ## _open(struct inode *inode, struct file *file)	\
 {									\
 	__simple_attr_check_format(__fmt, 0ull);			\
 	return simple_attr_open(inode, file, __get, __set, __fmt);	\
 }									\
-static struct file_operations __fops = {				\
+__static const struct file_operations __fops = {			\
 	.owner	 = THIS_MODULE,						\
 	.open	 = __fops ## _open,					\
 	.release = simple_attr_release,					\
 	.read	 = simple_attr_read,					\
 	.write	 = simple_attr_write,					\
-};
+}
+
+#define DEFINE_SIMPLE_ATTRIBUTE(__fops, __get, __set, __fmt)		\
+	__DEFINE_SIMPLE_ATTRIBUTE(static, __fops, __get, __set, __fmt)
+
+#define DEFINE_SIMPLE_EXPORTED_ATTRIBUTE(__fops, __get, __set, __fmt)	\
+	__DEFINE_SIMPLE_ATTRIBUTE(/**/, __fops, __get, __set, __fmt);	\
+	EXPORT_SYMBOL_GPL(__fops)
 
 static inline void __attribute__((format(printf, 1, 2)))
 __simple_attr_check_format(const char *fmt, ...)

-- 


  reply	other threads:[~2008-02-19  4:14 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-02-19  4:04 [RFC 00/11] possible debugfs/libfs consolidation Arnd Bergmann
2008-02-19  4:04 ` Arnd Bergmann [this message]
2008-02-23 12:24   ` [RFC 01/11] add generic versions of debugfs file operations Al Viro
2008-02-24 10:46     ` Arnd Bergmann
2008-02-24 18:00       ` Greg KH
2008-02-23 12:33   ` Al Viro
2008-02-19  4:04 ` [RFC 02/11] introduce simple_fs_type Arnd Bergmann
2008-02-23 12:28   ` Al Viro
2008-02-24 10:55     ` Arnd Bergmann
2008-02-19  4:04 ` [RFC 03/11] slim down debugfs Arnd Bergmann
2008-02-23 12:37   ` Al Viro
2008-02-23 19:55     ` Hugh Dickins
2008-02-24 10:59     ` Arnd Bergmann
2008-02-19  4:04 ` [RFC 04/11] slim down securityfs Arnd Bergmann
2008-02-19  4:04 ` [RFC 05/11] slim down usbfs Arnd Bergmann
2008-02-19  4:04 ` [RFC 06/11] split out linux/libfs.h from linux/fs.h Arnd Bergmann
2008-02-19  4:04 ` [RFC 07/11] split out libfs/file.c from libfs.c Arnd Bergmann
2008-02-19  4:04 ` [RFC 08/11] split out libfs/dentry.c " Arnd Bergmann
2008-02-19  4:04 ` [RFC 09/11] split out libfs/super.c " Arnd Bergmann
2008-02-19  4:04 ` [RFC 10/11] split out libfs/inode.c " Arnd Bergmann
2008-02-19  4:04 ` [RFC 11/11] split out libfs/aops.c " Arnd Bergmann
2008-02-19 16:38 ` [RFC 00/11] possible debugfs/libfs consolidation Greg KH

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20080219040828.415218298@arndb.de \
    --to=arnd@arndb.de \
    --cc=dhowells@redhat.com \
    --cc=gregkh@suse.de \
    --cc=hch@lst.de \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=viro@zeniv.linux.org.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox