blob: e0777eefd84650debb0bed304401f81d8ccb1c8e [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
Guo Ren59c10c52022-04-05 15:13:05 +0800685#if defined(CONFIG_COMPAT) && defined(__ARCH_WANT_COMPAT_PREAD64)
686COMPAT_SYSCALL_DEFINE5(pread64, unsigned int, fd, char __user *, buf,
687 size_t, count, compat_arg_u64_dual(pos))
688{
689 return ksys_pread64(fd, buf, count, compat_arg_u64_glue(pos));
690}
691#endif
692
Dominik Brodowski36028d52018-03-19 17:38:31 +0100693ssize_t ksys_pwrite64(unsigned int fd, const char __user *buf,
694 size_t count, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
Al Viro2903ff02012-08-28 12:52:22 -0400696 struct fd f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
699 if (pos < 0)
700 return -EINVAL;
701
Al Viro2903ff02012-08-28 12:52:22 -0400702 f = fdget(fd);
703 if (f.file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -0400705 if (f.file->f_mode & FMODE_PWRITE)
706 ret = vfs_write(f.file, buf, count, &pos);
707 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
709
710 return ret;
711}
712
Dominik Brodowski36028d52018-03-19 17:38:31 +0100713SYSCALL_DEFINE4(pwrite64, unsigned int, fd, const char __user *, buf,
714 size_t, count, loff_t, pos)
715{
716 return ksys_pwrite64(fd, buf, count, pos);
717}
718
Guo Ren59c10c52022-04-05 15:13:05 +0800719#if defined(CONFIG_COMPAT) && defined(__ARCH_WANT_COMPAT_PWRITE64)
720COMPAT_SYSCALL_DEFINE5(pwrite64, unsigned int, fd, const char __user *, buf,
721 size_t, count, compat_arg_u64_dual(pos))
722{
723 return ksys_pwrite64(fd, buf, count, compat_arg_u64_glue(pos));
724}
725#endif
726
Al Viroac15ac02015-03-20 20:10:21 -0400727static ssize_t do_iter_readv_writev(struct file *filp, struct iov_iter *iter,
Christoph Hellwigddef7ed22017-07-06 18:58:37 +0200728 loff_t *ppos, int type, rwf_t flags)
Al Viro293bc982014-02-11 18:37:41 -0500729{
730 struct kiocb kiocb;
Al Viro293bc982014-02-11 18:37:41 -0500731 ssize_t ret;
732
733 init_sync_kiocb(&kiocb, filp);
Goldwyn Rodriguesfdd2f5b2017-06-20 07:05:40 -0500734 ret = kiocb_set_rw_flags(&kiocb, flags);
735 if (ret)
736 return ret;
Kirill Smelkov438ab722019-04-12 12:31:57 +0300737 kiocb.ki_pos = (ppos ? *ppos : 0);
Al Viro293bc982014-02-11 18:37:41 -0500738
Miklos Szeredi0f78d062017-02-20 16:51:23 +0100739 if (type == READ)
Miklos Szeredibb7462b2017-02-20 16:51:23 +0100740 ret = call_read_iter(filp, &kiocb, iter);
Miklos Szeredi0f78d062017-02-20 16:51:23 +0100741 else
Miklos Szeredibb7462b2017-02-20 16:51:23 +0100742 ret = call_write_iter(filp, &kiocb, iter);
Christoph Hellwig599bd192015-02-11 19:59:44 +0100743 BUG_ON(ret == -EIOCBQUEUED);
Kirill Smelkov438ab722019-04-12 12:31:57 +0300744 if (ppos)
745 *ppos = kiocb.ki_pos;
Al Viro293bc982014-02-11 18:37:41 -0500746 return ret;
747}
748
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700749/* Do it by hand, with file-ops */
Al Viroac15ac02015-03-20 20:10:21 -0400750static ssize_t do_loop_readv_writev(struct file *filp, struct iov_iter *iter,
Christoph Hellwigddef7ed22017-07-06 18:58:37 +0200751 loff_t *ppos, int type, rwf_t flags)
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700752{
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700753 ssize_t ret = 0;
754
Christoph Hellwig97be7eb2016-03-03 16:04:01 +0100755 if (flags & ~RWF_HIPRI)
Christoph Hellwig793b80e2016-03-03 16:03:58 +0100756 return -EOPNOTSUPP;
757
Al Viroac15ac02015-03-20 20:10:21 -0400758 while (iov_iter_count(iter)) {
759 struct iovec iovec = iov_iter_iovec(iter);
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700760 ssize_t nr;
761
Miklos Szeredi0f78d062017-02-20 16:51:23 +0100762 if (type == READ) {
763 nr = filp->f_op->read(filp, iovec.iov_base,
764 iovec.iov_len, ppos);
765 } else {
766 nr = filp->f_op->write(filp, iovec.iov_base,
767 iovec.iov_len, ppos);
768 }
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700769
770 if (nr < 0) {
771 if (!ret)
772 ret = nr;
773 break;
774 }
775 ret += nr;
Al Viroac15ac02015-03-20 20:10:21 -0400776 if (nr != iovec.iov_len)
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700777 break;
Al Viroac15ac02015-03-20 20:10:21 -0400778 iov_iter_advance(iter, nr);
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700779 }
780
781 return ret;
782}
783
Christoph Hellwig19c73582017-05-27 11:16:48 +0300784static ssize_t do_iter_read(struct file *file, struct iov_iter *iter,
Christoph Hellwigddef7ed22017-07-06 18:58:37 +0200785 loff_t *pos, rwf_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 size_t tot_len;
Miklos Szeredi7687a7a42017-02-20 16:51:23 +0100788 ssize_t ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Christoph Hellwigedab5fe2017-05-27 11:16:49 +0300790 if (!(file->f_mode & FMODE_READ))
791 return -EBADF;
792 if (!(file->f_mode & FMODE_CAN_READ))
793 return -EINVAL;
794
Miklos Szeredi7687a7a42017-02-20 16:51:23 +0100795 tot_len = iov_iter_count(iter);
Al Viro0504c072015-03-21 19:40:11 -0400796 if (!tot_len)
797 goto out;
Christoph Hellwig19c73582017-05-27 11:16:48 +0300798 ret = rw_verify_area(READ, file, pos, tot_len);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800799 if (ret < 0)
Christoph Hellwig19c73582017-05-27 11:16:48 +0300800 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Christoph Hellwig19c73582017-05-27 11:16:48 +0300802 if (file->f_op->read_iter)
803 ret = do_iter_readv_writev(file, iter, pos, READ, flags);
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700804 else
Christoph Hellwig19c73582017-05-27 11:16:48 +0300805 ret = do_loop_readv_writev(file, iter, pos, READ, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806out:
Christoph Hellwig19c73582017-05-27 11:16:48 +0300807 if (ret >= 0)
808 fsnotify_access(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810}
811
Jiufei Xue5dcdc432019-11-20 17:45:25 +0800812ssize_t vfs_iocb_iter_read(struct file *file, struct kiocb *iocb,
813 struct iov_iter *iter)
814{
815 size_t tot_len;
816 ssize_t ret = 0;
817
818 if (!file->f_op->read_iter)
819 return -EINVAL;
820 if (!(file->f_mode & FMODE_READ))
821 return -EBADF;
822 if (!(file->f_mode & FMODE_CAN_READ))
823 return -EINVAL;
824
825 tot_len = iov_iter_count(iter);
826 if (!tot_len)
827 goto out;
828 ret = rw_verify_area(READ, file, &iocb->ki_pos, tot_len);
829 if (ret < 0)
830 return ret;
831
832 ret = call_read_iter(file, iocb, iter);
833out:
834 if (ret >= 0)
835 fsnotify_access(file);
836 return ret;
837}
838EXPORT_SYMBOL(vfs_iocb_iter_read);
839
Christoph Hellwig18e97102017-05-27 11:16:51 +0300840ssize_t vfs_iter_read(struct file *file, struct iov_iter *iter, loff_t *ppos,
Christoph Hellwigddef7ed22017-07-06 18:58:37 +0200841 rwf_t flags)
Christoph Hellwig18e97102017-05-27 11:16:51 +0300842{
843 if (!file->f_op->read_iter)
844 return -EINVAL;
845 return do_iter_read(file, iter, ppos, flags);
846}
847EXPORT_SYMBOL(vfs_iter_read);
848
Christoph Hellwig19c73582017-05-27 11:16:48 +0300849static ssize_t do_iter_write(struct file *file, struct iov_iter *iter,
Christoph Hellwigddef7ed22017-07-06 18:58:37 +0200850 loff_t *pos, rwf_t flags)
Christoph Hellwig19c73582017-05-27 11:16:48 +0300851{
852 size_t tot_len;
853 ssize_t ret = 0;
854
Christoph Hellwigedab5fe2017-05-27 11:16:49 +0300855 if (!(file->f_mode & FMODE_WRITE))
856 return -EBADF;
857 if (!(file->f_mode & FMODE_CAN_WRITE))
858 return -EINVAL;
859
Christoph Hellwig19c73582017-05-27 11:16:48 +0300860 tot_len = iov_iter_count(iter);
861 if (!tot_len)
862 return 0;
863 ret = rw_verify_area(WRITE, file, pos, tot_len);
864 if (ret < 0)
865 return ret;
866
Christoph Hellwig19c73582017-05-27 11:16:48 +0300867 if (file->f_op->write_iter)
868 ret = do_iter_readv_writev(file, iter, pos, WRITE, flags);
869 else
870 ret = do_loop_readv_writev(file, iter, pos, WRITE, flags);
Christoph Hellwig19c73582017-05-27 11:16:48 +0300871 if (ret > 0)
872 fsnotify_modify(file);
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700873 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874}
875
Jiufei Xue5dcdc432019-11-20 17:45:25 +0800876ssize_t vfs_iocb_iter_write(struct file *file, struct kiocb *iocb,
877 struct iov_iter *iter)
878{
879 size_t tot_len;
880 ssize_t ret = 0;
881
882 if (!file->f_op->write_iter)
883 return -EINVAL;
884 if (!(file->f_mode & FMODE_WRITE))
885 return -EBADF;
886 if (!(file->f_mode & FMODE_CAN_WRITE))
887 return -EINVAL;
888
889 tot_len = iov_iter_count(iter);
890 if (!tot_len)
891 return 0;
892 ret = rw_verify_area(WRITE, file, &iocb->ki_pos, tot_len);
893 if (ret < 0)
894 return ret;
895
896 ret = call_write_iter(file, iocb, iter);
897 if (ret > 0)
898 fsnotify_modify(file);
899
900 return ret;
901}
902EXPORT_SYMBOL(vfs_iocb_iter_write);
903
Christoph Hellwigabbb65892017-05-27 11:16:52 +0300904ssize_t vfs_iter_write(struct file *file, struct iov_iter *iter, loff_t *ppos,
Christoph Hellwigddef7ed22017-07-06 18:58:37 +0200905 rwf_t flags)
Christoph Hellwigabbb65892017-05-27 11:16:52 +0300906{
907 if (!file->f_op->write_iter)
908 return -EINVAL;
909 return do_iter_write(file, iter, ppos, flags);
910}
911EXPORT_SYMBOL(vfs_iter_write);
912
Christoph Hellwig36e2c742020-09-03 16:22:34 +0200913static ssize_t vfs_readv(struct file *file, const struct iovec __user *vec,
Christoph Hellwigddef7ed22017-07-06 18:58:37 +0200914 unsigned long vlen, loff_t *pos, rwf_t flags)
Miklos Szeredi7687a7a42017-02-20 16:51:23 +0100915{
916 struct iovec iovstack[UIO_FASTIOV];
917 struct iovec *iov = iovstack;
918 struct iov_iter iter;
919 ssize_t ret;
920
Christoph Hellwig251b42a2017-05-27 11:16:46 +0300921 ret = import_iovec(READ, vec, vlen, ARRAY_SIZE(iovstack), &iov, &iter);
Christoph Hellwigedab5fe2017-05-27 11:16:49 +0300922 if (ret >= 0) {
923 ret = do_iter_read(file, &iter, pos, flags);
924 kfree(iov);
925 }
Miklos Szeredi7687a7a42017-02-20 16:51:23 +0100926
927 return ret;
928}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Christoph Hellwig9725d4c2017-09-01 17:39:25 +0200930static ssize_t vfs_writev(struct file *file, const struct iovec __user *vec,
Christoph Hellwigddef7ed22017-07-06 18:58:37 +0200931 unsigned long vlen, loff_t *pos, rwf_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932{
Christoph Hellwig251b42a2017-05-27 11:16:46 +0300933 struct iovec iovstack[UIO_FASTIOV];
934 struct iovec *iov = iovstack;
935 struct iov_iter iter;
936 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Christoph Hellwig251b42a2017-05-27 11:16:46 +0300938 ret = import_iovec(WRITE, vec, vlen, ARRAY_SIZE(iovstack), &iov, &iter);
Christoph Hellwigedab5fe2017-05-27 11:16:49 +0300939 if (ret >= 0) {
Al Viro62473a22017-07-06 09:15:47 -0400940 file_start_write(file);
Christoph Hellwigedab5fe2017-05-27 11:16:49 +0300941 ret = do_iter_write(file, &iter, pos, flags);
Al Viro62473a22017-07-06 09:15:47 -0400942 file_end_write(file);
Christoph Hellwigedab5fe2017-05-27 11:16:49 +0300943 kfree(iov);
944 }
Christoph Hellwig251b42a2017-05-27 11:16:46 +0300945 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Milosz Tanskif17d8b32016-03-03 16:03:59 +0100948static ssize_t do_readv(unsigned long fd, const struct iovec __user *vec,
Christoph Hellwigddef7ed22017-07-06 18:58:37 +0200949 unsigned long vlen, rwf_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950{
Linus Torvalds9c225f22014-03-03 09:36:58 -0800951 struct fd f = fdget_pos(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Al Viro2903ff02012-08-28 12:52:22 -0400954 if (f.file) {
Kirill Smelkov438ab722019-04-12 12:31:57 +0300955 loff_t pos, *ppos = file_ppos(f.file);
956 if (ppos) {
957 pos = *ppos;
958 ppos = &pos;
959 }
960 ret = vfs_readv(f.file, vec, vlen, ppos, flags);
961 if (ret >= 0 && ppos)
962 f.file->f_pos = pos;
Linus Torvalds9c225f22014-03-03 09:36:58 -0800963 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 }
965
966 if (ret > 0)
Alexey Dobriyan4b98d112007-02-10 01:46:45 -0800967 add_rchar(current, ret);
968 inc_syscr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 return ret;
970}
971
Milosz Tanskif17d8b32016-03-03 16:03:59 +0100972static ssize_t do_writev(unsigned long fd, const struct iovec __user *vec,
Christoph Hellwigddef7ed22017-07-06 18:58:37 +0200973 unsigned long vlen, rwf_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974{
Linus Torvalds9c225f22014-03-03 09:36:58 -0800975 struct fd f = fdget_pos(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Al Viro2903ff02012-08-28 12:52:22 -0400978 if (f.file) {
Kirill Smelkov438ab722019-04-12 12:31:57 +0300979 loff_t pos, *ppos = file_ppos(f.file);
980 if (ppos) {
981 pos = *ppos;
982 ppos = &pos;
983 }
984 ret = vfs_writev(f.file, vec, vlen, ppos, flags);
985 if (ret >= 0 && ppos)
986 f.file->f_pos = pos;
Linus Torvalds9c225f22014-03-03 09:36:58 -0800987 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 }
989
990 if (ret > 0)
Alexey Dobriyan4b98d112007-02-10 01:46:45 -0800991 add_wchar(current, ret);
992 inc_syscw(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 return ret;
994}
995
Linus Torvalds601cc11d2009-04-03 08:03:22 -0700996static inline loff_t pos_from_hilo(unsigned long high, unsigned long low)
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700997{
Linus Torvalds601cc11d2009-04-03 08:03:22 -0700998#define HALF_LONG_BITS (BITS_PER_LONG / 2)
999 return (((loff_t)high << HALF_LONG_BITS) << HALF_LONG_BITS) | low;
1000}
1001
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001002static ssize_t do_preadv(unsigned long fd, const struct iovec __user *vec,
Christoph Hellwigddef7ed22017-07-06 18:58:37 +02001003 unsigned long vlen, loff_t pos, rwf_t flags)
Linus Torvalds601cc11d2009-04-03 08:03:22 -07001004{
Al Viro2903ff02012-08-28 12:52:22 -04001005 struct fd f;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -07001006 ssize_t ret = -EBADF;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -07001007
1008 if (pos < 0)
1009 return -EINVAL;
1010
Al Viro2903ff02012-08-28 12:52:22 -04001011 f = fdget(fd);
1012 if (f.file) {
Gerd Hoffmannf3554f42009-04-02 16:59:23 -07001013 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -04001014 if (f.file->f_mode & FMODE_PREAD)
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001015 ret = vfs_readv(f.file, vec, vlen, &pos, flags);
Al Viro2903ff02012-08-28 12:52:22 -04001016 fdput(f);
Gerd Hoffmannf3554f42009-04-02 16:59:23 -07001017 }
1018
1019 if (ret > 0)
1020 add_rchar(current, ret);
1021 inc_syscr(current);
1022 return ret;
1023}
1024
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001025static ssize_t do_pwritev(unsigned long fd, const struct iovec __user *vec,
Christoph Hellwigddef7ed22017-07-06 18:58:37 +02001026 unsigned long vlen, loff_t pos, rwf_t flags)
Gerd Hoffmannf3554f42009-04-02 16:59:23 -07001027{
Al Viro2903ff02012-08-28 12:52:22 -04001028 struct fd f;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -07001029 ssize_t ret = -EBADF;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -07001030
1031 if (pos < 0)
1032 return -EINVAL;
1033
Al Viro2903ff02012-08-28 12:52:22 -04001034 f = fdget(fd);
1035 if (f.file) {
Gerd Hoffmannf3554f42009-04-02 16:59:23 -07001036 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -04001037 if (f.file->f_mode & FMODE_PWRITE)
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001038 ret = vfs_writev(f.file, vec, vlen, &pos, flags);
Al Viro2903ff02012-08-28 12:52:22 -04001039 fdput(f);
Gerd Hoffmannf3554f42009-04-02 16:59:23 -07001040 }
1041
1042 if (ret > 0)
1043 add_wchar(current, ret);
1044 inc_syscw(current);
1045 return ret;
1046}
1047
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001048SYSCALL_DEFINE3(readv, unsigned long, fd, const struct iovec __user *, vec,
1049 unsigned long, vlen)
1050{
1051 return do_readv(fd, vec, vlen, 0);
1052}
1053
1054SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec __user *, vec,
1055 unsigned long, vlen)
1056{
1057 return do_writev(fd, vec, vlen, 0);
1058}
1059
1060SYSCALL_DEFINE5(preadv, unsigned long, fd, const struct iovec __user *, vec,
1061 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
1062{
1063 loff_t pos = pos_from_hilo(pos_h, pos_l);
1064
1065 return do_preadv(fd, vec, vlen, pos, 0);
1066}
1067
1068SYSCALL_DEFINE6(preadv2, unsigned long, fd, const struct iovec __user *, vec,
1069 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h,
Christoph Hellwigddef7ed22017-07-06 18:58:37 +02001070 rwf_t, flags)
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001071{
1072 loff_t pos = pos_from_hilo(pos_h, pos_l);
1073
1074 if (pos == -1)
1075 return do_readv(fd, vec, vlen, flags);
1076
1077 return do_preadv(fd, vec, vlen, pos, flags);
1078}
1079
1080SYSCALL_DEFINE5(pwritev, unsigned long, fd, const struct iovec __user *, vec,
1081 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
1082{
1083 loff_t pos = pos_from_hilo(pos_h, pos_l);
1084
1085 return do_pwritev(fd, vec, vlen, pos, 0);
1086}
1087
1088SYSCALL_DEFINE6(pwritev2, unsigned long, fd, const struct iovec __user *, vec,
1089 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h,
Christoph Hellwigddef7ed22017-07-06 18:58:37 +02001090 rwf_t, flags)
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001091{
1092 loff_t pos = pos_from_hilo(pos_h, pos_l);
1093
1094 if (pos == -1)
1095 return do_writev(fd, vec, vlen, flags);
1096
1097 return do_pwritev(fd, vec, vlen, pos, flags);
1098}
1099
Christoph Hellwig3523a9d2020-09-25 06:51:42 +02001100/*
1101 * Various compat syscalls. Note that they all pretend to take a native
1102 * iovec - import_iovec will properly treat those as compat_iovecs based on
1103 * in_compat_syscall().
1104 */
Al Viro72ec3512013-03-20 10:42:10 -04001105#ifdef CONFIG_COMPAT
Heiko Carstens378a10f2014-03-05 10:43:51 +01001106#ifdef __ARCH_WANT_COMPAT_SYS_PREADV64
1107COMPAT_SYSCALL_DEFINE4(preadv64, unsigned long, fd,
Christoph Hellwig3523a9d2020-09-25 06:51:42 +02001108 const struct iovec __user *, vec,
Heiko Carstens378a10f2014-03-05 10:43:51 +01001109 unsigned long, vlen, loff_t, pos)
1110{
Christoph Hellwig3523a9d2020-09-25 06:51:42 +02001111 return do_preadv(fd, vec, vlen, pos, 0);
Heiko Carstens378a10f2014-03-05 10:43:51 +01001112}
1113#endif
1114
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001115COMPAT_SYSCALL_DEFINE5(preadv, compat_ulong_t, fd,
Christoph Hellwig3523a9d2020-09-25 06:51:42 +02001116 const struct iovec __user *, vec,
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001117 compat_ulong_t, vlen, u32, pos_low, u32, pos_high)
Al Viro72ec3512013-03-20 10:42:10 -04001118{
1119 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
Heiko Carstens378a10f2014-03-05 10:43:51 +01001120
Christoph Hellwig3523a9d2020-09-25 06:51:42 +02001121 return do_preadv(fd, vec, vlen, pos, 0);
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001122}
1123
H.J. Lu3ebfd812016-07-14 12:31:53 -07001124#ifdef __ARCH_WANT_COMPAT_SYS_PREADV64V2
1125COMPAT_SYSCALL_DEFINE5(preadv64v2, unsigned long, fd,
Christoph Hellwig3523a9d2020-09-25 06:51:42 +02001126 const struct iovec __user *, vec,
Christoph Hellwigddef7ed22017-07-06 18:58:37 +02001127 unsigned long, vlen, loff_t, pos, rwf_t, flags)
H.J. Lu3ebfd812016-07-14 12:31:53 -07001128{
Aurelien Jarnocc4b1242018-12-06 20:05:34 +01001129 if (pos == -1)
Christoph Hellwig3523a9d2020-09-25 06:51:42 +02001130 return do_readv(fd, vec, vlen, flags);
1131 return do_preadv(fd, vec, vlen, pos, flags);
H.J. Lu3ebfd812016-07-14 12:31:53 -07001132}
1133#endif
1134
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001135COMPAT_SYSCALL_DEFINE6(preadv2, compat_ulong_t, fd,
Christoph Hellwig3523a9d2020-09-25 06:51:42 +02001136 const struct iovec __user *, vec,
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001137 compat_ulong_t, vlen, u32, pos_low, u32, pos_high,
Christoph Hellwigddef7ed22017-07-06 18:58:37 +02001138 rwf_t, flags)
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001139{
1140 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
1141
1142 if (pos == -1)
Christoph Hellwig3523a9d2020-09-25 06:51:42 +02001143 return do_readv(fd, vec, vlen, flags);
1144 return do_preadv(fd, vec, vlen, pos, flags);
Al Viro72ec3512013-03-20 10:42:10 -04001145}
1146
Heiko Carstens378a10f2014-03-05 10:43:51 +01001147#ifdef __ARCH_WANT_COMPAT_SYS_PWRITEV64
1148COMPAT_SYSCALL_DEFINE4(pwritev64, unsigned long, fd,
Christoph Hellwig3523a9d2020-09-25 06:51:42 +02001149 const struct iovec __user *, vec,
Heiko Carstens378a10f2014-03-05 10:43:51 +01001150 unsigned long, vlen, loff_t, pos)
1151{
Christoph Hellwig3523a9d2020-09-25 06:51:42 +02001152 return do_pwritev(fd, vec, vlen, pos, 0);
Heiko Carstens378a10f2014-03-05 10:43:51 +01001153}
1154#endif
1155
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001156COMPAT_SYSCALL_DEFINE5(pwritev, compat_ulong_t, fd,
Christoph Hellwig3523a9d2020-09-25 06:51:42 +02001157 const struct iovec __user *,vec,
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001158 compat_ulong_t, vlen, u32, pos_low, u32, pos_high)
Al Viro72ec3512013-03-20 10:42:10 -04001159{
1160 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
Heiko Carstens378a10f2014-03-05 10:43:51 +01001161
Christoph Hellwig3523a9d2020-09-25 06:51:42 +02001162 return do_pwritev(fd, vec, vlen, pos, 0);
Al Viro72ec3512013-03-20 10:42:10 -04001163}
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001164
H.J. Lu3ebfd812016-07-14 12:31:53 -07001165#ifdef __ARCH_WANT_COMPAT_SYS_PWRITEV64V2
1166COMPAT_SYSCALL_DEFINE5(pwritev64v2, unsigned long, fd,
Christoph Hellwig3523a9d2020-09-25 06:51:42 +02001167 const struct iovec __user *, vec,
Christoph Hellwigddef7ed22017-07-06 18:58:37 +02001168 unsigned long, vlen, loff_t, pos, rwf_t, flags)
H.J. Lu3ebfd812016-07-14 12:31:53 -07001169{
Aurelien Jarnocc4b1242018-12-06 20:05:34 +01001170 if (pos == -1)
Christoph Hellwig3523a9d2020-09-25 06:51:42 +02001171 return do_writev(fd, vec, vlen, flags);
1172 return do_pwritev(fd, vec, vlen, pos, flags);
H.J. Lu3ebfd812016-07-14 12:31:53 -07001173}
1174#endif
1175
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001176COMPAT_SYSCALL_DEFINE6(pwritev2, compat_ulong_t, fd,
Christoph Hellwig3523a9d2020-09-25 06:51:42 +02001177 const struct iovec __user *,vec,
Christoph Hellwigddef7ed22017-07-06 18:58:37 +02001178 compat_ulong_t, vlen, u32, pos_low, u32, pos_high, rwf_t, flags)
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001179{
1180 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
1181
1182 if (pos == -1)
Christoph Hellwig3523a9d2020-09-25 06:51:42 +02001183 return do_writev(fd, vec, vlen, flags);
1184 return do_pwritev(fd, vec, vlen, pos, flags);
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001185}
Christoph Hellwig3523a9d2020-09-25 06:51:42 +02001186#endif /* CONFIG_COMPAT */
Al Viro72ec3512013-03-20 10:42:10 -04001187
Al Viro19f4fc32013-02-24 02:17:03 -05001188static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,
1189 size_t count, loff_t max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190{
Al Viro2903ff02012-08-28 12:52:22 -04001191 struct fd in, out;
1192 struct inode *in_inode, *out_inode;
Al Virob964bf52021-01-25 22:24:28 -05001193 struct pipe_inode_info *opipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 loff_t pos;
Al Viro7995bd22013-06-20 18:58:36 +04001195 loff_t out_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 ssize_t retval;
Al Viro2903ff02012-08-28 12:52:22 -04001197 int fl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
1199 /*
1200 * Get input file, and verify that it is ok..
1201 */
1202 retval = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -04001203 in = fdget(in_fd);
1204 if (!in.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 goto out;
Al Viro2903ff02012-08-28 12:52:22 -04001206 if (!(in.file->f_mode & FMODE_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 goto fput_in;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 retval = -ESPIPE;
Al Viro7995bd22013-06-20 18:58:36 +04001209 if (!ppos) {
1210 pos = in.file->f_pos;
1211 } else {
1212 pos = *ppos;
Al Viro2903ff02012-08-28 12:52:22 -04001213 if (!(in.file->f_mode & FMODE_PREAD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 goto fput_in;
Al Viro7995bd22013-06-20 18:58:36 +04001215 }
1216 retval = rw_verify_area(READ, in.file, &pos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -08001217 if (retval < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 goto fput_in;
Al Virobc613842016-03-31 21:48:20 -04001219 if (count > MAX_RW_COUNT)
1220 count = MAX_RW_COUNT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 /*
1223 * Get output file, and verify that it is ok..
1224 */
1225 retval = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -04001226 out = fdget(out_fd);
1227 if (!out.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 goto fput_in;
Al Viro2903ff02012-08-28 12:52:22 -04001229 if (!(out.file->f_mode & FMODE_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 goto fput_out;
Al Viro496ad9a2013-01-23 17:07:38 -05001231 in_inode = file_inode(in.file);
1232 out_inode = file_inode(out.file);
Al Viro7995bd22013-06-20 18:58:36 +04001233 out_pos = out.file->f_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 if (!max)
1236 max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes);
1237
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 if (unlikely(pos + count > max)) {
1239 retval = -EOVERFLOW;
1240 if (pos >= max)
1241 goto fput_out;
1242 count = max - pos;
1243 }
1244
Jens Axboed96e6e72007-06-11 12:18:52 +02001245 fl = 0;
Jens Axboe534f2aa2007-06-01 14:52:37 +02001246#if 0
Jens Axboed96e6e72007-06-11 12:18:52 +02001247 /*
1248 * We need to debate whether we can enable this or not. The
1249 * man page documents EAGAIN return for the output at least,
1250 * and the application is arguably buggy if it doesn't expect
1251 * EAGAIN on a non-blocking file descriptor.
1252 */
Al Viro2903ff02012-08-28 12:52:22 -04001253 if (in.file->f_flags & O_NONBLOCK)
Jens Axboed96e6e72007-06-11 12:18:52 +02001254 fl = SPLICE_F_NONBLOCK;
Jens Axboe534f2aa2007-06-01 14:52:37 +02001255#endif
Al Virob964bf52021-01-25 22:24:28 -05001256 opipe = get_pipe_info(out.file, true);
1257 if (!opipe) {
1258 retval = rw_verify_area(WRITE, out.file, &out_pos, count);
1259 if (retval < 0)
1260 goto fput_out;
1261 file_start_write(out.file);
1262 retval = do_splice_direct(in.file, &pos, out.file, &out_pos,
1263 count, fl);
1264 file_end_write(out.file);
1265 } else {
1266 retval = splice_file_to_pipe(in.file, opipe, &pos, count, fl);
1267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
1269 if (retval > 0) {
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08001270 add_rchar(current, retval);
1271 add_wchar(current, retval);
Scott Wolchoka68c2f12012-12-20 15:05:52 -08001272 fsnotify_access(in.file);
1273 fsnotify_modify(out.file);
Al Viro7995bd22013-06-20 18:58:36 +04001274 out.file->f_pos = out_pos;
1275 if (ppos)
1276 *ppos = pos;
1277 else
1278 in.file->f_pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08001281 inc_syscr(current);
1282 inc_syscw(current);
Al Viro7995bd22013-06-20 18:58:36 +04001283 if (pos > max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 retval = -EOVERFLOW;
1285
1286fput_out:
Al Viro2903ff02012-08-28 12:52:22 -04001287 fdput(out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288fput_in:
Al Viro2903ff02012-08-28 12:52:22 -04001289 fdput(in);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290out:
1291 return retval;
1292}
1293
Heiko Carstens002c8972009-01-14 14:14:18 +01001294SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd, off_t __user *, offset, size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295{
1296 loff_t pos;
1297 off_t off;
1298 ssize_t ret;
1299
1300 if (offset) {
1301 if (unlikely(get_user(off, offset)))
1302 return -EFAULT;
1303 pos = off;
1304 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
1305 if (unlikely(put_user(pos, offset)))
1306 return -EFAULT;
1307 return ret;
1308 }
1309
1310 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1311}
1312
Heiko Carstens002c8972009-01-14 14:14:18 +01001313SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd, loff_t __user *, offset, size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314{
1315 loff_t pos;
1316 ssize_t ret;
1317
1318 if (offset) {
1319 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
1320 return -EFAULT;
1321 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
1322 if (unlikely(put_user(pos, offset)))
1323 return -EFAULT;
1324 return ret;
1325 }
1326
1327 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1328}
Al Viro19f4fc32013-02-24 02:17:03 -05001329
1330#ifdef CONFIG_COMPAT
1331COMPAT_SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd,
1332 compat_off_t __user *, offset, compat_size_t, count)
1333{
1334 loff_t pos;
1335 off_t off;
1336 ssize_t ret;
1337
1338 if (offset) {
1339 if (unlikely(get_user(off, offset)))
1340 return -EFAULT;
1341 pos = off;
1342 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
1343 if (unlikely(put_user(pos, offset)))
1344 return -EFAULT;
1345 return ret;
1346 }
1347
1348 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1349}
1350
1351COMPAT_SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd,
1352 compat_loff_t __user *, offset, compat_size_t, count)
1353{
1354 loff_t pos;
1355 ssize_t ret;
1356
1357 if (offset) {
1358 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
1359 return -EFAULT;
1360 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
1361 if (unlikely(put_user(pos, offset)))
1362 return -EFAULT;
1363 return ret;
1364 }
1365
1366 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1367}
1368#endif
Zach Brown29732932015-11-10 16:53:30 -05001369
Dave Chinnerf16acc92019-06-05 08:04:47 -07001370/**
1371 * generic_copy_file_range - copy data between two files
1372 * @file_in: file structure to read from
1373 * @pos_in: file offset to read from
1374 * @file_out: file structure to write data to
1375 * @pos_out: file offset to write data to
1376 * @len: amount of data to copy
1377 * @flags: copy flags
1378 *
1379 * This is a generic filesystem helper to copy data from one file to another.
1380 * It has no constraints on the source or destination file owners - the files
1381 * can belong to different superblocks and different filesystem types. Short
1382 * copies are allowed.
1383 *
1384 * This should be called from the @file_out filesystem, as per the
1385 * ->copy_file_range() method.
1386 *
1387 * Returns the number of bytes copied or a negative error indicating the
1388 * failure.
1389 */
1390
1391ssize_t generic_copy_file_range(struct file *file_in, loff_t pos_in,
1392 struct file *file_out, loff_t pos_out,
1393 size_t len, unsigned int flags)
1394{
1395 return do_splice_direct(file_in, &pos_in, file_out, &pos_out,
1396 len > MAX_RW_COUNT ? MAX_RW_COUNT : len, 0);
1397}
1398EXPORT_SYMBOL(generic_copy_file_range);
1399
Zach Brown29732932015-11-10 16:53:30 -05001400/*
Darrick J. Wong407e9c62020-10-15 09:21:17 -07001401 * Performs necessary checks before doing a file copy
1402 *
1403 * Can adjust amount of bytes to copy via @req_count argument.
1404 * Returns appropriate error code that caller should return or
1405 * zero in case the copy should be allowed.
1406 */
1407static int generic_copy_file_checks(struct file *file_in, loff_t pos_in,
1408 struct file *file_out, loff_t pos_out,
1409 size_t *req_count, unsigned int flags)
1410{
1411 struct inode *inode_in = file_inode(file_in);
1412 struct inode *inode_out = file_inode(file_out);
1413 uint64_t count = *req_count;
1414 loff_t size_in;
1415 int ret;
1416
1417 ret = generic_file_rw_checks(file_in, file_out);
1418 if (ret)
1419 return ret;
1420
Amir Goldstein868f9f22022-06-30 22:58:49 +03001421 /*
1422 * We allow some filesystems to handle cross sb copy, but passing
1423 * a file of the wrong filesystem type to filesystem driver can result
1424 * in an attempt to dereference the wrong type of ->private_data, so
1425 * avoid doing that until we really have a good reason.
1426 *
1427 * nfs and cifs define several different file_system_type structures
1428 * and several different sets of file_operations, but they all end up
1429 * using the same ->copy_file_range() function pointer.
1430 */
1431 if (file_out->f_op->copy_file_range) {
1432 if (file_in->f_op->copy_file_range !=
1433 file_out->f_op->copy_file_range)
1434 return -EXDEV;
1435 } else if (file_inode(file_in)->i_sb != file_inode(file_out)->i_sb) {
1436 return -EXDEV;
1437 }
1438
Darrick J. Wong407e9c62020-10-15 09:21:17 -07001439 /* Don't touch certain kinds of inodes */
1440 if (IS_IMMUTABLE(inode_out))
1441 return -EPERM;
1442
1443 if (IS_SWAPFILE(inode_in) || IS_SWAPFILE(inode_out))
1444 return -ETXTBSY;
1445
1446 /* Ensure offsets don't wrap. */
1447 if (pos_in + count < pos_in || pos_out + count < pos_out)
1448 return -EOVERFLOW;
1449
1450 /* Shorten the copy to EOF */
1451 size_in = i_size_read(inode_in);
1452 if (pos_in >= size_in)
1453 count = 0;
1454 else
1455 count = min(count, size_in - (uint64_t)pos_in);
1456
1457 ret = generic_write_check_limits(file_out, pos_out, &count);
1458 if (ret)
1459 return ret;
1460
1461 /* Don't allow overlapped copying within the same file. */
1462 if (inode_in == inode_out &&
1463 pos_out + count > pos_in &&
1464 pos_out < pos_in + count)
1465 return -EINVAL;
1466
1467 *req_count = count;
1468 return 0;
1469}
1470
1471/*
Zach Brown29732932015-11-10 16:53:30 -05001472 * copy_file_range() differs from regular file read and write in that it
1473 * specifically allows return partial success. When it does so is up to
1474 * the copy_file_range method.
1475 */
1476ssize_t vfs_copy_file_range(struct file *file_in, loff_t pos_in,
1477 struct file *file_out, loff_t pos_out,
1478 size_t len, unsigned int flags)
1479{
Zach Brown29732932015-11-10 16:53:30 -05001480 ssize_t ret;
1481
1482 if (flags != 0)
1483 return -EINVAL;
1484
Amir Goldstein96e6e8f2019-06-05 08:04:49 -07001485 ret = generic_copy_file_checks(file_in, pos_in, file_out, pos_out, &len,
1486 flags);
Amir Goldsteina3171352019-06-05 08:04:48 -07001487 if (unlikely(ret))
1488 return ret;
Amir Goldstein11cbfb12017-01-31 10:34:56 +02001489
Zach Brown29732932015-11-10 16:53:30 -05001490 ret = rw_verify_area(READ, file_in, &pos_in, len);
Al Virobc613842016-03-31 21:48:20 -04001491 if (unlikely(ret))
1492 return ret;
1493
1494 ret = rw_verify_area(WRITE, file_out, &pos_out, len);
1495 if (unlikely(ret))
Zach Brown29732932015-11-10 16:53:30 -05001496 return ret;
1497
Zach Brown29732932015-11-10 16:53:30 -05001498 if (len == 0)
1499 return 0;
1500
Amir Goldsteinbfe219d2017-01-31 10:34:57 +02001501 file_start_write(file_out);
Zach Brown29732932015-11-10 16:53:30 -05001502
Christoph Hellwiga76b5b02016-12-09 16:17:19 -08001503 /*
Amir Goldstein868f9f22022-06-30 22:58:49 +03001504 * Cloning is supported by more file systems, so we implement copy on
1505 * same sb using clone, but for filesystems where both clone and copy
1506 * are supported (e.g. nfs,cifs), we only call the copy method.
Christoph Hellwiga76b5b02016-12-09 16:17:19 -08001507 */
Amir Goldstein868f9f22022-06-30 22:58:49 +03001508 if (file_out->f_op->copy_file_range) {
1509 ret = file_out->f_op->copy_file_range(file_in, pos_in,
1510 file_out, pos_out,
1511 len, flags);
1512 goto done;
1513 }
1514
Amir Goldstein5dae2222019-06-05 08:04:50 -07001515 if (file_in->f_op->remap_file_range &&
1516 file_inode(file_in)->i_sb == file_inode(file_out)->i_sb) {
Amir Goldstein868f9f22022-06-30 22:58:49 +03001517 ret = file_in->f_op->remap_file_range(file_in, pos_in,
Darrick J. Wong42ec3d42018-10-30 10:41:49 +11001518 file_out, pos_out,
Darrick J. Wongeca36542018-10-30 10:42:10 +11001519 min_t(loff_t, MAX_RW_COUNT, len),
1520 REMAP_FILE_CAN_SHORTEN);
Amir Goldstein868f9f22022-06-30 22:58:49 +03001521 if (ret > 0)
Christoph Hellwiga76b5b02016-12-09 16:17:19 -08001522 goto done;
Christoph Hellwiga76b5b02016-12-09 16:17:19 -08001523 }
1524
Amir Goldstein868f9f22022-06-30 22:58:49 +03001525 /*
1526 * We can get here for same sb copy of filesystems that do not implement
1527 * ->copy_file_range() in case filesystem does not support clone or in
1528 * case filesystem supports clone but rejected the clone request (e.g.
1529 * because it was not block aligned).
1530 *
1531 * In both cases, fall back to kernel copy so we are able to maintain a
1532 * consistent story about which filesystems support copy_file_range()
1533 * and which filesystems do not, that will allow userspace tools to
1534 * make consistent desicions w.r.t using copy_file_range().
1535 */
1536 ret = generic_copy_file_range(file_in, pos_in, file_out, pos_out, len,
1537 flags);
1538
Christoph Hellwiga76b5b02016-12-09 16:17:19 -08001539done:
Zach Brown29732932015-11-10 16:53:30 -05001540 if (ret > 0) {
1541 fsnotify_access(file_in);
1542 add_rchar(current, ret);
1543 fsnotify_modify(file_out);
1544 add_wchar(current, ret);
1545 }
Christoph Hellwiga76b5b02016-12-09 16:17:19 -08001546
Zach Brown29732932015-11-10 16:53:30 -05001547 inc_syscr(current);
1548 inc_syscw(current);
1549
Amir Goldsteinbfe219d2017-01-31 10:34:57 +02001550 file_end_write(file_out);
Zach Brown29732932015-11-10 16:53:30 -05001551
1552 return ret;
1553}
1554EXPORT_SYMBOL(vfs_copy_file_range);
1555
1556SYSCALL_DEFINE6(copy_file_range, int, fd_in, loff_t __user *, off_in,
1557 int, fd_out, loff_t __user *, off_out,
1558 size_t, len, unsigned int, flags)
1559{
1560 loff_t pos_in;
1561 loff_t pos_out;
1562 struct fd f_in;
1563 struct fd f_out;
1564 ssize_t ret = -EBADF;
1565
1566 f_in = fdget(fd_in);
1567 if (!f_in.file)
1568 goto out2;
1569
1570 f_out = fdget(fd_out);
1571 if (!f_out.file)
1572 goto out1;
1573
1574 ret = -EFAULT;
1575 if (off_in) {
1576 if (copy_from_user(&pos_in, off_in, sizeof(loff_t)))
1577 goto out;
1578 } else {
1579 pos_in = f_in.file->f_pos;
1580 }
1581
1582 if (off_out) {
1583 if (copy_from_user(&pos_out, off_out, sizeof(loff_t)))
1584 goto out;
1585 } else {
1586 pos_out = f_out.file->f_pos;
1587 }
1588
1589 ret = vfs_copy_file_range(f_in.file, pos_in, f_out.file, pos_out, len,
1590 flags);
1591 if (ret > 0) {
1592 pos_in += ret;
1593 pos_out += ret;
1594
1595 if (off_in) {
1596 if (copy_to_user(off_in, &pos_in, sizeof(loff_t)))
1597 ret = -EFAULT;
1598 } else {
1599 f_in.file->f_pos = pos_in;
1600 }
1601
1602 if (off_out) {
1603 if (copy_to_user(off_out, &pos_out, sizeof(loff_t)))
1604 ret = -EFAULT;
1605 } else {
1606 f_out.file->f_pos = pos_out;
1607 }
1608 }
1609
1610out:
1611 fdput(f_out);
1612out1:
1613 fdput(f_in);
1614out2:
1615 return ret;
1616}
Christoph Hellwig04b38d62015-12-03 12:59:50 +01001617
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001618/*
Darrick J. Wong407e9c62020-10-15 09:21:17 -07001619 * Don't operate on ranges the page cache doesn't support, and don't exceed the
1620 * LFS limits. If pos is under the limit it becomes a short access. If it
1621 * exceeds the limit we return -EFBIG.
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001622 */
Darrick J. Wong407e9c62020-10-15 09:21:17 -07001623int generic_write_check_limits(struct file *file, loff_t pos, loff_t *count)
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001624{
Darrick J. Wong407e9c62020-10-15 09:21:17 -07001625 struct inode *inode = file->f_mapping->host;
1626 loff_t max_size = inode->i_sb->s_maxbytes;
1627 loff_t limit = rlimit(RLIMIT_FSIZE);
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001628
Darrick J. Wong407e9c62020-10-15 09:21:17 -07001629 if (limit != RLIM_INFINITY) {
1630 if (pos >= limit) {
1631 send_sig(SIGXFSZ, current, 0);
1632 return -EFBIG;
1633 }
1634 *count = min(*count, limit - pos);
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001635 }
1636
Darrick J. Wong407e9c62020-10-15 09:21:17 -07001637 if (!(file->f_flags & O_LARGEFILE))
1638 max_size = MAX_NON_LFS;
Darrick J. Wong54dbc152015-12-19 00:55:59 -08001639
Darrick J. Wong407e9c62020-10-15 09:21:17 -07001640 if (unlikely(pos >= max_size))
1641 return -EFBIG;
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001642
Darrick J. Wong407e9c62020-10-15 09:21:17 -07001643 *count = min(*count, max_size - pos);
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001644
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001645 return 0;
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001646}
Darrick J. Wongc32e5f32018-10-30 10:42:17 +11001647
Omar Sandovalf6f7a252021-08-12 15:34:57 -07001648/* Like generic_write_checks(), but takes size of write instead of iter. */
1649int generic_write_checks_count(struct kiocb *iocb, loff_t *count)
Darrick J. Wong407e9c62020-10-15 09:21:17 -07001650{
1651 struct file *file = iocb->ki_filp;
1652 struct inode *inode = file->f_mapping->host;
Darrick J. Wong407e9c62020-10-15 09:21:17 -07001653
1654 if (IS_SWAPFILE(inode))
1655 return -ETXTBSY;
1656
Omar Sandovalf6f7a252021-08-12 15:34:57 -07001657 if (!*count)
Darrick J. Wong407e9c62020-10-15 09:21:17 -07001658 return 0;
1659
Darrick J. Wong407e9c62020-10-15 09:21:17 -07001660 if (iocb->ki_flags & IOCB_APPEND)
1661 iocb->ki_pos = i_size_read(inode);
1662
1663 if ((iocb->ki_flags & IOCB_NOWAIT) && !(iocb->ki_flags & IOCB_DIRECT))
1664 return -EINVAL;
1665
Omar Sandovalf6f7a252021-08-12 15:34:57 -07001666 return generic_write_check_limits(iocb->ki_filp, iocb->ki_pos, count);
1667}
1668EXPORT_SYMBOL(generic_write_checks_count);
1669
1670/*
1671 * Performs necessary checks before doing a write
1672 *
1673 * Can adjust writing position or amount of bytes to write.
1674 * Returns appropriate error code that caller should return or
1675 * zero in case that write should be allowed.
1676 */
1677ssize_t generic_write_checks(struct kiocb *iocb, struct iov_iter *from)
1678{
1679 loff_t count = iov_iter_count(from);
1680 int ret;
1681
1682 ret = generic_write_checks_count(iocb, &count);
Darrick J. Wong407e9c62020-10-15 09:21:17 -07001683 if (ret)
1684 return ret;
1685
1686 iov_iter_truncate(from, count);
1687 return iov_iter_count(from);
1688}
1689EXPORT_SYMBOL(generic_write_checks);
1690
1691/*
1692 * Performs common checks before doing a file copy/clone
1693 * from @file_in to @file_out.
1694 */
1695int generic_file_rw_checks(struct file *file_in, struct file *file_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696{
Darrick J. Wong1383a7e2018-10-30 10:40:31 +11001697 struct inode *inode_in = file_inode(file_in);
1698 struct inode *inode_out = file_inode(file_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
Darrick J. Wong407e9c62020-10-15 09:21:17 -07001700 /* Don't copy dirs, pipes, sockets... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 if (S_ISDIR(inode_in->i_mode) || S_ISDIR(inode_out->i_mode))
1702 return -EISDIR;
1703 if (!S_ISREG(inode_in->i_mode) || !S_ISREG(inode_out->i_mode))
1704 return -EINVAL;
1705
Darrick J. Wong407e9c62020-10-15 09:21:17 -07001706 if (!(file_in->f_mode & FMODE_READ) ||
1707 !(file_out->f_mode & FMODE_WRITE) ||
1708 (file_out->f_flags & O_APPEND))
1709 return -EBADF;
Darrick J. Wong1383a7e2018-10-30 10:40:31 +11001710
Darrick J. Wong407e9c62020-10-15 09:21:17 -07001711 return 0;
Christoph Hellwig04b38d62015-12-03 12:59:50 +01001712}