LCOV - code coverage report
Current view: top level - block - bdev.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 34 437 7.8 %
Date: 2023-08-24 13:40:31 Functions: 7 45 15.6 %

          Line data    Source code
       1             : // SPDX-License-Identifier: GPL-2.0-only
       2             : /*
       3             :  *  Copyright (C) 1991, 1992  Linus Torvalds
       4             :  *  Copyright (C) 2001  Andrea Arcangeli <andrea@suse.de> SuSE
       5             :  *  Copyright (C) 2016 - 2020 Christoph Hellwig
       6             :  */
       7             : 
       8             : #include <linux/init.h>
       9             : #include <linux/mm.h>
      10             : #include <linux/slab.h>
      11             : #include <linux/kmod.h>
      12             : #include <linux/major.h>
      13             : #include <linux/device_cgroup.h>
      14             : #include <linux/blkdev.h>
      15             : #include <linux/blk-integrity.h>
      16             : #include <linux/backing-dev.h>
      17             : #include <linux/module.h>
      18             : #include <linux/blkpg.h>
      19             : #include <linux/magic.h>
      20             : #include <linux/buffer_head.h>
      21             : #include <linux/swap.h>
      22             : #include <linux/writeback.h>
      23             : #include <linux/mount.h>
      24             : #include <linux/pseudo_fs.h>
      25             : #include <linux/uio.h>
      26             : #include <linux/namei.h>
      27             : #include <linux/part_stat.h>
      28             : #include <linux/uaccess.h>
      29             : #include <linux/stat.h>
      30             : #include "../fs/internal.h"
      31             : #include "blk.h"
      32             : 
      33             : struct bdev_inode {
      34             :         struct block_device bdev;
      35             :         struct inode vfs_inode;
      36             : };
      37             : 
      38             : static inline struct bdev_inode *BDEV_I(struct inode *inode)
      39             : {
      40           0 :         return container_of(inode, struct bdev_inode, vfs_inode);
      41             : }
      42             : 
      43           0 : struct block_device *I_BDEV(struct inode *inode)
      44             : {
      45           0 :         return &BDEV_I(inode)->bdev;
      46             : }
      47             : EXPORT_SYMBOL(I_BDEV);
      48             : 
      49           0 : static void bdev_write_inode(struct block_device *bdev)
      50             : {
      51           0 :         struct inode *inode = bdev->bd_inode;
      52             :         int ret;
      53             : 
      54           0 :         spin_lock(&inode->i_lock);
      55           0 :         while (inode->i_state & I_DIRTY) {
      56           0 :                 spin_unlock(&inode->i_lock);
      57           0 :                 ret = write_inode_now(inode, true);
      58           0 :                 if (ret)
      59           0 :                         pr_warn_ratelimited(
      60             :         "VFS: Dirty inode writeback failed for block device %pg (err=%d).\n",
      61             :                                 bdev, ret);
      62           0 :                 spin_lock(&inode->i_lock);
      63             :         }
      64           0 :         spin_unlock(&inode->i_lock);
      65           0 : }
      66             : 
      67             : /* Kill _all_ buffers and pagecache , dirty or not.. */
      68           0 : static void kill_bdev(struct block_device *bdev)
      69             : {
      70           0 :         struct address_space *mapping = bdev->bd_inode->i_mapping;
      71             : 
      72           0 :         if (mapping_empty(mapping))
      73             :                 return;
      74             : 
      75           0 :         invalidate_bh_lrus();
      76           0 :         truncate_inode_pages(mapping, 0);
      77             : }
      78             : 
      79             : /* Invalidate clean unused buffers and pagecache. */
      80           0 : void invalidate_bdev(struct block_device *bdev)
      81             : {
      82           0 :         struct address_space *mapping = bdev->bd_inode->i_mapping;
      83             : 
      84           0 :         if (mapping->nrpages) {
      85           0 :                 invalidate_bh_lrus();
      86           0 :                 lru_add_drain_all();    /* make sure all lru add caches are flushed */
      87           0 :                 invalidate_mapping_pages(mapping, 0, -1);
      88             :         }
      89           0 : }
      90             : EXPORT_SYMBOL(invalidate_bdev);
      91             : 
      92             : /*
      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).
      95             :  */
      96           0 : int truncate_bdev_range(struct block_device *bdev, blk_mode_t mode,
      97             :                         loff_t lstart, loff_t lend)
      98             : {
      99             :         /*
     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.
     103             :          */
     104           0 :         if (!(mode & BLK_OPEN_EXCL)) {
     105           0 :                 int err = bd_prepare_to_claim(bdev, truncate_bdev_range, NULL);
     106           0 :                 if (err)
     107             :                         goto invalidate;
     108             :         }
     109             : 
     110           0 :         truncate_inode_pages_range(bdev->bd_inode->i_mapping, lstart, lend);
     111           0 :         if (!(mode & BLK_OPEN_EXCL))
     112           0 :                 bd_abort_claiming(bdev, truncate_bdev_range);
     113             :         return 0;
     114             : 
     115             : invalidate:
     116             :         /*
     117             :          * Someone else has handle exclusively open. Try invalidating instead.
     118             :          * The 'end' argument is inclusive so the rounding is safe.
     119             :          */
     120           0 :         return invalidate_inode_pages2_range(bdev->bd_inode->i_mapping,
     121           0 :                                              lstart >> PAGE_SHIFT,
     122           0 :                                              lend >> PAGE_SHIFT);
     123             : }
     124             : 
     125           0 : static void set_init_blocksize(struct block_device *bdev)
     126             : {
     127           0 :         unsigned int bsize = bdev_logical_block_size(bdev);
     128           0 :         loff_t size = i_size_read(bdev->bd_inode);
     129             : 
     130           0 :         while (bsize < PAGE_SIZE) {
     131           0 :                 if (size & bsize)
     132             :                         break;
     133           0 :                 bsize <<= 1;
     134             :         }
     135           0 :         bdev->bd_inode->i_blkbits = blksize_bits(bsize);
     136           0 : }
     137             : 
     138           0 : int set_blocksize(struct block_device *bdev, int size)
     139             : {
     140             :         /* Size must be a power of two, and between 512 and PAGE_SIZE */
     141           0 :         if (size > PAGE_SIZE || size < 512 || !is_power_of_2(size))
     142             :                 return -EINVAL;
     143             : 
     144             :         /* Size cannot be smaller than the size supported by the device */
     145           0 :         if (size < bdev_logical_block_size(bdev))
     146             :                 return -EINVAL;
     147             : 
     148             :         /* Don't change the size if it is same as current */
     149           0 :         if (bdev->bd_inode->i_blkbits != blksize_bits(size)) {
     150           0 :                 sync_blockdev(bdev);
     151           0 :                 bdev->bd_inode->i_blkbits = blksize_bits(size);
     152           0 :                 kill_bdev(bdev);
     153             :         }
     154             :         return 0;
     155             : }
     156             : 
     157             : EXPORT_SYMBOL(set_blocksize);
     158             : 
     159           0 : int sb_set_blocksize(struct super_block *sb, int size)
     160             : {
     161           0 :         if (set_blocksize(sb->s_bdev, size))
     162             :                 return 0;
     163             :         /* If we get here, we know size is power of two
     164             :          * and it's value is between 512 and PAGE_SIZE */
     165           0 :         sb->s_blocksize = size;
     166           0 :         sb->s_blocksize_bits = blksize_bits(size);
     167           0 :         return sb->s_blocksize;
     168             : }
     169             : 
     170             : EXPORT_SYMBOL(sb_set_blocksize);
     171             : 
     172           0 : int sb_min_blocksize(struct super_block *sb, int size)
     173             : {
     174           0 :         int minsize = bdev_logical_block_size(sb->s_bdev);
     175           0 :         if (size < minsize)
     176           0 :                 size = minsize;
     177           0 :         return sb_set_blocksize(sb, size);
     178             : }
     179             : 
     180             : EXPORT_SYMBOL(sb_min_blocksize);
     181             : 
     182           5 : int sync_blockdev_nowait(struct block_device *bdev)
     183             : {
     184           5 :         if (!bdev)
     185             :                 return 0;
     186           0 :         return filemap_flush(bdev->bd_inode->i_mapping);
     187             : }
     188             : EXPORT_SYMBOL_GPL(sync_blockdev_nowait);
     189             : 
     190             : /*
     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.
     193             :  */
     194           5 : int sync_blockdev(struct block_device *bdev)
     195             : {
     196           5 :         if (!bdev)
     197             :                 return 0;
     198           0 :         return filemap_write_and_wait(bdev->bd_inode->i_mapping);
     199             : }
     200             : EXPORT_SYMBOL(sync_blockdev);
     201             : 
     202           0 : int sync_blockdev_range(struct block_device *bdev, loff_t lstart, loff_t lend)
     203             : {
     204           0 :         return filemap_write_and_wait_range(bdev->bd_inode->i_mapping,
     205             :                         lstart, lend);
     206             : }
     207             : EXPORT_SYMBOL(sync_blockdev_range);
     208             : 
     209             : /*
     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.
     213             :  */
     214           0 : int fsync_bdev(struct block_device *bdev)
     215             : {
     216           0 :         struct super_block *sb = get_super(bdev);
     217           0 :         if (sb) {
     218           0 :                 int res = sync_filesystem(sb);
     219           0 :                 drop_super(sb);
     220           0 :                 return res;
     221             :         }
     222             :         return sync_blockdev(bdev);
     223             : }
     224             : EXPORT_SYMBOL(fsync_bdev);
     225             : 
     226             : /**
     227             :  * freeze_bdev - lock a filesystem and force it into a consistent state
     228             :  * @bdev:       blockdevice to lock
     229             :  *
     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
     236             :  * actually.
     237             :  */
     238           0 : int freeze_bdev(struct block_device *bdev)
     239             : {
     240             :         struct super_block *sb;
     241           0 :         int error = 0;
     242             : 
     243           0 :         mutex_lock(&bdev->bd_fsfreeze_mutex);
     244           0 :         if (++bdev->bd_fsfreeze_count > 1)
     245             :                 goto done;
     246             : 
     247           0 :         sb = get_active_super(bdev);
     248           0 :         if (!sb)
     249             :                 goto sync;
     250           0 :         if (sb->s_op->freeze_super)
     251           0 :                 error = sb->s_op->freeze_super(sb);
     252             :         else
     253           0 :                 error = freeze_super(sb);
     254           0 :         deactivate_super(sb);
     255             : 
     256           0 :         if (error) {
     257           0 :                 bdev->bd_fsfreeze_count--;
     258           0 :                 goto done;
     259             :         }
     260           0 :         bdev->bd_fsfreeze_sb = sb;
     261             : 
     262             : sync:
     263             :         sync_blockdev(bdev);
     264             : done:
     265           0 :         mutex_unlock(&bdev->bd_fsfreeze_mutex);
     266           0 :         return error;
     267             : }
     268             : EXPORT_SYMBOL(freeze_bdev);
     269             : 
     270             : /**
     271             :  * thaw_bdev - unlock filesystem
     272             :  * @bdev:       blockdevice to unlock
     273             :  *
     274             :  * Unlocks the filesystem and marks it writeable again after freeze_bdev().
     275             :  */
     276           0 : int thaw_bdev(struct block_device *bdev)
     277             : {
     278             :         struct super_block *sb;
     279           0 :         int error = -EINVAL;
     280             : 
     281           0 :         mutex_lock(&bdev->bd_fsfreeze_mutex);
     282           0 :         if (!bdev->bd_fsfreeze_count)
     283             :                 goto out;
     284             : 
     285           0 :         error = 0;
     286           0 :         if (--bdev->bd_fsfreeze_count > 0)
     287             :                 goto out;
     288             : 
     289           0 :         sb = bdev->bd_fsfreeze_sb;
     290           0 :         if (!sb)
     291             :                 goto out;
     292             : 
     293           0 :         if (sb->s_op->thaw_super)
     294           0 :                 error = sb->s_op->thaw_super(sb);
     295             :         else
     296           0 :                 error = thaw_super(sb);
     297           0 :         if (error)
     298           0 :                 bdev->bd_fsfreeze_count++;
     299             :         else
     300           0 :                 bdev->bd_fsfreeze_sb = NULL;
     301             : out:
     302           0 :         mutex_unlock(&bdev->bd_fsfreeze_mutex);
     303           0 :         return error;
     304             : }
     305             : EXPORT_SYMBOL(thaw_bdev);
     306             : 
     307             : /*
     308             :  * pseudo-fs
     309             :  */
     310             : 
     311             : static  __cacheline_aligned_in_smp DEFINE_MUTEX(bdev_lock);
     312             : static struct kmem_cache * bdev_cachep __read_mostly;
     313             : 
     314           1 : static struct inode *bdev_alloc_inode(struct super_block *sb)
     315             : {
     316           2 :         struct bdev_inode *ei = alloc_inode_sb(sb, bdev_cachep, GFP_KERNEL);
     317             : 
     318           1 :         if (!ei)
     319             :                 return NULL;
     320           2 :         memset(&ei->bdev, 0, sizeof(ei->bdev));
     321           1 :         return &ei->vfs_inode;
     322             : }
     323             : 
     324           0 : static void bdev_free_inode(struct inode *inode)
     325             : {
     326           0 :         struct block_device *bdev = I_BDEV(inode);
     327             : 
     328           0 :         free_percpu(bdev->bd_stats);
     329           0 :         kfree(bdev->bd_meta_info);
     330             : 
     331           0 :         if (!bdev_is_partition(bdev)) {
     332           0 :                 if (bdev->bd_disk && bdev->bd_disk->bdi)
     333           0 :                         bdi_put(bdev->bd_disk->bdi);
     334           0 :                 kfree(bdev->bd_disk);
     335             :         }
     336             : 
     337           0 :         if (MAJOR(bdev->bd_dev) == BLOCK_EXT_MAJOR)
     338           0 :                 blk_free_ext_minor(MINOR(bdev->bd_dev));
     339             : 
     340           0 :         kmem_cache_free(bdev_cachep, BDEV_I(inode));
     341           0 : }
     342             : 
     343          11 : static void init_once(void *data)
     344             : {
     345          11 :         struct bdev_inode *ei = data;
     346             : 
     347          11 :         inode_init_once(&ei->vfs_inode);
     348          11 : }
     349             : 
     350           0 : static void bdev_evict_inode(struct inode *inode)
     351             : {
     352           0 :         truncate_inode_pages_final(&inode->i_data);
     353           0 :         invalidate_inode_buffers(inode); /* is it needed here? */
     354           0 :         clear_inode(inode);
     355           0 : }
     356             : 
     357             : static const struct super_operations bdev_sops = {
     358             :         .statfs = simple_statfs,
     359             :         .alloc_inode = bdev_alloc_inode,
     360             :         .free_inode = bdev_free_inode,
     361             :         .drop_inode = generic_delete_inode,
     362             :         .evict_inode = bdev_evict_inode,
     363             : };
     364             : 
     365           1 : static int bd_init_fs_context(struct fs_context *fc)
     366             : {
     367           1 :         struct pseudo_fs_context *ctx = init_pseudo(fc, BDEVFS_MAGIC);
     368           1 :         if (!ctx)
     369             :                 return -ENOMEM;
     370           1 :         fc->s_iflags |= SB_I_CGROUPWB;
     371           1 :         ctx->ops = &bdev_sops;
     372           1 :         return 0;
     373             : }
     374             : 
     375             : static struct file_system_type bd_type = {
     376             :         .name           = "bdev",
     377             :         .init_fs_context = bd_init_fs_context,
     378             :         .kill_sb        = kill_anon_super,
     379             : };
     380             : 
     381             : struct super_block *blockdev_superblock __read_mostly;
     382             : EXPORT_SYMBOL_GPL(blockdev_superblock);
     383             : 
     384           1 : void __init bdev_cache_init(void)
     385             : {
     386             :         int err;
     387             :         static struct vfsmount *bd_mnt;
     388             : 
     389           1 :         bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode),
     390             :                         0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
     391             :                                 SLAB_MEM_SPREAD|SLAB_ACCOUNT|SLAB_PANIC),
     392             :                         init_once);
     393           1 :         err = register_filesystem(&bd_type);
     394           1 :         if (err)
     395           0 :                 panic("Cannot register bdev pseudo-fs");
     396           1 :         bd_mnt = kern_mount(&bd_type);
     397           2 :         if (IS_ERR(bd_mnt))
     398           0 :                 panic("Cannot create bdev pseudo-fs");
     399           1 :         blockdev_superblock = bd_mnt->mnt_sb;   /* For writeback */
     400           1 : }
     401             : 
     402           0 : struct block_device *bdev_alloc(struct gendisk *disk, u8 partno)
     403             : {
     404             :         struct block_device *bdev;
     405             :         struct inode *inode;
     406             : 
     407           0 :         inode = new_inode(blockdev_superblock);
     408           0 :         if (!inode)
     409             :                 return NULL;
     410           0 :         inode->i_mode = S_IFBLK;
     411           0 :         inode->i_rdev = 0;
     412           0 :         inode->i_data.a_ops = &def_blk_aops;
     413           0 :         mapping_set_gfp_mask(&inode->i_data, GFP_USER);
     414             : 
     415           0 :         bdev = I_BDEV(inode);
     416           0 :         mutex_init(&bdev->bd_fsfreeze_mutex);
     417           0 :         spin_lock_init(&bdev->bd_size_lock);
     418           0 :         mutex_init(&bdev->bd_holder_lock);
     419           0 :         bdev->bd_partno = partno;
     420           0 :         bdev->bd_inode = inode;
     421           0 :         bdev->bd_queue = disk->queue;
     422           0 :         if (partno)
     423           0 :                 bdev->bd_has_submit_bio = disk->part0->bd_has_submit_bio;
     424             :         else
     425           0 :                 bdev->bd_has_submit_bio = false;
     426           0 :         bdev->bd_stats = alloc_percpu(struct disk_stats);
     427           0 :         if (!bdev->bd_stats) {
     428           0 :                 iput(inode);
     429           0 :                 return NULL;
     430             :         }
     431           0 :         bdev->bd_disk = disk;
     432           0 :         return bdev;
     433             : }
     434             : 
     435           0 : void bdev_set_nr_sectors(struct block_device *bdev, sector_t sectors)
     436             : {
     437           0 :         spin_lock(&bdev->bd_size_lock);
     438           0 :         i_size_write(bdev->bd_inode, (loff_t)sectors << SECTOR_SHIFT);
     439           0 :         bdev->bd_nr_sectors = sectors;
     440           0 :         spin_unlock(&bdev->bd_size_lock);
     441           0 : }
     442             : 
     443           0 : void bdev_add(struct block_device *bdev, dev_t dev)
     444             : {
     445           0 :         bdev->bd_dev = dev;
     446           0 :         bdev->bd_inode->i_rdev = dev;
     447           0 :         bdev->bd_inode->i_ino = dev;
     448           0 :         insert_inode_hash(bdev->bd_inode);
     449           0 : }
     450             : 
     451           2 : long nr_blockdev_pages(void)
     452             : {
     453             :         struct inode *inode;
     454           2 :         long ret = 0;
     455             : 
     456           4 :         spin_lock(&blockdev_superblock->s_inode_list_lock);
     457           4 :         list_for_each_entry(inode, &blockdev_superblock->s_inodes, i_sb_list)
     458           2 :                 ret += inode->i_mapping->nrpages;
     459           4 :         spin_unlock(&blockdev_superblock->s_inode_list_lock);
     460             : 
     461           2 :         return ret;
     462             : }
     463             : 
     464             : /**
     465             :  * bd_may_claim - test whether a block device can be claimed
     466             :  * @bdev: block device of interest
     467             :  * @holder: holder trying to claim @bdev
     468             :  * @hops: holder ops
     469             :  *
     470             :  * Test whether @bdev can be claimed by @holder.
     471             :  *
     472             :  * RETURNS:
     473             :  * %true if @bdev can be claimed, %false otherwise.
     474             :  */
     475           0 : static bool bd_may_claim(struct block_device *bdev, void *holder,
     476             :                 const struct blk_holder_ops *hops)
     477             : {
     478           0 :         struct block_device *whole = bdev_whole(bdev);
     479             : 
     480             :         lockdep_assert_held(&bdev_lock);
     481             : 
     482           0 :         if (bdev->bd_holder) {
     483             :                 /*
     484             :                  * The same holder can always re-claim.
     485             :                  */
     486           0 :                 if (bdev->bd_holder == holder) {
     487           0 :                         if (WARN_ON_ONCE(bdev->bd_holder_ops != hops))
     488             :                                 return false;
     489           0 :                         return true;
     490             :                 }
     491             :                 return false;
     492             :         }
     493             : 
     494             :         /*
     495             :          * If the whole devices holder is set to bd_may_claim, a partition on
     496             :          * the device is claimed, but not the whole device.
     497             :          */
     498           0 :         if (whole != bdev &&
     499           0 :             whole->bd_holder && whole->bd_holder != bd_may_claim)
     500             :                 return false;
     501           0 :         return true;
     502             : }
     503             : 
     504             : /**
     505             :  * bd_prepare_to_claim - claim a block device
     506             :  * @bdev: block device of interest
     507             :  * @holder: holder trying to claim @bdev
     508             :  * @hops: holder ops.
     509             :  *
     510             :  * Claim @bdev.  This function fails if @bdev is already claimed by another
     511             :  * holder and waits if another claiming is in progress. return, the caller
     512             :  * has ownership of bd_claiming and bd_holder[s].
     513             :  *
     514             :  * RETURNS:
     515             :  * 0 if @bdev can be claimed, -EBUSY otherwise.
     516             :  */
     517           0 : int bd_prepare_to_claim(struct block_device *bdev, void *holder,
     518             :                 const struct blk_holder_ops *hops)
     519             : {
     520           0 :         struct block_device *whole = bdev_whole(bdev);
     521             : 
     522           0 :         if (WARN_ON_ONCE(!holder))
     523             :                 return -EINVAL;
     524             : retry:
     525           0 :         mutex_lock(&bdev_lock);
     526             :         /* if someone else claimed, fail */
     527           0 :         if (!bd_may_claim(bdev, holder, hops)) {
     528           0 :                 mutex_unlock(&bdev_lock);
     529           0 :                 return -EBUSY;
     530             :         }
     531             : 
     532             :         /* if claiming is already in progress, wait for it to finish */
     533           0 :         if (whole->bd_claiming) {
     534           0 :                 wait_queue_head_t *wq = bit_waitqueue(&whole->bd_claiming, 0);
     535           0 :                 DEFINE_WAIT(wait);
     536             : 
     537           0 :                 prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
     538           0 :                 mutex_unlock(&bdev_lock);
     539           0 :                 schedule();
     540           0 :                 finish_wait(wq, &wait);
     541             :                 goto retry;
     542             :         }
     543             : 
     544             :         /* yay, all mine */
     545           0 :         whole->bd_claiming = holder;
     546           0 :         mutex_unlock(&bdev_lock);
     547           0 :         return 0;
     548             : }
     549             : EXPORT_SYMBOL_GPL(bd_prepare_to_claim); /* only for the loop driver */
     550             : 
     551           0 : static void bd_clear_claiming(struct block_device *whole, void *holder)
     552             : {
     553             :         lockdep_assert_held(&bdev_lock);
     554             :         /* tell others that we're done */
     555           0 :         BUG_ON(whole->bd_claiming != holder);
     556           0 :         whole->bd_claiming = NULL;
     557           0 :         wake_up_bit(&whole->bd_claiming, 0);
     558           0 : }
     559             : 
     560             : /**
     561             :  * bd_finish_claiming - finish claiming of a block device
     562             :  * @bdev: block device of interest
     563             :  * @holder: holder that has claimed @bdev
     564             :  * @hops: block device holder operations
     565             :  *
     566             :  * Finish exclusive open of a block device. Mark the device as exlusively
     567             :  * open by the holder and wake up all waiters for exclusive open to finish.
     568             :  */
     569           0 : static void bd_finish_claiming(struct block_device *bdev, void *holder,
     570             :                 const struct blk_holder_ops *hops)
     571             : {
     572           0 :         struct block_device *whole = bdev_whole(bdev);
     573             : 
     574           0 :         mutex_lock(&bdev_lock);
     575           0 :         BUG_ON(!bd_may_claim(bdev, holder, hops));
     576             :         /*
     577             :          * Note that for a whole device bd_holders will be incremented twice,
     578             :          * and bd_holder will be set to bd_may_claim before being set to holder
     579             :          */
     580           0 :         whole->bd_holders++;
     581           0 :         whole->bd_holder = bd_may_claim;
     582           0 :         bdev->bd_holders++;
     583           0 :         mutex_lock(&bdev->bd_holder_lock);
     584           0 :         bdev->bd_holder = holder;
     585           0 :         bdev->bd_holder_ops = hops;
     586           0 :         mutex_unlock(&bdev->bd_holder_lock);
     587           0 :         bd_clear_claiming(whole, holder);
     588           0 :         mutex_unlock(&bdev_lock);
     589           0 : }
     590             : 
     591             : /**
     592             :  * bd_abort_claiming - abort claiming of a block device
     593             :  * @bdev: block device of interest
     594             :  * @holder: holder that has claimed @bdev
     595             :  *
     596             :  * Abort claiming of a block device when the exclusive open failed. This can be
     597             :  * also used when exclusive open is not actually desired and we just needed
     598             :  * to block other exclusive openers for a while.
     599             :  */
     600           0 : void bd_abort_claiming(struct block_device *bdev, void *holder)
     601             : {
     602           0 :         mutex_lock(&bdev_lock);
     603           0 :         bd_clear_claiming(bdev_whole(bdev), holder);
     604           0 :         mutex_unlock(&bdev_lock);
     605           0 : }
     606             : EXPORT_SYMBOL(bd_abort_claiming);
     607             : 
     608           0 : static void bd_end_claim(struct block_device *bdev, void *holder)
     609             : {
     610           0 :         struct block_device *whole = bdev_whole(bdev);
     611           0 :         bool unblock = false;
     612             : 
     613             :         /*
     614             :          * Release a claim on the device.  The holder fields are protected with
     615             :          * bdev_lock.  open_mutex is used to synchronize disk_holder unlinking.
     616             :          */
     617           0 :         mutex_lock(&bdev_lock);
     618           0 :         WARN_ON_ONCE(bdev->bd_holder != holder);
     619           0 :         WARN_ON_ONCE(--bdev->bd_holders < 0);
     620           0 :         WARN_ON_ONCE(--whole->bd_holders < 0);
     621           0 :         if (!bdev->bd_holders) {
     622           0 :                 mutex_lock(&bdev->bd_holder_lock);
     623           0 :                 bdev->bd_holder = NULL;
     624           0 :                 bdev->bd_holder_ops = NULL;
     625           0 :                 mutex_unlock(&bdev->bd_holder_lock);
     626           0 :                 if (bdev->bd_write_holder)
     627           0 :                         unblock = true;
     628             :         }
     629           0 :         if (!whole->bd_holders)
     630           0 :                 whole->bd_holder = NULL;
     631           0 :         mutex_unlock(&bdev_lock);
     632             : 
     633             :         /*
     634             :          * If this was the last claim, remove holder link and unblock evpoll if
     635             :          * it was a write holder.
     636             :          */
     637           0 :         if (unblock) {
     638           0 :                 disk_unblock_events(bdev->bd_disk);
     639           0 :                 bdev->bd_write_holder = false;
     640             :         }
     641           0 : }
     642             : 
     643           0 : static void blkdev_flush_mapping(struct block_device *bdev)
     644             : {
     645           0 :         WARN_ON_ONCE(bdev->bd_holders);
     646           0 :         sync_blockdev(bdev);
     647           0 :         kill_bdev(bdev);
     648           0 :         bdev_write_inode(bdev);
     649           0 : }
     650             : 
     651           0 : static int blkdev_get_whole(struct block_device *bdev, blk_mode_t mode)
     652             : {
     653           0 :         struct gendisk *disk = bdev->bd_disk;
     654             :         int ret;
     655             : 
     656           0 :         if (disk->fops->open) {
     657           0 :                 ret = disk->fops->open(disk, mode);
     658           0 :                 if (ret) {
     659             :                         /* avoid ghost partitions on a removed medium */
     660           0 :                         if (ret == -ENOMEDIUM &&
     661           0 :                              test_bit(GD_NEED_PART_SCAN, &disk->state))
     662           0 :                                 bdev_disk_changed(disk, true);
     663             :                         return ret;
     664             :                 }
     665             :         }
     666             : 
     667           0 :         if (!atomic_read(&bdev->bd_openers))
     668           0 :                 set_init_blocksize(bdev);
     669           0 :         if (test_bit(GD_NEED_PART_SCAN, &disk->state))
     670           0 :                 bdev_disk_changed(disk, false);
     671           0 :         atomic_inc(&bdev->bd_openers);
     672           0 :         return 0;
     673             : }
     674             : 
     675           0 : static void blkdev_put_whole(struct block_device *bdev)
     676             : {
     677           0 :         if (atomic_dec_and_test(&bdev->bd_openers))
     678           0 :                 blkdev_flush_mapping(bdev);
     679           0 :         if (bdev->bd_disk->fops->release)
     680           0 :                 bdev->bd_disk->fops->release(bdev->bd_disk);
     681           0 : }
     682             : 
     683           0 : static int blkdev_get_part(struct block_device *part, blk_mode_t mode)
     684             : {
     685           0 :         struct gendisk *disk = part->bd_disk;
     686             :         int ret;
     687             : 
     688           0 :         ret = blkdev_get_whole(bdev_whole(part), mode);
     689           0 :         if (ret)
     690             :                 return ret;
     691             : 
     692           0 :         ret = -ENXIO;
     693           0 :         if (!bdev_nr_sectors(part))
     694             :                 goto out_blkdev_put;
     695             : 
     696           0 :         if (!atomic_read(&part->bd_openers)) {
     697           0 :                 disk->open_partitions++;
     698           0 :                 set_init_blocksize(part);
     699             :         }
     700           0 :         atomic_inc(&part->bd_openers);
     701           0 :         return 0;
     702             : 
     703             : out_blkdev_put:
     704           0 :         blkdev_put_whole(bdev_whole(part));
     705           0 :         return ret;
     706             : }
     707             : 
     708           0 : static void blkdev_put_part(struct block_device *part)
     709             : {
     710           0 :         struct block_device *whole = bdev_whole(part);
     711             : 
     712           0 :         if (atomic_dec_and_test(&part->bd_openers)) {
     713           0 :                 blkdev_flush_mapping(part);
     714           0 :                 whole->bd_disk->open_partitions--;
     715             :         }
     716           0 :         blkdev_put_whole(whole);
     717           0 : }
     718             : 
     719           0 : struct block_device *blkdev_get_no_open(dev_t dev)
     720             : {
     721             :         struct block_device *bdev;
     722             :         struct inode *inode;
     723             : 
     724           0 :         inode = ilookup(blockdev_superblock, dev);
     725           0 :         if (!inode && IS_ENABLED(CONFIG_BLOCK_LEGACY_AUTOLOAD)) {
     726           0 :                 blk_request_module(dev);
     727           0 :                 inode = ilookup(blockdev_superblock, dev);
     728           0 :                 if (inode)
     729           0 :                         pr_warn_ratelimited(
     730             : "block device autoloading is deprecated and will be removed.\n");
     731             :         }
     732           0 :         if (!inode)
     733             :                 return NULL;
     734             : 
     735             :         /* switch from the inode reference to a device mode one: */
     736           0 :         bdev = &BDEV_I(inode)->bdev;
     737           0 :         if (!kobject_get_unless_zero(&bdev->bd_device.kobj))
     738           0 :                 bdev = NULL;
     739           0 :         iput(inode);
     740           0 :         return bdev;
     741             : }
     742             : 
     743           0 : void blkdev_put_no_open(struct block_device *bdev)
     744             : {
     745           0 :         put_device(&bdev->bd_device);
     746           0 : }
     747             :         
     748             : /**
     749             :  * blkdev_get_by_dev - open a block device by device number
     750             :  * @dev: device number of block device to open
     751             :  * @mode: open mode (BLK_OPEN_*)
     752             :  * @holder: exclusive holder identifier
     753             :  * @hops: holder operations
     754             :  *
     755             :  * Open the block device described by device number @dev. If @holder is not
     756             :  * %NULL, the block device is opened with exclusive access.  Exclusive opens may
     757             :  * nest for the same @holder.
     758             :  *
     759             :  * Use this interface ONLY if you really do not have anything better - i.e. when
     760             :  * you are behind a truly sucky interface and all you are given is a device
     761             :  * number.  Everything else should use blkdev_get_by_path().
     762             :  *
     763             :  * CONTEXT:
     764             :  * Might sleep.
     765             :  *
     766             :  * RETURNS:
     767             :  * Reference to the block_device on success, ERR_PTR(-errno) on failure.
     768             :  */
     769           0 : struct block_device *blkdev_get_by_dev(dev_t dev, blk_mode_t mode, void *holder,
     770             :                 const struct blk_holder_ops *hops)
     771             : {
     772           0 :         bool unblock_events = true;
     773             :         struct block_device *bdev;
     774             :         struct gendisk *disk;
     775             :         int ret;
     776             : 
     777           0 :         ret = devcgroup_check_permission(DEVCG_DEV_BLOCK,
     778           0 :                         MAJOR(dev), MINOR(dev),
     779           0 :                         ((mode & BLK_OPEN_READ) ? DEVCG_ACC_READ : 0) |
     780             :                         ((mode & BLK_OPEN_WRITE) ? DEVCG_ACC_WRITE : 0));
     781             :         if (ret)
     782             :                 return ERR_PTR(ret);
     783             : 
     784           0 :         bdev = blkdev_get_no_open(dev);
     785           0 :         if (!bdev)
     786             :                 return ERR_PTR(-ENXIO);
     787           0 :         disk = bdev->bd_disk;
     788             : 
     789           0 :         if (holder) {
     790           0 :                 mode |= BLK_OPEN_EXCL;
     791           0 :                 ret = bd_prepare_to_claim(bdev, holder, hops);
     792           0 :                 if (ret)
     793             :                         goto put_blkdev;
     794             :         } else {
     795           0 :                 if (WARN_ON_ONCE(mode & BLK_OPEN_EXCL)) {
     796             :                         ret = -EIO;
     797             :                         goto put_blkdev;
     798             :                 }
     799             :         }
     800             : 
     801           0 :         disk_block_events(disk);
     802             : 
     803           0 :         mutex_lock(&disk->open_mutex);
     804           0 :         ret = -ENXIO;
     805           0 :         if (!disk_live(disk))
     806             :                 goto abort_claiming;
     807           0 :         if (!try_module_get(disk->fops->owner))
     808             :                 goto abort_claiming;
     809           0 :         if (bdev_is_partition(bdev))
     810           0 :                 ret = blkdev_get_part(bdev, mode);
     811             :         else
     812           0 :                 ret = blkdev_get_whole(bdev, mode);
     813           0 :         if (ret)
     814             :                 goto put_module;
     815           0 :         if (holder) {
     816           0 :                 bd_finish_claiming(bdev, holder, hops);
     817             : 
     818             :                 /*
     819             :                  * Block event polling for write claims if requested.  Any write
     820             :                  * holder makes the write_holder state stick until all are
     821             :                  * released.  This is good enough and tracking individual
     822             :                  * writeable reference is too fragile given the way @mode is
     823             :                  * used in blkdev_get/put().
     824             :                  */
     825           0 :                 if ((mode & BLK_OPEN_WRITE) && !bdev->bd_write_holder &&
     826           0 :                     (disk->event_flags & DISK_EVENT_FLAG_BLOCK_ON_EXCL_WRITE)) {
     827           0 :                         bdev->bd_write_holder = true;
     828           0 :                         unblock_events = false;
     829             :                 }
     830             :         }
     831           0 :         mutex_unlock(&disk->open_mutex);
     832             : 
     833           0 :         if (unblock_events)
     834           0 :                 disk_unblock_events(disk);
     835             :         return bdev;
     836             : put_module:
     837             :         module_put(disk->fops->owner);
     838             : abort_claiming:
     839           0 :         if (holder)
     840           0 :                 bd_abort_claiming(bdev, holder);
     841           0 :         mutex_unlock(&disk->open_mutex);
     842           0 :         disk_unblock_events(disk);
     843             : put_blkdev:
     844           0 :         blkdev_put_no_open(bdev);
     845           0 :         return ERR_PTR(ret);
     846             : }
     847             : EXPORT_SYMBOL(blkdev_get_by_dev);
     848             : 
     849             : /**
     850             :  * blkdev_get_by_path - open a block device by name
     851             :  * @path: path to the block device to open
     852             :  * @mode: open mode (BLK_OPEN_*)
     853             :  * @holder: exclusive holder identifier
     854             :  * @hops: holder operations
     855             :  *
     856             :  * Open the block device described by the device file at @path.  If @holder is
     857             :  * not %NULL, the block device is opened with exclusive access.  Exclusive opens
     858             :  * may nest for the same @holder.
     859             :  *
     860             :  * CONTEXT:
     861             :  * Might sleep.
     862             :  *
     863             :  * RETURNS:
     864             :  * Reference to the block_device on success, ERR_PTR(-errno) on failure.
     865             :  */
     866           0 : struct block_device *blkdev_get_by_path(const char *path, blk_mode_t mode,
     867             :                 void *holder, const struct blk_holder_ops *hops)
     868             : {
     869             :         struct block_device *bdev;
     870             :         dev_t dev;
     871             :         int error;
     872             : 
     873           0 :         error = lookup_bdev(path, &dev);
     874           0 :         if (error)
     875           0 :                 return ERR_PTR(error);
     876             : 
     877           0 :         bdev = blkdev_get_by_dev(dev, mode, holder, hops);
     878           0 :         if (!IS_ERR(bdev) && (mode & BLK_OPEN_WRITE) && bdev_read_only(bdev)) {
     879           0 :                 blkdev_put(bdev, holder);
     880           0 :                 return ERR_PTR(-EACCES);
     881             :         }
     882             : 
     883             :         return bdev;
     884             : }
     885             : EXPORT_SYMBOL(blkdev_get_by_path);
     886             : 
     887           0 : void blkdev_put(struct block_device *bdev, void *holder)
     888             : {
     889           0 :         struct gendisk *disk = bdev->bd_disk;
     890             : 
     891             :         /*
     892             :          * Sync early if it looks like we're the last one.  If someone else
     893             :          * opens the block device between now and the decrement of bd_openers
     894             :          * then we did a sync that we didn't need to, but that's not the end
     895             :          * of the world and we want to avoid long (could be several minute)
     896             :          * syncs while holding the mutex.
     897             :          */
     898           0 :         if (atomic_read(&bdev->bd_openers) == 1)
     899             :                 sync_blockdev(bdev);
     900             : 
     901           0 :         mutex_lock(&disk->open_mutex);
     902           0 :         if (holder)
     903           0 :                 bd_end_claim(bdev, holder);
     904             : 
     905             :         /*
     906             :          * Trigger event checking and tell drivers to flush MEDIA_CHANGE
     907             :          * event.  This is to ensure detection of media removal commanded
     908             :          * from userland - e.g. eject(1).
     909             :          */
     910           0 :         disk_flush_events(disk, DISK_EVENT_MEDIA_CHANGE);
     911             : 
     912           0 :         if (bdev_is_partition(bdev))
     913           0 :                 blkdev_put_part(bdev);
     914             :         else
     915           0 :                 blkdev_put_whole(bdev);
     916           0 :         mutex_unlock(&disk->open_mutex);
     917             : 
     918           0 :         module_put(disk->fops->owner);
     919           0 :         blkdev_put_no_open(bdev);
     920           0 : }
     921             : EXPORT_SYMBOL(blkdev_put);
     922             : 
     923             : /**
     924             :  * lookup_bdev() - Look up a struct block_device by name.
     925             :  * @pathname: Name of the block device in the filesystem.
     926             :  * @dev: Pointer to the block device's dev_t, if found.
     927             :  *
     928             :  * Lookup the block device's dev_t at @pathname in the current
     929             :  * namespace if possible and return it in @dev.
     930             :  *
     931             :  * Context: May sleep.
     932             :  * Return: 0 if succeeded, negative errno otherwise.
     933             :  */
     934           0 : int lookup_bdev(const char *pathname, dev_t *dev)
     935             : {
     936             :         struct inode *inode;
     937             :         struct path path;
     938             :         int error;
     939             : 
     940           0 :         if (!pathname || !*pathname)
     941             :                 return -EINVAL;
     942             : 
     943           0 :         error = kern_path(pathname, LOOKUP_FOLLOW, &path);
     944           0 :         if (error)
     945             :                 return error;
     946             : 
     947           0 :         inode = d_backing_inode(path.dentry);
     948           0 :         error = -ENOTBLK;
     949           0 :         if (!S_ISBLK(inode->i_mode))
     950             :                 goto out_path_put;
     951           0 :         error = -EACCES;
     952           0 :         if (!may_open_dev(&path))
     953             :                 goto out_path_put;
     954             : 
     955           0 :         *dev = inode->i_rdev;
     956           0 :         error = 0;
     957             : out_path_put:
     958           0 :         path_put(&path);
     959           0 :         return error;
     960             : }
     961             : EXPORT_SYMBOL(lookup_bdev);
     962             : 
     963           0 : int __invalidate_device(struct block_device *bdev, bool kill_dirty)
     964             : {
     965           0 :         struct super_block *sb = get_super(bdev);
     966           0 :         int res = 0;
     967             : 
     968           0 :         if (sb) {
     969             :                 /*
     970             :                  * no need to lock the super, get_super holds the
     971             :                  * read mutex so the filesystem cannot go away
     972             :                  * under us (->put_super runs with the write lock
     973             :                  * hold).
     974             :                  */
     975           0 :                 shrink_dcache_sb(sb);
     976           0 :                 res = invalidate_inodes(sb, kill_dirty);
     977           0 :                 drop_super(sb);
     978             :         }
     979           0 :         invalidate_bdev(bdev);
     980           0 :         return res;
     981             : }
     982             : EXPORT_SYMBOL(__invalidate_device);
     983             : 
     984           0 : void sync_bdevs(bool wait)
     985             : {
     986           0 :         struct inode *inode, *old_inode = NULL;
     987             : 
     988           0 :         spin_lock(&blockdev_superblock->s_inode_list_lock);
     989           0 :         list_for_each_entry(inode, &blockdev_superblock->s_inodes, i_sb_list) {
     990           0 :                 struct address_space *mapping = inode->i_mapping;
     991             :                 struct block_device *bdev;
     992             : 
     993           0 :                 spin_lock(&inode->i_lock);
     994           0 :                 if (inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW) ||
     995           0 :                     mapping->nrpages == 0) {
     996           0 :                         spin_unlock(&inode->i_lock);
     997           0 :                         continue;
     998             :                 }
     999           0 :                 __iget(inode);
    1000           0 :                 spin_unlock(&inode->i_lock);
    1001           0 :                 spin_unlock(&blockdev_superblock->s_inode_list_lock);
    1002             :                 /*
    1003             :                  * We hold a reference to 'inode' so it couldn't have been
    1004             :                  * removed from s_inodes list while we dropped the
    1005             :                  * s_inode_list_lock  We cannot iput the inode now as we can
    1006             :                  * be holding the last reference and we cannot iput it under
    1007             :                  * s_inode_list_lock. So we keep the reference and iput it
    1008             :                  * later.
    1009             :                  */
    1010           0 :                 iput(old_inode);
    1011           0 :                 old_inode = inode;
    1012           0 :                 bdev = I_BDEV(inode);
    1013             : 
    1014           0 :                 mutex_lock(&bdev->bd_disk->open_mutex);
    1015           0 :                 if (!atomic_read(&bdev->bd_openers)) {
    1016             :                         ; /* skip */
    1017           0 :                 } else if (wait) {
    1018             :                         /*
    1019             :                          * We keep the error status of individual mapping so
    1020             :                          * that applications can catch the writeback error using
    1021             :                          * fsync(2). See filemap_fdatawait_keep_errors() for
    1022             :                          * details.
    1023             :                          */
    1024           0 :                         filemap_fdatawait_keep_errors(inode->i_mapping);
    1025             :                 } else {
    1026           0 :                         filemap_fdatawrite(inode->i_mapping);
    1027             :                 }
    1028           0 :                 mutex_unlock(&bdev->bd_disk->open_mutex);
    1029             : 
    1030           0 :                 spin_lock(&blockdev_superblock->s_inode_list_lock);
    1031             :         }
    1032           0 :         spin_unlock(&blockdev_superblock->s_inode_list_lock);
    1033           0 :         iput(old_inode);
    1034           0 : }
    1035             : 
    1036             : /*
    1037             :  * Handle STATX_DIOALIGN for block devices.
    1038             :  *
    1039             :  * Note that the inode passed to this is the inode of a block device node file,
    1040             :  * not the block device's internal inode.  Therefore it is *not* valid to use
    1041             :  * I_BDEV() here; the block device has to be looked up by i_rdev instead.
    1042             :  */
    1043           0 : void bdev_statx_dioalign(struct inode *inode, struct kstat *stat)
    1044             : {
    1045             :         struct block_device *bdev;
    1046             : 
    1047           0 :         bdev = blkdev_get_no_open(inode->i_rdev);
    1048           0 :         if (!bdev)
    1049             :                 return;
    1050             : 
    1051           0 :         stat->dio_mem_align = bdev_dma_alignment(bdev) + 1;
    1052           0 :         stat->dio_offset_align = bdev_logical_block_size(bdev);
    1053           0 :         stat->result_mask |= STATX_DIOALIGN;
    1054             : 
    1055             :         blkdev_put_no_open(bdev);
    1056             : }

Generated by: LCOV version 1.14