Skip to content

Commit 61a707c

Browse files
author
Christoph Hellwig
committed
fs: add a __kernel_read helper
This is the counterpart to __kernel_write, and skip the rw_verify_area call compared to kernel_read. Signed-off-by: Christoph Hellwig <[email protected]>
1 parent 53ad862 commit 61a707c

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

fs/read_write.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,29 @@ ssize_t __vfs_read(struct file *file, char __user *buf, size_t count,
430430
return -EINVAL;
431431
}
432432

433+
ssize_t __kernel_read(struct file *file, void *buf, size_t count, loff_t *pos)
434+
{
435+
mm_segment_t old_fs = get_fs();
436+
ssize_t ret;
437+
438+
if (WARN_ON_ONCE(!(file->f_mode & FMODE_READ)))
439+
return -EINVAL;
440+
if (!(file->f_mode & FMODE_CAN_READ))
441+
return -EINVAL;
442+
443+
if (count > MAX_RW_COUNT)
444+
count = MAX_RW_COUNT;
445+
set_fs(KERNEL_DS);
446+
ret = __vfs_read(file, (void __user *)buf, count, pos);
447+
set_fs(old_fs);
448+
if (ret > 0) {
449+
fsnotify_access(file);
450+
add_rchar(current, ret);
451+
}
452+
inc_syscr(current);
453+
return ret;
454+
}
455+
433456
ssize_t kernel_read(struct file *file, void *buf, size_t count, loff_t *pos)
434457
{
435458
mm_segment_t old_fs;

include/linux/fs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3033,6 +3033,7 @@ extern int kernel_read_file_from_path_initns(const char *, void **, loff_t *, lo
30333033
extern int kernel_read_file_from_fd(int, void **, loff_t *, loff_t,
30343034
enum kernel_read_file_id);
30353035
extern ssize_t kernel_read(struct file *, void *, size_t, loff_t *);
3036+
ssize_t __kernel_read(struct file *file, void *buf, size_t count, loff_t *pos);
30363037
extern ssize_t kernel_write(struct file *, const void *, size_t, loff_t *);
30373038
extern ssize_t __kernel_write(struct file *, const void *, size_t, loff_t *);
30383039
extern struct file * open_exec(const char *);

0 commit comments

Comments
 (0)