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