1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 1991, 1992 Linus Torvalds
4 * Copyright (C) 2001 Andrea Arcangeli <andrea@suse.de> SuSE
5 * Copyright (C) 2016 - 2020 Christoph Hellwig
8 #include <linux/init.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"
34 /* Should we allow writing to mounted block devices? */
35 static bool bdev_allow_write_mounted
= IS_ENABLED(CONFIG_BLK_DEV_WRITE_MOUNTED
);
38 struct block_device bdev
;
39 struct inode vfs_inode
;
42 static inline struct bdev_inode
*BDEV_I(struct inode
*inode
)
44 return container_of(inode
, struct bdev_inode
, vfs_inode
);
47 static inline struct inode
*BD_INODE(struct block_device
*bdev
)
49 return &container_of(bdev
, struct bdev_inode
, bdev
)->vfs_inode
;
52 struct block_device
*I_BDEV(struct inode
*inode
)
54 return &BDEV_I(inode
)->bdev
;
56 EXPORT_SYMBOL(I_BDEV
);
58 struct block_device
*file_bdev(struct file
*bdev_file
)
60 return I_BDEV(bdev_file
->f_mapping
->host
);
62 EXPORT_SYMBOL(file_bdev
);
64 static void bdev_write_inode(struct block_device
*bdev
)
66 struct inode
*inode
= BD_INODE(bdev
);
69 spin_lock(&inode
->i_lock
);
70 while (inode
->i_state
& I_DIRTY
) {
71 spin_unlock(&inode
->i_lock
);
72 ret
= write_inode_now(inode
, true);
75 "VFS: Dirty inode writeback failed for block device %pg (err=%d).\n",
77 spin_lock(&inode
->i_lock
);
79 spin_unlock(&inode
->i_lock
);
82 /* Kill _all_ buffers and pagecache , dirty or not.. */
83 static void kill_bdev(struct block_device
*bdev
)
85 struct address_space
*mapping
= bdev
->bd_mapping
;
87 if (mapping_empty(mapping
))
91 truncate_inode_pages(mapping
, 0);
94 /* Invalidate clean unused buffers and pagecache. */
95 void invalidate_bdev(struct block_device
*bdev
)
97 struct address_space
*mapping
= bdev
->bd_mapping
;
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);
105 EXPORT_SYMBOL(invalidate_bdev
);
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).
111 int truncate_bdev_range(struct block_device
*bdev
, blk_mode_t mode
,
112 loff_t lstart
, loff_t lend
)
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.
119 if (!(mode
& BLK_OPEN_EXCL
)) {
120 int err
= bd_prepare_to_claim(bdev
, truncate_bdev_range
, NULL
);
125 truncate_inode_pages_range(bdev
->bd_mapping
, lstart
, lend
);
126 if (!(mode
& BLK_OPEN_EXCL
))
127 bd_abort_claiming(bdev
, truncate_bdev_range
);
132 * Someone else has handle exclusively open. Try invalidating instead.
133 * The 'end' argument is inclusive so the rounding is safe.
135 return invalidate_inode_pages2_range(bdev
->bd_mapping
,
136 lstart
>> PAGE_SHIFT
,
140 static void set_init_blocksize(struct block_device
*bdev
)
142 unsigned int bsize
= bdev_logical_block_size(bdev
);
143 loff_t size
= i_size_read(BD_INODE(bdev
));
145 while (bsize
< PAGE_SIZE
) {
150 BD_INODE(bdev
)->i_blkbits
= blksize_bits(bsize
);
151 mapping_set_folio_min_order(BD_INODE(bdev
)->i_mapping
,
156 * bdev_validate_blocksize - check that this block size is acceptable
157 * @bdev: blockdevice to check
158 * @block_size: block size to check
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.
163 * Return: On success zero is returned, negative error code on failure.
165 int bdev_validate_blocksize(struct block_device
*bdev
, int block_size
)
167 if (blk_validate_block_size(block_size
))
170 /* Size cannot be smaller than the size supported by the device */
171 if (block_size
< bdev_logical_block_size(bdev
))
176 EXPORT_SYMBOL_GPL(bdev_validate_blocksize
);
178 int set_blocksize(struct file
*file
, int size
)
180 struct inode
*inode
= file
->f_mapping
->host
;
181 struct block_device
*bdev
= I_BDEV(inode
);
184 ret
= bdev_validate_blocksize(bdev
, size
);
188 if (!file
->private_data
)
191 /* Don't change the size if it is same as current */
192 if (inode
->i_blkbits
!= blksize_bits(size
)) {
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.
204 filemap_invalidate_lock(inode
->i_mapping
);
209 inode
->i_blkbits
= blksize_bits(size
);
210 mapping_set_folio_min_order(inode
->i_mapping
, get_order(size
));
212 filemap_invalidate_unlock(inode
->i_mapping
);
218 EXPORT_SYMBOL(set_blocksize
);
220 int sb_set_blocksize(struct super_block
*sb
, int size
)
222 if (!(sb
->s_type
->fs_flags
& FS_LBS
) && size
> PAGE_SIZE
)
224 if (set_blocksize(sb
->s_bdev_file
, size
))
226 /* If we get here, we know size is validated */
227 sb
->s_blocksize
= size
;
228 sb
->s_blocksize_bits
= blksize_bits(size
);
229 return sb
->s_blocksize
;
232 EXPORT_SYMBOL(sb_set_blocksize
);
234 int sb_min_blocksize(struct super_block
*sb
, int size
)
236 int minsize
= bdev_logical_block_size(sb
->s_bdev
);
239 return sb_set_blocksize(sb
, size
);
242 EXPORT_SYMBOL(sb_min_blocksize
);
244 int sync_blockdev_nowait(struct block_device
*bdev
)
248 return filemap_flush(bdev
->bd_mapping
);
250 EXPORT_SYMBOL_GPL(sync_blockdev_nowait
);
253 * Write out and wait upon all the dirty data associated with a block
254 * device via its mapping. Does not take the superblock lock.
256 int sync_blockdev(struct block_device
*bdev
)
260 return filemap_write_and_wait(bdev
->bd_mapping
);
262 EXPORT_SYMBOL(sync_blockdev
);
264 int sync_blockdev_range(struct block_device
*bdev
, loff_t lstart
, loff_t lend
)
266 return filemap_write_and_wait_range(bdev
->bd_mapping
,
269 EXPORT_SYMBOL(sync_blockdev_range
);
272 * bdev_freeze - lock a filesystem and force it into a consistent state
273 * @bdev: blockdevice to lock
275 * If a superblock is found on this device, we take the s_umount semaphore
276 * on it to make sure nobody unmounts until the snapshot creation is done.
277 * The reference counter (bd_fsfreeze_count) guarantees that only the last
278 * unfreeze process can unfreeze the frozen filesystem actually when multiple
279 * freeze requests arrive simultaneously. It counts up in bdev_freeze() and
280 * count down in bdev_thaw(). When it becomes 0, thaw_bdev() will unfreeze
283 * Return: On success zero is returned, negative error code on failure.
285 int bdev_freeze(struct block_device
*bdev
)
289 mutex_lock(&bdev
->bd_fsfreeze_mutex
);
291 if (atomic_inc_return(&bdev
->bd_fsfreeze_count
) > 1) {
292 mutex_unlock(&bdev
->bd_fsfreeze_mutex
);
296 mutex_lock(&bdev
->bd_holder_lock
);
297 if (bdev
->bd_holder_ops
&& bdev
->bd_holder_ops
->freeze
) {
298 error
= bdev
->bd_holder_ops
->freeze(bdev
);
299 lockdep_assert_not_held(&bdev
->bd_holder_lock
);
301 mutex_unlock(&bdev
->bd_holder_lock
);
302 error
= sync_blockdev(bdev
);
306 atomic_dec(&bdev
->bd_fsfreeze_count
);
308 mutex_unlock(&bdev
->bd_fsfreeze_mutex
);
311 EXPORT_SYMBOL(bdev_freeze
);
314 * bdev_thaw - unlock filesystem
315 * @bdev: blockdevice to unlock
317 * Unlocks the filesystem and marks it writeable again after bdev_freeze().
319 * Return: On success zero is returned, negative error code on failure.
321 int bdev_thaw(struct block_device
*bdev
)
323 int error
= -EINVAL
, nr_freeze
;
325 mutex_lock(&bdev
->bd_fsfreeze_mutex
);
328 * If this returns < 0 it means that @bd_fsfreeze_count was
329 * already 0 and no decrement was performed.
331 nr_freeze
= atomic_dec_if_positive(&bdev
->bd_fsfreeze_count
);
339 mutex_lock(&bdev
->bd_holder_lock
);
340 if (bdev
->bd_holder_ops
&& bdev
->bd_holder_ops
->thaw
) {
341 error
= bdev
->bd_holder_ops
->thaw(bdev
);
342 lockdep_assert_not_held(&bdev
->bd_holder_lock
);
344 mutex_unlock(&bdev
->bd_holder_lock
);
348 atomic_inc(&bdev
->bd_fsfreeze_count
);
350 mutex_unlock(&bdev
->bd_fsfreeze_mutex
);
353 EXPORT_SYMBOL(bdev_thaw
);
359 static __cacheline_aligned_in_smp
DEFINE_MUTEX(bdev_lock
);
360 static struct kmem_cache
*bdev_cachep __ro_after_init
;
362 static struct inode
*bdev_alloc_inode(struct super_block
*sb
)
364 struct bdev_inode
*ei
= alloc_inode_sb(sb
, bdev_cachep
, GFP_KERNEL
);
368 memset(&ei
->bdev
, 0, sizeof(ei
->bdev
));
370 if (security_bdev_alloc(&ei
->bdev
)) {
371 kmem_cache_free(bdev_cachep
, ei
);
374 return &ei
->vfs_inode
;
377 static void bdev_free_inode(struct inode
*inode
)
379 struct block_device
*bdev
= I_BDEV(inode
);
381 free_percpu(bdev
->bd_stats
);
382 kfree(bdev
->bd_meta_info
);
383 security_bdev_free(bdev
);
385 if (!bdev_is_partition(bdev
)) {
386 if (bdev
->bd_disk
&& bdev
->bd_disk
->bdi
)
387 bdi_put(bdev
->bd_disk
->bdi
);
388 kfree(bdev
->bd_disk
);
391 if (MAJOR(bdev
->bd_dev
) == BLOCK_EXT_MAJOR
)
392 blk_free_ext_minor(MINOR(bdev
->bd_dev
));
394 kmem_cache_free(bdev_cachep
, BDEV_I(inode
));
397 static void init_once(void *data
)
399 struct bdev_inode
*ei
= data
;
401 inode_init_once(&ei
->vfs_inode
);
404 static void bdev_evict_inode(struct inode
*inode
)
406 truncate_inode_pages_final(&inode
->i_data
);
407 invalidate_inode_buffers(inode
); /* is it needed here? */
411 static const struct super_operations bdev_sops
= {
412 .statfs
= simple_statfs
,
413 .alloc_inode
= bdev_alloc_inode
,
414 .free_inode
= bdev_free_inode
,
415 .drop_inode
= generic_delete_inode
,
416 .evict_inode
= bdev_evict_inode
,
419 static int bd_init_fs_context(struct fs_context
*fc
)
421 struct pseudo_fs_context
*ctx
= init_pseudo(fc
, BDEVFS_MAGIC
);
424 fc
->s_iflags
|= SB_I_CGROUPWB
;
425 ctx
->ops
= &bdev_sops
;
429 static struct file_system_type bd_type
= {
431 .init_fs_context
= bd_init_fs_context
,
432 .kill_sb
= kill_anon_super
,
435 struct super_block
*blockdev_superblock __ro_after_init
;
436 static struct vfsmount
*blockdev_mnt __ro_after_init
;
437 EXPORT_SYMBOL_GPL(blockdev_superblock
);
439 void __init
bdev_cache_init(void)
443 bdev_cachep
= kmem_cache_create("bdev_cache", sizeof(struct bdev_inode
),
444 0, (SLAB_HWCACHE_ALIGN
|SLAB_RECLAIM_ACCOUNT
|
445 SLAB_ACCOUNT
|SLAB_PANIC
),
447 err
= register_filesystem(&bd_type
);
449 panic("Cannot register bdev pseudo-fs");
450 blockdev_mnt
= kern_mount(&bd_type
);
451 if (IS_ERR(blockdev_mnt
))
452 panic("Cannot create bdev pseudo-fs");
453 blockdev_superblock
= blockdev_mnt
->mnt_sb
; /* For writeback */
456 struct block_device
*bdev_alloc(struct gendisk
*disk
, u8 partno
)
458 struct block_device
*bdev
;
461 inode
= new_inode(blockdev_superblock
);
464 inode
->i_mode
= S_IFBLK
;
466 inode
->i_data
.a_ops
= &def_blk_aops
;
467 mapping_set_gfp_mask(&inode
->i_data
, GFP_USER
);
469 bdev
= I_BDEV(inode
);
470 mutex_init(&bdev
->bd_fsfreeze_mutex
);
471 spin_lock_init(&bdev
->bd_size_lock
);
472 mutex_init(&bdev
->bd_holder_lock
);
473 atomic_set(&bdev
->__bd_flags
, partno
);
474 bdev
->bd_mapping
= &inode
->i_data
;
475 bdev
->bd_queue
= disk
->queue
;
476 if (partno
&& bdev_test_flag(disk
->part0
, BD_HAS_SUBMIT_BIO
))
477 bdev_set_flag(bdev
, BD_HAS_SUBMIT_BIO
);
478 bdev
->bd_stats
= alloc_percpu(struct disk_stats
);
479 if (!bdev
->bd_stats
) {
483 bdev
->bd_disk
= disk
;
487 void bdev_set_nr_sectors(struct block_device
*bdev
, sector_t sectors
)
489 spin_lock(&bdev
->bd_size_lock
);
490 i_size_write(BD_INODE(bdev
), (loff_t
)sectors
<< SECTOR_SHIFT
);
491 bdev
->bd_nr_sectors
= sectors
;
492 spin_unlock(&bdev
->bd_size_lock
);
495 void bdev_add(struct block_device
*bdev
, dev_t dev
)
497 struct inode
*inode
= BD_INODE(bdev
);
498 if (bdev_stable_writes(bdev
))
499 mapping_set_stable_writes(bdev
->bd_mapping
);
503 insert_inode_hash(inode
);
506 void bdev_unhash(struct block_device
*bdev
)
508 remove_inode_hash(BD_INODE(bdev
));
511 void bdev_drop(struct block_device
*bdev
)
513 iput(BD_INODE(bdev
));
516 long nr_blockdev_pages(void)
521 spin_lock(&blockdev_superblock
->s_inode_list_lock
);
522 list_for_each_entry(inode
, &blockdev_superblock
->s_inodes
, i_sb_list
)
523 ret
+= inode
->i_mapping
->nrpages
;
524 spin_unlock(&blockdev_superblock
->s_inode_list_lock
);
530 * bd_may_claim - test whether a block device can be claimed
531 * @bdev: block device of interest
532 * @holder: holder trying to claim @bdev
535 * Test whether @bdev can be claimed by @holder.
538 * %true if @bdev can be claimed, %false otherwise.
540 static bool bd_may_claim(struct block_device
*bdev
, void *holder
,
541 const struct blk_holder_ops
*hops
)
543 struct block_device
*whole
= bdev_whole(bdev
);
545 lockdep_assert_held(&bdev_lock
);
547 if (bdev
->bd_holder
) {
549 * The same holder can always re-claim.
551 if (bdev
->bd_holder
== holder
) {
552 if (WARN_ON_ONCE(bdev
->bd_holder_ops
!= hops
))
560 * If the whole devices holder is set to bd_may_claim, a partition on
561 * the device is claimed, but not the whole device.
564 whole
->bd_holder
&& whole
->bd_holder
!= bd_may_claim
)
570 * bd_prepare_to_claim - claim a block device
571 * @bdev: block device of interest
572 * @holder: holder trying to claim @bdev
575 * Claim @bdev. This function fails if @bdev is already claimed by another
576 * holder and waits if another claiming is in progress. return, the caller
577 * has ownership of bd_claiming and bd_holder[s].
580 * 0 if @bdev can be claimed, -EBUSY otherwise.
582 int bd_prepare_to_claim(struct block_device
*bdev
, void *holder
,
583 const struct blk_holder_ops
*hops
)
585 struct block_device
*whole
= bdev_whole(bdev
);
587 if (WARN_ON_ONCE(!holder
))
590 mutex_lock(&bdev_lock
);
591 /* if someone else claimed, fail */
592 if (!bd_may_claim(bdev
, holder
, hops
)) {
593 mutex_unlock(&bdev_lock
);
597 /* if claiming is already in progress, wait for it to finish */
598 if (whole
->bd_claiming
) {
599 wait_queue_head_t
*wq
= __var_waitqueue(&whole
->bd_claiming
);
602 prepare_to_wait(wq
, &wait
, TASK_UNINTERRUPTIBLE
);
603 mutex_unlock(&bdev_lock
);
605 finish_wait(wq
, &wait
);
610 whole
->bd_claiming
= holder
;
611 mutex_unlock(&bdev_lock
);
614 EXPORT_SYMBOL_GPL(bd_prepare_to_claim
); /* only for the loop driver */
616 static void bd_clear_claiming(struct block_device
*whole
, void *holder
)
618 lockdep_assert_held(&bdev_lock
);
619 /* tell others that we're done */
620 BUG_ON(whole
->bd_claiming
!= holder
);
621 whole
->bd_claiming
= NULL
;
622 wake_up_var(&whole
->bd_claiming
);
626 * bd_finish_claiming - finish claiming of a block device
627 * @bdev: block device of interest
628 * @holder: holder that has claimed @bdev
629 * @hops: block device holder operations
631 * Finish exclusive open of a block device. Mark the device as exlusively
632 * open by the holder and wake up all waiters for exclusive open to finish.
634 static void bd_finish_claiming(struct block_device
*bdev
, void *holder
,
635 const struct blk_holder_ops
*hops
)
637 struct block_device
*whole
= bdev_whole(bdev
);
639 mutex_lock(&bdev_lock
);
640 BUG_ON(!bd_may_claim(bdev
, holder
, hops
));
642 * Note that for a whole device bd_holders will be incremented twice,
643 * and bd_holder will be set to bd_may_claim before being set to holder
646 whole
->bd_holder
= bd_may_claim
;
648 mutex_lock(&bdev
->bd_holder_lock
);
649 bdev
->bd_holder
= holder
;
650 bdev
->bd_holder_ops
= hops
;
651 mutex_unlock(&bdev
->bd_holder_lock
);
652 bd_clear_claiming(whole
, holder
);
653 mutex_unlock(&bdev_lock
);
657 * bd_abort_claiming - abort claiming of a block device
658 * @bdev: block device of interest
659 * @holder: holder that has claimed @bdev
661 * Abort claiming of a block device when the exclusive open failed. This can be
662 * also used when exclusive open is not actually desired and we just needed
663 * to block other exclusive openers for a while.
665 void bd_abort_claiming(struct block_device
*bdev
, void *holder
)
667 mutex_lock(&bdev_lock
);
668 bd_clear_claiming(bdev_whole(bdev
), holder
);
669 mutex_unlock(&bdev_lock
);
671 EXPORT_SYMBOL(bd_abort_claiming
);
673 static void bd_end_claim(struct block_device
*bdev
, void *holder
)
675 struct block_device
*whole
= bdev_whole(bdev
);
676 bool unblock
= false;
679 * Release a claim on the device. The holder fields are protected with
680 * bdev_lock. open_mutex is used to synchronize disk_holder unlinking.
682 mutex_lock(&bdev_lock
);
683 WARN_ON_ONCE(bdev
->bd_holder
!= holder
);
684 WARN_ON_ONCE(--bdev
->bd_holders
< 0);
685 WARN_ON_ONCE(--whole
->bd_holders
< 0);
686 if (!bdev
->bd_holders
) {
687 mutex_lock(&bdev
->bd_holder_lock
);
688 bdev
->bd_holder
= NULL
;
689 bdev
->bd_holder_ops
= NULL
;
690 mutex_unlock(&bdev
->bd_holder_lock
);
691 if (bdev_test_flag(bdev
, BD_WRITE_HOLDER
))
694 if (!whole
->bd_holders
)
695 whole
->bd_holder
= NULL
;
696 mutex_unlock(&bdev_lock
);
699 * If this was the last claim, remove holder link and unblock evpoll if
700 * it was a write holder.
703 disk_unblock_events(bdev
->bd_disk
);
704 bdev_clear_flag(bdev
, BD_WRITE_HOLDER
);
708 static void blkdev_flush_mapping(struct block_device
*bdev
)
710 WARN_ON_ONCE(bdev
->bd_holders
);
713 bdev_write_inode(bdev
);
716 static void blkdev_put_whole(struct block_device
*bdev
)
718 if (atomic_dec_and_test(&bdev
->bd_openers
))
719 blkdev_flush_mapping(bdev
);
720 if (bdev
->bd_disk
->fops
->release
)
721 bdev
->bd_disk
->fops
->release(bdev
->bd_disk
);
724 static int blkdev_get_whole(struct block_device
*bdev
, blk_mode_t mode
)
726 struct gendisk
*disk
= bdev
->bd_disk
;
729 if (disk
->fops
->open
) {
730 ret
= disk
->fops
->open(disk
, mode
);
732 /* avoid ghost partitions on a removed medium */
733 if (ret
== -ENOMEDIUM
&&
734 test_bit(GD_NEED_PART_SCAN
, &disk
->state
))
735 bdev_disk_changed(disk
, true);
740 if (!atomic_read(&bdev
->bd_openers
))
741 set_init_blocksize(bdev
);
742 atomic_inc(&bdev
->bd_openers
);
743 if (test_bit(GD_NEED_PART_SCAN
, &disk
->state
)) {
745 * Only return scanning errors if we are called from contexts
746 * that explicitly want them, e.g. the BLKRRPART ioctl.
748 ret
= bdev_disk_changed(disk
, false);
749 if (ret
&& (mode
& BLK_OPEN_STRICT_SCAN
)) {
750 blkdev_put_whole(bdev
);
757 static int blkdev_get_part(struct block_device
*part
, blk_mode_t mode
)
759 struct gendisk
*disk
= part
->bd_disk
;
762 ret
= blkdev_get_whole(bdev_whole(part
), mode
);
767 if (!bdev_nr_sectors(part
))
770 if (!atomic_read(&part
->bd_openers
)) {
771 disk
->open_partitions
++;
772 set_init_blocksize(part
);
774 atomic_inc(&part
->bd_openers
);
778 blkdev_put_whole(bdev_whole(part
));
782 int bdev_permission(dev_t dev
, blk_mode_t mode
, void *holder
)
786 ret
= devcgroup_check_permission(DEVCG_DEV_BLOCK
,
787 MAJOR(dev
), MINOR(dev
),
788 ((mode
& BLK_OPEN_READ
) ? DEVCG_ACC_READ
: 0) |
789 ((mode
& BLK_OPEN_WRITE
) ? DEVCG_ACC_WRITE
: 0));
793 /* Blocking writes requires exclusive opener */
794 if (mode
& BLK_OPEN_RESTRICT_WRITES
&& !holder
)
798 * We're using error pointers to indicate to ->release() when we
799 * failed to open that block device. Also this doesn't make sense.
801 if (WARN_ON_ONCE(IS_ERR(holder
)))
807 static void blkdev_put_part(struct block_device
*part
)
809 struct block_device
*whole
= bdev_whole(part
);
811 if (atomic_dec_and_test(&part
->bd_openers
)) {
812 blkdev_flush_mapping(part
);
813 whole
->bd_disk
->open_partitions
--;
815 blkdev_put_whole(whole
);
818 struct block_device
*blkdev_get_no_open(dev_t dev
, bool autoload
)
820 struct block_device
*bdev
;
823 inode
= ilookup(blockdev_superblock
, dev
);
824 if (!inode
&& autoload
&& IS_ENABLED(CONFIG_BLOCK_LEGACY_AUTOLOAD
)) {
825 blk_request_module(dev
);
826 inode
= ilookup(blockdev_superblock
, dev
);
829 "block device autoloading is deprecated and will be removed.\n");
834 /* switch from the inode reference to a device mode one: */
835 bdev
= &BDEV_I(inode
)->bdev
;
836 if (!kobject_get_unless_zero(&bdev
->bd_device
.kobj
))
842 void blkdev_put_no_open(struct block_device
*bdev
)
844 put_device(&bdev
->bd_device
);
847 static bool bdev_writes_blocked(struct block_device
*bdev
)
849 return bdev
->bd_writers
< 0;
852 static void bdev_block_writes(struct block_device
*bdev
)
857 static void bdev_unblock_writes(struct block_device
*bdev
)
862 static bool bdev_may_open(struct block_device
*bdev
, blk_mode_t mode
)
864 if (bdev_allow_write_mounted
)
866 /* Writes blocked? */
867 if (mode
& BLK_OPEN_WRITE
&& bdev_writes_blocked(bdev
))
869 if (mode
& BLK_OPEN_RESTRICT_WRITES
&& bdev
->bd_writers
> 0)
874 static void bdev_claim_write_access(struct block_device
*bdev
, blk_mode_t mode
)
876 if (bdev_allow_write_mounted
)
879 /* Claim exclusive or shared write access. */
880 if (mode
& BLK_OPEN_RESTRICT_WRITES
)
881 bdev_block_writes(bdev
);
882 else if (mode
& BLK_OPEN_WRITE
)
886 static inline bool bdev_unclaimed(const struct file
*bdev_file
)
888 return bdev_file
->private_data
== BDEV_I(bdev_file
->f_mapping
->host
);
891 static void bdev_yield_write_access(struct file
*bdev_file
)
893 struct block_device
*bdev
;
895 if (bdev_allow_write_mounted
)
898 if (bdev_unclaimed(bdev_file
))
901 bdev
= file_bdev(bdev_file
);
903 if (bdev_file
->f_mode
& FMODE_WRITE_RESTRICTED
)
904 bdev_unblock_writes(bdev
);
905 else if (bdev_file
->f_mode
& FMODE_WRITE
)
910 * bdev_open - open a block device
911 * @bdev: block device to open
912 * @mode: open mode (BLK_OPEN_*)
913 * @holder: exclusive holder identifier
914 * @hops: holder operations
915 * @bdev_file: file for the block device
917 * Open the block device. If @holder is not %NULL, the block device is opened
918 * with exclusive access. Exclusive opens may nest for the same @holder.
924 * zero on success, -errno on failure.
926 int bdev_open(struct block_device
*bdev
, blk_mode_t mode
, void *holder
,
927 const struct blk_holder_ops
*hops
, struct file
*bdev_file
)
929 bool unblock_events
= true;
930 struct gendisk
*disk
= bdev
->bd_disk
;
934 mode
|= BLK_OPEN_EXCL
;
935 ret
= bd_prepare_to_claim(bdev
, holder
, hops
);
939 if (WARN_ON_ONCE(mode
& BLK_OPEN_EXCL
))
943 disk_block_events(disk
);
945 mutex_lock(&disk
->open_mutex
);
947 if (!disk_live(disk
))
949 if (!try_module_get(disk
->fops
->owner
))
952 if (!bdev_may_open(bdev
, mode
))
954 if (bdev_is_partition(bdev
))
955 ret
= blkdev_get_part(bdev
, mode
);
957 ret
= blkdev_get_whole(bdev
, mode
);
960 bdev_claim_write_access(bdev
, mode
);
962 bd_finish_claiming(bdev
, holder
, hops
);
965 * Block event polling for write claims if requested. Any write
966 * holder makes the write_holder state stick until all are
967 * released. This is good enough and tracking individual
968 * writeable reference is too fragile given the way @mode is
969 * used in blkdev_get/put().
971 if ((mode
& BLK_OPEN_WRITE
) &&
972 !bdev_test_flag(bdev
, BD_WRITE_HOLDER
) &&
973 (disk
->event_flags
& DISK_EVENT_FLAG_BLOCK_ON_EXCL_WRITE
)) {
974 bdev_set_flag(bdev
, BD_WRITE_HOLDER
);
975 unblock_events
= false;
978 mutex_unlock(&disk
->open_mutex
);
981 disk_unblock_events(disk
);
983 bdev_file
->f_flags
|= O_LARGEFILE
;
984 bdev_file
->f_mode
|= FMODE_CAN_ODIRECT
;
985 if (bdev_nowait(bdev
))
986 bdev_file
->f_mode
|= FMODE_NOWAIT
;
987 if (mode
& BLK_OPEN_RESTRICT_WRITES
)
988 bdev_file
->f_mode
|= FMODE_WRITE_RESTRICTED
;
989 bdev_file
->f_mapping
= bdev
->bd_mapping
;
990 bdev_file
->f_wb_err
= filemap_sample_wb_err(bdev_file
->f_mapping
);
991 bdev_file
->private_data
= holder
;
995 module_put(disk
->fops
->owner
);
998 bd_abort_claiming(bdev
, holder
);
999 mutex_unlock(&disk
->open_mutex
);
1000 disk_unblock_events(disk
);
1005 * If BLK_OPEN_WRITE_IOCTL is set then this is a historical quirk
1006 * associated with the floppy driver where it has allowed ioctls if the
1007 * file was opened for writing, but does not allow reads or writes.
1008 * Make sure that this quirk is reflected in @f_flags.
1010 * It can also happen if a block device is opened as O_RDWR | O_WRONLY.
1012 static unsigned blk_to_file_flags(blk_mode_t mode
)
1014 unsigned int flags
= 0;
1016 if ((mode
& (BLK_OPEN_READ
| BLK_OPEN_WRITE
)) ==
1017 (BLK_OPEN_READ
| BLK_OPEN_WRITE
))
1019 else if (mode
& BLK_OPEN_WRITE_IOCTL
)
1020 flags
|= O_RDWR
| O_WRONLY
;
1021 else if (mode
& BLK_OPEN_WRITE
)
1023 else if (mode
& BLK_OPEN_READ
)
1024 flags
|= O_RDONLY
; /* homeopathic, because O_RDONLY is 0 */
1028 if (mode
& BLK_OPEN_NDELAY
)
1034 struct file
*bdev_file_open_by_dev(dev_t dev
, blk_mode_t mode
, void *holder
,
1035 const struct blk_holder_ops
*hops
)
1037 struct file
*bdev_file
;
1038 struct block_device
*bdev
;
1042 ret
= bdev_permission(dev
, mode
, holder
);
1044 return ERR_PTR(ret
);
1046 bdev
= blkdev_get_no_open(dev
, true);
1048 return ERR_PTR(-ENXIO
);
1050 flags
= blk_to_file_flags(mode
);
1051 bdev_file
= alloc_file_pseudo_noaccount(BD_INODE(bdev
),
1052 blockdev_mnt
, "", flags
| O_LARGEFILE
, &def_blk_fops
);
1053 if (IS_ERR(bdev_file
)) {
1054 blkdev_put_no_open(bdev
);
1057 ihold(BD_INODE(bdev
));
1059 ret
= bdev_open(bdev
, mode
, holder
, hops
, bdev_file
);
1061 /* We failed to open the block device. Let ->release() know. */
1062 bdev_file
->private_data
= ERR_PTR(ret
);
1064 return ERR_PTR(ret
);
1068 EXPORT_SYMBOL(bdev_file_open_by_dev
);
1070 struct file
*bdev_file_open_by_path(const char *path
, blk_mode_t mode
,
1072 const struct blk_holder_ops
*hops
)
1078 error
= lookup_bdev(path
, &dev
);
1080 return ERR_PTR(error
);
1082 file
= bdev_file_open_by_dev(dev
, mode
, holder
, hops
);
1083 if (!IS_ERR(file
) && (mode
& BLK_OPEN_WRITE
)) {
1084 if (bdev_read_only(file_bdev(file
))) {
1086 file
= ERR_PTR(-EACCES
);
1092 EXPORT_SYMBOL(bdev_file_open_by_path
);
1094 static inline void bd_yield_claim(struct file
*bdev_file
)
1096 struct block_device
*bdev
= file_bdev(bdev_file
);
1097 void *holder
= bdev_file
->private_data
;
1099 lockdep_assert_held(&bdev
->bd_disk
->open_mutex
);
1101 if (WARN_ON_ONCE(IS_ERR_OR_NULL(holder
)))
1104 if (!bdev_unclaimed(bdev_file
))
1105 bd_end_claim(bdev
, holder
);
1108 void bdev_release(struct file
*bdev_file
)
1110 struct block_device
*bdev
= file_bdev(bdev_file
);
1111 void *holder
= bdev_file
->private_data
;
1112 struct gendisk
*disk
= bdev
->bd_disk
;
1114 /* We failed to open that block device. */
1119 * Sync early if it looks like we're the last one. If someone else
1120 * opens the block device between now and the decrement of bd_openers
1121 * then we did a sync that we didn't need to, but that's not the end
1122 * of the world and we want to avoid long (could be several minute)
1123 * syncs while holding the mutex.
1125 if (atomic_read(&bdev
->bd_openers
) == 1)
1126 sync_blockdev(bdev
);
1128 mutex_lock(&disk
->open_mutex
);
1129 bdev_yield_write_access(bdev_file
);
1132 bd_yield_claim(bdev_file
);
1135 * Trigger event checking and tell drivers to flush MEDIA_CHANGE
1136 * event. This is to ensure detection of media removal commanded
1137 * from userland - e.g. eject(1).
1139 disk_flush_events(disk
, DISK_EVENT_MEDIA_CHANGE
);
1141 if (bdev_is_partition(bdev
))
1142 blkdev_put_part(bdev
);
1144 blkdev_put_whole(bdev
);
1145 mutex_unlock(&disk
->open_mutex
);
1147 module_put(disk
->fops
->owner
);
1149 blkdev_put_no_open(bdev
);
1153 * bdev_fput - yield claim to the block device and put the file
1154 * @bdev_file: open block device
1156 * Yield claim on the block device and put the file. Ensure that the
1157 * block device can be reclaimed before the file is closed which is a
1158 * deferred operation.
1160 void bdev_fput(struct file
*bdev_file
)
1162 if (WARN_ON_ONCE(bdev_file
->f_op
!= &def_blk_fops
))
1165 if (bdev_file
->private_data
) {
1166 struct block_device
*bdev
= file_bdev(bdev_file
);
1167 struct gendisk
*disk
= bdev
->bd_disk
;
1169 mutex_lock(&disk
->open_mutex
);
1170 bdev_yield_write_access(bdev_file
);
1171 bd_yield_claim(bdev_file
);
1173 * Tell release we already gave up our hold on the
1174 * device and if write restrictions are available that
1175 * we already gave up write access to the device.
1177 bdev_file
->private_data
= BDEV_I(bdev_file
->f_mapping
->host
);
1178 mutex_unlock(&disk
->open_mutex
);
1183 EXPORT_SYMBOL(bdev_fput
);
1186 * lookup_bdev() - Look up a struct block_device by name.
1187 * @pathname: Name of the block device in the filesystem.
1188 * @dev: Pointer to the block device's dev_t, if found.
1190 * Lookup the block device's dev_t at @pathname in the current
1191 * namespace if possible and return it in @dev.
1193 * Context: May sleep.
1194 * Return: 0 if succeeded, negative errno otherwise.
1196 int lookup_bdev(const char *pathname
, dev_t
*dev
)
1198 struct inode
*inode
;
1202 if (!pathname
|| !*pathname
)
1205 error
= kern_path(pathname
, LOOKUP_FOLLOW
, &path
);
1209 inode
= d_backing_inode(path
.dentry
);
1211 if (!S_ISBLK(inode
->i_mode
))
1214 if (!may_open_dev(&path
))
1217 *dev
= inode
->i_rdev
;
1223 EXPORT_SYMBOL(lookup_bdev
);
1226 * bdev_mark_dead - mark a block device as dead
1227 * @bdev: block device to operate on
1228 * @surprise: indicate a surprise removal
1230 * Tell the file system that this devices or media is dead. If @surprise is set
1231 * to %true the device or media is already gone, if not we are preparing for an
1234 * This calls into the file system, which then typicall syncs out all dirty data
1235 * and writes back inodes and then invalidates any cached data in the inodes on
1236 * the file system. In addition we also invalidate the block device mapping.
1238 void bdev_mark_dead(struct block_device
*bdev
, bool surprise
)
1240 mutex_lock(&bdev
->bd_holder_lock
);
1241 if (bdev
->bd_holder_ops
&& bdev
->bd_holder_ops
->mark_dead
)
1242 bdev
->bd_holder_ops
->mark_dead(bdev
, surprise
);
1244 mutex_unlock(&bdev
->bd_holder_lock
);
1245 sync_blockdev(bdev
);
1248 invalidate_bdev(bdev
);
1251 * New drivers should not use this directly. There are some drivers however
1252 * that needs this for historical reasons. For example, the DASD driver has
1253 * historically had a shutdown to offline mode that doesn't actually remove the
1254 * gendisk that otherwise looks a lot like a safe device removal.
1256 EXPORT_SYMBOL_GPL(bdev_mark_dead
);
1258 void sync_bdevs(bool wait
)
1260 struct inode
*inode
, *old_inode
= NULL
;
1262 spin_lock(&blockdev_superblock
->s_inode_list_lock
);
1263 list_for_each_entry(inode
, &blockdev_superblock
->s_inodes
, i_sb_list
) {
1264 struct address_space
*mapping
= inode
->i_mapping
;
1265 struct block_device
*bdev
;
1267 spin_lock(&inode
->i_lock
);
1268 if (inode
->i_state
& (I_FREEING
|I_WILL_FREE
|I_NEW
) ||
1269 mapping
->nrpages
== 0) {
1270 spin_unlock(&inode
->i_lock
);
1274 spin_unlock(&inode
->i_lock
);
1275 spin_unlock(&blockdev_superblock
->s_inode_list_lock
);
1277 * We hold a reference to 'inode' so it couldn't have been
1278 * removed from s_inodes list while we dropped the
1279 * s_inode_list_lock We cannot iput the inode now as we can
1280 * be holding the last reference and we cannot iput it under
1281 * s_inode_list_lock. So we keep the reference and iput it
1286 bdev
= I_BDEV(inode
);
1288 mutex_lock(&bdev
->bd_disk
->open_mutex
);
1289 if (!atomic_read(&bdev
->bd_openers
)) {
1293 * We keep the error status of individual mapping so
1294 * that applications can catch the writeback error using
1295 * fsync(2). See filemap_fdatawait_keep_errors() for
1298 filemap_fdatawait_keep_errors(inode
->i_mapping
);
1300 filemap_fdatawrite(inode
->i_mapping
);
1302 mutex_unlock(&bdev
->bd_disk
->open_mutex
);
1304 spin_lock(&blockdev_superblock
->s_inode_list_lock
);
1306 spin_unlock(&blockdev_superblock
->s_inode_list_lock
);
1311 * Handle STATX_{DIOALIGN, WRITE_ATOMIC} for block devices.
1313 void bdev_statx(const struct path
*path
, struct kstat
*stat
, u32 request_mask
)
1315 struct block_device
*bdev
;
1318 * Note that d_backing_inode() returns the block device node inode, not
1319 * the block device's internal inode. Therefore it is *not* valid to
1320 * use I_BDEV() here; the block device has to be looked up by i_rdev
1323 bdev
= blkdev_get_no_open(d_backing_inode(path
->dentry
)->i_rdev
, false);
1327 if (request_mask
& STATX_DIOALIGN
) {
1328 stat
->dio_mem_align
= bdev_dma_alignment(bdev
) + 1;
1329 stat
->dio_offset_align
= bdev_logical_block_size(bdev
);
1330 stat
->result_mask
|= STATX_DIOALIGN
;
1333 if (request_mask
& STATX_WRITE_ATOMIC
&& bdev_can_atomic_write(bdev
)) {
1334 struct request_queue
*bd_queue
= bdev
->bd_queue
;
1336 generic_fill_statx_atomic_writes(stat
,
1337 queue_atomic_write_unit_min_bytes(bd_queue
),
1338 queue_atomic_write_unit_max_bytes(bd_queue
),
1342 stat
->blksize
= bdev_io_min(bdev
);
1344 blkdev_put_no_open(bdev
);
1347 bool disk_live(struct gendisk
*disk
)
1349 return !inode_unhashed(BD_INODE(disk
->part0
));
1351 EXPORT_SYMBOL_GPL(disk_live
);
1353 unsigned int block_size(struct block_device
*bdev
)
1355 return 1 << BD_INODE(bdev
)->i_blkbits
;
1357 EXPORT_SYMBOL_GPL(block_size
);
1359 static int __init
setup_bdev_allow_write_mounted(char *str
)
1361 if (kstrtobool(str
, &bdev_allow_write_mounted
))
1362 pr_warn("Invalid option string for bdev_allow_write_mounted:"
1366 __setup("bdev_allow_write_mounted=", setup_bdev_allow_write_mounted
);