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 * Can be deleted altogether. Later.
168 #define BLKDEV_MAJOR_HASH_SIZE 255
169 static struct blk_major_name
{
170 struct blk_major_name
*next
;
173 void (*probe
)(dev_t devt
);
174 } *major_names
[BLKDEV_MAJOR_HASH_SIZE
];
175 static DEFINE_MUTEX(major_names_lock
);
177 /* index in the above - for now: assume no multimajor ranges */
178 static inline int major_to_index(unsigned major
)
180 return major
% BLKDEV_MAJOR_HASH_SIZE
;
183 #ifdef CONFIG_PROC_FS
184 void blkdev_show(struct seq_file
*seqf
, off_t offset
)
186 struct blk_major_name
*dp
;
188 mutex_lock(&major_names_lock
);
189 for (dp
= major_names
[major_to_index(offset
)]; dp
; dp
= dp
->next
)
190 if (dp
->major
== offset
)
191 seq_printf(seqf
, "%3d %s\n", dp
->major
, dp
->name
);
192 mutex_unlock(&major_names_lock
);
194 #endif /* CONFIG_PROC_FS */
197 * __register_blkdev - register a new block device
199 * @major: the requested major device number [1..BLKDEV_MAJOR_MAX-1]. If
200 * @major = 0, try to allocate any unused major number.
201 * @name: the name of the new block device as a zero terminated string
202 * @probe: allback that is called on access to any minor number of @major
204 * The @name must be unique within the system.
206 * The return value depends on the @major input parameter:
208 * - if a major device number was requested in range [1..BLKDEV_MAJOR_MAX-1]
209 * then the function returns zero on success, or a negative error code
210 * - if any unused major number was requested with @major = 0 parameter
211 * then the return value is the allocated major number in range
212 * [1..BLKDEV_MAJOR_MAX-1] or a negative error code otherwise
214 * See Documentation/admin-guide/devices.txt for the list of allocated
217 * Use register_blkdev instead for any new code.
219 int __register_blkdev(unsigned int major
, const char *name
,
220 void (*probe
)(dev_t devt
))
222 struct blk_major_name
**n
, *p
;
225 mutex_lock(&major_names_lock
);
229 for (index
= ARRAY_SIZE(major_names
)-1; index
> 0; index
--) {
230 if (major_names
[index
] == NULL
)
235 printk("%s: failed to get major for %s\n",
244 if (major
>= BLKDEV_MAJOR_MAX
) {
245 pr_err("%s: major requested (%u) is greater than the maximum (%u) for %s\n",
246 __func__
, major
, BLKDEV_MAJOR_MAX
-1, name
);
252 p
= kmalloc(sizeof(struct blk_major_name
), GFP_KERNEL
);
260 strlcpy(p
->name
, name
, sizeof(p
->name
));
262 index
= major_to_index(major
);
264 for (n
= &major_names
[index
]; *n
; n
= &(*n
)->next
) {
265 if ((*n
)->major
== major
)
274 printk("register_blkdev: cannot get major %u for %s\n",
279 mutex_unlock(&major_names_lock
);
282 EXPORT_SYMBOL(__register_blkdev
);
284 void unregister_blkdev(unsigned int major
, const char *name
)
286 struct blk_major_name
**n
;
287 struct blk_major_name
*p
= NULL
;
288 int index
= major_to_index(major
);
290 mutex_lock(&major_names_lock
);
291 for (n
= &major_names
[index
]; *n
; n
= &(*n
)->next
)
292 if ((*n
)->major
== major
)
294 if (!*n
|| strcmp((*n
)->name
, name
)) {
300 mutex_unlock(&major_names_lock
);
304 EXPORT_SYMBOL(unregister_blkdev
);
307 * blk_mangle_minor - scatter minor numbers apart
308 * @minor: minor number to mangle
310 * Scatter consecutively allocated @minor number apart if MANGLE_DEVT
311 * is enabled. Mangling twice gives the original value.
319 static int blk_mangle_minor(int minor
)
321 #ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT
324 for (i
= 0; i
< MINORBITS
/ 2; i
++) {
325 int low
= minor
& (1 << i
);
326 int high
= minor
& (1 << (MINORBITS
- 1 - i
));
327 int distance
= MINORBITS
- 1 - 2 * i
;
329 minor
^= low
| high
; /* clear both bits */
330 low
<<= distance
; /* swap the positions */
332 minor
|= low
| high
; /* and set */
339 * blk_alloc_devt - allocate a dev_t for a block device
340 * @bdev: block device to allocate dev_t for
341 * @devt: out parameter for resulting dev_t
343 * Allocate a dev_t for block device.
346 * 0 on success, allocated dev_t is returned in *@devt. -errno on
352 int blk_alloc_devt(struct block_device
*bdev
, dev_t
*devt
)
354 struct gendisk
*disk
= bdev
->bd_disk
;
357 /* in consecutive minor range? */
358 if (bdev
->bd_partno
< disk
->minors
) {
359 *devt
= MKDEV(disk
->major
, disk
->first_minor
+ bdev
->bd_partno
);
363 idx
= ida_alloc_range(&ext_devt_ida
, 0, NR_EXT_DEVT
, GFP_KERNEL
);
365 return idx
== -ENOSPC
? -EBUSY
: idx
;
367 *devt
= MKDEV(BLOCK_EXT_MAJOR
, blk_mangle_minor(idx
));
372 * blk_free_devt - free a dev_t
373 * @devt: dev_t to free
375 * Free @devt which was allocated using blk_alloc_devt().
380 void blk_free_devt(dev_t devt
)
382 if (MAJOR(devt
) == BLOCK_EXT_MAJOR
)
383 ida_free(&ext_devt_ida
, blk_mangle_minor(MINOR(devt
)));
386 static char *bdevt_str(dev_t devt
, char *buf
)
388 if (MAJOR(devt
) <= 0xff && MINOR(devt
) <= 0xff) {
389 char tbuf
[BDEVT_SIZE
];
390 snprintf(tbuf
, BDEVT_SIZE
, "%02x%02x", MAJOR(devt
), MINOR(devt
));
391 snprintf(buf
, BDEVT_SIZE
, "%-9s", tbuf
);
393 snprintf(buf
, BDEVT_SIZE
, "%03x:%05x", MAJOR(devt
), MINOR(devt
));
398 void disk_uevent(struct gendisk
*disk
, enum kobject_action action
)
400 struct block_device
*part
;
404 xa_for_each(&disk
->part_tbl
, idx
, part
) {
405 if (bdev_is_partition(part
) && !bdev_nr_sectors(part
))
411 kobject_uevent(bdev_kobj(part
), action
);
417 EXPORT_SYMBOL_GPL(disk_uevent
);
419 static void disk_scan_partitions(struct gendisk
*disk
)
421 struct block_device
*bdev
;
423 if (!get_capacity(disk
) || !disk_part_scan_enabled(disk
))
426 set_bit(GD_NEED_PART_SCAN
, &disk
->state
);
427 bdev
= blkdev_get_by_dev(disk_devt(disk
), FMODE_READ
, NULL
);
429 blkdev_put(bdev
, FMODE_READ
);
432 static void register_disk(struct device
*parent
, struct gendisk
*disk
,
433 const struct attribute_group
**groups
)
435 struct device
*ddev
= disk_to_dev(disk
);
438 ddev
->parent
= parent
;
440 dev_set_name(ddev
, "%s", disk
->disk_name
);
442 /* delay uevents, until we scanned partition table */
443 dev_set_uevent_suppress(ddev
, 1);
446 WARN_ON(ddev
->groups
);
447 ddev
->groups
= groups
;
449 if (device_add(ddev
))
451 if (!sysfs_deprecated
) {
452 err
= sysfs_create_link(block_depr
, &ddev
->kobj
,
453 kobject_name(&ddev
->kobj
));
461 * avoid probable deadlock caused by allocating memory with
462 * GFP_KERNEL in runtime_resume callback of its all ancestor
465 pm_runtime_set_memalloc_noio(ddev
, true);
467 disk
->part0
->bd_holder_dir
=
468 kobject_create_and_add("holders", &ddev
->kobj
);
469 disk
->slave_dir
= kobject_create_and_add("slaves", &ddev
->kobj
);
471 if (disk
->flags
& GENHD_FL_HIDDEN
)
474 disk_scan_partitions(disk
);
476 /* announce the disk and partitions after all partitions are created */
477 dev_set_uevent_suppress(ddev
, 0);
478 disk_uevent(disk
, KOBJ_ADD
);
480 if (disk
->queue
->backing_dev_info
->dev
) {
481 err
= sysfs_create_link(&ddev
->kobj
,
482 &disk
->queue
->backing_dev_info
->dev
->kobj
,
489 * __device_add_disk - add disk information to kernel list
490 * @parent: parent device for the disk
491 * @disk: per-device partitioning information
492 * @groups: Additional per-device sysfs groups
493 * @register_queue: register the queue if set to true
495 * This function registers the partitioning information in @disk
498 * FIXME: error handling
500 static void __device_add_disk(struct device
*parent
, struct gendisk
*disk
,
501 const struct attribute_group
**groups
,
508 * The disk queue should now be all set with enough information about
509 * the device for the elevator code to pick an adequate default
510 * elevator if one is needed, that is, for devices requesting queue
514 elevator_init_mq(disk
->queue
);
516 /* minors == 0 indicates to use ext devt from part0 and should
517 * be accompanied with EXT_DEVT flag. Make sure all
518 * parameters make sense.
520 WARN_ON(disk
->minors
&& !(disk
->major
|| disk
->first_minor
));
521 WARN_ON(!disk
->minors
&&
522 !(disk
->flags
& (GENHD_FL_EXT_DEVT
| GENHD_FL_HIDDEN
)));
524 disk
->flags
|= GENHD_FL_UP
;
526 retval
= blk_alloc_devt(disk
->part0
, &devt
);
531 disk
->major
= MAJOR(devt
);
532 disk
->first_minor
= MINOR(devt
);
534 disk_alloc_events(disk
);
536 if (disk
->flags
& GENHD_FL_HIDDEN
) {
538 * Don't let hidden disks show up in /proc/partitions,
539 * and don't bother scanning for partitions either.
541 disk
->flags
|= GENHD_FL_SUPPRESS_PARTITION_INFO
;
542 disk
->flags
|= GENHD_FL_NO_PART_SCAN
;
544 struct backing_dev_info
*bdi
= disk
->queue
->backing_dev_info
;
545 struct device
*dev
= disk_to_dev(disk
);
548 /* Register BDI before referencing it from bdev */
550 ret
= bdi_register(bdi
, "%u:%u", MAJOR(devt
), MINOR(devt
));
552 bdi_set_owner(bdi
, dev
);
553 bdev_add(disk
->part0
, devt
);
555 register_disk(parent
, disk
, groups
);
557 blk_register_queue(disk
);
560 * Take an extra ref on queue which will be put on disk_release()
561 * so that it sticks around as long as @disk is there.
563 WARN_ON_ONCE(!blk_get_queue(disk
->queue
));
565 disk_add_events(disk
);
566 blk_integrity_add(disk
);
569 void device_add_disk(struct device
*parent
, struct gendisk
*disk
,
570 const struct attribute_group
**groups
)
573 __device_add_disk(parent
, disk
, groups
, true);
575 EXPORT_SYMBOL(device_add_disk
);
577 void device_add_disk_no_queue_reg(struct device
*parent
, struct gendisk
*disk
)
579 __device_add_disk(parent
, disk
, NULL
, false);
581 EXPORT_SYMBOL(device_add_disk_no_queue_reg
);
584 * del_gendisk - remove the gendisk
585 * @disk: the struct gendisk to remove
587 * Removes the gendisk and all its associated resources. This deletes the
588 * partitions associated with the gendisk, and unregisters the associated
591 * This is the counter to the respective __device_add_disk() call.
593 * The final removal of the struct gendisk happens when its refcount reaches 0
594 * with put_disk(), which should be called after del_gendisk(), if
595 * __device_add_disk() was used.
597 * Drivers exist which depend on the release of the gendisk to be synchronous,
598 * it should not be deferred.
602 void del_gendisk(struct gendisk
*disk
)
606 if (WARN_ON_ONCE(!disk
->queue
))
609 blk_integrity_del(disk
);
610 disk_del_events(disk
);
613 * Block lookups of the disk until all bdevs are unhashed and the
614 * disk is marked as dead (GENHD_FL_UP cleared).
616 down_write(&bdev_lookup_sem
);
618 mutex_lock(&disk
->part0
->bd_mutex
);
619 blk_drop_partitions(disk
);
620 mutex_unlock(&disk
->part0
->bd_mutex
);
622 fsync_bdev(disk
->part0
);
623 __invalidate_device(disk
->part0
, true);
626 * Unhash the bdev inode for this device so that it can't be looked
627 * up any more even if openers still hold references to it.
629 remove_inode_hash(disk
->part0
->bd_inode
);
631 set_capacity(disk
, 0);
632 disk
->flags
&= ~GENHD_FL_UP
;
633 up_write(&bdev_lookup_sem
);
635 if (!(disk
->flags
& GENHD_FL_HIDDEN
)) {
636 sysfs_remove_link(&disk_to_dev(disk
)->kobj
, "bdi");
639 * Unregister bdi before releasing device numbers (as they can
640 * get reused and we'd get clashes in sysfs).
642 bdi_unregister(disk
->queue
->backing_dev_info
);
645 blk_unregister_queue(disk
);
647 kobject_put(disk
->part0
->bd_holder_dir
);
648 kobject_put(disk
->slave_dir
);
650 part_stat_set_all(disk
->part0
, 0);
651 disk
->part0
->bd_stamp
= 0;
652 if (!sysfs_deprecated
)
653 sysfs_remove_link(block_depr
, dev_name(disk_to_dev(disk
)));
654 pm_runtime_set_memalloc_noio(disk_to_dev(disk
), false);
655 device_del(disk_to_dev(disk
));
657 EXPORT_SYMBOL(del_gendisk
);
659 /* sysfs access to bad-blocks list. */
660 static ssize_t
disk_badblocks_show(struct device
*dev
,
661 struct device_attribute
*attr
,
664 struct gendisk
*disk
= dev_to_disk(dev
);
667 return sprintf(page
, "\n");
669 return badblocks_show(disk
->bb
, page
, 0);
672 static ssize_t
disk_badblocks_store(struct device
*dev
,
673 struct device_attribute
*attr
,
674 const char *page
, size_t len
)
676 struct gendisk
*disk
= dev_to_disk(dev
);
681 return badblocks_store(disk
->bb
, page
, len
, 0);
684 void blk_request_module(dev_t devt
)
686 unsigned int major
= MAJOR(devt
);
687 struct blk_major_name
**n
;
689 mutex_lock(&major_names_lock
);
690 for (n
= &major_names
[major_to_index(major
)]; *n
; n
= &(*n
)->next
) {
691 if ((*n
)->major
== major
&& (*n
)->probe
) {
693 mutex_unlock(&major_names_lock
);
697 mutex_unlock(&major_names_lock
);
699 if (request_module("block-major-%d-%d", MAJOR(devt
), MINOR(devt
)) > 0)
700 /* Make old-style 2.4 aliases work */
701 request_module("block-major-%d", MAJOR(devt
));
705 * bdget_disk - do bdget() by gendisk and partition number
706 * @disk: gendisk of interest
707 * @partno: partition number
709 * Find partition @partno from @disk, do bdget() on it.
715 * Resulting block_device on success, NULL on failure.
717 struct block_device
*bdget_disk(struct gendisk
*disk
, int partno
)
719 struct block_device
*bdev
= NULL
;
722 bdev
= xa_load(&disk
->part_tbl
, partno
);
723 if (bdev
&& !bdgrab(bdev
))
731 * print a full list of all partitions - intended for places where the root
732 * filesystem can't be mounted and thus to give the victim some idea of what
735 void __init
printk_all_partitions(void)
737 struct class_dev_iter iter
;
740 class_dev_iter_init(&iter
, &block_class
, NULL
, &disk_type
);
741 while ((dev
= class_dev_iter_next(&iter
))) {
742 struct gendisk
*disk
= dev_to_disk(dev
);
743 struct block_device
*part
;
744 char name_buf
[BDEVNAME_SIZE
];
745 char devt_buf
[BDEVT_SIZE
];
749 * Don't show empty devices or things that have been
752 if (get_capacity(disk
) == 0 ||
753 (disk
->flags
& GENHD_FL_SUPPRESS_PARTITION_INFO
))
757 * Note, unlike /proc/partitions, I am showing the numbers in
758 * hex - the same format as the root= option takes.
761 xa_for_each(&disk
->part_tbl
, idx
, part
) {
762 if (!bdev_nr_sectors(part
))
764 printk("%s%s %10llu %s %s",
765 bdev_is_partition(part
) ? " " : "",
766 bdevt_str(part
->bd_dev
, devt_buf
),
767 bdev_nr_sectors(part
) >> 1,
768 disk_name(disk
, part
->bd_partno
, name_buf
),
770 part
->bd_meta_info
->uuid
: "");
771 if (bdev_is_partition(part
))
773 else if (dev
->parent
&& dev
->parent
->driver
)
774 printk(" driver: %s\n",
775 dev
->parent
->driver
->name
);
777 printk(" (driver?)\n");
781 class_dev_iter_exit(&iter
);
784 #ifdef CONFIG_PROC_FS
786 static void *disk_seqf_start(struct seq_file
*seqf
, loff_t
*pos
)
789 struct class_dev_iter
*iter
;
792 iter
= kmalloc(sizeof(*iter
), GFP_KERNEL
);
794 return ERR_PTR(-ENOMEM
);
796 seqf
->private = iter
;
797 class_dev_iter_init(iter
, &block_class
, NULL
, &disk_type
);
799 dev
= class_dev_iter_next(iter
);
804 return dev_to_disk(dev
);
807 static void *disk_seqf_next(struct seq_file
*seqf
, void *v
, loff_t
*pos
)
812 dev
= class_dev_iter_next(seqf
->private);
814 return dev_to_disk(dev
);
819 static void disk_seqf_stop(struct seq_file
*seqf
, void *v
)
821 struct class_dev_iter
*iter
= seqf
->private;
823 /* stop is called even after start failed :-( */
825 class_dev_iter_exit(iter
);
827 seqf
->private = NULL
;
831 static void *show_partition_start(struct seq_file
*seqf
, loff_t
*pos
)
835 p
= disk_seqf_start(seqf
, pos
);
836 if (!IS_ERR_OR_NULL(p
) && !*pos
)
837 seq_puts(seqf
, "major minor #blocks name\n\n");
841 static int show_partition(struct seq_file
*seqf
, void *v
)
843 struct gendisk
*sgp
= v
;
844 struct block_device
*part
;
846 char buf
[BDEVNAME_SIZE
];
848 /* Don't show non-partitionable removeable devices or empty devices */
849 if (!get_capacity(sgp
) || (!disk_max_parts(sgp
) &&
850 (sgp
->flags
& GENHD_FL_REMOVABLE
)))
852 if (sgp
->flags
& GENHD_FL_SUPPRESS_PARTITION_INFO
)
856 xa_for_each(&sgp
->part_tbl
, idx
, part
) {
857 if (!bdev_nr_sectors(part
))
859 seq_printf(seqf
, "%4d %7d %10llu %s\n",
860 MAJOR(part
->bd_dev
), MINOR(part
->bd_dev
),
861 bdev_nr_sectors(part
) >> 1,
862 disk_name(sgp
, part
->bd_partno
, buf
));
868 static const struct seq_operations partitions_op
= {
869 .start
= show_partition_start
,
870 .next
= disk_seqf_next
,
871 .stop
= disk_seqf_stop
,
872 .show
= show_partition
876 static int __init
genhd_device_init(void)
880 block_class
.dev_kobj
= sysfs_dev_block_kobj
;
881 error
= class_register(&block_class
);
886 register_blkdev(BLOCK_EXT_MAJOR
, "blkext");
888 /* create top-level block dir */
889 if (!sysfs_deprecated
)
890 block_depr
= kobject_create_and_add("block", NULL
);
894 subsys_initcall(genhd_device_init
);
896 static ssize_t
disk_range_show(struct device
*dev
,
897 struct device_attribute
*attr
, char *buf
)
899 struct gendisk
*disk
= dev_to_disk(dev
);
901 return sprintf(buf
, "%d\n", disk
->minors
);
904 static ssize_t
disk_ext_range_show(struct device
*dev
,
905 struct device_attribute
*attr
, char *buf
)
907 struct gendisk
*disk
= dev_to_disk(dev
);
909 return sprintf(buf
, "%d\n", disk_max_parts(disk
));
912 static ssize_t
disk_removable_show(struct device
*dev
,
913 struct device_attribute
*attr
, char *buf
)
915 struct gendisk
*disk
= dev_to_disk(dev
);
917 return sprintf(buf
, "%d\n",
918 (disk
->flags
& GENHD_FL_REMOVABLE
? 1 : 0));
921 static ssize_t
disk_hidden_show(struct device
*dev
,
922 struct device_attribute
*attr
, char *buf
)
924 struct gendisk
*disk
= dev_to_disk(dev
);
926 return sprintf(buf
, "%d\n",
927 (disk
->flags
& GENHD_FL_HIDDEN
? 1 : 0));
930 static ssize_t
disk_ro_show(struct device
*dev
,
931 struct device_attribute
*attr
, char *buf
)
933 struct gendisk
*disk
= dev_to_disk(dev
);
935 return sprintf(buf
, "%d\n", get_disk_ro(disk
) ? 1 : 0);
938 ssize_t
part_size_show(struct device
*dev
,
939 struct device_attribute
*attr
, char *buf
)
941 return sprintf(buf
, "%llu\n", bdev_nr_sectors(dev_to_bdev(dev
)));
944 ssize_t
part_stat_show(struct device
*dev
,
945 struct device_attribute
*attr
, char *buf
)
947 struct block_device
*bdev
= dev_to_bdev(dev
);
948 struct request_queue
*q
= bdev
->bd_disk
->queue
;
949 struct disk_stats stat
;
950 unsigned int inflight
;
952 part_stat_read_all(bdev
, &stat
);
954 inflight
= blk_mq_in_flight(q
, bdev
);
956 inflight
= part_in_flight(bdev
);
959 "%8lu %8lu %8llu %8u "
960 "%8lu %8lu %8llu %8u "
962 "%8lu %8lu %8llu %8u "
966 stat
.merges
[STAT_READ
],
967 (unsigned long long)stat
.sectors
[STAT_READ
],
968 (unsigned int)div_u64(stat
.nsecs
[STAT_READ
], NSEC_PER_MSEC
),
969 stat
.ios
[STAT_WRITE
],
970 stat
.merges
[STAT_WRITE
],
971 (unsigned long long)stat
.sectors
[STAT_WRITE
],
972 (unsigned int)div_u64(stat
.nsecs
[STAT_WRITE
], NSEC_PER_MSEC
),
974 jiffies_to_msecs(stat
.io_ticks
),
975 (unsigned int)div_u64(stat
.nsecs
[STAT_READ
] +
976 stat
.nsecs
[STAT_WRITE
] +
977 stat
.nsecs
[STAT_DISCARD
] +
978 stat
.nsecs
[STAT_FLUSH
],
980 stat
.ios
[STAT_DISCARD
],
981 stat
.merges
[STAT_DISCARD
],
982 (unsigned long long)stat
.sectors
[STAT_DISCARD
],
983 (unsigned int)div_u64(stat
.nsecs
[STAT_DISCARD
], NSEC_PER_MSEC
),
984 stat
.ios
[STAT_FLUSH
],
985 (unsigned int)div_u64(stat
.nsecs
[STAT_FLUSH
], NSEC_PER_MSEC
));
988 ssize_t
part_inflight_show(struct device
*dev
, struct device_attribute
*attr
,
991 struct block_device
*bdev
= dev_to_bdev(dev
);
992 struct request_queue
*q
= bdev
->bd_disk
->queue
;
993 unsigned int inflight
[2];
996 blk_mq_in_flight_rw(q
, bdev
, inflight
);
998 part_in_flight_rw(bdev
, inflight
);
1000 return sprintf(buf
, "%8u %8u\n", inflight
[0], inflight
[1]);
1003 static ssize_t
disk_capability_show(struct device
*dev
,
1004 struct device_attribute
*attr
, char *buf
)
1006 struct gendisk
*disk
= dev_to_disk(dev
);
1008 return sprintf(buf
, "%x\n", disk
->flags
);
1011 static ssize_t
disk_alignment_offset_show(struct device
*dev
,
1012 struct device_attribute
*attr
,
1015 struct gendisk
*disk
= dev_to_disk(dev
);
1017 return sprintf(buf
, "%d\n", queue_alignment_offset(disk
->queue
));
1020 static ssize_t
disk_discard_alignment_show(struct device
*dev
,
1021 struct device_attribute
*attr
,
1024 struct gendisk
*disk
= dev_to_disk(dev
);
1026 return sprintf(buf
, "%d\n", queue_discard_alignment(disk
->queue
));
1029 static DEVICE_ATTR(range
, 0444, disk_range_show
, NULL
);
1030 static DEVICE_ATTR(ext_range
, 0444, disk_ext_range_show
, NULL
);
1031 static DEVICE_ATTR(removable
, 0444, disk_removable_show
, NULL
);
1032 static DEVICE_ATTR(hidden
, 0444, disk_hidden_show
, NULL
);
1033 static DEVICE_ATTR(ro
, 0444, disk_ro_show
, NULL
);
1034 static DEVICE_ATTR(size
, 0444, part_size_show
, NULL
);
1035 static DEVICE_ATTR(alignment_offset
, 0444, disk_alignment_offset_show
, NULL
);
1036 static DEVICE_ATTR(discard_alignment
, 0444, disk_discard_alignment_show
, NULL
);
1037 static DEVICE_ATTR(capability
, 0444, disk_capability_show
, NULL
);
1038 static DEVICE_ATTR(stat
, 0444, part_stat_show
, NULL
);
1039 static DEVICE_ATTR(inflight
, 0444, part_inflight_show
, NULL
);
1040 static DEVICE_ATTR(badblocks
, 0644, disk_badblocks_show
, disk_badblocks_store
);
1042 #ifdef CONFIG_FAIL_MAKE_REQUEST
1043 ssize_t
part_fail_show(struct device
*dev
,
1044 struct device_attribute
*attr
, char *buf
)
1046 return sprintf(buf
, "%d\n", dev_to_bdev(dev
)->bd_make_it_fail
);
1049 ssize_t
part_fail_store(struct device
*dev
,
1050 struct device_attribute
*attr
,
1051 const char *buf
, size_t count
)
1055 if (count
> 0 && sscanf(buf
, "%d", &i
) > 0)
1056 dev_to_bdev(dev
)->bd_make_it_fail
= i
;
1061 static struct device_attribute dev_attr_fail
=
1062 __ATTR(make
-it
-fail
, 0644, part_fail_show
, part_fail_store
);
1063 #endif /* CONFIG_FAIL_MAKE_REQUEST */
1065 #ifdef CONFIG_FAIL_IO_TIMEOUT
1066 static struct device_attribute dev_attr_fail_timeout
=
1067 __ATTR(io
-timeout
-fail
, 0644, part_timeout_show
, part_timeout_store
);
1070 static struct attribute
*disk_attrs
[] = {
1071 &dev_attr_range
.attr
,
1072 &dev_attr_ext_range
.attr
,
1073 &dev_attr_removable
.attr
,
1074 &dev_attr_hidden
.attr
,
1076 &dev_attr_size
.attr
,
1077 &dev_attr_alignment_offset
.attr
,
1078 &dev_attr_discard_alignment
.attr
,
1079 &dev_attr_capability
.attr
,
1080 &dev_attr_stat
.attr
,
1081 &dev_attr_inflight
.attr
,
1082 &dev_attr_badblocks
.attr
,
1083 #ifdef CONFIG_FAIL_MAKE_REQUEST
1084 &dev_attr_fail
.attr
,
1086 #ifdef CONFIG_FAIL_IO_TIMEOUT
1087 &dev_attr_fail_timeout
.attr
,
1092 static umode_t
disk_visible(struct kobject
*kobj
, struct attribute
*a
, int n
)
1094 struct device
*dev
= container_of(kobj
, typeof(*dev
), kobj
);
1095 struct gendisk
*disk
= dev_to_disk(dev
);
1097 if (a
== &dev_attr_badblocks
.attr
&& !disk
->bb
)
1102 static struct attribute_group disk_attr_group
= {
1103 .attrs
= disk_attrs
,
1104 .is_visible
= disk_visible
,
1107 static const struct attribute_group
*disk_attr_groups
[] = {
1113 * disk_release - releases all allocated resources of the gendisk
1114 * @dev: the device representing this disk
1116 * This function releases all allocated resources of the gendisk.
1118 * Drivers which used __device_add_disk() have a gendisk with a request_queue
1119 * assigned. Since the request_queue sits on top of the gendisk for these
1120 * drivers we also call blk_put_queue() for them, and we expect the
1121 * request_queue refcount to reach 0 at this point, and so the request_queue
1122 * will also be freed prior to the disk.
1124 * Context: can sleep
1126 static void disk_release(struct device
*dev
)
1128 struct gendisk
*disk
= dev_to_disk(dev
);
1132 blk_free_devt(dev
->devt
);
1133 disk_release_events(disk
);
1134 kfree(disk
->random
);
1135 xa_destroy(&disk
->part_tbl
);
1138 blk_put_queue(disk
->queue
);
1141 struct class block_class
= {
1145 static char *block_devnode(struct device
*dev
, umode_t
*mode
,
1146 kuid_t
*uid
, kgid_t
*gid
)
1148 struct gendisk
*disk
= dev_to_disk(dev
);
1150 if (disk
->fops
->devnode
)
1151 return disk
->fops
->devnode(disk
, mode
);
1155 const struct device_type disk_type
= {
1157 .groups
= disk_attr_groups
,
1158 .release
= disk_release
,
1159 .devnode
= block_devnode
,
1162 #ifdef CONFIG_PROC_FS
1164 * aggregate disk stat collector. Uses the same stats that the sysfs
1165 * entries do, above, but makes them available through one seq_file.
1167 * The output looks suspiciously like /proc/partitions with a bunch of
1170 static int diskstats_show(struct seq_file
*seqf
, void *v
)
1172 struct gendisk
*gp
= v
;
1173 struct block_device
*hd
;
1174 char buf
[BDEVNAME_SIZE
];
1175 unsigned int inflight
;
1176 struct disk_stats stat
;
1180 if (&disk_to_dev(gp)->kobj.entry == block_class.devices.next)
1181 seq_puts(seqf, "major minor name"
1182 " rio rmerge rsect ruse wio wmerge "
1183 "wsect wuse running use aveq"
1188 xa_for_each(&gp
->part_tbl
, idx
, hd
) {
1189 if (bdev_is_partition(hd
) && !bdev_nr_sectors(hd
))
1191 part_stat_read_all(hd
, &stat
);
1192 if (queue_is_mq(gp
->queue
))
1193 inflight
= blk_mq_in_flight(gp
->queue
, hd
);
1195 inflight
= part_in_flight(hd
);
1197 seq_printf(seqf
, "%4d %7d %s "
1204 MAJOR(hd
->bd_dev
), MINOR(hd
->bd_dev
),
1205 disk_name(gp
, hd
->bd_partno
, buf
),
1206 stat
.ios
[STAT_READ
],
1207 stat
.merges
[STAT_READ
],
1208 stat
.sectors
[STAT_READ
],
1209 (unsigned int)div_u64(stat
.nsecs
[STAT_READ
],
1211 stat
.ios
[STAT_WRITE
],
1212 stat
.merges
[STAT_WRITE
],
1213 stat
.sectors
[STAT_WRITE
],
1214 (unsigned int)div_u64(stat
.nsecs
[STAT_WRITE
],
1217 jiffies_to_msecs(stat
.io_ticks
),
1218 (unsigned int)div_u64(stat
.nsecs
[STAT_READ
] +
1219 stat
.nsecs
[STAT_WRITE
] +
1220 stat
.nsecs
[STAT_DISCARD
] +
1221 stat
.nsecs
[STAT_FLUSH
],
1223 stat
.ios
[STAT_DISCARD
],
1224 stat
.merges
[STAT_DISCARD
],
1225 stat
.sectors
[STAT_DISCARD
],
1226 (unsigned int)div_u64(stat
.nsecs
[STAT_DISCARD
],
1228 stat
.ios
[STAT_FLUSH
],
1229 (unsigned int)div_u64(stat
.nsecs
[STAT_FLUSH
],
1238 static const struct seq_operations diskstats_op
= {
1239 .start
= disk_seqf_start
,
1240 .next
= disk_seqf_next
,
1241 .stop
= disk_seqf_stop
,
1242 .show
= diskstats_show
1245 static int __init
proc_genhd_init(void)
1247 proc_create_seq("diskstats", 0, NULL
, &diskstats_op
);
1248 proc_create_seq("partitions", 0, NULL
, &partitions_op
);
1251 module_init(proc_genhd_init
);
1252 #endif /* CONFIG_PROC_FS */
1254 dev_t
blk_lookup_devt(const char *name
, int partno
)
1256 dev_t devt
= MKDEV(0, 0);
1257 struct class_dev_iter iter
;
1260 class_dev_iter_init(&iter
, &block_class
, NULL
, &disk_type
);
1261 while ((dev
= class_dev_iter_next(&iter
))) {
1262 struct gendisk
*disk
= dev_to_disk(dev
);
1263 struct block_device
*part
;
1265 if (strcmp(dev_name(dev
), name
))
1268 if (partno
< disk
->minors
) {
1269 /* We need to return the right devno, even
1270 * if the partition doesn't exist yet.
1272 devt
= MKDEV(MAJOR(dev
->devt
),
1273 MINOR(dev
->devt
) + partno
);
1276 part
= bdget_disk(disk
, partno
);
1278 devt
= part
->bd_dev
;
1283 class_dev_iter_exit(&iter
);
1287 struct gendisk
*__alloc_disk_node(int minors
, int node_id
)
1289 struct gendisk
*disk
;
1291 if (minors
> DISK_MAX_PARTS
) {
1293 "block: can't allocate more than %d partitions\n",
1295 minors
= DISK_MAX_PARTS
;
1298 disk
= kzalloc_node(sizeof(struct gendisk
), GFP_KERNEL
, node_id
);
1302 disk
->part0
= bdev_alloc(disk
, 0);
1306 disk
->node_id
= node_id
;
1307 xa_init(&disk
->part_tbl
);
1308 if (xa_insert(&disk
->part_tbl
, 0, disk
->part0
, GFP_KERNEL
))
1309 goto out_destroy_part_tbl
;
1311 disk
->minors
= minors
;
1312 rand_initialize_disk(disk
);
1313 disk_to_dev(disk
)->class = &block_class
;
1314 disk_to_dev(disk
)->type
= &disk_type
;
1315 device_initialize(disk_to_dev(disk
));
1318 out_destroy_part_tbl
:
1319 xa_destroy(&disk
->part_tbl
);
1325 EXPORT_SYMBOL(__alloc_disk_node
);
1328 * put_disk - decrements the gendisk refcount
1329 * @disk: the struct gendisk to decrement the refcount for
1331 * This decrements the refcount for the struct gendisk. When this reaches 0
1332 * we'll have disk_release() called.
1334 * Context: Any context, but the last reference must not be dropped from
1337 void put_disk(struct gendisk
*disk
)
1340 put_device(disk_to_dev(disk
));
1342 EXPORT_SYMBOL(put_disk
);
1344 static void set_disk_ro_uevent(struct gendisk
*gd
, int ro
)
1346 char event
[] = "DISK_RO=1";
1347 char *envp
[] = { event
, NULL
};
1351 kobject_uevent_env(&disk_to_dev(gd
)->kobj
, KOBJ_CHANGE
, envp
);
1355 * set_disk_ro - set a gendisk read-only
1356 * @disk: gendisk to operate on
1357 * @read_only: %true to set the disk read-only, %false set the disk read/write
1359 * This function is used to indicate whether a given disk device should have its
1360 * read-only flag set. set_disk_ro() is typically used by device drivers to
1361 * indicate whether the underlying physical device is write-protected.
1363 void set_disk_ro(struct gendisk
*disk
, bool read_only
)
1366 if (test_and_set_bit(GD_READ_ONLY
, &disk
->state
))
1369 if (!test_and_clear_bit(GD_READ_ONLY
, &disk
->state
))
1372 set_disk_ro_uevent(disk
, read_only
);
1374 EXPORT_SYMBOL(set_disk_ro
);
1376 int bdev_read_only(struct block_device
*bdev
)
1378 return bdev
->bd_read_only
|| get_disk_ro(bdev
->bd_disk
);
1380 EXPORT_SYMBOL(bdev_read_only
);
1383 * Disk events - monitor disk events like media change and eject request.
1385 struct disk_events
{
1386 struct list_head node
; /* all disk_event's */
1387 struct gendisk
*disk
; /* the associated disk */
1390 struct mutex block_mutex
; /* protects blocking */
1391 int block
; /* event blocking depth */
1392 unsigned int pending
; /* events already sent out */
1393 unsigned int clearing
; /* events being cleared */
1395 long poll_msecs
; /* interval, -1 for default */
1396 struct delayed_work dwork
;
1399 static const char *disk_events_strs
[] = {
1400 [ilog2(DISK_EVENT_MEDIA_CHANGE
)] = "media_change",
1401 [ilog2(DISK_EVENT_EJECT_REQUEST
)] = "eject_request",
1404 static char *disk_uevents
[] = {
1405 [ilog2(DISK_EVENT_MEDIA_CHANGE
)] = "DISK_MEDIA_CHANGE=1",
1406 [ilog2(DISK_EVENT_EJECT_REQUEST
)] = "DISK_EJECT_REQUEST=1",
1409 /* list of all disk_events */
1410 static DEFINE_MUTEX(disk_events_mutex
);
1411 static LIST_HEAD(disk_events
);
1413 /* disable in-kernel polling by default */
1414 static unsigned long disk_events_dfl_poll_msecs
;
1416 static unsigned long disk_events_poll_jiffies(struct gendisk
*disk
)
1418 struct disk_events
*ev
= disk
->ev
;
1419 long intv_msecs
= 0;
1422 * If device-specific poll interval is set, always use it. If
1423 * the default is being used, poll if the POLL flag is set.
1425 if (ev
->poll_msecs
>= 0)
1426 intv_msecs
= ev
->poll_msecs
;
1427 else if (disk
->event_flags
& DISK_EVENT_FLAG_POLL
)
1428 intv_msecs
= disk_events_dfl_poll_msecs
;
1430 return msecs_to_jiffies(intv_msecs
);
1434 * disk_block_events - block and flush disk event checking
1435 * @disk: disk to block events for
1437 * On return from this function, it is guaranteed that event checking
1438 * isn't in progress and won't happen until unblocked by
1439 * disk_unblock_events(). Events blocking is counted and the actual
1440 * unblocking happens after the matching number of unblocks are done.
1442 * Note that this intentionally does not block event checking from
1443 * disk_clear_events().
1448 void disk_block_events(struct gendisk
*disk
)
1450 struct disk_events
*ev
= disk
->ev
;
1451 unsigned long flags
;
1458 * Outer mutex ensures that the first blocker completes canceling
1459 * the event work before further blockers are allowed to finish.
1461 mutex_lock(&ev
->block_mutex
);
1463 spin_lock_irqsave(&ev
->lock
, flags
);
1464 cancel
= !ev
->block
++;
1465 spin_unlock_irqrestore(&ev
->lock
, flags
);
1468 cancel_delayed_work_sync(&disk
->ev
->dwork
);
1470 mutex_unlock(&ev
->block_mutex
);
1473 static void __disk_unblock_events(struct gendisk
*disk
, bool check_now
)
1475 struct disk_events
*ev
= disk
->ev
;
1477 unsigned long flags
;
1479 spin_lock_irqsave(&ev
->lock
, flags
);
1481 if (WARN_ON_ONCE(ev
->block
<= 0))
1487 intv
= disk_events_poll_jiffies(disk
);
1489 queue_delayed_work(system_freezable_power_efficient_wq
,
1492 queue_delayed_work(system_freezable_power_efficient_wq
,
1495 spin_unlock_irqrestore(&ev
->lock
, flags
);
1499 * disk_unblock_events - unblock disk event checking
1500 * @disk: disk to unblock events for
1502 * Undo disk_block_events(). When the block count reaches zero, it
1503 * starts events polling if configured.
1506 * Don't care. Safe to call from irq context.
1508 void disk_unblock_events(struct gendisk
*disk
)
1511 __disk_unblock_events(disk
, false);
1515 * disk_flush_events - schedule immediate event checking and flushing
1516 * @disk: disk to check and flush events for
1517 * @mask: events to flush
1519 * Schedule immediate event checking on @disk if not blocked. Events in
1520 * @mask are scheduled to be cleared from the driver. Note that this
1521 * doesn't clear the events from @disk->ev.
1524 * If @mask is non-zero must be called with bdev->bd_mutex held.
1526 void disk_flush_events(struct gendisk
*disk
, unsigned int mask
)
1528 struct disk_events
*ev
= disk
->ev
;
1533 spin_lock_irq(&ev
->lock
);
1534 ev
->clearing
|= mask
;
1536 mod_delayed_work(system_freezable_power_efficient_wq
,
1538 spin_unlock_irq(&ev
->lock
);
1542 * disk_clear_events - synchronously check, clear and return pending events
1543 * @disk: disk to fetch and clear events from
1544 * @mask: mask of events to be fetched and cleared
1546 * Disk events are synchronously checked and pending events in @mask
1547 * are cleared and returned. This ignores the block count.
1552 static unsigned int disk_clear_events(struct gendisk
*disk
, unsigned int mask
)
1554 struct disk_events
*ev
= disk
->ev
;
1555 unsigned int pending
;
1556 unsigned int clearing
= mask
;
1561 disk_block_events(disk
);
1564 * store the union of mask and ev->clearing on the stack so that the
1565 * race with disk_flush_events does not cause ambiguity (ev->clearing
1566 * can still be modified even if events are blocked).
1568 spin_lock_irq(&ev
->lock
);
1569 clearing
|= ev
->clearing
;
1571 spin_unlock_irq(&ev
->lock
);
1573 disk_check_events(ev
, &clearing
);
1575 * if ev->clearing is not 0, the disk_flush_events got called in the
1576 * middle of this function, so we want to run the workfn without delay.
1578 __disk_unblock_events(disk
, ev
->clearing
? true : false);
1580 /* then, fetch and clear pending events */
1581 spin_lock_irq(&ev
->lock
);
1582 pending
= ev
->pending
& mask
;
1583 ev
->pending
&= ~mask
;
1584 spin_unlock_irq(&ev
->lock
);
1585 WARN_ON_ONCE(clearing
& mask
);
1591 * bdev_check_media_change - check if a removable media has been changed
1592 * @bdev: block device to check
1594 * Check whether a removable media has been changed, and attempt to free all
1595 * dentries and inodes and invalidates all block device page cache entries in
1598 * Returns %true if the block device changed, or %false if not.
1600 bool bdev_check_media_change(struct block_device
*bdev
)
1602 unsigned int events
;
1604 events
= disk_clear_events(bdev
->bd_disk
, DISK_EVENT_MEDIA_CHANGE
|
1605 DISK_EVENT_EJECT_REQUEST
);
1606 if (!(events
& DISK_EVENT_MEDIA_CHANGE
))
1609 if (__invalidate_device(bdev
, true))
1610 pr_warn("VFS: busy inodes on changed media %s\n",
1611 bdev
->bd_disk
->disk_name
);
1612 set_bit(GD_NEED_PART_SCAN
, &bdev
->bd_disk
->state
);
1615 EXPORT_SYMBOL(bdev_check_media_change
);
1618 * Separate this part out so that a different pointer for clearing_ptr can be
1619 * passed in for disk_clear_events.
1621 static void disk_events_workfn(struct work_struct
*work
)
1623 struct delayed_work
*dwork
= to_delayed_work(work
);
1624 struct disk_events
*ev
= container_of(dwork
, struct disk_events
, dwork
);
1626 disk_check_events(ev
, &ev
->clearing
);
1629 static void disk_check_events(struct disk_events
*ev
,
1630 unsigned int *clearing_ptr
)
1632 struct gendisk
*disk
= ev
->disk
;
1633 char *envp
[ARRAY_SIZE(disk_uevents
) + 1] = { };
1634 unsigned int clearing
= *clearing_ptr
;
1635 unsigned int events
;
1637 int nr_events
= 0, i
;
1640 events
= disk
->fops
->check_events(disk
, clearing
);
1642 /* accumulate pending events and schedule next poll if necessary */
1643 spin_lock_irq(&ev
->lock
);
1645 events
&= ~ev
->pending
;
1646 ev
->pending
|= events
;
1647 *clearing_ptr
&= ~clearing
;
1649 intv
= disk_events_poll_jiffies(disk
);
1650 if (!ev
->block
&& intv
)
1651 queue_delayed_work(system_freezable_power_efficient_wq
,
1654 spin_unlock_irq(&ev
->lock
);
1657 * Tell userland about new events. Only the events listed in
1658 * @disk->events are reported, and only if DISK_EVENT_FLAG_UEVENT
1659 * is set. Otherwise, events are processed internally but never
1660 * get reported to userland.
1662 for (i
= 0; i
< ARRAY_SIZE(disk_uevents
); i
++)
1663 if ((events
& disk
->events
& (1 << i
)) &&
1664 (disk
->event_flags
& DISK_EVENT_FLAG_UEVENT
))
1665 envp
[nr_events
++] = disk_uevents
[i
];
1668 kobject_uevent_env(&disk_to_dev(disk
)->kobj
, KOBJ_CHANGE
, envp
);
1672 * A disk events enabled device has the following sysfs nodes under
1673 * its /sys/block/X/ directory.
1675 * events : list of all supported events
1676 * events_async : list of events which can be detected w/o polling
1677 * (always empty, only for backwards compatibility)
1678 * events_poll_msecs : polling interval, 0: disable, -1: system default
1680 static ssize_t
__disk_events_show(unsigned int events
, char *buf
)
1682 const char *delim
= "";
1686 for (i
= 0; i
< ARRAY_SIZE(disk_events_strs
); i
++)
1687 if (events
& (1 << i
)) {
1688 pos
+= sprintf(buf
+ pos
, "%s%s",
1689 delim
, disk_events_strs
[i
]);
1693 pos
+= sprintf(buf
+ pos
, "\n");
1697 static ssize_t
disk_events_show(struct device
*dev
,
1698 struct device_attribute
*attr
, char *buf
)
1700 struct gendisk
*disk
= dev_to_disk(dev
);
1702 if (!(disk
->event_flags
& DISK_EVENT_FLAG_UEVENT
))
1705 return __disk_events_show(disk
->events
, buf
);
1708 static ssize_t
disk_events_async_show(struct device
*dev
,
1709 struct device_attribute
*attr
, char *buf
)
1714 static ssize_t
disk_events_poll_msecs_show(struct device
*dev
,
1715 struct device_attribute
*attr
,
1718 struct gendisk
*disk
= dev_to_disk(dev
);
1721 return sprintf(buf
, "-1\n");
1723 return sprintf(buf
, "%ld\n", disk
->ev
->poll_msecs
);
1726 static ssize_t
disk_events_poll_msecs_store(struct device
*dev
,
1727 struct device_attribute
*attr
,
1728 const char *buf
, size_t count
)
1730 struct gendisk
*disk
= dev_to_disk(dev
);
1733 if (!count
|| !sscanf(buf
, "%ld", &intv
))
1736 if (intv
< 0 && intv
!= -1)
1742 disk_block_events(disk
);
1743 disk
->ev
->poll_msecs
= intv
;
1744 __disk_unblock_events(disk
, true);
1749 static const DEVICE_ATTR(events
, 0444, disk_events_show
, NULL
);
1750 static const DEVICE_ATTR(events_async
, 0444, disk_events_async_show
, NULL
);
1751 static const DEVICE_ATTR(events_poll_msecs
, 0644,
1752 disk_events_poll_msecs_show
,
1753 disk_events_poll_msecs_store
);
1755 static const struct attribute
*disk_events_attrs
[] = {
1756 &dev_attr_events
.attr
,
1757 &dev_attr_events_async
.attr
,
1758 &dev_attr_events_poll_msecs
.attr
,
1763 * The default polling interval can be specified by the kernel
1764 * parameter block.events_dfl_poll_msecs which defaults to 0
1765 * (disable). This can also be modified runtime by writing to
1766 * /sys/module/block/parameters/events_dfl_poll_msecs.
1768 static int disk_events_set_dfl_poll_msecs(const char *val
,
1769 const struct kernel_param
*kp
)
1771 struct disk_events
*ev
;
1774 ret
= param_set_ulong(val
, kp
);
1778 mutex_lock(&disk_events_mutex
);
1780 list_for_each_entry(ev
, &disk_events
, node
)
1781 disk_flush_events(ev
->disk
, 0);
1783 mutex_unlock(&disk_events_mutex
);
1788 static const struct kernel_param_ops disk_events_dfl_poll_msecs_param_ops
= {
1789 .set
= disk_events_set_dfl_poll_msecs
,
1790 .get
= param_get_ulong
,
1793 #undef MODULE_PARAM_PREFIX
1794 #define MODULE_PARAM_PREFIX "block."
1796 module_param_cb(events_dfl_poll_msecs
, &disk_events_dfl_poll_msecs_param_ops
,
1797 &disk_events_dfl_poll_msecs
, 0644);
1800 * disk_{alloc|add|del|release}_events - initialize and destroy disk_events.
1802 static void disk_alloc_events(struct gendisk
*disk
)
1804 struct disk_events
*ev
;
1806 if (!disk
->fops
->check_events
|| !disk
->events
)
1809 ev
= kzalloc(sizeof(*ev
), GFP_KERNEL
);
1811 pr_warn("%s: failed to initialize events\n", disk
->disk_name
);
1815 INIT_LIST_HEAD(&ev
->node
);
1817 spin_lock_init(&ev
->lock
);
1818 mutex_init(&ev
->block_mutex
);
1820 ev
->poll_msecs
= -1;
1821 INIT_DELAYED_WORK(&ev
->dwork
, disk_events_workfn
);
1826 static void disk_add_events(struct gendisk
*disk
)
1828 /* FIXME: error handling */
1829 if (sysfs_create_files(&disk_to_dev(disk
)->kobj
, disk_events_attrs
) < 0)
1830 pr_warn("%s: failed to create sysfs files for events\n",
1836 mutex_lock(&disk_events_mutex
);
1837 list_add_tail(&disk
->ev
->node
, &disk_events
);
1838 mutex_unlock(&disk_events_mutex
);
1841 * Block count is initialized to 1 and the following initial
1842 * unblock kicks it into action.
1844 __disk_unblock_events(disk
, true);
1847 static void disk_del_events(struct gendisk
*disk
)
1850 disk_block_events(disk
);
1852 mutex_lock(&disk_events_mutex
);
1853 list_del_init(&disk
->ev
->node
);
1854 mutex_unlock(&disk_events_mutex
);
1857 sysfs_remove_files(&disk_to_dev(disk
)->kobj
, disk_events_attrs
);
1860 static void disk_release_events(struct gendisk
*disk
)
1862 /* the block count should be 1 from disk_del_events() */
1863 WARN_ON_ONCE(disk
->ev
&& disk
->ev
->block
!= 1);