Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4d3773c

Browse files
htejungregkh
authored andcommitted
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 <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent d35258e commit 4d3773c

File tree

2 files changed

+37
-20
lines changed

2 files changed

+37
-20
lines changed

fs/kernfs/file.c

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -252,19 +252,9 @@ static ssize_t kernfs_fop_write(struct file *file, const char __user *user_buf,
252252
size_t count, loff_t *ppos)
253253
{
254254
struct kernfs_open_file *of = kernfs_of(file);
255-
ssize_t len = min_t(size_t, count, PAGE_SIZE);
256255
const struct kernfs_ops *ops;
257-
char *buf;
258-
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 */
256+
char *buf = NULL;
257+
ssize_t len;
268258

269259
/*
270260
* @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,
273263
mutex_lock(&of->mutex);
274264
if (!kernfs_get_active(of->kn)) {
275265
mutex_unlock(&of->mutex);
276-
len = -ENODEV;
277-
goto out_free;
266+
return -ENODEV;
278267
}
279268

280269
ops = kernfs_ops(of->kn);
281-
if (ops->write)
282-
len = ops->write(of, buf, len, *ppos);
283-
else
270+
if (!ops->write) {
284271
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+
}
285290

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:
286299
kernfs_put_active(of->kn);
287300
mutex_unlock(&of->mutex);
288301

289302
if (len > 0)
290303
*ppos += len;
291-
out_free:
304+
292305
kfree(buf);
293306
return len;
294307
}

include/linux/kernfs.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,13 @@ struct kernfs_ops {
178178
loff_t off);
179179

180180
/*
181-
* write() is bounced through kernel buffer and a write larger than
182-
* PAGE_SIZE results in partial operation of PAGE_SIZE.
181+
* write() is bounced through kernel buffer. If atomic_write_len
182+
* is not set, a write larger than PAGE_SIZE results in partial
183+
* operations of PAGE_SIZE chunks. If atomic_write_len is set,
184+
* writes upto the specified size are executed atomically but
185+
* larger ones are rejected with -E2BIG.
183186
*/
187+
size_t atomic_write_len;
184188
ssize_t (*write)(struct kernfs_open_file *of, char *buf, size_t bytes,
185189
loff_t off);
186190

0 commit comments

Comments
 (0)