Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Lukas Prediger and committed by
Jens Axboe
67f1e027 31634624

+89 -4
+11
Documentation/cdrom/cdrom-standard.rst
··· 907 907 specifies the slot for which the information is given. The special 908 908 value *CDSL_CURRENT* requests that information about the currently 909 909 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. 910 921 `CDROM_DRIVE_STATUS` 911 922 Returns the status of the drive by a call to 912 923 *drive_status()*. Return values are defined in cdrom_drive_status_.
+3
Documentation/userspace-api/ioctl/cdrom.rst
··· 54 54 CDROM_SELECT_SPEED Set the CD-ROM speed 55 55 CDROM_SELECT_DISC Select disc (for juke-boxes) 56 56 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) 57 60 CDROM_DRIVE_STATUS Get tray position, etc. 58 61 CDROM_DISC_STATUS Get disc type, etc. 59 62 CDROM_CHANGER_NSLOTS Get number of slots
+55 -4
drivers/cdrom/cdrom.c
··· 344 344 345 345 static LIST_HEAD(cdrom_list); 346 346 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 + 347 353 int cdrom_dummy_generic_packet(struct cdrom_device_info *cdi, 348 354 struct packet_command *cgc) 349 355 { ··· 622 616 ENSURE(cdo, generic_packet, CDC_GENERIC_PACKET); 623 617 cdi->mc_flags = 0; 624 618 cdi->options = CDO_USE_FFLAGS; 619 + cdi->last_media_change_ms = ktime_to_ms(ktime_get()); 625 620 626 621 if (autoclose == 1 && CDROM_CAN(CDC_CLOSE_TRAY)) 627 622 cdi->options |= (int) CDO_AUTO_CLOSE; ··· 1428 1421 cdi->ops->check_events(cdi, 0, slot); 1429 1422 1430 1423 if (slot == CDSL_NONE) { 1431 - /* set media changed bits, on both queues */ 1432 - cdi->mc_flags = 0x3; 1424 + signal_media_change(cdi); 1433 1425 return cdrom_load_unload(cdi, -1); 1434 1426 } 1435 1427 ··· 1461 1455 slot = curslot; 1462 1456 1463 1457 /* set media changed bits on both queues */ 1464 - cdi->mc_flags = 0x3; 1458 + signal_media_change(cdi); 1465 1459 if ((ret = cdrom_load_unload(cdi, slot))) 1466 1460 return ret; 1467 1461 ··· 1527 1521 cdi->ioctl_events = 0; 1528 1522 1529 1523 if (changed) { 1530 - cdi->mc_flags = 0x3; /* set bit on both queues */ 1524 + signal_media_change(cdi); 1531 1525 ret |= 1; 1532 1526 cdi->media_written = 0; 1533 1527 } ··· 2340 2334 ret = info->slots[arg].change; 2341 2335 kfree(info); 2342 2336 return ret; 2337 + } 2338 + 2339 + /* 2340 + * Media change detection with timing information. 2341 + * 2342 + * arg is a pointer to a cdrom_timed_media_change_info struct. 2343 + * arg->last_media_change may be set by calling code to signal 2344 + * the timestamp (in ms) of the last known media change (by the caller). 2345 + * Upon successful return, ioctl call will set arg->last_media_change 2346 + * to the latest media change timestamp known by the kernel/driver 2347 + * and set arg->has_changed to 1 if that timestamp is more recent 2348 + * than the timestamp set by the caller. 2349 + */ 2350 + static int cdrom_ioctl_timed_media_change(struct cdrom_device_info *cdi, 2351 + unsigned long arg) 2352 + { 2353 + int ret; 2354 + struct cdrom_timed_media_change_info __user *info; 2355 + struct cdrom_timed_media_change_info tmp_info; 2356 + 2357 + if (!CDROM_CAN(CDC_MEDIA_CHANGED)) 2358 + return -ENOSYS; 2359 + 2360 + info = (struct cdrom_timed_media_change_info __user *)arg; 2361 + cd_dbg(CD_DO_IOCTL, "entering CDROM_TIMED_MEDIA_CHANGE\n"); 2362 + 2363 + ret = cdrom_ioctl_media_changed(cdi, CDSL_CURRENT); 2364 + if (ret < 0) 2365 + return ret; 2366 + 2367 + if (copy_from_user(&tmp_info, info, sizeof(tmp_info)) != 0) 2368 + return -EFAULT; 2369 + 2370 + tmp_info.media_flags = 0; 2371 + if (tmp_info.last_media_change - cdi->last_media_change_ms < 0) 2372 + tmp_info.media_flags |= MEDIA_CHANGED_FLAG; 2373 + 2374 + tmp_info.last_media_change = cdi->last_media_change_ms; 2375 + 2376 + if (copy_to_user(info, &tmp_info, sizeof(*info)) != 0) 2377 + return -EFAULT; 2378 + 2379 + return 0; 2343 2380 } 2344 2381 2345 2382 static int cdrom_ioctl_set_options(struct cdrom_device_info *cdi, ··· 3362 3313 return cdrom_ioctl_eject_sw(cdi, arg); 3363 3314 case CDROM_MEDIA_CHANGED: 3364 3315 return cdrom_ioctl_media_changed(cdi, arg); 3316 + case CDROM_TIMED_MEDIA_CHANGE: 3317 + return cdrom_ioctl_timed_media_change(cdi, arg); 3365 3318 case CDROM_SET_OPTIONS: 3366 3319 return cdrom_ioctl_set_options(cdi, arg); 3367 3320 case CDROM_CLEAR_OPTIONS:
+1
include/linux/cdrom.h
··· 64 64 int for_data; 65 65 int (*exit)(struct cdrom_device_info *); 66 66 int mrw_mode_page; 67 + __s64 last_media_change_ms; 67 68 }; 68 69 69 70 struct cdrom_device_ops {
+19
include/uapi/linux/cdrom.h
··· 147 147 #define CDROM_NEXT_WRITABLE 0x5394 /* get next writable block */ 148 148 #define CDROM_LAST_WRITTEN 0x5395 /* get last block written on disc */ 149 149 150 + #define CDROM_TIMED_MEDIA_CHANGE 0x5396 /* get the timestamp of the last media change */ 151 + 150 152 /******************************************************* 151 153 * CDROM IOCTL structures 152 154 *******************************************************/ ··· 296 294 void __user *unused; 297 295 }; 298 296 }; 297 + 298 + /* This struct is used by CDROM_TIMED_MEDIA_CHANGE */ 299 + struct cdrom_timed_media_change_info { 300 + __s64 last_media_change; /* Timestamp of the last detected media 301 + * change in ms. May be set by caller, 302 + * updated upon successful return of 303 + * ioctl. 304 + */ 305 + __u64 media_flags; /* Flags returned by ioctl to indicate 306 + * media status. 307 + */ 308 + }; 309 + #define MEDIA_CHANGED_FLAG 0x1 /* Last detected media change was more 310 + * recent than last_media_change set by 311 + * caller. 312 + */ 313 + /* other bits of media_flags available for future use */ 299 314 300 315 /* 301 316 * A CD-ROM physical sector size is 2048, 2052, 2056, 2324, 2332, 2336,