block: remove (__)blkdev_reread_part as an exported API

In general drivers should never mess with partition tables directly.
Unfortunately s390 and loop do for somewhat historic reasons, but they
can use bdev_disk_changed directly instead when we export it as they
satisfy the sanity checks we have in __blkdev_reread_part.

Signed-off-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Stefan Haberland <sth@linux.ibm.com>	[dasd]
Reviewed-by: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
This commit is contained in:
Christoph Hellwig 2019-11-14 15:34:36 +01:00 committed by Jens Axboe
parent 142fe8f4bb
commit f0b870df80
5 changed files with 23 additions and 38 deletions

View File

@ -155,46 +155,21 @@ static int blkpg_ioctl(struct block_device *bdev, struct blkpg_ioctl_arg __user
} }
} }
/* static int blkdev_reread_part(struct block_device *bdev)
* This is an exported API for the block driver, and will not
* acquire bd_mutex. This API should be used in case that
* caller has held bd_mutex already.
*/
int __blkdev_reread_part(struct block_device *bdev)
{ {
int ret;
if (!disk_part_scan_enabled(bdev->bd_disk) || bdev != bdev->bd_contains) if (!disk_part_scan_enabled(bdev->bd_disk) || bdev != bdev->bd_contains)
return -EINVAL; return -EINVAL;
if (!capable(CAP_SYS_ADMIN)) if (!capable(CAP_SYS_ADMIN))
return -EACCES; return -EACCES;
lockdep_assert_held(&bdev->bd_mutex);
return bdev_disk_changed(bdev, false);
}
EXPORT_SYMBOL(__blkdev_reread_part);
/*
* This is an exported API for the block driver, and will
* try to acquire bd_mutex. If bd_mutex has been held already
* in current context, please call __blkdev_reread_part().
*
* Make sure the held locks in current context aren't required
* in open()/close() handler and I/O path for avoiding ABBA deadlock:
* - bd_mutex is held before calling block driver's open/close
* handler
* - reading partition table may submit I/O to the block device
*/
int blkdev_reread_part(struct block_device *bdev)
{
int res;
mutex_lock(&bdev->bd_mutex); mutex_lock(&bdev->bd_mutex);
res = __blkdev_reread_part(bdev); ret = bdev_disk_changed(bdev, false);
mutex_unlock(&bdev->bd_mutex); mutex_unlock(&bdev->bd_mutex);
return res; return ret;
} }
EXPORT_SYMBOL(blkdev_reread_part);
static int blk_ioctl_discard(struct block_device *bdev, fmode_t mode, static int blk_ioctl_discard(struct block_device *bdev, fmode_t mode,
unsigned long arg, unsigned long flags) unsigned long arg, unsigned long flags)

View File

@ -640,7 +640,9 @@ static void loop_reread_partitions(struct loop_device *lo,
{ {
int rc; int rc;
rc = blkdev_reread_part(bdev); mutex_lock(&bdev->bd_mutex);
rc = bdev_disk_changed(bdev, false);
mutex_unlock(&bdev->bd_mutex);
if (rc) if (rc)
pr_warn("%s: partition scan of loop%d (%s) failed (rc=%d)\n", pr_warn("%s: partition scan of loop%d (%s) failed (rc=%d)\n",
__func__, lo->lo_number, lo->lo_file_name, rc); __func__, lo->lo_number, lo->lo_file_name, rc);
@ -1164,10 +1166,11 @@ out_unlock:
* must be at least one and it can only become zero when the * must be at least one and it can only become zero when the
* current holder is released. * current holder is released.
*/ */
if (release) if (!release)
err = __blkdev_reread_part(bdev); mutex_lock(&bdev->bd_mutex);
else err = bdev_disk_changed(bdev, false);
err = blkdev_reread_part(bdev); if (!release)
mutex_unlock(&bdev->bd_mutex);
if (err) if (err)
pr_warn("%s: partition scan of loop%d failed (rc=%d)\n", pr_warn("%s: partition scan of loop%d failed (rc=%d)\n",
__func__, lo_number, err); __func__, lo_number, err);

View File

@ -116,7 +116,9 @@ int dasd_scan_partitions(struct dasd_block *block)
return -ENODEV; return -ENODEV;
} }
rc = blkdev_reread_part(bdev); mutex_lock(&bdev->bd_mutex);
rc = bdev_disk_changed(bdev, false);
mutex_unlock(&bdev->bd_mutex);
if (rc) if (rc)
DBF_DEV_EVENT(DBF_ERR, block->base, DBF_DEV_EVENT(DBF_ERR, block->base,
"scan partitions error, rc %d", rc); "scan partitions error, rc %d", rc);

View File

@ -1513,6 +1513,8 @@ int bdev_disk_changed(struct block_device *bdev, bool invalidate)
struct gendisk *disk = bdev->bd_disk; struct gendisk *disk = bdev->bd_disk;
int ret; int ret;
lockdep_assert_held(&bdev->bd_mutex);
rescan: rescan:
ret = blk_drop_partitions(disk, bdev); ret = blk_drop_partitions(disk, bdev);
if (ret) if (ret)
@ -1540,6 +1542,11 @@ rescan:
return ret; return ret;
} }
/*
* Only exported for for loop and dasd for historic reasons. Don't use in new
* code!
*/
EXPORT_SYMBOL_GPL(bdev_disk_changed);
/* /*
* bd_mutex locking: * bd_mutex locking:

View File

@ -2632,8 +2632,6 @@ extern void bd_finish_claiming(struct block_device *bdev,
extern void bd_abort_claiming(struct block_device *bdev, extern void bd_abort_claiming(struct block_device *bdev,
struct block_device *whole, void *holder); struct block_device *whole, void *holder);
extern void blkdev_put(struct block_device *bdev, fmode_t mode); extern void blkdev_put(struct block_device *bdev, fmode_t mode);
extern int __blkdev_reread_part(struct block_device *bdev);
extern int blkdev_reread_part(struct block_device *bdev);
#ifdef CONFIG_SYSFS #ifdef CONFIG_SYSFS
extern int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk); extern int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk);