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