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