]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - block/genhd.c
Merge tag 'libnvdimm-for-5.11' of git://git.kernel.org/pub/scm/linux/kernel/git/nvdim...
[mirror_ubuntu-jammy-kernel.git] / block / genhd.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * gendisk handling
4 */
5
6 #include <linux/module.h>
7 #include <linux/ctype.h>
8 #include <linux/fs.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>
25
26 #include "blk.h"
27
28 static struct kobject *block_depr;
29
30 DECLARE_RWSEM(bdev_lookup_sem);
31
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);
35
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);
42
43 void set_capacity(struct gendisk *disk, sector_t sectors)
44 {
45 struct block_device *bdev = disk->part0;
46
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);
50 }
51 EXPORT_SYMBOL(set_capacity);
52
53 /*
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.
56 */
57 bool set_capacity_and_notify(struct gendisk *disk, sector_t size)
58 {
59 sector_t capacity = get_capacity(disk);
60 char *envp[] = { "RESIZE=1", NULL };
61
62 set_capacity(disk, size);
63
64 /*
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.
68 */
69 if (size == capacity ||
70 (disk->flags & (GENHD_FL_UP | GENHD_FL_HIDDEN)) != GENHD_FL_UP)
71 return false;
72
73 pr_info("%s: detected capacity change from %lld to %lld\n",
74 disk->disk_name, size, capacity);
75
76 /*
77 * Historically we did not send a uevent for changes to/from an empty
78 * device.
79 */
80 if (!capacity || !size)
81 return false;
82 kobject_uevent_env(&disk_to_dev(disk)->kobj, KOBJ_CHANGE, envp);
83 return true;
84 }
85 EXPORT_SYMBOL_GPL(set_capacity_and_notify);
86
87 /*
88 * Format the device name of the indicated disk into the supplied buffer and
89 * return a pointer to that same buffer for convenience.
90 */
91 char *disk_name(struct gendisk *hd, int partno, char *buf)
92 {
93 if (!partno)
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);
97 else
98 snprintf(buf, BDEVNAME_SIZE, "%s%d", hd->disk_name, partno);
99
100 return buf;
101 }
102
103 const char *bdevname(struct block_device *bdev, char *buf)
104 {
105 return disk_name(bdev->bd_disk, bdev->bd_partno, buf);
106 }
107 EXPORT_SYMBOL(bdevname);
108
109 static void part_stat_read_all(struct block_device *part,
110 struct disk_stats *stat)
111 {
112 int cpu;
113
114 memset(stat, 0, sizeof(struct disk_stats));
115 for_each_possible_cpu(cpu) {
116 struct disk_stats *ptr = per_cpu_ptr(part->bd_stats, cpu);
117 int group;
118
119 for (group = 0; group < NR_STAT_GROUPS; group++) {
120 stat->nsecs[group] += ptr->nsecs[group];
121 stat->sectors[group] += ptr->sectors[group];
122 stat->ios[group] += ptr->ios[group];
123 stat->merges[group] += ptr->merges[group];
124 }
125
126 stat->io_ticks += ptr->io_ticks;
127 }
128 }
129
130 static unsigned int part_in_flight(struct block_device *part)
131 {
132 unsigned int inflight = 0;
133 int cpu;
134
135 for_each_possible_cpu(cpu) {
136 inflight += part_stat_local_read_cpu(part, in_flight[0], cpu) +
137 part_stat_local_read_cpu(part, in_flight[1], cpu);
138 }
139 if ((int)inflight < 0)
140 inflight = 0;
141
142 return inflight;
143 }
144
145 static void part_in_flight_rw(struct block_device *part,
146 unsigned int inflight[2])
147 {
148 int cpu;
149
150 inflight[0] = 0;
151 inflight[1] = 0;
152 for_each_possible_cpu(cpu) {
153 inflight[0] += part_stat_local_read_cpu(part, in_flight[0], cpu);
154 inflight[1] += part_stat_local_read_cpu(part, in_flight[1], cpu);
155 }
156 if ((int)inflight[0] < 0)
157 inflight[0] = 0;
158 if ((int)inflight[1] < 0)
159 inflight[1] = 0;
160 }
161
162 struct block_device *__disk_get_part(struct gendisk *disk, int partno)
163 {
164 struct disk_part_tbl *ptbl = rcu_dereference(disk->part_tbl);
165
166 if (unlikely(partno < 0 || partno >= ptbl->len))
167 return NULL;
168 return rcu_dereference(ptbl->part[partno]);
169 }
170
171 /**
172 * disk_part_iter_init - initialize partition iterator
173 * @piter: iterator to initialize
174 * @disk: disk to iterate over
175 * @flags: DISK_PITER_* flags
176 *
177 * Initialize @piter so that it iterates over partitions of @disk.
178 *
179 * CONTEXT:
180 * Don't care.
181 */
182 void disk_part_iter_init(struct disk_part_iter *piter, struct gendisk *disk,
183 unsigned int flags)
184 {
185 struct disk_part_tbl *ptbl;
186
187 rcu_read_lock();
188 ptbl = rcu_dereference(disk->part_tbl);
189
190 piter->disk = disk;
191 piter->part = NULL;
192
193 if (flags & DISK_PITER_REVERSE)
194 piter->idx = ptbl->len - 1;
195 else if (flags & (DISK_PITER_INCL_PART0 | DISK_PITER_INCL_EMPTY_PART0))
196 piter->idx = 0;
197 else
198 piter->idx = 1;
199
200 piter->flags = flags;
201
202 rcu_read_unlock();
203 }
204 EXPORT_SYMBOL_GPL(disk_part_iter_init);
205
206 /**
207 * disk_part_iter_next - proceed iterator to the next partition and return it
208 * @piter: iterator of interest
209 *
210 * Proceed @piter to the next partition and return it.
211 *
212 * CONTEXT:
213 * Don't care.
214 */
215 struct block_device *disk_part_iter_next(struct disk_part_iter *piter)
216 {
217 struct disk_part_tbl *ptbl;
218 int inc, end;
219
220 /* put the last partition */
221 disk_part_iter_exit(piter);
222
223 /* get part_tbl */
224 rcu_read_lock();
225 ptbl = rcu_dereference(piter->disk->part_tbl);
226
227 /* determine iteration parameters */
228 if (piter->flags & DISK_PITER_REVERSE) {
229 inc = -1;
230 if (piter->flags & (DISK_PITER_INCL_PART0 |
231 DISK_PITER_INCL_EMPTY_PART0))
232 end = -1;
233 else
234 end = 0;
235 } else {
236 inc = 1;
237 end = ptbl->len;
238 }
239
240 /* iterate to the next partition */
241 for (; piter->idx != end; piter->idx += inc) {
242 struct block_device *part;
243
244 part = rcu_dereference(ptbl->part[piter->idx]);
245 if (!part)
246 continue;
247 if (!bdev_nr_sectors(part) &&
248 !(piter->flags & DISK_PITER_INCL_EMPTY) &&
249 !(piter->flags & DISK_PITER_INCL_EMPTY_PART0 &&
250 piter->idx == 0))
251 continue;
252
253 piter->part = bdgrab(part);
254 if (!piter->part)
255 continue;
256 piter->idx += inc;
257 break;
258 }
259
260 rcu_read_unlock();
261
262 return piter->part;
263 }
264 EXPORT_SYMBOL_GPL(disk_part_iter_next);
265
266 /**
267 * disk_part_iter_exit - finish up partition iteration
268 * @piter: iter of interest
269 *
270 * Called when iteration is over. Cleans up @piter.
271 *
272 * CONTEXT:
273 * Don't care.
274 */
275 void disk_part_iter_exit(struct disk_part_iter *piter)
276 {
277 if (piter->part)
278 bdput(piter->part);
279 piter->part = NULL;
280 }
281 EXPORT_SYMBOL_GPL(disk_part_iter_exit);
282
283 static inline int sector_in_part(struct block_device *part, sector_t sector)
284 {
285 return part->bd_start_sect <= sector &&
286 sector < part->bd_start_sect + bdev_nr_sectors(part);
287 }
288
289 /**
290 * disk_map_sector_rcu - map sector to partition
291 * @disk: gendisk of interest
292 * @sector: sector to map
293 *
294 * Find out which partition @sector maps to on @disk. This is
295 * primarily used for stats accounting.
296 *
297 * CONTEXT:
298 * RCU read locked.
299 *
300 * RETURNS:
301 * Found partition on success, part0 is returned if no partition matches
302 * or the matched partition is being deleted.
303 */
304 struct block_device *disk_map_sector_rcu(struct gendisk *disk, sector_t sector)
305 {
306 struct disk_part_tbl *ptbl;
307 struct block_device *part;
308 int i;
309
310 rcu_read_lock();
311 ptbl = rcu_dereference(disk->part_tbl);
312
313 part = rcu_dereference(ptbl->last_lookup);
314 if (part && sector_in_part(part, sector))
315 goto out_unlock;
316
317 for (i = 1; i < ptbl->len; i++) {
318 part = rcu_dereference(ptbl->part[i]);
319 if (part && sector_in_part(part, sector)) {
320 rcu_assign_pointer(ptbl->last_lookup, part);
321 goto out_unlock;
322 }
323 }
324
325 part = disk->part0;
326 out_unlock:
327 rcu_read_unlock();
328 return part;
329 }
330
331 /**
332 * disk_has_partitions
333 * @disk: gendisk of interest
334 *
335 * Walk through the partition table and check if valid partition exists.
336 *
337 * CONTEXT:
338 * Don't care.
339 *
340 * RETURNS:
341 * True if the gendisk has at least one valid non-zero size partition.
342 * Otherwise false.
343 */
344 bool disk_has_partitions(struct gendisk *disk)
345 {
346 struct disk_part_tbl *ptbl;
347 int i;
348 bool ret = false;
349
350 rcu_read_lock();
351 ptbl = rcu_dereference(disk->part_tbl);
352
353 /* Iterate partitions skipping the whole device at index 0 */
354 for (i = 1; i < ptbl->len; i++) {
355 if (rcu_dereference(ptbl->part[i])) {
356 ret = true;
357 break;
358 }
359 }
360
361 rcu_read_unlock();
362
363 return ret;
364 }
365 EXPORT_SYMBOL_GPL(disk_has_partitions);
366
367 /*
368 * Can be deleted altogether. Later.
369 *
370 */
371 #define BLKDEV_MAJOR_HASH_SIZE 255
372 static struct blk_major_name {
373 struct blk_major_name *next;
374 int major;
375 char name[16];
376 void (*probe)(dev_t devt);
377 } *major_names[BLKDEV_MAJOR_HASH_SIZE];
378 static DEFINE_MUTEX(major_names_lock);
379
380 /* index in the above - for now: assume no multimajor ranges */
381 static inline int major_to_index(unsigned major)
382 {
383 return major % BLKDEV_MAJOR_HASH_SIZE;
384 }
385
386 #ifdef CONFIG_PROC_FS
387 void blkdev_show(struct seq_file *seqf, off_t offset)
388 {
389 struct blk_major_name *dp;
390
391 mutex_lock(&major_names_lock);
392 for (dp = major_names[major_to_index(offset)]; dp; dp = dp->next)
393 if (dp->major == offset)
394 seq_printf(seqf, "%3d %s\n", dp->major, dp->name);
395 mutex_unlock(&major_names_lock);
396 }
397 #endif /* CONFIG_PROC_FS */
398
399 /**
400 * __register_blkdev - register a new block device
401 *
402 * @major: the requested major device number [1..BLKDEV_MAJOR_MAX-1]. If
403 * @major = 0, try to allocate any unused major number.
404 * @name: the name of the new block device as a zero terminated string
405 * @probe: allback that is called on access to any minor number of @major
406 *
407 * The @name must be unique within the system.
408 *
409 * The return value depends on the @major input parameter:
410 *
411 * - if a major device number was requested in range [1..BLKDEV_MAJOR_MAX-1]
412 * then the function returns zero on success, or a negative error code
413 * - if any unused major number was requested with @major = 0 parameter
414 * then the return value is the allocated major number in range
415 * [1..BLKDEV_MAJOR_MAX-1] or a negative error code otherwise
416 *
417 * See Documentation/admin-guide/devices.txt for the list of allocated
418 * major numbers.
419 *
420 * Use register_blkdev instead for any new code.
421 */
422 int __register_blkdev(unsigned int major, const char *name,
423 void (*probe)(dev_t devt))
424 {
425 struct blk_major_name **n, *p;
426 int index, ret = 0;
427
428 mutex_lock(&major_names_lock);
429
430 /* temporary */
431 if (major == 0) {
432 for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) {
433 if (major_names[index] == NULL)
434 break;
435 }
436
437 if (index == 0) {
438 printk("%s: failed to get major for %s\n",
439 __func__, name);
440 ret = -EBUSY;
441 goto out;
442 }
443 major = index;
444 ret = major;
445 }
446
447 if (major >= BLKDEV_MAJOR_MAX) {
448 pr_err("%s: major requested (%u) is greater than the maximum (%u) for %s\n",
449 __func__, major, BLKDEV_MAJOR_MAX-1, name);
450
451 ret = -EINVAL;
452 goto out;
453 }
454
455 p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL);
456 if (p == NULL) {
457 ret = -ENOMEM;
458 goto out;
459 }
460
461 p->major = major;
462 p->probe = probe;
463 strlcpy(p->name, name, sizeof(p->name));
464 p->next = NULL;
465 index = major_to_index(major);
466
467 for (n = &major_names[index]; *n; n = &(*n)->next) {
468 if ((*n)->major == major)
469 break;
470 }
471 if (!*n)
472 *n = p;
473 else
474 ret = -EBUSY;
475
476 if (ret < 0) {
477 printk("register_blkdev: cannot get major %u for %s\n",
478 major, name);
479 kfree(p);
480 }
481 out:
482 mutex_unlock(&major_names_lock);
483 return ret;
484 }
485 EXPORT_SYMBOL(__register_blkdev);
486
487 void unregister_blkdev(unsigned int major, const char *name)
488 {
489 struct blk_major_name **n;
490 struct blk_major_name *p = NULL;
491 int index = major_to_index(major);
492
493 mutex_lock(&major_names_lock);
494 for (n = &major_names[index]; *n; n = &(*n)->next)
495 if ((*n)->major == major)
496 break;
497 if (!*n || strcmp((*n)->name, name)) {
498 WARN_ON(1);
499 } else {
500 p = *n;
501 *n = p->next;
502 }
503 mutex_unlock(&major_names_lock);
504 kfree(p);
505 }
506
507 EXPORT_SYMBOL(unregister_blkdev);
508
509 /**
510 * blk_mangle_minor - scatter minor numbers apart
511 * @minor: minor number to mangle
512 *
513 * Scatter consecutively allocated @minor number apart if MANGLE_DEVT
514 * is enabled. Mangling twice gives the original value.
515 *
516 * RETURNS:
517 * Mangled value.
518 *
519 * CONTEXT:
520 * Don't care.
521 */
522 static int blk_mangle_minor(int minor)
523 {
524 #ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT
525 int i;
526
527 for (i = 0; i < MINORBITS / 2; i++) {
528 int low = minor & (1 << i);
529 int high = minor & (1 << (MINORBITS - 1 - i));
530 int distance = MINORBITS - 1 - 2 * i;
531
532 minor ^= low | high; /* clear both bits */
533 low <<= distance; /* swap the positions */
534 high >>= distance;
535 minor |= low | high; /* and set */
536 }
537 #endif
538 return minor;
539 }
540
541 /**
542 * blk_alloc_devt - allocate a dev_t for a block device
543 * @bdev: block device to allocate dev_t for
544 * @devt: out parameter for resulting dev_t
545 *
546 * Allocate a dev_t for block device.
547 *
548 * RETURNS:
549 * 0 on success, allocated dev_t is returned in *@devt. -errno on
550 * failure.
551 *
552 * CONTEXT:
553 * Might sleep.
554 */
555 int blk_alloc_devt(struct block_device *bdev, dev_t *devt)
556 {
557 struct gendisk *disk = bdev->bd_disk;
558 int idx;
559
560 /* in consecutive minor range? */
561 if (bdev->bd_partno < disk->minors) {
562 *devt = MKDEV(disk->major, disk->first_minor + bdev->bd_partno);
563 return 0;
564 }
565
566 idx = ida_alloc_range(&ext_devt_ida, 0, NR_EXT_DEVT, GFP_KERNEL);
567 if (idx < 0)
568 return idx == -ENOSPC ? -EBUSY : idx;
569
570 *devt = MKDEV(BLOCK_EXT_MAJOR, blk_mangle_minor(idx));
571 return 0;
572 }
573
574 /**
575 * blk_free_devt - free a dev_t
576 * @devt: dev_t to free
577 *
578 * Free @devt which was allocated using blk_alloc_devt().
579 *
580 * CONTEXT:
581 * Might sleep.
582 */
583 void blk_free_devt(dev_t devt)
584 {
585 if (MAJOR(devt) == BLOCK_EXT_MAJOR)
586 ida_free(&ext_devt_ida, blk_mangle_minor(MINOR(devt)));
587 }
588
589 static char *bdevt_str(dev_t devt, char *buf)
590 {
591 if (MAJOR(devt) <= 0xff && MINOR(devt) <= 0xff) {
592 char tbuf[BDEVT_SIZE];
593 snprintf(tbuf, BDEVT_SIZE, "%02x%02x", MAJOR(devt), MINOR(devt));
594 snprintf(buf, BDEVT_SIZE, "%-9s", tbuf);
595 } else
596 snprintf(buf, BDEVT_SIZE, "%03x:%05x", MAJOR(devt), MINOR(devt));
597
598 return buf;
599 }
600
601 static void disk_scan_partitions(struct gendisk *disk)
602 {
603 struct block_device *bdev;
604
605 if (!get_capacity(disk) || !disk_part_scan_enabled(disk))
606 return;
607
608 set_bit(GD_NEED_PART_SCAN, &disk->state);
609 bdev = blkdev_get_by_dev(disk_devt(disk), FMODE_READ, NULL);
610 if (!IS_ERR(bdev))
611 blkdev_put(bdev, FMODE_READ);
612 }
613
614 static void register_disk(struct device *parent, struct gendisk *disk,
615 const struct attribute_group **groups)
616 {
617 struct device *ddev = disk_to_dev(disk);
618 struct disk_part_iter piter;
619 struct block_device *part;
620 int err;
621
622 ddev->parent = parent;
623
624 dev_set_name(ddev, "%s", disk->disk_name);
625
626 /* delay uevents, until we scanned partition table */
627 dev_set_uevent_suppress(ddev, 1);
628
629 if (groups) {
630 WARN_ON(ddev->groups);
631 ddev->groups = groups;
632 }
633 if (device_add(ddev))
634 return;
635 if (!sysfs_deprecated) {
636 err = sysfs_create_link(block_depr, &ddev->kobj,
637 kobject_name(&ddev->kobj));
638 if (err) {
639 device_del(ddev);
640 return;
641 }
642 }
643
644 /*
645 * avoid probable deadlock caused by allocating memory with
646 * GFP_KERNEL in runtime_resume callback of its all ancestor
647 * devices
648 */
649 pm_runtime_set_memalloc_noio(ddev, true);
650
651 disk->part0->bd_holder_dir =
652 kobject_create_and_add("holders", &ddev->kobj);
653 disk->slave_dir = kobject_create_and_add("slaves", &ddev->kobj);
654
655 if (disk->flags & GENHD_FL_HIDDEN) {
656 dev_set_uevent_suppress(ddev, 0);
657 return;
658 }
659
660 disk_scan_partitions(disk);
661
662 /* announce disk after possible partitions are created */
663 dev_set_uevent_suppress(ddev, 0);
664 kobject_uevent(&ddev->kobj, KOBJ_ADD);
665
666 /* announce possible partitions */
667 disk_part_iter_init(&piter, disk, 0);
668 while ((part = disk_part_iter_next(&piter)))
669 kobject_uevent(bdev_kobj(part), KOBJ_ADD);
670 disk_part_iter_exit(&piter);
671
672 if (disk->queue->backing_dev_info->dev) {
673 err = sysfs_create_link(&ddev->kobj,
674 &disk->queue->backing_dev_info->dev->kobj,
675 "bdi");
676 WARN_ON(err);
677 }
678 }
679
680 /**
681 * __device_add_disk - add disk information to kernel list
682 * @parent: parent device for the disk
683 * @disk: per-device partitioning information
684 * @groups: Additional per-device sysfs groups
685 * @register_queue: register the queue if set to true
686 *
687 * This function registers the partitioning information in @disk
688 * with the kernel.
689 *
690 * FIXME: error handling
691 */
692 static void __device_add_disk(struct device *parent, struct gendisk *disk,
693 const struct attribute_group **groups,
694 bool register_queue)
695 {
696 dev_t devt;
697 int retval;
698
699 /*
700 * The disk queue should now be all set with enough information about
701 * the device for the elevator code to pick an adequate default
702 * elevator if one is needed, that is, for devices requesting queue
703 * registration.
704 */
705 if (register_queue)
706 elevator_init_mq(disk->queue);
707
708 /* minors == 0 indicates to use ext devt from part0 and should
709 * be accompanied with EXT_DEVT flag. Make sure all
710 * parameters make sense.
711 */
712 WARN_ON(disk->minors && !(disk->major || disk->first_minor));
713 WARN_ON(!disk->minors &&
714 !(disk->flags & (GENHD_FL_EXT_DEVT | GENHD_FL_HIDDEN)));
715
716 disk->flags |= GENHD_FL_UP;
717
718 retval = blk_alloc_devt(disk->part0, &devt);
719 if (retval) {
720 WARN_ON(1);
721 return;
722 }
723 disk->major = MAJOR(devt);
724 disk->first_minor = MINOR(devt);
725
726 disk_alloc_events(disk);
727
728 if (disk->flags & GENHD_FL_HIDDEN) {
729 /*
730 * Don't let hidden disks show up in /proc/partitions,
731 * and don't bother scanning for partitions either.
732 */
733 disk->flags |= GENHD_FL_SUPPRESS_PARTITION_INFO;
734 disk->flags |= GENHD_FL_NO_PART_SCAN;
735 } else {
736 struct backing_dev_info *bdi = disk->queue->backing_dev_info;
737 struct device *dev = disk_to_dev(disk);
738 int ret;
739
740 /* Register BDI before referencing it from bdev */
741 dev->devt = devt;
742 ret = bdi_register(bdi, "%u:%u", MAJOR(devt), MINOR(devt));
743 WARN_ON(ret);
744 bdi_set_owner(bdi, dev);
745 bdev_add(disk->part0, devt);
746 }
747 register_disk(parent, disk, groups);
748 if (register_queue)
749 blk_register_queue(disk);
750
751 /*
752 * Take an extra ref on queue which will be put on disk_release()
753 * so that it sticks around as long as @disk is there.
754 */
755 WARN_ON_ONCE(!blk_get_queue(disk->queue));
756
757 disk_add_events(disk);
758 blk_integrity_add(disk);
759 }
760
761 void device_add_disk(struct device *parent, struct gendisk *disk,
762 const struct attribute_group **groups)
763
764 {
765 __device_add_disk(parent, disk, groups, true);
766 }
767 EXPORT_SYMBOL(device_add_disk);
768
769 void device_add_disk_no_queue_reg(struct device *parent, struct gendisk *disk)
770 {
771 __device_add_disk(parent, disk, NULL, false);
772 }
773 EXPORT_SYMBOL(device_add_disk_no_queue_reg);
774
775 static void invalidate_partition(struct block_device *bdev)
776 {
777 fsync_bdev(bdev);
778 __invalidate_device(bdev, true);
779
780 /*
781 * Unhash the bdev inode for this device so that it can't be looked
782 * up any more even if openers still hold references to it.
783 */
784 remove_inode_hash(bdev->bd_inode);
785 }
786
787 /**
788 * del_gendisk - remove the gendisk
789 * @disk: the struct gendisk to remove
790 *
791 * Removes the gendisk and all its associated resources. This deletes the
792 * partitions associated with the gendisk, and unregisters the associated
793 * request_queue.
794 *
795 * This is the counter to the respective __device_add_disk() call.
796 *
797 * The final removal of the struct gendisk happens when its refcount reaches 0
798 * with put_disk(), which should be called after del_gendisk(), if
799 * __device_add_disk() was used.
800 *
801 * Drivers exist which depend on the release of the gendisk to be synchronous,
802 * it should not be deferred.
803 *
804 * Context: can sleep
805 */
806 void del_gendisk(struct gendisk *disk)
807 {
808 struct disk_part_iter piter;
809 struct block_device *part;
810
811 might_sleep();
812
813 if (WARN_ON_ONCE(!disk->queue))
814 return;
815
816 blk_integrity_del(disk);
817 disk_del_events(disk);
818
819 /*
820 * Block lookups of the disk until all bdevs are unhashed and the
821 * disk is marked as dead (GENHD_FL_UP cleared).
822 */
823 down_write(&bdev_lookup_sem);
824
825 /* invalidate stuff */
826 disk_part_iter_init(&piter, disk,
827 DISK_PITER_INCL_EMPTY | DISK_PITER_REVERSE);
828 while ((part = disk_part_iter_next(&piter))) {
829 invalidate_partition(part);
830 delete_partition(part);
831 }
832 disk_part_iter_exit(&piter);
833
834 invalidate_partition(disk->part0);
835 set_capacity(disk, 0);
836 disk->flags &= ~GENHD_FL_UP;
837 up_write(&bdev_lookup_sem);
838
839 if (!(disk->flags & GENHD_FL_HIDDEN)) {
840 sysfs_remove_link(&disk_to_dev(disk)->kobj, "bdi");
841
842 /*
843 * Unregister bdi before releasing device numbers (as they can
844 * get reused and we'd get clashes in sysfs).
845 */
846 bdi_unregister(disk->queue->backing_dev_info);
847 }
848
849 blk_unregister_queue(disk);
850
851 kobject_put(disk->part0->bd_holder_dir);
852 kobject_put(disk->slave_dir);
853
854 part_stat_set_all(disk->part0, 0);
855 disk->part0->bd_stamp = 0;
856 if (!sysfs_deprecated)
857 sysfs_remove_link(block_depr, dev_name(disk_to_dev(disk)));
858 pm_runtime_set_memalloc_noio(disk_to_dev(disk), false);
859 device_del(disk_to_dev(disk));
860 }
861 EXPORT_SYMBOL(del_gendisk);
862
863 /* sysfs access to bad-blocks list. */
864 static ssize_t disk_badblocks_show(struct device *dev,
865 struct device_attribute *attr,
866 char *page)
867 {
868 struct gendisk *disk = dev_to_disk(dev);
869
870 if (!disk->bb)
871 return sprintf(page, "\n");
872
873 return badblocks_show(disk->bb, page, 0);
874 }
875
876 static ssize_t disk_badblocks_store(struct device *dev,
877 struct device_attribute *attr,
878 const char *page, size_t len)
879 {
880 struct gendisk *disk = dev_to_disk(dev);
881
882 if (!disk->bb)
883 return -ENXIO;
884
885 return badblocks_store(disk->bb, page, len, 0);
886 }
887
888 void blk_request_module(dev_t devt)
889 {
890 unsigned int major = MAJOR(devt);
891 struct blk_major_name **n;
892
893 mutex_lock(&major_names_lock);
894 for (n = &major_names[major_to_index(major)]; *n; n = &(*n)->next) {
895 if ((*n)->major == major && (*n)->probe) {
896 (*n)->probe(devt);
897 mutex_unlock(&major_names_lock);
898 return;
899 }
900 }
901 mutex_unlock(&major_names_lock);
902
903 if (request_module("block-major-%d-%d", MAJOR(devt), MINOR(devt)) > 0)
904 /* Make old-style 2.4 aliases work */
905 request_module("block-major-%d", MAJOR(devt));
906 }
907
908 /**
909 * bdget_disk - do bdget() by gendisk and partition number
910 * @disk: gendisk of interest
911 * @partno: partition number
912 *
913 * Find partition @partno from @disk, do bdget() on it.
914 *
915 * CONTEXT:
916 * Don't care.
917 *
918 * RETURNS:
919 * Resulting block_device on success, NULL on failure.
920 */
921 struct block_device *bdget_disk(struct gendisk *disk, int partno)
922 {
923 struct block_device *bdev = NULL;
924
925 rcu_read_lock();
926 bdev = __disk_get_part(disk, partno);
927 if (bdev && !bdgrab(bdev))
928 bdev = NULL;
929 rcu_read_unlock();
930
931 return bdev;
932 }
933
934 /*
935 * print a full list of all partitions - intended for places where the root
936 * filesystem can't be mounted and thus to give the victim some idea of what
937 * went wrong
938 */
939 void __init printk_all_partitions(void)
940 {
941 struct class_dev_iter iter;
942 struct device *dev;
943
944 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
945 while ((dev = class_dev_iter_next(&iter))) {
946 struct gendisk *disk = dev_to_disk(dev);
947 struct disk_part_iter piter;
948 struct block_device *part;
949 char name_buf[BDEVNAME_SIZE];
950 char devt_buf[BDEVT_SIZE];
951
952 /*
953 * Don't show empty devices or things that have been
954 * suppressed
955 */
956 if (get_capacity(disk) == 0 ||
957 (disk->flags & GENHD_FL_SUPPRESS_PARTITION_INFO))
958 continue;
959
960 /*
961 * Note, unlike /proc/partitions, I am showing the
962 * numbers in hex - the same format as the root=
963 * option takes.
964 */
965 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
966 while ((part = disk_part_iter_next(&piter))) {
967 bool is_part0 = part == disk->part0;
968
969 printk("%s%s %10llu %s %s", is_part0 ? "" : " ",
970 bdevt_str(part->bd_dev, devt_buf),
971 bdev_nr_sectors(part) >> 1,
972 disk_name(disk, part->bd_partno, name_buf),
973 part->bd_meta_info ?
974 part->bd_meta_info->uuid : "");
975 if (is_part0) {
976 if (dev->parent && dev->parent->driver)
977 printk(" driver: %s\n",
978 dev->parent->driver->name);
979 else
980 printk(" (driver?)\n");
981 } else
982 printk("\n");
983 }
984 disk_part_iter_exit(&piter);
985 }
986 class_dev_iter_exit(&iter);
987 }
988
989 #ifdef CONFIG_PROC_FS
990 /* iterator */
991 static void *disk_seqf_start(struct seq_file *seqf, loff_t *pos)
992 {
993 loff_t skip = *pos;
994 struct class_dev_iter *iter;
995 struct device *dev;
996
997 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
998 if (!iter)
999 return ERR_PTR(-ENOMEM);
1000
1001 seqf->private = iter;
1002 class_dev_iter_init(iter, &block_class, NULL, &disk_type);
1003 do {
1004 dev = class_dev_iter_next(iter);
1005 if (!dev)
1006 return NULL;
1007 } while (skip--);
1008
1009 return dev_to_disk(dev);
1010 }
1011
1012 static void *disk_seqf_next(struct seq_file *seqf, void *v, loff_t *pos)
1013 {
1014 struct device *dev;
1015
1016 (*pos)++;
1017 dev = class_dev_iter_next(seqf->private);
1018 if (dev)
1019 return dev_to_disk(dev);
1020
1021 return NULL;
1022 }
1023
1024 static void disk_seqf_stop(struct seq_file *seqf, void *v)
1025 {
1026 struct class_dev_iter *iter = seqf->private;
1027
1028 /* stop is called even after start failed :-( */
1029 if (iter) {
1030 class_dev_iter_exit(iter);
1031 kfree(iter);
1032 seqf->private = NULL;
1033 }
1034 }
1035
1036 static void *show_partition_start(struct seq_file *seqf, loff_t *pos)
1037 {
1038 void *p;
1039
1040 p = disk_seqf_start(seqf, pos);
1041 if (!IS_ERR_OR_NULL(p) && !*pos)
1042 seq_puts(seqf, "major minor #blocks name\n\n");
1043 return p;
1044 }
1045
1046 static int show_partition(struct seq_file *seqf, void *v)
1047 {
1048 struct gendisk *sgp = v;
1049 struct disk_part_iter piter;
1050 struct block_device *part;
1051 char buf[BDEVNAME_SIZE];
1052
1053 /* Don't show non-partitionable removeable devices or empty devices */
1054 if (!get_capacity(sgp) || (!disk_max_parts(sgp) &&
1055 (sgp->flags & GENHD_FL_REMOVABLE)))
1056 return 0;
1057 if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)
1058 return 0;
1059
1060 /* show the full disk and all non-0 size partitions of it */
1061 disk_part_iter_init(&piter, sgp, DISK_PITER_INCL_PART0);
1062 while ((part = disk_part_iter_next(&piter)))
1063 seq_printf(seqf, "%4d %7d %10llu %s\n",
1064 MAJOR(part->bd_dev), MINOR(part->bd_dev),
1065 bdev_nr_sectors(part) >> 1,
1066 disk_name(sgp, part->bd_partno, buf));
1067 disk_part_iter_exit(&piter);
1068
1069 return 0;
1070 }
1071
1072 static const struct seq_operations partitions_op = {
1073 .start = show_partition_start,
1074 .next = disk_seqf_next,
1075 .stop = disk_seqf_stop,
1076 .show = show_partition
1077 };
1078 #endif
1079
1080 static int __init genhd_device_init(void)
1081 {
1082 int error;
1083
1084 block_class.dev_kobj = sysfs_dev_block_kobj;
1085 error = class_register(&block_class);
1086 if (unlikely(error))
1087 return error;
1088 blk_dev_init();
1089
1090 register_blkdev(BLOCK_EXT_MAJOR, "blkext");
1091
1092 /* create top-level block dir */
1093 if (!sysfs_deprecated)
1094 block_depr = kobject_create_and_add("block", NULL);
1095 return 0;
1096 }
1097
1098 subsys_initcall(genhd_device_init);
1099
1100 static ssize_t disk_range_show(struct device *dev,
1101 struct device_attribute *attr, char *buf)
1102 {
1103 struct gendisk *disk = dev_to_disk(dev);
1104
1105 return sprintf(buf, "%d\n", disk->minors);
1106 }
1107
1108 static ssize_t disk_ext_range_show(struct device *dev,
1109 struct device_attribute *attr, char *buf)
1110 {
1111 struct gendisk *disk = dev_to_disk(dev);
1112
1113 return sprintf(buf, "%d\n", disk_max_parts(disk));
1114 }
1115
1116 static ssize_t disk_removable_show(struct device *dev,
1117 struct device_attribute *attr, char *buf)
1118 {
1119 struct gendisk *disk = dev_to_disk(dev);
1120
1121 return sprintf(buf, "%d\n",
1122 (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
1123 }
1124
1125 static ssize_t disk_hidden_show(struct device *dev,
1126 struct device_attribute *attr, char *buf)
1127 {
1128 struct gendisk *disk = dev_to_disk(dev);
1129
1130 return sprintf(buf, "%d\n",
1131 (disk->flags & GENHD_FL_HIDDEN ? 1 : 0));
1132 }
1133
1134 static ssize_t disk_ro_show(struct device *dev,
1135 struct device_attribute *attr, char *buf)
1136 {
1137 struct gendisk *disk = dev_to_disk(dev);
1138
1139 return sprintf(buf, "%d\n", get_disk_ro(disk) ? 1 : 0);
1140 }
1141
1142 ssize_t part_size_show(struct device *dev,
1143 struct device_attribute *attr, char *buf)
1144 {
1145 return sprintf(buf, "%llu\n", bdev_nr_sectors(dev_to_bdev(dev)));
1146 }
1147
1148 ssize_t part_stat_show(struct device *dev,
1149 struct device_attribute *attr, char *buf)
1150 {
1151 struct block_device *bdev = dev_to_bdev(dev);
1152 struct request_queue *q = bdev->bd_disk->queue;
1153 struct disk_stats stat;
1154 unsigned int inflight;
1155
1156 part_stat_read_all(bdev, &stat);
1157 if (queue_is_mq(q))
1158 inflight = blk_mq_in_flight(q, bdev);
1159 else
1160 inflight = part_in_flight(bdev);
1161
1162 return sprintf(buf,
1163 "%8lu %8lu %8llu %8u "
1164 "%8lu %8lu %8llu %8u "
1165 "%8u %8u %8u "
1166 "%8lu %8lu %8llu %8u "
1167 "%8lu %8u"
1168 "\n",
1169 stat.ios[STAT_READ],
1170 stat.merges[STAT_READ],
1171 (unsigned long long)stat.sectors[STAT_READ],
1172 (unsigned int)div_u64(stat.nsecs[STAT_READ], NSEC_PER_MSEC),
1173 stat.ios[STAT_WRITE],
1174 stat.merges[STAT_WRITE],
1175 (unsigned long long)stat.sectors[STAT_WRITE],
1176 (unsigned int)div_u64(stat.nsecs[STAT_WRITE], NSEC_PER_MSEC),
1177 inflight,
1178 jiffies_to_msecs(stat.io_ticks),
1179 (unsigned int)div_u64(stat.nsecs[STAT_READ] +
1180 stat.nsecs[STAT_WRITE] +
1181 stat.nsecs[STAT_DISCARD] +
1182 stat.nsecs[STAT_FLUSH],
1183 NSEC_PER_MSEC),
1184 stat.ios[STAT_DISCARD],
1185 stat.merges[STAT_DISCARD],
1186 (unsigned long long)stat.sectors[STAT_DISCARD],
1187 (unsigned int)div_u64(stat.nsecs[STAT_DISCARD], NSEC_PER_MSEC),
1188 stat.ios[STAT_FLUSH],
1189 (unsigned int)div_u64(stat.nsecs[STAT_FLUSH], NSEC_PER_MSEC));
1190 }
1191
1192 ssize_t part_inflight_show(struct device *dev, struct device_attribute *attr,
1193 char *buf)
1194 {
1195 struct block_device *bdev = dev_to_bdev(dev);
1196 struct request_queue *q = bdev->bd_disk->queue;
1197 unsigned int inflight[2];
1198
1199 if (queue_is_mq(q))
1200 blk_mq_in_flight_rw(q, bdev, inflight);
1201 else
1202 part_in_flight_rw(bdev, inflight);
1203
1204 return sprintf(buf, "%8u %8u\n", inflight[0], inflight[1]);
1205 }
1206
1207 static ssize_t disk_capability_show(struct device *dev,
1208 struct device_attribute *attr, char *buf)
1209 {
1210 struct gendisk *disk = dev_to_disk(dev);
1211
1212 return sprintf(buf, "%x\n", disk->flags);
1213 }
1214
1215 static ssize_t disk_alignment_offset_show(struct device *dev,
1216 struct device_attribute *attr,
1217 char *buf)
1218 {
1219 struct gendisk *disk = dev_to_disk(dev);
1220
1221 return sprintf(buf, "%d\n", queue_alignment_offset(disk->queue));
1222 }
1223
1224 static ssize_t disk_discard_alignment_show(struct device *dev,
1225 struct device_attribute *attr,
1226 char *buf)
1227 {
1228 struct gendisk *disk = dev_to_disk(dev);
1229
1230 return sprintf(buf, "%d\n", queue_discard_alignment(disk->queue));
1231 }
1232
1233 static DEVICE_ATTR(range, 0444, disk_range_show, NULL);
1234 static DEVICE_ATTR(ext_range, 0444, disk_ext_range_show, NULL);
1235 static DEVICE_ATTR(removable, 0444, disk_removable_show, NULL);
1236 static DEVICE_ATTR(hidden, 0444, disk_hidden_show, NULL);
1237 static DEVICE_ATTR(ro, 0444, disk_ro_show, NULL);
1238 static DEVICE_ATTR(size, 0444, part_size_show, NULL);
1239 static DEVICE_ATTR(alignment_offset, 0444, disk_alignment_offset_show, NULL);
1240 static DEVICE_ATTR(discard_alignment, 0444, disk_discard_alignment_show, NULL);
1241 static DEVICE_ATTR(capability, 0444, disk_capability_show, NULL);
1242 static DEVICE_ATTR(stat, 0444, part_stat_show, NULL);
1243 static DEVICE_ATTR(inflight, 0444, part_inflight_show, NULL);
1244 static DEVICE_ATTR(badblocks, 0644, disk_badblocks_show, disk_badblocks_store);
1245
1246 #ifdef CONFIG_FAIL_MAKE_REQUEST
1247 ssize_t part_fail_show(struct device *dev,
1248 struct device_attribute *attr, char *buf)
1249 {
1250 return sprintf(buf, "%d\n", dev_to_bdev(dev)->bd_make_it_fail);
1251 }
1252
1253 ssize_t part_fail_store(struct device *dev,
1254 struct device_attribute *attr,
1255 const char *buf, size_t count)
1256 {
1257 int i;
1258
1259 if (count > 0 && sscanf(buf, "%d", &i) > 0)
1260 dev_to_bdev(dev)->bd_make_it_fail = i;
1261
1262 return count;
1263 }
1264
1265 static struct device_attribute dev_attr_fail =
1266 __ATTR(make-it-fail, 0644, part_fail_show, part_fail_store);
1267 #endif /* CONFIG_FAIL_MAKE_REQUEST */
1268
1269 #ifdef CONFIG_FAIL_IO_TIMEOUT
1270 static struct device_attribute dev_attr_fail_timeout =
1271 __ATTR(io-timeout-fail, 0644, part_timeout_show, part_timeout_store);
1272 #endif
1273
1274 static struct attribute *disk_attrs[] = {
1275 &dev_attr_range.attr,
1276 &dev_attr_ext_range.attr,
1277 &dev_attr_removable.attr,
1278 &dev_attr_hidden.attr,
1279 &dev_attr_ro.attr,
1280 &dev_attr_size.attr,
1281 &dev_attr_alignment_offset.attr,
1282 &dev_attr_discard_alignment.attr,
1283 &dev_attr_capability.attr,
1284 &dev_attr_stat.attr,
1285 &dev_attr_inflight.attr,
1286 &dev_attr_badblocks.attr,
1287 #ifdef CONFIG_FAIL_MAKE_REQUEST
1288 &dev_attr_fail.attr,
1289 #endif
1290 #ifdef CONFIG_FAIL_IO_TIMEOUT
1291 &dev_attr_fail_timeout.attr,
1292 #endif
1293 NULL
1294 };
1295
1296 static umode_t disk_visible(struct kobject *kobj, struct attribute *a, int n)
1297 {
1298 struct device *dev = container_of(kobj, typeof(*dev), kobj);
1299 struct gendisk *disk = dev_to_disk(dev);
1300
1301 if (a == &dev_attr_badblocks.attr && !disk->bb)
1302 return 0;
1303 return a->mode;
1304 }
1305
1306 static struct attribute_group disk_attr_group = {
1307 .attrs = disk_attrs,
1308 .is_visible = disk_visible,
1309 };
1310
1311 static const struct attribute_group *disk_attr_groups[] = {
1312 &disk_attr_group,
1313 NULL
1314 };
1315
1316 /**
1317 * disk_replace_part_tbl - replace disk->part_tbl in RCU-safe way
1318 * @disk: disk to replace part_tbl for
1319 * @new_ptbl: new part_tbl to install
1320 *
1321 * Replace disk->part_tbl with @new_ptbl in RCU-safe way. The
1322 * original ptbl is freed using RCU callback.
1323 *
1324 * LOCKING:
1325 * Matching bd_mutex locked or the caller is the only user of @disk.
1326 */
1327 static void disk_replace_part_tbl(struct gendisk *disk,
1328 struct disk_part_tbl *new_ptbl)
1329 {
1330 struct disk_part_tbl *old_ptbl =
1331 rcu_dereference_protected(disk->part_tbl, 1);
1332
1333 rcu_assign_pointer(disk->part_tbl, new_ptbl);
1334
1335 if (old_ptbl) {
1336 rcu_assign_pointer(old_ptbl->last_lookup, NULL);
1337 kfree_rcu(old_ptbl, rcu_head);
1338 }
1339 }
1340
1341 /**
1342 * disk_expand_part_tbl - expand disk->part_tbl
1343 * @disk: disk to expand part_tbl for
1344 * @partno: expand such that this partno can fit in
1345 *
1346 * Expand disk->part_tbl such that @partno can fit in. disk->part_tbl
1347 * uses RCU to allow unlocked dereferencing for stats and other stuff.
1348 *
1349 * LOCKING:
1350 * Matching bd_mutex locked or the caller is the only user of @disk.
1351 * Might sleep.
1352 *
1353 * RETURNS:
1354 * 0 on success, -errno on failure.
1355 */
1356 int disk_expand_part_tbl(struct gendisk *disk, int partno)
1357 {
1358 struct disk_part_tbl *old_ptbl =
1359 rcu_dereference_protected(disk->part_tbl, 1);
1360 struct disk_part_tbl *new_ptbl;
1361 int len = old_ptbl ? old_ptbl->len : 0;
1362 int i, target;
1363
1364 /*
1365 * check for int overflow, since we can get here from blkpg_ioctl()
1366 * with a user passed 'partno'.
1367 */
1368 target = partno + 1;
1369 if (target < 0)
1370 return -EINVAL;
1371
1372 /* disk_max_parts() is zero during initialization, ignore if so */
1373 if (disk_max_parts(disk) && target > disk_max_parts(disk))
1374 return -EINVAL;
1375
1376 if (target <= len)
1377 return 0;
1378
1379 new_ptbl = kzalloc_node(struct_size(new_ptbl, part, target), GFP_KERNEL,
1380 disk->node_id);
1381 if (!new_ptbl)
1382 return -ENOMEM;
1383
1384 new_ptbl->len = target;
1385
1386 for (i = 0; i < len; i++)
1387 rcu_assign_pointer(new_ptbl->part[i], old_ptbl->part[i]);
1388
1389 disk_replace_part_tbl(disk, new_ptbl);
1390 return 0;
1391 }
1392
1393 /**
1394 * disk_release - releases all allocated resources of the gendisk
1395 * @dev: the device representing this disk
1396 *
1397 * This function releases all allocated resources of the gendisk.
1398 *
1399 * Drivers which used __device_add_disk() have a gendisk with a request_queue
1400 * assigned. Since the request_queue sits on top of the gendisk for these
1401 * drivers we also call blk_put_queue() for them, and we expect the
1402 * request_queue refcount to reach 0 at this point, and so the request_queue
1403 * will also be freed prior to the disk.
1404 *
1405 * Context: can sleep
1406 */
1407 static void disk_release(struct device *dev)
1408 {
1409 struct gendisk *disk = dev_to_disk(dev);
1410
1411 might_sleep();
1412
1413 blk_free_devt(dev->devt);
1414 disk_release_events(disk);
1415 kfree(disk->random);
1416 disk_replace_part_tbl(disk, NULL);
1417 bdput(disk->part0);
1418 if (disk->queue)
1419 blk_put_queue(disk->queue);
1420 kfree(disk);
1421 }
1422 struct class block_class = {
1423 .name = "block",
1424 };
1425
1426 static char *block_devnode(struct device *dev, umode_t *mode,
1427 kuid_t *uid, kgid_t *gid)
1428 {
1429 struct gendisk *disk = dev_to_disk(dev);
1430
1431 if (disk->fops->devnode)
1432 return disk->fops->devnode(disk, mode);
1433 return NULL;
1434 }
1435
1436 const struct device_type disk_type = {
1437 .name = "disk",
1438 .groups = disk_attr_groups,
1439 .release = disk_release,
1440 .devnode = block_devnode,
1441 };
1442
1443 #ifdef CONFIG_PROC_FS
1444 /*
1445 * aggregate disk stat collector. Uses the same stats that the sysfs
1446 * entries do, above, but makes them available through one seq_file.
1447 *
1448 * The output looks suspiciously like /proc/partitions with a bunch of
1449 * extra fields.
1450 */
1451 static int diskstats_show(struct seq_file *seqf, void *v)
1452 {
1453 struct gendisk *gp = v;
1454 struct disk_part_iter piter;
1455 struct block_device *hd;
1456 char buf[BDEVNAME_SIZE];
1457 unsigned int inflight;
1458 struct disk_stats stat;
1459
1460 /*
1461 if (&disk_to_dev(gp)->kobj.entry == block_class.devices.next)
1462 seq_puts(seqf, "major minor name"
1463 " rio rmerge rsect ruse wio wmerge "
1464 "wsect wuse running use aveq"
1465 "\n\n");
1466 */
1467
1468 disk_part_iter_init(&piter, gp, DISK_PITER_INCL_EMPTY_PART0);
1469 while ((hd = disk_part_iter_next(&piter))) {
1470 part_stat_read_all(hd, &stat);
1471 if (queue_is_mq(gp->queue))
1472 inflight = blk_mq_in_flight(gp->queue, hd);
1473 else
1474 inflight = part_in_flight(hd);
1475
1476 seq_printf(seqf, "%4d %7d %s "
1477 "%lu %lu %lu %u "
1478 "%lu %lu %lu %u "
1479 "%u %u %u "
1480 "%lu %lu %lu %u "
1481 "%lu %u"
1482 "\n",
1483 MAJOR(hd->bd_dev), MINOR(hd->bd_dev),
1484 disk_name(gp, hd->bd_partno, buf),
1485 stat.ios[STAT_READ],
1486 stat.merges[STAT_READ],
1487 stat.sectors[STAT_READ],
1488 (unsigned int)div_u64(stat.nsecs[STAT_READ],
1489 NSEC_PER_MSEC),
1490 stat.ios[STAT_WRITE],
1491 stat.merges[STAT_WRITE],
1492 stat.sectors[STAT_WRITE],
1493 (unsigned int)div_u64(stat.nsecs[STAT_WRITE],
1494 NSEC_PER_MSEC),
1495 inflight,
1496 jiffies_to_msecs(stat.io_ticks),
1497 (unsigned int)div_u64(stat.nsecs[STAT_READ] +
1498 stat.nsecs[STAT_WRITE] +
1499 stat.nsecs[STAT_DISCARD] +
1500 stat.nsecs[STAT_FLUSH],
1501 NSEC_PER_MSEC),
1502 stat.ios[STAT_DISCARD],
1503 stat.merges[STAT_DISCARD],
1504 stat.sectors[STAT_DISCARD],
1505 (unsigned int)div_u64(stat.nsecs[STAT_DISCARD],
1506 NSEC_PER_MSEC),
1507 stat.ios[STAT_FLUSH],
1508 (unsigned int)div_u64(stat.nsecs[STAT_FLUSH],
1509 NSEC_PER_MSEC)
1510 );
1511 }
1512 disk_part_iter_exit(&piter);
1513
1514 return 0;
1515 }
1516
1517 static const struct seq_operations diskstats_op = {
1518 .start = disk_seqf_start,
1519 .next = disk_seqf_next,
1520 .stop = disk_seqf_stop,
1521 .show = diskstats_show
1522 };
1523
1524 static int __init proc_genhd_init(void)
1525 {
1526 proc_create_seq("diskstats", 0, NULL, &diskstats_op);
1527 proc_create_seq("partitions", 0, NULL, &partitions_op);
1528 return 0;
1529 }
1530 module_init(proc_genhd_init);
1531 #endif /* CONFIG_PROC_FS */
1532
1533 dev_t blk_lookup_devt(const char *name, int partno)
1534 {
1535 dev_t devt = MKDEV(0, 0);
1536 struct class_dev_iter iter;
1537 struct device *dev;
1538
1539 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
1540 while ((dev = class_dev_iter_next(&iter))) {
1541 struct gendisk *disk = dev_to_disk(dev);
1542 struct block_device *part;
1543
1544 if (strcmp(dev_name(dev), name))
1545 continue;
1546
1547 if (partno < disk->minors) {
1548 /* We need to return the right devno, even
1549 * if the partition doesn't exist yet.
1550 */
1551 devt = MKDEV(MAJOR(dev->devt),
1552 MINOR(dev->devt) + partno);
1553 break;
1554 }
1555 part = bdget_disk(disk, partno);
1556 if (part) {
1557 devt = part->bd_dev;
1558 bdput(part);
1559 break;
1560 }
1561 }
1562 class_dev_iter_exit(&iter);
1563 return devt;
1564 }
1565
1566 struct gendisk *__alloc_disk_node(int minors, int node_id)
1567 {
1568 struct gendisk *disk;
1569 struct disk_part_tbl *ptbl;
1570
1571 if (minors > DISK_MAX_PARTS) {
1572 printk(KERN_ERR
1573 "block: can't allocate more than %d partitions\n",
1574 DISK_MAX_PARTS);
1575 minors = DISK_MAX_PARTS;
1576 }
1577
1578 disk = kzalloc_node(sizeof(struct gendisk), GFP_KERNEL, node_id);
1579 if (!disk)
1580 return NULL;
1581
1582 disk->part0 = bdev_alloc(disk, 0);
1583 if (!disk->part0)
1584 goto out_free_disk;
1585
1586 disk->node_id = node_id;
1587 if (disk_expand_part_tbl(disk, 0))
1588 goto out_bdput;
1589
1590 ptbl = rcu_dereference_protected(disk->part_tbl, 1);
1591 rcu_assign_pointer(ptbl->part[0], disk->part0);
1592
1593 disk->minors = minors;
1594 rand_initialize_disk(disk);
1595 disk_to_dev(disk)->class = &block_class;
1596 disk_to_dev(disk)->type = &disk_type;
1597 device_initialize(disk_to_dev(disk));
1598 return disk;
1599
1600 out_bdput:
1601 bdput(disk->part0);
1602 out_free_disk:
1603 kfree(disk);
1604 return NULL;
1605 }
1606 EXPORT_SYMBOL(__alloc_disk_node);
1607
1608 /**
1609 * put_disk - decrements the gendisk refcount
1610 * @disk: the struct gendisk to decrement the refcount for
1611 *
1612 * This decrements the refcount for the struct gendisk. When this reaches 0
1613 * we'll have disk_release() called.
1614 *
1615 * Context: Any context, but the last reference must not be dropped from
1616 * atomic context.
1617 */
1618 void put_disk(struct gendisk *disk)
1619 {
1620 if (disk)
1621 put_device(disk_to_dev(disk));
1622 }
1623 EXPORT_SYMBOL(put_disk);
1624
1625 static void set_disk_ro_uevent(struct gendisk *gd, int ro)
1626 {
1627 char event[] = "DISK_RO=1";
1628 char *envp[] = { event, NULL };
1629
1630 if (!ro)
1631 event[8] = '0';
1632 kobject_uevent_env(&disk_to_dev(gd)->kobj, KOBJ_CHANGE, envp);
1633 }
1634
1635 void set_disk_ro(struct gendisk *disk, int flag)
1636 {
1637 struct disk_part_iter piter;
1638 struct block_device *part;
1639
1640 if (disk->part0->bd_read_only != flag) {
1641 set_disk_ro_uevent(disk, flag);
1642 disk->part0->bd_read_only = flag;
1643 }
1644
1645 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_EMPTY);
1646 while ((part = disk_part_iter_next(&piter)))
1647 part->bd_read_only = flag;
1648 disk_part_iter_exit(&piter);
1649 }
1650
1651 EXPORT_SYMBOL(set_disk_ro);
1652
1653 int bdev_read_only(struct block_device *bdev)
1654 {
1655 if (!bdev)
1656 return 0;
1657 return bdev->bd_read_only;
1658 }
1659
1660 EXPORT_SYMBOL(bdev_read_only);
1661
1662 /*
1663 * Disk events - monitor disk events like media change and eject request.
1664 */
1665 struct disk_events {
1666 struct list_head node; /* all disk_event's */
1667 struct gendisk *disk; /* the associated disk */
1668 spinlock_t lock;
1669
1670 struct mutex block_mutex; /* protects blocking */
1671 int block; /* event blocking depth */
1672 unsigned int pending; /* events already sent out */
1673 unsigned int clearing; /* events being cleared */
1674
1675 long poll_msecs; /* interval, -1 for default */
1676 struct delayed_work dwork;
1677 };
1678
1679 static const char *disk_events_strs[] = {
1680 [ilog2(DISK_EVENT_MEDIA_CHANGE)] = "media_change",
1681 [ilog2(DISK_EVENT_EJECT_REQUEST)] = "eject_request",
1682 };
1683
1684 static char *disk_uevents[] = {
1685 [ilog2(DISK_EVENT_MEDIA_CHANGE)] = "DISK_MEDIA_CHANGE=1",
1686 [ilog2(DISK_EVENT_EJECT_REQUEST)] = "DISK_EJECT_REQUEST=1",
1687 };
1688
1689 /* list of all disk_events */
1690 static DEFINE_MUTEX(disk_events_mutex);
1691 static LIST_HEAD(disk_events);
1692
1693 /* disable in-kernel polling by default */
1694 static unsigned long disk_events_dfl_poll_msecs;
1695
1696 static unsigned long disk_events_poll_jiffies(struct gendisk *disk)
1697 {
1698 struct disk_events *ev = disk->ev;
1699 long intv_msecs = 0;
1700
1701 /*
1702 * If device-specific poll interval is set, always use it. If
1703 * the default is being used, poll if the POLL flag is set.
1704 */
1705 if (ev->poll_msecs >= 0)
1706 intv_msecs = ev->poll_msecs;
1707 else if (disk->event_flags & DISK_EVENT_FLAG_POLL)
1708 intv_msecs = disk_events_dfl_poll_msecs;
1709
1710 return msecs_to_jiffies(intv_msecs);
1711 }
1712
1713 /**
1714 * disk_block_events - block and flush disk event checking
1715 * @disk: disk to block events for
1716 *
1717 * On return from this function, it is guaranteed that event checking
1718 * isn't in progress and won't happen until unblocked by
1719 * disk_unblock_events(). Events blocking is counted and the actual
1720 * unblocking happens after the matching number of unblocks are done.
1721 *
1722 * Note that this intentionally does not block event checking from
1723 * disk_clear_events().
1724 *
1725 * CONTEXT:
1726 * Might sleep.
1727 */
1728 void disk_block_events(struct gendisk *disk)
1729 {
1730 struct disk_events *ev = disk->ev;
1731 unsigned long flags;
1732 bool cancel;
1733
1734 if (!ev)
1735 return;
1736
1737 /*
1738 * Outer mutex ensures that the first blocker completes canceling
1739 * the event work before further blockers are allowed to finish.
1740 */
1741 mutex_lock(&ev->block_mutex);
1742
1743 spin_lock_irqsave(&ev->lock, flags);
1744 cancel = !ev->block++;
1745 spin_unlock_irqrestore(&ev->lock, flags);
1746
1747 if (cancel)
1748 cancel_delayed_work_sync(&disk->ev->dwork);
1749
1750 mutex_unlock(&ev->block_mutex);
1751 }
1752
1753 static void __disk_unblock_events(struct gendisk *disk, bool check_now)
1754 {
1755 struct disk_events *ev = disk->ev;
1756 unsigned long intv;
1757 unsigned long flags;
1758
1759 spin_lock_irqsave(&ev->lock, flags);
1760
1761 if (WARN_ON_ONCE(ev->block <= 0))
1762 goto out_unlock;
1763
1764 if (--ev->block)
1765 goto out_unlock;
1766
1767 intv = disk_events_poll_jiffies(disk);
1768 if (check_now)
1769 queue_delayed_work(system_freezable_power_efficient_wq,
1770 &ev->dwork, 0);
1771 else if (intv)
1772 queue_delayed_work(system_freezable_power_efficient_wq,
1773 &ev->dwork, intv);
1774 out_unlock:
1775 spin_unlock_irqrestore(&ev->lock, flags);
1776 }
1777
1778 /**
1779 * disk_unblock_events - unblock disk event checking
1780 * @disk: disk to unblock events for
1781 *
1782 * Undo disk_block_events(). When the block count reaches zero, it
1783 * starts events polling if configured.
1784 *
1785 * CONTEXT:
1786 * Don't care. Safe to call from irq context.
1787 */
1788 void disk_unblock_events(struct gendisk *disk)
1789 {
1790 if (disk->ev)
1791 __disk_unblock_events(disk, false);
1792 }
1793
1794 /**
1795 * disk_flush_events - schedule immediate event checking and flushing
1796 * @disk: disk to check and flush events for
1797 * @mask: events to flush
1798 *
1799 * Schedule immediate event checking on @disk if not blocked. Events in
1800 * @mask are scheduled to be cleared from the driver. Note that this
1801 * doesn't clear the events from @disk->ev.
1802 *
1803 * CONTEXT:
1804 * If @mask is non-zero must be called with bdev->bd_mutex held.
1805 */
1806 void disk_flush_events(struct gendisk *disk, unsigned int mask)
1807 {
1808 struct disk_events *ev = disk->ev;
1809
1810 if (!ev)
1811 return;
1812
1813 spin_lock_irq(&ev->lock);
1814 ev->clearing |= mask;
1815 if (!ev->block)
1816 mod_delayed_work(system_freezable_power_efficient_wq,
1817 &ev->dwork, 0);
1818 spin_unlock_irq(&ev->lock);
1819 }
1820
1821 /**
1822 * disk_clear_events - synchronously check, clear and return pending events
1823 * @disk: disk to fetch and clear events from
1824 * @mask: mask of events to be fetched and cleared
1825 *
1826 * Disk events are synchronously checked and pending events in @mask
1827 * are cleared and returned. This ignores the block count.
1828 *
1829 * CONTEXT:
1830 * Might sleep.
1831 */
1832 static unsigned int disk_clear_events(struct gendisk *disk, unsigned int mask)
1833 {
1834 struct disk_events *ev = disk->ev;
1835 unsigned int pending;
1836 unsigned int clearing = mask;
1837
1838 if (!ev)
1839 return 0;
1840
1841 disk_block_events(disk);
1842
1843 /*
1844 * store the union of mask and ev->clearing on the stack so that the
1845 * race with disk_flush_events does not cause ambiguity (ev->clearing
1846 * can still be modified even if events are blocked).
1847 */
1848 spin_lock_irq(&ev->lock);
1849 clearing |= ev->clearing;
1850 ev->clearing = 0;
1851 spin_unlock_irq(&ev->lock);
1852
1853 disk_check_events(ev, &clearing);
1854 /*
1855 * if ev->clearing is not 0, the disk_flush_events got called in the
1856 * middle of this function, so we want to run the workfn without delay.
1857 */
1858 __disk_unblock_events(disk, ev->clearing ? true : false);
1859
1860 /* then, fetch and clear pending events */
1861 spin_lock_irq(&ev->lock);
1862 pending = ev->pending & mask;
1863 ev->pending &= ~mask;
1864 spin_unlock_irq(&ev->lock);
1865 WARN_ON_ONCE(clearing & mask);
1866
1867 return pending;
1868 }
1869
1870 /**
1871 * bdev_check_media_change - check if a removable media has been changed
1872 * @bdev: block device to check
1873 *
1874 * Check whether a removable media has been changed, and attempt to free all
1875 * dentries and inodes and invalidates all block device page cache entries in
1876 * that case.
1877 *
1878 * Returns %true if the block device changed, or %false if not.
1879 */
1880 bool bdev_check_media_change(struct block_device *bdev)
1881 {
1882 unsigned int events;
1883
1884 events = disk_clear_events(bdev->bd_disk, DISK_EVENT_MEDIA_CHANGE |
1885 DISK_EVENT_EJECT_REQUEST);
1886 if (!(events & DISK_EVENT_MEDIA_CHANGE))
1887 return false;
1888
1889 if (__invalidate_device(bdev, true))
1890 pr_warn("VFS: busy inodes on changed media %s\n",
1891 bdev->bd_disk->disk_name);
1892 set_bit(GD_NEED_PART_SCAN, &bdev->bd_disk->state);
1893 return true;
1894 }
1895 EXPORT_SYMBOL(bdev_check_media_change);
1896
1897 /*
1898 * Separate this part out so that a different pointer for clearing_ptr can be
1899 * passed in for disk_clear_events.
1900 */
1901 static void disk_events_workfn(struct work_struct *work)
1902 {
1903 struct delayed_work *dwork = to_delayed_work(work);
1904 struct disk_events *ev = container_of(dwork, struct disk_events, dwork);
1905
1906 disk_check_events(ev, &ev->clearing);
1907 }
1908
1909 static void disk_check_events(struct disk_events *ev,
1910 unsigned int *clearing_ptr)
1911 {
1912 struct gendisk *disk = ev->disk;
1913 char *envp[ARRAY_SIZE(disk_uevents) + 1] = { };
1914 unsigned int clearing = *clearing_ptr;
1915 unsigned int events;
1916 unsigned long intv;
1917 int nr_events = 0, i;
1918
1919 /* check events */
1920 events = disk->fops->check_events(disk, clearing);
1921
1922 /* accumulate pending events and schedule next poll if necessary */
1923 spin_lock_irq(&ev->lock);
1924
1925 events &= ~ev->pending;
1926 ev->pending |= events;
1927 *clearing_ptr &= ~clearing;
1928
1929 intv = disk_events_poll_jiffies(disk);
1930 if (!ev->block && intv)
1931 queue_delayed_work(system_freezable_power_efficient_wq,
1932 &ev->dwork, intv);
1933
1934 spin_unlock_irq(&ev->lock);
1935
1936 /*
1937 * Tell userland about new events. Only the events listed in
1938 * @disk->events are reported, and only if DISK_EVENT_FLAG_UEVENT
1939 * is set. Otherwise, events are processed internally but never
1940 * get reported to userland.
1941 */
1942 for (i = 0; i < ARRAY_SIZE(disk_uevents); i++)
1943 if ((events & disk->events & (1 << i)) &&
1944 (disk->event_flags & DISK_EVENT_FLAG_UEVENT))
1945 envp[nr_events++] = disk_uevents[i];
1946
1947 if (nr_events)
1948 kobject_uevent_env(&disk_to_dev(disk)->kobj, KOBJ_CHANGE, envp);
1949 }
1950
1951 /*
1952 * A disk events enabled device has the following sysfs nodes under
1953 * its /sys/block/X/ directory.
1954 *
1955 * events : list of all supported events
1956 * events_async : list of events which can be detected w/o polling
1957 * (always empty, only for backwards compatibility)
1958 * events_poll_msecs : polling interval, 0: disable, -1: system default
1959 */
1960 static ssize_t __disk_events_show(unsigned int events, char *buf)
1961 {
1962 const char *delim = "";
1963 ssize_t pos = 0;
1964 int i;
1965
1966 for (i = 0; i < ARRAY_SIZE(disk_events_strs); i++)
1967 if (events & (1 << i)) {
1968 pos += sprintf(buf + pos, "%s%s",
1969 delim, disk_events_strs[i]);
1970 delim = " ";
1971 }
1972 if (pos)
1973 pos += sprintf(buf + pos, "\n");
1974 return pos;
1975 }
1976
1977 static ssize_t disk_events_show(struct device *dev,
1978 struct device_attribute *attr, char *buf)
1979 {
1980 struct gendisk *disk = dev_to_disk(dev);
1981
1982 if (!(disk->event_flags & DISK_EVENT_FLAG_UEVENT))
1983 return 0;
1984
1985 return __disk_events_show(disk->events, buf);
1986 }
1987
1988 static ssize_t disk_events_async_show(struct device *dev,
1989 struct device_attribute *attr, char *buf)
1990 {
1991 return 0;
1992 }
1993
1994 static ssize_t disk_events_poll_msecs_show(struct device *dev,
1995 struct device_attribute *attr,
1996 char *buf)
1997 {
1998 struct gendisk *disk = dev_to_disk(dev);
1999
2000 if (!disk->ev)
2001 return sprintf(buf, "-1\n");
2002
2003 return sprintf(buf, "%ld\n", disk->ev->poll_msecs);
2004 }
2005
2006 static ssize_t disk_events_poll_msecs_store(struct device *dev,
2007 struct device_attribute *attr,
2008 const char *buf, size_t count)
2009 {
2010 struct gendisk *disk = dev_to_disk(dev);
2011 long intv;
2012
2013 if (!count || !sscanf(buf, "%ld", &intv))
2014 return -EINVAL;
2015
2016 if (intv < 0 && intv != -1)
2017 return -EINVAL;
2018
2019 if (!disk->ev)
2020 return -ENODEV;
2021
2022 disk_block_events(disk);
2023 disk->ev->poll_msecs = intv;
2024 __disk_unblock_events(disk, true);
2025
2026 return count;
2027 }
2028
2029 static const DEVICE_ATTR(events, 0444, disk_events_show, NULL);
2030 static const DEVICE_ATTR(events_async, 0444, disk_events_async_show, NULL);
2031 static const DEVICE_ATTR(events_poll_msecs, 0644,
2032 disk_events_poll_msecs_show,
2033 disk_events_poll_msecs_store);
2034
2035 static const struct attribute *disk_events_attrs[] = {
2036 &dev_attr_events.attr,
2037 &dev_attr_events_async.attr,
2038 &dev_attr_events_poll_msecs.attr,
2039 NULL,
2040 };
2041
2042 /*
2043 * The default polling interval can be specified by the kernel
2044 * parameter block.events_dfl_poll_msecs which defaults to 0
2045 * (disable). This can also be modified runtime by writing to
2046 * /sys/module/block/parameters/events_dfl_poll_msecs.
2047 */
2048 static int disk_events_set_dfl_poll_msecs(const char *val,
2049 const struct kernel_param *kp)
2050 {
2051 struct disk_events *ev;
2052 int ret;
2053
2054 ret = param_set_ulong(val, kp);
2055 if (ret < 0)
2056 return ret;
2057
2058 mutex_lock(&disk_events_mutex);
2059
2060 list_for_each_entry(ev, &disk_events, node)
2061 disk_flush_events(ev->disk, 0);
2062
2063 mutex_unlock(&disk_events_mutex);
2064
2065 return 0;
2066 }
2067
2068 static const struct kernel_param_ops disk_events_dfl_poll_msecs_param_ops = {
2069 .set = disk_events_set_dfl_poll_msecs,
2070 .get = param_get_ulong,
2071 };
2072
2073 #undef MODULE_PARAM_PREFIX
2074 #define MODULE_PARAM_PREFIX "block."
2075
2076 module_param_cb(events_dfl_poll_msecs, &disk_events_dfl_poll_msecs_param_ops,
2077 &disk_events_dfl_poll_msecs, 0644);
2078
2079 /*
2080 * disk_{alloc|add|del|release}_events - initialize and destroy disk_events.
2081 */
2082 static void disk_alloc_events(struct gendisk *disk)
2083 {
2084 struct disk_events *ev;
2085
2086 if (!disk->fops->check_events || !disk->events)
2087 return;
2088
2089 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
2090 if (!ev) {
2091 pr_warn("%s: failed to initialize events\n", disk->disk_name);
2092 return;
2093 }
2094
2095 INIT_LIST_HEAD(&ev->node);
2096 ev->disk = disk;
2097 spin_lock_init(&ev->lock);
2098 mutex_init(&ev->block_mutex);
2099 ev->block = 1;
2100 ev->poll_msecs = -1;
2101 INIT_DELAYED_WORK(&ev->dwork, disk_events_workfn);
2102
2103 disk->ev = ev;
2104 }
2105
2106 static void disk_add_events(struct gendisk *disk)
2107 {
2108 /* FIXME: error handling */
2109 if (sysfs_create_files(&disk_to_dev(disk)->kobj, disk_events_attrs) < 0)
2110 pr_warn("%s: failed to create sysfs files for events\n",
2111 disk->disk_name);
2112
2113 if (!disk->ev)
2114 return;
2115
2116 mutex_lock(&disk_events_mutex);
2117 list_add_tail(&disk->ev->node, &disk_events);
2118 mutex_unlock(&disk_events_mutex);
2119
2120 /*
2121 * Block count is initialized to 1 and the following initial
2122 * unblock kicks it into action.
2123 */
2124 __disk_unblock_events(disk, true);
2125 }
2126
2127 static void disk_del_events(struct gendisk *disk)
2128 {
2129 if (disk->ev) {
2130 disk_block_events(disk);
2131
2132 mutex_lock(&disk_events_mutex);
2133 list_del_init(&disk->ev->node);
2134 mutex_unlock(&disk_events_mutex);
2135 }
2136
2137 sysfs_remove_files(&disk_to_dev(disk)->kobj, disk_events_attrs);
2138 }
2139
2140 static void disk_release_events(struct gendisk *disk)
2141 {
2142 /* the block count should be 1 from disk_del_events() */
2143 WARN_ON_ONCE(disk->ev && disk->ev->block != 1);
2144 kfree(disk->ev);
2145 }