1 // SPDX-License-Identifier: GPL-2.0
6 #include <linux/module.h>
7 #include <linux/ctype.h>
9 #include <linux/genhd.h>
10 #include <linux/kdev_t.h>
11 #include <linux/kernel.h>
12 #include <linux/blkdev.h>
13 #include <linux/backing-dev.h>
14 #include <linux/init.h>
15 #include <linux/spinlock.h>
16 #include <linux/proc_fs.h>
17 #include <linux/seq_file.h>
18 #include <linux/slab.h>
19 #include <linux/kmod.h>
20 #include <linux/mutex.h>
21 #include <linux/idr.h>
22 #include <linux/log2.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/badblocks.h>
28 static struct kobject
*block_depr
;
30 DECLARE_RWSEM(bdev_lookup_sem
);
32 /* for extended dynamic devt allocation, currently only one major is used */
33 #define NR_EXT_DEVT (1 << MINORBITS)
34 static DEFINE_IDA(ext_devt_ida
);
36 static void disk_check_events(struct disk_events
*ev
,
37 unsigned int *clearing_ptr
);
38 static void disk_alloc_events(struct gendisk
*disk
);
39 static void disk_add_events(struct gendisk
*disk
);
40 static void disk_del_events(struct gendisk
*disk
);
41 static void disk_release_events(struct gendisk
*disk
);
43 void set_capacity(struct gendisk
*disk
, sector_t sectors
)
45 struct block_device
*bdev
= disk
->part0
;
47 spin_lock(&bdev
->bd_size_lock
);
48 i_size_write(bdev
->bd_inode
, (loff_t
)sectors
<< SECTOR_SHIFT
);
49 spin_unlock(&bdev
->bd_size_lock
);
51 EXPORT_SYMBOL(set_capacity
);
54 * Set disk capacity and notify if the size is not currently zero and will not
55 * be set to zero. Returns true if a uevent was sent, otherwise false.
57 bool set_capacity_and_notify(struct gendisk
*disk
, sector_t size
)
59 sector_t capacity
= get_capacity(disk
);
60 char *envp
[] = { "RESIZE=1", NULL
};
62 set_capacity(disk
, size
);
65 * Only print a message and send a uevent if the gendisk is user visible
66 * and alive. This avoids spamming the log and udev when setting the
67 * initial capacity during probing.
69 if (size
== capacity
||
70 (disk
->flags
& (GENHD_FL_UP
| GENHD_FL_HIDDEN
)) != GENHD_FL_UP
)
73 pr_info("%s: detected capacity change from %lld to %lld\n",
74 disk
->disk_name
, size
, capacity
);
77 * Historically we did not send a uevent for changes to/from an empty
80 if (!capacity
|| !size
)
82 kobject_uevent_env(&disk_to_dev(disk
)->kobj
, KOBJ_CHANGE
, envp
);
85 EXPORT_SYMBOL_GPL(set_capacity_and_notify
);
88 * Format the device name of the indicated disk into the supplied buffer and
89 * return a pointer to that same buffer for convenience.
91 char *disk_name(struct gendisk
*hd
, int partno
, char *buf
)
94 snprintf(buf
, BDEVNAME_SIZE
, "%s", hd
->disk_name
);
95 else if (isdigit(hd
->disk_name
[strlen(hd
->disk_name
)-1]))
96 snprintf(buf
, BDEVNAME_SIZE
, "%sp%d", hd
->disk_name
, partno
);
98 snprintf(buf
, BDEVNAME_SIZE
, "%s%d", hd
->disk_name
, partno
);
103 const char *bdevname(struct block_device
*bdev
, char *buf
)
105 return disk_name(bdev
->bd_disk
, bdev
->bd_partno
, buf
);
107 EXPORT_SYMBOL(bdevname
);
109 static void part_stat_read_all(struct hd_struct
*part
, struct disk_stats
*stat
)
113 memset(stat
, 0, sizeof(struct disk_stats
));
114 for_each_possible_cpu(cpu
) {
115 struct disk_stats
*ptr
= per_cpu_ptr(part
->bdev
->bd_stats
, cpu
);
118 for (group
= 0; group
< NR_STAT_GROUPS
; group
++) {
119 stat
->nsecs
[group
] += ptr
->nsecs
[group
];
120 stat
->sectors
[group
] += ptr
->sectors
[group
];
121 stat
->ios
[group
] += ptr
->ios
[group
];
122 stat
->merges
[group
] += ptr
->merges
[group
];
125 stat
->io_ticks
+= ptr
->io_ticks
;
129 static unsigned int part_in_flight(struct block_device
*part
)
131 unsigned int inflight
= 0;
134 for_each_possible_cpu(cpu
) {
135 inflight
+= part_stat_local_read_cpu(part
, in_flight
[0], cpu
) +
136 part_stat_local_read_cpu(part
, in_flight
[1], cpu
);
138 if ((int)inflight
< 0)
144 static void part_in_flight_rw(struct block_device
*part
,
145 unsigned int inflight
[2])
151 for_each_possible_cpu(cpu
) {
152 inflight
[0] += part_stat_local_read_cpu(part
, in_flight
[0], cpu
);
153 inflight
[1] += part_stat_local_read_cpu(part
, in_flight
[1], cpu
);
155 if ((int)inflight
[0] < 0)
157 if ((int)inflight
[1] < 0)
161 struct block_device
*__disk_get_part(struct gendisk
*disk
, int partno
)
163 struct disk_part_tbl
*ptbl
= rcu_dereference(disk
->part_tbl
);
165 if (unlikely(partno
< 0 || partno
>= ptbl
->len
))
167 return rcu_dereference(ptbl
->part
[partno
]);
171 * disk_get_part - get partition
172 * @disk: disk to look partition from
173 * @partno: partition number
175 * Look for partition @partno from @disk. If found, increment
176 * reference count and return it.
182 * Pointer to the found partition on success, NULL if not found.
184 struct hd_struct
*disk_get_part(struct gendisk
*disk
, int partno
)
186 struct block_device
*bdev
;
187 struct hd_struct
*part
;
190 bdev
= __disk_get_part(disk
, partno
);
193 part
= bdev
->bd_part
;
194 if (!kobject_get_unless_zero(&part_to_dev(part
)->kobj
))
204 * disk_part_iter_init - initialize partition iterator
205 * @piter: iterator to initialize
206 * @disk: disk to iterate over
207 * @flags: DISK_PITER_* flags
209 * Initialize @piter so that it iterates over partitions of @disk.
214 void disk_part_iter_init(struct disk_part_iter
*piter
, struct gendisk
*disk
,
217 struct disk_part_tbl
*ptbl
;
220 ptbl
= rcu_dereference(disk
->part_tbl
);
225 if (flags
& DISK_PITER_REVERSE
)
226 piter
->idx
= ptbl
->len
- 1;
227 else if (flags
& (DISK_PITER_INCL_PART0
| DISK_PITER_INCL_EMPTY_PART0
))
232 piter
->flags
= flags
;
236 EXPORT_SYMBOL_GPL(disk_part_iter_init
);
239 * disk_part_iter_next - proceed iterator to the next partition and return it
240 * @piter: iterator of interest
242 * Proceed @piter to the next partition and return it.
247 struct hd_struct
*disk_part_iter_next(struct disk_part_iter
*piter
)
249 struct disk_part_tbl
*ptbl
;
252 /* put the last partition */
253 disk_part_iter_exit(piter
);
257 ptbl
= rcu_dereference(piter
->disk
->part_tbl
);
259 /* determine iteration parameters */
260 if (piter
->flags
& DISK_PITER_REVERSE
) {
262 if (piter
->flags
& (DISK_PITER_INCL_PART0
|
263 DISK_PITER_INCL_EMPTY_PART0
))
272 /* iterate to the next partition */
273 for (; piter
->idx
!= end
; piter
->idx
+= inc
) {
274 struct block_device
*part
;
276 part
= rcu_dereference(ptbl
->part
[piter
->idx
]);
279 if (!bdev_nr_sectors(part
) &&
280 !(piter
->flags
& DISK_PITER_INCL_EMPTY
) &&
281 !(piter
->flags
& DISK_PITER_INCL_EMPTY_PART0
&&
285 get_device(part_to_dev(part
->bd_part
));
286 piter
->part
= part
->bd_part
;
295 EXPORT_SYMBOL_GPL(disk_part_iter_next
);
298 * disk_part_iter_exit - finish up partition iteration
299 * @piter: iter of interest
301 * Called when iteration is over. Cleans up @piter.
306 void disk_part_iter_exit(struct disk_part_iter
*piter
)
308 disk_put_part(piter
->part
);
311 EXPORT_SYMBOL_GPL(disk_part_iter_exit
);
313 static inline int sector_in_part(struct block_device
*part
, sector_t sector
)
315 return part
->bd_start_sect
<= sector
&&
316 sector
< part
->bd_start_sect
+ bdev_nr_sectors(part
);
320 * disk_map_sector_rcu - map sector to partition
321 * @disk: gendisk of interest
322 * @sector: sector to map
324 * Find out which partition @sector maps to on @disk. This is
325 * primarily used for stats accounting.
331 * Found partition on success, part0 is returned if no partition matches
332 * or the matched partition is being deleted.
334 struct block_device
*disk_map_sector_rcu(struct gendisk
*disk
, sector_t sector
)
336 struct disk_part_tbl
*ptbl
;
337 struct block_device
*part
;
341 ptbl
= rcu_dereference(disk
->part_tbl
);
343 part
= rcu_dereference(ptbl
->last_lookup
);
344 if (part
&& sector_in_part(part
, sector
))
347 for (i
= 1; i
< ptbl
->len
; i
++) {
348 part
= rcu_dereference(ptbl
->part
[i
]);
350 if (part
&& sector_in_part(part
, sector
)) {
351 rcu_assign_pointer(ptbl
->last_lookup
, part
);
363 * disk_has_partitions
364 * @disk: gendisk of interest
366 * Walk through the partition table and check if valid partition exists.
372 * True if the gendisk has at least one valid non-zero size partition.
375 bool disk_has_partitions(struct gendisk
*disk
)
377 struct disk_part_tbl
*ptbl
;
382 ptbl
= rcu_dereference(disk
->part_tbl
);
384 /* Iterate partitions skipping the whole device at index 0 */
385 for (i
= 1; i
< ptbl
->len
; i
++) {
386 if (rcu_dereference(ptbl
->part
[i
])) {
396 EXPORT_SYMBOL_GPL(disk_has_partitions
);
399 * Can be deleted altogether. Later.
402 #define BLKDEV_MAJOR_HASH_SIZE 255
403 static struct blk_major_name
{
404 struct blk_major_name
*next
;
407 void (*probe
)(dev_t devt
);
408 } *major_names
[BLKDEV_MAJOR_HASH_SIZE
];
409 static DEFINE_MUTEX(major_names_lock
);
411 /* index in the above - for now: assume no multimajor ranges */
412 static inline int major_to_index(unsigned major
)
414 return major
% BLKDEV_MAJOR_HASH_SIZE
;
417 #ifdef CONFIG_PROC_FS
418 void blkdev_show(struct seq_file
*seqf
, off_t offset
)
420 struct blk_major_name
*dp
;
422 mutex_lock(&major_names_lock
);
423 for (dp
= major_names
[major_to_index(offset
)]; dp
; dp
= dp
->next
)
424 if (dp
->major
== offset
)
425 seq_printf(seqf
, "%3d %s\n", dp
->major
, dp
->name
);
426 mutex_unlock(&major_names_lock
);
428 #endif /* CONFIG_PROC_FS */
431 * __register_blkdev - register a new block device
433 * @major: the requested major device number [1..BLKDEV_MAJOR_MAX-1]. If
434 * @major = 0, try to allocate any unused major number.
435 * @name: the name of the new block device as a zero terminated string
436 * @probe: allback that is called on access to any minor number of @major
438 * The @name must be unique within the system.
440 * The return value depends on the @major input parameter:
442 * - if a major device number was requested in range [1..BLKDEV_MAJOR_MAX-1]
443 * then the function returns zero on success, or a negative error code
444 * - if any unused major number was requested with @major = 0 parameter
445 * then the return value is the allocated major number in range
446 * [1..BLKDEV_MAJOR_MAX-1] or a negative error code otherwise
448 * See Documentation/admin-guide/devices.txt for the list of allocated
451 * Use register_blkdev instead for any new code.
453 int __register_blkdev(unsigned int major
, const char *name
,
454 void (*probe
)(dev_t devt
))
456 struct blk_major_name
**n
, *p
;
459 mutex_lock(&major_names_lock
);
463 for (index
= ARRAY_SIZE(major_names
)-1; index
> 0; index
--) {
464 if (major_names
[index
] == NULL
)
469 printk("%s: failed to get major for %s\n",
478 if (major
>= BLKDEV_MAJOR_MAX
) {
479 pr_err("%s: major requested (%u) is greater than the maximum (%u) for %s\n",
480 __func__
, major
, BLKDEV_MAJOR_MAX
-1, name
);
486 p
= kmalloc(sizeof(struct blk_major_name
), GFP_KERNEL
);
494 strlcpy(p
->name
, name
, sizeof(p
->name
));
496 index
= major_to_index(major
);
498 for (n
= &major_names
[index
]; *n
; n
= &(*n
)->next
) {
499 if ((*n
)->major
== major
)
508 printk("register_blkdev: cannot get major %u for %s\n",
513 mutex_unlock(&major_names_lock
);
516 EXPORT_SYMBOL(__register_blkdev
);
518 void unregister_blkdev(unsigned int major
, const char *name
)
520 struct blk_major_name
**n
;
521 struct blk_major_name
*p
= NULL
;
522 int index
= major_to_index(major
);
524 mutex_lock(&major_names_lock
);
525 for (n
= &major_names
[index
]; *n
; n
= &(*n
)->next
)
526 if ((*n
)->major
== major
)
528 if (!*n
|| strcmp((*n
)->name
, name
)) {
534 mutex_unlock(&major_names_lock
);
538 EXPORT_SYMBOL(unregister_blkdev
);
541 * blk_mangle_minor - scatter minor numbers apart
542 * @minor: minor number to mangle
544 * Scatter consecutively allocated @minor number apart if MANGLE_DEVT
545 * is enabled. Mangling twice gives the original value.
553 static int blk_mangle_minor(int minor
)
555 #ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT
558 for (i
= 0; i
< MINORBITS
/ 2; i
++) {
559 int low
= minor
& (1 << i
);
560 int high
= minor
& (1 << (MINORBITS
- 1 - i
));
561 int distance
= MINORBITS
- 1 - 2 * i
;
563 minor
^= low
| high
; /* clear both bits */
564 low
<<= distance
; /* swap the positions */
566 minor
|= low
| high
; /* and set */
573 * blk_alloc_devt - allocate a dev_t for a block device
574 * @bdev: block device to allocate dev_t for
575 * @devt: out parameter for resulting dev_t
577 * Allocate a dev_t for block device.
580 * 0 on success, allocated dev_t is returned in *@devt. -errno on
586 int blk_alloc_devt(struct block_device
*bdev
, dev_t
*devt
)
588 struct gendisk
*disk
= bdev
->bd_disk
;
591 /* in consecutive minor range? */
592 if (bdev
->bd_partno
< disk
->minors
) {
593 *devt
= MKDEV(disk
->major
, disk
->first_minor
+ bdev
->bd_partno
);
597 idx
= ida_alloc_range(&ext_devt_ida
, 0, NR_EXT_DEVT
, GFP_KERNEL
);
599 return idx
== -ENOSPC
? -EBUSY
: idx
;
601 *devt
= MKDEV(BLOCK_EXT_MAJOR
, blk_mangle_minor(idx
));
606 * blk_free_devt - free a dev_t
607 * @devt: dev_t to free
609 * Free @devt which was allocated using blk_alloc_devt().
614 void blk_free_devt(dev_t devt
)
616 if (MAJOR(devt
) == BLOCK_EXT_MAJOR
)
617 ida_free(&ext_devt_ida
, blk_mangle_minor(MINOR(devt
)));
620 static char *bdevt_str(dev_t devt
, char *buf
)
622 if (MAJOR(devt
) <= 0xff && MINOR(devt
) <= 0xff) {
623 char tbuf
[BDEVT_SIZE
];
624 snprintf(tbuf
, BDEVT_SIZE
, "%02x%02x", MAJOR(devt
), MINOR(devt
));
625 snprintf(buf
, BDEVT_SIZE
, "%-9s", tbuf
);
627 snprintf(buf
, BDEVT_SIZE
, "%03x:%05x", MAJOR(devt
), MINOR(devt
));
632 static void disk_scan_partitions(struct gendisk
*disk
)
634 struct block_device
*bdev
;
636 if (!get_capacity(disk
) || !disk_part_scan_enabled(disk
))
639 set_bit(GD_NEED_PART_SCAN
, &disk
->state
);
640 bdev
= blkdev_get_by_dev(disk_devt(disk
), FMODE_READ
, NULL
);
642 blkdev_put(bdev
, FMODE_READ
);
645 static void register_disk(struct device
*parent
, struct gendisk
*disk
,
646 const struct attribute_group
**groups
)
648 struct device
*ddev
= disk_to_dev(disk
);
649 struct disk_part_iter piter
;
650 struct hd_struct
*part
;
653 ddev
->parent
= parent
;
655 dev_set_name(ddev
, "%s", disk
->disk_name
);
657 /* delay uevents, until we scanned partition table */
658 dev_set_uevent_suppress(ddev
, 1);
661 WARN_ON(ddev
->groups
);
662 ddev
->groups
= groups
;
664 if (device_add(ddev
))
666 if (!sysfs_deprecated
) {
667 err
= sysfs_create_link(block_depr
, &ddev
->kobj
,
668 kobject_name(&ddev
->kobj
));
676 * avoid probable deadlock caused by allocating memory with
677 * GFP_KERNEL in runtime_resume callback of its all ancestor
680 pm_runtime_set_memalloc_noio(ddev
, true);
682 disk
->part0
->bd_holder_dir
=
683 kobject_create_and_add("holders", &ddev
->kobj
);
684 disk
->slave_dir
= kobject_create_and_add("slaves", &ddev
->kobj
);
686 if (disk
->flags
& GENHD_FL_HIDDEN
) {
687 dev_set_uevent_suppress(ddev
, 0);
691 disk_scan_partitions(disk
);
693 /* announce disk after possible partitions are created */
694 dev_set_uevent_suppress(ddev
, 0);
695 kobject_uevent(&ddev
->kobj
, KOBJ_ADD
);
697 /* announce possible partitions */
698 disk_part_iter_init(&piter
, disk
, 0);
699 while ((part
= disk_part_iter_next(&piter
)))
700 kobject_uevent(&part_to_dev(part
)->kobj
, KOBJ_ADD
);
701 disk_part_iter_exit(&piter
);
703 if (disk
->queue
->backing_dev_info
->dev
) {
704 err
= sysfs_create_link(&ddev
->kobj
,
705 &disk
->queue
->backing_dev_info
->dev
->kobj
,
712 * __device_add_disk - add disk information to kernel list
713 * @parent: parent device for the disk
714 * @disk: per-device partitioning information
715 * @groups: Additional per-device sysfs groups
716 * @register_queue: register the queue if set to true
718 * This function registers the partitioning information in @disk
721 * FIXME: error handling
723 static void __device_add_disk(struct device
*parent
, struct gendisk
*disk
,
724 const struct attribute_group
**groups
,
731 * The disk queue should now be all set with enough information about
732 * the device for the elevator code to pick an adequate default
733 * elevator if one is needed, that is, for devices requesting queue
737 elevator_init_mq(disk
->queue
);
739 /* minors == 0 indicates to use ext devt from part0 and should
740 * be accompanied with EXT_DEVT flag. Make sure all
741 * parameters make sense.
743 WARN_ON(disk
->minors
&& !(disk
->major
|| disk
->first_minor
));
744 WARN_ON(!disk
->minors
&&
745 !(disk
->flags
& (GENHD_FL_EXT_DEVT
| GENHD_FL_HIDDEN
)));
747 disk
->flags
|= GENHD_FL_UP
;
749 retval
= blk_alloc_devt(disk
->part0
, &devt
);
754 disk
->major
= MAJOR(devt
);
755 disk
->first_minor
= MINOR(devt
);
757 disk_alloc_events(disk
);
759 if (disk
->flags
& GENHD_FL_HIDDEN
) {
761 * Don't let hidden disks show up in /proc/partitions,
762 * and don't bother scanning for partitions either.
764 disk
->flags
|= GENHD_FL_SUPPRESS_PARTITION_INFO
;
765 disk
->flags
|= GENHD_FL_NO_PART_SCAN
;
767 struct backing_dev_info
*bdi
= disk
->queue
->backing_dev_info
;
768 struct device
*dev
= disk_to_dev(disk
);
771 /* Register BDI before referencing it from bdev */
773 ret
= bdi_register(bdi
, "%u:%u", MAJOR(devt
), MINOR(devt
));
775 bdi_set_owner(bdi
, dev
);
776 bdev_add(disk
->part0
, devt
);
778 register_disk(parent
, disk
, groups
);
780 blk_register_queue(disk
);
783 * Take an extra ref on queue which will be put on disk_release()
784 * so that it sticks around as long as @disk is there.
786 WARN_ON_ONCE(!blk_get_queue(disk
->queue
));
788 disk_add_events(disk
);
789 blk_integrity_add(disk
);
792 void device_add_disk(struct device
*parent
, struct gendisk
*disk
,
793 const struct attribute_group
**groups
)
796 __device_add_disk(parent
, disk
, groups
, true);
798 EXPORT_SYMBOL(device_add_disk
);
800 void device_add_disk_no_queue_reg(struct device
*parent
, struct gendisk
*disk
)
802 __device_add_disk(parent
, disk
, NULL
, false);
804 EXPORT_SYMBOL(device_add_disk_no_queue_reg
);
806 static void invalidate_partition(struct gendisk
*disk
, int partno
)
808 struct block_device
*bdev
;
810 bdev
= bdget_disk(disk
, partno
);
815 __invalidate_device(bdev
, true);
818 * Unhash the bdev inode for this device so that it can't be looked
819 * up any more even if openers still hold references to it.
821 remove_inode_hash(bdev
->bd_inode
);
826 * del_gendisk - remove the gendisk
827 * @disk: the struct gendisk to remove
829 * Removes the gendisk and all its associated resources. This deletes the
830 * partitions associated with the gendisk, and unregisters the associated
833 * This is the counter to the respective __device_add_disk() call.
835 * The final removal of the struct gendisk happens when its refcount reaches 0
836 * with put_disk(), which should be called after del_gendisk(), if
837 * __device_add_disk() was used.
839 * Drivers exist which depend on the release of the gendisk to be synchronous,
840 * it should not be deferred.
844 void del_gendisk(struct gendisk
*disk
)
846 struct disk_part_iter piter
;
847 struct hd_struct
*part
;
851 if (WARN_ON_ONCE(!disk
->queue
))
854 blk_integrity_del(disk
);
855 disk_del_events(disk
);
858 * Block lookups of the disk until all bdevs are unhashed and the
859 * disk is marked as dead (GENHD_FL_UP cleared).
861 down_write(&bdev_lookup_sem
);
863 /* invalidate stuff */
864 disk_part_iter_init(&piter
, disk
,
865 DISK_PITER_INCL_EMPTY
| DISK_PITER_REVERSE
);
866 while ((part
= disk_part_iter_next(&piter
))) {
867 invalidate_partition(disk
, part
->bdev
->bd_partno
);
868 delete_partition(part
);
870 disk_part_iter_exit(&piter
);
872 invalidate_partition(disk
, 0);
873 set_capacity(disk
, 0);
874 disk
->flags
&= ~GENHD_FL_UP
;
875 up_write(&bdev_lookup_sem
);
877 if (!(disk
->flags
& GENHD_FL_HIDDEN
)) {
878 sysfs_remove_link(&disk_to_dev(disk
)->kobj
, "bdi");
881 * Unregister bdi before releasing device numbers (as they can
882 * get reused and we'd get clashes in sysfs).
884 bdi_unregister(disk
->queue
->backing_dev_info
);
887 blk_unregister_queue(disk
);
889 kobject_put(disk
->part0
->bd_holder_dir
);
890 kobject_put(disk
->slave_dir
);
892 part_stat_set_all(disk
->part0
, 0);
893 disk
->part0
->bd_stamp
= 0;
894 if (!sysfs_deprecated
)
895 sysfs_remove_link(block_depr
, dev_name(disk_to_dev(disk
)));
896 pm_runtime_set_memalloc_noio(disk_to_dev(disk
), false);
897 device_del(disk_to_dev(disk
));
899 EXPORT_SYMBOL(del_gendisk
);
901 /* sysfs access to bad-blocks list. */
902 static ssize_t
disk_badblocks_show(struct device
*dev
,
903 struct device_attribute
*attr
,
906 struct gendisk
*disk
= dev_to_disk(dev
);
909 return sprintf(page
, "\n");
911 return badblocks_show(disk
->bb
, page
, 0);
914 static ssize_t
disk_badblocks_store(struct device
*dev
,
915 struct device_attribute
*attr
,
916 const char *page
, size_t len
)
918 struct gendisk
*disk
= dev_to_disk(dev
);
923 return badblocks_store(disk
->bb
, page
, len
, 0);
926 void blk_request_module(dev_t devt
)
928 unsigned int major
= MAJOR(devt
);
929 struct blk_major_name
**n
;
931 mutex_lock(&major_names_lock
);
932 for (n
= &major_names
[major_to_index(major
)]; *n
; n
= &(*n
)->next
) {
933 if ((*n
)->major
== major
&& (*n
)->probe
) {
935 mutex_unlock(&major_names_lock
);
939 mutex_unlock(&major_names_lock
);
941 if (request_module("block-major-%d-%d", MAJOR(devt
), MINOR(devt
)) > 0)
942 /* Make old-style 2.4 aliases work */
943 request_module("block-major-%d", MAJOR(devt
));
947 * bdget_disk - do bdget() by gendisk and partition number
948 * @disk: gendisk of interest
949 * @partno: partition number
951 * Find partition @partno from @disk, do bdget() on it.
957 * Resulting block_device on success, NULL on failure.
959 struct block_device
*bdget_disk(struct gendisk
*disk
, int partno
)
961 struct hd_struct
*part
;
962 struct block_device
*bdev
= NULL
;
964 part
= disk_get_part(disk
, partno
);
966 bdev
= bdget_part(part
);
971 EXPORT_SYMBOL(bdget_disk
);
974 * print a full list of all partitions - intended for places where the root
975 * filesystem can't be mounted and thus to give the victim some idea of what
978 void __init
printk_all_partitions(void)
980 struct class_dev_iter iter
;
983 class_dev_iter_init(&iter
, &block_class
, NULL
, &disk_type
);
984 while ((dev
= class_dev_iter_next(&iter
))) {
985 struct gendisk
*disk
= dev_to_disk(dev
);
986 struct disk_part_iter piter
;
987 struct hd_struct
*part
;
988 char name_buf
[BDEVNAME_SIZE
];
989 char devt_buf
[BDEVT_SIZE
];
992 * Don't show empty devices or things that have been
995 if (get_capacity(disk
) == 0 ||
996 (disk
->flags
& GENHD_FL_SUPPRESS_PARTITION_INFO
))
1000 * Note, unlike /proc/partitions, I am showing the
1001 * numbers in hex - the same format as the root=
1004 disk_part_iter_init(&piter
, disk
, DISK_PITER_INCL_PART0
);
1005 while ((part
= disk_part_iter_next(&piter
))) {
1006 bool is_part0
= part
== disk
->part0
->bd_part
;
1008 printk("%s%s %10llu %s %s", is_part0
? "" : " ",
1009 bdevt_str(part_devt(part
), devt_buf
),
1010 bdev_nr_sectors(part
->bdev
) >> 1,
1011 disk_name(disk
, part
->bdev
->bd_partno
, name_buf
),
1012 part
->bdev
->bd_meta_info
?
1013 part
->bdev
->bd_meta_info
->uuid
: "");
1015 if (dev
->parent
&& dev
->parent
->driver
)
1016 printk(" driver: %s\n",
1017 dev
->parent
->driver
->name
);
1019 printk(" (driver?)\n");
1023 disk_part_iter_exit(&piter
);
1025 class_dev_iter_exit(&iter
);
1028 #ifdef CONFIG_PROC_FS
1030 static void *disk_seqf_start(struct seq_file
*seqf
, loff_t
*pos
)
1033 struct class_dev_iter
*iter
;
1036 iter
= kmalloc(sizeof(*iter
), GFP_KERNEL
);
1038 return ERR_PTR(-ENOMEM
);
1040 seqf
->private = iter
;
1041 class_dev_iter_init(iter
, &block_class
, NULL
, &disk_type
);
1043 dev
= class_dev_iter_next(iter
);
1048 return dev_to_disk(dev
);
1051 static void *disk_seqf_next(struct seq_file
*seqf
, void *v
, loff_t
*pos
)
1056 dev
= class_dev_iter_next(seqf
->private);
1058 return dev_to_disk(dev
);
1063 static void disk_seqf_stop(struct seq_file
*seqf
, void *v
)
1065 struct class_dev_iter
*iter
= seqf
->private;
1067 /* stop is called even after start failed :-( */
1069 class_dev_iter_exit(iter
);
1071 seqf
->private = NULL
;
1075 static void *show_partition_start(struct seq_file
*seqf
, loff_t
*pos
)
1079 p
= disk_seqf_start(seqf
, pos
);
1080 if (!IS_ERR_OR_NULL(p
) && !*pos
)
1081 seq_puts(seqf
, "major minor #blocks name\n\n");
1085 static int show_partition(struct seq_file
*seqf
, void *v
)
1087 struct gendisk
*sgp
= v
;
1088 struct disk_part_iter piter
;
1089 struct hd_struct
*part
;
1090 char buf
[BDEVNAME_SIZE
];
1092 /* Don't show non-partitionable removeable devices or empty devices */
1093 if (!get_capacity(sgp
) || (!disk_max_parts(sgp
) &&
1094 (sgp
->flags
& GENHD_FL_REMOVABLE
)))
1096 if (sgp
->flags
& GENHD_FL_SUPPRESS_PARTITION_INFO
)
1099 /* show the full disk and all non-0 size partitions of it */
1100 disk_part_iter_init(&piter
, sgp
, DISK_PITER_INCL_PART0
);
1101 while ((part
= disk_part_iter_next(&piter
)))
1102 seq_printf(seqf
, "%4d %7d %10llu %s\n",
1103 MAJOR(part_devt(part
)), MINOR(part_devt(part
)),
1104 bdev_nr_sectors(part
->bdev
) >> 1,
1105 disk_name(sgp
, part
->bdev
->bd_partno
, buf
));
1106 disk_part_iter_exit(&piter
);
1111 static const struct seq_operations partitions_op
= {
1112 .start
= show_partition_start
,
1113 .next
= disk_seqf_next
,
1114 .stop
= disk_seqf_stop
,
1115 .show
= show_partition
1119 static int __init
genhd_device_init(void)
1123 block_class
.dev_kobj
= sysfs_dev_block_kobj
;
1124 error
= class_register(&block_class
);
1125 if (unlikely(error
))
1129 register_blkdev(BLOCK_EXT_MAJOR
, "blkext");
1131 /* create top-level block dir */
1132 if (!sysfs_deprecated
)
1133 block_depr
= kobject_create_and_add("block", NULL
);
1137 subsys_initcall(genhd_device_init
);
1139 static ssize_t
disk_range_show(struct device
*dev
,
1140 struct device_attribute
*attr
, char *buf
)
1142 struct gendisk
*disk
= dev_to_disk(dev
);
1144 return sprintf(buf
, "%d\n", disk
->minors
);
1147 static ssize_t
disk_ext_range_show(struct device
*dev
,
1148 struct device_attribute
*attr
, char *buf
)
1150 struct gendisk
*disk
= dev_to_disk(dev
);
1152 return sprintf(buf
, "%d\n", disk_max_parts(disk
));
1155 static ssize_t
disk_removable_show(struct device
*dev
,
1156 struct device_attribute
*attr
, char *buf
)
1158 struct gendisk
*disk
= dev_to_disk(dev
);
1160 return sprintf(buf
, "%d\n",
1161 (disk
->flags
& GENHD_FL_REMOVABLE
? 1 : 0));
1164 static ssize_t
disk_hidden_show(struct device
*dev
,
1165 struct device_attribute
*attr
, char *buf
)
1167 struct gendisk
*disk
= dev_to_disk(dev
);
1169 return sprintf(buf
, "%d\n",
1170 (disk
->flags
& GENHD_FL_HIDDEN
? 1 : 0));
1173 static ssize_t
disk_ro_show(struct device
*dev
,
1174 struct device_attribute
*attr
, char *buf
)
1176 struct gendisk
*disk
= dev_to_disk(dev
);
1178 return sprintf(buf
, "%d\n", get_disk_ro(disk
) ? 1 : 0);
1181 ssize_t
part_size_show(struct device
*dev
,
1182 struct device_attribute
*attr
, char *buf
)
1184 struct hd_struct
*p
= dev_to_part(dev
);
1186 return sprintf(buf
, "%llu\n", bdev_nr_sectors(p
->bdev
));
1189 ssize_t
part_stat_show(struct device
*dev
,
1190 struct device_attribute
*attr
, char *buf
)
1192 struct hd_struct
*p
= dev_to_part(dev
);
1193 struct request_queue
*q
= part_to_disk(p
)->queue
;
1194 struct disk_stats stat
;
1195 unsigned int inflight
;
1197 part_stat_read_all(p
, &stat
);
1199 inflight
= blk_mq_in_flight(q
, p
->bdev
);
1201 inflight
= part_in_flight(p
->bdev
);
1204 "%8lu %8lu %8llu %8u "
1205 "%8lu %8lu %8llu %8u "
1207 "%8lu %8lu %8llu %8u "
1210 stat
.ios
[STAT_READ
],
1211 stat
.merges
[STAT_READ
],
1212 (unsigned long long)stat
.sectors
[STAT_READ
],
1213 (unsigned int)div_u64(stat
.nsecs
[STAT_READ
], NSEC_PER_MSEC
),
1214 stat
.ios
[STAT_WRITE
],
1215 stat
.merges
[STAT_WRITE
],
1216 (unsigned long long)stat
.sectors
[STAT_WRITE
],
1217 (unsigned int)div_u64(stat
.nsecs
[STAT_WRITE
], NSEC_PER_MSEC
),
1219 jiffies_to_msecs(stat
.io_ticks
),
1220 (unsigned int)div_u64(stat
.nsecs
[STAT_READ
] +
1221 stat
.nsecs
[STAT_WRITE
] +
1222 stat
.nsecs
[STAT_DISCARD
] +
1223 stat
.nsecs
[STAT_FLUSH
],
1225 stat
.ios
[STAT_DISCARD
],
1226 stat
.merges
[STAT_DISCARD
],
1227 (unsigned long long)stat
.sectors
[STAT_DISCARD
],
1228 (unsigned int)div_u64(stat
.nsecs
[STAT_DISCARD
], NSEC_PER_MSEC
),
1229 stat
.ios
[STAT_FLUSH
],
1230 (unsigned int)div_u64(stat
.nsecs
[STAT_FLUSH
], NSEC_PER_MSEC
));
1233 ssize_t
part_inflight_show(struct device
*dev
, struct device_attribute
*attr
,
1236 struct hd_struct
*p
= dev_to_part(dev
);
1237 struct request_queue
*q
= part_to_disk(p
)->queue
;
1238 unsigned int inflight
[2];
1241 blk_mq_in_flight_rw(q
, p
->bdev
, inflight
);
1243 part_in_flight_rw(p
->bdev
, inflight
);
1245 return sprintf(buf
, "%8u %8u\n", inflight
[0], inflight
[1]);
1248 static ssize_t
disk_capability_show(struct device
*dev
,
1249 struct device_attribute
*attr
, char *buf
)
1251 struct gendisk
*disk
= dev_to_disk(dev
);
1253 return sprintf(buf
, "%x\n", disk
->flags
);
1256 static ssize_t
disk_alignment_offset_show(struct device
*dev
,
1257 struct device_attribute
*attr
,
1260 struct gendisk
*disk
= dev_to_disk(dev
);
1262 return sprintf(buf
, "%d\n", queue_alignment_offset(disk
->queue
));
1265 static ssize_t
disk_discard_alignment_show(struct device
*dev
,
1266 struct device_attribute
*attr
,
1269 struct gendisk
*disk
= dev_to_disk(dev
);
1271 return sprintf(buf
, "%d\n", queue_discard_alignment(disk
->queue
));
1274 static DEVICE_ATTR(range
, 0444, disk_range_show
, NULL
);
1275 static DEVICE_ATTR(ext_range
, 0444, disk_ext_range_show
, NULL
);
1276 static DEVICE_ATTR(removable
, 0444, disk_removable_show
, NULL
);
1277 static DEVICE_ATTR(hidden
, 0444, disk_hidden_show
, NULL
);
1278 static DEVICE_ATTR(ro
, 0444, disk_ro_show
, NULL
);
1279 static DEVICE_ATTR(size
, 0444, part_size_show
, NULL
);
1280 static DEVICE_ATTR(alignment_offset
, 0444, disk_alignment_offset_show
, NULL
);
1281 static DEVICE_ATTR(discard_alignment
, 0444, disk_discard_alignment_show
, NULL
);
1282 static DEVICE_ATTR(capability
, 0444, disk_capability_show
, NULL
);
1283 static DEVICE_ATTR(stat
, 0444, part_stat_show
, NULL
);
1284 static DEVICE_ATTR(inflight
, 0444, part_inflight_show
, NULL
);
1285 static DEVICE_ATTR(badblocks
, 0644, disk_badblocks_show
, disk_badblocks_store
);
1287 #ifdef CONFIG_FAIL_MAKE_REQUEST
1288 ssize_t
part_fail_show(struct device
*dev
,
1289 struct device_attribute
*attr
, char *buf
)
1291 struct hd_struct
*p
= dev_to_part(dev
);
1293 return sprintf(buf
, "%d\n", p
->bdev
->bd_make_it_fail
);
1296 ssize_t
part_fail_store(struct device
*dev
,
1297 struct device_attribute
*attr
,
1298 const char *buf
, size_t count
)
1300 struct hd_struct
*p
= dev_to_part(dev
);
1303 if (count
> 0 && sscanf(buf
, "%d", &i
) > 0)
1304 p
->bdev
->bd_make_it_fail
= i
;
1309 static struct device_attribute dev_attr_fail
=
1310 __ATTR(make
-it
-fail
, 0644, part_fail_show
, part_fail_store
);
1311 #endif /* CONFIG_FAIL_MAKE_REQUEST */
1313 #ifdef CONFIG_FAIL_IO_TIMEOUT
1314 static struct device_attribute dev_attr_fail_timeout
=
1315 __ATTR(io
-timeout
-fail
, 0644, part_timeout_show
, part_timeout_store
);
1318 static struct attribute
*disk_attrs
[] = {
1319 &dev_attr_range
.attr
,
1320 &dev_attr_ext_range
.attr
,
1321 &dev_attr_removable
.attr
,
1322 &dev_attr_hidden
.attr
,
1324 &dev_attr_size
.attr
,
1325 &dev_attr_alignment_offset
.attr
,
1326 &dev_attr_discard_alignment
.attr
,
1327 &dev_attr_capability
.attr
,
1328 &dev_attr_stat
.attr
,
1329 &dev_attr_inflight
.attr
,
1330 &dev_attr_badblocks
.attr
,
1331 #ifdef CONFIG_FAIL_MAKE_REQUEST
1332 &dev_attr_fail
.attr
,
1334 #ifdef CONFIG_FAIL_IO_TIMEOUT
1335 &dev_attr_fail_timeout
.attr
,
1340 static umode_t
disk_visible(struct kobject
*kobj
, struct attribute
*a
, int n
)
1342 struct device
*dev
= container_of(kobj
, typeof(*dev
), kobj
);
1343 struct gendisk
*disk
= dev_to_disk(dev
);
1345 if (a
== &dev_attr_badblocks
.attr
&& !disk
->bb
)
1350 static struct attribute_group disk_attr_group
= {
1351 .attrs
= disk_attrs
,
1352 .is_visible
= disk_visible
,
1355 static const struct attribute_group
*disk_attr_groups
[] = {
1361 * disk_replace_part_tbl - replace disk->part_tbl in RCU-safe way
1362 * @disk: disk to replace part_tbl for
1363 * @new_ptbl: new part_tbl to install
1365 * Replace disk->part_tbl with @new_ptbl in RCU-safe way. The
1366 * original ptbl is freed using RCU callback.
1369 * Matching bd_mutex locked or the caller is the only user of @disk.
1371 static void disk_replace_part_tbl(struct gendisk
*disk
,
1372 struct disk_part_tbl
*new_ptbl
)
1374 struct disk_part_tbl
*old_ptbl
=
1375 rcu_dereference_protected(disk
->part_tbl
, 1);
1377 rcu_assign_pointer(disk
->part_tbl
, new_ptbl
);
1380 rcu_assign_pointer(old_ptbl
->last_lookup
, NULL
);
1381 kfree_rcu(old_ptbl
, rcu_head
);
1386 * disk_expand_part_tbl - expand disk->part_tbl
1387 * @disk: disk to expand part_tbl for
1388 * @partno: expand such that this partno can fit in
1390 * Expand disk->part_tbl such that @partno can fit in. disk->part_tbl
1391 * uses RCU to allow unlocked dereferencing for stats and other stuff.
1394 * Matching bd_mutex locked or the caller is the only user of @disk.
1398 * 0 on success, -errno on failure.
1400 int disk_expand_part_tbl(struct gendisk
*disk
, int partno
)
1402 struct disk_part_tbl
*old_ptbl
=
1403 rcu_dereference_protected(disk
->part_tbl
, 1);
1404 struct disk_part_tbl
*new_ptbl
;
1405 int len
= old_ptbl
? old_ptbl
->len
: 0;
1409 * check for int overflow, since we can get here from blkpg_ioctl()
1410 * with a user passed 'partno'.
1412 target
= partno
+ 1;
1416 /* disk_max_parts() is zero during initialization, ignore if so */
1417 if (disk_max_parts(disk
) && target
> disk_max_parts(disk
))
1423 new_ptbl
= kzalloc_node(struct_size(new_ptbl
, part
, target
), GFP_KERNEL
,
1428 new_ptbl
->len
= target
;
1430 for (i
= 0; i
< len
; i
++)
1431 rcu_assign_pointer(new_ptbl
->part
[i
], old_ptbl
->part
[i
]);
1433 disk_replace_part_tbl(disk
, new_ptbl
);
1438 * disk_release - releases all allocated resources of the gendisk
1439 * @dev: the device representing this disk
1441 * This function releases all allocated resources of the gendisk.
1443 * Drivers which used __device_add_disk() have a gendisk with a request_queue
1444 * assigned. Since the request_queue sits on top of the gendisk for these
1445 * drivers we also call blk_put_queue() for them, and we expect the
1446 * request_queue refcount to reach 0 at this point, and so the request_queue
1447 * will also be freed prior to the disk.
1449 * Context: can sleep
1451 static void disk_release(struct device
*dev
)
1453 struct gendisk
*disk
= dev_to_disk(dev
);
1457 blk_free_devt(dev
->devt
);
1458 disk_release_events(disk
);
1459 kfree(disk
->random
);
1460 disk_replace_part_tbl(disk
, NULL
);
1463 blk_put_queue(disk
->queue
);
1466 struct class block_class
= {
1470 static char *block_devnode(struct device
*dev
, umode_t
*mode
,
1471 kuid_t
*uid
, kgid_t
*gid
)
1473 struct gendisk
*disk
= dev_to_disk(dev
);
1475 if (disk
->fops
->devnode
)
1476 return disk
->fops
->devnode(disk
, mode
);
1480 const struct device_type disk_type
= {
1482 .groups
= disk_attr_groups
,
1483 .release
= disk_release
,
1484 .devnode
= block_devnode
,
1487 #ifdef CONFIG_PROC_FS
1489 * aggregate disk stat collector. Uses the same stats that the sysfs
1490 * entries do, above, but makes them available through one seq_file.
1492 * The output looks suspiciously like /proc/partitions with a bunch of
1495 static int diskstats_show(struct seq_file
*seqf
, void *v
)
1497 struct gendisk
*gp
= v
;
1498 struct disk_part_iter piter
;
1499 struct hd_struct
*hd
;
1500 char buf
[BDEVNAME_SIZE
];
1501 unsigned int inflight
;
1502 struct disk_stats stat
;
1505 if (&disk_to_dev(gp)->kobj.entry == block_class.devices.next)
1506 seq_puts(seqf, "major minor name"
1507 " rio rmerge rsect ruse wio wmerge "
1508 "wsect wuse running use aveq"
1512 disk_part_iter_init(&piter
, gp
, DISK_PITER_INCL_EMPTY_PART0
);
1513 while ((hd
= disk_part_iter_next(&piter
))) {
1514 part_stat_read_all(hd
, &stat
);
1515 if (queue_is_mq(gp
->queue
))
1516 inflight
= blk_mq_in_flight(gp
->queue
, hd
->bdev
);
1518 inflight
= part_in_flight(hd
->bdev
);
1520 seq_printf(seqf
, "%4d %7d %s "
1527 MAJOR(part_devt(hd
)), MINOR(part_devt(hd
)),
1528 disk_name(gp
, hd
->bdev
->bd_partno
, buf
),
1529 stat
.ios
[STAT_READ
],
1530 stat
.merges
[STAT_READ
],
1531 stat
.sectors
[STAT_READ
],
1532 (unsigned int)div_u64(stat
.nsecs
[STAT_READ
],
1534 stat
.ios
[STAT_WRITE
],
1535 stat
.merges
[STAT_WRITE
],
1536 stat
.sectors
[STAT_WRITE
],
1537 (unsigned int)div_u64(stat
.nsecs
[STAT_WRITE
],
1540 jiffies_to_msecs(stat
.io_ticks
),
1541 (unsigned int)div_u64(stat
.nsecs
[STAT_READ
] +
1542 stat
.nsecs
[STAT_WRITE
] +
1543 stat
.nsecs
[STAT_DISCARD
] +
1544 stat
.nsecs
[STAT_FLUSH
],
1546 stat
.ios
[STAT_DISCARD
],
1547 stat
.merges
[STAT_DISCARD
],
1548 stat
.sectors
[STAT_DISCARD
],
1549 (unsigned int)div_u64(stat
.nsecs
[STAT_DISCARD
],
1551 stat
.ios
[STAT_FLUSH
],
1552 (unsigned int)div_u64(stat
.nsecs
[STAT_FLUSH
],
1556 disk_part_iter_exit(&piter
);
1561 static const struct seq_operations diskstats_op
= {
1562 .start
= disk_seqf_start
,
1563 .next
= disk_seqf_next
,
1564 .stop
= disk_seqf_stop
,
1565 .show
= diskstats_show
1568 static int __init
proc_genhd_init(void)
1570 proc_create_seq("diskstats", 0, NULL
, &diskstats_op
);
1571 proc_create_seq("partitions", 0, NULL
, &partitions_op
);
1574 module_init(proc_genhd_init
);
1575 #endif /* CONFIG_PROC_FS */
1577 dev_t
blk_lookup_devt(const char *name
, int partno
)
1579 dev_t devt
= MKDEV(0, 0);
1580 struct class_dev_iter iter
;
1583 class_dev_iter_init(&iter
, &block_class
, NULL
, &disk_type
);
1584 while ((dev
= class_dev_iter_next(&iter
))) {
1585 struct gendisk
*disk
= dev_to_disk(dev
);
1586 struct hd_struct
*part
;
1588 if (strcmp(dev_name(dev
), name
))
1591 if (partno
< disk
->minors
) {
1592 /* We need to return the right devno, even
1593 * if the partition doesn't exist yet.
1595 devt
= MKDEV(MAJOR(dev
->devt
),
1596 MINOR(dev
->devt
) + partno
);
1599 part
= disk_get_part(disk
, partno
);
1601 devt
= part_devt(part
);
1602 disk_put_part(part
);
1605 disk_put_part(part
);
1607 class_dev_iter_exit(&iter
);
1611 struct gendisk
*__alloc_disk_node(int minors
, int node_id
)
1613 struct gendisk
*disk
;
1614 struct disk_part_tbl
*ptbl
;
1616 if (minors
> DISK_MAX_PARTS
) {
1618 "block: can't allocate more than %d partitions\n",
1620 minors
= DISK_MAX_PARTS
;
1623 disk
= kzalloc_node(sizeof(struct gendisk
), GFP_KERNEL
, node_id
);
1627 disk
->part0
= bdev_alloc(disk
, 0);
1631 disk
->node_id
= node_id
;
1632 if (disk_expand_part_tbl(disk
, 0))
1635 ptbl
= rcu_dereference_protected(disk
->part_tbl
, 1);
1636 rcu_assign_pointer(ptbl
->part
[0], disk
->part0
);
1638 disk
->minors
= minors
;
1639 rand_initialize_disk(disk
);
1640 disk_to_dev(disk
)->class = &block_class
;
1641 disk_to_dev(disk
)->type
= &disk_type
;
1642 device_initialize(disk_to_dev(disk
));
1651 EXPORT_SYMBOL(__alloc_disk_node
);
1654 * put_disk - decrements the gendisk refcount
1655 * @disk: the struct gendisk to decrement the refcount for
1657 * This decrements the refcount for the struct gendisk. When this reaches 0
1658 * we'll have disk_release() called.
1660 * Context: Any context, but the last reference must not be dropped from
1663 void put_disk(struct gendisk
*disk
)
1666 put_device(disk_to_dev(disk
));
1668 EXPORT_SYMBOL(put_disk
);
1670 static void set_disk_ro_uevent(struct gendisk
*gd
, int ro
)
1672 char event
[] = "DISK_RO=1";
1673 char *envp
[] = { event
, NULL
};
1677 kobject_uevent_env(&disk_to_dev(gd
)->kobj
, KOBJ_CHANGE
, envp
);
1680 void set_disk_ro(struct gendisk
*disk
, int flag
)
1682 struct disk_part_iter piter
;
1683 struct hd_struct
*part
;
1685 if (disk
->part0
->bd_read_only
!= flag
) {
1686 set_disk_ro_uevent(disk
, flag
);
1687 disk
->part0
->bd_read_only
= flag
;
1690 disk_part_iter_init(&piter
, disk
, DISK_PITER_INCL_EMPTY
);
1691 while ((part
= disk_part_iter_next(&piter
)))
1692 part
->bdev
->bd_read_only
= flag
;
1693 disk_part_iter_exit(&piter
);
1696 EXPORT_SYMBOL(set_disk_ro
);
1698 int bdev_read_only(struct block_device
*bdev
)
1702 return bdev
->bd_read_only
;
1705 EXPORT_SYMBOL(bdev_read_only
);
1708 * Disk events - monitor disk events like media change and eject request.
1710 struct disk_events
{
1711 struct list_head node
; /* all disk_event's */
1712 struct gendisk
*disk
; /* the associated disk */
1715 struct mutex block_mutex
; /* protects blocking */
1716 int block
; /* event blocking depth */
1717 unsigned int pending
; /* events already sent out */
1718 unsigned int clearing
; /* events being cleared */
1720 long poll_msecs
; /* interval, -1 for default */
1721 struct delayed_work dwork
;
1724 static const char *disk_events_strs
[] = {
1725 [ilog2(DISK_EVENT_MEDIA_CHANGE
)] = "media_change",
1726 [ilog2(DISK_EVENT_EJECT_REQUEST
)] = "eject_request",
1729 static char *disk_uevents
[] = {
1730 [ilog2(DISK_EVENT_MEDIA_CHANGE
)] = "DISK_MEDIA_CHANGE=1",
1731 [ilog2(DISK_EVENT_EJECT_REQUEST
)] = "DISK_EJECT_REQUEST=1",
1734 /* list of all disk_events */
1735 static DEFINE_MUTEX(disk_events_mutex
);
1736 static LIST_HEAD(disk_events
);
1738 /* disable in-kernel polling by default */
1739 static unsigned long disk_events_dfl_poll_msecs
;
1741 static unsigned long disk_events_poll_jiffies(struct gendisk
*disk
)
1743 struct disk_events
*ev
= disk
->ev
;
1744 long intv_msecs
= 0;
1747 * If device-specific poll interval is set, always use it. If
1748 * the default is being used, poll if the POLL flag is set.
1750 if (ev
->poll_msecs
>= 0)
1751 intv_msecs
= ev
->poll_msecs
;
1752 else if (disk
->event_flags
& DISK_EVENT_FLAG_POLL
)
1753 intv_msecs
= disk_events_dfl_poll_msecs
;
1755 return msecs_to_jiffies(intv_msecs
);
1759 * disk_block_events - block and flush disk event checking
1760 * @disk: disk to block events for
1762 * On return from this function, it is guaranteed that event checking
1763 * isn't in progress and won't happen until unblocked by
1764 * disk_unblock_events(). Events blocking is counted and the actual
1765 * unblocking happens after the matching number of unblocks are done.
1767 * Note that this intentionally does not block event checking from
1768 * disk_clear_events().
1773 void disk_block_events(struct gendisk
*disk
)
1775 struct disk_events
*ev
= disk
->ev
;
1776 unsigned long flags
;
1783 * Outer mutex ensures that the first blocker completes canceling
1784 * the event work before further blockers are allowed to finish.
1786 mutex_lock(&ev
->block_mutex
);
1788 spin_lock_irqsave(&ev
->lock
, flags
);
1789 cancel
= !ev
->block
++;
1790 spin_unlock_irqrestore(&ev
->lock
, flags
);
1793 cancel_delayed_work_sync(&disk
->ev
->dwork
);
1795 mutex_unlock(&ev
->block_mutex
);
1798 static void __disk_unblock_events(struct gendisk
*disk
, bool check_now
)
1800 struct disk_events
*ev
= disk
->ev
;
1802 unsigned long flags
;
1804 spin_lock_irqsave(&ev
->lock
, flags
);
1806 if (WARN_ON_ONCE(ev
->block
<= 0))
1812 intv
= disk_events_poll_jiffies(disk
);
1814 queue_delayed_work(system_freezable_power_efficient_wq
,
1817 queue_delayed_work(system_freezable_power_efficient_wq
,
1820 spin_unlock_irqrestore(&ev
->lock
, flags
);
1824 * disk_unblock_events - unblock disk event checking
1825 * @disk: disk to unblock events for
1827 * Undo disk_block_events(). When the block count reaches zero, it
1828 * starts events polling if configured.
1831 * Don't care. Safe to call from irq context.
1833 void disk_unblock_events(struct gendisk
*disk
)
1836 __disk_unblock_events(disk
, false);
1840 * disk_flush_events - schedule immediate event checking and flushing
1841 * @disk: disk to check and flush events for
1842 * @mask: events to flush
1844 * Schedule immediate event checking on @disk if not blocked. Events in
1845 * @mask are scheduled to be cleared from the driver. Note that this
1846 * doesn't clear the events from @disk->ev.
1849 * If @mask is non-zero must be called with bdev->bd_mutex held.
1851 void disk_flush_events(struct gendisk
*disk
, unsigned int mask
)
1853 struct disk_events
*ev
= disk
->ev
;
1858 spin_lock_irq(&ev
->lock
);
1859 ev
->clearing
|= mask
;
1861 mod_delayed_work(system_freezable_power_efficient_wq
,
1863 spin_unlock_irq(&ev
->lock
);
1867 * disk_clear_events - synchronously check, clear and return pending events
1868 * @disk: disk to fetch and clear events from
1869 * @mask: mask of events to be fetched and cleared
1871 * Disk events are synchronously checked and pending events in @mask
1872 * are cleared and returned. This ignores the block count.
1877 static unsigned int disk_clear_events(struct gendisk
*disk
, unsigned int mask
)
1879 struct disk_events
*ev
= disk
->ev
;
1880 unsigned int pending
;
1881 unsigned int clearing
= mask
;
1886 disk_block_events(disk
);
1889 * store the union of mask and ev->clearing on the stack so that the
1890 * race with disk_flush_events does not cause ambiguity (ev->clearing
1891 * can still be modified even if events are blocked).
1893 spin_lock_irq(&ev
->lock
);
1894 clearing
|= ev
->clearing
;
1896 spin_unlock_irq(&ev
->lock
);
1898 disk_check_events(ev
, &clearing
);
1900 * if ev->clearing is not 0, the disk_flush_events got called in the
1901 * middle of this function, so we want to run the workfn without delay.
1903 __disk_unblock_events(disk
, ev
->clearing
? true : false);
1905 /* then, fetch and clear pending events */
1906 spin_lock_irq(&ev
->lock
);
1907 pending
= ev
->pending
& mask
;
1908 ev
->pending
&= ~mask
;
1909 spin_unlock_irq(&ev
->lock
);
1910 WARN_ON_ONCE(clearing
& mask
);
1916 * bdev_check_media_change - check if a removable media has been changed
1917 * @bdev: block device to check
1919 * Check whether a removable media has been changed, and attempt to free all
1920 * dentries and inodes and invalidates all block device page cache entries in
1923 * Returns %true if the block device changed, or %false if not.
1925 bool bdev_check_media_change(struct block_device
*bdev
)
1927 unsigned int events
;
1929 events
= disk_clear_events(bdev
->bd_disk
, DISK_EVENT_MEDIA_CHANGE
|
1930 DISK_EVENT_EJECT_REQUEST
);
1931 if (!(events
& DISK_EVENT_MEDIA_CHANGE
))
1934 if (__invalidate_device(bdev
, true))
1935 pr_warn("VFS: busy inodes on changed media %s\n",
1936 bdev
->bd_disk
->disk_name
);
1937 set_bit(GD_NEED_PART_SCAN
, &bdev
->bd_disk
->state
);
1940 EXPORT_SYMBOL(bdev_check_media_change
);
1943 * Separate this part out so that a different pointer for clearing_ptr can be
1944 * passed in for disk_clear_events.
1946 static void disk_events_workfn(struct work_struct
*work
)
1948 struct delayed_work
*dwork
= to_delayed_work(work
);
1949 struct disk_events
*ev
= container_of(dwork
, struct disk_events
, dwork
);
1951 disk_check_events(ev
, &ev
->clearing
);
1954 static void disk_check_events(struct disk_events
*ev
,
1955 unsigned int *clearing_ptr
)
1957 struct gendisk
*disk
= ev
->disk
;
1958 char *envp
[ARRAY_SIZE(disk_uevents
) + 1] = { };
1959 unsigned int clearing
= *clearing_ptr
;
1960 unsigned int events
;
1962 int nr_events
= 0, i
;
1965 events
= disk
->fops
->check_events(disk
, clearing
);
1967 /* accumulate pending events and schedule next poll if necessary */
1968 spin_lock_irq(&ev
->lock
);
1970 events
&= ~ev
->pending
;
1971 ev
->pending
|= events
;
1972 *clearing_ptr
&= ~clearing
;
1974 intv
= disk_events_poll_jiffies(disk
);
1975 if (!ev
->block
&& intv
)
1976 queue_delayed_work(system_freezable_power_efficient_wq
,
1979 spin_unlock_irq(&ev
->lock
);
1982 * Tell userland about new events. Only the events listed in
1983 * @disk->events are reported, and only if DISK_EVENT_FLAG_UEVENT
1984 * is set. Otherwise, events are processed internally but never
1985 * get reported to userland.
1987 for (i
= 0; i
< ARRAY_SIZE(disk_uevents
); i
++)
1988 if ((events
& disk
->events
& (1 << i
)) &&
1989 (disk
->event_flags
& DISK_EVENT_FLAG_UEVENT
))
1990 envp
[nr_events
++] = disk_uevents
[i
];
1993 kobject_uevent_env(&disk_to_dev(disk
)->kobj
, KOBJ_CHANGE
, envp
);
1997 * A disk events enabled device has the following sysfs nodes under
1998 * its /sys/block/X/ directory.
2000 * events : list of all supported events
2001 * events_async : list of events which can be detected w/o polling
2002 * (always empty, only for backwards compatibility)
2003 * events_poll_msecs : polling interval, 0: disable, -1: system default
2005 static ssize_t
__disk_events_show(unsigned int events
, char *buf
)
2007 const char *delim
= "";
2011 for (i
= 0; i
< ARRAY_SIZE(disk_events_strs
); i
++)
2012 if (events
& (1 << i
)) {
2013 pos
+= sprintf(buf
+ pos
, "%s%s",
2014 delim
, disk_events_strs
[i
]);
2018 pos
+= sprintf(buf
+ pos
, "\n");
2022 static ssize_t
disk_events_show(struct device
*dev
,
2023 struct device_attribute
*attr
, char *buf
)
2025 struct gendisk
*disk
= dev_to_disk(dev
);
2027 if (!(disk
->event_flags
& DISK_EVENT_FLAG_UEVENT
))
2030 return __disk_events_show(disk
->events
, buf
);
2033 static ssize_t
disk_events_async_show(struct device
*dev
,
2034 struct device_attribute
*attr
, char *buf
)
2039 static ssize_t
disk_events_poll_msecs_show(struct device
*dev
,
2040 struct device_attribute
*attr
,
2043 struct gendisk
*disk
= dev_to_disk(dev
);
2046 return sprintf(buf
, "-1\n");
2048 return sprintf(buf
, "%ld\n", disk
->ev
->poll_msecs
);
2051 static ssize_t
disk_events_poll_msecs_store(struct device
*dev
,
2052 struct device_attribute
*attr
,
2053 const char *buf
, size_t count
)
2055 struct gendisk
*disk
= dev_to_disk(dev
);
2058 if (!count
|| !sscanf(buf
, "%ld", &intv
))
2061 if (intv
< 0 && intv
!= -1)
2067 disk_block_events(disk
);
2068 disk
->ev
->poll_msecs
= intv
;
2069 __disk_unblock_events(disk
, true);
2074 static const DEVICE_ATTR(events
, 0444, disk_events_show
, NULL
);
2075 static const DEVICE_ATTR(events_async
, 0444, disk_events_async_show
, NULL
);
2076 static const DEVICE_ATTR(events_poll_msecs
, 0644,
2077 disk_events_poll_msecs_show
,
2078 disk_events_poll_msecs_store
);
2080 static const struct attribute
*disk_events_attrs
[] = {
2081 &dev_attr_events
.attr
,
2082 &dev_attr_events_async
.attr
,
2083 &dev_attr_events_poll_msecs
.attr
,
2088 * The default polling interval can be specified by the kernel
2089 * parameter block.events_dfl_poll_msecs which defaults to 0
2090 * (disable). This can also be modified runtime by writing to
2091 * /sys/module/block/parameters/events_dfl_poll_msecs.
2093 static int disk_events_set_dfl_poll_msecs(const char *val
,
2094 const struct kernel_param
*kp
)
2096 struct disk_events
*ev
;
2099 ret
= param_set_ulong(val
, kp
);
2103 mutex_lock(&disk_events_mutex
);
2105 list_for_each_entry(ev
, &disk_events
, node
)
2106 disk_flush_events(ev
->disk
, 0);
2108 mutex_unlock(&disk_events_mutex
);
2113 static const struct kernel_param_ops disk_events_dfl_poll_msecs_param_ops
= {
2114 .set
= disk_events_set_dfl_poll_msecs
,
2115 .get
= param_get_ulong
,
2118 #undef MODULE_PARAM_PREFIX
2119 #define MODULE_PARAM_PREFIX "block."
2121 module_param_cb(events_dfl_poll_msecs
, &disk_events_dfl_poll_msecs_param_ops
,
2122 &disk_events_dfl_poll_msecs
, 0644);
2125 * disk_{alloc|add|del|release}_events - initialize and destroy disk_events.
2127 static void disk_alloc_events(struct gendisk
*disk
)
2129 struct disk_events
*ev
;
2131 if (!disk
->fops
->check_events
|| !disk
->events
)
2134 ev
= kzalloc(sizeof(*ev
), GFP_KERNEL
);
2136 pr_warn("%s: failed to initialize events\n", disk
->disk_name
);
2140 INIT_LIST_HEAD(&ev
->node
);
2142 spin_lock_init(&ev
->lock
);
2143 mutex_init(&ev
->block_mutex
);
2145 ev
->poll_msecs
= -1;
2146 INIT_DELAYED_WORK(&ev
->dwork
, disk_events_workfn
);
2151 static void disk_add_events(struct gendisk
*disk
)
2153 /* FIXME: error handling */
2154 if (sysfs_create_files(&disk_to_dev(disk
)->kobj
, disk_events_attrs
) < 0)
2155 pr_warn("%s: failed to create sysfs files for events\n",
2161 mutex_lock(&disk_events_mutex
);
2162 list_add_tail(&disk
->ev
->node
, &disk_events
);
2163 mutex_unlock(&disk_events_mutex
);
2166 * Block count is initialized to 1 and the following initial
2167 * unblock kicks it into action.
2169 __disk_unblock_events(disk
, true);
2172 static void disk_del_events(struct gendisk
*disk
)
2175 disk_block_events(disk
);
2177 mutex_lock(&disk_events_mutex
);
2178 list_del_init(&disk
->ev
->node
);
2179 mutex_unlock(&disk_events_mutex
);
2182 sysfs_remove_files(&disk_to_dev(disk
)->kobj
, disk_events_attrs
);
2185 static void disk_release_events(struct gendisk
*disk
)
2187 /* the block count should be 1 from disk_del_events() */
2188 WARN_ON_ONCE(disk
->ev
&& disk
->ev
->block
!= 1);