blob: dc60075653a84b9f45299db074719fe56861c1c7 [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
Ingo Molnarb12fb7f2017-02-08 18:51:33 +01007#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/stat.h>
Ingo Molnarb12fb7f2017-02-08 18:51:33 +01009#include <linux/sched/xacct.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/fcntl.h>
11#include <linux/file.h>
12#include <linux/uio.h>
Robert Love0eeca282005-07-12 17:06:03 -040013#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/security.h>
Paul Gortmaker630d9c42011-11-16 23:57:37 -050015#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/syscalls.h>
Linus Torvaldse28cc712006-01-04 16:20:40 -080017#include <linux/pagemap.h>
Jens Axboed6b29d72007-06-04 09:59:47 +020018#include <linux/splice.h>
Al Viro561c6732013-02-24 10:52:26 -050019#include <linux/compat.h>
Zach Brown29732932015-11-10 16:53:30 -050020#include <linux/mount.h>
Wouter van Kesteren2feb55f2016-02-16 22:20:59 +010021#include <linux/fs.h>
Al Viro06ae43f2013-03-20 13:19:30 -040022#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080024#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/unistd.h>
26
Al Viroc0bd14af2013-05-04 15:00:54 -040027typedef ssize_t (*io_fn_t)(struct file *, char __user *, size_t, loff_t *);
Al Viro293bc982014-02-11 18:37:41 -050028typedef ssize_t (*iter_fn_t)(struct kiocb *, struct iov_iter *);
Al Viroc0bd14af2013-05-04 15:00:54 -040029
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080030const struct file_operations generic_ro_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 .llseek = generic_file_llseek,
Al Viroaad4f8b2014-04-02 14:33:16 -040032 .read_iter = generic_file_read_iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 .mmap = generic_file_readonly_mmap,
Jens Axboe534f2aa2007-06-01 14:52:37 +020034 .splice_read = generic_file_splice_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -070035};
36
37EXPORT_SYMBOL(generic_ro_fops);
38
Al Virocccb5a12010-12-17 07:44:05 -050039static inline int unsigned_offsets(struct file *file)
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -070040{
Al Virocccb5a12010-12-17 07:44:05 -050041 return file->f_mode & FMODE_UNSIGNED_OFFSET;
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -070042}
43
Jie Liu46a1c2c2013-06-25 12:02:13 +080044/**
45 * vfs_setpos - update the file offset for lseek
46 * @file: file structure in question
47 * @offset: file offset to seek to
48 * @maxsize: maximum file size
49 *
50 * This is a low-level filesystem helper for updating the file offset to
51 * the value specified by @offset if the given offset is valid and it is
52 * not equal to the current file offset.
53 *
54 * Return the specified offset on success and -EINVAL on invalid offset.
55 */
56loff_t vfs_setpos(struct file *file, loff_t offset, loff_t maxsize)
Andi Kleenef3d0fd2011-09-15 16:06:48 -070057{
58 if (offset < 0 && !unsigned_offsets(file))
59 return -EINVAL;
60 if (offset > maxsize)
61 return -EINVAL;
62
63 if (offset != file->f_pos) {
64 file->f_pos = offset;
65 file->f_version = 0;
66 }
67 return offset;
68}
Jie Liu46a1c2c2013-06-25 12:02:13 +080069EXPORT_SYMBOL(vfs_setpos);
Andi Kleenef3d0fd2011-09-15 16:06:48 -070070
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020071/**
Andi Kleen57604952011-09-15 16:06:50 -070072 * generic_file_llseek_size - generic llseek implementation for regular files
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020073 * @file: file structure to seek on
74 * @offset: file offset to seek to
Andrew Morton965c8e52012-12-17 15:59:39 -080075 * @whence: type of seek
Eric Sandeene8b96eb2012-04-30 13:11:29 -050076 * @size: max size of this file in file system
77 * @eof: offset used for SEEK_END position
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020078 *
Andi Kleen57604952011-09-15 16:06:50 -070079 * This is a variant of generic_file_llseek that allows passing in a custom
Eric Sandeene8b96eb2012-04-30 13:11:29 -050080 * maximum file size and a custom EOF position, for e.g. hashed directories
Andi Kleenef3d0fd2011-09-15 16:06:48 -070081 *
82 * Synchronization:
Andi Kleen57604952011-09-15 16:06:50 -070083 * SEEK_SET and SEEK_END are unsynchronized (but atomic on 64bit platforms)
Andi Kleenef3d0fd2011-09-15 16:06:48 -070084 * SEEK_CUR is synchronized against other SEEK_CURs, but not read/writes.
85 * read/writes behave like SEEK_SET against seeks.
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020086 */
Andi Kleen9465efc2008-06-27 11:05:24 +020087loff_t
Andrew Morton965c8e52012-12-17 15:59:39 -080088generic_file_llseek_size(struct file *file, loff_t offset, int whence,
Eric Sandeene8b96eb2012-04-30 13:11:29 -050089 loff_t maxsize, loff_t eof)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
Andrew Morton965c8e52012-12-17 15:59:39 -080091 switch (whence) {
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020092 case SEEK_END:
Eric Sandeene8b96eb2012-04-30 13:11:29 -050093 offset += eof;
Christoph Hellwig3a8cff42008-08-11 15:37:17 +020094 break;
95 case SEEK_CUR:
Alain Knaff5b6f1eb2008-11-10 17:08:08 -080096 /*
97 * Here we special-case the lseek(fd, 0, SEEK_CUR)
98 * position-querying operation. Avoid rewriting the "same"
99 * f_pos value back to the file because a concurrent read(),
100 * write() or lseek() might have altered it
101 */
102 if (offset == 0)
103 return file->f_pos;
Andi Kleenef3d0fd2011-09-15 16:06:48 -0700104 /*
105 * f_lock protects against read/modify/write race with other
106 * SEEK_CURs. Note that parallel writes and reads behave
107 * like SEEK_SET.
108 */
109 spin_lock(&file->f_lock);
Jie Liu46a1c2c2013-06-25 12:02:13 +0800110 offset = vfs_setpos(file, file->f_pos + offset, maxsize);
Andi Kleenef3d0fd2011-09-15 16:06:48 -0700111 spin_unlock(&file->f_lock);
112 return offset;
Josef Bacik982d8162011-07-18 13:21:35 -0400113 case SEEK_DATA:
114 /*
115 * In the generic case the entire file is data, so as long as
116 * offset isn't at the end of the file then the offset is data.
117 */
Eric Sandeene8b96eb2012-04-30 13:11:29 -0500118 if (offset >= eof)
Josef Bacik982d8162011-07-18 13:21:35 -0400119 return -ENXIO;
120 break;
121 case SEEK_HOLE:
122 /*
123 * There is a virtual hole at the end of the file, so as long as
124 * offset isn't i_size or larger, return i_size.
125 */
Eric Sandeene8b96eb2012-04-30 13:11:29 -0500126 if (offset >= eof)
Josef Bacik982d8162011-07-18 13:21:35 -0400127 return -ENXIO;
Eric Sandeene8b96eb2012-04-30 13:11:29 -0500128 offset = eof;
Josef Bacik982d8162011-07-18 13:21:35 -0400129 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 }
Christoph Hellwig3a8cff42008-08-11 15:37:17 +0200131
Jie Liu46a1c2c2013-06-25 12:02:13 +0800132 return vfs_setpos(file, offset, maxsize);
Andi Kleen57604952011-09-15 16:06:50 -0700133}
134EXPORT_SYMBOL(generic_file_llseek_size);
135
136/**
137 * generic_file_llseek - generic llseek implementation for regular files
138 * @file: file structure to seek on
139 * @offset: file offset to seek to
Andrew Morton965c8e52012-12-17 15:59:39 -0800140 * @whence: type of seek
Andi Kleen57604952011-09-15 16:06:50 -0700141 *
142 * This is a generic implemenation of ->llseek useable for all normal local
143 * filesystems. It just updates the file offset to the value specified by
Ming Lei546ae2d2013-04-29 15:06:07 -0700144 * @offset and @whence.
Andi Kleen57604952011-09-15 16:06:50 -0700145 */
Andrew Morton965c8e52012-12-17 15:59:39 -0800146loff_t generic_file_llseek(struct file *file, loff_t offset, int whence)
Andi Kleen57604952011-09-15 16:06:50 -0700147{
148 struct inode *inode = file->f_mapping->host;
149
Andrew Morton965c8e52012-12-17 15:59:39 -0800150 return generic_file_llseek_size(file, offset, whence,
Eric Sandeene8b96eb2012-04-30 13:11:29 -0500151 inode->i_sb->s_maxbytes,
152 i_size_read(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
Andi Kleen9465efc2008-06-27 11:05:24 +0200154EXPORT_SYMBOL(generic_file_llseek);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
jan Blunckae6afc32010-05-26 14:44:48 -0700156/**
Al Viro1bf9d142013-06-16 20:27:42 +0400157 * fixed_size_llseek - llseek implementation for fixed-sized devices
158 * @file: file structure to seek on
159 * @offset: file offset to seek to
160 * @whence: type of seek
161 * @size: size of the file
162 *
163 */
164loff_t fixed_size_llseek(struct file *file, loff_t offset, int whence, loff_t size)
165{
166 switch (whence) {
167 case SEEK_SET: case SEEK_CUR: case SEEK_END:
168 return generic_file_llseek_size(file, offset, whence,
169 size, size);
170 default:
171 return -EINVAL;
172 }
173}
174EXPORT_SYMBOL(fixed_size_llseek);
175
176/**
Al Virob25472f2015-12-05 22:04:48 -0500177 * no_seek_end_llseek - llseek implementation for fixed-sized devices
178 * @file: file structure to seek on
179 * @offset: file offset to seek to
180 * @whence: type of seek
181 *
182 */
183loff_t no_seek_end_llseek(struct file *file, loff_t offset, int whence)
184{
185 switch (whence) {
186 case SEEK_SET: case SEEK_CUR:
187 return generic_file_llseek_size(file, offset, whence,
Wouter van Kesteren2feb55f2016-02-16 22:20:59 +0100188 OFFSET_MAX, 0);
Al Virob25472f2015-12-05 22:04:48 -0500189 default:
190 return -EINVAL;
191 }
192}
193EXPORT_SYMBOL(no_seek_end_llseek);
194
195/**
196 * no_seek_end_llseek_size - llseek implementation for fixed-sized devices
197 * @file: file structure to seek on
198 * @offset: file offset to seek to
199 * @whence: type of seek
200 * @size: maximal offset allowed
201 *
202 */
203loff_t no_seek_end_llseek_size(struct file *file, loff_t offset, int whence, loff_t size)
204{
205 switch (whence) {
206 case SEEK_SET: case SEEK_CUR:
207 return generic_file_llseek_size(file, offset, whence,
208 size, 0);
209 default:
210 return -EINVAL;
211 }
212}
213EXPORT_SYMBOL(no_seek_end_llseek_size);
214
215/**
jan Blunckae6afc32010-05-26 14:44:48 -0700216 * noop_llseek - No Operation Performed llseek implementation
217 * @file: file structure to seek on
218 * @offset: file offset to seek to
Andrew Morton965c8e52012-12-17 15:59:39 -0800219 * @whence: type of seek
jan Blunckae6afc32010-05-26 14:44:48 -0700220 *
221 * This is an implementation of ->llseek useable for the rare special case when
222 * userspace expects the seek to succeed but the (device) file is actually not
223 * able to perform the seek. In this case you use noop_llseek() instead of
224 * falling back to the default implementation of ->llseek.
225 */
Andrew Morton965c8e52012-12-17 15:59:39 -0800226loff_t noop_llseek(struct file *file, loff_t offset, int whence)
jan Blunckae6afc32010-05-26 14:44:48 -0700227{
228 return file->f_pos;
229}
230EXPORT_SYMBOL(noop_llseek);
231
Andrew Morton965c8e52012-12-17 15:59:39 -0800232loff_t no_llseek(struct file *file, loff_t offset, int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
234 return -ESPIPE;
235}
236EXPORT_SYMBOL(no_llseek);
237
Andrew Morton965c8e52012-12-17 15:59:39 -0800238loff_t default_llseek(struct file *file, loff_t offset, int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
Al Viro496ad9a2013-01-23 17:07:38 -0500240 struct inode *inode = file_inode(file);
David Sterba16abef02008-04-22 15:09:22 +0200241 loff_t retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Al Viro59551022016-01-22 15:40:57 -0500243 inode_lock(inode);
Andrew Morton965c8e52012-12-17 15:59:39 -0800244 switch (whence) {
Chris Snook7b8e8922007-05-08 00:24:13 -0700245 case SEEK_END:
Josef Bacik982d8162011-07-18 13:21:35 -0400246 offset += i_size_read(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 break;
Chris Snook7b8e8922007-05-08 00:24:13 -0700248 case SEEK_CUR:
Alain Knaff5b6f1eb2008-11-10 17:08:08 -0800249 if (offset == 0) {
250 retval = file->f_pos;
251 goto out;
252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 offset += file->f_pos;
Josef Bacik982d8162011-07-18 13:21:35 -0400254 break;
255 case SEEK_DATA:
256 /*
257 * In the generic case the entire file is data, so as
258 * long as offset isn't at the end of the file then the
259 * offset is data.
260 */
Dan Carpenterbacb2d82011-07-26 17:25:20 +0300261 if (offset >= inode->i_size) {
262 retval = -ENXIO;
263 goto out;
264 }
Josef Bacik982d8162011-07-18 13:21:35 -0400265 break;
266 case SEEK_HOLE:
267 /*
268 * There is a virtual hole at the end of the file, so
269 * as long as offset isn't i_size or larger, return
270 * i_size.
271 */
Dan Carpenterbacb2d82011-07-26 17:25:20 +0300272 if (offset >= inode->i_size) {
273 retval = -ENXIO;
274 goto out;
275 }
Josef Bacik982d8162011-07-18 13:21:35 -0400276 offset = inode->i_size;
277 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 }
279 retval = -EINVAL;
Al Virocccb5a12010-12-17 07:44:05 -0500280 if (offset >= 0 || unsigned_offsets(file)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 if (offset != file->f_pos) {
282 file->f_pos = offset;
283 file->f_version = 0;
284 }
285 retval = offset;
286 }
Alain Knaff5b6f1eb2008-11-10 17:08:08 -0800287out:
Al Viro59551022016-01-22 15:40:57 -0500288 inode_unlock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 return retval;
290}
291EXPORT_SYMBOL(default_llseek);
292
Andrew Morton965c8e52012-12-17 15:59:39 -0800293loff_t vfs_llseek(struct file *file, loff_t offset, int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
295 loff_t (*fn)(struct file *, loff_t, int);
296
297 fn = no_llseek;
298 if (file->f_mode & FMODE_LSEEK) {
Al Viro72c2d532013-09-22 16:27:52 -0400299 if (file->f_op->llseek)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 fn = file->f_op->llseek;
301 }
Andrew Morton965c8e52012-12-17 15:59:39 -0800302 return fn(file, offset, whence);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304EXPORT_SYMBOL(vfs_llseek);
305
Andrew Morton965c8e52012-12-17 15:59:39 -0800306SYSCALL_DEFINE3(lseek, unsigned int, fd, off_t, offset, unsigned int, whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
308 off_t retval;
Linus Torvalds9c225f22014-03-03 09:36:58 -0800309 struct fd f = fdget_pos(fd);
Al Viro2903ff02012-08-28 12:52:22 -0400310 if (!f.file)
311 return -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313 retval = -EINVAL;
Andrew Morton965c8e52012-12-17 15:59:39 -0800314 if (whence <= SEEK_MAX) {
315 loff_t res = vfs_llseek(f.file, offset, whence);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 retval = res;
317 if (res != (loff_t)retval)
318 retval = -EOVERFLOW; /* LFS: should only happen on 32 bit platforms */
319 }
Linus Torvalds9c225f22014-03-03 09:36:58 -0800320 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 return retval;
322}
323
Al Viro561c6732013-02-24 10:52:26 -0500324#ifdef CONFIG_COMPAT
325COMPAT_SYSCALL_DEFINE3(lseek, unsigned int, fd, compat_off_t, offset, unsigned int, whence)
326{
327 return sys_lseek(fd, offset, whence);
328}
329#endif
330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331#ifdef __ARCH_WANT_SYS_LLSEEK
Heiko Carstens003d7ab2009-01-14 14:14:21 +0100332SYSCALL_DEFINE5(llseek, unsigned int, fd, unsigned long, offset_high,
333 unsigned long, offset_low, loff_t __user *, result,
Andrew Morton965c8e52012-12-17 15:59:39 -0800334 unsigned int, whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
336 int retval;
Eric Biggersd7a15f82014-03-16 14:24:08 -0500337 struct fd f = fdget_pos(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 loff_t offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Al Viro2903ff02012-08-28 12:52:22 -0400340 if (!f.file)
341 return -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343 retval = -EINVAL;
Andrew Morton965c8e52012-12-17 15:59:39 -0800344 if (whence > SEEK_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 goto out_putf;
346
Al Viro2903ff02012-08-28 12:52:22 -0400347 offset = vfs_llseek(f.file, ((loff_t) offset_high << 32) | offset_low,
Andrew Morton965c8e52012-12-17 15:59:39 -0800348 whence);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350 retval = (int)offset;
351 if (offset >= 0) {
352 retval = -EFAULT;
353 if (!copy_to_user(result, &offset, sizeof(offset)))
354 retval = 0;
355 }
356out_putf:
Eric Biggersd7a15f82014-03-16 14:24:08 -0500357 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 return retval;
359}
360#endif
361
Christoph Hellwigdbe4e192015-01-25 21:11:59 +0100362ssize_t vfs_iter_read(struct file *file, struct iov_iter *iter, loff_t *ppos)
363{
364 struct kiocb kiocb;
365 ssize_t ret;
366
367 if (!file->f_op->read_iter)
368 return -EINVAL;
369
370 init_sync_kiocb(&kiocb, file);
371 kiocb.ki_pos = *ppos;
Christoph Hellwigdbe4e192015-01-25 21:11:59 +0100372
373 iter->type |= READ;
374 ret = file->f_op->read_iter(&kiocb, iter);
Christoph Hellwig599bd192015-02-11 19:59:44 +0100375 BUG_ON(ret == -EIOCBQUEUED);
Christoph Hellwigdbe4e192015-01-25 21:11:59 +0100376 if (ret > 0)
377 *ppos = kiocb.ki_pos;
378 return ret;
379}
380EXPORT_SYMBOL(vfs_iter_read);
381
382ssize_t vfs_iter_write(struct file *file, struct iov_iter *iter, loff_t *ppos)
383{
384 struct kiocb kiocb;
385 ssize_t ret;
386
387 if (!file->f_op->write_iter)
388 return -EINVAL;
389
390 init_sync_kiocb(&kiocb, file);
391 kiocb.ki_pos = *ppos;
Christoph Hellwigdbe4e192015-01-25 21:11:59 +0100392
393 iter->type |= WRITE;
394 ret = file->f_op->write_iter(&kiocb, iter);
Christoph Hellwig599bd192015-02-11 19:59:44 +0100395 BUG_ON(ret == -EIOCBQUEUED);
Christoph Hellwigdbe4e192015-01-25 21:11:59 +0100396 if (ret > 0)
397 *ppos = kiocb.ki_pos;
398 return ret;
399}
400EXPORT_SYMBOL(vfs_iter_write);
401
Al Viro68d70d02013-06-19 15:26:04 +0400402int rw_verify_area(int read_write, struct file *file, const loff_t *ppos, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
404 struct inode *inode;
405 loff_t pos;
James Morrisc43e2592008-01-12 22:05:48 +1100406 int retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Al Viro496ad9a2013-01-23 17:07:38 -0500408 inode = file_inode(file);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800409 if (unlikely((ssize_t) count < 0))
James Morrisc43e2592008-01-12 22:05:48 +1100410 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 pos = *ppos;
Al Virocccb5a12010-12-17 07:44:05 -0500412 if (unlikely(pos < 0)) {
413 if (!unsigned_offsets(file))
414 return retval;
415 if (count >= -pos) /* both values are in 0..LLONG_MAX */
416 return -EOVERFLOW;
417 } else if (unlikely((loff_t) (pos + count) < 0)) {
418 if (!unsigned_offsets(file))
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -0700419 return retval;
420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Jeff Laytonbd61e0a2015-01-16 15:05:55 -0500422 if (unlikely(inode->i_flctx && mandatory_lock(inode))) {
Christoph Hellwigacc15572015-12-03 12:59:49 +0100423 retval = locks_mandatory_area(inode, file, pos, pos + count - 1,
424 read_write == READ ? F_RDLCK : F_WRLCK);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800425 if (retval < 0)
426 return retval;
427 }
Al Virobc613842016-03-31 21:48:20 -0400428 return security_file_permission(file,
James Morrisc43e2592008-01-12 22:05:48 +1100429 read_write == READ ? MAY_READ : MAY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
431
Al Viro5d5d5682015-04-03 15:41:18 -0400432static ssize_t new_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
Al Viro293bc982014-02-11 18:37:41 -0500433{
434 struct iovec iov = { .iov_base = buf, .iov_len = len };
435 struct kiocb kiocb;
436 struct iov_iter iter;
437 ssize_t ret;
438
439 init_sync_kiocb(&kiocb, filp);
440 kiocb.ki_pos = *ppos;
Al Viro293bc982014-02-11 18:37:41 -0500441 iov_iter_init(&iter, READ, &iov, 1, len);
442
443 ret = filp->f_op->read_iter(&kiocb, &iter);
Christoph Hellwig599bd192015-02-11 19:59:44 +0100444 BUG_ON(ret == -EIOCBQUEUED);
Al Viro293bc982014-02-11 18:37:41 -0500445 *ppos = kiocb.ki_pos;
446 return ret;
447}
448
Dmitry Kasatkin6fb50322014-11-05 17:01:17 +0200449ssize_t __vfs_read(struct file *file, char __user *buf, size_t count,
450 loff_t *pos)
451{
Dmitry Kasatkin6fb50322014-11-05 17:01:17 +0200452 if (file->f_op->read)
Al Viro3d04c8a2015-04-03 15:09:18 -0400453 return file->f_op->read(file, buf, count, pos);
Dmitry Kasatkin6fb50322014-11-05 17:01:17 +0200454 else if (file->f_op->read_iter)
Al Viro3d04c8a2015-04-03 15:09:18 -0400455 return new_sync_read(file, buf, count, pos);
Dmitry Kasatkin6fb50322014-11-05 17:01:17 +0200456 else
Al Viro3d04c8a2015-04-03 15:09:18 -0400457 return -EINVAL;
Dmitry Kasatkin6fb50322014-11-05 17:01:17 +0200458}
Al Viro3d04c8a2015-04-03 15:09:18 -0400459EXPORT_SYMBOL(__vfs_read);
Dmitry Kasatkin6fb50322014-11-05 17:01:17 +0200460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
462{
463 ssize_t ret;
464
465 if (!(file->f_mode & FMODE_READ))
466 return -EBADF;
Al Viro7f7f25e2014-02-11 17:49:24 -0500467 if (!(file->f_mode & FMODE_CAN_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 return -EINVAL;
469 if (unlikely(!access_ok(VERIFY_WRITE, buf, count)))
470 return -EFAULT;
471
472 ret = rw_verify_area(READ, file, pos, count);
Al Virobc613842016-03-31 21:48:20 -0400473 if (!ret) {
474 if (count > MAX_RW_COUNT)
475 count = MAX_RW_COUNT;
Dmitry Kasatkin6fb50322014-11-05 17:01:17 +0200476 ret = __vfs_read(file, buf, count, pos);
James Morrisc43e2592008-01-12 22:05:48 +1100477 if (ret > 0) {
Eric Paris2a12a9d2009-12-17 21:24:21 -0500478 fsnotify_access(file);
James Morrisc43e2592008-01-12 22:05:48 +1100479 add_rchar(current, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 }
James Morrisc43e2592008-01-12 22:05:48 +1100481 inc_syscr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 }
483
484 return ret;
485}
486
487EXPORT_SYMBOL(vfs_read);
488
Al Viro5d5d5682015-04-03 15:41:18 -0400489static 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 -0500490{
491 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len };
492 struct kiocb kiocb;
493 struct iov_iter iter;
494 ssize_t ret;
495
496 init_sync_kiocb(&kiocb, filp);
497 kiocb.ki_pos = *ppos;
Al Viro293bc982014-02-11 18:37:41 -0500498 iov_iter_init(&iter, WRITE, &iov, 1, len);
499
500 ret = filp->f_op->write_iter(&kiocb, &iter);
Christoph Hellwig599bd192015-02-11 19:59:44 +0100501 BUG_ON(ret == -EIOCBQUEUED);
Al Virof765b132015-04-06 20:50:38 -0400502 if (ret > 0)
503 *ppos = kiocb.ki_pos;
Al Viro293bc982014-02-11 18:37:41 -0500504 return ret;
505}
506
Al Viro493c84c2015-04-03 15:06:43 -0400507ssize_t __vfs_write(struct file *file, const char __user *p, size_t count,
508 loff_t *pos)
509{
510 if (file->f_op->write)
511 return file->f_op->write(file, p, count, pos);
Al Viro493c84c2015-04-03 15:06:43 -0400512 else if (file->f_op->write_iter)
513 return new_sync_write(file, p, count, pos);
514 else
515 return -EINVAL;
516}
517EXPORT_SYMBOL(__vfs_write);
518
Al Viro06ae43f2013-03-20 13:19:30 -0400519ssize_t __kernel_write(struct file *file, const char *buf, size_t count, loff_t *pos)
520{
521 mm_segment_t old_fs;
522 const char __user *p;
523 ssize_t ret;
524
Al Viro7f7f25e2014-02-11 17:49:24 -0500525 if (!(file->f_mode & FMODE_CAN_WRITE))
Al Viro3e84f482013-03-27 15:20:30 +0000526 return -EINVAL;
527
Al Viro06ae43f2013-03-20 13:19:30 -0400528 old_fs = get_fs();
529 set_fs(get_ds());
530 p = (__force const char __user *)buf;
531 if (count > MAX_RW_COUNT)
532 count = MAX_RW_COUNT;
Al Viro493c84c2015-04-03 15:06:43 -0400533 ret = __vfs_write(file, p, count, pos);
Al Viro06ae43f2013-03-20 13:19:30 -0400534 set_fs(old_fs);
535 if (ret > 0) {
536 fsnotify_modify(file);
537 add_wchar(current, ret);
538 }
539 inc_syscw(current);
540 return ret;
541}
542
Al Viro2ec3a122014-08-19 11:48:09 -0400543EXPORT_SYMBOL(__kernel_write);
544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
546{
547 ssize_t ret;
548
549 if (!(file->f_mode & FMODE_WRITE))
550 return -EBADF;
Al Viro7f7f25e2014-02-11 17:49:24 -0500551 if (!(file->f_mode & FMODE_CAN_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 return -EINVAL;
553 if (unlikely(!access_ok(VERIFY_READ, buf, count)))
554 return -EFAULT;
555
556 ret = rw_verify_area(WRITE, file, pos, count);
Al Virobc613842016-03-31 21:48:20 -0400557 if (!ret) {
558 if (count > MAX_RW_COUNT)
559 count = MAX_RW_COUNT;
Al Viro03d95eb2013-03-20 13:04:20 -0400560 file_start_write(file);
Al Viro493c84c2015-04-03 15:06:43 -0400561 ret = __vfs_write(file, buf, count, pos);
James Morrisc43e2592008-01-12 22:05:48 +1100562 if (ret > 0) {
Eric Paris2a12a9d2009-12-17 21:24:21 -0500563 fsnotify_modify(file);
James Morrisc43e2592008-01-12 22:05:48 +1100564 add_wchar(current, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 }
James Morrisc43e2592008-01-12 22:05:48 +1100566 inc_syscw(current);
Al Viro03d95eb2013-03-20 13:04:20 -0400567 file_end_write(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 }
569
570 return ret;
571}
572
573EXPORT_SYMBOL(vfs_write);
574
575static inline loff_t file_pos_read(struct file *file)
576{
577 return file->f_pos;
578}
579
580static inline void file_pos_write(struct file *file, loff_t pos)
581{
582 file->f_pos = pos;
583}
584
Heiko Carstens3cdad422009-01-14 14:14:22 +0100585SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
Linus Torvalds9c225f22014-03-03 09:36:58 -0800587 struct fd f = fdget_pos(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Al Viro2903ff02012-08-28 12:52:22 -0400590 if (f.file) {
591 loff_t pos = file_pos_read(f.file);
592 ret = vfs_read(f.file, buf, count, &pos);
Al Viro5faf1532013-06-15 05:49:36 +0400593 if (ret >= 0)
594 file_pos_write(f.file, pos);
Linus Torvalds9c225f22014-03-03 09:36:58 -0800595 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 return ret;
598}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Heiko Carstens3cdad422009-01-14 14:14:22 +0100600SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf,
601 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_write(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 }
613
614 return ret;
615}
616
Al Viro4a0fd5b2013-01-21 15:16:58 -0500617SYSCALL_DEFINE4(pread64, unsigned int, fd, char __user *, buf,
618 size_t, count, loff_t, pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
Al Viro2903ff02012-08-28 12:52:22 -0400620 struct fd f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623 if (pos < 0)
624 return -EINVAL;
625
Al Viro2903ff02012-08-28 12:52:22 -0400626 f = fdget(fd);
627 if (f.file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -0400629 if (f.file->f_mode & FMODE_PREAD)
630 ret = vfs_read(f.file, buf, count, &pos);
631 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 }
633
634 return ret;
635}
636
Al Viro4a0fd5b2013-01-21 15:16:58 -0500637SYSCALL_DEFINE4(pwrite64, unsigned int, fd, const char __user *, buf,
638 size_t, count, loff_t, pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
Al Viro2903ff02012-08-28 12:52:22 -0400640 struct fd f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643 if (pos < 0)
644 return -EINVAL;
645
Al Viro2903ff02012-08-28 12:52:22 -0400646 f = fdget(fd);
647 if (f.file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -0400649 if (f.file->f_mode & FMODE_PWRITE)
650 ret = vfs_write(f.file, buf, count, &pos);
651 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 }
653
654 return ret;
655}
656
657/*
658 * Reduce an iovec's length in-place. Return the resulting number of segments
659 */
660unsigned long iov_shorten(struct iovec *iov, unsigned long nr_segs, size_t to)
661{
662 unsigned long seg = 0;
663 size_t len = 0;
664
665 while (seg < nr_segs) {
666 seg++;
667 if (len + iov->iov_len >= to) {
668 iov->iov_len = to - len;
669 break;
670 }
671 len += iov->iov_len;
672 iov++;
673 }
674 return seg;
675}
Eric Sandeen19295522008-01-28 23:58:27 -0500676EXPORT_SYMBOL(iov_shorten);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Al Viroac15ac02015-03-20 20:10:21 -0400678static ssize_t do_iter_readv_writev(struct file *filp, struct iov_iter *iter,
Christoph Hellwig793b80e2016-03-03 16:03:58 +0100679 loff_t *ppos, iter_fn_t fn, int flags)
Al Viro293bc982014-02-11 18:37:41 -0500680{
681 struct kiocb kiocb;
Al Viro293bc982014-02-11 18:37:41 -0500682 ssize_t ret;
683
Christoph Hellwige864f392016-04-07 08:52:03 -0700684 if (flags & ~(RWF_HIPRI | RWF_DSYNC | RWF_SYNC))
Christoph Hellwig793b80e2016-03-03 16:03:58 +0100685 return -EOPNOTSUPP;
686
Al Viro293bc982014-02-11 18:37:41 -0500687 init_sync_kiocb(&kiocb, filp);
Christoph Hellwig97be7eb2016-03-03 16:04:01 +0100688 if (flags & RWF_HIPRI)
689 kiocb.ki_flags |= IOCB_HIPRI;
Christoph Hellwige864f392016-04-07 08:52:03 -0700690 if (flags & RWF_DSYNC)
691 kiocb.ki_flags |= IOCB_DSYNC;
692 if (flags & RWF_SYNC)
693 kiocb.ki_flags |= (IOCB_DSYNC | IOCB_SYNC);
Al Viro293bc982014-02-11 18:37:41 -0500694 kiocb.ki_pos = *ppos;
Al Viro293bc982014-02-11 18:37:41 -0500695
Al Viroac15ac02015-03-20 20:10:21 -0400696 ret = fn(&kiocb, iter);
Christoph Hellwig599bd192015-02-11 19:59:44 +0100697 BUG_ON(ret == -EIOCBQUEUED);
Al Viro293bc982014-02-11 18:37:41 -0500698 *ppos = kiocb.ki_pos;
699 return ret;
700}
701
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700702/* Do it by hand, with file-ops */
Al Viroac15ac02015-03-20 20:10:21 -0400703static ssize_t do_loop_readv_writev(struct file *filp, struct iov_iter *iter,
Christoph Hellwig793b80e2016-03-03 16:03:58 +0100704 loff_t *ppos, io_fn_t fn, int flags)
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700705{
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700706 ssize_t ret = 0;
707
Christoph Hellwig97be7eb2016-03-03 16:04:01 +0100708 if (flags & ~RWF_HIPRI)
Christoph Hellwig793b80e2016-03-03 16:03:58 +0100709 return -EOPNOTSUPP;
710
Al Viroac15ac02015-03-20 20:10:21 -0400711 while (iov_iter_count(iter)) {
712 struct iovec iovec = iov_iter_iovec(iter);
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700713 ssize_t nr;
714
Al Viroac15ac02015-03-20 20:10:21 -0400715 nr = fn(filp, iovec.iov_base, iovec.iov_len, ppos);
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700716
717 if (nr < 0) {
718 if (!ret)
719 ret = nr;
720 break;
721 }
722 ret += nr;
Al Viroac15ac02015-03-20 20:10:21 -0400723 if (nr != iovec.iov_len)
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700724 break;
Al Viroac15ac02015-03-20 20:10:21 -0400725 iov_iter_advance(iter, nr);
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700726 }
727
728 return ret;
729}
730
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731/* A write operation does a read from user space and vice versa */
732#define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
733
Vegard Nossumffecee42016-10-08 11:18:07 +0200734/**
735 * rw_copy_check_uvector() - Copy an array of &struct iovec from userspace
736 * into the kernel and check that it is valid.
737 *
738 * @type: One of %CHECK_IOVEC_ONLY, %READ, or %WRITE.
739 * @uvector: Pointer to the userspace array.
740 * @nr_segs: Number of elements in userspace array.
741 * @fast_segs: Number of elements in @fast_pointer.
742 * @fast_pointer: Pointer to (usually small on-stack) kernel array.
743 * @ret_pointer: (output parameter) Pointer to a variable that will point to
744 * either @fast_pointer, a newly allocated kernel array, or NULL,
745 * depending on which array was used.
746 *
747 * This function copies an array of &struct iovec of @nr_segs from
748 * userspace into the kernel and checks that each element is valid (e.g.
749 * it does not point to a kernel address or cause overflow by being too
750 * large, etc.).
751 *
752 * As an optimization, the caller may provide a pointer to a small
753 * on-stack array in @fast_pointer, typically %UIO_FASTIOV elements long
754 * (the size of this array, or 0 if unused, should be given in @fast_segs).
755 *
756 * @ret_pointer will always point to the array that was used, so the
757 * caller must take care not to call kfree() on it e.g. in case the
758 * @fast_pointer array was used and it was allocated on the stack.
759 *
760 * Return: The total number of bytes covered by the iovec array on success
761 * or a negative error code on error.
762 */
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700763ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
764 unsigned long nr_segs, unsigned long fast_segs,
765 struct iovec *fast_pointer,
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700766 struct iovec **ret_pointer)
Linus Torvalds435f49a2010-10-29 10:36:49 -0700767{
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700768 unsigned long seg;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700769 ssize_t ret;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700770 struct iovec *iov = fast_pointer;
771
Linus Torvalds435f49a2010-10-29 10:36:49 -0700772 /*
773 * SuS says "The readv() function *may* fail if the iovcnt argument
774 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
775 * traditionally returned zero for zero segments, so...
776 */
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700777 if (nr_segs == 0) {
778 ret = 0;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700779 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700780 }
781
Linus Torvalds435f49a2010-10-29 10:36:49 -0700782 /*
783 * First get the "struct iovec" from user memory and
784 * verify all the pointers
785 */
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700786 if (nr_segs > UIO_MAXIOV) {
787 ret = -EINVAL;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700788 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700789 }
790 if (nr_segs > fast_segs) {
Linus Torvalds435f49a2010-10-29 10:36:49 -0700791 iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700792 if (iov == NULL) {
793 ret = -ENOMEM;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700794 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700795 }
Linus Torvalds435f49a2010-10-29 10:36:49 -0700796 }
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700797 if (copy_from_user(iov, uvector, nr_segs*sizeof(*uvector))) {
798 ret = -EFAULT;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700799 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700800 }
801
Linus Torvalds435f49a2010-10-29 10:36:49 -0700802 /*
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700803 * According to the Single Unix Specification we should return EINVAL
804 * if an element length is < 0 when cast to ssize_t or if the
805 * total length would overflow the ssize_t return value of the
806 * system call.
Linus Torvalds435f49a2010-10-29 10:36:49 -0700807 *
808 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
809 * overflow case.
810 */
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700811 ret = 0;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700812 for (seg = 0; seg < nr_segs; seg++) {
813 void __user *buf = iov[seg].iov_base;
814 ssize_t len = (ssize_t)iov[seg].iov_len;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700815
816 /* see if we we're about to use an invalid len or if
817 * it's about to overflow ssize_t */
Linus Torvalds435f49a2010-10-29 10:36:49 -0700818 if (len < 0) {
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700819 ret = -EINVAL;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700820 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700821 }
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700822 if (type >= 0
Christopher Yeohfcf63402011-10-31 17:06:39 -0700823 && unlikely(!access_ok(vrfy_dir(type), buf, len))) {
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700824 ret = -EFAULT;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700825 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700826 }
Linus Torvalds435f49a2010-10-29 10:36:49 -0700827 if (len > MAX_RW_COUNT - ret) {
828 len = MAX_RW_COUNT - ret;
829 iov[seg].iov_len = len;
830 }
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700831 ret += len;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700832 }
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700833out:
834 *ret_pointer = iov;
835 return ret;
836}
837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838static ssize_t do_readv_writev(int type, struct file *file,
839 const struct iovec __user * uvector,
Christoph Hellwig793b80e2016-03-03 16:03:58 +0100840 unsigned long nr_segs, loff_t *pos,
841 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 size_t tot_len;
844 struct iovec iovstack[UIO_FASTIOV];
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700845 struct iovec *iov = iovstack;
Al Viroac15ac02015-03-20 20:10:21 -0400846 struct iov_iter iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 io_fn_t fn;
Al Viro293bc982014-02-11 18:37:41 -0500849 iter_fn_t iter_fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Al Viro0504c072015-03-21 19:40:11 -0400851 ret = import_iovec(type, uvector, nr_segs,
852 ARRAY_SIZE(iovstack), &iov, &iter);
853 if (ret < 0)
854 return ret;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700855
Al Viro0504c072015-03-21 19:40:11 -0400856 tot_len = iov_iter_count(&iter);
857 if (!tot_len)
858 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 ret = rw_verify_area(type, file, pos, tot_len);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800860 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 goto out;
862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 if (type == READ) {
864 fn = file->f_op->read;
Al Viro293bc982014-02-11 18:37:41 -0500865 iter_fn = file->f_op->read_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 } else {
867 fn = (io_fn_t)file->f_op->write;
Al Viro293bc982014-02-11 18:37:41 -0500868 iter_fn = file->f_op->write_iter;
Al Viro03d95eb2013-03-20 13:04:20 -0400869 file_start_write(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 }
871
Al Viro293bc982014-02-11 18:37:41 -0500872 if (iter_fn)
Christoph Hellwig793b80e2016-03-03 16:03:58 +0100873 ret = do_iter_readv_writev(file, &iter, pos, iter_fn, flags);
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700874 else
Christoph Hellwig793b80e2016-03-03 16:03:58 +0100875 ret = do_loop_readv_writev(file, &iter, pos, fn, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Al Viro03d95eb2013-03-20 13:04:20 -0400877 if (type != READ)
878 file_end_write(file);
879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880out:
Al Viro0504c072015-03-21 19:40:11 -0400881 kfree(iov);
Robert Love0eeca282005-07-12 17:06:03 -0400882 if ((ret + (type == READ)) > 0) {
883 if (type == READ)
Eric Paris2a12a9d2009-12-17 21:24:21 -0500884 fsnotify_access(file);
Robert Love0eeca282005-07-12 17:06:03 -0400885 else
Eric Paris2a12a9d2009-12-17 21:24:21 -0500886 fsnotify_modify(file);
Robert Love0eeca282005-07-12 17:06:03 -0400887 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889}
890
891ssize_t vfs_readv(struct file *file, const struct iovec __user *vec,
Christoph Hellwig793b80e2016-03-03 16:03:58 +0100892 unsigned long vlen, loff_t *pos, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
894 if (!(file->f_mode & FMODE_READ))
895 return -EBADF;
Al Viro7f7f25e2014-02-11 17:49:24 -0500896 if (!(file->f_mode & FMODE_CAN_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 return -EINVAL;
898
Christoph Hellwig793b80e2016-03-03 16:03:58 +0100899 return do_readv_writev(READ, file, vec, vlen, pos, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900}
901
902EXPORT_SYMBOL(vfs_readv);
903
904ssize_t vfs_writev(struct file *file, const struct iovec __user *vec,
Christoph Hellwig793b80e2016-03-03 16:03:58 +0100905 unsigned long vlen, loff_t *pos, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
907 if (!(file->f_mode & FMODE_WRITE))
908 return -EBADF;
Al Viro7f7f25e2014-02-11 17:49:24 -0500909 if (!(file->f_mode & FMODE_CAN_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 return -EINVAL;
911
Christoph Hellwig793b80e2016-03-03 16:03:58 +0100912 return do_readv_writev(WRITE, file, vec, vlen, pos, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913}
914
915EXPORT_SYMBOL(vfs_writev);
916
Milosz Tanskif17d8b32016-03-03 16:03:59 +0100917static ssize_t do_readv(unsigned long fd, const struct iovec __user *vec,
918 unsigned long vlen, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
Linus Torvalds9c225f22014-03-03 09:36:58 -0800920 struct fd f = fdget_pos(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Al Viro2903ff02012-08-28 12:52:22 -0400923 if (f.file) {
924 loff_t pos = file_pos_read(f.file);
Milosz Tanskif17d8b32016-03-03 16:03:59 +0100925 ret = vfs_readv(f.file, vec, vlen, &pos, flags);
Al Viro5faf1532013-06-15 05:49:36 +0400926 if (ret >= 0)
927 file_pos_write(f.file, pos);
Linus Torvalds9c225f22014-03-03 09:36:58 -0800928 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 }
930
931 if (ret > 0)
Alexey Dobriyan4b98d112007-02-10 01:46:45 -0800932 add_rchar(current, ret);
933 inc_syscr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 return ret;
935}
936
Milosz Tanskif17d8b32016-03-03 16:03:59 +0100937static ssize_t do_writev(unsigned long fd, const struct iovec __user *vec,
938 unsigned long vlen, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939{
Linus Torvalds9c225f22014-03-03 09:36:58 -0800940 struct fd f = fdget_pos(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Al Viro2903ff02012-08-28 12:52:22 -0400943 if (f.file) {
944 loff_t pos = file_pos_read(f.file);
Milosz Tanskif17d8b32016-03-03 16:03:59 +0100945 ret = vfs_writev(f.file, vec, vlen, &pos, flags);
Al Viro5faf1532013-06-15 05:49:36 +0400946 if (ret >= 0)
947 file_pos_write(f.file, pos);
Linus Torvalds9c225f22014-03-03 09:36:58 -0800948 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 }
950
951 if (ret > 0)
Alexey Dobriyan4b98d112007-02-10 01:46:45 -0800952 add_wchar(current, ret);
953 inc_syscw(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 return ret;
955}
956
Linus Torvalds601cc112009-04-03 08:03:22 -0700957static inline loff_t pos_from_hilo(unsigned long high, unsigned long low)
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700958{
Linus Torvalds601cc112009-04-03 08:03:22 -0700959#define HALF_LONG_BITS (BITS_PER_LONG / 2)
960 return (((loff_t)high << HALF_LONG_BITS) << HALF_LONG_BITS) | low;
961}
962
Milosz Tanskif17d8b32016-03-03 16:03:59 +0100963static ssize_t do_preadv(unsigned long fd, const struct iovec __user *vec,
964 unsigned long vlen, loff_t pos, int flags)
Linus Torvalds601cc112009-04-03 08:03:22 -0700965{
Al Viro2903ff02012-08-28 12:52:22 -0400966 struct fd f;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700967 ssize_t ret = -EBADF;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700968
969 if (pos < 0)
970 return -EINVAL;
971
Al Viro2903ff02012-08-28 12:52:22 -0400972 f = fdget(fd);
973 if (f.file) {
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700974 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -0400975 if (f.file->f_mode & FMODE_PREAD)
Milosz Tanskif17d8b32016-03-03 16:03:59 +0100976 ret = vfs_readv(f.file, vec, vlen, &pos, flags);
Al Viro2903ff02012-08-28 12:52:22 -0400977 fdput(f);
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700978 }
979
980 if (ret > 0)
981 add_rchar(current, ret);
982 inc_syscr(current);
983 return ret;
984}
985
Milosz Tanskif17d8b32016-03-03 16:03:59 +0100986static ssize_t do_pwritev(unsigned long fd, const struct iovec __user *vec,
987 unsigned long vlen, loff_t pos, int flags)
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700988{
Al Viro2903ff02012-08-28 12:52:22 -0400989 struct fd f;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700990 ssize_t ret = -EBADF;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700991
992 if (pos < 0)
993 return -EINVAL;
994
Al Viro2903ff02012-08-28 12:52:22 -0400995 f = fdget(fd);
996 if (f.file) {
Gerd Hoffmannf3554f42009-04-02 16:59:23 -0700997 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -0400998 if (f.file->f_mode & FMODE_PWRITE)
Milosz Tanskif17d8b32016-03-03 16:03:59 +0100999 ret = vfs_writev(f.file, vec, vlen, &pos, flags);
Al Viro2903ff02012-08-28 12:52:22 -04001000 fdput(f);
Gerd Hoffmannf3554f42009-04-02 16:59:23 -07001001 }
1002
1003 if (ret > 0)
1004 add_wchar(current, ret);
1005 inc_syscw(current);
1006 return ret;
1007}
1008
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001009SYSCALL_DEFINE3(readv, unsigned long, fd, const struct iovec __user *, vec,
1010 unsigned long, vlen)
1011{
1012 return do_readv(fd, vec, vlen, 0);
1013}
1014
1015SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec __user *, vec,
1016 unsigned long, vlen)
1017{
1018 return do_writev(fd, vec, vlen, 0);
1019}
1020
1021SYSCALL_DEFINE5(preadv, unsigned long, fd, const struct iovec __user *, vec,
1022 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
1023{
1024 loff_t pos = pos_from_hilo(pos_h, pos_l);
1025
1026 return do_preadv(fd, vec, vlen, pos, 0);
1027}
1028
1029SYSCALL_DEFINE6(preadv2, unsigned long, fd, const struct iovec __user *, vec,
1030 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h,
1031 int, flags)
1032{
1033 loff_t pos = pos_from_hilo(pos_h, pos_l);
1034
1035 if (pos == -1)
1036 return do_readv(fd, vec, vlen, flags);
1037
1038 return do_preadv(fd, vec, vlen, pos, flags);
1039}
1040
1041SYSCALL_DEFINE5(pwritev, unsigned long, fd, const struct iovec __user *, vec,
1042 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
1043{
1044 loff_t pos = pos_from_hilo(pos_h, pos_l);
1045
1046 return do_pwritev(fd, vec, vlen, pos, 0);
1047}
1048
1049SYSCALL_DEFINE6(pwritev2, unsigned long, fd, const struct iovec __user *, vec,
1050 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h,
1051 int, flags)
1052{
1053 loff_t pos = pos_from_hilo(pos_h, pos_l);
1054
1055 if (pos == -1)
1056 return do_writev(fd, vec, vlen, flags);
1057
1058 return do_pwritev(fd, vec, vlen, pos, flags);
1059}
1060
Al Viro72ec3512013-03-20 10:42:10 -04001061#ifdef CONFIG_COMPAT
1062
1063static ssize_t compat_do_readv_writev(int type, struct file *file,
1064 const struct compat_iovec __user *uvector,
Christoph Hellwig793b80e2016-03-03 16:03:58 +01001065 unsigned long nr_segs, loff_t *pos,
1066 int flags)
Al Viro72ec3512013-03-20 10:42:10 -04001067{
1068 compat_ssize_t tot_len;
1069 struct iovec iovstack[UIO_FASTIOV];
1070 struct iovec *iov = iovstack;
Al Viroac15ac02015-03-20 20:10:21 -04001071 struct iov_iter iter;
Al Viro72ec3512013-03-20 10:42:10 -04001072 ssize_t ret;
1073 io_fn_t fn;
Al Viro293bc982014-02-11 18:37:41 -05001074 iter_fn_t iter_fn;
Al Viro72ec3512013-03-20 10:42:10 -04001075
Al Viro0504c072015-03-21 19:40:11 -04001076 ret = compat_import_iovec(type, uvector, nr_segs,
1077 UIO_FASTIOV, &iov, &iter);
1078 if (ret < 0)
1079 return ret;
Al Viro72ec3512013-03-20 10:42:10 -04001080
Al Viro0504c072015-03-21 19:40:11 -04001081 tot_len = iov_iter_count(&iter);
1082 if (!tot_len)
1083 goto out;
Al Viro72ec3512013-03-20 10:42:10 -04001084 ret = rw_verify_area(type, file, pos, tot_len);
1085 if (ret < 0)
1086 goto out;
1087
Al Viro72ec3512013-03-20 10:42:10 -04001088 if (type == READ) {
1089 fn = file->f_op->read;
Al Viro293bc982014-02-11 18:37:41 -05001090 iter_fn = file->f_op->read_iter;
Al Viro72ec3512013-03-20 10:42:10 -04001091 } else {
1092 fn = (io_fn_t)file->f_op->write;
Al Viro293bc982014-02-11 18:37:41 -05001093 iter_fn = file->f_op->write_iter;
Al Viro03d95eb2013-03-20 13:04:20 -04001094 file_start_write(file);
Al Viro72ec3512013-03-20 10:42:10 -04001095 }
1096
Al Viro293bc982014-02-11 18:37:41 -05001097 if (iter_fn)
Christoph Hellwig793b80e2016-03-03 16:03:58 +01001098 ret = do_iter_readv_writev(file, &iter, pos, iter_fn, flags);
Al Viro03d95eb2013-03-20 13:04:20 -04001099 else
Christoph Hellwig793b80e2016-03-03 16:03:58 +01001100 ret = do_loop_readv_writev(file, &iter, pos, fn, flags);
Al Viro72ec3512013-03-20 10:42:10 -04001101
Al Viro03d95eb2013-03-20 13:04:20 -04001102 if (type != READ)
1103 file_end_write(file);
1104
Al Viro72ec3512013-03-20 10:42:10 -04001105out:
Al Viro0504c072015-03-21 19:40:11 -04001106 kfree(iov);
Al Viro72ec3512013-03-20 10:42:10 -04001107 if ((ret + (type == READ)) > 0) {
1108 if (type == READ)
1109 fsnotify_access(file);
1110 else
1111 fsnotify_modify(file);
1112 }
1113 return ret;
1114}
1115
1116static size_t compat_readv(struct file *file,
1117 const struct compat_iovec __user *vec,
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001118 unsigned long vlen, loff_t *pos, int flags)
Al Viro72ec3512013-03-20 10:42:10 -04001119{
1120 ssize_t ret = -EBADF;
1121
1122 if (!(file->f_mode & FMODE_READ))
1123 goto out;
1124
1125 ret = -EINVAL;
Al Viro7f7f25e2014-02-11 17:49:24 -05001126 if (!(file->f_mode & FMODE_CAN_READ))
Al Viro72ec3512013-03-20 10:42:10 -04001127 goto out;
1128
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001129 ret = compat_do_readv_writev(READ, file, vec, vlen, pos, flags);
Al Viro72ec3512013-03-20 10:42:10 -04001130
1131out:
1132 if (ret > 0)
1133 add_rchar(current, ret);
1134 inc_syscr(current);
1135 return ret;
1136}
1137
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001138static size_t do_compat_readv(compat_ulong_t fd,
1139 const struct compat_iovec __user *vec,
1140 compat_ulong_t vlen, int flags)
Al Viro72ec3512013-03-20 10:42:10 -04001141{
Linus Torvalds9c225f22014-03-03 09:36:58 -08001142 struct fd f = fdget_pos(fd);
Al Viro72ec3512013-03-20 10:42:10 -04001143 ssize_t ret;
1144 loff_t pos;
1145
1146 if (!f.file)
1147 return -EBADF;
1148 pos = f.file->f_pos;
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001149 ret = compat_readv(f.file, vec, vlen, &pos, flags);
Al Viro5faf1532013-06-15 05:49:36 +04001150 if (ret >= 0)
1151 f.file->f_pos = pos;
Linus Torvalds9c225f22014-03-03 09:36:58 -08001152 fdput_pos(f);
Al Viro72ec3512013-03-20 10:42:10 -04001153 return ret;
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001154
Al Viro72ec3512013-03-20 10:42:10 -04001155}
1156
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001157COMPAT_SYSCALL_DEFINE3(readv, compat_ulong_t, fd,
1158 const struct compat_iovec __user *,vec,
1159 compat_ulong_t, vlen)
1160{
1161 return do_compat_readv(fd, vec, vlen, 0);
1162}
1163
1164static long do_compat_preadv64(unsigned long fd,
Heiko Carstens378a10f2014-03-05 10:43:51 +01001165 const struct compat_iovec __user *vec,
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001166 unsigned long vlen, loff_t pos, int flags)
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_PREAD)
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001178 ret = compat_readv(f.file, vec, vlen, &pos, flags);
Al Viro72ec3512013-03-20 10:42:10 -04001179 fdput(f);
1180 return ret;
1181}
1182
Heiko Carstens378a10f2014-03-05 10:43:51 +01001183#ifdef __ARCH_WANT_COMPAT_SYS_PREADV64
1184COMPAT_SYSCALL_DEFINE4(preadv64, unsigned long, fd,
1185 const struct compat_iovec __user *,vec,
1186 unsigned long, vlen, loff_t, pos)
1187{
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001188 return do_compat_preadv64(fd, vec, vlen, pos, 0);
Heiko Carstens378a10f2014-03-05 10:43:51 +01001189}
1190#endif
1191
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001192COMPAT_SYSCALL_DEFINE5(preadv, 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
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001198 return do_compat_preadv64(fd, vec, vlen, pos, 0);
1199}
1200
H.J. Lu3ebfd812016-07-14 12:31:53 -07001201#ifdef __ARCH_WANT_COMPAT_SYS_PREADV64V2
1202COMPAT_SYSCALL_DEFINE5(preadv64v2, unsigned long, fd,
1203 const struct compat_iovec __user *,vec,
1204 unsigned long, vlen, loff_t, pos, int, flags)
1205{
1206 return do_compat_preadv64(fd, vec, vlen, pos, flags);
1207}
1208#endif
1209
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001210COMPAT_SYSCALL_DEFINE6(preadv2, compat_ulong_t, fd,
1211 const struct compat_iovec __user *,vec,
1212 compat_ulong_t, vlen, u32, pos_low, u32, pos_high,
1213 int, flags)
1214{
1215 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
1216
1217 if (pos == -1)
1218 return do_compat_readv(fd, vec, vlen, flags);
1219
1220 return do_compat_preadv64(fd, vec, vlen, pos, flags);
Al Viro72ec3512013-03-20 10:42:10 -04001221}
1222
1223static size_t compat_writev(struct file *file,
1224 const struct compat_iovec __user *vec,
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001225 unsigned long vlen, loff_t *pos, int flags)
Al Viro72ec3512013-03-20 10:42:10 -04001226{
1227 ssize_t ret = -EBADF;
1228
1229 if (!(file->f_mode & FMODE_WRITE))
1230 goto out;
1231
1232 ret = -EINVAL;
Al Viro7f7f25e2014-02-11 17:49:24 -05001233 if (!(file->f_mode & FMODE_CAN_WRITE))
Al Viro72ec3512013-03-20 10:42:10 -04001234 goto out;
1235
Christoph Hellwig793b80e2016-03-03 16:03:58 +01001236 ret = compat_do_readv_writev(WRITE, file, vec, vlen, pos, 0);
Al Viro72ec3512013-03-20 10:42:10 -04001237
1238out:
1239 if (ret > 0)
1240 add_wchar(current, ret);
1241 inc_syscw(current);
1242 return ret;
1243}
1244
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001245static size_t do_compat_writev(compat_ulong_t fd,
1246 const struct compat_iovec __user* vec,
1247 compat_ulong_t vlen, int flags)
Al Viro72ec3512013-03-20 10:42:10 -04001248{
Linus Torvalds9c225f22014-03-03 09:36:58 -08001249 struct fd f = fdget_pos(fd);
Al Viro72ec3512013-03-20 10:42:10 -04001250 ssize_t ret;
1251 loff_t pos;
1252
1253 if (!f.file)
1254 return -EBADF;
1255 pos = f.file->f_pos;
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001256 ret = compat_writev(f.file, vec, vlen, &pos, flags);
Al Viro5faf1532013-06-15 05:49:36 +04001257 if (ret >= 0)
1258 f.file->f_pos = pos;
Linus Torvalds9c225f22014-03-03 09:36:58 -08001259 fdput_pos(f);
Al Viro72ec3512013-03-20 10:42:10 -04001260 return ret;
1261}
1262
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001263COMPAT_SYSCALL_DEFINE3(writev, compat_ulong_t, fd,
1264 const struct compat_iovec __user *, vec,
1265 compat_ulong_t, vlen)
1266{
1267 return do_compat_writev(fd, vec, vlen, 0);
1268}
1269
1270static long do_compat_pwritev64(unsigned long fd,
Heiko Carstens378a10f2014-03-05 10:43:51 +01001271 const struct compat_iovec __user *vec,
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001272 unsigned long vlen, loff_t pos, int flags)
Al Viro72ec3512013-03-20 10:42:10 -04001273{
1274 struct fd f;
1275 ssize_t ret;
1276
1277 if (pos < 0)
1278 return -EINVAL;
1279 f = fdget(fd);
1280 if (!f.file)
1281 return -EBADF;
1282 ret = -ESPIPE;
1283 if (f.file->f_mode & FMODE_PWRITE)
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001284 ret = compat_writev(f.file, vec, vlen, &pos, flags);
Al Viro72ec3512013-03-20 10:42:10 -04001285 fdput(f);
1286 return ret;
1287}
1288
Heiko Carstens378a10f2014-03-05 10:43:51 +01001289#ifdef __ARCH_WANT_COMPAT_SYS_PWRITEV64
1290COMPAT_SYSCALL_DEFINE4(pwritev64, unsigned long, fd,
1291 const struct compat_iovec __user *,vec,
1292 unsigned long, vlen, loff_t, pos)
1293{
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001294 return do_compat_pwritev64(fd, vec, vlen, pos, 0);
Heiko Carstens378a10f2014-03-05 10:43:51 +01001295}
1296#endif
1297
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001298COMPAT_SYSCALL_DEFINE5(pwritev, compat_ulong_t, fd,
Al Viro72ec3512013-03-20 10:42:10 -04001299 const struct compat_iovec __user *,vec,
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001300 compat_ulong_t, vlen, u32, pos_low, u32, pos_high)
Al Viro72ec3512013-03-20 10:42:10 -04001301{
1302 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
Heiko Carstens378a10f2014-03-05 10:43:51 +01001303
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001304 return do_compat_pwritev64(fd, vec, vlen, pos, 0);
Al Viro72ec3512013-03-20 10:42:10 -04001305}
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001306
H.J. Lu3ebfd812016-07-14 12:31:53 -07001307#ifdef __ARCH_WANT_COMPAT_SYS_PWRITEV64V2
1308COMPAT_SYSCALL_DEFINE5(pwritev64v2, unsigned long, fd,
1309 const struct compat_iovec __user *,vec,
1310 unsigned long, vlen, loff_t, pos, int, flags)
1311{
1312 return do_compat_pwritev64(fd, vec, vlen, pos, flags);
1313}
1314#endif
1315
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001316COMPAT_SYSCALL_DEFINE6(pwritev2, compat_ulong_t, fd,
1317 const struct compat_iovec __user *,vec,
1318 compat_ulong_t, vlen, u32, pos_low, u32, pos_high, int, flags)
1319{
1320 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
1321
1322 if (pos == -1)
1323 return do_compat_writev(fd, vec, vlen, flags);
1324
1325 return do_compat_pwritev64(fd, vec, vlen, pos, flags);
1326}
1327
Al Viro72ec3512013-03-20 10:42:10 -04001328#endif
1329
Al Viro19f4fc32013-02-24 02:17:03 -05001330static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,
1331 size_t count, loff_t max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332{
Al Viro2903ff02012-08-28 12:52:22 -04001333 struct fd in, out;
1334 struct inode *in_inode, *out_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 loff_t pos;
Al Viro7995bd22013-06-20 18:58:36 +04001336 loff_t out_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 ssize_t retval;
Al Viro2903ff02012-08-28 12:52:22 -04001338 int fl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
1340 /*
1341 * Get input file, and verify that it is ok..
1342 */
1343 retval = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -04001344 in = fdget(in_fd);
1345 if (!in.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 goto out;
Al Viro2903ff02012-08-28 12:52:22 -04001347 if (!(in.file->f_mode & FMODE_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 goto fput_in;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 retval = -ESPIPE;
Al Viro7995bd22013-06-20 18:58:36 +04001350 if (!ppos) {
1351 pos = in.file->f_pos;
1352 } else {
1353 pos = *ppos;
Al Viro2903ff02012-08-28 12:52:22 -04001354 if (!(in.file->f_mode & FMODE_PREAD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 goto fput_in;
Al Viro7995bd22013-06-20 18:58:36 +04001356 }
1357 retval = rw_verify_area(READ, in.file, &pos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -08001358 if (retval < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 goto fput_in;
Al Virobc613842016-03-31 21:48:20 -04001360 if (count > MAX_RW_COUNT)
1361 count = MAX_RW_COUNT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 /*
1364 * Get output file, and verify that it is ok..
1365 */
1366 retval = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -04001367 out = fdget(out_fd);
1368 if (!out.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 goto fput_in;
Al Viro2903ff02012-08-28 12:52:22 -04001370 if (!(out.file->f_mode & FMODE_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 goto fput_out;
1372 retval = -EINVAL;
Al Viro496ad9a2013-01-23 17:07:38 -05001373 in_inode = file_inode(in.file);
1374 out_inode = file_inode(out.file);
Al Viro7995bd22013-06-20 18:58:36 +04001375 out_pos = out.file->f_pos;
1376 retval = rw_verify_area(WRITE, out.file, &out_pos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -08001377 if (retval < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 goto fput_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 if (!max)
1381 max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes);
1382
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 if (unlikely(pos + count > max)) {
1384 retval = -EOVERFLOW;
1385 if (pos >= max)
1386 goto fput_out;
1387 count = max - pos;
1388 }
1389
Jens Axboed96e6e72007-06-11 12:18:52 +02001390 fl = 0;
Jens Axboe534f2aa2007-06-01 14:52:37 +02001391#if 0
Jens Axboed96e6e72007-06-11 12:18:52 +02001392 /*
1393 * We need to debate whether we can enable this or not. The
1394 * man page documents EAGAIN return for the output at least,
1395 * and the application is arguably buggy if it doesn't expect
1396 * EAGAIN on a non-blocking file descriptor.
1397 */
Al Viro2903ff02012-08-28 12:52:22 -04001398 if (in.file->f_flags & O_NONBLOCK)
Jens Axboed96e6e72007-06-11 12:18:52 +02001399 fl = SPLICE_F_NONBLOCK;
Jens Axboe534f2aa2007-06-01 14:52:37 +02001400#endif
Al Viro50cd2c52013-05-23 20:10:34 -04001401 file_start_write(out.file);
Al Viro7995bd22013-06-20 18:58:36 +04001402 retval = do_splice_direct(in.file, &pos, out.file, &out_pos, count, fl);
Al Viro50cd2c52013-05-23 20:10:34 -04001403 file_end_write(out.file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
1405 if (retval > 0) {
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08001406 add_rchar(current, retval);
1407 add_wchar(current, retval);
Scott Wolchoka68c2f12012-12-20 15:05:52 -08001408 fsnotify_access(in.file);
1409 fsnotify_modify(out.file);
Al Viro7995bd22013-06-20 18:58:36 +04001410 out.file->f_pos = out_pos;
1411 if (ppos)
1412 *ppos = pos;
1413 else
1414 in.file->f_pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08001417 inc_syscr(current);
1418 inc_syscw(current);
Al Viro7995bd22013-06-20 18:58:36 +04001419 if (pos > max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 retval = -EOVERFLOW;
1421
1422fput_out:
Al Viro2903ff02012-08-28 12:52:22 -04001423 fdput(out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424fput_in:
Al Viro2903ff02012-08-28 12:52:22 -04001425 fdput(in);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426out:
1427 return retval;
1428}
1429
Heiko Carstens002c8972009-01-14 14:14:18 +01001430SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd, off_t __user *, offset, size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431{
1432 loff_t pos;
1433 off_t off;
1434 ssize_t ret;
1435
1436 if (offset) {
1437 if (unlikely(get_user(off, offset)))
1438 return -EFAULT;
1439 pos = off;
1440 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
1441 if (unlikely(put_user(pos, offset)))
1442 return -EFAULT;
1443 return ret;
1444 }
1445
1446 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1447}
1448
Heiko Carstens002c8972009-01-14 14:14:18 +01001449SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd, loff_t __user *, offset, size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450{
1451 loff_t pos;
1452 ssize_t ret;
1453
1454 if (offset) {
1455 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
1456 return -EFAULT;
1457 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
1458 if (unlikely(put_user(pos, offset)))
1459 return -EFAULT;
1460 return ret;
1461 }
1462
1463 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1464}
Al Viro19f4fc32013-02-24 02:17:03 -05001465
1466#ifdef CONFIG_COMPAT
1467COMPAT_SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd,
1468 compat_off_t __user *, offset, compat_size_t, count)
1469{
1470 loff_t pos;
1471 off_t off;
1472 ssize_t ret;
1473
1474 if (offset) {
1475 if (unlikely(get_user(off, offset)))
1476 return -EFAULT;
1477 pos = off;
1478 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
1479 if (unlikely(put_user(pos, offset)))
1480 return -EFAULT;
1481 return ret;
1482 }
1483
1484 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1485}
1486
1487COMPAT_SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd,
1488 compat_loff_t __user *, offset, compat_size_t, count)
1489{
1490 loff_t pos;
1491 ssize_t ret;
1492
1493 if (offset) {
1494 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
1495 return -EFAULT;
1496 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
1497 if (unlikely(put_user(pos, offset)))
1498 return -EFAULT;
1499 return ret;
1500 }
1501
1502 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1503}
1504#endif
Zach Brown29732932015-11-10 16:53:30 -05001505
1506/*
1507 * copy_file_range() differs from regular file read and write in that it
1508 * specifically allows return partial success. When it does so is up to
1509 * the copy_file_range method.
1510 */
1511ssize_t vfs_copy_file_range(struct file *file_in, loff_t pos_in,
1512 struct file *file_out, loff_t pos_out,
1513 size_t len, unsigned int flags)
1514{
1515 struct inode *inode_in = file_inode(file_in);
1516 struct inode *inode_out = file_inode(file_out);
1517 ssize_t ret;
1518
1519 if (flags != 0)
1520 return -EINVAL;
1521
Zach Brown29732932015-11-10 16:53:30 -05001522 ret = rw_verify_area(READ, file_in, &pos_in, len);
Al Virobc613842016-03-31 21:48:20 -04001523 if (unlikely(ret))
1524 return ret;
1525
1526 ret = rw_verify_area(WRITE, file_out, &pos_out, len);
1527 if (unlikely(ret))
Zach Brown29732932015-11-10 16:53:30 -05001528 return ret;
1529
1530 if (!(file_in->f_mode & FMODE_READ) ||
1531 !(file_out->f_mode & FMODE_WRITE) ||
Anna Schumakereac70052015-11-10 16:53:33 -05001532 (file_out->f_flags & O_APPEND))
Zach Brown29732932015-11-10 16:53:30 -05001533 return -EBADF;
1534
1535 /* this could be relaxed once a method supports cross-fs copies */
1536 if (inode_in->i_sb != inode_out->i_sb)
1537 return -EXDEV;
1538
1539 if (len == 0)
1540 return 0;
1541
Miklos Szeredi36161192016-12-16 11:02:54 +01001542 sb_start_write(inode_out->i_sb);
Zach Brown29732932015-11-10 16:53:30 -05001543
Christoph Hellwiga76b5b02016-12-09 16:17:19 -08001544 /*
1545 * Try cloning first, this is supported by more file systems, and
1546 * more efficient if both clone and copy are supported (e.g. NFS).
1547 */
1548 if (file_in->f_op->clone_file_range) {
1549 ret = file_in->f_op->clone_file_range(file_in, pos_in,
1550 file_out, pos_out, len);
1551 if (ret == 0) {
1552 ret = len;
1553 goto done;
1554 }
1555 }
1556
1557 if (file_out->f_op->copy_file_range) {
Anna Schumakereac70052015-11-10 16:53:33 -05001558 ret = file_out->f_op->copy_file_range(file_in, pos_in, file_out,
1559 pos_out, len, flags);
Christoph Hellwiga76b5b02016-12-09 16:17:19 -08001560 if (ret != -EOPNOTSUPP)
1561 goto done;
1562 }
Anna Schumakereac70052015-11-10 16:53:33 -05001563
Christoph Hellwiga76b5b02016-12-09 16:17:19 -08001564 ret = do_splice_direct(file_in, &pos_in, file_out, &pos_out,
1565 len > MAX_RW_COUNT ? MAX_RW_COUNT : len, 0);
1566
1567done:
Zach Brown29732932015-11-10 16:53:30 -05001568 if (ret > 0) {
1569 fsnotify_access(file_in);
1570 add_rchar(current, ret);
1571 fsnotify_modify(file_out);
1572 add_wchar(current, ret);
1573 }
Christoph Hellwiga76b5b02016-12-09 16:17:19 -08001574
Zach Brown29732932015-11-10 16:53:30 -05001575 inc_syscr(current);
1576 inc_syscw(current);
1577
Miklos Szeredi36161192016-12-16 11:02:54 +01001578 sb_end_write(inode_out->i_sb);
Zach Brown29732932015-11-10 16:53:30 -05001579
1580 return ret;
1581}
1582EXPORT_SYMBOL(vfs_copy_file_range);
1583
1584SYSCALL_DEFINE6(copy_file_range, int, fd_in, loff_t __user *, off_in,
1585 int, fd_out, loff_t __user *, off_out,
1586 size_t, len, unsigned int, flags)
1587{
1588 loff_t pos_in;
1589 loff_t pos_out;
1590 struct fd f_in;
1591 struct fd f_out;
1592 ssize_t ret = -EBADF;
1593
1594 f_in = fdget(fd_in);
1595 if (!f_in.file)
1596 goto out2;
1597
1598 f_out = fdget(fd_out);
1599 if (!f_out.file)
1600 goto out1;
1601
1602 ret = -EFAULT;
1603 if (off_in) {
1604 if (copy_from_user(&pos_in, off_in, sizeof(loff_t)))
1605 goto out;
1606 } else {
1607 pos_in = f_in.file->f_pos;
1608 }
1609
1610 if (off_out) {
1611 if (copy_from_user(&pos_out, off_out, sizeof(loff_t)))
1612 goto out;
1613 } else {
1614 pos_out = f_out.file->f_pos;
1615 }
1616
1617 ret = vfs_copy_file_range(f_in.file, pos_in, f_out.file, pos_out, len,
1618 flags);
1619 if (ret > 0) {
1620 pos_in += ret;
1621 pos_out += ret;
1622
1623 if (off_in) {
1624 if (copy_to_user(off_in, &pos_in, sizeof(loff_t)))
1625 ret = -EFAULT;
1626 } else {
1627 f_in.file->f_pos = pos_in;
1628 }
1629
1630 if (off_out) {
1631 if (copy_to_user(off_out, &pos_out, sizeof(loff_t)))
1632 ret = -EFAULT;
1633 } else {
1634 f_out.file->f_pos = pos_out;
1635 }
1636 }
1637
1638out:
1639 fdput(f_out);
1640out1:
1641 fdput(f_in);
1642out2:
1643 return ret;
1644}
Christoph Hellwig04b38d62015-12-03 12:59:50 +01001645
1646static int clone_verify_area(struct file *file, loff_t pos, u64 len, bool write)
1647{
1648 struct inode *inode = file_inode(file);
1649
1650 if (unlikely(pos < 0))
1651 return -EINVAL;
1652
1653 if (unlikely((loff_t) (pos + len) < 0))
1654 return -EINVAL;
1655
1656 if (unlikely(inode->i_flctx && mandatory_lock(inode))) {
1657 loff_t end = len ? pos + len - 1 : OFFSET_MAX;
1658 int retval;
1659
1660 retval = locks_mandatory_area(inode, file, pos, end,
1661 write ? F_WRLCK : F_RDLCK);
1662 if (retval < 0)
1663 return retval;
1664 }
1665
1666 return security_file_permission(file, write ? MAY_WRITE : MAY_READ);
1667}
1668
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001669/*
1670 * Check that the two inodes are eligible for cloning, the ranges make
1671 * sense, and then flush all dirty data. Caller must ensure that the
1672 * inodes have been locked against any other modifications.
Darrick J. Wong22725ce2016-12-19 15:13:26 -08001673 *
1674 * Returns: 0 for "nothing to clone", 1 for "something to clone", or
1675 * the usual negative error code.
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001676 */
1677int vfs_clone_file_prep_inodes(struct inode *inode_in, loff_t pos_in,
1678 struct inode *inode_out, loff_t pos_out,
1679 u64 *len, bool is_dedupe)
1680{
1681 loff_t bs = inode_out->i_sb->s_blocksize;
1682 loff_t blen;
1683 loff_t isize;
1684 bool same_inode = (inode_in == inode_out);
1685 int ret;
1686
1687 /* Don't touch certain kinds of inodes */
1688 if (IS_IMMUTABLE(inode_out))
1689 return -EPERM;
1690
1691 if (IS_SWAPFILE(inode_in) || IS_SWAPFILE(inode_out))
1692 return -ETXTBSY;
1693
1694 /* Don't reflink dirs, pipes, sockets... */
1695 if (S_ISDIR(inode_in->i_mode) || S_ISDIR(inode_out->i_mode))
1696 return -EISDIR;
1697 if (!S_ISREG(inode_in->i_mode) || !S_ISREG(inode_out->i_mode))
1698 return -EINVAL;
1699
1700 /* Are we going all the way to the end? */
1701 isize = i_size_read(inode_in);
Darrick J. Wong22725ce2016-12-19 15:13:26 -08001702 if (isize == 0)
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001703 return 0;
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001704
1705 /* Zero length dedupe exits immediately; reflink goes to EOF. */
1706 if (*len == 0) {
Darrick J. Wong22725ce2016-12-19 15:13:26 -08001707 if (is_dedupe || pos_in == isize)
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001708 return 0;
Darrick J. Wong22725ce2016-12-19 15:13:26 -08001709 if (pos_in > isize)
1710 return -EINVAL;
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001711 *len = isize - pos_in;
1712 }
1713
1714 /* Ensure offsets don't wrap and the input is inside i_size */
1715 if (pos_in + *len < pos_in || pos_out + *len < pos_out ||
1716 pos_in + *len > isize)
1717 return -EINVAL;
1718
1719 /* Don't allow dedupe past EOF in the dest file */
1720 if (is_dedupe) {
1721 loff_t disize;
1722
1723 disize = i_size_read(inode_out);
1724 if (pos_out >= disize || pos_out + *len > disize)
1725 return -EINVAL;
1726 }
1727
1728 /* If we're linking to EOF, continue to the block boundary. */
1729 if (pos_in + *len == isize)
1730 blen = ALIGN(isize, bs) - pos_in;
1731 else
1732 blen = *len;
1733
1734 /* Only reflink if we're aligned to block boundaries */
1735 if (!IS_ALIGNED(pos_in, bs) || !IS_ALIGNED(pos_in + blen, bs) ||
1736 !IS_ALIGNED(pos_out, bs) || !IS_ALIGNED(pos_out + blen, bs))
1737 return -EINVAL;
1738
1739 /* Don't allow overlapped reflink within the same file */
1740 if (same_inode) {
1741 if (pos_out + blen > pos_in && pos_out < pos_in + blen)
1742 return -EINVAL;
1743 }
1744
1745 /* Wait for the completion of any pending IOs on both files */
1746 inode_dio_wait(inode_in);
1747 if (!same_inode)
1748 inode_dio_wait(inode_out);
1749
1750 ret = filemap_write_and_wait_range(inode_in->i_mapping,
1751 pos_in, pos_in + *len - 1);
1752 if (ret)
1753 return ret;
1754
1755 ret = filemap_write_and_wait_range(inode_out->i_mapping,
1756 pos_out, pos_out + *len - 1);
1757 if (ret)
1758 return ret;
1759
1760 /*
1761 * Check that the extents are the same.
1762 */
1763 if (is_dedupe) {
1764 bool is_same = false;
1765
1766 ret = vfs_dedupe_file_range_compare(inode_in, pos_in,
1767 inode_out, pos_out, *len, &is_same);
1768 if (ret)
1769 return ret;
1770 if (!is_same)
1771 return -EBADE;
1772 }
1773
Darrick J. Wong22725ce2016-12-19 15:13:26 -08001774 return 1;
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001775}
1776EXPORT_SYMBOL(vfs_clone_file_prep_inodes);
1777
Christoph Hellwig04b38d62015-12-03 12:59:50 +01001778int vfs_clone_file_range(struct file *file_in, loff_t pos_in,
1779 struct file *file_out, loff_t pos_out, u64 len)
1780{
1781 struct inode *inode_in = file_inode(file_in);
1782 struct inode *inode_out = file_inode(file_out);
1783 int ret;
1784
Amir Goldsteinb335e9d2016-10-26 22:34:01 +03001785 if (S_ISDIR(inode_in->i_mode) || S_ISDIR(inode_out->i_mode))
1786 return -EISDIR;
1787 if (!S_ISREG(inode_in->i_mode) || !S_ISREG(inode_out->i_mode))
1788 return -EINVAL;
1789
Amir Goldstein913b86e2016-09-23 11:38:10 +03001790 /*
1791 * FICLONE/FICLONERANGE ioctls enforce that src and dest files are on
1792 * the same mount. Practically, they only need to be on the same file
1793 * system.
1794 */
1795 if (inode_in->i_sb != inode_out->i_sb)
Christoph Hellwig04b38d62015-12-03 12:59:50 +01001796 return -EXDEV;
1797
Christoph Hellwig04b38d62015-12-03 12:59:50 +01001798 if (!(file_in->f_mode & FMODE_READ) ||
1799 !(file_out->f_mode & FMODE_WRITE) ||
Christoph Hellwig0fcbf992016-02-26 18:53:12 +01001800 (file_out->f_flags & O_APPEND))
Christoph Hellwig04b38d62015-12-03 12:59:50 +01001801 return -EBADF;
1802
Christoph Hellwig0fcbf992016-02-26 18:53:12 +01001803 if (!file_in->f_op->clone_file_range)
1804 return -EOPNOTSUPP;
1805
Christoph Hellwig04b38d62015-12-03 12:59:50 +01001806 ret = clone_verify_area(file_in, pos_in, len, false);
1807 if (ret)
1808 return ret;
1809
1810 ret = clone_verify_area(file_out, pos_out, len, true);
1811 if (ret)
1812 return ret;
1813
1814 if (pos_in + len > i_size_read(inode_in))
1815 return -EINVAL;
1816
Christoph Hellwig04b38d62015-12-03 12:59:50 +01001817 ret = file_in->f_op->clone_file_range(file_in, pos_in,
1818 file_out, pos_out, len);
1819 if (!ret) {
1820 fsnotify_access(file_in);
1821 fsnotify_modify(file_out);
1822 }
1823
Christoph Hellwig04b38d62015-12-03 12:59:50 +01001824 return ret;
1825}
1826EXPORT_SYMBOL(vfs_clone_file_range);
Darrick J. Wong54dbc152015-12-19 00:55:59 -08001827
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001828/*
1829 * Read a page's worth of file data into the page cache. Return the page
1830 * locked.
1831 */
1832static struct page *vfs_dedupe_get_page(struct inode *inode, loff_t offset)
1833{
1834 struct address_space *mapping;
1835 struct page *page;
1836 pgoff_t n;
1837
1838 n = offset >> PAGE_SHIFT;
1839 mapping = inode->i_mapping;
1840 page = read_mapping_page(mapping, n, NULL);
1841 if (IS_ERR(page))
1842 return page;
1843 if (!PageUptodate(page)) {
1844 put_page(page);
1845 return ERR_PTR(-EIO);
1846 }
1847 lock_page(page);
1848 return page;
1849}
1850
1851/*
1852 * Compare extents of two files to see if they are the same.
1853 * Caller must have locked both inodes to prevent write races.
1854 */
1855int vfs_dedupe_file_range_compare(struct inode *src, loff_t srcoff,
1856 struct inode *dest, loff_t destoff,
1857 loff_t len, bool *is_same)
1858{
1859 loff_t src_poff;
1860 loff_t dest_poff;
1861 void *src_addr;
1862 void *dest_addr;
1863 struct page *src_page;
1864 struct page *dest_page;
1865 loff_t cmp_len;
1866 bool same;
1867 int error;
1868
1869 error = -EINVAL;
1870 same = true;
1871 while (len) {
1872 src_poff = srcoff & (PAGE_SIZE - 1);
1873 dest_poff = destoff & (PAGE_SIZE - 1);
1874 cmp_len = min(PAGE_SIZE - src_poff,
1875 PAGE_SIZE - dest_poff);
1876 cmp_len = min(cmp_len, len);
1877 if (cmp_len <= 0)
1878 goto out_error;
1879
1880 src_page = vfs_dedupe_get_page(src, srcoff);
1881 if (IS_ERR(src_page)) {
1882 error = PTR_ERR(src_page);
1883 goto out_error;
1884 }
1885 dest_page = vfs_dedupe_get_page(dest, destoff);
1886 if (IS_ERR(dest_page)) {
1887 error = PTR_ERR(dest_page);
1888 unlock_page(src_page);
1889 put_page(src_page);
1890 goto out_error;
1891 }
1892 src_addr = kmap_atomic(src_page);
1893 dest_addr = kmap_atomic(dest_page);
1894
1895 flush_dcache_page(src_page);
1896 flush_dcache_page(dest_page);
1897
1898 if (memcmp(src_addr + src_poff, dest_addr + dest_poff, cmp_len))
1899 same = false;
1900
1901 kunmap_atomic(dest_addr);
1902 kunmap_atomic(src_addr);
1903 unlock_page(dest_page);
1904 unlock_page(src_page);
1905 put_page(dest_page);
1906 put_page(src_page);
1907
1908 if (!same)
1909 break;
1910
1911 srcoff += cmp_len;
1912 destoff += cmp_len;
1913 len -= cmp_len;
1914 }
1915
1916 *is_same = same;
1917 return 0;
1918
1919out_error:
1920 return error;
1921}
1922EXPORT_SYMBOL(vfs_dedupe_file_range_compare);
1923
Darrick J. Wong54dbc152015-12-19 00:55:59 -08001924int vfs_dedupe_file_range(struct file *file, struct file_dedupe_range *same)
1925{
1926 struct file_dedupe_range_info *info;
1927 struct inode *src = file_inode(file);
1928 u64 off;
1929 u64 len;
1930 int i;
1931 int ret;
1932 bool is_admin = capable(CAP_SYS_ADMIN);
1933 u16 count = same->dest_count;
1934 struct file *dst_file;
1935 loff_t dst_off;
1936 ssize_t deduped;
1937
1938 if (!(file->f_mode & FMODE_READ))
1939 return -EINVAL;
1940
1941 if (same->reserved1 || same->reserved2)
1942 return -EINVAL;
1943
1944 off = same->src_offset;
1945 len = same->src_length;
1946
1947 ret = -EISDIR;
1948 if (S_ISDIR(src->i_mode))
1949 goto out;
1950
1951 ret = -EINVAL;
1952 if (!S_ISREG(src->i_mode))
1953 goto out;
1954
1955 ret = clone_verify_area(file, off, len, false);
1956 if (ret < 0)
1957 goto out;
1958 ret = 0;
1959
Darrick J. Wong22725ce2016-12-19 15:13:26 -08001960 if (off + len > i_size_read(src))
1961 return -EINVAL;
1962
Darrick J. Wong54dbc152015-12-19 00:55:59 -08001963 /* pre-format output fields to sane values */
1964 for (i = 0; i < count; i++) {
1965 same->info[i].bytes_deduped = 0ULL;
1966 same->info[i].status = FILE_DEDUPE_RANGE_SAME;
1967 }
1968
1969 for (i = 0, info = same->info; i < count; i++, info++) {
1970 struct inode *dst;
1971 struct fd dst_fd = fdget(info->dest_fd);
1972
1973 dst_file = dst_fd.file;
1974 if (!dst_file) {
1975 info->status = -EBADF;
1976 goto next_loop;
1977 }
1978 dst = file_inode(dst_file);
1979
1980 ret = mnt_want_write_file(dst_file);
1981 if (ret) {
1982 info->status = ret;
1983 goto next_loop;
1984 }
1985
1986 dst_off = info->dest_offset;
1987 ret = clone_verify_area(dst_file, dst_off, len, true);
1988 if (ret < 0) {
1989 info->status = ret;
1990 goto next_file;
1991 }
1992 ret = 0;
1993
1994 if (info->reserved) {
1995 info->status = -EINVAL;
1996 } else if (!(is_admin || (dst_file->f_mode & FMODE_WRITE))) {
1997 info->status = -EINVAL;
1998 } else if (file->f_path.mnt != dst_file->f_path.mnt) {
1999 info->status = -EXDEV;
2000 } else if (S_ISDIR(dst->i_mode)) {
2001 info->status = -EISDIR;
2002 } else if (dst_file->f_op->dedupe_file_range == NULL) {
2003 info->status = -EINVAL;
2004 } else {
2005 deduped = dst_file->f_op->dedupe_file_range(file, off,
2006 len, dst_file,
2007 info->dest_offset);
2008 if (deduped == -EBADE)
2009 info->status = FILE_DEDUPE_RANGE_DIFFERS;
2010 else if (deduped < 0)
2011 info->status = deduped;
2012 else
2013 info->bytes_deduped += deduped;
2014 }
2015
2016next_file:
2017 mnt_drop_write_file(dst_file);
2018next_loop:
2019 fdput(dst_fd);
Darrick J. Wonge62e5602016-01-22 16:58:28 -08002020
2021 if (fatal_signal_pending(current))
2022 goto out;
Darrick J. Wong54dbc152015-12-19 00:55:59 -08002023 }
2024
2025out:
2026 return ret;
2027}
2028EXPORT_SYMBOL(vfs_dedupe_file_range);