blob: 2f1027d708d27d3311f94ee43c2123a086b7ecbf [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);
Christoph Hellwigdbe4e192015-01-25 21:11:59 +0100392 if (ret > 0)
393 *ppos = kiocb.ki_pos;
394 return ret;
395}
396EXPORT_SYMBOL(vfs_iter_write);
397
Al Viro68d70d02013-06-19 15:26:04 +0400398int rw_verify_area(int read_write, struct file *file, const loff_t *ppos, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
400 struct inode *inode;
401 loff_t pos;
James Morrisc43e2592008-01-12 22:05:48 +1100402 int retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Al Viro496ad9a2013-01-23 17:07:38 -0500404 inode = file_inode(file);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800405 if (unlikely((ssize_t) count < 0))
James Morrisc43e2592008-01-12 22:05:48 +1100406 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 pos = *ppos;
Al Virocccb5a12010-12-17 07:44:05 -0500408 if (unlikely(pos < 0)) {
409 if (!unsigned_offsets(file))
410 return retval;
411 if (count >= -pos) /* both values are in 0..LLONG_MAX */
412 return -EOVERFLOW;
413 } else if (unlikely((loff_t) (pos + count) < 0)) {
414 if (!unsigned_offsets(file))
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -0700415 return retval;
416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Jeff Laytonbd61e0a2015-01-16 15:05:55 -0500418 if (unlikely(inode->i_flctx && mandatory_lock(inode))) {
Christoph Hellwigacc15572015-12-03 12:59:49 +0100419 retval = locks_mandatory_area(inode, file, pos, pos + count - 1,
420 read_write == READ ? F_RDLCK : F_WRLCK);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800421 if (retval < 0)
422 return retval;
423 }
Al Virobc613842016-03-31 21:48:20 -0400424 return security_file_permission(file,
James Morrisc43e2592008-01-12 22:05:48 +1100425 read_write == READ ? MAY_READ : MAY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426}
427
Al Viro5d5d5682015-04-03 15:41:18 -0400428static ssize_t new_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
Al Viro293bc982014-02-11 18:37:41 -0500429{
430 struct iovec iov = { .iov_base = buf, .iov_len = len };
431 struct kiocb kiocb;
432 struct iov_iter iter;
433 ssize_t ret;
434
435 init_sync_kiocb(&kiocb, filp);
436 kiocb.ki_pos = *ppos;
Al Viro293bc982014-02-11 18:37:41 -0500437 iov_iter_init(&iter, READ, &iov, 1, len);
438
439 ret = filp->f_op->read_iter(&kiocb, &iter);
Christoph Hellwig599bd192015-02-11 19:59:44 +0100440 BUG_ON(ret == -EIOCBQUEUED);
Al Viro293bc982014-02-11 18:37:41 -0500441 *ppos = kiocb.ki_pos;
442 return ret;
443}
444
Dmitry Kasatkin6fb50322014-11-05 17:01:17 +0200445ssize_t __vfs_read(struct file *file, char __user *buf, size_t count,
446 loff_t *pos)
447{
Dmitry Kasatkin6fb50322014-11-05 17:01:17 +0200448 if (file->f_op->read)
Al Viro3d04c8a2015-04-03 15:09:18 -0400449 return file->f_op->read(file, buf, count, pos);
Dmitry Kasatkin6fb50322014-11-05 17:01:17 +0200450 else if (file->f_op->read_iter)
Al Viro3d04c8a2015-04-03 15:09:18 -0400451 return new_sync_read(file, buf, count, pos);
Dmitry Kasatkin6fb50322014-11-05 17:01:17 +0200452 else
Al Viro3d04c8a2015-04-03 15:09:18 -0400453 return -EINVAL;
Dmitry Kasatkin6fb50322014-11-05 17:01:17 +0200454}
Al Viro3d04c8a2015-04-03 15:09:18 -0400455EXPORT_SYMBOL(__vfs_read);
Dmitry Kasatkin6fb50322014-11-05 17:01:17 +0200456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
458{
459 ssize_t ret;
460
461 if (!(file->f_mode & FMODE_READ))
462 return -EBADF;
Al Viro7f7f25e2014-02-11 17:49:24 -0500463 if (!(file->f_mode & FMODE_CAN_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 return -EINVAL;
465 if (unlikely(!access_ok(VERIFY_WRITE, buf, count)))
466 return -EFAULT;
467
468 ret = rw_verify_area(READ, file, pos, count);
Al Virobc613842016-03-31 21:48:20 -0400469 if (!ret) {
470 if (count > MAX_RW_COUNT)
471 count = MAX_RW_COUNT;
Dmitry Kasatkin6fb50322014-11-05 17:01:17 +0200472 ret = __vfs_read(file, buf, count, pos);
James Morrisc43e2592008-01-12 22:05:48 +1100473 if (ret > 0) {
Eric Paris2a12a9d2009-12-17 21:24:21 -0500474 fsnotify_access(file);
James Morrisc43e2592008-01-12 22:05:48 +1100475 add_rchar(current, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 }
James Morrisc43e2592008-01-12 22:05:48 +1100477 inc_syscr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 }
479
480 return ret;
481}
482
483EXPORT_SYMBOL(vfs_read);
484
Al Viro5d5d5682015-04-03 15:41:18 -0400485static 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 -0500486{
487 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len };
488 struct kiocb kiocb;
489 struct iov_iter iter;
490 ssize_t ret;
491
492 init_sync_kiocb(&kiocb, filp);
493 kiocb.ki_pos = *ppos;
Al Viro293bc982014-02-11 18:37:41 -0500494 iov_iter_init(&iter, WRITE, &iov, 1, len);
495
496 ret = filp->f_op->write_iter(&kiocb, &iter);
Christoph Hellwig599bd192015-02-11 19:59:44 +0100497 BUG_ON(ret == -EIOCBQUEUED);
Al Virof765b132015-04-06 20:50:38 -0400498 if (ret > 0)
499 *ppos = kiocb.ki_pos;
Al Viro293bc982014-02-11 18:37:41 -0500500 return ret;
501}
502
Al Viro493c84c2015-04-03 15:06:43 -0400503ssize_t __vfs_write(struct file *file, const char __user *p, size_t count,
504 loff_t *pos)
505{
506 if (file->f_op->write)
507 return file->f_op->write(file, p, count, pos);
Al Viro493c84c2015-04-03 15:06:43 -0400508 else if (file->f_op->write_iter)
509 return new_sync_write(file, p, count, pos);
510 else
511 return -EINVAL;
512}
513EXPORT_SYMBOL(__vfs_write);
514
Al Viro06ae43f2013-03-20 13:19:30 -0400515ssize_t __kernel_write(struct file *file, const char *buf, size_t count, loff_t *pos)
516{
517 mm_segment_t old_fs;
518 const char __user *p;
519 ssize_t ret;
520
Al Viro7f7f25e2014-02-11 17:49:24 -0500521 if (!(file->f_mode & FMODE_CAN_WRITE))
Al Viro3e84f482013-03-27 15:20:30 +0000522 return -EINVAL;
523
Al Viro06ae43f2013-03-20 13:19:30 -0400524 old_fs = get_fs();
525 set_fs(get_ds());
526 p = (__force const char __user *)buf;
527 if (count > MAX_RW_COUNT)
528 count = MAX_RW_COUNT;
Al Viro493c84c2015-04-03 15:06:43 -0400529 ret = __vfs_write(file, p, count, pos);
Al Viro06ae43f2013-03-20 13:19:30 -0400530 set_fs(old_fs);
531 if (ret > 0) {
532 fsnotify_modify(file);
533 add_wchar(current, ret);
534 }
535 inc_syscw(current);
536 return ret;
537}
538
Al Viro2ec3a122014-08-19 11:48:09 -0400539EXPORT_SYMBOL(__kernel_write);
540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
542{
543 ssize_t ret;
544
545 if (!(file->f_mode & FMODE_WRITE))
546 return -EBADF;
Al Viro7f7f25e2014-02-11 17:49:24 -0500547 if (!(file->f_mode & FMODE_CAN_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 return -EINVAL;
549 if (unlikely(!access_ok(VERIFY_READ, buf, count)))
550 return -EFAULT;
551
552 ret = rw_verify_area(WRITE, file, pos, count);
Al Virobc613842016-03-31 21:48:20 -0400553 if (!ret) {
554 if (count > MAX_RW_COUNT)
555 count = MAX_RW_COUNT;
Al Viro03d95eb2013-03-20 13:04:20 -0400556 file_start_write(file);
Al Viro493c84c2015-04-03 15:06:43 -0400557 ret = __vfs_write(file, buf, count, pos);
James Morrisc43e2592008-01-12 22:05:48 +1100558 if (ret > 0) {
Eric Paris2a12a9d2009-12-17 21:24:21 -0500559 fsnotify_modify(file);
James Morrisc43e2592008-01-12 22:05:48 +1100560 add_wchar(current, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 }
James Morrisc43e2592008-01-12 22:05:48 +1100562 inc_syscw(current);
Al Viro03d95eb2013-03-20 13:04:20 -0400563 file_end_write(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 }
565
566 return ret;
567}
568
569EXPORT_SYMBOL(vfs_write);
570
571static inline loff_t file_pos_read(struct file *file)
572{
573 return file->f_pos;
574}
575
576static inline void file_pos_write(struct file *file, loff_t pos)
577{
578 file->f_pos = pos;
579}
580
Heiko Carstens3cdad422009-01-14 14:14:22 +0100581SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
Linus Torvalds9c225f22014-03-03 09:36:58 -0800583 struct fd f = fdget_pos(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Al Viro2903ff02012-08-28 12:52:22 -0400586 if (f.file) {
587 loff_t pos = file_pos_read(f.file);
588 ret = vfs_read(f.file, buf, count, &pos);
Al Viro5faf1532013-06-15 05:49:36 +0400589 if (ret >= 0)
590 file_pos_write(f.file, pos);
Linus Torvalds9c225f22014-03-03 09:36:58 -0800591 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 return ret;
594}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Heiko Carstens3cdad422009-01-14 14:14:22 +0100596SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf,
597 size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
Linus Torvalds9c225f22014-03-03 09:36:58 -0800599 struct fd f = fdget_pos(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Al Viro2903ff02012-08-28 12:52:22 -0400602 if (f.file) {
603 loff_t pos = file_pos_read(f.file);
604 ret = vfs_write(f.file, buf, count, &pos);
Al Viro5faf1532013-06-15 05:49:36 +0400605 if (ret >= 0)
606 file_pos_write(f.file, pos);
Linus Torvalds9c225f22014-03-03 09:36:58 -0800607 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 }
609
610 return ret;
611}
612
Al Viro4a0fd5b2013-01-21 15:16:58 -0500613SYSCALL_DEFINE4(pread64, unsigned int, fd, char __user *, buf,
614 size_t, count, loff_t, pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
Al Viro2903ff02012-08-28 12:52:22 -0400616 struct fd f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619 if (pos < 0)
620 return -EINVAL;
621
Al Viro2903ff02012-08-28 12:52:22 -0400622 f = fdget(fd);
623 if (f.file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -0400625 if (f.file->f_mode & FMODE_PREAD)
626 ret = vfs_read(f.file, buf, count, &pos);
627 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 }
629
630 return ret;
631}
632
Al Viro4a0fd5b2013-01-21 15:16:58 -0500633SYSCALL_DEFINE4(pwrite64, unsigned int, fd, const char __user *, buf,
634 size_t, count, loff_t, pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
Al Viro2903ff02012-08-28 12:52:22 -0400636 struct fd f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639 if (pos < 0)
640 return -EINVAL;
641
Al Viro2903ff02012-08-28 12:52:22 -0400642 f = fdget(fd);
643 if (f.file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -0400645 if (f.file->f_mode & FMODE_PWRITE)
646 ret = vfs_write(f.file, buf, count, &pos);
647 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 }
649
650 return ret;
651}
652
653/*
654 * Reduce an iovec's length in-place. Return the resulting number of segments
655 */
656unsigned long iov_shorten(struct iovec *iov, unsigned long nr_segs, size_t to)
657{
658 unsigned long seg = 0;
659 size_t len = 0;
660
661 while (seg < nr_segs) {
662 seg++;
663 if (len + iov->iov_len >= to) {
664 iov->iov_len = to - len;
665 break;
666 }
667 len += iov->iov_len;
668 iov++;
669 }
670 return seg;
671}
Eric Sandeen19295522008-01-28 23:58:27 -0500672EXPORT_SYMBOL(iov_shorten);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Al Viroac15ac02015-03-20 20:10:21 -0400674static ssize_t do_iter_readv_writev(struct file *filp, struct iov_iter *iter,
Miklos Szeredi04676262017-02-20 16:51:23 +0100675 loff_t *ppos, int type, int flags)
Al Viro293bc982014-02-11 18:37:41 -0500676{
677 struct kiocb kiocb;
Al Viro293bc982014-02-11 18:37:41 -0500678 ssize_t ret;
679
Christoph Hellwige864f392016-04-07 08:52:03 -0700680 if (flags & ~(RWF_HIPRI | RWF_DSYNC | RWF_SYNC))
Christoph Hellwig793b80e2016-03-03 16:03:58 +0100681 return -EOPNOTSUPP;
682
Al Viro293bc982014-02-11 18:37:41 -0500683 init_sync_kiocb(&kiocb, filp);
Christoph Hellwig97be7eb2016-03-03 16:04:01 +0100684 if (flags & RWF_HIPRI)
685 kiocb.ki_flags |= IOCB_HIPRI;
Christoph Hellwige864f392016-04-07 08:52:03 -0700686 if (flags & RWF_DSYNC)
687 kiocb.ki_flags |= IOCB_DSYNC;
688 if (flags & RWF_SYNC)
689 kiocb.ki_flags |= (IOCB_DSYNC | IOCB_SYNC);
Al Viro293bc982014-02-11 18:37:41 -0500690 kiocb.ki_pos = *ppos;
Al Viro293bc982014-02-11 18:37:41 -0500691
Miklos Szeredi04676262017-02-20 16:51:23 +0100692 if (type == READ)
693 ret = filp->f_op->read_iter(&kiocb, iter);
694 else
695 ret = filp->f_op->write_iter(&kiocb, iter);
Christoph Hellwig599bd192015-02-11 19:59:44 +0100696 BUG_ON(ret == -EIOCBQUEUED);
Al Viro293bc982014-02-11 18:37:41 -0500697 *ppos = kiocb.ki_pos;
698 return ret;
699}
700
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700701/* Do it by hand, with file-ops */
Al Viroac15ac02015-03-20 20:10:21 -0400702static ssize_t do_loop_readv_writev(struct file *filp, struct iov_iter *iter,
Miklos Szeredi04676262017-02-20 16:51:23 +0100703 loff_t *ppos, int type, int flags)
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700704{
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700705 ssize_t ret = 0;
706
Christoph Hellwig97be7eb2016-03-03 16:04:01 +0100707 if (flags & ~RWF_HIPRI)
Christoph Hellwig793b80e2016-03-03 16:03:58 +0100708 return -EOPNOTSUPP;
709
Al Viroac15ac02015-03-20 20:10:21 -0400710 while (iov_iter_count(iter)) {
711 struct iovec iovec = iov_iter_iovec(iter);
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700712 ssize_t nr;
713
Miklos Szeredi04676262017-02-20 16:51:23 +0100714 if (type == READ) {
715 nr = filp->f_op->read(filp, iovec.iov_base,
716 iovec.iov_len, ppos);
717 } else {
718 nr = filp->f_op->write(filp, iovec.iov_base,
719 iovec.iov_len, ppos);
720 }
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700721
722 if (nr < 0) {
723 if (!ret)
724 ret = nr;
725 break;
726 }
727 ret += nr;
Al Viroac15ac02015-03-20 20:10:21 -0400728 if (nr != iovec.iov_len)
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700729 break;
Al Viroac15ac02015-03-20 20:10:21 -0400730 iov_iter_advance(iter, nr);
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700731 }
732
733 return ret;
734}
735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736/* A write operation does a read from user space and vice versa */
737#define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
738
Vegard Nossumffecee42016-10-08 11:18:07 +0200739/**
740 * rw_copy_check_uvector() - Copy an array of &struct iovec from userspace
741 * into the kernel and check that it is valid.
742 *
743 * @type: One of %CHECK_IOVEC_ONLY, %READ, or %WRITE.
744 * @uvector: Pointer to the userspace array.
745 * @nr_segs: Number of elements in userspace array.
746 * @fast_segs: Number of elements in @fast_pointer.
747 * @fast_pointer: Pointer to (usually small on-stack) kernel array.
748 * @ret_pointer: (output parameter) Pointer to a variable that will point to
749 * either @fast_pointer, a newly allocated kernel array, or NULL,
750 * depending on which array was used.
751 *
752 * This function copies an array of &struct iovec of @nr_segs from
753 * userspace into the kernel and checks that each element is valid (e.g.
754 * it does not point to a kernel address or cause overflow by being too
755 * large, etc.).
756 *
757 * As an optimization, the caller may provide a pointer to a small
758 * on-stack array in @fast_pointer, typically %UIO_FASTIOV elements long
759 * (the size of this array, or 0 if unused, should be given in @fast_segs).
760 *
761 * @ret_pointer will always point to the array that was used, so the
762 * caller must take care not to call kfree() on it e.g. in case the
763 * @fast_pointer array was used and it was allocated on the stack.
764 *
765 * Return: The total number of bytes covered by the iovec array on success
766 * or a negative error code on error.
767 */
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700768ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
769 unsigned long nr_segs, unsigned long fast_segs,
770 struct iovec *fast_pointer,
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700771 struct iovec **ret_pointer)
Linus Torvalds435f49a2010-10-29 10:36:49 -0700772{
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700773 unsigned long seg;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700774 ssize_t ret;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700775 struct iovec *iov = fast_pointer;
776
Linus Torvalds435f49a2010-10-29 10:36:49 -0700777 /*
778 * SuS says "The readv() function *may* fail if the iovcnt argument
779 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
780 * traditionally returned zero for zero segments, so...
781 */
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700782 if (nr_segs == 0) {
783 ret = 0;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700784 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700785 }
786
Linus Torvalds435f49a2010-10-29 10:36:49 -0700787 /*
788 * First get the "struct iovec" from user memory and
789 * verify all the pointers
790 */
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700791 if (nr_segs > UIO_MAXIOV) {
792 ret = -EINVAL;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700793 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700794 }
795 if (nr_segs > fast_segs) {
Linus Torvalds435f49a2010-10-29 10:36:49 -0700796 iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700797 if (iov == NULL) {
798 ret = -ENOMEM;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700799 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700800 }
Linus Torvalds435f49a2010-10-29 10:36:49 -0700801 }
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700802 if (copy_from_user(iov, uvector, nr_segs*sizeof(*uvector))) {
803 ret = -EFAULT;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700804 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700805 }
806
Linus Torvalds435f49a2010-10-29 10:36:49 -0700807 /*
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700808 * According to the Single Unix Specification we should return EINVAL
809 * if an element length is < 0 when cast to ssize_t or if the
810 * total length would overflow the ssize_t return value of the
811 * system call.
Linus Torvalds435f49a2010-10-29 10:36:49 -0700812 *
813 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
814 * overflow case.
815 */
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700816 ret = 0;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700817 for (seg = 0; seg < nr_segs; seg++) {
818 void __user *buf = iov[seg].iov_base;
819 ssize_t len = (ssize_t)iov[seg].iov_len;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700820
821 /* see if we we're about to use an invalid len or if
822 * it's about to overflow ssize_t */
Linus Torvalds435f49a2010-10-29 10:36:49 -0700823 if (len < 0) {
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700824 ret = -EINVAL;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700825 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700826 }
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700827 if (type >= 0
Christopher Yeohfcf63402011-10-31 17:06:39 -0700828 && unlikely(!access_ok(vrfy_dir(type), buf, len))) {
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700829 ret = -EFAULT;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700830 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700831 }
Linus Torvalds435f49a2010-10-29 10:36:49 -0700832 if (len > MAX_RW_COUNT - ret) {
833 len = MAX_RW_COUNT - ret;
834 iov[seg].iov_len = len;
835 }
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700836 ret += len;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700837 }
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700838out:
839 *ret_pointer = iov;
840 return ret;
841}
842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843static ssize_t do_readv_writev(int type, struct file *file,
844 const struct iovec __user * uvector,
Christoph Hellwig793b80e2016-03-03 16:03:58 +0100845 unsigned long nr_segs, loff_t *pos,
846 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 size_t tot_len;
849 struct iovec iovstack[UIO_FASTIOV];
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700850 struct iovec *iov = iovstack;
Al Viroac15ac02015-03-20 20:10:21 -0400851 struct iov_iter iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Al Viro0504c072015-03-21 19:40:11 -0400854 ret = import_iovec(type, uvector, nr_segs,
855 ARRAY_SIZE(iovstack), &iov, &iter);
856 if (ret < 0)
857 return ret;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700858
Al Viro0504c072015-03-21 19:40:11 -0400859 tot_len = iov_iter_count(&iter);
860 if (!tot_len)
861 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 ret = rw_verify_area(type, file, pos, tot_len);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800863 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 goto out;
865
Miklos Szeredi04676262017-02-20 16:51:23 +0100866 if (type != READ)
Al Viro03d95eb2013-03-20 13:04:20 -0400867 file_start_write(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Miklos Szeredi04676262017-02-20 16:51:23 +0100869 if ((type == READ && file->f_op->read_iter) ||
870 (type == WRITE && file->f_op->write_iter))
871 ret = do_iter_readv_writev(file, &iter, pos, type, flags);
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700872 else
Miklos Szeredi04676262017-02-20 16:51:23 +0100873 ret = do_loop_readv_writev(file, &iter, pos, type, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Al Viro03d95eb2013-03-20 13:04:20 -0400875 if (type != READ)
876 file_end_write(file);
877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878out:
Al Viro0504c072015-03-21 19:40:11 -0400879 kfree(iov);
Robert Love0eeca282005-07-12 17:06:03 -0400880 if ((ret + (type == READ)) > 0) {
881 if (type == READ)
Eric Paris2a12a9d2009-12-17 21:24:21 -0500882 fsnotify_access(file);
Robert Love0eeca282005-07-12 17:06:03 -0400883 else
Eric Paris2a12a9d2009-12-17 21:24:21 -0500884 fsnotify_modify(file);
Robert Love0eeca282005-07-12 17:06:03 -0400885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887}
888
889ssize_t vfs_readv(struct file *file, const struct iovec __user *vec,
Christoph Hellwig793b80e2016-03-03 16:03:58 +0100890 unsigned long vlen, loff_t *pos, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891{
892 if (!(file->f_mode & FMODE_READ))
893 return -EBADF;
Al Viro7f7f25e2014-02-11 17:49:24 -0500894 if (!(file->f_mode & FMODE_CAN_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 return -EINVAL;
896
Christoph Hellwig793b80e2016-03-03 16:03:58 +0100897 return do_readv_writev(READ, file, vec, vlen, pos, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898}
899
900EXPORT_SYMBOL(vfs_readv);
901
902ssize_t vfs_writev(struct file *file, const struct iovec __user *vec,
Christoph Hellwig793b80e2016-03-03 16:03:58 +0100903 unsigned long vlen, loff_t *pos, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
905 if (!(file->f_mode & FMODE_WRITE))
906 return -EBADF;
Al Viro7f7f25e2014-02-11 17:49:24 -0500907 if (!(file->f_mode & FMODE_CAN_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 return -EINVAL;
909
Christoph Hellwig793b80e2016-03-03 16:03:58 +0100910 return do_readv_writev(WRITE, file, vec, vlen, pos, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911}
912
913EXPORT_SYMBOL(vfs_writev);
914
Milosz Tanskif17d8b32016-03-03 16:03:59 +0100915static ssize_t do_readv(unsigned long fd, const struct iovec __user *vec,
916 unsigned long vlen, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
Linus Torvalds9c225f22014-03-03 09:36:58 -0800918 struct fd f = fdget_pos(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Al Viro2903ff02012-08-28 12:52:22 -0400921 if (f.file) {
922 loff_t pos = file_pos_read(f.file);
Milosz Tanskif17d8b32016-03-03 16:03:59 +0100923 ret = vfs_readv(f.file, vec, vlen, &pos, flags);
Al Viro5faf1532013-06-15 05:49:36 +0400924 if (ret >= 0)
925 file_pos_write(f.file, pos);
Linus Torvalds9c225f22014-03-03 09:36:58 -0800926 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 }
928
929 if (ret > 0)
Alexey Dobriyan4b98d112007-02-10 01:46:45 -0800930 add_rchar(current, ret);
931 inc_syscr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 return ret;
933}
934
Milosz Tanskif17d8b32016-03-03 16:03:59 +0100935static ssize_t do_writev(unsigned long fd, const struct iovec __user *vec,
936 unsigned long vlen, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
Linus Torvalds9c225f22014-03-03 09:36:58 -0800938 struct fd f = fdget_pos(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Al Viro2903ff02012-08-28 12:52:22 -0400941 if (f.file) {
942 loff_t pos = file_pos_read(f.file);
Milosz Tanskif17d8b32016-03-03 16:03:59 +0100943 ret = vfs_writev(f.file, vec, vlen, &pos, flags);
Al Viro5faf1532013-06-15 05:49:36 +0400944 if (ret >= 0)
945 file_pos_write(f.file, pos);
Linus Torvalds9c225f22014-03-03 09:36:58 -0800946 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 }
948
949 if (ret > 0)
Alexey Dobriyan4b98d112007-02-10 01:46:45 -0800950 add_wchar(current, ret);
951 inc_syscw(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 return ret;
953}
954
Linus Torvalds601cc112009-04-03 08:03:22 -0700955static inline loff_t pos_from_hilo(unsigned long high, unsigned long low)
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700956{
Linus Torvalds601cc112009-04-03 08:03:22 -0700957#define HALF_LONG_BITS (BITS_PER_LONG / 2)
958 return (((loff_t)high << HALF_LONG_BITS) << HALF_LONG_BITS) | low;
959}
960
Milosz Tanskif17d8b32016-03-03 16:03:59 +0100961static ssize_t do_preadv(unsigned long fd, const struct iovec __user *vec,
962 unsigned long vlen, loff_t pos, int flags)
Linus Torvalds601cc112009-04-03 08:03:22 -0700963{
Al Viro2903ff02012-08-28 12:52:22 -0400964 struct fd f;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700965 ssize_t ret = -EBADF;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700966
967 if (pos < 0)
968 return -EINVAL;
969
Al Viro2903ff02012-08-28 12:52:22 -0400970 f = fdget(fd);
971 if (f.file) {
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700972 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -0400973 if (f.file->f_mode & FMODE_PREAD)
Milosz Tanskif17d8b32016-03-03 16:03:59 +0100974 ret = vfs_readv(f.file, vec, vlen, &pos, flags);
Al Viro2903ff02012-08-28 12:52:22 -0400975 fdput(f);
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700976 }
977
978 if (ret > 0)
979 add_rchar(current, ret);
980 inc_syscr(current);
981 return ret;
982}
983
Milosz Tanskif17d8b32016-03-03 16:03:59 +0100984static ssize_t do_pwritev(unsigned long fd, const struct iovec __user *vec,
985 unsigned long vlen, loff_t pos, int flags)
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700986{
Al Viro2903ff02012-08-28 12:52:22 -0400987 struct fd f;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700988 ssize_t ret = -EBADF;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700989
990 if (pos < 0)
991 return -EINVAL;
992
Al Viro2903ff02012-08-28 12:52:22 -0400993 f = fdget(fd);
994 if (f.file) {
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700995 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -0400996 if (f.file->f_mode & FMODE_PWRITE)
Milosz Tanskif17d8b32016-03-03 16:03:59 +0100997 ret = vfs_writev(f.file, vec, vlen, &pos, flags);
Al Viro2903ff02012-08-28 12:52:22 -0400998 fdput(f);
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700999 }
1000
1001 if (ret > 0)
1002 add_wchar(current, ret);
1003 inc_syscw(current);
1004 return ret;
1005}
1006
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001007SYSCALL_DEFINE3(readv, unsigned long, fd, const struct iovec __user *, vec,
1008 unsigned long, vlen)
1009{
1010 return do_readv(fd, vec, vlen, 0);
1011}
1012
1013SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec __user *, vec,
1014 unsigned long, vlen)
1015{
1016 return do_writev(fd, vec, vlen, 0);
1017}
1018
1019SYSCALL_DEFINE5(preadv, unsigned long, fd, const struct iovec __user *, vec,
1020 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
1021{
1022 loff_t pos = pos_from_hilo(pos_h, pos_l);
1023
1024 return do_preadv(fd, vec, vlen, pos, 0);
1025}
1026
1027SYSCALL_DEFINE6(preadv2, unsigned long, fd, const struct iovec __user *, vec,
1028 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h,
1029 int, flags)
1030{
1031 loff_t pos = pos_from_hilo(pos_h, pos_l);
1032
1033 if (pos == -1)
1034 return do_readv(fd, vec, vlen, flags);
1035
1036 return do_preadv(fd, vec, vlen, pos, flags);
1037}
1038
1039SYSCALL_DEFINE5(pwritev, unsigned long, fd, const struct iovec __user *, vec,
1040 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
1041{
1042 loff_t pos = pos_from_hilo(pos_h, pos_l);
1043
1044 return do_pwritev(fd, vec, vlen, pos, 0);
1045}
1046
1047SYSCALL_DEFINE6(pwritev2, unsigned long, fd, const struct iovec __user *, vec,
1048 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h,
1049 int, flags)
1050{
1051 loff_t pos = pos_from_hilo(pos_h, pos_l);
1052
1053 if (pos == -1)
1054 return do_writev(fd, vec, vlen, flags);
1055
1056 return do_pwritev(fd, vec, vlen, pos, flags);
1057}
1058
Al Viro72ec3512013-03-20 10:42:10 -04001059#ifdef CONFIG_COMPAT
1060
1061static ssize_t compat_do_readv_writev(int type, struct file *file,
1062 const struct compat_iovec __user *uvector,
Christoph Hellwig793b80e2016-03-03 16:03:58 +01001063 unsigned long nr_segs, loff_t *pos,
1064 int flags)
Al Viro72ec3512013-03-20 10:42:10 -04001065{
1066 compat_ssize_t tot_len;
1067 struct iovec iovstack[UIO_FASTIOV];
1068 struct iovec *iov = iovstack;
Al Viroac15ac02015-03-20 20:10:21 -04001069 struct iov_iter iter;
Al Viro72ec3512013-03-20 10:42:10 -04001070 ssize_t ret;
Al Viro72ec3512013-03-20 10:42:10 -04001071
Al Viro0504c072015-03-21 19:40:11 -04001072 ret = compat_import_iovec(type, uvector, nr_segs,
1073 UIO_FASTIOV, &iov, &iter);
1074 if (ret < 0)
1075 return ret;
Al Viro72ec3512013-03-20 10:42:10 -04001076
Al Viro0504c072015-03-21 19:40:11 -04001077 tot_len = iov_iter_count(&iter);
1078 if (!tot_len)
1079 goto out;
Al Viro72ec3512013-03-20 10:42:10 -04001080 ret = rw_verify_area(type, file, pos, tot_len);
1081 if (ret < 0)
1082 goto out;
1083
Miklos Szeredi04676262017-02-20 16:51:23 +01001084 if (type != READ)
Al Viro03d95eb2013-03-20 13:04:20 -04001085 file_start_write(file);
Al Viro72ec3512013-03-20 10:42:10 -04001086
Miklos Szeredi04676262017-02-20 16:51:23 +01001087 if ((type == READ && file->f_op->read_iter) ||
1088 (type == WRITE && file->f_op->write_iter))
1089 ret = do_iter_readv_writev(file, &iter, pos, type, flags);
Al Viro03d95eb2013-03-20 13:04:20 -04001090 else
Miklos Szeredi04676262017-02-20 16:51:23 +01001091 ret = do_loop_readv_writev(file, &iter, pos, type, flags);
Al Viro72ec3512013-03-20 10:42:10 -04001092
Al Viro03d95eb2013-03-20 13:04:20 -04001093 if (type != READ)
1094 file_end_write(file);
1095
Al Viro72ec3512013-03-20 10:42:10 -04001096out:
Al Viro0504c072015-03-21 19:40:11 -04001097 kfree(iov);
Al Viro72ec3512013-03-20 10:42:10 -04001098 if ((ret + (type == READ)) > 0) {
1099 if (type == READ)
1100 fsnotify_access(file);
1101 else
1102 fsnotify_modify(file);
1103 }
1104 return ret;
1105}
1106
1107static size_t compat_readv(struct file *file,
1108 const struct compat_iovec __user *vec,
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001109 unsigned long vlen, loff_t *pos, int flags)
Al Viro72ec3512013-03-20 10:42:10 -04001110{
1111 ssize_t ret = -EBADF;
1112
1113 if (!(file->f_mode & FMODE_READ))
1114 goto out;
1115
1116 ret = -EINVAL;
Al Viro7f7f25e2014-02-11 17:49:24 -05001117 if (!(file->f_mode & FMODE_CAN_READ))
Al Viro72ec3512013-03-20 10:42:10 -04001118 goto out;
1119
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001120 ret = compat_do_readv_writev(READ, file, vec, vlen, pos, flags);
Al Viro72ec3512013-03-20 10:42:10 -04001121
1122out:
1123 if (ret > 0)
1124 add_rchar(current, ret);
1125 inc_syscr(current);
1126 return ret;
1127}
1128
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001129static size_t do_compat_readv(compat_ulong_t fd,
1130 const struct compat_iovec __user *vec,
1131 compat_ulong_t vlen, int flags)
Al Viro72ec3512013-03-20 10:42:10 -04001132{
Linus Torvalds9c225f22014-03-03 09:36:58 -08001133 struct fd f = fdget_pos(fd);
Al Viro72ec3512013-03-20 10:42:10 -04001134 ssize_t ret;
1135 loff_t pos;
1136
1137 if (!f.file)
1138 return -EBADF;
1139 pos = f.file->f_pos;
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001140 ret = compat_readv(f.file, vec, vlen, &pos, flags);
Al Viro5faf1532013-06-15 05:49:36 +04001141 if (ret >= 0)
1142 f.file->f_pos = pos;
Linus Torvalds9c225f22014-03-03 09:36:58 -08001143 fdput_pos(f);
Al Viro72ec3512013-03-20 10:42:10 -04001144 return ret;
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001145
Al Viro72ec3512013-03-20 10:42:10 -04001146}
1147
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001148COMPAT_SYSCALL_DEFINE3(readv, compat_ulong_t, fd,
1149 const struct compat_iovec __user *,vec,
1150 compat_ulong_t, vlen)
1151{
1152 return do_compat_readv(fd, vec, vlen, 0);
1153}
1154
1155static long do_compat_preadv64(unsigned long fd,
Heiko Carstens378a10f2014-03-05 10:43:51 +01001156 const struct compat_iovec __user *vec,
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001157 unsigned long vlen, loff_t pos, int flags)
Al Viro72ec3512013-03-20 10:42:10 -04001158{
1159 struct fd f;
1160 ssize_t ret;
1161
1162 if (pos < 0)
1163 return -EINVAL;
1164 f = fdget(fd);
1165 if (!f.file)
1166 return -EBADF;
1167 ret = -ESPIPE;
1168 if (f.file->f_mode & FMODE_PREAD)
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001169 ret = compat_readv(f.file, vec, vlen, &pos, flags);
Al Viro72ec3512013-03-20 10:42:10 -04001170 fdput(f);
1171 return ret;
1172}
1173
Heiko Carstens378a10f2014-03-05 10:43:51 +01001174#ifdef __ARCH_WANT_COMPAT_SYS_PREADV64
1175COMPAT_SYSCALL_DEFINE4(preadv64, unsigned long, fd,
1176 const struct compat_iovec __user *,vec,
1177 unsigned long, vlen, loff_t, pos)
1178{
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001179 return do_compat_preadv64(fd, vec, vlen, pos, 0);
Heiko Carstens378a10f2014-03-05 10:43:51 +01001180}
1181#endif
1182
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001183COMPAT_SYSCALL_DEFINE5(preadv, compat_ulong_t, fd,
Al Viro72ec3512013-03-20 10:42:10 -04001184 const struct compat_iovec __user *,vec,
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001185 compat_ulong_t, vlen, u32, pos_low, u32, pos_high)
Al Viro72ec3512013-03-20 10:42:10 -04001186{
1187 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
Heiko Carstens378a10f2014-03-05 10:43:51 +01001188
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001189 return do_compat_preadv64(fd, vec, vlen, pos, 0);
1190}
1191
H.J. Lu3ebfd812016-07-14 12:31:53 -07001192#ifdef __ARCH_WANT_COMPAT_SYS_PREADV64V2
1193COMPAT_SYSCALL_DEFINE5(preadv64v2, unsigned long, fd,
1194 const struct compat_iovec __user *,vec,
1195 unsigned long, vlen, loff_t, pos, int, flags)
1196{
1197 return do_compat_preadv64(fd, vec, vlen, pos, flags);
1198}
1199#endif
1200
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001201COMPAT_SYSCALL_DEFINE6(preadv2, compat_ulong_t, fd,
1202 const struct compat_iovec __user *,vec,
1203 compat_ulong_t, vlen, u32, pos_low, u32, pos_high,
1204 int, flags)
1205{
1206 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
1207
1208 if (pos == -1)
1209 return do_compat_readv(fd, vec, vlen, flags);
1210
1211 return do_compat_preadv64(fd, vec, vlen, pos, flags);
Al Viro72ec3512013-03-20 10:42:10 -04001212}
1213
1214static size_t compat_writev(struct file *file,
1215 const struct compat_iovec __user *vec,
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001216 unsigned long vlen, loff_t *pos, int flags)
Al Viro72ec3512013-03-20 10:42:10 -04001217{
1218 ssize_t ret = -EBADF;
1219
1220 if (!(file->f_mode & FMODE_WRITE))
1221 goto out;
1222
1223 ret = -EINVAL;
Al Viro7f7f25e2014-02-11 17:49:24 -05001224 if (!(file->f_mode & FMODE_CAN_WRITE))
Al Viro72ec3512013-03-20 10:42:10 -04001225 goto out;
1226
Christoph Hellwig222aa342017-06-16 11:08:24 +02001227 ret = compat_do_readv_writev(WRITE, file, vec, vlen, pos, flags);
Al Viro72ec3512013-03-20 10:42:10 -04001228
1229out:
1230 if (ret > 0)
1231 add_wchar(current, ret);
1232 inc_syscw(current);
1233 return ret;
1234}
1235
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001236static size_t do_compat_writev(compat_ulong_t fd,
1237 const struct compat_iovec __user* vec,
1238 compat_ulong_t vlen, int flags)
Al Viro72ec3512013-03-20 10:42:10 -04001239{
Linus Torvalds9c225f22014-03-03 09:36:58 -08001240 struct fd f = fdget_pos(fd);
Al Viro72ec3512013-03-20 10:42:10 -04001241 ssize_t ret;
1242 loff_t pos;
1243
1244 if (!f.file)
1245 return -EBADF;
1246 pos = f.file->f_pos;
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001247 ret = compat_writev(f.file, vec, vlen, &pos, flags);
Al Viro5faf1532013-06-15 05:49:36 +04001248 if (ret >= 0)
1249 f.file->f_pos = pos;
Linus Torvalds9c225f22014-03-03 09:36:58 -08001250 fdput_pos(f);
Al Viro72ec3512013-03-20 10:42:10 -04001251 return ret;
1252}
1253
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001254COMPAT_SYSCALL_DEFINE3(writev, compat_ulong_t, fd,
1255 const struct compat_iovec __user *, vec,
1256 compat_ulong_t, vlen)
1257{
1258 return do_compat_writev(fd, vec, vlen, 0);
1259}
1260
1261static long do_compat_pwritev64(unsigned long fd,
Heiko Carstens378a10f2014-03-05 10:43:51 +01001262 const struct compat_iovec __user *vec,
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001263 unsigned long vlen, loff_t pos, int flags)
Al Viro72ec3512013-03-20 10:42:10 -04001264{
1265 struct fd f;
1266 ssize_t ret;
1267
1268 if (pos < 0)
1269 return -EINVAL;
1270 f = fdget(fd);
1271 if (!f.file)
1272 return -EBADF;
1273 ret = -ESPIPE;
1274 if (f.file->f_mode & FMODE_PWRITE)
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001275 ret = compat_writev(f.file, vec, vlen, &pos, flags);
Al Viro72ec3512013-03-20 10:42:10 -04001276 fdput(f);
1277 return ret;
1278}
1279
Heiko Carstens378a10f2014-03-05 10:43:51 +01001280#ifdef __ARCH_WANT_COMPAT_SYS_PWRITEV64
1281COMPAT_SYSCALL_DEFINE4(pwritev64, unsigned long, fd,
1282 const struct compat_iovec __user *,vec,
1283 unsigned long, vlen, loff_t, pos)
1284{
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001285 return do_compat_pwritev64(fd, vec, vlen, pos, 0);
Heiko Carstens378a10f2014-03-05 10:43:51 +01001286}
1287#endif
1288
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001289COMPAT_SYSCALL_DEFINE5(pwritev, compat_ulong_t, fd,
Al Viro72ec3512013-03-20 10:42:10 -04001290 const struct compat_iovec __user *,vec,
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001291 compat_ulong_t, vlen, u32, pos_low, u32, pos_high)
Al Viro72ec3512013-03-20 10:42:10 -04001292{
1293 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
Heiko Carstens378a10f2014-03-05 10:43:51 +01001294
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001295 return do_compat_pwritev64(fd, vec, vlen, pos, 0);
Al Viro72ec3512013-03-20 10:42:10 -04001296}
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001297
H.J. Lu3ebfd812016-07-14 12:31:53 -07001298#ifdef __ARCH_WANT_COMPAT_SYS_PWRITEV64V2
1299COMPAT_SYSCALL_DEFINE5(pwritev64v2, unsigned long, fd,
1300 const struct compat_iovec __user *,vec,
1301 unsigned long, vlen, loff_t, pos, int, flags)
1302{
1303 return do_compat_pwritev64(fd, vec, vlen, pos, flags);
1304}
1305#endif
1306
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001307COMPAT_SYSCALL_DEFINE6(pwritev2, compat_ulong_t, fd,
1308 const struct compat_iovec __user *,vec,
1309 compat_ulong_t, vlen, u32, pos_low, u32, pos_high, int, flags)
1310{
1311 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
1312
1313 if (pos == -1)
1314 return do_compat_writev(fd, vec, vlen, flags);
1315
1316 return do_compat_pwritev64(fd, vec, vlen, pos, flags);
1317}
1318
Al Viro72ec3512013-03-20 10:42:10 -04001319#endif
1320
Al Viro19f4fc32013-02-24 02:17:03 -05001321static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,
1322 size_t count, loff_t max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323{
Al Viro2903ff02012-08-28 12:52:22 -04001324 struct fd in, out;
1325 struct inode *in_inode, *out_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 loff_t pos;
Al Viro7995bd22013-06-20 18:58:36 +04001327 loff_t out_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 ssize_t retval;
Al Viro2903ff02012-08-28 12:52:22 -04001329 int fl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
1331 /*
1332 * Get input file, and verify that it is ok..
1333 */
1334 retval = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -04001335 in = fdget(in_fd);
1336 if (!in.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 goto out;
Al Viro2903ff02012-08-28 12:52:22 -04001338 if (!(in.file->f_mode & FMODE_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 goto fput_in;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 retval = -ESPIPE;
Al Viro7995bd22013-06-20 18:58:36 +04001341 if (!ppos) {
1342 pos = in.file->f_pos;
1343 } else {
1344 pos = *ppos;
Al Viro2903ff02012-08-28 12:52:22 -04001345 if (!(in.file->f_mode & FMODE_PREAD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 goto fput_in;
Al Viro7995bd22013-06-20 18:58:36 +04001347 }
1348 retval = rw_verify_area(READ, in.file, &pos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -08001349 if (retval < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 goto fput_in;
Al Virobc613842016-03-31 21:48:20 -04001351 if (count > MAX_RW_COUNT)
1352 count = MAX_RW_COUNT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 /*
1355 * Get output file, and verify that it is ok..
1356 */
1357 retval = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -04001358 out = fdget(out_fd);
1359 if (!out.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 goto fput_in;
Al Viro2903ff02012-08-28 12:52:22 -04001361 if (!(out.file->f_mode & FMODE_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 goto fput_out;
1363 retval = -EINVAL;
Al Viro496ad9a2013-01-23 17:07:38 -05001364 in_inode = file_inode(in.file);
1365 out_inode = file_inode(out.file);
Al Viro7995bd22013-06-20 18:58:36 +04001366 out_pos = out.file->f_pos;
1367 retval = rw_verify_area(WRITE, out.file, &out_pos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -08001368 if (retval < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 goto fput_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 if (!max)
1372 max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes);
1373
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 if (unlikely(pos + count > max)) {
1375 retval = -EOVERFLOW;
1376 if (pos >= max)
1377 goto fput_out;
1378 count = max - pos;
1379 }
1380
Jens Axboed96e6e72007-06-11 12:18:52 +02001381 fl = 0;
Jens Axboe534f2aa2007-06-01 14:52:37 +02001382#if 0
Jens Axboed96e6e72007-06-11 12:18:52 +02001383 /*
1384 * We need to debate whether we can enable this or not. The
1385 * man page documents EAGAIN return for the output at least,
1386 * and the application is arguably buggy if it doesn't expect
1387 * EAGAIN on a non-blocking file descriptor.
1388 */
Al Viro2903ff02012-08-28 12:52:22 -04001389 if (in.file->f_flags & O_NONBLOCK)
Jens Axboed96e6e72007-06-11 12:18:52 +02001390 fl = SPLICE_F_NONBLOCK;
Jens Axboe534f2aa2007-06-01 14:52:37 +02001391#endif
Al Viro50cd2c52013-05-23 20:10:34 -04001392 file_start_write(out.file);
Al Viro7995bd22013-06-20 18:58:36 +04001393 retval = do_splice_direct(in.file, &pos, out.file, &out_pos, count, fl);
Al Viro50cd2c52013-05-23 20:10:34 -04001394 file_end_write(out.file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
1396 if (retval > 0) {
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08001397 add_rchar(current, retval);
1398 add_wchar(current, retval);
Scott Wolchoka68c2f12012-12-20 15:05:52 -08001399 fsnotify_access(in.file);
1400 fsnotify_modify(out.file);
Al Viro7995bd22013-06-20 18:58:36 +04001401 out.file->f_pos = out_pos;
1402 if (ppos)
1403 *ppos = pos;
1404 else
1405 in.file->f_pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08001408 inc_syscr(current);
1409 inc_syscw(current);
Al Viro7995bd22013-06-20 18:58:36 +04001410 if (pos > max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 retval = -EOVERFLOW;
1412
1413fput_out:
Al Viro2903ff02012-08-28 12:52:22 -04001414 fdput(out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415fput_in:
Al Viro2903ff02012-08-28 12:52:22 -04001416 fdput(in);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417out:
1418 return retval;
1419}
1420
Heiko Carstens002c8972009-01-14 14:14:18 +01001421SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd, off_t __user *, offset, size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422{
1423 loff_t pos;
1424 off_t off;
1425 ssize_t ret;
1426
1427 if (offset) {
1428 if (unlikely(get_user(off, offset)))
1429 return -EFAULT;
1430 pos = off;
1431 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
1432 if (unlikely(put_user(pos, offset)))
1433 return -EFAULT;
1434 return ret;
1435 }
1436
1437 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1438}
1439
Heiko Carstens002c8972009-01-14 14:14:18 +01001440SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd, loff_t __user *, offset, size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{
1442 loff_t pos;
1443 ssize_t ret;
1444
1445 if (offset) {
1446 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
1447 return -EFAULT;
1448 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
1449 if (unlikely(put_user(pos, offset)))
1450 return -EFAULT;
1451 return ret;
1452 }
1453
1454 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1455}
Al Viro19f4fc32013-02-24 02:17:03 -05001456
1457#ifdef CONFIG_COMPAT
1458COMPAT_SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd,
1459 compat_off_t __user *, offset, compat_size_t, count)
1460{
1461 loff_t pos;
1462 off_t off;
1463 ssize_t ret;
1464
1465 if (offset) {
1466 if (unlikely(get_user(off, offset)))
1467 return -EFAULT;
1468 pos = off;
1469 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
1470 if (unlikely(put_user(pos, offset)))
1471 return -EFAULT;
1472 return ret;
1473 }
1474
1475 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1476}
1477
1478COMPAT_SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd,
1479 compat_loff_t __user *, offset, compat_size_t, count)
1480{
1481 loff_t pos;
1482 ssize_t ret;
1483
1484 if (offset) {
1485 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
1486 return -EFAULT;
1487 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
1488 if (unlikely(put_user(pos, offset)))
1489 return -EFAULT;
1490 return ret;
1491 }
1492
1493 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1494}
1495#endif
Zach Brown29732932015-11-10 16:53:30 -05001496
1497/*
1498 * copy_file_range() differs from regular file read and write in that it
1499 * specifically allows return partial success. When it does so is up to
1500 * the copy_file_range method.
1501 */
1502ssize_t vfs_copy_file_range(struct file *file_in, loff_t pos_in,
1503 struct file *file_out, loff_t pos_out,
1504 size_t len, unsigned int flags)
1505{
1506 struct inode *inode_in = file_inode(file_in);
1507 struct inode *inode_out = file_inode(file_out);
1508 ssize_t ret;
1509
1510 if (flags != 0)
1511 return -EINVAL;
1512
Amir Goldstein27db1f02017-01-31 10:34:56 +02001513 if (S_ISDIR(inode_in->i_mode) || S_ISDIR(inode_out->i_mode))
1514 return -EISDIR;
1515 if (!S_ISREG(inode_in->i_mode) || !S_ISREG(inode_out->i_mode))
1516 return -EINVAL;
1517
Zach Brown29732932015-11-10 16:53:30 -05001518 ret = rw_verify_area(READ, file_in, &pos_in, len);
Al Virobc613842016-03-31 21:48:20 -04001519 if (unlikely(ret))
1520 return ret;
1521
1522 ret = rw_verify_area(WRITE, file_out, &pos_out, len);
1523 if (unlikely(ret))
Zach Brown29732932015-11-10 16:53:30 -05001524 return ret;
1525
1526 if (!(file_in->f_mode & FMODE_READ) ||
1527 !(file_out->f_mode & FMODE_WRITE) ||
Anna Schumakereac70052015-11-10 16:53:33 -05001528 (file_out->f_flags & O_APPEND))
Zach Brown29732932015-11-10 16:53:30 -05001529 return -EBADF;
1530
1531 /* this could be relaxed once a method supports cross-fs copies */
1532 if (inode_in->i_sb != inode_out->i_sb)
1533 return -EXDEV;
1534
1535 if (len == 0)
1536 return 0;
1537
1538 ret = mnt_want_write_file(file_out);
1539 if (ret)
1540 return ret;
1541
Anna Schumakereac70052015-11-10 16:53:33 -05001542 ret = -EOPNOTSUPP;
1543 if (file_out->f_op->copy_file_range)
1544 ret = file_out->f_op->copy_file_range(file_in, pos_in, file_out,
1545 pos_out, len, flags);
1546 if (ret == -EOPNOTSUPP)
1547 ret = do_splice_direct(file_in, &pos_in, file_out, &pos_out,
1548 len > MAX_RW_COUNT ? MAX_RW_COUNT : len, 0);
1549
Zach Brown29732932015-11-10 16:53:30 -05001550 if (ret > 0) {
1551 fsnotify_access(file_in);
1552 add_rchar(current, ret);
1553 fsnotify_modify(file_out);
1554 add_wchar(current, ret);
1555 }
1556 inc_syscr(current);
1557 inc_syscw(current);
1558
1559 mnt_drop_write_file(file_out);
1560
1561 return ret;
1562}
1563EXPORT_SYMBOL(vfs_copy_file_range);
1564
1565SYSCALL_DEFINE6(copy_file_range, int, fd_in, loff_t __user *, off_in,
1566 int, fd_out, loff_t __user *, off_out,
1567 size_t, len, unsigned int, flags)
1568{
1569 loff_t pos_in;
1570 loff_t pos_out;
1571 struct fd f_in;
1572 struct fd f_out;
1573 ssize_t ret = -EBADF;
1574
1575 f_in = fdget(fd_in);
1576 if (!f_in.file)
1577 goto out2;
1578
1579 f_out = fdget(fd_out);
1580 if (!f_out.file)
1581 goto out1;
1582
1583 ret = -EFAULT;
1584 if (off_in) {
1585 if (copy_from_user(&pos_in, off_in, sizeof(loff_t)))
1586 goto out;
1587 } else {
1588 pos_in = f_in.file->f_pos;
1589 }
1590
1591 if (off_out) {
1592 if (copy_from_user(&pos_out, off_out, sizeof(loff_t)))
1593 goto out;
1594 } else {
1595 pos_out = f_out.file->f_pos;
1596 }
1597
1598 ret = vfs_copy_file_range(f_in.file, pos_in, f_out.file, pos_out, len,
1599 flags);
1600 if (ret > 0) {
1601 pos_in += ret;
1602 pos_out += ret;
1603
1604 if (off_in) {
1605 if (copy_to_user(off_in, &pos_in, sizeof(loff_t)))
1606 ret = -EFAULT;
1607 } else {
1608 f_in.file->f_pos = pos_in;
1609 }
1610
1611 if (off_out) {
1612 if (copy_to_user(off_out, &pos_out, sizeof(loff_t)))
1613 ret = -EFAULT;
1614 } else {
1615 f_out.file->f_pos = pos_out;
1616 }
1617 }
1618
1619out:
1620 fdput(f_out);
1621out1:
1622 fdput(f_in);
1623out2:
1624 return ret;
1625}
Christoph Hellwig04b38d62015-12-03 12:59:50 +01001626
1627static int clone_verify_area(struct file *file, loff_t pos, u64 len, bool write)
1628{
1629 struct inode *inode = file_inode(file);
1630
1631 if (unlikely(pos < 0))
1632 return -EINVAL;
1633
1634 if (unlikely((loff_t) (pos + len) < 0))
1635 return -EINVAL;
1636
1637 if (unlikely(inode->i_flctx && mandatory_lock(inode))) {
1638 loff_t end = len ? pos + len - 1 : OFFSET_MAX;
1639 int retval;
1640
1641 retval = locks_mandatory_area(inode, file, pos, end,
1642 write ? F_WRLCK : F_RDLCK);
1643 if (retval < 0)
1644 return retval;
1645 }
1646
1647 return security_file_permission(file, write ? MAY_WRITE : MAY_READ);
1648}
1649
1650int vfs_clone_file_range(struct file *file_in, loff_t pos_in,
1651 struct file *file_out, loff_t pos_out, u64 len)
1652{
1653 struct inode *inode_in = file_inode(file_in);
1654 struct inode *inode_out = file_inode(file_out);
1655 int ret;
1656
1657 if (inode_in->i_sb != inode_out->i_sb ||
1658 file_in->f_path.mnt != file_out->f_path.mnt)
1659 return -EXDEV;
1660
1661 if (S_ISDIR(inode_in->i_mode) || S_ISDIR(inode_out->i_mode))
1662 return -EISDIR;
1663 if (!S_ISREG(inode_in->i_mode) || !S_ISREG(inode_out->i_mode))
Darrick J. Wongd79bdd52015-12-19 00:55:52 -08001664 return -EINVAL;
Christoph Hellwig04b38d62015-12-03 12:59:50 +01001665
1666 if (!(file_in->f_mode & FMODE_READ) ||
1667 !(file_out->f_mode & FMODE_WRITE) ||
Christoph Hellwig0fcbf992016-02-26 18:53:12 +01001668 (file_out->f_flags & O_APPEND))
Christoph Hellwig04b38d62015-12-03 12:59:50 +01001669 return -EBADF;
1670
Christoph Hellwig0fcbf992016-02-26 18:53:12 +01001671 if (!file_in->f_op->clone_file_range)
1672 return -EOPNOTSUPP;
1673
Christoph Hellwig04b38d62015-12-03 12:59:50 +01001674 ret = clone_verify_area(file_in, pos_in, len, false);
1675 if (ret)
1676 return ret;
1677
1678 ret = clone_verify_area(file_out, pos_out, len, true);
1679 if (ret)
1680 return ret;
1681
1682 if (pos_in + len > i_size_read(inode_in))
1683 return -EINVAL;
1684
1685 ret = mnt_want_write_file(file_out);
1686 if (ret)
1687 return ret;
1688
1689 ret = file_in->f_op->clone_file_range(file_in, pos_in,
1690 file_out, pos_out, len);
1691 if (!ret) {
1692 fsnotify_access(file_in);
1693 fsnotify_modify(file_out);
1694 }
1695
1696 mnt_drop_write_file(file_out);
1697 return ret;
1698}
1699EXPORT_SYMBOL(vfs_clone_file_range);
Darrick J. Wong54dbc152015-12-19 00:55:59 -08001700
1701int vfs_dedupe_file_range(struct file *file, struct file_dedupe_range *same)
1702{
1703 struct file_dedupe_range_info *info;
1704 struct inode *src = file_inode(file);
1705 u64 off;
1706 u64 len;
1707 int i;
1708 int ret;
1709 bool is_admin = capable(CAP_SYS_ADMIN);
1710 u16 count = same->dest_count;
1711 struct file *dst_file;
1712 loff_t dst_off;
1713 ssize_t deduped;
1714
1715 if (!(file->f_mode & FMODE_READ))
1716 return -EINVAL;
1717
1718 if (same->reserved1 || same->reserved2)
1719 return -EINVAL;
1720
1721 off = same->src_offset;
1722 len = same->src_length;
1723
1724 ret = -EISDIR;
1725 if (S_ISDIR(src->i_mode))
1726 goto out;
1727
1728 ret = -EINVAL;
1729 if (!S_ISREG(src->i_mode))
1730 goto out;
1731
1732 ret = clone_verify_area(file, off, len, false);
1733 if (ret < 0)
1734 goto out;
1735 ret = 0;
1736
1737 /* pre-format output fields to sane values */
1738 for (i = 0; i < count; i++) {
1739 same->info[i].bytes_deduped = 0ULL;
1740 same->info[i].status = FILE_DEDUPE_RANGE_SAME;
1741 }
1742
1743 for (i = 0, info = same->info; i < count; i++, info++) {
1744 struct inode *dst;
1745 struct fd dst_fd = fdget(info->dest_fd);
1746
1747 dst_file = dst_fd.file;
1748 if (!dst_file) {
1749 info->status = -EBADF;
1750 goto next_loop;
1751 }
1752 dst = file_inode(dst_file);
1753
1754 ret = mnt_want_write_file(dst_file);
1755 if (ret) {
1756 info->status = ret;
1757 goto next_loop;
1758 }
1759
1760 dst_off = info->dest_offset;
1761 ret = clone_verify_area(dst_file, dst_off, len, true);
1762 if (ret < 0) {
1763 info->status = ret;
1764 goto next_file;
1765 }
1766 ret = 0;
1767
1768 if (info->reserved) {
1769 info->status = -EINVAL;
1770 } else if (!(is_admin || (dst_file->f_mode & FMODE_WRITE))) {
1771 info->status = -EINVAL;
1772 } else if (file->f_path.mnt != dst_file->f_path.mnt) {
1773 info->status = -EXDEV;
1774 } else if (S_ISDIR(dst->i_mode)) {
1775 info->status = -EISDIR;
1776 } else if (dst_file->f_op->dedupe_file_range == NULL) {
1777 info->status = -EINVAL;
1778 } else {
1779 deduped = dst_file->f_op->dedupe_file_range(file, off,
1780 len, dst_file,
1781 info->dest_offset);
1782 if (deduped == -EBADE)
1783 info->status = FILE_DEDUPE_RANGE_DIFFERS;
1784 else if (deduped < 0)
1785 info->status = deduped;
1786 else
1787 info->bytes_deduped += deduped;
1788 }
1789
1790next_file:
1791 mnt_drop_write_file(dst_file);
1792next_loop:
1793 fdput(dst_fd);
Darrick J. Wonge62e5602016-01-22 16:58:28 -08001794
1795 if (fatal_signal_pending(current))
1796 goto out;
Darrick J. Wong54dbc152015-12-19 00:55:59 -08001797 }
1798
1799out:
1800 return ret;
1801}
1802EXPORT_SYMBOL(vfs_dedupe_file_range);