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