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/slab.h> 11 #include <linux/kmod.h> 12 #include <linux/major.h> 13 #include <linux/device_cgroup.h> 14 #include <linux/blkdev.h> 15 #include <linux/blk-integrity.h> 16 #include <linux/backing-dev.h> 17 #include <linux/module.h> 18 #include <linux/blkpg.h> 19 #include <linux/magic.h> 20 #include <linux/buffer_head.h> 21 #include <linux/swap.h> 22 #include <linux/writeback.h> 23 #include <linux/mount.h> 24 #include <linux/pseudo_fs.h> 25 #include <linux/uio.h> 26 #include <linux/namei.h> 27 #include <linux/security.h> 28 #include <linux/part_stat.h> 29 #include <linux/uaccess.h> 30 #include <linux/stat.h> 31 #include "../fs/internal.h" 32 #include "blk.h" 33 34 /* Should we allow writing to mounted block devices? */ 35 static bool bdev_allow_write_mounted = IS_ENABLED(CONFIG_BLK_DEV_WRITE_MOUNTED); 36 37 struct bdev_inode { 38 struct block_device bdev; 39 struct inode vfs_inode; 40 }; 41 42 static inline struct bdev_inode *BDEV_I(struct inode *inode) 43 { 44 return container_of(inode, struct bdev_inode, vfs_inode); 45 } 46 47 static inline struct inode *BD_INODE(struct block_device *bdev) 48 { 49 return &container_of(bdev, struct bdev_inode, bdev)->vfs_inode; 50 } 51 52 struct block_device *I_BDEV(struct inode *inode) 53 { 54 return &BDEV_I(inode)->bdev; 55 } 56 EXPORT_SYMBOL(I_BDEV); 57 58 struct block_device *file_bdev(struct file *bdev_file) 59 { 60 return I_BDEV(bdev_file->f_mapping->host); 61 } 62 EXPORT_SYMBOL(file_bdev); 63 64 static void bdev_write_inode(struct block_device *bdev) 65 { 66 struct inode *inode = BD_INODE(bdev); 67 int ret; 68 69 spin_lock(&inode->i_lock); 70 while (inode_state_read(inode) & I_DIRTY) { 71 spin_unlock(&inode->i_lock); 72 ret = write_inode_now(inode, true); 73 if (ret) 74 pr_warn_ratelimited( 75 "VFS: Dirty inode writeback failed for block device %pg (err=%d).\n", 76 bdev, ret); 77 spin_lock(&inode->i_lock); 78 } 79 spin_unlock(&inode->i_lock); 80 } 81 82 /* Kill _all_ buffers and pagecache , dirty or not.. */ 83 static void kill_bdev(struct block_device *bdev) 84 { 85 struct address_space *mapping = bdev->bd_mapping; 86 87 if (mapping_empty(mapping)) 88 return; 89 90 invalidate_bh_lrus(); 91 truncate_inode_pages(mapping, 0); 92 } 93 94 /* Invalidate clean unused buffers and pagecache. */ 95 void invalidate_bdev(struct block_device *bdev) 96 { 97 struct address_space *mapping = bdev->bd_mapping; 98 99 if (mapping->nrpages) { 100 invalidate_bh_lrus(); 101 lru_add_drain_all(); /* make sure all lru add caches are flushed */ 102 invalidate_mapping_pages(mapping, 0, -1); 103 } 104 } 105 EXPORT_SYMBOL(invalidate_bdev); 106 107 /* 108 * Drop all buffers & page cache for given bdev range. This function bails 109 * with error if bdev has other exclusive owner (such as filesystem). 110 */ 111 int truncate_bdev_range(struct block_device *bdev, blk_mode_t mode, 112 loff_t lstart, loff_t lend) 113 { 114 /* 115 * If we don't hold exclusive handle for the device, upgrade to it 116 * while we discard the buffer cache to avoid discarding buffers 117 * under live filesystem. 118 */ 119 if (!(mode & BLK_OPEN_EXCL)) { 120 int err = bd_prepare_to_claim(bdev, truncate_bdev_range, NULL); 121 if (err) 122 goto invalidate; 123 } 124 125 truncate_inode_pages_range(bdev->bd_mapping, lstart, lend); 126 if (!(mode & BLK_OPEN_EXCL)) 127 bd_abort_claiming(bdev, truncate_bdev_range); 128 return 0; 129 130 invalidate: 131 /* 132 * Someone else has handle exclusively open. Try invalidating instead. 133 * The 'end' argument is inclusive so the rounding is safe. 134 */ 135 return invalidate_inode_pages2_range(bdev->bd_mapping, 136 lstart >> PAGE_SHIFT, 137 lend >> PAGE_SHIFT); 138 } 139 140 static void set_init_blocksize(struct block_device *bdev) 141 { 142 unsigned int bsize = bdev_logical_block_size(bdev); 143 loff_t size = i_size_read(BD_INODE(bdev)); 144 145 while (bsize < PAGE_SIZE) { 146 if (size & bsize) 147 break; 148 bsize <<= 1; 149 } 150 BD_INODE(bdev)->i_blkbits = blksize_bits(bsize); 151 mapping_set_folio_min_order(BD_INODE(bdev)->i_mapping, 152 get_order(bsize)); 153 } 154 155 /** 156 * bdev_validate_blocksize - check that this block size is acceptable 157 * @bdev: blockdevice to check 158 * @block_size: block size to check 159 * 160 * For block device users that do not use buffer heads or the block device 161 * page cache, make sure that this block size can be used with the device. 162 * 163 * Return: On success zero is returned, negative error code on failure. 164 */ 165 int bdev_validate_blocksize(struct block_device *bdev, int block_size) 166 { 167 if (blk_validate_block_size(block_size)) 168 return -EINVAL; 169 170 /* Size cannot be smaller than the size supported by the device */ 171 if (block_size < bdev_logical_block_size(bdev)) 172 return -EINVAL; 173 174 return 0; 175 } 176 EXPORT_SYMBOL_GPL(bdev_validate_blocksize); 177 178 int set_blocksize(struct file *file, int size) 179 { 180 struct inode *inode = file->f_mapping->host; 181 struct block_device *bdev = I_BDEV(inode); 182 int ret; 183 184 ret = bdev_validate_blocksize(bdev, size); 185 if (ret) 186 return ret; 187 188 if (!file->private_data) 189 return -EINVAL; 190 191 /* Don't change the size if it is same as current */ 192 if (inode->i_blkbits != blksize_bits(size)) { 193 /* 194 * Flush and truncate the pagecache before we reconfigure the 195 * mapping geometry because folio sizes are variable now. If a 196 * reader has already allocated a folio whose size is smaller 197 * than the new min_order but invokes readahead after the new 198 * min_order becomes visible, readahead will think there are 199 * "zero" blocks per folio and crash. Take the inode and 200 * invalidation locks to avoid racing with 201 * read/write/fallocate. 202 */ 203 inode_lock(inode); 204 filemap_invalidate_lock(inode->i_mapping); 205 206 sync_blockdev(bdev); 207 kill_bdev(bdev); 208 209 inode->i_blkbits = blksize_bits(size); 210 mapping_set_folio_min_order(inode->i_mapping, get_order(size)); 211 filemap_invalidate_unlock(inode->i_mapping); 212 inode_unlock(inode); 213 } 214 return 0; 215 } 216 217 EXPORT_SYMBOL(set_blocksize); 218 219 static int sb_validate_large_blocksize(struct super_block *sb, int size) 220 { 221 const char *err_str = NULL; 222 223 if (!(sb->s_type->fs_flags & FS_LBS)) 224 err_str = "not supported by filesystem"; 225 else if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) 226 err_str = "is only supported with CONFIG_TRANSPARENT_HUGEPAGE"; 227 228 if (!err_str) 229 return 0; 230 231 pr_warn_ratelimited("%s: block size(%d) > page size(%lu) %s\n", 232 sb->s_type->name, size, PAGE_SIZE, err_str); 233 return -EINVAL; 234 } 235 236 int sb_set_blocksize(struct super_block *sb, int size) 237 { 238 if (size > PAGE_SIZE && sb_validate_large_blocksize(sb, size)) 239 return 0; 240 if (set_blocksize(sb->s_bdev_file, size)) 241 return 0; 242 /* If we get here, we know size is validated */ 243 sb->s_blocksize = size; 244 sb->s_blocksize_bits = blksize_bits(size); 245 return sb->s_blocksize; 246 } 247 248 EXPORT_SYMBOL(sb_set_blocksize); 249 250 int __must_check sb_min_blocksize(struct super_block *sb, int size) 251 { 252 int minsize = bdev_logical_block_size(sb->s_bdev); 253 if (size < minsize) 254 size = minsize; 255 return sb_set_blocksize(sb, size); 256 } 257 258 EXPORT_SYMBOL(sb_min_blocksize); 259 260 int sync_blockdev_nowait(struct block_device *bdev) 261 { 262 if (!bdev) 263 return 0; 264 return filemap_flush(bdev->bd_mapping); 265 } 266 EXPORT_SYMBOL_GPL(sync_blockdev_nowait); 267 268 /* 269 * Write out and wait upon all the dirty data associated with a block 270 * device via its mapping. Does not take the superblock lock. 271 */ 272 int sync_blockdev(struct block_device *bdev) 273 { 274 if (!bdev) 275 return 0; 276 return filemap_write_and_wait(bdev->bd_mapping); 277 } 278 EXPORT_SYMBOL(sync_blockdev); 279 280 int sync_blockdev_range(struct block_device *bdev, loff_t lstart, loff_t lend) 281 { 282 return filemap_write_and_wait_range(bdev->bd_mapping, 283 lstart, lend); 284 } 285 EXPORT_SYMBOL(sync_blockdev_range); 286 287 /** 288 * bdev_freeze - lock a filesystem and force it into a consistent state 289 * @bdev: blockdevice to lock 290 * 291 * If a superblock is found on this device, we take the s_umount semaphore 292 * on it to make sure nobody unmounts until the snapshot creation is done. 293 * The reference counter (bd_fsfreeze_count) guarantees that only the last 294 * unfreeze process can unfreeze the frozen filesystem actually when multiple 295 * freeze requests arrive simultaneously. It counts up in bdev_freeze() and 296 * count down in bdev_thaw(). When it becomes 0, thaw_bdev() will unfreeze 297 * actually. 298 * 299 * Return: On success zero is returned, negative error code on failure. 300 */ 301 int bdev_freeze(struct block_device *bdev) 302 { 303 int error = 0; 304 305 mutex_lock(&bdev->bd_fsfreeze_mutex); 306 307 /* A device being removed from its filesystem refuses freezes. */ 308 if (!atomic_inc_unless_negative(&bdev->bd_fsfreeze_count)) { 309 mutex_unlock(&bdev->bd_fsfreeze_mutex); 310 return -EBUSY; 311 } 312 if (atomic_read(&bdev->bd_fsfreeze_count) > 1) { 313 mutex_unlock(&bdev->bd_fsfreeze_mutex); 314 return 0; 315 } 316 317 mutex_lock(&bdev->bd_holder_lock); 318 if (bdev->bd_holder_ops && bdev->bd_holder_ops->freeze) { 319 error = bdev->bd_holder_ops->freeze(bdev); 320 lockdep_assert_not_held(&bdev->bd_holder_lock); 321 } else { 322 mutex_unlock(&bdev->bd_holder_lock); 323 error = sync_blockdev(bdev); 324 } 325 326 if (error) 327 atomic_dec(&bdev->bd_fsfreeze_count); 328 329 mutex_unlock(&bdev->bd_fsfreeze_mutex); 330 return error; 331 } 332 EXPORT_SYMBOL(bdev_freeze); 333 334 /** 335 * bdev_thaw - unlock filesystem 336 * @bdev: blockdevice to unlock 337 * 338 * Unlocks the filesystem and marks it writeable again after bdev_freeze(). 339 * 340 * Return: On success zero is returned, negative error code on failure. 341 */ 342 int bdev_thaw(struct block_device *bdev) 343 { 344 int error = -EINVAL, nr_freeze; 345 346 mutex_lock(&bdev->bd_fsfreeze_mutex); 347 348 /* <= 0: not frozen (0) or a freeze deny is held (< 0); leave it. */ 349 nr_freeze = atomic_read(&bdev->bd_fsfreeze_count); 350 if (nr_freeze <= 0) 351 goto out; 352 353 error = 0; 354 if (nr_freeze > 1) { 355 atomic_dec(&bdev->bd_fsfreeze_count); 356 goto out; 357 } 358 359 /* Keep the count positive across the thaw so a deny is refused. */ 360 mutex_lock(&bdev->bd_holder_lock); 361 if (bdev->bd_holder_ops && bdev->bd_holder_ops->thaw) { 362 error = bdev->bd_holder_ops->thaw(bdev); 363 lockdep_assert_not_held(&bdev->bd_holder_lock); 364 } else { 365 mutex_unlock(&bdev->bd_holder_lock); 366 } 367 368 if (!error) 369 atomic_dec(&bdev->bd_fsfreeze_count); 370 out: 371 mutex_unlock(&bdev->bd_fsfreeze_mutex); 372 return error; 373 } 374 EXPORT_SYMBOL(bdev_thaw); 375 376 /** 377 * bdev_deny_freeze - make a block device unfreezable 378 * @bdev: block device 379 * 380 * Reserve @bdev against bdev_freeze() the way deny_write_access() reserves a 381 * file against writers. bd_fsfreeze_count is sign-encoded: > 0 counts active 382 * freezes, < 0 counts deniers, so a deny succeeds only while no freeze is in 383 * progress. While held, bdev_freeze() returns -EBUSY. Pair with 384 * bdev_allow_freeze(). 385 * 386 * A filesystem removing, adding or replacing a member device denies freezes on 387 * it for the duration, so a claim a freeze walk might act on is never torn down 388 * behind the freezer's back. The deny is device-scoped, not (device, 389 * superblock)-scoped: a device shared by several superblocks is refused for all 390 * of them. No in-tree filesystem removes a shared claim from a live superblock. 391 * 392 * Return: 0, or -EBUSY if the device is currently frozen. 393 */ 394 int bdev_deny_freeze(struct block_device *bdev) 395 { 396 return atomic_dec_unless_positive(&bdev->bd_fsfreeze_count) ? 0 : -EBUSY; 397 } 398 EXPORT_SYMBOL_GPL(bdev_deny_freeze); 399 400 /** 401 * bdev_allow_freeze - allow freezing a block device again 402 * @bdev: block device 403 * 404 * Undo one bdev_deny_freeze(). 405 */ 406 void bdev_allow_freeze(struct block_device *bdev) 407 { 408 /* A deny must be held, i.e. the count must be negative. */ 409 WARN_ON_ONCE(atomic_read(&bdev->bd_fsfreeze_count) >= 0); 410 atomic_inc(&bdev->bd_fsfreeze_count); 411 } 412 EXPORT_SYMBOL_GPL(bdev_allow_freeze); 413 414 /* 415 * pseudo-fs 416 */ 417 418 static __cacheline_aligned_in_smp DEFINE_MUTEX(bdev_lock); 419 static struct kmem_cache *bdev_cachep __ro_after_init; 420 421 static struct inode *bdev_alloc_inode(struct super_block *sb) 422 { 423 struct bdev_inode *ei = alloc_inode_sb(sb, bdev_cachep, GFP_KERNEL); 424 425 if (!ei) 426 return NULL; 427 memset(&ei->bdev, 0, sizeof(ei->bdev)); 428 429 if (security_bdev_alloc(&ei->bdev)) { 430 kmem_cache_free(bdev_cachep, ei); 431 return NULL; 432 } 433 return &ei->vfs_inode; 434 } 435 436 static void bdev_free_inode(struct inode *inode) 437 { 438 struct block_device *bdev = I_BDEV(inode); 439 440 free_percpu(bdev->bd_stats); 441 kfree(bdev->bd_meta_info); 442 security_bdev_free(bdev); 443 444 if (!bdev_is_partition(bdev)) { 445 if (bdev->bd_disk && bdev->bd_disk->bdi) 446 bdi_put(bdev->bd_disk->bdi); 447 kfree(bdev->bd_disk); 448 } 449 450 if (MAJOR(bdev->bd_dev) == BLOCK_EXT_MAJOR) 451 blk_free_ext_minor(MINOR(bdev->bd_dev)); 452 453 kmem_cache_free(bdev_cachep, BDEV_I(inode)); 454 } 455 456 static void init_once(void *data) 457 { 458 struct bdev_inode *ei = data; 459 460 inode_init_once(&ei->vfs_inode); 461 } 462 463 static const struct super_operations bdev_sops = { 464 .statfs = simple_statfs, 465 .alloc_inode = bdev_alloc_inode, 466 .free_inode = bdev_free_inode, 467 .drop_inode = inode_just_drop, 468 }; 469 470 static int bd_init_fs_context(struct fs_context *fc) 471 { 472 struct pseudo_fs_context *ctx = init_pseudo(fc, BDEVFS_MAGIC); 473 if (!ctx) 474 return -ENOMEM; 475 fc->s_iflags |= SB_I_CGROUPWB; 476 ctx->ops = &bdev_sops; 477 return 0; 478 } 479 480 static struct file_system_type bd_type = { 481 .name = "bdev", 482 .init_fs_context = bd_init_fs_context, 483 .kill_sb = kill_anon_super, 484 }; 485 486 struct super_block *blockdev_superblock __ro_after_init; 487 static struct vfsmount *blockdev_mnt __ro_after_init; 488 EXPORT_SYMBOL_GPL(blockdev_superblock); 489 490 void __init bdev_cache_init(void) 491 { 492 bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode), 493 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT| 494 SLAB_ACCOUNT|SLAB_PANIC), 495 init_once); 496 blockdev_mnt = kern_mount(&bd_type); 497 if (IS_ERR(blockdev_mnt)) 498 panic("Cannot create bdev pseudo-fs"); 499 blockdev_superblock = blockdev_mnt->mnt_sb; /* For writeback */ 500 } 501 502 struct block_device *bdev_alloc(struct gendisk *disk, u8 partno) 503 { 504 struct block_device *bdev; 505 struct inode *inode; 506 507 inode = new_inode(blockdev_superblock); 508 if (!inode) 509 return NULL; 510 inode->i_mode = S_IFBLK; 511 inode->i_rdev = 0; 512 inode->i_data.a_ops = &def_blk_aops; 513 mapping_set_gfp_mask(&inode->i_data, GFP_USER); 514 515 bdev = I_BDEV(inode); 516 mutex_init(&bdev->bd_fsfreeze_mutex); 517 spin_lock_init(&bdev->bd_size_lock); 518 mutex_init(&bdev->bd_holder_lock); 519 atomic_set(&bdev->__bd_flags, partno); 520 bdev->bd_mapping = &inode->i_data; 521 bdev->bd_queue = disk->queue; 522 if (partno && bdev_test_flag(disk->part0, BD_HAS_SUBMIT_BIO)) 523 bdev_set_flag(bdev, BD_HAS_SUBMIT_BIO); 524 bdev->bd_stats = alloc_percpu(struct disk_stats); 525 if (!bdev->bd_stats) { 526 iput(inode); 527 return NULL; 528 } 529 bdev->bd_disk = disk; 530 return bdev; 531 } 532 533 void bdev_set_nr_sectors(struct block_device *bdev, sector_t sectors) 534 { 535 spin_lock(&bdev->bd_size_lock); 536 i_size_write(BD_INODE(bdev), (loff_t)sectors << SECTOR_SHIFT); 537 bdev->bd_nr_sectors = sectors; 538 spin_unlock(&bdev->bd_size_lock); 539 } 540 541 void bdev_add(struct block_device *bdev, dev_t dev) 542 { 543 struct inode *inode = BD_INODE(bdev); 544 if (bdev_stable_writes(bdev)) 545 mapping_set_stable_writes(bdev->bd_mapping); 546 bdev->bd_dev = dev; 547 inode->i_rdev = dev; 548 inode->i_ino = dev; 549 insert_inode_hash(inode); 550 } 551 552 void bdev_unhash(struct block_device *bdev) 553 { 554 remove_inode_hash(BD_INODE(bdev)); 555 } 556 557 void bdev_drop(struct block_device *bdev) 558 { 559 iput(BD_INODE(bdev)); 560 } 561 562 long nr_blockdev_pages(void) 563 { 564 struct inode *inode; 565 long ret = 0; 566 567 spin_lock(&blockdev_superblock->s_inode_list_lock); 568 list_for_each_entry(inode, &blockdev_superblock->s_inodes, i_sb_list) 569 ret += inode->i_mapping->nrpages; 570 spin_unlock(&blockdev_superblock->s_inode_list_lock); 571 572 return ret; 573 } 574 575 /** 576 * bd_may_claim - test whether a block device can be claimed 577 * @bdev: block device of interest 578 * @holder: holder trying to claim @bdev 579 * @hops: holder ops 580 * 581 * Test whether @bdev can be claimed by @holder. 582 * 583 * RETURNS: 584 * %true if @bdev can be claimed, %false otherwise. 585 */ 586 static bool bd_may_claim(struct block_device *bdev, void *holder, 587 const struct blk_holder_ops *hops) 588 { 589 struct block_device *whole = bdev_whole(bdev); 590 591 lockdep_assert_held(&bdev_lock); 592 593 if (bdev->bd_holder) { 594 /* 595 * The same holder can always re-claim. 596 */ 597 if (bdev->bd_holder == holder) { 598 if (WARN_ON_ONCE(bdev->bd_holder_ops != hops)) 599 return false; 600 return true; 601 } 602 return false; 603 } 604 605 /* 606 * If the whole devices holder is set to bd_may_claim, a partition on 607 * the device is claimed, but not the whole device. 608 */ 609 if (whole != bdev && 610 whole->bd_holder && whole->bd_holder != bd_may_claim) 611 return false; 612 return true; 613 } 614 615 /** 616 * bd_prepare_to_claim - claim a block device 617 * @bdev: block device of interest 618 * @holder: holder trying to claim @bdev 619 * @hops: holder ops. 620 * 621 * Claim @bdev. This function fails if @bdev is already claimed by another 622 * holder and waits if another claiming is in progress. return, the caller 623 * has ownership of bd_claiming and bd_holder[s]. 624 * 625 * RETURNS: 626 * 0 if @bdev can be claimed, -EBUSY otherwise. 627 */ 628 int bd_prepare_to_claim(struct block_device *bdev, void *holder, 629 const struct blk_holder_ops *hops) 630 { 631 struct block_device *whole = bdev_whole(bdev); 632 633 if (WARN_ON_ONCE(!holder)) 634 return -EINVAL; 635 retry: 636 mutex_lock(&bdev_lock); 637 /* if someone else claimed, fail */ 638 if (!bd_may_claim(bdev, holder, hops)) { 639 mutex_unlock(&bdev_lock); 640 return -EBUSY; 641 } 642 643 /* if claiming is already in progress, wait for it to finish */ 644 if (whole->bd_claiming) { 645 wait_queue_head_t *wq = __var_waitqueue(&whole->bd_claiming); 646 DEFINE_WAIT(wait); 647 648 prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE); 649 mutex_unlock(&bdev_lock); 650 schedule(); 651 finish_wait(wq, &wait); 652 goto retry; 653 } 654 655 /* yay, all mine */ 656 whole->bd_claiming = holder; 657 mutex_unlock(&bdev_lock); 658 return 0; 659 } 660 EXPORT_SYMBOL_GPL(bd_prepare_to_claim); /* only for the loop driver */ 661 662 static void bd_clear_claiming(struct block_device *whole, void *holder) 663 { 664 lockdep_assert_held(&bdev_lock); 665 /* tell others that we're done */ 666 BUG_ON(whole->bd_claiming != holder); 667 whole->bd_claiming = NULL; 668 wake_up_var(&whole->bd_claiming); 669 } 670 671 /** 672 * bd_finish_claiming - finish claiming of a block device 673 * @bdev: block device of interest 674 * @holder: holder that has claimed @bdev 675 * @hops: block device holder operations 676 * 677 * Finish exclusive open of a block device. Mark the device as exlusively 678 * open by the holder and wake up all waiters for exclusive open to finish. 679 */ 680 static void bd_finish_claiming(struct block_device *bdev, void *holder, 681 const struct blk_holder_ops *hops) 682 { 683 struct block_device *whole = bdev_whole(bdev); 684 685 mutex_lock(&bdev_lock); 686 BUG_ON(!bd_may_claim(bdev, holder, hops)); 687 /* 688 * Note that for a whole device bd_holders will be incremented twice, 689 * and bd_holder will be set to bd_may_claim before being set to holder 690 */ 691 whole->bd_holders++; 692 whole->bd_holder = bd_may_claim; 693 bdev->bd_holders++; 694 mutex_lock(&bdev->bd_holder_lock); 695 bdev->bd_holder = holder; 696 bdev->bd_holder_ops = hops; 697 mutex_unlock(&bdev->bd_holder_lock); 698 bd_clear_claiming(whole, holder); 699 mutex_unlock(&bdev_lock); 700 } 701 702 /** 703 * bd_abort_claiming - abort claiming of a block device 704 * @bdev: block device of interest 705 * @holder: holder that has claimed @bdev 706 * 707 * Abort claiming of a block device when the exclusive open failed. This can be 708 * also used when exclusive open is not actually desired and we just needed 709 * to block other exclusive openers for a while. 710 */ 711 void bd_abort_claiming(struct block_device *bdev, void *holder) 712 { 713 mutex_lock(&bdev_lock); 714 bd_clear_claiming(bdev_whole(bdev), holder); 715 mutex_unlock(&bdev_lock); 716 } 717 EXPORT_SYMBOL(bd_abort_claiming); 718 719 static void bd_end_claim(struct block_device *bdev, void *holder) 720 { 721 struct block_device *whole = bdev_whole(bdev); 722 bool unblock = false; 723 724 /* 725 * Release a claim on the device. The holder fields are protected with 726 * bdev_lock. open_mutex is used to synchronize disk_holder unlinking. 727 */ 728 mutex_lock(&bdev_lock); 729 WARN_ON_ONCE(bdev->bd_holder != holder); 730 WARN_ON_ONCE(--bdev->bd_holders < 0); 731 WARN_ON_ONCE(--whole->bd_holders < 0); 732 if (!bdev->bd_holders) { 733 mutex_lock(&bdev->bd_holder_lock); 734 bdev->bd_holder = NULL; 735 bdev->bd_holder_ops = NULL; 736 mutex_unlock(&bdev->bd_holder_lock); 737 if (bdev_test_flag(bdev, BD_WRITE_HOLDER)) 738 unblock = true; 739 } 740 if (!whole->bd_holders) 741 whole->bd_holder = NULL; 742 mutex_unlock(&bdev_lock); 743 744 /* 745 * If this was the last claim, remove holder link and unblock evpoll if 746 * it was a write holder. 747 */ 748 if (unblock) { 749 disk_unblock_events(bdev->bd_disk); 750 bdev_clear_flag(bdev, BD_WRITE_HOLDER); 751 } 752 } 753 754 static void blkdev_flush_mapping(struct block_device *bdev) 755 { 756 WARN_ON_ONCE(bdev->bd_holders); 757 sync_blockdev(bdev); 758 kill_bdev(bdev); 759 bdev_write_inode(bdev); 760 } 761 762 static void blkdev_put_whole(struct block_device *bdev) 763 { 764 if (atomic_dec_and_test(&bdev->bd_openers)) 765 blkdev_flush_mapping(bdev); 766 if (bdev->bd_disk->fops->release) 767 bdev->bd_disk->fops->release(bdev->bd_disk); 768 } 769 770 static int blkdev_get_whole(struct block_device *bdev, blk_mode_t mode) 771 { 772 struct gendisk *disk = bdev->bd_disk; 773 int ret; 774 775 if (disk->fops->open) { 776 ret = disk->fops->open(disk, mode); 777 if (ret) { 778 /* avoid ghost partitions on a removed medium */ 779 if (ret == -ENOMEDIUM && 780 test_bit(GD_NEED_PART_SCAN, &disk->state)) 781 bdev_disk_changed(disk, true); 782 return ret; 783 } 784 } 785 786 if (!atomic_read(&bdev->bd_openers)) 787 set_init_blocksize(bdev); 788 atomic_inc(&bdev->bd_openers); 789 if (test_bit(GD_NEED_PART_SCAN, &disk->state)) { 790 /* 791 * Only return scanning errors if we are called from contexts 792 * that explicitly want them, e.g. the BLKRRPART ioctl. 793 */ 794 ret = bdev_disk_changed(disk, false); 795 if (ret && (mode & BLK_OPEN_STRICT_SCAN)) { 796 blkdev_put_whole(bdev); 797 return ret; 798 } 799 } 800 return 0; 801 } 802 803 static int blkdev_get_part(struct block_device *part, blk_mode_t mode) 804 { 805 struct gendisk *disk = part->bd_disk; 806 int ret; 807 808 ret = blkdev_get_whole(bdev_whole(part), mode); 809 if (ret) 810 return ret; 811 812 ret = -ENXIO; 813 if (!bdev_nr_sectors(part)) 814 goto out_blkdev_put; 815 816 if (!atomic_read(&part->bd_openers)) { 817 disk->open_partitions++; 818 set_init_blocksize(part); 819 } 820 atomic_inc(&part->bd_openers); 821 return 0; 822 823 out_blkdev_put: 824 blkdev_put_whole(bdev_whole(part)); 825 return ret; 826 } 827 828 int bdev_permission(dev_t dev, blk_mode_t mode, void *holder) 829 { 830 int ret; 831 832 ret = devcgroup_check_permission(DEVCG_DEV_BLOCK, 833 MAJOR(dev), MINOR(dev), 834 ((mode & BLK_OPEN_READ) ? DEVCG_ACC_READ : 0) | 835 ((mode & BLK_OPEN_WRITE) ? DEVCG_ACC_WRITE : 0)); 836 if (ret) 837 return ret; 838 839 /* Blocking writes requires exclusive opener */ 840 if (mode & BLK_OPEN_RESTRICT_WRITES && !holder) 841 return -EINVAL; 842 843 /* 844 * We're using error pointers to indicate to ->release() when we 845 * failed to open that block device. Also this doesn't make sense. 846 */ 847 if (WARN_ON_ONCE(IS_ERR(holder))) 848 return -EINVAL; 849 850 return 0; 851 } 852 853 static void blkdev_put_part(struct block_device *part) 854 { 855 struct block_device *whole = bdev_whole(part); 856 857 if (atomic_dec_and_test(&part->bd_openers)) { 858 blkdev_flush_mapping(part); 859 whole->bd_disk->open_partitions--; 860 } 861 blkdev_put_whole(whole); 862 } 863 864 struct block_device *blkdev_get_no_open(dev_t dev, bool autoload) 865 { 866 struct block_device *bdev; 867 struct inode *inode; 868 869 inode = ilookup(blockdev_superblock, dev); 870 if (!inode && autoload && IS_ENABLED(CONFIG_BLOCK_LEGACY_AUTOLOAD)) { 871 blk_request_module(dev); 872 inode = ilookup(blockdev_superblock, dev); 873 if (inode) 874 pr_warn_ratelimited( 875 "block device autoloading is deprecated and will be removed.\n"); 876 } 877 if (!inode) 878 return NULL; 879 880 /* switch from the inode reference to a device mode one: */ 881 bdev = &BDEV_I(inode)->bdev; 882 if (!kobject_get_unless_zero(&bdev->bd_device.kobj)) 883 bdev = NULL; 884 iput(inode); 885 return bdev; 886 } 887 888 void blkdev_put_no_open(struct block_device *bdev) 889 { 890 put_device(&bdev->bd_device); 891 } 892 893 static bool bdev_writes_blocked(struct block_device *bdev) 894 { 895 return bdev->bd_writers < 0; 896 } 897 898 static void bdev_block_writes(struct block_device *bdev) 899 { 900 bdev->bd_writers--; 901 } 902 903 static void bdev_unblock_writes(struct block_device *bdev) 904 { 905 bdev->bd_writers++; 906 } 907 908 static bool bdev_may_open(struct block_device *bdev, blk_mode_t mode) 909 { 910 if (bdev_allow_write_mounted) 911 return true; 912 /* Writes blocked? */ 913 if (mode & BLK_OPEN_WRITE && bdev_writes_blocked(bdev)) 914 return false; 915 if (mode & BLK_OPEN_RESTRICT_WRITES && bdev->bd_writers > 0) 916 return false; 917 return true; 918 } 919 920 static void bdev_claim_write_access(struct block_device *bdev, blk_mode_t mode) 921 { 922 if (bdev_allow_write_mounted) 923 return; 924 925 /* Claim exclusive or shared write access. */ 926 if (mode & BLK_OPEN_RESTRICT_WRITES) 927 bdev_block_writes(bdev); 928 else if (mode & BLK_OPEN_WRITE) 929 bdev->bd_writers++; 930 } 931 932 static inline bool bdev_unclaimed(const struct file *bdev_file) 933 { 934 return bdev_file->private_data == BDEV_I(bdev_file->f_mapping->host); 935 } 936 937 static void bdev_yield_write_access(struct file *bdev_file) 938 { 939 struct block_device *bdev; 940 941 if (bdev_allow_write_mounted) 942 return; 943 944 if (bdev_unclaimed(bdev_file)) 945 return; 946 947 bdev = file_bdev(bdev_file); 948 949 if (bdev_file->f_mode & FMODE_WRITE_RESTRICTED) 950 bdev_unblock_writes(bdev); 951 else if (bdev_file->f_mode & FMODE_WRITE) 952 bdev->bd_writers--; 953 } 954 955 /** 956 * bdev_open - open a block device 957 * @bdev: block device to open 958 * @mode: open mode (BLK_OPEN_*) 959 * @holder: exclusive holder identifier 960 * @hops: holder operations 961 * @bdev_file: file for the block device 962 * 963 * Open the block device. If @holder is not %NULL, the block device is opened 964 * with exclusive access. Exclusive opens may nest for the same @holder. 965 * 966 * CONTEXT: 967 * Might sleep. 968 * 969 * RETURNS: 970 * zero on success, -errno on failure. 971 */ 972 int bdev_open(struct block_device *bdev, blk_mode_t mode, void *holder, 973 const struct blk_holder_ops *hops, struct file *bdev_file) 974 { 975 bool unblock_events = true; 976 struct gendisk *disk = bdev->bd_disk; 977 int ret; 978 979 if (holder) { 980 mode |= BLK_OPEN_EXCL; 981 ret = bd_prepare_to_claim(bdev, holder, hops); 982 if (ret) 983 return ret; 984 } else { 985 if (WARN_ON_ONCE(mode & BLK_OPEN_EXCL)) 986 return -EIO; 987 } 988 989 disk_block_events(disk); 990 991 mutex_lock(&disk->open_mutex); 992 ret = -ENXIO; 993 if (!disk_live(disk)) 994 goto abort_claiming; 995 if (!try_module_get(disk->fops->owner)) 996 goto abort_claiming; 997 ret = -EBUSY; 998 if (!bdev_may_open(bdev, mode)) 999 goto put_module; 1000 if (bdev_is_partition(bdev)) 1001 ret = blkdev_get_part(bdev, mode); 1002 else 1003 ret = blkdev_get_whole(bdev, mode); 1004 if (ret) 1005 goto put_module; 1006 bdev_claim_write_access(bdev, mode); 1007 if (holder) { 1008 bd_finish_claiming(bdev, holder, hops); 1009 1010 /* 1011 * Block event polling for write claims if requested. Any write 1012 * holder makes the write_holder state stick until all are 1013 * released. This is good enough and tracking individual 1014 * writeable reference is too fragile given the way @mode is 1015 * used in blkdev_get/put(). 1016 */ 1017 if ((mode & BLK_OPEN_WRITE) && 1018 !bdev_test_flag(bdev, BD_WRITE_HOLDER) && 1019 (disk->event_flags & DISK_EVENT_FLAG_BLOCK_ON_EXCL_WRITE)) { 1020 bdev_set_flag(bdev, BD_WRITE_HOLDER); 1021 unblock_events = false; 1022 } 1023 } 1024 mutex_unlock(&disk->open_mutex); 1025 1026 if (unblock_events) 1027 disk_unblock_events(disk); 1028 1029 bdev_file->f_flags |= O_LARGEFILE; 1030 bdev_file->f_mode |= FMODE_CAN_ODIRECT; 1031 if (bdev_nowait(bdev)) 1032 bdev_file->f_mode |= FMODE_NOWAIT; 1033 if (mode & BLK_OPEN_RESTRICT_WRITES) 1034 bdev_file->f_mode |= FMODE_WRITE_RESTRICTED; 1035 bdev_file->f_mapping = bdev->bd_mapping; 1036 bdev_file->f_wb_err = filemap_sample_wb_err(bdev_file->f_mapping); 1037 bdev_file->private_data = holder; 1038 1039 return 0; 1040 put_module: 1041 module_put(disk->fops->owner); 1042 abort_claiming: 1043 if (holder) 1044 bd_abort_claiming(bdev, holder); 1045 mutex_unlock(&disk->open_mutex); 1046 disk_unblock_events(disk); 1047 return ret; 1048 } 1049 1050 /* 1051 * If BLK_OPEN_WRITE_IOCTL is set then this is a historical quirk 1052 * associated with the floppy driver where it has allowed ioctls if the 1053 * file was opened for writing, but does not allow reads or writes. 1054 * Make sure that this quirk is reflected in @f_flags. 1055 * 1056 * It can also happen if a block device is opened as O_RDWR | O_WRONLY. 1057 */ 1058 static unsigned blk_to_file_flags(blk_mode_t mode) 1059 { 1060 unsigned int flags = 0; 1061 1062 if ((mode & (BLK_OPEN_READ | BLK_OPEN_WRITE)) == 1063 (BLK_OPEN_READ | BLK_OPEN_WRITE)) 1064 flags |= O_RDWR; 1065 else if (mode & BLK_OPEN_WRITE_IOCTL) 1066 flags |= O_RDWR | O_WRONLY; 1067 else if (mode & BLK_OPEN_WRITE) 1068 flags |= O_WRONLY; 1069 else if (mode & BLK_OPEN_READ) 1070 flags |= O_RDONLY; /* homeopathic, because O_RDONLY is 0 */ 1071 else 1072 WARN_ON_ONCE(true); 1073 1074 if (mode & BLK_OPEN_NDELAY) 1075 flags |= O_NDELAY; 1076 1077 return flags; 1078 } 1079 1080 struct file *bdev_file_open_by_dev(dev_t dev, blk_mode_t mode, void *holder, 1081 const struct blk_holder_ops *hops) 1082 { 1083 struct file *bdev_file; 1084 struct block_device *bdev; 1085 unsigned int flags; 1086 int ret; 1087 1088 ret = bdev_permission(dev, mode, holder); 1089 if (ret) 1090 return ERR_PTR(ret); 1091 1092 bdev = blkdev_get_no_open(dev, true); 1093 if (!bdev) 1094 return ERR_PTR(-ENXIO); 1095 1096 flags = blk_to_file_flags(mode); 1097 bdev_file = alloc_file_pseudo_noaccount(BD_INODE(bdev), 1098 blockdev_mnt, "", flags | O_LARGEFILE, &def_blk_fops); 1099 if (IS_ERR(bdev_file)) { 1100 blkdev_put_no_open(bdev); 1101 return bdev_file; 1102 } 1103 ihold(BD_INODE(bdev)); 1104 1105 ret = bdev_open(bdev, mode, holder, hops, bdev_file); 1106 if (ret) { 1107 /* We failed to open the block device. Let ->release() know. */ 1108 bdev_file->private_data = ERR_PTR(ret); 1109 fput(bdev_file); 1110 return ERR_PTR(ret); 1111 } 1112 return bdev_file; 1113 } 1114 EXPORT_SYMBOL(bdev_file_open_by_dev); 1115 1116 struct file *bdev_file_open_by_path(const char *path, blk_mode_t mode, 1117 void *holder, 1118 const struct blk_holder_ops *hops) 1119 { 1120 struct file *file; 1121 dev_t dev; 1122 int error; 1123 1124 error = lookup_bdev(path, &dev); 1125 if (error) 1126 return ERR_PTR(error); 1127 1128 file = bdev_file_open_by_dev(dev, mode, holder, hops); 1129 if (!IS_ERR(file) && (mode & BLK_OPEN_WRITE)) { 1130 if (bdev_read_only(file_bdev(file))) { 1131 fput(file); 1132 file = ERR_PTR(-EACCES); 1133 } 1134 } 1135 1136 return file; 1137 } 1138 EXPORT_SYMBOL(bdev_file_open_by_path); 1139 1140 static inline void bd_yield_claim(struct file *bdev_file) 1141 { 1142 struct block_device *bdev = file_bdev(bdev_file); 1143 void *holder = bdev_file->private_data; 1144 1145 lockdep_assert_held(&bdev->bd_disk->open_mutex); 1146 1147 if (WARN_ON_ONCE(IS_ERR_OR_NULL(holder))) 1148 return; 1149 1150 if (!bdev_unclaimed(bdev_file)) 1151 bd_end_claim(bdev, holder); 1152 } 1153 1154 void bdev_release(struct file *bdev_file) 1155 { 1156 struct block_device *bdev = file_bdev(bdev_file); 1157 void *holder = bdev_file->private_data; 1158 struct gendisk *disk = bdev->bd_disk; 1159 1160 /* We failed to open that block device. */ 1161 if (IS_ERR(holder)) 1162 goto put_no_open; 1163 1164 /* 1165 * Sync early if it looks like we're the last one. If someone else 1166 * opens the block device between now and the decrement of bd_openers 1167 * then we did a sync that we didn't need to, but that's not the end 1168 * of the world and we want to avoid long (could be several minute) 1169 * syncs while holding the mutex. 1170 */ 1171 if (atomic_read(&bdev->bd_openers) == 1) 1172 sync_blockdev(bdev); 1173 1174 mutex_lock(&disk->open_mutex); 1175 bdev_yield_write_access(bdev_file); 1176 1177 if (holder) 1178 bd_yield_claim(bdev_file); 1179 1180 /* 1181 * Trigger event checking and tell drivers to flush MEDIA_CHANGE 1182 * event. This is to ensure detection of media removal commanded 1183 * from userland - e.g. eject(1). 1184 */ 1185 disk_flush_events(disk, DISK_EVENT_MEDIA_CHANGE); 1186 1187 if (bdev_is_partition(bdev)) 1188 blkdev_put_part(bdev); 1189 else 1190 blkdev_put_whole(bdev); 1191 mutex_unlock(&disk->open_mutex); 1192 1193 module_put(disk->fops->owner); 1194 put_no_open: 1195 blkdev_put_no_open(bdev); 1196 } 1197 1198 /** 1199 * bdev_yield_claim - give up the holder claim on an open block device 1200 * @bdev_file: open block device 1201 * 1202 * Yield the holder and any write access for @bdev_file without closing it, so 1203 * the caller can still act on the device - e.g. bdev_allow_freeze() it - before 1204 * the final bdev_fput(). bdev_fput() yields too, so calling it afterwards is 1205 * safe. 1206 */ 1207 void bdev_yield_claim(struct file *bdev_file) 1208 { 1209 struct block_device *bdev; 1210 struct gendisk *disk; 1211 1212 if (!bdev_file->private_data) 1213 return; 1214 1215 bdev = file_bdev(bdev_file); 1216 disk = bdev->bd_disk; 1217 1218 mutex_lock(&disk->open_mutex); 1219 bdev_yield_write_access(bdev_file); 1220 bd_yield_claim(bdev_file); 1221 /* 1222 * Tell release we already gave up our hold on the 1223 * device and if write restrictions are available that 1224 * we already gave up write access to the device. 1225 */ 1226 bdev_file->private_data = BDEV_I(bdev_file->f_mapping->host); 1227 mutex_unlock(&disk->open_mutex); 1228 } 1229 EXPORT_SYMBOL_GPL(bdev_yield_claim); 1230 1231 /** 1232 * bdev_fput - yield claim to the block device and put the file 1233 * @bdev_file: open block device 1234 * 1235 * Yield claim on the block device and put the file. Ensure that the 1236 * block device can be reclaimed before the file is closed which is a 1237 * deferred operation. 1238 */ 1239 void bdev_fput(struct file *bdev_file) 1240 { 1241 if (WARN_ON_ONCE(bdev_file->f_op != &def_blk_fops)) 1242 return; 1243 1244 bdev_yield_claim(bdev_file); 1245 fput(bdev_file); 1246 } 1247 EXPORT_SYMBOL(bdev_fput); 1248 1249 /** 1250 * lookup_bdev() - Look up a struct block_device by name. 1251 * @pathname: Name of the block device in the filesystem. 1252 * @dev: Pointer to the block device's dev_t, if found. 1253 * 1254 * Lookup the block device's dev_t at @pathname in the current 1255 * namespace if possible and return it in @dev. 1256 * 1257 * Context: May sleep. 1258 * Return: 0 if succeeded, negative errno otherwise. 1259 */ 1260 int lookup_bdev(const char *pathname, dev_t *dev) 1261 { 1262 struct inode *inode; 1263 struct path path; 1264 int error; 1265 1266 if (!pathname || !*pathname) 1267 return -EINVAL; 1268 1269 error = kern_path(pathname, LOOKUP_FOLLOW, &path); 1270 if (error) 1271 return error; 1272 1273 inode = d_backing_inode(path.dentry); 1274 error = -ENOTBLK; 1275 if (!S_ISBLK(inode->i_mode)) 1276 goto out_path_put; 1277 error = -EACCES; 1278 if (!may_open_dev(&path)) 1279 goto out_path_put; 1280 1281 /* 1282 * Reject a block device inode with i_rdev == 0. A dev_t of 0 is 1283 * never valid for a block device: no real block device driver 1284 * registers major 0. Fake block device inodes (e.g. fuse with 1285 * rootmode=S_IFBLK) can expose i_rdev == 0, and letting that 1286 * propagate would confuse superblock lookup and trigger warnings 1287 * in the device-to-superblock table (super_dev_register). 1288 */ 1289 error = -ENODEV; 1290 if (!inode->i_rdev) 1291 goto out_path_put; 1292 1293 *dev = inode->i_rdev; 1294 error = 0; 1295 out_path_put: 1296 path_put(&path); 1297 return error; 1298 } 1299 EXPORT_SYMBOL(lookup_bdev); 1300 1301 /** 1302 * bdev_mark_dead - mark a block device as dead 1303 * @bdev: block device to operate on 1304 * @surprise: indicate a surprise removal 1305 * 1306 * Tell the file system that this devices or media is dead. If @surprise is set 1307 * to %true the device or media is already gone, if not we are preparing for an 1308 * orderly removal. 1309 * 1310 * This calls into the file system, which then typicall syncs out all dirty data 1311 * and writes back inodes and then invalidates any cached data in the inodes on 1312 * the file system. In addition we also invalidate the block device mapping. 1313 */ 1314 void bdev_mark_dead(struct block_device *bdev, bool surprise) 1315 { 1316 mutex_lock(&bdev->bd_holder_lock); 1317 if (bdev->bd_holder_ops && bdev->bd_holder_ops->mark_dead) 1318 bdev->bd_holder_ops->mark_dead(bdev, surprise); 1319 else { 1320 mutex_unlock(&bdev->bd_holder_lock); 1321 /* 1322 * On surprise removal the device is already gone; syncing is 1323 * futile and can hang forever waiting on I/O that will never 1324 * complete. Match fs_bdev_mark_dead(), which also skips it. 1325 */ 1326 if (!surprise) 1327 sync_blockdev(bdev); 1328 } 1329 1330 invalidate_bdev(bdev); 1331 } 1332 /* 1333 * New drivers should not use this directly. There are some drivers however 1334 * that needs this for historical reasons. For example, the DASD driver has 1335 * historically had a shutdown to offline mode that doesn't actually remove the 1336 * gendisk that otherwise looks a lot like a safe device removal. 1337 */ 1338 EXPORT_SYMBOL_GPL(bdev_mark_dead); 1339 1340 void sync_bdevs(bool wait) 1341 { 1342 struct inode *inode, *old_inode = NULL; 1343 1344 spin_lock(&blockdev_superblock->s_inode_list_lock); 1345 list_for_each_entry(inode, &blockdev_superblock->s_inodes, i_sb_list) { 1346 struct address_space *mapping = inode->i_mapping; 1347 struct block_device *bdev; 1348 1349 spin_lock(&inode->i_lock); 1350 if (inode_state_read(inode) & (I_FREEING | I_WILL_FREE | I_NEW) || 1351 mapping->nrpages == 0) { 1352 spin_unlock(&inode->i_lock); 1353 continue; 1354 } 1355 __iget(inode); 1356 spin_unlock(&inode->i_lock); 1357 spin_unlock(&blockdev_superblock->s_inode_list_lock); 1358 /* 1359 * We hold a reference to 'inode' so it couldn't have been 1360 * removed from s_inodes list while we dropped the 1361 * s_inode_list_lock We cannot iput the inode now as we can 1362 * be holding the last reference and we cannot iput it under 1363 * s_inode_list_lock. So we keep the reference and iput it 1364 * later. 1365 */ 1366 iput(old_inode); 1367 old_inode = inode; 1368 bdev = I_BDEV(inode); 1369 1370 mutex_lock(&bdev->bd_disk->open_mutex); 1371 if (!atomic_read(&bdev->bd_openers)) { 1372 ; /* skip */ 1373 } else if (wait) { 1374 /* 1375 * We keep the error status of individual mapping so 1376 * that applications can catch the writeback error using 1377 * fsync(2). See filemap_fdatawait_keep_errors() for 1378 * details. 1379 */ 1380 filemap_fdatawait_keep_errors(inode->i_mapping); 1381 } else { 1382 filemap_fdatawrite(inode->i_mapping); 1383 } 1384 mutex_unlock(&bdev->bd_disk->open_mutex); 1385 1386 spin_lock(&blockdev_superblock->s_inode_list_lock); 1387 } 1388 spin_unlock(&blockdev_superblock->s_inode_list_lock); 1389 iput(old_inode); 1390 } 1391 1392 /* 1393 * Handle STATX_{DIOALIGN, WRITE_ATOMIC} for block devices. 1394 */ 1395 void bdev_statx(const struct path *path, struct kstat *stat, u32 request_mask) 1396 { 1397 struct block_device *bdev; 1398 1399 /* 1400 * Note that d_backing_inode() returns the block device node inode, not 1401 * the block device's internal inode. Therefore it is *not* valid to 1402 * use I_BDEV() here; the block device has to be looked up by i_rdev 1403 * instead. 1404 */ 1405 bdev = blkdev_get_no_open(d_backing_inode(path->dentry)->i_rdev, false); 1406 if (!bdev) 1407 return; 1408 1409 if (request_mask & STATX_DIOALIGN) { 1410 stat->dio_mem_align = bdev_dma_alignment(bdev) + 1; 1411 stat->dio_offset_align = bdev_logical_block_size(bdev); 1412 stat->result_mask |= STATX_DIOALIGN; 1413 } 1414 1415 if (request_mask & STATX_WRITE_ATOMIC && bdev_can_atomic_write(bdev)) { 1416 struct request_queue *bd_queue = bdev->bd_queue; 1417 1418 generic_fill_statx_atomic_writes(stat, 1419 queue_atomic_write_unit_min_bytes(bd_queue), 1420 queue_atomic_write_unit_max_bytes(bd_queue), 1421 0); 1422 } 1423 1424 stat->blksize = bdev_io_min(bdev); 1425 1426 blkdev_put_no_open(bdev); 1427 } 1428 1429 bool disk_live(struct gendisk *disk) 1430 { 1431 return !inode_unhashed(BD_INODE(disk->part0)); 1432 } 1433 EXPORT_SYMBOL_GPL(disk_live); 1434 1435 unsigned int block_size(struct block_device *bdev) 1436 { 1437 return 1 << BD_INODE(bdev)->i_blkbits; 1438 } 1439 EXPORT_SYMBOL_GPL(block_size); 1440 1441 static int __init setup_bdev_allow_write_mounted(char *str) 1442 { 1443 if (kstrtobool(str, &bdev_allow_write_mounted)) 1444 pr_warn("Invalid option string for bdev_allow_write_mounted:" 1445 " '%s'\n", str); 1446 return 1; 1447 } 1448 __setup("bdev_allow_write_mounted=", setup_bdev_allow_write_mounted); 1449