5 #include <linux/module.h>
7 #include <linux/genhd.h>
8 #include <linux/kdev_t.h>
9 #include <linux/kernel.h>
10 #include <linux/blkdev.h>
11 #include <linux/backing-dev.h>
12 #include <linux/init.h>
13 #include <linux/spinlock.h>
14 #include <linux/proc_fs.h>
15 #include <linux/seq_file.h>
16 #include <linux/slab.h>
17 #include <linux/kmod.h>
18 #include <linux/kobj_map.h>
19 #include <linux/mutex.h>
20 #include <linux/idr.h>
21 #include <linux/log2.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/badblocks.h>
27 static DEFINE_MUTEX(block_class_lock
);
28 struct kobject
*block_depr
;
30 /* for extended dynamic devt allocation, currently only one major is used */
31 #define NR_EXT_DEVT (1 << MINORBITS)
33 /* For extended devt allocation. ext_devt_lock prevents look up
34 * results from going away underneath its user.
36 static DEFINE_SPINLOCK(ext_devt_lock
);
37 static DEFINE_IDR(ext_devt_idr
);
39 static const struct device_type disk_type
;
41 static void disk_check_events(struct disk_events
*ev
,
42 unsigned int *clearing_ptr
);
43 static void disk_alloc_events(struct gendisk
*disk
);
44 static void disk_add_events(struct gendisk
*disk
);
45 static void disk_del_events(struct gendisk
*disk
);
46 static void disk_release_events(struct gendisk
*disk
);
48 void part_inc_in_flight(struct request_queue
*q
, struct hd_struct
*part
, int rw
)
53 atomic_inc(&part
->in_flight
[rw
]);
55 atomic_inc(&part_to_disk(part
)->part0
.in_flight
[rw
]);
58 void part_dec_in_flight(struct request_queue
*q
, struct hd_struct
*part
, int rw
)
63 atomic_dec(&part
->in_flight
[rw
]);
65 atomic_dec(&part_to_disk(part
)->part0
.in_flight
[rw
]);
68 void part_in_flight(struct request_queue
*q
, struct hd_struct
*part
,
69 unsigned int inflight
[2])
72 blk_mq_in_flight(q
, part
, inflight
);
76 inflight
[0] = atomic_read(&part
->in_flight
[0]) +
77 atomic_read(&part
->in_flight
[1]);
79 part
= &part_to_disk(part
)->part0
;
80 inflight
[1] = atomic_read(&part
->in_flight
[0]) +
81 atomic_read(&part
->in_flight
[1]);
85 void part_in_flight_rw(struct request_queue
*q
, struct hd_struct
*part
,
86 unsigned int inflight
[2])
89 blk_mq_in_flight_rw(q
, part
, inflight
);
93 inflight
[0] = atomic_read(&part
->in_flight
[0]);
94 inflight
[1] = atomic_read(&part
->in_flight
[1]);
97 struct hd_struct
*__disk_get_part(struct gendisk
*disk
, int partno
)
99 struct disk_part_tbl
*ptbl
= rcu_dereference(disk
->part_tbl
);
101 if (unlikely(partno
< 0 || partno
>= ptbl
->len
))
103 return rcu_dereference(ptbl
->part
[partno
]);
107 * disk_get_part - get partition
108 * @disk: disk to look partition from
109 * @partno: partition number
111 * Look for partition @partno from @disk. If found, increment
112 * reference count and return it.
118 * Pointer to the found partition on success, NULL if not found.
120 struct hd_struct
*disk_get_part(struct gendisk
*disk
, int partno
)
122 struct hd_struct
*part
;
125 part
= __disk_get_part(disk
, partno
);
127 get_device(part_to_dev(part
));
132 EXPORT_SYMBOL_GPL(disk_get_part
);
135 * disk_part_iter_init - initialize partition iterator
136 * @piter: iterator to initialize
137 * @disk: disk to iterate over
138 * @flags: DISK_PITER_* flags
140 * Initialize @piter so that it iterates over partitions of @disk.
145 void disk_part_iter_init(struct disk_part_iter
*piter
, struct gendisk
*disk
,
148 struct disk_part_tbl
*ptbl
;
151 ptbl
= rcu_dereference(disk
->part_tbl
);
156 if (flags
& DISK_PITER_REVERSE
)
157 piter
->idx
= ptbl
->len
- 1;
158 else if (flags
& (DISK_PITER_INCL_PART0
| DISK_PITER_INCL_EMPTY_PART0
))
163 piter
->flags
= flags
;
167 EXPORT_SYMBOL_GPL(disk_part_iter_init
);
170 * disk_part_iter_next - proceed iterator to the next partition and return it
171 * @piter: iterator of interest
173 * Proceed @piter to the next partition and return it.
178 struct hd_struct
*disk_part_iter_next(struct disk_part_iter
*piter
)
180 struct disk_part_tbl
*ptbl
;
183 /* put the last partition */
184 disk_put_part(piter
->part
);
189 ptbl
= rcu_dereference(piter
->disk
->part_tbl
);
191 /* determine iteration parameters */
192 if (piter
->flags
& DISK_PITER_REVERSE
) {
194 if (piter
->flags
& (DISK_PITER_INCL_PART0
|
195 DISK_PITER_INCL_EMPTY_PART0
))
204 /* iterate to the next partition */
205 for (; piter
->idx
!= end
; piter
->idx
+= inc
) {
206 struct hd_struct
*part
;
208 part
= rcu_dereference(ptbl
->part
[piter
->idx
]);
211 if (!part_nr_sects_read(part
) &&
212 !(piter
->flags
& DISK_PITER_INCL_EMPTY
) &&
213 !(piter
->flags
& DISK_PITER_INCL_EMPTY_PART0
&&
217 get_device(part_to_dev(part
));
227 EXPORT_SYMBOL_GPL(disk_part_iter_next
);
230 * disk_part_iter_exit - finish up partition iteration
231 * @piter: iter of interest
233 * Called when iteration is over. Cleans up @piter.
238 void disk_part_iter_exit(struct disk_part_iter
*piter
)
240 disk_put_part(piter
->part
);
243 EXPORT_SYMBOL_GPL(disk_part_iter_exit
);
245 static inline int sector_in_part(struct hd_struct
*part
, sector_t sector
)
247 return part
->start_sect
<= sector
&&
248 sector
< part
->start_sect
+ part_nr_sects_read(part
);
252 * disk_map_sector_rcu - map sector to partition
253 * @disk: gendisk of interest
254 * @sector: sector to map
256 * Find out which partition @sector maps to on @disk. This is
257 * primarily used for stats accounting.
260 * RCU read locked. The returned partition pointer is valid only
261 * while preemption is disabled.
264 * Found partition on success, part0 is returned if no partition matches
266 struct hd_struct
*disk_map_sector_rcu(struct gendisk
*disk
, sector_t sector
)
268 struct disk_part_tbl
*ptbl
;
269 struct hd_struct
*part
;
272 ptbl
= rcu_dereference(disk
->part_tbl
);
274 part
= rcu_dereference(ptbl
->last_lookup
);
275 if (part
&& sector_in_part(part
, sector
))
278 for (i
= 1; i
< ptbl
->len
; i
++) {
279 part
= rcu_dereference(ptbl
->part
[i
]);
281 if (part
&& sector_in_part(part
, sector
)) {
282 rcu_assign_pointer(ptbl
->last_lookup
, part
);
288 EXPORT_SYMBOL_GPL(disk_map_sector_rcu
);
291 * Can be deleted altogether. Later.
294 #define BLKDEV_MAJOR_HASH_SIZE 255
295 static struct blk_major_name
{
296 struct blk_major_name
*next
;
299 } *major_names
[BLKDEV_MAJOR_HASH_SIZE
];
301 /* index in the above - for now: assume no multimajor ranges */
302 static inline int major_to_index(unsigned major
)
304 return major
% BLKDEV_MAJOR_HASH_SIZE
;
307 #ifdef CONFIG_PROC_FS
308 void blkdev_show(struct seq_file
*seqf
, off_t offset
)
310 struct blk_major_name
*dp
;
312 mutex_lock(&block_class_lock
);
313 for (dp
= major_names
[major_to_index(offset
)]; dp
; dp
= dp
->next
)
314 if (dp
->major
== offset
)
315 seq_printf(seqf
, "%3d %s\n", dp
->major
, dp
->name
);
316 mutex_unlock(&block_class_lock
);
318 #endif /* CONFIG_PROC_FS */
321 * register_blkdev - register a new block device
323 * @major: the requested major device number [1..255]. If @major = 0, try to
324 * allocate any unused major number.
325 * @name: the name of the new block device as a zero terminated string
327 * The @name must be unique within the system.
329 * The return value depends on the @major input parameter:
331 * - if a major device number was requested in range [1..255] then the
332 * function returns zero on success, or a negative error code
333 * - if any unused major number was requested with @major = 0 parameter
334 * then the return value is the allocated major number in range
335 * [1..255] or a negative error code otherwise
337 int register_blkdev(unsigned int major
, const char *name
)
339 struct blk_major_name
**n
, *p
;
342 mutex_lock(&block_class_lock
);
346 for (index
= ARRAY_SIZE(major_names
)-1; index
> 0; index
--) {
347 if (major_names
[index
] == NULL
)
352 printk("register_blkdev: failed to get major for %s\n",
361 if (major
>= BLKDEV_MAJOR_MAX
) {
362 pr_err("register_blkdev: major requested (%d) is greater than the maximum (%d) for %s\n",
363 major
, BLKDEV_MAJOR_MAX
, name
);
369 p
= kmalloc(sizeof(struct blk_major_name
), GFP_KERNEL
);
376 strlcpy(p
->name
, name
, sizeof(p
->name
));
378 index
= major_to_index(major
);
380 for (n
= &major_names
[index
]; *n
; n
= &(*n
)->next
) {
381 if ((*n
)->major
== major
)
390 printk("register_blkdev: cannot get major %d for %s\n",
395 mutex_unlock(&block_class_lock
);
399 EXPORT_SYMBOL(register_blkdev
);
401 void unregister_blkdev(unsigned int major
, const char *name
)
403 struct blk_major_name
**n
;
404 struct blk_major_name
*p
= NULL
;
405 int index
= major_to_index(major
);
407 mutex_lock(&block_class_lock
);
408 for (n
= &major_names
[index
]; *n
; n
= &(*n
)->next
)
409 if ((*n
)->major
== major
)
411 if (!*n
|| strcmp((*n
)->name
, name
)) {
417 mutex_unlock(&block_class_lock
);
421 EXPORT_SYMBOL(unregister_blkdev
);
423 static struct kobj_map
*bdev_map
;
426 * blk_mangle_minor - scatter minor numbers apart
427 * @minor: minor number to mangle
429 * Scatter consecutively allocated @minor number apart if MANGLE_DEVT
430 * is enabled. Mangling twice gives the original value.
438 static int blk_mangle_minor(int minor
)
440 #ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT
443 for (i
= 0; i
< MINORBITS
/ 2; i
++) {
444 int low
= minor
& (1 << i
);
445 int high
= minor
& (1 << (MINORBITS
- 1 - i
));
446 int distance
= MINORBITS
- 1 - 2 * i
;
448 minor
^= low
| high
; /* clear both bits */
449 low
<<= distance
; /* swap the positions */
451 minor
|= low
| high
; /* and set */
458 * blk_alloc_devt - allocate a dev_t for a partition
459 * @part: partition to allocate dev_t for
460 * @devt: out parameter for resulting dev_t
462 * Allocate a dev_t for block device.
465 * 0 on success, allocated dev_t is returned in *@devt. -errno on
471 int blk_alloc_devt(struct hd_struct
*part
, dev_t
*devt
)
473 struct gendisk
*disk
= part_to_disk(part
);
476 /* in consecutive minor range? */
477 if (part
->partno
< disk
->minors
) {
478 *devt
= MKDEV(disk
->major
, disk
->first_minor
+ part
->partno
);
482 /* allocate ext devt */
483 idr_preload(GFP_KERNEL
);
485 spin_lock_bh(&ext_devt_lock
);
486 idx
= idr_alloc(&ext_devt_idr
, part
, 0, NR_EXT_DEVT
, GFP_NOWAIT
);
487 spin_unlock_bh(&ext_devt_lock
);
491 return idx
== -ENOSPC
? -EBUSY
: idx
;
493 *devt
= MKDEV(BLOCK_EXT_MAJOR
, blk_mangle_minor(idx
));
498 * blk_free_devt - free a dev_t
499 * @devt: dev_t to free
501 * Free @devt which was allocated using blk_alloc_devt().
506 void blk_free_devt(dev_t devt
)
508 if (devt
== MKDEV(0, 0))
511 if (MAJOR(devt
) == BLOCK_EXT_MAJOR
) {
512 spin_lock_bh(&ext_devt_lock
);
513 idr_remove(&ext_devt_idr
, blk_mangle_minor(MINOR(devt
)));
514 spin_unlock_bh(&ext_devt_lock
);
518 static char *bdevt_str(dev_t devt
, char *buf
)
520 if (MAJOR(devt
) <= 0xff && MINOR(devt
) <= 0xff) {
521 char tbuf
[BDEVT_SIZE
];
522 snprintf(tbuf
, BDEVT_SIZE
, "%02x%02x", MAJOR(devt
), MINOR(devt
));
523 snprintf(buf
, BDEVT_SIZE
, "%-9s", tbuf
);
525 snprintf(buf
, BDEVT_SIZE
, "%03x:%05x", MAJOR(devt
), MINOR(devt
));
531 * Register device numbers dev..(dev+range-1)
532 * range must be nonzero
533 * The hash chain is sorted on range, so that subranges can override.
535 void blk_register_region(dev_t devt
, unsigned long range
, struct module
*module
,
536 struct kobject
*(*probe
)(dev_t
, int *, void *),
537 int (*lock
)(dev_t
, void *), void *data
)
539 kobj_map(bdev_map
, devt
, range
, module
, probe
, lock
, data
);
542 EXPORT_SYMBOL(blk_register_region
);
544 void blk_unregister_region(dev_t devt
, unsigned long range
)
546 kobj_unmap(bdev_map
, devt
, range
);
549 EXPORT_SYMBOL(blk_unregister_region
);
551 static struct kobject
*exact_match(dev_t devt
, int *partno
, void *data
)
553 struct gendisk
*p
= data
;
555 return &disk_to_dev(p
)->kobj
;
558 static int exact_lock(dev_t devt
, void *data
)
560 struct gendisk
*p
= data
;
567 static void register_disk(struct device
*parent
, struct gendisk
*disk
)
569 struct device
*ddev
= disk_to_dev(disk
);
570 struct block_device
*bdev
;
571 struct disk_part_iter piter
;
572 struct hd_struct
*part
;
575 ddev
->parent
= parent
;
577 dev_set_name(ddev
, "%s", disk
->disk_name
);
579 /* delay uevents, until we scanned partition table */
580 dev_set_uevent_suppress(ddev
, 1);
582 if (device_add(ddev
))
584 if (!sysfs_deprecated
) {
585 err
= sysfs_create_link(block_depr
, &ddev
->kobj
,
586 kobject_name(&ddev
->kobj
));
594 * avoid probable deadlock caused by allocating memory with
595 * GFP_KERNEL in runtime_resume callback of its all ancestor
598 pm_runtime_set_memalloc_noio(ddev
, true);
600 disk
->part0
.holder_dir
= kobject_create_and_add("holders", &ddev
->kobj
);
601 disk
->slave_dir
= kobject_create_and_add("slaves", &ddev
->kobj
);
603 if (disk
->flags
& GENHD_FL_HIDDEN
) {
604 dev_set_uevent_suppress(ddev
, 0);
608 /* No minors to use for partitions */
609 if (!disk_part_scan_enabled(disk
))
612 /* No such device (e.g., media were just removed) */
613 if (!get_capacity(disk
))
616 bdev
= bdget_disk(disk
, 0);
620 bdev
->bd_invalidated
= 1;
621 err
= blkdev_get(bdev
, FMODE_READ
, NULL
);
624 blkdev_put(bdev
, FMODE_READ
);
627 /* announce disk after possible partitions are created */
628 dev_set_uevent_suppress(ddev
, 0);
629 kobject_uevent(&ddev
->kobj
, KOBJ_ADD
);
631 /* announce possible partitions */
632 disk_part_iter_init(&piter
, disk
, 0);
633 while ((part
= disk_part_iter_next(&piter
)))
634 kobject_uevent(&part_to_dev(part
)->kobj
, KOBJ_ADD
);
635 disk_part_iter_exit(&piter
);
637 err
= sysfs_create_link(&ddev
->kobj
,
638 &disk
->queue
->backing_dev_info
->dev
->kobj
,
644 * device_add_disk - add partitioning information to kernel list
645 * @parent: parent device for the disk
646 * @disk: per-device partitioning information
648 * This function registers the partitioning information in @disk
651 * FIXME: error handling
653 void device_add_disk(struct device
*parent
, struct gendisk
*disk
)
658 /* minors == 0 indicates to use ext devt from part0 and should
659 * be accompanied with EXT_DEVT flag. Make sure all
660 * parameters make sense.
662 WARN_ON(disk
->minors
&& !(disk
->major
|| disk
->first_minor
));
663 WARN_ON(!disk
->minors
&&
664 !(disk
->flags
& (GENHD_FL_EXT_DEVT
| GENHD_FL_HIDDEN
)));
666 disk
->flags
|= GENHD_FL_UP
;
668 retval
= blk_alloc_devt(&disk
->part0
, &devt
);
673 disk
->major
= MAJOR(devt
);
674 disk
->first_minor
= MINOR(devt
);
676 disk_alloc_events(disk
);
678 if (disk
->flags
& GENHD_FL_HIDDEN
) {
680 * Don't let hidden disks show up in /proc/partitions,
681 * and don't bother scanning for partitions either.
683 disk
->flags
|= GENHD_FL_SUPPRESS_PARTITION_INFO
;
684 disk
->flags
|= GENHD_FL_NO_PART_SCAN
;
688 /* Register BDI before referencing it from bdev */
689 disk_to_dev(disk
)->devt
= devt
;
690 ret
= bdi_register_owner(disk
->queue
->backing_dev_info
,
693 blk_register_region(disk_devt(disk
), disk
->minors
, NULL
,
694 exact_match
, exact_lock
, disk
);
696 register_disk(parent
, disk
);
697 blk_register_queue(disk
);
700 * Take an extra ref on queue which will be put on disk_release()
701 * so that it sticks around as long as @disk is there.
703 WARN_ON_ONCE(!blk_get_queue(disk
->queue
));
705 disk_add_events(disk
);
706 blk_integrity_add(disk
);
708 EXPORT_SYMBOL(device_add_disk
);
710 void del_gendisk(struct gendisk
*disk
)
712 struct disk_part_iter piter
;
713 struct hd_struct
*part
;
715 blk_integrity_del(disk
);
716 disk_del_events(disk
);
718 /* invalidate stuff */
719 disk_part_iter_init(&piter
, disk
,
720 DISK_PITER_INCL_EMPTY
| DISK_PITER_REVERSE
);
721 while ((part
= disk_part_iter_next(&piter
))) {
722 invalidate_partition(disk
, part
->partno
);
723 bdev_unhash_inode(part_devt(part
));
724 delete_partition(disk
, part
->partno
);
726 disk_part_iter_exit(&piter
);
728 invalidate_partition(disk
, 0);
729 bdev_unhash_inode(disk_devt(disk
));
730 set_capacity(disk
, 0);
731 disk
->flags
&= ~GENHD_FL_UP
;
733 if (!(disk
->flags
& GENHD_FL_HIDDEN
))
734 sysfs_remove_link(&disk_to_dev(disk
)->kobj
, "bdi");
737 * Unregister bdi before releasing device numbers (as they can
738 * get reused and we'd get clashes in sysfs).
740 bdi_unregister(disk
->queue
->backing_dev_info
);
741 blk_unregister_queue(disk
);
746 if (!(disk
->flags
& GENHD_FL_HIDDEN
))
747 blk_unregister_region(disk_devt(disk
), disk
->minors
);
749 kobject_put(disk
->part0
.holder_dir
);
750 kobject_put(disk
->slave_dir
);
752 part_stat_set_all(&disk
->part0
, 0);
753 disk
->part0
.stamp
= 0;
754 if (!sysfs_deprecated
)
755 sysfs_remove_link(block_depr
, dev_name(disk_to_dev(disk
)));
756 pm_runtime_set_memalloc_noio(disk_to_dev(disk
), false);
757 device_del(disk_to_dev(disk
));
759 EXPORT_SYMBOL(del_gendisk
);
761 /* sysfs access to bad-blocks list. */
762 static ssize_t
disk_badblocks_show(struct device
*dev
,
763 struct device_attribute
*attr
,
766 struct gendisk
*disk
= dev_to_disk(dev
);
769 return sprintf(page
, "\n");
771 return badblocks_show(disk
->bb
, page
, 0);
774 static ssize_t
disk_badblocks_store(struct device
*dev
,
775 struct device_attribute
*attr
,
776 const char *page
, size_t len
)
778 struct gendisk
*disk
= dev_to_disk(dev
);
783 return badblocks_store(disk
->bb
, page
, len
, 0);
787 * get_gendisk - get partitioning information for a given device
788 * @devt: device to get partitioning information for
789 * @partno: returned partition index
791 * This function gets the structure containing partitioning
792 * information for the given device @devt.
794 struct gendisk
*get_gendisk(dev_t devt
, int *partno
)
796 struct gendisk
*disk
= NULL
;
798 if (MAJOR(devt
) != BLOCK_EXT_MAJOR
) {
799 struct kobject
*kobj
;
801 kobj
= kobj_lookup(bdev_map
, devt
, partno
);
803 disk
= dev_to_disk(kobj_to_dev(kobj
));
805 struct hd_struct
*part
;
807 spin_lock_bh(&ext_devt_lock
);
808 part
= idr_find(&ext_devt_idr
, blk_mangle_minor(MINOR(devt
)));
809 if (part
&& get_disk(part_to_disk(part
))) {
810 *partno
= part
->partno
;
811 disk
= part_to_disk(part
);
813 spin_unlock_bh(&ext_devt_lock
);
816 if (disk
&& unlikely(disk
->flags
& GENHD_FL_HIDDEN
)) {
822 EXPORT_SYMBOL(get_gendisk
);
825 * bdget_disk - do bdget() by gendisk and partition number
826 * @disk: gendisk of interest
827 * @partno: partition number
829 * Find partition @partno from @disk, do bdget() on it.
835 * Resulting block_device on success, NULL on failure.
837 struct block_device
*bdget_disk(struct gendisk
*disk
, int partno
)
839 struct hd_struct
*part
;
840 struct block_device
*bdev
= NULL
;
842 part
= disk_get_part(disk
, partno
);
844 bdev
= bdget(part_devt(part
));
849 EXPORT_SYMBOL(bdget_disk
);
852 * print a full list of all partitions - intended for places where the root
853 * filesystem can't be mounted and thus to give the victim some idea of what
856 void __init
printk_all_partitions(void)
858 struct class_dev_iter iter
;
861 class_dev_iter_init(&iter
, &block_class
, NULL
, &disk_type
);
862 while ((dev
= class_dev_iter_next(&iter
))) {
863 struct gendisk
*disk
= dev_to_disk(dev
);
864 struct disk_part_iter piter
;
865 struct hd_struct
*part
;
866 char name_buf
[BDEVNAME_SIZE
];
867 char devt_buf
[BDEVT_SIZE
];
870 * Don't show empty devices or things that have been
873 if (get_capacity(disk
) == 0 ||
874 (disk
->flags
& GENHD_FL_SUPPRESS_PARTITION_INFO
))
878 * Note, unlike /proc/partitions, I am showing the
879 * numbers in hex - the same format as the root=
882 disk_part_iter_init(&piter
, disk
, DISK_PITER_INCL_PART0
);
883 while ((part
= disk_part_iter_next(&piter
))) {
884 bool is_part0
= part
== &disk
->part0
;
886 printk("%s%s %10llu %s %s", is_part0
? "" : " ",
887 bdevt_str(part_devt(part
), devt_buf
),
888 (unsigned long long)part_nr_sects_read(part
) >> 1
889 , disk_name(disk
, part
->partno
, name_buf
),
890 part
->info
? part
->info
->uuid
: "");
892 if (dev
->parent
&& dev
->parent
->driver
)
893 printk(" driver: %s\n",
894 dev
->parent
->driver
->name
);
896 printk(" (driver?)\n");
900 disk_part_iter_exit(&piter
);
902 class_dev_iter_exit(&iter
);
905 #ifdef CONFIG_PROC_FS
907 static void *disk_seqf_start(struct seq_file
*seqf
, loff_t
*pos
)
910 struct class_dev_iter
*iter
;
913 iter
= kmalloc(sizeof(*iter
), GFP_KERNEL
);
915 return ERR_PTR(-ENOMEM
);
917 seqf
->private = iter
;
918 class_dev_iter_init(iter
, &block_class
, NULL
, &disk_type
);
920 dev
= class_dev_iter_next(iter
);
925 return dev_to_disk(dev
);
928 static void *disk_seqf_next(struct seq_file
*seqf
, void *v
, loff_t
*pos
)
933 dev
= class_dev_iter_next(seqf
->private);
935 return dev_to_disk(dev
);
940 static void disk_seqf_stop(struct seq_file
*seqf
, void *v
)
942 struct class_dev_iter
*iter
= seqf
->private;
944 /* stop is called even after start failed :-( */
946 class_dev_iter_exit(iter
);
948 seqf
->private = NULL
;
952 static void *show_partition_start(struct seq_file
*seqf
, loff_t
*pos
)
956 p
= disk_seqf_start(seqf
, pos
);
957 if (!IS_ERR_OR_NULL(p
) && !*pos
)
958 seq_puts(seqf
, "major minor #blocks name\n\n");
962 static int show_partition(struct seq_file
*seqf
, void *v
)
964 struct gendisk
*sgp
= v
;
965 struct disk_part_iter piter
;
966 struct hd_struct
*part
;
967 char buf
[BDEVNAME_SIZE
];
969 /* Don't show non-partitionable removeable devices or empty devices */
970 if (!get_capacity(sgp
) || (!disk_max_parts(sgp
) &&
971 (sgp
->flags
& GENHD_FL_REMOVABLE
)))
973 if (sgp
->flags
& GENHD_FL_SUPPRESS_PARTITION_INFO
)
976 /* show the full disk and all non-0 size partitions of it */
977 disk_part_iter_init(&piter
, sgp
, DISK_PITER_INCL_PART0
);
978 while ((part
= disk_part_iter_next(&piter
)))
979 seq_printf(seqf
, "%4d %7d %10llu %s\n",
980 MAJOR(part_devt(part
)), MINOR(part_devt(part
)),
981 (unsigned long long)part_nr_sects_read(part
) >> 1,
982 disk_name(sgp
, part
->partno
, buf
));
983 disk_part_iter_exit(&piter
);
988 static const struct seq_operations partitions_op
= {
989 .start
= show_partition_start
,
990 .next
= disk_seqf_next
,
991 .stop
= disk_seqf_stop
,
992 .show
= show_partition
995 static int partitions_open(struct inode
*inode
, struct file
*file
)
997 return seq_open(file
, &partitions_op
);
1000 static const struct file_operations proc_partitions_operations
= {
1001 .open
= partitions_open
,
1003 .llseek
= seq_lseek
,
1004 .release
= seq_release
,
1009 static struct kobject
*base_probe(dev_t devt
, int *partno
, void *data
)
1011 if (request_module("block-major-%d-%d", MAJOR(devt
), MINOR(devt
)) > 0)
1012 /* Make old-style 2.4 aliases work */
1013 request_module("block-major-%d", MAJOR(devt
));
1017 static int __init
genhd_device_init(void)
1021 block_class
.dev_kobj
= sysfs_dev_block_kobj
;
1022 error
= class_register(&block_class
);
1023 if (unlikely(error
))
1025 bdev_map
= kobj_map_init(base_probe
, &block_class_lock
);
1028 register_blkdev(BLOCK_EXT_MAJOR
, "blkext");
1030 /* create top-level block dir */
1031 if (!sysfs_deprecated
)
1032 block_depr
= kobject_create_and_add("block", NULL
);
1036 subsys_initcall(genhd_device_init
);
1038 static ssize_t
disk_range_show(struct device
*dev
,
1039 struct device_attribute
*attr
, char *buf
)
1041 struct gendisk
*disk
= dev_to_disk(dev
);
1043 return sprintf(buf
, "%d\n", disk
->minors
);
1046 static ssize_t
disk_ext_range_show(struct device
*dev
,
1047 struct device_attribute
*attr
, char *buf
)
1049 struct gendisk
*disk
= dev_to_disk(dev
);
1051 return sprintf(buf
, "%d\n", disk_max_parts(disk
));
1054 static ssize_t
disk_removable_show(struct device
*dev
,
1055 struct device_attribute
*attr
, char *buf
)
1057 struct gendisk
*disk
= dev_to_disk(dev
);
1059 return sprintf(buf
, "%d\n",
1060 (disk
->flags
& GENHD_FL_REMOVABLE
? 1 : 0));
1063 static ssize_t
disk_hidden_show(struct device
*dev
,
1064 struct device_attribute
*attr
, char *buf
)
1066 struct gendisk
*disk
= dev_to_disk(dev
);
1068 return sprintf(buf
, "%d\n",
1069 (disk
->flags
& GENHD_FL_HIDDEN
? 1 : 0));
1072 static ssize_t
disk_ro_show(struct device
*dev
,
1073 struct device_attribute
*attr
, char *buf
)
1075 struct gendisk
*disk
= dev_to_disk(dev
);
1077 return sprintf(buf
, "%d\n", get_disk_ro(disk
) ? 1 : 0);
1080 static ssize_t
disk_capability_show(struct device
*dev
,
1081 struct device_attribute
*attr
, char *buf
)
1083 struct gendisk
*disk
= dev_to_disk(dev
);
1085 return sprintf(buf
, "%x\n", disk
->flags
);
1088 static ssize_t
disk_alignment_offset_show(struct device
*dev
,
1089 struct device_attribute
*attr
,
1092 struct gendisk
*disk
= dev_to_disk(dev
);
1094 return sprintf(buf
, "%d\n", queue_alignment_offset(disk
->queue
));
1097 static ssize_t
disk_discard_alignment_show(struct device
*dev
,
1098 struct device_attribute
*attr
,
1101 struct gendisk
*disk
= dev_to_disk(dev
);
1103 return sprintf(buf
, "%d\n", queue_discard_alignment(disk
->queue
));
1106 static DEVICE_ATTR(range
, S_IRUGO
, disk_range_show
, NULL
);
1107 static DEVICE_ATTR(ext_range
, S_IRUGO
, disk_ext_range_show
, NULL
);
1108 static DEVICE_ATTR(removable
, S_IRUGO
, disk_removable_show
, NULL
);
1109 static DEVICE_ATTR(hidden
, S_IRUGO
, disk_hidden_show
, NULL
);
1110 static DEVICE_ATTR(ro
, S_IRUGO
, disk_ro_show
, NULL
);
1111 static DEVICE_ATTR(size
, S_IRUGO
, part_size_show
, NULL
);
1112 static DEVICE_ATTR(alignment_offset
, S_IRUGO
, disk_alignment_offset_show
, NULL
);
1113 static DEVICE_ATTR(discard_alignment
, S_IRUGO
, disk_discard_alignment_show
,
1115 static DEVICE_ATTR(capability
, S_IRUGO
, disk_capability_show
, NULL
);
1116 static DEVICE_ATTR(stat
, S_IRUGO
, part_stat_show
, NULL
);
1117 static DEVICE_ATTR(inflight
, S_IRUGO
, part_inflight_show
, NULL
);
1118 static DEVICE_ATTR(badblocks
, S_IRUGO
| S_IWUSR
, disk_badblocks_show
,
1119 disk_badblocks_store
);
1120 #ifdef CONFIG_FAIL_MAKE_REQUEST
1121 static struct device_attribute dev_attr_fail
=
1122 __ATTR(make
-it
-fail
, S_IRUGO
|S_IWUSR
, part_fail_show
, part_fail_store
);
1124 #ifdef CONFIG_FAIL_IO_TIMEOUT
1125 static struct device_attribute dev_attr_fail_timeout
=
1126 __ATTR(io
-timeout
-fail
, S_IRUGO
|S_IWUSR
, part_timeout_show
,
1127 part_timeout_store
);
1130 static struct attribute
*disk_attrs
[] = {
1131 &dev_attr_range
.attr
,
1132 &dev_attr_ext_range
.attr
,
1133 &dev_attr_removable
.attr
,
1134 &dev_attr_hidden
.attr
,
1136 &dev_attr_size
.attr
,
1137 &dev_attr_alignment_offset
.attr
,
1138 &dev_attr_discard_alignment
.attr
,
1139 &dev_attr_capability
.attr
,
1140 &dev_attr_stat
.attr
,
1141 &dev_attr_inflight
.attr
,
1142 &dev_attr_badblocks
.attr
,
1143 #ifdef CONFIG_FAIL_MAKE_REQUEST
1144 &dev_attr_fail
.attr
,
1146 #ifdef CONFIG_FAIL_IO_TIMEOUT
1147 &dev_attr_fail_timeout
.attr
,
1152 static umode_t
disk_visible(struct kobject
*kobj
, struct attribute
*a
, int n
)
1154 struct device
*dev
= container_of(kobj
, typeof(*dev
), kobj
);
1155 struct gendisk
*disk
= dev_to_disk(dev
);
1157 if (a
== &dev_attr_badblocks
.attr
&& !disk
->bb
)
1162 static struct attribute_group disk_attr_group
= {
1163 .attrs
= disk_attrs
,
1164 .is_visible
= disk_visible
,
1167 static const struct attribute_group
*disk_attr_groups
[] = {
1173 * disk_replace_part_tbl - replace disk->part_tbl in RCU-safe way
1174 * @disk: disk to replace part_tbl for
1175 * @new_ptbl: new part_tbl to install
1177 * Replace disk->part_tbl with @new_ptbl in RCU-safe way. The
1178 * original ptbl is freed using RCU callback.
1181 * Matching bd_mutex locked or the caller is the only user of @disk.
1183 static void disk_replace_part_tbl(struct gendisk
*disk
,
1184 struct disk_part_tbl
*new_ptbl
)
1186 struct disk_part_tbl
*old_ptbl
=
1187 rcu_dereference_protected(disk
->part_tbl
, 1);
1189 rcu_assign_pointer(disk
->part_tbl
, new_ptbl
);
1192 rcu_assign_pointer(old_ptbl
->last_lookup
, NULL
);
1193 kfree_rcu(old_ptbl
, rcu_head
);
1198 * disk_expand_part_tbl - expand disk->part_tbl
1199 * @disk: disk to expand part_tbl for
1200 * @partno: expand such that this partno can fit in
1202 * Expand disk->part_tbl such that @partno can fit in. disk->part_tbl
1203 * uses RCU to allow unlocked dereferencing for stats and other stuff.
1206 * Matching bd_mutex locked or the caller is the only user of @disk.
1210 * 0 on success, -errno on failure.
1212 int disk_expand_part_tbl(struct gendisk
*disk
, int partno
)
1214 struct disk_part_tbl
*old_ptbl
=
1215 rcu_dereference_protected(disk
->part_tbl
, 1);
1216 struct disk_part_tbl
*new_ptbl
;
1217 int len
= old_ptbl
? old_ptbl
->len
: 0;
1222 * check for int overflow, since we can get here from blkpg_ioctl()
1223 * with a user passed 'partno'.
1225 target
= partno
+ 1;
1229 /* disk_max_parts() is zero during initialization, ignore if so */
1230 if (disk_max_parts(disk
) && target
> disk_max_parts(disk
))
1236 size
= sizeof(*new_ptbl
) + target
* sizeof(new_ptbl
->part
[0]);
1237 new_ptbl
= kzalloc_node(size
, GFP_KERNEL
, disk
->node_id
);
1241 new_ptbl
->len
= target
;
1243 for (i
= 0; i
< len
; i
++)
1244 rcu_assign_pointer(new_ptbl
->part
[i
], old_ptbl
->part
[i
]);
1246 disk_replace_part_tbl(disk
, new_ptbl
);
1250 static void disk_release(struct device
*dev
)
1252 struct gendisk
*disk
= dev_to_disk(dev
);
1254 blk_free_devt(dev
->devt
);
1255 disk_release_events(disk
);
1256 kfree(disk
->random
);
1257 disk_replace_part_tbl(disk
, NULL
);
1258 hd_free_part(&disk
->part0
);
1260 blk_put_queue(disk
->queue
);
1263 struct class block_class
= {
1267 static char *block_devnode(struct device
*dev
, umode_t
*mode
,
1268 kuid_t
*uid
, kgid_t
*gid
)
1270 struct gendisk
*disk
= dev_to_disk(dev
);
1273 return disk
->devnode(disk
, mode
);
1277 static const struct device_type disk_type
= {
1279 .groups
= disk_attr_groups
,
1280 .release
= disk_release
,
1281 .devnode
= block_devnode
,
1284 #ifdef CONFIG_PROC_FS
1286 * aggregate disk stat collector. Uses the same stats that the sysfs
1287 * entries do, above, but makes them available through one seq_file.
1289 * The output looks suspiciously like /proc/partitions with a bunch of
1292 static int diskstats_show(struct seq_file
*seqf
, void *v
)
1294 struct gendisk
*gp
= v
;
1295 struct disk_part_iter piter
;
1296 struct hd_struct
*hd
;
1297 char buf
[BDEVNAME_SIZE
];
1298 unsigned int inflight
[2];
1302 if (&disk_to_dev(gp)->kobj.entry == block_class.devices.next)
1303 seq_puts(seqf, "major minor name"
1304 " rio rmerge rsect ruse wio wmerge "
1305 "wsect wuse running use aveq"
1309 disk_part_iter_init(&piter
, gp
, DISK_PITER_INCL_EMPTY_PART0
);
1310 while ((hd
= disk_part_iter_next(&piter
))) {
1311 cpu
= part_stat_lock();
1312 part_round_stats(gp
->queue
, cpu
, hd
);
1314 part_in_flight(gp
->queue
, hd
, inflight
);
1315 seq_printf(seqf
, "%4d %7d %s %lu %lu %lu "
1316 "%u %lu %lu %lu %u %u %u %u\n",
1317 MAJOR(part_devt(hd
)), MINOR(part_devt(hd
)),
1318 disk_name(gp
, hd
->partno
, buf
),
1319 part_stat_read(hd
, ios
[READ
]),
1320 part_stat_read(hd
, merges
[READ
]),
1321 part_stat_read(hd
, sectors
[READ
]),
1322 jiffies_to_msecs(part_stat_read(hd
, ticks
[READ
])),
1323 part_stat_read(hd
, ios
[WRITE
]),
1324 part_stat_read(hd
, merges
[WRITE
]),
1325 part_stat_read(hd
, sectors
[WRITE
]),
1326 jiffies_to_msecs(part_stat_read(hd
, ticks
[WRITE
])),
1328 jiffies_to_msecs(part_stat_read(hd
, io_ticks
)),
1329 jiffies_to_msecs(part_stat_read(hd
, time_in_queue
))
1332 disk_part_iter_exit(&piter
);
1337 static const struct seq_operations diskstats_op
= {
1338 .start
= disk_seqf_start
,
1339 .next
= disk_seqf_next
,
1340 .stop
= disk_seqf_stop
,
1341 .show
= diskstats_show
1344 static int diskstats_open(struct inode
*inode
, struct file
*file
)
1346 return seq_open(file
, &diskstats_op
);
1349 static const struct file_operations proc_diskstats_operations
= {
1350 .open
= diskstats_open
,
1352 .llseek
= seq_lseek
,
1353 .release
= seq_release
,
1356 static int __init
proc_genhd_init(void)
1358 proc_create("diskstats", 0, NULL
, &proc_diskstats_operations
);
1359 proc_create("partitions", 0, NULL
, &proc_partitions_operations
);
1362 module_init(proc_genhd_init
);
1363 #endif /* CONFIG_PROC_FS */
1365 dev_t
blk_lookup_devt(const char *name
, int partno
)
1367 dev_t devt
= MKDEV(0, 0);
1368 struct class_dev_iter iter
;
1371 class_dev_iter_init(&iter
, &block_class
, NULL
, &disk_type
);
1372 while ((dev
= class_dev_iter_next(&iter
))) {
1373 struct gendisk
*disk
= dev_to_disk(dev
);
1374 struct hd_struct
*part
;
1376 if (strcmp(dev_name(dev
), name
))
1379 if (partno
< disk
->minors
) {
1380 /* We need to return the right devno, even
1381 * if the partition doesn't exist yet.
1383 devt
= MKDEV(MAJOR(dev
->devt
),
1384 MINOR(dev
->devt
) + partno
);
1387 part
= disk_get_part(disk
, partno
);
1389 devt
= part_devt(part
);
1390 disk_put_part(part
);
1393 disk_put_part(part
);
1395 class_dev_iter_exit(&iter
);
1398 EXPORT_SYMBOL(blk_lookup_devt
);
1400 struct gendisk
*__alloc_disk_node(int minors
, int node_id
)
1402 struct gendisk
*disk
;
1403 struct disk_part_tbl
*ptbl
;
1405 if (minors
> DISK_MAX_PARTS
) {
1407 "block: can't allocate more than %d partitions\n",
1409 minors
= DISK_MAX_PARTS
;
1412 disk
= kzalloc_node(sizeof(struct gendisk
), GFP_KERNEL
, node_id
);
1414 if (!init_part_stats(&disk
->part0
)) {
1418 disk
->node_id
= node_id
;
1419 if (disk_expand_part_tbl(disk
, 0)) {
1420 free_part_stats(&disk
->part0
);
1424 ptbl
= rcu_dereference_protected(disk
->part_tbl
, 1);
1425 rcu_assign_pointer(ptbl
->part
[0], &disk
->part0
);
1428 * set_capacity() and get_capacity() currently don't use
1429 * seqcounter to read/update the part0->nr_sects. Still init
1430 * the counter as we can read the sectors in IO submission
1431 * patch using seqence counters.
1433 * TODO: Ideally set_capacity() and get_capacity() should be
1434 * converted to make use of bd_mutex and sequence counters.
1436 seqcount_init(&disk
->part0
.nr_sects_seq
);
1437 if (hd_ref_init(&disk
->part0
)) {
1438 hd_free_part(&disk
->part0
);
1443 disk
->minors
= minors
;
1444 rand_initialize_disk(disk
);
1445 disk_to_dev(disk
)->class = &block_class
;
1446 disk_to_dev(disk
)->type
= &disk_type
;
1447 device_initialize(disk_to_dev(disk
));
1451 EXPORT_SYMBOL(__alloc_disk_node
);
1453 struct kobject
*get_disk(struct gendisk
*disk
)
1455 struct module
*owner
;
1456 struct kobject
*kobj
;
1460 owner
= disk
->fops
->owner
;
1461 if (owner
&& !try_module_get(owner
))
1463 kobj
= kobject_get_unless_zero(&disk_to_dev(disk
)->kobj
);
1472 EXPORT_SYMBOL(get_disk
);
1474 void put_disk(struct gendisk
*disk
)
1477 kobject_put(&disk_to_dev(disk
)->kobj
);
1480 EXPORT_SYMBOL(put_disk
);
1482 static void set_disk_ro_uevent(struct gendisk
*gd
, int ro
)
1484 char event
[] = "DISK_RO=1";
1485 char *envp
[] = { event
, NULL
};
1489 kobject_uevent_env(&disk_to_dev(gd
)->kobj
, KOBJ_CHANGE
, envp
);
1492 void set_device_ro(struct block_device
*bdev
, int flag
)
1494 bdev
->bd_part
->policy
= flag
;
1497 EXPORT_SYMBOL(set_device_ro
);
1499 void set_disk_ro(struct gendisk
*disk
, int flag
)
1501 struct disk_part_iter piter
;
1502 struct hd_struct
*part
;
1504 if (disk
->part0
.policy
!= flag
) {
1505 set_disk_ro_uevent(disk
, flag
);
1506 disk
->part0
.policy
= flag
;
1509 disk_part_iter_init(&piter
, disk
, DISK_PITER_INCL_EMPTY
);
1510 while ((part
= disk_part_iter_next(&piter
)))
1511 part
->policy
= flag
;
1512 disk_part_iter_exit(&piter
);
1515 EXPORT_SYMBOL(set_disk_ro
);
1517 int bdev_read_only(struct block_device
*bdev
)
1521 return bdev
->bd_part
->policy
;
1524 EXPORT_SYMBOL(bdev_read_only
);
1526 int invalidate_partition(struct gendisk
*disk
, int partno
)
1529 struct block_device
*bdev
= bdget_disk(disk
, partno
);
1532 res
= __invalidate_device(bdev
, true);
1538 EXPORT_SYMBOL(invalidate_partition
);
1541 * Disk events - monitor disk events like media change and eject request.
1543 struct disk_events
{
1544 struct list_head node
; /* all disk_event's */
1545 struct gendisk
*disk
; /* the associated disk */
1548 struct mutex block_mutex
; /* protects blocking */
1549 int block
; /* event blocking depth */
1550 unsigned int pending
; /* events already sent out */
1551 unsigned int clearing
; /* events being cleared */
1553 long poll_msecs
; /* interval, -1 for default */
1554 struct delayed_work dwork
;
1557 static const char *disk_events_strs
[] = {
1558 [ilog2(DISK_EVENT_MEDIA_CHANGE
)] = "media_change",
1559 [ilog2(DISK_EVENT_EJECT_REQUEST
)] = "eject_request",
1562 static char *disk_uevents
[] = {
1563 [ilog2(DISK_EVENT_MEDIA_CHANGE
)] = "DISK_MEDIA_CHANGE=1",
1564 [ilog2(DISK_EVENT_EJECT_REQUEST
)] = "DISK_EJECT_REQUEST=1",
1567 /* list of all disk_events */
1568 static DEFINE_MUTEX(disk_events_mutex
);
1569 static LIST_HEAD(disk_events
);
1571 /* disable in-kernel polling by default */
1572 static unsigned long disk_events_dfl_poll_msecs
;
1574 static unsigned long disk_events_poll_jiffies(struct gendisk
*disk
)
1576 struct disk_events
*ev
= disk
->ev
;
1577 long intv_msecs
= 0;
1580 * If device-specific poll interval is set, always use it. If
1581 * the default is being used, poll iff there are events which
1582 * can't be monitored asynchronously.
1584 if (ev
->poll_msecs
>= 0)
1585 intv_msecs
= ev
->poll_msecs
;
1586 else if (disk
->events
& ~disk
->async_events
)
1587 intv_msecs
= disk_events_dfl_poll_msecs
;
1589 return msecs_to_jiffies(intv_msecs
);
1593 * disk_block_events - block and flush disk event checking
1594 * @disk: disk to block events for
1596 * On return from this function, it is guaranteed that event checking
1597 * isn't in progress and won't happen until unblocked by
1598 * disk_unblock_events(). Events blocking is counted and the actual
1599 * unblocking happens after the matching number of unblocks are done.
1601 * Note that this intentionally does not block event checking from
1602 * disk_clear_events().
1607 void disk_block_events(struct gendisk
*disk
)
1609 struct disk_events
*ev
= disk
->ev
;
1610 unsigned long flags
;
1617 * Outer mutex ensures that the first blocker completes canceling
1618 * the event work before further blockers are allowed to finish.
1620 mutex_lock(&ev
->block_mutex
);
1622 spin_lock_irqsave(&ev
->lock
, flags
);
1623 cancel
= !ev
->block
++;
1624 spin_unlock_irqrestore(&ev
->lock
, flags
);
1627 cancel_delayed_work_sync(&disk
->ev
->dwork
);
1629 mutex_unlock(&ev
->block_mutex
);
1632 static void __disk_unblock_events(struct gendisk
*disk
, bool check_now
)
1634 struct disk_events
*ev
= disk
->ev
;
1636 unsigned long flags
;
1638 spin_lock_irqsave(&ev
->lock
, flags
);
1640 if (WARN_ON_ONCE(ev
->block
<= 0))
1646 intv
= disk_events_poll_jiffies(disk
);
1648 queue_delayed_work(system_freezable_power_efficient_wq
,
1651 queue_delayed_work(system_freezable_power_efficient_wq
,
1654 spin_unlock_irqrestore(&ev
->lock
, flags
);
1658 * disk_unblock_events - unblock disk event checking
1659 * @disk: disk to unblock events for
1661 * Undo disk_block_events(). When the block count reaches zero, it
1662 * starts events polling if configured.
1665 * Don't care. Safe to call from irq context.
1667 void disk_unblock_events(struct gendisk
*disk
)
1670 __disk_unblock_events(disk
, false);
1674 * disk_flush_events - schedule immediate event checking and flushing
1675 * @disk: disk to check and flush events for
1676 * @mask: events to flush
1678 * Schedule immediate event checking on @disk if not blocked. Events in
1679 * @mask are scheduled to be cleared from the driver. Note that this
1680 * doesn't clear the events from @disk->ev.
1683 * If @mask is non-zero must be called with bdev->bd_mutex held.
1685 void disk_flush_events(struct gendisk
*disk
, unsigned int mask
)
1687 struct disk_events
*ev
= disk
->ev
;
1692 spin_lock_irq(&ev
->lock
);
1693 ev
->clearing
|= mask
;
1695 mod_delayed_work(system_freezable_power_efficient_wq
,
1697 spin_unlock_irq(&ev
->lock
);
1701 * disk_clear_events - synchronously check, clear and return pending events
1702 * @disk: disk to fetch and clear events from
1703 * @mask: mask of events to be fetched and cleared
1705 * Disk events are synchronously checked and pending events in @mask
1706 * are cleared and returned. This ignores the block count.
1711 unsigned int disk_clear_events(struct gendisk
*disk
, unsigned int mask
)
1713 const struct block_device_operations
*bdops
= disk
->fops
;
1714 struct disk_events
*ev
= disk
->ev
;
1715 unsigned int pending
;
1716 unsigned int clearing
= mask
;
1719 /* for drivers still using the old ->media_changed method */
1720 if ((mask
& DISK_EVENT_MEDIA_CHANGE
) &&
1721 bdops
->media_changed
&& bdops
->media_changed(disk
))
1722 return DISK_EVENT_MEDIA_CHANGE
;
1726 disk_block_events(disk
);
1729 * store the union of mask and ev->clearing on the stack so that the
1730 * race with disk_flush_events does not cause ambiguity (ev->clearing
1731 * can still be modified even if events are blocked).
1733 spin_lock_irq(&ev
->lock
);
1734 clearing
|= ev
->clearing
;
1736 spin_unlock_irq(&ev
->lock
);
1738 disk_check_events(ev
, &clearing
);
1740 * if ev->clearing is not 0, the disk_flush_events got called in the
1741 * middle of this function, so we want to run the workfn without delay.
1743 __disk_unblock_events(disk
, ev
->clearing
? true : false);
1745 /* then, fetch and clear pending events */
1746 spin_lock_irq(&ev
->lock
);
1747 pending
= ev
->pending
& mask
;
1748 ev
->pending
&= ~mask
;
1749 spin_unlock_irq(&ev
->lock
);
1750 WARN_ON_ONCE(clearing
& mask
);
1756 * Separate this part out so that a different pointer for clearing_ptr can be
1757 * passed in for disk_clear_events.
1759 static void disk_events_workfn(struct work_struct
*work
)
1761 struct delayed_work
*dwork
= to_delayed_work(work
);
1762 struct disk_events
*ev
= container_of(dwork
, struct disk_events
, dwork
);
1764 disk_check_events(ev
, &ev
->clearing
);
1767 static void disk_check_events(struct disk_events
*ev
,
1768 unsigned int *clearing_ptr
)
1770 struct gendisk
*disk
= ev
->disk
;
1771 char *envp
[ARRAY_SIZE(disk_uevents
) + 1] = { };
1772 unsigned int clearing
= *clearing_ptr
;
1773 unsigned int events
;
1775 int nr_events
= 0, i
;
1778 events
= disk
->fops
->check_events(disk
, clearing
);
1780 /* accumulate pending events and schedule next poll if necessary */
1781 spin_lock_irq(&ev
->lock
);
1783 events
&= ~ev
->pending
;
1784 ev
->pending
|= events
;
1785 *clearing_ptr
&= ~clearing
;
1787 intv
= disk_events_poll_jiffies(disk
);
1788 if (!ev
->block
&& intv
)
1789 queue_delayed_work(system_freezable_power_efficient_wq
,
1792 spin_unlock_irq(&ev
->lock
);
1795 * Tell userland about new events. Only the events listed in
1796 * @disk->events are reported. Unlisted events are processed the
1797 * same internally but never get reported to userland.
1799 for (i
= 0; i
< ARRAY_SIZE(disk_uevents
); i
++)
1800 if (events
& disk
->events
& (1 << i
))
1801 envp
[nr_events
++] = disk_uevents
[i
];
1804 kobject_uevent_env(&disk_to_dev(disk
)->kobj
, KOBJ_CHANGE
, envp
);
1808 * A disk events enabled device has the following sysfs nodes under
1809 * its /sys/block/X/ directory.
1811 * events : list of all supported events
1812 * events_async : list of events which can be detected w/o polling
1813 * events_poll_msecs : polling interval, 0: disable, -1: system default
1815 static ssize_t
__disk_events_show(unsigned int events
, char *buf
)
1817 const char *delim
= "";
1821 for (i
= 0; i
< ARRAY_SIZE(disk_events_strs
); i
++)
1822 if (events
& (1 << i
)) {
1823 pos
+= sprintf(buf
+ pos
, "%s%s",
1824 delim
, disk_events_strs
[i
]);
1828 pos
+= sprintf(buf
+ pos
, "\n");
1832 static ssize_t
disk_events_show(struct device
*dev
,
1833 struct device_attribute
*attr
, char *buf
)
1835 struct gendisk
*disk
= dev_to_disk(dev
);
1837 return __disk_events_show(disk
->events
, buf
);
1840 static ssize_t
disk_events_async_show(struct device
*dev
,
1841 struct device_attribute
*attr
, char *buf
)
1843 struct gendisk
*disk
= dev_to_disk(dev
);
1845 return __disk_events_show(disk
->async_events
, buf
);
1848 static ssize_t
disk_events_poll_msecs_show(struct device
*dev
,
1849 struct device_attribute
*attr
,
1852 struct gendisk
*disk
= dev_to_disk(dev
);
1854 return sprintf(buf
, "%ld\n", disk
->ev
->poll_msecs
);
1857 static ssize_t
disk_events_poll_msecs_store(struct device
*dev
,
1858 struct device_attribute
*attr
,
1859 const char *buf
, size_t count
)
1861 struct gendisk
*disk
= dev_to_disk(dev
);
1864 if (!count
|| !sscanf(buf
, "%ld", &intv
))
1867 if (intv
< 0 && intv
!= -1)
1870 disk_block_events(disk
);
1871 disk
->ev
->poll_msecs
= intv
;
1872 __disk_unblock_events(disk
, true);
1877 static const DEVICE_ATTR(events
, S_IRUGO
, disk_events_show
, NULL
);
1878 static const DEVICE_ATTR(events_async
, S_IRUGO
, disk_events_async_show
, NULL
);
1879 static const DEVICE_ATTR(events_poll_msecs
, S_IRUGO
|S_IWUSR
,
1880 disk_events_poll_msecs_show
,
1881 disk_events_poll_msecs_store
);
1883 static const struct attribute
*disk_events_attrs
[] = {
1884 &dev_attr_events
.attr
,
1885 &dev_attr_events_async
.attr
,
1886 &dev_attr_events_poll_msecs
.attr
,
1891 * The default polling interval can be specified by the kernel
1892 * parameter block.events_dfl_poll_msecs which defaults to 0
1893 * (disable). This can also be modified runtime by writing to
1894 * /sys/module/block/events_dfl_poll_msecs.
1896 static int disk_events_set_dfl_poll_msecs(const char *val
,
1897 const struct kernel_param
*kp
)
1899 struct disk_events
*ev
;
1902 ret
= param_set_ulong(val
, kp
);
1906 mutex_lock(&disk_events_mutex
);
1908 list_for_each_entry(ev
, &disk_events
, node
)
1909 disk_flush_events(ev
->disk
, 0);
1911 mutex_unlock(&disk_events_mutex
);
1916 static const struct kernel_param_ops disk_events_dfl_poll_msecs_param_ops
= {
1917 .set
= disk_events_set_dfl_poll_msecs
,
1918 .get
= param_get_ulong
,
1921 #undef MODULE_PARAM_PREFIX
1922 #define MODULE_PARAM_PREFIX "block."
1924 module_param_cb(events_dfl_poll_msecs
, &disk_events_dfl_poll_msecs_param_ops
,
1925 &disk_events_dfl_poll_msecs
, 0644);
1928 * disk_{alloc|add|del|release}_events - initialize and destroy disk_events.
1930 static void disk_alloc_events(struct gendisk
*disk
)
1932 struct disk_events
*ev
;
1934 if (!disk
->fops
->check_events
)
1937 ev
= kzalloc(sizeof(*ev
), GFP_KERNEL
);
1939 pr_warn("%s: failed to initialize events\n", disk
->disk_name
);
1943 INIT_LIST_HEAD(&ev
->node
);
1945 spin_lock_init(&ev
->lock
);
1946 mutex_init(&ev
->block_mutex
);
1948 ev
->poll_msecs
= -1;
1949 INIT_DELAYED_WORK(&ev
->dwork
, disk_events_workfn
);
1954 static void disk_add_events(struct gendisk
*disk
)
1959 /* FIXME: error handling */
1960 if (sysfs_create_files(&disk_to_dev(disk
)->kobj
, disk_events_attrs
) < 0)
1961 pr_warn("%s: failed to create sysfs files for events\n",
1964 mutex_lock(&disk_events_mutex
);
1965 list_add_tail(&disk
->ev
->node
, &disk_events
);
1966 mutex_unlock(&disk_events_mutex
);
1969 * Block count is initialized to 1 and the following initial
1970 * unblock kicks it into action.
1972 __disk_unblock_events(disk
, true);
1975 static void disk_del_events(struct gendisk
*disk
)
1980 disk_block_events(disk
);
1982 mutex_lock(&disk_events_mutex
);
1983 list_del_init(&disk
->ev
->node
);
1984 mutex_unlock(&disk_events_mutex
);
1986 sysfs_remove_files(&disk_to_dev(disk
)->kobj
, disk_events_attrs
);
1989 static void disk_release_events(struct gendisk
*disk
)
1991 /* the block count should be 1 from disk_del_events() */
1992 WARN_ON_ONCE(disk
->ev
&& disk
->ev
->block
!= 1);