Commit 4d3773c4 authored by Tejun Heo's avatar Tejun Heo Committed by Greg Kroah-Hartman

kernfs: implement kernfs_ops->atomic_write_len

A write to a kernfs_node is buffered through a kernel buffer.  Writes
<= PAGE_SIZE are performed atomically, while larger ones are executed
in PAGE_SIZE chunks.  While this is enough for sysfs, cgroup which is
scheduled to be converted to use kernfs needs a bit more control over
it.

This patch adds kernfs_ops->atomic_write_len.  If not set (zero), the
behavior stays the same.  If set, writes upto the size are executed
atomically and larger writes are rejected with -E2BIG.

A different implementation strategy would be allowing configuring
chunking size while making the original write size available to the
write method; however, such strategy, while being more complicated,
doesn't really buy anything.  If the write implementation has to
handle chunking, the specific chunk size shouldn't matter all that
much.
Signed-off-by: default avatarTejun Heo <tj@kernel.org>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent d35258ef
...@@ -252,19 +252,9 @@ static ssize_t kernfs_fop_write(struct file *file, const char __user *user_buf, ...@@ -252,19 +252,9 @@ static ssize_t kernfs_fop_write(struct file *file, const char __user *user_buf,
size_t count, loff_t *ppos) size_t count, loff_t *ppos)
{ {
struct kernfs_open_file *of = kernfs_of(file); struct kernfs_open_file *of = kernfs_of(file);
ssize_t len = min_t(size_t, count, PAGE_SIZE);
const struct kernfs_ops *ops; const struct kernfs_ops *ops;
char *buf; char *buf = NULL;
ssize_t len;
buf = kmalloc(len + 1, GFP_KERNEL);
if (!buf)
return -ENOMEM;
if (copy_from_user(buf, user_buf, len)) {
len = -EFAULT;
goto out_free;
}
buf[len] = '\0'; /* guarantee string termination */
/* /*
* @of->mutex nests outside active ref and is just to ensure that * @of->mutex nests outside active ref and is just to ensure that
...@@ -273,22 +263,45 @@ static ssize_t kernfs_fop_write(struct file *file, const char __user *user_buf, ...@@ -273,22 +263,45 @@ static ssize_t kernfs_fop_write(struct file *file, const char __user *user_buf,
mutex_lock(&of->mutex); mutex_lock(&of->mutex);
if (!kernfs_get_active(of->kn)) { if (!kernfs_get_active(of->kn)) {
mutex_unlock(&of->mutex); mutex_unlock(&of->mutex);
len = -ENODEV; return -ENODEV;
goto out_free;
} }
ops = kernfs_ops(of->kn); ops = kernfs_ops(of->kn);
if (ops->write) if (!ops->write) {
len = ops->write(of, buf, len, *ppos);
else
len = -EINVAL; len = -EINVAL;
goto out_unlock;
}
if (ops->atomic_write_len) {
len = count;
if (len > ops->atomic_write_len) {
len = -E2BIG;
goto out_unlock;
}
} else {
len = min_t(size_t, count, PAGE_SIZE);
}
buf = kmalloc(len + 1, GFP_KERNEL);
if (!buf) {
len = -ENOMEM;
goto out_unlock;
}
if (copy_from_user(buf, user_buf, len)) {
len = -EFAULT;
goto out_unlock;
}
buf[len] = '\0'; /* guarantee string termination */
len = ops->write(of, buf, len, *ppos);
out_unlock:
kernfs_put_active(of->kn); kernfs_put_active(of->kn);
mutex_unlock(&of->mutex); mutex_unlock(&of->mutex);
if (len > 0) if (len > 0)
*ppos += len; *ppos += len;
out_free:
kfree(buf); kfree(buf);
return len; return len;
} }
......
...@@ -178,9 +178,13 @@ struct kernfs_ops { ...@@ -178,9 +178,13 @@ struct kernfs_ops {
loff_t off); loff_t off);
/* /*
* write() is bounced through kernel buffer and a write larger than * write() is bounced through kernel buffer. If atomic_write_len
* PAGE_SIZE results in partial operation of PAGE_SIZE. * is not set, a write larger than PAGE_SIZE results in partial
* operations of PAGE_SIZE chunks. If atomic_write_len is set,
* writes upto the specified size are executed atomically but
* larger ones are rejected with -E2BIG.
*/ */
size_t atomic_write_len;
ssize_t (*write)(struct kernfs_open_file *of, char *buf, size_t bytes, ssize_t (*write)(struct kernfs_open_file *of, char *buf, size_t bytes,
loff_t off); loff_t off);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment