Skip to content

Commit

Permalink
block: add a ->free_disk method
Browse files Browse the repository at this point in the history
Add a method to notify the driver that the gendisk is about to be freed.
This allows drivers to tie the lifetime of their private data to that of
the gendisk and thus deal with device removal races without expensive
synchronization and boilerplate code.

A new flag is added so that ->free_disk is only called after a successful
call to add_disk, which significantly simplifies the error handling path
during probing.

Signed-off-by: Christoph Hellwig <hch@lst.de>
Link: https://lore.kernel.org/r/20220215094514.3828912-2-hch@lst.de
Signed-off-by: Jens Axboe <axboe@kernel.dk>
  • Loading branch information
Christoph Hellwig authored and Jens Axboe committed Feb 17, 2022
1 parent 34841e6 commit 7679205
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
5 changes: 5 additions & 0 deletions block/genhd.c
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,7 @@ int __must_check device_add_disk(struct device *parent, struct gendisk *disk,

disk_update_readahead(disk);
disk_add_events(disk);
set_bit(GD_ADDED, &disk->state);
return 0;

out_unregister_bdi:
Expand Down Expand Up @@ -1119,6 +1120,10 @@ static void disk_release(struct device *dev)
xa_destroy(&disk->part_tbl);
disk->queue->disk = NULL;
blk_put_queue(disk->queue);

if (test_bit(GD_ADDED, &disk->state) && disk->fops->free_disk)
disk->fops->free_disk(disk);

iput(disk->part0->bd_inode); /* frees the disk */
}

Expand Down
2 changes: 2 additions & 0 deletions include/linux/blkdev.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ struct gendisk {
#define GD_READ_ONLY 1
#define GD_DEAD 2
#define GD_NATIVE_CAPACITY 3
#define GD_ADDED 4

struct mutex open_mutex; /* open/close mutex */
unsigned open_partitions; /* number of open partitions */
Expand Down Expand Up @@ -1464,6 +1465,7 @@ struct block_device_operations {
void (*unlock_native_capacity) (struct gendisk *);
int (*getgeo)(struct block_device *, struct hd_geometry *);
int (*set_read_only)(struct block_device *bdev, bool ro);
void (*free_disk)(struct gendisk *disk);
/* this callback is with swap_lock and sometimes page table lock held */
void (*swap_slot_free_notify) (struct block_device *, unsigned long);
int (*report_zones)(struct gendisk *, sector_t sector,
Expand Down

0 comments on commit 7679205

Please sign in to comment.