From: Tejun Heo <tj@kernel.org>
To: gregkh@linuxfoundation.org
Cc: kay@vrfy.org, linux-kernel@vger.kernel.org,
ebiederm@xmission.com, bhelgaas@google.com,
Tejun Heo <tj@kernel.org>
Subject: [PATCH 14/34] sysfs, kernfs: prepare read path for kernfs
Date: Thu, 24 Oct 2013 11:49:20 -0400 [thread overview]
Message-ID: <1382629780-10006-15-git-send-email-tj@kernel.org> (raw)
In-Reply-To: <1382629780-10006-1-git-send-email-tj@kernel.org>
We're in the process of separating out core sysfs functionality into
kernfs which will deal with sysfs_dirents directly. This patch
rearranges read path so that the kernfs and sysfs parts are separate.
* Regular file read path is refactored such that
kernfs_seq_start/next/stop/show() handle all the boilerplate work
including locking and updating event count for poll, while
sysfs_kf_seq_show() deals with interaction with kobj show method.
* Bin file read path is refactored such that kernfs_file_direct_read()
handles all the boilerplate work including buffer management and
locking, while sysfs_kf_bin_read() deals with interaction with
bin_attribute read method.
kernfs_file_read() is added. It invokes either the seq_file or direct
read path depending on the file type. This will eventually allow
using the same file_operations for both file types, which is necessary
to separate out kernfs.
While this patch changes the order of some operations, it shouldn't
change any visible behavior.
Signed-off-by: Tejun Heo <tj@kernel.org>
---
fs/sysfs/file.c | 186 ++++++++++++++++++++++++++++++++++++--------------------
1 file changed, 121 insertions(+), 65 deletions(-)
diff --git a/fs/sysfs/file.c b/fs/sysfs/file.c
index 444615d..0f93330 100644
--- a/fs/sysfs/file.c
+++ b/fs/sysfs/file.c
@@ -86,13 +86,13 @@ static const struct sysfs_ops *sysfs_file_ops(struct sysfs_dirent *sd)
* details like buffering and seeking. The following function pipes
* sysfs_ops->show() result through seq_file.
*/
-static int sysfs_seq_show(struct seq_file *sf, void *v)
+static int sysfs_kf_seq_show(struct seq_file *sf, void *v)
{
struct sysfs_open_file *of = sf->private;
struct kobject *kobj = of->sd->s_parent->priv;
- const struct sysfs_ops *ops;
+ const struct sysfs_ops *ops = sysfs_file_ops(of->sd);
+ ssize_t count = 0;
char *buf;
- ssize_t count;
/* acquire buffer and ensure that it's >= PAGE_SIZE */
count = seq_get_buf(sf, &buf);
@@ -102,33 +102,14 @@ static int sysfs_seq_show(struct seq_file *sf, void *v)
}
/*
- * Need @of->sd for attr and ops, its parent for kobj. @of->mutex
- * nests outside active ref and is just to ensure that the ops
- * aren't called concurrently for the same open file.
- */
- mutex_lock(&of->mutex);
- if (!sysfs_get_active(of->sd)) {
- mutex_unlock(&of->mutex);
- return -ENODEV;
- }
-
- of->event = atomic_read(&of->sd->s_attr.open->event);
-
- /*
- * Lookup @ops and invoke show(). Control may reach here via seq
- * file lseek even if @ops->show() isn't implemented.
+ * Invoke show(). Control may reach here via seq file lseek even
+ * if @ops->show() isn't implemented.
*/
- ops = sysfs_file_ops(of->sd);
- if (ops->show)
+ if (ops->show) {
count = ops->show(kobj, of->sd->priv, buf);
- else
- count = 0;
-
- sysfs_put_active(of->sd);
- mutex_unlock(&of->mutex);
-
- if (count < 0)
- return count;
+ if (count < 0)
+ return count;
+ }
/*
* The code works fine with PAGE_SIZE return but it's likely to
@@ -144,68 +125,141 @@ static int sysfs_seq_show(struct seq_file *sf, void *v)
return 0;
}
-/*
- * Read method for bin files. As reading a bin file can have side-effects,
- * the exact offset and bytes specified in read(2) call should be passed to
- * the read callback making it difficult to use seq_file. Implement
- * simplistic custom buffering for bin files.
- */
-static ssize_t sysfs_bin_read(struct file *file, char __user *userbuf,
- size_t bytes, loff_t *off)
+static ssize_t sysfs_kf_bin_read(struct sysfs_open_file *of, char *buf,
+ size_t count, loff_t pos)
{
- struct sysfs_open_file *of = sysfs_of(file);
struct bin_attribute *battr = of->sd->priv;
struct kobject *kobj = of->sd->s_parent->priv;
- loff_t size = file_inode(file)->i_size;
- int count = min_t(size_t, bytes, PAGE_SIZE);
- loff_t offs = *off;
- char *buf;
+ loff_t size = file_inode(of->file)->i_size;
- if (!bytes)
+ if (!count)
return 0;
if (size) {
- if (offs > size)
+ if (pos > size)
return 0;
- if (offs + count > size)
- count = size - offs;
+ if (pos + count > size)
+ count = size - pos;
}
- buf = kmalloc(count, GFP_KERNEL);
+ if (!battr->read)
+ return -EIO;
+
+ return battr->read(of->file, kobj, battr, buf, pos, count);
+}
+
+static void *kernfs_seq_start(struct seq_file *sf, loff_t *ppos)
+{
+ struct sysfs_open_file *of = sf->private;
+
+ /*
+ * @of->mutex nests outside active ref and is just to ensure that
+ * the ops aren't called concurrently for the same open file.
+ */
+ mutex_lock(&of->mutex);
+ if (!sysfs_get_active(of->sd)) {
+ mutex_unlock(&of->mutex);
+ return ERR_PTR(-ENODEV);
+ }
+
+ /* the same behavior as single_open() */
+ return NULL + !*ppos;
+}
+
+static void *kernfs_seq_next(struct seq_file *sf, void *v, loff_t *ppos)
+{
+ ++*ppos;
+ return NULL;
+}
+
+static void kernfs_seq_stop(struct seq_file *sf, void *v)
+{
+ struct sysfs_open_file *of = sf->private;
+
+ sysfs_put_active(of->sd);
+ mutex_unlock(&of->mutex);
+}
+
+static int kernfs_seq_show(struct seq_file *sf, void *v)
+{
+ struct sysfs_open_file *of = sf->private;
+
+ of->event = atomic_read(&of->sd->s_attr.open->event);
+
+ return sysfs_kf_seq_show(sf, v);
+}
+
+static const struct seq_operations kernfs_seq_ops = {
+ .start = kernfs_seq_start,
+ .next = kernfs_seq_next,
+ .stop = kernfs_seq_stop,
+ .show = kernfs_seq_show,
+};
+
+/*
+ * As reading a bin file can have side-effects, the exact offset and bytes
+ * specified in read(2) call should be passed to the read callback making
+ * it difficult to use seq_file. Implement simplistic custom buffering for
+ * bin files.
+ */
+static ssize_t kernfs_file_direct_read(struct sysfs_open_file *of,
+ char __user *user_buf, size_t count,
+ loff_t *ppos)
+{
+ ssize_t len = min_t(size_t, count, PAGE_SIZE);
+ char *buf;
+
+ buf = kmalloc(len, GFP_KERNEL);
if (!buf)
return -ENOMEM;
- /* need of->sd for battr, its parent for kobj */
+ /*
+ * @of->mutex nests outside active ref and is just to ensure that
+ * the ops aren't called concurrently for the same open file.
+ */
mutex_lock(&of->mutex);
if (!sysfs_get_active(of->sd)) {
- count = -ENODEV;
+ len = -ENODEV;
mutex_unlock(&of->mutex);
goto out_free;
}
- if (battr->read)
- count = battr->read(file, kobj, battr, buf, offs, count);
- else
- count = -EIO;
+ len = sysfs_kf_bin_read(of, buf, len, *ppos);
sysfs_put_active(of->sd);
mutex_unlock(&of->mutex);
- if (count < 0)
+ if (len < 0)
goto out_free;
- if (copy_to_user(userbuf, buf, count)) {
- count = -EFAULT;
+ if (copy_to_user(user_buf, buf, len)) {
+ len = -EFAULT;
goto out_free;
}
- pr_debug("offs = %lld, *off = %lld, count = %d\n", offs, *off, count);
-
- *off = offs + count;
+ *ppos += len;
out_free:
kfree(buf);
- return count;
+ return len;
+}
+
+/**
+ * kernfs_file_read - kernfs vfs read callback
+ * @file: file pointer
+ * @user_buf: data to write
+ * @count: number of bytes
+ * @ppos: starting offset
+ */
+static ssize_t kernfs_file_read(struct file *file, char __user *user_buf,
+ size_t count, loff_t *ppos)
+{
+ struct sysfs_open_file *of = sysfs_of(file);
+
+ if (sysfs_is_bin(of->sd))
+ return kernfs_file_direct_read(of, user_buf, count, ppos);
+ else
+ return seq_read(file, user_buf, count, ppos);
}
/**
@@ -660,12 +714,14 @@ static int sysfs_open_file(struct inode *inode, struct file *file)
* and readable regular files are the vast majority anyway.
*/
if (sysfs_is_bin(attr_sd))
- error = single_open(file, NULL, of);
+ error = seq_open(file, NULL);
else
- error = single_open(file, sysfs_seq_show, of);
+ error = seq_open(file, &kernfs_seq_ops);
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;
@@ -680,7 +736,7 @@ static int sysfs_open_file(struct inode *inode, struct file *file)
return 0;
err_close:
- single_release(inode, file);
+ seq_release(inode, file);
err_free:
kfree(of);
err_out:
@@ -694,7 +750,7 @@ 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);
+ seq_release(inode, filp);
kfree(of);
return 0;
@@ -799,7 +855,7 @@ void sysfs_notify(struct kobject *k, const char *dir, const char *attr)
EXPORT_SYMBOL_GPL(sysfs_notify);
const struct file_operations sysfs_file_operations = {
- .read = seq_read,
+ .read = kernfs_file_read,
.write = sysfs_write_file,
.llseek = seq_lseek,
.open = sysfs_open_file,
@@ -808,7 +864,7 @@ const struct file_operations sysfs_file_operations = {
};
const struct file_operations sysfs_bin_operations = {
- .read = sysfs_bin_read,
+ .read = kernfs_file_read,
.write = sysfs_write_file,
.llseek = generic_file_llseek,
.mmap = sysfs_bin_mmap,
--
1.8.3.1
next prev parent reply other threads:[~2013-10-24 15:56 UTC|newest]
Thread overview: 46+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-10-24 15:49 Subject: [PATCHSET driver-core-next] sysfs: separate out kernfs, part #1 Tejun Heo
2013-10-24 15:49 ` [PATCH 01/34] sysfs: merge sysfs_elem_bin_attr into sysfs_elem_attr Tejun Heo
2013-10-24 15:49 ` [PATCH 02/34] sysfs: honor bin_attr.attr.ignore_lockdep Tejun Heo
2013-10-24 15:49 ` [PATCH 03/34] sysfs: remove unused sysfs_get_dentry() prototype Tejun Heo
2013-10-24 15:49 ` [PATCH 04/34] sysfs: move sysfs_hash_and_remove() to fs/sysfs/dir.c Tejun Heo
2013-10-24 15:49 ` [PATCH 05/34] sysfs: separate out dup filename warning into a separate function Tejun Heo
2013-10-24 15:49 ` [PATCH 06/34] sysfs: make __sysfs_add_one() fail if the parent isn't a directory Tejun Heo
2013-10-24 15:49 ` [PATCH 07/34] sysfs, kernfs: add skeletons for kernfs Tejun Heo
2013-10-24 15:49 ` [PATCH 08/34] sysfs, kernfs: introduce kernfs_remove[_by_name[_ns]]() Tejun Heo
2013-10-24 15:49 ` [PATCH 09/34] sysfs, kernfs: introduce kernfs_create_link() Tejun Heo
2013-10-24 15:49 ` [PATCH 10/34] sysfs, kernfs: introduce kernfs_rename[_ns]() Tejun Heo
2013-10-24 15:49 ` [PATCH 11/34] sysfs, kernfs: introduce kernfs_setattr() Tejun Heo
2013-10-24 15:49 ` [PATCH 12/34] sysfs, kernfs: replace sysfs_dirent->s_dir.kobj and ->s_attr.[bin_]attr with ->priv Tejun Heo
2013-10-24 15:49 ` [PATCH 13/34] sysfs, kernfs: introduce kernfs_create_dir[_ns]() Tejun Heo
2013-10-24 15:49 ` Tejun Heo [this message]
2013-10-26 11:32 ` [PATCH 14/34] sysfs, kernfs: prepare read path for kernfs Pavel Machek
2013-10-27 11:30 ` Tejun Heo
2013-10-28 13:03 ` [PATCH v2 " Tejun Heo
2013-10-28 16:33 ` [PATCH v3 " Tejun Heo
2013-10-24 15:49 ` [PATCH 15/34] sysfs, kernfs: prepare write " Tejun Heo
2013-10-24 15:49 ` [PATCH 16/34] sysfs, kernfs: prepare llseek " Tejun Heo
2013-10-24 15:49 ` [PATCH 17/34] sysfs, kernfs: prepare mmap " Tejun Heo
2013-10-24 15:49 ` [PATCH 18/34] sysfs, kernfs: prepare open, release, poll paths " Tejun Heo
2013-10-24 15:49 ` [PATCH 19/34] sysfs, kernfs: move sysfs_open_file to include/linux/kernfs.h Tejun Heo
2013-10-24 15:49 ` [PATCH 20/34] sysfs, kernfs: introduce kernfs_ops Tejun Heo
2013-10-24 15:49 ` [PATCH 21/34] sysfs, kernfs: add sysfs_dirent->s_attr.size Tejun Heo
2013-10-24 15:49 ` [PATCH 22/34] sysfs, kernfs: remove SYSFS_KOBJ_BIN_ATTR Tejun Heo
2013-10-24 15:49 ` [PATCH 23/34] sysfs, kernfs: introduce kernfs_create_file[_ns]() Tejun Heo
2013-10-24 15:49 ` [PATCH 24/34] sysfs, kernfs: remove sysfs_add_one() Tejun Heo
2013-10-24 15:49 ` [PATCH 25/34] sysfs, kernfs: add kernfs_ops->seq_{start|next|stop}() Tejun Heo
2013-10-28 13:04 ` [PATCH v2 " Tejun Heo
2013-10-24 15:49 ` [PATCH 26/34] sysfs, kernfs: introduce kernfs_notify() Tejun Heo
2013-10-24 15:49 ` [PATCH 27/34] sysfs, kernfs: reorganize SYSFS_* constants Tejun Heo
2013-10-24 15:49 ` [PATCH 28/34] sysfs, kernfs: revamp sysfs_dirent active_ref lockdep annotation Tejun Heo
2013-10-24 15:49 ` [PATCH 29/34] sysfs, kernfs: introduce kernfs[_find_and]_get() and kernfs_put() Tejun Heo
2013-10-24 15:49 ` [PATCH 30/34] sysfs, kernfs: move internal decls to fs/kernfs/kernfs-internal.h Tejun Heo
2013-10-24 15:49 ` [PATCH 31/34] sysfs, kernfs: move inode code to fs/kernfs/inode.c Tejun Heo
2013-10-24 15:49 ` [PATCH 32/34] sysfs, kernfs: move dir core code to fs/kernfs/dir.c Tejun Heo
2013-10-24 15:49 ` [PATCH 33/34] sysfs, kernfs: move file core code to fs/kernfs/file.c Tejun Heo
2013-10-28 13:04 ` [PATCH v2 " Tejun Heo
2013-10-28 16:34 ` [PATCH v3 " Tejun Heo
2013-10-24 15:49 ` [PATCH 34/34] sysfs, kernfs: move symlink core code to fs/kernfs/symlink.c Tejun Heo
2013-10-28 13:06 ` Subject: [PATCHSET driver-core-next] sysfs: separate out kernfs, part #1 Tejun Heo
2013-10-28 16:35 ` Tejun Heo
2013-10-29 22:29 ` Greg KH
2013-10-30 14:41 ` 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=1382629780-10006-15-git-send-email-tj@kernel.org \
--to=tj@kernel.org \
--cc=bhelgaas@google.com \
--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;
as well as URLs for NNTP newsgroup(s).