Removable Devices
Thus far, we have ignored the final two file operations in the
block_device_operations structure, which deal with
devices that support removable media. It’s now time to look at them;
sbull isn’t actually removable but it
pretends to be, and therefore it implements these methods.
The operations in question are check_media_change and revalidate. The former is used to find out if the device has changed since the last access, and the latter re-initializes the driver’s status after a disk change.
As far as sbull is concerned, the data area associated with a device is released half a minute after its usage count drops to zero. Leaving the device unmounted (or closed) long enough simulates a disk change, and the next access to the device allocates a new memory area.
This kind of “timely expiration” is implemented using a kernel timer.
check_media_change
The checking function receives kdev_t as a single
argument that identifies the device. The return value is 1 if the
medium has been changed and 0 otherwise. A block driver that doesn’t
support removable devices can avoid declaring the function by setting
bdops->check_media_change to
NULL.
It’s interesting to note that when the device is removable but there is no way to know if it changed, returning 1 is a safe choice. This is the behavior of the IDE driver when dealing with removable disks.
The implementation in sbull returns 1 if the device has already been removed from memory due to the timer expiration, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access