blob: e643aec2b0efe929bce63d6b4d1ddcf81b0b0399 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/fs/read_write.c
4 *
5 * Copyright (C) 1991, 1992 Linus Torvalds
6 */
7
Ingo Molnarb12fb7f2017-02-08 18:51:33 +01008#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/stat.h>
Ingo Molnarb12fb7f2017-02-08 18:51:33 +010010#include <linux/sched/xacct.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/fcntl.h>
12#include <linux/file.h>
13#include <linux/uio.h>
Robert Love0eeca282005-07-12 17:06:03 -040014#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/security.h>
Paul Gortmaker630d9c42011-11-16 23:57:37 -050016#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/syscalls.h>
Linus Torvaldse28cc712006-01-04 16:20:40 -080018#include <linux/pagemap.h>
Jens Axboed6b29d72007-06-04 09:59:47 +020019#include <linux/splice.h>
Al Viro561c6732013-02-24 10:52:26 -050020#include <linux/compat.h>
Zach Brown29732932015-11-10 16:53:30 -050021#include <linux/mount.h>
Wouter van Kesteren2feb55f2016-02-16 22:20:59 +010022#include <linux/fs.h>
Al Viro06ae43f32013-03-20 13:19:30 -040023#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080025#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <asm/unistd.h>
27
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080028const struct file_operations generic_ro_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 .llseek = generic_file_llseek,
Al Viroaad4f8b2014-04-02 14:33:16 -040030 .read_iter = generic_file_read_iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 .mmap = generic_file_readonly_mmap,
Jens Axboe534f2aa2007-06-01 14:52:37 +020032 .splice_read = generic_file_splice_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -070033};
34
35EXPORT_SYMBOL(generic_ro_fops);
36
Christoph Hellwigddef7ed22017-07-06 18:58:37 +020037static inline bool unsigned_offsets(struct file *file)
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -070038{
Al Virocccb5a12010-12-17 07:44:05 -050039 return file->f_mode & FMODE_UNSIGNED_OFFSET;
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -070040}
41
Jie Liu46a1c2c72013-06-25 12:02:13 +080042/**
43 * vfs_setpos - update the file offset for lseek
44 * @file: file structure in question
45 * @offset: file offset to seek to
46 * @maxsize: maximum file size
47 *
48 * This is a low-level filesystem helper for updating the file offset to
49 * the value specified by @offset if the given offset is valid and it is
50 * not equal to the current file offset.
51 *
52 * Return the specified offset on success and -EINVAL on invalid offset.
53 */
54loff_t vfs_setpos(struct file *file, loff_t offset, loff_t maxsize)
Andi Kleenef3d0fd2011-09-15 16:06:48 -070055{
56 if (offset < 0 && !unsigned_offsets(file))
57 return -EINVAL;
58 if (offset > maxsize)
59 return -EINVAL;
60
61 if (offset != file->f_pos) {
62 file->f_pos = offset;
63 file->f_version = 0;
64 }
65 return offset;
66}
Jie Liu46a1c2c72013-06-25 12:02:13 +080067EXPORT_SYMBOL(vfs_setpos);
Andi Kleenef3d0fd2011-09-15 16:06:48 -070068
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020069/**
Andi Kleen57604952011-09-15 16:06:50 -070070 * generic_file_llseek_size - generic llseek implementation for regular files
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020071 * @file: file structure to seek on
72 * @offset: file offset to seek to
Andrew Morton965c8e52012-12-17 15:59:39 -080073 * @whence: type of seek
Eric Sandeene8b96eb2012-04-30 13:11:29 -050074 * @size: max size of this file in file system
75 * @eof: offset used for SEEK_END position
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020076 *
Andi Kleen57604952011-09-15 16:06:50 -070077 * This is a variant of generic_file_llseek that allows passing in a custom
Eric Sandeene8b96eb2012-04-30 13:11:29 -050078 * maximum file size and a custom EOF position, for e.g. hashed directories
Andi Kleenef3d0fd2011-09-15 16:06:48 -070079 *
80 * Synchronization:
Andi Kleen57604952011-09-15 16:06:50 -070081 * SEEK_SET and SEEK_END are unsynchronized (but atomic on 64bit platforms)
Andi Kleenef3d0fd2011-09-15 16:06:48 -070082 * SEEK_CUR is synchronized against other SEEK_CURs, but not read/writes.
83 * read/writes behave like SEEK_SET against seeks.
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020084 */
Andi Kleen9465efc2008-06-27 11:05:24 +020085loff_t
Andrew Morton965c8e52012-12-17 15:59:39 -080086generic_file_llseek_size(struct file *file, loff_t offset, int whence,
Eric Sandeene8b96eb2012-04-30 13:11:29 -050087 loff_t maxsize, loff_t eof)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
Andrew Morton965c8e52012-12-17 15:59:39 -080089 switch (whence) {
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020090 case SEEK_END:
Eric Sandeene8b96eb2012-04-30 13:11:29 -050091 offset += eof;
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020092 break;
93 case SEEK_CUR:
Alain Knaff5b6f1eb2008-11-10 17:08:08 -080094 /*
95 * Here we special-case the lseek(fd, 0, SEEK_CUR)
96 * position-querying operation. Avoid rewriting the "same"
97 * f_pos value back to the file because a concurrent read(),
98 * write() or lseek() might have altered it
99 */
100 if (offset == 0)
101 return file->f_pos;
Andi Kleenef3d0fd2011-09-15 16:06:48 -0700102 /*
103 * f_lock protects against read/modify/write race with other
104 * SEEK_CURs. Note that parallel writes and reads behave
105 * like SEEK_SET.
106 */
107 spin_lock(&file->f_lock);
Jie Liu46a1c2c72013-06-25 12:02:13 +0800108 offset = vfs_setpos(file, file->f_pos + offset, maxsize);
Andi Kleenef3d0fd2011-09-15 16:06:48 -0700109 spin_unlock(&file->f_lock);
110 return offset;
Josef Bacik982d8162011-07-18 13:21:35 -0400111 case SEEK_DATA:
112 /*
113 * In the generic case the entire file is data, so as long as
114 * offset isn't at the end of the file then the offset is data.
115 */
Andreas Gruenbacherfc468202017-09-25 12:23:03 +0200116 if ((unsigned long long)offset >= eof)
Josef Bacik982d8162011-07-18 13:21:35 -0400117 return -ENXIO;
118 break;
119 case SEEK_HOLE:
120 /*
121 * There is a virtual hole at the end of the file, so as long as
122 * offset isn't i_size or larger, return i_size.
123 */
Andreas Gruenbacherfc468202017-09-25 12:23:03 +0200124 if ((unsigned long long)offset >= eof)
Josef Bacik982d8162011-07-18 13:21:35 -0400125 return -ENXIO;
Eric Sandeene8b96eb2012-04-30 13:11:29 -0500126 offset = eof;
Josef Bacik982d8162011-07-18 13:21:35 -0400127 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 }
Christoph Hellwig3a8cff42008-08-11 15:37:17 +0200129
Jie Liu46a1c2c72013-06-25 12:02:13 +0800130 return vfs_setpos(file, offset, maxsize);
Andi Kleen57604952011-09-15 16:06:50 -0700131}
132EXPORT_SYMBOL(generic_file_llseek_size);
133
134/**
135 * generic_file_llseek - generic llseek implementation for regular files
136 * @file: file structure to seek on
137 * @offset: file offset to seek to
Andrew Morton965c8e52012-12-17 15:59:39 -0800138 * @whence: type of seek
Andi Kleen57604952011-09-15 16:06:50 -0700139 *
140 * This is a generic implemenation of ->llseek useable for all normal local
141 * filesystems. It just updates the file offset to the value specified by
Ming Lei546ae2d2013-04-29 15:06:07 -0700142 * @offset and @whence.
Andi Kleen57604952011-09-15 16:06:50 -0700143 */
Andrew Morton965c8e52012-12-17 15:59:39 -0800144loff_t generic_file_llseek(struct file *file, loff_t offset, int whence)
Andi Kleen57604952011-09-15 16:06:50 -0700145{
146 struct inode *inode = file->f_mapping->host;
147
Andrew Morton965c8e52012-12-17 15:59:39 -0800148 return generic_file_llseek_size(file, offset, whence,
Eric Sandeene8b96eb2012-04-30 13:11:29 -0500149 inode->i_sb->s_maxbytes,
150 i_size_read(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
Andi Kleen9465efc2008-06-27 11:05:24 +0200152EXPORT_SYMBOL(generic_file_llseek);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
jan Blunckae6afc32010-05-26 14:44:48 -0700154/**
Al Viro1bf9d142013-06-16 20:27:42 +0400155 * fixed_size_llseek - llseek implementation for fixed-sized devices
156 * @file: file structure to seek on
157 * @offset: file offset to seek to
158 * @whence: type of seek
159 * @size: size of the file
160 *
161 */
162loff_t fixed_size_llseek(struct file *file, loff_t offset, int whence, loff_t size)
163{
164 switch (whence) {
165 case SEEK_SET: case SEEK_CUR: case SEEK_END:
166 return generic_file_llseek_size(file, offset, whence,
167 size, size);
168 default:
169 return -EINVAL;
170 }
171}
172EXPORT_SYMBOL(fixed_size_llseek);
173
174/**
Al Virob25472f2015-12-05 22:04:48 -0500175 * no_seek_end_llseek - llseek implementation for fixed-sized devices
176 * @file: file structure to seek on
177 * @offset: file offset to seek to
178 * @whence: type of seek
179 *
180 */
181loff_t no_seek_end_llseek(struct file *file, loff_t offset, int whence)
182{
183 switch (whence) {
184 case SEEK_SET: case SEEK_CUR:
185 return generic_file_llseek_size(file, offset, whence,
Wouter van Kesteren2feb55f2016-02-16 22:20:59 +0100186 OFFSET_MAX, 0);
Al Virob25472f2015-12-05 22:04:48 -0500187 default:
188 return -EINVAL;
189 }
190}
191EXPORT_SYMBOL(no_seek_end_llseek);
192
193/**
194 * no_seek_end_llseek_size - llseek implementation for fixed-sized devices
195 * @file: file structure to seek on
196 * @offset: file offset to seek to
197 * @whence: type of seek
198 * @size: maximal offset allowed
199 *
200 */
201loff_t no_seek_end_llseek_size(struct file *file, loff_t offset, int whence, loff_t size)
202{
203 switch (whence) {
204 case SEEK_SET: case SEEK_CUR:
205 return generic_file_llseek_size(file, offset, whence,
206 size, 0);
207 default:
208 return -EINVAL;
209 }
210}
211EXPORT_SYMBOL(no_seek_end_llseek_size);
212
213/**
jan Blunckae6afc32010-05-26 14:44:48 -0700214 * noop_llseek - No Operation Performed llseek implementation
215 * @file: file structure to seek on
216 * @offset: file offset to seek to
Andrew Morton965c8e52012-12-17 15:59:39 -0800217 * @whence: type of seek
jan Blunckae6afc32010-05-26 14:44:48 -0700218 *
219 * This is an implementation of ->llseek useable for the rare special case when
220 * userspace expects the seek to succeed but the (device) file is actually not
221 * able to perform the seek. In this case you use noop_llseek() instead of
222 * falling back to the default implementation of ->llseek.
223 */
Andrew Morton965c8e52012-12-17 15:59:39 -0800224loff_t noop_llseek(struct file *file, loff_t offset, int whence)
jan Blunckae6afc32010-05-26 14:44:48 -0700225{
226 return file->f_pos;
227}
228EXPORT_SYMBOL(noop_llseek);
229
Andrew Morton965c8e52012-12-17 15:59:39 -0800230loff_t no_llseek(struct file *file, loff_t offset, int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
232 return -ESPIPE;
233}
234EXPORT_SYMBOL(no_llseek);
235
Andrew Morton965c8e52012-12-17 15:59:39 -0800236loff_t default_llseek(struct file *file, loff_t offset, int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
Al Viro496ad9a2013-01-23 17:07:38 -0500238 struct inode *inode = file_inode(file);
David Sterba16abef02008-04-22 15:09:22 +0200239 loff_t retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Al Viro59551022016-01-22 15:40:57 -0500241 inode_lock(inode);
Andrew Morton965c8e52012-12-17 15:59:39 -0800242 switch (whence) {
Chris Snook7b8e8922007-05-08 00:24:13 -0700243 case SEEK_END:
Josef Bacik982d8162011-07-18 13:21:35 -0400244 offset += i_size_read(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 break;
Chris Snook7b8e8922007-05-08 00:24:13 -0700246 case SEEK_CUR:
Alain Knaff5b6f1eb2008-11-10 17:08:08 -0800247 if (offset == 0) {
248 retval = file->f_pos;
249 goto out;
250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 offset += file->f_pos;
Josef Bacik982d8162011-07-18 13:21:35 -0400252 break;
253 case SEEK_DATA:
254 /*
255 * In the generic case the entire file is data, so as
256 * long as offset isn't at the end of the file then the
257 * offset is data.
258 */
Dan Carpenterbacb2d82011-07-26 17:25:20 +0300259 if (offset >= inode->i_size) {
260 retval = -ENXIO;
261 goto out;
262 }
Josef Bacik982d8162011-07-18 13:21:35 -0400263 break;
264 case SEEK_HOLE:
265 /*
266 * There is a virtual hole at the end of the file, so
267 * as long as offset isn't i_size or larger, return
268 * i_size.
269 */
Dan Carpenterbacb2d82011-07-26 17:25:20 +0300270 if (offset >= inode->i_size) {
271 retval = -ENXIO;
272 goto out;
273 }
Josef Bacik982d8162011-07-18 13:21:35 -0400274 offset = inode->i_size;
275 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 }
277 retval = -EINVAL;
Al Virocccb5a12010-12-17 07:44:05 -0500278 if (offset >= 0 || unsigned_offsets(file)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 if (offset != file->f_pos) {
280 file->f_pos = offset;
281 file->f_version = 0;
282 }
283 retval = offset;
284 }
Alain Knaff5b6f1eb2008-11-10 17:08:08 -0800285out:
Al Viro59551022016-01-22 15:40:57 -0500286 inode_unlock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 return retval;
288}
289EXPORT_SYMBOL(default_llseek);
290
Andrew Morton965c8e52012-12-17 15:59:39 -0800291loff_t vfs_llseek(struct file *file, loff_t offset, int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
293 loff_t (*fn)(struct file *, loff_t, int);
294
295 fn = no_llseek;
296 if (file->f_mode & FMODE_LSEEK) {
Al Viro72c2d532013-09-22 16:27:52 -0400297 if (file->f_op->llseek)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 fn = file->f_op->llseek;
299 }
Andrew Morton965c8e52012-12-17 15:59:39 -0800300 return fn(file, offset, whence);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301}
302EXPORT_SYMBOL(vfs_llseek);
303
Christoph Hellwigbef17322020-06-06 14:49:58 +0200304static off_t ksys_lseek(unsigned int fd, off_t offset, unsigned int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
306 off_t retval;
Linus Torvalds9c225f22014-03-03 09:36:58 -0800307 struct fd f = fdget_pos(fd);
Al Viro2903ff02012-08-28 12:52:22 -0400308 if (!f.file)
309 return -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
311 retval = -EINVAL;
Andrew Morton965c8e52012-12-17 15:59:39 -0800312 if (whence <= SEEK_MAX) {
313 loff_t res = vfs_llseek(f.file, offset, whence);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 retval = res;
315 if (res != (loff_t)retval)
316 retval = -EOVERFLOW; /* LFS: should only happen on 32 bit platforms */
317 }
Linus Torvalds9c225f22014-03-03 09:36:58 -0800318 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 return retval;
320}
321
Dominik Brodowski76847e42018-03-13 21:51:17 +0100322SYSCALL_DEFINE3(lseek, unsigned int, fd, off_t, offset, unsigned int, whence)
323{
324 return ksys_lseek(fd, offset, whence);
325}
326
Al Viro561c6732013-02-24 10:52:26 -0500327#ifdef CONFIG_COMPAT
328COMPAT_SYSCALL_DEFINE3(lseek, unsigned int, fd, compat_off_t, offset, unsigned int, whence)
329{
Dominik Brodowski76847e42018-03-13 21:51:17 +0100330 return ksys_lseek(fd, offset, whence);
Al Viro561c6732013-02-24 10:52:26 -0500331}
332#endif
333
Michal Suchanek9e62cce2020-03-20 11:20:12 +0100334#if !defined(CONFIG_64BIT) || defined(CONFIG_COMPAT) || \
335 defined(__ARCH_WANT_SYS_LLSEEK)
Heiko Carstens003d7ab2009-01-14 14:14:21 +0100336SYSCALL_DEFINE5(llseek, unsigned int, fd, unsigned long, offset_high,
337 unsigned long, offset_low, loff_t __user *, result,
Andrew Morton965c8e52012-12-17 15:59:39 -0800338 unsigned int, whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
340 int retval;
Eric Biggersd7a15f82014-03-16 14:24:08 -0500341 struct fd f = fdget_pos(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 loff_t offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Al Viro2903ff02012-08-28 12:52:22 -0400344 if (!f.file)
345 return -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347 retval = -EINVAL;
Andrew Morton965c8e52012-12-17 15:59:39 -0800348 if (whence > SEEK_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 goto out_putf;
350
Al Viro2903ff02012-08-28 12:52:22 -0400351 offset = vfs_llseek(f.file, ((loff_t) offset_high << 32) | offset_low,
Andrew Morton965c8e52012-12-17 15:59:39 -0800352 whence);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 retval = (int)offset;
355 if (offset >= 0) {
356 retval = -EFAULT;
357 if (!copy_to_user(result, &offset, sizeof(offset)))
358 retval = 0;
359 }
360out_putf:
Eric Biggersd7a15f82014-03-16 14:24:08 -0500361 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 return retval;
363}
364#endif
365
Al Viro68d70d02013-06-19 15:26:04 +0400366int rw_verify_area(int read_write, struct file *file, const loff_t *ppos, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Linus Torvaldse28cc712006-01-04 16:20:40 -0800368 if (unlikely((ssize_t) count < 0))
Lukas Bulwahn2949e842021-08-24 13:12:59 +0200369 return -EINVAL;
Kirill Smelkov438ab722019-04-12 12:31:57 +0300370
Kirill Smelkov438ab722019-04-12 12:31:57 +0300371 if (ppos) {
372 loff_t pos = *ppos;
373
374 if (unlikely(pos < 0)) {
375 if (!unsigned_offsets(file))
Lukas Bulwahn2949e842021-08-24 13:12:59 +0200376 return -EINVAL;
Kirill Smelkov438ab722019-04-12 12:31:57 +0300377 if (count >= -pos) /* both values are in 0..LLONG_MAX */
378 return -EOVERFLOW;
379 } else if (unlikely((loff_t) (pos + count) < 0)) {
380 if (!unsigned_offsets(file))
Lukas Bulwahn2949e842021-08-24 13:12:59 +0200381 return -EINVAL;
Kirill Smelkov438ab722019-04-12 12:31:57 +0300382 }
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -0700383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Al Virobc613842016-03-31 21:48:20 -0400385 return security_file_permission(file,
James Morrisc43e2592008-01-12 22:05:48 +1100386 read_write == READ ? MAY_READ : MAY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387}
Omar Sandoval87112932019-09-04 12:13:25 -0700388EXPORT_SYMBOL(rw_verify_area);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Al Viro5d5d5682015-04-03 15:41:18 -0400390static ssize_t new_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
Al Viro293bc982014-02-11 18:37:41 -0500391{
392 struct iovec iov = { .iov_base = buf, .iov_len = len };
393 struct kiocb kiocb;
394 struct iov_iter iter;
395 ssize_t ret;
396
397 init_sync_kiocb(&kiocb, filp);
Kirill Smelkov438ab722019-04-12 12:31:57 +0300398 kiocb.ki_pos = (ppos ? *ppos : 0);
Al Viro293bc982014-02-11 18:37:41 -0500399 iov_iter_init(&iter, READ, &iov, 1, len);
400
Miklos Szeredibb7462b2017-02-20 16:51:23 +0100401 ret = call_read_iter(filp, &kiocb, &iter);
Christoph Hellwig599bd192015-02-11 19:59:44 +0100402 BUG_ON(ret == -EIOCBQUEUED);
Kirill Smelkov438ab722019-04-12 12:31:57 +0300403 if (ppos)
404 *ppos = kiocb.ki_pos;
Al Viro293bc982014-02-11 18:37:41 -0500405 return ret;
406}
407
Christoph Hellwig4d03e3c2020-09-03 16:22:33 +0200408static int warn_unsupported(struct file *file, const char *op)
409{
410 pr_warn_ratelimited(
411 "kernel %s not supported for file %pD4 (pid: %d comm: %.20s)\n",
412 op, file, current->pid, current->comm);
413 return -EINVAL;
414}
415
Christoph Hellwig61a707c2020-05-08 08:54:16 +0200416ssize_t __kernel_read(struct file *file, void *buf, size_t count, loff_t *pos)
417{
Christoph Hellwig4d03e3c2020-09-03 16:22:33 +0200418 struct kvec iov = {
419 .iov_base = buf,
420 .iov_len = min_t(size_t, count, MAX_RW_COUNT),
421 };
422 struct kiocb kiocb;
423 struct iov_iter iter;
Christoph Hellwig61a707c2020-05-08 08:54:16 +0200424 ssize_t ret;
425
426 if (WARN_ON_ONCE(!(file->f_mode & FMODE_READ)))
427 return -EINVAL;
428 if (!(file->f_mode & FMODE_CAN_READ))
429 return -EINVAL;
Christoph Hellwig4d03e3c2020-09-03 16:22:33 +0200430 /*
431 * Also fail if ->read_iter and ->read are both wired up as that
432 * implies very convoluted semantics.
433 */
434 if (unlikely(!file->f_op->read_iter || file->f_op->read))
435 return warn_unsupported(file, "read");
Christoph Hellwig61a707c2020-05-08 08:54:16 +0200436
Christoph Hellwig4d03e3c2020-09-03 16:22:33 +0200437 init_sync_kiocb(&kiocb, file);
Matthew Wilcox (Oracle)7b84b662020-10-03 03:55:23 +0100438 kiocb.ki_pos = pos ? *pos : 0;
Christoph Hellwig4d03e3c2020-09-03 16:22:33 +0200439 iov_iter_kvec(&iter, READ, &iov, 1, iov.iov_len);
440 ret = file->f_op->read_iter(&kiocb, &iter);
Christoph Hellwig61a707c2020-05-08 08:54:16 +0200441 if (ret > 0) {
Matthew Wilcox (Oracle)7b84b662020-10-03 03:55:23 +0100442 if (pos)
443 *pos = kiocb.ki_pos;
Christoph Hellwig61a707c2020-05-08 08:54:16 +0200444 fsnotify_access(file);
445 add_rchar(current, ret);
446 }
447 inc_syscr(current);
448 return ret;
449}
450
Christoph Hellwigbdd1d2d2017-09-01 17:39:13 +0200451ssize_t kernel_read(struct file *file, void *buf, size_t count, loff_t *pos)
Christoph Hellwigc41fbad2017-09-01 17:39:12 +0200452{
Christoph Hellwig6209dd92020-05-08 09:00:28 +0200453 ssize_t ret;
Christoph Hellwigc41fbad2017-09-01 17:39:12 +0200454
Christoph Hellwig6209dd92020-05-08 09:00:28 +0200455 ret = rw_verify_area(READ, file, pos, count);
456 if (ret)
457 return ret;
458 return __kernel_read(file, buf, count, pos);
Christoph Hellwigc41fbad2017-09-01 17:39:12 +0200459}
460EXPORT_SYMBOL(kernel_read);
Dmitry Kasatkin6fb50322014-11-05 17:01:17 +0200461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
463{
464 ssize_t ret;
465
466 if (!(file->f_mode & FMODE_READ))
467 return -EBADF;
Al Viro7f7f25e2014-02-11 17:49:24 -0500468 if (!(file->f_mode & FMODE_CAN_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 return -EINVAL;
Linus Torvalds96d4f262019-01-03 18:57:57 -0800470 if (unlikely(!access_ok(buf, count)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 return -EFAULT;
472
473 ret = rw_verify_area(READ, file, pos, count);
Christoph Hellwig775802c2020-05-08 11:17:46 +0200474 if (ret)
475 return ret;
476 if (count > MAX_RW_COUNT)
477 count = MAX_RW_COUNT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Christoph Hellwig775802c2020-05-08 11:17:46 +0200479 if (file->f_op->read)
480 ret = file->f_op->read(file, buf, count, pos);
481 else if (file->f_op->read_iter)
482 ret = new_sync_read(file, buf, count, pos);
483 else
484 ret = -EINVAL;
485 if (ret > 0) {
486 fsnotify_access(file);
487 add_rchar(current, ret);
488 }
489 inc_syscr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 return ret;
491}
492
Al Viro5d5d5682015-04-03 15:41:18 -0400493static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
Al Viro293bc982014-02-11 18:37:41 -0500494{
495 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len };
496 struct kiocb kiocb;
497 struct iov_iter iter;
498 ssize_t ret;
499
500 init_sync_kiocb(&kiocb, filp);
Kirill Smelkov438ab722019-04-12 12:31:57 +0300501 kiocb.ki_pos = (ppos ? *ppos : 0);
Al Viro293bc982014-02-11 18:37:41 -0500502 iov_iter_init(&iter, WRITE, &iov, 1, len);
503
Miklos Szeredibb7462b2017-02-20 16:51:23 +0100504 ret = call_write_iter(filp, &kiocb, &iter);
Christoph Hellwig599bd192015-02-11 19:59:44 +0100505 BUG_ON(ret == -EIOCBQUEUED);
Kirill Smelkov438ab722019-04-12 12:31:57 +0300506 if (ret > 0 && ppos)
Al Virof765b132015-04-06 20:50:38 -0400507 *ppos = kiocb.ki_pos;
Al Viro293bc982014-02-11 18:37:41 -0500508 return ret;
509}
510
Christoph Hellwig81238b22020-05-07 19:33:03 +0200511/* caller is responsible for file_start_write/file_end_write */
Christoph Hellwig73e18f72017-09-01 17:39:15 +0200512ssize_t __kernel_write(struct file *file, const void *buf, size_t count, loff_t *pos)
Al Viro06ae43f32013-03-20 13:19:30 -0400513{
Christoph Hellwig4d03e3c2020-09-03 16:22:33 +0200514 struct kvec iov = {
515 .iov_base = (void *)buf,
516 .iov_len = min_t(size_t, count, MAX_RW_COUNT),
517 };
518 struct kiocb kiocb;
519 struct iov_iter iter;
Al Viro06ae43f32013-03-20 13:19:30 -0400520 ssize_t ret;
521
Christoph Hellwiga01ac272020-05-08 08:55:03 +0200522 if (WARN_ON_ONCE(!(file->f_mode & FMODE_WRITE)))
523 return -EBADF;
Al Viro7f7f25e2014-02-11 17:49:24 -0500524 if (!(file->f_mode & FMODE_CAN_WRITE))
Al Viro3e84f482013-03-27 15:20:30 +0000525 return -EINVAL;
Christoph Hellwig4d03e3c2020-09-03 16:22:33 +0200526 /*
527 * Also fail if ->write_iter and ->write are both wired up as that
528 * implies very convoluted semantics.
529 */
530 if (unlikely(!file->f_op->write_iter || file->f_op->write))
531 return warn_unsupported(file, "write");
Al Viro3e84f482013-03-27 15:20:30 +0000532
Christoph Hellwig4d03e3c2020-09-03 16:22:33 +0200533 init_sync_kiocb(&kiocb, file);
Matthew Wilcox (Oracle)4c207ef2020-10-03 03:55:22 +0100534 kiocb.ki_pos = pos ? *pos : 0;
Christoph Hellwig4d03e3c2020-09-03 16:22:33 +0200535 iov_iter_kvec(&iter, WRITE, &iov, 1, iov.iov_len);
536 ret = file->f_op->write_iter(&kiocb, &iter);
Al Viro06ae43f32013-03-20 13:19:30 -0400537 if (ret > 0) {
Matthew Wilcox (Oracle)4c207ef2020-10-03 03:55:22 +0100538 if (pos)
539 *pos = kiocb.ki_pos;
Al Viro06ae43f32013-03-20 13:19:30 -0400540 fsnotify_modify(file);
541 add_wchar(current, ret);
542 }
543 inc_syscw(current);
544 return ret;
545}
Linus Torvalds90fb7022020-09-29 17:18:34 -0700546/*
547 * This "EXPORT_SYMBOL_GPL()" is more of a "EXPORT_SYMBOL_DONTUSE()",
548 * but autofs is one of the few internal kernel users that actually
549 * wants this _and_ can be built as a module. So we need to export
550 * this symbol for autofs, even though it really isn't appropriate
551 * for any other kernel modules.
552 */
553EXPORT_SYMBOL_GPL(__kernel_write);
Al Viro2ec3a122014-08-19 11:48:09 -0400554
Christoph Hellwige13ec932017-09-01 17:39:14 +0200555ssize_t kernel_write(struct file *file, const void *buf, size_t count,
556 loff_t *pos)
Christoph Hellwigac452ac2017-09-01 17:39:11 +0200557{
Christoph Hellwig81238b22020-05-07 19:33:03 +0200558 ssize_t ret;
Christoph Hellwigac452ac2017-09-01 17:39:11 +0200559
Christoph Hellwig81238b22020-05-07 19:33:03 +0200560 ret = rw_verify_area(WRITE, file, pos, count);
561 if (ret)
562 return ret;
Christoph Hellwigac452ac2017-09-01 17:39:11 +0200563
Christoph Hellwig81238b22020-05-07 19:33:03 +0200564 file_start_write(file);
565 ret = __kernel_write(file, buf, count, pos);
566 file_end_write(file);
567 return ret;
Christoph Hellwigac452ac2017-09-01 17:39:11 +0200568}
569EXPORT_SYMBOL(kernel_write);
570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
572{
573 ssize_t ret;
574
575 if (!(file->f_mode & FMODE_WRITE))
576 return -EBADF;
Al Viro7f7f25e2014-02-11 17:49:24 -0500577 if (!(file->f_mode & FMODE_CAN_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 return -EINVAL;
Linus Torvalds96d4f262019-01-03 18:57:57 -0800579 if (unlikely(!access_ok(buf, count)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 return -EFAULT;
581
582 ret = rw_verify_area(WRITE, file, pos, count);
Christoph Hellwig53ad8622020-05-13 08:51:46 +0200583 if (ret)
584 return ret;
585 if (count > MAX_RW_COUNT)
586 count = MAX_RW_COUNT;
587 file_start_write(file);
588 if (file->f_op->write)
589 ret = file->f_op->write(file, buf, count, pos);
590 else if (file->f_op->write_iter)
591 ret = new_sync_write(file, buf, count, pos);
592 else
593 ret = -EINVAL;
594 if (ret > 0) {
595 fsnotify_modify(file);
596 add_wchar(current, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 }
Christoph Hellwig53ad8622020-05-13 08:51:46 +0200598 inc_syscw(current);
599 file_end_write(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 return ret;
601}
602
Kirill Smelkov438ab722019-04-12 12:31:57 +0300603/* file_ppos returns &file->f_pos or NULL if file is stream */
604static inline loff_t *file_ppos(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
Kirill Smelkov438ab722019-04-12 12:31:57 +0300606 return file->f_mode & FMODE_STREAM ? NULL : &file->f_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607}
608
Dominik Brodowski3ce4a7b2018-03-13 21:56:26 +0100609ssize_t ksys_read(unsigned int fd, char __user *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
Linus Torvalds9c225f22014-03-03 09:36:58 -0800611 struct fd f = fdget_pos(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Al Viro2903ff02012-08-28 12:52:22 -0400614 if (f.file) {
Kirill Smelkov438ab722019-04-12 12:31:57 +0300615 loff_t pos, *ppos = file_ppos(f.file);
616 if (ppos) {
617 pos = *ppos;
618 ppos = &pos;
619 }
620 ret = vfs_read(f.file, buf, count, ppos);
621 if (ret >= 0 && ppos)
622 f.file->f_pos = pos;
Linus Torvalds9c225f22014-03-03 09:36:58 -0800623 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 return ret;
626}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Dominik Brodowski3ce4a7b2018-03-13 21:56:26 +0100628SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count)
629{
630 return ksys_read(fd, buf, count);
631}
632
Dominik Brodowskie7a3e8b2018-03-11 11:34:41 +0100633ssize_t ksys_write(unsigned int fd, const char __user *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
Linus Torvalds9c225f22014-03-03 09:36:58 -0800635 struct fd f = fdget_pos(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Al Viro2903ff02012-08-28 12:52:22 -0400638 if (f.file) {
Kirill Smelkov438ab722019-04-12 12:31:57 +0300639 loff_t pos, *ppos = file_ppos(f.file);
640 if (ppos) {
641 pos = *ppos;
642 ppos = &pos;
643 }
644 ret = vfs_write(f.file, buf, count, ppos);
645 if (ret >= 0 && ppos)
646 f.file->f_pos = pos;
Linus Torvalds9c225f22014-03-03 09:36:58 -0800647 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 }
649
650 return ret;
651}
652
Dominik Brodowskie7a3e8b2018-03-11 11:34:41 +0100653SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf,
654 size_t, count)
655{
656 return ksys_write(fd, buf, count);
657}
658
Dominik Brodowski36028d52018-03-19 17:38:31 +0100659ssize_t ksys_pread64(unsigned int fd, char __user *buf, size_t count,
660 loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
Al Viro2903ff02012-08-28 12:52:22 -0400662 struct fd f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665 if (pos < 0)
666 return -EINVAL;
667
Al Viro2903ff02012-08-28 12:52:22 -0400668 f = fdget(fd);
669 if (f.file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -0400671 if (f.file->f_mode & FMODE_PREAD)
672 ret = vfs_read(f.file, buf, count, &pos);
673 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 }
675
676 return ret;
677}
678
Dominik Brodowski36028d52018-03-19 17:38:31 +0100679SYSCALL_DEFINE4(pread64, unsigned int, fd, char __user *, buf,
680 size_t, count, loff_t, pos)
681{
682 return ksys_pread64(fd, buf, count, pos);
683}
684
685ssize_t ksys_pwrite64(unsigned int fd, const char __user *buf,
686 size_t count, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
Al Viro2903ff02012-08-28 12:52:22 -0400688 struct fd f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691 if (pos < 0)
692 return -EINVAL;
693
Al Viro2903ff02012-08-28 12:52:22 -0400694 f = fdget(fd);
695 if (f.file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -0400697 if (f.file->f_mode & FMODE_PWRITE)
698 ret = vfs_write(f.file, buf, count, &pos);
699 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 }
701
702 return ret;
703}
704
Dominik Brodowski36028d52018-03-19 17:38:31 +0100705SYSCALL_DEFINE4(pwrite64, unsigned int, fd, const char __user *, buf,
706 size_t, count, loff_t, pos)
707{
708 return ksys_pwrite64(fd, buf, count, pos);
709}
710
Al Viroac15ac02015-03-20 20:10:21 -0400711static ssize_t do_iter_readv_writev(struct file *filp, struct iov_iter *iter,
Christoph Hellwigddef7ed22017-07-06 18:58:37 +0200712 loff_t *ppos, int type, rwf_t flags)
Al Viro293bc982014-02-11 18:37:41 -0500713{
714 struct kiocb kiocb;
Al Viro293bc982014-02-11 18:37:41 -0500715 ssize_t ret;
716
717 init_sync_kiocb(&kiocb, filp);
Goldwyn Rodriguesfdd2f5b2017-06-20 07:05:40 -0500718 ret = kiocb_set_rw_flags(&kiocb, flags);
719 if (ret)
720 return ret;
Kirill Smelkov438ab722019-04-12 12:31:57 +0300721 kiocb.ki_pos = (ppos ? *ppos : 0);
Al Viro293bc982014-02-11 18:37:41 -0500722
Miklos Szeredi0f78d062017-02-20 16:51:23 +0100723 if (type == READ)
Miklos Szeredibb7462b2017-02-20 16:51:23 +0100724 ret = call_read_iter(filp, &kiocb, iter);
Miklos Szeredi0f78d062017-02-20 16:51:23 +0100725 else
Miklos Szeredibb7462b2017-02-20 16:51:23 +0100726 ret = call_write_iter(filp, &kiocb, iter);
Christoph Hellwig599bd192015-02-11 19:59:44 +0100727 BUG_ON(ret == -EIOCBQUEUED);
Kirill Smelkov438ab722019-04-12 12:31:57 +0300728 if (ppos)
729 *ppos = kiocb.ki_pos;
Al Viro293bc982014-02-11 18:37:41 -0500730 return ret;
731}
732
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700733/* Do it by hand, with file-ops */
Al Viroac15ac02015-03-20 20:10:21 -0400734static ssize_t do_loop_readv_writev(struct file *filp, struct iov_iter *iter,
Christoph Hellwigddef7ed22017-07-06 18:58:37 +0200735 loff_t *ppos, int type, rwf_t flags)
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700736{
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700737 ssize_t ret = 0;
738
Christoph Hellwig97be7eb2016-03-03 16:04:01 +0100739 if (flags & ~RWF_HIPRI)
Christoph Hellwig793b80e2016-03-03 16:03:58 +0100740 return -EOPNOTSUPP;
741
Al Viroac15ac02015-03-20 20:10:21 -0400742 while (iov_iter_count(iter)) {
743 struct iovec iovec = iov_iter_iovec(iter);
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700744 ssize_t nr;
745
Miklos Szeredi0f78d062017-02-20 16:51:23 +0100746 if (type == READ) {
747 nr = filp->f_op->read(filp, iovec.iov_base,
748 iovec.iov_len, ppos);
749 } else {
750 nr = filp->f_op->write(filp, iovec.iov_base,
751 iovec.iov_len, ppos);
752 }
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700753
754 if (nr < 0) {
755 if (!ret)
756 ret = nr;
757 break;
758 }
759 ret += nr;
Al Viroac15ac02015-03-20 20:10:21 -0400760 if (nr != iovec.iov_len)
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700761 break;
Al Viroac15ac02015-03-20 20:10:21 -0400762 iov_iter_advance(iter, nr);
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700763 }
764
765 return ret;
766}
767
Christoph Hellwig19c73582017-05-27 11:16:48 +0300768static ssize_t do_iter_read(struct file *file, struct iov_iter *iter,
Christoph Hellwigddef7ed22017-07-06 18:58:37 +0200769 loff_t *pos, rwf_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 size_t tot_len;
Miklos Szeredi7687a7a42017-02-20 16:51:23 +0100772 ssize_t ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Christoph Hellwigedab5fe2017-05-27 11:16:49 +0300774 if (!(file->f_mode & FMODE_READ))
775 return -EBADF;
776 if (!(file->f_mode & FMODE_CAN_READ))
777 return -EINVAL;
778
Miklos Szeredi7687a7a42017-02-20 16:51:23 +0100779 tot_len = iov_iter_count(iter);
Al Viro0504c072015-03-21 19:40:11 -0400780 if (!tot_len)
781 goto out;
Christoph Hellwig19c73582017-05-27 11:16:48 +0300782 ret = rw_verify_area(READ, file, pos, tot_len);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800783 if (ret < 0)
Christoph Hellwig19c73582017-05-27 11:16:48 +0300784 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Christoph Hellwig19c73582017-05-27 11:16:48 +0300786 if (file->f_op->read_iter)
787 ret = do_iter_readv_writev(file, iter, pos, READ, flags);
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700788 else
Christoph Hellwig19c73582017-05-27 11:16:48 +0300789 ret = do_loop_readv_writev(file, iter, pos, READ, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790out:
Christoph Hellwig19c73582017-05-27 11:16:48 +0300791 if (ret >= 0)
792 fsnotify_access(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794}
795
Jiufei Xue5dcdc432019-11-20 17:45:25 +0800796ssize_t vfs_iocb_iter_read(struct file *file, struct kiocb *iocb,
797 struct iov_iter *iter)
798{
799 size_t tot_len;
800 ssize_t ret = 0;
801
802 if (!file->f_op->read_iter)
803 return -EINVAL;
804 if (!(file->f_mode & FMODE_READ))
805 return -EBADF;
806 if (!(file->f_mode & FMODE_CAN_READ))
807 return -EINVAL;
808
809 tot_len = iov_iter_count(iter);
810 if (!tot_len)
811 goto out;
812 ret = rw_verify_area(READ, file, &iocb->ki_pos, tot_len);
813 if (ret < 0)
814 return ret;
815
816 ret = call_read_iter(file, iocb, iter);
817out:
818 if (ret >= 0)
819 fsnotify_access(file);
820 return ret;
821}
822EXPORT_SYMBOL(vfs_iocb_iter_read);
823
Christoph Hellwig18e97102017-05-27 11:16:51 +0300824ssize_t vfs_iter_read(struct file *file, struct iov_iter *iter, loff_t *ppos,
Christoph Hellwigddef7ed22017-07-06 18:58:37 +0200825 rwf_t flags)
Christoph Hellwig18e97102017-05-27 11:16:51 +0300826{
827 if (!file->f_op->read_iter)
828 return -EINVAL;
829 return do_iter_read(file, iter, ppos, flags);
830}
831EXPORT_SYMBOL(vfs_iter_read);
832
Christoph Hellwig19c73582017-05-27 11:16:48 +0300833static ssize_t do_iter_write(struct file *file, struct iov_iter *iter,
Christoph Hellwigddef7ed22017-07-06 18:58:37 +0200834 loff_t *pos, rwf_t flags)
Christoph Hellwig19c73582017-05-27 11:16:48 +0300835{
836 size_t tot_len;
837 ssize_t ret = 0;
838
Christoph Hellwigedab5fe2017-05-27 11:16:49 +0300839 if (!(file->f_mode & FMODE_WRITE))
840 return -EBADF;
841 if (!(file->f_mode & FMODE_CAN_WRITE))
842 return -EINVAL;
843
Christoph Hellwig19c73582017-05-27 11:16:48 +0300844 tot_len = iov_iter_count(iter);
845 if (!tot_len)
846 return 0;
847 ret = rw_verify_area(WRITE, file, pos, tot_len);
848 if (ret < 0)
849 return ret;
850
Christoph Hellwig19c73582017-05-27 11:16:48 +0300851 if (file->f_op->write_iter)
852 ret = do_iter_readv_writev(file, iter, pos, WRITE, flags);
853 else
854 ret = do_loop_readv_writev(file, iter, pos, WRITE, flags);
Christoph Hellwig19c73582017-05-27 11:16:48 +0300855 if (ret > 0)
856 fsnotify_modify(file);
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700857 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858}
859
Jiufei Xue5dcdc432019-11-20 17:45:25 +0800860ssize_t vfs_iocb_iter_write(struct file *file, struct kiocb *iocb,
861 struct iov_iter *iter)
862{
863 size_t tot_len;
864 ssize_t ret = 0;
865
866 if (!file->f_op->write_iter)
867 return -EINVAL;
868 if (!(file->f_mode & FMODE_WRITE))
869 return -EBADF;
870 if (!(file->f_mode & FMODE_CAN_WRITE))
871 return -EINVAL;
872
873 tot_len = iov_iter_count(iter);
874 if (!tot_len)
875 return 0;
876 ret = rw_verify_area(WRITE, file, &iocb->ki_pos, tot_len);
877 if (ret < 0)
878 return ret;
879
880 ret = call_write_iter(file, iocb, iter);
881 if (ret > 0)
882 fsnotify_modify(file);
883
884 return ret;
885}
886EXPORT_SYMBOL(vfs_iocb_iter_write);
887
Christoph Hellwigabbb65892017-05-27 11:16:52 +0300888ssize_t vfs_iter_write(struct file *file, struct iov_iter *iter, loff_t *ppos,
Christoph Hellwigddef7ed22017-07-06 18:58:37 +0200889 rwf_t flags)
Christoph Hellwigabbb65892017-05-27 11:16:52 +0300890{
891 if (!file->f_op->write_iter)
892 return -EINVAL;
893 return do_iter_write(file, iter, ppos, flags);
894}
895EXPORT_SYMBOL(vfs_iter_write);
896
Christoph Hellwig36e2c742020-09-03 16:22:34 +0200897static ssize_t vfs_readv(struct file *file, const struct iovec __user *vec,
Christoph Hellwigddef7ed22017-07-06 18:58:37 +0200898 unsigned long vlen, loff_t *pos, rwf_t flags)
Miklos Szeredi7687a7a42017-02-20 16:51:23 +0100899{
900 struct iovec iovstack[UIO_FASTIOV];
901 struct iovec *iov = iovstack;
902 struct iov_iter iter;
903 ssize_t ret;
904
Christoph Hellwig251b42a2017-05-27 11:16:46 +0300905 ret = import_iovec(READ, vec, vlen, ARRAY_SIZE(iovstack), &iov, &iter);
Christoph Hellwigedab5fe2017-05-27 11:16:49 +0300906 if (ret >= 0) {
907 ret = do_iter_read(file, &iter, pos, flags);
908 kfree(iov);
909 }
Miklos Szeredi7687a7a42017-02-20 16:51:23 +0100910
911 return ret;
912}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Christoph Hellwig9725d4c2017-09-01 17:39:25 +0200914static ssize_t vfs_writev(struct file *file, const struct iovec __user *vec,
Christoph Hellwigddef7ed22017-07-06 18:58:37 +0200915 unsigned long vlen, loff_t *pos, rwf_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
Christoph Hellwig251b42a2017-05-27 11:16:46 +0300917 struct iovec iovstack[UIO_FASTIOV];
918 struct iovec *iov = iovstack;
919 struct iov_iter iter;
920 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Christoph Hellwig251b42a2017-05-27 11:16:46 +0300922 ret = import_iovec(WRITE, vec, vlen, ARRAY_SIZE(iovstack), &iov, &iter);
Christoph Hellwigedab5fe2017-05-27 11:16:49 +0300923 if (ret >= 0) {
Al Viro62473a22017-07-06 09:15:47 -0400924 file_start_write(file);
Christoph Hellwigedab5fe2017-05-27 11:16:49 +0300925 ret = do_iter_write(file, &iter, pos, flags);
Al Viro62473a22017-07-06 09:15:47 -0400926 file_end_write(file);
Christoph Hellwigedab5fe2017-05-27 11:16:49 +0300927 kfree(iov);
928 }
Christoph Hellwig251b42a2017-05-27 11:16:46 +0300929 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Milosz Tanskif17d8b32016-03-03 16:03:59 +0100932static ssize_t do_readv(unsigned long fd, const struct iovec __user *vec,
Christoph Hellwigddef7ed22017-07-06 18:58:37 +0200933 unsigned long vlen, rwf_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
Linus Torvalds9c225f22014-03-03 09:36:58 -0800935 struct fd f = fdget_pos(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Al Viro2903ff02012-08-28 12:52:22 -0400938 if (f.file) {
Kirill Smelkov438ab722019-04-12 12:31:57 +0300939 loff_t pos, *ppos = file_ppos(f.file);
940 if (ppos) {
941 pos = *ppos;
942 ppos = &pos;
943 }
944 ret = vfs_readv(f.file, vec, vlen, ppos, flags);
945 if (ret >= 0 && ppos)
946 f.file->f_pos = pos;
Linus Torvalds9c225f22014-03-03 09:36:58 -0800947 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 }
949
950 if (ret > 0)
Alexey Dobriyan4b98d112007-02-10 01:46:45 -0800951 add_rchar(current, ret);
952 inc_syscr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 return ret;
954}
955
Milosz Tanskif17d8b32016-03-03 16:03:59 +0100956static ssize_t do_writev(unsigned long fd, const struct iovec __user *vec,
Christoph Hellwigddef7ed22017-07-06 18:58:37 +0200957 unsigned long vlen, rwf_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958{
Linus Torvalds9c225f22014-03-03 09:36:58 -0800959 struct fd f = fdget_pos(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
Al Viro2903ff02012-08-28 12:52:22 -0400962 if (f.file) {
Kirill Smelkov438ab722019-04-12 12:31:57 +0300963 loff_t pos, *ppos = file_ppos(f.file);
964 if (ppos) {
965 pos = *ppos;
966 ppos = &pos;
967 }
968 ret = vfs_writev(f.file, vec, vlen, ppos, flags);
969 if (ret >= 0 && ppos)
970 f.file->f_pos = pos;
Linus Torvalds9c225f22014-03-03 09:36:58 -0800971 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 }
973
974 if (ret > 0)
Alexey Dobriyan4b98d112007-02-10 01:46:45 -0800975 add_wchar(current, ret);
976 inc_syscw(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 return ret;
978}
979
Linus Torvalds601cc11d2009-04-03 08:03:22 -0700980static inline loff_t pos_from_hilo(unsigned long high, unsigned long low)
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700981{
Linus Torvalds601cc11d2009-04-03 08:03:22 -0700982#define HALF_LONG_BITS (BITS_PER_LONG / 2)
983 return (((loff_t)high << HALF_LONG_BITS) << HALF_LONG_BITS) | low;
984}
985
Milosz Tanskif17d8b32016-03-03 16:03:59 +0100986static ssize_t do_preadv(unsigned long fd, const struct iovec __user *vec,
Christoph Hellwigddef7ed22017-07-06 18:58:37 +0200987 unsigned long vlen, loff_t pos, rwf_t flags)
Linus Torvalds601cc11d2009-04-03 08:03:22 -0700988{
Al Viro2903ff02012-08-28 12:52:22 -0400989 struct fd f;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700990 ssize_t ret = -EBADF;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700991
992 if (pos < 0)
993 return -EINVAL;
994
Al Viro2903ff02012-08-28 12:52:22 -0400995 f = fdget(fd);
996 if (f.file) {
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700997 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -0400998 if (f.file->f_mode & FMODE_PREAD)
Milosz Tanskif17d8b32016-03-03 16:03:59 +0100999 ret = vfs_readv(f.file, vec, vlen, &pos, flags);
Al Viro2903ff02012-08-28 12:52:22 -04001000 fdput(f);
Gerd Hoffmannf3554f42009-04-02 16:59:23 -07001001 }
1002
1003 if (ret > 0)
1004 add_rchar(current, ret);
1005 inc_syscr(current);
1006 return ret;
1007}
1008
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001009static ssize_t do_pwritev(unsigned long fd, const struct iovec __user *vec,
Christoph Hellwigddef7ed22017-07-06 18:58:37 +02001010 unsigned long vlen, loff_t pos, rwf_t flags)
Gerd Hoffmannf3554f42009-04-02 16:59:23 -07001011{
Al Viro2903ff02012-08-28 12:52:22 -04001012 struct fd f;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -07001013 ssize_t ret = -EBADF;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -07001014
1015 if (pos < 0)
1016 return -EINVAL;
1017
Al Viro2903ff02012-08-28 12:52:22 -04001018 f = fdget(fd);
1019 if (f.file) {
Gerd Hoffmannf3554f42009-04-02 16:59:23 -07001020 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -04001021 if (f.file->f_mode & FMODE_PWRITE)
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001022 ret = vfs_writev(f.file, vec, vlen, &pos, flags);
Al Viro2903ff02012-08-28 12:52:22 -04001023 fdput(f);
Gerd Hoffmannf3554f42009-04-02 16:59:23 -07001024 }
1025
1026 if (ret > 0)
1027 add_wchar(current, ret);
1028 inc_syscw(current);
1029 return ret;
1030}
1031
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001032SYSCALL_DEFINE3(readv, unsigned long, fd, const struct iovec __user *, vec,
1033 unsigned long, vlen)
1034{
1035 return do_readv(fd, vec, vlen, 0);
1036}
1037
1038SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec __user *, vec,
1039 unsigned long, vlen)
1040{
1041 return do_writev(fd, vec, vlen, 0);
1042}
1043
1044SYSCALL_DEFINE5(preadv, unsigned long, fd, const struct iovec __user *, vec,
1045 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
1046{
1047 loff_t pos = pos_from_hilo(pos_h, pos_l);
1048
1049 return do_preadv(fd, vec, vlen, pos, 0);
1050}
1051
1052SYSCALL_DEFINE6(preadv2, unsigned long, fd, const struct iovec __user *, vec,
1053 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h,
Christoph Hellwigddef7ed22017-07-06 18:58:37 +02001054 rwf_t, flags)
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001055{
1056 loff_t pos = pos_from_hilo(pos_h, pos_l);
1057
1058 if (pos == -1)
1059 return do_readv(fd, vec, vlen, flags);
1060
1061 return do_preadv(fd, vec, vlen, pos, flags);
1062}
1063
1064SYSCALL_DEFINE5(pwritev, unsigned long, fd, const struct iovec __user *, vec,
1065 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
1066{
1067 loff_t pos = pos_from_hilo(pos_h, pos_l);
1068
1069 return do_pwritev(fd, vec, vlen, pos, 0);
1070}
1071
1072SYSCALL_DEFINE6(pwritev2, unsigned long, fd, const struct iovec __user *, vec,
1073 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h,
Christoph Hellwigddef7ed22017-07-06 18:58:37 +02001074 rwf_t, flags)
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001075{
1076 loff_t pos = pos_from_hilo(pos_h, pos_l);
1077
1078 if (pos == -1)
1079 return do_writev(fd, vec, vlen, flags);
1080
1081 return do_pwritev(fd, vec, vlen, pos, flags);
1082}
1083
Christoph Hellwig3523a9d2020-09-25 06:51:42 +02001084/*
1085 * Various compat syscalls. Note that they all pretend to take a native
1086 * iovec - import_iovec will properly treat those as compat_iovecs based on
1087 * in_compat_syscall().
1088 */
Al Viro72ec3512013-03-20 10:42:10 -04001089#ifdef CONFIG_COMPAT
Heiko Carstens378a10f2014-03-05 10:43:51 +01001090#ifdef __ARCH_WANT_COMPAT_SYS_PREADV64
1091COMPAT_SYSCALL_DEFINE4(preadv64, unsigned long, fd,
Christoph Hellwig3523a9d2020-09-25 06:51:42 +02001092 const struct iovec __user *, vec,
Heiko Carstens378a10f2014-03-05 10:43:51 +01001093 unsigned long, vlen, loff_t, pos)
1094{
Christoph Hellwig3523a9d2020-09-25 06:51:42 +02001095 return do_preadv(fd, vec, vlen, pos, 0);
Heiko Carstens378a10f2014-03-05 10:43:51 +01001096}
1097#endif
1098
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001099COMPAT_SYSCALL_DEFINE5(preadv, compat_ulong_t, fd,
Christoph Hellwig3523a9d2020-09-25 06:51:42 +02001100 const struct iovec __user *, vec,
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001101 compat_ulong_t, vlen, u32, pos_low, u32, pos_high)
Al Viro72ec3512013-03-20 10:42:10 -04001102{
1103 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
Heiko Carstens378a10f2014-03-05 10:43:51 +01001104
Christoph Hellwig3523a9d2020-09-25 06:51:42 +02001105 return do_preadv(fd, vec, vlen, pos, 0);
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001106}
1107
H.J. Lu3ebfd812016-07-14 12:31:53 -07001108#ifdef __ARCH_WANT_COMPAT_SYS_PREADV64V2
1109COMPAT_SYSCALL_DEFINE5(preadv64v2, unsigned long, fd,
Christoph Hellwig3523a9d2020-09-25 06:51:42 +02001110 const struct iovec __user *, vec,
Christoph Hellwigddef7ed22017-07-06 18:58:37 +02001111 unsigned long, vlen, loff_t, pos, rwf_t, flags)
H.J. Lu3ebfd812016-07-14 12:31:53 -07001112{
Aurelien Jarnocc4b1242018-12-06 20:05:34 +01001113 if (pos == -1)
Christoph Hellwig3523a9d2020-09-25 06:51:42 +02001114 return do_readv(fd, vec, vlen, flags);
1115 return do_preadv(fd, vec, vlen, pos, flags);
H.J. Lu3ebfd812016-07-14 12:31:53 -07001116}
1117#endif
1118
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001119COMPAT_SYSCALL_DEFINE6(preadv2, compat_ulong_t, fd,
Christoph Hellwig3523a9d2020-09-25 06:51:42 +02001120 const struct iovec __user *, vec,
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001121 compat_ulong_t, vlen, u32, pos_low, u32, pos_high,
Christoph Hellwigddef7ed22017-07-06 18:58:37 +02001122 rwf_t, flags)
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001123{
1124 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
1125
1126 if (pos == -1)
Christoph Hellwig3523a9d2020-09-25 06:51:42 +02001127 return do_readv(fd, vec, vlen, flags);
1128 return do_preadv(fd, vec, vlen, pos, flags);
Al Viro72ec3512013-03-20 10:42:10 -04001129}
1130
Heiko Carstens378a10f2014-03-05 10:43:51 +01001131#ifdef __ARCH_WANT_COMPAT_SYS_PWRITEV64
1132COMPAT_SYSCALL_DEFINE4(pwritev64, unsigned long, fd,
Christoph Hellwig3523a9d2020-09-25 06:51:42 +02001133 const struct iovec __user *, vec,
Heiko Carstens378a10f2014-03-05 10:43:51 +01001134 unsigned long, vlen, loff_t, pos)
1135{
Christoph Hellwig3523a9d2020-09-25 06:51:42 +02001136 return do_pwritev(fd, vec, vlen, pos, 0);
Heiko Carstens378a10f2014-03-05 10:43:51 +01001137}
1138#endif
1139
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001140COMPAT_SYSCALL_DEFINE5(pwritev, compat_ulong_t, fd,
Christoph Hellwig3523a9d2020-09-25 06:51:42 +02001141 const struct iovec __user *,vec,
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001142 compat_ulong_t, vlen, u32, pos_low, u32, pos_high)
Al Viro72ec3512013-03-20 10:42:10 -04001143{
1144 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
Heiko Carstens378a10f2014-03-05 10:43:51 +01001145
Christoph Hellwig3523a9d2020-09-25 06:51:42 +02001146 return do_pwritev(fd, vec, vlen, pos, 0);
Al Viro72ec3512013-03-20 10:42:10 -04001147}
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001148
H.J. Lu3ebfd812016-07-14 12:31:53 -07001149#ifdef __ARCH_WANT_COMPAT_SYS_PWRITEV64V2
1150COMPAT_SYSCALL_DEFINE5(pwritev64v2, unsigned long, fd,
Christoph Hellwig3523a9d2020-09-25 06:51:42 +02001151 const struct iovec __user *, vec,
Christoph Hellwigddef7ed22017-07-06 18:58:37 +02001152 unsigned long, vlen, loff_t, pos, rwf_t, flags)
H.J. Lu3ebfd812016-07-14 12:31:53 -07001153{
Aurelien Jarnocc4b1242018-12-06 20:05:34 +01001154 if (pos == -1)
Christoph Hellwig3523a9d2020-09-25 06:51:42 +02001155 return do_writev(fd, vec, vlen, flags);
1156 return do_pwritev(fd, vec, vlen, pos, flags);
H.J. Lu3ebfd812016-07-14 12:31:53 -07001157}
1158#endif
1159
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001160COMPAT_SYSCALL_DEFINE6(pwritev2, compat_ulong_t, fd,
Christoph Hellwig3523a9d2020-09-25 06:51:42 +02001161 const struct iovec __user *,vec,
Christoph Hellwigddef7ed22017-07-06 18:58:37 +02001162 compat_ulong_t, vlen, u32, pos_low, u32, pos_high, rwf_t, flags)
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001163{
1164 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
1165
1166 if (pos == -1)
Christoph Hellwig3523a9d2020-09-25 06:51:42 +02001167 return do_writev(fd, vec, vlen, flags);
1168 return do_pwritev(fd, vec, vlen, pos, flags);
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001169}
Christoph Hellwig3523a9d2020-09-25 06:51:42 +02001170#endif /* CONFIG_COMPAT */
Al Viro72ec3512013-03-20 10:42:10 -04001171
Al Viro19f4fc32013-02-24 02:17:03 -05001172static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,
1173 size_t count, loff_t max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174{
Al Viro2903ff02012-08-28 12:52:22 -04001175 struct fd in, out;
1176 struct inode *in_inode, *out_inode;
Al Virob964bf52021-01-25 22:24:28 -05001177 struct pipe_inode_info *opipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 loff_t pos;
Al Viro7995bd22013-06-20 18:58:36 +04001179 loff_t out_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 ssize_t retval;
Al Viro2903ff02012-08-28 12:52:22 -04001181 int fl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
1183 /*
1184 * Get input file, and verify that it is ok..
1185 */
1186 retval = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -04001187 in = fdget(in_fd);
1188 if (!in.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 goto out;
Al Viro2903ff02012-08-28 12:52:22 -04001190 if (!(in.file->f_mode & FMODE_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 goto fput_in;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 retval = -ESPIPE;
Al Viro7995bd22013-06-20 18:58:36 +04001193 if (!ppos) {
1194 pos = in.file->f_pos;
1195 } else {
1196 pos = *ppos;
Al Viro2903ff02012-08-28 12:52:22 -04001197 if (!(in.file->f_mode & FMODE_PREAD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 goto fput_in;
Al Viro7995bd22013-06-20 18:58:36 +04001199 }
1200 retval = rw_verify_area(READ, in.file, &pos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -08001201 if (retval < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 goto fput_in;
Al Virobc613842016-03-31 21:48:20 -04001203 if (count > MAX_RW_COUNT)
1204 count = MAX_RW_COUNT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 /*
1207 * Get output file, and verify that it is ok..
1208 */
1209 retval = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -04001210 out = fdget(out_fd);
1211 if (!out.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 goto fput_in;
Al Viro2903ff02012-08-28 12:52:22 -04001213 if (!(out.file->f_mode & FMODE_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 goto fput_out;
Al Viro496ad9a2013-01-23 17:07:38 -05001215 in_inode = file_inode(in.file);
1216 out_inode = file_inode(out.file);
Al Viro7995bd22013-06-20 18:58:36 +04001217 out_pos = out.file->f_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 if (!max)
1220 max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes);
1221
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 if (unlikely(pos + count > max)) {
1223 retval = -EOVERFLOW;
1224 if (pos >= max)
1225 goto fput_out;
1226 count = max - pos;
1227 }
1228
Jens Axboed96e6e72007-06-11 12:18:52 +02001229 fl = 0;
Jens Axboe534f2aa2007-06-01 14:52:37 +02001230#if 0
Jens Axboed96e6e72007-06-11 12:18:52 +02001231 /*
1232 * We need to debate whether we can enable this or not. The
1233 * man page documents EAGAIN return for the output at least,
1234 * and the application is arguably buggy if it doesn't expect
1235 * EAGAIN on a non-blocking file descriptor.
1236 */
Al Viro2903ff02012-08-28 12:52:22 -04001237 if (in.file->f_flags & O_NONBLOCK)
Jens Axboed96e6e72007-06-11 12:18:52 +02001238 fl = SPLICE_F_NONBLOCK;
Jens Axboe534f2aa2007-06-01 14:52:37 +02001239#endif
Al Virob964bf52021-01-25 22:24:28 -05001240 opipe = get_pipe_info(out.file, true);
1241 if (!opipe) {
1242 retval = rw_verify_area(WRITE, out.file, &out_pos, count);
1243 if (retval < 0)
1244 goto fput_out;
1245 file_start_write(out.file);
1246 retval = do_splice_direct(in.file, &pos, out.file, &out_pos,
1247 count, fl);
1248 file_end_write(out.file);
1249 } else {
1250 retval = splice_file_to_pipe(in.file, opipe, &pos, count, fl);
1251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
1253 if (retval > 0) {
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08001254 add_rchar(current, retval);
1255 add_wchar(current, retval);
Scott Wolchoka68c2f12012-12-20 15:05:52 -08001256 fsnotify_access(in.file);
1257 fsnotify_modify(out.file);
Al Viro7995bd22013-06-20 18:58:36 +04001258 out.file->f_pos = out_pos;
1259 if (ppos)
1260 *ppos = pos;
1261 else
1262 in.file->f_pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08001265 inc_syscr(current);
1266 inc_syscw(current);
Al Viro7995bd22013-06-20 18:58:36 +04001267 if (pos > max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 retval = -EOVERFLOW;
1269
1270fput_out:
Al Viro2903ff02012-08-28 12:52:22 -04001271 fdput(out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272fput_in:
Al Viro2903ff02012-08-28 12:52:22 -04001273 fdput(in);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274out:
1275 return retval;
1276}
1277
Heiko Carstens002c8972009-01-14 14:14:18 +01001278SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd, off_t __user *, offset, size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279{
1280 loff_t pos;
1281 off_t off;
1282 ssize_t ret;
1283
1284 if (offset) {
1285 if (unlikely(get_user(off, offset)))
1286 return -EFAULT;
1287 pos = off;
1288 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
1289 if (unlikely(put_user(pos, offset)))
1290 return -EFAULT;
1291 return ret;
1292 }
1293
1294 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1295}
1296
Heiko Carstens002c8972009-01-14 14:14:18 +01001297SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd, loff_t __user *, offset, size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298{
1299 loff_t pos;
1300 ssize_t ret;
1301
1302 if (offset) {
1303 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
1304 return -EFAULT;
1305 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
1306 if (unlikely(put_user(pos, offset)))
1307 return -EFAULT;
1308 return ret;
1309 }
1310
1311 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1312}
Al Viro19f4fc32013-02-24 02:17:03 -05001313
1314#ifdef CONFIG_COMPAT
1315COMPAT_SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd,
1316 compat_off_t __user *, offset, compat_size_t, count)
1317{
1318 loff_t pos;
1319 off_t off;
1320 ssize_t ret;
1321
1322 if (offset) {
1323 if (unlikely(get_user(off, offset)))
1324 return -EFAULT;
1325 pos = off;
1326 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
1327 if (unlikely(put_user(pos, offset)))
1328 return -EFAULT;
1329 return ret;
1330 }
1331
1332 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1333}
1334
1335COMPAT_SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd,
1336 compat_loff_t __user *, offset, compat_size_t, count)
1337{
1338 loff_t pos;
1339 ssize_t ret;
1340
1341 if (offset) {
1342 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
1343 return -EFAULT;
1344 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
1345 if (unlikely(put_user(pos, offset)))
1346 return -EFAULT;
1347 return ret;
1348 }
1349
1350 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1351}
1352#endif
Zach Brown29732932015-11-10 16:53:30 -05001353
Dave Chinnerf16acc92019-06-05 08:04:47 -07001354/**
1355 * generic_copy_file_range - copy data between two files
1356 * @file_in: file structure to read from
1357 * @pos_in: file offset to read from
1358 * @file_out: file structure to write data to
1359 * @pos_out: file offset to write data to
1360 * @len: amount of data to copy
1361 * @flags: copy flags
1362 *
1363 * This is a generic filesystem helper to copy data from one file to another.
1364 * It has no constraints on the source or destination file owners - the files
1365 * can belong to different superblocks and different filesystem types. Short
1366 * copies are allowed.
1367 *
1368 * This should be called from the @file_out filesystem, as per the
1369 * ->copy_file_range() method.
1370 *
1371 * Returns the number of bytes copied or a negative error indicating the
1372 * failure.
1373 */
1374
1375ssize_t generic_copy_file_range(struct file *file_in, loff_t pos_in,
1376 struct file *file_out, loff_t pos_out,
1377 size_t len, unsigned int flags)
1378{
1379 return do_splice_direct(file_in, &pos_in, file_out, &pos_out,
1380 len > MAX_RW_COUNT ? MAX_RW_COUNT : len, 0);
1381}
1382EXPORT_SYMBOL(generic_copy_file_range);
1383
Dave Chinner64bf5ff2019-06-05 08:04:47 -07001384static ssize_t do_copy_file_range(struct file *file_in, loff_t pos_in,
1385 struct file *file_out, loff_t pos_out,
1386 size_t len, unsigned int flags)
1387{
Amir Goldstein5dae2222019-06-05 08:04:50 -07001388 /*
1389 * Although we now allow filesystems to handle cross sb copy, passing
1390 * a file of the wrong filesystem type to filesystem driver can result
1391 * in an attempt to dereference the wrong type of ->private_data, so
1392 * avoid doing that until we really have a good reason. NFS defines
1393 * several different file_system_type structures, but they all end up
1394 * using the same ->copy_file_range() function pointer.
1395 */
1396 if (file_out->f_op->copy_file_range &&
1397 file_out->f_op->copy_file_range == file_in->f_op->copy_file_range)
Dave Chinner64bf5ff2019-06-05 08:04:47 -07001398 return file_out->f_op->copy_file_range(file_in, pos_in,
1399 file_out, pos_out,
1400 len, flags);
1401
1402 return generic_copy_file_range(file_in, pos_in, file_out, pos_out, len,
1403 flags);
1404}
1405
Zach Brown29732932015-11-10 16:53:30 -05001406/*
Darrick J. Wong407e9c62020-10-15 09:21:17 -07001407 * Performs necessary checks before doing a file copy
1408 *
1409 * Can adjust amount of bytes to copy via @req_count argument.
1410 * Returns appropriate error code that caller should return or
1411 * zero in case the copy should be allowed.
1412 */
1413static int generic_copy_file_checks(struct file *file_in, loff_t pos_in,
1414 struct file *file_out, loff_t pos_out,
1415 size_t *req_count, unsigned int flags)
1416{
1417 struct inode *inode_in = file_inode(file_in);
1418 struct inode *inode_out = file_inode(file_out);
1419 uint64_t count = *req_count;
1420 loff_t size_in;
1421 int ret;
1422
1423 ret = generic_file_rw_checks(file_in, file_out);
1424 if (ret)
1425 return ret;
1426
1427 /* Don't touch certain kinds of inodes */
1428 if (IS_IMMUTABLE(inode_out))
1429 return -EPERM;
1430
1431 if (IS_SWAPFILE(inode_in) || IS_SWAPFILE(inode_out))
1432 return -ETXTBSY;
1433
1434 /* Ensure offsets don't wrap. */
1435 if (pos_in + count < pos_in || pos_out + count < pos_out)
1436 return -EOVERFLOW;
1437
1438 /* Shorten the copy to EOF */
1439 size_in = i_size_read(inode_in);
1440 if (pos_in >= size_in)
1441 count = 0;
1442 else
1443 count = min(count, size_in - (uint64_t)pos_in);
1444
1445 ret = generic_write_check_limits(file_out, pos_out, &count);
1446 if (ret)
1447 return ret;
1448
1449 /* Don't allow overlapped copying within the same file. */
1450 if (inode_in == inode_out &&
1451 pos_out + count > pos_in &&
1452 pos_out < pos_in + count)
1453 return -EINVAL;
1454
1455 *req_count = count;
1456 return 0;
1457}
1458
1459/*
Zach Brown29732932015-11-10 16:53:30 -05001460 * copy_file_range() differs from regular file read and write in that it
1461 * specifically allows return partial success. When it does so is up to
1462 * the copy_file_range method.
1463 */
1464ssize_t vfs_copy_file_range(struct file *file_in, loff_t pos_in,
1465 struct file *file_out, loff_t pos_out,
1466 size_t len, unsigned int flags)
1467{
Zach Brown29732932015-11-10 16:53:30 -05001468 ssize_t ret;
1469
1470 if (flags != 0)
1471 return -EINVAL;
1472
Amir Goldstein96e6e8f2019-06-05 08:04:49 -07001473 ret = generic_copy_file_checks(file_in, pos_in, file_out, pos_out, &len,
1474 flags);
Amir Goldsteina3171352019-06-05 08:04:48 -07001475 if (unlikely(ret))
1476 return ret;
Amir Goldstein11cbfb12017-01-31 10:34:56 +02001477
Zach Brown29732932015-11-10 16:53:30 -05001478 ret = rw_verify_area(READ, file_in, &pos_in, len);
Al Virobc613842016-03-31 21:48:20 -04001479 if (unlikely(ret))
1480 return ret;
1481
1482 ret = rw_verify_area(WRITE, file_out, &pos_out, len);
1483 if (unlikely(ret))
Zach Brown29732932015-11-10 16:53:30 -05001484 return ret;
1485
Zach Brown29732932015-11-10 16:53:30 -05001486 if (len == 0)
1487 return 0;
1488
Amir Goldsteinbfe219d2017-01-31 10:34:57 +02001489 file_start_write(file_out);
Zach Brown29732932015-11-10 16:53:30 -05001490
Christoph Hellwiga76b5b02016-12-09 16:17:19 -08001491 /*
1492 * Try cloning first, this is supported by more file systems, and
1493 * more efficient if both clone and copy are supported (e.g. NFS).
1494 */
Amir Goldstein5dae2222019-06-05 08:04:50 -07001495 if (file_in->f_op->remap_file_range &&
1496 file_inode(file_in)->i_sb == file_inode(file_out)->i_sb) {
Darrick J. Wong42ec3d42018-10-30 10:41:49 +11001497 loff_t cloned;
1498
1499 cloned = file_in->f_op->remap_file_range(file_in, pos_in,
1500 file_out, pos_out,
Darrick J. Wongeca36542018-10-30 10:42:10 +11001501 min_t(loff_t, MAX_RW_COUNT, len),
1502 REMAP_FILE_CAN_SHORTEN);
Darrick J. Wong42ec3d42018-10-30 10:41:49 +11001503 if (cloned > 0) {
1504 ret = cloned;
Christoph Hellwiga76b5b02016-12-09 16:17:19 -08001505 goto done;
1506 }
1507 }
1508
Dave Chinner64bf5ff2019-06-05 08:04:47 -07001509 ret = do_copy_file_range(file_in, pos_in, file_out, pos_out, len,
1510 flags);
1511 WARN_ON_ONCE(ret == -EOPNOTSUPP);
Christoph Hellwiga76b5b02016-12-09 16:17:19 -08001512done:
Zach Brown29732932015-11-10 16:53:30 -05001513 if (ret > 0) {
1514 fsnotify_access(file_in);
1515 add_rchar(current, ret);
1516 fsnotify_modify(file_out);
1517 add_wchar(current, ret);
1518 }
Christoph Hellwiga76b5b02016-12-09 16:17:19 -08001519
Zach Brown29732932015-11-10 16:53:30 -05001520 inc_syscr(current);
1521 inc_syscw(current);
1522
Amir Goldsteinbfe219d2017-01-31 10:34:57 +02001523 file_end_write(file_out);
Zach Brown29732932015-11-10 16:53:30 -05001524
1525 return ret;
1526}
1527EXPORT_SYMBOL(vfs_copy_file_range);
1528
1529SYSCALL_DEFINE6(copy_file_range, int, fd_in, loff_t __user *, off_in,
1530 int, fd_out, loff_t __user *, off_out,
1531 size_t, len, unsigned int, flags)
1532{
1533 loff_t pos_in;
1534 loff_t pos_out;
1535 struct fd f_in;
1536 struct fd f_out;
1537 ssize_t ret = -EBADF;
1538
1539 f_in = fdget(fd_in);
1540 if (!f_in.file)
1541 goto out2;
1542
1543 f_out = fdget(fd_out);
1544 if (!f_out.file)
1545 goto out1;
1546
1547 ret = -EFAULT;
1548 if (off_in) {
1549 if (copy_from_user(&pos_in, off_in, sizeof(loff_t)))
1550 goto out;
1551 } else {
1552 pos_in = f_in.file->f_pos;
1553 }
1554
1555 if (off_out) {
1556 if (copy_from_user(&pos_out, off_out, sizeof(loff_t)))
1557 goto out;
1558 } else {
1559 pos_out = f_out.file->f_pos;
1560 }
1561
1562 ret = vfs_copy_file_range(f_in.file, pos_in, f_out.file, pos_out, len,
1563 flags);
1564 if (ret > 0) {
1565 pos_in += ret;
1566 pos_out += ret;
1567
1568 if (off_in) {
1569 if (copy_to_user(off_in, &pos_in, sizeof(loff_t)))
1570 ret = -EFAULT;
1571 } else {
1572 f_in.file->f_pos = pos_in;
1573 }
1574
1575 if (off_out) {
1576 if (copy_to_user(off_out, &pos_out, sizeof(loff_t)))
1577 ret = -EFAULT;
1578 } else {
1579 f_out.file->f_pos = pos_out;
1580 }
1581 }
1582
1583out:
1584 fdput(f_out);
1585out1:
1586 fdput(f_in);
1587out2:
1588 return ret;
1589}
Christoph Hellwig04b38d62015-12-03 12:59:50 +01001590
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001591/*
Darrick J. Wong407e9c62020-10-15 09:21:17 -07001592 * Don't operate on ranges the page cache doesn't support, and don't exceed the
1593 * LFS limits. If pos is under the limit it becomes a short access. If it
1594 * exceeds the limit we return -EFBIG.
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001595 */
Darrick J. Wong407e9c62020-10-15 09:21:17 -07001596int generic_write_check_limits(struct file *file, loff_t pos, loff_t *count)
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001597{
Darrick J. Wong407e9c62020-10-15 09:21:17 -07001598 struct inode *inode = file->f_mapping->host;
1599 loff_t max_size = inode->i_sb->s_maxbytes;
1600 loff_t limit = rlimit(RLIMIT_FSIZE);
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001601
Darrick J. Wong407e9c62020-10-15 09:21:17 -07001602 if (limit != RLIM_INFINITY) {
1603 if (pos >= limit) {
1604 send_sig(SIGXFSZ, current, 0);
1605 return -EFBIG;
1606 }
1607 *count = min(*count, limit - pos);
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001608 }
1609
Darrick J. Wong407e9c62020-10-15 09:21:17 -07001610 if (!(file->f_flags & O_LARGEFILE))
1611 max_size = MAX_NON_LFS;
Darrick J. Wong54dbc152015-12-19 00:55:59 -08001612
Darrick J. Wong407e9c62020-10-15 09:21:17 -07001613 if (unlikely(pos >= max_size))
1614 return -EFBIG;
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001615
Darrick J. Wong407e9c62020-10-15 09:21:17 -07001616 *count = min(*count, max_size - pos);
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001617
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001618 return 0;
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001619}
Darrick J. Wongc32e5f32018-10-30 10:42:17 +11001620
Omar Sandovalf6f7a252021-08-12 15:34:57 -07001621/* Like generic_write_checks(), but takes size of write instead of iter. */
1622int generic_write_checks_count(struct kiocb *iocb, loff_t *count)
Darrick J. Wong407e9c62020-10-15 09:21:17 -07001623{
1624 struct file *file = iocb->ki_filp;
1625 struct inode *inode = file->f_mapping->host;
Darrick J. Wong407e9c62020-10-15 09:21:17 -07001626
1627 if (IS_SWAPFILE(inode))
1628 return -ETXTBSY;
1629
Omar Sandovalf6f7a252021-08-12 15:34:57 -07001630 if (!*count)
Darrick J. Wong407e9c62020-10-15 09:21:17 -07001631 return 0;
1632
Darrick J. Wong407e9c62020-10-15 09:21:17 -07001633 if (iocb->ki_flags & IOCB_APPEND)
1634 iocb->ki_pos = i_size_read(inode);
1635
1636 if ((iocb->ki_flags & IOCB_NOWAIT) && !(iocb->ki_flags & IOCB_DIRECT))
1637 return -EINVAL;
1638
Omar Sandovalf6f7a252021-08-12 15:34:57 -07001639 return generic_write_check_limits(iocb->ki_filp, iocb->ki_pos, count);
1640}
1641EXPORT_SYMBOL(generic_write_checks_count);
1642
1643/*
1644 * Performs necessary checks before doing a write
1645 *
1646 * Can adjust writing position or amount of bytes to write.
1647 * Returns appropriate error code that caller should return or
1648 * zero in case that write should be allowed.
1649 */
1650ssize_t generic_write_checks(struct kiocb *iocb, struct iov_iter *from)
1651{
1652 loff_t count = iov_iter_count(from);
1653 int ret;
1654
1655 ret = generic_write_checks_count(iocb, &count);
Darrick J. Wong407e9c62020-10-15 09:21:17 -07001656 if (ret)
1657 return ret;
1658
1659 iov_iter_truncate(from, count);
1660 return iov_iter_count(from);
1661}
1662EXPORT_SYMBOL(generic_write_checks);
1663
1664/*
1665 * Performs common checks before doing a file copy/clone
1666 * from @file_in to @file_out.
1667 */
1668int generic_file_rw_checks(struct file *file_in, struct file *file_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669{
Darrick J. Wong1383a7e2018-10-30 10:40:31 +11001670 struct inode *inode_in = file_inode(file_in);
1671 struct inode *inode_out = file_inode(file_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
Darrick J. Wong407e9c62020-10-15 09:21:17 -07001673 /* Don't copy dirs, pipes, sockets... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 if (S_ISDIR(inode_in->i_mode) || S_ISDIR(inode_out->i_mode))
1675 return -EISDIR;
1676 if (!S_ISREG(inode_in->i_mode) || !S_ISREG(inode_out->i_mode))
1677 return -EINVAL;
1678
Darrick J. Wong407e9c62020-10-15 09:21:17 -07001679 if (!(file_in->f_mode & FMODE_READ) ||
1680 !(file_out->f_mode & FMODE_WRITE) ||
1681 (file_out->f_flags & O_APPEND))
1682 return -EBADF;
Darrick J. Wong1383a7e2018-10-30 10:40:31 +11001683
Darrick J. Wong407e9c62020-10-15 09:21:17 -07001684 return 0;
Christoph Hellwig04b38d62015-12-03 12:59:50 +01001685}