x86: wire up preadv2 and pwritev2
[deliverable/linux.git] / fs / read_write.c
CommitLineData
1da177e4
LT
1/*
2 * linux/fs/read_write.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
7#include <linux/slab.h>
8#include <linux/stat.h>
9#include <linux/fcntl.h>
10#include <linux/file.h>
11#include <linux/uio.h>
0eeca283 12#include <linux/fsnotify.h>
1da177e4 13#include <linux/security.h>
630d9c47 14#include <linux/export.h>
1da177e4 15#include <linux/syscalls.h>
e28cc715 16#include <linux/pagemap.h>
d6b29d7c 17#include <linux/splice.h>
561c6731 18#include <linux/compat.h>
29732938 19#include <linux/mount.h>
06ae43f3 20#include "internal.h"
1da177e4
LT
21
22#include <asm/uaccess.h>
23#include <asm/unistd.h>
24
c0bd14af 25typedef ssize_t (*io_fn_t)(struct file *, char __user *, size_t, loff_t *);
293bc982 26typedef ssize_t (*iter_fn_t)(struct kiocb *, struct iov_iter *);
c0bd14af 27
4b6f5d20 28const struct file_operations generic_ro_fops = {
1da177e4 29 .llseek = generic_file_llseek,
aad4f8bb 30 .read_iter = generic_file_read_iter,
1da177e4 31 .mmap = generic_file_readonly_mmap,
534f2aaa 32 .splice_read = generic_file_splice_read,
1da177e4
LT
33};
34
35EXPORT_SYMBOL(generic_ro_fops);
36
cccb5a1e 37static inline int unsigned_offsets(struct file *file)
4a3956c7 38{
cccb5a1e 39 return file->f_mode & FMODE_UNSIGNED_OFFSET;
4a3956c7
KH
40}
41
46a1c2c7
JL
42/**
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)
ef3d0fd2
AK
55{
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}
46a1c2c7 67EXPORT_SYMBOL(vfs_setpos);
ef3d0fd2 68
3a8cff4f 69/**
5760495a 70 * generic_file_llseek_size - generic llseek implementation for regular files
3a8cff4f
CH
71 * @file: file structure to seek on
72 * @offset: file offset to seek to
965c8e59 73 * @whence: type of seek
e8b96eb5
ES
74 * @size: max size of this file in file system
75 * @eof: offset used for SEEK_END position
3a8cff4f 76 *
5760495a 77 * This is a variant of generic_file_llseek that allows passing in a custom
e8b96eb5 78 * maximum file size and a custom EOF position, for e.g. hashed directories
ef3d0fd2
AK
79 *
80 * Synchronization:
5760495a 81 * SEEK_SET and SEEK_END are unsynchronized (but atomic on 64bit platforms)
ef3d0fd2
AK
82 * SEEK_CUR is synchronized against other SEEK_CURs, but not read/writes.
83 * read/writes behave like SEEK_SET against seeks.
3a8cff4f 84 */
9465efc9 85loff_t
965c8e59 86generic_file_llseek_size(struct file *file, loff_t offset, int whence,
e8b96eb5 87 loff_t maxsize, loff_t eof)
1da177e4 88{
965c8e59 89 switch (whence) {
3a8cff4f 90 case SEEK_END:
e8b96eb5 91 offset += eof;
3a8cff4f
CH
92 break;
93 case SEEK_CUR:
5b6f1eb9
AK
94 /*
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;
ef3d0fd2
AK
102 /*
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);
46a1c2c7 108 offset = vfs_setpos(file, file->f_pos + offset, maxsize);
ef3d0fd2
AK
109 spin_unlock(&file->f_lock);
110 return offset;
982d8165
JB
111 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 */
e8b96eb5 116 if (offset >= eof)
982d8165
JB
117 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 */
e8b96eb5 124 if (offset >= eof)
982d8165 125 return -ENXIO;
e8b96eb5 126 offset = eof;
982d8165 127 break;
1da177e4 128 }
3a8cff4f 129
46a1c2c7 130 return vfs_setpos(file, offset, maxsize);
5760495a
AK
131}
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
965c8e59 138 * @whence: type of seek
5760495a
AK
139 *
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
546ae2d2 142 * @offset and @whence.
5760495a 143 */
965c8e59 144loff_t generic_file_llseek(struct file *file, loff_t offset, int whence)
5760495a
AK
145{
146 struct inode *inode = file->f_mapping->host;
147
965c8e59 148 return generic_file_llseek_size(file, offset, whence,
e8b96eb5
ES
149 inode->i_sb->s_maxbytes,
150 i_size_read(inode));
1da177e4 151}
9465efc9 152EXPORT_SYMBOL(generic_file_llseek);
1da177e4 153
1bf9d14d
AV
154/**
155 * 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
b25472f9
AV
174/**
175 * no_seek_end_llseek - llseek implementation for fixed-sized devices
176 * @file: file structure to seek on
177 * @offset: file offset to seek to
178 * @whence: type of seek
179 *
180 */
181loff_t no_seek_end_llseek(struct file *file, loff_t offset, int whence)
182{
183 switch (whence) {
184 case SEEK_SET: case SEEK_CUR:
185 return generic_file_llseek_size(file, offset, whence,
186 ~0ULL, 0);
187 default:
188 return -EINVAL;
189 }
190}
191EXPORT_SYMBOL(no_seek_end_llseek);
192
193/**
194 * no_seek_end_llseek_size - llseek implementation for fixed-sized devices
195 * @file: file structure to seek on
196 * @offset: file offset to seek to
197 * @whence: type of seek
198 * @size: maximal offset allowed
199 *
200 */
201loff_t no_seek_end_llseek_size(struct file *file, loff_t offset, int whence, loff_t size)
202{
203 switch (whence) {
204 case SEEK_SET: case SEEK_CUR:
205 return generic_file_llseek_size(file, offset, whence,
206 size, 0);
207 default:
208 return -EINVAL;
209 }
210}
211EXPORT_SYMBOL(no_seek_end_llseek_size);
212
ae6afc3f
B
213/**
214 * noop_llseek - No Operation Performed llseek implementation
215 * @file: file structure to seek on
216 * @offset: file offset to seek to
965c8e59 217 * @whence: type of seek
ae6afc3f
B
218 *
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 */
965c8e59 224loff_t noop_llseek(struct file *file, loff_t offset, int whence)
ae6afc3f
B
225{
226 return file->f_pos;
227}
228EXPORT_SYMBOL(noop_llseek);
229
965c8e59 230loff_t no_llseek(struct file *file, loff_t offset, int whence)
1da177e4
LT
231{
232 return -ESPIPE;
233}
234EXPORT_SYMBOL(no_llseek);
235
965c8e59 236loff_t default_llseek(struct file *file, loff_t offset, int whence)
1da177e4 237{
496ad9aa 238 struct inode *inode = file_inode(file);
16abef0e 239 loff_t retval;
1da177e4 240
5955102c 241 inode_lock(inode);
965c8e59 242 switch (whence) {
7b8e8924 243 case SEEK_END:
982d8165 244 offset += i_size_read(inode);
1da177e4 245 break;
7b8e8924 246 case SEEK_CUR:
5b6f1eb9
AK
247 if (offset == 0) {
248 retval = file->f_pos;
249 goto out;
250 }
1da177e4 251 offset += file->f_pos;
982d8165
JB
252 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 */
bacb2d81
DC
259 if (offset >= inode->i_size) {
260 retval = -ENXIO;
261 goto out;
262 }
982d8165
JB
263 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 */
bacb2d81
DC
270 if (offset >= inode->i_size) {
271 retval = -ENXIO;
272 goto out;
273 }
982d8165
JB
274 offset = inode->i_size;
275 break;
1da177e4
LT
276 }
277 retval = -EINVAL;
cccb5a1e 278 if (offset >= 0 || unsigned_offsets(file)) {
1da177e4
LT
279 if (offset != file->f_pos) {
280 file->f_pos = offset;
281 file->f_version = 0;
282 }
283 retval = offset;
284 }
5b6f1eb9 285out:
5955102c 286 inode_unlock(inode);
1da177e4
LT
287 return retval;
288}
289EXPORT_SYMBOL(default_llseek);
290
965c8e59 291loff_t vfs_llseek(struct file *file, loff_t offset, int whence)
1da177e4
LT
292{
293 loff_t (*fn)(struct file *, loff_t, int);
294
295 fn = no_llseek;
296 if (file->f_mode & FMODE_LSEEK) {
72c2d531 297 if (file->f_op->llseek)
1da177e4
LT
298 fn = file->f_op->llseek;
299 }
965c8e59 300 return fn(file, offset, whence);
1da177e4
LT
301}
302EXPORT_SYMBOL(vfs_llseek);
303
9c225f26
LT
304static inline struct fd fdget_pos(int fd)
305{
bd2a31d5 306 return __to_fd(__fdget_pos(fd));
9c225f26
LT
307}
308
309static inline void fdput_pos(struct fd f)
310{
311 if (f.flags & FDPUT_POS_UNLOCK)
312 mutex_unlock(&f.file->f_pos_lock);
313 fdput(f);
314}
315
965c8e59 316SYSCALL_DEFINE3(lseek, unsigned int, fd, off_t, offset, unsigned int, whence)
1da177e4
LT
317{
318 off_t retval;
9c225f26 319 struct fd f = fdget_pos(fd);
2903ff01
AV
320 if (!f.file)
321 return -EBADF;
1da177e4
LT
322
323 retval = -EINVAL;
965c8e59
AM
324 if (whence <= SEEK_MAX) {
325 loff_t res = vfs_llseek(f.file, offset, whence);
1da177e4
LT
326 retval = res;
327 if (res != (loff_t)retval)
328 retval = -EOVERFLOW; /* LFS: should only happen on 32 bit platforms */
329 }
9c225f26 330 fdput_pos(f);
1da177e4
LT
331 return retval;
332}
333
561c6731
AV
334#ifdef CONFIG_COMPAT
335COMPAT_SYSCALL_DEFINE3(lseek, unsigned int, fd, compat_off_t, offset, unsigned int, whence)
336{
337 return sys_lseek(fd, offset, whence);
338}
339#endif
340
1da177e4 341#ifdef __ARCH_WANT_SYS_LLSEEK
003d7ab4
HC
342SYSCALL_DEFINE5(llseek, unsigned int, fd, unsigned long, offset_high,
343 unsigned long, offset_low, loff_t __user *, result,
965c8e59 344 unsigned int, whence)
1da177e4
LT
345{
346 int retval;
d7a15f8d 347 struct fd f = fdget_pos(fd);
1da177e4 348 loff_t offset;
1da177e4 349
2903ff01
AV
350 if (!f.file)
351 return -EBADF;
1da177e4
LT
352
353 retval = -EINVAL;
965c8e59 354 if (whence > SEEK_MAX)
1da177e4
LT
355 goto out_putf;
356
2903ff01 357 offset = vfs_llseek(f.file, ((loff_t) offset_high << 32) | offset_low,
965c8e59 358 whence);
1da177e4
LT
359
360 retval = (int)offset;
361 if (offset >= 0) {
362 retval = -EFAULT;
363 if (!copy_to_user(result, &offset, sizeof(offset)))
364 retval = 0;
365 }
366out_putf:
d7a15f8d 367 fdput_pos(f);
1da177e4
LT
368 return retval;
369}
370#endif
371
dbe4e192
CH
372ssize_t vfs_iter_read(struct file *file, struct iov_iter *iter, loff_t *ppos)
373{
374 struct kiocb kiocb;
375 ssize_t ret;
376
377 if (!file->f_op->read_iter)
378 return -EINVAL;
379
380 init_sync_kiocb(&kiocb, file);
381 kiocb.ki_pos = *ppos;
dbe4e192
CH
382
383 iter->type |= READ;
384 ret = file->f_op->read_iter(&kiocb, iter);
599bd19b 385 BUG_ON(ret == -EIOCBQUEUED);
dbe4e192
CH
386 if (ret > 0)
387 *ppos = kiocb.ki_pos;
388 return ret;
389}
390EXPORT_SYMBOL(vfs_iter_read);
391
392ssize_t vfs_iter_write(struct file *file, struct iov_iter *iter, loff_t *ppos)
393{
394 struct kiocb kiocb;
395 ssize_t ret;
396
397 if (!file->f_op->write_iter)
398 return -EINVAL;
399
400 init_sync_kiocb(&kiocb, file);
401 kiocb.ki_pos = *ppos;
dbe4e192
CH
402
403 iter->type |= WRITE;
404 ret = file->f_op->write_iter(&kiocb, iter);
599bd19b 405 BUG_ON(ret == -EIOCBQUEUED);
dbe4e192
CH
406 if (ret > 0)
407 *ppos = kiocb.ki_pos;
408 return ret;
409}
410EXPORT_SYMBOL(vfs_iter_write);
411
e28cc715
LT
412/*
413 * rw_verify_area doesn't like huge counts. We limit
414 * them to something that fits in "int" so that others
415 * won't have to do range checks all the time.
416 */
68d70d03 417int rw_verify_area(int read_write, struct file *file, const loff_t *ppos, size_t count)
1da177e4
LT
418{
419 struct inode *inode;
420 loff_t pos;
c43e259c 421 int retval = -EINVAL;
1da177e4 422
496ad9aa 423 inode = file_inode(file);
e28cc715 424 if (unlikely((ssize_t) count < 0))
c43e259c 425 return retval;
1da177e4 426 pos = *ppos;
cccb5a1e
AV
427 if (unlikely(pos < 0)) {
428 if (!unsigned_offsets(file))
429 return retval;
430 if (count >= -pos) /* both values are in 0..LLONG_MAX */
431 return -EOVERFLOW;
432 } else if (unlikely((loff_t) (pos + count) < 0)) {
433 if (!unsigned_offsets(file))
4a3956c7
KH
434 return retval;
435 }
1da177e4 436
bd61e0a9 437 if (unlikely(inode->i_flctx && mandatory_lock(inode))) {
acc15575
CH
438 retval = locks_mandatory_area(inode, file, pos, pos + count - 1,
439 read_write == READ ? F_RDLCK : F_WRLCK);
e28cc715
LT
440 if (retval < 0)
441 return retval;
442 }
c43e259c
JM
443 retval = security_file_permission(file,
444 read_write == READ ? MAY_READ : MAY_WRITE);
445 if (retval)
446 return retval;
e28cc715 447 return count > MAX_RW_COUNT ? MAX_RW_COUNT : count;
1da177e4
LT
448}
449
5d5d5689 450static ssize_t new_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
293bc982
AV
451{
452 struct iovec iov = { .iov_base = buf, .iov_len = len };
453 struct kiocb kiocb;
454 struct iov_iter iter;
455 ssize_t ret;
456
457 init_sync_kiocb(&kiocb, filp);
458 kiocb.ki_pos = *ppos;
293bc982
AV
459 iov_iter_init(&iter, READ, &iov, 1, len);
460
461 ret = filp->f_op->read_iter(&kiocb, &iter);
599bd19b 462 BUG_ON(ret == -EIOCBQUEUED);
293bc982
AV
463 *ppos = kiocb.ki_pos;
464 return ret;
465}
466
6fb5032e
DK
467ssize_t __vfs_read(struct file *file, char __user *buf, size_t count,
468 loff_t *pos)
469{
6fb5032e 470 if (file->f_op->read)
3d04c8a1 471 return file->f_op->read(file, buf, count, pos);
6fb5032e 472 else if (file->f_op->read_iter)
3d04c8a1 473 return new_sync_read(file, buf, count, pos);
6fb5032e 474 else
3d04c8a1 475 return -EINVAL;
6fb5032e 476}
3d04c8a1 477EXPORT_SYMBOL(__vfs_read);
6fb5032e 478
1da177e4
LT
479ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
480{
481 ssize_t ret;
482
483 if (!(file->f_mode & FMODE_READ))
484 return -EBADF;
7f7f25e8 485 if (!(file->f_mode & FMODE_CAN_READ))
1da177e4
LT
486 return -EINVAL;
487 if (unlikely(!access_ok(VERIFY_WRITE, buf, count)))
488 return -EFAULT;
489
490 ret = rw_verify_area(READ, file, pos, count);
e28cc715
LT
491 if (ret >= 0) {
492 count = ret;
6fb5032e 493 ret = __vfs_read(file, buf, count, pos);
c43e259c 494 if (ret > 0) {
2a12a9d7 495 fsnotify_access(file);
c43e259c 496 add_rchar(current, ret);
1da177e4 497 }
c43e259c 498 inc_syscr(current);
1da177e4
LT
499 }
500
501 return ret;
502}
503
504EXPORT_SYMBOL(vfs_read);
505
5d5d5689 506static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
293bc982
AV
507{
508 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len };
509 struct kiocb kiocb;
510 struct iov_iter iter;
511 ssize_t ret;
512
513 init_sync_kiocb(&kiocb, filp);
514 kiocb.ki_pos = *ppos;
293bc982
AV
515 iov_iter_init(&iter, WRITE, &iov, 1, len);
516
517 ret = filp->f_op->write_iter(&kiocb, &iter);
599bd19b 518 BUG_ON(ret == -EIOCBQUEUED);
f765b134
AV
519 if (ret > 0)
520 *ppos = kiocb.ki_pos;
293bc982
AV
521 return ret;
522}
523
493c84c0
AV
524ssize_t __vfs_write(struct file *file, const char __user *p, size_t count,
525 loff_t *pos)
526{
527 if (file->f_op->write)
528 return file->f_op->write(file, p, count, pos);
493c84c0
AV
529 else if (file->f_op->write_iter)
530 return new_sync_write(file, p, count, pos);
531 else
532 return -EINVAL;
533}
534EXPORT_SYMBOL(__vfs_write);
535
06ae43f3
AV
536ssize_t __kernel_write(struct file *file, const char *buf, size_t count, loff_t *pos)
537{
538 mm_segment_t old_fs;
539 const char __user *p;
540 ssize_t ret;
541
7f7f25e8 542 if (!(file->f_mode & FMODE_CAN_WRITE))
3e84f48e
AV
543 return -EINVAL;
544
06ae43f3
AV
545 old_fs = get_fs();
546 set_fs(get_ds());
547 p = (__force const char __user *)buf;
548 if (count > MAX_RW_COUNT)
549 count = MAX_RW_COUNT;
493c84c0 550 ret = __vfs_write(file, p, count, pos);
06ae43f3
AV
551 set_fs(old_fs);
552 if (ret > 0) {
553 fsnotify_modify(file);
554 add_wchar(current, ret);
555 }
556 inc_syscw(current);
557 return ret;
558}
559
2ec3a12a
AV
560EXPORT_SYMBOL(__kernel_write);
561
1da177e4
LT
562ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
563{
564 ssize_t ret;
565
566 if (!(file->f_mode & FMODE_WRITE))
567 return -EBADF;
7f7f25e8 568 if (!(file->f_mode & FMODE_CAN_WRITE))
1da177e4
LT
569 return -EINVAL;
570 if (unlikely(!access_ok(VERIFY_READ, buf, count)))
571 return -EFAULT;
572
573 ret = rw_verify_area(WRITE, file, pos, count);
e28cc715
LT
574 if (ret >= 0) {
575 count = ret;
03d95eb2 576 file_start_write(file);
493c84c0 577 ret = __vfs_write(file, buf, count, pos);
c43e259c 578 if (ret > 0) {
2a12a9d7 579 fsnotify_modify(file);
c43e259c 580 add_wchar(current, ret);
1da177e4 581 }
c43e259c 582 inc_syscw(current);
03d95eb2 583 file_end_write(file);
1da177e4
LT
584 }
585
586 return ret;
587}
588
589EXPORT_SYMBOL(vfs_write);
590
591static inline loff_t file_pos_read(struct file *file)
592{
593 return file->f_pos;
594}
595
596static inline void file_pos_write(struct file *file, loff_t pos)
597{
598 file->f_pos = pos;
599}
600
3cdad428 601SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count)
1da177e4 602{
9c225f26 603 struct fd f = fdget_pos(fd);
1da177e4 604 ssize_t ret = -EBADF;
1da177e4 605
2903ff01
AV
606 if (f.file) {
607 loff_t pos = file_pos_read(f.file);
608 ret = vfs_read(f.file, buf, count, &pos);
5faf153e
AV
609 if (ret >= 0)
610 file_pos_write(f.file, pos);
9c225f26 611 fdput_pos(f);
1da177e4 612 }
1da177e4
LT
613 return ret;
614}
1da177e4 615
3cdad428
HC
616SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf,
617 size_t, count)
1da177e4 618{
9c225f26 619 struct fd f = fdget_pos(fd);
1da177e4 620 ssize_t ret = -EBADF;
1da177e4 621
2903ff01
AV
622 if (f.file) {
623 loff_t pos = file_pos_read(f.file);
624 ret = vfs_write(f.file, buf, count, &pos);
5faf153e
AV
625 if (ret >= 0)
626 file_pos_write(f.file, pos);
9c225f26 627 fdput_pos(f);
1da177e4
LT
628 }
629
630 return ret;
631}
632
4a0fd5bf
AV
633SYSCALL_DEFINE4(pread64, unsigned int, fd, char __user *, buf,
634 size_t, count, loff_t, pos)
1da177e4 635{
2903ff01 636 struct fd f;
1da177e4 637 ssize_t ret = -EBADF;
1da177e4
LT
638
639 if (pos < 0)
640 return -EINVAL;
641
2903ff01
AV
642 f = fdget(fd);
643 if (f.file) {
1da177e4 644 ret = -ESPIPE;
2903ff01
AV
645 if (f.file->f_mode & FMODE_PREAD)
646 ret = vfs_read(f.file, buf, count, &pos);
647 fdput(f);
1da177e4
LT
648 }
649
650 return ret;
651}
652
4a0fd5bf
AV
653SYSCALL_DEFINE4(pwrite64, unsigned int, fd, const char __user *, buf,
654 size_t, count, loff_t, pos)
1da177e4 655{
2903ff01 656 struct fd f;
1da177e4 657 ssize_t ret = -EBADF;
1da177e4
LT
658
659 if (pos < 0)
660 return -EINVAL;
661
2903ff01
AV
662 f = fdget(fd);
663 if (f.file) {
1da177e4 664 ret = -ESPIPE;
2903ff01
AV
665 if (f.file->f_mode & FMODE_PWRITE)
666 ret = vfs_write(f.file, buf, count, &pos);
667 fdput(f);
1da177e4
LT
668 }
669
670 return ret;
671}
672
673/*
674 * Reduce an iovec's length in-place. Return the resulting number of segments
675 */
676unsigned long iov_shorten(struct iovec *iov, unsigned long nr_segs, size_t to)
677{
678 unsigned long seg = 0;
679 size_t len = 0;
680
681 while (seg < nr_segs) {
682 seg++;
683 if (len + iov->iov_len >= to) {
684 iov->iov_len = to - len;
685 break;
686 }
687 len += iov->iov_len;
688 iov++;
689 }
690 return seg;
691}
19295529 692EXPORT_SYMBOL(iov_shorten);
1da177e4 693
ac15ac06 694static ssize_t do_iter_readv_writev(struct file *filp, struct iov_iter *iter,
793b80ef 695 loff_t *ppos, iter_fn_t fn, int flags)
293bc982
AV
696{
697 struct kiocb kiocb;
293bc982
AV
698 ssize_t ret;
699
793b80ef
CH
700 if (flags)
701 return -EOPNOTSUPP;
702
293bc982
AV
703 init_sync_kiocb(&kiocb, filp);
704 kiocb.ki_pos = *ppos;
293bc982 705
ac15ac06 706 ret = fn(&kiocb, iter);
599bd19b 707 BUG_ON(ret == -EIOCBQUEUED);
293bc982
AV
708 *ppos = kiocb.ki_pos;
709 return ret;
710}
711
ee0b3e67 712/* Do it by hand, with file-ops */
ac15ac06 713static ssize_t do_loop_readv_writev(struct file *filp, struct iov_iter *iter,
793b80ef 714 loff_t *ppos, io_fn_t fn, int flags)
ee0b3e67 715{
ee0b3e67
BP
716 ssize_t ret = 0;
717
793b80ef
CH
718 if (flags)
719 return -EOPNOTSUPP;
720
ac15ac06
AV
721 while (iov_iter_count(iter)) {
722 struct iovec iovec = iov_iter_iovec(iter);
ee0b3e67
BP
723 ssize_t nr;
724
ac15ac06 725 nr = fn(filp, iovec.iov_base, iovec.iov_len, ppos);
ee0b3e67
BP
726
727 if (nr < 0) {
728 if (!ret)
729 ret = nr;
730 break;
731 }
732 ret += nr;
ac15ac06 733 if (nr != iovec.iov_len)
ee0b3e67 734 break;
ac15ac06 735 iov_iter_advance(iter, nr);
ee0b3e67
BP
736 }
737
738 return ret;
739}
740
1da177e4
LT
741/* A write operation does a read from user space and vice versa */
742#define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
743
eed4e51f
BP
744ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
745 unsigned long nr_segs, unsigned long fast_segs,
746 struct iovec *fast_pointer,
ac34ebb3 747 struct iovec **ret_pointer)
435f49a5 748{
eed4e51f 749 unsigned long seg;
435f49a5 750 ssize_t ret;
eed4e51f
BP
751 struct iovec *iov = fast_pointer;
752
435f49a5
LT
753 /*
754 * SuS says "The readv() function *may* fail if the iovcnt argument
755 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
756 * traditionally returned zero for zero segments, so...
757 */
eed4e51f
BP
758 if (nr_segs == 0) {
759 ret = 0;
435f49a5 760 goto out;
eed4e51f
BP
761 }
762
435f49a5
LT
763 /*
764 * First get the "struct iovec" from user memory and
765 * verify all the pointers
766 */
eed4e51f
BP
767 if (nr_segs > UIO_MAXIOV) {
768 ret = -EINVAL;
435f49a5 769 goto out;
eed4e51f
BP
770 }
771 if (nr_segs > fast_segs) {
435f49a5 772 iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
eed4e51f
BP
773 if (iov == NULL) {
774 ret = -ENOMEM;
435f49a5 775 goto out;
eed4e51f 776 }
435f49a5 777 }
eed4e51f
BP
778 if (copy_from_user(iov, uvector, nr_segs*sizeof(*uvector))) {
779 ret = -EFAULT;
435f49a5 780 goto out;
eed4e51f
BP
781 }
782
435f49a5 783 /*
eed4e51f
BP
784 * According to the Single Unix Specification we should return EINVAL
785 * if an element length is < 0 when cast to ssize_t or if the
786 * total length would overflow the ssize_t return value of the
787 * system call.
435f49a5
LT
788 *
789 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
790 * overflow case.
791 */
eed4e51f 792 ret = 0;
435f49a5
LT
793 for (seg = 0; seg < nr_segs; seg++) {
794 void __user *buf = iov[seg].iov_base;
795 ssize_t len = (ssize_t)iov[seg].iov_len;
eed4e51f
BP
796
797 /* see if we we're about to use an invalid len or if
798 * it's about to overflow ssize_t */
435f49a5 799 if (len < 0) {
eed4e51f 800 ret = -EINVAL;
435f49a5 801 goto out;
eed4e51f 802 }
ac34ebb3 803 if (type >= 0
fcf63409 804 && unlikely(!access_ok(vrfy_dir(type), buf, len))) {
eed4e51f 805 ret = -EFAULT;
435f49a5
LT
806 goto out;
807 }
808 if (len > MAX_RW_COUNT - ret) {
809 len = MAX_RW_COUNT - ret;
810 iov[seg].iov_len = len;
eed4e51f 811 }
eed4e51f 812 ret += len;
435f49a5 813 }
eed4e51f
BP
814out:
815 *ret_pointer = iov;
816 return ret;
817}
818
1da177e4
LT
819static ssize_t do_readv_writev(int type, struct file *file,
820 const struct iovec __user * uvector,
793b80ef
CH
821 unsigned long nr_segs, loff_t *pos,
822 int flags)
1da177e4 823{
1da177e4
LT
824 size_t tot_len;
825 struct iovec iovstack[UIO_FASTIOV];
ee0b3e67 826 struct iovec *iov = iovstack;
ac15ac06 827 struct iov_iter iter;
1da177e4 828 ssize_t ret;
1da177e4 829 io_fn_t fn;
293bc982 830 iter_fn_t iter_fn;
1da177e4 831
0504c074
AV
832 ret = import_iovec(type, uvector, nr_segs,
833 ARRAY_SIZE(iovstack), &iov, &iter);
834 if (ret < 0)
835 return ret;
1da177e4 836
0504c074
AV
837 tot_len = iov_iter_count(&iter);
838 if (!tot_len)
839 goto out;
1da177e4 840 ret = rw_verify_area(type, file, pos, tot_len);
e28cc715 841 if (ret < 0)
411b67b4 842 goto out;
1da177e4 843
1da177e4
LT
844 if (type == READ) {
845 fn = file->f_op->read;
293bc982 846 iter_fn = file->f_op->read_iter;
1da177e4
LT
847 } else {
848 fn = (io_fn_t)file->f_op->write;
293bc982 849 iter_fn = file->f_op->write_iter;
03d95eb2 850 file_start_write(file);
1da177e4
LT
851 }
852
293bc982 853 if (iter_fn)
793b80ef 854 ret = do_iter_readv_writev(file, &iter, pos, iter_fn, flags);
ee0b3e67 855 else
793b80ef 856 ret = do_loop_readv_writev(file, &iter, pos, fn, flags);
1da177e4 857
03d95eb2
AV
858 if (type != READ)
859 file_end_write(file);
860
1da177e4 861out:
0504c074 862 kfree(iov);
0eeca283
RL
863 if ((ret + (type == READ)) > 0) {
864 if (type == READ)
2a12a9d7 865 fsnotify_access(file);
0eeca283 866 else
2a12a9d7 867 fsnotify_modify(file);
0eeca283 868 }
1da177e4 869 return ret;
1da177e4
LT
870}
871
872ssize_t vfs_readv(struct file *file, const struct iovec __user *vec,
793b80ef 873 unsigned long vlen, loff_t *pos, int flags)
1da177e4
LT
874{
875 if (!(file->f_mode & FMODE_READ))
876 return -EBADF;
7f7f25e8 877 if (!(file->f_mode & FMODE_CAN_READ))
1da177e4
LT
878 return -EINVAL;
879
793b80ef 880 return do_readv_writev(READ, file, vec, vlen, pos, flags);
1da177e4
LT
881}
882
883EXPORT_SYMBOL(vfs_readv);
884
885ssize_t vfs_writev(struct file *file, const struct iovec __user *vec,
793b80ef 886 unsigned long vlen, loff_t *pos, int flags)
1da177e4
LT
887{
888 if (!(file->f_mode & FMODE_WRITE))
889 return -EBADF;
7f7f25e8 890 if (!(file->f_mode & FMODE_CAN_WRITE))
1da177e4
LT
891 return -EINVAL;
892
793b80ef 893 return do_readv_writev(WRITE, file, vec, vlen, pos, flags);
1da177e4
LT
894}
895
896EXPORT_SYMBOL(vfs_writev);
897
f17d8b35
MT
898static ssize_t do_readv(unsigned long fd, const struct iovec __user *vec,
899 unsigned long vlen, int flags)
1da177e4 900{
9c225f26 901 struct fd f = fdget_pos(fd);
1da177e4 902 ssize_t ret = -EBADF;
1da177e4 903
2903ff01
AV
904 if (f.file) {
905 loff_t pos = file_pos_read(f.file);
f17d8b35 906 ret = vfs_readv(f.file, vec, vlen, &pos, flags);
5faf153e
AV
907 if (ret >= 0)
908 file_pos_write(f.file, pos);
9c225f26 909 fdput_pos(f);
1da177e4
LT
910 }
911
912 if (ret > 0)
4b98d11b
AD
913 add_rchar(current, ret);
914 inc_syscr(current);
1da177e4
LT
915 return ret;
916}
917
f17d8b35
MT
918static ssize_t do_writev(unsigned long fd, const struct iovec __user *vec,
919 unsigned long vlen, int flags)
1da177e4 920{
9c225f26 921 struct fd f = fdget_pos(fd);
1da177e4 922 ssize_t ret = -EBADF;
1da177e4 923
2903ff01
AV
924 if (f.file) {
925 loff_t pos = file_pos_read(f.file);
f17d8b35 926 ret = vfs_writev(f.file, vec, vlen, &pos, flags);
5faf153e
AV
927 if (ret >= 0)
928 file_pos_write(f.file, pos);
9c225f26 929 fdput_pos(f);
1da177e4
LT
930 }
931
932 if (ret > 0)
4b98d11b
AD
933 add_wchar(current, ret);
934 inc_syscw(current);
1da177e4
LT
935 return ret;
936}
937
601cc11d
LT
938static inline loff_t pos_from_hilo(unsigned long high, unsigned long low)
939{
940#define HALF_LONG_BITS (BITS_PER_LONG / 2)
941 return (((loff_t)high << HALF_LONG_BITS) << HALF_LONG_BITS) | low;
942}
943
f17d8b35
MT
944static ssize_t do_preadv(unsigned long fd, const struct iovec __user *vec,
945 unsigned long vlen, loff_t pos, int flags)
f3554f4b 946{
2903ff01 947 struct fd f;
f3554f4b 948 ssize_t ret = -EBADF;
f3554f4b
GH
949
950 if (pos < 0)
951 return -EINVAL;
952
2903ff01
AV
953 f = fdget(fd);
954 if (f.file) {
f3554f4b 955 ret = -ESPIPE;
2903ff01 956 if (f.file->f_mode & FMODE_PREAD)
f17d8b35 957 ret = vfs_readv(f.file, vec, vlen, &pos, flags);
2903ff01 958 fdput(f);
f3554f4b
GH
959 }
960
961 if (ret > 0)
962 add_rchar(current, ret);
963 inc_syscr(current);
964 return ret;
965}
966
f17d8b35
MT
967static ssize_t do_pwritev(unsigned long fd, const struct iovec __user *vec,
968 unsigned long vlen, loff_t pos, int flags)
f3554f4b 969{
2903ff01 970 struct fd f;
f3554f4b 971 ssize_t ret = -EBADF;
f3554f4b
GH
972
973 if (pos < 0)
974 return -EINVAL;
975
2903ff01
AV
976 f = fdget(fd);
977 if (f.file) {
f3554f4b 978 ret = -ESPIPE;
2903ff01 979 if (f.file->f_mode & FMODE_PWRITE)
f17d8b35 980 ret = vfs_writev(f.file, vec, vlen, &pos, flags);
2903ff01 981 fdput(f);
f3554f4b
GH
982 }
983
984 if (ret > 0)
985 add_wchar(current, ret);
986 inc_syscw(current);
987 return ret;
988}
989
f17d8b35
MT
990SYSCALL_DEFINE3(readv, unsigned long, fd, const struct iovec __user *, vec,
991 unsigned long, vlen)
992{
993 return do_readv(fd, vec, vlen, 0);
994}
995
996SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec __user *, vec,
997 unsigned long, vlen)
998{
999 return do_writev(fd, vec, vlen, 0);
1000}
1001
1002SYSCALL_DEFINE5(preadv, unsigned long, fd, const struct iovec __user *, vec,
1003 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
1004{
1005 loff_t pos = pos_from_hilo(pos_h, pos_l);
1006
1007 return do_preadv(fd, vec, vlen, pos, 0);
1008}
1009
1010SYSCALL_DEFINE6(preadv2, unsigned long, fd, const struct iovec __user *, vec,
1011 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h,
1012 int, flags)
1013{
1014 loff_t pos = pos_from_hilo(pos_h, pos_l);
1015
1016 if (pos == -1)
1017 return do_readv(fd, vec, vlen, flags);
1018
1019 return do_preadv(fd, vec, vlen, pos, flags);
1020}
1021
1022SYSCALL_DEFINE5(pwritev, unsigned long, fd, const struct iovec __user *, vec,
1023 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
1024{
1025 loff_t pos = pos_from_hilo(pos_h, pos_l);
1026
1027 return do_pwritev(fd, vec, vlen, pos, 0);
1028}
1029
1030SYSCALL_DEFINE6(pwritev2, unsigned long, fd, const struct iovec __user *, vec,
1031 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h,
1032 int, flags)
1033{
1034 loff_t pos = pos_from_hilo(pos_h, pos_l);
1035
1036 if (pos == -1)
1037 return do_writev(fd, vec, vlen, flags);
1038
1039 return do_pwritev(fd, vec, vlen, pos, flags);
1040}
1041
72ec3516
AV
1042#ifdef CONFIG_COMPAT
1043
1044static ssize_t compat_do_readv_writev(int type, struct file *file,
1045 const struct compat_iovec __user *uvector,
793b80ef
CH
1046 unsigned long nr_segs, loff_t *pos,
1047 int flags)
72ec3516
AV
1048{
1049 compat_ssize_t tot_len;
1050 struct iovec iovstack[UIO_FASTIOV];
1051 struct iovec *iov = iovstack;
ac15ac06 1052 struct iov_iter iter;
72ec3516
AV
1053 ssize_t ret;
1054 io_fn_t fn;
293bc982 1055 iter_fn_t iter_fn;
72ec3516 1056
0504c074
AV
1057 ret = compat_import_iovec(type, uvector, nr_segs,
1058 UIO_FASTIOV, &iov, &iter);
1059 if (ret < 0)
1060 return ret;
72ec3516 1061
0504c074
AV
1062 tot_len = iov_iter_count(&iter);
1063 if (!tot_len)
1064 goto out;
72ec3516
AV
1065 ret = rw_verify_area(type, file, pos, tot_len);
1066 if (ret < 0)
1067 goto out;
1068
72ec3516
AV
1069 if (type == READ) {
1070 fn = file->f_op->read;
293bc982 1071 iter_fn = file->f_op->read_iter;
72ec3516
AV
1072 } else {
1073 fn = (io_fn_t)file->f_op->write;
293bc982 1074 iter_fn = file->f_op->write_iter;
03d95eb2 1075 file_start_write(file);
72ec3516
AV
1076 }
1077
293bc982 1078 if (iter_fn)
793b80ef 1079 ret = do_iter_readv_writev(file, &iter, pos, iter_fn, flags);
03d95eb2 1080 else
793b80ef 1081 ret = do_loop_readv_writev(file, &iter, pos, fn, flags);
72ec3516 1082
03d95eb2
AV
1083 if (type != READ)
1084 file_end_write(file);
1085
72ec3516 1086out:
0504c074 1087 kfree(iov);
72ec3516
AV
1088 if ((ret + (type == READ)) > 0) {
1089 if (type == READ)
1090 fsnotify_access(file);
1091 else
1092 fsnotify_modify(file);
1093 }
1094 return ret;
1095}
1096
1097static size_t compat_readv(struct file *file,
1098 const struct compat_iovec __user *vec,
f17d8b35 1099 unsigned long vlen, loff_t *pos, int flags)
72ec3516
AV
1100{
1101 ssize_t ret = -EBADF;
1102
1103 if (!(file->f_mode & FMODE_READ))
1104 goto out;
1105
1106 ret = -EINVAL;
7f7f25e8 1107 if (!(file->f_mode & FMODE_CAN_READ))
72ec3516
AV
1108 goto out;
1109
f17d8b35 1110 ret = compat_do_readv_writev(READ, file, vec, vlen, pos, flags);
72ec3516
AV
1111
1112out:
1113 if (ret > 0)
1114 add_rchar(current, ret);
1115 inc_syscr(current);
1116 return ret;
1117}
1118
f17d8b35
MT
1119static size_t do_compat_readv(compat_ulong_t fd,
1120 const struct compat_iovec __user *vec,
1121 compat_ulong_t vlen, int flags)
72ec3516 1122{
9c225f26 1123 struct fd f = fdget_pos(fd);
72ec3516
AV
1124 ssize_t ret;
1125 loff_t pos;
1126
1127 if (!f.file)
1128 return -EBADF;
1129 pos = f.file->f_pos;
f17d8b35 1130 ret = compat_readv(f.file, vec, vlen, &pos, flags);
5faf153e
AV
1131 if (ret >= 0)
1132 f.file->f_pos = pos;
9c225f26 1133 fdput_pos(f);
72ec3516 1134 return ret;
f17d8b35 1135
72ec3516
AV
1136}
1137
f17d8b35
MT
1138COMPAT_SYSCALL_DEFINE3(readv, compat_ulong_t, fd,
1139 const struct compat_iovec __user *,vec,
1140 compat_ulong_t, vlen)
1141{
1142 return do_compat_readv(fd, vec, vlen, 0);
1143}
1144
1145static long do_compat_preadv64(unsigned long fd,
378a10f3 1146 const struct compat_iovec __user *vec,
f17d8b35 1147 unsigned long vlen, loff_t pos, int flags)
72ec3516
AV
1148{
1149 struct fd f;
1150 ssize_t ret;
1151
1152 if (pos < 0)
1153 return -EINVAL;
1154 f = fdget(fd);
1155 if (!f.file)
1156 return -EBADF;
1157 ret = -ESPIPE;
1158 if (f.file->f_mode & FMODE_PREAD)
f17d8b35 1159 ret = compat_readv(f.file, vec, vlen, &pos, flags);
72ec3516
AV
1160 fdput(f);
1161 return ret;
1162}
1163
378a10f3
HC
1164#ifdef __ARCH_WANT_COMPAT_SYS_PREADV64
1165COMPAT_SYSCALL_DEFINE4(preadv64, unsigned long, fd,
1166 const struct compat_iovec __user *,vec,
1167 unsigned long, vlen, loff_t, pos)
1168{
f17d8b35 1169 return do_compat_preadv64(fd, vec, vlen, pos, 0);
378a10f3
HC
1170}
1171#endif
1172
dfd948e3 1173COMPAT_SYSCALL_DEFINE5(preadv, compat_ulong_t, fd,
72ec3516 1174 const struct compat_iovec __user *,vec,
dfd948e3 1175 compat_ulong_t, vlen, u32, pos_low, u32, pos_high)
72ec3516
AV
1176{
1177 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
378a10f3 1178
f17d8b35
MT
1179 return do_compat_preadv64(fd, vec, vlen, pos, 0);
1180}
1181
1182COMPAT_SYSCALL_DEFINE6(preadv2, compat_ulong_t, fd,
1183 const struct compat_iovec __user *,vec,
1184 compat_ulong_t, vlen, u32, pos_low, u32, pos_high,
1185 int, flags)
1186{
1187 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
1188
1189 if (pos == -1)
1190 return do_compat_readv(fd, vec, vlen, flags);
1191
1192 return do_compat_preadv64(fd, vec, vlen, pos, flags);
72ec3516
AV
1193}
1194
1195static size_t compat_writev(struct file *file,
1196 const struct compat_iovec __user *vec,
f17d8b35 1197 unsigned long vlen, loff_t *pos, int flags)
72ec3516
AV
1198{
1199 ssize_t ret = -EBADF;
1200
1201 if (!(file->f_mode & FMODE_WRITE))
1202 goto out;
1203
1204 ret = -EINVAL;
7f7f25e8 1205 if (!(file->f_mode & FMODE_CAN_WRITE))
72ec3516
AV
1206 goto out;
1207
793b80ef 1208 ret = compat_do_readv_writev(WRITE, file, vec, vlen, pos, 0);
72ec3516
AV
1209
1210out:
1211 if (ret > 0)
1212 add_wchar(current, ret);
1213 inc_syscw(current);
1214 return ret;
1215}
1216
f17d8b35
MT
1217static size_t do_compat_writev(compat_ulong_t fd,
1218 const struct compat_iovec __user* vec,
1219 compat_ulong_t vlen, int flags)
72ec3516 1220{
9c225f26 1221 struct fd f = fdget_pos(fd);
72ec3516
AV
1222 ssize_t ret;
1223 loff_t pos;
1224
1225 if (!f.file)
1226 return -EBADF;
1227 pos = f.file->f_pos;
f17d8b35 1228 ret = compat_writev(f.file, vec, vlen, &pos, flags);
5faf153e
AV
1229 if (ret >= 0)
1230 f.file->f_pos = pos;
9c225f26 1231 fdput_pos(f);
72ec3516
AV
1232 return ret;
1233}
1234
f17d8b35
MT
1235COMPAT_SYSCALL_DEFINE3(writev, compat_ulong_t, fd,
1236 const struct compat_iovec __user *, vec,
1237 compat_ulong_t, vlen)
1238{
1239 return do_compat_writev(fd, vec, vlen, 0);
1240}
1241
1242static long do_compat_pwritev64(unsigned long fd,
378a10f3 1243 const struct compat_iovec __user *vec,
f17d8b35 1244 unsigned long vlen, loff_t pos, int flags)
72ec3516
AV
1245{
1246 struct fd f;
1247 ssize_t ret;
1248
1249 if (pos < 0)
1250 return -EINVAL;
1251 f = fdget(fd);
1252 if (!f.file)
1253 return -EBADF;
1254 ret = -ESPIPE;
1255 if (f.file->f_mode & FMODE_PWRITE)
f17d8b35 1256 ret = compat_writev(f.file, vec, vlen, &pos, flags);
72ec3516
AV
1257 fdput(f);
1258 return ret;
1259}
1260
378a10f3
HC
1261#ifdef __ARCH_WANT_COMPAT_SYS_PWRITEV64
1262COMPAT_SYSCALL_DEFINE4(pwritev64, unsigned long, fd,
1263 const struct compat_iovec __user *,vec,
1264 unsigned long, vlen, loff_t, pos)
1265{
f17d8b35 1266 return do_compat_pwritev64(fd, vec, vlen, pos, 0);
378a10f3
HC
1267}
1268#endif
1269
dfd948e3 1270COMPAT_SYSCALL_DEFINE5(pwritev, compat_ulong_t, fd,
72ec3516 1271 const struct compat_iovec __user *,vec,
dfd948e3 1272 compat_ulong_t, vlen, u32, pos_low, u32, pos_high)
72ec3516
AV
1273{
1274 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
378a10f3 1275
f17d8b35
MT
1276 return do_compat_pwritev64(fd, vec, vlen, pos, 0);
1277}
1278
1279COMPAT_SYSCALL_DEFINE6(pwritev2, compat_ulong_t, fd,
1280 const struct compat_iovec __user *,vec,
1281 compat_ulong_t, vlen, u32, pos_low, u32, pos_high, int, flags)
1282{
1283 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
1284
1285 if (pos == -1)
1286 return do_compat_writev(fd, vec, vlen, flags);
1287
1288 return do_compat_pwritev64(fd, vec, vlen, pos, flags);
72ec3516 1289}
f17d8b35 1290
72ec3516
AV
1291#endif
1292
19f4fc3a
AV
1293static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,
1294 size_t count, loff_t max)
1da177e4 1295{
2903ff01
AV
1296 struct fd in, out;
1297 struct inode *in_inode, *out_inode;
1da177e4 1298 loff_t pos;
7995bd28 1299 loff_t out_pos;
1da177e4 1300 ssize_t retval;
2903ff01 1301 int fl;
1da177e4
LT
1302
1303 /*
1304 * Get input file, and verify that it is ok..
1305 */
1306 retval = -EBADF;
2903ff01
AV
1307 in = fdget(in_fd);
1308 if (!in.file)
1da177e4 1309 goto out;
2903ff01 1310 if (!(in.file->f_mode & FMODE_READ))
1da177e4 1311 goto fput_in;
1da177e4 1312 retval = -ESPIPE;
7995bd28
AV
1313 if (!ppos) {
1314 pos = in.file->f_pos;
1315 } else {
1316 pos = *ppos;
2903ff01 1317 if (!(in.file->f_mode & FMODE_PREAD))
1da177e4 1318 goto fput_in;
7995bd28
AV
1319 }
1320 retval = rw_verify_area(READ, in.file, &pos, count);
e28cc715 1321 if (retval < 0)
1da177e4 1322 goto fput_in;
e28cc715 1323 count = retval;
1da177e4 1324
1da177e4
LT
1325 /*
1326 * Get output file, and verify that it is ok..
1327 */
1328 retval = -EBADF;
2903ff01
AV
1329 out = fdget(out_fd);
1330 if (!out.file)
1da177e4 1331 goto fput_in;
2903ff01 1332 if (!(out.file->f_mode & FMODE_WRITE))
1da177e4
LT
1333 goto fput_out;
1334 retval = -EINVAL;
496ad9aa
AV
1335 in_inode = file_inode(in.file);
1336 out_inode = file_inode(out.file);
7995bd28
AV
1337 out_pos = out.file->f_pos;
1338 retval = rw_verify_area(WRITE, out.file, &out_pos, count);
e28cc715 1339 if (retval < 0)
1da177e4 1340 goto fput_out;
e28cc715 1341 count = retval;
1da177e4 1342
1da177e4
LT
1343 if (!max)
1344 max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes);
1345
1da177e4
LT
1346 if (unlikely(pos + count > max)) {
1347 retval = -EOVERFLOW;
1348 if (pos >= max)
1349 goto fput_out;
1350 count = max - pos;
1351 }
1352
d96e6e71 1353 fl = 0;
534f2aaa 1354#if 0
d96e6e71
JA
1355 /*
1356 * We need to debate whether we can enable this or not. The
1357 * man page documents EAGAIN return for the output at least,
1358 * and the application is arguably buggy if it doesn't expect
1359 * EAGAIN on a non-blocking file descriptor.
1360 */
2903ff01 1361 if (in.file->f_flags & O_NONBLOCK)
d96e6e71 1362 fl = SPLICE_F_NONBLOCK;
534f2aaa 1363#endif
50cd2c57 1364 file_start_write(out.file);
7995bd28 1365 retval = do_splice_direct(in.file, &pos, out.file, &out_pos, count, fl);
50cd2c57 1366 file_end_write(out.file);
1da177e4
LT
1367
1368 if (retval > 0) {
4b98d11b
AD
1369 add_rchar(current, retval);
1370 add_wchar(current, retval);
a68c2f12
SW
1371 fsnotify_access(in.file);
1372 fsnotify_modify(out.file);
7995bd28
AV
1373 out.file->f_pos = out_pos;
1374 if (ppos)
1375 *ppos = pos;
1376 else
1377 in.file->f_pos = pos;
1da177e4 1378 }
1da177e4 1379
4b98d11b
AD
1380 inc_syscr(current);
1381 inc_syscw(current);
7995bd28 1382 if (pos > max)
1da177e4
LT
1383 retval = -EOVERFLOW;
1384
1385fput_out:
2903ff01 1386 fdput(out);
1da177e4 1387fput_in:
2903ff01 1388 fdput(in);
1da177e4
LT
1389out:
1390 return retval;
1391}
1392
002c8976 1393SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd, off_t __user *, offset, size_t, count)
1da177e4
LT
1394{
1395 loff_t pos;
1396 off_t off;
1397 ssize_t ret;
1398
1399 if (offset) {
1400 if (unlikely(get_user(off, offset)))
1401 return -EFAULT;
1402 pos = off;
1403 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
1404 if (unlikely(put_user(pos, offset)))
1405 return -EFAULT;
1406 return ret;
1407 }
1408
1409 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1410}
1411
002c8976 1412SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd, loff_t __user *, offset, size_t, count)
1da177e4
LT
1413{
1414 loff_t pos;
1415 ssize_t ret;
1416
1417 if (offset) {
1418 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
1419 return -EFAULT;
1420 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
1421 if (unlikely(put_user(pos, offset)))
1422 return -EFAULT;
1423 return ret;
1424 }
1425
1426 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1427}
19f4fc3a
AV
1428
1429#ifdef CONFIG_COMPAT
1430COMPAT_SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd,
1431 compat_off_t __user *, offset, compat_size_t, count)
1432{
1433 loff_t pos;
1434 off_t off;
1435 ssize_t ret;
1436
1437 if (offset) {
1438 if (unlikely(get_user(off, offset)))
1439 return -EFAULT;
1440 pos = off;
1441 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
1442 if (unlikely(put_user(pos, offset)))
1443 return -EFAULT;
1444 return ret;
1445 }
1446
1447 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1448}
1449
1450COMPAT_SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd,
1451 compat_loff_t __user *, offset, compat_size_t, count)
1452{
1453 loff_t pos;
1454 ssize_t ret;
1455
1456 if (offset) {
1457 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
1458 return -EFAULT;
1459 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
1460 if (unlikely(put_user(pos, offset)))
1461 return -EFAULT;
1462 return ret;
1463 }
1464
1465 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1466}
1467#endif
29732938
ZB
1468
1469/*
1470 * copy_file_range() differs from regular file read and write in that it
1471 * specifically allows return partial success. When it does so is up to
1472 * the copy_file_range method.
1473 */
1474ssize_t vfs_copy_file_range(struct file *file_in, loff_t pos_in,
1475 struct file *file_out, loff_t pos_out,
1476 size_t len, unsigned int flags)
1477{
1478 struct inode *inode_in = file_inode(file_in);
1479 struct inode *inode_out = file_inode(file_out);
1480 ssize_t ret;
1481
1482 if (flags != 0)
1483 return -EINVAL;
1484
1485 /* copy_file_range allows full ssize_t len, ignoring MAX_RW_COUNT */
1486 ret = rw_verify_area(READ, file_in, &pos_in, len);
1487 if (ret >= 0)
1488 ret = rw_verify_area(WRITE, file_out, &pos_out, len);
1489 if (ret < 0)
1490 return ret;
1491
1492 if (!(file_in->f_mode & FMODE_READ) ||
1493 !(file_out->f_mode & FMODE_WRITE) ||
eac70053 1494 (file_out->f_flags & O_APPEND))
29732938
ZB
1495 return -EBADF;
1496
1497 /* this could be relaxed once a method supports cross-fs copies */
1498 if (inode_in->i_sb != inode_out->i_sb)
1499 return -EXDEV;
1500
1501 if (len == 0)
1502 return 0;
1503
1504 ret = mnt_want_write_file(file_out);
1505 if (ret)
1506 return ret;
1507
eac70053
AS
1508 ret = -EOPNOTSUPP;
1509 if (file_out->f_op->copy_file_range)
1510 ret = file_out->f_op->copy_file_range(file_in, pos_in, file_out,
1511 pos_out, len, flags);
1512 if (ret == -EOPNOTSUPP)
1513 ret = do_splice_direct(file_in, &pos_in, file_out, &pos_out,
1514 len > MAX_RW_COUNT ? MAX_RW_COUNT : len, 0);
1515
29732938
ZB
1516 if (ret > 0) {
1517 fsnotify_access(file_in);
1518 add_rchar(current, ret);
1519 fsnotify_modify(file_out);
1520 add_wchar(current, ret);
1521 }
1522 inc_syscr(current);
1523 inc_syscw(current);
1524
1525 mnt_drop_write_file(file_out);
1526
1527 return ret;
1528}
1529EXPORT_SYMBOL(vfs_copy_file_range);
1530
1531SYSCALL_DEFINE6(copy_file_range, int, fd_in, loff_t __user *, off_in,
1532 int, fd_out, loff_t __user *, off_out,
1533 size_t, len, unsigned int, flags)
1534{
1535 loff_t pos_in;
1536 loff_t pos_out;
1537 struct fd f_in;
1538 struct fd f_out;
1539 ssize_t ret = -EBADF;
1540
1541 f_in = fdget(fd_in);
1542 if (!f_in.file)
1543 goto out2;
1544
1545 f_out = fdget(fd_out);
1546 if (!f_out.file)
1547 goto out1;
1548
1549 ret = -EFAULT;
1550 if (off_in) {
1551 if (copy_from_user(&pos_in, off_in, sizeof(loff_t)))
1552 goto out;
1553 } else {
1554 pos_in = f_in.file->f_pos;
1555 }
1556
1557 if (off_out) {
1558 if (copy_from_user(&pos_out, off_out, sizeof(loff_t)))
1559 goto out;
1560 } else {
1561 pos_out = f_out.file->f_pos;
1562 }
1563
1564 ret = vfs_copy_file_range(f_in.file, pos_in, f_out.file, pos_out, len,
1565 flags);
1566 if (ret > 0) {
1567 pos_in += ret;
1568 pos_out += ret;
1569
1570 if (off_in) {
1571 if (copy_to_user(off_in, &pos_in, sizeof(loff_t)))
1572 ret = -EFAULT;
1573 } else {
1574 f_in.file->f_pos = pos_in;
1575 }
1576
1577 if (off_out) {
1578 if (copy_to_user(off_out, &pos_out, sizeof(loff_t)))
1579 ret = -EFAULT;
1580 } else {
1581 f_out.file->f_pos = pos_out;
1582 }
1583 }
1584
1585out:
1586 fdput(f_out);
1587out1:
1588 fdput(f_in);
1589out2:
1590 return ret;
1591}
04b38d60
CH
1592
1593static int clone_verify_area(struct file *file, loff_t pos, u64 len, bool write)
1594{
1595 struct inode *inode = file_inode(file);
1596
1597 if (unlikely(pos < 0))
1598 return -EINVAL;
1599
1600 if (unlikely((loff_t) (pos + len) < 0))
1601 return -EINVAL;
1602
1603 if (unlikely(inode->i_flctx && mandatory_lock(inode))) {
1604 loff_t end = len ? pos + len - 1 : OFFSET_MAX;
1605 int retval;
1606
1607 retval = locks_mandatory_area(inode, file, pos, end,
1608 write ? F_WRLCK : F_RDLCK);
1609 if (retval < 0)
1610 return retval;
1611 }
1612
1613 return security_file_permission(file, write ? MAY_WRITE : MAY_READ);
1614}
1615
1616int vfs_clone_file_range(struct file *file_in, loff_t pos_in,
1617 struct file *file_out, loff_t pos_out, u64 len)
1618{
1619 struct inode *inode_in = file_inode(file_in);
1620 struct inode *inode_out = file_inode(file_out);
1621 int ret;
1622
1623 if (inode_in->i_sb != inode_out->i_sb ||
1624 file_in->f_path.mnt != file_out->f_path.mnt)
1625 return -EXDEV;
1626
1627 if (S_ISDIR(inode_in->i_mode) || S_ISDIR(inode_out->i_mode))
1628 return -EISDIR;
1629 if (!S_ISREG(inode_in->i_mode) || !S_ISREG(inode_out->i_mode))
d79bdd52 1630 return -EINVAL;
04b38d60
CH
1631
1632 if (!(file_in->f_mode & FMODE_READ) ||
1633 !(file_out->f_mode & FMODE_WRITE) ||
1634 (file_out->f_flags & O_APPEND) ||
1635 !file_in->f_op->clone_file_range)
1636 return -EBADF;
1637
1638 ret = clone_verify_area(file_in, pos_in, len, false);
1639 if (ret)
1640 return ret;
1641
1642 ret = clone_verify_area(file_out, pos_out, len, true);
1643 if (ret)
1644 return ret;
1645
1646 if (pos_in + len > i_size_read(inode_in))
1647 return -EINVAL;
1648
1649 ret = mnt_want_write_file(file_out);
1650 if (ret)
1651 return ret;
1652
1653 ret = file_in->f_op->clone_file_range(file_in, pos_in,
1654 file_out, pos_out, len);
1655 if (!ret) {
1656 fsnotify_access(file_in);
1657 fsnotify_modify(file_out);
1658 }
1659
1660 mnt_drop_write_file(file_out);
1661 return ret;
1662}
1663EXPORT_SYMBOL(vfs_clone_file_range);
54dbc151
DW
1664
1665int vfs_dedupe_file_range(struct file *file, struct file_dedupe_range *same)
1666{
1667 struct file_dedupe_range_info *info;
1668 struct inode *src = file_inode(file);
1669 u64 off;
1670 u64 len;
1671 int i;
1672 int ret;
1673 bool is_admin = capable(CAP_SYS_ADMIN);
1674 u16 count = same->dest_count;
1675 struct file *dst_file;
1676 loff_t dst_off;
1677 ssize_t deduped;
1678
1679 if (!(file->f_mode & FMODE_READ))
1680 return -EINVAL;
1681
1682 if (same->reserved1 || same->reserved2)
1683 return -EINVAL;
1684
1685 off = same->src_offset;
1686 len = same->src_length;
1687
1688 ret = -EISDIR;
1689 if (S_ISDIR(src->i_mode))
1690 goto out;
1691
1692 ret = -EINVAL;
1693 if (!S_ISREG(src->i_mode))
1694 goto out;
1695
1696 ret = clone_verify_area(file, off, len, false);
1697 if (ret < 0)
1698 goto out;
1699 ret = 0;
1700
1701 /* pre-format output fields to sane values */
1702 for (i = 0; i < count; i++) {
1703 same->info[i].bytes_deduped = 0ULL;
1704 same->info[i].status = FILE_DEDUPE_RANGE_SAME;
1705 }
1706
1707 for (i = 0, info = same->info; i < count; i++, info++) {
1708 struct inode *dst;
1709 struct fd dst_fd = fdget(info->dest_fd);
1710
1711 dst_file = dst_fd.file;
1712 if (!dst_file) {
1713 info->status = -EBADF;
1714 goto next_loop;
1715 }
1716 dst = file_inode(dst_file);
1717
1718 ret = mnt_want_write_file(dst_file);
1719 if (ret) {
1720 info->status = ret;
1721 goto next_loop;
1722 }
1723
1724 dst_off = info->dest_offset;
1725 ret = clone_verify_area(dst_file, dst_off, len, true);
1726 if (ret < 0) {
1727 info->status = ret;
1728 goto next_file;
1729 }
1730 ret = 0;
1731
1732 if (info->reserved) {
1733 info->status = -EINVAL;
1734 } else if (!(is_admin || (dst_file->f_mode & FMODE_WRITE))) {
1735 info->status = -EINVAL;
1736 } else if (file->f_path.mnt != dst_file->f_path.mnt) {
1737 info->status = -EXDEV;
1738 } else if (S_ISDIR(dst->i_mode)) {
1739 info->status = -EISDIR;
1740 } else if (dst_file->f_op->dedupe_file_range == NULL) {
1741 info->status = -EINVAL;
1742 } else {
1743 deduped = dst_file->f_op->dedupe_file_range(file, off,
1744 len, dst_file,
1745 info->dest_offset);
1746 if (deduped == -EBADE)
1747 info->status = FILE_DEDUPE_RANGE_DIFFERS;
1748 else if (deduped < 0)
1749 info->status = deduped;
1750 else
1751 info->bytes_deduped += deduped;
1752 }
1753
1754next_file:
1755 mnt_drop_write_file(dst_file);
1756next_loop:
1757 fdput(dst_fd);
e62e560f
DW
1758
1759 if (fatal_signal_pending(current))
1760 goto out;
54dbc151
DW
1761 }
1762
1763out:
1764 return ret;
1765}
1766EXPORT_SYMBOL(vfs_dedupe_file_range);
This page took 0.892259 seconds and 5 git commands to generate.