drivers/cdrom: improved ioctl for media change detection
Subsystems (3)
PATCH v3 1/1 drivers/cdrom: improved ioctl for media change detection
The current implementation of the CDROM_MEDIA_CHANGED ioctl relies on global state, meaning that only one process can detect a disc change while the ioctl call will return 0 for other calling processes afterwards (see bug 213267 ).
This introduces a new cdrom ioctl, CDROM_TIMED_MEDIA_CHANGE, that works by maintaining a timestamp of the last detected disc change instead of a boolean flag: Processes calling this ioctl command can provide a timestamp of the last disc change known to them and receive an indication whether the disc was changed since then and the updated timestamp.
I considered fixing the buggy behavior in the original CDROM_MEDIA_CHANGED ioctl but that would require maintaining state for each calling process in the kernel, which seems like a worse solution than introducing this new ioctl.
---
I have now incorporated the suggestions of Phil, Christoph and Randy for v2.
- fixed overly long lines
- has_changed is now media_flags in the new ioctl struct
- fixed brace on new line after struct definition
- using if instead of cryptic assignment for better readability
Hope everyone is happy with it now. However, if anything still comes up, let me know. Thanks for all the feedback so far.
Best,
Lukas
--- Documentation/cdrom/cdrom-standard.rst | 11 +++++ Documentation/ioctl/cdrom.rst | 3 ++ drivers/cdrom/cdrom.c | 59 ++++++++++++++++++++++++-- include/linux/cdrom.h | 1 + include/uapi/linux/cdrom.h | 19 +++++++++ 5 files changed, 89 insertions(+), 4 deletions(-)
Documentation/cdrom/cdrom-standard.rst +11 -0
@@ -923,6 +923,17 @@ commands can be identified by the underscores in their names. specifies the slot for which the information is given. The special value *CDSL_CURRENT* requests that information about the currently selected slot be returned. +`CDROM_TIMED_MEDIA_CHANGE` + Checks whether the disc has been changed since a user supplied time + and returns the time of the last disc change. + + *arg* is a pointer to a *cdrom_timed_media_change_info* struct. + *arg->last_media_change* may be set by calling code to signal + the timestamp of the last known media change (by the caller). + Upon successful return, this ioctl call will set + *arg->last_media_change* to the latest media change timestamp (in ms) + known by the kernel/driver and set *arg->has_changed* to 1 if + that timestamp is more recent than the timestamp set by the caller. `CDROM_DRIVE_STATUS` Returns the status of the drive by a call to *drive_status()*. Return values are defined in cdrom_drive_status_.
Documentation/ioctl/cdrom.rst +3 -0
@@ -54,6 +54,9 @@ are as follows: CDROM_SELECT_SPEED Set the CD-ROM speed CDROM_SELECT_DISC Select disc (for juke-boxes) CDROM_MEDIA_CHANGED Check is media changed + CDROM_TIMED_MEDIA_CHANGE Check if media changed + since given time + (struct cdrom_timed_media_change_info) CDROM_DRIVE_STATUS Get tray position, etc. CDROM_DISC_STATUS Get disc type, etc. CDROM_CHANGER_NSLOTS Get number of slots
drivers/cdrom/cdrom.c +55 -4
101 lines not on this page · raw message
include/linux/cdrom.h +1 -0
@@ -64,6 +64,7 @@ struct cdrom_device_info {int for_data;int (*exit)(struct cdrom_device_info *);int mrw_mode_page; +__s64 last_media_change_ms; };struct cdrom_device_ops {
include/uapi/linux/cdrom.h +19 -0
@@ -147,6 +147,8 @@#define CDROM_NEXT_WRITABLE 0x5394/* get next writable block */ #define CDROM_LAST_WRITTEN 0x5395/* get last block written on disc */ +#define CDROM_TIMED_MEDIA_CHANGE 0x5396/* get the timestamp of the last media change */ +/******************************************************* * CDROM IOCTL structures *******************************************************/
@@ -292,6 +294,23 @@ struct cdrom_generic_commandvoid __user *reserved[1 ];/* unused, actually */ }; +/* This struct is used by CDROM_TIMED_MEDIA_CHANGE */ +struct cdrom_timed_media_change_info { +__s64 last_media_change;/* Timestamp of the last detected media +* change in ms. May be set by caller, +* updated upon successful return of +* ioctl. +*/ +__u64 media_flags;/* Flags returned by ioctl to indicate +* media status. +*/ +}; +#define MEDIA_CHANGED_FLAG 0x1/* Last detected media change was more \ +* recent than last_media_change set by\ +* caller. \ +*/ +/* other bits of media_flags available for future use */ +/* * A CD-ROM physical sector size is 2048, 2052, 2056, 2324, 2332, 2336, * 2340, or 2352 bytes long.
Signature · 1 line
2.25.1
Randy Dunlap
reviewerHi Lukas,
Just a minor nit:
include/uapi/linux/cdrom.h struct cdrom_generic_command · line 311
Quoted 4 lines · Lukas Prediger
include/uapi/linux/cdrom.h +4
-0+#define MEDIA_CHANGED_FLAG 0x1/* Last detected media change was more \ +* recent than last_media_change set by\ +* caller. \ +*/
Drop the "continuation" backslashes.
They are not needed.
thanks.
Signature · 1 line
~Randy
Phillip Potter
maintainerQuoted 11 lines · Randy Dunlap
Hi Lukas,
Just a minor nit:
include/uapi/linux/cdrom.h struct cdrom_generic_command · line 311
Quoted 4 lines · Lukas Prediger
include/uapi/linux/cdrom.h +4
-0+#define MEDIA_CHANGED_FLAG 0x1/* Last detected media change was more \ +* recent than last_media_change set by\ +* caller. \ +*/
Quoted 7 lines
Drop the "continuation" backslashes.
They are not needed.thanks.
--
~Randy
Dear Lukas,
Happy to take these out for you and save you resubmitting. I'm very happy with patch anyway. Once I hear back from you I'll send onto Jens with my approval after one final test :-)
Thanks again for the code.
Regards,
Phil
Hi Randy, Hi Phil,
Quoted 11 lines
Quoted 11 lines
Hi Lukas,
Just a minor nit:
Quoted 4 lines · Lukas Prediger
include/uapi/linux/cdrom.h +4
-0+#define MEDIA_CHANGED_FLAG 0x1/* Last detected media change was more \ +* recent than last_media_change set by\ +* caller. \ +*/
Quoted 7 lines
Quoted 7 lines
Drop the "continuation" backslashes.
They are not needed.thanks.
--
~Randy
Hm, my IDE was complaining about these but I just tested building without and that worked fine. No idea what that was about then..
Quoted 7 lines
Dear Lukas,
Happy to take these out for you and save you resubmitting. I'm very happy with patch anyway. Once I hear back from you I'll send onto Jens with my approval after one final test :-)
That would be very nice of you!
Quoted 3 lines
Thanks again for the code.
My pleasure, and thanks for the helpful feedback!
Quoted 3 lines
Regards,
Phil
Best regards,
Lukas
Phillip Potter
maintainerQuoted 38 lines · Lukas Prediger
Hi Randy, Hi Phil,
Quoted 11 lines
Quoted 11 lines
Hi Lukas,
Just a minor nit:
Quoted 4 lines · Lukas Prediger
include/uapi/linux/cdrom.h +4
-0+#define MEDIA_CHANGED_FLAG 0x1/* Last detected media change was more \ +* recent than last_media_change set by\ +* caller. \ +*/ Quoted 7 lines
Quoted 7 lines
Drop the "continuation" backslashes.
They are not needed.thanks.
--
~RandyHm, my IDE was complaining about these but I just tested building without and that worked fine. No idea what that was about then..
Quoted 7 lines
Dear Lukas,
Happy to take these out for you and save you resubmitting. I'm very happy with patch anyway. Once I hear back from you I'll send onto Jens with my approval after one final test :-)
That would be very nice of you!
Quoted 3 lines
Thanks again for the code.
Quoted 8 lines
My pleasure, and thanks for the helpful feedback!
Quoted 3 lines
Regards,
PhilBest regards,
Lukas
Dear Lukas,
This v3 patch does not apply to my tree, or the mainline one for that matter. A few problems I've noticed that are the cause of this:
Misnamed file:Documentation/ioctl/cdrom.rst - this file should actually be:Documentation/userspace-api/ioctl/cdrom.rst (and is in v2)
Failed hunks:
v3 fails on applying the changes to drivers/cdrom/cdrom.c as well, due to two hunks failing (because of different lines preceding your changes).
It also fails to apply the second hunk to include/uapi/linux/cdrom.h, again due to differences in pre-existing content. Did you base this v3 patch against a different/older tree/branch?
Please advise, many thanks. As mentioned before, v2 applied perfectly fine for me.
Regards,
Phil
PATCH v3 1/1 drivers/cdrom: improved ioctl for media change detection
The current implementation of the CDROM_MEDIA_CHANGED ioctl relies on global state, meaning that only one process can detect a disc change while the ioctl call will return 0 for other calling processes afterwards (see bug 213267 ).
This introduces a new cdrom ioctl, CDROM_TIMED_MEDIA_CHANGE, that works by maintaining a timestamp of the last detected disc change instead of a boolean flag: Processes calling this ioctl command can provide a timestamp of the last disc change known to them and receive an indication whether the disc was changed since then and the updated timestamp.
I considered fixing the buggy behavior in the original CDROM_MEDIA_CHANGED ioctl but that would require maintaining state for each calling process in the kernel, which seems like a worse solution than introducing this new ioctl.
---
Quoted 5 lines
Dear Lukas,
This v3 patch does not apply to my tree, or the mainline one for that matter. A few problems I've noticed that are the cause of this:
Quoted 7 lines
[..]
Please advise, many thanks. As mentioned before, v2 applied perfectly fine for me.
Regards,
Phil
Hey Phil,
it seems I had accidentally formated the patch off my v5.4 testing branch. Here's the proper version based on current master, that should apply cleanly.
I've also removed the continuation backslashes that Randy pointed out now.
Sorry for the troubles,
Lukas
--- Documentation/cdrom/cdrom-standard.rst | 11 ++++ Documentation/userspace-api/ioctl/cdrom.rst | 3 ++ drivers/cdrom/cdrom.c | 59 +++++++++++++++++++-- include/linux/cdrom.h | 1 + include/uapi/linux/cdrom.h | 19 +++++++ 5 files changed, 89 insertions(+), 4 deletions(-)
Documentation/cdrom/cdrom-standard.rst +11 -0
@@ -907,6 +907,17 @@ commands can be identified by the underscores in their names. specifies the slot for which the information is given. The special value *CDSL_CURRENT* requests that information about the currently selected slot be returned. +`CDROM_TIMED_MEDIA_CHANGE` + Checks whether the disc has been changed since a user supplied time + and returns the time of the last disc change. + + *arg* is a pointer to a *cdrom_timed_media_change_info* struct. + *arg->last_media_change* may be set by calling code to signal + the timestamp of the last known media change (by the caller). + Upon successful return, this ioctl call will set + *arg->last_media_change* to the latest media change timestamp (in ms) + known by the kernel/driver and set *arg->has_changed* to 1 if + that timestamp is more recent than the timestamp set by the caller. `CDROM_DRIVE_STATUS` Returns the status of the drive by a call to *drive_status()*. Return values are defined in cdrom_drive_status_.
Documentation/userspace-api/ioctl/cdrom.rst +3 -0
@@ -54,6 +54,9 @@ are as follows: CDROM_SELECT_SPEED Set the CD-ROM speed CDROM_SELECT_DISC Select disc (for juke-boxes) CDROM_MEDIA_CHANGED Check is media changed + CDROM_TIMED_MEDIA_CHANGE Check if media changed + since given time + (struct cdrom_timed_media_change_info) CDROM_DRIVE_STATUS Get tray position, etc. CDROM_DISC_STATUS Get disc type, etc. CDROM_CHANGER_NSLOTS Get number of slots
drivers/cdrom/cdrom.c +55 -4
101 lines not on this page · raw message
include/linux/cdrom.h +1 -0
@@ -64,6 +64,7 @@ struct cdrom_device_info {int for_data;int (*exit)(struct cdrom_device_info *);int mrw_mode_page; +__s64 last_media_change_ms; };struct cdrom_device_ops {
include/uapi/linux/cdrom.h +19 -0
@@ -147,6 +147,8 @@#define CDROM_NEXT_WRITABLE 0x5394/* get next writable block */ #define CDROM_LAST_WRITTEN 0x5395/* get last block written on disc */ +#define CDROM_TIMED_MEDIA_CHANGE 0x5396/* get the timestamp of the last media change */ +/******************************************************* * CDROM IOCTL structures *******************************************************/
@@ -295,6 +297,23 @@ struct cdrom_generic_command }; }; +/* This struct is used by CDROM_TIMED_MEDIA_CHANGE */ +struct cdrom_timed_media_change_info { +__s64 last_media_change;/* Timestamp of the last detected media +* change in ms. May be set by caller, +* updated upon successful return of +* ioctl. +*/ +__u64 media_flags;/* Flags returned by ioctl to indicate +* media status. +*/ +}; +#define MEDIA_CHANGED_FLAG 0x1/* Last detected media change was more +* recent than last_media_change set by +* caller. +*/ +/* other bits of media_flags available for future use */ +/* * A CD-ROM physical sector size is 2048, 2052, 2056, 2324, 2332, 2336, * 2340, or 2352 bytes long.
Signature · 1 line
2.25.1
Phillip Potter
maintainerQuoted 13 lines · Lukas Prediger
Quoted 5 lines
Dear Lukas,
This v3 patch does not apply to my tree, or the mainline one for that matter. A few problems I've noticed that are the cause of this:
Quoted 7 lines
[..]
Please advise, many thanks. As mentioned before, v2 applied perfectly fine for me.
Regards,
Phil
Quoted 9 lines
Hey Phil,
it seems I had accidentally formated the patch off my v5.4 testing branch. Here's the proper version based on current master, that should apply cleanly.I've also removed the continuation backslashes that Randy pointed out now.
Sorry for the troubles,
Lukas
Dear Lukas,
No apology necessary - I will check this over after work this evening and then send onto Jens. Many thanks.
Regards,
Phil
Randy Dunlap
reviewerQuoted 5 lines · Lukas Prediger
Documentation/userspace-api/ioctl/cdrom.rst +0
-0
Documentation/userspace-api/ioctl/cdrom.rst are as follows: · line 62
Quoted 9 lines
Documentation/userspace-api/ioctl/cdrom.rst +3
-0@@ -54,6 +54,9 @@ are as follows: CDROM_SELECT_SPEED Set the CD-ROM speed CDROM_SELECT_DISC Select disc (for juke-boxes) CDROM_MEDIA_CHANGED Check is media changed + CDROM_TIMED_MEDIA_CHANGE Check if media changed + since given time + (struct cdrom_timed_media_change_info) CDROM_DRIVE_STATUS Get tray position, etc. CDROM_DISC_STATUS Get disc type, etc. CDROM_CHANGER_NSLOTS Get number of slots
Hi Lukas, Phil,
This doc change causes a documentation build warning:
Documentation/userspace-api/ioctl/cdrom.rst:57: WARNING: Malformed table. Text in column margin in table line 42.
The "=====" lines describe the table columns and they cannot be exceeded without a warning. The table needs to be reformatted.
Lukas, will you handle that?
thanks.
====================== ===============================================
CDROMPAUSE Pause Audio Operation
CDROMRESUME Resume paused Audio Operation
CDROMPLAYMSF Play Audio MSF (struct cdrom_msf)
CDROMPLAYTRKIND Play Audio Track/index (struct cdrom_ti)
CDROMREADTOCHDR Read TOC header (struct cdrom_tochdr)
CDROMREADTOCENTRY Read TOC entry (struct cdrom_tocentry)
CDROMSTOP Stop the cdrom drive
CDROMSTART Start the cdrom drive
CDROMEJECT Ejects the cdrom media
CDROMVOLCTRL Control output volume (struct cdrom_volctrl)
CDROMSUBCHNL Read subchannel data (struct cdrom_subchnl)
CDROMREADMODE2 Read CDROM mode 2 data (2336 Bytes)
(struct cdrom_read)
CDROMREADMODE1 Read CDROM mode 1 data (2048 Bytes)
(struct cdrom_read)
CDROMREADAUDIO (struct cdrom_read_audio)
CDROMEJECT_SW enable(1)/disable(0) auto-ejecting
CDROMMULTISESSION Obtain the start-of-last-session address of multi session disks (struct cdrom_multisession)CDROM_GET_MCN Obtain the "Universal Product Code"
if available (struct cdrom_mcn)
CDROM_GET_UPC Deprecated, use CDROM_GET_MCN instead.
CDROMRESET hard-reset the drive
CDROMVOLREAD Get the drive's volume setting
(struct cdrom_volctrl)
CDROMREADRAW read data in raw mode (2352 Bytes)
(struct cdrom_read)
CDROMREADCOOKED read data in cooked mode
CDROMSEEK seek msf address
CDROMPLAYBLK scsi-cd only, (struct cdrom_blk)
CDROMREADALL read all 2646 bytes
CDROMGETSPINDOWN return 4-bit spindown value
CDROMSETSPINDOWN set 4-bit spindown value
CDROMCLOSETRAY pendant of CDROMEJECT
CDROM_SET_OPTIONS Set behavior options
CDROM_CLEAR_OPTIONS Clear behavior options
CDROM_SELECT_SPEED Set the CD-ROM speed
CDROM_SELECT_DISC Select disc (for juke-boxes)
CDROM_MEDIA_CHANGED Check is media changed
CDROM_TIMED_MEDIA_CHANGE Check if media changed
since given time
(struct cdrom_timed_media_change_info)
CDROM_DRIVE_STATUS Get tray position, etc.
CDROM_DISC_STATUS Get disc type, etc.
CDROM_CHANGER_NSLOTS Get number of slots
CDROM_LOCKDOOR lock or unlock door
CDROM_DEBUG Turn debug messages on/off
CDROM_GET_CAPABILITY get capabilities
CDROMAUDIOBUFSIZ set the audio buffer size
DVD_READ_STRUCT Read structure
DVD_WRITE_STRUCT Write structure
DVD_AUTH Authentication
CDROM_SEND_PACKET send a packet to the drive
CDROM_NEXT_WRITABLE get next writable block
CDROM_LAST_WRITTEN get last block written on disc
====================== ===============================================Signature · 1 line
~Randy
Phillip Potter
maintainerQuoted 80 lines · Randy Dunlap
Quoted 5 lines · Lukas Prediger
Documentation/userspace-api/ioctl/cdrom.rst +0
-0Documentation/userspace-api/ioctl/cdrom.rst are as follows: · line 62
Quoted 9 lines
Documentation/userspace-api/ioctl/cdrom.rst +3
-0@@ -54,6 +54,9 @@ are as follows: CDROM_SELECT_SPEED Set the CD-ROM speed CDROM_SELECT_DISC Select disc (for juke-boxes) CDROM_MEDIA_CHANGED Check is media changed + CDROM_TIMED_MEDIA_CHANGE Check if media changed + since given time + (struct cdrom_timed_media_change_info) CDROM_DRIVE_STATUS Get tray position, etc. CDROM_DISC_STATUS Get disc type, etc. CDROM_CHANGER_NSLOTS Get number of slotsHi Lukas, Phil,
This doc change causes a documentation build warning:
Documentation/userspace-api/ioctl/cdrom.rst:57: WARNING: Malformed table. Text in column margin in table line 42.The "=====" lines describe the table columns and they cannot be exceeded without a warning. The table needs to be reformatted.
Lukas, will you handle that?
thanks.====================== =============================================== CDROMPAUSE Pause Audio Operation CDROMRESUME Resume paused Audio Operation CDROMPLAYMSF Play Audio MSF (struct cdrom_msf) CDROMPLAYTRKIND Play Audio Track/index (struct cdrom_ti) CDROMREADTOCHDR Read TOC header (struct cdrom_tochdr) CDROMREADTOCENTRY Read TOC entry (struct cdrom_tocentry) CDROMSTOP Stop the cdrom drive CDROMSTART Start the cdrom drive CDROMEJECT Ejects the cdrom media CDROMVOLCTRL Control output volume (struct cdrom_volctrl) CDROMSUBCHNL Read subchannel data (struct cdrom_subchnl) CDROMREADMODE2 Read CDROM mode 2 data (2336 Bytes) (struct cdrom_read) CDROMREADMODE1 Read CDROM mode 1 data (2048 Bytes) (struct cdrom_read) CDROMREADAUDIO (struct cdrom_read_audio) CDROMEJECT_SW enable(1)/disable(0) auto-ejecting CDROMMULTISESSION Obtain the start-of-last-sessionaddress of multi session disks (struct
cdrom_multisession)CDROM_GET_MCNObtain the "Universal Product Code"
if available (structcdrom_mcn)CDROM_GET_UPC Deprecated, use CDROM_GET_MCN instead. CDROMRESET hard-reset the drive CDROMVOLREAD Get the drive's volume setting (struct cdrom_volctrl) CDROMREADRAW read data in raw mode (2352 Bytes) (struct cdrom_read) CDROMREADCOOKED read data in cooked mode CDROMSEEK seek msf address CDROMPLAYBLK scsi-cd only, (struct cdrom_blk) CDROMREADALL read all 2646 bytes CDROMGETSPINDOWN return 4-bit spindown value CDROMSETSPINDOWN set 4-bit spindown value CDROMCLOSETRAY pendant of CDROMEJECT CDROM_SET_OPTIONS Set behavior options CDROM_CLEAR_OPTIONS Clear behavior options CDROM_SELECT_SPEED Set the CD-ROM speed CDROM_SELECT_DISC Select disc (for juke-boxes) CDROM_MEDIA_CHANGED Check is media changed CDROM_TIMED_MEDIA_CHANGE Check if media changed since given time (struct cdrom_timed_media_change_info) CDROM_DRIVE_STATUS Get tray position, etc. CDROM_DISC_STATUS Get disc type, etc. CDROM_CHANGER_NSLOTS Get number of slots CDROM_LOCKDOOR lock or unlock door CDROM_DEBUG Turn debug messages on/off CDROM_GET_CAPABILITY get capabilities
Quoted 12 lines
CDROMAUDIOBUFSIZ set the audio buffer size DVD_READ_STRUCT Read structure DVD_WRITE_STRUCT Write structure DVD_AUTH Authentication CDROM_SEND_PACKET send a packet to the drive CDROM_NEXT_WRITABLE get next writable block CDROM_LAST_WRITTEN get last block written on disc ====================== ===============================================Signature · 1 line
~Randy
Hi Randy,
Thanks for heads up. I've prepared a patch to reformat the table which I'll send shortly.
Regards,
Phil