blob: c06d7f9e35200549ba7ca5ff1a8dfc2e67b091db [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/read_write.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
7#include <linux/slab.h>
8#include <linux/stat.h>
9#include <linux/fcntl.h>
10#include <linux/file.h>
11#include <linux/uio.h>
Robert Love0eeca282005-07-12 17:06:03 -040012#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/security.h>
Paul Gortmaker630d9c42011-11-16 23:57:37 -050014#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/syscalls.h>
Linus Torvaldse28cc712006-01-04 16:20:40 -080016#include <linux/pagemap.h>
Jens Axboed6b29d72007-06-04 09:59:47 +020017#include <linux/splice.h>
Al Viro561c6732013-02-24 10:52:26 -050018#include <linux/compat.h>
Zach Brown29732932015-11-10 16:53:30 -050019#include <linux/mount.h>
Wouter van Kesteren2feb55f2016-02-16 22:20:59 +010020#include <linux/fs.h>
Al Viro06ae43f2013-03-20 13:19:30 -040021#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23#include <asm/uaccess.h>
24#include <asm/unistd.h>
25
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080026const struct file_operations generic_ro_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 .llseek = generic_file_llseek,
Al Viroaad4f8b2014-04-02 14:33:16 -040028 .read_iter = generic_file_read_iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 .mmap = generic_file_readonly_mmap,
Jens Axboe534f2aa2007-06-01 14:52:37 +020030 .splice_read = generic_file_splice_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -070031};
32
33EXPORT_SYMBOL(generic_ro_fops);
34
Al Virocccb5a12010-12-17 07:44:05 -050035static inline int unsigned_offsets(struct file *file)
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -070036{
Al Virocccb5a12010-12-17 07:44:05 -050037 return file->f_mode & FMODE_UNSIGNED_OFFSET;
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -070038}
39
Jie Liu46a1c2c2013-06-25 12:02:13 +080040/**
41 * vfs_setpos - update the file offset for lseek
42 * @file: file structure in question
43 * @offset: file offset to seek to
44 * @maxsize: maximum file size
45 *
46 * This is a low-level filesystem helper for updating the file offset to
47 * the value specified by @offset if the given offset is valid and it is
48 * not equal to the current file offset.
49 *
50 * Return the specified offset on success and -EINVAL on invalid offset.
51 */
52loff_t vfs_setpos(struct file *file, loff_t offset, loff_t maxsize)
Andi Kleenef3d0fd2011-09-15 16:06:48 -070053{
54 if (offset < 0 && !unsigned_offsets(file))
55 return -EINVAL;
56 if (offset > maxsize)
57 return -EINVAL;
58
59 if (offset != file->f_pos) {
60 file->f_pos = offset;
61 file->f_version = 0;
62 }
63 return offset;
64}
Jie Liu46a1c2c2013-06-25 12:02:13 +080065EXPORT_SYMBOL(vfs_setpos);
Andi Kleenef3d0fd2011-09-15 16:06:48 -070066
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020067/**
Andi Kleen57604952011-09-15 16:06:50 -070068 * generic_file_llseek_size - generic llseek implementation for regular files
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020069 * @file: file structure to seek on
70 * @offset: file offset to seek to
Andrew Morton965c8e52012-12-17 15:59:39 -080071 * @whence: type of seek
Eric Sandeene8b96eb2012-04-30 13:11:29 -050072 * @size: max size of this file in file system
73 * @eof: offset used for SEEK_END position
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020074 *
Andi Kleen57604952011-09-15 16:06:50 -070075 * This is a variant of generic_file_llseek that allows passing in a custom
Eric Sandeene8b96eb2012-04-30 13:11:29 -050076 * maximum file size and a custom EOF position, for e.g. hashed directories
Andi Kleenef3d0fd2011-09-15 16:06:48 -070077 *
78 * Synchronization:
Andi Kleen57604952011-09-15 16:06:50 -070079 * SEEK_SET and SEEK_END are unsynchronized (but atomic on 64bit platforms)
Andi Kleenef3d0fd2011-09-15 16:06:48 -070080 * SEEK_CUR is synchronized against other SEEK_CURs, but not read/writes.
81 * read/writes behave like SEEK_SET against seeks.
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020082 */
Andi Kleen9465efc2008-06-27 11:05:24 +020083loff_t
Andrew Morton965c8e52012-12-17 15:59:39 -080084generic_file_llseek_size(struct file *file, loff_t offset, int whence,
Eric Sandeene8b96eb2012-04-30 13:11:29 -050085 loff_t maxsize, loff_t eof)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
Andrew Morton965c8e52012-12-17 15:59:39 -080087 switch (whence) {
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020088 case SEEK_END:
Eric Sandeene8b96eb2012-04-30 13:11:29 -050089 offset += eof;
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020090 break;
91 case SEEK_CUR:
Alain Knaff5b6f1eb2008-11-10 17:08:08 -080092 /*
93 * Here we special-case the lseek(fd, 0, SEEK_CUR)
94 * position-querying operation. Avoid rewriting the "same"
95 * f_pos value back to the file because a concurrent read(),
96 * write() or lseek() might have altered it
97 */
98 if (offset == 0)
99 return file->f_pos;
Andi Kleenef3d0fd2011-09-15 16:06:48 -0700100 /*
101 * f_lock protects against read/modify/write race with other
102 * SEEK_CURs. Note that parallel writes and reads behave
103 * like SEEK_SET.
104 */
105 spin_lock(&file->f_lock);
Jie Liu46a1c2c2013-06-25 12:02:13 +0800106 offset = vfs_setpos(file, file->f_pos + offset, maxsize);
Andi Kleenef3d0fd2011-09-15 16:06:48 -0700107 spin_unlock(&file->f_lock);
108 return offset;
Josef Bacik982d8162011-07-18 13:21:35 -0400109 case SEEK_DATA:
110 /*
111 * In the generic case the entire file is data, so as long as
112 * offset isn't at the end of the file then the offset is data.
113 */
Andreas Gruenbacherf3e2e7f2017-09-25 12:23:03 +0200114 if ((unsigned long long)offset >= eof)
Josef Bacik982d8162011-07-18 13:21:35 -0400115 return -ENXIO;
116 break;
117 case SEEK_HOLE:
118 /*
119 * There is a virtual hole at the end of the file, so as long as
120 * offset isn't i_size or larger, return i_size.
121 */
Andreas Gruenbacherf3e2e7f2017-09-25 12:23:03 +0200122 if ((unsigned long long)offset >= eof)
Josef Bacik982d8162011-07-18 13:21:35 -0400123 return -ENXIO;
Eric Sandeene8b96eb2012-04-30 13:11:29 -0500124 offset = eof;
Josef Bacik982d8162011-07-18 13:21:35 -0400125 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 }
Christoph Hellwig3a8cff42008-08-11 15:37:17 +0200127
Jie Liu46a1c2c2013-06-25 12:02:13 +0800128 return vfs_setpos(file, offset, maxsize);
Andi Kleen57604952011-09-15 16:06:50 -0700129}
130EXPORT_SYMBOL(generic_file_llseek_size);
131
132/**
133 * generic_file_llseek - generic llseek implementation for regular files
134 * @file: file structure to seek on
135 * @offset: file offset to seek to
Andrew Morton965c8e52012-12-17 15:59:39 -0800136 * @whence: type of seek
Andi Kleen57604952011-09-15 16:06:50 -0700137 *
138 * This is a generic implemenation of ->llseek useable for all normal local
139 * filesystems. It just updates the file offset to the value specified by
Ming Lei546ae2d2013-04-29 15:06:07 -0700140 * @offset and @whence.
Andi Kleen57604952011-09-15 16:06:50 -0700141 */
Andrew Morton965c8e52012-12-17 15:59:39 -0800142loff_t generic_file_llseek(struct file *file, loff_t offset, int whence)
Andi Kleen57604952011-09-15 16:06:50 -0700143{
144 struct inode *inode = file->f_mapping->host;
145
Andrew Morton965c8e52012-12-17 15:59:39 -0800146 return generic_file_llseek_size(file, offset, whence,
Eric Sandeene8b96eb2012-04-30 13:11:29 -0500147 inode->i_sb->s_maxbytes,
148 i_size_read(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
Andi Kleen9465efc2008-06-27 11:05:24 +0200150EXPORT_SYMBOL(generic_file_llseek);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
jan Blunckae6afc32010-05-26 14:44:48 -0700152/**
Al Viro1bf9d142013-06-16 20:27:42 +0400153 * fixed_size_llseek - llseek implementation for fixed-sized devices
154 * @file: file structure to seek on
155 * @offset: file offset to seek to
156 * @whence: type of seek
157 * @size: size of the file
158 *
159 */
160loff_t fixed_size_llseek(struct file *file, loff_t offset, int whence, loff_t size)
161{
162 switch (whence) {
163 case SEEK_SET: case SEEK_CUR: case SEEK_END:
164 return generic_file_llseek_size(file, offset, whence,
165 size, size);
166 default:
167 return -EINVAL;
168 }
169}
170EXPORT_SYMBOL(fixed_size_llseek);
171
172/**
Al Virob25472f2015-12-05 22:04:48 -0500173 * no_seek_end_llseek - llseek implementation for fixed-sized devices
174 * @file: file structure to seek on
175 * @offset: file offset to seek to
176 * @whence: type of seek
177 *
178 */
179loff_t no_seek_end_llseek(struct file *file, loff_t offset, int whence)
180{
181 switch (whence) {
182 case SEEK_SET: case SEEK_CUR:
183 return generic_file_llseek_size(file, offset, whence,
Wouter van Kesteren2feb55f2016-02-16 22:20:59 +0100184 OFFSET_MAX, 0);
Al Virob25472f2015-12-05 22:04:48 -0500185 default:
186 return -EINVAL;
187 }
188}
189EXPORT_SYMBOL(no_seek_end_llseek);
190
191/**
192 * no_seek_end_llseek_size - llseek implementation for fixed-sized devices
193 * @file: file structure to seek on
194 * @offset: file offset to seek to
195 * @whence: type of seek
196 * @size: maximal offset allowed
197 *
198 */
199loff_t no_seek_end_llseek_size(struct file *file, loff_t offset, int whence, loff_t size)
200{
201 switch (whence) {
202 case SEEK_SET: case SEEK_CUR:
203 return generic_file_llseek_size(file, offset, whence,
204 size, 0);
205 default:
206 return -EINVAL;
207 }
208}
209EXPORT_SYMBOL(no_seek_end_llseek_size);
210
211/**
jan Blunckae6afc32010-05-26 14:44:48 -0700212 * noop_llseek - No Operation Performed llseek implementation
213 * @file: file structure to seek on
214 * @offset: file offset to seek to
Andrew Morton965c8e52012-12-17 15:59:39 -0800215 * @whence: type of seek
jan Blunckae6afc32010-05-26 14:44:48 -0700216 *
217 * This is an implementation of ->llseek useable for the rare special case when
218 * userspace expects the seek to succeed but the (device) file is actually not
219 * able to perform the seek. In this case you use noop_llseek() instead of
220 * falling back to the default implementation of ->llseek.
221 */
Andrew Morton965c8e52012-12-17 15:59:39 -0800222loff_t noop_llseek(struct file *file, loff_t offset, int whence)
jan Blunckae6afc32010-05-26 14:44:48 -0700223{
224 return file->f_pos;
225}
226EXPORT_SYMBOL(noop_llseek);
227
Andrew Morton965c8e52012-12-17 15:59:39 -0800228loff_t no_llseek(struct file *file, loff_t offset, int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
230 return -ESPIPE;
231}
232EXPORT_SYMBOL(no_llseek);
233
Andrew Morton965c8e52012-12-17 15:59:39 -0800234loff_t default_llseek(struct file *file, loff_t offset, int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
Al Viro496ad9a2013-01-23 17:07:38 -0500236 struct inode *inode = file_inode(file);
David Sterba16abef02008-04-22 15:09:22 +0200237 loff_t retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Al Viro59551022016-01-22 15:40:57 -0500239 inode_lock(inode);
Andrew Morton965c8e52012-12-17 15:59:39 -0800240 switch (whence) {
Chris Snook7b8e8922007-05-08 00:24:13 -0700241 case SEEK_END:
Josef Bacik982d8162011-07-18 13:21:35 -0400242 offset += i_size_read(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 break;
Chris Snook7b8e8922007-05-08 00:24:13 -0700244 case SEEK_CUR:
Alain Knaff5b6f1eb2008-11-10 17:08:08 -0800245 if (offset == 0) {
246 retval = file->f_pos;
247 goto out;
248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 offset += file->f_pos;
Josef Bacik982d8162011-07-18 13:21:35 -0400250 break;
251 case SEEK_DATA:
252 /*
253 * In the generic case the entire file is data, so as
254 * long as offset isn't at the end of the file then the
255 * offset is data.
256 */
Dan Carpenterbacb2d82011-07-26 17:25:20 +0300257 if (offset >= inode->i_size) {
258 retval = -ENXIO;
259 goto out;
260 }
Josef Bacik982d8162011-07-18 13:21:35 -0400261 break;
262 case SEEK_HOLE:
263 /*
264 * There is a virtual hole at the end of the file, so
265 * as long as offset isn't i_size or larger, return
266 * i_size.
267 */
Dan Carpenterbacb2d82011-07-26 17:25:20 +0300268 if (offset >= inode->i_size) {
269 retval = -ENXIO;
270 goto out;
271 }
Josef Bacik982d8162011-07-18 13:21:35 -0400272 offset = inode->i_size;
273 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 }
275 retval = -EINVAL;
Al Virocccb5a12010-12-17 07:44:05 -0500276 if (offset >= 0 || unsigned_offsets(file)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 if (offset != file->f_pos) {
278 file->f_pos = offset;
279 file->f_version = 0;
280 }
281 retval = offset;
282 }
Alain Knaff5b6f1eb2008-11-10 17:08:08 -0800283out:
Al Viro59551022016-01-22 15:40:57 -0500284 inode_unlock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 return retval;
286}
287EXPORT_SYMBOL(default_llseek);
288
Andrew Morton965c8e52012-12-17 15:59:39 -0800289loff_t vfs_llseek(struct file *file, loff_t offset, int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
291 loff_t (*fn)(struct file *, loff_t, int);
292
293 fn = no_llseek;
294 if (file->f_mode & FMODE_LSEEK) {
Al Viro72c2d532013-09-22 16:27:52 -0400295 if (file->f_op->llseek)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 fn = file->f_op->llseek;
297 }
Andrew Morton965c8e52012-12-17 15:59:39 -0800298 return fn(file, offset, whence);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
300EXPORT_SYMBOL(vfs_llseek);
301
Andrew Morton965c8e52012-12-17 15:59:39 -0800302SYSCALL_DEFINE3(lseek, unsigned int, fd, off_t, offset, unsigned int, whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
304 off_t retval;
Linus Torvalds9c225f22014-03-03 09:36:58 -0800305 struct fd f = fdget_pos(fd);
Al Viro2903ff02012-08-28 12:52:22 -0400306 if (!f.file)
307 return -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 retval = -EINVAL;
Andrew Morton965c8e52012-12-17 15:59:39 -0800310 if (whence <= SEEK_MAX) {
311 loff_t res = vfs_llseek(f.file, offset, whence);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 retval = res;
313 if (res != (loff_t)retval)
314 retval = -EOVERFLOW; /* LFS: should only happen on 32 bit platforms */
315 }
Linus Torvalds9c225f22014-03-03 09:36:58 -0800316 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 return retval;
318}
319
Al Viro561c6732013-02-24 10:52:26 -0500320#ifdef CONFIG_COMPAT
321COMPAT_SYSCALL_DEFINE3(lseek, unsigned int, fd, compat_off_t, offset, unsigned int, whence)
322{
323 return sys_lseek(fd, offset, whence);
324}
325#endif
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327#ifdef __ARCH_WANT_SYS_LLSEEK
Heiko Carstens003d7ab2009-01-14 14:14:21 +0100328SYSCALL_DEFINE5(llseek, unsigned int, fd, unsigned long, offset_high,
329 unsigned long, offset_low, loff_t __user *, result,
Andrew Morton965c8e52012-12-17 15:59:39 -0800330 unsigned int, whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
332 int retval;
Eric Biggersd7a15f82014-03-16 14:24:08 -0500333 struct fd f = fdget_pos(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 loff_t offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Al Viro2903ff02012-08-28 12:52:22 -0400336 if (!f.file)
337 return -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 retval = -EINVAL;
Andrew Morton965c8e52012-12-17 15:59:39 -0800340 if (whence > SEEK_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 goto out_putf;
342
Al Viro2903ff02012-08-28 12:52:22 -0400343 offset = vfs_llseek(f.file, ((loff_t) offset_high << 32) | offset_low,
Andrew Morton965c8e52012-12-17 15:59:39 -0800344 whence);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 retval = (int)offset;
347 if (offset >= 0) {
348 retval = -EFAULT;
349 if (!copy_to_user(result, &offset, sizeof(offset)))
350 retval = 0;
351 }
352out_putf:
Eric Biggersd7a15f82014-03-16 14:24:08 -0500353 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return retval;
355}
356#endif
357
Christoph Hellwigdbe4e192015-01-25 21:11:59 +0100358ssize_t vfs_iter_read(struct file *file, struct iov_iter *iter, loff_t *ppos)
359{
360 struct kiocb kiocb;
361 ssize_t ret;
362
363 if (!file->f_op->read_iter)
364 return -EINVAL;
365
366 init_sync_kiocb(&kiocb, file);
367 kiocb.ki_pos = *ppos;
Christoph Hellwigdbe4e192015-01-25 21:11:59 +0100368
369 iter->type |= READ;
370 ret = file->f_op->read_iter(&kiocb, iter);
Christoph Hellwig599bd192015-02-11 19:59:44 +0100371 BUG_ON(ret == -EIOCBQUEUED);
Christoph Hellwigdbe4e192015-01-25 21:11:59 +0100372 if (ret > 0)
373 *ppos = kiocb.ki_pos;
374 return ret;
375}
376EXPORT_SYMBOL(vfs_iter_read);
377
378ssize_t vfs_iter_write(struct file *file, struct iov_iter *iter, loff_t *ppos)
379{
380 struct kiocb kiocb;
381 ssize_t ret;
382
383 if (!file->f_op->write_iter)
384 return -EINVAL;
385
386 init_sync_kiocb(&kiocb, file);
387 kiocb.ki_pos = *ppos;
Christoph Hellwigdbe4e192015-01-25 21:11:59 +0100388
389 iter->type |= WRITE;
390 ret = file->f_op->write_iter(&kiocb, iter);
Christoph Hellwig599bd192015-02-11 19:59:44 +0100391 BUG_ON(ret == -EIOCBQUEUED);
Jimmy Durand Wesolowskic5cc9332019-01-31 15:19:39 +0100392 if (ret > 0) {
Christoph Hellwigdbe4e192015-01-25 21:11:59 +0100393 *ppos = kiocb.ki_pos;
Jimmy Durand Wesolowskic5cc9332019-01-31 15:19:39 +0100394 fsnotify_modify(file);
395 }
Christoph Hellwigdbe4e192015-01-25 21:11:59 +0100396 return ret;
397}
398EXPORT_SYMBOL(vfs_iter_write);
399
Al Viro68d70d02013-06-19 15:26:04 +0400400int rw_verify_area(int read_write, struct file *file, const loff_t *ppos, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
402 struct inode *inode;
403 loff_t pos;
James Morrisc43e2592008-01-12 22:05:48 +1100404 int retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Al Viro496ad9a2013-01-23 17:07:38 -0500406 inode = file_inode(file);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800407 if (unlikely((ssize_t) count < 0))
James Morrisc43e2592008-01-12 22:05:48 +1100408 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 pos = *ppos;
Al Virocccb5a12010-12-17 07:44:05 -0500410 if (unlikely(pos < 0)) {
411 if (!unsigned_offsets(file))
412 return retval;
413 if (count >= -pos) /* both values are in 0..LLONG_MAX */
414 return -EOVERFLOW;
415 } else if (unlikely((loff_t) (pos + count) < 0)) {
416 if (!unsigned_offsets(file))
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -0700417 return retval;
418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Jeff Laytonbd61e0a2015-01-16 15:05:55 -0500420 if (unlikely(inode->i_flctx && mandatory_lock(inode))) {
Christoph Hellwigacc15572015-12-03 12:59:49 +0100421 retval = locks_mandatory_area(inode, file, pos, pos + count - 1,
422 read_write == READ ? F_RDLCK : F_WRLCK);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800423 if (retval < 0)
424 return retval;
425 }
Al Virobc613842016-03-31 21:48:20 -0400426 return security_file_permission(file,
James Morrisc43e2592008-01-12 22:05:48 +1100427 read_write == READ ? MAY_READ : MAY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428}
429
Al Viro5d5d5682015-04-03 15:41:18 -0400430static ssize_t new_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
Al Viro293bc982014-02-11 18:37:41 -0500431{
432 struct iovec iov = { .iov_base = buf, .iov_len = len };
433 struct kiocb kiocb;
434 struct iov_iter iter;
435 ssize_t ret;
436
437 init_sync_kiocb(&kiocb, filp);
438 kiocb.ki_pos = *ppos;
Al Viro293bc982014-02-11 18:37:41 -0500439 iov_iter_init(&iter, READ, &iov, 1, len);
440
441 ret = filp->f_op->read_iter(&kiocb, &iter);
Christoph Hellwig599bd192015-02-11 19:59:44 +0100442 BUG_ON(ret == -EIOCBQUEUED);
Al Viro293bc982014-02-11 18:37:41 -0500443 *ppos = kiocb.ki_pos;
444 return ret;
445}
446
Dmitry Kasatkin6fb50322014-11-05 17:01:17 +0200447ssize_t __vfs_read(struct file *file, char __user *buf, size_t count,
448 loff_t *pos)
449{
Dmitry Kasatkin6fb50322014-11-05 17:01:17 +0200450 if (file->f_op->read)
Al Viro3d04c8a2015-04-03 15:09:18 -0400451 return file->f_op->read(file, buf, count, pos);
Dmitry Kasatkin6fb50322014-11-05 17:01:17 +0200452 else if (file->f_op->read_iter)
Al Viro3d04c8a2015-04-03 15:09:18 -0400453 return new_sync_read(file, buf, count, pos);
Dmitry Kasatkin6fb50322014-11-05 17:01:17 +0200454 else
Al Viro3d04c8a2015-04-03 15:09:18 -0400455 return -EINVAL;
Dmitry Kasatkin6fb50322014-11-05 17:01:17 +0200456}
Al Viro3d04c8a2015-04-03 15:09:18 -0400457EXPORT_SYMBOL(__vfs_read);
Dmitry Kasatkin6fb50322014-11-05 17:01:17 +0200458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
460{
461 ssize_t ret;
462
463 if (!(file->f_mode & FMODE_READ))
464 return -EBADF;
Al Viro7f7f25e2014-02-11 17:49:24 -0500465 if (!(file->f_mode & FMODE_CAN_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 return -EINVAL;
467 if (unlikely(!access_ok(VERIFY_WRITE, buf, count)))
468 return -EFAULT;
469
470 ret = rw_verify_area(READ, file, pos, count);
Al Virobc613842016-03-31 21:48:20 -0400471 if (!ret) {
472 if (count > MAX_RW_COUNT)
473 count = MAX_RW_COUNT;
Dmitry Kasatkin6fb50322014-11-05 17:01:17 +0200474 ret = __vfs_read(file, buf, count, pos);
James Morrisc43e2592008-01-12 22:05:48 +1100475 if (ret > 0) {
Eric Paris2a12a9d2009-12-17 21:24:21 -0500476 fsnotify_access(file);
James Morrisc43e2592008-01-12 22:05:48 +1100477 add_rchar(current, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 }
James Morrisc43e2592008-01-12 22:05:48 +1100479 inc_syscr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 }
481
482 return ret;
483}
484
485EXPORT_SYMBOL(vfs_read);
486
Al Viro5d5d5682015-04-03 15:41:18 -0400487static 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 -0500488{
489 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len };
490 struct kiocb kiocb;
491 struct iov_iter iter;
492 ssize_t ret;
493
494 init_sync_kiocb(&kiocb, filp);
495 kiocb.ki_pos = *ppos;
Al Viro293bc982014-02-11 18:37:41 -0500496 iov_iter_init(&iter, WRITE, &iov, 1, len);
497
498 ret = filp->f_op->write_iter(&kiocb, &iter);
Christoph Hellwig599bd192015-02-11 19:59:44 +0100499 BUG_ON(ret == -EIOCBQUEUED);
Al Virof765b132015-04-06 20:50:38 -0400500 if (ret > 0)
501 *ppos = kiocb.ki_pos;
Al Viro293bc982014-02-11 18:37:41 -0500502 return ret;
503}
504
Al Viro493c84c2015-04-03 15:06:43 -0400505ssize_t __vfs_write(struct file *file, const char __user *p, size_t count,
506 loff_t *pos)
507{
508 if (file->f_op->write)
509 return file->f_op->write(file, p, count, pos);
Al Viro493c84c2015-04-03 15:06:43 -0400510 else if (file->f_op->write_iter)
511 return new_sync_write(file, p, count, pos);
512 else
513 return -EINVAL;
514}
515EXPORT_SYMBOL(__vfs_write);
516
Al Viro06ae43f2013-03-20 13:19:30 -0400517ssize_t __kernel_write(struct file *file, const char *buf, size_t count, loff_t *pos)
518{
519 mm_segment_t old_fs;
520 const char __user *p;
521 ssize_t ret;
522
Al Viro7f7f25e2014-02-11 17:49:24 -0500523 if (!(file->f_mode & FMODE_CAN_WRITE))
Al Viro3e84f482013-03-27 15:20:30 +0000524 return -EINVAL;
525
Al Viro06ae43f2013-03-20 13:19:30 -0400526 old_fs = get_fs();
527 set_fs(get_ds());
528 p = (__force const char __user *)buf;
529 if (count > MAX_RW_COUNT)
530 count = MAX_RW_COUNT;
Al Viro493c84c2015-04-03 15:06:43 -0400531 ret = __vfs_write(file, p, count, pos);
Al Viro06ae43f2013-03-20 13:19:30 -0400532 set_fs(old_fs);
533 if (ret > 0) {
534 fsnotify_modify(file);
535 add_wchar(current, ret);
536 }
537 inc_syscw(current);
538 return ret;
539}
540
Al Viro2ec3a122014-08-19 11:48:09 -0400541EXPORT_SYMBOL(__kernel_write);
542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
544{
545 ssize_t ret;
546
547 if (!(file->f_mode & FMODE_WRITE))
548 return -EBADF;
Al Viro7f7f25e2014-02-11 17:49:24 -0500549 if (!(file->f_mode & FMODE_CAN_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 return -EINVAL;
551 if (unlikely(!access_ok(VERIFY_READ, buf, count)))
552 return -EFAULT;
553
554 ret = rw_verify_area(WRITE, file, pos, count);
Al Virobc613842016-03-31 21:48:20 -0400555 if (!ret) {
556 if (count > MAX_RW_COUNT)
557 count = MAX_RW_COUNT;
Al Viro03d95eb2013-03-20 13:04:20 -0400558 file_start_write(file);
Al Viro493c84c2015-04-03 15:06:43 -0400559 ret = __vfs_write(file, buf, count, pos);
James Morrisc43e2592008-01-12 22:05:48 +1100560 if (ret > 0) {
Eric Paris2a12a9d2009-12-17 21:24:21 -0500561 fsnotify_modify(file);
James Morrisc43e2592008-01-12 22:05:48 +1100562 add_wchar(current, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 }
James Morrisc43e2592008-01-12 22:05:48 +1100564 inc_syscw(current);
Al Viro03d95eb2013-03-20 13:04:20 -0400565 file_end_write(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 }
567
568 return ret;
569}
570
571EXPORT_SYMBOL(vfs_write);
572
573static inline loff_t file_pos_read(struct file *file)
574{
575 return file->f_pos;
576}
577
578static inline void file_pos_write(struct file *file, loff_t pos)
579{
580 file->f_pos = pos;
581}
582
Heiko Carstens3cdad422009-01-14 14:14:22 +0100583SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
Linus Torvalds9c225f22014-03-03 09:36:58 -0800585 struct fd f = fdget_pos(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Al Viro2903ff02012-08-28 12:52:22 -0400588 if (f.file) {
589 loff_t pos = file_pos_read(f.file);
590 ret = vfs_read(f.file, buf, count, &pos);
Al Viro5faf1532013-06-15 05:49:36 +0400591 if (ret >= 0)
592 file_pos_write(f.file, pos);
Linus Torvalds9c225f22014-03-03 09:36:58 -0800593 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 return ret;
596}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Heiko Carstens3cdad422009-01-14 14:14:22 +0100598SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf,
599 size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
Linus Torvalds9c225f22014-03-03 09:36:58 -0800601 struct fd f = fdget_pos(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Al Viro2903ff02012-08-28 12:52:22 -0400604 if (f.file) {
605 loff_t pos = file_pos_read(f.file);
606 ret = vfs_write(f.file, buf, count, &pos);
Al Viro5faf1532013-06-15 05:49:36 +0400607 if (ret >= 0)
608 file_pos_write(f.file, pos);
Linus Torvalds9c225f22014-03-03 09:36:58 -0800609 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 }
611
612 return ret;
613}
614
Al Viro4a0fd5b2013-01-21 15:16:58 -0500615SYSCALL_DEFINE4(pread64, unsigned int, fd, char __user *, buf,
616 size_t, count, loff_t, pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
Al Viro2903ff02012-08-28 12:52:22 -0400618 struct fd f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 if (pos < 0)
622 return -EINVAL;
623
Al Viro2903ff02012-08-28 12:52:22 -0400624 f = fdget(fd);
625 if (f.file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -0400627 if (f.file->f_mode & FMODE_PREAD)
628 ret = vfs_read(f.file, buf, count, &pos);
629 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 }
631
632 return ret;
633}
634
Al Viro4a0fd5b2013-01-21 15:16:58 -0500635SYSCALL_DEFINE4(pwrite64, unsigned int, fd, const char __user *, buf,
636 size_t, count, loff_t, pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
Al Viro2903ff02012-08-28 12:52:22 -0400638 struct fd f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641 if (pos < 0)
642 return -EINVAL;
643
Al Viro2903ff02012-08-28 12:52:22 -0400644 f = fdget(fd);
645 if (f.file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -0400647 if (f.file->f_mode & FMODE_PWRITE)
648 ret = vfs_write(f.file, buf, count, &pos);
649 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 }
651
652 return ret;
653}
654
655/*
656 * Reduce an iovec's length in-place. Return the resulting number of segments
657 */
658unsigned long iov_shorten(struct iovec *iov, unsigned long nr_segs, size_t to)
659{
660 unsigned long seg = 0;
661 size_t len = 0;
662
663 while (seg < nr_segs) {
664 seg++;
665 if (len + iov->iov_len >= to) {
666 iov->iov_len = to - len;
667 break;
668 }
669 len += iov->iov_len;
670 iov++;
671 }
672 return seg;
673}
Eric Sandeen19295522008-01-28 23:58:27 -0500674EXPORT_SYMBOL(iov_shorten);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Al Viroac15ac02015-03-20 20:10:21 -0400676static ssize_t do_iter_readv_writev(struct file *filp, struct iov_iter *iter,
Miklos Szeredi04676262017-02-20 16:51:23 +0100677 loff_t *ppos, int type, int flags)
Al Viro293bc982014-02-11 18:37:41 -0500678{
679 struct kiocb kiocb;
Al Viro293bc982014-02-11 18:37:41 -0500680 ssize_t ret;
681
Christoph Hellwige864f392016-04-07 08:52:03 -0700682 if (flags & ~(RWF_HIPRI | RWF_DSYNC | RWF_SYNC))
Christoph Hellwig793b80e2016-03-03 16:03:58 +0100683 return -EOPNOTSUPP;
684
Al Viro293bc982014-02-11 18:37:41 -0500685 init_sync_kiocb(&kiocb, filp);
Christoph Hellwig97be7eb2016-03-03 16:04:01 +0100686 if (flags & RWF_HIPRI)
687 kiocb.ki_flags |= IOCB_HIPRI;
Christoph Hellwige864f392016-04-07 08:52:03 -0700688 if (flags & RWF_DSYNC)
689 kiocb.ki_flags |= IOCB_DSYNC;
690 if (flags & RWF_SYNC)
691 kiocb.ki_flags |= (IOCB_DSYNC | IOCB_SYNC);
Al Viro293bc982014-02-11 18:37:41 -0500692 kiocb.ki_pos = *ppos;
Al Viro293bc982014-02-11 18:37:41 -0500693
Miklos Szeredi04676262017-02-20 16:51:23 +0100694 if (type == READ)
695 ret = filp->f_op->read_iter(&kiocb, iter);
696 else
697 ret = filp->f_op->write_iter(&kiocb, iter);
Christoph Hellwig599bd192015-02-11 19:59:44 +0100698 BUG_ON(ret == -EIOCBQUEUED);
Al Viro293bc982014-02-11 18:37:41 -0500699 *ppos = kiocb.ki_pos;
700 return ret;
701}
702
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700703/* Do it by hand, with file-ops */
Al Viroac15ac02015-03-20 20:10:21 -0400704static ssize_t do_loop_readv_writev(struct file *filp, struct iov_iter *iter,
Miklos Szeredi04676262017-02-20 16:51:23 +0100705 loff_t *ppos, int type, int flags)
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700706{
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700707 ssize_t ret = 0;
708
Christoph Hellwig97be7eb2016-03-03 16:04:01 +0100709 if (flags & ~RWF_HIPRI)
Christoph Hellwig793b80e2016-03-03 16:03:58 +0100710 return -EOPNOTSUPP;
711
Al Viroac15ac02015-03-20 20:10:21 -0400712 while (iov_iter_count(iter)) {
713 struct iovec iovec = iov_iter_iovec(iter);
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700714 ssize_t nr;
715
Miklos Szeredi04676262017-02-20 16:51:23 +0100716 if (type == READ) {
717 nr = filp->f_op->read(filp, iovec.iov_base,
718 iovec.iov_len, ppos);
719 } else {
720 nr = filp->f_op->write(filp, iovec.iov_base,
721 iovec.iov_len, ppos);
722 }
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700723
724 if (nr < 0) {
725 if (!ret)
726 ret = nr;
727 break;
728 }
729 ret += nr;
Al Viroac15ac02015-03-20 20:10:21 -0400730 if (nr != iovec.iov_len)
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700731 break;
Al Viroac15ac02015-03-20 20:10:21 -0400732 iov_iter_advance(iter, nr);
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700733 }
734
735 return ret;
736}
737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738/* A write operation does a read from user space and vice versa */
739#define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
740
Vegard Nossumffecee42016-10-08 11:18:07 +0200741/**
742 * rw_copy_check_uvector() - Copy an array of &struct iovec from userspace
743 * into the kernel and check that it is valid.
744 *
745 * @type: One of %CHECK_IOVEC_ONLY, %READ, or %WRITE.
746 * @uvector: Pointer to the userspace array.
747 * @nr_segs: Number of elements in userspace array.
748 * @fast_segs: Number of elements in @fast_pointer.
749 * @fast_pointer: Pointer to (usually small on-stack) kernel array.
750 * @ret_pointer: (output parameter) Pointer to a variable that will point to
751 * either @fast_pointer, a newly allocated kernel array, or NULL,
752 * depending on which array was used.
753 *
754 * This function copies an array of &struct iovec of @nr_segs from
755 * userspace into the kernel and checks that each element is valid (e.g.
756 * it does not point to a kernel address or cause overflow by being too
757 * large, etc.).
758 *
759 * As an optimization, the caller may provide a pointer to a small
760 * on-stack array in @fast_pointer, typically %UIO_FASTIOV elements long
761 * (the size of this array, or 0 if unused, should be given in @fast_segs).
762 *
763 * @ret_pointer will always point to the array that was used, so the
764 * caller must take care not to call kfree() on it e.g. in case the
765 * @fast_pointer array was used and it was allocated on the stack.
766 *
767 * Return: The total number of bytes covered by the iovec array on success
768 * or a negative error code on error.
769 */
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700770ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
771 unsigned long nr_segs, unsigned long fast_segs,
772 struct iovec *fast_pointer,
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700773 struct iovec **ret_pointer)
Linus Torvalds435f49a2010-10-29 10:36:49 -0700774{
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700775 unsigned long seg;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700776 ssize_t ret;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700777 struct iovec *iov = fast_pointer;
778
Linus Torvalds435f49a2010-10-29 10:36:49 -0700779 /*
780 * SuS says "The readv() function *may* fail if the iovcnt argument
781 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
782 * traditionally returned zero for zero segments, so...
783 */
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700784 if (nr_segs == 0) {
785 ret = 0;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700786 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700787 }
788
Linus Torvalds435f49a2010-10-29 10:36:49 -0700789 /*
790 * First get the "struct iovec" from user memory and
791 * verify all the pointers
792 */
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700793 if (nr_segs > UIO_MAXIOV) {
794 ret = -EINVAL;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700795 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700796 }
797 if (nr_segs > fast_segs) {
Linus Torvalds435f49a2010-10-29 10:36:49 -0700798 iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700799 if (iov == NULL) {
800 ret = -ENOMEM;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700801 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700802 }
Linus Torvalds435f49a2010-10-29 10:36:49 -0700803 }
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700804 if (copy_from_user(iov, uvector, nr_segs*sizeof(*uvector))) {
805 ret = -EFAULT;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700806 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700807 }
808
Linus Torvalds435f49a2010-10-29 10:36:49 -0700809 /*
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700810 * According to the Single Unix Specification we should return EINVAL
811 * if an element length is < 0 when cast to ssize_t or if the
812 * total length would overflow the ssize_t return value of the
813 * system call.
Linus Torvalds435f49a2010-10-29 10:36:49 -0700814 *
815 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
816 * overflow case.
817 */
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700818 ret = 0;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700819 for (seg = 0; seg < nr_segs; seg++) {
820 void __user *buf = iov[seg].iov_base;
821 ssize_t len = (ssize_t)iov[seg].iov_len;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700822
823 /* see if we we're about to use an invalid len or if
824 * it's about to overflow ssize_t */
Linus Torvalds435f49a2010-10-29 10:36:49 -0700825 if (len < 0) {
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700826 ret = -EINVAL;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700827 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700828 }
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700829 if (type >= 0
Christopher Yeohfcf63402011-10-31 17:06:39 -0700830 && unlikely(!access_ok(vrfy_dir(type), buf, len))) {
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700831 ret = -EFAULT;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700832 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700833 }
Linus Torvalds435f49a2010-10-29 10:36:49 -0700834 if (len > MAX_RW_COUNT - ret) {
835 len = MAX_RW_COUNT - ret;
836 iov[seg].iov_len = len;
837 }
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700838 ret += len;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700839 }
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700840out:
841 *ret_pointer = iov;
842 return ret;
843}
844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845static ssize_t do_readv_writev(int type, struct file *file,
846 const struct iovec __user * uvector,
Christoph Hellwig793b80e2016-03-03 16:03:58 +0100847 unsigned long nr_segs, loff_t *pos,
848 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 size_t tot_len;
851 struct iovec iovstack[UIO_FASTIOV];
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700852 struct iovec *iov = iovstack;
Al Viroac15ac02015-03-20 20:10:21 -0400853 struct iov_iter iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Al Viro0504c072015-03-21 19:40:11 -0400856 ret = import_iovec(type, uvector, nr_segs,
857 ARRAY_SIZE(iovstack), &iov, &iter);
858 if (ret < 0)
859 return ret;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700860
Al Viro0504c072015-03-21 19:40:11 -0400861 tot_len = iov_iter_count(&iter);
862 if (!tot_len)
863 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 ret = rw_verify_area(type, file, pos, tot_len);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800865 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 goto out;
867
Miklos Szeredi04676262017-02-20 16:51:23 +0100868 if (type != READ)
Al Viro03d95eb2013-03-20 13:04:20 -0400869 file_start_write(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Miklos Szeredi04676262017-02-20 16:51:23 +0100871 if ((type == READ && file->f_op->read_iter) ||
872 (type == WRITE && file->f_op->write_iter))
873 ret = do_iter_readv_writev(file, &iter, pos, type, flags);
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700874 else
Miklos Szeredi04676262017-02-20 16:51:23 +0100875 ret = do_loop_readv_writev(file, &iter, pos, type, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Al Viro03d95eb2013-03-20 13:04:20 -0400877 if (type != READ)
878 file_end_write(file);
879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880out:
Al Viro0504c072015-03-21 19:40:11 -0400881 kfree(iov);
Robert Love0eeca282005-07-12 17:06:03 -0400882 if ((ret + (type == READ)) > 0) {
883 if (type == READ)
Eric Paris2a12a9d2009-12-17 21:24:21 -0500884 fsnotify_access(file);
Robert Love0eeca282005-07-12 17:06:03 -0400885 else
Eric Paris2a12a9d2009-12-17 21:24:21 -0500886 fsnotify_modify(file);
Robert Love0eeca282005-07-12 17:06:03 -0400887 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889}
890
891ssize_t vfs_readv(struct file *file, const struct iovec __user *vec,
Christoph Hellwig793b80e2016-03-03 16:03:58 +0100892 unsigned long vlen, loff_t *pos, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
894 if (!(file->f_mode & FMODE_READ))
895 return -EBADF;
Al Viro7f7f25e2014-02-11 17:49:24 -0500896 if (!(file->f_mode & FMODE_CAN_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 return -EINVAL;
898
Christoph Hellwig793b80e2016-03-03 16:03:58 +0100899 return do_readv_writev(READ, file, vec, vlen, pos, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900}
901
902EXPORT_SYMBOL(vfs_readv);
903
904ssize_t vfs_writev(struct file *file, const struct iovec __user *vec,
Christoph Hellwig793b80e2016-03-03 16:03:58 +0100905 unsigned long vlen, loff_t *pos, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
907 if (!(file->f_mode & FMODE_WRITE))
908 return -EBADF;
Al Viro7f7f25e2014-02-11 17:49:24 -0500909 if (!(file->f_mode & FMODE_CAN_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 return -EINVAL;
911
Christoph Hellwig793b80e2016-03-03 16:03:58 +0100912 return do_readv_writev(WRITE, file, vec, vlen, pos, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913}
914
915EXPORT_SYMBOL(vfs_writev);
916
Milosz Tanskif17d8b32016-03-03 16:03:59 +0100917static ssize_t do_readv(unsigned long fd, const struct iovec __user *vec,
918 unsigned long vlen, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
Linus Torvalds9c225f22014-03-03 09:36:58 -0800920 struct fd f = fdget_pos(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Al Viro2903ff02012-08-28 12:52:22 -0400923 if (f.file) {
924 loff_t pos = file_pos_read(f.file);
Milosz Tanskif17d8b32016-03-03 16:03:59 +0100925 ret = vfs_readv(f.file, vec, vlen, &pos, flags);
Al Viro5faf1532013-06-15 05:49:36 +0400926 if (ret >= 0)
927 file_pos_write(f.file, pos);
Linus Torvalds9c225f22014-03-03 09:36:58 -0800928 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 }
930
931 if (ret > 0)
Alexey Dobriyan4b98d112007-02-10 01:46:45 -0800932 add_rchar(current, ret);
933 inc_syscr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 return ret;
935}
936
Milosz Tanskif17d8b32016-03-03 16:03:59 +0100937static ssize_t do_writev(unsigned long fd, const struct iovec __user *vec,
938 unsigned long vlen, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939{
Linus Torvalds9c225f22014-03-03 09:36:58 -0800940 struct fd f = fdget_pos(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Al Viro2903ff02012-08-28 12:52:22 -0400943 if (f.file) {
944 loff_t pos = file_pos_read(f.file);
Milosz Tanskif17d8b32016-03-03 16:03:59 +0100945 ret = vfs_writev(f.file, vec, vlen, &pos, flags);
Al Viro5faf1532013-06-15 05:49:36 +0400946 if (ret >= 0)
947 file_pos_write(f.file, pos);
Linus Torvalds9c225f22014-03-03 09:36:58 -0800948 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 }
950
951 if (ret > 0)
Alexey Dobriyan4b98d112007-02-10 01:46:45 -0800952 add_wchar(current, ret);
953 inc_syscw(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 return ret;
955}
956
Linus Torvalds601cc112009-04-03 08:03:22 -0700957static inline loff_t pos_from_hilo(unsigned long high, unsigned long low)
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700958{
Linus Torvalds601cc112009-04-03 08:03:22 -0700959#define HALF_LONG_BITS (BITS_PER_LONG / 2)
960 return (((loff_t)high << HALF_LONG_BITS) << HALF_LONG_BITS) | low;
961}
962
Milosz Tanskif17d8b32016-03-03 16:03:59 +0100963static ssize_t do_preadv(unsigned long fd, const struct iovec __user *vec,
964 unsigned long vlen, loff_t pos, int flags)
Linus Torvalds601cc112009-04-03 08:03:22 -0700965{
Al Viro2903ff02012-08-28 12:52:22 -0400966 struct fd f;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700967 ssize_t ret = -EBADF;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700968
969 if (pos < 0)
970 return -EINVAL;
971
Al Viro2903ff02012-08-28 12:52:22 -0400972 f = fdget(fd);
973 if (f.file) {
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700974 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -0400975 if (f.file->f_mode & FMODE_PREAD)
Milosz Tanskif17d8b32016-03-03 16:03:59 +0100976 ret = vfs_readv(f.file, vec, vlen, &pos, flags);
Al Viro2903ff02012-08-28 12:52:22 -0400977 fdput(f);
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700978 }
979
980 if (ret > 0)
981 add_rchar(current, ret);
982 inc_syscr(current);
983 return ret;
984}
985
Milosz Tanskif17d8b32016-03-03 16:03:59 +0100986static ssize_t do_pwritev(unsigned long fd, const struct iovec __user *vec,
987 unsigned long vlen, loff_t pos, int flags)
Gerd Hoffmannf3554f42009-04-02 16:59:23 -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_PWRITE)
Milosz Tanskif17d8b32016-03-03 16:03:59 +0100999 ret = vfs_writev(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_wchar(current, ret);
1005 inc_syscw(current);
1006 return ret;
1007}
1008
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001009SYSCALL_DEFINE3(readv, unsigned long, fd, const struct iovec __user *, vec,
1010 unsigned long, vlen)
1011{
1012 return do_readv(fd, vec, vlen, 0);
1013}
1014
1015SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec __user *, vec,
1016 unsigned long, vlen)
1017{
1018 return do_writev(fd, vec, vlen, 0);
1019}
1020
1021SYSCALL_DEFINE5(preadv, unsigned long, fd, const struct iovec __user *, vec,
1022 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
1023{
1024 loff_t pos = pos_from_hilo(pos_h, pos_l);
1025
1026 return do_preadv(fd, vec, vlen, pos, 0);
1027}
1028
1029SYSCALL_DEFINE6(preadv2, unsigned long, fd, const struct iovec __user *, vec,
1030 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h,
1031 int, flags)
1032{
1033 loff_t pos = pos_from_hilo(pos_h, pos_l);
1034
1035 if (pos == -1)
1036 return do_readv(fd, vec, vlen, flags);
1037
1038 return do_preadv(fd, vec, vlen, pos, flags);
1039}
1040
1041SYSCALL_DEFINE5(pwritev, unsigned long, fd, const struct iovec __user *, vec,
1042 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
1043{
1044 loff_t pos = pos_from_hilo(pos_h, pos_l);
1045
1046 return do_pwritev(fd, vec, vlen, pos, 0);
1047}
1048
1049SYSCALL_DEFINE6(pwritev2, unsigned long, fd, const struct iovec __user *, vec,
1050 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h,
1051 int, flags)
1052{
1053 loff_t pos = pos_from_hilo(pos_h, pos_l);
1054
1055 if (pos == -1)
1056 return do_writev(fd, vec, vlen, flags);
1057
1058 return do_pwritev(fd, vec, vlen, pos, flags);
1059}
1060
Al Viro72ec3512013-03-20 10:42:10 -04001061#ifdef CONFIG_COMPAT
1062
1063static ssize_t compat_do_readv_writev(int type, struct file *file,
1064 const struct compat_iovec __user *uvector,
Christoph Hellwig793b80e2016-03-03 16:03:58 +01001065 unsigned long nr_segs, loff_t *pos,
1066 int flags)
Al Viro72ec3512013-03-20 10:42:10 -04001067{
1068 compat_ssize_t tot_len;
1069 struct iovec iovstack[UIO_FASTIOV];
1070 struct iovec *iov = iovstack;
Al Viroac15ac02015-03-20 20:10:21 -04001071 struct iov_iter iter;
Al Viro72ec3512013-03-20 10:42:10 -04001072 ssize_t ret;
Al Viro72ec3512013-03-20 10:42:10 -04001073
Al Viro0504c072015-03-21 19:40:11 -04001074 ret = compat_import_iovec(type, uvector, nr_segs,
1075 UIO_FASTIOV, &iov, &iter);
1076 if (ret < 0)
1077 return ret;
Al Viro72ec3512013-03-20 10:42:10 -04001078
Al Viro0504c072015-03-21 19:40:11 -04001079 tot_len = iov_iter_count(&iter);
1080 if (!tot_len)
1081 goto out;
Al Viro72ec3512013-03-20 10:42:10 -04001082 ret = rw_verify_area(type, file, pos, tot_len);
1083 if (ret < 0)
1084 goto out;
1085
Miklos Szeredi04676262017-02-20 16:51:23 +01001086 if (type != READ)
Al Viro03d95eb2013-03-20 13:04:20 -04001087 file_start_write(file);
Al Viro72ec3512013-03-20 10:42:10 -04001088
Miklos Szeredi04676262017-02-20 16:51:23 +01001089 if ((type == READ && file->f_op->read_iter) ||
1090 (type == WRITE && file->f_op->write_iter))
1091 ret = do_iter_readv_writev(file, &iter, pos, type, flags);
Al Viro03d95eb2013-03-20 13:04:20 -04001092 else
Miklos Szeredi04676262017-02-20 16:51:23 +01001093 ret = do_loop_readv_writev(file, &iter, pos, type, flags);
Al Viro72ec3512013-03-20 10:42:10 -04001094
Al Viro03d95eb2013-03-20 13:04:20 -04001095 if (type != READ)
1096 file_end_write(file);
1097
Al Viro72ec3512013-03-20 10:42:10 -04001098out:
Al Viro0504c072015-03-21 19:40:11 -04001099 kfree(iov);
Al Viro72ec3512013-03-20 10:42:10 -04001100 if ((ret + (type == READ)) > 0) {
1101 if (type == READ)
1102 fsnotify_access(file);
1103 else
1104 fsnotify_modify(file);
1105 }
1106 return ret;
1107}
1108
1109static size_t compat_readv(struct file *file,
1110 const struct compat_iovec __user *vec,
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001111 unsigned long vlen, loff_t *pos, int flags)
Al Viro72ec3512013-03-20 10:42:10 -04001112{
1113 ssize_t ret = -EBADF;
1114
1115 if (!(file->f_mode & FMODE_READ))
1116 goto out;
1117
1118 ret = -EINVAL;
Al Viro7f7f25e2014-02-11 17:49:24 -05001119 if (!(file->f_mode & FMODE_CAN_READ))
Al Viro72ec3512013-03-20 10:42:10 -04001120 goto out;
1121
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001122 ret = compat_do_readv_writev(READ, file, vec, vlen, pos, flags);
Al Viro72ec3512013-03-20 10:42:10 -04001123
1124out:
1125 if (ret > 0)
1126 add_rchar(current, ret);
1127 inc_syscr(current);
1128 return ret;
1129}
1130
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001131static size_t do_compat_readv(compat_ulong_t fd,
1132 const struct compat_iovec __user *vec,
1133 compat_ulong_t vlen, int flags)
Al Viro72ec3512013-03-20 10:42:10 -04001134{
Linus Torvalds9c225f22014-03-03 09:36:58 -08001135 struct fd f = fdget_pos(fd);
Al Viro72ec3512013-03-20 10:42:10 -04001136 ssize_t ret;
1137 loff_t pos;
1138
1139 if (!f.file)
1140 return -EBADF;
1141 pos = f.file->f_pos;
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001142 ret = compat_readv(f.file, vec, vlen, &pos, flags);
Al Viro5faf1532013-06-15 05:49:36 +04001143 if (ret >= 0)
1144 f.file->f_pos = pos;
Linus Torvalds9c225f22014-03-03 09:36:58 -08001145 fdput_pos(f);
Al Viro72ec3512013-03-20 10:42:10 -04001146 return ret;
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001147
Al Viro72ec3512013-03-20 10:42:10 -04001148}
1149
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001150COMPAT_SYSCALL_DEFINE3(readv, compat_ulong_t, fd,
1151 const struct compat_iovec __user *,vec,
1152 compat_ulong_t, vlen)
1153{
1154 return do_compat_readv(fd, vec, vlen, 0);
1155}
1156
1157static long do_compat_preadv64(unsigned long fd,
Heiko Carstens378a10f2014-03-05 10:43:51 +01001158 const struct compat_iovec __user *vec,
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001159 unsigned long vlen, loff_t pos, int flags)
Al Viro72ec3512013-03-20 10:42:10 -04001160{
1161 struct fd f;
1162 ssize_t ret;
1163
1164 if (pos < 0)
1165 return -EINVAL;
1166 f = fdget(fd);
1167 if (!f.file)
1168 return -EBADF;
1169 ret = -ESPIPE;
1170 if (f.file->f_mode & FMODE_PREAD)
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001171 ret = compat_readv(f.file, vec, vlen, &pos, flags);
Al Viro72ec3512013-03-20 10:42:10 -04001172 fdput(f);
1173 return ret;
1174}
1175
Heiko Carstens378a10f2014-03-05 10:43:51 +01001176#ifdef __ARCH_WANT_COMPAT_SYS_PREADV64
1177COMPAT_SYSCALL_DEFINE4(preadv64, unsigned long, fd,
1178 const struct compat_iovec __user *,vec,
1179 unsigned long, vlen, loff_t, pos)
1180{
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001181 return do_compat_preadv64(fd, vec, vlen, pos, 0);
Heiko Carstens378a10f2014-03-05 10:43:51 +01001182}
1183#endif
1184
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001185COMPAT_SYSCALL_DEFINE5(preadv, compat_ulong_t, fd,
Al Viro72ec3512013-03-20 10:42:10 -04001186 const struct compat_iovec __user *,vec,
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001187 compat_ulong_t, vlen, u32, pos_low, u32, pos_high)
Al Viro72ec3512013-03-20 10:42:10 -04001188{
1189 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
Heiko Carstens378a10f2014-03-05 10:43:51 +01001190
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001191 return do_compat_preadv64(fd, vec, vlen, pos, 0);
1192}
1193
H.J. Lu3ebfd812016-07-14 12:31:53 -07001194#ifdef __ARCH_WANT_COMPAT_SYS_PREADV64V2
1195COMPAT_SYSCALL_DEFINE5(preadv64v2, unsigned long, fd,
1196 const struct compat_iovec __user *,vec,
1197 unsigned long, vlen, loff_t, pos, int, flags)
1198{
1199 return do_compat_preadv64(fd, vec, vlen, pos, flags);
1200}
1201#endif
1202
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001203COMPAT_SYSCALL_DEFINE6(preadv2, compat_ulong_t, fd,
1204 const struct compat_iovec __user *,vec,
1205 compat_ulong_t, vlen, u32, pos_low, u32, pos_high,
1206 int, flags)
1207{
1208 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
1209
1210 if (pos == -1)
1211 return do_compat_readv(fd, vec, vlen, flags);
1212
1213 return do_compat_preadv64(fd, vec, vlen, pos, flags);
Al Viro72ec3512013-03-20 10:42:10 -04001214}
1215
1216static size_t compat_writev(struct file *file,
1217 const struct compat_iovec __user *vec,
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001218 unsigned long vlen, loff_t *pos, int flags)
Al Viro72ec3512013-03-20 10:42:10 -04001219{
1220 ssize_t ret = -EBADF;
1221
1222 if (!(file->f_mode & FMODE_WRITE))
1223 goto out;
1224
1225 ret = -EINVAL;
Al Viro7f7f25e2014-02-11 17:49:24 -05001226 if (!(file->f_mode & FMODE_CAN_WRITE))
Al Viro72ec3512013-03-20 10:42:10 -04001227 goto out;
1228
Christoph Hellwig222aa342017-06-16 11:08:24 +02001229 ret = compat_do_readv_writev(WRITE, file, vec, vlen, pos, flags);
Al Viro72ec3512013-03-20 10:42:10 -04001230
1231out:
1232 if (ret > 0)
1233 add_wchar(current, ret);
1234 inc_syscw(current);
1235 return ret;
1236}
1237
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001238static size_t do_compat_writev(compat_ulong_t fd,
1239 const struct compat_iovec __user* vec,
1240 compat_ulong_t vlen, int flags)
Al Viro72ec3512013-03-20 10:42:10 -04001241{
Linus Torvalds9c225f22014-03-03 09:36:58 -08001242 struct fd f = fdget_pos(fd);
Al Viro72ec3512013-03-20 10:42:10 -04001243 ssize_t ret;
1244 loff_t pos;
1245
1246 if (!f.file)
1247 return -EBADF;
1248 pos = f.file->f_pos;
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001249 ret = compat_writev(f.file, vec, vlen, &pos, flags);
Al Viro5faf1532013-06-15 05:49:36 +04001250 if (ret >= 0)
1251 f.file->f_pos = pos;
Linus Torvalds9c225f22014-03-03 09:36:58 -08001252 fdput_pos(f);
Al Viro72ec3512013-03-20 10:42:10 -04001253 return ret;
1254}
1255
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001256COMPAT_SYSCALL_DEFINE3(writev, compat_ulong_t, fd,
1257 const struct compat_iovec __user *, vec,
1258 compat_ulong_t, vlen)
1259{
1260 return do_compat_writev(fd, vec, vlen, 0);
1261}
1262
1263static long do_compat_pwritev64(unsigned long fd,
Heiko Carstens378a10f2014-03-05 10:43:51 +01001264 const struct compat_iovec __user *vec,
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001265 unsigned long vlen, loff_t pos, int flags)
Al Viro72ec3512013-03-20 10:42:10 -04001266{
1267 struct fd f;
1268 ssize_t ret;
1269
1270 if (pos < 0)
1271 return -EINVAL;
1272 f = fdget(fd);
1273 if (!f.file)
1274 return -EBADF;
1275 ret = -ESPIPE;
1276 if (f.file->f_mode & FMODE_PWRITE)
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001277 ret = compat_writev(f.file, vec, vlen, &pos, flags);
Al Viro72ec3512013-03-20 10:42:10 -04001278 fdput(f);
1279 return ret;
1280}
1281
Heiko Carstens378a10f2014-03-05 10:43:51 +01001282#ifdef __ARCH_WANT_COMPAT_SYS_PWRITEV64
1283COMPAT_SYSCALL_DEFINE4(pwritev64, unsigned long, fd,
1284 const struct compat_iovec __user *,vec,
1285 unsigned long, vlen, loff_t, pos)
1286{
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001287 return do_compat_pwritev64(fd, vec, vlen, pos, 0);
Heiko Carstens378a10f2014-03-05 10:43:51 +01001288}
1289#endif
1290
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001291COMPAT_SYSCALL_DEFINE5(pwritev, compat_ulong_t, fd,
Al Viro72ec3512013-03-20 10:42:10 -04001292 const struct compat_iovec __user *,vec,
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001293 compat_ulong_t, vlen, u32, pos_low, u32, pos_high)
Al Viro72ec3512013-03-20 10:42:10 -04001294{
1295 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
Heiko Carstens378a10f2014-03-05 10:43:51 +01001296
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001297 return do_compat_pwritev64(fd, vec, vlen, pos, 0);
Al Viro72ec3512013-03-20 10:42:10 -04001298}
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001299
H.J. Lu3ebfd812016-07-14 12:31:53 -07001300#ifdef __ARCH_WANT_COMPAT_SYS_PWRITEV64V2
1301COMPAT_SYSCALL_DEFINE5(pwritev64v2, unsigned long, fd,
1302 const struct compat_iovec __user *,vec,
1303 unsigned long, vlen, loff_t, pos, int, flags)
1304{
1305 return do_compat_pwritev64(fd, vec, vlen, pos, flags);
1306}
1307#endif
1308
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001309COMPAT_SYSCALL_DEFINE6(pwritev2, compat_ulong_t, fd,
1310 const struct compat_iovec __user *,vec,
1311 compat_ulong_t, vlen, u32, pos_low, u32, pos_high, int, flags)
1312{
1313 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
1314
1315 if (pos == -1)
1316 return do_compat_writev(fd, vec, vlen, flags);
1317
1318 return do_compat_pwritev64(fd, vec, vlen, pos, flags);
1319}
1320
Al Viro72ec3512013-03-20 10:42:10 -04001321#endif
1322
Al Viro19f4fc32013-02-24 02:17:03 -05001323static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,
1324 size_t count, loff_t max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325{
Al Viro2903ff02012-08-28 12:52:22 -04001326 struct fd in, out;
1327 struct inode *in_inode, *out_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 loff_t pos;
Al Viro7995bd22013-06-20 18:58:36 +04001329 loff_t out_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 ssize_t retval;
Al Viro2903ff02012-08-28 12:52:22 -04001331 int fl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
1333 /*
1334 * Get input file, and verify that it is ok..
1335 */
1336 retval = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -04001337 in = fdget(in_fd);
1338 if (!in.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 goto out;
Al Viro2903ff02012-08-28 12:52:22 -04001340 if (!(in.file->f_mode & FMODE_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 goto fput_in;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 retval = -ESPIPE;
Al Viro7995bd22013-06-20 18:58:36 +04001343 if (!ppos) {
1344 pos = in.file->f_pos;
1345 } else {
1346 pos = *ppos;
Al Viro2903ff02012-08-28 12:52:22 -04001347 if (!(in.file->f_mode & FMODE_PREAD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 goto fput_in;
Al Viro7995bd22013-06-20 18:58:36 +04001349 }
1350 retval = rw_verify_area(READ, in.file, &pos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -08001351 if (retval < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 goto fput_in;
Al Virobc613842016-03-31 21:48:20 -04001353 if (count > MAX_RW_COUNT)
1354 count = MAX_RW_COUNT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 /*
1357 * Get output file, and verify that it is ok..
1358 */
1359 retval = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -04001360 out = fdget(out_fd);
1361 if (!out.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 goto fput_in;
Al Viro2903ff02012-08-28 12:52:22 -04001363 if (!(out.file->f_mode & FMODE_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 goto fput_out;
1365 retval = -EINVAL;
Al Viro496ad9a2013-01-23 17:07:38 -05001366 in_inode = file_inode(in.file);
1367 out_inode = file_inode(out.file);
Al Viro7995bd22013-06-20 18:58:36 +04001368 out_pos = out.file->f_pos;
1369 retval = rw_verify_area(WRITE, out.file, &out_pos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -08001370 if (retval < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 goto fput_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 if (!max)
1374 max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes);
1375
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 if (unlikely(pos + count > max)) {
1377 retval = -EOVERFLOW;
1378 if (pos >= max)
1379 goto fput_out;
1380 count = max - pos;
1381 }
1382
Jens Axboed96e6e72007-06-11 12:18:52 +02001383 fl = 0;
Jens Axboe534f2aa2007-06-01 14:52:37 +02001384#if 0
Jens Axboed96e6e72007-06-11 12:18:52 +02001385 /*
1386 * We need to debate whether we can enable this or not. The
1387 * man page documents EAGAIN return for the output at least,
1388 * and the application is arguably buggy if it doesn't expect
1389 * EAGAIN on a non-blocking file descriptor.
1390 */
Al Viro2903ff02012-08-28 12:52:22 -04001391 if (in.file->f_flags & O_NONBLOCK)
Jens Axboed96e6e72007-06-11 12:18:52 +02001392 fl = SPLICE_F_NONBLOCK;
Jens Axboe534f2aa2007-06-01 14:52:37 +02001393#endif
Al Viro50cd2c52013-05-23 20:10:34 -04001394 file_start_write(out.file);
Al Viro7995bd22013-06-20 18:58:36 +04001395 retval = do_splice_direct(in.file, &pos, out.file, &out_pos, count, fl);
Al Viro50cd2c52013-05-23 20:10:34 -04001396 file_end_write(out.file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
1398 if (retval > 0) {
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08001399 add_rchar(current, retval);
1400 add_wchar(current, retval);
Scott Wolchoka68c2f12012-12-20 15:05:52 -08001401 fsnotify_access(in.file);
1402 fsnotify_modify(out.file);
Al Viro7995bd22013-06-20 18:58:36 +04001403 out.file->f_pos = out_pos;
1404 if (ppos)
1405 *ppos = pos;
1406 else
1407 in.file->f_pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08001410 inc_syscr(current);
1411 inc_syscw(current);
Al Viro7995bd22013-06-20 18:58:36 +04001412 if (pos > max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 retval = -EOVERFLOW;
1414
1415fput_out:
Al Viro2903ff02012-08-28 12:52:22 -04001416 fdput(out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417fput_in:
Al Viro2903ff02012-08-28 12:52:22 -04001418 fdput(in);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419out:
1420 return retval;
1421}
1422
Heiko Carstens002c8972009-01-14 14:14:18 +01001423SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd, off_t __user *, offset, size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424{
1425 loff_t pos;
1426 off_t off;
1427 ssize_t ret;
1428
1429 if (offset) {
1430 if (unlikely(get_user(off, offset)))
1431 return -EFAULT;
1432 pos = off;
1433 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
1434 if (unlikely(put_user(pos, offset)))
1435 return -EFAULT;
1436 return ret;
1437 }
1438
1439 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1440}
1441
Heiko Carstens002c8972009-01-14 14:14:18 +01001442SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd, loff_t __user *, offset, size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443{
1444 loff_t pos;
1445 ssize_t ret;
1446
1447 if (offset) {
1448 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
1449 return -EFAULT;
1450 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
1451 if (unlikely(put_user(pos, offset)))
1452 return -EFAULT;
1453 return ret;
1454 }
1455
1456 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1457}
Al Viro19f4fc32013-02-24 02:17:03 -05001458
1459#ifdef CONFIG_COMPAT
1460COMPAT_SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd,
1461 compat_off_t __user *, offset, compat_size_t, count)
1462{
1463 loff_t pos;
1464 off_t off;
1465 ssize_t ret;
1466
1467 if (offset) {
1468 if (unlikely(get_user(off, offset)))
1469 return -EFAULT;
1470 pos = off;
1471 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
1472 if (unlikely(put_user(pos, offset)))
1473 return -EFAULT;
1474 return ret;
1475 }
1476
1477 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1478}
1479
1480COMPAT_SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd,
1481 compat_loff_t __user *, offset, compat_size_t, count)
1482{
1483 loff_t pos;
1484 ssize_t ret;
1485
1486 if (offset) {
1487 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
1488 return -EFAULT;
1489 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
1490 if (unlikely(put_user(pos, offset)))
1491 return -EFAULT;
1492 return ret;
1493 }
1494
1495 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1496}
1497#endif
Zach Brown29732932015-11-10 16:53:30 -05001498
1499/*
1500 * copy_file_range() differs from regular file read and write in that it
1501 * specifically allows return partial success. When it does so is up to
1502 * the copy_file_range method.
1503 */
1504ssize_t vfs_copy_file_range(struct file *file_in, loff_t pos_in,
1505 struct file *file_out, loff_t pos_out,
1506 size_t len, unsigned int flags)
1507{
1508 struct inode *inode_in = file_inode(file_in);
1509 struct inode *inode_out = file_inode(file_out);
1510 ssize_t ret;
1511
1512 if (flags != 0)
1513 return -EINVAL;
1514
Amir Goldstein27db1f02017-01-31 10:34:56 +02001515 if (S_ISDIR(inode_in->i_mode) || S_ISDIR(inode_out->i_mode))
1516 return -EISDIR;
1517 if (!S_ISREG(inode_in->i_mode) || !S_ISREG(inode_out->i_mode))
1518 return -EINVAL;
1519
Zach Brown29732932015-11-10 16:53:30 -05001520 ret = rw_verify_area(READ, file_in, &pos_in, len);
Al Virobc613842016-03-31 21:48:20 -04001521 if (unlikely(ret))
1522 return ret;
1523
1524 ret = rw_verify_area(WRITE, file_out, &pos_out, len);
1525 if (unlikely(ret))
Zach Brown29732932015-11-10 16:53:30 -05001526 return ret;
1527
1528 if (!(file_in->f_mode & FMODE_READ) ||
1529 !(file_out->f_mode & FMODE_WRITE) ||
Anna Schumakereac70052015-11-10 16:53:33 -05001530 (file_out->f_flags & O_APPEND))
Zach Brown29732932015-11-10 16:53:30 -05001531 return -EBADF;
1532
1533 /* this could be relaxed once a method supports cross-fs copies */
1534 if (inode_in->i_sb != inode_out->i_sb)
1535 return -EXDEV;
1536
1537 if (len == 0)
1538 return 0;
1539
1540 ret = mnt_want_write_file(file_out);
1541 if (ret)
1542 return ret;
1543
Anna Schumakereac70052015-11-10 16:53:33 -05001544 ret = -EOPNOTSUPP;
1545 if (file_out->f_op->copy_file_range)
1546 ret = file_out->f_op->copy_file_range(file_in, pos_in, file_out,
1547 pos_out, len, flags);
1548 if (ret == -EOPNOTSUPP)
1549 ret = do_splice_direct(file_in, &pos_in, file_out, &pos_out,
1550 len > MAX_RW_COUNT ? MAX_RW_COUNT : len, 0);
1551
Zach Brown29732932015-11-10 16:53:30 -05001552 if (ret > 0) {
1553 fsnotify_access(file_in);
1554 add_rchar(current, ret);
1555 fsnotify_modify(file_out);
1556 add_wchar(current, ret);
1557 }
1558 inc_syscr(current);
1559 inc_syscw(current);
1560
1561 mnt_drop_write_file(file_out);
1562
1563 return ret;
1564}
1565EXPORT_SYMBOL(vfs_copy_file_range);
1566
1567SYSCALL_DEFINE6(copy_file_range, int, fd_in, loff_t __user *, off_in,
1568 int, fd_out, loff_t __user *, off_out,
1569 size_t, len, unsigned int, flags)
1570{
1571 loff_t pos_in;
1572 loff_t pos_out;
1573 struct fd f_in;
1574 struct fd f_out;
1575 ssize_t ret = -EBADF;
1576
1577 f_in = fdget(fd_in);
1578 if (!f_in.file)
1579 goto out2;
1580
1581 f_out = fdget(fd_out);
1582 if (!f_out.file)
1583 goto out1;
1584
1585 ret = -EFAULT;
1586 if (off_in) {
1587 if (copy_from_user(&pos_in, off_in, sizeof(loff_t)))
1588 goto out;
1589 } else {
1590 pos_in = f_in.file->f_pos;
1591 }
1592
1593 if (off_out) {
1594 if (copy_from_user(&pos_out, off_out, sizeof(loff_t)))
1595 goto out;
1596 } else {
1597 pos_out = f_out.file->f_pos;
1598 }
1599
1600 ret = vfs_copy_file_range(f_in.file, pos_in, f_out.file, pos_out, len,
1601 flags);
1602 if (ret > 0) {
1603 pos_in += ret;
1604 pos_out += ret;
1605
1606 if (off_in) {
1607 if (copy_to_user(off_in, &pos_in, sizeof(loff_t)))
1608 ret = -EFAULT;
1609 } else {
1610 f_in.file->f_pos = pos_in;
1611 }
1612
1613 if (off_out) {
1614 if (copy_to_user(off_out, &pos_out, sizeof(loff_t)))
1615 ret = -EFAULT;
1616 } else {
1617 f_out.file->f_pos = pos_out;
1618 }
1619 }
1620
1621out:
1622 fdput(f_out);
1623out1:
1624 fdput(f_in);
1625out2:
1626 return ret;
1627}
Christoph Hellwig04b38d62015-12-03 12:59:50 +01001628
1629static int clone_verify_area(struct file *file, loff_t pos, u64 len, bool write)
1630{
1631 struct inode *inode = file_inode(file);
1632
1633 if (unlikely(pos < 0))
1634 return -EINVAL;
1635
1636 if (unlikely((loff_t) (pos + len) < 0))
1637 return -EINVAL;
1638
1639 if (unlikely(inode->i_flctx && mandatory_lock(inode))) {
1640 loff_t end = len ? pos + len - 1 : OFFSET_MAX;
1641 int retval;
1642
1643 retval = locks_mandatory_area(inode, file, pos, end,
1644 write ? F_WRLCK : F_RDLCK);
1645 if (retval < 0)
1646 return retval;
1647 }
1648
1649 return security_file_permission(file, write ? MAY_WRITE : MAY_READ);
1650}
1651
1652int vfs_clone_file_range(struct file *file_in, loff_t pos_in,
1653 struct file *file_out, loff_t pos_out, u64 len)
1654{
1655 struct inode *inode_in = file_inode(file_in);
1656 struct inode *inode_out = file_inode(file_out);
1657 int ret;
1658
1659 if (inode_in->i_sb != inode_out->i_sb ||
1660 file_in->f_path.mnt != file_out->f_path.mnt)
1661 return -EXDEV;
1662
1663 if (S_ISDIR(inode_in->i_mode) || S_ISDIR(inode_out->i_mode))
1664 return -EISDIR;
1665 if (!S_ISREG(inode_in->i_mode) || !S_ISREG(inode_out->i_mode))
Darrick J. Wongd79bdd52015-12-19 00:55:52 -08001666 return -EINVAL;
Christoph Hellwig04b38d62015-12-03 12:59:50 +01001667
1668 if (!(file_in->f_mode & FMODE_READ) ||
1669 !(file_out->f_mode & FMODE_WRITE) ||
Christoph Hellwig0fcbf992016-02-26 18:53:12 +01001670 (file_out->f_flags & O_APPEND))
Christoph Hellwig04b38d62015-12-03 12:59:50 +01001671 return -EBADF;
1672
Christoph Hellwig0fcbf992016-02-26 18:53:12 +01001673 if (!file_in->f_op->clone_file_range)
1674 return -EOPNOTSUPP;
1675
Christoph Hellwig04b38d62015-12-03 12:59:50 +01001676 ret = clone_verify_area(file_in, pos_in, len, false);
1677 if (ret)
1678 return ret;
1679
1680 ret = clone_verify_area(file_out, pos_out, len, true);
1681 if (ret)
1682 return ret;
1683
1684 if (pos_in + len > i_size_read(inode_in))
1685 return -EINVAL;
1686
1687 ret = mnt_want_write_file(file_out);
1688 if (ret)
1689 return ret;
1690
1691 ret = file_in->f_op->clone_file_range(file_in, pos_in,
1692 file_out, pos_out, len);
1693 if (!ret) {
1694 fsnotify_access(file_in);
1695 fsnotify_modify(file_out);
1696 }
1697
1698 mnt_drop_write_file(file_out);
1699 return ret;
1700}
1701EXPORT_SYMBOL(vfs_clone_file_range);
Darrick J. Wong54dbc152015-12-19 00:55:59 -08001702
1703int vfs_dedupe_file_range(struct file *file, struct file_dedupe_range *same)
1704{
1705 struct file_dedupe_range_info *info;
1706 struct inode *src = file_inode(file);
1707 u64 off;
1708 u64 len;
1709 int i;
1710 int ret;
1711 bool is_admin = capable(CAP_SYS_ADMIN);
1712 u16 count = same->dest_count;
1713 struct file *dst_file;
1714 loff_t dst_off;
1715 ssize_t deduped;
1716
1717 if (!(file->f_mode & FMODE_READ))
1718 return -EINVAL;
1719
1720 if (same->reserved1 || same->reserved2)
1721 return -EINVAL;
1722
1723 off = same->src_offset;
1724 len = same->src_length;
1725
1726 ret = -EISDIR;
1727 if (S_ISDIR(src->i_mode))
1728 goto out;
1729
1730 ret = -EINVAL;
1731 if (!S_ISREG(src->i_mode))
1732 goto out;
1733
1734 ret = clone_verify_area(file, off, len, false);
1735 if (ret < 0)
1736 goto out;
1737 ret = 0;
1738
1739 /* pre-format output fields to sane values */
1740 for (i = 0; i < count; i++) {
1741 same->info[i].bytes_deduped = 0ULL;
1742 same->info[i].status = FILE_DEDUPE_RANGE_SAME;
1743 }
1744
1745 for (i = 0, info = same->info; i < count; i++, info++) {
1746 struct inode *dst;
1747 struct fd dst_fd = fdget(info->dest_fd);
1748
1749 dst_file = dst_fd.file;
1750 if (!dst_file) {
1751 info->status = -EBADF;
1752 goto next_loop;
1753 }
1754 dst = file_inode(dst_file);
1755
1756 ret = mnt_want_write_file(dst_file);
1757 if (ret) {
1758 info->status = ret;
1759 goto next_loop;
1760 }
1761
1762 dst_off = info->dest_offset;
1763 ret = clone_verify_area(dst_file, dst_off, len, true);
1764 if (ret < 0) {
1765 info->status = ret;
1766 goto next_file;
1767 }
1768 ret = 0;
1769
1770 if (info->reserved) {
1771 info->status = -EINVAL;
1772 } else if (!(is_admin || (dst_file->f_mode & FMODE_WRITE))) {
1773 info->status = -EINVAL;
1774 } else if (file->f_path.mnt != dst_file->f_path.mnt) {
1775 info->status = -EXDEV;
1776 } else if (S_ISDIR(dst->i_mode)) {
1777 info->status = -EISDIR;
1778 } else if (dst_file->f_op->dedupe_file_range == NULL) {
1779 info->status = -EINVAL;
1780 } else {
1781 deduped = dst_file->f_op->dedupe_file_range(file, off,
1782 len, dst_file,
1783 info->dest_offset);
1784 if (deduped == -EBADE)
1785 info->status = FILE_DEDUPE_RANGE_DIFFERS;
1786 else if (deduped < 0)
1787 info->status = deduped;
1788 else
1789 info->bytes_deduped += deduped;
1790 }
1791
1792next_file:
1793 mnt_drop_write_file(dst_file);
1794next_loop:
1795 fdput(dst_fd);
Darrick J. Wonge62e5602016-01-22 16:58:28 -08001796
1797 if (fatal_signal_pending(current))
1798 goto out;
Darrick J. Wong54dbc152015-12-19 00:55:59 -08001799 }
1800
1801out:
1802 return ret;
1803}
1804EXPORT_SYMBOL(vfs_dedupe_file_range);