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
, capacity
, size
);
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)
165 * disk_part_iter_init - initialize partition iterator
166 * @piter: iterator to initialize
167 * @disk: disk to iterate over
168 * @flags: DISK_PITER_* flags
170 * Initialize @piter so that it iterates over partitions of @disk.
175 void disk_part_iter_init(struct disk_part_iter
*piter
, struct gendisk
*disk
,
180 if (flags
& (DISK_PITER_INCL_PART0
| DISK_PITER_INCL_EMPTY_PART0
))
184 piter
->flags
= flags
;
188 * disk_part_iter_next - proceed iterator to the next partition and return it
189 * @piter: iterator of interest
191 * Proceed @piter to the next partition and return it.
196 struct block_device
*disk_part_iter_next(struct disk_part_iter
*piter
)
198 struct block_device
*part
;
201 /* put the last partition */
202 disk_part_iter_exit(piter
);
205 xa_for_each_start(&piter
->disk
->part_tbl
, idx
, part
, piter
->idx
) {
206 if (!bdev_nr_sectors(part
) &&
207 !(piter
->flags
& DISK_PITER_INCL_EMPTY
) &&
208 !(piter
->flags
& DISK_PITER_INCL_EMPTY_PART0
&&
212 piter
->part
= bdgrab(part
);
215 piter
->idx
= idx
+ 1;
224 * disk_part_iter_exit - finish up partition iteration
225 * @piter: iter of interest
227 * Called when iteration is over. Cleans up @piter.
232 void disk_part_iter_exit(struct disk_part_iter
*piter
)
240 * Can be deleted altogether. Later.
243 #define BLKDEV_MAJOR_HASH_SIZE 255
244 static struct blk_major_name
{
245 struct blk_major_name
*next
;
248 void (*probe
)(dev_t devt
);
249 } *major_names
[BLKDEV_MAJOR_HASH_SIZE
];
250 static DEFINE_MUTEX(major_names_lock
);
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(&major_names_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(&major_names_lock
);
269 #endif /* CONFIG_PROC_FS */
272 * __register_blkdev - register a new block device
274 * @major: the requested major device number [1..BLKDEV_MAJOR_MAX-1]. If
275 * @major = 0, try to allocate any unused major number.
276 * @name: the name of the new block device as a zero terminated string
277 * @probe: allback that is called on access to any minor number of @major
279 * The @name must be unique within the system.
281 * The return value depends on the @major input parameter:
283 * - if a major device number was requested in range [1..BLKDEV_MAJOR_MAX-1]
284 * then the function returns zero on success, or a negative error code
285 * - if any unused major number was requested with @major = 0 parameter
286 * then the return value is the allocated major number in range
287 * [1..BLKDEV_MAJOR_MAX-1] or a negative error code otherwise
289 * See Documentation/admin-guide/devices.txt for the list of allocated
292 * Use register_blkdev instead for any new code.
294 int __register_blkdev(unsigned int major
, const char *name
,
295 void (*probe
)(dev_t devt
))
297 struct blk_major_name
**n
, *p
;
300 mutex_lock(&major_names_lock
);
304 for (index
= ARRAY_SIZE(major_names
)-1; index
> 0; index
--) {
305 if (major_names
[index
] == NULL
)
310 printk("%s: failed to get major for %s\n",
319 if (major
>= BLKDEV_MAJOR_MAX
) {
320 pr_err("%s: major requested (%u) is greater than the maximum (%u) for %s\n",
321 __func__
, major
, BLKDEV_MAJOR_MAX
-1, name
);
327 p
= kmalloc(sizeof(struct blk_major_name
), GFP_KERNEL
);
335 strlcpy(p
->name
, name
, sizeof(p
->name
));
337 index
= major_to_index(major
);
339 for (n
= &major_names
[index
]; *n
; n
= &(*n
)->next
) {
340 if ((*n
)->major
== major
)
349 printk("register_blkdev: cannot get major %u for %s\n",
354 mutex_unlock(&major_names_lock
);
357 EXPORT_SYMBOL(__register_blkdev
);
359 void unregister_blkdev(unsigned int major
, const char *name
)
361 struct blk_major_name
**n
;
362 struct blk_major_name
*p
= NULL
;
363 int index
= major_to_index(major
);
365 mutex_lock(&major_names_lock
);
366 for (n
= &major_names
[index
]; *n
; n
= &(*n
)->next
)
367 if ((*n
)->major
== major
)
369 if (!*n
|| strcmp((*n
)->name
, name
)) {
375 mutex_unlock(&major_names_lock
);
379 EXPORT_SYMBOL(unregister_blkdev
);
382 * blk_mangle_minor - scatter minor numbers apart
383 * @minor: minor number to mangle
385 * Scatter consecutively allocated @minor number apart if MANGLE_DEVT
386 * is enabled. Mangling twice gives the original value.
394 static int blk_mangle_minor(int minor
)
396 #ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT
399 for (i
= 0; i
< MINORBITS
/ 2; i
++) {
400 int low
= minor
& (1 << i
);
401 int high
= minor
& (1 << (MINORBITS
- 1 - i
));
402 int distance
= MINORBITS
- 1 - 2 * i
;
404 minor
^= low
| high
; /* clear both bits */
405 low
<<= distance
; /* swap the positions */
407 minor
|= low
| high
; /* and set */
414 * blk_alloc_devt - allocate a dev_t for a block device
415 * @bdev: block device to allocate dev_t for
416 * @devt: out parameter for resulting dev_t
418 * Allocate a dev_t for block device.
421 * 0 on success, allocated dev_t is returned in *@devt. -errno on
427 int blk_alloc_devt(struct block_device
*bdev
, dev_t
*devt
)
429 struct gendisk
*disk
= bdev
->bd_disk
;
432 /* in consecutive minor range? */
433 if (bdev
->bd_partno
< disk
->minors
) {
434 *devt
= MKDEV(disk
->major
, disk
->first_minor
+ bdev
->bd_partno
);
438 idx
= ida_alloc_range(&ext_devt_ida
, 0, NR_EXT_DEVT
, GFP_KERNEL
);
440 return idx
== -ENOSPC
? -EBUSY
: idx
;
442 *devt
= MKDEV(BLOCK_EXT_MAJOR
, blk_mangle_minor(idx
));
447 * blk_free_devt - free a dev_t
448 * @devt: dev_t to free
450 * Free @devt which was allocated using blk_alloc_devt().
455 void blk_free_devt(dev_t devt
)
457 if (MAJOR(devt
) == BLOCK_EXT_MAJOR
)
458 ida_free(&ext_devt_ida
, blk_mangle_minor(MINOR(devt
)));
461 static char *bdevt_str(dev_t devt
, char *buf
)
463 if (MAJOR(devt
) <= 0xff && MINOR(devt
) <= 0xff) {
464 char tbuf
[BDEVT_SIZE
];
465 snprintf(tbuf
, BDEVT_SIZE
, "%02x%02x", MAJOR(devt
), MINOR(devt
));
466 snprintf(buf
, BDEVT_SIZE
, "%-9s", tbuf
);
468 snprintf(buf
, BDEVT_SIZE
, "%03x:%05x", MAJOR(devt
), MINOR(devt
));
473 void disk_uevent(struct gendisk
*disk
, enum kobject_action action
)
475 struct disk_part_iter piter
;
476 struct block_device
*part
;
478 disk_part_iter_init(&piter
, disk
, DISK_PITER_INCL_EMPTY_PART0
);
479 while ((part
= disk_part_iter_next(&piter
)))
480 kobject_uevent(bdev_kobj(part
), action
);
481 disk_part_iter_exit(&piter
);
483 EXPORT_SYMBOL_GPL(disk_uevent
);
485 static void disk_scan_partitions(struct gendisk
*disk
)
487 struct block_device
*bdev
;
489 if (!get_capacity(disk
) || !disk_part_scan_enabled(disk
))
492 set_bit(GD_NEED_PART_SCAN
, &disk
->state
);
493 bdev
= blkdev_get_by_dev(disk_devt(disk
), FMODE_READ
, NULL
);
495 blkdev_put(bdev
, FMODE_READ
);
498 static void register_disk(struct device
*parent
, struct gendisk
*disk
,
499 const struct attribute_group
**groups
)
501 struct device
*ddev
= disk_to_dev(disk
);
504 ddev
->parent
= parent
;
506 dev_set_name(ddev
, "%s", disk
->disk_name
);
508 /* delay uevents, until we scanned partition table */
509 dev_set_uevent_suppress(ddev
, 1);
512 WARN_ON(ddev
->groups
);
513 ddev
->groups
= groups
;
515 if (device_add(ddev
))
517 if (!sysfs_deprecated
) {
518 err
= sysfs_create_link(block_depr
, &ddev
->kobj
,
519 kobject_name(&ddev
->kobj
));
527 * avoid probable deadlock caused by allocating memory with
528 * GFP_KERNEL in runtime_resume callback of its all ancestor
531 pm_runtime_set_memalloc_noio(ddev
, true);
533 disk
->part0
->bd_holder_dir
=
534 kobject_create_and_add("holders", &ddev
->kobj
);
535 disk
->slave_dir
= kobject_create_and_add("slaves", &ddev
->kobj
);
537 if (disk
->flags
& GENHD_FL_HIDDEN
) {
538 dev_set_uevent_suppress(ddev
, 0);
542 disk_scan_partitions(disk
);
544 /* announce the disk and partitions after all partitions are created */
545 dev_set_uevent_suppress(ddev
, 0);
546 disk_uevent(disk
, KOBJ_ADD
);
548 if (disk
->queue
->backing_dev_info
->dev
) {
549 err
= sysfs_create_link(&ddev
->kobj
,
550 &disk
->queue
->backing_dev_info
->dev
->kobj
,
557 * __device_add_disk - add disk information to kernel list
558 * @parent: parent device for the disk
559 * @disk: per-device partitioning information
560 * @groups: Additional per-device sysfs groups
561 * @register_queue: register the queue if set to true
563 * This function registers the partitioning information in @disk
566 * FIXME: error handling
568 static void __device_add_disk(struct device
*parent
, struct gendisk
*disk
,
569 const struct attribute_group
**groups
,
576 * The disk queue should now be all set with enough information about
577 * the device for the elevator code to pick an adequate default
578 * elevator if one is needed, that is, for devices requesting queue
582 elevator_init_mq(disk
->queue
);
584 /* minors == 0 indicates to use ext devt from part0 and should
585 * be accompanied with EXT_DEVT flag. Make sure all
586 * parameters make sense.
588 WARN_ON(disk
->minors
&& !(disk
->major
|| disk
->first_minor
));
589 WARN_ON(!disk
->minors
&&
590 !(disk
->flags
& (GENHD_FL_EXT_DEVT
| GENHD_FL_HIDDEN
)));
592 disk
->flags
|= GENHD_FL_UP
;
594 retval
= blk_alloc_devt(disk
->part0
, &devt
);
599 disk
->major
= MAJOR(devt
);
600 disk
->first_minor
= MINOR(devt
);
602 disk_alloc_events(disk
);
604 if (disk
->flags
& GENHD_FL_HIDDEN
) {
606 * Don't let hidden disks show up in /proc/partitions,
607 * and don't bother scanning for partitions either.
609 disk
->flags
|= GENHD_FL_SUPPRESS_PARTITION_INFO
;
610 disk
->flags
|= GENHD_FL_NO_PART_SCAN
;
612 struct backing_dev_info
*bdi
= disk
->queue
->backing_dev_info
;
613 struct device
*dev
= disk_to_dev(disk
);
616 /* Register BDI before referencing it from bdev */
618 ret
= bdi_register(bdi
, "%u:%u", MAJOR(devt
), MINOR(devt
));
620 bdi_set_owner(bdi
, dev
);
621 bdev_add(disk
->part0
, devt
);
623 register_disk(parent
, disk
, groups
);
625 blk_register_queue(disk
);
628 * Take an extra ref on queue which will be put on disk_release()
629 * so that it sticks around as long as @disk is there.
631 WARN_ON_ONCE(!blk_get_queue(disk
->queue
));
633 disk_add_events(disk
);
634 blk_integrity_add(disk
);
637 void device_add_disk(struct device
*parent
, struct gendisk
*disk
,
638 const struct attribute_group
**groups
)
641 __device_add_disk(parent
, disk
, groups
, true);
643 EXPORT_SYMBOL(device_add_disk
);
645 void device_add_disk_no_queue_reg(struct device
*parent
, struct gendisk
*disk
)
647 __device_add_disk(parent
, disk
, NULL
, false);
649 EXPORT_SYMBOL(device_add_disk_no_queue_reg
);
651 static void invalidate_partition(struct block_device
*bdev
)
654 __invalidate_device(bdev
, true);
657 * Unhash the bdev inode for this device so that it can't be looked
658 * up any more even if openers still hold references to it.
660 remove_inode_hash(bdev
->bd_inode
);
664 * del_gendisk - remove the gendisk
665 * @disk: the struct gendisk to remove
667 * Removes the gendisk and all its associated resources. This deletes the
668 * partitions associated with the gendisk, and unregisters the associated
671 * This is the counter to the respective __device_add_disk() call.
673 * The final removal of the struct gendisk happens when its refcount reaches 0
674 * with put_disk(), which should be called after del_gendisk(), if
675 * __device_add_disk() was used.
677 * Drivers exist which depend on the release of the gendisk to be synchronous,
678 * it should not be deferred.
682 void del_gendisk(struct gendisk
*disk
)
684 struct disk_part_iter piter
;
685 struct block_device
*part
;
689 if (WARN_ON_ONCE(!disk
->queue
))
692 blk_integrity_del(disk
);
693 disk_del_events(disk
);
696 * Block lookups of the disk until all bdevs are unhashed and the
697 * disk is marked as dead (GENHD_FL_UP cleared).
699 down_write(&bdev_lookup_sem
);
701 /* invalidate stuff */
702 disk_part_iter_init(&piter
, disk
, DISK_PITER_INCL_EMPTY
);
703 while ((part
= disk_part_iter_next(&piter
))) {
704 invalidate_partition(part
);
705 delete_partition(part
);
707 disk_part_iter_exit(&piter
);
709 invalidate_partition(disk
->part0
);
710 set_capacity(disk
, 0);
711 disk
->flags
&= ~GENHD_FL_UP
;
712 up_write(&bdev_lookup_sem
);
714 if (!(disk
->flags
& GENHD_FL_HIDDEN
)) {
715 sysfs_remove_link(&disk_to_dev(disk
)->kobj
, "bdi");
718 * Unregister bdi before releasing device numbers (as they can
719 * get reused and we'd get clashes in sysfs).
721 bdi_unregister(disk
->queue
->backing_dev_info
);
724 blk_unregister_queue(disk
);
726 kobject_put(disk
->part0
->bd_holder_dir
);
727 kobject_put(disk
->slave_dir
);
729 part_stat_set_all(disk
->part0
, 0);
730 disk
->part0
->bd_stamp
= 0;
731 if (!sysfs_deprecated
)
732 sysfs_remove_link(block_depr
, dev_name(disk_to_dev(disk
)));
733 pm_runtime_set_memalloc_noio(disk_to_dev(disk
), false);
734 device_del(disk_to_dev(disk
));
736 EXPORT_SYMBOL(del_gendisk
);
738 /* sysfs access to bad-blocks list. */
739 static ssize_t
disk_badblocks_show(struct device
*dev
,
740 struct device_attribute
*attr
,
743 struct gendisk
*disk
= dev_to_disk(dev
);
746 return sprintf(page
, "\n");
748 return badblocks_show(disk
->bb
, page
, 0);
751 static ssize_t
disk_badblocks_store(struct device
*dev
,
752 struct device_attribute
*attr
,
753 const char *page
, size_t len
)
755 struct gendisk
*disk
= dev_to_disk(dev
);
760 return badblocks_store(disk
->bb
, page
, len
, 0);
763 void blk_request_module(dev_t devt
)
765 unsigned int major
= MAJOR(devt
);
766 struct blk_major_name
**n
;
768 mutex_lock(&major_names_lock
);
769 for (n
= &major_names
[major_to_index(major
)]; *n
; n
= &(*n
)->next
) {
770 if ((*n
)->major
== major
&& (*n
)->probe
) {
772 mutex_unlock(&major_names_lock
);
776 mutex_unlock(&major_names_lock
);
778 if (request_module("block-major-%d-%d", MAJOR(devt
), MINOR(devt
)) > 0)
779 /* Make old-style 2.4 aliases work */
780 request_module("block-major-%d", MAJOR(devt
));
784 * bdget_disk - do bdget() by gendisk and partition number
785 * @disk: gendisk of interest
786 * @partno: partition number
788 * Find partition @partno from @disk, do bdget() on it.
794 * Resulting block_device on success, NULL on failure.
796 struct block_device
*bdget_disk(struct gendisk
*disk
, int partno
)
798 struct block_device
*bdev
= NULL
;
801 bdev
= xa_load(&disk
->part_tbl
, partno
);
802 if (bdev
&& !bdgrab(bdev
))
810 * print a full list of all partitions - intended for places where the root
811 * filesystem can't be mounted and thus to give the victim some idea of what
814 void __init
printk_all_partitions(void)
816 struct class_dev_iter iter
;
819 class_dev_iter_init(&iter
, &block_class
, NULL
, &disk_type
);
820 while ((dev
= class_dev_iter_next(&iter
))) {
821 struct gendisk
*disk
= dev_to_disk(dev
);
822 struct disk_part_iter piter
;
823 struct block_device
*part
;
824 char name_buf
[BDEVNAME_SIZE
];
825 char devt_buf
[BDEVT_SIZE
];
828 * Don't show empty devices or things that have been
831 if (get_capacity(disk
) == 0 ||
832 (disk
->flags
& GENHD_FL_SUPPRESS_PARTITION_INFO
))
836 * Note, unlike /proc/partitions, I am showing the
837 * numbers in hex - the same format as the root=
840 disk_part_iter_init(&piter
, disk
, DISK_PITER_INCL_PART0
);
841 while ((part
= disk_part_iter_next(&piter
))) {
842 bool is_part0
= part
== disk
->part0
;
844 printk("%s%s %10llu %s %s", is_part0
? "" : " ",
845 bdevt_str(part
->bd_dev
, devt_buf
),
846 bdev_nr_sectors(part
) >> 1,
847 disk_name(disk
, part
->bd_partno
, name_buf
),
849 part
->bd_meta_info
->uuid
: "");
851 if (dev
->parent
&& dev
->parent
->driver
)
852 printk(" driver: %s\n",
853 dev
->parent
->driver
->name
);
855 printk(" (driver?)\n");
859 disk_part_iter_exit(&piter
);
861 class_dev_iter_exit(&iter
);
864 #ifdef CONFIG_PROC_FS
866 static void *disk_seqf_start(struct seq_file
*seqf
, loff_t
*pos
)
869 struct class_dev_iter
*iter
;
872 iter
= kmalloc(sizeof(*iter
), GFP_KERNEL
);
874 return ERR_PTR(-ENOMEM
);
876 seqf
->private = iter
;
877 class_dev_iter_init(iter
, &block_class
, NULL
, &disk_type
);
879 dev
= class_dev_iter_next(iter
);
884 return dev_to_disk(dev
);
887 static void *disk_seqf_next(struct seq_file
*seqf
, void *v
, loff_t
*pos
)
892 dev
= class_dev_iter_next(seqf
->private);
894 return dev_to_disk(dev
);
899 static void disk_seqf_stop(struct seq_file
*seqf
, void *v
)
901 struct class_dev_iter
*iter
= seqf
->private;
903 /* stop is called even after start failed :-( */
905 class_dev_iter_exit(iter
);
907 seqf
->private = NULL
;
911 static void *show_partition_start(struct seq_file
*seqf
, loff_t
*pos
)
915 p
= disk_seqf_start(seqf
, pos
);
916 if (!IS_ERR_OR_NULL(p
) && !*pos
)
917 seq_puts(seqf
, "major minor #blocks name\n\n");
921 static int show_partition(struct seq_file
*seqf
, void *v
)
923 struct gendisk
*sgp
= v
;
924 struct disk_part_iter piter
;
925 struct block_device
*part
;
926 char buf
[BDEVNAME_SIZE
];
928 /* Don't show non-partitionable removeable devices or empty devices */
929 if (!get_capacity(sgp
) || (!disk_max_parts(sgp
) &&
930 (sgp
->flags
& GENHD_FL_REMOVABLE
)))
932 if (sgp
->flags
& GENHD_FL_SUPPRESS_PARTITION_INFO
)
935 /* show the full disk and all non-0 size partitions of it */
936 disk_part_iter_init(&piter
, sgp
, DISK_PITER_INCL_PART0
);
937 while ((part
= disk_part_iter_next(&piter
)))
938 seq_printf(seqf
, "%4d %7d %10llu %s\n",
939 MAJOR(part
->bd_dev
), MINOR(part
->bd_dev
),
940 bdev_nr_sectors(part
) >> 1,
941 disk_name(sgp
, part
->bd_partno
, buf
));
942 disk_part_iter_exit(&piter
);
947 static const struct seq_operations partitions_op
= {
948 .start
= show_partition_start
,
949 .next
= disk_seqf_next
,
950 .stop
= disk_seqf_stop
,
951 .show
= show_partition
955 static int __init
genhd_device_init(void)
959 block_class
.dev_kobj
= sysfs_dev_block_kobj
;
960 error
= class_register(&block_class
);
965 register_blkdev(BLOCK_EXT_MAJOR
, "blkext");
967 /* create top-level block dir */
968 if (!sysfs_deprecated
)
969 block_depr
= kobject_create_and_add("block", NULL
);
973 subsys_initcall(genhd_device_init
);
975 static ssize_t
disk_range_show(struct device
*dev
,
976 struct device_attribute
*attr
, char *buf
)
978 struct gendisk
*disk
= dev_to_disk(dev
);
980 return sprintf(buf
, "%d\n", disk
->minors
);
983 static ssize_t
disk_ext_range_show(struct device
*dev
,
984 struct device_attribute
*attr
, char *buf
)
986 struct gendisk
*disk
= dev_to_disk(dev
);
988 return sprintf(buf
, "%d\n", disk_max_parts(disk
));
991 static ssize_t
disk_removable_show(struct device
*dev
,
992 struct device_attribute
*attr
, char *buf
)
994 struct gendisk
*disk
= dev_to_disk(dev
);
996 return sprintf(buf
, "%d\n",
997 (disk
->flags
& GENHD_FL_REMOVABLE
? 1 : 0));
1000 static ssize_t
disk_hidden_show(struct device
*dev
,
1001 struct device_attribute
*attr
, char *buf
)
1003 struct gendisk
*disk
= dev_to_disk(dev
);
1005 return sprintf(buf
, "%d\n",
1006 (disk
->flags
& GENHD_FL_HIDDEN
? 1 : 0));
1009 static ssize_t
disk_ro_show(struct device
*dev
,
1010 struct device_attribute
*attr
, char *buf
)
1012 struct gendisk
*disk
= dev_to_disk(dev
);
1014 return sprintf(buf
, "%d\n", get_disk_ro(disk
) ? 1 : 0);
1017 ssize_t
part_size_show(struct device
*dev
,
1018 struct device_attribute
*attr
, char *buf
)
1020 return sprintf(buf
, "%llu\n", bdev_nr_sectors(dev_to_bdev(dev
)));
1023 ssize_t
part_stat_show(struct device
*dev
,
1024 struct device_attribute
*attr
, char *buf
)
1026 struct block_device
*bdev
= dev_to_bdev(dev
);
1027 struct request_queue
*q
= bdev
->bd_disk
->queue
;
1028 struct disk_stats stat
;
1029 unsigned int inflight
;
1031 part_stat_read_all(bdev
, &stat
);
1033 inflight
= blk_mq_in_flight(q
, bdev
);
1035 inflight
= part_in_flight(bdev
);
1038 "%8lu %8lu %8llu %8u "
1039 "%8lu %8lu %8llu %8u "
1041 "%8lu %8lu %8llu %8u "
1044 stat
.ios
[STAT_READ
],
1045 stat
.merges
[STAT_READ
],
1046 (unsigned long long)stat
.sectors
[STAT_READ
],
1047 (unsigned int)div_u64(stat
.nsecs
[STAT_READ
], NSEC_PER_MSEC
),
1048 stat
.ios
[STAT_WRITE
],
1049 stat
.merges
[STAT_WRITE
],
1050 (unsigned long long)stat
.sectors
[STAT_WRITE
],
1051 (unsigned int)div_u64(stat
.nsecs
[STAT_WRITE
], NSEC_PER_MSEC
),
1053 jiffies_to_msecs(stat
.io_ticks
),
1054 (unsigned int)div_u64(stat
.nsecs
[STAT_READ
] +
1055 stat
.nsecs
[STAT_WRITE
] +
1056 stat
.nsecs
[STAT_DISCARD
] +
1057 stat
.nsecs
[STAT_FLUSH
],
1059 stat
.ios
[STAT_DISCARD
],
1060 stat
.merges
[STAT_DISCARD
],
1061 (unsigned long long)stat
.sectors
[STAT_DISCARD
],
1062 (unsigned int)div_u64(stat
.nsecs
[STAT_DISCARD
], NSEC_PER_MSEC
),
1063 stat
.ios
[STAT_FLUSH
],
1064 (unsigned int)div_u64(stat
.nsecs
[STAT_FLUSH
], NSEC_PER_MSEC
));
1067 ssize_t
part_inflight_show(struct device
*dev
, struct device_attribute
*attr
,
1070 struct block_device
*bdev
= dev_to_bdev(dev
);
1071 struct request_queue
*q
= bdev
->bd_disk
->queue
;
1072 unsigned int inflight
[2];
1075 blk_mq_in_flight_rw(q
, bdev
, inflight
);
1077 part_in_flight_rw(bdev
, inflight
);
1079 return sprintf(buf
, "%8u %8u\n", inflight
[0], inflight
[1]);
1082 static ssize_t
disk_capability_show(struct device
*dev
,
1083 struct device_attribute
*attr
, char *buf
)
1085 struct gendisk
*disk
= dev_to_disk(dev
);
1087 return sprintf(buf
, "%x\n", disk
->flags
);
1090 static ssize_t
disk_alignment_offset_show(struct device
*dev
,
1091 struct device_attribute
*attr
,
1094 struct gendisk
*disk
= dev_to_disk(dev
);
1096 return sprintf(buf
, "%d\n", queue_alignment_offset(disk
->queue
));
1099 static ssize_t
disk_discard_alignment_show(struct device
*dev
,
1100 struct device_attribute
*attr
,
1103 struct gendisk
*disk
= dev_to_disk(dev
);
1105 return sprintf(buf
, "%d\n", queue_discard_alignment(disk
->queue
));
1108 static DEVICE_ATTR(range
, 0444, disk_range_show
, NULL
);
1109 static DEVICE_ATTR(ext_range
, 0444, disk_ext_range_show
, NULL
);
1110 static DEVICE_ATTR(removable
, 0444, disk_removable_show
, NULL
);
1111 static DEVICE_ATTR(hidden
, 0444, disk_hidden_show
, NULL
);
1112 static DEVICE_ATTR(ro
, 0444, disk_ro_show
, NULL
);
1113 static DEVICE_ATTR(size
, 0444, part_size_show
, NULL
);
1114 static DEVICE_ATTR(alignment_offset
, 0444, disk_alignment_offset_show
, NULL
);
1115 static DEVICE_ATTR(discard_alignment
, 0444, disk_discard_alignment_show
, NULL
);
1116 static DEVICE_ATTR(capability
, 0444, disk_capability_show
, NULL
);
1117 static DEVICE_ATTR(stat
, 0444, part_stat_show
, NULL
);
1118 static DEVICE_ATTR(inflight
, 0444, part_inflight_show
, NULL
);
1119 static DEVICE_ATTR(badblocks
, 0644, disk_badblocks_show
, disk_badblocks_store
);
1121 #ifdef CONFIG_FAIL_MAKE_REQUEST
1122 ssize_t
part_fail_show(struct device
*dev
,
1123 struct device_attribute
*attr
, char *buf
)
1125 return sprintf(buf
, "%d\n", dev_to_bdev(dev
)->bd_make_it_fail
);
1128 ssize_t
part_fail_store(struct device
*dev
,
1129 struct device_attribute
*attr
,
1130 const char *buf
, size_t count
)
1134 if (count
> 0 && sscanf(buf
, "%d", &i
) > 0)
1135 dev_to_bdev(dev
)->bd_make_it_fail
= i
;
1140 static struct device_attribute dev_attr_fail
=
1141 __ATTR(make
-it
-fail
, 0644, part_fail_show
, part_fail_store
);
1142 #endif /* CONFIG_FAIL_MAKE_REQUEST */
1144 #ifdef CONFIG_FAIL_IO_TIMEOUT
1145 static struct device_attribute dev_attr_fail_timeout
=
1146 __ATTR(io
-timeout
-fail
, 0644, part_timeout_show
, part_timeout_store
);
1149 static struct attribute
*disk_attrs
[] = {
1150 &dev_attr_range
.attr
,
1151 &dev_attr_ext_range
.attr
,
1152 &dev_attr_removable
.attr
,
1153 &dev_attr_hidden
.attr
,
1155 &dev_attr_size
.attr
,
1156 &dev_attr_alignment_offset
.attr
,
1157 &dev_attr_discard_alignment
.attr
,
1158 &dev_attr_capability
.attr
,
1159 &dev_attr_stat
.attr
,
1160 &dev_attr_inflight
.attr
,
1161 &dev_attr_badblocks
.attr
,
1162 #ifdef CONFIG_FAIL_MAKE_REQUEST
1163 &dev_attr_fail
.attr
,
1165 #ifdef CONFIG_FAIL_IO_TIMEOUT
1166 &dev_attr_fail_timeout
.attr
,
1171 static umode_t
disk_visible(struct kobject
*kobj
, struct attribute
*a
, int n
)
1173 struct device
*dev
= container_of(kobj
, typeof(*dev
), kobj
);
1174 struct gendisk
*disk
= dev_to_disk(dev
);
1176 if (a
== &dev_attr_badblocks
.attr
&& !disk
->bb
)
1181 static struct attribute_group disk_attr_group
= {
1182 .attrs
= disk_attrs
,
1183 .is_visible
= disk_visible
,
1186 static const struct attribute_group
*disk_attr_groups
[] = {
1192 * disk_release - releases all allocated resources of the gendisk
1193 * @dev: the device representing this disk
1195 * This function releases all allocated resources of the gendisk.
1197 * Drivers which used __device_add_disk() have a gendisk with a request_queue
1198 * assigned. Since the request_queue sits on top of the gendisk for these
1199 * drivers we also call blk_put_queue() for them, and we expect the
1200 * request_queue refcount to reach 0 at this point, and so the request_queue
1201 * will also be freed prior to the disk.
1203 * Context: can sleep
1205 static void disk_release(struct device
*dev
)
1207 struct gendisk
*disk
= dev_to_disk(dev
);
1211 blk_free_devt(dev
->devt
);
1212 disk_release_events(disk
);
1213 kfree(disk
->random
);
1214 xa_destroy(&disk
->part_tbl
);
1217 blk_put_queue(disk
->queue
);
1220 struct class block_class
= {
1224 static char *block_devnode(struct device
*dev
, umode_t
*mode
,
1225 kuid_t
*uid
, kgid_t
*gid
)
1227 struct gendisk
*disk
= dev_to_disk(dev
);
1229 if (disk
->fops
->devnode
)
1230 return disk
->fops
->devnode(disk
, mode
);
1234 const struct device_type disk_type
= {
1236 .groups
= disk_attr_groups
,
1237 .release
= disk_release
,
1238 .devnode
= block_devnode
,
1241 #ifdef CONFIG_PROC_FS
1243 * aggregate disk stat collector. Uses the same stats that the sysfs
1244 * entries do, above, but makes them available through one seq_file.
1246 * The output looks suspiciously like /proc/partitions with a bunch of
1249 static int diskstats_show(struct seq_file
*seqf
, void *v
)
1251 struct gendisk
*gp
= v
;
1252 struct disk_part_iter piter
;
1253 struct block_device
*hd
;
1254 char buf
[BDEVNAME_SIZE
];
1255 unsigned int inflight
;
1256 struct disk_stats stat
;
1259 if (&disk_to_dev(gp)->kobj.entry == block_class.devices.next)
1260 seq_puts(seqf, "major minor name"
1261 " rio rmerge rsect ruse wio wmerge "
1262 "wsect wuse running use aveq"
1266 disk_part_iter_init(&piter
, gp
, DISK_PITER_INCL_EMPTY_PART0
);
1267 while ((hd
= disk_part_iter_next(&piter
))) {
1268 part_stat_read_all(hd
, &stat
);
1269 if (queue_is_mq(gp
->queue
))
1270 inflight
= blk_mq_in_flight(gp
->queue
, hd
);
1272 inflight
= part_in_flight(hd
);
1274 seq_printf(seqf
, "%4d %7d %s "
1281 MAJOR(hd
->bd_dev
), MINOR(hd
->bd_dev
),
1282 disk_name(gp
, hd
->bd_partno
, buf
),
1283 stat
.ios
[STAT_READ
],
1284 stat
.merges
[STAT_READ
],
1285 stat
.sectors
[STAT_READ
],
1286 (unsigned int)div_u64(stat
.nsecs
[STAT_READ
],
1288 stat
.ios
[STAT_WRITE
],
1289 stat
.merges
[STAT_WRITE
],
1290 stat
.sectors
[STAT_WRITE
],
1291 (unsigned int)div_u64(stat
.nsecs
[STAT_WRITE
],
1294 jiffies_to_msecs(stat
.io_ticks
),
1295 (unsigned int)div_u64(stat
.nsecs
[STAT_READ
] +
1296 stat
.nsecs
[STAT_WRITE
] +
1297 stat
.nsecs
[STAT_DISCARD
] +
1298 stat
.nsecs
[STAT_FLUSH
],
1300 stat
.ios
[STAT_DISCARD
],
1301 stat
.merges
[STAT_DISCARD
],
1302 stat
.sectors
[STAT_DISCARD
],
1303 (unsigned int)div_u64(stat
.nsecs
[STAT_DISCARD
],
1305 stat
.ios
[STAT_FLUSH
],
1306 (unsigned int)div_u64(stat
.nsecs
[STAT_FLUSH
],
1310 disk_part_iter_exit(&piter
);
1315 static const struct seq_operations diskstats_op
= {
1316 .start
= disk_seqf_start
,
1317 .next
= disk_seqf_next
,
1318 .stop
= disk_seqf_stop
,
1319 .show
= diskstats_show
1322 static int __init
proc_genhd_init(void)
1324 proc_create_seq("diskstats", 0, NULL
, &diskstats_op
);
1325 proc_create_seq("partitions", 0, NULL
, &partitions_op
);
1328 module_init(proc_genhd_init
);
1329 #endif /* CONFIG_PROC_FS */
1331 dev_t
blk_lookup_devt(const char *name
, int partno
)
1333 dev_t devt
= MKDEV(0, 0);
1334 struct class_dev_iter iter
;
1337 class_dev_iter_init(&iter
, &block_class
, NULL
, &disk_type
);
1338 while ((dev
= class_dev_iter_next(&iter
))) {
1339 struct gendisk
*disk
= dev_to_disk(dev
);
1340 struct block_device
*part
;
1342 if (strcmp(dev_name(dev
), name
))
1345 if (partno
< disk
->minors
) {
1346 /* We need to return the right devno, even
1347 * if the partition doesn't exist yet.
1349 devt
= MKDEV(MAJOR(dev
->devt
),
1350 MINOR(dev
->devt
) + partno
);
1353 part
= bdget_disk(disk
, partno
);
1355 devt
= part
->bd_dev
;
1360 class_dev_iter_exit(&iter
);
1364 struct gendisk
*__alloc_disk_node(int minors
, int node_id
)
1366 struct gendisk
*disk
;
1368 if (minors
> DISK_MAX_PARTS
) {
1370 "block: can't allocate more than %d partitions\n",
1372 minors
= DISK_MAX_PARTS
;
1375 disk
= kzalloc_node(sizeof(struct gendisk
), GFP_KERNEL
, node_id
);
1379 disk
->part0
= bdev_alloc(disk
, 0);
1383 disk
->node_id
= node_id
;
1384 xa_init(&disk
->part_tbl
);
1385 if (xa_insert(&disk
->part_tbl
, 0, disk
->part0
, GFP_KERNEL
))
1386 goto out_destroy_part_tbl
;
1388 disk
->minors
= minors
;
1389 rand_initialize_disk(disk
);
1390 disk_to_dev(disk
)->class = &block_class
;
1391 disk_to_dev(disk
)->type
= &disk_type
;
1392 device_initialize(disk_to_dev(disk
));
1395 out_destroy_part_tbl
:
1396 xa_destroy(&disk
->part_tbl
);
1402 EXPORT_SYMBOL(__alloc_disk_node
);
1405 * put_disk - decrements the gendisk refcount
1406 * @disk: the struct gendisk to decrement the refcount for
1408 * This decrements the refcount for the struct gendisk. When this reaches 0
1409 * we'll have disk_release() called.
1411 * Context: Any context, but the last reference must not be dropped from
1414 void put_disk(struct gendisk
*disk
)
1417 put_device(disk_to_dev(disk
));
1419 EXPORT_SYMBOL(put_disk
);
1421 static void set_disk_ro_uevent(struct gendisk
*gd
, int ro
)
1423 char event
[] = "DISK_RO=1";
1424 char *envp
[] = { event
, NULL
};
1428 kobject_uevent_env(&disk_to_dev(gd
)->kobj
, KOBJ_CHANGE
, envp
);
1432 * set_disk_ro - set a gendisk read-only
1433 * @disk: gendisk to operate on
1434 * @read_only: %true to set the disk read-only, %false set the disk read/write
1436 * This function is used to indicate whether a given disk device should have its
1437 * read-only flag set. set_disk_ro() is typically used by device drivers to
1438 * indicate whether the underlying physical device is write-protected.
1440 void set_disk_ro(struct gendisk
*disk
, bool read_only
)
1443 if (test_and_set_bit(GD_READ_ONLY
, &disk
->state
))
1446 if (!test_and_clear_bit(GD_READ_ONLY
, &disk
->state
))
1449 set_disk_ro_uevent(disk
, read_only
);
1451 EXPORT_SYMBOL(set_disk_ro
);
1453 int bdev_read_only(struct block_device
*bdev
)
1455 return bdev
->bd_read_only
|| get_disk_ro(bdev
->bd_disk
);
1457 EXPORT_SYMBOL(bdev_read_only
);
1460 * Disk events - monitor disk events like media change and eject request.
1462 struct disk_events
{
1463 struct list_head node
; /* all disk_event's */
1464 struct gendisk
*disk
; /* the associated disk */
1467 struct mutex block_mutex
; /* protects blocking */
1468 int block
; /* event blocking depth */
1469 unsigned int pending
; /* events already sent out */
1470 unsigned int clearing
; /* events being cleared */
1472 long poll_msecs
; /* interval, -1 for default */
1473 struct delayed_work dwork
;
1476 static const char *disk_events_strs
[] = {
1477 [ilog2(DISK_EVENT_MEDIA_CHANGE
)] = "media_change",
1478 [ilog2(DISK_EVENT_EJECT_REQUEST
)] = "eject_request",
1481 static char *disk_uevents
[] = {
1482 [ilog2(DISK_EVENT_MEDIA_CHANGE
)] = "DISK_MEDIA_CHANGE=1",
1483 [ilog2(DISK_EVENT_EJECT_REQUEST
)] = "DISK_EJECT_REQUEST=1",
1486 /* list of all disk_events */
1487 static DEFINE_MUTEX(disk_events_mutex
);
1488 static LIST_HEAD(disk_events
);
1490 /* disable in-kernel polling by default */
1491 static unsigned long disk_events_dfl_poll_msecs
;
1493 static unsigned long disk_events_poll_jiffies(struct gendisk
*disk
)
1495 struct disk_events
*ev
= disk
->ev
;
1496 long intv_msecs
= 0;
1499 * If device-specific poll interval is set, always use it. If
1500 * the default is being used, poll if the POLL flag is set.
1502 if (ev
->poll_msecs
>= 0)
1503 intv_msecs
= ev
->poll_msecs
;
1504 else if (disk
->event_flags
& DISK_EVENT_FLAG_POLL
)
1505 intv_msecs
= disk_events_dfl_poll_msecs
;
1507 return msecs_to_jiffies(intv_msecs
);
1511 * disk_block_events - block and flush disk event checking
1512 * @disk: disk to block events for
1514 * On return from this function, it is guaranteed that event checking
1515 * isn't in progress and won't happen until unblocked by
1516 * disk_unblock_events(). Events blocking is counted and the actual
1517 * unblocking happens after the matching number of unblocks are done.
1519 * Note that this intentionally does not block event checking from
1520 * disk_clear_events().
1525 void disk_block_events(struct gendisk
*disk
)
1527 struct disk_events
*ev
= disk
->ev
;
1528 unsigned long flags
;
1535 * Outer mutex ensures that the first blocker completes canceling
1536 * the event work before further blockers are allowed to finish.
1538 mutex_lock(&ev
->block_mutex
);
1540 spin_lock_irqsave(&ev
->lock
, flags
);
1541 cancel
= !ev
->block
++;
1542 spin_unlock_irqrestore(&ev
->lock
, flags
);
1545 cancel_delayed_work_sync(&disk
->ev
->dwork
);
1547 mutex_unlock(&ev
->block_mutex
);
1550 static void __disk_unblock_events(struct gendisk
*disk
, bool check_now
)
1552 struct disk_events
*ev
= disk
->ev
;
1554 unsigned long flags
;
1556 spin_lock_irqsave(&ev
->lock
, flags
);
1558 if (WARN_ON_ONCE(ev
->block
<= 0))
1564 intv
= disk_events_poll_jiffies(disk
);
1566 queue_delayed_work(system_freezable_power_efficient_wq
,
1569 queue_delayed_work(system_freezable_power_efficient_wq
,
1572 spin_unlock_irqrestore(&ev
->lock
, flags
);
1576 * disk_unblock_events - unblock disk event checking
1577 * @disk: disk to unblock events for
1579 * Undo disk_block_events(). When the block count reaches zero, it
1580 * starts events polling if configured.
1583 * Don't care. Safe to call from irq context.
1585 void disk_unblock_events(struct gendisk
*disk
)
1588 __disk_unblock_events(disk
, false);
1592 * disk_flush_events - schedule immediate event checking and flushing
1593 * @disk: disk to check and flush events for
1594 * @mask: events to flush
1596 * Schedule immediate event checking on @disk if not blocked. Events in
1597 * @mask are scheduled to be cleared from the driver. Note that this
1598 * doesn't clear the events from @disk->ev.
1601 * If @mask is non-zero must be called with bdev->bd_mutex held.
1603 void disk_flush_events(struct gendisk
*disk
, unsigned int mask
)
1605 struct disk_events
*ev
= disk
->ev
;
1610 spin_lock_irq(&ev
->lock
);
1611 ev
->clearing
|= mask
;
1613 mod_delayed_work(system_freezable_power_efficient_wq
,
1615 spin_unlock_irq(&ev
->lock
);
1619 * disk_clear_events - synchronously check, clear and return pending events
1620 * @disk: disk to fetch and clear events from
1621 * @mask: mask of events to be fetched and cleared
1623 * Disk events are synchronously checked and pending events in @mask
1624 * are cleared and returned. This ignores the block count.
1629 static unsigned int disk_clear_events(struct gendisk
*disk
, unsigned int mask
)
1631 struct disk_events
*ev
= disk
->ev
;
1632 unsigned int pending
;
1633 unsigned int clearing
= mask
;
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 * bdev_check_media_change - check if a removable media has been changed
1669 * @bdev: block device to check
1671 * Check whether a removable media has been changed, and attempt to free all
1672 * dentries and inodes and invalidates all block device page cache entries in
1675 * Returns %true if the block device changed, or %false if not.
1677 bool bdev_check_media_change(struct block_device
*bdev
)
1679 unsigned int events
;
1681 events
= disk_clear_events(bdev
->bd_disk
, DISK_EVENT_MEDIA_CHANGE
|
1682 DISK_EVENT_EJECT_REQUEST
);
1683 if (!(events
& DISK_EVENT_MEDIA_CHANGE
))
1686 if (__invalidate_device(bdev
, true))
1687 pr_warn("VFS: busy inodes on changed media %s\n",
1688 bdev
->bd_disk
->disk_name
);
1689 set_bit(GD_NEED_PART_SCAN
, &bdev
->bd_disk
->state
);
1692 EXPORT_SYMBOL(bdev_check_media_change
);
1695 * Separate this part out so that a different pointer for clearing_ptr can be
1696 * passed in for disk_clear_events.
1698 static void disk_events_workfn(struct work_struct
*work
)
1700 struct delayed_work
*dwork
= to_delayed_work(work
);
1701 struct disk_events
*ev
= container_of(dwork
, struct disk_events
, dwork
);
1703 disk_check_events(ev
, &ev
->clearing
);
1706 static void disk_check_events(struct disk_events
*ev
,
1707 unsigned int *clearing_ptr
)
1709 struct gendisk
*disk
= ev
->disk
;
1710 char *envp
[ARRAY_SIZE(disk_uevents
) + 1] = { };
1711 unsigned int clearing
= *clearing_ptr
;
1712 unsigned int events
;
1714 int nr_events
= 0, i
;
1717 events
= disk
->fops
->check_events(disk
, clearing
);
1719 /* accumulate pending events and schedule next poll if necessary */
1720 spin_lock_irq(&ev
->lock
);
1722 events
&= ~ev
->pending
;
1723 ev
->pending
|= events
;
1724 *clearing_ptr
&= ~clearing
;
1726 intv
= disk_events_poll_jiffies(disk
);
1727 if (!ev
->block
&& intv
)
1728 queue_delayed_work(system_freezable_power_efficient_wq
,
1731 spin_unlock_irq(&ev
->lock
);
1734 * Tell userland about new events. Only the events listed in
1735 * @disk->events are reported, and only if DISK_EVENT_FLAG_UEVENT
1736 * is set. Otherwise, events are processed internally but never
1737 * get reported to userland.
1739 for (i
= 0; i
< ARRAY_SIZE(disk_uevents
); i
++)
1740 if ((events
& disk
->events
& (1 << i
)) &&
1741 (disk
->event_flags
& DISK_EVENT_FLAG_UEVENT
))
1742 envp
[nr_events
++] = disk_uevents
[i
];
1745 kobject_uevent_env(&disk_to_dev(disk
)->kobj
, KOBJ_CHANGE
, envp
);
1749 * A disk events enabled device has the following sysfs nodes under
1750 * its /sys/block/X/ directory.
1752 * events : list of all supported events
1753 * events_async : list of events which can be detected w/o polling
1754 * (always empty, only for backwards compatibility)
1755 * events_poll_msecs : polling interval, 0: disable, -1: system default
1757 static ssize_t
__disk_events_show(unsigned int events
, char *buf
)
1759 const char *delim
= "";
1763 for (i
= 0; i
< ARRAY_SIZE(disk_events_strs
); i
++)
1764 if (events
& (1 << i
)) {
1765 pos
+= sprintf(buf
+ pos
, "%s%s",
1766 delim
, disk_events_strs
[i
]);
1770 pos
+= sprintf(buf
+ pos
, "\n");
1774 static ssize_t
disk_events_show(struct device
*dev
,
1775 struct device_attribute
*attr
, char *buf
)
1777 struct gendisk
*disk
= dev_to_disk(dev
);
1779 if (!(disk
->event_flags
& DISK_EVENT_FLAG_UEVENT
))
1782 return __disk_events_show(disk
->events
, buf
);
1785 static ssize_t
disk_events_async_show(struct device
*dev
,
1786 struct device_attribute
*attr
, char *buf
)
1791 static ssize_t
disk_events_poll_msecs_show(struct device
*dev
,
1792 struct device_attribute
*attr
,
1795 struct gendisk
*disk
= dev_to_disk(dev
);
1798 return sprintf(buf
, "-1\n");
1800 return sprintf(buf
, "%ld\n", disk
->ev
->poll_msecs
);
1803 static ssize_t
disk_events_poll_msecs_store(struct device
*dev
,
1804 struct device_attribute
*attr
,
1805 const char *buf
, size_t count
)
1807 struct gendisk
*disk
= dev_to_disk(dev
);
1810 if (!count
|| !sscanf(buf
, "%ld", &intv
))
1813 if (intv
< 0 && intv
!= -1)
1819 disk_block_events(disk
);
1820 disk
->ev
->poll_msecs
= intv
;
1821 __disk_unblock_events(disk
, true);
1826 static const DEVICE_ATTR(events
, 0444, disk_events_show
, NULL
);
1827 static const DEVICE_ATTR(events_async
, 0444, disk_events_async_show
, NULL
);
1828 static const DEVICE_ATTR(events_poll_msecs
, 0644,
1829 disk_events_poll_msecs_show
,
1830 disk_events_poll_msecs_store
);
1832 static const struct attribute
*disk_events_attrs
[] = {
1833 &dev_attr_events
.attr
,
1834 &dev_attr_events_async
.attr
,
1835 &dev_attr_events_poll_msecs
.attr
,
1840 * The default polling interval can be specified by the kernel
1841 * parameter block.events_dfl_poll_msecs which defaults to 0
1842 * (disable). This can also be modified runtime by writing to
1843 * /sys/module/block/parameters/events_dfl_poll_msecs.
1845 static int disk_events_set_dfl_poll_msecs(const char *val
,
1846 const struct kernel_param
*kp
)
1848 struct disk_events
*ev
;
1851 ret
= param_set_ulong(val
, kp
);
1855 mutex_lock(&disk_events_mutex
);
1857 list_for_each_entry(ev
, &disk_events
, node
)
1858 disk_flush_events(ev
->disk
, 0);
1860 mutex_unlock(&disk_events_mutex
);
1865 static const struct kernel_param_ops disk_events_dfl_poll_msecs_param_ops
= {
1866 .set
= disk_events_set_dfl_poll_msecs
,
1867 .get
= param_get_ulong
,
1870 #undef MODULE_PARAM_PREFIX
1871 #define MODULE_PARAM_PREFIX "block."
1873 module_param_cb(events_dfl_poll_msecs
, &disk_events_dfl_poll_msecs_param_ops
,
1874 &disk_events_dfl_poll_msecs
, 0644);
1877 * disk_{alloc|add|del|release}_events - initialize and destroy disk_events.
1879 static void disk_alloc_events(struct gendisk
*disk
)
1881 struct disk_events
*ev
;
1883 if (!disk
->fops
->check_events
|| !disk
->events
)
1886 ev
= kzalloc(sizeof(*ev
), GFP_KERNEL
);
1888 pr_warn("%s: failed to initialize events\n", disk
->disk_name
);
1892 INIT_LIST_HEAD(&ev
->node
);
1894 spin_lock_init(&ev
->lock
);
1895 mutex_init(&ev
->block_mutex
);
1897 ev
->poll_msecs
= -1;
1898 INIT_DELAYED_WORK(&ev
->dwork
, disk_events_workfn
);
1903 static void disk_add_events(struct gendisk
*disk
)
1905 /* FIXME: error handling */
1906 if (sysfs_create_files(&disk_to_dev(disk
)->kobj
, disk_events_attrs
) < 0)
1907 pr_warn("%s: failed to create sysfs files for events\n",
1913 mutex_lock(&disk_events_mutex
);
1914 list_add_tail(&disk
->ev
->node
, &disk_events
);
1915 mutex_unlock(&disk_events_mutex
);
1918 * Block count is initialized to 1 and the following initial
1919 * unblock kicks it into action.
1921 __disk_unblock_events(disk
, true);
1924 static void disk_del_events(struct gendisk
*disk
)
1927 disk_block_events(disk
);
1929 mutex_lock(&disk_events_mutex
);
1930 list_del_init(&disk
->ev
->node
);
1931 mutex_unlock(&disk_events_mutex
);
1934 sysfs_remove_files(&disk_to_dev(disk
)->kobj
, disk_events_attrs
);
1937 static void disk_release_events(struct gendisk
*disk
)
1939 /* the block count should be 1 from disk_del_events() */
1940 WARN_ON_ONCE(disk
->ev
&& disk
->ev
->block
!= 1);