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/part_stat.h>
28 #include <linux/uaccess.h>
29 #include <linux/stat.h>
30 #include "../fs/internal.h"
34 struct block_device bdev;
35 struct inode vfs_inode;
38 static inline struct bdev_inode *BDEV_I(struct inode *inode)
40 return container_of(inode, struct bdev_inode, vfs_inode);
43 struct block_device *I_BDEV(struct inode *inode)
45 return &BDEV_I(inode)->bdev;
47 EXPORT_SYMBOL(I_BDEV);
49 static void bdev_write_inode(struct block_device *bdev)
51 struct inode *inode = bdev->bd_inode;
54 spin_lock(&inode->i_lock);
55 while (inode->i_state & I_DIRTY) {
56 spin_unlock(&inode->i_lock);
57 ret = write_inode_now(inode, true);
60 "VFS: Dirty inode writeback failed for block device %pg (err=%d).\n",
62 spin_lock(&inode->i_lock);
64 spin_unlock(&inode->i_lock);
67 /* Kill _all_ buffers and pagecache , dirty or not.. */
68 static void kill_bdev(struct block_device *bdev)
70 struct address_space *mapping = bdev->bd_inode->i_mapping;
72 if (mapping_empty(mapping))
76 truncate_inode_pages(mapping, 0);
79 /* Invalidate clean unused buffers and pagecache. */
80 void invalidate_bdev(struct block_device *bdev)
82 struct address_space *mapping = bdev->bd_inode->i_mapping;
84 if (mapping->nrpages) {
86 lru_add_drain_all(); /* make sure all lru add caches are flushed */
87 invalidate_mapping_pages(mapping, 0, -1);
90 EXPORT_SYMBOL(invalidate_bdev);
93 * Drop all buffers & page cache for given bdev range. This function bails
94 * with error if bdev has other exclusive owner (such as filesystem).
96 int truncate_bdev_range(struct block_device *bdev, fmode_t mode,
97 loff_t lstart, loff_t lend)
100 * If we don't hold exclusive handle for the device, upgrade to it
101 * while we discard the buffer cache to avoid discarding buffers
102 * under live filesystem.
104 if (!(mode & FMODE_EXCL)) {
105 int err = bd_prepare_to_claim(bdev, truncate_bdev_range);
110 truncate_inode_pages_range(bdev->bd_inode->i_mapping, lstart, lend);
111 if (!(mode & FMODE_EXCL))
112 bd_abort_claiming(bdev, truncate_bdev_range);
117 * Someone else has handle exclusively open. Try invalidating instead.
118 * The 'end' argument is inclusive so the rounding is safe.
120 return invalidate_inode_pages2_range(bdev->bd_inode->i_mapping,
121 lstart >> PAGE_SHIFT,
125 static void set_init_blocksize(struct block_device *bdev)
127 unsigned int bsize = bdev_logical_block_size(bdev);
128 loff_t size = i_size_read(bdev->bd_inode);
130 while (bsize < PAGE_SIZE) {
135 bdev->bd_inode->i_blkbits = blksize_bits(bsize);
138 int set_blocksize(struct block_device *bdev, int size)
140 /* Size must be a power of two, and between 512 and PAGE_SIZE */
141 if (size > PAGE_SIZE || size < 512 || !is_power_of_2(size))
144 /* Size cannot be smaller than the size supported by the device */
145 if (size < bdev_logical_block_size(bdev))
148 /* Don't change the size if it is same as current */
149 if (bdev->bd_inode->i_blkbits != blksize_bits(size)) {
151 bdev->bd_inode->i_blkbits = blksize_bits(size);
157 EXPORT_SYMBOL(set_blocksize);
159 int sb_set_blocksize(struct super_block *sb, int size)
161 if (set_blocksize(sb->s_bdev, size))
163 /* If we get here, we know size is power of two
164 * and it's value is between 512 and PAGE_SIZE */
165 sb->s_blocksize = size;
166 sb->s_blocksize_bits = blksize_bits(size);
167 return sb->s_blocksize;
170 EXPORT_SYMBOL(sb_set_blocksize);
172 int sb_min_blocksize(struct super_block *sb, int size)
174 int minsize = bdev_logical_block_size(sb->s_bdev);
177 return sb_set_blocksize(sb, size);
180 EXPORT_SYMBOL(sb_min_blocksize);
182 int sync_blockdev_nowait(struct block_device *bdev)
186 return filemap_flush(bdev->bd_inode->i_mapping);
188 EXPORT_SYMBOL_GPL(sync_blockdev_nowait);
191 * Write out and wait upon all the dirty data associated with a block
192 * device via its mapping. Does not take the superblock lock.
194 int sync_blockdev(struct block_device *bdev)
198 return filemap_write_and_wait(bdev->bd_inode->i_mapping);
200 EXPORT_SYMBOL(sync_blockdev);
202 int sync_blockdev_range(struct block_device *bdev, loff_t lstart, loff_t lend)
204 return filemap_write_and_wait_range(bdev->bd_inode->i_mapping,
207 EXPORT_SYMBOL(sync_blockdev_range);
210 * Write out and wait upon all dirty data associated with this
211 * device. Filesystem data as well as the underlying block
212 * device. Takes the superblock lock.
214 int fsync_bdev(struct block_device *bdev)
216 struct super_block *sb = get_super(bdev);
218 int res = sync_filesystem(sb);
222 return sync_blockdev(bdev);
224 EXPORT_SYMBOL(fsync_bdev);
227 * freeze_bdev - lock a filesystem and force it into a consistent state
228 * @bdev: blockdevice to lock
230 * If a superblock is found on this device, we take the s_umount semaphore
231 * on it to make sure nobody unmounts until the snapshot creation is done.
232 * The reference counter (bd_fsfreeze_count) guarantees that only the last
233 * unfreeze process can unfreeze the frozen filesystem actually when multiple
234 * freeze requests arrive simultaneously. It counts up in freeze_bdev() and
235 * count down in thaw_bdev(). When it becomes 0, thaw_bdev() will unfreeze
238 int freeze_bdev(struct block_device *bdev)
240 struct super_block *sb;
243 mutex_lock(&bdev->bd_fsfreeze_mutex);
244 if (++bdev->bd_fsfreeze_count > 1)
247 sb = get_active_super(bdev);
250 if (sb->s_op->freeze_super)
251 error = sb->s_op->freeze_super(sb);
253 error = freeze_super(sb);
254 deactivate_super(sb);
257 bdev->bd_fsfreeze_count--;
260 bdev->bd_fsfreeze_sb = sb;
265 mutex_unlock(&bdev->bd_fsfreeze_mutex);
268 EXPORT_SYMBOL(freeze_bdev);
271 * thaw_bdev - unlock filesystem
272 * @bdev: blockdevice to unlock
274 * Unlocks the filesystem and marks it writeable again after freeze_bdev().
276 int thaw_bdev(struct block_device *bdev)
278 struct super_block *sb;
281 mutex_lock(&bdev->bd_fsfreeze_mutex);
282 if (!bdev->bd_fsfreeze_count)
286 if (--bdev->bd_fsfreeze_count > 0)
289 sb = bdev->bd_fsfreeze_sb;
293 if (sb->s_op->thaw_super)
294 error = sb->s_op->thaw_super(sb);
296 error = thaw_super(sb);
298 bdev->bd_fsfreeze_count++;
300 bdev->bd_fsfreeze_sb = NULL;
302 mutex_unlock(&bdev->bd_fsfreeze_mutex);
305 EXPORT_SYMBOL(thaw_bdev);
308 * bdev_read_page() - Start reading a page from a block device
309 * @bdev: The device to read the page from
310 * @sector: The offset on the device to read the page to (need not be aligned)
311 * @page: The page to read
313 * On entry, the page should be locked. It will be unlocked when the page
314 * has been read. If the block driver implements rw_page synchronously,
315 * that will be true on exit from this function, but it need not be.
317 * Errors returned by this function are usually "soft", eg out of memory, or
318 * queue full; callers should try a different route to read this page rather
319 * than propagate an error back up the stack.
321 * Return: negative errno if an error occurs, 0 if submission was successful.
323 int bdev_read_page(struct block_device *bdev, sector_t sector,
326 const struct block_device_operations *ops = bdev->bd_disk->fops;
327 int result = -EOPNOTSUPP;
329 if (!ops->rw_page || bdev_get_integrity(bdev))
332 result = blk_queue_enter(bdev_get_queue(bdev), 0);
335 result = ops->rw_page(bdev, sector + get_start_sect(bdev), page,
337 blk_queue_exit(bdev_get_queue(bdev));
342 * bdev_write_page() - Start writing a page to a block device
343 * @bdev: The device to write the page to
344 * @sector: The offset on the device to write the page to (need not be aligned)
345 * @page: The page to write
346 * @wbc: The writeback_control for the write
348 * On entry, the page should be locked and not currently under writeback.
349 * On exit, if the write started successfully, the page will be unlocked and
350 * under writeback. If the write failed already (eg the driver failed to
351 * queue the page to the device), the page will still be locked. If the
352 * caller is a ->writepage implementation, it will need to unlock the page.
354 * Errors returned by this function are usually "soft", eg out of memory, or
355 * queue full; callers should try a different route to write this page rather
356 * than propagate an error back up the stack.
358 * Return: negative errno if an error occurs, 0 if submission was successful.
360 int bdev_write_page(struct block_device *bdev, sector_t sector,
361 struct page *page, struct writeback_control *wbc)
364 const struct block_device_operations *ops = bdev->bd_disk->fops;
366 if (!ops->rw_page || bdev_get_integrity(bdev))
368 result = blk_queue_enter(bdev_get_queue(bdev), 0);
372 set_page_writeback(page);
373 result = ops->rw_page(bdev, sector + get_start_sect(bdev), page,
376 end_page_writeback(page);
378 clean_page_buffers(page);
381 blk_queue_exit(bdev_get_queue(bdev));
389 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock);
390 static struct kmem_cache * bdev_cachep __read_mostly;
392 static struct inode *bdev_alloc_inode(struct super_block *sb)
394 struct bdev_inode *ei = alloc_inode_sb(sb, bdev_cachep, GFP_KERNEL);
398 memset(&ei->bdev, 0, sizeof(ei->bdev));
399 return &ei->vfs_inode;
402 static void bdev_free_inode(struct inode *inode)
404 struct block_device *bdev = I_BDEV(inode);
406 free_percpu(bdev->bd_stats);
407 kfree(bdev->bd_meta_info);
409 if (!bdev_is_partition(bdev)) {
410 if (bdev->bd_disk && bdev->bd_disk->bdi)
411 bdi_put(bdev->bd_disk->bdi);
412 kfree(bdev->bd_disk);
415 if (MAJOR(bdev->bd_dev) == BLOCK_EXT_MAJOR)
416 blk_free_ext_minor(MINOR(bdev->bd_dev));
418 kmem_cache_free(bdev_cachep, BDEV_I(inode));
421 static void init_once(void *data)
423 struct bdev_inode *ei = data;
425 inode_init_once(&ei->vfs_inode);
428 static void bdev_evict_inode(struct inode *inode)
430 truncate_inode_pages_final(&inode->i_data);
431 invalidate_inode_buffers(inode); /* is it needed here? */
435 static const struct super_operations bdev_sops = {
436 .statfs = simple_statfs,
437 .alloc_inode = bdev_alloc_inode,
438 .free_inode = bdev_free_inode,
439 .drop_inode = generic_delete_inode,
440 .evict_inode = bdev_evict_inode,
443 static int bd_init_fs_context(struct fs_context *fc)
445 struct pseudo_fs_context *ctx = init_pseudo(fc, BDEVFS_MAGIC);
448 fc->s_iflags |= SB_I_CGROUPWB;
449 ctx->ops = &bdev_sops;
453 static struct file_system_type bd_type = {
455 .init_fs_context = bd_init_fs_context,
456 .kill_sb = kill_anon_super,
459 struct super_block *blockdev_superblock __read_mostly;
460 EXPORT_SYMBOL_GPL(blockdev_superblock);
462 void __init bdev_cache_init(void)
465 static struct vfsmount *bd_mnt;
467 bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode),
468 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
469 SLAB_MEM_SPREAD|SLAB_ACCOUNT|SLAB_PANIC),
471 err = register_filesystem(&bd_type);
473 panic("Cannot register bdev pseudo-fs");
474 bd_mnt = kern_mount(&bd_type);
476 panic("Cannot create bdev pseudo-fs");
477 blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */
480 struct block_device *bdev_alloc(struct gendisk *disk, u8 partno)
482 struct block_device *bdev;
485 inode = new_inode(blockdev_superblock);
488 inode->i_mode = S_IFBLK;
490 inode->i_data.a_ops = &def_blk_aops;
491 mapping_set_gfp_mask(&inode->i_data, GFP_USER);
493 bdev = I_BDEV(inode);
494 mutex_init(&bdev->bd_fsfreeze_mutex);
495 spin_lock_init(&bdev->bd_size_lock);
496 bdev->bd_partno = partno;
497 bdev->bd_inode = inode;
498 bdev->bd_queue = disk->queue;
499 bdev->bd_stats = alloc_percpu(struct disk_stats);
500 if (!bdev->bd_stats) {
504 bdev->bd_disk = disk;
508 void bdev_add(struct block_device *bdev, dev_t dev)
511 bdev->bd_inode->i_rdev = dev;
512 bdev->bd_inode->i_ino = dev;
513 insert_inode_hash(bdev->bd_inode);
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 * @whole: whole block device containing @bdev, may equal @bdev
533 * @holder: holder trying to claim @bdev
535 * Test whether @bdev can be claimed by @holder.
538 * spin_lock(&bdev_lock).
541 * %true if @bdev can be claimed, %false otherwise.
543 static bool bd_may_claim(struct block_device *bdev, struct block_device *whole,
546 if (bdev->bd_holder == holder)
547 return true; /* already a holder */
548 else if (bdev->bd_holder != NULL)
549 return false; /* held by someone else */
550 else if (whole == bdev)
551 return true; /* is a whole device which isn't held */
553 else if (whole->bd_holder == bd_may_claim)
554 return true; /* is a partition of a device that is being partitioned */
555 else if (whole->bd_holder != NULL)
556 return false; /* is a partition of a held device */
558 return true; /* is a partition of an un-held device */
562 * bd_prepare_to_claim - claim a block device
563 * @bdev: block device of interest
564 * @holder: holder trying to claim @bdev
566 * Claim @bdev. This function fails if @bdev is already claimed by another
567 * holder and waits if another claiming is in progress. return, the caller
568 * has ownership of bd_claiming and bd_holder[s].
571 * 0 if @bdev can be claimed, -EBUSY otherwise.
573 int bd_prepare_to_claim(struct block_device *bdev, void *holder)
575 struct block_device *whole = bdev_whole(bdev);
577 if (WARN_ON_ONCE(!holder))
580 spin_lock(&bdev_lock);
581 /* if someone else claimed, fail */
582 if (!bd_may_claim(bdev, whole, holder)) {
583 spin_unlock(&bdev_lock);
587 /* if claiming is already in progress, wait for it to finish */
588 if (whole->bd_claiming) {
589 wait_queue_head_t *wq = bit_waitqueue(&whole->bd_claiming, 0);
592 prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
593 spin_unlock(&bdev_lock);
595 finish_wait(wq, &wait);
600 whole->bd_claiming = holder;
601 spin_unlock(&bdev_lock);
604 EXPORT_SYMBOL_GPL(bd_prepare_to_claim); /* only for the loop driver */
606 static void bd_clear_claiming(struct block_device *whole, void *holder)
608 lockdep_assert_held(&bdev_lock);
609 /* tell others that we're done */
610 BUG_ON(whole->bd_claiming != holder);
611 whole->bd_claiming = NULL;
612 wake_up_bit(&whole->bd_claiming, 0);
616 * bd_finish_claiming - finish claiming of a block device
617 * @bdev: block device of interest
618 * @holder: holder that has claimed @bdev
620 * Finish exclusive open of a block device. Mark the device as exlusively
621 * open by the holder and wake up all waiters for exclusive open to finish.
623 static void bd_finish_claiming(struct block_device *bdev, void *holder)
625 struct block_device *whole = bdev_whole(bdev);
627 spin_lock(&bdev_lock);
628 BUG_ON(!bd_may_claim(bdev, whole, holder));
630 * Note that for a whole device bd_holders will be incremented twice,
631 * and bd_holder will be set to bd_may_claim before being set to holder
634 whole->bd_holder = bd_may_claim;
636 bdev->bd_holder = holder;
637 bd_clear_claiming(whole, holder);
638 spin_unlock(&bdev_lock);
642 * bd_abort_claiming - abort claiming of a block device
643 * @bdev: block device of interest
644 * @holder: holder that has claimed @bdev
646 * Abort claiming of a block device when the exclusive open failed. This can be
647 * also used when exclusive open is not actually desired and we just needed
648 * to block other exclusive openers for a while.
650 void bd_abort_claiming(struct block_device *bdev, void *holder)
652 spin_lock(&bdev_lock);
653 bd_clear_claiming(bdev_whole(bdev), holder);
654 spin_unlock(&bdev_lock);
656 EXPORT_SYMBOL(bd_abort_claiming);
658 static void blkdev_flush_mapping(struct block_device *bdev)
660 WARN_ON_ONCE(bdev->bd_holders);
663 bdev_write_inode(bdev);
666 static int blkdev_get_whole(struct block_device *bdev, fmode_t mode)
668 struct gendisk *disk = bdev->bd_disk;
671 if (disk->fops->open) {
672 ret = disk->fops->open(bdev, mode);
674 /* avoid ghost partitions on a removed medium */
675 if (ret == -ENOMEDIUM &&
676 test_bit(GD_NEED_PART_SCAN, &disk->state))
677 bdev_disk_changed(disk, true);
682 if (!atomic_read(&bdev->bd_openers))
683 set_init_blocksize(bdev);
684 if (test_bit(GD_NEED_PART_SCAN, &disk->state))
685 bdev_disk_changed(disk, false);
686 atomic_inc(&bdev->bd_openers);
690 static void blkdev_put_whole(struct block_device *bdev, fmode_t mode)
692 if (atomic_dec_and_test(&bdev->bd_openers))
693 blkdev_flush_mapping(bdev);
694 if (bdev->bd_disk->fops->release)
695 bdev->bd_disk->fops->release(bdev->bd_disk, mode);
698 static int blkdev_get_part(struct block_device *part, fmode_t mode)
700 struct gendisk *disk = part->bd_disk;
703 if (atomic_read(&part->bd_openers))
706 ret = blkdev_get_whole(bdev_whole(part), mode);
711 if (!bdev_nr_sectors(part))
714 disk->open_partitions++;
715 set_init_blocksize(part);
717 atomic_inc(&part->bd_openers);
721 blkdev_put_whole(bdev_whole(part), mode);
725 static void blkdev_put_part(struct block_device *part, fmode_t mode)
727 struct block_device *whole = bdev_whole(part);
729 if (!atomic_dec_and_test(&part->bd_openers))
731 blkdev_flush_mapping(part);
732 whole->bd_disk->open_partitions--;
733 blkdev_put_whole(whole, mode);
736 struct block_device *blkdev_get_no_open(dev_t dev)
738 struct block_device *bdev;
741 inode = ilookup(blockdev_superblock, dev);
742 if (!inode && IS_ENABLED(CONFIG_BLOCK_LEGACY_AUTOLOAD)) {
743 blk_request_module(dev);
744 inode = ilookup(blockdev_superblock, dev);
747 "block device autoloading is deprecated and will be removed.\n");
752 /* switch from the inode reference to a device mode one: */
753 bdev = &BDEV_I(inode)->bdev;
754 if (!kobject_get_unless_zero(&bdev->bd_device.kobj))
760 void blkdev_put_no_open(struct block_device *bdev)
762 put_device(&bdev->bd_device);
766 * blkdev_get_by_dev - open a block device by device number
767 * @dev: device number of block device to open
768 * @mode: FMODE_* mask
769 * @holder: exclusive holder identifier
771 * Open the block device described by device number @dev. If @mode includes
772 * %FMODE_EXCL, the block device is opened with exclusive access. Specifying
773 * %FMODE_EXCL with a %NULL @holder is invalid. Exclusive opens may nest for
776 * Use this interface ONLY if you really do not have anything better - i.e. when
777 * you are behind a truly sucky interface and all you are given is a device
778 * number. Everything else should use blkdev_get_by_path().
784 * Reference to the block_device on success, ERR_PTR(-errno) on failure.
786 struct block_device *blkdev_get_by_dev(dev_t dev, fmode_t mode, void *holder)
788 bool unblock_events = true;
789 struct block_device *bdev;
790 struct gendisk *disk;
793 ret = devcgroup_check_permission(DEVCG_DEV_BLOCK,
794 MAJOR(dev), MINOR(dev),
795 ((mode & FMODE_READ) ? DEVCG_ACC_READ : 0) |
796 ((mode & FMODE_WRITE) ? DEVCG_ACC_WRITE : 0));
800 bdev = blkdev_get_no_open(dev);
802 return ERR_PTR(-ENXIO);
803 disk = bdev->bd_disk;
805 if (mode & FMODE_EXCL) {
806 ret = bd_prepare_to_claim(bdev, holder);
811 disk_block_events(disk);
813 mutex_lock(&disk->open_mutex);
815 if (!disk_live(disk))
817 if (!try_module_get(disk->fops->owner))
819 if (bdev_is_partition(bdev))
820 ret = blkdev_get_part(bdev, mode);
822 ret = blkdev_get_whole(bdev, mode);
825 if (mode & FMODE_EXCL) {
826 bd_finish_claiming(bdev, holder);
829 * Block event polling for write claims if requested. Any write
830 * holder makes the write_holder state stick until all are
831 * released. This is good enough and tracking individual
832 * writeable reference is too fragile given the way @mode is
833 * used in blkdev_get/put().
835 if ((mode & FMODE_WRITE) && !bdev->bd_write_holder &&
836 (disk->event_flags & DISK_EVENT_FLAG_BLOCK_ON_EXCL_WRITE)) {
837 bdev->bd_write_holder = true;
838 unblock_events = false;
841 mutex_unlock(&disk->open_mutex);
844 disk_unblock_events(disk);
847 module_put(disk->fops->owner);
849 if (mode & FMODE_EXCL)
850 bd_abort_claiming(bdev, holder);
851 mutex_unlock(&disk->open_mutex);
852 disk_unblock_events(disk);
854 blkdev_put_no_open(bdev);
857 EXPORT_SYMBOL(blkdev_get_by_dev);
860 * blkdev_get_by_path - open a block device by name
861 * @path: path to the block device to open
862 * @mode: FMODE_* mask
863 * @holder: exclusive holder identifier
865 * Open the block device described by the device file at @path. If @mode
866 * includes %FMODE_EXCL, the block device is opened with exclusive access.
867 * Specifying %FMODE_EXCL with a %NULL @holder is invalid. Exclusive opens may
868 * nest for the same @holder.
874 * Reference to the block_device on success, ERR_PTR(-errno) on failure.
876 struct block_device *blkdev_get_by_path(const char *path, fmode_t mode,
879 struct block_device *bdev;
883 error = lookup_bdev(path, &dev);
885 return ERR_PTR(error);
887 bdev = blkdev_get_by_dev(dev, mode, holder);
888 if (!IS_ERR(bdev) && (mode & FMODE_WRITE) && bdev_read_only(bdev)) {
889 blkdev_put(bdev, mode);
890 return ERR_PTR(-EACCES);
895 EXPORT_SYMBOL(blkdev_get_by_path);
897 void blkdev_put(struct block_device *bdev, fmode_t mode)
899 struct gendisk *disk = bdev->bd_disk;
902 * Sync early if it looks like we're the last one. If someone else
903 * opens the block device between now and the decrement of bd_openers
904 * then we did a sync that we didn't need to, but that's not the end
905 * of the world and we want to avoid long (could be several minute)
906 * syncs while holding the mutex.
908 if (atomic_read(&bdev->bd_openers) == 1)
911 mutex_lock(&disk->open_mutex);
912 if (mode & FMODE_EXCL) {
913 struct block_device *whole = bdev_whole(bdev);
917 * Release a claim on the device. The holder fields
918 * are protected with bdev_lock. open_mutex is to
919 * synchronize disk_holder unlinking.
921 spin_lock(&bdev_lock);
923 WARN_ON_ONCE(--bdev->bd_holders < 0);
924 WARN_ON_ONCE(--whole->bd_holders < 0);
926 if ((bdev_free = !bdev->bd_holders))
927 bdev->bd_holder = NULL;
928 if (!whole->bd_holders)
929 whole->bd_holder = NULL;
931 spin_unlock(&bdev_lock);
934 * If this was the last claim, remove holder link and
935 * unblock evpoll if it was a write holder.
937 if (bdev_free && bdev->bd_write_holder) {
938 disk_unblock_events(disk);
939 bdev->bd_write_holder = false;
944 * Trigger event checking and tell drivers to flush MEDIA_CHANGE
945 * event. This is to ensure detection of media removal commanded
946 * from userland - e.g. eject(1).
948 disk_flush_events(disk, DISK_EVENT_MEDIA_CHANGE);
950 if (bdev_is_partition(bdev))
951 blkdev_put_part(bdev, mode);
953 blkdev_put_whole(bdev, mode);
954 mutex_unlock(&disk->open_mutex);
956 module_put(disk->fops->owner);
957 blkdev_put_no_open(bdev);
959 EXPORT_SYMBOL(blkdev_put);
962 * lookup_bdev() - Look up a struct block_device by name.
963 * @pathname: Name of the block device in the filesystem.
964 * @dev: Pointer to the block device's dev_t, if found.
966 * Lookup the block device's dev_t at @pathname in the current
967 * namespace if possible and return it in @dev.
969 * Context: May sleep.
970 * Return: 0 if succeeded, negative errno otherwise.
972 int lookup_bdev(const char *pathname, dev_t *dev)
978 if (!pathname || !*pathname)
981 error = kern_path(pathname, LOOKUP_FOLLOW, &path);
985 inode = d_backing_inode(path.dentry);
987 if (!S_ISBLK(inode->i_mode))
990 if (!may_open_dev(&path))
993 *dev = inode->i_rdev;
999 EXPORT_SYMBOL(lookup_bdev);
1001 int __invalidate_device(struct block_device *bdev, bool kill_dirty)
1003 struct super_block *sb = get_super(bdev);
1008 * no need to lock the super, get_super holds the
1009 * read mutex so the filesystem cannot go away
1010 * under us (->put_super runs with the write lock
1013 shrink_dcache_sb(sb);
1014 res = invalidate_inodes(sb, kill_dirty);
1017 invalidate_bdev(bdev);
1020 EXPORT_SYMBOL(__invalidate_device);
1022 void sync_bdevs(bool wait)
1024 struct inode *inode, *old_inode = NULL;
1026 spin_lock(&blockdev_superblock->s_inode_list_lock);
1027 list_for_each_entry(inode, &blockdev_superblock->s_inodes, i_sb_list) {
1028 struct address_space *mapping = inode->i_mapping;
1029 struct block_device *bdev;
1031 spin_lock(&inode->i_lock);
1032 if (inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW) ||
1033 mapping->nrpages == 0) {
1034 spin_unlock(&inode->i_lock);
1038 spin_unlock(&inode->i_lock);
1039 spin_unlock(&blockdev_superblock->s_inode_list_lock);
1041 * We hold a reference to 'inode' so it couldn't have been
1042 * removed from s_inodes list while we dropped the
1043 * s_inode_list_lock We cannot iput the inode now as we can
1044 * be holding the last reference and we cannot iput it under
1045 * s_inode_list_lock. So we keep the reference and iput it
1050 bdev = I_BDEV(inode);
1052 mutex_lock(&bdev->bd_disk->open_mutex);
1053 if (!atomic_read(&bdev->bd_openers)) {
1057 * We keep the error status of individual mapping so
1058 * that applications can catch the writeback error using
1059 * fsync(2). See filemap_fdatawait_keep_errors() for
1062 filemap_fdatawait_keep_errors(inode->i_mapping);
1064 filemap_fdatawrite(inode->i_mapping);
1066 mutex_unlock(&bdev->bd_disk->open_mutex);
1068 spin_lock(&blockdev_superblock->s_inode_list_lock);
1070 spin_unlock(&blockdev_superblock->s_inode_list_lock);
1075 * Handle STATX_DIOALIGN for block devices.
1077 * Note that the inode passed to this is the inode of a block device node file,
1078 * not the block device's internal inode. Therefore it is *not* valid to use
1079 * I_BDEV() here; the block device has to be looked up by i_rdev instead.
1081 void bdev_statx_dioalign(struct inode *inode, struct kstat *stat)
1083 struct block_device *bdev;
1085 bdev = blkdev_get_no_open(inode->i_rdev);
1089 stat->dio_mem_align = bdev_dma_alignment(bdev) + 1;
1090 stat->dio_offset_align = bdev_logical_block_size(bdev);
1091 stat->result_mask |= STATX_DIOALIGN;
1093 blkdev_put_no_open(bdev);