blob: f8547b82dfb36e6e0a39fb91520addbc4b5a2d75 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/fs/read_write.c
4 *
5 * Copyright (C) 1991, 1992 Linus Torvalds
6 */
7
Ingo Molnarb12fb7f2017-02-08 18:51:33 +01008#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/stat.h>
Ingo Molnarb12fb7f2017-02-08 18:51:33 +010010#include <linux/sched/xacct.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/fcntl.h>
12#include <linux/file.h>
13#include <linux/uio.h>
Robert Love0eeca282005-07-12 17:06:03 -040014#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/security.h>
Paul Gortmaker630d9c42011-11-16 23:57:37 -050016#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/syscalls.h>
Linus Torvaldse28cc712006-01-04 16:20:40 -080018#include <linux/pagemap.h>
Jens Axboed6b29d72007-06-04 09:59:47 +020019#include <linux/splice.h>
Al Viro561c6732013-02-24 10:52:26 -050020#include <linux/compat.h>
Zach Brown29732932015-11-10 16:53:30 -050021#include <linux/mount.h>
Wouter van Kesteren2feb55f2016-02-16 22:20:59 +010022#include <linux/fs.h>
Al Viro06ae43f2013-03-20 13:19:30 -040023#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080025#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <asm/unistd.h>
27
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080028const struct file_operations generic_ro_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 .llseek = generic_file_llseek,
Al Viroaad4f8b2014-04-02 14:33:16 -040030 .read_iter = generic_file_read_iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 .mmap = generic_file_readonly_mmap,
Jens Axboe534f2aa2007-06-01 14:52:37 +020032 .splice_read = generic_file_splice_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -070033};
34
35EXPORT_SYMBOL(generic_ro_fops);
36
Christoph Hellwigddef7ed2017-07-06 18:58:37 +020037static inline bool unsigned_offsets(struct file *file)
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -070038{
Al Virocccb5a12010-12-17 07:44:05 -050039 return file->f_mode & FMODE_UNSIGNED_OFFSET;
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -070040}
41
Jie 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 */
Andreas Gruenbacherfc468202017-09-25 12:23:03 +0200116 if ((unsigned long long)offset >= eof)
Josef Bacik982d8162011-07-18 13:21:35 -0400117 return -ENXIO;
118 break;
119 case SEEK_HOLE:
120 /*
121 * There is a virtual hole at the end of the file, so as long as
122 * offset isn't i_size or larger, return i_size.
123 */
Andreas Gruenbacherfc468202017-09-25 12:23:03 +0200124 if ((unsigned long long)offset >= eof)
Josef Bacik982d8162011-07-18 13:21:35 -0400125 return -ENXIO;
Eric Sandeene8b96eb2012-04-30 13:11:29 -0500126 offset = eof;
Josef Bacik982d8162011-07-18 13:21:35 -0400127 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 }
Christoph Hellwig3a8cff42008-08-11 15:37:17 +0200129
Jie 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,
Wouter van Kesteren2feb55f2016-02-16 22:20:59 +0100186 OFFSET_MAX, 0);
Al Virob25472f2015-12-05 22:04:48 -0500187 default:
188 return -EINVAL;
189 }
190}
191EXPORT_SYMBOL(no_seek_end_llseek);
192
193/**
194 * no_seek_end_llseek_size - llseek implementation for fixed-sized devices
195 * @file: file structure to seek on
196 * @offset: file offset to seek to
197 * @whence: type of seek
198 * @size: maximal offset allowed
199 *
200 */
201loff_t no_seek_end_llseek_size(struct file *file, loff_t offset, int whence, loff_t size)
202{
203 switch (whence) {
204 case SEEK_SET: case SEEK_CUR:
205 return generic_file_llseek_size(file, offset, whence,
206 size, 0);
207 default:
208 return -EINVAL;
209 }
210}
211EXPORT_SYMBOL(no_seek_end_llseek_size);
212
213/**
jan Blunckae6afc32010-05-26 14:44:48 -0700214 * noop_llseek - No Operation Performed llseek implementation
215 * @file: file structure to seek on
216 * @offset: file offset to seek to
Andrew Morton965c8e52012-12-17 15:59:39 -0800217 * @whence: type of seek
jan Blunckae6afc32010-05-26 14:44:48 -0700218 *
219 * This is an implementation of ->llseek useable for the rare special case when
220 * userspace expects the seek to succeed but the (device) file is actually not
221 * able to perform the seek. In this case you use noop_llseek() instead of
222 * falling back to the default implementation of ->llseek.
223 */
Andrew Morton965c8e52012-12-17 15:59:39 -0800224loff_t noop_llseek(struct file *file, loff_t offset, int whence)
jan Blunckae6afc32010-05-26 14:44:48 -0700225{
226 return file->f_pos;
227}
228EXPORT_SYMBOL(noop_llseek);
229
Andrew Morton965c8e52012-12-17 15:59:39 -0800230loff_t no_llseek(struct file *file, loff_t offset, int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
232 return -ESPIPE;
233}
234EXPORT_SYMBOL(no_llseek);
235
Andrew Morton965c8e52012-12-17 15:59:39 -0800236loff_t default_llseek(struct file *file, loff_t offset, int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
Al Viro496ad9a2013-01-23 17:07:38 -0500238 struct inode *inode = file_inode(file);
David Sterba16abef02008-04-22 15:09:22 +0200239 loff_t retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Al Viro59551022016-01-22 15:40:57 -0500241 inode_lock(inode);
Andrew Morton965c8e52012-12-17 15:59:39 -0800242 switch (whence) {
Chris Snook7b8e8922007-05-08 00:24:13 -0700243 case SEEK_END:
Josef Bacik982d8162011-07-18 13:21:35 -0400244 offset += i_size_read(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 break;
Chris Snook7b8e8922007-05-08 00:24:13 -0700246 case SEEK_CUR:
Alain Knaff5b6f1eb2008-11-10 17:08:08 -0800247 if (offset == 0) {
248 retval = file->f_pos;
249 goto out;
250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 offset += file->f_pos;
Josef Bacik982d8162011-07-18 13:21:35 -0400252 break;
253 case SEEK_DATA:
254 /*
255 * In the generic case the entire file is data, so as
256 * long as offset isn't at the end of the file then the
257 * offset is data.
258 */
Dan Carpenterbacb2d82011-07-26 17:25:20 +0300259 if (offset >= inode->i_size) {
260 retval = -ENXIO;
261 goto out;
262 }
Josef Bacik982d8162011-07-18 13:21:35 -0400263 break;
264 case SEEK_HOLE:
265 /*
266 * There is a virtual hole at the end of the file, so
267 * as long as offset isn't i_size or larger, return
268 * i_size.
269 */
Dan Carpenterbacb2d82011-07-26 17:25:20 +0300270 if (offset >= inode->i_size) {
271 retval = -ENXIO;
272 goto out;
273 }
Josef Bacik982d8162011-07-18 13:21:35 -0400274 offset = inode->i_size;
275 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 }
277 retval = -EINVAL;
Al Virocccb5a12010-12-17 07:44:05 -0500278 if (offset >= 0 || unsigned_offsets(file)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 if (offset != file->f_pos) {
280 file->f_pos = offset;
281 file->f_version = 0;
282 }
283 retval = offset;
284 }
Alain Knaff5b6f1eb2008-11-10 17:08:08 -0800285out:
Al Viro59551022016-01-22 15:40:57 -0500286 inode_unlock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 return retval;
288}
289EXPORT_SYMBOL(default_llseek);
290
Andrew Morton965c8e52012-12-17 15:59:39 -0800291loff_t vfs_llseek(struct file *file, loff_t offset, int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
293 loff_t (*fn)(struct file *, loff_t, int);
294
295 fn = no_llseek;
296 if (file->f_mode & FMODE_LSEEK) {
Al Viro72c2d532013-09-22 16:27:52 -0400297 if (file->f_op->llseek)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 fn = file->f_op->llseek;
299 }
Andrew Morton965c8e52012-12-17 15:59:39 -0800300 return fn(file, offset, whence);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301}
302EXPORT_SYMBOL(vfs_llseek);
303
Andrew Morton965c8e52012-12-17 15:59:39 -0800304SYSCALL_DEFINE3(lseek, unsigned int, fd, off_t, offset, unsigned int, whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
306 off_t retval;
Linus Torvalds9c225f22014-03-03 09:36:58 -0800307 struct fd f = fdget_pos(fd);
Al Viro2903ff02012-08-28 12:52:22 -0400308 if (!f.file)
309 return -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
311 retval = -EINVAL;
Andrew Morton965c8e52012-12-17 15:59:39 -0800312 if (whence <= SEEK_MAX) {
313 loff_t res = vfs_llseek(f.file, offset, whence);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 retval = res;
315 if (res != (loff_t)retval)
316 retval = -EOVERFLOW; /* LFS: should only happen on 32 bit platforms */
317 }
Linus Torvalds9c225f22014-03-03 09:36:58 -0800318 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 return retval;
320}
321
Al Viro561c6732013-02-24 10:52:26 -0500322#ifdef CONFIG_COMPAT
323COMPAT_SYSCALL_DEFINE3(lseek, unsigned int, fd, compat_off_t, offset, unsigned int, whence)
324{
325 return sys_lseek(fd, offset, whence);
326}
327#endif
328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329#ifdef __ARCH_WANT_SYS_LLSEEK
Heiko Carstens003d7ab2009-01-14 14:14:21 +0100330SYSCALL_DEFINE5(llseek, unsigned int, fd, unsigned long, offset_high,
331 unsigned long, offset_low, loff_t __user *, result,
Andrew Morton965c8e52012-12-17 15:59:39 -0800332 unsigned int, whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
334 int retval;
Eric Biggersd7a15f82014-03-16 14:24:08 -0500335 struct fd f = fdget_pos(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 loff_t offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Al Viro2903ff02012-08-28 12:52:22 -0400338 if (!f.file)
339 return -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 retval = -EINVAL;
Andrew Morton965c8e52012-12-17 15:59:39 -0800342 if (whence > SEEK_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 goto out_putf;
344
Al Viro2903ff02012-08-28 12:52:22 -0400345 offset = vfs_llseek(f.file, ((loff_t) offset_high << 32) | offset_low,
Andrew Morton965c8e52012-12-17 15:59:39 -0800346 whence);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 retval = (int)offset;
349 if (offset >= 0) {
350 retval = -EFAULT;
351 if (!copy_to_user(result, &offset, sizeof(offset)))
352 retval = 0;
353 }
354out_putf:
Eric Biggersd7a15f82014-03-16 14:24:08 -0500355 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return retval;
357}
358#endif
359
Al Viro68d70d02013-06-19 15:26:04 +0400360int rw_verify_area(int read_write, struct file *file, const loff_t *ppos, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
362 struct inode *inode;
363 loff_t pos;
James Morrisc43e2592008-01-12 22:05:48 +1100364 int retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Al Viro496ad9a2013-01-23 17:07:38 -0500366 inode = file_inode(file);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800367 if (unlikely((ssize_t) count < 0))
James Morrisc43e2592008-01-12 22:05:48 +1100368 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 pos = *ppos;
Al Virocccb5a12010-12-17 07:44:05 -0500370 if (unlikely(pos < 0)) {
371 if (!unsigned_offsets(file))
372 return retval;
373 if (count >= -pos) /* both values are in 0..LLONG_MAX */
374 return -EOVERFLOW;
375 } else if (unlikely((loff_t) (pos + count) < 0)) {
376 if (!unsigned_offsets(file))
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -0700377 return retval;
378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Jeff Laytonbd61e0a2015-01-16 15:05:55 -0500380 if (unlikely(inode->i_flctx && mandatory_lock(inode))) {
Christoph Hellwigacc15572015-12-03 12:59:49 +0100381 retval = locks_mandatory_area(inode, file, pos, pos + count - 1,
382 read_write == READ ? F_RDLCK : F_WRLCK);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800383 if (retval < 0)
384 return retval;
385 }
Al Virobc613842016-03-31 21:48:20 -0400386 return security_file_permission(file,
James Morrisc43e2592008-01-12 22:05:48 +1100387 read_write == READ ? MAY_READ : MAY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
389
Al Viro5d5d5682015-04-03 15:41:18 -0400390static ssize_t new_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
Al Viro293bc982014-02-11 18:37:41 -0500391{
392 struct iovec iov = { .iov_base = buf, .iov_len = len };
393 struct kiocb kiocb;
394 struct iov_iter iter;
395 ssize_t ret;
396
397 init_sync_kiocb(&kiocb, filp);
398 kiocb.ki_pos = *ppos;
Al Viro293bc982014-02-11 18:37:41 -0500399 iov_iter_init(&iter, READ, &iov, 1, len);
400
Miklos Szeredibb7462b2017-02-20 16:51:23 +0100401 ret = call_read_iter(filp, &kiocb, &iter);
Christoph Hellwig599bd192015-02-11 19:59:44 +0100402 BUG_ON(ret == -EIOCBQUEUED);
Al Viro293bc982014-02-11 18:37:41 -0500403 *ppos = kiocb.ki_pos;
404 return ret;
405}
406
Dmitry Kasatkin6fb50322014-11-05 17:01:17 +0200407ssize_t __vfs_read(struct file *file, char __user *buf, size_t count,
408 loff_t *pos)
409{
Dmitry Kasatkin6fb50322014-11-05 17:01:17 +0200410 if (file->f_op->read)
Al Viro3d04c8a2015-04-03 15:09:18 -0400411 return file->f_op->read(file, buf, count, pos);
Dmitry Kasatkin6fb50322014-11-05 17:01:17 +0200412 else if (file->f_op->read_iter)
Al Viro3d04c8a2015-04-03 15:09:18 -0400413 return new_sync_read(file, buf, count, pos);
Dmitry Kasatkin6fb50322014-11-05 17:01:17 +0200414 else
Al Viro3d04c8a2015-04-03 15:09:18 -0400415 return -EINVAL;
Dmitry Kasatkin6fb50322014-11-05 17:01:17 +0200416}
417
Christoph Hellwigbdd1d2d2017-09-01 17:39:13 +0200418ssize_t kernel_read(struct file *file, void *buf, size_t count, loff_t *pos)
Christoph Hellwigc41fbad2017-09-01 17:39:12 +0200419{
420 mm_segment_t old_fs;
Christoph Hellwigbdd1d2d2017-09-01 17:39:13 +0200421 ssize_t result;
Christoph Hellwigc41fbad2017-09-01 17:39:12 +0200422
423 old_fs = get_fs();
424 set_fs(get_ds());
425 /* The cast to a user pointer is valid due to the set_fs() */
Christoph Hellwigbdd1d2d2017-09-01 17:39:13 +0200426 result = vfs_read(file, (void __user *)buf, count, pos);
Christoph Hellwigc41fbad2017-09-01 17:39:12 +0200427 set_fs(old_fs);
428 return result;
429}
430EXPORT_SYMBOL(kernel_read);
Al Viro293bc982014-02-11 18:37:41 -0500431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
433{
434 ssize_t ret;
435
436 if (!(file->f_mode & FMODE_READ))
437 return -EBADF;
Al Viro7f7f25e2014-02-11 17:49:24 -0500438 if (!(file->f_mode & FMODE_CAN_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 return -EINVAL;
440 if (unlikely(!access_ok(VERIFY_WRITE, buf, count)))
441 return -EFAULT;
442
443 ret = rw_verify_area(READ, file, pos, count);
Al Virobc613842016-03-31 21:48:20 -0400444 if (!ret) {
445 if (count > MAX_RW_COUNT)
446 count = MAX_RW_COUNT;
Dmitry Kasatkin6fb50322014-11-05 17:01:17 +0200447 ret = __vfs_read(file, buf, count, pos);
James Morrisc43e2592008-01-12 22:05:48 +1100448 if (ret > 0) {
Eric Paris2a12a9d2009-12-17 21:24:21 -0500449 fsnotify_access(file);
James Morrisc43e2592008-01-12 22:05:48 +1100450 add_rchar(current, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 }
James Morrisc43e2592008-01-12 22:05:48 +1100452 inc_syscr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 }
454
455 return ret;
456}
457
Al Viro5d5d5682015-04-03 15:41:18 -0400458static 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 -0500459{
460 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len };
461 struct kiocb kiocb;
462 struct iov_iter iter;
463 ssize_t ret;
464
465 init_sync_kiocb(&kiocb, filp);
466 kiocb.ki_pos = *ppos;
Al Viro293bc982014-02-11 18:37:41 -0500467 iov_iter_init(&iter, WRITE, &iov, 1, len);
468
Miklos Szeredibb7462b2017-02-20 16:51:23 +0100469 ret = call_write_iter(filp, &kiocb, &iter);
Christoph Hellwig599bd192015-02-11 19:59:44 +0100470 BUG_ON(ret == -EIOCBQUEUED);
Al Virof765b132015-04-06 20:50:38 -0400471 if (ret > 0)
472 *ppos = kiocb.ki_pos;
Al Viro293bc982014-02-11 18:37:41 -0500473 return ret;
474}
475
Al Viro493c84c2015-04-03 15:06:43 -0400476ssize_t __vfs_write(struct file *file, const char __user *p, size_t count,
477 loff_t *pos)
478{
479 if (file->f_op->write)
480 return file->f_op->write(file, p, count, pos);
Al Viro493c84c2015-04-03 15:06:43 -0400481 else if (file->f_op->write_iter)
482 return new_sync_write(file, p, count, pos);
483 else
484 return -EINVAL;
485}
Al Viro493c84c2015-04-03 15:06:43 -0400486
Christoph Hellwig73e18f72017-09-01 17:39:15 +0200487ssize_t __kernel_write(struct file *file, const void *buf, size_t count, loff_t *pos)
Al Viro06ae43f2013-03-20 13:19:30 -0400488{
489 mm_segment_t old_fs;
490 const char __user *p;
491 ssize_t ret;
492
Al Viro7f7f25e2014-02-11 17:49:24 -0500493 if (!(file->f_mode & FMODE_CAN_WRITE))
Al Viro3e84f482013-03-27 15:20:30 +0000494 return -EINVAL;
495
Al Viro06ae43f2013-03-20 13:19:30 -0400496 old_fs = get_fs();
497 set_fs(get_ds());
498 p = (__force const char __user *)buf;
499 if (count > MAX_RW_COUNT)
500 count = MAX_RW_COUNT;
Al Viro493c84c2015-04-03 15:06:43 -0400501 ret = __vfs_write(file, p, count, pos);
Al Viro06ae43f2013-03-20 13:19:30 -0400502 set_fs(old_fs);
503 if (ret > 0) {
504 fsnotify_modify(file);
505 add_wchar(current, ret);
506 }
507 inc_syscw(current);
508 return ret;
509}
Al Viro2ec3a122014-08-19 11:48:09 -0400510EXPORT_SYMBOL(__kernel_write);
511
Christoph Hellwige13ec932017-09-01 17:39:14 +0200512ssize_t kernel_write(struct file *file, const void *buf, size_t count,
513 loff_t *pos)
Christoph Hellwigac452ac2017-09-01 17:39:11 +0200514{
515 mm_segment_t old_fs;
516 ssize_t res;
517
518 old_fs = get_fs();
519 set_fs(get_ds());
520 /* The cast to a user pointer is valid due to the set_fs() */
Christoph Hellwige13ec932017-09-01 17:39:14 +0200521 res = vfs_write(file, (__force const char __user *)buf, count, pos);
Christoph Hellwigac452ac2017-09-01 17:39:11 +0200522 set_fs(old_fs);
523
524 return res;
525}
526EXPORT_SYMBOL(kernel_write);
527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
529{
530 ssize_t ret;
531
532 if (!(file->f_mode & FMODE_WRITE))
533 return -EBADF;
Al Viro7f7f25e2014-02-11 17:49:24 -0500534 if (!(file->f_mode & FMODE_CAN_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 return -EINVAL;
536 if (unlikely(!access_ok(VERIFY_READ, buf, count)))
537 return -EFAULT;
538
539 ret = rw_verify_area(WRITE, file, pos, count);
Al Virobc613842016-03-31 21:48:20 -0400540 if (!ret) {
541 if (count > MAX_RW_COUNT)
542 count = MAX_RW_COUNT;
Al Viro03d95eb2013-03-20 13:04:20 -0400543 file_start_write(file);
Al Viro493c84c2015-04-03 15:06:43 -0400544 ret = __vfs_write(file, buf, count, pos);
James Morrisc43e2592008-01-12 22:05:48 +1100545 if (ret > 0) {
Eric Paris2a12a9d2009-12-17 21:24:21 -0500546 fsnotify_modify(file);
James Morrisc43e2592008-01-12 22:05:48 +1100547 add_wchar(current, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
James Morrisc43e2592008-01-12 22:05:48 +1100549 inc_syscw(current);
Al Viro03d95eb2013-03-20 13:04:20 -0400550 file_end_write(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 }
552
553 return ret;
554}
555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556static inline loff_t file_pos_read(struct file *file)
557{
558 return file->f_pos;
559}
560
561static inline void file_pos_write(struct file *file, loff_t pos)
562{
563 file->f_pos = pos;
564}
565
Heiko Carstens3cdad422009-01-14 14:14:22 +0100566SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
Linus Torvalds9c225f22014-03-03 09:36:58 -0800568 struct fd f = fdget_pos(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Al Viro2903ff02012-08-28 12:52:22 -0400571 if (f.file) {
572 loff_t pos = file_pos_read(f.file);
573 ret = vfs_read(f.file, buf, count, &pos);
Al Viro5faf1532013-06-15 05:49:36 +0400574 if (ret >= 0)
575 file_pos_write(f.file, pos);
Linus Torvalds9c225f22014-03-03 09:36:58 -0800576 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 return ret;
579}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Heiko Carstens3cdad422009-01-14 14:14:22 +0100581SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf,
582 size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
Linus Torvalds9c225f22014-03-03 09:36:58 -0800584 struct fd f = fdget_pos(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Al Viro2903ff02012-08-28 12:52:22 -0400587 if (f.file) {
588 loff_t pos = file_pos_read(f.file);
589 ret = vfs_write(f.file, buf, count, &pos);
Al Viro5faf1532013-06-15 05:49:36 +0400590 if (ret >= 0)
591 file_pos_write(f.file, pos);
Linus Torvalds9c225f22014-03-03 09:36:58 -0800592 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 }
594
595 return ret;
596}
597
Al Viro4a0fd5b2013-01-21 15:16:58 -0500598SYSCALL_DEFINE4(pread64, unsigned int, fd, char __user *, buf,
599 size_t, count, loff_t, pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
Al Viro2903ff02012-08-28 12:52:22 -0400601 struct fd f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 if (pos < 0)
605 return -EINVAL;
606
Al Viro2903ff02012-08-28 12:52:22 -0400607 f = fdget(fd);
608 if (f.file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -0400610 if (f.file->f_mode & FMODE_PREAD)
611 ret = vfs_read(f.file, buf, count, &pos);
612 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 }
614
615 return ret;
616}
617
Al Viro4a0fd5b2013-01-21 15:16:58 -0500618SYSCALL_DEFINE4(pwrite64, unsigned int, fd, const char __user *, buf,
619 size_t, count, loff_t, pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
Al Viro2903ff02012-08-28 12:52:22 -0400621 struct fd f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624 if (pos < 0)
625 return -EINVAL;
626
Al Viro2903ff02012-08-28 12:52:22 -0400627 f = fdget(fd);
628 if (f.file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -0400630 if (f.file->f_mode & FMODE_PWRITE)
631 ret = vfs_write(f.file, buf, count, &pos);
632 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 }
634
635 return ret;
636}
637
Al Viroac15ac02015-03-20 20:10:21 -0400638static ssize_t do_iter_readv_writev(struct file *filp, struct iov_iter *iter,
Christoph Hellwigddef7ed2017-07-06 18:58:37 +0200639 loff_t *ppos, int type, rwf_t flags)
Al Viro293bc982014-02-11 18:37:41 -0500640{
641 struct kiocb kiocb;
Al Viro293bc982014-02-11 18:37:41 -0500642 ssize_t ret;
643
644 init_sync_kiocb(&kiocb, filp);
Goldwyn Rodriguesfdd2f5b2017-06-20 07:05:40 -0500645 ret = kiocb_set_rw_flags(&kiocb, flags);
646 if (ret)
647 return ret;
Al Viro293bc982014-02-11 18:37:41 -0500648 kiocb.ki_pos = *ppos;
Al Viro293bc982014-02-11 18:37:41 -0500649
Miklos Szeredi0f78d062017-02-20 16:51:23 +0100650 if (type == READ)
Miklos Szeredibb7462b2017-02-20 16:51:23 +0100651 ret = call_read_iter(filp, &kiocb, iter);
Miklos Szeredi0f78d062017-02-20 16:51:23 +0100652 else
Miklos Szeredibb7462b2017-02-20 16:51:23 +0100653 ret = call_write_iter(filp, &kiocb, iter);
Christoph Hellwig599bd192015-02-11 19:59:44 +0100654 BUG_ON(ret == -EIOCBQUEUED);
Al Viro293bc982014-02-11 18:37:41 -0500655 *ppos = kiocb.ki_pos;
656 return ret;
657}
658
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700659/* Do it by hand, with file-ops */
Al Viroac15ac02015-03-20 20:10:21 -0400660static ssize_t do_loop_readv_writev(struct file *filp, struct iov_iter *iter,
Christoph Hellwigddef7ed2017-07-06 18:58:37 +0200661 loff_t *ppos, int type, rwf_t flags)
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700662{
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700663 ssize_t ret = 0;
664
Christoph Hellwig97be7eb2016-03-03 16:04:01 +0100665 if (flags & ~RWF_HIPRI)
Christoph Hellwig793b80e2016-03-03 16:03:58 +0100666 return -EOPNOTSUPP;
667
Al Viroac15ac02015-03-20 20:10:21 -0400668 while (iov_iter_count(iter)) {
669 struct iovec iovec = iov_iter_iovec(iter);
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700670 ssize_t nr;
671
Miklos Szeredi0f78d062017-02-20 16:51:23 +0100672 if (type == READ) {
673 nr = filp->f_op->read(filp, iovec.iov_base,
674 iovec.iov_len, ppos);
675 } else {
676 nr = filp->f_op->write(filp, iovec.iov_base,
677 iovec.iov_len, ppos);
678 }
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700679
680 if (nr < 0) {
681 if (!ret)
682 ret = nr;
683 break;
684 }
685 ret += nr;
Al Viroac15ac02015-03-20 20:10:21 -0400686 if (nr != iovec.iov_len)
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700687 break;
Al Viroac15ac02015-03-20 20:10:21 -0400688 iov_iter_advance(iter, nr);
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700689 }
690
691 return ret;
692}
693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694/* A write operation does a read from user space and vice versa */
695#define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
696
Vegard Nossumffecee42016-10-08 11:18:07 +0200697/**
698 * rw_copy_check_uvector() - Copy an array of &struct iovec from userspace
699 * into the kernel and check that it is valid.
700 *
701 * @type: One of %CHECK_IOVEC_ONLY, %READ, or %WRITE.
702 * @uvector: Pointer to the userspace array.
703 * @nr_segs: Number of elements in userspace array.
704 * @fast_segs: Number of elements in @fast_pointer.
705 * @fast_pointer: Pointer to (usually small on-stack) kernel array.
706 * @ret_pointer: (output parameter) Pointer to a variable that will point to
707 * either @fast_pointer, a newly allocated kernel array, or NULL,
708 * depending on which array was used.
709 *
710 * This function copies an array of &struct iovec of @nr_segs from
711 * userspace into the kernel and checks that each element is valid (e.g.
712 * it does not point to a kernel address or cause overflow by being too
713 * large, etc.).
714 *
715 * As an optimization, the caller may provide a pointer to a small
716 * on-stack array in @fast_pointer, typically %UIO_FASTIOV elements long
717 * (the size of this array, or 0 if unused, should be given in @fast_segs).
718 *
719 * @ret_pointer will always point to the array that was used, so the
720 * caller must take care not to call kfree() on it e.g. in case the
721 * @fast_pointer array was used and it was allocated on the stack.
722 *
723 * Return: The total number of bytes covered by the iovec array on success
724 * or a negative error code on error.
725 */
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700726ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
727 unsigned long nr_segs, unsigned long fast_segs,
728 struct iovec *fast_pointer,
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700729 struct iovec **ret_pointer)
Linus Torvalds435f49a2010-10-29 10:36:49 -0700730{
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700731 unsigned long seg;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700732 ssize_t ret;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700733 struct iovec *iov = fast_pointer;
734
Linus Torvalds435f49a2010-10-29 10:36:49 -0700735 /*
736 * SuS says "The readv() function *may* fail if the iovcnt argument
737 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
738 * traditionally returned zero for zero segments, so...
739 */
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700740 if (nr_segs == 0) {
741 ret = 0;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700742 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700743 }
744
Linus Torvalds435f49a2010-10-29 10:36:49 -0700745 /*
746 * First get the "struct iovec" from user memory and
747 * verify all the pointers
748 */
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700749 if (nr_segs > UIO_MAXIOV) {
750 ret = -EINVAL;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700751 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700752 }
753 if (nr_segs > fast_segs) {
Linus Torvalds435f49a2010-10-29 10:36:49 -0700754 iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700755 if (iov == NULL) {
756 ret = -ENOMEM;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700757 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700758 }
Linus Torvalds435f49a2010-10-29 10:36:49 -0700759 }
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700760 if (copy_from_user(iov, uvector, nr_segs*sizeof(*uvector))) {
761 ret = -EFAULT;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700762 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700763 }
764
Linus Torvalds435f49a2010-10-29 10:36:49 -0700765 /*
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700766 * According to the Single Unix Specification we should return EINVAL
767 * if an element length is < 0 when cast to ssize_t or if the
768 * total length would overflow the ssize_t return value of the
769 * system call.
Linus Torvalds435f49a2010-10-29 10:36:49 -0700770 *
771 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
772 * overflow case.
773 */
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700774 ret = 0;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700775 for (seg = 0; seg < nr_segs; seg++) {
776 void __user *buf = iov[seg].iov_base;
777 ssize_t len = (ssize_t)iov[seg].iov_len;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700778
779 /* see if we we're about to use an invalid len or if
780 * it's about to overflow ssize_t */
Linus Torvalds435f49a2010-10-29 10:36:49 -0700781 if (len < 0) {
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700782 ret = -EINVAL;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700783 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700784 }
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700785 if (type >= 0
Christopher Yeohfcf63402011-10-31 17:06:39 -0700786 && unlikely(!access_ok(vrfy_dir(type), buf, len))) {
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700787 ret = -EFAULT;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700788 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700789 }
Linus Torvalds435f49a2010-10-29 10:36:49 -0700790 if (len > MAX_RW_COUNT - ret) {
791 len = MAX_RW_COUNT - ret;
792 iov[seg].iov_len = len;
793 }
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700794 ret += len;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700795 }
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700796out:
797 *ret_pointer = iov;
798 return ret;
799}
800
Al Virof5029852017-04-08 18:18:48 -0400801#ifdef CONFIG_COMPAT
802ssize_t compat_rw_copy_check_uvector(int type,
803 const struct compat_iovec __user *uvector, unsigned long nr_segs,
804 unsigned long fast_segs, struct iovec *fast_pointer,
805 struct iovec **ret_pointer)
806{
807 compat_ssize_t tot_len;
808 struct iovec *iov = *ret_pointer = fast_pointer;
809 ssize_t ret = 0;
810 int seg;
811
812 /*
813 * SuS says "The readv() function *may* fail if the iovcnt argument
814 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
815 * traditionally returned zero for zero segments, so...
816 */
817 if (nr_segs == 0)
818 goto out;
819
820 ret = -EINVAL;
821 if (nr_segs > UIO_MAXIOV)
822 goto out;
823 if (nr_segs > fast_segs) {
824 ret = -ENOMEM;
825 iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
826 if (iov == NULL)
827 goto out;
828 }
829 *ret_pointer = iov;
830
831 ret = -EFAULT;
832 if (!access_ok(VERIFY_READ, uvector, nr_segs*sizeof(*uvector)))
833 goto out;
834
835 /*
836 * Single unix specification:
837 * We should -EINVAL if an element length is not >= 0 and fitting an
838 * ssize_t.
839 *
840 * In Linux, the total length is limited to MAX_RW_COUNT, there is
841 * no overflow possibility.
842 */
843 tot_len = 0;
844 ret = -EINVAL;
845 for (seg = 0; seg < nr_segs; seg++) {
846 compat_uptr_t buf;
847 compat_ssize_t len;
848
849 if (__get_user(len, &uvector->iov_len) ||
850 __get_user(buf, &uvector->iov_base)) {
851 ret = -EFAULT;
852 goto out;
853 }
854 if (len < 0) /* size_t not fitting in compat_ssize_t .. */
855 goto out;
856 if (type >= 0 &&
857 !access_ok(vrfy_dir(type), compat_ptr(buf), len)) {
858 ret = -EFAULT;
859 goto out;
860 }
861 if (len > MAX_RW_COUNT - tot_len)
862 len = MAX_RW_COUNT - tot_len;
863 tot_len += len;
864 iov->iov_base = compat_ptr(buf);
865 iov->iov_len = (compat_size_t) len;
866 uvector++;
867 iov++;
868 }
869 ret = tot_len;
870
871out:
872 return ret;
873}
874#endif
875
Christoph Hellwig19c73582017-05-27 11:16:48 +0300876static ssize_t do_iter_read(struct file *file, struct iov_iter *iter,
Christoph Hellwigddef7ed2017-07-06 18:58:37 +0200877 loff_t *pos, rwf_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 size_t tot_len;
Miklos Szeredi7687a7a2017-02-20 16:51:23 +0100880 ssize_t ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
Christoph Hellwigedab5fe2017-05-27 11:16:49 +0300882 if (!(file->f_mode & FMODE_READ))
883 return -EBADF;
884 if (!(file->f_mode & FMODE_CAN_READ))
885 return -EINVAL;
886
Miklos Szeredi7687a7a2017-02-20 16:51:23 +0100887 tot_len = iov_iter_count(iter);
Al Viro0504c072015-03-21 19:40:11 -0400888 if (!tot_len)
889 goto out;
Christoph Hellwig19c73582017-05-27 11:16:48 +0300890 ret = rw_verify_area(READ, file, pos, tot_len);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800891 if (ret < 0)
Christoph Hellwig19c73582017-05-27 11:16:48 +0300892 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Christoph Hellwig19c73582017-05-27 11:16:48 +0300894 if (file->f_op->read_iter)
895 ret = do_iter_readv_writev(file, iter, pos, READ, flags);
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700896 else
Christoph Hellwig19c73582017-05-27 11:16:48 +0300897 ret = do_loop_readv_writev(file, iter, pos, READ, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898out:
Christoph Hellwig19c73582017-05-27 11:16:48 +0300899 if (ret >= 0)
900 fsnotify_access(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902}
903
Christoph Hellwig18e97102017-05-27 11:16:51 +0300904ssize_t vfs_iter_read(struct file *file, struct iov_iter *iter, loff_t *ppos,
Christoph Hellwigddef7ed2017-07-06 18:58:37 +0200905 rwf_t flags)
Christoph Hellwig18e97102017-05-27 11:16:51 +0300906{
907 if (!file->f_op->read_iter)
908 return -EINVAL;
909 return do_iter_read(file, iter, ppos, flags);
910}
911EXPORT_SYMBOL(vfs_iter_read);
912
Christoph Hellwig19c73582017-05-27 11:16:48 +0300913static ssize_t do_iter_write(struct file *file, struct iov_iter *iter,
Christoph Hellwigddef7ed2017-07-06 18:58:37 +0200914 loff_t *pos, rwf_t flags)
Christoph Hellwig19c73582017-05-27 11:16:48 +0300915{
916 size_t tot_len;
917 ssize_t ret = 0;
918
Christoph Hellwigedab5fe2017-05-27 11:16:49 +0300919 if (!(file->f_mode & FMODE_WRITE))
920 return -EBADF;
921 if (!(file->f_mode & FMODE_CAN_WRITE))
922 return -EINVAL;
923
Christoph Hellwig19c73582017-05-27 11:16:48 +0300924 tot_len = iov_iter_count(iter);
925 if (!tot_len)
926 return 0;
927 ret = rw_verify_area(WRITE, file, pos, tot_len);
928 if (ret < 0)
929 return ret;
930
Christoph Hellwig19c73582017-05-27 11:16:48 +0300931 if (file->f_op->write_iter)
932 ret = do_iter_readv_writev(file, iter, pos, WRITE, flags);
933 else
934 ret = do_loop_readv_writev(file, iter, pos, WRITE, flags);
Christoph Hellwig19c73582017-05-27 11:16:48 +0300935 if (ret > 0)
936 fsnotify_modify(file);
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700937 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938}
939
Christoph Hellwigabbb65892017-05-27 11:16:52 +0300940ssize_t vfs_iter_write(struct file *file, struct iov_iter *iter, loff_t *ppos,
Christoph Hellwigddef7ed2017-07-06 18:58:37 +0200941 rwf_t flags)
Christoph Hellwigabbb65892017-05-27 11:16:52 +0300942{
943 if (!file->f_op->write_iter)
944 return -EINVAL;
945 return do_iter_write(file, iter, ppos, flags);
946}
947EXPORT_SYMBOL(vfs_iter_write);
948
Christoph Hellwig251b42a2017-05-27 11:16:46 +0300949ssize_t vfs_readv(struct file *file, const struct iovec __user *vec,
Christoph Hellwigddef7ed2017-07-06 18:58:37 +0200950 unsigned long vlen, loff_t *pos, rwf_t flags)
Miklos Szeredi7687a7a2017-02-20 16:51:23 +0100951{
952 struct iovec iovstack[UIO_FASTIOV];
953 struct iovec *iov = iovstack;
954 struct iov_iter iter;
955 ssize_t ret;
956
Christoph Hellwig251b42a2017-05-27 11:16:46 +0300957 ret = import_iovec(READ, vec, vlen, ARRAY_SIZE(iovstack), &iov, &iter);
Christoph Hellwigedab5fe2017-05-27 11:16:49 +0300958 if (ret >= 0) {
959 ret = do_iter_read(file, &iter, pos, flags);
960 kfree(iov);
961 }
Miklos Szeredi7687a7a2017-02-20 16:51:23 +0100962
963 return ret;
964}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Christoph Hellwig9725d4c2017-09-01 17:39:25 +0200966static ssize_t vfs_writev(struct file *file, const struct iovec __user *vec,
Christoph Hellwigddef7ed2017-07-06 18:58:37 +0200967 unsigned long vlen, loff_t *pos, rwf_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
Christoph Hellwig251b42a2017-05-27 11:16:46 +0300969 struct iovec iovstack[UIO_FASTIOV];
970 struct iovec *iov = iovstack;
971 struct iov_iter iter;
972 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Christoph Hellwig251b42a2017-05-27 11:16:46 +0300974 ret = import_iovec(WRITE, vec, vlen, ARRAY_SIZE(iovstack), &iov, &iter);
Christoph Hellwigedab5fe2017-05-27 11:16:49 +0300975 if (ret >= 0) {
Al Viro62473a22017-07-06 09:15:47 -0400976 file_start_write(file);
Christoph Hellwigedab5fe2017-05-27 11:16:49 +0300977 ret = do_iter_write(file, &iter, pos, flags);
Al Viro62473a22017-07-06 09:15:47 -0400978 file_end_write(file);
Christoph Hellwigedab5fe2017-05-27 11:16:49 +0300979 kfree(iov);
980 }
Christoph Hellwig251b42a2017-05-27 11:16:46 +0300981 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Milosz Tanskif17d8b32016-03-03 16:03:59 +0100984static ssize_t do_readv(unsigned long fd, const struct iovec __user *vec,
Christoph Hellwigddef7ed2017-07-06 18:58:37 +0200985 unsigned long vlen, rwf_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986{
Linus Torvalds9c225f22014-03-03 09:36:58 -0800987 struct fd f = fdget_pos(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Al Viro2903ff02012-08-28 12:52:22 -0400990 if (f.file) {
991 loff_t pos = file_pos_read(f.file);
Milosz Tanskif17d8b32016-03-03 16:03:59 +0100992 ret = vfs_readv(f.file, vec, vlen, &pos, flags);
Al Viro5faf1532013-06-15 05:49:36 +0400993 if (ret >= 0)
994 file_pos_write(f.file, pos);
Linus Torvalds9c225f22014-03-03 09:36:58 -0800995 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 }
997
998 if (ret > 0)
Alexey Dobriyan4b98d112007-02-10 01:46:45 -0800999 add_rchar(current, ret);
1000 inc_syscr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 return ret;
1002}
1003
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001004static ssize_t do_writev(unsigned long fd, const struct iovec __user *vec,
Christoph Hellwigddef7ed2017-07-06 18:58:37 +02001005 unsigned long vlen, rwf_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006{
Linus Torvalds9c225f22014-03-03 09:36:58 -08001007 struct fd f = fdget_pos(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
Al Viro2903ff02012-08-28 12:52:22 -04001010 if (f.file) {
1011 loff_t pos = file_pos_read(f.file);
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001012 ret = vfs_writev(f.file, vec, vlen, &pos, flags);
Al Viro5faf1532013-06-15 05:49:36 +04001013 if (ret >= 0)
1014 file_pos_write(f.file, pos);
Linus Torvalds9c225f22014-03-03 09:36:58 -08001015 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 }
1017
1018 if (ret > 0)
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08001019 add_wchar(current, ret);
1020 inc_syscw(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 return ret;
1022}
1023
Linus Torvalds601cc112009-04-03 08:03:22 -07001024static inline loff_t pos_from_hilo(unsigned long high, unsigned long low)
Gerd Hoffmannf3554f42009-04-02 16:59:23 -07001025{
Linus Torvalds601cc112009-04-03 08:03:22 -07001026#define HALF_LONG_BITS (BITS_PER_LONG / 2)
1027 return (((loff_t)high << HALF_LONG_BITS) << HALF_LONG_BITS) | low;
1028}
1029
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001030static ssize_t do_preadv(unsigned long fd, const struct iovec __user *vec,
Christoph Hellwigddef7ed2017-07-06 18:58:37 +02001031 unsigned long vlen, loff_t pos, rwf_t flags)
Linus Torvalds601cc112009-04-03 08:03:22 -07001032{
Al Viro2903ff02012-08-28 12:52:22 -04001033 struct fd f;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -07001034 ssize_t ret = -EBADF;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -07001035
1036 if (pos < 0)
1037 return -EINVAL;
1038
Al Viro2903ff02012-08-28 12:52:22 -04001039 f = fdget(fd);
1040 if (f.file) {
Gerd Hoffmannf3554f42009-04-02 16:59:23 -07001041 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -04001042 if (f.file->f_mode & FMODE_PREAD)
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001043 ret = vfs_readv(f.file, vec, vlen, &pos, flags);
Al Viro2903ff02012-08-28 12:52:22 -04001044 fdput(f);
Gerd Hoffmannf3554f42009-04-02 16:59:23 -07001045 }
1046
1047 if (ret > 0)
1048 add_rchar(current, ret);
1049 inc_syscr(current);
1050 return ret;
1051}
1052
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001053static ssize_t do_pwritev(unsigned long fd, const struct iovec __user *vec,
Christoph Hellwigddef7ed2017-07-06 18:58:37 +02001054 unsigned long vlen, loff_t pos, rwf_t flags)
Gerd Hoffmannf3554f42009-04-02 16:59:23 -07001055{
Al Viro2903ff02012-08-28 12:52:22 -04001056 struct fd f;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -07001057 ssize_t ret = -EBADF;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -07001058
1059 if (pos < 0)
1060 return -EINVAL;
1061
Al Viro2903ff02012-08-28 12:52:22 -04001062 f = fdget(fd);
1063 if (f.file) {
Gerd Hoffmannf3554f42009-04-02 16:59:23 -07001064 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -04001065 if (f.file->f_mode & FMODE_PWRITE)
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001066 ret = vfs_writev(f.file, vec, vlen, &pos, flags);
Al Viro2903ff02012-08-28 12:52:22 -04001067 fdput(f);
Gerd Hoffmannf3554f42009-04-02 16:59:23 -07001068 }
1069
1070 if (ret > 0)
1071 add_wchar(current, ret);
1072 inc_syscw(current);
1073 return ret;
1074}
1075
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001076SYSCALL_DEFINE3(readv, unsigned long, fd, const struct iovec __user *, vec,
1077 unsigned long, vlen)
1078{
1079 return do_readv(fd, vec, vlen, 0);
1080}
1081
1082SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec __user *, vec,
1083 unsigned long, vlen)
1084{
1085 return do_writev(fd, vec, vlen, 0);
1086}
1087
1088SYSCALL_DEFINE5(preadv, unsigned long, fd, const struct iovec __user *, vec,
1089 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
1090{
1091 loff_t pos = pos_from_hilo(pos_h, pos_l);
1092
1093 return do_preadv(fd, vec, vlen, pos, 0);
1094}
1095
1096SYSCALL_DEFINE6(preadv2, unsigned long, fd, const struct iovec __user *, vec,
1097 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h,
Christoph Hellwigddef7ed2017-07-06 18:58:37 +02001098 rwf_t, flags)
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001099{
1100 loff_t pos = pos_from_hilo(pos_h, pos_l);
1101
1102 if (pos == -1)
1103 return do_readv(fd, vec, vlen, flags);
1104
1105 return do_preadv(fd, vec, vlen, pos, flags);
1106}
1107
1108SYSCALL_DEFINE5(pwritev, unsigned long, fd, const struct iovec __user *, vec,
1109 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
1110{
1111 loff_t pos = pos_from_hilo(pos_h, pos_l);
1112
1113 return do_pwritev(fd, vec, vlen, pos, 0);
1114}
1115
1116SYSCALL_DEFINE6(pwritev2, unsigned long, fd, const struct iovec __user *, vec,
1117 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h,
Christoph Hellwigddef7ed2017-07-06 18:58:37 +02001118 rwf_t, flags)
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001119{
1120 loff_t pos = pos_from_hilo(pos_h, pos_l);
1121
1122 if (pos == -1)
1123 return do_writev(fd, vec, vlen, flags);
1124
1125 return do_pwritev(fd, vec, vlen, pos, flags);
1126}
1127
Al Viro72ec3512013-03-20 10:42:10 -04001128#ifdef CONFIG_COMPAT
Al Viro72ec3512013-03-20 10:42:10 -04001129static size_t compat_readv(struct file *file,
1130 const struct compat_iovec __user *vec,
Christoph Hellwigddef7ed2017-07-06 18:58:37 +02001131 unsigned long vlen, loff_t *pos, rwf_t flags)
Al Viro72ec3512013-03-20 10:42:10 -04001132{
1133 struct iovec iovstack[UIO_FASTIOV];
1134 struct iovec *iov = iovstack;
1135 struct iov_iter iter;
1136 ssize_t ret;
1137
Christoph Hellwig26c87fb2017-05-27 11:16:47 +03001138 ret = compat_import_iovec(READ, vec, vlen, UIO_FASTIOV, &iov, &iter);
Christoph Hellwigedab5fe2017-05-27 11:16:49 +03001139 if (ret >= 0) {
1140 ret = do_iter_read(file, &iter, pos, flags);
1141 kfree(iov);
1142 }
Al Viro72ec3512013-03-20 10:42:10 -04001143 if (ret > 0)
1144 add_rchar(current, ret);
1145 inc_syscr(current);
1146 return ret;
1147}
1148
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001149static size_t do_compat_readv(compat_ulong_t fd,
1150 const struct compat_iovec __user *vec,
Christoph Hellwigddef7ed2017-07-06 18:58:37 +02001151 compat_ulong_t vlen, rwf_t flags)
Al Viro72ec3512013-03-20 10:42:10 -04001152{
Linus Torvalds9c225f22014-03-03 09:36:58 -08001153 struct fd f = fdget_pos(fd);
Al Viro72ec3512013-03-20 10:42:10 -04001154 ssize_t ret;
1155 loff_t pos;
1156
1157 if (!f.file)
1158 return -EBADF;
1159 pos = f.file->f_pos;
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001160 ret = compat_readv(f.file, vec, vlen, &pos, flags);
Al Viro5faf1532013-06-15 05:49:36 +04001161 if (ret >= 0)
1162 f.file->f_pos = pos;
Linus Torvalds9c225f22014-03-03 09:36:58 -08001163 fdput_pos(f);
Al Viro72ec3512013-03-20 10:42:10 -04001164 return ret;
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001165
Al Viro72ec3512013-03-20 10:42:10 -04001166}
1167
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001168COMPAT_SYSCALL_DEFINE3(readv, compat_ulong_t, fd,
1169 const struct compat_iovec __user *,vec,
1170 compat_ulong_t, vlen)
1171{
1172 return do_compat_readv(fd, vec, vlen, 0);
1173}
1174
1175static long do_compat_preadv64(unsigned long fd,
Heiko Carstens378a10f2014-03-05 10:43:51 +01001176 const struct compat_iovec __user *vec,
Christoph Hellwigddef7ed2017-07-06 18:58:37 +02001177 unsigned long vlen, loff_t pos, rwf_t flags)
Al Viro72ec3512013-03-20 10:42:10 -04001178{
1179 struct fd f;
1180 ssize_t ret;
1181
1182 if (pos < 0)
1183 return -EINVAL;
1184 f = fdget(fd);
1185 if (!f.file)
1186 return -EBADF;
1187 ret = -ESPIPE;
1188 if (f.file->f_mode & FMODE_PREAD)
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001189 ret = compat_readv(f.file, vec, vlen, &pos, flags);
Al Viro72ec3512013-03-20 10:42:10 -04001190 fdput(f);
1191 return ret;
1192}
1193
Heiko Carstens378a10f2014-03-05 10:43:51 +01001194#ifdef __ARCH_WANT_COMPAT_SYS_PREADV64
1195COMPAT_SYSCALL_DEFINE4(preadv64, unsigned long, fd,
1196 const struct compat_iovec __user *,vec,
1197 unsigned long, vlen, loff_t, pos)
1198{
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001199 return do_compat_preadv64(fd, vec, vlen, pos, 0);
Heiko Carstens378a10f2014-03-05 10:43:51 +01001200}
1201#endif
1202
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001203COMPAT_SYSCALL_DEFINE5(preadv, compat_ulong_t, fd,
Al Viro72ec3512013-03-20 10:42:10 -04001204 const struct compat_iovec __user *,vec,
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001205 compat_ulong_t, vlen, u32, pos_low, u32, pos_high)
Al Viro72ec3512013-03-20 10:42:10 -04001206{
1207 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
Heiko Carstens378a10f2014-03-05 10:43:51 +01001208
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001209 return do_compat_preadv64(fd, vec, vlen, pos, 0);
1210}
1211
H.J. Lu3ebfd812016-07-14 12:31:53 -07001212#ifdef __ARCH_WANT_COMPAT_SYS_PREADV64V2
1213COMPAT_SYSCALL_DEFINE5(preadv64v2, unsigned long, fd,
1214 const struct compat_iovec __user *,vec,
Christoph Hellwigddef7ed2017-07-06 18:58:37 +02001215 unsigned long, vlen, loff_t, pos, rwf_t, flags)
H.J. Lu3ebfd812016-07-14 12:31:53 -07001216{
1217 return do_compat_preadv64(fd, vec, vlen, pos, flags);
1218}
1219#endif
1220
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001221COMPAT_SYSCALL_DEFINE6(preadv2, compat_ulong_t, fd,
1222 const struct compat_iovec __user *,vec,
1223 compat_ulong_t, vlen, u32, pos_low, u32, pos_high,
Christoph Hellwigddef7ed2017-07-06 18:58:37 +02001224 rwf_t, flags)
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001225{
1226 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
1227
1228 if (pos == -1)
1229 return do_compat_readv(fd, vec, vlen, flags);
1230
1231 return do_compat_preadv64(fd, vec, vlen, pos, flags);
Al Viro72ec3512013-03-20 10:42:10 -04001232}
1233
1234static size_t compat_writev(struct file *file,
1235 const struct compat_iovec __user *vec,
Christoph Hellwigddef7ed2017-07-06 18:58:37 +02001236 unsigned long vlen, loff_t *pos, rwf_t flags)
Al Viro72ec3512013-03-20 10:42:10 -04001237{
Christoph Hellwig26c87fb2017-05-27 11:16:47 +03001238 struct iovec iovstack[UIO_FASTIOV];
1239 struct iovec *iov = iovstack;
1240 struct iov_iter iter;
Christoph Hellwigedab5fe2017-05-27 11:16:49 +03001241 ssize_t ret;
Al Viro72ec3512013-03-20 10:42:10 -04001242
Christoph Hellwig26c87fb2017-05-27 11:16:47 +03001243 ret = compat_import_iovec(WRITE, vec, vlen, UIO_FASTIOV, &iov, &iter);
Christoph Hellwigedab5fe2017-05-27 11:16:49 +03001244 if (ret >= 0) {
Al Viro62473a22017-07-06 09:15:47 -04001245 file_start_write(file);
Christoph Hellwigedab5fe2017-05-27 11:16:49 +03001246 ret = do_iter_write(file, &iter, pos, flags);
Al Viro62473a22017-07-06 09:15:47 -04001247 file_end_write(file);
Christoph Hellwigedab5fe2017-05-27 11:16:49 +03001248 kfree(iov);
1249 }
Al Viro72ec3512013-03-20 10:42:10 -04001250 if (ret > 0)
1251 add_wchar(current, ret);
1252 inc_syscw(current);
1253 return ret;
1254}
1255
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001256static size_t do_compat_writev(compat_ulong_t fd,
1257 const struct compat_iovec __user* vec,
Christoph Hellwigddef7ed2017-07-06 18:58:37 +02001258 compat_ulong_t vlen, rwf_t flags)
Al Viro72ec3512013-03-20 10:42:10 -04001259{
Linus Torvalds9c225f22014-03-03 09:36:58 -08001260 struct fd f = fdget_pos(fd);
Al Viro72ec3512013-03-20 10:42:10 -04001261 ssize_t ret;
1262 loff_t pos;
1263
1264 if (!f.file)
1265 return -EBADF;
1266 pos = f.file->f_pos;
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001267 ret = compat_writev(f.file, vec, vlen, &pos, flags);
Al Viro5faf1532013-06-15 05:49:36 +04001268 if (ret >= 0)
1269 f.file->f_pos = pos;
Linus Torvalds9c225f22014-03-03 09:36:58 -08001270 fdput_pos(f);
Al Viro72ec3512013-03-20 10:42:10 -04001271 return ret;
1272}
1273
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001274COMPAT_SYSCALL_DEFINE3(writev, compat_ulong_t, fd,
1275 const struct compat_iovec __user *, vec,
1276 compat_ulong_t, vlen)
1277{
1278 return do_compat_writev(fd, vec, vlen, 0);
1279}
1280
1281static long do_compat_pwritev64(unsigned long fd,
Heiko Carstens378a10f2014-03-05 10:43:51 +01001282 const struct compat_iovec __user *vec,
Christoph Hellwigddef7ed2017-07-06 18:58:37 +02001283 unsigned long vlen, loff_t pos, rwf_t flags)
Al Viro72ec3512013-03-20 10:42:10 -04001284{
1285 struct fd f;
1286 ssize_t ret;
1287
1288 if (pos < 0)
1289 return -EINVAL;
1290 f = fdget(fd);
1291 if (!f.file)
1292 return -EBADF;
1293 ret = -ESPIPE;
1294 if (f.file->f_mode & FMODE_PWRITE)
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001295 ret = compat_writev(f.file, vec, vlen, &pos, flags);
Al Viro72ec3512013-03-20 10:42:10 -04001296 fdput(f);
1297 return ret;
1298}
1299
Heiko Carstens378a10f2014-03-05 10:43:51 +01001300#ifdef __ARCH_WANT_COMPAT_SYS_PWRITEV64
1301COMPAT_SYSCALL_DEFINE4(pwritev64, unsigned long, fd,
1302 const struct compat_iovec __user *,vec,
1303 unsigned long, vlen, loff_t, pos)
1304{
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001305 return do_compat_pwritev64(fd, vec, vlen, pos, 0);
Heiko Carstens378a10f2014-03-05 10:43:51 +01001306}
1307#endif
1308
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001309COMPAT_SYSCALL_DEFINE5(pwritev, compat_ulong_t, fd,
Al Viro72ec3512013-03-20 10:42:10 -04001310 const struct compat_iovec __user *,vec,
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001311 compat_ulong_t, vlen, u32, pos_low, u32, pos_high)
Al Viro72ec3512013-03-20 10:42:10 -04001312{
1313 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
Heiko Carstens378a10f2014-03-05 10:43:51 +01001314
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001315 return do_compat_pwritev64(fd, vec, vlen, pos, 0);
Al Viro72ec3512013-03-20 10:42:10 -04001316}
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001317
H.J. Lu3ebfd812016-07-14 12:31:53 -07001318#ifdef __ARCH_WANT_COMPAT_SYS_PWRITEV64V2
1319COMPAT_SYSCALL_DEFINE5(pwritev64v2, unsigned long, fd,
1320 const struct compat_iovec __user *,vec,
Christoph Hellwigddef7ed2017-07-06 18:58:37 +02001321 unsigned long, vlen, loff_t, pos, rwf_t, flags)
H.J. Lu3ebfd812016-07-14 12:31:53 -07001322{
1323 return do_compat_pwritev64(fd, vec, vlen, pos, flags);
1324}
1325#endif
1326
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001327COMPAT_SYSCALL_DEFINE6(pwritev2, compat_ulong_t, fd,
1328 const struct compat_iovec __user *,vec,
Christoph Hellwigddef7ed2017-07-06 18:58:37 +02001329 compat_ulong_t, vlen, u32, pos_low, u32, pos_high, rwf_t, flags)
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001330{
1331 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
1332
1333 if (pos == -1)
1334 return do_compat_writev(fd, vec, vlen, flags);
1335
1336 return do_compat_pwritev64(fd, vec, vlen, pos, flags);
1337}
1338
Al Viro72ec3512013-03-20 10:42:10 -04001339#endif
1340
Al Viro19f4fc32013-02-24 02:17:03 -05001341static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,
1342 size_t count, loff_t max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343{
Al Viro2903ff02012-08-28 12:52:22 -04001344 struct fd in, out;
1345 struct inode *in_inode, *out_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 loff_t pos;
Al Viro7995bd22013-06-20 18:58:36 +04001347 loff_t out_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 ssize_t retval;
Al Viro2903ff02012-08-28 12:52:22 -04001349 int fl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
1351 /*
1352 * Get input file, and verify that it is ok..
1353 */
1354 retval = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -04001355 in = fdget(in_fd);
1356 if (!in.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 goto out;
Al Viro2903ff02012-08-28 12:52:22 -04001358 if (!(in.file->f_mode & FMODE_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 goto fput_in;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 retval = -ESPIPE;
Al Viro7995bd22013-06-20 18:58:36 +04001361 if (!ppos) {
1362 pos = in.file->f_pos;
1363 } else {
1364 pos = *ppos;
Al Viro2903ff02012-08-28 12:52:22 -04001365 if (!(in.file->f_mode & FMODE_PREAD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 goto fput_in;
Al Viro7995bd22013-06-20 18:58:36 +04001367 }
1368 retval = rw_verify_area(READ, in.file, &pos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -08001369 if (retval < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 goto fput_in;
Al Virobc613842016-03-31 21:48:20 -04001371 if (count > MAX_RW_COUNT)
1372 count = MAX_RW_COUNT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 /*
1375 * Get output file, and verify that it is ok..
1376 */
1377 retval = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -04001378 out = fdget(out_fd);
1379 if (!out.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 goto fput_in;
Al Viro2903ff02012-08-28 12:52:22 -04001381 if (!(out.file->f_mode & FMODE_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 goto fput_out;
1383 retval = -EINVAL;
Al Viro496ad9a2013-01-23 17:07:38 -05001384 in_inode = file_inode(in.file);
1385 out_inode = file_inode(out.file);
Al Viro7995bd22013-06-20 18:58:36 +04001386 out_pos = out.file->f_pos;
1387 retval = rw_verify_area(WRITE, out.file, &out_pos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -08001388 if (retval < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 goto fput_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 if (!max)
1392 max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes);
1393
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 if (unlikely(pos + count > max)) {
1395 retval = -EOVERFLOW;
1396 if (pos >= max)
1397 goto fput_out;
1398 count = max - pos;
1399 }
1400
Jens Axboed96e6e72007-06-11 12:18:52 +02001401 fl = 0;
Jens Axboe534f2aa2007-06-01 14:52:37 +02001402#if 0
Jens Axboed96e6e72007-06-11 12:18:52 +02001403 /*
1404 * We need to debate whether we can enable this or not. The
1405 * man page documents EAGAIN return for the output at least,
1406 * and the application is arguably buggy if it doesn't expect
1407 * EAGAIN on a non-blocking file descriptor.
1408 */
Al Viro2903ff02012-08-28 12:52:22 -04001409 if (in.file->f_flags & O_NONBLOCK)
Jens Axboed96e6e72007-06-11 12:18:52 +02001410 fl = SPLICE_F_NONBLOCK;
Jens Axboe534f2aa2007-06-01 14:52:37 +02001411#endif
Al Viro50cd2c52013-05-23 20:10:34 -04001412 file_start_write(out.file);
Al Viro7995bd22013-06-20 18:58:36 +04001413 retval = do_splice_direct(in.file, &pos, out.file, &out_pos, count, fl);
Al Viro50cd2c52013-05-23 20:10:34 -04001414 file_end_write(out.file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
1416 if (retval > 0) {
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08001417 add_rchar(current, retval);
1418 add_wchar(current, retval);
Scott Wolchoka68c2f12012-12-20 15:05:52 -08001419 fsnotify_access(in.file);
1420 fsnotify_modify(out.file);
Al Viro7995bd22013-06-20 18:58:36 +04001421 out.file->f_pos = out_pos;
1422 if (ppos)
1423 *ppos = pos;
1424 else
1425 in.file->f_pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08001428 inc_syscr(current);
1429 inc_syscw(current);
Al Viro7995bd22013-06-20 18:58:36 +04001430 if (pos > max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 retval = -EOVERFLOW;
1432
1433fput_out:
Al Viro2903ff02012-08-28 12:52:22 -04001434 fdput(out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435fput_in:
Al Viro2903ff02012-08-28 12:52:22 -04001436 fdput(in);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437out:
1438 return retval;
1439}
1440
Heiko Carstens002c8972009-01-14 14:14:18 +01001441SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd, off_t __user *, offset, size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442{
1443 loff_t pos;
1444 off_t off;
1445 ssize_t ret;
1446
1447 if (offset) {
1448 if (unlikely(get_user(off, offset)))
1449 return -EFAULT;
1450 pos = off;
1451 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
1452 if (unlikely(put_user(pos, offset)))
1453 return -EFAULT;
1454 return ret;
1455 }
1456
1457 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1458}
1459
Heiko Carstens002c8972009-01-14 14:14:18 +01001460SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd, loff_t __user *, offset, size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461{
1462 loff_t pos;
1463 ssize_t ret;
1464
1465 if (offset) {
1466 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
1467 return -EFAULT;
1468 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
1469 if (unlikely(put_user(pos, offset)))
1470 return -EFAULT;
1471 return ret;
1472 }
1473
1474 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1475}
Al Viro19f4fc32013-02-24 02:17:03 -05001476
1477#ifdef CONFIG_COMPAT
1478COMPAT_SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd,
1479 compat_off_t __user *, offset, compat_size_t, count)
1480{
1481 loff_t pos;
1482 off_t off;
1483 ssize_t ret;
1484
1485 if (offset) {
1486 if (unlikely(get_user(off, offset)))
1487 return -EFAULT;
1488 pos = off;
1489 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
1490 if (unlikely(put_user(pos, offset)))
1491 return -EFAULT;
1492 return ret;
1493 }
1494
1495 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1496}
1497
1498COMPAT_SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd,
1499 compat_loff_t __user *, offset, compat_size_t, count)
1500{
1501 loff_t pos;
1502 ssize_t ret;
1503
1504 if (offset) {
1505 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
1506 return -EFAULT;
1507 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
1508 if (unlikely(put_user(pos, offset)))
1509 return -EFAULT;
1510 return ret;
1511 }
1512
1513 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1514}
1515#endif
Zach Brown29732932015-11-10 16:53:30 -05001516
1517/*
1518 * copy_file_range() differs from regular file read and write in that it
1519 * specifically allows return partial success. When it does so is up to
1520 * the copy_file_range method.
1521 */
1522ssize_t vfs_copy_file_range(struct file *file_in, loff_t pos_in,
1523 struct file *file_out, loff_t pos_out,
1524 size_t len, unsigned int flags)
1525{
1526 struct inode *inode_in = file_inode(file_in);
1527 struct inode *inode_out = file_inode(file_out);
1528 ssize_t ret;
1529
1530 if (flags != 0)
1531 return -EINVAL;
1532
Amir Goldstein11cbfb12017-01-31 10:34:56 +02001533 if (S_ISDIR(inode_in->i_mode) || S_ISDIR(inode_out->i_mode))
1534 return -EISDIR;
1535 if (!S_ISREG(inode_in->i_mode) || !S_ISREG(inode_out->i_mode))
1536 return -EINVAL;
1537
Zach Brown29732932015-11-10 16:53:30 -05001538 ret = rw_verify_area(READ, file_in, &pos_in, len);
Al Virobc613842016-03-31 21:48:20 -04001539 if (unlikely(ret))
1540 return ret;
1541
1542 ret = rw_verify_area(WRITE, file_out, &pos_out, len);
1543 if (unlikely(ret))
Zach Brown29732932015-11-10 16:53:30 -05001544 return ret;
1545
1546 if (!(file_in->f_mode & FMODE_READ) ||
1547 !(file_out->f_mode & FMODE_WRITE) ||
Anna Schumakereac70052015-11-10 16:53:33 -05001548 (file_out->f_flags & O_APPEND))
Zach Brown29732932015-11-10 16:53:30 -05001549 return -EBADF;
1550
1551 /* this could be relaxed once a method supports cross-fs copies */
1552 if (inode_in->i_sb != inode_out->i_sb)
1553 return -EXDEV;
1554
1555 if (len == 0)
1556 return 0;
1557
Amir Goldsteinbfe219d2017-01-31 10:34:57 +02001558 file_start_write(file_out);
Zach Brown29732932015-11-10 16:53:30 -05001559
Christoph Hellwiga76b5b02016-12-09 16:17:19 -08001560 /*
1561 * Try cloning first, this is supported by more file systems, and
1562 * more efficient if both clone and copy are supported (e.g. NFS).
1563 */
1564 if (file_in->f_op->clone_file_range) {
1565 ret = file_in->f_op->clone_file_range(file_in, pos_in,
1566 file_out, pos_out, len);
1567 if (ret == 0) {
1568 ret = len;
1569 goto done;
1570 }
1571 }
1572
1573 if (file_out->f_op->copy_file_range) {
Anna Schumakereac70052015-11-10 16:53:33 -05001574 ret = file_out->f_op->copy_file_range(file_in, pos_in, file_out,
1575 pos_out, len, flags);
Christoph Hellwiga76b5b02016-12-09 16:17:19 -08001576 if (ret != -EOPNOTSUPP)
1577 goto done;
1578 }
Anna Schumakereac70052015-11-10 16:53:33 -05001579
Christoph Hellwiga76b5b02016-12-09 16:17:19 -08001580 ret = do_splice_direct(file_in, &pos_in, file_out, &pos_out,
1581 len > MAX_RW_COUNT ? MAX_RW_COUNT : len, 0);
1582
1583done:
Zach Brown29732932015-11-10 16:53:30 -05001584 if (ret > 0) {
1585 fsnotify_access(file_in);
1586 add_rchar(current, ret);
1587 fsnotify_modify(file_out);
1588 add_wchar(current, ret);
1589 }
Christoph Hellwiga76b5b02016-12-09 16:17:19 -08001590
Zach Brown29732932015-11-10 16:53:30 -05001591 inc_syscr(current);
1592 inc_syscw(current);
1593
Amir Goldsteinbfe219d2017-01-31 10:34:57 +02001594 file_end_write(file_out);
Zach Brown29732932015-11-10 16:53:30 -05001595
1596 return ret;
1597}
1598EXPORT_SYMBOL(vfs_copy_file_range);
1599
1600SYSCALL_DEFINE6(copy_file_range, int, fd_in, loff_t __user *, off_in,
1601 int, fd_out, loff_t __user *, off_out,
1602 size_t, len, unsigned int, flags)
1603{
1604 loff_t pos_in;
1605 loff_t pos_out;
1606 struct fd f_in;
1607 struct fd f_out;
1608 ssize_t ret = -EBADF;
1609
1610 f_in = fdget(fd_in);
1611 if (!f_in.file)
1612 goto out2;
1613
1614 f_out = fdget(fd_out);
1615 if (!f_out.file)
1616 goto out1;
1617
1618 ret = -EFAULT;
1619 if (off_in) {
1620 if (copy_from_user(&pos_in, off_in, sizeof(loff_t)))
1621 goto out;
1622 } else {
1623 pos_in = f_in.file->f_pos;
1624 }
1625
1626 if (off_out) {
1627 if (copy_from_user(&pos_out, off_out, sizeof(loff_t)))
1628 goto out;
1629 } else {
1630 pos_out = f_out.file->f_pos;
1631 }
1632
1633 ret = vfs_copy_file_range(f_in.file, pos_in, f_out.file, pos_out, len,
1634 flags);
1635 if (ret > 0) {
1636 pos_in += ret;
1637 pos_out += ret;
1638
1639 if (off_in) {
1640 if (copy_to_user(off_in, &pos_in, sizeof(loff_t)))
1641 ret = -EFAULT;
1642 } else {
1643 f_in.file->f_pos = pos_in;
1644 }
1645
1646 if (off_out) {
1647 if (copy_to_user(off_out, &pos_out, sizeof(loff_t)))
1648 ret = -EFAULT;
1649 } else {
1650 f_out.file->f_pos = pos_out;
1651 }
1652 }
1653
1654out:
1655 fdput(f_out);
1656out1:
1657 fdput(f_in);
1658out2:
1659 return ret;
1660}
Christoph Hellwig04b38d62015-12-03 12:59:50 +01001661
1662static int clone_verify_area(struct file *file, loff_t pos, u64 len, bool write)
1663{
1664 struct inode *inode = file_inode(file);
1665
1666 if (unlikely(pos < 0))
1667 return -EINVAL;
1668
1669 if (unlikely((loff_t) (pos + len) < 0))
1670 return -EINVAL;
1671
1672 if (unlikely(inode->i_flctx && mandatory_lock(inode))) {
1673 loff_t end = len ? pos + len - 1 : OFFSET_MAX;
1674 int retval;
1675
1676 retval = locks_mandatory_area(inode, file, pos, end,
1677 write ? F_WRLCK : F_RDLCK);
1678 if (retval < 0)
1679 return retval;
1680 }
1681
1682 return security_file_permission(file, write ? MAY_WRITE : MAY_READ);
1683}
1684
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001685/*
1686 * Check that the two inodes are eligible for cloning, the ranges make
1687 * sense, and then flush all dirty data. Caller must ensure that the
1688 * inodes have been locked against any other modifications.
Darrick J. Wong22725ce2016-12-19 15:13:26 -08001689 *
1690 * Returns: 0 for "nothing to clone", 1 for "something to clone", or
1691 * the usual negative error code.
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001692 */
1693int vfs_clone_file_prep_inodes(struct inode *inode_in, loff_t pos_in,
1694 struct inode *inode_out, loff_t pos_out,
1695 u64 *len, bool is_dedupe)
1696{
1697 loff_t bs = inode_out->i_sb->s_blocksize;
1698 loff_t blen;
1699 loff_t isize;
1700 bool same_inode = (inode_in == inode_out);
1701 int ret;
1702
1703 /* Don't touch certain kinds of inodes */
1704 if (IS_IMMUTABLE(inode_out))
1705 return -EPERM;
1706
1707 if (IS_SWAPFILE(inode_in) || IS_SWAPFILE(inode_out))
1708 return -ETXTBSY;
1709
1710 /* Don't reflink dirs, pipes, sockets... */
1711 if (S_ISDIR(inode_in->i_mode) || S_ISDIR(inode_out->i_mode))
1712 return -EISDIR;
1713 if (!S_ISREG(inode_in->i_mode) || !S_ISREG(inode_out->i_mode))
1714 return -EINVAL;
1715
1716 /* Are we going all the way to the end? */
1717 isize = i_size_read(inode_in);
Darrick J. Wong22725ce2016-12-19 15:13:26 -08001718 if (isize == 0)
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001719 return 0;
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001720
1721 /* Zero length dedupe exits immediately; reflink goes to EOF. */
1722 if (*len == 0) {
Darrick J. Wong22725ce2016-12-19 15:13:26 -08001723 if (is_dedupe || pos_in == isize)
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001724 return 0;
Darrick J. Wong22725ce2016-12-19 15:13:26 -08001725 if (pos_in > isize)
1726 return -EINVAL;
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001727 *len = isize - pos_in;
1728 }
1729
1730 /* Ensure offsets don't wrap and the input is inside i_size */
1731 if (pos_in + *len < pos_in || pos_out + *len < pos_out ||
1732 pos_in + *len > isize)
1733 return -EINVAL;
1734
1735 /* Don't allow dedupe past EOF in the dest file */
1736 if (is_dedupe) {
1737 loff_t disize;
1738
1739 disize = i_size_read(inode_out);
1740 if (pos_out >= disize || pos_out + *len > disize)
1741 return -EINVAL;
1742 }
1743
1744 /* If we're linking to EOF, continue to the block boundary. */
1745 if (pos_in + *len == isize)
1746 blen = ALIGN(isize, bs) - pos_in;
1747 else
1748 blen = *len;
1749
1750 /* Only reflink if we're aligned to block boundaries */
1751 if (!IS_ALIGNED(pos_in, bs) || !IS_ALIGNED(pos_in + blen, bs) ||
1752 !IS_ALIGNED(pos_out, bs) || !IS_ALIGNED(pos_out + blen, bs))
1753 return -EINVAL;
1754
1755 /* Don't allow overlapped reflink within the same file */
1756 if (same_inode) {
1757 if (pos_out + blen > pos_in && pos_out < pos_in + blen)
1758 return -EINVAL;
1759 }
1760
1761 /* Wait for the completion of any pending IOs on both files */
1762 inode_dio_wait(inode_in);
1763 if (!same_inode)
1764 inode_dio_wait(inode_out);
1765
1766 ret = filemap_write_and_wait_range(inode_in->i_mapping,
1767 pos_in, pos_in + *len - 1);
1768 if (ret)
1769 return ret;
1770
1771 ret = filemap_write_and_wait_range(inode_out->i_mapping,
1772 pos_out, pos_out + *len - 1);
1773 if (ret)
1774 return ret;
1775
1776 /*
1777 * Check that the extents are the same.
1778 */
1779 if (is_dedupe) {
1780 bool is_same = false;
1781
1782 ret = vfs_dedupe_file_range_compare(inode_in, pos_in,
1783 inode_out, pos_out, *len, &is_same);
1784 if (ret)
1785 return ret;
1786 if (!is_same)
1787 return -EBADE;
1788 }
1789
Darrick J. Wong22725ce2016-12-19 15:13:26 -08001790 return 1;
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001791}
1792EXPORT_SYMBOL(vfs_clone_file_prep_inodes);
1793
Christoph Hellwig04b38d62015-12-03 12:59:50 +01001794int vfs_clone_file_range(struct file *file_in, loff_t pos_in,
1795 struct file *file_out, loff_t pos_out, u64 len)
1796{
1797 struct inode *inode_in = file_inode(file_in);
1798 struct inode *inode_out = file_inode(file_out);
1799 int ret;
1800
Amir Goldsteinb335e9d2016-10-26 22:34:01 +03001801 if (S_ISDIR(inode_in->i_mode) || S_ISDIR(inode_out->i_mode))
1802 return -EISDIR;
1803 if (!S_ISREG(inode_in->i_mode) || !S_ISREG(inode_out->i_mode))
1804 return -EINVAL;
1805
Amir Goldstein913b86e2016-09-23 11:38:10 +03001806 /*
1807 * FICLONE/FICLONERANGE ioctls enforce that src and dest files are on
1808 * the same mount. Practically, they only need to be on the same file
1809 * system.
1810 */
1811 if (inode_in->i_sb != inode_out->i_sb)
Christoph Hellwig04b38d62015-12-03 12:59:50 +01001812 return -EXDEV;
1813
Christoph Hellwig04b38d62015-12-03 12:59:50 +01001814 if (!(file_in->f_mode & FMODE_READ) ||
1815 !(file_out->f_mode & FMODE_WRITE) ||
Christoph Hellwig0fcbf992016-02-26 18:53:12 +01001816 (file_out->f_flags & O_APPEND))
Christoph Hellwig04b38d62015-12-03 12:59:50 +01001817 return -EBADF;
1818
Christoph Hellwig0fcbf992016-02-26 18:53:12 +01001819 if (!file_in->f_op->clone_file_range)
1820 return -EOPNOTSUPP;
1821
Christoph Hellwig04b38d62015-12-03 12:59:50 +01001822 ret = clone_verify_area(file_in, pos_in, len, false);
1823 if (ret)
1824 return ret;
1825
1826 ret = clone_verify_area(file_out, pos_out, len, true);
1827 if (ret)
1828 return ret;
1829
1830 if (pos_in + len > i_size_read(inode_in))
1831 return -EINVAL;
1832
Christoph Hellwig04b38d62015-12-03 12:59:50 +01001833 ret = file_in->f_op->clone_file_range(file_in, pos_in,
1834 file_out, pos_out, len);
1835 if (!ret) {
1836 fsnotify_access(file_in);
1837 fsnotify_modify(file_out);
1838 }
1839
Christoph Hellwig04b38d62015-12-03 12:59:50 +01001840 return ret;
1841}
1842EXPORT_SYMBOL(vfs_clone_file_range);
Darrick J. Wong54dbc152015-12-19 00:55:59 -08001843
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001844/*
1845 * Read a page's worth of file data into the page cache. Return the page
1846 * locked.
1847 */
1848static struct page *vfs_dedupe_get_page(struct inode *inode, loff_t offset)
1849{
1850 struct address_space *mapping;
1851 struct page *page;
1852 pgoff_t n;
1853
1854 n = offset >> PAGE_SHIFT;
1855 mapping = inode->i_mapping;
1856 page = read_mapping_page(mapping, n, NULL);
1857 if (IS_ERR(page))
1858 return page;
1859 if (!PageUptodate(page)) {
1860 put_page(page);
1861 return ERR_PTR(-EIO);
1862 }
1863 lock_page(page);
1864 return page;
1865}
1866
1867/*
1868 * Compare extents of two files to see if they are the same.
1869 * Caller must have locked both inodes to prevent write races.
1870 */
1871int vfs_dedupe_file_range_compare(struct inode *src, loff_t srcoff,
1872 struct inode *dest, loff_t destoff,
1873 loff_t len, bool *is_same)
1874{
1875 loff_t src_poff;
1876 loff_t dest_poff;
1877 void *src_addr;
1878 void *dest_addr;
1879 struct page *src_page;
1880 struct page *dest_page;
1881 loff_t cmp_len;
1882 bool same;
1883 int error;
1884
1885 error = -EINVAL;
1886 same = true;
1887 while (len) {
1888 src_poff = srcoff & (PAGE_SIZE - 1);
1889 dest_poff = destoff & (PAGE_SIZE - 1);
1890 cmp_len = min(PAGE_SIZE - src_poff,
1891 PAGE_SIZE - dest_poff);
1892 cmp_len = min(cmp_len, len);
1893 if (cmp_len <= 0)
1894 goto out_error;
1895
1896 src_page = vfs_dedupe_get_page(src, srcoff);
1897 if (IS_ERR(src_page)) {
1898 error = PTR_ERR(src_page);
1899 goto out_error;
1900 }
1901 dest_page = vfs_dedupe_get_page(dest, destoff);
1902 if (IS_ERR(dest_page)) {
1903 error = PTR_ERR(dest_page);
1904 unlock_page(src_page);
1905 put_page(src_page);
1906 goto out_error;
1907 }
1908 src_addr = kmap_atomic(src_page);
1909 dest_addr = kmap_atomic(dest_page);
1910
1911 flush_dcache_page(src_page);
1912 flush_dcache_page(dest_page);
1913
1914 if (memcmp(src_addr + src_poff, dest_addr + dest_poff, cmp_len))
1915 same = false;
1916
1917 kunmap_atomic(dest_addr);
1918 kunmap_atomic(src_addr);
1919 unlock_page(dest_page);
1920 unlock_page(src_page);
1921 put_page(dest_page);
1922 put_page(src_page);
1923
1924 if (!same)
1925 break;
1926
1927 srcoff += cmp_len;
1928 destoff += cmp_len;
1929 len -= cmp_len;
1930 }
1931
1932 *is_same = same;
1933 return 0;
1934
1935out_error:
1936 return error;
1937}
1938EXPORT_SYMBOL(vfs_dedupe_file_range_compare);
1939
Darrick J. Wong54dbc152015-12-19 00:55:59 -08001940int vfs_dedupe_file_range(struct file *file, struct file_dedupe_range *same)
1941{
1942 struct file_dedupe_range_info *info;
1943 struct inode *src = file_inode(file);
1944 u64 off;
1945 u64 len;
1946 int i;
1947 int ret;
1948 bool is_admin = capable(CAP_SYS_ADMIN);
1949 u16 count = same->dest_count;
1950 struct file *dst_file;
1951 loff_t dst_off;
1952 ssize_t deduped;
1953
1954 if (!(file->f_mode & FMODE_READ))
1955 return -EINVAL;
1956
1957 if (same->reserved1 || same->reserved2)
1958 return -EINVAL;
1959
1960 off = same->src_offset;
1961 len = same->src_length;
1962
1963 ret = -EISDIR;
1964 if (S_ISDIR(src->i_mode))
1965 goto out;
1966
1967 ret = -EINVAL;
1968 if (!S_ISREG(src->i_mode))
1969 goto out;
1970
1971 ret = clone_verify_area(file, off, len, false);
1972 if (ret < 0)
1973 goto out;
1974 ret = 0;
1975
Darrick J. Wong22725ce2016-12-19 15:13:26 -08001976 if (off + len > i_size_read(src))
1977 return -EINVAL;
1978
Darrick J. Wong54dbc152015-12-19 00:55:59 -08001979 /* pre-format output fields to sane values */
1980 for (i = 0; i < count; i++) {
1981 same->info[i].bytes_deduped = 0ULL;
1982 same->info[i].status = FILE_DEDUPE_RANGE_SAME;
1983 }
1984
1985 for (i = 0, info = same->info; i < count; i++, info++) {
1986 struct inode *dst;
1987 struct fd dst_fd = fdget(info->dest_fd);
1988
1989 dst_file = dst_fd.file;
1990 if (!dst_file) {
1991 info->status = -EBADF;
1992 goto next_loop;
1993 }
1994 dst = file_inode(dst_file);
1995
1996 ret = mnt_want_write_file(dst_file);
1997 if (ret) {
1998 info->status = ret;
1999 goto next_loop;
2000 }
2001
2002 dst_off = info->dest_offset;
2003 ret = clone_verify_area(dst_file, dst_off, len, true);
2004 if (ret < 0) {
2005 info->status = ret;
2006 goto next_file;
2007 }
2008 ret = 0;
2009
2010 if (info->reserved) {
2011 info->status = -EINVAL;
2012 } else if (!(is_admin || (dst_file->f_mode & FMODE_WRITE))) {
2013 info->status = -EINVAL;
2014 } else if (file->f_path.mnt != dst_file->f_path.mnt) {
2015 info->status = -EXDEV;
2016 } else if (S_ISDIR(dst->i_mode)) {
2017 info->status = -EISDIR;
2018 } else if (dst_file->f_op->dedupe_file_range == NULL) {
2019 info->status = -EINVAL;
2020 } else {
2021 deduped = dst_file->f_op->dedupe_file_range(file, off,
2022 len, dst_file,
2023 info->dest_offset);
2024 if (deduped == -EBADE)
2025 info->status = FILE_DEDUPE_RANGE_DIFFERS;
2026 else if (deduped < 0)
2027 info->status = deduped;
2028 else
2029 info->bytes_deduped += deduped;
2030 }
2031
2032next_file:
2033 mnt_drop_write_file(dst_file);
2034next_loop:
2035 fdput(dst_fd);
Darrick J. Wonge62e5602016-01-22 16:58:28 -08002036
2037 if (fatal_signal_pending(current))
2038 goto out;
Darrick J. Wong54dbc152015-12-19 00:55:59 -08002039 }
2040
2041out:
2042 return ret;
2043}
2044EXPORT_SYMBOL(vfs_dedupe_file_range);