xref: /openbmc/linux/block/genhd.c (revision e8377f49)
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 /*
33  * Unique, monotonically increasing sequential number associated with block
34  * devices instances (i.e. incremented each time a device is attached).
35  * Associating uevents with block devices in userspace is difficult and racy:
36  * the uevent netlink socket is lossy, and on slow and overloaded systems has
37  * a very high latency.
38  * Block devices do not have exclusive owners in userspace, any process can set
39  * one up (e.g. loop devices). Moreover, device names can be reused (e.g. loop0
40  * can be reused again and again).
41  * A userspace process setting up a block device and watching for its events
42  * cannot thus reliably tell whether an event relates to the device it just set
43  * up or another earlier instance with the same name.
44  * This sequential number allows userspace processes to solve this problem, and
45  * uniquely associate an uevent to the lifetime to a device.
46  */
47 static atomic64_t diskseq;
48 
49 /* for extended dynamic devt allocation, currently only one major is used */
50 #define NR_EXT_DEVT		(1 << MINORBITS)
51 static DEFINE_IDA(ext_devt_ida);
52 
53 void set_capacity(struct gendisk *disk, sector_t sectors)
54 {
55 	struct block_device *bdev = disk->part0;
56 
57 	spin_lock(&bdev->bd_size_lock);
58 	i_size_write(bdev->bd_inode, (loff_t)sectors << SECTOR_SHIFT);
59 	spin_unlock(&bdev->bd_size_lock);
60 }
61 EXPORT_SYMBOL(set_capacity);
62 
63 /*
64  * Set disk capacity and notify if the size is not currently zero and will not
65  * be set to zero.  Returns true if a uevent was sent, otherwise false.
66  */
67 bool set_capacity_and_notify(struct gendisk *disk, sector_t size)
68 {
69 	sector_t capacity = get_capacity(disk);
70 	char *envp[] = { "RESIZE=1", NULL };
71 
72 	set_capacity(disk, size);
73 
74 	/*
75 	 * Only print a message and send a uevent if the gendisk is user visible
76 	 * and alive.  This avoids spamming the log and udev when setting the
77 	 * initial capacity during probing.
78 	 */
79 	if (size == capacity ||
80 	    !disk_live(disk) ||
81 	    (disk->flags & GENHD_FL_HIDDEN))
82 		return false;
83 
84 	pr_info("%s: detected capacity change from %lld to %lld\n",
85 		disk->disk_name, capacity, size);
86 
87 	/*
88 	 * Historically we did not send a uevent for changes to/from an empty
89 	 * device.
90 	 */
91 	if (!capacity || !size)
92 		return false;
93 	kobject_uevent_env(&disk_to_dev(disk)->kobj, KOBJ_CHANGE, envp);
94 	return true;
95 }
96 EXPORT_SYMBOL_GPL(set_capacity_and_notify);
97 
98 /*
99  * Format the device name of the indicated block device into the supplied buffer
100  * and return a pointer to that same buffer for convenience.
101  *
102  * Note: do not use this in new code, use the %pg specifier to sprintf and
103  * printk insted.
104  */
105 const char *bdevname(struct block_device *bdev, char *buf)
106 {
107 	struct gendisk *hd = bdev->bd_disk;
108 	int partno = bdev->bd_partno;
109 
110 	if (!partno)
111 		snprintf(buf, BDEVNAME_SIZE, "%s", hd->disk_name);
112 	else if (isdigit(hd->disk_name[strlen(hd->disk_name)-1]))
113 		snprintf(buf, BDEVNAME_SIZE, "%sp%d", hd->disk_name, partno);
114 	else
115 		snprintf(buf, BDEVNAME_SIZE, "%s%d", hd->disk_name, partno);
116 
117 	return buf;
118 }
119 EXPORT_SYMBOL(bdevname);
120 
121 static void part_stat_read_all(struct block_device *part,
122 		struct disk_stats *stat)
123 {
124 	int cpu;
125 
126 	memset(stat, 0, sizeof(struct disk_stats));
127 	for_each_possible_cpu(cpu) {
128 		struct disk_stats *ptr = per_cpu_ptr(part->bd_stats, cpu);
129 		int group;
130 
131 		for (group = 0; group < NR_STAT_GROUPS; group++) {
132 			stat->nsecs[group] += ptr->nsecs[group];
133 			stat->sectors[group] += ptr->sectors[group];
134 			stat->ios[group] += ptr->ios[group];
135 			stat->merges[group] += ptr->merges[group];
136 		}
137 
138 		stat->io_ticks += ptr->io_ticks;
139 	}
140 }
141 
142 static unsigned int part_in_flight(struct block_device *part)
143 {
144 	unsigned int inflight = 0;
145 	int cpu;
146 
147 	for_each_possible_cpu(cpu) {
148 		inflight += part_stat_local_read_cpu(part, in_flight[0], cpu) +
149 			    part_stat_local_read_cpu(part, in_flight[1], cpu);
150 	}
151 	if ((int)inflight < 0)
152 		inflight = 0;
153 
154 	return inflight;
155 }
156 
157 static void part_in_flight_rw(struct block_device *part,
158 		unsigned int inflight[2])
159 {
160 	int cpu;
161 
162 	inflight[0] = 0;
163 	inflight[1] = 0;
164 	for_each_possible_cpu(cpu) {
165 		inflight[0] += part_stat_local_read_cpu(part, in_flight[0], cpu);
166 		inflight[1] += part_stat_local_read_cpu(part, in_flight[1], cpu);
167 	}
168 	if ((int)inflight[0] < 0)
169 		inflight[0] = 0;
170 	if ((int)inflight[1] < 0)
171 		inflight[1] = 0;
172 }
173 
174 /*
175  * Can be deleted altogether. Later.
176  *
177  */
178 #define BLKDEV_MAJOR_HASH_SIZE 255
179 static struct blk_major_name {
180 	struct blk_major_name *next;
181 	int major;
182 	char name[16];
183 	void (*probe)(dev_t devt);
184 } *major_names[BLKDEV_MAJOR_HASH_SIZE];
185 static DEFINE_MUTEX(major_names_lock);
186 static DEFINE_SPINLOCK(major_names_spinlock);
187 
188 /* index in the above - for now: assume no multimajor ranges */
189 static inline int major_to_index(unsigned major)
190 {
191 	return major % BLKDEV_MAJOR_HASH_SIZE;
192 }
193 
194 #ifdef CONFIG_PROC_FS
195 void blkdev_show(struct seq_file *seqf, off_t offset)
196 {
197 	struct blk_major_name *dp;
198 
199 	spin_lock(&major_names_spinlock);
200 	for (dp = major_names[major_to_index(offset)]; dp; dp = dp->next)
201 		if (dp->major == offset)
202 			seq_printf(seqf, "%3d %s\n", dp->major, dp->name);
203 	spin_unlock(&major_names_spinlock);
204 }
205 #endif /* CONFIG_PROC_FS */
206 
207 /**
208  * __register_blkdev - register a new block device
209  *
210  * @major: the requested major device number [1..BLKDEV_MAJOR_MAX-1]. If
211  *         @major = 0, try to allocate any unused major number.
212  * @name: the name of the new block device as a zero terminated string
213  * @probe: allback that is called on access to any minor number of @major
214  *
215  * The @name must be unique within the system.
216  *
217  * The return value depends on the @major input parameter:
218  *
219  *  - if a major device number was requested in range [1..BLKDEV_MAJOR_MAX-1]
220  *    then the function returns zero on success, or a negative error code
221  *  - if any unused major number was requested with @major = 0 parameter
222  *    then the return value is the allocated major number in range
223  *    [1..BLKDEV_MAJOR_MAX-1] or a negative error code otherwise
224  *
225  * See Documentation/admin-guide/devices.txt for the list of allocated
226  * major numbers.
227  *
228  * Use register_blkdev instead for any new code.
229  */
230 int __register_blkdev(unsigned int major, const char *name,
231 		void (*probe)(dev_t devt))
232 {
233 	struct blk_major_name **n, *p;
234 	int index, ret = 0;
235 
236 	mutex_lock(&major_names_lock);
237 
238 	/* temporary */
239 	if (major == 0) {
240 		for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) {
241 			if (major_names[index] == NULL)
242 				break;
243 		}
244 
245 		if (index == 0) {
246 			printk("%s: failed to get major for %s\n",
247 			       __func__, name);
248 			ret = -EBUSY;
249 			goto out;
250 		}
251 		major = index;
252 		ret = major;
253 	}
254 
255 	if (major >= BLKDEV_MAJOR_MAX) {
256 		pr_err("%s: major requested (%u) is greater than the maximum (%u) for %s\n",
257 		       __func__, major, BLKDEV_MAJOR_MAX-1, name);
258 
259 		ret = -EINVAL;
260 		goto out;
261 	}
262 
263 	p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL);
264 	if (p == NULL) {
265 		ret = -ENOMEM;
266 		goto out;
267 	}
268 
269 	p->major = major;
270 	p->probe = probe;
271 	strlcpy(p->name, name, sizeof(p->name));
272 	p->next = NULL;
273 	index = major_to_index(major);
274 
275 	spin_lock(&major_names_spinlock);
276 	for (n = &major_names[index]; *n; n = &(*n)->next) {
277 		if ((*n)->major == major)
278 			break;
279 	}
280 	if (!*n)
281 		*n = p;
282 	else
283 		ret = -EBUSY;
284 	spin_unlock(&major_names_spinlock);
285 
286 	if (ret < 0) {
287 		printk("register_blkdev: cannot get major %u for %s\n",
288 		       major, name);
289 		kfree(p);
290 	}
291 out:
292 	mutex_unlock(&major_names_lock);
293 	return ret;
294 }
295 EXPORT_SYMBOL(__register_blkdev);
296 
297 void unregister_blkdev(unsigned int major, const char *name)
298 {
299 	struct blk_major_name **n;
300 	struct blk_major_name *p = NULL;
301 	int index = major_to_index(major);
302 
303 	mutex_lock(&major_names_lock);
304 	spin_lock(&major_names_spinlock);
305 	for (n = &major_names[index]; *n; n = &(*n)->next)
306 		if ((*n)->major == major)
307 			break;
308 	if (!*n || strcmp((*n)->name, name)) {
309 		WARN_ON(1);
310 	} else {
311 		p = *n;
312 		*n = p->next;
313 	}
314 	spin_unlock(&major_names_spinlock);
315 	mutex_unlock(&major_names_lock);
316 	kfree(p);
317 }
318 
319 EXPORT_SYMBOL(unregister_blkdev);
320 
321 int blk_alloc_ext_minor(void)
322 {
323 	int idx;
324 
325 	idx = ida_alloc_range(&ext_devt_ida, 0, NR_EXT_DEVT, GFP_KERNEL);
326 	if (idx == -ENOSPC)
327 		return -EBUSY;
328 	return idx;
329 }
330 
331 void blk_free_ext_minor(unsigned int minor)
332 {
333 	ida_free(&ext_devt_ida, minor);
334 }
335 
336 static char *bdevt_str(dev_t devt, char *buf)
337 {
338 	if (MAJOR(devt) <= 0xff && MINOR(devt) <= 0xff) {
339 		char tbuf[BDEVT_SIZE];
340 		snprintf(tbuf, BDEVT_SIZE, "%02x%02x", MAJOR(devt), MINOR(devt));
341 		snprintf(buf, BDEVT_SIZE, "%-9s", tbuf);
342 	} else
343 		snprintf(buf, BDEVT_SIZE, "%03x:%05x", MAJOR(devt), MINOR(devt));
344 
345 	return buf;
346 }
347 
348 void disk_uevent(struct gendisk *disk, enum kobject_action action)
349 {
350 	struct block_device *part;
351 	unsigned long idx;
352 
353 	rcu_read_lock();
354 	xa_for_each(&disk->part_tbl, idx, part) {
355 		if (bdev_is_partition(part) && !bdev_nr_sectors(part))
356 			continue;
357 		if (!kobject_get_unless_zero(&part->bd_device.kobj))
358 			continue;
359 
360 		rcu_read_unlock();
361 		kobject_uevent(bdev_kobj(part), action);
362 		put_device(&part->bd_device);
363 		rcu_read_lock();
364 	}
365 	rcu_read_unlock();
366 }
367 EXPORT_SYMBOL_GPL(disk_uevent);
368 
369 static void disk_scan_partitions(struct gendisk *disk)
370 {
371 	struct block_device *bdev;
372 
373 	if (!get_capacity(disk) || !disk_part_scan_enabled(disk))
374 		return;
375 
376 	set_bit(GD_NEED_PART_SCAN, &disk->state);
377 	bdev = blkdev_get_by_dev(disk_devt(disk), FMODE_READ, NULL);
378 	if (!IS_ERR(bdev))
379 		blkdev_put(bdev, FMODE_READ);
380 }
381 
382 /**
383  * device_add_disk - add disk information to kernel list
384  * @parent: parent device for the disk
385  * @disk: per-device partitioning information
386  * @groups: Additional per-device sysfs groups
387  *
388  * This function registers the partitioning information in @disk
389  * with the kernel.
390  */
391 int device_add_disk(struct device *parent, struct gendisk *disk,
392 		     const struct attribute_group **groups)
393 
394 {
395 	struct device *ddev = disk_to_dev(disk);
396 	int ret;
397 
398 	/*
399 	 * The disk queue should now be all set with enough information about
400 	 * the device for the elevator code to pick an adequate default
401 	 * elevator if one is needed, that is, for devices requesting queue
402 	 * registration.
403 	 */
404 	elevator_init_mq(disk->queue);
405 
406 	/*
407 	 * If the driver provides an explicit major number it also must provide
408 	 * the number of minors numbers supported, and those will be used to
409 	 * setup the gendisk.
410 	 * Otherwise just allocate the device numbers for both the whole device
411 	 * and all partitions from the extended dev_t space.
412 	 */
413 	if (disk->major) {
414 		if (WARN_ON(!disk->minors))
415 			return -EINVAL;
416 
417 		if (disk->minors > DISK_MAX_PARTS) {
418 			pr_err("block: can't allocate more than %d partitions\n",
419 				DISK_MAX_PARTS);
420 			disk->minors = DISK_MAX_PARTS;
421 		}
422 	} else {
423 		if (WARN_ON(disk->minors))
424 			return -EINVAL;
425 
426 		ret = blk_alloc_ext_minor();
427 		if (ret < 0)
428 			return ret;
429 		disk->major = BLOCK_EXT_MAJOR;
430 		disk->first_minor = ret;
431 		disk->flags |= GENHD_FL_EXT_DEVT;
432 	}
433 
434 	ret = disk_alloc_events(disk);
435 	if (ret)
436 		goto out_free_ext_minor;
437 
438 	/* delay uevents, until we scanned partition table */
439 	dev_set_uevent_suppress(ddev, 1);
440 
441 	ddev->parent = parent;
442 	ddev->groups = groups;
443 	dev_set_name(ddev, "%s", disk->disk_name);
444 	if (!(disk->flags & GENHD_FL_HIDDEN))
445 		ddev->devt = MKDEV(disk->major, disk->first_minor);
446 	ret = device_add(ddev);
447 	if (ret)
448 		goto out_disk_release_events;
449 	if (!sysfs_deprecated) {
450 		ret = sysfs_create_link(block_depr, &ddev->kobj,
451 					kobject_name(&ddev->kobj));
452 		if (ret)
453 			goto out_device_del;
454 	}
455 
456 	/*
457 	 * avoid probable deadlock caused by allocating memory with
458 	 * GFP_KERNEL in runtime_resume callback of its all ancestor
459 	 * devices
460 	 */
461 	pm_runtime_set_memalloc_noio(ddev, true);
462 
463 	ret = blk_integrity_add(disk);
464 	if (ret)
465 		goto out_del_block_link;
466 
467 	disk->part0->bd_holder_dir =
468 		kobject_create_and_add("holders", &ddev->kobj);
469 	if (!disk->part0->bd_holder_dir)
470 		goto out_del_integrity;
471 	disk->slave_dir = kobject_create_and_add("slaves", &ddev->kobj);
472 	if (!disk->slave_dir)
473 		goto out_put_holder_dir;
474 
475 	ret = bd_register_pending_holders(disk);
476 	if (ret < 0)
477 		goto out_put_slave_dir;
478 
479 	ret = blk_register_queue(disk);
480 	if (ret)
481 		goto out_put_slave_dir;
482 
483 	if (disk->flags & GENHD_FL_HIDDEN) {
484 		/*
485 		 * Don't let hidden disks show up in /proc/partitions,
486 		 * and don't bother scanning for partitions either.
487 		 */
488 		disk->flags |= GENHD_FL_SUPPRESS_PARTITION_INFO;
489 		disk->flags |= GENHD_FL_NO_PART_SCAN;
490 	} else {
491 		ret = bdi_register(disk->bdi, "%u:%u",
492 				   disk->major, disk->first_minor);
493 		if (ret)
494 			goto out_unregister_queue;
495 		bdi_set_owner(disk->bdi, ddev);
496 		ret = sysfs_create_link(&ddev->kobj,
497 					&disk->bdi->dev->kobj, "bdi");
498 		if (ret)
499 			goto out_unregister_bdi;
500 
501 		bdev_add(disk->part0, ddev->devt);
502 		disk_scan_partitions(disk);
503 
504 		/*
505 		 * Announce the disk and partitions after all partitions are
506 		 * created. (for hidden disks uevents remain suppressed forever)
507 		 */
508 		dev_set_uevent_suppress(ddev, 0);
509 		disk_uevent(disk, KOBJ_ADD);
510 	}
511 
512 	disk_update_readahead(disk);
513 	disk_add_events(disk);
514 	return 0;
515 
516 out_unregister_bdi:
517 	if (!(disk->flags & GENHD_FL_HIDDEN))
518 		bdi_unregister(disk->bdi);
519 out_unregister_queue:
520 	blk_unregister_queue(disk);
521 out_put_slave_dir:
522 	kobject_put(disk->slave_dir);
523 out_put_holder_dir:
524 	kobject_put(disk->part0->bd_holder_dir);
525 out_del_integrity:
526 	blk_integrity_del(disk);
527 out_del_block_link:
528 	if (!sysfs_deprecated)
529 		sysfs_remove_link(block_depr, dev_name(ddev));
530 out_device_del:
531 	device_del(ddev);
532 out_disk_release_events:
533 	disk_release_events(disk);
534 out_free_ext_minor:
535 	if (disk->major == BLOCK_EXT_MAJOR)
536 		blk_free_ext_minor(disk->first_minor);
537 	return WARN_ON_ONCE(ret); /* keep until all callers handle errors */
538 }
539 EXPORT_SYMBOL(device_add_disk);
540 
541 /**
542  * del_gendisk - remove the gendisk
543  * @disk: the struct gendisk to remove
544  *
545  * Removes the gendisk and all its associated resources. This deletes the
546  * partitions associated with the gendisk, and unregisters the associated
547  * request_queue.
548  *
549  * This is the counter to the respective __device_add_disk() call.
550  *
551  * The final removal of the struct gendisk happens when its refcount reaches 0
552  * with put_disk(), which should be called after del_gendisk(), if
553  * __device_add_disk() was used.
554  *
555  * Drivers exist which depend on the release of the gendisk to be synchronous,
556  * it should not be deferred.
557  *
558  * Context: can sleep
559  */
560 void del_gendisk(struct gendisk *disk)
561 {
562 	might_sleep();
563 
564 	if (WARN_ON_ONCE(!disk_live(disk) && !(disk->flags & GENHD_FL_HIDDEN)))
565 		return;
566 
567 	blk_integrity_del(disk);
568 	disk_del_events(disk);
569 
570 	mutex_lock(&disk->open_mutex);
571 	remove_inode_hash(disk->part0->bd_inode);
572 	blk_drop_partitions(disk);
573 	mutex_unlock(&disk->open_mutex);
574 
575 	fsync_bdev(disk->part0);
576 	__invalidate_device(disk->part0, true);
577 
578 	set_capacity(disk, 0);
579 
580 	if (!(disk->flags & GENHD_FL_HIDDEN)) {
581 		sysfs_remove_link(&disk_to_dev(disk)->kobj, "bdi");
582 
583 		/*
584 		 * Unregister bdi before releasing device numbers (as they can
585 		 * get reused and we'd get clashes in sysfs).
586 		 */
587 		bdi_unregister(disk->bdi);
588 	}
589 
590 	blk_unregister_queue(disk);
591 
592 	kobject_put(disk->part0->bd_holder_dir);
593 	kobject_put(disk->slave_dir);
594 
595 	part_stat_set_all(disk->part0, 0);
596 	disk->part0->bd_stamp = 0;
597 	if (!sysfs_deprecated)
598 		sysfs_remove_link(block_depr, dev_name(disk_to_dev(disk)));
599 	pm_runtime_set_memalloc_noio(disk_to_dev(disk), false);
600 	device_del(disk_to_dev(disk));
601 }
602 EXPORT_SYMBOL(del_gendisk);
603 
604 /* sysfs access to bad-blocks list. */
605 static ssize_t disk_badblocks_show(struct device *dev,
606 					struct device_attribute *attr,
607 					char *page)
608 {
609 	struct gendisk *disk = dev_to_disk(dev);
610 
611 	if (!disk->bb)
612 		return sprintf(page, "\n");
613 
614 	return badblocks_show(disk->bb, page, 0);
615 }
616 
617 static ssize_t disk_badblocks_store(struct device *dev,
618 					struct device_attribute *attr,
619 					const char *page, size_t len)
620 {
621 	struct gendisk *disk = dev_to_disk(dev);
622 
623 	if (!disk->bb)
624 		return -ENXIO;
625 
626 	return badblocks_store(disk->bb, page, len, 0);
627 }
628 
629 void blk_request_module(dev_t devt)
630 {
631 	unsigned int major = MAJOR(devt);
632 	struct blk_major_name **n;
633 
634 	mutex_lock(&major_names_lock);
635 	for (n = &major_names[major_to_index(major)]; *n; n = &(*n)->next) {
636 		if ((*n)->major == major && (*n)->probe) {
637 			(*n)->probe(devt);
638 			mutex_unlock(&major_names_lock);
639 			return;
640 		}
641 	}
642 	mutex_unlock(&major_names_lock);
643 
644 	if (request_module("block-major-%d-%d", MAJOR(devt), MINOR(devt)) > 0)
645 		/* Make old-style 2.4 aliases work */
646 		request_module("block-major-%d", MAJOR(devt));
647 }
648 
649 /*
650  * print a full list of all partitions - intended for places where the root
651  * filesystem can't be mounted and thus to give the victim some idea of what
652  * went wrong
653  */
654 void __init printk_all_partitions(void)
655 {
656 	struct class_dev_iter iter;
657 	struct device *dev;
658 
659 	class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
660 	while ((dev = class_dev_iter_next(&iter))) {
661 		struct gendisk *disk = dev_to_disk(dev);
662 		struct block_device *part;
663 		char devt_buf[BDEVT_SIZE];
664 		unsigned long idx;
665 
666 		/*
667 		 * Don't show empty devices or things that have been
668 		 * suppressed
669 		 */
670 		if (get_capacity(disk) == 0 ||
671 		    (disk->flags & GENHD_FL_SUPPRESS_PARTITION_INFO))
672 			continue;
673 
674 		/*
675 		 * Note, unlike /proc/partitions, I am showing the numbers in
676 		 * hex - the same format as the root= option takes.
677 		 */
678 		rcu_read_lock();
679 		xa_for_each(&disk->part_tbl, idx, part) {
680 			if (!bdev_nr_sectors(part))
681 				continue;
682 			printk("%s%s %10llu %pg %s",
683 			       bdev_is_partition(part) ? "  " : "",
684 			       bdevt_str(part->bd_dev, devt_buf),
685 			       bdev_nr_sectors(part) >> 1, part,
686 			       part->bd_meta_info ?
687 					part->bd_meta_info->uuid : "");
688 			if (bdev_is_partition(part))
689 				printk("\n");
690 			else if (dev->parent && dev->parent->driver)
691 				printk(" driver: %s\n",
692 					dev->parent->driver->name);
693 			else
694 				printk(" (driver?)\n");
695 		}
696 		rcu_read_unlock();
697 	}
698 	class_dev_iter_exit(&iter);
699 }
700 
701 #ifdef CONFIG_PROC_FS
702 /* iterator */
703 static void *disk_seqf_start(struct seq_file *seqf, loff_t *pos)
704 {
705 	loff_t skip = *pos;
706 	struct class_dev_iter *iter;
707 	struct device *dev;
708 
709 	iter = kmalloc(sizeof(*iter), GFP_KERNEL);
710 	if (!iter)
711 		return ERR_PTR(-ENOMEM);
712 
713 	seqf->private = iter;
714 	class_dev_iter_init(iter, &block_class, NULL, &disk_type);
715 	do {
716 		dev = class_dev_iter_next(iter);
717 		if (!dev)
718 			return NULL;
719 	} while (skip--);
720 
721 	return dev_to_disk(dev);
722 }
723 
724 static void *disk_seqf_next(struct seq_file *seqf, void *v, loff_t *pos)
725 {
726 	struct device *dev;
727 
728 	(*pos)++;
729 	dev = class_dev_iter_next(seqf->private);
730 	if (dev)
731 		return dev_to_disk(dev);
732 
733 	return NULL;
734 }
735 
736 static void disk_seqf_stop(struct seq_file *seqf, void *v)
737 {
738 	struct class_dev_iter *iter = seqf->private;
739 
740 	/* stop is called even after start failed :-( */
741 	if (iter) {
742 		class_dev_iter_exit(iter);
743 		kfree(iter);
744 		seqf->private = NULL;
745 	}
746 }
747 
748 static void *show_partition_start(struct seq_file *seqf, loff_t *pos)
749 {
750 	void *p;
751 
752 	p = disk_seqf_start(seqf, pos);
753 	if (!IS_ERR_OR_NULL(p) && !*pos)
754 		seq_puts(seqf, "major minor  #blocks  name\n\n");
755 	return p;
756 }
757 
758 static int show_partition(struct seq_file *seqf, void *v)
759 {
760 	struct gendisk *sgp = v;
761 	struct block_device *part;
762 	unsigned long idx;
763 
764 	/* Don't show non-partitionable removeable devices or empty devices */
765 	if (!get_capacity(sgp) || (!disk_max_parts(sgp) &&
766 				   (sgp->flags & GENHD_FL_REMOVABLE)))
767 		return 0;
768 	if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)
769 		return 0;
770 
771 	rcu_read_lock();
772 	xa_for_each(&sgp->part_tbl, idx, part) {
773 		if (!bdev_nr_sectors(part))
774 			continue;
775 		seq_printf(seqf, "%4d  %7d %10llu %pg\n",
776 			   MAJOR(part->bd_dev), MINOR(part->bd_dev),
777 			   bdev_nr_sectors(part) >> 1, part);
778 	}
779 	rcu_read_unlock();
780 	return 0;
781 }
782 
783 static const struct seq_operations partitions_op = {
784 	.start	= show_partition_start,
785 	.next	= disk_seqf_next,
786 	.stop	= disk_seqf_stop,
787 	.show	= show_partition
788 };
789 #endif
790 
791 static int __init genhd_device_init(void)
792 {
793 	int error;
794 
795 	block_class.dev_kobj = sysfs_dev_block_kobj;
796 	error = class_register(&block_class);
797 	if (unlikely(error))
798 		return error;
799 	blk_dev_init();
800 
801 	register_blkdev(BLOCK_EXT_MAJOR, "blkext");
802 
803 	/* create top-level block dir */
804 	if (!sysfs_deprecated)
805 		block_depr = kobject_create_and_add("block", NULL);
806 	return 0;
807 }
808 
809 subsys_initcall(genhd_device_init);
810 
811 static ssize_t disk_range_show(struct device *dev,
812 			       struct device_attribute *attr, char *buf)
813 {
814 	struct gendisk *disk = dev_to_disk(dev);
815 
816 	return sprintf(buf, "%d\n", disk->minors);
817 }
818 
819 static ssize_t disk_ext_range_show(struct device *dev,
820 				   struct device_attribute *attr, char *buf)
821 {
822 	struct gendisk *disk = dev_to_disk(dev);
823 
824 	return sprintf(buf, "%d\n", disk_max_parts(disk));
825 }
826 
827 static ssize_t disk_removable_show(struct device *dev,
828 				   struct device_attribute *attr, char *buf)
829 {
830 	struct gendisk *disk = dev_to_disk(dev);
831 
832 	return sprintf(buf, "%d\n",
833 		       (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
834 }
835 
836 static ssize_t disk_hidden_show(struct device *dev,
837 				   struct device_attribute *attr, char *buf)
838 {
839 	struct gendisk *disk = dev_to_disk(dev);
840 
841 	return sprintf(buf, "%d\n",
842 		       (disk->flags & GENHD_FL_HIDDEN ? 1 : 0));
843 }
844 
845 static ssize_t disk_ro_show(struct device *dev,
846 				   struct device_attribute *attr, char *buf)
847 {
848 	struct gendisk *disk = dev_to_disk(dev);
849 
850 	return sprintf(buf, "%d\n", get_disk_ro(disk) ? 1 : 0);
851 }
852 
853 ssize_t part_size_show(struct device *dev,
854 		       struct device_attribute *attr, char *buf)
855 {
856 	return sprintf(buf, "%llu\n", bdev_nr_sectors(dev_to_bdev(dev)));
857 }
858 
859 ssize_t part_stat_show(struct device *dev,
860 		       struct device_attribute *attr, char *buf)
861 {
862 	struct block_device *bdev = dev_to_bdev(dev);
863 	struct request_queue *q = bdev->bd_disk->queue;
864 	struct disk_stats stat;
865 	unsigned int inflight;
866 
867 	part_stat_read_all(bdev, &stat);
868 	if (queue_is_mq(q))
869 		inflight = blk_mq_in_flight(q, bdev);
870 	else
871 		inflight = part_in_flight(bdev);
872 
873 	return sprintf(buf,
874 		"%8lu %8lu %8llu %8u "
875 		"%8lu %8lu %8llu %8u "
876 		"%8u %8u %8u "
877 		"%8lu %8lu %8llu %8u "
878 		"%8lu %8u"
879 		"\n",
880 		stat.ios[STAT_READ],
881 		stat.merges[STAT_READ],
882 		(unsigned long long)stat.sectors[STAT_READ],
883 		(unsigned int)div_u64(stat.nsecs[STAT_READ], NSEC_PER_MSEC),
884 		stat.ios[STAT_WRITE],
885 		stat.merges[STAT_WRITE],
886 		(unsigned long long)stat.sectors[STAT_WRITE],
887 		(unsigned int)div_u64(stat.nsecs[STAT_WRITE], NSEC_PER_MSEC),
888 		inflight,
889 		jiffies_to_msecs(stat.io_ticks),
890 		(unsigned int)div_u64(stat.nsecs[STAT_READ] +
891 				      stat.nsecs[STAT_WRITE] +
892 				      stat.nsecs[STAT_DISCARD] +
893 				      stat.nsecs[STAT_FLUSH],
894 						NSEC_PER_MSEC),
895 		stat.ios[STAT_DISCARD],
896 		stat.merges[STAT_DISCARD],
897 		(unsigned long long)stat.sectors[STAT_DISCARD],
898 		(unsigned int)div_u64(stat.nsecs[STAT_DISCARD], NSEC_PER_MSEC),
899 		stat.ios[STAT_FLUSH],
900 		(unsigned int)div_u64(stat.nsecs[STAT_FLUSH], NSEC_PER_MSEC));
901 }
902 
903 ssize_t part_inflight_show(struct device *dev, struct device_attribute *attr,
904 			   char *buf)
905 {
906 	struct block_device *bdev = dev_to_bdev(dev);
907 	struct request_queue *q = bdev->bd_disk->queue;
908 	unsigned int inflight[2];
909 
910 	if (queue_is_mq(q))
911 		blk_mq_in_flight_rw(q, bdev, inflight);
912 	else
913 		part_in_flight_rw(bdev, inflight);
914 
915 	return sprintf(buf, "%8u %8u\n", inflight[0], inflight[1]);
916 }
917 
918 static ssize_t disk_capability_show(struct device *dev,
919 				    struct device_attribute *attr, char *buf)
920 {
921 	struct gendisk *disk = dev_to_disk(dev);
922 
923 	return sprintf(buf, "%x\n", disk->flags);
924 }
925 
926 static ssize_t disk_alignment_offset_show(struct device *dev,
927 					  struct device_attribute *attr,
928 					  char *buf)
929 {
930 	struct gendisk *disk = dev_to_disk(dev);
931 
932 	return sprintf(buf, "%d\n", queue_alignment_offset(disk->queue));
933 }
934 
935 static ssize_t disk_discard_alignment_show(struct device *dev,
936 					   struct device_attribute *attr,
937 					   char *buf)
938 {
939 	struct gendisk *disk = dev_to_disk(dev);
940 
941 	return sprintf(buf, "%d\n", queue_discard_alignment(disk->queue));
942 }
943 
944 static ssize_t diskseq_show(struct device *dev,
945 			    struct device_attribute *attr, char *buf)
946 {
947 	struct gendisk *disk = dev_to_disk(dev);
948 
949 	return sprintf(buf, "%llu\n", disk->diskseq);
950 }
951 
952 static DEVICE_ATTR(range, 0444, disk_range_show, NULL);
953 static DEVICE_ATTR(ext_range, 0444, disk_ext_range_show, NULL);
954 static DEVICE_ATTR(removable, 0444, disk_removable_show, NULL);
955 static DEVICE_ATTR(hidden, 0444, disk_hidden_show, NULL);
956 static DEVICE_ATTR(ro, 0444, disk_ro_show, NULL);
957 static DEVICE_ATTR(size, 0444, part_size_show, NULL);
958 static DEVICE_ATTR(alignment_offset, 0444, disk_alignment_offset_show, NULL);
959 static DEVICE_ATTR(discard_alignment, 0444, disk_discard_alignment_show, NULL);
960 static DEVICE_ATTR(capability, 0444, disk_capability_show, NULL);
961 static DEVICE_ATTR(stat, 0444, part_stat_show, NULL);
962 static DEVICE_ATTR(inflight, 0444, part_inflight_show, NULL);
963 static DEVICE_ATTR(badblocks, 0644, disk_badblocks_show, disk_badblocks_store);
964 static DEVICE_ATTR(diskseq, 0444, diskseq_show, NULL);
965 
966 #ifdef CONFIG_FAIL_MAKE_REQUEST
967 ssize_t part_fail_show(struct device *dev,
968 		       struct device_attribute *attr, char *buf)
969 {
970 	return sprintf(buf, "%d\n", dev_to_bdev(dev)->bd_make_it_fail);
971 }
972 
973 ssize_t part_fail_store(struct device *dev,
974 			struct device_attribute *attr,
975 			const char *buf, size_t count)
976 {
977 	int i;
978 
979 	if (count > 0 && sscanf(buf, "%d", &i) > 0)
980 		dev_to_bdev(dev)->bd_make_it_fail = i;
981 
982 	return count;
983 }
984 
985 static struct device_attribute dev_attr_fail =
986 	__ATTR(make-it-fail, 0644, part_fail_show, part_fail_store);
987 #endif /* CONFIG_FAIL_MAKE_REQUEST */
988 
989 #ifdef CONFIG_FAIL_IO_TIMEOUT
990 static struct device_attribute dev_attr_fail_timeout =
991 	__ATTR(io-timeout-fail, 0644, part_timeout_show, part_timeout_store);
992 #endif
993 
994 static struct attribute *disk_attrs[] = {
995 	&dev_attr_range.attr,
996 	&dev_attr_ext_range.attr,
997 	&dev_attr_removable.attr,
998 	&dev_attr_hidden.attr,
999 	&dev_attr_ro.attr,
1000 	&dev_attr_size.attr,
1001 	&dev_attr_alignment_offset.attr,
1002 	&dev_attr_discard_alignment.attr,
1003 	&dev_attr_capability.attr,
1004 	&dev_attr_stat.attr,
1005 	&dev_attr_inflight.attr,
1006 	&dev_attr_badblocks.attr,
1007 	&dev_attr_events.attr,
1008 	&dev_attr_events_async.attr,
1009 	&dev_attr_events_poll_msecs.attr,
1010 	&dev_attr_diskseq.attr,
1011 #ifdef CONFIG_FAIL_MAKE_REQUEST
1012 	&dev_attr_fail.attr,
1013 #endif
1014 #ifdef CONFIG_FAIL_IO_TIMEOUT
1015 	&dev_attr_fail_timeout.attr,
1016 #endif
1017 	NULL
1018 };
1019 
1020 static umode_t disk_visible(struct kobject *kobj, struct attribute *a, int n)
1021 {
1022 	struct device *dev = container_of(kobj, typeof(*dev), kobj);
1023 	struct gendisk *disk = dev_to_disk(dev);
1024 
1025 	if (a == &dev_attr_badblocks.attr && !disk->bb)
1026 		return 0;
1027 	return a->mode;
1028 }
1029 
1030 static struct attribute_group disk_attr_group = {
1031 	.attrs = disk_attrs,
1032 	.is_visible = disk_visible,
1033 };
1034 
1035 static const struct attribute_group *disk_attr_groups[] = {
1036 	&disk_attr_group,
1037 	NULL
1038 };
1039 
1040 /**
1041  * disk_release - releases all allocated resources of the gendisk
1042  * @dev: the device representing this disk
1043  *
1044  * This function releases all allocated resources of the gendisk.
1045  *
1046  * Drivers which used __device_add_disk() have a gendisk with a request_queue
1047  * assigned. Since the request_queue sits on top of the gendisk for these
1048  * drivers we also call blk_put_queue() for them, and we expect the
1049  * request_queue refcount to reach 0 at this point, and so the request_queue
1050  * will also be freed prior to the disk.
1051  *
1052  * Context: can sleep
1053  */
1054 static void disk_release(struct device *dev)
1055 {
1056 	struct gendisk *disk = dev_to_disk(dev);
1057 
1058 	might_sleep();
1059 
1060 	disk_release_events(disk);
1061 	kfree(disk->random);
1062 	xa_destroy(&disk->part_tbl);
1063 	disk->queue->disk = NULL;
1064 	blk_put_queue(disk->queue);
1065 	iput(disk->part0->bd_inode);	/* frees the disk */
1066 }
1067 
1068 static int block_uevent(struct device *dev, struct kobj_uevent_env *env)
1069 {
1070 	struct gendisk *disk = dev_to_disk(dev);
1071 
1072 	return add_uevent_var(env, "DISKSEQ=%llu", disk->diskseq);
1073 }
1074 
1075 struct class block_class = {
1076 	.name		= "block",
1077 	.dev_uevent	= block_uevent,
1078 };
1079 
1080 static char *block_devnode(struct device *dev, umode_t *mode,
1081 			   kuid_t *uid, kgid_t *gid)
1082 {
1083 	struct gendisk *disk = dev_to_disk(dev);
1084 
1085 	if (disk->fops->devnode)
1086 		return disk->fops->devnode(disk, mode);
1087 	return NULL;
1088 }
1089 
1090 const struct device_type disk_type = {
1091 	.name		= "disk",
1092 	.groups		= disk_attr_groups,
1093 	.release	= disk_release,
1094 	.devnode	= block_devnode,
1095 };
1096 
1097 #ifdef CONFIG_PROC_FS
1098 /*
1099  * aggregate disk stat collector.  Uses the same stats that the sysfs
1100  * entries do, above, but makes them available through one seq_file.
1101  *
1102  * The output looks suspiciously like /proc/partitions with a bunch of
1103  * extra fields.
1104  */
1105 static int diskstats_show(struct seq_file *seqf, void *v)
1106 {
1107 	struct gendisk *gp = v;
1108 	struct block_device *hd;
1109 	unsigned int inflight;
1110 	struct disk_stats stat;
1111 	unsigned long idx;
1112 
1113 	/*
1114 	if (&disk_to_dev(gp)->kobj.entry == block_class.devices.next)
1115 		seq_puts(seqf,	"major minor name"
1116 				"     rio rmerge rsect ruse wio wmerge "
1117 				"wsect wuse running use aveq"
1118 				"\n\n");
1119 	*/
1120 
1121 	rcu_read_lock();
1122 	xa_for_each(&gp->part_tbl, idx, hd) {
1123 		if (bdev_is_partition(hd) && !bdev_nr_sectors(hd))
1124 			continue;
1125 		part_stat_read_all(hd, &stat);
1126 		if (queue_is_mq(gp->queue))
1127 			inflight = blk_mq_in_flight(gp->queue, hd);
1128 		else
1129 			inflight = part_in_flight(hd);
1130 
1131 		seq_printf(seqf, "%4d %7d %pg "
1132 			   "%lu %lu %lu %u "
1133 			   "%lu %lu %lu %u "
1134 			   "%u %u %u "
1135 			   "%lu %lu %lu %u "
1136 			   "%lu %u"
1137 			   "\n",
1138 			   MAJOR(hd->bd_dev), MINOR(hd->bd_dev), hd,
1139 			   stat.ios[STAT_READ],
1140 			   stat.merges[STAT_READ],
1141 			   stat.sectors[STAT_READ],
1142 			   (unsigned int)div_u64(stat.nsecs[STAT_READ],
1143 							NSEC_PER_MSEC),
1144 			   stat.ios[STAT_WRITE],
1145 			   stat.merges[STAT_WRITE],
1146 			   stat.sectors[STAT_WRITE],
1147 			   (unsigned int)div_u64(stat.nsecs[STAT_WRITE],
1148 							NSEC_PER_MSEC),
1149 			   inflight,
1150 			   jiffies_to_msecs(stat.io_ticks),
1151 			   (unsigned int)div_u64(stat.nsecs[STAT_READ] +
1152 						 stat.nsecs[STAT_WRITE] +
1153 						 stat.nsecs[STAT_DISCARD] +
1154 						 stat.nsecs[STAT_FLUSH],
1155 							NSEC_PER_MSEC),
1156 			   stat.ios[STAT_DISCARD],
1157 			   stat.merges[STAT_DISCARD],
1158 			   stat.sectors[STAT_DISCARD],
1159 			   (unsigned int)div_u64(stat.nsecs[STAT_DISCARD],
1160 						 NSEC_PER_MSEC),
1161 			   stat.ios[STAT_FLUSH],
1162 			   (unsigned int)div_u64(stat.nsecs[STAT_FLUSH],
1163 						 NSEC_PER_MSEC)
1164 			);
1165 	}
1166 	rcu_read_unlock();
1167 
1168 	return 0;
1169 }
1170 
1171 static const struct seq_operations diskstats_op = {
1172 	.start	= disk_seqf_start,
1173 	.next	= disk_seqf_next,
1174 	.stop	= disk_seqf_stop,
1175 	.show	= diskstats_show
1176 };
1177 
1178 static int __init proc_genhd_init(void)
1179 {
1180 	proc_create_seq("diskstats", 0, NULL, &diskstats_op);
1181 	proc_create_seq("partitions", 0, NULL, &partitions_op);
1182 	return 0;
1183 }
1184 module_init(proc_genhd_init);
1185 #endif /* CONFIG_PROC_FS */
1186 
1187 dev_t part_devt(struct gendisk *disk, u8 partno)
1188 {
1189 	struct block_device *part;
1190 	dev_t devt = 0;
1191 
1192 	rcu_read_lock();
1193 	part = xa_load(&disk->part_tbl, partno);
1194 	if (part)
1195 		devt = part->bd_dev;
1196 	rcu_read_unlock();
1197 
1198 	return devt;
1199 }
1200 
1201 dev_t blk_lookup_devt(const char *name, int partno)
1202 {
1203 	dev_t devt = MKDEV(0, 0);
1204 	struct class_dev_iter iter;
1205 	struct device *dev;
1206 
1207 	class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
1208 	while ((dev = class_dev_iter_next(&iter))) {
1209 		struct gendisk *disk = dev_to_disk(dev);
1210 
1211 		if (strcmp(dev_name(dev), name))
1212 			continue;
1213 
1214 		if (partno < disk->minors) {
1215 			/* We need to return the right devno, even
1216 			 * if the partition doesn't exist yet.
1217 			 */
1218 			devt = MKDEV(MAJOR(dev->devt),
1219 				     MINOR(dev->devt) + partno);
1220 		} else {
1221 			devt = part_devt(disk, partno);
1222 			if (devt)
1223 				break;
1224 		}
1225 	}
1226 	class_dev_iter_exit(&iter);
1227 	return devt;
1228 }
1229 
1230 struct gendisk *__alloc_disk_node(struct request_queue *q, int node_id,
1231 		struct lock_class_key *lkclass)
1232 {
1233 	struct gendisk *disk;
1234 
1235 	if (!blk_get_queue(q))
1236 		return NULL;
1237 
1238 	disk = kzalloc_node(sizeof(struct gendisk), GFP_KERNEL, node_id);
1239 	if (!disk)
1240 		goto out_put_queue;
1241 
1242 	disk->bdi = bdi_alloc(node_id);
1243 	if (!disk->bdi)
1244 		goto out_free_disk;
1245 
1246 	disk->part0 = bdev_alloc(disk, 0);
1247 	if (!disk->part0)
1248 		goto out_free_bdi;
1249 
1250 	disk->node_id = node_id;
1251 	mutex_init(&disk->open_mutex);
1252 	xa_init(&disk->part_tbl);
1253 	if (xa_insert(&disk->part_tbl, 0, disk->part0, GFP_KERNEL))
1254 		goto out_destroy_part_tbl;
1255 
1256 	rand_initialize_disk(disk);
1257 	disk_to_dev(disk)->class = &block_class;
1258 	disk_to_dev(disk)->type = &disk_type;
1259 	device_initialize(disk_to_dev(disk));
1260 	inc_diskseq(disk);
1261 	disk->queue = q;
1262 	q->disk = disk;
1263 	lockdep_init_map(&disk->lockdep_map, "(bio completion)", lkclass, 0);
1264 #ifdef CONFIG_BLOCK_HOLDER_DEPRECATED
1265 	INIT_LIST_HEAD(&disk->slave_bdevs);
1266 #endif
1267 	return disk;
1268 
1269 out_destroy_part_tbl:
1270 	xa_destroy(&disk->part_tbl);
1271 	iput(disk->part0->bd_inode);
1272 out_free_bdi:
1273 	bdi_put(disk->bdi);
1274 out_free_disk:
1275 	kfree(disk);
1276 out_put_queue:
1277 	blk_put_queue(q);
1278 	return NULL;
1279 }
1280 EXPORT_SYMBOL(__alloc_disk_node);
1281 
1282 struct gendisk *__blk_alloc_disk(int node, struct lock_class_key *lkclass)
1283 {
1284 	struct request_queue *q;
1285 	struct gendisk *disk;
1286 
1287 	q = blk_alloc_queue(node);
1288 	if (!q)
1289 		return NULL;
1290 
1291 	disk = __alloc_disk_node(q, node, lkclass);
1292 	if (!disk) {
1293 		blk_cleanup_queue(q);
1294 		return NULL;
1295 	}
1296 	return disk;
1297 }
1298 EXPORT_SYMBOL(__blk_alloc_disk);
1299 
1300 /**
1301  * put_disk - decrements the gendisk refcount
1302  * @disk: the struct gendisk to decrement the refcount for
1303  *
1304  * This decrements the refcount for the struct gendisk. When this reaches 0
1305  * we'll have disk_release() called.
1306  *
1307  * Context: Any context, but the last reference must not be dropped from
1308  *          atomic context.
1309  */
1310 void put_disk(struct gendisk *disk)
1311 {
1312 	if (disk)
1313 		put_device(disk_to_dev(disk));
1314 }
1315 EXPORT_SYMBOL(put_disk);
1316 
1317 /**
1318  * blk_cleanup_disk - shutdown a gendisk allocated by blk_alloc_disk
1319  * @disk: gendisk to shutdown
1320  *
1321  * Mark the queue hanging off @disk DYING, drain all pending requests, then mark
1322  * the queue DEAD, destroy and put it and the gendisk structure.
1323  *
1324  * Context: can sleep
1325  */
1326 void blk_cleanup_disk(struct gendisk *disk)
1327 {
1328 	blk_cleanup_queue(disk->queue);
1329 	put_disk(disk);
1330 }
1331 EXPORT_SYMBOL(blk_cleanup_disk);
1332 
1333 static void set_disk_ro_uevent(struct gendisk *gd, int ro)
1334 {
1335 	char event[] = "DISK_RO=1";
1336 	char *envp[] = { event, NULL };
1337 
1338 	if (!ro)
1339 		event[8] = '0';
1340 	kobject_uevent_env(&disk_to_dev(gd)->kobj, KOBJ_CHANGE, envp);
1341 }
1342 
1343 /**
1344  * set_disk_ro - set a gendisk read-only
1345  * @disk:	gendisk to operate on
1346  * @read_only:	%true to set the disk read-only, %false set the disk read/write
1347  *
1348  * This function is used to indicate whether a given disk device should have its
1349  * read-only flag set. set_disk_ro() is typically used by device drivers to
1350  * indicate whether the underlying physical device is write-protected.
1351  */
1352 void set_disk_ro(struct gendisk *disk, bool read_only)
1353 {
1354 	if (read_only) {
1355 		if (test_and_set_bit(GD_READ_ONLY, &disk->state))
1356 			return;
1357 	} else {
1358 		if (!test_and_clear_bit(GD_READ_ONLY, &disk->state))
1359 			return;
1360 	}
1361 	set_disk_ro_uevent(disk, read_only);
1362 }
1363 EXPORT_SYMBOL(set_disk_ro);
1364 
1365 int bdev_read_only(struct block_device *bdev)
1366 {
1367 	return bdev->bd_read_only || get_disk_ro(bdev->bd_disk);
1368 }
1369 EXPORT_SYMBOL(bdev_read_only);
1370 
1371 void inc_diskseq(struct gendisk *disk)
1372 {
1373 	disk->diskseq = atomic64_inc_return(&diskseq);
1374 }
1375