diff options
author | Tejun Heo <tj@kernel.org> | 2014-02-03 14:09:13 -0500 |
---|---|---|
committer | Greg Kroah-Hartman <gregkh@linuxfoundation.org> | 2014-02-07 18:52:48 -0500 |
commit | 4d3773c4bb41ed5228f1ab7a4a52b79e17b10515 (patch) | |
tree | 71c30d7009b36d22e9aab816f3dc7e373bb5f1e9 /fs/kernfs | |
parent | d35258ef702cca0c4e66d799f8e38b78c02ce8a5 (diff) |
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: Tejun Heo <tj@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Diffstat (limited to 'fs/kernfs')
-rw-r--r-- | fs/kernfs/file.c | 49 |
1 files changed, 31 insertions, 18 deletions
diff --git a/fs/kernfs/file.c b/fs/kernfs/file.c index 10a8c91c49d6..ddcb471b9cc9 100644 --- a/fs/kernfs/file.c +++ b/fs/kernfs/file.c | |||
@@ -252,19 +252,9 @@ static ssize_t kernfs_fop_write(struct file *file, const char __user *user_buf, | |||
252 | size_t count, loff_t *ppos) | 252 | size_t count, loff_t *ppos) |
253 | { | 253 | { |
254 | struct kernfs_open_file *of = kernfs_of(file); | 254 | struct kernfs_open_file *of = kernfs_of(file); |
255 | ssize_t len = min_t(size_t, count, PAGE_SIZE); | ||
256 | const struct kernfs_ops *ops; | 255 | const struct kernfs_ops *ops; |
257 | char *buf; | 256 | char *buf = NULL; |
258 | 257 | ssize_t len; | |
259 | buf = kmalloc(len + 1, GFP_KERNEL); | ||
260 | if (!buf) | ||
261 | return -ENOMEM; | ||
262 | |||
263 | if (copy_from_user(buf, user_buf, len)) { | ||
264 | len = -EFAULT; | ||
265 | goto out_free; | ||
266 | } | ||
267 | buf[len] = '\0'; /* guarantee string termination */ | ||
268 | 258 | ||
269 | /* | 259 | /* |
270 | * @of->mutex nests outside active ref and is just to ensure that | 260 | * @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 | mutex_lock(&of->mutex); | 263 | mutex_lock(&of->mutex); |
274 | if (!kernfs_get_active(of->kn)) { | 264 | if (!kernfs_get_active(of->kn)) { |
275 | mutex_unlock(&of->mutex); | 265 | mutex_unlock(&of->mutex); |
276 | len = -ENODEV; | 266 | return -ENODEV; |
277 | goto out_free; | ||
278 | } | 267 | } |
279 | 268 | ||
280 | ops = kernfs_ops(of->kn); | 269 | ops = kernfs_ops(of->kn); |
281 | if (ops->write) | 270 | if (!ops->write) { |
282 | len = ops->write(of, buf, len, *ppos); | ||
283 | else | ||
284 | len = -EINVAL; | 271 | len = -EINVAL; |
272 | goto out_unlock; | ||
273 | } | ||
274 | |||
275 | if (ops->atomic_write_len) { | ||
276 | len = count; | ||
277 | if (len > ops->atomic_write_len) { | ||
278 | len = -E2BIG; | ||
279 | goto out_unlock; | ||
280 | } | ||
281 | } else { | ||
282 | len = min_t(size_t, count, PAGE_SIZE); | ||
283 | } | ||
284 | |||
285 | buf = kmalloc(len + 1, GFP_KERNEL); | ||
286 | if (!buf) { | ||
287 | len = -ENOMEM; | ||
288 | goto out_unlock; | ||
289 | } | ||
285 | 290 | ||
291 | if (copy_from_user(buf, user_buf, len)) { | ||
292 | len = -EFAULT; | ||
293 | goto out_unlock; | ||
294 | } | ||
295 | buf[len] = '\0'; /* guarantee string termination */ | ||
296 | |||
297 | len = ops->write(of, buf, len, *ppos); | ||
298 | out_unlock: | ||
286 | kernfs_put_active(of->kn); | 299 | kernfs_put_active(of->kn); |
287 | mutex_unlock(&of->mutex); | 300 | mutex_unlock(&of->mutex); |
288 | 301 | ||
289 | if (len > 0) | 302 | if (len > 0) |
290 | *ppos += len; | 303 | *ppos += len; |
291 | out_free: | 304 | |
292 | kfree(buf); | 305 | kfree(buf); |
293 | return len; | 306 | return len; |
294 | } | 307 | } |