2019-05-01 02:42:43 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* gendisk handling
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <linux/genhd.h>
|
2007-02-21 05:57:48 +08:00
|
|
|
#include <linux/kdev_t.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/blkdev.h>
|
2015-05-23 05:13:32 +08:00
|
|
|
#include <linux/backing-dev.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/spinlock.h>
|
2008-10-05 03:53:21 +08:00
|
|
|
#include <linux/proc_fs.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <linux/seq_file.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/kmod.h>
|
|
|
|
#include <linux/kobj_map.h>
|
2006-02-07 06:12:43 +08:00
|
|
|
#include <linux/mutex.h>
|
2008-08-25 18:47:22 +08:00
|
|
|
#include <linux/idr.h>
|
implement in-kernel gendisk events handling
Currently, media presence polling for removeable block devices is done
from userland. There are several issues with this.
* Polling is done by periodically opening the device. For SCSI
devices, the command sequence generated by such action involves a
few different commands including TEST_UNIT_READY. This behavior,
while perfectly legal, is different from Windows which only issues
single command, GET_EVENT_STATUS_NOTIFICATION. Unfortunately, some
ATAPI devices lock up after being periodically queried such command
sequences.
* There is no reliable and unintrusive way for a userland program to
tell whether the target device is safe for media presence polling.
For example, polling for media presence during an on-going burning
session can make it fail. The polling program can avoid this by
opening the device with O_EXCL but then it risks making a valid
exclusive user of the device fail w/ -EBUSY.
* Userland polling is unnecessarily heavy and in-kernel implementation
is lighter and better coordinated (workqueue, timer slack).
This patch implements framework for in-kernel disk event handling,
which includes media presence polling.
* bdops->check_events() is added, which supercedes ->media_changed().
It should check whether there's any pending event and return if so.
Currently, two events are defined - DISK_EVENT_MEDIA_CHANGE and
DISK_EVENT_EJECT_REQUEST. ->check_events() is guaranteed not to be
called parallelly.
* gendisk->events and ->async_events are added. These should be
initialized by block driver before passing the device to add_disk().
The former contains the mask of all supported events and the latter
the mask of all events which the device can report without polling.
/sys/block/*/events[_async] export these to userland.
* Kernel parameter block.events_dfl_poll_msecs controls the system
polling interval (default is 0 which means disable) and
/sys/block/*/events_poll_msecs control polling intervals for
individual devices (default is -1 meaning use system setting). Note
that if a device can report all supported events asynchronously and
its polling interval isn't explicitly set, the device won't be
polled regardless of the system polling interval.
* If a device is opened exclusively with write access, event checking
is automatically disabled until all write exclusive accesses are
released.
* There are event 'clearing' events. For example, both of currently
defined events are cleared after the device has been successfully
opened. This information is passed to ->check_events() callback
using @clearing argument as a hint.
* Event checking is always performed from system_nrt_wq and timer
slack is set to 25% for polling.
* Nothing changes for drivers which implement ->media_changed() but
not ->check_events(). Going forward, all drivers will be converted
to ->check_events() and ->media_change() will be dropped.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
2010-12-09 03:57:37 +08:00
|
|
|
#include <linux/log2.h>
|
2013-02-23 08:34:13 +08:00
|
|
|
#include <linux/pm_runtime.h>
|
2016-01-10 00:36:51 +08:00
|
|
|
#include <linux/badblocks.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2008-03-04 18:23:45 +08:00
|
|
|
#include "blk.h"
|
|
|
|
|
2007-05-22 04:08:01 +08:00
|
|
|
static DEFINE_MUTEX(block_class_lock);
|
|
|
|
struct kobject *block_depr;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2008-08-25 18:47:22 +08:00
|
|
|
/* for extended dynamic devt allocation, currently only one major is used */
|
2013-02-28 09:03:56 +08:00
|
|
|
#define NR_EXT_DEVT (1 << MINORBITS)
|
2008-08-25 18:47:22 +08:00
|
|
|
|
2014-08-26 23:05:36 +08:00
|
|
|
/* For extended devt allocation. ext_devt_lock prevents look up
|
2008-08-25 18:47:22 +08:00
|
|
|
* results from going away underneath its user.
|
|
|
|
*/
|
2014-08-26 23:05:36 +08:00
|
|
|
static DEFINE_SPINLOCK(ext_devt_lock);
|
2008-08-25 18:47:22 +08:00
|
|
|
static DEFINE_IDR(ext_devt_idr);
|
|
|
|
|
2017-06-21 02:15:48 +08:00
|
|
|
static const struct device_type disk_type;
|
2008-03-04 18:23:46 +08:00
|
|
|
|
2012-12-19 04:27:20 +08:00
|
|
|
static void disk_check_events(struct disk_events *ev,
|
|
|
|
unsigned int *clearing_ptr);
|
2012-03-02 17:43:28 +08:00
|
|
|
static void disk_alloc_events(struct gendisk *disk);
|
implement in-kernel gendisk events handling
Currently, media presence polling for removeable block devices is done
from userland. There are several issues with this.
* Polling is done by periodically opening the device. For SCSI
devices, the command sequence generated by such action involves a
few different commands including TEST_UNIT_READY. This behavior,
while perfectly legal, is different from Windows which only issues
single command, GET_EVENT_STATUS_NOTIFICATION. Unfortunately, some
ATAPI devices lock up after being periodically queried such command
sequences.
* There is no reliable and unintrusive way for a userland program to
tell whether the target device is safe for media presence polling.
For example, polling for media presence during an on-going burning
session can make it fail. The polling program can avoid this by
opening the device with O_EXCL but then it risks making a valid
exclusive user of the device fail w/ -EBUSY.
* Userland polling is unnecessarily heavy and in-kernel implementation
is lighter and better coordinated (workqueue, timer slack).
This patch implements framework for in-kernel disk event handling,
which includes media presence polling.
* bdops->check_events() is added, which supercedes ->media_changed().
It should check whether there's any pending event and return if so.
Currently, two events are defined - DISK_EVENT_MEDIA_CHANGE and
DISK_EVENT_EJECT_REQUEST. ->check_events() is guaranteed not to be
called parallelly.
* gendisk->events and ->async_events are added. These should be
initialized by block driver before passing the device to add_disk().
The former contains the mask of all supported events and the latter
the mask of all events which the device can report without polling.
/sys/block/*/events[_async] export these to userland.
* Kernel parameter block.events_dfl_poll_msecs controls the system
polling interval (default is 0 which means disable) and
/sys/block/*/events_poll_msecs control polling intervals for
individual devices (default is -1 meaning use system setting). Note
that if a device can report all supported events asynchronously and
its polling interval isn't explicitly set, the device won't be
polled regardless of the system polling interval.
* If a device is opened exclusively with write access, event checking
is automatically disabled until all write exclusive accesses are
released.
* There are event 'clearing' events. For example, both of currently
defined events are cleared after the device has been successfully
opened. This information is passed to ->check_events() callback
using @clearing argument as a hint.
* Event checking is always performed from system_nrt_wq and timer
slack is set to 25% for polling.
* Nothing changes for drivers which implement ->media_changed() but
not ->check_events(). Going forward, all drivers will be converted
to ->check_events() and ->media_change() will be dropped.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
2010-12-09 03:57:37 +08:00
|
|
|
static void disk_add_events(struct gendisk *disk);
|
|
|
|
static void disk_del_events(struct gendisk *disk);
|
|
|
|
static void disk_release_events(struct gendisk *disk);
|
|
|
|
|
2020-03-13 13:30:05 +08:00
|
|
|
/*
|
|
|
|
* Set disk capacity and notify if the size is not currently
|
|
|
|
* zero and will not be set to zero
|
|
|
|
*/
|
|
|
|
void set_capacity_revalidate_and_notify(struct gendisk *disk, sector_t size,
|
|
|
|
bool revalidate)
|
|
|
|
{
|
|
|
|
sector_t capacity = get_capacity(disk);
|
|
|
|
|
|
|
|
set_capacity(disk, size);
|
|
|
|
|
|
|
|
if (revalidate)
|
|
|
|
revalidate_disk(disk);
|
|
|
|
|
|
|
|
if (capacity != size && capacity != 0 && size != 0) {
|
|
|
|
char *envp[] = { "RESIZE=1", NULL };
|
|
|
|
|
|
|
|
kobject_uevent_env(&disk_to_dev(disk)->kobj, KOBJ_CHANGE, envp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
EXPORT_SYMBOL_GPL(set_capacity_revalidate_and_notify);
|
|
|
|
|
|
|
|
|
2017-08-09 07:51:45 +08:00
|
|
|
void part_inc_in_flight(struct request_queue *q, struct hd_struct *part, int rw)
|
|
|
|
{
|
2018-11-16 03:22:51 +08:00
|
|
|
if (queue_is_mq(q))
|
2017-08-09 07:51:45 +08:00
|
|
|
return;
|
|
|
|
|
2018-12-07 00:41:20 +08:00
|
|
|
part_stat_local_inc(part, in_flight[rw]);
|
2017-08-09 07:51:45 +08:00
|
|
|
if (part->partno)
|
2018-12-07 00:41:20 +08:00
|
|
|
part_stat_local_inc(&part_to_disk(part)->part0, in_flight[rw]);
|
2017-08-09 07:51:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void part_dec_in_flight(struct request_queue *q, struct hd_struct *part, int rw)
|
|
|
|
{
|
2018-11-16 03:22:51 +08:00
|
|
|
if (queue_is_mq(q))
|
2017-08-09 07:51:45 +08:00
|
|
|
return;
|
|
|
|
|
2018-12-07 00:41:20 +08:00
|
|
|
part_stat_local_dec(part, in_flight[rw]);
|
2017-08-09 07:51:45 +08:00
|
|
|
if (part->partno)
|
2018-12-07 00:41:20 +08:00
|
|
|
part_stat_local_dec(&part_to_disk(part)->part0, in_flight[rw]);
|
2017-08-09 07:51:45 +08:00
|
|
|
}
|
|
|
|
|
2018-12-07 00:41:21 +08:00
|
|
|
unsigned int part_in_flight(struct request_queue *q, struct hd_struct *part)
|
2017-08-09 07:51:45 +08:00
|
|
|
{
|
2018-12-07 00:41:20 +08:00
|
|
|
int cpu;
|
2018-12-07 00:41:21 +08:00
|
|
|
unsigned int inflight;
|
2018-12-07 00:41:20 +08:00
|
|
|
|
2018-11-16 03:22:51 +08:00
|
|
|
if (queue_is_mq(q)) {
|
2018-12-07 00:41:21 +08:00
|
|
|
return blk_mq_in_flight(q, part);
|
2017-08-09 07:51:45 +08:00
|
|
|
}
|
|
|
|
|
2018-12-07 00:41:21 +08:00
|
|
|
inflight = 0;
|
2018-12-07 00:41:20 +08:00
|
|
|
for_each_possible_cpu(cpu) {
|
2018-12-07 00:41:21 +08:00
|
|
|
inflight += part_stat_local_read_cpu(part, in_flight[0], cpu) +
|
|
|
|
part_stat_local_read_cpu(part, in_flight[1], cpu);
|
2018-12-07 00:41:20 +08:00
|
|
|
}
|
2018-12-07 00:41:21 +08:00
|
|
|
if ((int)inflight < 0)
|
|
|
|
inflight = 0;
|
2018-12-07 00:41:20 +08:00
|
|
|
|
2018-12-07 00:41:21 +08:00
|
|
|
return inflight;
|
2017-08-09 07:51:45 +08:00
|
|
|
}
|
|
|
|
|
2018-04-26 15:21:59 +08:00
|
|
|
void part_in_flight_rw(struct request_queue *q, struct hd_struct *part,
|
|
|
|
unsigned int inflight[2])
|
|
|
|
{
|
2018-12-07 00:41:20 +08:00
|
|
|
int cpu;
|
|
|
|
|
2018-11-16 03:22:51 +08:00
|
|
|
if (queue_is_mq(q)) {
|
2018-04-26 15:21:59 +08:00
|
|
|
blk_mq_in_flight_rw(q, part, inflight);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-07 00:41:20 +08:00
|
|
|
inflight[0] = 0;
|
|
|
|
inflight[1] = 0;
|
|
|
|
for_each_possible_cpu(cpu) {
|
|
|
|
inflight[0] += part_stat_local_read_cpu(part, in_flight[0], cpu);
|
|
|
|
inflight[1] += part_stat_local_read_cpu(part, in_flight[1], cpu);
|
|
|
|
}
|
|
|
|
if ((int)inflight[0] < 0)
|
|
|
|
inflight[0] = 0;
|
|
|
|
if ((int)inflight[1] < 0)
|
|
|
|
inflight[1] = 0;
|
2018-04-26 15:21:59 +08:00
|
|
|
}
|
|
|
|
|
2017-08-24 01:10:30 +08:00
|
|
|
struct hd_struct *__disk_get_part(struct gendisk *disk, int partno)
|
|
|
|
{
|
|
|
|
struct disk_part_tbl *ptbl = rcu_dereference(disk->part_tbl);
|
|
|
|
|
|
|
|
if (unlikely(partno < 0 || partno >= ptbl->len))
|
|
|
|
return NULL;
|
|
|
|
return rcu_dereference(ptbl->part[partno]);
|
|
|
|
}
|
|
|
|
|
2008-09-03 15:03:02 +08:00
|
|
|
/**
|
|
|
|
* disk_get_part - get partition
|
|
|
|
* @disk: disk to look partition from
|
|
|
|
* @partno: partition number
|
|
|
|
*
|
|
|
|
* Look for partition @partno from @disk. If found, increment
|
|
|
|
* reference count and return it.
|
|
|
|
*
|
|
|
|
* CONTEXT:
|
|
|
|
* Don't care.
|
|
|
|
*
|
|
|
|
* RETURNS:
|
|
|
|
* Pointer to the found partition on success, NULL if not found.
|
|
|
|
*/
|
|
|
|
struct hd_struct *disk_get_part(struct gendisk *disk, int partno)
|
|
|
|
{
|
2017-08-24 01:10:30 +08:00
|
|
|
struct hd_struct *part;
|
2008-08-25 18:56:15 +08:00
|
|
|
|
2008-09-03 15:03:02 +08:00
|
|
|
rcu_read_lock();
|
2017-08-24 01:10:30 +08:00
|
|
|
part = __disk_get_part(disk, partno);
|
|
|
|
if (part)
|
|
|
|
get_device(part_to_dev(part));
|
2008-09-03 15:03:02 +08:00
|
|
|
rcu_read_unlock();
|
|
|
|
|
|
|
|
return part;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(disk_get_part);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* disk_part_iter_init - initialize partition iterator
|
|
|
|
* @piter: iterator to initialize
|
|
|
|
* @disk: disk to iterate over
|
|
|
|
* @flags: DISK_PITER_* flags
|
|
|
|
*
|
|
|
|
* Initialize @piter so that it iterates over partitions of @disk.
|
|
|
|
*
|
|
|
|
* CONTEXT:
|
|
|
|
* Don't care.
|
|
|
|
*/
|
|
|
|
void disk_part_iter_init(struct disk_part_iter *piter, struct gendisk *disk,
|
|
|
|
unsigned int flags)
|
|
|
|
{
|
2008-08-25 18:56:15 +08:00
|
|
|
struct disk_part_tbl *ptbl;
|
|
|
|
|
|
|
|
rcu_read_lock();
|
|
|
|
ptbl = rcu_dereference(disk->part_tbl);
|
|
|
|
|
2008-09-03 15:03:02 +08:00
|
|
|
piter->disk = disk;
|
|
|
|
piter->part = NULL;
|
|
|
|
|
|
|
|
if (flags & DISK_PITER_REVERSE)
|
2008-08-25 18:56:15 +08:00
|
|
|
piter->idx = ptbl->len - 1;
|
2009-04-17 14:34:48 +08:00
|
|
|
else if (flags & (DISK_PITER_INCL_PART0 | DISK_PITER_INCL_EMPTY_PART0))
|
2008-09-03 15:03:02 +08:00
|
|
|
piter->idx = 0;
|
2008-09-03 15:06:42 +08:00
|
|
|
else
|
|
|
|
piter->idx = 1;
|
2008-09-03 15:03:02 +08:00
|
|
|
|
|
|
|
piter->flags = flags;
|
2008-08-25 18:56:15 +08:00
|
|
|
|
|
|
|
rcu_read_unlock();
|
2008-09-03 15:03:02 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(disk_part_iter_init);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* disk_part_iter_next - proceed iterator to the next partition and return it
|
|
|
|
* @piter: iterator of interest
|
|
|
|
*
|
|
|
|
* Proceed @piter to the next partition and return it.
|
|
|
|
*
|
|
|
|
* CONTEXT:
|
|
|
|
* Don't care.
|
|
|
|
*/
|
|
|
|
struct hd_struct *disk_part_iter_next(struct disk_part_iter *piter)
|
|
|
|
{
|
2008-08-25 18:56:15 +08:00
|
|
|
struct disk_part_tbl *ptbl;
|
2008-09-03 15:03:02 +08:00
|
|
|
int inc, end;
|
|
|
|
|
|
|
|
/* put the last partition */
|
|
|
|
disk_put_part(piter->part);
|
|
|
|
piter->part = NULL;
|
|
|
|
|
2008-08-25 18:56:15 +08:00
|
|
|
/* get part_tbl */
|
2008-09-03 15:03:02 +08:00
|
|
|
rcu_read_lock();
|
2008-08-25 18:56:15 +08:00
|
|
|
ptbl = rcu_dereference(piter->disk->part_tbl);
|
2008-09-03 15:03:02 +08:00
|
|
|
|
|
|
|
/* determine iteration parameters */
|
|
|
|
if (piter->flags & DISK_PITER_REVERSE) {
|
|
|
|
inc = -1;
|
2009-04-17 14:34:48 +08:00
|
|
|
if (piter->flags & (DISK_PITER_INCL_PART0 |
|
|
|
|
DISK_PITER_INCL_EMPTY_PART0))
|
2008-09-03 15:06:42 +08:00
|
|
|
end = -1;
|
|
|
|
else
|
|
|
|
end = 0;
|
2008-09-03 15:03:02 +08:00
|
|
|
} else {
|
|
|
|
inc = 1;
|
2008-08-25 18:56:15 +08:00
|
|
|
end = ptbl->len;
|
2008-09-03 15:03:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* iterate to the next partition */
|
|
|
|
for (; piter->idx != end; piter->idx += inc) {
|
|
|
|
struct hd_struct *part;
|
|
|
|
|
2008-08-25 18:56:15 +08:00
|
|
|
part = rcu_dereference(ptbl->part[piter->idx]);
|
2008-09-03 15:03:02 +08:00
|
|
|
if (!part)
|
|
|
|
continue;
|
2012-08-01 18:24:18 +08:00
|
|
|
if (!part_nr_sects_read(part) &&
|
2009-04-17 14:34:48 +08:00
|
|
|
!(piter->flags & DISK_PITER_INCL_EMPTY) &&
|
|
|
|
!(piter->flags & DISK_PITER_INCL_EMPTY_PART0 &&
|
|
|
|
piter->idx == 0))
|
2008-09-03 15:03:02 +08:00
|
|
|
continue;
|
|
|
|
|
2008-08-25 18:56:05 +08:00
|
|
|
get_device(part_to_dev(part));
|
2008-09-03 15:03:02 +08:00
|
|
|
piter->part = part;
|
|
|
|
piter->idx += inc;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
rcu_read_unlock();
|
|
|
|
|
|
|
|
return piter->part;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(disk_part_iter_next);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* disk_part_iter_exit - finish up partition iteration
|
|
|
|
* @piter: iter of interest
|
|
|
|
*
|
|
|
|
* Called when iteration is over. Cleans up @piter.
|
|
|
|
*
|
|
|
|
* CONTEXT:
|
|
|
|
* Don't care.
|
|
|
|
*/
|
|
|
|
void disk_part_iter_exit(struct disk_part_iter *piter)
|
|
|
|
{
|
|
|
|
disk_put_part(piter->part);
|
|
|
|
piter->part = NULL;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(disk_part_iter_exit);
|
|
|
|
|
2008-10-24 18:52:42 +08:00
|
|
|
static inline int sector_in_part(struct hd_struct *part, sector_t sector)
|
|
|
|
{
|
|
|
|
return part->start_sect <= sector &&
|
2012-08-01 18:24:18 +08:00
|
|
|
sector < part->start_sect + part_nr_sects_read(part);
|
2008-10-24 18:52:42 +08:00
|
|
|
}
|
|
|
|
|
2008-09-03 15:03:02 +08:00
|
|
|
/**
|
|
|
|
* disk_map_sector_rcu - map sector to partition
|
|
|
|
* @disk: gendisk of interest
|
|
|
|
* @sector: sector to map
|
|
|
|
*
|
|
|
|
* Find out which partition @sector maps to on @disk. This is
|
|
|
|
* primarily used for stats accounting.
|
|
|
|
*
|
|
|
|
* CONTEXT:
|
|
|
|
* RCU read locked. The returned partition pointer is valid only
|
|
|
|
* while preemption is disabled.
|
|
|
|
*
|
|
|
|
* RETURNS:
|
2008-08-25 18:56:14 +08:00
|
|
|
* Found partition on success, part0 is returned if no partition matches
|
2008-09-03 15:03:02 +08:00
|
|
|
*/
|
|
|
|
struct hd_struct *disk_map_sector_rcu(struct gendisk *disk, sector_t sector)
|
|
|
|
{
|
2008-08-25 18:56:15 +08:00
|
|
|
struct disk_part_tbl *ptbl;
|
2008-10-24 18:52:42 +08:00
|
|
|
struct hd_struct *part;
|
2008-09-03 15:03:02 +08:00
|
|
|
int i;
|
|
|
|
|
2008-08-25 18:56:15 +08:00
|
|
|
ptbl = rcu_dereference(disk->part_tbl);
|
|
|
|
|
2008-10-24 18:52:42 +08:00
|
|
|
part = rcu_dereference(ptbl->last_lookup);
|
|
|
|
if (part && sector_in_part(part, sector))
|
|
|
|
return part;
|
|
|
|
|
2008-08-25 18:56:15 +08:00
|
|
|
for (i = 1; i < ptbl->len; i++) {
|
2008-10-24 18:52:42 +08:00
|
|
|
part = rcu_dereference(ptbl->part[i]);
|
2008-09-03 15:03:02 +08:00
|
|
|
|
2008-10-24 18:52:42 +08:00
|
|
|
if (part && sector_in_part(part, sector)) {
|
|
|
|
rcu_assign_pointer(ptbl->last_lookup, part);
|
2008-09-03 15:03:02 +08:00
|
|
|
return part;
|
2008-10-24 18:52:42 +08:00
|
|
|
}
|
2008-09-03 15:03:02 +08:00
|
|
|
}
|
2008-08-25 18:56:14 +08:00
|
|
|
return &disk->part0;
|
2008-09-03 15:03:02 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(disk_map_sector_rcu);
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* Can be deleted altogether. Later.
|
|
|
|
*
|
|
|
|
*/
|
2017-06-17 07:48:21 +08:00
|
|
|
#define BLKDEV_MAJOR_HASH_SIZE 255
|
2005-04-17 06:20:36 +08:00
|
|
|
static struct blk_major_name {
|
|
|
|
struct blk_major_name *next;
|
|
|
|
int major;
|
|
|
|
char name[16];
|
2006-03-31 18:30:32 +08:00
|
|
|
} *major_names[BLKDEV_MAJOR_HASH_SIZE];
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/* index in the above - for now: assume no multimajor ranges */
|
2010-12-17 16:00:18 +08:00
|
|
|
static inline int major_to_index(unsigned major)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2006-03-31 18:30:32 +08:00
|
|
|
return major % BLKDEV_MAJOR_HASH_SIZE;
|
2006-01-15 05:20:38 +08:00
|
|
|
}
|
|
|
|
|
2006-03-31 18:30:32 +08:00
|
|
|
#ifdef CONFIG_PROC_FS
|
2008-09-03 15:01:09 +08:00
|
|
|
void blkdev_show(struct seq_file *seqf, off_t offset)
|
2006-01-15 05:20:38 +08:00
|
|
|
{
|
2006-03-31 18:30:32 +08:00
|
|
|
struct blk_major_name *dp;
|
2006-01-15 05:20:38 +08:00
|
|
|
|
2017-06-17 07:48:21 +08:00
|
|
|
mutex_lock(&block_class_lock);
|
|
|
|
for (dp = major_names[major_to_index(offset)]; dp; dp = dp->next)
|
|
|
|
if (dp->major == offset)
|
2008-09-03 15:01:09 +08:00
|
|
|
seq_printf(seqf, "%3d %s\n", dp->major, dp->name);
|
2017-06-17 07:48:21 +08:00
|
|
|
mutex_unlock(&block_class_lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2006-03-31 18:30:32 +08:00
|
|
|
#endif /* CONFIG_PROC_FS */
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2009-02-20 15:12:51 +08:00
|
|
|
/**
|
|
|
|
* register_blkdev - register a new block device
|
|
|
|
*
|
2018-02-06 10:25:27 +08:00
|
|
|
* @major: the requested major device number [1..BLKDEV_MAJOR_MAX-1]. If
|
|
|
|
* @major = 0, try to allocate any unused major number.
|
2009-02-20 15:12:51 +08:00
|
|
|
* @name: the name of the new block device as a zero terminated string
|
|
|
|
*
|
|
|
|
* The @name must be unique within the system.
|
|
|
|
*
|
2017-03-31 04:11:36 +08:00
|
|
|
* The return value depends on the @major input parameter:
|
|
|
|
*
|
2018-02-06 10:25:27 +08:00
|
|
|
* - if a major device number was requested in range [1..BLKDEV_MAJOR_MAX-1]
|
|
|
|
* then the function returns zero on success, or a negative error code
|
2017-03-31 04:11:36 +08:00
|
|
|
* - if any unused major number was requested with @major = 0 parameter
|
2009-02-20 15:12:51 +08:00
|
|
|
* then the return value is the allocated major number in range
|
2018-02-06 10:25:27 +08:00
|
|
|
* [1..BLKDEV_MAJOR_MAX-1] or a negative error code otherwise
|
|
|
|
*
|
|
|
|
* See Documentation/admin-guide/devices.txt for the list of allocated
|
|
|
|
* major numbers.
|
2009-02-20 15:12:51 +08:00
|
|
|
*/
|
2005-04-17 06:20:36 +08:00
|
|
|
int register_blkdev(unsigned int major, const char *name)
|
|
|
|
{
|
|
|
|
struct blk_major_name **n, *p;
|
|
|
|
int index, ret = 0;
|
|
|
|
|
2007-05-22 04:08:01 +08:00
|
|
|
mutex_lock(&block_class_lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/* temporary */
|
|
|
|
if (major == 0) {
|
|
|
|
for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) {
|
|
|
|
if (major_names[index] == NULL)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index == 0) {
|
2019-02-17 23:21:56 +08:00
|
|
|
printk("%s: failed to get major for %s\n",
|
|
|
|
__func__, name);
|
2005-04-17 06:20:36 +08:00
|
|
|
ret = -EBUSY;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
major = index;
|
|
|
|
ret = major;
|
|
|
|
}
|
|
|
|
|
2017-06-17 07:48:21 +08:00
|
|
|
if (major >= BLKDEV_MAJOR_MAX) {
|
2019-02-17 23:21:56 +08:00
|
|
|
pr_err("%s: major requested (%u) is greater than the maximum (%u) for %s\n",
|
|
|
|
__func__, major, BLKDEV_MAJOR_MAX-1, name);
|
2017-06-17 07:48:21 +08:00
|
|
|
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL);
|
|
|
|
if (p == NULL) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
p->major = major;
|
|
|
|
strlcpy(p->name, name, sizeof(p->name));
|
|
|
|
p->next = NULL;
|
|
|
|
index = major_to_index(major);
|
|
|
|
|
|
|
|
for (n = &major_names[index]; *n; n = &(*n)->next) {
|
|
|
|
if ((*n)->major == major)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!*n)
|
|
|
|
*n = p;
|
|
|
|
else
|
|
|
|
ret = -EBUSY;
|
|
|
|
|
|
|
|
if (ret < 0) {
|
2018-02-06 10:25:27 +08:00
|
|
|
printk("register_blkdev: cannot get major %u for %s\n",
|
2005-04-17 06:20:36 +08:00
|
|
|
major, name);
|
|
|
|
kfree(p);
|
|
|
|
}
|
|
|
|
out:
|
2007-05-22 04:08:01 +08:00
|
|
|
mutex_unlock(&block_class_lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
EXPORT_SYMBOL(register_blkdev);
|
|
|
|
|
2007-07-17 19:03:47 +08:00
|
|
|
void unregister_blkdev(unsigned int major, const char *name)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct blk_major_name **n;
|
|
|
|
struct blk_major_name *p = NULL;
|
|
|
|
int index = major_to_index(major);
|
|
|
|
|
2007-05-22 04:08:01 +08:00
|
|
|
mutex_lock(&block_class_lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
for (n = &major_names[index]; *n; n = &(*n)->next)
|
|
|
|
if ((*n)->major == major)
|
|
|
|
break;
|
2007-07-17 19:03:45 +08:00
|
|
|
if (!*n || strcmp((*n)->name, name)) {
|
|
|
|
WARN_ON(1);
|
|
|
|
} else {
|
2005-04-17 06:20:36 +08:00
|
|
|
p = *n;
|
|
|
|
*n = p->next;
|
|
|
|
}
|
2007-05-22 04:08:01 +08:00
|
|
|
mutex_unlock(&block_class_lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
kfree(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
EXPORT_SYMBOL(unregister_blkdev);
|
|
|
|
|
|
|
|
static struct kobj_map *bdev_map;
|
|
|
|
|
2008-08-25 18:47:25 +08:00
|
|
|
/**
|
|
|
|
* blk_mangle_minor - scatter minor numbers apart
|
|
|
|
* @minor: minor number to mangle
|
|
|
|
*
|
|
|
|
* Scatter consecutively allocated @minor number apart if MANGLE_DEVT
|
|
|
|
* is enabled. Mangling twice gives the original value.
|
|
|
|
*
|
|
|
|
* RETURNS:
|
|
|
|
* Mangled value.
|
|
|
|
*
|
|
|
|
* CONTEXT:
|
|
|
|
* Don't care.
|
|
|
|
*/
|
|
|
|
static int blk_mangle_minor(int minor)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < MINORBITS / 2; i++) {
|
|
|
|
int low = minor & (1 << i);
|
|
|
|
int high = minor & (1 << (MINORBITS - 1 - i));
|
|
|
|
int distance = MINORBITS - 1 - 2 * i;
|
|
|
|
|
|
|
|
minor ^= low | high; /* clear both bits */
|
|
|
|
low <<= distance; /* swap the positions */
|
|
|
|
high >>= distance;
|
|
|
|
minor |= low | high; /* and set */
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return minor;
|
|
|
|
}
|
|
|
|
|
2008-08-25 18:47:22 +08:00
|
|
|
/**
|
|
|
|
* blk_alloc_devt - allocate a dev_t for a partition
|
|
|
|
* @part: partition to allocate dev_t for
|
|
|
|
* @devt: out parameter for resulting dev_t
|
|
|
|
*
|
|
|
|
* Allocate a dev_t for block device.
|
|
|
|
*
|
|
|
|
* RETURNS:
|
|
|
|
* 0 on success, allocated dev_t is returned in *@devt. -errno on
|
|
|
|
* failure.
|
|
|
|
*
|
|
|
|
* CONTEXT:
|
|
|
|
* Might sleep.
|
|
|
|
*/
|
|
|
|
int blk_alloc_devt(struct hd_struct *part, dev_t *devt)
|
|
|
|
{
|
|
|
|
struct gendisk *disk = part_to_disk(part);
|
2013-02-28 09:03:57 +08:00
|
|
|
int idx;
|
2008-08-25 18:47:22 +08:00
|
|
|
|
|
|
|
/* in consecutive minor range? */
|
|
|
|
if (part->partno < disk->minors) {
|
|
|
|
*devt = MKDEV(disk->major, disk->first_minor + part->partno);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* allocate ext devt */
|
2014-08-26 23:05:36 +08:00
|
|
|
idr_preload(GFP_KERNEL);
|
|
|
|
|
2015-06-11 11:47:14 +08:00
|
|
|
spin_lock_bh(&ext_devt_lock);
|
2014-08-26 23:05:36 +08:00
|
|
|
idx = idr_alloc(&ext_devt_idr, part, 0, NR_EXT_DEVT, GFP_NOWAIT);
|
2015-06-11 11:47:14 +08:00
|
|
|
spin_unlock_bh(&ext_devt_lock);
|
2014-08-26 23:05:36 +08:00
|
|
|
|
|
|
|
idr_preload_end();
|
2013-02-28 09:03:57 +08:00
|
|
|
if (idx < 0)
|
|
|
|
return idx == -ENOSPC ? -EBUSY : idx;
|
2008-08-25 18:47:22 +08:00
|
|
|
|
2008-08-25 18:47:25 +08:00
|
|
|
*devt = MKDEV(BLOCK_EXT_MAJOR, blk_mangle_minor(idx));
|
2008-08-25 18:47:22 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* blk_free_devt - free a dev_t
|
|
|
|
* @devt: dev_t to free
|
|
|
|
*
|
|
|
|
* Free @devt which was allocated using blk_alloc_devt().
|
|
|
|
*
|
|
|
|
* CONTEXT:
|
|
|
|
* Might sleep.
|
|
|
|
*/
|
|
|
|
void blk_free_devt(dev_t devt)
|
|
|
|
{
|
|
|
|
if (devt == MKDEV(0, 0))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (MAJOR(devt) == BLOCK_EXT_MAJOR) {
|
2015-06-11 11:47:14 +08:00
|
|
|
spin_lock_bh(&ext_devt_lock);
|
2008-08-25 18:47:25 +08:00
|
|
|
idr_remove(&ext_devt_idr, blk_mangle_minor(MINOR(devt)));
|
2015-06-11 11:47:14 +08:00
|
|
|
spin_unlock_bh(&ext_devt_lock);
|
2008-08-25 18:47:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-31 08:00:47 +08:00
|
|
|
/*
|
|
|
|
* We invalidate devt by assigning NULL pointer for devt in idr.
|
2019-04-02 20:06:34 +08:00
|
|
|
*/
|
|
|
|
void blk_invalidate_devt(dev_t devt)
|
|
|
|
{
|
|
|
|
if (MAJOR(devt) == BLOCK_EXT_MAJOR) {
|
|
|
|
spin_lock_bh(&ext_devt_lock);
|
|
|
|
idr_replace(&ext_devt_idr, NULL, blk_mangle_minor(MINOR(devt)));
|
|
|
|
spin_unlock_bh(&ext_devt_lock);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-25 18:47:23 +08:00
|
|
|
static char *bdevt_str(dev_t devt, char *buf)
|
|
|
|
{
|
|
|
|
if (MAJOR(devt) <= 0xff && MINOR(devt) <= 0xff) {
|
|
|
|
char tbuf[BDEVT_SIZE];
|
|
|
|
snprintf(tbuf, BDEVT_SIZE, "%02x%02x", MAJOR(devt), MINOR(devt));
|
|
|
|
snprintf(buf, BDEVT_SIZE, "%-9s", tbuf);
|
|
|
|
} else
|
|
|
|
snprintf(buf, BDEVT_SIZE, "%03x:%05x", MAJOR(devt), MINOR(devt));
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* Register device numbers dev..(dev+range-1)
|
|
|
|
* range must be nonzero
|
|
|
|
* The hash chain is sorted on range, so that subranges can override.
|
|
|
|
*/
|
2007-05-22 04:08:01 +08:00
|
|
|
void blk_register_region(dev_t devt, unsigned long range, struct module *module,
|
2005-04-17 06:20:36 +08:00
|
|
|
struct kobject *(*probe)(dev_t, int *, void *),
|
|
|
|
int (*lock)(dev_t, void *), void *data)
|
|
|
|
{
|
2007-05-22 04:08:01 +08:00
|
|
|
kobj_map(bdev_map, devt, range, module, probe, lock, data);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
EXPORT_SYMBOL(blk_register_region);
|
|
|
|
|
2007-05-22 04:08:01 +08:00
|
|
|
void blk_unregister_region(dev_t devt, unsigned long range)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2007-05-22 04:08:01 +08:00
|
|
|
kobj_unmap(bdev_map, devt, range);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
EXPORT_SYMBOL(blk_unregister_region);
|
|
|
|
|
2008-09-03 15:01:09 +08:00
|
|
|
static struct kobject *exact_match(dev_t devt, int *partno, void *data)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct gendisk *p = data;
|
2007-05-22 04:08:01 +08:00
|
|
|
|
2008-08-25 18:56:05 +08:00
|
|
|
return &disk_to_dev(p)->kobj;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2007-05-22 04:08:01 +08:00
|
|
|
static int exact_lock(dev_t devt, void *data)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct gendisk *p = data;
|
|
|
|
|
2018-02-26 20:01:38 +08:00
|
|
|
if (!get_disk_and_module(p))
|
2005-04-17 06:20:36 +08:00
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-09-28 14:17:19 +08:00
|
|
|
static void register_disk(struct device *parent, struct gendisk *disk,
|
|
|
|
const struct attribute_group **groups)
|
2010-12-09 03:57:36 +08:00
|
|
|
{
|
|
|
|
struct device *ddev = disk_to_dev(disk);
|
|
|
|
struct block_device *bdev;
|
|
|
|
struct disk_part_iter piter;
|
|
|
|
struct hd_struct *part;
|
|
|
|
int err;
|
|
|
|
|
2016-06-16 09:17:27 +08:00
|
|
|
ddev->parent = parent;
|
2010-12-09 03:57:36 +08:00
|
|
|
|
2013-07-04 06:01:14 +08:00
|
|
|
dev_set_name(ddev, "%s", disk->disk_name);
|
2010-12-09 03:57:36 +08:00
|
|
|
|
|
|
|
/* delay uevents, until we scanned partition table */
|
|
|
|
dev_set_uevent_suppress(ddev, 1);
|
|
|
|
|
2018-09-28 14:17:19 +08:00
|
|
|
if (groups) {
|
|
|
|
WARN_ON(ddev->groups);
|
|
|
|
ddev->groups = groups;
|
|
|
|
}
|
2010-12-09 03:57:36 +08:00
|
|
|
if (device_add(ddev))
|
|
|
|
return;
|
|
|
|
if (!sysfs_deprecated) {
|
|
|
|
err = sysfs_create_link(block_depr, &ddev->kobj,
|
|
|
|
kobject_name(&ddev->kobj));
|
|
|
|
if (err) {
|
|
|
|
device_del(ddev);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2013-02-23 08:34:13 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* avoid probable deadlock caused by allocating memory with
|
|
|
|
* GFP_KERNEL in runtime_resume callback of its all ancestor
|
|
|
|
* devices
|
|
|
|
*/
|
|
|
|
pm_runtime_set_memalloc_noio(ddev, true);
|
|
|
|
|
2010-12-09 03:57:36 +08:00
|
|
|
disk->part0.holder_dir = kobject_create_and_add("holders", &ddev->kobj);
|
|
|
|
disk->slave_dir = kobject_create_and_add("slaves", &ddev->kobj);
|
|
|
|
|
2017-11-03 02:29:53 +08:00
|
|
|
if (disk->flags & GENHD_FL_HIDDEN) {
|
|
|
|
dev_set_uevent_suppress(ddev, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-12-09 03:57:36 +08:00
|
|
|
/* No minors to use for partitions */
|
2011-08-24 02:01:04 +08:00
|
|
|
if (!disk_part_scan_enabled(disk))
|
2010-12-09 03:57:36 +08:00
|
|
|
goto exit;
|
|
|
|
|
|
|
|
/* No such device (e.g., media were just removed) */
|
|
|
|
if (!get_capacity(disk))
|
|
|
|
goto exit;
|
|
|
|
|
|
|
|
bdev = bdget_disk(disk, 0);
|
|
|
|
if (!bdev)
|
|
|
|
goto exit;
|
|
|
|
|
|
|
|
bdev->bd_invalidated = 1;
|
|
|
|
err = blkdev_get(bdev, FMODE_READ, NULL);
|
|
|
|
if (err < 0)
|
|
|
|
goto exit;
|
|
|
|
blkdev_put(bdev, FMODE_READ);
|
|
|
|
|
|
|
|
exit:
|
|
|
|
/* announce disk after possible partitions are created */
|
|
|
|
dev_set_uevent_suppress(ddev, 0);
|
|
|
|
kobject_uevent(&ddev->kobj, KOBJ_ADD);
|
|
|
|
|
|
|
|
/* announce possible partitions */
|
|
|
|
disk_part_iter_init(&piter, disk, 0);
|
|
|
|
while ((part = disk_part_iter_next(&piter)))
|
|
|
|
kobject_uevent(&part_to_dev(part)->kobj, KOBJ_ADD);
|
|
|
|
disk_part_iter_exit(&piter);
|
2017-11-03 02:29:53 +08:00
|
|
|
|
2019-02-20 21:27:05 +08:00
|
|
|
if (disk->queue->backing_dev_info->dev) {
|
|
|
|
err = sysfs_create_link(&ddev->kobj,
|
|
|
|
&disk->queue->backing_dev_info->dev->kobj,
|
|
|
|
"bdi");
|
|
|
|
WARN_ON(err);
|
|
|
|
}
|
2010-12-09 03:57:36 +08:00
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/**
|
2018-01-09 11:01:13 +08:00
|
|
|
* __device_add_disk - add disk information to kernel list
|
2016-06-16 09:17:27 +08:00
|
|
|
* @parent: parent device for the disk
|
2005-04-17 06:20:36 +08:00
|
|
|
* @disk: per-device partitioning information
|
2018-09-28 14:17:19 +08:00
|
|
|
* @groups: Additional per-device sysfs groups
|
2018-01-09 11:01:13 +08:00
|
|
|
* @register_queue: register the queue if set to true
|
2005-04-17 06:20:36 +08:00
|
|
|
*
|
|
|
|
* This function registers the partitioning information in @disk
|
|
|
|
* with the kernel.
|
2008-08-25 18:56:17 +08:00
|
|
|
*
|
|
|
|
* FIXME: error handling
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
2018-01-09 11:01:13 +08:00
|
|
|
static void __device_add_disk(struct device *parent, struct gendisk *disk,
|
2018-09-28 14:17:19 +08:00
|
|
|
const struct attribute_group **groups,
|
2018-01-09 11:01:13 +08:00
|
|
|
bool register_queue)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2008-08-25 18:56:17 +08:00
|
|
|
dev_t devt;
|
2008-05-23 05:21:08 +08:00
|
|
|
int retval;
|
2008-04-30 15:54:32 +08:00
|
|
|
|
2019-09-05 17:51:33 +08:00
|
|
|
/*
|
|
|
|
* The disk queue should now be all set with enough information about
|
|
|
|
* the device for the elevator code to pick an adequate default
|
|
|
|
* elevator if one is needed, that is, for devices requesting queue
|
|
|
|
* registration.
|
|
|
|
*/
|
|
|
|
if (register_queue)
|
|
|
|
elevator_init_mq(disk->queue);
|
|
|
|
|
2008-08-25 18:56:17 +08:00
|
|
|
/* minors == 0 indicates to use ext devt from part0 and should
|
|
|
|
* be accompanied with EXT_DEVT flag. Make sure all
|
|
|
|
* parameters make sense.
|
|
|
|
*/
|
|
|
|
WARN_ON(disk->minors && !(disk->major || disk->first_minor));
|
2017-11-03 02:29:53 +08:00
|
|
|
WARN_ON(!disk->minors &&
|
|
|
|
!(disk->flags & (GENHD_FL_EXT_DEVT | GENHD_FL_HIDDEN)));
|
2008-08-25 18:56:17 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
disk->flags |= GENHD_FL_UP;
|
2008-08-25 18:56:17 +08:00
|
|
|
|
|
|
|
retval = blk_alloc_devt(&disk->part0, &devt);
|
|
|
|
if (retval) {
|
|
|
|
WARN_ON(1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
disk->major = MAJOR(devt);
|
|
|
|
disk->first_minor = MINOR(devt);
|
|
|
|
|
2012-03-02 17:43:28 +08:00
|
|
|
disk_alloc_events(disk);
|
|
|
|
|
2017-11-03 02:29:53 +08:00
|
|
|
if (disk->flags & GENHD_FL_HIDDEN) {
|
|
|
|
/*
|
|
|
|
* Don't let hidden disks show up in /proc/partitions,
|
|
|
|
* and don't bother scanning for partitions either.
|
|
|
|
*/
|
|
|
|
disk->flags |= GENHD_FL_SUPPRESS_PARTITION_INFO;
|
|
|
|
disk->flags |= GENHD_FL_NO_PART_SCAN;
|
|
|
|
} else {
|
2017-10-31 18:38:59 +08:00
|
|
|
int ret;
|
|
|
|
|
2017-11-03 02:29:53 +08:00
|
|
|
/* Register BDI before referencing it from bdev */
|
|
|
|
disk_to_dev(disk)->devt = devt;
|
2017-10-31 18:38:59 +08:00
|
|
|
ret = bdi_register_owner(disk->queue->backing_dev_info,
|
|
|
|
disk_to_dev(disk));
|
|
|
|
WARN_ON(ret);
|
2017-11-03 02:29:53 +08:00
|
|
|
blk_register_region(disk_devt(disk), disk->minors, NULL,
|
|
|
|
exact_match, exact_lock, disk);
|
|
|
|
}
|
2018-09-28 14:17:19 +08:00
|
|
|
register_disk(parent, disk, groups);
|
2018-01-09 11:01:13 +08:00
|
|
|
if (register_queue)
|
|
|
|
blk_register_queue(disk);
|
2008-04-30 15:54:32 +08:00
|
|
|
|
2011-10-19 20:31:07 +08:00
|
|
|
/*
|
|
|
|
* Take an extra ref on queue which will be put on disk_release()
|
|
|
|
* so that it sticks around as long as @disk is there.
|
|
|
|
*/
|
2011-12-14 07:33:38 +08:00
|
|
|
WARN_ON_ONCE(!blk_get_queue(disk->queue));
|
2011-10-19 20:31:07 +08:00
|
|
|
|
implement in-kernel gendisk events handling
Currently, media presence polling for removeable block devices is done
from userland. There are several issues with this.
* Polling is done by periodically opening the device. For SCSI
devices, the command sequence generated by such action involves a
few different commands including TEST_UNIT_READY. This behavior,
while perfectly legal, is different from Windows which only issues
single command, GET_EVENT_STATUS_NOTIFICATION. Unfortunately, some
ATAPI devices lock up after being periodically queried such command
sequences.
* There is no reliable and unintrusive way for a userland program to
tell whether the target device is safe for media presence polling.
For example, polling for media presence during an on-going burning
session can make it fail. The polling program can avoid this by
opening the device with O_EXCL but then it risks making a valid
exclusive user of the device fail w/ -EBUSY.
* Userland polling is unnecessarily heavy and in-kernel implementation
is lighter and better coordinated (workqueue, timer slack).
This patch implements framework for in-kernel disk event handling,
which includes media presence polling.
* bdops->check_events() is added, which supercedes ->media_changed().
It should check whether there's any pending event and return if so.
Currently, two events are defined - DISK_EVENT_MEDIA_CHANGE and
DISK_EVENT_EJECT_REQUEST. ->check_events() is guaranteed not to be
called parallelly.
* gendisk->events and ->async_events are added. These should be
initialized by block driver before passing the device to add_disk().
The former contains the mask of all supported events and the latter
the mask of all events which the device can report without polling.
/sys/block/*/events[_async] export these to userland.
* Kernel parameter block.events_dfl_poll_msecs controls the system
polling interval (default is 0 which means disable) and
/sys/block/*/events_poll_msecs control polling intervals for
individual devices (default is -1 meaning use system setting). Note
that if a device can report all supported events asynchronously and
its polling interval isn't explicitly set, the device won't be
polled regardless of the system polling interval.
* If a device is opened exclusively with write access, event checking
is automatically disabled until all write exclusive accesses are
released.
* There are event 'clearing' events. For example, both of currently
defined events are cleared after the device has been successfully
opened. This information is passed to ->check_events() callback
using @clearing argument as a hint.
* Event checking is always performed from system_nrt_wq and timer
slack is set to 25% for polling.
* Nothing changes for drivers which implement ->media_changed() but
not ->check_events(). Going forward, all drivers will be converted
to ->check_events() and ->media_change() will be dropped.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
2010-12-09 03:57:37 +08:00
|
|
|
disk_add_events(disk);
|
2015-10-22 01:19:49 +08:00
|
|
|
blk_integrity_add(disk);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2018-01-09 11:01:13 +08:00
|
|
|
|
2018-09-28 14:17:19 +08:00
|
|
|
void device_add_disk(struct device *parent, struct gendisk *disk,
|
|
|
|
const struct attribute_group **groups)
|
|
|
|
|
2018-01-09 11:01:13 +08:00
|
|
|
{
|
2018-09-28 14:17:19 +08:00
|
|
|
__device_add_disk(parent, disk, groups, true);
|
2018-01-09 11:01:13 +08:00
|
|
|
}
|
2016-06-16 09:17:27 +08:00
|
|
|
EXPORT_SYMBOL(device_add_disk);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2018-01-09 11:01:13 +08:00
|
|
|
void device_add_disk_no_queue_reg(struct device *parent, struct gendisk *disk)
|
|
|
|
{
|
2018-09-28 14:17:19 +08:00
|
|
|
__device_add_disk(parent, disk, NULL, false);
|
2018-01-09 11:01:13 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(device_add_disk_no_queue_reg);
|
|
|
|
|
2010-12-09 03:57:36 +08:00
|
|
|
void del_gendisk(struct gendisk *disk)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2010-12-09 03:57:36 +08:00
|
|
|
struct disk_part_iter piter;
|
|
|
|
struct hd_struct *part;
|
|
|
|
|
2015-10-22 01:19:49 +08:00
|
|
|
blk_integrity_del(disk);
|
implement in-kernel gendisk events handling
Currently, media presence polling for removeable block devices is done
from userland. There are several issues with this.
* Polling is done by periodically opening the device. For SCSI
devices, the command sequence generated by such action involves a
few different commands including TEST_UNIT_READY. This behavior,
while perfectly legal, is different from Windows which only issues
single command, GET_EVENT_STATUS_NOTIFICATION. Unfortunately, some
ATAPI devices lock up after being periodically queried such command
sequences.
* There is no reliable and unintrusive way for a userland program to
tell whether the target device is safe for media presence polling.
For example, polling for media presence during an on-going burning
session can make it fail. The polling program can avoid this by
opening the device with O_EXCL but then it risks making a valid
exclusive user of the device fail w/ -EBUSY.
* Userland polling is unnecessarily heavy and in-kernel implementation
is lighter and better coordinated (workqueue, timer slack).
This patch implements framework for in-kernel disk event handling,
which includes media presence polling.
* bdops->check_events() is added, which supercedes ->media_changed().
It should check whether there's any pending event and return if so.
Currently, two events are defined - DISK_EVENT_MEDIA_CHANGE and
DISK_EVENT_EJECT_REQUEST. ->check_events() is guaranteed not to be
called parallelly.
* gendisk->events and ->async_events are added. These should be
initialized by block driver before passing the device to add_disk().
The former contains the mask of all supported events and the latter
the mask of all events which the device can report without polling.
/sys/block/*/events[_async] export these to userland.
* Kernel parameter block.events_dfl_poll_msecs controls the system
polling interval (default is 0 which means disable) and
/sys/block/*/events_poll_msecs control polling intervals for
individual devices (default is -1 meaning use system setting). Note
that if a device can report all supported events asynchronously and
its polling interval isn't explicitly set, the device won't be
polled regardless of the system polling interval.
* If a device is opened exclusively with write access, event checking
is automatically disabled until all write exclusive accesses are
released.
* There are event 'clearing' events. For example, both of currently
defined events are cleared after the device has been successfully
opened. This information is passed to ->check_events() callback
using @clearing argument as a hint.
* Event checking is always performed from system_nrt_wq and timer
slack is set to 25% for polling.
* Nothing changes for drivers which implement ->media_changed() but
not ->check_events(). Going forward, all drivers will be converted
to ->check_events() and ->media_change() will be dropped.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
2010-12-09 03:57:37 +08:00
|
|
|
disk_del_events(disk);
|
|
|
|
|
2018-02-26 20:01:41 +08:00
|
|
|
/*
|
|
|
|
* Block lookups of the disk until all bdevs are unhashed and the
|
|
|
|
* disk is marked as dead (GENHD_FL_UP cleared).
|
|
|
|
*/
|
|
|
|
down_write(&disk->lookup_sem);
|
2010-12-09 03:57:36 +08:00
|
|
|
/* invalidate stuff */
|
|
|
|
disk_part_iter_init(&piter, disk,
|
|
|
|
DISK_PITER_INCL_EMPTY | DISK_PITER_REVERSE);
|
|
|
|
while ((part = disk_part_iter_next(&piter))) {
|
|
|
|
invalidate_partition(disk, part->partno);
|
2017-02-22 01:09:46 +08:00
|
|
|
bdev_unhash_inode(part_devt(part));
|
2010-12-09 03:57:36 +08:00
|
|
|
delete_partition(disk, part->partno);
|
|
|
|
}
|
|
|
|
disk_part_iter_exit(&piter);
|
|
|
|
|
|
|
|
invalidate_partition(disk, 0);
|
2017-02-22 01:09:47 +08:00
|
|
|
bdev_unhash_inode(disk_devt(disk));
|
2010-12-09 03:57:36 +08:00
|
|
|
set_capacity(disk, 0);
|
|
|
|
disk->flags &= ~GENHD_FL_UP;
|
2018-02-26 20:01:41 +08:00
|
|
|
up_write(&disk->lookup_sem);
|
2010-12-09 03:57:36 +08:00
|
|
|
|
2017-11-03 02:29:53 +08:00
|
|
|
if (!(disk->flags & GENHD_FL_HIDDEN))
|
|
|
|
sysfs_remove_link(&disk_to_dev(disk)->kobj, "bdi");
|
2017-03-09 00:48:33 +08:00
|
|
|
if (disk->queue) {
|
|
|
|
/*
|
|
|
|
* Unregister bdi before releasing device numbers (as they can
|
|
|
|
* get reused and we'd get clashes in sysfs).
|
|
|
|
*/
|
2018-01-10 09:46:49 +08:00
|
|
|
if (!(disk->flags & GENHD_FL_HIDDEN))
|
|
|
|
bdi_unregister(disk->queue->backing_dev_info);
|
2017-03-09 00:48:33 +08:00
|
|
|
blk_unregister_queue(disk);
|
|
|
|
} else {
|
|
|
|
WARN_ON(1);
|
|
|
|
}
|
2010-12-09 03:57:36 +08:00
|
|
|
|
2017-11-10 00:57:06 +08:00
|
|
|
if (!(disk->flags & GENHD_FL_HIDDEN))
|
2017-11-03 02:29:53 +08:00
|
|
|
blk_unregister_region(disk_devt(disk), disk->minors);
|
2019-04-02 20:06:34 +08:00
|
|
|
/*
|
|
|
|
* Remove gendisk pointer from idr so that it cannot be looked up
|
|
|
|
* while RCU period before freeing gendisk is running to prevent
|
|
|
|
* use-after-free issues. Note that the device number stays
|
|
|
|
* "in-use" until we really free the gendisk.
|
|
|
|
*/
|
|
|
|
blk_invalidate_devt(disk_devt(disk));
|
2010-12-09 03:57:36 +08:00
|
|
|
|
|
|
|
kobject_put(disk->part0.holder_dir);
|
|
|
|
kobject_put(disk->slave_dir);
|
|
|
|
|
|
|
|
part_stat_set_all(&disk->part0, 0);
|
|
|
|
disk->part0.stamp = 0;
|
|
|
|
if (!sysfs_deprecated)
|
|
|
|
sysfs_remove_link(block_depr, dev_name(disk_to_dev(disk)));
|
2013-02-23 08:34:13 +08:00
|
|
|
pm_runtime_set_memalloc_noio(disk_to_dev(disk), false);
|
2010-12-09 03:57:36 +08:00
|
|
|
device_del(disk_to_dev(disk));
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2010-12-09 03:57:36 +08:00
|
|
|
EXPORT_SYMBOL(del_gendisk);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2016-01-10 00:36:51 +08:00
|
|
|
/* sysfs access to bad-blocks list. */
|
|
|
|
static ssize_t disk_badblocks_show(struct device *dev,
|
|
|
|
struct device_attribute *attr,
|
|
|
|
char *page)
|
|
|
|
{
|
|
|
|
struct gendisk *disk = dev_to_disk(dev);
|
|
|
|
|
|
|
|
if (!disk->bb)
|
|
|
|
return sprintf(page, "\n");
|
|
|
|
|
|
|
|
return badblocks_show(disk->bb, page, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t disk_badblocks_store(struct device *dev,
|
|
|
|
struct device_attribute *attr,
|
|
|
|
const char *page, size_t len)
|
|
|
|
{
|
|
|
|
struct gendisk *disk = dev_to_disk(dev);
|
|
|
|
|
|
|
|
if (!disk->bb)
|
|
|
|
return -ENXIO;
|
|
|
|
|
|
|
|
return badblocks_store(disk->bb, page, len, 0);
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/**
|
|
|
|
* get_gendisk - get partitioning information for a given device
|
2008-08-20 02:13:11 +08:00
|
|
|
* @devt: device to get partitioning information for
|
2008-10-16 13:46:23 +08:00
|
|
|
* @partno: returned partition index
|
2005-04-17 06:20:36 +08:00
|
|
|
*
|
|
|
|
* This function gets the structure containing partitioning
|
2008-08-20 02:13:11 +08:00
|
|
|
* information for the given device @devt.
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
2008-09-03 15:01:09 +08:00
|
|
|
struct gendisk *get_gendisk(dev_t devt, int *partno)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2008-08-25 18:47:22 +08:00
|
|
|
struct gendisk *disk = NULL;
|
|
|
|
|
|
|
|
if (MAJOR(devt) != BLOCK_EXT_MAJOR) {
|
|
|
|
struct kobject *kobj;
|
|
|
|
|
|
|
|
kobj = kobj_lookup(bdev_map, devt, partno);
|
|
|
|
if (kobj)
|
|
|
|
disk = dev_to_disk(kobj_to_dev(kobj));
|
|
|
|
} else {
|
|
|
|
struct hd_struct *part;
|
|
|
|
|
2015-06-11 11:47:14 +08:00
|
|
|
spin_lock_bh(&ext_devt_lock);
|
2008-08-25 18:47:25 +08:00
|
|
|
part = idr_find(&ext_devt_idr, blk_mangle_minor(MINOR(devt)));
|
2018-02-26 20:01:38 +08:00
|
|
|
if (part && get_disk_and_module(part_to_disk(part))) {
|
2008-08-25 18:47:22 +08:00
|
|
|
*partno = part->partno;
|
|
|
|
disk = part_to_disk(part);
|
|
|
|
}
|
2015-06-11 11:47:14 +08:00
|
|
|
spin_unlock_bh(&ext_devt_lock);
|
2008-08-25 18:47:22 +08:00
|
|
|
}
|
2007-05-22 04:08:01 +08:00
|
|
|
|
2018-02-26 20:01:41 +08:00
|
|
|
if (!disk)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Synchronize with del_gendisk() to not return disk that is being
|
|
|
|
* destroyed.
|
|
|
|
*/
|
|
|
|
down_read(&disk->lookup_sem);
|
|
|
|
if (unlikely((disk->flags & GENHD_FL_HIDDEN) ||
|
|
|
|
!(disk->flags & GENHD_FL_UP))) {
|
|
|
|
up_read(&disk->lookup_sem);
|
2018-02-26 20:01:39 +08:00
|
|
|
put_disk_and_module(disk);
|
2017-11-03 02:29:53 +08:00
|
|
|
disk = NULL;
|
2018-02-26 20:01:41 +08:00
|
|
|
} else {
|
|
|
|
up_read(&disk->lookup_sem);
|
2017-11-03 02:29:53 +08:00
|
|
|
}
|
2008-08-25 18:47:22 +08:00
|
|
|
return disk;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2010-04-15 14:54:59 +08:00
|
|
|
EXPORT_SYMBOL(get_gendisk);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2008-09-03 15:01:48 +08:00
|
|
|
/**
|
|
|
|
* bdget_disk - do bdget() by gendisk and partition number
|
|
|
|
* @disk: gendisk of interest
|
|
|
|
* @partno: partition number
|
|
|
|
*
|
|
|
|
* Find partition @partno from @disk, do bdget() on it.
|
|
|
|
*
|
|
|
|
* CONTEXT:
|
|
|
|
* Don't care.
|
|
|
|
*
|
|
|
|
* RETURNS:
|
|
|
|
* Resulting block_device on success, NULL on failure.
|
|
|
|
*/
|
2008-08-28 15:27:42 +08:00
|
|
|
struct block_device *bdget_disk(struct gendisk *disk, int partno)
|
2008-09-03 15:01:48 +08:00
|
|
|
{
|
2008-08-29 15:01:47 +08:00
|
|
|
struct hd_struct *part;
|
|
|
|
struct block_device *bdev = NULL;
|
2008-09-03 15:01:48 +08:00
|
|
|
|
2008-08-29 15:01:47 +08:00
|
|
|
part = disk_get_part(disk, partno);
|
2008-08-29 17:41:51 +08:00
|
|
|
if (part)
|
2008-08-29 15:01:47 +08:00
|
|
|
bdev = bdget(part_devt(part));
|
|
|
|
disk_put_part(part);
|
2008-09-03 15:01:48 +08:00
|
|
|
|
2008-08-29 15:01:47 +08:00
|
|
|
return bdev;
|
2008-09-03 15:01:48 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(bdget_disk);
|
|
|
|
|
2008-05-23 05:21:08 +08:00
|
|
|
/*
|
|
|
|
* print a full list of all partitions - intended for places where the root
|
|
|
|
* filesystem can't be mounted and thus to give the victim some idea of what
|
|
|
|
* went wrong
|
|
|
|
*/
|
|
|
|
void __init printk_all_partitions(void)
|
|
|
|
{
|
2008-09-03 14:57:12 +08:00
|
|
|
struct class_dev_iter iter;
|
|
|
|
struct device *dev;
|
|
|
|
|
|
|
|
class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
|
|
|
|
while ((dev = class_dev_iter_next(&iter))) {
|
|
|
|
struct gendisk *disk = dev_to_disk(dev);
|
2008-09-03 15:03:02 +08:00
|
|
|
struct disk_part_iter piter;
|
|
|
|
struct hd_struct *part;
|
2008-08-25 18:47:23 +08:00
|
|
|
char name_buf[BDEVNAME_SIZE];
|
|
|
|
char devt_buf[BDEVT_SIZE];
|
2008-09-03 14:57:12 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Don't show empty devices or things that have been
|
2011-03-31 09:57:33 +08:00
|
|
|
* suppressed
|
2008-09-03 14:57:12 +08:00
|
|
|
*/
|
|
|
|
if (get_capacity(disk) == 0 ||
|
|
|
|
(disk->flags & GENHD_FL_SUPPRESS_PARTITION_INFO))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Note, unlike /proc/partitions, I am showing the
|
|
|
|
* numbers in hex - the same format as the root=
|
|
|
|
* option takes.
|
|
|
|
*/
|
2008-08-25 18:56:14 +08:00
|
|
|
disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
|
|
|
|
while ((part = disk_part_iter_next(&piter))) {
|
|
|
|
bool is_part0 = part == &disk->part0;
|
2008-09-03 14:57:12 +08:00
|
|
|
|
2010-09-01 04:47:07 +08:00
|
|
|
printk("%s%s %10llu %s %s", is_part0 ? "" : " ",
|
2008-08-25 18:47:23 +08:00
|
|
|
bdevt_str(part_devt(part), devt_buf),
|
2012-08-01 18:24:18 +08:00
|
|
|
(unsigned long long)part_nr_sects_read(part) >> 1
|
|
|
|
, disk_name(disk, part->partno, name_buf),
|
2012-11-09 08:12:25 +08:00
|
|
|
part->info ? part->info->uuid : "");
|
2008-08-25 18:56:14 +08:00
|
|
|
if (is_part0) {
|
2016-06-16 10:43:07 +08:00
|
|
|
if (dev->parent && dev->parent->driver)
|
2008-08-25 18:56:14 +08:00
|
|
|
printk(" driver: %s\n",
|
2016-06-16 10:43:07 +08:00
|
|
|
dev->parent->driver->name);
|
2008-08-25 18:56:14 +08:00
|
|
|
else
|
|
|
|
printk(" (driver?)\n");
|
|
|
|
} else
|
|
|
|
printk("\n");
|
|
|
|
}
|
2008-09-03 15:03:02 +08:00
|
|
|
disk_part_iter_exit(&piter);
|
2008-09-03 14:57:12 +08:00
|
|
|
}
|
|
|
|
class_dev_iter_exit(&iter);
|
2007-05-09 17:33:24 +08:00
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
#ifdef CONFIG_PROC_FS
|
|
|
|
/* iterator */
|
2008-09-03 14:57:12 +08:00
|
|
|
static void *disk_seqf_start(struct seq_file *seqf, loff_t *pos)
|
2008-05-23 05:21:08 +08:00
|
|
|
{
|
2008-09-03 14:57:12 +08:00
|
|
|
loff_t skip = *pos;
|
|
|
|
struct class_dev_iter *iter;
|
|
|
|
struct device *dev;
|
2008-05-23 05:21:08 +08:00
|
|
|
|
2008-08-28 15:27:42 +08:00
|
|
|
iter = kmalloc(sizeof(*iter), GFP_KERNEL);
|
2008-09-03 14:57:12 +08:00
|
|
|
if (!iter)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
|
|
|
seqf->private = iter;
|
|
|
|
class_dev_iter_init(iter, &block_class, NULL, &disk_type);
|
|
|
|
do {
|
|
|
|
dev = class_dev_iter_next(iter);
|
|
|
|
if (!dev)
|
|
|
|
return NULL;
|
|
|
|
} while (skip--);
|
|
|
|
|
|
|
|
return dev_to_disk(dev);
|
2008-05-23 05:21:08 +08:00
|
|
|
}
|
|
|
|
|
2008-09-03 14:57:12 +08:00
|
|
|
static void *disk_seqf_next(struct seq_file *seqf, void *v, loff_t *pos)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2007-05-22 04:08:01 +08:00
|
|
|
struct device *dev;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2008-09-03 14:57:12 +08:00
|
|
|
(*pos)++;
|
|
|
|
dev = class_dev_iter_next(seqf->private);
|
2008-09-03 14:53:37 +08:00
|
|
|
if (dev)
|
2008-05-23 05:21:08 +08:00
|
|
|
return dev_to_disk(dev);
|
2008-09-03 14:53:37 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-09-03 14:57:12 +08:00
|
|
|
static void disk_seqf_stop(struct seq_file *seqf, void *v)
|
2008-05-23 05:21:08 +08:00
|
|
|
{
|
2008-09-03 14:57:12 +08:00
|
|
|
struct class_dev_iter *iter = seqf->private;
|
2008-05-23 05:21:08 +08:00
|
|
|
|
2008-09-03 14:57:12 +08:00
|
|
|
/* stop is called even after start failed :-( */
|
|
|
|
if (iter) {
|
|
|
|
class_dev_iter_exit(iter);
|
|
|
|
kfree(iter);
|
2016-07-29 16:40:31 +08:00
|
|
|
seqf->private = NULL;
|
2008-08-16 20:30:30 +08:00
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2008-09-03 14:57:12 +08:00
|
|
|
static void *show_partition_start(struct seq_file *seqf, loff_t *pos)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
block: Don't use static to define "void *p" in show_partition_start()
I met a odd prblem:read /proc/partitions may return zero.
I wrote a file test.c:
int main()
{
char buff[4096];
int ret;
int fd;
printf("pid=%d\n",getpid());
while (1) {
fd = open("/proc/partitions", O_RDONLY);
if (fd < 0) {
printf("open error %s\n", strerror(errno));
return 0;
}
ret = read(fd, buff, 4096);
if (ret <= 0)
printf("ret=%d, %s, %ld\n", ret,
strerror(errno), lseek(fd,0,SEEK_CUR));
close(fd);
}
exit(0);
}
You can reproduce by:
1:while true;do cat /proc/partitions > /dev/null ;done
2:./test
I reviewed the code and found:
>> static void *show_partition_start(struct seq_file *seqf, loff_t *pos)
>> {
>> static void *p;
>>
>> p = disk_seqf_start(seqf, pos);
>> if (!IS_ERR_OR_NULL(p) && !*pos)
>> seq_puts(seqf, "major minor #blocks name\n\n");
>> return p;
>> }
test cat /proc/partitions
p = disk_seqf_start()(Not NULL)
p = disk_seqf_start()(NULL because pos)
if (!IS_ERR_OR_NULL(p) && !*pos)
Signed-off-by: Jianpeng Ma <majianpeng@gmail.com>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2012-08-03 16:42:00 +08:00
|
|
|
void *p;
|
2008-09-03 14:57:12 +08:00
|
|
|
|
|
|
|
p = disk_seqf_start(seqf, pos);
|
2010-12-17 15:58:36 +08:00
|
|
|
if (!IS_ERR_OR_NULL(p) && !*pos)
|
2008-09-03 14:57:12 +08:00
|
|
|
seq_puts(seqf, "major minor #blocks name\n\n");
|
|
|
|
return p;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2008-09-03 15:01:09 +08:00
|
|
|
static int show_partition(struct seq_file *seqf, void *v)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct gendisk *sgp = v;
|
2008-09-03 15:03:02 +08:00
|
|
|
struct disk_part_iter piter;
|
|
|
|
struct hd_struct *part;
|
2005-04-17 06:20:36 +08:00
|
|
|
char buf[BDEVNAME_SIZE];
|
|
|
|
|
|
|
|
/* Don't show non-partitionable removeable devices or empty devices */
|
2011-08-24 02:01:04 +08:00
|
|
|
if (!get_capacity(sgp) || (!disk_max_parts(sgp) &&
|
2008-09-03 15:01:48 +08:00
|
|
|
(sgp->flags & GENHD_FL_REMOVABLE)))
|
2005-04-17 06:20:36 +08:00
|
|
|
return 0;
|
|
|
|
if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* show the full disk and all non-0 size partitions of it */
|
2008-08-25 18:56:14 +08:00
|
|
|
disk_part_iter_init(&piter, sgp, DISK_PITER_INCL_PART0);
|
2008-09-03 15:03:02 +08:00
|
|
|
while ((part = disk_part_iter_next(&piter)))
|
2008-08-25 18:47:23 +08:00
|
|
|
seq_printf(seqf, "%4d %7d %10llu %s\n",
|
2008-09-03 15:01:48 +08:00
|
|
|
MAJOR(part_devt(part)), MINOR(part_devt(part)),
|
2012-08-01 18:24:18 +08:00
|
|
|
(unsigned long long)part_nr_sects_read(part) >> 1,
|
2008-09-03 15:01:48 +08:00
|
|
|
disk_name(sgp, part->partno, buf));
|
2008-09-03 15:03:02 +08:00
|
|
|
disk_part_iter_exit(&piter);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-10-05 03:53:21 +08:00
|
|
|
static const struct seq_operations partitions_op = {
|
2008-09-03 14:57:12 +08:00
|
|
|
.start = show_partition_start,
|
|
|
|
.next = disk_seqf_next,
|
|
|
|
.stop = disk_seqf_stop,
|
2007-05-22 04:08:01 +08:00
|
|
|
.show = show_partition
|
2005-04-17 06:20:36 +08:00
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2008-09-03 15:01:09 +08:00
|
|
|
static struct kobject *base_probe(dev_t devt, int *partno, void *data)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2007-05-22 04:08:01 +08:00
|
|
|
if (request_module("block-major-%d-%d", MAJOR(devt), MINOR(devt)) > 0)
|
2005-04-17 06:20:36 +08:00
|
|
|
/* Make old-style 2.4 aliases work */
|
2007-05-22 04:08:01 +08:00
|
|
|
request_module("block-major-%d", MAJOR(devt));
|
2005-04-17 06:20:36 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __init genhd_device_init(void)
|
|
|
|
{
|
2008-04-22 01:51:07 +08:00
|
|
|
int error;
|
|
|
|
|
|
|
|
block_class.dev_kobj = sysfs_dev_block_kobj;
|
|
|
|
error = class_register(&block_class);
|
2008-03-12 08:13:15 +08:00
|
|
|
if (unlikely(error))
|
|
|
|
return error;
|
2007-05-22 04:08:01 +08:00
|
|
|
bdev_map = kobj_map_init(base_probe, &block_class_lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
blk_dev_init();
|
2007-05-22 04:08:01 +08:00
|
|
|
|
block: fix boot failure with CONFIG_DEBUG_BLOCK_EXT_DEVT=y and nash
We run into system boot failure with kernel 2.6.28-rc. We found it on a
couple of machines, including T61 notebook, nehalem machine, and another
HPC NX6325 notebook. All the machines use FedoraCore 8 or FedoraCore 9.
With kernel prior to 2.6.28-rc, system boot doesn't fail.
I debug it and locate the root cause. Pls. see
http://bugzilla.kernel.org/show_bug.cgi?id=11899
https://bugzilla.redhat.com/show_bug.cgi?id=471517
As a matter of fact, there are 2 bugs.
1)root=/dev/sda1, system boot randomly fails. Mostly, boot for 5 times
and fails once. nash has a bug. Some of its functions misuse return
value 0. Sometimes, 0 means timeout and no uevent available. Sometimes,
0 means nash gets an uevent, but the uevent isn't block-related (for
exmaple, usb). If by coincidence, kernel tells nash that uevents are
available, but kernel also set timeout, nash might stops collecting
other uevents in queue if current uevent isn't block-related. I work
out a patch for nash to fix it.
http://bugzilla.kernel.org/attachment.cgi?id=18858
2) root=LABEL=/, system always can't boot. initrd init reports
switchroot fails. Here is an executation branch of nash when booting:
(1) nash read /sys/block/sda/dev; Assume major is 8 (on my desktop)
(2) nash query /proc/devices with the major number; It found line
"8 sd";
(3) nash use 'sd' to search its own probe table to find device (DISK)
type for the device and add it to its own list;
(4) Later on, it probes all devices in its list to get filesystem
labels; scsi register "8 sd" always.
When major is 259, nash fails to find the device(DISK) type. I enables
CONFIG_DEBUG_BLOCK_EXT_DEVT=y when compiling kernel, so 259 is picked up
for device /dev/sda1, which causes nash to fail to find device (DISK)
type.
To fixing issue 2), I create a patch for nash and another patch for
kernel.
http://bugzilla.kernel.org/attachment.cgi?id=18859
http://bugzilla.kernel.org/attachment.cgi?id=18837
Below is the patch for kernel 2.6.28-rc4. It registers blkext, a new
block device in proc/devices.
With 2 patches on nash and 1 patch on kernel, I boot my machines for
dozens of times without failure.
Signed-off-by Zhang Yanmin <yanmin.zhang@linux.intel.com>
Acked-by: Tejun Heo <tj@kernel.org>
Signed-off-by: Jens Axboe <jens.axboe@oracle.com>
2008-11-14 15:26:30 +08:00
|
|
|
register_blkdev(BLOCK_EXT_MAJOR, "blkext");
|
|
|
|
|
2007-05-22 04:08:01 +08:00
|
|
|
/* create top-level block dir */
|
2010-09-08 22:54:17 +08:00
|
|
|
if (!sysfs_deprecated)
|
|
|
|
block_depr = kobject_create_and_add("block", NULL);
|
2007-11-07 02:36:58 +08:00
|
|
|
return 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
subsys_initcall(genhd_device_init);
|
|
|
|
|
2007-05-22 04:08:01 +08:00
|
|
|
static ssize_t disk_range_show(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *buf)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2007-05-22 04:08:01 +08:00
|
|
|
struct gendisk *disk = dev_to_disk(dev);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-05-22 04:08:01 +08:00
|
|
|
return sprintf(buf, "%d\n", disk->minors);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2008-08-25 18:47:23 +08:00
|
|
|
static ssize_t disk_ext_range_show(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *buf)
|
|
|
|
{
|
|
|
|
struct gendisk *disk = dev_to_disk(dev);
|
|
|
|
|
2008-09-03 15:06:42 +08:00
|
|
|
return sprintf(buf, "%d\n", disk_max_parts(disk));
|
2008-08-25 18:47:23 +08:00
|
|
|
}
|
|
|
|
|
2007-05-22 04:08:01 +08:00
|
|
|
static ssize_t disk_removable_show(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *buf)
|
2005-10-01 20:49:43 +08:00
|
|
|
{
|
2007-05-22 04:08:01 +08:00
|
|
|
struct gendisk *disk = dev_to_disk(dev);
|
2005-10-01 20:49:43 +08:00
|
|
|
|
2007-05-22 04:08:01 +08:00
|
|
|
return sprintf(buf, "%d\n",
|
|
|
|
(disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
|
2005-10-01 20:49:43 +08:00
|
|
|
}
|
|
|
|
|
2017-11-03 02:29:53 +08:00
|
|
|
static ssize_t disk_hidden_show(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *buf)
|
|
|
|
{
|
|
|
|
struct gendisk *disk = dev_to_disk(dev);
|
|
|
|
|
|
|
|
return sprintf(buf, "%d\n",
|
|
|
|
(disk->flags & GENHD_FL_HIDDEN ? 1 : 0));
|
|
|
|
}
|
|
|
|
|
2008-06-13 15:41:00 +08:00
|
|
|
static ssize_t disk_ro_show(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *buf)
|
|
|
|
{
|
|
|
|
struct gendisk *disk = dev_to_disk(dev);
|
|
|
|
|
2008-08-25 18:56:10 +08:00
|
|
|
return sprintf(buf, "%d\n", get_disk_ro(disk) ? 1 : 0);
|
2008-06-13 15:41:00 +08:00
|
|
|
}
|
|
|
|
|
2007-05-22 04:08:01 +08:00
|
|
|
static ssize_t disk_capability_show(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *buf)
|
2007-05-24 04:57:38 +08:00
|
|
|
{
|
2007-05-22 04:08:01 +08:00
|
|
|
struct gendisk *disk = dev_to_disk(dev);
|
|
|
|
|
|
|
|
return sprintf(buf, "%x\n", disk->flags);
|
2007-05-24 04:57:38 +08:00
|
|
|
}
|
2007-05-22 04:08:01 +08:00
|
|
|
|
2009-05-23 05:17:53 +08:00
|
|
|
static ssize_t disk_alignment_offset_show(struct device *dev,
|
|
|
|
struct device_attribute *attr,
|
|
|
|
char *buf)
|
|
|
|
{
|
|
|
|
struct gendisk *disk = dev_to_disk(dev);
|
|
|
|
|
|
|
|
return sprintf(buf, "%d\n", queue_alignment_offset(disk->queue));
|
|
|
|
}
|
|
|
|
|
2009-11-10 18:50:21 +08:00
|
|
|
static ssize_t disk_discard_alignment_show(struct device *dev,
|
|
|
|
struct device_attribute *attr,
|
|
|
|
char *buf)
|
|
|
|
{
|
|
|
|
struct gendisk *disk = dev_to_disk(dev);
|
|
|
|
|
2010-01-11 16:21:48 +08:00
|
|
|
return sprintf(buf, "%d\n", queue_discard_alignment(disk->queue));
|
2009-11-10 18:50:21 +08:00
|
|
|
}
|
|
|
|
|
2018-05-25 03:38:59 +08:00
|
|
|
static DEVICE_ATTR(range, 0444, disk_range_show, NULL);
|
|
|
|
static DEVICE_ATTR(ext_range, 0444, disk_ext_range_show, NULL);
|
|
|
|
static DEVICE_ATTR(removable, 0444, disk_removable_show, NULL);
|
|
|
|
static DEVICE_ATTR(hidden, 0444, disk_hidden_show, NULL);
|
|
|
|
static DEVICE_ATTR(ro, 0444, disk_ro_show, NULL);
|
|
|
|
static DEVICE_ATTR(size, 0444, part_size_show, NULL);
|
|
|
|
static DEVICE_ATTR(alignment_offset, 0444, disk_alignment_offset_show, NULL);
|
|
|
|
static DEVICE_ATTR(discard_alignment, 0444, disk_discard_alignment_show, NULL);
|
|
|
|
static DEVICE_ATTR(capability, 0444, disk_capability_show, NULL);
|
|
|
|
static DEVICE_ATTR(stat, 0444, part_stat_show, NULL);
|
|
|
|
static DEVICE_ATTR(inflight, 0444, part_inflight_show, NULL);
|
|
|
|
static DEVICE_ATTR(badblocks, 0644, disk_badblocks_show, disk_badblocks_store);
|
2006-12-08 18:39:46 +08:00
|
|
|
#ifdef CONFIG_FAIL_MAKE_REQUEST
|
2007-05-22 04:08:01 +08:00
|
|
|
static struct device_attribute dev_attr_fail =
|
2018-05-25 03:38:59 +08:00
|
|
|
__ATTR(make-it-fail, 0644, part_fail_show, part_fail_store);
|
2006-12-08 18:39:46 +08:00
|
|
|
#endif
|
2008-09-14 20:56:33 +08:00
|
|
|
#ifdef CONFIG_FAIL_IO_TIMEOUT
|
|
|
|
static struct device_attribute dev_attr_fail_timeout =
|
2018-05-25 03:38:59 +08:00
|
|
|
__ATTR(io-timeout-fail, 0644, part_timeout_show, part_timeout_store);
|
2008-09-14 20:56:33 +08:00
|
|
|
#endif
|
2007-05-22 04:08:01 +08:00
|
|
|
|
|
|
|
static struct attribute *disk_attrs[] = {
|
|
|
|
&dev_attr_range.attr,
|
2008-08-25 18:47:23 +08:00
|
|
|
&dev_attr_ext_range.attr,
|
2007-05-22 04:08:01 +08:00
|
|
|
&dev_attr_removable.attr,
|
2017-11-03 02:29:53 +08:00
|
|
|
&dev_attr_hidden.attr,
|
2008-06-13 15:41:00 +08:00
|
|
|
&dev_attr_ro.attr,
|
2007-05-22 04:08:01 +08:00
|
|
|
&dev_attr_size.attr,
|
2009-05-23 05:17:53 +08:00
|
|
|
&dev_attr_alignment_offset.attr,
|
2009-11-10 18:50:21 +08:00
|
|
|
&dev_attr_discard_alignment.attr,
|
2007-05-22 04:08:01 +08:00
|
|
|
&dev_attr_capability.attr,
|
|
|
|
&dev_attr_stat.attr,
|
block: Seperate read and write statistics of in_flight requests v2
Commit a9327cac440be4d8333bba975cbbf76045096275 added seperate read
and write statistics of in_flight requests. And exported the number
of read and write requests in progress seperately through sysfs.
But Corrado Zoccolo <czoccolo@gmail.com> reported getting strange
output from "iostat -kx 2". Global values for service time and
utilization were garbage. For interval values, utilization was always
100%, and service time is higher than normal.
So this was reverted by commit 0f78ab9899e9d6acb09d5465def618704255963b
The problem was in part_round_stats_single(), I missed the following:
if (now == part->stamp)
return;
- if (part->in_flight) {
+ if (part_in_flight(part)) {
__part_stat_add(cpu, part, time_in_queue,
part_in_flight(part) * (now - part->stamp));
__part_stat_add(cpu, part, io_ticks, (now - part->stamp));
With this chunk included, the reported regression gets fixed.
Signed-off-by: Nikanth Karthikesan <knikanth@suse.de>
--
Signed-off-by: Jens Axboe <jens.axboe@oracle.com>
2009-10-07 02:16:55 +08:00
|
|
|
&dev_attr_inflight.attr,
|
2016-01-10 00:36:51 +08:00
|
|
|
&dev_attr_badblocks.attr,
|
2007-05-22 04:08:01 +08:00
|
|
|
#ifdef CONFIG_FAIL_MAKE_REQUEST
|
|
|
|
&dev_attr_fail.attr,
|
2008-09-14 20:56:33 +08:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_FAIL_IO_TIMEOUT
|
|
|
|
&dev_attr_fail_timeout.attr,
|
2007-05-22 04:08:01 +08:00
|
|
|
#endif
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2017-04-28 05:46:26 +08:00
|
|
|
static umode_t disk_visible(struct kobject *kobj, struct attribute *a, int n)
|
|
|
|
{
|
|
|
|
struct device *dev = container_of(kobj, typeof(*dev), kobj);
|
|
|
|
struct gendisk *disk = dev_to_disk(dev);
|
|
|
|
|
|
|
|
if (a == &dev_attr_badblocks.attr && !disk->bb)
|
|
|
|
return 0;
|
|
|
|
return a->mode;
|
|
|
|
}
|
|
|
|
|
2007-05-22 04:08:01 +08:00
|
|
|
static struct attribute_group disk_attr_group = {
|
|
|
|
.attrs = disk_attrs,
|
2017-04-28 05:46:26 +08:00
|
|
|
.is_visible = disk_visible,
|
2007-05-22 04:08:01 +08:00
|
|
|
};
|
|
|
|
|
2009-06-25 01:06:31 +08:00
|
|
|
static const struct attribute_group *disk_attr_groups[] = {
|
2007-05-22 04:08:01 +08:00
|
|
|
&disk_attr_group,
|
|
|
|
NULL
|
2005-04-17 06:20:36 +08:00
|
|
|
};
|
|
|
|
|
2008-08-25 18:56:15 +08:00
|
|
|
/**
|
|
|
|
* disk_replace_part_tbl - replace disk->part_tbl in RCU-safe way
|
|
|
|
* @disk: disk to replace part_tbl for
|
|
|
|
* @new_ptbl: new part_tbl to install
|
|
|
|
*
|
|
|
|
* Replace disk->part_tbl with @new_ptbl in RCU-safe way. The
|
|
|
|
* original ptbl is freed using RCU callback.
|
|
|
|
*
|
|
|
|
* LOCKING:
|
2017-08-18 07:23:06 +08:00
|
|
|
* Matching bd_mutex locked or the caller is the only user of @disk.
|
2008-08-25 18:56:15 +08:00
|
|
|
*/
|
|
|
|
static void disk_replace_part_tbl(struct gendisk *disk,
|
|
|
|
struct disk_part_tbl *new_ptbl)
|
|
|
|
{
|
2017-08-18 07:23:06 +08:00
|
|
|
struct disk_part_tbl *old_ptbl =
|
|
|
|
rcu_dereference_protected(disk->part_tbl, 1);
|
2008-08-25 18:56:15 +08:00
|
|
|
|
|
|
|
rcu_assign_pointer(disk->part_tbl, new_ptbl);
|
2008-10-24 18:52:42 +08:00
|
|
|
|
|
|
|
if (old_ptbl) {
|
|
|
|
rcu_assign_pointer(old_ptbl->last_lookup, NULL);
|
2011-03-18 11:42:58 +08:00
|
|
|
kfree_rcu(old_ptbl, rcu_head);
|
2008-10-24 18:52:42 +08:00
|
|
|
}
|
2008-08-25 18:56:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* disk_expand_part_tbl - expand disk->part_tbl
|
|
|
|
* @disk: disk to expand part_tbl for
|
|
|
|
* @partno: expand such that this partno can fit in
|
|
|
|
*
|
|
|
|
* Expand disk->part_tbl such that @partno can fit in. disk->part_tbl
|
|
|
|
* uses RCU to allow unlocked dereferencing for stats and other stuff.
|
|
|
|
*
|
|
|
|
* LOCKING:
|
2017-08-18 07:23:06 +08:00
|
|
|
* Matching bd_mutex locked or the caller is the only user of @disk.
|
|
|
|
* Might sleep.
|
2008-08-25 18:56:15 +08:00
|
|
|
*
|
|
|
|
* RETURNS:
|
|
|
|
* 0 on success, -errno on failure.
|
|
|
|
*/
|
|
|
|
int disk_expand_part_tbl(struct gendisk *disk, int partno)
|
|
|
|
{
|
2017-08-18 07:23:06 +08:00
|
|
|
struct disk_part_tbl *old_ptbl =
|
|
|
|
rcu_dereference_protected(disk->part_tbl, 1);
|
2008-08-25 18:56:15 +08:00
|
|
|
struct disk_part_tbl *new_ptbl;
|
|
|
|
int len = old_ptbl ? old_ptbl->len : 0;
|
2014-11-20 04:06:22 +08:00
|
|
|
int i, target;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* check for int overflow, since we can get here from blkpg_ioctl()
|
|
|
|
* with a user passed 'partno'.
|
|
|
|
*/
|
|
|
|
target = partno + 1;
|
|
|
|
if (target < 0)
|
|
|
|
return -EINVAL;
|
2008-08-25 18:56:15 +08:00
|
|
|
|
|
|
|
/* disk_max_parts() is zero during initialization, ignore if so */
|
|
|
|
if (disk_max_parts(disk) && target > disk_max_parts(disk))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (target <= len)
|
|
|
|
return 0;
|
|
|
|
|
2019-06-01 02:47:54 +08:00
|
|
|
new_ptbl = kzalloc_node(struct_size(new_ptbl, part, target), GFP_KERNEL,
|
|
|
|
disk->node_id);
|
2008-08-25 18:56:15 +08:00
|
|
|
if (!new_ptbl)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
new_ptbl->len = target;
|
|
|
|
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
rcu_assign_pointer(new_ptbl->part[i], old_ptbl->part[i]);
|
|
|
|
|
|
|
|
disk_replace_part_tbl(disk, new_ptbl);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-05-22 04:08:01 +08:00
|
|
|
static void disk_release(struct device *dev)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2007-05-22 04:08:01 +08:00
|
|
|
struct gendisk *disk = dev_to_disk(dev);
|
|
|
|
|
2014-08-26 23:05:36 +08:00
|
|
|
blk_free_devt(dev->devt);
|
implement in-kernel gendisk events handling
Currently, media presence polling for removeable block devices is done
from userland. There are several issues with this.
* Polling is done by periodically opening the device. For SCSI
devices, the command sequence generated by such action involves a
few different commands including TEST_UNIT_READY. This behavior,
while perfectly legal, is different from Windows which only issues
single command, GET_EVENT_STATUS_NOTIFICATION. Unfortunately, some
ATAPI devices lock up after being periodically queried such command
sequences.
* There is no reliable and unintrusive way for a userland program to
tell whether the target device is safe for media presence polling.
For example, polling for media presence during an on-going burning
session can make it fail. The polling program can avoid this by
opening the device with O_EXCL but then it risks making a valid
exclusive user of the device fail w/ -EBUSY.
* Userland polling is unnecessarily heavy and in-kernel implementation
is lighter and better coordinated (workqueue, timer slack).
This patch implements framework for in-kernel disk event handling,
which includes media presence polling.
* bdops->check_events() is added, which supercedes ->media_changed().
It should check whether there's any pending event and return if so.
Currently, two events are defined - DISK_EVENT_MEDIA_CHANGE and
DISK_EVENT_EJECT_REQUEST. ->check_events() is guaranteed not to be
called parallelly.
* gendisk->events and ->async_events are added. These should be
initialized by block driver before passing the device to add_disk().
The former contains the mask of all supported events and the latter
the mask of all events which the device can report without polling.
/sys/block/*/events[_async] export these to userland.
* Kernel parameter block.events_dfl_poll_msecs controls the system
polling interval (default is 0 which means disable) and
/sys/block/*/events_poll_msecs control polling intervals for
individual devices (default is -1 meaning use system setting). Note
that if a device can report all supported events asynchronously and
its polling interval isn't explicitly set, the device won't be
polled regardless of the system polling interval.
* If a device is opened exclusively with write access, event checking
is automatically disabled until all write exclusive accesses are
released.
* There are event 'clearing' events. For example, both of currently
defined events are cleared after the device has been successfully
opened. This information is passed to ->check_events() callback
using @clearing argument as a hint.
* Event checking is always performed from system_nrt_wq and timer
slack is set to 25% for polling.
* Nothing changes for drivers which implement ->media_changed() but
not ->check_events(). Going forward, all drivers will be converted
to ->check_events() and ->media_change() will be dropped.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
2010-12-09 03:57:37 +08:00
|
|
|
disk_release_events(disk);
|
2005-04-17 06:20:36 +08:00
|
|
|
kfree(disk->random);
|
2008-08-25 18:56:15 +08:00
|
|
|
disk_replace_part_tbl(disk, NULL);
|
2015-07-16 11:16:44 +08:00
|
|
|
hd_free_part(&disk->part0);
|
2011-10-19 20:31:07 +08:00
|
|
|
if (disk->queue)
|
|
|
|
blk_put_queue(disk->queue);
|
2005-04-17 06:20:36 +08:00
|
|
|
kfree(disk);
|
|
|
|
}
|
2007-05-22 04:08:01 +08:00
|
|
|
struct class block_class = {
|
|
|
|
.name = "block",
|
2005-04-17 06:20:36 +08:00
|
|
|
};
|
|
|
|
|
2013-04-07 00:56:00 +08:00
|
|
|
static char *block_devnode(struct device *dev, umode_t *mode,
|
2013-04-12 02:43:29 +08:00
|
|
|
kuid_t *uid, kgid_t *gid)
|
2009-04-30 21:23:42 +08:00
|
|
|
{
|
|
|
|
struct gendisk *disk = dev_to_disk(dev);
|
|
|
|
|
2009-09-19 05:01:12 +08:00
|
|
|
if (disk->devnode)
|
|
|
|
return disk->devnode(disk, mode);
|
2009-04-30 21:23:42 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-06-21 02:15:48 +08:00
|
|
|
static const struct device_type disk_type = {
|
2007-05-22 04:08:01 +08:00
|
|
|
.name = "disk",
|
|
|
|
.groups = disk_attr_groups,
|
|
|
|
.release = disk_release,
|
2009-09-19 05:01:12 +08:00
|
|
|
.devnode = block_devnode,
|
2005-04-17 06:20:36 +08:00
|
|
|
};
|
|
|
|
|
2008-05-24 00:44:11 +08:00
|
|
|
#ifdef CONFIG_PROC_FS
|
2008-09-03 15:01:09 +08:00
|
|
|
/*
|
|
|
|
* aggregate disk stat collector. Uses the same stats that the sysfs
|
|
|
|
* entries do, above, but makes them available through one seq_file.
|
|
|
|
*
|
|
|
|
* The output looks suspiciously like /proc/partitions with a bunch of
|
|
|
|
* extra fields.
|
|
|
|
*/
|
|
|
|
static int diskstats_show(struct seq_file *seqf, void *v)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct gendisk *gp = v;
|
2008-09-03 15:03:02 +08:00
|
|
|
struct disk_part_iter piter;
|
|
|
|
struct hd_struct *hd;
|
2005-04-17 06:20:36 +08:00
|
|
|
char buf[BDEVNAME_SIZE];
|
2018-12-07 00:41:21 +08:00
|
|
|
unsigned int inflight;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/*
|
2008-08-25 18:56:05 +08:00
|
|
|
if (&disk_to_dev(gp)->kobj.entry == block_class.devices.next)
|
2008-09-03 15:01:09 +08:00
|
|
|
seq_puts(seqf, "major minor name"
|
2005-04-17 06:20:36 +08:00
|
|
|
" rio rmerge rsect ruse wio wmerge "
|
|
|
|
"wsect wuse running use aveq"
|
|
|
|
"\n\n");
|
|
|
|
*/
|
2011-06-13 16:45:43 +08:00
|
|
|
|
2009-04-17 14:34:48 +08:00
|
|
|
disk_part_iter_init(&piter, gp, DISK_PITER_INCL_EMPTY_PART0);
|
2008-09-03 15:03:02 +08:00
|
|
|
while ((hd = disk_part_iter_next(&piter))) {
|
2018-12-07 00:41:21 +08:00
|
|
|
inflight = part_in_flight(gp->queue, hd);
|
2018-07-18 19:47:40 +08:00
|
|
|
seq_printf(seqf, "%4d %7d %s "
|
|
|
|
"%lu %lu %lu %u "
|
|
|
|
"%lu %lu %lu %u "
|
|
|
|
"%u %u %u "
|
2019-11-21 18:40:26 +08:00
|
|
|
"%lu %lu %lu %u "
|
|
|
|
"%lu %u"
|
|
|
|
"\n",
|
2008-09-03 15:01:48 +08:00
|
|
|
MAJOR(part_devt(hd)), MINOR(part_devt(hd)),
|
|
|
|
disk_name(gp, hd->partno, buf),
|
2018-07-18 19:47:38 +08:00
|
|
|
part_stat_read(hd, ios[STAT_READ]),
|
|
|
|
part_stat_read(hd, merges[STAT_READ]),
|
|
|
|
part_stat_read(hd, sectors[STAT_READ]),
|
2018-09-22 07:44:34 +08:00
|
|
|
(unsigned int)part_stat_read_msecs(hd, STAT_READ),
|
2018-07-18 19:47:38 +08:00
|
|
|
part_stat_read(hd, ios[STAT_WRITE]),
|
|
|
|
part_stat_read(hd, merges[STAT_WRITE]),
|
|
|
|
part_stat_read(hd, sectors[STAT_WRITE]),
|
2018-09-22 07:44:34 +08:00
|
|
|
(unsigned int)part_stat_read_msecs(hd, STAT_WRITE),
|
2018-12-07 00:41:21 +08:00
|
|
|
inflight,
|
2008-02-08 18:04:56 +08:00
|
|
|
jiffies_to_msecs(part_stat_read(hd, io_ticks)),
|
2018-07-18 19:47:40 +08:00
|
|
|
jiffies_to_msecs(part_stat_read(hd, time_in_queue)),
|
|
|
|
part_stat_read(hd, ios[STAT_DISCARD]),
|
|
|
|
part_stat_read(hd, merges[STAT_DISCARD]),
|
|
|
|
part_stat_read(hd, sectors[STAT_DISCARD]),
|
2019-11-21 18:40:26 +08:00
|
|
|
(unsigned int)part_stat_read_msecs(hd, STAT_DISCARD),
|
|
|
|
part_stat_read(hd, ios[STAT_FLUSH]),
|
|
|
|
(unsigned int)part_stat_read_msecs(hd, STAT_FLUSH)
|
2008-02-08 18:04:56 +08:00
|
|
|
);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2008-09-03 15:03:02 +08:00
|
|
|
disk_part_iter_exit(&piter);
|
2011-06-13 16:45:43 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-10-06 16:55:38 +08:00
|
|
|
static const struct seq_operations diskstats_op = {
|
2008-09-03 14:57:12 +08:00
|
|
|
.start = disk_seqf_start,
|
|
|
|
.next = disk_seqf_next,
|
|
|
|
.stop = disk_seqf_stop,
|
2005-04-17 06:20:36 +08:00
|
|
|
.show = diskstats_show
|
|
|
|
};
|
2008-10-05 03:53:21 +08:00
|
|
|
|
|
|
|
static int __init proc_genhd_init(void)
|
|
|
|
{
|
2018-04-14 01:44:18 +08:00
|
|
|
proc_create_seq("diskstats", 0, NULL, &diskstats_op);
|
|
|
|
proc_create_seq("partitions", 0, NULL, &partitions_op);
|
2008-10-05 03:53:21 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
module_init(proc_genhd_init);
|
2008-05-24 00:44:11 +08:00
|
|
|
#endif /* CONFIG_PROC_FS */
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2008-09-03 15:01:09 +08:00
|
|
|
dev_t blk_lookup_devt(const char *name, int partno)
|
2008-05-23 05:21:08 +08:00
|
|
|
{
|
2008-09-03 14:57:12 +08:00
|
|
|
dev_t devt = MKDEV(0, 0);
|
|
|
|
struct class_dev_iter iter;
|
|
|
|
struct device *dev;
|
2008-05-23 05:21:08 +08:00
|
|
|
|
2008-09-03 14:57:12 +08:00
|
|
|
class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
|
|
|
|
while ((dev = class_dev_iter_next(&iter))) {
|
2008-05-23 05:21:08 +08:00
|
|
|
struct gendisk *disk = dev_to_disk(dev);
|
2008-08-29 15:01:47 +08:00
|
|
|
struct hd_struct *part;
|
2008-05-23 05:21:08 +08:00
|
|
|
|
2009-01-07 02:44:43 +08:00
|
|
|
if (strcmp(dev_name(dev), name))
|
2008-09-03 15:01:48 +08:00
|
|
|
continue;
|
|
|
|
|
2009-02-18 17:33:59 +08:00
|
|
|
if (partno < disk->minors) {
|
|
|
|
/* We need to return the right devno, even
|
|
|
|
* if the partition doesn't exist yet.
|
|
|
|
*/
|
|
|
|
devt = MKDEV(MAJOR(dev->devt),
|
|
|
|
MINOR(dev->devt) + partno);
|
|
|
|
break;
|
|
|
|
}
|
2008-08-29 15:01:47 +08:00
|
|
|
part = disk_get_part(disk, partno);
|
2008-08-29 17:41:51 +08:00
|
|
|
if (part) {
|
2008-09-03 15:01:48 +08:00
|
|
|
devt = part_devt(part);
|
2008-09-03 15:03:02 +08:00
|
|
|
disk_put_part(part);
|
2008-08-29 15:01:47 +08:00
|
|
|
break;
|
2008-09-03 14:57:12 +08:00
|
|
|
}
|
2008-08-29 15:01:47 +08:00
|
|
|
disk_put_part(part);
|
2008-08-16 20:30:30 +08:00
|
|
|
}
|
2008-09-03 14:57:12 +08:00
|
|
|
class_dev_iter_exit(&iter);
|
2007-05-22 04:08:01 +08:00
|
|
|
return devt;
|
|
|
|
}
|
|
|
|
|
2017-10-25 16:56:05 +08:00
|
|
|
struct gendisk *__alloc_disk_node(int minors, int node_id)
|
2005-06-23 15:08:19 +08:00
|
|
|
{
|
|
|
|
struct gendisk *disk;
|
2017-08-18 07:23:06 +08:00
|
|
|
struct disk_part_tbl *ptbl;
|
2005-06-23 15:08:19 +08:00
|
|
|
|
2017-08-24 01:10:29 +08:00
|
|
|
if (minors > DISK_MAX_PARTS) {
|
|
|
|
printk(KERN_ERR
|
2017-11-19 09:43:38 +08:00
|
|
|
"block: can't allocate more than %d partitions\n",
|
2017-08-24 01:10:29 +08:00
|
|
|
DISK_MAX_PARTS);
|
|
|
|
minors = DISK_MAX_PARTS;
|
|
|
|
}
|
2005-06-23 15:08:19 +08:00
|
|
|
|
2013-08-30 06:21:42 +08:00
|
|
|
disk = kzalloc_node(sizeof(struct gendisk), GFP_KERNEL, node_id);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (disk) {
|
2008-08-25 18:56:14 +08:00
|
|
|
if (!init_part_stats(&disk->part0)) {
|
2005-04-17 06:20:36 +08:00
|
|
|
kfree(disk);
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-02-26 20:01:41 +08:00
|
|
|
init_rwsem(&disk->lookup_sem);
|
2008-11-20 15:37:37 +08:00
|
|
|
disk->node_id = node_id;
|
2008-08-25 18:56:15 +08:00
|
|
|
if (disk_expand_part_tbl(disk, 0)) {
|
|
|
|
free_part_stats(&disk->part0);
|
2008-09-03 15:06:42 +08:00
|
|
|
kfree(disk);
|
|
|
|
return NULL;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2017-08-18 07:23:06 +08:00
|
|
|
ptbl = rcu_dereference_protected(disk->part_tbl, 1);
|
|
|
|
rcu_assign_pointer(ptbl->part[0], &disk->part0);
|
2011-01-07 15:43:37 +08:00
|
|
|
|
2012-08-01 18:24:18 +08:00
|
|
|
/*
|
|
|
|
* set_capacity() and get_capacity() currently don't use
|
|
|
|
* seqcounter to read/update the part0->nr_sects. Still init
|
|
|
|
* the counter as we can read the sectors in IO submission
|
|
|
|
* patch using seqence counters.
|
|
|
|
*
|
|
|
|
* TODO: Ideally set_capacity() and get_capacity() should be
|
|
|
|
* converted to make use of bd_mutex and sequence counters.
|
|
|
|
*/
|
|
|
|
seqcount_init(&disk->part0.nr_sects_seq);
|
2015-07-16 11:16:45 +08:00
|
|
|
if (hd_ref_init(&disk->part0)) {
|
|
|
|
hd_free_part(&disk->part0);
|
|
|
|
kfree(disk);
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-09-03 15:06:42 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
disk->minors = minors;
|
|
|
|
rand_initialize_disk(disk);
|
2008-08-25 18:56:05 +08:00
|
|
|
disk_to_dev(disk)->class = &block_class;
|
|
|
|
disk_to_dev(disk)->type = &disk_type;
|
|
|
|
device_initialize(disk_to_dev(disk));
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
return disk;
|
|
|
|
}
|
2017-10-25 16:56:05 +08:00
|
|
|
EXPORT_SYMBOL(__alloc_disk_node);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2018-02-26 20:01:38 +08:00
|
|
|
struct kobject *get_disk_and_module(struct gendisk *disk)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct module *owner;
|
|
|
|
struct kobject *kobj;
|
|
|
|
|
|
|
|
if (!disk->fops)
|
|
|
|
return NULL;
|
|
|
|
owner = disk->fops->owner;
|
|
|
|
if (owner && !try_module_get(owner))
|
|
|
|
return NULL;
|
2017-03-23 08:37:02 +08:00
|
|
|
kobj = kobject_get_unless_zero(&disk_to_dev(disk)->kobj);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (kobj == NULL) {
|
|
|
|
module_put(owner);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return kobj;
|
|
|
|
|
|
|
|
}
|
2018-02-26 20:01:38 +08:00
|
|
|
EXPORT_SYMBOL(get_disk_and_module);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
void put_disk(struct gendisk *disk)
|
|
|
|
{
|
|
|
|
if (disk)
|
2008-08-25 18:56:05 +08:00
|
|
|
kobject_put(&disk_to_dev(disk)->kobj);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(put_disk);
|
|
|
|
|
2018-02-26 20:01:39 +08:00
|
|
|
/*
|
|
|
|
* This is a counterpart of get_disk_and_module() and thus also of
|
|
|
|
* get_gendisk().
|
|
|
|
*/
|
|
|
|
void put_disk_and_module(struct gendisk *disk)
|
|
|
|
{
|
|
|
|
if (disk) {
|
|
|
|
struct module *owner = disk->fops->owner;
|
|
|
|
|
|
|
|
put_disk(disk);
|
|
|
|
module_put(owner);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(put_disk_and_module);
|
|
|
|
|
2009-07-28 15:13:13 +08:00
|
|
|
static void set_disk_ro_uevent(struct gendisk *gd, int ro)
|
|
|
|
{
|
|
|
|
char event[] = "DISK_RO=1";
|
|
|
|
char *envp[] = { event, NULL };
|
|
|
|
|
|
|
|
if (!ro)
|
|
|
|
event[8] = '0';
|
|
|
|
kobject_uevent_env(&disk_to_dev(gd)->kobj, KOBJ_CHANGE, envp);
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
void set_device_ro(struct block_device *bdev, int flag)
|
|
|
|
{
|
2008-08-25 18:56:10 +08:00
|
|
|
bdev->bd_part->policy = flag;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
EXPORT_SYMBOL(set_device_ro);
|
|
|
|
|
|
|
|
void set_disk_ro(struct gendisk *disk, int flag)
|
|
|
|
{
|
2008-09-03 15:03:02 +08:00
|
|
|
struct disk_part_iter piter;
|
|
|
|
struct hd_struct *part;
|
|
|
|
|
2009-07-28 15:13:13 +08:00
|
|
|
if (disk->part0.policy != flag) {
|
|
|
|
set_disk_ro_uevent(disk, flag);
|
|
|
|
disk->part0.policy = flag;
|
|
|
|
}
|
|
|
|
|
|
|
|
disk_part_iter_init(&piter, disk, DISK_PITER_INCL_EMPTY);
|
2008-09-03 15:03:02 +08:00
|
|
|
while ((part = disk_part_iter_next(&piter)))
|
|
|
|
part->policy = flag;
|
|
|
|
disk_part_iter_exit(&piter);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
EXPORT_SYMBOL(set_disk_ro);
|
|
|
|
|
|
|
|
int bdev_read_only(struct block_device *bdev)
|
|
|
|
{
|
|
|
|
if (!bdev)
|
|
|
|
return 0;
|
2008-08-25 18:56:10 +08:00
|
|
|
return bdev->bd_part->policy;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
EXPORT_SYMBOL(bdev_read_only);
|
|
|
|
|
2008-09-03 15:01:09 +08:00
|
|
|
int invalidate_partition(struct gendisk *disk, int partno)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
int res = 0;
|
2008-09-03 15:01:09 +08:00
|
|
|
struct block_device *bdev = bdget_disk(disk, partno);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (bdev) {
|
2005-05-06 07:15:59 +08:00
|
|
|
fsync_bdev(bdev);
|
2011-02-24 14:25:47 +08:00
|
|
|
res = __invalidate_device(bdev, true);
|
2005-04-17 06:20:36 +08:00
|
|
|
bdput(bdev);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
EXPORT_SYMBOL(invalidate_partition);
|
implement in-kernel gendisk events handling
Currently, media presence polling for removeable block devices is done
from userland. There are several issues with this.
* Polling is done by periodically opening the device. For SCSI
devices, the command sequence generated by such action involves a
few different commands including TEST_UNIT_READY. This behavior,
while perfectly legal, is different from Windows which only issues
single command, GET_EVENT_STATUS_NOTIFICATION. Unfortunately, some
ATAPI devices lock up after being periodically queried such command
sequences.
* There is no reliable and unintrusive way for a userland program to
tell whether the target device is safe for media presence polling.
For example, polling for media presence during an on-going burning
session can make it fail. The polling program can avoid this by
opening the device with O_EXCL but then it risks making a valid
exclusive user of the device fail w/ -EBUSY.
* Userland polling is unnecessarily heavy and in-kernel implementation
is lighter and better coordinated (workqueue, timer slack).
This patch implements framework for in-kernel disk event handling,
which includes media presence polling.
* bdops->check_events() is added, which supercedes ->media_changed().
It should check whether there's any pending event and return if so.
Currently, two events are defined - DISK_EVENT_MEDIA_CHANGE and
DISK_EVENT_EJECT_REQUEST. ->check_events() is guaranteed not to be
called parallelly.
* gendisk->events and ->async_events are added. These should be
initialized by block driver before passing the device to add_disk().
The former contains the mask of all supported events and the latter
the mask of all events which the device can report without polling.
/sys/block/*/events[_async] export these to userland.
* Kernel parameter block.events_dfl_poll_msecs controls the system
polling interval (default is 0 which means disable) and
/sys/block/*/events_poll_msecs control polling intervals for
individual devices (default is -1 meaning use system setting). Note
that if a device can report all supported events asynchronously and
its polling interval isn't explicitly set, the device won't be
polled regardless of the system polling interval.
* If a device is opened exclusively with write access, event checking
is automatically disabled until all write exclusive accesses are
released.
* There are event 'clearing' events. For example, both of currently
defined events are cleared after the device has been successfully
opened. This information is passed to ->check_events() callback
using @clearing argument as a hint.
* Event checking is always performed from system_nrt_wq and timer
slack is set to 25% for polling.
* Nothing changes for drivers which implement ->media_changed() but
not ->check_events(). Going forward, all drivers will be converted
to ->check_events() and ->media_change() will be dropped.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
2010-12-09 03:57:37 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Disk events - monitor disk events like media change and eject request.
|
|
|
|
*/
|
|
|
|
struct disk_events {
|
|
|
|
struct list_head node; /* all disk_event's */
|
|
|
|
struct gendisk *disk; /* the associated disk */
|
|
|
|
spinlock_t lock;
|
|
|
|
|
2011-06-10 02:43:59 +08:00
|
|
|
struct mutex block_mutex; /* protects blocking */
|
implement in-kernel gendisk events handling
Currently, media presence polling for removeable block devices is done
from userland. There are several issues with this.
* Polling is done by periodically opening the device. For SCSI
devices, the command sequence generated by such action involves a
few different commands including TEST_UNIT_READY. This behavior,
while perfectly legal, is different from Windows which only issues
single command, GET_EVENT_STATUS_NOTIFICATION. Unfortunately, some
ATAPI devices lock up after being periodically queried such command
sequences.
* There is no reliable and unintrusive way for a userland program to
tell whether the target device is safe for media presence polling.
For example, polling for media presence during an on-going burning
session can make it fail. The polling program can avoid this by
opening the device with O_EXCL but then it risks making a valid
exclusive user of the device fail w/ -EBUSY.
* Userland polling is unnecessarily heavy and in-kernel implementation
is lighter and better coordinated (workqueue, timer slack).
This patch implements framework for in-kernel disk event handling,
which includes media presence polling.
* bdops->check_events() is added, which supercedes ->media_changed().
It should check whether there's any pending event and return if so.
Currently, two events are defined - DISK_EVENT_MEDIA_CHANGE and
DISK_EVENT_EJECT_REQUEST. ->check_events() is guaranteed not to be
called parallelly.
* gendisk->events and ->async_events are added. These should be
initialized by block driver before passing the device to add_disk().
The former contains the mask of all supported events and the latter
the mask of all events which the device can report without polling.
/sys/block/*/events[_async] export these to userland.
* Kernel parameter block.events_dfl_poll_msecs controls the system
polling interval (default is 0 which means disable) and
/sys/block/*/events_poll_msecs control polling intervals for
individual devices (default is -1 meaning use system setting). Note
that if a device can report all supported events asynchronously and
its polling interval isn't explicitly set, the device won't be
polled regardless of the system polling interval.
* If a device is opened exclusively with write access, event checking
is automatically disabled until all write exclusive accesses are
released.
* There are event 'clearing' events. For example, both of currently
defined events are cleared after the device has been successfully
opened. This information is passed to ->check_events() callback
using @clearing argument as a hint.
* Event checking is always performed from system_nrt_wq and timer
slack is set to 25% for polling.
* Nothing changes for drivers which implement ->media_changed() but
not ->check_events(). Going forward, all drivers will be converted
to ->check_events() and ->media_change() will be dropped.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
2010-12-09 03:57:37 +08:00
|
|
|
int block; /* event blocking depth */
|
|
|
|
unsigned int pending; /* events already sent out */
|
|
|
|
unsigned int clearing; /* events being cleared */
|
|
|
|
|
|
|
|
long poll_msecs; /* interval, -1 for default */
|
|
|
|
struct delayed_work dwork;
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char *disk_events_strs[] = {
|
|
|
|
[ilog2(DISK_EVENT_MEDIA_CHANGE)] = "media_change",
|
|
|
|
[ilog2(DISK_EVENT_EJECT_REQUEST)] = "eject_request",
|
|
|
|
};
|
|
|
|
|
|
|
|
static char *disk_uevents[] = {
|
|
|
|
[ilog2(DISK_EVENT_MEDIA_CHANGE)] = "DISK_MEDIA_CHANGE=1",
|
|
|
|
[ilog2(DISK_EVENT_EJECT_REQUEST)] = "DISK_EJECT_REQUEST=1",
|
|
|
|
};
|
|
|
|
|
|
|
|
/* list of all disk_events */
|
|
|
|
static DEFINE_MUTEX(disk_events_mutex);
|
|
|
|
static LIST_HEAD(disk_events);
|
|
|
|
|
|
|
|
/* disable in-kernel polling by default */
|
2015-11-24 09:58:46 +08:00
|
|
|
static unsigned long disk_events_dfl_poll_msecs;
|
implement in-kernel gendisk events handling
Currently, media presence polling for removeable block devices is done
from userland. There are several issues with this.
* Polling is done by periodically opening the device. For SCSI
devices, the command sequence generated by such action involves a
few different commands including TEST_UNIT_READY. This behavior,
while perfectly legal, is different from Windows which only issues
single command, GET_EVENT_STATUS_NOTIFICATION. Unfortunately, some
ATAPI devices lock up after being periodically queried such command
sequences.
* There is no reliable and unintrusive way for a userland program to
tell whether the target device is safe for media presence polling.
For example, polling for media presence during an on-going burning
session can make it fail. The polling program can avoid this by
opening the device with O_EXCL but then it risks making a valid
exclusive user of the device fail w/ -EBUSY.
* Userland polling is unnecessarily heavy and in-kernel implementation
is lighter and better coordinated (workqueue, timer slack).
This patch implements framework for in-kernel disk event handling,
which includes media presence polling.
* bdops->check_events() is added, which supercedes ->media_changed().
It should check whether there's any pending event and return if so.
Currently, two events are defined - DISK_EVENT_MEDIA_CHANGE and
DISK_EVENT_EJECT_REQUEST. ->check_events() is guaranteed not to be
called parallelly.
* gendisk->events and ->async_events are added. These should be
initialized by block driver before passing the device to add_disk().
The former contains the mask of all supported events and the latter
the mask of all events which the device can report without polling.
/sys/block/*/events[_async] export these to userland.
* Kernel parameter block.events_dfl_poll_msecs controls the system
polling interval (default is 0 which means disable) and
/sys/block/*/events_poll_msecs control polling intervals for
individual devices (default is -1 meaning use system setting). Note
that if a device can report all supported events asynchronously and
its polling interval isn't explicitly set, the device won't be
polled regardless of the system polling interval.
* If a device is opened exclusively with write access, event checking
is automatically disabled until all write exclusive accesses are
released.
* There are event 'clearing' events. For example, both of currently
defined events are cleared after the device has been successfully
opened. This information is passed to ->check_events() callback
using @clearing argument as a hint.
* Event checking is always performed from system_nrt_wq and timer
slack is set to 25% for polling.
* Nothing changes for drivers which implement ->media_changed() but
not ->check_events(). Going forward, all drivers will be converted
to ->check_events() and ->media_change() will be dropped.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
2010-12-09 03:57:37 +08:00
|
|
|
|
|
|
|
static unsigned long disk_events_poll_jiffies(struct gendisk *disk)
|
|
|
|
{
|
|
|
|
struct disk_events *ev = disk->ev;
|
|
|
|
long intv_msecs = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If device-specific poll interval is set, always use it. If
|
2019-03-27 21:51:01 +08:00
|
|
|
* the default is being used, poll if the POLL flag is set.
|
implement in-kernel gendisk events handling
Currently, media presence polling for removeable block devices is done
from userland. There are several issues with this.
* Polling is done by periodically opening the device. For SCSI
devices, the command sequence generated by such action involves a
few different commands including TEST_UNIT_READY. This behavior,
while perfectly legal, is different from Windows which only issues
single command, GET_EVENT_STATUS_NOTIFICATION. Unfortunately, some
ATAPI devices lock up after being periodically queried such command
sequences.
* There is no reliable and unintrusive way for a userland program to
tell whether the target device is safe for media presence polling.
For example, polling for media presence during an on-going burning
session can make it fail. The polling program can avoid this by
opening the device with O_EXCL but then it risks making a valid
exclusive user of the device fail w/ -EBUSY.
* Userland polling is unnecessarily heavy and in-kernel implementation
is lighter and better coordinated (workqueue, timer slack).
This patch implements framework for in-kernel disk event handling,
which includes media presence polling.
* bdops->check_events() is added, which supercedes ->media_changed().
It should check whether there's any pending event and return if so.
Currently, two events are defined - DISK_EVENT_MEDIA_CHANGE and
DISK_EVENT_EJECT_REQUEST. ->check_events() is guaranteed not to be
called parallelly.
* gendisk->events and ->async_events are added. These should be
initialized by block driver before passing the device to add_disk().
The former contains the mask of all supported events and the latter
the mask of all events which the device can report without polling.
/sys/block/*/events[_async] export these to userland.
* Kernel parameter block.events_dfl_poll_msecs controls the system
polling interval (default is 0 which means disable) and
/sys/block/*/events_poll_msecs control polling intervals for
individual devices (default is -1 meaning use system setting). Note
that if a device can report all supported events asynchronously and
its polling interval isn't explicitly set, the device won't be
polled regardless of the system polling interval.
* If a device is opened exclusively with write access, event checking
is automatically disabled until all write exclusive accesses are
released.
* There are event 'clearing' events. For example, both of currently
defined events are cleared after the device has been successfully
opened. This information is passed to ->check_events() callback
using @clearing argument as a hint.
* Event checking is always performed from system_nrt_wq and timer
slack is set to 25% for polling.
* Nothing changes for drivers which implement ->media_changed() but
not ->check_events(). Going forward, all drivers will be converted
to ->check_events() and ->media_change() will be dropped.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
2010-12-09 03:57:37 +08:00
|
|
|
*/
|
|
|
|
if (ev->poll_msecs >= 0)
|
|
|
|
intv_msecs = ev->poll_msecs;
|
2019-03-27 21:51:02 +08:00
|
|
|
else if (disk->event_flags & DISK_EVENT_FLAG_POLL)
|
implement in-kernel gendisk events handling
Currently, media presence polling for removeable block devices is done
from userland. There are several issues with this.
* Polling is done by periodically opening the device. For SCSI
devices, the command sequence generated by such action involves a
few different commands including TEST_UNIT_READY. This behavior,
while perfectly legal, is different from Windows which only issues
single command, GET_EVENT_STATUS_NOTIFICATION. Unfortunately, some
ATAPI devices lock up after being periodically queried such command
sequences.
* There is no reliable and unintrusive way for a userland program to
tell whether the target device is safe for media presence polling.
For example, polling for media presence during an on-going burning
session can make it fail. The polling program can avoid this by
opening the device with O_EXCL but then it risks making a valid
exclusive user of the device fail w/ -EBUSY.
* Userland polling is unnecessarily heavy and in-kernel implementation
is lighter and better coordinated (workqueue, timer slack).
This patch implements framework for in-kernel disk event handling,
which includes media presence polling.
* bdops->check_events() is added, which supercedes ->media_changed().
It should check whether there's any pending event and return if so.
Currently, two events are defined - DISK_EVENT_MEDIA_CHANGE and
DISK_EVENT_EJECT_REQUEST. ->check_events() is guaranteed not to be
called parallelly.
* gendisk->events and ->async_events are added. These should be
initialized by block driver before passing the device to add_disk().
The former contains the mask of all supported events and the latter
the mask of all events which the device can report without polling.
/sys/block/*/events[_async] export these to userland.
* Kernel parameter block.events_dfl_poll_msecs controls the system
polling interval (default is 0 which means disable) and
/sys/block/*/events_poll_msecs control polling intervals for
individual devices (default is -1 meaning use system setting). Note
that if a device can report all supported events asynchronously and
its polling interval isn't explicitly set, the device won't be
polled regardless of the system polling interval.
* If a device is opened exclusively with write access, event checking
is automatically disabled until all write exclusive accesses are
released.
* There are event 'clearing' events. For example, both of currently
defined events are cleared after the device has been successfully
opened. This information is passed to ->check_events() callback
using @clearing argument as a hint.
* Event checking is always performed from system_nrt_wq and timer
slack is set to 25% for polling.
* Nothing changes for drivers which implement ->media_changed() but
not ->check_events(). Going forward, all drivers will be converted
to ->check_events() and ->media_change() will be dropped.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
2010-12-09 03:57:37 +08:00
|
|
|
intv_msecs = disk_events_dfl_poll_msecs;
|
|
|
|
|
|
|
|
return msecs_to_jiffies(intv_msecs);
|
|
|
|
}
|
|
|
|
|
2011-06-10 02:43:55 +08:00
|
|
|
/**
|
|
|
|
* disk_block_events - block and flush disk event checking
|
|
|
|
* @disk: disk to block events for
|
|
|
|
*
|
|
|
|
* On return from this function, it is guaranteed that event checking
|
|
|
|
* isn't in progress and won't happen until unblocked by
|
|
|
|
* disk_unblock_events(). Events blocking is counted and the actual
|
|
|
|
* unblocking happens after the matching number of unblocks are done.
|
|
|
|
*
|
|
|
|
* Note that this intentionally does not block event checking from
|
|
|
|
* disk_clear_events().
|
|
|
|
*
|
|
|
|
* CONTEXT:
|
|
|
|
* Might sleep.
|
|
|
|
*/
|
|
|
|
void disk_block_events(struct gendisk *disk)
|
implement in-kernel gendisk events handling
Currently, media presence polling for removeable block devices is done
from userland. There are several issues with this.
* Polling is done by periodically opening the device. For SCSI
devices, the command sequence generated by such action involves a
few different commands including TEST_UNIT_READY. This behavior,
while perfectly legal, is different from Windows which only issues
single command, GET_EVENT_STATUS_NOTIFICATION. Unfortunately, some
ATAPI devices lock up after being periodically queried such command
sequences.
* There is no reliable and unintrusive way for a userland program to
tell whether the target device is safe for media presence polling.
For example, polling for media presence during an on-going burning
session can make it fail. The polling program can avoid this by
opening the device with O_EXCL but then it risks making a valid
exclusive user of the device fail w/ -EBUSY.
* Userland polling is unnecessarily heavy and in-kernel implementation
is lighter and better coordinated (workqueue, timer slack).
This patch implements framework for in-kernel disk event handling,
which includes media presence polling.
* bdops->check_events() is added, which supercedes ->media_changed().
It should check whether there's any pending event and return if so.
Currently, two events are defined - DISK_EVENT_MEDIA_CHANGE and
DISK_EVENT_EJECT_REQUEST. ->check_events() is guaranteed not to be
called parallelly.
* gendisk->events and ->async_events are added. These should be
initialized by block driver before passing the device to add_disk().
The former contains the mask of all supported events and the latter
the mask of all events which the device can report without polling.
/sys/block/*/events[_async] export these to userland.
* Kernel parameter block.events_dfl_poll_msecs controls the system
polling interval (default is 0 which means disable) and
/sys/block/*/events_poll_msecs control polling intervals for
individual devices (default is -1 meaning use system setting). Note
that if a device can report all supported events asynchronously and
its polling interval isn't explicitly set, the device won't be
polled regardless of the system polling interval.
* If a device is opened exclusively with write access, event checking
is automatically disabled until all write exclusive accesses are
released.
* There are event 'clearing' events. For example, both of currently
defined events are cleared after the device has been successfully
opened. This information is passed to ->check_events() callback
using @clearing argument as a hint.
* Event checking is always performed from system_nrt_wq and timer
slack is set to 25% for polling.
* Nothing changes for drivers which implement ->media_changed() but
not ->check_events(). Going forward, all drivers will be converted
to ->check_events() and ->media_change() will be dropped.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
2010-12-09 03:57:37 +08:00
|
|
|
{
|
|
|
|
struct disk_events *ev = disk->ev;
|
|
|
|
unsigned long flags;
|
|
|
|
bool cancel;
|
|
|
|
|
2011-06-10 02:43:55 +08:00
|
|
|
if (!ev)
|
|
|
|
return;
|
|
|
|
|
2011-06-10 02:43:59 +08:00
|
|
|
/*
|
|
|
|
* Outer mutex ensures that the first blocker completes canceling
|
|
|
|
* the event work before further blockers are allowed to finish.
|
|
|
|
*/
|
|
|
|
mutex_lock(&ev->block_mutex);
|
|
|
|
|
implement in-kernel gendisk events handling
Currently, media presence polling for removeable block devices is done
from userland. There are several issues with this.
* Polling is done by periodically opening the device. For SCSI
devices, the command sequence generated by such action involves a
few different commands including TEST_UNIT_READY. This behavior,
while perfectly legal, is different from Windows which only issues
single command, GET_EVENT_STATUS_NOTIFICATION. Unfortunately, some
ATAPI devices lock up after being periodically queried such command
sequences.
* There is no reliable and unintrusive way for a userland program to
tell whether the target device is safe for media presence polling.
For example, polling for media presence during an on-going burning
session can make it fail. The polling program can avoid this by
opening the device with O_EXCL but then it risks making a valid
exclusive user of the device fail w/ -EBUSY.
* Userland polling is unnecessarily heavy and in-kernel implementation
is lighter and better coordinated (workqueue, timer slack).
This patch implements framework for in-kernel disk event handling,
which includes media presence polling.
* bdops->check_events() is added, which supercedes ->media_changed().
It should check whether there's any pending event and return if so.
Currently, two events are defined - DISK_EVENT_MEDIA_CHANGE and
DISK_EVENT_EJECT_REQUEST. ->check_events() is guaranteed not to be
called parallelly.
* gendisk->events and ->async_events are added. These should be
initialized by block driver before passing the device to add_disk().
The former contains the mask of all supported events and the latter
the mask of all events which the device can report without polling.
/sys/block/*/events[_async] export these to userland.
* Kernel parameter block.events_dfl_poll_msecs controls the system
polling interval (default is 0 which means disable) and
/sys/block/*/events_poll_msecs control polling intervals for
individual devices (default is -1 meaning use system setting). Note
that if a device can report all supported events asynchronously and
its polling interval isn't explicitly set, the device won't be
polled regardless of the system polling interval.
* If a device is opened exclusively with write access, event checking
is automatically disabled until all write exclusive accesses are
released.
* There are event 'clearing' events. For example, both of currently
defined events are cleared after the device has been successfully
opened. This information is passed to ->check_events() callback
using @clearing argument as a hint.
* Event checking is always performed from system_nrt_wq and timer
slack is set to 25% for polling.
* Nothing changes for drivers which implement ->media_changed() but
not ->check_events(). Going forward, all drivers will be converted
to ->check_events() and ->media_change() will be dropped.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
2010-12-09 03:57:37 +08:00
|
|
|
spin_lock_irqsave(&ev->lock, flags);
|
|
|
|
cancel = !ev->block++;
|
|
|
|
spin_unlock_irqrestore(&ev->lock, flags);
|
|
|
|
|
2011-06-10 02:43:55 +08:00
|
|
|
if (cancel)
|
|
|
|
cancel_delayed_work_sync(&disk->ev->dwork);
|
2011-06-10 02:43:59 +08:00
|
|
|
|
|
|
|
mutex_unlock(&ev->block_mutex);
|
implement in-kernel gendisk events handling
Currently, media presence polling for removeable block devices is done
from userland. There are several issues with this.
* Polling is done by periodically opening the device. For SCSI
devices, the command sequence generated by such action involves a
few different commands including TEST_UNIT_READY. This behavior,
while perfectly legal, is different from Windows which only issues
single command, GET_EVENT_STATUS_NOTIFICATION. Unfortunately, some
ATAPI devices lock up after being periodically queried such command
sequences.
* There is no reliable and unintrusive way for a userland program to
tell whether the target device is safe for media presence polling.
For example, polling for media presence during an on-going burning
session can make it fail. The polling program can avoid this by
opening the device with O_EXCL but then it risks making a valid
exclusive user of the device fail w/ -EBUSY.
* Userland polling is unnecessarily heavy and in-kernel implementation
is lighter and better coordinated (workqueue, timer slack).
This patch implements framework for in-kernel disk event handling,
which includes media presence polling.
* bdops->check_events() is added, which supercedes ->media_changed().
It should check whether there's any pending event and return if so.
Currently, two events are defined - DISK_EVENT_MEDIA_CHANGE and
DISK_EVENT_EJECT_REQUEST. ->check_events() is guaranteed not to be
called parallelly.
* gendisk->events and ->async_events are added. These should be
initialized by block driver before passing the device to add_disk().
The former contains the mask of all supported events and the latter
the mask of all events which the device can report without polling.
/sys/block/*/events[_async] export these to userland.
* Kernel parameter block.events_dfl_poll_msecs controls the system
polling interval (default is 0 which means disable) and
/sys/block/*/events_poll_msecs control polling intervals for
individual devices (default is -1 meaning use system setting). Note
that if a device can report all supported events asynchronously and
its polling interval isn't explicitly set, the device won't be
polled regardless of the system polling interval.
* If a device is opened exclusively with write access, event checking
is automatically disabled until all write exclusive accesses are
released.
* There are event 'clearing' events. For example, both of currently
defined events are cleared after the device has been successfully
opened. This information is passed to ->check_events() callback
using @clearing argument as a hint.
* Event checking is always performed from system_nrt_wq and timer
slack is set to 25% for polling.
* Nothing changes for drivers which implement ->media_changed() but
not ->check_events(). Going forward, all drivers will be converted
to ->check_events() and ->media_change() will be dropped.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
2010-12-09 03:57:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void __disk_unblock_events(struct gendisk *disk, bool check_now)
|
|
|
|
{
|
|
|
|
struct disk_events *ev = disk->ev;
|
|
|
|
unsigned long intv;
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&ev->lock, flags);
|
|
|
|
|
|
|
|
if (WARN_ON_ONCE(ev->block <= 0))
|
|
|
|
goto out_unlock;
|
|
|
|
|
|
|
|
if (--ev->block)
|
|
|
|
goto out_unlock;
|
|
|
|
|
|
|
|
intv = disk_events_poll_jiffies(disk);
|
|
|
|
if (check_now)
|
2013-04-24 19:42:56 +08:00
|
|
|
queue_delayed_work(system_freezable_power_efficient_wq,
|
|
|
|
&ev->dwork, 0);
|
implement in-kernel gendisk events handling
Currently, media presence polling for removeable block devices is done
from userland. There are several issues with this.
* Polling is done by periodically opening the device. For SCSI
devices, the command sequence generated by such action involves a
few different commands including TEST_UNIT_READY. This behavior,
while perfectly legal, is different from Windows which only issues
single command, GET_EVENT_STATUS_NOTIFICATION. Unfortunately, some
ATAPI devices lock up after being periodically queried such command
sequences.
* There is no reliable and unintrusive way for a userland program to
tell whether the target device is safe for media presence polling.
For example, polling for media presence during an on-going burning
session can make it fail. The polling program can avoid this by
opening the device with O_EXCL but then it risks making a valid
exclusive user of the device fail w/ -EBUSY.
* Userland polling is unnecessarily heavy and in-kernel implementation
is lighter and better coordinated (workqueue, timer slack).
This patch implements framework for in-kernel disk event handling,
which includes media presence polling.
* bdops->check_events() is added, which supercedes ->media_changed().
It should check whether there's any pending event and return if so.
Currently, two events are defined - DISK_EVENT_MEDIA_CHANGE and
DISK_EVENT_EJECT_REQUEST. ->check_events() is guaranteed not to be
called parallelly.
* gendisk->events and ->async_events are added. These should be
initialized by block driver before passing the device to add_disk().
The former contains the mask of all supported events and the latter
the mask of all events which the device can report without polling.
/sys/block/*/events[_async] export these to userland.
* Kernel parameter block.events_dfl_poll_msecs controls the system
polling interval (default is 0 which means disable) and
/sys/block/*/events_poll_msecs control polling intervals for
individual devices (default is -1 meaning use system setting). Note
that if a device can report all supported events asynchronously and
its polling interval isn't explicitly set, the device won't be
polled regardless of the system polling interval.
* If a device is opened exclusively with write access, event checking
is automatically disabled until all write exclusive accesses are
released.
* There are event 'clearing' events. For example, both of currently
defined events are cleared after the device has been successfully
opened. This information is passed to ->check_events() callback
using @clearing argument as a hint.
* Event checking is always performed from system_nrt_wq and timer
slack is set to 25% for polling.
* Nothing changes for drivers which implement ->media_changed() but
not ->check_events(). Going forward, all drivers will be converted
to ->check_events() and ->media_change() will be dropped.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
2010-12-09 03:57:37 +08:00
|
|
|
else if (intv)
|
2013-04-24 19:42:56 +08:00
|
|
|
queue_delayed_work(system_freezable_power_efficient_wq,
|
|
|
|
&ev->dwork, intv);
|
implement in-kernel gendisk events handling
Currently, media presence polling for removeable block devices is done
from userland. There are several issues with this.
* Polling is done by periodically opening the device. For SCSI
devices, the command sequence generated by such action involves a
few different commands including TEST_UNIT_READY. This behavior,
while perfectly legal, is different from Windows which only issues
single command, GET_EVENT_STATUS_NOTIFICATION. Unfortunately, some
ATAPI devices lock up after being periodically queried such command
sequences.
* There is no reliable and unintrusive way for a userland program to
tell whether the target device is safe for media presence polling.
For example, polling for media presence during an on-going burning
session can make it fail. The polling program can avoid this by
opening the device with O_EXCL but then it risks making a valid
exclusive user of the device fail w/ -EBUSY.
* Userland polling is unnecessarily heavy and in-kernel implementation
is lighter and better coordinated (workqueue, timer slack).
This patch implements framework for in-kernel disk event handling,
which includes media presence polling.
* bdops->check_events() is added, which supercedes ->media_changed().
It should check whether there's any pending event and return if so.
Currently, two events are defined - DISK_EVENT_MEDIA_CHANGE and
DISK_EVENT_EJECT_REQUEST. ->check_events() is guaranteed not to be
called parallelly.
* gendisk->events and ->async_events are added. These should be
initialized by block driver before passing the device to add_disk().
The former contains the mask of all supported events and the latter
the mask of all events which the device can report without polling.
/sys/block/*/events[_async] export these to userland.
* Kernel parameter block.events_dfl_poll_msecs controls the system
polling interval (default is 0 which means disable) and
/sys/block/*/events_poll_msecs control polling intervals for
individual devices (default is -1 meaning use system setting). Note
that if a device can report all supported events asynchronously and
its polling interval isn't explicitly set, the device won't be
polled regardless of the system polling interval.
* If a device is opened exclusively with write access, event checking
is automatically disabled until all write exclusive accesses are
released.
* There are event 'clearing' events. For example, both of currently
defined events are cleared after the device has been successfully
opened. This information is passed to ->check_events() callback
using @clearing argument as a hint.
* Event checking is always performed from system_nrt_wq and timer
slack is set to 25% for polling.
* Nothing changes for drivers which implement ->media_changed() but
not ->check_events(). Going forward, all drivers will be converted
to ->check_events() and ->media_change() will be dropped.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
2010-12-09 03:57:37 +08:00
|
|
|
out_unlock:
|
|
|
|
spin_unlock_irqrestore(&ev->lock, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* disk_unblock_events - unblock disk event checking
|
|
|
|
* @disk: disk to unblock events for
|
|
|
|
*
|
|
|
|
* Undo disk_block_events(). When the block count reaches zero, it
|
|
|
|
* starts events polling if configured.
|
|
|
|
*
|
|
|
|
* CONTEXT:
|
|
|
|
* Don't care. Safe to call from irq context.
|
|
|
|
*/
|
|
|
|
void disk_unblock_events(struct gendisk *disk)
|
|
|
|
{
|
|
|
|
if (disk->ev)
|
2011-03-10 02:54:27 +08:00
|
|
|
__disk_unblock_events(disk, false);
|
implement in-kernel gendisk events handling
Currently, media presence polling for removeable block devices is done
from userland. There are several issues with this.
* Polling is done by periodically opening the device. For SCSI
devices, the command sequence generated by such action involves a
few different commands including TEST_UNIT_READY. This behavior,
while perfectly legal, is different from Windows which only issues
single command, GET_EVENT_STATUS_NOTIFICATION. Unfortunately, some
ATAPI devices lock up after being periodically queried such command
sequences.
* There is no reliable and unintrusive way for a userland program to
tell whether the target device is safe for media presence polling.
For example, polling for media presence during an on-going burning
session can make it fail. The polling program can avoid this by
opening the device with O_EXCL but then it risks making a valid
exclusive user of the device fail w/ -EBUSY.
* Userland polling is unnecessarily heavy and in-kernel implementation
is lighter and better coordinated (workqueue, timer slack).
This patch implements framework for in-kernel disk event handling,
which includes media presence polling.
* bdops->check_events() is added, which supercedes ->media_changed().
It should check whether there's any pending event and return if so.
Currently, two events are defined - DISK_EVENT_MEDIA_CHANGE and
DISK_EVENT_EJECT_REQUEST. ->check_events() is guaranteed not to be
called parallelly.
* gendisk->events and ->async_events are added. These should be
initialized by block driver before passing the device to add_disk().
The former contains the mask of all supported events and the latter
the mask of all events which the device can report without polling.
/sys/block/*/events[_async] export these to userland.
* Kernel parameter block.events_dfl_poll_msecs controls the system
polling interval (default is 0 which means disable) and
/sys/block/*/events_poll_msecs control polling intervals for
individual devices (default is -1 meaning use system setting). Note
that if a device can report all supported events asynchronously and
its polling interval isn't explicitly set, the device won't be
polled regardless of the system polling interval.
* If a device is opened exclusively with write access, event checking
is automatically disabled until all write exclusive accesses are
released.
* There are event 'clearing' events. For example, both of currently
defined events are cleared after the device has been successfully
opened. This information is passed to ->check_events() callback
using @clearing argument as a hint.
* Event checking is always performed from system_nrt_wq and timer
slack is set to 25% for polling.
* Nothing changes for drivers which implement ->media_changed() but
not ->check_events(). Going forward, all drivers will be converted
to ->check_events() and ->media_change() will be dropped.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
2010-12-09 03:57:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-07-01 22:17:47 +08:00
|
|
|
* disk_flush_events - schedule immediate event checking and flushing
|
|
|
|
* @disk: disk to check and flush events for
|
|
|
|
* @mask: events to flush
|
implement in-kernel gendisk events handling
Currently, media presence polling for removeable block devices is done
from userland. There are several issues with this.
* Polling is done by periodically opening the device. For SCSI
devices, the command sequence generated by such action involves a
few different commands including TEST_UNIT_READY. This behavior,
while perfectly legal, is different from Windows which only issues
single command, GET_EVENT_STATUS_NOTIFICATION. Unfortunately, some
ATAPI devices lock up after being periodically queried such command
sequences.
* There is no reliable and unintrusive way for a userland program to
tell whether the target device is safe for media presence polling.
For example, polling for media presence during an on-going burning
session can make it fail. The polling program can avoid this by
opening the device with O_EXCL but then it risks making a valid
exclusive user of the device fail w/ -EBUSY.
* Userland polling is unnecessarily heavy and in-kernel implementation
is lighter and better coordinated (workqueue, timer slack).
This patch implements framework for in-kernel disk event handling,
which includes media presence polling.
* bdops->check_events() is added, which supercedes ->media_changed().
It should check whether there's any pending event and return if so.
Currently, two events are defined - DISK_EVENT_MEDIA_CHANGE and
DISK_EVENT_EJECT_REQUEST. ->check_events() is guaranteed not to be
called parallelly.
* gendisk->events and ->async_events are added. These should be
initialized by block driver before passing the device to add_disk().
The former contains the mask of all supported events and the latter
the mask of all events which the device can report without polling.
/sys/block/*/events[_async] export these to userland.
* Kernel parameter block.events_dfl_poll_msecs controls the system
polling interval (default is 0 which means disable) and
/sys/block/*/events_poll_msecs control polling intervals for
individual devices (default is -1 meaning use system setting). Note
that if a device can report all supported events asynchronously and
its polling interval isn't explicitly set, the device won't be
polled regardless of the system polling interval.
* If a device is opened exclusively with write access, event checking
is automatically disabled until all write exclusive accesses are
released.
* There are event 'clearing' events. For example, both of currently
defined events are cleared after the device has been successfully
opened. This information is passed to ->check_events() callback
using @clearing argument as a hint.
* Event checking is always performed from system_nrt_wq and timer
slack is set to 25% for polling.
* Nothing changes for drivers which implement ->media_changed() but
not ->check_events(). Going forward, all drivers will be converted
to ->check_events() and ->media_change() will be dropped.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
2010-12-09 03:57:37 +08:00
|
|
|
*
|
2011-07-01 22:17:47 +08:00
|
|
|
* Schedule immediate event checking on @disk if not blocked. Events in
|
|
|
|
* @mask are scheduled to be cleared from the driver. Note that this
|
|
|
|
* doesn't clear the events from @disk->ev.
|
implement in-kernel gendisk events handling
Currently, media presence polling for removeable block devices is done
from userland. There are several issues with this.
* Polling is done by periodically opening the device. For SCSI
devices, the command sequence generated by such action involves a
few different commands including TEST_UNIT_READY. This behavior,
while perfectly legal, is different from Windows which only issues
single command, GET_EVENT_STATUS_NOTIFICATION. Unfortunately, some
ATAPI devices lock up after being periodically queried such command
sequences.
* There is no reliable and unintrusive way for a userland program to
tell whether the target device is safe for media presence polling.
For example, polling for media presence during an on-going burning
session can make it fail. The polling program can avoid this by
opening the device with O_EXCL but then it risks making a valid
exclusive user of the device fail w/ -EBUSY.
* Userland polling is unnecessarily heavy and in-kernel implementation
is lighter and better coordinated (workqueue, timer slack).
This patch implements framework for in-kernel disk event handling,
which includes media presence polling.
* bdops->check_events() is added, which supercedes ->media_changed().
It should check whether there's any pending event and return if so.
Currently, two events are defined - DISK_EVENT_MEDIA_CHANGE and
DISK_EVENT_EJECT_REQUEST. ->check_events() is guaranteed not to be
called parallelly.
* gendisk->events and ->async_events are added. These should be
initialized by block driver before passing the device to add_disk().
The former contains the mask of all supported events and the latter
the mask of all events which the device can report without polling.
/sys/block/*/events[_async] export these to userland.
* Kernel parameter block.events_dfl_poll_msecs controls the system
polling interval (default is 0 which means disable) and
/sys/block/*/events_poll_msecs control polling intervals for
individual devices (default is -1 meaning use system setting). Note
that if a device can report all supported events asynchronously and
its polling interval isn't explicitly set, the device won't be
polled regardless of the system polling interval.
* If a device is opened exclusively with write access, event checking
is automatically disabled until all write exclusive accesses are
released.
* There are event 'clearing' events. For example, both of currently
defined events are cleared after the device has been successfully
opened. This information is passed to ->check_events() callback
using @clearing argument as a hint.
* Event checking is always performed from system_nrt_wq and timer
slack is set to 25% for polling.
* Nothing changes for drivers which implement ->media_changed() but
not ->check_events(). Going forward, all drivers will be converted
to ->check_events() and ->media_change() will be dropped.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
2010-12-09 03:57:37 +08:00
|
|
|
*
|
|
|
|
* CONTEXT:
|
2011-07-01 22:17:47 +08:00
|
|
|
* If @mask is non-zero must be called with bdev->bd_mutex held.
|
implement in-kernel gendisk events handling
Currently, media presence polling for removeable block devices is done
from userland. There are several issues with this.
* Polling is done by periodically opening the device. For SCSI
devices, the command sequence generated by such action involves a
few different commands including TEST_UNIT_READY. This behavior,
while perfectly legal, is different from Windows which only issues
single command, GET_EVENT_STATUS_NOTIFICATION. Unfortunately, some
ATAPI devices lock up after being periodically queried such command
sequences.
* There is no reliable and unintrusive way for a userland program to
tell whether the target device is safe for media presence polling.
For example, polling for media presence during an on-going burning
session can make it fail. The polling program can avoid this by
opening the device with O_EXCL but then it risks making a valid
exclusive user of the device fail w/ -EBUSY.
* Userland polling is unnecessarily heavy and in-kernel implementation
is lighter and better coordinated (workqueue, timer slack).
This patch implements framework for in-kernel disk event handling,
which includes media presence polling.
* bdops->check_events() is added, which supercedes ->media_changed().
It should check whether there's any pending event and return if so.
Currently, two events are defined - DISK_EVENT_MEDIA_CHANGE and
DISK_EVENT_EJECT_REQUEST. ->check_events() is guaranteed not to be
called parallelly.
* gendisk->events and ->async_events are added. These should be
initialized by block driver before passing the device to add_disk().
The former contains the mask of all supported events and the latter
the mask of all events which the device can report without polling.
/sys/block/*/events[_async] export these to userland.
* Kernel parameter block.events_dfl_poll_msecs controls the system
polling interval (default is 0 which means disable) and
/sys/block/*/events_poll_msecs control polling intervals for
individual devices (default is -1 meaning use system setting). Note
that if a device can report all supported events asynchronously and
its polling interval isn't explicitly set, the device won't be
polled regardless of the system polling interval.
* If a device is opened exclusively with write access, event checking
is automatically disabled until all write exclusive accesses are
released.
* There are event 'clearing' events. For example, both of currently
defined events are cleared after the device has been successfully
opened. This information is passed to ->check_events() callback
using @clearing argument as a hint.
* Event checking is always performed from system_nrt_wq and timer
slack is set to 25% for polling.
* Nothing changes for drivers which implement ->media_changed() but
not ->check_events(). Going forward, all drivers will be converted
to ->check_events() and ->media_change() will be dropped.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
2010-12-09 03:57:37 +08:00
|
|
|
*/
|
2011-07-01 22:17:47 +08:00
|
|
|
void disk_flush_events(struct gendisk *disk, unsigned int mask)
|
implement in-kernel gendisk events handling
Currently, media presence polling for removeable block devices is done
from userland. There are several issues with this.
* Polling is done by periodically opening the device. For SCSI
devices, the command sequence generated by such action involves a
few different commands including TEST_UNIT_READY. This behavior,
while perfectly legal, is different from Windows which only issues
single command, GET_EVENT_STATUS_NOTIFICATION. Unfortunately, some
ATAPI devices lock up after being periodically queried such command
sequences.
* There is no reliable and unintrusive way for a userland program to
tell whether the target device is safe for media presence polling.
For example, polling for media presence during an on-going burning
session can make it fail. The polling program can avoid this by
opening the device with O_EXCL but then it risks making a valid
exclusive user of the device fail w/ -EBUSY.
* Userland polling is unnecessarily heavy and in-kernel implementation
is lighter and better coordinated (workqueue, timer slack).
This patch implements framework for in-kernel disk event handling,
which includes media presence polling.
* bdops->check_events() is added, which supercedes ->media_changed().
It should check whether there's any pending event and return if so.
Currently, two events are defined - DISK_EVENT_MEDIA_CHANGE and
DISK_EVENT_EJECT_REQUEST. ->check_events() is guaranteed not to be
called parallelly.
* gendisk->events and ->async_events are added. These should be
initialized by block driver before passing the device to add_disk().
The former contains the mask of all supported events and the latter
the mask of all events which the device can report without polling.
/sys/block/*/events[_async] export these to userland.
* Kernel parameter block.events_dfl_poll_msecs controls the system
polling interval (default is 0 which means disable) and
/sys/block/*/events_poll_msecs control polling intervals for
individual devices (default is -1 meaning use system setting). Note
that if a device can report all supported events asynchronously and
its polling interval isn't explicitly set, the device won't be
polled regardless of the system polling interval.
* If a device is opened exclusively with write access, event checking
is automatically disabled until all write exclusive accesses are
released.
* There are event 'clearing' events. For example, both of currently
defined events are cleared after the device has been successfully
opened. This information is passed to ->check_events() callback
using @clearing argument as a hint.
* Event checking is always performed from system_nrt_wq and timer
slack is set to 25% for polling.
* Nothing changes for drivers which implement ->media_changed() but
not ->check_events(). Going forward, all drivers will be converted
to ->check_events() and ->media_change() will be dropped.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
2010-12-09 03:57:37 +08:00
|
|
|
{
|
2011-06-10 02:43:54 +08:00
|
|
|
struct disk_events *ev = disk->ev;
|
|
|
|
|
|
|
|
if (!ev)
|
|
|
|
return;
|
|
|
|
|
2011-07-01 22:17:47 +08:00
|
|
|
spin_lock_irq(&ev->lock);
|
|
|
|
ev->clearing |= mask;
|
2012-08-04 01:30:47 +08:00
|
|
|
if (!ev->block)
|
2013-04-24 19:42:56 +08:00
|
|
|
mod_delayed_work(system_freezable_power_efficient_wq,
|
|
|
|
&ev->dwork, 0);
|
2011-07-01 22:17:47 +08:00
|
|
|
spin_unlock_irq(&ev->lock);
|
implement in-kernel gendisk events handling
Currently, media presence polling for removeable block devices is done
from userland. There are several issues with this.
* Polling is done by periodically opening the device. For SCSI
devices, the command sequence generated by such action involves a
few different commands including TEST_UNIT_READY. This behavior,
while perfectly legal, is different from Windows which only issues
single command, GET_EVENT_STATUS_NOTIFICATION. Unfortunately, some
ATAPI devices lock up after being periodically queried such command
sequences.
* There is no reliable and unintrusive way for a userland program to
tell whether the target device is safe for media presence polling.
For example, polling for media presence during an on-going burning
session can make it fail. The polling program can avoid this by
opening the device with O_EXCL but then it risks making a valid
exclusive user of the device fail w/ -EBUSY.
* Userland polling is unnecessarily heavy and in-kernel implementation
is lighter and better coordinated (workqueue, timer slack).
This patch implements framework for in-kernel disk event handling,
which includes media presence polling.
* bdops->check_events() is added, which supercedes ->media_changed().
It should check whether there's any pending event and return if so.
Currently, two events are defined - DISK_EVENT_MEDIA_CHANGE and
DISK_EVENT_EJECT_REQUEST. ->check_events() is guaranteed not to be
called parallelly.
* gendisk->events and ->async_events are added. These should be
initialized by block driver before passing the device to add_disk().
The former contains the mask of all supported events and the latter
the mask of all events which the device can report without polling.
/sys/block/*/events[_async] export these to userland.
* Kernel parameter block.events_dfl_poll_msecs controls the system
polling interval (default is 0 which means disable) and
/sys/block/*/events_poll_msecs control polling intervals for
individual devices (default is -1 meaning use system setting). Note
that if a device can report all supported events asynchronously and
its polling interval isn't explicitly set, the device won't be
polled regardless of the system polling interval.
* If a device is opened exclusively with write access, event checking
is automatically disabled until all write exclusive accesses are
released.
* There are event 'clearing' events. For example, both of currently
defined events are cleared after the device has been successfully
opened. This information is passed to ->check_events() callback
using @clearing argument as a hint.
* Event checking is always performed from system_nrt_wq and timer
slack is set to 25% for polling.
* Nothing changes for drivers which implement ->media_changed() but
not ->check_events(). Going forward, all drivers will be converted
to ->check_events() and ->media_change() will be dropped.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
2010-12-09 03:57:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* disk_clear_events - synchronously check, clear and return pending events
|
|
|
|
* @disk: disk to fetch and clear events from
|
2014-09-09 00:27:23 +08:00
|
|
|
* @mask: mask of events to be fetched and cleared
|
implement in-kernel gendisk events handling
Currently, media presence polling for removeable block devices is done
from userland. There are several issues with this.
* Polling is done by periodically opening the device. For SCSI
devices, the command sequence generated by such action involves a
few different commands including TEST_UNIT_READY. This behavior,
while perfectly legal, is different from Windows which only issues
single command, GET_EVENT_STATUS_NOTIFICATION. Unfortunately, some
ATAPI devices lock up after being periodically queried such command
sequences.
* There is no reliable and unintrusive way for a userland program to
tell whether the target device is safe for media presence polling.
For example, polling for media presence during an on-going burning
session can make it fail. The polling program can avoid this by
opening the device with O_EXCL but then it risks making a valid
exclusive user of the device fail w/ -EBUSY.
* Userland polling is unnecessarily heavy and in-kernel implementation
is lighter and better coordinated (workqueue, timer slack).
This patch implements framework for in-kernel disk event handling,
which includes media presence polling.
* bdops->check_events() is added, which supercedes ->media_changed().
It should check whether there's any pending event and return if so.
Currently, two events are defined - DISK_EVENT_MEDIA_CHANGE and
DISK_EVENT_EJECT_REQUEST. ->check_events() is guaranteed not to be
called parallelly.
* gendisk->events and ->async_events are added. These should be
initialized by block driver before passing the device to add_disk().
The former contains the mask of all supported events and the latter
the mask of all events which the device can report without polling.
/sys/block/*/events[_async] export these to userland.
* Kernel parameter block.events_dfl_poll_msecs controls the system
polling interval (default is 0 which means disable) and
/sys/block/*/events_poll_msecs control polling intervals for
individual devices (default is -1 meaning use system setting). Note
that if a device can report all supported events asynchronously and
its polling interval isn't explicitly set, the device won't be
polled regardless of the system polling interval.
* If a device is opened exclusively with write access, event checking
is automatically disabled until all write exclusive accesses are
released.
* There are event 'clearing' events. For example, both of currently
defined events are cleared after the device has been successfully
opened. This information is passed to ->check_events() callback
using @clearing argument as a hint.
* Event checking is always performed from system_nrt_wq and timer
slack is set to 25% for polling.
* Nothing changes for drivers which implement ->media_changed() but
not ->check_events(). Going forward, all drivers will be converted
to ->check_events() and ->media_change() will be dropped.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
2010-12-09 03:57:37 +08:00
|
|
|
*
|
|
|
|
* Disk events are synchronously checked and pending events in @mask
|
|
|
|
* are cleared and returned. This ignores the block count.
|
|
|
|
*
|
|
|
|
* CONTEXT:
|
|
|
|
* Might sleep.
|
|
|
|
*/
|
|
|
|
unsigned int disk_clear_events(struct gendisk *disk, unsigned int mask)
|
|
|
|
{
|
|
|
|
const struct block_device_operations *bdops = disk->fops;
|
|
|
|
struct disk_events *ev = disk->ev;
|
|
|
|
unsigned int pending;
|
2012-12-19 04:27:20 +08:00
|
|
|
unsigned int clearing = mask;
|
implement in-kernel gendisk events handling
Currently, media presence polling for removeable block devices is done
from userland. There are several issues with this.
* Polling is done by periodically opening the device. For SCSI
devices, the command sequence generated by such action involves a
few different commands including TEST_UNIT_READY. This behavior,
while perfectly legal, is different from Windows which only issues
single command, GET_EVENT_STATUS_NOTIFICATION. Unfortunately, some
ATAPI devices lock up after being periodically queried such command
sequences.
* There is no reliable and unintrusive way for a userland program to
tell whether the target device is safe for media presence polling.
For example, polling for media presence during an on-going burning
session can make it fail. The polling program can avoid this by
opening the device with O_EXCL but then it risks making a valid
exclusive user of the device fail w/ -EBUSY.
* Userland polling is unnecessarily heavy and in-kernel implementation
is lighter and better coordinated (workqueue, timer slack).
This patch implements framework for in-kernel disk event handling,
which includes media presence polling.
* bdops->check_events() is added, which supercedes ->media_changed().
It should check whether there's any pending event and return if so.
Currently, two events are defined - DISK_EVENT_MEDIA_CHANGE and
DISK_EVENT_EJECT_REQUEST. ->check_events() is guaranteed not to be
called parallelly.
* gendisk->events and ->async_events are added. These should be
initialized by block driver before passing the device to add_disk().
The former contains the mask of all supported events and the latter
the mask of all events which the device can report without polling.
/sys/block/*/events[_async] export these to userland.
* Kernel parameter block.events_dfl_poll_msecs controls the system
polling interval (default is 0 which means disable) and
/sys/block/*/events_poll_msecs control polling intervals for
individual devices (default is -1 meaning use system setting). Note
that if a device can report all supported events asynchronously and
its polling interval isn't explicitly set, the device won't be
polled regardless of the system polling interval.
* If a device is opened exclusively with write access, event checking
is automatically disabled until all write exclusive accesses are
released.
* There are event 'clearing' events. For example, both of currently
defined events are cleared after the device has been successfully
opened. This information is passed to ->check_events() callback
using @clearing argument as a hint.
* Event checking is always performed from system_nrt_wq and timer
slack is set to 25% for polling.
* Nothing changes for drivers which implement ->media_changed() but
not ->check_events(). Going forward, all drivers will be converted
to ->check_events() and ->media_change() will be dropped.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
2010-12-09 03:57:37 +08:00
|
|
|
|
|
|
|
if (!ev) {
|
|
|
|
/* for drivers still using the old ->media_changed method */
|
|
|
|
if ((mask & DISK_EVENT_MEDIA_CHANGE) &&
|
|
|
|
bdops->media_changed && bdops->media_changed(disk))
|
|
|
|
return DISK_EVENT_MEDIA_CHANGE;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-12-19 04:27:20 +08:00
|
|
|
disk_block_events(disk);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* store the union of mask and ev->clearing on the stack so that the
|
|
|
|
* race with disk_flush_events does not cause ambiguity (ev->clearing
|
|
|
|
* can still be modified even if events are blocked).
|
|
|
|
*/
|
implement in-kernel gendisk events handling
Currently, media presence polling for removeable block devices is done
from userland. There are several issues with this.
* Polling is done by periodically opening the device. For SCSI
devices, the command sequence generated by such action involves a
few different commands including TEST_UNIT_READY. This behavior,
while perfectly legal, is different from Windows which only issues
single command, GET_EVENT_STATUS_NOTIFICATION. Unfortunately, some
ATAPI devices lock up after being periodically queried such command
sequences.
* There is no reliable and unintrusive way for a userland program to
tell whether the target device is safe for media presence polling.
For example, polling for media presence during an on-going burning
session can make it fail. The polling program can avoid this by
opening the device with O_EXCL but then it risks making a valid
exclusive user of the device fail w/ -EBUSY.
* Userland polling is unnecessarily heavy and in-kernel implementation
is lighter and better coordinated (workqueue, timer slack).
This patch implements framework for in-kernel disk event handling,
which includes media presence polling.
* bdops->check_events() is added, which supercedes ->media_changed().
It should check whether there's any pending event and return if so.
Currently, two events are defined - DISK_EVENT_MEDIA_CHANGE and
DISK_EVENT_EJECT_REQUEST. ->check_events() is guaranteed not to be
called parallelly.
* gendisk->events and ->async_events are added. These should be
initialized by block driver before passing the device to add_disk().
The former contains the mask of all supported events and the latter
the mask of all events which the device can report without polling.
/sys/block/*/events[_async] export these to userland.
* Kernel parameter block.events_dfl_poll_msecs controls the system
polling interval (default is 0 which means disable) and
/sys/block/*/events_poll_msecs control polling intervals for
individual devices (default is -1 meaning use system setting). Note
that if a device can report all supported events asynchronously and
its polling interval isn't explicitly set, the device won't be
polled regardless of the system polling interval.
* If a device is opened exclusively with write access, event checking
is automatically disabled until all write exclusive accesses are
released.
* There are event 'clearing' events. For example, both of currently
defined events are cleared after the device has been successfully
opened. This information is passed to ->check_events() callback
using @clearing argument as a hint.
* Event checking is always performed from system_nrt_wq and timer
slack is set to 25% for polling.
* Nothing changes for drivers which implement ->media_changed() but
not ->check_events(). Going forward, all drivers will be converted
to ->check_events() and ->media_change() will be dropped.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
2010-12-09 03:57:37 +08:00
|
|
|
spin_lock_irq(&ev->lock);
|
2012-12-19 04:27:20 +08:00
|
|
|
clearing |= ev->clearing;
|
|
|
|
ev->clearing = 0;
|
implement in-kernel gendisk events handling
Currently, media presence polling for removeable block devices is done
from userland. There are several issues with this.
* Polling is done by periodically opening the device. For SCSI
devices, the command sequence generated by such action involves a
few different commands including TEST_UNIT_READY. This behavior,
while perfectly legal, is different from Windows which only issues
single command, GET_EVENT_STATUS_NOTIFICATION. Unfortunately, some
ATAPI devices lock up after being periodically queried such command
sequences.
* There is no reliable and unintrusive way for a userland program to
tell whether the target device is safe for media presence polling.
For example, polling for media presence during an on-going burning
session can make it fail. The polling program can avoid this by
opening the device with O_EXCL but then it risks making a valid
exclusive user of the device fail w/ -EBUSY.
* Userland polling is unnecessarily heavy and in-kernel implementation
is lighter and better coordinated (workqueue, timer slack).
This patch implements framework for in-kernel disk event handling,
which includes media presence polling.
* bdops->check_events() is added, which supercedes ->media_changed().
It should check whether there's any pending event and return if so.
Currently, two events are defined - DISK_EVENT_MEDIA_CHANGE and
DISK_EVENT_EJECT_REQUEST. ->check_events() is guaranteed not to be
called parallelly.
* gendisk->events and ->async_events are added. These should be
initialized by block driver before passing the device to add_disk().
The former contains the mask of all supported events and the latter
the mask of all events which the device can report without polling.
/sys/block/*/events[_async] export these to userland.
* Kernel parameter block.events_dfl_poll_msecs controls the system
polling interval (default is 0 which means disable) and
/sys/block/*/events_poll_msecs control polling intervals for
individual devices (default is -1 meaning use system setting). Note
that if a device can report all supported events asynchronously and
its polling interval isn't explicitly set, the device won't be
polled regardless of the system polling interval.
* If a device is opened exclusively with write access, event checking
is automatically disabled until all write exclusive accesses are
released.
* There are event 'clearing' events. For example, both of currently
defined events are cleared after the device has been successfully
opened. This information is passed to ->check_events() callback
using @clearing argument as a hint.
* Event checking is always performed from system_nrt_wq and timer
slack is set to 25% for polling.
* Nothing changes for drivers which implement ->media_changed() but
not ->check_events(). Going forward, all drivers will be converted
to ->check_events() and ->media_change() will be dropped.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
2010-12-09 03:57:37 +08:00
|
|
|
spin_unlock_irq(&ev->lock);
|
|
|
|
|
2012-12-19 04:27:20 +08:00
|
|
|
disk_check_events(ev, &clearing);
|
block: remove deadlock in disk_clear_events
In disk_clear_events, do not put work on system_nrt_freezable_wq.
Instead, put it on system_nrt_wq.
There is a race between probing a usb and suspending the device. Since
probing a usb calls disk_clear_events, which puts work on a frozen
workqueue, probing cannot finish after the workqueue is frozen. However,
suspending cannot finish until the usb probe is finished, so we get a
deadlock, causing the system to reboot.
The way to reproduce this bug is to wake up from suspend with a usb
storage device plugged in, or plugging in a usb storage device right
before suspend. The window of time is on the order of time it takes to
probe the usb device. As long as the workqueues are frozen before the
call to add_disk within sd_probe_async finishes, there will be a deadlock
(which calls blkdev_get, sd_open, check_disk_change, then
disk_clear_events). This is not difficult to reproduce after figuring out
the timings.
[akpm@linux-foundation.org: fix up comment]
Signed-off-by: Derek Basehore <dbasehore@chromium.org>
Reviewed-by: Mandeep Singh Baines <msb@chromium.org>
Cc: Jens Axboe <axboe@kernel.dk>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2012-12-19 04:27:18 +08:00
|
|
|
/*
|
2012-12-19 04:27:20 +08:00
|
|
|
* if ev->clearing is not 0, the disk_flush_events got called in the
|
|
|
|
* middle of this function, so we want to run the workfn without delay.
|
block: remove deadlock in disk_clear_events
In disk_clear_events, do not put work on system_nrt_freezable_wq.
Instead, put it on system_nrt_wq.
There is a race between probing a usb and suspending the device. Since
probing a usb calls disk_clear_events, which puts work on a frozen
workqueue, probing cannot finish after the workqueue is frozen. However,
suspending cannot finish until the usb probe is finished, so we get a
deadlock, causing the system to reboot.
The way to reproduce this bug is to wake up from suspend with a usb
storage device plugged in, or plugging in a usb storage device right
before suspend. The window of time is on the order of time it takes to
probe the usb device. As long as the workqueues are frozen before the
call to add_disk within sd_probe_async finishes, there will be a deadlock
(which calls blkdev_get, sd_open, check_disk_change, then
disk_clear_events). This is not difficult to reproduce after figuring out
the timings.
[akpm@linux-foundation.org: fix up comment]
Signed-off-by: Derek Basehore <dbasehore@chromium.org>
Reviewed-by: Mandeep Singh Baines <msb@chromium.org>
Cc: Jens Axboe <axboe@kernel.dk>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2012-12-19 04:27:18 +08:00
|
|
|
*/
|
2012-12-19 04:27:20 +08:00
|
|
|
__disk_unblock_events(disk, ev->clearing ? true : false);
|
implement in-kernel gendisk events handling
Currently, media presence polling for removeable block devices is done
from userland. There are several issues with this.
* Polling is done by periodically opening the device. For SCSI
devices, the command sequence generated by such action involves a
few different commands including TEST_UNIT_READY. This behavior,
while perfectly legal, is different from Windows which only issues
single command, GET_EVENT_STATUS_NOTIFICATION. Unfortunately, some
ATAPI devices lock up after being periodically queried such command
sequences.
* There is no reliable and unintrusive way for a userland program to
tell whether the target device is safe for media presence polling.
For example, polling for media presence during an on-going burning
session can make it fail. The polling program can avoid this by
opening the device with O_EXCL but then it risks making a valid
exclusive user of the device fail w/ -EBUSY.
* Userland polling is unnecessarily heavy and in-kernel implementation
is lighter and better coordinated (workqueue, timer slack).
This patch implements framework for in-kernel disk event handling,
which includes media presence polling.
* bdops->check_events() is added, which supercedes ->media_changed().
It should check whether there's any pending event and return if so.
Currently, two events are defined - DISK_EVENT_MEDIA_CHANGE and
DISK_EVENT_EJECT_REQUEST. ->check_events() is guaranteed not to be
called parallelly.
* gendisk->events and ->async_events are added. These should be
initialized by block driver before passing the device to add_disk().
The former contains the mask of all supported events and the latter
the mask of all events which the device can report without polling.
/sys/block/*/events[_async] export these to userland.
* Kernel parameter block.events_dfl_poll_msecs controls the system
polling interval (default is 0 which means disable) and
/sys/block/*/events_poll_msecs control polling intervals for
individual devices (default is -1 meaning use system setting). Note
that if a device can report all supported events asynchronously and
its polling interval isn't explicitly set, the device won't be
polled regardless of the system polling interval.
* If a device is opened exclusively with write access, event checking
is automatically disabled until all write exclusive accesses are
released.
* There are event 'clearing' events. For example, both of currently
defined events are cleared after the device has been successfully
opened. This information is passed to ->check_events() callback
using @clearing argument as a hint.
* Event checking is always performed from system_nrt_wq and timer
slack is set to 25% for polling.
* Nothing changes for drivers which implement ->media_changed() but
not ->check_events(). Going forward, all drivers will be converted
to ->check_events() and ->media_change() will be dropped.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
2010-12-09 03:57:37 +08:00
|
|
|
|
|
|
|
/* then, fetch and clear pending events */
|
|
|
|
spin_lock_irq(&ev->lock);
|
|
|
|
pending = ev->pending & mask;
|
|
|
|
ev->pending &= ~mask;
|
|
|
|
spin_unlock_irq(&ev->lock);
|
2012-12-19 04:27:20 +08:00
|
|
|
WARN_ON_ONCE(clearing & mask);
|
implement in-kernel gendisk events handling
Currently, media presence polling for removeable block devices is done
from userland. There are several issues with this.
* Polling is done by periodically opening the device. For SCSI
devices, the command sequence generated by such action involves a
few different commands including TEST_UNIT_READY. This behavior,
while perfectly legal, is different from Windows which only issues
single command, GET_EVENT_STATUS_NOTIFICATION. Unfortunately, some
ATAPI devices lock up after being periodically queried such command
sequences.
* There is no reliable and unintrusive way for a userland program to
tell whether the target device is safe for media presence polling.
For example, polling for media presence during an on-going burning
session can make it fail. The polling program can avoid this by
opening the device with O_EXCL but then it risks making a valid
exclusive user of the device fail w/ -EBUSY.
* Userland polling is unnecessarily heavy and in-kernel implementation
is lighter and better coordinated (workqueue, timer slack).
This patch implements framework for in-kernel disk event handling,
which includes media presence polling.
* bdops->check_events() is added, which supercedes ->media_changed().
It should check whether there's any pending event and return if so.
Currently, two events are defined - DISK_EVENT_MEDIA_CHANGE and
DISK_EVENT_EJECT_REQUEST. ->check_events() is guaranteed not to be
called parallelly.
* gendisk->events and ->async_events are added. These should be
initialized by block driver before passing the device to add_disk().
The former contains the mask of all supported events and the latter
the mask of all events which the device can report without polling.
/sys/block/*/events[_async] export these to userland.
* Kernel parameter block.events_dfl_poll_msecs controls the system
polling interval (default is 0 which means disable) and
/sys/block/*/events_poll_msecs control polling intervals for
individual devices (default is -1 meaning use system setting). Note
that if a device can report all supported events asynchronously and
its polling interval isn't explicitly set, the device won't be
polled regardless of the system polling interval.
* If a device is opened exclusively with write access, event checking
is automatically disabled until all write exclusive accesses are
released.
* There are event 'clearing' events. For example, both of currently
defined events are cleared after the device has been successfully
opened. This information is passed to ->check_events() callback
using @clearing argument as a hint.
* Event checking is always performed from system_nrt_wq and timer
slack is set to 25% for polling.
* Nothing changes for drivers which implement ->media_changed() but
not ->check_events(). Going forward, all drivers will be converted
to ->check_events() and ->media_change() will be dropped.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
2010-12-09 03:57:37 +08:00
|
|
|
|
|
|
|
return pending;
|
|
|
|
}
|
|
|
|
|
2012-12-19 04:27:20 +08:00
|
|
|
/*
|
|
|
|
* Separate this part out so that a different pointer for clearing_ptr can be
|
|
|
|
* passed in for disk_clear_events.
|
|
|
|
*/
|
implement in-kernel gendisk events handling
Currently, media presence polling for removeable block devices is done
from userland. There are several issues with this.
* Polling is done by periodically opening the device. For SCSI
devices, the command sequence generated by such action involves a
few different commands including TEST_UNIT_READY. This behavior,
while perfectly legal, is different from Windows which only issues
single command, GET_EVENT_STATUS_NOTIFICATION. Unfortunately, some
ATAPI devices lock up after being periodically queried such command
sequences.
* There is no reliable and unintrusive way for a userland program to
tell whether the target device is safe for media presence polling.
For example, polling for media presence during an on-going burning
session can make it fail. The polling program can avoid this by
opening the device with O_EXCL but then it risks making a valid
exclusive user of the device fail w/ -EBUSY.
* Userland polling is unnecessarily heavy and in-kernel implementation
is lighter and better coordinated (workqueue, timer slack).
This patch implements framework for in-kernel disk event handling,
which includes media presence polling.
* bdops->check_events() is added, which supercedes ->media_changed().
It should check whether there's any pending event and return if so.
Currently, two events are defined - DISK_EVENT_MEDIA_CHANGE and
DISK_EVENT_EJECT_REQUEST. ->check_events() is guaranteed not to be
called parallelly.
* gendisk->events and ->async_events are added. These should be
initialized by block driver before passing the device to add_disk().
The former contains the mask of all supported events and the latter
the mask of all events which the device can report without polling.
/sys/block/*/events[_async] export these to userland.
* Kernel parameter block.events_dfl_poll_msecs controls the system
polling interval (default is 0 which means disable) and
/sys/block/*/events_poll_msecs control polling intervals for
individual devices (default is -1 meaning use system setting). Note
that if a device can report all supported events asynchronously and
its polling interval isn't explicitly set, the device won't be
polled regardless of the system polling interval.
* If a device is opened exclusively with write access, event checking
is automatically disabled until all write exclusive accesses are
released.
* There are event 'clearing' events. For example, both of currently
defined events are cleared after the device has been successfully
opened. This information is passed to ->check_events() callback
using @clearing argument as a hint.
* Event checking is always performed from system_nrt_wq and timer
slack is set to 25% for polling.
* Nothing changes for drivers which implement ->media_changed() but
not ->check_events(). Going forward, all drivers will be converted
to ->check_events() and ->media_change() will be dropped.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
2010-12-09 03:57:37 +08:00
|
|
|
static void disk_events_workfn(struct work_struct *work)
|
|
|
|
{
|
|
|
|
struct delayed_work *dwork = to_delayed_work(work);
|
|
|
|
struct disk_events *ev = container_of(dwork, struct disk_events, dwork);
|
2012-12-19 04:27:20 +08:00
|
|
|
|
|
|
|
disk_check_events(ev, &ev->clearing);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void disk_check_events(struct disk_events *ev,
|
|
|
|
unsigned int *clearing_ptr)
|
|
|
|
{
|
implement in-kernel gendisk events handling
Currently, media presence polling for removeable block devices is done
from userland. There are several issues with this.
* Polling is done by periodically opening the device. For SCSI
devices, the command sequence generated by such action involves a
few different commands including TEST_UNIT_READY. This behavior,
while perfectly legal, is different from Windows which only issues
single command, GET_EVENT_STATUS_NOTIFICATION. Unfortunately, some
ATAPI devices lock up after being periodically queried such command
sequences.
* There is no reliable and unintrusive way for a userland program to
tell whether the target device is safe for media presence polling.
For example, polling for media presence during an on-going burning
session can make it fail. The polling program can avoid this by
opening the device with O_EXCL but then it risks making a valid
exclusive user of the device fail w/ -EBUSY.
* Userland polling is unnecessarily heavy and in-kernel implementation
is lighter and better coordinated (workqueue, timer slack).
This patch implements framework for in-kernel disk event handling,
which includes media presence polling.
* bdops->check_events() is added, which supercedes ->media_changed().
It should check whether there's any pending event and return if so.
Currently, two events are defined - DISK_EVENT_MEDIA_CHANGE and
DISK_EVENT_EJECT_REQUEST. ->check_events() is guaranteed not to be
called parallelly.
* gendisk->events and ->async_events are added. These should be
initialized by block driver before passing the device to add_disk().
The former contains the mask of all supported events and the latter
the mask of all events which the device can report without polling.
/sys/block/*/events[_async] export these to userland.
* Kernel parameter block.events_dfl_poll_msecs controls the system
polling interval (default is 0 which means disable) and
/sys/block/*/events_poll_msecs control polling intervals for
individual devices (default is -1 meaning use system setting). Note
that if a device can report all supported events asynchronously and
its polling interval isn't explicitly set, the device won't be
polled regardless of the system polling interval.
* If a device is opened exclusively with write access, event checking
is automatically disabled until all write exclusive accesses are
released.
* There are event 'clearing' events. For example, both of currently
defined events are cleared after the device has been successfully
opened. This information is passed to ->check_events() callback
using @clearing argument as a hint.
* Event checking is always performed from system_nrt_wq and timer
slack is set to 25% for polling.
* Nothing changes for drivers which implement ->media_changed() but
not ->check_events(). Going forward, all drivers will be converted
to ->check_events() and ->media_change() will be dropped.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
2010-12-09 03:57:37 +08:00
|
|
|
struct gendisk *disk = ev->disk;
|
|
|
|
char *envp[ARRAY_SIZE(disk_uevents) + 1] = { };
|
2012-12-19 04:27:20 +08:00
|
|
|
unsigned int clearing = *clearing_ptr;
|
implement in-kernel gendisk events handling
Currently, media presence polling for removeable block devices is done
from userland. There are several issues with this.
* Polling is done by periodically opening the device. For SCSI
devices, the command sequence generated by such action involves a
few different commands including TEST_UNIT_READY. This behavior,
while perfectly legal, is different from Windows which only issues
single command, GET_EVENT_STATUS_NOTIFICATION. Unfortunately, some
ATAPI devices lock up after being periodically queried such command
sequences.
* There is no reliable and unintrusive way for a userland program to
tell whether the target device is safe for media presence polling.
For example, polling for media presence during an on-going burning
session can make it fail. The polling program can avoid this by
opening the device with O_EXCL but then it risks making a valid
exclusive user of the device fail w/ -EBUSY.
* Userland polling is unnecessarily heavy and in-kernel implementation
is lighter and better coordinated (workqueue, timer slack).
This patch implements framework for in-kernel disk event handling,
which includes media presence polling.
* bdops->check_events() is added, which supercedes ->media_changed().
It should check whether there's any pending event and return if so.
Currently, two events are defined - DISK_EVENT_MEDIA_CHANGE and
DISK_EVENT_EJECT_REQUEST. ->check_events() is guaranteed not to be
called parallelly.
* gendisk->events and ->async_events are added. These should be
initialized by block driver before passing the device to add_disk().
The former contains the mask of all supported events and the latter
the mask of all events which the device can report without polling.
/sys/block/*/events[_async] export these to userland.
* Kernel parameter block.events_dfl_poll_msecs controls the system
polling interval (default is 0 which means disable) and
/sys/block/*/events_poll_msecs control polling intervals for
individual devices (default is -1 meaning use system setting). Note
that if a device can report all supported events asynchronously and
its polling interval isn't explicitly set, the device won't be
polled regardless of the system polling interval.
* If a device is opened exclusively with write access, event checking
is automatically disabled until all write exclusive accesses are
released.
* There are event 'clearing' events. For example, both of currently
defined events are cleared after the device has been successfully
opened. This information is passed to ->check_events() callback
using @clearing argument as a hint.
* Event checking is always performed from system_nrt_wq and timer
slack is set to 25% for polling.
* Nothing changes for drivers which implement ->media_changed() but
not ->check_events(). Going forward, all drivers will be converted
to ->check_events() and ->media_change() will be dropped.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
2010-12-09 03:57:37 +08:00
|
|
|
unsigned int events;
|
|
|
|
unsigned long intv;
|
|
|
|
int nr_events = 0, i;
|
|
|
|
|
|
|
|
/* check events */
|
|
|
|
events = disk->fops->check_events(disk, clearing);
|
|
|
|
|
|
|
|
/* accumulate pending events and schedule next poll if necessary */
|
|
|
|
spin_lock_irq(&ev->lock);
|
|
|
|
|
|
|
|
events &= ~ev->pending;
|
|
|
|
ev->pending |= events;
|
2012-12-19 04:27:20 +08:00
|
|
|
*clearing_ptr &= ~clearing;
|
implement in-kernel gendisk events handling
Currently, media presence polling for removeable block devices is done
from userland. There are several issues with this.
* Polling is done by periodically opening the device. For SCSI
devices, the command sequence generated by such action involves a
few different commands including TEST_UNIT_READY. This behavior,
while perfectly legal, is different from Windows which only issues
single command, GET_EVENT_STATUS_NOTIFICATION. Unfortunately, some
ATAPI devices lock up after being periodically queried such command
sequences.
* There is no reliable and unintrusive way for a userland program to
tell whether the target device is safe for media presence polling.
For example, polling for media presence during an on-going burning
session can make it fail. The polling program can avoid this by
opening the device with O_EXCL but then it risks making a valid
exclusive user of the device fail w/ -EBUSY.
* Userland polling is unnecessarily heavy and in-kernel implementation
is lighter and better coordinated (workqueue, timer slack).
This patch implements framework for in-kernel disk event handling,
which includes media presence polling.
* bdops->check_events() is added, which supercedes ->media_changed().
It should check whether there's any pending event and return if so.
Currently, two events are defined - DISK_EVENT_MEDIA_CHANGE and
DISK_EVENT_EJECT_REQUEST. ->check_events() is guaranteed not to be
called parallelly.
* gendisk->events and ->async_events are added. These should be
initialized by block driver before passing the device to add_disk().
The former contains the mask of all supported events and the latter
the mask of all events which the device can report without polling.
/sys/block/*/events[_async] export these to userland.
* Kernel parameter block.events_dfl_poll_msecs controls the system
polling interval (default is 0 which means disable) and
/sys/block/*/events_poll_msecs control polling intervals for
individual devices (default is -1 meaning use system setting). Note
that if a device can report all supported events asynchronously and
its polling interval isn't explicitly set, the device won't be
polled regardless of the system polling interval.
* If a device is opened exclusively with write access, event checking
is automatically disabled until all write exclusive accesses are
released.
* There are event 'clearing' events. For example, both of currently
defined events are cleared after the device has been successfully
opened. This information is passed to ->check_events() callback
using @clearing argument as a hint.
* Event checking is always performed from system_nrt_wq and timer
slack is set to 25% for polling.
* Nothing changes for drivers which implement ->media_changed() but
not ->check_events(). Going forward, all drivers will be converted
to ->check_events() and ->media_change() will be dropped.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
2010-12-09 03:57:37 +08:00
|
|
|
|
|
|
|
intv = disk_events_poll_jiffies(disk);
|
|
|
|
if (!ev->block && intv)
|
2013-04-24 19:42:56 +08:00
|
|
|
queue_delayed_work(system_freezable_power_efficient_wq,
|
|
|
|
&ev->dwork, intv);
|
implement in-kernel gendisk events handling
Currently, media presence polling for removeable block devices is done
from userland. There are several issues with this.
* Polling is done by periodically opening the device. For SCSI
devices, the command sequence generated by such action involves a
few different commands including TEST_UNIT_READY. This behavior,
while perfectly legal, is different from Windows which only issues
single command, GET_EVENT_STATUS_NOTIFICATION. Unfortunately, some
ATAPI devices lock up after being periodically queried such command
sequences.
* There is no reliable and unintrusive way for a userland program to
tell whether the target device is safe for media presence polling.
For example, polling for media presence during an on-going burning
session can make it fail. The polling program can avoid this by
opening the device with O_EXCL but then it risks making a valid
exclusive user of the device fail w/ -EBUSY.
* Userland polling is unnecessarily heavy and in-kernel implementation
is lighter and better coordinated (workqueue, timer slack).
This patch implements framework for in-kernel disk event handling,
which includes media presence polling.
* bdops->check_events() is added, which supercedes ->media_changed().
It should check whether there's any pending event and return if so.
Currently, two events are defined - DISK_EVENT_MEDIA_CHANGE and
DISK_EVENT_EJECT_REQUEST. ->check_events() is guaranteed not to be
called parallelly.
* gendisk->events and ->async_events are added. These should be
initialized by block driver before passing the device to add_disk().
The former contains the mask of all supported events and the latter
the mask of all events which the device can report without polling.
/sys/block/*/events[_async] export these to userland.
* Kernel parameter block.events_dfl_poll_msecs controls the system
polling interval (default is 0 which means disable) and
/sys/block/*/events_poll_msecs control polling intervals for
individual devices (default is -1 meaning use system setting). Note
that if a device can report all supported events asynchronously and
its polling interval isn't explicitly set, the device won't be
polled regardless of the system polling interval.
* If a device is opened exclusively with write access, event checking
is automatically disabled until all write exclusive accesses are
released.
* There are event 'clearing' events. For example, both of currently
defined events are cleared after the device has been successfully
opened. This information is passed to ->check_events() callback
using @clearing argument as a hint.
* Event checking is always performed from system_nrt_wq and timer
slack is set to 25% for polling.
* Nothing changes for drivers which implement ->media_changed() but
not ->check_events(). Going forward, all drivers will be converted
to ->check_events() and ->media_change() will be dropped.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
2010-12-09 03:57:37 +08:00
|
|
|
|
|
|
|
spin_unlock_irq(&ev->lock);
|
|
|
|
|
2011-04-22 01:43:58 +08:00
|
|
|
/*
|
|
|
|
* Tell userland about new events. Only the events listed in
|
2019-03-27 21:51:02 +08:00
|
|
|
* @disk->events are reported, and only if DISK_EVENT_FLAG_UEVENT
|
|
|
|
* is set. Otherwise, events are processed internally but never
|
|
|
|
* get reported to userland.
|
2011-04-22 01:43:58 +08:00
|
|
|
*/
|
implement in-kernel gendisk events handling
Currently, media presence polling for removeable block devices is done
from userland. There are several issues with this.
* Polling is done by periodically opening the device. For SCSI
devices, the command sequence generated by such action involves a
few different commands including TEST_UNIT_READY. This behavior,
while perfectly legal, is different from Windows which only issues
single command, GET_EVENT_STATUS_NOTIFICATION. Unfortunately, some
ATAPI devices lock up after being periodically queried such command
sequences.
* There is no reliable and unintrusive way for a userland program to
tell whether the target device is safe for media presence polling.
For example, polling for media presence during an on-going burning
session can make it fail. The polling program can avoid this by
opening the device with O_EXCL but then it risks making a valid
exclusive user of the device fail w/ -EBUSY.
* Userland polling is unnecessarily heavy and in-kernel implementation
is lighter and better coordinated (workqueue, timer slack).
This patch implements framework for in-kernel disk event handling,
which includes media presence polling.
* bdops->check_events() is added, which supercedes ->media_changed().
It should check whether there's any pending event and return if so.
Currently, two events are defined - DISK_EVENT_MEDIA_CHANGE and
DISK_EVENT_EJECT_REQUEST. ->check_events() is guaranteed not to be
called parallelly.
* gendisk->events and ->async_events are added. These should be
initialized by block driver before passing the device to add_disk().
The former contains the mask of all supported events and the latter
the mask of all events which the device can report without polling.
/sys/block/*/events[_async] export these to userland.
* Kernel parameter block.events_dfl_poll_msecs controls the system
polling interval (default is 0 which means disable) and
/sys/block/*/events_poll_msecs control polling intervals for
individual devices (default is -1 meaning use system setting). Note
that if a device can report all supported events asynchronously and
its polling interval isn't explicitly set, the device won't be
polled regardless of the system polling interval.
* If a device is opened exclusively with write access, event checking
is automatically disabled until all write exclusive accesses are
released.
* There are event 'clearing' events. For example, both of currently
defined events are cleared after the device has been successfully
opened. This information is passed to ->check_events() callback
using @clearing argument as a hint.
* Event checking is always performed from system_nrt_wq and timer
slack is set to 25% for polling.
* Nothing changes for drivers which implement ->media_changed() but
not ->check_events(). Going forward, all drivers will be converted
to ->check_events() and ->media_change() will be dropped.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
2010-12-09 03:57:37 +08:00
|
|
|
for (i = 0; i < ARRAY_SIZE(disk_uevents); i++)
|
2019-03-27 21:51:02 +08:00
|
|
|
if ((events & disk->events & (1 << i)) &&
|
|
|
|
(disk->event_flags & DISK_EVENT_FLAG_UEVENT))
|
implement in-kernel gendisk events handling
Currently, media presence polling for removeable block devices is done
from userland. There are several issues with this.
* Polling is done by periodically opening the device. For SCSI
devices, the command sequence generated by such action involves a
few different commands including TEST_UNIT_READY. This behavior,
while perfectly legal, is different from Windows which only issues
single command, GET_EVENT_STATUS_NOTIFICATION. Unfortunately, some
ATAPI devices lock up after being periodically queried such command
sequences.
* There is no reliable and unintrusive way for a userland program to
tell whether the target device is safe for media presence polling.
For example, polling for media presence during an on-going burning
session can make it fail. The polling program can avoid this by
opening the device with O_EXCL but then it risks making a valid
exclusive user of the device fail w/ -EBUSY.
* Userland polling is unnecessarily heavy and in-kernel implementation
is lighter and better coordinated (workqueue, timer slack).
This patch implements framework for in-kernel disk event handling,
which includes media presence polling.
* bdops->check_events() is added, which supercedes ->media_changed().
It should check whether there's any pending event and return if so.
Currently, two events are defined - DISK_EVENT_MEDIA_CHANGE and
DISK_EVENT_EJECT_REQUEST. ->check_events() is guaranteed not to be
called parallelly.
* gendisk->events and ->async_events are added. These should be
initialized by block driver before passing the device to add_disk().
The former contains the mask of all supported events and the latter
the mask of all events which the device can report without polling.
/sys/block/*/events[_async] export these to userland.
* Kernel parameter block.events_dfl_poll_msecs controls the system
polling interval (default is 0 which means disable) and
/sys/block/*/events_poll_msecs control polling intervals for
individual devices (default is -1 meaning use system setting). Note
that if a device can report all supported events asynchronously and
its polling interval isn't explicitly set, the device won't be
polled regardless of the system polling interval.
* If a device is opened exclusively with write access, event checking
is automatically disabled until all write exclusive accesses are
released.
* There are event 'clearing' events. For example, both of currently
defined events are cleared after the device has been successfully
opened. This information is passed to ->check_events() callback
using @clearing argument as a hint.
* Event checking is always performed from system_nrt_wq and timer
slack is set to 25% for polling.
* Nothing changes for drivers which implement ->media_changed() but
not ->check_events(). Going forward, all drivers will be converted
to ->check_events() and ->media_change() will be dropped.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
2010-12-09 03:57:37 +08:00
|
|
|
envp[nr_events++] = disk_uevents[i];
|
|
|
|
|
|
|
|
if (nr_events)
|
|
|
|
kobject_uevent_env(&disk_to_dev(disk)->kobj, KOBJ_CHANGE, envp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A disk events enabled device has the following sysfs nodes under
|
|
|
|
* its /sys/block/X/ directory.
|
|
|
|
*
|
|
|
|
* events : list of all supported events
|
|
|
|
* events_async : list of events which can be detected w/o polling
|
2019-03-27 21:51:01 +08:00
|
|
|
* (always empty, only for backwards compatibility)
|
implement in-kernel gendisk events handling
Currently, media presence polling for removeable block devices is done
from userland. There are several issues with this.
* Polling is done by periodically opening the device. For SCSI
devices, the command sequence generated by such action involves a
few different commands including TEST_UNIT_READY. This behavior,
while perfectly legal, is different from Windows which only issues
single command, GET_EVENT_STATUS_NOTIFICATION. Unfortunately, some
ATAPI devices lock up after being periodically queried such command
sequences.
* There is no reliable and unintrusive way for a userland program to
tell whether the target device is safe for media presence polling.
For example, polling for media presence during an on-going burning
session can make it fail. The polling program can avoid this by
opening the device with O_EXCL but then it risks making a valid
exclusive user of the device fail w/ -EBUSY.
* Userland polling is unnecessarily heavy and in-kernel implementation
is lighter and better coordinated (workqueue, timer slack).
This patch implements framework for in-kernel disk event handling,
which includes media presence polling.
* bdops->check_events() is added, which supercedes ->media_changed().
It should check whether there's any pending event and return if so.
Currently, two events are defined - DISK_EVENT_MEDIA_CHANGE and
DISK_EVENT_EJECT_REQUEST. ->check_events() is guaranteed not to be
called parallelly.
* gendisk->events and ->async_events are added. These should be
initialized by block driver before passing the device to add_disk().
The former contains the mask of all supported events and the latter
the mask of all events which the device can report without polling.
/sys/block/*/events[_async] export these to userland.
* Kernel parameter block.events_dfl_poll_msecs controls the system
polling interval (default is 0 which means disable) and
/sys/block/*/events_poll_msecs control polling intervals for
individual devices (default is -1 meaning use system setting). Note
that if a device can report all supported events asynchronously and
its polling interval isn't explicitly set, the device won't be
polled regardless of the system polling interval.
* If a device is opened exclusively with write access, event checking
is automatically disabled until all write exclusive accesses are
released.
* There are event 'clearing' events. For example, both of currently
defined events are cleared after the device has been successfully
opened. This information is passed to ->check_events() callback
using @clearing argument as a hint.
* Event checking is always performed from system_nrt_wq and timer
slack is set to 25% for polling.
* Nothing changes for drivers which implement ->media_changed() but
not ->check_events(). Going forward, all drivers will be converted
to ->check_events() and ->media_change() will be dropped.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
2010-12-09 03:57:37 +08:00
|
|
|
* events_poll_msecs : polling interval, 0: disable, -1: system default
|
|
|
|
*/
|
|
|
|
static ssize_t __disk_events_show(unsigned int events, char *buf)
|
|
|
|
{
|
|
|
|
const char *delim = "";
|
|
|
|
ssize_t pos = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(disk_events_strs); i++)
|
|
|
|
if (events & (1 << i)) {
|
|
|
|
pos += sprintf(buf + pos, "%s%s",
|
|
|
|
delim, disk_events_strs[i]);
|
|
|
|
delim = " ";
|
|
|
|
}
|
|
|
|
if (pos)
|
|
|
|
pos += sprintf(buf + pos, "\n");
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t disk_events_show(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *buf)
|
|
|
|
{
|
|
|
|
struct gendisk *disk = dev_to_disk(dev);
|
|
|
|
|
2019-03-27 21:51:02 +08:00
|
|
|
if (!(disk->event_flags & DISK_EVENT_FLAG_UEVENT))
|
|
|
|
return 0;
|
|
|
|
|
implement in-kernel gendisk events handling
Currently, media presence polling for removeable block devices is done
from userland. There are several issues with this.
* Polling is done by periodically opening the device. For SCSI
devices, the command sequence generated by such action involves a
few different commands including TEST_UNIT_READY. This behavior,
while perfectly legal, is different from Windows which only issues
single command, GET_EVENT_STATUS_NOTIFICATION. Unfortunately, some
ATAPI devices lock up after being periodically queried such command
sequences.
* There is no reliable and unintrusive way for a userland program to
tell whether the target device is safe for media presence polling.
For example, polling for media presence during an on-going burning
session can make it fail. The polling program can avoid this by
opening the device with O_EXCL but then it risks making a valid
exclusive user of the device fail w/ -EBUSY.
* Userland polling is unnecessarily heavy and in-kernel implementation
is lighter and better coordinated (workqueue, timer slack).
This patch implements framework for in-kernel disk event handling,
which includes media presence polling.
* bdops->check_events() is added, which supercedes ->media_changed().
It should check whether there's any pending event and return if so.
Currently, two events are defined - DISK_EVENT_MEDIA_CHANGE and
DISK_EVENT_EJECT_REQUEST. ->check_events() is guaranteed not to be
called parallelly.
* gendisk->events and ->async_events are added. These should be
initialized by block driver before passing the device to add_disk().
The former contains the mask of all supported events and the latter
the mask of all events which the device can report without polling.
/sys/block/*/events[_async] export these to userland.
* Kernel parameter block.events_dfl_poll_msecs controls the system
polling interval (default is 0 which means disable) and
/sys/block/*/events_poll_msecs control polling intervals for
individual devices (default is -1 meaning use system setting). Note
that if a device can report all supported events asynchronously and
its polling interval isn't explicitly set, the device won't be
polled regardless of the system polling interval.
* If a device is opened exclusively with write access, event checking
is automatically disabled until all write exclusive accesses are
released.
* There are event 'clearing' events. For example, both of currently
defined events are cleared after the device has been successfully
opened. This information is passed to ->check_events() callback
using @clearing argument as a hint.
* Event checking is always performed from system_nrt_wq and timer
slack is set to 25% for polling.
* Nothing changes for drivers which implement ->media_changed() but
not ->check_events(). Going forward, all drivers will be converted
to ->check_events() and ->media_change() will be dropped.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
2010-12-09 03:57:37 +08:00
|
|
|
return __disk_events_show(disk->events, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t disk_events_async_show(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *buf)
|
|
|
|
{
|
2019-03-27 21:51:01 +08:00
|
|
|
return 0;
|
implement in-kernel gendisk events handling
Currently, media presence polling for removeable block devices is done
from userland. There are several issues with this.
* Polling is done by periodically opening the device. For SCSI
devices, the command sequence generated by such action involves a
few different commands including TEST_UNIT_READY. This behavior,
while perfectly legal, is different from Windows which only issues
single command, GET_EVENT_STATUS_NOTIFICATION. Unfortunately, some
ATAPI devices lock up after being periodically queried such command
sequences.
* There is no reliable and unintrusive way for a userland program to
tell whether the target device is safe for media presence polling.
For example, polling for media presence during an on-going burning
session can make it fail. The polling program can avoid this by
opening the device with O_EXCL but then it risks making a valid
exclusive user of the device fail w/ -EBUSY.
* Userland polling is unnecessarily heavy and in-kernel implementation
is lighter and better coordinated (workqueue, timer slack).
This patch implements framework for in-kernel disk event handling,
which includes media presence polling.
* bdops->check_events() is added, which supercedes ->media_changed().
It should check whether there's any pending event and return if so.
Currently, two events are defined - DISK_EVENT_MEDIA_CHANGE and
DISK_EVENT_EJECT_REQUEST. ->check_events() is guaranteed not to be
called parallelly.
* gendisk->events and ->async_events are added. These should be
initialized by block driver before passing the device to add_disk().
The former contains the mask of all supported events and the latter
the mask of all events which the device can report without polling.
/sys/block/*/events[_async] export these to userland.
* Kernel parameter block.events_dfl_poll_msecs controls the system
polling interval (default is 0 which means disable) and
/sys/block/*/events_poll_msecs control polling intervals for
individual devices (default is -1 meaning use system setting). Note
that if a device can report all supported events asynchronously and
its polling interval isn't explicitly set, the device won't be
polled regardless of the system polling interval.
* If a device is opened exclusively with write access, event checking
is automatically disabled until all write exclusive accesses are
released.
* There are event 'clearing' events. For example, both of currently
defined events are cleared after the device has been successfully
opened. This information is passed to ->check_events() callback
using @clearing argument as a hint.
* Event checking is always performed from system_nrt_wq and timer
slack is set to 25% for polling.
* Nothing changes for drivers which implement ->media_changed() but
not ->check_events(). Going forward, all drivers will be converted
to ->check_events() and ->media_change() will be dropped.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
2010-12-09 03:57:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t disk_events_poll_msecs_show(struct device *dev,
|
|
|
|
struct device_attribute *attr,
|
|
|
|
char *buf)
|
|
|
|
{
|
|
|
|
struct gendisk *disk = dev_to_disk(dev);
|
|
|
|
|
2019-03-27 21:51:05 +08:00
|
|
|
if (!disk->ev)
|
|
|
|
return sprintf(buf, "-1\n");
|
|
|
|
|
implement in-kernel gendisk events handling
Currently, media presence polling for removeable block devices is done
from userland. There are several issues with this.
* Polling is done by periodically opening the device. For SCSI
devices, the command sequence generated by such action involves a
few different commands including TEST_UNIT_READY. This behavior,
while perfectly legal, is different from Windows which only issues
single command, GET_EVENT_STATUS_NOTIFICATION. Unfortunately, some
ATAPI devices lock up after being periodically queried such command
sequences.
* There is no reliable and unintrusive way for a userland program to
tell whether the target device is safe for media presence polling.
For example, polling for media presence during an on-going burning
session can make it fail. The polling program can avoid this by
opening the device with O_EXCL but then it risks making a valid
exclusive user of the device fail w/ -EBUSY.
* Userland polling is unnecessarily heavy and in-kernel implementation
is lighter and better coordinated (workqueue, timer slack).
This patch implements framework for in-kernel disk event handling,
which includes media presence polling.
* bdops->check_events() is added, which supercedes ->media_changed().
It should check whether there's any pending event and return if so.
Currently, two events are defined - DISK_EVENT_MEDIA_CHANGE and
DISK_EVENT_EJECT_REQUEST. ->check_events() is guaranteed not to be
called parallelly.
* gendisk->events and ->async_events are added. These should be
initialized by block driver before passing the device to add_disk().
The former contains the mask of all supported events and the latter
the mask of all events which the device can report without polling.
/sys/block/*/events[_async] export these to userland.
* Kernel parameter block.events_dfl_poll_msecs controls the system
polling interval (default is 0 which means disable) and
/sys/block/*/events_poll_msecs control polling intervals for
individual devices (default is -1 meaning use system setting). Note
that if a device can report all supported events asynchronously and
its polling interval isn't explicitly set, the device won't be
polled regardless of the system polling interval.
* If a device is opened exclusively with write access, event checking
is automatically disabled until all write exclusive accesses are
released.
* There are event 'clearing' events. For example, both of currently
defined events are cleared after the device has been successfully
opened. This information is passed to ->check_events() callback
using @clearing argument as a hint.
* Event checking is always performed from system_nrt_wq and timer
slack is set to 25% for polling.
* Nothing changes for drivers which implement ->media_changed() but
not ->check_events(). Going forward, all drivers will be converted
to ->check_events() and ->media_change() will be dropped.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
2010-12-09 03:57:37 +08:00
|
|
|
return sprintf(buf, "%ld\n", disk->ev->poll_msecs);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t disk_events_poll_msecs_store(struct device *dev,
|
|
|
|
struct device_attribute *attr,
|
|
|
|
const char *buf, size_t count)
|
|
|
|
{
|
|
|
|
struct gendisk *disk = dev_to_disk(dev);
|
|
|
|
long intv;
|
|
|
|
|
|
|
|
if (!count || !sscanf(buf, "%ld", &intv))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (intv < 0 && intv != -1)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2019-03-27 21:51:05 +08:00
|
|
|
if (!disk->ev)
|
|
|
|
return -ENODEV;
|
|
|
|
|
2011-06-10 02:43:55 +08:00
|
|
|
disk_block_events(disk);
|
implement in-kernel gendisk events handling
Currently, media presence polling for removeable block devices is done
from userland. There are several issues with this.
* Polling is done by periodically opening the device. For SCSI
devices, the command sequence generated by such action involves a
few different commands including TEST_UNIT_READY. This behavior,
while perfectly legal, is different from Windows which only issues
single command, GET_EVENT_STATUS_NOTIFICATION. Unfortunately, some
ATAPI devices lock up after being periodically queried such command
sequences.
* There is no reliable and unintrusive way for a userland program to
tell whether the target device is safe for media presence polling.
For example, polling for media presence during an on-going burning
session can make it fail. The polling program can avoid this by
opening the device with O_EXCL but then it risks making a valid
exclusive user of the device fail w/ -EBUSY.
* Userland polling is unnecessarily heavy and in-kernel implementation
is lighter and better coordinated (workqueue, timer slack).
This patch implements framework for in-kernel disk event handling,
which includes media presence polling.
* bdops->check_events() is added, which supercedes ->media_changed().
It should check whether there's any pending event and return if so.
Currently, two events are defined - DISK_EVENT_MEDIA_CHANGE and
DISK_EVENT_EJECT_REQUEST. ->check_events() is guaranteed not to be
called parallelly.
* gendisk->events and ->async_events are added. These should be
initialized by block driver before passing the device to add_disk().
The former contains the mask of all supported events and the latter
the mask of all events which the device can report without polling.
/sys/block/*/events[_async] export these to userland.
* Kernel parameter block.events_dfl_poll_msecs controls the system
polling interval (default is 0 which means disable) and
/sys/block/*/events_poll_msecs control polling intervals for
individual devices (default is -1 meaning use system setting). Note
that if a device can report all supported events asynchronously and
its polling interval isn't explicitly set, the device won't be
polled regardless of the system polling interval.
* If a device is opened exclusively with write access, event checking
is automatically disabled until all write exclusive accesses are
released.
* There are event 'clearing' events. For example, both of currently
defined events are cleared after the device has been successfully
opened. This information is passed to ->check_events() callback
using @clearing argument as a hint.
* Event checking is always performed from system_nrt_wq and timer
slack is set to 25% for polling.
* Nothing changes for drivers which implement ->media_changed() but
not ->check_events(). Going forward, all drivers will be converted
to ->check_events() and ->media_change() will be dropped.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
2010-12-09 03:57:37 +08:00
|
|
|
disk->ev->poll_msecs = intv;
|
|
|
|
__disk_unblock_events(disk, true);
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2018-05-25 03:38:59 +08:00
|
|
|
static const DEVICE_ATTR(events, 0444, disk_events_show, NULL);
|
|
|
|
static const DEVICE_ATTR(events_async, 0444, disk_events_async_show, NULL);
|
|
|
|
static const DEVICE_ATTR(events_poll_msecs, 0644,
|
implement in-kernel gendisk events handling
Currently, media presence polling for removeable block devices is done
from userland. There are several issues with this.
* Polling is done by periodically opening the device. For SCSI
devices, the command sequence generated by such action involves a
few different commands including TEST_UNIT_READY. This behavior,
while perfectly legal, is different from Windows which only issues
single command, GET_EVENT_STATUS_NOTIFICATION. Unfortunately, some
ATAPI devices lock up after being periodically queried such command
sequences.
* There is no reliable and unintrusive way for a userland program to
tell whether the target device is safe for media presence polling.
For example, polling for media presence during an on-going burning
session can make it fail. The polling program can avoid this by
opening the device with O_EXCL but then it risks making a valid
exclusive user of the device fail w/ -EBUSY.
* Userland polling is unnecessarily heavy and in-kernel implementation
is lighter and better coordinated (workqueue, timer slack).
This patch implements framework for in-kernel disk event handling,
which includes media presence polling.
* bdops->check_events() is added, which supercedes ->media_changed().
It should check whether there's any pending event and return if so.
Currently, two events are defined - DISK_EVENT_MEDIA_CHANGE and
DISK_EVENT_EJECT_REQUEST. ->check_events() is guaranteed not to be
called parallelly.
* gendisk->events and ->async_events are added. These should be
initialized by block driver before passing the device to add_disk().
The former contains the mask of all supported events and the latter
the mask of all events which the device can report without polling.
/sys/block/*/events[_async] export these to userland.
* Kernel parameter block.events_dfl_poll_msecs controls the system
polling interval (default is 0 which means disable) and
/sys/block/*/events_poll_msecs control polling intervals for
individual devices (default is -1 meaning use system setting). Note
that if a device can report all supported events asynchronously and
its polling interval isn't explicitly set, the device won't be
polled regardless of the system polling interval.
* If a device is opened exclusively with write access, event checking
is automatically disabled until all write exclusive accesses are
released.
* There are event 'clearing' events. For example, both of currently
defined events are cleared after the device has been successfully
opened. This information is passed to ->check_events() callback
using @clearing argument as a hint.
* Event checking is always performed from system_nrt_wq and timer
slack is set to 25% for polling.
* Nothing changes for drivers which implement ->media_changed() but
not ->check_events(). Going forward, all drivers will be converted
to ->check_events() and ->media_change() will be dropped.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
2010-12-09 03:57:37 +08:00
|
|
|
disk_events_poll_msecs_show,
|
|
|
|
disk_events_poll_msecs_store);
|
|
|
|
|
|
|
|
static const struct attribute *disk_events_attrs[] = {
|
|
|
|
&dev_attr_events.attr,
|
|
|
|
&dev_attr_events_async.attr,
|
|
|
|
&dev_attr_events_poll_msecs.attr,
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The default polling interval can be specified by the kernel
|
|
|
|
* parameter block.events_dfl_poll_msecs which defaults to 0
|
|
|
|
* (disable). This can also be modified runtime by writing to
|
2019-07-16 20:59:35 +08:00
|
|
|
* /sys/module/block/parameters/events_dfl_poll_msecs.
|
implement in-kernel gendisk events handling
Currently, media presence polling for removeable block devices is done
from userland. There are several issues with this.
* Polling is done by periodically opening the device. For SCSI
devices, the command sequence generated by such action involves a
few different commands including TEST_UNIT_READY. This behavior,
while perfectly legal, is different from Windows which only issues
single command, GET_EVENT_STATUS_NOTIFICATION. Unfortunately, some
ATAPI devices lock up after being periodically queried such command
sequences.
* There is no reliable and unintrusive way for a userland program to
tell whether the target device is safe for media presence polling.
For example, polling for media presence during an on-going burning
session can make it fail. The polling program can avoid this by
opening the device with O_EXCL but then it risks making a valid
exclusive user of the device fail w/ -EBUSY.
* Userland polling is unnecessarily heavy and in-kernel implementation
is lighter and better coordinated (workqueue, timer slack).
This patch implements framework for in-kernel disk event handling,
which includes media presence polling.
* bdops->check_events() is added, which supercedes ->media_changed().
It should check whether there's any pending event and return if so.
Currently, two events are defined - DISK_EVENT_MEDIA_CHANGE and
DISK_EVENT_EJECT_REQUEST. ->check_events() is guaranteed not to be
called parallelly.
* gendisk->events and ->async_events are added. These should be
initialized by block driver before passing the device to add_disk().
The former contains the mask of all supported events and the latter
the mask of all events which the device can report without polling.
/sys/block/*/events[_async] export these to userland.
* Kernel parameter block.events_dfl_poll_msecs controls the system
polling interval (default is 0 which means disable) and
/sys/block/*/events_poll_msecs control polling intervals for
individual devices (default is -1 meaning use system setting). Note
that if a device can report all supported events asynchronously and
its polling interval isn't explicitly set, the device won't be
polled regardless of the system polling interval.
* If a device is opened exclusively with write access, event checking
is automatically disabled until all write exclusive accesses are
released.
* There are event 'clearing' events. For example, both of currently
defined events are cleared after the device has been successfully
opened. This information is passed to ->check_events() callback
using @clearing argument as a hint.
* Event checking is always performed from system_nrt_wq and timer
slack is set to 25% for polling.
* Nothing changes for drivers which implement ->media_changed() but
not ->check_events(). Going forward, all drivers will be converted
to ->check_events() and ->media_change() will be dropped.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
2010-12-09 03:57:37 +08:00
|
|
|
*/
|
|
|
|
static int disk_events_set_dfl_poll_msecs(const char *val,
|
|
|
|
const struct kernel_param *kp)
|
|
|
|
{
|
|
|
|
struct disk_events *ev;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = param_set_ulong(val, kp);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
mutex_lock(&disk_events_mutex);
|
|
|
|
|
|
|
|
list_for_each_entry(ev, &disk_events, node)
|
2011-07-01 22:17:47 +08:00
|
|
|
disk_flush_events(ev->disk, 0);
|
implement in-kernel gendisk events handling
Currently, media presence polling for removeable block devices is done
from userland. There are several issues with this.
* Polling is done by periodically opening the device. For SCSI
devices, the command sequence generated by such action involves a
few different commands including TEST_UNIT_READY. This behavior,
while perfectly legal, is different from Windows which only issues
single command, GET_EVENT_STATUS_NOTIFICATION. Unfortunately, some
ATAPI devices lock up after being periodically queried such command
sequences.
* There is no reliable and unintrusive way for a userland program to
tell whether the target device is safe for media presence polling.
For example, polling for media presence during an on-going burning
session can make it fail. The polling program can avoid this by
opening the device with O_EXCL but then it risks making a valid
exclusive user of the device fail w/ -EBUSY.
* Userland polling is unnecessarily heavy and in-kernel implementation
is lighter and better coordinated (workqueue, timer slack).
This patch implements framework for in-kernel disk event handling,
which includes media presence polling.
* bdops->check_events() is added, which supercedes ->media_changed().
It should check whether there's any pending event and return if so.
Currently, two events are defined - DISK_EVENT_MEDIA_CHANGE and
DISK_EVENT_EJECT_REQUEST. ->check_events() is guaranteed not to be
called parallelly.
* gendisk->events and ->async_events are added. These should be
initialized by block driver before passing the device to add_disk().
The former contains the mask of all supported events and the latter
the mask of all events which the device can report without polling.
/sys/block/*/events[_async] export these to userland.
* Kernel parameter block.events_dfl_poll_msecs controls the system
polling interval (default is 0 which means disable) and
/sys/block/*/events_poll_msecs control polling intervals for
individual devices (default is -1 meaning use system setting). Note
that if a device can report all supported events asynchronously and
its polling interval isn't explicitly set, the device won't be
polled regardless of the system polling interval.
* If a device is opened exclusively with write access, event checking
is automatically disabled until all write exclusive accesses are
released.
* There are event 'clearing' events. For example, both of currently
defined events are cleared after the device has been successfully
opened. This information is passed to ->check_events() callback
using @clearing argument as a hint.
* Event checking is always performed from system_nrt_wq and timer
slack is set to 25% for polling.
* Nothing changes for drivers which implement ->media_changed() but
not ->check_events(). Going forward, all drivers will be converted
to ->check_events() and ->media_change() will be dropped.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
2010-12-09 03:57:37 +08:00
|
|
|
|
|
|
|
mutex_unlock(&disk_events_mutex);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct kernel_param_ops disk_events_dfl_poll_msecs_param_ops = {
|
|
|
|
.set = disk_events_set_dfl_poll_msecs,
|
|
|
|
.get = param_get_ulong,
|
|
|
|
};
|
|
|
|
|
|
|
|
#undef MODULE_PARAM_PREFIX
|
|
|
|
#define MODULE_PARAM_PREFIX "block."
|
|
|
|
|
|
|
|
module_param_cb(events_dfl_poll_msecs, &disk_events_dfl_poll_msecs_param_ops,
|
|
|
|
&disk_events_dfl_poll_msecs, 0644);
|
|
|
|
|
|
|
|
/*
|
2012-03-02 17:43:28 +08:00
|
|
|
* disk_{alloc|add|del|release}_events - initialize and destroy disk_events.
|
implement in-kernel gendisk events handling
Currently, media presence polling for removeable block devices is done
from userland. There are several issues with this.
* Polling is done by periodically opening the device. For SCSI
devices, the command sequence generated by such action involves a
few different commands including TEST_UNIT_READY. This behavior,
while perfectly legal, is different from Windows which only issues
single command, GET_EVENT_STATUS_NOTIFICATION. Unfortunately, some
ATAPI devices lock up after being periodically queried such command
sequences.
* There is no reliable and unintrusive way for a userland program to
tell whether the target device is safe for media presence polling.
For example, polling for media presence during an on-going burning
session can make it fail. The polling program can avoid this by
opening the device with O_EXCL but then it risks making a valid
exclusive user of the device fail w/ -EBUSY.
* Userland polling is unnecessarily heavy and in-kernel implementation
is lighter and better coordinated (workqueue, timer slack).
This patch implements framework for in-kernel disk event handling,
which includes media presence polling.
* bdops->check_events() is added, which supercedes ->media_changed().
It should check whether there's any pending event and return if so.
Currently, two events are defined - DISK_EVENT_MEDIA_CHANGE and
DISK_EVENT_EJECT_REQUEST. ->check_events() is guaranteed not to be
called parallelly.
* gendisk->events and ->async_events are added. These should be
initialized by block driver before passing the device to add_disk().
The former contains the mask of all supported events and the latter
the mask of all events which the device can report without polling.
/sys/block/*/events[_async] export these to userland.
* Kernel parameter block.events_dfl_poll_msecs controls the system
polling interval (default is 0 which means disable) and
/sys/block/*/events_poll_msecs control polling intervals for
individual devices (default is -1 meaning use system setting). Note
that if a device can report all supported events asynchronously and
its polling interval isn't explicitly set, the device won't be
polled regardless of the system polling interval.
* If a device is opened exclusively with write access, event checking
is automatically disabled until all write exclusive accesses are
released.
* There are event 'clearing' events. For example, both of currently
defined events are cleared after the device has been successfully
opened. This information is passed to ->check_events() callback
using @clearing argument as a hint.
* Event checking is always performed from system_nrt_wq and timer
slack is set to 25% for polling.
* Nothing changes for drivers which implement ->media_changed() but
not ->check_events(). Going forward, all drivers will be converted
to ->check_events() and ->media_change() will be dropped.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
2010-12-09 03:57:37 +08:00
|
|
|
*/
|
2012-03-02 17:43:28 +08:00
|
|
|
static void disk_alloc_events(struct gendisk *disk)
|
implement in-kernel gendisk events handling
Currently, media presence polling for removeable block devices is done
from userland. There are several issues with this.
* Polling is done by periodically opening the device. For SCSI
devices, the command sequence generated by such action involves a
few different commands including TEST_UNIT_READY. This behavior,
while perfectly legal, is different from Windows which only issues
single command, GET_EVENT_STATUS_NOTIFICATION. Unfortunately, some
ATAPI devices lock up after being periodically queried such command
sequences.
* There is no reliable and unintrusive way for a userland program to
tell whether the target device is safe for media presence polling.
For example, polling for media presence during an on-going burning
session can make it fail. The polling program can avoid this by
opening the device with O_EXCL but then it risks making a valid
exclusive user of the device fail w/ -EBUSY.
* Userland polling is unnecessarily heavy and in-kernel implementation
is lighter and better coordinated (workqueue, timer slack).
This patch implements framework for in-kernel disk event handling,
which includes media presence polling.
* bdops->check_events() is added, which supercedes ->media_changed().
It should check whether there's any pending event and return if so.
Currently, two events are defined - DISK_EVENT_MEDIA_CHANGE and
DISK_EVENT_EJECT_REQUEST. ->check_events() is guaranteed not to be
called parallelly.
* gendisk->events and ->async_events are added. These should be
initialized by block driver before passing the device to add_disk().
The former contains the mask of all supported events and the latter
the mask of all events which the device can report without polling.
/sys/block/*/events[_async] export these to userland.
* Kernel parameter block.events_dfl_poll_msecs controls the system
polling interval (default is 0 which means disable) and
/sys/block/*/events_poll_msecs control polling intervals for
individual devices (default is -1 meaning use system setting). Note
that if a device can report all supported events asynchronously and
its polling interval isn't explicitly set, the device won't be
polled regardless of the system polling interval.
* If a device is opened exclusively with write access, event checking
is automatically disabled until all write exclusive accesses are
released.
* There are event 'clearing' events. For example, both of currently
defined events are cleared after the device has been successfully
opened. This information is passed to ->check_events() callback
using @clearing argument as a hint.
* Event checking is always performed from system_nrt_wq and timer
slack is set to 25% for polling.
* Nothing changes for drivers which implement ->media_changed() but
not ->check_events(). Going forward, all drivers will be converted
to ->check_events() and ->media_change() will be dropped.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
2010-12-09 03:57:37 +08:00
|
|
|
{
|
|
|
|
struct disk_events *ev;
|
|
|
|
|
2019-03-27 21:51:05 +08:00
|
|
|
if (!disk->fops->check_events || !disk->events)
|
implement in-kernel gendisk events handling
Currently, media presence polling for removeable block devices is done
from userland. There are several issues with this.
* Polling is done by periodically opening the device. For SCSI
devices, the command sequence generated by such action involves a
few different commands including TEST_UNIT_READY. This behavior,
while perfectly legal, is different from Windows which only issues
single command, GET_EVENT_STATUS_NOTIFICATION. Unfortunately, some
ATAPI devices lock up after being periodically queried such command
sequences.
* There is no reliable and unintrusive way for a userland program to
tell whether the target device is safe for media presence polling.
For example, polling for media presence during an on-going burning
session can make it fail. The polling program can avoid this by
opening the device with O_EXCL but then it risks making a valid
exclusive user of the device fail w/ -EBUSY.
* Userland polling is unnecessarily heavy and in-kernel implementation
is lighter and better coordinated (workqueue, timer slack).
This patch implements framework for in-kernel disk event handling,
which includes media presence polling.
* bdops->check_events() is added, which supercedes ->media_changed().
It should check whether there's any pending event and return if so.
Currently, two events are defined - DISK_EVENT_MEDIA_CHANGE and
DISK_EVENT_EJECT_REQUEST. ->check_events() is guaranteed not to be
called parallelly.
* gendisk->events and ->async_events are added. These should be
initialized by block driver before passing the device to add_disk().
The former contains the mask of all supported events and the latter
the mask of all events which the device can report without polling.
/sys/block/*/events[_async] export these to userland.
* Kernel parameter block.events_dfl_poll_msecs controls the system
polling interval (default is 0 which means disable) and
/sys/block/*/events_poll_msecs control polling intervals for
individual devices (default is -1 meaning use system setting). Note
that if a device can report all supported events asynchronously and
its polling interval isn't explicitly set, the device won't be
polled regardless of the system polling interval.
* If a device is opened exclusively with write access, event checking
is automatically disabled until all write exclusive accesses are
released.
* There are event 'clearing' events. For example, both of currently
defined events are cleared after the device has been successfully
opened. This information is passed to ->check_events() callback
using @clearing argument as a hint.
* Event checking is always performed from system_nrt_wq and timer
slack is set to 25% for polling.
* Nothing changes for drivers which implement ->media_changed() but
not ->check_events(). Going forward, all drivers will be converted
to ->check_events() and ->media_change() will be dropped.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
2010-12-09 03:57:37 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
ev = kzalloc(sizeof(*ev), GFP_KERNEL);
|
|
|
|
if (!ev) {
|
|
|
|
pr_warn("%s: failed to initialize events\n", disk->disk_name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
INIT_LIST_HEAD(&ev->node);
|
|
|
|
ev->disk = disk;
|
|
|
|
spin_lock_init(&ev->lock);
|
2011-06-10 02:43:59 +08:00
|
|
|
mutex_init(&ev->block_mutex);
|
implement in-kernel gendisk events handling
Currently, media presence polling for removeable block devices is done
from userland. There are several issues with this.
* Polling is done by periodically opening the device. For SCSI
devices, the command sequence generated by such action involves a
few different commands including TEST_UNIT_READY. This behavior,
while perfectly legal, is different from Windows which only issues
single command, GET_EVENT_STATUS_NOTIFICATION. Unfortunately, some
ATAPI devices lock up after being periodically queried such command
sequences.
* There is no reliable and unintrusive way for a userland program to
tell whether the target device is safe for media presence polling.
For example, polling for media presence during an on-going burning
session can make it fail. The polling program can avoid this by
opening the device with O_EXCL but then it risks making a valid
exclusive user of the device fail w/ -EBUSY.
* Userland polling is unnecessarily heavy and in-kernel implementation
is lighter and better coordinated (workqueue, timer slack).
This patch implements framework for in-kernel disk event handling,
which includes media presence polling.
* bdops->check_events() is added, which supercedes ->media_changed().
It should check whether there's any pending event and return if so.
Currently, two events are defined - DISK_EVENT_MEDIA_CHANGE and
DISK_EVENT_EJECT_REQUEST. ->check_events() is guaranteed not to be
called parallelly.
* gendisk->events and ->async_events are added. These should be
initialized by block driver before passing the device to add_disk().
The former contains the mask of all supported events and the latter
the mask of all events which the device can report without polling.
/sys/block/*/events[_async] export these to userland.
* Kernel parameter block.events_dfl_poll_msecs controls the system
polling interval (default is 0 which means disable) and
/sys/block/*/events_poll_msecs control polling intervals for
individual devices (default is -1 meaning use system setting). Note
that if a device can report all supported events asynchronously and
its polling interval isn't explicitly set, the device won't be
polled regardless of the system polling interval.
* If a device is opened exclusively with write access, event checking
is automatically disabled until all write exclusive accesses are
released.
* There are event 'clearing' events. For example, both of currently
defined events are cleared after the device has been successfully
opened. This information is passed to ->check_events() callback
using @clearing argument as a hint.
* Event checking is always performed from system_nrt_wq and timer
slack is set to 25% for polling.
* Nothing changes for drivers which implement ->media_changed() but
not ->check_events(). Going forward, all drivers will be converted
to ->check_events() and ->media_change() will be dropped.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
2010-12-09 03:57:37 +08:00
|
|
|
ev->block = 1;
|
|
|
|
ev->poll_msecs = -1;
|
|
|
|
INIT_DELAYED_WORK(&ev->dwork, disk_events_workfn);
|
|
|
|
|
2012-03-02 17:43:28 +08:00
|
|
|
disk->ev = ev;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void disk_add_events(struct gendisk *disk)
|
|
|
|
{
|
|
|
|
/* FIXME: error handling */
|
|
|
|
if (sysfs_create_files(&disk_to_dev(disk)->kobj, disk_events_attrs) < 0)
|
|
|
|
pr_warn("%s: failed to create sysfs files for events\n",
|
|
|
|
disk->disk_name);
|
|
|
|
|
2019-03-27 21:51:05 +08:00
|
|
|
if (!disk->ev)
|
|
|
|
return;
|
|
|
|
|
implement in-kernel gendisk events handling
Currently, media presence polling for removeable block devices is done
from userland. There are several issues with this.
* Polling is done by periodically opening the device. For SCSI
devices, the command sequence generated by such action involves a
few different commands including TEST_UNIT_READY. This behavior,
while perfectly legal, is different from Windows which only issues
single command, GET_EVENT_STATUS_NOTIFICATION. Unfortunately, some
ATAPI devices lock up after being periodically queried such command
sequences.
* There is no reliable and unintrusive way for a userland program to
tell whether the target device is safe for media presence polling.
For example, polling for media presence during an on-going burning
session can make it fail. The polling program can avoid this by
opening the device with O_EXCL but then it risks making a valid
exclusive user of the device fail w/ -EBUSY.
* Userland polling is unnecessarily heavy and in-kernel implementation
is lighter and better coordinated (workqueue, timer slack).
This patch implements framework for in-kernel disk event handling,
which includes media presence polling.
* bdops->check_events() is added, which supercedes ->media_changed().
It should check whether there's any pending event and return if so.
Currently, two events are defined - DISK_EVENT_MEDIA_CHANGE and
DISK_EVENT_EJECT_REQUEST. ->check_events() is guaranteed not to be
called parallelly.
* gendisk->events and ->async_events are added. These should be
initialized by block driver before passing the device to add_disk().
The former contains the mask of all supported events and the latter
the mask of all events which the device can report without polling.
/sys/block/*/events[_async] export these to userland.
* Kernel parameter block.events_dfl_poll_msecs controls the system
polling interval (default is 0 which means disable) and
/sys/block/*/events_poll_msecs control polling intervals for
individual devices (default is -1 meaning use system setting). Note
that if a device can report all supported events asynchronously and
its polling interval isn't explicitly set, the device won't be
polled regardless of the system polling interval.
* If a device is opened exclusively with write access, event checking
is automatically disabled until all write exclusive accesses are
released.
* There are event 'clearing' events. For example, both of currently
defined events are cleared after the device has been successfully
opened. This information is passed to ->check_events() callback
using @clearing argument as a hint.
* Event checking is always performed from system_nrt_wq and timer
slack is set to 25% for polling.
* Nothing changes for drivers which implement ->media_changed() but
not ->check_events(). Going forward, all drivers will be converted
to ->check_events() and ->media_change() will be dropped.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
2010-12-09 03:57:37 +08:00
|
|
|
mutex_lock(&disk_events_mutex);
|
2012-03-02 17:43:28 +08:00
|
|
|
list_add_tail(&disk->ev->node, &disk_events);
|
implement in-kernel gendisk events handling
Currently, media presence polling for removeable block devices is done
from userland. There are several issues with this.
* Polling is done by periodically opening the device. For SCSI
devices, the command sequence generated by such action involves a
few different commands including TEST_UNIT_READY. This behavior,
while perfectly legal, is different from Windows which only issues
single command, GET_EVENT_STATUS_NOTIFICATION. Unfortunately, some
ATAPI devices lock up after being periodically queried such command
sequences.
* There is no reliable and unintrusive way for a userland program to
tell whether the target device is safe for media presence polling.
For example, polling for media presence during an on-going burning
session can make it fail. The polling program can avoid this by
opening the device with O_EXCL but then it risks making a valid
exclusive user of the device fail w/ -EBUSY.
* Userland polling is unnecessarily heavy and in-kernel implementation
is lighter and better coordinated (workqueue, timer slack).
This patch implements framework for in-kernel disk event handling,
which includes media presence polling.
* bdops->check_events() is added, which supercedes ->media_changed().
It should check whether there's any pending event and return if so.
Currently, two events are defined - DISK_EVENT_MEDIA_CHANGE and
DISK_EVENT_EJECT_REQUEST. ->check_events() is guaranteed not to be
called parallelly.
* gendisk->events and ->async_events are added. These should be
initialized by block driver before passing the device to add_disk().
The former contains the mask of all supported events and the latter
the mask of all events which the device can report without polling.
/sys/block/*/events[_async] export these to userland.
* Kernel parameter block.events_dfl_poll_msecs controls the system
polling interval (default is 0 which means disable) and
/sys/block/*/events_poll_msecs control polling intervals for
individual devices (default is -1 meaning use system setting). Note
that if a device can report all supported events asynchronously and
its polling interval isn't explicitly set, the device won't be
polled regardless of the system polling interval.
* If a device is opened exclusively with write access, event checking
is automatically disabled until all write exclusive accesses are
released.
* There are event 'clearing' events. For example, both of currently
defined events are cleared after the device has been successfully
opened. This information is passed to ->check_events() callback
using @clearing argument as a hint.
* Event checking is always performed from system_nrt_wq and timer
slack is set to 25% for polling.
* Nothing changes for drivers which implement ->media_changed() but
not ->check_events(). Going forward, all drivers will be converted
to ->check_events() and ->media_change() will be dropped.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
2010-12-09 03:57:37 +08:00
|
|
|
mutex_unlock(&disk_events_mutex);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Block count is initialized to 1 and the following initial
|
|
|
|
* unblock kicks it into action.
|
|
|
|
*/
|
|
|
|
__disk_unblock_events(disk, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void disk_del_events(struct gendisk *disk)
|
|
|
|
{
|
2019-03-27 21:51:05 +08:00
|
|
|
if (disk->ev) {
|
|
|
|
disk_block_events(disk);
|
implement in-kernel gendisk events handling
Currently, media presence polling for removeable block devices is done
from userland. There are several issues with this.
* Polling is done by periodically opening the device. For SCSI
devices, the command sequence generated by such action involves a
few different commands including TEST_UNIT_READY. This behavior,
while perfectly legal, is different from Windows which only issues
single command, GET_EVENT_STATUS_NOTIFICATION. Unfortunately, some
ATAPI devices lock up after being periodically queried such command
sequences.
* There is no reliable and unintrusive way for a userland program to
tell whether the target device is safe for media presence polling.
For example, polling for media presence during an on-going burning
session can make it fail. The polling program can avoid this by
opening the device with O_EXCL but then it risks making a valid
exclusive user of the device fail w/ -EBUSY.
* Userland polling is unnecessarily heavy and in-kernel implementation
is lighter and better coordinated (workqueue, timer slack).
This patch implements framework for in-kernel disk event handling,
which includes media presence polling.
* bdops->check_events() is added, which supercedes ->media_changed().
It should check whether there's any pending event and return if so.
Currently, two events are defined - DISK_EVENT_MEDIA_CHANGE and
DISK_EVENT_EJECT_REQUEST. ->check_events() is guaranteed not to be
called parallelly.
* gendisk->events and ->async_events are added. These should be
initialized by block driver before passing the device to add_disk().
The former contains the mask of all supported events and the latter
the mask of all events which the device can report without polling.
/sys/block/*/events[_async] export these to userland.
* Kernel parameter block.events_dfl_poll_msecs controls the system
polling interval (default is 0 which means disable) and
/sys/block/*/events_poll_msecs control polling intervals for
individual devices (default is -1 meaning use system setting). Note
that if a device can report all supported events asynchronously and
its polling interval isn't explicitly set, the device won't be
polled regardless of the system polling interval.
* If a device is opened exclusively with write access, event checking
is automatically disabled until all write exclusive accesses are
released.
* There are event 'clearing' events. For example, both of currently
defined events are cleared after the device has been successfully
opened. This information is passed to ->check_events() callback
using @clearing argument as a hint.
* Event checking is always performed from system_nrt_wq and timer
slack is set to 25% for polling.
* Nothing changes for drivers which implement ->media_changed() but
not ->check_events(). Going forward, all drivers will be converted
to ->check_events() and ->media_change() will be dropped.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
2010-12-09 03:57:37 +08:00
|
|
|
|
2019-03-27 21:51:05 +08:00
|
|
|
mutex_lock(&disk_events_mutex);
|
|
|
|
list_del_init(&disk->ev->node);
|
|
|
|
mutex_unlock(&disk_events_mutex);
|
|
|
|
}
|
implement in-kernel gendisk events handling
Currently, media presence polling for removeable block devices is done
from userland. There are several issues with this.
* Polling is done by periodically opening the device. For SCSI
devices, the command sequence generated by such action involves a
few different commands including TEST_UNIT_READY. This behavior,
while perfectly legal, is different from Windows which only issues
single command, GET_EVENT_STATUS_NOTIFICATION. Unfortunately, some
ATAPI devices lock up after being periodically queried such command
sequences.
* There is no reliable and unintrusive way for a userland program to
tell whether the target device is safe for media presence polling.
For example, polling for media presence during an on-going burning
session can make it fail. The polling program can avoid this by
opening the device with O_EXCL but then it risks making a valid
exclusive user of the device fail w/ -EBUSY.
* Userland polling is unnecessarily heavy and in-kernel implementation
is lighter and better coordinated (workqueue, timer slack).
This patch implements framework for in-kernel disk event handling,
which includes media presence polling.
* bdops->check_events() is added, which supercedes ->media_changed().
It should check whether there's any pending event and return if so.
Currently, two events are defined - DISK_EVENT_MEDIA_CHANGE and
DISK_EVENT_EJECT_REQUEST. ->check_events() is guaranteed not to be
called parallelly.
* gendisk->events and ->async_events are added. These should be
initialized by block driver before passing the device to add_disk().
The former contains the mask of all supported events and the latter
the mask of all events which the device can report without polling.
/sys/block/*/events[_async] export these to userland.
* Kernel parameter block.events_dfl_poll_msecs controls the system
polling interval (default is 0 which means disable) and
/sys/block/*/events_poll_msecs control polling intervals for
individual devices (default is -1 meaning use system setting). Note
that if a device can report all supported events asynchronously and
its polling interval isn't explicitly set, the device won't be
polled regardless of the system polling interval.
* If a device is opened exclusively with write access, event checking
is automatically disabled until all write exclusive accesses are
released.
* There are event 'clearing' events. For example, both of currently
defined events are cleared after the device has been successfully
opened. This information is passed to ->check_events() callback
using @clearing argument as a hint.
* Event checking is always performed from system_nrt_wq and timer
slack is set to 25% for polling.
* Nothing changes for drivers which implement ->media_changed() but
not ->check_events(). Going forward, all drivers will be converted
to ->check_events() and ->media_change() will be dropped.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
2010-12-09 03:57:37 +08:00
|
|
|
|
|
|
|
sysfs_remove_files(&disk_to_dev(disk)->kobj, disk_events_attrs);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void disk_release_events(struct gendisk *disk)
|
|
|
|
{
|
|
|
|
/* the block count should be 1 from disk_del_events() */
|
|
|
|
WARN_ON_ONCE(disk->ev && disk->ev->block != 1);
|
|
|
|
kfree(disk->ev);
|
|
|
|
}
|