1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 1991, 1992 Linus Torvalds 4 * Copyright (C) 2001 Andrea Arcangeli <andrea@suse.de> SuSE 5 * Copyright (C) 2016 - 2020 Christoph Hellwig 6 */ 7 #include <linux/init.h> 8 #include <linux/mm.h> 9 #include <linux/blkdev.h> 10 #include <linux/buffer_head.h> 11 #include <linux/mpage.h> 12 #include <linux/uio.h> 13 #include <linux/namei.h> 14 #include <linux/task_io_accounting_ops.h> 15 #include <linux/falloc.h> 16 #include <linux/suspend.h> 17 #include <linux/fs.h> 18 #include <linux/iomap.h> 19 #include <linux/module.h> 20 #include <linux/io_uring/cmd.h> 21 #include "blk.h" 22 23 static inline struct inode *bdev_file_inode(struct file *file) 24 { 25 return file->f_mapping->host; 26 } 27 28 static blk_opf_t dio_bio_write_op(struct kiocb *iocb) 29 { 30 blk_opf_t opf = REQ_OP_WRITE | REQ_SYNC | REQ_IDLE; 31 32 /* avoid the need for a I/O completion work item */ 33 if (iocb_is_dsync(iocb)) 34 opf |= REQ_FUA; 35 return opf; 36 } 37 38 static bool blkdev_dio_invalid(struct block_device *bdev, struct kiocb *iocb, 39 struct iov_iter *iter) 40 { 41 return iocb->ki_pos & (bdev_logical_block_size(bdev) - 1) || 42 !bdev_iter_is_aligned(bdev, iter); 43 } 44 45 #define DIO_INLINE_BIO_VECS 4 46 47 static ssize_t __blkdev_direct_IO_simple(struct kiocb *iocb, 48 struct iov_iter *iter, struct block_device *bdev, 49 unsigned int nr_pages) 50 { 51 struct bio_vec inline_vecs[DIO_INLINE_BIO_VECS], *vecs; 52 loff_t pos = iocb->ki_pos; 53 bool should_dirty = false; 54 struct bio bio; 55 ssize_t ret; 56 57 if (nr_pages <= DIO_INLINE_BIO_VECS) 58 vecs = inline_vecs; 59 else { 60 vecs = kmalloc_array(nr_pages, sizeof(struct bio_vec), 61 GFP_KERNEL); 62 if (!vecs) 63 return -ENOMEM; 64 } 65 66 if (iov_iter_rw(iter) == READ) { 67 bio_init(&bio, bdev, vecs, nr_pages, REQ_OP_READ); 68 if (user_backed_iter(iter)) 69 should_dirty = true; 70 } else { 71 bio_init(&bio, bdev, vecs, nr_pages, dio_bio_write_op(iocb)); 72 } 73 bio.bi_iter.bi_sector = pos >> SECTOR_SHIFT; 74 bio.bi_write_hint = file_inode(iocb->ki_filp)->i_write_hint; 75 bio.bi_ioprio = iocb->ki_ioprio; 76 if (iocb->ki_flags & IOCB_ATOMIC) 77 bio.bi_opf |= REQ_ATOMIC; 78 79 ret = bio_iov_iter_get_pages(&bio, iter); 80 if (unlikely(ret)) 81 goto out; 82 ret = bio.bi_iter.bi_size; 83 84 if (iov_iter_rw(iter) == WRITE) 85 task_io_account_write(ret); 86 87 if (iocb->ki_flags & IOCB_NOWAIT) 88 bio.bi_opf |= REQ_NOWAIT; 89 90 submit_bio_wait(&bio); 91 92 bio_release_pages(&bio, should_dirty); 93 if (unlikely(bio.bi_status)) 94 ret = blk_status_to_errno(bio.bi_status); 95 96 out: 97 if (vecs != inline_vecs) 98 kfree(vecs); 99 100 bio_uninit(&bio); 101 102 return ret; 103 } 104 105 enum { 106 DIO_SHOULD_DIRTY = 1, 107 DIO_IS_SYNC = 2, 108 }; 109 110 struct blkdev_dio { 111 union { 112 struct kiocb *iocb; 113 struct task_struct *waiter; 114 }; 115 size_t size; 116 atomic_t ref; 117 unsigned int flags; 118 struct bio bio ____cacheline_aligned_in_smp; 119 }; 120 121 static struct bio_set blkdev_dio_pool; 122 123 static void blkdev_bio_end_io(struct bio *bio) 124 { 125 struct blkdev_dio *dio = bio->bi_private; 126 bool should_dirty = dio->flags & DIO_SHOULD_DIRTY; 127 128 if (bio->bi_status && !dio->bio.bi_status) 129 dio->bio.bi_status = bio->bi_status; 130 131 if (atomic_dec_and_test(&dio->ref)) { 132 if (!(dio->flags & DIO_IS_SYNC)) { 133 struct kiocb *iocb = dio->iocb; 134 ssize_t ret; 135 136 WRITE_ONCE(iocb->private, NULL); 137 138 if (likely(!dio->bio.bi_status)) { 139 ret = dio->size; 140 iocb->ki_pos += ret; 141 } else { 142 ret = blk_status_to_errno(dio->bio.bi_status); 143 } 144 145 dio->iocb->ki_complete(iocb, ret); 146 bio_put(&dio->bio); 147 } else { 148 struct task_struct *waiter = dio->waiter; 149 150 WRITE_ONCE(dio->waiter, NULL); 151 blk_wake_io_task(waiter); 152 } 153 } 154 155 if (should_dirty) { 156 bio_check_pages_dirty(bio); 157 } else { 158 bio_release_pages(bio, false); 159 bio_put(bio); 160 } 161 } 162 163 static ssize_t __blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter, 164 struct block_device *bdev, unsigned int nr_pages) 165 { 166 struct blk_plug plug; 167 struct blkdev_dio *dio; 168 struct bio *bio; 169 bool is_read = (iov_iter_rw(iter) == READ), is_sync; 170 blk_opf_t opf = is_read ? REQ_OP_READ : dio_bio_write_op(iocb); 171 loff_t pos = iocb->ki_pos; 172 int ret = 0; 173 174 if (iocb->ki_flags & IOCB_ALLOC_CACHE) 175 opf |= REQ_ALLOC_CACHE; 176 bio = bio_alloc_bioset(bdev, nr_pages, opf, GFP_KERNEL, 177 &blkdev_dio_pool); 178 dio = container_of(bio, struct blkdev_dio, bio); 179 atomic_set(&dio->ref, 1); 180 /* 181 * Grab an extra reference to ensure the dio structure which is embedded 182 * into the first bio stays around. 183 */ 184 bio_get(bio); 185 186 is_sync = is_sync_kiocb(iocb); 187 if (is_sync) { 188 dio->flags = DIO_IS_SYNC; 189 dio->waiter = current; 190 } else { 191 dio->flags = 0; 192 dio->iocb = iocb; 193 } 194 195 dio->size = 0; 196 if (is_read && user_backed_iter(iter)) 197 dio->flags |= DIO_SHOULD_DIRTY; 198 199 blk_start_plug(&plug); 200 201 for (;;) { 202 bio->bi_iter.bi_sector = pos >> SECTOR_SHIFT; 203 bio->bi_write_hint = file_inode(iocb->ki_filp)->i_write_hint; 204 bio->bi_private = dio; 205 bio->bi_end_io = blkdev_bio_end_io; 206 bio->bi_ioprio = iocb->ki_ioprio; 207 208 ret = bio_iov_iter_get_pages(bio, iter); 209 if (unlikely(ret)) { 210 bio->bi_status = BLK_STS_IOERR; 211 bio_endio(bio); 212 break; 213 } 214 if (iocb->ki_flags & IOCB_NOWAIT) { 215 /* 216 * This is nonblocking IO, and we need to allocate 217 * another bio if we have data left to map. As we 218 * cannot guarantee that one of the sub bios will not 219 * fail getting issued FOR NOWAIT and as error results 220 * are coalesced across all of them, be safe and ask for 221 * a retry of this from blocking context. 222 */ 223 if (unlikely(iov_iter_count(iter))) { 224 bio_release_pages(bio, false); 225 bio_clear_flag(bio, BIO_REFFED); 226 bio_put(bio); 227 blk_finish_plug(&plug); 228 return -EAGAIN; 229 } 230 bio->bi_opf |= REQ_NOWAIT; 231 } 232 233 if (is_read) { 234 if (dio->flags & DIO_SHOULD_DIRTY) 235 bio_set_pages_dirty(bio); 236 } else { 237 task_io_account_write(bio->bi_iter.bi_size); 238 } 239 dio->size += bio->bi_iter.bi_size; 240 pos += bio->bi_iter.bi_size; 241 242 nr_pages = bio_iov_vecs_to_alloc(iter, BIO_MAX_VECS); 243 if (!nr_pages) { 244 submit_bio(bio); 245 break; 246 } 247 atomic_inc(&dio->ref); 248 submit_bio(bio); 249 bio = bio_alloc(bdev, nr_pages, opf, GFP_KERNEL); 250 } 251 252 blk_finish_plug(&plug); 253 254 if (!is_sync) 255 return -EIOCBQUEUED; 256 257 for (;;) { 258 set_current_state(TASK_UNINTERRUPTIBLE); 259 if (!READ_ONCE(dio->waiter)) 260 break; 261 blk_io_schedule(); 262 } 263 __set_current_state(TASK_RUNNING); 264 265 if (!ret) 266 ret = blk_status_to_errno(dio->bio.bi_status); 267 if (likely(!ret)) 268 ret = dio->size; 269 270 bio_put(&dio->bio); 271 return ret; 272 } 273 274 static void blkdev_bio_end_io_async(struct bio *bio) 275 { 276 struct blkdev_dio *dio = container_of(bio, struct blkdev_dio, bio); 277 struct kiocb *iocb = dio->iocb; 278 ssize_t ret; 279 280 WRITE_ONCE(iocb->private, NULL); 281 282 if (likely(!bio->bi_status)) { 283 ret = dio->size; 284 iocb->ki_pos += ret; 285 } else { 286 ret = blk_status_to_errno(bio->bi_status); 287 } 288 289 iocb->ki_complete(iocb, ret); 290 291 if (dio->flags & DIO_SHOULD_DIRTY) { 292 bio_check_pages_dirty(bio); 293 } else { 294 bio_release_pages(bio, false); 295 bio_put(bio); 296 } 297 } 298 299 static ssize_t __blkdev_direct_IO_async(struct kiocb *iocb, 300 struct iov_iter *iter, 301 struct block_device *bdev, 302 unsigned int nr_pages) 303 { 304 bool is_read = iov_iter_rw(iter) == READ; 305 blk_opf_t opf = is_read ? REQ_OP_READ : dio_bio_write_op(iocb); 306 struct blkdev_dio *dio; 307 struct bio *bio; 308 loff_t pos = iocb->ki_pos; 309 int ret = 0; 310 311 if (iocb->ki_flags & IOCB_ALLOC_CACHE) 312 opf |= REQ_ALLOC_CACHE; 313 bio = bio_alloc_bioset(bdev, nr_pages, opf, GFP_KERNEL, 314 &blkdev_dio_pool); 315 dio = container_of(bio, struct blkdev_dio, bio); 316 dio->flags = 0; 317 dio->iocb = iocb; 318 bio->bi_iter.bi_sector = pos >> SECTOR_SHIFT; 319 bio->bi_write_hint = file_inode(iocb->ki_filp)->i_write_hint; 320 bio->bi_end_io = blkdev_bio_end_io_async; 321 bio->bi_ioprio = iocb->ki_ioprio; 322 323 if (iov_iter_is_bvec(iter)) { 324 /* 325 * Users don't rely on the iterator being in any particular 326 * state for async I/O returning -EIOCBQUEUED, hence we can 327 * avoid expensive iov_iter_advance(). Bypass 328 * bio_iov_iter_get_pages() and set the bvec directly. 329 */ 330 bio_iov_bvec_set(bio, iter); 331 } else { 332 ret = bio_iov_iter_get_pages(bio, iter); 333 if (unlikely(ret)) { 334 bio_put(bio); 335 return ret; 336 } 337 } 338 dio->size = bio->bi_iter.bi_size; 339 340 if (is_read) { 341 if (user_backed_iter(iter)) { 342 dio->flags |= DIO_SHOULD_DIRTY; 343 bio_set_pages_dirty(bio); 344 } 345 } else { 346 task_io_account_write(bio->bi_iter.bi_size); 347 } 348 349 if (iocb->ki_flags & IOCB_ATOMIC) 350 bio->bi_opf |= REQ_ATOMIC; 351 352 if (iocb->ki_flags & IOCB_NOWAIT) 353 bio->bi_opf |= REQ_NOWAIT; 354 355 if (iocb->ki_flags & IOCB_HIPRI) { 356 bio->bi_opf |= REQ_POLLED; 357 submit_bio(bio); 358 WRITE_ONCE(iocb->private, bio); 359 } else { 360 submit_bio(bio); 361 } 362 return -EIOCBQUEUED; 363 } 364 365 static ssize_t blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter) 366 { 367 struct block_device *bdev = I_BDEV(iocb->ki_filp->f_mapping->host); 368 unsigned int nr_pages; 369 370 if (!iov_iter_count(iter)) 371 return 0; 372 373 if (blkdev_dio_invalid(bdev, iocb, iter)) 374 return -EINVAL; 375 376 nr_pages = bio_iov_vecs_to_alloc(iter, BIO_MAX_VECS + 1); 377 if (likely(nr_pages <= BIO_MAX_VECS)) { 378 if (is_sync_kiocb(iocb)) 379 return __blkdev_direct_IO_simple(iocb, iter, bdev, 380 nr_pages); 381 return __blkdev_direct_IO_async(iocb, iter, bdev, nr_pages); 382 } else if (iocb->ki_flags & IOCB_ATOMIC) { 383 return -EINVAL; 384 } 385 return __blkdev_direct_IO(iocb, iter, bdev, bio_max_segs(nr_pages)); 386 } 387 388 static int blkdev_iomap_begin(struct inode *inode, loff_t offset, loff_t length, 389 unsigned int flags, struct iomap *iomap, struct iomap *srcmap) 390 { 391 struct block_device *bdev = I_BDEV(inode); 392 loff_t isize = i_size_read(inode); 393 394 if (offset >= isize) 395 return -EIO; 396 397 iomap->bdev = bdev; 398 iomap->offset = ALIGN_DOWN(offset, bdev_logical_block_size(bdev)); 399 iomap->type = IOMAP_MAPPED; 400 iomap->addr = iomap->offset; 401 iomap->length = isize - iomap->offset; 402 iomap->flags |= IOMAP_F_BUFFER_HEAD; /* noop for !CONFIG_BUFFER_HEAD */ 403 return 0; 404 } 405 406 static const struct iomap_ops blkdev_iomap_ops = { 407 .iomap_begin = blkdev_iomap_begin, 408 }; 409 410 #ifdef CONFIG_BUFFER_HEAD 411 static int blkdev_get_block(struct inode *inode, sector_t iblock, 412 struct buffer_head *bh, int create) 413 { 414 bh->b_bdev = I_BDEV(inode); 415 bh->b_blocknr = iblock; 416 set_buffer_mapped(bh); 417 return 0; 418 } 419 420 /* 421 * We cannot call mpage_writepages() as it does not take the buffer lock. 422 * We must use block_write_full_folio() directly which holds the buffer 423 * lock. The buffer lock provides the synchronisation with writeback 424 * that filesystems rely on when they use the blockdev's mapping. 425 */ 426 static int blkdev_writepages(struct address_space *mapping, 427 struct writeback_control *wbc) 428 { 429 struct blk_plug plug; 430 int err; 431 432 blk_start_plug(&plug); 433 err = write_cache_pages(mapping, wbc, block_write_full_folio, 434 blkdev_get_block); 435 blk_finish_plug(&plug); 436 437 return err; 438 } 439 440 static int blkdev_read_folio(struct file *file, struct folio *folio) 441 { 442 return block_read_full_folio(folio, blkdev_get_block); 443 } 444 445 static void blkdev_readahead(struct readahead_control *rac) 446 { 447 mpage_readahead(rac, blkdev_get_block); 448 } 449 450 static int blkdev_write_begin(struct file *file, struct address_space *mapping, 451 loff_t pos, unsigned len, struct folio **foliop, void **fsdata) 452 { 453 return block_write_begin(mapping, pos, len, foliop, blkdev_get_block); 454 } 455 456 static int blkdev_write_end(struct file *file, struct address_space *mapping, 457 loff_t pos, unsigned len, unsigned copied, struct folio *folio, 458 void *fsdata) 459 { 460 int ret; 461 ret = block_write_end(file, mapping, pos, len, copied, folio, fsdata); 462 463 folio_unlock(folio); 464 folio_put(folio); 465 466 return ret; 467 } 468 469 const struct address_space_operations def_blk_aops = { 470 .dirty_folio = block_dirty_folio, 471 .invalidate_folio = block_invalidate_folio, 472 .read_folio = blkdev_read_folio, 473 .readahead = blkdev_readahead, 474 .writepages = blkdev_writepages, 475 .write_begin = blkdev_write_begin, 476 .write_end = blkdev_write_end, 477 .migrate_folio = buffer_migrate_folio_norefs, 478 .is_dirty_writeback = buffer_check_dirty_writeback, 479 }; 480 #else /* CONFIG_BUFFER_HEAD */ 481 static int blkdev_read_folio(struct file *file, struct folio *folio) 482 { 483 return iomap_read_folio(folio, &blkdev_iomap_ops); 484 } 485 486 static void blkdev_readahead(struct readahead_control *rac) 487 { 488 iomap_readahead(rac, &blkdev_iomap_ops); 489 } 490 491 static int blkdev_map_blocks(struct iomap_writepage_ctx *wpc, 492 struct inode *inode, loff_t offset, unsigned int len) 493 { 494 loff_t isize = i_size_read(inode); 495 496 if (WARN_ON_ONCE(offset >= isize)) 497 return -EIO; 498 if (offset >= wpc->iomap.offset && 499 offset < wpc->iomap.offset + wpc->iomap.length) 500 return 0; 501 return blkdev_iomap_begin(inode, offset, isize - offset, 502 IOMAP_WRITE, &wpc->iomap, NULL); 503 } 504 505 static const struct iomap_writeback_ops blkdev_writeback_ops = { 506 .map_blocks = blkdev_map_blocks, 507 }; 508 509 static int blkdev_writepages(struct address_space *mapping, 510 struct writeback_control *wbc) 511 { 512 struct iomap_writepage_ctx wpc = { }; 513 514 return iomap_writepages(mapping, wbc, &wpc, &blkdev_writeback_ops); 515 } 516 517 const struct address_space_operations def_blk_aops = { 518 .dirty_folio = filemap_dirty_folio, 519 .release_folio = iomap_release_folio, 520 .invalidate_folio = iomap_invalidate_folio, 521 .read_folio = blkdev_read_folio, 522 .readahead = blkdev_readahead, 523 .writepages = blkdev_writepages, 524 .is_partially_uptodate = iomap_is_partially_uptodate, 525 .error_remove_folio = generic_error_remove_folio, 526 .migrate_folio = filemap_migrate_folio, 527 }; 528 #endif /* CONFIG_BUFFER_HEAD */ 529 530 /* 531 * for a block special file file_inode(file)->i_size is zero 532 * so we compute the size by hand (just as in block_read/write above) 533 */ 534 static loff_t blkdev_llseek(struct file *file, loff_t offset, int whence) 535 { 536 struct inode *bd_inode = bdev_file_inode(file); 537 loff_t retval; 538 539 inode_lock(bd_inode); 540 retval = fixed_size_llseek(file, offset, whence, i_size_read(bd_inode)); 541 inode_unlock(bd_inode); 542 return retval; 543 } 544 545 static int blkdev_fsync(struct file *filp, loff_t start, loff_t end, 546 int datasync) 547 { 548 struct block_device *bdev = I_BDEV(filp->f_mapping->host); 549 int error; 550 551 error = file_write_and_wait_range(filp, start, end); 552 if (error) 553 return error; 554 555 /* 556 * There is no need to serialise calls to blkdev_issue_flush with 557 * i_mutex and doing so causes performance issues with concurrent 558 * O_SYNC writers to a block device. 559 */ 560 error = blkdev_issue_flush(bdev); 561 if (error == -EOPNOTSUPP) 562 error = 0; 563 564 return error; 565 } 566 567 /** 568 * file_to_blk_mode - get block open flags from file flags 569 * @file: file whose open flags should be converted 570 * 571 * Look at file open flags and generate corresponding block open flags from 572 * them. The function works both for file just being open (e.g. during ->open 573 * callback) and for file that is already open. This is actually non-trivial 574 * (see comment in the function). 575 */ 576 blk_mode_t file_to_blk_mode(struct file *file) 577 { 578 blk_mode_t mode = 0; 579 580 if (file->f_mode & FMODE_READ) 581 mode |= BLK_OPEN_READ; 582 if (file->f_mode & FMODE_WRITE) 583 mode |= BLK_OPEN_WRITE; 584 /* 585 * do_dentry_open() clears O_EXCL from f_flags, use file->private_data 586 * to determine whether the open was exclusive for already open files. 587 */ 588 if (file->private_data) 589 mode |= BLK_OPEN_EXCL; 590 else if (file->f_flags & O_EXCL) 591 mode |= BLK_OPEN_EXCL; 592 if (file->f_flags & O_NDELAY) 593 mode |= BLK_OPEN_NDELAY; 594 595 /* 596 * If all bits in O_ACCMODE set (aka O_RDWR | O_WRONLY), the floppy 597 * driver has historically allowed ioctls as if the file was opened for 598 * writing, but does not allow and actual reads or writes. 599 */ 600 if ((file->f_flags & O_ACCMODE) == (O_RDWR | O_WRONLY)) 601 mode |= BLK_OPEN_WRITE_IOCTL; 602 603 return mode; 604 } 605 606 static int blkdev_open(struct inode *inode, struct file *filp) 607 { 608 struct block_device *bdev; 609 blk_mode_t mode; 610 int ret; 611 612 mode = file_to_blk_mode(filp); 613 /* Use the file as the holder. */ 614 if (mode & BLK_OPEN_EXCL) 615 filp->private_data = filp; 616 ret = bdev_permission(inode->i_rdev, mode, filp->private_data); 617 if (ret) 618 return ret; 619 620 bdev = blkdev_get_no_open(inode->i_rdev); 621 if (!bdev) 622 return -ENXIO; 623 624 if (bdev_can_atomic_write(bdev)) 625 filp->f_mode |= FMODE_CAN_ATOMIC_WRITE; 626 627 ret = bdev_open(bdev, mode, filp->private_data, NULL, filp); 628 if (ret) 629 blkdev_put_no_open(bdev); 630 return ret; 631 } 632 633 static int blkdev_release(struct inode *inode, struct file *filp) 634 { 635 bdev_release(filp); 636 return 0; 637 } 638 639 static ssize_t 640 blkdev_direct_write(struct kiocb *iocb, struct iov_iter *from) 641 { 642 size_t count = iov_iter_count(from); 643 ssize_t written; 644 645 written = kiocb_invalidate_pages(iocb, count); 646 if (written) { 647 if (written == -EBUSY) 648 return 0; 649 return written; 650 } 651 652 written = blkdev_direct_IO(iocb, from); 653 if (written > 0) { 654 kiocb_invalidate_post_direct_write(iocb, count); 655 iocb->ki_pos += written; 656 count -= written; 657 } 658 if (written != -EIOCBQUEUED) 659 iov_iter_revert(from, count - iov_iter_count(from)); 660 return written; 661 } 662 663 static ssize_t blkdev_buffered_write(struct kiocb *iocb, struct iov_iter *from) 664 { 665 return iomap_file_buffered_write(iocb, from, &blkdev_iomap_ops, NULL); 666 } 667 668 /* 669 * Write data to the block device. Only intended for the block device itself 670 * and the raw driver which basically is a fake block device. 671 * 672 * Does not take i_mutex for the write and thus is not for general purpose 673 * use. 674 */ 675 static ssize_t blkdev_write_iter(struct kiocb *iocb, struct iov_iter *from) 676 { 677 struct file *file = iocb->ki_filp; 678 struct inode *bd_inode = bdev_file_inode(file); 679 struct block_device *bdev = I_BDEV(bd_inode); 680 loff_t size = bdev_nr_bytes(bdev); 681 size_t shorted = 0; 682 ssize_t ret; 683 684 if (bdev_read_only(bdev)) 685 return -EPERM; 686 687 if (IS_SWAPFILE(bd_inode) && !is_hibernate_resume_dev(bd_inode->i_rdev)) 688 return -ETXTBSY; 689 690 if (!iov_iter_count(from)) 691 return 0; 692 693 if (iocb->ki_pos >= size) 694 return -ENOSPC; 695 696 if ((iocb->ki_flags & (IOCB_NOWAIT | IOCB_DIRECT)) == IOCB_NOWAIT) 697 return -EOPNOTSUPP; 698 699 if (iocb->ki_flags & IOCB_ATOMIC) { 700 ret = generic_atomic_write_valid(iocb, from); 701 if (ret) 702 return ret; 703 } 704 705 size -= iocb->ki_pos; 706 if (iov_iter_count(from) > size) { 707 shorted = iov_iter_count(from) - size; 708 iov_iter_truncate(from, size); 709 } 710 711 ret = file_update_time(file); 712 if (ret) 713 return ret; 714 715 if (iocb->ki_flags & IOCB_DIRECT) { 716 ret = blkdev_direct_write(iocb, from); 717 if (ret >= 0 && iov_iter_count(from)) 718 ret = direct_write_fallback(iocb, from, ret, 719 blkdev_buffered_write(iocb, from)); 720 } else { 721 ret = blkdev_buffered_write(iocb, from); 722 } 723 724 if (ret > 0) 725 ret = generic_write_sync(iocb, ret); 726 iov_iter_reexpand(from, iov_iter_count(from) + shorted); 727 return ret; 728 } 729 730 static ssize_t blkdev_read_iter(struct kiocb *iocb, struct iov_iter *to) 731 { 732 struct block_device *bdev = I_BDEV(iocb->ki_filp->f_mapping->host); 733 loff_t size = bdev_nr_bytes(bdev); 734 loff_t pos = iocb->ki_pos; 735 size_t shorted = 0; 736 ssize_t ret = 0; 737 size_t count; 738 739 if (unlikely(pos + iov_iter_count(to) > size)) { 740 if (pos >= size) 741 return 0; 742 size -= pos; 743 shorted = iov_iter_count(to) - size; 744 iov_iter_truncate(to, size); 745 } 746 747 count = iov_iter_count(to); 748 if (!count) 749 goto reexpand; /* skip atime */ 750 751 if (iocb->ki_flags & IOCB_DIRECT) { 752 ret = kiocb_write_and_wait(iocb, count); 753 if (ret < 0) 754 goto reexpand; 755 file_accessed(iocb->ki_filp); 756 757 ret = blkdev_direct_IO(iocb, to); 758 if (ret >= 0) { 759 iocb->ki_pos += ret; 760 count -= ret; 761 } 762 iov_iter_revert(to, count - iov_iter_count(to)); 763 if (ret < 0 || !count) 764 goto reexpand; 765 } 766 767 ret = filemap_read(iocb, to, ret); 768 769 reexpand: 770 if (unlikely(shorted)) 771 iov_iter_reexpand(to, iov_iter_count(to) + shorted); 772 return ret; 773 } 774 775 #define BLKDEV_FALLOC_FL_SUPPORTED \ 776 (FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | \ 777 FALLOC_FL_ZERO_RANGE) 778 779 static long blkdev_fallocate(struct file *file, int mode, loff_t start, 780 loff_t len) 781 { 782 struct inode *inode = bdev_file_inode(file); 783 struct block_device *bdev = I_BDEV(inode); 784 loff_t end = start + len - 1; 785 loff_t isize; 786 int error; 787 788 /* Fail if we don't recognize the flags. */ 789 if (mode & ~BLKDEV_FALLOC_FL_SUPPORTED) 790 return -EOPNOTSUPP; 791 792 /* Don't go off the end of the device. */ 793 isize = bdev_nr_bytes(bdev); 794 if (start >= isize) 795 return -EINVAL; 796 if (end >= isize) { 797 if (mode & FALLOC_FL_KEEP_SIZE) { 798 len = isize - start; 799 end = start + len - 1; 800 } else 801 return -EINVAL; 802 } 803 804 /* 805 * Don't allow IO that isn't aligned to logical block size. 806 */ 807 if ((start | len) & (bdev_logical_block_size(bdev) - 1)) 808 return -EINVAL; 809 810 filemap_invalidate_lock(inode->i_mapping); 811 812 /* 813 * Invalidate the page cache, including dirty pages, for valid 814 * de-allocate mode calls to fallocate(). 815 */ 816 switch (mode) { 817 case FALLOC_FL_ZERO_RANGE: 818 case FALLOC_FL_ZERO_RANGE | FALLOC_FL_KEEP_SIZE: 819 error = truncate_bdev_range(bdev, file_to_blk_mode(file), start, end); 820 if (error) 821 goto fail; 822 823 error = blkdev_issue_zeroout(bdev, start >> SECTOR_SHIFT, 824 len >> SECTOR_SHIFT, GFP_KERNEL, 825 BLKDEV_ZERO_NOUNMAP); 826 break; 827 case FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE: 828 error = truncate_bdev_range(bdev, file_to_blk_mode(file), start, end); 829 if (error) 830 goto fail; 831 832 error = blkdev_issue_zeroout(bdev, start >> SECTOR_SHIFT, 833 len >> SECTOR_SHIFT, GFP_KERNEL, 834 BLKDEV_ZERO_NOFALLBACK); 835 break; 836 default: 837 error = -EOPNOTSUPP; 838 } 839 840 fail: 841 filemap_invalidate_unlock(inode->i_mapping); 842 return error; 843 } 844 845 static int blkdev_mmap(struct file *file, struct vm_area_struct *vma) 846 { 847 struct inode *bd_inode = bdev_file_inode(file); 848 849 if (bdev_read_only(I_BDEV(bd_inode))) 850 return generic_file_readonly_mmap(file, vma); 851 852 return generic_file_mmap(file, vma); 853 } 854 855 const struct file_operations def_blk_fops = { 856 .open = blkdev_open, 857 .release = blkdev_release, 858 .llseek = blkdev_llseek, 859 .read_iter = blkdev_read_iter, 860 .write_iter = blkdev_write_iter, 861 .iopoll = iocb_bio_iopoll, 862 .mmap = blkdev_mmap, 863 .fsync = blkdev_fsync, 864 .unlocked_ioctl = blkdev_ioctl, 865 #ifdef CONFIG_COMPAT 866 .compat_ioctl = compat_blkdev_ioctl, 867 #endif 868 .splice_read = filemap_splice_read, 869 .splice_write = iter_file_splice_write, 870 .fallocate = blkdev_fallocate, 871 .uring_cmd = blkdev_uring_cmd, 872 .fop_flags = FOP_BUFFER_RASYNC, 873 }; 874 875 static __init int blkdev_init(void) 876 { 877 return bioset_init(&blkdev_dio_pool, 4, 878 offsetof(struct blkdev_dio, bio), 879 BIOSET_NEED_BVECS|BIOSET_PERCPU_CACHE); 880 } 881 module_init(blkdev_init); 882