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 DECLARE_RWSEM(bdev_lookup_sem);
34 /* for extended dynamic devt allocation, currently only one major is used */
35 #define NR_EXT_DEVT (1 << MINORBITS)
36 static DEFINE_IDA(ext_devt_ida);
38 static void disk_check_events(struct disk_events *ev,
39 unsigned int *clearing_ptr);
40 static void disk_alloc_events(struct gendisk *disk);
41 static void disk_add_events(struct gendisk *disk);
42 static void disk_del_events(struct gendisk *disk);
43 static void disk_release_events(struct gendisk *disk);
45 void set_capacity(struct gendisk *disk, sector_t sectors)
47 struct block_device *bdev = disk->part0;
49 spin_lock(&bdev->bd_size_lock);
50 i_size_write(bdev->bd_inode, (loff_t)sectors << SECTOR_SHIFT);
51 spin_unlock(&bdev->bd_size_lock);
53 EXPORT_SYMBOL(set_capacity);
56 * Set disk capacity and notify if the size is not currently zero and will not
57 * be set to zero. Returns true if a uevent was sent, otherwise false.
59 bool set_capacity_and_notify(struct gendisk *disk, sector_t size)
61 sector_t capacity = get_capacity(disk);
62 char *envp[] = { "RESIZE=1", NULL };
64 set_capacity(disk, size);
67 * Only print a message and send a uevent if the gendisk is user visible
68 * and alive. This avoids spamming the log and udev when setting the
69 * initial capacity during probing.
71 if (size == capacity ||
72 (disk->flags & (GENHD_FL_UP | GENHD_FL_HIDDEN)) != GENHD_FL_UP)
75 pr_info("%s: detected capacity change from %lld to %lld\n",
76 disk->disk_name, size, capacity);
79 * Historically we did not send a uevent for changes to/from an empty
82 if (!capacity || !size)
84 kobject_uevent_env(&disk_to_dev(disk)->kobj, KOBJ_CHANGE, envp);
87 EXPORT_SYMBOL_GPL(set_capacity_and_notify);
90 * Format the device name of the indicated disk into the supplied buffer and
91 * return a pointer to that same buffer for convenience.
93 char *disk_name(struct gendisk *hd, int partno, char *buf)
96 snprintf(buf, BDEVNAME_SIZE, "%s", hd->disk_name);
97 else if (isdigit(hd->disk_name[strlen(hd->disk_name)-1]))
98 snprintf(buf, BDEVNAME_SIZE, "%sp%d", hd->disk_name, partno);
100 snprintf(buf, BDEVNAME_SIZE, "%s%d", hd->disk_name, partno);
105 const char *bdevname(struct block_device *bdev, char *buf)
107 return disk_name(bdev->bd_disk, bdev->bd_partno, buf);
109 EXPORT_SYMBOL(bdevname);
111 static void part_stat_read_all(struct block_device *part,
112 struct disk_stats *stat)
116 memset(stat, 0, sizeof(struct disk_stats));
117 for_each_possible_cpu(cpu) {
118 struct disk_stats *ptr = per_cpu_ptr(part->bd_stats, cpu);
121 for (group = 0; group < NR_STAT_GROUPS; group++) {
122 stat->nsecs[group] += ptr->nsecs[group];
123 stat->sectors[group] += ptr->sectors[group];
124 stat->ios[group] += ptr->ios[group];
125 stat->merges[group] += ptr->merges[group];
128 stat->io_ticks += ptr->io_ticks;
132 static unsigned int part_in_flight(struct block_device *part)
134 unsigned int inflight = 0;
137 for_each_possible_cpu(cpu) {
138 inflight += part_stat_local_read_cpu(part, in_flight[0], cpu) +
139 part_stat_local_read_cpu(part, in_flight[1], cpu);
141 if ((int)inflight < 0)
147 static void part_in_flight_rw(struct block_device *part,
148 unsigned int inflight[2])
154 for_each_possible_cpu(cpu) {
155 inflight[0] += part_stat_local_read_cpu(part, in_flight[0], cpu);
156 inflight[1] += part_stat_local_read_cpu(part, in_flight[1], cpu);
158 if ((int)inflight[0] < 0)
160 if ((int)inflight[1] < 0)
164 struct block_device *__disk_get_part(struct gendisk *disk, int partno)
166 struct disk_part_tbl *ptbl = rcu_dereference(disk->part_tbl);
168 if (unlikely(partno < 0 || partno >= ptbl->len))
170 return rcu_dereference(ptbl->part[partno]);
174 * disk_part_iter_init - initialize partition iterator
175 * @piter: iterator to initialize
176 * @disk: disk to iterate over
177 * @flags: DISK_PITER_* flags
179 * Initialize @piter so that it iterates over partitions of @disk.
184 void disk_part_iter_init(struct disk_part_iter *piter, struct gendisk *disk,
187 struct disk_part_tbl *ptbl;
190 ptbl = rcu_dereference(disk->part_tbl);
195 if (flags & DISK_PITER_REVERSE)
196 piter->idx = ptbl->len - 1;
197 else if (flags & (DISK_PITER_INCL_PART0 | DISK_PITER_INCL_EMPTY_PART0))
202 piter->flags = flags;
206 EXPORT_SYMBOL_GPL(disk_part_iter_init);
209 * disk_part_iter_next - proceed iterator to the next partition and return it
210 * @piter: iterator of interest
212 * Proceed @piter to the next partition and return it.
217 struct block_device *disk_part_iter_next(struct disk_part_iter *piter)
219 struct disk_part_tbl *ptbl;
222 /* put the last partition */
223 disk_part_iter_exit(piter);
227 ptbl = rcu_dereference(piter->disk->part_tbl);
229 /* determine iteration parameters */
230 if (piter->flags & DISK_PITER_REVERSE) {
232 if (piter->flags & (DISK_PITER_INCL_PART0 |
233 DISK_PITER_INCL_EMPTY_PART0))
242 /* iterate to the next partition */
243 for (; piter->idx != end; piter->idx += inc) {
244 struct block_device *part;
246 part = rcu_dereference(ptbl->part[piter->idx]);
249 if (!bdev_nr_sectors(part) &&
250 !(piter->flags & DISK_PITER_INCL_EMPTY) &&
251 !(piter->flags & DISK_PITER_INCL_EMPTY_PART0 &&
255 piter->part = bdgrab(part);
266 EXPORT_SYMBOL_GPL(disk_part_iter_next);
269 * disk_part_iter_exit - finish up partition iteration
270 * @piter: iter of interest
272 * Called when iteration is over. Cleans up @piter.
277 void disk_part_iter_exit(struct disk_part_iter *piter)
283 EXPORT_SYMBOL_GPL(disk_part_iter_exit);
285 static inline int sector_in_part(struct block_device *part, sector_t sector)
287 return part->bd_start_sect <= sector &&
288 sector < part->bd_start_sect + bdev_nr_sectors(part);
292 * disk_map_sector_rcu - map sector to partition
293 * @disk: gendisk of interest
294 * @sector: sector to map
296 * Find out which partition @sector maps to on @disk. This is
297 * primarily used for stats accounting.
303 * Found partition on success, part0 is returned if no partition matches
304 * or the matched partition is being deleted.
306 struct block_device *disk_map_sector_rcu(struct gendisk *disk, sector_t sector)
308 struct disk_part_tbl *ptbl;
309 struct block_device *part;
313 ptbl = rcu_dereference(disk->part_tbl);
315 part = rcu_dereference(ptbl->last_lookup);
316 if (part && sector_in_part(part, sector))
319 for (i = 1; i < ptbl->len; i++) {
320 part = rcu_dereference(ptbl->part[i]);
321 if (part && sector_in_part(part, sector)) {
322 rcu_assign_pointer(ptbl->last_lookup, part);
334 * disk_has_partitions
335 * @disk: gendisk of interest
337 * Walk through the partition table and check if valid partition exists.
343 * True if the gendisk has at least one valid non-zero size partition.
346 bool disk_has_partitions(struct gendisk *disk)
348 struct disk_part_tbl *ptbl;
353 ptbl = rcu_dereference(disk->part_tbl);
355 /* Iterate partitions skipping the whole device at index 0 */
356 for (i = 1; i < ptbl->len; i++) {
357 if (rcu_dereference(ptbl->part[i])) {
367 EXPORT_SYMBOL_GPL(disk_has_partitions);
370 * Can be deleted altogether. Later.
373 #define BLKDEV_MAJOR_HASH_SIZE 255
374 static struct blk_major_name {
375 struct blk_major_name *next;
378 void (*probe)(dev_t devt);
379 } *major_names[BLKDEV_MAJOR_HASH_SIZE];
380 static DEFINE_MUTEX(major_names_lock);
382 /* index in the above - for now: assume no multimajor ranges */
383 static inline int major_to_index(unsigned major)
385 return major % BLKDEV_MAJOR_HASH_SIZE;
388 #ifdef CONFIG_PROC_FS
389 void blkdev_show(struct seq_file *seqf, off_t offset)
391 struct blk_major_name *dp;
393 mutex_lock(&major_names_lock);
394 for (dp = major_names[major_to_index(offset)]; dp; dp = dp->next)
395 if (dp->major == offset)
396 seq_printf(seqf, "%3d %s\n", dp->major, dp->name);
397 mutex_unlock(&major_names_lock);
399 #endif /* CONFIG_PROC_FS */
402 * __register_blkdev - register a new block device
404 * @major: the requested major device number [1..BLKDEV_MAJOR_MAX-1]. If
405 * @major = 0, try to allocate any unused major number.
406 * @name: the name of the new block device as a zero terminated string
407 * @probe: allback that is called on access to any minor number of @major
409 * The @name must be unique within the system.
411 * The return value depends on the @major input parameter:
413 * - if a major device number was requested in range [1..BLKDEV_MAJOR_MAX-1]
414 * then the function returns zero on success, or a negative error code
415 * - if any unused major number was requested with @major = 0 parameter
416 * then the return value is the allocated major number in range
417 * [1..BLKDEV_MAJOR_MAX-1] or a negative error code otherwise
419 * See Documentation/admin-guide/devices.txt for the list of allocated
422 * Use register_blkdev instead for any new code.
424 int __register_blkdev(unsigned int major, const char *name,
425 void (*probe)(dev_t devt))
427 struct blk_major_name **n, *p;
430 mutex_lock(&major_names_lock);
434 for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) {
435 if (major_names[index] == NULL)
440 printk("%s: failed to get major for %s\n",
449 if (major >= BLKDEV_MAJOR_MAX) {
450 pr_err("%s: major requested (%u) is greater than the maximum (%u) for %s\n",
451 __func__, major, BLKDEV_MAJOR_MAX-1, name);
457 p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL);
465 strlcpy(p->name, name, sizeof(p->name));
467 index = major_to_index(major);
469 for (n = &major_names[index]; *n; n = &(*n)->next) {
470 if ((*n)->major == major)
479 printk("register_blkdev: cannot get major %u for %s\n",
484 mutex_unlock(&major_names_lock);
487 EXPORT_SYMBOL(__register_blkdev);
489 void unregister_blkdev(unsigned int major, const char *name)
491 struct blk_major_name **n;
492 struct blk_major_name *p = NULL;
493 int index = major_to_index(major);
495 mutex_lock(&major_names_lock);
496 for (n = &major_names[index]; *n; n = &(*n)->next)
497 if ((*n)->major == major)
499 if (!*n || strcmp((*n)->name, name)) {
505 mutex_unlock(&major_names_lock);
509 EXPORT_SYMBOL(unregister_blkdev);
512 * blk_mangle_minor - scatter minor numbers apart
513 * @minor: minor number to mangle
515 * Scatter consecutively allocated @minor number apart if MANGLE_DEVT
516 * is enabled. Mangling twice gives the original value.
524 static int blk_mangle_minor(int minor)
526 #ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT
529 for (i = 0; i < MINORBITS / 2; i++) {
530 int low = minor & (1 << i);
531 int high = minor & (1 << (MINORBITS - 1 - i));
532 int distance = MINORBITS - 1 - 2 * i;
534 minor ^= low | high; /* clear both bits */
535 low <<= distance; /* swap the positions */
537 minor |= low | high; /* and set */
544 * blk_alloc_devt - allocate a dev_t for a block device
545 * @bdev: block device to allocate dev_t for
546 * @devt: out parameter for resulting dev_t
548 * Allocate a dev_t for block device.
551 * 0 on success, allocated dev_t is returned in *@devt. -errno on
557 int blk_alloc_devt(struct block_device *bdev, dev_t *devt)
559 struct gendisk *disk = bdev->bd_disk;
562 /* in consecutive minor range? */
563 if (bdev->bd_partno < disk->minors) {
564 *devt = MKDEV(disk->major, disk->first_minor + bdev->bd_partno);
568 idx = ida_alloc_range(&ext_devt_ida, 0, NR_EXT_DEVT, GFP_KERNEL);
570 return idx == -ENOSPC ? -EBUSY : idx;
572 *devt = MKDEV(BLOCK_EXT_MAJOR, blk_mangle_minor(idx));
577 * blk_free_devt - free a dev_t
578 * @devt: dev_t to free
580 * Free @devt which was allocated using blk_alloc_devt().
585 void blk_free_devt(dev_t devt)
587 if (MAJOR(devt) == BLOCK_EXT_MAJOR)
588 ida_free(&ext_devt_ida, blk_mangle_minor(MINOR(devt)));
591 static char *bdevt_str(dev_t devt, char *buf)
593 if (MAJOR(devt) <= 0xff && MINOR(devt) <= 0xff) {
594 char tbuf[BDEVT_SIZE];
595 snprintf(tbuf, BDEVT_SIZE, "%02x%02x", MAJOR(devt), MINOR(devt));
596 snprintf(buf, BDEVT_SIZE, "%-9s", tbuf);
598 snprintf(buf, BDEVT_SIZE, "%03x:%05x", MAJOR(devt), MINOR(devt));
603 static void disk_scan_partitions(struct gendisk *disk)
605 struct block_device *bdev;
607 if (!get_capacity(disk) || !disk_part_scan_enabled(disk))
610 set_bit(GD_NEED_PART_SCAN, &disk->state);
611 bdev = blkdev_get_by_dev(disk_devt(disk), FMODE_READ, NULL);
613 blkdev_put(bdev, FMODE_READ);
616 static void register_disk(struct device *parent, struct gendisk *disk,
617 const struct attribute_group **groups)
619 struct device *ddev = disk_to_dev(disk);
620 struct disk_part_iter piter;
621 struct block_device *part;
624 ddev->parent = parent;
626 dev_set_name(ddev, "%s", disk->disk_name);
628 /* delay uevents, until we scanned partition table */
629 dev_set_uevent_suppress(ddev, 1);
632 WARN_ON(ddev->groups);
633 ddev->groups = groups;
635 if (device_add(ddev))
637 if (!sysfs_deprecated) {
638 err = sysfs_create_link(block_depr, &ddev->kobj,
639 kobject_name(&ddev->kobj));
647 * avoid probable deadlock caused by allocating memory with
648 * GFP_KERNEL in runtime_resume callback of its all ancestor
651 pm_runtime_set_memalloc_noio(ddev, true);
653 disk->part0->bd_holder_dir =
654 kobject_create_and_add("holders", &ddev->kobj);
655 disk->slave_dir = kobject_create_and_add("slaves", &ddev->kobj);
657 if (disk->flags & GENHD_FL_HIDDEN) {
658 dev_set_uevent_suppress(ddev, 0);
662 disk_scan_partitions(disk);
664 /* announce disk after possible partitions are created */
665 dev_set_uevent_suppress(ddev, 0);
666 kobject_uevent(&ddev->kobj, KOBJ_ADD);
668 /* announce possible partitions */
669 disk_part_iter_init(&piter, disk, 0);
670 while ((part = disk_part_iter_next(&piter)))
671 kobject_uevent(bdev_kobj(part), KOBJ_ADD);
672 disk_part_iter_exit(&piter);
674 if (disk->queue->backing_dev_info->dev) {
675 err = sysfs_create_link(&ddev->kobj,
676 &disk->queue->backing_dev_info->dev->kobj,
683 * __device_add_disk - add disk information to kernel list
684 * @parent: parent device for the disk
685 * @disk: per-device partitioning information
686 * @groups: Additional per-device sysfs groups
687 * @register_queue: register the queue if set to true
689 * This function registers the partitioning information in @disk
692 * FIXME: error handling
694 static void __device_add_disk(struct device *parent, struct gendisk *disk,
695 const struct attribute_group **groups,
702 * The disk queue should now be all set with enough information about
703 * the device for the elevator code to pick an adequate default
704 * elevator if one is needed, that is, for devices requesting queue
708 elevator_init_mq(disk->queue);
710 /* minors == 0 indicates to use ext devt from part0 and should
711 * be accompanied with EXT_DEVT flag. Make sure all
712 * parameters make sense.
714 WARN_ON(disk->minors && !(disk->major || disk->first_minor));
715 WARN_ON(!disk->minors &&
716 !(disk->flags & (GENHD_FL_EXT_DEVT | GENHD_FL_HIDDEN)));
718 disk->flags |= GENHD_FL_UP;
720 retval = blk_alloc_devt(disk->part0, &devt);
725 disk->major = MAJOR(devt);
726 disk->first_minor = MINOR(devt);
728 disk_alloc_events(disk);
730 if (disk->flags & GENHD_FL_HIDDEN) {
732 * Don't let hidden disks show up in /proc/partitions,
733 * and don't bother scanning for partitions either.
735 disk->flags |= GENHD_FL_SUPPRESS_PARTITION_INFO;
736 disk->flags |= GENHD_FL_NO_PART_SCAN;
738 struct backing_dev_info *bdi = disk->queue->backing_dev_info;
739 struct device *dev = disk_to_dev(disk);
742 /* Register BDI before referencing it from bdev */
744 ret = bdi_register(bdi, "%u:%u", MAJOR(devt), MINOR(devt));
746 bdi_set_owner(bdi, dev);
747 bdev_add(disk->part0, devt);
749 register_disk(parent, disk, groups);
751 blk_register_queue(disk);
754 * Take an extra ref on queue which will be put on disk_release()
755 * so that it sticks around as long as @disk is there.
757 WARN_ON_ONCE(!blk_get_queue(disk->queue));
759 disk_add_events(disk);
760 blk_integrity_add(disk);
763 void device_add_disk(struct device *parent, struct gendisk *disk,
764 const struct attribute_group **groups)
767 __device_add_disk(parent, disk, groups, true);
769 EXPORT_SYMBOL(device_add_disk);
771 void device_add_disk_no_queue_reg(struct device *parent, struct gendisk *disk)
773 __device_add_disk(parent, disk, NULL, false);
775 EXPORT_SYMBOL(device_add_disk_no_queue_reg);
777 static void invalidate_partition(struct block_device *bdev)
780 __invalidate_device(bdev, true);
783 * Unhash the bdev inode for this device so that it can't be looked
784 * up any more even if openers still hold references to it.
786 remove_inode_hash(bdev->bd_inode);
790 * del_gendisk - remove the gendisk
791 * @disk: the struct gendisk to remove
793 * Removes the gendisk and all its associated resources. This deletes the
794 * partitions associated with the gendisk, and unregisters the associated
797 * This is the counter to the respective __device_add_disk() call.
799 * The final removal of the struct gendisk happens when its refcount reaches 0
800 * with put_disk(), which should be called after del_gendisk(), if
801 * __device_add_disk() was used.
803 * Drivers exist which depend on the release of the gendisk to be synchronous,
804 * it should not be deferred.
808 void del_gendisk(struct gendisk *disk)
810 struct disk_part_iter piter;
811 struct block_device *part;
815 if (WARN_ON_ONCE(!disk->queue))
818 blk_integrity_del(disk);
819 disk_del_events(disk);
822 * Block lookups of the disk until all bdevs are unhashed and the
823 * disk is marked as dead (GENHD_FL_UP cleared).
825 down_write(&bdev_lookup_sem);
827 /* invalidate stuff */
828 disk_part_iter_init(&piter, disk,
829 DISK_PITER_INCL_EMPTY | DISK_PITER_REVERSE);
830 while ((part = disk_part_iter_next(&piter))) {
831 invalidate_partition(part);
832 delete_partition(part);
834 disk_part_iter_exit(&piter);
836 invalidate_partition(disk->part0);
837 set_capacity(disk, 0);
838 disk->flags &= ~GENHD_FL_UP;
839 up_write(&bdev_lookup_sem);
841 if (!(disk->flags & GENHD_FL_HIDDEN)) {
842 sysfs_remove_link(&disk_to_dev(disk)->kobj, "bdi");
845 * Unregister bdi before releasing device numbers (as they can
846 * get reused and we'd get clashes in sysfs).
848 bdi_unregister(disk->queue->backing_dev_info);
851 blk_unregister_queue(disk);
853 kobject_put(disk->part0->bd_holder_dir);
854 kobject_put(disk->slave_dir);
856 part_stat_set_all(disk->part0, 0);
857 disk->part0->bd_stamp = 0;
858 if (!sysfs_deprecated)
859 sysfs_remove_link(block_depr, dev_name(disk_to_dev(disk)));
860 pm_runtime_set_memalloc_noio(disk_to_dev(disk), false);
861 device_del(disk_to_dev(disk));
863 EXPORT_SYMBOL(del_gendisk);
865 /* sysfs access to bad-blocks list. */
866 static ssize_t disk_badblocks_show(struct device *dev,
867 struct device_attribute *attr,
870 struct gendisk *disk = dev_to_disk(dev);
873 return sprintf(page, "\n");
875 return badblocks_show(disk->bb, page, 0);
878 static ssize_t disk_badblocks_store(struct device *dev,
879 struct device_attribute *attr,
880 const char *page, size_t len)
882 struct gendisk *disk = dev_to_disk(dev);
887 return badblocks_store(disk->bb, page, len, 0);
890 void blk_request_module(dev_t devt)
892 unsigned int major = MAJOR(devt);
893 struct blk_major_name **n;
895 mutex_lock(&major_names_lock);
896 for (n = &major_names[major_to_index(major)]; *n; n = &(*n)->next) {
897 if ((*n)->major == major && (*n)->probe) {
899 mutex_unlock(&major_names_lock);
903 mutex_unlock(&major_names_lock);
905 if (request_module("block-major-%d-%d", MAJOR(devt), MINOR(devt)) > 0)
906 /* Make old-style 2.4 aliases work */
907 request_module("block-major-%d", MAJOR(devt));
911 * bdget_disk - do bdget() by gendisk and partition number
912 * @disk: gendisk of interest
913 * @partno: partition number
915 * Find partition @partno from @disk, do bdget() on it.
921 * Resulting block_device on success, NULL on failure.
923 struct block_device *bdget_disk(struct gendisk *disk, int partno)
925 struct block_device *bdev = NULL;
928 bdev = __disk_get_part(disk, partno);
929 if (bdev && !bdgrab(bdev))
937 * print a full list of all partitions - intended for places where the root
938 * filesystem can't be mounted and thus to give the victim some idea of what
941 void __init printk_all_partitions(void)
943 struct class_dev_iter iter;
946 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
947 while ((dev = class_dev_iter_next(&iter))) {
948 struct gendisk *disk = dev_to_disk(dev);
949 struct disk_part_iter piter;
950 struct block_device *part;
951 char name_buf[BDEVNAME_SIZE];
952 char devt_buf[BDEVT_SIZE];
955 * Don't show empty devices or things that have been
958 if (get_capacity(disk) == 0 ||
959 (disk->flags & GENHD_FL_SUPPRESS_PARTITION_INFO))
963 * Note, unlike /proc/partitions, I am showing the
964 * numbers in hex - the same format as the root=
967 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
968 while ((part = disk_part_iter_next(&piter))) {
969 bool is_part0 = part == disk->part0;
971 printk("%s%s %10llu %s %s", is_part0 ? "" : " ",
972 bdevt_str(part->bd_dev, devt_buf),
973 bdev_nr_sectors(part) >> 1,
974 disk_name(disk, part->bd_partno, name_buf),
976 part->bd_meta_info->uuid : "");
978 if (dev->parent && dev->parent->driver)
979 printk(" driver: %s\n",
980 dev->parent->driver->name);
982 printk(" (driver?)\n");
986 disk_part_iter_exit(&piter);
988 class_dev_iter_exit(&iter);
991 #ifdef CONFIG_PROC_FS
993 static void *disk_seqf_start(struct seq_file *seqf, loff_t *pos)
996 struct class_dev_iter *iter;
999 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
1001 return ERR_PTR(-ENOMEM);
1003 seqf->private = iter;
1004 class_dev_iter_init(iter, &block_class, NULL, &disk_type);
1006 dev = class_dev_iter_next(iter);
1011 return dev_to_disk(dev);
1014 static void *disk_seqf_next(struct seq_file *seqf, void *v, loff_t *pos)
1019 dev = class_dev_iter_next(seqf->private);
1021 return dev_to_disk(dev);
1026 static void disk_seqf_stop(struct seq_file *seqf, void *v)
1028 struct class_dev_iter *iter = seqf->private;
1030 /* stop is called even after start failed :-( */
1032 class_dev_iter_exit(iter);
1034 seqf->private = NULL;
1038 static void *show_partition_start(struct seq_file *seqf, loff_t *pos)
1042 p = disk_seqf_start(seqf, pos);
1043 if (!IS_ERR_OR_NULL(p) && !*pos)
1044 seq_puts(seqf, "major minor #blocks name\n\n");
1048 static int show_partition(struct seq_file *seqf, void *v)
1050 struct gendisk *sgp = v;
1051 struct disk_part_iter piter;
1052 struct block_device *part;
1053 char buf[BDEVNAME_SIZE];
1055 /* Don't show non-partitionable removeable devices or empty devices */
1056 if (!get_capacity(sgp) || (!disk_max_parts(sgp) &&
1057 (sgp->flags & GENHD_FL_REMOVABLE)))
1059 if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)
1062 /* show the full disk and all non-0 size partitions of it */
1063 disk_part_iter_init(&piter, sgp, DISK_PITER_INCL_PART0);
1064 while ((part = disk_part_iter_next(&piter)))
1065 seq_printf(seqf, "%4d %7d %10llu %s\n",
1066 MAJOR(part->bd_dev), MINOR(part->bd_dev),
1067 bdev_nr_sectors(part) >> 1,
1068 disk_name(sgp, part->bd_partno, buf));
1069 disk_part_iter_exit(&piter);
1074 static const struct seq_operations partitions_op = {
1075 .start = show_partition_start,
1076 .next = disk_seqf_next,
1077 .stop = disk_seqf_stop,
1078 .show = show_partition
1082 static int __init genhd_device_init(void)
1086 block_class.dev_kobj = sysfs_dev_block_kobj;
1087 error = class_register(&block_class);
1088 if (unlikely(error))
1092 register_blkdev(BLOCK_EXT_MAJOR, "blkext");
1094 /* create top-level block dir */
1095 if (!sysfs_deprecated)
1096 block_depr = kobject_create_and_add("block", NULL);
1100 subsys_initcall(genhd_device_init);
1102 static ssize_t disk_range_show(struct device *dev,
1103 struct device_attribute *attr, char *buf)
1105 struct gendisk *disk = dev_to_disk(dev);
1107 return sprintf(buf, "%d\n", disk->minors);
1110 static ssize_t disk_ext_range_show(struct device *dev,
1111 struct device_attribute *attr, char *buf)
1113 struct gendisk *disk = dev_to_disk(dev);
1115 return sprintf(buf, "%d\n", disk_max_parts(disk));
1118 static ssize_t disk_removable_show(struct device *dev,
1119 struct device_attribute *attr, char *buf)
1121 struct gendisk *disk = dev_to_disk(dev);
1123 return sprintf(buf, "%d\n",
1124 (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
1127 static ssize_t disk_hidden_show(struct device *dev,
1128 struct device_attribute *attr, char *buf)
1130 struct gendisk *disk = dev_to_disk(dev);
1132 return sprintf(buf, "%d\n",
1133 (disk->flags & GENHD_FL_HIDDEN ? 1 : 0));
1136 static ssize_t disk_ro_show(struct device *dev,
1137 struct device_attribute *attr, char *buf)
1139 struct gendisk *disk = dev_to_disk(dev);
1141 return sprintf(buf, "%d\n", get_disk_ro(disk) ? 1 : 0);
1144 ssize_t part_size_show(struct device *dev,
1145 struct device_attribute *attr, char *buf)
1147 return sprintf(buf, "%llu\n", bdev_nr_sectors(dev_to_bdev(dev)));
1150 ssize_t part_stat_show(struct device *dev,
1151 struct device_attribute *attr, char *buf)
1153 struct block_device *bdev = dev_to_bdev(dev);
1154 struct request_queue *q = bdev->bd_disk->queue;
1155 struct disk_stats stat;
1156 unsigned int inflight;
1158 part_stat_read_all(bdev, &stat);
1160 inflight = blk_mq_in_flight(q, bdev);
1162 inflight = part_in_flight(bdev);
1165 "%8lu %8lu %8llu %8u "
1166 "%8lu %8lu %8llu %8u "
1168 "%8lu %8lu %8llu %8u "
1171 stat.ios[STAT_READ],
1172 stat.merges[STAT_READ],
1173 (unsigned long long)stat.sectors[STAT_READ],
1174 (unsigned int)div_u64(stat.nsecs[STAT_READ], NSEC_PER_MSEC),
1175 stat.ios[STAT_WRITE],
1176 stat.merges[STAT_WRITE],
1177 (unsigned long long)stat.sectors[STAT_WRITE],
1178 (unsigned int)div_u64(stat.nsecs[STAT_WRITE], NSEC_PER_MSEC),
1180 jiffies_to_msecs(stat.io_ticks),
1181 (unsigned int)div_u64(stat.nsecs[STAT_READ] +
1182 stat.nsecs[STAT_WRITE] +
1183 stat.nsecs[STAT_DISCARD] +
1184 stat.nsecs[STAT_FLUSH],
1186 stat.ios[STAT_DISCARD],
1187 stat.merges[STAT_DISCARD],
1188 (unsigned long long)stat.sectors[STAT_DISCARD],
1189 (unsigned int)div_u64(stat.nsecs[STAT_DISCARD], NSEC_PER_MSEC),
1190 stat.ios[STAT_FLUSH],
1191 (unsigned int)div_u64(stat.nsecs[STAT_FLUSH], NSEC_PER_MSEC));
1194 ssize_t part_inflight_show(struct device *dev, struct device_attribute *attr,
1197 struct block_device *bdev = dev_to_bdev(dev);
1198 struct request_queue *q = bdev->bd_disk->queue;
1199 unsigned int inflight[2];
1202 blk_mq_in_flight_rw(q, bdev, inflight);
1204 part_in_flight_rw(bdev, inflight);
1206 return sprintf(buf, "%8u %8u\n", inflight[0], inflight[1]);
1209 static ssize_t disk_capability_show(struct device *dev,
1210 struct device_attribute *attr, char *buf)
1212 struct gendisk *disk = dev_to_disk(dev);
1214 return sprintf(buf, "%x\n", disk->flags);
1217 static ssize_t disk_alignment_offset_show(struct device *dev,
1218 struct device_attribute *attr,
1221 struct gendisk *disk = dev_to_disk(dev);
1223 return sprintf(buf, "%d\n", queue_alignment_offset(disk->queue));
1226 static ssize_t disk_discard_alignment_show(struct device *dev,
1227 struct device_attribute *attr,
1230 struct gendisk *disk = dev_to_disk(dev);
1232 return sprintf(buf, "%d\n", queue_discard_alignment(disk->queue));
1235 static DEVICE_ATTR(range, 0444, disk_range_show, NULL);
1236 static DEVICE_ATTR(ext_range, 0444, disk_ext_range_show, NULL);
1237 static DEVICE_ATTR(removable, 0444, disk_removable_show, NULL);
1238 static DEVICE_ATTR(hidden, 0444, disk_hidden_show, NULL);
1239 static DEVICE_ATTR(ro, 0444, disk_ro_show, NULL);
1240 static DEVICE_ATTR(size, 0444, part_size_show, NULL);
1241 static DEVICE_ATTR(alignment_offset, 0444, disk_alignment_offset_show, NULL);
1242 static DEVICE_ATTR(discard_alignment, 0444, disk_discard_alignment_show, NULL);
1243 static DEVICE_ATTR(capability, 0444, disk_capability_show, NULL);
1244 static DEVICE_ATTR(stat, 0444, part_stat_show, NULL);
1245 static DEVICE_ATTR(inflight, 0444, part_inflight_show, NULL);
1246 static DEVICE_ATTR(badblocks, 0644, disk_badblocks_show, disk_badblocks_store);
1248 #ifdef CONFIG_FAIL_MAKE_REQUEST
1249 ssize_t part_fail_show(struct device *dev,
1250 struct device_attribute *attr, char *buf)
1252 return sprintf(buf, "%d\n", dev_to_bdev(dev)->bd_make_it_fail);
1255 ssize_t part_fail_store(struct device *dev,
1256 struct device_attribute *attr,
1257 const char *buf, size_t count)
1261 if (count > 0 && sscanf(buf, "%d", &i) > 0)
1262 dev_to_bdev(dev)->bd_make_it_fail = i;
1267 static struct device_attribute dev_attr_fail =
1268 __ATTR(make-it-fail, 0644, part_fail_show, part_fail_store);
1269 #endif /* CONFIG_FAIL_MAKE_REQUEST */
1271 #ifdef CONFIG_FAIL_IO_TIMEOUT
1272 static struct device_attribute dev_attr_fail_timeout =
1273 __ATTR(io-timeout-fail, 0644, part_timeout_show, part_timeout_store);
1276 static struct attribute *disk_attrs[] = {
1277 &dev_attr_range.attr,
1278 &dev_attr_ext_range.attr,
1279 &dev_attr_removable.attr,
1280 &dev_attr_hidden.attr,
1282 &dev_attr_size.attr,
1283 &dev_attr_alignment_offset.attr,
1284 &dev_attr_discard_alignment.attr,
1285 &dev_attr_capability.attr,
1286 &dev_attr_stat.attr,
1287 &dev_attr_inflight.attr,
1288 &dev_attr_badblocks.attr,
1289 #ifdef CONFIG_FAIL_MAKE_REQUEST
1290 &dev_attr_fail.attr,
1292 #ifdef CONFIG_FAIL_IO_TIMEOUT
1293 &dev_attr_fail_timeout.attr,
1298 static umode_t disk_visible(struct kobject *kobj, struct attribute *a, int n)
1300 struct device *dev = container_of(kobj, typeof(*dev), kobj);
1301 struct gendisk *disk = dev_to_disk(dev);
1303 if (a == &dev_attr_badblocks.attr && !disk->bb)
1308 static struct attribute_group disk_attr_group = {
1309 .attrs = disk_attrs,
1310 .is_visible = disk_visible,
1313 static const struct attribute_group *disk_attr_groups[] = {
1319 * disk_replace_part_tbl - replace disk->part_tbl in RCU-safe way
1320 * @disk: disk to replace part_tbl for
1321 * @new_ptbl: new part_tbl to install
1323 * Replace disk->part_tbl with @new_ptbl in RCU-safe way. The
1324 * original ptbl is freed using RCU callback.
1327 * Matching bd_mutex locked or the caller is the only user of @disk.
1329 static void disk_replace_part_tbl(struct gendisk *disk,
1330 struct disk_part_tbl *new_ptbl)
1332 struct disk_part_tbl *old_ptbl =
1333 rcu_dereference_protected(disk->part_tbl, 1);
1335 rcu_assign_pointer(disk->part_tbl, new_ptbl);
1338 rcu_assign_pointer(old_ptbl->last_lookup, NULL);
1339 kfree_rcu(old_ptbl, rcu_head);
1344 * disk_expand_part_tbl - expand disk->part_tbl
1345 * @disk: disk to expand part_tbl for
1346 * @partno: expand such that this partno can fit in
1348 * Expand disk->part_tbl such that @partno can fit in. disk->part_tbl
1349 * uses RCU to allow unlocked dereferencing for stats and other stuff.
1352 * Matching bd_mutex locked or the caller is the only user of @disk.
1356 * 0 on success, -errno on failure.
1358 int disk_expand_part_tbl(struct gendisk *disk, int partno)
1360 struct disk_part_tbl *old_ptbl =
1361 rcu_dereference_protected(disk->part_tbl, 1);
1362 struct disk_part_tbl *new_ptbl;
1363 int len = old_ptbl ? old_ptbl->len : 0;
1367 * check for int overflow, since we can get here from blkpg_ioctl()
1368 * with a user passed 'partno'.
1370 target = partno + 1;
1374 /* disk_max_parts() is zero during initialization, ignore if so */
1375 if (disk_max_parts(disk) && target > disk_max_parts(disk))
1381 new_ptbl = kzalloc_node(struct_size(new_ptbl, part, target), GFP_KERNEL,
1386 new_ptbl->len = target;
1388 for (i = 0; i < len; i++)
1389 rcu_assign_pointer(new_ptbl->part[i], old_ptbl->part[i]);
1391 disk_replace_part_tbl(disk, new_ptbl);
1396 * disk_release - releases all allocated resources of the gendisk
1397 * @dev: the device representing this disk
1399 * This function releases all allocated resources of the gendisk.
1401 * Drivers which used __device_add_disk() have a gendisk with a request_queue
1402 * assigned. Since the request_queue sits on top of the gendisk for these
1403 * drivers we also call blk_put_queue() for them, and we expect the
1404 * request_queue refcount to reach 0 at this point, and so the request_queue
1405 * will also be freed prior to the disk.
1407 * Context: can sleep
1409 static void disk_release(struct device *dev)
1411 struct gendisk *disk = dev_to_disk(dev);
1415 blk_free_devt(dev->devt);
1416 disk_release_events(disk);
1417 kfree(disk->random);
1418 disk_replace_part_tbl(disk, NULL);
1421 blk_put_queue(disk->queue);
1424 struct class block_class = {
1428 static char *block_devnode(struct device *dev, umode_t *mode,
1429 kuid_t *uid, kgid_t *gid)
1431 struct gendisk *disk = dev_to_disk(dev);
1433 if (disk->fops->devnode)
1434 return disk->fops->devnode(disk, mode);
1438 const struct device_type disk_type = {
1440 .groups = disk_attr_groups,
1441 .release = disk_release,
1442 .devnode = block_devnode,
1445 #ifdef CONFIG_PROC_FS
1447 * aggregate disk stat collector. Uses the same stats that the sysfs
1448 * entries do, above, but makes them available through one seq_file.
1450 * The output looks suspiciously like /proc/partitions with a bunch of
1453 static int diskstats_show(struct seq_file *seqf, void *v)
1455 struct gendisk *gp = v;
1456 struct disk_part_iter piter;
1457 struct block_device *hd;
1458 char buf[BDEVNAME_SIZE];
1459 unsigned int inflight;
1460 struct disk_stats stat;
1463 if (&disk_to_dev(gp)->kobj.entry == block_class.devices.next)
1464 seq_puts(seqf, "major minor name"
1465 " rio rmerge rsect ruse wio wmerge "
1466 "wsect wuse running use aveq"
1470 disk_part_iter_init(&piter, gp, DISK_PITER_INCL_EMPTY_PART0);
1471 while ((hd = disk_part_iter_next(&piter))) {
1472 part_stat_read_all(hd, &stat);
1473 if (queue_is_mq(gp->queue))
1474 inflight = blk_mq_in_flight(gp->queue, hd);
1476 inflight = part_in_flight(hd);
1478 seq_printf(seqf, "%4d %7d %s "
1485 MAJOR(hd->bd_dev), MINOR(hd->bd_dev),
1486 disk_name(gp, hd->bd_partno, buf),
1487 stat.ios[STAT_READ],
1488 stat.merges[STAT_READ],
1489 stat.sectors[STAT_READ],
1490 (unsigned int)div_u64(stat.nsecs[STAT_READ],
1492 stat.ios[STAT_WRITE],
1493 stat.merges[STAT_WRITE],
1494 stat.sectors[STAT_WRITE],
1495 (unsigned int)div_u64(stat.nsecs[STAT_WRITE],
1498 jiffies_to_msecs(stat.io_ticks),
1499 (unsigned int)div_u64(stat.nsecs[STAT_READ] +
1500 stat.nsecs[STAT_WRITE] +
1501 stat.nsecs[STAT_DISCARD] +
1502 stat.nsecs[STAT_FLUSH],
1504 stat.ios[STAT_DISCARD],
1505 stat.merges[STAT_DISCARD],
1506 stat.sectors[STAT_DISCARD],
1507 (unsigned int)div_u64(stat.nsecs[STAT_DISCARD],
1509 stat.ios[STAT_FLUSH],
1510 (unsigned int)div_u64(stat.nsecs[STAT_FLUSH],
1514 disk_part_iter_exit(&piter);
1519 static const struct seq_operations diskstats_op = {
1520 .start = disk_seqf_start,
1521 .next = disk_seqf_next,
1522 .stop = disk_seqf_stop,
1523 .show = diskstats_show
1526 static int __init proc_genhd_init(void)
1528 proc_create_seq("diskstats", 0, NULL, &diskstats_op);
1529 proc_create_seq("partitions", 0, NULL, &partitions_op);
1532 module_init(proc_genhd_init);
1533 #endif /* CONFIG_PROC_FS */
1535 dev_t blk_lookup_devt(const char *name, int partno)
1537 dev_t devt = MKDEV(0, 0);
1538 struct class_dev_iter iter;
1541 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
1542 while ((dev = class_dev_iter_next(&iter))) {
1543 struct gendisk *disk = dev_to_disk(dev);
1544 struct block_device *part;
1546 if (strcmp(dev_name(dev), name))
1549 if (partno < disk->minors) {
1550 /* We need to return the right devno, even
1551 * if the partition doesn't exist yet.
1553 devt = MKDEV(MAJOR(dev->devt),
1554 MINOR(dev->devt) + partno);
1557 part = bdget_disk(disk, partno);
1559 devt = part->bd_dev;
1564 class_dev_iter_exit(&iter);
1568 struct gendisk *__alloc_disk_node(int minors, int node_id)
1570 struct gendisk *disk;
1571 struct disk_part_tbl *ptbl;
1573 if (minors > DISK_MAX_PARTS) {
1575 "block: can't allocate more than %d partitions\n",
1577 minors = DISK_MAX_PARTS;
1580 disk = kzalloc_node(sizeof(struct gendisk), GFP_KERNEL, node_id);
1584 disk->part0 = bdev_alloc(disk, 0);
1588 disk->node_id = node_id;
1589 if (disk_expand_part_tbl(disk, 0))
1592 ptbl = rcu_dereference_protected(disk->part_tbl, 1);
1593 rcu_assign_pointer(ptbl->part[0], disk->part0);
1595 disk->minors = minors;
1596 rand_initialize_disk(disk);
1597 disk_to_dev(disk)->class = &block_class;
1598 disk_to_dev(disk)->type = &disk_type;
1599 device_initialize(disk_to_dev(disk));
1608 EXPORT_SYMBOL(__alloc_disk_node);
1611 * put_disk - decrements the gendisk refcount
1612 * @disk: the struct gendisk to decrement the refcount for
1614 * This decrements the refcount for the struct gendisk. When this reaches 0
1615 * we'll have disk_release() called.
1617 * Context: Any context, but the last reference must not be dropped from
1620 void put_disk(struct gendisk *disk)
1623 put_device(disk_to_dev(disk));
1625 EXPORT_SYMBOL(put_disk);
1627 static void set_disk_ro_uevent(struct gendisk *gd, int ro)
1629 char event[] = "DISK_RO=1";
1630 char *envp[] = { event, NULL };
1634 kobject_uevent_env(&disk_to_dev(gd)->kobj, KOBJ_CHANGE, envp);
1637 void set_disk_ro(struct gendisk *disk, int flag)
1639 struct disk_part_iter piter;
1640 struct block_device *part;
1642 if (disk->part0->bd_read_only != flag) {
1643 set_disk_ro_uevent(disk, flag);
1644 disk->part0->bd_read_only = flag;
1647 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_EMPTY);
1648 while ((part = disk_part_iter_next(&piter)))
1649 part->bd_read_only = flag;
1650 disk_part_iter_exit(&piter);
1653 EXPORT_SYMBOL(set_disk_ro);
1655 int bdev_read_only(struct block_device *bdev)
1659 return bdev->bd_read_only;
1662 EXPORT_SYMBOL(bdev_read_only);
1665 * Disk events - monitor disk events like media change and eject request.
1667 struct disk_events {
1668 struct list_head node; /* all disk_event's */
1669 struct gendisk *disk; /* the associated disk */
1672 struct mutex block_mutex; /* protects blocking */
1673 int block; /* event blocking depth */
1674 unsigned int pending; /* events already sent out */
1675 unsigned int clearing; /* events being cleared */
1677 long poll_msecs; /* interval, -1 for default */
1678 struct delayed_work dwork;
1681 static const char *disk_events_strs[] = {
1682 [ilog2(DISK_EVENT_MEDIA_CHANGE)] = "media_change",
1683 [ilog2(DISK_EVENT_EJECT_REQUEST)] = "eject_request",
1686 static char *disk_uevents[] = {
1687 [ilog2(DISK_EVENT_MEDIA_CHANGE)] = "DISK_MEDIA_CHANGE=1",
1688 [ilog2(DISK_EVENT_EJECT_REQUEST)] = "DISK_EJECT_REQUEST=1",
1691 /* list of all disk_events */
1692 static DEFINE_MUTEX(disk_events_mutex);
1693 static LIST_HEAD(disk_events);
1695 /* disable in-kernel polling by default */
1696 static unsigned long disk_events_dfl_poll_msecs;
1698 static unsigned long disk_events_poll_jiffies(struct gendisk *disk)
1700 struct disk_events *ev = disk->ev;
1701 long intv_msecs = 0;
1704 * If device-specific poll interval is set, always use it. If
1705 * the default is being used, poll if the POLL flag is set.
1707 if (ev->poll_msecs >= 0)
1708 intv_msecs = ev->poll_msecs;
1709 else if (disk->event_flags & DISK_EVENT_FLAG_POLL)
1710 intv_msecs = disk_events_dfl_poll_msecs;
1712 return msecs_to_jiffies(intv_msecs);
1716 * disk_block_events - block and flush disk event checking
1717 * @disk: disk to block events for
1719 * On return from this function, it is guaranteed that event checking
1720 * isn't in progress and won't happen until unblocked by
1721 * disk_unblock_events(). Events blocking is counted and the actual
1722 * unblocking happens after the matching number of unblocks are done.
1724 * Note that this intentionally does not block event checking from
1725 * disk_clear_events().
1730 void disk_block_events(struct gendisk *disk)
1732 struct disk_events *ev = disk->ev;
1733 unsigned long flags;
1740 * Outer mutex ensures that the first blocker completes canceling
1741 * the event work before further blockers are allowed to finish.
1743 mutex_lock(&ev->block_mutex);
1745 spin_lock_irqsave(&ev->lock, flags);
1746 cancel = !ev->block++;
1747 spin_unlock_irqrestore(&ev->lock, flags);
1750 cancel_delayed_work_sync(&disk->ev->dwork);
1752 mutex_unlock(&ev->block_mutex);
1755 static void __disk_unblock_events(struct gendisk *disk, bool check_now)
1757 struct disk_events *ev = disk->ev;
1759 unsigned long flags;
1761 spin_lock_irqsave(&ev->lock, flags);
1763 if (WARN_ON_ONCE(ev->block <= 0))
1769 intv = disk_events_poll_jiffies(disk);
1771 queue_delayed_work(system_freezable_power_efficient_wq,
1774 queue_delayed_work(system_freezable_power_efficient_wq,
1777 spin_unlock_irqrestore(&ev->lock, flags);
1781 * disk_unblock_events - unblock disk event checking
1782 * @disk: disk to unblock events for
1784 * Undo disk_block_events(). When the block count reaches zero, it
1785 * starts events polling if configured.
1788 * Don't care. Safe to call from irq context.
1790 void disk_unblock_events(struct gendisk *disk)
1793 __disk_unblock_events(disk, false);
1797 * disk_flush_events - schedule immediate event checking and flushing
1798 * @disk: disk to check and flush events for
1799 * @mask: events to flush
1801 * Schedule immediate event checking on @disk if not blocked. Events in
1802 * @mask are scheduled to be cleared from the driver. Note that this
1803 * doesn't clear the events from @disk->ev.
1806 * If @mask is non-zero must be called with bdev->bd_mutex held.
1808 void disk_flush_events(struct gendisk *disk, unsigned int mask)
1810 struct disk_events *ev = disk->ev;
1815 spin_lock_irq(&ev->lock);
1816 ev->clearing |= mask;
1818 mod_delayed_work(system_freezable_power_efficient_wq,
1820 spin_unlock_irq(&ev->lock);
1824 * disk_clear_events - synchronously check, clear and return pending events
1825 * @disk: disk to fetch and clear events from
1826 * @mask: mask of events to be fetched and cleared
1828 * Disk events are synchronously checked and pending events in @mask
1829 * are cleared and returned. This ignores the block count.
1834 static unsigned int disk_clear_events(struct gendisk *disk, unsigned int mask)
1836 struct disk_events *ev = disk->ev;
1837 unsigned int pending;
1838 unsigned int clearing = mask;
1843 disk_block_events(disk);
1846 * store the union of mask and ev->clearing on the stack so that the
1847 * race with disk_flush_events does not cause ambiguity (ev->clearing
1848 * can still be modified even if events are blocked).
1850 spin_lock_irq(&ev->lock);
1851 clearing |= ev->clearing;
1853 spin_unlock_irq(&ev->lock);
1855 disk_check_events(ev, &clearing);
1857 * if ev->clearing is not 0, the disk_flush_events got called in the
1858 * middle of this function, so we want to run the workfn without delay.
1860 __disk_unblock_events(disk, ev->clearing ? true : false);
1862 /* then, fetch and clear pending events */
1863 spin_lock_irq(&ev->lock);
1864 pending = ev->pending & mask;
1865 ev->pending &= ~mask;
1866 spin_unlock_irq(&ev->lock);
1867 WARN_ON_ONCE(clearing & mask);
1873 * bdev_check_media_change - check if a removable media has been changed
1874 * @bdev: block device to check
1876 * Check whether a removable media has been changed, and attempt to free all
1877 * dentries and inodes and invalidates all block device page cache entries in
1880 * Returns %true if the block device changed, or %false if not.
1882 bool bdev_check_media_change(struct block_device *bdev)
1884 unsigned int events;
1886 events = disk_clear_events(bdev->bd_disk, DISK_EVENT_MEDIA_CHANGE |
1887 DISK_EVENT_EJECT_REQUEST);
1888 if (!(events & DISK_EVENT_MEDIA_CHANGE))
1891 if (__invalidate_device(bdev, true))
1892 pr_warn("VFS: busy inodes on changed media %s\n",
1893 bdev->bd_disk->disk_name);
1894 set_bit(GD_NEED_PART_SCAN, &bdev->bd_disk->state);
1897 EXPORT_SYMBOL(bdev_check_media_change);
1900 * Separate this part out so that a different pointer for clearing_ptr can be
1901 * passed in for disk_clear_events.
1903 static void disk_events_workfn(struct work_struct *work)
1905 struct delayed_work *dwork = to_delayed_work(work);
1906 struct disk_events *ev = container_of(dwork, struct disk_events, dwork);
1908 disk_check_events(ev, &ev->clearing);
1911 static void disk_check_events(struct disk_events *ev,
1912 unsigned int *clearing_ptr)
1914 struct gendisk *disk = ev->disk;
1915 char *envp[ARRAY_SIZE(disk_uevents) + 1] = { };
1916 unsigned int clearing = *clearing_ptr;
1917 unsigned int events;
1919 int nr_events = 0, i;
1922 events = disk->fops->check_events(disk, clearing);
1924 /* accumulate pending events and schedule next poll if necessary */
1925 spin_lock_irq(&ev->lock);
1927 events &= ~ev->pending;
1928 ev->pending |= events;
1929 *clearing_ptr &= ~clearing;
1931 intv = disk_events_poll_jiffies(disk);
1932 if (!ev->block && intv)
1933 queue_delayed_work(system_freezable_power_efficient_wq,
1936 spin_unlock_irq(&ev->lock);
1939 * Tell userland about new events. Only the events listed in
1940 * @disk->events are reported, and only if DISK_EVENT_FLAG_UEVENT
1941 * is set. Otherwise, events are processed internally but never
1942 * get reported to userland.
1944 for (i = 0; i < ARRAY_SIZE(disk_uevents); i++)
1945 if ((events & disk->events & (1 << i)) &&
1946 (disk->event_flags & DISK_EVENT_FLAG_UEVENT))
1947 envp[nr_events++] = disk_uevents[i];
1950 kobject_uevent_env(&disk_to_dev(disk)->kobj, KOBJ_CHANGE, envp);
1954 * A disk events enabled device has the following sysfs nodes under
1955 * its /sys/block/X/ directory.
1957 * events : list of all supported events
1958 * events_async : list of events which can be detected w/o polling
1959 * (always empty, only for backwards compatibility)
1960 * events_poll_msecs : polling interval, 0: disable, -1: system default
1962 static ssize_t __disk_events_show(unsigned int events, char *buf)
1964 const char *delim = "";
1968 for (i = 0; i < ARRAY_SIZE(disk_events_strs); i++)
1969 if (events & (1 << i)) {
1970 pos += sprintf(buf + pos, "%s%s",
1971 delim, disk_events_strs[i]);
1975 pos += sprintf(buf + pos, "\n");
1979 static ssize_t disk_events_show(struct device *dev,
1980 struct device_attribute *attr, char *buf)
1982 struct gendisk *disk = dev_to_disk(dev);
1984 if (!(disk->event_flags & DISK_EVENT_FLAG_UEVENT))
1987 return __disk_events_show(disk->events, buf);
1990 static ssize_t disk_events_async_show(struct device *dev,
1991 struct device_attribute *attr, char *buf)
1996 static ssize_t disk_events_poll_msecs_show(struct device *dev,
1997 struct device_attribute *attr,
2000 struct gendisk *disk = dev_to_disk(dev);
2003 return sprintf(buf, "-1\n");
2005 return sprintf(buf, "%ld\n", disk->ev->poll_msecs);
2008 static ssize_t disk_events_poll_msecs_store(struct device *dev,
2009 struct device_attribute *attr,
2010 const char *buf, size_t count)
2012 struct gendisk *disk = dev_to_disk(dev);
2015 if (!count || !sscanf(buf, "%ld", &intv))
2018 if (intv < 0 && intv != -1)
2024 disk_block_events(disk);
2025 disk->ev->poll_msecs = intv;
2026 __disk_unblock_events(disk, true);
2031 static const DEVICE_ATTR(events, 0444, disk_events_show, NULL);
2032 static const DEVICE_ATTR(events_async, 0444, disk_events_async_show, NULL);
2033 static const DEVICE_ATTR(events_poll_msecs, 0644,
2034 disk_events_poll_msecs_show,
2035 disk_events_poll_msecs_store);
2037 static const struct attribute *disk_events_attrs[] = {
2038 &dev_attr_events.attr,
2039 &dev_attr_events_async.attr,
2040 &dev_attr_events_poll_msecs.attr,
2045 * The default polling interval can be specified by the kernel
2046 * parameter block.events_dfl_poll_msecs which defaults to 0
2047 * (disable). This can also be modified runtime by writing to
2048 * /sys/module/block/parameters/events_dfl_poll_msecs.
2050 static int disk_events_set_dfl_poll_msecs(const char *val,
2051 const struct kernel_param *kp)
2053 struct disk_events *ev;
2056 ret = param_set_ulong(val, kp);
2060 mutex_lock(&disk_events_mutex);
2062 list_for_each_entry(ev, &disk_events, node)
2063 disk_flush_events(ev->disk, 0);
2065 mutex_unlock(&disk_events_mutex);
2070 static const struct kernel_param_ops disk_events_dfl_poll_msecs_param_ops = {
2071 .set = disk_events_set_dfl_poll_msecs,
2072 .get = param_get_ulong,
2075 #undef MODULE_PARAM_PREFIX
2076 #define MODULE_PARAM_PREFIX "block."
2078 module_param_cb(events_dfl_poll_msecs, &disk_events_dfl_poll_msecs_param_ops,
2079 &disk_events_dfl_poll_msecs, 0644);
2082 * disk_{alloc|add|del|release}_events - initialize and destroy disk_events.
2084 static void disk_alloc_events(struct gendisk *disk)
2086 struct disk_events *ev;
2088 if (!disk->fops->check_events || !disk->events)
2091 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
2093 pr_warn("%s: failed to initialize events\n", disk->disk_name);
2097 INIT_LIST_HEAD(&ev->node);
2099 spin_lock_init(&ev->lock);
2100 mutex_init(&ev->block_mutex);
2102 ev->poll_msecs = -1;
2103 INIT_DELAYED_WORK(&ev->dwork, disk_events_workfn);
2108 static void disk_add_events(struct gendisk *disk)
2110 /* FIXME: error handling */
2111 if (sysfs_create_files(&disk_to_dev(disk)->kobj, disk_events_attrs) < 0)
2112 pr_warn("%s: failed to create sysfs files for events\n",
2118 mutex_lock(&disk_events_mutex);
2119 list_add_tail(&disk->ev->node, &disk_events);
2120 mutex_unlock(&disk_events_mutex);
2123 * Block count is initialized to 1 and the following initial
2124 * unblock kicks it into action.
2126 __disk_unblock_events(disk, true);
2129 static void disk_del_events(struct gendisk *disk)
2132 disk_block_events(disk);
2134 mutex_lock(&disk_events_mutex);
2135 list_del_init(&disk->ev->node);
2136 mutex_unlock(&disk_events_mutex);
2139 sysfs_remove_files(&disk_to_dev(disk)->kobj, disk_events_attrs);
2142 static void disk_release_events(struct gendisk *disk)
2144 /* the block count should be 1 from disk_del_events() */
2145 WARN_ON_ONCE(disk->ev && disk->ev->block != 1);