2 * linux/fs/read_write.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
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>
12 #include <linux/aio.h>
13 #include <linux/fsnotify.h>
14 #include <linux/security.h>
15 #include <linux/export.h>
16 #include <linux/syscalls.h>
17 #include <linux/pagemap.h>
18 #include <linux/splice.h>
19 #include <linux/compat.h>
22 #include <asm/uaccess.h>
23 #include <asm/unistd.h>
25 typedef ssize_t (*io_fn_t
)(struct file
*, char __user
*, size_t, loff_t
*);
26 typedef ssize_t (*iov_fn_t
)(struct kiocb
*, const struct iovec
*,
27 unsigned long, loff_t
);
29 const struct file_operations generic_ro_fops
= {
30 .llseek
= generic_file_llseek
,
32 .aio_read
= generic_file_aio_read
,
33 .mmap
= generic_file_readonly_mmap
,
34 .splice_read
= generic_file_splice_read
,
37 EXPORT_SYMBOL(generic_ro_fops
);
39 static inline int unsigned_offsets(struct file
*file
)
41 return file
->f_mode
& FMODE_UNSIGNED_OFFSET
;
44 static loff_t
lseek_execute(struct file
*file
, struct inode
*inode
,
45 loff_t offset
, loff_t maxsize
)
47 if (offset
< 0 && !unsigned_offsets(file
))
52 if (offset
!= file
->f_pos
) {
60 * generic_file_llseek_size - generic llseek implementation for regular files
61 * @file: file structure to seek on
62 * @offset: file offset to seek to
63 * @whence: type of seek
64 * @size: max size of this file in file system
65 * @eof: offset used for SEEK_END position
67 * This is a variant of generic_file_llseek that allows passing in a custom
68 * maximum file size and a custom EOF position, for e.g. hashed directories
71 * SEEK_SET and SEEK_END are unsynchronized (but atomic on 64bit platforms)
72 * SEEK_CUR is synchronized against other SEEK_CURs, but not read/writes.
73 * read/writes behave like SEEK_SET against seeks.
76 generic_file_llseek_size(struct file
*file
, loff_t offset
, int whence
,
77 loff_t maxsize
, loff_t eof
)
79 struct inode
*inode
= file
->f_mapping
->host
;
87 * Here we special-case the lseek(fd, 0, SEEK_CUR)
88 * position-querying operation. Avoid rewriting the "same"
89 * f_pos value back to the file because a concurrent read(),
90 * write() or lseek() might have altered it
95 * f_lock protects against read/modify/write race with other
96 * SEEK_CURs. Note that parallel writes and reads behave
99 spin_lock(&file
->f_lock
);
100 offset
= lseek_execute(file
, inode
, file
->f_pos
+ offset
,
102 spin_unlock(&file
->f_lock
);
106 * In the generic case the entire file is data, so as long as
107 * offset isn't at the end of the file then the offset is data.
114 * There is a virtual hole at the end of the file, so as long as
115 * offset isn't i_size or larger, return i_size.
123 return lseek_execute(file
, inode
, offset
, maxsize
);
125 EXPORT_SYMBOL(generic_file_llseek_size
);
128 * generic_file_llseek - generic llseek implementation for regular files
129 * @file: file structure to seek on
130 * @offset: file offset to seek to
131 * @whence: type of seek
133 * This is a generic implemenation of ->llseek useable for all normal local
134 * filesystems. It just updates the file offset to the value specified by
135 * @offset and @whence.
137 loff_t
generic_file_llseek(struct file
*file
, loff_t offset
, int whence
)
139 struct inode
*inode
= file
->f_mapping
->host
;
141 return generic_file_llseek_size(file
, offset
, whence
,
142 inode
->i_sb
->s_maxbytes
,
145 EXPORT_SYMBOL(generic_file_llseek
);
148 * fixed_size_llseek - llseek implementation for fixed-sized devices
149 * @file: file structure to seek on
150 * @offset: file offset to seek to
151 * @whence: type of seek
152 * @size: size of the file
155 loff_t
fixed_size_llseek(struct file
*file
, loff_t offset
, int whence
, loff_t size
)
158 case SEEK_SET
: case SEEK_CUR
: case SEEK_END
:
159 return generic_file_llseek_size(file
, offset
, whence
,
165 EXPORT_SYMBOL(fixed_size_llseek
);
168 * noop_llseek - No Operation Performed llseek implementation
169 * @file: file structure to seek on
170 * @offset: file offset to seek to
171 * @whence: type of seek
173 * This is an implementation of ->llseek useable for the rare special case when
174 * userspace expects the seek to succeed but the (device) file is actually not
175 * able to perform the seek. In this case you use noop_llseek() instead of
176 * falling back to the default implementation of ->llseek.
178 loff_t
noop_llseek(struct file
*file
, loff_t offset
, int whence
)
182 EXPORT_SYMBOL(noop_llseek
);
184 loff_t
no_llseek(struct file
*file
, loff_t offset
, int whence
)
188 EXPORT_SYMBOL(no_llseek
);
190 loff_t
default_llseek(struct file
*file
, loff_t offset
, int whence
)
192 struct inode
*inode
= file_inode(file
);
195 mutex_lock(&inode
->i_mutex
);
198 offset
+= i_size_read(inode
);
202 retval
= file
->f_pos
;
205 offset
+= file
->f_pos
;
209 * In the generic case the entire file is data, so as
210 * long as offset isn't at the end of the file then the
213 if (offset
>= inode
->i_size
) {
220 * There is a virtual hole at the end of the file, so
221 * as long as offset isn't i_size or larger, return
224 if (offset
>= inode
->i_size
) {
228 offset
= inode
->i_size
;
232 if (offset
>= 0 || unsigned_offsets(file
)) {
233 if (offset
!= file
->f_pos
) {
234 file
->f_pos
= offset
;
240 mutex_unlock(&inode
->i_mutex
);
243 EXPORT_SYMBOL(default_llseek
);
245 loff_t
vfs_llseek(struct file
*file
, loff_t offset
, int whence
)
247 loff_t (*fn
)(struct file
*, loff_t
, int);
250 if (file
->f_mode
& FMODE_LSEEK
) {
251 if (file
->f_op
&& file
->f_op
->llseek
)
252 fn
= file
->f_op
->llseek
;
254 return fn(file
, offset
, whence
);
256 EXPORT_SYMBOL(vfs_llseek
);
258 SYSCALL_DEFINE3(lseek
, unsigned int, fd
, off_t
, offset
, unsigned int, whence
)
261 struct fd f
= fdget(fd
);
266 if (whence
<= SEEK_MAX
) {
267 loff_t res
= vfs_llseek(f
.file
, offset
, whence
);
269 if (res
!= (loff_t
)retval
)
270 retval
= -EOVERFLOW
; /* LFS: should only happen on 32 bit platforms */
277 COMPAT_SYSCALL_DEFINE3(lseek
, unsigned int, fd
, compat_off_t
, offset
, unsigned int, whence
)
279 return sys_lseek(fd
, offset
, whence
);
283 #ifdef __ARCH_WANT_SYS_LLSEEK
284 SYSCALL_DEFINE5(llseek
, unsigned int, fd
, unsigned long, offset_high
,
285 unsigned long, offset_low
, loff_t __user
*, result
,
286 unsigned int, whence
)
289 struct fd f
= fdget(fd
);
296 if (whence
> SEEK_MAX
)
299 offset
= vfs_llseek(f
.file
, ((loff_t
) offset_high
<< 32) | offset_low
,
302 retval
= (int)offset
;
305 if (!copy_to_user(result
, &offset
, sizeof(offset
)))
315 * rw_verify_area doesn't like huge counts. We limit
316 * them to something that fits in "int" so that others
317 * won't have to do range checks all the time.
319 int rw_verify_area(int read_write
, struct file
*file
, const loff_t
*ppos
, size_t count
)
323 int retval
= -EINVAL
;
325 inode
= file_inode(file
);
326 if (unlikely((ssize_t
) count
< 0))
329 if (unlikely(pos
< 0)) {
330 if (!unsigned_offsets(file
))
332 if (count
>= -pos
) /* both values are in 0..LLONG_MAX */
334 } else if (unlikely((loff_t
) (pos
+ count
) < 0)) {
335 if (!unsigned_offsets(file
))
339 if (unlikely(inode
->i_flock
&& mandatory_lock(inode
))) {
340 retval
= locks_mandatory_area(
341 read_write
== READ
? FLOCK_VERIFY_READ
: FLOCK_VERIFY_WRITE
,
342 inode
, file
, pos
, count
);
346 retval
= security_file_permission(file
,
347 read_write
== READ
? MAY_READ
: MAY_WRITE
);
350 return count
> MAX_RW_COUNT
? MAX_RW_COUNT
: count
;
353 ssize_t
do_sync_read(struct file
*filp
, char __user
*buf
, size_t len
, loff_t
*ppos
)
355 struct iovec iov
= { .iov_base
= buf
, .iov_len
= len
};
359 init_sync_kiocb(&kiocb
, filp
);
360 kiocb
.ki_pos
= *ppos
;
362 kiocb
.ki_nbytes
= len
;
364 ret
= filp
->f_op
->aio_read(&kiocb
, &iov
, 1, kiocb
.ki_pos
);
365 if (-EIOCBQUEUED
== ret
)
366 ret
= wait_on_sync_kiocb(&kiocb
);
367 *ppos
= kiocb
.ki_pos
;
371 EXPORT_SYMBOL(do_sync_read
);
373 ssize_t
vfs_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*pos
)
377 if (!(file
->f_mode
& FMODE_READ
))
379 if (!file
->f_op
|| (!file
->f_op
->read
&& !file
->f_op
->aio_read
))
381 if (unlikely(!access_ok(VERIFY_WRITE
, buf
, count
)))
384 ret
= rw_verify_area(READ
, file
, pos
, count
);
387 if (file
->f_op
->read
)
388 ret
= file
->f_op
->read(file
, buf
, count
, pos
);
390 ret
= do_sync_read(file
, buf
, count
, pos
);
392 fsnotify_access(file
);
393 add_rchar(current
, ret
);
401 EXPORT_SYMBOL(vfs_read
);
403 ssize_t
do_sync_write(struct file
*filp
, const char __user
*buf
, size_t len
, loff_t
*ppos
)
405 struct iovec iov
= { .iov_base
= (void __user
*)buf
, .iov_len
= len
};
409 init_sync_kiocb(&kiocb
, filp
);
410 kiocb
.ki_pos
= *ppos
;
412 kiocb
.ki_nbytes
= len
;
414 ret
= filp
->f_op
->aio_write(&kiocb
, &iov
, 1, kiocb
.ki_pos
);
415 if (-EIOCBQUEUED
== ret
)
416 ret
= wait_on_sync_kiocb(&kiocb
);
417 *ppos
= kiocb
.ki_pos
;
421 EXPORT_SYMBOL(do_sync_write
);
423 ssize_t
__kernel_write(struct file
*file
, const char *buf
, size_t count
, loff_t
*pos
)
426 const char __user
*p
;
429 if (!file
->f_op
|| (!file
->f_op
->write
&& !file
->f_op
->aio_write
))
434 p
= (__force
const char __user
*)buf
;
435 if (count
> MAX_RW_COUNT
)
436 count
= MAX_RW_COUNT
;
437 if (file
->f_op
->write
)
438 ret
= file
->f_op
->write(file
, p
, count
, pos
);
440 ret
= do_sync_write(file
, p
, count
, pos
);
443 fsnotify_modify(file
);
444 add_wchar(current
, ret
);
450 ssize_t
vfs_write(struct file
*file
, const char __user
*buf
, size_t count
, loff_t
*pos
)
454 if (!(file
->f_mode
& FMODE_WRITE
))
456 if (!file
->f_op
|| (!file
->f_op
->write
&& !file
->f_op
->aio_write
))
458 if (unlikely(!access_ok(VERIFY_READ
, buf
, count
)))
461 ret
= rw_verify_area(WRITE
, file
, pos
, count
);
464 file_start_write(file
);
465 if (file
->f_op
->write
)
466 ret
= file
->f_op
->write(file
, buf
, count
, pos
);
468 ret
= do_sync_write(file
, buf
, count
, pos
);
470 fsnotify_modify(file
);
471 add_wchar(current
, ret
);
474 file_end_write(file
);
480 EXPORT_SYMBOL(vfs_write
);
482 static inline loff_t
file_pos_read(struct file
*file
)
487 static inline void file_pos_write(struct file
*file
, loff_t pos
)
492 SYSCALL_DEFINE3(read
, unsigned int, fd
, char __user
*, buf
, size_t, count
)
494 struct fd f
= fdget(fd
);
495 ssize_t ret
= -EBADF
;
498 loff_t pos
= file_pos_read(f
.file
);
499 ret
= vfs_read(f
.file
, buf
, count
, &pos
);
501 file_pos_write(f
.file
, pos
);
507 SYSCALL_DEFINE3(write
, unsigned int, fd
, const char __user
*, buf
,
510 struct fd f
= fdget(fd
);
511 ssize_t ret
= -EBADF
;
514 loff_t pos
= file_pos_read(f
.file
);
515 ret
= vfs_write(f
.file
, buf
, count
, &pos
);
517 file_pos_write(f
.file
, pos
);
524 SYSCALL_DEFINE4(pread64
, unsigned int, fd
, char __user
*, buf
,
525 size_t, count
, loff_t
, pos
)
528 ssize_t ret
= -EBADF
;
536 if (f
.file
->f_mode
& FMODE_PREAD
)
537 ret
= vfs_read(f
.file
, buf
, count
, &pos
);
544 SYSCALL_DEFINE4(pwrite64
, unsigned int, fd
, const char __user
*, buf
,
545 size_t, count
, loff_t
, pos
)
548 ssize_t ret
= -EBADF
;
556 if (f
.file
->f_mode
& FMODE_PWRITE
)
557 ret
= vfs_write(f
.file
, buf
, count
, &pos
);
565 * Reduce an iovec's length in-place. Return the resulting number of segments
567 unsigned long iov_shorten(struct iovec
*iov
, unsigned long nr_segs
, size_t to
)
569 unsigned long seg
= 0;
572 while (seg
< nr_segs
) {
574 if (len
+ iov
->iov_len
>= to
) {
575 iov
->iov_len
= to
- len
;
583 EXPORT_SYMBOL(iov_shorten
);
585 static ssize_t
do_sync_readv_writev(struct file
*filp
, const struct iovec
*iov
,
586 unsigned long nr_segs
, size_t len
, loff_t
*ppos
, iov_fn_t fn
)
591 init_sync_kiocb(&kiocb
, filp
);
592 kiocb
.ki_pos
= *ppos
;
594 kiocb
.ki_nbytes
= len
;
596 ret
= fn(&kiocb
, iov
, nr_segs
, kiocb
.ki_pos
);
597 if (ret
== -EIOCBQUEUED
)
598 ret
= wait_on_sync_kiocb(&kiocb
);
599 *ppos
= kiocb
.ki_pos
;
603 /* Do it by hand, with file-ops */
604 static ssize_t
do_loop_readv_writev(struct file
*filp
, struct iovec
*iov
,
605 unsigned long nr_segs
, loff_t
*ppos
, io_fn_t fn
)
607 struct iovec
*vector
= iov
;
610 while (nr_segs
> 0) {
615 base
= vector
->iov_base
;
616 len
= vector
->iov_len
;
620 nr
= fn(filp
, base
, len
, ppos
);
635 /* A write operation does a read from user space and vice versa */
636 #define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
638 ssize_t
rw_copy_check_uvector(int type
, const struct iovec __user
* uvector
,
639 unsigned long nr_segs
, unsigned long fast_segs
,
640 struct iovec
*fast_pointer
,
641 struct iovec
**ret_pointer
)
645 struct iovec
*iov
= fast_pointer
;
648 * SuS says "The readv() function *may* fail if the iovcnt argument
649 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
650 * traditionally returned zero for zero segments, so...
658 * First get the "struct iovec" from user memory and
659 * verify all the pointers
661 if (nr_segs
> UIO_MAXIOV
) {
665 if (nr_segs
> fast_segs
) {
666 iov
= kmalloc(nr_segs
*sizeof(struct iovec
), GFP_KERNEL
);
672 if (copy_from_user(iov
, uvector
, nr_segs
*sizeof(*uvector
))) {
678 * According to the Single Unix Specification we should return EINVAL
679 * if an element length is < 0 when cast to ssize_t or if the
680 * total length would overflow the ssize_t return value of the
683 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
687 for (seg
= 0; seg
< nr_segs
; seg
++) {
688 void __user
*buf
= iov
[seg
].iov_base
;
689 ssize_t len
= (ssize_t
)iov
[seg
].iov_len
;
691 /* see if we we're about to use an invalid len or if
692 * it's about to overflow ssize_t */
698 && unlikely(!access_ok(vrfy_dir(type
), buf
, len
))) {
702 if (len
> MAX_RW_COUNT
- ret
) {
703 len
= MAX_RW_COUNT
- ret
;
704 iov
[seg
].iov_len
= len
;
713 static ssize_t
do_readv_writev(int type
, struct file
*file
,
714 const struct iovec __user
* uvector
,
715 unsigned long nr_segs
, loff_t
*pos
)
718 struct iovec iovstack
[UIO_FASTIOV
];
719 struct iovec
*iov
= iovstack
;
729 ret
= rw_copy_check_uvector(type
, uvector
, nr_segs
,
730 ARRAY_SIZE(iovstack
), iovstack
, &iov
);
735 ret
= rw_verify_area(type
, file
, pos
, tot_len
);
741 fn
= file
->f_op
->read
;
742 fnv
= file
->f_op
->aio_read
;
744 fn
= (io_fn_t
)file
->f_op
->write
;
745 fnv
= file
->f_op
->aio_write
;
746 file_start_write(file
);
750 ret
= do_sync_readv_writev(file
, iov
, nr_segs
, tot_len
,
753 ret
= do_loop_readv_writev(file
, iov
, nr_segs
, pos
, fn
);
756 file_end_write(file
);
761 if ((ret
+ (type
== READ
)) > 0) {
763 fsnotify_access(file
);
765 fsnotify_modify(file
);
770 ssize_t
vfs_readv(struct file
*file
, const struct iovec __user
*vec
,
771 unsigned long vlen
, loff_t
*pos
)
773 if (!(file
->f_mode
& FMODE_READ
))
775 if (!file
->f_op
|| (!file
->f_op
->aio_read
&& !file
->f_op
->read
))
778 return do_readv_writev(READ
, file
, vec
, vlen
, pos
);
781 EXPORT_SYMBOL(vfs_readv
);
783 ssize_t
vfs_writev(struct file
*file
, const struct iovec __user
*vec
,
784 unsigned long vlen
, loff_t
*pos
)
786 if (!(file
->f_mode
& FMODE_WRITE
))
788 if (!file
->f_op
|| (!file
->f_op
->aio_write
&& !file
->f_op
->write
))
791 return do_readv_writev(WRITE
, file
, vec
, vlen
, pos
);
794 EXPORT_SYMBOL(vfs_writev
);
796 SYSCALL_DEFINE3(readv
, unsigned long, fd
, const struct iovec __user
*, vec
,
799 struct fd f
= fdget(fd
);
800 ssize_t ret
= -EBADF
;
803 loff_t pos
= file_pos_read(f
.file
);
804 ret
= vfs_readv(f
.file
, vec
, vlen
, &pos
);
806 file_pos_write(f
.file
, pos
);
811 add_rchar(current
, ret
);
816 SYSCALL_DEFINE3(writev
, unsigned long, fd
, const struct iovec __user
*, vec
,
819 struct fd f
= fdget(fd
);
820 ssize_t ret
= -EBADF
;
823 loff_t pos
= file_pos_read(f
.file
);
824 ret
= vfs_writev(f
.file
, vec
, vlen
, &pos
);
826 file_pos_write(f
.file
, pos
);
831 add_wchar(current
, ret
);
836 static inline loff_t
pos_from_hilo(unsigned long high
, unsigned long low
)
838 #define HALF_LONG_BITS (BITS_PER_LONG / 2)
839 return (((loff_t
)high
<< HALF_LONG_BITS
) << HALF_LONG_BITS
) | low
;
842 SYSCALL_DEFINE5(preadv
, unsigned long, fd
, const struct iovec __user
*, vec
,
843 unsigned long, vlen
, unsigned long, pos_l
, unsigned long, pos_h
)
845 loff_t pos
= pos_from_hilo(pos_h
, pos_l
);
847 ssize_t ret
= -EBADF
;
855 if (f
.file
->f_mode
& FMODE_PREAD
)
856 ret
= vfs_readv(f
.file
, vec
, vlen
, &pos
);
861 add_rchar(current
, ret
);
866 SYSCALL_DEFINE5(pwritev
, unsigned long, fd
, const struct iovec __user
*, vec
,
867 unsigned long, vlen
, unsigned long, pos_l
, unsigned long, pos_h
)
869 loff_t pos
= pos_from_hilo(pos_h
, pos_l
);
871 ssize_t ret
= -EBADF
;
879 if (f
.file
->f_mode
& FMODE_PWRITE
)
880 ret
= vfs_writev(f
.file
, vec
, vlen
, &pos
);
885 add_wchar(current
, ret
);
892 static ssize_t
compat_do_readv_writev(int type
, struct file
*file
,
893 const struct compat_iovec __user
*uvector
,
894 unsigned long nr_segs
, loff_t
*pos
)
896 compat_ssize_t tot_len
;
897 struct iovec iovstack
[UIO_FASTIOV
];
898 struct iovec
*iov
= iovstack
;
908 if (!access_ok(VERIFY_READ
, uvector
, nr_segs
*sizeof(*uvector
)))
911 ret
= compat_rw_copy_check_uvector(type
, uvector
, nr_segs
,
912 UIO_FASTIOV
, iovstack
, &iov
);
917 ret
= rw_verify_area(type
, file
, pos
, tot_len
);
923 fn
= file
->f_op
->read
;
924 fnv
= file
->f_op
->aio_read
;
926 fn
= (io_fn_t
)file
->f_op
->write
;
927 fnv
= file
->f_op
->aio_write
;
928 file_start_write(file
);
932 ret
= do_sync_readv_writev(file
, iov
, nr_segs
, tot_len
,
935 ret
= do_loop_readv_writev(file
, iov
, nr_segs
, pos
, fn
);
938 file_end_write(file
);
943 if ((ret
+ (type
== READ
)) > 0) {
945 fsnotify_access(file
);
947 fsnotify_modify(file
);
952 static size_t compat_readv(struct file
*file
,
953 const struct compat_iovec __user
*vec
,
954 unsigned long vlen
, loff_t
*pos
)
956 ssize_t ret
= -EBADF
;
958 if (!(file
->f_mode
& FMODE_READ
))
962 if (!file
->f_op
|| (!file
->f_op
->aio_read
&& !file
->f_op
->read
))
965 ret
= compat_do_readv_writev(READ
, file
, vec
, vlen
, pos
);
969 add_rchar(current
, ret
);
974 COMPAT_SYSCALL_DEFINE3(readv
, unsigned long, fd
,
975 const struct compat_iovec __user
*,vec
,
978 struct fd f
= fdget(fd
);
985 ret
= compat_readv(f
.file
, vec
, vlen
, &pos
);
992 COMPAT_SYSCALL_DEFINE4(preadv64
, unsigned long, fd
,
993 const struct compat_iovec __user
*,vec
,
994 unsigned long, vlen
, loff_t
, pos
)
1005 if (f
.file
->f_mode
& FMODE_PREAD
)
1006 ret
= compat_readv(f
.file
, vec
, vlen
, &pos
);
1011 COMPAT_SYSCALL_DEFINE5(preadv
, unsigned long, fd
,
1012 const struct compat_iovec __user
*,vec
,
1013 unsigned long, vlen
, u32
, pos_low
, u32
, pos_high
)
1015 loff_t pos
= ((loff_t
)pos_high
<< 32) | pos_low
;
1016 return compat_sys_preadv64(fd
, vec
, vlen
, pos
);
1019 static size_t compat_writev(struct file
*file
,
1020 const struct compat_iovec __user
*vec
,
1021 unsigned long vlen
, loff_t
*pos
)
1023 ssize_t ret
= -EBADF
;
1025 if (!(file
->f_mode
& FMODE_WRITE
))
1029 if (!file
->f_op
|| (!file
->f_op
->aio_write
&& !file
->f_op
->write
))
1032 ret
= compat_do_readv_writev(WRITE
, file
, vec
, vlen
, pos
);
1036 add_wchar(current
, ret
);
1041 COMPAT_SYSCALL_DEFINE3(writev
, unsigned long, fd
,
1042 const struct compat_iovec __user
*, vec
,
1043 unsigned long, vlen
)
1045 struct fd f
= fdget(fd
);
1051 pos
= f
.file
->f_pos
;
1052 ret
= compat_writev(f
.file
, vec
, vlen
, &pos
);
1054 f
.file
->f_pos
= pos
;
1059 COMPAT_SYSCALL_DEFINE4(pwritev64
, unsigned long, fd
,
1060 const struct compat_iovec __user
*,vec
,
1061 unsigned long, vlen
, loff_t
, pos
)
1072 if (f
.file
->f_mode
& FMODE_PWRITE
)
1073 ret
= compat_writev(f
.file
, vec
, vlen
, &pos
);
1078 COMPAT_SYSCALL_DEFINE5(pwritev
, unsigned long, fd
,
1079 const struct compat_iovec __user
*,vec
,
1080 unsigned long, vlen
, u32
, pos_low
, u32
, pos_high
)
1082 loff_t pos
= ((loff_t
)pos_high
<< 32) | pos_low
;
1083 return compat_sys_pwritev64(fd
, vec
, vlen
, pos
);
1087 static ssize_t
do_sendfile(int out_fd
, int in_fd
, loff_t
*ppos
,
1088 size_t count
, loff_t max
)
1091 struct inode
*in_inode
, *out_inode
;
1098 * Get input file, and verify that it is ok..
1104 if (!(in
.file
->f_mode
& FMODE_READ
))
1108 pos
= in
.file
->f_pos
;
1111 if (!(in
.file
->f_mode
& FMODE_PREAD
))
1114 retval
= rw_verify_area(READ
, in
.file
, &pos
, count
);
1120 * Get output file, and verify that it is ok..
1123 out
= fdget(out_fd
);
1126 if (!(out
.file
->f_mode
& FMODE_WRITE
))
1129 in_inode
= file_inode(in
.file
);
1130 out_inode
= file_inode(out
.file
);
1131 out_pos
= out
.file
->f_pos
;
1132 retval
= rw_verify_area(WRITE
, out
.file
, &out_pos
, count
);
1138 max
= min(in_inode
->i_sb
->s_maxbytes
, out_inode
->i_sb
->s_maxbytes
);
1140 if (unlikely(pos
+ count
> max
)) {
1141 retval
= -EOVERFLOW
;
1150 * We need to debate whether we can enable this or not. The
1151 * man page documents EAGAIN return for the output at least,
1152 * and the application is arguably buggy if it doesn't expect
1153 * EAGAIN on a non-blocking file descriptor.
1155 if (in
.file
->f_flags
& O_NONBLOCK
)
1156 fl
= SPLICE_F_NONBLOCK
;
1158 file_start_write(out
.file
);
1159 retval
= do_splice_direct(in
.file
, &pos
, out
.file
, &out_pos
, count
, fl
);
1160 file_end_write(out
.file
);
1163 add_rchar(current
, retval
);
1164 add_wchar(current
, retval
);
1165 fsnotify_access(in
.file
);
1166 fsnotify_modify(out
.file
);
1167 out
.file
->f_pos
= out_pos
;
1171 in
.file
->f_pos
= pos
;
1177 retval
= -EOVERFLOW
;
1187 SYSCALL_DEFINE4(sendfile
, int, out_fd
, int, in_fd
, off_t __user
*, offset
, size_t, count
)
1194 if (unlikely(get_user(off
, offset
)))
1197 ret
= do_sendfile(out_fd
, in_fd
, &pos
, count
, MAX_NON_LFS
);
1198 if (unlikely(put_user(pos
, offset
)))
1203 return do_sendfile(out_fd
, in_fd
, NULL
, count
, 0);
1206 SYSCALL_DEFINE4(sendfile64
, int, out_fd
, int, in_fd
, loff_t __user
*, offset
, size_t, count
)
1212 if (unlikely(copy_from_user(&pos
, offset
, sizeof(loff_t
))))
1214 ret
= do_sendfile(out_fd
, in_fd
, &pos
, count
, 0);
1215 if (unlikely(put_user(pos
, offset
)))
1220 return do_sendfile(out_fd
, in_fd
, NULL
, count
, 0);
1223 #ifdef CONFIG_COMPAT
1224 COMPAT_SYSCALL_DEFINE4(sendfile
, int, out_fd
, int, in_fd
,
1225 compat_off_t __user
*, offset
, compat_size_t
, count
)
1232 if (unlikely(get_user(off
, offset
)))
1235 ret
= do_sendfile(out_fd
, in_fd
, &pos
, count
, MAX_NON_LFS
);
1236 if (unlikely(put_user(pos
, offset
)))
1241 return do_sendfile(out_fd
, in_fd
, NULL
, count
, 0);
1244 COMPAT_SYSCALL_DEFINE4(sendfile64
, int, out_fd
, int, in_fd
,
1245 compat_loff_t __user
*, offset
, compat_size_t
, count
)
1251 if (unlikely(copy_from_user(&pos
, offset
, sizeof(loff_t
))))
1253 ret
= do_sendfile(out_fd
, in_fd
, &pos
, count
, 0);
1254 if (unlikely(put_user(pos
, offset
)))
1259 return do_sendfile(out_fd
, in_fd
, NULL
, count
, 0);