blob: 702c4301d9eb6b32a82ecc4cfa189482018915a1 [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 Hellwigddef7ed22017-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
Christoph Hellwigbef17322020-06-06 14:49:58 +0200304static off_t ksys_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
Dominik Brodowski76847e42018-03-13 21:51:17 +0100322SYSCALL_DEFINE3(lseek, unsigned int, fd, off_t, offset, unsigned int, whence)
323{
324 return ksys_lseek(fd, offset, whence);
325}
326
Al Viro561c6732013-02-24 10:52:26 -0500327#ifdef CONFIG_COMPAT
328COMPAT_SYSCALL_DEFINE3(lseek, unsigned int, fd, compat_off_t, offset, unsigned int, whence)
329{
Dominik Brodowski76847e42018-03-13 21:51:17 +0100330 return ksys_lseek(fd, offset, whence);
Al Viro561c6732013-02-24 10:52:26 -0500331}
332#endif
333
Michal Suchanek9e62cce2020-03-20 11:20:12 +0100334#if !defined(CONFIG_64BIT) || defined(CONFIG_COMPAT) || \
335 defined(__ARCH_WANT_SYS_LLSEEK)
Heiko Carstens003d7ab2009-01-14 14:14:21 +0100336SYSCALL_DEFINE5(llseek, unsigned int, fd, unsigned long, offset_high,
337 unsigned long, offset_low, loff_t __user *, result,
Andrew Morton965c8e52012-12-17 15:59:39 -0800338 unsigned int, whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
340 int retval;
Eric Biggersd7a15f82014-03-16 14:24:08 -0500341 struct fd f = fdget_pos(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 loff_t offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Al Viro2903ff02012-08-28 12:52:22 -0400344 if (!f.file)
345 return -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347 retval = -EINVAL;
Andrew Morton965c8e52012-12-17 15:59:39 -0800348 if (whence > SEEK_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 goto out_putf;
350
Al Viro2903ff02012-08-28 12:52:22 -0400351 offset = vfs_llseek(f.file, ((loff_t) offset_high << 32) | offset_low,
Andrew Morton965c8e52012-12-17 15:59:39 -0800352 whence);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 retval = (int)offset;
355 if (offset >= 0) {
356 retval = -EFAULT;
357 if (!copy_to_user(result, &offset, sizeof(offset)))
358 retval = 0;
359 }
360out_putf:
Eric Biggersd7a15f82014-03-16 14:24:08 -0500361 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 return retval;
363}
364#endif
365
Al Viro68d70d02013-06-19 15:26:04 +0400366int rw_verify_area(int read_write, struct file *file, const loff_t *ppos, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
368 struct inode *inode;
James Morrisc43e2592008-01-12 22:05:48 +1100369 int retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Al Viro496ad9a2013-01-23 17:07:38 -0500371 inode = file_inode(file);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800372 if (unlikely((ssize_t) count < 0))
James Morrisc43e2592008-01-12 22:05:48 +1100373 return retval;
Kirill Smelkov438ab722019-04-12 12:31:57 +0300374
375 /*
376 * ranged mandatory locking does not apply to streams - it makes sense
377 * only for files where position has a meaning.
378 */
379 if (ppos) {
380 loff_t pos = *ppos;
381
382 if (unlikely(pos < 0)) {
383 if (!unsigned_offsets(file))
384 return retval;
385 if (count >= -pos) /* both values are in 0..LLONG_MAX */
386 return -EOVERFLOW;
387 } else if (unlikely((loff_t) (pos + count) < 0)) {
388 if (!unsigned_offsets(file))
389 return retval;
390 }
391
392 if (unlikely(inode->i_flctx && mandatory_lock(inode))) {
393 retval = locks_mandatory_area(inode, file, pos, pos + count - 1,
394 read_write == READ ? F_RDLCK : F_WRLCK);
395 if (retval < 0)
396 return retval;
397 }
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -0700398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Al Virobc613842016-03-31 21:48:20 -0400400 return security_file_permission(file,
James Morrisc43e2592008-01-12 22:05:48 +1100401 read_write == READ ? MAY_READ : MAY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402}
403
Al Viro5d5d5682015-04-03 15:41:18 -0400404static ssize_t new_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
Al Viro293bc982014-02-11 18:37:41 -0500405{
406 struct iovec iov = { .iov_base = buf, .iov_len = len };
407 struct kiocb kiocb;
408 struct iov_iter iter;
409 ssize_t ret;
410
411 init_sync_kiocb(&kiocb, filp);
Kirill Smelkov438ab722019-04-12 12:31:57 +0300412 kiocb.ki_pos = (ppos ? *ppos : 0);
Al Viro293bc982014-02-11 18:37:41 -0500413 iov_iter_init(&iter, READ, &iov, 1, len);
414
Miklos Szeredibb7462b2017-02-20 16:51:23 +0100415 ret = call_read_iter(filp, &kiocb, &iter);
Christoph Hellwig599bd192015-02-11 19:59:44 +0100416 BUG_ON(ret == -EIOCBQUEUED);
Kirill Smelkov438ab722019-04-12 12:31:57 +0300417 if (ppos)
418 *ppos = kiocb.ki_pos;
Al Viro293bc982014-02-11 18:37:41 -0500419 return ret;
420}
421
Christoph Hellwig4d03e3c2020-09-03 16:22:33 +0200422static int warn_unsupported(struct file *file, const char *op)
423{
424 pr_warn_ratelimited(
425 "kernel %s not supported for file %pD4 (pid: %d comm: %.20s)\n",
426 op, file, current->pid, current->comm);
427 return -EINVAL;
428}
429
Christoph Hellwig61a707c2020-05-08 08:54:16 +0200430ssize_t __kernel_read(struct file *file, void *buf, size_t count, loff_t *pos)
431{
Christoph Hellwig4d03e3c2020-09-03 16:22:33 +0200432 struct kvec iov = {
433 .iov_base = buf,
434 .iov_len = min_t(size_t, count, MAX_RW_COUNT),
435 };
436 struct kiocb kiocb;
437 struct iov_iter iter;
Christoph Hellwig61a707c2020-05-08 08:54:16 +0200438 ssize_t ret;
439
440 if (WARN_ON_ONCE(!(file->f_mode & FMODE_READ)))
441 return -EINVAL;
442 if (!(file->f_mode & FMODE_CAN_READ))
443 return -EINVAL;
Christoph Hellwig4d03e3c2020-09-03 16:22:33 +0200444 /*
445 * Also fail if ->read_iter and ->read are both wired up as that
446 * implies very convoluted semantics.
447 */
448 if (unlikely(!file->f_op->read_iter || file->f_op->read))
449 return warn_unsupported(file, "read");
Christoph Hellwig61a707c2020-05-08 08:54:16 +0200450
Christoph Hellwig4d03e3c2020-09-03 16:22:33 +0200451 init_sync_kiocb(&kiocb, file);
452 kiocb.ki_pos = *pos;
453 iov_iter_kvec(&iter, READ, &iov, 1, iov.iov_len);
454 ret = file->f_op->read_iter(&kiocb, &iter);
Christoph Hellwig61a707c2020-05-08 08:54:16 +0200455 if (ret > 0) {
Christoph Hellwig4d03e3c2020-09-03 16:22:33 +0200456 *pos = kiocb.ki_pos;
Christoph Hellwig61a707c2020-05-08 08:54:16 +0200457 fsnotify_access(file);
458 add_rchar(current, ret);
459 }
460 inc_syscr(current);
461 return ret;
462}
463
Christoph Hellwigbdd1d2d2017-09-01 17:39:13 +0200464ssize_t kernel_read(struct file *file, void *buf, size_t count, loff_t *pos)
Christoph Hellwigc41fbad2017-09-01 17:39:12 +0200465{
Christoph Hellwig6209dd92020-05-08 09:00:28 +0200466 ssize_t ret;
Christoph Hellwigc41fbad2017-09-01 17:39:12 +0200467
Christoph Hellwig6209dd92020-05-08 09:00:28 +0200468 ret = rw_verify_area(READ, file, pos, count);
469 if (ret)
470 return ret;
471 return __kernel_read(file, buf, count, pos);
Christoph Hellwigc41fbad2017-09-01 17:39:12 +0200472}
473EXPORT_SYMBOL(kernel_read);
Dmitry Kasatkin6fb50322014-11-05 17:01:17 +0200474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
476{
477 ssize_t ret;
478
479 if (!(file->f_mode & FMODE_READ))
480 return -EBADF;
Al Viro7f7f25e2014-02-11 17:49:24 -0500481 if (!(file->f_mode & FMODE_CAN_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 return -EINVAL;
Linus Torvalds96d4f262019-01-03 18:57:57 -0800483 if (unlikely(!access_ok(buf, count)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 return -EFAULT;
485
486 ret = rw_verify_area(READ, file, pos, count);
Christoph Hellwig775802c2020-05-08 11:17:46 +0200487 if (ret)
488 return ret;
489 if (count > MAX_RW_COUNT)
490 count = MAX_RW_COUNT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Christoph Hellwig775802c2020-05-08 11:17:46 +0200492 if (file->f_op->read)
493 ret = file->f_op->read(file, buf, count, pos);
494 else if (file->f_op->read_iter)
495 ret = new_sync_read(file, buf, count, pos);
496 else
497 ret = -EINVAL;
498 if (ret > 0) {
499 fsnotify_access(file);
500 add_rchar(current, ret);
501 }
502 inc_syscr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 return ret;
504}
505
Al Viro5d5d5682015-04-03 15:41:18 -0400506static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
Al Viro293bc982014-02-11 18:37:41 -0500507{
508 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len };
509 struct kiocb kiocb;
510 struct iov_iter iter;
511 ssize_t ret;
512
513 init_sync_kiocb(&kiocb, filp);
Kirill Smelkov438ab722019-04-12 12:31:57 +0300514 kiocb.ki_pos = (ppos ? *ppos : 0);
Al Viro293bc982014-02-11 18:37:41 -0500515 iov_iter_init(&iter, WRITE, &iov, 1, len);
516
Miklos Szeredibb7462b2017-02-20 16:51:23 +0100517 ret = call_write_iter(filp, &kiocb, &iter);
Christoph Hellwig599bd192015-02-11 19:59:44 +0100518 BUG_ON(ret == -EIOCBQUEUED);
Kirill Smelkov438ab722019-04-12 12:31:57 +0300519 if (ret > 0 && ppos)
Al Virof765b132015-04-06 20:50:38 -0400520 *ppos = kiocb.ki_pos;
Al Viro293bc982014-02-11 18:37:41 -0500521 return ret;
522}
523
Christoph Hellwig81238b22020-05-07 19:33:03 +0200524/* caller is responsible for file_start_write/file_end_write */
Christoph Hellwig73e18f72017-09-01 17:39:15 +0200525ssize_t __kernel_write(struct file *file, const void *buf, size_t count, loff_t *pos)
Al Viro06ae43f2013-03-20 13:19:30 -0400526{
Christoph Hellwig4d03e3c2020-09-03 16:22:33 +0200527 struct kvec iov = {
528 .iov_base = (void *)buf,
529 .iov_len = min_t(size_t, count, MAX_RW_COUNT),
530 };
531 struct kiocb kiocb;
532 struct iov_iter iter;
Al Viro06ae43f2013-03-20 13:19:30 -0400533 ssize_t ret;
534
Christoph Hellwiga01ac272020-05-08 08:55:03 +0200535 if (WARN_ON_ONCE(!(file->f_mode & FMODE_WRITE)))
536 return -EBADF;
Al Viro7f7f25e2014-02-11 17:49:24 -0500537 if (!(file->f_mode & FMODE_CAN_WRITE))
Al Viro3e84f482013-03-27 15:20:30 +0000538 return -EINVAL;
Christoph Hellwig4d03e3c2020-09-03 16:22:33 +0200539 /*
540 * Also fail if ->write_iter and ->write are both wired up as that
541 * implies very convoluted semantics.
542 */
543 if (unlikely(!file->f_op->write_iter || file->f_op->write))
544 return warn_unsupported(file, "write");
Al Viro3e84f482013-03-27 15:20:30 +0000545
Christoph Hellwig4d03e3c2020-09-03 16:22:33 +0200546 init_sync_kiocb(&kiocb, file);
547 kiocb.ki_pos = *pos;
548 iov_iter_kvec(&iter, WRITE, &iov, 1, iov.iov_len);
549 ret = file->f_op->write_iter(&kiocb, &iter);
Al Viro06ae43f2013-03-20 13:19:30 -0400550 if (ret > 0) {
Christoph Hellwig4d03e3c2020-09-03 16:22:33 +0200551 *pos = kiocb.ki_pos;
Al Viro06ae43f2013-03-20 13:19:30 -0400552 fsnotify_modify(file);
553 add_wchar(current, ret);
554 }
555 inc_syscw(current);
556 return ret;
557}
Al Viro2ec3a122014-08-19 11:48:09 -0400558
Christoph Hellwige13ec932017-09-01 17:39:14 +0200559ssize_t kernel_write(struct file *file, const void *buf, size_t count,
560 loff_t *pos)
Christoph Hellwigac452ac2017-09-01 17:39:11 +0200561{
Christoph Hellwig81238b22020-05-07 19:33:03 +0200562 ssize_t ret;
Christoph Hellwigac452ac2017-09-01 17:39:11 +0200563
Christoph Hellwig81238b22020-05-07 19:33:03 +0200564 ret = rw_verify_area(WRITE, file, pos, count);
565 if (ret)
566 return ret;
Christoph Hellwigac452ac2017-09-01 17:39:11 +0200567
Christoph Hellwig81238b22020-05-07 19:33:03 +0200568 file_start_write(file);
569 ret = __kernel_write(file, buf, count, pos);
570 file_end_write(file);
571 return ret;
Christoph Hellwigac452ac2017-09-01 17:39:11 +0200572}
573EXPORT_SYMBOL(kernel_write);
574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
576{
577 ssize_t ret;
578
579 if (!(file->f_mode & FMODE_WRITE))
580 return -EBADF;
Al Viro7f7f25e2014-02-11 17:49:24 -0500581 if (!(file->f_mode & FMODE_CAN_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 return -EINVAL;
Linus Torvalds96d4f262019-01-03 18:57:57 -0800583 if (unlikely(!access_ok(buf, count)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 return -EFAULT;
585
586 ret = rw_verify_area(WRITE, file, pos, count);
Christoph Hellwig53ad8622020-05-13 08:51:46 +0200587 if (ret)
588 return ret;
589 if (count > MAX_RW_COUNT)
590 count = MAX_RW_COUNT;
591 file_start_write(file);
592 if (file->f_op->write)
593 ret = file->f_op->write(file, buf, count, pos);
594 else if (file->f_op->write_iter)
595 ret = new_sync_write(file, buf, count, pos);
596 else
597 ret = -EINVAL;
598 if (ret > 0) {
599 fsnotify_modify(file);
600 add_wchar(current, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 }
Christoph Hellwig53ad8622020-05-13 08:51:46 +0200602 inc_syscw(current);
603 file_end_write(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 return ret;
605}
606
Kirill Smelkov438ab722019-04-12 12:31:57 +0300607/* file_ppos returns &file->f_pos or NULL if file is stream */
608static inline loff_t *file_ppos(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
Kirill Smelkov438ab722019-04-12 12:31:57 +0300610 return file->f_mode & FMODE_STREAM ? NULL : &file->f_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611}
612
Dominik Brodowski3ce4a7b2018-03-13 21:56:26 +0100613ssize_t ksys_read(unsigned int fd, char __user *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
Linus Torvalds9c225f22014-03-03 09:36:58 -0800615 struct fd f = fdget_pos(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Al Viro2903ff02012-08-28 12:52:22 -0400618 if (f.file) {
Kirill Smelkov438ab722019-04-12 12:31:57 +0300619 loff_t pos, *ppos = file_ppos(f.file);
620 if (ppos) {
621 pos = *ppos;
622 ppos = &pos;
623 }
624 ret = vfs_read(f.file, buf, count, ppos);
625 if (ret >= 0 && ppos)
626 f.file->f_pos = pos;
Linus Torvalds9c225f22014-03-03 09:36:58 -0800627 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 return ret;
630}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Dominik Brodowski3ce4a7b2018-03-13 21:56:26 +0100632SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count)
633{
634 return ksys_read(fd, buf, count);
635}
636
Dominik Brodowskie7a3e8b2018-03-11 11:34:41 +0100637ssize_t ksys_write(unsigned int fd, const char __user *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
Linus Torvalds9c225f22014-03-03 09:36:58 -0800639 struct fd f = fdget_pos(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Al Viro2903ff02012-08-28 12:52:22 -0400642 if (f.file) {
Kirill Smelkov438ab722019-04-12 12:31:57 +0300643 loff_t pos, *ppos = file_ppos(f.file);
644 if (ppos) {
645 pos = *ppos;
646 ppos = &pos;
647 }
648 ret = vfs_write(f.file, buf, count, ppos);
649 if (ret >= 0 && ppos)
650 f.file->f_pos = pos;
Linus Torvalds9c225f22014-03-03 09:36:58 -0800651 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 }
653
654 return ret;
655}
656
Dominik Brodowskie7a3e8b2018-03-11 11:34:41 +0100657SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf,
658 size_t, count)
659{
660 return ksys_write(fd, buf, count);
661}
662
Dominik Brodowski36028d52018-03-19 17:38:31 +0100663ssize_t ksys_pread64(unsigned int fd, char __user *buf, size_t count,
664 loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
Al Viro2903ff02012-08-28 12:52:22 -0400666 struct fd f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669 if (pos < 0)
670 return -EINVAL;
671
Al Viro2903ff02012-08-28 12:52:22 -0400672 f = fdget(fd);
673 if (f.file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -0400675 if (f.file->f_mode & FMODE_PREAD)
676 ret = vfs_read(f.file, buf, count, &pos);
677 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 }
679
680 return ret;
681}
682
Dominik Brodowski36028d52018-03-19 17:38:31 +0100683SYSCALL_DEFINE4(pread64, unsigned int, fd, char __user *, buf,
684 size_t, count, loff_t, pos)
685{
686 return ksys_pread64(fd, buf, count, pos);
687}
688
689ssize_t ksys_pwrite64(unsigned int fd, const char __user *buf,
690 size_t count, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
Al Viro2903ff02012-08-28 12:52:22 -0400692 struct fd f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 if (pos < 0)
696 return -EINVAL;
697
Al Viro2903ff02012-08-28 12:52:22 -0400698 f = fdget(fd);
699 if (f.file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -0400701 if (f.file->f_mode & FMODE_PWRITE)
702 ret = vfs_write(f.file, buf, count, &pos);
703 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 }
705
706 return ret;
707}
708
Dominik Brodowski36028d52018-03-19 17:38:31 +0100709SYSCALL_DEFINE4(pwrite64, unsigned int, fd, const char __user *, buf,
710 size_t, count, loff_t, pos)
711{
712 return ksys_pwrite64(fd, buf, count, pos);
713}
714
Al Viroac15ac02015-03-20 20:10:21 -0400715static ssize_t do_iter_readv_writev(struct file *filp, struct iov_iter *iter,
Christoph Hellwigddef7ed22017-07-06 18:58:37 +0200716 loff_t *ppos, int type, rwf_t flags)
Al Viro293bc982014-02-11 18:37:41 -0500717{
718 struct kiocb kiocb;
Al Viro293bc982014-02-11 18:37:41 -0500719 ssize_t ret;
720
721 init_sync_kiocb(&kiocb, filp);
Goldwyn Rodriguesfdd2f5b2017-06-20 07:05:40 -0500722 ret = kiocb_set_rw_flags(&kiocb, flags);
723 if (ret)
724 return ret;
Kirill Smelkov438ab722019-04-12 12:31:57 +0300725 kiocb.ki_pos = (ppos ? *ppos : 0);
Al Viro293bc982014-02-11 18:37:41 -0500726
Miklos Szeredi0f78d062017-02-20 16:51:23 +0100727 if (type == READ)
Miklos Szeredibb7462b2017-02-20 16:51:23 +0100728 ret = call_read_iter(filp, &kiocb, iter);
Miklos Szeredi0f78d062017-02-20 16:51:23 +0100729 else
Miklos Szeredibb7462b2017-02-20 16:51:23 +0100730 ret = call_write_iter(filp, &kiocb, iter);
Christoph Hellwig599bd192015-02-11 19:59:44 +0100731 BUG_ON(ret == -EIOCBQUEUED);
Kirill Smelkov438ab722019-04-12 12:31:57 +0300732 if (ppos)
733 *ppos = kiocb.ki_pos;
Al Viro293bc982014-02-11 18:37:41 -0500734 return ret;
735}
736
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700737/* Do it by hand, with file-ops */
Al Viroac15ac02015-03-20 20:10:21 -0400738static ssize_t do_loop_readv_writev(struct file *filp, struct iov_iter *iter,
Christoph Hellwigddef7ed22017-07-06 18:58:37 +0200739 loff_t *ppos, int type, rwf_t flags)
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700740{
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700741 ssize_t ret = 0;
742
Christoph Hellwig97be7eb2016-03-03 16:04:01 +0100743 if (flags & ~RWF_HIPRI)
Christoph Hellwig793b80e2016-03-03 16:03:58 +0100744 return -EOPNOTSUPP;
745
Al Viroac15ac02015-03-20 20:10:21 -0400746 while (iov_iter_count(iter)) {
747 struct iovec iovec = iov_iter_iovec(iter);
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700748 ssize_t nr;
749
Miklos Szeredi0f78d062017-02-20 16:51:23 +0100750 if (type == READ) {
751 nr = filp->f_op->read(filp, iovec.iov_base,
752 iovec.iov_len, ppos);
753 } else {
754 nr = filp->f_op->write(filp, iovec.iov_base,
755 iovec.iov_len, ppos);
756 }
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700757
758 if (nr < 0) {
759 if (!ret)
760 ret = nr;
761 break;
762 }
763 ret += nr;
Al Viroac15ac02015-03-20 20:10:21 -0400764 if (nr != iovec.iov_len)
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700765 break;
Al Viroac15ac02015-03-20 20:10:21 -0400766 iov_iter_advance(iter, nr);
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700767 }
768
769 return ret;
770}
771
Vegard Nossumffecee42016-10-08 11:18:07 +0200772/**
773 * rw_copy_check_uvector() - Copy an array of &struct iovec from userspace
774 * into the kernel and check that it is valid.
775 *
776 * @type: One of %CHECK_IOVEC_ONLY, %READ, or %WRITE.
777 * @uvector: Pointer to the userspace array.
778 * @nr_segs: Number of elements in userspace array.
779 * @fast_segs: Number of elements in @fast_pointer.
780 * @fast_pointer: Pointer to (usually small on-stack) kernel array.
781 * @ret_pointer: (output parameter) Pointer to a variable that will point to
782 * either @fast_pointer, a newly allocated kernel array, or NULL,
783 * depending on which array was used.
784 *
785 * This function copies an array of &struct iovec of @nr_segs from
786 * userspace into the kernel and checks that each element is valid (e.g.
787 * it does not point to a kernel address or cause overflow by being too
788 * large, etc.).
789 *
790 * As an optimization, the caller may provide a pointer to a small
791 * on-stack array in @fast_pointer, typically %UIO_FASTIOV elements long
792 * (the size of this array, or 0 if unused, should be given in @fast_segs).
793 *
794 * @ret_pointer will always point to the array that was used, so the
795 * caller must take care not to call kfree() on it e.g. in case the
796 * @fast_pointer array was used and it was allocated on the stack.
797 *
798 * Return: The total number of bytes covered by the iovec array on success
799 * or a negative error code on error.
800 */
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700801ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
802 unsigned long nr_segs, unsigned long fast_segs,
803 struct iovec *fast_pointer,
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700804 struct iovec **ret_pointer)
Linus Torvalds435f49a2010-10-29 10:36:49 -0700805{
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700806 unsigned long seg;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700807 ssize_t ret;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700808 struct iovec *iov = fast_pointer;
809
Linus Torvalds435f49a2010-10-29 10:36:49 -0700810 /*
811 * SuS says "The readv() function *may* fail if the iovcnt argument
812 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
813 * traditionally returned zero for zero segments, so...
814 */
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700815 if (nr_segs == 0) {
816 ret = 0;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700817 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700818 }
819
Linus Torvalds435f49a2010-10-29 10:36:49 -0700820 /*
821 * First get the "struct iovec" from user memory and
822 * verify all the pointers
823 */
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700824 if (nr_segs > UIO_MAXIOV) {
825 ret = -EINVAL;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700826 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700827 }
828 if (nr_segs > fast_segs) {
Kees Cook6da2ec52018-06-12 13:55:00 -0700829 iov = kmalloc_array(nr_segs, sizeof(struct iovec), GFP_KERNEL);
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700830 if (iov == NULL) {
831 ret = -ENOMEM;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700832 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700833 }
Linus Torvalds435f49a2010-10-29 10:36:49 -0700834 }
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700835 if (copy_from_user(iov, uvector, nr_segs*sizeof(*uvector))) {
836 ret = -EFAULT;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700837 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700838 }
839
Linus Torvalds435f49a2010-10-29 10:36:49 -0700840 /*
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700841 * According to the Single Unix Specification we should return EINVAL
842 * if an element length is < 0 when cast to ssize_t or if the
843 * total length would overflow the ssize_t return value of the
844 * system call.
Linus Torvalds435f49a2010-10-29 10:36:49 -0700845 *
846 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
847 * overflow case.
848 */
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700849 ret = 0;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700850 for (seg = 0; seg < nr_segs; seg++) {
851 void __user *buf = iov[seg].iov_base;
852 ssize_t len = (ssize_t)iov[seg].iov_len;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700853
854 /* see if we we're about to use an invalid len or if
855 * it's about to overflow ssize_t */
Linus Torvalds435f49a2010-10-29 10:36:49 -0700856 if (len < 0) {
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700857 ret = -EINVAL;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700858 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700859 }
Christopher Yeohac34ebb2012-05-31 16:26:42 -0700860 if (type >= 0
Linus Torvalds96d4f262019-01-03 18:57:57 -0800861 && unlikely(!access_ok(buf, len))) {
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700862 ret = -EFAULT;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700863 goto out;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700864 }
Linus Torvalds435f49a2010-10-29 10:36:49 -0700865 if (len > MAX_RW_COUNT - ret) {
866 len = MAX_RW_COUNT - ret;
867 iov[seg].iov_len = len;
868 }
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700869 ret += len;
Linus Torvalds435f49a2010-10-29 10:36:49 -0700870 }
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700871out:
872 *ret_pointer = iov;
873 return ret;
874}
875
Al Virof5029852017-04-08 18:18:48 -0400876#ifdef CONFIG_COMPAT
877ssize_t compat_rw_copy_check_uvector(int type,
878 const struct compat_iovec __user *uvector, unsigned long nr_segs,
879 unsigned long fast_segs, struct iovec *fast_pointer,
880 struct iovec **ret_pointer)
881{
882 compat_ssize_t tot_len;
883 struct iovec *iov = *ret_pointer = fast_pointer;
884 ssize_t ret = 0;
885 int seg;
886
887 /*
888 * SuS says "The readv() function *may* fail if the iovcnt argument
889 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
890 * traditionally returned zero for zero segments, so...
891 */
892 if (nr_segs == 0)
893 goto out;
894
895 ret = -EINVAL;
896 if (nr_segs > UIO_MAXIOV)
897 goto out;
898 if (nr_segs > fast_segs) {
899 ret = -ENOMEM;
Kees Cook6da2ec52018-06-12 13:55:00 -0700900 iov = kmalloc_array(nr_segs, sizeof(struct iovec), GFP_KERNEL);
Al Virof5029852017-04-08 18:18:48 -0400901 if (iov == NULL)
902 goto out;
903 }
904 *ret_pointer = iov;
905
906 ret = -EFAULT;
Linus Torvalds96d4f262019-01-03 18:57:57 -0800907 if (!access_ok(uvector, nr_segs*sizeof(*uvector)))
Al Virof5029852017-04-08 18:18:48 -0400908 goto out;
909
910 /*
911 * Single unix specification:
912 * We should -EINVAL if an element length is not >= 0 and fitting an
913 * ssize_t.
914 *
915 * In Linux, the total length is limited to MAX_RW_COUNT, there is
916 * no overflow possibility.
917 */
918 tot_len = 0;
919 ret = -EINVAL;
920 for (seg = 0; seg < nr_segs; seg++) {
921 compat_uptr_t buf;
922 compat_ssize_t len;
923
924 if (__get_user(len, &uvector->iov_len) ||
925 __get_user(buf, &uvector->iov_base)) {
926 ret = -EFAULT;
927 goto out;
928 }
929 if (len < 0) /* size_t not fitting in compat_ssize_t .. */
930 goto out;
931 if (type >= 0 &&
Linus Torvalds96d4f262019-01-03 18:57:57 -0800932 !access_ok(compat_ptr(buf), len)) {
Al Virof5029852017-04-08 18:18:48 -0400933 ret = -EFAULT;
934 goto out;
935 }
936 if (len > MAX_RW_COUNT - tot_len)
937 len = MAX_RW_COUNT - tot_len;
938 tot_len += len;
939 iov->iov_base = compat_ptr(buf);
940 iov->iov_len = (compat_size_t) len;
941 uvector++;
942 iov++;
943 }
944 ret = tot_len;
945
946out:
947 return ret;
948}
949#endif
950
Christoph Hellwig19c73582017-05-27 11:16:48 +0300951static ssize_t do_iter_read(struct file *file, struct iov_iter *iter,
Christoph Hellwigddef7ed22017-07-06 18:58:37 +0200952 loff_t *pos, rwf_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 size_t tot_len;
Miklos Szeredi7687a7a2017-02-20 16:51:23 +0100955 ssize_t ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
Christoph Hellwigedab5fe2017-05-27 11:16:49 +0300957 if (!(file->f_mode & FMODE_READ))
958 return -EBADF;
959 if (!(file->f_mode & FMODE_CAN_READ))
960 return -EINVAL;
961
Miklos Szeredi7687a7a2017-02-20 16:51:23 +0100962 tot_len = iov_iter_count(iter);
Al Viro0504c072015-03-21 19:40:11 -0400963 if (!tot_len)
964 goto out;
Christoph Hellwig19c73582017-05-27 11:16:48 +0300965 ret = rw_verify_area(READ, file, pos, tot_len);
Linus Torvaldse28cc712006-01-04 16:20:40 -0800966 if (ret < 0)
Christoph Hellwig19c73582017-05-27 11:16:48 +0300967 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Christoph Hellwig19c73582017-05-27 11:16:48 +0300969 if (file->f_op->read_iter)
970 ret = do_iter_readv_writev(file, iter, pos, READ, flags);
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700971 else
Christoph Hellwig19c73582017-05-27 11:16:48 +0300972 ret = do_loop_readv_writev(file, iter, pos, READ, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973out:
Christoph Hellwig19c73582017-05-27 11:16:48 +0300974 if (ret >= 0)
975 fsnotify_access(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977}
978
Jiufei Xue5dcdc432019-11-20 17:45:25 +0800979ssize_t vfs_iocb_iter_read(struct file *file, struct kiocb *iocb,
980 struct iov_iter *iter)
981{
982 size_t tot_len;
983 ssize_t ret = 0;
984
985 if (!file->f_op->read_iter)
986 return -EINVAL;
987 if (!(file->f_mode & FMODE_READ))
988 return -EBADF;
989 if (!(file->f_mode & FMODE_CAN_READ))
990 return -EINVAL;
991
992 tot_len = iov_iter_count(iter);
993 if (!tot_len)
994 goto out;
995 ret = rw_verify_area(READ, file, &iocb->ki_pos, tot_len);
996 if (ret < 0)
997 return ret;
998
999 ret = call_read_iter(file, iocb, iter);
1000out:
1001 if (ret >= 0)
1002 fsnotify_access(file);
1003 return ret;
1004}
1005EXPORT_SYMBOL(vfs_iocb_iter_read);
1006
Christoph Hellwig18e97102017-05-27 11:16:51 +03001007ssize_t vfs_iter_read(struct file *file, struct iov_iter *iter, loff_t *ppos,
Christoph Hellwigddef7ed22017-07-06 18:58:37 +02001008 rwf_t flags)
Christoph Hellwig18e97102017-05-27 11:16:51 +03001009{
1010 if (!file->f_op->read_iter)
1011 return -EINVAL;
1012 return do_iter_read(file, iter, ppos, flags);
1013}
1014EXPORT_SYMBOL(vfs_iter_read);
1015
Christoph Hellwig19c73582017-05-27 11:16:48 +03001016static ssize_t do_iter_write(struct file *file, struct iov_iter *iter,
Christoph Hellwigddef7ed22017-07-06 18:58:37 +02001017 loff_t *pos, rwf_t flags)
Christoph Hellwig19c73582017-05-27 11:16:48 +03001018{
1019 size_t tot_len;
1020 ssize_t ret = 0;
1021
Christoph Hellwigedab5fe2017-05-27 11:16:49 +03001022 if (!(file->f_mode & FMODE_WRITE))
1023 return -EBADF;
1024 if (!(file->f_mode & FMODE_CAN_WRITE))
1025 return -EINVAL;
1026
Christoph Hellwig19c73582017-05-27 11:16:48 +03001027 tot_len = iov_iter_count(iter);
1028 if (!tot_len)
1029 return 0;
1030 ret = rw_verify_area(WRITE, file, pos, tot_len);
1031 if (ret < 0)
1032 return ret;
1033
Christoph Hellwig19c73582017-05-27 11:16:48 +03001034 if (file->f_op->write_iter)
1035 ret = do_iter_readv_writev(file, iter, pos, WRITE, flags);
1036 else
1037 ret = do_loop_readv_writev(file, iter, pos, WRITE, flags);
Christoph Hellwig19c73582017-05-27 11:16:48 +03001038 if (ret > 0)
1039 fsnotify_modify(file);
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07001040 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041}
1042
Jiufei Xue5dcdc432019-11-20 17:45:25 +08001043ssize_t vfs_iocb_iter_write(struct file *file, struct kiocb *iocb,
1044 struct iov_iter *iter)
1045{
1046 size_t tot_len;
1047 ssize_t ret = 0;
1048
1049 if (!file->f_op->write_iter)
1050 return -EINVAL;
1051 if (!(file->f_mode & FMODE_WRITE))
1052 return -EBADF;
1053 if (!(file->f_mode & FMODE_CAN_WRITE))
1054 return -EINVAL;
1055
1056 tot_len = iov_iter_count(iter);
1057 if (!tot_len)
1058 return 0;
1059 ret = rw_verify_area(WRITE, file, &iocb->ki_pos, tot_len);
1060 if (ret < 0)
1061 return ret;
1062
1063 ret = call_write_iter(file, iocb, iter);
1064 if (ret > 0)
1065 fsnotify_modify(file);
1066
1067 return ret;
1068}
1069EXPORT_SYMBOL(vfs_iocb_iter_write);
1070
Christoph Hellwigabbb65892017-05-27 11:16:52 +03001071ssize_t vfs_iter_write(struct file *file, struct iov_iter *iter, loff_t *ppos,
Christoph Hellwigddef7ed22017-07-06 18:58:37 +02001072 rwf_t flags)
Christoph Hellwigabbb65892017-05-27 11:16:52 +03001073{
1074 if (!file->f_op->write_iter)
1075 return -EINVAL;
1076 return do_iter_write(file, iter, ppos, flags);
1077}
1078EXPORT_SYMBOL(vfs_iter_write);
1079
Christoph Hellwig251b42a2017-05-27 11:16:46 +03001080ssize_t vfs_readv(struct file *file, const struct iovec __user *vec,
Christoph Hellwigddef7ed22017-07-06 18:58:37 +02001081 unsigned long vlen, loff_t *pos, rwf_t flags)
Miklos Szeredi7687a7a2017-02-20 16:51:23 +01001082{
1083 struct iovec iovstack[UIO_FASTIOV];
1084 struct iovec *iov = iovstack;
1085 struct iov_iter iter;
1086 ssize_t ret;
1087
Christoph Hellwig251b42a2017-05-27 11:16:46 +03001088 ret = import_iovec(READ, vec, vlen, ARRAY_SIZE(iovstack), &iov, &iter);
Christoph Hellwigedab5fe2017-05-27 11:16:49 +03001089 if (ret >= 0) {
1090 ret = do_iter_read(file, &iter, pos, flags);
1091 kfree(iov);
1092 }
Miklos Szeredi7687a7a2017-02-20 16:51:23 +01001093
1094 return ret;
1095}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
Christoph Hellwig9725d4c2017-09-01 17:39:25 +02001097static ssize_t vfs_writev(struct file *file, const struct iovec __user *vec,
Christoph Hellwigddef7ed22017-07-06 18:58:37 +02001098 unsigned long vlen, loff_t *pos, rwf_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099{
Christoph Hellwig251b42a2017-05-27 11:16:46 +03001100 struct iovec iovstack[UIO_FASTIOV];
1101 struct iovec *iov = iovstack;
1102 struct iov_iter iter;
1103 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Christoph Hellwig251b42a2017-05-27 11:16:46 +03001105 ret = import_iovec(WRITE, vec, vlen, ARRAY_SIZE(iovstack), &iov, &iter);
Christoph Hellwigedab5fe2017-05-27 11:16:49 +03001106 if (ret >= 0) {
Al Viro62473a22017-07-06 09:15:47 -04001107 file_start_write(file);
Christoph Hellwigedab5fe2017-05-27 11:16:49 +03001108 ret = do_iter_write(file, &iter, pos, flags);
Al Viro62473a22017-07-06 09:15:47 -04001109 file_end_write(file);
Christoph Hellwigedab5fe2017-05-27 11:16:49 +03001110 kfree(iov);
1111 }
Christoph Hellwig251b42a2017-05-27 11:16:46 +03001112 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001115static ssize_t do_readv(unsigned long fd, const struct iovec __user *vec,
Christoph Hellwigddef7ed22017-07-06 18:58:37 +02001116 unsigned long vlen, rwf_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117{
Linus Torvalds9c225f22014-03-03 09:36:58 -08001118 struct fd f = fdget_pos(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Al Viro2903ff02012-08-28 12:52:22 -04001121 if (f.file) {
Kirill Smelkov438ab722019-04-12 12:31:57 +03001122 loff_t pos, *ppos = file_ppos(f.file);
1123 if (ppos) {
1124 pos = *ppos;
1125 ppos = &pos;
1126 }
1127 ret = vfs_readv(f.file, vec, vlen, ppos, flags);
1128 if (ret >= 0 && ppos)
1129 f.file->f_pos = pos;
Linus Torvalds9c225f22014-03-03 09:36:58 -08001130 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 }
1132
1133 if (ret > 0)
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08001134 add_rchar(current, ret);
1135 inc_syscr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 return ret;
1137}
1138
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001139static ssize_t do_writev(unsigned long fd, const struct iovec __user *vec,
Christoph Hellwigddef7ed22017-07-06 18:58:37 +02001140 unsigned long vlen, rwf_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141{
Linus Torvalds9c225f22014-03-03 09:36:58 -08001142 struct fd f = fdget_pos(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 ssize_t ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Al Viro2903ff02012-08-28 12:52:22 -04001145 if (f.file) {
Kirill Smelkov438ab722019-04-12 12:31:57 +03001146 loff_t pos, *ppos = file_ppos(f.file);
1147 if (ppos) {
1148 pos = *ppos;
1149 ppos = &pos;
1150 }
1151 ret = vfs_writev(f.file, vec, vlen, ppos, flags);
1152 if (ret >= 0 && ppos)
1153 f.file->f_pos = pos;
Linus Torvalds9c225f22014-03-03 09:36:58 -08001154 fdput_pos(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 }
1156
1157 if (ret > 0)
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08001158 add_wchar(current, ret);
1159 inc_syscw(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 return ret;
1161}
1162
Linus Torvalds601cc11d2009-04-03 08:03:22 -07001163static inline loff_t pos_from_hilo(unsigned long high, unsigned long low)
Gerd Hoffmannf3554f42009-04-02 16:59:23 -07001164{
Linus Torvalds601cc11d2009-04-03 08:03:22 -07001165#define HALF_LONG_BITS (BITS_PER_LONG / 2)
1166 return (((loff_t)high << HALF_LONG_BITS) << HALF_LONG_BITS) | low;
1167}
1168
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001169static ssize_t do_preadv(unsigned long fd, const struct iovec __user *vec,
Christoph Hellwigddef7ed22017-07-06 18:58:37 +02001170 unsigned long vlen, loff_t pos, rwf_t flags)
Linus Torvalds601cc11d2009-04-03 08:03:22 -07001171{
Al Viro2903ff02012-08-28 12:52:22 -04001172 struct fd f;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -07001173 ssize_t ret = -EBADF;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -07001174
1175 if (pos < 0)
1176 return -EINVAL;
1177
Al Viro2903ff02012-08-28 12:52:22 -04001178 f = fdget(fd);
1179 if (f.file) {
Gerd Hoffmannf3554f42009-04-02 16:59:23 -07001180 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -04001181 if (f.file->f_mode & FMODE_PREAD)
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001182 ret = vfs_readv(f.file, vec, vlen, &pos, flags);
Al Viro2903ff02012-08-28 12:52:22 -04001183 fdput(f);
Gerd Hoffmannf3554f42009-04-02 16:59:23 -07001184 }
1185
1186 if (ret > 0)
1187 add_rchar(current, ret);
1188 inc_syscr(current);
1189 return ret;
1190}
1191
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001192static ssize_t do_pwritev(unsigned long fd, const struct iovec __user *vec,
Christoph Hellwigddef7ed22017-07-06 18:58:37 +02001193 unsigned long vlen, loff_t pos, rwf_t flags)
Gerd Hoffmannf3554f42009-04-02 16:59:23 -07001194{
Al Viro2903ff02012-08-28 12:52:22 -04001195 struct fd f;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -07001196 ssize_t ret = -EBADF;
Gerd Hoffmannf3554f42009-04-02 16:59:23 -07001197
1198 if (pos < 0)
1199 return -EINVAL;
1200
Al Viro2903ff02012-08-28 12:52:22 -04001201 f = fdget(fd);
1202 if (f.file) {
Gerd Hoffmannf3554f42009-04-02 16:59:23 -07001203 ret = -ESPIPE;
Al Viro2903ff02012-08-28 12:52:22 -04001204 if (f.file->f_mode & FMODE_PWRITE)
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001205 ret = vfs_writev(f.file, vec, vlen, &pos, flags);
Al Viro2903ff02012-08-28 12:52:22 -04001206 fdput(f);
Gerd Hoffmannf3554f42009-04-02 16:59:23 -07001207 }
1208
1209 if (ret > 0)
1210 add_wchar(current, ret);
1211 inc_syscw(current);
1212 return ret;
1213}
1214
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001215SYSCALL_DEFINE3(readv, unsigned long, fd, const struct iovec __user *, vec,
1216 unsigned long, vlen)
1217{
1218 return do_readv(fd, vec, vlen, 0);
1219}
1220
1221SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec __user *, vec,
1222 unsigned long, vlen)
1223{
1224 return do_writev(fd, vec, vlen, 0);
1225}
1226
1227SYSCALL_DEFINE5(preadv, unsigned long, fd, const struct iovec __user *, vec,
1228 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
1229{
1230 loff_t pos = pos_from_hilo(pos_h, pos_l);
1231
1232 return do_preadv(fd, vec, vlen, pos, 0);
1233}
1234
1235SYSCALL_DEFINE6(preadv2, unsigned long, fd, const struct iovec __user *, vec,
1236 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h,
Christoph Hellwigddef7ed22017-07-06 18:58:37 +02001237 rwf_t, flags)
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001238{
1239 loff_t pos = pos_from_hilo(pos_h, pos_l);
1240
1241 if (pos == -1)
1242 return do_readv(fd, vec, vlen, flags);
1243
1244 return do_preadv(fd, vec, vlen, pos, flags);
1245}
1246
1247SYSCALL_DEFINE5(pwritev, unsigned long, fd, const struct iovec __user *, vec,
1248 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
1249{
1250 loff_t pos = pos_from_hilo(pos_h, pos_l);
1251
1252 return do_pwritev(fd, vec, vlen, pos, 0);
1253}
1254
1255SYSCALL_DEFINE6(pwritev2, unsigned long, fd, const struct iovec __user *, vec,
1256 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h,
Christoph Hellwigddef7ed22017-07-06 18:58:37 +02001257 rwf_t, flags)
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001258{
1259 loff_t pos = pos_from_hilo(pos_h, pos_l);
1260
1261 if (pos == -1)
1262 return do_writev(fd, vec, vlen, flags);
1263
1264 return do_pwritev(fd, vec, vlen, pos, flags);
1265}
1266
Al Viro72ec3512013-03-20 10:42:10 -04001267#ifdef CONFIG_COMPAT
Al Viro72ec3512013-03-20 10:42:10 -04001268static size_t compat_readv(struct file *file,
1269 const struct compat_iovec __user *vec,
Christoph Hellwigddef7ed22017-07-06 18:58:37 +02001270 unsigned long vlen, loff_t *pos, rwf_t flags)
Al Viro72ec3512013-03-20 10:42:10 -04001271{
1272 struct iovec iovstack[UIO_FASTIOV];
1273 struct iovec *iov = iovstack;
1274 struct iov_iter iter;
1275 ssize_t ret;
1276
Christoph Hellwig26c87fb2017-05-27 11:16:47 +03001277 ret = compat_import_iovec(READ, vec, vlen, UIO_FASTIOV, &iov, &iter);
Christoph Hellwigedab5fe2017-05-27 11:16:49 +03001278 if (ret >= 0) {
1279 ret = do_iter_read(file, &iter, pos, flags);
1280 kfree(iov);
1281 }
Al Viro72ec3512013-03-20 10:42:10 -04001282 if (ret > 0)
1283 add_rchar(current, ret);
1284 inc_syscr(current);
1285 return ret;
1286}
1287
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001288static size_t do_compat_readv(compat_ulong_t fd,
1289 const struct compat_iovec __user *vec,
Christoph Hellwigddef7ed22017-07-06 18:58:37 +02001290 compat_ulong_t vlen, rwf_t flags)
Al Viro72ec3512013-03-20 10:42:10 -04001291{
Linus Torvalds9c225f22014-03-03 09:36:58 -08001292 struct fd f = fdget_pos(fd);
Al Viro72ec3512013-03-20 10:42:10 -04001293 ssize_t ret;
1294 loff_t pos;
1295
1296 if (!f.file)
1297 return -EBADF;
1298 pos = f.file->f_pos;
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001299 ret = compat_readv(f.file, vec, vlen, &pos, flags);
Al Viro5faf1532013-06-15 05:49:36 +04001300 if (ret >= 0)
1301 f.file->f_pos = pos;
Linus Torvalds9c225f22014-03-03 09:36:58 -08001302 fdput_pos(f);
Al Viro72ec3512013-03-20 10:42:10 -04001303 return ret;
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001304
Al Viro72ec3512013-03-20 10:42:10 -04001305}
1306
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001307COMPAT_SYSCALL_DEFINE3(readv, compat_ulong_t, fd,
1308 const struct compat_iovec __user *,vec,
1309 compat_ulong_t, vlen)
1310{
1311 return do_compat_readv(fd, vec, vlen, 0);
1312}
1313
1314static long do_compat_preadv64(unsigned long fd,
Heiko Carstens378a10f2014-03-05 10:43:51 +01001315 const struct compat_iovec __user *vec,
Christoph Hellwigddef7ed22017-07-06 18:58:37 +02001316 unsigned long vlen, loff_t pos, rwf_t flags)
Al Viro72ec3512013-03-20 10:42:10 -04001317{
1318 struct fd f;
1319 ssize_t ret;
1320
1321 if (pos < 0)
1322 return -EINVAL;
1323 f = fdget(fd);
1324 if (!f.file)
1325 return -EBADF;
1326 ret = -ESPIPE;
1327 if (f.file->f_mode & FMODE_PREAD)
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001328 ret = compat_readv(f.file, vec, vlen, &pos, flags);
Al Viro72ec3512013-03-20 10:42:10 -04001329 fdput(f);
1330 return ret;
1331}
1332
Heiko Carstens378a10f2014-03-05 10:43:51 +01001333#ifdef __ARCH_WANT_COMPAT_SYS_PREADV64
1334COMPAT_SYSCALL_DEFINE4(preadv64, unsigned long, fd,
1335 const struct compat_iovec __user *,vec,
1336 unsigned long, vlen, loff_t, pos)
1337{
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001338 return do_compat_preadv64(fd, vec, vlen, pos, 0);
Heiko Carstens378a10f2014-03-05 10:43:51 +01001339}
1340#endif
1341
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001342COMPAT_SYSCALL_DEFINE5(preadv, compat_ulong_t, fd,
Al Viro72ec3512013-03-20 10:42:10 -04001343 const struct compat_iovec __user *,vec,
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001344 compat_ulong_t, vlen, u32, pos_low, u32, pos_high)
Al Viro72ec3512013-03-20 10:42:10 -04001345{
1346 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
Heiko Carstens378a10f2014-03-05 10:43:51 +01001347
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001348 return do_compat_preadv64(fd, vec, vlen, pos, 0);
1349}
1350
H.J. Lu3ebfd812016-07-14 12:31:53 -07001351#ifdef __ARCH_WANT_COMPAT_SYS_PREADV64V2
1352COMPAT_SYSCALL_DEFINE5(preadv64v2, unsigned long, fd,
1353 const struct compat_iovec __user *,vec,
Christoph Hellwigddef7ed22017-07-06 18:58:37 +02001354 unsigned long, vlen, loff_t, pos, rwf_t, flags)
H.J. Lu3ebfd812016-07-14 12:31:53 -07001355{
Aurelien Jarnocc4b1242018-12-06 20:05:34 +01001356 if (pos == -1)
1357 return do_compat_readv(fd, vec, vlen, flags);
1358
H.J. Lu3ebfd812016-07-14 12:31:53 -07001359 return do_compat_preadv64(fd, vec, vlen, pos, flags);
1360}
1361#endif
1362
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001363COMPAT_SYSCALL_DEFINE6(preadv2, compat_ulong_t, fd,
1364 const struct compat_iovec __user *,vec,
1365 compat_ulong_t, vlen, u32, pos_low, u32, pos_high,
Christoph Hellwigddef7ed22017-07-06 18:58:37 +02001366 rwf_t, flags)
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001367{
1368 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
1369
1370 if (pos == -1)
1371 return do_compat_readv(fd, vec, vlen, flags);
1372
1373 return do_compat_preadv64(fd, vec, vlen, pos, flags);
Al Viro72ec3512013-03-20 10:42:10 -04001374}
1375
1376static size_t compat_writev(struct file *file,
1377 const struct compat_iovec __user *vec,
Christoph Hellwigddef7ed22017-07-06 18:58:37 +02001378 unsigned long vlen, loff_t *pos, rwf_t flags)
Al Viro72ec3512013-03-20 10:42:10 -04001379{
Christoph Hellwig26c87fb2017-05-27 11:16:47 +03001380 struct iovec iovstack[UIO_FASTIOV];
1381 struct iovec *iov = iovstack;
1382 struct iov_iter iter;
Christoph Hellwigedab5fe2017-05-27 11:16:49 +03001383 ssize_t ret;
Al Viro72ec3512013-03-20 10:42:10 -04001384
Christoph Hellwig26c87fb2017-05-27 11:16:47 +03001385 ret = compat_import_iovec(WRITE, vec, vlen, UIO_FASTIOV, &iov, &iter);
Christoph Hellwigedab5fe2017-05-27 11:16:49 +03001386 if (ret >= 0) {
Al Viro62473a22017-07-06 09:15:47 -04001387 file_start_write(file);
Christoph Hellwigedab5fe2017-05-27 11:16:49 +03001388 ret = do_iter_write(file, &iter, pos, flags);
Al Viro62473a22017-07-06 09:15:47 -04001389 file_end_write(file);
Christoph Hellwigedab5fe2017-05-27 11:16:49 +03001390 kfree(iov);
1391 }
Al Viro72ec3512013-03-20 10:42:10 -04001392 if (ret > 0)
1393 add_wchar(current, ret);
1394 inc_syscw(current);
1395 return ret;
1396}
1397
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001398static size_t do_compat_writev(compat_ulong_t fd,
1399 const struct compat_iovec __user* vec,
Christoph Hellwigddef7ed22017-07-06 18:58:37 +02001400 compat_ulong_t vlen, rwf_t flags)
Al Viro72ec3512013-03-20 10:42:10 -04001401{
Linus Torvalds9c225f22014-03-03 09:36:58 -08001402 struct fd f = fdget_pos(fd);
Al Viro72ec3512013-03-20 10:42:10 -04001403 ssize_t ret;
1404 loff_t pos;
1405
1406 if (!f.file)
1407 return -EBADF;
1408 pos = f.file->f_pos;
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001409 ret = compat_writev(f.file, vec, vlen, &pos, flags);
Al Viro5faf1532013-06-15 05:49:36 +04001410 if (ret >= 0)
1411 f.file->f_pos = pos;
Linus Torvalds9c225f22014-03-03 09:36:58 -08001412 fdput_pos(f);
Al Viro72ec3512013-03-20 10:42:10 -04001413 return ret;
1414}
1415
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001416COMPAT_SYSCALL_DEFINE3(writev, compat_ulong_t, fd,
1417 const struct compat_iovec __user *, vec,
1418 compat_ulong_t, vlen)
1419{
1420 return do_compat_writev(fd, vec, vlen, 0);
1421}
1422
1423static long do_compat_pwritev64(unsigned long fd,
Heiko Carstens378a10f2014-03-05 10:43:51 +01001424 const struct compat_iovec __user *vec,
Christoph Hellwigddef7ed22017-07-06 18:58:37 +02001425 unsigned long vlen, loff_t pos, rwf_t flags)
Al Viro72ec3512013-03-20 10:42:10 -04001426{
1427 struct fd f;
1428 ssize_t ret;
1429
1430 if (pos < 0)
1431 return -EINVAL;
1432 f = fdget(fd);
1433 if (!f.file)
1434 return -EBADF;
1435 ret = -ESPIPE;
1436 if (f.file->f_mode & FMODE_PWRITE)
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001437 ret = compat_writev(f.file, vec, vlen, &pos, flags);
Al Viro72ec3512013-03-20 10:42:10 -04001438 fdput(f);
1439 return ret;
1440}
1441
Heiko Carstens378a10f2014-03-05 10:43:51 +01001442#ifdef __ARCH_WANT_COMPAT_SYS_PWRITEV64
1443COMPAT_SYSCALL_DEFINE4(pwritev64, unsigned long, fd,
1444 const struct compat_iovec __user *,vec,
1445 unsigned long, vlen, loff_t, pos)
1446{
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001447 return do_compat_pwritev64(fd, vec, vlen, pos, 0);
Heiko Carstens378a10f2014-03-05 10:43:51 +01001448}
1449#endif
1450
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001451COMPAT_SYSCALL_DEFINE5(pwritev, compat_ulong_t, fd,
Al Viro72ec3512013-03-20 10:42:10 -04001452 const struct compat_iovec __user *,vec,
Heiko Carstensdfd948e2014-01-29 14:05:44 -08001453 compat_ulong_t, vlen, u32, pos_low, u32, pos_high)
Al Viro72ec3512013-03-20 10:42:10 -04001454{
1455 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
Heiko Carstens378a10f2014-03-05 10:43:51 +01001456
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001457 return do_compat_pwritev64(fd, vec, vlen, pos, 0);
Al Viro72ec3512013-03-20 10:42:10 -04001458}
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001459
H.J. Lu3ebfd812016-07-14 12:31:53 -07001460#ifdef __ARCH_WANT_COMPAT_SYS_PWRITEV64V2
1461COMPAT_SYSCALL_DEFINE5(pwritev64v2, unsigned long, fd,
1462 const struct compat_iovec __user *,vec,
Christoph Hellwigddef7ed22017-07-06 18:58:37 +02001463 unsigned long, vlen, loff_t, pos, rwf_t, flags)
H.J. Lu3ebfd812016-07-14 12:31:53 -07001464{
Aurelien Jarnocc4b1242018-12-06 20:05:34 +01001465 if (pos == -1)
1466 return do_compat_writev(fd, vec, vlen, flags);
1467
H.J. Lu3ebfd812016-07-14 12:31:53 -07001468 return do_compat_pwritev64(fd, vec, vlen, pos, flags);
1469}
1470#endif
1471
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001472COMPAT_SYSCALL_DEFINE6(pwritev2, compat_ulong_t, fd,
1473 const struct compat_iovec __user *,vec,
Christoph Hellwigddef7ed22017-07-06 18:58:37 +02001474 compat_ulong_t, vlen, u32, pos_low, u32, pos_high, rwf_t, flags)
Milosz Tanskif17d8b32016-03-03 16:03:59 +01001475{
1476 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
1477
1478 if (pos == -1)
1479 return do_compat_writev(fd, vec, vlen, flags);
1480
1481 return do_compat_pwritev64(fd, vec, vlen, pos, flags);
1482}
1483
Al Viro72ec3512013-03-20 10:42:10 -04001484#endif
1485
Al Viro19f4fc32013-02-24 02:17:03 -05001486static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,
1487 size_t count, loff_t max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488{
Al Viro2903ff02012-08-28 12:52:22 -04001489 struct fd in, out;
1490 struct inode *in_inode, *out_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 loff_t pos;
Al Viro7995bd22013-06-20 18:58:36 +04001492 loff_t out_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 ssize_t retval;
Al Viro2903ff02012-08-28 12:52:22 -04001494 int fl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
1496 /*
1497 * Get input file, and verify that it is ok..
1498 */
1499 retval = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -04001500 in = fdget(in_fd);
1501 if (!in.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 goto out;
Al Viro2903ff02012-08-28 12:52:22 -04001503 if (!(in.file->f_mode & FMODE_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 goto fput_in;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 retval = -ESPIPE;
Al Viro7995bd22013-06-20 18:58:36 +04001506 if (!ppos) {
1507 pos = in.file->f_pos;
1508 } else {
1509 pos = *ppos;
Al Viro2903ff02012-08-28 12:52:22 -04001510 if (!(in.file->f_mode & FMODE_PREAD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 goto fput_in;
Al Viro7995bd22013-06-20 18:58:36 +04001512 }
1513 retval = rw_verify_area(READ, in.file, &pos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -08001514 if (retval < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 goto fput_in;
Al Virobc613842016-03-31 21:48:20 -04001516 if (count > MAX_RW_COUNT)
1517 count = MAX_RW_COUNT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 /*
1520 * Get output file, and verify that it is ok..
1521 */
1522 retval = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -04001523 out = fdget(out_fd);
1524 if (!out.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 goto fput_in;
Al Viro2903ff02012-08-28 12:52:22 -04001526 if (!(out.file->f_mode & FMODE_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 goto fput_out;
Al Viro496ad9a2013-01-23 17:07:38 -05001528 in_inode = file_inode(in.file);
1529 out_inode = file_inode(out.file);
Al Viro7995bd22013-06-20 18:58:36 +04001530 out_pos = out.file->f_pos;
1531 retval = rw_verify_area(WRITE, out.file, &out_pos, count);
Linus Torvaldse28cc712006-01-04 16:20:40 -08001532 if (retval < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 goto fput_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 if (!max)
1536 max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes);
1537
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 if (unlikely(pos + count > max)) {
1539 retval = -EOVERFLOW;
1540 if (pos >= max)
1541 goto fput_out;
1542 count = max - pos;
1543 }
1544
Jens Axboed96e6e72007-06-11 12:18:52 +02001545 fl = 0;
Jens Axboe534f2aa2007-06-01 14:52:37 +02001546#if 0
Jens Axboed96e6e72007-06-11 12:18:52 +02001547 /*
1548 * We need to debate whether we can enable this or not. The
1549 * man page documents EAGAIN return for the output at least,
1550 * and the application is arguably buggy if it doesn't expect
1551 * EAGAIN on a non-blocking file descriptor.
1552 */
Al Viro2903ff02012-08-28 12:52:22 -04001553 if (in.file->f_flags & O_NONBLOCK)
Jens Axboed96e6e72007-06-11 12:18:52 +02001554 fl = SPLICE_F_NONBLOCK;
Jens Axboe534f2aa2007-06-01 14:52:37 +02001555#endif
Al Viro50cd2c52013-05-23 20:10:34 -04001556 file_start_write(out.file);
Al Viro7995bd22013-06-20 18:58:36 +04001557 retval = do_splice_direct(in.file, &pos, out.file, &out_pos, count, fl);
Al Viro50cd2c52013-05-23 20:10:34 -04001558 file_end_write(out.file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
1560 if (retval > 0) {
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08001561 add_rchar(current, retval);
1562 add_wchar(current, retval);
Scott Wolchoka68c2f12012-12-20 15:05:52 -08001563 fsnotify_access(in.file);
1564 fsnotify_modify(out.file);
Al Viro7995bd22013-06-20 18:58:36 +04001565 out.file->f_pos = out_pos;
1566 if (ppos)
1567 *ppos = pos;
1568 else
1569 in.file->f_pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08001572 inc_syscr(current);
1573 inc_syscw(current);
Al Viro7995bd22013-06-20 18:58:36 +04001574 if (pos > max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 retval = -EOVERFLOW;
1576
1577fput_out:
Al Viro2903ff02012-08-28 12:52:22 -04001578 fdput(out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579fput_in:
Al Viro2903ff02012-08-28 12:52:22 -04001580 fdput(in);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581out:
1582 return retval;
1583}
1584
Heiko Carstens002c8972009-01-14 14:14:18 +01001585SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd, off_t __user *, offset, size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586{
1587 loff_t pos;
1588 off_t off;
1589 ssize_t ret;
1590
1591 if (offset) {
1592 if (unlikely(get_user(off, offset)))
1593 return -EFAULT;
1594 pos = off;
1595 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
1596 if (unlikely(put_user(pos, offset)))
1597 return -EFAULT;
1598 return ret;
1599 }
1600
1601 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1602}
1603
Heiko Carstens002c8972009-01-14 14:14:18 +01001604SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd, loff_t __user *, offset, size_t, count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605{
1606 loff_t pos;
1607 ssize_t ret;
1608
1609 if (offset) {
1610 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
1611 return -EFAULT;
1612 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
1613 if (unlikely(put_user(pos, offset)))
1614 return -EFAULT;
1615 return ret;
1616 }
1617
1618 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1619}
Al Viro19f4fc32013-02-24 02:17:03 -05001620
1621#ifdef CONFIG_COMPAT
1622COMPAT_SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd,
1623 compat_off_t __user *, offset, compat_size_t, count)
1624{
1625 loff_t pos;
1626 off_t off;
1627 ssize_t ret;
1628
1629 if (offset) {
1630 if (unlikely(get_user(off, offset)))
1631 return -EFAULT;
1632 pos = off;
1633 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
1634 if (unlikely(put_user(pos, offset)))
1635 return -EFAULT;
1636 return ret;
1637 }
1638
1639 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1640}
1641
1642COMPAT_SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd,
1643 compat_loff_t __user *, offset, compat_size_t, count)
1644{
1645 loff_t pos;
1646 ssize_t ret;
1647
1648 if (offset) {
1649 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
1650 return -EFAULT;
1651 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
1652 if (unlikely(put_user(pos, offset)))
1653 return -EFAULT;
1654 return ret;
1655 }
1656
1657 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1658}
1659#endif
Zach Brown29732932015-11-10 16:53:30 -05001660
Dave Chinnerf16acc92019-06-05 08:04:47 -07001661/**
1662 * generic_copy_file_range - copy data between two files
1663 * @file_in: file structure to read from
1664 * @pos_in: file offset to read from
1665 * @file_out: file structure to write data to
1666 * @pos_out: file offset to write data to
1667 * @len: amount of data to copy
1668 * @flags: copy flags
1669 *
1670 * This is a generic filesystem helper to copy data from one file to another.
1671 * It has no constraints on the source or destination file owners - the files
1672 * can belong to different superblocks and different filesystem types. Short
1673 * copies are allowed.
1674 *
1675 * This should be called from the @file_out filesystem, as per the
1676 * ->copy_file_range() method.
1677 *
1678 * Returns the number of bytes copied or a negative error indicating the
1679 * failure.
1680 */
1681
1682ssize_t generic_copy_file_range(struct file *file_in, loff_t pos_in,
1683 struct file *file_out, loff_t pos_out,
1684 size_t len, unsigned int flags)
1685{
1686 return do_splice_direct(file_in, &pos_in, file_out, &pos_out,
1687 len > MAX_RW_COUNT ? MAX_RW_COUNT : len, 0);
1688}
1689EXPORT_SYMBOL(generic_copy_file_range);
1690
Dave Chinner64bf5ff2019-06-05 08:04:47 -07001691static ssize_t do_copy_file_range(struct file *file_in, loff_t pos_in,
1692 struct file *file_out, loff_t pos_out,
1693 size_t len, unsigned int flags)
1694{
Amir Goldstein5dae2222019-06-05 08:04:50 -07001695 /*
1696 * Although we now allow filesystems to handle cross sb copy, passing
1697 * a file of the wrong filesystem type to filesystem driver can result
1698 * in an attempt to dereference the wrong type of ->private_data, so
1699 * avoid doing that until we really have a good reason. NFS defines
1700 * several different file_system_type structures, but they all end up
1701 * using the same ->copy_file_range() function pointer.
1702 */
1703 if (file_out->f_op->copy_file_range &&
1704 file_out->f_op->copy_file_range == file_in->f_op->copy_file_range)
Dave Chinner64bf5ff2019-06-05 08:04:47 -07001705 return file_out->f_op->copy_file_range(file_in, pos_in,
1706 file_out, pos_out,
1707 len, flags);
1708
1709 return generic_copy_file_range(file_in, pos_in, file_out, pos_out, len,
1710 flags);
1711}
1712
Zach Brown29732932015-11-10 16:53:30 -05001713/*
1714 * copy_file_range() differs from regular file read and write in that it
1715 * specifically allows return partial success. When it does so is up to
1716 * the copy_file_range method.
1717 */
1718ssize_t vfs_copy_file_range(struct file *file_in, loff_t pos_in,
1719 struct file *file_out, loff_t pos_out,
1720 size_t len, unsigned int flags)
1721{
Zach Brown29732932015-11-10 16:53:30 -05001722 ssize_t ret;
1723
1724 if (flags != 0)
1725 return -EINVAL;
1726
Amir Goldstein96e6e8f2019-06-05 08:04:49 -07001727 ret = generic_copy_file_checks(file_in, pos_in, file_out, pos_out, &len,
1728 flags);
Amir Goldsteina3171352019-06-05 08:04:48 -07001729 if (unlikely(ret))
1730 return ret;
Amir Goldstein11cbfb12017-01-31 10:34:56 +02001731
Zach Brown29732932015-11-10 16:53:30 -05001732 ret = rw_verify_area(READ, file_in, &pos_in, len);
Al Virobc613842016-03-31 21:48:20 -04001733 if (unlikely(ret))
1734 return ret;
1735
1736 ret = rw_verify_area(WRITE, file_out, &pos_out, len);
1737 if (unlikely(ret))
Zach Brown29732932015-11-10 16:53:30 -05001738 return ret;
1739
Zach Brown29732932015-11-10 16:53:30 -05001740 if (len == 0)
1741 return 0;
1742
Amir Goldsteinbfe219d2017-01-31 10:34:57 +02001743 file_start_write(file_out);
Zach Brown29732932015-11-10 16:53:30 -05001744
Christoph Hellwiga76b5b02016-12-09 16:17:19 -08001745 /*
1746 * Try cloning first, this is supported by more file systems, and
1747 * more efficient if both clone and copy are supported (e.g. NFS).
1748 */
Amir Goldstein5dae2222019-06-05 08:04:50 -07001749 if (file_in->f_op->remap_file_range &&
1750 file_inode(file_in)->i_sb == file_inode(file_out)->i_sb) {
Darrick J. Wong42ec3d42018-10-30 10:41:49 +11001751 loff_t cloned;
1752
1753 cloned = file_in->f_op->remap_file_range(file_in, pos_in,
1754 file_out, pos_out,
Darrick J. Wongeca36542018-10-30 10:42:10 +11001755 min_t(loff_t, MAX_RW_COUNT, len),
1756 REMAP_FILE_CAN_SHORTEN);
Darrick J. Wong42ec3d42018-10-30 10:41:49 +11001757 if (cloned > 0) {
1758 ret = cloned;
Christoph Hellwiga76b5b02016-12-09 16:17:19 -08001759 goto done;
1760 }
1761 }
1762
Dave Chinner64bf5ff2019-06-05 08:04:47 -07001763 ret = do_copy_file_range(file_in, pos_in, file_out, pos_out, len,
1764 flags);
1765 WARN_ON_ONCE(ret == -EOPNOTSUPP);
Christoph Hellwiga76b5b02016-12-09 16:17:19 -08001766done:
Zach Brown29732932015-11-10 16:53:30 -05001767 if (ret > 0) {
1768 fsnotify_access(file_in);
1769 add_rchar(current, ret);
1770 fsnotify_modify(file_out);
1771 add_wchar(current, ret);
1772 }
Christoph Hellwiga76b5b02016-12-09 16:17:19 -08001773
Zach Brown29732932015-11-10 16:53:30 -05001774 inc_syscr(current);
1775 inc_syscw(current);
1776
Amir Goldsteinbfe219d2017-01-31 10:34:57 +02001777 file_end_write(file_out);
Zach Brown29732932015-11-10 16:53:30 -05001778
1779 return ret;
1780}
1781EXPORT_SYMBOL(vfs_copy_file_range);
1782
1783SYSCALL_DEFINE6(copy_file_range, int, fd_in, loff_t __user *, off_in,
1784 int, fd_out, loff_t __user *, off_out,
1785 size_t, len, unsigned int, flags)
1786{
1787 loff_t pos_in;
1788 loff_t pos_out;
1789 struct fd f_in;
1790 struct fd f_out;
1791 ssize_t ret = -EBADF;
1792
1793 f_in = fdget(fd_in);
1794 if (!f_in.file)
1795 goto out2;
1796
1797 f_out = fdget(fd_out);
1798 if (!f_out.file)
1799 goto out1;
1800
1801 ret = -EFAULT;
1802 if (off_in) {
1803 if (copy_from_user(&pos_in, off_in, sizeof(loff_t)))
1804 goto out;
1805 } else {
1806 pos_in = f_in.file->f_pos;
1807 }
1808
1809 if (off_out) {
1810 if (copy_from_user(&pos_out, off_out, sizeof(loff_t)))
1811 goto out;
1812 } else {
1813 pos_out = f_out.file->f_pos;
1814 }
1815
1816 ret = vfs_copy_file_range(f_in.file, pos_in, f_out.file, pos_out, len,
1817 flags);
1818 if (ret > 0) {
1819 pos_in += ret;
1820 pos_out += ret;
1821
1822 if (off_in) {
1823 if (copy_to_user(off_in, &pos_in, sizeof(loff_t)))
1824 ret = -EFAULT;
1825 } else {
1826 f_in.file->f_pos = pos_in;
1827 }
1828
1829 if (off_out) {
1830 if (copy_to_user(off_out, &pos_out, sizeof(loff_t)))
1831 ret = -EFAULT;
1832 } else {
1833 f_out.file->f_pos = pos_out;
1834 }
1835 }
1836
1837out:
1838 fdput(f_out);
1839out1:
1840 fdput(f_in);
1841out2:
1842 return ret;
1843}
Christoph Hellwig04b38d62015-12-03 12:59:50 +01001844
Darrick J. Wong42ec3d42018-10-30 10:41:49 +11001845static int remap_verify_area(struct file *file, loff_t pos, loff_t len,
1846 bool write)
Christoph Hellwig04b38d62015-12-03 12:59:50 +01001847{
1848 struct inode *inode = file_inode(file);
1849
Darrick J. Wong42ec3d42018-10-30 10:41:49 +11001850 if (unlikely(pos < 0 || len < 0))
Christoph Hellwig04b38d62015-12-03 12:59:50 +01001851 return -EINVAL;
1852
1853 if (unlikely((loff_t) (pos + len) < 0))
1854 return -EINVAL;
1855
1856 if (unlikely(inode->i_flctx && mandatory_lock(inode))) {
1857 loff_t end = len ? pos + len - 1 : OFFSET_MAX;
1858 int retval;
1859
1860 retval = locks_mandatory_area(inode, file, pos, end,
1861 write ? F_WRLCK : F_RDLCK);
1862 if (retval < 0)
1863 return retval;
1864 }
1865
1866 return security_file_permission(file, write ? MAY_WRITE : MAY_READ);
1867}
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001868/*
Darrick J. Wong07d19dc2018-10-30 10:40:55 +11001869 * Ensure that we don't remap a partial EOF block in the middle of something
1870 * else. Assume that the offsets have already been checked for block
1871 * alignment.
Darrick J. Wong22725ce2016-12-19 15:13:26 -08001872 *
Filipe Mananaa5e6ea12019-12-16 18:26:55 +00001873 * For clone we only link a partial EOF block above or at the destination file's
1874 * EOF. For deduplication we accept a partial EOF block only if it ends at the
1875 * destination file's EOF (can not link it into the middle of a file).
Darrick J. Wongeca36542018-10-30 10:42:10 +11001876 *
1877 * Shorten the request if possible.
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001878 */
Darrick J. Wong07d19dc2018-10-30 10:40:55 +11001879static int generic_remap_check_len(struct inode *inode_in,
1880 struct inode *inode_out,
1881 loff_t pos_out,
Darrick J. Wong42ec3d42018-10-30 10:41:49 +11001882 loff_t *len,
Darrick J. Wonga91ae49b2018-10-30 10:41:28 +11001883 unsigned int remap_flags)
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001884{
Darrick J. Wong07d19dc2018-10-30 10:40:55 +11001885 u64 blkmask = i_blocksize(inode_in) - 1;
Darrick J. Wongeca36542018-10-30 10:42:10 +11001886 loff_t new_len = *len;
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001887
Darrick J. Wong07d19dc2018-10-30 10:40:55 +11001888 if ((*len & blkmask) == 0)
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001889 return 0;
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001890
Filipe Mananaa5e6ea12019-12-16 18:26:55 +00001891 if (pos_out + *len < i_size_read(inode_out))
Darrick J. Wongeca36542018-10-30 10:42:10 +11001892 new_len &= ~blkmask;
Darrick J. Wong07d19dc2018-10-30 10:40:55 +11001893
Darrick J. Wongeca36542018-10-30 10:42:10 +11001894 if (new_len == *len)
1895 return 0;
1896
1897 if (remap_flags & REMAP_FILE_CAN_SHORTEN) {
1898 *len = new_len;
1899 return 0;
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001900 }
1901
Darrick J. Wongeca36542018-10-30 10:42:10 +11001902 return (remap_flags & REMAP_FILE_DEDUP) ? -EBADE : -EINVAL;
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001903}
Darrick J. Wong54dbc152015-12-19 00:55:59 -08001904
Darrick J. Wongedc58dd2019-08-11 15:52:25 -07001905/* Read a page's worth of file data into the page cache. */
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001906static struct page *vfs_dedupe_get_page(struct inode *inode, loff_t offset)
1907{
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001908 struct page *page;
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001909
Darrick J. Wongc32e5f32018-10-30 10:42:17 +11001910 page = read_mapping_page(inode->i_mapping, offset >> PAGE_SHIFT, NULL);
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001911 if (IS_ERR(page))
1912 return page;
1913 if (!PageUptodate(page)) {
1914 put_page(page);
1915 return ERR_PTR(-EIO);
1916 }
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001917 return page;
1918}
1919
1920/*
Darrick J. Wongedc58dd2019-08-11 15:52:25 -07001921 * Lock two pages, ensuring that we lock in offset order if the pages are from
1922 * the same file.
1923 */
1924static void vfs_lock_two_pages(struct page *page1, struct page *page2)
1925{
1926 /* Always lock in order of increasing index. */
1927 if (page1->index > page2->index)
1928 swap(page1, page2);
1929
1930 lock_page(page1);
1931 if (page1 != page2)
1932 lock_page(page2);
1933}
1934
1935/* Unlock two pages, being careful not to unlock the same page twice. */
1936static void vfs_unlock_two_pages(struct page *page1, struct page *page2)
1937{
1938 unlock_page(page1);
1939 if (page1 != page2)
1940 unlock_page(page2);
1941}
1942
1943/*
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001944 * Compare extents of two files to see if they are the same.
1945 * Caller must have locked both inodes to prevent write races.
1946 */
Darrick J. Wongc32e5f32018-10-30 10:42:17 +11001947static int vfs_dedupe_file_range_compare(struct inode *src, loff_t srcoff,
1948 struct inode *dest, loff_t destoff,
1949 loff_t len, bool *is_same)
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001950{
1951 loff_t src_poff;
1952 loff_t dest_poff;
1953 void *src_addr;
1954 void *dest_addr;
1955 struct page *src_page;
1956 struct page *dest_page;
1957 loff_t cmp_len;
1958 bool same;
1959 int error;
1960
1961 error = -EINVAL;
1962 same = true;
1963 while (len) {
1964 src_poff = srcoff & (PAGE_SIZE - 1);
1965 dest_poff = destoff & (PAGE_SIZE - 1);
1966 cmp_len = min(PAGE_SIZE - src_poff,
1967 PAGE_SIZE - dest_poff);
1968 cmp_len = min(cmp_len, len);
1969 if (cmp_len <= 0)
1970 goto out_error;
1971
1972 src_page = vfs_dedupe_get_page(src, srcoff);
1973 if (IS_ERR(src_page)) {
1974 error = PTR_ERR(src_page);
1975 goto out_error;
1976 }
1977 dest_page = vfs_dedupe_get_page(dest, destoff);
1978 if (IS_ERR(dest_page)) {
1979 error = PTR_ERR(dest_page);
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001980 put_page(src_page);
1981 goto out_error;
1982 }
Darrick J. Wongedc58dd2019-08-11 15:52:25 -07001983
1984 vfs_lock_two_pages(src_page, dest_page);
1985
1986 /*
1987 * Now that we've locked both pages, make sure they're still
1988 * mapped to the file data we're interested in. If not,
1989 * someone is invalidating pages on us and we lose.
1990 */
1991 if (!PageUptodate(src_page) || !PageUptodate(dest_page) ||
1992 src_page->mapping != src->i_mapping ||
1993 dest_page->mapping != dest->i_mapping) {
1994 same = false;
1995 goto unlock;
1996 }
1997
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001998 src_addr = kmap_atomic(src_page);
1999 dest_addr = kmap_atomic(dest_page);
2000
2001 flush_dcache_page(src_page);
2002 flush_dcache_page(dest_page);
2003
2004 if (memcmp(src_addr + src_poff, dest_addr + dest_poff, cmp_len))
2005 same = false;
2006
2007 kunmap_atomic(dest_addr);
2008 kunmap_atomic(src_addr);
Darrick J. Wongedc58dd2019-08-11 15:52:25 -07002009unlock:
2010 vfs_unlock_two_pages(src_page, dest_page);
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08002011 put_page(dest_page);
2012 put_page(src_page);
2013
2014 if (!same)
2015 break;
2016
2017 srcoff += cmp_len;
2018 destoff += cmp_len;
2019 len -= cmp_len;
2020 }
2021
2022 *is_same = same;
2023 return 0;
2024
2025out_error:
2026 return error;
2027}
Darrick J. Wongc32e5f32018-10-30 10:42:17 +11002028
2029/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 * Check that the two inodes are eligible for cloning, the ranges make
2031 * sense, and then flush all dirty data. Caller must ensure that the
2032 * inodes have been locked against any other modifications.
2033 *
Darrick J. Wong8c5c8362018-10-30 10:42:24 +11002034 * If there's an error, then the usual negative error code is returned.
2035 * Otherwise returns 0 with *len set to the request length.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 */
Darrick J. Wonga83ab012018-10-30 10:41:08 +11002037int generic_remap_file_range_prep(struct file *file_in, loff_t pos_in,
2038 struct file *file_out, loff_t pos_out,
Darrick J. Wong42ec3d42018-10-30 10:41:49 +11002039 loff_t *len, unsigned int remap_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040{
Darrick J. Wong1383a7e2018-10-30 10:40:31 +11002041 struct inode *inode_in = file_inode(file_in);
2042 struct inode *inode_out = file_inode(file_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 bool same_inode = (inode_in == inode_out);
2044 int ret;
2045
2046 /* Don't touch certain kinds of inodes */
2047 if (IS_IMMUTABLE(inode_out))
2048 return -EPERM;
2049
2050 if (IS_SWAPFILE(inode_in) || IS_SWAPFILE(inode_out))
2051 return -ETXTBSY;
2052
2053 /* Don't reflink dirs, pipes, sockets... */
2054 if (S_ISDIR(inode_in->i_mode) || S_ISDIR(inode_out->i_mode))
2055 return -EISDIR;
2056 if (!S_ISREG(inode_in->i_mode) || !S_ISREG(inode_out->i_mode))
2057 return -EINVAL;
2058
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 /* Zero length dedupe exits immediately; reflink goes to EOF. */
2060 if (*len == 0) {
Darrick J. Wong1383a7e2018-10-30 10:40:31 +11002061 loff_t isize = i_size_read(inode_in);
2062
Darrick J. Wonga91ae49b2018-10-30 10:41:28 +11002063 if ((remap_flags & REMAP_FILE_DEDUP) || pos_in == isize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 return 0;
2065 if (pos_in > isize)
2066 return -EINVAL;
2067 *len = isize - pos_in;
Darrick J. Wong2c5773f2018-10-30 10:40:39 +11002068 if (*len == 0)
2069 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 }
2071
Darrick J. Wong1383a7e2018-10-30 10:40:31 +11002072 /* Check that we don't violate system file offset limits. */
2073 ret = generic_remap_checks(file_in, pos_in, file_out, pos_out, len,
Darrick J. Wong3d281932018-10-30 10:41:34 +11002074 remap_flags);
Darrick J. Wong1383a7e2018-10-30 10:40:31 +11002075 if (ret)
2076 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
2078 /* Wait for the completion of any pending IOs on both files */
2079 inode_dio_wait(inode_in);
2080 if (!same_inode)
2081 inode_dio_wait(inode_out);
2082
2083 ret = filemap_write_and_wait_range(inode_in->i_mapping,
2084 pos_in, pos_in + *len - 1);
2085 if (ret)
2086 return ret;
2087
2088 ret = filemap_write_and_wait_range(inode_out->i_mapping,
Christoph Hellwig04b38d62015-12-03 12:59:50 +01002089 pos_out, pos_out + *len - 1);
2090 if (ret)
2091 return ret;
2092
2093 /*
2094 * Check that the extents are the same.
2095 */
Darrick J. Wonga91ae49b2018-10-30 10:41:28 +11002096 if (remap_flags & REMAP_FILE_DEDUP) {
Christoph Hellwig04b38d62015-12-03 12:59:50 +01002097 bool is_same = false;
2098
2099 ret = vfs_dedupe_file_range_compare(inode_in, pos_in,
2100 inode_out, pos_out, *len, &is_same);
2101 if (ret)
2102 return ret;
2103 if (!is_same)
2104 return -EBADE;
2105 }
2106
Darrick J. Wong07d19dc2018-10-30 10:40:55 +11002107 ret = generic_remap_check_len(inode_in, inode_out, pos_out, len,
Darrick J. Wonga91ae49b2018-10-30 10:41:28 +11002108 remap_flags);
Darrick J. Wong07d19dc2018-10-30 10:40:55 +11002109 if (ret)
2110 return ret;
2111
Darrick J. Wong8dde90b2018-10-30 10:41:41 +11002112 /* If can't alter the file contents, we're done. */
Amir Goldsteine38f7f52019-06-05 08:04:49 -07002113 if (!(remap_flags & REMAP_FILE_DEDUP))
2114 ret = file_modified(file_out);
Darrick J. Wong8dde90b2018-10-30 10:41:41 +11002115
Amir Goldsteine38f7f52019-06-05 08:04:49 -07002116 return ret;
Christoph Hellwig04b38d62015-12-03 12:59:50 +01002117}
Darrick J. Wonga83ab012018-10-30 10:41:08 +11002118EXPORT_SYMBOL(generic_remap_file_range_prep);
Christoph Hellwig04b38d62015-12-03 12:59:50 +01002119
Darrick J. Wong42ec3d42018-10-30 10:41:49 +11002120loff_t do_clone_file_range(struct file *file_in, loff_t pos_in,
Darrick J. Wong452ce652018-10-30 10:41:56 +11002121 struct file *file_out, loff_t pos_out,
2122 loff_t len, unsigned int remap_flags)
Christoph Hellwig04b38d62015-12-03 12:59:50 +01002123{
Darrick J. Wong42ec3d42018-10-30 10:41:49 +11002124 loff_t ret;
Christoph Hellwig04b38d62015-12-03 12:59:50 +01002125
Darrick J. Wong67445572018-11-30 12:32:38 -08002126 WARN_ON_ONCE(remap_flags & REMAP_FILE_DEDUP);
Darrick J. Wong452ce652018-10-30 10:41:56 +11002127
Christoph Hellwig04b38d62015-12-03 12:59:50 +01002128 /*
2129 * FICLONE/FICLONERANGE ioctls enforce that src and dest files are on
2130 * the same mount. Practically, they only need to be on the same file
2131 * system.
Darrick J. Wongd79bdd52015-12-19 00:55:52 -08002132 */
Amir Goldsteina3171352019-06-05 08:04:48 -07002133 if (file_inode(file_in)->i_sb != file_inode(file_out)->i_sb)
Christoph Hellwig04b38d62015-12-03 12:59:50 +01002134 return -EXDEV;
2135
Amir Goldsteina3171352019-06-05 08:04:48 -07002136 ret = generic_file_rw_checks(file_in, file_out);
2137 if (ret < 0)
2138 return ret;
Christoph Hellwig04b38d62015-12-03 12:59:50 +01002139
Darrick J. Wong2e5dfc92018-10-30 10:41:21 +11002140 if (!file_in->f_op->remap_file_range)
Christoph Hellwig04b38d62015-12-03 12:59:50 +01002141 return -EOPNOTSUPP;
2142
Darrick J. Wong60950282018-10-30 10:41:14 +11002143 ret = remap_verify_area(file_in, pos_in, len, false);
Christoph Hellwig04b38d62015-12-03 12:59:50 +01002144 if (ret)
2145 return ret;
2146
Darrick J. Wong60950282018-10-30 10:41:14 +11002147 ret = remap_verify_area(file_out, pos_out, len, true);
Christoph Hellwig04b38d62015-12-03 12:59:50 +01002148 if (ret)
2149 return ret;
2150
Darrick J. Wong2e5dfc92018-10-30 10:41:21 +11002151 ret = file_in->f_op->remap_file_range(file_in, pos_in,
Darrick J. Wong452ce652018-10-30 10:41:56 +11002152 file_out, pos_out, len, remap_flags);
Darrick J. Wong42ec3d42018-10-30 10:41:49 +11002153 if (ret < 0)
2154 return ret;
Christoph Hellwig04b38d62015-12-03 12:59:50 +01002155
Darrick J. Wong42ec3d42018-10-30 10:41:49 +11002156 fsnotify_access(file_in);
2157 fsnotify_modify(file_out);
Christoph Hellwig04b38d62015-12-03 12:59:50 +01002158 return ret;
2159}
2160EXPORT_SYMBOL(do_clone_file_range);
Darrick J. Wong54dbc152015-12-19 00:55:59 -08002161
Darrick J. Wong42ec3d42018-10-30 10:41:49 +11002162loff_t vfs_clone_file_range(struct file *file_in, loff_t pos_in,
Darrick J. Wong452ce652018-10-30 10:41:56 +11002163 struct file *file_out, loff_t pos_out,
2164 loff_t len, unsigned int remap_flags)
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08002165{
Darrick J. Wong42ec3d42018-10-30 10:41:49 +11002166 loff_t ret;
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08002167
2168 file_start_write(file_out);
Darrick J. Wong452ce652018-10-30 10:41:56 +11002169 ret = do_clone_file_range(file_in, pos_in, file_out, pos_out, len,
2170 remap_flags);
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08002171 file_end_write(file_out);
2172
2173 return ret;
2174}
2175EXPORT_SYMBOL(vfs_clone_file_range);
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08002176
Mark Fasheh5de44802018-09-10 16:21:17 -07002177/* Check whether we are allowed to dedupe the destination file */
2178static bool allow_file_dedupe(struct file *file)
2179{
2180 if (capable(CAP_SYS_ADMIN))
2181 return true;
2182 if (file->f_mode & FMODE_WRITE)
2183 return true;
2184 if (uid_eq(current_fsuid(), file_inode(file)->i_uid))
2185 return true;
2186 if (!inode_permission(file_inode(file), MAY_WRITE))
2187 return true;
2188 return false;
2189}
2190
Darrick J. Wong42ec3d42018-10-30 10:41:49 +11002191loff_t vfs_dedupe_file_range_one(struct file *src_file, loff_t src_pos,
2192 struct file *dst_file, loff_t dst_pos,
Darrick J. Wongdf365832018-10-30 10:42:03 +11002193 loff_t len, unsigned int remap_flags)
Miklos Szeredi1b4f42a2018-07-06 23:57:03 +02002194{
Darrick J. Wong42ec3d42018-10-30 10:41:49 +11002195 loff_t ret;
Miklos Szeredi1b4f42a2018-07-06 23:57:03 +02002196
Darrick J. Wongeca36542018-10-30 10:42:10 +11002197 WARN_ON_ONCE(remap_flags & ~(REMAP_FILE_DEDUP |
2198 REMAP_FILE_CAN_SHORTEN));
Miklos Szeredi1b4f42a2018-07-06 23:57:03 +02002199
2200 ret = mnt_want_write_file(dst_file);
2201 if (ret)
2202 return ret;
2203
Darrick J. Wong60950282018-10-30 10:41:14 +11002204 ret = remap_verify_area(dst_file, dst_pos, len, true);
Miklos Szeredi1b4f42a2018-07-06 23:57:03 +02002205 if (ret < 0)
2206 goto out_drop_write;
2207
Mark Fasheh85c95f22018-09-10 16:21:18 -07002208 ret = -EPERM;
Mark Fasheh5de44802018-09-10 16:21:17 -07002209 if (!allow_file_dedupe(dst_file))
Miklos Szeredi1b4f42a2018-07-06 23:57:03 +02002210 goto out_drop_write;
2211
2212 ret = -EXDEV;
2213 if (src_file->f_path.mnt != dst_file->f_path.mnt)
2214 goto out_drop_write;
2215
2216 ret = -EISDIR;
2217 if (S_ISDIR(file_inode(dst_file)->i_mode))
2218 goto out_drop_write;
2219
2220 ret = -EINVAL;
Darrick J. Wong2e5dfc92018-10-30 10:41:21 +11002221 if (!dst_file->f_op->remap_file_range)
Miklos Szeredi1b4f42a2018-07-06 23:57:03 +02002222 goto out_drop_write;
2223
Darrick J. Wong9aae2052018-10-30 10:41:01 +11002224 if (len == 0) {
2225 ret = 0;
2226 goto out_drop_write;
2227 }
2228
Darrick J. Wong2e5dfc92018-10-30 10:41:21 +11002229 ret = dst_file->f_op->remap_file_range(src_file, src_pos, dst_file,
Darrick J. Wongdf365832018-10-30 10:42:03 +11002230 dst_pos, len, remap_flags | REMAP_FILE_DEDUP);
Miklos Szeredi1b4f42a2018-07-06 23:57:03 +02002231out_drop_write:
2232 mnt_drop_write_file(dst_file);
2233
2234 return ret;
2235}
Miklos Szeredif1825362018-07-18 15:44:40 +02002236EXPORT_SYMBOL(vfs_dedupe_file_range_one);
Miklos Szeredi1b4f42a2018-07-06 23:57:03 +02002237
Darrick J. Wong54dbc152015-12-19 00:55:59 -08002238int vfs_dedupe_file_range(struct file *file, struct file_dedupe_range *same)
2239{
2240 struct file_dedupe_range_info *info;
2241 struct inode *src = file_inode(file);
2242 u64 off;
2243 u64 len;
2244 int i;
2245 int ret;
Darrick J. Wong54dbc152015-12-19 00:55:59 -08002246 u16 count = same->dest_count;
Darrick J. Wong42ec3d42018-10-30 10:41:49 +11002247 loff_t deduped;
Darrick J. Wong54dbc152015-12-19 00:55:59 -08002248
2249 if (!(file->f_mode & FMODE_READ))
2250 return -EINVAL;
2251
2252 if (same->reserved1 || same->reserved2)
2253 return -EINVAL;
2254
2255 off = same->src_offset;
2256 len = same->src_length;
2257
Darrick J. Wong54dbc152015-12-19 00:55:59 -08002258 if (S_ISDIR(src->i_mode))
Dave Chinner494633f2018-11-19 13:31:12 -08002259 return -EISDIR;
Darrick J. Wong54dbc152015-12-19 00:55:59 -08002260
Darrick J. Wong54dbc152015-12-19 00:55:59 -08002261 if (!S_ISREG(src->i_mode))
Dave Chinner494633f2018-11-19 13:31:12 -08002262 return -EINVAL;
2263
2264 if (!file->f_op->remap_file_range)
2265 return -EOPNOTSUPP;
Darrick J. Wong54dbc152015-12-19 00:55:59 -08002266
Darrick J. Wong60950282018-10-30 10:41:14 +11002267 ret = remap_verify_area(file, off, len, false);
Darrick J. Wong54dbc152015-12-19 00:55:59 -08002268 if (ret < 0)
Dave Chinner494633f2018-11-19 13:31:12 -08002269 return ret;
Darrick J. Wong54dbc152015-12-19 00:55:59 -08002270 ret = 0;
2271
Darrick J. Wong22725ce2016-12-19 15:13:26 -08002272 if (off + len > i_size_read(src))
2273 return -EINVAL;
2274
Miklos Szeredi92b66d22018-07-06 23:57:02 +02002275 /* Arbitrary 1G limit on a single dedupe request, can be raised. */
2276 len = min_t(u64, len, 1 << 30);
2277
Darrick J. Wong54dbc152015-12-19 00:55:59 -08002278 /* pre-format output fields to sane values */
2279 for (i = 0; i < count; i++) {
2280 same->info[i].bytes_deduped = 0ULL;
2281 same->info[i].status = FILE_DEDUPE_RANGE_SAME;
2282 }
2283
2284 for (i = 0, info = same->info; i < count; i++, info++) {
Darrick J. Wong54dbc152015-12-19 00:55:59 -08002285 struct fd dst_fd = fdget(info->dest_fd);
Miklos Szeredi1b4f42a2018-07-06 23:57:03 +02002286 struct file *dst_file = dst_fd.file;
Darrick J. Wong54dbc152015-12-19 00:55:59 -08002287
Darrick J. Wong54dbc152015-12-19 00:55:59 -08002288 if (!dst_file) {
2289 info->status = -EBADF;
2290 goto next_loop;
2291 }
Darrick J. Wong54dbc152015-12-19 00:55:59 -08002292
2293 if (info->reserved) {
2294 info->status = -EINVAL;
Miklos Szeredi1b4f42a2018-07-06 23:57:03 +02002295 goto next_fdput;
Darrick J. Wong54dbc152015-12-19 00:55:59 -08002296 }
2297
Miklos Szeredi1b4f42a2018-07-06 23:57:03 +02002298 deduped = vfs_dedupe_file_range_one(file, off, dst_file,
Darrick J. Wongdf365832018-10-30 10:42:03 +11002299 info->dest_offset, len,
Darrick J. Wongeca36542018-10-30 10:42:10 +11002300 REMAP_FILE_CAN_SHORTEN);
Miklos Szeredi1b4f42a2018-07-06 23:57:03 +02002301 if (deduped == -EBADE)
2302 info->status = FILE_DEDUPE_RANGE_DIFFERS;
2303 else if (deduped < 0)
2304 info->status = deduped;
2305 else
2306 info->bytes_deduped = len;
2307
Zev Weiss22762712018-04-14 01:16:58 -05002308next_fdput:
Darrick J. Wong54dbc152015-12-19 00:55:59 -08002309 fdput(dst_fd);
Zev Weiss22762712018-04-14 01:16:58 -05002310next_loop:
Darrick J. Wonge62e5602016-01-22 16:58:28 -08002311 if (fatal_signal_pending(current))
Dave Chinner494633f2018-11-19 13:31:12 -08002312 break;
Darrick J. Wong54dbc152015-12-19 00:55:59 -08002313 }
Darrick J. Wong54dbc152015-12-19 00:55:59 -08002314 return ret;
2315}
2316EXPORT_SYMBOL(vfs_dedupe_file_range);