[PATCH 04/11] fs: implement kernel_write using __kernel_write

From: Christoph Hellwig
Date: Fri May 08 2020 - 05:22:39 EST


Consolidate the two in-kernel write helpers to make upcoming changes
easier. The only difference are the missing call to rw_verify_area
in kernel_write, and an access_ok check that doesn't make sense for
kernel buffers to start with.

Signed-off-by: Christoph Hellwig <hch@xxxxxx>
---
fs/read_write.c | 13 +++++--------
1 file changed, 5 insertions(+), 8 deletions(-)

diff --git a/fs/read_write.c b/fs/read_write.c
index d91fe7ff6cc55..6b456a257b31c 100644
--- a/fs/read_write.c
+++ b/fs/read_write.c
@@ -531,16 +531,13 @@ EXPORT_SYMBOL(__kernel_write);
ssize_t kernel_write(struct file *file, const void *buf, size_t count,
loff_t *pos)
{
- mm_segment_t old_fs;
- ssize_t res;

- old_fs = get_fs();
- set_fs(KERNEL_DS);
- /* The cast to a user pointer is valid due to the set_fs() */
- res = vfs_write(file, (__force const char __user *)buf, count, pos);
- set_fs(old_fs);
+ ssize_t ret;

- return res;
+ ret = rw_verify_area(WRITE, file, pos, count);
+ if (ret)
+ return ret;
+ return __kernel_write(file, buf, count, pos);
}
EXPORT_SYMBOL(kernel_write);

--
2.26.2