From: Tejun Heo <tj@kernel.org>
To: gregkh@linuxfoundation.org
Cc: kay@vrfy.org, linux-kernel@vger.kernel.org,
ebiederm@xmission.com, Tejun Heo <tj@kernel.org>
Subject: [PATCH 13/14] sysfs: prepare open path for unified regular / bin file handling
Date: Sat, 28 Sep 2013 17:49:43 -0400 [thread overview]
Message-ID: <1380404984-31858-14-git-send-email-tj@kernel.org> (raw)
In-Reply-To: <1380404984-31858-1-git-send-email-tj@kernel.org>
sysfs bin file handling will be merged into the regular file support.
All three access paths - read, write and mmap - can now handle both
regular and bin files. This patch updates sysfs_open_file() and
sysfs_release() such that they can handle both regular and bin files.
This is a preparation and the new bin file path isn't used yet.
Signed-off-by: Tejun Heo <tj@kernel.org>
---
fs/sysfs/file.c | 61 ++++++++++++++++++++++++++++++++++-----------------------
1 file changed, 37 insertions(+), 24 deletions(-)
diff --git a/fs/sysfs/file.c b/fs/sysfs/file.c
index fe5c440..723f78c 100644
--- a/fs/sysfs/file.c
+++ b/fs/sysfs/file.c
@@ -638,38 +638,40 @@ static int sysfs_open_file(struct inode *inode, struct file *file)
struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
struct sysfs_open_file *of;
- const struct sysfs_ops *ops;
+ bool has_read, has_write;
int error = -EACCES;
/* need attr_sd for attr and ops, its parent for kobj */
if (!sysfs_get_active(attr_sd))
return -ENODEV;
- /* every kobject with an attribute needs a ktype assigned */
- ops = sysfs_file_ops(attr_sd);
- if (WARN(!ops, KERN_ERR
- "missing sysfs attribute operations for kobject: %s\n",
- kobject_name(kobj)))
- goto err_out;
+ if (sysfs_is_bin(attr_sd)) {
+ struct bin_attribute *battr = attr_sd->s_bin_attr.bin_attr;
- /* File needs write support.
- * The inode's perms must say it's ok,
- * and we must have a store method.
- */
- if (file->f_mode & FMODE_WRITE) {
- if (!(inode->i_mode & S_IWUGO) || !ops->store)
- goto err_out;
- }
+ has_read = battr->read || battr->mmap;
+ has_write = battr->write || battr->mmap;
+ } else {
+ const struct sysfs_ops *ops = sysfs_file_ops(attr_sd);
- /* File needs read support.
- * The inode's perms must say it's ok, and we there
- * must be a show method for it.
- */
- if (file->f_mode & FMODE_READ) {
- if (!(inode->i_mode & S_IRUGO) || !ops->show)
+ /* every kobject with an attribute needs a ktype assigned */
+ if (WARN(!ops, KERN_ERR
+ "missing sysfs attribute operations for kobject: %s\n",
+ kobject_name(kobj)))
goto err_out;
+
+ has_read = ops->show;
+ has_write = ops->store;
}
+ /* check perms and supported operations */
+ if ((file->f_mode & FMODE_WRITE) &&
+ (!(inode->i_mode & S_IWUGO) || !has_write))
+ goto err_out;
+
+ if ((file->f_mode & FMODE_READ) &&
+ (!(inode->i_mode & S_IRUGO) || !has_read))
+ goto err_out;
+
/* allocate a sysfs_open_file for the file */
error = -ENOMEM;
of = kzalloc(sizeof(struct sysfs_open_file), GFP_KERNEL);
@@ -685,10 +687,15 @@ static int sysfs_open_file(struct inode *inode, struct file *file)
* implemented or requested. This unifies private data access and
* most files are readable anyway.
*/
- error = single_open(file, sysfs_seq_show, of);
+ if (sysfs_is_bin(attr_sd))
+ error = seq_open(file, &sysfs_bin_seq_ops);
+ else
+ error = single_open(file, sysfs_seq_show, NULL);
if (error)
goto err_free;
+ ((struct seq_file *)file->private_data)->private = of;
+
/* seq_file clears PWRITE unconditionally, restore it if WRITE */
if (file->f_mode & FMODE_WRITE)
file->f_mode |= FMODE_PWRITE;
@@ -703,7 +710,10 @@ static int sysfs_open_file(struct inode *inode, struct file *file)
return 0;
err_close:
- single_release(inode, file);
+ if (sysfs_is_bin(attr_sd))
+ seq_release(inode, file);
+ else
+ single_release(inode, file);
err_free:
kfree(of);
err_out:
@@ -717,7 +727,10 @@ static int sysfs_release(struct inode *inode, struct file *filp)
struct sysfs_open_file *of = sysfs_of(filp);
sysfs_put_open_dirent(sd, of);
- single_release(inode, filp);
+ if (sysfs_is_bin(sd))
+ seq_release(inode, filp);
+ else
+ single_release(inode, filp);
kfree(of);
return 0;
--
1.8.3.1
next prev parent reply other threads:[~2013-09-28 21:51 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-09-28 21:49 [PATCHSET] sysfs: use seq_file and unify regular and " Tejun Heo
2013-09-28 21:49 ` [PATCH 01/14] sysfs: remove unused sysfs_buffer->pos Tejun Heo
2013-09-28 21:49 ` [PATCH 02/14] sysfs: remove sysfs_buffer->needs_read_fill Tejun Heo
2013-09-28 21:49 ` [PATCH 03/14] sysfs: remove sysfs_buffer->ops Tejun Heo
2013-09-28 21:49 ` [PATCH 04/14] sysfs: add sysfs_open_file_mutex Tejun Heo
2013-09-28 21:49 ` [PATCH 05/14] sysfs: rename sysfs_buffer to sysfs_open_file Tejun Heo
2013-09-28 21:49 ` [PATCH 06/14] sysfs: add sysfs_open_file->sd and ->file Tejun Heo
2013-09-28 21:49 ` [PATCH 07/14] sysfs: use transient write buffer Tejun Heo
2013-09-28 21:49 ` [PATCH 08/14] sysfs: use seq_file when reading regular files Tejun Heo
2013-09-28 21:49 ` [PATCH 09/14] sysfs: prepare llseek path for unified regular / bin file handling Tejun Heo
2013-09-28 21:49 ` [PATCH 10/14] sysfs: prepare path write " Tejun Heo
2013-09-28 21:49 ` [PATCH 11/14] sysfs: prepare read path " Tejun Heo
2013-09-28 21:49 ` [PATCH 12/14] sysfs: copy bin mmap support from fs/sysfs/bin.c to fs/sysfs/file.c Tejun Heo
2013-09-28 21:49 ` Tejun Heo [this message]
2013-09-28 21:49 ` [PATCH 14/14] sysfs: merge regular and bin file handling Tejun Heo
2013-09-28 22:15 ` [PATCHSET] sysfs: use seq_file and unify " Tejun Heo
2013-09-30 19:54 ` Tejun Heo
2013-09-30 20:14 ` Greg KH
2013-10-01 5:03 ` Bjorn Helgaas
2013-10-01 14:23 ` 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=1380404984-31858-14-git-send-email-tj@kernel.org \
--to=tj@kernel.org \
--cc=ebiederm@xmission.com \
--cc=gregkh@linuxfoundation.org \
--cc=kay@vrfy.org \
--cc=linux-kernel@vger.kernel.org \
/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
Powered by JetHome