Changing sd device from read only to read/write fails in 5.10 (BLKROSET)

From: Michael Katzmann
Date: Fri Jan 15 2021 - 22:04:36 EST


I have USB devices that have a write enable/write protect feature.
A vendor specific SCSI command write enables a write protected drive.
In kernels prior to 5.10 I have been able to write-enable the drive
(by sending the vendor specific command to the SCSI generic device)
and then change the read only state of the sd block device by clearing
the read only state with BLKROSET.

This code used to work to tell the kernel that the device is
read/write (once enabled at the SCSI level). As of kernel 5.10 it does
not.

--------------------------

// other routine opens SCSI generic device
// like /dev/sg1 and sends vendor specific commands to change
// device from read only to read/write.
// The bit in the SCSI Mode Sense is read to confirm device is R/W

int readOnly = 0;
DeviceFD = open( /dev/sdb, O_RDONLY );
// Clear the RO flag in the block device
rtn0 = ioctl( DeviceFD, BLKROSET, &readOnly );
// This re-reads the device so the OS knows that it is now write-enabled
rtn1 = ioctl( DeviceFD, BLKRRPART, 0 );
close( DeviceFD );
// not sure this is necessary
chmod( /dev/sdb, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);

if( (DeviceFD = open( /dev/sdb, O_RDWR )) == ERROR ) {
printf("success\n"); // <== use to work (open R/W)
} else {
printf("failure"); // <== now fails under kernel 5.10
}
close( DeviceFD );

--------------------------

Can anyone tell me if the setting of the R/W state of the block device
has changed or if the method of signaling to the system that the write
state has changed is different in 5.10?