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 "../fs/internal.h"
33 struct block_device bdev;
34 struct inode vfs_inode;
37 static inline struct bdev_inode *BDEV_I(struct inode *inode)
39 return container_of(inode, struct bdev_inode, vfs_inode);
42 struct block_device *I_BDEV(struct inode *inode)
44 return &BDEV_I(inode)->bdev;
46 EXPORT_SYMBOL(I_BDEV);
48 static void bdev_write_inode(struct block_device *bdev)
50 struct inode *inode = bdev->bd_inode;
53 spin_lock(&inode->i_lock);
54 while (inode->i_state & I_DIRTY) {
55 spin_unlock(&inode->i_lock);
56 ret = write_inode_now(inode, true);
59 "VFS: Dirty inode writeback failed for block device %pg (err=%d).\n",
61 spin_lock(&inode->i_lock);
63 spin_unlock(&inode->i_lock);
66 /* Kill _all_ buffers and pagecache , dirty or not.. */
67 static void kill_bdev(struct block_device *bdev)
69 struct address_space *mapping = bdev->bd_inode->i_mapping;
71 if (mapping_empty(mapping))
75 truncate_inode_pages(mapping, 0);
78 /* Invalidate clean unused buffers and pagecache. */
79 void invalidate_bdev(struct block_device *bdev)
81 struct address_space *mapping = bdev->bd_inode->i_mapping;
83 if (mapping->nrpages) {
85 lru_add_drain_all(); /* make sure all lru add caches are flushed */
86 invalidate_mapping_pages(mapping, 0, -1);
89 EXPORT_SYMBOL(invalidate_bdev);
92 * Drop all buffers & page cache for given bdev range. This function bails
93 * with error if bdev has other exclusive owner (such as filesystem).
95 int truncate_bdev_range(struct block_device *bdev, fmode_t mode,
96 loff_t lstart, loff_t lend)
99 * If we don't hold exclusive handle for the device, upgrade to it
100 * while we discard the buffer cache to avoid discarding buffers
101 * under live filesystem.
103 if (!(mode & FMODE_EXCL)) {
104 int err = bd_prepare_to_claim(bdev, truncate_bdev_range);
109 truncate_inode_pages_range(bdev->bd_inode->i_mapping, lstart, lend);
110 if (!(mode & FMODE_EXCL))
111 bd_abort_claiming(bdev, truncate_bdev_range);
116 * Someone else has handle exclusively open. Try invalidating instead.
117 * The 'end' argument is inclusive so the rounding is safe.
119 return invalidate_inode_pages2_range(bdev->bd_inode->i_mapping,
120 lstart >> PAGE_SHIFT,
124 static void set_init_blocksize(struct block_device *bdev)
126 unsigned int bsize = bdev_logical_block_size(bdev);
127 loff_t size = i_size_read(bdev->bd_inode);
129 while (bsize < PAGE_SIZE) {
134 bdev->bd_inode->i_blkbits = blksize_bits(bsize);
137 int set_blocksize(struct block_device *bdev, int size)
139 /* Size must be a power of two, and between 512 and PAGE_SIZE */
140 if (size > PAGE_SIZE || size < 512 || !is_power_of_2(size))
143 /* Size cannot be smaller than the size supported by the device */
144 if (size < bdev_logical_block_size(bdev))
147 /* Don't change the size if it is same as current */
148 if (bdev->bd_inode->i_blkbits != blksize_bits(size)) {
150 bdev->bd_inode->i_blkbits = blksize_bits(size);
156 EXPORT_SYMBOL(set_blocksize);
158 int sb_set_blocksize(struct super_block *sb, int size)
160 if (set_blocksize(sb->s_bdev, size))
162 /* If we get here, we know size is power of two
163 * and it's value is between 512 and PAGE_SIZE */
164 sb->s_blocksize = size;
165 sb->s_blocksize_bits = blksize_bits(size);
166 return sb->s_blocksize;
169 EXPORT_SYMBOL(sb_set_blocksize);
171 int sb_min_blocksize(struct super_block *sb, int size)
173 int minsize = bdev_logical_block_size(sb->s_bdev);
176 return sb_set_blocksize(sb, size);
179 EXPORT_SYMBOL(sb_min_blocksize);
181 int sync_blockdev_nowait(struct block_device *bdev)
185 return filemap_flush(bdev->bd_inode->i_mapping);
187 EXPORT_SYMBOL_GPL(sync_blockdev_nowait);
190 * Write out and wait upon all the dirty data associated with a block
191 * device via its mapping. Does not take the superblock lock.
193 int sync_blockdev(struct block_device *bdev)
197 return filemap_write_and_wait(bdev->bd_inode->i_mapping);
199 EXPORT_SYMBOL(sync_blockdev);
201 int sync_blockdev_range(struct block_device *bdev, loff_t lstart, loff_t lend)
203 return filemap_write_and_wait_range(bdev->bd_inode->i_mapping,
206 EXPORT_SYMBOL(sync_blockdev_range);
209 * Write out and wait upon all dirty data associated with this
210 * device. Filesystem data as well as the underlying block
211 * device. Takes the superblock lock.
213 int fsync_bdev(struct block_device *bdev)
215 struct super_block *sb = get_super(bdev);
217 int res = sync_filesystem(sb);
221 return sync_blockdev(bdev);
223 EXPORT_SYMBOL(fsync_bdev);
226 * freeze_bdev -- lock a filesystem and force it into a consistent state
227 * @bdev: blockdevice to lock
229 * If a superblock is found on this device, we take the s_umount semaphore
230 * on it to make sure nobody unmounts until the snapshot creation is done.
231 * The reference counter (bd_fsfreeze_count) guarantees that only the last
232 * unfreeze process can unfreeze the frozen filesystem actually when multiple
233 * freeze requests arrive simultaneously. It counts up in freeze_bdev() and
234 * count down in thaw_bdev(). When it becomes 0, thaw_bdev() will unfreeze
237 int freeze_bdev(struct block_device *bdev)
239 struct super_block *sb;
242 mutex_lock(&bdev->bd_fsfreeze_mutex);
243 if (++bdev->bd_fsfreeze_count > 1)
246 sb = get_active_super(bdev);
249 if (sb->s_op->freeze_super)
250 error = sb->s_op->freeze_super(sb);
252 error = freeze_super(sb);
253 deactivate_super(sb);
256 bdev->bd_fsfreeze_count--;
259 bdev->bd_fsfreeze_sb = sb;
264 mutex_unlock(&bdev->bd_fsfreeze_mutex);
267 EXPORT_SYMBOL(freeze_bdev);
270 * thaw_bdev -- unlock filesystem
271 * @bdev: blockdevice to unlock
273 * Unlocks the filesystem and marks it writeable again after freeze_bdev().
275 int thaw_bdev(struct block_device *bdev)
277 struct super_block *sb;
280 mutex_lock(&bdev->bd_fsfreeze_mutex);
281 if (!bdev->bd_fsfreeze_count)
285 if (--bdev->bd_fsfreeze_count > 0)
288 sb = bdev->bd_fsfreeze_sb;
292 if (sb->s_op->thaw_super)
293 error = sb->s_op->thaw_super(sb);
295 error = thaw_super(sb);
297 bdev->bd_fsfreeze_count++;
299 bdev->bd_fsfreeze_sb = NULL;
301 mutex_unlock(&bdev->bd_fsfreeze_mutex);
304 EXPORT_SYMBOL(thaw_bdev);
307 * bdev_read_page() - Start reading a page from a block device
308 * @bdev: The device to read the page from
309 * @sector: The offset on the device to read the page to (need not be aligned)
310 * @page: The page to read
312 * On entry, the page should be locked. It will be unlocked when the page
313 * has been read. If the block driver implements rw_page synchronously,
314 * that will be true on exit from this function, but it need not be.
316 * Errors returned by this function are usually "soft", eg out of memory, or
317 * queue full; callers should try a different route to read this page rather
318 * than propagate an error back up the stack.
320 * Return: negative errno if an error occurs, 0 if submission was successful.
322 int bdev_read_page(struct block_device *bdev, sector_t sector,
325 const struct block_device_operations *ops = bdev->bd_disk->fops;
326 int result = -EOPNOTSUPP;
328 if (!ops->rw_page || bdev_get_integrity(bdev))
331 result = blk_queue_enter(bdev_get_queue(bdev), 0);
334 result = ops->rw_page(bdev, sector + get_start_sect(bdev), page,
336 blk_queue_exit(bdev_get_queue(bdev));
341 * bdev_write_page() - Start writing a page to a block device
342 * @bdev: The device to write the page to
343 * @sector: The offset on the device to write the page to (need not be aligned)
344 * @page: The page to write
345 * @wbc: The writeback_control for the write
347 * On entry, the page should be locked and not currently under writeback.
348 * On exit, if the write started successfully, the page will be unlocked and
349 * under writeback. If the write failed already (eg the driver failed to
350 * queue the page to the device), the page will still be locked. If the
351 * caller is a ->writepage implementation, it will need to unlock the page.
353 * Errors returned by this function are usually "soft", eg out of memory, or
354 * queue full; callers should try a different route to write this page rather
355 * than propagate an error back up the stack.
357 * Return: negative errno if an error occurs, 0 if submission was successful.
359 int bdev_write_page(struct block_device *bdev, sector_t sector,
360 struct page *page, struct writeback_control *wbc)
363 const struct block_device_operations *ops = bdev->bd_disk->fops;
365 if (!ops->rw_page || bdev_get_integrity(bdev))
367 result = blk_queue_enter(bdev_get_queue(bdev), 0);
371 set_page_writeback(page);
372 result = ops->rw_page(bdev, sector + get_start_sect(bdev), page,
375 end_page_writeback(page);
377 clean_page_buffers(page);
380 blk_queue_exit(bdev_get_queue(bdev));
388 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock);
389 static struct kmem_cache * bdev_cachep __read_mostly;
391 static struct inode *bdev_alloc_inode(struct super_block *sb)
393 struct bdev_inode *ei = alloc_inode_sb(sb, bdev_cachep, GFP_KERNEL);
397 memset(&ei->bdev, 0, sizeof(ei->bdev));
398 return &ei->vfs_inode;
401 static void bdev_free_inode(struct inode *inode)
403 struct block_device *bdev = I_BDEV(inode);
405 free_percpu(bdev->bd_stats);
406 kfree(bdev->bd_meta_info);
408 if (!bdev_is_partition(bdev)) {
409 if (bdev->bd_disk && bdev->bd_disk->bdi)
410 bdi_put(bdev->bd_disk->bdi);
411 kfree(bdev->bd_disk);
414 if (MAJOR(bdev->bd_dev) == BLOCK_EXT_MAJOR)
415 blk_free_ext_minor(MINOR(bdev->bd_dev));
417 kmem_cache_free(bdev_cachep, BDEV_I(inode));
420 static void init_once(void *data)
422 struct bdev_inode *ei = data;
424 inode_init_once(&ei->vfs_inode);
427 static void bdev_evict_inode(struct inode *inode)
429 truncate_inode_pages_final(&inode->i_data);
430 invalidate_inode_buffers(inode); /* is it needed here? */
434 static const struct super_operations bdev_sops = {
435 .statfs = simple_statfs,
436 .alloc_inode = bdev_alloc_inode,
437 .free_inode = bdev_free_inode,
438 .drop_inode = generic_delete_inode,
439 .evict_inode = bdev_evict_inode,
442 static int bd_init_fs_context(struct fs_context *fc)
444 struct pseudo_fs_context *ctx = init_pseudo(fc, BDEVFS_MAGIC);
447 fc->s_iflags |= SB_I_CGROUPWB;
448 ctx->ops = &bdev_sops;
452 static struct file_system_type bd_type = {
454 .init_fs_context = bd_init_fs_context,
455 .kill_sb = kill_anon_super,
458 struct super_block *blockdev_superblock __read_mostly;
459 EXPORT_SYMBOL_GPL(blockdev_superblock);
461 void __init bdev_cache_init(void)
464 static struct vfsmount *bd_mnt;
466 bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode),
467 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
468 SLAB_MEM_SPREAD|SLAB_ACCOUNT|SLAB_PANIC),
470 err = register_filesystem(&bd_type);
472 panic("Cannot register bdev pseudo-fs");
473 bd_mnt = kern_mount(&bd_type);
475 panic("Cannot create bdev pseudo-fs");
476 blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */
479 struct block_device *bdev_alloc(struct gendisk *disk, u8 partno)
481 struct block_device *bdev;
484 inode = new_inode(blockdev_superblock);
487 inode->i_mode = S_IFBLK;
489 inode->i_data.a_ops = &def_blk_aops;
490 mapping_set_gfp_mask(&inode->i_data, GFP_USER);
492 bdev = I_BDEV(inode);
493 mutex_init(&bdev->bd_fsfreeze_mutex);
494 spin_lock_init(&bdev->bd_size_lock);
495 bdev->bd_partno = partno;
496 bdev->bd_inode = inode;
497 bdev->bd_queue = disk->queue;
498 bdev->bd_stats = alloc_percpu(struct disk_stats);
499 if (!bdev->bd_stats) {
503 bdev->bd_disk = disk;
507 void bdev_add(struct block_device *bdev, dev_t dev)
510 bdev->bd_inode->i_rdev = dev;
511 bdev->bd_inode->i_ino = dev;
512 insert_inode_hash(bdev->bd_inode);
515 long nr_blockdev_pages(void)
520 spin_lock(&blockdev_superblock->s_inode_list_lock);
521 list_for_each_entry(inode, &blockdev_superblock->s_inodes, i_sb_list)
522 ret += inode->i_mapping->nrpages;
523 spin_unlock(&blockdev_superblock->s_inode_list_lock);
529 * bd_may_claim - test whether a block device can be claimed
530 * @bdev: block device of interest
531 * @whole: whole block device containing @bdev, may equal @bdev
532 * @holder: holder trying to claim @bdev
534 * Test whether @bdev can be claimed by @holder.
537 * spin_lock(&bdev_lock).
540 * %true if @bdev can be claimed, %false otherwise.
542 static bool bd_may_claim(struct block_device *bdev, struct block_device *whole,
545 if (bdev->bd_holder == holder)
546 return true; /* already a holder */
547 else if (bdev->bd_holder != NULL)
548 return false; /* held by someone else */
549 else if (whole == bdev)
550 return true; /* is a whole device which isn't held */
552 else if (whole->bd_holder == bd_may_claim)
553 return true; /* is a partition of a device that is being partitioned */
554 else if (whole->bd_holder != NULL)
555 return false; /* is a partition of a held device */
557 return true; /* is a partition of an un-held device */
561 * bd_prepare_to_claim - claim a block device
562 * @bdev: block device of interest
563 * @holder: holder trying to claim @bdev
565 * Claim @bdev. This function fails if @bdev is already claimed by another
566 * holder and waits if another claiming is in progress. return, the caller
567 * has ownership of bd_claiming and bd_holder[s].
570 * 0 if @bdev can be claimed, -EBUSY otherwise.
572 int bd_prepare_to_claim(struct block_device *bdev, void *holder)
574 struct block_device *whole = bdev_whole(bdev);
576 if (WARN_ON_ONCE(!holder))
579 spin_lock(&bdev_lock);
580 /* if someone else claimed, fail */
581 if (!bd_may_claim(bdev, whole, holder)) {
582 spin_unlock(&bdev_lock);
586 /* if claiming is already in progress, wait for it to finish */
587 if (whole->bd_claiming) {
588 wait_queue_head_t *wq = bit_waitqueue(&whole->bd_claiming, 0);
591 prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
592 spin_unlock(&bdev_lock);
594 finish_wait(wq, &wait);
599 whole->bd_claiming = holder;
600 spin_unlock(&bdev_lock);
603 EXPORT_SYMBOL_GPL(bd_prepare_to_claim); /* only for the loop driver */
605 static void bd_clear_claiming(struct block_device *whole, void *holder)
607 lockdep_assert_held(&bdev_lock);
608 /* tell others that we're done */
609 BUG_ON(whole->bd_claiming != holder);
610 whole->bd_claiming = NULL;
611 wake_up_bit(&whole->bd_claiming, 0);
615 * bd_finish_claiming - finish claiming of a block device
616 * @bdev: block device of interest
617 * @holder: holder that has claimed @bdev
619 * Finish exclusive open of a block device. Mark the device as exlusively
620 * open by the holder and wake up all waiters for exclusive open to finish.
622 static void bd_finish_claiming(struct block_device *bdev, void *holder)
624 struct block_device *whole = bdev_whole(bdev);
626 spin_lock(&bdev_lock);
627 BUG_ON(!bd_may_claim(bdev, whole, holder));
629 * Note that for a whole device bd_holders will be incremented twice,
630 * and bd_holder will be set to bd_may_claim before being set to holder
633 whole->bd_holder = bd_may_claim;
635 bdev->bd_holder = holder;
636 bd_clear_claiming(whole, holder);
637 spin_unlock(&bdev_lock);
641 * bd_abort_claiming - abort claiming of a block device
642 * @bdev: block device of interest
643 * @holder: holder that has claimed @bdev
645 * Abort claiming of a block device when the exclusive open failed. This can be
646 * also used when exclusive open is not actually desired and we just needed
647 * to block other exclusive openers for a while.
649 void bd_abort_claiming(struct block_device *bdev, void *holder)
651 spin_lock(&bdev_lock);
652 bd_clear_claiming(bdev_whole(bdev), holder);
653 spin_unlock(&bdev_lock);
655 EXPORT_SYMBOL(bd_abort_claiming);
657 static void blkdev_flush_mapping(struct block_device *bdev)
659 WARN_ON_ONCE(bdev->bd_holders);
662 bdev_write_inode(bdev);
665 static int blkdev_get_whole(struct block_device *bdev, fmode_t mode)
667 struct gendisk *disk = bdev->bd_disk;
670 if (disk->fops->open) {
671 ret = disk->fops->open(bdev, mode);
673 /* avoid ghost partitions on a removed medium */
674 if (ret == -ENOMEDIUM &&
675 test_bit(GD_NEED_PART_SCAN, &disk->state))
676 bdev_disk_changed(disk, true);
681 if (!atomic_read(&bdev->bd_openers))
682 set_init_blocksize(bdev);
683 if (test_bit(GD_NEED_PART_SCAN, &disk->state))
684 bdev_disk_changed(disk, false);
685 atomic_inc(&bdev->bd_openers);
689 static void blkdev_put_whole(struct block_device *bdev, fmode_t mode)
691 if (atomic_dec_and_test(&bdev->bd_openers))
692 blkdev_flush_mapping(bdev);
693 if (bdev->bd_disk->fops->release)
694 bdev->bd_disk->fops->release(bdev->bd_disk, mode);
697 static int blkdev_get_part(struct block_device *part, fmode_t mode)
699 struct gendisk *disk = part->bd_disk;
702 if (atomic_read(&part->bd_openers))
705 ret = blkdev_get_whole(bdev_whole(part), mode);
710 if (!bdev_nr_sectors(part))
713 disk->open_partitions++;
714 set_init_blocksize(part);
716 atomic_inc(&part->bd_openers);
720 blkdev_put_whole(bdev_whole(part), mode);
724 static void blkdev_put_part(struct block_device *part, fmode_t mode)
726 struct block_device *whole = bdev_whole(part);
728 if (!atomic_dec_and_test(&part->bd_openers))
730 blkdev_flush_mapping(part);
731 whole->bd_disk->open_partitions--;
732 blkdev_put_whole(whole, mode);
735 struct block_device *blkdev_get_no_open(dev_t dev)
737 struct block_device *bdev;
740 inode = ilookup(blockdev_superblock, dev);
741 if (!inode && IS_ENABLED(CONFIG_BLOCK_LEGACY_AUTOLOAD)) {
742 blk_request_module(dev);
743 inode = ilookup(blockdev_superblock, dev);
746 "block device autoloading is deprecated and will be removed.\n");
751 /* switch from the inode reference to a device mode one: */
752 bdev = &BDEV_I(inode)->bdev;
753 if (!kobject_get_unless_zero(&bdev->bd_device.kobj))
759 void blkdev_put_no_open(struct block_device *bdev)
761 put_device(&bdev->bd_device);
765 * blkdev_get_by_dev - open a block device by device number
766 * @dev: device number of block device to open
767 * @mode: FMODE_* mask
768 * @holder: exclusive holder identifier
770 * Open the block device described by device number @dev. If @mode includes
771 * %FMODE_EXCL, the block device is opened with exclusive access. Specifying
772 * %FMODE_EXCL with a %NULL @holder is invalid. Exclusive opens may nest for
775 * Use this interface ONLY if you really do not have anything better - i.e. when
776 * you are behind a truly sucky interface and all you are given is a device
777 * number. Everything else should use blkdev_get_by_path().
783 * Reference to the block_device on success, ERR_PTR(-errno) on failure.
785 struct block_device *blkdev_get_by_dev(dev_t dev, fmode_t mode, void *holder)
787 bool unblock_events = true;
788 struct block_device *bdev;
789 struct gendisk *disk;
792 ret = devcgroup_check_permission(DEVCG_DEV_BLOCK,
793 MAJOR(dev), MINOR(dev),
794 ((mode & FMODE_READ) ? DEVCG_ACC_READ : 0) |
795 ((mode & FMODE_WRITE) ? DEVCG_ACC_WRITE : 0));
799 bdev = blkdev_get_no_open(dev);
801 return ERR_PTR(-ENXIO);
802 disk = bdev->bd_disk;
804 if (mode & FMODE_EXCL) {
805 ret = bd_prepare_to_claim(bdev, holder);
810 disk_block_events(disk);
812 mutex_lock(&disk->open_mutex);
814 if (!disk_live(disk))
816 if (!try_module_get(disk->fops->owner))
818 if (bdev_is_partition(bdev))
819 ret = blkdev_get_part(bdev, mode);
821 ret = blkdev_get_whole(bdev, mode);
824 if (mode & FMODE_EXCL) {
825 bd_finish_claiming(bdev, holder);
828 * Block event polling for write claims if requested. Any write
829 * holder makes the write_holder state stick until all are
830 * released. This is good enough and tracking individual
831 * writeable reference is too fragile given the way @mode is
832 * used in blkdev_get/put().
834 if ((mode & FMODE_WRITE) && !bdev->bd_write_holder &&
835 (disk->event_flags & DISK_EVENT_FLAG_BLOCK_ON_EXCL_WRITE)) {
836 bdev->bd_write_holder = true;
837 unblock_events = false;
840 mutex_unlock(&disk->open_mutex);
843 disk_unblock_events(disk);
846 module_put(disk->fops->owner);
848 if (mode & FMODE_EXCL)
849 bd_abort_claiming(bdev, holder);
850 mutex_unlock(&disk->open_mutex);
851 disk_unblock_events(disk);
853 blkdev_put_no_open(bdev);
856 EXPORT_SYMBOL(blkdev_get_by_dev);
859 * blkdev_get_by_path - open a block device by name
860 * @path: path to the block device to open
861 * @mode: FMODE_* mask
862 * @holder: exclusive holder identifier
864 * Open the block device described by the device file at @path. If @mode
865 * includes %FMODE_EXCL, the block device is opened with exclusive access.
866 * Specifying %FMODE_EXCL with a %NULL @holder is invalid. Exclusive opens may
867 * nest for the same @holder.
873 * Reference to the block_device on success, ERR_PTR(-errno) on failure.
875 struct block_device *blkdev_get_by_path(const char *path, fmode_t mode,
878 struct block_device *bdev;
882 error = lookup_bdev(path, &dev);
884 return ERR_PTR(error);
886 bdev = blkdev_get_by_dev(dev, mode, holder);
887 if (!IS_ERR(bdev) && (mode & FMODE_WRITE) && bdev_read_only(bdev)) {
888 blkdev_put(bdev, mode);
889 return ERR_PTR(-EACCES);
894 EXPORT_SYMBOL(blkdev_get_by_path);
896 void blkdev_put(struct block_device *bdev, fmode_t mode)
898 struct gendisk *disk = bdev->bd_disk;
901 * Sync early if it looks like we're the last one. If someone else
902 * opens the block device between now and the decrement of bd_openers
903 * then we did a sync that we didn't need to, but that's not the end
904 * of the world and we want to avoid long (could be several minute)
905 * syncs while holding the mutex.
907 if (atomic_read(&bdev->bd_openers) == 1)
910 mutex_lock(&disk->open_mutex);
911 if (mode & FMODE_EXCL) {
912 struct block_device *whole = bdev_whole(bdev);
916 * Release a claim on the device. The holder fields
917 * are protected with bdev_lock. open_mutex is to
918 * synchronize disk_holder unlinking.
920 spin_lock(&bdev_lock);
922 WARN_ON_ONCE(--bdev->bd_holders < 0);
923 WARN_ON_ONCE(--whole->bd_holders < 0);
925 if ((bdev_free = !bdev->bd_holders))
926 bdev->bd_holder = NULL;
927 if (!whole->bd_holders)
928 whole->bd_holder = NULL;
930 spin_unlock(&bdev_lock);
933 * If this was the last claim, remove holder link and
934 * unblock evpoll if it was a write holder.
936 if (bdev_free && bdev->bd_write_holder) {
937 disk_unblock_events(disk);
938 bdev->bd_write_holder = false;
943 * Trigger event checking and tell drivers to flush MEDIA_CHANGE
944 * event. This is to ensure detection of media removal commanded
945 * from userland - e.g. eject(1).
947 disk_flush_events(disk, DISK_EVENT_MEDIA_CHANGE);
949 if (bdev_is_partition(bdev))
950 blkdev_put_part(bdev, mode);
952 blkdev_put_whole(bdev, mode);
953 mutex_unlock(&disk->open_mutex);
955 module_put(disk->fops->owner);
956 blkdev_put_no_open(bdev);
958 EXPORT_SYMBOL(blkdev_put);
961 * lookup_bdev() - Look up a struct block_device by name.
962 * @pathname: Name of the block device in the filesystem.
963 * @dev: Pointer to the block device's dev_t, if found.
965 * Lookup the block device's dev_t at @pathname in the current
966 * namespace if possible and return it in @dev.
968 * Context: May sleep.
969 * Return: 0 if succeeded, negative errno otherwise.
971 int lookup_bdev(const char *pathname, dev_t *dev)
977 if (!pathname || !*pathname)
980 error = kern_path(pathname, LOOKUP_FOLLOW, &path);
984 inode = d_backing_inode(path.dentry);
986 if (!S_ISBLK(inode->i_mode))
989 if (!may_open_dev(&path))
992 *dev = inode->i_rdev;
998 EXPORT_SYMBOL(lookup_bdev);
1000 int __invalidate_device(struct block_device *bdev, bool kill_dirty)
1002 struct super_block *sb = get_super(bdev);
1007 * no need to lock the super, get_super holds the
1008 * read mutex so the filesystem cannot go away
1009 * under us (->put_super runs with the write lock
1012 shrink_dcache_sb(sb);
1013 res = invalidate_inodes(sb, kill_dirty);
1016 invalidate_bdev(bdev);
1019 EXPORT_SYMBOL(__invalidate_device);
1021 void sync_bdevs(bool wait)
1023 struct inode *inode, *old_inode = NULL;
1025 spin_lock(&blockdev_superblock->s_inode_list_lock);
1026 list_for_each_entry(inode, &blockdev_superblock->s_inodes, i_sb_list) {
1027 struct address_space *mapping = inode->i_mapping;
1028 struct block_device *bdev;
1030 spin_lock(&inode->i_lock);
1031 if (inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW) ||
1032 mapping->nrpages == 0) {
1033 spin_unlock(&inode->i_lock);
1037 spin_unlock(&inode->i_lock);
1038 spin_unlock(&blockdev_superblock->s_inode_list_lock);
1040 * We hold a reference to 'inode' so it couldn't have been
1041 * removed from s_inodes list while we dropped the
1042 * s_inode_list_lock We cannot iput the inode now as we can
1043 * be holding the last reference and we cannot iput it under
1044 * s_inode_list_lock. So we keep the reference and iput it
1049 bdev = I_BDEV(inode);
1051 mutex_lock(&bdev->bd_disk->open_mutex);
1052 if (!atomic_read(&bdev->bd_openers)) {
1056 * We keep the error status of individual mapping so
1057 * that applications can catch the writeback error using
1058 * fsync(2). See filemap_fdatawait_keep_errors() for
1061 filemap_fdatawait_keep_errors(inode->i_mapping);
1063 filemap_fdatawrite(inode->i_mapping);
1065 mutex_unlock(&bdev->bd_disk->open_mutex);
1067 spin_lock(&blockdev_superblock->s_inode_list_lock);
1069 spin_unlock(&blockdev_superblock->s_inode_list_lock);