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