public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] add mmap support to struct bin_attribute files
@ 2004-12-21 17:32 Jesse Barnes
  2004-12-21 17:46 ` Greg KH
  2004-12-21 18:37 ` Greg KH
  0 siblings, 2 replies; 3+ messages in thread
From: Jesse Barnes @ 2004-12-21 17:32 UTC (permalink / raw)
  To: Greg KH, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 607 bytes --]

This patch adds an mmap method and some more error checking to struct 
bin_attribute--good for things like exporting PCI resources directly.  I 
wasn't sure about the return values for the case where an attribute is 
missing a given method, and it looks like mm.h can't be included in sysfs.h, 
so I had to forward declare struct vm_area_struct.  Other than that, it works 
fine for my test cases.

 fs/sysfs/bin.c        |   27 +++++++++++++++++++++++++--
 include/linux/sysfs.h |    6 ++++++
 2 files changed, 31 insertions(+), 2 deletions(-)

Signed-off-by: Jesse Barnes <jbarnes@sgi.com>

Thanks,
Jesse

[-- Attachment #2: bin-file-mmap-support.patch --]
[-- Type: text/plain, Size: 2664 bytes --]

===== include/linux/sysfs.h 1.38 vs edited =====
--- 1.38/include/linux/sysfs.h	2004-11-01 12:47:02 -08:00
+++ edited/include/linux/sysfs.h	2004-12-21 09:31:01 -08:00
@@ -2,6 +2,7 @@
  * sysfs.h - definitions for the device driver filesystem
  *
  * Copyright (c) 2001,2002 Patrick Mochel
+ * Copyright (c) 2004 Silicon Graphics, Inc.
  *
  * Please see Documentation/filesystems/sysfs.txt for more information.
  */
@@ -47,11 +48,16 @@
 
 #define attr_name(_attr) (_attr).attr.name
 
+struct vm_area_struct; /* circular dependencies? */
+
 struct bin_attribute {
 	struct attribute	attr;
 	size_t			size;
+	void			*private;
 	ssize_t (*read)(struct kobject *, char *, loff_t, size_t);
 	ssize_t (*write)(struct kobject *, char *, loff_t, size_t);
+	int (*mmap)(struct kobject *, struct bin_attribute *attr,
+		    struct vm_area_struct *vma);
 };
 
 struct sysfs_ops {
===== fs/sysfs/bin.c 1.19 vs edited =====
--- 1.19/fs/sysfs/bin.c	2004-11-01 12:46:46 -08:00
+++ edited/fs/sysfs/bin.c	2004-12-21 09:32:08 -08:00
@@ -1,5 +1,9 @@
 /*
  * bin.c - binary file operations for sysfs.
+ *
+ * Copyright (c) 2003 Patrick Mochel
+ * Copyright (c) 2003 Matthew Wilcox
+ * Copyright (c) 2004 Silicon Graphics, Inc.
  */
 
 #undef DEBUG
@@ -20,6 +24,9 @@
 	struct bin_attribute * attr = to_bin_attr(dentry);
 	struct kobject * kobj = to_kobj(dentry->d_parent);
 
+	if (!attr->read)
+		return -EINVAL;
+
 	return attr->read(kobj, buffer, off, count);
 }
 
@@ -63,6 +70,9 @@
 	struct bin_attribute *attr = to_bin_attr(dentry);
 	struct kobject *kobj = to_kobj(dentry->d_parent);
 
+	if (!attr->write)
+		return -EINVAL;
+
 	return attr->write(kobj, buffer, offset, count);
 }
 
@@ -92,6 +102,18 @@
 	return count;
 }
 
+static int mmap(struct file *file, struct vm_area_struct *vma)
+{
+	struct dentry *dentry = file->f_dentry;
+	struct bin_attribute *attr = to_bin_attr(dentry);
+	struct kobject *kobj = to_kobj(dentry->d_parent);
+
+	if (!attr->mmap)
+		return -EINVAL;
+
+	return attr->mmap(kobj, attr, vma);
+}
+
 static int open(struct inode * inode, struct file * file)
 {
 	struct kobject *kobj = sysfs_get_kobject(file->f_dentry->d_parent);
@@ -107,9 +129,9 @@
 		goto Done;
 
 	error = -EACCES;
-	if ((file->f_mode & FMODE_WRITE) && !attr->write)
+	if ((file->f_mode & FMODE_WRITE) && !(attr->write || attr->mmap))
 		goto Error;
-	if ((file->f_mode & FMODE_READ) && !attr->read)
+	if ((file->f_mode & FMODE_READ) && !(attr->read || attr->mmap))
 		goto Error;
 
 	error = -ENOMEM;
@@ -144,6 +166,7 @@
 struct file_operations bin_fops = {
 	.read		= read,
 	.write		= write,
+	.mmap		= mmap,
 	.llseek		= generic_file_llseek,
 	.open		= open,
 	.release	= release,

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] add mmap support to struct bin_attribute files
  2004-12-21 17:32 [PATCH] add mmap support to struct bin_attribute files Jesse Barnes
@ 2004-12-21 17:46 ` Greg KH
  2004-12-21 18:37 ` Greg KH
  1 sibling, 0 replies; 3+ messages in thread
From: Greg KH @ 2004-12-21 17:46 UTC (permalink / raw)
  To: Jesse Barnes; +Cc: linux-kernel

On Tue, Dec 21, 2004 at 09:32:54AM -0800, Jesse Barnes wrote:
>   * Copyright (c) 2001,2002 Patrick Mochel
> + * Copyright (c) 2004 Silicon Graphics, Inc.

Minor nit, some copyright lawyers I know say it should be (C) not (c),
for what it's worth...

> +struct vm_area_struct; /* circular dependencies? */

Drop the comment :)

>  struct bin_attribute {
>  	struct attribute	attr;
>  	size_t			size;
> +	void			*private;

Why the private pointer?  Don't you get everything you need in the
kobject itself?

thanks,

greg k-h

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] add mmap support to struct bin_attribute files
  2004-12-21 17:32 [PATCH] add mmap support to struct bin_attribute files Jesse Barnes
  2004-12-21 17:46 ` Greg KH
@ 2004-12-21 18:37 ` Greg KH
  1 sibling, 0 replies; 3+ messages in thread
From: Greg KH @ 2004-12-21 18:37 UTC (permalink / raw)
  To: Jesse Barnes; +Cc: linux-kernel

On Tue, Dec 21, 2004 at 09:32:54AM -0800, Jesse Barnes wrote:
> This patch adds an mmap method and some more error checking to struct 
> bin_attribute--good for things like exporting PCI resources directly.  I 
> wasn't sure about the return values for the case where an attribute is 
> missing a given method, and it looks like mm.h can't be included in sysfs.h, 
> so I had to forward declare struct vm_area_struct.  Other than that, it works 
> fine for my test cases.
> 
>  fs/sysfs/bin.c        |   27 +++++++++++++++++++++++++--
>  include/linux/sysfs.h |    6 ++++++
>  2 files changed, 31 insertions(+), 2 deletions(-)
> 
> Signed-off-by: Jesse Barnes <jbarnes@sgi.com>

Nice, thanks for doing this.  I've applied it to my trees.

greg k-h

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2004-12-21 18:37 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-12-21 17:32 [PATCH] add mmap support to struct bin_attribute files Jesse Barnes
2004-12-21 17:46 ` Greg KH
2004-12-21 18:37 ` Greg KH

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox