mmc: don't set GENHD_FL_SUPPRESS_PARTITION_INFO
[linux-2.6-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
e16e506c
CH
379 if (!disk_part_scan_enabled(disk))
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;
0d1feb72 441 disk->flags |= GENHD_FL_EXT_DEVT;
3e1a7ff8 442 }
7c3f828b 443
83cbce95
LC
444 ret = disk_alloc_events(disk);
445 if (ret)
446 goto out_free_ext_minor;
9f53d2fe 447
52b85909
CH
448 /* delay uevents, until we scanned partition table */
449 dev_set_uevent_suppress(ddev, 1);
450
451 ddev->parent = parent;
452 ddev->groups = groups;
453 dev_set_name(ddev, "%s", disk->disk_name);
8235b5c1
CH
454 if (!(disk->flags & GENHD_FL_HIDDEN))
455 ddev->devt = MKDEV(disk->major, disk->first_minor);
83cbce95
LC
456 ret = device_add(ddev);
457 if (ret)
458 goto out_disk_release_events;
52b85909
CH
459 if (!sysfs_deprecated) {
460 ret = sysfs_create_link(block_depr, &ddev->kobj,
461 kobject_name(&ddev->kobj));
83cbce95
LC
462 if (ret)
463 goto out_device_del;
52b85909
CH
464 }
465
466 /*
467 * avoid probable deadlock caused by allocating memory with
468 * GFP_KERNEL in runtime_resume callback of its all ancestor
469 * devices
470 */
471 pm_runtime_set_memalloc_noio(ddev, true);
472
83cbce95
LC
473 ret = blk_integrity_add(disk);
474 if (ret)
475 goto out_del_block_link;
bab53f6b 476
52b85909
CH
477 disk->part0->bd_holder_dir =
478 kobject_create_and_add("holders", &ddev->kobj);
fe7d064f
LC
479 if (!disk->part0->bd_holder_dir) {
480 ret = -ENOMEM;
83cbce95 481 goto out_del_integrity;
fe7d064f 482 }
52b85909 483 disk->slave_dir = kobject_create_and_add("slaves", &ddev->kobj);
fe7d064f
LC
484 if (!disk->slave_dir) {
485 ret = -ENOMEM;
83cbce95 486 goto out_put_holder_dir;
fe7d064f 487 }
52b85909 488
83cbce95
LC
489 ret = bd_register_pending_holders(disk);
490 if (ret < 0)
491 goto out_put_slave_dir;
52b85909 492
83cbce95
LC
493 ret = blk_register_queue(disk);
494 if (ret)
495 goto out_put_slave_dir;
75f4dca5 496
8235b5c1
CH
497 if (disk->flags & GENHD_FL_HIDDEN) {
498 /*
499 * Don't let hidden disks show up in /proc/partitions,
500 * and don't bother scanning for partitions either.
501 */
502 disk->flags |= GENHD_FL_SUPPRESS_PARTITION_INFO;
46e7eac6 503 disk->flags |= GENHD_FL_NO_PART;
8235b5c1
CH
504 } else {
505 ret = bdi_register(disk->bdi, "%u:%u",
506 disk->major, disk->first_minor);
83cbce95
LC
507 if (ret)
508 goto out_unregister_queue;
8235b5c1 509 bdi_set_owner(disk->bdi, ddev);
83cbce95
LC
510 ret = sysfs_create_link(&ddev->kobj,
511 &disk->bdi->dev->kobj, "bdi");
512 if (ret)
513 goto out_unregister_bdi;
8235b5c1 514
9d5ee676 515 bdev_add(disk->part0, ddev->devt);
e16e506c
CH
516 if (get_capacity(disk))
517 disk_scan_partitions(disk, FMODE_READ);
52b85909
CH
518
519 /*
520 * Announce the disk and partitions after all partitions are
8235b5c1 521 * created. (for hidden disks uevents remain suppressed forever)
52b85909
CH
522 */
523 dev_set_uevent_suppress(ddev, 0);
524 disk_uevent(disk, KOBJ_ADD);
52b85909
CH
525 }
526
75f4dca5 527 disk_update_readahead(disk);
77ea887e 528 disk_add_events(disk);
83cbce95
LC
529 return 0;
530
531out_unregister_bdi:
532 if (!(disk->flags & GENHD_FL_HIDDEN))
533 bdi_unregister(disk->bdi);
534out_unregister_queue:
535 blk_unregister_queue(disk);
536out_put_slave_dir:
537 kobject_put(disk->slave_dir);
538out_put_holder_dir:
539 kobject_put(disk->part0->bd_holder_dir);
540out_del_integrity:
541 blk_integrity_del(disk);
542out_del_block_link:
543 if (!sysfs_deprecated)
544 sysfs_remove_link(block_depr, dev_name(ddev));
545out_device_del:
546 device_del(ddev);
547out_disk_release_events:
548 disk_release_events(disk);
549out_free_ext_minor:
550 if (disk->major == BLOCK_EXT_MAJOR)
551 blk_free_ext_minor(disk->first_minor);
278167fd 552 return ret;
1da177e4 553}
e63a46be 554EXPORT_SYMBOL(device_add_disk);
1da177e4 555
b5bd357c
LC
556/**
557 * del_gendisk - remove the gendisk
558 * @disk: the struct gendisk to remove
559 *
560 * Removes the gendisk and all its associated resources. This deletes the
561 * partitions associated with the gendisk, and unregisters the associated
562 * request_queue.
563 *
564 * This is the counter to the respective __device_add_disk() call.
565 *
566 * The final removal of the struct gendisk happens when its refcount reaches 0
567 * with put_disk(), which should be called after del_gendisk(), if
568 * __device_add_disk() was used.
e8c7d14a
LC
569 *
570 * Drivers exist which depend on the release of the gendisk to be synchronous,
571 * it should not be deferred.
572 *
573 * Context: can sleep
b5bd357c 574 */
d2bf1b67 575void del_gendisk(struct gendisk *disk)
1da177e4 576{
8e141f9e
CH
577 struct request_queue *q = disk->queue;
578
e8c7d14a
LC
579 might_sleep();
580
9f286992 581 if (WARN_ON_ONCE(!disk_live(disk) && !(disk->flags & GENHD_FL_HIDDEN)))
6b3ba976
CH
582 return;
583
25520d55 584 blk_integrity_del(disk);
77ea887e
TH
585 disk_del_events(disk);
586
a8698707 587 mutex_lock(&disk->open_mutex);
d7a66574 588 remove_inode_hash(disk->part0->bd_inode);
d3c4a43d 589 blk_drop_partitions(disk);
a8698707 590 mutex_unlock(&disk->open_mutex);
c76f48eb 591
45611837
CH
592 fsync_bdev(disk->part0);
593 __invalidate_device(disk->part0, true);
594
8e141f9e
CH
595 /*
596 * Fail any new I/O.
597 */
598 set_bit(GD_DEAD, &disk->state);
d2bf1b67 599 set_capacity(disk, 0);
d2bf1b67 600
8e141f9e
CH
601 /*
602 * Prevent new I/O from crossing bio_queue_enter().
603 */
604 blk_queue_start_drain(q);
8e141f9e 605
6b3ba976 606 if (!(disk->flags & GENHD_FL_HIDDEN)) {
8ddcd653 607 sysfs_remove_link(&disk_to_dev(disk)->kobj, "bdi");
6b3ba976 608
90f16fdd
JK
609 /*
610 * Unregister bdi before releasing device numbers (as they can
611 * get reused and we'd get clashes in sysfs).
612 */
edb0872f 613 bdi_unregister(disk->bdi);
90f16fdd 614 }
d2bf1b67 615
6b3ba976 616 blk_unregister_queue(disk);
d2bf1b67 617
cb8432d6 618 kobject_put(disk->part0->bd_holder_dir);
d2bf1b67 619 kobject_put(disk->slave_dir);
d2bf1b67 620
8446fe92 621 part_stat_set_all(disk->part0, 0);
cb8432d6 622 disk->part0->bd_stamp = 0;
d2bf1b67
TH
623 if (!sysfs_deprecated)
624 sysfs_remove_link(block_depr, dev_name(disk_to_dev(disk)));
25e823c8 625 pm_runtime_set_memalloc_noio(disk_to_dev(disk), false);
d2bf1b67 626 device_del(disk_to_dev(disk));
d308ae0d
ML
627
628 blk_mq_freeze_queue_wait(q);
629
630 rq_qos_exit(q);
631 blk_sync_queue(q);
632 blk_flush_integrity();
633 /*
634 * Allow using passthrough request again after the queue is torn down.
635 */
636 blk_queue_flag_clear(QUEUE_FLAG_INIT_DONE, q);
637 __blk_mq_unfreeze_queue(q, true);
638
1da177e4 639}
d2bf1b67 640EXPORT_SYMBOL(del_gendisk);
1da177e4 641
f059a1d2
XY
642/**
643 * invalidate_disk - invalidate the disk
644 * @disk: the struct gendisk to invalidate
645 *
646 * A helper to invalidates the disk. It will clean the disk's associated
647 * buffer/page caches and reset its internal states so that the disk
648 * can be reused by the drivers.
649 *
650 * Context: can sleep
651 */
652void invalidate_disk(struct gendisk *disk)
653{
654 struct block_device *bdev = disk->part0;
655
656 invalidate_bdev(bdev);
657 bdev->bd_inode->i_mapping->wb_err = 0;
658 set_capacity(disk, 0);
659}
660EXPORT_SYMBOL(invalidate_disk);
661
99e6608c
VV
662/* sysfs access to bad-blocks list. */
663static ssize_t disk_badblocks_show(struct device *dev,
664 struct device_attribute *attr,
665 char *page)
666{
667 struct gendisk *disk = dev_to_disk(dev);
668
669 if (!disk->bb)
670 return sprintf(page, "\n");
671
672 return badblocks_show(disk->bb, page, 0);
673}
674
675static ssize_t disk_badblocks_store(struct device *dev,
676 struct device_attribute *attr,
677 const char *page, size_t len)
678{
679 struct gendisk *disk = dev_to_disk(dev);
680
681 if (!disk->bb)
682 return -ENXIO;
683
684 return badblocks_store(disk->bb, page, len, 0);
685}
686
22ae8ce8 687void blk_request_module(dev_t devt)
bd8eff3b 688{
a160c615
CH
689 unsigned int major = MAJOR(devt);
690 struct blk_major_name **n;
691
692 mutex_lock(&major_names_lock);
693 for (n = &major_names[major_to_index(major)]; *n; n = &(*n)->next) {
694 if ((*n)->major == major && (*n)->probe) {
695 (*n)->probe(devt);
696 mutex_unlock(&major_names_lock);
697 return;
698 }
699 }
700 mutex_unlock(&major_names_lock);
701
bd8eff3b
CH
702 if (request_module("block-major-%d-%d", MAJOR(devt), MINOR(devt)) > 0)
703 /* Make old-style 2.4 aliases work */
704 request_module("block-major-%d", MAJOR(devt));
705}
706
5c6f35c5
GKH
707/*
708 * print a full list of all partitions - intended for places where the root
709 * filesystem can't be mounted and thus to give the victim some idea of what
710 * went wrong
711 */
712void __init printk_all_partitions(void)
713{
def4e38d
TH
714 struct class_dev_iter iter;
715 struct device *dev;
716
717 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
718 while ((dev = class_dev_iter_next(&iter))) {
719 struct gendisk *disk = dev_to_disk(dev);
ad1eaa53 720 struct block_device *part;
1f014290 721 char devt_buf[BDEVT_SIZE];
e559f58d 722 unsigned long idx;
def4e38d
TH
723
724 /*
725 * Don't show empty devices or things that have been
25985edc 726 * suppressed
def4e38d
TH
727 */
728 if (get_capacity(disk) == 0 ||
729 (disk->flags & GENHD_FL_SUPPRESS_PARTITION_INFO))
730 continue;
731
732 /*
e559f58d
CH
733 * Note, unlike /proc/partitions, I am showing the numbers in
734 * hex - the same format as the root= option takes.
def4e38d 735 */
e559f58d
CH
736 rcu_read_lock();
737 xa_for_each(&disk->part_tbl, idx, part) {
738 if (!bdev_nr_sectors(part))
739 continue;
a9e7bc3d 740 printk("%s%s %10llu %pg %s",
e559f58d 741 bdev_is_partition(part) ? " " : "",
ad1eaa53 742 bdevt_str(part->bd_dev, devt_buf),
a9e7bc3d 743 bdev_nr_sectors(part) >> 1, part,
ad1eaa53
CH
744 part->bd_meta_info ?
745 part->bd_meta_info->uuid : "");
e559f58d 746 if (bdev_is_partition(part))
074a7aca 747 printk("\n");
e559f58d
CH
748 else if (dev->parent && dev->parent->driver)
749 printk(" driver: %s\n",
750 dev->parent->driver->name);
751 else
752 printk(" (driver?)\n");
074a7aca 753 }
e559f58d 754 rcu_read_unlock();
def4e38d
TH
755 }
756 class_dev_iter_exit(&iter);
dd2a345f
DG
757}
758
1da177e4
LT
759#ifdef CONFIG_PROC_FS
760/* iterator */
def4e38d 761static void *disk_seqf_start(struct seq_file *seqf, loff_t *pos)
68c4d4a7 762{
def4e38d
TH
763 loff_t skip = *pos;
764 struct class_dev_iter *iter;
765 struct device *dev;
68c4d4a7 766
aeb3d3a8 767 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
def4e38d
TH
768 if (!iter)
769 return ERR_PTR(-ENOMEM);
770
771 seqf->private = iter;
772 class_dev_iter_init(iter, &block_class, NULL, &disk_type);
773 do {
774 dev = class_dev_iter_next(iter);
775 if (!dev)
776 return NULL;
777 } while (skip--);
778
779 return dev_to_disk(dev);
68c4d4a7
GKH
780}
781
def4e38d 782static void *disk_seqf_next(struct seq_file *seqf, void *v, loff_t *pos)
1da177e4 783{
edfaa7c3 784 struct device *dev;
1da177e4 785
def4e38d
TH
786 (*pos)++;
787 dev = class_dev_iter_next(seqf->private);
2ac3cee5 788 if (dev)
68c4d4a7 789 return dev_to_disk(dev);
2ac3cee5 790
1da177e4
LT
791 return NULL;
792}
793
def4e38d 794static void disk_seqf_stop(struct seq_file *seqf, void *v)
27f30251 795{
def4e38d 796 struct class_dev_iter *iter = seqf->private;
27f30251 797
def4e38d
TH
798 /* stop is called even after start failed :-( */
799 if (iter) {
800 class_dev_iter_exit(iter);
801 kfree(iter);
77da1605 802 seqf->private = NULL;
5c0ef6d0 803 }
1da177e4
LT
804}
805
def4e38d 806static void *show_partition_start(struct seq_file *seqf, loff_t *pos)
1da177e4 807{
06768067 808 void *p;
def4e38d
TH
809
810 p = disk_seqf_start(seqf, pos);
b9f985b6 811 if (!IS_ERR_OR_NULL(p) && !*pos)
def4e38d
TH
812 seq_puts(seqf, "major minor #blocks name\n\n");
813 return p;
1da177e4
LT
814}
815
cf771cb5 816static int show_partition(struct seq_file *seqf, void *v)
1da177e4
LT
817{
818 struct gendisk *sgp = v;
ad1eaa53 819 struct block_device *part;
ecc75a98 820 unsigned long idx;
1da177e4 821
e3b3bad3 822 if (!get_capacity(sgp))
1da177e4
LT
823 return 0;
824 if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)
825 return 0;
826
ecc75a98
CH
827 rcu_read_lock();
828 xa_for_each(&sgp->part_tbl, idx, part) {
829 if (!bdev_nr_sectors(part))
830 continue;
a291bb43 831 seq_printf(seqf, "%4d %7d %10llu %pg\n",
ad1eaa53 832 MAJOR(part->bd_dev), MINOR(part->bd_dev),
a291bb43 833 bdev_nr_sectors(part) >> 1, part);
ecc75a98
CH
834 }
835 rcu_read_unlock();
1da177e4
LT
836 return 0;
837}
838
f500975a 839static const struct seq_operations partitions_op = {
def4e38d
TH
840 .start = show_partition_start,
841 .next = disk_seqf_next,
842 .stop = disk_seqf_stop,
edfaa7c3 843 .show = show_partition
1da177e4
LT
844};
845#endif
846
1da177e4
LT
847static int __init genhd_device_init(void)
848{
e105b8bf
DW
849 int error;
850
851 block_class.dev_kobj = sysfs_dev_block_kobj;
852 error = class_register(&block_class);
ee27a558
RM
853 if (unlikely(error))
854 return error;
1da177e4 855 blk_dev_init();
edfaa7c3 856
561ec68e
ZY
857 register_blkdev(BLOCK_EXT_MAJOR, "blkext");
858
edfaa7c3 859 /* create top-level block dir */
e52eec13
AK
860 if (!sysfs_deprecated)
861 block_depr = kobject_create_and_add("block", NULL);
830d3cfb 862 return 0;
1da177e4
LT
863}
864
865subsys_initcall(genhd_device_init);
866
edfaa7c3
KS
867static ssize_t disk_range_show(struct device *dev,
868 struct device_attribute *attr, char *buf)
1da177e4 869{
edfaa7c3 870 struct gendisk *disk = dev_to_disk(dev);
1da177e4 871
edfaa7c3 872 return sprintf(buf, "%d\n", disk->minors);
1da177e4
LT
873}
874
1f014290
TH
875static ssize_t disk_ext_range_show(struct device *dev,
876 struct device_attribute *attr, char *buf)
877{
878 struct gendisk *disk = dev_to_disk(dev);
879
b5d0b9df 880 return sprintf(buf, "%d\n", disk_max_parts(disk));
1f014290
TH
881}
882
edfaa7c3
KS
883static ssize_t disk_removable_show(struct device *dev,
884 struct device_attribute *attr, char *buf)
a7fd6706 885{
edfaa7c3 886 struct gendisk *disk = dev_to_disk(dev);
a7fd6706 887
edfaa7c3
KS
888 return sprintf(buf, "%d\n",
889 (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
a7fd6706
KS
890}
891
8ddcd653
CH
892static ssize_t disk_hidden_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",
898 (disk->flags & GENHD_FL_HIDDEN ? 1 : 0));
899}
900
1c9ce527
KS
901static ssize_t disk_ro_show(struct device *dev,
902 struct device_attribute *attr, char *buf)
903{
904 struct gendisk *disk = dev_to_disk(dev);
905
b7db9956 906 return sprintf(buf, "%d\n", get_disk_ro(disk) ? 1 : 0);
1c9ce527
KS
907}
908
3ad5cee5
CH
909ssize_t part_size_show(struct device *dev,
910 struct device_attribute *attr, char *buf)
911{
0d02129e 912 return sprintf(buf, "%llu\n", bdev_nr_sectors(dev_to_bdev(dev)));
3ad5cee5
CH
913}
914
915ssize_t part_stat_show(struct device *dev,
916 struct device_attribute *attr, char *buf)
917{
0d02129e 918 struct block_device *bdev = dev_to_bdev(dev);
ed6cddef 919 struct request_queue *q = bdev_get_queue(bdev);
ea18e0f0 920 struct disk_stats stat;
3ad5cee5
CH
921 unsigned int inflight;
922
0d02129e 923 part_stat_read_all(bdev, &stat);
b2f609e1 924 if (queue_is_mq(q))
0d02129e 925 inflight = blk_mq_in_flight(q, bdev);
b2f609e1 926 else
0d02129e 927 inflight = part_in_flight(bdev);
ea18e0f0 928
3ad5cee5
CH
929 return sprintf(buf,
930 "%8lu %8lu %8llu %8u "
931 "%8lu %8lu %8llu %8u "
932 "%8u %8u %8u "
933 "%8lu %8lu %8llu %8u "
934 "%8lu %8u"
935 "\n",
ea18e0f0
KK
936 stat.ios[STAT_READ],
937 stat.merges[STAT_READ],
938 (unsigned long long)stat.sectors[STAT_READ],
939 (unsigned int)div_u64(stat.nsecs[STAT_READ], NSEC_PER_MSEC),
940 stat.ios[STAT_WRITE],
941 stat.merges[STAT_WRITE],
942 (unsigned long long)stat.sectors[STAT_WRITE],
943 (unsigned int)div_u64(stat.nsecs[STAT_WRITE], NSEC_PER_MSEC),
3ad5cee5 944 inflight,
ea18e0f0 945 jiffies_to_msecs(stat.io_ticks),
8cd5b8fc
KK
946 (unsigned int)div_u64(stat.nsecs[STAT_READ] +
947 stat.nsecs[STAT_WRITE] +
948 stat.nsecs[STAT_DISCARD] +
949 stat.nsecs[STAT_FLUSH],
950 NSEC_PER_MSEC),
ea18e0f0
KK
951 stat.ios[STAT_DISCARD],
952 stat.merges[STAT_DISCARD],
953 (unsigned long long)stat.sectors[STAT_DISCARD],
954 (unsigned int)div_u64(stat.nsecs[STAT_DISCARD], NSEC_PER_MSEC),
955 stat.ios[STAT_FLUSH],
956 (unsigned int)div_u64(stat.nsecs[STAT_FLUSH], NSEC_PER_MSEC));
3ad5cee5
CH
957}
958
959ssize_t part_inflight_show(struct device *dev, struct device_attribute *attr,
960 char *buf)
961{
0d02129e 962 struct block_device *bdev = dev_to_bdev(dev);
ed6cddef 963 struct request_queue *q = bdev_get_queue(bdev);
3ad5cee5
CH
964 unsigned int inflight[2];
965
b2f609e1 966 if (queue_is_mq(q))
0d02129e 967 blk_mq_in_flight_rw(q, bdev, inflight);
b2f609e1 968 else
0d02129e 969 part_in_flight_rw(bdev, inflight);
b2f609e1 970
3ad5cee5
CH
971 return sprintf(buf, "%8u %8u\n", inflight[0], inflight[1]);
972}
973
edfaa7c3
KS
974static ssize_t disk_capability_show(struct device *dev,
975 struct device_attribute *attr, char *buf)
86ce18d7 976{
edfaa7c3
KS
977 struct gendisk *disk = dev_to_disk(dev);
978
979 return sprintf(buf, "%x\n", disk->flags);
86ce18d7 980}
edfaa7c3 981
c72758f3
MP
982static ssize_t disk_alignment_offset_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_alignment_offset(disk->queue));
989}
990
86b37281
MP
991static ssize_t disk_discard_alignment_show(struct device *dev,
992 struct device_attribute *attr,
993 char *buf)
994{
995 struct gendisk *disk = dev_to_disk(dev);
996
dd3d145d 997 return sprintf(buf, "%d\n", queue_discard_alignment(disk->queue));
86b37281
MP
998}
999
13927b31
MC
1000static ssize_t diskseq_show(struct device *dev,
1001 struct device_attribute *attr, char *buf)
1002{
1003 struct gendisk *disk = dev_to_disk(dev);
1004
1005 return sprintf(buf, "%llu\n", disk->diskseq);
1006}
1007
5657a819
JP
1008static DEVICE_ATTR(range, 0444, disk_range_show, NULL);
1009static DEVICE_ATTR(ext_range, 0444, disk_ext_range_show, NULL);
1010static DEVICE_ATTR(removable, 0444, disk_removable_show, NULL);
1011static DEVICE_ATTR(hidden, 0444, disk_hidden_show, NULL);
1012static DEVICE_ATTR(ro, 0444, disk_ro_show, NULL);
1013static DEVICE_ATTR(size, 0444, part_size_show, NULL);
1014static DEVICE_ATTR(alignment_offset, 0444, disk_alignment_offset_show, NULL);
1015static DEVICE_ATTR(discard_alignment, 0444, disk_discard_alignment_show, NULL);
1016static DEVICE_ATTR(capability, 0444, disk_capability_show, NULL);
1017static DEVICE_ATTR(stat, 0444, part_stat_show, NULL);
1018static DEVICE_ATTR(inflight, 0444, part_inflight_show, NULL);
1019static DEVICE_ATTR(badblocks, 0644, disk_badblocks_show, disk_badblocks_store);
13927b31 1020static DEVICE_ATTR(diskseq, 0444, diskseq_show, NULL);
3ad5cee5 1021
c17bb495 1022#ifdef CONFIG_FAIL_MAKE_REQUEST
3ad5cee5
CH
1023ssize_t part_fail_show(struct device *dev,
1024 struct device_attribute *attr, char *buf)
1025{
0d02129e 1026 return sprintf(buf, "%d\n", dev_to_bdev(dev)->bd_make_it_fail);
3ad5cee5
CH
1027}
1028
1029ssize_t part_fail_store(struct device *dev,
1030 struct device_attribute *attr,
1031 const char *buf, size_t count)
1032{
3ad5cee5
CH
1033 int i;
1034
1035 if (count > 0 && sscanf(buf, "%d", &i) > 0)
0d02129e 1036 dev_to_bdev(dev)->bd_make_it_fail = i;
3ad5cee5
CH
1037
1038 return count;
1039}
1040
edfaa7c3 1041static struct device_attribute dev_attr_fail =
5657a819 1042 __ATTR(make-it-fail, 0644, part_fail_show, part_fail_store);
3ad5cee5
CH
1043#endif /* CONFIG_FAIL_MAKE_REQUEST */
1044
581d4e28
JA
1045#ifdef CONFIG_FAIL_IO_TIMEOUT
1046static struct device_attribute dev_attr_fail_timeout =
5657a819 1047 __ATTR(io-timeout-fail, 0644, part_timeout_show, part_timeout_store);
581d4e28 1048#endif
edfaa7c3
KS
1049
1050static struct attribute *disk_attrs[] = {
1051 &dev_attr_range.attr,
1f014290 1052 &dev_attr_ext_range.attr,
edfaa7c3 1053 &dev_attr_removable.attr,
8ddcd653 1054 &dev_attr_hidden.attr,
1c9ce527 1055 &dev_attr_ro.attr,
edfaa7c3 1056 &dev_attr_size.attr,
c72758f3 1057 &dev_attr_alignment_offset.attr,
86b37281 1058 &dev_attr_discard_alignment.attr,
edfaa7c3
KS
1059 &dev_attr_capability.attr,
1060 &dev_attr_stat.attr,
316d315b 1061 &dev_attr_inflight.attr,
99e6608c 1062 &dev_attr_badblocks.attr,
2bc8cda5
CH
1063 &dev_attr_events.attr,
1064 &dev_attr_events_async.attr,
1065 &dev_attr_events_poll_msecs.attr,
13927b31 1066 &dev_attr_diskseq.attr,
edfaa7c3
KS
1067#ifdef CONFIG_FAIL_MAKE_REQUEST
1068 &dev_attr_fail.attr,
581d4e28
JA
1069#endif
1070#ifdef CONFIG_FAIL_IO_TIMEOUT
1071 &dev_attr_fail_timeout.attr,
edfaa7c3
KS
1072#endif
1073 NULL
1074};
1075
9438b3e0
DW
1076static umode_t disk_visible(struct kobject *kobj, struct attribute *a, int n)
1077{
1078 struct device *dev = container_of(kobj, typeof(*dev), kobj);
1079 struct gendisk *disk = dev_to_disk(dev);
1080
1081 if (a == &dev_attr_badblocks.attr && !disk->bb)
1082 return 0;
1083 return a->mode;
1084}
1085
edfaa7c3
KS
1086static struct attribute_group disk_attr_group = {
1087 .attrs = disk_attrs,
9438b3e0 1088 .is_visible = disk_visible,
edfaa7c3
KS
1089};
1090
a4dbd674 1091static const struct attribute_group *disk_attr_groups[] = {
edfaa7c3
KS
1092 &disk_attr_group,
1093 NULL
1da177e4
LT
1094};
1095
b5bd357c
LC
1096/**
1097 * disk_release - releases all allocated resources of the gendisk
1098 * @dev: the device representing this disk
1099 *
1100 * This function releases all allocated resources of the gendisk.
1101 *
b5bd357c
LC
1102 * Drivers which used __device_add_disk() have a gendisk with a request_queue
1103 * assigned. Since the request_queue sits on top of the gendisk for these
1104 * drivers we also call blk_put_queue() for them, and we expect the
1105 * request_queue refcount to reach 0 at this point, and so the request_queue
1106 * will also be freed prior to the disk.
e8c7d14a
LC
1107 *
1108 * Context: can sleep
b5bd357c 1109 */
edfaa7c3 1110static void disk_release(struct device *dev)
1da177e4 1111{
edfaa7c3
KS
1112 struct gendisk *disk = dev_to_disk(dev);
1113
e8c7d14a 1114 might_sleep();
a2041761 1115 WARN_ON_ONCE(disk_live(disk));
e8c7d14a 1116
2a19b28f
ML
1117 blk_mq_cancel_work_sync(disk->queue);
1118
77ea887e 1119 disk_release_events(disk);
1da177e4 1120 kfree(disk->random);
a33df75c 1121 xa_destroy(&disk->part_tbl);
d152c682 1122 disk->queue->disk = NULL;
61a35cfc 1123 blk_put_queue(disk->queue);
2f4731dc 1124 iput(disk->part0->bd_inode); /* frees the disk */
1da177e4 1125}
87eb7107
MC
1126
1127static int block_uevent(struct device *dev, struct kobj_uevent_env *env)
1128{
1129 struct gendisk *disk = dev_to_disk(dev);
1130
1131 return add_uevent_var(env, "DISKSEQ=%llu", disk->diskseq);
1132}
1133
edfaa7c3
KS
1134struct class block_class = {
1135 .name = "block",
87eb7107 1136 .dev_uevent = block_uevent,
1da177e4
LT
1137};
1138
3c2670e6 1139static char *block_devnode(struct device *dev, umode_t *mode,
4e4098a3 1140 kuid_t *uid, kgid_t *gid)
b03f38b6
KS
1141{
1142 struct gendisk *disk = dev_to_disk(dev);
1143
348e114b
CH
1144 if (disk->fops->devnode)
1145 return disk->fops->devnode(disk, mode);
b03f38b6
KS
1146 return NULL;
1147}
1148
ef45fe47 1149const struct device_type disk_type = {
edfaa7c3
KS
1150 .name = "disk",
1151 .groups = disk_attr_groups,
1152 .release = disk_release,
e454cea2 1153 .devnode = block_devnode,
1da177e4
LT
1154};
1155
a6e2ba88 1156#ifdef CONFIG_PROC_FS
cf771cb5
TH
1157/*
1158 * aggregate disk stat collector. Uses the same stats that the sysfs
1159 * entries do, above, but makes them available through one seq_file.
1160 *
1161 * The output looks suspiciously like /proc/partitions with a bunch of
1162 * extra fields.
1163 */
1164static int diskstats_show(struct seq_file *seqf, void *v)
1da177e4
LT
1165{
1166 struct gendisk *gp = v;
ad1eaa53 1167 struct block_device *hd;
e016b782 1168 unsigned int inflight;
ea18e0f0 1169 struct disk_stats stat;
7fae67cc 1170 unsigned long idx;
1da177e4
LT
1171
1172 /*
ed9e1982 1173 if (&disk_to_dev(gp)->kobj.entry == block_class.devices.next)
cf771cb5 1174 seq_puts(seqf, "major minor name"
1da177e4
LT
1175 " rio rmerge rsect ruse wio wmerge "
1176 "wsect wuse running use aveq"
1177 "\n\n");
1178 */
9f5e4865 1179
7fae67cc
CH
1180 rcu_read_lock();
1181 xa_for_each(&gp->part_tbl, idx, hd) {
1182 if (bdev_is_partition(hd) && !bdev_nr_sectors(hd))
1183 continue;
0d02129e 1184 part_stat_read_all(hd, &stat);
b2f609e1 1185 if (queue_is_mq(gp->queue))
ad1eaa53 1186 inflight = blk_mq_in_flight(gp->queue, hd);
b2f609e1 1187 else
ad1eaa53 1188 inflight = part_in_flight(hd);
ea18e0f0 1189
26e2d7a3 1190 seq_printf(seqf, "%4d %7d %pg "
bdca3c87
MC
1191 "%lu %lu %lu %u "
1192 "%lu %lu %lu %u "
1193 "%u %u %u "
b6866318
KK
1194 "%lu %lu %lu %u "
1195 "%lu %u"
1196 "\n",
26e2d7a3 1197 MAJOR(hd->bd_dev), MINOR(hd->bd_dev), hd,
ea18e0f0
KK
1198 stat.ios[STAT_READ],
1199 stat.merges[STAT_READ],
1200 stat.sectors[STAT_READ],
1201 (unsigned int)div_u64(stat.nsecs[STAT_READ],
1202 NSEC_PER_MSEC),
1203 stat.ios[STAT_WRITE],
1204 stat.merges[STAT_WRITE],
1205 stat.sectors[STAT_WRITE],
1206 (unsigned int)div_u64(stat.nsecs[STAT_WRITE],
1207 NSEC_PER_MSEC),
e016b782 1208 inflight,
ea18e0f0 1209 jiffies_to_msecs(stat.io_ticks),
8cd5b8fc
KK
1210 (unsigned int)div_u64(stat.nsecs[STAT_READ] +
1211 stat.nsecs[STAT_WRITE] +
1212 stat.nsecs[STAT_DISCARD] +
1213 stat.nsecs[STAT_FLUSH],
1214 NSEC_PER_MSEC),
ea18e0f0
KK
1215 stat.ios[STAT_DISCARD],
1216 stat.merges[STAT_DISCARD],
1217 stat.sectors[STAT_DISCARD],
1218 (unsigned int)div_u64(stat.nsecs[STAT_DISCARD],
1219 NSEC_PER_MSEC),
1220 stat.ios[STAT_FLUSH],
1221 (unsigned int)div_u64(stat.nsecs[STAT_FLUSH],
1222 NSEC_PER_MSEC)
28f39d55 1223 );
1da177e4 1224 }
7fae67cc 1225 rcu_read_unlock();
9f5e4865 1226
1da177e4
LT
1227 return 0;
1228}
1229
31d85ab2 1230static const struct seq_operations diskstats_op = {
def4e38d
TH
1231 .start = disk_seqf_start,
1232 .next = disk_seqf_next,
1233 .stop = disk_seqf_stop,
1da177e4
LT
1234 .show = diskstats_show
1235};
f500975a
AD
1236
1237static int __init proc_genhd_init(void)
1238{
fddda2b7
CH
1239 proc_create_seq("diskstats", 0, NULL, &diskstats_op);
1240 proc_create_seq("partitions", 0, NULL, &partitions_op);
f500975a
AD
1241 return 0;
1242}
1243module_init(proc_genhd_init);
a6e2ba88 1244#endif /* CONFIG_PROC_FS */
1da177e4 1245
c97d93c3
CH
1246dev_t part_devt(struct gendisk *disk, u8 partno)
1247{
0e0ccdec 1248 struct block_device *part;
c97d93c3
CH
1249 dev_t devt = 0;
1250
0e0ccdec
CH
1251 rcu_read_lock();
1252 part = xa_load(&disk->part_tbl, partno);
1253 if (part)
c97d93c3 1254 devt = part->bd_dev;
0e0ccdec 1255 rcu_read_unlock();
c97d93c3
CH
1256
1257 return devt;
1258}
1259
cf771cb5 1260dev_t blk_lookup_devt(const char *name, int partno)
a142be85 1261{
def4e38d
TH
1262 dev_t devt = MKDEV(0, 0);
1263 struct class_dev_iter iter;
1264 struct device *dev;
a142be85 1265
def4e38d
TH
1266 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
1267 while ((dev = class_dev_iter_next(&iter))) {
a142be85 1268 struct gendisk *disk = dev_to_disk(dev);
a142be85 1269
3ada8b7e 1270 if (strcmp(dev_name(dev), name))
f331c029 1271 continue;
f331c029 1272
41b8c853
NB
1273 if (partno < disk->minors) {
1274 /* We need to return the right devno, even
1275 * if the partition doesn't exist yet.
1276 */
1277 devt = MKDEV(MAJOR(dev->devt),
1278 MINOR(dev->devt) + partno);
c97d93c3
CH
1279 } else {
1280 devt = part_devt(disk, partno);
1281 if (devt)
1282 break;
def4e38d 1283 }
5c0ef6d0 1284 }
def4e38d 1285 class_dev_iter_exit(&iter);
edfaa7c3
KS
1286 return devt;
1287}
edfaa7c3 1288
4a1fa41d
CH
1289struct gendisk *__alloc_disk_node(struct request_queue *q, int node_id,
1290 struct lock_class_key *lkclass)
1946089a
CL
1291{
1292 struct gendisk *disk;
1293
61a35cfc
CH
1294 if (!blk_get_queue(q))
1295 return NULL;
1296
c1b511eb 1297 disk = kzalloc_node(sizeof(struct gendisk), GFP_KERNEL, node_id);
f93af2a4 1298 if (!disk)
61a35cfc 1299 goto out_put_queue;
6c23a968 1300
edb0872f
CH
1301 disk->bdi = bdi_alloc(node_id);
1302 if (!disk->bdi)
1303 goto out_free_disk;
1304
17220ca5
PB
1305 /* bdev_alloc() might need the queue, set before the first call */
1306 disk->queue = q;
1307
cb8432d6
CH
1308 disk->part0 = bdev_alloc(disk, 0);
1309 if (!disk->part0)
edb0872f 1310 goto out_free_bdi;
22ae8ce8 1311
f93af2a4 1312 disk->node_id = node_id;
a8698707 1313 mutex_init(&disk->open_mutex);
a33df75c
CH
1314 xa_init(&disk->part_tbl);
1315 if (xa_insert(&disk->part_tbl, 0, disk->part0, GFP_KERNEL))
1316 goto out_destroy_part_tbl;
f93af2a4 1317
f93af2a4
CH
1318 rand_initialize_disk(disk);
1319 disk_to_dev(disk)->class = &block_class;
1320 disk_to_dev(disk)->type = &disk_type;
1321 device_initialize(disk_to_dev(disk));
cf179948 1322 inc_diskseq(disk);
d152c682 1323 q->disk = disk;
4dcc4874 1324 lockdep_init_map(&disk->lockdep_map, "(bio completion)", lkclass, 0);
0dbcfe24
CH
1325#ifdef CONFIG_BLOCK_HOLDER_DEPRECATED
1326 INIT_LIST_HEAD(&disk->slave_bdevs);
1327#endif
1da177e4 1328 return disk;
f93af2a4 1329
a33df75c
CH
1330out_destroy_part_tbl:
1331 xa_destroy(&disk->part_tbl);
06cc978d 1332 disk->part0->bd_disk = NULL;
2f4731dc 1333 iput(disk->part0->bd_inode);
edb0872f
CH
1334out_free_bdi:
1335 bdi_put(disk->bdi);
f93af2a4
CH
1336out_free_disk:
1337 kfree(disk);
61a35cfc
CH
1338out_put_queue:
1339 blk_put_queue(q);
f93af2a4 1340 return NULL;
1da177e4 1341}
e319e1fb 1342EXPORT_SYMBOL(__alloc_disk_node);
1da177e4 1343
4dcc4874 1344struct gendisk *__blk_alloc_disk(int node, struct lock_class_key *lkclass)
f525464a
CH
1345{
1346 struct request_queue *q;
1347 struct gendisk *disk;
1348
1349 q = blk_alloc_queue(node);
1350 if (!q)
1351 return NULL;
1352
4a1fa41d 1353 disk = __alloc_disk_node(q, node, lkclass);
f525464a
CH
1354 if (!disk) {
1355 blk_cleanup_queue(q);
1356 return NULL;
1357 }
f525464a
CH
1358 return disk;
1359}
1360EXPORT_SYMBOL(__blk_alloc_disk);
1361
b5bd357c
LC
1362/**
1363 * put_disk - decrements the gendisk refcount
0d20dcc2 1364 * @disk: the struct gendisk to decrement the refcount for
b5bd357c
LC
1365 *
1366 * This decrements the refcount for the struct gendisk. When this reaches 0
1367 * we'll have disk_release() called.
e8c7d14a
LC
1368 *
1369 * Context: Any context, but the last reference must not be dropped from
1370 * atomic context.
b5bd357c 1371 */
1da177e4
LT
1372void put_disk(struct gendisk *disk)
1373{
1374 if (disk)
efdc41c8 1375 put_device(disk_to_dev(disk));
1da177e4 1376}
1da177e4
LT
1377EXPORT_SYMBOL(put_disk);
1378
f525464a
CH
1379/**
1380 * blk_cleanup_disk - shutdown a gendisk allocated by blk_alloc_disk
1381 * @disk: gendisk to shutdown
1382 *
1383 * Mark the queue hanging off @disk DYING, drain all pending requests, then mark
1384 * the queue DEAD, destroy and put it and the gendisk structure.
1385 *
1386 * Context: can sleep
1387 */
1388void blk_cleanup_disk(struct gendisk *disk)
1389{
1390 blk_cleanup_queue(disk->queue);
1391 put_disk(disk);
1392}
1393EXPORT_SYMBOL(blk_cleanup_disk);
1394
e3264a4d
HR
1395static void set_disk_ro_uevent(struct gendisk *gd, int ro)
1396{
1397 char event[] = "DISK_RO=1";
1398 char *envp[] = { event, NULL };
1399
1400 if (!ro)
1401 event[8] = '0';
1402 kobject_uevent_env(&disk_to_dev(gd)->kobj, KOBJ_CHANGE, envp);
1403}
1404
52f019d4
CH
1405/**
1406 * set_disk_ro - set a gendisk read-only
1407 * @disk: gendisk to operate on
7f31bee3 1408 * @read_only: %true to set the disk read-only, %false set the disk read/write
52f019d4
CH
1409 *
1410 * This function is used to indicate whether a given disk device should have its
1411 * read-only flag set. set_disk_ro() is typically used by device drivers to
1412 * indicate whether the underlying physical device is write-protected.
1413 */
1414void set_disk_ro(struct gendisk *disk, bool read_only)
1da177e4 1415{
52f019d4
CH
1416 if (read_only) {
1417 if (test_and_set_bit(GD_READ_ONLY, &disk->state))
1418 return;
1419 } else {
1420 if (!test_and_clear_bit(GD_READ_ONLY, &disk->state))
1421 return;
e3264a4d 1422 }
52f019d4 1423 set_disk_ro_uevent(disk, read_only);
1da177e4 1424}
1da177e4
LT
1425EXPORT_SYMBOL(set_disk_ro);
1426
cf179948
MC
1427void inc_diskseq(struct gendisk *disk)
1428{
1429 disk->diskseq = atomic64_inc_return(&diskseq);
1430}