Add linux-next specific files for 20110831
[linux-2.6/next.git] / fs / read_write.c
blob7a220bec217cf88b63947c63c4ac866b91b0ba45
1 /*
2 * linux/fs/read_write.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
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/fsnotify.h>
13 #include <linux/security.h>
14 #include <linux/module.h>
15 #include <linux/syscalls.h>
16 #include <linux/pagemap.h>
17 #include <linux/splice.h>
18 #include "read_write.h"
20 #include <asm/uaccess.h>
21 #include <asm/unistd.h>
23 const struct file_operations generic_ro_fops = {
24 .llseek = generic_file_llseek,
25 .read = do_sync_read,
26 .aio_read = generic_file_aio_read,
27 .mmap = generic_file_readonly_mmap,
28 .splice_read = generic_file_splice_read,
31 EXPORT_SYMBOL(generic_ro_fops);
33 static inline int unsigned_offsets(struct file *file)
35 return file->f_mode & FMODE_UNSIGNED_OFFSET;
38 /**
39 * generic_file_llseek_unlocked - lockless generic llseek implementation
40 * @file: file structure to seek on
41 * @offset: file offset to seek to
42 * @origin: type of seek
44 * Updates the file offset to the value specified by @offset and @origin.
45 * Locking must be provided by the caller.
47 loff_t
48 generic_file_llseek_unlocked(struct file *file, loff_t offset, int origin)
50 struct inode *inode = file->f_mapping->host;
52 switch (origin) {
53 case SEEK_END:
54 offset += inode->i_size;
55 break;
56 case SEEK_CUR:
58 * Here we special-case the lseek(fd, 0, SEEK_CUR)
59 * position-querying operation. Avoid rewriting the "same"
60 * f_pos value back to the file because a concurrent read(),
61 * write() or lseek() might have altered it
63 if (offset == 0)
64 return file->f_pos;
65 offset += file->f_pos;
66 break;
67 case SEEK_DATA:
69 * In the generic case the entire file is data, so as long as
70 * offset isn't at the end of the file then the offset is data.
72 if (offset >= inode->i_size)
73 return -ENXIO;
74 break;
75 case SEEK_HOLE:
77 * There is a virtual hole at the end of the file, so as long as
78 * offset isn't i_size or larger, return i_size.
80 if (offset >= inode->i_size)
81 return -ENXIO;
82 offset = inode->i_size;
83 break;
86 if (offset < 0 && !unsigned_offsets(file))
87 return -EINVAL;
88 if (offset > inode->i_sb->s_maxbytes)
89 return -EINVAL;
91 /* Special lock needed here? */
92 if (offset != file->f_pos) {
93 file->f_pos = offset;
94 file->f_version = 0;
97 return offset;
99 EXPORT_SYMBOL(generic_file_llseek_unlocked);
102 * generic_file_llseek - generic llseek implementation for regular files
103 * @file: file structure to seek on
104 * @offset: file offset to seek to
105 * @origin: type of seek
107 * This is a generic implemenation of ->llseek useable for all normal local
108 * filesystems. It just updates the file offset to the value specified by
109 * @offset and @origin under i_mutex.
111 loff_t generic_file_llseek(struct file *file, loff_t offset, int origin)
113 loff_t rval;
115 mutex_lock(&file->f_dentry->d_inode->i_mutex);
116 rval = generic_file_llseek_unlocked(file, offset, origin);
117 mutex_unlock(&file->f_dentry->d_inode->i_mutex);
119 return rval;
121 EXPORT_SYMBOL(generic_file_llseek);
124 * noop_llseek - No Operation Performed llseek implementation
125 * @file: file structure to seek on
126 * @offset: file offset to seek to
127 * @origin: type of seek
129 * This is an implementation of ->llseek useable for the rare special case when
130 * userspace expects the seek to succeed but the (device) file is actually not
131 * able to perform the seek. In this case you use noop_llseek() instead of
132 * falling back to the default implementation of ->llseek.
134 loff_t noop_llseek(struct file *file, loff_t offset, int origin)
136 return file->f_pos;
138 EXPORT_SYMBOL(noop_llseek);
140 loff_t no_llseek(struct file *file, loff_t offset, int origin)
142 return -ESPIPE;
144 EXPORT_SYMBOL(no_llseek);
146 loff_t default_llseek(struct file *file, loff_t offset, int origin)
148 struct inode *inode = file->f_path.dentry->d_inode;
149 loff_t retval;
151 mutex_lock(&inode->i_mutex);
152 switch (origin) {
153 case SEEK_END:
154 offset += i_size_read(inode);
155 break;
156 case SEEK_CUR:
157 if (offset == 0) {
158 retval = file->f_pos;
159 goto out;
161 offset += file->f_pos;
162 break;
163 case SEEK_DATA:
165 * In the generic case the entire file is data, so as
166 * long as offset isn't at the end of the file then the
167 * offset is data.
169 if (offset >= inode->i_size) {
170 retval = -ENXIO;
171 goto out;
173 break;
174 case SEEK_HOLE:
176 * There is a virtual hole at the end of the file, so
177 * as long as offset isn't i_size or larger, return
178 * i_size.
180 if (offset >= inode->i_size) {
181 retval = -ENXIO;
182 goto out;
184 offset = inode->i_size;
185 break;
187 retval = -EINVAL;
188 if (offset >= 0 || unsigned_offsets(file)) {
189 if (offset != file->f_pos) {
190 file->f_pos = offset;
191 file->f_version = 0;
193 retval = offset;
195 out:
196 mutex_unlock(&inode->i_mutex);
197 return retval;
199 EXPORT_SYMBOL(default_llseek);
201 loff_t vfs_llseek(struct file *file, loff_t offset, int origin)
203 loff_t (*fn)(struct file *, loff_t, int);
205 fn = no_llseek;
206 if (file->f_mode & FMODE_LSEEK) {
207 if (file->f_op && file->f_op->llseek)
208 fn = file->f_op->llseek;
210 return fn(file, offset, origin);
212 EXPORT_SYMBOL(vfs_llseek);
214 SYSCALL_DEFINE3(lseek, unsigned int, fd, off_t, offset, unsigned int, origin)
216 off_t retval;
217 struct file * file;
218 int fput_needed;
220 retval = -EBADF;
221 file = fget_light(fd, &fput_needed);
222 if (!file)
223 goto bad;
225 retval = -EINVAL;
226 if (origin <= SEEK_MAX) {
227 loff_t res = vfs_llseek(file, offset, origin);
228 retval = res;
229 if (res != (loff_t)retval)
230 retval = -EOVERFLOW; /* LFS: should only happen on 32 bit platforms */
232 fput_light(file, fput_needed);
233 bad:
234 return retval;
237 #ifdef __ARCH_WANT_SYS_LLSEEK
238 SYSCALL_DEFINE5(llseek, unsigned int, fd, unsigned long, offset_high,
239 unsigned long, offset_low, loff_t __user *, result,
240 unsigned int, origin)
242 int retval;
243 struct file * file;
244 loff_t offset;
245 int fput_needed;
247 retval = -EBADF;
248 file = fget_light(fd, &fput_needed);
249 if (!file)
250 goto bad;
252 retval = -EINVAL;
253 if (origin > SEEK_MAX)
254 goto out_putf;
256 offset = vfs_llseek(file, ((loff_t) offset_high << 32) | offset_low,
257 origin);
259 retval = (int)offset;
260 if (offset >= 0) {
261 retval = -EFAULT;
262 if (!copy_to_user(result, &offset, sizeof(offset)))
263 retval = 0;
265 out_putf:
266 fput_light(file, fput_needed);
267 bad:
268 return retval;
270 #endif
274 * rw_verify_area doesn't like huge counts. We limit
275 * them to something that fits in "int" so that others
276 * won't have to do range checks all the time.
278 int rw_verify_area(int read_write, struct file *file, loff_t *ppos, size_t count)
280 struct inode *inode;
281 loff_t pos;
282 int retval = -EINVAL;
284 inode = file->f_path.dentry->d_inode;
285 if (unlikely((ssize_t) count < 0))
286 return retval;
287 pos = *ppos;
288 if (unlikely(pos < 0)) {
289 if (!unsigned_offsets(file))
290 return retval;
291 if (count >= -pos) /* both values are in 0..LLONG_MAX */
292 return -EOVERFLOW;
293 } else if (unlikely((loff_t) (pos + count) < 0)) {
294 if (!unsigned_offsets(file))
295 return retval;
298 if (unlikely(inode->i_flock && mandatory_lock(inode))) {
299 retval = locks_mandatory_area(
300 read_write == READ ? FLOCK_VERIFY_READ : FLOCK_VERIFY_WRITE,
301 inode, file, pos, count);
302 if (retval < 0)
303 return retval;
305 retval = security_file_permission(file,
306 read_write == READ ? MAY_READ : MAY_WRITE);
307 if (retval)
308 return retval;
309 return count > MAX_RW_COUNT ? MAX_RW_COUNT : count;
312 static void wait_on_retry_sync_kiocb(struct kiocb *iocb)
314 set_current_state(TASK_UNINTERRUPTIBLE);
315 if (!kiocbIsKicked(iocb))
316 schedule();
317 else
318 kiocbClearKicked(iocb);
319 __set_current_state(TASK_RUNNING);
322 ssize_t do_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
324 struct iovec iov = { .iov_base = buf, .iov_len = len };
325 struct kiocb kiocb;
326 ssize_t ret;
328 init_sync_kiocb(&kiocb, filp);
329 kiocb.ki_pos = *ppos;
330 kiocb.ki_left = len;
331 kiocb.ki_nbytes = len;
333 for (;;) {
334 ret = filp->f_op->aio_read(&kiocb, &iov, 1, kiocb.ki_pos);
335 if (ret != -EIOCBRETRY)
336 break;
337 wait_on_retry_sync_kiocb(&kiocb);
340 if (-EIOCBQUEUED == ret)
341 ret = wait_on_sync_kiocb(&kiocb);
342 *ppos = kiocb.ki_pos;
343 return ret;
346 EXPORT_SYMBOL(do_sync_read);
348 ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
350 ssize_t ret;
352 if (!(file->f_mode & FMODE_READ))
353 return -EBADF;
354 if (!file->f_op || (!file->f_op->read && !file->f_op->aio_read))
355 return -EINVAL;
356 if (unlikely(!access_ok(VERIFY_WRITE, buf, count)))
357 return -EFAULT;
359 ret = rw_verify_area(READ, file, pos, count);
360 if (ret >= 0) {
361 count = ret;
362 if (file->f_op->read)
363 ret = file->f_op->read(file, buf, count, pos);
364 else
365 ret = do_sync_read(file, buf, count, pos);
366 if (ret > 0) {
367 fsnotify_access(file);
368 add_rchar(current, ret);
370 inc_syscr(current);
373 return ret;
376 EXPORT_SYMBOL(vfs_read);
378 ssize_t do_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
380 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len };
381 struct kiocb kiocb;
382 ssize_t ret;
384 init_sync_kiocb(&kiocb, filp);
385 kiocb.ki_pos = *ppos;
386 kiocb.ki_left = len;
387 kiocb.ki_nbytes = len;
389 for (;;) {
390 ret = filp->f_op->aio_write(&kiocb, &iov, 1, kiocb.ki_pos);
391 if (ret != -EIOCBRETRY)
392 break;
393 wait_on_retry_sync_kiocb(&kiocb);
396 if (-EIOCBQUEUED == ret)
397 ret = wait_on_sync_kiocb(&kiocb);
398 *ppos = kiocb.ki_pos;
399 return ret;
402 EXPORT_SYMBOL(do_sync_write);
404 ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
406 ssize_t ret;
408 if (!(file->f_mode & FMODE_WRITE))
409 return -EBADF;
410 if (!file->f_op || (!file->f_op->write && !file->f_op->aio_write))
411 return -EINVAL;
412 if (unlikely(!access_ok(VERIFY_READ, buf, count)))
413 return -EFAULT;
415 ret = rw_verify_area(WRITE, file, pos, count);
416 if (ret >= 0) {
417 count = ret;
418 if (file->f_op->write)
419 ret = file->f_op->write(file, buf, count, pos);
420 else
421 ret = do_sync_write(file, buf, count, pos);
422 if (ret > 0) {
423 fsnotify_modify(file);
424 add_wchar(current, ret);
426 inc_syscw(current);
429 return ret;
432 EXPORT_SYMBOL(vfs_write);
434 static inline loff_t file_pos_read(struct file *file)
436 return file->f_pos;
439 static inline void file_pos_write(struct file *file, loff_t pos)
441 file->f_pos = pos;
444 SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count)
446 struct file *file;
447 ssize_t ret = -EBADF;
448 int fput_needed;
450 file = fget_light(fd, &fput_needed);
451 if (file) {
452 loff_t pos = file_pos_read(file);
453 ret = vfs_read(file, buf, count, &pos);
454 file_pos_write(file, pos);
455 fput_light(file, fput_needed);
458 return ret;
461 SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf,
462 size_t, count)
464 struct file *file;
465 ssize_t ret = -EBADF;
466 int fput_needed;
468 file = fget_light(fd, &fput_needed);
469 if (file) {
470 loff_t pos = file_pos_read(file);
471 ret = vfs_write(file, buf, count, &pos);
472 file_pos_write(file, pos);
473 fput_light(file, fput_needed);
476 return ret;
479 SYSCALL_DEFINE(pread64)(unsigned int fd, char __user *buf,
480 size_t count, loff_t pos)
482 struct file *file;
483 ssize_t ret = -EBADF;
484 int fput_needed;
486 if (pos < 0)
487 return -EINVAL;
489 file = fget_light(fd, &fput_needed);
490 if (file) {
491 ret = -ESPIPE;
492 if (file->f_mode & FMODE_PREAD)
493 ret = vfs_read(file, buf, count, &pos);
494 fput_light(file, fput_needed);
497 return ret;
499 #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
500 asmlinkage long SyS_pread64(long fd, long buf, long count, loff_t pos)
502 return SYSC_pread64((unsigned int) fd, (char __user *) buf,
503 (size_t) count, pos);
505 SYSCALL_ALIAS(sys_pread64, SyS_pread64);
506 #endif
508 SYSCALL_DEFINE(pwrite64)(unsigned int fd, const char __user *buf,
509 size_t count, loff_t pos)
511 struct file *file;
512 ssize_t ret = -EBADF;
513 int fput_needed;
515 if (pos < 0)
516 return -EINVAL;
518 file = fget_light(fd, &fput_needed);
519 if (file) {
520 ret = -ESPIPE;
521 if (file->f_mode & FMODE_PWRITE)
522 ret = vfs_write(file, buf, count, &pos);
523 fput_light(file, fput_needed);
526 return ret;
528 #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
529 asmlinkage long SyS_pwrite64(long fd, long buf, long count, loff_t pos)
531 return SYSC_pwrite64((unsigned int) fd, (const char __user *) buf,
532 (size_t) count, pos);
534 SYSCALL_ALIAS(sys_pwrite64, SyS_pwrite64);
535 #endif
538 * Reduce an iovec's length in-place. Return the resulting number of segments
540 unsigned long iov_shorten(struct iovec *iov, unsigned long nr_segs, size_t to)
542 unsigned long seg = 0;
543 size_t len = 0;
545 while (seg < nr_segs) {
546 seg++;
547 if (len + iov->iov_len >= to) {
548 iov->iov_len = to - len;
549 break;
551 len += iov->iov_len;
552 iov++;
554 return seg;
556 EXPORT_SYMBOL(iov_shorten);
558 ssize_t do_sync_readv_writev(struct file *filp, const struct iovec *iov,
559 unsigned long nr_segs, size_t len, loff_t *ppos, iov_fn_t fn)
561 struct kiocb kiocb;
562 ssize_t ret;
564 init_sync_kiocb(&kiocb, filp);
565 kiocb.ki_pos = *ppos;
566 kiocb.ki_left = len;
567 kiocb.ki_nbytes = len;
569 for (;;) {
570 ret = fn(&kiocb, iov, nr_segs, kiocb.ki_pos);
571 if (ret != -EIOCBRETRY)
572 break;
573 wait_on_retry_sync_kiocb(&kiocb);
576 if (ret == -EIOCBQUEUED)
577 ret = wait_on_sync_kiocb(&kiocb);
578 *ppos = kiocb.ki_pos;
579 return ret;
582 /* Do it by hand, with file-ops */
583 ssize_t do_loop_readv_writev(struct file *filp, struct iovec *iov,
584 unsigned long nr_segs, loff_t *ppos, io_fn_t fn)
586 struct iovec *vector = iov;
587 ssize_t ret = 0;
589 while (nr_segs > 0) {
590 void __user *base;
591 size_t len;
592 ssize_t nr;
594 base = vector->iov_base;
595 len = vector->iov_len;
596 vector++;
597 nr_segs--;
599 nr = fn(filp, base, len, ppos);
601 if (nr < 0) {
602 if (!ret)
603 ret = nr;
604 break;
606 ret += nr;
607 if (nr != len)
608 break;
611 return ret;
614 /* A write operation does a read from user space and vice versa */
615 #define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
617 ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
618 unsigned long nr_segs, unsigned long fast_segs,
619 struct iovec *fast_pointer,
620 struct iovec **ret_pointer,
621 int check_access)
623 unsigned long seg;
624 ssize_t ret;
625 struct iovec *iov = fast_pointer;
628 * SuS says "The readv() function *may* fail if the iovcnt argument
629 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
630 * traditionally returned zero for zero segments, so...
632 if (nr_segs == 0) {
633 ret = 0;
634 goto out;
638 * First get the "struct iovec" from user memory and
639 * verify all the pointers
641 if (nr_segs > UIO_MAXIOV) {
642 ret = -EINVAL;
643 goto out;
645 if (nr_segs > fast_segs) {
646 iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
647 if (iov == NULL) {
648 ret = -ENOMEM;
649 goto out;
652 if (copy_from_user(iov, uvector, nr_segs*sizeof(*uvector))) {
653 ret = -EFAULT;
654 goto out;
658 * According to the Single Unix Specification we should return EINVAL
659 * if an element length is < 0 when cast to ssize_t or if the
660 * total length would overflow the ssize_t return value of the
661 * system call.
663 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
664 * overflow case.
666 ret = 0;
667 for (seg = 0; seg < nr_segs; seg++) {
668 void __user *buf = iov[seg].iov_base;
669 ssize_t len = (ssize_t)iov[seg].iov_len;
671 /* see if we we're about to use an invalid len or if
672 * it's about to overflow ssize_t */
673 if (len < 0) {
674 ret = -EINVAL;
675 goto out;
677 if (check_access
678 && unlikely(!access_ok(vrfy_dir(type), buf, len))) {
679 ret = -EFAULT;
680 goto out;
682 if (len > MAX_RW_COUNT - ret) {
683 len = MAX_RW_COUNT - ret;
684 iov[seg].iov_len = len;
686 ret += len;
688 out:
689 *ret_pointer = iov;
690 return ret;
693 static ssize_t do_readv_writev(int type, struct file *file,
694 const struct iovec __user * uvector,
695 unsigned long nr_segs, loff_t *pos)
697 size_t tot_len;
698 struct iovec iovstack[UIO_FASTIOV];
699 struct iovec *iov = iovstack;
700 ssize_t ret;
701 io_fn_t fn;
702 iov_fn_t fnv;
704 if (!file->f_op) {
705 ret = -EINVAL;
706 goto out;
709 ret = rw_copy_check_uvector(type, uvector, nr_segs,
710 ARRAY_SIZE(iovstack), iovstack, &iov, 1);
711 if (ret <= 0)
712 goto out;
714 tot_len = ret;
715 ret = rw_verify_area(type, file, pos, tot_len);
716 if (ret < 0)
717 goto out;
719 fnv = NULL;
720 if (type == READ) {
721 fn = file->f_op->read;
722 fnv = file->f_op->aio_read;
723 } else {
724 fn = (io_fn_t)file->f_op->write;
725 fnv = file->f_op->aio_write;
728 if (fnv)
729 ret = do_sync_readv_writev(file, iov, nr_segs, tot_len,
730 pos, fnv);
731 else
732 ret = do_loop_readv_writev(file, iov, nr_segs, pos, fn);
734 out:
735 if (iov != iovstack)
736 kfree(iov);
737 if ((ret + (type == READ)) > 0) {
738 if (type == READ)
739 fsnotify_access(file);
740 else
741 fsnotify_modify(file);
743 return ret;
746 ssize_t vfs_readv(struct file *file, const struct iovec __user *vec,
747 unsigned long vlen, loff_t *pos)
749 if (!(file->f_mode & FMODE_READ))
750 return -EBADF;
751 if (!file->f_op || (!file->f_op->aio_read && !file->f_op->read))
752 return -EINVAL;
754 return do_readv_writev(READ, file, vec, vlen, pos);
757 EXPORT_SYMBOL(vfs_readv);
759 ssize_t vfs_writev(struct file *file, const struct iovec __user *vec,
760 unsigned long vlen, loff_t *pos)
762 if (!(file->f_mode & FMODE_WRITE))
763 return -EBADF;
764 if (!file->f_op || (!file->f_op->aio_write && !file->f_op->write))
765 return -EINVAL;
767 return do_readv_writev(WRITE, file, vec, vlen, pos);
770 EXPORT_SYMBOL(vfs_writev);
772 SYSCALL_DEFINE3(readv, unsigned long, fd, const struct iovec __user *, vec,
773 unsigned long, vlen)
775 struct file *file;
776 ssize_t ret = -EBADF;
777 int fput_needed;
779 file = fget_light(fd, &fput_needed);
780 if (file) {
781 loff_t pos = file_pos_read(file);
782 ret = vfs_readv(file, vec, vlen, &pos);
783 file_pos_write(file, pos);
784 fput_light(file, fput_needed);
787 if (ret > 0)
788 add_rchar(current, ret);
789 inc_syscr(current);
790 return ret;
793 SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec __user *, vec,
794 unsigned long, vlen)
796 struct file *file;
797 ssize_t ret = -EBADF;
798 int fput_needed;
800 file = fget_light(fd, &fput_needed);
801 if (file) {
802 loff_t pos = file_pos_read(file);
803 ret = vfs_writev(file, vec, vlen, &pos);
804 file_pos_write(file, pos);
805 fput_light(file, fput_needed);
808 if (ret > 0)
809 add_wchar(current, ret);
810 inc_syscw(current);
811 return ret;
814 static inline loff_t pos_from_hilo(unsigned long high, unsigned long low)
816 #define HALF_LONG_BITS (BITS_PER_LONG / 2)
817 return (((loff_t)high << HALF_LONG_BITS) << HALF_LONG_BITS) | low;
820 SYSCALL_DEFINE5(preadv, unsigned long, fd, const struct iovec __user *, vec,
821 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
823 loff_t pos = pos_from_hilo(pos_h, pos_l);
824 struct file *file;
825 ssize_t ret = -EBADF;
826 int fput_needed;
828 if (pos < 0)
829 return -EINVAL;
831 file = fget_light(fd, &fput_needed);
832 if (file) {
833 ret = -ESPIPE;
834 if (file->f_mode & FMODE_PREAD)
835 ret = vfs_readv(file, vec, vlen, &pos);
836 fput_light(file, fput_needed);
839 if (ret > 0)
840 add_rchar(current, ret);
841 inc_syscr(current);
842 return ret;
845 SYSCALL_DEFINE5(pwritev, unsigned long, fd, const struct iovec __user *, vec,
846 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
848 loff_t pos = pos_from_hilo(pos_h, pos_l);
849 struct file *file;
850 ssize_t ret = -EBADF;
851 int fput_needed;
853 if (pos < 0)
854 return -EINVAL;
856 file = fget_light(fd, &fput_needed);
857 if (file) {
858 ret = -ESPIPE;
859 if (file->f_mode & FMODE_PWRITE)
860 ret = vfs_writev(file, vec, vlen, &pos);
861 fput_light(file, fput_needed);
864 if (ret > 0)
865 add_wchar(current, ret);
866 inc_syscw(current);
867 return ret;
870 static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,
871 size_t count, loff_t max)
873 struct file * in_file, * out_file;
874 struct inode * in_inode, * out_inode;
875 loff_t pos;
876 ssize_t retval;
877 int fput_needed_in, fput_needed_out, fl;
880 * Get input file, and verify that it is ok..
882 retval = -EBADF;
883 in_file = fget_light(in_fd, &fput_needed_in);
884 if (!in_file)
885 goto out;
886 if (!(in_file->f_mode & FMODE_READ))
887 goto fput_in;
888 retval = -ESPIPE;
889 if (!ppos)
890 ppos = &in_file->f_pos;
891 else
892 if (!(in_file->f_mode & FMODE_PREAD))
893 goto fput_in;
894 retval = rw_verify_area(READ, in_file, ppos, count);
895 if (retval < 0)
896 goto fput_in;
897 count = retval;
900 * Get output file, and verify that it is ok..
902 retval = -EBADF;
903 out_file = fget_light(out_fd, &fput_needed_out);
904 if (!out_file)
905 goto fput_in;
906 if (!(out_file->f_mode & FMODE_WRITE))
907 goto fput_out;
908 retval = -EINVAL;
909 in_inode = in_file->f_path.dentry->d_inode;
910 out_inode = out_file->f_path.dentry->d_inode;
911 retval = rw_verify_area(WRITE, out_file, &out_file->f_pos, count);
912 if (retval < 0)
913 goto fput_out;
914 count = retval;
916 if (!max)
917 max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes);
919 pos = *ppos;
920 if (unlikely(pos + count > max)) {
921 retval = -EOVERFLOW;
922 if (pos >= max)
923 goto fput_out;
924 count = max - pos;
927 fl = 0;
928 #if 0
930 * We need to debate whether we can enable this or not. The
931 * man page documents EAGAIN return for the output at least,
932 * and the application is arguably buggy if it doesn't expect
933 * EAGAIN on a non-blocking file descriptor.
935 if (in_file->f_flags & O_NONBLOCK)
936 fl = SPLICE_F_NONBLOCK;
937 #endif
938 retval = do_splice_direct(in_file, ppos, out_file, count, fl);
940 if (retval > 0) {
941 add_rchar(current, retval);
942 add_wchar(current, retval);
945 inc_syscr(current);
946 inc_syscw(current);
947 if (*ppos > max)
948 retval = -EOVERFLOW;
950 fput_out:
951 fput_light(out_file, fput_needed_out);
952 fput_in:
953 fput_light(in_file, fput_needed_in);
954 out:
955 return retval;
958 SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd, off_t __user *, offset, size_t, count)
960 loff_t pos;
961 off_t off;
962 ssize_t ret;
964 if (offset) {
965 if (unlikely(get_user(off, offset)))
966 return -EFAULT;
967 pos = off;
968 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
969 if (unlikely(put_user(pos, offset)))
970 return -EFAULT;
971 return ret;
974 return do_sendfile(out_fd, in_fd, NULL, count, 0);
977 SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd, loff_t __user *, offset, size_t, count)
979 loff_t pos;
980 ssize_t ret;
982 if (offset) {
983 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
984 return -EFAULT;
985 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
986 if (unlikely(put_user(pos, offset)))
987 return -EFAULT;
988 return ret;
991 return do_sendfile(out_fd, in_fd, NULL, count, 0);