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/backing-dev.h>
16 #include <linux/module.h>
17 #include <linux/blkpg.h>
18 #include <linux/magic.h>
19 #include <linux/buffer_head.h>
20 #include <linux/swap.h>
21 #include <linux/writeback.h>
22 #include <linux/mount.h>
23 #include <linux/pseudo_fs.h>
24 #include <linux/uio.h>
25 #include <linux/namei.h>
26 #include <linux/cleancache.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);
58 char name[BDEVNAME_SIZE];
59 pr_warn_ratelimited("VFS: Dirty inode writeback failed "
60 "for block device %s (err=%d).\n",
61 bdevname(bdev, name), ret);
63 spin_lock(&inode->i_lock);
65 spin_unlock(&inode->i_lock);
68 /* Kill _all_ buffers and pagecache , dirty or not.. */
69 static void kill_bdev(struct block_device *bdev)
71 struct address_space *mapping = bdev->bd_inode->i_mapping;
73 if (mapping_empty(mapping))
77 truncate_inode_pages(mapping, 0);
80 /* Invalidate clean unused buffers and pagecache. */
81 void invalidate_bdev(struct block_device *bdev)
83 struct address_space *mapping = bdev->bd_inode->i_mapping;
85 if (mapping->nrpages) {
87 lru_add_drain_all(); /* make sure all lru add caches are flushed */
88 invalidate_mapping_pages(mapping, 0, -1);
90 /* 99% of the time, we don't need to flush the cleancache on the bdev.
91 * But, for the strange corners, lets be cautious
93 cleancache_invalidate_inode(mapping);
95 EXPORT_SYMBOL(invalidate_bdev);
98 * Drop all buffers & page cache for given bdev range. This function bails
99 * with error if bdev has other exclusive owner (such as filesystem).
101 int truncate_bdev_range(struct block_device *bdev, fmode_t mode,
102 loff_t lstart, loff_t lend)
105 * If we don't hold exclusive handle for the device, upgrade to it
106 * while we discard the buffer cache to avoid discarding buffers
107 * under live filesystem.
109 if (!(mode & FMODE_EXCL)) {
110 int err = bd_prepare_to_claim(bdev, truncate_bdev_range);
115 truncate_inode_pages_range(bdev->bd_inode->i_mapping, lstart, lend);
116 if (!(mode & FMODE_EXCL))
117 bd_abort_claiming(bdev, truncate_bdev_range);
122 * Someone else has handle exclusively open. Try invalidating instead.
123 * The 'end' argument is inclusive so the rounding is safe.
125 return invalidate_inode_pages2_range(bdev->bd_inode->i_mapping,
126 lstart >> PAGE_SHIFT,
130 static void set_init_blocksize(struct block_device *bdev)
132 unsigned int bsize = bdev_logical_block_size(bdev);
133 loff_t size = i_size_read(bdev->bd_inode);
135 while (bsize < PAGE_SIZE) {
140 bdev->bd_inode->i_blkbits = blksize_bits(bsize);
143 int set_blocksize(struct block_device *bdev, int size)
145 /* Size must be a power of two, and between 512 and PAGE_SIZE */
146 if (size > PAGE_SIZE || size < 512 || !is_power_of_2(size))
149 /* Size cannot be smaller than the size supported by the device */
150 if (size < bdev_logical_block_size(bdev))
153 /* Don't change the size if it is same as current */
154 if (bdev->bd_inode->i_blkbits != blksize_bits(size)) {
156 bdev->bd_inode->i_blkbits = blksize_bits(size);
162 EXPORT_SYMBOL(set_blocksize);
164 int sb_set_blocksize(struct super_block *sb, int size)
166 if (set_blocksize(sb->s_bdev, size))
168 /* If we get here, we know size is power of two
169 * and it's value is between 512 and PAGE_SIZE */
170 sb->s_blocksize = size;
171 sb->s_blocksize_bits = blksize_bits(size);
172 return sb->s_blocksize;
175 EXPORT_SYMBOL(sb_set_blocksize);
177 int sb_min_blocksize(struct super_block *sb, int size)
179 int minsize = bdev_logical_block_size(sb->s_bdev);
182 return sb_set_blocksize(sb, size);
185 EXPORT_SYMBOL(sb_min_blocksize);
187 int sync_blockdev_nowait(struct block_device *bdev)
191 return filemap_flush(bdev->bd_inode->i_mapping);
193 EXPORT_SYMBOL_GPL(sync_blockdev_nowait);
196 * Write out and wait upon all the dirty data associated with a block
197 * device via its mapping. Does not take the superblock lock.
199 int sync_blockdev(struct block_device *bdev)
203 return filemap_write_and_wait(bdev->bd_inode->i_mapping);
205 EXPORT_SYMBOL(sync_blockdev);
208 * Write out and wait upon all dirty data associated with this
209 * device. Filesystem data as well as the underlying block
210 * device. Takes the superblock lock.
212 int fsync_bdev(struct block_device *bdev)
214 struct super_block *sb = get_super(bdev);
216 int res = sync_filesystem(sb);
220 return sync_blockdev(bdev);
222 EXPORT_SYMBOL(fsync_bdev);
225 * freeze_bdev -- lock a filesystem and force it into a consistent state
226 * @bdev: blockdevice to lock
228 * If a superblock is found on this device, we take the s_umount semaphore
229 * on it to make sure nobody unmounts until the snapshot creation is done.
230 * The reference counter (bd_fsfreeze_count) guarantees that only the last
231 * unfreeze process can unfreeze the frozen filesystem actually when multiple
232 * freeze requests arrive simultaneously. It counts up in freeze_bdev() and
233 * count down in thaw_bdev(). When it becomes 0, thaw_bdev() will unfreeze
236 int freeze_bdev(struct block_device *bdev)
238 struct super_block *sb;
241 mutex_lock(&bdev->bd_fsfreeze_mutex);
242 if (++bdev->bd_fsfreeze_count > 1)
245 sb = get_active_super(bdev);
248 if (sb->s_op->freeze_super)
249 error = sb->s_op->freeze_super(sb);
251 error = freeze_super(sb);
252 deactivate_super(sb);
255 bdev->bd_fsfreeze_count--;
258 bdev->bd_fsfreeze_sb = sb;
263 mutex_unlock(&bdev->bd_fsfreeze_mutex);
266 EXPORT_SYMBOL(freeze_bdev);
269 * thaw_bdev -- unlock filesystem
270 * @bdev: blockdevice to unlock
272 * Unlocks the filesystem and marks it writeable again after freeze_bdev().
274 int thaw_bdev(struct block_device *bdev)
276 struct super_block *sb;
279 mutex_lock(&bdev->bd_fsfreeze_mutex);
280 if (!bdev->bd_fsfreeze_count)
284 if (--bdev->bd_fsfreeze_count > 0)
287 sb = bdev->bd_fsfreeze_sb;
291 if (sb->s_op->thaw_super)
292 error = sb->s_op->thaw_super(sb);
294 error = thaw_super(sb);
296 bdev->bd_fsfreeze_count++;
298 bdev->bd_fsfreeze_sb = NULL;
300 mutex_unlock(&bdev->bd_fsfreeze_mutex);
303 EXPORT_SYMBOL(thaw_bdev);
306 * bdev_read_page() - Start reading a page from a block device
307 * @bdev: The device to read the page from
308 * @sector: The offset on the device to read the page to (need not be aligned)
309 * @page: The page to read
311 * On entry, the page should be locked. It will be unlocked when the page
312 * has been read. If the block driver implements rw_page synchronously,
313 * that will be true on exit from this function, but it need not be.
315 * Errors returned by this function are usually "soft", eg out of memory, or
316 * queue full; callers should try a different route to read this page rather
317 * than propagate an error back up the stack.
319 * Return: negative errno if an error occurs, 0 if submission was successful.
321 int bdev_read_page(struct block_device *bdev, sector_t sector,
324 const struct block_device_operations *ops = bdev->bd_disk->fops;
325 int result = -EOPNOTSUPP;
327 if (!ops->rw_page || bdev_get_integrity(bdev))
330 result = blk_queue_enter(bdev->bd_disk->queue, 0);
333 result = ops->rw_page(bdev, sector + get_start_sect(bdev), page,
335 blk_queue_exit(bdev->bd_disk->queue);
340 * bdev_write_page() - Start writing a page to a block device
341 * @bdev: The device to write the page to
342 * @sector: The offset on the device to write the page to (need not be aligned)
343 * @page: The page to write
344 * @wbc: The writeback_control for the write
346 * On entry, the page should be locked and not currently under writeback.
347 * On exit, if the write started successfully, the page will be unlocked and
348 * under writeback. If the write failed already (eg the driver failed to
349 * queue the page to the device), the page will still be locked. If the
350 * caller is a ->writepage implementation, it will need to unlock the page.
352 * Errors returned by this function are usually "soft", eg out of memory, or
353 * queue full; callers should try a different route to write this page rather
354 * than propagate an error back up the stack.
356 * Return: negative errno if an error occurs, 0 if submission was successful.
358 int bdev_write_page(struct block_device *bdev, sector_t sector,
359 struct page *page, struct writeback_control *wbc)
362 const struct block_device_operations *ops = bdev->bd_disk->fops;
364 if (!ops->rw_page || bdev_get_integrity(bdev))
366 result = blk_queue_enter(bdev->bd_disk->queue, 0);
370 set_page_writeback(page);
371 result = ops->rw_page(bdev, sector + get_start_sect(bdev), page,
374 end_page_writeback(page);
376 clean_page_buffers(page);
379 blk_queue_exit(bdev->bd_disk->queue);
387 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock);
388 static struct kmem_cache * bdev_cachep __read_mostly;
390 static struct inode *bdev_alloc_inode(struct super_block *sb)
392 struct bdev_inode *ei = kmem_cache_alloc(bdev_cachep, GFP_KERNEL);
396 memset(&ei->bdev, 0, sizeof(ei->bdev));
397 return &ei->vfs_inode;
400 static void bdev_free_inode(struct inode *inode)
402 struct block_device *bdev = I_BDEV(inode);
404 free_percpu(bdev->bd_stats);
405 kfree(bdev->bd_meta_info);
407 if (!bdev_is_partition(bdev)) {
408 if (bdev->bd_disk && bdev->bd_disk->bdi)
409 bdi_put(bdev->bd_disk->bdi);
410 kfree(bdev->bd_disk);
413 if (MAJOR(bdev->bd_dev) == BLOCK_EXT_MAJOR)
414 blk_free_ext_minor(MINOR(bdev->bd_dev));
416 kmem_cache_free(bdev_cachep, BDEV_I(inode));
419 static void init_once(void *data)
421 struct bdev_inode *ei = data;
423 inode_init_once(&ei->vfs_inode);
426 static void bdev_evict_inode(struct inode *inode)
428 truncate_inode_pages_final(&inode->i_data);
429 invalidate_inode_buffers(inode); /* is it needed here? */
433 static const struct super_operations bdev_sops = {
434 .statfs = simple_statfs,
435 .alloc_inode = bdev_alloc_inode,
436 .free_inode = bdev_free_inode,
437 .drop_inode = generic_delete_inode,
438 .evict_inode = bdev_evict_inode,
441 static int bd_init_fs_context(struct fs_context *fc)
443 struct pseudo_fs_context *ctx = init_pseudo(fc, BDEVFS_MAGIC);
446 fc->s_iflags |= SB_I_CGROUPWB;
447 ctx->ops = &bdev_sops;
451 static struct file_system_type bd_type = {
453 .init_fs_context = bd_init_fs_context,
454 .kill_sb = kill_anon_super,
457 struct super_block *blockdev_superblock __read_mostly;
458 EXPORT_SYMBOL_GPL(blockdev_superblock);
460 void __init bdev_cache_init(void)
463 static struct vfsmount *bd_mnt;
465 bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode),
466 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
467 SLAB_MEM_SPREAD|SLAB_ACCOUNT|SLAB_PANIC),
469 err = register_filesystem(&bd_type);
471 panic("Cannot register bdev pseudo-fs");
472 bd_mnt = kern_mount(&bd_type);
474 panic("Cannot create bdev pseudo-fs");
475 blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */
478 struct block_device *bdev_alloc(struct gendisk *disk, u8 partno)
480 struct block_device *bdev;
483 inode = new_inode(blockdev_superblock);
486 inode->i_mode = S_IFBLK;
488 inode->i_data.a_ops = &def_blk_aops;
489 mapping_set_gfp_mask(&inode->i_data, GFP_USER);
491 bdev = I_BDEV(inode);
492 mutex_init(&bdev->bd_fsfreeze_mutex);
493 spin_lock_init(&bdev->bd_size_lock);
494 bdev->bd_partno = partno;
495 bdev->bd_inode = inode;
496 bdev->bd_stats = alloc_percpu(struct disk_stats);
497 if (!bdev->bd_stats) {
501 bdev->bd_disk = disk;
505 void bdev_add(struct block_device *bdev, dev_t dev)
508 bdev->bd_inode->i_rdev = dev;
509 bdev->bd_inode->i_ino = dev;
510 insert_inode_hash(bdev->bd_inode);
513 long nr_blockdev_pages(void)
518 spin_lock(&blockdev_superblock->s_inode_list_lock);
519 list_for_each_entry(inode, &blockdev_superblock->s_inodes, i_sb_list)
520 ret += inode->i_mapping->nrpages;
521 spin_unlock(&blockdev_superblock->s_inode_list_lock);
527 * bd_may_claim - test whether a block device can be claimed
528 * @bdev: block device of interest
529 * @whole: whole block device containing @bdev, may equal @bdev
530 * @holder: holder trying to claim @bdev
532 * Test whether @bdev can be claimed by @holder.
535 * spin_lock(&bdev_lock).
538 * %true if @bdev can be claimed, %false otherwise.
540 static bool bd_may_claim(struct block_device *bdev, struct block_device *whole,
543 if (bdev->bd_holder == holder)
544 return true; /* already a holder */
545 else if (bdev->bd_holder != NULL)
546 return false; /* held by someone else */
547 else if (whole == bdev)
548 return true; /* is a whole device which isn't held */
550 else if (whole->bd_holder == bd_may_claim)
551 return true; /* is a partition of a device that is being partitioned */
552 else if (whole->bd_holder != NULL)
553 return false; /* is a partition of a held device */
555 return true; /* is a partition of an un-held device */
559 * bd_prepare_to_claim - claim a block device
560 * @bdev: block device of interest
561 * @holder: holder trying to claim @bdev
563 * Claim @bdev. This function fails if @bdev is already claimed by another
564 * holder and waits if another claiming is in progress. return, the caller
565 * has ownership of bd_claiming and bd_holder[s].
568 * 0 if @bdev can be claimed, -EBUSY otherwise.
570 int bd_prepare_to_claim(struct block_device *bdev, void *holder)
572 struct block_device *whole = bdev_whole(bdev);
574 if (WARN_ON_ONCE(!holder))
577 spin_lock(&bdev_lock);
578 /* if someone else claimed, fail */
579 if (!bd_may_claim(bdev, whole, holder)) {
580 spin_unlock(&bdev_lock);
584 /* if claiming is already in progress, wait for it to finish */
585 if (whole->bd_claiming) {
586 wait_queue_head_t *wq = bit_waitqueue(&whole->bd_claiming, 0);
589 prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
590 spin_unlock(&bdev_lock);
592 finish_wait(wq, &wait);
597 whole->bd_claiming = holder;
598 spin_unlock(&bdev_lock);
601 EXPORT_SYMBOL_GPL(bd_prepare_to_claim); /* only for the loop driver */
603 static void bd_clear_claiming(struct block_device *whole, void *holder)
605 lockdep_assert_held(&bdev_lock);
606 /* tell others that we're done */
607 BUG_ON(whole->bd_claiming != holder);
608 whole->bd_claiming = NULL;
609 wake_up_bit(&whole->bd_claiming, 0);
613 * bd_finish_claiming - finish claiming of a block device
614 * @bdev: block device of interest
615 * @holder: holder that has claimed @bdev
617 * Finish exclusive open of a block device. Mark the device as exlusively
618 * open by the holder and wake up all waiters for exclusive open to finish.
620 static void bd_finish_claiming(struct block_device *bdev, void *holder)
622 struct block_device *whole = bdev_whole(bdev);
624 spin_lock(&bdev_lock);
625 BUG_ON(!bd_may_claim(bdev, whole, holder));
627 * Note that for a whole device bd_holders will be incremented twice,
628 * and bd_holder will be set to bd_may_claim before being set to holder
631 whole->bd_holder = bd_may_claim;
633 bdev->bd_holder = holder;
634 bd_clear_claiming(whole, holder);
635 spin_unlock(&bdev_lock);
639 * bd_abort_claiming - abort claiming of a block device
640 * @bdev: block device of interest
641 * @holder: holder that has claimed @bdev
643 * Abort claiming of a block device when the exclusive open failed. This can be
644 * also used when exclusive open is not actually desired and we just needed
645 * to block other exclusive openers for a while.
647 void bd_abort_claiming(struct block_device *bdev, void *holder)
649 spin_lock(&bdev_lock);
650 bd_clear_claiming(bdev_whole(bdev), holder);
651 spin_unlock(&bdev_lock);
653 EXPORT_SYMBOL(bd_abort_claiming);
655 static void blkdev_flush_mapping(struct block_device *bdev)
657 WARN_ON_ONCE(bdev->bd_holders);
660 bdev_write_inode(bdev);
663 static int blkdev_get_whole(struct block_device *bdev, fmode_t mode)
665 struct gendisk *disk = bdev->bd_disk;
668 if (disk->fops->open) {
669 ret = disk->fops->open(bdev, mode);
671 /* avoid ghost partitions on a removed medium */
672 if (ret == -ENOMEDIUM &&
673 test_bit(GD_NEED_PART_SCAN, &disk->state))
674 bdev_disk_changed(disk, true);
679 if (!bdev->bd_openers)
680 set_init_blocksize(bdev);
681 if (test_bit(GD_NEED_PART_SCAN, &disk->state))
682 bdev_disk_changed(disk, false);
687 static void blkdev_put_whole(struct block_device *bdev, fmode_t mode)
689 if (!--bdev->bd_openers)
690 blkdev_flush_mapping(bdev);
691 if (bdev->bd_disk->fops->release)
692 bdev->bd_disk->fops->release(bdev->bd_disk, mode);
695 static int blkdev_get_part(struct block_device *part, fmode_t mode)
697 struct gendisk *disk = part->bd_disk;
700 if (part->bd_openers)
703 ret = blkdev_get_whole(bdev_whole(part), mode);
708 if (!bdev_nr_sectors(part))
711 disk->open_partitions++;
712 set_init_blocksize(part);
718 blkdev_put_whole(bdev_whole(part), mode);
722 static void blkdev_put_part(struct block_device *part, fmode_t mode)
724 struct block_device *whole = bdev_whole(part);
726 if (--part->bd_openers)
728 blkdev_flush_mapping(part);
729 whole->bd_disk->open_partitions--;
730 blkdev_put_whole(whole, mode);
733 struct block_device *blkdev_get_no_open(dev_t dev)
735 struct block_device *bdev;
738 inode = ilookup(blockdev_superblock, dev);
740 blk_request_module(dev);
741 inode = ilookup(blockdev_superblock, dev);
746 /* switch from the inode reference to a device mode one: */
747 bdev = &BDEV_I(inode)->bdev;
748 if (!kobject_get_unless_zero(&bdev->bd_device.kobj))
754 if ((bdev->bd_disk->flags & GENHD_FL_HIDDEN) ||
755 !try_module_get(bdev->bd_disk->fops->owner)) {
756 put_device(&bdev->bd_device);
763 void blkdev_put_no_open(struct block_device *bdev)
765 module_put(bdev->bd_disk->fops->owner);
766 put_device(&bdev->bd_device);
770 * blkdev_get_by_dev - open a block device by device number
771 * @dev: device number of block device to open
772 * @mode: FMODE_* mask
773 * @holder: exclusive holder identifier
775 * Open the block device described by device number @dev. If @mode includes
776 * %FMODE_EXCL, the block device is opened with exclusive access. Specifying
777 * %FMODE_EXCL with a %NULL @holder is invalid. Exclusive opens may nest for
780 * Use this interface ONLY if you really do not have anything better - i.e. when
781 * you are behind a truly sucky interface and all you are given is a device
782 * number. Everything else should use blkdev_get_by_path().
788 * Reference to the block_device on success, ERR_PTR(-errno) on failure.
790 struct block_device *blkdev_get_by_dev(dev_t dev, fmode_t mode, void *holder)
792 bool unblock_events = true;
793 struct block_device *bdev;
794 struct gendisk *disk;
797 ret = devcgroup_check_permission(DEVCG_DEV_BLOCK,
798 MAJOR(dev), MINOR(dev),
799 ((mode & FMODE_READ) ? DEVCG_ACC_READ : 0) |
800 ((mode & FMODE_WRITE) ? DEVCG_ACC_WRITE : 0));
804 bdev = blkdev_get_no_open(dev);
806 return ERR_PTR(-ENXIO);
807 disk = bdev->bd_disk;
809 if (mode & FMODE_EXCL) {
810 ret = bd_prepare_to_claim(bdev, holder);
815 disk_block_events(disk);
817 mutex_lock(&disk->open_mutex);
819 if (!disk_live(disk))
821 if (bdev_is_partition(bdev))
822 ret = blkdev_get_part(bdev, mode);
824 ret = blkdev_get_whole(bdev, mode);
827 if (mode & FMODE_EXCL) {
828 bd_finish_claiming(bdev, holder);
831 * Block event polling for write claims if requested. Any write
832 * holder makes the write_holder state stick until all are
833 * released. This is good enough and tracking individual
834 * writeable reference is too fragile given the way @mode is
835 * used in blkdev_get/put().
837 if ((mode & FMODE_WRITE) && !bdev->bd_write_holder &&
838 (disk->flags & GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE)) {
839 bdev->bd_write_holder = true;
840 unblock_events = false;
843 mutex_unlock(&disk->open_mutex);
846 disk_unblock_events(disk);
850 if (mode & FMODE_EXCL)
851 bd_abort_claiming(bdev, holder);
852 mutex_unlock(&disk->open_mutex);
853 disk_unblock_events(disk);
855 blkdev_put_no_open(bdev);
858 EXPORT_SYMBOL(blkdev_get_by_dev);
861 * blkdev_get_by_path - open a block device by name
862 * @path: path to the block device to open
863 * @mode: FMODE_* mask
864 * @holder: exclusive holder identifier
866 * Open the block device described by the device file at @path. If @mode
867 * includes %FMODE_EXCL, the block device is opened with exclusive access.
868 * Specifying %FMODE_EXCL with a %NULL @holder is invalid. Exclusive opens may
869 * nest for the same @holder.
875 * Reference to the block_device on success, ERR_PTR(-errno) on failure.
877 struct block_device *blkdev_get_by_path(const char *path, fmode_t mode,
880 struct block_device *bdev;
884 error = lookup_bdev(path, &dev);
886 return ERR_PTR(error);
888 bdev = blkdev_get_by_dev(dev, mode, holder);
889 if (!IS_ERR(bdev) && (mode & FMODE_WRITE) && bdev_read_only(bdev)) {
890 blkdev_put(bdev, mode);
891 return ERR_PTR(-EACCES);
896 EXPORT_SYMBOL(blkdev_get_by_path);
898 void blkdev_put(struct block_device *bdev, fmode_t mode)
900 struct gendisk *disk = bdev->bd_disk;
903 * Sync early if it looks like we're the last one. If someone else
904 * opens the block device between now and the decrement of bd_openers
905 * then we did a sync that we didn't need to, but that's not the end
906 * of the world and we want to avoid long (could be several minute)
907 * syncs while holding the mutex.
909 if (bdev->bd_openers == 1)
912 mutex_lock(&disk->open_mutex);
913 if (mode & FMODE_EXCL) {
914 struct block_device *whole = bdev_whole(bdev);
918 * Release a claim on the device. The holder fields
919 * are protected with bdev_lock. open_mutex is to
920 * synchronize disk_holder unlinking.
922 spin_lock(&bdev_lock);
924 WARN_ON_ONCE(--bdev->bd_holders < 0);
925 WARN_ON_ONCE(--whole->bd_holders < 0);
927 if ((bdev_free = !bdev->bd_holders))
928 bdev->bd_holder = NULL;
929 if (!whole->bd_holders)
930 whole->bd_holder = NULL;
932 spin_unlock(&bdev_lock);
935 * If this was the last claim, remove holder link and
936 * unblock evpoll if it was a write holder.
938 if (bdev_free && bdev->bd_write_holder) {
939 disk_unblock_events(disk);
940 bdev->bd_write_holder = false;
945 * Trigger event checking and tell drivers to flush MEDIA_CHANGE
946 * event. This is to ensure detection of media removal commanded
947 * from userland - e.g. eject(1).
949 disk_flush_events(disk, DISK_EVENT_MEDIA_CHANGE);
951 if (bdev_is_partition(bdev))
952 blkdev_put_part(bdev, mode);
954 blkdev_put_whole(bdev, mode);
955 mutex_unlock(&disk->open_mutex);
957 blkdev_put_no_open(bdev);
959 EXPORT_SYMBOL(blkdev_put);
962 * lookup_bdev - lookup a struct block_device by name
963 * @pathname: special file representing the block device
964 * @dev: return value of the block device's dev_t
966 * Get a reference to the blockdevice at @pathname in the current
967 * namespace if possible and return it. Return ERR_PTR(error)
970 int lookup_bdev(const char *pathname, dev_t *dev)
976 if (!pathname || !*pathname)
979 error = kern_path(pathname, LOOKUP_FOLLOW, &path);
983 inode = d_backing_inode(path.dentry);
985 if (!S_ISBLK(inode->i_mode))
988 if (!may_open_dev(&path))
991 *dev = inode->i_rdev;
997 EXPORT_SYMBOL(lookup_bdev);
999 int __invalidate_device(struct block_device *bdev, bool kill_dirty)
1001 struct super_block *sb = get_super(bdev);
1006 * no need to lock the super, get_super holds the
1007 * read mutex so the filesystem cannot go away
1008 * under us (->put_super runs with the write lock
1011 shrink_dcache_sb(sb);
1012 res = invalidate_inodes(sb, kill_dirty);
1015 invalidate_bdev(bdev);
1018 EXPORT_SYMBOL(__invalidate_device);
1020 void sync_bdevs(bool wait)
1022 struct inode *inode, *old_inode = NULL;
1024 spin_lock(&blockdev_superblock->s_inode_list_lock);
1025 list_for_each_entry(inode, &blockdev_superblock->s_inodes, i_sb_list) {
1026 struct address_space *mapping = inode->i_mapping;
1027 struct block_device *bdev;
1029 spin_lock(&inode->i_lock);
1030 if (inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW) ||
1031 mapping->nrpages == 0) {
1032 spin_unlock(&inode->i_lock);
1036 spin_unlock(&inode->i_lock);
1037 spin_unlock(&blockdev_superblock->s_inode_list_lock);
1039 * We hold a reference to 'inode' so it couldn't have been
1040 * removed from s_inodes list while we dropped the
1041 * s_inode_list_lock We cannot iput the inode now as we can
1042 * be holding the last reference and we cannot iput it under
1043 * s_inode_list_lock. So we keep the reference and iput it
1048 bdev = I_BDEV(inode);
1050 mutex_lock(&bdev->bd_disk->open_mutex);
1051 if (!bdev->bd_openers) {
1055 * We keep the error status of individual mapping so
1056 * that applications can catch the writeback error using
1057 * fsync(2). See filemap_fdatawait_keep_errors() for
1060 filemap_fdatawait_keep_errors(inode->i_mapping);
1062 filemap_fdatawrite(inode->i_mapping);
1064 mutex_unlock(&bdev->bd_disk->open_mutex);
1066 spin_lock(&blockdev_superblock->s_inode_list_lock);
1068 spin_unlock(&blockdev_superblock->s_inode_list_lock);