Skip to content

Commit

Permalink
block: refactor blk_drop_partitions
Browse files Browse the repository at this point in the history
Move the busy check and disk-wide sync into the only caller, so that
the remainder can be shared with del_gendisk.  Also pass the gendisk
instead of the bdev as that is all that is needed.

Signed-off-by: Christoph Hellwig <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Jens Axboe <[email protected]>
  • Loading branch information
Christoph Hellwig authored and axboe committed Apr 8, 2021
1 parent 473338b commit d3c4a43
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 26 deletions.
1 change: 0 additions & 1 deletion block/blk.h
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,6 @@ char *disk_name(struct gendisk *hd, int partno, char *buf);
#define ADDPART_FLAG_NONE 0
#define ADDPART_FLAG_RAID 1
#define ADDPART_FLAG_WHOLEDISK 2
void delete_partition(struct block_device *part);
int bdev_add_partition(struct block_device *bdev, int partno,
sector_t start, sector_t length);
int bdev_del_partition(struct block_device *bdev, int partno);
Expand Down
11 changes: 1 addition & 10 deletions block/genhd.c
Original file line number Diff line number Diff line change
Expand Up @@ -667,9 +667,6 @@ EXPORT_SYMBOL(device_add_disk_no_queue_reg);
*/
void del_gendisk(struct gendisk *disk)
{
struct disk_part_iter piter;
struct block_device *part;

might_sleep();

if (WARN_ON_ONCE(!disk->queue))
Expand All @@ -683,13 +680,7 @@ void del_gendisk(struct gendisk *disk)
* disk is marked as dead (GENHD_FL_UP cleared).
*/
down_write(&bdev_lookup_sem);

/* invalidate stuff */
disk_part_iter_init(&piter, disk, DISK_PITER_INCL_EMPTY);
while ((part = disk_part_iter_next(&piter)))
delete_partition(part);
disk_part_iter_exit(&piter);

blk_drop_partitions(disk);
fsync_bdev(disk->part0);
__invalidate_device(disk->part0, true);

Expand Down
14 changes: 3 additions & 11 deletions block/partitions/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ struct device_type part_type = {
* Must be called either with bd_mutex held, before a disk can be opened or
* after all disk users are gone.
*/
void delete_partition(struct block_device *part)
static void delete_partition(struct block_device *part)
{
fsync_bdev(part);
__invalidate_device(part, true);
Expand Down Expand Up @@ -526,23 +526,15 @@ static bool disk_unlock_native_capacity(struct gendisk *disk)
}
}

int blk_drop_partitions(struct block_device *bdev)
void blk_drop_partitions(struct gendisk *disk)
{
struct disk_part_iter piter;
struct block_device *part;

if (bdev->bd_part_count)
return -EBUSY;

sync_blockdev(bdev);
invalidate_bdev(bdev);

disk_part_iter_init(&piter, bdev->bd_disk, DISK_PITER_INCL_EMPTY);
disk_part_iter_init(&piter, disk, DISK_PITER_INCL_EMPTY);
while ((part = disk_part_iter_next(&piter)))
delete_partition(part);
disk_part_iter_exit(&piter);

return 0;
}

static bool blk_add_partition(struct gendisk *disk, struct block_device *bdev,
Expand Down
8 changes: 5 additions & 3 deletions fs/block_dev.c
Original file line number Diff line number Diff line change
Expand Up @@ -1243,9 +1243,11 @@ int bdev_disk_changed(struct block_device *bdev, bool invalidate)
clear_bit(GD_NEED_PART_SCAN, &bdev->bd_disk->state);

rescan:
ret = blk_drop_partitions(bdev);
if (ret)
return ret;
if (bdev->bd_part_count)
return -EBUSY;
sync_blockdev(bdev);
invalidate_bdev(bdev);
blk_drop_partitions(disk);

/*
* Historically we only set the capacity to zero for devices that
Expand Down
2 changes: 1 addition & 1 deletion include/linux/genhd.h
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ static inline sector_t get_capacity(struct gendisk *disk)

int bdev_disk_changed(struct block_device *bdev, bool invalidate);
int blk_add_partitions(struct gendisk *disk, struct block_device *bdev);
int blk_drop_partitions(struct block_device *bdev);
void blk_drop_partitions(struct gendisk *disk);

extern struct gendisk *__alloc_disk_node(int minors, int node_id);
extern void put_disk(struct gendisk *disk);
Expand Down

0 comments on commit d3c4a43

Please sign in to comment.