From: Yu Kuai <yukuai3@xxxxxxxxxx> Currently scsi driver is reading without opening disk as file( scsi_bios_ptable()), factor out a helper to read into block device page cache to prevent access bd_inode directly from scsi. Signed-off-by: Yu Kuai <yukuai3@xxxxxxxxxx> Reviewed-by: Christoph Hellwig <hch@xxxxxx> Reviewed-by: Jan Kara <jack@xxxxxxx> --- block/bdev.c | 19 +++++++++++++++++++ include/linux/blkdev.h | 1 + 2 files changed, 20 insertions(+) diff --git a/block/bdev.c b/block/bdev.c index c0b30392563a..4335df6d1266 100644 --- a/block/bdev.c +++ b/block/bdev.c @@ -1266,6 +1266,25 @@ unsigned int block_size(struct block_device *bdev) } EXPORT_SYMBOL_GPL(block_size); +/** + * bdev_read_folio - Read into block device page cache. + * @bdev: the block device which holds the cache to read. + * @pos: the offset that allocated folio will contain. + * + * Read one page into the block device page cache. If it succeeds, the folio + * returned will contain @pos; + * + * This is only used for scsi_bios_ptable(), the bdev is not opened as files. + * + * Return: Uptodate folio on success, ERR_PTR() on failure. + */ +struct folio *bdev_read_folio(struct block_device *bdev, loff_t pos) +{ + return mapping_read_folio_gfp(bdev_mapping(bdev), + pos >> PAGE_SHIFT, GFP_KERNEL); +} +EXPORT_SYMBOL_GPL(bdev_read_folio); + static int __init setup_bdev_allow_write_mounted(char *str) { if (kstrtobool(str, &bdev_allow_write_mounted)) diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h index 08d4e6a0940c..bc840e0fb6e5 100644 --- a/include/linux/blkdev.h +++ b/include/linux/blkdev.h @@ -1519,6 +1519,7 @@ struct file *bdev_file_open_by_path(const char *path, blk_mode_t mode, int bd_prepare_to_claim(struct block_device *bdev, void *holder, const struct blk_holder_ops *hops); void bd_abort_claiming(struct block_device *bdev, void *holder); +struct folio *bdev_read_folio(struct block_device *bdev, loff_t pos); /* just for blk-cgroup, don't use elsewhere */ struct block_device *blkdev_get_no_open(dev_t dev); -- 2.39.2