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);
49 * disk_get_part - get partition
50 * @disk: disk to look partition from
51 * @partno: partition number
53 * Look for partition @partno from @disk. If found, increment
54 * reference count and return it.
60 * Pointer to the found partition on success, NULL if not found.
62 struct hd_struct *disk_get_part(struct gendisk *disk, int partno)
64 struct hd_struct *part = NULL;
65 struct disk_part_tbl *ptbl;
67 if (unlikely(partno < 0))
72 ptbl = rcu_dereference(disk->part_tbl);
73 if (likely(partno < ptbl->len)) {
74 part = rcu_dereference(ptbl->part[partno]);
76 get_device(part_to_dev(part));
83 EXPORT_SYMBOL_GPL(disk_get_part);
86 * disk_part_iter_init - initialize partition iterator
87 * @piter: iterator to initialize
88 * @disk: disk to iterate over
89 * @flags: DISK_PITER_* flags
91 * Initialize @piter so that it iterates over partitions of @disk.
96 void disk_part_iter_init(struct disk_part_iter *piter, struct gendisk *disk,
99 struct disk_part_tbl *ptbl;
102 ptbl = rcu_dereference(disk->part_tbl);
107 if (flags & DISK_PITER_REVERSE)
108 piter->idx = ptbl->len - 1;
109 else if (flags & (DISK_PITER_INCL_PART0 | DISK_PITER_INCL_EMPTY_PART0))
114 piter->flags = flags;
118 EXPORT_SYMBOL_GPL(disk_part_iter_init);
121 * disk_part_iter_next - proceed iterator to the next partition and return it
122 * @piter: iterator of interest
124 * Proceed @piter to the next partition and return it.
129 struct hd_struct *disk_part_iter_next(struct disk_part_iter *piter)
131 struct disk_part_tbl *ptbl;
134 /* put the last partition */
135 disk_put_part(piter->part);
140 ptbl = rcu_dereference(piter->disk->part_tbl);
142 /* determine iteration parameters */
143 if (piter->flags & DISK_PITER_REVERSE) {
145 if (piter->flags & (DISK_PITER_INCL_PART0 |
146 DISK_PITER_INCL_EMPTY_PART0))
155 /* iterate to the next partition */
156 for (; piter->idx != end; piter->idx += inc) {
157 struct hd_struct *part;
159 part = rcu_dereference(ptbl->part[piter->idx]);
162 if (!part_nr_sects_read(part) &&
163 !(piter->flags & DISK_PITER_INCL_EMPTY) &&
164 !(piter->flags & DISK_PITER_INCL_EMPTY_PART0 &&
168 get_device(part_to_dev(part));
178 EXPORT_SYMBOL_GPL(disk_part_iter_next);
181 * disk_part_iter_exit - finish up partition iteration
182 * @piter: iter of interest
184 * Called when iteration is over. Cleans up @piter.
189 void disk_part_iter_exit(struct disk_part_iter *piter)
191 disk_put_part(piter->part);
194 EXPORT_SYMBOL_GPL(disk_part_iter_exit);
196 static inline int sector_in_part(struct hd_struct *part, sector_t sector)
198 return part->start_sect <= sector &&
199 sector < part->start_sect + part_nr_sects_read(part);
203 * disk_map_sector_rcu - map sector to partition
204 * @disk: gendisk of interest
205 * @sector: sector to map
207 * Find out which partition @sector maps to on @disk. This is
208 * primarily used for stats accounting.
211 * RCU read locked. The returned partition pointer is valid only
212 * while preemption is disabled.
215 * Found partition on success, part0 is returned if no partition matches
217 struct hd_struct *disk_map_sector_rcu(struct gendisk *disk, sector_t sector)
219 struct disk_part_tbl *ptbl;
220 struct hd_struct *part;
223 ptbl = rcu_dereference(disk->part_tbl);
225 part = rcu_dereference(ptbl->last_lookup);
226 if (part && sector_in_part(part, sector))
229 for (i = 1; i < ptbl->len; i++) {
230 part = rcu_dereference(ptbl->part[i]);
232 if (part && sector_in_part(part, sector)) {
233 rcu_assign_pointer(ptbl->last_lookup, part);
239 EXPORT_SYMBOL_GPL(disk_map_sector_rcu);
242 * Can be deleted altogether. Later.
245 #define BLKDEV_MAJOR_HASH_SIZE 255
246 static struct blk_major_name {
247 struct blk_major_name *next;
250 } *major_names[BLKDEV_MAJOR_HASH_SIZE];
252 /* index in the above - for now: assume no multimajor ranges */
253 static inline int major_to_index(unsigned major)
255 return major % BLKDEV_MAJOR_HASH_SIZE;
258 #ifdef CONFIG_PROC_FS
259 void blkdev_show(struct seq_file *seqf, off_t offset)
261 struct blk_major_name *dp;
263 mutex_lock(&block_class_lock);
264 for (dp = major_names[major_to_index(offset)]; dp; dp = dp->next)
265 if (dp->major == offset)
266 seq_printf(seqf, "%3d %s\n", dp->major, dp->name);
267 mutex_unlock(&block_class_lock);
269 #endif /* CONFIG_PROC_FS */
272 * register_blkdev - register a new block device
274 * @major: the requested major device number [1..255]. If @major = 0, try to
275 * allocate any unused major number.
276 * @name: the name of the new block device as a zero terminated string
278 * The @name must be unique within the system.
280 * The return value depends on the @major input parameter:
282 * - if a major device number was requested in range [1..255] then the
283 * function returns zero on success, or a negative error code
284 * - if any unused major number was requested with @major = 0 parameter
285 * then the return value is the allocated major number in range
286 * [1..255] or a negative error code otherwise
288 int register_blkdev(unsigned int major, const char *name)
290 struct blk_major_name **n, *p;
293 mutex_lock(&block_class_lock);
297 for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) {
298 if (major_names[index] == NULL)
303 printk("register_blkdev: failed to get major for %s\n",
312 if (major >= BLKDEV_MAJOR_MAX) {
313 pr_err("register_blkdev: major requested (%d) is greater than the maximum (%d) for %s\n",
314 major, BLKDEV_MAJOR_MAX, name);
320 p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL);
327 strlcpy(p->name, name, sizeof(p->name));
329 index = major_to_index(major);
331 for (n = &major_names[index]; *n; n = &(*n)->next) {
332 if ((*n)->major == major)
341 printk("register_blkdev: cannot get major %d for %s\n",
346 mutex_unlock(&block_class_lock);
350 EXPORT_SYMBOL(register_blkdev);
352 void unregister_blkdev(unsigned int major, const char *name)
354 struct blk_major_name **n;
355 struct blk_major_name *p = NULL;
356 int index = major_to_index(major);
358 mutex_lock(&block_class_lock);
359 for (n = &major_names[index]; *n; n = &(*n)->next)
360 if ((*n)->major == major)
362 if (!*n || strcmp((*n)->name, name)) {
368 mutex_unlock(&block_class_lock);
372 EXPORT_SYMBOL(unregister_blkdev);
374 static struct kobj_map *bdev_map;
377 * blk_mangle_minor - scatter minor numbers apart
378 * @minor: minor number to mangle
380 * Scatter consecutively allocated @minor number apart if MANGLE_DEVT
381 * is enabled. Mangling twice gives the original value.
389 static int blk_mangle_minor(int minor)
391 #ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT
394 for (i = 0; i < MINORBITS / 2; i++) {
395 int low = minor & (1 << i);
396 int high = minor & (1 << (MINORBITS - 1 - i));
397 int distance = MINORBITS - 1 - 2 * i;
399 minor ^= low | high; /* clear both bits */
400 low <<= distance; /* swap the positions */
402 minor |= low | high; /* and set */
409 * blk_alloc_devt - allocate a dev_t for a partition
410 * @part: partition to allocate dev_t for
411 * @devt: out parameter for resulting dev_t
413 * Allocate a dev_t for block device.
416 * 0 on success, allocated dev_t is returned in *@devt. -errno on
422 int blk_alloc_devt(struct hd_struct *part, dev_t *devt)
424 struct gendisk *disk = part_to_disk(part);
427 /* in consecutive minor range? */
428 if (part->partno < disk->minors) {
429 *devt = MKDEV(disk->major, disk->first_minor + part->partno);
433 /* allocate ext devt */
434 idr_preload(GFP_KERNEL);
436 spin_lock_bh(&ext_devt_lock);
437 idx = idr_alloc(&ext_devt_idr, part, 0, NR_EXT_DEVT, GFP_NOWAIT);
438 spin_unlock_bh(&ext_devt_lock);
442 return idx == -ENOSPC ? -EBUSY : idx;
444 *devt = MKDEV(BLOCK_EXT_MAJOR, blk_mangle_minor(idx));
449 * blk_free_devt - free a dev_t
450 * @devt: dev_t to free
452 * Free @devt which was allocated using blk_alloc_devt().
457 void blk_free_devt(dev_t devt)
459 if (devt == MKDEV(0, 0))
462 if (MAJOR(devt) == BLOCK_EXT_MAJOR) {
463 spin_lock_bh(&ext_devt_lock);
464 idr_remove(&ext_devt_idr, blk_mangle_minor(MINOR(devt)));
465 spin_unlock_bh(&ext_devt_lock);
469 static char *bdevt_str(dev_t devt, char *buf)
471 if (MAJOR(devt) <= 0xff && MINOR(devt) <= 0xff) {
472 char tbuf[BDEVT_SIZE];
473 snprintf(tbuf, BDEVT_SIZE, "%02x%02x", MAJOR(devt), MINOR(devt));
474 snprintf(buf, BDEVT_SIZE, "%-9s", tbuf);
476 snprintf(buf, BDEVT_SIZE, "%03x:%05x", MAJOR(devt), MINOR(devt));
482 * Register device numbers dev..(dev+range-1)
483 * range must be nonzero
484 * The hash chain is sorted on range, so that subranges can override.
486 void blk_register_region(dev_t devt, unsigned long range, struct module *module,
487 struct kobject *(*probe)(dev_t, int *, void *),
488 int (*lock)(dev_t, void *), void *data)
490 kobj_map(bdev_map, devt, range, module, probe, lock, data);
493 EXPORT_SYMBOL(blk_register_region);
495 void blk_unregister_region(dev_t devt, unsigned long range)
497 kobj_unmap(bdev_map, devt, range);
500 EXPORT_SYMBOL(blk_unregister_region);
502 static struct kobject *exact_match(dev_t devt, int *partno, void *data)
504 struct gendisk *p = data;
506 return &disk_to_dev(p)->kobj;
509 static int exact_lock(dev_t devt, void *data)
511 struct gendisk *p = data;
518 static void register_disk(struct device *parent, struct gendisk *disk)
520 struct device *ddev = disk_to_dev(disk);
521 struct block_device *bdev;
522 struct disk_part_iter piter;
523 struct hd_struct *part;
526 ddev->parent = parent;
528 dev_set_name(ddev, "%s", disk->disk_name);
530 /* delay uevents, until we scanned partition table */
531 dev_set_uevent_suppress(ddev, 1);
533 if (device_add(ddev))
535 if (!sysfs_deprecated) {
536 err = sysfs_create_link(block_depr, &ddev->kobj,
537 kobject_name(&ddev->kobj));
545 * avoid probable deadlock caused by allocating memory with
546 * GFP_KERNEL in runtime_resume callback of its all ancestor
549 pm_runtime_set_memalloc_noio(ddev, true);
551 disk->part0.holder_dir = kobject_create_and_add("holders", &ddev->kobj);
552 disk->slave_dir = kobject_create_and_add("slaves", &ddev->kobj);
554 /* No minors to use for partitions */
555 if (!disk_part_scan_enabled(disk))
558 /* No such device (e.g., media were just removed) */
559 if (!get_capacity(disk))
562 bdev = bdget_disk(disk, 0);
566 bdev->bd_invalidated = 1;
567 err = blkdev_get(bdev, FMODE_READ, NULL);
570 blkdev_put(bdev, FMODE_READ);
573 /* announce disk after possible partitions are created */
574 dev_set_uevent_suppress(ddev, 0);
575 kobject_uevent(&ddev->kobj, KOBJ_ADD);
577 /* announce possible partitions */
578 disk_part_iter_init(&piter, disk, 0);
579 while ((part = disk_part_iter_next(&piter)))
580 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_ADD);
581 disk_part_iter_exit(&piter);
585 * device_add_disk - add partitioning information to kernel list
586 * @parent: parent device for the disk
587 * @disk: per-device partitioning information
589 * This function registers the partitioning information in @disk
592 * FIXME: error handling
594 void device_add_disk(struct device *parent, struct gendisk *disk)
596 struct backing_dev_info *bdi;
600 /* minors == 0 indicates to use ext devt from part0 and should
601 * be accompanied with EXT_DEVT flag. Make sure all
602 * parameters make sense.
604 WARN_ON(disk->minors && !(disk->major || disk->first_minor));
605 WARN_ON(!disk->minors && !(disk->flags & GENHD_FL_EXT_DEVT));
607 disk->flags |= GENHD_FL_UP;
609 retval = blk_alloc_devt(&disk->part0, &devt);
614 disk_to_dev(disk)->devt = devt;
616 /* ->major and ->first_minor aren't supposed to be
617 * dereferenced from here on, but set them just in case.
619 disk->major = MAJOR(devt);
620 disk->first_minor = MINOR(devt);
622 disk_alloc_events(disk);
624 /* Register BDI before referencing it from bdev */
625 bdi = disk->queue->backing_dev_info;
626 bdi_register_owner(bdi, disk_to_dev(disk));
628 blk_register_region(disk_devt(disk), disk->minors, NULL,
629 exact_match, exact_lock, disk);
630 register_disk(parent, disk);
631 blk_register_queue(disk);
634 * Take an extra ref on queue which will be put on disk_release()
635 * so that it sticks around as long as @disk is there.
637 WARN_ON_ONCE(!blk_get_queue(disk->queue));
639 retval = sysfs_create_link(&disk_to_dev(disk)->kobj, &bdi->dev->kobj,
643 disk_add_events(disk);
644 blk_integrity_add(disk);
646 EXPORT_SYMBOL(device_add_disk);
648 void del_gendisk(struct gendisk *disk)
650 struct disk_part_iter piter;
651 struct hd_struct *part;
653 blk_integrity_del(disk);
654 disk_del_events(disk);
656 /* invalidate stuff */
657 disk_part_iter_init(&piter, disk,
658 DISK_PITER_INCL_EMPTY | DISK_PITER_REVERSE);
659 while ((part = disk_part_iter_next(&piter))) {
660 invalidate_partition(disk, part->partno);
661 bdev_unhash_inode(part_devt(part));
662 delete_partition(disk, part->partno);
664 disk_part_iter_exit(&piter);
666 invalidate_partition(disk, 0);
667 bdev_unhash_inode(disk_devt(disk));
668 set_capacity(disk, 0);
669 disk->flags &= ~GENHD_FL_UP;
671 sysfs_remove_link(&disk_to_dev(disk)->kobj, "bdi");
674 * Unregister bdi before releasing device numbers (as they can
675 * get reused and we'd get clashes in sysfs).
677 bdi_unregister(disk->queue->backing_dev_info);
678 blk_unregister_queue(disk);
682 blk_unregister_region(disk_devt(disk), disk->minors);
684 part_stat_set_all(&disk->part0, 0);
685 disk->part0.stamp = 0;
687 kobject_put(disk->part0.holder_dir);
688 kobject_put(disk->slave_dir);
689 if (!sysfs_deprecated)
690 sysfs_remove_link(block_depr, dev_name(disk_to_dev(disk)));
691 pm_runtime_set_memalloc_noio(disk_to_dev(disk), false);
692 device_del(disk_to_dev(disk));
694 EXPORT_SYMBOL(del_gendisk);
696 /* sysfs access to bad-blocks list. */
697 static ssize_t disk_badblocks_show(struct device *dev,
698 struct device_attribute *attr,
701 struct gendisk *disk = dev_to_disk(dev);
704 return sprintf(page, "\n");
706 return badblocks_show(disk->bb, page, 0);
709 static ssize_t disk_badblocks_store(struct device *dev,
710 struct device_attribute *attr,
711 const char *page, size_t len)
713 struct gendisk *disk = dev_to_disk(dev);
718 return badblocks_store(disk->bb, page, len, 0);
722 * get_gendisk - get partitioning information for a given device
723 * @devt: device to get partitioning information for
724 * @partno: returned partition index
726 * This function gets the structure containing partitioning
727 * information for the given device @devt.
729 struct gendisk *get_gendisk(dev_t devt, int *partno)
731 struct gendisk *disk = NULL;
733 if (MAJOR(devt) != BLOCK_EXT_MAJOR) {
734 struct kobject *kobj;
736 kobj = kobj_lookup(bdev_map, devt, partno);
738 disk = dev_to_disk(kobj_to_dev(kobj));
740 struct hd_struct *part;
742 spin_lock_bh(&ext_devt_lock);
743 part = idr_find(&ext_devt_idr, blk_mangle_minor(MINOR(devt)));
744 if (part && get_disk(part_to_disk(part))) {
745 *partno = part->partno;
746 disk = part_to_disk(part);
748 spin_unlock_bh(&ext_devt_lock);
753 EXPORT_SYMBOL(get_gendisk);
756 * bdget_disk - do bdget() by gendisk and partition number
757 * @disk: gendisk of interest
758 * @partno: partition number
760 * Find partition @partno from @disk, do bdget() on it.
766 * Resulting block_device on success, NULL on failure.
768 struct block_device *bdget_disk(struct gendisk *disk, int partno)
770 struct hd_struct *part;
771 struct block_device *bdev = NULL;
773 part = disk_get_part(disk, partno);
775 bdev = bdget(part_devt(part));
780 EXPORT_SYMBOL(bdget_disk);
783 * print a full list of all partitions - intended for places where the root
784 * filesystem can't be mounted and thus to give the victim some idea of what
787 void __init printk_all_partitions(void)
789 struct class_dev_iter iter;
792 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
793 while ((dev = class_dev_iter_next(&iter))) {
794 struct gendisk *disk = dev_to_disk(dev);
795 struct disk_part_iter piter;
796 struct hd_struct *part;
797 char name_buf[BDEVNAME_SIZE];
798 char devt_buf[BDEVT_SIZE];
801 * Don't show empty devices or things that have been
804 if (get_capacity(disk) == 0 ||
805 (disk->flags & GENHD_FL_SUPPRESS_PARTITION_INFO))
809 * Note, unlike /proc/partitions, I am showing the
810 * numbers in hex - the same format as the root=
813 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
814 while ((part = disk_part_iter_next(&piter))) {
815 bool is_part0 = part == &disk->part0;
817 printk("%s%s %10llu %s %s", is_part0 ? "" : " ",
818 bdevt_str(part_devt(part), devt_buf),
819 (unsigned long long)part_nr_sects_read(part) >> 1
820 , disk_name(disk, part->partno, name_buf),
821 part->info ? part->info->uuid : "");
823 if (dev->parent && dev->parent->driver)
824 printk(" driver: %s\n",
825 dev->parent->driver->name);
827 printk(" (driver?)\n");
831 disk_part_iter_exit(&piter);
833 class_dev_iter_exit(&iter);
836 #ifdef CONFIG_PROC_FS
838 static void *disk_seqf_start(struct seq_file *seqf, loff_t *pos)
841 struct class_dev_iter *iter;
844 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
846 return ERR_PTR(-ENOMEM);
848 seqf->private = iter;
849 class_dev_iter_init(iter, &block_class, NULL, &disk_type);
851 dev = class_dev_iter_next(iter);
856 return dev_to_disk(dev);
859 static void *disk_seqf_next(struct seq_file *seqf, void *v, loff_t *pos)
864 dev = class_dev_iter_next(seqf->private);
866 return dev_to_disk(dev);
871 static void disk_seqf_stop(struct seq_file *seqf, void *v)
873 struct class_dev_iter *iter = seqf->private;
875 /* stop is called even after start failed :-( */
877 class_dev_iter_exit(iter);
879 seqf->private = NULL;
883 static void *show_partition_start(struct seq_file *seqf, loff_t *pos)
887 p = disk_seqf_start(seqf, pos);
888 if (!IS_ERR_OR_NULL(p) && !*pos)
889 seq_puts(seqf, "major minor #blocks name\n\n");
893 static int show_partition(struct seq_file *seqf, void *v)
895 struct gendisk *sgp = v;
896 struct disk_part_iter piter;
897 struct hd_struct *part;
898 char buf[BDEVNAME_SIZE];
900 /* Don't show non-partitionable removeable devices or empty devices */
901 if (!get_capacity(sgp) || (!disk_max_parts(sgp) &&
902 (sgp->flags & GENHD_FL_REMOVABLE)))
904 if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)
907 /* show the full disk and all non-0 size partitions of it */
908 disk_part_iter_init(&piter, sgp, DISK_PITER_INCL_PART0);
909 while ((part = disk_part_iter_next(&piter)))
910 seq_printf(seqf, "%4d %7d %10llu %s\n",
911 MAJOR(part_devt(part)), MINOR(part_devt(part)),
912 (unsigned long long)part_nr_sects_read(part) >> 1,
913 disk_name(sgp, part->partno, buf));
914 disk_part_iter_exit(&piter);
919 static const struct seq_operations partitions_op = {
920 .start = show_partition_start,
921 .next = disk_seqf_next,
922 .stop = disk_seqf_stop,
923 .show = show_partition
926 static int partitions_open(struct inode *inode, struct file *file)
928 return seq_open(file, &partitions_op);
931 static const struct file_operations proc_partitions_operations = {
932 .open = partitions_open,
935 .release = seq_release,
940 static struct kobject *base_probe(dev_t devt, int *partno, void *data)
942 if (request_module("block-major-%d-%d", MAJOR(devt), MINOR(devt)) > 0)
943 /* Make old-style 2.4 aliases work */
944 request_module("block-major-%d", MAJOR(devt));
948 static int __init genhd_device_init(void)
952 block_class.dev_kobj = sysfs_dev_block_kobj;
953 error = class_register(&block_class);
956 bdev_map = kobj_map_init(base_probe, &block_class_lock);
959 register_blkdev(BLOCK_EXT_MAJOR, "blkext");
961 /* create top-level block dir */
962 if (!sysfs_deprecated)
963 block_depr = kobject_create_and_add("block", NULL);
967 subsys_initcall(genhd_device_init);
969 static ssize_t disk_range_show(struct device *dev,
970 struct device_attribute *attr, char *buf)
972 struct gendisk *disk = dev_to_disk(dev);
974 return sprintf(buf, "%d\n", disk->minors);
977 static ssize_t disk_ext_range_show(struct device *dev,
978 struct device_attribute *attr, char *buf)
980 struct gendisk *disk = dev_to_disk(dev);
982 return sprintf(buf, "%d\n", disk_max_parts(disk));
985 static ssize_t disk_removable_show(struct device *dev,
986 struct device_attribute *attr, char *buf)
988 struct gendisk *disk = dev_to_disk(dev);
990 return sprintf(buf, "%d\n",
991 (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
994 static ssize_t disk_ro_show(struct device *dev,
995 struct device_attribute *attr, char *buf)
997 struct gendisk *disk = dev_to_disk(dev);
999 return sprintf(buf, "%d\n", get_disk_ro(disk) ? 1 : 0);
1002 static ssize_t disk_capability_show(struct device *dev,
1003 struct device_attribute *attr, char *buf)
1005 struct gendisk *disk = dev_to_disk(dev);
1007 return sprintf(buf, "%x\n", disk->flags);
1010 static ssize_t disk_alignment_offset_show(struct device *dev,
1011 struct device_attribute *attr,
1014 struct gendisk *disk = dev_to_disk(dev);
1016 return sprintf(buf, "%d\n", queue_alignment_offset(disk->queue));
1019 static ssize_t disk_discard_alignment_show(struct device *dev,
1020 struct device_attribute *attr,
1023 struct gendisk *disk = dev_to_disk(dev);
1025 return sprintf(buf, "%d\n", queue_discard_alignment(disk->queue));
1028 static DEVICE_ATTR(range, S_IRUGO, disk_range_show, NULL);
1029 static DEVICE_ATTR(ext_range, S_IRUGO, disk_ext_range_show, NULL);
1030 static DEVICE_ATTR(removable, S_IRUGO, disk_removable_show, NULL);
1031 static DEVICE_ATTR(ro, S_IRUGO, disk_ro_show, NULL);
1032 static DEVICE_ATTR(size, S_IRUGO, part_size_show, NULL);
1033 static DEVICE_ATTR(alignment_offset, S_IRUGO, disk_alignment_offset_show, NULL);
1034 static DEVICE_ATTR(discard_alignment, S_IRUGO, disk_discard_alignment_show,
1036 static DEVICE_ATTR(capability, S_IRUGO, disk_capability_show, NULL);
1037 static DEVICE_ATTR(stat, S_IRUGO, part_stat_show, NULL);
1038 static DEVICE_ATTR(inflight, S_IRUGO, part_inflight_show, NULL);
1039 static DEVICE_ATTR(badblocks, S_IRUGO | S_IWUSR, disk_badblocks_show,
1040 disk_badblocks_store);
1041 #ifdef CONFIG_FAIL_MAKE_REQUEST
1042 static struct device_attribute dev_attr_fail =
1043 __ATTR(make-it-fail, S_IRUGO|S_IWUSR, part_fail_show, part_fail_store);
1045 #ifdef CONFIG_FAIL_IO_TIMEOUT
1046 static struct device_attribute dev_attr_fail_timeout =
1047 __ATTR(io-timeout-fail, S_IRUGO|S_IWUSR, part_timeout_show,
1048 part_timeout_store);
1051 static struct attribute *disk_attrs[] = {
1052 &dev_attr_range.attr,
1053 &dev_attr_ext_range.attr,
1054 &dev_attr_removable.attr,
1056 &dev_attr_size.attr,
1057 &dev_attr_alignment_offset.attr,
1058 &dev_attr_discard_alignment.attr,
1059 &dev_attr_capability.attr,
1060 &dev_attr_stat.attr,
1061 &dev_attr_inflight.attr,
1062 &dev_attr_badblocks.attr,
1063 #ifdef CONFIG_FAIL_MAKE_REQUEST
1064 &dev_attr_fail.attr,
1066 #ifdef CONFIG_FAIL_IO_TIMEOUT
1067 &dev_attr_fail_timeout.attr,
1072 static umode_t disk_visible(struct kobject *kobj, struct attribute *a, int n)
1074 struct device *dev = container_of(kobj, typeof(*dev), kobj);
1075 struct gendisk *disk = dev_to_disk(dev);
1077 if (a == &dev_attr_badblocks.attr && !disk->bb)
1082 static struct attribute_group disk_attr_group = {
1083 .attrs = disk_attrs,
1084 .is_visible = disk_visible,
1087 static const struct attribute_group *disk_attr_groups[] = {
1093 * disk_replace_part_tbl - replace disk->part_tbl in RCU-safe way
1094 * @disk: disk to replace part_tbl for
1095 * @new_ptbl: new part_tbl to install
1097 * Replace disk->part_tbl with @new_ptbl in RCU-safe way. The
1098 * original ptbl is freed using RCU callback.
1101 * Matching bd_mutx locked.
1103 static void disk_replace_part_tbl(struct gendisk *disk,
1104 struct disk_part_tbl *new_ptbl)
1106 struct disk_part_tbl *old_ptbl = disk->part_tbl;
1108 rcu_assign_pointer(disk->part_tbl, new_ptbl);
1111 rcu_assign_pointer(old_ptbl->last_lookup, NULL);
1112 kfree_rcu(old_ptbl, rcu_head);
1117 * disk_expand_part_tbl - expand disk->part_tbl
1118 * @disk: disk to expand part_tbl for
1119 * @partno: expand such that this partno can fit in
1121 * Expand disk->part_tbl such that @partno can fit in. disk->part_tbl
1122 * uses RCU to allow unlocked dereferencing for stats and other stuff.
1125 * Matching bd_mutex locked, might sleep.
1128 * 0 on success, -errno on failure.
1130 int disk_expand_part_tbl(struct gendisk *disk, int partno)
1132 struct disk_part_tbl *old_ptbl = disk->part_tbl;
1133 struct disk_part_tbl *new_ptbl;
1134 int len = old_ptbl ? old_ptbl->len : 0;
1139 * check for int overflow, since we can get here from blkpg_ioctl()
1140 * with a user passed 'partno'.
1142 target = partno + 1;
1146 /* disk_max_parts() is zero during initialization, ignore if so */
1147 if (disk_max_parts(disk) && target > disk_max_parts(disk))
1153 size = sizeof(*new_ptbl) + target * sizeof(new_ptbl->part[0]);
1154 new_ptbl = kzalloc_node(size, GFP_KERNEL, disk->node_id);
1158 new_ptbl->len = target;
1160 for (i = 0; i < len; i++)
1161 rcu_assign_pointer(new_ptbl->part[i], old_ptbl->part[i]);
1163 disk_replace_part_tbl(disk, new_ptbl);
1167 static void disk_release(struct device *dev)
1169 struct gendisk *disk = dev_to_disk(dev);
1171 blk_free_devt(dev->devt);
1172 disk_release_events(disk);
1173 kfree(disk->random);
1174 disk_replace_part_tbl(disk, NULL);
1175 hd_free_part(&disk->part0);
1177 blk_put_queue(disk->queue);
1180 struct class block_class = {
1184 static char *block_devnode(struct device *dev, umode_t *mode,
1185 kuid_t *uid, kgid_t *gid)
1187 struct gendisk *disk = dev_to_disk(dev);
1190 return disk->devnode(disk, mode);
1194 static const struct device_type disk_type = {
1196 .groups = disk_attr_groups,
1197 .release = disk_release,
1198 .devnode = block_devnode,
1201 #ifdef CONFIG_PROC_FS
1203 * aggregate disk stat collector. Uses the same stats that the sysfs
1204 * entries do, above, but makes them available through one seq_file.
1206 * The output looks suspiciously like /proc/partitions with a bunch of
1209 static int diskstats_show(struct seq_file *seqf, void *v)
1211 struct gendisk *gp = v;
1212 struct disk_part_iter piter;
1213 struct hd_struct *hd;
1214 char buf[BDEVNAME_SIZE];
1218 if (&disk_to_dev(gp)->kobj.entry == block_class.devices.next)
1219 seq_puts(seqf, "major minor name"
1220 " rio rmerge rsect ruse wio wmerge "
1221 "wsect wuse running use aveq"
1225 disk_part_iter_init(&piter, gp, DISK_PITER_INCL_EMPTY_PART0);
1226 while ((hd = disk_part_iter_next(&piter))) {
1227 cpu = part_stat_lock();
1228 part_round_stats(cpu, hd);
1230 seq_printf(seqf, "%4d %7d %s %lu %lu %lu "
1231 "%u %lu %lu %lu %u %u %u %u\n",
1232 MAJOR(part_devt(hd)), MINOR(part_devt(hd)),
1233 disk_name(gp, hd->partno, buf),
1234 part_stat_read(hd, ios[READ]),
1235 part_stat_read(hd, merges[READ]),
1236 part_stat_read(hd, sectors[READ]),
1237 jiffies_to_msecs(part_stat_read(hd, ticks[READ])),
1238 part_stat_read(hd, ios[WRITE]),
1239 part_stat_read(hd, merges[WRITE]),
1240 part_stat_read(hd, sectors[WRITE]),
1241 jiffies_to_msecs(part_stat_read(hd, ticks[WRITE])),
1243 jiffies_to_msecs(part_stat_read(hd, io_ticks)),
1244 jiffies_to_msecs(part_stat_read(hd, time_in_queue))
1247 disk_part_iter_exit(&piter);
1252 static const struct seq_operations diskstats_op = {
1253 .start = disk_seqf_start,
1254 .next = disk_seqf_next,
1255 .stop = disk_seqf_stop,
1256 .show = diskstats_show
1259 static int diskstats_open(struct inode *inode, struct file *file)
1261 return seq_open(file, &diskstats_op);
1264 static const struct file_operations proc_diskstats_operations = {
1265 .open = diskstats_open,
1267 .llseek = seq_lseek,
1268 .release = seq_release,
1271 static int __init proc_genhd_init(void)
1273 proc_create("diskstats", 0, NULL, &proc_diskstats_operations);
1274 proc_create("partitions", 0, NULL, &proc_partitions_operations);
1277 module_init(proc_genhd_init);
1278 #endif /* CONFIG_PROC_FS */
1280 dev_t blk_lookup_devt(const char *name, int partno)
1282 dev_t devt = MKDEV(0, 0);
1283 struct class_dev_iter iter;
1286 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
1287 while ((dev = class_dev_iter_next(&iter))) {
1288 struct gendisk *disk = dev_to_disk(dev);
1289 struct hd_struct *part;
1291 if (strcmp(dev_name(dev), name))
1294 if (partno < disk->minors) {
1295 /* We need to return the right devno, even
1296 * if the partition doesn't exist yet.
1298 devt = MKDEV(MAJOR(dev->devt),
1299 MINOR(dev->devt) + partno);
1302 part = disk_get_part(disk, partno);
1304 devt = part_devt(part);
1305 disk_put_part(part);
1308 disk_put_part(part);
1310 class_dev_iter_exit(&iter);
1313 EXPORT_SYMBOL(blk_lookup_devt);
1315 struct gendisk *alloc_disk(int minors)
1317 return alloc_disk_node(minors, NUMA_NO_NODE);
1319 EXPORT_SYMBOL(alloc_disk);
1321 struct gendisk *alloc_disk_node(int minors, int node_id)
1323 struct gendisk *disk;
1325 disk = kzalloc_node(sizeof(struct gendisk), GFP_KERNEL, node_id);
1327 if (!init_part_stats(&disk->part0)) {
1331 disk->node_id = node_id;
1332 if (disk_expand_part_tbl(disk, 0)) {
1333 free_part_stats(&disk->part0);
1337 disk->part_tbl->part[0] = &disk->part0;
1340 * set_capacity() and get_capacity() currently don't use
1341 * seqcounter to read/update the part0->nr_sects. Still init
1342 * the counter as we can read the sectors in IO submission
1343 * patch using seqence counters.
1345 * TODO: Ideally set_capacity() and get_capacity() should be
1346 * converted to make use of bd_mutex and sequence counters.
1348 seqcount_init(&disk->part0.nr_sects_seq);
1349 if (hd_ref_init(&disk->part0)) {
1350 hd_free_part(&disk->part0);
1355 disk->minors = minors;
1356 rand_initialize_disk(disk);
1357 disk_to_dev(disk)->class = &block_class;
1358 disk_to_dev(disk)->type = &disk_type;
1359 device_initialize(disk_to_dev(disk));
1363 EXPORT_SYMBOL(alloc_disk_node);
1365 struct kobject *get_disk(struct gendisk *disk)
1367 struct module *owner;
1368 struct kobject *kobj;
1372 owner = disk->fops->owner;
1373 if (owner && !try_module_get(owner))
1375 kobj = kobject_get_unless_zero(&disk_to_dev(disk)->kobj);
1384 EXPORT_SYMBOL(get_disk);
1386 void put_disk(struct gendisk *disk)
1389 kobject_put(&disk_to_dev(disk)->kobj);
1392 EXPORT_SYMBOL(put_disk);
1394 static void set_disk_ro_uevent(struct gendisk *gd, int ro)
1396 char event[] = "DISK_RO=1";
1397 char *envp[] = { event, NULL };
1401 kobject_uevent_env(&disk_to_dev(gd)->kobj, KOBJ_CHANGE, envp);
1404 void set_device_ro(struct block_device *bdev, int flag)
1406 bdev->bd_part->policy = flag;
1409 EXPORT_SYMBOL(set_device_ro);
1411 void set_disk_ro(struct gendisk *disk, int flag)
1413 struct disk_part_iter piter;
1414 struct hd_struct *part;
1416 if (disk->part0.policy != flag) {
1417 set_disk_ro_uevent(disk, flag);
1418 disk->part0.policy = flag;
1421 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_EMPTY);
1422 while ((part = disk_part_iter_next(&piter)))
1423 part->policy = flag;
1424 disk_part_iter_exit(&piter);
1427 EXPORT_SYMBOL(set_disk_ro);
1429 int bdev_read_only(struct block_device *bdev)
1433 return bdev->bd_part->policy;
1436 EXPORT_SYMBOL(bdev_read_only);
1438 int invalidate_partition(struct gendisk *disk, int partno)
1441 struct block_device *bdev = bdget_disk(disk, partno);
1444 res = __invalidate_device(bdev, true);
1450 EXPORT_SYMBOL(invalidate_partition);
1453 * Disk events - monitor disk events like media change and eject request.
1455 struct disk_events {
1456 struct list_head node; /* all disk_event's */
1457 struct gendisk *disk; /* the associated disk */
1460 struct mutex block_mutex; /* protects blocking */
1461 int block; /* event blocking depth */
1462 unsigned int pending; /* events already sent out */
1463 unsigned int clearing; /* events being cleared */
1465 long poll_msecs; /* interval, -1 for default */
1466 struct delayed_work dwork;
1469 static const char *disk_events_strs[] = {
1470 [ilog2(DISK_EVENT_MEDIA_CHANGE)] = "media_change",
1471 [ilog2(DISK_EVENT_EJECT_REQUEST)] = "eject_request",
1474 static char *disk_uevents[] = {
1475 [ilog2(DISK_EVENT_MEDIA_CHANGE)] = "DISK_MEDIA_CHANGE=1",
1476 [ilog2(DISK_EVENT_EJECT_REQUEST)] = "DISK_EJECT_REQUEST=1",
1479 /* list of all disk_events */
1480 static DEFINE_MUTEX(disk_events_mutex);
1481 static LIST_HEAD(disk_events);
1483 /* disable in-kernel polling by default */
1484 static unsigned long disk_events_dfl_poll_msecs;
1486 static unsigned long disk_events_poll_jiffies(struct gendisk *disk)
1488 struct disk_events *ev = disk->ev;
1489 long intv_msecs = 0;
1492 * If device-specific poll interval is set, always use it. If
1493 * the default is being used, poll iff there are events which
1494 * can't be monitored asynchronously.
1496 if (ev->poll_msecs >= 0)
1497 intv_msecs = ev->poll_msecs;
1498 else if (disk->events & ~disk->async_events)
1499 intv_msecs = disk_events_dfl_poll_msecs;
1501 return msecs_to_jiffies(intv_msecs);
1505 * disk_block_events - block and flush disk event checking
1506 * @disk: disk to block events for
1508 * On return from this function, it is guaranteed that event checking
1509 * isn't in progress and won't happen until unblocked by
1510 * disk_unblock_events(). Events blocking is counted and the actual
1511 * unblocking happens after the matching number of unblocks are done.
1513 * Note that this intentionally does not block event checking from
1514 * disk_clear_events().
1519 void disk_block_events(struct gendisk *disk)
1521 struct disk_events *ev = disk->ev;
1522 unsigned long flags;
1529 * Outer mutex ensures that the first blocker completes canceling
1530 * the event work before further blockers are allowed to finish.
1532 mutex_lock(&ev->block_mutex);
1534 spin_lock_irqsave(&ev->lock, flags);
1535 cancel = !ev->block++;
1536 spin_unlock_irqrestore(&ev->lock, flags);
1539 cancel_delayed_work_sync(&disk->ev->dwork);
1541 mutex_unlock(&ev->block_mutex);
1544 static void __disk_unblock_events(struct gendisk *disk, bool check_now)
1546 struct disk_events *ev = disk->ev;
1548 unsigned long flags;
1550 spin_lock_irqsave(&ev->lock, flags);
1552 if (WARN_ON_ONCE(ev->block <= 0))
1558 intv = disk_events_poll_jiffies(disk);
1560 queue_delayed_work(system_freezable_power_efficient_wq,
1563 queue_delayed_work(system_freezable_power_efficient_wq,
1566 spin_unlock_irqrestore(&ev->lock, flags);
1570 * disk_unblock_events - unblock disk event checking
1571 * @disk: disk to unblock events for
1573 * Undo disk_block_events(). When the block count reaches zero, it
1574 * starts events polling if configured.
1577 * Don't care. Safe to call from irq context.
1579 void disk_unblock_events(struct gendisk *disk)
1582 __disk_unblock_events(disk, false);
1586 * disk_flush_events - schedule immediate event checking and flushing
1587 * @disk: disk to check and flush events for
1588 * @mask: events to flush
1590 * Schedule immediate event checking on @disk if not blocked. Events in
1591 * @mask are scheduled to be cleared from the driver. Note that this
1592 * doesn't clear the events from @disk->ev.
1595 * If @mask is non-zero must be called with bdev->bd_mutex held.
1597 void disk_flush_events(struct gendisk *disk, unsigned int mask)
1599 struct disk_events *ev = disk->ev;
1604 spin_lock_irq(&ev->lock);
1605 ev->clearing |= mask;
1607 mod_delayed_work(system_freezable_power_efficient_wq,
1609 spin_unlock_irq(&ev->lock);
1613 * disk_clear_events - synchronously check, clear and return pending events
1614 * @disk: disk to fetch and clear events from
1615 * @mask: mask of events to be fetched and cleared
1617 * Disk events are synchronously checked and pending events in @mask
1618 * are cleared and returned. This ignores the block count.
1623 unsigned int disk_clear_events(struct gendisk *disk, unsigned int mask)
1625 const struct block_device_operations *bdops = disk->fops;
1626 struct disk_events *ev = disk->ev;
1627 unsigned int pending;
1628 unsigned int clearing = mask;
1631 /* for drivers still using the old ->media_changed method */
1632 if ((mask & DISK_EVENT_MEDIA_CHANGE) &&
1633 bdops->media_changed && bdops->media_changed(disk))
1634 return DISK_EVENT_MEDIA_CHANGE;
1638 disk_block_events(disk);
1641 * store the union of mask and ev->clearing on the stack so that the
1642 * race with disk_flush_events does not cause ambiguity (ev->clearing
1643 * can still be modified even if events are blocked).
1645 spin_lock_irq(&ev->lock);
1646 clearing |= ev->clearing;
1648 spin_unlock_irq(&ev->lock);
1650 disk_check_events(ev, &clearing);
1652 * if ev->clearing is not 0, the disk_flush_events got called in the
1653 * middle of this function, so we want to run the workfn without delay.
1655 __disk_unblock_events(disk, ev->clearing ? true : false);
1657 /* then, fetch and clear pending events */
1658 spin_lock_irq(&ev->lock);
1659 pending = ev->pending & mask;
1660 ev->pending &= ~mask;
1661 spin_unlock_irq(&ev->lock);
1662 WARN_ON_ONCE(clearing & mask);
1668 * Separate this part out so that a different pointer for clearing_ptr can be
1669 * passed in for disk_clear_events.
1671 static void disk_events_workfn(struct work_struct *work)
1673 struct delayed_work *dwork = to_delayed_work(work);
1674 struct disk_events *ev = container_of(dwork, struct disk_events, dwork);
1676 disk_check_events(ev, &ev->clearing);
1679 static void disk_check_events(struct disk_events *ev,
1680 unsigned int *clearing_ptr)
1682 struct gendisk *disk = ev->disk;
1683 char *envp[ARRAY_SIZE(disk_uevents) + 1] = { };
1684 unsigned int clearing = *clearing_ptr;
1685 unsigned int events;
1687 int nr_events = 0, i;
1690 events = disk->fops->check_events(disk, clearing);
1692 /* accumulate pending events and schedule next poll if necessary */
1693 spin_lock_irq(&ev->lock);
1695 events &= ~ev->pending;
1696 ev->pending |= events;
1697 *clearing_ptr &= ~clearing;
1699 intv = disk_events_poll_jiffies(disk);
1700 if (!ev->block && intv)
1701 queue_delayed_work(system_freezable_power_efficient_wq,
1704 spin_unlock_irq(&ev->lock);
1707 * Tell userland about new events. Only the events listed in
1708 * @disk->events are reported. Unlisted events are processed the
1709 * same internally but never get reported to userland.
1711 for (i = 0; i < ARRAY_SIZE(disk_uevents); i++)
1712 if (events & disk->events & (1 << i))
1713 envp[nr_events++] = disk_uevents[i];
1716 kobject_uevent_env(&disk_to_dev(disk)->kobj, KOBJ_CHANGE, envp);
1720 * A disk events enabled device has the following sysfs nodes under
1721 * its /sys/block/X/ directory.
1723 * events : list of all supported events
1724 * events_async : list of events which can be detected w/o polling
1725 * events_poll_msecs : polling interval, 0: disable, -1: system default
1727 static ssize_t __disk_events_show(unsigned int events, char *buf)
1729 const char *delim = "";
1733 for (i = 0; i < ARRAY_SIZE(disk_events_strs); i++)
1734 if (events & (1 << i)) {
1735 pos += sprintf(buf + pos, "%s%s",
1736 delim, disk_events_strs[i]);
1740 pos += sprintf(buf + pos, "\n");
1744 static ssize_t disk_events_show(struct device *dev,
1745 struct device_attribute *attr, char *buf)
1747 struct gendisk *disk = dev_to_disk(dev);
1749 return __disk_events_show(disk->events, buf);
1752 static ssize_t disk_events_async_show(struct device *dev,
1753 struct device_attribute *attr, char *buf)
1755 struct gendisk *disk = dev_to_disk(dev);
1757 return __disk_events_show(disk->async_events, buf);
1760 static ssize_t disk_events_poll_msecs_show(struct device *dev,
1761 struct device_attribute *attr,
1764 struct gendisk *disk = dev_to_disk(dev);
1766 return sprintf(buf, "%ld\n", disk->ev->poll_msecs);
1769 static ssize_t disk_events_poll_msecs_store(struct device *dev,
1770 struct device_attribute *attr,
1771 const char *buf, size_t count)
1773 struct gendisk *disk = dev_to_disk(dev);
1776 if (!count || !sscanf(buf, "%ld", &intv))
1779 if (intv < 0 && intv != -1)
1782 disk_block_events(disk);
1783 disk->ev->poll_msecs = intv;
1784 __disk_unblock_events(disk, true);
1789 static const DEVICE_ATTR(events, S_IRUGO, disk_events_show, NULL);
1790 static const DEVICE_ATTR(events_async, S_IRUGO, disk_events_async_show, NULL);
1791 static const DEVICE_ATTR(events_poll_msecs, S_IRUGO|S_IWUSR,
1792 disk_events_poll_msecs_show,
1793 disk_events_poll_msecs_store);
1795 static const struct attribute *disk_events_attrs[] = {
1796 &dev_attr_events.attr,
1797 &dev_attr_events_async.attr,
1798 &dev_attr_events_poll_msecs.attr,
1803 * The default polling interval can be specified by the kernel
1804 * parameter block.events_dfl_poll_msecs which defaults to 0
1805 * (disable). This can also be modified runtime by writing to
1806 * /sys/module/block/events_dfl_poll_msecs.
1808 static int disk_events_set_dfl_poll_msecs(const char *val,
1809 const struct kernel_param *kp)
1811 struct disk_events *ev;
1814 ret = param_set_ulong(val, kp);
1818 mutex_lock(&disk_events_mutex);
1820 list_for_each_entry(ev, &disk_events, node)
1821 disk_flush_events(ev->disk, 0);
1823 mutex_unlock(&disk_events_mutex);
1828 static const struct kernel_param_ops disk_events_dfl_poll_msecs_param_ops = {
1829 .set = disk_events_set_dfl_poll_msecs,
1830 .get = param_get_ulong,
1833 #undef MODULE_PARAM_PREFIX
1834 #define MODULE_PARAM_PREFIX "block."
1836 module_param_cb(events_dfl_poll_msecs, &disk_events_dfl_poll_msecs_param_ops,
1837 &disk_events_dfl_poll_msecs, 0644);
1840 * disk_{alloc|add|del|release}_events - initialize and destroy disk_events.
1842 static void disk_alloc_events(struct gendisk *disk)
1844 struct disk_events *ev;
1846 if (!disk->fops->check_events)
1849 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
1851 pr_warn("%s: failed to initialize events\n", disk->disk_name);
1855 INIT_LIST_HEAD(&ev->node);
1857 spin_lock_init(&ev->lock);
1858 mutex_init(&ev->block_mutex);
1860 ev->poll_msecs = -1;
1861 INIT_DELAYED_WORK(&ev->dwork, disk_events_workfn);
1866 static void disk_add_events(struct gendisk *disk)
1871 /* FIXME: error handling */
1872 if (sysfs_create_files(&disk_to_dev(disk)->kobj, disk_events_attrs) < 0)
1873 pr_warn("%s: failed to create sysfs files for events\n",
1876 mutex_lock(&disk_events_mutex);
1877 list_add_tail(&disk->ev->node, &disk_events);
1878 mutex_unlock(&disk_events_mutex);
1881 * Block count is initialized to 1 and the following initial
1882 * unblock kicks it into action.
1884 __disk_unblock_events(disk, true);
1887 static void disk_del_events(struct gendisk *disk)
1892 disk_block_events(disk);
1894 mutex_lock(&disk_events_mutex);
1895 list_del_init(&disk->ev->node);
1896 mutex_unlock(&disk_events_mutex);
1898 sysfs_remove_files(&disk_to_dev(disk)->kobj, disk_events_attrs);
1901 static void disk_release_events(struct gendisk *disk)
1903 /* the block count should be 1 from disk_del_events() */
1904 WARN_ON_ONCE(disk->ev && disk->ev->block != 1);