Reinstate Monica
Reinstate Monica

Reputation: 628

How does a linux block driver ensure all I/O has finished before unloading

A linux block device driver allocates a struct gendisk and any associated metdata, and provides a queue or some implementation of the submit_bio function to handle incoming I/O requests from the operating system.

When a disk is being removed, it calls:

del_gendisk(brd->brd_disk);
blk_cleanup_disk(brd->brd_disk);
...
kfree(brd);

see for example, the brd_cleanup routine for the ramdisk driver.

How does the driver know that there is no longer any code running (in submit_bio or in the bio_end_io functions) that might access the gendisk or metadata struct (free'd by kfree(brd) in the example above).

I see that del_gendisk et. al. freeze the queue and don't allow any more I/O, but from perusing the source it seems like this just stop new submissions to submit_bio. It does not track anything past the actual submit_bio call.

The _disk cleanup functions probably block until all open file handles to the device or its partitions are closed. Where is that code?

Upvotes: 1

Views: 189

Answers (1)

stark
stark

Reputation: 13189

blkdev_close is the higher level call which waits for all outstanding I/O. It calls __sync_blockdev to get everything written out. https://elixir.bootlin.com/linux/v4.20.17/source/fs/block_dev.c#L457

Upvotes: 0

Related Questions