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