324ec271cc4e64868c34e3ff2f28ac2c0542475e
[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 <linux/mount.h>
20 #include "internal.h"
21
22 #include <asm/uaccess.h>
23 #include <asm/unistd.h>
24
25 typedef ssize_t (*io_fn_t)(struct file *, char __user *, size_t, loff_t *);
26 typedef ssize_t (*iter_fn_t)(struct kiocb *, struct iov_iter *);
27
28 const struct file_operations generic_ro_fops = {
29 .llseek = generic_file_llseek,
30 .read_iter = generic_file_read_iter,
31 .mmap = generic_file_readonly_mmap,
32 .splice_read = generic_file_splice_read,
33 };
34
35 EXPORT_SYMBOL(generic_ro_fops);
36
37 static inline int unsigned_offsets(struct file *file)
38 {
39 return file->f_mode & FMODE_UNSIGNED_OFFSET;
40 }
41
42 /**
43 * vfs_setpos - update the file offset for lseek
44 * @file: file structure in question
45 * @offset: file offset to seek to
46 * @maxsize: maximum file size
47 *
48 * This is a low-level filesystem helper for updating the file offset to
49 * the value specified by @offset if the given offset is valid and it is
50 * not equal to the current file offset.
51 *
52 * Return the specified offset on success and -EINVAL on invalid offset.
53 */
54 loff_t vfs_setpos(struct file *file, loff_t offset, loff_t maxsize)
55 {
56 if (offset < 0 && !unsigned_offsets(file))
57 return -EINVAL;
58 if (offset > maxsize)
59 return -EINVAL;
60
61 if (offset != file->f_pos) {
62 file->f_pos = offset;
63 file->f_version = 0;
64 }
65 return offset;
66 }
67 EXPORT_SYMBOL(vfs_setpos);
68
69 /**
70 * generic_file_llseek_size - generic llseek implementation for regular files
71 * @file: file structure to seek on
72 * @offset: file offset to seek to
73 * @whence: type of seek
74 * @size: max size of this file in file system
75 * @eof: offset used for SEEK_END position
76 *
77 * This is a variant of generic_file_llseek that allows passing in a custom
78 * maximum file size and a custom EOF position, for e.g. hashed directories
79 *
80 * Synchronization:
81 * SEEK_SET and SEEK_END are unsynchronized (but atomic on 64bit platforms)
82 * SEEK_CUR is synchronized against other SEEK_CURs, but not read/writes.
83 * read/writes behave like SEEK_SET against seeks.
84 */
85 loff_t
86 generic_file_llseek_size(struct file *file, loff_t offset, int whence,
87 loff_t maxsize, loff_t eof)
88 {
89 switch (whence) {
90 case SEEK_END:
91 offset += eof;
92 break;
93 case SEEK_CUR:
94 /*
95 * Here we special-case the lseek(fd, 0, SEEK_CUR)
96 * position-querying operation. Avoid rewriting the "same"
97 * f_pos value back to the file because a concurrent read(),
98 * write() or lseek() might have altered it
99 */
100 if (offset == 0)
101 return file->f_pos;
102 /*
103 * f_lock protects against read/modify/write race with other
104 * SEEK_CURs. Note that parallel writes and reads behave
105 * like SEEK_SET.
106 */
107 spin_lock(&file->f_lock);
108 offset = vfs_setpos(file, file->f_pos + offset, maxsize);
109 spin_unlock(&file->f_lock);
110 return offset;
111 case SEEK_DATA:
112 /*
113 * In the generic case the entire file is data, so as long as
114 * offset isn't at the end of the file then the offset is data.
115 */
116 if (offset >= eof)
117 return -ENXIO;
118 break;
119 case SEEK_HOLE:
120 /*
121 * There is a virtual hole at the end of the file, so as long as
122 * offset isn't i_size or larger, return i_size.
123 */
124 if (offset >= eof)
125 return -ENXIO;
126 offset = eof;
127 break;
128 }
129
130 return vfs_setpos(file, offset, maxsize);
131 }
132 EXPORT_SYMBOL(generic_file_llseek_size);
133
134 /**
135 * generic_file_llseek - generic llseek implementation for regular files
136 * @file: file structure to seek on
137 * @offset: file offset to seek to
138 * @whence: type of seek
139 *
140 * This is a generic implemenation of ->llseek useable for all normal local
141 * filesystems. It just updates the file offset to the value specified by
142 * @offset and @whence.
143 */
144 loff_t generic_file_llseek(struct file *file, loff_t offset, int whence)
145 {
146 struct inode *inode = file->f_mapping->host;
147
148 return generic_file_llseek_size(file, offset, whence,
149 inode->i_sb->s_maxbytes,
150 i_size_read(inode));
151 }
152 EXPORT_SYMBOL(generic_file_llseek);
153
154 /**
155 * fixed_size_llseek - llseek implementation for fixed-sized devices
156 * @file: file structure to seek on
157 * @offset: file offset to seek to
158 * @whence: type of seek
159 * @size: size of the file
160 *
161 */
162 loff_t fixed_size_llseek(struct file *file, loff_t offset, int whence, loff_t size)
163 {
164 switch (whence) {
165 case SEEK_SET: case SEEK_CUR: case SEEK_END:
166 return generic_file_llseek_size(file, offset, whence,
167 size, size);
168 default:
169 return -EINVAL;
170 }
171 }
172 EXPORT_SYMBOL(fixed_size_llseek);
173
174 /**
175 * no_seek_end_llseek - llseek implementation for fixed-sized devices
176 * @file: file structure to seek on
177 * @offset: file offset to seek to
178 * @whence: type of seek
179 *
180 */
181 loff_t no_seek_end_llseek(struct file *file, loff_t offset, int whence)
182 {
183 switch (whence) {
184 case SEEK_SET: case SEEK_CUR:
185 return generic_file_llseek_size(file, offset, whence,
186 ~0ULL, 0);
187 default:
188 return -EINVAL;
189 }
190 }
191 EXPORT_SYMBOL(no_seek_end_llseek);
192
193 /**
194 * no_seek_end_llseek_size - llseek implementation for fixed-sized devices
195 * @file: file structure to seek on
196 * @offset: file offset to seek to
197 * @whence: type of seek
198 * @size: maximal offset allowed
199 *
200 */
201 loff_t no_seek_end_llseek_size(struct file *file, loff_t offset, int whence, loff_t size)
202 {
203 switch (whence) {
204 case SEEK_SET: case SEEK_CUR:
205 return generic_file_llseek_size(file, offset, whence,
206 size, 0);
207 default:
208 return -EINVAL;
209 }
210 }
211 EXPORT_SYMBOL(no_seek_end_llseek_size);
212
213 /**
214 * noop_llseek - No Operation Performed llseek implementation
215 * @file: file structure to seek on
216 * @offset: file offset to seek to
217 * @whence: type of seek
218 *
219 * This is an implementation of ->llseek useable for the rare special case when
220 * userspace expects the seek to succeed but the (device) file is actually not
221 * able to perform the seek. In this case you use noop_llseek() instead of
222 * falling back to the default implementation of ->llseek.
223 */
224 loff_t noop_llseek(struct file *file, loff_t offset, int whence)
225 {
226 return file->f_pos;
227 }
228 EXPORT_SYMBOL(noop_llseek);
229
230 loff_t no_llseek(struct file *file, loff_t offset, int whence)
231 {
232 return -ESPIPE;
233 }
234 EXPORT_SYMBOL(no_llseek);
235
236 loff_t default_llseek(struct file *file, loff_t offset, int whence)
237 {
238 struct inode *inode = file_inode(file);
239 loff_t retval;
240
241 inode_lock(inode);
242 switch (whence) {
243 case SEEK_END:
244 offset += i_size_read(inode);
245 break;
246 case SEEK_CUR:
247 if (offset == 0) {
248 retval = file->f_pos;
249 goto out;
250 }
251 offset += file->f_pos;
252 break;
253 case SEEK_DATA:
254 /*
255 * In the generic case the entire file is data, so as
256 * long as offset isn't at the end of the file then the
257 * offset is data.
258 */
259 if (offset >= inode->i_size) {
260 retval = -ENXIO;
261 goto out;
262 }
263 break;
264 case SEEK_HOLE:
265 /*
266 * There is a virtual hole at the end of the file, so
267 * as long as offset isn't i_size or larger, return
268 * i_size.
269 */
270 if (offset >= inode->i_size) {
271 retval = -ENXIO;
272 goto out;
273 }
274 offset = inode->i_size;
275 break;
276 }
277 retval = -EINVAL;
278 if (offset >= 0 || unsigned_offsets(file)) {
279 if (offset != file->f_pos) {
280 file->f_pos = offset;
281 file->f_version = 0;
282 }
283 retval = offset;
284 }
285 out:
286 inode_unlock(inode);
287 return retval;
288 }
289 EXPORT_SYMBOL(default_llseek);
290
291 loff_t vfs_llseek(struct file *file, loff_t offset, int whence)
292 {
293 loff_t (*fn)(struct file *, loff_t, int);
294
295 fn = no_llseek;
296 if (file->f_mode & FMODE_LSEEK) {
297 if (file->f_op->llseek)
298 fn = file->f_op->llseek;
299 }
300 return fn(file, offset, whence);
301 }
302 EXPORT_SYMBOL(vfs_llseek);
303
304 static inline struct fd fdget_pos(int fd)
305 {
306 return __to_fd(__fdget_pos(fd));
307 }
308
309 static inline void fdput_pos(struct fd f)
310 {
311 if (f.flags & FDPUT_POS_UNLOCK)
312 mutex_unlock(&f.file->f_pos_lock);
313 fdput(f);
314 }
315
316 SYSCALL_DEFINE3(lseek, unsigned int, fd, off_t, offset, unsigned int, whence)
317 {
318 off_t retval;
319 struct fd f = fdget_pos(fd);
320 if (!f.file)
321 return -EBADF;
322
323 retval = -EINVAL;
324 if (whence <= SEEK_MAX) {
325 loff_t res = vfs_llseek(f.file, offset, whence);
326 retval = res;
327 if (res != (loff_t)retval)
328 retval = -EOVERFLOW; /* LFS: should only happen on 32 bit platforms */
329 }
330 fdput_pos(f);
331 return retval;
332 }
333
334 #ifdef CONFIG_COMPAT
335 COMPAT_SYSCALL_DEFINE3(lseek, unsigned int, fd, compat_off_t, offset, unsigned int, whence)
336 {
337 return sys_lseek(fd, offset, whence);
338 }
339 #endif
340
341 #ifdef __ARCH_WANT_SYS_LLSEEK
342 SYSCALL_DEFINE5(llseek, unsigned int, fd, unsigned long, offset_high,
343 unsigned long, offset_low, loff_t __user *, result,
344 unsigned int, whence)
345 {
346 int retval;
347 struct fd f = fdget_pos(fd);
348 loff_t offset;
349
350 if (!f.file)
351 return -EBADF;
352
353 retval = -EINVAL;
354 if (whence > SEEK_MAX)
355 goto out_putf;
356
357 offset = vfs_llseek(f.file, ((loff_t) offset_high << 32) | offset_low,
358 whence);
359
360 retval = (int)offset;
361 if (offset >= 0) {
362 retval = -EFAULT;
363 if (!copy_to_user(result, &offset, sizeof(offset)))
364 retval = 0;
365 }
366 out_putf:
367 fdput_pos(f);
368 return retval;
369 }
370 #endif
371
372 ssize_t vfs_iter_read(struct file *file, struct iov_iter *iter, loff_t *ppos)
373 {
374 struct kiocb kiocb;
375 ssize_t ret;
376
377 if (!file->f_op->read_iter)
378 return -EINVAL;
379
380 init_sync_kiocb(&kiocb, file);
381 kiocb.ki_pos = *ppos;
382
383 iter->type |= READ;
384 ret = file->f_op->read_iter(&kiocb, iter);
385 BUG_ON(ret == -EIOCBQUEUED);
386 if (ret > 0)
387 *ppos = kiocb.ki_pos;
388 return ret;
389 }
390 EXPORT_SYMBOL(vfs_iter_read);
391
392 ssize_t vfs_iter_write(struct file *file, struct iov_iter *iter, loff_t *ppos)
393 {
394 struct kiocb kiocb;
395 ssize_t ret;
396
397 if (!file->f_op->write_iter)
398 return -EINVAL;
399
400 init_sync_kiocb(&kiocb, file);
401 kiocb.ki_pos = *ppos;
402
403 iter->type |= WRITE;
404 ret = file->f_op->write_iter(&kiocb, iter);
405 BUG_ON(ret == -EIOCBQUEUED);
406 if (ret > 0)
407 *ppos = kiocb.ki_pos;
408 return ret;
409 }
410 EXPORT_SYMBOL(vfs_iter_write);
411
412 /*
413 * rw_verify_area doesn't like huge counts. We limit
414 * them to something that fits in "int" so that others
415 * won't have to do range checks all the time.
416 */
417 int rw_verify_area(int read_write, struct file *file, const loff_t *ppos, size_t count)
418 {
419 struct inode *inode;
420 loff_t pos;
421 int retval = -EINVAL;
422
423 inode = file_inode(file);
424 if (unlikely((ssize_t) count < 0))
425 return retval;
426 pos = *ppos;
427 if (unlikely(pos < 0)) {
428 if (!unsigned_offsets(file))
429 return retval;
430 if (count >= -pos) /* both values are in 0..LLONG_MAX */
431 return -EOVERFLOW;
432 } else if (unlikely((loff_t) (pos + count) < 0)) {
433 if (!unsigned_offsets(file))
434 return retval;
435 }
436
437 if (unlikely(inode->i_flctx && mandatory_lock(inode))) {
438 retval = locks_mandatory_area(inode, file, pos, pos + count - 1,
439 read_write == READ ? F_RDLCK : F_WRLCK);
440 if (retval < 0)
441 return retval;
442 }
443 retval = security_file_permission(file,
444 read_write == READ ? MAY_READ : MAY_WRITE);
445 if (retval)
446 return retval;
447 return count > MAX_RW_COUNT ? MAX_RW_COUNT : count;
448 }
449
450 static ssize_t new_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
451 {
452 struct iovec iov = { .iov_base = buf, .iov_len = len };
453 struct kiocb kiocb;
454 struct iov_iter iter;
455 ssize_t ret;
456
457 init_sync_kiocb(&kiocb, filp);
458 kiocb.ki_pos = *ppos;
459 iov_iter_init(&iter, READ, &iov, 1, len);
460
461 ret = filp->f_op->read_iter(&kiocb, &iter);
462 BUG_ON(ret == -EIOCBQUEUED);
463 *ppos = kiocb.ki_pos;
464 return ret;
465 }
466
467 ssize_t __vfs_read(struct file *file, char __user *buf, size_t count,
468 loff_t *pos)
469 {
470 if (file->f_op->read)
471 return file->f_op->read(file, buf, count, pos);
472 else if (file->f_op->read_iter)
473 return new_sync_read(file, buf, count, pos);
474 else
475 return -EINVAL;
476 }
477 EXPORT_SYMBOL(__vfs_read);
478
479 ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
480 {
481 ssize_t ret;
482
483 if (!(file->f_mode & FMODE_READ))
484 return -EBADF;
485 if (!(file->f_mode & FMODE_CAN_READ))
486 return -EINVAL;
487 if (unlikely(!access_ok(VERIFY_WRITE, buf, count)))
488 return -EFAULT;
489
490 ret = rw_verify_area(READ, file, pos, count);
491 if (ret >= 0) {
492 count = ret;
493 ret = __vfs_read(file, buf, count, pos);
494 if (ret > 0) {
495 fsnotify_access(file);
496 add_rchar(current, ret);
497 }
498 inc_syscr(current);
499 }
500
501 return ret;
502 }
503
504 EXPORT_SYMBOL(vfs_read);
505
506 static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
507 {
508 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len };
509 struct kiocb kiocb;
510 struct iov_iter iter;
511 ssize_t ret;
512
513 init_sync_kiocb(&kiocb, filp);
514 kiocb.ki_pos = *ppos;
515 iov_iter_init(&iter, WRITE, &iov, 1, len);
516
517 ret = filp->f_op->write_iter(&kiocb, &iter);
518 BUG_ON(ret == -EIOCBQUEUED);
519 if (ret > 0)
520 *ppos = kiocb.ki_pos;
521 return ret;
522 }
523
524 ssize_t __vfs_write(struct file *file, const char __user *p, size_t count,
525 loff_t *pos)
526 {
527 if (file->f_op->write)
528 return file->f_op->write(file, p, count, pos);
529 else if (file->f_op->write_iter)
530 return new_sync_write(file, p, count, pos);
531 else
532 return -EINVAL;
533 }
534 EXPORT_SYMBOL(__vfs_write);
535
536 ssize_t __kernel_write(struct file *file, const char *buf, size_t count, loff_t *pos)
537 {
538 mm_segment_t old_fs;
539 const char __user *p;
540 ssize_t ret;
541
542 if (!(file->f_mode & FMODE_CAN_WRITE))
543 return -EINVAL;
544
545 old_fs = get_fs();
546 set_fs(get_ds());
547 p = (__force const char __user *)buf;
548 if (count > MAX_RW_COUNT)
549 count = MAX_RW_COUNT;
550 ret = __vfs_write(file, p, count, pos);
551 set_fs(old_fs);
552 if (ret > 0) {
553 fsnotify_modify(file);
554 add_wchar(current, ret);
555 }
556 inc_syscw(current);
557 return ret;
558 }
559
560 EXPORT_SYMBOL(__kernel_write);
561
562 ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
563 {
564 ssize_t ret;
565
566 if (!(file->f_mode & FMODE_WRITE))
567 return -EBADF;
568 if (!(file->f_mode & FMODE_CAN_WRITE))
569 return -EINVAL;
570 if (unlikely(!access_ok(VERIFY_READ, buf, count)))
571 return -EFAULT;
572
573 ret = rw_verify_area(WRITE, file, pos, count);
574 if (ret >= 0) {
575 count = ret;
576 file_start_write(file);
577 ret = __vfs_write(file, buf, count, pos);
578 if (ret > 0) {
579 fsnotify_modify(file);
580 add_wchar(current, ret);
581 }
582 inc_syscw(current);
583 file_end_write(file);
584 }
585
586 return ret;
587 }
588
589 EXPORT_SYMBOL(vfs_write);
590
591 static inline loff_t file_pos_read(struct file *file)
592 {
593 return file->f_pos;
594 }
595
596 static inline void file_pos_write(struct file *file, loff_t pos)
597 {
598 file->f_pos = pos;
599 }
600
601 SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count)
602 {
603 struct fd f = fdget_pos(fd);
604 ssize_t ret = -EBADF;
605
606 if (f.file) {
607 loff_t pos = file_pos_read(f.file);
608 ret = vfs_read(f.file, buf, count, &pos);
609 if (ret >= 0)
610 file_pos_write(f.file, pos);
611 fdput_pos(f);
612 }
613 return ret;
614 }
615
616 SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf,
617 size_t, count)
618 {
619 struct fd f = fdget_pos(fd);
620 ssize_t ret = -EBADF;
621
622 if (f.file) {
623 loff_t pos = file_pos_read(f.file);
624 ret = vfs_write(f.file, buf, count, &pos);
625 if (ret >= 0)
626 file_pos_write(f.file, pos);
627 fdput_pos(f);
628 }
629
630 return ret;
631 }
632
633 SYSCALL_DEFINE4(pread64, unsigned int, fd, char __user *, buf,
634 size_t, count, loff_t, pos)
635 {
636 struct fd f;
637 ssize_t ret = -EBADF;
638
639 if (pos < 0)
640 return -EINVAL;
641
642 f = fdget(fd);
643 if (f.file) {
644 ret = -ESPIPE;
645 if (f.file->f_mode & FMODE_PREAD)
646 ret = vfs_read(f.file, buf, count, &pos);
647 fdput(f);
648 }
649
650 return ret;
651 }
652
653 SYSCALL_DEFINE4(pwrite64, unsigned int, fd, const char __user *, buf,
654 size_t, count, loff_t, pos)
655 {
656 struct fd f;
657 ssize_t ret = -EBADF;
658
659 if (pos < 0)
660 return -EINVAL;
661
662 f = fdget(fd);
663 if (f.file) {
664 ret = -ESPIPE;
665 if (f.file->f_mode & FMODE_PWRITE)
666 ret = vfs_write(f.file, buf, count, &pos);
667 fdput(f);
668 }
669
670 return ret;
671 }
672
673 /*
674 * Reduce an iovec's length in-place. Return the resulting number of segments
675 */
676 unsigned long iov_shorten(struct iovec *iov, unsigned long nr_segs, size_t to)
677 {
678 unsigned long seg = 0;
679 size_t len = 0;
680
681 while (seg < nr_segs) {
682 seg++;
683 if (len + iov->iov_len >= to) {
684 iov->iov_len = to - len;
685 break;
686 }
687 len += iov->iov_len;
688 iov++;
689 }
690 return seg;
691 }
692 EXPORT_SYMBOL(iov_shorten);
693
694 static ssize_t do_iter_readv_writev(struct file *filp, struct iov_iter *iter,
695 loff_t *ppos, iter_fn_t fn)
696 {
697 struct kiocb kiocb;
698 ssize_t ret;
699
700 init_sync_kiocb(&kiocb, filp);
701 kiocb.ki_pos = *ppos;
702
703 ret = fn(&kiocb, iter);
704 BUG_ON(ret == -EIOCBQUEUED);
705 *ppos = kiocb.ki_pos;
706 return ret;
707 }
708
709 /* Do it by hand, with file-ops */
710 static ssize_t do_loop_readv_writev(struct file *filp, struct iov_iter *iter,
711 loff_t *ppos, io_fn_t fn)
712 {
713 ssize_t ret = 0;
714
715 while (iov_iter_count(iter)) {
716 struct iovec iovec = iov_iter_iovec(iter);
717 ssize_t nr;
718
719 nr = fn(filp, iovec.iov_base, iovec.iov_len, ppos);
720
721 if (nr < 0) {
722 if (!ret)
723 ret = nr;
724 break;
725 }
726 ret += nr;
727 if (nr != iovec.iov_len)
728 break;
729 iov_iter_advance(iter, nr);
730 }
731
732 return ret;
733 }
734
735 /* A write operation does a read from user space and vice versa */
736 #define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
737
738 ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
739 unsigned long nr_segs, unsigned long fast_segs,
740 struct iovec *fast_pointer,
741 struct iovec **ret_pointer)
742 {
743 unsigned long seg;
744 ssize_t ret;
745 struct iovec *iov = fast_pointer;
746
747 /*
748 * SuS says "The readv() function *may* fail if the iovcnt argument
749 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
750 * traditionally returned zero for zero segments, so...
751 */
752 if (nr_segs == 0) {
753 ret = 0;
754 goto out;
755 }
756
757 /*
758 * First get the "struct iovec" from user memory and
759 * verify all the pointers
760 */
761 if (nr_segs > UIO_MAXIOV) {
762 ret = -EINVAL;
763 goto out;
764 }
765 if (nr_segs > fast_segs) {
766 iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
767 if (iov == NULL) {
768 ret = -ENOMEM;
769 goto out;
770 }
771 }
772 if (copy_from_user(iov, uvector, nr_segs*sizeof(*uvector))) {
773 ret = -EFAULT;
774 goto out;
775 }
776
777 /*
778 * According to the Single Unix Specification we should return EINVAL
779 * if an element length is < 0 when cast to ssize_t or if the
780 * total length would overflow the ssize_t return value of the
781 * system call.
782 *
783 * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
784 * overflow case.
785 */
786 ret = 0;
787 for (seg = 0; seg < nr_segs; seg++) {
788 void __user *buf = iov[seg].iov_base;
789 ssize_t len = (ssize_t)iov[seg].iov_len;
790
791 /* see if we we're about to use an invalid len or if
792 * it's about to overflow ssize_t */
793 if (len < 0) {
794 ret = -EINVAL;
795 goto out;
796 }
797 if (type >= 0
798 && unlikely(!access_ok(vrfy_dir(type), buf, len))) {
799 ret = -EFAULT;
800 goto out;
801 }
802 if (len > MAX_RW_COUNT - ret) {
803 len = MAX_RW_COUNT - ret;
804 iov[seg].iov_len = len;
805 }
806 ret += len;
807 }
808 out:
809 *ret_pointer = iov;
810 return ret;
811 }
812
813 static ssize_t do_readv_writev(int type, struct file *file,
814 const struct iovec __user * uvector,
815 unsigned long nr_segs, loff_t *pos)
816 {
817 size_t tot_len;
818 struct iovec iovstack[UIO_FASTIOV];
819 struct iovec *iov = iovstack;
820 struct iov_iter iter;
821 ssize_t ret;
822 io_fn_t fn;
823 iter_fn_t iter_fn;
824
825 ret = import_iovec(type, uvector, nr_segs,
826 ARRAY_SIZE(iovstack), &iov, &iter);
827 if (ret < 0)
828 return ret;
829
830 tot_len = iov_iter_count(&iter);
831 if (!tot_len)
832 goto out;
833 ret = rw_verify_area(type, file, pos, tot_len);
834 if (ret < 0)
835 goto out;
836
837 if (type == READ) {
838 fn = file->f_op->read;
839 iter_fn = file->f_op->read_iter;
840 } else {
841 fn = (io_fn_t)file->f_op->write;
842 iter_fn = file->f_op->write_iter;
843 file_start_write(file);
844 }
845
846 if (iter_fn)
847 ret = do_iter_readv_writev(file, &iter, pos, iter_fn);
848 else
849 ret = do_loop_readv_writev(file, &iter, pos, fn);
850
851 if (type != READ)
852 file_end_write(file);
853
854 out:
855 kfree(iov);
856 if ((ret + (type == READ)) > 0) {
857 if (type == READ)
858 fsnotify_access(file);
859 else
860 fsnotify_modify(file);
861 }
862 return ret;
863 }
864
865 ssize_t vfs_readv(struct file *file, const struct iovec __user *vec,
866 unsigned long vlen, loff_t *pos)
867 {
868 if (!(file->f_mode & FMODE_READ))
869 return -EBADF;
870 if (!(file->f_mode & FMODE_CAN_READ))
871 return -EINVAL;
872
873 return do_readv_writev(READ, file, vec, vlen, pos);
874 }
875
876 EXPORT_SYMBOL(vfs_readv);
877
878 ssize_t vfs_writev(struct file *file, const struct iovec __user *vec,
879 unsigned long vlen, loff_t *pos)
880 {
881 if (!(file->f_mode & FMODE_WRITE))
882 return -EBADF;
883 if (!(file->f_mode & FMODE_CAN_WRITE))
884 return -EINVAL;
885
886 return do_readv_writev(WRITE, file, vec, vlen, pos);
887 }
888
889 EXPORT_SYMBOL(vfs_writev);
890
891 SYSCALL_DEFINE3(readv, unsigned long, fd, const struct iovec __user *, vec,
892 unsigned long, vlen)
893 {
894 struct fd f = fdget_pos(fd);
895 ssize_t ret = -EBADF;
896
897 if (f.file) {
898 loff_t pos = file_pos_read(f.file);
899 ret = vfs_readv(f.file, vec, vlen, &pos);
900 if (ret >= 0)
901 file_pos_write(f.file, pos);
902 fdput_pos(f);
903 }
904
905 if (ret > 0)
906 add_rchar(current, ret);
907 inc_syscr(current);
908 return ret;
909 }
910
911 SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec __user *, vec,
912 unsigned long, vlen)
913 {
914 struct fd f = fdget_pos(fd);
915 ssize_t ret = -EBADF;
916
917 if (f.file) {
918 loff_t pos = file_pos_read(f.file);
919 ret = vfs_writev(f.file, vec, vlen, &pos);
920 if (ret >= 0)
921 file_pos_write(f.file, pos);
922 fdput_pos(f);
923 }
924
925 if (ret > 0)
926 add_wchar(current, ret);
927 inc_syscw(current);
928 return ret;
929 }
930
931 static inline loff_t pos_from_hilo(unsigned long high, unsigned long low)
932 {
933 #define HALF_LONG_BITS (BITS_PER_LONG / 2)
934 return (((loff_t)high << HALF_LONG_BITS) << HALF_LONG_BITS) | low;
935 }
936
937 SYSCALL_DEFINE5(preadv, unsigned long, fd, const struct iovec __user *, vec,
938 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
939 {
940 loff_t pos = pos_from_hilo(pos_h, pos_l);
941 struct fd f;
942 ssize_t ret = -EBADF;
943
944 if (pos < 0)
945 return -EINVAL;
946
947 f = fdget(fd);
948 if (f.file) {
949 ret = -ESPIPE;
950 if (f.file->f_mode & FMODE_PREAD)
951 ret = vfs_readv(f.file, vec, vlen, &pos);
952 fdput(f);
953 }
954
955 if (ret > 0)
956 add_rchar(current, ret);
957 inc_syscr(current);
958 return ret;
959 }
960
961 SYSCALL_DEFINE5(pwritev, unsigned long, fd, const struct iovec __user *, vec,
962 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
963 {
964 loff_t pos = pos_from_hilo(pos_h, pos_l);
965 struct fd f;
966 ssize_t ret = -EBADF;
967
968 if (pos < 0)
969 return -EINVAL;
970
971 f = fdget(fd);
972 if (f.file) {
973 ret = -ESPIPE;
974 if (f.file->f_mode & FMODE_PWRITE)
975 ret = vfs_writev(f.file, vec, vlen, &pos);
976 fdput(f);
977 }
978
979 if (ret > 0)
980 add_wchar(current, ret);
981 inc_syscw(current);
982 return ret;
983 }
984
985 #ifdef CONFIG_COMPAT
986
987 static ssize_t compat_do_readv_writev(int type, struct file *file,
988 const struct compat_iovec __user *uvector,
989 unsigned long nr_segs, loff_t *pos)
990 {
991 compat_ssize_t tot_len;
992 struct iovec iovstack[UIO_FASTIOV];
993 struct iovec *iov = iovstack;
994 struct iov_iter iter;
995 ssize_t ret;
996 io_fn_t fn;
997 iter_fn_t iter_fn;
998
999 ret = compat_import_iovec(type, uvector, nr_segs,
1000 UIO_FASTIOV, &iov, &iter);
1001 if (ret < 0)
1002 return ret;
1003
1004 tot_len = iov_iter_count(&iter);
1005 if (!tot_len)
1006 goto out;
1007 ret = rw_verify_area(type, file, pos, tot_len);
1008 if (ret < 0)
1009 goto out;
1010
1011 if (type == READ) {
1012 fn = file->f_op->read;
1013 iter_fn = file->f_op->read_iter;
1014 } else {
1015 fn = (io_fn_t)file->f_op->write;
1016 iter_fn = file->f_op->write_iter;
1017 file_start_write(file);
1018 }
1019
1020 if (iter_fn)
1021 ret = do_iter_readv_writev(file, &iter, pos, iter_fn);
1022 else
1023 ret = do_loop_readv_writev(file, &iter, pos, fn);
1024
1025 if (type != READ)
1026 file_end_write(file);
1027
1028 out:
1029 kfree(iov);
1030 if ((ret + (type == READ)) > 0) {
1031 if (type == READ)
1032 fsnotify_access(file);
1033 else
1034 fsnotify_modify(file);
1035 }
1036 return ret;
1037 }
1038
1039 static size_t compat_readv(struct file *file,
1040 const struct compat_iovec __user *vec,
1041 unsigned long vlen, loff_t *pos)
1042 {
1043 ssize_t ret = -EBADF;
1044
1045 if (!(file->f_mode & FMODE_READ))
1046 goto out;
1047
1048 ret = -EINVAL;
1049 if (!(file->f_mode & FMODE_CAN_READ))
1050 goto out;
1051
1052 ret = compat_do_readv_writev(READ, file, vec, vlen, pos);
1053
1054 out:
1055 if (ret > 0)
1056 add_rchar(current, ret);
1057 inc_syscr(current);
1058 return ret;
1059 }
1060
1061 COMPAT_SYSCALL_DEFINE3(readv, compat_ulong_t, fd,
1062 const struct compat_iovec __user *,vec,
1063 compat_ulong_t, vlen)
1064 {
1065 struct fd f = fdget_pos(fd);
1066 ssize_t ret;
1067 loff_t pos;
1068
1069 if (!f.file)
1070 return -EBADF;
1071 pos = f.file->f_pos;
1072 ret = compat_readv(f.file, vec, vlen, &pos);
1073 if (ret >= 0)
1074 f.file->f_pos = pos;
1075 fdput_pos(f);
1076 return ret;
1077 }
1078
1079 static long __compat_sys_preadv64(unsigned long fd,
1080 const struct compat_iovec __user *vec,
1081 unsigned long vlen, loff_t pos)
1082 {
1083 struct fd f;
1084 ssize_t ret;
1085
1086 if (pos < 0)
1087 return -EINVAL;
1088 f = fdget(fd);
1089 if (!f.file)
1090 return -EBADF;
1091 ret = -ESPIPE;
1092 if (f.file->f_mode & FMODE_PREAD)
1093 ret = compat_readv(f.file, vec, vlen, &pos);
1094 fdput(f);
1095 return ret;
1096 }
1097
1098 #ifdef __ARCH_WANT_COMPAT_SYS_PREADV64
1099 COMPAT_SYSCALL_DEFINE4(preadv64, unsigned long, fd,
1100 const struct compat_iovec __user *,vec,
1101 unsigned long, vlen, loff_t, pos)
1102 {
1103 return __compat_sys_preadv64(fd, vec, vlen, pos);
1104 }
1105 #endif
1106
1107 COMPAT_SYSCALL_DEFINE5(preadv, compat_ulong_t, fd,
1108 const struct compat_iovec __user *,vec,
1109 compat_ulong_t, vlen, u32, pos_low, u32, pos_high)
1110 {
1111 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
1112
1113 return __compat_sys_preadv64(fd, vec, vlen, pos);
1114 }
1115
1116 static size_t compat_writev(struct file *file,
1117 const struct compat_iovec __user *vec,
1118 unsigned long vlen, loff_t *pos)
1119 {
1120 ssize_t ret = -EBADF;
1121
1122 if (!(file->f_mode & FMODE_WRITE))
1123 goto out;
1124
1125 ret = -EINVAL;
1126 if (!(file->f_mode & FMODE_CAN_WRITE))
1127 goto out;
1128
1129 ret = compat_do_readv_writev(WRITE, file, vec, vlen, pos);
1130
1131 out:
1132 if (ret > 0)
1133 add_wchar(current, ret);
1134 inc_syscw(current);
1135 return ret;
1136 }
1137
1138 COMPAT_SYSCALL_DEFINE3(writev, compat_ulong_t, fd,
1139 const struct compat_iovec __user *, vec,
1140 compat_ulong_t, vlen)
1141 {
1142 struct fd f = fdget_pos(fd);
1143 ssize_t ret;
1144 loff_t pos;
1145
1146 if (!f.file)
1147 return -EBADF;
1148 pos = f.file->f_pos;
1149 ret = compat_writev(f.file, vec, vlen, &pos);
1150 if (ret >= 0)
1151 f.file->f_pos = pos;
1152 fdput_pos(f);
1153 return ret;
1154 }
1155
1156 static long __compat_sys_pwritev64(unsigned long fd,
1157 const struct compat_iovec __user *vec,
1158 unsigned long vlen, loff_t pos)
1159 {
1160 struct fd f;
1161 ssize_t ret;
1162
1163 if (pos < 0)
1164 return -EINVAL;
1165 f = fdget(fd);
1166 if (!f.file)
1167 return -EBADF;
1168 ret = -ESPIPE;
1169 if (f.file->f_mode & FMODE_PWRITE)
1170 ret = compat_writev(f.file, vec, vlen, &pos);
1171 fdput(f);
1172 return ret;
1173 }
1174
1175 #ifdef __ARCH_WANT_COMPAT_SYS_PWRITEV64
1176 COMPAT_SYSCALL_DEFINE4(pwritev64, unsigned long, fd,
1177 const struct compat_iovec __user *,vec,
1178 unsigned long, vlen, loff_t, pos)
1179 {
1180 return __compat_sys_pwritev64(fd, vec, vlen, pos);
1181 }
1182 #endif
1183
1184 COMPAT_SYSCALL_DEFINE5(pwritev, compat_ulong_t, fd,
1185 const struct compat_iovec __user *,vec,
1186 compat_ulong_t, vlen, u32, pos_low, u32, pos_high)
1187 {
1188 loff_t pos = ((loff_t)pos_high << 32) | pos_low;
1189
1190 return __compat_sys_pwritev64(fd, vec, vlen, pos);
1191 }
1192 #endif
1193
1194 static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,
1195 size_t count, loff_t max)
1196 {
1197 struct fd in, out;
1198 struct inode *in_inode, *out_inode;
1199 loff_t pos;
1200 loff_t out_pos;
1201 ssize_t retval;
1202 int fl;
1203
1204 /*
1205 * Get input file, and verify that it is ok..
1206 */
1207 retval = -EBADF;
1208 in = fdget(in_fd);
1209 if (!in.file)
1210 goto out;
1211 if (!(in.file->f_mode & FMODE_READ))
1212 goto fput_in;
1213 retval = -ESPIPE;
1214 if (!ppos) {
1215 pos = in.file->f_pos;
1216 } else {
1217 pos = *ppos;
1218 if (!(in.file->f_mode & FMODE_PREAD))
1219 goto fput_in;
1220 }
1221 retval = rw_verify_area(READ, in.file, &pos, count);
1222 if (retval < 0)
1223 goto fput_in;
1224 count = retval;
1225
1226 /*
1227 * Get output file, and verify that it is ok..
1228 */
1229 retval = -EBADF;
1230 out = fdget(out_fd);
1231 if (!out.file)
1232 goto fput_in;
1233 if (!(out.file->f_mode & FMODE_WRITE))
1234 goto fput_out;
1235 retval = -EINVAL;
1236 in_inode = file_inode(in.file);
1237 out_inode = file_inode(out.file);
1238 out_pos = out.file->f_pos;
1239 retval = rw_verify_area(WRITE, out.file, &out_pos, count);
1240 if (retval < 0)
1241 goto fput_out;
1242 count = retval;
1243
1244 if (!max)
1245 max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes);
1246
1247 if (unlikely(pos + count > max)) {
1248 retval = -EOVERFLOW;
1249 if (pos >= max)
1250 goto fput_out;
1251 count = max - pos;
1252 }
1253
1254 fl = 0;
1255 #if 0
1256 /*
1257 * We need to debate whether we can enable this or not. The
1258 * man page documents EAGAIN return for the output at least,
1259 * and the application is arguably buggy if it doesn't expect
1260 * EAGAIN on a non-blocking file descriptor.
1261 */
1262 if (in.file->f_flags & O_NONBLOCK)
1263 fl = SPLICE_F_NONBLOCK;
1264 #endif
1265 file_start_write(out.file);
1266 retval = do_splice_direct(in.file, &pos, out.file, &out_pos, count, fl);
1267 file_end_write(out.file);
1268
1269 if (retval > 0) {
1270 add_rchar(current, retval);
1271 add_wchar(current, retval);
1272 fsnotify_access(in.file);
1273 fsnotify_modify(out.file);
1274 out.file->f_pos = out_pos;
1275 if (ppos)
1276 *ppos = pos;
1277 else
1278 in.file->f_pos = pos;
1279 }
1280
1281 inc_syscr(current);
1282 inc_syscw(current);
1283 if (pos > max)
1284 retval = -EOVERFLOW;
1285
1286 fput_out:
1287 fdput(out);
1288 fput_in:
1289 fdput(in);
1290 out:
1291 return retval;
1292 }
1293
1294 SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd, off_t __user *, offset, size_t, count)
1295 {
1296 loff_t pos;
1297 off_t off;
1298 ssize_t ret;
1299
1300 if (offset) {
1301 if (unlikely(get_user(off, offset)))
1302 return -EFAULT;
1303 pos = off;
1304 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
1305 if (unlikely(put_user(pos, offset)))
1306 return -EFAULT;
1307 return ret;
1308 }
1309
1310 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1311 }
1312
1313 SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd, loff_t __user *, offset, size_t, count)
1314 {
1315 loff_t pos;
1316 ssize_t ret;
1317
1318 if (offset) {
1319 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
1320 return -EFAULT;
1321 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
1322 if (unlikely(put_user(pos, offset)))
1323 return -EFAULT;
1324 return ret;
1325 }
1326
1327 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1328 }
1329
1330 #ifdef CONFIG_COMPAT
1331 COMPAT_SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd,
1332 compat_off_t __user *, offset, compat_size_t, count)
1333 {
1334 loff_t pos;
1335 off_t off;
1336 ssize_t ret;
1337
1338 if (offset) {
1339 if (unlikely(get_user(off, offset)))
1340 return -EFAULT;
1341 pos = off;
1342 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
1343 if (unlikely(put_user(pos, offset)))
1344 return -EFAULT;
1345 return ret;
1346 }
1347
1348 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1349 }
1350
1351 COMPAT_SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd,
1352 compat_loff_t __user *, offset, compat_size_t, count)
1353 {
1354 loff_t pos;
1355 ssize_t ret;
1356
1357 if (offset) {
1358 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
1359 return -EFAULT;
1360 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
1361 if (unlikely(put_user(pos, offset)))
1362 return -EFAULT;
1363 return ret;
1364 }
1365
1366 return do_sendfile(out_fd, in_fd, NULL, count, 0);
1367 }
1368 #endif
1369
1370 /*
1371 * copy_file_range() differs from regular file read and write in that it
1372 * specifically allows return partial success. When it does so is up to
1373 * the copy_file_range method.
1374 */
1375 ssize_t vfs_copy_file_range(struct file *file_in, loff_t pos_in,
1376 struct file *file_out, loff_t pos_out,
1377 size_t len, unsigned int flags)
1378 {
1379 struct inode *inode_in = file_inode(file_in);
1380 struct inode *inode_out = file_inode(file_out);
1381 ssize_t ret;
1382
1383 if (flags != 0)
1384 return -EINVAL;
1385
1386 /* copy_file_range allows full ssize_t len, ignoring MAX_RW_COUNT */
1387 ret = rw_verify_area(READ, file_in, &pos_in, len);
1388 if (ret >= 0)
1389 ret = rw_verify_area(WRITE, file_out, &pos_out, len);
1390 if (ret < 0)
1391 return ret;
1392
1393 if (!(file_in->f_mode & FMODE_READ) ||
1394 !(file_out->f_mode & FMODE_WRITE) ||
1395 (file_out->f_flags & O_APPEND))
1396 return -EBADF;
1397
1398 /* this could be relaxed once a method supports cross-fs copies */
1399 if (inode_in->i_sb != inode_out->i_sb)
1400 return -EXDEV;
1401
1402 if (len == 0)
1403 return 0;
1404
1405 ret = mnt_want_write_file(file_out);
1406 if (ret)
1407 return ret;
1408
1409 ret = -EOPNOTSUPP;
1410 if (file_out->f_op->copy_file_range)
1411 ret = file_out->f_op->copy_file_range(file_in, pos_in, file_out,
1412 pos_out, len, flags);
1413 if (ret == -EOPNOTSUPP)
1414 ret = do_splice_direct(file_in, &pos_in, file_out, &pos_out,
1415 len > MAX_RW_COUNT ? MAX_RW_COUNT : len, 0);
1416
1417 if (ret > 0) {
1418 fsnotify_access(file_in);
1419 add_rchar(current, ret);
1420 fsnotify_modify(file_out);
1421 add_wchar(current, ret);
1422 }
1423 inc_syscr(current);
1424 inc_syscw(current);
1425
1426 mnt_drop_write_file(file_out);
1427
1428 return ret;
1429 }
1430 EXPORT_SYMBOL(vfs_copy_file_range);
1431
1432 SYSCALL_DEFINE6(copy_file_range, int, fd_in, loff_t __user *, off_in,
1433 int, fd_out, loff_t __user *, off_out,
1434 size_t, len, unsigned int, flags)
1435 {
1436 loff_t pos_in;
1437 loff_t pos_out;
1438 struct fd f_in;
1439 struct fd f_out;
1440 ssize_t ret = -EBADF;
1441
1442 f_in = fdget(fd_in);
1443 if (!f_in.file)
1444 goto out2;
1445
1446 f_out = fdget(fd_out);
1447 if (!f_out.file)
1448 goto out1;
1449
1450 ret = -EFAULT;
1451 if (off_in) {
1452 if (copy_from_user(&pos_in, off_in, sizeof(loff_t)))
1453 goto out;
1454 } else {
1455 pos_in = f_in.file->f_pos;
1456 }
1457
1458 if (off_out) {
1459 if (copy_from_user(&pos_out, off_out, sizeof(loff_t)))
1460 goto out;
1461 } else {
1462 pos_out = f_out.file->f_pos;
1463 }
1464
1465 ret = vfs_copy_file_range(f_in.file, pos_in, f_out.file, pos_out, len,
1466 flags);
1467 if (ret > 0) {
1468 pos_in += ret;
1469 pos_out += ret;
1470
1471 if (off_in) {
1472 if (copy_to_user(off_in, &pos_in, sizeof(loff_t)))
1473 ret = -EFAULT;
1474 } else {
1475 f_in.file->f_pos = pos_in;
1476 }
1477
1478 if (off_out) {
1479 if (copy_to_user(off_out, &pos_out, sizeof(loff_t)))
1480 ret = -EFAULT;
1481 } else {
1482 f_out.file->f_pos = pos_out;
1483 }
1484 }
1485
1486 out:
1487 fdput(f_out);
1488 out1:
1489 fdput(f_in);
1490 out2:
1491 return ret;
1492 }
1493
1494 static int clone_verify_area(struct file *file, loff_t pos, u64 len, bool write)
1495 {
1496 struct inode *inode = file_inode(file);
1497
1498 if (unlikely(pos < 0))
1499 return -EINVAL;
1500
1501 if (unlikely((loff_t) (pos + len) < 0))
1502 return -EINVAL;
1503
1504 if (unlikely(inode->i_flctx && mandatory_lock(inode))) {
1505 loff_t end = len ? pos + len - 1 : OFFSET_MAX;
1506 int retval;
1507
1508 retval = locks_mandatory_area(inode, file, pos, end,
1509 write ? F_WRLCK : F_RDLCK);
1510 if (retval < 0)
1511 return retval;
1512 }
1513
1514 return security_file_permission(file, write ? MAY_WRITE : MAY_READ);
1515 }
1516
1517 int vfs_clone_file_range(struct file *file_in, loff_t pos_in,
1518 struct file *file_out, loff_t pos_out, u64 len)
1519 {
1520 struct inode *inode_in = file_inode(file_in);
1521 struct inode *inode_out = file_inode(file_out);
1522 int ret;
1523
1524 if (inode_in->i_sb != inode_out->i_sb ||
1525 file_in->f_path.mnt != file_out->f_path.mnt)
1526 return -EXDEV;
1527
1528 if (S_ISDIR(inode_in->i_mode) || S_ISDIR(inode_out->i_mode))
1529 return -EISDIR;
1530 if (!S_ISREG(inode_in->i_mode) || !S_ISREG(inode_out->i_mode))
1531 return -EINVAL;
1532
1533 if (!(file_in->f_mode & FMODE_READ) ||
1534 !(file_out->f_mode & FMODE_WRITE) ||
1535 (file_out->f_flags & O_APPEND) ||
1536 !file_in->f_op->clone_file_range)
1537 return -EBADF;
1538
1539 ret = clone_verify_area(file_in, pos_in, len, false);
1540 if (ret)
1541 return ret;
1542
1543 ret = clone_verify_area(file_out, pos_out, len, true);
1544 if (ret)
1545 return ret;
1546
1547 if (pos_in + len > i_size_read(inode_in))
1548 return -EINVAL;
1549
1550 ret = mnt_want_write_file(file_out);
1551 if (ret)
1552 return ret;
1553
1554 ret = file_in->f_op->clone_file_range(file_in, pos_in,
1555 file_out, pos_out, len);
1556 if (!ret) {
1557 fsnotify_access(file_in);
1558 fsnotify_modify(file_out);
1559 }
1560
1561 mnt_drop_write_file(file_out);
1562 return ret;
1563 }
1564 EXPORT_SYMBOL(vfs_clone_file_range);
1565
1566 int vfs_dedupe_file_range(struct file *file, struct file_dedupe_range *same)
1567 {
1568 struct file_dedupe_range_info *info;
1569 struct inode *src = file_inode(file);
1570 u64 off;
1571 u64 len;
1572 int i;
1573 int ret;
1574 bool is_admin = capable(CAP_SYS_ADMIN);
1575 u16 count = same->dest_count;
1576 struct file *dst_file;
1577 loff_t dst_off;
1578 ssize_t deduped;
1579
1580 if (!(file->f_mode & FMODE_READ))
1581 return -EINVAL;
1582
1583 if (same->reserved1 || same->reserved2)
1584 return -EINVAL;
1585
1586 off = same->src_offset;
1587 len = same->src_length;
1588
1589 ret = -EISDIR;
1590 if (S_ISDIR(src->i_mode))
1591 goto out;
1592
1593 ret = -EINVAL;
1594 if (!S_ISREG(src->i_mode))
1595 goto out;
1596
1597 ret = clone_verify_area(file, off, len, false);
1598 if (ret < 0)
1599 goto out;
1600 ret = 0;
1601
1602 /* pre-format output fields to sane values */
1603 for (i = 0; i < count; i++) {
1604 same->info[i].bytes_deduped = 0ULL;
1605 same->info[i].status = FILE_DEDUPE_RANGE_SAME;
1606 }
1607
1608 for (i = 0, info = same->info; i < count; i++, info++) {
1609 struct inode *dst;
1610 struct fd dst_fd = fdget(info->dest_fd);
1611
1612 dst_file = dst_fd.file;
1613 if (!dst_file) {
1614 info->status = -EBADF;
1615 goto next_loop;
1616 }
1617 dst = file_inode(dst_file);
1618
1619 ret = mnt_want_write_file(dst_file);
1620 if (ret) {
1621 info->status = ret;
1622 goto next_loop;
1623 }
1624
1625 dst_off = info->dest_offset;
1626 ret = clone_verify_area(dst_file, dst_off, len, true);
1627 if (ret < 0) {
1628 info->status = ret;
1629 goto next_file;
1630 }
1631 ret = 0;
1632
1633 if (info->reserved) {
1634 info->status = -EINVAL;
1635 } else if (!(is_admin || (dst_file->f_mode & FMODE_WRITE))) {
1636 info->status = -EINVAL;
1637 } else if (file->f_path.mnt != dst_file->f_path.mnt) {
1638 info->status = -EXDEV;
1639 } else if (S_ISDIR(dst->i_mode)) {
1640 info->status = -EISDIR;
1641 } else if (dst_file->f_op->dedupe_file_range == NULL) {
1642 info->status = -EINVAL;
1643 } else {
1644 deduped = dst_file->f_op->dedupe_file_range(file, off,
1645 len, dst_file,
1646 info->dest_offset);
1647 if (deduped == -EBADE)
1648 info->status = FILE_DEDUPE_RANGE_DIFFERS;
1649 else if (deduped < 0)
1650 info->status = deduped;
1651 else
1652 info->bytes_deduped += deduped;
1653 }
1654
1655 next_file:
1656 mnt_drop_write_file(dst_file);
1657 next_loop:
1658 fdput(dst_fd);
1659
1660 if (fatal_signal_pending(current))
1661 goto out;
1662 }
1663
1664 out:
1665 return ret;
1666 }
1667 EXPORT_SYMBOL(vfs_dedupe_file_range);
This page took 0.068752 seconds and 5 git commands to generate.