^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * linux/fs/block_dev.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 1991, 1992 Linus Torvalds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (C) 2001 Andrea Arcangeli <andrea@suse.de> SuSE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/kmod.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/major.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/device_cgroup.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/highmem.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/blkdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/backing-dev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/blkpg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/magic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/buffer_head.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/swap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/pagevec.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/writeback.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/mpage.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/mount.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/pseudo_fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <linux/uio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <linux/namei.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include <linux/log2.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include <linux/cleancache.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include <linux/task_io_accounting_ops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #include <linux/falloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #include <linux/suspend.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #include "internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) struct bdev_inode {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) struct block_device bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) struct inode vfs_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) static const struct address_space_operations def_blk_aops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) static inline struct bdev_inode *BDEV_I(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) return container_of(inode, struct bdev_inode, vfs_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) struct block_device *I_BDEV(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) return &BDEV_I(inode)->bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) EXPORT_SYMBOL(I_BDEV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) static void bdev_write_inode(struct block_device *bdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) struct inode *inode = bdev->bd_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) spin_lock(&inode->i_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) while (inode->i_state & I_DIRTY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) spin_unlock(&inode->i_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) ret = write_inode_now(inode, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) char name[BDEVNAME_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) pr_warn_ratelimited("VFS: Dirty inode writeback failed "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) "for block device %s (err=%d).\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) bdevname(bdev, name), ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) spin_lock(&inode->i_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) spin_unlock(&inode->i_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) /* Kill _all_ buffers and pagecache , dirty or not.. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) static void kill_bdev(struct block_device *bdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) struct address_space *mapping = bdev->bd_inode->i_mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) if (mapping->nrpages == 0 && mapping->nrexceptional == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) invalidate_bh_lrus();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) truncate_inode_pages(mapping, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) /* Invalidate clean unused buffers and pagecache. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) void invalidate_bdev(struct block_device *bdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) struct address_space *mapping = bdev->bd_inode->i_mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) if (mapping->nrpages) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) invalidate_bh_lrus();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) lru_add_drain_all(); /* make sure all lru add caches are flushed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) invalidate_mapping_pages(mapping, 0, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) /* 99% of the time, we don't need to flush the cleancache on the bdev.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * But, for the strange corners, lets be cautious
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) cleancache_invalidate_inode(mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) EXPORT_SYMBOL(invalidate_bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) * Drop all buffers & page cache for given bdev range. This function bails
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) * with error if bdev has other exclusive owner (such as filesystem).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) int truncate_bdev_range(struct block_device *bdev, fmode_t mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) loff_t lstart, loff_t lend)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) struct block_device *claimed_bdev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) * If we don't hold exclusive handle for the device, upgrade to it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) * while we discard the buffer cache to avoid discarding buffers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) * under live filesystem.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) if (!(mode & FMODE_EXCL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) claimed_bdev = bdev->bd_contains;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) err = bd_prepare_to_claim(bdev, claimed_bdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) truncate_bdev_range);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) goto invalidate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) truncate_inode_pages_range(bdev->bd_inode->i_mapping, lstart, lend);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) if (claimed_bdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) bd_abort_claiming(bdev, claimed_bdev, truncate_bdev_range);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) invalidate:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) * Someone else has handle exclusively open. Try invalidating instead.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) * The 'end' argument is inclusive so the rounding is safe.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) return invalidate_inode_pages2_range(bdev->bd_inode->i_mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) lstart >> PAGE_SHIFT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) lend >> PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) EXPORT_SYMBOL(truncate_bdev_range);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) static void set_init_blocksize(struct block_device *bdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) unsigned int bsize = bdev_logical_block_size(bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) loff_t size = i_size_read(bdev->bd_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) while (bsize < PAGE_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) if (size & bsize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) bsize <<= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) bdev->bd_inode->i_blkbits = blksize_bits(bsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) int set_blocksize(struct block_device *bdev, int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) /* Size must be a power of two, and between 512 and PAGE_SIZE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) if (size > PAGE_SIZE || size < 512 || !is_power_of_2(size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) /* Size cannot be smaller than the size supported by the device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) if (size < bdev_logical_block_size(bdev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) /* Don't change the size if it is same as current */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) if (bdev->bd_inode->i_blkbits != blksize_bits(size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) sync_blockdev(bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) bdev->bd_inode->i_blkbits = blksize_bits(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) kill_bdev(bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) EXPORT_SYMBOL(set_blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) int sb_set_blocksize(struct super_block *sb, int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) if (set_blocksize(sb->s_bdev, size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) /* If we get here, we know size is power of two
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) * and it's value is between 512 and PAGE_SIZE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) sb->s_blocksize = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) sb->s_blocksize_bits = blksize_bits(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) return sb->s_blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) EXPORT_SYMBOL_NS(sb_set_blocksize, ANDROID_GKI_VFS_EXPORT_ONLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) int sb_min_blocksize(struct super_block *sb, int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) int minsize = bdev_logical_block_size(sb->s_bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) if (size < minsize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) size = minsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) return sb_set_blocksize(sb, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) EXPORT_SYMBOL_NS(sb_min_blocksize, ANDROID_GKI_VFS_EXPORT_ONLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) blkdev_get_block(struct inode *inode, sector_t iblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) struct buffer_head *bh, int create)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) bh->b_bdev = I_BDEV(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) bh->b_blocknr = iblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) set_buffer_mapped(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) static struct inode *bdev_file_inode(struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) return file->f_mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) static unsigned int dio_bio_write_op(struct kiocb *iocb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) unsigned int op = REQ_OP_WRITE | REQ_SYNC | REQ_IDLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) /* avoid the need for a I/O completion work item */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) if (iocb->ki_flags & IOCB_DSYNC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) op |= REQ_FUA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) return op;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) #define DIO_INLINE_BIO_VECS 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) static void blkdev_bio_end_io_simple(struct bio *bio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) struct task_struct *waiter = bio->bi_private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) WRITE_ONCE(bio->bi_private, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) blk_wake_io_task(waiter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) __blkdev_direct_IO_simple(struct kiocb *iocb, struct iov_iter *iter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) int nr_pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) struct file *file = iocb->ki_filp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) struct block_device *bdev = I_BDEV(bdev_file_inode(file));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) struct bio_vec inline_vecs[DIO_INLINE_BIO_VECS], *vecs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) loff_t pos = iocb->ki_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) bool should_dirty = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) struct bio bio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) blk_qc_t qc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) if ((pos | iov_iter_alignment(iter)) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) (bdev_logical_block_size(bdev) - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) if (nr_pages <= DIO_INLINE_BIO_VECS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) vecs = inline_vecs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) vecs = kmalloc_array(nr_pages, sizeof(struct bio_vec),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) if (!vecs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) bio_init(&bio, vecs, nr_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) bio_set_dev(&bio, bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) bio.bi_iter.bi_sector = pos >> 9;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) bio.bi_write_hint = iocb->ki_hint;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) bio.bi_private = current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) bio.bi_end_io = blkdev_bio_end_io_simple;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) bio.bi_ioprio = iocb->ki_ioprio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) ret = bio_iov_iter_get_pages(&bio, iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) if (unlikely(ret))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) ret = bio.bi_iter.bi_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) if (iov_iter_rw(iter) == READ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) bio.bi_opf = REQ_OP_READ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) if (iter_is_iovec(iter))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) should_dirty = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) bio.bi_opf = dio_bio_write_op(iocb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) task_io_account_write(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) if (iocb->ki_flags & IOCB_NOWAIT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) bio.bi_opf |= REQ_NOWAIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) if (iocb->ki_flags & IOCB_HIPRI)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) bio_set_polled(&bio, iocb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) qc = submit_bio(&bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) set_current_state(TASK_UNINTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) if (!READ_ONCE(bio.bi_private))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) if (!(iocb->ki_flags & IOCB_HIPRI) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) !blk_poll(bdev_get_queue(bdev), qc, true))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) blk_io_schedule();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) __set_current_state(TASK_RUNNING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) bio_release_pages(&bio, should_dirty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) if (unlikely(bio.bi_status))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) ret = blk_status_to_errno(bio.bi_status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) if (vecs != inline_vecs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) kfree(vecs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) bio_uninit(&bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) struct blkdev_dio {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) struct kiocb *iocb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) struct task_struct *waiter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) size_t size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) atomic_t ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) bool multi_bio : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) bool should_dirty : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) bool is_sync : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) struct bio bio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) static struct bio_set blkdev_dio_pool;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) static int blkdev_iopoll(struct kiocb *kiocb, bool wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) struct block_device *bdev = I_BDEV(kiocb->ki_filp->f_mapping->host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) struct request_queue *q = bdev_get_queue(bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) return blk_poll(q, READ_ONCE(kiocb->ki_cookie), wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) static void blkdev_bio_end_io(struct bio *bio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) struct blkdev_dio *dio = bio->bi_private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) bool should_dirty = dio->should_dirty;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) if (bio->bi_status && !dio->bio.bi_status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) dio->bio.bi_status = bio->bi_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) if (!dio->multi_bio || atomic_dec_and_test(&dio->ref)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) if (!dio->is_sync) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) struct kiocb *iocb = dio->iocb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) if (likely(!dio->bio.bi_status)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) ret = dio->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) iocb->ki_pos += ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) ret = blk_status_to_errno(dio->bio.bi_status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) dio->iocb->ki_complete(iocb, ret, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) if (dio->multi_bio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) bio_put(&dio->bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) struct task_struct *waiter = dio->waiter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) WRITE_ONCE(dio->waiter, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) blk_wake_io_task(waiter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) if (should_dirty) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) bio_check_pages_dirty(bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) bio_release_pages(bio, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) bio_put(bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) __blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter, int nr_pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) struct file *file = iocb->ki_filp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) struct inode *inode = bdev_file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) struct block_device *bdev = I_BDEV(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) struct blk_plug plug;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) struct blkdev_dio *dio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) struct bio *bio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) bool is_poll = (iocb->ki_flags & IOCB_HIPRI) != 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) bool is_read = (iov_iter_rw(iter) == READ), is_sync;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) loff_t pos = iocb->ki_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) blk_qc_t qc = BLK_QC_T_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) if ((pos | iov_iter_alignment(iter)) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) (bdev_logical_block_size(bdev) - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) bio = bio_alloc_bioset(GFP_KERNEL, nr_pages, &blkdev_dio_pool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) dio = container_of(bio, struct blkdev_dio, bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) dio->is_sync = is_sync = is_sync_kiocb(iocb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) if (dio->is_sync) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) dio->waiter = current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) bio_get(bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) dio->iocb = iocb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) dio->size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) dio->multi_bio = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) dio->should_dirty = is_read && iter_is_iovec(iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) * Don't plug for HIPRI/polled IO, as those should go straight
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) * to issue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) if (!is_poll)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) blk_start_plug(&plug);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) bio_set_dev(bio, bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) bio->bi_iter.bi_sector = pos >> 9;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) bio->bi_write_hint = iocb->ki_hint;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) bio->bi_private = dio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) bio->bi_end_io = blkdev_bio_end_io;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) bio->bi_ioprio = iocb->ki_ioprio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) ret = bio_iov_iter_get_pages(bio, iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) if (unlikely(ret)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) bio->bi_status = BLK_STS_IOERR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) bio_endio(bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) if (is_read) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) bio->bi_opf = REQ_OP_READ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) if (dio->should_dirty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) bio_set_pages_dirty(bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) bio->bi_opf = dio_bio_write_op(iocb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) task_io_account_write(bio->bi_iter.bi_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) if (iocb->ki_flags & IOCB_NOWAIT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) bio->bi_opf |= REQ_NOWAIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) dio->size += bio->bi_iter.bi_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) pos += bio->bi_iter.bi_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) nr_pages = iov_iter_npages(iter, BIO_MAX_PAGES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) if (!nr_pages) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) bool polled = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) if (iocb->ki_flags & IOCB_HIPRI) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) bio_set_polled(bio, iocb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) polled = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) qc = submit_bio(bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) if (polled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) WRITE_ONCE(iocb->ki_cookie, qc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) if (!dio->multi_bio) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) * AIO needs an extra reference to ensure the dio
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) * structure which is embedded into the first bio
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) * stays around.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) if (!is_sync)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) bio_get(bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) dio->multi_bio = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) atomic_set(&dio->ref, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) atomic_inc(&dio->ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) submit_bio(bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) bio = bio_alloc(GFP_KERNEL, nr_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) if (!is_poll)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) blk_finish_plug(&plug);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) if (!is_sync)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) return -EIOCBQUEUED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) set_current_state(TASK_UNINTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) if (!READ_ONCE(dio->waiter))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) if (!(iocb->ki_flags & IOCB_HIPRI) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) !blk_poll(bdev_get_queue(bdev), qc, true))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) blk_io_schedule();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) __set_current_state(TASK_RUNNING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) ret = blk_status_to_errno(dio->bio.bi_status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) if (likely(!ret))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) ret = dio->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) bio_put(&dio->bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) int nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) nr_pages = iov_iter_npages(iter, BIO_MAX_PAGES + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) if (!nr_pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) if (is_sync_kiocb(iocb) && nr_pages <= BIO_MAX_PAGES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) return __blkdev_direct_IO_simple(iocb, iter, nr_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) return __blkdev_direct_IO(iocb, iter, min(nr_pages, BIO_MAX_PAGES));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) static __init int blkdev_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) return bioset_init(&blkdev_dio_pool, 4, offsetof(struct blkdev_dio, bio), BIOSET_NEED_BVECS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) module_init(blkdev_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) int __sync_blockdev(struct block_device *bdev, int wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) if (!bdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) if (!wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) return filemap_flush(bdev->bd_inode->i_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) return filemap_write_and_wait(bdev->bd_inode->i_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) * Write out and wait upon all the dirty data associated with a block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) * device via its mapping. Does not take the superblock lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) int sync_blockdev(struct block_device *bdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) return __sync_blockdev(bdev, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) EXPORT_SYMBOL(sync_blockdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) * Write out and wait upon all dirty data associated with this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) * device. Filesystem data as well as the underlying block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) * device. Takes the superblock lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) int fsync_bdev(struct block_device *bdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) struct super_block *sb = get_super(bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) if (sb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) int res = sync_filesystem(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) drop_super(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) return sync_blockdev(bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) EXPORT_SYMBOL(fsync_bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) * freeze_bdev -- lock a filesystem and force it into a consistent state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) * @bdev: blockdevice to lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) * If a superblock is found on this device, we take the s_umount semaphore
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) * on it to make sure nobody unmounts until the snapshot creation is done.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) * The reference counter (bd_fsfreeze_count) guarantees that only the last
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) * unfreeze process can unfreeze the frozen filesystem actually when multiple
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) * freeze requests arrive simultaneously. It counts up in freeze_bdev() and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) * count down in thaw_bdev(). When it becomes 0, thaw_bdev() will unfreeze
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) * actually.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) int freeze_bdev(struct block_device *bdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) struct super_block *sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) int error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) mutex_lock(&bdev->bd_fsfreeze_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) if (++bdev->bd_fsfreeze_count > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) sb = get_active_super(bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) if (!sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) goto sync;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) if (sb->s_op->freeze_super)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) error = sb->s_op->freeze_super(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) error = freeze_super(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) deactivate_super(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) bdev->bd_fsfreeze_count--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) bdev->bd_fsfreeze_sb = sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) sync:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) sync_blockdev(bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) mutex_unlock(&bdev->bd_fsfreeze_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) EXPORT_SYMBOL(freeze_bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) * thaw_bdev -- unlock filesystem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) * @bdev: blockdevice to unlock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) * Unlocks the filesystem and marks it writeable again after freeze_bdev().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) int thaw_bdev(struct block_device *bdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) struct super_block *sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) int error = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) mutex_lock(&bdev->bd_fsfreeze_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) if (!bdev->bd_fsfreeze_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) if (--bdev->bd_fsfreeze_count > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) sb = bdev->bd_fsfreeze_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) if (!sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) if (sb->s_op->thaw_super)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) error = sb->s_op->thaw_super(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) error = thaw_super(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) bdev->bd_fsfreeze_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) mutex_unlock(&bdev->bd_fsfreeze_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) EXPORT_SYMBOL(thaw_bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) static int blkdev_writepage(struct page *page, struct writeback_control *wbc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) return block_write_full_page(page, blkdev_get_block, wbc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) static int blkdev_readpage(struct file * file, struct page * page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) return block_read_full_page(page, blkdev_get_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) static void blkdev_readahead(struct readahead_control *rac)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) mpage_readahead(rac, blkdev_get_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) static int blkdev_write_begin(struct file *file, struct address_space *mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) loff_t pos, unsigned len, unsigned flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) struct page **pagep, void **fsdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) return block_write_begin(mapping, pos, len, flags, pagep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) blkdev_get_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) static int blkdev_write_end(struct file *file, struct address_space *mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) loff_t pos, unsigned len, unsigned copied,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) struct page *page, void *fsdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) ret = block_write_end(file, mapping, pos, len, copied, page, fsdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) * private llseek:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) * for a block special file file_inode(file)->i_size is zero
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) * so we compute the size by hand (just as in block_read/write above)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) static loff_t block_llseek(struct file *file, loff_t offset, int whence)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) struct inode *bd_inode = bdev_file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) loff_t retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) inode_lock(bd_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) retval = fixed_size_llseek(file, offset, whence, i_size_read(bd_inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) inode_unlock(bd_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) int blkdev_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) struct inode *bd_inode = bdev_file_inode(filp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) struct block_device *bdev = I_BDEV(bd_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) error = file_write_and_wait_range(filp, start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) * There is no need to serialise calls to blkdev_issue_flush with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) * i_mutex and doing so causes performance issues with concurrent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) * O_SYNC writers to a block device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) error = blkdev_issue_flush(bdev, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) if (error == -EOPNOTSUPP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) EXPORT_SYMBOL(blkdev_fsync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) * bdev_read_page() - Start reading a page from a block device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) * @bdev: The device to read the page from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) * @sector: The offset on the device to read the page to (need not be aligned)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) * @page: The page to read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) * On entry, the page should be locked. It will be unlocked when the page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) * has been read. If the block driver implements rw_page synchronously,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) * that will be true on exit from this function, but it need not be.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) * Errors returned by this function are usually "soft", eg out of memory, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) * queue full; callers should try a different route to read this page rather
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) * than propagate an error back up the stack.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) * Return: negative errno if an error occurs, 0 if submission was successful.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) int bdev_read_page(struct block_device *bdev, sector_t sector,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) const struct block_device_operations *ops = bdev->bd_disk->fops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) int result = -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) if (!ops->rw_page || bdev_get_integrity(bdev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) result = blk_queue_enter(bdev->bd_disk->queue, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) result = ops->rw_page(bdev, sector + get_start_sect(bdev), page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) REQ_OP_READ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) blk_queue_exit(bdev->bd_disk->queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) * bdev_write_page() - Start writing a page to a block device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) * @bdev: The device to write the page to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) * @sector: The offset on the device to write the page to (need not be aligned)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) * @page: The page to write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) * @wbc: The writeback_control for the write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) * On entry, the page should be locked and not currently under writeback.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) * On exit, if the write started successfully, the page will be unlocked and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) * under writeback. If the write failed already (eg the driver failed to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) * queue the page to the device), the page will still be locked. If the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) * caller is a ->writepage implementation, it will need to unlock the page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) * Errors returned by this function are usually "soft", eg out of memory, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) * queue full; callers should try a different route to write this page rather
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) * than propagate an error back up the stack.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) * Return: negative errno if an error occurs, 0 if submission was successful.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) int bdev_write_page(struct block_device *bdev, sector_t sector,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) struct page *page, struct writeback_control *wbc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) const struct block_device_operations *ops = bdev->bd_disk->fops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) if (!ops->rw_page || bdev_get_integrity(bdev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) result = blk_queue_enter(bdev->bd_disk->queue, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) set_page_writeback(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) result = ops->rw_page(bdev, sector + get_start_sect(bdev), page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) REQ_OP_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) if (result) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) end_page_writeback(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) clean_page_buffers(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) blk_queue_exit(bdev->bd_disk->queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) * pseudo-fs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) static __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) static struct kmem_cache * bdev_cachep __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) static struct inode *bdev_alloc_inode(struct super_block *sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) struct bdev_inode *ei = kmem_cache_alloc(bdev_cachep, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) if (!ei)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) return &ei->vfs_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) static void bdev_free_inode(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) kmem_cache_free(bdev_cachep, BDEV_I(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) static void init_once(void *foo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) struct bdev_inode *ei = (struct bdev_inode *) foo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) struct block_device *bdev = &ei->bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) memset(bdev, 0, sizeof(*bdev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) mutex_init(&bdev->bd_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) #ifdef CONFIG_SYSFS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) INIT_LIST_HEAD(&bdev->bd_holder_disks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) bdev->bd_bdi = &noop_backing_dev_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) inode_init_once(&ei->vfs_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) /* Initialize mutex for freeze. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) mutex_init(&bdev->bd_fsfreeze_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) static void bdev_evict_inode(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) struct block_device *bdev = &BDEV_I(inode)->bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) truncate_inode_pages_final(&inode->i_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) invalidate_inode_buffers(inode); /* is it needed here? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) clear_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) /* Detach inode from wb early as bdi_put() may free bdi->wb */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) inode_detach_wb(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) if (bdev->bd_bdi != &noop_backing_dev_info) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) bdi_put(bdev->bd_bdi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) bdev->bd_bdi = &noop_backing_dev_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) static const struct super_operations bdev_sops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) .statfs = simple_statfs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) .alloc_inode = bdev_alloc_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) .free_inode = bdev_free_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) .drop_inode = generic_delete_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) .evict_inode = bdev_evict_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) static int bd_init_fs_context(struct fs_context *fc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) struct pseudo_fs_context *ctx = init_pseudo(fc, BDEVFS_MAGIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) if (!ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) fc->s_iflags |= SB_I_CGROUPWB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) ctx->ops = &bdev_sops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) static struct file_system_type bd_type = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) .name = "bdev",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) .init_fs_context = bd_init_fs_context,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) .kill_sb = kill_anon_super,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) struct super_block *blockdev_superblock __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) EXPORT_SYMBOL_GPL(blockdev_superblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) void __init bdev_cache_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) static struct vfsmount *bd_mnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) SLAB_MEM_SPREAD|SLAB_ACCOUNT|SLAB_PANIC),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) init_once);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) err = register_filesystem(&bd_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) panic("Cannot register bdev pseudo-fs");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) bd_mnt = kern_mount(&bd_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) if (IS_ERR(bd_mnt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) panic("Cannot create bdev pseudo-fs");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) * Most likely _very_ bad one - but then it's hardly critical for small
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) * /dev and can be fixed when somebody will need really large one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) * Keep in mind that it will be fed through icache hash function too.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) static inline unsigned long hash(dev_t dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) return MAJOR(dev)+MINOR(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) static int bdev_test(struct inode *inode, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) return BDEV_I(inode)->bdev.bd_dev == *(dev_t *)data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) static int bdev_set(struct inode *inode, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) BDEV_I(inode)->bdev.bd_dev = *(dev_t *)data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) static struct block_device *bdget(dev_t dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) struct block_device *bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) inode = iget5_locked(blockdev_superblock, hash(dev),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) bdev_test, bdev_set, &dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) if (!inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) bdev = &BDEV_I(inode)->bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) if (inode->i_state & I_NEW) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) spin_lock_init(&bdev->bd_size_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) bdev->bd_contains = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) bdev->bd_super = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) bdev->bd_inode = inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) bdev->bd_part_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) inode->i_mode = S_IFBLK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) inode->i_rdev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) inode->i_bdev = bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) inode->i_data.a_ops = &def_blk_aops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) mapping_set_gfp_mask(&inode->i_data, GFP_USER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) unlock_new_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) return bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) * bdgrab -- Grab a reference to an already referenced block device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) * @bdev: Block device to grab a reference to.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) struct block_device *bdgrab(struct block_device *bdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) ihold(bdev->bd_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) return bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) EXPORT_SYMBOL(bdgrab);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) struct block_device *bdget_part(struct hd_struct *part)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) return bdget(part_devt(part));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) long nr_blockdev_pages(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) long ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) spin_lock(&blockdev_superblock->s_inode_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) list_for_each_entry(inode, &blockdev_superblock->s_inodes, i_sb_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) ret += inode->i_mapping->nrpages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) spin_unlock(&blockdev_superblock->s_inode_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) void bdput(struct block_device *bdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) iput(bdev->bd_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) EXPORT_SYMBOL(bdput);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) static struct block_device *bd_acquire(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) struct block_device *bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) spin_lock(&bdev_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) bdev = inode->i_bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) if (bdev && !inode_unhashed(bdev->bd_inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) bdgrab(bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) spin_unlock(&bdev_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) return bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) spin_unlock(&bdev_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) * i_bdev references block device inode that was already shut down
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) * (corresponding device got removed). Remove the reference and look
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) * up block device inode again just in case new device got
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) * reestablished under the same device number.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) if (bdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) bd_forget(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) bdev = bdget(inode->i_rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) if (bdev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) spin_lock(&bdev_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) if (!inode->i_bdev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) * We take an additional reference to bd_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) * and it's released in clear_inode() of inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) * So, we can access it via ->i_mapping always
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) * without igrab().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) bdgrab(bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) inode->i_bdev = bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) inode->i_mapping = bdev->bd_inode->i_mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) spin_unlock(&bdev_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) return bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) /* Call when you free inode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) void bd_forget(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) struct block_device *bdev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) spin_lock(&bdev_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) if (!sb_is_blkdev_sb(inode->i_sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) bdev = inode->i_bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) inode->i_bdev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) inode->i_mapping = &inode->i_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) spin_unlock(&bdev_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) if (bdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) bdput(bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) * bd_may_claim - test whether a block device can be claimed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) * @bdev: block device of interest
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) * @whole: whole block device containing @bdev, may equal @bdev
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) * @holder: holder trying to claim @bdev
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) * Test whether @bdev can be claimed by @holder.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) * CONTEXT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) * spin_lock(&bdev_lock).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) * RETURNS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) * %true if @bdev can be claimed, %false otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) static bool bd_may_claim(struct block_device *bdev, struct block_device *whole,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) void *holder)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) if (bdev->bd_holder == holder)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) return true; /* already a holder */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) else if (bdev->bd_holder != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) return false; /* held by someone else */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) else if (whole == bdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) return true; /* is a whole device which isn't held */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) else if (whole->bd_holder == bd_may_claim)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) return true; /* is a partition of a device that is being partitioned */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) else if (whole->bd_holder != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) return false; /* is a partition of a held device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) return true; /* is a partition of an un-held device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) * bd_prepare_to_claim - claim a block device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) * @bdev: block device of interest
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) * @whole: the whole device containing @bdev, may equal @bdev
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) * @holder: holder trying to claim @bdev
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) * Claim @bdev. This function fails if @bdev is already claimed by another
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) * holder and waits if another claiming is in progress. return, the caller
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) * has ownership of bd_claiming and bd_holder[s].
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) * RETURNS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) * 0 if @bdev can be claimed, -EBUSY otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) int bd_prepare_to_claim(struct block_device *bdev, struct block_device *whole,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) void *holder)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) spin_lock(&bdev_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) /* if someone else claimed, fail */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) if (!bd_may_claim(bdev, whole, holder)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) spin_unlock(&bdev_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) /* if claiming is already in progress, wait for it to finish */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) if (whole->bd_claiming) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) wait_queue_head_t *wq = bit_waitqueue(&whole->bd_claiming, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) DEFINE_WAIT(wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) spin_unlock(&bdev_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) schedule();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) finish_wait(wq, &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) /* yay, all mine */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) whole->bd_claiming = holder;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) spin_unlock(&bdev_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) EXPORT_SYMBOL_GPL(bd_prepare_to_claim); /* only for the loop driver */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) static struct gendisk *bdev_get_gendisk(struct block_device *bdev, int *partno)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) struct gendisk *disk = get_gendisk(bdev->bd_dev, partno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) if (!disk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) * Now that we hold gendisk reference we make sure bdev we looked up is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) * not stale. If it is, it means device got removed and created before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) * we looked up gendisk and we fail open in such case. Associating
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) * unhashed bdev with newly created gendisk could lead to two bdevs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) * (and thus two independent caches) being associated with one device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) * which is bad.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) if (inode_unhashed(bdev->bd_inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) put_disk_and_module(disk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) return disk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) static void bd_clear_claiming(struct block_device *whole, void *holder)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) lockdep_assert_held(&bdev_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) /* tell others that we're done */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) BUG_ON(whole->bd_claiming != holder);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) whole->bd_claiming = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) wake_up_bit(&whole->bd_claiming, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) * bd_finish_claiming - finish claiming of a block device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) * @bdev: block device of interest
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) * @whole: whole block device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) * @holder: holder that has claimed @bdev
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) * Finish exclusive open of a block device. Mark the device as exlusively
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) * open by the holder and wake up all waiters for exclusive open to finish.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) static void bd_finish_claiming(struct block_device *bdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) struct block_device *whole, void *holder)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) spin_lock(&bdev_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) BUG_ON(!bd_may_claim(bdev, whole, holder));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) * Note that for a whole device bd_holders will be incremented twice,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) * and bd_holder will be set to bd_may_claim before being set to holder
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) whole->bd_holders++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) whole->bd_holder = bd_may_claim;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) bdev->bd_holders++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) bdev->bd_holder = holder;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) bd_clear_claiming(whole, holder);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) spin_unlock(&bdev_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) * bd_abort_claiming - abort claiming of a block device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) * @bdev: block device of interest
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) * @whole: whole block device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) * @holder: holder that has claimed @bdev
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) * Abort claiming of a block device when the exclusive open failed. This can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) * also used when exclusive open is not actually desired and we just needed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) * to block other exclusive openers for a while.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) void bd_abort_claiming(struct block_device *bdev, struct block_device *whole,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) void *holder)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) spin_lock(&bdev_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) bd_clear_claiming(whole, holder);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) spin_unlock(&bdev_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) EXPORT_SYMBOL(bd_abort_claiming);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) #ifdef CONFIG_SYSFS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) struct bd_holder_disk {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) struct gendisk *disk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) int refcnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) static struct bd_holder_disk *bd_find_holder_disk(struct block_device *bdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) struct gendisk *disk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) struct bd_holder_disk *holder;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) list_for_each_entry(holder, &bdev->bd_holder_disks, list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) if (holder->disk == disk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) return holder;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) static int add_symlink(struct kobject *from, struct kobject *to)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) return sysfs_create_link(from, to, kobject_name(to));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) static void del_symlink(struct kobject *from, struct kobject *to)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) sysfs_remove_link(from, kobject_name(to));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) * bd_link_disk_holder - create symlinks between holding disk and slave bdev
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) * @bdev: the claimed slave bdev
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) * @disk: the holding disk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) * This functions creates the following sysfs symlinks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) * - from "slaves" directory of the holder @disk to the claimed @bdev
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) * - from "holders" directory of the @bdev to the holder @disk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) * For example, if /dev/dm-0 maps to /dev/sda and disk for dm-0 is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) * passed to bd_link_disk_holder(), then:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) * /sys/block/dm-0/slaves/sda --> /sys/block/sda
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) * The caller must have claimed @bdev before calling this function and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) * ensure that both @bdev and @disk are valid during the creation and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) * lifetime of these symlinks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) * CONTEXT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) * Might sleep.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) * RETURNS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) * 0 on success, -errno on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) struct bd_holder_disk *holder;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) mutex_lock(&bdev->bd_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) WARN_ON_ONCE(!bdev->bd_holder);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) /* FIXME: remove the following once add_disk() handles errors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) if (WARN_ON(!disk->slave_dir || !bdev->bd_part->holder_dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) holder = bd_find_holder_disk(bdev, disk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) if (holder) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) holder->refcnt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) holder = kzalloc(sizeof(*holder), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) if (!holder) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) INIT_LIST_HEAD(&holder->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) holder->disk = disk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) holder->refcnt = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) ret = add_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) ret = add_symlink(bdev->bd_part->holder_dir, &disk_to_dev(disk)->kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) goto out_del;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) * bdev could be deleted beneath us which would implicitly destroy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) * the holder directory. Hold on to it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) kobject_get(bdev->bd_part->holder_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) list_add(&holder->list, &bdev->bd_holder_disks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) out_del:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) del_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) kfree(holder);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) mutex_unlock(&bdev->bd_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) EXPORT_SYMBOL_GPL(bd_link_disk_holder);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) * bd_unlink_disk_holder - destroy symlinks created by bd_link_disk_holder()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) * @bdev: the calimed slave bdev
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) * @disk: the holding disk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) * CONTEXT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) * Might sleep.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) void bd_unlink_disk_holder(struct block_device *bdev, struct gendisk *disk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) struct bd_holder_disk *holder;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) mutex_lock(&bdev->bd_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) holder = bd_find_holder_disk(bdev, disk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) if (!WARN_ON_ONCE(holder == NULL) && !--holder->refcnt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) del_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) del_symlink(bdev->bd_part->holder_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) &disk_to_dev(disk)->kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) kobject_put(bdev->bd_part->holder_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) list_del_init(&holder->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) kfree(holder);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) mutex_unlock(&bdev->bd_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) EXPORT_SYMBOL_GPL(bd_unlink_disk_holder);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) * check_disk_size_change - checks for disk size change and adjusts bdev size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) * @disk: struct gendisk to check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) * @bdev: struct bdev to adjust.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) * @verbose: if %true log a message about a size change if there is any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) * This routine checks to see if the bdev size does not match the disk size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) * and adjusts it if it differs. When shrinking the bdev size, its all caches
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) * are freed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) static void check_disk_size_change(struct gendisk *disk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) struct block_device *bdev, bool verbose)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) loff_t disk_size, bdev_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) spin_lock(&bdev->bd_size_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) disk_size = (loff_t)get_capacity(disk) << 9;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) bdev_size = i_size_read(bdev->bd_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) if (disk_size != bdev_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) if (verbose) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) printk(KERN_INFO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) "%s: detected capacity change from %lld to %lld\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) disk->disk_name, bdev_size, disk_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) i_size_write(bdev->bd_inode, disk_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) spin_unlock(&bdev->bd_size_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) if (bdev_size > disk_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) if (__invalidate_device(bdev, false))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) pr_warn("VFS: busy inodes on resized disk %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) disk->disk_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) * revalidate_disk_size - checks for disk size change and adjusts bdev size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) * @disk: struct gendisk to check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) * @verbose: if %true log a message about a size change if there is any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) * This routine checks to see if the bdev size does not match the disk size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) * and adjusts it if it differs. When shrinking the bdev size, its all caches
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) * are freed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) void revalidate_disk_size(struct gendisk *disk, bool verbose)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) struct block_device *bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) * Hidden disks don't have associated bdev so there's no point in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) * revalidating them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) if (disk->flags & GENHD_FL_HIDDEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) bdev = bdget_disk(disk, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) if (bdev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) check_disk_size_change(disk, bdev, verbose);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) bdput(bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) EXPORT_SYMBOL(revalidate_disk_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) void bd_set_nr_sectors(struct block_device *bdev, sector_t sectors)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) spin_lock(&bdev->bd_size_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) i_size_write(bdev->bd_inode, (loff_t)sectors << SECTOR_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) spin_unlock(&bdev->bd_size_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) EXPORT_SYMBOL(bd_set_nr_sectors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) static void __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) int bdev_disk_changed(struct block_device *bdev, bool invalidate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) struct gendisk *disk = bdev->bd_disk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) lockdep_assert_held(&bdev->bd_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) if (!(disk->flags & GENHD_FL_UP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) rescan:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) ret = blk_drop_partitions(bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) clear_bit(GD_NEED_PART_SCAN, &disk->state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) * Historically we only set the capacity to zero for devices that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) * support partitions (independ of actually having partitions created).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) * Doing that is rather inconsistent, but changing it broke legacy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) * udisks polling for legacy ide-cdrom devices. Use the crude check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) * below to get the sane behavior for most device while not breaking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) * userspace for this particular setup.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) if (invalidate) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) if (disk_part_scan_enabled(disk) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) !(disk->flags & GENHD_FL_REMOVABLE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) set_capacity(disk, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) if (disk->fops->revalidate_disk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) disk->fops->revalidate_disk(disk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) check_disk_size_change(disk, bdev, !invalidate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) if (get_capacity(disk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) ret = blk_add_partitions(disk, bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) if (ret == -EAGAIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) goto rescan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) } else if (invalidate) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) * Tell userspace that the media / partition table may have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) * changed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) * Only exported for for loop and dasd for historic reasons. Don't use in new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) * code!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) EXPORT_SYMBOL_GPL(bdev_disk_changed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) * bd_mutex locking:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) * mutex_lock(part->bd_mutex)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) * mutex_lock_nested(whole->bd_mutex, 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) static int __blkdev_get(struct block_device *bdev, fmode_t mode, void *holder,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) int for_part)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) struct block_device *whole = NULL, *claiming = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) struct gendisk *disk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) int partno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) bool first_open = false, unblock_events = true, need_restart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) restart:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) need_restart = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) ret = -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) disk = bdev_get_gendisk(bdev, &partno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) if (!disk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) if (partno) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) whole = bdget_disk(disk, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) if (!whole) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) goto out_put_disk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) if (!for_part && (mode & FMODE_EXCL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) WARN_ON_ONCE(!holder);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) if (whole)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) claiming = whole;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) claiming = bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) ret = bd_prepare_to_claim(bdev, claiming, holder);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) goto out_put_whole;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) disk_block_events(disk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) mutex_lock_nested(&bdev->bd_mutex, for_part);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) if (!bdev->bd_openers) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) first_open = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) bdev->bd_disk = disk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) bdev->bd_contains = bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) bdev->bd_partno = partno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) if (!partno) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) ret = -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) bdev->bd_part = disk_get_part(disk, partno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) if (!bdev->bd_part)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) goto out_clear;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) if (disk->fops->open) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) ret = disk->fops->open(bdev, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) * If we lost a race with 'disk' being deleted,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) * try again. See md.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) if (ret == -ERESTARTSYS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) need_restart = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) bd_set_nr_sectors(bdev, get_capacity(disk));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) set_init_blocksize(bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) * If the device is invalidated, rescan partition
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) * if open succeeded or failed with -ENOMEDIUM.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) * The latter is necessary to prevent ghost
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) * partitions on a removed medium.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) if (test_bit(GD_NEED_PART_SCAN, &disk->state) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) (!ret || ret == -ENOMEDIUM))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) bdev_disk_changed(bdev, ret == -ENOMEDIUM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) goto out_clear;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) BUG_ON(for_part);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) ret = __blkdev_get(whole, mode, NULL, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) goto out_clear;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) bdev->bd_contains = bdgrab(whole);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) bdev->bd_part = disk_get_part(disk, partno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) if (!(disk->flags & GENHD_FL_UP) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) !bdev->bd_part || !bdev->bd_part->nr_sects) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) ret = -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) goto out_clear;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) bd_set_nr_sectors(bdev, bdev->bd_part->nr_sects);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) set_init_blocksize(bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) if (bdev->bd_bdi == &noop_backing_dev_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) bdev->bd_bdi = bdi_get(disk->queue->backing_dev_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) if (bdev->bd_contains == bdev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) if (bdev->bd_disk->fops->open)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) ret = bdev->bd_disk->fops->open(bdev, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) /* the same as first opener case, read comment there */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) if (test_bit(GD_NEED_PART_SCAN, &disk->state) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) (!ret || ret == -ENOMEDIUM))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) bdev_disk_changed(bdev, ret == -ENOMEDIUM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) goto out_unlock_bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) bdev->bd_openers++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) if (for_part)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) bdev->bd_part_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) if (claiming)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) bd_finish_claiming(bdev, claiming, holder);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) * Block event polling for write claims if requested. Any write holder
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) * makes the write_holder state stick until all are released. This is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) * good enough and tracking individual writeable reference is too
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) * fragile given the way @mode is used in blkdev_get/put().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) if (claiming && (mode & FMODE_WRITE) && !bdev->bd_write_holder &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) (disk->flags & GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) bdev->bd_write_holder = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) unblock_events = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) mutex_unlock(&bdev->bd_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) if (unblock_events)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) disk_unblock_events(disk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) /* only one opener holds refs to the module and disk */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) if (!first_open)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) put_disk_and_module(disk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) if (whole)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) bdput(whole);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) out_clear:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) disk_put_part(bdev->bd_part);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) bdev->bd_disk = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) bdev->bd_part = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) if (bdev != bdev->bd_contains)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) __blkdev_put(bdev->bd_contains, mode, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) bdev->bd_contains = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) out_unlock_bdev:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) if (claiming)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) bd_abort_claiming(bdev, claiming, holder);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) mutex_unlock(&bdev->bd_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) disk_unblock_events(disk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) out_put_whole:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) if (whole)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) bdput(whole);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) out_put_disk:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) put_disk_and_module(disk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) if (need_restart)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) goto restart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) * blkdev_get - open a block device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) * @bdev: block_device to open
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) * @mode: FMODE_* mask
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) * @holder: exclusive holder identifier
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) * Open @bdev with @mode. If @mode includes %FMODE_EXCL, @bdev is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) * open with exclusive access. Specifying %FMODE_EXCL with %NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) * @holder is invalid. Exclusive opens may nest for the same @holder.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) * On success, the reference count of @bdev is unchanged. On failure,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) * @bdev is put.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) * CONTEXT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) * Might sleep.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) * RETURNS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) * 0 on success, -errno on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) static int blkdev_get(struct block_device *bdev, fmode_t mode, void *holder)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) int ret, perm = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) if (mode & FMODE_READ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) perm |= MAY_READ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) if (mode & FMODE_WRITE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) perm |= MAY_WRITE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) ret = devcgroup_inode_permission(bdev->bd_inode, perm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) goto bdput;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) ret =__blkdev_get(bdev, mode, holder, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) goto bdput;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) bdput:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) bdput(bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) * blkdev_get_by_path - open a block device by name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) * @path: path to the block device to open
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) * @mode: FMODE_* mask
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) * @holder: exclusive holder identifier
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) * Open the blockdevice described by the device file at @path. @mode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) * and @holder are identical to blkdev_get().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) * On success, the returned block_device has reference count of one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) * CONTEXT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) * Might sleep.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) * RETURNS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) * Pointer to block_device on success, ERR_PTR(-errno) on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) struct block_device *blkdev_get_by_path(const char *path, fmode_t mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) void *holder)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) struct block_device *bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) bdev = lookup_bdev(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) if (IS_ERR(bdev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) return bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) err = blkdev_get(bdev, mode, holder);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) if ((mode & FMODE_WRITE) && bdev_read_only(bdev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) blkdev_put(bdev, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) return ERR_PTR(-EACCES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) return bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) EXPORT_SYMBOL(blkdev_get_by_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) * blkdev_get_by_dev - open a block device by device number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) * @dev: device number of block device to open
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) * @mode: FMODE_* mask
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) * @holder: exclusive holder identifier
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) * Open the blockdevice described by device number @dev. @mode and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) * @holder are identical to blkdev_get().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) * Use it ONLY if you really do not have anything better - i.e. when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) * you are behind a truly sucky interface and all you are given is a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) * device number. _Never_ to be used for internal purposes. If you
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) * ever need it - reconsider your API.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) * On success, the returned block_device has reference count of one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) * CONTEXT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) * Might sleep.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) * RETURNS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) * Pointer to block_device on success, ERR_PTR(-errno) on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) struct block_device *blkdev_get_by_dev(dev_t dev, fmode_t mode, void *holder)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) struct block_device *bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) bdev = bdget(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) if (!bdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) err = blkdev_get(bdev, mode, holder);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) return bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) EXPORT_SYMBOL(blkdev_get_by_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) static int blkdev_open(struct inode * inode, struct file * filp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) struct block_device *bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) * Preserve backwards compatibility and allow large file access
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) * even if userspace doesn't ask for it explicitly. Some mkfs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) * binary needs it. We might want to drop this workaround
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) * during an unstable branch.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) filp->f_flags |= O_LARGEFILE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) filp->f_mode |= FMODE_NOWAIT | FMODE_BUF_RASYNC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) if (filp->f_flags & O_NDELAY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) filp->f_mode |= FMODE_NDELAY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) if (filp->f_flags & O_EXCL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) filp->f_mode |= FMODE_EXCL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) if ((filp->f_flags & O_ACCMODE) == 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) filp->f_mode |= FMODE_WRITE_IOCTL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) bdev = bd_acquire(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) if (bdev == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) filp->f_mapping = bdev->bd_inode->i_mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) filp->f_wb_err = filemap_sample_wb_err(filp->f_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) return blkdev_get(bdev, filp->f_mode, filp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) static void __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) struct gendisk *disk = bdev->bd_disk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) struct block_device *victim = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) * Sync early if it looks like we're the last one. If someone else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) * opens the block device between now and the decrement of bd_openers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) * then we did a sync that we didn't need to, but that's not the end
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) * of the world and we want to avoid long (could be several minute)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) * syncs while holding the mutex.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) if (bdev->bd_openers == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) sync_blockdev(bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) mutex_lock_nested(&bdev->bd_mutex, for_part);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) if (for_part)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) bdev->bd_part_count--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) if (!--bdev->bd_openers) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) WARN_ON_ONCE(bdev->bd_holders);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) sync_blockdev(bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) kill_bdev(bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) bdev_write_inode(bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) if (bdev->bd_contains == bdev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) if (disk->fops->release)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) disk->fops->release(disk, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) if (!bdev->bd_openers) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) disk_put_part(bdev->bd_part);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) bdev->bd_part = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) bdev->bd_disk = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) if (bdev != bdev->bd_contains)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) victim = bdev->bd_contains;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) bdev->bd_contains = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) put_disk_and_module(disk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) mutex_unlock(&bdev->bd_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) bdput(bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) if (victim)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) __blkdev_put(victim, mode, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) void blkdev_put(struct block_device *bdev, fmode_t mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) mutex_lock(&bdev->bd_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) if (mode & FMODE_EXCL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) bool bdev_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) * Release a claim on the device. The holder fields
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) * are protected with bdev_lock. bd_mutex is to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) * synchronize disk_holder unlinking.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) spin_lock(&bdev_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) WARN_ON_ONCE(--bdev->bd_holders < 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) WARN_ON_ONCE(--bdev->bd_contains->bd_holders < 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) /* bd_contains might point to self, check in a separate step */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) if ((bdev_free = !bdev->bd_holders))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) bdev->bd_holder = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) if (!bdev->bd_contains->bd_holders)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) bdev->bd_contains->bd_holder = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) spin_unlock(&bdev_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) * If this was the last claim, remove holder link and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) * unblock evpoll if it was a write holder.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) if (bdev_free && bdev->bd_write_holder) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) disk_unblock_events(bdev->bd_disk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) bdev->bd_write_holder = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) * Trigger event checking and tell drivers to flush MEDIA_CHANGE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) * event. This is to ensure detection of media removal commanded
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) * from userland - e.g. eject(1).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) disk_flush_events(bdev->bd_disk, DISK_EVENT_MEDIA_CHANGE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) mutex_unlock(&bdev->bd_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) __blkdev_put(bdev, mode, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) EXPORT_SYMBOL(blkdev_put);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) static int blkdev_close(struct inode * inode, struct file * filp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) struct block_device *bdev = I_BDEV(bdev_file_inode(filp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) blkdev_put(bdev, filp->f_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) struct block_device *bdev = I_BDEV(bdev_file_inode(file));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) fmode_t mode = file->f_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) * to updated it before every ioctl.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) if (file->f_flags & O_NDELAY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) mode |= FMODE_NDELAY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) mode &= ~FMODE_NDELAY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) return blkdev_ioctl(bdev, mode, cmd, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) * Write data to the block device. Only intended for the block device itself
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) * and the raw driver which basically is a fake block device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) * Does not take i_mutex for the write and thus is not for general purpose
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) * use.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) ssize_t blkdev_write_iter(struct kiocb *iocb, struct iov_iter *from)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) struct file *file = iocb->ki_filp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) struct inode *bd_inode = bdev_file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) loff_t size = i_size_read(bd_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) struct blk_plug plug;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) size_t shorted = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) if (bdev_read_only(I_BDEV(bd_inode)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) if (IS_SWAPFILE(bd_inode) && !is_hibernate_resume_dev(bd_inode->i_rdev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) return -ETXTBSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) if (!iov_iter_count(from))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) if (iocb->ki_pos >= size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) if ((iocb->ki_flags & (IOCB_NOWAIT | IOCB_DIRECT)) == IOCB_NOWAIT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) size -= iocb->ki_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) if (iov_iter_count(from) > size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) shorted = iov_iter_count(from) - size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) iov_iter_truncate(from, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) blk_start_plug(&plug);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) ret = __generic_file_write_iter(iocb, from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) if (ret > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) ret = generic_write_sync(iocb, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) iov_iter_reexpand(from, iov_iter_count(from) + shorted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) blk_finish_plug(&plug);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) EXPORT_SYMBOL_GPL(blkdev_write_iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) ssize_t blkdev_read_iter(struct kiocb *iocb, struct iov_iter *to)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) struct file *file = iocb->ki_filp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) struct inode *bd_inode = bdev_file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) loff_t size = i_size_read(bd_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) loff_t pos = iocb->ki_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) size_t shorted = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) if (pos >= size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) size -= pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) if (iov_iter_count(to) > size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) shorted = iov_iter_count(to) - size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) iov_iter_truncate(to, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) ret = generic_file_read_iter(iocb, to);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) iov_iter_reexpand(to, iov_iter_count(to) + shorted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) EXPORT_SYMBOL_GPL(blkdev_read_iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) * Try to release a page associated with block device when the system
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) * is under memory pressure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) static int blkdev_releasepage(struct page *page, gfp_t wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) struct super_block *super = BDEV_I(page->mapping->host)->bdev.bd_super;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) if (super && super->s_op->bdev_try_to_free_page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) return super->s_op->bdev_try_to_free_page(super, page, wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) return try_to_free_buffers(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) static int blkdev_writepages(struct address_space *mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) struct writeback_control *wbc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) return generic_writepages(mapping, wbc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) static const struct address_space_operations def_blk_aops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) .readpage = blkdev_readpage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) .readahead = blkdev_readahead,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) .writepage = blkdev_writepage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) .write_begin = blkdev_write_begin,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) .write_end = blkdev_write_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) .writepages = blkdev_writepages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) .releasepage = blkdev_releasepage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) .direct_IO = blkdev_direct_IO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) .migratepage = buffer_migrate_page_norefs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) .is_dirty_writeback = buffer_check_dirty_writeback,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) #define BLKDEV_FALLOC_FL_SUPPORTED \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) (FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) FALLOC_FL_ZERO_RANGE | FALLOC_FL_NO_HIDE_STALE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) static long blkdev_fallocate(struct file *file, int mode, loff_t start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) loff_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) struct block_device *bdev = I_BDEV(bdev_file_inode(file));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) loff_t end = start + len - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) loff_t isize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) /* Fail if we don't recognize the flags. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) if (mode & ~BLKDEV_FALLOC_FL_SUPPORTED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) /* Don't go off the end of the device. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) isize = i_size_read(bdev->bd_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) if (start >= isize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) if (end >= isize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) if (mode & FALLOC_FL_KEEP_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) len = isize - start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) end = start + len - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) * Don't allow IO that isn't aligned to logical block size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) if ((start | len) & (bdev_logical_block_size(bdev) - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) /* Invalidate the page cache, including dirty pages. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) error = truncate_bdev_range(bdev, file->f_mode, start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) switch (mode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) case FALLOC_FL_ZERO_RANGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) case FALLOC_FL_ZERO_RANGE | FALLOC_FL_KEEP_SIZE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) error = blkdev_issue_zeroout(bdev, start >> 9, len >> 9,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) GFP_KERNEL, BLKDEV_ZERO_NOUNMAP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) case FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) error = blkdev_issue_zeroout(bdev, start >> 9, len >> 9,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) GFP_KERNEL, BLKDEV_ZERO_NOFALLBACK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) case FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE | FALLOC_FL_NO_HIDE_STALE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) error = blkdev_issue_discard(bdev, start >> 9, len >> 9,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) GFP_KERNEL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) * Invalidate again; if someone wandered in and dirtied a page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) * the caller will be given -EBUSY. The third argument is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) * inclusive, so the rounding here is safe.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) return invalidate_inode_pages2_range(bdev->bd_inode->i_mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) start >> PAGE_SHIFT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) end >> PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062) const struct file_operations def_blk_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) .open = blkdev_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) .release = blkdev_close,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) .llseek = block_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) .read_iter = blkdev_read_iter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) .write_iter = blkdev_write_iter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) .iopoll = blkdev_iopoll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) .mmap = generic_file_mmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) .fsync = blkdev_fsync,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) .unlocked_ioctl = block_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) .compat_ioctl = compat_blkdev_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) .splice_read = generic_file_splice_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) .splice_write = iter_file_splice_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) .fallocate = blkdev_fallocate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) * lookup_bdev - lookup a struct block_device by name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) * @pathname: special file representing the block device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) * Get a reference to the blockdevice at @pathname in the current
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) * namespace if possible and return it. Return ERR_PTR(error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086) * otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088) struct block_device *lookup_bdev(const char *pathname)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) struct block_device *bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091) struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) struct path path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) if (!pathname || !*pathname)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098) error = kern_path(pathname, LOOKUP_FOLLOW, &path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) return ERR_PTR(error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) inode = d_backing_inode(path.dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) error = -ENOTBLK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) if (!S_ISBLK(inode->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) error = -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) if (!may_open_dev(&path))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) error = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) bdev = bd_acquire(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) if (!bdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) path_put(&path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) return bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) bdev = ERR_PTR(error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) EXPORT_SYMBOL(lookup_bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) int __invalidate_device(struct block_device *bdev, bool kill_dirty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) struct super_block *sb = get_super(bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) int res = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) if (sb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) * no need to lock the super, get_super holds the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130) * read mutex so the filesystem cannot go away
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131) * under us (->put_super runs with the write lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132) * hold).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134) shrink_dcache_sb(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135) res = invalidate_inodes(sb, kill_dirty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136) drop_super(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138) invalidate_bdev(bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141) EXPORT_SYMBOL(__invalidate_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2143) void iterate_bdevs(void (*func)(struct block_device *, void *), void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2144) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2145) struct inode *inode, *old_inode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2147) spin_lock(&blockdev_superblock->s_inode_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2148) list_for_each_entry(inode, &blockdev_superblock->s_inodes, i_sb_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2149) struct address_space *mapping = inode->i_mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2150) struct block_device *bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2152) spin_lock(&inode->i_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2153) if (inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2154) mapping->nrpages == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2155) spin_unlock(&inode->i_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2156) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2158) __iget(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2159) spin_unlock(&inode->i_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2160) spin_unlock(&blockdev_superblock->s_inode_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2161) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2162) * We hold a reference to 'inode' so it couldn't have been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2163) * removed from s_inodes list while we dropped the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2164) * s_inode_list_lock We cannot iput the inode now as we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2165) * be holding the last reference and we cannot iput it under
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2166) * s_inode_list_lock. So we keep the reference and iput it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2167) * later.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2168) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2169) iput(old_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2170) old_inode = inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2171) bdev = I_BDEV(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2173) mutex_lock(&bdev->bd_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2174) if (bdev->bd_openers)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2175) func(bdev, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2176) mutex_unlock(&bdev->bd_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2178) spin_lock(&blockdev_superblock->s_inode_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2180) spin_unlock(&blockdev_superblock->s_inode_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2181) iput(old_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2182) }