2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* gendisk handling
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <linux/genhd.h>
|
2007-02-20 21:57:48 +00:00
|
|
|
#include <linux/kdev_t.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/blkdev.h>
|
2015-05-22 21:13:32 +00:00
|
|
|
#include <linux/backing-dev.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/spinlock.h>
|
2008-10-04 19:53:21 +00:00
|
|
|
#include <linux/proc_fs.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
#include <linux/seq_file.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/kmod.h>
|
|
|
|
#include <linux/kobj_map.h>
|
2006-02-06 22:12:43 +00:00
|
|
|
#include <linux/mutex.h>
|
2008-08-25 10:47:22 +00: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-08 19:57:37 +00:00
|
|
|
#include <linux/log2.h>
|
2013-02-23 00:34:13 +00:00
|
|
|
#include <linux/pm_runtime.h>
|
2016-01-09 16:36:51 +00:00
|
|
|
#include <linux/badblocks.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2008-03-04 10:23:45 +00:00
|
|
|
#include "blk.h"
|
|
|
|
|
2007-05-21 20:08:01 +00:00
|
|
|
static DEFINE_MUTEX(block_class_lock);
|
|
|
|
struct kobject *block_depr;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2008-08-25 10:47:22 +00:00
|
|
|
/* for extended dynamic devt allocation, currently only one major is used */
|
2013-02-28 01:03:56 +00:00
|
|
|
#define NR_EXT_DEVT (1 << MINORBITS)
|
2008-08-25 10:47:22 +00:00
|
|
|
|
2014-08-26 15:05:36 +00:00
|
|
|
/* For extended devt allocation. ext_devt_lock prevents look up
|
2008-08-25 10:47:22 +00:00
|
|
|
* results from going away underneath its user.
|
|
|
|
*/
|
2014-08-26 15:05:36 +00:00
|
|
|
static DEFINE_SPINLOCK(ext_devt_lock);
|
2008-08-25 10:47:22 +00:00
|
|
|
static DEFINE_IDR(ext_devt_idr);
|
|
|
|
|
2017-06-20 18:15:48 +00:00
|
|
|
static const struct device_type disk_type;
|
2008-03-04 10:23:46 +00:00
|
|
|
|
2012-12-18 20:27:20 +00:00
|
|
|
static void disk_check_events(struct disk_events *ev,
|
|
|
|
unsigned int *clearing_ptr);
|
2012-03-02 09:43:28 +00: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-08 19:57:37 +00: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);
|
|
|
|
|
2017-08-08 23:51:45 +00:00
|
|
|
void part_inc_in_flight(struct request_queue *q, struct hd_struct *part, int rw)
|
|
|
|
{
|
|
|
|
if (q->mq_ops)
|
|
|
|
return;
|
|
|
|
|
|
|
|
atomic_inc(&part->in_flight[rw]);
|
|
|
|
if (part->partno)
|
|
|
|
atomic_inc(&part_to_disk(part)->part0.in_flight[rw]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void part_dec_in_flight(struct request_queue *q, struct hd_struct *part, int rw)
|
|
|
|
{
|
|
|
|
if (q->mq_ops)
|
|
|
|
return;
|
|
|
|
|
|
|
|
atomic_dec(&part->in_flight[rw]);
|
|
|
|
if (part->partno)
|
|
|
|
atomic_dec(&part_to_disk(part)->part0.in_flight[rw]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void part_in_flight(struct request_queue *q, struct hd_struct *part,
|
|
|
|
unsigned int inflight[2])
|
|
|
|
{
|
|
|
|
if (q->mq_ops) {
|
|
|
|
blk_mq_in_flight(q, part, inflight);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
inflight[0] = atomic_read(&part->in_flight[0]) +
|
|
|
|
atomic_read(&part->in_flight[1]);
|
|
|
|
if (part->partno) {
|
|
|
|
part = &part_to_disk(part)->part0;
|
|
|
|
inflight[1] = atomic_read(&part->in_flight[0]) +
|
|
|
|
atomic_read(&part->in_flight[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-23 17:10:30 +00: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 07:03:02 +00: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-23 17:10:30 +00:00
|
|
|
struct hd_struct *part;
|
2008-08-25 10:56:15 +00:00
|
|
|
|
2008-09-03 07:03:02 +00:00
|
|
|
rcu_read_lock();
|
2017-08-23 17:10:30 +00:00
|
|
|
part = __disk_get_part(disk, partno);
|
|
|
|
if (part)
|
|
|
|
get_device(part_to_dev(part));
|
2008-09-03 07:03:02 +00: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 10:56:15 +00:00
|
|
|
struct disk_part_tbl *ptbl;
|
|
|
|
|
|
|
|
rcu_read_lock();
|
|
|
|
ptbl = rcu_dereference(disk->part_tbl);
|
|
|
|
|
2008-09-03 07:03:02 +00:00
|
|
|
piter->disk = disk;
|
|
|
|
piter->part = NULL;
|
|
|
|
|
|
|
|
if (flags & DISK_PITER_REVERSE)
|
2008-08-25 10:56:15 +00:00
|
|
|
piter->idx = ptbl->len - 1;
|
2009-04-17 06:34:48 +00:00
|
|
|
else if (flags & (DISK_PITER_INCL_PART0 | DISK_PITER_INCL_EMPTY_PART0))
|
2008-09-03 07:03:02 +00:00
|
|
|
piter->idx = 0;
|
2008-09-03 07:06:42 +00:00
|
|
|
else
|
|
|
|
piter->idx = 1;
|
2008-09-03 07:03:02 +00:00
|
|
|
|
|
|
|
piter->flags = flags;
|
2008-08-25 10:56:15 +00:00
|
|
|
|
|
|
|
rcu_read_unlock();
|
2008-09-03 07:03:02 +00: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 10:56:15 +00:00
|
|
|
struct disk_part_tbl *ptbl;
|
2008-09-03 07:03:02 +00:00
|
|
|
int inc, end;
|
|
|
|
|
|
|
|
/* put the last partition */
|
|
|
|
disk_put_part(piter->part);
|
|
|
|
piter->part = NULL;
|
|
|
|
|
2008-08-25 10:56:15 +00:00
|
|
|
/* get part_tbl */
|
2008-09-03 07:03:02 +00:00
|
|
|
rcu_read_lock();
|
2008-08-25 10:56:15 +00:00
|
|
|
ptbl = rcu_dereference(piter->disk->part_tbl);
|
2008-09-03 07:03:02 +00:00
|
|
|
|
|
|
|
/* determine iteration parameters */
|
|
|
|
if (piter->flags & DISK_PITER_REVERSE) {
|
|
|
|
inc = -1;
|
2009-04-17 06:34:48 +00:00
|
|
|
if (piter->flags & (DISK_PITER_INCL_PART0 |
|
|
|
|
DISK_PITER_INCL_EMPTY_PART0))
|
2008-09-03 07:06:42 +00:00
|
|
|
end = -1;
|
|
|
|
else
|
|
|
|
end = 0;
|
2008-09-03 07:03:02 +00:00
|
|
|
} else {
|
|
|
|
inc = 1;
|
2008-08-25 10:56:15 +00:00
|
|
|
end = ptbl->len;
|
2008-09-03 07:03:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* iterate to the next partition */
|
|
|
|
for (; piter->idx != end; piter->idx += inc) {
|
|
|
|
struct hd_struct *part;
|
|
|
|
|
2008-08-25 10:56:15 +00:00
|
|
|
part = rcu_dereference(ptbl->part[piter->idx]);
|
2008-09-03 07:03:02 +00:00
|
|
|
if (!part)
|
|
|
|
continue;
|
2012-08-01 10:24:18 +00:00
|
|
|
if (!part_nr_sects_read(part) &&
|
2009-04-17 06:34:48 +00:00
|
|
|
!(piter->flags & DISK_PITER_INCL_EMPTY) &&
|
|
|
|
!(piter->flags & DISK_PITER_INCL_EMPTY_PART0 &&
|
|
|
|
piter->idx == 0))
|
2008-09-03 07:03:02 +00:00
|
|
|
continue;
|
|
|
|
|
2008-08-25 10:56:05 +00:00
|
|
|
get_device(part_to_dev(part));
|
2008-09-03 07:03:02 +00: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 10:52:42 +00:00
|
|
|
static inline int sector_in_part(struct hd_struct *part, sector_t sector)
|
|
|
|
{
|
|
|
|
return part->start_sect <= sector &&
|
2012-08-01 10:24:18 +00:00
|
|
|
sector < part->start_sect + part_nr_sects_read(part);
|
2008-10-24 10:52:42 +00:00
|
|
|
}
|
|
|
|
|
2008-09-03 07:03:02 +00: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 10:56:14 +00:00
|
|
|
* Found partition on success, part0 is returned if no partition matches
|
2008-09-03 07:03:02 +00:00
|
|
|
*/
|
|
|
|
struct hd_struct *disk_map_sector_rcu(struct gendisk *disk, sector_t sector)
|
|
|
|
{
|
2008-08-25 10:56:15 +00:00
|
|
|
struct disk_part_tbl *ptbl;
|
2008-10-24 10:52:42 +00:00
|
|
|
struct hd_struct *part;
|
2008-09-03 07:03:02 +00:00
|
|
|
int i;
|
|
|
|
|
2008-08-25 10:56:15 +00:00
|
|
|
ptbl = rcu_dereference(disk->part_tbl);
|
|
|
|
|
2008-10-24 10:52:42 +00:00
|
|
|
part = rcu_dereference(ptbl->last_lookup);
|
|
|
|
if (part && sector_in_part(part, sector))
|
|
|
|
return part;
|
|
|
|
|
2008-08-25 10:56:15 +00:00
|
|
|
for (i = 1; i < ptbl->len; i++) {
|
2008-10-24 10:52:42 +00:00
|
|
|
part = rcu_dereference(ptbl->part[i]);
|
2008-09-03 07:03:02 +00:00
|
|
|
|
2008-10-24 10:52:42 +00:00
|
|
|
if (part && sector_in_part(part, sector)) {
|
|
|
|
rcu_assign_pointer(ptbl->last_lookup, part);
|
2008-09-03 07:03:02 +00:00
|
|
|
return part;
|
2008-10-24 10:52:42 +00:00
|
|
|
}
|
2008-09-03 07:03:02 +00:00
|
|
|
}
|
2008-08-25 10:56:14 +00:00
|
|
|
return &disk->part0;
|
2008-09-03 07:03:02 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(disk_map_sector_rcu);
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* Can be deleted altogether. Later.
|
|
|
|
*
|
|
|
|
*/
|
2017-06-16 23:48:21 +00:00
|
|
|
#define BLKDEV_MAJOR_HASH_SIZE 255
|
2005-04-16 22:20:36 +00:00
|
|
|
static struct blk_major_name {
|
|
|
|
struct blk_major_name *next;
|
|
|
|
int major;
|
|
|
|
char name[16];
|
2006-03-31 10:30:32 +00:00
|
|
|
} *major_names[BLKDEV_MAJOR_HASH_SIZE];
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/* index in the above - for now: assume no multimajor ranges */
|
2010-12-17 08:00:18 +00:00
|
|
|
static inline int major_to_index(unsigned major)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2006-03-31 10:30:32 +00:00
|
|
|
return major % BLKDEV_MAJOR_HASH_SIZE;
|
2006-01-14 21:20:38 +00:00
|
|
|
}
|
|
|
|
|
2006-03-31 10:30:32 +00:00
|
|
|
#ifdef CONFIG_PROC_FS
|
2008-09-03 07:01:09 +00:00
|
|
|
void blkdev_show(struct seq_file *seqf, off_t offset)
|
2006-01-14 21:20:38 +00:00
|
|
|
{
|
2006-03-31 10:30:32 +00:00
|
|
|
struct blk_major_name *dp;
|
2006-01-14 21:20:38 +00:00
|
|
|
|
2017-06-16 23:48:21 +00: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 07:01:09 +00:00
|
|
|
seq_printf(seqf, "%3d %s\n", dp->major, dp->name);
|
2017-06-16 23:48:21 +00:00
|
|
|
mutex_unlock(&block_class_lock);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2006-03-31 10:30:32 +00:00
|
|
|
#endif /* CONFIG_PROC_FS */
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2009-02-20 07:12:51 +00:00
|
|
|
/**
|
|
|
|
* register_blkdev - register a new block device
|
|
|
|
*
|
2017-03-30 20:11:36 +00:00
|
|
|
* @major: the requested major device number [1..255]. If @major = 0, try to
|
2009-02-20 07:12:51 +00:00
|
|
|
* allocate any unused major number.
|
|
|
|
* @name: the name of the new block device as a zero terminated string
|
|
|
|
*
|
|
|
|
* The @name must be unique within the system.
|
|
|
|
*
|
2017-03-30 20:11:36 +00:00
|
|
|
* The return value depends on the @major input parameter:
|
|
|
|
*
|
2009-02-20 07:12:51 +00:00
|
|
|
* - if a major device number was requested in range [1..255] then the
|
|
|
|
* function returns zero on success, or a negative error code
|
2017-03-30 20:11:36 +00:00
|
|
|
* - if any unused major number was requested with @major = 0 parameter
|
2009-02-20 07:12:51 +00:00
|
|
|
* then the return value is the allocated major number in range
|
|
|
|
* [1..255] or a negative error code otherwise
|
|
|
|
*/
|
2005-04-16 22:20:36 +00:00
|
|
|
int register_blkdev(unsigned int major, const char *name)
|
|
|
|
{
|
|
|
|
struct blk_major_name **n, *p;
|
|
|
|
int index, ret = 0;
|
|
|
|
|
2007-05-21 20:08:01 +00:00
|
|
|
mutex_lock(&block_class_lock);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/* temporary */
|
|
|
|
if (major == 0) {
|
|
|
|
for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) {
|
|
|
|
if (major_names[index] == NULL)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index == 0) {
|
|
|
|
printk("register_blkdev: failed to get major for %s\n",
|
|
|
|
name);
|
|
|
|
ret = -EBUSY;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
major = index;
|
|
|
|
ret = major;
|
|
|
|
}
|
|
|
|
|
2017-06-16 23:48:21 +00:00
|
|
|
if (major >= BLKDEV_MAJOR_MAX) {
|
|
|
|
pr_err("register_blkdev: major requested (%d) is greater than the maximum (%d) for %s\n",
|
|
|
|
major, BLKDEV_MAJOR_MAX, name);
|
|
|
|
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00: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) {
|
|
|
|
printk("register_blkdev: cannot get major %d for %s\n",
|
|
|
|
major, name);
|
|
|
|
kfree(p);
|
|
|
|
}
|
|
|
|
out:
|
2007-05-21 20:08:01 +00:00
|
|
|
mutex_unlock(&block_class_lock);
|
2005-04-16 22:20:36 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
EXPORT_SYMBOL(register_blkdev);
|
|
|
|
|
2007-07-17 11:03:47 +00:00
|
|
|
void unregister_blkdev(unsigned int major, const char *name)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
struct blk_major_name **n;
|
|
|
|
struct blk_major_name *p = NULL;
|
|
|
|
int index = major_to_index(major);
|
|
|
|
|
2007-05-21 20:08:01 +00:00
|
|
|
mutex_lock(&block_class_lock);
|
2005-04-16 22:20:36 +00:00
|
|
|
for (n = &major_names[index]; *n; n = &(*n)->next)
|
|
|
|
if ((*n)->major == major)
|
|
|
|
break;
|
2007-07-17 11:03:45 +00:00
|
|
|
if (!*n || strcmp((*n)->name, name)) {
|
|
|
|
WARN_ON(1);
|
|
|
|
} else {
|
2005-04-16 22:20:36 +00:00
|
|
|
p = *n;
|
|
|
|
*n = p->next;
|
|
|
|
}
|
2007-05-21 20:08:01 +00:00
|
|
|
mutex_unlock(&block_class_lock);
|
2005-04-16 22:20:36 +00:00
|
|
|
kfree(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
EXPORT_SYMBOL(unregister_blkdev);
|
|
|
|
|
|
|
|
static struct kobj_map *bdev_map;
|
|
|
|
|
2008-08-25 10:47:25 +00: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 10:47:22 +00: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 01:03:57 +00:00
|
|
|
int idx;
|
2008-08-25 10:47:22 +00: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 15:05:36 +00:00
|
|
|
idr_preload(GFP_KERNEL);
|
|
|
|
|
2015-06-11 03:47:14 +00:00
|
|
|
spin_lock_bh(&ext_devt_lock);
|
2014-08-26 15:05:36 +00:00
|
|
|
idx = idr_alloc(&ext_devt_idr, part, 0, NR_EXT_DEVT, GFP_NOWAIT);
|
2015-06-11 03:47:14 +00:00
|
|
|
spin_unlock_bh(&ext_devt_lock);
|
2014-08-26 15:05:36 +00:00
|
|
|
|
|
|
|
idr_preload_end();
|
2013-02-28 01:03:57 +00:00
|
|
|
if (idx < 0)
|
|
|
|
return idx == -ENOSPC ? -EBUSY : idx;
|
2008-08-25 10:47:22 +00:00
|
|
|
|
2008-08-25 10:47:25 +00:00
|
|
|
*devt = MKDEV(BLOCK_EXT_MAJOR, blk_mangle_minor(idx));
|
2008-08-25 10:47:22 +00: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 03:47:14 +00:00
|
|
|
spin_lock_bh(&ext_devt_lock);
|
2008-08-25 10:47:25 +00:00
|
|
|
idr_remove(&ext_devt_idr, blk_mangle_minor(MINOR(devt)));
|
2015-06-11 03:47:14 +00:00
|
|
|
spin_unlock_bh(&ext_devt_lock);
|
2008-08-25 10:47:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-25 10:47:23 +00: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-16 22:20:36 +00: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-21 20:08:01 +00:00
|
|
|
void blk_register_region(dev_t devt, unsigned long range, struct module *module,
|
2005-04-16 22:20:36 +00:00
|
|
|
struct kobject *(*probe)(dev_t, int *, void *),
|
|
|
|
int (*lock)(dev_t, void *), void *data)
|
|
|
|
{
|
2007-05-21 20:08:01 +00:00
|
|
|
kobj_map(bdev_map, devt, range, module, probe, lock, data);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
EXPORT_SYMBOL(blk_register_region);
|
|
|
|
|
2007-05-21 20:08:01 +00:00
|
|
|
void blk_unregister_region(dev_t devt, unsigned long range)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2007-05-21 20:08:01 +00:00
|
|
|
kobj_unmap(bdev_map, devt, range);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
EXPORT_SYMBOL(blk_unregister_region);
|
|
|
|
|
2008-09-03 07:01:09 +00:00
|
|
|
static struct kobject *exact_match(dev_t devt, int *partno, void *data)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
struct gendisk *p = data;
|
2007-05-21 20:08:01 +00:00
|
|
|
|
2008-08-25 10:56:05 +00:00
|
|
|
return &disk_to_dev(p)->kobj;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2007-05-21 20:08:01 +00:00
|
|
|
static int exact_lock(dev_t devt, void *data)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
struct gendisk *p = data;
|
|
|
|
|
|
|
|
if (!get_disk(p))
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-06-16 01:17:27 +00:00
|
|
|
static void register_disk(struct device *parent, struct gendisk *disk)
|
2010-12-08 19:57:36 +00: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 01:17:27 +00:00
|
|
|
ddev->parent = parent;
|
2010-12-08 19:57:36 +00:00
|
|
|
|
2013-07-03 22:01:14 +00:00
|
|
|
dev_set_name(ddev, "%s", disk->disk_name);
|
2010-12-08 19:57:36 +00:00
|
|
|
|
|
|
|
/* delay uevents, until we scanned partition table */
|
|
|
|
dev_set_uevent_suppress(ddev, 1);
|
|
|
|
|
|
|
|
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 00:34:13 +00: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-08 19:57:36 +00:00
|
|
|
disk->part0.holder_dir = kobject_create_and_add("holders", &ddev->kobj);
|
|
|
|
disk->slave_dir = kobject_create_and_add("slaves", &ddev->kobj);
|
|
|
|
|
2017-11-02 18:29:53 +00:00
|
|
|
if (disk->flags & GENHD_FL_HIDDEN) {
|
|
|
|
dev_set_uevent_suppress(ddev, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-12-08 19:57:36 +00:00
|
|
|
/* No minors to use for partitions */
|
2011-08-23 18:01:04 +00:00
|
|
|
if (!disk_part_scan_enabled(disk))
|
2010-12-08 19:57:36 +00: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-02 18:29:53 +00:00
|
|
|
|
|
|
|
err = sysfs_create_link(&ddev->kobj,
|
|
|
|
&disk->queue->backing_dev_info->dev->kobj,
|
|
|
|
"bdi");
|
|
|
|
WARN_ON(err);
|
2010-12-08 19:57:36 +00:00
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/**
|
2018-01-09 03:01:13 +00:00
|
|
|
* __device_add_disk - add disk information to kernel list
|
2016-06-16 01:17:27 +00:00
|
|
|
* @parent: parent device for the disk
|
2005-04-16 22:20:36 +00:00
|
|
|
* @disk: per-device partitioning information
|
2018-01-09 03:01:13 +00:00
|
|
|
* @register_queue: register the queue if set to true
|
2005-04-16 22:20:36 +00:00
|
|
|
*
|
|
|
|
* This function registers the partitioning information in @disk
|
|
|
|
* with the kernel.
|
2008-08-25 10:56:17 +00:00
|
|
|
*
|
|
|
|
* FIXME: error handling
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
2018-01-09 03:01:13 +00:00
|
|
|
static void __device_add_disk(struct device *parent, struct gendisk *disk,
|
|
|
|
bool register_queue)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2008-08-25 10:56:17 +00:00
|
|
|
dev_t devt;
|
2008-05-22 21:21:08 +00:00
|
|
|
int retval;
|
2008-04-30 07:54:32 +00:00
|
|
|
|
2008-08-25 10:56:17 +00: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-02 18:29:53 +00:00
|
|
|
WARN_ON(!disk->minors &&
|
|
|
|
!(disk->flags & (GENHD_FL_EXT_DEVT | GENHD_FL_HIDDEN)));
|
2008-08-25 10:56:17 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
disk->flags |= GENHD_FL_UP;
|
2008-08-25 10:56:17 +00: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 09:43:28 +00:00
|
|
|
disk_alloc_events(disk);
|
|
|
|
|
2017-11-02 18:29:53 +00: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 10:38:59 +00:00
|
|
|
int ret;
|
|
|
|
|
2017-11-02 18:29:53 +00:00
|
|
|
/* Register BDI before referencing it from bdev */
|
|
|
|
disk_to_dev(disk)->devt = devt;
|
2017-10-31 10:38:59 +00:00
|
|
|
ret = bdi_register_owner(disk->queue->backing_dev_info,
|
|
|
|
disk_to_dev(disk));
|
|
|
|
WARN_ON(ret);
|
2017-11-02 18:29:53 +00:00
|
|
|
blk_register_region(disk_devt(disk), disk->minors, NULL,
|
|
|
|
exact_match, exact_lock, disk);
|
|
|
|
}
|
2016-06-16 01:17:27 +00:00
|
|
|
register_disk(parent, disk);
|
2018-01-09 03:01:13 +00:00
|
|
|
if (register_queue)
|
|
|
|
blk_register_queue(disk);
|
2008-04-30 07:54:32 +00:00
|
|
|
|
2011-10-19 12:31:07 +00: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-13 23:33:38 +00:00
|
|
|
WARN_ON_ONCE(!blk_get_queue(disk->queue));
|
2011-10-19 12:31:07 +00: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-08 19:57:37 +00:00
|
|
|
disk_add_events(disk);
|
2015-10-21 17:19:49 +00:00
|
|
|
blk_integrity_add(disk);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2018-01-09 03:01:13 +00:00
|
|
|
|
|
|
|
void device_add_disk(struct device *parent, struct gendisk *disk)
|
|
|
|
{
|
|
|
|
__device_add_disk(parent, disk, true);
|
|
|
|
}
|
2016-06-16 01:17:27 +00:00
|
|
|
EXPORT_SYMBOL(device_add_disk);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2018-01-09 03:01:13 +00:00
|
|
|
void device_add_disk_no_queue_reg(struct device *parent, struct gendisk *disk)
|
|
|
|
{
|
|
|
|
__device_add_disk(parent, disk, false);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(device_add_disk_no_queue_reg);
|
|
|
|
|
2010-12-08 19:57:36 +00:00
|
|
|
void del_gendisk(struct gendisk *disk)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2010-12-08 19:57:36 +00:00
|
|
|
struct disk_part_iter piter;
|
|
|
|
struct hd_struct *part;
|
|
|
|
|
2015-10-21 17:19:49 +00: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-08 19:57:37 +00:00
|
|
|
disk_del_events(disk);
|
|
|
|
|
2010-12-08 19:57:36 +00: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-21 17:09:46 +00:00
|
|
|
bdev_unhash_inode(part_devt(part));
|
2010-12-08 19:57:36 +00:00
|
|
|
delete_partition(disk, part->partno);
|
|
|
|
}
|
|
|
|
disk_part_iter_exit(&piter);
|
|
|
|
|
|
|
|
invalidate_partition(disk, 0);
|
2017-02-21 17:09:47 +00:00
|
|
|
bdev_unhash_inode(disk_devt(disk));
|
2010-12-08 19:57:36 +00:00
|
|
|
set_capacity(disk, 0);
|
|
|
|
disk->flags &= ~GENHD_FL_UP;
|
|
|
|
|
2017-11-02 18:29:53 +00:00
|
|
|
if (!(disk->flags & GENHD_FL_HIDDEN))
|
|
|
|
sysfs_remove_link(&disk_to_dev(disk)->kobj, "bdi");
|
2017-03-08 16:48:33 +00: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 01:46:49 +00:00
|
|
|
if (!(disk->flags & GENHD_FL_HIDDEN))
|
|
|
|
bdi_unregister(disk->queue->backing_dev_info);
|
2017-03-08 16:48:33 +00:00
|
|
|
blk_unregister_queue(disk);
|
|
|
|
} else {
|
|
|
|
WARN_ON(1);
|
|
|
|
}
|
2010-12-08 19:57:36 +00:00
|
|
|
|
2017-11-09 16:57:06 +00:00
|
|
|
if (!(disk->flags & GENHD_FL_HIDDEN))
|
2017-11-02 18:29:53 +00:00
|
|
|
blk_unregister_region(disk_devt(disk), disk->minors);
|
2010-12-08 19:57:36 +00: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 00:34:13 +00:00
|
|
|
pm_runtime_set_memalloc_noio(disk_to_dev(disk), false);
|
2010-12-08 19:57:36 +00:00
|
|
|
device_del(disk_to_dev(disk));
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2010-12-08 19:57:36 +00:00
|
|
|
EXPORT_SYMBOL(del_gendisk);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2016-01-09 16:36:51 +00: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-16 22:20:36 +00:00
|
|
|
/**
|
|
|
|
* get_gendisk - get partitioning information for a given device
|
2008-08-19 18:13:11 +00:00
|
|
|
* @devt: device to get partitioning information for
|
2008-10-16 05:46:23 +00:00
|
|
|
* @partno: returned partition index
|
2005-04-16 22:20:36 +00:00
|
|
|
*
|
|
|
|
* This function gets the structure containing partitioning
|
2008-08-19 18:13:11 +00:00
|
|
|
* information for the given device @devt.
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
2008-09-03 07:01:09 +00:00
|
|
|
struct gendisk *get_gendisk(dev_t devt, int *partno)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2008-08-25 10:47:22 +00: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 03:47:14 +00:00
|
|
|
spin_lock_bh(&ext_devt_lock);
|
2008-08-25 10:47:25 +00:00
|
|
|
part = idr_find(&ext_devt_idr, blk_mangle_minor(MINOR(devt)));
|
2008-08-25 10:47:22 +00:00
|
|
|
if (part && get_disk(part_to_disk(part))) {
|
|
|
|
*partno = part->partno;
|
|
|
|
disk = part_to_disk(part);
|
|
|
|
}
|
2015-06-11 03:47:14 +00:00
|
|
|
spin_unlock_bh(&ext_devt_lock);
|
2008-08-25 10:47:22 +00:00
|
|
|
}
|
2007-05-21 20:08:01 +00:00
|
|
|
|
2017-11-06 17:53:53 +00:00
|
|
|
if (disk && unlikely(disk->flags & GENHD_FL_HIDDEN)) {
|
2018-02-26 12:01:37 +00:00
|
|
|
struct module *owner = disk->fops->owner;
|
|
|
|
|
2017-11-02 18:29:53 +00:00
|
|
|
put_disk(disk);
|
2018-02-26 12:01:37 +00:00
|
|
|
module_put(owner);
|
2017-11-02 18:29:53 +00:00
|
|
|
disk = NULL;
|
|
|
|
}
|
2008-08-25 10:47:22 +00:00
|
|
|
return disk;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2010-04-15 06:54:59 +00:00
|
|
|
EXPORT_SYMBOL(get_gendisk);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2008-09-03 07:01:48 +00: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 07:27:42 +00:00
|
|
|
struct block_device *bdget_disk(struct gendisk *disk, int partno)
|
2008-09-03 07:01:48 +00:00
|
|
|
{
|
2008-08-29 07:01:47 +00:00
|
|
|
struct hd_struct *part;
|
|
|
|
struct block_device *bdev = NULL;
|
2008-09-03 07:01:48 +00:00
|
|
|
|
2008-08-29 07:01:47 +00:00
|
|
|
part = disk_get_part(disk, partno);
|
2008-08-29 09:41:51 +00:00
|
|
|
if (part)
|
2008-08-29 07:01:47 +00:00
|
|
|
bdev = bdget(part_devt(part));
|
|
|
|
disk_put_part(part);
|
2008-09-03 07:01:48 +00:00
|
|
|
|
2008-08-29 07:01:47 +00:00
|
|
|
return bdev;
|
2008-09-03 07:01:48 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(bdget_disk);
|
|
|
|
|
2008-05-22 21:21:08 +00: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 06:57:12 +00: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 07:03:02 +00:00
|
|
|
struct disk_part_iter piter;
|
|
|
|
struct hd_struct *part;
|
2008-08-25 10:47:23 +00:00
|
|
|
char name_buf[BDEVNAME_SIZE];
|
|
|
|
char devt_buf[BDEVT_SIZE];
|
2008-09-03 06:57:12 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Don't show empty devices or things that have been
|
2011-03-31 01:57:33 +00:00
|
|
|
* suppressed
|
2008-09-03 06:57:12 +00: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 10:56:14 +00: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 06:57:12 +00:00
|
|
|
|
2010-08-31 20:47:07 +00:00
|
|
|
printk("%s%s %10llu %s %s", is_part0 ? "" : " ",
|
2008-08-25 10:47:23 +00:00
|
|
|
bdevt_str(part_devt(part), devt_buf),
|
2012-08-01 10:24:18 +00:00
|
|
|
(unsigned long long)part_nr_sects_read(part) >> 1
|
|
|
|
, disk_name(disk, part->partno, name_buf),
|
2012-11-09 00:12:25 +00:00
|
|
|
part->info ? part->info->uuid : "");
|
2008-08-25 10:56:14 +00:00
|
|
|
if (is_part0) {
|
2016-06-16 02:43:07 +00:00
|
|
|
if (dev->parent && dev->parent->driver)
|
2008-08-25 10:56:14 +00:00
|
|
|
printk(" driver: %s\n",
|
2016-06-16 02:43:07 +00:00
|
|
|
dev->parent->driver->name);
|
2008-08-25 10:56:14 +00:00
|
|
|
else
|
|
|
|
printk(" (driver?)\n");
|
|
|
|
} else
|
|
|
|
printk("\n");
|
|
|
|
}
|
2008-09-03 07:03:02 +00:00
|
|
|
disk_part_iter_exit(&piter);
|
2008-09-03 06:57:12 +00:00
|
|
|
}
|
|
|
|
class_dev_iter_exit(&iter);
|
2007-05-09 09:33:24 +00:00
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
#ifdef CONFIG_PROC_FS
|
|
|
|
/* iterator */
|
2008-09-03 06:57:12 +00:00
|
|
|
static void *disk_seqf_start(struct seq_file *seqf, loff_t *pos)
|
2008-05-22 21:21:08 +00:00
|
|
|
{
|
2008-09-03 06:57:12 +00:00
|
|
|
loff_t skip = *pos;
|
|
|
|
struct class_dev_iter *iter;
|
|
|
|
struct device *dev;
|
2008-05-22 21:21:08 +00:00
|
|
|
|
2008-08-28 07:27:42 +00:00
|
|
|
iter = kmalloc(sizeof(*iter), GFP_KERNEL);
|
2008-09-03 06:57:12 +00: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-22 21:21:08 +00:00
|
|
|
}
|
|
|
|
|
2008-09-03 06:57:12 +00:00
|
|
|
static void *disk_seqf_next(struct seq_file *seqf, void *v, loff_t *pos)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2007-05-21 20:08:01 +00:00
|
|
|
struct device *dev;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2008-09-03 06:57:12 +00:00
|
|
|
(*pos)++;
|
|
|
|
dev = class_dev_iter_next(seqf->private);
|
2008-09-03 06:53:37 +00:00
|
|
|
if (dev)
|
2008-05-22 21:21:08 +00:00
|
|
|
return dev_to_disk(dev);
|
2008-09-03 06:53:37 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-09-03 06:57:12 +00:00
|
|
|
static void disk_seqf_stop(struct seq_file *seqf, void *v)
|
2008-05-22 21:21:08 +00:00
|
|
|
{
|
2008-09-03 06:57:12 +00:00
|
|
|
struct class_dev_iter *iter = seqf->private;
|
2008-05-22 21:21:08 +00:00
|
|
|
|
2008-09-03 06:57:12 +00:00
|
|
|
/* stop is called even after start failed :-( */
|
|
|
|
if (iter) {
|
|
|
|
class_dev_iter_exit(iter);
|
|
|
|
kfree(iter);
|
2016-07-29 08:40:31 +00:00
|
|
|
seqf->private = NULL;
|
2008-08-16 12:30:30 +00:00
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2008-09-03 06:57:12 +00:00
|
|
|
static void *show_partition_start(struct seq_file *seqf, loff_t *pos)
|
2005-04-16 22:20:36 +00: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 08:42:00 +00:00
|
|
|
void *p;
|
2008-09-03 06:57:12 +00:00
|
|
|
|
|
|
|
p = disk_seqf_start(seqf, pos);
|
2010-12-17 07:58:36 +00:00
|
|
|
if (!IS_ERR_OR_NULL(p) && !*pos)
|
2008-09-03 06:57:12 +00:00
|
|
|
seq_puts(seqf, "major minor #blocks name\n\n");
|
|
|
|
return p;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2008-09-03 07:01:09 +00:00
|
|
|
static int show_partition(struct seq_file *seqf, void *v)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
struct gendisk *sgp = v;
|
2008-09-03 07:03:02 +00:00
|
|
|
struct disk_part_iter piter;
|
|
|
|
struct hd_struct *part;
|
2005-04-16 22:20:36 +00:00
|
|
|
char buf[BDEVNAME_SIZE];
|
|
|
|
|
|
|
|
/* Don't show non-partitionable removeable devices or empty devices */
|
2011-08-23 18:01:04 +00:00
|
|
|
if (!get_capacity(sgp) || (!disk_max_parts(sgp) &&
|
2008-09-03 07:01:48 +00:00
|
|
|
(sgp->flags & GENHD_FL_REMOVABLE)))
|
2005-04-16 22:20:36 +00: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 10:56:14 +00:00
|
|
|
disk_part_iter_init(&piter, sgp, DISK_PITER_INCL_PART0);
|
2008-09-03 07:03:02 +00:00
|
|
|
while ((part = disk_part_iter_next(&piter)))
|
2008-08-25 10:47:23 +00:00
|
|
|
seq_printf(seqf, "%4d %7d %10llu %s\n",
|
2008-09-03 07:01:48 +00:00
|
|
|
MAJOR(part_devt(part)), MINOR(part_devt(part)),
|
2012-08-01 10:24:18 +00:00
|
|
|
(unsigned long long)part_nr_sects_read(part) >> 1,
|
2008-09-03 07:01:48 +00:00
|
|
|
disk_name(sgp, part->partno, buf));
|
2008-09-03 07:03:02 +00:00
|
|
|
disk_part_iter_exit(&piter);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-10-04 19:53:21 +00:00
|
|
|
static const struct seq_operations partitions_op = {
|
2008-09-03 06:57:12 +00:00
|
|
|
.start = show_partition_start,
|
|
|
|
.next = disk_seqf_next,
|
|
|
|
.stop = disk_seqf_stop,
|
2007-05-21 20:08:01 +00:00
|
|
|
.show = show_partition
|
2005-04-16 22:20:36 +00:00
|
|
|
};
|
2008-10-04 19:53:21 +00:00
|
|
|
|
|
|
|
static int partitions_open(struct inode *inode, struct file *file)
|
|
|
|
{
|
|
|
|
return seq_open(file, &partitions_op);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct file_operations proc_partitions_operations = {
|
|
|
|
.open = partitions_open,
|
|
|
|
.read = seq_read,
|
|
|
|
.llseek = seq_lseek,
|
|
|
|
.release = seq_release,
|
|
|
|
};
|
2005-04-16 22:20:36 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2008-09-03 07:01:09 +00:00
|
|
|
static struct kobject *base_probe(dev_t devt, int *partno, void *data)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2007-05-21 20:08:01 +00:00
|
|
|
if (request_module("block-major-%d-%d", MAJOR(devt), MINOR(devt)) > 0)
|
2005-04-16 22:20:36 +00:00
|
|
|
/* Make old-style 2.4 aliases work */
|
2007-05-21 20:08:01 +00:00
|
|
|
request_module("block-major-%d", MAJOR(devt));
|
2005-04-16 22:20:36 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __init genhd_device_init(void)
|
|
|
|
{
|
2008-04-21 17:51:07 +00:00
|
|
|
int error;
|
|
|
|
|
|
|
|
block_class.dev_kobj = sysfs_dev_block_kobj;
|
|
|
|
error = class_register(&block_class);
|
2008-03-12 00:13:15 +00:00
|
|
|
if (unlikely(error))
|
|
|
|
return error;
|
2007-05-21 20:08:01 +00:00
|
|
|
bdev_map = kobj_map_init(base_probe, &block_class_lock);
|
2005-04-16 22:20:36 +00:00
|
|
|
blk_dev_init();
|
2007-05-21 20:08:01 +00: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 07:26:30 +00:00
|
|
|
register_blkdev(BLOCK_EXT_MAJOR, "blkext");
|
|
|
|
|
2007-05-21 20:08:01 +00:00
|
|
|
/* create top-level block dir */
|
2010-09-08 14:54:17 +00:00
|
|
|
if (!sysfs_deprecated)
|
|
|
|
block_depr = kobject_create_and_add("block", NULL);
|
2007-11-06 18:36:58 +00:00
|
|
|
return 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
subsys_initcall(genhd_device_init);
|
|
|
|
|
2007-05-21 20:08:01 +00:00
|
|
|
static ssize_t disk_range_show(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *buf)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2007-05-21 20:08:01 +00:00
|
|
|
struct gendisk *disk = dev_to_disk(dev);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2007-05-21 20:08:01 +00:00
|
|
|
return sprintf(buf, "%d\n", disk->minors);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2008-08-25 10:47:23 +00: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 07:06:42 +00:00
|
|
|
return sprintf(buf, "%d\n", disk_max_parts(disk));
|
2008-08-25 10:47:23 +00:00
|
|
|
}
|
|
|
|
|
2007-05-21 20:08:01 +00:00
|
|
|
static ssize_t disk_removable_show(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *buf)
|
2005-10-01 12:49:43 +00:00
|
|
|
{
|
2007-05-21 20:08:01 +00:00
|
|
|
struct gendisk *disk = dev_to_disk(dev);
|
2005-10-01 12:49:43 +00:00
|
|
|
|
2007-05-21 20:08:01 +00:00
|
|
|
return sprintf(buf, "%d\n",
|
|
|
|
(disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
|
2005-10-01 12:49:43 +00:00
|
|
|
}
|
|
|
|
|
2017-11-02 18:29:53 +00: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 07:41:00 +00: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 10:56:10 +00:00
|
|
|
return sprintf(buf, "%d\n", get_disk_ro(disk) ? 1 : 0);
|
2008-06-13 07:41:00 +00:00
|
|
|
}
|
|
|
|
|
2007-05-21 20:08:01 +00:00
|
|
|
static ssize_t disk_capability_show(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *buf)
|
2007-05-23 20:57:38 +00:00
|
|
|
{
|
2007-05-21 20:08:01 +00:00
|
|
|
struct gendisk *disk = dev_to_disk(dev);
|
|
|
|
|
|
|
|
return sprintf(buf, "%x\n", disk->flags);
|
2007-05-23 20:57:38 +00:00
|
|
|
}
|
2007-05-21 20:08:01 +00:00
|
|
|
|
2009-05-22 21:17:53 +00: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 10:50:21 +00: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 08:21:48 +00:00
|
|
|
return sprintf(buf, "%d\n", queue_discard_alignment(disk->queue));
|
2009-11-10 10:50:21 +00:00
|
|
|
}
|
|
|
|
|
2007-05-21 20:08:01 +00:00
|
|
|
static DEVICE_ATTR(range, S_IRUGO, disk_range_show, NULL);
|
2008-08-25 10:47:23 +00:00
|
|
|
static DEVICE_ATTR(ext_range, S_IRUGO, disk_ext_range_show, NULL);
|
2007-05-21 20:08:01 +00:00
|
|
|
static DEVICE_ATTR(removable, S_IRUGO, disk_removable_show, NULL);
|
2017-11-02 18:29:53 +00:00
|
|
|
static DEVICE_ATTR(hidden, S_IRUGO, disk_hidden_show, NULL);
|
2008-06-13 07:41:00 +00:00
|
|
|
static DEVICE_ATTR(ro, S_IRUGO, disk_ro_show, NULL);
|
2008-08-25 10:56:09 +00:00
|
|
|
static DEVICE_ATTR(size, S_IRUGO, part_size_show, NULL);
|
2009-05-22 21:17:53 +00:00
|
|
|
static DEVICE_ATTR(alignment_offset, S_IRUGO, disk_alignment_offset_show, NULL);
|
2009-11-10 10:50:21 +00:00
|
|
|
static DEVICE_ATTR(discard_alignment, S_IRUGO, disk_discard_alignment_show,
|
|
|
|
NULL);
|
2007-05-21 20:08:01 +00:00
|
|
|
static DEVICE_ATTR(capability, S_IRUGO, disk_capability_show, NULL);
|
2008-08-25 10:56:14 +00:00
|
|
|
static DEVICE_ATTR(stat, S_IRUGO, part_stat_show, NULL);
|
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-06 18:16:55 +00:00
|
|
|
static DEVICE_ATTR(inflight, S_IRUGO, part_inflight_show, NULL);
|
2016-01-09 16:36:51 +00:00
|
|
|
static DEVICE_ATTR(badblocks, S_IRUGO | S_IWUSR, disk_badblocks_show,
|
|
|
|
disk_badblocks_store);
|
2006-12-08 10:39:46 +00:00
|
|
|
#ifdef CONFIG_FAIL_MAKE_REQUEST
|
2007-05-21 20:08:01 +00:00
|
|
|
static struct device_attribute dev_attr_fail =
|
2008-08-25 10:56:13 +00:00
|
|
|
__ATTR(make-it-fail, S_IRUGO|S_IWUSR, part_fail_show, part_fail_store);
|
2006-12-08 10:39:46 +00:00
|
|
|
#endif
|
2008-09-14 12:56:33 +00:00
|
|
|
#ifdef CONFIG_FAIL_IO_TIMEOUT
|
|
|
|
static struct device_attribute dev_attr_fail_timeout =
|
|
|
|
__ATTR(io-timeout-fail, S_IRUGO|S_IWUSR, part_timeout_show,
|
|
|
|
part_timeout_store);
|
|
|
|
#endif
|
2007-05-21 20:08:01 +00:00
|
|
|
|
|
|
|
static struct attribute *disk_attrs[] = {
|
|
|
|
&dev_attr_range.attr,
|
2008-08-25 10:47:23 +00:00
|
|
|
&dev_attr_ext_range.attr,
|
2007-05-21 20:08:01 +00:00
|
|
|
&dev_attr_removable.attr,
|
2017-11-02 18:29:53 +00:00
|
|
|
&dev_attr_hidden.attr,
|
2008-06-13 07:41:00 +00:00
|
|
|
&dev_attr_ro.attr,
|
2007-05-21 20:08:01 +00:00
|
|
|
&dev_attr_size.attr,
|
2009-05-22 21:17:53 +00:00
|
|
|
&dev_attr_alignment_offset.attr,
|
2009-11-10 10:50:21 +00:00
|
|
|
&dev_attr_discard_alignment.attr,
|
2007-05-21 20:08:01 +00: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-06 18:16:55 +00:00
|
|
|
&dev_attr_inflight.attr,
|
2016-01-09 16:36:51 +00:00
|
|
|
&dev_attr_badblocks.attr,
|
2007-05-21 20:08:01 +00:00
|
|
|
#ifdef CONFIG_FAIL_MAKE_REQUEST
|
|
|
|
&dev_attr_fail.attr,
|
2008-09-14 12:56:33 +00:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_FAIL_IO_TIMEOUT
|
|
|
|
&dev_attr_fail_timeout.attr,
|
2007-05-21 20:08:01 +00:00
|
|
|
#endif
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2017-04-27 21:46:26 +00: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-21 20:08:01 +00:00
|
|
|
static struct attribute_group disk_attr_group = {
|
|
|
|
.attrs = disk_attrs,
|
2017-04-27 21:46:26 +00:00
|
|
|
.is_visible = disk_visible,
|
2007-05-21 20:08:01 +00:00
|
|
|
};
|
|
|
|
|
2009-06-24 17:06:31 +00:00
|
|
|
static const struct attribute_group *disk_attr_groups[] = {
|
2007-05-21 20:08:01 +00:00
|
|
|
&disk_attr_group,
|
|
|
|
NULL
|
2005-04-16 22:20:36 +00:00
|
|
|
};
|
|
|
|
|
2008-08-25 10:56:15 +00: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-17 23:23:06 +00:00
|
|
|
* Matching bd_mutex locked or the caller is the only user of @disk.
|
2008-08-25 10:56:15 +00:00
|
|
|
*/
|
|
|
|
static void disk_replace_part_tbl(struct gendisk *disk,
|
|
|
|
struct disk_part_tbl *new_ptbl)
|
|
|
|
{
|
2017-08-17 23:23:06 +00:00
|
|
|
struct disk_part_tbl *old_ptbl =
|
|
|
|
rcu_dereference_protected(disk->part_tbl, 1);
|
2008-08-25 10:56:15 +00:00
|
|
|
|
|
|
|
rcu_assign_pointer(disk->part_tbl, new_ptbl);
|
2008-10-24 10:52:42 +00:00
|
|
|
|
|
|
|
if (old_ptbl) {
|
|
|
|
rcu_assign_pointer(old_ptbl->last_lookup, NULL);
|
2011-03-18 03:42:58 +00:00
|
|
|
kfree_rcu(old_ptbl, rcu_head);
|
2008-10-24 10:52:42 +00:00
|
|
|
}
|
2008-08-25 10:56:15 +00: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-17 23:23:06 +00:00
|
|
|
* Matching bd_mutex locked or the caller is the only user of @disk.
|
|
|
|
* Might sleep.
|
2008-08-25 10:56:15 +00:00
|
|
|
*
|
|
|
|
* RETURNS:
|
|
|
|
* 0 on success, -errno on failure.
|
|
|
|
*/
|
|
|
|
int disk_expand_part_tbl(struct gendisk *disk, int partno)
|
|
|
|
{
|
2017-08-17 23:23:06 +00:00
|
|
|
struct disk_part_tbl *old_ptbl =
|
|
|
|
rcu_dereference_protected(disk->part_tbl, 1);
|
2008-08-25 10:56:15 +00:00
|
|
|
struct disk_part_tbl *new_ptbl;
|
|
|
|
int len = old_ptbl ? old_ptbl->len : 0;
|
2014-11-19 20:06:22 +00:00
|
|
|
int i, target;
|
2008-08-25 10:56:15 +00:00
|
|
|
size_t size;
|
2014-11-19 20:06:22 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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 10:56:15 +00: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;
|
|
|
|
|
|
|
|
size = sizeof(*new_ptbl) + target * sizeof(new_ptbl->part[0]);
|
|
|
|
new_ptbl = kzalloc_node(size, GFP_KERNEL, disk->node_id);
|
|
|
|
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-21 20:08:01 +00:00
|
|
|
static void disk_release(struct device *dev)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2007-05-21 20:08:01 +00:00
|
|
|
struct gendisk *disk = dev_to_disk(dev);
|
|
|
|
|
2014-08-26 15:05:36 +00: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-08 19:57:37 +00:00
|
|
|
disk_release_events(disk);
|
2005-04-16 22:20:36 +00:00
|
|
|
kfree(disk->random);
|
2008-08-25 10:56:15 +00:00
|
|
|
disk_replace_part_tbl(disk, NULL);
|
2015-07-16 03:16:44 +00:00
|
|
|
hd_free_part(&disk->part0);
|
2011-10-19 12:31:07 +00:00
|
|
|
if (disk->queue)
|
|
|
|
blk_put_queue(disk->queue);
|
2005-04-16 22:20:36 +00:00
|
|
|
kfree(disk);
|
|
|
|
}
|
2007-05-21 20:08:01 +00:00
|
|
|
struct class block_class = {
|
|
|
|
.name = "block",
|
2005-04-16 22:20:36 +00:00
|
|
|
};
|
|
|
|
|
2013-04-06 16:56:00 +00:00
|
|
|
static char *block_devnode(struct device *dev, umode_t *mode,
|
2013-04-11 18:43:29 +00:00
|
|
|
kuid_t *uid, kgid_t *gid)
|
2009-04-30 13:23:42 +00:00
|
|
|
{
|
|
|
|
struct gendisk *disk = dev_to_disk(dev);
|
|
|
|
|
2009-09-18 21:01:12 +00:00
|
|
|
if (disk->devnode)
|
|
|
|
return disk->devnode(disk, mode);
|
2009-04-30 13:23:42 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-06-20 18:15:48 +00:00
|
|
|
static const struct device_type disk_type = {
|
2007-05-21 20:08:01 +00:00
|
|
|
.name = "disk",
|
|
|
|
.groups = disk_attr_groups,
|
|
|
|
.release = disk_release,
|
2009-09-18 21:01:12 +00:00
|
|
|
.devnode = block_devnode,
|
2005-04-16 22:20:36 +00:00
|
|
|
};
|
|
|
|
|
2008-05-23 16:44:11 +00:00
|
|
|
#ifdef CONFIG_PROC_FS
|
2008-09-03 07:01:09 +00: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-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
struct gendisk *gp = v;
|
2008-09-03 07:03:02 +00:00
|
|
|
struct disk_part_iter piter;
|
|
|
|
struct hd_struct *hd;
|
2005-04-16 22:20:36 +00:00
|
|
|
char buf[BDEVNAME_SIZE];
|
2017-08-08 23:49:47 +00:00
|
|
|
unsigned int inflight[2];
|
2008-08-25 10:47:21 +00:00
|
|
|
int cpu;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/*
|
2008-08-25 10:56:05 +00:00
|
|
|
if (&disk_to_dev(gp)->kobj.entry == block_class.devices.next)
|
2008-09-03 07:01:09 +00:00
|
|
|
seq_puts(seqf, "major minor name"
|
2005-04-16 22:20:36 +00:00
|
|
|
" rio rmerge rsect ruse wio wmerge "
|
|
|
|
"wsect wuse running use aveq"
|
|
|
|
"\n\n");
|
|
|
|
*/
|
2011-06-13 08:45:43 +00:00
|
|
|
|
2009-04-17 06:34:48 +00:00
|
|
|
disk_part_iter_init(&piter, gp, DISK_PITER_INCL_EMPTY_PART0);
|
2008-09-03 07:03:02 +00:00
|
|
|
while ((hd = disk_part_iter_next(&piter))) {
|
2008-08-25 10:56:14 +00:00
|
|
|
cpu = part_stat_lock();
|
2017-07-01 03:55:08 +00:00
|
|
|
part_round_stats(gp->queue, cpu, hd);
|
2008-08-25 10:56:14 +00:00
|
|
|
part_stat_unlock();
|
2017-08-08 23:49:47 +00:00
|
|
|
part_in_flight(gp->queue, hd, inflight);
|
2011-08-02 10:43:50 +00:00
|
|
|
seq_printf(seqf, "%4d %7d %s %lu %lu %lu "
|
|
|
|
"%u %lu %lu %lu %u %u %u %u\n",
|
2008-09-03 07:01:48 +00:00
|
|
|
MAJOR(part_devt(hd)), MINOR(part_devt(hd)),
|
|
|
|
disk_name(gp, hd->partno, buf),
|
2011-03-02 16:00:15 +00:00
|
|
|
part_stat_read(hd, ios[READ]),
|
|
|
|
part_stat_read(hd, merges[READ]),
|
2011-08-02 10:43:50 +00:00
|
|
|
part_stat_read(hd, sectors[READ]),
|
2011-03-02 16:00:15 +00:00
|
|
|
jiffies_to_msecs(part_stat_read(hd, ticks[READ])),
|
|
|
|
part_stat_read(hd, ios[WRITE]),
|
|
|
|
part_stat_read(hd, merges[WRITE]),
|
2011-08-02 10:43:50 +00:00
|
|
|
part_stat_read(hd, sectors[WRITE]),
|
2011-03-02 16:00:15 +00:00
|
|
|
jiffies_to_msecs(part_stat_read(hd, ticks[WRITE])),
|
2017-08-08 23:49:47 +00:00
|
|
|
inflight[0],
|
2008-02-08 10:04:56 +00:00
|
|
|
jiffies_to_msecs(part_stat_read(hd, io_ticks)),
|
|
|
|
jiffies_to_msecs(part_stat_read(hd, time_in_queue))
|
|
|
|
);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2008-09-03 07:03:02 +00:00
|
|
|
disk_part_iter_exit(&piter);
|
2011-06-13 08:45:43 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-10-06 08:55:38 +00:00
|
|
|
static const struct seq_operations diskstats_op = {
|
2008-09-03 06:57:12 +00:00
|
|
|
.start = disk_seqf_start,
|
|
|
|
.next = disk_seqf_next,
|
|
|
|
.stop = disk_seqf_stop,
|
2005-04-16 22:20:36 +00:00
|
|
|
.show = diskstats_show
|
|
|
|
};
|
2008-10-04 19:53:21 +00:00
|
|
|
|
2008-10-06 08:55:38 +00:00
|
|
|
static int diskstats_open(struct inode *inode, struct file *file)
|
|
|
|
{
|
|
|
|
return seq_open(file, &diskstats_op);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct file_operations proc_diskstats_operations = {
|
|
|
|
.open = diskstats_open,
|
|
|
|
.read = seq_read,
|
|
|
|
.llseek = seq_lseek,
|
|
|
|
.release = seq_release,
|
|
|
|
};
|
|
|
|
|
2008-10-04 19:53:21 +00:00
|
|
|
static int __init proc_genhd_init(void)
|
|
|
|
{
|
2008-10-06 08:55:38 +00:00
|
|
|
proc_create("diskstats", 0, NULL, &proc_diskstats_operations);
|
2008-10-04 19:53:21 +00:00
|
|
|
proc_create("partitions", 0, NULL, &proc_partitions_operations);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
module_init(proc_genhd_init);
|
2008-05-23 16:44:11 +00:00
|
|
|
#endif /* CONFIG_PROC_FS */
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2008-09-03 07:01:09 +00:00
|
|
|
dev_t blk_lookup_devt(const char *name, int partno)
|
2008-05-22 21:21:08 +00:00
|
|
|
{
|
2008-09-03 06:57:12 +00:00
|
|
|
dev_t devt = MKDEV(0, 0);
|
|
|
|
struct class_dev_iter iter;
|
|
|
|
struct device *dev;
|
2008-05-22 21:21:08 +00:00
|
|
|
|
2008-09-03 06:57:12 +00:00
|
|
|
class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
|
|
|
|
while ((dev = class_dev_iter_next(&iter))) {
|
2008-05-22 21:21:08 +00:00
|
|
|
struct gendisk *disk = dev_to_disk(dev);
|
2008-08-29 07:01:47 +00:00
|
|
|
struct hd_struct *part;
|
2008-05-22 21:21:08 +00:00
|
|
|
|
2009-01-06 18:44:43 +00:00
|
|
|
if (strcmp(dev_name(dev), name))
|
2008-09-03 07:01:48 +00:00
|
|
|
continue;
|
|
|
|
|
2009-02-18 09:33:59 +00: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 07:01:47 +00:00
|
|
|
part = disk_get_part(disk, partno);
|
2008-08-29 09:41:51 +00:00
|
|
|
if (part) {
|
2008-09-03 07:01:48 +00:00
|
|
|
devt = part_devt(part);
|
2008-09-03 07:03:02 +00:00
|
|
|
disk_put_part(part);
|
2008-08-29 07:01:47 +00:00
|
|
|
break;
|
2008-09-03 06:57:12 +00:00
|
|
|
}
|
2008-08-29 07:01:47 +00:00
|
|
|
disk_put_part(part);
|
2008-08-16 12:30:30 +00:00
|
|
|
}
|
2008-09-03 06:57:12 +00:00
|
|
|
class_dev_iter_exit(&iter);
|
2007-05-21 20:08:01 +00:00
|
|
|
return devt;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(blk_lookup_devt);
|
|
|
|
|
2017-10-25 08:56:05 +00:00
|
|
|
struct gendisk *__alloc_disk_node(int minors, int node_id)
|
2005-06-23 07:08:19 +00:00
|
|
|
{
|
|
|
|
struct gendisk *disk;
|
2017-08-17 23:23:06 +00:00
|
|
|
struct disk_part_tbl *ptbl;
|
2005-06-23 07:08:19 +00:00
|
|
|
|
2017-08-23 17:10:29 +00:00
|
|
|
if (minors > DISK_MAX_PARTS) {
|
|
|
|
printk(KERN_ERR
|
2017-11-19 01:43:38 +00:00
|
|
|
"block: can't allocate more than %d partitions\n",
|
2017-08-23 17:10:29 +00:00
|
|
|
DISK_MAX_PARTS);
|
|
|
|
minors = DISK_MAX_PARTS;
|
|
|
|
}
|
2005-06-23 07:08:19 +00:00
|
|
|
|
2013-08-29 22:21:42 +00:00
|
|
|
disk = kzalloc_node(sizeof(struct gendisk), GFP_KERNEL, node_id);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (disk) {
|
2008-08-25 10:56:14 +00:00
|
|
|
if (!init_part_stats(&disk->part0)) {
|
2005-04-16 22:20:36 +00:00
|
|
|
kfree(disk);
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-11-20 07:37:37 +00:00
|
|
|
disk->node_id = node_id;
|
2008-08-25 10:56:15 +00:00
|
|
|
if (disk_expand_part_tbl(disk, 0)) {
|
|
|
|
free_part_stats(&disk->part0);
|
2008-09-03 07:06:42 +00:00
|
|
|
kfree(disk);
|
|
|
|
return NULL;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2017-08-17 23:23:06 +00:00
|
|
|
ptbl = rcu_dereference_protected(disk->part_tbl, 1);
|
|
|
|
rcu_assign_pointer(ptbl->part[0], &disk->part0);
|
2011-01-07 07:43:37 +00:00
|
|
|
|
2012-08-01 10:24:18 +00: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 03:16:45 +00:00
|
|
|
if (hd_ref_init(&disk->part0)) {
|
|
|
|
hd_free_part(&disk->part0);
|
|
|
|
kfree(disk);
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-09-03 07:06:42 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
disk->minors = minors;
|
|
|
|
rand_initialize_disk(disk);
|
2008-08-25 10:56:05 +00:00
|
|
|
disk_to_dev(disk)->class = &block_class;
|
|
|
|
disk_to_dev(disk)->type = &disk_type;
|
|
|
|
device_initialize(disk_to_dev(disk));
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
return disk;
|
|
|
|
}
|
2017-10-25 08:56:05 +00:00
|
|
|
EXPORT_SYMBOL(__alloc_disk_node);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
struct kobject *get_disk(struct gendisk *disk)
|
|
|
|
{
|
|
|
|
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 00:37:02 +00:00
|
|
|
kobj = kobject_get_unless_zero(&disk_to_dev(disk)->kobj);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (kobj == NULL) {
|
|
|
|
module_put(owner);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return kobj;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
EXPORT_SYMBOL(get_disk);
|
|
|
|
|
|
|
|
void put_disk(struct gendisk *disk)
|
|
|
|
{
|
|
|
|
if (disk)
|
2008-08-25 10:56:05 +00:00
|
|
|
kobject_put(&disk_to_dev(disk)->kobj);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
EXPORT_SYMBOL(put_disk);
|
|
|
|
|
2009-07-28 07:13:13 +00: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-16 22:20:36 +00:00
|
|
|
void set_device_ro(struct block_device *bdev, int flag)
|
|
|
|
{
|
2008-08-25 10:56:10 +00:00
|
|
|
bdev->bd_part->policy = flag;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
EXPORT_SYMBOL(set_device_ro);
|
|
|
|
|
|
|
|
void set_disk_ro(struct gendisk *disk, int flag)
|
|
|
|
{
|
2008-09-03 07:03:02 +00:00
|
|
|
struct disk_part_iter piter;
|
|
|
|
struct hd_struct *part;
|
|
|
|
|
2009-07-28 07:13:13 +00: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 07:03:02 +00:00
|
|
|
while ((part = disk_part_iter_next(&piter)))
|
|
|
|
part->policy = flag;
|
|
|
|
disk_part_iter_exit(&piter);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
EXPORT_SYMBOL(set_disk_ro);
|
|
|
|
|
|
|
|
int bdev_read_only(struct block_device *bdev)
|
|
|
|
{
|
|
|
|
if (!bdev)
|
|
|
|
return 0;
|
2008-08-25 10:56:10 +00:00
|
|
|
return bdev->bd_part->policy;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
EXPORT_SYMBOL(bdev_read_only);
|
|
|
|
|
2008-09-03 07:01:09 +00:00
|
|
|
int invalidate_partition(struct gendisk *disk, int partno)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
int res = 0;
|
2008-09-03 07:01:09 +00:00
|
|
|
struct block_device *bdev = bdget_disk(disk, partno);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (bdev) {
|
2005-05-05 23:15:59 +00:00
|
|
|
fsync_bdev(bdev);
|
2011-02-24 06:25:47 +00:00
|
|
|
res = __invalidate_device(bdev, true);
|
2005-04-16 22:20:36 +00: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-08 19:57:37 +00: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-09 18:43:59 +00: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-08 19:57:37 +00: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 01:58:46 +00: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-08 19:57:37 +00: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
|
|
|
|
* the default is being used, poll iff there are events which
|
|
|
|
* can't be monitored asynchronously.
|
|
|
|
*/
|
|
|
|
if (ev->poll_msecs >= 0)
|
|
|
|
intv_msecs = ev->poll_msecs;
|
|
|
|
else if (disk->events & ~disk->async_events)
|
|
|
|
intv_msecs = disk_events_dfl_poll_msecs;
|
|
|
|
|
|
|
|
return msecs_to_jiffies(intv_msecs);
|
|
|
|
}
|
|
|
|
|
2011-06-09 18:43:55 +00: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-08 19:57:37 +00:00
|
|
|
{
|
|
|
|
struct disk_events *ev = disk->ev;
|
|
|
|
unsigned long flags;
|
|
|
|
bool cancel;
|
|
|
|
|
2011-06-09 18:43:55 +00:00
|
|
|
if (!ev)
|
|
|
|
return;
|
|
|
|
|
2011-06-09 18:43:59 +00: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-08 19:57:37 +00:00
|
|
|
spin_lock_irqsave(&ev->lock, flags);
|
|
|
|
cancel = !ev->block++;
|
|
|
|
spin_unlock_irqrestore(&ev->lock, flags);
|
|
|
|
|
2011-06-09 18:43:55 +00:00
|
|
|
if (cancel)
|
|
|
|
cancel_delayed_work_sync(&disk->ev->dwork);
|
2011-06-09 18:43:59 +00: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-08 19:57:37 +00: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 11:42:56 +00: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-08 19:57:37 +00:00
|
|
|
else if (intv)
|
2013-04-24 11:42:56 +00: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-08 19:57:37 +00: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-09 18:54:27 +00: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-08 19:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-07-01 14:17:47 +00: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-08 19:57:37 +00:00
|
|
|
*
|
2011-07-01 14:17:47 +00: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-08 19:57:37 +00:00
|
|
|
*
|
|
|
|
* CONTEXT:
|
2011-07-01 14:17:47 +00: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-08 19:57:37 +00:00
|
|
|
*/
|
2011-07-01 14:17:47 +00: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-08 19:57:37 +00:00
|
|
|
{
|
2011-06-09 18:43:54 +00:00
|
|
|
struct disk_events *ev = disk->ev;
|
|
|
|
|
|
|
|
if (!ev)
|
|
|
|
return;
|
|
|
|
|
2011-07-01 14:17:47 +00:00
|
|
|
spin_lock_irq(&ev->lock);
|
|
|
|
ev->clearing |= mask;
|
2012-08-03 17:30:47 +00:00
|
|
|
if (!ev->block)
|
2013-04-24 11:42:56 +00:00
|
|
|
mod_delayed_work(system_freezable_power_efficient_wq,
|
|
|
|
&ev->dwork, 0);
|
2011-07-01 14:17:47 +00: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-08 19:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* disk_clear_events - synchronously check, clear and return pending events
|
|
|
|
* @disk: disk to fetch and clear events from
|
2014-09-08 16:27:23 +00: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-08 19:57:37 +00: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-18 20:27:20 +00: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-08 19:57:37 +00: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-18 20:27:20 +00: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-08 19:57:37 +00:00
|
|
|
spin_lock_irq(&ev->lock);
|
2012-12-18 20:27:20 +00: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-08 19:57:37 +00:00
|
|
|
spin_unlock_irq(&ev->lock);
|
|
|
|
|
2012-12-18 20:27:20 +00: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-18 20:27:18 +00:00
|
|
|
/*
|
2012-12-18 20:27:20 +00: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-18 20:27:18 +00:00
|
|
|
*/
|
2012-12-18 20:27:20 +00: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-08 19:57:37 +00: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-18 20:27:20 +00: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-08 19:57:37 +00:00
|
|
|
|
|
|
|
return pending;
|
|
|
|
}
|
|
|
|
|
2012-12-18 20:27:20 +00: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-08 19:57:37 +00: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-18 20:27:20 +00: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-08 19:57:37 +00:00
|
|
|
struct gendisk *disk = ev->disk;
|
|
|
|
char *envp[ARRAY_SIZE(disk_uevents) + 1] = { };
|
2012-12-18 20:27:20 +00: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-08 19:57:37 +00: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-18 20:27:20 +00: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-08 19:57:37 +00:00
|
|
|
|
|
|
|
intv = disk_events_poll_jiffies(disk);
|
|
|
|
if (!ev->block && intv)
|
2013-04-24 11:42:56 +00: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-08 19:57:37 +00:00
|
|
|
|
|
|
|
spin_unlock_irq(&ev->lock);
|
|
|
|
|
2011-04-21 17:43:58 +00:00
|
|
|
/*
|
|
|
|
* Tell userland about new events. Only the events listed in
|
|
|
|
* @disk->events are reported. Unlisted events are processed the
|
|
|
|
* same internally but never get reported to userland.
|
|
|
|
*/
|
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-08 19:57:37 +00:00
|
|
|
for (i = 0; i < ARRAY_SIZE(disk_uevents); i++)
|
2011-04-21 17:43:58 +00:00
|
|
|
if (events & disk->events & (1 << i))
|
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-08 19:57:37 +00: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
|
|
|
|
* 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);
|
|
|
|
|
|
|
|
return __disk_events_show(disk->events, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t disk_events_async_show(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *buf)
|
|
|
|
{
|
|
|
|
struct gendisk *disk = dev_to_disk(dev);
|
|
|
|
|
|
|
|
return __disk_events_show(disk->async_events, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t disk_events_poll_msecs_show(struct device *dev,
|
|
|
|
struct device_attribute *attr,
|
|
|
|
char *buf)
|
|
|
|
{
|
|
|
|
struct gendisk *disk = dev_to_disk(dev);
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2011-06-09 18:43:55 +00: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-08 19:57:37 +00:00
|
|
|
disk->ev->poll_msecs = intv;
|
|
|
|
__disk_unblock_events(disk, true);
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const DEVICE_ATTR(events, S_IRUGO, disk_events_show, NULL);
|
|
|
|
static const DEVICE_ATTR(events_async, S_IRUGO, disk_events_async_show, NULL);
|
|
|
|
static const DEVICE_ATTR(events_poll_msecs, S_IRUGO|S_IWUSR,
|
|
|
|
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
|
|
|
|
* /sys/module/block/events_dfl_poll_msecs.
|
|
|
|
*/
|
|
|
|
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 14:17:47 +00: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-08 19:57:37 +00: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 09:43:28 +00: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-08 19:57:37 +00:00
|
|
|
*/
|
2012-03-02 09:43:28 +00: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-08 19:57:37 +00:00
|
|
|
{
|
|
|
|
struct disk_events *ev;
|
|
|
|
|
2011-05-26 19:06:50 +00:00
|
|
|
if (!disk->fops->check_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-08 19:57:37 +00: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-09 18:43:59 +00: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-08 19:57:37 +00:00
|
|
|
ev->block = 1;
|
|
|
|
ev->poll_msecs = -1;
|
|
|
|
INIT_DELAYED_WORK(&ev->dwork, disk_events_workfn);
|
|
|
|
|
2012-03-02 09:43:28 +00:00
|
|
|
disk->ev = ev;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void disk_add_events(struct gendisk *disk)
|
|
|
|
{
|
|
|
|
if (!disk->ev)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
|
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-08 19:57:37 +00:00
|
|
|
mutex_lock(&disk_events_mutex);
|
2012-03-02 09:43:28 +00: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-08 19:57:37 +00: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)
|
|
|
|
{
|
|
|
|
if (!disk->ev)
|
|
|
|
return;
|
|
|
|
|
2011-06-09 18:43:55 +00: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-08 19:57:37 +00:00
|
|
|
|
|
|
|
mutex_lock(&disk_events_mutex);
|
|
|
|
list_del_init(&disk->ev->node);
|
|
|
|
mutex_unlock(&disk_events_mutex);
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|