Skip to content

Commit 285e967

Browse files
kaysieversjejb
authored andcommitted
[SCSI] sr,sd: send media state change modification events
This will send for a card reader slot (remove/add media): UEVENT[1187091572.155884] change /devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/host7/target7:0:0/7:0:0:0 (scsi) UEVENT[1187091572.162314] remove /block/sdb/sdb1 (block) UEVENT[1187091572.172464] add /block/sdb/sdb1 (block) UEVENT[1187091572.175408] change /devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/host7/target7:0:0/7:0:0:0 (scsi) and for a DVD drive (add/eject media): UEVENT[1187091590.189159] change /devices/pci0000:00/0000:00:1f.1/host4/target4:0:0/4:0:0:0 (scsi) UEVENT[1187091590.957124] add /module/isofs (module) UEVENT[1187091604.468207] change /devices/pci0000:00/0000:00:1f.1/host4/target4:0:0/4:0:0:0 (scsi) Userspace gets events, even for unpartitioned media. This unifies the event handling for asynchronoous events (AN) and events caused by perodical polling the device from userspace. Signed-off-by: Kay Sievers <kay.sievers@vrfy.org> [jejb: modified for new event API] Signed-off-by: James Bottomley <James.Bottomley@HansenPartnership.com>
1 parent d0c4c9d commit 285e967

4 files changed

Lines changed: 28 additions & 12 deletions

File tree

‎drivers/scsi/sd.c‎

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -749,8 +749,11 @@ static int sd_media_changed(struct gendisk *disk)
749749
* can deal with it then. It is only because of unrecoverable errors
750750
* that we would ever take a device offline in the first place.
751751
*/
752-
if (!scsi_device_online(sdp))
753-
goto not_present;
752+
if (!scsi_device_online(sdp)) {
753+
set_media_not_present(sdkp);
754+
retval = 1;
755+
goto out;
756+
}
754757

755758
/*
756759
* Using TEST_UNIT_READY enables differentiation between drive with
@@ -762,6 +765,7 @@ static int sd_media_changed(struct gendisk *disk)
762765
* sd_revalidate() is called.
763766
*/
764767
retval = -ENODEV;
768+
765769
if (scsi_block_when_processing_errors(sdp))
766770
retval = scsi_test_unit_ready(sdp, SD_TIMEOUT, SD_MAX_RETRIES);
767771

@@ -771,8 +775,11 @@ static int sd_media_changed(struct gendisk *disk)
771775
* and we will figure it out later once the drive is
772776
* available again.
773777
*/
774-
if (retval)
775-
goto not_present;
778+
if (retval) {
779+
set_media_not_present(sdkp);
780+
retval = 1;
781+
goto out;
782+
}
776783

777784
/*
778785
* For removable scsi disk we have to recognise the presence
@@ -783,12 +790,11 @@ static int sd_media_changed(struct gendisk *disk)
783790

784791
retval = sdp->changed;
785792
sdp->changed = 0;
786-
793+
out:
794+
if (retval != sdkp->previous_state)
795+
sdev_evt_send_simple(sdp, SDEV_EVT_MEDIA_CHANGE, GFP_KERNEL);
796+
sdkp->previous_state = retval;
787797
return retval;
788-
789-
not_present:
790-
set_media_not_present(sdkp);
791-
return 1;
792798
}
793799

794800
static int sd_sync_cache(struct scsi_disk *sdkp)

‎drivers/scsi/sr.c‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,9 @@ static int sr_media_change(struct cdrom_device_info *cdi, int slot)
192192
* and we will figure it out later once the drive is
193193
* available again. */
194194
cd->device->changed = 1;
195-
return 1; /* This will force a flush, if called from
196-
* check_disk_change */
195+
/* This will force a flush, if called from check_disk_change */
196+
retval = 1;
197+
goto out;
197198
};
198199

199200
retval = cd->device->changed;
@@ -203,9 +204,16 @@ static int sr_media_change(struct cdrom_device_info *cdi, int slot)
203204
if (retval) {
204205
/* check multisession offset etc */
205206
sr_cd_check(cdi);
206-
207207
get_sectorsize(cd);
208208
}
209+
210+
out:
211+
/* Notify userspace, that media has changed. */
212+
if (retval != cd->previous_state)
213+
sdev_evt_send_simple(cd->device, SDEV_EVT_MEDIA_CHANGE,
214+
GFP_KERNEL);
215+
cd->previous_state = retval;
216+
209217
return retval;
210218
}
211219

‎drivers/scsi/sr.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ typedef struct scsi_cd {
3737
unsigned xa_flag:1; /* CD has XA sectors ? */
3838
unsigned readcd_known:1; /* drive supports READ_CD (0xbe) */
3939
unsigned readcd_cdda:1; /* reading audio data using READ_CD */
40+
unsigned previous_state:1; /* media has changed */
4041
struct cdrom_device_info cdi;
4142
/* We hold gendisk and scsi_device references on probe and use
4243
* the refs on this kref to decide when to release them */

‎include/scsi/sd.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ struct scsi_disk {
4141
u32 index;
4242
u8 media_present;
4343
u8 write_prot;
44+
unsigned previous_state : 1;
4445
unsigned WCE : 1; /* state of disk WCE bit */
4546
unsigned RCD : 1; /* state of disk RCD bit, unused */
4647
unsigned DPOFUA : 1; /* state of disk DPOFUA bit */

0 commit comments

Comments
 (0)