5 #include <linux/module.h>
8 #include <linux/kdev_t.h>
9 #include <linux/kernel.h>
15 #include <linux/slab.h>
28 #define MAX_EXT_DEVT (1 << MINORBITS)
38 static void disk_alloc_events(
struct gendisk *disk);
39 static void disk_add_events(
struct gendisk *disk);
40 static void disk_del_events(
struct gendisk *disk);
41 static void disk_release_events(
struct gendisk *disk);
60 struct disk_part_tbl *ptbl;
94 struct disk_part_tbl *ptbl;
102 if (flags & DISK_PITER_REVERSE)
103 piter->idx = ptbl->len - 1;
104 else if (flags & (DISK_PITER_INCL_PART0 | DISK_PITER_INCL_EMPTY_PART0))
109 piter->flags =
flags;
126 struct disk_part_tbl *ptbl;
130 disk_put_part(piter->part);
138 if (piter->flags & DISK_PITER_REVERSE) {
140 if (piter->flags & (DISK_PITER_INCL_PART0 |
141 DISK_PITER_INCL_EMPTY_PART0))
151 for (; piter->idx !=
end; piter->idx +=
inc) {
152 struct hd_struct *
part;
157 if (!part_nr_sects_read(part) &&
158 !(piter->flags & DISK_PITER_INCL_EMPTY) &&
159 !(piter->flags & DISK_PITER_INCL_EMPTY_PART0 &&
186 disk_put_part(piter->part);
193 return part->start_sect <= sector &&
194 sector < part->start_sect + part_nr_sects_read(part);
214 struct disk_part_tbl *ptbl;
215 struct hd_struct *
part;
221 if (part && sector_in_part(part, sector))
224 for (i = 1; i < ptbl->len; i++) {
227 if (part && sector_in_part(part, sector)) {
240 static struct blk_major_name {
241 struct blk_major_name *
next;
247 static inline int major_to_index(
unsigned major)
252 #ifdef CONFIG_PROC_FS
255 struct blk_major_name *
dp;
259 for (dp = major_names[offset];
dp; dp = dp->next)
260 seq_printf(seqf,
"%3d %s\n", dp->major, dp->name);
284 struct blk_major_name **
n, *
p;
291 for (index =
ARRAY_SIZE(major_names)-1; index > 0; index--) {
292 if (major_names[index] ==
NULL)
297 printk(
"register_blkdev: failed to get major for %s\n",
313 strlcpy(p->name, name,
sizeof(p->name));
315 index = major_to_index(major);
317 for (n = &major_names[index]; *
n; n = &(*n)->next) {
318 if ((*n)->major == major)
327 printk(
"register_blkdev: cannot get major %d for %s\n",
340 struct blk_major_name **
n;
341 struct blk_major_name *
p =
NULL;
342 int index = major_to_index(major);
345 for (n = &major_names[index]; *
n; n = &(*n)->next)
346 if ((*n)->major == major)
348 if (!*n ||
strcmp((*n)->name, name)) {
375 static int blk_mangle_minor(
int minor)
377 #ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT
381 int low = minor & (1 <<
i);
410 struct gendisk *disk = part_to_disk(part);
414 if (part->partno < disk->minors) {
415 *devt =
MKDEV(disk->major, disk->first_minor + part->partno);
451 if (devt ==
MKDEV(0, 0))
463 if (
MAJOR(devt) <= 0xff &&
MINOR(devt) <= 0xff) {
482 kobj_map(bdev_map, devt, range, module, probe,
lock, data);
496 struct gendisk *
p =
data;
498 return &disk_to_dev(p)->kobj;
501 static int exact_lock(
dev_t devt,
void *
data)
503 struct gendisk *
p =
data;
510 static void register_disk(
struct gendisk *disk)
512 struct device *ddev = disk_to_dev(disk);
514 struct disk_part_iter piter;
515 struct hd_struct *
part;
518 ddev->
parent = disk->driverfs_dev;
523 dev_set_uevent_suppress(ddev, 1);
529 kobject_name(&ddev->
kobj));
539 if (!disk_part_scan_enabled(disk))
543 if (!get_capacity(disk))
558 dev_set_uevent_suppress(ddev, 0);
587 WARN_ON(disk->minors && !(disk->major || disk->first_minor));
588 WARN_ON(!disk->minors && !(disk->flags & GENHD_FL_EXT_DEVT));
590 disk->flags |= GENHD_FL_UP;
597 disk_to_dev(disk)->devt =
devt;
602 disk->major =
MAJOR(devt);
603 disk->first_minor =
MINOR(devt);
605 disk_alloc_events(disk);
608 bdi = &disk->queue->backing_dev_info;
612 exact_match, exact_lock, disk);
626 disk_add_events(disk);
632 struct disk_part_iter piter;
633 struct hd_struct *
part;
635 disk_del_events(disk);
639 DISK_PITER_INCL_EMPTY | DISK_PITER_REVERSE);
648 set_capacity(disk, 0);
649 disk->flags &= ~GENHD_FL_UP;
656 part_stat_set_all(&disk->part0, 0);
657 disk->part0.stamp = 0;
661 disk->driverfs_dev =
NULL;
678 struct gendisk *disk =
NULL;
685 disk = dev_to_disk(kobj_to_dev(kobj));
687 struct hd_struct *
part;
690 part =
idr_find(&ext_devt_idr, blk_mangle_minor(
MINOR(devt)));
691 if (part &&
get_disk(part_to_disk(part))) {
692 *partno = part->partno;
693 disk = part_to_disk(part);
717 struct hd_struct *
part;
722 bdev =
bdget(part_devt(part));
741 struct gendisk *disk = dev_to_disk(dev);
742 struct disk_part_iter piter;
743 struct hd_struct *
part;
746 char uuid_buf[PARTITION_META_INFO_UUIDLTH * 2 + 5];
752 if (get_capacity(disk) == 0 ||
753 (disk->flags & GENHD_FL_SUPPRESS_PARTITION_INFO))
763 bool is_part0 = part == &disk->part0;
767 snprintf(uuid_buf,
sizeof(uuid_buf),
"%pU",
770 printk(
"%s%s %10llu %s %s", is_part0 ?
"" :
" ",
771 bdevt_str(part_devt(part), devt_buf),
772 (
unsigned long long)part_nr_sects_read(part) >> 1
773 ,
disk_name(disk, part->partno, name_buf),
776 if (disk->driverfs_dev !=
NULL &&
777 disk->driverfs_dev->driver !=
NULL)
779 disk->driverfs_dev->driver->name);
790 #ifdef CONFIG_PROC_FS
792 static void *disk_seqf_start(
struct seq_file *seqf, loff_t *
pos)
810 return dev_to_disk(dev);
813 static void *disk_seqf_next(
struct seq_file *seqf,
void *
v, loff_t *pos)
820 return dev_to_disk(dev);
825 static void disk_seqf_stop(
struct seq_file *seqf,
void *v)
836 static void *show_partition_start(
struct seq_file *seqf, loff_t *pos)
840 p = disk_seqf_start(seqf, pos);
841 if (!IS_ERR_OR_NULL(p) && !*pos)
842 seq_puts(seqf,
"major minor #blocks name\n\n");
846 static int show_partition(
struct seq_file *seqf,
void *v)
848 struct gendisk *sgp =
v;
849 struct disk_part_iter piter;
850 struct hd_struct *
part;
854 if (!get_capacity(sgp) || (!disk_max_parts(sgp) &&
855 (sgp->flags & GENHD_FL_REMOVABLE)))
857 if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)
864 MAJOR(part_devt(part)),
MINOR(part_devt(part)),
865 (
unsigned long long)part_nr_sects_read(part) >> 1,
873 .
start = show_partition_start,
874 .next = disk_seqf_next,
875 .stop = disk_seqf_stop,
876 .show = show_partition
881 return seq_open(file, &partitions_op);
885 .
open = partitions_open,
893 static struct kobject *base_probe(
dev_t devt,
int *partno,
void *data)
895 if (request_module(
"block-major-%d-%d",
MAJOR(devt),
MINOR(devt)) > 0)
897 request_module(
"block-major-%d",
MAJOR(devt));
901 static int __init genhd_device_init(
void)
925 struct gendisk *disk = dev_to_disk(dev);
927 return sprintf(buf,
"%d\n", disk->minors);
933 struct gendisk *disk = dev_to_disk(dev);
935 return sprintf(buf,
"%d\n", disk_max_parts(disk));
941 struct gendisk *disk = dev_to_disk(dev);
944 (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
950 struct gendisk *disk = dev_to_disk(dev);
952 return sprintf(buf,
"%d\n", get_disk_ro(disk) ? 1 : 0);
958 struct gendisk *disk = dev_to_disk(dev);
960 return sprintf(buf,
"%x\n", disk->flags);
963 static ssize_t disk_alignment_offset_show(
struct device *dev,
967 struct gendisk *disk = dev_to_disk(dev);
969 return sprintf(buf,
"%d\n", queue_alignment_offset(disk->queue));
972 static ssize_t disk_discard_alignment_show(
struct device *dev,
976 struct gendisk *disk = dev_to_disk(dev);
978 return sprintf(buf,
"%d\n", queue_discard_alignment(disk->queue));
992 #ifdef CONFIG_FAIL_MAKE_REQUEST
996 #ifdef CONFIG_FAIL_IO_TIMEOUT
1002 static struct attribute *disk_attrs[] = {
1003 &dev_attr_range.attr,
1004 &dev_attr_ext_range.attr,
1005 &dev_attr_removable.attr,
1007 &dev_attr_size.attr,
1008 &dev_attr_alignment_offset.attr,
1009 &dev_attr_discard_alignment.attr,
1010 &dev_attr_capability.attr,
1011 &dev_attr_stat.attr,
1012 &dev_attr_inflight.attr,
1013 #ifdef CONFIG_FAIL_MAKE_REQUEST
1014 &dev_attr_fail.
attr,
1016 #ifdef CONFIG_FAIL_IO_TIMEOUT
1017 &dev_attr_fail_timeout.
attr,
1023 .attrs = disk_attrs,
1042 static void disk_replace_part_tbl(
struct gendisk *disk,
1043 struct disk_part_tbl *new_ptbl)
1045 struct disk_part_tbl *old_ptbl = disk->part_tbl;
1071 struct disk_part_tbl *old_ptbl = disk->part_tbl;
1072 struct disk_part_tbl *new_ptbl;
1073 int len = old_ptbl ? old_ptbl->len : 0;
1079 if (disk_max_parts(disk) && target > disk_max_parts(disk))
1085 size =
sizeof(*new_ptbl) + target *
sizeof(new_ptbl->part[0]);
1086 new_ptbl = kzalloc_node(size,
GFP_KERNEL, disk->node_id);
1092 for (i = 0; i < len; i++)
1095 disk_replace_part_tbl(disk, new_ptbl);
1099 static void disk_release(
struct device *dev)
1101 struct gendisk *disk = dev_to_disk(dev);
1103 disk_release_events(disk);
1104 kfree(disk->random);
1105 disk_replace_part_tbl(disk,
NULL);
1106 free_part_stats(&disk->part0);
1107 free_part_info(&disk->part0);
1118 struct gendisk *disk = dev_to_disk(dev);
1121 return disk->devnode(disk, mode);
1127 .groups = disk_attr_groups,
1128 .release = disk_release,
1129 .devnode = block_devnode,
1132 #ifdef CONFIG_PROC_FS
1140 static int diskstats_show(
struct seq_file *seqf,
void *v)
1142 struct gendisk *
gp =
v;
1143 struct disk_part_iter piter;
1144 struct hd_struct *hd;
1158 cpu = part_stat_lock();
1162 "%u %lu %lu %lu %u %u %u %u\n",
1165 part_stat_read(hd, ios[
READ]),
1166 part_stat_read(hd, merges[READ]),
1167 part_stat_read(hd,
sectors[READ]),
1169 part_stat_read(hd, ios[
WRITE]),
1170 part_stat_read(hd, merges[WRITE]),
1171 part_stat_read(hd,
sectors[WRITE]),
1184 .
start = disk_seqf_start,
1185 .next = disk_seqf_next,
1186 .stop = disk_seqf_stop,
1187 .show = diskstats_show
1192 return seq_open(file, &diskstats_op);
1196 .
open = diskstats_open,
1202 static int __init proc_genhd_init(
void)
1204 proc_create(
"diskstats", 0,
NULL, &proc_diskstats_operations);
1205 proc_create(
"partitions", 0,
NULL, &proc_partitions_operations);
1219 struct gendisk *disk = dev_to_disk(dev);
1220 struct hd_struct *
part;
1222 if (
strcmp(dev_name(dev), name))
1225 if (partno < disk->minors) {
1235 devt = part_devt(part);
1236 disk_put_part(part);
1239 disk_put_part(part);
1254 struct gendisk *disk;
1256 disk = kmalloc_node(
sizeof(
struct gendisk),
1259 if (!init_part_stats(&disk->part0)) {
1265 free_part_stats(&disk->part0);
1269 disk->part_tbl->part[0] = &disk->part0;
1281 hd_ref_init(&disk->part0);
1283 disk->minors = minors;
1284 rand_initialize_disk(disk);
1286 disk_to_dev(disk)->type = &disk_type;
1295 struct module *
owner;
1300 owner = disk->fops->owner;
1301 if (owner && !try_module_get(owner))
1322 static void set_disk_ro_uevent(
struct gendisk *gd,
int ro)
1324 char event[] =
"DISK_RO=1";
1341 struct disk_part_iter piter;
1342 struct hd_struct *
part;
1344 if (disk->part0.policy != flag) {
1345 set_disk_ro_uevent(disk, flag);
1346 disk->part0.policy =
flag;
1351 part->policy =
flag;
1397 static const char *disk_events_strs[] = {
1398 [
ilog2(DISK_EVENT_MEDIA_CHANGE)] =
"media_change",
1399 [
ilog2(DISK_EVENT_EJECT_REQUEST)] =
"eject_request",
1402 static char *disk_uevents[] = {
1403 [
ilog2(DISK_EVENT_MEDIA_CHANGE)] =
"DISK_MEDIA_CHANGE=1",
1404 [
ilog2(DISK_EVENT_EJECT_REQUEST)] =
"DISK_EJECT_REQUEST=1",
1412 static unsigned long disk_events_dfl_poll_msecs = 0;
1414 static unsigned long disk_events_poll_jiffies(
struct gendisk *disk)
1417 long intv_msecs = 0;
1426 else if (disk->events & ~disk->async_events)
1427 intv_msecs = disk_events_dfl_poll_msecs;
1450 unsigned long flags;
1463 cancel = !ev->
block++;
1464 spin_unlock_irqrestore(&ev->
lock, flags);
1472 static void __disk_unblock_events(
struct gendisk *disk,
bool check_now)
1476 unsigned long flags;
1490 intv = disk_events_poll_jiffies(disk);
1497 spin_unlock_irqrestore(&ev->
lock, flags);
1513 __disk_unblock_events(disk,
false);
1535 spin_lock_irq(&ev->
lock);
1539 spin_unlock_irq(&ev->
lock);
1555 const struct block_device_operations *bdops = disk->fops;
1561 if ((mask & DISK_EVENT_MEDIA_CHANGE) &&
1562 bdops->media_changed && bdops->media_changed(disk))
1563 return DISK_EVENT_MEDIA_CHANGE;
1568 spin_lock_irq(&ev->
lock);
1570 spin_unlock_irq(&ev->
lock);
1576 __disk_unblock_events(disk,
false);
1579 spin_lock_irq(&ev->
lock);
1583 spin_unlock_irq(&ev->
lock);
1592 struct gendisk *disk = ev->
disk;
1593 char *envp[
ARRAY_SIZE(disk_uevents) + 1] = { };
1594 unsigned int clearing = ev->
clearing;
1597 int nr_events = 0,
i;
1600 events = disk->fops->check_events(disk, clearing);
1603 spin_lock_irq(&ev->
lock);
1609 intv = disk_events_poll_jiffies(disk);
1610 if (!ev->
block && intv)
1613 spin_unlock_irq(&ev->
lock);
1621 if (events & disk->events & (1 <<
i))
1622 envp[nr_events++] = disk_uevents[
i];
1636 static ssize_t __disk_events_show(
unsigned int events,
char *buf)
1638 const char *delim =
"";
1642 for (i = 0; i <
ARRAY_SIZE(disk_events_strs); i++)
1643 if (events & (1 << i)) {
1644 pos +=
sprintf(buf + pos,
"%s%s",
1645 delim, disk_events_strs[i]);
1649 pos +=
sprintf(buf + pos,
"\n");
1656 struct gendisk *disk = dev_to_disk(dev);
1658 return __disk_events_show(disk->events, buf);
1664 struct gendisk *disk = dev_to_disk(dev);
1666 return __disk_events_show(disk->async_events, buf);
1669 static ssize_t disk_events_poll_msecs_show(
struct device *dev,
1673 struct gendisk *disk = dev_to_disk(dev);
1675 return sprintf(buf,
"%ld\n", disk->ev->poll_msecs);
1678 static ssize_t disk_events_poll_msecs_store(
struct device *dev,
1680 const char *buf,
size_t count)
1682 struct gendisk *disk = dev_to_disk(dev);
1685 if (!count || !
sscanf(buf,
"%ld", &intv))
1688 if (intv < 0 && intv != -1)
1692 disk->ev->poll_msecs = intv;
1693 __disk_unblock_events(disk,
true);
1701 disk_events_poll_msecs_show,
1702 disk_events_poll_msecs_store);
1704 static const struct attribute *disk_events_attrs[] = {
1705 &dev_attr_events.attr,
1706 &dev_attr_events_async.attr,
1707 &dev_attr_events_poll_msecs.attr,
1717 static int disk_events_set_dfl_poll_msecs(
const char *
val,
1738 .
set = disk_events_set_dfl_poll_msecs,
1742 #undef MODULE_PARAM_PREFIX
1743 #define MODULE_PARAM_PREFIX "block."
1745 module_param_cb(events_dfl_poll_msecs, &disk_events_dfl_poll_msecs_param_ops,
1746 &disk_events_dfl_poll_msecs, 0644);
1751 static void disk_alloc_events(
struct gendisk *disk)
1755 if (!disk->fops->check_events)
1760 pr_warn(
"%s: failed to initialize events\n", disk->disk_name);
1764 INIT_LIST_HEAD(&ev->
node);
1775 static void disk_add_events(
struct gendisk *disk)
1782 pr_warn(
"%s: failed to create sysfs files for events\n",
1793 __disk_unblock_events(disk,
true);
1796 static void disk_del_events(
struct gendisk *disk)
1804 list_del_init(&disk->ev->node);
1810 static void disk_release_events(
struct gendisk *disk)