5 #include <linux/module.h>
7 #include <linux/genhd.h>
8 #include <linux/kdev_t.h>
9 #include <linux/kernel.h>
10 #include <linux/blkdev.h>
11 #include <linux/backing-dev.h>
12 #include <linux/init.h>
13 #include <linux/spinlock.h>
14 #include <linux/proc_fs.h>
15 #include <linux/seq_file.h>
16 #include <linux/slab.h>
17 #include <linux/kmod.h>
18 #include <linux/kobj_map.h>
19 #include <linux/mutex.h>
20 #include <linux/idr.h>
21 #include <linux/log2.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/badblocks.h>
27 static DEFINE_MUTEX(block_class_lock);
28 struct kobject *block_depr;
30 /* for extended dynamic devt allocation, currently only one major is used */
31 #define NR_EXT_DEVT (1 << MINORBITS)
33 /* For extended devt allocation. ext_devt_lock prevents look up
34 * results from going away underneath its user.
36 static DEFINE_SPINLOCK(ext_devt_lock);
37 static DEFINE_IDR(ext_devt_idr);
39 static const struct device_type disk_type;
41 static void disk_check_events(struct disk_events *ev,
42 unsigned int *clearing_ptr);
43 static void disk_alloc_events(struct gendisk *disk);
44 static void disk_add_events(struct gendisk *disk);
45 static void disk_del_events(struct gendisk *disk);
46 static void disk_release_events(struct gendisk *disk);
48 void part_inc_in_flight(struct request_queue *q, struct hd_struct *part, int rw)
53 part_stat_local_inc(part, in_flight[rw]);
55 part_stat_local_inc(&part_to_disk(part)->part0, in_flight[rw]);
58 void part_dec_in_flight(struct request_queue *q, struct hd_struct *part, int rw)
63 part_stat_local_dec(part, in_flight[rw]);
65 part_stat_local_dec(&part_to_disk(part)->part0, in_flight[rw]);
68 unsigned int part_in_flight(struct request_queue *q, struct hd_struct *part)
71 unsigned int inflight;
74 return blk_mq_in_flight(q, part);
78 for_each_possible_cpu(cpu) {
79 inflight += part_stat_local_read_cpu(part, in_flight[0], cpu) +
80 part_stat_local_read_cpu(part, in_flight[1], cpu);
82 if ((int)inflight < 0)
88 void part_in_flight_rw(struct request_queue *q, struct hd_struct *part,
89 unsigned int inflight[2])
94 blk_mq_in_flight_rw(q, part, inflight);
100 for_each_possible_cpu(cpu) {
101 inflight[0] += part_stat_local_read_cpu(part, in_flight[0], cpu);
102 inflight[1] += part_stat_local_read_cpu(part, in_flight[1], cpu);
104 if ((int)inflight[0] < 0)
106 if ((int)inflight[1] < 0)
110 struct hd_struct *__disk_get_part(struct gendisk *disk, int partno)
112 struct disk_part_tbl *ptbl = rcu_dereference(disk->part_tbl);
114 if (unlikely(partno < 0 || partno >= ptbl->len))
116 return rcu_dereference(ptbl->part[partno]);
120 * disk_get_part - get partition
121 * @disk: disk to look partition from
122 * @partno: partition number
124 * Look for partition @partno from @disk. If found, increment
125 * reference count and return it.
131 * Pointer to the found partition on success, NULL if not found.
133 struct hd_struct *disk_get_part(struct gendisk *disk, int partno)
135 struct hd_struct *part;
138 part = __disk_get_part(disk, partno);
140 get_device(part_to_dev(part));
145 EXPORT_SYMBOL_GPL(disk_get_part);
148 * disk_part_iter_init - initialize partition iterator
149 * @piter: iterator to initialize
150 * @disk: disk to iterate over
151 * @flags: DISK_PITER_* flags
153 * Initialize @piter so that it iterates over partitions of @disk.
158 void disk_part_iter_init(struct disk_part_iter *piter, struct gendisk *disk,
161 struct disk_part_tbl *ptbl;
164 ptbl = rcu_dereference(disk->part_tbl);
169 if (flags & DISK_PITER_REVERSE)
170 piter->idx = ptbl->len - 1;
171 else if (flags & (DISK_PITER_INCL_PART0 | DISK_PITER_INCL_EMPTY_PART0))
176 piter->flags = flags;
180 EXPORT_SYMBOL_GPL(disk_part_iter_init);
183 * disk_part_iter_next - proceed iterator to the next partition and return it
184 * @piter: iterator of interest
186 * Proceed @piter to the next partition and return it.
191 struct hd_struct *disk_part_iter_next(struct disk_part_iter *piter)
193 struct disk_part_tbl *ptbl;
196 /* put the last partition */
197 disk_put_part(piter->part);
202 ptbl = rcu_dereference(piter->disk->part_tbl);
204 /* determine iteration parameters */
205 if (piter->flags & DISK_PITER_REVERSE) {
207 if (piter->flags & (DISK_PITER_INCL_PART0 |
208 DISK_PITER_INCL_EMPTY_PART0))
217 /* iterate to the next partition */
218 for (; piter->idx != end; piter->idx += inc) {
219 struct hd_struct *part;
221 part = rcu_dereference(ptbl->part[piter->idx]);
224 if (!part_nr_sects_read(part) &&
225 !(piter->flags & DISK_PITER_INCL_EMPTY) &&
226 !(piter->flags & DISK_PITER_INCL_EMPTY_PART0 &&
230 get_device(part_to_dev(part));
240 EXPORT_SYMBOL_GPL(disk_part_iter_next);
243 * disk_part_iter_exit - finish up partition iteration
244 * @piter: iter of interest
246 * Called when iteration is over. Cleans up @piter.
251 void disk_part_iter_exit(struct disk_part_iter *piter)
253 disk_put_part(piter->part);
256 EXPORT_SYMBOL_GPL(disk_part_iter_exit);
258 static inline int sector_in_part(struct hd_struct *part, sector_t sector)
260 return part->start_sect <= sector &&
261 sector < part->start_sect + part_nr_sects_read(part);
265 * disk_map_sector_rcu - map sector to partition
266 * @disk: gendisk of interest
267 * @sector: sector to map
269 * Find out which partition @sector maps to on @disk. This is
270 * primarily used for stats accounting.
273 * RCU read locked. The returned partition pointer is valid only
274 * while preemption is disabled.
277 * Found partition on success, part0 is returned if no partition matches
279 struct hd_struct *disk_map_sector_rcu(struct gendisk *disk, sector_t sector)
281 struct disk_part_tbl *ptbl;
282 struct hd_struct *part;
285 ptbl = rcu_dereference(disk->part_tbl);
287 part = rcu_dereference(ptbl->last_lookup);
288 if (part && sector_in_part(part, sector))
291 for (i = 1; i < ptbl->len; i++) {
292 part = rcu_dereference(ptbl->part[i]);
294 if (part && sector_in_part(part, sector)) {
295 rcu_assign_pointer(ptbl->last_lookup, part);
301 EXPORT_SYMBOL_GPL(disk_map_sector_rcu);
304 * Can be deleted altogether. Later.
307 #define BLKDEV_MAJOR_HASH_SIZE 255
308 static struct blk_major_name {
309 struct blk_major_name *next;
312 } *major_names[BLKDEV_MAJOR_HASH_SIZE];
314 /* index in the above - for now: assume no multimajor ranges */
315 static inline int major_to_index(unsigned major)
317 return major % BLKDEV_MAJOR_HASH_SIZE;
320 #ifdef CONFIG_PROC_FS
321 void blkdev_show(struct seq_file *seqf, off_t offset)
323 struct blk_major_name *dp;
325 mutex_lock(&block_class_lock);
326 for (dp = major_names[major_to_index(offset)]; dp; dp = dp->next)
327 if (dp->major == offset)
328 seq_printf(seqf, "%3d %s\n", dp->major, dp->name);
329 mutex_unlock(&block_class_lock);
331 #endif /* CONFIG_PROC_FS */
334 * register_blkdev - register a new block device
336 * @major: the requested major device number [1..BLKDEV_MAJOR_MAX-1]. If
337 * @major = 0, try to allocate any unused major number.
338 * @name: the name of the new block device as a zero terminated string
340 * The @name must be unique within the system.
342 * The return value depends on the @major input parameter:
344 * - if a major device number was requested in range [1..BLKDEV_MAJOR_MAX-1]
345 * then the function returns zero on success, or a negative error code
346 * - if any unused major number was requested with @major = 0 parameter
347 * then the return value is the allocated major number in range
348 * [1..BLKDEV_MAJOR_MAX-1] or a negative error code otherwise
350 * See Documentation/admin-guide/devices.txt for the list of allocated
353 int register_blkdev(unsigned int major, const char *name)
355 struct blk_major_name **n, *p;
358 mutex_lock(&block_class_lock);
362 for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) {
363 if (major_names[index] == NULL)
368 printk("register_blkdev: failed to get major for %s\n",
377 if (major >= BLKDEV_MAJOR_MAX) {
378 pr_err("register_blkdev: major requested (%u) is greater than the maximum (%u) for %s\n",
379 major, BLKDEV_MAJOR_MAX-1, name);
385 p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL);
392 strlcpy(p->name, name, sizeof(p->name));
394 index = major_to_index(major);
396 for (n = &major_names[index]; *n; n = &(*n)->next) {
397 if ((*n)->major == major)
406 printk("register_blkdev: cannot get major %u for %s\n",
411 mutex_unlock(&block_class_lock);
415 EXPORT_SYMBOL(register_blkdev);
417 void unregister_blkdev(unsigned int major, const char *name)
419 struct blk_major_name **n;
420 struct blk_major_name *p = NULL;
421 int index = major_to_index(major);
423 mutex_lock(&block_class_lock);
424 for (n = &major_names[index]; *n; n = &(*n)->next)
425 if ((*n)->major == major)
427 if (!*n || strcmp((*n)->name, name)) {
433 mutex_unlock(&block_class_lock);
437 EXPORT_SYMBOL(unregister_blkdev);
439 static struct kobj_map *bdev_map;
442 * blk_mangle_minor - scatter minor numbers apart
443 * @minor: minor number to mangle
445 * Scatter consecutively allocated @minor number apart if MANGLE_DEVT
446 * is enabled. Mangling twice gives the original value.
454 static int blk_mangle_minor(int minor)
456 #ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT
459 for (i = 0; i < MINORBITS / 2; i++) {
460 int low = minor & (1 << i);
461 int high = minor & (1 << (MINORBITS - 1 - i));
462 int distance = MINORBITS - 1 - 2 * i;
464 minor ^= low | high; /* clear both bits */
465 low <<= distance; /* swap the positions */
467 minor |= low | high; /* and set */
474 * blk_alloc_devt - allocate a dev_t for a partition
475 * @part: partition to allocate dev_t for
476 * @devt: out parameter for resulting dev_t
478 * Allocate a dev_t for block device.
481 * 0 on success, allocated dev_t is returned in *@devt. -errno on
487 int blk_alloc_devt(struct hd_struct *part, dev_t *devt)
489 struct gendisk *disk = part_to_disk(part);
492 /* in consecutive minor range? */
493 if (part->partno < disk->minors) {
494 *devt = MKDEV(disk->major, disk->first_minor + part->partno);
498 /* allocate ext devt */
499 idr_preload(GFP_KERNEL);
501 spin_lock_bh(&ext_devt_lock);
502 idx = idr_alloc(&ext_devt_idr, part, 0, NR_EXT_DEVT, GFP_NOWAIT);
503 spin_unlock_bh(&ext_devt_lock);
507 return idx == -ENOSPC ? -EBUSY : idx;
509 *devt = MKDEV(BLOCK_EXT_MAJOR, blk_mangle_minor(idx));
514 * blk_free_devt - free a dev_t
515 * @devt: dev_t to free
517 * Free @devt which was allocated using blk_alloc_devt().
522 void blk_free_devt(dev_t devt)
524 if (devt == MKDEV(0, 0))
527 if (MAJOR(devt) == BLOCK_EXT_MAJOR) {
528 spin_lock_bh(&ext_devt_lock);
529 idr_remove(&ext_devt_idr, blk_mangle_minor(MINOR(devt)));
530 spin_unlock_bh(&ext_devt_lock);
534 static char *bdevt_str(dev_t devt, char *buf)
536 if (MAJOR(devt) <= 0xff && MINOR(devt) <= 0xff) {
537 char tbuf[BDEVT_SIZE];
538 snprintf(tbuf, BDEVT_SIZE, "%02x%02x", MAJOR(devt), MINOR(devt));
539 snprintf(buf, BDEVT_SIZE, "%-9s", tbuf);
541 snprintf(buf, BDEVT_SIZE, "%03x:%05x", MAJOR(devt), MINOR(devt));
547 * Register device numbers dev..(dev+range-1)
548 * range must be nonzero
549 * The hash chain is sorted on range, so that subranges can override.
551 void blk_register_region(dev_t devt, unsigned long range, struct module *module,
552 struct kobject *(*probe)(dev_t, int *, void *),
553 int (*lock)(dev_t, void *), void *data)
555 kobj_map(bdev_map, devt, range, module, probe, lock, data);
558 EXPORT_SYMBOL(blk_register_region);
560 void blk_unregister_region(dev_t devt, unsigned long range)
562 kobj_unmap(bdev_map, devt, range);
565 EXPORT_SYMBOL(blk_unregister_region);
567 static struct kobject *exact_match(dev_t devt, int *partno, void *data)
569 struct gendisk *p = data;
571 return &disk_to_dev(p)->kobj;
574 static int exact_lock(dev_t devt, void *data)
576 struct gendisk *p = data;
578 if (!get_disk_and_module(p))
583 static void register_disk(struct device *parent, struct gendisk *disk,
584 const struct attribute_group **groups)
586 struct device *ddev = disk_to_dev(disk);
587 struct block_device *bdev;
588 struct disk_part_iter piter;
589 struct hd_struct *part;
592 ddev->parent = parent;
594 dev_set_name(ddev, "%s", disk->disk_name);
596 /* delay uevents, until we scanned partition table */
597 dev_set_uevent_suppress(ddev, 1);
600 WARN_ON(ddev->groups);
601 ddev->groups = groups;
603 if (device_add(ddev))
605 if (!sysfs_deprecated) {
606 err = sysfs_create_link(block_depr, &ddev->kobj,
607 kobject_name(&ddev->kobj));
615 * avoid probable deadlock caused by allocating memory with
616 * GFP_KERNEL in runtime_resume callback of its all ancestor
619 pm_runtime_set_memalloc_noio(ddev, true);
621 disk->part0.holder_dir = kobject_create_and_add("holders", &ddev->kobj);
622 disk->slave_dir = kobject_create_and_add("slaves", &ddev->kobj);
624 if (disk->flags & GENHD_FL_HIDDEN) {
625 dev_set_uevent_suppress(ddev, 0);
629 /* No minors to use for partitions */
630 if (!disk_part_scan_enabled(disk))
633 /* No such device (e.g., media were just removed) */
634 if (!get_capacity(disk))
637 bdev = bdget_disk(disk, 0);
641 bdev->bd_invalidated = 1;
642 err = blkdev_get(bdev, FMODE_READ, NULL);
645 blkdev_put(bdev, FMODE_READ);
648 /* announce disk after possible partitions are created */
649 dev_set_uevent_suppress(ddev, 0);
650 kobject_uevent(&ddev->kobj, KOBJ_ADD);
652 /* announce possible partitions */
653 disk_part_iter_init(&piter, disk, 0);
654 while ((part = disk_part_iter_next(&piter)))
655 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_ADD);
656 disk_part_iter_exit(&piter);
658 err = sysfs_create_link(&ddev->kobj,
659 &disk->queue->backing_dev_info->dev->kobj,
665 * __device_add_disk - add disk information to kernel list
666 * @parent: parent device for the disk
667 * @disk: per-device partitioning information
668 * @groups: Additional per-device sysfs groups
669 * @register_queue: register the queue if set to true
671 * This function registers the partitioning information in @disk
674 * FIXME: error handling
676 static void __device_add_disk(struct device *parent, struct gendisk *disk,
677 const struct attribute_group **groups,
683 /* minors == 0 indicates to use ext devt from part0 and should
684 * be accompanied with EXT_DEVT flag. Make sure all
685 * parameters make sense.
687 WARN_ON(disk->minors && !(disk->major || disk->first_minor));
688 WARN_ON(!disk->minors &&
689 !(disk->flags & (GENHD_FL_EXT_DEVT | GENHD_FL_HIDDEN)));
691 disk->flags |= GENHD_FL_UP;
693 retval = blk_alloc_devt(&disk->part0, &devt);
698 disk->major = MAJOR(devt);
699 disk->first_minor = MINOR(devt);
701 disk_alloc_events(disk);
703 if (disk->flags & GENHD_FL_HIDDEN) {
705 * Don't let hidden disks show up in /proc/partitions,
706 * and don't bother scanning for partitions either.
708 disk->flags |= GENHD_FL_SUPPRESS_PARTITION_INFO;
709 disk->flags |= GENHD_FL_NO_PART_SCAN;
713 /* Register BDI before referencing it from bdev */
714 disk_to_dev(disk)->devt = devt;
715 ret = bdi_register_owner(disk->queue->backing_dev_info,
718 blk_register_region(disk_devt(disk), disk->minors, NULL,
719 exact_match, exact_lock, disk);
721 register_disk(parent, disk, groups);
723 blk_register_queue(disk);
726 * Take an extra ref on queue which will be put on disk_release()
727 * so that it sticks around as long as @disk is there.
729 WARN_ON_ONCE(!blk_get_queue(disk->queue));
731 disk_add_events(disk);
732 blk_integrity_add(disk);
735 void device_add_disk(struct device *parent, struct gendisk *disk,
736 const struct attribute_group **groups)
739 __device_add_disk(parent, disk, groups, true);
741 EXPORT_SYMBOL(device_add_disk);
743 void device_add_disk_no_queue_reg(struct device *parent, struct gendisk *disk)
745 __device_add_disk(parent, disk, NULL, false);
747 EXPORT_SYMBOL(device_add_disk_no_queue_reg);
749 void del_gendisk(struct gendisk *disk)
751 struct disk_part_iter piter;
752 struct hd_struct *part;
754 blk_integrity_del(disk);
755 disk_del_events(disk);
758 * Block lookups of the disk until all bdevs are unhashed and the
759 * disk is marked as dead (GENHD_FL_UP cleared).
761 down_write(&disk->lookup_sem);
762 /* invalidate stuff */
763 disk_part_iter_init(&piter, disk,
764 DISK_PITER_INCL_EMPTY | DISK_PITER_REVERSE);
765 while ((part = disk_part_iter_next(&piter))) {
766 invalidate_partition(disk, part->partno);
767 bdev_unhash_inode(part_devt(part));
768 delete_partition(disk, part->partno);
770 disk_part_iter_exit(&piter);
772 invalidate_partition(disk, 0);
773 bdev_unhash_inode(disk_devt(disk));
774 set_capacity(disk, 0);
775 disk->flags &= ~GENHD_FL_UP;
776 up_write(&disk->lookup_sem);
778 if (!(disk->flags & GENHD_FL_HIDDEN))
779 sysfs_remove_link(&disk_to_dev(disk)->kobj, "bdi");
782 * Unregister bdi before releasing device numbers (as they can
783 * get reused and we'd get clashes in sysfs).
785 if (!(disk->flags & GENHD_FL_HIDDEN))
786 bdi_unregister(disk->queue->backing_dev_info);
787 blk_unregister_queue(disk);
792 if (!(disk->flags & GENHD_FL_HIDDEN))
793 blk_unregister_region(disk_devt(disk), disk->minors);
795 kobject_put(disk->part0.holder_dir);
796 kobject_put(disk->slave_dir);
798 part_stat_set_all(&disk->part0, 0);
799 disk->part0.stamp = 0;
800 if (!sysfs_deprecated)
801 sysfs_remove_link(block_depr, dev_name(disk_to_dev(disk)));
802 pm_runtime_set_memalloc_noio(disk_to_dev(disk), false);
803 device_del(disk_to_dev(disk));
805 EXPORT_SYMBOL(del_gendisk);
807 /* sysfs access to bad-blocks list. */
808 static ssize_t disk_badblocks_show(struct device *dev,
809 struct device_attribute *attr,
812 struct gendisk *disk = dev_to_disk(dev);
815 return sprintf(page, "\n");
817 return badblocks_show(disk->bb, page, 0);
820 static ssize_t disk_badblocks_store(struct device *dev,
821 struct device_attribute *attr,
822 const char *page, size_t len)
824 struct gendisk *disk = dev_to_disk(dev);
829 return badblocks_store(disk->bb, page, len, 0);
833 * get_gendisk - get partitioning information for a given device
834 * @devt: device to get partitioning information for
835 * @partno: returned partition index
837 * This function gets the structure containing partitioning
838 * information for the given device @devt.
840 struct gendisk *get_gendisk(dev_t devt, int *partno)
842 struct gendisk *disk = NULL;
844 if (MAJOR(devt) != BLOCK_EXT_MAJOR) {
845 struct kobject *kobj;
847 kobj = kobj_lookup(bdev_map, devt, partno);
849 disk = dev_to_disk(kobj_to_dev(kobj));
851 struct hd_struct *part;
853 spin_lock_bh(&ext_devt_lock);
854 part = idr_find(&ext_devt_idr, blk_mangle_minor(MINOR(devt)));
855 if (part && get_disk_and_module(part_to_disk(part))) {
856 *partno = part->partno;
857 disk = part_to_disk(part);
859 spin_unlock_bh(&ext_devt_lock);
866 * Synchronize with del_gendisk() to not return disk that is being
869 down_read(&disk->lookup_sem);
870 if (unlikely((disk->flags & GENHD_FL_HIDDEN) ||
871 !(disk->flags & GENHD_FL_UP))) {
872 up_read(&disk->lookup_sem);
873 put_disk_and_module(disk);
876 up_read(&disk->lookup_sem);
880 EXPORT_SYMBOL(get_gendisk);
883 * bdget_disk - do bdget() by gendisk and partition number
884 * @disk: gendisk of interest
885 * @partno: partition number
887 * Find partition @partno from @disk, do bdget() on it.
893 * Resulting block_device on success, NULL on failure.
895 struct block_device *bdget_disk(struct gendisk *disk, int partno)
897 struct hd_struct *part;
898 struct block_device *bdev = NULL;
900 part = disk_get_part(disk, partno);
902 bdev = bdget(part_devt(part));
907 EXPORT_SYMBOL(bdget_disk);
910 * print a full list of all partitions - intended for places where the root
911 * filesystem can't be mounted and thus to give the victim some idea of what
914 void __init printk_all_partitions(void)
916 struct class_dev_iter iter;
919 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
920 while ((dev = class_dev_iter_next(&iter))) {
921 struct gendisk *disk = dev_to_disk(dev);
922 struct disk_part_iter piter;
923 struct hd_struct *part;
924 char name_buf[BDEVNAME_SIZE];
925 char devt_buf[BDEVT_SIZE];
928 * Don't show empty devices or things that have been
931 if (get_capacity(disk) == 0 ||
932 (disk->flags & GENHD_FL_SUPPRESS_PARTITION_INFO))
936 * Note, unlike /proc/partitions, I am showing the
937 * numbers in hex - the same format as the root=
940 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
941 while ((part = disk_part_iter_next(&piter))) {
942 bool is_part0 = part == &disk->part0;
944 printk("%s%s %10llu %s %s", is_part0 ? "" : " ",
945 bdevt_str(part_devt(part), devt_buf),
946 (unsigned long long)part_nr_sects_read(part) >> 1
947 , disk_name(disk, part->partno, name_buf),
948 part->info ? part->info->uuid : "");
950 if (dev->parent && dev->parent->driver)
951 printk(" driver: %s\n",
952 dev->parent->driver->name);
954 printk(" (driver?)\n");
958 disk_part_iter_exit(&piter);
960 class_dev_iter_exit(&iter);
963 #ifdef CONFIG_PROC_FS
965 static void *disk_seqf_start(struct seq_file *seqf, loff_t *pos)
968 struct class_dev_iter *iter;
971 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
973 return ERR_PTR(-ENOMEM);
975 seqf->private = iter;
976 class_dev_iter_init(iter, &block_class, NULL, &disk_type);
978 dev = class_dev_iter_next(iter);
983 return dev_to_disk(dev);
986 static void *disk_seqf_next(struct seq_file *seqf, void *v, loff_t *pos)
991 dev = class_dev_iter_next(seqf->private);
993 return dev_to_disk(dev);
998 static void disk_seqf_stop(struct seq_file *seqf, void *v)
1000 struct class_dev_iter *iter = seqf->private;
1002 /* stop is called even after start failed :-( */
1004 class_dev_iter_exit(iter);
1006 seqf->private = NULL;
1010 static void *show_partition_start(struct seq_file *seqf, loff_t *pos)
1014 p = disk_seqf_start(seqf, pos);
1015 if (!IS_ERR_OR_NULL(p) && !*pos)
1016 seq_puts(seqf, "major minor #blocks name\n\n");
1020 static int show_partition(struct seq_file *seqf, void *v)
1022 struct gendisk *sgp = v;
1023 struct disk_part_iter piter;
1024 struct hd_struct *part;
1025 char buf[BDEVNAME_SIZE];
1027 /* Don't show non-partitionable removeable devices or empty devices */
1028 if (!get_capacity(sgp) || (!disk_max_parts(sgp) &&
1029 (sgp->flags & GENHD_FL_REMOVABLE)))
1031 if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)
1034 /* show the full disk and all non-0 size partitions of it */
1035 disk_part_iter_init(&piter, sgp, DISK_PITER_INCL_PART0);
1036 while ((part = disk_part_iter_next(&piter)))
1037 seq_printf(seqf, "%4d %7d %10llu %s\n",
1038 MAJOR(part_devt(part)), MINOR(part_devt(part)),
1039 (unsigned long long)part_nr_sects_read(part) >> 1,
1040 disk_name(sgp, part->partno, buf));
1041 disk_part_iter_exit(&piter);
1046 static const struct seq_operations partitions_op = {
1047 .start = show_partition_start,
1048 .next = disk_seqf_next,
1049 .stop = disk_seqf_stop,
1050 .show = show_partition
1055 static struct kobject *base_probe(dev_t devt, int *partno, void *data)
1057 if (request_module("block-major-%d-%d", MAJOR(devt), MINOR(devt)) > 0)
1058 /* Make old-style 2.4 aliases work */
1059 request_module("block-major-%d", MAJOR(devt));
1063 static int __init genhd_device_init(void)
1067 block_class.dev_kobj = sysfs_dev_block_kobj;
1068 error = class_register(&block_class);
1069 if (unlikely(error))
1071 bdev_map = kobj_map_init(base_probe, &block_class_lock);
1074 register_blkdev(BLOCK_EXT_MAJOR, "blkext");
1076 /* create top-level block dir */
1077 if (!sysfs_deprecated)
1078 block_depr = kobject_create_and_add("block", NULL);
1082 subsys_initcall(genhd_device_init);
1084 static ssize_t disk_range_show(struct device *dev,
1085 struct device_attribute *attr, char *buf)
1087 struct gendisk *disk = dev_to_disk(dev);
1089 return sprintf(buf, "%d\n", disk->minors);
1092 static ssize_t disk_ext_range_show(struct device *dev,
1093 struct device_attribute *attr, char *buf)
1095 struct gendisk *disk = dev_to_disk(dev);
1097 return sprintf(buf, "%d\n", disk_max_parts(disk));
1100 static ssize_t disk_removable_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",
1106 (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
1109 static ssize_t disk_hidden_show(struct device *dev,
1110 struct device_attribute *attr, char *buf)
1112 struct gendisk *disk = dev_to_disk(dev);
1114 return sprintf(buf, "%d\n",
1115 (disk->flags & GENHD_FL_HIDDEN ? 1 : 0));
1118 static ssize_t disk_ro_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", get_disk_ro(disk) ? 1 : 0);
1126 static ssize_t disk_capability_show(struct device *dev,
1127 struct device_attribute *attr, char *buf)
1129 struct gendisk *disk = dev_to_disk(dev);
1131 return sprintf(buf, "%x\n", disk->flags);
1134 static ssize_t disk_alignment_offset_show(struct device *dev,
1135 struct device_attribute *attr,
1138 struct gendisk *disk = dev_to_disk(dev);
1140 return sprintf(buf, "%d\n", queue_alignment_offset(disk->queue));
1143 static ssize_t disk_discard_alignment_show(struct device *dev,
1144 struct device_attribute *attr,
1147 struct gendisk *disk = dev_to_disk(dev);
1149 return sprintf(buf, "%d\n", queue_discard_alignment(disk->queue));
1152 static DEVICE_ATTR(range, 0444, disk_range_show, NULL);
1153 static DEVICE_ATTR(ext_range, 0444, disk_ext_range_show, NULL);
1154 static DEVICE_ATTR(removable, 0444, disk_removable_show, NULL);
1155 static DEVICE_ATTR(hidden, 0444, disk_hidden_show, NULL);
1156 static DEVICE_ATTR(ro, 0444, disk_ro_show, NULL);
1157 static DEVICE_ATTR(size, 0444, part_size_show, NULL);
1158 static DEVICE_ATTR(alignment_offset, 0444, disk_alignment_offset_show, NULL);
1159 static DEVICE_ATTR(discard_alignment, 0444, disk_discard_alignment_show, NULL);
1160 static DEVICE_ATTR(capability, 0444, disk_capability_show, NULL);
1161 static DEVICE_ATTR(stat, 0444, part_stat_show, NULL);
1162 static DEVICE_ATTR(inflight, 0444, part_inflight_show, NULL);
1163 static DEVICE_ATTR(badblocks, 0644, disk_badblocks_show, disk_badblocks_store);
1164 #ifdef CONFIG_FAIL_MAKE_REQUEST
1165 static struct device_attribute dev_attr_fail =
1166 __ATTR(make-it-fail, 0644, part_fail_show, part_fail_store);
1168 #ifdef CONFIG_FAIL_IO_TIMEOUT
1169 static struct device_attribute dev_attr_fail_timeout =
1170 __ATTR(io-timeout-fail, 0644, part_timeout_show, part_timeout_store);
1173 static struct attribute *disk_attrs[] = {
1174 &dev_attr_range.attr,
1175 &dev_attr_ext_range.attr,
1176 &dev_attr_removable.attr,
1177 &dev_attr_hidden.attr,
1179 &dev_attr_size.attr,
1180 &dev_attr_alignment_offset.attr,
1181 &dev_attr_discard_alignment.attr,
1182 &dev_attr_capability.attr,
1183 &dev_attr_stat.attr,
1184 &dev_attr_inflight.attr,
1185 &dev_attr_badblocks.attr,
1186 #ifdef CONFIG_FAIL_MAKE_REQUEST
1187 &dev_attr_fail.attr,
1189 #ifdef CONFIG_FAIL_IO_TIMEOUT
1190 &dev_attr_fail_timeout.attr,
1195 static umode_t disk_visible(struct kobject *kobj, struct attribute *a, int n)
1197 struct device *dev = container_of(kobj, typeof(*dev), kobj);
1198 struct gendisk *disk = dev_to_disk(dev);
1200 if (a == &dev_attr_badblocks.attr && !disk->bb)
1205 static struct attribute_group disk_attr_group = {
1206 .attrs = disk_attrs,
1207 .is_visible = disk_visible,
1210 static const struct attribute_group *disk_attr_groups[] = {
1216 * disk_replace_part_tbl - replace disk->part_tbl in RCU-safe way
1217 * @disk: disk to replace part_tbl for
1218 * @new_ptbl: new part_tbl to install
1220 * Replace disk->part_tbl with @new_ptbl in RCU-safe way. The
1221 * original ptbl is freed using RCU callback.
1224 * Matching bd_mutex locked or the caller is the only user of @disk.
1226 static void disk_replace_part_tbl(struct gendisk *disk,
1227 struct disk_part_tbl *new_ptbl)
1229 struct disk_part_tbl *old_ptbl =
1230 rcu_dereference_protected(disk->part_tbl, 1);
1232 rcu_assign_pointer(disk->part_tbl, new_ptbl);
1235 rcu_assign_pointer(old_ptbl->last_lookup, NULL);
1236 kfree_rcu(old_ptbl, rcu_head);
1241 * disk_expand_part_tbl - expand disk->part_tbl
1242 * @disk: disk to expand part_tbl for
1243 * @partno: expand such that this partno can fit in
1245 * Expand disk->part_tbl such that @partno can fit in. disk->part_tbl
1246 * uses RCU to allow unlocked dereferencing for stats and other stuff.
1249 * Matching bd_mutex locked or the caller is the only user of @disk.
1253 * 0 on success, -errno on failure.
1255 int disk_expand_part_tbl(struct gendisk *disk, int partno)
1257 struct disk_part_tbl *old_ptbl =
1258 rcu_dereference_protected(disk->part_tbl, 1);
1259 struct disk_part_tbl *new_ptbl;
1260 int len = old_ptbl ? old_ptbl->len : 0;
1265 * check for int overflow, since we can get here from blkpg_ioctl()
1266 * with a user passed 'partno'.
1268 target = partno + 1;
1272 /* disk_max_parts() is zero during initialization, ignore if so */
1273 if (disk_max_parts(disk) && target > disk_max_parts(disk))
1279 size = sizeof(*new_ptbl) + target * sizeof(new_ptbl->part[0]);
1280 new_ptbl = kzalloc_node(size, GFP_KERNEL, disk->node_id);
1284 new_ptbl->len = target;
1286 for (i = 0; i < len; i++)
1287 rcu_assign_pointer(new_ptbl->part[i], old_ptbl->part[i]);
1289 disk_replace_part_tbl(disk, new_ptbl);
1293 static void disk_release(struct device *dev)
1295 struct gendisk *disk = dev_to_disk(dev);
1297 blk_free_devt(dev->devt);
1298 disk_release_events(disk);
1299 kfree(disk->random);
1300 disk_replace_part_tbl(disk, NULL);
1301 hd_free_part(&disk->part0);
1303 blk_put_queue(disk->queue);
1306 struct class block_class = {
1310 static char *block_devnode(struct device *dev, umode_t *mode,
1311 kuid_t *uid, kgid_t *gid)
1313 struct gendisk *disk = dev_to_disk(dev);
1316 return disk->devnode(disk, mode);
1320 static const struct device_type disk_type = {
1322 .groups = disk_attr_groups,
1323 .release = disk_release,
1324 .devnode = block_devnode,
1327 #ifdef CONFIG_PROC_FS
1329 * aggregate disk stat collector. Uses the same stats that the sysfs
1330 * entries do, above, but makes them available through one seq_file.
1332 * The output looks suspiciously like /proc/partitions with a bunch of
1335 static int diskstats_show(struct seq_file *seqf, void *v)
1337 struct gendisk *gp = v;
1338 struct disk_part_iter piter;
1339 struct hd_struct *hd;
1340 char buf[BDEVNAME_SIZE];
1341 unsigned int inflight;
1344 if (&disk_to_dev(gp)->kobj.entry == block_class.devices.next)
1345 seq_puts(seqf, "major minor name"
1346 " rio rmerge rsect ruse wio wmerge "
1347 "wsect wuse running use aveq"
1351 disk_part_iter_init(&piter, gp, DISK_PITER_INCL_EMPTY_PART0);
1352 while ((hd = disk_part_iter_next(&piter))) {
1353 inflight = part_in_flight(gp->queue, hd);
1354 seq_printf(seqf, "%4d %7d %s "
1359 MAJOR(part_devt(hd)), MINOR(part_devt(hd)),
1360 disk_name(gp, hd->partno, buf),
1361 part_stat_read(hd, ios[STAT_READ]),
1362 part_stat_read(hd, merges[STAT_READ]),
1363 part_stat_read(hd, sectors[STAT_READ]),
1364 (unsigned int)part_stat_read_msecs(hd, STAT_READ),
1365 part_stat_read(hd, ios[STAT_WRITE]),
1366 part_stat_read(hd, merges[STAT_WRITE]),
1367 part_stat_read(hd, sectors[STAT_WRITE]),
1368 (unsigned int)part_stat_read_msecs(hd, STAT_WRITE),
1370 jiffies_to_msecs(part_stat_read(hd, io_ticks)),
1371 jiffies_to_msecs(part_stat_read(hd, time_in_queue)),
1372 part_stat_read(hd, ios[STAT_DISCARD]),
1373 part_stat_read(hd, merges[STAT_DISCARD]),
1374 part_stat_read(hd, sectors[STAT_DISCARD]),
1375 (unsigned int)part_stat_read_msecs(hd, STAT_DISCARD)
1378 disk_part_iter_exit(&piter);
1383 static const struct seq_operations diskstats_op = {
1384 .start = disk_seqf_start,
1385 .next = disk_seqf_next,
1386 .stop = disk_seqf_stop,
1387 .show = diskstats_show
1390 static int __init proc_genhd_init(void)
1392 proc_create_seq("diskstats", 0, NULL, &diskstats_op);
1393 proc_create_seq("partitions", 0, NULL, &partitions_op);
1396 module_init(proc_genhd_init);
1397 #endif /* CONFIG_PROC_FS */
1399 dev_t blk_lookup_devt(const char *name, int partno)
1401 dev_t devt = MKDEV(0, 0);
1402 struct class_dev_iter iter;
1405 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
1406 while ((dev = class_dev_iter_next(&iter))) {
1407 struct gendisk *disk = dev_to_disk(dev);
1408 struct hd_struct *part;
1410 if (strcmp(dev_name(dev), name))
1413 if (partno < disk->minors) {
1414 /* We need to return the right devno, even
1415 * if the partition doesn't exist yet.
1417 devt = MKDEV(MAJOR(dev->devt),
1418 MINOR(dev->devt) + partno);
1421 part = disk_get_part(disk, partno);
1423 devt = part_devt(part);
1424 disk_put_part(part);
1427 disk_put_part(part);
1429 class_dev_iter_exit(&iter);
1432 EXPORT_SYMBOL(blk_lookup_devt);
1434 struct gendisk *__alloc_disk_node(int minors, int node_id)
1436 struct gendisk *disk;
1437 struct disk_part_tbl *ptbl;
1439 if (minors > DISK_MAX_PARTS) {
1441 "block: can't allocate more than %d partitions\n",
1443 minors = DISK_MAX_PARTS;
1446 disk = kzalloc_node(sizeof(struct gendisk), GFP_KERNEL, node_id);
1448 if (!init_part_stats(&disk->part0)) {
1452 init_rwsem(&disk->lookup_sem);
1453 disk->node_id = node_id;
1454 if (disk_expand_part_tbl(disk, 0)) {
1455 free_part_stats(&disk->part0);
1459 ptbl = rcu_dereference_protected(disk->part_tbl, 1);
1460 rcu_assign_pointer(ptbl->part[0], &disk->part0);
1463 * set_capacity() and get_capacity() currently don't use
1464 * seqcounter to read/update the part0->nr_sects. Still init
1465 * the counter as we can read the sectors in IO submission
1466 * patch using seqence counters.
1468 * TODO: Ideally set_capacity() and get_capacity() should be
1469 * converted to make use of bd_mutex and sequence counters.
1471 seqcount_init(&disk->part0.nr_sects_seq);
1472 if (hd_ref_init(&disk->part0)) {
1473 hd_free_part(&disk->part0);
1478 disk->minors = minors;
1479 rand_initialize_disk(disk);
1480 disk_to_dev(disk)->class = &block_class;
1481 disk_to_dev(disk)->type = &disk_type;
1482 device_initialize(disk_to_dev(disk));
1486 EXPORT_SYMBOL(__alloc_disk_node);
1488 struct kobject *get_disk_and_module(struct gendisk *disk)
1490 struct module *owner;
1491 struct kobject *kobj;
1495 owner = disk->fops->owner;
1496 if (owner && !try_module_get(owner))
1498 kobj = kobject_get_unless_zero(&disk_to_dev(disk)->kobj);
1506 EXPORT_SYMBOL(get_disk_and_module);
1508 void put_disk(struct gendisk *disk)
1511 kobject_put(&disk_to_dev(disk)->kobj);
1513 EXPORT_SYMBOL(put_disk);
1516 * This is a counterpart of get_disk_and_module() and thus also of
1519 void put_disk_and_module(struct gendisk *disk)
1522 struct module *owner = disk->fops->owner;
1528 EXPORT_SYMBOL(put_disk_and_module);
1530 static void set_disk_ro_uevent(struct gendisk *gd, int ro)
1532 char event[] = "DISK_RO=1";
1533 char *envp[] = { event, NULL };
1537 kobject_uevent_env(&disk_to_dev(gd)->kobj, KOBJ_CHANGE, envp);
1540 void set_device_ro(struct block_device *bdev, int flag)
1542 bdev->bd_part->policy = flag;
1545 EXPORT_SYMBOL(set_device_ro);
1547 void set_disk_ro(struct gendisk *disk, int flag)
1549 struct disk_part_iter piter;
1550 struct hd_struct *part;
1552 if (disk->part0.policy != flag) {
1553 set_disk_ro_uevent(disk, flag);
1554 disk->part0.policy = flag;
1557 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_EMPTY);
1558 while ((part = disk_part_iter_next(&piter)))
1559 part->policy = flag;
1560 disk_part_iter_exit(&piter);
1563 EXPORT_SYMBOL(set_disk_ro);
1565 int bdev_read_only(struct block_device *bdev)
1569 return bdev->bd_part->policy;
1572 EXPORT_SYMBOL(bdev_read_only);
1574 int invalidate_partition(struct gendisk *disk, int partno)
1577 struct block_device *bdev = bdget_disk(disk, partno);
1580 res = __invalidate_device(bdev, true);
1586 EXPORT_SYMBOL(invalidate_partition);
1589 * Disk events - monitor disk events like media change and eject request.
1591 struct disk_events {
1592 struct list_head node; /* all disk_event's */
1593 struct gendisk *disk; /* the associated disk */
1596 struct mutex block_mutex; /* protects blocking */
1597 int block; /* event blocking depth */
1598 unsigned int pending; /* events already sent out */
1599 unsigned int clearing; /* events being cleared */
1601 long poll_msecs; /* interval, -1 for default */
1602 struct delayed_work dwork;
1605 static const char *disk_events_strs[] = {
1606 [ilog2(DISK_EVENT_MEDIA_CHANGE)] = "media_change",
1607 [ilog2(DISK_EVENT_EJECT_REQUEST)] = "eject_request",
1610 static char *disk_uevents[] = {
1611 [ilog2(DISK_EVENT_MEDIA_CHANGE)] = "DISK_MEDIA_CHANGE=1",
1612 [ilog2(DISK_EVENT_EJECT_REQUEST)] = "DISK_EJECT_REQUEST=1",
1615 /* list of all disk_events */
1616 static DEFINE_MUTEX(disk_events_mutex);
1617 static LIST_HEAD(disk_events);
1619 /* disable in-kernel polling by default */
1620 static unsigned long disk_events_dfl_poll_msecs;
1622 static unsigned long disk_events_poll_jiffies(struct gendisk *disk)
1624 struct disk_events *ev = disk->ev;
1625 long intv_msecs = 0;
1628 * If device-specific poll interval is set, always use it. If
1629 * the default is being used, poll iff there are events which
1630 * can't be monitored asynchronously.
1632 if (ev->poll_msecs >= 0)
1633 intv_msecs = ev->poll_msecs;
1634 else if (disk->events & ~disk->async_events)
1635 intv_msecs = disk_events_dfl_poll_msecs;
1637 return msecs_to_jiffies(intv_msecs);
1641 * disk_block_events - block and flush disk event checking
1642 * @disk: disk to block events for
1644 * On return from this function, it is guaranteed that event checking
1645 * isn't in progress and won't happen until unblocked by
1646 * disk_unblock_events(). Events blocking is counted and the actual
1647 * unblocking happens after the matching number of unblocks are done.
1649 * Note that this intentionally does not block event checking from
1650 * disk_clear_events().
1655 void disk_block_events(struct gendisk *disk)
1657 struct disk_events *ev = disk->ev;
1658 unsigned long flags;
1665 * Outer mutex ensures that the first blocker completes canceling
1666 * the event work before further blockers are allowed to finish.
1668 mutex_lock(&ev->block_mutex);
1670 spin_lock_irqsave(&ev->lock, flags);
1671 cancel = !ev->block++;
1672 spin_unlock_irqrestore(&ev->lock, flags);
1675 cancel_delayed_work_sync(&disk->ev->dwork);
1677 mutex_unlock(&ev->block_mutex);
1680 static void __disk_unblock_events(struct gendisk *disk, bool check_now)
1682 struct disk_events *ev = disk->ev;
1684 unsigned long flags;
1686 spin_lock_irqsave(&ev->lock, flags);
1688 if (WARN_ON_ONCE(ev->block <= 0))
1694 intv = disk_events_poll_jiffies(disk);
1696 queue_delayed_work(system_freezable_power_efficient_wq,
1699 queue_delayed_work(system_freezable_power_efficient_wq,
1702 spin_unlock_irqrestore(&ev->lock, flags);
1706 * disk_unblock_events - unblock disk event checking
1707 * @disk: disk to unblock events for
1709 * Undo disk_block_events(). When the block count reaches zero, it
1710 * starts events polling if configured.
1713 * Don't care. Safe to call from irq context.
1715 void disk_unblock_events(struct gendisk *disk)
1718 __disk_unblock_events(disk, false);
1722 * disk_flush_events - schedule immediate event checking and flushing
1723 * @disk: disk to check and flush events for
1724 * @mask: events to flush
1726 * Schedule immediate event checking on @disk if not blocked. Events in
1727 * @mask are scheduled to be cleared from the driver. Note that this
1728 * doesn't clear the events from @disk->ev.
1731 * If @mask is non-zero must be called with bdev->bd_mutex held.
1733 void disk_flush_events(struct gendisk *disk, unsigned int mask)
1735 struct disk_events *ev = disk->ev;
1740 spin_lock_irq(&ev->lock);
1741 ev->clearing |= mask;
1743 mod_delayed_work(system_freezable_power_efficient_wq,
1745 spin_unlock_irq(&ev->lock);
1749 * disk_clear_events - synchronously check, clear and return pending events
1750 * @disk: disk to fetch and clear events from
1751 * @mask: mask of events to be fetched and cleared
1753 * Disk events are synchronously checked and pending events in @mask
1754 * are cleared and returned. This ignores the block count.
1759 unsigned int disk_clear_events(struct gendisk *disk, unsigned int mask)
1761 const struct block_device_operations *bdops = disk->fops;
1762 struct disk_events *ev = disk->ev;
1763 unsigned int pending;
1764 unsigned int clearing = mask;
1767 /* for drivers still using the old ->media_changed method */
1768 if ((mask & DISK_EVENT_MEDIA_CHANGE) &&
1769 bdops->media_changed && bdops->media_changed(disk))
1770 return DISK_EVENT_MEDIA_CHANGE;
1774 disk_block_events(disk);
1777 * store the union of mask and ev->clearing on the stack so that the
1778 * race with disk_flush_events does not cause ambiguity (ev->clearing
1779 * can still be modified even if events are blocked).
1781 spin_lock_irq(&ev->lock);
1782 clearing |= ev->clearing;
1784 spin_unlock_irq(&ev->lock);
1786 disk_check_events(ev, &clearing);
1788 * if ev->clearing is not 0, the disk_flush_events got called in the
1789 * middle of this function, so we want to run the workfn without delay.
1791 __disk_unblock_events(disk, ev->clearing ? true : false);
1793 /* then, fetch and clear pending events */
1794 spin_lock_irq(&ev->lock);
1795 pending = ev->pending & mask;
1796 ev->pending &= ~mask;
1797 spin_unlock_irq(&ev->lock);
1798 WARN_ON_ONCE(clearing & mask);
1804 * Separate this part out so that a different pointer for clearing_ptr can be
1805 * passed in for disk_clear_events.
1807 static void disk_events_workfn(struct work_struct *work)
1809 struct delayed_work *dwork = to_delayed_work(work);
1810 struct disk_events *ev = container_of(dwork, struct disk_events, dwork);
1812 disk_check_events(ev, &ev->clearing);
1815 static void disk_check_events(struct disk_events *ev,
1816 unsigned int *clearing_ptr)
1818 struct gendisk *disk = ev->disk;
1819 char *envp[ARRAY_SIZE(disk_uevents) + 1] = { };
1820 unsigned int clearing = *clearing_ptr;
1821 unsigned int events;
1823 int nr_events = 0, i;
1826 events = disk->fops->check_events(disk, clearing);
1828 /* accumulate pending events and schedule next poll if necessary */
1829 spin_lock_irq(&ev->lock);
1831 events &= ~ev->pending;
1832 ev->pending |= events;
1833 *clearing_ptr &= ~clearing;
1835 intv = disk_events_poll_jiffies(disk);
1836 if (!ev->block && intv)
1837 queue_delayed_work(system_freezable_power_efficient_wq,
1840 spin_unlock_irq(&ev->lock);
1843 * Tell userland about new events. Only the events listed in
1844 * @disk->events are reported. Unlisted events are processed the
1845 * same internally but never get reported to userland.
1847 for (i = 0; i < ARRAY_SIZE(disk_uevents); i++)
1848 if (events & disk->events & (1 << i))
1849 envp[nr_events++] = disk_uevents[i];
1852 kobject_uevent_env(&disk_to_dev(disk)->kobj, KOBJ_CHANGE, envp);
1856 * A disk events enabled device has the following sysfs nodes under
1857 * its /sys/block/X/ directory.
1859 * events : list of all supported events
1860 * events_async : list of events which can be detected w/o polling
1861 * events_poll_msecs : polling interval, 0: disable, -1: system default
1863 static ssize_t __disk_events_show(unsigned int events, char *buf)
1865 const char *delim = "";
1869 for (i = 0; i < ARRAY_SIZE(disk_events_strs); i++)
1870 if (events & (1 << i)) {
1871 pos += sprintf(buf + pos, "%s%s",
1872 delim, disk_events_strs[i]);
1876 pos += sprintf(buf + pos, "\n");
1880 static ssize_t disk_events_show(struct device *dev,
1881 struct device_attribute *attr, char *buf)
1883 struct gendisk *disk = dev_to_disk(dev);
1885 return __disk_events_show(disk->events, buf);
1888 static ssize_t disk_events_async_show(struct device *dev,
1889 struct device_attribute *attr, char *buf)
1891 struct gendisk *disk = dev_to_disk(dev);
1893 return __disk_events_show(disk->async_events, buf);
1896 static ssize_t disk_events_poll_msecs_show(struct device *dev,
1897 struct device_attribute *attr,
1900 struct gendisk *disk = dev_to_disk(dev);
1902 return sprintf(buf, "%ld\n", disk->ev->poll_msecs);
1905 static ssize_t disk_events_poll_msecs_store(struct device *dev,
1906 struct device_attribute *attr,
1907 const char *buf, size_t count)
1909 struct gendisk *disk = dev_to_disk(dev);
1912 if (!count || !sscanf(buf, "%ld", &intv))
1915 if (intv < 0 && intv != -1)
1918 disk_block_events(disk);
1919 disk->ev->poll_msecs = intv;
1920 __disk_unblock_events(disk, true);
1925 static const DEVICE_ATTR(events, 0444, disk_events_show, NULL);
1926 static const DEVICE_ATTR(events_async, 0444, disk_events_async_show, NULL);
1927 static const DEVICE_ATTR(events_poll_msecs, 0644,
1928 disk_events_poll_msecs_show,
1929 disk_events_poll_msecs_store);
1931 static const struct attribute *disk_events_attrs[] = {
1932 &dev_attr_events.attr,
1933 &dev_attr_events_async.attr,
1934 &dev_attr_events_poll_msecs.attr,
1939 * The default polling interval can be specified by the kernel
1940 * parameter block.events_dfl_poll_msecs which defaults to 0
1941 * (disable). This can also be modified runtime by writing to
1942 * /sys/module/block/events_dfl_poll_msecs.
1944 static int disk_events_set_dfl_poll_msecs(const char *val,
1945 const struct kernel_param *kp)
1947 struct disk_events *ev;
1950 ret = param_set_ulong(val, kp);
1954 mutex_lock(&disk_events_mutex);
1956 list_for_each_entry(ev, &disk_events, node)
1957 disk_flush_events(ev->disk, 0);
1959 mutex_unlock(&disk_events_mutex);
1964 static const struct kernel_param_ops disk_events_dfl_poll_msecs_param_ops = {
1965 .set = disk_events_set_dfl_poll_msecs,
1966 .get = param_get_ulong,
1969 #undef MODULE_PARAM_PREFIX
1970 #define MODULE_PARAM_PREFIX "block."
1972 module_param_cb(events_dfl_poll_msecs, &disk_events_dfl_poll_msecs_param_ops,
1973 &disk_events_dfl_poll_msecs, 0644);
1976 * disk_{alloc|add|del|release}_events - initialize and destroy disk_events.
1978 static void disk_alloc_events(struct gendisk *disk)
1980 struct disk_events *ev;
1982 if (!disk->fops->check_events)
1985 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
1987 pr_warn("%s: failed to initialize events\n", disk->disk_name);
1991 INIT_LIST_HEAD(&ev->node);
1993 spin_lock_init(&ev->lock);
1994 mutex_init(&ev->block_mutex);
1996 ev->poll_msecs = -1;
1997 INIT_DELAYED_WORK(&ev->dwork, disk_events_workfn);
2002 static void disk_add_events(struct gendisk *disk)
2007 /* FIXME: error handling */
2008 if (sysfs_create_files(&disk_to_dev(disk)->kobj, disk_events_attrs) < 0)
2009 pr_warn("%s: failed to create sysfs files for events\n",
2012 mutex_lock(&disk_events_mutex);
2013 list_add_tail(&disk->ev->node, &disk_events);
2014 mutex_unlock(&disk_events_mutex);
2017 * Block count is initialized to 1 and the following initial
2018 * unblock kicks it into action.
2020 __disk_unblock_events(disk, true);
2023 static void disk_del_events(struct gendisk *disk)
2028 disk_block_events(disk);
2030 mutex_lock(&disk_events_mutex);
2031 list_del_init(&disk->ev->node);
2032 mutex_unlock(&disk_events_mutex);
2034 sysfs_remove_files(&disk_to_dev(disk)->kobj, disk_events_attrs);
2037 static void disk_release_events(struct gendisk *disk)
2039 /* the block count should be 1 from disk_del_events() */
2040 WARN_ON_ONCE(disk->ev && disk->ev->block != 1);