Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
[deliverable/linux.git] / fs / read_write.c
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>
12 #include <linux/fsnotify.h>
13 #include <linux/security.h>
14 #include <linux/export.h>
15 #include <linux/syscalls.h>
16 #include <linux/pagemap.h>
17 #include <linux/splice.h>
18 #include <linux/compat.h>
19 #include "read_write.h"
20 #include "internal.h"
21
22 #include <asm/uaccess.h>
23 #include <asm/unistd.h>
24
25 const struct file_operations generic_ro_fops = {
26 .llseek = generic_file_llseek,
27 .read = do_sync_read,
28 .aio_read = generic_file_aio_read,
29 .mmap = generic_file_readonly_mmap,
30 .splice_read = generic_file_splice_read,
31 };
32
33 EXPORT_SYMBOL(generic_ro_fops);
34
35 static inline int unsigned_offsets(struct file *file)
36 {
37 return file->f_mode & FMODE_UNSIGNED_OFFSET;
38 }
39
40 static loff_t lseek_execute(struct file *file, struct inode *inode,
41 loff_t offset, loff_t maxsize)
42 {
43 if (offset < 0 && !unsigned_offsets(file))
44 return -EINVAL;
45 if (offset > maxsize)
46 return -EINVAL;
47
48 if (offset != file->f_pos) {
49 file->f_pos = offset;
50 file->f_version = 0;
51 }
52 return offset;
53 }
54
55 /**
56 * generic_file_llseek_size - generic llseek implementation for regular files
57 * @file: file structure to seek on
58 * @offset: file offset to seek to
59 * @whence: type of seek
60 * @size: max size of this file in file system
61 * @eof: offset used for SEEK_END position
62 *
63 * This is a variant of generic_file_llseek that allows passing in a custom
64 * maximum file size and a custom EOF position, for e.g. hashed directories
65 *
66 * Synchronization:
67 * SEEK_SET and SEEK_END are unsynchronized (but atomic on 64bit platforms)
68 * SEEK_CUR is synchronized against other SEEK_CURs, but not read/writes.
69 * read/writes behave like SEEK_SET against seeks.
70 */
71 loff_t
72 generic_file_llseek_size(struct file *file, loff_t offset, int whence,
73 loff_t maxsize, loff_t eof)
74 {
75 struct inode *inode = file->f_mapping->host;
76
77 switch (whence) {
78 case SEEK_END:
79 offset += eof;
80 break;
81 case SEEK_CUR:
82 /*
83 * Here we special-case the lseek(fd, 0, SEEK_CUR)
84 * position-querying operation. Avoid rewriting the "same"
85 * f_pos value back to the file because a concurrent read(),
86 * write() or lseek() might have altered it
87 */
88 if (offset == 0)
89 return file->f_pos;
90 /*
91 * f_lock protects against read/modify/write race with other
92 * SEEK_CURs. Note that parallel writes and reads behave
93 * like SEEK_SET.
94 */
95 spin_lock(&file->f_lock);
96 offset = lseek_execute(file, inode, file->f_pos + offset,
97 maxsize);
98 spin_unlock(&file->f_lock);
99 return offset;
100 case SEEK_DATA:
101 /*
102 * In the generic case the entire file is data, so as long as
103 * offset isn't at the end of the file then the offset is data.
104 */
105 if (offset >= eof)
106 return -ENXIO;
107 break;
108 case SEEK_HOLE:
109 /*
110 * There is a virtual hole at the end of the file, so as long as
111 * offset isn't i_size or larger, return i_size.
112 */
113 if (offset >= eof)
114 return -ENXIO;
115 offset = eof;
116 break;
117 }
118
119 return lseek_execute(file, inode, offset, maxsize);
120 }
121 EXPORT_SYMBOL(generic_file_llseek_size);
122
123 /**
124 * generic_file_llseek - generic llseek implementation for regular files
125 * @file: file structure to seek on
126 * @offset: file offset to seek to
127 * @whence: type of seek
128 *
129 * This is a generic implemenation of ->llseek useable for all normal local
130 * filesystems. It just updates the file offset to the value specified by
131 * @offset and @whence under i_mutex.
132 */
133 loff_t generic_file_llseek(struct file *file, loff_t offset, int whence)
134 {
135 struct inode *inode = file->f_mapping->host;
136
137 return generic_file_llseek_size(file, offset, whence,
138 inode->i_sb->s_maxbytes,
139 i_size_read(inode));
140 }
141 EXPORT_SYMBOL(generic_file_llseek);
142
143 /**
144 * noop_llseek - No Operation Performed llseek implementation
145 * @file: file structure to seek on
146 * @offset: file offset to seek to
147 * @whence: type of seek
148 *
149 * This is an implementation of ->llseek useable for the rare special case when
150 * userspace expects the seek to succeed but the (device) file is actually not
151 * able to perform the seek. In this case you use noop_llseek() instead of
152 * falling back to the default implementation of ->llseek.
153 */
154 loff_t noop_llseek(struct file *file, loff_t offset, int whence)
155 {
156 return file->f_pos;
157 }
158 EXPORT_SYMBOL(noop_llseek);
159
160 loff_t no_llseek(struct file *file, loff_t offset, int whence)
161 {
162 return -ESPIPE;
163 }
164 EXPORT_SYMBOL(no_llseek);
165
166 loff_t default_llseek(struct file *file, loff_t offset, int whence)
167 {
168 struct inode *inode = file_inode(file);
169 loff_t retval;
170
171 mutex_lock(&inode->i_mutex);
172 switch (whence) {
173 case SEEK_END:
174 offset += i_size_read(inode);
175 break;
176 case SEEK_CUR:
177 if (offset == 0) {
178 retval = file->f_pos;
179 goto out;
180 }
181 offset += file->f_pos;
182 break;
183 case SEEK_DATA:
184 /*
185 * In the generic case the entire file is data, so as
186 * long as offset isn't at the end of the file then the
187 * offset is data.
188 */
189 if (offset >= inode->i_size) {
190 retval = -ENXIO;
191 goto out;
192 }
193 break;
194 case SEEK_HOLE:
195 /*
196 * There is a virtual hole at the end of the file, so
197 * as long as offset isn't i_size or larger, return
198 * i_size.
199 */
200 if (offset >= inode->i_size) {
201 retval = -ENXIO;
202 goto out;
203 }
204 offset = inode->i_size;
205 break;
206 }
207 retval = -EINVAL;
208 if (offset >= 0 || unsigned_offsets(file)) {
209 if (offset != file->f_pos) {
210 file->f_pos = offset;
211 file->f_version = 0;
212 }
213 retval = offset;
214 }
215 out:
216 mutex_unlock(&inode->i_mutex);
217 return retval;
218 }
219 EXPORT_SYMBOL(default_llseek);
220
221 loff_t vfs_llseek(struct file *file, loff_t offset, int whence)
222 {
223 loff_t (*fn)(struct file *, loff_t, int);
224
225 fn = no_llseek;
226 if (file->f_mode & FMODE_LSEEK) {
227 if (file->f_op && file->f_op->llseek)
228 fn = file->f_op->llseek;
229 }
230 return fn(file, offset, whence);
231 }
232 EXPORT_SYMBOL(vfs_llseek);
233
234 SYSCALL_DEFINE3(lseek, unsigned int, fd, off_t, offset, unsigned int, whence)
235 {
236 off_t retval;
237 struct fd f = fdget(fd);
238 if (!f.file)
239 return -EBADF;
240
241 retval = -EINVAL;
242 if (whence <= SEEK_MAX) {
243 loff_t res = vfs_llseek(f.file, offset, whence);
244 retval = res;
245 if (res != (loff_t)retval)
246 retval = -EOVERFLOW; /* LFS: should only happen on 32 bit platforms */
247 }
248 fdput(f);
249 return retval;
250 }
251
252 #ifdef CONFIG_COMPAT
253 COMPAT_SYSCALL_DEFINE3(lseek, unsigned int, fd, compat_off_t, offset, unsigned int, whence)
254 {
255 return sys_lseek(fd, offset, whence);
256 }
257 #endif
258
259 #ifdef __ARCH_WANT_SYS_LLSEEK
260 SYSCALL_DEFINE5(llseek, unsigned int, fd, unsigned long, offset_high,
261 unsigned long, offset_low, loff_t __user *, result,
262 unsigned int, whence)
263 {
264 int retval;
265 struct fd f = fdget(fd);
266 loff_t offset;
267
268 if (!f.file)
269 return -EBADF;
270
271 retval = -EINVAL;
272 if (whence > SEEK_MAX)
273 goto out_putf;
274
275 offset = vfs_llseek(f.file, ((loff_t) offset_high << 32) | offset_low,
276 whence);
277
278 retval = (int)offset;
279 if (offset >= 0) {
280 retval = -EFAULT;
281 if (!copy_to_user(result, &offset, sizeof(offset)))
282 retval = 0;
283 }
284 out_putf:
285 fdput(f);
286 return retval;
287 }
288 #endif
289
290 /*
291 * rw_verify_area doesn't like huge counts. We limit
292 * them to something that fits in "int" so that others
293 * won't have to do range checks all the time.
294 */
295 int rw_verify_area(int read_write, struct file *file, loff_t *ppos, size_t count)
296 {
297 struct inode *inode;
298 loff_t pos;
299 int retval = -EINVAL;
300
301 inode = file_inode(file);
302 if (unlikely((ssize_t) count < 0))
303 return retval;
304 pos = *ppos;
305 if (unlikely(pos < 0)) {
306 if (!unsigned_offsets(file))
307 return retval;
308 if (count >= -pos) /* both values are in 0..LLONG_MAX */
309 return -EOVERFLOW;
310 } else if (unlikely((loff_t) (pos + count) < 0)) {
311 if (!unsigned_offsets(file))
312 return retval;
313 }
314
315 if (unlikely(inode->i_flock && mandatory_lock(inode))) {
316 retval = locks_mandatory_area(
317 read_write == READ ? FLOCK_VERIFY_READ : FLOCK_VERIFY_WRITE,
318 inode, file, pos, count);
319 if (retval < 0)
320 return retval;
321 }
322 retval = security_file_permission(file,
323 read_write == READ ? MAY_READ : MAY_WRITE);
324 if (retval)
325 return retval;
326 return count > MAX_RW_COUNT ? MAX_RW_COUNT : count;
327 }
328
329 static void wait_on_retry_sync_kiocb(struct kiocb *iocb)
330 {
331 set_current_state(TASK_UNINTERRUPTIBLE);
332 if (!kiocbIsKicked(iocb))
333 schedule();
334 else
335 kiocbClearKicked(iocb);
336 __set_current_state(TASK_RUNNING);
337 }
338
339 ssize_t do_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
340 {
341 struct iovec iov = { .iov_base = buf, .iov_len = len };
342 struct kiocb kiocb;
343 ssize_t ret;
344
345 init_sync_kiocb(&kiocb, filp);
346 kiocb.ki_pos = *ppos;
347 kiocb.ki_left = len;
348 kiocb.ki_nbytes = len;
349
350 for (;;) {
351 ret = filp->f_op->aio_read(&kiocb, &iov, 1, kiocb.ki_pos);
352 if (ret != -EIOCBRETRY)
353 break;
354 wait_on_retry_sync_kiocb(&kiocb);
355 }
356
357 if (-EIOCBQUEUED == ret)
358 ret = wait_on_sync_kiocb(&kiocb);
359 *ppos = kiocb.ki_pos;
360 return ret;
361 }
362
363 EXPORT_SYMBOL(do_sync_read);
364
365 ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
366 {
367 ssize_t ret;
368
369 if (!(file->f_mode & FMODE_READ))
370 return -EBADF;
371 if (!file->f_op || (!file->f_op->read && !file->f_op->aio_read))
372 return -EINVAL;
373 if (unlikely(!access_ok(VERIFY_WRITE, buf, count)))
374 return -EFAULT;
375
376 ret = rw_verify_area(READ, file, pos, count);
377 if (ret >= 0) {
378 count = ret;
379 if (file->f_op->read)
380 ret = file->f_op->read(file, buf, count, pos);
381 else
382 ret = do_sync_read(file, buf, count, pos);
383 if (ret > 0) {
384 fsnotify_access(file);
385 add_rchar(current, ret);
386 }
387 inc_syscr(current);
388 }
389
390 return ret;
391 }
392
393 EXPORT_SYMBOL(vfs_read);
394
395 ssize_t do_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
396 {
397 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len };
398 struct kiocb kiocb;
399 ssize_t ret;
400
401 init_sync_kiocb(&kiocb, filp);
402 kiocb.ki_pos = *ppos;
403 kiocb.ki_left = len;
404 kiocb.ki_nbytes = len;
405
406 for (;;) {
407 ret = filp->f_op->aio_write(&kiocb, &iov, 1, kiocb.ki_pos);
408 if (ret != -EIOCBRETRY)
409 break;
410 wait_on_retry_sync_kiocb(&kiocb);
411 }
412
413 if (-EIOCBQUEUED == ret)
414 ret = wait_on_sync_kiocb(&kiocb);
415 *ppos = kiocb.ki_pos;
416 return ret;
417 }
418
419 EXPORT_SYMBOL(do_sync_write);
420
421 ssize_t __kernel_write(struct file *file, const char *buf, size_t count, loff_t *pos)
422 {
423 mm_segment_t old_fs;
424 const char __user *p;
425 ssize_t ret;
426
427 old_fs = get_fs();
428 set_fs(get_ds());
429 p = (__force const char __user *)buf;
430 if (count > MAX_RW_COUNT)
431 count = MAX_RW_COUNT;
432 if (file->f_op->write)
433 ret = file->f_op->write(file, p, count, pos);
434 else
435 ret = do_sync_write(file, p, count, pos);
436 set_fs(old_fs);
437 if (ret > 0) {
438 fsnotify_modify(file);
439 add_wchar(current, ret);
440 }
441 inc_syscw(current);
442 return ret;
443 }
444
445 ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
446 {
447 ssize_t ret;
448
449 if (!(file->f_mode & FMODE_WRITE))
450 return -EBADF;
451 if (!file->f_op || (!file->f_op->write && !file->f_op->aio_write))
452 return -EINVAL;
453 if (unlikely(!access_ok(VERIFY_READ, buf, count)))
454 return -EFAULT;
455
456 ret = rw_verify_area(WRITE, file, pos, count);
457 if (ret >= 0) {
458 count = ret;
459 if (file->f_op->write)
460 ret = file->f_op->write(file, buf, count, pos);
461 else
462 ret = do_sync_write(file, buf, count, pos);
463 if (ret > 0) {
464 fsnotify_modify(file);
465 add_wchar(current, ret);
466 }
467 inc_syscw(current);
468 }
469
470 return ret;
471 }
472
473 EXPORT_SYMBOL(vfs_write);
474
475 static inline loff_t file_pos_read(struct file *file)
476 {
477 return file->f_pos;
478 }
479
480 static inline void file_pos_write(struct file *file, loff_t pos)
481 {
482 file->f_pos = pos;
483 }
484
485 SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count)
486 {
487 struct fd f = fdget(fd);
488 ssize_t ret = -EBADF;
489
490 if (f.file) {
491 loff_t pos = file_pos_read(f.file);
492 ret = vfs_read(f.file, buf, count, &pos);
493 file_pos_write(f.file, pos);
494 fdput(f);
495 }
496 return ret;
497 }
498
499 SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf,
500 size_t, count)
501 {
502 struct fd f = fdget(fd);
503 ssize_t ret = -EBADF;
504
505 if (f.file) {
506 loff_t pos = file_pos_read(f.file);
507 ret = vfs_write(f.file, buf, count, &pos);
508 file_pos_write(f.file, pos);
509 fdput(f);
510 }
511
512 return ret;
513 }
514
515 SYSCALL_DEFINE(pread64)(unsigned int fd, char __user *buf,
516 size_t count, loff_t pos)
517 {
518 struct fd f;
519 ssize_t ret = -EBADF;
520
521 if (pos < 0)
522 return -EINVAL;
523
524 f = fdget(fd);
525 if (f.file) {
526 ret = -ESPIPE;
527 if (f.file->f_mode & FMODE_PREAD)
528 ret = vfs_read(f.file, buf, count, &pos);
529 fdput(f);
530 }
531
532 return ret;
533 }
534 #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
535 asmlinkage long SyS_pread64(long fd, long buf, long count, loff_t pos)
536 {
537 return SYSC_pread64((unsigned int) fd, (char __user *) buf,
538 (size_t) count, pos);
539 }
540 SYSCALL_ALIAS(sys_pread64, SyS_pread64);
541 #endif
542
543 SYSCALL_DEFINE(pwrite64)(unsigned int fd, const char __user *buf,
544 size_t count, loff_t pos)
545 {
546 struct fd f;
547 ssize_t ret = -EBADF;
548
549 if (pos < 0)
550 return -EINVAL;
551
552 f = fdget(fd);
553 if (f.file) {
554 ret = -ESPIPE;
555 if (f.file->f_mode & FMODE_PWRITE)
556 ret = vfs_write(f.file, buf, count, &pos);
557 fdput(f);
558 }
559
560 return ret;
561 }
562 #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
563 asmlinkage long SyS_pwrite64(long fd, long buf, long count, loff_t pos)
564 {
565 return SYSC_pwrite64((unsigned int) fd, (const char __user *) buf,
566 (size_t) count, pos);
567 }
568 SYSCALL_ALIAS(sys_pwrite64, SyS_pwrite64);
569 #endif
570
571 /*
572 * Reduce an iovec's length in-place. Return the resulting number of segments
573 */
574 unsigned long iov_shorten(struct iovec *iov, unsigned long nr_segs, size_t to)
575 {
576 unsigned long seg = 0;
577 size_t len = 0;
578
579 while (seg < nr_segs) {
580 seg++;
581 if (len + iov->iov_len >= to) {
582 iov->iov_len = to - len;
583 break;
584 }
585 len += iov->iov_len;
586 iov++;
587 }
588 return seg;
589 }
590 EXPORT_SYMBOL(iov_shorten);
591
592 ssize_t do_sync_readv_writev(struct file *filp, const struct iovec *iov,
593 unsigned long nr_segs, size_t len, loff_t *ppos, iov_fn_t fn)
594 {
595 struct kiocb kiocb;
596 ssize_t ret;
597
598 init_sync_kiocb(&kiocb, filp);
599 kiocb.ki_pos = *ppos;
600 kiocb.ki_left = len;
601 kiocb.ki_nbytes = len;
602
603 for (;;) {
604 ret = fn(&kiocb, iov, nr_segs, kiocb.ki_pos);
605 if (ret != -EIOCBRETRY)
606 break;
607 wait_on_retry_sync_kiocb(&kiocb);
608 }
609
610 if (ret == -EIOCBQUEUED)
611 ret = wait_on_sync_kiocb(&kiocb);
612 *ppos = kiocb.ki_pos;
613 return ret;
614 }
615
616 /* Do it by hand, with file-ops */
617 ssize_t do_loop_readv_writev(struct file *filp, struct iovec *iov,
618 unsigned long nr_segs, loff_t *ppos, io_fn_t fn)
619 {
620 struct iovec *vector = iov;
621 ssize_t ret = 0;
622
623 while (nr_segs > 0) {
624 void __user *base;
625 size_t len;
626 ssize_t nr;
627
628 base = vector->iov_base;
629 len = vector->iov_len;
630 vector++;
631 nr_segs--;
632
633 nr = fn(filp, base, len, ppos);
634
635 if (nr < 0) {
636 if (!ret)
637 ret = nr;
638 break;
639 }
640 ret += nr;
641 if (nr != len)
642 break;
643 }
644
645 return ret;
646 }
647
648 /* A write operation does a read from user space and vice versa */
649 #define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
650
651 ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
652 unsigned long nr_segs, unsigned long fast_segs,
653 struct iovec *fast_pointer,
654 struct iovec **ret_pointer)
655 {
656 unsigned long seg;
657 ssize_t ret;
658 struct iovec *iov = fast_pointer;
659
660 /*
661 * SuS says "The readv() function *may* fail if the iovcnt argument
662 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
663 * traditionally returned zero for zero segments, so...
664 */
665 if (nr_segs == 0) {
666 ret = 0;
667 goto out;
668 }
669
670 /*
671 * First get the "struct iovec" from user memory and
672 * verify all the pointers
673 */
674 if (nr_segs > UIO_MAXIOV) {
675 ret = -EINVAL;
676 goto out;
677 }
678 if (nr_segs > fast_segs) {
679 iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
680 if (iov == NULL) {
681 ret = -ENOMEM;
682 goto out;
683 }
684 }
685 if (copy_from_user(iov, uvector, nr_segs*sizeof(*uvector))) {
686 ret = -EFAULT;
687 goto out;
688 }
689
690 /*
691 * According to the Single Unix Specification we should return EINVAL
692 * if an element length is < 0 when cast to ssize_t or if the
693 * total length would overflow the ssize_t return value of the
694 * system call.
695 *
696 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
697 * overflow case.
698 */
699 ret = 0;
700 for (seg = 0; seg < nr_segs; seg++) {
701 void __user *buf = iov[seg].iov_base;
702 ssize_t len = (ssize_t)iov[seg].iov_len;
703
704 /* see if we we're about to use an invalid len or if
705 * it's about to overflow ssize_t */
706 if (len < 0) {
707 ret = -EINVAL;
708 goto out;
709 }
710 if (type >= 0
711 && unlikely(!access_ok(vrfy_dir(type), buf, len))) {
712 ret = -EFAULT;
713 goto out;
714 }
715 if (len > MAX_RW_COUNT - ret) {
716 len = MAX_RW_COUNT - ret;
717 iov[seg].iov_len = len;
718 }
719 ret += len;
720 }
721 out:
722 *ret_pointer = iov;
723 return ret;
724 }
725
726 static ssize_t do_readv_writev(int type, struct file *file,
727 const struct iovec __user * uvector,
728 unsigned long nr_segs, loff_t *pos)
729 {
730 size_t tot_len;
731 struct iovec iovstack[UIO_FASTIOV];
732 struct iovec *iov = iovstack;
733 ssize_t ret;
734 io_fn_t fn;
735 iov_fn_t fnv;
736
737 if (!file->f_op) {
738 ret = -EINVAL;
739 goto out;
740 }
741
742 ret = rw_copy_check_uvector(type, uvector, nr_segs,
743 ARRAY_SIZE(iovstack), iovstack, &iov);
744 if (ret <= 0)
745 goto out;
746
747 tot_len = ret;
748 ret = rw_verify_area(type, file, pos, tot_len);
749 if (ret < 0)
750 goto out;
751
752 fnv = NULL;
753 if (type == READ) {
754 fn = file->f_op->read;
755 fnv = file->f_op->aio_read;
756 } else {
757 fn = (io_fn_t)file->f_op->write;
758 fnv = file->f_op->aio_write;
759 }
760
761 if (fnv)
762 ret = do_sync_readv_writev(file, iov, nr_segs, tot_len,
763 pos, fnv);
764 else
765 ret = do_loop_readv_writev(file, iov, nr_segs, pos, fn);
766
767 out:
768 if (iov != iovstack)
769 kfree(iov);
770 if ((ret + (type == READ)) > 0) {
771 if (type == READ)
772 fsnotify_access(file);
773 else
774 fsnotify_modify(file);
775 }
776 return ret;
777 }
778
779 ssize_t vfs_readv(struct file *file, const struct iovec __user *vec,
780 unsigned long vlen, loff_t *pos)
781 {
782 if (!(file->f_mode & FMODE_READ))
783 return -EBADF;
784 if (!file->f_op || (!file->f_op->aio_read && !file->f_op->read))
785 return -EINVAL;
786
787 return do_readv_writev(READ, file, vec, vlen, pos);
788 }
789
790 EXPORT_SYMBOL(vfs_readv);
791
792 ssize_t vfs_writev(struct file *file, const struct iovec __user *vec,
793 unsigned long vlen, loff_t *pos)
794 {
795 if (!(file->f_mode & FMODE_WRITE))
796 return -EBADF;
797 if (!file->f_op || (!file->f_op->aio_write && !file->f_op->write))
798 return -EINVAL;
799
800 return do_readv_writev(WRITE, file, vec, vlen, pos);
801 }
802
803 EXPORT_SYMBOL(vfs_writev);
804
805 SYSCALL_DEFINE3(readv, unsigned long, fd, const struct iovec __user *, vec,
806 unsigned long, vlen)
807 {
808 struct fd f = fdget(fd);
809 ssize_t ret = -EBADF;
810
811 if (f.file) {
812 loff_t pos = file_pos_read(f.file);
813 ret = vfs_readv(f.file, vec, vlen, &pos);
814 file_pos_write(f.file, pos);
815 fdput(f);
816 }
817
818 if (ret > 0)
819 add_rchar(current, ret);
820 inc_syscr(current);
821 return ret;
822 }
823
824 SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec __user *, vec,
825 unsigned long, vlen)
826 {
827 struct fd f = fdget(fd);
828 ssize_t ret = -EBADF;
829
830 if (f.file) {
831 loff_t pos = file_pos_read(f.file);
832 ret = vfs_writev(f.file, vec, vlen, &pos);
833 file_pos_write(f.file, pos);
834 fdput(f);
835 }
836
837 if (ret > 0)
838 add_wchar(current, ret);
839 inc_syscw(current);
840 return ret;
841 }
842
843 static inline loff_t pos_from_hilo(unsigned long high, unsigned long low)
844 {
845 #define HALF_LONG_BITS (BITS_PER_LONG / 2)
846 return (((loff_t)high << HALF_LONG_BITS) << HALF_LONG_BITS) | low;
847 }
848
849 SYSCALL_DEFINE5(preadv, unsigned long, fd, const struct iovec __user *, vec,
850 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
851 {
852 loff_t pos = pos_from_hilo(pos_h, pos_l);
853 struct fd f;
854 ssize_t ret = -EBADF;
855
856 if (pos < 0)
857 return -EINVAL;
858
859 f = fdget(fd);
860 if (f.file) {
861 ret = -ESPIPE;
862 if (f.file->f_mode & FMODE_PREAD)
863 ret = vfs_readv(f.file, vec, vlen, &pos);
864 fdput(f);
865 }
866
867 if (ret > 0)
868 add_rchar(current, ret);
869 inc_syscr(current);
870 return ret;
871 }
872
873 SYSCALL_DEFINE5(pwritev, unsigned long, fd, const struct iovec __user *, vec,
874 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
875 {
876 loff_t pos = pos_from_hilo(pos_h, pos_l);
877 struct fd f;
878 ssize_t ret = -EBADF;
879
880 if (pos < 0)
881 return -EINVAL;
882
883 f = fdget(fd);
884 if (f.file) {
885 ret = -ESPIPE;
886 if (f.file->f_mode & FMODE_PWRITE)
887 ret = vfs_writev(f.file, vec, vlen, &pos);
888 fdput(f);
889 }
890
891 if (ret > 0)
892 add_wchar(current, ret);
893 inc_syscw(current);
894 return ret;
895 }
896
897 ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos, size_t count,
898 loff_t max)
899 {
900 struct fd in, out;
901 struct inode *in_inode, *out_inode;
902 loff_t pos;
903 ssize_t retval;
904 int fl;
905
906 /*
907 * Get input file, and verify that it is ok..
908 */
909 retval = -EBADF;
910 in = fdget(in_fd);
911 if (!in.file)
912 goto out;
913 if (!(in.file->f_mode & FMODE_READ))
914 goto fput_in;
915 retval = -ESPIPE;
916 if (!ppos)
917 ppos = &in.file->f_pos;
918 else
919 if (!(in.file->f_mode & FMODE_PREAD))
920 goto fput_in;
921 retval = rw_verify_area(READ, in.file, ppos, count);
922 if (retval < 0)
923 goto fput_in;
924 count = retval;
925
926 /*
927 * Get output file, and verify that it is ok..
928 */
929 retval = -EBADF;
930 out = fdget(out_fd);
931 if (!out.file)
932 goto fput_in;
933 if (!(out.file->f_mode & FMODE_WRITE))
934 goto fput_out;
935 retval = -EINVAL;
936 in_inode = file_inode(in.file);
937 out_inode = file_inode(out.file);
938 retval = rw_verify_area(WRITE, out.file, &out.file->f_pos, count);
939 if (retval < 0)
940 goto fput_out;
941 count = retval;
942
943 if (!max)
944 max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes);
945
946 pos = *ppos;
947 if (unlikely(pos + count > max)) {
948 retval = -EOVERFLOW;
949 if (pos >= max)
950 goto fput_out;
951 count = max - pos;
952 }
953
954 fl = 0;
955 #if 0
956 /*
957 * We need to debate whether we can enable this or not. The
958 * man page documents EAGAIN return for the output at least,
959 * and the application is arguably buggy if it doesn't expect
960 * EAGAIN on a non-blocking file descriptor.
961 */
962 if (in.file->f_flags & O_NONBLOCK)
963 fl = SPLICE_F_NONBLOCK;
964 #endif
965 retval = do_splice_direct(in.file, ppos, out.file, count, fl);
966
967 if (retval > 0) {
968 add_rchar(current, retval);
969 add_wchar(current, retval);
970 fsnotify_access(in.file);
971 fsnotify_modify(out.file);
972 }
973
974 inc_syscr(current);
975 inc_syscw(current);
976 if (*ppos > max)
977 retval = -EOVERFLOW;
978
979 fput_out:
980 fdput(out);
981 fput_in:
982 fdput(in);
983 out:
984 return retval;
985 }
986
987 SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd, off_t __user *, offset, size_t, count)
988 {
989 loff_t pos;
990 off_t off;
991 ssize_t ret;
992
993 if (offset) {
994 if (unlikely(get_user(off, offset)))
995 return -EFAULT;
996 pos = off;
997 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
998 if (unlikely(put_user(pos, offset)))
999 return -EFAULT;
1000 return ret;
1001 }
1002
1003 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1004 }
1005
1006 SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd, loff_t __user *, offset, size_t, count)
1007 {
1008 loff_t pos;
1009 ssize_t ret;
1010
1011 if (offset) {
1012 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
1013 return -EFAULT;
1014 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
1015 if (unlikely(put_user(pos, offset)))
1016 return -EFAULT;
1017 return ret;
1018 }
1019
1020 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1021 }
This page took 0.092558 seconds and 6 git commands to generate.