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
;
33 * Unique, monotonically increasing sequential number associated with block
34 * devices instances (i.e. incremented each time a device is attached).
35 * Associating uevents with block devices in userspace is difficult and racy:
36 * the uevent netlink socket is lossy, and on slow and overloaded systems has
37 * a very high latency.
38 * Block devices do not have exclusive owners in userspace, any process can set
39 * one up (e.g. loop devices). Moreover, device names can be reused (e.g. loop0
40 * can be reused again and again).
41 * A userspace process setting up a block device and watching for its events
42 * cannot thus reliably tell whether an event relates to the device it just set
43 * up or another earlier instance with the same name.
44 * This sequential number allows userspace processes to solve this problem, and
45 * uniquely associate an uevent to the lifetime to a device.
47 static atomic64_t diskseq
;
49 /* for extended dynamic devt allocation, currently only one major is used */
50 #define NR_EXT_DEVT (1 << MINORBITS)
51 static DEFINE_IDA(ext_devt_ida
);
53 void set_capacity(struct gendisk
*disk
, sector_t sectors
)
55 struct block_device
*bdev
= disk
->part0
;
57 spin_lock(&bdev
->bd_size_lock
);
58 i_size_write(bdev
->bd_inode
, (loff_t
)sectors
<< SECTOR_SHIFT
);
59 spin_unlock(&bdev
->bd_size_lock
);
61 EXPORT_SYMBOL(set_capacity
);
64 * Set disk capacity and notify if the size is not currently zero and will not
65 * be set to zero. Returns true if a uevent was sent, otherwise false.
67 bool set_capacity_and_notify(struct gendisk
*disk
, sector_t size
)
69 sector_t capacity
= get_capacity(disk
);
70 char *envp
[] = { "RESIZE=1", NULL
};
72 set_capacity(disk
, size
);
75 * Only print a message and send a uevent if the gendisk is user visible
76 * and alive. This avoids spamming the log and udev when setting the
77 * initial capacity during probing.
79 if (size
== capacity
||
81 (disk
->flags
& GENHD_FL_HIDDEN
))
84 pr_info("%s: detected capacity change from %lld to %lld\n",
85 disk
->disk_name
, capacity
, size
);
88 * Historically we did not send a uevent for changes to/from an empty
91 if (!capacity
|| !size
)
93 kobject_uevent_env(&disk_to_dev(disk
)->kobj
, KOBJ_CHANGE
, envp
);
96 EXPORT_SYMBOL_GPL(set_capacity_and_notify
);
99 * Format the device name of the indicated block device into the supplied buffer
100 * and return a pointer to that same buffer for convenience.
102 * Note: do not use this in new code, use the %pg specifier to sprintf and
105 const char *bdevname(struct block_device
*bdev
, char *buf
)
107 struct gendisk
*hd
= bdev
->bd_disk
;
108 int partno
= bdev
->bd_partno
;
111 snprintf(buf
, BDEVNAME_SIZE
, "%s", hd
->disk_name
);
112 else if (isdigit(hd
->disk_name
[strlen(hd
->disk_name
)-1]))
113 snprintf(buf
, BDEVNAME_SIZE
, "%sp%d", hd
->disk_name
, partno
);
115 snprintf(buf
, BDEVNAME_SIZE
, "%s%d", hd
->disk_name
, partno
);
119 EXPORT_SYMBOL(bdevname
);
121 static void part_stat_read_all(struct block_device
*part
,
122 struct disk_stats
*stat
)
126 memset(stat
, 0, sizeof(struct disk_stats
));
127 for_each_possible_cpu(cpu
) {
128 struct disk_stats
*ptr
= per_cpu_ptr(part
->bd_stats
, cpu
);
131 for (group
= 0; group
< NR_STAT_GROUPS
; group
++) {
132 stat
->nsecs
[group
] += ptr
->nsecs
[group
];
133 stat
->sectors
[group
] += ptr
->sectors
[group
];
134 stat
->ios
[group
] += ptr
->ios
[group
];
135 stat
->merges
[group
] += ptr
->merges
[group
];
138 stat
->io_ticks
+= ptr
->io_ticks
;
142 static unsigned int part_in_flight(struct block_device
*part
)
144 unsigned int inflight
= 0;
147 for_each_possible_cpu(cpu
) {
148 inflight
+= part_stat_local_read_cpu(part
, in_flight
[0], cpu
) +
149 part_stat_local_read_cpu(part
, in_flight
[1], cpu
);
151 if ((int)inflight
< 0)
157 static void part_in_flight_rw(struct block_device
*part
,
158 unsigned int inflight
[2])
164 for_each_possible_cpu(cpu
) {
165 inflight
[0] += part_stat_local_read_cpu(part
, in_flight
[0], cpu
);
166 inflight
[1] += part_stat_local_read_cpu(part
, in_flight
[1], cpu
);
168 if ((int)inflight
[0] < 0)
170 if ((int)inflight
[1] < 0)
175 * Can be deleted altogether. Later.
178 #define BLKDEV_MAJOR_HASH_SIZE 255
179 static struct blk_major_name
{
180 struct blk_major_name
*next
;
183 void (*probe
)(dev_t devt
);
184 } *major_names
[BLKDEV_MAJOR_HASH_SIZE
];
185 static DEFINE_MUTEX(major_names_lock
);
186 static DEFINE_SPINLOCK(major_names_spinlock
);
188 /* index in the above - for now: assume no multimajor ranges */
189 static inline int major_to_index(unsigned major
)
191 return major
% BLKDEV_MAJOR_HASH_SIZE
;
194 #ifdef CONFIG_PROC_FS
195 void blkdev_show(struct seq_file
*seqf
, off_t offset
)
197 struct blk_major_name
*dp
;
199 spin_lock(&major_names_spinlock
);
200 for (dp
= major_names
[major_to_index(offset
)]; dp
; dp
= dp
->next
)
201 if (dp
->major
== offset
)
202 seq_printf(seqf
, "%3d %s\n", dp
->major
, dp
->name
);
203 spin_unlock(&major_names_spinlock
);
205 #endif /* CONFIG_PROC_FS */
208 * __register_blkdev - register a new block device
210 * @major: the requested major device number [1..BLKDEV_MAJOR_MAX-1]. If
211 * @major = 0, try to allocate any unused major number.
212 * @name: the name of the new block device as a zero terminated string
213 * @probe: allback that is called on access to any minor number of @major
215 * The @name must be unique within the system.
217 * The return value depends on the @major input parameter:
219 * - if a major device number was requested in range [1..BLKDEV_MAJOR_MAX-1]
220 * then the function returns zero on success, or a negative error code
221 * - if any unused major number was requested with @major = 0 parameter
222 * then the return value is the allocated major number in range
223 * [1..BLKDEV_MAJOR_MAX-1] or a negative error code otherwise
225 * See Documentation/admin-guide/devices.txt for the list of allocated
228 * Use register_blkdev instead for any new code.
230 int __register_blkdev(unsigned int major
, const char *name
,
231 void (*probe
)(dev_t devt
))
233 struct blk_major_name
**n
, *p
;
236 mutex_lock(&major_names_lock
);
240 for (index
= ARRAY_SIZE(major_names
)-1; index
> 0; index
--) {
241 if (major_names
[index
] == NULL
)
246 printk("%s: failed to get major for %s\n",
255 if (major
>= BLKDEV_MAJOR_MAX
) {
256 pr_err("%s: major requested (%u) is greater than the maximum (%u) for %s\n",
257 __func__
, major
, BLKDEV_MAJOR_MAX
-1, name
);
263 p
= kmalloc(sizeof(struct blk_major_name
), GFP_KERNEL
);
271 strlcpy(p
->name
, name
, sizeof(p
->name
));
273 index
= major_to_index(major
);
275 spin_lock(&major_names_spinlock
);
276 for (n
= &major_names
[index
]; *n
; n
= &(*n
)->next
) {
277 if ((*n
)->major
== major
)
284 spin_unlock(&major_names_spinlock
);
287 printk("register_blkdev: cannot get major %u for %s\n",
292 mutex_unlock(&major_names_lock
);
295 EXPORT_SYMBOL(__register_blkdev
);
297 void unregister_blkdev(unsigned int major
, const char *name
)
299 struct blk_major_name
**n
;
300 struct blk_major_name
*p
= NULL
;
301 int index
= major_to_index(major
);
303 mutex_lock(&major_names_lock
);
304 spin_lock(&major_names_spinlock
);
305 for (n
= &major_names
[index
]; *n
; n
= &(*n
)->next
)
306 if ((*n
)->major
== major
)
308 if (!*n
|| strcmp((*n
)->name
, name
)) {
314 spin_unlock(&major_names_spinlock
);
315 mutex_unlock(&major_names_lock
);
319 EXPORT_SYMBOL(unregister_blkdev
);
321 int blk_alloc_ext_minor(void)
325 idx
= ida_alloc_range(&ext_devt_ida
, 0, NR_EXT_DEVT
, GFP_KERNEL
);
331 void blk_free_ext_minor(unsigned int minor
)
333 ida_free(&ext_devt_ida
, minor
);
336 static char *bdevt_str(dev_t devt
, char *buf
)
338 if (MAJOR(devt
) <= 0xff && MINOR(devt
) <= 0xff) {
339 char tbuf
[BDEVT_SIZE
];
340 snprintf(tbuf
, BDEVT_SIZE
, "%02x%02x", MAJOR(devt
), MINOR(devt
));
341 snprintf(buf
, BDEVT_SIZE
, "%-9s", tbuf
);
343 snprintf(buf
, BDEVT_SIZE
, "%03x:%05x", MAJOR(devt
), MINOR(devt
));
348 void disk_uevent(struct gendisk
*disk
, enum kobject_action action
)
350 struct block_device
*part
;
354 xa_for_each(&disk
->part_tbl
, idx
, part
) {
355 if (bdev_is_partition(part
) && !bdev_nr_sectors(part
))
357 if (!kobject_get_unless_zero(&part
->bd_device
.kobj
))
361 kobject_uevent(bdev_kobj(part
), action
);
362 put_device(&part
->bd_device
);
367 EXPORT_SYMBOL_GPL(disk_uevent
);
369 static void disk_scan_partitions(struct gendisk
*disk
)
371 struct block_device
*bdev
;
373 if (!get_capacity(disk
) || !disk_part_scan_enabled(disk
))
376 set_bit(GD_NEED_PART_SCAN
, &disk
->state
);
377 bdev
= blkdev_get_by_dev(disk_devt(disk
), FMODE_READ
, NULL
);
379 blkdev_put(bdev
, FMODE_READ
);
383 * device_add_disk - add disk information to kernel list
384 * @parent: parent device for the disk
385 * @disk: per-device partitioning information
386 * @groups: Additional per-device sysfs groups
388 * This function registers the partitioning information in @disk
391 int device_add_disk(struct device
*parent
, struct gendisk
*disk
,
392 const struct attribute_group
**groups
)
395 struct device
*ddev
= disk_to_dev(disk
);
399 * The disk queue should now be all set with enough information about
400 * the device for the elevator code to pick an adequate default
401 * elevator if one is needed, that is, for devices requesting queue
404 elevator_init_mq(disk
->queue
);
407 * If the driver provides an explicit major number it also must provide
408 * the number of minors numbers supported, and those will be used to
410 * Otherwise just allocate the device numbers for both the whole device
411 * and all partitions from the extended dev_t space.
414 if (WARN_ON(!disk
->minors
))
417 if (disk
->minors
> DISK_MAX_PARTS
) {
418 pr_err("block: can't allocate more than %d partitions\n",
420 disk
->minors
= DISK_MAX_PARTS
;
423 if (WARN_ON(disk
->minors
))
426 ret
= blk_alloc_ext_minor();
429 disk
->major
= BLOCK_EXT_MAJOR
;
430 disk
->first_minor
= ret
;
431 disk
->flags
|= GENHD_FL_EXT_DEVT
;
434 ret
= disk_alloc_events(disk
);
436 goto out_free_ext_minor
;
438 /* delay uevents, until we scanned partition table */
439 dev_set_uevent_suppress(ddev
, 1);
441 ddev
->parent
= parent
;
442 ddev
->groups
= groups
;
443 dev_set_name(ddev
, "%s", disk
->disk_name
);
444 if (!(disk
->flags
& GENHD_FL_HIDDEN
))
445 ddev
->devt
= MKDEV(disk
->major
, disk
->first_minor
);
446 ret
= device_add(ddev
);
448 goto out_disk_release_events
;
449 if (!sysfs_deprecated
) {
450 ret
= sysfs_create_link(block_depr
, &ddev
->kobj
,
451 kobject_name(&ddev
->kobj
));
457 * avoid probable deadlock caused by allocating memory with
458 * GFP_KERNEL in runtime_resume callback of its all ancestor
461 pm_runtime_set_memalloc_noio(ddev
, true);
463 ret
= blk_integrity_add(disk
);
465 goto out_del_block_link
;
467 disk
->part0
->bd_holder_dir
=
468 kobject_create_and_add("holders", &ddev
->kobj
);
469 if (!disk
->part0
->bd_holder_dir
)
470 goto out_del_integrity
;
471 disk
->slave_dir
= kobject_create_and_add("slaves", &ddev
->kobj
);
472 if (!disk
->slave_dir
)
473 goto out_put_holder_dir
;
475 ret
= bd_register_pending_holders(disk
);
477 goto out_put_slave_dir
;
479 ret
= blk_register_queue(disk
);
481 goto out_put_slave_dir
;
483 if (disk
->flags
& GENHD_FL_HIDDEN
) {
485 * Don't let hidden disks show up in /proc/partitions,
486 * and don't bother scanning for partitions either.
488 disk
->flags
|= GENHD_FL_SUPPRESS_PARTITION_INFO
;
489 disk
->flags
|= GENHD_FL_NO_PART_SCAN
;
491 ret
= bdi_register(disk
->bdi
, "%u:%u",
492 disk
->major
, disk
->first_minor
);
494 goto out_unregister_queue
;
495 bdi_set_owner(disk
->bdi
, ddev
);
496 ret
= sysfs_create_link(&ddev
->kobj
,
497 &disk
->bdi
->dev
->kobj
, "bdi");
499 goto out_unregister_bdi
;
501 bdev_add(disk
->part0
, ddev
->devt
);
502 disk_scan_partitions(disk
);
505 * Announce the disk and partitions after all partitions are
506 * created. (for hidden disks uevents remain suppressed forever)
508 dev_set_uevent_suppress(ddev
, 0);
509 disk_uevent(disk
, KOBJ_ADD
);
512 disk_update_readahead(disk
);
513 disk_add_events(disk
);
517 if (!(disk
->flags
& GENHD_FL_HIDDEN
))
518 bdi_unregister(disk
->bdi
);
519 out_unregister_queue
:
520 blk_unregister_queue(disk
);
522 kobject_put(disk
->slave_dir
);
524 kobject_put(disk
->part0
->bd_holder_dir
);
526 blk_integrity_del(disk
);
528 if (!sysfs_deprecated
)
529 sysfs_remove_link(block_depr
, dev_name(ddev
));
532 out_disk_release_events
:
533 disk_release_events(disk
);
535 if (disk
->major
== BLOCK_EXT_MAJOR
)
536 blk_free_ext_minor(disk
->first_minor
);
537 return WARN_ON_ONCE(ret
); /* keep until all callers handle errors */
539 EXPORT_SYMBOL(device_add_disk
);
542 * del_gendisk - remove the gendisk
543 * @disk: the struct gendisk to remove
545 * Removes the gendisk and all its associated resources. This deletes the
546 * partitions associated with the gendisk, and unregisters the associated
549 * This is the counter to the respective __device_add_disk() call.
551 * The final removal of the struct gendisk happens when its refcount reaches 0
552 * with put_disk(), which should be called after del_gendisk(), if
553 * __device_add_disk() was used.
555 * Drivers exist which depend on the release of the gendisk to be synchronous,
556 * it should not be deferred.
560 void del_gendisk(struct gendisk
*disk
)
564 if (WARN_ON_ONCE(!disk_live(disk
) && !(disk
->flags
& GENHD_FL_HIDDEN
)))
567 blk_integrity_del(disk
);
568 disk_del_events(disk
);
570 mutex_lock(&disk
->open_mutex
);
571 remove_inode_hash(disk
->part0
->bd_inode
);
572 blk_drop_partitions(disk
);
573 mutex_unlock(&disk
->open_mutex
);
575 fsync_bdev(disk
->part0
);
576 __invalidate_device(disk
->part0
, true);
578 set_capacity(disk
, 0);
580 if (!(disk
->flags
& GENHD_FL_HIDDEN
)) {
581 sysfs_remove_link(&disk_to_dev(disk
)->kobj
, "bdi");
584 * Unregister bdi before releasing device numbers (as they can
585 * get reused and we'd get clashes in sysfs).
587 bdi_unregister(disk
->bdi
);
590 blk_unregister_queue(disk
);
592 kobject_put(disk
->part0
->bd_holder_dir
);
593 kobject_put(disk
->slave_dir
);
595 part_stat_set_all(disk
->part0
, 0);
596 disk
->part0
->bd_stamp
= 0;
597 if (!sysfs_deprecated
)
598 sysfs_remove_link(block_depr
, dev_name(disk_to_dev(disk
)));
599 pm_runtime_set_memalloc_noio(disk_to_dev(disk
), false);
600 device_del(disk_to_dev(disk
));
602 EXPORT_SYMBOL(del_gendisk
);
604 /* sysfs access to bad-blocks list. */
605 static ssize_t
disk_badblocks_show(struct device
*dev
,
606 struct device_attribute
*attr
,
609 struct gendisk
*disk
= dev_to_disk(dev
);
612 return sprintf(page
, "\n");
614 return badblocks_show(disk
->bb
, page
, 0);
617 static ssize_t
disk_badblocks_store(struct device
*dev
,
618 struct device_attribute
*attr
,
619 const char *page
, size_t len
)
621 struct gendisk
*disk
= dev_to_disk(dev
);
626 return badblocks_store(disk
->bb
, page
, len
, 0);
629 void blk_request_module(dev_t devt
)
631 unsigned int major
= MAJOR(devt
);
632 struct blk_major_name
**n
;
634 mutex_lock(&major_names_lock
);
635 for (n
= &major_names
[major_to_index(major
)]; *n
; n
= &(*n
)->next
) {
636 if ((*n
)->major
== major
&& (*n
)->probe
) {
638 mutex_unlock(&major_names_lock
);
642 mutex_unlock(&major_names_lock
);
644 if (request_module("block-major-%d-%d", MAJOR(devt
), MINOR(devt
)) > 0)
645 /* Make old-style 2.4 aliases work */
646 request_module("block-major-%d", MAJOR(devt
));
650 * print a full list of all partitions - intended for places where the root
651 * filesystem can't be mounted and thus to give the victim some idea of what
654 void __init
printk_all_partitions(void)
656 struct class_dev_iter iter
;
659 class_dev_iter_init(&iter
, &block_class
, NULL
, &disk_type
);
660 while ((dev
= class_dev_iter_next(&iter
))) {
661 struct gendisk
*disk
= dev_to_disk(dev
);
662 struct block_device
*part
;
663 char devt_buf
[BDEVT_SIZE
];
667 * Don't show empty devices or things that have been
670 if (get_capacity(disk
) == 0 ||
671 (disk
->flags
& GENHD_FL_SUPPRESS_PARTITION_INFO
))
675 * Note, unlike /proc/partitions, I am showing the numbers in
676 * hex - the same format as the root= option takes.
679 xa_for_each(&disk
->part_tbl
, idx
, part
) {
680 if (!bdev_nr_sectors(part
))
682 printk("%s%s %10llu %pg %s",
683 bdev_is_partition(part
) ? " " : "",
684 bdevt_str(part
->bd_dev
, devt_buf
),
685 bdev_nr_sectors(part
) >> 1, part
,
687 part
->bd_meta_info
->uuid
: "");
688 if (bdev_is_partition(part
))
690 else if (dev
->parent
&& dev
->parent
->driver
)
691 printk(" driver: %s\n",
692 dev
->parent
->driver
->name
);
694 printk(" (driver?)\n");
698 class_dev_iter_exit(&iter
);
701 #ifdef CONFIG_PROC_FS
703 static void *disk_seqf_start(struct seq_file
*seqf
, loff_t
*pos
)
706 struct class_dev_iter
*iter
;
709 iter
= kmalloc(sizeof(*iter
), GFP_KERNEL
);
711 return ERR_PTR(-ENOMEM
);
713 seqf
->private = iter
;
714 class_dev_iter_init(iter
, &block_class
, NULL
, &disk_type
);
716 dev
= class_dev_iter_next(iter
);
721 return dev_to_disk(dev
);
724 static void *disk_seqf_next(struct seq_file
*seqf
, void *v
, loff_t
*pos
)
729 dev
= class_dev_iter_next(seqf
->private);
731 return dev_to_disk(dev
);
736 static void disk_seqf_stop(struct seq_file
*seqf
, void *v
)
738 struct class_dev_iter
*iter
= seqf
->private;
740 /* stop is called even after start failed :-( */
742 class_dev_iter_exit(iter
);
744 seqf
->private = NULL
;
748 static void *show_partition_start(struct seq_file
*seqf
, loff_t
*pos
)
752 p
= disk_seqf_start(seqf
, pos
);
753 if (!IS_ERR_OR_NULL(p
) && !*pos
)
754 seq_puts(seqf
, "major minor #blocks name\n\n");
758 static int show_partition(struct seq_file
*seqf
, void *v
)
760 struct gendisk
*sgp
= v
;
761 struct block_device
*part
;
764 /* Don't show non-partitionable removeable devices or empty devices */
765 if (!get_capacity(sgp
) || (!disk_max_parts(sgp
) &&
766 (sgp
->flags
& GENHD_FL_REMOVABLE
)))
768 if (sgp
->flags
& GENHD_FL_SUPPRESS_PARTITION_INFO
)
772 xa_for_each(&sgp
->part_tbl
, idx
, part
) {
773 if (!bdev_nr_sectors(part
))
775 seq_printf(seqf
, "%4d %7d %10llu %pg\n",
776 MAJOR(part
->bd_dev
), MINOR(part
->bd_dev
),
777 bdev_nr_sectors(part
) >> 1, part
);
783 static const struct seq_operations partitions_op
= {
784 .start
= show_partition_start
,
785 .next
= disk_seqf_next
,
786 .stop
= disk_seqf_stop
,
787 .show
= show_partition
791 static int __init
genhd_device_init(void)
795 block_class
.dev_kobj
= sysfs_dev_block_kobj
;
796 error
= class_register(&block_class
);
801 register_blkdev(BLOCK_EXT_MAJOR
, "blkext");
803 /* create top-level block dir */
804 if (!sysfs_deprecated
)
805 block_depr
= kobject_create_and_add("block", NULL
);
809 subsys_initcall(genhd_device_init
);
811 static ssize_t
disk_range_show(struct device
*dev
,
812 struct device_attribute
*attr
, char *buf
)
814 struct gendisk
*disk
= dev_to_disk(dev
);
816 return sprintf(buf
, "%d\n", disk
->minors
);
819 static ssize_t
disk_ext_range_show(struct device
*dev
,
820 struct device_attribute
*attr
, char *buf
)
822 struct gendisk
*disk
= dev_to_disk(dev
);
824 return sprintf(buf
, "%d\n", disk_max_parts(disk
));
827 static ssize_t
disk_removable_show(struct device
*dev
,
828 struct device_attribute
*attr
, char *buf
)
830 struct gendisk
*disk
= dev_to_disk(dev
);
832 return sprintf(buf
, "%d\n",
833 (disk
->flags
& GENHD_FL_REMOVABLE
? 1 : 0));
836 static ssize_t
disk_hidden_show(struct device
*dev
,
837 struct device_attribute
*attr
, char *buf
)
839 struct gendisk
*disk
= dev_to_disk(dev
);
841 return sprintf(buf
, "%d\n",
842 (disk
->flags
& GENHD_FL_HIDDEN
? 1 : 0));
845 static ssize_t
disk_ro_show(struct device
*dev
,
846 struct device_attribute
*attr
, char *buf
)
848 struct gendisk
*disk
= dev_to_disk(dev
);
850 return sprintf(buf
, "%d\n", get_disk_ro(disk
) ? 1 : 0);
853 ssize_t
part_size_show(struct device
*dev
,
854 struct device_attribute
*attr
, char *buf
)
856 return sprintf(buf
, "%llu\n", bdev_nr_sectors(dev_to_bdev(dev
)));
859 ssize_t
part_stat_show(struct device
*dev
,
860 struct device_attribute
*attr
, char *buf
)
862 struct block_device
*bdev
= dev_to_bdev(dev
);
863 struct request_queue
*q
= bdev
->bd_disk
->queue
;
864 struct disk_stats stat
;
865 unsigned int inflight
;
867 part_stat_read_all(bdev
, &stat
);
869 inflight
= blk_mq_in_flight(q
, bdev
);
871 inflight
= part_in_flight(bdev
);
874 "%8lu %8lu %8llu %8u "
875 "%8lu %8lu %8llu %8u "
877 "%8lu %8lu %8llu %8u "
881 stat
.merges
[STAT_READ
],
882 (unsigned long long)stat
.sectors
[STAT_READ
],
883 (unsigned int)div_u64(stat
.nsecs
[STAT_READ
], NSEC_PER_MSEC
),
884 stat
.ios
[STAT_WRITE
],
885 stat
.merges
[STAT_WRITE
],
886 (unsigned long long)stat
.sectors
[STAT_WRITE
],
887 (unsigned int)div_u64(stat
.nsecs
[STAT_WRITE
], NSEC_PER_MSEC
),
889 jiffies_to_msecs(stat
.io_ticks
),
890 (unsigned int)div_u64(stat
.nsecs
[STAT_READ
] +
891 stat
.nsecs
[STAT_WRITE
] +
892 stat
.nsecs
[STAT_DISCARD
] +
893 stat
.nsecs
[STAT_FLUSH
],
895 stat
.ios
[STAT_DISCARD
],
896 stat
.merges
[STAT_DISCARD
],
897 (unsigned long long)stat
.sectors
[STAT_DISCARD
],
898 (unsigned int)div_u64(stat
.nsecs
[STAT_DISCARD
], NSEC_PER_MSEC
),
899 stat
.ios
[STAT_FLUSH
],
900 (unsigned int)div_u64(stat
.nsecs
[STAT_FLUSH
], NSEC_PER_MSEC
));
903 ssize_t
part_inflight_show(struct device
*dev
, struct device_attribute
*attr
,
906 struct block_device
*bdev
= dev_to_bdev(dev
);
907 struct request_queue
*q
= bdev
->bd_disk
->queue
;
908 unsigned int inflight
[2];
911 blk_mq_in_flight_rw(q
, bdev
, inflight
);
913 part_in_flight_rw(bdev
, inflight
);
915 return sprintf(buf
, "%8u %8u\n", inflight
[0], inflight
[1]);
918 static ssize_t
disk_capability_show(struct device
*dev
,
919 struct device_attribute
*attr
, char *buf
)
921 struct gendisk
*disk
= dev_to_disk(dev
);
923 return sprintf(buf
, "%x\n", disk
->flags
);
926 static ssize_t
disk_alignment_offset_show(struct device
*dev
,
927 struct device_attribute
*attr
,
930 struct gendisk
*disk
= dev_to_disk(dev
);
932 return sprintf(buf
, "%d\n", queue_alignment_offset(disk
->queue
));
935 static ssize_t
disk_discard_alignment_show(struct device
*dev
,
936 struct device_attribute
*attr
,
939 struct gendisk
*disk
= dev_to_disk(dev
);
941 return sprintf(buf
, "%d\n", queue_discard_alignment(disk
->queue
));
944 static ssize_t
diskseq_show(struct device
*dev
,
945 struct device_attribute
*attr
, char *buf
)
947 struct gendisk
*disk
= dev_to_disk(dev
);
949 return sprintf(buf
, "%llu\n", disk
->diskseq
);
952 static DEVICE_ATTR(range
, 0444, disk_range_show
, NULL
);
953 static DEVICE_ATTR(ext_range
, 0444, disk_ext_range_show
, NULL
);
954 static DEVICE_ATTR(removable
, 0444, disk_removable_show
, NULL
);
955 static DEVICE_ATTR(hidden
, 0444, disk_hidden_show
, NULL
);
956 static DEVICE_ATTR(ro
, 0444, disk_ro_show
, NULL
);
957 static DEVICE_ATTR(size
, 0444, part_size_show
, NULL
);
958 static DEVICE_ATTR(alignment_offset
, 0444, disk_alignment_offset_show
, NULL
);
959 static DEVICE_ATTR(discard_alignment
, 0444, disk_discard_alignment_show
, NULL
);
960 static DEVICE_ATTR(capability
, 0444, disk_capability_show
, NULL
);
961 static DEVICE_ATTR(stat
, 0444, part_stat_show
, NULL
);
962 static DEVICE_ATTR(inflight
, 0444, part_inflight_show
, NULL
);
963 static DEVICE_ATTR(badblocks
, 0644, disk_badblocks_show
, disk_badblocks_store
);
964 static DEVICE_ATTR(diskseq
, 0444, diskseq_show
, NULL
);
966 #ifdef CONFIG_FAIL_MAKE_REQUEST
967 ssize_t
part_fail_show(struct device
*dev
,
968 struct device_attribute
*attr
, char *buf
)
970 return sprintf(buf
, "%d\n", dev_to_bdev(dev
)->bd_make_it_fail
);
973 ssize_t
part_fail_store(struct device
*dev
,
974 struct device_attribute
*attr
,
975 const char *buf
, size_t count
)
979 if (count
> 0 && sscanf(buf
, "%d", &i
) > 0)
980 dev_to_bdev(dev
)->bd_make_it_fail
= i
;
985 static struct device_attribute dev_attr_fail
=
986 __ATTR(make
-it
-fail
, 0644, part_fail_show
, part_fail_store
);
987 #endif /* CONFIG_FAIL_MAKE_REQUEST */
989 #ifdef CONFIG_FAIL_IO_TIMEOUT
990 static struct device_attribute dev_attr_fail_timeout
=
991 __ATTR(io
-timeout
-fail
, 0644, part_timeout_show
, part_timeout_store
);
994 static struct attribute
*disk_attrs
[] = {
995 &dev_attr_range
.attr
,
996 &dev_attr_ext_range
.attr
,
997 &dev_attr_removable
.attr
,
998 &dev_attr_hidden
.attr
,
1000 &dev_attr_size
.attr
,
1001 &dev_attr_alignment_offset
.attr
,
1002 &dev_attr_discard_alignment
.attr
,
1003 &dev_attr_capability
.attr
,
1004 &dev_attr_stat
.attr
,
1005 &dev_attr_inflight
.attr
,
1006 &dev_attr_badblocks
.attr
,
1007 &dev_attr_events
.attr
,
1008 &dev_attr_events_async
.attr
,
1009 &dev_attr_events_poll_msecs
.attr
,
1010 &dev_attr_diskseq
.attr
,
1011 #ifdef CONFIG_FAIL_MAKE_REQUEST
1012 &dev_attr_fail
.attr
,
1014 #ifdef CONFIG_FAIL_IO_TIMEOUT
1015 &dev_attr_fail_timeout
.attr
,
1020 static umode_t
disk_visible(struct kobject
*kobj
, struct attribute
*a
, int n
)
1022 struct device
*dev
= container_of(kobj
, typeof(*dev
), kobj
);
1023 struct gendisk
*disk
= dev_to_disk(dev
);
1025 if (a
== &dev_attr_badblocks
.attr
&& !disk
->bb
)
1030 static struct attribute_group disk_attr_group
= {
1031 .attrs
= disk_attrs
,
1032 .is_visible
= disk_visible
,
1035 static const struct attribute_group
*disk_attr_groups
[] = {
1041 * disk_release - releases all allocated resources of the gendisk
1042 * @dev: the device representing this disk
1044 * This function releases all allocated resources of the gendisk.
1046 * Drivers which used __device_add_disk() have a gendisk with a request_queue
1047 * assigned. Since the request_queue sits on top of the gendisk for these
1048 * drivers we also call blk_put_queue() for them, and we expect the
1049 * request_queue refcount to reach 0 at this point, and so the request_queue
1050 * will also be freed prior to the disk.
1052 * Context: can sleep
1054 static void disk_release(struct device
*dev
)
1056 struct gendisk
*disk
= dev_to_disk(dev
);
1060 disk_release_events(disk
);
1061 kfree(disk
->random
);
1062 xa_destroy(&disk
->part_tbl
);
1063 disk
->queue
->disk
= NULL
;
1064 blk_put_queue(disk
->queue
);
1065 iput(disk
->part0
->bd_inode
); /* frees the disk */
1068 static int block_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
1070 struct gendisk
*disk
= dev_to_disk(dev
);
1072 return add_uevent_var(env
, "DISKSEQ=%llu", disk
->diskseq
);
1075 struct class block_class
= {
1077 .dev_uevent
= block_uevent
,
1080 static char *block_devnode(struct device
*dev
, umode_t
*mode
,
1081 kuid_t
*uid
, kgid_t
*gid
)
1083 struct gendisk
*disk
= dev_to_disk(dev
);
1085 if (disk
->fops
->devnode
)
1086 return disk
->fops
->devnode(disk
, mode
);
1090 const struct device_type disk_type
= {
1092 .groups
= disk_attr_groups
,
1093 .release
= disk_release
,
1094 .devnode
= block_devnode
,
1097 #ifdef CONFIG_PROC_FS
1099 * aggregate disk stat collector. Uses the same stats that the sysfs
1100 * entries do, above, but makes them available through one seq_file.
1102 * The output looks suspiciously like /proc/partitions with a bunch of
1105 static int diskstats_show(struct seq_file
*seqf
, void *v
)
1107 struct gendisk
*gp
= v
;
1108 struct block_device
*hd
;
1109 unsigned int inflight
;
1110 struct disk_stats stat
;
1114 if (&disk_to_dev(gp)->kobj.entry == block_class.devices.next)
1115 seq_puts(seqf, "major minor name"
1116 " rio rmerge rsect ruse wio wmerge "
1117 "wsect wuse running use aveq"
1122 xa_for_each(&gp
->part_tbl
, idx
, hd
) {
1123 if (bdev_is_partition(hd
) && !bdev_nr_sectors(hd
))
1125 part_stat_read_all(hd
, &stat
);
1126 if (queue_is_mq(gp
->queue
))
1127 inflight
= blk_mq_in_flight(gp
->queue
, hd
);
1129 inflight
= part_in_flight(hd
);
1131 seq_printf(seqf
, "%4d %7d %pg "
1138 MAJOR(hd
->bd_dev
), MINOR(hd
->bd_dev
), hd
,
1139 stat
.ios
[STAT_READ
],
1140 stat
.merges
[STAT_READ
],
1141 stat
.sectors
[STAT_READ
],
1142 (unsigned int)div_u64(stat
.nsecs
[STAT_READ
],
1144 stat
.ios
[STAT_WRITE
],
1145 stat
.merges
[STAT_WRITE
],
1146 stat
.sectors
[STAT_WRITE
],
1147 (unsigned int)div_u64(stat
.nsecs
[STAT_WRITE
],
1150 jiffies_to_msecs(stat
.io_ticks
),
1151 (unsigned int)div_u64(stat
.nsecs
[STAT_READ
] +
1152 stat
.nsecs
[STAT_WRITE
] +
1153 stat
.nsecs
[STAT_DISCARD
] +
1154 stat
.nsecs
[STAT_FLUSH
],
1156 stat
.ios
[STAT_DISCARD
],
1157 stat
.merges
[STAT_DISCARD
],
1158 stat
.sectors
[STAT_DISCARD
],
1159 (unsigned int)div_u64(stat
.nsecs
[STAT_DISCARD
],
1161 stat
.ios
[STAT_FLUSH
],
1162 (unsigned int)div_u64(stat
.nsecs
[STAT_FLUSH
],
1171 static const struct seq_operations diskstats_op
= {
1172 .start
= disk_seqf_start
,
1173 .next
= disk_seqf_next
,
1174 .stop
= disk_seqf_stop
,
1175 .show
= diskstats_show
1178 static int __init
proc_genhd_init(void)
1180 proc_create_seq("diskstats", 0, NULL
, &diskstats_op
);
1181 proc_create_seq("partitions", 0, NULL
, &partitions_op
);
1184 module_init(proc_genhd_init
);
1185 #endif /* CONFIG_PROC_FS */
1187 dev_t
part_devt(struct gendisk
*disk
, u8 partno
)
1189 struct block_device
*part
;
1193 part
= xa_load(&disk
->part_tbl
, partno
);
1195 devt
= part
->bd_dev
;
1201 dev_t
blk_lookup_devt(const char *name
, int partno
)
1203 dev_t devt
= MKDEV(0, 0);
1204 struct class_dev_iter iter
;
1207 class_dev_iter_init(&iter
, &block_class
, NULL
, &disk_type
);
1208 while ((dev
= class_dev_iter_next(&iter
))) {
1209 struct gendisk
*disk
= dev_to_disk(dev
);
1211 if (strcmp(dev_name(dev
), name
))
1214 if (partno
< disk
->minors
) {
1215 /* We need to return the right devno, even
1216 * if the partition doesn't exist yet.
1218 devt
= MKDEV(MAJOR(dev
->devt
),
1219 MINOR(dev
->devt
) + partno
);
1221 devt
= part_devt(disk
, partno
);
1226 class_dev_iter_exit(&iter
);
1230 struct gendisk
*__alloc_disk_node(struct request_queue
*q
, int node_id
,
1231 struct lock_class_key
*lkclass
)
1233 struct gendisk
*disk
;
1235 if (!blk_get_queue(q
))
1238 disk
= kzalloc_node(sizeof(struct gendisk
), GFP_KERNEL
, node_id
);
1242 disk
->bdi
= bdi_alloc(node_id
);
1246 disk
->part0
= bdev_alloc(disk
, 0);
1250 disk
->node_id
= node_id
;
1251 mutex_init(&disk
->open_mutex
);
1252 xa_init(&disk
->part_tbl
);
1253 if (xa_insert(&disk
->part_tbl
, 0, disk
->part0
, GFP_KERNEL
))
1254 goto out_destroy_part_tbl
;
1256 rand_initialize_disk(disk
);
1257 disk_to_dev(disk
)->class = &block_class
;
1258 disk_to_dev(disk
)->type
= &disk_type
;
1259 device_initialize(disk_to_dev(disk
));
1263 lockdep_init_map(&disk
->lockdep_map
, "(bio completion)", lkclass
, 0);
1264 #ifdef CONFIG_BLOCK_HOLDER_DEPRECATED
1265 INIT_LIST_HEAD(&disk
->slave_bdevs
);
1269 out_destroy_part_tbl
:
1270 xa_destroy(&disk
->part_tbl
);
1271 disk
->part0
->bd_disk
= NULL
;
1272 iput(disk
->part0
->bd_inode
);
1281 EXPORT_SYMBOL(__alloc_disk_node
);
1283 struct gendisk
*__blk_alloc_disk(int node
, struct lock_class_key
*lkclass
)
1285 struct request_queue
*q
;
1286 struct gendisk
*disk
;
1288 q
= blk_alloc_queue(node
);
1292 disk
= __alloc_disk_node(q
, node
, lkclass
);
1294 blk_cleanup_queue(q
);
1299 EXPORT_SYMBOL(__blk_alloc_disk
);
1302 * put_disk - decrements the gendisk refcount
1303 * @disk: the struct gendisk to decrement the refcount for
1305 * This decrements the refcount for the struct gendisk. When this reaches 0
1306 * we'll have disk_release() called.
1308 * Context: Any context, but the last reference must not be dropped from
1311 void put_disk(struct gendisk
*disk
)
1314 put_device(disk_to_dev(disk
));
1316 EXPORT_SYMBOL(put_disk
);
1319 * blk_cleanup_disk - shutdown a gendisk allocated by blk_alloc_disk
1320 * @disk: gendisk to shutdown
1322 * Mark the queue hanging off @disk DYING, drain all pending requests, then mark
1323 * the queue DEAD, destroy and put it and the gendisk structure.
1325 * Context: can sleep
1327 void blk_cleanup_disk(struct gendisk
*disk
)
1329 blk_cleanup_queue(disk
->queue
);
1332 EXPORT_SYMBOL(blk_cleanup_disk
);
1334 static void set_disk_ro_uevent(struct gendisk
*gd
, int ro
)
1336 char event
[] = "DISK_RO=1";
1337 char *envp
[] = { event
, NULL
};
1341 kobject_uevent_env(&disk_to_dev(gd
)->kobj
, KOBJ_CHANGE
, envp
);
1345 * set_disk_ro - set a gendisk read-only
1346 * @disk: gendisk to operate on
1347 * @read_only: %true to set the disk read-only, %false set the disk read/write
1349 * This function is used to indicate whether a given disk device should have its
1350 * read-only flag set. set_disk_ro() is typically used by device drivers to
1351 * indicate whether the underlying physical device is write-protected.
1353 void set_disk_ro(struct gendisk
*disk
, bool read_only
)
1356 if (test_and_set_bit(GD_READ_ONLY
, &disk
->state
))
1359 if (!test_and_clear_bit(GD_READ_ONLY
, &disk
->state
))
1362 set_disk_ro_uevent(disk
, read_only
);
1364 EXPORT_SYMBOL(set_disk_ro
);
1366 int bdev_read_only(struct block_device
*bdev
)
1368 return bdev
->bd_read_only
|| get_disk_ro(bdev
->bd_disk
);
1370 EXPORT_SYMBOL(bdev_read_only
);
1372 void inc_diskseq(struct gendisk
*disk
)
1374 disk
->diskseq
= atomic64_inc_return(&diskseq
);