Skip to content

Commit 85ef06d

Browse files
htejunJens Axboe
authored andcommitted
block: flush MEDIA_CHANGE from drivers on close(2)
Currently, only open(2) is defined as the 'clearing' point. It has two roles - first, it's an acknowledgement from userland indicating that the event has been received and kernel can clear pending states and proceed to generate more events. Secondly, it's passed on to device drivers as a hint indicating that a synchronization point has been reached and it might want to take a deeper look at the device. The latter currently is only used by sr which uses two different mechanisms - GET_EVENT_MEDIA_STATUS_NOTIFICATION and TEST_UNIT_READY to discover events, where the former is lighter weight and safe to be used repeatedly but may not provide full coverage. Among other things, GET_EVENT can't detect media removal while TUR can. This patch makes close(2) - blkdev_put() - indicate clearing hint for MEDIA_CHANGE to drivers. disk_check_events() is renamed to disk_flush_events() and updated to take @Mask for events to flush which is or'd to ev->clearing and will be passed to the driver on the next ->check_events() invocation. This change makes sr generate MEDIA_CHANGE when media is ejected from userland - e.g. with eject(1). Note: Given the current usage, it seems @clearing hint is needlessly complex. disk_clear_events() can simply clear all events and the hint can be boolean @flush. Signed-off-by: Tejun Heo <tj@kernel.org> Cc: Kay Sievers <kay.sievers@vrfy.org> Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
1 parent 04bf786 commit 85ef06d

3 files changed

Lines changed: 27 additions & 20 deletions

File tree

‎block/genhd.c‎

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,30 +1500,32 @@ void disk_unblock_events(struct gendisk *disk)
15001500
}
15011501

15021502
/**
1503-
* disk_check_events - schedule immediate event checking
1504-
* @disk: disk to check events for
1503+
* disk_flush_events - schedule immediate event checking and flushing
1504+
* @disk: disk to check and flush events for
1505+
* @mask: events to flush
15051506
*
1506-
* Schedule immediate event checking on @disk if not blocked.
1507+
* Schedule immediate event checking on @disk if not blocked. Events in
1508+
* @mask are scheduled to be cleared from the driver. Note that this
1509+
* doesn't clear the events from @disk->ev.
15071510
*
15081511
* CONTEXT:
1509-
* Don't care. Safe to call from irq context.
1512+
* If @mask is non-zero must be called with bdev->bd_mutex held.
15101513
*/
1511-
void disk_check_events(struct gendisk *disk)
1514+
void disk_flush_events(struct gendisk *disk, unsigned int mask)
15121515
{
15131516
struct disk_events *ev = disk->ev;
1514-
unsigned long flags;
15151517

15161518
if (!ev)
15171519
return;
15181520

1519-
spin_lock_irqsave(&ev->lock, flags);
1521+
spin_lock_irq(&ev->lock);
1522+
ev->clearing |= mask;
15201523
if (!ev->block) {
15211524
cancel_delayed_work(&ev->dwork);
15221525
queue_delayed_work(system_nrt_wq, &ev->dwork, 0);
15231526
}
1524-
spin_unlock_irqrestore(&ev->lock, flags);
1527+
spin_unlock_irq(&ev->lock);
15251528
}
1526-
EXPORT_SYMBOL_GPL(disk_check_events);
15271529

15281530
/**
15291531
* disk_clear_events - synchronously check, clear and return pending events
@@ -1713,7 +1715,7 @@ static int disk_events_set_dfl_poll_msecs(const char *val,
17131715
mutex_lock(&disk_events_mutex);
17141716

17151717
list_for_each_entry(ev, &disk_events, node)
1716-
disk_check_events(ev->disk);
1718+
disk_flush_events(ev->disk, 0);
17171719

17181720
mutex_unlock(&disk_events_mutex);
17191721

‎fs/block_dev.c‎

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1447,6 +1447,8 @@ static int __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part)
14471447

14481448
int blkdev_put(struct block_device *bdev, fmode_t mode)
14491449
{
1450+
mutex_lock(&bdev->bd_mutex);
1451+
14501452
if (mode & FMODE_EXCL) {
14511453
bool bdev_free;
14521454

@@ -1455,7 +1457,6 @@ int blkdev_put(struct block_device *bdev, fmode_t mode)
14551457
* are protected with bdev_lock. bd_mutex is to
14561458
* synchronize disk_holder unlinking.
14571459
*/
1458-
mutex_lock(&bdev->bd_mutex);
14591460
spin_lock(&bdev_lock);
14601461

14611462
WARN_ON_ONCE(--bdev->bd_holders < 0);
@@ -1473,17 +1474,21 @@ int blkdev_put(struct block_device *bdev, fmode_t mode)
14731474
* If this was the last claim, remove holder link and
14741475
* unblock evpoll if it was a write holder.
14751476
*/
1476-
if (bdev_free) {
1477-
if (bdev->bd_write_holder) {
1478-
disk_unblock_events(bdev->bd_disk);
1479-
disk_check_events(bdev->bd_disk);
1480-
bdev->bd_write_holder = false;
1481-
}
1477+
if (bdev_free && bdev->bd_write_holder) {
1478+
disk_unblock_events(bdev->bd_disk);
1479+
bdev->bd_write_holder = false;
14821480
}
1483-
1484-
mutex_unlock(&bdev->bd_mutex);
14851481
}
14861482

1483+
/*
1484+
* Trigger event checking and tell drivers to flush MEDIA_CHANGE
1485+
* event. This is to ensure detection of media removal commanded
1486+
* from userland - e.g. eject(1).
1487+
*/
1488+
disk_flush_events(bdev->bd_disk, DISK_EVENT_MEDIA_CHANGE);
1489+
1490+
mutex_unlock(&bdev->bd_mutex);
1491+
14871492
return __blkdev_put(bdev, mode, 0);
14881493
}
14891494
EXPORT_SYMBOL(blkdev_put);

‎include/linux/genhd.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ static inline int get_disk_ro(struct gendisk *disk)
420420

421421
extern void disk_block_events(struct gendisk *disk);
422422
extern void disk_unblock_events(struct gendisk *disk);
423-
extern void disk_check_events(struct gendisk *disk);
423+
extern void disk_flush_events(struct gendisk *disk, unsigned int mask);
424424
extern unsigned int disk_clear_events(struct gendisk *disk, unsigned int mask);
425425

426426
/* drivers/char/random.c */

0 commit comments

Comments
 (0)