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;
45 * vfs_setpos - update the file offset for lseek
46 * @file: file structure in question
47 * @offset: file offset to seek to
48 * @maxsize: maximum file size
50 * This is a low-level filesystem helper for updating the file offset to
51 * the value specified by @offset if the given offset is valid and it is
52 * not equal to the current file offset.
54 * Return the specified offset on success and -EINVAL on invalid offset.
56 loff_t vfs_setpos(struct file *file, loff_t offset, loff_t maxsize)
58 if (offset < 0 && !unsigned_offsets(file))
63 if (offset != file->f_pos) {
69 EXPORT_SYMBOL(vfs_setpos);
72 * generic_file_llseek_size - generic llseek implementation for regular files
73 * @file: file structure to seek on
74 * @offset: file offset to seek to
75 * @whence: type of seek
76 * @size: max size of this file in file system
77 * @eof: offset used for SEEK_END position
79 * This is a variant of generic_file_llseek that allows passing in a custom
80 * maximum file size and a custom EOF position, for e.g. hashed directories
83 * SEEK_SET and SEEK_END are unsynchronized (but atomic on 64bit platforms)
84 * SEEK_CUR is synchronized against other SEEK_CURs, but not read/writes.
85 * read/writes behave like SEEK_SET against seeks.
88 generic_file_llseek_size(struct file *file, loff_t offset, int whence,
89 loff_t maxsize, loff_t eof)
97 * Here we special-case the lseek(fd, 0, SEEK_CUR)
98 * position-querying operation. Avoid rewriting the "same"
99 * f_pos value back to the file because a concurrent read(),
100 * write() or lseek() might have altered it
105 * f_lock protects against read/modify/write race with other
106 * SEEK_CURs. Note that parallel writes and reads behave
109 spin_lock(&file->f_lock);
110 offset = vfs_setpos(file, file->f_pos + offset, maxsize);
111 spin_unlock(&file->f_lock);
115 * In the generic case the entire file is data, so as long as
116 * offset isn't at the end of the file then the offset is data.
123 * There is a virtual hole at the end of the file, so as long as
124 * offset isn't i_size or larger, return i_size.
132 return vfs_setpos(file, offset, maxsize);
134 EXPORT_SYMBOL(generic_file_llseek_size);
137 * generic_file_llseek - generic llseek implementation for regular files
138 * @file: file structure to seek on
139 * @offset: file offset to seek to
140 * @whence: type of seek
142 * This is a generic implemenation of ->llseek useable for all normal local
143 * filesystems. It just updates the file offset to the value specified by
144 * @offset and @whence.
146 loff_t generic_file_llseek(struct file *file, loff_t offset, int whence)
148 struct inode *inode = file->f_mapping->host;
150 return generic_file_llseek_size(file, offset, whence,
151 inode->i_sb->s_maxbytes,
154 EXPORT_SYMBOL(generic_file_llseek);
157 * fixed_size_llseek - llseek implementation for fixed-sized devices
158 * @file: file structure to seek on
159 * @offset: file offset to seek to
160 * @whence: type of seek
161 * @size: size of the file
164 loff_t fixed_size_llseek(struct file *file, loff_t offset, int whence, loff_t size)
167 case SEEK_SET: case SEEK_CUR: case SEEK_END:
168 return generic_file_llseek_size(file, offset, whence,
174 EXPORT_SYMBOL(fixed_size_llseek);
177 * noop_llseek - No Operation Performed llseek implementation
178 * @file: file structure to seek on
179 * @offset: file offset to seek to
180 * @whence: type of seek
182 * This is an implementation of ->llseek useable for the rare special case when
183 * userspace expects the seek to succeed but the (device) file is actually not
184 * able to perform the seek. In this case you use noop_llseek() instead of
185 * falling back to the default implementation of ->llseek.
187 loff_t noop_llseek(struct file *file, loff_t offset, int whence)
191 EXPORT_SYMBOL(noop_llseek);
193 loff_t no_llseek(struct file *file, loff_t offset, int whence)
197 EXPORT_SYMBOL(no_llseek);
199 loff_t default_llseek(struct file *file, loff_t offset, int whence)
201 struct inode *inode = file_inode(file);
204 mutex_lock(&inode->i_mutex);
207 offset += i_size_read(inode);
211 retval = file->f_pos;
214 offset += file->f_pos;
218 * In the generic case the entire file is data, so as
219 * long as offset isn't at the end of the file then the
222 if (offset >= inode->i_size) {
229 * There is a virtual hole at the end of the file, so
230 * as long as offset isn't i_size or larger, return
233 if (offset >= inode->i_size) {
237 offset = inode->i_size;
241 if (offset >= 0 || unsigned_offsets(file)) {
242 if (offset != file->f_pos) {
243 file->f_pos = offset;
249 mutex_unlock(&inode->i_mutex);
252 EXPORT_SYMBOL(default_llseek);
254 loff_t vfs_llseek(struct file *file, loff_t offset, int whence)
256 loff_t (*fn)(struct file *, loff_t, int);
259 if (file->f_mode & FMODE_LSEEK) {
260 if (file->f_op->llseek)
261 fn = file->f_op->llseek;
263 return fn(file, offset, whence);
265 EXPORT_SYMBOL(vfs_llseek);
268 * We only lock f_pos if we have threads or if the file might be
269 * shared with another process. In both cases we'll have an elevated
270 * file count (done either by fdget() or by fork()).
272 static inline struct fd fdget_pos(int fd)
274 struct fd f = fdget(fd);
275 struct file *file = f.file;
277 if (file && (file->f_mode & FMODE_ATOMIC_POS)) {
278 if (file_count(file) > 1) {
279 f.flags |= FDPUT_POS_UNLOCK;
280 mutex_lock(&file->f_pos_lock);
286 static inline void fdput_pos(struct fd f)
288 if (f.flags & FDPUT_POS_UNLOCK)
289 mutex_unlock(&f.file->f_pos_lock);
293 SYSCALL_DEFINE3(lseek, unsigned int, fd, off_t, offset, unsigned int, whence)
296 struct fd f = fdget_pos(fd);
301 if (whence <= SEEK_MAX) {
302 loff_t res = vfs_llseek(f.file, offset, whence);
304 if (res != (loff_t)retval)
305 retval = -EOVERFLOW; /* LFS: should only happen on 32 bit platforms */
312 COMPAT_SYSCALL_DEFINE3(lseek, unsigned int, fd, compat_off_t, offset, unsigned int, whence)
314 return sys_lseek(fd, offset, whence);
318 #ifdef __ARCH_WANT_SYS_LLSEEK
319 SYSCALL_DEFINE5(llseek, unsigned int, fd, unsigned long, offset_high,
320 unsigned long, offset_low, loff_t __user *, result,
321 unsigned int, whence)
324 struct fd f = fdget(fd);
331 if (whence > SEEK_MAX)
334 offset = vfs_llseek(f.file, ((loff_t) offset_high << 32) | offset_low,
337 retval = (int)offset;
340 if (!copy_to_user(result, &offset, sizeof(offset)))
350 * rw_verify_area doesn't like huge counts. We limit
351 * them to something that fits in "int" so that others
352 * won't have to do range checks all the time.
354 int rw_verify_area(int read_write, struct file *file, const loff_t *ppos, size_t count)
358 int retval = -EINVAL;
360 inode = file_inode(file);
361 if (unlikely((ssize_t) count < 0))
364 if (unlikely(pos < 0)) {
365 if (!unsigned_offsets(file))
367 if (count >= -pos) /* both values are in 0..LLONG_MAX */
369 } else if (unlikely((loff_t) (pos + count) < 0)) {
370 if (!unsigned_offsets(file))
374 if (unlikely(inode->i_flock && mandatory_lock(inode))) {
375 retval = locks_mandatory_area(
376 read_write == READ ? FLOCK_VERIFY_READ : FLOCK_VERIFY_WRITE,
377 inode, file, pos, count);
381 retval = security_file_permission(file,
382 read_write == READ ? MAY_READ : MAY_WRITE);
385 return count > MAX_RW_COUNT ? MAX_RW_COUNT : count;
388 ssize_t do_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
390 struct iovec iov = { .iov_base = buf, .iov_len = len };
394 init_sync_kiocb(&kiocb, filp);
395 kiocb.ki_pos = *ppos;
396 kiocb.ki_nbytes = len;
398 ret = filp->f_op->aio_read(&kiocb, &iov, 1, kiocb.ki_pos);
399 if (-EIOCBQUEUED == ret)
400 ret = wait_on_sync_kiocb(&kiocb);
401 *ppos = kiocb.ki_pos;
405 EXPORT_SYMBOL(do_sync_read);
407 ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
411 if (!(file->f_mode & FMODE_READ))
413 if (!file->f_op->read && !file->f_op->aio_read)
415 if (unlikely(!access_ok(VERIFY_WRITE, buf, count)))
418 ret = rw_verify_area(READ, file, pos, count);
421 if (file->f_op->read)
422 ret = file->f_op->read(file, buf, count, pos);
424 ret = do_sync_read(file, buf, count, pos);
426 fsnotify_access(file);
427 add_rchar(current, ret);
435 EXPORT_SYMBOL(vfs_read);
437 ssize_t do_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
439 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len };
443 init_sync_kiocb(&kiocb, filp);
444 kiocb.ki_pos = *ppos;
445 kiocb.ki_nbytes = len;
447 ret = filp->f_op->aio_write(&kiocb, &iov, 1, kiocb.ki_pos);
448 if (-EIOCBQUEUED == ret)
449 ret = wait_on_sync_kiocb(&kiocb);
450 *ppos = kiocb.ki_pos;
454 EXPORT_SYMBOL(do_sync_write);
456 ssize_t __kernel_write(struct file *file, const char *buf, size_t count, loff_t *pos)
459 const char __user *p;
462 if (!file->f_op->write && !file->f_op->aio_write)
467 p = (__force const char __user *)buf;
468 if (count > MAX_RW_COUNT)
469 count = MAX_RW_COUNT;
470 if (file->f_op->write)
471 ret = file->f_op->write(file, p, count, pos);
473 ret = do_sync_write(file, p, count, pos);
476 fsnotify_modify(file);
477 add_wchar(current, ret);
483 ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
487 if (!(file->f_mode & FMODE_WRITE))
489 if (!file->f_op->write && !file->f_op->aio_write)
491 if (unlikely(!access_ok(VERIFY_READ, buf, count)))
494 ret = rw_verify_area(WRITE, file, pos, count);
497 file_start_write(file);
498 if (file->f_op->write)
499 ret = file->f_op->write(file, buf, count, pos);
501 ret = do_sync_write(file, buf, count, pos);
503 fsnotify_modify(file);
504 add_wchar(current, ret);
507 file_end_write(file);
513 EXPORT_SYMBOL(vfs_write);
515 static inline loff_t file_pos_read(struct file *file)
520 static inline void file_pos_write(struct file *file, loff_t pos)
525 SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count)
527 struct fd f = fdget_pos(fd);
528 ssize_t ret = -EBADF;
531 loff_t pos = file_pos_read(f.file);
532 ret = vfs_read(f.file, buf, count, &pos);
534 file_pos_write(f.file, pos);
540 SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf,
543 struct fd f = fdget_pos(fd);
544 ssize_t ret = -EBADF;
547 loff_t pos = file_pos_read(f.file);
548 ret = vfs_write(f.file, buf, count, &pos);
550 file_pos_write(f.file, pos);
557 SYSCALL_DEFINE4(pread64, unsigned int, fd, char __user *, buf,
558 size_t, count, loff_t, pos)
561 ssize_t ret = -EBADF;
569 if (f.file->f_mode & FMODE_PREAD)
570 ret = vfs_read(f.file, buf, count, &pos);
577 SYSCALL_DEFINE4(pwrite64, unsigned int, fd, const char __user *, buf,
578 size_t, count, loff_t, pos)
581 ssize_t ret = -EBADF;
589 if (f.file->f_mode & FMODE_PWRITE)
590 ret = vfs_write(f.file, buf, count, &pos);
598 * Reduce an iovec's length in-place. Return the resulting number of segments
600 unsigned long iov_shorten(struct iovec *iov, unsigned long nr_segs, size_t to)
602 unsigned long seg = 0;
605 while (seg < nr_segs) {
607 if (len + iov->iov_len >= to) {
608 iov->iov_len = to - len;
616 EXPORT_SYMBOL(iov_shorten);
618 static ssize_t do_sync_readv_writev(struct file *filp, const struct iovec *iov,
619 unsigned long nr_segs, size_t len, loff_t *ppos, iov_fn_t fn)
624 init_sync_kiocb(&kiocb, filp);
625 kiocb.ki_pos = *ppos;
626 kiocb.ki_nbytes = len;
628 ret = fn(&kiocb, iov, nr_segs, kiocb.ki_pos);
629 if (ret == -EIOCBQUEUED)
630 ret = wait_on_sync_kiocb(&kiocb);
631 *ppos = kiocb.ki_pos;
635 /* Do it by hand, with file-ops */
636 static ssize_t do_loop_readv_writev(struct file *filp, struct iovec *iov,
637 unsigned long nr_segs, loff_t *ppos, io_fn_t fn)
639 struct iovec *vector = iov;
642 while (nr_segs > 0) {
647 base = vector->iov_base;
648 len = vector->iov_len;
652 nr = fn(filp, base, len, ppos);
667 /* A write operation does a read from user space and vice versa */
668 #define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
670 ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
671 unsigned long nr_segs, unsigned long fast_segs,
672 struct iovec *fast_pointer,
673 struct iovec **ret_pointer)
677 struct iovec *iov = fast_pointer;
680 * SuS says "The readv() function *may* fail if the iovcnt argument
681 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
682 * traditionally returned zero for zero segments, so...
690 * First get the "struct iovec" from user memory and
691 * verify all the pointers
693 if (nr_segs > UIO_MAXIOV) {
697 if (nr_segs > fast_segs) {
698 iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
704 if (copy_from_user(iov, uvector, nr_segs*sizeof(*uvector))) {
710 * According to the Single Unix Specification we should return EINVAL
711 * if an element length is < 0 when cast to ssize_t or if the
712 * total length would overflow the ssize_t return value of the
715 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
719 for (seg = 0; seg < nr_segs; seg++) {
720 void __user *buf = iov[seg].iov_base;
721 ssize_t len = (ssize_t)iov[seg].iov_len;
723 /* see if we we're about to use an invalid len or if
724 * it's about to overflow ssize_t */
730 && unlikely(!access_ok(vrfy_dir(type), buf, len))) {
734 if (len > MAX_RW_COUNT - ret) {
735 len = MAX_RW_COUNT - ret;
736 iov[seg].iov_len = len;
745 static ssize_t do_readv_writev(int type, struct file *file,
746 const struct iovec __user * uvector,
747 unsigned long nr_segs, loff_t *pos)
750 struct iovec iovstack[UIO_FASTIOV];
751 struct iovec *iov = iovstack;
756 ret = rw_copy_check_uvector(type, uvector, nr_segs,
757 ARRAY_SIZE(iovstack), iovstack, &iov);
762 ret = rw_verify_area(type, file, pos, tot_len);
768 fn = file->f_op->read;
769 fnv = file->f_op->aio_read;
771 fn = (io_fn_t)file->f_op->write;
772 fnv = file->f_op->aio_write;
773 file_start_write(file);
777 ret = do_sync_readv_writev(file, iov, nr_segs, tot_len,
780 ret = do_loop_readv_writev(file, iov, nr_segs, pos, fn);
783 file_end_write(file);
788 if ((ret + (type == READ)) > 0) {
790 fsnotify_access(file);
792 fsnotify_modify(file);
797 ssize_t vfs_readv(struct file *file, const struct iovec __user *vec,
798 unsigned long vlen, loff_t *pos)
800 if (!(file->f_mode & FMODE_READ))
802 if (!file->f_op->aio_read && !file->f_op->read)
805 return do_readv_writev(READ, file, vec, vlen, pos);
808 EXPORT_SYMBOL(vfs_readv);
810 ssize_t vfs_writev(struct file *file, const struct iovec __user *vec,
811 unsigned long vlen, loff_t *pos)
813 if (!(file->f_mode & FMODE_WRITE))
815 if (!file->f_op->aio_write && !file->f_op->write)
818 return do_readv_writev(WRITE, file, vec, vlen, pos);
821 EXPORT_SYMBOL(vfs_writev);
823 SYSCALL_DEFINE3(readv, unsigned long, fd, const struct iovec __user *, vec,
826 struct fd f = fdget_pos(fd);
827 ssize_t ret = -EBADF;
830 loff_t pos = file_pos_read(f.file);
831 ret = vfs_readv(f.file, vec, vlen, &pos);
833 file_pos_write(f.file, pos);
838 add_rchar(current, ret);
843 SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec __user *, vec,
846 struct fd f = fdget_pos(fd);
847 ssize_t ret = -EBADF;
850 loff_t pos = file_pos_read(f.file);
851 ret = vfs_writev(f.file, vec, vlen, &pos);
853 file_pos_write(f.file, pos);
858 add_wchar(current, ret);
863 static inline loff_t pos_from_hilo(unsigned long high, unsigned long low)
865 #define HALF_LONG_BITS (BITS_PER_LONG / 2)
866 return (((loff_t)high << HALF_LONG_BITS) << HALF_LONG_BITS) | low;
869 SYSCALL_DEFINE5(preadv, unsigned long, fd, const struct iovec __user *, vec,
870 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
872 loff_t pos = pos_from_hilo(pos_h, pos_l);
874 ssize_t ret = -EBADF;
882 if (f.file->f_mode & FMODE_PREAD)
883 ret = vfs_readv(f.file, vec, vlen, &pos);
888 add_rchar(current, ret);
893 SYSCALL_DEFINE5(pwritev, unsigned long, fd, const struct iovec __user *, vec,
894 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
896 loff_t pos = pos_from_hilo(pos_h, pos_l);
898 ssize_t ret = -EBADF;
906 if (f.file->f_mode & FMODE_PWRITE)
907 ret = vfs_writev(f.file, vec, vlen, &pos);
912 add_wchar(current, ret);
919 static ssize_t compat_do_readv_writev(int type, struct file *file,
920 const struct compat_iovec __user *uvector,
921 unsigned long nr_segs, loff_t *pos)
923 compat_ssize_t tot_len;
924 struct iovec iovstack[UIO_FASTIOV];
925 struct iovec *iov = iovstack;
930 ret = compat_rw_copy_check_uvector(type, uvector, nr_segs,
931 UIO_FASTIOV, iovstack, &iov);
936 ret = rw_verify_area(type, file, pos, tot_len);
942 fn = file->f_op->read;
943 fnv = file->f_op->aio_read;
945 fn = (io_fn_t)file->f_op->write;
946 fnv = file->f_op->aio_write;
947 file_start_write(file);
951 ret = do_sync_readv_writev(file, iov, nr_segs, tot_len,
954 ret = do_loop_readv_writev(file, iov, nr_segs, pos, fn);
957 file_end_write(file);
962 if ((ret + (type == READ)) > 0) {
964 fsnotify_access(file);
966 fsnotify_modify(file);
971 static size_t compat_readv(struct file *file,
972 const struct compat_iovec __user *vec,
973 unsigned long vlen, loff_t *pos)
975 ssize_t ret = -EBADF;
977 if (!(file->f_mode & FMODE_READ))
981 if (!file->f_op->aio_read && !file->f_op->read)
984 ret = compat_do_readv_writev(READ, file, vec, vlen, pos);
988 add_rchar(current, ret);
993 COMPAT_SYSCALL_DEFINE3(readv, compat_ulong_t, fd,
994 const struct compat_iovec __user *,vec,
995 compat_ulong_t, vlen)
997 struct fd f = fdget_pos(fd);
1003 pos = f.file->f_pos;
1004 ret = compat_readv(f.file, vec, vlen, &pos);
1006 f.file->f_pos = pos;
1011 COMPAT_SYSCALL_DEFINE4(preadv64, unsigned long, fd,
1012 const struct compat_iovec __user *,vec,
1013 unsigned long, vlen, loff_t, pos)
1024 if (f.file->f_mode & FMODE_PREAD)
1025 ret = compat_readv(f.file, vec, vlen, &pos);
1030 COMPAT_SYSCALL_DEFINE5(preadv, compat_ulong_t, fd,
1031 const struct compat_iovec __user *,vec,
1032 compat_ulong_t, vlen, u32, pos_low, u32, pos_high)
1034 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
1035 return compat_sys_preadv64(fd, vec, vlen, pos);
1038 static size_t compat_writev(struct file *file,
1039 const struct compat_iovec __user *vec,
1040 unsigned long vlen, loff_t *pos)
1042 ssize_t ret = -EBADF;
1044 if (!(file->f_mode & FMODE_WRITE))
1048 if (!file->f_op->aio_write && !file->f_op->write)
1051 ret = compat_do_readv_writev(WRITE, file, vec, vlen, pos);
1055 add_wchar(current, ret);
1060 COMPAT_SYSCALL_DEFINE3(writev, compat_ulong_t, fd,
1061 const struct compat_iovec __user *, vec,
1062 compat_ulong_t, vlen)
1064 struct fd f = fdget_pos(fd);
1070 pos = f.file->f_pos;
1071 ret = compat_writev(f.file, vec, vlen, &pos);
1073 f.file->f_pos = pos;
1078 COMPAT_SYSCALL_DEFINE4(pwritev64, unsigned long, fd,
1079 const struct compat_iovec __user *,vec,
1080 unsigned long, vlen, loff_t, pos)
1091 if (f.file->f_mode & FMODE_PWRITE)
1092 ret = compat_writev(f.file, vec, vlen, &pos);
1097 COMPAT_SYSCALL_DEFINE5(pwritev, compat_ulong_t, fd,
1098 const struct compat_iovec __user *,vec,
1099 compat_ulong_t, vlen, u32, pos_low, u32, pos_high)
1101 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
1102 return compat_sys_pwritev64(fd, vec, vlen, pos);
1106 static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,
1107 size_t count, loff_t max)
1110 struct inode *in_inode, *out_inode;
1117 * Get input file, and verify that it is ok..
1123 if (!(in.file->f_mode & FMODE_READ))
1127 pos = in.file->f_pos;
1130 if (!(in.file->f_mode & FMODE_PREAD))
1133 retval = rw_verify_area(READ, in.file, &pos, count);
1139 * Get output file, and verify that it is ok..
1142 out = fdget(out_fd);
1145 if (!(out.file->f_mode & FMODE_WRITE))
1148 in_inode = file_inode(in.file);
1149 out_inode = file_inode(out.file);
1150 out_pos = out.file->f_pos;
1151 retval = rw_verify_area(WRITE, out.file, &out_pos, count);
1157 max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes);
1159 if (unlikely(pos + count > max)) {
1160 retval = -EOVERFLOW;
1169 * We need to debate whether we can enable this or not. The
1170 * man page documents EAGAIN return for the output at least,
1171 * and the application is arguably buggy if it doesn't expect
1172 * EAGAIN on a non-blocking file descriptor.
1174 if (in.file->f_flags & O_NONBLOCK)
1175 fl = SPLICE_F_NONBLOCK;
1177 file_start_write(out.file);
1178 retval = do_splice_direct(in.file, &pos, out.file, &out_pos, count, fl);
1179 file_end_write(out.file);
1182 add_rchar(current, retval);
1183 add_wchar(current, retval);
1184 fsnotify_access(in.file);
1185 fsnotify_modify(out.file);
1186 out.file->f_pos = out_pos;
1190 in.file->f_pos = pos;
1196 retval = -EOVERFLOW;
1206 SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd, off_t __user *, offset, size_t, count)
1213 if (unlikely(get_user(off, offset)))
1216 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
1217 if (unlikely(put_user(pos, offset)))
1222 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1225 SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd, loff_t __user *, offset, size_t, count)
1231 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
1233 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
1234 if (unlikely(put_user(pos, offset)))
1239 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1242 #ifdef CONFIG_COMPAT
1243 COMPAT_SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd,
1244 compat_off_t __user *, offset, compat_size_t, count)
1251 if (unlikely(get_user(off, offset)))
1254 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
1255 if (unlikely(put_user(pos, offset)))
1260 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1263 COMPAT_SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd,
1264 compat_loff_t __user *, offset, compat_size_t, count)
1270 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
1272 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
1273 if (unlikely(put_user(pos, offset)))
1278 return do_sendfile(out_fd, in_fd, NULL, count, 0);