From: Tejun Heo <htejun@gmail.com>
To: gregkh@suse.de, maneesh@in.ibm.com, dmitry.torokhov@gmail.com,
cornelia.huck@de.ibm.com, oneukum@suse.de, rpurdie@rpsys.net,
James.Bottomley@SteelEye.com, stern@rowland.harvard.edu,
linux-kernel@vger.kernel.org, htejun@gmail.com
Cc: Tejun Heo <htejun@gmail.com>
Subject: [PATCH 11/14] sysfs: implement bin_buffer
Date: Mon, 9 Apr 2007 13:18:48 +0900 [thread overview]
Message-ID: <1176092328564-git-send-email-htejun@gmail.com> (raw)
In-Reply-To: <11760923261269-git-send-email-htejun@gmail.com>
Implement bin_buffer which contains a mutex and pointer to PAGE_SIZE
buffer to properly synchronize accesses to per-openfile buffer and
prepare for immediate-kobj-disconnect.
Signed-off-by: Tejun Heo <htejun@gmail.com>
---
fs/sysfs/bin.c | 64 ++++++++++++++++++++++++++++++++++++++++++-------------
1 files changed, 49 insertions(+), 15 deletions(-)
diff --git a/fs/sysfs/bin.c b/fs/sysfs/bin.c
index 0f0027b..1dd1bf1 100644
--- a/fs/sysfs/bin.c
+++ b/fs/sysfs/bin.c
@@ -20,6 +20,11 @@
#include "sysfs.h"
+struct bin_buffer {
+ struct mutex mutex;
+ void *buffer;
+};
+
static int
fill_read(struct dentry *dentry, char *buffer, loff_t off, size_t count)
{
@@ -36,7 +41,7 @@ fill_read(struct dentry *dentry, char *buffer, loff_t off, size_t count)
static ssize_t
read(struct file *file, char __user *userbuf, size_t bytes, loff_t *off)
{
- char *buffer = file->private_data;
+ struct bin_buffer *bb = file->private_data;
struct dentry *dentry = file->f_path.dentry;
int size = dentry->d_inode->i_size;
loff_t offs = *off;
@@ -49,17 +54,23 @@ read(struct file *file, char __user *userbuf, size_t bytes, loff_t *off)
count = size - offs;
}
- count = fill_read(dentry, buffer, offs, count);
+ mutex_lock(&bb->mutex);
+
+ count = fill_read(dentry, bb->buffer, offs, count);
if (count < 0)
- return count;
+ goto out_unlock;
- if (copy_to_user(userbuf, buffer, count))
- return -EFAULT;
+ if (copy_to_user(userbuf, bb->buffer, count)) {
+ count = -EFAULT;
+ goto out_unlock;
+ }
pr_debug("offs = %lld, *off = %lld, count = %zd\n", offs, *off, count);
*off = offs + count;
+ out_unlock:
+ mutex_unlock(&bb->mutex);
return count;
}
@@ -79,7 +90,7 @@ flush_write(struct dentry *dentry, char *buffer, loff_t offset, size_t count)
static ssize_t write(struct file *file, const char __user *userbuf,
size_t bytes, loff_t *off)
{
- char *buffer = file->private_data;
+ struct bin_buffer *bb = file->private_data;
struct dentry *dentry = file->f_path.dentry;
int size = dentry->d_inode->i_size;
loff_t offs = *off;
@@ -92,25 +103,38 @@ static ssize_t write(struct file *file, const char __user *userbuf,
count = size - offs;
}
- if (copy_from_user(buffer, userbuf, count))
- return -EFAULT;
+ mutex_lock(&bb->mutex);
+
+ if (copy_from_user(bb->buffer, userbuf, count)) {
+ count = -EFAULT;
+ goto out_unlock;
+ }
- count = flush_write(dentry, buffer, offs, count);
+ count = flush_write(dentry, bb->buffer, offs, count);
if (count > 0)
*off = offs + count;
+
+ out_unlock:
+ mutex_unlock(&bb->mutex);
return count;
}
static int mmap(struct file *file, struct vm_area_struct *vma)
{
+ struct bin_buffer *bb = file->private_data;
struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
struct bin_attribute *attr = attr_sd->s_elem.bin_attr.bin_attr;
struct kobject *kobj = to_kobj(file->f_path.dentry->d_parent);
+ int rc;
if (!attr->mmap)
return -EINVAL;
- return attr->mmap(kobj, attr, vma);
+ mutex_lock(&bb->mutex);
+ rc = attr->mmap(kobj, attr, vma);
+ mutex_unlock(&bb->mutex);
+
+ return rc;
}
static int open(struct inode * inode, struct file * file)
@@ -118,6 +142,7 @@ static int open(struct inode * inode, struct file * file)
struct kobject *kobj = sysfs_get_kobject(file->f_path.dentry->d_parent);
struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
struct bin_attribute *attr = attr_sd->s_elem.bin_attr.bin_attr;
+ struct bin_buffer *bb = NULL;
int error = -EINVAL;
if (!kobj || !attr)
@@ -135,14 +160,22 @@ static int open(struct inode * inode, struct file * file)
goto Error;
error = -ENOMEM;
- file->private_data = kmalloc(PAGE_SIZE, GFP_KERNEL);
- if (!file->private_data)
+ bb = kzalloc(sizeof(*bb), GFP_KERNEL);
+ if (!bb)
goto Error;
+ bb->buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
+ if (!bb->buffer)
+ goto Error;
+
+ mutex_init(&bb->mutex);
+ file->private_data = bb;
+
error = 0;
- goto Done;
+ goto Done;
Error:
+ kfree(bb);
module_put(attr->attr.owner);
Done:
if (error)
@@ -155,11 +188,12 @@ static int release(struct inode * inode, struct file * file)
struct kobject * kobj = to_kobj(file->f_path.dentry->d_parent);
struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
struct bin_attribute *attr = attr_sd->s_elem.bin_attr.bin_attr;
- u8 * buffer = file->private_data;
+ struct bin_buffer *bb = file->private_data;
kobject_put(kobj);
module_put(attr->attr.owner);
- kfree(buffer);
+ kfree(bb->buffer);
+ kfree(bb);
return 0;
}
--
1.5.0.3
next prev parent reply other threads:[~2007-04-09 4:21 UTC|newest]
Thread overview: 36+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-04-09 4:18 [PATCHSET #master] sysfs: make sysfs disconnect immediately on deletion, take 2 Tejun Heo
2007-04-09 4:18 ` [PATCH 03/14] sysfs: move release_sysfs_dirent() to dir.c Tejun Heo
2007-04-09 4:18 ` [PATCH 01/14] sysfs: fix i_ino handling in sysfs Tejun Heo
2007-04-27 15:29 ` Eric Sandeen
2007-04-27 15:32 ` Greg KH
2007-04-27 16:04 ` Eric Sandeen
2007-04-09 4:18 ` [PATCH 05/14] sysfs: consolidate sysfs_dirent creation functions Tejun Heo
2007-04-09 4:18 ` [PATCH 04/14] sysfs: flatten cleanup paths in sysfs_add_link() and create_dir() Tejun Heo
2007-04-09 4:18 ` [PATCH 02/14] sysfs: fix error handling in binattr write() Tejun Heo
2007-04-09 4:18 ` Tejun Heo [this message]
2007-04-09 4:18 ` [PATCH 07/14] sysfs: add sysfs_dirent->s_name Tejun Heo
2007-04-09 4:18 ` [PATCH 06/14] sysfs: add sysfs_dirent->s_parent Tejun Heo
2007-04-09 4:18 ` [PATCH 10/14] sysfs: reimplement symlink using sysfs_dirent tree Tejun Heo
2007-04-09 4:18 ` [PATCH 08/14] sysfs: make sysfs_dirent->s_element a union Tejun Heo
2007-04-09 4:18 ` [PATCH 09/14] sysfs: implement kobj_sysfs_assoc_lock Tejun Heo
2007-04-09 4:18 ` [PATCH 14/14] sysfs: kill unnecessary attribute->owner Tejun Heo
2007-04-10 14:17 ` Cornelia Huck
2007-04-10 14:30 ` Cornelia Huck
2007-04-11 4:21 ` Tejun Heo
2007-04-11 4:25 ` [PATCH 14/14 UPDATED] " Tejun Heo
2007-04-09 4:18 ` [PATCH 12/14] sysfs: implement sysfs_dirent active reference and immediate disconnect Tejun Heo
2007-04-11 4:15 ` [PATCH 12/14 UPDATED] " Tejun Heo
2007-04-11 9:00 ` Cornelia Huck
2007-04-11 9:26 ` Tejun Heo
2007-04-11 9:32 ` Tejun Heo
2007-04-11 10:13 ` Tejun Heo
2007-04-11 10:26 ` Cornelia Huck
2007-04-12 7:18 ` Greg KH
2007-04-12 7:39 ` Greg KH
2007-04-09 4:18 ` [PATCH 13/14] sysfs: kill attribute file orphaning Tejun Heo
2007-04-10 14:44 ` [PATCHSET #master] sysfs: make sysfs disconnect immediately on deletion, take 2 Cornelia Huck
2007-04-11 4:18 ` Tejun Heo
2007-04-16 8:22 ` Maneesh Soni
2007-04-17 5:06 ` Tejun Heo
2007-04-17 12:42 ` Tejun Heo
-- strict thread matches above, loose matches on Subject: below --
2007-04-07 8:23 [PATCHSET #master] sysfs: make sysfs disconnect immediately from kobject on deletion Tejun Heo
2007-04-07 8:23 ` [PATCH 11/14] sysfs: implement bin_buffer Tejun Heo
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=1176092328564-git-send-email-htejun@gmail.com \
--to=htejun@gmail.com \
--cc=James.Bottomley@SteelEye.com \
--cc=cornelia.huck@de.ibm.com \
--cc=dmitry.torokhov@gmail.com \
--cc=gregkh@suse.de \
--cc=linux-kernel@vger.kernel.org \
--cc=maneesh@in.ibm.com \
--cc=oneukum@suse.de \
--cc=rpurdie@rpsys.net \
--cc=stern@rowland.harvard.edu \
/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