1 // SPDX-License-Identifier: GPL-2.0
6 #include <linux/module.h>
7 #include <linux/ctype.h>
9 #include <linux/genhd.h>
10 #include <linux/kdev_t.h>
11 #include <linux/kernel.h>
12 #include <linux/blkdev.h>
13 #include <linux/backing-dev.h>
14 #include <linux/init.h>
15 #include <linux/spinlock.h>
16 #include <linux/proc_fs.h>
17 #include <linux/seq_file.h>
18 #include <linux/slab.h>
19 #include <linux/kmod.h>
20 #include <linux/mutex.h>
21 #include <linux/idr.h>
22 #include <linux/log2.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/badblocks.h>
28 static struct kobject *block_depr;
30 DECLARE_RWSEM(bdev_lookup_sem);
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, size, capacity);
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)
162 struct block_device *__disk_get_part(struct gendisk *disk, int partno)
164 struct disk_part_tbl *ptbl = rcu_dereference(disk->part_tbl);
166 if (unlikely(partno < 0 || partno >= ptbl->len))
168 return rcu_dereference(ptbl->part[partno]);
172 * disk_part_iter_init - initialize partition iterator
173 * @piter: iterator to initialize
174 * @disk: disk to iterate over
175 * @flags: DISK_PITER_* flags
177 * Initialize @piter so that it iterates over partitions of @disk.
182 void disk_part_iter_init(struct disk_part_iter *piter, struct gendisk *disk,
185 struct disk_part_tbl *ptbl;
188 ptbl = rcu_dereference(disk->part_tbl);
193 if (flags & DISK_PITER_REVERSE)
194 piter->idx = ptbl->len - 1;
195 else if (flags & (DISK_PITER_INCL_PART0 | DISK_PITER_INCL_EMPTY_PART0))
200 piter->flags = flags;
204 EXPORT_SYMBOL_GPL(disk_part_iter_init);
207 * disk_part_iter_next - proceed iterator to the next partition and return it
208 * @piter: iterator of interest
210 * Proceed @piter to the next partition and return it.
215 struct block_device *disk_part_iter_next(struct disk_part_iter *piter)
217 struct disk_part_tbl *ptbl;
220 /* put the last partition */
221 disk_part_iter_exit(piter);
225 ptbl = rcu_dereference(piter->disk->part_tbl);
227 /* determine iteration parameters */
228 if (piter->flags & DISK_PITER_REVERSE) {
230 if (piter->flags & (DISK_PITER_INCL_PART0 |
231 DISK_PITER_INCL_EMPTY_PART0))
240 /* iterate to the next partition */
241 for (; piter->idx != end; piter->idx += inc) {
242 struct block_device *part;
244 part = rcu_dereference(ptbl->part[piter->idx]);
247 if (!bdev_nr_sectors(part) &&
248 !(piter->flags & DISK_PITER_INCL_EMPTY) &&
249 !(piter->flags & DISK_PITER_INCL_EMPTY_PART0 &&
253 piter->part = bdgrab(part);
264 EXPORT_SYMBOL_GPL(disk_part_iter_next);
267 * disk_part_iter_exit - finish up partition iteration
268 * @piter: iter of interest
270 * Called when iteration is over. Cleans up @piter.
275 void disk_part_iter_exit(struct disk_part_iter *piter)
281 EXPORT_SYMBOL_GPL(disk_part_iter_exit);
283 static inline int sector_in_part(struct block_device *part, sector_t sector)
285 return part->bd_start_sect <= sector &&
286 sector < part->bd_start_sect + bdev_nr_sectors(part);
290 * disk_map_sector_rcu - map sector to partition
291 * @disk: gendisk of interest
292 * @sector: sector to map
294 * Find out which partition @sector maps to on @disk. This is
295 * primarily used for stats accounting.
301 * Found partition on success, part0 is returned if no partition matches
302 * or the matched partition is being deleted.
304 struct block_device *disk_map_sector_rcu(struct gendisk *disk, sector_t sector)
306 struct disk_part_tbl *ptbl;
307 struct block_device *part;
311 ptbl = rcu_dereference(disk->part_tbl);
313 part = rcu_dereference(ptbl->last_lookup);
314 if (part && sector_in_part(part, sector))
317 for (i = 1; i < ptbl->len; i++) {
318 part = rcu_dereference(ptbl->part[i]);
319 if (part && sector_in_part(part, sector)) {
320 rcu_assign_pointer(ptbl->last_lookup, part);
332 * disk_has_partitions
333 * @disk: gendisk of interest
335 * Walk through the partition table and check if valid partition exists.
341 * True if the gendisk has at least one valid non-zero size partition.
344 bool disk_has_partitions(struct gendisk *disk)
346 struct disk_part_tbl *ptbl;
351 ptbl = rcu_dereference(disk->part_tbl);
353 /* Iterate partitions skipping the whole device at index 0 */
354 for (i = 1; i < ptbl->len; i++) {
355 if (rcu_dereference(ptbl->part[i])) {
365 EXPORT_SYMBOL_GPL(disk_has_partitions);
368 * Can be deleted altogether. Later.
371 #define BLKDEV_MAJOR_HASH_SIZE 255
372 static struct blk_major_name {
373 struct blk_major_name *next;
376 void (*probe)(dev_t devt);
377 } *major_names[BLKDEV_MAJOR_HASH_SIZE];
378 static DEFINE_MUTEX(major_names_lock);
380 /* index in the above - for now: assume no multimajor ranges */
381 static inline int major_to_index(unsigned major)
383 return major % BLKDEV_MAJOR_HASH_SIZE;
386 #ifdef CONFIG_PROC_FS
387 void blkdev_show(struct seq_file *seqf, off_t offset)
389 struct blk_major_name *dp;
391 mutex_lock(&major_names_lock);
392 for (dp = major_names[major_to_index(offset)]; dp; dp = dp->next)
393 if (dp->major == offset)
394 seq_printf(seqf, "%3d %s\n", dp->major, dp->name);
395 mutex_unlock(&major_names_lock);
397 #endif /* CONFIG_PROC_FS */
400 * __register_blkdev - register a new block device
402 * @major: the requested major device number [1..BLKDEV_MAJOR_MAX-1]. If
403 * @major = 0, try to allocate any unused major number.
404 * @name: the name of the new block device as a zero terminated string
405 * @probe: allback that is called on access to any minor number of @major
407 * The @name must be unique within the system.
409 * The return value depends on the @major input parameter:
411 * - if a major device number was requested in range [1..BLKDEV_MAJOR_MAX-1]
412 * then the function returns zero on success, or a negative error code
413 * - if any unused major number was requested with @major = 0 parameter
414 * then the return value is the allocated major number in range
415 * [1..BLKDEV_MAJOR_MAX-1] or a negative error code otherwise
417 * See Documentation/admin-guide/devices.txt for the list of allocated
420 * Use register_blkdev instead for any new code.
422 int __register_blkdev(unsigned int major, const char *name,
423 void (*probe)(dev_t devt))
425 struct blk_major_name **n, *p;
428 mutex_lock(&major_names_lock);
432 for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) {
433 if (major_names[index] == NULL)
438 printk("%s: failed to get major for %s\n",
447 if (major >= BLKDEV_MAJOR_MAX) {
448 pr_err("%s: major requested (%u) is greater than the maximum (%u) for %s\n",
449 __func__, major, BLKDEV_MAJOR_MAX-1, name);
455 p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL);
463 strlcpy(p->name, name, sizeof(p->name));
465 index = major_to_index(major);
467 for (n = &major_names[index]; *n; n = &(*n)->next) {
468 if ((*n)->major == major)
477 printk("register_blkdev: cannot get major %u for %s\n",
482 mutex_unlock(&major_names_lock);
485 EXPORT_SYMBOL(__register_blkdev);
487 void unregister_blkdev(unsigned int major, const char *name)
489 struct blk_major_name **n;
490 struct blk_major_name *p = NULL;
491 int index = major_to_index(major);
493 mutex_lock(&major_names_lock);
494 for (n = &major_names[index]; *n; n = &(*n)->next)
495 if ((*n)->major == major)
497 if (!*n || strcmp((*n)->name, name)) {
503 mutex_unlock(&major_names_lock);
507 EXPORT_SYMBOL(unregister_blkdev);
510 * blk_mangle_minor - scatter minor numbers apart
511 * @minor: minor number to mangle
513 * Scatter consecutively allocated @minor number apart if MANGLE_DEVT
514 * is enabled. Mangling twice gives the original value.
522 static int blk_mangle_minor(int minor)
524 #ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT
527 for (i = 0; i < MINORBITS / 2; i++) {
528 int low = minor & (1 << i);
529 int high = minor & (1 << (MINORBITS - 1 - i));
530 int distance = MINORBITS - 1 - 2 * i;
532 minor ^= low | high; /* clear both bits */
533 low <<= distance; /* swap the positions */
535 minor |= low | high; /* and set */
542 * blk_alloc_devt - allocate a dev_t for a block device
543 * @bdev: block device to allocate dev_t for
544 * @devt: out parameter for resulting dev_t
546 * Allocate a dev_t for block device.
549 * 0 on success, allocated dev_t is returned in *@devt. -errno on
555 int blk_alloc_devt(struct block_device *bdev, dev_t *devt)
557 struct gendisk *disk = bdev->bd_disk;
560 /* in consecutive minor range? */
561 if (bdev->bd_partno < disk->minors) {
562 *devt = MKDEV(disk->major, disk->first_minor + bdev->bd_partno);
566 idx = ida_alloc_range(&ext_devt_ida, 0, NR_EXT_DEVT, GFP_KERNEL);
568 return idx == -ENOSPC ? -EBUSY : idx;
570 *devt = MKDEV(BLOCK_EXT_MAJOR, blk_mangle_minor(idx));
575 * blk_free_devt - free a dev_t
576 * @devt: dev_t to free
578 * Free @devt which was allocated using blk_alloc_devt().
583 void blk_free_devt(dev_t devt)
585 if (MAJOR(devt) == BLOCK_EXT_MAJOR)
586 ida_free(&ext_devt_ida, blk_mangle_minor(MINOR(devt)));
589 static char *bdevt_str(dev_t devt, char *buf)
591 if (MAJOR(devt) <= 0xff && MINOR(devt) <= 0xff) {
592 char tbuf[BDEVT_SIZE];
593 snprintf(tbuf, BDEVT_SIZE, "%02x%02x", MAJOR(devt), MINOR(devt));
594 snprintf(buf, BDEVT_SIZE, "%-9s", tbuf);
596 snprintf(buf, BDEVT_SIZE, "%03x:%05x", MAJOR(devt), MINOR(devt));
601 static void disk_scan_partitions(struct gendisk *disk)
603 struct block_device *bdev;
605 if (!get_capacity(disk) || !disk_part_scan_enabled(disk))
608 set_bit(GD_NEED_PART_SCAN, &disk->state);
609 bdev = blkdev_get_by_dev(disk_devt(disk), FMODE_READ, NULL);
611 blkdev_put(bdev, FMODE_READ);
614 static void register_disk(struct device *parent, struct gendisk *disk,
615 const struct attribute_group **groups)
617 struct device *ddev = disk_to_dev(disk);
618 struct disk_part_iter piter;
619 struct block_device *part;
622 ddev->parent = parent;
624 dev_set_name(ddev, "%s", disk->disk_name);
626 /* delay uevents, until we scanned partition table */
627 dev_set_uevent_suppress(ddev, 1);
630 WARN_ON(ddev->groups);
631 ddev->groups = groups;
633 if (device_add(ddev))
635 if (!sysfs_deprecated) {
636 err = sysfs_create_link(block_depr, &ddev->kobj,
637 kobject_name(&ddev->kobj));
645 * avoid probable deadlock caused by allocating memory with
646 * GFP_KERNEL in runtime_resume callback of its all ancestor
649 pm_runtime_set_memalloc_noio(ddev, true);
651 disk->part0->bd_holder_dir =
652 kobject_create_and_add("holders", &ddev->kobj);
653 disk->slave_dir = kobject_create_and_add("slaves", &ddev->kobj);
655 if (disk->flags & GENHD_FL_HIDDEN) {
656 dev_set_uevent_suppress(ddev, 0);
660 disk_scan_partitions(disk);
662 /* announce disk after possible partitions are created */
663 dev_set_uevent_suppress(ddev, 0);
664 kobject_uevent(&ddev->kobj, KOBJ_ADD);
666 /* announce possible partitions */
667 disk_part_iter_init(&piter, disk, 0);
668 while ((part = disk_part_iter_next(&piter)))
669 kobject_uevent(bdev_kobj(part), KOBJ_ADD);
670 disk_part_iter_exit(&piter);
672 if (disk->queue->backing_dev_info->dev) {
673 err = sysfs_create_link(&ddev->kobj,
674 &disk->queue->backing_dev_info->dev->kobj,
681 * __device_add_disk - add disk information to kernel list
682 * @parent: parent device for the disk
683 * @disk: per-device partitioning information
684 * @groups: Additional per-device sysfs groups
685 * @register_queue: register the queue if set to true
687 * This function registers the partitioning information in @disk
690 * FIXME: error handling
692 static void __device_add_disk(struct device *parent, struct gendisk *disk,
693 const struct attribute_group **groups,
700 * The disk queue should now be all set with enough information about
701 * the device for the elevator code to pick an adequate default
702 * elevator if one is needed, that is, for devices requesting queue
706 elevator_init_mq(disk->queue);
708 /* minors == 0 indicates to use ext devt from part0 and should
709 * be accompanied with EXT_DEVT flag. Make sure all
710 * parameters make sense.
712 WARN_ON(disk->minors && !(disk->major || disk->first_minor));
713 WARN_ON(!disk->minors &&
714 !(disk->flags & (GENHD_FL_EXT_DEVT | GENHD_FL_HIDDEN)));
716 disk->flags |= GENHD_FL_UP;
718 retval = blk_alloc_devt(disk->part0, &devt);
723 disk->major = MAJOR(devt);
724 disk->first_minor = MINOR(devt);
726 disk_alloc_events(disk);
728 if (disk->flags & GENHD_FL_HIDDEN) {
730 * Don't let hidden disks show up in /proc/partitions,
731 * and don't bother scanning for partitions either.
733 disk->flags |= GENHD_FL_SUPPRESS_PARTITION_INFO;
734 disk->flags |= GENHD_FL_NO_PART_SCAN;
736 struct backing_dev_info *bdi = disk->queue->backing_dev_info;
737 struct device *dev = disk_to_dev(disk);
740 /* Register BDI before referencing it from bdev */
742 ret = bdi_register(bdi, "%u:%u", MAJOR(devt), MINOR(devt));
744 bdi_set_owner(bdi, dev);
745 bdev_add(disk->part0, devt);
747 register_disk(parent, disk, groups);
749 blk_register_queue(disk);
752 * Take an extra ref on queue which will be put on disk_release()
753 * so that it sticks around as long as @disk is there.
755 WARN_ON_ONCE(!blk_get_queue(disk->queue));
757 disk_add_events(disk);
758 blk_integrity_add(disk);
761 void device_add_disk(struct device *parent, struct gendisk *disk,
762 const struct attribute_group **groups)
765 __device_add_disk(parent, disk, groups, true);
767 EXPORT_SYMBOL(device_add_disk);
769 void device_add_disk_no_queue_reg(struct device *parent, struct gendisk *disk)
771 __device_add_disk(parent, disk, NULL, false);
773 EXPORT_SYMBOL(device_add_disk_no_queue_reg);
775 static void invalidate_partition(struct block_device *bdev)
778 __invalidate_device(bdev, true);
781 * Unhash the bdev inode for this device so that it can't be looked
782 * up any more even if openers still hold references to it.
784 remove_inode_hash(bdev->bd_inode);
788 * del_gendisk - remove the gendisk
789 * @disk: the struct gendisk to remove
791 * Removes the gendisk and all its associated resources. This deletes the
792 * partitions associated with the gendisk, and unregisters the associated
795 * This is the counter to the respective __device_add_disk() call.
797 * The final removal of the struct gendisk happens when its refcount reaches 0
798 * with put_disk(), which should be called after del_gendisk(), if
799 * __device_add_disk() was used.
801 * Drivers exist which depend on the release of the gendisk to be synchronous,
802 * it should not be deferred.
806 void del_gendisk(struct gendisk *disk)
808 struct disk_part_iter piter;
809 struct block_device *part;
813 if (WARN_ON_ONCE(!disk->queue))
816 blk_integrity_del(disk);
817 disk_del_events(disk);
820 * Block lookups of the disk until all bdevs are unhashed and the
821 * disk is marked as dead (GENHD_FL_UP cleared).
823 down_write(&bdev_lookup_sem);
825 /* invalidate stuff */
826 disk_part_iter_init(&piter, disk,
827 DISK_PITER_INCL_EMPTY | DISK_PITER_REVERSE);
828 while ((part = disk_part_iter_next(&piter))) {
829 invalidate_partition(part);
830 delete_partition(part);
832 disk_part_iter_exit(&piter);
834 invalidate_partition(disk->part0);
835 set_capacity(disk, 0);
836 disk->flags &= ~GENHD_FL_UP;
837 up_write(&bdev_lookup_sem);
839 if (!(disk->flags & GENHD_FL_HIDDEN)) {
840 sysfs_remove_link(&disk_to_dev(disk)->kobj, "bdi");
843 * Unregister bdi before releasing device numbers (as they can
844 * get reused and we'd get clashes in sysfs).
846 bdi_unregister(disk->queue->backing_dev_info);
849 blk_unregister_queue(disk);
851 kobject_put(disk->part0->bd_holder_dir);
852 kobject_put(disk->slave_dir);
854 part_stat_set_all(disk->part0, 0);
855 disk->part0->bd_stamp = 0;
856 if (!sysfs_deprecated)
857 sysfs_remove_link(block_depr, dev_name(disk_to_dev(disk)));
858 pm_runtime_set_memalloc_noio(disk_to_dev(disk), false);
859 device_del(disk_to_dev(disk));
861 EXPORT_SYMBOL(del_gendisk);
863 /* sysfs access to bad-blocks list. */
864 static ssize_t disk_badblocks_show(struct device *dev,
865 struct device_attribute *attr,
868 struct gendisk *disk = dev_to_disk(dev);
871 return sprintf(page, "\n");
873 return badblocks_show(disk->bb, page, 0);
876 static ssize_t disk_badblocks_store(struct device *dev,
877 struct device_attribute *attr,
878 const char *page, size_t len)
880 struct gendisk *disk = dev_to_disk(dev);
885 return badblocks_store(disk->bb, page, len, 0);
888 void blk_request_module(dev_t devt)
890 unsigned int major = MAJOR(devt);
891 struct blk_major_name **n;
893 mutex_lock(&major_names_lock);
894 for (n = &major_names[major_to_index(major)]; *n; n = &(*n)->next) {
895 if ((*n)->major == major && (*n)->probe) {
897 mutex_unlock(&major_names_lock);
901 mutex_unlock(&major_names_lock);
903 if (request_module("block-major-%d-%d", MAJOR(devt), MINOR(devt)) > 0)
904 /* Make old-style 2.4 aliases work */
905 request_module("block-major-%d", MAJOR(devt));
909 * bdget_disk - do bdget() by gendisk and partition number
910 * @disk: gendisk of interest
911 * @partno: partition number
913 * Find partition @partno from @disk, do bdget() on it.
919 * Resulting block_device on success, NULL on failure.
921 struct block_device *bdget_disk(struct gendisk *disk, int partno)
923 struct block_device *bdev = NULL;
926 bdev = __disk_get_part(disk, partno);
927 if (bdev && !bdgrab(bdev))
935 * print a full list of all partitions - intended for places where the root
936 * filesystem can't be mounted and thus to give the victim some idea of what
939 void __init printk_all_partitions(void)
941 struct class_dev_iter iter;
944 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
945 while ((dev = class_dev_iter_next(&iter))) {
946 struct gendisk *disk = dev_to_disk(dev);
947 struct disk_part_iter piter;
948 struct block_device *part;
949 char name_buf[BDEVNAME_SIZE];
950 char devt_buf[BDEVT_SIZE];
953 * Don't show empty devices or things that have been
956 if (get_capacity(disk) == 0 ||
957 (disk->flags & GENHD_FL_SUPPRESS_PARTITION_INFO))
961 * Note, unlike /proc/partitions, I am showing the
962 * numbers in hex - the same format as the root=
965 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
966 while ((part = disk_part_iter_next(&piter))) {
967 bool is_part0 = part == disk->part0;
969 printk("%s%s %10llu %s %s", is_part0 ? "" : " ",
970 bdevt_str(part->bd_dev, devt_buf),
971 bdev_nr_sectors(part) >> 1,
972 disk_name(disk, part->bd_partno, name_buf),
974 part->bd_meta_info->uuid : "");
976 if (dev->parent && dev->parent->driver)
977 printk(" driver: %s\n",
978 dev->parent->driver->name);
980 printk(" (driver?)\n");
984 disk_part_iter_exit(&piter);
986 class_dev_iter_exit(&iter);
989 #ifdef CONFIG_PROC_FS
991 static void *disk_seqf_start(struct seq_file *seqf, loff_t *pos)
994 struct class_dev_iter *iter;
997 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
999 return ERR_PTR(-ENOMEM);
1001 seqf->private = iter;
1002 class_dev_iter_init(iter, &block_class, NULL, &disk_type);
1004 dev = class_dev_iter_next(iter);
1009 return dev_to_disk(dev);
1012 static void *disk_seqf_next(struct seq_file *seqf, void *v, loff_t *pos)
1017 dev = class_dev_iter_next(seqf->private);
1019 return dev_to_disk(dev);
1024 static void disk_seqf_stop(struct seq_file *seqf, void *v)
1026 struct class_dev_iter *iter = seqf->private;
1028 /* stop is called even after start failed :-( */
1030 class_dev_iter_exit(iter);
1032 seqf->private = NULL;
1036 static void *show_partition_start(struct seq_file *seqf, loff_t *pos)
1040 p = disk_seqf_start(seqf, pos);
1041 if (!IS_ERR_OR_NULL(p) && !*pos)
1042 seq_puts(seqf, "major minor #blocks name\n\n");
1046 static int show_partition(struct seq_file *seqf, void *v)
1048 struct gendisk *sgp = v;
1049 struct disk_part_iter piter;
1050 struct block_device *part;
1051 char buf[BDEVNAME_SIZE];
1053 /* Don't show non-partitionable removeable devices or empty devices */
1054 if (!get_capacity(sgp) || (!disk_max_parts(sgp) &&
1055 (sgp->flags & GENHD_FL_REMOVABLE)))
1057 if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)
1060 /* show the full disk and all non-0 size partitions of it */
1061 disk_part_iter_init(&piter, sgp, DISK_PITER_INCL_PART0);
1062 while ((part = disk_part_iter_next(&piter)))
1063 seq_printf(seqf, "%4d %7d %10llu %s\n",
1064 MAJOR(part->bd_dev), MINOR(part->bd_dev),
1065 bdev_nr_sectors(part) >> 1,
1066 disk_name(sgp, part->bd_partno, buf));
1067 disk_part_iter_exit(&piter);
1072 static const struct seq_operations partitions_op = {
1073 .start = show_partition_start,
1074 .next = disk_seqf_next,
1075 .stop = disk_seqf_stop,
1076 .show = show_partition
1080 static int __init genhd_device_init(void)
1084 block_class.dev_kobj = sysfs_dev_block_kobj;
1085 error = class_register(&block_class);
1086 if (unlikely(error))
1090 register_blkdev(BLOCK_EXT_MAJOR, "blkext");
1092 /* create top-level block dir */
1093 if (!sysfs_deprecated)
1094 block_depr = kobject_create_and_add("block", NULL);
1098 subsys_initcall(genhd_device_init);
1100 static ssize_t disk_range_show(struct device *dev,
1101 struct device_attribute *attr, char *buf)
1103 struct gendisk *disk = dev_to_disk(dev);
1105 return sprintf(buf, "%d\n", disk->minors);
1108 static ssize_t disk_ext_range_show(struct device *dev,
1109 struct device_attribute *attr, char *buf)
1111 struct gendisk *disk = dev_to_disk(dev);
1113 return sprintf(buf, "%d\n", disk_max_parts(disk));
1116 static ssize_t disk_removable_show(struct device *dev,
1117 struct device_attribute *attr, char *buf)
1119 struct gendisk *disk = dev_to_disk(dev);
1121 return sprintf(buf, "%d\n",
1122 (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
1125 static ssize_t disk_hidden_show(struct device *dev,
1126 struct device_attribute *attr, char *buf)
1128 struct gendisk *disk = dev_to_disk(dev);
1130 return sprintf(buf, "%d\n",
1131 (disk->flags & GENHD_FL_HIDDEN ? 1 : 0));
1134 static ssize_t disk_ro_show(struct device *dev,
1135 struct device_attribute *attr, char *buf)
1137 struct gendisk *disk = dev_to_disk(dev);
1139 return sprintf(buf, "%d\n", get_disk_ro(disk) ? 1 : 0);
1142 ssize_t part_size_show(struct device *dev,
1143 struct device_attribute *attr, char *buf)
1145 return sprintf(buf, "%llu\n", bdev_nr_sectors(dev_to_bdev(dev)));
1148 ssize_t part_stat_show(struct device *dev,
1149 struct device_attribute *attr, char *buf)
1151 struct block_device *bdev = dev_to_bdev(dev);
1152 struct request_queue *q = bdev->bd_disk->queue;
1153 struct disk_stats stat;
1154 unsigned int inflight;
1156 part_stat_read_all(bdev, &stat);
1158 inflight = blk_mq_in_flight(q, bdev);
1160 inflight = part_in_flight(bdev);
1163 "%8lu %8lu %8llu %8u "
1164 "%8lu %8lu %8llu %8u "
1166 "%8lu %8lu %8llu %8u "
1169 stat.ios[STAT_READ],
1170 stat.merges[STAT_READ],
1171 (unsigned long long)stat.sectors[STAT_READ],
1172 (unsigned int)div_u64(stat.nsecs[STAT_READ], NSEC_PER_MSEC),
1173 stat.ios[STAT_WRITE],
1174 stat.merges[STAT_WRITE],
1175 (unsigned long long)stat.sectors[STAT_WRITE],
1176 (unsigned int)div_u64(stat.nsecs[STAT_WRITE], NSEC_PER_MSEC),
1178 jiffies_to_msecs(stat.io_ticks),
1179 (unsigned int)div_u64(stat.nsecs[STAT_READ] +
1180 stat.nsecs[STAT_WRITE] +
1181 stat.nsecs[STAT_DISCARD] +
1182 stat.nsecs[STAT_FLUSH],
1184 stat.ios[STAT_DISCARD],
1185 stat.merges[STAT_DISCARD],
1186 (unsigned long long)stat.sectors[STAT_DISCARD],
1187 (unsigned int)div_u64(stat.nsecs[STAT_DISCARD], NSEC_PER_MSEC),
1188 stat.ios[STAT_FLUSH],
1189 (unsigned int)div_u64(stat.nsecs[STAT_FLUSH], NSEC_PER_MSEC));
1192 ssize_t part_inflight_show(struct device *dev, struct device_attribute *attr,
1195 struct block_device *bdev = dev_to_bdev(dev);
1196 struct request_queue *q = bdev->bd_disk->queue;
1197 unsigned int inflight[2];
1200 blk_mq_in_flight_rw(q, bdev, inflight);
1202 part_in_flight_rw(bdev, inflight);
1204 return sprintf(buf, "%8u %8u\n", inflight[0], inflight[1]);
1207 static ssize_t disk_capability_show(struct device *dev,
1208 struct device_attribute *attr, char *buf)
1210 struct gendisk *disk = dev_to_disk(dev);
1212 return sprintf(buf, "%x\n", disk->flags);
1215 static ssize_t disk_alignment_offset_show(struct device *dev,
1216 struct device_attribute *attr,
1219 struct gendisk *disk = dev_to_disk(dev);
1221 return sprintf(buf, "%d\n", queue_alignment_offset(disk->queue));
1224 static ssize_t disk_discard_alignment_show(struct device *dev,
1225 struct device_attribute *attr,
1228 struct gendisk *disk = dev_to_disk(dev);
1230 return sprintf(buf, "%d\n", queue_discard_alignment(disk->queue));
1233 static DEVICE_ATTR(range, 0444, disk_range_show, NULL);
1234 static DEVICE_ATTR(ext_range, 0444, disk_ext_range_show, NULL);
1235 static DEVICE_ATTR(removable, 0444, disk_removable_show, NULL);
1236 static DEVICE_ATTR(hidden, 0444, disk_hidden_show, NULL);
1237 static DEVICE_ATTR(ro, 0444, disk_ro_show, NULL);
1238 static DEVICE_ATTR(size, 0444, part_size_show, NULL);
1239 static DEVICE_ATTR(alignment_offset, 0444, disk_alignment_offset_show, NULL);
1240 static DEVICE_ATTR(discard_alignment, 0444, disk_discard_alignment_show, NULL);
1241 static DEVICE_ATTR(capability, 0444, disk_capability_show, NULL);
1242 static DEVICE_ATTR(stat, 0444, part_stat_show, NULL);
1243 static DEVICE_ATTR(inflight, 0444, part_inflight_show, NULL);
1244 static DEVICE_ATTR(badblocks, 0644, disk_badblocks_show, disk_badblocks_store);
1246 #ifdef CONFIG_FAIL_MAKE_REQUEST
1247 ssize_t part_fail_show(struct device *dev,
1248 struct device_attribute *attr, char *buf)
1250 return sprintf(buf, "%d\n", dev_to_bdev(dev)->bd_make_it_fail);
1253 ssize_t part_fail_store(struct device *dev,
1254 struct device_attribute *attr,
1255 const char *buf, size_t count)
1259 if (count > 0 && sscanf(buf, "%d", &i) > 0)
1260 dev_to_bdev(dev)->bd_make_it_fail = i;
1265 static struct device_attribute dev_attr_fail =
1266 __ATTR(make-it-fail, 0644, part_fail_show, part_fail_store);
1267 #endif /* CONFIG_FAIL_MAKE_REQUEST */
1269 #ifdef CONFIG_FAIL_IO_TIMEOUT
1270 static struct device_attribute dev_attr_fail_timeout =
1271 __ATTR(io-timeout-fail, 0644, part_timeout_show, part_timeout_store);
1274 static struct attribute *disk_attrs[] = {
1275 &dev_attr_range.attr,
1276 &dev_attr_ext_range.attr,
1277 &dev_attr_removable.attr,
1278 &dev_attr_hidden.attr,
1280 &dev_attr_size.attr,
1281 &dev_attr_alignment_offset.attr,
1282 &dev_attr_discard_alignment.attr,
1283 &dev_attr_capability.attr,
1284 &dev_attr_stat.attr,
1285 &dev_attr_inflight.attr,
1286 &dev_attr_badblocks.attr,
1287 #ifdef CONFIG_FAIL_MAKE_REQUEST
1288 &dev_attr_fail.attr,
1290 #ifdef CONFIG_FAIL_IO_TIMEOUT
1291 &dev_attr_fail_timeout.attr,
1296 static umode_t disk_visible(struct kobject *kobj, struct attribute *a, int n)
1298 struct device *dev = container_of(kobj, typeof(*dev), kobj);
1299 struct gendisk *disk = dev_to_disk(dev);
1301 if (a == &dev_attr_badblocks.attr && !disk->bb)
1306 static struct attribute_group disk_attr_group = {
1307 .attrs = disk_attrs,
1308 .is_visible = disk_visible,
1311 static const struct attribute_group *disk_attr_groups[] = {
1317 * disk_replace_part_tbl - replace disk->part_tbl in RCU-safe way
1318 * @disk: disk to replace part_tbl for
1319 * @new_ptbl: new part_tbl to install
1321 * Replace disk->part_tbl with @new_ptbl in RCU-safe way. The
1322 * original ptbl is freed using RCU callback.
1325 * Matching bd_mutex locked or the caller is the only user of @disk.
1327 static void disk_replace_part_tbl(struct gendisk *disk,
1328 struct disk_part_tbl *new_ptbl)
1330 struct disk_part_tbl *old_ptbl =
1331 rcu_dereference_protected(disk->part_tbl, 1);
1333 rcu_assign_pointer(disk->part_tbl, new_ptbl);
1336 rcu_assign_pointer(old_ptbl->last_lookup, NULL);
1337 kfree_rcu(old_ptbl, rcu_head);
1342 * disk_expand_part_tbl - expand disk->part_tbl
1343 * @disk: disk to expand part_tbl for
1344 * @partno: expand such that this partno can fit in
1346 * Expand disk->part_tbl such that @partno can fit in. disk->part_tbl
1347 * uses RCU to allow unlocked dereferencing for stats and other stuff.
1350 * Matching bd_mutex locked or the caller is the only user of @disk.
1354 * 0 on success, -errno on failure.
1356 int disk_expand_part_tbl(struct gendisk *disk, int partno)
1358 struct disk_part_tbl *old_ptbl =
1359 rcu_dereference_protected(disk->part_tbl, 1);
1360 struct disk_part_tbl *new_ptbl;
1361 int len = old_ptbl ? old_ptbl->len : 0;
1365 * check for int overflow, since we can get here from blkpg_ioctl()
1366 * with a user passed 'partno'.
1368 target = partno + 1;
1372 /* disk_max_parts() is zero during initialization, ignore if so */
1373 if (disk_max_parts(disk) && target > disk_max_parts(disk))
1379 new_ptbl = kzalloc_node(struct_size(new_ptbl, part, target), GFP_KERNEL,
1384 new_ptbl->len = target;
1386 for (i = 0; i < len; i++)
1387 rcu_assign_pointer(new_ptbl->part[i], old_ptbl->part[i]);
1389 disk_replace_part_tbl(disk, new_ptbl);
1394 * disk_release - releases all allocated resources of the gendisk
1395 * @dev: the device representing this disk
1397 * This function releases all allocated resources of the gendisk.
1399 * Drivers which used __device_add_disk() have a gendisk with a request_queue
1400 * assigned. Since the request_queue sits on top of the gendisk for these
1401 * drivers we also call blk_put_queue() for them, and we expect the
1402 * request_queue refcount to reach 0 at this point, and so the request_queue
1403 * will also be freed prior to the disk.
1405 * Context: can sleep
1407 static void disk_release(struct device *dev)
1409 struct gendisk *disk = dev_to_disk(dev);
1413 blk_free_devt(dev->devt);
1414 disk_release_events(disk);
1415 kfree(disk->random);
1416 disk_replace_part_tbl(disk, NULL);
1419 blk_put_queue(disk->queue);
1422 struct class block_class = {
1426 static char *block_devnode(struct device *dev, umode_t *mode,
1427 kuid_t *uid, kgid_t *gid)
1429 struct gendisk *disk = dev_to_disk(dev);
1431 if (disk->fops->devnode)
1432 return disk->fops->devnode(disk, mode);
1436 const struct device_type disk_type = {
1438 .groups = disk_attr_groups,
1439 .release = disk_release,
1440 .devnode = block_devnode,
1443 #ifdef CONFIG_PROC_FS
1445 * aggregate disk stat collector. Uses the same stats that the sysfs
1446 * entries do, above, but makes them available through one seq_file.
1448 * The output looks suspiciously like /proc/partitions with a bunch of
1451 static int diskstats_show(struct seq_file *seqf, void *v)
1453 struct gendisk *gp = v;
1454 struct disk_part_iter piter;
1455 struct block_device *hd;
1456 char buf[BDEVNAME_SIZE];
1457 unsigned int inflight;
1458 struct disk_stats stat;
1461 if (&disk_to_dev(gp)->kobj.entry == block_class.devices.next)
1462 seq_puts(seqf, "major minor name"
1463 " rio rmerge rsect ruse wio wmerge "
1464 "wsect wuse running use aveq"
1468 disk_part_iter_init(&piter, gp, DISK_PITER_INCL_EMPTY_PART0);
1469 while ((hd = disk_part_iter_next(&piter))) {
1470 part_stat_read_all(hd, &stat);
1471 if (queue_is_mq(gp->queue))
1472 inflight = blk_mq_in_flight(gp->queue, hd);
1474 inflight = part_in_flight(hd);
1476 seq_printf(seqf, "%4d %7d %s "
1483 MAJOR(hd->bd_dev), MINOR(hd->bd_dev),
1484 disk_name(gp, hd->bd_partno, buf),
1485 stat.ios[STAT_READ],
1486 stat.merges[STAT_READ],
1487 stat.sectors[STAT_READ],
1488 (unsigned int)div_u64(stat.nsecs[STAT_READ],
1490 stat.ios[STAT_WRITE],
1491 stat.merges[STAT_WRITE],
1492 stat.sectors[STAT_WRITE],
1493 (unsigned int)div_u64(stat.nsecs[STAT_WRITE],
1496 jiffies_to_msecs(stat.io_ticks),
1497 (unsigned int)div_u64(stat.nsecs[STAT_READ] +
1498 stat.nsecs[STAT_WRITE] +
1499 stat.nsecs[STAT_DISCARD] +
1500 stat.nsecs[STAT_FLUSH],
1502 stat.ios[STAT_DISCARD],
1503 stat.merges[STAT_DISCARD],
1504 stat.sectors[STAT_DISCARD],
1505 (unsigned int)div_u64(stat.nsecs[STAT_DISCARD],
1507 stat.ios[STAT_FLUSH],
1508 (unsigned int)div_u64(stat.nsecs[STAT_FLUSH],
1512 disk_part_iter_exit(&piter);
1517 static const struct seq_operations diskstats_op = {
1518 .start = disk_seqf_start,
1519 .next = disk_seqf_next,
1520 .stop = disk_seqf_stop,
1521 .show = diskstats_show
1524 static int __init proc_genhd_init(void)
1526 proc_create_seq("diskstats", 0, NULL, &diskstats_op);
1527 proc_create_seq("partitions", 0, NULL, &partitions_op);
1530 module_init(proc_genhd_init);
1531 #endif /* CONFIG_PROC_FS */
1533 dev_t blk_lookup_devt(const char *name, int partno)
1535 dev_t devt = MKDEV(0, 0);
1536 struct class_dev_iter iter;
1539 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
1540 while ((dev = class_dev_iter_next(&iter))) {
1541 struct gendisk *disk = dev_to_disk(dev);
1542 struct block_device *part;
1544 if (strcmp(dev_name(dev), name))
1547 if (partno < disk->minors) {
1548 /* We need to return the right devno, even
1549 * if the partition doesn't exist yet.
1551 devt = MKDEV(MAJOR(dev->devt),
1552 MINOR(dev->devt) + partno);
1555 part = bdget_disk(disk, partno);
1557 devt = part->bd_dev;
1562 class_dev_iter_exit(&iter);
1566 struct gendisk *__alloc_disk_node(int minors, int node_id)
1568 struct gendisk *disk;
1569 struct disk_part_tbl *ptbl;
1571 if (minors > DISK_MAX_PARTS) {
1573 "block: can't allocate more than %d partitions\n",
1575 minors = DISK_MAX_PARTS;
1578 disk = kzalloc_node(sizeof(struct gendisk), GFP_KERNEL, node_id);
1582 disk->part0 = bdev_alloc(disk, 0);
1586 disk->node_id = node_id;
1587 if (disk_expand_part_tbl(disk, 0))
1590 ptbl = rcu_dereference_protected(disk->part_tbl, 1);
1591 rcu_assign_pointer(ptbl->part[0], disk->part0);
1593 disk->minors = minors;
1594 rand_initialize_disk(disk);
1595 disk_to_dev(disk)->class = &block_class;
1596 disk_to_dev(disk)->type = &disk_type;
1597 device_initialize(disk_to_dev(disk));
1606 EXPORT_SYMBOL(__alloc_disk_node);
1609 * put_disk - decrements the gendisk refcount
1610 * @disk: the struct gendisk to decrement the refcount for
1612 * This decrements the refcount for the struct gendisk. When this reaches 0
1613 * we'll have disk_release() called.
1615 * Context: Any context, but the last reference must not be dropped from
1618 void put_disk(struct gendisk *disk)
1621 put_device(disk_to_dev(disk));
1623 EXPORT_SYMBOL(put_disk);
1625 static void set_disk_ro_uevent(struct gendisk *gd, int ro)
1627 char event[] = "DISK_RO=1";
1628 char *envp[] = { event, NULL };
1632 kobject_uevent_env(&disk_to_dev(gd)->kobj, KOBJ_CHANGE, envp);
1635 void set_disk_ro(struct gendisk *disk, int flag)
1637 struct disk_part_iter piter;
1638 struct block_device *part;
1640 if (disk->part0->bd_read_only != flag) {
1641 set_disk_ro_uevent(disk, flag);
1642 disk->part0->bd_read_only = flag;
1645 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_EMPTY);
1646 while ((part = disk_part_iter_next(&piter)))
1647 part->bd_read_only = flag;
1648 disk_part_iter_exit(&piter);
1651 EXPORT_SYMBOL(set_disk_ro);
1653 int bdev_read_only(struct block_device *bdev)
1657 return bdev->bd_read_only;
1660 EXPORT_SYMBOL(bdev_read_only);
1663 * Disk events - monitor disk events like media change and eject request.
1665 struct disk_events {
1666 struct list_head node; /* all disk_event's */
1667 struct gendisk *disk; /* the associated disk */
1670 struct mutex block_mutex; /* protects blocking */
1671 int block; /* event blocking depth */
1672 unsigned int pending; /* events already sent out */
1673 unsigned int clearing; /* events being cleared */
1675 long poll_msecs; /* interval, -1 for default */
1676 struct delayed_work dwork;
1679 static const char *disk_events_strs[] = {
1680 [ilog2(DISK_EVENT_MEDIA_CHANGE)] = "media_change",
1681 [ilog2(DISK_EVENT_EJECT_REQUEST)] = "eject_request",
1684 static char *disk_uevents[] = {
1685 [ilog2(DISK_EVENT_MEDIA_CHANGE)] = "DISK_MEDIA_CHANGE=1",
1686 [ilog2(DISK_EVENT_EJECT_REQUEST)] = "DISK_EJECT_REQUEST=1",
1689 /* list of all disk_events */
1690 static DEFINE_MUTEX(disk_events_mutex);
1691 static LIST_HEAD(disk_events);
1693 /* disable in-kernel polling by default */
1694 static unsigned long disk_events_dfl_poll_msecs;
1696 static unsigned long disk_events_poll_jiffies(struct gendisk *disk)
1698 struct disk_events *ev = disk->ev;
1699 long intv_msecs = 0;
1702 * If device-specific poll interval is set, always use it. If
1703 * the default is being used, poll if the POLL flag is set.
1705 if (ev->poll_msecs >= 0)
1706 intv_msecs = ev->poll_msecs;
1707 else if (disk->event_flags & DISK_EVENT_FLAG_POLL)
1708 intv_msecs = disk_events_dfl_poll_msecs;
1710 return msecs_to_jiffies(intv_msecs);
1714 * disk_block_events - block and flush disk event checking
1715 * @disk: disk to block events for
1717 * On return from this function, it is guaranteed that event checking
1718 * isn't in progress and won't happen until unblocked by
1719 * disk_unblock_events(). Events blocking is counted and the actual
1720 * unblocking happens after the matching number of unblocks are done.
1722 * Note that this intentionally does not block event checking from
1723 * disk_clear_events().
1728 void disk_block_events(struct gendisk *disk)
1730 struct disk_events *ev = disk->ev;
1731 unsigned long flags;
1738 * Outer mutex ensures that the first blocker completes canceling
1739 * the event work before further blockers are allowed to finish.
1741 mutex_lock(&ev->block_mutex);
1743 spin_lock_irqsave(&ev->lock, flags);
1744 cancel = !ev->block++;
1745 spin_unlock_irqrestore(&ev->lock, flags);
1748 cancel_delayed_work_sync(&disk->ev->dwork);
1750 mutex_unlock(&ev->block_mutex);
1753 static void __disk_unblock_events(struct gendisk *disk, bool check_now)
1755 struct disk_events *ev = disk->ev;
1757 unsigned long flags;
1759 spin_lock_irqsave(&ev->lock, flags);
1761 if (WARN_ON_ONCE(ev->block <= 0))
1767 intv = disk_events_poll_jiffies(disk);
1769 queue_delayed_work(system_freezable_power_efficient_wq,
1772 queue_delayed_work(system_freezable_power_efficient_wq,
1775 spin_unlock_irqrestore(&ev->lock, flags);
1779 * disk_unblock_events - unblock disk event checking
1780 * @disk: disk to unblock events for
1782 * Undo disk_block_events(). When the block count reaches zero, it
1783 * starts events polling if configured.
1786 * Don't care. Safe to call from irq context.
1788 void disk_unblock_events(struct gendisk *disk)
1791 __disk_unblock_events(disk, false);
1795 * disk_flush_events - schedule immediate event checking and flushing
1796 * @disk: disk to check and flush events for
1797 * @mask: events to flush
1799 * Schedule immediate event checking on @disk if not blocked. Events in
1800 * @mask are scheduled to be cleared from the driver. Note that this
1801 * doesn't clear the events from @disk->ev.
1804 * If @mask is non-zero must be called with bdev->bd_mutex held.
1806 void disk_flush_events(struct gendisk *disk, unsigned int mask)
1808 struct disk_events *ev = disk->ev;
1813 spin_lock_irq(&ev->lock);
1814 ev->clearing |= mask;
1816 mod_delayed_work(system_freezable_power_efficient_wq,
1818 spin_unlock_irq(&ev->lock);
1822 * disk_clear_events - synchronously check, clear and return pending events
1823 * @disk: disk to fetch and clear events from
1824 * @mask: mask of events to be fetched and cleared
1826 * Disk events are synchronously checked and pending events in @mask
1827 * are cleared and returned. This ignores the block count.
1832 static unsigned int disk_clear_events(struct gendisk *disk, unsigned int mask)
1834 struct disk_events *ev = disk->ev;
1835 unsigned int pending;
1836 unsigned int clearing = mask;
1841 disk_block_events(disk);
1844 * store the union of mask and ev->clearing on the stack so that the
1845 * race with disk_flush_events does not cause ambiguity (ev->clearing
1846 * can still be modified even if events are blocked).
1848 spin_lock_irq(&ev->lock);
1849 clearing |= ev->clearing;
1851 spin_unlock_irq(&ev->lock);
1853 disk_check_events(ev, &clearing);
1855 * if ev->clearing is not 0, the disk_flush_events got called in the
1856 * middle of this function, so we want to run the workfn without delay.
1858 __disk_unblock_events(disk, ev->clearing ? true : false);
1860 /* then, fetch and clear pending events */
1861 spin_lock_irq(&ev->lock);
1862 pending = ev->pending & mask;
1863 ev->pending &= ~mask;
1864 spin_unlock_irq(&ev->lock);
1865 WARN_ON_ONCE(clearing & mask);
1871 * bdev_check_media_change - check if a removable media has been changed
1872 * @bdev: block device to check
1874 * Check whether a removable media has been changed, and attempt to free all
1875 * dentries and inodes and invalidates all block device page cache entries in
1878 * Returns %true if the block device changed, or %false if not.
1880 bool bdev_check_media_change(struct block_device *bdev)
1882 unsigned int events;
1884 events = disk_clear_events(bdev->bd_disk, DISK_EVENT_MEDIA_CHANGE |
1885 DISK_EVENT_EJECT_REQUEST);
1886 if (!(events & DISK_EVENT_MEDIA_CHANGE))
1889 if (__invalidate_device(bdev, true))
1890 pr_warn("VFS: busy inodes on changed media %s\n",
1891 bdev->bd_disk->disk_name);
1892 set_bit(GD_NEED_PART_SCAN, &bdev->bd_disk->state);
1895 EXPORT_SYMBOL(bdev_check_media_change);
1898 * Separate this part out so that a different pointer for clearing_ptr can be
1899 * passed in for disk_clear_events.
1901 static void disk_events_workfn(struct work_struct *work)
1903 struct delayed_work *dwork = to_delayed_work(work);
1904 struct disk_events *ev = container_of(dwork, struct disk_events, dwork);
1906 disk_check_events(ev, &ev->clearing);
1909 static void disk_check_events(struct disk_events *ev,
1910 unsigned int *clearing_ptr)
1912 struct gendisk *disk = ev->disk;
1913 char *envp[ARRAY_SIZE(disk_uevents) + 1] = { };
1914 unsigned int clearing = *clearing_ptr;
1915 unsigned int events;
1917 int nr_events = 0, i;
1920 events = disk->fops->check_events(disk, clearing);
1922 /* accumulate pending events and schedule next poll if necessary */
1923 spin_lock_irq(&ev->lock);
1925 events &= ~ev->pending;
1926 ev->pending |= events;
1927 *clearing_ptr &= ~clearing;
1929 intv = disk_events_poll_jiffies(disk);
1930 if (!ev->block && intv)
1931 queue_delayed_work(system_freezable_power_efficient_wq,
1934 spin_unlock_irq(&ev->lock);
1937 * Tell userland about new events. Only the events listed in
1938 * @disk->events are reported, and only if DISK_EVENT_FLAG_UEVENT
1939 * is set. Otherwise, events are processed internally but never
1940 * get reported to userland.
1942 for (i = 0; i < ARRAY_SIZE(disk_uevents); i++)
1943 if ((events & disk->events & (1 << i)) &&
1944 (disk->event_flags & DISK_EVENT_FLAG_UEVENT))
1945 envp[nr_events++] = disk_uevents[i];
1948 kobject_uevent_env(&disk_to_dev(disk)->kobj, KOBJ_CHANGE, envp);
1952 * A disk events enabled device has the following sysfs nodes under
1953 * its /sys/block/X/ directory.
1955 * events : list of all supported events
1956 * events_async : list of events which can be detected w/o polling
1957 * (always empty, only for backwards compatibility)
1958 * events_poll_msecs : polling interval, 0: disable, -1: system default
1960 static ssize_t __disk_events_show(unsigned int events, char *buf)
1962 const char *delim = "";
1966 for (i = 0; i < ARRAY_SIZE(disk_events_strs); i++)
1967 if (events & (1 << i)) {
1968 pos += sprintf(buf + pos, "%s%s",
1969 delim, disk_events_strs[i]);
1973 pos += sprintf(buf + pos, "\n");
1977 static ssize_t disk_events_show(struct device *dev,
1978 struct device_attribute *attr, char *buf)
1980 struct gendisk *disk = dev_to_disk(dev);
1982 if (!(disk->event_flags & DISK_EVENT_FLAG_UEVENT))
1985 return __disk_events_show(disk->events, buf);
1988 static ssize_t disk_events_async_show(struct device *dev,
1989 struct device_attribute *attr, char *buf)
1994 static ssize_t disk_events_poll_msecs_show(struct device *dev,
1995 struct device_attribute *attr,
1998 struct gendisk *disk = dev_to_disk(dev);
2001 return sprintf(buf, "-1\n");
2003 return sprintf(buf, "%ld\n", disk->ev->poll_msecs);
2006 static ssize_t disk_events_poll_msecs_store(struct device *dev,
2007 struct device_attribute *attr,
2008 const char *buf, size_t count)
2010 struct gendisk *disk = dev_to_disk(dev);
2013 if (!count || !sscanf(buf, "%ld", &intv))
2016 if (intv < 0 && intv != -1)
2022 disk_block_events(disk);
2023 disk->ev->poll_msecs = intv;
2024 __disk_unblock_events(disk, true);
2029 static const DEVICE_ATTR(events, 0444, disk_events_show, NULL);
2030 static const DEVICE_ATTR(events_async, 0444, disk_events_async_show, NULL);
2031 static const DEVICE_ATTR(events_poll_msecs, 0644,
2032 disk_events_poll_msecs_show,
2033 disk_events_poll_msecs_store);
2035 static const struct attribute *disk_events_attrs[] = {
2036 &dev_attr_events.attr,
2037 &dev_attr_events_async.attr,
2038 &dev_attr_events_poll_msecs.attr,
2043 * The default polling interval can be specified by the kernel
2044 * parameter block.events_dfl_poll_msecs which defaults to 0
2045 * (disable). This can also be modified runtime by writing to
2046 * /sys/module/block/parameters/events_dfl_poll_msecs.
2048 static int disk_events_set_dfl_poll_msecs(const char *val,
2049 const struct kernel_param *kp)
2051 struct disk_events *ev;
2054 ret = param_set_ulong(val, kp);
2058 mutex_lock(&disk_events_mutex);
2060 list_for_each_entry(ev, &disk_events, node)
2061 disk_flush_events(ev->disk, 0);
2063 mutex_unlock(&disk_events_mutex);
2068 static const struct kernel_param_ops disk_events_dfl_poll_msecs_param_ops = {
2069 .set = disk_events_set_dfl_poll_msecs,
2070 .get = param_get_ulong,
2073 #undef MODULE_PARAM_PREFIX
2074 #define MODULE_PARAM_PREFIX "block."
2076 module_param_cb(events_dfl_poll_msecs, &disk_events_dfl_poll_msecs_param_ops,
2077 &disk_events_dfl_poll_msecs, 0644);
2080 * disk_{alloc|add|del|release}_events - initialize and destroy disk_events.
2082 static void disk_alloc_events(struct gendisk *disk)
2084 struct disk_events *ev;
2086 if (!disk->fops->check_events || !disk->events)
2089 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
2091 pr_warn("%s: failed to initialize events\n", disk->disk_name);
2095 INIT_LIST_HEAD(&ev->node);
2097 spin_lock_init(&ev->lock);
2098 mutex_init(&ev->block_mutex);
2100 ev->poll_msecs = -1;
2101 INIT_DELAYED_WORK(&ev->dwork, disk_events_workfn);
2106 static void disk_add_events(struct gendisk *disk)
2108 /* FIXME: error handling */
2109 if (sysfs_create_files(&disk_to_dev(disk)->kobj, disk_events_attrs) < 0)
2110 pr_warn("%s: failed to create sysfs files for events\n",
2116 mutex_lock(&disk_events_mutex);
2117 list_add_tail(&disk->ev->node, &disk_events);
2118 mutex_unlock(&disk_events_mutex);
2121 * Block count is initialized to 1 and the following initial
2122 * unblock kicks it into action.
2124 __disk_unblock_events(disk, true);
2127 static void disk_del_events(struct gendisk *disk)
2130 disk_block_events(disk);
2132 mutex_lock(&disk_events_mutex);
2133 list_del_init(&disk->ev->node);
2134 mutex_unlock(&disk_events_mutex);
2137 sysfs_remove_files(&disk_to_dev(disk)->kobj, disk_events_attrs);
2140 static void disk_release_events(struct gendisk *disk)
2142 /* the block count should be 1 from disk_del_events() */
2143 WARN_ON_ONCE(disk->ev && disk->ev->block != 1);