blob: 7d453c3e1cb67ecee9f3aacaf797badd8f7be093 [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>
Al Viro06ae43f2013-03-20 13:19:30 -040020#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22#include <asm/uaccess.h>
23#include <asm/unistd.h>
24
Al Viroc0bd14af2013-05-04 15:00:54 -040025typedef ssize_t (*io_fn_t)(struct file *, char __user *, size_t, loff_t *);
Al Viro293bc982014-02-11 18:37:41 -050026typedef ssize_t (*iter_fn_t)(struct kiocb *, struct iov_iter *);
Al Viroc0bd14af2013-05-04 15:00:54 -040027
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080028const struct file_operations generic_ro_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 .llseek = generic_file_llseek,
Al Viroaad4f8b2014-04-02 14:33:16 -040030 .read_iter = generic_file_read_iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 .mmap = generic_file_readonly_mmap,
Jens Axboe534f2aa2007-06-01 14:52:37 +020032 .splice_read = generic_file_splice_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -070033};
34
35EXPORT_SYMBOL(generic_ro_fops);
36
Al Virocccb5a12010-12-17 07:44:05 -050037static inline int unsigned_offsets(struct file *file)
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -070038{
Al Virocccb5a12010-12-17 07:44:05 -050039 return file->f_mode & FMODE_UNSIGNED_OFFSET;
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -070040}
41
Jie Liu46a1c2c2013-06-25 12:02:13 +080042/**
43 * vfs_setpos - update the file offset for lseek
44 * @file: file structure in question
45 * @offset: file offset to seek to
46 * @maxsize: maximum file size
47 *
48 * This is a low-level filesystem helper for updating the file offset to
49 * the value specified by @offset if the given offset is valid and it is
50 * not equal to the current file offset.
51 *
52 * Return the specified offset on success and -EINVAL on invalid offset.
53 */
54loff_t vfs_setpos(struct file *file, loff_t offset, loff_t maxsize)
Andi Kleenef3d0fd2011-09-15 16:06:48 -070055{
56 if (offset < 0 && !unsigned_offsets(file))
57 return -EINVAL;
58 if (offset > maxsize)
59 return -EINVAL;
60
61 if (offset != file->f_pos) {
62 file->f_pos = offset;
63 file->f_version = 0;
64 }
65 return offset;
66}
Jie Liu46a1c2c2013-06-25 12:02:13 +080067EXPORT_SYMBOL(vfs_setpos);
Andi Kleenef3d0fd2011-09-15 16:06:48 -070068
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020069/**
Andi Kleen57604952011-09-15 16:06:50 -070070 * generic_file_llseek_size - generic llseek implementation for regular files
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020071 * @file: file structure to seek on
72 * @offset: file offset to seek to
Andrew Morton965c8e52012-12-17 15:59:39 -080073 * @whence: type of seek
Eric Sandeene8b96eb2012-04-30 13:11:29 -050074 * @size: max size of this file in file system
75 * @eof: offset used for SEEK_END position
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020076 *
Andi Kleen57604952011-09-15 16:06:50 -070077 * This is a variant of generic_file_llseek that allows passing in a custom
Eric Sandeene8b96eb2012-04-30 13:11:29 -050078 * maximum file size and a custom EOF position, for e.g. hashed directories
Andi Kleenef3d0fd2011-09-15 16:06:48 -070079 *
80 * Synchronization:
Andi Kleen57604952011-09-15 16:06:50 -070081 * SEEK_SET and SEEK_END are unsynchronized (but atomic on 64bit platforms)
Andi Kleenef3d0fd2011-09-15 16:06:48 -070082 * SEEK_CUR is synchronized against other SEEK_CURs, but not read/writes.
83 * read/writes behave like SEEK_SET against seeks.
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020084 */
Andi Kleen9465efc2008-06-27 11:05:24 +020085loff_t
Andrew Morton965c8e52012-12-17 15:59:39 -080086generic_file_llseek_size(struct file *file, loff_t offset, int whence,
Eric Sandeene8b96eb2012-04-30 13:11:29 -050087 loff_t maxsize, loff_t eof)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
Andrew Morton965c8e52012-12-17 15:59:39 -080089 switch (whence) {
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020090 case SEEK_END:
Eric Sandeene8b96eb2012-04-30 13:11:29 -050091 offset += eof;
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020092 break;
93 case SEEK_CUR:
Alain Knaff5b6f1eb2008-11-10 17:08:08 -080094 /*
95 * Here we special-case the lseek(fd, 0, SEEK_CUR)
96 * position-querying operation. Avoid rewriting the "same"
97 * f_pos value back to the file because a concurrent read(),
98 * write() or lseek() might have altered it
99 */
100 if (offset == 0)
101 return file->f_pos;
Andi Kleenef3d0fd2011-09-15 16:06:48 -0700102 /*
103 * f_lock protects against read/modify/write race with other
104 * SEEK_CURs. Note that parallel writes and reads behave
105 * like SEEK_SET.
106 */
107 spin_lock(&file->f_lock);
Jie Liu46a1c2c2013-06-25 12:02:13 +0800108 offset = vfs_setpos(file, file->f_pos + offset, maxsize);
Andi Kleenef3d0fd2011-09-15 16:06:48 -0700109 spin_unlock(&file->f_lock);
110 return offset;
Josef Bacik982d8162011-07-18 13:21:35 -0400111 case SEEK_DATA:
112 /*
113 * In the generic case the entire file is data, so as long as
114 * offset isn't at the end of the file then the offset is data.
115 */
Eric Sandeene8b96eb2012-04-30 13:11:29 -0500116 if (offset >= eof)
Josef Bacik982d8162011-07-18 13:21:35 -0400117 return -ENXIO;
118 break;
119 case SEEK_HOLE:
120 /*
121 * There is a virtual hole at the end of the file, so as long as
122 * offset isn't i_size or larger, return i_size.
123 */
Eric Sandeene8b96eb2012-04-30 13:11:29 -0500124 if (offset >= eof)
Josef Bacik982d8162011-07-18 13:21:35 -0400125 return -ENXIO;
Eric Sandeene8b96eb2012-04-30 13:11:29 -0500126 offset = eof;
Josef Bacik982d8162011-07-18 13:21:35 -0400127 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 }
Christoph Hellwig3a8cff42008-08-11 15:37:17 +0200129
Jie Liu46a1c2c2013-06-25 12:02:13 +0800130 return vfs_setpos(file, offset, maxsize);
Andi Kleen57604952011-09-15 16:06:50 -0700131}
132EXPORT_SYMBOL(generic_file_llseek_size);
133
134/**
135 * generic_file_llseek - generic llseek implementation for regular files
136 * @file: file structure to seek on
137 * @offset: file offset to seek to
Andrew Morton965c8e52012-12-17 15:59:39 -0800138 * @whence: type of seek
Andi Kleen57604952011-09-15 16:06:50 -0700139 *
140 * This is a generic implemenation of ->llseek useable for all normal local
141 * filesystems. It just updates the file offset to the value specified by
Ming Lei546ae2d2013-04-29 15:06:07 -0700142 * @offset and @whence.
Andi Kleen57604952011-09-15 16:06:50 -0700143 */
Andrew Morton965c8e52012-12-17 15:59:39 -0800144loff_t generic_file_llseek(struct file *file, loff_t offset, int whence)
Andi Kleen57604952011-09-15 16:06:50 -0700145{
146 struct inode *inode = file->f_mapping->host;
147
Andrew Morton965c8e52012-12-17 15:59:39 -0800148 return generic_file_llseek_size(file, offset, whence,
Eric Sandeene8b96eb2012-04-30 13:11:29 -0500149 inode->i_sb->s_maxbytes,
150 i_size_read(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
Andi Kleen9465efc2008-06-27 11:05:24 +0200152EXPORT_SYMBOL(generic_file_llseek);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
jan Blunckae6afc32010-05-26 14:44:48 -0700154/**
Al Viro1bf9d142013-06-16 20:27:42 +0400155 * fixed_size_llseek - llseek implementation for fixed-sized devices
156 * @file: file structure to seek on
157 * @offset: file offset to seek to
158 * @whence: type of seek
159 * @size: size of the file
160 *
161 */
162loff_t fixed_size_llseek(struct file *file, loff_t offset, int whence, loff_t size)
163{
164 switch (whence) {
165 case SEEK_SET: case SEEK_CUR: case SEEK_END:
166 return generic_file_llseek_size(file, offset, whence,
167 size, size);
168 default:
169 return -EINVAL;
170 }
171}
172EXPORT_SYMBOL(fixed_size_llseek);
173
174/**
Al Virob25472f2015-12-05 22:04:48 -0500175 * no_seek_end_llseek - llseek implementation for fixed-sized devices
176 * @file: file structure to seek on
177 * @offset: file offset to seek to
178 * @whence: type of seek
179 *
180 */
181loff_t no_seek_end_llseek(struct file *file, loff_t offset, int whence)
182{
183 switch (whence) {
184 case SEEK_SET: case SEEK_CUR:
185 return generic_file_llseek_size(file, offset, whence,
186 ~0ULL, 0);
187 default:
188 return -EINVAL;
189 }
190}
191EXPORT_SYMBOL(no_seek_end_llseek);
192
193/**
194 * no_seek_end_llseek_size - llseek implementation for fixed-sized devices
195 * @file: file structure to seek on
196 * @offset: file offset to seek to
197 * @whence: type of seek
198 * @size: maximal offset allowed
199 *
200 */
201loff_t no_seek_end_llseek_size(struct file *file, loff_t offset, int whence, loff_t size)
202{
203 switch (whence) {
204 case SEEK_SET: case SEEK_CUR:
205 return generic_file_llseek_size(file, offset, whence,
206 size, 0);
207 default:
208 return -EINVAL;
209 }
210}
211EXPORT_SYMBOL(no_seek_end_llseek_size);
212
213/**
jan Blunckae6afc32010-05-26 14:44:48 -0700214 * noop_llseek - No Operation Performed llseek implementation
215 * @file: file structure to seek on
216 * @offset: file offset to seek to
Andrew Morton965c8e52012-12-17 15:59:39 -0800217 * @whence: type of seek
jan Blunckae6afc32010-05-26 14:44:48 -0700218 *
219 * This is an implementation of ->llseek useable for the rare special case when
220 * userspace expects the seek to succeed but the (device) file is actually not
221 * able to perform the seek. In this case you use noop_llseek() instead of
222 * falling back to the default implementation of ->llseek.
223 */
Andrew Morton965c8e52012-12-17 15:59:39 -0800224loff_t noop_llseek(struct file *file, loff_t offset, int whence)
jan Blunckae6afc32010-05-26 14:44:48 -0700225{
226 return file->f_pos;
227}
228EXPORT_SYMBOL(noop_llseek);
229
Andrew Morton965c8e52012-12-17 15:59:39 -0800230loff_t no_llseek(struct file *file, loff_t offset, int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
232 return -ESPIPE;
233}
234EXPORT_SYMBOL(no_llseek);
235
Andrew Morton965c8e52012-12-17 15:59:39 -0800236loff_t default_llseek(struct file *file, loff_t offset, int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
Al Viro496ad9a2013-01-23 17:07:38 -0500238 struct inode *inode = file_inode(file);
David Sterba16abef02008-04-22 15:09:22 +0200239 loff_t retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Al Viro59551022016-01-22 15:40:57 -0500241 inode_lock(inode);
Andrew Morton965c8e52012-12-17 15:59:39 -0800242 switch (whence) {
Chris Snook7b8e8922007-05-08 00:24:13 -0700243 case SEEK_END:
Josef Bacik982d8162011-07-18 13:21:35 -0400244 offset += i_size_read(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 break;
Chris Snook7b8e8922007-05-08 00:24:13 -0700246 case SEEK_CUR:
Alain Knaff5b6f1eb2008-11-10 17:08:08 -0800247 if (offset == 0) {
248 retval = file->f_pos;
249 goto out;
250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 offset += file->f_pos;
Josef Bacik982d8162011-07-18 13:21:35 -0400252 break;
253 case SEEK_DATA:
254 /*
255 * In the generic case the entire file is data, so as
256 * long as offset isn't at the end of the file then the
257 * offset is data.
258 */
Dan Carpenterbacb2d82011-07-26 17:25:20 +0300259 if (offset >= inode->i_size) {
260 retval = -ENXIO;
261 goto out;
262 }
Josef Bacik982d8162011-07-18 13:21:35 -0400263 break;
264 case SEEK_HOLE:
265 /*
266 * There is a virtual hole at the end of the file, so
267 * as long as offset isn't i_size or larger, return
268 * i_size.
269 */
Dan Carpenterbacb2d82011-07-26 17:25:20 +0300270 if (offset >= inode->i_size) {
271 retval = -ENXIO;
272 goto out;
273 }
Josef Bacik982d8162011-07-18 13:21:35 -0400274 offset = inode->i_size;
275 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 }
277 retval = -EINVAL;
Al Virocccb5a12010-12-17 07:44:05 -0500278 if (offset >= 0 || unsigned_offsets(file)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 if (offset != file->f_pos) {
280 file->f_pos = offset;
281 file->f_version = 0;
282 }
283 retval = offset;
284 }
Alain Knaff5b6f1eb2008-11-10 17:08:08 -0800285out:
Al Viro59551022016-01-22 15:40:57 -0500286 inode_unlock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 return retval;
288}
289EXPORT_SYMBOL(default_llseek);
290
Andrew Morton965c8e52012-12-17 15:59:39 -0800291loff_t vfs_llseek(struct file *file, loff_t offset, int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
293 loff_t (*fn)(struct file *, loff_t, int);
294
295 fn = no_llseek;
296 if (file->f_mode & FMODE_LSEEK) {
Al Viro72c2d532013-09-22 16:27:52 -0400297 if (file->f_op->llseek)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 fn = file->f_op->llseek;
299 }
Andrew Morton965c8e52012-12-17 15:59:39 -0800300 return fn(file, offset, whence);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301}
302EXPORT_SYMBOL(vfs_llseek);
303
Linus Torvalds9c225f22014-03-03 09:36:58 -0800304static inline struct fd fdget_pos(int fd)
305{
Al Virobd2a31d2014-03-04 14:54:22 -0500306 return __to_fd(__fdget_pos(fd));
Linus Torvalds9c225f22014-03-03 09:36:58 -0800307}
308
309static inline void fdput_pos(struct fd f)
310{
311 if (f.flags & FDPUT_POS_UNLOCK)
312 mutex_unlock(&f.file->f_pos_lock);
313 fdput(f);
314}
315
Andrew Morton965c8e52012-12-17 15:59:39 -0800316SYSCALL_DEFINE3(lseek, unsigned int, fd, off_t, offset, unsigned int, whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
318 off_t retval;
Linus Torvalds9c225f22014-03-03 09:36:58 -0800319 struct fd f = fdget_pos(fd);
Al Viro2903ff02012-08-28 12:52:22 -0400320 if (!f.file)
321 return -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323 retval = -EINVAL;
Andrew Morton965c8e52012-12-17 15:59:39 -0800324 if (whence <= SEEK_MAX) {
325 loff_t res = vfs_llseek(f.file, offset, whence);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 retval = res;
327 if (res != (loff_t)retval)
328 retval = -EOVERFLOW; /* LFS: should only happen on 32 bit platforms */
329 }
Linus Torvalds9c225f22014-03-03 09:36:58 -0800330 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 return retval;
332}
333
Al Viro561c6732013-02-24 10:52:26 -0500334#ifdef CONFIG_COMPAT
335COMPAT_SYSCALL_DEFINE3(lseek, unsigned int, fd, compat_off_t, offset, unsigned int, whence)
336{
337 return sys_lseek(fd, offset, whence);
338}
339#endif
340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341#ifdef __ARCH_WANT_SYS_LLSEEK
Heiko Carstens003d7ab2009-01-14 14:14:21 +0100342SYSCALL_DEFINE5(llseek, unsigned int, fd, unsigned long, offset_high,
343 unsigned long, offset_low, loff_t __user *, result,
Andrew Morton965c8e52012-12-17 15:59:39 -0800344 unsigned int, whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
346 int retval;
Eric Biggersd7a15f82014-03-16 14:24:08 -0500347 struct fd f = fdget_pos(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 loff_t offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Al Viro2903ff02012-08-28 12:52:22 -0400350 if (!f.file)
351 return -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353 retval = -EINVAL;
Andrew Morton965c8e52012-12-17 15:59:39 -0800354 if (whence > SEEK_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 goto out_putf;
356
Al Viro2903ff02012-08-28 12:52:22 -0400357 offset = vfs_llseek(f.file, ((loff_t) offset_high << 32) | offset_low,
Andrew Morton965c8e52012-12-17 15:59:39 -0800358 whence);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 retval = (int)offset;
361 if (offset >= 0) {
362 retval = -EFAULT;
363 if (!copy_to_user(result, &offset, sizeof(offset)))
364 retval = 0;
365 }
366out_putf:
Eric Biggersd7a15f82014-03-16 14:24:08 -0500367 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return retval;
369}
370#endif
371
Christoph Hellwigdbe4e192015-01-25 21:11:59 +0100372ssize_t vfs_iter_read(struct file *file, struct iov_iter *iter, loff_t *ppos)
373{
374 struct kiocb kiocb;
375 ssize_t ret;
376
377 if (!file->f_op->read_iter)
378 return -EINVAL;
379
380 init_sync_kiocb(&kiocb, file);
381 kiocb.ki_pos = *ppos;
Christoph Hellwigdbe4e192015-01-25 21:11:59 +0100382
383 iter->type |= READ;
384 ret = file->f_op->read_iter(&kiocb, iter);
Christoph Hellwig599bd192015-02-11 19:59:44 +0100385 BUG_ON(ret == -EIOCBQUEUED);
Christoph Hellwigdbe4e192015-01-25 21:11:59 +0100386 if (ret > 0)
387 *ppos = kiocb.ki_pos;
388 return ret;
389}
390EXPORT_SYMBOL(vfs_iter_read);
391
392ssize_t vfs_iter_write(struct file *file, struct iov_iter *iter, loff_t *ppos)
393{
394 struct kiocb kiocb;
395 ssize_t ret;
396
397 if (!file->f_op->write_iter)
398 return -EINVAL;
399
400 init_sync_kiocb(&kiocb, file);
401 kiocb.ki_pos = *ppos;
Christoph Hellwigdbe4e192015-01-25 21:11:59 +0100402
403 iter->type |= WRITE;
404 ret = file->f_op->write_iter(&kiocb, iter);
Christoph Hellwig599bd192015-02-11 19:59:44 +0100405 BUG_ON(ret == -EIOCBQUEUED);
Christoph Hellwigdbe4e192015-01-25 21:11:59 +0100406 if (ret > 0)
407 *ppos = kiocb.ki_pos;
408 return ret;
409}
410EXPORT_SYMBOL(vfs_iter_write);
411
Linus Torvaldse28cc712006-01-04 16:20:40 -0800412/*
413 * rw_verify_area doesn't like huge counts. We limit
414 * them to something that fits in "int" so that others
415 * won't have to do range checks all the time.
416 */
Al Viro68d70d02013-06-19 15:26:04 +0400417int rw_verify_area(int read_write, struct file *file, const loff_t *ppos, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
419 struct inode *inode;
420 loff_t pos;
James Morrisc43e2592008-01-12 22:05:48 +1100421 int retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Al Viro496ad9a2013-01-23 17:07:38 -0500423 inode = file_inode(file);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800424 if (unlikely((ssize_t) count < 0))
James Morrisc43e2592008-01-12 22:05:48 +1100425 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 pos = *ppos;
Al Virocccb5a12010-12-17 07:44:05 -0500427 if (unlikely(pos < 0)) {
428 if (!unsigned_offsets(file))
429 return retval;
430 if (count >= -pos) /* both values are in 0..LLONG_MAX */
431 return -EOVERFLOW;
432 } else if (unlikely((loff_t) (pos + count) < 0)) {
433 if (!unsigned_offsets(file))
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -0700434 return retval;
435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Jeff Laytonbd61e0a2015-01-16 15:05:55 -0500437 if (unlikely(inode->i_flctx && mandatory_lock(inode))) {
Christoph Hellwigacc15572015-12-03 12:59:49 +0100438 retval = locks_mandatory_area(inode, file, pos, pos + count - 1,
439 read_write == READ ? F_RDLCK : F_WRLCK);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800440 if (retval < 0)
441 return retval;
442 }
James Morrisc43e2592008-01-12 22:05:48 +1100443 retval = security_file_permission(file,
444 read_write == READ ? MAY_READ : MAY_WRITE);
445 if (retval)
446 return retval;
Linus Torvaldse28cc712006-01-04 16:20:40 -0800447 return count > MAX_RW_COUNT ? MAX_RW_COUNT : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
449
Al Viro5d5d5682015-04-03 15:41:18 -0400450static ssize_t new_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
Al Viro293bc982014-02-11 18:37:41 -0500451{
452 struct iovec iov = { .iov_base = buf, .iov_len = len };
453 struct kiocb kiocb;
454 struct iov_iter iter;
455 ssize_t ret;
456
457 init_sync_kiocb(&kiocb, filp);
458 kiocb.ki_pos = *ppos;
Al Viro293bc982014-02-11 18:37:41 -0500459 iov_iter_init(&iter, READ, &iov, 1, len);
460
461 ret = filp->f_op->read_iter(&kiocb, &iter);
Christoph Hellwig599bd192015-02-11 19:59:44 +0100462 BUG_ON(ret == -EIOCBQUEUED);
Al Viro293bc982014-02-11 18:37:41 -0500463 *ppos = kiocb.ki_pos;
464 return ret;
465}
466
Dmitry Kasatkin6fb50322014-11-05 17:01:17 +0200467ssize_t __vfs_read(struct file *file, char __user *buf, size_t count,
468 loff_t *pos)
469{
Dmitry Kasatkin6fb50322014-11-05 17:01:17 +0200470 if (file->f_op->read)
Al Viro3d04c8a2015-04-03 15:09:18 -0400471 return file->f_op->read(file, buf, count, pos);
Dmitry Kasatkin6fb50322014-11-05 17:01:17 +0200472 else if (file->f_op->read_iter)
Al Viro3d04c8a2015-04-03 15:09:18 -0400473 return new_sync_read(file, buf, count, pos);
Dmitry Kasatkin6fb50322014-11-05 17:01:17 +0200474 else
Al Viro3d04c8a2015-04-03 15:09:18 -0400475 return -EINVAL;
Dmitry Kasatkin6fb50322014-11-05 17:01:17 +0200476}
Al Viro3d04c8a2015-04-03 15:09:18 -0400477EXPORT_SYMBOL(__vfs_read);
Dmitry Kasatkin6fb50322014-11-05 17:01:17 +0200478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
480{
481 ssize_t ret;
482
483 if (!(file->f_mode & FMODE_READ))
484 return -EBADF;
Al Viro7f7f25e2014-02-11 17:49:24 -0500485 if (!(file->f_mode & FMODE_CAN_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 return -EINVAL;
487 if (unlikely(!access_ok(VERIFY_WRITE, buf, count)))
488 return -EFAULT;
489
490 ret = rw_verify_area(READ, file, pos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800491 if (ret >= 0) {
492 count = ret;
Dmitry Kasatkin6fb50322014-11-05 17:01:17 +0200493 ret = __vfs_read(file, buf, count, pos);
James Morrisc43e2592008-01-12 22:05:48 +1100494 if (ret > 0) {
Eric Paris2a12a9d2009-12-17 21:24:21 -0500495 fsnotify_access(file);
James Morrisc43e2592008-01-12 22:05:48 +1100496 add_rchar(current, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 }
James Morrisc43e2592008-01-12 22:05:48 +1100498 inc_syscr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 }
500
501 return ret;
502}
503
504EXPORT_SYMBOL(vfs_read);
505
Al Viro5d5d5682015-04-03 15:41:18 -0400506static 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 -0500507{
508 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len };
509 struct kiocb kiocb;
510 struct iov_iter iter;
511 ssize_t ret;
512
513 init_sync_kiocb(&kiocb, filp);
514 kiocb.ki_pos = *ppos;
Al Viro293bc982014-02-11 18:37:41 -0500515 iov_iter_init(&iter, WRITE, &iov, 1, len);
516
517 ret = filp->f_op->write_iter(&kiocb, &iter);
Christoph Hellwig599bd192015-02-11 19:59:44 +0100518 BUG_ON(ret == -EIOCBQUEUED);
Al Virof765b132015-04-06 20:50:38 -0400519 if (ret > 0)
520 *ppos = kiocb.ki_pos;
Al Viro293bc982014-02-11 18:37:41 -0500521 return ret;
522}
523
Al Viro493c84c2015-04-03 15:06:43 -0400524ssize_t __vfs_write(struct file *file, const char __user *p, size_t count,
525 loff_t *pos)
526{
527 if (file->f_op->write)
528 return file->f_op->write(file, p, count, pos);
Al Viro493c84c2015-04-03 15:06:43 -0400529 else if (file->f_op->write_iter)
530 return new_sync_write(file, p, count, pos);
531 else
532 return -EINVAL;
533}
534EXPORT_SYMBOL(__vfs_write);
535
Al Viro06ae43f2013-03-20 13:19:30 -0400536ssize_t __kernel_write(struct file *file, const char *buf, size_t count, loff_t *pos)
537{
538 mm_segment_t old_fs;
539 const char __user *p;
540 ssize_t ret;
541
Al Viro7f7f25e2014-02-11 17:49:24 -0500542 if (!(file->f_mode & FMODE_CAN_WRITE))
Al Viro3e84f482013-03-27 15:20:30 +0000543 return -EINVAL;
544
Al Viro06ae43f2013-03-20 13:19:30 -0400545 old_fs = get_fs();
546 set_fs(get_ds());
547 p = (__force const char __user *)buf;
548 if (count > MAX_RW_COUNT)
549 count = MAX_RW_COUNT;
Al Viro493c84c2015-04-03 15:06:43 -0400550 ret = __vfs_write(file, p, count, pos);
Al Viro06ae43f2013-03-20 13:19:30 -0400551 set_fs(old_fs);
552 if (ret > 0) {
553 fsnotify_modify(file);
554 add_wchar(current, ret);
555 }
556 inc_syscw(current);
557 return ret;
558}
559
Al Viro2ec3a122014-08-19 11:48:09 -0400560EXPORT_SYMBOL(__kernel_write);
561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
563{
564 ssize_t ret;
565
566 if (!(file->f_mode & FMODE_WRITE))
567 return -EBADF;
Al Viro7f7f25e2014-02-11 17:49:24 -0500568 if (!(file->f_mode & FMODE_CAN_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 return -EINVAL;
570 if (unlikely(!access_ok(VERIFY_READ, buf, count)))
571 return -EFAULT;
572
573 ret = rw_verify_area(WRITE, file, pos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800574 if (ret >= 0) {
575 count = ret;
Al Viro03d95eb2013-03-20 13:04:20 -0400576 file_start_write(file);
Al Viro493c84c2015-04-03 15:06:43 -0400577 ret = __vfs_write(file, buf, count, pos);
James Morrisc43e2592008-01-12 22:05:48 +1100578 if (ret > 0) {
Eric Paris2a12a9d2009-12-17 21:24:21 -0500579 fsnotify_modify(file);
James Morrisc43e2592008-01-12 22:05:48 +1100580 add_wchar(current, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 }
James Morrisc43e2592008-01-12 22:05:48 +1100582 inc_syscw(current);
Al Viro03d95eb2013-03-20 13:04:20 -0400583 file_end_write(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
585
586 return ret;
587}
588
589EXPORT_SYMBOL(vfs_write);
590
591static inline loff_t file_pos_read(struct file *file)
592{
593 return file->f_pos;
594}
595
596static inline void file_pos_write(struct file *file, loff_t pos)
597{
598 file->f_pos = pos;
599}
600
Heiko Carstens3cdad422009-01-14 14:14:22 +0100601SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
Linus Torvalds9c225f22014-03-03 09:36:58 -0800603 struct fd f = fdget_pos(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Al Viro2903ff02012-08-28 12:52:22 -0400606 if (f.file) {
607 loff_t pos = file_pos_read(f.file);
608 ret = vfs_read(f.file, buf, count, &pos);
Al Viro5faf1532013-06-15 05:49:36 +0400609 if (ret >= 0)
610 file_pos_write(f.file, pos);
Linus Torvalds9c225f22014-03-03 09:36:58 -0800611 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 return ret;
614}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Heiko Carstens3cdad422009-01-14 14:14:22 +0100616SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf,
617 size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
Linus Torvalds9c225f22014-03-03 09:36:58 -0800619 struct fd f = fdget_pos(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Al Viro2903ff02012-08-28 12:52:22 -0400622 if (f.file) {
623 loff_t pos = file_pos_read(f.file);
624 ret = vfs_write(f.file, buf, count, &pos);
Al Viro5faf1532013-06-15 05:49:36 +0400625 if (ret >= 0)
626 file_pos_write(f.file, pos);
Linus Torvalds9c225f22014-03-03 09:36:58 -0800627 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 }
629
630 return ret;
631}
632
Al Viro4a0fd5b2013-01-21 15:16:58 -0500633SYSCALL_DEFINE4(pread64, unsigned int, fd, 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_PREAD)
646 ret = vfs_read(f.file, buf, count, &pos);
647 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 }
649
650 return ret;
651}
652
Al Viro4a0fd5b2013-01-21 15:16:58 -0500653SYSCALL_DEFINE4(pwrite64, unsigned int, fd, const char __user *, buf,
654 size_t, count, loff_t, pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
Al Viro2903ff02012-08-28 12:52:22 -0400656 struct fd f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659 if (pos < 0)
660 return -EINVAL;
661
Al Viro2903ff02012-08-28 12:52:22 -0400662 f = fdget(fd);
663 if (f.file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -0400665 if (f.file->f_mode & FMODE_PWRITE)
666 ret = vfs_write(f.file, buf, count, &pos);
667 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 }
669
670 return ret;
671}
672
673/*
674 * Reduce an iovec's length in-place. Return the resulting number of segments
675 */
676unsigned long iov_shorten(struct iovec *iov, unsigned long nr_segs, size_t to)
677{
678 unsigned long seg = 0;
679 size_t len = 0;
680
681 while (seg < nr_segs) {
682 seg++;
683 if (len + iov->iov_len >= to) {
684 iov->iov_len = to - len;
685 break;
686 }
687 len += iov->iov_len;
688 iov++;
689 }
690 return seg;
691}
Eric Sandeen19295522008-01-28 23:58:27 -0500692EXPORT_SYMBOL(iov_shorten);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Al Viroac15ac02015-03-20 20:10:21 -0400694static ssize_t do_iter_readv_writev(struct file *filp, struct iov_iter *iter,
Christoph Hellwig793b80e2016-03-03 16:03:58 +0100695 loff_t *ppos, iter_fn_t fn, int flags)
Al Viro293bc982014-02-11 18:37:41 -0500696{
697 struct kiocb kiocb;
Al Viro293bc982014-02-11 18:37:41 -0500698 ssize_t ret;
699
Christoph Hellwig793b80e2016-03-03 16:03:58 +0100700 if (flags)
701 return -EOPNOTSUPP;
702
Al Viro293bc982014-02-11 18:37:41 -0500703 init_sync_kiocb(&kiocb, filp);
704 kiocb.ki_pos = *ppos;
Al Viro293bc982014-02-11 18:37:41 -0500705
Al Viroac15ac02015-03-20 20:10:21 -0400706 ret = fn(&kiocb, iter);
Christoph Hellwig599bd192015-02-11 19:59:44 +0100707 BUG_ON(ret == -EIOCBQUEUED);
Al Viro293bc982014-02-11 18:37:41 -0500708 *ppos = kiocb.ki_pos;
709 return ret;
710}
711
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700712/* Do it by hand, with file-ops */
Al Viroac15ac02015-03-20 20:10:21 -0400713static ssize_t do_loop_readv_writev(struct file *filp, struct iov_iter *iter,
Christoph Hellwig793b80e2016-03-03 16:03:58 +0100714 loff_t *ppos, io_fn_t fn, int flags)
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700715{
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700716 ssize_t ret = 0;
717
Christoph Hellwig793b80e2016-03-03 16:03:58 +0100718 if (flags)
719 return -EOPNOTSUPP;
720
Al Viroac15ac02015-03-20 20:10:21 -0400721 while (iov_iter_count(iter)) {
722 struct iovec iovec = iov_iter_iovec(iter);
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700723 ssize_t nr;
724
Al Viroac15ac02015-03-20 20:10:21 -0400725 nr = fn(filp, iovec.iov_base, iovec.iov_len, ppos);
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700726
727 if (nr < 0) {
728 if (!ret)
729 ret = nr;
730 break;
731 }
732 ret += nr;
Al Viroac15ac02015-03-20 20:10:21 -0400733 if (nr != iovec.iov_len)
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700734 break;
Al Viroac15ac02015-03-20 20:10:21 -0400735 iov_iter_advance(iter, nr);
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700736 }
737
738 return ret;
739}
740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741/* A write operation does a read from user space and vice versa */
742#define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
743
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700744ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
745 unsigned long nr_segs, unsigned long fast_segs,
746 struct iovec *fast_pointer,
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700747 struct iovec **ret_pointer)
Linus Torvalds435f49a2010-10-29 10:36:49 -0700748{
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700749 unsigned long seg;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700750 ssize_t ret;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700751 struct iovec *iov = fast_pointer;
752
Linus Torvalds435f49a2010-10-29 10:36:49 -0700753 /*
754 * SuS says "The readv() function *may* fail if the iovcnt argument
755 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
756 * traditionally returned zero for zero segments, so...
757 */
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700758 if (nr_segs == 0) {
759 ret = 0;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700760 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700761 }
762
Linus Torvalds435f49a2010-10-29 10:36:49 -0700763 /*
764 * First get the "struct iovec" from user memory and
765 * verify all the pointers
766 */
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700767 if (nr_segs > UIO_MAXIOV) {
768 ret = -EINVAL;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700769 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700770 }
771 if (nr_segs > fast_segs) {
Linus Torvalds435f49a2010-10-29 10:36:49 -0700772 iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700773 if (iov == NULL) {
774 ret = -ENOMEM;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700775 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700776 }
Linus Torvalds435f49a2010-10-29 10:36:49 -0700777 }
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700778 if (copy_from_user(iov, uvector, nr_segs*sizeof(*uvector))) {
779 ret = -EFAULT;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700780 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700781 }
782
Linus Torvalds435f49a2010-10-29 10:36:49 -0700783 /*
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700784 * According to the Single Unix Specification we should return EINVAL
785 * if an element length is < 0 when cast to ssize_t or if the
786 * total length would overflow the ssize_t return value of the
787 * system call.
Linus Torvalds435f49a2010-10-29 10:36:49 -0700788 *
789 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
790 * overflow case.
791 */
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700792 ret = 0;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700793 for (seg = 0; seg < nr_segs; seg++) {
794 void __user *buf = iov[seg].iov_base;
795 ssize_t len = (ssize_t)iov[seg].iov_len;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700796
797 /* see if we we're about to use an invalid len or if
798 * it's about to overflow ssize_t */
Linus Torvalds435f49a2010-10-29 10:36:49 -0700799 if (len < 0) {
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700800 ret = -EINVAL;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700801 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700802 }
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700803 if (type >= 0
Christopher Yeohfcf63402011-10-31 17:06:39 -0700804 && unlikely(!access_ok(vrfy_dir(type), buf, len))) {
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700805 ret = -EFAULT;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700806 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700807 }
Linus Torvalds435f49a2010-10-29 10:36:49 -0700808 if (len > MAX_RW_COUNT - ret) {
809 len = MAX_RW_COUNT - ret;
810 iov[seg].iov_len = len;
811 }
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700812 ret += len;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700813 }
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700814out:
815 *ret_pointer = iov;
816 return ret;
817}
818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819static ssize_t do_readv_writev(int type, struct file *file,
820 const struct iovec __user * uvector,
Christoph Hellwig793b80e2016-03-03 16:03:58 +0100821 unsigned long nr_segs, loff_t *pos,
822 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 size_t tot_len;
825 struct iovec iovstack[UIO_FASTIOV];
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700826 struct iovec *iov = iovstack;
Al Viroac15ac02015-03-20 20:10:21 -0400827 struct iov_iter iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 io_fn_t fn;
Al Viro293bc982014-02-11 18:37:41 -0500830 iter_fn_t iter_fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Al Viro0504c072015-03-21 19:40:11 -0400832 ret = import_iovec(type, uvector, nr_segs,
833 ARRAY_SIZE(iovstack), &iov, &iter);
834 if (ret < 0)
835 return ret;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700836
Al Viro0504c072015-03-21 19:40:11 -0400837 tot_len = iov_iter_count(&iter);
838 if (!tot_len)
839 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 ret = rw_verify_area(type, file, pos, tot_len);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800841 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 goto out;
843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 if (type == READ) {
845 fn = file->f_op->read;
Al Viro293bc982014-02-11 18:37:41 -0500846 iter_fn = file->f_op->read_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 } else {
848 fn = (io_fn_t)file->f_op->write;
Al Viro293bc982014-02-11 18:37:41 -0500849 iter_fn = file->f_op->write_iter;
Al Viro03d95eb2013-03-20 13:04:20 -0400850 file_start_write(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 }
852
Al Viro293bc982014-02-11 18:37:41 -0500853 if (iter_fn)
Christoph Hellwig793b80e2016-03-03 16:03:58 +0100854 ret = do_iter_readv_writev(file, &iter, pos, iter_fn, flags);
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700855 else
Christoph Hellwig793b80e2016-03-03 16:03:58 +0100856 ret = do_loop_readv_writev(file, &iter, pos, fn, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Al Viro03d95eb2013-03-20 13:04:20 -0400858 if (type != READ)
859 file_end_write(file);
860
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861out:
Al Viro0504c072015-03-21 19:40:11 -0400862 kfree(iov);
Robert Love0eeca282005-07-12 17:06:03 -0400863 if ((ret + (type == READ)) > 0) {
864 if (type == READ)
Eric Paris2a12a9d2009-12-17 21:24:21 -0500865 fsnotify_access(file);
Robert Love0eeca282005-07-12 17:06:03 -0400866 else
Eric Paris2a12a9d2009-12-17 21:24:21 -0500867 fsnotify_modify(file);
Robert Love0eeca282005-07-12 17:06:03 -0400868 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870}
871
872ssize_t vfs_readv(struct file *file, const struct iovec __user *vec,
Christoph Hellwig793b80e2016-03-03 16:03:58 +0100873 unsigned long vlen, loff_t *pos, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
875 if (!(file->f_mode & FMODE_READ))
876 return -EBADF;
Al Viro7f7f25e2014-02-11 17:49:24 -0500877 if (!(file->f_mode & FMODE_CAN_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 return -EINVAL;
879
Christoph Hellwig793b80e2016-03-03 16:03:58 +0100880 return do_readv_writev(READ, file, vec, vlen, pos, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881}
882
883EXPORT_SYMBOL(vfs_readv);
884
885ssize_t vfs_writev(struct file *file, const struct iovec __user *vec,
Christoph Hellwig793b80e2016-03-03 16:03:58 +0100886 unsigned long vlen, loff_t *pos, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887{
888 if (!(file->f_mode & FMODE_WRITE))
889 return -EBADF;
Al Viro7f7f25e2014-02-11 17:49:24 -0500890 if (!(file->f_mode & FMODE_CAN_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 return -EINVAL;
892
Christoph Hellwig793b80e2016-03-03 16:03:58 +0100893 return do_readv_writev(WRITE, file, vec, vlen, pos, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894}
895
896EXPORT_SYMBOL(vfs_writev);
897
Heiko Carstens3cdad422009-01-14 14:14:22 +0100898SYSCALL_DEFINE3(readv, unsigned long, fd, const struct iovec __user *, vec,
899 unsigned long, vlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
Linus Torvalds9c225f22014-03-03 09:36:58 -0800901 struct fd f = fdget_pos(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Al Viro2903ff02012-08-28 12:52:22 -0400904 if (f.file) {
905 loff_t pos = file_pos_read(f.file);
Christoph Hellwig793b80e2016-03-03 16:03:58 +0100906 ret = vfs_readv(f.file, vec, vlen, &pos, 0);
Al Viro5faf1532013-06-15 05:49:36 +0400907 if (ret >= 0)
908 file_pos_write(f.file, pos);
Linus Torvalds9c225f22014-03-03 09:36:58 -0800909 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 }
911
912 if (ret > 0)
Alexey Dobriyan4b98d112007-02-10 01:46:45 -0800913 add_rchar(current, ret);
914 inc_syscr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 return ret;
916}
917
Heiko Carstens3cdad422009-01-14 14:14:22 +0100918SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec __user *, vec,
919 unsigned long, vlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
Linus Torvalds9c225f22014-03-03 09:36:58 -0800921 struct fd f = fdget_pos(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Al Viro2903ff02012-08-28 12:52:22 -0400924 if (f.file) {
925 loff_t pos = file_pos_read(f.file);
Christoph Hellwig793b80e2016-03-03 16:03:58 +0100926 ret = vfs_writev(f.file, vec, vlen, &pos, 0);
Al Viro5faf1532013-06-15 05:49:36 +0400927 if (ret >= 0)
928 file_pos_write(f.file, pos);
Linus Torvalds9c225f22014-03-03 09:36:58 -0800929 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 }
931
932 if (ret > 0)
Alexey Dobriyan4b98d112007-02-10 01:46:45 -0800933 add_wchar(current, ret);
934 inc_syscw(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 return ret;
936}
937
Linus Torvalds601cc112009-04-03 08:03:22 -0700938static inline loff_t pos_from_hilo(unsigned long high, unsigned long low)
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700939{
Linus Torvalds601cc112009-04-03 08:03:22 -0700940#define HALF_LONG_BITS (BITS_PER_LONG / 2)
941 return (((loff_t)high << HALF_LONG_BITS) << HALF_LONG_BITS) | low;
942}
943
944SYSCALL_DEFINE5(preadv, unsigned long, fd, const struct iovec __user *, vec,
945 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
946{
947 loff_t pos = pos_from_hilo(pos_h, pos_l);
Al Viro2903ff02012-08-28 12:52:22 -0400948 struct fd f;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700949 ssize_t ret = -EBADF;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700950
951 if (pos < 0)
952 return -EINVAL;
953
Al Viro2903ff02012-08-28 12:52:22 -0400954 f = fdget(fd);
955 if (f.file) {
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700956 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -0400957 if (f.file->f_mode & FMODE_PREAD)
Christoph Hellwig793b80e2016-03-03 16:03:58 +0100958 ret = vfs_readv(f.file, vec, vlen, &pos, 0);
Al Viro2903ff02012-08-28 12:52:22 -0400959 fdput(f);
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700960 }
961
962 if (ret > 0)
963 add_rchar(current, ret);
964 inc_syscr(current);
965 return ret;
966}
967
968SYSCALL_DEFINE5(pwritev, unsigned long, fd, const struct iovec __user *, vec,
Linus Torvalds601cc112009-04-03 08:03:22 -0700969 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700970{
Linus Torvalds601cc112009-04-03 08:03:22 -0700971 loff_t pos = pos_from_hilo(pos_h, pos_l);
Al Viro2903ff02012-08-28 12:52:22 -0400972 struct fd f;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700973 ssize_t ret = -EBADF;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700974
975 if (pos < 0)
976 return -EINVAL;
977
Al Viro2903ff02012-08-28 12:52:22 -0400978 f = fdget(fd);
979 if (f.file) {
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700980 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -0400981 if (f.file->f_mode & FMODE_PWRITE)
Christoph Hellwig793b80e2016-03-03 16:03:58 +0100982 ret = vfs_writev(f.file, vec, vlen, &pos, 0);
Al Viro2903ff02012-08-28 12:52:22 -0400983 fdput(f);
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700984 }
985
986 if (ret > 0)
987 add_wchar(current, ret);
988 inc_syscw(current);
989 return ret;
990}
991
Al Viro72ec3512013-03-20 10:42:10 -0400992#ifdef CONFIG_COMPAT
993
994static ssize_t compat_do_readv_writev(int type, struct file *file,
995 const struct compat_iovec __user *uvector,
Christoph Hellwig793b80e2016-03-03 16:03:58 +0100996 unsigned long nr_segs, loff_t *pos,
997 int flags)
Al Viro72ec3512013-03-20 10:42:10 -0400998{
999 compat_ssize_t tot_len;
1000 struct iovec iovstack[UIO_FASTIOV];
1001 struct iovec *iov = iovstack;
Al Viroac15ac02015-03-20 20:10:21 -04001002 struct iov_iter iter;
Al Viro72ec3512013-03-20 10:42:10 -04001003 ssize_t ret;
1004 io_fn_t fn;
Al Viro293bc982014-02-11 18:37:41 -05001005 iter_fn_t iter_fn;
Al Viro72ec3512013-03-20 10:42:10 -04001006
Al Viro0504c072015-03-21 19:40:11 -04001007 ret = compat_import_iovec(type, uvector, nr_segs,
1008 UIO_FASTIOV, &iov, &iter);
1009 if (ret < 0)
1010 return ret;
Al Viro72ec3512013-03-20 10:42:10 -04001011
Al Viro0504c072015-03-21 19:40:11 -04001012 tot_len = iov_iter_count(&iter);
1013 if (!tot_len)
1014 goto out;
Al Viro72ec3512013-03-20 10:42:10 -04001015 ret = rw_verify_area(type, file, pos, tot_len);
1016 if (ret < 0)
1017 goto out;
1018
Al Viro72ec3512013-03-20 10:42:10 -04001019 if (type == READ) {
1020 fn = file->f_op->read;
Al Viro293bc982014-02-11 18:37:41 -05001021 iter_fn = file->f_op->read_iter;
Al Viro72ec3512013-03-20 10:42:10 -04001022 } else {
1023 fn = (io_fn_t)file->f_op->write;
Al Viro293bc982014-02-11 18:37:41 -05001024 iter_fn = file->f_op->write_iter;
Al Viro03d95eb2013-03-20 13:04:20 -04001025 file_start_write(file);
Al Viro72ec3512013-03-20 10:42:10 -04001026 }
1027
Al Viro293bc982014-02-11 18:37:41 -05001028 if (iter_fn)
Christoph Hellwig793b80e2016-03-03 16:03:58 +01001029 ret = do_iter_readv_writev(file, &iter, pos, iter_fn, flags);
Al Viro03d95eb2013-03-20 13:04:20 -04001030 else
Christoph Hellwig793b80e2016-03-03 16:03:58 +01001031 ret = do_loop_readv_writev(file, &iter, pos, fn, flags);
Al Viro72ec3512013-03-20 10:42:10 -04001032
Al Viro03d95eb2013-03-20 13:04:20 -04001033 if (type != READ)
1034 file_end_write(file);
1035
Al Viro72ec3512013-03-20 10:42:10 -04001036out:
Al Viro0504c072015-03-21 19:40:11 -04001037 kfree(iov);
Al Viro72ec3512013-03-20 10:42:10 -04001038 if ((ret + (type == READ)) > 0) {
1039 if (type == READ)
1040 fsnotify_access(file);
1041 else
1042 fsnotify_modify(file);
1043 }
1044 return ret;
1045}
1046
1047static size_t compat_readv(struct file *file,
1048 const struct compat_iovec __user *vec,
1049 unsigned long vlen, loff_t *pos)
1050{
1051 ssize_t ret = -EBADF;
1052
1053 if (!(file->f_mode & FMODE_READ))
1054 goto out;
1055
1056 ret = -EINVAL;
Al Viro7f7f25e2014-02-11 17:49:24 -05001057 if (!(file->f_mode & FMODE_CAN_READ))
Al Viro72ec3512013-03-20 10:42:10 -04001058 goto out;
1059
Christoph Hellwig793b80e2016-03-03 16:03:58 +01001060 ret = compat_do_readv_writev(READ, file, vec, vlen, pos, 0);
Al Viro72ec3512013-03-20 10:42:10 -04001061
1062out:
1063 if (ret > 0)
1064 add_rchar(current, ret);
1065 inc_syscr(current);
1066 return ret;
1067}
1068
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001069COMPAT_SYSCALL_DEFINE3(readv, compat_ulong_t, fd,
Al Viro72ec3512013-03-20 10:42:10 -04001070 const struct compat_iovec __user *,vec,
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001071 compat_ulong_t, vlen)
Al Viro72ec3512013-03-20 10:42:10 -04001072{
Linus Torvalds9c225f22014-03-03 09:36:58 -08001073 struct fd f = fdget_pos(fd);
Al Viro72ec3512013-03-20 10:42:10 -04001074 ssize_t ret;
1075 loff_t pos;
1076
1077 if (!f.file)
1078 return -EBADF;
1079 pos = f.file->f_pos;
1080 ret = compat_readv(f.file, vec, vlen, &pos);
Al Viro5faf1532013-06-15 05:49:36 +04001081 if (ret >= 0)
1082 f.file->f_pos = pos;
Linus Torvalds9c225f22014-03-03 09:36:58 -08001083 fdput_pos(f);
Al Viro72ec3512013-03-20 10:42:10 -04001084 return ret;
1085}
1086
Heiko Carstens378a10f2014-03-05 10:43:51 +01001087static long __compat_sys_preadv64(unsigned long fd,
1088 const struct compat_iovec __user *vec,
1089 unsigned long vlen, loff_t pos)
Al Viro72ec3512013-03-20 10:42:10 -04001090{
1091 struct fd f;
1092 ssize_t ret;
1093
1094 if (pos < 0)
1095 return -EINVAL;
1096 f = fdget(fd);
1097 if (!f.file)
1098 return -EBADF;
1099 ret = -ESPIPE;
1100 if (f.file->f_mode & FMODE_PREAD)
1101 ret = compat_readv(f.file, vec, vlen, &pos);
1102 fdput(f);
1103 return ret;
1104}
1105
Heiko Carstens378a10f2014-03-05 10:43:51 +01001106#ifdef __ARCH_WANT_COMPAT_SYS_PREADV64
1107COMPAT_SYSCALL_DEFINE4(preadv64, unsigned long, fd,
1108 const struct compat_iovec __user *,vec,
1109 unsigned long, vlen, loff_t, pos)
1110{
1111 return __compat_sys_preadv64(fd, vec, vlen, pos);
1112}
1113#endif
1114
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001115COMPAT_SYSCALL_DEFINE5(preadv, compat_ulong_t, fd,
Al Viro72ec3512013-03-20 10:42:10 -04001116 const struct compat_iovec __user *,vec,
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001117 compat_ulong_t, vlen, u32, pos_low, u32, pos_high)
Al Viro72ec3512013-03-20 10:42:10 -04001118{
1119 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
Heiko Carstens378a10f2014-03-05 10:43:51 +01001120
1121 return __compat_sys_preadv64(fd, vec, vlen, pos);
Al Viro72ec3512013-03-20 10:42:10 -04001122}
1123
1124static size_t compat_writev(struct file *file,
1125 const struct compat_iovec __user *vec,
1126 unsigned long vlen, loff_t *pos)
1127{
1128 ssize_t ret = -EBADF;
1129
1130 if (!(file->f_mode & FMODE_WRITE))
1131 goto out;
1132
1133 ret = -EINVAL;
Al Viro7f7f25e2014-02-11 17:49:24 -05001134 if (!(file->f_mode & FMODE_CAN_WRITE))
Al Viro72ec3512013-03-20 10:42:10 -04001135 goto out;
1136
Christoph Hellwig793b80e2016-03-03 16:03:58 +01001137 ret = compat_do_readv_writev(WRITE, file, vec, vlen, pos, 0);
Al Viro72ec3512013-03-20 10:42:10 -04001138
1139out:
1140 if (ret > 0)
1141 add_wchar(current, ret);
1142 inc_syscw(current);
1143 return ret;
1144}
1145
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001146COMPAT_SYSCALL_DEFINE3(writev, compat_ulong_t, fd,
Al Viro72ec3512013-03-20 10:42:10 -04001147 const struct compat_iovec __user *, vec,
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001148 compat_ulong_t, vlen)
Al Viro72ec3512013-03-20 10:42:10 -04001149{
Linus Torvalds9c225f22014-03-03 09:36:58 -08001150 struct fd f = fdget_pos(fd);
Al Viro72ec3512013-03-20 10:42:10 -04001151 ssize_t ret;
1152 loff_t pos;
1153
1154 if (!f.file)
1155 return -EBADF;
1156 pos = f.file->f_pos;
1157 ret = compat_writev(f.file, vec, vlen, &pos);
Al Viro5faf1532013-06-15 05:49:36 +04001158 if (ret >= 0)
1159 f.file->f_pos = pos;
Linus Torvalds9c225f22014-03-03 09:36:58 -08001160 fdput_pos(f);
Al Viro72ec3512013-03-20 10:42:10 -04001161 return ret;
1162}
1163
Heiko Carstens378a10f2014-03-05 10:43:51 +01001164static long __compat_sys_pwritev64(unsigned long fd,
1165 const struct compat_iovec __user *vec,
1166 unsigned long vlen, loff_t pos)
Al Viro72ec3512013-03-20 10:42:10 -04001167{
1168 struct fd f;
1169 ssize_t ret;
1170
1171 if (pos < 0)
1172 return -EINVAL;
1173 f = fdget(fd);
1174 if (!f.file)
1175 return -EBADF;
1176 ret = -ESPIPE;
1177 if (f.file->f_mode & FMODE_PWRITE)
1178 ret = compat_writev(f.file, vec, vlen, &pos);
1179 fdput(f);
1180 return ret;
1181}
1182
Heiko Carstens378a10f2014-03-05 10:43:51 +01001183#ifdef __ARCH_WANT_COMPAT_SYS_PWRITEV64
1184COMPAT_SYSCALL_DEFINE4(pwritev64, unsigned long, fd,
1185 const struct compat_iovec __user *,vec,
1186 unsigned long, vlen, loff_t, pos)
1187{
1188 return __compat_sys_pwritev64(fd, vec, vlen, pos);
1189}
1190#endif
1191
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001192COMPAT_SYSCALL_DEFINE5(pwritev, compat_ulong_t, fd,
Al Viro72ec3512013-03-20 10:42:10 -04001193 const struct compat_iovec __user *,vec,
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001194 compat_ulong_t, vlen, u32, pos_low, u32, pos_high)
Al Viro72ec3512013-03-20 10:42:10 -04001195{
1196 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
Heiko Carstens378a10f2014-03-05 10:43:51 +01001197
1198 return __compat_sys_pwritev64(fd, vec, vlen, pos);
Al Viro72ec3512013-03-20 10:42:10 -04001199}
1200#endif
1201
Al Viro19f4fc32013-02-24 02:17:03 -05001202static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,
1203 size_t count, loff_t max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204{
Al Viro2903ff02012-08-28 12:52:22 -04001205 struct fd in, out;
1206 struct inode *in_inode, *out_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 loff_t pos;
Al Viro7995bd22013-06-20 18:58:36 +04001208 loff_t out_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 ssize_t retval;
Al Viro2903ff02012-08-28 12:52:22 -04001210 int fl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
1212 /*
1213 * Get input file, and verify that it is ok..
1214 */
1215 retval = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -04001216 in = fdget(in_fd);
1217 if (!in.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 goto out;
Al Viro2903ff02012-08-28 12:52:22 -04001219 if (!(in.file->f_mode & FMODE_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 goto fput_in;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 retval = -ESPIPE;
Al Viro7995bd22013-06-20 18:58:36 +04001222 if (!ppos) {
1223 pos = in.file->f_pos;
1224 } else {
1225 pos = *ppos;
Al Viro2903ff02012-08-28 12:52:22 -04001226 if (!(in.file->f_mode & FMODE_PREAD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 goto fput_in;
Al Viro7995bd22013-06-20 18:58:36 +04001228 }
1229 retval = rw_verify_area(READ, in.file, &pos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -08001230 if (retval < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 goto fput_in;
Linus Torvaldse28cc712006-01-04 16:20:40 -08001232 count = retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 /*
1235 * Get output file, and verify that it is ok..
1236 */
1237 retval = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -04001238 out = fdget(out_fd);
1239 if (!out.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 goto fput_in;
Al Viro2903ff02012-08-28 12:52:22 -04001241 if (!(out.file->f_mode & FMODE_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 goto fput_out;
1243 retval = -EINVAL;
Al Viro496ad9a2013-01-23 17:07:38 -05001244 in_inode = file_inode(in.file);
1245 out_inode = file_inode(out.file);
Al Viro7995bd22013-06-20 18:58:36 +04001246 out_pos = out.file->f_pos;
1247 retval = rw_verify_area(WRITE, out.file, &out_pos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -08001248 if (retval < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 goto fput_out;
Linus Torvaldse28cc712006-01-04 16:20:40 -08001250 count = retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 if (!max)
1253 max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes);
1254
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 if (unlikely(pos + count > max)) {
1256 retval = -EOVERFLOW;
1257 if (pos >= max)
1258 goto fput_out;
1259 count = max - pos;
1260 }
1261
Jens Axboed96e6e72007-06-11 12:18:52 +02001262 fl = 0;
Jens Axboe534f2aa2007-06-01 14:52:37 +02001263#if 0
Jens Axboed96e6e72007-06-11 12:18:52 +02001264 /*
1265 * We need to debate whether we can enable this or not. The
1266 * man page documents EAGAIN return for the output at least,
1267 * and the application is arguably buggy if it doesn't expect
1268 * EAGAIN on a non-blocking file descriptor.
1269 */
Al Viro2903ff02012-08-28 12:52:22 -04001270 if (in.file->f_flags & O_NONBLOCK)
Jens Axboed96e6e72007-06-11 12:18:52 +02001271 fl = SPLICE_F_NONBLOCK;
Jens Axboe534f2aa2007-06-01 14:52:37 +02001272#endif
Al Viro50cd2c52013-05-23 20:10:34 -04001273 file_start_write(out.file);
Al Viro7995bd22013-06-20 18:58:36 +04001274 retval = do_splice_direct(in.file, &pos, out.file, &out_pos, count, fl);
Al Viro50cd2c52013-05-23 20:10:34 -04001275 file_end_write(out.file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
1277 if (retval > 0) {
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08001278 add_rchar(current, retval);
1279 add_wchar(current, retval);
Scott Wolchoka68c2f12012-12-20 15:05:52 -08001280 fsnotify_access(in.file);
1281 fsnotify_modify(out.file);
Al Viro7995bd22013-06-20 18:58:36 +04001282 out.file->f_pos = out_pos;
1283 if (ppos)
1284 *ppos = pos;
1285 else
1286 in.file->f_pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08001289 inc_syscr(current);
1290 inc_syscw(current);
Al Viro7995bd22013-06-20 18:58:36 +04001291 if (pos > max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 retval = -EOVERFLOW;
1293
1294fput_out:
Al Viro2903ff02012-08-28 12:52:22 -04001295 fdput(out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296fput_in:
Al Viro2903ff02012-08-28 12:52:22 -04001297 fdput(in);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298out:
1299 return retval;
1300}
1301
Heiko Carstens002c8972009-01-14 14:14:18 +01001302SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd, off_t __user *, offset, size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303{
1304 loff_t pos;
1305 off_t off;
1306 ssize_t ret;
1307
1308 if (offset) {
1309 if (unlikely(get_user(off, offset)))
1310 return -EFAULT;
1311 pos = off;
1312 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
1313 if (unlikely(put_user(pos, offset)))
1314 return -EFAULT;
1315 return ret;
1316 }
1317
1318 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1319}
1320
Heiko Carstens002c8972009-01-14 14:14:18 +01001321SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd, loff_t __user *, offset, size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322{
1323 loff_t pos;
1324 ssize_t ret;
1325
1326 if (offset) {
1327 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
1328 return -EFAULT;
1329 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
1330 if (unlikely(put_user(pos, offset)))
1331 return -EFAULT;
1332 return ret;
1333 }
1334
1335 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1336}
Al Viro19f4fc32013-02-24 02:17:03 -05001337
1338#ifdef CONFIG_COMPAT
1339COMPAT_SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd,
1340 compat_off_t __user *, offset, compat_size_t, count)
1341{
1342 loff_t pos;
1343 off_t off;
1344 ssize_t ret;
1345
1346 if (offset) {
1347 if (unlikely(get_user(off, offset)))
1348 return -EFAULT;
1349 pos = off;
1350 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
1351 if (unlikely(put_user(pos, offset)))
1352 return -EFAULT;
1353 return ret;
1354 }
1355
1356 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1357}
1358
1359COMPAT_SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd,
1360 compat_loff_t __user *, offset, compat_size_t, count)
1361{
1362 loff_t pos;
1363 ssize_t ret;
1364
1365 if (offset) {
1366 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
1367 return -EFAULT;
1368 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
1369 if (unlikely(put_user(pos, offset)))
1370 return -EFAULT;
1371 return ret;
1372 }
1373
1374 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1375}
1376#endif
Zach Brown29732932015-11-10 16:53:30 -05001377
1378/*
1379 * copy_file_range() differs from regular file read and write in that it
1380 * specifically allows return partial success. When it does so is up to
1381 * the copy_file_range method.
1382 */
1383ssize_t vfs_copy_file_range(struct file *file_in, loff_t pos_in,
1384 struct file *file_out, loff_t pos_out,
1385 size_t len, unsigned int flags)
1386{
1387 struct inode *inode_in = file_inode(file_in);
1388 struct inode *inode_out = file_inode(file_out);
1389 ssize_t ret;
1390
1391 if (flags != 0)
1392 return -EINVAL;
1393
1394 /* copy_file_range allows full ssize_t len, ignoring MAX_RW_COUNT */
1395 ret = rw_verify_area(READ, file_in, &pos_in, len);
1396 if (ret >= 0)
1397 ret = rw_verify_area(WRITE, file_out, &pos_out, len);
1398 if (ret < 0)
1399 return ret;
1400
1401 if (!(file_in->f_mode & FMODE_READ) ||
1402 !(file_out->f_mode & FMODE_WRITE) ||
Anna Schumakereac70052015-11-10 16:53:33 -05001403 (file_out->f_flags & O_APPEND))
Zach Brown29732932015-11-10 16:53:30 -05001404 return -EBADF;
1405
1406 /* this could be relaxed once a method supports cross-fs copies */
1407 if (inode_in->i_sb != inode_out->i_sb)
1408 return -EXDEV;
1409
1410 if (len == 0)
1411 return 0;
1412
1413 ret = mnt_want_write_file(file_out);
1414 if (ret)
1415 return ret;
1416
Anna Schumakereac70052015-11-10 16:53:33 -05001417 ret = -EOPNOTSUPP;
1418 if (file_out->f_op->copy_file_range)
1419 ret = file_out->f_op->copy_file_range(file_in, pos_in, file_out,
1420 pos_out, len, flags);
1421 if (ret == -EOPNOTSUPP)
1422 ret = do_splice_direct(file_in, &pos_in, file_out, &pos_out,
1423 len > MAX_RW_COUNT ? MAX_RW_COUNT : len, 0);
1424
Zach Brown29732932015-11-10 16:53:30 -05001425 if (ret > 0) {
1426 fsnotify_access(file_in);
1427 add_rchar(current, ret);
1428 fsnotify_modify(file_out);
1429 add_wchar(current, ret);
1430 }
1431 inc_syscr(current);
1432 inc_syscw(current);
1433
1434 mnt_drop_write_file(file_out);
1435
1436 return ret;
1437}
1438EXPORT_SYMBOL(vfs_copy_file_range);
1439
1440SYSCALL_DEFINE6(copy_file_range, int, fd_in, loff_t __user *, off_in,
1441 int, fd_out, loff_t __user *, off_out,
1442 size_t, len, unsigned int, flags)
1443{
1444 loff_t pos_in;
1445 loff_t pos_out;
1446 struct fd f_in;
1447 struct fd f_out;
1448 ssize_t ret = -EBADF;
1449
1450 f_in = fdget(fd_in);
1451 if (!f_in.file)
1452 goto out2;
1453
1454 f_out = fdget(fd_out);
1455 if (!f_out.file)
1456 goto out1;
1457
1458 ret = -EFAULT;
1459 if (off_in) {
1460 if (copy_from_user(&pos_in, off_in, sizeof(loff_t)))
1461 goto out;
1462 } else {
1463 pos_in = f_in.file->f_pos;
1464 }
1465
1466 if (off_out) {
1467 if (copy_from_user(&pos_out, off_out, sizeof(loff_t)))
1468 goto out;
1469 } else {
1470 pos_out = f_out.file->f_pos;
1471 }
1472
1473 ret = vfs_copy_file_range(f_in.file, pos_in, f_out.file, pos_out, len,
1474 flags);
1475 if (ret > 0) {
1476 pos_in += ret;
1477 pos_out += ret;
1478
1479 if (off_in) {
1480 if (copy_to_user(off_in, &pos_in, sizeof(loff_t)))
1481 ret = -EFAULT;
1482 } else {
1483 f_in.file->f_pos = pos_in;
1484 }
1485
1486 if (off_out) {
1487 if (copy_to_user(off_out, &pos_out, sizeof(loff_t)))
1488 ret = -EFAULT;
1489 } else {
1490 f_out.file->f_pos = pos_out;
1491 }
1492 }
1493
1494out:
1495 fdput(f_out);
1496out1:
1497 fdput(f_in);
1498out2:
1499 return ret;
1500}
Christoph Hellwig04b38d62015-12-03 12:59:50 +01001501
1502static int clone_verify_area(struct file *file, loff_t pos, u64 len, bool write)
1503{
1504 struct inode *inode = file_inode(file);
1505
1506 if (unlikely(pos < 0))
1507 return -EINVAL;
1508
1509 if (unlikely((loff_t) (pos + len) < 0))
1510 return -EINVAL;
1511
1512 if (unlikely(inode->i_flctx && mandatory_lock(inode))) {
1513 loff_t end = len ? pos + len - 1 : OFFSET_MAX;
1514 int retval;
1515
1516 retval = locks_mandatory_area(inode, file, pos, end,
1517 write ? F_WRLCK : F_RDLCK);
1518 if (retval < 0)
1519 return retval;
1520 }
1521
1522 return security_file_permission(file, write ? MAY_WRITE : MAY_READ);
1523}
1524
1525int vfs_clone_file_range(struct file *file_in, loff_t pos_in,
1526 struct file *file_out, loff_t pos_out, u64 len)
1527{
1528 struct inode *inode_in = file_inode(file_in);
1529 struct inode *inode_out = file_inode(file_out);
1530 int ret;
1531
1532 if (inode_in->i_sb != inode_out->i_sb ||
1533 file_in->f_path.mnt != file_out->f_path.mnt)
1534 return -EXDEV;
1535
1536 if (S_ISDIR(inode_in->i_mode) || S_ISDIR(inode_out->i_mode))
1537 return -EISDIR;
1538 if (!S_ISREG(inode_in->i_mode) || !S_ISREG(inode_out->i_mode))
Darrick J. Wongd79bdd52015-12-19 00:55:52 -08001539 return -EINVAL;
Christoph Hellwig04b38d62015-12-03 12:59:50 +01001540
1541 if (!(file_in->f_mode & FMODE_READ) ||
1542 !(file_out->f_mode & FMODE_WRITE) ||
1543 (file_out->f_flags & O_APPEND) ||
1544 !file_in->f_op->clone_file_range)
1545 return -EBADF;
1546
1547 ret = clone_verify_area(file_in, pos_in, len, false);
1548 if (ret)
1549 return ret;
1550
1551 ret = clone_verify_area(file_out, pos_out, len, true);
1552 if (ret)
1553 return ret;
1554
1555 if (pos_in + len > i_size_read(inode_in))
1556 return -EINVAL;
1557
1558 ret = mnt_want_write_file(file_out);
1559 if (ret)
1560 return ret;
1561
1562 ret = file_in->f_op->clone_file_range(file_in, pos_in,
1563 file_out, pos_out, len);
1564 if (!ret) {
1565 fsnotify_access(file_in);
1566 fsnotify_modify(file_out);
1567 }
1568
1569 mnt_drop_write_file(file_out);
1570 return ret;
1571}
1572EXPORT_SYMBOL(vfs_clone_file_range);
Darrick J. Wong54dbc152015-12-19 00:55:59 -08001573
1574int vfs_dedupe_file_range(struct file *file, struct file_dedupe_range *same)
1575{
1576 struct file_dedupe_range_info *info;
1577 struct inode *src = file_inode(file);
1578 u64 off;
1579 u64 len;
1580 int i;
1581 int ret;
1582 bool is_admin = capable(CAP_SYS_ADMIN);
1583 u16 count = same->dest_count;
1584 struct file *dst_file;
1585 loff_t dst_off;
1586 ssize_t deduped;
1587
1588 if (!(file->f_mode & FMODE_READ))
1589 return -EINVAL;
1590
1591 if (same->reserved1 || same->reserved2)
1592 return -EINVAL;
1593
1594 off = same->src_offset;
1595 len = same->src_length;
1596
1597 ret = -EISDIR;
1598 if (S_ISDIR(src->i_mode))
1599 goto out;
1600
1601 ret = -EINVAL;
1602 if (!S_ISREG(src->i_mode))
1603 goto out;
1604
1605 ret = clone_verify_area(file, off, len, false);
1606 if (ret < 0)
1607 goto out;
1608 ret = 0;
1609
1610 /* pre-format output fields to sane values */
1611 for (i = 0; i < count; i++) {
1612 same->info[i].bytes_deduped = 0ULL;
1613 same->info[i].status = FILE_DEDUPE_RANGE_SAME;
1614 }
1615
1616 for (i = 0, info = same->info; i < count; i++, info++) {
1617 struct inode *dst;
1618 struct fd dst_fd = fdget(info->dest_fd);
1619
1620 dst_file = dst_fd.file;
1621 if (!dst_file) {
1622 info->status = -EBADF;
1623 goto next_loop;
1624 }
1625 dst = file_inode(dst_file);
1626
1627 ret = mnt_want_write_file(dst_file);
1628 if (ret) {
1629 info->status = ret;
1630 goto next_loop;
1631 }
1632
1633 dst_off = info->dest_offset;
1634 ret = clone_verify_area(dst_file, dst_off, len, true);
1635 if (ret < 0) {
1636 info->status = ret;
1637 goto next_file;
1638 }
1639 ret = 0;
1640
1641 if (info->reserved) {
1642 info->status = -EINVAL;
1643 } else if (!(is_admin || (dst_file->f_mode & FMODE_WRITE))) {
1644 info->status = -EINVAL;
1645 } else if (file->f_path.mnt != dst_file->f_path.mnt) {
1646 info->status = -EXDEV;
1647 } else if (S_ISDIR(dst->i_mode)) {
1648 info->status = -EISDIR;
1649 } else if (dst_file->f_op->dedupe_file_range == NULL) {
1650 info->status = -EINVAL;
1651 } else {
1652 deduped = dst_file->f_op->dedupe_file_range(file, off,
1653 len, dst_file,
1654 info->dest_offset);
1655 if (deduped == -EBADE)
1656 info->status = FILE_DEDUPE_RANGE_DIFFERS;
1657 else if (deduped < 0)
1658 info->status = deduped;
1659 else
1660 info->bytes_deduped += deduped;
1661 }
1662
1663next_file:
1664 mnt_drop_write_file(dst_file);
1665next_loop:
1666 fdput(dst_fd);
Darrick J. Wonge62e5602016-01-22 16:58:28 -08001667
1668 if (fatal_signal_pending(current))
1669 goto out;
Darrick J. Wong54dbc152015-12-19 00:55:59 -08001670 }
1671
1672out:
1673 return ret;
1674}
1675EXPORT_SYMBOL(vfs_dedupe_file_range);