at v5.11 49 kB view raw
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 8#include <linux/init.h> 9#include <linux/mm.h> 10#include <linux/fcntl.h> 11#include <linux/slab.h> 12#include <linux/kmod.h> 13#include <linux/major.h> 14#include <linux/device_cgroup.h> 15#include <linux/highmem.h> 16#include <linux/blkdev.h> 17#include <linux/backing-dev.h> 18#include <linux/module.h> 19#include <linux/blkpg.h> 20#include <linux/magic.h> 21#include <linux/buffer_head.h> 22#include <linux/swap.h> 23#include <linux/pagevec.h> 24#include <linux/writeback.h> 25#include <linux/mpage.h> 26#include <linux/mount.h> 27#include <linux/pseudo_fs.h> 28#include <linux/uio.h> 29#include <linux/namei.h> 30#include <linux/log2.h> 31#include <linux/cleancache.h> 32#include <linux/task_io_accounting_ops.h> 33#include <linux/falloc.h> 34#include <linux/part_stat.h> 35#include <linux/uaccess.h> 36#include <linux/suspend.h> 37#include "internal.h" 38 39struct bdev_inode { 40 struct block_device bdev; 41 struct inode vfs_inode; 42}; 43 44static const struct address_space_operations def_blk_aops; 45 46static inline struct bdev_inode *BDEV_I(struct inode *inode) 47{ 48 return container_of(inode, struct bdev_inode, vfs_inode); 49} 50 51struct block_device *I_BDEV(struct inode *inode) 52{ 53 return &BDEV_I(inode)->bdev; 54} 55EXPORT_SYMBOL(I_BDEV); 56 57static void bdev_write_inode(struct block_device *bdev) 58{ 59 struct inode *inode = bdev->bd_inode; 60 int ret; 61 62 spin_lock(&inode->i_lock); 63 while (inode->i_state & I_DIRTY) { 64 spin_unlock(&inode->i_lock); 65 ret = write_inode_now(inode, true); 66 if (ret) { 67 char name[BDEVNAME_SIZE]; 68 pr_warn_ratelimited("VFS: Dirty inode writeback failed " 69 "for block device %s (err=%d).\n", 70 bdevname(bdev, name), ret); 71 } 72 spin_lock(&inode->i_lock); 73 } 74 spin_unlock(&inode->i_lock); 75} 76 77/* Kill _all_ buffers and pagecache , dirty or not.. */ 78static void kill_bdev(struct block_device *bdev) 79{ 80 struct address_space *mapping = bdev->bd_inode->i_mapping; 81 82 if (mapping->nrpages == 0 && mapping->nrexceptional == 0) 83 return; 84 85 invalidate_bh_lrus(); 86 truncate_inode_pages(mapping, 0); 87} 88 89/* Invalidate clean unused buffers and pagecache. */ 90void invalidate_bdev(struct block_device *bdev) 91{ 92 struct address_space *mapping = bdev->bd_inode->i_mapping; 93 94 if (mapping->nrpages) { 95 invalidate_bh_lrus(); 96 lru_add_drain_all(); /* make sure all lru add caches are flushed */ 97 invalidate_mapping_pages(mapping, 0, -1); 98 } 99 /* 99% of the time, we don't need to flush the cleancache on the bdev. 100 * But, for the strange corners, lets be cautious 101 */ 102 cleancache_invalidate_inode(mapping); 103} 104EXPORT_SYMBOL(invalidate_bdev); 105 106/* 107 * Drop all buffers & page cache for given bdev range. This function bails 108 * with error if bdev has other exclusive owner (such as filesystem). 109 */ 110int truncate_bdev_range(struct block_device *bdev, fmode_t mode, 111 loff_t lstart, loff_t lend) 112{ 113 /* 114 * If we don't hold exclusive handle for the device, upgrade to it 115 * while we discard the buffer cache to avoid discarding buffers 116 * under live filesystem. 117 */ 118 if (!(mode & FMODE_EXCL)) { 119 int err = bd_prepare_to_claim(bdev, truncate_bdev_range); 120 if (err) 121 return err; 122 } 123 124 truncate_inode_pages_range(bdev->bd_inode->i_mapping, lstart, lend); 125 if (!(mode & FMODE_EXCL)) 126 bd_abort_claiming(bdev, truncate_bdev_range); 127 return 0; 128} 129EXPORT_SYMBOL(truncate_bdev_range); 130 131static void set_init_blocksize(struct block_device *bdev) 132{ 133 unsigned int bsize = bdev_logical_block_size(bdev); 134 loff_t size = i_size_read(bdev->bd_inode); 135 136 while (bsize < PAGE_SIZE) { 137 if (size & bsize) 138 break; 139 bsize <<= 1; 140 } 141 bdev->bd_inode->i_blkbits = blksize_bits(bsize); 142} 143 144int set_blocksize(struct block_device *bdev, int size) 145{ 146 /* Size must be a power of two, and between 512 and PAGE_SIZE */ 147 if (size > PAGE_SIZE || size < 512 || !is_power_of_2(size)) 148 return -EINVAL; 149 150 /* Size cannot be smaller than the size supported by the device */ 151 if (size < bdev_logical_block_size(bdev)) 152 return -EINVAL; 153 154 /* Don't change the size if it is same as current */ 155 if (bdev->bd_inode->i_blkbits != blksize_bits(size)) { 156 sync_blockdev(bdev); 157 bdev->bd_inode->i_blkbits = blksize_bits(size); 158 kill_bdev(bdev); 159 } 160 return 0; 161} 162 163EXPORT_SYMBOL(set_blocksize); 164 165int sb_set_blocksize(struct super_block *sb, int size) 166{ 167 if (set_blocksize(sb->s_bdev, size)) 168 return 0; 169 /* If we get here, we know size is power of two 170 * and it's value is between 512 and PAGE_SIZE */ 171 sb->s_blocksize = size; 172 sb->s_blocksize_bits = blksize_bits(size); 173 return sb->s_blocksize; 174} 175 176EXPORT_SYMBOL(sb_set_blocksize); 177 178int sb_min_blocksize(struct super_block *sb, int size) 179{ 180 int minsize = bdev_logical_block_size(sb->s_bdev); 181 if (size < minsize) 182 size = minsize; 183 return sb_set_blocksize(sb, size); 184} 185 186EXPORT_SYMBOL(sb_min_blocksize); 187 188static int 189blkdev_get_block(struct inode *inode, sector_t iblock, 190 struct buffer_head *bh, int create) 191{ 192 bh->b_bdev = I_BDEV(inode); 193 bh->b_blocknr = iblock; 194 set_buffer_mapped(bh); 195 return 0; 196} 197 198static struct inode *bdev_file_inode(struct file *file) 199{ 200 return file->f_mapping->host; 201} 202 203static unsigned int dio_bio_write_op(struct kiocb *iocb) 204{ 205 unsigned int op = REQ_OP_WRITE | REQ_SYNC | REQ_IDLE; 206 207 /* avoid the need for a I/O completion work item */ 208 if (iocb->ki_flags & IOCB_DSYNC) 209 op |= REQ_FUA; 210 return op; 211} 212 213#define DIO_INLINE_BIO_VECS 4 214 215static void blkdev_bio_end_io_simple(struct bio *bio) 216{ 217 struct task_struct *waiter = bio->bi_private; 218 219 WRITE_ONCE(bio->bi_private, NULL); 220 blk_wake_io_task(waiter); 221} 222 223static ssize_t 224__blkdev_direct_IO_simple(struct kiocb *iocb, struct iov_iter *iter, 225 int nr_pages) 226{ 227 struct file *file = iocb->ki_filp; 228 struct block_device *bdev = I_BDEV(bdev_file_inode(file)); 229 struct bio_vec inline_vecs[DIO_INLINE_BIO_VECS], *vecs; 230 loff_t pos = iocb->ki_pos; 231 bool should_dirty = false; 232 struct bio bio; 233 ssize_t ret; 234 blk_qc_t qc; 235 236 if ((pos | iov_iter_alignment(iter)) & 237 (bdev_logical_block_size(bdev) - 1)) 238 return -EINVAL; 239 240 if (nr_pages <= DIO_INLINE_BIO_VECS) 241 vecs = inline_vecs; 242 else { 243 vecs = kmalloc_array(nr_pages, sizeof(struct bio_vec), 244 GFP_KERNEL); 245 if (!vecs) 246 return -ENOMEM; 247 } 248 249 bio_init(&bio, vecs, nr_pages); 250 bio_set_dev(&bio, bdev); 251 bio.bi_iter.bi_sector = pos >> 9; 252 bio.bi_write_hint = iocb->ki_hint; 253 bio.bi_private = current; 254 bio.bi_end_io = blkdev_bio_end_io_simple; 255 bio.bi_ioprio = iocb->ki_ioprio; 256 257 ret = bio_iov_iter_get_pages(&bio, iter); 258 if (unlikely(ret)) 259 goto out; 260 ret = bio.bi_iter.bi_size; 261 262 if (iov_iter_rw(iter) == READ) { 263 bio.bi_opf = REQ_OP_READ; 264 if (iter_is_iovec(iter)) 265 should_dirty = true; 266 } else { 267 bio.bi_opf = dio_bio_write_op(iocb); 268 task_io_account_write(ret); 269 } 270 if (iocb->ki_flags & IOCB_HIPRI) 271 bio_set_polled(&bio, iocb); 272 273 qc = submit_bio(&bio); 274 for (;;) { 275 set_current_state(TASK_UNINTERRUPTIBLE); 276 if (!READ_ONCE(bio.bi_private)) 277 break; 278 if (!(iocb->ki_flags & IOCB_HIPRI) || 279 !blk_poll(bdev_get_queue(bdev), qc, true)) 280 blk_io_schedule(); 281 } 282 __set_current_state(TASK_RUNNING); 283 284 bio_release_pages(&bio, should_dirty); 285 if (unlikely(bio.bi_status)) 286 ret = blk_status_to_errno(bio.bi_status); 287 288out: 289 if (vecs != inline_vecs) 290 kfree(vecs); 291 292 bio_uninit(&bio); 293 294 return ret; 295} 296 297struct blkdev_dio { 298 union { 299 struct kiocb *iocb; 300 struct task_struct *waiter; 301 }; 302 size_t size; 303 atomic_t ref; 304 bool multi_bio : 1; 305 bool should_dirty : 1; 306 bool is_sync : 1; 307 struct bio bio; 308}; 309 310static struct bio_set blkdev_dio_pool; 311 312static int blkdev_iopoll(struct kiocb *kiocb, bool wait) 313{ 314 struct block_device *bdev = I_BDEV(kiocb->ki_filp->f_mapping->host); 315 struct request_queue *q = bdev_get_queue(bdev); 316 317 return blk_poll(q, READ_ONCE(kiocb->ki_cookie), wait); 318} 319 320static void blkdev_bio_end_io(struct bio *bio) 321{ 322 struct blkdev_dio *dio = bio->bi_private; 323 bool should_dirty = dio->should_dirty; 324 325 if (bio->bi_status && !dio->bio.bi_status) 326 dio->bio.bi_status = bio->bi_status; 327 328 if (!dio->multi_bio || atomic_dec_and_test(&dio->ref)) { 329 if (!dio->is_sync) { 330 struct kiocb *iocb = dio->iocb; 331 ssize_t ret; 332 333 if (likely(!dio->bio.bi_status)) { 334 ret = dio->size; 335 iocb->ki_pos += ret; 336 } else { 337 ret = blk_status_to_errno(dio->bio.bi_status); 338 } 339 340 dio->iocb->ki_complete(iocb, ret, 0); 341 if (dio->multi_bio) 342 bio_put(&dio->bio); 343 } else { 344 struct task_struct *waiter = dio->waiter; 345 346 WRITE_ONCE(dio->waiter, NULL); 347 blk_wake_io_task(waiter); 348 } 349 } 350 351 if (should_dirty) { 352 bio_check_pages_dirty(bio); 353 } else { 354 bio_release_pages(bio, false); 355 bio_put(bio); 356 } 357} 358 359static ssize_t 360__blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter, int nr_pages) 361{ 362 struct file *file = iocb->ki_filp; 363 struct inode *inode = bdev_file_inode(file); 364 struct block_device *bdev = I_BDEV(inode); 365 struct blk_plug plug; 366 struct blkdev_dio *dio; 367 struct bio *bio; 368 bool is_poll = (iocb->ki_flags & IOCB_HIPRI) != 0; 369 bool is_read = (iov_iter_rw(iter) == READ), is_sync; 370 loff_t pos = iocb->ki_pos; 371 blk_qc_t qc = BLK_QC_T_NONE; 372 int ret = 0; 373 374 if ((pos | iov_iter_alignment(iter)) & 375 (bdev_logical_block_size(bdev) - 1)) 376 return -EINVAL; 377 378 bio = bio_alloc_bioset(GFP_KERNEL, nr_pages, &blkdev_dio_pool); 379 380 dio = container_of(bio, struct blkdev_dio, bio); 381 dio->is_sync = is_sync = is_sync_kiocb(iocb); 382 if (dio->is_sync) { 383 dio->waiter = current; 384 bio_get(bio); 385 } else { 386 dio->iocb = iocb; 387 } 388 389 dio->size = 0; 390 dio->multi_bio = false; 391 dio->should_dirty = is_read && iter_is_iovec(iter); 392 393 /* 394 * Don't plug for HIPRI/polled IO, as those should go straight 395 * to issue 396 */ 397 if (!is_poll) 398 blk_start_plug(&plug); 399 400 for (;;) { 401 bio_set_dev(bio, bdev); 402 bio->bi_iter.bi_sector = pos >> 9; 403 bio->bi_write_hint = iocb->ki_hint; 404 bio->bi_private = dio; 405 bio->bi_end_io = blkdev_bio_end_io; 406 bio->bi_ioprio = iocb->ki_ioprio; 407 408 ret = bio_iov_iter_get_pages(bio, iter); 409 if (unlikely(ret)) { 410 bio->bi_status = BLK_STS_IOERR; 411 bio_endio(bio); 412 break; 413 } 414 415 if (is_read) { 416 bio->bi_opf = REQ_OP_READ; 417 if (dio->should_dirty) 418 bio_set_pages_dirty(bio); 419 } else { 420 bio->bi_opf = dio_bio_write_op(iocb); 421 task_io_account_write(bio->bi_iter.bi_size); 422 } 423 424 dio->size += bio->bi_iter.bi_size; 425 pos += bio->bi_iter.bi_size; 426 427 nr_pages = iov_iter_npages(iter, BIO_MAX_PAGES); 428 if (!nr_pages) { 429 bool polled = false; 430 431 if (iocb->ki_flags & IOCB_HIPRI) { 432 bio_set_polled(bio, iocb); 433 polled = true; 434 } 435 436 qc = submit_bio(bio); 437 438 if (polled) 439 WRITE_ONCE(iocb->ki_cookie, qc); 440 break; 441 } 442 443 if (!dio->multi_bio) { 444 /* 445 * AIO needs an extra reference to ensure the dio 446 * structure which is embedded into the first bio 447 * stays around. 448 */ 449 if (!is_sync) 450 bio_get(bio); 451 dio->multi_bio = true; 452 atomic_set(&dio->ref, 2); 453 } else { 454 atomic_inc(&dio->ref); 455 } 456 457 submit_bio(bio); 458 bio = bio_alloc(GFP_KERNEL, nr_pages); 459 } 460 461 if (!is_poll) 462 blk_finish_plug(&plug); 463 464 if (!is_sync) 465 return -EIOCBQUEUED; 466 467 for (;;) { 468 set_current_state(TASK_UNINTERRUPTIBLE); 469 if (!READ_ONCE(dio->waiter)) 470 break; 471 472 if (!(iocb->ki_flags & IOCB_HIPRI) || 473 !blk_poll(bdev_get_queue(bdev), qc, true)) 474 blk_io_schedule(); 475 } 476 __set_current_state(TASK_RUNNING); 477 478 if (!ret) 479 ret = blk_status_to_errno(dio->bio.bi_status); 480 if (likely(!ret)) 481 ret = dio->size; 482 483 bio_put(&dio->bio); 484 return ret; 485} 486 487static ssize_t 488blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter) 489{ 490 int nr_pages; 491 492 nr_pages = iov_iter_npages(iter, BIO_MAX_PAGES + 1); 493 if (!nr_pages) 494 return 0; 495 if (is_sync_kiocb(iocb) && nr_pages <= BIO_MAX_PAGES) 496 return __blkdev_direct_IO_simple(iocb, iter, nr_pages); 497 498 return __blkdev_direct_IO(iocb, iter, min(nr_pages, BIO_MAX_PAGES)); 499} 500 501static __init int blkdev_init(void) 502{ 503 return bioset_init(&blkdev_dio_pool, 4, offsetof(struct blkdev_dio, bio), BIOSET_NEED_BVECS); 504} 505module_init(blkdev_init); 506 507int __sync_blockdev(struct block_device *bdev, int wait) 508{ 509 if (!bdev) 510 return 0; 511 if (!wait) 512 return filemap_flush(bdev->bd_inode->i_mapping); 513 return filemap_write_and_wait(bdev->bd_inode->i_mapping); 514} 515 516/* 517 * Write out and wait upon all the dirty data associated with a block 518 * device via its mapping. Does not take the superblock lock. 519 */ 520int sync_blockdev(struct block_device *bdev) 521{ 522 return __sync_blockdev(bdev, 1); 523} 524EXPORT_SYMBOL(sync_blockdev); 525 526/* 527 * Write out and wait upon all dirty data associated with this 528 * device. Filesystem data as well as the underlying block 529 * device. Takes the superblock lock. 530 */ 531int fsync_bdev(struct block_device *bdev) 532{ 533 struct super_block *sb = get_super(bdev); 534 if (sb) { 535 int res = sync_filesystem(sb); 536 drop_super(sb); 537 return res; 538 } 539 return sync_blockdev(bdev); 540} 541EXPORT_SYMBOL(fsync_bdev); 542 543/** 544 * freeze_bdev -- lock a filesystem and force it into a consistent state 545 * @bdev: blockdevice to lock 546 * 547 * If a superblock is found on this device, we take the s_umount semaphore 548 * on it to make sure nobody unmounts until the snapshot creation is done. 549 * The reference counter (bd_fsfreeze_count) guarantees that only the last 550 * unfreeze process can unfreeze the frozen filesystem actually when multiple 551 * freeze requests arrive simultaneously. It counts up in freeze_bdev() and 552 * count down in thaw_bdev(). When it becomes 0, thaw_bdev() will unfreeze 553 * actually. 554 */ 555int freeze_bdev(struct block_device *bdev) 556{ 557 struct super_block *sb; 558 int error = 0; 559 560 mutex_lock(&bdev->bd_fsfreeze_mutex); 561 if (++bdev->bd_fsfreeze_count > 1) 562 goto done; 563 564 sb = get_active_super(bdev); 565 if (!sb) 566 goto sync; 567 if (sb->s_op->freeze_super) 568 error = sb->s_op->freeze_super(sb); 569 else 570 error = freeze_super(sb); 571 deactivate_super(sb); 572 573 if (error) { 574 bdev->bd_fsfreeze_count--; 575 goto done; 576 } 577 bdev->bd_fsfreeze_sb = sb; 578 579sync: 580 sync_blockdev(bdev); 581done: 582 mutex_unlock(&bdev->bd_fsfreeze_mutex); 583 return error; 584} 585EXPORT_SYMBOL(freeze_bdev); 586 587/** 588 * thaw_bdev -- unlock filesystem 589 * @bdev: blockdevice to unlock 590 * 591 * Unlocks the filesystem and marks it writeable again after freeze_bdev(). 592 */ 593int thaw_bdev(struct block_device *bdev) 594{ 595 struct super_block *sb; 596 int error = -EINVAL; 597 598 mutex_lock(&bdev->bd_fsfreeze_mutex); 599 if (!bdev->bd_fsfreeze_count) 600 goto out; 601 602 error = 0; 603 if (--bdev->bd_fsfreeze_count > 0) 604 goto out; 605 606 sb = bdev->bd_fsfreeze_sb; 607 if (!sb) 608 goto out; 609 610 if (sb->s_op->thaw_super) 611 error = sb->s_op->thaw_super(sb); 612 else 613 error = thaw_super(sb); 614 if (error) 615 bdev->bd_fsfreeze_count++; 616 else 617 bdev->bd_fsfreeze_sb = NULL; 618out: 619 mutex_unlock(&bdev->bd_fsfreeze_mutex); 620 return error; 621} 622EXPORT_SYMBOL(thaw_bdev); 623 624static int blkdev_writepage(struct page *page, struct writeback_control *wbc) 625{ 626 return block_write_full_page(page, blkdev_get_block, wbc); 627} 628 629static int blkdev_readpage(struct file * file, struct page * page) 630{ 631 return block_read_full_page(page, blkdev_get_block); 632} 633 634static void blkdev_readahead(struct readahead_control *rac) 635{ 636 mpage_readahead(rac, blkdev_get_block); 637} 638 639static int blkdev_write_begin(struct file *file, struct address_space *mapping, 640 loff_t pos, unsigned len, unsigned flags, 641 struct page **pagep, void **fsdata) 642{ 643 return block_write_begin(mapping, pos, len, flags, pagep, 644 blkdev_get_block); 645} 646 647static int blkdev_write_end(struct file *file, struct address_space *mapping, 648 loff_t pos, unsigned len, unsigned copied, 649 struct page *page, void *fsdata) 650{ 651 int ret; 652 ret = block_write_end(file, mapping, pos, len, copied, page, fsdata); 653 654 unlock_page(page); 655 put_page(page); 656 657 return ret; 658} 659 660/* 661 * private llseek: 662 * for a block special file file_inode(file)->i_size is zero 663 * so we compute the size by hand (just as in block_read/write above) 664 */ 665static loff_t block_llseek(struct file *file, loff_t offset, int whence) 666{ 667 struct inode *bd_inode = bdev_file_inode(file); 668 loff_t retval; 669 670 inode_lock(bd_inode); 671 retval = fixed_size_llseek(file, offset, whence, i_size_read(bd_inode)); 672 inode_unlock(bd_inode); 673 return retval; 674} 675 676int blkdev_fsync(struct file *filp, loff_t start, loff_t end, int datasync) 677{ 678 struct inode *bd_inode = bdev_file_inode(filp); 679 struct block_device *bdev = I_BDEV(bd_inode); 680 int error; 681 682 error = file_write_and_wait_range(filp, start, end); 683 if (error) 684 return error; 685 686 /* 687 * There is no need to serialise calls to blkdev_issue_flush with 688 * i_mutex and doing so causes performance issues with concurrent 689 * O_SYNC writers to a block device. 690 */ 691 error = blkdev_issue_flush(bdev, GFP_KERNEL); 692 if (error == -EOPNOTSUPP) 693 error = 0; 694 695 return error; 696} 697EXPORT_SYMBOL(blkdev_fsync); 698 699/** 700 * bdev_read_page() - Start reading a page from a block device 701 * @bdev: The device to read the page from 702 * @sector: The offset on the device to read the page to (need not be aligned) 703 * @page: The page to read 704 * 705 * On entry, the page should be locked. It will be unlocked when the page 706 * has been read. If the block driver implements rw_page synchronously, 707 * that will be true on exit from this function, but it need not be. 708 * 709 * Errors returned by this function are usually "soft", eg out of memory, or 710 * queue full; callers should try a different route to read this page rather 711 * than propagate an error back up the stack. 712 * 713 * Return: negative errno if an error occurs, 0 if submission was successful. 714 */ 715int bdev_read_page(struct block_device *bdev, sector_t sector, 716 struct page *page) 717{ 718 const struct block_device_operations *ops = bdev->bd_disk->fops; 719 int result = -EOPNOTSUPP; 720 721 if (!ops->rw_page || bdev_get_integrity(bdev)) 722 return result; 723 724 result = blk_queue_enter(bdev->bd_disk->queue, 0); 725 if (result) 726 return result; 727 result = ops->rw_page(bdev, sector + get_start_sect(bdev), page, 728 REQ_OP_READ); 729 blk_queue_exit(bdev->bd_disk->queue); 730 return result; 731} 732 733/** 734 * bdev_write_page() - Start writing a page to a block device 735 * @bdev: The device to write the page to 736 * @sector: The offset on the device to write the page to (need not be aligned) 737 * @page: The page to write 738 * @wbc: The writeback_control for the write 739 * 740 * On entry, the page should be locked and not currently under writeback. 741 * On exit, if the write started successfully, the page will be unlocked and 742 * under writeback. If the write failed already (eg the driver failed to 743 * queue the page to the device), the page will still be locked. If the 744 * caller is a ->writepage implementation, it will need to unlock the page. 745 * 746 * Errors returned by this function are usually "soft", eg out of memory, or 747 * queue full; callers should try a different route to write this page rather 748 * than propagate an error back up the stack. 749 * 750 * Return: negative errno if an error occurs, 0 if submission was successful. 751 */ 752int bdev_write_page(struct block_device *bdev, sector_t sector, 753 struct page *page, struct writeback_control *wbc) 754{ 755 int result; 756 const struct block_device_operations *ops = bdev->bd_disk->fops; 757 758 if (!ops->rw_page || bdev_get_integrity(bdev)) 759 return -EOPNOTSUPP; 760 result = blk_queue_enter(bdev->bd_disk->queue, 0); 761 if (result) 762 return result; 763 764 set_page_writeback(page); 765 result = ops->rw_page(bdev, sector + get_start_sect(bdev), page, 766 REQ_OP_WRITE); 767 if (result) { 768 end_page_writeback(page); 769 } else { 770 clean_page_buffers(page); 771 unlock_page(page); 772 } 773 blk_queue_exit(bdev->bd_disk->queue); 774 return result; 775} 776 777/* 778 * pseudo-fs 779 */ 780 781static __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock); 782static struct kmem_cache * bdev_cachep __read_mostly; 783 784static struct inode *bdev_alloc_inode(struct super_block *sb) 785{ 786 struct bdev_inode *ei = kmem_cache_alloc(bdev_cachep, GFP_KERNEL); 787 788 if (!ei) 789 return NULL; 790 memset(&ei->bdev, 0, sizeof(ei->bdev)); 791 ei->bdev.bd_bdi = &noop_backing_dev_info; 792 return &ei->vfs_inode; 793} 794 795static void bdev_free_inode(struct inode *inode) 796{ 797 struct block_device *bdev = I_BDEV(inode); 798 799 free_percpu(bdev->bd_stats); 800 kfree(bdev->bd_meta_info); 801 802 kmem_cache_free(bdev_cachep, BDEV_I(inode)); 803} 804 805static void init_once(void *data) 806{ 807 struct bdev_inode *ei = data; 808 809 inode_init_once(&ei->vfs_inode); 810} 811 812static void bdev_evict_inode(struct inode *inode) 813{ 814 struct block_device *bdev = &BDEV_I(inode)->bdev; 815 truncate_inode_pages_final(&inode->i_data); 816 invalidate_inode_buffers(inode); /* is it needed here? */ 817 clear_inode(inode); 818 /* Detach inode from wb early as bdi_put() may free bdi->wb */ 819 inode_detach_wb(inode); 820 if (bdev->bd_bdi != &noop_backing_dev_info) { 821 bdi_put(bdev->bd_bdi); 822 bdev->bd_bdi = &noop_backing_dev_info; 823 } 824} 825 826static const struct super_operations bdev_sops = { 827 .statfs = simple_statfs, 828 .alloc_inode = bdev_alloc_inode, 829 .free_inode = bdev_free_inode, 830 .drop_inode = generic_delete_inode, 831 .evict_inode = bdev_evict_inode, 832}; 833 834static int bd_init_fs_context(struct fs_context *fc) 835{ 836 struct pseudo_fs_context *ctx = init_pseudo(fc, BDEVFS_MAGIC); 837 if (!ctx) 838 return -ENOMEM; 839 fc->s_iflags |= SB_I_CGROUPWB; 840 ctx->ops = &bdev_sops; 841 return 0; 842} 843 844static struct file_system_type bd_type = { 845 .name = "bdev", 846 .init_fs_context = bd_init_fs_context, 847 .kill_sb = kill_anon_super, 848}; 849 850struct super_block *blockdev_superblock __read_mostly; 851EXPORT_SYMBOL_GPL(blockdev_superblock); 852 853void __init bdev_cache_init(void) 854{ 855 int err; 856 static struct vfsmount *bd_mnt; 857 858 bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode), 859 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT| 860 SLAB_MEM_SPREAD|SLAB_ACCOUNT|SLAB_PANIC), 861 init_once); 862 err = register_filesystem(&bd_type); 863 if (err) 864 panic("Cannot register bdev pseudo-fs"); 865 bd_mnt = kern_mount(&bd_type); 866 if (IS_ERR(bd_mnt)) 867 panic("Cannot create bdev pseudo-fs"); 868 blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */ 869} 870 871struct block_device *bdev_alloc(struct gendisk *disk, u8 partno) 872{ 873 struct block_device *bdev; 874 struct inode *inode; 875 876 inode = new_inode(blockdev_superblock); 877 if (!inode) 878 return NULL; 879 inode->i_mode = S_IFBLK; 880 inode->i_rdev = 0; 881 inode->i_data.a_ops = &def_blk_aops; 882 mapping_set_gfp_mask(&inode->i_data, GFP_USER); 883 884 bdev = I_BDEV(inode); 885 mutex_init(&bdev->bd_mutex); 886 mutex_init(&bdev->bd_fsfreeze_mutex); 887 spin_lock_init(&bdev->bd_size_lock); 888 bdev->bd_disk = disk; 889 bdev->bd_partno = partno; 890 bdev->bd_inode = inode; 891#ifdef CONFIG_SYSFS 892 INIT_LIST_HEAD(&bdev->bd_holder_disks); 893#endif 894 bdev->bd_stats = alloc_percpu(struct disk_stats); 895 if (!bdev->bd_stats) { 896 iput(inode); 897 return NULL; 898 } 899 return bdev; 900} 901 902void bdev_add(struct block_device *bdev, dev_t dev) 903{ 904 bdev->bd_dev = dev; 905 bdev->bd_inode->i_rdev = dev; 906 bdev->bd_inode->i_ino = dev; 907 insert_inode_hash(bdev->bd_inode); 908} 909 910static struct block_device *bdget(dev_t dev) 911{ 912 struct inode *inode; 913 914 inode = ilookup(blockdev_superblock, dev); 915 if (!inode) 916 return NULL; 917 return &BDEV_I(inode)->bdev; 918} 919 920/** 921 * bdgrab -- Grab a reference to an already referenced block device 922 * @bdev: Block device to grab a reference to. 923 * 924 * Returns the block_device with an additional reference when successful, 925 * or NULL if the inode is already beeing freed. 926 */ 927struct block_device *bdgrab(struct block_device *bdev) 928{ 929 if (!igrab(bdev->bd_inode)) 930 return NULL; 931 return bdev; 932} 933EXPORT_SYMBOL(bdgrab); 934 935long nr_blockdev_pages(void) 936{ 937 struct inode *inode; 938 long ret = 0; 939 940 spin_lock(&blockdev_superblock->s_inode_list_lock); 941 list_for_each_entry(inode, &blockdev_superblock->s_inodes, i_sb_list) 942 ret += inode->i_mapping->nrpages; 943 spin_unlock(&blockdev_superblock->s_inode_list_lock); 944 945 return ret; 946} 947 948void bdput(struct block_device *bdev) 949{ 950 iput(bdev->bd_inode); 951} 952EXPORT_SYMBOL(bdput); 953 954/** 955 * bd_may_claim - test whether a block device can be claimed 956 * @bdev: block device of interest 957 * @whole: whole block device containing @bdev, may equal @bdev 958 * @holder: holder trying to claim @bdev 959 * 960 * Test whether @bdev can be claimed by @holder. 961 * 962 * CONTEXT: 963 * spin_lock(&bdev_lock). 964 * 965 * RETURNS: 966 * %true if @bdev can be claimed, %false otherwise. 967 */ 968static bool bd_may_claim(struct block_device *bdev, struct block_device *whole, 969 void *holder) 970{ 971 if (bdev->bd_holder == holder) 972 return true; /* already a holder */ 973 else if (bdev->bd_holder != NULL) 974 return false; /* held by someone else */ 975 else if (whole == bdev) 976 return true; /* is a whole device which isn't held */ 977 978 else if (whole->bd_holder == bd_may_claim) 979 return true; /* is a partition of a device that is being partitioned */ 980 else if (whole->bd_holder != NULL) 981 return false; /* is a partition of a held device */ 982 else 983 return true; /* is a partition of an un-held device */ 984} 985 986/** 987 * bd_prepare_to_claim - claim a block device 988 * @bdev: block device of interest 989 * @holder: holder trying to claim @bdev 990 * 991 * Claim @bdev. This function fails if @bdev is already claimed by another 992 * holder and waits if another claiming is in progress. return, the caller 993 * has ownership of bd_claiming and bd_holder[s]. 994 * 995 * RETURNS: 996 * 0 if @bdev can be claimed, -EBUSY otherwise. 997 */ 998int bd_prepare_to_claim(struct block_device *bdev, void *holder) 999{ 1000 struct block_device *whole = bdev_whole(bdev); 1001 1002 if (WARN_ON_ONCE(!holder)) 1003 return -EINVAL; 1004retry: 1005 spin_lock(&bdev_lock); 1006 /* if someone else claimed, fail */ 1007 if (!bd_may_claim(bdev, whole, holder)) { 1008 spin_unlock(&bdev_lock); 1009 return -EBUSY; 1010 } 1011 1012 /* if claiming is already in progress, wait for it to finish */ 1013 if (whole->bd_claiming) { 1014 wait_queue_head_t *wq = bit_waitqueue(&whole->bd_claiming, 0); 1015 DEFINE_WAIT(wait); 1016 1017 prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE); 1018 spin_unlock(&bdev_lock); 1019 schedule(); 1020 finish_wait(wq, &wait); 1021 goto retry; 1022 } 1023 1024 /* yay, all mine */ 1025 whole->bd_claiming = holder; 1026 spin_unlock(&bdev_lock); 1027 return 0; 1028} 1029EXPORT_SYMBOL_GPL(bd_prepare_to_claim); /* only for the loop driver */ 1030 1031static void bd_clear_claiming(struct block_device *whole, void *holder) 1032{ 1033 lockdep_assert_held(&bdev_lock); 1034 /* tell others that we're done */ 1035 BUG_ON(whole->bd_claiming != holder); 1036 whole->bd_claiming = NULL; 1037 wake_up_bit(&whole->bd_claiming, 0); 1038} 1039 1040/** 1041 * bd_finish_claiming - finish claiming of a block device 1042 * @bdev: block device of interest 1043 * @holder: holder that has claimed @bdev 1044 * 1045 * Finish exclusive open of a block device. Mark the device as exlusively 1046 * open by the holder and wake up all waiters for exclusive open to finish. 1047 */ 1048static void bd_finish_claiming(struct block_device *bdev, void *holder) 1049{ 1050 struct block_device *whole = bdev_whole(bdev); 1051 1052 spin_lock(&bdev_lock); 1053 BUG_ON(!bd_may_claim(bdev, whole, holder)); 1054 /* 1055 * Note that for a whole device bd_holders will be incremented twice, 1056 * and bd_holder will be set to bd_may_claim before being set to holder 1057 */ 1058 whole->bd_holders++; 1059 whole->bd_holder = bd_may_claim; 1060 bdev->bd_holders++; 1061 bdev->bd_holder = holder; 1062 bd_clear_claiming(whole, holder); 1063 spin_unlock(&bdev_lock); 1064} 1065 1066/** 1067 * bd_abort_claiming - abort claiming of a block device 1068 * @bdev: block device of interest 1069 * @holder: holder that has claimed @bdev 1070 * 1071 * Abort claiming of a block device when the exclusive open failed. This can be 1072 * also used when exclusive open is not actually desired and we just needed 1073 * to block other exclusive openers for a while. 1074 */ 1075void bd_abort_claiming(struct block_device *bdev, void *holder) 1076{ 1077 spin_lock(&bdev_lock); 1078 bd_clear_claiming(bdev_whole(bdev), holder); 1079 spin_unlock(&bdev_lock); 1080} 1081EXPORT_SYMBOL(bd_abort_claiming); 1082 1083#ifdef CONFIG_SYSFS 1084struct bd_holder_disk { 1085 struct list_head list; 1086 struct gendisk *disk; 1087 int refcnt; 1088}; 1089 1090static struct bd_holder_disk *bd_find_holder_disk(struct block_device *bdev, 1091 struct gendisk *disk) 1092{ 1093 struct bd_holder_disk *holder; 1094 1095 list_for_each_entry(holder, &bdev->bd_holder_disks, list) 1096 if (holder->disk == disk) 1097 return holder; 1098 return NULL; 1099} 1100 1101static int add_symlink(struct kobject *from, struct kobject *to) 1102{ 1103 return sysfs_create_link(from, to, kobject_name(to)); 1104} 1105 1106static void del_symlink(struct kobject *from, struct kobject *to) 1107{ 1108 sysfs_remove_link(from, kobject_name(to)); 1109} 1110 1111/** 1112 * bd_link_disk_holder - create symlinks between holding disk and slave bdev 1113 * @bdev: the claimed slave bdev 1114 * @disk: the holding disk 1115 * 1116 * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT. 1117 * 1118 * This functions creates the following sysfs symlinks. 1119 * 1120 * - from "slaves" directory of the holder @disk to the claimed @bdev 1121 * - from "holders" directory of the @bdev to the holder @disk 1122 * 1123 * For example, if /dev/dm-0 maps to /dev/sda and disk for dm-0 is 1124 * passed to bd_link_disk_holder(), then: 1125 * 1126 * /sys/block/dm-0/slaves/sda --> /sys/block/sda 1127 * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0 1128 * 1129 * The caller must have claimed @bdev before calling this function and 1130 * ensure that both @bdev and @disk are valid during the creation and 1131 * lifetime of these symlinks. 1132 * 1133 * CONTEXT: 1134 * Might sleep. 1135 * 1136 * RETURNS: 1137 * 0 on success, -errno on failure. 1138 */ 1139int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk) 1140{ 1141 struct bd_holder_disk *holder; 1142 int ret = 0; 1143 1144 mutex_lock(&bdev->bd_mutex); 1145 1146 WARN_ON_ONCE(!bdev->bd_holder); 1147 1148 /* FIXME: remove the following once add_disk() handles errors */ 1149 if (WARN_ON(!disk->slave_dir || !bdev->bd_holder_dir)) 1150 goto out_unlock; 1151 1152 holder = bd_find_holder_disk(bdev, disk); 1153 if (holder) { 1154 holder->refcnt++; 1155 goto out_unlock; 1156 } 1157 1158 holder = kzalloc(sizeof(*holder), GFP_KERNEL); 1159 if (!holder) { 1160 ret = -ENOMEM; 1161 goto out_unlock; 1162 } 1163 1164 INIT_LIST_HEAD(&holder->list); 1165 holder->disk = disk; 1166 holder->refcnt = 1; 1167 1168 ret = add_symlink(disk->slave_dir, bdev_kobj(bdev)); 1169 if (ret) 1170 goto out_free; 1171 1172 ret = add_symlink(bdev->bd_holder_dir, &disk_to_dev(disk)->kobj); 1173 if (ret) 1174 goto out_del; 1175 /* 1176 * bdev could be deleted beneath us which would implicitly destroy 1177 * the holder directory. Hold on to it. 1178 */ 1179 kobject_get(bdev->bd_holder_dir); 1180 1181 list_add(&holder->list, &bdev->bd_holder_disks); 1182 goto out_unlock; 1183 1184out_del: 1185 del_symlink(disk->slave_dir, bdev_kobj(bdev)); 1186out_free: 1187 kfree(holder); 1188out_unlock: 1189 mutex_unlock(&bdev->bd_mutex); 1190 return ret; 1191} 1192EXPORT_SYMBOL_GPL(bd_link_disk_holder); 1193 1194/** 1195 * bd_unlink_disk_holder - destroy symlinks created by bd_link_disk_holder() 1196 * @bdev: the calimed slave bdev 1197 * @disk: the holding disk 1198 * 1199 * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT. 1200 * 1201 * CONTEXT: 1202 * Might sleep. 1203 */ 1204void bd_unlink_disk_holder(struct block_device *bdev, struct gendisk *disk) 1205{ 1206 struct bd_holder_disk *holder; 1207 1208 mutex_lock(&bdev->bd_mutex); 1209 1210 holder = bd_find_holder_disk(bdev, disk); 1211 1212 if (!WARN_ON_ONCE(holder == NULL) && !--holder->refcnt) { 1213 del_symlink(disk->slave_dir, bdev_kobj(bdev)); 1214 del_symlink(bdev->bd_holder_dir, &disk_to_dev(disk)->kobj); 1215 kobject_put(bdev->bd_holder_dir); 1216 list_del_init(&holder->list); 1217 kfree(holder); 1218 } 1219 1220 mutex_unlock(&bdev->bd_mutex); 1221} 1222EXPORT_SYMBOL_GPL(bd_unlink_disk_holder); 1223#endif 1224 1225static void __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part); 1226 1227int bdev_disk_changed(struct block_device *bdev, bool invalidate) 1228{ 1229 struct gendisk *disk = bdev->bd_disk; 1230 int ret; 1231 1232 lockdep_assert_held(&bdev->bd_mutex); 1233 1234 clear_bit(GD_NEED_PART_SCAN, &bdev->bd_disk->state); 1235 1236rescan: 1237 ret = blk_drop_partitions(bdev); 1238 if (ret) 1239 return ret; 1240 1241 /* 1242 * Historically we only set the capacity to zero for devices that 1243 * support partitions (independ of actually having partitions created). 1244 * Doing that is rather inconsistent, but changing it broke legacy 1245 * udisks polling for legacy ide-cdrom devices. Use the crude check 1246 * below to get the sane behavior for most device while not breaking 1247 * userspace for this particular setup. 1248 */ 1249 if (invalidate) { 1250 if (disk_part_scan_enabled(disk) || 1251 !(disk->flags & GENHD_FL_REMOVABLE)) 1252 set_capacity(disk, 0); 1253 } else { 1254 if (disk->fops->revalidate_disk) 1255 disk->fops->revalidate_disk(disk); 1256 } 1257 1258 if (get_capacity(disk)) { 1259 ret = blk_add_partitions(disk, bdev); 1260 if (ret == -EAGAIN) 1261 goto rescan; 1262 } else if (invalidate) { 1263 /* 1264 * Tell userspace that the media / partition table may have 1265 * changed. 1266 */ 1267 kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE); 1268 } 1269 1270 return ret; 1271} 1272/* 1273 * Only exported for for loop and dasd for historic reasons. Don't use in new 1274 * code! 1275 */ 1276EXPORT_SYMBOL_GPL(bdev_disk_changed); 1277 1278/* 1279 * bd_mutex locking: 1280 * 1281 * mutex_lock(part->bd_mutex) 1282 * mutex_lock_nested(whole->bd_mutex, 1) 1283 */ 1284static int __blkdev_get(struct block_device *bdev, fmode_t mode) 1285{ 1286 struct gendisk *disk = bdev->bd_disk; 1287 int ret = 0; 1288 1289 if (!bdev->bd_openers) { 1290 if (!bdev_is_partition(bdev)) { 1291 ret = 0; 1292 if (disk->fops->open) 1293 ret = disk->fops->open(bdev, mode); 1294 1295 if (!ret) 1296 set_init_blocksize(bdev); 1297 1298 /* 1299 * If the device is invalidated, rescan partition 1300 * if open succeeded or failed with -ENOMEDIUM. 1301 * The latter is necessary to prevent ghost 1302 * partitions on a removed medium. 1303 */ 1304 if (test_bit(GD_NEED_PART_SCAN, &disk->state) && 1305 (!ret || ret == -ENOMEDIUM)) 1306 bdev_disk_changed(bdev, ret == -ENOMEDIUM); 1307 1308 if (ret) 1309 return ret; 1310 } else { 1311 struct block_device *whole = bdgrab(disk->part0); 1312 1313 mutex_lock_nested(&whole->bd_mutex, 1); 1314 ret = __blkdev_get(whole, mode); 1315 if (ret) { 1316 mutex_unlock(&whole->bd_mutex); 1317 bdput(whole); 1318 return ret; 1319 } 1320 whole->bd_part_count++; 1321 mutex_unlock(&whole->bd_mutex); 1322 1323 if (!(disk->flags & GENHD_FL_UP) || 1324 !bdev_nr_sectors(bdev)) { 1325 __blkdev_put(whole, mode, 1); 1326 bdput(whole); 1327 return -ENXIO; 1328 } 1329 set_init_blocksize(bdev); 1330 } 1331 1332 if (bdev->bd_bdi == &noop_backing_dev_info) 1333 bdev->bd_bdi = bdi_get(disk->queue->backing_dev_info); 1334 } else { 1335 if (!bdev_is_partition(bdev)) { 1336 if (bdev->bd_disk->fops->open) 1337 ret = bdev->bd_disk->fops->open(bdev, mode); 1338 /* the same as first opener case, read comment there */ 1339 if (test_bit(GD_NEED_PART_SCAN, &disk->state) && 1340 (!ret || ret == -ENOMEDIUM)) 1341 bdev_disk_changed(bdev, ret == -ENOMEDIUM); 1342 if (ret) 1343 return ret; 1344 } 1345 } 1346 bdev->bd_openers++; 1347 return 0; 1348} 1349 1350struct block_device *blkdev_get_no_open(dev_t dev) 1351{ 1352 struct block_device *bdev; 1353 struct gendisk *disk; 1354 1355 down_read(&bdev_lookup_sem); 1356 bdev = bdget(dev); 1357 if (!bdev) { 1358 up_read(&bdev_lookup_sem); 1359 blk_request_module(dev); 1360 down_read(&bdev_lookup_sem); 1361 1362 bdev = bdget(dev); 1363 if (!bdev) 1364 goto unlock; 1365 } 1366 1367 disk = bdev->bd_disk; 1368 if (!kobject_get_unless_zero(&disk_to_dev(disk)->kobj)) 1369 goto bdput; 1370 if ((disk->flags & (GENHD_FL_UP | GENHD_FL_HIDDEN)) != GENHD_FL_UP) 1371 goto put_disk; 1372 if (!try_module_get(bdev->bd_disk->fops->owner)) 1373 goto put_disk; 1374 up_read(&bdev_lookup_sem); 1375 return bdev; 1376put_disk: 1377 put_disk(disk); 1378bdput: 1379 bdput(bdev); 1380unlock: 1381 up_read(&bdev_lookup_sem); 1382 return NULL; 1383} 1384 1385void blkdev_put_no_open(struct block_device *bdev) 1386{ 1387 module_put(bdev->bd_disk->fops->owner); 1388 put_disk(bdev->bd_disk); 1389 bdput(bdev); 1390} 1391 1392/** 1393 * blkdev_get_by_dev - open a block device by device number 1394 * @dev: device number of block device to open 1395 * @mode: FMODE_* mask 1396 * @holder: exclusive holder identifier 1397 * 1398 * Open the block device described by device number @dev. If @mode includes 1399 * %FMODE_EXCL, the block device is opened with exclusive access. Specifying 1400 * %FMODE_EXCL with a %NULL @holder is invalid. Exclusive opens may nest for 1401 * the same @holder. 1402 * 1403 * Use this interface ONLY if you really do not have anything better - i.e. when 1404 * you are behind a truly sucky interface and all you are given is a device 1405 * number. Everything else should use blkdev_get_by_path(). 1406 * 1407 * CONTEXT: 1408 * Might sleep. 1409 * 1410 * RETURNS: 1411 * Reference to the block_device on success, ERR_PTR(-errno) on failure. 1412 */ 1413struct block_device *blkdev_get_by_dev(dev_t dev, fmode_t mode, void *holder) 1414{ 1415 bool unblock_events = true; 1416 struct block_device *bdev; 1417 struct gendisk *disk; 1418 int ret; 1419 1420 ret = devcgroup_check_permission(DEVCG_DEV_BLOCK, 1421 MAJOR(dev), MINOR(dev), 1422 ((mode & FMODE_READ) ? DEVCG_ACC_READ : 0) | 1423 ((mode & FMODE_WRITE) ? DEVCG_ACC_WRITE : 0)); 1424 if (ret) 1425 return ERR_PTR(ret); 1426 1427 /* 1428 * If we lost a race with 'disk' being deleted, try again. See md.c. 1429 */ 1430retry: 1431 bdev = blkdev_get_no_open(dev); 1432 if (!bdev) 1433 return ERR_PTR(-ENXIO); 1434 disk = bdev->bd_disk; 1435 1436 if (mode & FMODE_EXCL) { 1437 ret = bd_prepare_to_claim(bdev, holder); 1438 if (ret) 1439 goto put_blkdev; 1440 } 1441 1442 disk_block_events(disk); 1443 1444 mutex_lock(&bdev->bd_mutex); 1445 ret =__blkdev_get(bdev, mode); 1446 if (ret) 1447 goto abort_claiming; 1448 if (mode & FMODE_EXCL) { 1449 bd_finish_claiming(bdev, holder); 1450 1451 /* 1452 * Block event polling for write claims if requested. Any write 1453 * holder makes the write_holder state stick until all are 1454 * released. This is good enough and tracking individual 1455 * writeable reference is too fragile given the way @mode is 1456 * used in blkdev_get/put(). 1457 */ 1458 if ((mode & FMODE_WRITE) && !bdev->bd_write_holder && 1459 (disk->flags & GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE)) { 1460 bdev->bd_write_holder = true; 1461 unblock_events = false; 1462 } 1463 } 1464 mutex_unlock(&bdev->bd_mutex); 1465 1466 if (unblock_events) 1467 disk_unblock_events(disk); 1468 return bdev; 1469 1470abort_claiming: 1471 if (mode & FMODE_EXCL) 1472 bd_abort_claiming(bdev, holder); 1473 mutex_unlock(&bdev->bd_mutex); 1474 disk_unblock_events(disk); 1475put_blkdev: 1476 blkdev_put_no_open(bdev); 1477 if (ret == -ERESTARTSYS) 1478 goto retry; 1479 return ERR_PTR(ret); 1480} 1481EXPORT_SYMBOL(blkdev_get_by_dev); 1482 1483/** 1484 * blkdev_get_by_path - open a block device by name 1485 * @path: path to the block device to open 1486 * @mode: FMODE_* mask 1487 * @holder: exclusive holder identifier 1488 * 1489 * Open the block device described by the device file at @path. If @mode 1490 * includes %FMODE_EXCL, the block device is opened with exclusive access. 1491 * Specifying %FMODE_EXCL with a %NULL @holder is invalid. Exclusive opens may 1492 * nest for the same @holder. 1493 * 1494 * CONTEXT: 1495 * Might sleep. 1496 * 1497 * RETURNS: 1498 * Reference to the block_device on success, ERR_PTR(-errno) on failure. 1499 */ 1500struct block_device *blkdev_get_by_path(const char *path, fmode_t mode, 1501 void *holder) 1502{ 1503 struct block_device *bdev; 1504 dev_t dev; 1505 int error; 1506 1507 error = lookup_bdev(path, &dev); 1508 if (error) 1509 return ERR_PTR(error); 1510 1511 bdev = blkdev_get_by_dev(dev, mode, holder); 1512 if (!IS_ERR(bdev) && (mode & FMODE_WRITE) && bdev_read_only(bdev)) { 1513 blkdev_put(bdev, mode); 1514 return ERR_PTR(-EACCES); 1515 } 1516 1517 return bdev; 1518} 1519EXPORT_SYMBOL(blkdev_get_by_path); 1520 1521static int blkdev_open(struct inode * inode, struct file * filp) 1522{ 1523 struct block_device *bdev; 1524 1525 /* 1526 * Preserve backwards compatibility and allow large file access 1527 * even if userspace doesn't ask for it explicitly. Some mkfs 1528 * binary needs it. We might want to drop this workaround 1529 * during an unstable branch. 1530 */ 1531 filp->f_flags |= O_LARGEFILE; 1532 1533 filp->f_mode |= FMODE_NOWAIT | FMODE_BUF_RASYNC; 1534 1535 if (filp->f_flags & O_NDELAY) 1536 filp->f_mode |= FMODE_NDELAY; 1537 if (filp->f_flags & O_EXCL) 1538 filp->f_mode |= FMODE_EXCL; 1539 if ((filp->f_flags & O_ACCMODE) == 3) 1540 filp->f_mode |= FMODE_WRITE_IOCTL; 1541 1542 bdev = blkdev_get_by_dev(inode->i_rdev, filp->f_mode, filp); 1543 if (IS_ERR(bdev)) 1544 return PTR_ERR(bdev); 1545 filp->f_mapping = bdev->bd_inode->i_mapping; 1546 filp->f_wb_err = filemap_sample_wb_err(filp->f_mapping); 1547 return 0; 1548} 1549 1550static void __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part) 1551{ 1552 struct gendisk *disk = bdev->bd_disk; 1553 struct block_device *victim = NULL; 1554 1555 /* 1556 * Sync early if it looks like we're the last one. If someone else 1557 * opens the block device between now and the decrement of bd_openers 1558 * then we did a sync that we didn't need to, but that's not the end 1559 * of the world and we want to avoid long (could be several minute) 1560 * syncs while holding the mutex. 1561 */ 1562 if (bdev->bd_openers == 1) 1563 sync_blockdev(bdev); 1564 1565 mutex_lock_nested(&bdev->bd_mutex, for_part); 1566 if (for_part) 1567 bdev->bd_part_count--; 1568 1569 if (!--bdev->bd_openers) { 1570 WARN_ON_ONCE(bdev->bd_holders); 1571 sync_blockdev(bdev); 1572 kill_bdev(bdev); 1573 bdev_write_inode(bdev); 1574 if (bdev_is_partition(bdev)) 1575 victim = bdev_whole(bdev); 1576 } 1577 1578 if (!bdev_is_partition(bdev) && disk->fops->release) 1579 disk->fops->release(disk, mode); 1580 mutex_unlock(&bdev->bd_mutex); 1581 if (victim) { 1582 __blkdev_put(victim, mode, 1); 1583 bdput(victim); 1584 } 1585} 1586 1587void blkdev_put(struct block_device *bdev, fmode_t mode) 1588{ 1589 struct gendisk *disk = bdev->bd_disk; 1590 1591 mutex_lock(&bdev->bd_mutex); 1592 1593 if (mode & FMODE_EXCL) { 1594 struct block_device *whole = bdev_whole(bdev); 1595 bool bdev_free; 1596 1597 /* 1598 * Release a claim on the device. The holder fields 1599 * are protected with bdev_lock. bd_mutex is to 1600 * synchronize disk_holder unlinking. 1601 */ 1602 spin_lock(&bdev_lock); 1603 1604 WARN_ON_ONCE(--bdev->bd_holders < 0); 1605 WARN_ON_ONCE(--whole->bd_holders < 0); 1606 1607 if ((bdev_free = !bdev->bd_holders)) 1608 bdev->bd_holder = NULL; 1609 if (!whole->bd_holders) 1610 whole->bd_holder = NULL; 1611 1612 spin_unlock(&bdev_lock); 1613 1614 /* 1615 * If this was the last claim, remove holder link and 1616 * unblock evpoll if it was a write holder. 1617 */ 1618 if (bdev_free && bdev->bd_write_holder) { 1619 disk_unblock_events(disk); 1620 bdev->bd_write_holder = false; 1621 } 1622 } 1623 1624 /* 1625 * Trigger event checking and tell drivers to flush MEDIA_CHANGE 1626 * event. This is to ensure detection of media removal commanded 1627 * from userland - e.g. eject(1). 1628 */ 1629 disk_flush_events(disk, DISK_EVENT_MEDIA_CHANGE); 1630 mutex_unlock(&bdev->bd_mutex); 1631 1632 __blkdev_put(bdev, mode, 0); 1633 blkdev_put_no_open(bdev); 1634} 1635EXPORT_SYMBOL(blkdev_put); 1636 1637static int blkdev_close(struct inode * inode, struct file * filp) 1638{ 1639 struct block_device *bdev = I_BDEV(bdev_file_inode(filp)); 1640 blkdev_put(bdev, filp->f_mode); 1641 return 0; 1642} 1643 1644static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg) 1645{ 1646 struct block_device *bdev = I_BDEV(bdev_file_inode(file)); 1647 fmode_t mode = file->f_mode; 1648 1649 /* 1650 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have 1651 * to updated it before every ioctl. 1652 */ 1653 if (file->f_flags & O_NDELAY) 1654 mode |= FMODE_NDELAY; 1655 else 1656 mode &= ~FMODE_NDELAY; 1657 1658 return blkdev_ioctl(bdev, mode, cmd, arg); 1659} 1660 1661/* 1662 * Write data to the block device. Only intended for the block device itself 1663 * and the raw driver which basically is a fake block device. 1664 * 1665 * Does not take i_mutex for the write and thus is not for general purpose 1666 * use. 1667 */ 1668ssize_t blkdev_write_iter(struct kiocb *iocb, struct iov_iter *from) 1669{ 1670 struct file *file = iocb->ki_filp; 1671 struct inode *bd_inode = bdev_file_inode(file); 1672 loff_t size = i_size_read(bd_inode); 1673 struct blk_plug plug; 1674 ssize_t ret; 1675 1676 if (bdev_read_only(I_BDEV(bd_inode))) 1677 return -EPERM; 1678 1679 if (IS_SWAPFILE(bd_inode) && !is_hibernate_resume_dev(bd_inode->i_rdev)) 1680 return -ETXTBSY; 1681 1682 if (!iov_iter_count(from)) 1683 return 0; 1684 1685 if (iocb->ki_pos >= size) 1686 return -ENOSPC; 1687 1688 if ((iocb->ki_flags & (IOCB_NOWAIT | IOCB_DIRECT)) == IOCB_NOWAIT) 1689 return -EOPNOTSUPP; 1690 1691 iov_iter_truncate(from, size - iocb->ki_pos); 1692 1693 blk_start_plug(&plug); 1694 ret = __generic_file_write_iter(iocb, from); 1695 if (ret > 0) 1696 ret = generic_write_sync(iocb, ret); 1697 blk_finish_plug(&plug); 1698 return ret; 1699} 1700EXPORT_SYMBOL_GPL(blkdev_write_iter); 1701 1702ssize_t blkdev_read_iter(struct kiocb *iocb, struct iov_iter *to) 1703{ 1704 struct file *file = iocb->ki_filp; 1705 struct inode *bd_inode = bdev_file_inode(file); 1706 loff_t size = i_size_read(bd_inode); 1707 loff_t pos = iocb->ki_pos; 1708 1709 if (pos >= size) 1710 return 0; 1711 1712 size -= pos; 1713 iov_iter_truncate(to, size); 1714 return generic_file_read_iter(iocb, to); 1715} 1716EXPORT_SYMBOL_GPL(blkdev_read_iter); 1717 1718/* 1719 * Try to release a page associated with block device when the system 1720 * is under memory pressure. 1721 */ 1722static int blkdev_releasepage(struct page *page, gfp_t wait) 1723{ 1724 struct super_block *super = BDEV_I(page->mapping->host)->bdev.bd_super; 1725 1726 if (super && super->s_op->bdev_try_to_free_page) 1727 return super->s_op->bdev_try_to_free_page(super, page, wait); 1728 1729 return try_to_free_buffers(page); 1730} 1731 1732static int blkdev_writepages(struct address_space *mapping, 1733 struct writeback_control *wbc) 1734{ 1735 return generic_writepages(mapping, wbc); 1736} 1737 1738static const struct address_space_operations def_blk_aops = { 1739 .readpage = blkdev_readpage, 1740 .readahead = blkdev_readahead, 1741 .writepage = blkdev_writepage, 1742 .write_begin = blkdev_write_begin, 1743 .write_end = blkdev_write_end, 1744 .writepages = blkdev_writepages, 1745 .releasepage = blkdev_releasepage, 1746 .direct_IO = blkdev_direct_IO, 1747 .migratepage = buffer_migrate_page_norefs, 1748 .is_dirty_writeback = buffer_check_dirty_writeback, 1749}; 1750 1751#define BLKDEV_FALLOC_FL_SUPPORTED \ 1752 (FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | \ 1753 FALLOC_FL_ZERO_RANGE | FALLOC_FL_NO_HIDE_STALE) 1754 1755static long blkdev_fallocate(struct file *file, int mode, loff_t start, 1756 loff_t len) 1757{ 1758 struct block_device *bdev = I_BDEV(bdev_file_inode(file)); 1759 loff_t end = start + len - 1; 1760 loff_t isize; 1761 int error; 1762 1763 /* Fail if we don't recognize the flags. */ 1764 if (mode & ~BLKDEV_FALLOC_FL_SUPPORTED) 1765 return -EOPNOTSUPP; 1766 1767 /* Don't go off the end of the device. */ 1768 isize = i_size_read(bdev->bd_inode); 1769 if (start >= isize) 1770 return -EINVAL; 1771 if (end >= isize) { 1772 if (mode & FALLOC_FL_KEEP_SIZE) { 1773 len = isize - start; 1774 end = start + len - 1; 1775 } else 1776 return -EINVAL; 1777 } 1778 1779 /* 1780 * Don't allow IO that isn't aligned to logical block size. 1781 */ 1782 if ((start | len) & (bdev_logical_block_size(bdev) - 1)) 1783 return -EINVAL; 1784 1785 /* Invalidate the page cache, including dirty pages. */ 1786 error = truncate_bdev_range(bdev, file->f_mode, start, end); 1787 if (error) 1788 return error; 1789 1790 switch (mode) { 1791 case FALLOC_FL_ZERO_RANGE: 1792 case FALLOC_FL_ZERO_RANGE | FALLOC_FL_KEEP_SIZE: 1793 error = blkdev_issue_zeroout(bdev, start >> 9, len >> 9, 1794 GFP_KERNEL, BLKDEV_ZERO_NOUNMAP); 1795 break; 1796 case FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE: 1797 error = blkdev_issue_zeroout(bdev, start >> 9, len >> 9, 1798 GFP_KERNEL, BLKDEV_ZERO_NOFALLBACK); 1799 break; 1800 case FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE | FALLOC_FL_NO_HIDE_STALE: 1801 error = blkdev_issue_discard(bdev, start >> 9, len >> 9, 1802 GFP_KERNEL, 0); 1803 break; 1804 default: 1805 return -EOPNOTSUPP; 1806 } 1807 if (error) 1808 return error; 1809 1810 /* 1811 * Invalidate again; if someone wandered in and dirtied a page, 1812 * the caller will be given -EBUSY. The third argument is 1813 * inclusive, so the rounding here is safe. 1814 */ 1815 return invalidate_inode_pages2_range(bdev->bd_inode->i_mapping, 1816 start >> PAGE_SHIFT, 1817 end >> PAGE_SHIFT); 1818} 1819 1820const struct file_operations def_blk_fops = { 1821 .open = blkdev_open, 1822 .release = blkdev_close, 1823 .llseek = block_llseek, 1824 .read_iter = blkdev_read_iter, 1825 .write_iter = blkdev_write_iter, 1826 .iopoll = blkdev_iopoll, 1827 .mmap = generic_file_mmap, 1828 .fsync = blkdev_fsync, 1829 .unlocked_ioctl = block_ioctl, 1830#ifdef CONFIG_COMPAT 1831 .compat_ioctl = compat_blkdev_ioctl, 1832#endif 1833 .splice_read = generic_file_splice_read, 1834 .splice_write = iter_file_splice_write, 1835 .fallocate = blkdev_fallocate, 1836}; 1837 1838/** 1839 * lookup_bdev - lookup a struct block_device by name 1840 * @pathname: special file representing the block device 1841 * @dev: return value of the block device's dev_t 1842 * 1843 * Get a reference to the blockdevice at @pathname in the current 1844 * namespace if possible and return it. Return ERR_PTR(error) 1845 * otherwise. 1846 */ 1847int lookup_bdev(const char *pathname, dev_t *dev) 1848{ 1849 struct inode *inode; 1850 struct path path; 1851 int error; 1852 1853 if (!pathname || !*pathname) 1854 return -EINVAL; 1855 1856 error = kern_path(pathname, LOOKUP_FOLLOW, &path); 1857 if (error) 1858 return error; 1859 1860 inode = d_backing_inode(path.dentry); 1861 error = -ENOTBLK; 1862 if (!S_ISBLK(inode->i_mode)) 1863 goto out_path_put; 1864 error = -EACCES; 1865 if (!may_open_dev(&path)) 1866 goto out_path_put; 1867 1868 *dev = inode->i_rdev; 1869 error = 0; 1870out_path_put: 1871 path_put(&path); 1872 return error; 1873} 1874EXPORT_SYMBOL(lookup_bdev); 1875 1876int __invalidate_device(struct block_device *bdev, bool kill_dirty) 1877{ 1878 struct super_block *sb = get_super(bdev); 1879 int res = 0; 1880 1881 if (sb) { 1882 /* 1883 * no need to lock the super, get_super holds the 1884 * read mutex so the filesystem cannot go away 1885 * under us (->put_super runs with the write lock 1886 * hold). 1887 */ 1888 shrink_dcache_sb(sb); 1889 res = invalidate_inodes(sb, kill_dirty); 1890 drop_super(sb); 1891 } 1892 invalidate_bdev(bdev); 1893 return res; 1894} 1895EXPORT_SYMBOL(__invalidate_device); 1896 1897void iterate_bdevs(void (*func)(struct block_device *, void *), void *arg) 1898{ 1899 struct inode *inode, *old_inode = NULL; 1900 1901 spin_lock(&blockdev_superblock->s_inode_list_lock); 1902 list_for_each_entry(inode, &blockdev_superblock->s_inodes, i_sb_list) { 1903 struct address_space *mapping = inode->i_mapping; 1904 struct block_device *bdev; 1905 1906 spin_lock(&inode->i_lock); 1907 if (inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW) || 1908 mapping->nrpages == 0) { 1909 spin_unlock(&inode->i_lock); 1910 continue; 1911 } 1912 __iget(inode); 1913 spin_unlock(&inode->i_lock); 1914 spin_unlock(&blockdev_superblock->s_inode_list_lock); 1915 /* 1916 * We hold a reference to 'inode' so it couldn't have been 1917 * removed from s_inodes list while we dropped the 1918 * s_inode_list_lock We cannot iput the inode now as we can 1919 * be holding the last reference and we cannot iput it under 1920 * s_inode_list_lock. So we keep the reference and iput it 1921 * later. 1922 */ 1923 iput(old_inode); 1924 old_inode = inode; 1925 bdev = I_BDEV(inode); 1926 1927 mutex_lock(&bdev->bd_mutex); 1928 if (bdev->bd_openers) 1929 func(bdev, arg); 1930 mutex_unlock(&bdev->bd_mutex); 1931 1932 spin_lock(&blockdev_superblock->s_inode_list_lock); 1933 } 1934 spin_unlock(&blockdev_superblock->s_inode_list_lock); 1935 iput(old_inode); 1936}