2019-04-30 18:42:43 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* gendisk handling
|
2020-12-10 07:55:44 +00:00
|
|
|
*
|
|
|
|
* Portions Copyright (C) 2020 Christoph Hellwig
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
2020-03-24 07:25:13 +00:00
|
|
|
#include <linux/ctype.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
#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>
|
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"
|
|
|
|
|
2020-03-25 15:48:35 +00:00
|
|
|
static 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)
|
2020-11-26 08:23:26 +00:00
|
|
|
static DEFINE_IDA(ext_devt_ida);
|
2008-08-25 10:47:22 +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);
|
|
|
|
|
2020-11-26 17:43:37 +00:00
|
|
|
void set_capacity(struct gendisk *disk, sector_t sectors)
|
|
|
|
{
|
2020-11-26 17:47:17 +00:00
|
|
|
struct block_device *bdev = disk->part0;
|
2020-11-26 17:43:37 +00:00
|
|
|
|
2021-03-01 03:04:02 +00:00
|
|
|
spin_lock(&bdev->bd_size_lock);
|
2020-11-26 17:43:37 +00:00
|
|
|
i_size_write(bdev->bd_inode, (loff_t)sectors << SECTOR_SHIFT);
|
2021-03-01 03:04:02 +00:00
|
|
|
spin_unlock(&bdev->bd_size_lock);
|
2020-11-26 17:43:37 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(set_capacity);
|
|
|
|
|
2020-03-13 05:30:05 +00:00
|
|
|
/*
|
2020-11-16 14:56:56 +00:00
|
|
|
* Set disk capacity and notify if the size is not currently zero and will not
|
|
|
|
* be set to zero. Returns true if a uevent was sent, otherwise false.
|
2020-03-13 05:30:05 +00:00
|
|
|
*/
|
2020-11-16 14:56:56 +00:00
|
|
|
bool set_capacity_and_notify(struct gendisk *disk, sector_t size)
|
2020-03-13 05:30:05 +00:00
|
|
|
{
|
|
|
|
sector_t capacity = get_capacity(disk);
|
2020-11-26 17:43:37 +00:00
|
|
|
char *envp[] = { "RESIZE=1", NULL };
|
2020-03-13 05:30:05 +00:00
|
|
|
|
|
|
|
set_capacity(disk, size);
|
|
|
|
|
2020-11-26 17:43:37 +00:00
|
|
|
/*
|
|
|
|
* Only print a message and send a uevent if the gendisk is user visible
|
|
|
|
* and alive. This avoids spamming the log and udev when setting the
|
|
|
|
* initial capacity during probing.
|
|
|
|
*/
|
|
|
|
if (size == capacity ||
|
|
|
|
(disk->flags & (GENHD_FL_UP | GENHD_FL_HIDDEN)) != GENHD_FL_UP)
|
|
|
|
return false;
|
2020-03-13 05:30:05 +00:00
|
|
|
|
2020-11-26 17:43:37 +00:00
|
|
|
pr_info("%s: detected capacity change from %lld to %lld\n",
|
2021-02-23 08:50:15 +00:00
|
|
|
disk->disk_name, capacity, size);
|
2020-11-12 16:50:04 +00:00
|
|
|
|
2020-11-26 17:43:37 +00:00
|
|
|
/*
|
|
|
|
* Historically we did not send a uevent for changes to/from an empty
|
|
|
|
* device.
|
|
|
|
*/
|
|
|
|
if (!capacity || !size)
|
|
|
|
return false;
|
|
|
|
kobject_uevent_env(&disk_to_dev(disk)->kobj, KOBJ_CHANGE, envp);
|
|
|
|
return true;
|
2020-03-13 05:30:05 +00:00
|
|
|
}
|
2020-11-16 14:56:56 +00:00
|
|
|
EXPORT_SYMBOL_GPL(set_capacity_and_notify);
|
2020-03-13 05:30:05 +00:00
|
|
|
|
2020-03-24 07:25:12 +00:00
|
|
|
/*
|
|
|
|
* Format the device name of the indicated disk into the supplied buffer and
|
|
|
|
* return a pointer to that same buffer for convenience.
|
|
|
|
*/
|
|
|
|
char *disk_name(struct gendisk *hd, int partno, char *buf)
|
|
|
|
{
|
|
|
|
if (!partno)
|
|
|
|
snprintf(buf, BDEVNAME_SIZE, "%s", hd->disk_name);
|
|
|
|
else if (isdigit(hd->disk_name[strlen(hd->disk_name)-1]))
|
|
|
|
snprintf(buf, BDEVNAME_SIZE, "%sp%d", hd->disk_name, partno);
|
|
|
|
else
|
|
|
|
snprintf(buf, BDEVNAME_SIZE, "%s%d", hd->disk_name, partno);
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *bdevname(struct block_device *bdev, char *buf)
|
|
|
|
{
|
2020-09-03 05:41:03 +00:00
|
|
|
return disk_name(bdev->bd_disk, bdev->bd_partno, buf);
|
2020-03-24 07:25:12 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(bdevname);
|
2020-03-13 05:30:05 +00:00
|
|
|
|
2020-11-27 15:43:51 +00:00
|
|
|
static void part_stat_read_all(struct block_device *part,
|
|
|
|
struct disk_stats *stat)
|
2020-03-25 13:07:06 +00:00
|
|
|
{
|
|
|
|
int cpu;
|
|
|
|
|
|
|
|
memset(stat, 0, sizeof(struct disk_stats));
|
|
|
|
for_each_possible_cpu(cpu) {
|
2020-11-27 15:43:51 +00:00
|
|
|
struct disk_stats *ptr = per_cpu_ptr(part->bd_stats, cpu);
|
2020-03-25 13:07:06 +00:00
|
|
|
int group;
|
|
|
|
|
|
|
|
for (group = 0; group < NR_STAT_GROUPS; group++) {
|
|
|
|
stat->nsecs[group] += ptr->nsecs[group];
|
|
|
|
stat->sectors[group] += ptr->sectors[group];
|
|
|
|
stat->ios[group] += ptr->ios[group];
|
|
|
|
stat->merges[group] += ptr->merges[group];
|
|
|
|
}
|
|
|
|
|
|
|
|
stat->io_ticks += ptr->io_ticks;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-24 08:36:54 +00:00
|
|
|
static unsigned int part_in_flight(struct block_device *part)
|
2017-08-08 23:51:45 +00:00
|
|
|
{
|
2020-05-13 10:49:33 +00:00
|
|
|
unsigned int inflight = 0;
|
2018-12-06 16:41:20 +00:00
|
|
|
int cpu;
|
2017-08-08 23:51:45 +00:00
|
|
|
|
2018-12-06 16:41:20 +00:00
|
|
|
for_each_possible_cpu(cpu) {
|
2018-12-06 16:41:21 +00:00
|
|
|
inflight += part_stat_local_read_cpu(part, in_flight[0], cpu) +
|
|
|
|
part_stat_local_read_cpu(part, in_flight[1], cpu);
|
2018-12-06 16:41:20 +00:00
|
|
|
}
|
2018-12-06 16:41:21 +00:00
|
|
|
if ((int)inflight < 0)
|
|
|
|
inflight = 0;
|
2018-12-06 16:41:20 +00:00
|
|
|
|
2018-12-06 16:41:21 +00:00
|
|
|
return inflight;
|
2017-08-08 23:51:45 +00:00
|
|
|
}
|
|
|
|
|
2020-11-24 08:36:54 +00:00
|
|
|
static void part_in_flight_rw(struct block_device *part,
|
|
|
|
unsigned int inflight[2])
|
2018-04-26 07:21:59 +00:00
|
|
|
{
|
2018-12-06 16:41:20 +00:00
|
|
|
int cpu;
|
|
|
|
|
|
|
|
inflight[0] = 0;
|
|
|
|
inflight[1] = 0;
|
|
|
|
for_each_possible_cpu(cpu) {
|
|
|
|
inflight[0] += part_stat_local_read_cpu(part, in_flight[0], cpu);
|
|
|
|
inflight[1] += part_stat_local_read_cpu(part, in_flight[1], cpu);
|
|
|
|
}
|
|
|
|
if ((int)inflight[0] < 0)
|
|
|
|
inflight[0] = 0;
|
|
|
|
if ((int)inflight[1] < 0)
|
|
|
|
inflight[1] = 0;
|
2018-04-26 07:21:59 +00:00
|
|
|
}
|
|
|
|
|
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];
|
2020-10-29 14:58:28 +00:00
|
|
|
void (*probe)(dev_t devt);
|
2006-03-31 10:30:32 +00:00
|
|
|
} *major_names[BLKDEV_MAJOR_HASH_SIZE];
|
2020-10-29 14:58:26 +00:00
|
|
|
static DEFINE_MUTEX(major_names_lock);
|
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
|
|
|
|
2020-10-29 14:58:26 +00:00
|
|
|
mutex_lock(&major_names_lock);
|
2017-06-16 23:48:21 +00:00
|
|
|
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);
|
2020-10-29 14:58:26 +00:00
|
|
|
mutex_unlock(&major_names_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
|
|
|
/**
|
2020-11-14 17:08:21 +00:00
|
|
|
* __register_blkdev - register a new block device
|
2009-02-20 07:12:51 +00:00
|
|
|
*
|
2018-02-06 02:25:27 +00:00
|
|
|
* @major: the requested major device number [1..BLKDEV_MAJOR_MAX-1]. If
|
|
|
|
* @major = 0, try to allocate any unused major number.
|
2009-02-20 07:12:51 +00:00
|
|
|
* @name: the name of the new block device as a zero terminated string
|
2020-11-14 17:08:21 +00:00
|
|
|
* @probe: allback that is called on access to any minor number of @major
|
2009-02-20 07:12:51 +00:00
|
|
|
*
|
|
|
|
* 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:
|
|
|
|
*
|
2018-02-06 02:25:27 +00:00
|
|
|
* - if a major device number was requested in range [1..BLKDEV_MAJOR_MAX-1]
|
|
|
|
* then the function returns zero on success, or a negative error code
|
2017-03-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
|
2018-02-06 02:25:27 +00:00
|
|
|
* [1..BLKDEV_MAJOR_MAX-1] or a negative error code otherwise
|
|
|
|
*
|
|
|
|
* See Documentation/admin-guide/devices.txt for the list of allocated
|
|
|
|
* major numbers.
|
2020-11-14 17:08:21 +00:00
|
|
|
*
|
|
|
|
* Use register_blkdev instead for any new code.
|
2009-02-20 07:12:51 +00:00
|
|
|
*/
|
2020-10-29 14:58:28 +00:00
|
|
|
int __register_blkdev(unsigned int major, const char *name,
|
|
|
|
void (*probe)(dev_t devt))
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
struct blk_major_name **n, *p;
|
|
|
|
int index, ret = 0;
|
|
|
|
|
2020-10-29 14:58:26 +00:00
|
|
|
mutex_lock(&major_names_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) {
|
2019-02-17 15:21:56 +00:00
|
|
|
printk("%s: failed to get major for %s\n",
|
|
|
|
__func__, name);
|
2005-04-16 22:20:36 +00:00
|
|
|
ret = -EBUSY;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
major = index;
|
|
|
|
ret = major;
|
|
|
|
}
|
|
|
|
|
2017-06-16 23:48:21 +00:00
|
|
|
if (major >= BLKDEV_MAJOR_MAX) {
|
2019-02-17 15:21:56 +00:00
|
|
|
pr_err("%s: major requested (%u) is greater than the maximum (%u) for %s\n",
|
|
|
|
__func__, major, BLKDEV_MAJOR_MAX-1, name);
|
2017-06-16 23:48:21 +00:00
|
|
|
|
|
|
|
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;
|
2020-10-29 14:58:28 +00:00
|
|
|
p->probe = probe;
|
2005-04-16 22:20:36 +00:00
|
|
|
strlcpy(p->name, name, sizeof(p->name));
|
|
|
|
p->next = NULL;
|
|
|
|
index = major_to_index(major);
|
|
|
|
|
|
|
|
for (n = &major_names[index]; *n; n = &(*n)->next) {
|
|
|
|
if ((*n)->major == major)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!*n)
|
|
|
|
*n = p;
|
|
|
|
else
|
|
|
|
ret = -EBUSY;
|
|
|
|
|
|
|
|
if (ret < 0) {
|
2018-02-06 02:25:27 +00:00
|
|
|
printk("register_blkdev: cannot get major %u for %s\n",
|
2005-04-16 22:20:36 +00:00
|
|
|
major, name);
|
|
|
|
kfree(p);
|
|
|
|
}
|
|
|
|
out:
|
2020-10-29 14:58:26 +00:00
|
|
|
mutex_unlock(&major_names_lock);
|
2005-04-16 22:20:36 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2020-10-29 14:58:28 +00:00
|
|
|
EXPORT_SYMBOL(__register_blkdev);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
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);
|
|
|
|
|
2020-10-29 14:58:26 +00:00
|
|
|
mutex_lock(&major_names_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;
|
|
|
|
}
|
2020-10-29 14:58:26 +00:00
|
|
|
mutex_unlock(&major_names_lock);
|
2005-04-16 22:20:36 +00:00
|
|
|
kfree(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
EXPORT_SYMBOL(unregister_blkdev);
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-05-21 05:50:51 +00:00
|
|
|
int blk_alloc_ext_minor(void)
|
2008-08-25 10:47:22 +00:00
|
|
|
{
|
2013-02-28 01:03:57 +00:00
|
|
|
int idx;
|
2008-08-25 10:47:22 +00:00
|
|
|
|
2020-11-26 08:23:26 +00:00
|
|
|
idx = ida_alloc_range(&ext_devt_ida, 0, NR_EXT_DEVT, GFP_KERNEL);
|
2021-05-21 05:50:51 +00:00
|
|
|
if (idx < 0) {
|
|
|
|
if (idx == -ENOSPC)
|
|
|
|
return -EBUSY;
|
|
|
|
return idx;
|
|
|
|
}
|
|
|
|
return blk_mangle_minor(idx);
|
2008-08-25 10:47:22 +00:00
|
|
|
}
|
|
|
|
|
2021-05-21 05:50:51 +00:00
|
|
|
void blk_free_ext_minor(unsigned int minor)
|
2008-08-25 10:47:22 +00:00
|
|
|
{
|
2021-05-21 05:50:51 +00:00
|
|
|
ida_free(&ext_devt_ida, blk_mangle_minor(minor));
|
2019-04-02 12:06:34 +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;
|
|
|
|
}
|
|
|
|
|
2021-01-24 10:02:39 +00:00
|
|
|
void disk_uevent(struct gendisk *disk, enum kobject_action action)
|
|
|
|
{
|
|
|
|
struct block_device *part;
|
2021-04-06 06:23:02 +00:00
|
|
|
unsigned long idx;
|
2021-01-24 10:02:39 +00:00
|
|
|
|
2021-04-06 06:23:02 +00:00
|
|
|
rcu_read_lock();
|
|
|
|
xa_for_each(&disk->part_tbl, idx, part) {
|
|
|
|
if (bdev_is_partition(part) && !bdev_nr_sectors(part))
|
|
|
|
continue;
|
|
|
|
if (!bdgrab(part))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
rcu_read_unlock();
|
2021-01-24 10:02:39 +00:00
|
|
|
kobject_uevent(bdev_kobj(part), action);
|
2021-04-06 06:23:02 +00:00
|
|
|
bdput(part);
|
|
|
|
rcu_read_lock();
|
|
|
|
}
|
|
|
|
rcu_read_unlock();
|
2021-01-24 10:02:39 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(disk_uevent);
|
|
|
|
|
2020-09-21 07:19:46 +00:00
|
|
|
static void disk_scan_partitions(struct gendisk *disk)
|
|
|
|
{
|
|
|
|
struct block_device *bdev;
|
|
|
|
|
|
|
|
if (!get_capacity(disk) || !disk_part_scan_enabled(disk))
|
|
|
|
return;
|
|
|
|
|
|
|
|
set_bit(GD_NEED_PART_SCAN, &disk->state);
|
|
|
|
bdev = blkdev_get_by_dev(disk_devt(disk), FMODE_READ, NULL);
|
|
|
|
if (!IS_ERR(bdev))
|
|
|
|
blkdev_put(bdev, FMODE_READ);
|
|
|
|
}
|
|
|
|
|
2018-09-28 06:17:19 +00:00
|
|
|
static void register_disk(struct device *parent, struct gendisk *disk,
|
|
|
|
const struct attribute_group **groups)
|
2010-12-08 19:57:36 +00:00
|
|
|
{
|
|
|
|
struct device *ddev = disk_to_dev(disk);
|
|
|
|
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);
|
|
|
|
|
2018-09-28 06:17:19 +00:00
|
|
|
if (groups) {
|
|
|
|
WARN_ON(ddev->groups);
|
|
|
|
ddev->groups = groups;
|
|
|
|
}
|
2010-12-08 19:57:36 +00:00
|
|
|
if (device_add(ddev))
|
|
|
|
return;
|
|
|
|
if (!sysfs_deprecated) {
|
|
|
|
err = sysfs_create_link(block_depr, &ddev->kobj,
|
|
|
|
kobject_name(&ddev->kobj));
|
|
|
|
if (err) {
|
|
|
|
device_del(ddev);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2013-02-23 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);
|
|
|
|
|
2020-11-26 17:47:17 +00:00
|
|
|
disk->part0->bd_holder_dir =
|
|
|
|
kobject_create_and_add("holders", &ddev->kobj);
|
2010-12-08 19:57:36 +00:00
|
|
|
disk->slave_dir = kobject_create_and_add("slaves", &ddev->kobj);
|
|
|
|
|
2021-03-11 15:19:17 +00:00
|
|
|
if (disk->flags & GENHD_FL_HIDDEN)
|
2017-11-02 18:29:53 +00:00
|
|
|
return;
|
|
|
|
|
2020-09-21 07:19:46 +00:00
|
|
|
disk_scan_partitions(disk);
|
2010-12-08 19:57:36 +00:00
|
|
|
|
2021-01-24 10:02:39 +00:00
|
|
|
/* announce the disk and partitions after all partitions are created */
|
2010-12-08 19:57:36 +00:00
|
|
|
dev_set_uevent_suppress(ddev, 0);
|
2021-01-24 10:02:39 +00:00
|
|
|
disk_uevent(disk, KOBJ_ADD);
|
2017-11-02 18:29:53 +00:00
|
|
|
|
2019-02-20 13:27:05 +00:00
|
|
|
if (disk->queue->backing_dev_info->dev) {
|
|
|
|
err = sysfs_create_link(&ddev->kobj,
|
|
|
|
&disk->queue->backing_dev_info->dev->kobj,
|
|
|
|
"bdi");
|
|
|
|
WARN_ON(err);
|
|
|
|
}
|
2010-12-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-09-28 06:17:19 +00:00
|
|
|
* @groups: Additional per-device sysfs groups
|
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,
|
2018-09-28 06:17:19 +00:00
|
|
|
const struct attribute_group **groups,
|
2018-01-09 03:01:13 +00:00
|
|
|
bool register_queue)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2021-05-21 05:50:51 +00:00
|
|
|
int ret;
|
2008-04-30 07:54:32 +00:00
|
|
|
|
2019-09-05 09:51:33 +00:00
|
|
|
/*
|
|
|
|
* The disk queue should now be all set with enough information about
|
|
|
|
* the device for the elevator code to pick an adequate default
|
|
|
|
* elevator if one is needed, that is, for devices requesting queue
|
|
|
|
* registration.
|
|
|
|
*/
|
|
|
|
if (register_queue)
|
|
|
|
elevator_init_mq(disk->queue);
|
|
|
|
|
2021-05-21 05:50:51 +00:00
|
|
|
/*
|
|
|
|
* If the driver provides an explicit major number it also must provide
|
|
|
|
* the number of minors numbers supported, and those will be used to
|
|
|
|
* setup the gendisk.
|
|
|
|
* Otherwise just allocate the device numbers for both the whole device
|
|
|
|
* and all partitions from the extended dev_t space.
|
2008-08-25 10:56:17 +00:00
|
|
|
*/
|
2021-05-21 05:50:51 +00:00
|
|
|
if (disk->major) {
|
|
|
|
WARN_ON(!disk->minors);
|
2021-05-21 05:50:52 +00:00
|
|
|
|
|
|
|
if (disk->minors > DISK_MAX_PARTS) {
|
|
|
|
pr_err("block: can't allocate more than %d partitions\n",
|
|
|
|
DISK_MAX_PARTS);
|
|
|
|
disk->minors = DISK_MAX_PARTS;
|
|
|
|
}
|
2021-05-21 05:50:51 +00:00
|
|
|
} else {
|
|
|
|
WARN_ON(disk->minors);
|
2008-08-25 10:56:17 +00:00
|
|
|
|
2021-05-21 05:50:51 +00:00
|
|
|
ret = blk_alloc_ext_minor();
|
|
|
|
if (ret < 0) {
|
|
|
|
WARN_ON(1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
disk->major = BLOCK_EXT_MAJOR;
|
|
|
|
disk->first_minor = MINOR(ret);
|
2021-05-21 05:50:53 +00:00
|
|
|
disk->flags |= GENHD_FL_EXT_DEVT;
|
2008-08-25 10:56:17 +00:00
|
|
|
}
|
2021-05-21 05:50:51 +00:00
|
|
|
|
|
|
|
disk->flags |= GENHD_FL_UP;
|
2008-08-25 10:56:17 +00:00
|
|
|
|
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 {
|
2020-05-04 12:47:59 +00:00
|
|
|
struct backing_dev_info *bdi = disk->queue->backing_dev_info;
|
|
|
|
struct device *dev = disk_to_dev(disk);
|
2017-10-31 10:38:59 +00:00
|
|
|
|
2017-11-02 18:29:53 +00:00
|
|
|
/* Register BDI before referencing it from bdev */
|
2021-05-21 05:50:51 +00:00
|
|
|
dev->devt = MKDEV(disk->major, disk->first_minor);
|
|
|
|
ret = bdi_register(bdi, "%u:%u",
|
|
|
|
disk->major, disk->first_minor);
|
2017-10-31 10:38:59 +00:00
|
|
|
WARN_ON(ret);
|
2020-05-04 12:47:59 +00:00
|
|
|
bdi_set_owner(bdi, dev);
|
2021-05-21 05:50:51 +00:00
|
|
|
bdev_add(disk->part0, dev->devt);
|
2017-11-02 18:29:53 +00:00
|
|
|
}
|
2018-09-28 06:17:19 +00:00
|
|
|
register_disk(parent, disk, groups);
|
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.
|
|
|
|
*/
|
2021-05-21 05:50:54 +00:00
|
|
|
if (blk_get_queue(disk->queue))
|
|
|
|
set_bit(GD_QUEUE_REF, &disk->state);
|
|
|
|
else
|
|
|
|
WARN_ON_ONCE(1);
|
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
|
|
|
|
2018-09-28 06:17:19 +00:00
|
|
|
void device_add_disk(struct device *parent, struct gendisk *disk,
|
|
|
|
const struct attribute_group **groups)
|
|
|
|
|
2018-01-09 03:01:13 +00:00
|
|
|
{
|
2018-09-28 06:17:19 +00:00
|
|
|
__device_add_disk(parent, disk, groups, true);
|
2018-01-09 03:01:13 +00:00
|
|
|
}
|
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)
|
|
|
|
{
|
2018-09-28 06:17:19 +00:00
|
|
|
__device_add_disk(parent, disk, NULL, false);
|
2018-01-09 03:01:13 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(device_add_disk_no_queue_reg);
|
|
|
|
|
2020-06-19 20:47:23 +00:00
|
|
|
/**
|
|
|
|
* del_gendisk - remove the gendisk
|
|
|
|
* @disk: the struct gendisk to remove
|
|
|
|
*
|
|
|
|
* Removes the gendisk and all its associated resources. This deletes the
|
|
|
|
* partitions associated with the gendisk, and unregisters the associated
|
|
|
|
* request_queue.
|
|
|
|
*
|
|
|
|
* This is the counter to the respective __device_add_disk() call.
|
|
|
|
*
|
|
|
|
* The final removal of the struct gendisk happens when its refcount reaches 0
|
|
|
|
* with put_disk(), which should be called after del_gendisk(), if
|
|
|
|
* __device_add_disk() was used.
|
2020-06-19 20:47:25 +00:00
|
|
|
*
|
|
|
|
* Drivers exist which depend on the release of the gendisk to be synchronous,
|
|
|
|
* it should not be deferred.
|
|
|
|
*
|
|
|
|
* Context: can sleep
|
2020-06-19 20:47:23 +00:00
|
|
|
*/
|
2010-12-08 19:57:36 +00:00
|
|
|
void del_gendisk(struct gendisk *disk)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2020-06-19 20:47:25 +00:00
|
|
|
might_sleep();
|
|
|
|
|
2020-10-29 14:58:24 +00:00
|
|
|
if (WARN_ON_ONCE(!disk->queue))
|
|
|
|
return;
|
|
|
|
|
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);
|
|
|
|
|
2021-04-06 06:22:56 +00:00
|
|
|
mutex_lock(&disk->part0->bd_mutex);
|
2021-05-14 13:18:41 +00:00
|
|
|
disk->flags &= ~GENHD_FL_UP;
|
2021-04-06 06:22:55 +00:00
|
|
|
blk_drop_partitions(disk);
|
2021-04-06 06:22:56 +00:00
|
|
|
mutex_unlock(&disk->part0->bd_mutex);
|
|
|
|
|
2021-04-06 06:22:53 +00:00
|
|
|
fsync_bdev(disk->part0);
|
|
|
|
__invalidate_device(disk->part0, true);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Unhash the bdev inode for this device so that it can't be looked
|
|
|
|
* up any more even if openers still hold references to it.
|
|
|
|
*/
|
|
|
|
remove_inode_hash(disk->part0->bd_inode);
|
|
|
|
|
2010-12-08 19:57:36 +00:00
|
|
|
set_capacity(disk, 0);
|
|
|
|
|
2020-10-29 14:58:24 +00:00
|
|
|
if (!(disk->flags & GENHD_FL_HIDDEN)) {
|
2017-11-02 18:29:53 +00:00
|
|
|
sysfs_remove_link(&disk_to_dev(disk)->kobj, "bdi");
|
2020-10-29 14:58:24 +00:00
|
|
|
|
2017-03-08 16:48:33 +00:00
|
|
|
/*
|
|
|
|
* Unregister bdi before releasing device numbers (as they can
|
|
|
|
* get reused and we'd get clashes in sysfs).
|
|
|
|
*/
|
2020-10-29 14:58:24 +00:00
|
|
|
bdi_unregister(disk->queue->backing_dev_info);
|
2017-03-08 16:48:33 +00:00
|
|
|
}
|
2010-12-08 19:57:36 +00:00
|
|
|
|
2020-10-29 14:58:24 +00:00
|
|
|
blk_unregister_queue(disk);
|
2010-12-08 19:57:36 +00:00
|
|
|
|
2020-11-26 17:47:17 +00:00
|
|
|
kobject_put(disk->part0->bd_holder_dir);
|
2010-12-08 19:57:36 +00:00
|
|
|
kobject_put(disk->slave_dir);
|
|
|
|
|
2020-11-24 08:36:54 +00:00
|
|
|
part_stat_set_all(disk->part0, 0);
|
2020-11-26 17:47:17 +00:00
|
|
|
disk->part0->bd_stamp = 0;
|
2010-12-08 19:57:36 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-11-26 08:23:26 +00:00
|
|
|
void blk_request_module(dev_t devt)
|
2020-10-29 14:58:27 +00:00
|
|
|
{
|
2020-10-29 14:58:28 +00:00
|
|
|
unsigned int major = MAJOR(devt);
|
|
|
|
struct blk_major_name **n;
|
|
|
|
|
|
|
|
mutex_lock(&major_names_lock);
|
|
|
|
for (n = &major_names[major_to_index(major)]; *n; n = &(*n)->next) {
|
|
|
|
if ((*n)->major == major && (*n)->probe) {
|
|
|
|
(*n)->probe(devt);
|
|
|
|
mutex_unlock(&major_names_lock);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mutex_unlock(&major_names_lock);
|
|
|
|
|
2020-10-29 14:58:27 +00:00
|
|
|
if (request_module("block-major-%d-%d", MAJOR(devt), MINOR(devt)) > 0)
|
|
|
|
/* Make old-style 2.4 aliases work */
|
|
|
|
request_module("block-major-%d", MAJOR(devt));
|
|
|
|
}
|
|
|
|
|
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 block_device *bdev = NULL;
|
2008-09-03 07:01:48 +00:00
|
|
|
|
2020-11-27 15:43:51 +00:00
|
|
|
rcu_read_lock();
|
2021-01-24 10:02:41 +00:00
|
|
|
bdev = xa_load(&disk->part_tbl, partno);
|
2020-11-27 15:43:51 +00:00
|
|
|
if (bdev && !bdgrab(bdev))
|
|
|
|
bdev = NULL;
|
|
|
|
rcu_read_unlock();
|
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
|
|
|
}
|
|
|
|
|
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);
|
2020-11-24 08:52:59 +00:00
|
|
|
struct block_device *part;
|
2008-08-25 10:47:23 +00:00
|
|
|
char name_buf[BDEVNAME_SIZE];
|
|
|
|
char devt_buf[BDEVT_SIZE];
|
2021-04-06 06:22:59 +00:00
|
|
|
unsigned long idx;
|
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;
|
|
|
|
|
|
|
|
/*
|
2021-04-06 06:22:59 +00:00
|
|
|
* Note, unlike /proc/partitions, I am showing the numbers in
|
|
|
|
* hex - the same format as the root= option takes.
|
2008-09-03 06:57:12 +00:00
|
|
|
*/
|
2021-04-06 06:22:59 +00:00
|
|
|
rcu_read_lock();
|
|
|
|
xa_for_each(&disk->part_tbl, idx, part) {
|
|
|
|
if (!bdev_nr_sectors(part))
|
|
|
|
continue;
|
|
|
|
printk("%s%s %10llu %s %s",
|
|
|
|
bdev_is_partition(part) ? " " : "",
|
2020-11-24 08:52:59 +00:00
|
|
|
bdevt_str(part->bd_dev, devt_buf),
|
|
|
|
bdev_nr_sectors(part) >> 1,
|
|
|
|
disk_name(disk, part->bd_partno, name_buf),
|
|
|
|
part->bd_meta_info ?
|
|
|
|
part->bd_meta_info->uuid : "");
|
2021-04-06 06:22:59 +00:00
|
|
|
if (bdev_is_partition(part))
|
2008-08-25 10:56:14 +00:00
|
|
|
printk("\n");
|
2021-04-06 06:22:59 +00:00
|
|
|
else if (dev->parent && dev->parent->driver)
|
|
|
|
printk(" driver: %s\n",
|
|
|
|
dev->parent->driver->name);
|
|
|
|
else
|
|
|
|
printk(" (driver?)\n");
|
2008-08-25 10:56:14 +00:00
|
|
|
}
|
2021-04-06 06:22:59 +00:00
|
|
|
rcu_read_unlock();
|
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;
|
2020-11-24 08:52:59 +00:00
|
|
|
struct block_device *part;
|
2021-04-06 06:23:00 +00:00
|
|
|
unsigned long idx;
|
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;
|
|
|
|
|
2021-04-06 06:23:00 +00:00
|
|
|
rcu_read_lock();
|
|
|
|
xa_for_each(&sgp->part_tbl, idx, part) {
|
|
|
|
if (!bdev_nr_sectors(part))
|
|
|
|
continue;
|
2008-08-25 10:47:23 +00:00
|
|
|
seq_printf(seqf, "%4d %7d %10llu %s\n",
|
2020-11-24 08:52:59 +00:00
|
|
|
MAJOR(part->bd_dev), MINOR(part->bd_dev),
|
|
|
|
bdev_nr_sectors(part) >> 1,
|
|
|
|
disk_name(sgp, part->bd_partno, buf));
|
2021-04-06 06:23:00 +00:00
|
|
|
}
|
|
|
|
rcu_read_unlock();
|
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
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
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;
|
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
|
|
|
}
|
|
|
|
|
2020-03-24 07:25:13 +00:00
|
|
|
ssize_t part_size_show(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *buf)
|
|
|
|
{
|
2020-11-27 15:43:51 +00:00
|
|
|
return sprintf(buf, "%llu\n", bdev_nr_sectors(dev_to_bdev(dev)));
|
2020-03-24 07:25:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t part_stat_show(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *buf)
|
|
|
|
{
|
2020-11-27 15:43:51 +00:00
|
|
|
struct block_device *bdev = dev_to_bdev(dev);
|
|
|
|
struct request_queue *q = bdev->bd_disk->queue;
|
2020-03-25 13:07:06 +00:00
|
|
|
struct disk_stats stat;
|
2020-03-24 07:25:13 +00:00
|
|
|
unsigned int inflight;
|
|
|
|
|
2020-11-27 15:43:51 +00:00
|
|
|
part_stat_read_all(bdev, &stat);
|
2020-05-13 10:49:33 +00:00
|
|
|
if (queue_is_mq(q))
|
2020-11-27 15:43:51 +00:00
|
|
|
inflight = blk_mq_in_flight(q, bdev);
|
2020-05-13 10:49:33 +00:00
|
|
|
else
|
2020-11-27 15:43:51 +00:00
|
|
|
inflight = part_in_flight(bdev);
|
2020-03-25 13:07:06 +00:00
|
|
|
|
2020-03-24 07:25:13 +00:00
|
|
|
return sprintf(buf,
|
|
|
|
"%8lu %8lu %8llu %8u "
|
|
|
|
"%8lu %8lu %8llu %8u "
|
|
|
|
"%8u %8u %8u "
|
|
|
|
"%8lu %8lu %8llu %8u "
|
|
|
|
"%8lu %8u"
|
|
|
|
"\n",
|
2020-03-25 13:07:06 +00:00
|
|
|
stat.ios[STAT_READ],
|
|
|
|
stat.merges[STAT_READ],
|
|
|
|
(unsigned long long)stat.sectors[STAT_READ],
|
|
|
|
(unsigned int)div_u64(stat.nsecs[STAT_READ], NSEC_PER_MSEC),
|
|
|
|
stat.ios[STAT_WRITE],
|
|
|
|
stat.merges[STAT_WRITE],
|
|
|
|
(unsigned long long)stat.sectors[STAT_WRITE],
|
|
|
|
(unsigned int)div_u64(stat.nsecs[STAT_WRITE], NSEC_PER_MSEC),
|
2020-03-24 07:25:13 +00:00
|
|
|
inflight,
|
2020-03-25 13:07:06 +00:00
|
|
|
jiffies_to_msecs(stat.io_ticks),
|
2020-03-25 13:07:08 +00:00
|
|
|
(unsigned int)div_u64(stat.nsecs[STAT_READ] +
|
|
|
|
stat.nsecs[STAT_WRITE] +
|
|
|
|
stat.nsecs[STAT_DISCARD] +
|
|
|
|
stat.nsecs[STAT_FLUSH],
|
|
|
|
NSEC_PER_MSEC),
|
2020-03-25 13:07:06 +00:00
|
|
|
stat.ios[STAT_DISCARD],
|
|
|
|
stat.merges[STAT_DISCARD],
|
|
|
|
(unsigned long long)stat.sectors[STAT_DISCARD],
|
|
|
|
(unsigned int)div_u64(stat.nsecs[STAT_DISCARD], NSEC_PER_MSEC),
|
|
|
|
stat.ios[STAT_FLUSH],
|
|
|
|
(unsigned int)div_u64(stat.nsecs[STAT_FLUSH], NSEC_PER_MSEC));
|
2020-03-24 07:25:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t part_inflight_show(struct device *dev, struct device_attribute *attr,
|
|
|
|
char *buf)
|
|
|
|
{
|
2020-11-27 15:43:51 +00:00
|
|
|
struct block_device *bdev = dev_to_bdev(dev);
|
|
|
|
struct request_queue *q = bdev->bd_disk->queue;
|
2020-03-24 07:25:13 +00:00
|
|
|
unsigned int inflight[2];
|
|
|
|
|
2020-05-13 10:49:33 +00:00
|
|
|
if (queue_is_mq(q))
|
2020-11-27 15:43:51 +00:00
|
|
|
blk_mq_in_flight_rw(q, bdev, inflight);
|
2020-05-13 10:49:33 +00:00
|
|
|
else
|
2020-11-27 15:43:51 +00:00
|
|
|
part_in_flight_rw(bdev, inflight);
|
2020-05-13 10:49:33 +00:00
|
|
|
|
2020-03-24 07:25:13 +00:00
|
|
|
return sprintf(buf, "%8u %8u\n", inflight[0], inflight[1]);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-05-24 19:38:59 +00:00
|
|
|
static DEVICE_ATTR(range, 0444, disk_range_show, NULL);
|
|
|
|
static DEVICE_ATTR(ext_range, 0444, disk_ext_range_show, NULL);
|
|
|
|
static DEVICE_ATTR(removable, 0444, disk_removable_show, NULL);
|
|
|
|
static DEVICE_ATTR(hidden, 0444, disk_hidden_show, NULL);
|
|
|
|
static DEVICE_ATTR(ro, 0444, disk_ro_show, NULL);
|
|
|
|
static DEVICE_ATTR(size, 0444, part_size_show, NULL);
|
|
|
|
static DEVICE_ATTR(alignment_offset, 0444, disk_alignment_offset_show, NULL);
|
|
|
|
static DEVICE_ATTR(discard_alignment, 0444, disk_discard_alignment_show, NULL);
|
|
|
|
static DEVICE_ATTR(capability, 0444, disk_capability_show, NULL);
|
|
|
|
static DEVICE_ATTR(stat, 0444, part_stat_show, NULL);
|
|
|
|
static DEVICE_ATTR(inflight, 0444, part_inflight_show, NULL);
|
|
|
|
static DEVICE_ATTR(badblocks, 0644, disk_badblocks_show, disk_badblocks_store);
|
2020-03-24 07:25:13 +00:00
|
|
|
|
2006-12-08 10:39:46 +00:00
|
|
|
#ifdef CONFIG_FAIL_MAKE_REQUEST
|
2020-03-24 07:25:13 +00:00
|
|
|
ssize_t part_fail_show(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *buf)
|
|
|
|
{
|
2020-11-27 15:43:51 +00:00
|
|
|
return sprintf(buf, "%d\n", dev_to_bdev(dev)->bd_make_it_fail);
|
2020-03-24 07:25:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t part_fail_store(struct device *dev,
|
|
|
|
struct device_attribute *attr,
|
|
|
|
const char *buf, size_t count)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (count > 0 && sscanf(buf, "%d", &i) > 0)
|
2020-11-27 15:43:51 +00:00
|
|
|
dev_to_bdev(dev)->bd_make_it_fail = i;
|
2020-03-24 07:25:13 +00:00
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2007-05-21 20:08:01 +00:00
|
|
|
static struct device_attribute dev_attr_fail =
|
2018-05-24 19:38:59 +00:00
|
|
|
__ATTR(make-it-fail, 0644, part_fail_show, part_fail_store);
|
2020-03-24 07:25:13 +00:00
|
|
|
#endif /* CONFIG_FAIL_MAKE_REQUEST */
|
|
|
|
|
2008-09-14 12:56:33 +00:00
|
|
|
#ifdef CONFIG_FAIL_IO_TIMEOUT
|
|
|
|
static struct device_attribute dev_attr_fail_timeout =
|
2018-05-24 19:38:59 +00:00
|
|
|
__ATTR(io-timeout-fail, 0644, part_timeout_show, part_timeout_store);
|
2008-09-14 12:56:33 +00:00
|
|
|
#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
|
|
|
};
|
|
|
|
|
2020-06-19 20:47:23 +00:00
|
|
|
/**
|
|
|
|
* disk_release - releases all allocated resources of the gendisk
|
|
|
|
* @dev: the device representing this disk
|
|
|
|
*
|
|
|
|
* This function releases all allocated resources of the gendisk.
|
|
|
|
*
|
|
|
|
* Drivers which used __device_add_disk() have a gendisk with a request_queue
|
|
|
|
* assigned. Since the request_queue sits on top of the gendisk for these
|
|
|
|
* drivers we also call blk_put_queue() for them, and we expect the
|
|
|
|
* request_queue refcount to reach 0 at this point, and so the request_queue
|
|
|
|
* will also be freed prior to the disk.
|
2020-06-19 20:47:25 +00:00
|
|
|
*
|
|
|
|
* Context: can sleep
|
2020-06-19 20:47:23 +00:00
|
|
|
*/
|
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);
|
|
|
|
|
2020-06-19 20:47:25 +00:00
|
|
|
might_sleep();
|
|
|
|
|
2021-05-21 05:50:51 +00:00
|
|
|
if (MAJOR(dev->devt) == BLOCK_EXT_MAJOR)
|
|
|
|
blk_free_ext_minor(MINOR(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);
|
2021-01-24 10:02:41 +00:00
|
|
|
xa_destroy(&disk->part_tbl);
|
2020-11-26 17:47:17 +00:00
|
|
|
bdput(disk->part0);
|
2021-05-21 05:50:54 +00:00
|
|
|
if (test_bit(GD_QUEUE_REF, &disk->state) && disk->queue)
|
2011-10-19 12:31:07 +00:00
|
|
|
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);
|
|
|
|
|
2020-03-27 08:07:17 +00:00
|
|
|
if (disk->fops->devnode)
|
|
|
|
return disk->fops->devnode(disk, mode);
|
2009-04-30 13:23:42 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-06-01 20:12:05 +00:00
|
|
|
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;
|
2020-11-24 08:52:59 +00:00
|
|
|
struct block_device *hd;
|
2005-04-16 22:20:36 +00:00
|
|
|
char buf[BDEVNAME_SIZE];
|
2018-12-06 16:41:21 +00:00
|
|
|
unsigned int inflight;
|
2020-03-25 13:07:06 +00:00
|
|
|
struct disk_stats stat;
|
2021-04-06 06:23:01 +00:00
|
|
|
unsigned long idx;
|
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
|
|
|
|
2021-04-06 06:23:01 +00:00
|
|
|
rcu_read_lock();
|
|
|
|
xa_for_each(&gp->part_tbl, idx, hd) {
|
|
|
|
if (bdev_is_partition(hd) && !bdev_nr_sectors(hd))
|
|
|
|
continue;
|
2020-11-27 15:43:51 +00:00
|
|
|
part_stat_read_all(hd, &stat);
|
2020-05-13 10:49:33 +00:00
|
|
|
if (queue_is_mq(gp->queue))
|
2020-11-24 08:52:59 +00:00
|
|
|
inflight = blk_mq_in_flight(gp->queue, hd);
|
2020-05-13 10:49:33 +00:00
|
|
|
else
|
2020-11-24 08:52:59 +00:00
|
|
|
inflight = part_in_flight(hd);
|
2020-03-25 13:07:06 +00:00
|
|
|
|
2018-07-18 11:47:40 +00:00
|
|
|
seq_printf(seqf, "%4d %7d %s "
|
|
|
|
"%lu %lu %lu %u "
|
|
|
|
"%lu %lu %lu %u "
|
|
|
|
"%u %u %u "
|
2019-11-21 10:40:26 +00:00
|
|
|
"%lu %lu %lu %u "
|
|
|
|
"%lu %u"
|
|
|
|
"\n",
|
2020-11-24 08:52:59 +00:00
|
|
|
MAJOR(hd->bd_dev), MINOR(hd->bd_dev),
|
|
|
|
disk_name(gp, hd->bd_partno, buf),
|
2020-03-25 13:07:06 +00:00
|
|
|
stat.ios[STAT_READ],
|
|
|
|
stat.merges[STAT_READ],
|
|
|
|
stat.sectors[STAT_READ],
|
|
|
|
(unsigned int)div_u64(stat.nsecs[STAT_READ],
|
|
|
|
NSEC_PER_MSEC),
|
|
|
|
stat.ios[STAT_WRITE],
|
|
|
|
stat.merges[STAT_WRITE],
|
|
|
|
stat.sectors[STAT_WRITE],
|
|
|
|
(unsigned int)div_u64(stat.nsecs[STAT_WRITE],
|
|
|
|
NSEC_PER_MSEC),
|
2018-12-06 16:41:21 +00:00
|
|
|
inflight,
|
2020-03-25 13:07:06 +00:00
|
|
|
jiffies_to_msecs(stat.io_ticks),
|
2020-03-25 13:07:08 +00:00
|
|
|
(unsigned int)div_u64(stat.nsecs[STAT_READ] +
|
|
|
|
stat.nsecs[STAT_WRITE] +
|
|
|
|
stat.nsecs[STAT_DISCARD] +
|
|
|
|
stat.nsecs[STAT_FLUSH],
|
|
|
|
NSEC_PER_MSEC),
|
2020-03-25 13:07:06 +00:00
|
|
|
stat.ios[STAT_DISCARD],
|
|
|
|
stat.merges[STAT_DISCARD],
|
|
|
|
stat.sectors[STAT_DISCARD],
|
|
|
|
(unsigned int)div_u64(stat.nsecs[STAT_DISCARD],
|
|
|
|
NSEC_PER_MSEC),
|
|
|
|
stat.ios[STAT_FLUSH],
|
|
|
|
(unsigned int)div_u64(stat.nsecs[STAT_FLUSH],
|
|
|
|
NSEC_PER_MSEC)
|
2008-02-08 10:04:56 +00:00
|
|
|
);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2021-04-06 06:23:01 +00:00
|
|
|
rcu_read_unlock();
|
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
|
|
|
|
|
|
|
static int __init proc_genhd_init(void)
|
|
|
|
{
|
2018-04-13 17:44:18 +00:00
|
|
|
proc_create_seq("diskstats", 0, NULL, &diskstats_op);
|
|
|
|
proc_create_seq("partitions", 0, NULL, &partitions_op);
|
2008-10-04 19:53:21 +00:00
|
|
|
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);
|
2020-11-27 15:43:51 +00:00
|
|
|
struct block_device *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;
|
|
|
|
}
|
2020-11-27 15:43:51 +00:00
|
|
|
part = bdget_disk(disk, partno);
|
2008-08-29 09:41:51 +00:00
|
|
|
if (part) {
|
2020-11-27 15:43:51 +00:00
|
|
|
devt = part->bd_dev;
|
|
|
|
bdput(part);
|
2008-08-29 07:01:47 +00:00
|
|
|
break;
|
2008-09-03 06:57:12 +00:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2013-08-29 22:21:42 +00:00
|
|
|
disk = kzalloc_node(sizeof(struct gendisk), GFP_KERNEL, node_id);
|
2020-08-31 18:02:37 +00:00
|
|
|
if (!disk)
|
|
|
|
return NULL;
|
2011-01-07 07:43:37 +00:00
|
|
|
|
2020-11-26 17:47:17 +00:00
|
|
|
disk->part0 = bdev_alloc(disk, 0);
|
|
|
|
if (!disk->part0)
|
2020-11-26 08:23:26 +00:00
|
|
|
goto out_free_disk;
|
|
|
|
|
2020-08-31 18:02:37 +00:00
|
|
|
disk->node_id = node_id;
|
2021-01-24 10:02:41 +00:00
|
|
|
xa_init(&disk->part_tbl);
|
|
|
|
if (xa_insert(&disk->part_tbl, 0, disk->part0, GFP_KERNEL))
|
|
|
|
goto out_destroy_part_tbl;
|
2020-08-31 18:02:37 +00:00
|
|
|
|
|
|
|
disk->minors = minors;
|
|
|
|
rand_initialize_disk(disk);
|
|
|
|
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;
|
2020-08-31 18:02:37 +00:00
|
|
|
|
2021-01-24 10:02:41 +00:00
|
|
|
out_destroy_part_tbl:
|
|
|
|
xa_destroy(&disk->part_tbl);
|
2020-11-26 17:47:17 +00:00
|
|
|
bdput(disk->part0);
|
2020-08-31 18:02:37 +00:00
|
|
|
out_free_disk:
|
|
|
|
kfree(disk);
|
|
|
|
return NULL;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2017-10-25 08:56:05 +00:00
|
|
|
EXPORT_SYMBOL(__alloc_disk_node);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2021-05-21 05:50:55 +00:00
|
|
|
struct gendisk *__blk_alloc_disk(int node)
|
|
|
|
{
|
|
|
|
struct request_queue *q;
|
|
|
|
struct gendisk *disk;
|
|
|
|
|
|
|
|
q = blk_alloc_queue(node);
|
|
|
|
if (!q)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
disk = __alloc_disk_node(0, node);
|
|
|
|
if (!disk) {
|
|
|
|
blk_cleanup_queue(q);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
disk->queue = q;
|
|
|
|
return disk;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(__blk_alloc_disk);
|
|
|
|
|
2020-06-19 20:47:23 +00:00
|
|
|
/**
|
|
|
|
* put_disk - decrements the gendisk refcount
|
2020-07-31 01:42:30 +00:00
|
|
|
* @disk: the struct gendisk to decrement the refcount for
|
2020-06-19 20:47:23 +00:00
|
|
|
*
|
|
|
|
* This decrements the refcount for the struct gendisk. When this reaches 0
|
|
|
|
* we'll have disk_release() called.
|
2020-06-19 20:47:25 +00:00
|
|
|
*
|
|
|
|
* Context: Any context, but the last reference must not be dropped from
|
|
|
|
* atomic context.
|
2020-06-19 20:47:23 +00:00
|
|
|
*/
|
2005-04-16 22:20:36 +00:00
|
|
|
void put_disk(struct gendisk *disk)
|
|
|
|
{
|
|
|
|
if (disk)
|
2020-11-10 06:25:37 +00:00
|
|
|
put_device(disk_to_dev(disk));
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(put_disk);
|
|
|
|
|
2021-05-21 05:50:55 +00:00
|
|
|
/**
|
|
|
|
* blk_cleanup_disk - shutdown a gendisk allocated by blk_alloc_disk
|
|
|
|
* @disk: gendisk to shutdown
|
|
|
|
*
|
|
|
|
* Mark the queue hanging off @disk DYING, drain all pending requests, then mark
|
|
|
|
* the queue DEAD, destroy and put it and the gendisk structure.
|
|
|
|
*
|
|
|
|
* Context: can sleep
|
|
|
|
*/
|
|
|
|
void blk_cleanup_disk(struct gendisk *disk)
|
|
|
|
{
|
|
|
|
blk_cleanup_queue(disk->queue);
|
|
|
|
put_disk(disk);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(blk_cleanup_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);
|
|
|
|
}
|
|
|
|
|
2021-01-09 10:42:51 +00:00
|
|
|
/**
|
|
|
|
* set_disk_ro - set a gendisk read-only
|
|
|
|
* @disk: gendisk to operate on
|
2021-01-29 04:55:05 +00:00
|
|
|
* @read_only: %true to set the disk read-only, %false set the disk read/write
|
2021-01-09 10:42:51 +00:00
|
|
|
*
|
|
|
|
* This function is used to indicate whether a given disk device should have its
|
|
|
|
* read-only flag set. set_disk_ro() is typically used by device drivers to
|
|
|
|
* indicate whether the underlying physical device is write-protected.
|
|
|
|
*/
|
|
|
|
void set_disk_ro(struct gendisk *disk, bool read_only)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2021-01-09 10:42:51 +00:00
|
|
|
if (read_only) {
|
|
|
|
if (test_and_set_bit(GD_READ_ONLY, &disk->state))
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
if (!test_and_clear_bit(GD_READ_ONLY, &disk->state))
|
|
|
|
return;
|
2009-07-28 07:13:13 +00:00
|
|
|
}
|
2021-01-09 10:42:51 +00:00
|
|
|
set_disk_ro_uevent(disk, read_only);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(set_disk_ro);
|
|
|
|
|
|
|
|
int bdev_read_only(struct block_device *bdev)
|
|
|
|
{
|
2021-01-09 10:42:52 +00:00
|
|
|
return bdev->bd_read_only || get_disk_ro(bdev->bd_disk);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(bdev_read_only);
|
|
|
|
|
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
|
2019-03-27 13:51:01 +00:00
|
|
|
* the default is being used, poll if the POLL flag is set.
|
implement in-kernel gendisk events handling
Currently, media presence polling for removeable block devices is done
from userland. There are several issues with this.
* Polling is done by periodically opening the device. For SCSI
devices, the command sequence generated by such action involves a
few different commands including TEST_UNIT_READY. This behavior,
while perfectly legal, is different from Windows which only issues
single command, GET_EVENT_STATUS_NOTIFICATION. Unfortunately, some
ATAPI devices lock up after being periodically queried such command
sequences.
* There is no reliable and unintrusive way for a userland program to
tell whether the target device is safe for media presence polling.
For example, polling for media presence during an on-going burning
session can make it fail. The polling program can avoid this by
opening the device with O_EXCL but then it risks making a valid
exclusive user of the device fail w/ -EBUSY.
* Userland polling is unnecessarily heavy and in-kernel implementation
is lighter and better coordinated (workqueue, timer slack).
This patch implements framework for in-kernel disk event handling,
which includes media presence polling.
* bdops->check_events() is added, which supercedes ->media_changed().
It should check whether there's any pending event and return if so.
Currently, two events are defined - DISK_EVENT_MEDIA_CHANGE and
DISK_EVENT_EJECT_REQUEST. ->check_events() is guaranteed not to be
called parallelly.
* gendisk->events and ->async_events are added. These should be
initialized by block driver before passing the device to add_disk().
The former contains the mask of all supported events and the latter
the mask of all events which the device can report without polling.
/sys/block/*/events[_async] export these to userland.
* Kernel parameter block.events_dfl_poll_msecs controls the system
polling interval (default is 0 which means disable) and
/sys/block/*/events_poll_msecs control polling intervals for
individual devices (default is -1 meaning use system setting). Note
that if a device can report all supported events asynchronously and
its polling interval isn't explicitly set, the device won't be
polled regardless of the system polling interval.
* If a device is opened exclusively with write access, event checking
is automatically disabled until all write exclusive accesses are
released.
* There are event 'clearing' events. For example, both of currently
defined events are cleared after the device has been successfully
opened. This information is passed to ->check_events() callback
using @clearing argument as a hint.
* Event checking is always performed from system_nrt_wq and timer
slack is set to 25% for polling.
* Nothing changes for drivers which implement ->media_changed() but
not ->check_events(). Going forward, all drivers will be converted
to ->check_events() and ->media_change() will be dropped.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
2010-12-08 19:57:37 +00:00
|
|
|
*/
|
|
|
|
if (ev->poll_msecs >= 0)
|
|
|
|
intv_msecs = ev->poll_msecs;
|
2019-03-27 13:51:02 +00:00
|
|
|
else if (disk->event_flags & DISK_EVENT_FLAG_POLL)
|
implement in-kernel gendisk events handling
Currently, media presence polling for removeable block devices is done
from userland. There are several issues with this.
* Polling is done by periodically opening the device. For SCSI
devices, the command sequence generated by such action involves a
few different commands including TEST_UNIT_READY. This behavior,
while perfectly legal, is different from Windows which only issues
single command, GET_EVENT_STATUS_NOTIFICATION. Unfortunately, some
ATAPI devices lock up after being periodically queried such command
sequences.
* There is no reliable and unintrusive way for a userland program to
tell whether the target device is safe for media presence polling.
For example, polling for media presence during an on-going burning
session can make it fail. The polling program can avoid this by
opening the device with O_EXCL but then it risks making a valid
exclusive user of the device fail w/ -EBUSY.
* Userland polling is unnecessarily heavy and in-kernel implementation
is lighter and better coordinated (workqueue, timer slack).
This patch implements framework for in-kernel disk event handling,
which includes media presence polling.
* bdops->check_events() is added, which supercedes ->media_changed().
It should check whether there's any pending event and return if so.
Currently, two events are defined - DISK_EVENT_MEDIA_CHANGE and
DISK_EVENT_EJECT_REQUEST. ->check_events() is guaranteed not to be
called parallelly.
* gendisk->events and ->async_events are added. These should be
initialized by block driver before passing the device to add_disk().
The former contains the mask of all supported events and the latter
the mask of all events which the device can report without polling.
/sys/block/*/events[_async] export these to userland.
* Kernel parameter block.events_dfl_poll_msecs controls the system
polling interval (default is 0 which means disable) and
/sys/block/*/events_poll_msecs control polling intervals for
individual devices (default is -1 meaning use system setting). Note
that if a device can report all supported events asynchronously and
its polling interval isn't explicitly set, the device won't be
polled regardless of the system polling interval.
* If a device is opened exclusively with write access, event checking
is automatically disabled until all write exclusive accesses are
released.
* There are event 'clearing' events. For example, both of currently
defined events are cleared after the device has been successfully
opened. This information is passed to ->check_events() callback
using @clearing argument as a hint.
* Event checking is always performed from system_nrt_wq and timer
slack is set to 25% for polling.
* Nothing changes for drivers which implement ->media_changed() but
not ->check_events(). Going forward, all drivers will be converted
to ->check_events() and ->media_change() will be dropped.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
2010-12-08 19:57:37 +00:00
|
|
|
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.
|
|
|
|
*/
|
2020-09-08 14:53:29 +00:00
|
|
|
static unsigned int disk_clear_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
|
|
|
{
|
|
|
|
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
|
|
|
|
2020-07-08 12:25:41 +00:00
|
|
|
if (!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
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-09-08 14:53:29 +00:00
|
|
|
/**
|
|
|
|
* bdev_check_media_change - check if a removable media has been changed
|
|
|
|
* @bdev: block device to check
|
|
|
|
*
|
|
|
|
* Check whether a removable media has been changed, and attempt to free all
|
|
|
|
* dentries and inodes and invalidates all block device page cache entries in
|
|
|
|
* that case.
|
|
|
|
*
|
|
|
|
* Returns %true if the block device changed, or %false if not.
|
|
|
|
*/
|
|
|
|
bool bdev_check_media_change(struct block_device *bdev)
|
|
|
|
{
|
|
|
|
unsigned int events;
|
|
|
|
|
|
|
|
events = disk_clear_events(bdev->bd_disk, DISK_EVENT_MEDIA_CHANGE |
|
|
|
|
DISK_EVENT_EJECT_REQUEST);
|
|
|
|
if (!(events & DISK_EVENT_MEDIA_CHANGE))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (__invalidate_device(bdev, true))
|
|
|
|
pr_warn("VFS: busy inodes on changed media %s\n",
|
|
|
|
bdev->bd_disk->disk_name);
|
2020-09-21 07:19:45 +00:00
|
|
|
set_bit(GD_NEED_PART_SCAN, &bdev->bd_disk->state);
|
2020-09-08 14:53:29 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(bdev_check_media_change);
|
|
|
|
|
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
|
2019-03-27 13:51:02 +00:00
|
|
|
* @disk->events are reported, and only if DISK_EVENT_FLAG_UEVENT
|
|
|
|
* is set. Otherwise, events are processed internally but never
|
|
|
|
* get reported to userland.
|
2011-04-21 17:43:58 +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
|
|
|
for (i = 0; i < ARRAY_SIZE(disk_uevents); i++)
|
2019-03-27 13:51:02 +00:00
|
|
|
if ((events & disk->events & (1 << i)) &&
|
|
|
|
(disk->event_flags & DISK_EVENT_FLAG_UEVENT))
|
implement in-kernel gendisk events handling
Currently, media presence polling for removeable block devices is done
from userland. There are several issues with this.
* Polling is done by periodically opening the device. For SCSI
devices, the command sequence generated by such action involves a
few different commands including TEST_UNIT_READY. This behavior,
while perfectly legal, is different from Windows which only issues
single command, GET_EVENT_STATUS_NOTIFICATION. Unfortunately, some
ATAPI devices lock up after being periodically queried such command
sequences.
* There is no reliable and unintrusive way for a userland program to
tell whether the target device is safe for media presence polling.
For example, polling for media presence during an on-going burning
session can make it fail. The polling program can avoid this by
opening the device with O_EXCL but then it risks making a valid
exclusive user of the device fail w/ -EBUSY.
* Userland polling is unnecessarily heavy and in-kernel implementation
is lighter and better coordinated (workqueue, timer slack).
This patch implements framework for in-kernel disk event handling,
which includes media presence polling.
* bdops->check_events() is added, which supercedes ->media_changed().
It should check whether there's any pending event and return if so.
Currently, two events are defined - DISK_EVENT_MEDIA_CHANGE and
DISK_EVENT_EJECT_REQUEST. ->check_events() is guaranteed not to be
called parallelly.
* gendisk->events and ->async_events are added. These should be
initialized by block driver before passing the device to add_disk().
The former contains the mask of all supported events and the latter
the mask of all events which the device can report without polling.
/sys/block/*/events[_async] export these to userland.
* Kernel parameter block.events_dfl_poll_msecs controls the system
polling interval (default is 0 which means disable) and
/sys/block/*/events_poll_msecs control polling intervals for
individual devices (default is -1 meaning use system setting). Note
that if a device can report all supported events asynchronously and
its polling interval isn't explicitly set, the device won't be
polled regardless of the system polling interval.
* If a device is opened exclusively with write access, event checking
is automatically disabled until all write exclusive accesses are
released.
* There are event 'clearing' events. For example, both of currently
defined events are cleared after the device has been successfully
opened. This information is passed to ->check_events() callback
using @clearing argument as a hint.
* Event checking is always performed from system_nrt_wq and timer
slack is set to 25% for polling.
* Nothing changes for drivers which implement ->media_changed() but
not ->check_events(). Going forward, all drivers will be converted
to ->check_events() and ->media_change() will be dropped.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
2010-12-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
|
2019-03-27 13:51:01 +00:00
|
|
|
* (always empty, only for backwards compatibility)
|
implement in-kernel gendisk events handling
Currently, media presence polling for removeable block devices is done
from userland. There are several issues with this.
* Polling is done by periodically opening the device. For SCSI
devices, the command sequence generated by such action involves a
few different commands including TEST_UNIT_READY. This behavior,
while perfectly legal, is different from Windows which only issues
single command, GET_EVENT_STATUS_NOTIFICATION. Unfortunately, some
ATAPI devices lock up after being periodically queried such command
sequences.
* There is no reliable and unintrusive way for a userland program to
tell whether the target device is safe for media presence polling.
For example, polling for media presence during an on-going burning
session can make it fail. The polling program can avoid this by
opening the device with O_EXCL but then it risks making a valid
exclusive user of the device fail w/ -EBUSY.
* Userland polling is unnecessarily heavy and in-kernel implementation
is lighter and better coordinated (workqueue, timer slack).
This patch implements framework for in-kernel disk event handling,
which includes media presence polling.
* bdops->check_events() is added, which supercedes ->media_changed().
It should check whether there's any pending event and return if so.
Currently, two events are defined - DISK_EVENT_MEDIA_CHANGE and
DISK_EVENT_EJECT_REQUEST. ->check_events() is guaranteed not to be
called parallelly.
* gendisk->events and ->async_events are added. These should be
initialized by block driver before passing the device to add_disk().
The former contains the mask of all supported events and the latter
the mask of all events which the device can report without polling.
/sys/block/*/events[_async] export these to userland.
* Kernel parameter block.events_dfl_poll_msecs controls the system
polling interval (default is 0 which means disable) and
/sys/block/*/events_poll_msecs control polling intervals for
individual devices (default is -1 meaning use system setting). Note
that if a device can report all supported events asynchronously and
its polling interval isn't explicitly set, the device won't be
polled regardless of the system polling interval.
* If a device is opened exclusively with write access, event checking
is automatically disabled until all write exclusive accesses are
released.
* There are event 'clearing' events. For example, both of currently
defined events are cleared after the device has been successfully
opened. This information is passed to ->check_events() callback
using @clearing argument as a hint.
* Event checking is always performed from system_nrt_wq and timer
slack is set to 25% for polling.
* Nothing changes for drivers which implement ->media_changed() but
not ->check_events(). Going forward, all drivers will be converted
to ->check_events() and ->media_change() will be dropped.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
2010-12-08 19:57:37 +00:00
|
|
|
* events_poll_msecs : polling interval, 0: disable, -1: system default
|
|
|
|
*/
|
|
|
|
static ssize_t __disk_events_show(unsigned int events, char *buf)
|
|
|
|
{
|
|
|
|
const char *delim = "";
|
|
|
|
ssize_t pos = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(disk_events_strs); i++)
|
|
|
|
if (events & (1 << i)) {
|
|
|
|
pos += sprintf(buf + pos, "%s%s",
|
|
|
|
delim, disk_events_strs[i]);
|
|
|
|
delim = " ";
|
|
|
|
}
|
|
|
|
if (pos)
|
|
|
|
pos += sprintf(buf + pos, "\n");
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t disk_events_show(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *buf)
|
|
|
|
{
|
|
|
|
struct gendisk *disk = dev_to_disk(dev);
|
|
|
|
|
2019-03-27 13:51:02 +00:00
|
|
|
if (!(disk->event_flags & DISK_EVENT_FLAG_UEVENT))
|
|
|
|
return 0;
|
|
|
|
|
implement in-kernel gendisk events handling
Currently, media presence polling for removeable block devices is done
from userland. There are several issues with this.
* Polling is done by periodically opening the device. For SCSI
devices, the command sequence generated by such action involves a
few different commands including TEST_UNIT_READY. This behavior,
while perfectly legal, is different from Windows which only issues
single command, GET_EVENT_STATUS_NOTIFICATION. Unfortunately, some
ATAPI devices lock up after being periodically queried such command
sequences.
* There is no reliable and unintrusive way for a userland program to
tell whether the target device is safe for media presence polling.
For example, polling for media presence during an on-going burning
session can make it fail. The polling program can avoid this by
opening the device with O_EXCL but then it risks making a valid
exclusive user of the device fail w/ -EBUSY.
* Userland polling is unnecessarily heavy and in-kernel implementation
is lighter and better coordinated (workqueue, timer slack).
This patch implements framework for in-kernel disk event handling,
which includes media presence polling.
* bdops->check_events() is added, which supercedes ->media_changed().
It should check whether there's any pending event and return if so.
Currently, two events are defined - DISK_EVENT_MEDIA_CHANGE and
DISK_EVENT_EJECT_REQUEST. ->check_events() is guaranteed not to be
called parallelly.
* gendisk->events and ->async_events are added. These should be
initialized by block driver before passing the device to add_disk().
The former contains the mask of all supported events and the latter
the mask of all events which the device can report without polling.
/sys/block/*/events[_async] export these to userland.
* Kernel parameter block.events_dfl_poll_msecs controls the system
polling interval (default is 0 which means disable) and
/sys/block/*/events_poll_msecs control polling intervals for
individual devices (default is -1 meaning use system setting). Note
that if a device can report all supported events asynchronously and
its polling interval isn't explicitly set, the device won't be
polled regardless of the system polling interval.
* If a device is opened exclusively with write access, event checking
is automatically disabled until all write exclusive accesses are
released.
* There are event 'clearing' events. For example, both of currently
defined events are cleared after the device has been successfully
opened. This information is passed to ->check_events() callback
using @clearing argument as a hint.
* Event checking is always performed from system_nrt_wq and timer
slack is set to 25% for polling.
* Nothing changes for drivers which implement ->media_changed() but
not ->check_events(). Going forward, all drivers will be converted
to ->check_events() and ->media_change() will be dropped.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
2010-12-08 19:57:37 +00:00
|
|
|
return __disk_events_show(disk->events, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t disk_events_async_show(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *buf)
|
|
|
|
{
|
2019-03-27 13:51:01 +00:00
|
|
|
return 0;
|
implement in-kernel gendisk events handling
Currently, media presence polling for removeable block devices is done
from userland. There are several issues with this.
* Polling is done by periodically opening the device. For SCSI
devices, the command sequence generated by such action involves a
few different commands including TEST_UNIT_READY. This behavior,
while perfectly legal, is different from Windows which only issues
single command, GET_EVENT_STATUS_NOTIFICATION. Unfortunately, some
ATAPI devices lock up after being periodically queried such command
sequences.
* There is no reliable and unintrusive way for a userland program to
tell whether the target device is safe for media presence polling.
For example, polling for media presence during an on-going burning
session can make it fail. The polling program can avoid this by
opening the device with O_EXCL but then it risks making a valid
exclusive user of the device fail w/ -EBUSY.
* Userland polling is unnecessarily heavy and in-kernel implementation
is lighter and better coordinated (workqueue, timer slack).
This patch implements framework for in-kernel disk event handling,
which includes media presence polling.
* bdops->check_events() is added, which supercedes ->media_changed().
It should check whether there's any pending event and return if so.
Currently, two events are defined - DISK_EVENT_MEDIA_CHANGE and
DISK_EVENT_EJECT_REQUEST. ->check_events() is guaranteed not to be
called parallelly.
* gendisk->events and ->async_events are added. These should be
initialized by block driver before passing the device to add_disk().
The former contains the mask of all supported events and the latter
the mask of all events which the device can report without polling.
/sys/block/*/events[_async] export these to userland.
* Kernel parameter block.events_dfl_poll_msecs controls the system
polling interval (default is 0 which means disable) and
/sys/block/*/events_poll_msecs control polling intervals for
individual devices (default is -1 meaning use system setting). Note
that if a device can report all supported events asynchronously and
its polling interval isn't explicitly set, the device won't be
polled regardless of the system polling interval.
* If a device is opened exclusively with write access, event checking
is automatically disabled until all write exclusive accesses are
released.
* There are event 'clearing' events. For example, both of currently
defined events are cleared after the device has been successfully
opened. This information is passed to ->check_events() callback
using @clearing argument as a hint.
* Event checking is always performed from system_nrt_wq and timer
slack is set to 25% for polling.
* Nothing changes for drivers which implement ->media_changed() but
not ->check_events(). Going forward, all drivers will be converted
to ->check_events() and ->media_change() will be dropped.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
2010-12-08 19:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t disk_events_poll_msecs_show(struct device *dev,
|
|
|
|
struct device_attribute *attr,
|
|
|
|
char *buf)
|
|
|
|
{
|
|
|
|
struct gendisk *disk = dev_to_disk(dev);
|
|
|
|
|
2019-03-27 13:51:05 +00:00
|
|
|
if (!disk->ev)
|
|
|
|
return sprintf(buf, "-1\n");
|
|
|
|
|
implement in-kernel gendisk events handling
Currently, media presence polling for removeable block devices is done
from userland. There are several issues with this.
* Polling is done by periodically opening the device. For SCSI
devices, the command sequence generated by such action involves a
few different commands including TEST_UNIT_READY. This behavior,
while perfectly legal, is different from Windows which only issues
single command, GET_EVENT_STATUS_NOTIFICATION. Unfortunately, some
ATAPI devices lock up after being periodically queried such command
sequences.
* There is no reliable and unintrusive way for a userland program to
tell whether the target device is safe for media presence polling.
For example, polling for media presence during an on-going burning
session can make it fail. The polling program can avoid this by
opening the device with O_EXCL but then it risks making a valid
exclusive user of the device fail w/ -EBUSY.
* Userland polling is unnecessarily heavy and in-kernel implementation
is lighter and better coordinated (workqueue, timer slack).
This patch implements framework for in-kernel disk event handling,
which includes media presence polling.
* bdops->check_events() is added, which supercedes ->media_changed().
It should check whether there's any pending event and return if so.
Currently, two events are defined - DISK_EVENT_MEDIA_CHANGE and
DISK_EVENT_EJECT_REQUEST. ->check_events() is guaranteed not to be
called parallelly.
* gendisk->events and ->async_events are added. These should be
initialized by block driver before passing the device to add_disk().
The former contains the mask of all supported events and the latter
the mask of all events which the device can report without polling.
/sys/block/*/events[_async] export these to userland.
* Kernel parameter block.events_dfl_poll_msecs controls the system
polling interval (default is 0 which means disable) and
/sys/block/*/events_poll_msecs control polling intervals for
individual devices (default is -1 meaning use system setting). Note
that if a device can report all supported events asynchronously and
its polling interval isn't explicitly set, the device won't be
polled regardless of the system polling interval.
* If a device is opened exclusively with write access, event checking
is automatically disabled until all write exclusive accesses are
released.
* There are event 'clearing' events. For example, both of currently
defined events are cleared after the device has been successfully
opened. This information is passed to ->check_events() callback
using @clearing argument as a hint.
* Event checking is always performed from system_nrt_wq and timer
slack is set to 25% for polling.
* Nothing changes for drivers which implement ->media_changed() but
not ->check_events(). Going forward, all drivers will be converted
to ->check_events() and ->media_change() will be dropped.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
2010-12-08 19:57:37 +00:00
|
|
|
return sprintf(buf, "%ld\n", disk->ev->poll_msecs);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t disk_events_poll_msecs_store(struct device *dev,
|
|
|
|
struct device_attribute *attr,
|
|
|
|
const char *buf, size_t count)
|
|
|
|
{
|
|
|
|
struct gendisk *disk = dev_to_disk(dev);
|
|
|
|
long intv;
|
|
|
|
|
|
|
|
if (!count || !sscanf(buf, "%ld", &intv))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (intv < 0 && intv != -1)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2019-03-27 13:51:05 +00:00
|
|
|
if (!disk->ev)
|
|
|
|
return -ENODEV;
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-05-24 19:38:59 +00:00
|
|
|
static const DEVICE_ATTR(events, 0444, disk_events_show, NULL);
|
|
|
|
static const DEVICE_ATTR(events_async, 0444, disk_events_async_show, NULL);
|
|
|
|
static const DEVICE_ATTR(events_poll_msecs, 0644,
|
implement in-kernel gendisk events handling
Currently, media presence polling for removeable block devices is done
from userland. There are several issues with this.
* Polling is done by periodically opening the device. For SCSI
devices, the command sequence generated by such action involves a
few different commands including TEST_UNIT_READY. This behavior,
while perfectly legal, is different from Windows which only issues
single command, GET_EVENT_STATUS_NOTIFICATION. Unfortunately, some
ATAPI devices lock up after being periodically queried such command
sequences.
* There is no reliable and unintrusive way for a userland program to
tell whether the target device is safe for media presence polling.
For example, polling for media presence during an on-going burning
session can make it fail. The polling program can avoid this by
opening the device with O_EXCL but then it risks making a valid
exclusive user of the device fail w/ -EBUSY.
* Userland polling is unnecessarily heavy and in-kernel implementation
is lighter and better coordinated (workqueue, timer slack).
This patch implements framework for in-kernel disk event handling,
which includes media presence polling.
* bdops->check_events() is added, which supercedes ->media_changed().
It should check whether there's any pending event and return if so.
Currently, two events are defined - DISK_EVENT_MEDIA_CHANGE and
DISK_EVENT_EJECT_REQUEST. ->check_events() is guaranteed not to be
called parallelly.
* gendisk->events and ->async_events are added. These should be
initialized by block driver before passing the device to add_disk().
The former contains the mask of all supported events and the latter
the mask of all events which the device can report without polling.
/sys/block/*/events[_async] export these to userland.
* Kernel parameter block.events_dfl_poll_msecs controls the system
polling interval (default is 0 which means disable) and
/sys/block/*/events_poll_msecs control polling intervals for
individual devices (default is -1 meaning use system setting). Note
that if a device can report all supported events asynchronously and
its polling interval isn't explicitly set, the device won't be
polled regardless of the system polling interval.
* If a device is opened exclusively with write access, event checking
is automatically disabled until all write exclusive accesses are
released.
* There are event 'clearing' events. For example, both of currently
defined events are cleared after the device has been successfully
opened. This information is passed to ->check_events() callback
using @clearing argument as a hint.
* Event checking is always performed from system_nrt_wq and timer
slack is set to 25% for polling.
* Nothing changes for drivers which implement ->media_changed() but
not ->check_events(). Going forward, all drivers will be converted
to ->check_events() and ->media_change() will be dropped.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
2010-12-08 19:57:37 +00:00
|
|
|
disk_events_poll_msecs_show,
|
|
|
|
disk_events_poll_msecs_store);
|
|
|
|
|
|
|
|
static const struct attribute *disk_events_attrs[] = {
|
|
|
|
&dev_attr_events.attr,
|
|
|
|
&dev_attr_events_async.attr,
|
|
|
|
&dev_attr_events_poll_msecs.attr,
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The default polling interval can be specified by the kernel
|
|
|
|
* parameter block.events_dfl_poll_msecs which defaults to 0
|
|
|
|
* (disable). This can also be modified runtime by writing to
|
2019-07-16 12:59:35 +00:00
|
|
|
* /sys/module/block/parameters/events_dfl_poll_msecs.
|
implement in-kernel gendisk events handling
Currently, media presence polling for removeable block devices is done
from userland. There are several issues with this.
* Polling is done by periodically opening the device. For SCSI
devices, the command sequence generated by such action involves a
few different commands including TEST_UNIT_READY. This behavior,
while perfectly legal, is different from Windows which only issues
single command, GET_EVENT_STATUS_NOTIFICATION. Unfortunately, some
ATAPI devices lock up after being periodically queried such command
sequences.
* There is no reliable and unintrusive way for a userland program to
tell whether the target device is safe for media presence polling.
For example, polling for media presence during an on-going burning
session can make it fail. The polling program can avoid this by
opening the device with O_EXCL but then it risks making a valid
exclusive user of the device fail w/ -EBUSY.
* Userland polling is unnecessarily heavy and in-kernel implementation
is lighter and better coordinated (workqueue, timer slack).
This patch implements framework for in-kernel disk event handling,
which includes media presence polling.
* bdops->check_events() is added, which supercedes ->media_changed().
It should check whether there's any pending event and return if so.
Currently, two events are defined - DISK_EVENT_MEDIA_CHANGE and
DISK_EVENT_EJECT_REQUEST. ->check_events() is guaranteed not to be
called parallelly.
* gendisk->events and ->async_events are added. These should be
initialized by block driver before passing the device to add_disk().
The former contains the mask of all supported events and the latter
the mask of all events which the device can report without polling.
/sys/block/*/events[_async] export these to userland.
* Kernel parameter block.events_dfl_poll_msecs controls the system
polling interval (default is 0 which means disable) and
/sys/block/*/events_poll_msecs control polling intervals for
individual devices (default is -1 meaning use system setting). Note
that if a device can report all supported events asynchronously and
its polling interval isn't explicitly set, the device won't be
polled regardless of the system polling interval.
* If a device is opened exclusively with write access, event checking
is automatically disabled until all write exclusive accesses are
released.
* There are event 'clearing' events. For example, both of currently
defined events are cleared after the device has been successfully
opened. This information is passed to ->check_events() callback
using @clearing argument as a hint.
* Event checking is always performed from system_nrt_wq and timer
slack is set to 25% for polling.
* Nothing changes for drivers which implement ->media_changed() but
not ->check_events(). Going forward, all drivers will be converted
to ->check_events() and ->media_change() will be dropped.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
2010-12-08 19:57:37 +00:00
|
|
|
*/
|
|
|
|
static int disk_events_set_dfl_poll_msecs(const char *val,
|
|
|
|
const struct kernel_param *kp)
|
|
|
|
{
|
|
|
|
struct disk_events *ev;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = param_set_ulong(val, kp);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
mutex_lock(&disk_events_mutex);
|
|
|
|
|
|
|
|
list_for_each_entry(ev, &disk_events, node)
|
2011-07-01 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;
|
|
|
|
|
2019-03-27 13:51:05 +00:00
|
|
|
if (!disk->fops->check_events || !disk->events)
|
implement in-kernel gendisk events handling
Currently, media presence polling for removeable block devices is done
from userland. There are several issues with this.
* Polling is done by periodically opening the device. For SCSI
devices, the command sequence generated by such action involves a
few different commands including TEST_UNIT_READY. This behavior,
while perfectly legal, is different from Windows which only issues
single command, GET_EVENT_STATUS_NOTIFICATION. Unfortunately, some
ATAPI devices lock up after being periodically queried such command
sequences.
* There is no reliable and unintrusive way for a userland program to
tell whether the target device is safe for media presence polling.
For example, polling for media presence during an on-going burning
session can make it fail. The polling program can avoid this by
opening the device with O_EXCL but then it risks making a valid
exclusive user of the device fail w/ -EBUSY.
* Userland polling is unnecessarily heavy and in-kernel implementation
is lighter and better coordinated (workqueue, timer slack).
This patch implements framework for in-kernel disk event handling,
which includes media presence polling.
* bdops->check_events() is added, which supercedes ->media_changed().
It should check whether there's any pending event and return if so.
Currently, two events are defined - DISK_EVENT_MEDIA_CHANGE and
DISK_EVENT_EJECT_REQUEST. ->check_events() is guaranteed not to be
called parallelly.
* gendisk->events and ->async_events are added. These should be
initialized by block driver before passing the device to add_disk().
The former contains the mask of all supported events and the latter
the mask of all events which the device can report without polling.
/sys/block/*/events[_async] export these to userland.
* Kernel parameter block.events_dfl_poll_msecs controls the system
polling interval (default is 0 which means disable) and
/sys/block/*/events_poll_msecs control polling intervals for
individual devices (default is -1 meaning use system setting). Note
that if a device can report all supported events asynchronously and
its polling interval isn't explicitly set, the device won't be
polled regardless of the system polling interval.
* If a device is opened exclusively with write access, event checking
is automatically disabled until all write exclusive accesses are
released.
* There are event 'clearing' events. For example, both of currently
defined events are cleared after the device has been successfully
opened. This information is passed to ->check_events() callback
using @clearing argument as a hint.
* Event checking is always performed from system_nrt_wq and timer
slack is set to 25% for polling.
* Nothing changes for drivers which implement ->media_changed() but
not ->check_events(). Going forward, all drivers will be converted
to ->check_events() and ->media_change() will be dropped.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
2010-12-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)
|
|
|
|
{
|
|
|
|
/* FIXME: error handling */
|
|
|
|
if (sysfs_create_files(&disk_to_dev(disk)->kobj, disk_events_attrs) < 0)
|
|
|
|
pr_warn("%s: failed to create sysfs files for events\n",
|
|
|
|
disk->disk_name);
|
|
|
|
|
2019-03-27 13:51:05 +00:00
|
|
|
if (!disk->ev)
|
|
|
|
return;
|
|
|
|
|
implement in-kernel gendisk events handling
Currently, media presence polling for removeable block devices is done
from userland. There are several issues with this.
* Polling is done by periodically opening the device. For SCSI
devices, the command sequence generated by such action involves a
few different commands including TEST_UNIT_READY. This behavior,
while perfectly legal, is different from Windows which only issues
single command, GET_EVENT_STATUS_NOTIFICATION. Unfortunately, some
ATAPI devices lock up after being periodically queried such command
sequences.
* There is no reliable and unintrusive way for a userland program to
tell whether the target device is safe for media presence polling.
For example, polling for media presence during an on-going burning
session can make it fail. The polling program can avoid this by
opening the device with O_EXCL but then it risks making a valid
exclusive user of the device fail w/ -EBUSY.
* Userland polling is unnecessarily heavy and in-kernel implementation
is lighter and better coordinated (workqueue, timer slack).
This patch implements framework for in-kernel disk event handling,
which includes media presence polling.
* bdops->check_events() is added, which supercedes ->media_changed().
It should check whether there's any pending event and return if so.
Currently, two events are defined - DISK_EVENT_MEDIA_CHANGE and
DISK_EVENT_EJECT_REQUEST. ->check_events() is guaranteed not to be
called parallelly.
* gendisk->events and ->async_events are added. These should be
initialized by block driver before passing the device to add_disk().
The former contains the mask of all supported events and the latter
the mask of all events which the device can report without polling.
/sys/block/*/events[_async] export these to userland.
* Kernel parameter block.events_dfl_poll_msecs controls the system
polling interval (default is 0 which means disable) and
/sys/block/*/events_poll_msecs control polling intervals for
individual devices (default is -1 meaning use system setting). Note
that if a device can report all supported events asynchronously and
its polling interval isn't explicitly set, the device won't be
polled regardless of the system polling interval.
* If a device is opened exclusively with write access, event checking
is automatically disabled until all write exclusive accesses are
released.
* There are event 'clearing' events. For example, both of currently
defined events are cleared after the device has been successfully
opened. This information is passed to ->check_events() callback
using @clearing argument as a hint.
* Event checking is always performed from system_nrt_wq and timer
slack is set to 25% for polling.
* Nothing changes for drivers which implement ->media_changed() but
not ->check_events(). Going forward, all drivers will be converted
to ->check_events() and ->media_change() will be dropped.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
2010-12-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)
|
|
|
|
{
|
2019-03-27 13:51:05 +00:00
|
|
|
if (disk->ev) {
|
|
|
|
disk_block_events(disk);
|
implement in-kernel gendisk events handling
Currently, media presence polling for removeable block devices is done
from userland. There are several issues with this.
* Polling is done by periodically opening the device. For SCSI
devices, the command sequence generated by such action involves a
few different commands including TEST_UNIT_READY. This behavior,
while perfectly legal, is different from Windows which only issues
single command, GET_EVENT_STATUS_NOTIFICATION. Unfortunately, some
ATAPI devices lock up after being periodically queried such command
sequences.
* There is no reliable and unintrusive way for a userland program to
tell whether the target device is safe for media presence polling.
For example, polling for media presence during an on-going burning
session can make it fail. The polling program can avoid this by
opening the device with O_EXCL but then it risks making a valid
exclusive user of the device fail w/ -EBUSY.
* Userland polling is unnecessarily heavy and in-kernel implementation
is lighter and better coordinated (workqueue, timer slack).
This patch implements framework for in-kernel disk event handling,
which includes media presence polling.
* bdops->check_events() is added, which supercedes ->media_changed().
It should check whether there's any pending event and return if so.
Currently, two events are defined - DISK_EVENT_MEDIA_CHANGE and
DISK_EVENT_EJECT_REQUEST. ->check_events() is guaranteed not to be
called parallelly.
* gendisk->events and ->async_events are added. These should be
initialized by block driver before passing the device to add_disk().
The former contains the mask of all supported events and the latter
the mask of all events which the device can report without polling.
/sys/block/*/events[_async] export these to userland.
* Kernel parameter block.events_dfl_poll_msecs controls the system
polling interval (default is 0 which means disable) and
/sys/block/*/events_poll_msecs control polling intervals for
individual devices (default is -1 meaning use system setting). Note
that if a device can report all supported events asynchronously and
its polling interval isn't explicitly set, the device won't be
polled regardless of the system polling interval.
* If a device is opened exclusively with write access, event checking
is automatically disabled until all write exclusive accesses are
released.
* There are event 'clearing' events. For example, both of currently
defined events are cleared after the device has been successfully
opened. This information is passed to ->check_events() callback
using @clearing argument as a hint.
* Event checking is always performed from system_nrt_wq and timer
slack is set to 25% for polling.
* Nothing changes for drivers which implement ->media_changed() but
not ->check_events(). Going forward, all drivers will be converted
to ->check_events() and ->media_change() will be dropped.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
2010-12-08 19:57:37 +00:00
|
|
|
|
2019-03-27 13:51:05 +00:00
|
|
|
mutex_lock(&disk_events_mutex);
|
|
|
|
list_del_init(&disk->ev->node);
|
|
|
|
mutex_unlock(&disk_events_mutex);
|
|
|
|
}
|
implement in-kernel gendisk events handling
Currently, media presence polling for removeable block devices is done
from userland. There are several issues with this.
* Polling is done by periodically opening the device. For SCSI
devices, the command sequence generated by such action involves a
few different commands including TEST_UNIT_READY. This behavior,
while perfectly legal, is different from Windows which only issues
single command, GET_EVENT_STATUS_NOTIFICATION. Unfortunately, some
ATAPI devices lock up after being periodically queried such command
sequences.
* There is no reliable and unintrusive way for a userland program to
tell whether the target device is safe for media presence polling.
For example, polling for media presence during an on-going burning
session can make it fail. The polling program can avoid this by
opening the device with O_EXCL but then it risks making a valid
exclusive user of the device fail w/ -EBUSY.
* Userland polling is unnecessarily heavy and in-kernel implementation
is lighter and better coordinated (workqueue, timer slack).
This patch implements framework for in-kernel disk event handling,
which includes media presence polling.
* bdops->check_events() is added, which supercedes ->media_changed().
It should check whether there's any pending event and return if so.
Currently, two events are defined - DISK_EVENT_MEDIA_CHANGE and
DISK_EVENT_EJECT_REQUEST. ->check_events() is guaranteed not to be
called parallelly.
* gendisk->events and ->async_events are added. These should be
initialized by block driver before passing the device to add_disk().
The former contains the mask of all supported events and the latter
the mask of all events which the device can report without polling.
/sys/block/*/events[_async] export these to userland.
* Kernel parameter block.events_dfl_poll_msecs controls the system
polling interval (default is 0 which means disable) and
/sys/block/*/events_poll_msecs control polling intervals for
individual devices (default is -1 meaning use system setting). Note
that if a device can report all supported events asynchronously and
its polling interval isn't explicitly set, the device won't be
polled regardless of the system polling interval.
* If a device is opened exclusively with write access, event checking
is automatically disabled until all write exclusive accesses are
released.
* There are event 'clearing' events. For example, both of currently
defined events are cleared after the device has been successfully
opened. This information is passed to ->check_events() callback
using @clearing argument as a hint.
* Event checking is always performed from system_nrt_wq and timer
slack is set to 25% for polling.
* Nothing changes for drivers which implement ->media_changed() but
not ->check_events(). Going forward, all drivers will be converted
to ->check_events() and ->media_change() will be dropped.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
2010-12-08 19:57:37 +00:00
|
|
|
|
|
|
|
sysfs_remove_files(&disk_to_dev(disk)->kobj, disk_events_attrs);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void disk_release_events(struct gendisk *disk)
|
|
|
|
{
|
|
|
|
/* the block count should be 1 from disk_del_events() */
|
|
|
|
WARN_ON_ONCE(disk->ev && disk->ev->block != 1);
|
|
|
|
kfree(disk->ev);
|
|
|
|
}
|