logfs: constify logfs_block_ops structures
[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>
06ae43f3 19#include "internal.h"
1da177e4
LT
20
21#include <asm/uaccess.h>
22#include <asm/unistd.h>
23
c0bd14af 24typedef ssize_t (*io_fn_t)(struct file *, char __user *, size_t, loff_t *);
293bc982 25typedef ssize_t (*iter_fn_t)(struct kiocb *, struct iov_iter *);
c0bd14af 26
4b6f5d20 27const struct file_operations generic_ro_fops = {
1da177e4 28 .llseek = generic_file_llseek,
aad4f8bb 29 .read_iter = generic_file_read_iter,
1da177e4 30 .mmap = generic_file_readonly_mmap,
534f2aaa 31 .splice_read = generic_file_splice_read,
1da177e4
LT
32};
33
34EXPORT_SYMBOL(generic_ro_fops);
35
cccb5a1e 36static inline int unsigned_offsets(struct file *file)
4a3956c7 37{
cccb5a1e 38 return file->f_mode & FMODE_UNSIGNED_OFFSET;
4a3956c7
KH
39}
40
46a1c2c7
JL
41/**
42 * vfs_setpos - update the file offset for lseek
43 * @file: file structure in question
44 * @offset: file offset to seek to
45 * @maxsize: maximum file size
46 *
47 * This is a low-level filesystem helper for updating the file offset to
48 * the value specified by @offset if the given offset is valid and it is
49 * not equal to the current file offset.
50 *
51 * Return the specified offset on success and -EINVAL on invalid offset.
52 */
53loff_t vfs_setpos(struct file *file, loff_t offset, loff_t maxsize)
ef3d0fd2
AK
54{
55 if (offset < 0 && !unsigned_offsets(file))
56 return -EINVAL;
57 if (offset > maxsize)
58 return -EINVAL;
59
60 if (offset != file->f_pos) {
61 file->f_pos = offset;
62 file->f_version = 0;
63 }
64 return offset;
65}
46a1c2c7 66EXPORT_SYMBOL(vfs_setpos);
ef3d0fd2 67
3a8cff4f 68/**
5760495a 69 * generic_file_llseek_size - generic llseek implementation for regular files
3a8cff4f
CH
70 * @file: file structure to seek on
71 * @offset: file offset to seek to
965c8e59 72 * @whence: type of seek
e8b96eb5
ES
73 * @size: max size of this file in file system
74 * @eof: offset used for SEEK_END position
3a8cff4f 75 *
5760495a 76 * This is a variant of generic_file_llseek that allows passing in a custom
e8b96eb5 77 * maximum file size and a custom EOF position, for e.g. hashed directories
ef3d0fd2
AK
78 *
79 * Synchronization:
5760495a 80 * SEEK_SET and SEEK_END are unsynchronized (but atomic on 64bit platforms)
ef3d0fd2
AK
81 * SEEK_CUR is synchronized against other SEEK_CURs, but not read/writes.
82 * read/writes behave like SEEK_SET against seeks.
3a8cff4f 83 */
9465efc9 84loff_t
965c8e59 85generic_file_llseek_size(struct file *file, loff_t offset, int whence,
e8b96eb5 86 loff_t maxsize, loff_t eof)
1da177e4 87{
965c8e59 88 switch (whence) {
3a8cff4f 89 case SEEK_END:
e8b96eb5 90 offset += eof;
3a8cff4f
CH
91 break;
92 case SEEK_CUR:
5b6f1eb9
AK
93 /*
94 * Here we special-case the lseek(fd, 0, SEEK_CUR)
95 * position-querying operation. Avoid rewriting the "same"
96 * f_pos value back to the file because a concurrent read(),
97 * write() or lseek() might have altered it
98 */
99 if (offset == 0)
100 return file->f_pos;
ef3d0fd2
AK
101 /*
102 * f_lock protects against read/modify/write race with other
103 * SEEK_CURs. Note that parallel writes and reads behave
104 * like SEEK_SET.
105 */
106 spin_lock(&file->f_lock);
46a1c2c7 107 offset = vfs_setpos(file, file->f_pos + offset, maxsize);
ef3d0fd2
AK
108 spin_unlock(&file->f_lock);
109 return offset;
982d8165
JB
110 case SEEK_DATA:
111 /*
112 * In the generic case the entire file is data, so as long as
113 * offset isn't at the end of the file then the offset is data.
114 */
e8b96eb5 115 if (offset >= eof)
982d8165
JB
116 return -ENXIO;
117 break;
118 case SEEK_HOLE:
119 /*
120 * There is a virtual hole at the end of the file, so as long as
121 * offset isn't i_size or larger, return i_size.
122 */
e8b96eb5 123 if (offset >= eof)
982d8165 124 return -ENXIO;
e8b96eb5 125 offset = eof;
982d8165 126 break;
1da177e4 127 }
3a8cff4f 128
46a1c2c7 129 return vfs_setpos(file, offset, maxsize);
5760495a
AK
130}
131EXPORT_SYMBOL(generic_file_llseek_size);
132
133/**
134 * generic_file_llseek - generic llseek implementation for regular files
135 * @file: file structure to seek on
136 * @offset: file offset to seek to
965c8e59 137 * @whence: type of seek
5760495a
AK
138 *
139 * This is a generic implemenation of ->llseek useable for all normal local
140 * filesystems. It just updates the file offset to the value specified by
546ae2d2 141 * @offset and @whence.
5760495a 142 */
965c8e59 143loff_t generic_file_llseek(struct file *file, loff_t offset, int whence)
5760495a
AK
144{
145 struct inode *inode = file->f_mapping->host;
146
965c8e59 147 return generic_file_llseek_size(file, offset, whence,
e8b96eb5
ES
148 inode->i_sb->s_maxbytes,
149 i_size_read(inode));
1da177e4 150}
9465efc9 151EXPORT_SYMBOL(generic_file_llseek);
1da177e4 152
1bf9d14d
AV
153/**
154 * fixed_size_llseek - llseek implementation for fixed-sized devices
155 * @file: file structure to seek on
156 * @offset: file offset to seek to
157 * @whence: type of seek
158 * @size: size of the file
159 *
160 */
161loff_t fixed_size_llseek(struct file *file, loff_t offset, int whence, loff_t size)
162{
163 switch (whence) {
164 case SEEK_SET: case SEEK_CUR: case SEEK_END:
165 return generic_file_llseek_size(file, offset, whence,
166 size, size);
167 default:
168 return -EINVAL;
169 }
170}
171EXPORT_SYMBOL(fixed_size_llseek);
172
b25472f9
AV
173/**
174 * no_seek_end_llseek - llseek implementation for fixed-sized devices
175 * @file: file structure to seek on
176 * @offset: file offset to seek to
177 * @whence: type of seek
178 *
179 */
180loff_t no_seek_end_llseek(struct file *file, loff_t offset, int whence)
181{
182 switch (whence) {
183 case SEEK_SET: case SEEK_CUR:
184 return generic_file_llseek_size(file, offset, whence,
185 ~0ULL, 0);
186 default:
187 return -EINVAL;
188 }
189}
190EXPORT_SYMBOL(no_seek_end_llseek);
191
192/**
193 * no_seek_end_llseek_size - llseek implementation for fixed-sized devices
194 * @file: file structure to seek on
195 * @offset: file offset to seek to
196 * @whence: type of seek
197 * @size: maximal offset allowed
198 *
199 */
200loff_t no_seek_end_llseek_size(struct file *file, loff_t offset, int whence, loff_t size)
201{
202 switch (whence) {
203 case SEEK_SET: case SEEK_CUR:
204 return generic_file_llseek_size(file, offset, whence,
205 size, 0);
206 default:
207 return -EINVAL;
208 }
209}
210EXPORT_SYMBOL(no_seek_end_llseek_size);
211
ae6afc3f
B
212/**
213 * noop_llseek - No Operation Performed llseek implementation
214 * @file: file structure to seek on
215 * @offset: file offset to seek to
965c8e59 216 * @whence: type of seek
ae6afc3f
B
217 *
218 * This is an implementation of ->llseek useable for the rare special case when
219 * userspace expects the seek to succeed but the (device) file is actually not
220 * able to perform the seek. In this case you use noop_llseek() instead of
221 * falling back to the default implementation of ->llseek.
222 */
965c8e59 223loff_t noop_llseek(struct file *file, loff_t offset, int whence)
ae6afc3f
B
224{
225 return file->f_pos;
226}
227EXPORT_SYMBOL(noop_llseek);
228
965c8e59 229loff_t no_llseek(struct file *file, loff_t offset, int whence)
1da177e4
LT
230{
231 return -ESPIPE;
232}
233EXPORT_SYMBOL(no_llseek);
234
965c8e59 235loff_t default_llseek(struct file *file, loff_t offset, int whence)
1da177e4 236{
496ad9aa 237 struct inode *inode = file_inode(file);
16abef0e 238 loff_t retval;
1da177e4 239
982d8165 240 mutex_lock(&inode->i_mutex);
965c8e59 241 switch (whence) {
7b8e8924 242 case SEEK_END:
982d8165 243 offset += i_size_read(inode);
1da177e4 244 break;
7b8e8924 245 case SEEK_CUR:
5b6f1eb9
AK
246 if (offset == 0) {
247 retval = file->f_pos;
248 goto out;
249 }
1da177e4 250 offset += file->f_pos;
982d8165
JB
251 break;
252 case SEEK_DATA:
253 /*
254 * In the generic case the entire file is data, so as
255 * long as offset isn't at the end of the file then the
256 * offset is data.
257 */
bacb2d81
DC
258 if (offset >= inode->i_size) {
259 retval = -ENXIO;
260 goto out;
261 }
982d8165
JB
262 break;
263 case SEEK_HOLE:
264 /*
265 * There is a virtual hole at the end of the file, so
266 * as long as offset isn't i_size or larger, return
267 * i_size.
268 */
bacb2d81
DC
269 if (offset >= inode->i_size) {
270 retval = -ENXIO;
271 goto out;
272 }
982d8165
JB
273 offset = inode->i_size;
274 break;
1da177e4
LT
275 }
276 retval = -EINVAL;
cccb5a1e 277 if (offset >= 0 || unsigned_offsets(file)) {
1da177e4
LT
278 if (offset != file->f_pos) {
279 file->f_pos = offset;
280 file->f_version = 0;
281 }
282 retval = offset;
283 }
5b6f1eb9 284out:
982d8165 285 mutex_unlock(&inode->i_mutex);
1da177e4
LT
286 return retval;
287}
288EXPORT_SYMBOL(default_llseek);
289
965c8e59 290loff_t vfs_llseek(struct file *file, loff_t offset, int whence)
1da177e4
LT
291{
292 loff_t (*fn)(struct file *, loff_t, int);
293
294 fn = no_llseek;
295 if (file->f_mode & FMODE_LSEEK) {
72c2d531 296 if (file->f_op->llseek)
1da177e4
LT
297 fn = file->f_op->llseek;
298 }
965c8e59 299 return fn(file, offset, whence);
1da177e4
LT
300}
301EXPORT_SYMBOL(vfs_llseek);
302
9c225f26
LT
303static inline struct fd fdget_pos(int fd)
304{
bd2a31d5 305 return __to_fd(__fdget_pos(fd));
9c225f26
LT
306}
307
308static inline void fdput_pos(struct fd f)
309{
310 if (f.flags & FDPUT_POS_UNLOCK)
311 mutex_unlock(&f.file->f_pos_lock);
312 fdput(f);
313}
314
965c8e59 315SYSCALL_DEFINE3(lseek, unsigned int, fd, off_t, offset, unsigned int, whence)
1da177e4
LT
316{
317 off_t retval;
9c225f26 318 struct fd f = fdget_pos(fd);
2903ff01
AV
319 if (!f.file)
320 return -EBADF;
1da177e4
LT
321
322 retval = -EINVAL;
965c8e59
AM
323 if (whence <= SEEK_MAX) {
324 loff_t res = vfs_llseek(f.file, offset, whence);
1da177e4
LT
325 retval = res;
326 if (res != (loff_t)retval)
327 retval = -EOVERFLOW; /* LFS: should only happen on 32 bit platforms */
328 }
9c225f26 329 fdput_pos(f);
1da177e4
LT
330 return retval;
331}
332
561c6731
AV
333#ifdef CONFIG_COMPAT
334COMPAT_SYSCALL_DEFINE3(lseek, unsigned int, fd, compat_off_t, offset, unsigned int, whence)
335{
336 return sys_lseek(fd, offset, whence);
337}
338#endif
339
1da177e4 340#ifdef __ARCH_WANT_SYS_LLSEEK
003d7ab4
HC
341SYSCALL_DEFINE5(llseek, unsigned int, fd, unsigned long, offset_high,
342 unsigned long, offset_low, loff_t __user *, result,
965c8e59 343 unsigned int, whence)
1da177e4
LT
344{
345 int retval;
d7a15f8d 346 struct fd f = fdget_pos(fd);
1da177e4 347 loff_t offset;
1da177e4 348
2903ff01
AV
349 if (!f.file)
350 return -EBADF;
1da177e4
LT
351
352 retval = -EINVAL;
965c8e59 353 if (whence > SEEK_MAX)
1da177e4
LT
354 goto out_putf;
355
2903ff01 356 offset = vfs_llseek(f.file, ((loff_t) offset_high << 32) | offset_low,
965c8e59 357 whence);
1da177e4
LT
358
359 retval = (int)offset;
360 if (offset >= 0) {
361 retval = -EFAULT;
362 if (!copy_to_user(result, &offset, sizeof(offset)))
363 retval = 0;
364 }
365out_putf:
d7a15f8d 366 fdput_pos(f);
1da177e4
LT
367 return retval;
368}
369#endif
370
dbe4e192
CH
371ssize_t vfs_iter_read(struct file *file, struct iov_iter *iter, loff_t *ppos)
372{
373 struct kiocb kiocb;
374 ssize_t ret;
375
376 if (!file->f_op->read_iter)
377 return -EINVAL;
378
379 init_sync_kiocb(&kiocb, file);
380 kiocb.ki_pos = *ppos;
dbe4e192
CH
381
382 iter->type |= READ;
383 ret = file->f_op->read_iter(&kiocb, iter);
599bd19b 384 BUG_ON(ret == -EIOCBQUEUED);
dbe4e192
CH
385 if (ret > 0)
386 *ppos = kiocb.ki_pos;
387 return ret;
388}
389EXPORT_SYMBOL(vfs_iter_read);
390
391ssize_t vfs_iter_write(struct file *file, struct iov_iter *iter, loff_t *ppos)
392{
393 struct kiocb kiocb;
394 ssize_t ret;
395
396 if (!file->f_op->write_iter)
397 return -EINVAL;
398
399 init_sync_kiocb(&kiocb, file);
400 kiocb.ki_pos = *ppos;
dbe4e192
CH
401
402 iter->type |= WRITE;
403 ret = file->f_op->write_iter(&kiocb, iter);
599bd19b 404 BUG_ON(ret == -EIOCBQUEUED);
dbe4e192
CH
405 if (ret > 0)
406 *ppos = kiocb.ki_pos;
407 return ret;
408}
409EXPORT_SYMBOL(vfs_iter_write);
410
e28cc715
LT
411/*
412 * rw_verify_area doesn't like huge counts. We limit
413 * them to something that fits in "int" so that others
414 * won't have to do range checks all the time.
415 */
68d70d03 416int rw_verify_area(int read_write, struct file *file, const loff_t *ppos, size_t count)
1da177e4
LT
417{
418 struct inode *inode;
419 loff_t pos;
c43e259c 420 int retval = -EINVAL;
1da177e4 421
496ad9aa 422 inode = file_inode(file);
e28cc715 423 if (unlikely((ssize_t) count < 0))
c43e259c 424 return retval;
1da177e4 425 pos = *ppos;
cccb5a1e
AV
426 if (unlikely(pos < 0)) {
427 if (!unsigned_offsets(file))
428 return retval;
429 if (count >= -pos) /* both values are in 0..LLONG_MAX */
430 return -EOVERFLOW;
431 } else if (unlikely((loff_t) (pos + count) < 0)) {
432 if (!unsigned_offsets(file))
4a3956c7
KH
433 return retval;
434 }
1da177e4 435
bd61e0a9 436 if (unlikely(inode->i_flctx && mandatory_lock(inode))) {
c43e259c 437 retval = locks_mandatory_area(
e28cc715
LT
438 read_write == READ ? FLOCK_VERIFY_READ : FLOCK_VERIFY_WRITE,
439 inode, file, pos, count);
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
AV
694static ssize_t do_iter_readv_writev(struct file *filp, struct iov_iter *iter,
695 loff_t *ppos, iter_fn_t fn)
293bc982
AV
696{
697 struct kiocb kiocb;
293bc982
AV
698 ssize_t ret;
699
700 init_sync_kiocb(&kiocb, filp);
701 kiocb.ki_pos = *ppos;
293bc982 702
ac15ac06 703 ret = fn(&kiocb, iter);
599bd19b 704 BUG_ON(ret == -EIOCBQUEUED);
293bc982
AV
705 *ppos = kiocb.ki_pos;
706 return ret;
707}
708
ee0b3e67 709/* Do it by hand, with file-ops */
ac15ac06
AV
710static ssize_t do_loop_readv_writev(struct file *filp, struct iov_iter *iter,
711 loff_t *ppos, io_fn_t fn)
ee0b3e67 712{
ee0b3e67
BP
713 ssize_t ret = 0;
714
ac15ac06
AV
715 while (iov_iter_count(iter)) {
716 struct iovec iovec = iov_iter_iovec(iter);
ee0b3e67
BP
717 ssize_t nr;
718
ac15ac06 719 nr = fn(filp, iovec.iov_base, iovec.iov_len, ppos);
ee0b3e67
BP
720
721 if (nr < 0) {
722 if (!ret)
723 ret = nr;
724 break;
725 }
726 ret += nr;
ac15ac06 727 if (nr != iovec.iov_len)
ee0b3e67 728 break;
ac15ac06 729 iov_iter_advance(iter, nr);
ee0b3e67
BP
730 }
731
732 return ret;
733}
734
1da177e4
LT
735/* A write operation does a read from user space and vice versa */
736#define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
737
eed4e51f
BP
738ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
739 unsigned long nr_segs, unsigned long fast_segs,
740 struct iovec *fast_pointer,
ac34ebb3 741 struct iovec **ret_pointer)
435f49a5 742{
eed4e51f 743 unsigned long seg;
435f49a5 744 ssize_t ret;
eed4e51f
BP
745 struct iovec *iov = fast_pointer;
746
435f49a5
LT
747 /*
748 * SuS says "The readv() function *may* fail if the iovcnt argument
749 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
750 * traditionally returned zero for zero segments, so...
751 */
eed4e51f
BP
752 if (nr_segs == 0) {
753 ret = 0;
435f49a5 754 goto out;
eed4e51f
BP
755 }
756
435f49a5
LT
757 /*
758 * First get the "struct iovec" from user memory and
759 * verify all the pointers
760 */
eed4e51f
BP
761 if (nr_segs > UIO_MAXIOV) {
762 ret = -EINVAL;
435f49a5 763 goto out;
eed4e51f
BP
764 }
765 if (nr_segs > fast_segs) {
435f49a5 766 iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
eed4e51f
BP
767 if (iov == NULL) {
768 ret = -ENOMEM;
435f49a5 769 goto out;
eed4e51f 770 }
435f49a5 771 }
eed4e51f
BP
772 if (copy_from_user(iov, uvector, nr_segs*sizeof(*uvector))) {
773 ret = -EFAULT;
435f49a5 774 goto out;
eed4e51f
BP
775 }
776
435f49a5 777 /*
eed4e51f
BP
778 * According to the Single Unix Specification we should return EINVAL
779 * if an element length is < 0 when cast to ssize_t or if the
780 * total length would overflow the ssize_t return value of the
781 * system call.
435f49a5
LT
782 *
783 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
784 * overflow case.
785 */
eed4e51f 786 ret = 0;
435f49a5
LT
787 for (seg = 0; seg < nr_segs; seg++) {
788 void __user *buf = iov[seg].iov_base;
789 ssize_t len = (ssize_t)iov[seg].iov_len;
eed4e51f
BP
790
791 /* see if we we're about to use an invalid len or if
792 * it's about to overflow ssize_t */
435f49a5 793 if (len < 0) {
eed4e51f 794 ret = -EINVAL;
435f49a5 795 goto out;
eed4e51f 796 }
ac34ebb3 797 if (type >= 0
fcf63409 798 && unlikely(!access_ok(vrfy_dir(type), buf, len))) {
eed4e51f 799 ret = -EFAULT;
435f49a5
LT
800 goto out;
801 }
802 if (len > MAX_RW_COUNT - ret) {
803 len = MAX_RW_COUNT - ret;
804 iov[seg].iov_len = len;
eed4e51f 805 }
eed4e51f 806 ret += len;
435f49a5 807 }
eed4e51f
BP
808out:
809 *ret_pointer = iov;
810 return ret;
811}
812
1da177e4
LT
813static ssize_t do_readv_writev(int type, struct file *file,
814 const struct iovec __user * uvector,
815 unsigned long nr_segs, loff_t *pos)
816{
1da177e4
LT
817 size_t tot_len;
818 struct iovec iovstack[UIO_FASTIOV];
ee0b3e67 819 struct iovec *iov = iovstack;
ac15ac06 820 struct iov_iter iter;
1da177e4 821 ssize_t ret;
1da177e4 822 io_fn_t fn;
293bc982 823 iter_fn_t iter_fn;
1da177e4 824
0504c074
AV
825 ret = import_iovec(type, uvector, nr_segs,
826 ARRAY_SIZE(iovstack), &iov, &iter);
827 if (ret < 0)
828 return ret;
1da177e4 829
0504c074
AV
830 tot_len = iov_iter_count(&iter);
831 if (!tot_len)
832 goto out;
1da177e4 833 ret = rw_verify_area(type, file, pos, tot_len);
e28cc715 834 if (ret < 0)
411b67b4 835 goto out;
1da177e4 836
1da177e4
LT
837 if (type == READ) {
838 fn = file->f_op->read;
293bc982 839 iter_fn = file->f_op->read_iter;
1da177e4
LT
840 } else {
841 fn = (io_fn_t)file->f_op->write;
293bc982 842 iter_fn = file->f_op->write_iter;
03d95eb2 843 file_start_write(file);
1da177e4
LT
844 }
845
293bc982 846 if (iter_fn)
ac15ac06 847 ret = do_iter_readv_writev(file, &iter, pos, iter_fn);
ee0b3e67 848 else
ac15ac06 849 ret = do_loop_readv_writev(file, &iter, pos, fn);
1da177e4 850
03d95eb2
AV
851 if (type != READ)
852 file_end_write(file);
853
1da177e4 854out:
0504c074 855 kfree(iov);
0eeca283
RL
856 if ((ret + (type == READ)) > 0) {
857 if (type == READ)
2a12a9d7 858 fsnotify_access(file);
0eeca283 859 else
2a12a9d7 860 fsnotify_modify(file);
0eeca283 861 }
1da177e4 862 return ret;
1da177e4
LT
863}
864
865ssize_t vfs_readv(struct file *file, const struct iovec __user *vec,
866 unsigned long vlen, loff_t *pos)
867{
868 if (!(file->f_mode & FMODE_READ))
869 return -EBADF;
7f7f25e8 870 if (!(file->f_mode & FMODE_CAN_READ))
1da177e4
LT
871 return -EINVAL;
872
873 return do_readv_writev(READ, file, vec, vlen, pos);
874}
875
876EXPORT_SYMBOL(vfs_readv);
877
878ssize_t vfs_writev(struct file *file, const struct iovec __user *vec,
879 unsigned long vlen, loff_t *pos)
880{
881 if (!(file->f_mode & FMODE_WRITE))
882 return -EBADF;
7f7f25e8 883 if (!(file->f_mode & FMODE_CAN_WRITE))
1da177e4
LT
884 return -EINVAL;
885
886 return do_readv_writev(WRITE, file, vec, vlen, pos);
887}
888
889EXPORT_SYMBOL(vfs_writev);
890
3cdad428
HC
891SYSCALL_DEFINE3(readv, unsigned long, fd, const struct iovec __user *, vec,
892 unsigned long, vlen)
1da177e4 893{
9c225f26 894 struct fd f = fdget_pos(fd);
1da177e4 895 ssize_t ret = -EBADF;
1da177e4 896
2903ff01
AV
897 if (f.file) {
898 loff_t pos = file_pos_read(f.file);
899 ret = vfs_readv(f.file, vec, vlen, &pos);
5faf153e
AV
900 if (ret >= 0)
901 file_pos_write(f.file, pos);
9c225f26 902 fdput_pos(f);
1da177e4
LT
903 }
904
905 if (ret > 0)
4b98d11b
AD
906 add_rchar(current, ret);
907 inc_syscr(current);
1da177e4
LT
908 return ret;
909}
910
3cdad428
HC
911SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec __user *, vec,
912 unsigned long, vlen)
1da177e4 913{
9c225f26 914 struct fd f = fdget_pos(fd);
1da177e4 915 ssize_t ret = -EBADF;
1da177e4 916
2903ff01
AV
917 if (f.file) {
918 loff_t pos = file_pos_read(f.file);
919 ret = vfs_writev(f.file, vec, vlen, &pos);
5faf153e
AV
920 if (ret >= 0)
921 file_pos_write(f.file, pos);
9c225f26 922 fdput_pos(f);
1da177e4
LT
923 }
924
925 if (ret > 0)
4b98d11b
AD
926 add_wchar(current, ret);
927 inc_syscw(current);
1da177e4
LT
928 return ret;
929}
930
601cc11d
LT
931static inline loff_t pos_from_hilo(unsigned long high, unsigned long low)
932{
933#define HALF_LONG_BITS (BITS_PER_LONG / 2)
934 return (((loff_t)high << HALF_LONG_BITS) << HALF_LONG_BITS) | low;
935}
936
f3554f4b 937SYSCALL_DEFINE5(preadv, unsigned long, fd, const struct iovec __user *, vec,
601cc11d 938 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
f3554f4b 939{
601cc11d 940 loff_t pos = pos_from_hilo(pos_h, pos_l);
2903ff01 941 struct fd f;
f3554f4b 942 ssize_t ret = -EBADF;
f3554f4b
GH
943
944 if (pos < 0)
945 return -EINVAL;
946
2903ff01
AV
947 f = fdget(fd);
948 if (f.file) {
f3554f4b 949 ret = -ESPIPE;
2903ff01
AV
950 if (f.file->f_mode & FMODE_PREAD)
951 ret = vfs_readv(f.file, vec, vlen, &pos);
952 fdput(f);
f3554f4b
GH
953 }
954
955 if (ret > 0)
956 add_rchar(current, ret);
957 inc_syscr(current);
958 return ret;
959}
960
961SYSCALL_DEFINE5(pwritev, unsigned long, fd, const struct iovec __user *, vec,
601cc11d 962 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
f3554f4b 963{
601cc11d 964 loff_t pos = pos_from_hilo(pos_h, pos_l);
2903ff01 965 struct fd f;
f3554f4b 966 ssize_t ret = -EBADF;
f3554f4b
GH
967
968 if (pos < 0)
969 return -EINVAL;
970
2903ff01
AV
971 f = fdget(fd);
972 if (f.file) {
f3554f4b 973 ret = -ESPIPE;
2903ff01
AV
974 if (f.file->f_mode & FMODE_PWRITE)
975 ret = vfs_writev(f.file, vec, vlen, &pos);
976 fdput(f);
f3554f4b
GH
977 }
978
979 if (ret > 0)
980 add_wchar(current, ret);
981 inc_syscw(current);
982 return ret;
983}
984
72ec3516
AV
985#ifdef CONFIG_COMPAT
986
987static ssize_t compat_do_readv_writev(int type, struct file *file,
988 const struct compat_iovec __user *uvector,
989 unsigned long nr_segs, loff_t *pos)
990{
991 compat_ssize_t tot_len;
992 struct iovec iovstack[UIO_FASTIOV];
993 struct iovec *iov = iovstack;
ac15ac06 994 struct iov_iter iter;
72ec3516
AV
995 ssize_t ret;
996 io_fn_t fn;
293bc982 997 iter_fn_t iter_fn;
72ec3516 998
0504c074
AV
999 ret = compat_import_iovec(type, uvector, nr_segs,
1000 UIO_FASTIOV, &iov, &iter);
1001 if (ret < 0)
1002 return ret;
72ec3516 1003
0504c074
AV
1004 tot_len = iov_iter_count(&iter);
1005 if (!tot_len)
1006 goto out;
72ec3516
AV
1007 ret = rw_verify_area(type, file, pos, tot_len);
1008 if (ret < 0)
1009 goto out;
1010
72ec3516
AV
1011 if (type == READ) {
1012 fn = file->f_op->read;
293bc982 1013 iter_fn = file->f_op->read_iter;
72ec3516
AV
1014 } else {
1015 fn = (io_fn_t)file->f_op->write;
293bc982 1016 iter_fn = file->f_op->write_iter;
03d95eb2 1017 file_start_write(file);
72ec3516
AV
1018 }
1019
293bc982 1020 if (iter_fn)
ac15ac06 1021 ret = do_iter_readv_writev(file, &iter, pos, iter_fn);
03d95eb2 1022 else
ac15ac06 1023 ret = do_loop_readv_writev(file, &iter, pos, fn);
72ec3516 1024
03d95eb2
AV
1025 if (type != READ)
1026 file_end_write(file);
1027
72ec3516 1028out:
0504c074 1029 kfree(iov);
72ec3516
AV
1030 if ((ret + (type == READ)) > 0) {
1031 if (type == READ)
1032 fsnotify_access(file);
1033 else
1034 fsnotify_modify(file);
1035 }
1036 return ret;
1037}
1038
1039static size_t compat_readv(struct file *file,
1040 const struct compat_iovec __user *vec,
1041 unsigned long vlen, loff_t *pos)
1042{
1043 ssize_t ret = -EBADF;
1044
1045 if (!(file->f_mode & FMODE_READ))
1046 goto out;
1047
1048 ret = -EINVAL;
7f7f25e8 1049 if (!(file->f_mode & FMODE_CAN_READ))
72ec3516
AV
1050 goto out;
1051
1052 ret = compat_do_readv_writev(READ, file, vec, vlen, pos);
1053
1054out:
1055 if (ret > 0)
1056 add_rchar(current, ret);
1057 inc_syscr(current);
1058 return ret;
1059}
1060
dfd948e3 1061COMPAT_SYSCALL_DEFINE3(readv, compat_ulong_t, fd,
72ec3516 1062 const struct compat_iovec __user *,vec,
dfd948e3 1063 compat_ulong_t, vlen)
72ec3516 1064{
9c225f26 1065 struct fd f = fdget_pos(fd);
72ec3516
AV
1066 ssize_t ret;
1067 loff_t pos;
1068
1069 if (!f.file)
1070 return -EBADF;
1071 pos = f.file->f_pos;
1072 ret = compat_readv(f.file, vec, vlen, &pos);
5faf153e
AV
1073 if (ret >= 0)
1074 f.file->f_pos = pos;
9c225f26 1075 fdput_pos(f);
72ec3516
AV
1076 return ret;
1077}
1078
378a10f3
HC
1079static long __compat_sys_preadv64(unsigned long fd,
1080 const struct compat_iovec __user *vec,
1081 unsigned long vlen, loff_t pos)
72ec3516
AV
1082{
1083 struct fd f;
1084 ssize_t ret;
1085
1086 if (pos < 0)
1087 return -EINVAL;
1088 f = fdget(fd);
1089 if (!f.file)
1090 return -EBADF;
1091 ret = -ESPIPE;
1092 if (f.file->f_mode & FMODE_PREAD)
1093 ret = compat_readv(f.file, vec, vlen, &pos);
1094 fdput(f);
1095 return ret;
1096}
1097
378a10f3
HC
1098#ifdef __ARCH_WANT_COMPAT_SYS_PREADV64
1099COMPAT_SYSCALL_DEFINE4(preadv64, unsigned long, fd,
1100 const struct compat_iovec __user *,vec,
1101 unsigned long, vlen, loff_t, pos)
1102{
1103 return __compat_sys_preadv64(fd, vec, vlen, pos);
1104}
1105#endif
1106
dfd948e3 1107COMPAT_SYSCALL_DEFINE5(preadv, compat_ulong_t, fd,
72ec3516 1108 const struct compat_iovec __user *,vec,
dfd948e3 1109 compat_ulong_t, vlen, u32, pos_low, u32, pos_high)
72ec3516
AV
1110{
1111 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
378a10f3
HC
1112
1113 return __compat_sys_preadv64(fd, vec, vlen, pos);
72ec3516
AV
1114}
1115
1116static size_t compat_writev(struct file *file,
1117 const struct compat_iovec __user *vec,
1118 unsigned long vlen, loff_t *pos)
1119{
1120 ssize_t ret = -EBADF;
1121
1122 if (!(file->f_mode & FMODE_WRITE))
1123 goto out;
1124
1125 ret = -EINVAL;
7f7f25e8 1126 if (!(file->f_mode & FMODE_CAN_WRITE))
72ec3516
AV
1127 goto out;
1128
1129 ret = compat_do_readv_writev(WRITE, file, vec, vlen, pos);
1130
1131out:
1132 if (ret > 0)
1133 add_wchar(current, ret);
1134 inc_syscw(current);
1135 return ret;
1136}
1137
dfd948e3 1138COMPAT_SYSCALL_DEFINE3(writev, compat_ulong_t, fd,
72ec3516 1139 const struct compat_iovec __user *, vec,
dfd948e3 1140 compat_ulong_t, vlen)
72ec3516 1141{
9c225f26 1142 struct fd f = fdget_pos(fd);
72ec3516
AV
1143 ssize_t ret;
1144 loff_t pos;
1145
1146 if (!f.file)
1147 return -EBADF;
1148 pos = f.file->f_pos;
1149 ret = compat_writev(f.file, vec, vlen, &pos);
5faf153e
AV
1150 if (ret >= 0)
1151 f.file->f_pos = pos;
9c225f26 1152 fdput_pos(f);
72ec3516
AV
1153 return ret;
1154}
1155
378a10f3
HC
1156static long __compat_sys_pwritev64(unsigned long fd,
1157 const struct compat_iovec __user *vec,
1158 unsigned long vlen, loff_t pos)
72ec3516
AV
1159{
1160 struct fd f;
1161 ssize_t ret;
1162
1163 if (pos < 0)
1164 return -EINVAL;
1165 f = fdget(fd);
1166 if (!f.file)
1167 return -EBADF;
1168 ret = -ESPIPE;
1169 if (f.file->f_mode & FMODE_PWRITE)
1170 ret = compat_writev(f.file, vec, vlen, &pos);
1171 fdput(f);
1172 return ret;
1173}
1174
378a10f3
HC
1175#ifdef __ARCH_WANT_COMPAT_SYS_PWRITEV64
1176COMPAT_SYSCALL_DEFINE4(pwritev64, unsigned long, fd,
1177 const struct compat_iovec __user *,vec,
1178 unsigned long, vlen, loff_t, pos)
1179{
1180 return __compat_sys_pwritev64(fd, vec, vlen, pos);
1181}
1182#endif
1183
dfd948e3 1184COMPAT_SYSCALL_DEFINE5(pwritev, compat_ulong_t, fd,
72ec3516 1185 const struct compat_iovec __user *,vec,
dfd948e3 1186 compat_ulong_t, vlen, u32, pos_low, u32, pos_high)
72ec3516
AV
1187{
1188 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
378a10f3
HC
1189
1190 return __compat_sys_pwritev64(fd, vec, vlen, pos);
72ec3516
AV
1191}
1192#endif
1193
19f4fc3a
AV
1194static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,
1195 size_t count, loff_t max)
1da177e4 1196{
2903ff01
AV
1197 struct fd in, out;
1198 struct inode *in_inode, *out_inode;
1da177e4 1199 loff_t pos;
7995bd28 1200 loff_t out_pos;
1da177e4 1201 ssize_t retval;
2903ff01 1202 int fl;
1da177e4
LT
1203
1204 /*
1205 * Get input file, and verify that it is ok..
1206 */
1207 retval = -EBADF;
2903ff01
AV
1208 in = fdget(in_fd);
1209 if (!in.file)
1da177e4 1210 goto out;
2903ff01 1211 if (!(in.file->f_mode & FMODE_READ))
1da177e4 1212 goto fput_in;
1da177e4 1213 retval = -ESPIPE;
7995bd28
AV
1214 if (!ppos) {
1215 pos = in.file->f_pos;
1216 } else {
1217 pos = *ppos;
2903ff01 1218 if (!(in.file->f_mode & FMODE_PREAD))
1da177e4 1219 goto fput_in;
7995bd28
AV
1220 }
1221 retval = rw_verify_area(READ, in.file, &pos, count);
e28cc715 1222 if (retval < 0)
1da177e4 1223 goto fput_in;
e28cc715 1224 count = retval;
1da177e4 1225
1da177e4
LT
1226 /*
1227 * Get output file, and verify that it is ok..
1228 */
1229 retval = -EBADF;
2903ff01
AV
1230 out = fdget(out_fd);
1231 if (!out.file)
1da177e4 1232 goto fput_in;
2903ff01 1233 if (!(out.file->f_mode & FMODE_WRITE))
1da177e4
LT
1234 goto fput_out;
1235 retval = -EINVAL;
496ad9aa
AV
1236 in_inode = file_inode(in.file);
1237 out_inode = file_inode(out.file);
7995bd28
AV
1238 out_pos = out.file->f_pos;
1239 retval = rw_verify_area(WRITE, out.file, &out_pos, count);
e28cc715 1240 if (retval < 0)
1da177e4 1241 goto fput_out;
e28cc715 1242 count = retval;
1da177e4 1243
1da177e4
LT
1244 if (!max)
1245 max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes);
1246
1da177e4
LT
1247 if (unlikely(pos + count > max)) {
1248 retval = -EOVERFLOW;
1249 if (pos >= max)
1250 goto fput_out;
1251 count = max - pos;
1252 }
1253
d96e6e71 1254 fl = 0;
534f2aaa 1255#if 0
d96e6e71
JA
1256 /*
1257 * We need to debate whether we can enable this or not. The
1258 * man page documents EAGAIN return for the output at least,
1259 * and the application is arguably buggy if it doesn't expect
1260 * EAGAIN on a non-blocking file descriptor.
1261 */
2903ff01 1262 if (in.file->f_flags & O_NONBLOCK)
d96e6e71 1263 fl = SPLICE_F_NONBLOCK;
534f2aaa 1264#endif
50cd2c57 1265 file_start_write(out.file);
7995bd28 1266 retval = do_splice_direct(in.file, &pos, out.file, &out_pos, count, fl);
50cd2c57 1267 file_end_write(out.file);
1da177e4
LT
1268
1269 if (retval > 0) {
4b98d11b
AD
1270 add_rchar(current, retval);
1271 add_wchar(current, retval);
a68c2f12
SW
1272 fsnotify_access(in.file);
1273 fsnotify_modify(out.file);
7995bd28
AV
1274 out.file->f_pos = out_pos;
1275 if (ppos)
1276 *ppos = pos;
1277 else
1278 in.file->f_pos = pos;
1da177e4 1279 }
1da177e4 1280
4b98d11b
AD
1281 inc_syscr(current);
1282 inc_syscw(current);
7995bd28 1283 if (pos > max)
1da177e4
LT
1284 retval = -EOVERFLOW;
1285
1286fput_out:
2903ff01 1287 fdput(out);
1da177e4 1288fput_in:
2903ff01 1289 fdput(in);
1da177e4
LT
1290out:
1291 return retval;
1292}
1293
002c8976 1294SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd, off_t __user *, offset, size_t, count)
1da177e4
LT
1295{
1296 loff_t pos;
1297 off_t off;
1298 ssize_t ret;
1299
1300 if (offset) {
1301 if (unlikely(get_user(off, offset)))
1302 return -EFAULT;
1303 pos = off;
1304 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
1305 if (unlikely(put_user(pos, offset)))
1306 return -EFAULT;
1307 return ret;
1308 }
1309
1310 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1311}
1312
002c8976 1313SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd, loff_t __user *, offset, size_t, count)
1da177e4
LT
1314{
1315 loff_t pos;
1316 ssize_t ret;
1317
1318 if (offset) {
1319 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
1320 return -EFAULT;
1321 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
1322 if (unlikely(put_user(pos, offset)))
1323 return -EFAULT;
1324 return ret;
1325 }
1326
1327 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1328}
19f4fc3a
AV
1329
1330#ifdef CONFIG_COMPAT
1331COMPAT_SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd,
1332 compat_off_t __user *, offset, compat_size_t, count)
1333{
1334 loff_t pos;
1335 off_t off;
1336 ssize_t ret;
1337
1338 if (offset) {
1339 if (unlikely(get_user(off, offset)))
1340 return -EFAULT;
1341 pos = off;
1342 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
1343 if (unlikely(put_user(pos, offset)))
1344 return -EFAULT;
1345 return ret;
1346 }
1347
1348 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1349}
1350
1351COMPAT_SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd,
1352 compat_loff_t __user *, offset, compat_size_t, count)
1353{
1354 loff_t pos;
1355 ssize_t ret;
1356
1357 if (offset) {
1358 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
1359 return -EFAULT;
1360 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
1361 if (unlikely(put_user(pos, offset)))
1362 return -EFAULT;
1363 return ret;
1364 }
1365
1366 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1367}
1368#endif
This page took 0.811937 seconds and 5 git commands to generate.