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
;
50 spin_lock_irqsave(&bdev
->bd_size_lock
, flags
);
51 i_size_write(bdev
->bd_inode
, (loff_t
)sectors
<< SECTOR_SHIFT
);
52 spin_unlock_irqrestore(&bdev
->bd_size_lock
, flags
);
54 EXPORT_SYMBOL(set_capacity
);
57 * Set disk capacity and notify if the size is not currently zero and will not
58 * be set to zero. Returns true if a uevent was sent, otherwise false.
60 bool set_capacity_and_notify(struct gendisk
*disk
, sector_t size
)
62 sector_t capacity
= get_capacity(disk
);
63 char *envp
[] = { "RESIZE=1", NULL
};
65 set_capacity(disk
, size
);
68 * Only print a message and send a uevent if the gendisk is user visible
69 * and alive. This avoids spamming the log and udev when setting the
70 * initial capacity during probing.
72 if (size
== capacity
||
73 (disk
->flags
& (GENHD_FL_UP
| GENHD_FL_HIDDEN
)) != GENHD_FL_UP
)
76 pr_info("%s: detected capacity change from %lld to %lld\n",
77 disk
->disk_name
, capacity
, size
);
80 * Historically we did not send a uevent for changes to/from an empty
83 if (!capacity
|| !size
)
85 kobject_uevent_env(&disk_to_dev(disk
)->kobj
, KOBJ_CHANGE
, envp
);
88 EXPORT_SYMBOL_GPL(set_capacity_and_notify
);
91 * Format the device name of the indicated disk into the supplied buffer and
92 * return a pointer to that same buffer for convenience.
94 char *disk_name(struct gendisk
*hd
, int partno
, char *buf
)
97 snprintf(buf
, BDEVNAME_SIZE
, "%s", hd
->disk_name
);
98 else if (isdigit(hd
->disk_name
[strlen(hd
->disk_name
)-1]))
99 snprintf(buf
, BDEVNAME_SIZE
, "%sp%d", hd
->disk_name
, partno
);
101 snprintf(buf
, BDEVNAME_SIZE
, "%s%d", hd
->disk_name
, partno
);
106 const char *bdevname(struct block_device
*bdev
, char *buf
)
108 return disk_name(bdev
->bd_disk
, bdev
->bd_partno
, buf
);
110 EXPORT_SYMBOL(bdevname
);
112 static void part_stat_read_all(struct block_device
*part
,
113 struct disk_stats
*stat
)
117 memset(stat
, 0, sizeof(struct disk_stats
));
118 for_each_possible_cpu(cpu
) {
119 struct disk_stats
*ptr
= per_cpu_ptr(part
->bd_stats
, cpu
);
122 for (group
= 0; group
< NR_STAT_GROUPS
; group
++) {
123 stat
->nsecs
[group
] += ptr
->nsecs
[group
];
124 stat
->sectors
[group
] += ptr
->sectors
[group
];
125 stat
->ios
[group
] += ptr
->ios
[group
];
126 stat
->merges
[group
] += ptr
->merges
[group
];
129 stat
->io_ticks
+= ptr
->io_ticks
;
133 static unsigned int part_in_flight(struct block_device
*part
)
135 unsigned int inflight
= 0;
138 for_each_possible_cpu(cpu
) {
139 inflight
+= part_stat_local_read_cpu(part
, in_flight
[0], cpu
) +
140 part_stat_local_read_cpu(part
, in_flight
[1], cpu
);
142 if ((int)inflight
< 0)
148 static void part_in_flight_rw(struct block_device
*part
,
149 unsigned int inflight
[2])
155 for_each_possible_cpu(cpu
) {
156 inflight
[0] += part_stat_local_read_cpu(part
, in_flight
[0], cpu
);
157 inflight
[1] += part_stat_local_read_cpu(part
, in_flight
[1], cpu
);
159 if ((int)inflight
[0] < 0)
161 if ((int)inflight
[1] < 0)
166 * disk_part_iter_init - initialize partition iterator
167 * @piter: iterator to initialize
168 * @disk: disk to iterate over
169 * @flags: DISK_PITER_* flags
171 * Initialize @piter so that it iterates over partitions of @disk.
176 void disk_part_iter_init(struct disk_part_iter
*piter
, struct gendisk
*disk
,
181 if (flags
& (DISK_PITER_INCL_PART0
| DISK_PITER_INCL_EMPTY_PART0
))
185 piter
->flags
= flags
;
189 * disk_part_iter_next - proceed iterator to the next partition and return it
190 * @piter: iterator of interest
192 * Proceed @piter to the next partition and return it.
197 struct block_device
*disk_part_iter_next(struct disk_part_iter
*piter
)
199 struct block_device
*part
;
202 /* put the last partition */
203 disk_part_iter_exit(piter
);
206 xa_for_each_start(&piter
->disk
->part_tbl
, idx
, part
, piter
->idx
) {
207 if (!bdev_nr_sectors(part
) &&
208 !(piter
->flags
& DISK_PITER_INCL_EMPTY
) &&
209 !(piter
->flags
& DISK_PITER_INCL_EMPTY_PART0
&&
213 piter
->part
= bdgrab(part
);
216 piter
->idx
= idx
+ 1;
225 * disk_part_iter_exit - finish up partition iteration
226 * @piter: iter of interest
228 * Called when iteration is over. Cleans up @piter.
233 void disk_part_iter_exit(struct disk_part_iter
*piter
)
241 * Can be deleted altogether. Later.
244 #define BLKDEV_MAJOR_HASH_SIZE 255
245 static struct blk_major_name
{
246 struct blk_major_name
*next
;
249 void (*probe
)(dev_t devt
);
250 } *major_names
[BLKDEV_MAJOR_HASH_SIZE
];
251 static DEFINE_MUTEX(major_names_lock
);
253 /* index in the above - for now: assume no multimajor ranges */
254 static inline int major_to_index(unsigned major
)
256 return major
% BLKDEV_MAJOR_HASH_SIZE
;
259 #ifdef CONFIG_PROC_FS
260 void blkdev_show(struct seq_file
*seqf
, off_t offset
)
262 struct blk_major_name
*dp
;
264 mutex_lock(&major_names_lock
);
265 for (dp
= major_names
[major_to_index(offset
)]; dp
; dp
= dp
->next
)
266 if (dp
->major
== offset
)
267 seq_printf(seqf
, "%3d %s\n", dp
->major
, dp
->name
);
268 mutex_unlock(&major_names_lock
);
270 #endif /* CONFIG_PROC_FS */
273 * __register_blkdev - register a new block device
275 * @major: the requested major device number [1..BLKDEV_MAJOR_MAX-1]. If
276 * @major = 0, try to allocate any unused major number.
277 * @name: the name of the new block device as a zero terminated string
278 * @probe: allback that is called on access to any minor number of @major
280 * The @name must be unique within the system.
282 * The return value depends on the @major input parameter:
284 * - if a major device number was requested in range [1..BLKDEV_MAJOR_MAX-1]
285 * then the function returns zero on success, or a negative error code
286 * - if any unused major number was requested with @major = 0 parameter
287 * then the return value is the allocated major number in range
288 * [1..BLKDEV_MAJOR_MAX-1] or a negative error code otherwise
290 * See Documentation/admin-guide/devices.txt for the list of allocated
293 * Use register_blkdev instead for any new code.
295 int __register_blkdev(unsigned int major
, const char *name
,
296 void (*probe
)(dev_t devt
))
298 struct blk_major_name
**n
, *p
;
301 mutex_lock(&major_names_lock
);
305 for (index
= ARRAY_SIZE(major_names
)-1; index
> 0; index
--) {
306 if (major_names
[index
] == NULL
)
311 printk("%s: failed to get major for %s\n",
320 if (major
>= BLKDEV_MAJOR_MAX
) {
321 pr_err("%s: major requested (%u) is greater than the maximum (%u) for %s\n",
322 __func__
, major
, BLKDEV_MAJOR_MAX
-1, name
);
328 p
= kmalloc(sizeof(struct blk_major_name
), GFP_KERNEL
);
336 strlcpy(p
->name
, name
, sizeof(p
->name
));
338 index
= major_to_index(major
);
340 for (n
= &major_names
[index
]; *n
; n
= &(*n
)->next
) {
341 if ((*n
)->major
== major
)
350 printk("register_blkdev: cannot get major %u for %s\n",
355 mutex_unlock(&major_names_lock
);
358 EXPORT_SYMBOL(__register_blkdev
);
360 void unregister_blkdev(unsigned int major
, const char *name
)
362 struct blk_major_name
**n
;
363 struct blk_major_name
*p
= NULL
;
364 int index
= major_to_index(major
);
366 mutex_lock(&major_names_lock
);
367 for (n
= &major_names
[index
]; *n
; n
= &(*n
)->next
)
368 if ((*n
)->major
== major
)
370 if (!*n
|| strcmp((*n
)->name
, name
)) {
376 mutex_unlock(&major_names_lock
);
380 EXPORT_SYMBOL(unregister_blkdev
);
383 * blk_mangle_minor - scatter minor numbers apart
384 * @minor: minor number to mangle
386 * Scatter consecutively allocated @minor number apart if MANGLE_DEVT
387 * is enabled. Mangling twice gives the original value.
395 static int blk_mangle_minor(int minor
)
397 #ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT
400 for (i
= 0; i
< MINORBITS
/ 2; i
++) {
401 int low
= minor
& (1 << i
);
402 int high
= minor
& (1 << (MINORBITS
- 1 - i
));
403 int distance
= MINORBITS
- 1 - 2 * i
;
405 minor
^= low
| high
; /* clear both bits */
406 low
<<= distance
; /* swap the positions */
408 minor
|= low
| high
; /* and set */
415 * blk_alloc_devt - allocate a dev_t for a block device
416 * @bdev: block device to allocate dev_t for
417 * @devt: out parameter for resulting dev_t
419 * Allocate a dev_t for block device.
422 * 0 on success, allocated dev_t is returned in *@devt. -errno on
428 int blk_alloc_devt(struct block_device
*bdev
, dev_t
*devt
)
430 struct gendisk
*disk
= bdev
->bd_disk
;
433 /* in consecutive minor range? */
434 if (bdev
->bd_partno
< disk
->minors
) {
435 *devt
= MKDEV(disk
->major
, disk
->first_minor
+ bdev
->bd_partno
);
439 idx
= ida_alloc_range(&ext_devt_ida
, 0, NR_EXT_DEVT
, GFP_KERNEL
);
441 return idx
== -ENOSPC
? -EBUSY
: idx
;
443 *devt
= MKDEV(BLOCK_EXT_MAJOR
, blk_mangle_minor(idx
));
448 * blk_free_devt - free a dev_t
449 * @devt: dev_t to free
451 * Free @devt which was allocated using blk_alloc_devt().
456 void blk_free_devt(dev_t devt
)
458 if (MAJOR(devt
) == BLOCK_EXT_MAJOR
)
459 ida_free(&ext_devt_ida
, blk_mangle_minor(MINOR(devt
)));
462 static char *bdevt_str(dev_t devt
, char *buf
)
464 if (MAJOR(devt
) <= 0xff && MINOR(devt
) <= 0xff) {
465 char tbuf
[BDEVT_SIZE
];
466 snprintf(tbuf
, BDEVT_SIZE
, "%02x%02x", MAJOR(devt
), MINOR(devt
));
467 snprintf(buf
, BDEVT_SIZE
, "%-9s", tbuf
);
469 snprintf(buf
, BDEVT_SIZE
, "%03x:%05x", MAJOR(devt
), MINOR(devt
));
474 void disk_uevent(struct gendisk
*disk
, enum kobject_action action
)
476 struct disk_part_iter piter
;
477 struct block_device
*part
;
479 disk_part_iter_init(&piter
, disk
, DISK_PITER_INCL_EMPTY_PART0
);
480 while ((part
= disk_part_iter_next(&piter
)))
481 kobject_uevent(bdev_kobj(part
), action
);
482 disk_part_iter_exit(&piter
);
484 EXPORT_SYMBOL_GPL(disk_uevent
);
486 static void disk_scan_partitions(struct gendisk
*disk
)
488 struct block_device
*bdev
;
490 if (!get_capacity(disk
) || !disk_part_scan_enabled(disk
))
493 set_bit(GD_NEED_PART_SCAN
, &disk
->state
);
494 bdev
= blkdev_get_by_dev(disk_devt(disk
), FMODE_READ
, NULL
);
496 blkdev_put(bdev
, FMODE_READ
);
499 static void register_disk(struct device
*parent
, struct gendisk
*disk
,
500 const struct attribute_group
**groups
)
502 struct device
*ddev
= disk_to_dev(disk
);
505 ddev
->parent
= parent
;
507 dev_set_name(ddev
, "%s", disk
->disk_name
);
509 /* delay uevents, until we scanned partition table */
510 dev_set_uevent_suppress(ddev
, 1);
513 WARN_ON(ddev
->groups
);
514 ddev
->groups
= groups
;
516 if (device_add(ddev
))
518 if (!sysfs_deprecated
) {
519 err
= sysfs_create_link(block_depr
, &ddev
->kobj
,
520 kobject_name(&ddev
->kobj
));
528 * avoid probable deadlock caused by allocating memory with
529 * GFP_KERNEL in runtime_resume callback of its all ancestor
532 pm_runtime_set_memalloc_noio(ddev
, true);
534 disk
->part0
->bd_holder_dir
=
535 kobject_create_and_add("holders", &ddev
->kobj
);
536 disk
->slave_dir
= kobject_create_and_add("slaves", &ddev
->kobj
);
538 if (disk
->flags
& GENHD_FL_HIDDEN
) {
539 dev_set_uevent_suppress(ddev
, 0);
543 disk_scan_partitions(disk
);
545 /* announce the disk and partitions after all partitions are created */
546 dev_set_uevent_suppress(ddev
, 0);
547 disk_uevent(disk
, KOBJ_ADD
);
549 if (disk
->queue
->backing_dev_info
->dev
) {
550 err
= sysfs_create_link(&ddev
->kobj
,
551 &disk
->queue
->backing_dev_info
->dev
->kobj
,
558 * __device_add_disk - add disk information to kernel list
559 * @parent: parent device for the disk
560 * @disk: per-device partitioning information
561 * @groups: Additional per-device sysfs groups
562 * @register_queue: register the queue if set to true
564 * This function registers the partitioning information in @disk
567 * FIXME: error handling
569 static void __device_add_disk(struct device
*parent
, struct gendisk
*disk
,
570 const struct attribute_group
**groups
,
577 * The disk queue should now be all set with enough information about
578 * the device for the elevator code to pick an adequate default
579 * elevator if one is needed, that is, for devices requesting queue
583 elevator_init_mq(disk
->queue
);
585 /* minors == 0 indicates to use ext devt from part0 and should
586 * be accompanied with EXT_DEVT flag. Make sure all
587 * parameters make sense.
589 WARN_ON(disk
->minors
&& !(disk
->major
|| disk
->first_minor
));
590 WARN_ON(!disk
->minors
&&
591 !(disk
->flags
& (GENHD_FL_EXT_DEVT
| GENHD_FL_HIDDEN
)));
593 disk
->flags
|= GENHD_FL_UP
;
595 retval
= blk_alloc_devt(disk
->part0
, &devt
);
600 disk
->major
= MAJOR(devt
);
601 disk
->first_minor
= MINOR(devt
);
603 disk_alloc_events(disk
);
605 if (disk
->flags
& GENHD_FL_HIDDEN
) {
607 * Don't let hidden disks show up in /proc/partitions,
608 * and don't bother scanning for partitions either.
610 disk
->flags
|= GENHD_FL_SUPPRESS_PARTITION_INFO
;
611 disk
->flags
|= GENHD_FL_NO_PART_SCAN
;
613 struct backing_dev_info
*bdi
= disk
->queue
->backing_dev_info
;
614 struct device
*dev
= disk_to_dev(disk
);
617 /* Register BDI before referencing it from bdev */
619 ret
= bdi_register(bdi
, "%u:%u", MAJOR(devt
), MINOR(devt
));
621 bdi_set_owner(bdi
, dev
);
622 bdev_add(disk
->part0
, devt
);
624 register_disk(parent
, disk
, groups
);
626 blk_register_queue(disk
);
629 * Take an extra ref on queue which will be put on disk_release()
630 * so that it sticks around as long as @disk is there.
632 WARN_ON_ONCE(!blk_get_queue(disk
->queue
));
634 disk_add_events(disk
);
635 blk_integrity_add(disk
);
638 void device_add_disk(struct device
*parent
, struct gendisk
*disk
,
639 const struct attribute_group
**groups
)
642 __device_add_disk(parent
, disk
, groups
, true);
644 EXPORT_SYMBOL(device_add_disk
);
646 void device_add_disk_no_queue_reg(struct device
*parent
, struct gendisk
*disk
)
648 __device_add_disk(parent
, disk
, NULL
, false);
650 EXPORT_SYMBOL(device_add_disk_no_queue_reg
);
652 static void invalidate_partition(struct block_device
*bdev
)
655 __invalidate_device(bdev
, true);
658 * Unhash the bdev inode for this device so that it can't be looked
659 * up any more even if openers still hold references to it.
661 remove_inode_hash(bdev
->bd_inode
);
665 * del_gendisk - remove the gendisk
666 * @disk: the struct gendisk to remove
668 * Removes the gendisk and all its associated resources. This deletes the
669 * partitions associated with the gendisk, and unregisters the associated
672 * This is the counter to the respective __device_add_disk() call.
674 * The final removal of the struct gendisk happens when its refcount reaches 0
675 * with put_disk(), which should be called after del_gendisk(), if
676 * __device_add_disk() was used.
678 * Drivers exist which depend on the release of the gendisk to be synchronous,
679 * it should not be deferred.
683 void del_gendisk(struct gendisk
*disk
)
685 struct disk_part_iter piter
;
686 struct block_device
*part
;
690 if (WARN_ON_ONCE(!disk
->queue
))
693 blk_integrity_del(disk
);
694 disk_del_events(disk
);
697 * Block lookups of the disk until all bdevs are unhashed and the
698 * disk is marked as dead (GENHD_FL_UP cleared).
700 down_write(&bdev_lookup_sem
);
702 /* invalidate stuff */
703 disk_part_iter_init(&piter
, disk
, DISK_PITER_INCL_EMPTY
);
704 while ((part
= disk_part_iter_next(&piter
))) {
705 invalidate_partition(part
);
706 delete_partition(part
);
708 disk_part_iter_exit(&piter
);
710 invalidate_partition(disk
->part0
);
711 set_capacity(disk
, 0);
712 disk
->flags
&= ~GENHD_FL_UP
;
713 up_write(&bdev_lookup_sem
);
715 if (!(disk
->flags
& GENHD_FL_HIDDEN
)) {
716 sysfs_remove_link(&disk_to_dev(disk
)->kobj
, "bdi");
719 * Unregister bdi before releasing device numbers (as they can
720 * get reused and we'd get clashes in sysfs).
722 bdi_unregister(disk
->queue
->backing_dev_info
);
725 blk_unregister_queue(disk
);
727 kobject_put(disk
->part0
->bd_holder_dir
);
728 kobject_put(disk
->slave_dir
);
730 part_stat_set_all(disk
->part0
, 0);
731 disk
->part0
->bd_stamp
= 0;
732 if (!sysfs_deprecated
)
733 sysfs_remove_link(block_depr
, dev_name(disk_to_dev(disk
)));
734 pm_runtime_set_memalloc_noio(disk_to_dev(disk
), false);
735 device_del(disk_to_dev(disk
));
737 EXPORT_SYMBOL(del_gendisk
);
739 /* sysfs access to bad-blocks list. */
740 static ssize_t
disk_badblocks_show(struct device
*dev
,
741 struct device_attribute
*attr
,
744 struct gendisk
*disk
= dev_to_disk(dev
);
747 return sprintf(page
, "\n");
749 return badblocks_show(disk
->bb
, page
, 0);
752 static ssize_t
disk_badblocks_store(struct device
*dev
,
753 struct device_attribute
*attr
,
754 const char *page
, size_t len
)
756 struct gendisk
*disk
= dev_to_disk(dev
);
761 return badblocks_store(disk
->bb
, page
, len
, 0);
764 void blk_request_module(dev_t devt
)
766 unsigned int major
= MAJOR(devt
);
767 struct blk_major_name
**n
;
769 mutex_lock(&major_names_lock
);
770 for (n
= &major_names
[major_to_index(major
)]; *n
; n
= &(*n
)->next
) {
771 if ((*n
)->major
== major
&& (*n
)->probe
) {
773 mutex_unlock(&major_names_lock
);
777 mutex_unlock(&major_names_lock
);
779 if (request_module("block-major-%d-%d", MAJOR(devt
), MINOR(devt
)) > 0)
780 /* Make old-style 2.4 aliases work */
781 request_module("block-major-%d", MAJOR(devt
));
785 * bdget_disk - do bdget() by gendisk and partition number
786 * @disk: gendisk of interest
787 * @partno: partition number
789 * Find partition @partno from @disk, do bdget() on it.
795 * Resulting block_device on success, NULL on failure.
797 struct block_device
*bdget_disk(struct gendisk
*disk
, int partno
)
799 struct block_device
*bdev
= NULL
;
802 bdev
= xa_load(&disk
->part_tbl
, partno
);
803 if (bdev
&& !bdgrab(bdev
))
811 * print a full list of all partitions - intended for places where the root
812 * filesystem can't be mounted and thus to give the victim some idea of what
815 void __init
printk_all_partitions(void)
817 struct class_dev_iter iter
;
820 class_dev_iter_init(&iter
, &block_class
, NULL
, &disk_type
);
821 while ((dev
= class_dev_iter_next(&iter
))) {
822 struct gendisk
*disk
= dev_to_disk(dev
);
823 struct disk_part_iter piter
;
824 struct block_device
*part
;
825 char name_buf
[BDEVNAME_SIZE
];
826 char devt_buf
[BDEVT_SIZE
];
829 * Don't show empty devices or things that have been
832 if (get_capacity(disk
) == 0 ||
833 (disk
->flags
& GENHD_FL_SUPPRESS_PARTITION_INFO
))
837 * Note, unlike /proc/partitions, I am showing the
838 * numbers in hex - the same format as the root=
841 disk_part_iter_init(&piter
, disk
, DISK_PITER_INCL_PART0
);
842 while ((part
= disk_part_iter_next(&piter
))) {
843 bool is_part0
= part
== disk
->part0
;
845 printk("%s%s %10llu %s %s", is_part0
? "" : " ",
846 bdevt_str(part
->bd_dev
, devt_buf
),
847 bdev_nr_sectors(part
) >> 1,
848 disk_name(disk
, part
->bd_partno
, name_buf
),
850 part
->bd_meta_info
->uuid
: "");
852 if (dev
->parent
&& dev
->parent
->driver
)
853 printk(" driver: %s\n",
854 dev
->parent
->driver
->name
);
856 printk(" (driver?)\n");
860 disk_part_iter_exit(&piter
);
862 class_dev_iter_exit(&iter
);
865 #ifdef CONFIG_PROC_FS
867 static void *disk_seqf_start(struct seq_file
*seqf
, loff_t
*pos
)
870 struct class_dev_iter
*iter
;
873 iter
= kmalloc(sizeof(*iter
), GFP_KERNEL
);
875 return ERR_PTR(-ENOMEM
);
877 seqf
->private = iter
;
878 class_dev_iter_init(iter
, &block_class
, NULL
, &disk_type
);
880 dev
= class_dev_iter_next(iter
);
885 return dev_to_disk(dev
);
888 static void *disk_seqf_next(struct seq_file
*seqf
, void *v
, loff_t
*pos
)
893 dev
= class_dev_iter_next(seqf
->private);
895 return dev_to_disk(dev
);
900 static void disk_seqf_stop(struct seq_file
*seqf
, void *v
)
902 struct class_dev_iter
*iter
= seqf
->private;
904 /* stop is called even after start failed :-( */
906 class_dev_iter_exit(iter
);
908 seqf
->private = NULL
;
912 static void *show_partition_start(struct seq_file
*seqf
, loff_t
*pos
)
916 p
= disk_seqf_start(seqf
, pos
);
917 if (!IS_ERR_OR_NULL(p
) && !*pos
)
918 seq_puts(seqf
, "major minor #blocks name\n\n");
922 static int show_partition(struct seq_file
*seqf
, void *v
)
924 struct gendisk
*sgp
= v
;
925 struct disk_part_iter piter
;
926 struct block_device
*part
;
927 char buf
[BDEVNAME_SIZE
];
929 /* Don't show non-partitionable removeable devices or empty devices */
930 if (!get_capacity(sgp
) || (!disk_max_parts(sgp
) &&
931 (sgp
->flags
& GENHD_FL_REMOVABLE
)))
933 if (sgp
->flags
& GENHD_FL_SUPPRESS_PARTITION_INFO
)
936 /* show the full disk and all non-0 size partitions of it */
937 disk_part_iter_init(&piter
, sgp
, DISK_PITER_INCL_PART0
);
938 while ((part
= disk_part_iter_next(&piter
)))
939 seq_printf(seqf
, "%4d %7d %10llu %s\n",
940 MAJOR(part
->bd_dev
), MINOR(part
->bd_dev
),
941 bdev_nr_sectors(part
) >> 1,
942 disk_name(sgp
, part
->bd_partno
, buf
));
943 disk_part_iter_exit(&piter
);
948 static const struct seq_operations partitions_op
= {
949 .start
= show_partition_start
,
950 .next
= disk_seqf_next
,
951 .stop
= disk_seqf_stop
,
952 .show
= show_partition
956 static int __init
genhd_device_init(void)
960 block_class
.dev_kobj
= sysfs_dev_block_kobj
;
961 error
= class_register(&block_class
);
966 register_blkdev(BLOCK_EXT_MAJOR
, "blkext");
968 /* create top-level block dir */
969 if (!sysfs_deprecated
)
970 block_depr
= kobject_create_and_add("block", NULL
);
974 subsys_initcall(genhd_device_init
);
976 static ssize_t
disk_range_show(struct device
*dev
,
977 struct device_attribute
*attr
, char *buf
)
979 struct gendisk
*disk
= dev_to_disk(dev
);
981 return sprintf(buf
, "%d\n", disk
->minors
);
984 static ssize_t
disk_ext_range_show(struct device
*dev
,
985 struct device_attribute
*attr
, char *buf
)
987 struct gendisk
*disk
= dev_to_disk(dev
);
989 return sprintf(buf
, "%d\n", disk_max_parts(disk
));
992 static ssize_t
disk_removable_show(struct device
*dev
,
993 struct device_attribute
*attr
, char *buf
)
995 struct gendisk
*disk
= dev_to_disk(dev
);
997 return sprintf(buf
, "%d\n",
998 (disk
->flags
& GENHD_FL_REMOVABLE
? 1 : 0));
1001 static ssize_t
disk_hidden_show(struct device
*dev
,
1002 struct device_attribute
*attr
, char *buf
)
1004 struct gendisk
*disk
= dev_to_disk(dev
);
1006 return sprintf(buf
, "%d\n",
1007 (disk
->flags
& GENHD_FL_HIDDEN
? 1 : 0));
1010 static ssize_t
disk_ro_show(struct device
*dev
,
1011 struct device_attribute
*attr
, char *buf
)
1013 struct gendisk
*disk
= dev_to_disk(dev
);
1015 return sprintf(buf
, "%d\n", get_disk_ro(disk
) ? 1 : 0);
1018 ssize_t
part_size_show(struct device
*dev
,
1019 struct device_attribute
*attr
, char *buf
)
1021 return sprintf(buf
, "%llu\n", bdev_nr_sectors(dev_to_bdev(dev
)));
1024 ssize_t
part_stat_show(struct device
*dev
,
1025 struct device_attribute
*attr
, char *buf
)
1027 struct block_device
*bdev
= dev_to_bdev(dev
);
1028 struct request_queue
*q
= bdev
->bd_disk
->queue
;
1029 struct disk_stats stat
;
1030 unsigned int inflight
;
1032 part_stat_read_all(bdev
, &stat
);
1034 inflight
= blk_mq_in_flight(q
, bdev
);
1036 inflight
= part_in_flight(bdev
);
1039 "%8lu %8lu %8llu %8u "
1040 "%8lu %8lu %8llu %8u "
1042 "%8lu %8lu %8llu %8u "
1045 stat
.ios
[STAT_READ
],
1046 stat
.merges
[STAT_READ
],
1047 (unsigned long long)stat
.sectors
[STAT_READ
],
1048 (unsigned int)div_u64(stat
.nsecs
[STAT_READ
], NSEC_PER_MSEC
),
1049 stat
.ios
[STAT_WRITE
],
1050 stat
.merges
[STAT_WRITE
],
1051 (unsigned long long)stat
.sectors
[STAT_WRITE
],
1052 (unsigned int)div_u64(stat
.nsecs
[STAT_WRITE
], NSEC_PER_MSEC
),
1054 jiffies_to_msecs(stat
.io_ticks
),
1055 (unsigned int)div_u64(stat
.nsecs
[STAT_READ
] +
1056 stat
.nsecs
[STAT_WRITE
] +
1057 stat
.nsecs
[STAT_DISCARD
] +
1058 stat
.nsecs
[STAT_FLUSH
],
1060 stat
.ios
[STAT_DISCARD
],
1061 stat
.merges
[STAT_DISCARD
],
1062 (unsigned long long)stat
.sectors
[STAT_DISCARD
],
1063 (unsigned int)div_u64(stat
.nsecs
[STAT_DISCARD
], NSEC_PER_MSEC
),
1064 stat
.ios
[STAT_FLUSH
],
1065 (unsigned int)div_u64(stat
.nsecs
[STAT_FLUSH
], NSEC_PER_MSEC
));
1068 ssize_t
part_inflight_show(struct device
*dev
, struct device_attribute
*attr
,
1071 struct block_device
*bdev
= dev_to_bdev(dev
);
1072 struct request_queue
*q
= bdev
->bd_disk
->queue
;
1073 unsigned int inflight
[2];
1076 blk_mq_in_flight_rw(q
, bdev
, inflight
);
1078 part_in_flight_rw(bdev
, inflight
);
1080 return sprintf(buf
, "%8u %8u\n", inflight
[0], inflight
[1]);
1083 static ssize_t
disk_capability_show(struct device
*dev
,
1084 struct device_attribute
*attr
, char *buf
)
1086 struct gendisk
*disk
= dev_to_disk(dev
);
1088 return sprintf(buf
, "%x\n", disk
->flags
);
1091 static ssize_t
disk_alignment_offset_show(struct device
*dev
,
1092 struct device_attribute
*attr
,
1095 struct gendisk
*disk
= dev_to_disk(dev
);
1097 return sprintf(buf
, "%d\n", queue_alignment_offset(disk
->queue
));
1100 static ssize_t
disk_discard_alignment_show(struct device
*dev
,
1101 struct device_attribute
*attr
,
1104 struct gendisk
*disk
= dev_to_disk(dev
);
1106 return sprintf(buf
, "%d\n", queue_discard_alignment(disk
->queue
));
1109 static DEVICE_ATTR(range
, 0444, disk_range_show
, NULL
);
1110 static DEVICE_ATTR(ext_range
, 0444, disk_ext_range_show
, NULL
);
1111 static DEVICE_ATTR(removable
, 0444, disk_removable_show
, NULL
);
1112 static DEVICE_ATTR(hidden
, 0444, disk_hidden_show
, NULL
);
1113 static DEVICE_ATTR(ro
, 0444, disk_ro_show
, NULL
);
1114 static DEVICE_ATTR(size
, 0444, part_size_show
, NULL
);
1115 static DEVICE_ATTR(alignment_offset
, 0444, disk_alignment_offset_show
, NULL
);
1116 static DEVICE_ATTR(discard_alignment
, 0444, disk_discard_alignment_show
, NULL
);
1117 static DEVICE_ATTR(capability
, 0444, disk_capability_show
, NULL
);
1118 static DEVICE_ATTR(stat
, 0444, part_stat_show
, NULL
);
1119 static DEVICE_ATTR(inflight
, 0444, part_inflight_show
, NULL
);
1120 static DEVICE_ATTR(badblocks
, 0644, disk_badblocks_show
, disk_badblocks_store
);
1122 #ifdef CONFIG_FAIL_MAKE_REQUEST
1123 ssize_t
part_fail_show(struct device
*dev
,
1124 struct device_attribute
*attr
, char *buf
)
1126 return sprintf(buf
, "%d\n", dev_to_bdev(dev
)->bd_make_it_fail
);
1129 ssize_t
part_fail_store(struct device
*dev
,
1130 struct device_attribute
*attr
,
1131 const char *buf
, size_t count
)
1135 if (count
> 0 && sscanf(buf
, "%d", &i
) > 0)
1136 dev_to_bdev(dev
)->bd_make_it_fail
= i
;
1141 static struct device_attribute dev_attr_fail
=
1142 __ATTR(make
-it
-fail
, 0644, part_fail_show
, part_fail_store
);
1143 #endif /* CONFIG_FAIL_MAKE_REQUEST */
1145 #ifdef CONFIG_FAIL_IO_TIMEOUT
1146 static struct device_attribute dev_attr_fail_timeout
=
1147 __ATTR(io
-timeout
-fail
, 0644, part_timeout_show
, part_timeout_store
);
1150 static struct attribute
*disk_attrs
[] = {
1151 &dev_attr_range
.attr
,
1152 &dev_attr_ext_range
.attr
,
1153 &dev_attr_removable
.attr
,
1154 &dev_attr_hidden
.attr
,
1156 &dev_attr_size
.attr
,
1157 &dev_attr_alignment_offset
.attr
,
1158 &dev_attr_discard_alignment
.attr
,
1159 &dev_attr_capability
.attr
,
1160 &dev_attr_stat
.attr
,
1161 &dev_attr_inflight
.attr
,
1162 &dev_attr_badblocks
.attr
,
1163 #ifdef CONFIG_FAIL_MAKE_REQUEST
1164 &dev_attr_fail
.attr
,
1166 #ifdef CONFIG_FAIL_IO_TIMEOUT
1167 &dev_attr_fail_timeout
.attr
,
1172 static umode_t
disk_visible(struct kobject
*kobj
, struct attribute
*a
, int n
)
1174 struct device
*dev
= container_of(kobj
, typeof(*dev
), kobj
);
1175 struct gendisk
*disk
= dev_to_disk(dev
);
1177 if (a
== &dev_attr_badblocks
.attr
&& !disk
->bb
)
1182 static struct attribute_group disk_attr_group
= {
1183 .attrs
= disk_attrs
,
1184 .is_visible
= disk_visible
,
1187 static const struct attribute_group
*disk_attr_groups
[] = {
1193 * disk_release - releases all allocated resources of the gendisk
1194 * @dev: the device representing this disk
1196 * This function releases all allocated resources of the gendisk.
1198 * Drivers which used __device_add_disk() have a gendisk with a request_queue
1199 * assigned. Since the request_queue sits on top of the gendisk for these
1200 * drivers we also call blk_put_queue() for them, and we expect the
1201 * request_queue refcount to reach 0 at this point, and so the request_queue
1202 * will also be freed prior to the disk.
1204 * Context: can sleep
1206 static void disk_release(struct device
*dev
)
1208 struct gendisk
*disk
= dev_to_disk(dev
);
1212 blk_free_devt(dev
->devt
);
1213 disk_release_events(disk
);
1214 kfree(disk
->random
);
1215 xa_destroy(&disk
->part_tbl
);
1218 blk_put_queue(disk
->queue
);
1221 struct class block_class
= {
1225 static char *block_devnode(struct device
*dev
, umode_t
*mode
,
1226 kuid_t
*uid
, kgid_t
*gid
)
1228 struct gendisk
*disk
= dev_to_disk(dev
);
1230 if (disk
->fops
->devnode
)
1231 return disk
->fops
->devnode(disk
, mode
);
1235 const struct device_type disk_type
= {
1237 .groups
= disk_attr_groups
,
1238 .release
= disk_release
,
1239 .devnode
= block_devnode
,
1242 #ifdef CONFIG_PROC_FS
1244 * aggregate disk stat collector. Uses the same stats that the sysfs
1245 * entries do, above, but makes them available through one seq_file.
1247 * The output looks suspiciously like /proc/partitions with a bunch of
1250 static int diskstats_show(struct seq_file
*seqf
, void *v
)
1252 struct gendisk
*gp
= v
;
1253 struct disk_part_iter piter
;
1254 struct block_device
*hd
;
1255 char buf
[BDEVNAME_SIZE
];
1256 unsigned int inflight
;
1257 struct disk_stats stat
;
1260 if (&disk_to_dev(gp)->kobj.entry == block_class.devices.next)
1261 seq_puts(seqf, "major minor name"
1262 " rio rmerge rsect ruse wio wmerge "
1263 "wsect wuse running use aveq"
1267 disk_part_iter_init(&piter
, gp
, DISK_PITER_INCL_EMPTY_PART0
);
1268 while ((hd
= disk_part_iter_next(&piter
))) {
1269 part_stat_read_all(hd
, &stat
);
1270 if (queue_is_mq(gp
->queue
))
1271 inflight
= blk_mq_in_flight(gp
->queue
, hd
);
1273 inflight
= part_in_flight(hd
);
1275 seq_printf(seqf
, "%4d %7d %s "
1282 MAJOR(hd
->bd_dev
), MINOR(hd
->bd_dev
),
1283 disk_name(gp
, hd
->bd_partno
, buf
),
1284 stat
.ios
[STAT_READ
],
1285 stat
.merges
[STAT_READ
],
1286 stat
.sectors
[STAT_READ
],
1287 (unsigned int)div_u64(stat
.nsecs
[STAT_READ
],
1289 stat
.ios
[STAT_WRITE
],
1290 stat
.merges
[STAT_WRITE
],
1291 stat
.sectors
[STAT_WRITE
],
1292 (unsigned int)div_u64(stat
.nsecs
[STAT_WRITE
],
1295 jiffies_to_msecs(stat
.io_ticks
),
1296 (unsigned int)div_u64(stat
.nsecs
[STAT_READ
] +
1297 stat
.nsecs
[STAT_WRITE
] +
1298 stat
.nsecs
[STAT_DISCARD
] +
1299 stat
.nsecs
[STAT_FLUSH
],
1301 stat
.ios
[STAT_DISCARD
],
1302 stat
.merges
[STAT_DISCARD
],
1303 stat
.sectors
[STAT_DISCARD
],
1304 (unsigned int)div_u64(stat
.nsecs
[STAT_DISCARD
],
1306 stat
.ios
[STAT_FLUSH
],
1307 (unsigned int)div_u64(stat
.nsecs
[STAT_FLUSH
],
1311 disk_part_iter_exit(&piter
);
1316 static const struct seq_operations diskstats_op
= {
1317 .start
= disk_seqf_start
,
1318 .next
= disk_seqf_next
,
1319 .stop
= disk_seqf_stop
,
1320 .show
= diskstats_show
1323 static int __init
proc_genhd_init(void)
1325 proc_create_seq("diskstats", 0, NULL
, &diskstats_op
);
1326 proc_create_seq("partitions", 0, NULL
, &partitions_op
);
1329 module_init(proc_genhd_init
);
1330 #endif /* CONFIG_PROC_FS */
1332 dev_t
blk_lookup_devt(const char *name
, int partno
)
1334 dev_t devt
= MKDEV(0, 0);
1335 struct class_dev_iter iter
;
1338 class_dev_iter_init(&iter
, &block_class
, NULL
, &disk_type
);
1339 while ((dev
= class_dev_iter_next(&iter
))) {
1340 struct gendisk
*disk
= dev_to_disk(dev
);
1341 struct block_device
*part
;
1343 if (strcmp(dev_name(dev
), name
))
1346 if (partno
< disk
->minors
) {
1347 /* We need to return the right devno, even
1348 * if the partition doesn't exist yet.
1350 devt
= MKDEV(MAJOR(dev
->devt
),
1351 MINOR(dev
->devt
) + partno
);
1354 part
= bdget_disk(disk
, partno
);
1356 devt
= part
->bd_dev
;
1361 class_dev_iter_exit(&iter
);
1365 struct gendisk
*__alloc_disk_node(int minors
, int node_id
)
1367 struct gendisk
*disk
;
1369 if (minors
> DISK_MAX_PARTS
) {
1371 "block: can't allocate more than %d partitions\n",
1373 minors
= DISK_MAX_PARTS
;
1376 disk
= kzalloc_node(sizeof(struct gendisk
), GFP_KERNEL
, node_id
);
1380 disk
->part0
= bdev_alloc(disk
, 0);
1384 disk
->node_id
= node_id
;
1385 xa_init(&disk
->part_tbl
);
1386 if (xa_insert(&disk
->part_tbl
, 0, disk
->part0
, GFP_KERNEL
))
1387 goto out_destroy_part_tbl
;
1389 disk
->minors
= minors
;
1390 rand_initialize_disk(disk
);
1391 disk_to_dev(disk
)->class = &block_class
;
1392 disk_to_dev(disk
)->type
= &disk_type
;
1393 device_initialize(disk_to_dev(disk
));
1396 out_destroy_part_tbl
:
1397 xa_destroy(&disk
->part_tbl
);
1403 EXPORT_SYMBOL(__alloc_disk_node
);
1406 * put_disk - decrements the gendisk refcount
1407 * @disk: the struct gendisk to decrement the refcount for
1409 * This decrements the refcount for the struct gendisk. When this reaches 0
1410 * we'll have disk_release() called.
1412 * Context: Any context, but the last reference must not be dropped from
1415 void put_disk(struct gendisk
*disk
)
1418 put_device(disk_to_dev(disk
));
1420 EXPORT_SYMBOL(put_disk
);
1422 static void set_disk_ro_uevent(struct gendisk
*gd
, int ro
)
1424 char event
[] = "DISK_RO=1";
1425 char *envp
[] = { event
, NULL
};
1429 kobject_uevent_env(&disk_to_dev(gd
)->kobj
, KOBJ_CHANGE
, envp
);
1433 * set_disk_ro - set a gendisk read-only
1434 * @disk: gendisk to operate on
1435 * @read_only: %true to set the disk read-only, %false set the disk read/write
1437 * This function is used to indicate whether a given disk device should have its
1438 * read-only flag set. set_disk_ro() is typically used by device drivers to
1439 * indicate whether the underlying physical device is write-protected.
1441 void set_disk_ro(struct gendisk
*disk
, bool read_only
)
1444 if (test_and_set_bit(GD_READ_ONLY
, &disk
->state
))
1447 if (!test_and_clear_bit(GD_READ_ONLY
, &disk
->state
))
1450 set_disk_ro_uevent(disk
, read_only
);
1452 EXPORT_SYMBOL(set_disk_ro
);
1454 int bdev_read_only(struct block_device
*bdev
)
1456 return bdev
->bd_read_only
|| get_disk_ro(bdev
->bd_disk
);
1458 EXPORT_SYMBOL(bdev_read_only
);
1461 * Disk events - monitor disk events like media change and eject request.
1463 struct disk_events
{
1464 struct list_head node
; /* all disk_event's */
1465 struct gendisk
*disk
; /* the associated disk */
1468 struct mutex block_mutex
; /* protects blocking */
1469 int block
; /* event blocking depth */
1470 unsigned int pending
; /* events already sent out */
1471 unsigned int clearing
; /* events being cleared */
1473 long poll_msecs
; /* interval, -1 for default */
1474 struct delayed_work dwork
;
1477 static const char *disk_events_strs
[] = {
1478 [ilog2(DISK_EVENT_MEDIA_CHANGE
)] = "media_change",
1479 [ilog2(DISK_EVENT_EJECT_REQUEST
)] = "eject_request",
1482 static char *disk_uevents
[] = {
1483 [ilog2(DISK_EVENT_MEDIA_CHANGE
)] = "DISK_MEDIA_CHANGE=1",
1484 [ilog2(DISK_EVENT_EJECT_REQUEST
)] = "DISK_EJECT_REQUEST=1",
1487 /* list of all disk_events */
1488 static DEFINE_MUTEX(disk_events_mutex
);
1489 static LIST_HEAD(disk_events
);
1491 /* disable in-kernel polling by default */
1492 static unsigned long disk_events_dfl_poll_msecs
;
1494 static unsigned long disk_events_poll_jiffies(struct gendisk
*disk
)
1496 struct disk_events
*ev
= disk
->ev
;
1497 long intv_msecs
= 0;
1500 * If device-specific poll interval is set, always use it. If
1501 * the default is being used, poll if the POLL flag is set.
1503 if (ev
->poll_msecs
>= 0)
1504 intv_msecs
= ev
->poll_msecs
;
1505 else if (disk
->event_flags
& DISK_EVENT_FLAG_POLL
)
1506 intv_msecs
= disk_events_dfl_poll_msecs
;
1508 return msecs_to_jiffies(intv_msecs
);
1512 * disk_block_events - block and flush disk event checking
1513 * @disk: disk to block events for
1515 * On return from this function, it is guaranteed that event checking
1516 * isn't in progress and won't happen until unblocked by
1517 * disk_unblock_events(). Events blocking is counted and the actual
1518 * unblocking happens after the matching number of unblocks are done.
1520 * Note that this intentionally does not block event checking from
1521 * disk_clear_events().
1526 void disk_block_events(struct gendisk
*disk
)
1528 struct disk_events
*ev
= disk
->ev
;
1529 unsigned long flags
;
1536 * Outer mutex ensures that the first blocker completes canceling
1537 * the event work before further blockers are allowed to finish.
1539 mutex_lock(&ev
->block_mutex
);
1541 spin_lock_irqsave(&ev
->lock
, flags
);
1542 cancel
= !ev
->block
++;
1543 spin_unlock_irqrestore(&ev
->lock
, flags
);
1546 cancel_delayed_work_sync(&disk
->ev
->dwork
);
1548 mutex_unlock(&ev
->block_mutex
);
1551 static void __disk_unblock_events(struct gendisk
*disk
, bool check_now
)
1553 struct disk_events
*ev
= disk
->ev
;
1555 unsigned long flags
;
1557 spin_lock_irqsave(&ev
->lock
, flags
);
1559 if (WARN_ON_ONCE(ev
->block
<= 0))
1565 intv
= disk_events_poll_jiffies(disk
);
1567 queue_delayed_work(system_freezable_power_efficient_wq
,
1570 queue_delayed_work(system_freezable_power_efficient_wq
,
1573 spin_unlock_irqrestore(&ev
->lock
, flags
);
1577 * disk_unblock_events - unblock disk event checking
1578 * @disk: disk to unblock events for
1580 * Undo disk_block_events(). When the block count reaches zero, it
1581 * starts events polling if configured.
1584 * Don't care. Safe to call from irq context.
1586 void disk_unblock_events(struct gendisk
*disk
)
1589 __disk_unblock_events(disk
, false);
1593 * disk_flush_events - schedule immediate event checking and flushing
1594 * @disk: disk to check and flush events for
1595 * @mask: events to flush
1597 * Schedule immediate event checking on @disk if not blocked. Events in
1598 * @mask are scheduled to be cleared from the driver. Note that this
1599 * doesn't clear the events from @disk->ev.
1602 * If @mask is non-zero must be called with bdev->bd_mutex held.
1604 void disk_flush_events(struct gendisk
*disk
, unsigned int mask
)
1606 struct disk_events
*ev
= disk
->ev
;
1611 spin_lock_irq(&ev
->lock
);
1612 ev
->clearing
|= mask
;
1614 mod_delayed_work(system_freezable_power_efficient_wq
,
1616 spin_unlock_irq(&ev
->lock
);
1620 * disk_clear_events - synchronously check, clear and return pending events
1621 * @disk: disk to fetch and clear events from
1622 * @mask: mask of events to be fetched and cleared
1624 * Disk events are synchronously checked and pending events in @mask
1625 * are cleared and returned. This ignores the block count.
1630 static unsigned int disk_clear_events(struct gendisk
*disk
, unsigned int mask
)
1632 struct disk_events
*ev
= disk
->ev
;
1633 unsigned int pending
;
1634 unsigned int clearing
= mask
;
1639 disk_block_events(disk
);
1642 * store the union of mask and ev->clearing on the stack so that the
1643 * race with disk_flush_events does not cause ambiguity (ev->clearing
1644 * can still be modified even if events are blocked).
1646 spin_lock_irq(&ev
->lock
);
1647 clearing
|= ev
->clearing
;
1649 spin_unlock_irq(&ev
->lock
);
1651 disk_check_events(ev
, &clearing
);
1653 * if ev->clearing is not 0, the disk_flush_events got called in the
1654 * middle of this function, so we want to run the workfn without delay.
1656 __disk_unblock_events(disk
, ev
->clearing
? true : false);
1658 /* then, fetch and clear pending events */
1659 spin_lock_irq(&ev
->lock
);
1660 pending
= ev
->pending
& mask
;
1661 ev
->pending
&= ~mask
;
1662 spin_unlock_irq(&ev
->lock
);
1663 WARN_ON_ONCE(clearing
& mask
);
1669 * bdev_check_media_change - check if a removable media has been changed
1670 * @bdev: block device to check
1672 * Check whether a removable media has been changed, and attempt to free all
1673 * dentries and inodes and invalidates all block device page cache entries in
1676 * Returns %true if the block device changed, or %false if not.
1678 bool bdev_check_media_change(struct block_device
*bdev
)
1680 unsigned int events
;
1682 events
= disk_clear_events(bdev
->bd_disk
, DISK_EVENT_MEDIA_CHANGE
|
1683 DISK_EVENT_EJECT_REQUEST
);
1684 if (!(events
& DISK_EVENT_MEDIA_CHANGE
))
1687 if (__invalidate_device(bdev
, true))
1688 pr_warn("VFS: busy inodes on changed media %s\n",
1689 bdev
->bd_disk
->disk_name
);
1690 set_bit(GD_NEED_PART_SCAN
, &bdev
->bd_disk
->state
);
1693 EXPORT_SYMBOL(bdev_check_media_change
);
1696 * Separate this part out so that a different pointer for clearing_ptr can be
1697 * passed in for disk_clear_events.
1699 static void disk_events_workfn(struct work_struct
*work
)
1701 struct delayed_work
*dwork
= to_delayed_work(work
);
1702 struct disk_events
*ev
= container_of(dwork
, struct disk_events
, dwork
);
1704 disk_check_events(ev
, &ev
->clearing
);
1707 static void disk_check_events(struct disk_events
*ev
,
1708 unsigned int *clearing_ptr
)
1710 struct gendisk
*disk
= ev
->disk
;
1711 char *envp
[ARRAY_SIZE(disk_uevents
) + 1] = { };
1712 unsigned int clearing
= *clearing_ptr
;
1713 unsigned int events
;
1715 int nr_events
= 0, i
;
1718 events
= disk
->fops
->check_events(disk
, clearing
);
1720 /* accumulate pending events and schedule next poll if necessary */
1721 spin_lock_irq(&ev
->lock
);
1723 events
&= ~ev
->pending
;
1724 ev
->pending
|= events
;
1725 *clearing_ptr
&= ~clearing
;
1727 intv
= disk_events_poll_jiffies(disk
);
1728 if (!ev
->block
&& intv
)
1729 queue_delayed_work(system_freezable_power_efficient_wq
,
1732 spin_unlock_irq(&ev
->lock
);
1735 * Tell userland about new events. Only the events listed in
1736 * @disk->events are reported, and only if DISK_EVENT_FLAG_UEVENT
1737 * is set. Otherwise, events are processed internally but never
1738 * get reported to userland.
1740 for (i
= 0; i
< ARRAY_SIZE(disk_uevents
); i
++)
1741 if ((events
& disk
->events
& (1 << i
)) &&
1742 (disk
->event_flags
& DISK_EVENT_FLAG_UEVENT
))
1743 envp
[nr_events
++] = disk_uevents
[i
];
1746 kobject_uevent_env(&disk_to_dev(disk
)->kobj
, KOBJ_CHANGE
, envp
);
1750 * A disk events enabled device has the following sysfs nodes under
1751 * its /sys/block/X/ directory.
1753 * events : list of all supported events
1754 * events_async : list of events which can be detected w/o polling
1755 * (always empty, only for backwards compatibility)
1756 * events_poll_msecs : polling interval, 0: disable, -1: system default
1758 static ssize_t
__disk_events_show(unsigned int events
, char *buf
)
1760 const char *delim
= "";
1764 for (i
= 0; i
< ARRAY_SIZE(disk_events_strs
); i
++)
1765 if (events
& (1 << i
)) {
1766 pos
+= sprintf(buf
+ pos
, "%s%s",
1767 delim
, disk_events_strs
[i
]);
1771 pos
+= sprintf(buf
+ pos
, "\n");
1775 static ssize_t
disk_events_show(struct device
*dev
,
1776 struct device_attribute
*attr
, char *buf
)
1778 struct gendisk
*disk
= dev_to_disk(dev
);
1780 if (!(disk
->event_flags
& DISK_EVENT_FLAG_UEVENT
))
1783 return __disk_events_show(disk
->events
, buf
);
1786 static ssize_t
disk_events_async_show(struct device
*dev
,
1787 struct device_attribute
*attr
, char *buf
)
1792 static ssize_t
disk_events_poll_msecs_show(struct device
*dev
,
1793 struct device_attribute
*attr
,
1796 struct gendisk
*disk
= dev_to_disk(dev
);
1799 return sprintf(buf
, "-1\n");
1801 return sprintf(buf
, "%ld\n", disk
->ev
->poll_msecs
);
1804 static ssize_t
disk_events_poll_msecs_store(struct device
*dev
,
1805 struct device_attribute
*attr
,
1806 const char *buf
, size_t count
)
1808 struct gendisk
*disk
= dev_to_disk(dev
);
1811 if (!count
|| !sscanf(buf
, "%ld", &intv
))
1814 if (intv
< 0 && intv
!= -1)
1820 disk_block_events(disk
);
1821 disk
->ev
->poll_msecs
= intv
;
1822 __disk_unblock_events(disk
, true);
1827 static const DEVICE_ATTR(events
, 0444, disk_events_show
, NULL
);
1828 static const DEVICE_ATTR(events_async
, 0444, disk_events_async_show
, NULL
);
1829 static const DEVICE_ATTR(events_poll_msecs
, 0644,
1830 disk_events_poll_msecs_show
,
1831 disk_events_poll_msecs_store
);
1833 static const struct attribute
*disk_events_attrs
[] = {
1834 &dev_attr_events
.attr
,
1835 &dev_attr_events_async
.attr
,
1836 &dev_attr_events_poll_msecs
.attr
,
1841 * The default polling interval can be specified by the kernel
1842 * parameter block.events_dfl_poll_msecs which defaults to 0
1843 * (disable). This can also be modified runtime by writing to
1844 * /sys/module/block/parameters/events_dfl_poll_msecs.
1846 static int disk_events_set_dfl_poll_msecs(const char *val
,
1847 const struct kernel_param
*kp
)
1849 struct disk_events
*ev
;
1852 ret
= param_set_ulong(val
, kp
);
1856 mutex_lock(&disk_events_mutex
);
1858 list_for_each_entry(ev
, &disk_events
, node
)
1859 disk_flush_events(ev
->disk
, 0);
1861 mutex_unlock(&disk_events_mutex
);
1866 static const struct kernel_param_ops disk_events_dfl_poll_msecs_param_ops
= {
1867 .set
= disk_events_set_dfl_poll_msecs
,
1868 .get
= param_get_ulong
,
1871 #undef MODULE_PARAM_PREFIX
1872 #define MODULE_PARAM_PREFIX "block."
1874 module_param_cb(events_dfl_poll_msecs
, &disk_events_dfl_poll_msecs_param_ops
,
1875 &disk_events_dfl_poll_msecs
, 0644);
1878 * disk_{alloc|add|del|release}_events - initialize and destroy disk_events.
1880 static void disk_alloc_events(struct gendisk
*disk
)
1882 struct disk_events
*ev
;
1884 if (!disk
->fops
->check_events
|| !disk
->events
)
1887 ev
= kzalloc(sizeof(*ev
), GFP_KERNEL
);
1889 pr_warn("%s: failed to initialize events\n", disk
->disk_name
);
1893 INIT_LIST_HEAD(&ev
->node
);
1895 spin_lock_init(&ev
->lock
);
1896 mutex_init(&ev
->block_mutex
);
1898 ev
->poll_msecs
= -1;
1899 INIT_DELAYED_WORK(&ev
->dwork
, disk_events_workfn
);
1904 static void disk_add_events(struct gendisk
*disk
)
1906 /* FIXME: error handling */
1907 if (sysfs_create_files(&disk_to_dev(disk
)->kobj
, disk_events_attrs
) < 0)
1908 pr_warn("%s: failed to create sysfs files for events\n",
1914 mutex_lock(&disk_events_mutex
);
1915 list_add_tail(&disk
->ev
->node
, &disk_events
);
1916 mutex_unlock(&disk_events_mutex
);
1919 * Block count is initialized to 1 and the following initial
1920 * unblock kicks it into action.
1922 __disk_unblock_events(disk
, true);
1925 static void disk_del_events(struct gendisk
*disk
)
1928 disk_block_events(disk
);
1930 mutex_lock(&disk_events_mutex
);
1931 list_del_init(&disk
->ev
->node
);
1932 mutex_unlock(&disk_events_mutex
);
1935 sysfs_remove_files(&disk_to_dev(disk
)->kobj
, disk_events_attrs
);
1938 static void disk_release_events(struct gendisk
*disk
)
1940 /* the block count should be 1 from disk_del_events() */
1941 WARN_ON_ONCE(disk
->ev
&& disk
->ev
->block
!= 1);