1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * linux/fs/read_write.c 4 * 5 * Copyright (C) 1991, 1992 Linus Torvalds 6 */ 7 8 #include <linux/slab.h> 9 #include <linux/stat.h> 10 #include <linux/sched/xacct.h> 11 #include <linux/fcntl.h> 12 #include <linux/file.h> 13 #include <linux/uio.h> 14 #include <linux/fsnotify.h> 15 #include <linux/security.h> 16 #include <linux/export.h> 17 #include <linux/syscalls.h> 18 #include <linux/pagemap.h> 19 #include <linux/splice.h> 20 #include <linux/compat.h> 21 #include <linux/mount.h> 22 #include <linux/fs.h> 23 #include <linux/filelock.h> 24 #include "internal.h" 25 26 #include <linux/uaccess.h> 27 #include <asm/unistd.h> 28 29 const struct file_operations generic_ro_fops = { 30 .llseek = generic_file_llseek, 31 .read_iter = generic_file_read_iter, 32 .mmap_prepare = generic_file_readonly_mmap_prepare, 33 .splice_read = filemap_splice_read, 34 .setlease = generic_setlease, 35 }; 36 37 EXPORT_SYMBOL(generic_ro_fops); 38 39 static inline bool unsigned_offsets(struct file *file) 40 { 41 return file->f_op->fop_flags & FOP_UNSIGNED_OFFSET; 42 } 43 44 /** 45 * vfs_setpos_cookie - update the file offset for lseek and reset cookie 46 * @file: file structure in question 47 * @offset: file offset to seek to 48 * @maxsize: maximum file size 49 * @cookie: cookie to reset 50 * 51 * Update the file offset to the value specified by @offset if the given 52 * offset is valid and it is not equal to the current file offset and 53 * reset the specified cookie to indicate that a seek happened. 54 * 55 * Return the specified offset on success and -EINVAL on invalid offset. 56 */ 57 static loff_t vfs_setpos_cookie(struct file *file, loff_t offset, 58 loff_t maxsize, u64 *cookie) 59 { 60 if (offset < 0 && !unsigned_offsets(file)) 61 return -EINVAL; 62 if (offset > maxsize) 63 return -EINVAL; 64 65 if (offset != file->f_pos) { 66 file->f_pos = offset; 67 if (cookie) 68 *cookie = 0; 69 } 70 return offset; 71 } 72 73 /** 74 * vfs_setpos - update the file offset for lseek 75 * @file: file structure in question 76 * @offset: file offset to seek to 77 * @maxsize: maximum file size 78 * 79 * This is a low-level filesystem helper for updating the file offset to 80 * the value specified by @offset if the given offset is valid and it is 81 * not equal to the current file offset. 82 * 83 * Return the specified offset on success and -EINVAL on invalid offset. 84 */ 85 loff_t vfs_setpos(struct file *file, loff_t offset, loff_t maxsize) 86 { 87 return vfs_setpos_cookie(file, offset, maxsize, NULL); 88 } 89 EXPORT_SYMBOL(vfs_setpos); 90 91 /** 92 * must_set_pos - check whether f_pos has to be updated 93 * @file: file to seek on 94 * @offset: offset to use 95 * @whence: type of seek operation 96 * @eof: end of file 97 * 98 * Check whether f_pos needs to be updated and update @offset according 99 * to @whence. 100 * 101 * Return: 0 if f_pos doesn't need to be updated, 1 if f_pos has to be 102 * updated, and negative error code on failure. 103 */ 104 static int must_set_pos(struct file *file, loff_t *offset, int whence, loff_t eof) 105 { 106 switch (whence) { 107 case SEEK_END: 108 *offset += eof; 109 break; 110 case SEEK_CUR: 111 /* 112 * Here we special-case the lseek(fd, 0, SEEK_CUR) 113 * position-querying operation. Avoid rewriting the "same" 114 * f_pos value back to the file because a concurrent read(), 115 * write() or lseek() might have altered it 116 */ 117 if (*offset == 0) { 118 *offset = file->f_pos; 119 return 0; 120 } 121 break; 122 case SEEK_DATA: 123 /* 124 * In the generic case the entire file is data, so as long as 125 * offset isn't at the end of the file then the offset is data. 126 */ 127 if ((unsigned long long)*offset >= eof) 128 return -ENXIO; 129 break; 130 case SEEK_HOLE: 131 /* 132 * There is a virtual hole at the end of the file, so as long as 133 * offset isn't i_size or larger, return i_size. 134 */ 135 if ((unsigned long long)*offset >= eof) 136 return -ENXIO; 137 *offset = eof; 138 break; 139 } 140 141 return 1; 142 } 143 144 /** 145 * generic_file_llseek_size - generic llseek implementation for regular files 146 * @file: file structure to seek on 147 * @offset: file offset to seek to 148 * @whence: type of seek 149 * @maxsize: max size of this file in file system 150 * @eof: offset used for SEEK_END position 151 * 152 * This is a variant of generic_file_llseek that allows passing in a custom 153 * maximum file size and a custom EOF position, for e.g. hashed directories 154 * 155 * Synchronization: 156 * SEEK_SET and SEEK_END are unsynchronized (but atomic on 64bit platforms) 157 * SEEK_CUR is synchronized against other SEEK_CURs, but not read/writes. 158 * read/writes behave like SEEK_SET against seeks. 159 */ 160 loff_t 161 generic_file_llseek_size(struct file *file, loff_t offset, int whence, 162 loff_t maxsize, loff_t eof) 163 { 164 int ret; 165 166 ret = must_set_pos(file, &offset, whence, eof); 167 if (ret < 0) 168 return ret; 169 if (ret == 0) 170 return offset; 171 172 if (whence == SEEK_CUR) { 173 /* 174 * If the file requires locking via f_pos_lock we know 175 * that mutual exclusion for SEEK_CUR on the same file 176 * is guaranteed. If the file isn't locked, we take 177 * f_lock to protect against f_pos races with other 178 * SEEK_CURs. 179 */ 180 if (file_seek_cur_needs_f_lock(file)) { 181 guard(spinlock)(&file->f_lock); 182 return vfs_setpos(file, file->f_pos + offset, maxsize); 183 } 184 return vfs_setpos(file, file->f_pos + offset, maxsize); 185 } 186 187 return vfs_setpos(file, offset, maxsize); 188 } 189 EXPORT_SYMBOL(generic_file_llseek_size); 190 191 /** 192 * generic_llseek_cookie - versioned llseek implementation 193 * @file: file structure to seek on 194 * @offset: file offset to seek to 195 * @whence: type of seek 196 * @cookie: cookie to update 197 * 198 * See generic_file_llseek for a general description and locking assumptions. 199 * 200 * In contrast to generic_file_llseek, this function also resets a 201 * specified cookie to indicate a seek took place. 202 */ 203 loff_t generic_llseek_cookie(struct file *file, loff_t offset, int whence, 204 u64 *cookie) 205 { 206 struct inode *inode = file->f_mapping->host; 207 loff_t maxsize = inode->i_sb->s_maxbytes; 208 loff_t eof = i_size_read(inode); 209 int ret; 210 211 if (WARN_ON_ONCE(!cookie)) 212 return -EINVAL; 213 214 /* 215 * Require that this is only used for directories that guarantee 216 * synchronization between readdir and seek so that an update to 217 * @cookie is correctly synchronized with concurrent readdir. 218 */ 219 if (WARN_ON_ONCE(!(file->f_mode & FMODE_ATOMIC_POS))) 220 return -EINVAL; 221 222 ret = must_set_pos(file, &offset, whence, eof); 223 if (ret < 0) 224 return ret; 225 if (ret == 0) 226 return offset; 227 228 /* No need to hold f_lock because we know that f_pos_lock is held. */ 229 if (whence == SEEK_CUR) 230 return vfs_setpos_cookie(file, file->f_pos + offset, maxsize, cookie); 231 232 return vfs_setpos_cookie(file, offset, maxsize, cookie); 233 } 234 EXPORT_SYMBOL(generic_llseek_cookie); 235 236 /** 237 * generic_file_llseek - generic llseek implementation for regular files 238 * @file: file structure to seek on 239 * @offset: file offset to seek to 240 * @whence: type of seek 241 * 242 * This is a generic implementation of ->llseek useable for all normal local 243 * filesystems. It just updates the file offset to the value specified by 244 * @offset and @whence. 245 */ 246 loff_t generic_file_llseek(struct file *file, loff_t offset, int whence) 247 { 248 struct inode *inode = file->f_mapping->host; 249 250 return generic_file_llseek_size(file, offset, whence, 251 inode->i_sb->s_maxbytes, 252 i_size_read(inode)); 253 } 254 EXPORT_SYMBOL(generic_file_llseek); 255 256 /** 257 * fixed_size_llseek - llseek implementation for fixed-sized devices 258 * @file: file structure to seek on 259 * @offset: file offset to seek to 260 * @whence: type of seek 261 * @size: size of the file 262 * 263 */ 264 loff_t fixed_size_llseek(struct file *file, loff_t offset, int whence, loff_t size) 265 { 266 switch (whence) { 267 case SEEK_SET: case SEEK_CUR: case SEEK_END: 268 return generic_file_llseek_size(file, offset, whence, 269 size, size); 270 default: 271 return -EINVAL; 272 } 273 } 274 EXPORT_SYMBOL(fixed_size_llseek); 275 276 /** 277 * no_seek_end_llseek - llseek implementation for files without SEEK_END 278 * @file: file structure to seek on 279 * @offset: file offset to seek to 280 * @whence: type of seek 281 * 282 */ 283 loff_t no_seek_end_llseek(struct file *file, loff_t offset, int whence) 284 { 285 switch (whence) { 286 case SEEK_SET: case SEEK_CUR: 287 return generic_file_llseek_size(file, offset, whence, 288 OFFSET_MAX, 0); 289 default: 290 return -EINVAL; 291 } 292 } 293 EXPORT_SYMBOL(no_seek_end_llseek); 294 295 /** 296 * no_seek_end_llseek_size - llseek implementation for files without SEEK_END 297 * @file: file structure to seek on 298 * @offset: file offset to seek to 299 * @whence: type of seek 300 * @size: maximal offset allowed 301 * 302 */ 303 loff_t no_seek_end_llseek_size(struct file *file, loff_t offset, int whence, loff_t size) 304 { 305 switch (whence) { 306 case SEEK_SET: case SEEK_CUR: 307 return generic_file_llseek_size(file, offset, whence, 308 size, 0); 309 default: 310 return -EINVAL; 311 } 312 } 313 EXPORT_SYMBOL(no_seek_end_llseek_size); 314 315 /** 316 * noop_llseek - No Operation Performed llseek implementation 317 * @file: file structure to seek on 318 * @offset: file offset to seek to 319 * @whence: type of seek 320 * 321 * This is an implementation of ->llseek useable for the rare special case when 322 * userspace expects the seek to succeed but the (device) file is actually not 323 * able to perform the seek. In this case you use noop_llseek() instead of 324 * falling back to the default implementation of ->llseek. 325 */ 326 loff_t noop_llseek(struct file *file, loff_t offset, int whence) 327 { 328 return file->f_pos; 329 } 330 EXPORT_SYMBOL(noop_llseek); 331 332 loff_t default_llseek(struct file *file, loff_t offset, int whence) 333 { 334 struct inode *inode = file_inode(file); 335 loff_t retval; 336 int ret; 337 338 retval = inode_lock_killable(inode); 339 if (retval) 340 return retval; 341 342 ret = must_set_pos(file, &offset, whence, i_size_read(inode)); 343 if (ret < 0) { 344 retval = ret; 345 goto out; 346 } 347 if (ret == 0) { 348 retval = offset; 349 goto out; 350 } 351 352 if (whence == SEEK_CUR) 353 offset += file->f_pos; 354 355 retval = -EINVAL; 356 if (offset >= 0 || unsigned_offsets(file)) { 357 if (offset != file->f_pos) 358 file->f_pos = offset; 359 retval = offset; 360 } 361 out: 362 inode_unlock(inode); 363 return retval; 364 } 365 EXPORT_SYMBOL(default_llseek); 366 367 loff_t vfs_llseek(struct file *file, loff_t offset, int whence) 368 { 369 if (!(file->f_mode & FMODE_LSEEK)) 370 return -ESPIPE; 371 return file->f_op->llseek(file, offset, whence); 372 } 373 EXPORT_SYMBOL(vfs_llseek); 374 375 static off_t ksys_lseek(unsigned int fd, off_t offset, unsigned int whence) 376 { 377 off_t retval; 378 CLASS(fd_pos, f)(fd); 379 if (fd_empty(f)) 380 return -EBADF; 381 382 retval = -EINVAL; 383 if (whence <= SEEK_MAX) { 384 loff_t res = vfs_llseek(fd_file(f), offset, whence); 385 retval = res; 386 if (res != (loff_t)retval) 387 retval = -EOVERFLOW; /* LFS: should only happen on 32 bit platforms */ 388 } 389 return retval; 390 } 391 392 SYSCALL_DEFINE3(lseek, unsigned int, fd, off_t, offset, unsigned int, whence) 393 { 394 return ksys_lseek(fd, offset, whence); 395 } 396 397 #ifdef CONFIG_COMPAT 398 COMPAT_SYSCALL_DEFINE3(lseek, unsigned int, fd, compat_off_t, offset, unsigned int, whence) 399 { 400 return ksys_lseek(fd, offset, whence); 401 } 402 #endif 403 404 #if !defined(CONFIG_64BIT) || defined(CONFIG_COMPAT) || \ 405 defined(__ARCH_WANT_SYS_LLSEEK) 406 SYSCALL_DEFINE5(llseek, unsigned int, fd, unsigned long, offset_high, 407 unsigned long, offset_low, loff_t __user *, result, 408 unsigned int, whence) 409 { 410 int retval; 411 CLASS(fd_pos, f)(fd); 412 loff_t offset; 413 414 if (fd_empty(f)) 415 return -EBADF; 416 417 if (whence > SEEK_MAX) 418 return -EINVAL; 419 420 offset = vfs_llseek(fd_file(f), ((loff_t) offset_high << 32) | offset_low, 421 whence); 422 423 retval = (int)offset; 424 if (offset >= 0 || (unsigned_offsets(fd_file(f)) && offset < -MAX_ERRNO)) { 425 retval = -EFAULT; 426 if (!copy_to_user(result, &offset, sizeof(offset))) 427 retval = 0; 428 } 429 return retval; 430 } 431 #endif 432 433 int rw_verify_area(int read_write, struct file *file, const loff_t *ppos, size_t count) 434 { 435 int mask = read_write == READ ? MAY_READ : MAY_WRITE; 436 int ret; 437 438 if (unlikely((ssize_t) count < 0)) 439 return -EINVAL; 440 441 if (ppos) { 442 loff_t pos = *ppos; 443 444 if (unlikely(pos < 0)) { 445 if (!unsigned_offsets(file)) 446 return -EINVAL; 447 if (count >= -pos) /* both values are in 0..LLONG_MAX */ 448 return -EOVERFLOW; 449 } else if (unlikely((loff_t) (pos + count) < 0)) { 450 if (!unsigned_offsets(file)) 451 return -EINVAL; 452 } 453 } 454 455 ret = security_file_permission(file, mask); 456 if (ret) 457 return ret; 458 459 return fsnotify_file_area_perm(file, mask, ppos, count); 460 } 461 EXPORT_SYMBOL(rw_verify_area); 462 463 static ssize_t new_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos) 464 { 465 struct kiocb kiocb; 466 struct iov_iter iter; 467 ssize_t ret; 468 469 init_sync_kiocb(&kiocb, filp); 470 kiocb.ki_pos = (ppos ? *ppos : 0); 471 iov_iter_ubuf(&iter, ITER_DEST, buf, len); 472 473 ret = filp->f_op->read_iter(&kiocb, &iter); 474 BUG_ON(ret == -EIOCBQUEUED); 475 if (ppos) 476 *ppos = kiocb.ki_pos; 477 return ret; 478 } 479 480 static int warn_unsupported(struct file *file, const char *op) 481 { 482 pr_warn_ratelimited( 483 "kernel %s not supported for file %pD4 (pid: %d comm: %.20s)\n", 484 op, file, current->pid, current->comm); 485 return -EINVAL; 486 } 487 488 ssize_t __kernel_read(struct file *file, void *buf, size_t count, loff_t *pos) 489 { 490 struct kvec iov = { 491 .iov_base = buf, 492 .iov_len = min_t(size_t, count, MAX_RW_COUNT), 493 }; 494 struct kiocb kiocb; 495 struct iov_iter iter; 496 ssize_t ret; 497 498 if (WARN_ON_ONCE(!(file->f_mode & FMODE_READ))) 499 return -EINVAL; 500 if (!(file->f_mode & FMODE_CAN_READ)) 501 return -EINVAL; 502 /* 503 * Also fail if ->read_iter and ->read are both wired up as that 504 * implies very convoluted semantics. 505 */ 506 if (unlikely(!file->f_op->read_iter || file->f_op->read)) 507 return warn_unsupported(file, "read"); 508 509 init_sync_kiocb(&kiocb, file); 510 kiocb.ki_pos = pos ? *pos : 0; 511 iov_iter_kvec(&iter, ITER_DEST, &iov, 1, iov.iov_len); 512 ret = file->f_op->read_iter(&kiocb, &iter); 513 if (ret > 0) { 514 if (pos) 515 *pos = kiocb.ki_pos; 516 fsnotify_access(file); 517 add_rchar(current, ret); 518 } 519 inc_syscr(current); 520 return ret; 521 } 522 523 ssize_t kernel_read(struct file *file, void *buf, size_t count, loff_t *pos) 524 { 525 ssize_t ret; 526 527 ret = rw_verify_area(READ, file, pos, count); 528 if (ret) 529 return ret; 530 return __kernel_read(file, buf, count, pos); 531 } 532 EXPORT_SYMBOL(kernel_read); 533 534 ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos) 535 { 536 ssize_t ret; 537 538 if (!(file->f_mode & FMODE_READ)) 539 return -EBADF; 540 if (!(file->f_mode & FMODE_CAN_READ)) 541 return -EINVAL; 542 if (unlikely(!access_ok(buf, count))) 543 return -EFAULT; 544 545 ret = rw_verify_area(READ, file, pos, count); 546 if (ret) 547 return ret; 548 if (count > MAX_RW_COUNT) 549 count = MAX_RW_COUNT; 550 551 if (file->f_op->read) 552 ret = file->f_op->read(file, buf, count, pos); 553 else if (file->f_op->read_iter) 554 ret = new_sync_read(file, buf, count, pos); 555 else 556 ret = -EINVAL; 557 if (ret > 0) { 558 fsnotify_access(file); 559 add_rchar(current, ret); 560 } 561 inc_syscr(current); 562 return ret; 563 } 564 565 static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos) 566 { 567 struct kiocb kiocb; 568 struct iov_iter iter; 569 ssize_t ret; 570 571 init_sync_kiocb(&kiocb, filp); 572 kiocb.ki_pos = (ppos ? *ppos : 0); 573 iov_iter_ubuf(&iter, ITER_SOURCE, (void __user *)buf, len); 574 575 ret = filp->f_op->write_iter(&kiocb, &iter); 576 BUG_ON(ret == -EIOCBQUEUED); 577 if (ret > 0 && ppos) 578 *ppos = kiocb.ki_pos; 579 return ret; 580 } 581 582 /* caller is responsible for file_start_write/file_end_write */ 583 ssize_t __kernel_write_iter(struct file *file, struct iov_iter *from, loff_t *pos) 584 { 585 struct kiocb kiocb; 586 ssize_t ret; 587 588 if (WARN_ON_ONCE(!(file->f_mode & FMODE_WRITE))) 589 return -EBADF; 590 if (!(file->f_mode & FMODE_CAN_WRITE)) 591 return -EINVAL; 592 /* 593 * Also fail if ->write_iter and ->write are both wired up as that 594 * implies very convoluted semantics. 595 */ 596 if (unlikely(!file->f_op->write_iter || file->f_op->write)) 597 return warn_unsupported(file, "write"); 598 599 init_sync_kiocb(&kiocb, file); 600 kiocb.ki_pos = pos ? *pos : 0; 601 ret = file->f_op->write_iter(&kiocb, from); 602 if (ret > 0) { 603 if (pos) 604 *pos = kiocb.ki_pos; 605 fsnotify_modify(file); 606 add_wchar(current, ret); 607 } 608 inc_syscw(current); 609 return ret; 610 } 611 612 /* caller is responsible for file_start_write/file_end_write */ 613 ssize_t __kernel_write(struct file *file, const void *buf, size_t count, loff_t *pos) 614 { 615 struct kvec iov = { 616 .iov_base = (void *)buf, 617 .iov_len = min_t(size_t, count, MAX_RW_COUNT), 618 }; 619 struct iov_iter iter; 620 iov_iter_kvec(&iter, ITER_SOURCE, &iov, 1, iov.iov_len); 621 return __kernel_write_iter(file, &iter, pos); 622 } 623 /* 624 * autofs is one of the few internal kernel users that actually 625 * wants this _and_ can be built as a module. So we need to export 626 * this symbol for autofs, even though it really isn't appropriate 627 * for any other kernel modules. 628 */ 629 EXPORT_SYMBOL_FOR_MODULES(__kernel_write, "autofs4"); 630 631 ssize_t kernel_write(struct file *file, const void *buf, size_t count, 632 loff_t *pos) 633 { 634 ssize_t ret; 635 636 ret = rw_verify_area(WRITE, file, pos, count); 637 if (ret) 638 return ret; 639 640 file_start_write(file); 641 ret = __kernel_write(file, buf, count, pos); 642 file_end_write(file); 643 return ret; 644 } 645 EXPORT_SYMBOL(kernel_write); 646 647 ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos) 648 { 649 ssize_t ret; 650 651 if (!(file->f_mode & FMODE_WRITE)) 652 return -EBADF; 653 if (!(file->f_mode & FMODE_CAN_WRITE)) 654 return -EINVAL; 655 if (unlikely(!access_ok(buf, count))) 656 return -EFAULT; 657 658 ret = rw_verify_area(WRITE, file, pos, count); 659 if (ret) 660 return ret; 661 if (count > MAX_RW_COUNT) 662 count = MAX_RW_COUNT; 663 file_start_write(file); 664 if (file->f_op->write) 665 ret = file->f_op->write(file, buf, count, pos); 666 else if (file->f_op->write_iter) 667 ret = new_sync_write(file, buf, count, pos); 668 else 669 ret = -EINVAL; 670 if (ret > 0) { 671 fsnotify_modify(file); 672 add_wchar(current, ret); 673 } 674 inc_syscw(current); 675 file_end_write(file); 676 return ret; 677 } 678 679 /* file_ppos returns &file->f_pos or NULL if file is stream */ 680 static inline loff_t *file_ppos(struct file *file) 681 { 682 return file->f_mode & FMODE_STREAM ? NULL : &file->f_pos; 683 } 684 685 ssize_t ksys_read(unsigned int fd, char __user *buf, size_t count) 686 { 687 CLASS(fd_pos, f)(fd); 688 ssize_t ret = -EBADF; 689 690 if (!fd_empty(f)) { 691 loff_t pos, *ppos = file_ppos(fd_file(f)); 692 if (ppos) { 693 pos = *ppos; 694 ppos = &pos; 695 } 696 ret = vfs_read(fd_file(f), buf, count, ppos); 697 if (ret >= 0 && ppos) 698 fd_file(f)->f_pos = pos; 699 } 700 return ret; 701 } 702 703 SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count) 704 { 705 return ksys_read(fd, buf, count); 706 } 707 708 ssize_t ksys_write(unsigned int fd, const char __user *buf, size_t count) 709 { 710 CLASS(fd_pos, f)(fd); 711 ssize_t ret = -EBADF; 712 713 if (!fd_empty(f)) { 714 loff_t pos, *ppos = file_ppos(fd_file(f)); 715 if (ppos) { 716 pos = *ppos; 717 ppos = &pos; 718 } 719 ret = vfs_write(fd_file(f), buf, count, ppos); 720 if (ret >= 0 && ppos) 721 fd_file(f)->f_pos = pos; 722 } 723 724 return ret; 725 } 726 727 SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf, 728 size_t, count) 729 { 730 return ksys_write(fd, buf, count); 731 } 732 733 ssize_t ksys_pread64(unsigned int fd, char __user *buf, size_t count, 734 loff_t pos) 735 { 736 CLASS(fd, f)(fd); 737 if (fd_empty(f)) 738 return -EBADF; 739 740 if (pos < 0 && !unsigned_offsets(fd_file(f))) 741 return -EINVAL; 742 743 if (fd_file(f)->f_mode & FMODE_PREAD) 744 return vfs_read(fd_file(f), buf, count, &pos); 745 746 return -ESPIPE; 747 } 748 749 SYSCALL_DEFINE4(pread64, unsigned int, fd, char __user *, buf, 750 size_t, count, loff_t, pos) 751 { 752 return ksys_pread64(fd, buf, count, pos); 753 } 754 755 #if defined(CONFIG_COMPAT) && defined(__ARCH_WANT_COMPAT_PREAD64) 756 COMPAT_SYSCALL_DEFINE5(pread64, unsigned int, fd, char __user *, buf, 757 size_t, count, compat_arg_u64_dual(pos)) 758 { 759 return ksys_pread64(fd, buf, count, compat_arg_u64_glue(pos)); 760 } 761 #endif 762 763 ssize_t ksys_pwrite64(unsigned int fd, const char __user *buf, 764 size_t count, loff_t pos) 765 { 766 CLASS(fd, f)(fd); 767 if (fd_empty(f)) 768 return -EBADF; 769 770 if (pos < 0 && !unsigned_offsets(fd_file(f))) 771 return -EINVAL; 772 773 if (fd_file(f)->f_mode & FMODE_PWRITE) 774 return vfs_write(fd_file(f), buf, count, &pos); 775 776 return -ESPIPE; 777 } 778 779 SYSCALL_DEFINE4(pwrite64, unsigned int, fd, const char __user *, buf, 780 size_t, count, loff_t, pos) 781 { 782 return ksys_pwrite64(fd, buf, count, pos); 783 } 784 785 #if defined(CONFIG_COMPAT) && defined(__ARCH_WANT_COMPAT_PWRITE64) 786 COMPAT_SYSCALL_DEFINE5(pwrite64, unsigned int, fd, const char __user *, buf, 787 size_t, count, compat_arg_u64_dual(pos)) 788 { 789 return ksys_pwrite64(fd, buf, count, compat_arg_u64_glue(pos)); 790 } 791 #endif 792 793 static ssize_t do_iter_readv_writev(struct file *filp, struct iov_iter *iter, 794 loff_t *ppos, int type, rwf_t flags) 795 { 796 struct kiocb kiocb; 797 ssize_t ret; 798 799 init_sync_kiocb(&kiocb, filp); 800 ret = kiocb_set_rw_flags(&kiocb, flags, type); 801 if (ret) 802 return ret; 803 kiocb.ki_pos = (ppos ? *ppos : 0); 804 805 if (type == READ) 806 ret = filp->f_op->read_iter(&kiocb, iter); 807 else 808 ret = filp->f_op->write_iter(&kiocb, iter); 809 BUG_ON(ret == -EIOCBQUEUED); 810 if (ppos) 811 *ppos = kiocb.ki_pos; 812 return ret; 813 } 814 815 /* Do it by hand, with file-ops */ 816 static ssize_t do_loop_readv_writev(struct file *filp, struct iov_iter *iter, 817 loff_t *ppos, int type, rwf_t flags) 818 { 819 ssize_t ret = 0; 820 821 if (flags & ~RWF_HIPRI) 822 return -EOPNOTSUPP; 823 824 while (iov_iter_count(iter)) { 825 ssize_t nr; 826 827 if (type == READ) { 828 nr = filp->f_op->read(filp, iter_iov_addr(iter), 829 iter_iov_len(iter), ppos); 830 } else { 831 nr = filp->f_op->write(filp, iter_iov_addr(iter), 832 iter_iov_len(iter), ppos); 833 } 834 835 if (nr < 0) { 836 if (!ret) 837 ret = nr; 838 break; 839 } 840 ret += nr; 841 if (nr != iter_iov_len(iter)) 842 break; 843 iov_iter_advance(iter, nr); 844 } 845 846 return ret; 847 } 848 849 ssize_t vfs_iocb_iter_read(struct file *file, struct kiocb *iocb, 850 struct iov_iter *iter) 851 { 852 size_t tot_len; 853 ssize_t ret = 0; 854 855 if (!file->f_op->read_iter) 856 return -EINVAL; 857 if (!(file->f_mode & FMODE_READ)) 858 return -EBADF; 859 if (!(file->f_mode & FMODE_CAN_READ)) 860 return -EINVAL; 861 862 tot_len = iov_iter_count(iter); 863 if (!tot_len) 864 goto out; 865 ret = rw_verify_area(READ, file, &iocb->ki_pos, tot_len); 866 if (ret < 0) 867 return ret; 868 869 ret = file->f_op->read_iter(iocb, iter); 870 out: 871 if (ret >= 0) 872 fsnotify_access(file); 873 return ret; 874 } 875 EXPORT_SYMBOL(vfs_iocb_iter_read); 876 877 ssize_t vfs_iter_read(struct file *file, struct iov_iter *iter, loff_t *ppos, 878 rwf_t flags) 879 { 880 size_t tot_len; 881 ssize_t ret = 0; 882 883 if (!file->f_op->read_iter) 884 return -EINVAL; 885 if (!(file->f_mode & FMODE_READ)) 886 return -EBADF; 887 if (!(file->f_mode & FMODE_CAN_READ)) 888 return -EINVAL; 889 890 tot_len = iov_iter_count(iter); 891 if (!tot_len) 892 goto out; 893 ret = rw_verify_area(READ, file, ppos, tot_len); 894 if (ret < 0) 895 return ret; 896 897 ret = do_iter_readv_writev(file, iter, ppos, READ, flags); 898 out: 899 if (ret >= 0) 900 fsnotify_access(file); 901 return ret; 902 } 903 EXPORT_SYMBOL(vfs_iter_read); 904 905 /* 906 * Caller is responsible for calling kiocb_end_write() on completion 907 * if async iocb was queued. 908 */ 909 ssize_t vfs_iocb_iter_write(struct file *file, struct kiocb *iocb, 910 struct iov_iter *iter) 911 { 912 size_t tot_len; 913 ssize_t ret = 0; 914 915 if (!file->f_op->write_iter) 916 return -EINVAL; 917 if (!(file->f_mode & FMODE_WRITE)) 918 return -EBADF; 919 if (!(file->f_mode & FMODE_CAN_WRITE)) 920 return -EINVAL; 921 922 tot_len = iov_iter_count(iter); 923 if (!tot_len) 924 return 0; 925 ret = rw_verify_area(WRITE, file, &iocb->ki_pos, tot_len); 926 if (ret < 0) 927 return ret; 928 929 kiocb_start_write(iocb); 930 ret = file->f_op->write_iter(iocb, iter); 931 if (ret != -EIOCBQUEUED) 932 kiocb_end_write(iocb); 933 if (ret > 0) 934 fsnotify_modify(file); 935 936 return ret; 937 } 938 EXPORT_SYMBOL(vfs_iocb_iter_write); 939 940 ssize_t vfs_iter_write(struct file *file, struct iov_iter *iter, loff_t *ppos, 941 rwf_t flags) 942 { 943 size_t tot_len; 944 ssize_t ret; 945 946 if (!(file->f_mode & FMODE_WRITE)) 947 return -EBADF; 948 if (!(file->f_mode & FMODE_CAN_WRITE)) 949 return -EINVAL; 950 if (!file->f_op->write_iter) 951 return -EINVAL; 952 953 tot_len = iov_iter_count(iter); 954 if (!tot_len) 955 return 0; 956 957 ret = rw_verify_area(WRITE, file, ppos, tot_len); 958 if (ret < 0) 959 return ret; 960 961 file_start_write(file); 962 ret = do_iter_readv_writev(file, iter, ppos, WRITE, flags); 963 if (ret > 0) 964 fsnotify_modify(file); 965 file_end_write(file); 966 967 return ret; 968 } 969 EXPORT_SYMBOL(vfs_iter_write); 970 971 static ssize_t vfs_readv(struct file *file, const struct iovec __user *vec, 972 unsigned long vlen, loff_t *pos, rwf_t flags) 973 { 974 struct iovec iovstack[UIO_FASTIOV]; 975 struct iovec *iov = iovstack; 976 struct iov_iter iter; 977 size_t tot_len; 978 ssize_t ret = 0; 979 980 if (!(file->f_mode & FMODE_READ)) 981 return -EBADF; 982 if (!(file->f_mode & FMODE_CAN_READ)) 983 return -EINVAL; 984 985 ret = import_iovec(ITER_DEST, vec, vlen, ARRAY_SIZE(iovstack), &iov, 986 &iter); 987 if (ret < 0) 988 return ret; 989 990 tot_len = iov_iter_count(&iter); 991 if (!tot_len) 992 goto out; 993 994 ret = rw_verify_area(READ, file, pos, tot_len); 995 if (ret < 0) 996 goto out; 997 998 if (file->f_op->read_iter) 999 ret = do_iter_readv_writev(file, &iter, pos, READ, flags); 1000 else 1001 ret = do_loop_readv_writev(file, &iter, pos, READ, flags); 1002 out: 1003 if (ret >= 0) 1004 fsnotify_access(file); 1005 kfree(iov); 1006 return ret; 1007 } 1008 1009 static ssize_t vfs_writev(struct file *file, const struct iovec __user *vec, 1010 unsigned long vlen, loff_t *pos, rwf_t flags) 1011 { 1012 struct iovec iovstack[UIO_FASTIOV]; 1013 struct iovec *iov = iovstack; 1014 struct iov_iter iter; 1015 size_t tot_len; 1016 ssize_t ret = 0; 1017 1018 if (!(file->f_mode & FMODE_WRITE)) 1019 return -EBADF; 1020 if (!(file->f_mode & FMODE_CAN_WRITE)) 1021 return -EINVAL; 1022 1023 ret = import_iovec(ITER_SOURCE, vec, vlen, ARRAY_SIZE(iovstack), &iov, 1024 &iter); 1025 if (ret < 0) 1026 return ret; 1027 1028 tot_len = iov_iter_count(&iter); 1029 if (!tot_len) 1030 goto out; 1031 1032 ret = rw_verify_area(WRITE, file, pos, tot_len); 1033 if (ret < 0) 1034 goto out; 1035 1036 file_start_write(file); 1037 if (file->f_op->write_iter) 1038 ret = do_iter_readv_writev(file, &iter, pos, WRITE, flags); 1039 else 1040 ret = do_loop_readv_writev(file, &iter, pos, WRITE, flags); 1041 if (ret > 0) 1042 fsnotify_modify(file); 1043 file_end_write(file); 1044 out: 1045 kfree(iov); 1046 return ret; 1047 } 1048 1049 static ssize_t do_readv(unsigned long fd, const struct iovec __user *vec, 1050 unsigned long vlen, rwf_t flags) 1051 { 1052 CLASS(fd_pos, f)(fd); 1053 ssize_t ret = -EBADF; 1054 1055 if (!fd_empty(f)) { 1056 loff_t pos, *ppos = file_ppos(fd_file(f)); 1057 if (ppos) { 1058 pos = *ppos; 1059 ppos = &pos; 1060 } 1061 ret = vfs_readv(fd_file(f), vec, vlen, ppos, flags); 1062 if (ret >= 0 && ppos) 1063 fd_file(f)->f_pos = pos; 1064 } 1065 1066 if (ret > 0) 1067 add_rchar(current, ret); 1068 inc_syscr(current); 1069 return ret; 1070 } 1071 1072 static ssize_t do_writev(unsigned long fd, const struct iovec __user *vec, 1073 unsigned long vlen, rwf_t flags) 1074 { 1075 CLASS(fd_pos, f)(fd); 1076 ssize_t ret = -EBADF; 1077 1078 if (!fd_empty(f)) { 1079 loff_t pos, *ppos = file_ppos(fd_file(f)); 1080 if (ppos) { 1081 pos = *ppos; 1082 ppos = &pos; 1083 } 1084 ret = vfs_writev(fd_file(f), vec, vlen, ppos, flags); 1085 if (ret >= 0 && ppos) 1086 fd_file(f)->f_pos = pos; 1087 } 1088 1089 if (ret > 0) 1090 add_wchar(current, ret); 1091 inc_syscw(current); 1092 return ret; 1093 } 1094 1095 static inline loff_t pos_from_hilo(unsigned long high, unsigned long low) 1096 { 1097 #define HALF_LONG_BITS (BITS_PER_LONG / 2) 1098 return (((loff_t)high << HALF_LONG_BITS) << HALF_LONG_BITS) | low; 1099 } 1100 1101 static ssize_t do_preadv(unsigned long fd, const struct iovec __user *vec, 1102 unsigned long vlen, loff_t pos, rwf_t flags) 1103 { 1104 ssize_t ret = -EBADF; 1105 1106 CLASS(fd, f)(fd); 1107 if (!fd_empty(f)) { 1108 ret = -EINVAL; 1109 if (pos >= 0 || unsigned_offsets(fd_file(f))) { 1110 ret = -ESPIPE; 1111 if (fd_file(f)->f_mode & FMODE_PREAD) 1112 ret = vfs_readv(fd_file(f), vec, vlen, &pos, flags); 1113 } 1114 } 1115 1116 if (ret > 0) 1117 add_rchar(current, ret); 1118 inc_syscr(current); 1119 return ret; 1120 } 1121 1122 static ssize_t do_pwritev(unsigned long fd, const struct iovec __user *vec, 1123 unsigned long vlen, loff_t pos, rwf_t flags) 1124 { 1125 ssize_t ret = -EBADF; 1126 1127 CLASS(fd, f)(fd); 1128 if (!fd_empty(f)) { 1129 ret = -EINVAL; 1130 if (pos >= 0 || unsigned_offsets(fd_file(f))) { 1131 ret = -ESPIPE; 1132 if (fd_file(f)->f_mode & FMODE_PWRITE) 1133 ret = vfs_writev(fd_file(f), vec, vlen, &pos, flags); 1134 } 1135 } 1136 1137 if (ret > 0) 1138 add_wchar(current, ret); 1139 inc_syscw(current); 1140 return ret; 1141 } 1142 1143 SYSCALL_DEFINE3(readv, unsigned long, fd, const struct iovec __user *, vec, 1144 unsigned long, vlen) 1145 { 1146 return do_readv(fd, vec, vlen, 0); 1147 } 1148 1149 SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec __user *, vec, 1150 unsigned long, vlen) 1151 { 1152 return do_writev(fd, vec, vlen, 0); 1153 } 1154 1155 SYSCALL_DEFINE5(preadv, unsigned long, fd, const struct iovec __user *, vec, 1156 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h) 1157 { 1158 loff_t pos = pos_from_hilo(pos_h, pos_l); 1159 1160 return do_preadv(fd, vec, vlen, pos, 0); 1161 } 1162 1163 SYSCALL_DEFINE6(preadv2, unsigned long, fd, const struct iovec __user *, vec, 1164 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h, 1165 rwf_t, flags) 1166 { 1167 loff_t pos = pos_from_hilo(pos_h, pos_l); 1168 1169 if (pos == -1) 1170 return do_readv(fd, vec, vlen, flags); 1171 1172 return do_preadv(fd, vec, vlen, pos, flags); 1173 } 1174 1175 SYSCALL_DEFINE5(pwritev, unsigned long, fd, const struct iovec __user *, vec, 1176 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h) 1177 { 1178 loff_t pos = pos_from_hilo(pos_h, pos_l); 1179 1180 return do_pwritev(fd, vec, vlen, pos, 0); 1181 } 1182 1183 SYSCALL_DEFINE6(pwritev2, unsigned long, fd, const struct iovec __user *, vec, 1184 unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h, 1185 rwf_t, flags) 1186 { 1187 loff_t pos = pos_from_hilo(pos_h, pos_l); 1188 1189 if (pos == -1) 1190 return do_writev(fd, vec, vlen, flags); 1191 1192 return do_pwritev(fd, vec, vlen, pos, flags); 1193 } 1194 1195 /* 1196 * Various compat syscalls. Note that they all pretend to take a native 1197 * iovec - import_iovec will properly treat those as compat_iovecs based on 1198 * in_compat_syscall(). 1199 */ 1200 #ifdef CONFIG_COMPAT 1201 #ifdef __ARCH_WANT_COMPAT_SYS_PREADV64 1202 COMPAT_SYSCALL_DEFINE4(preadv64, unsigned long, fd, 1203 const struct iovec __user *, vec, 1204 unsigned long, vlen, loff_t, pos) 1205 { 1206 return do_preadv(fd, vec, vlen, pos, 0); 1207 } 1208 #endif 1209 1210 COMPAT_SYSCALL_DEFINE5(preadv, compat_ulong_t, fd, 1211 const struct iovec __user *, vec, 1212 compat_ulong_t, vlen, u32, pos_low, u32, pos_high) 1213 { 1214 loff_t pos = ((loff_t)pos_high << 32) | pos_low; 1215 1216 return do_preadv(fd, vec, vlen, pos, 0); 1217 } 1218 1219 #ifdef __ARCH_WANT_COMPAT_SYS_PREADV64V2 1220 COMPAT_SYSCALL_DEFINE5(preadv64v2, unsigned long, fd, 1221 const struct iovec __user *, vec, 1222 unsigned long, vlen, loff_t, pos, rwf_t, flags) 1223 { 1224 if (pos == -1) 1225 return do_readv(fd, vec, vlen, flags); 1226 return do_preadv(fd, vec, vlen, pos, flags); 1227 } 1228 #endif 1229 1230 COMPAT_SYSCALL_DEFINE6(preadv2, compat_ulong_t, fd, 1231 const struct iovec __user *, vec, 1232 compat_ulong_t, vlen, u32, pos_low, u32, pos_high, 1233 rwf_t, flags) 1234 { 1235 loff_t pos = ((loff_t)pos_high << 32) | pos_low; 1236 1237 if (pos == -1) 1238 return do_readv(fd, vec, vlen, flags); 1239 return do_preadv(fd, vec, vlen, pos, flags); 1240 } 1241 1242 #ifdef __ARCH_WANT_COMPAT_SYS_PWRITEV64 1243 COMPAT_SYSCALL_DEFINE4(pwritev64, unsigned long, fd, 1244 const struct iovec __user *, vec, 1245 unsigned long, vlen, loff_t, pos) 1246 { 1247 return do_pwritev(fd, vec, vlen, pos, 0); 1248 } 1249 #endif 1250 1251 COMPAT_SYSCALL_DEFINE5(pwritev, compat_ulong_t, fd, 1252 const struct iovec __user *,vec, 1253 compat_ulong_t, vlen, u32, pos_low, u32, pos_high) 1254 { 1255 loff_t pos = ((loff_t)pos_high << 32) | pos_low; 1256 1257 return do_pwritev(fd, vec, vlen, pos, 0); 1258 } 1259 1260 #ifdef __ARCH_WANT_COMPAT_SYS_PWRITEV64V2 1261 COMPAT_SYSCALL_DEFINE5(pwritev64v2, unsigned long, fd, 1262 const struct iovec __user *, vec, 1263 unsigned long, vlen, loff_t, pos, rwf_t, flags) 1264 { 1265 if (pos == -1) 1266 return do_writev(fd, vec, vlen, flags); 1267 return do_pwritev(fd, vec, vlen, pos, flags); 1268 } 1269 #endif 1270 1271 COMPAT_SYSCALL_DEFINE6(pwritev2, compat_ulong_t, fd, 1272 const struct iovec __user *,vec, 1273 compat_ulong_t, vlen, u32, pos_low, u32, pos_high, rwf_t, flags) 1274 { 1275 loff_t pos = ((loff_t)pos_high << 32) | pos_low; 1276 1277 if (pos == -1) 1278 return do_writev(fd, vec, vlen, flags); 1279 return do_pwritev(fd, vec, vlen, pos, flags); 1280 } 1281 #endif /* CONFIG_COMPAT */ 1282 1283 static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos, 1284 size_t count, loff_t max) 1285 { 1286 struct inode *in_inode, *out_inode; 1287 struct pipe_inode_info *opipe; 1288 loff_t pos; 1289 loff_t out_pos; 1290 ssize_t retval; 1291 int fl; 1292 1293 /* 1294 * Get input file, and verify that it is ok.. 1295 */ 1296 CLASS(fd, in)(in_fd); 1297 if (fd_empty(in)) 1298 return -EBADF; 1299 if (!(fd_file(in)->f_mode & FMODE_READ)) 1300 return -EBADF; 1301 if (!ppos) { 1302 pos = fd_file(in)->f_pos; 1303 } else { 1304 pos = *ppos; 1305 if (!(fd_file(in)->f_mode & FMODE_PREAD)) 1306 return -ESPIPE; 1307 } 1308 retval = rw_verify_area(READ, fd_file(in), &pos, count); 1309 if (retval < 0) 1310 return retval; 1311 if (count > MAX_RW_COUNT) 1312 count = MAX_RW_COUNT; 1313 1314 /* 1315 * Get output file, and verify that it is ok.. 1316 */ 1317 CLASS(fd, out)(out_fd); 1318 if (fd_empty(out)) 1319 return -EBADF; 1320 if (!(fd_file(out)->f_mode & FMODE_WRITE)) 1321 return -EBADF; 1322 in_inode = file_inode(fd_file(in)); 1323 out_inode = file_inode(fd_file(out)); 1324 out_pos = fd_file(out)->f_pos; 1325 1326 if (!max) 1327 max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes); 1328 1329 if (unlikely(pos + count > max)) { 1330 if (pos >= max) 1331 return -EOVERFLOW; 1332 count = max - pos; 1333 } 1334 1335 fl = 0; 1336 #if 0 1337 /* 1338 * We need to debate whether we can enable this or not. The 1339 * man page documents EAGAIN return for the output at least, 1340 * and the application is arguably buggy if it doesn't expect 1341 * EAGAIN on a non-blocking file descriptor. 1342 */ 1343 if (fd_file(in)->f_flags & O_NONBLOCK) 1344 fl = SPLICE_F_NONBLOCK; 1345 #endif 1346 opipe = get_pipe_info(fd_file(out), true); 1347 if (!opipe) { 1348 retval = rw_verify_area(WRITE, fd_file(out), &out_pos, count); 1349 if (retval < 0) 1350 return retval; 1351 retval = do_splice_direct(fd_file(in), &pos, fd_file(out), &out_pos, 1352 count, fl); 1353 } else { 1354 if (fd_file(out)->f_flags & O_NONBLOCK) 1355 fl |= SPLICE_F_NONBLOCK; 1356 1357 retval = splice_file_to_pipe(fd_file(in), opipe, &pos, count, fl); 1358 } 1359 1360 if (retval > 0) { 1361 add_rchar(current, retval); 1362 add_wchar(current, retval); 1363 fsnotify_access(fd_file(in)); 1364 fsnotify_modify(fd_file(out)); 1365 fd_file(out)->f_pos = out_pos; 1366 if (ppos) 1367 *ppos = pos; 1368 else 1369 fd_file(in)->f_pos = pos; 1370 } 1371 1372 inc_syscr(current); 1373 inc_syscw(current); 1374 if (pos > max) 1375 retval = -EOVERFLOW; 1376 return retval; 1377 } 1378 1379 SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd, off_t __user *, offset, size_t, count) 1380 { 1381 loff_t pos; 1382 off_t off; 1383 ssize_t ret; 1384 1385 if (offset) { 1386 if (unlikely(get_user(off, offset))) 1387 return -EFAULT; 1388 pos = off; 1389 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS); 1390 if (unlikely(put_user(pos, offset))) 1391 return -EFAULT; 1392 return ret; 1393 } 1394 1395 return do_sendfile(out_fd, in_fd, NULL, count, 0); 1396 } 1397 1398 SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd, loff_t __user *, offset, size_t, count) 1399 { 1400 loff_t pos; 1401 ssize_t ret; 1402 1403 if (offset) { 1404 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t)))) 1405 return -EFAULT; 1406 ret = do_sendfile(out_fd, in_fd, &pos, count, 0); 1407 if (unlikely(put_user(pos, offset))) 1408 return -EFAULT; 1409 return ret; 1410 } 1411 1412 return do_sendfile(out_fd, in_fd, NULL, count, 0); 1413 } 1414 1415 #ifdef CONFIG_COMPAT 1416 COMPAT_SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd, 1417 compat_off_t __user *, offset, compat_size_t, count) 1418 { 1419 loff_t pos; 1420 off_t off; 1421 ssize_t ret; 1422 1423 if (offset) { 1424 if (unlikely(get_user(off, offset))) 1425 return -EFAULT; 1426 pos = off; 1427 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS); 1428 if (unlikely(put_user(pos, offset))) 1429 return -EFAULT; 1430 return ret; 1431 } 1432 1433 return do_sendfile(out_fd, in_fd, NULL, count, 0); 1434 } 1435 1436 COMPAT_SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd, 1437 compat_loff_t __user *, offset, compat_size_t, count) 1438 { 1439 loff_t pos; 1440 ssize_t ret; 1441 1442 if (offset) { 1443 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t)))) 1444 return -EFAULT; 1445 ret = do_sendfile(out_fd, in_fd, &pos, count, 0); 1446 if (unlikely(put_user(pos, offset))) 1447 return -EFAULT; 1448 return ret; 1449 } 1450 1451 return do_sendfile(out_fd, in_fd, NULL, count, 0); 1452 } 1453 #endif 1454 1455 /* 1456 * Performs necessary checks before doing a file copy 1457 * 1458 * Can adjust amount of bytes to copy via @req_count argument. 1459 * Returns appropriate error code that caller should return or 1460 * zero in case the copy should be allowed. 1461 */ 1462 static int generic_copy_file_checks(struct file *file_in, loff_t pos_in, 1463 struct file *file_out, loff_t pos_out, 1464 size_t *req_count, unsigned int flags) 1465 { 1466 struct inode *inode_in = file_inode(file_in); 1467 struct inode *inode_out = file_inode(file_out); 1468 uint64_t count = *req_count; 1469 loff_t size_in; 1470 int ret; 1471 1472 ret = generic_file_rw_checks(file_in, file_out); 1473 if (ret) 1474 return ret; 1475 1476 /* 1477 * We allow some filesystems to handle cross sb copy, but passing 1478 * a file of the wrong filesystem type to filesystem driver can result 1479 * in an attempt to dereference the wrong type of ->private_data, so 1480 * avoid doing that until we really have a good reason. 1481 * 1482 * nfs and cifs define several different file_system_type structures 1483 * and several different sets of file_operations, but they all end up 1484 * using the same ->copy_file_range() function pointer. 1485 */ 1486 if (flags & COPY_FILE_SPLICE) { 1487 /* cross sb splice is allowed */ 1488 } else if (file_out->f_op->copy_file_range) { 1489 if (file_in->f_op->copy_file_range != 1490 file_out->f_op->copy_file_range) 1491 return -EXDEV; 1492 } else if (file_inode(file_in)->i_sb != file_inode(file_out)->i_sb) { 1493 return -EXDEV; 1494 } 1495 1496 /* Don't touch certain kinds of inodes */ 1497 if (IS_IMMUTABLE(inode_out)) 1498 return -EPERM; 1499 1500 if (IS_SWAPFILE(inode_in) || IS_SWAPFILE(inode_out)) 1501 return -ETXTBSY; 1502 1503 /* Ensure offsets don't wrap. */ 1504 if (pos_in + count < pos_in || pos_out + count < pos_out) 1505 return -EOVERFLOW; 1506 1507 /* Shorten the copy to EOF */ 1508 size_in = i_size_read(inode_in); 1509 if (pos_in >= size_in) 1510 count = 0; 1511 else 1512 count = min(count, size_in - (uint64_t)pos_in); 1513 1514 if (count) { 1515 ret = generic_write_check_limits(file_out, pos_out, &count); 1516 if (ret) 1517 return ret; 1518 } 1519 1520 /* Don't allow overlapped copying within the same file. */ 1521 if (inode_in == inode_out && 1522 pos_out + count > pos_in && 1523 pos_out < pos_in + count) 1524 return -EINVAL; 1525 1526 *req_count = count; 1527 return 0; 1528 } 1529 1530 /* 1531 * copy_file_range() differs from regular file read and write in that it 1532 * specifically allows return partial success. When it does so is up to 1533 * the copy_file_range method. 1534 */ 1535 ssize_t vfs_copy_file_range(struct file *file_in, loff_t pos_in, 1536 struct file *file_out, loff_t pos_out, 1537 size_t len, unsigned int flags) 1538 { 1539 ssize_t ret; 1540 bool splice = flags & COPY_FILE_SPLICE; 1541 bool samesb = file_inode(file_in)->i_sb == file_inode(file_out)->i_sb; 1542 1543 if (flags & ~COPY_FILE_SPLICE) 1544 return -EINVAL; 1545 1546 ret = generic_copy_file_checks(file_in, pos_in, file_out, pos_out, &len, 1547 flags); 1548 if (unlikely(ret)) 1549 return ret; 1550 1551 ret = rw_verify_area(READ, file_in, &pos_in, len); 1552 if (unlikely(ret)) 1553 return ret; 1554 1555 ret = rw_verify_area(WRITE, file_out, &pos_out, len); 1556 if (unlikely(ret)) 1557 return ret; 1558 1559 if (len == 0) 1560 return 0; 1561 1562 /* 1563 * Make sure return value doesn't overflow in 32bit compat mode. Also 1564 * limit the size for all cases except when calling ->copy_file_range(). 1565 */ 1566 if (splice || !file_out->f_op->copy_file_range || in_compat_syscall()) 1567 len = min_t(size_t, MAX_RW_COUNT, len); 1568 1569 file_start_write(file_out); 1570 1571 /* 1572 * Cloning is supported by more file systems, so we implement copy on 1573 * same sb using clone, but for filesystems where both clone and copy 1574 * are supported (e.g. nfs,cifs), we only call the copy method. 1575 */ 1576 if (!splice && file_out->f_op->copy_file_range) { 1577 ret = file_out->f_op->copy_file_range(file_in, pos_in, 1578 file_out, pos_out, 1579 len, flags); 1580 } else if (!splice && file_in->f_op->remap_file_range && samesb) { 1581 ret = file_in->f_op->remap_file_range(file_in, pos_in, 1582 file_out, pos_out, len, REMAP_FILE_CAN_SHORTEN); 1583 /* fallback to splice */ 1584 if (ret <= 0) 1585 splice = true; 1586 } else if (samesb) { 1587 /* Fallback to splice for same sb copy for backward compat */ 1588 splice = true; 1589 } 1590 1591 file_end_write(file_out); 1592 1593 if (!splice) 1594 goto done; 1595 1596 /* 1597 * We can get here for same sb copy of filesystems that do not implement 1598 * ->copy_file_range() in case filesystem does not support clone or in 1599 * case filesystem supports clone but rejected the clone request (e.g. 1600 * because it was not block aligned). 1601 * 1602 * In both cases, fall back to kernel copy so we are able to maintain a 1603 * consistent story about which filesystems support copy_file_range() 1604 * and which filesystems do not, that will allow userspace tools to 1605 * make consistent desicions w.r.t using copy_file_range(). 1606 * 1607 * We also get here if caller (e.g. nfsd) requested COPY_FILE_SPLICE 1608 * for server-side-copy between any two sb. 1609 * 1610 * In any case, we call do_splice_direct() and not splice_file_range(), 1611 * without file_start_write() held, to avoid possible deadlocks related 1612 * to splicing from input file, while file_start_write() is held on 1613 * the output file on a different sb. 1614 */ 1615 ret = do_splice_direct(file_in, &pos_in, file_out, &pos_out, len, 0); 1616 done: 1617 if (ret > 0) { 1618 fsnotify_access(file_in); 1619 add_rchar(current, ret); 1620 fsnotify_modify(file_out); 1621 add_wchar(current, ret); 1622 } 1623 1624 inc_syscr(current); 1625 inc_syscw(current); 1626 1627 return ret; 1628 } 1629 EXPORT_SYMBOL(vfs_copy_file_range); 1630 1631 SYSCALL_DEFINE6(copy_file_range, int, fd_in, loff_t __user *, off_in, 1632 int, fd_out, loff_t __user *, off_out, 1633 size_t, len, unsigned int, flags) 1634 { 1635 loff_t pos_in; 1636 loff_t pos_out; 1637 ssize_t ret = -EBADF; 1638 1639 CLASS(fd, f_in)(fd_in); 1640 if (fd_empty(f_in)) 1641 return -EBADF; 1642 1643 CLASS(fd, f_out)(fd_out); 1644 if (fd_empty(f_out)) 1645 return -EBADF; 1646 1647 if (off_in) { 1648 if (copy_from_user(&pos_in, off_in, sizeof(loff_t))) 1649 return -EFAULT; 1650 } else { 1651 pos_in = fd_file(f_in)->f_pos; 1652 } 1653 1654 if (off_out) { 1655 if (copy_from_user(&pos_out, off_out, sizeof(loff_t))) 1656 return -EFAULT; 1657 } else { 1658 pos_out = fd_file(f_out)->f_pos; 1659 } 1660 1661 if (flags != 0) 1662 return -EINVAL; 1663 1664 ret = vfs_copy_file_range(fd_file(f_in), pos_in, fd_file(f_out), pos_out, len, 1665 flags); 1666 if (ret > 0) { 1667 pos_in += ret; 1668 pos_out += ret; 1669 1670 if (off_in) { 1671 if (copy_to_user(off_in, &pos_in, sizeof(loff_t))) 1672 ret = -EFAULT; 1673 } else { 1674 fd_file(f_in)->f_pos = pos_in; 1675 } 1676 1677 if (off_out) { 1678 if (copy_to_user(off_out, &pos_out, sizeof(loff_t))) 1679 ret = -EFAULT; 1680 } else { 1681 fd_file(f_out)->f_pos = pos_out; 1682 } 1683 } 1684 return ret; 1685 } 1686 1687 /* 1688 * Don't operate on ranges the page cache doesn't support, and don't exceed the 1689 * LFS limits. If pos is under the limit it becomes a short access. If it 1690 * exceeds the limit we return -EFBIG. 1691 */ 1692 int generic_write_check_limits(struct file *file, loff_t pos, loff_t *count) 1693 { 1694 struct inode *inode = file->f_mapping->host; 1695 loff_t max_size = inode->i_sb->s_maxbytes; 1696 loff_t limit = rlimit(RLIMIT_FSIZE); 1697 1698 if (limit != RLIM_INFINITY) { 1699 if (pos >= limit) { 1700 send_sig(SIGXFSZ, current, 0); 1701 return -EFBIG; 1702 } 1703 *count = min(*count, limit - pos); 1704 } 1705 1706 if (!(file->f_flags & O_LARGEFILE)) 1707 max_size = MAX_NON_LFS; 1708 1709 if (unlikely(pos >= max_size)) 1710 return -EFBIG; 1711 1712 *count = min(*count, max_size - pos); 1713 1714 return 0; 1715 } 1716 EXPORT_SYMBOL_GPL(generic_write_check_limits); 1717 1718 /* Like generic_write_checks(), but takes size of write instead of iter. */ 1719 int generic_write_checks_count(struct kiocb *iocb, loff_t *count) 1720 { 1721 struct file *file = iocb->ki_filp; 1722 struct inode *inode = file->f_mapping->host; 1723 1724 if (IS_SWAPFILE(inode)) 1725 return -ETXTBSY; 1726 1727 if (!*count) 1728 return 0; 1729 1730 if (iocb->ki_flags & IOCB_APPEND) 1731 iocb->ki_pos = i_size_read(inode); 1732 1733 if ((iocb->ki_flags & IOCB_NOWAIT) && 1734 !((iocb->ki_flags & IOCB_DIRECT) || 1735 (file->f_op->fop_flags & FOP_BUFFER_WASYNC))) 1736 return -EINVAL; 1737 1738 return generic_write_check_limits(iocb->ki_filp, iocb->ki_pos, count); 1739 } 1740 EXPORT_SYMBOL(generic_write_checks_count); 1741 1742 /* 1743 * Performs necessary checks before doing a write 1744 * 1745 * Can adjust writing position or amount of bytes to write. 1746 * Returns the number of bytes that may be written on success (which 1747 * may be less than requested if truncated), or a negative error code. 1748 */ 1749 ssize_t generic_write_checks(struct kiocb *iocb, struct iov_iter *from) 1750 { 1751 loff_t count = iov_iter_count(from); 1752 int ret; 1753 1754 ret = generic_write_checks_count(iocb, &count); 1755 if (ret) 1756 return ret; 1757 1758 iov_iter_truncate(from, count); 1759 return iov_iter_count(from); 1760 } 1761 EXPORT_SYMBOL(generic_write_checks); 1762 1763 /* 1764 * Performs common checks before doing a file copy/clone 1765 * from @file_in to @file_out. 1766 */ 1767 int generic_file_rw_checks(struct file *file_in, struct file *file_out) 1768 { 1769 struct inode *inode_in = file_inode(file_in); 1770 struct inode *inode_out = file_inode(file_out); 1771 1772 /* Don't copy dirs, pipes, sockets... */ 1773 if (S_ISDIR(inode_in->i_mode) || S_ISDIR(inode_out->i_mode)) 1774 return -EISDIR; 1775 if (!S_ISREG(inode_in->i_mode) || !S_ISREG(inode_out->i_mode)) 1776 return -EINVAL; 1777 1778 if (!(file_in->f_mode & FMODE_READ) || 1779 !(file_out->f_mode & FMODE_WRITE) || 1780 (file_out->f_flags & O_APPEND)) 1781 return -EBADF; 1782 1783 return 0; 1784 } 1785 1786 int generic_atomic_write_valid(struct kiocb *iocb, struct iov_iter *iter) 1787 { 1788 size_t len = iov_iter_count(iter); 1789 1790 if (!iter_is_ubuf(iter)) 1791 return -EINVAL; 1792 1793 if (!is_power_of_2(len)) 1794 return -EINVAL; 1795 1796 if (!IS_ALIGNED(iocb->ki_pos, len)) 1797 return -EINVAL; 1798 1799 if (!(iocb->ki_flags & IOCB_DIRECT)) 1800 return -EOPNOTSUPP; 1801 1802 return 0; 1803 } 1804 EXPORT_SYMBOL_GPL(generic_atomic_write_valid); 1805