Skip to content

Commit 67f1e02

Browse files
lumipaxboe
authored andcommitted
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. Signed-off-by: Lukas Prediger <lumip@lumip.de> Link: https://lore.kernel.org/all/20210912191207.74449-1-lumip@lumip.de Signed-off-by: Phillip Potter <phil@philpotter.co.uk> Link: https://lore.kernel.org/r/20210913230942.1188-1-phil@philpotter.co.uk Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 3163462 commit 67f1e02

5 files changed

Lines changed: 89 additions & 4 deletions

File tree

‎Documentation/cdrom/cdrom-standard.rst‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,17 @@ commands can be identified by the underscores in their names.
907907
specifies the slot for which the information is given. The special
908908
value *CDSL_CURRENT* requests that information about the currently
909909
selected slot be returned.
910+
`CDROM_TIMED_MEDIA_CHANGE`
911+
Checks whether the disc has been changed since a user supplied time
912+
and returns the time of the last disc change.
913+
914+
*arg* is a pointer to a *cdrom_timed_media_change_info* struct.
915+
*arg->last_media_change* may be set by calling code to signal
916+
the timestamp of the last known media change (by the caller).
917+
Upon successful return, this ioctl call will set
918+
*arg->last_media_change* to the latest media change timestamp (in ms)
919+
known by the kernel/driver and set *arg->has_changed* to 1 if
920+
that timestamp is more recent than the timestamp set by the caller.
910921
`CDROM_DRIVE_STATUS`
911922
Returns the status of the drive by a call to
912923
*drive_status()*. Return values are defined in cdrom_drive_status_.

‎Documentation/userspace-api/ioctl/cdrom.rst‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ are as follows:
5454
CDROM_SELECT_SPEED Set the CD-ROM speed
5555
CDROM_SELECT_DISC Select disc (for juke-boxes)
5656
CDROM_MEDIA_CHANGED Check is media changed
57+
CDROM_TIMED_MEDIA_CHANGE Check if media changed
58+
since given time
59+
(struct cdrom_timed_media_change_info)
5760
CDROM_DRIVE_STATUS Get tray position, etc.
5861
CDROM_DISC_STATUS Get disc type, etc.
5962
CDROM_CHANGER_NSLOTS Get number of slots

‎drivers/cdrom/cdrom.c‎

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,12 @@ static void cdrom_sysctl_register(void);
344344

345345
static LIST_HEAD(cdrom_list);
346346

347+
static void signal_media_change(struct cdrom_device_info *cdi)
348+
{
349+
cdi->mc_flags = 0x3; /* set media changed bits, on both queues */
350+
cdi->last_media_change_ms = ktime_to_ms(ktime_get());
351+
}
352+
347353
int cdrom_dummy_generic_packet(struct cdrom_device_info *cdi,
348354
struct packet_command *cgc)
349355
{
@@ -616,6 +622,7 @@ int register_cdrom(struct gendisk *disk, struct cdrom_device_info *cdi)
616622
ENSURE(cdo, generic_packet, CDC_GENERIC_PACKET);
617623
cdi->mc_flags = 0;
618624
cdi->options = CDO_USE_FFLAGS;
625+
cdi->last_media_change_ms = ktime_to_ms(ktime_get());
619626

620627
if (autoclose == 1 && CDROM_CAN(CDC_CLOSE_TRAY))
621628
cdi->options |= (int) CDO_AUTO_CLOSE;
@@ -1421,8 +1428,7 @@ static int cdrom_select_disc(struct cdrom_device_info *cdi, int slot)
14211428
cdi->ops->check_events(cdi, 0, slot);
14221429

14231430
if (slot == CDSL_NONE) {
1424-
/* set media changed bits, on both queues */
1425-
cdi->mc_flags = 0x3;
1431+
signal_media_change(cdi);
14261432
return cdrom_load_unload(cdi, -1);
14271433
}
14281434

@@ -1455,7 +1461,7 @@ static int cdrom_select_disc(struct cdrom_device_info *cdi, int slot)
14551461
slot = curslot;
14561462

14571463
/* set media changed bits on both queues */
1458-
cdi->mc_flags = 0x3;
1464+
signal_media_change(cdi);
14591465
if ((ret = cdrom_load_unload(cdi, slot)))
14601466
return ret;
14611467

@@ -1521,7 +1527,7 @@ int media_changed(struct cdrom_device_info *cdi, int queue)
15211527
cdi->ioctl_events = 0;
15221528

15231529
if (changed) {
1524-
cdi->mc_flags = 0x3; /* set bit on both queues */
1530+
signal_media_change(cdi);
15251531
ret |= 1;
15261532
cdi->media_written = 0;
15271533
}
@@ -2336,6 +2342,49 @@ static int cdrom_ioctl_media_changed(struct cdrom_device_info *cdi,
23362342
return ret;
23372343
}
23382344

2345+
/*
2346+
* Media change detection with timing information.
2347+
*
2348+
* arg is a pointer to a cdrom_timed_media_change_info struct.
2349+
* arg->last_media_change may be set by calling code to signal
2350+
* the timestamp (in ms) of the last known media change (by the caller).
2351+
* Upon successful return, ioctl call will set arg->last_media_change
2352+
* to the latest media change timestamp known by the kernel/driver
2353+
* and set arg->has_changed to 1 if that timestamp is more recent
2354+
* than the timestamp set by the caller.
2355+
*/
2356+
static int cdrom_ioctl_timed_media_change(struct cdrom_device_info *cdi,
2357+
unsigned long arg)
2358+
{
2359+
int ret;
2360+
struct cdrom_timed_media_change_info __user *info;
2361+
struct cdrom_timed_media_change_info tmp_info;
2362+
2363+
if (!CDROM_CAN(CDC_MEDIA_CHANGED))
2364+
return -ENOSYS;
2365+
2366+
info = (struct cdrom_timed_media_change_info __user *)arg;
2367+
cd_dbg(CD_DO_IOCTL, "entering CDROM_TIMED_MEDIA_CHANGE\n");
2368+
2369+
ret = cdrom_ioctl_media_changed(cdi, CDSL_CURRENT);
2370+
if (ret < 0)
2371+
return ret;
2372+
2373+
if (copy_from_user(&tmp_info, info, sizeof(tmp_info)) != 0)
2374+
return -EFAULT;
2375+
2376+
tmp_info.media_flags = 0;
2377+
if (tmp_info.last_media_change - cdi->last_media_change_ms < 0)
2378+
tmp_info.media_flags |= MEDIA_CHANGED_FLAG;
2379+
2380+
tmp_info.last_media_change = cdi->last_media_change_ms;
2381+
2382+
if (copy_to_user(info, &tmp_info, sizeof(*info)) != 0)
2383+
return -EFAULT;
2384+
2385+
return 0;
2386+
}
2387+
23392388
static int cdrom_ioctl_set_options(struct cdrom_device_info *cdi,
23402389
unsigned long arg)
23412390
{
@@ -3313,6 +3362,8 @@ int cdrom_ioctl(struct cdrom_device_info *cdi, struct block_device *bdev,
33133362
return cdrom_ioctl_eject_sw(cdi, arg);
33143363
case CDROM_MEDIA_CHANGED:
33153364
return cdrom_ioctl_media_changed(cdi, arg);
3365+
case CDROM_TIMED_MEDIA_CHANGE:
3366+
return cdrom_ioctl_timed_media_change(cdi, arg);
33163367
case CDROM_SET_OPTIONS:
33173368
return cdrom_ioctl_set_options(cdi, arg);
33183369
case CDROM_CLEAR_OPTIONS:

‎include/linux/cdrom.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ struct cdrom_device_info {
6464
int for_data;
6565
int (*exit)(struct cdrom_device_info *);
6666
int mrw_mode_page;
67+
__s64 last_media_change_ms;
6768
};
6869

6970
struct cdrom_device_ops {

‎include/uapi/linux/cdrom.h‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@
147147
#define CDROM_NEXT_WRITABLE 0x5394 /* get next writable block */
148148
#define CDROM_LAST_WRITTEN 0x5395 /* get last block written on disc */
149149

150+
#define CDROM_TIMED_MEDIA_CHANGE 0x5396 /* get the timestamp of the last media change */
151+
150152
/*******************************************************
151153
* CDROM IOCTL structures
152154
*******************************************************/
@@ -295,6 +297,23 @@ struct cdrom_generic_command
295297
};
296298
};
297299

300+
/* This struct is used by CDROM_TIMED_MEDIA_CHANGE */
301+
struct cdrom_timed_media_change_info {
302+
__s64 last_media_change; /* Timestamp of the last detected media
303+
* change in ms. May be set by caller,
304+
* updated upon successful return of
305+
* ioctl.
306+
*/
307+
__u64 media_flags; /* Flags returned by ioctl to indicate
308+
* media status.
309+
*/
310+
};
311+
#define MEDIA_CHANGED_FLAG 0x1 /* Last detected media change was more
312+
* recent than last_media_change set by
313+
* caller.
314+
*/
315+
/* other bits of media_flags available for future use */
316+
298317
/*
299318
* A CD-ROM physical sector size is 2048, 2052, 2056, 2324, 2332, 2336,
300319
* 2340, or 2352 bytes long.

0 commit comments

Comments
 (0)