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