1 // SPDX-License-Identifier: GPL-2.0
5 * Portions Copyright (C) 2020 Christoph Hellwig
8 #include <linux/module.h>
9 #include <linux/ctype.h>
11 #include <linux/genhd.h>
12 #include <linux/kdev_t.h>
13 #include <linux/kernel.h>
14 #include <linux/blkdev.h>
15 #include <linux/backing-dev.h>
16 #include <linux/init.h>
17 #include <linux/spinlock.h>
18 #include <linux/proc_fs.h>
19 #include <linux/seq_file.h>
20 #include <linux/slab.h>
21 #include <linux/kmod.h>
22 #include <linux/mutex.h>
23 #include <linux/idr.h>
24 #include <linux/log2.h>
25 #include <linux/pm_runtime.h>
26 #include <linux/badblocks.h>
30 static struct kobject *block_depr;
32 /* for extended dynamic devt allocation, currently only one major is used */
33 #define NR_EXT_DEVT (1 << MINORBITS)
34 static DEFINE_IDA(ext_devt_ida);
36 static void disk_check_events(struct disk_events *ev,
37 unsigned int *clearing_ptr);
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);
43 void set_capacity(struct gendisk *disk, sector_t sectors)
45 struct block_device *bdev = disk->part0;
47 spin_lock(&bdev->bd_size_lock);
48 i_size_write(bdev->bd_inode, (loff_t)sectors << SECTOR_SHIFT);
49 spin_unlock(&bdev->bd_size_lock);
51 EXPORT_SYMBOL(set_capacity);
54 * Set disk capacity and notify if the size is not currently zero and will not
55 * be set to zero. Returns true if a uevent was sent, otherwise false.
57 bool set_capacity_and_notify(struct gendisk *disk, sector_t size)
59 sector_t capacity = get_capacity(disk);
60 char *envp[] = { "RESIZE=1", NULL };
62 set_capacity(disk, size);
65 * Only print a message and send a uevent if the gendisk is user visible
66 * and alive. This avoids spamming the log and udev when setting the
67 * initial capacity during probing.
69 if (size == capacity ||
70 (disk->flags & (GENHD_FL_UP | GENHD_FL_HIDDEN)) != GENHD_FL_UP)
73 pr_info("%s: detected capacity change from %lld to %lld\n",
74 disk->disk_name, capacity, size);
77 * Historically we did not send a uevent for changes to/from an empty
80 if (!capacity || !size)
82 kobject_uevent_env(&disk_to_dev(disk)->kobj, KOBJ_CHANGE, envp);
85 EXPORT_SYMBOL_GPL(set_capacity_and_notify);
88 * Format the device name of the indicated disk into the supplied buffer and
89 * return a pointer to that same buffer for convenience.
91 char *disk_name(struct gendisk *hd, int partno, char *buf)
94 snprintf(buf, BDEVNAME_SIZE, "%s", hd->disk_name);
95 else if (isdigit(hd->disk_name[strlen(hd->disk_name)-1]))
96 snprintf(buf, BDEVNAME_SIZE, "%sp%d", hd->disk_name, partno);
98 snprintf(buf, BDEVNAME_SIZE, "%s%d", hd->disk_name, partno);
103 const char *bdevname(struct block_device *bdev, char *buf)
105 return disk_name(bdev->bd_disk, bdev->bd_partno, buf);
107 EXPORT_SYMBOL(bdevname);
109 static void part_stat_read_all(struct block_device *part,
110 struct disk_stats *stat)
114 memset(stat, 0, sizeof(struct disk_stats));
115 for_each_possible_cpu(cpu) {
116 struct disk_stats *ptr = per_cpu_ptr(part->bd_stats, cpu);
119 for (group = 0; group < NR_STAT_GROUPS; group++) {
120 stat->nsecs[group] += ptr->nsecs[group];
121 stat->sectors[group] += ptr->sectors[group];
122 stat->ios[group] += ptr->ios[group];
123 stat->merges[group] += ptr->merges[group];
126 stat->io_ticks += ptr->io_ticks;
130 static unsigned int part_in_flight(struct block_device *part)
132 unsigned int inflight = 0;
135 for_each_possible_cpu(cpu) {
136 inflight += part_stat_local_read_cpu(part, in_flight[0], cpu) +
137 part_stat_local_read_cpu(part, in_flight[1], cpu);
139 if ((int)inflight < 0)
145 static void part_in_flight_rw(struct block_device *part,
146 unsigned int inflight[2])
152 for_each_possible_cpu(cpu) {
153 inflight[0] += part_stat_local_read_cpu(part, in_flight[0], cpu);
154 inflight[1] += part_stat_local_read_cpu(part, in_flight[1], cpu);
156 if ((int)inflight[0] < 0)
158 if ((int)inflight[1] < 0)
163 * Can be deleted altogether. Later.
166 #define BLKDEV_MAJOR_HASH_SIZE 255
167 static struct blk_major_name {
168 struct blk_major_name *next;
171 void (*probe)(dev_t devt);
172 } *major_names[BLKDEV_MAJOR_HASH_SIZE];
173 static DEFINE_MUTEX(major_names_lock);
175 /* index in the above - for now: assume no multimajor ranges */
176 static inline int major_to_index(unsigned major)
178 return major % BLKDEV_MAJOR_HASH_SIZE;
181 #ifdef CONFIG_PROC_FS
182 void blkdev_show(struct seq_file *seqf, off_t offset)
184 struct blk_major_name *dp;
186 mutex_lock(&major_names_lock);
187 for (dp = major_names[major_to_index(offset)]; dp; dp = dp->next)
188 if (dp->major == offset)
189 seq_printf(seqf, "%3d %s\n", dp->major, dp->name);
190 mutex_unlock(&major_names_lock);
192 #endif /* CONFIG_PROC_FS */
195 * __register_blkdev - register a new block device
197 * @major: the requested major device number [1..BLKDEV_MAJOR_MAX-1]. If
198 * @major = 0, try to allocate any unused major number.
199 * @name: the name of the new block device as a zero terminated string
200 * @probe: allback that is called on access to any minor number of @major
202 * The @name must be unique within the system.
204 * The return value depends on the @major input parameter:
206 * - if a major device number was requested in range [1..BLKDEV_MAJOR_MAX-1]
207 * then the function returns zero on success, or a negative error code
208 * - if any unused major number was requested with @major = 0 parameter
209 * then the return value is the allocated major number in range
210 * [1..BLKDEV_MAJOR_MAX-1] or a negative error code otherwise
212 * See Documentation/admin-guide/devices.txt for the list of allocated
215 * Use register_blkdev instead for any new code.
217 int __register_blkdev(unsigned int major, const char *name,
218 void (*probe)(dev_t devt))
220 struct blk_major_name **n, *p;
223 mutex_lock(&major_names_lock);
227 for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) {
228 if (major_names[index] == NULL)
233 printk("%s: failed to get major for %s\n",
242 if (major >= BLKDEV_MAJOR_MAX) {
243 pr_err("%s: major requested (%u) is greater than the maximum (%u) for %s\n",
244 __func__, major, BLKDEV_MAJOR_MAX-1, name);
250 p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL);
258 strlcpy(p->name, name, sizeof(p->name));
260 index = major_to_index(major);
262 for (n = &major_names[index]; *n; n = &(*n)->next) {
263 if ((*n)->major == major)
272 printk("register_blkdev: cannot get major %u for %s\n",
277 mutex_unlock(&major_names_lock);
280 EXPORT_SYMBOL(__register_blkdev);
282 void unregister_blkdev(unsigned int major, const char *name)
284 struct blk_major_name **n;
285 struct blk_major_name *p = NULL;
286 int index = major_to_index(major);
288 mutex_lock(&major_names_lock);
289 for (n = &major_names[index]; *n; n = &(*n)->next)
290 if ((*n)->major == major)
292 if (!*n || strcmp((*n)->name, name)) {
298 mutex_unlock(&major_names_lock);
302 EXPORT_SYMBOL(unregister_blkdev);
305 * blk_mangle_minor - scatter minor numbers apart
306 * @minor: minor number to mangle
308 * Scatter consecutively allocated @minor number apart if MANGLE_DEVT
309 * is enabled. Mangling twice gives the original value.
317 static int blk_mangle_minor(int minor)
319 #ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT
322 for (i = 0; i < MINORBITS / 2; i++) {
323 int low = minor & (1 << i);
324 int high = minor & (1 << (MINORBITS - 1 - i));
325 int distance = MINORBITS - 1 - 2 * i;
327 minor ^= low | high; /* clear both bits */
328 low <<= distance; /* swap the positions */
330 minor |= low | high; /* and set */
336 int blk_alloc_ext_minor(void)
340 idx = ida_alloc_range(&ext_devt_ida, 0, NR_EXT_DEVT, GFP_KERNEL);
346 return blk_mangle_minor(idx);
349 void blk_free_ext_minor(unsigned int minor)
351 ida_free(&ext_devt_ida, blk_mangle_minor(minor));
354 static char *bdevt_str(dev_t devt, char *buf)
356 if (MAJOR(devt) <= 0xff && MINOR(devt) <= 0xff) {
357 char tbuf[BDEVT_SIZE];
358 snprintf(tbuf, BDEVT_SIZE, "%02x%02x", MAJOR(devt), MINOR(devt));
359 snprintf(buf, BDEVT_SIZE, "%-9s", tbuf);
361 snprintf(buf, BDEVT_SIZE, "%03x:%05x", MAJOR(devt), MINOR(devt));
366 void disk_uevent(struct gendisk *disk, enum kobject_action action)
368 struct block_device *part;
372 xa_for_each(&disk->part_tbl, idx, part) {
373 if (bdev_is_partition(part) && !bdev_nr_sectors(part))
379 kobject_uevent(bdev_kobj(part), action);
385 EXPORT_SYMBOL_GPL(disk_uevent);
387 static void disk_scan_partitions(struct gendisk *disk)
389 struct block_device *bdev;
391 if (!get_capacity(disk) || !disk_part_scan_enabled(disk))
394 set_bit(GD_NEED_PART_SCAN, &disk->state);
395 bdev = blkdev_get_by_dev(disk_devt(disk), FMODE_READ, NULL);
397 blkdev_put(bdev, FMODE_READ);
400 static void register_disk(struct device *parent, struct gendisk *disk,
401 const struct attribute_group **groups)
403 struct device *ddev = disk_to_dev(disk);
406 ddev->parent = parent;
408 dev_set_name(ddev, "%s", disk->disk_name);
410 /* delay uevents, until we scanned partition table */
411 dev_set_uevent_suppress(ddev, 1);
414 WARN_ON(ddev->groups);
415 ddev->groups = groups;
417 if (device_add(ddev))
419 if (!sysfs_deprecated) {
420 err = sysfs_create_link(block_depr, &ddev->kobj,
421 kobject_name(&ddev->kobj));
429 * avoid probable deadlock caused by allocating memory with
430 * GFP_KERNEL in runtime_resume callback of its all ancestor
433 pm_runtime_set_memalloc_noio(ddev, true);
435 disk->part0->bd_holder_dir =
436 kobject_create_and_add("holders", &ddev->kobj);
437 disk->slave_dir = kobject_create_and_add("slaves", &ddev->kobj);
439 if (disk->flags & GENHD_FL_HIDDEN)
442 disk_scan_partitions(disk);
444 /* announce the disk and partitions after all partitions are created */
445 dev_set_uevent_suppress(ddev, 0);
446 disk_uevent(disk, KOBJ_ADD);
448 if (disk->queue->backing_dev_info->dev) {
449 err = sysfs_create_link(&ddev->kobj,
450 &disk->queue->backing_dev_info->dev->kobj,
457 * __device_add_disk - add disk information to kernel list
458 * @parent: parent device for the disk
459 * @disk: per-device partitioning information
460 * @groups: Additional per-device sysfs groups
461 * @register_queue: register the queue if set to true
463 * This function registers the partitioning information in @disk
466 * FIXME: error handling
468 static void __device_add_disk(struct device *parent, struct gendisk *disk,
469 const struct attribute_group **groups,
475 * The disk queue should now be all set with enough information about
476 * the device for the elevator code to pick an adequate default
477 * elevator if one is needed, that is, for devices requesting queue
481 elevator_init_mq(disk->queue);
484 * If the driver provides an explicit major number it also must provide
485 * the number of minors numbers supported, and those will be used to
487 * Otherwise just allocate the device numbers for both the whole device
488 * and all partitions from the extended dev_t space.
491 WARN_ON(!disk->minors);
493 if (disk->minors > DISK_MAX_PARTS) {
494 pr_err("block: can't allocate more than %d partitions\n",
496 disk->minors = DISK_MAX_PARTS;
499 WARN_ON(disk->minors);
501 ret = blk_alloc_ext_minor();
506 disk->major = BLOCK_EXT_MAJOR;
507 disk->first_minor = MINOR(ret);
508 disk->flags |= GENHD_FL_EXT_DEVT;
511 disk->flags |= GENHD_FL_UP;
513 disk_alloc_events(disk);
515 if (disk->flags & GENHD_FL_HIDDEN) {
517 * Don't let hidden disks show up in /proc/partitions,
518 * and don't bother scanning for partitions either.
520 disk->flags |= GENHD_FL_SUPPRESS_PARTITION_INFO;
521 disk->flags |= GENHD_FL_NO_PART_SCAN;
523 struct backing_dev_info *bdi = disk->queue->backing_dev_info;
524 struct device *dev = disk_to_dev(disk);
526 /* Register BDI before referencing it from bdev */
527 dev->devt = MKDEV(disk->major, disk->first_minor);
528 ret = bdi_register(bdi, "%u:%u",
529 disk->major, disk->first_minor);
531 bdi_set_owner(bdi, dev);
532 bdev_add(disk->part0, dev->devt);
534 register_disk(parent, disk, groups);
536 blk_register_queue(disk);
539 * Take an extra ref on queue which will be put on disk_release()
540 * so that it sticks around as long as @disk is there.
542 if (blk_get_queue(disk->queue))
543 set_bit(GD_QUEUE_REF, &disk->state);
547 disk_add_events(disk);
548 blk_integrity_add(disk);
551 void device_add_disk(struct device *parent, struct gendisk *disk,
552 const struct attribute_group **groups)
555 __device_add_disk(parent, disk, groups, true);
557 EXPORT_SYMBOL(device_add_disk);
559 void device_add_disk_no_queue_reg(struct device *parent, struct gendisk *disk)
561 __device_add_disk(parent, disk, NULL, false);
563 EXPORT_SYMBOL(device_add_disk_no_queue_reg);
566 * del_gendisk - remove the gendisk
567 * @disk: the struct gendisk to remove
569 * Removes the gendisk and all its associated resources. This deletes the
570 * partitions associated with the gendisk, and unregisters the associated
573 * This is the counter to the respective __device_add_disk() call.
575 * The final removal of the struct gendisk happens when its refcount reaches 0
576 * with put_disk(), which should be called after del_gendisk(), if
577 * __device_add_disk() was used.
579 * Drivers exist which depend on the release of the gendisk to be synchronous,
580 * it should not be deferred.
584 void del_gendisk(struct gendisk *disk)
588 if (WARN_ON_ONCE(!disk->queue))
591 blk_integrity_del(disk);
592 disk_del_events(disk);
594 mutex_lock(&disk->part0->bd_mutex);
595 disk->flags &= ~GENHD_FL_UP;
596 blk_drop_partitions(disk);
597 mutex_unlock(&disk->part0->bd_mutex);
599 fsync_bdev(disk->part0);
600 __invalidate_device(disk->part0, true);
603 * Unhash the bdev inode for this device so that it can't be looked
604 * up any more even if openers still hold references to it.
606 remove_inode_hash(disk->part0->bd_inode);
608 set_capacity(disk, 0);
610 if (!(disk->flags & GENHD_FL_HIDDEN)) {
611 sysfs_remove_link(&disk_to_dev(disk)->kobj, "bdi");
614 * Unregister bdi before releasing device numbers (as they can
615 * get reused and we'd get clashes in sysfs).
617 bdi_unregister(disk->queue->backing_dev_info);
620 blk_unregister_queue(disk);
622 kobject_put(disk->part0->bd_holder_dir);
623 kobject_put(disk->slave_dir);
625 part_stat_set_all(disk->part0, 0);
626 disk->part0->bd_stamp = 0;
627 if (!sysfs_deprecated)
628 sysfs_remove_link(block_depr, dev_name(disk_to_dev(disk)));
629 pm_runtime_set_memalloc_noio(disk_to_dev(disk), false);
630 device_del(disk_to_dev(disk));
632 EXPORT_SYMBOL(del_gendisk);
634 /* sysfs access to bad-blocks list. */
635 static ssize_t disk_badblocks_show(struct device *dev,
636 struct device_attribute *attr,
639 struct gendisk *disk = dev_to_disk(dev);
642 return sprintf(page, "\n");
644 return badblocks_show(disk->bb, page, 0);
647 static ssize_t disk_badblocks_store(struct device *dev,
648 struct device_attribute *attr,
649 const char *page, size_t len)
651 struct gendisk *disk = dev_to_disk(dev);
656 return badblocks_store(disk->bb, page, len, 0);
659 void blk_request_module(dev_t devt)
661 unsigned int major = MAJOR(devt);
662 struct blk_major_name **n;
664 mutex_lock(&major_names_lock);
665 for (n = &major_names[major_to_index(major)]; *n; n = &(*n)->next) {
666 if ((*n)->major == major && (*n)->probe) {
668 mutex_unlock(&major_names_lock);
672 mutex_unlock(&major_names_lock);
674 if (request_module("block-major-%d-%d", MAJOR(devt), MINOR(devt)) > 0)
675 /* Make old-style 2.4 aliases work */
676 request_module("block-major-%d", MAJOR(devt));
680 * bdget_disk - do bdget() by gendisk and partition number
681 * @disk: gendisk of interest
682 * @partno: partition number
684 * Find partition @partno from @disk, do bdget() on it.
690 * Resulting block_device on success, NULL on failure.
692 struct block_device *bdget_disk(struct gendisk *disk, int partno)
694 struct block_device *bdev = NULL;
697 bdev = xa_load(&disk->part_tbl, partno);
698 if (bdev && !bdgrab(bdev))
706 * print a full list of all partitions - intended for places where the root
707 * filesystem can't be mounted and thus to give the victim some idea of what
710 void __init printk_all_partitions(void)
712 struct class_dev_iter iter;
715 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
716 while ((dev = class_dev_iter_next(&iter))) {
717 struct gendisk *disk = dev_to_disk(dev);
718 struct block_device *part;
719 char name_buf[BDEVNAME_SIZE];
720 char devt_buf[BDEVT_SIZE];
724 * Don't show empty devices or things that have been
727 if (get_capacity(disk) == 0 ||
728 (disk->flags & GENHD_FL_SUPPRESS_PARTITION_INFO))
732 * Note, unlike /proc/partitions, I am showing the numbers in
733 * hex - the same format as the root= option takes.
736 xa_for_each(&disk->part_tbl, idx, part) {
737 if (!bdev_nr_sectors(part))
739 printk("%s%s %10llu %s %s",
740 bdev_is_partition(part) ? " " : "",
741 bdevt_str(part->bd_dev, devt_buf),
742 bdev_nr_sectors(part) >> 1,
743 disk_name(disk, part->bd_partno, name_buf),
745 part->bd_meta_info->uuid : "");
746 if (bdev_is_partition(part))
748 else if (dev->parent && dev->parent->driver)
749 printk(" driver: %s\n",
750 dev->parent->driver->name);
752 printk(" (driver?)\n");
756 class_dev_iter_exit(&iter);
759 #ifdef CONFIG_PROC_FS
761 static void *disk_seqf_start(struct seq_file *seqf, loff_t *pos)
764 struct class_dev_iter *iter;
767 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
769 return ERR_PTR(-ENOMEM);
771 seqf->private = iter;
772 class_dev_iter_init(iter, &block_class, NULL, &disk_type);
774 dev = class_dev_iter_next(iter);
779 return dev_to_disk(dev);
782 static void *disk_seqf_next(struct seq_file *seqf, void *v, loff_t *pos)
787 dev = class_dev_iter_next(seqf->private);
789 return dev_to_disk(dev);
794 static void disk_seqf_stop(struct seq_file *seqf, void *v)
796 struct class_dev_iter *iter = seqf->private;
798 /* stop is called even after start failed :-( */
800 class_dev_iter_exit(iter);
802 seqf->private = NULL;
806 static void *show_partition_start(struct seq_file *seqf, loff_t *pos)
810 p = disk_seqf_start(seqf, pos);
811 if (!IS_ERR_OR_NULL(p) && !*pos)
812 seq_puts(seqf, "major minor #blocks name\n\n");
816 static int show_partition(struct seq_file *seqf, void *v)
818 struct gendisk *sgp = v;
819 struct block_device *part;
821 char buf[BDEVNAME_SIZE];
823 /* Don't show non-partitionable removeable devices or empty devices */
824 if (!get_capacity(sgp) || (!disk_max_parts(sgp) &&
825 (sgp->flags & GENHD_FL_REMOVABLE)))
827 if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)
831 xa_for_each(&sgp->part_tbl, idx, part) {
832 if (!bdev_nr_sectors(part))
834 seq_printf(seqf, "%4d %7d %10llu %s\n",
835 MAJOR(part->bd_dev), MINOR(part->bd_dev),
836 bdev_nr_sectors(part) >> 1,
837 disk_name(sgp, part->bd_partno, buf));
843 static const struct seq_operations partitions_op = {
844 .start = show_partition_start,
845 .next = disk_seqf_next,
846 .stop = disk_seqf_stop,
847 .show = show_partition
851 static int __init genhd_device_init(void)
855 block_class.dev_kobj = sysfs_dev_block_kobj;
856 error = class_register(&block_class);
861 register_blkdev(BLOCK_EXT_MAJOR, "blkext");
863 /* create top-level block dir */
864 if (!sysfs_deprecated)
865 block_depr = kobject_create_and_add("block", NULL);
869 subsys_initcall(genhd_device_init);
871 static ssize_t disk_range_show(struct device *dev,
872 struct device_attribute *attr, char *buf)
874 struct gendisk *disk = dev_to_disk(dev);
876 return sprintf(buf, "%d\n", disk->minors);
879 static ssize_t disk_ext_range_show(struct device *dev,
880 struct device_attribute *attr, char *buf)
882 struct gendisk *disk = dev_to_disk(dev);
884 return sprintf(buf, "%d\n", disk_max_parts(disk));
887 static ssize_t disk_removable_show(struct device *dev,
888 struct device_attribute *attr, char *buf)
890 struct gendisk *disk = dev_to_disk(dev);
892 return sprintf(buf, "%d\n",
893 (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
896 static ssize_t disk_hidden_show(struct device *dev,
897 struct device_attribute *attr, char *buf)
899 struct gendisk *disk = dev_to_disk(dev);
901 return sprintf(buf, "%d\n",
902 (disk->flags & GENHD_FL_HIDDEN ? 1 : 0));
905 static ssize_t disk_ro_show(struct device *dev,
906 struct device_attribute *attr, char *buf)
908 struct gendisk *disk = dev_to_disk(dev);
910 return sprintf(buf, "%d\n", get_disk_ro(disk) ? 1 : 0);
913 ssize_t part_size_show(struct device *dev,
914 struct device_attribute *attr, char *buf)
916 return sprintf(buf, "%llu\n", bdev_nr_sectors(dev_to_bdev(dev)));
919 ssize_t part_stat_show(struct device *dev,
920 struct device_attribute *attr, char *buf)
922 struct block_device *bdev = dev_to_bdev(dev);
923 struct request_queue *q = bdev->bd_disk->queue;
924 struct disk_stats stat;
925 unsigned int inflight;
927 part_stat_read_all(bdev, &stat);
929 inflight = blk_mq_in_flight(q, bdev);
931 inflight = part_in_flight(bdev);
934 "%8lu %8lu %8llu %8u "
935 "%8lu %8lu %8llu %8u "
937 "%8lu %8lu %8llu %8u "
941 stat.merges[STAT_READ],
942 (unsigned long long)stat.sectors[STAT_READ],
943 (unsigned int)div_u64(stat.nsecs[STAT_READ], NSEC_PER_MSEC),
944 stat.ios[STAT_WRITE],
945 stat.merges[STAT_WRITE],
946 (unsigned long long)stat.sectors[STAT_WRITE],
947 (unsigned int)div_u64(stat.nsecs[STAT_WRITE], NSEC_PER_MSEC),
949 jiffies_to_msecs(stat.io_ticks),
950 (unsigned int)div_u64(stat.nsecs[STAT_READ] +
951 stat.nsecs[STAT_WRITE] +
952 stat.nsecs[STAT_DISCARD] +
953 stat.nsecs[STAT_FLUSH],
955 stat.ios[STAT_DISCARD],
956 stat.merges[STAT_DISCARD],
957 (unsigned long long)stat.sectors[STAT_DISCARD],
958 (unsigned int)div_u64(stat.nsecs[STAT_DISCARD], NSEC_PER_MSEC),
959 stat.ios[STAT_FLUSH],
960 (unsigned int)div_u64(stat.nsecs[STAT_FLUSH], NSEC_PER_MSEC));
963 ssize_t part_inflight_show(struct device *dev, struct device_attribute *attr,
966 struct block_device *bdev = dev_to_bdev(dev);
967 struct request_queue *q = bdev->bd_disk->queue;
968 unsigned int inflight[2];
971 blk_mq_in_flight_rw(q, bdev, inflight);
973 part_in_flight_rw(bdev, inflight);
975 return sprintf(buf, "%8u %8u\n", inflight[0], inflight[1]);
978 static ssize_t disk_capability_show(struct device *dev,
979 struct device_attribute *attr, char *buf)
981 struct gendisk *disk = dev_to_disk(dev);
983 return sprintf(buf, "%x\n", disk->flags);
986 static ssize_t disk_alignment_offset_show(struct device *dev,
987 struct device_attribute *attr,
990 struct gendisk *disk = dev_to_disk(dev);
992 return sprintf(buf, "%d\n", queue_alignment_offset(disk->queue));
995 static ssize_t disk_discard_alignment_show(struct device *dev,
996 struct device_attribute *attr,
999 struct gendisk *disk = dev_to_disk(dev);
1001 return sprintf(buf, "%d\n", queue_discard_alignment(disk->queue));
1004 static DEVICE_ATTR(range, 0444, disk_range_show, NULL);
1005 static DEVICE_ATTR(ext_range, 0444, disk_ext_range_show, NULL);
1006 static DEVICE_ATTR(removable, 0444, disk_removable_show, NULL);
1007 static DEVICE_ATTR(hidden, 0444, disk_hidden_show, NULL);
1008 static DEVICE_ATTR(ro, 0444, disk_ro_show, NULL);
1009 static DEVICE_ATTR(size, 0444, part_size_show, NULL);
1010 static DEVICE_ATTR(alignment_offset, 0444, disk_alignment_offset_show, NULL);
1011 static DEVICE_ATTR(discard_alignment, 0444, disk_discard_alignment_show, NULL);
1012 static DEVICE_ATTR(capability, 0444, disk_capability_show, NULL);
1013 static DEVICE_ATTR(stat, 0444, part_stat_show, NULL);
1014 static DEVICE_ATTR(inflight, 0444, part_inflight_show, NULL);
1015 static DEVICE_ATTR(badblocks, 0644, disk_badblocks_show, disk_badblocks_store);
1017 #ifdef CONFIG_FAIL_MAKE_REQUEST
1018 ssize_t part_fail_show(struct device *dev,
1019 struct device_attribute *attr, char *buf)
1021 return sprintf(buf, "%d\n", dev_to_bdev(dev)->bd_make_it_fail);
1024 ssize_t part_fail_store(struct device *dev,
1025 struct device_attribute *attr,
1026 const char *buf, size_t count)
1030 if (count > 0 && sscanf(buf, "%d", &i) > 0)
1031 dev_to_bdev(dev)->bd_make_it_fail = i;
1036 static struct device_attribute dev_attr_fail =
1037 __ATTR(make-it-fail, 0644, part_fail_show, part_fail_store);
1038 #endif /* CONFIG_FAIL_MAKE_REQUEST */
1040 #ifdef CONFIG_FAIL_IO_TIMEOUT
1041 static struct device_attribute dev_attr_fail_timeout =
1042 __ATTR(io-timeout-fail, 0644, part_timeout_show, part_timeout_store);
1045 static struct attribute *disk_attrs[] = {
1046 &dev_attr_range.attr,
1047 &dev_attr_ext_range.attr,
1048 &dev_attr_removable.attr,
1049 &dev_attr_hidden.attr,
1051 &dev_attr_size.attr,
1052 &dev_attr_alignment_offset.attr,
1053 &dev_attr_discard_alignment.attr,
1054 &dev_attr_capability.attr,
1055 &dev_attr_stat.attr,
1056 &dev_attr_inflight.attr,
1057 &dev_attr_badblocks.attr,
1058 #ifdef CONFIG_FAIL_MAKE_REQUEST
1059 &dev_attr_fail.attr,
1061 #ifdef CONFIG_FAIL_IO_TIMEOUT
1062 &dev_attr_fail_timeout.attr,
1067 static umode_t disk_visible(struct kobject *kobj, struct attribute *a, int n)
1069 struct device *dev = container_of(kobj, typeof(*dev), kobj);
1070 struct gendisk *disk = dev_to_disk(dev);
1072 if (a == &dev_attr_badblocks.attr && !disk->bb)
1077 static struct attribute_group disk_attr_group = {
1078 .attrs = disk_attrs,
1079 .is_visible = disk_visible,
1082 static const struct attribute_group *disk_attr_groups[] = {
1088 * disk_release - releases all allocated resources of the gendisk
1089 * @dev: the device representing this disk
1091 * This function releases all allocated resources of the gendisk.
1093 * Drivers which used __device_add_disk() have a gendisk with a request_queue
1094 * assigned. Since the request_queue sits on top of the gendisk for these
1095 * drivers we also call blk_put_queue() for them, and we expect the
1096 * request_queue refcount to reach 0 at this point, and so the request_queue
1097 * will also be freed prior to the disk.
1099 * Context: can sleep
1101 static void disk_release(struct device *dev)
1103 struct gendisk *disk = dev_to_disk(dev);
1107 if (MAJOR(dev->devt) == BLOCK_EXT_MAJOR)
1108 blk_free_ext_minor(MINOR(dev->devt));
1109 disk_release_events(disk);
1110 kfree(disk->random);
1111 xa_destroy(&disk->part_tbl);
1113 if (test_bit(GD_QUEUE_REF, &disk->state) && disk->queue)
1114 blk_put_queue(disk->queue);
1117 struct class block_class = {
1121 static char *block_devnode(struct device *dev, umode_t *mode,
1122 kuid_t *uid, kgid_t *gid)
1124 struct gendisk *disk = dev_to_disk(dev);
1126 if (disk->fops->devnode)
1127 return disk->fops->devnode(disk, mode);
1131 const struct device_type disk_type = {
1133 .groups = disk_attr_groups,
1134 .release = disk_release,
1135 .devnode = block_devnode,
1138 #ifdef CONFIG_PROC_FS
1140 * aggregate disk stat collector. Uses the same stats that the sysfs
1141 * entries do, above, but makes them available through one seq_file.
1143 * The output looks suspiciously like /proc/partitions with a bunch of
1146 static int diskstats_show(struct seq_file *seqf, void *v)
1148 struct gendisk *gp = v;
1149 struct block_device *hd;
1150 char buf[BDEVNAME_SIZE];
1151 unsigned int inflight;
1152 struct disk_stats stat;
1156 if (&disk_to_dev(gp)->kobj.entry == block_class.devices.next)
1157 seq_puts(seqf, "major minor name"
1158 " rio rmerge rsect ruse wio wmerge "
1159 "wsect wuse running use aveq"
1164 xa_for_each(&gp->part_tbl, idx, hd) {
1165 if (bdev_is_partition(hd) && !bdev_nr_sectors(hd))
1167 part_stat_read_all(hd, &stat);
1168 if (queue_is_mq(gp->queue))
1169 inflight = blk_mq_in_flight(gp->queue, hd);
1171 inflight = part_in_flight(hd);
1173 seq_printf(seqf, "%4d %7d %s "
1180 MAJOR(hd->bd_dev), MINOR(hd->bd_dev),
1181 disk_name(gp, hd->bd_partno, buf),
1182 stat.ios[STAT_READ],
1183 stat.merges[STAT_READ],
1184 stat.sectors[STAT_READ],
1185 (unsigned int)div_u64(stat.nsecs[STAT_READ],
1187 stat.ios[STAT_WRITE],
1188 stat.merges[STAT_WRITE],
1189 stat.sectors[STAT_WRITE],
1190 (unsigned int)div_u64(stat.nsecs[STAT_WRITE],
1193 jiffies_to_msecs(stat.io_ticks),
1194 (unsigned int)div_u64(stat.nsecs[STAT_READ] +
1195 stat.nsecs[STAT_WRITE] +
1196 stat.nsecs[STAT_DISCARD] +
1197 stat.nsecs[STAT_FLUSH],
1199 stat.ios[STAT_DISCARD],
1200 stat.merges[STAT_DISCARD],
1201 stat.sectors[STAT_DISCARD],
1202 (unsigned int)div_u64(stat.nsecs[STAT_DISCARD],
1204 stat.ios[STAT_FLUSH],
1205 (unsigned int)div_u64(stat.nsecs[STAT_FLUSH],
1214 static const struct seq_operations diskstats_op = {
1215 .start = disk_seqf_start,
1216 .next = disk_seqf_next,
1217 .stop = disk_seqf_stop,
1218 .show = diskstats_show
1221 static int __init proc_genhd_init(void)
1223 proc_create_seq("diskstats", 0, NULL, &diskstats_op);
1224 proc_create_seq("partitions", 0, NULL, &partitions_op);
1227 module_init(proc_genhd_init);
1228 #endif /* CONFIG_PROC_FS */
1230 dev_t blk_lookup_devt(const char *name, int partno)
1232 dev_t devt = MKDEV(0, 0);
1233 struct class_dev_iter iter;
1236 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
1237 while ((dev = class_dev_iter_next(&iter))) {
1238 struct gendisk *disk = dev_to_disk(dev);
1239 struct block_device *part;
1241 if (strcmp(dev_name(dev), name))
1244 if (partno < disk->minors) {
1245 /* We need to return the right devno, even
1246 * if the partition doesn't exist yet.
1248 devt = MKDEV(MAJOR(dev->devt),
1249 MINOR(dev->devt) + partno);
1252 part = bdget_disk(disk, partno);
1254 devt = part->bd_dev;
1259 class_dev_iter_exit(&iter);
1263 struct gendisk *__alloc_disk_node(int minors, int node_id)
1265 struct gendisk *disk;
1267 disk = kzalloc_node(sizeof(struct gendisk), GFP_KERNEL, node_id);
1271 disk->part0 = bdev_alloc(disk, 0);
1275 disk->node_id = node_id;
1276 xa_init(&disk->part_tbl);
1277 if (xa_insert(&disk->part_tbl, 0, disk->part0, GFP_KERNEL))
1278 goto out_destroy_part_tbl;
1280 disk->minors = minors;
1281 rand_initialize_disk(disk);
1282 disk_to_dev(disk)->class = &block_class;
1283 disk_to_dev(disk)->type = &disk_type;
1284 device_initialize(disk_to_dev(disk));
1287 out_destroy_part_tbl:
1288 xa_destroy(&disk->part_tbl);
1294 EXPORT_SYMBOL(__alloc_disk_node);
1297 * put_disk - decrements the gendisk refcount
1298 * @disk: the struct gendisk to decrement the refcount for
1300 * This decrements the refcount for the struct gendisk. When this reaches 0
1301 * we'll have disk_release() called.
1303 * Context: Any context, but the last reference must not be dropped from
1306 void put_disk(struct gendisk *disk)
1309 put_device(disk_to_dev(disk));
1311 EXPORT_SYMBOL(put_disk);
1313 static void set_disk_ro_uevent(struct gendisk *gd, int ro)
1315 char event[] = "DISK_RO=1";
1316 char *envp[] = { event, NULL };
1320 kobject_uevent_env(&disk_to_dev(gd)->kobj, KOBJ_CHANGE, envp);
1324 * set_disk_ro - set a gendisk read-only
1325 * @disk: gendisk to operate on
1326 * @read_only: %true to set the disk read-only, %false set the disk read/write
1328 * This function is used to indicate whether a given disk device should have its
1329 * read-only flag set. set_disk_ro() is typically used by device drivers to
1330 * indicate whether the underlying physical device is write-protected.
1332 void set_disk_ro(struct gendisk *disk, bool read_only)
1335 if (test_and_set_bit(GD_READ_ONLY, &disk->state))
1338 if (!test_and_clear_bit(GD_READ_ONLY, &disk->state))
1341 set_disk_ro_uevent(disk, read_only);
1343 EXPORT_SYMBOL(set_disk_ro);
1345 int bdev_read_only(struct block_device *bdev)
1347 return bdev->bd_read_only || get_disk_ro(bdev->bd_disk);
1349 EXPORT_SYMBOL(bdev_read_only);
1352 * Disk events - monitor disk events like media change and eject request.
1354 struct disk_events {
1355 struct list_head node; /* all disk_event's */
1356 struct gendisk *disk; /* the associated disk */
1359 struct mutex block_mutex; /* protects blocking */
1360 int block; /* event blocking depth */
1361 unsigned int pending; /* events already sent out */
1362 unsigned int clearing; /* events being cleared */
1364 long poll_msecs; /* interval, -1 for default */
1365 struct delayed_work dwork;
1368 static const char *disk_events_strs[] = {
1369 [ilog2(DISK_EVENT_MEDIA_CHANGE)] = "media_change",
1370 [ilog2(DISK_EVENT_EJECT_REQUEST)] = "eject_request",
1373 static char *disk_uevents[] = {
1374 [ilog2(DISK_EVENT_MEDIA_CHANGE)] = "DISK_MEDIA_CHANGE=1",
1375 [ilog2(DISK_EVENT_EJECT_REQUEST)] = "DISK_EJECT_REQUEST=1",
1378 /* list of all disk_events */
1379 static DEFINE_MUTEX(disk_events_mutex);
1380 static LIST_HEAD(disk_events);
1382 /* disable in-kernel polling by default */
1383 static unsigned long disk_events_dfl_poll_msecs;
1385 static unsigned long disk_events_poll_jiffies(struct gendisk *disk)
1387 struct disk_events *ev = disk->ev;
1388 long intv_msecs = 0;
1391 * If device-specific poll interval is set, always use it. If
1392 * the default is being used, poll if the POLL flag is set.
1394 if (ev->poll_msecs >= 0)
1395 intv_msecs = ev->poll_msecs;
1396 else if (disk->event_flags & DISK_EVENT_FLAG_POLL)
1397 intv_msecs = disk_events_dfl_poll_msecs;
1399 return msecs_to_jiffies(intv_msecs);
1403 * disk_block_events - block and flush disk event checking
1404 * @disk: disk to block events for
1406 * On return from this function, it is guaranteed that event checking
1407 * isn't in progress and won't happen until unblocked by
1408 * disk_unblock_events(). Events blocking is counted and the actual
1409 * unblocking happens after the matching number of unblocks are done.
1411 * Note that this intentionally does not block event checking from
1412 * disk_clear_events().
1417 void disk_block_events(struct gendisk *disk)
1419 struct disk_events *ev = disk->ev;
1420 unsigned long flags;
1427 * Outer mutex ensures that the first blocker completes canceling
1428 * the event work before further blockers are allowed to finish.
1430 mutex_lock(&ev->block_mutex);
1432 spin_lock_irqsave(&ev->lock, flags);
1433 cancel = !ev->block++;
1434 spin_unlock_irqrestore(&ev->lock, flags);
1437 cancel_delayed_work_sync(&disk->ev->dwork);
1439 mutex_unlock(&ev->block_mutex);
1442 static void __disk_unblock_events(struct gendisk *disk, bool check_now)
1444 struct disk_events *ev = disk->ev;
1446 unsigned long flags;
1448 spin_lock_irqsave(&ev->lock, flags);
1450 if (WARN_ON_ONCE(ev->block <= 0))
1456 intv = disk_events_poll_jiffies(disk);
1458 queue_delayed_work(system_freezable_power_efficient_wq,
1461 queue_delayed_work(system_freezable_power_efficient_wq,
1464 spin_unlock_irqrestore(&ev->lock, flags);
1468 * disk_unblock_events - unblock disk event checking
1469 * @disk: disk to unblock events for
1471 * Undo disk_block_events(). When the block count reaches zero, it
1472 * starts events polling if configured.
1475 * Don't care. Safe to call from irq context.
1477 void disk_unblock_events(struct gendisk *disk)
1480 __disk_unblock_events(disk, false);
1484 * disk_flush_events - schedule immediate event checking and flushing
1485 * @disk: disk to check and flush events for
1486 * @mask: events to flush
1488 * Schedule immediate event checking on @disk if not blocked. Events in
1489 * @mask are scheduled to be cleared from the driver. Note that this
1490 * doesn't clear the events from @disk->ev.
1493 * If @mask is non-zero must be called with bdev->bd_mutex held.
1495 void disk_flush_events(struct gendisk *disk, unsigned int mask)
1497 struct disk_events *ev = disk->ev;
1502 spin_lock_irq(&ev->lock);
1503 ev->clearing |= mask;
1505 mod_delayed_work(system_freezable_power_efficient_wq,
1507 spin_unlock_irq(&ev->lock);
1511 * disk_clear_events - synchronously check, clear and return pending events
1512 * @disk: disk to fetch and clear events from
1513 * @mask: mask of events to be fetched and cleared
1515 * Disk events are synchronously checked and pending events in @mask
1516 * are cleared and returned. This ignores the block count.
1521 static unsigned int disk_clear_events(struct gendisk *disk, unsigned int mask)
1523 struct disk_events *ev = disk->ev;
1524 unsigned int pending;
1525 unsigned int clearing = mask;
1530 disk_block_events(disk);
1533 * store the union of mask and ev->clearing on the stack so that the
1534 * race with disk_flush_events does not cause ambiguity (ev->clearing
1535 * can still be modified even if events are blocked).
1537 spin_lock_irq(&ev->lock);
1538 clearing |= ev->clearing;
1540 spin_unlock_irq(&ev->lock);
1542 disk_check_events(ev, &clearing);
1544 * if ev->clearing is not 0, the disk_flush_events got called in the
1545 * middle of this function, so we want to run the workfn without delay.
1547 __disk_unblock_events(disk, ev->clearing ? true : false);
1549 /* then, fetch and clear pending events */
1550 spin_lock_irq(&ev->lock);
1551 pending = ev->pending & mask;
1552 ev->pending &= ~mask;
1553 spin_unlock_irq(&ev->lock);
1554 WARN_ON_ONCE(clearing & mask);
1560 * bdev_check_media_change - check if a removable media has been changed
1561 * @bdev: block device to check
1563 * Check whether a removable media has been changed, and attempt to free all
1564 * dentries and inodes and invalidates all block device page cache entries in
1567 * Returns %true if the block device changed, or %false if not.
1569 bool bdev_check_media_change(struct block_device *bdev)
1571 unsigned int events;
1573 events = disk_clear_events(bdev->bd_disk, DISK_EVENT_MEDIA_CHANGE |
1574 DISK_EVENT_EJECT_REQUEST);
1575 if (!(events & DISK_EVENT_MEDIA_CHANGE))
1578 if (__invalidate_device(bdev, true))
1579 pr_warn("VFS: busy inodes on changed media %s\n",
1580 bdev->bd_disk->disk_name);
1581 set_bit(GD_NEED_PART_SCAN, &bdev->bd_disk->state);
1584 EXPORT_SYMBOL(bdev_check_media_change);
1587 * Separate this part out so that a different pointer for clearing_ptr can be
1588 * passed in for disk_clear_events.
1590 static void disk_events_workfn(struct work_struct *work)
1592 struct delayed_work *dwork = to_delayed_work(work);
1593 struct disk_events *ev = container_of(dwork, struct disk_events, dwork);
1595 disk_check_events(ev, &ev->clearing);
1598 static void disk_check_events(struct disk_events *ev,
1599 unsigned int *clearing_ptr)
1601 struct gendisk *disk = ev->disk;
1602 char *envp[ARRAY_SIZE(disk_uevents) + 1] = { };
1603 unsigned int clearing = *clearing_ptr;
1604 unsigned int events;
1606 int nr_events = 0, i;
1609 events = disk->fops->check_events(disk, clearing);
1611 /* accumulate pending events and schedule next poll if necessary */
1612 spin_lock_irq(&ev->lock);
1614 events &= ~ev->pending;
1615 ev->pending |= events;
1616 *clearing_ptr &= ~clearing;
1618 intv = disk_events_poll_jiffies(disk);
1619 if (!ev->block && intv)
1620 queue_delayed_work(system_freezable_power_efficient_wq,
1623 spin_unlock_irq(&ev->lock);
1626 * Tell userland about new events. Only the events listed in
1627 * @disk->events are reported, and only if DISK_EVENT_FLAG_UEVENT
1628 * is set. Otherwise, events are processed internally but never
1629 * get reported to userland.
1631 for (i = 0; i < ARRAY_SIZE(disk_uevents); i++)
1632 if ((events & disk->events & (1 << i)) &&
1633 (disk->event_flags & DISK_EVENT_FLAG_UEVENT))
1634 envp[nr_events++] = disk_uevents[i];
1637 kobject_uevent_env(&disk_to_dev(disk)->kobj, KOBJ_CHANGE, envp);
1641 * A disk events enabled device has the following sysfs nodes under
1642 * its /sys/block/X/ directory.
1644 * events : list of all supported events
1645 * events_async : list of events which can be detected w/o polling
1646 * (always empty, only for backwards compatibility)
1647 * events_poll_msecs : polling interval, 0: disable, -1: system default
1649 static ssize_t __disk_events_show(unsigned int events, char *buf)
1651 const char *delim = "";
1655 for (i = 0; i < ARRAY_SIZE(disk_events_strs); i++)
1656 if (events & (1 << i)) {
1657 pos += sprintf(buf + pos, "%s%s",
1658 delim, disk_events_strs[i]);
1662 pos += sprintf(buf + pos, "\n");
1666 static ssize_t disk_events_show(struct device *dev,
1667 struct device_attribute *attr, char *buf)
1669 struct gendisk *disk = dev_to_disk(dev);
1671 if (!(disk->event_flags & DISK_EVENT_FLAG_UEVENT))
1674 return __disk_events_show(disk->events, buf);
1677 static ssize_t disk_events_async_show(struct device *dev,
1678 struct device_attribute *attr, char *buf)
1683 static ssize_t disk_events_poll_msecs_show(struct device *dev,
1684 struct device_attribute *attr,
1687 struct gendisk *disk = dev_to_disk(dev);
1690 return sprintf(buf, "-1\n");
1692 return sprintf(buf, "%ld\n", disk->ev->poll_msecs);
1695 static ssize_t disk_events_poll_msecs_store(struct device *dev,
1696 struct device_attribute *attr,
1697 const char *buf, size_t count)
1699 struct gendisk *disk = dev_to_disk(dev);
1702 if (!count || !sscanf(buf, "%ld", &intv))
1705 if (intv < 0 && intv != -1)
1711 disk_block_events(disk);
1712 disk->ev->poll_msecs = intv;
1713 __disk_unblock_events(disk, true);
1718 static const DEVICE_ATTR(events, 0444, disk_events_show, NULL);
1719 static const DEVICE_ATTR(events_async, 0444, disk_events_async_show, NULL);
1720 static const DEVICE_ATTR(events_poll_msecs, 0644,
1721 disk_events_poll_msecs_show,
1722 disk_events_poll_msecs_store);
1724 static const struct attribute *disk_events_attrs[] = {
1725 &dev_attr_events.attr,
1726 &dev_attr_events_async.attr,
1727 &dev_attr_events_poll_msecs.attr,
1732 * The default polling interval can be specified by the kernel
1733 * parameter block.events_dfl_poll_msecs which defaults to 0
1734 * (disable). This can also be modified runtime by writing to
1735 * /sys/module/block/parameters/events_dfl_poll_msecs.
1737 static int disk_events_set_dfl_poll_msecs(const char *val,
1738 const struct kernel_param *kp)
1740 struct disk_events *ev;
1743 ret = param_set_ulong(val, kp);
1747 mutex_lock(&disk_events_mutex);
1749 list_for_each_entry(ev, &disk_events, node)
1750 disk_flush_events(ev->disk, 0);
1752 mutex_unlock(&disk_events_mutex);
1757 static const struct kernel_param_ops disk_events_dfl_poll_msecs_param_ops = {
1758 .set = disk_events_set_dfl_poll_msecs,
1759 .get = param_get_ulong,
1762 #undef MODULE_PARAM_PREFIX
1763 #define MODULE_PARAM_PREFIX "block."
1765 module_param_cb(events_dfl_poll_msecs, &disk_events_dfl_poll_msecs_param_ops,
1766 &disk_events_dfl_poll_msecs, 0644);
1769 * disk_{alloc|add|del|release}_events - initialize and destroy disk_events.
1771 static void disk_alloc_events(struct gendisk *disk)
1773 struct disk_events *ev;
1775 if (!disk->fops->check_events || !disk->events)
1778 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
1780 pr_warn("%s: failed to initialize events\n", disk->disk_name);
1784 INIT_LIST_HEAD(&ev->node);
1786 spin_lock_init(&ev->lock);
1787 mutex_init(&ev->block_mutex);
1789 ev->poll_msecs = -1;
1790 INIT_DELAYED_WORK(&ev->dwork, disk_events_workfn);
1795 static void disk_add_events(struct gendisk *disk)
1797 /* FIXME: error handling */
1798 if (sysfs_create_files(&disk_to_dev(disk)->kobj, disk_events_attrs) < 0)
1799 pr_warn("%s: failed to create sysfs files for events\n",
1805 mutex_lock(&disk_events_mutex);
1806 list_add_tail(&disk->ev->node, &disk_events);
1807 mutex_unlock(&disk_events_mutex);
1810 * Block count is initialized to 1 and the following initial
1811 * unblock kicks it into action.
1813 __disk_unblock_events(disk, true);
1816 static void disk_del_events(struct gendisk *disk)
1819 disk_block_events(disk);
1821 mutex_lock(&disk_events_mutex);
1822 list_del_init(&disk->ev->node);
1823 mutex_unlock(&disk_events_mutex);
1826 sysfs_remove_files(&disk_to_dev(disk)->kobj, disk_events_attrs);
1829 static void disk_release_events(struct gendisk *disk)
1831 /* the block count should be 1 from disk_del_events() */
1832 WARN_ON_ONCE(disk->ev && disk->ev->block != 1);