]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - block/genhd.c
block: move the policy field to struct block_device
[mirror_ubuntu-jammy-kernel.git] / block / genhd.c
CommitLineData
3dcf60bc 1// SPDX-License-Identifier: GPL-2.0
1da177e4
LT
2/*
3 * gendisk handling
4 */
5
1da177e4 6#include <linux/module.h>
3ad5cee5 7#include <linux/ctype.h>
1da177e4
LT
8#include <linux/fs.h>
9#include <linux/genhd.h>
b446b60e 10#include <linux/kdev_t.h>
1da177e4
LT
11#include <linux/kernel.h>
12#include <linux/blkdev.h>
66114cad 13#include <linux/backing-dev.h>
1da177e4
LT
14#include <linux/init.h>
15#include <linux/spinlock.h>
f500975a 16#include <linux/proc_fs.h>
1da177e4
LT
17#include <linux/seq_file.h>
18#include <linux/slab.h>
19#include <linux/kmod.h>
58383af6 20#include <linux/mutex.h>
bcce3de1 21#include <linux/idr.h>
77ea887e 22#include <linux/log2.h>
25e823c8 23#include <linux/pm_runtime.h>
99e6608c 24#include <linux/badblocks.h>
1da177e4 25
ff88972c
AB
26#include "blk.h"
27
31eb6186 28static struct kobject *block_depr;
1da177e4 29
22ae8ce8 30DECLARE_RWSEM(bdev_lookup_sem);
62b508f8 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
12c2bdb2
DB
36static void disk_check_events(struct disk_events *ev,
37 unsigned int *clearing_ptr);
9f53d2fe 38static void disk_alloc_events(struct gendisk *disk);
77ea887e
TH
39static void disk_add_events(struct gendisk *disk);
40static void disk_del_events(struct gendisk *disk);
41static void disk_release_events(struct gendisk *disk);
42
a782483c
CH
43void set_capacity(struct gendisk *disk, sector_t sectors)
44{
45 struct block_device *bdev = disk->part0.bdev;
46
47 spin_lock(&bdev->bd_size_lock);
48 i_size_write(bdev->bd_inode, (loff_t)sectors << SECTOR_SHIFT);
49 spin_unlock(&bdev->bd_size_lock);
50}
51EXPORT_SYMBOL(set_capacity);
52
e598a72f 53/*
449f4ec9
CH
54 * Set disk capacity and notify if the size is not currently zero and will not
55 * be set to zero. Returns true if a uevent was sent, otherwise false.
e598a72f 56 */
449f4ec9 57bool set_capacity_and_notify(struct gendisk *disk, sector_t size)
e598a72f
BS
58{
59 sector_t capacity = get_capacity(disk);
a782483c 60 char *envp[] = { "RESIZE=1", NULL };
e598a72f
BS
61
62 set_capacity(disk, size);
e598a72f 63
a782483c
CH
64 /*
65 * Only print a message and send a uevent if the gendisk is user visible
66 * and alive. This avoids spamming the log and udev when setting the
67 * initial capacity during probing.
68 */
69 if (size == capacity ||
70 (disk->flags & (GENHD_FL_UP | GENHD_FL_HIDDEN)) != GENHD_FL_UP)
71 return false;
e598a72f 72
a782483c
CH
73 pr_info("%s: detected capacity change from %lld to %lld\n",
74 disk->disk_name, size, capacity);
7e890c37 75
a782483c
CH
76 /*
77 * Historically we did not send a uevent for changes to/from an empty
78 * device.
79 */
80 if (!capacity || !size)
81 return false;
82 kobject_uevent_env(&disk_to_dev(disk)->kobj, KOBJ_CHANGE, envp);
83 return true;
e598a72f 84}
449f4ec9 85EXPORT_SYMBOL_GPL(set_capacity_and_notify);
e598a72f 86
5cbd28e3
CH
87/*
88 * Format the device name of the indicated disk into the supplied buffer and
89 * return a pointer to that same buffer for convenience.
90 */
91char *disk_name(struct gendisk *hd, int partno, char *buf)
92{
93 if (!partno)
94 snprintf(buf, BDEVNAME_SIZE, "%s", hd->disk_name);
95 else if (isdigit(hd->disk_name[strlen(hd->disk_name)-1]))
96 snprintf(buf, BDEVNAME_SIZE, "%sp%d", hd->disk_name, partno);
97 else
98 snprintf(buf, BDEVNAME_SIZE, "%s%d", hd->disk_name, partno);
99
100 return buf;
101}
102
103const char *bdevname(struct block_device *bdev, char *buf)
104{
8a63a86e 105 return disk_name(bdev->bd_disk, bdev->bd_partno, buf);
5cbd28e3
CH
106}
107EXPORT_SYMBOL(bdevname);
e598a72f 108
ea18e0f0
KK
109static void part_stat_read_all(struct hd_struct *part, struct disk_stats *stat)
110{
111 int cpu;
112
113 memset(stat, 0, sizeof(struct disk_stats));
114 for_each_possible_cpu(cpu) {
15e3d2c5 115 struct disk_stats *ptr = per_cpu_ptr(part->bdev->bd_stats, cpu);
ea18e0f0
KK
116 int group;
117
118 for (group = 0; group < NR_STAT_GROUPS; group++) {
119 stat->nsecs[group] += ptr->nsecs[group];
120 stat->sectors[group] += ptr->sectors[group];
121 stat->ios[group] += ptr->ios[group];
122 stat->merges[group] += ptr->merges[group];
123 }
124
125 stat->io_ticks += ptr->io_ticks;
ea18e0f0
KK
126 }
127}
ea18e0f0 128
1f06959b 129static unsigned int part_in_flight(struct hd_struct *part)
f299b7c7 130{
b2f609e1 131 unsigned int inflight = 0;
1226b8dd 132 int cpu;
f299b7c7 133
1226b8dd 134 for_each_possible_cpu(cpu) {
e016b782
MP
135 inflight += part_stat_local_read_cpu(part, in_flight[0], cpu) +
136 part_stat_local_read_cpu(part, in_flight[1], cpu);
1226b8dd 137 }
e016b782
MP
138 if ((int)inflight < 0)
139 inflight = 0;
1226b8dd 140
e016b782 141 return inflight;
f299b7c7
JA
142}
143
1f06959b 144static void part_in_flight_rw(struct hd_struct *part, unsigned int inflight[2])
bf0ddaba 145{
1226b8dd
MP
146 int cpu;
147
1226b8dd
MP
148 inflight[0] = 0;
149 inflight[1] = 0;
150 for_each_possible_cpu(cpu) {
151 inflight[0] += part_stat_local_read_cpu(part, in_flight[0], cpu);
152 inflight[1] += part_stat_local_read_cpu(part, in_flight[1], cpu);
153 }
154 if ((int)inflight[0] < 0)
155 inflight[0] = 0;
156 if ((int)inflight[1] < 0)
157 inflight[1] = 0;
bf0ddaba
OS
158}
159
807d4af2
CH
160struct hd_struct *__disk_get_part(struct gendisk *disk, int partno)
161{
162 struct disk_part_tbl *ptbl = rcu_dereference(disk->part_tbl);
163
164 if (unlikely(partno < 0 || partno >= ptbl->len))
165 return NULL;
166 return rcu_dereference(ptbl->part[partno]);
167}
168
e71bf0d0
TH
169/**
170 * disk_get_part - get partition
171 * @disk: disk to look partition from
172 * @partno: partition number
173 *
174 * Look for partition @partno from @disk. If found, increment
175 * reference count and return it.
176 *
177 * CONTEXT:
178 * Don't care.
179 *
180 * RETURNS:
181 * Pointer to the found partition on success, NULL if not found.
182 */
183struct hd_struct *disk_get_part(struct gendisk *disk, int partno)
184{
807d4af2 185 struct hd_struct *part;
540eed56 186
e71bf0d0 187 rcu_read_lock();
807d4af2
CH
188 part = __disk_get_part(disk, partno);
189 if (part)
190 get_device(part_to_dev(part));
e71bf0d0
TH
191 rcu_read_unlock();
192
193 return part;
194}
e71bf0d0
TH
195
196/**
197 * disk_part_iter_init - initialize partition iterator
198 * @piter: iterator to initialize
199 * @disk: disk to iterate over
200 * @flags: DISK_PITER_* flags
201 *
202 * Initialize @piter so that it iterates over partitions of @disk.
203 *
204 * CONTEXT:
205 * Don't care.
206 */
207void disk_part_iter_init(struct disk_part_iter *piter, struct gendisk *disk,
208 unsigned int flags)
209{
540eed56
TH
210 struct disk_part_tbl *ptbl;
211
212 rcu_read_lock();
213 ptbl = rcu_dereference(disk->part_tbl);
214
e71bf0d0
TH
215 piter->disk = disk;
216 piter->part = NULL;
217
218 if (flags & DISK_PITER_REVERSE)
540eed56 219 piter->idx = ptbl->len - 1;
71982a40 220 else if (flags & (DISK_PITER_INCL_PART0 | DISK_PITER_INCL_EMPTY_PART0))
e71bf0d0 221 piter->idx = 0;
b5d0b9df
TH
222 else
223 piter->idx = 1;
e71bf0d0
TH
224
225 piter->flags = flags;
540eed56
TH
226
227 rcu_read_unlock();
e71bf0d0
TH
228}
229EXPORT_SYMBOL_GPL(disk_part_iter_init);
230
231/**
232 * disk_part_iter_next - proceed iterator to the next partition and return it
233 * @piter: iterator of interest
234 *
235 * Proceed @piter to the next partition and return it.
236 *
237 * CONTEXT:
238 * Don't care.
239 */
240struct hd_struct *disk_part_iter_next(struct disk_part_iter *piter)
241{
540eed56 242 struct disk_part_tbl *ptbl;
e71bf0d0
TH
243 int inc, end;
244
245 /* put the last partition */
e79319af 246 disk_part_iter_exit(piter);
e71bf0d0 247
540eed56 248 /* get part_tbl */
e71bf0d0 249 rcu_read_lock();
540eed56 250 ptbl = rcu_dereference(piter->disk->part_tbl);
e71bf0d0
TH
251
252 /* determine iteration parameters */
253 if (piter->flags & DISK_PITER_REVERSE) {
254 inc = -1;
71982a40
TH
255 if (piter->flags & (DISK_PITER_INCL_PART0 |
256 DISK_PITER_INCL_EMPTY_PART0))
b5d0b9df
TH
257 end = -1;
258 else
259 end = 0;
e71bf0d0
TH
260 } else {
261 inc = 1;
540eed56 262 end = ptbl->len;
e71bf0d0
TH
263 }
264
265 /* iterate to the next partition */
266 for (; piter->idx != end; piter->idx += inc) {
267 struct hd_struct *part;
268
540eed56 269 part = rcu_dereference(ptbl->part[piter->idx]);
e71bf0d0
TH
270 if (!part)
271 continue;
a782483c 272 if (!bdev_nr_sectors(part->bdev) &&
71982a40
TH
273 !(piter->flags & DISK_PITER_INCL_EMPTY) &&
274 !(piter->flags & DISK_PITER_INCL_EMPTY_PART0 &&
275 piter->idx == 0))
e71bf0d0
TH
276 continue;
277
ed9e1982 278 get_device(part_to_dev(part));
e71bf0d0
TH
279 piter->part = part;
280 piter->idx += inc;
281 break;
282 }
283
284 rcu_read_unlock();
285
286 return piter->part;
287}
288EXPORT_SYMBOL_GPL(disk_part_iter_next);
289
290/**
291 * disk_part_iter_exit - finish up partition iteration
292 * @piter: iter of interest
293 *
294 * Called when iteration is over. Cleans up @piter.
295 *
296 * CONTEXT:
297 * Don't care.
298 */
299void disk_part_iter_exit(struct disk_part_iter *piter)
300{
301 disk_put_part(piter->part);
302 piter->part = NULL;
303}
304EXPORT_SYMBOL_GPL(disk_part_iter_exit);
305
a6f23657
JA
306static inline int sector_in_part(struct hd_struct *part, sector_t sector)
307{
29ff57c6
CH
308 return part->bdev->bd_start_sect <= sector &&
309 sector < part->bdev->bd_start_sect + bdev_nr_sectors(part->bdev);
a6f23657
JA
310}
311
e71bf0d0
TH
312/**
313 * disk_map_sector_rcu - map sector to partition
314 * @disk: gendisk of interest
315 * @sector: sector to map
316 *
317 * Find out which partition @sector maps to on @disk. This is
318 * primarily used for stats accounting.
319 *
320 * CONTEXT:
b7d6c303 321 * RCU read locked. The returned partition pointer is always valid
27eb3af9
ML
322 * because its refcount is grabbed except for part0, which lifetime
323 * is same with the disk.
e71bf0d0
TH
324 *
325 * RETURNS:
074a7aca 326 * Found partition on success, part0 is returned if no partition matches
b7d6c303 327 * or the matched partition is being deleted.
e71bf0d0
TH
328 */
329struct hd_struct *disk_map_sector_rcu(struct gendisk *disk, sector_t sector)
330{
540eed56 331 struct disk_part_tbl *ptbl;
a6f23657 332 struct hd_struct *part;
e71bf0d0
TH
333 int i;
334
8ab1d40a 335 rcu_read_lock();
540eed56
TH
336 ptbl = rcu_dereference(disk->part_tbl);
337
a6f23657 338 part = rcu_dereference(ptbl->last_lookup);
b7d6c303 339 if (part && sector_in_part(part, sector) && hd_struct_try_get(part))
8ab1d40a 340 goto out_unlock;
a6f23657 341
540eed56 342 for (i = 1; i < ptbl->len; i++) {
a6f23657 343 part = rcu_dereference(ptbl->part[i]);
e71bf0d0 344
a6f23657 345 if (part && sector_in_part(part, sector)) {
b7d6c303
ML
346 /*
347 * only live partition can be cached for lookup,
348 * so use-after-free on cached & deleting partition
349 * can be avoided
350 */
351 if (!hd_struct_try_get(part))
352 break;
a6f23657 353 rcu_assign_pointer(ptbl->last_lookup, part);
8ab1d40a 354 goto out_unlock;
a6f23657 355 }
e71bf0d0 356 }
8ab1d40a
KK
357
358 part = &disk->part0;
359out_unlock:
360 rcu_read_unlock();
361 return part;
e71bf0d0 362}
e71bf0d0 363
b53df2e7
SK
364/**
365 * disk_has_partitions
366 * @disk: gendisk of interest
367 *
368 * Walk through the partition table and check if valid partition exists.
369 *
370 * CONTEXT:
371 * Don't care.
372 *
373 * RETURNS:
374 * True if the gendisk has at least one valid non-zero size partition.
375 * Otherwise false.
376 */
377bool disk_has_partitions(struct gendisk *disk)
378{
379 struct disk_part_tbl *ptbl;
380 int i;
381 bool ret = false;
382
383 rcu_read_lock();
384 ptbl = rcu_dereference(disk->part_tbl);
385
386 /* Iterate partitions skipping the whole device at index 0 */
387 for (i = 1; i < ptbl->len; i++) {
388 if (rcu_dereference(ptbl->part[i])) {
389 ret = true;
390 break;
391 }
392 }
393
394 rcu_read_unlock();
395
396 return ret;
397}
398EXPORT_SYMBOL_GPL(disk_has_partitions);
399
1da177e4
LT
400/*
401 * Can be deleted altogether. Later.
402 *
403 */
133d55cd 404#define BLKDEV_MAJOR_HASH_SIZE 255
1da177e4
LT
405static struct blk_major_name {
406 struct blk_major_name *next;
407 int major;
408 char name[16];
a160c615 409 void (*probe)(dev_t devt);
68eef3b4 410} *major_names[BLKDEV_MAJOR_HASH_SIZE];
e49fbbbf 411static DEFINE_MUTEX(major_names_lock);
1da177e4
LT
412
413/* index in the above - for now: assume no multimajor ranges */
e61eb2e9 414static inline int major_to_index(unsigned major)
1da177e4 415{
68eef3b4 416 return major % BLKDEV_MAJOR_HASH_SIZE;
7170be5f
NH
417}
418
68eef3b4 419#ifdef CONFIG_PROC_FS
cf771cb5 420void blkdev_show(struct seq_file *seqf, off_t offset)
7170be5f 421{
68eef3b4 422 struct blk_major_name *dp;
7170be5f 423
e49fbbbf 424 mutex_lock(&major_names_lock);
133d55cd
LG
425 for (dp = major_names[major_to_index(offset)]; dp; dp = dp->next)
426 if (dp->major == offset)
cf771cb5 427 seq_printf(seqf, "%3d %s\n", dp->major, dp->name);
e49fbbbf 428 mutex_unlock(&major_names_lock);
1da177e4 429}
68eef3b4 430#endif /* CONFIG_PROC_FS */
1da177e4 431
9e8c0bcc 432/**
e2b6b301 433 * __register_blkdev - register a new block device
9e8c0bcc 434 *
f33ff110
SB
435 * @major: the requested major device number [1..BLKDEV_MAJOR_MAX-1]. If
436 * @major = 0, try to allocate any unused major number.
9e8c0bcc 437 * @name: the name of the new block device as a zero terminated string
e2b6b301 438 * @probe: allback that is called on access to any minor number of @major
9e8c0bcc
MN
439 *
440 * The @name must be unique within the system.
441 *
0e056eb5
MCC
442 * The return value depends on the @major input parameter:
443 *
f33ff110
SB
444 * - if a major device number was requested in range [1..BLKDEV_MAJOR_MAX-1]
445 * then the function returns zero on success, or a negative error code
0e056eb5 446 * - if any unused major number was requested with @major = 0 parameter
9e8c0bcc 447 * then the return value is the allocated major number in range
f33ff110
SB
448 * [1..BLKDEV_MAJOR_MAX-1] or a negative error code otherwise
449 *
450 * See Documentation/admin-guide/devices.txt for the list of allocated
451 * major numbers.
e2b6b301
CH
452 *
453 * Use register_blkdev instead for any new code.
9e8c0bcc 454 */
a160c615
CH
455int __register_blkdev(unsigned int major, const char *name,
456 void (*probe)(dev_t devt))
1da177e4
LT
457{
458 struct blk_major_name **n, *p;
459 int index, ret = 0;
460
e49fbbbf 461 mutex_lock(&major_names_lock);
1da177e4
LT
462
463 /* temporary */
464 if (major == 0) {
465 for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) {
466 if (major_names[index] == NULL)
467 break;
468 }
469
470 if (index == 0) {
dfc76d11
KP
471 printk("%s: failed to get major for %s\n",
472 __func__, name);
1da177e4
LT
473 ret = -EBUSY;
474 goto out;
475 }
476 major = index;
477 ret = major;
478 }
479
133d55cd 480 if (major >= BLKDEV_MAJOR_MAX) {
dfc76d11
KP
481 pr_err("%s: major requested (%u) is greater than the maximum (%u) for %s\n",
482 __func__, major, BLKDEV_MAJOR_MAX-1, name);
133d55cd
LG
483
484 ret = -EINVAL;
485 goto out;
486 }
487
1da177e4
LT
488 p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL);
489 if (p == NULL) {
490 ret = -ENOMEM;
491 goto out;
492 }
493
494 p->major = major;
a160c615 495 p->probe = probe;
1da177e4
LT
496 strlcpy(p->name, name, sizeof(p->name));
497 p->next = NULL;
498 index = major_to_index(major);
499
500 for (n = &major_names[index]; *n; n = &(*n)->next) {
501 if ((*n)->major == major)
502 break;
503 }
504 if (!*n)
505 *n = p;
506 else
507 ret = -EBUSY;
508
509 if (ret < 0) {
f33ff110 510 printk("register_blkdev: cannot get major %u for %s\n",
1da177e4
LT
511 major, name);
512 kfree(p);
513 }
514out:
e49fbbbf 515 mutex_unlock(&major_names_lock);
1da177e4
LT
516 return ret;
517}
a160c615 518EXPORT_SYMBOL(__register_blkdev);
1da177e4 519
f4480240 520void unregister_blkdev(unsigned int major, const char *name)
1da177e4
LT
521{
522 struct blk_major_name **n;
523 struct blk_major_name *p = NULL;
524 int index = major_to_index(major);
1da177e4 525
e49fbbbf 526 mutex_lock(&major_names_lock);
1da177e4
LT
527 for (n = &major_names[index]; *n; n = &(*n)->next)
528 if ((*n)->major == major)
529 break;
294462a5
AM
530 if (!*n || strcmp((*n)->name, name)) {
531 WARN_ON(1);
294462a5 532 } else {
1da177e4
LT
533 p = *n;
534 *n = p->next;
535 }
e49fbbbf 536 mutex_unlock(&major_names_lock);
1da177e4 537 kfree(p);
1da177e4
LT
538}
539
540EXPORT_SYMBOL(unregister_blkdev);
541
870d6656
TH
542/**
543 * blk_mangle_minor - scatter minor numbers apart
544 * @minor: minor number to mangle
545 *
546 * Scatter consecutively allocated @minor number apart if MANGLE_DEVT
547 * is enabled. Mangling twice gives the original value.
548 *
549 * RETURNS:
550 * Mangled value.
551 *
552 * CONTEXT:
553 * Don't care.
554 */
555static int blk_mangle_minor(int minor)
556{
557#ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT
558 int i;
559
560 for (i = 0; i < MINORBITS / 2; i++) {
561 int low = minor & (1 << i);
562 int high = minor & (1 << (MINORBITS - 1 - i));
563 int distance = MINORBITS - 1 - 2 * i;
564
565 minor ^= low | high; /* clear both bits */
566 low <<= distance; /* swap the positions */
567 high >>= distance;
568 minor |= low | high; /* and set */
569 }
570#endif
571 return minor;
572}
573
bcce3de1
TH
574/**
575 * blk_alloc_devt - allocate a dev_t for a partition
576 * @part: partition to allocate dev_t for
bcce3de1
TH
577 * @devt: out parameter for resulting dev_t
578 *
579 * Allocate a dev_t for block device.
580 *
581 * RETURNS:
582 * 0 on success, allocated dev_t is returned in *@devt. -errno on
583 * failure.
584 *
585 * CONTEXT:
586 * Might sleep.
587 */
588int blk_alloc_devt(struct hd_struct *part, dev_t *devt)
589{
590 struct gendisk *disk = part_to_disk(part);
bab998d6 591 int idx;
bcce3de1
TH
592
593 /* in consecutive minor range? */
594 if (part->partno < disk->minors) {
595 *devt = MKDEV(disk->major, disk->first_minor + part->partno);
596 return 0;
597 }
598
22ae8ce8 599 idx = ida_alloc_range(&ext_devt_ida, 0, NR_EXT_DEVT, GFP_KERNEL);
bab998d6
TH
600 if (idx < 0)
601 return idx == -ENOSPC ? -EBUSY : idx;
bcce3de1 602
870d6656 603 *devt = MKDEV(BLOCK_EXT_MAJOR, blk_mangle_minor(idx));
bcce3de1
TH
604 return 0;
605}
606
607/**
608 * blk_free_devt - free a dev_t
609 * @devt: dev_t to free
610 *
611 * Free @devt which was allocated using blk_alloc_devt().
612 *
613 * CONTEXT:
614 * Might sleep.
615 */
616void blk_free_devt(dev_t devt)
617{
22ae8ce8
CH
618 if (MAJOR(devt) == BLOCK_EXT_MAJOR)
619 ida_free(&ext_devt_ida, blk_mangle_minor(MINOR(devt)));
6fcc44d1
YY
620}
621
1f014290
TH
622static char *bdevt_str(dev_t devt, char *buf)
623{
624 if (MAJOR(devt) <= 0xff && MINOR(devt) <= 0xff) {
625 char tbuf[BDEVT_SIZE];
626 snprintf(tbuf, BDEVT_SIZE, "%02x%02x", MAJOR(devt), MINOR(devt));
627 snprintf(buf, BDEVT_SIZE, "%-9s", tbuf);
628 } else
629 snprintf(buf, BDEVT_SIZE, "%03x:%05x", MAJOR(devt), MINOR(devt));
630
631 return buf;
632}
633
9301fe73
CH
634static void disk_scan_partitions(struct gendisk *disk)
635{
636 struct block_device *bdev;
637
638 if (!get_capacity(disk) || !disk_part_scan_enabled(disk))
639 return;
640
641 set_bit(GD_NEED_PART_SCAN, &disk->state);
642 bdev = blkdev_get_by_dev(disk_devt(disk), FMODE_READ, NULL);
643 if (!IS_ERR(bdev))
644 blkdev_put(bdev, FMODE_READ);
645}
646
fef912bf
HR
647static void register_disk(struct device *parent, struct gendisk *disk,
648 const struct attribute_group **groups)
d2bf1b67
TH
649{
650 struct device *ddev = disk_to_dev(disk);
d2bf1b67
TH
651 struct disk_part_iter piter;
652 struct hd_struct *part;
653 int err;
654
e63a46be 655 ddev->parent = parent;
d2bf1b67 656
ffc8b308 657 dev_set_name(ddev, "%s", disk->disk_name);
d2bf1b67
TH
658
659 /* delay uevents, until we scanned partition table */
660 dev_set_uevent_suppress(ddev, 1);
661
fef912bf
HR
662 if (groups) {
663 WARN_ON(ddev->groups);
664 ddev->groups = groups;
665 }
d2bf1b67
TH
666 if (device_add(ddev))
667 return;
668 if (!sysfs_deprecated) {
669 err = sysfs_create_link(block_depr, &ddev->kobj,
670 kobject_name(&ddev->kobj));
671 if (err) {
672 device_del(ddev);
673 return;
674 }
675 }
25e823c8
ML
676
677 /*
678 * avoid probable deadlock caused by allocating memory with
679 * GFP_KERNEL in runtime_resume callback of its all ancestor
680 * devices
681 */
682 pm_runtime_set_memalloc_noio(ddev, true);
683
1bdd5ae0
CH
684 disk->part0.bdev->bd_holder_dir =
685 kobject_create_and_add("holders", &ddev->kobj);
d2bf1b67
TH
686 disk->slave_dir = kobject_create_and_add("slaves", &ddev->kobj);
687
8ddcd653
CH
688 if (disk->flags & GENHD_FL_HIDDEN) {
689 dev_set_uevent_suppress(ddev, 0);
690 return;
691 }
692
9301fe73 693 disk_scan_partitions(disk);
d2bf1b67 694
d2bf1b67
TH
695 /* announce disk after possible partitions are created */
696 dev_set_uevent_suppress(ddev, 0);
697 kobject_uevent(&ddev->kobj, KOBJ_ADD);
698
699 /* announce possible partitions */
700 disk_part_iter_init(&piter, disk, 0);
701 while ((part = disk_part_iter_next(&piter)))
702 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_ADD);
703 disk_part_iter_exit(&piter);
8ddcd653 704
4d7c1d3f 705 if (disk->queue->backing_dev_info->dev) {
706 err = sysfs_create_link(&ddev->kobj,
707 &disk->queue->backing_dev_info->dev->kobj,
708 "bdi");
709 WARN_ON(err);
710 }
d2bf1b67
TH
711}
712
1da177e4 713/**
fa70d2e2 714 * __device_add_disk - add disk information to kernel list
e63a46be 715 * @parent: parent device for the disk
1da177e4 716 * @disk: per-device partitioning information
fef912bf 717 * @groups: Additional per-device sysfs groups
fa70d2e2 718 * @register_queue: register the queue if set to true
1da177e4
LT
719 *
720 * This function registers the partitioning information in @disk
721 * with the kernel.
3e1a7ff8
TH
722 *
723 * FIXME: error handling
1da177e4 724 */
fa70d2e2 725static void __device_add_disk(struct device *parent, struct gendisk *disk,
fef912bf 726 const struct attribute_group **groups,
fa70d2e2 727 bool register_queue)
1da177e4 728{
3e1a7ff8 729 dev_t devt;
6ffeea77 730 int retval;
cf0ca9fe 731
737eb78e
DLM
732 /*
733 * The disk queue should now be all set with enough information about
734 * the device for the elevator code to pick an adequate default
735 * elevator if one is needed, that is, for devices requesting queue
736 * registration.
737 */
738 if (register_queue)
739 elevator_init_mq(disk->queue);
740
3e1a7ff8
TH
741 /* minors == 0 indicates to use ext devt from part0 and should
742 * be accompanied with EXT_DEVT flag. Make sure all
743 * parameters make sense.
744 */
745 WARN_ON(disk->minors && !(disk->major || disk->first_minor));
8ddcd653
CH
746 WARN_ON(!disk->minors &&
747 !(disk->flags & (GENHD_FL_EXT_DEVT | GENHD_FL_HIDDEN)));
3e1a7ff8 748
1da177e4 749 disk->flags |= GENHD_FL_UP;
3e1a7ff8
TH
750
751 retval = blk_alloc_devt(&disk->part0, &devt);
752 if (retval) {
753 WARN_ON(1);
754 return;
755 }
3e1a7ff8
TH
756 disk->major = MAJOR(devt);
757 disk->first_minor = MINOR(devt);
758
9f53d2fe
SG
759 disk_alloc_events(disk);
760
8ddcd653
CH
761 if (disk->flags & GENHD_FL_HIDDEN) {
762 /*
763 * Don't let hidden disks show up in /proc/partitions,
764 * and don't bother scanning for partitions either.
765 */
766 disk->flags |= GENHD_FL_SUPPRESS_PARTITION_INFO;
767 disk->flags |= GENHD_FL_NO_PART_SCAN;
768 } else {
3c5d202b
CH
769 struct backing_dev_info *bdi = disk->queue->backing_dev_info;
770 struct device *dev = disk_to_dev(disk);
3a92168b 771 int ret;
772
8ddcd653 773 /* Register BDI before referencing it from bdev */
3c5d202b
CH
774 dev->devt = devt;
775 ret = bdi_register(bdi, "%u:%u", MAJOR(devt), MINOR(devt));
3a92168b 776 WARN_ON(ret);
3c5d202b 777 bdi_set_owner(bdi, dev);
22ae8ce8 778 bdev_add(disk->part0.bdev, devt);
8ddcd653 779 }
fef912bf 780 register_disk(parent, disk, groups);
fa70d2e2
MS
781 if (register_queue)
782 blk_register_queue(disk);
cf0ca9fe 783
523e1d39
TH
784 /*
785 * Take an extra ref on queue which will be put on disk_release()
786 * so that it sticks around as long as @disk is there.
787 */
09ac46c4 788 WARN_ON_ONCE(!blk_get_queue(disk->queue));
523e1d39 789
77ea887e 790 disk_add_events(disk);
25520d55 791 blk_integrity_add(disk);
1da177e4 792}
fa70d2e2 793
fef912bf
HR
794void device_add_disk(struct device *parent, struct gendisk *disk,
795 const struct attribute_group **groups)
796
fa70d2e2 797{
fef912bf 798 __device_add_disk(parent, disk, groups, true);
fa70d2e2 799}
e63a46be 800EXPORT_SYMBOL(device_add_disk);
1da177e4 801
fa70d2e2
MS
802void device_add_disk_no_queue_reg(struct device *parent, struct gendisk *disk)
803{
fef912bf 804 __device_add_disk(parent, disk, NULL, false);
fa70d2e2
MS
805}
806EXPORT_SYMBOL(device_add_disk_no_queue_reg);
807
02d33b67
CH
808static void invalidate_partition(struct gendisk *disk, int partno)
809{
810 struct block_device *bdev;
811
812 bdev = bdget_disk(disk, partno);
813 if (!bdev)
814 return;
815
816 fsync_bdev(bdev);
817 __invalidate_device(bdev, true);
9bc5c397
CH
818
819 /*
22ae8ce8
CH
820 * Unhash the bdev inode for this device so that it can't be looked
821 * up any more even if openers still hold references to it.
9bc5c397
CH
822 */
823 remove_inode_hash(bdev->bd_inode);
02d33b67
CH
824 bdput(bdev);
825}
826
b5bd357c
LC
827/**
828 * del_gendisk - remove the gendisk
829 * @disk: the struct gendisk to remove
830 *
831 * Removes the gendisk and all its associated resources. This deletes the
832 * partitions associated with the gendisk, and unregisters the associated
833 * request_queue.
834 *
835 * This is the counter to the respective __device_add_disk() call.
836 *
837 * The final removal of the struct gendisk happens when its refcount reaches 0
838 * with put_disk(), which should be called after del_gendisk(), if
839 * __device_add_disk() was used.
e8c7d14a
LC
840 *
841 * Drivers exist which depend on the release of the gendisk to be synchronous,
842 * it should not be deferred.
843 *
844 * Context: can sleep
b5bd357c 845 */
d2bf1b67 846void del_gendisk(struct gendisk *disk)
1da177e4 847{
d2bf1b67
TH
848 struct disk_part_iter piter;
849 struct hd_struct *part;
850
e8c7d14a
LC
851 might_sleep();
852
6b3ba976
CH
853 if (WARN_ON_ONCE(!disk->queue))
854 return;
855
25520d55 856 blk_integrity_del(disk);
77ea887e
TH
857 disk_del_events(disk);
858
56c0908c
JK
859 /*
860 * Block lookups of the disk until all bdevs are unhashed and the
861 * disk is marked as dead (GENHD_FL_UP cleared).
862 */
22ae8ce8
CH
863 down_write(&bdev_lookup_sem);
864
d2bf1b67
TH
865 /* invalidate stuff */
866 disk_part_iter_init(&piter, disk,
867 DISK_PITER_INCL_EMPTY | DISK_PITER_REVERSE);
868 while ((part = disk_part_iter_next(&piter))) {
869 invalidate_partition(disk, part->partno);
8328eb28 870 delete_partition(part);
d2bf1b67
TH
871 }
872 disk_part_iter_exit(&piter);
873
874 invalidate_partition(disk, 0);
d2bf1b67
TH
875 set_capacity(disk, 0);
876 disk->flags &= ~GENHD_FL_UP;
22ae8ce8 877 up_write(&bdev_lookup_sem);
d2bf1b67 878
6b3ba976 879 if (!(disk->flags & GENHD_FL_HIDDEN)) {
8ddcd653 880 sysfs_remove_link(&disk_to_dev(disk)->kobj, "bdi");
6b3ba976 881
90f16fdd
JK
882 /*
883 * Unregister bdi before releasing device numbers (as they can
884 * get reused and we'd get clashes in sysfs).
885 */
6b3ba976 886 bdi_unregister(disk->queue->backing_dev_info);
90f16fdd 887 }
d2bf1b67 888
6b3ba976 889 blk_unregister_queue(disk);
d2bf1b67 890
1bdd5ae0 891 kobject_put(disk->part0.bdev->bd_holder_dir);
d2bf1b67 892 kobject_put(disk->slave_dir);
d2bf1b67
TH
893
894 part_stat_set_all(&disk->part0, 0);
15e3d2c5 895 disk->part0.bdev->bd_stamp = 0;
d2bf1b67
TH
896 if (!sysfs_deprecated)
897 sysfs_remove_link(block_depr, dev_name(disk_to_dev(disk)));
25e823c8 898 pm_runtime_set_memalloc_noio(disk_to_dev(disk), false);
d2bf1b67 899 device_del(disk_to_dev(disk));
1da177e4 900}
d2bf1b67 901EXPORT_SYMBOL(del_gendisk);
1da177e4 902
99e6608c
VV
903/* sysfs access to bad-blocks list. */
904static ssize_t disk_badblocks_show(struct device *dev,
905 struct device_attribute *attr,
906 char *page)
907{
908 struct gendisk *disk = dev_to_disk(dev);
909
910 if (!disk->bb)
911 return sprintf(page, "\n");
912
913 return badblocks_show(disk->bb, page, 0);
914}
915
916static ssize_t disk_badblocks_store(struct device *dev,
917 struct device_attribute *attr,
918 const char *page, size_t len)
919{
920 struct gendisk *disk = dev_to_disk(dev);
921
922 if (!disk->bb)
923 return -ENXIO;
924
925 return badblocks_store(disk->bb, page, len, 0);
926}
927
22ae8ce8 928void blk_request_module(dev_t devt)
bd8eff3b 929{
a160c615
CH
930 unsigned int major = MAJOR(devt);
931 struct blk_major_name **n;
932
933 mutex_lock(&major_names_lock);
934 for (n = &major_names[major_to_index(major)]; *n; n = &(*n)->next) {
935 if ((*n)->major == major && (*n)->probe) {
936 (*n)->probe(devt);
937 mutex_unlock(&major_names_lock);
938 return;
939 }
940 }
941 mutex_unlock(&major_names_lock);
942
bd8eff3b
CH
943 if (request_module("block-major-%d-%d", MAJOR(devt), MINOR(devt)) > 0)
944 /* Make old-style 2.4 aliases work */
945 request_module("block-major-%d", MAJOR(devt));
946}
947
f331c029
TH
948/**
949 * bdget_disk - do bdget() by gendisk and partition number
950 * @disk: gendisk of interest
951 * @partno: partition number
952 *
953 * Find partition @partno from @disk, do bdget() on it.
954 *
955 * CONTEXT:
956 * Don't care.
957 *
958 * RETURNS:
959 * Resulting block_device on success, NULL on failure.
960 */
aeb3d3a8 961struct block_device *bdget_disk(struct gendisk *disk, int partno)
f331c029 962{
548b10eb
TH
963 struct hd_struct *part;
964 struct block_device *bdev = NULL;
f331c029 965
548b10eb 966 part = disk_get_part(disk, partno);
2bbedcb4 967 if (part)
10ed1666 968 bdev = bdget_part(part);
548b10eb 969 disk_put_part(part);
f331c029 970
548b10eb 971 return bdev;
f331c029
TH
972}
973EXPORT_SYMBOL(bdget_disk);
974
5c6f35c5
GKH
975/*
976 * print a full list of all partitions - intended for places where the root
977 * filesystem can't be mounted and thus to give the victim some idea of what
978 * went wrong
979 */
980void __init printk_all_partitions(void)
981{
def4e38d
TH
982 struct class_dev_iter iter;
983 struct device *dev;
984
985 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
986 while ((dev = class_dev_iter_next(&iter))) {
987 struct gendisk *disk = dev_to_disk(dev);
e71bf0d0
TH
988 struct disk_part_iter piter;
989 struct hd_struct *part;
1f014290
TH
990 char name_buf[BDEVNAME_SIZE];
991 char devt_buf[BDEVT_SIZE];
def4e38d
TH
992
993 /*
994 * Don't show empty devices or things that have been
25985edc 995 * suppressed
def4e38d
TH
996 */
997 if (get_capacity(disk) == 0 ||
998 (disk->flags & GENHD_FL_SUPPRESS_PARTITION_INFO))
999 continue;
1000
1001 /*
1002 * Note, unlike /proc/partitions, I am showing the
1003 * numbers in hex - the same format as the root=
1004 * option takes.
1005 */
074a7aca
TH
1006 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
1007 while ((part = disk_part_iter_next(&piter))) {
1008 bool is_part0 = part == &disk->part0;
def4e38d 1009
b5af921e 1010 printk("%s%s %10llu %s %s", is_part0 ? "" : " ",
1f014290 1011 bdevt_str(part_devt(part), devt_buf),
a782483c
CH
1012 bdev_nr_sectors(part->bdev) >> 1,
1013 disk_name(disk, part->partno, name_buf),
231926db
CH
1014 part->bdev->bd_meta_info ?
1015 part->bdev->bd_meta_info->uuid : "");
074a7aca 1016 if (is_part0) {
52c44d93 1017 if (dev->parent && dev->parent->driver)
074a7aca 1018 printk(" driver: %s\n",
52c44d93 1019 dev->parent->driver->name);
074a7aca
TH
1020 else
1021 printk(" (driver?)\n");
1022 } else
1023 printk("\n");
1024 }
e71bf0d0 1025 disk_part_iter_exit(&piter);
def4e38d
TH
1026 }
1027 class_dev_iter_exit(&iter);
dd2a345f
DG
1028}
1029
1da177e4
LT
1030#ifdef CONFIG_PROC_FS
1031/* iterator */
def4e38d 1032static void *disk_seqf_start(struct seq_file *seqf, loff_t *pos)
68c4d4a7 1033{
def4e38d
TH
1034 loff_t skip = *pos;
1035 struct class_dev_iter *iter;
1036 struct device *dev;
68c4d4a7 1037
aeb3d3a8 1038 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
def4e38d
TH
1039 if (!iter)
1040 return ERR_PTR(-ENOMEM);
1041
1042 seqf->private = iter;
1043 class_dev_iter_init(iter, &block_class, NULL, &disk_type);
1044 do {
1045 dev = class_dev_iter_next(iter);
1046 if (!dev)
1047 return NULL;
1048 } while (skip--);
1049
1050 return dev_to_disk(dev);
68c4d4a7
GKH
1051}
1052
def4e38d 1053static void *disk_seqf_next(struct seq_file *seqf, void *v, loff_t *pos)
1da177e4 1054{
edfaa7c3 1055 struct device *dev;
1da177e4 1056
def4e38d
TH
1057 (*pos)++;
1058 dev = class_dev_iter_next(seqf->private);
2ac3cee5 1059 if (dev)
68c4d4a7 1060 return dev_to_disk(dev);
2ac3cee5 1061
1da177e4
LT
1062 return NULL;
1063}
1064
def4e38d 1065static void disk_seqf_stop(struct seq_file *seqf, void *v)
27f30251 1066{
def4e38d 1067 struct class_dev_iter *iter = seqf->private;
27f30251 1068
def4e38d
TH
1069 /* stop is called even after start failed :-( */
1070 if (iter) {
1071 class_dev_iter_exit(iter);
1072 kfree(iter);
77da1605 1073 seqf->private = NULL;
5c0ef6d0 1074 }
1da177e4
LT
1075}
1076
def4e38d 1077static void *show_partition_start(struct seq_file *seqf, loff_t *pos)
1da177e4 1078{
06768067 1079 void *p;
def4e38d
TH
1080
1081 p = disk_seqf_start(seqf, pos);
b9f985b6 1082 if (!IS_ERR_OR_NULL(p) && !*pos)
def4e38d
TH
1083 seq_puts(seqf, "major minor #blocks name\n\n");
1084 return p;
1da177e4
LT
1085}
1086
cf771cb5 1087static int show_partition(struct seq_file *seqf, void *v)
1da177e4
LT
1088{
1089 struct gendisk *sgp = v;
e71bf0d0
TH
1090 struct disk_part_iter piter;
1091 struct hd_struct *part;
1da177e4
LT
1092 char buf[BDEVNAME_SIZE];
1093
1da177e4 1094 /* Don't show non-partitionable removeable devices or empty devices */
d27769ec 1095 if (!get_capacity(sgp) || (!disk_max_parts(sgp) &&
f331c029 1096 (sgp->flags & GENHD_FL_REMOVABLE)))
1da177e4
LT
1097 return 0;
1098 if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)
1099 return 0;
1100
1101 /* show the full disk and all non-0 size partitions of it */
074a7aca 1102 disk_part_iter_init(&piter, sgp, DISK_PITER_INCL_PART0);
e71bf0d0 1103 while ((part = disk_part_iter_next(&piter)))
1f014290 1104 seq_printf(seqf, "%4d %7d %10llu %s\n",
f331c029 1105 MAJOR(part_devt(part)), MINOR(part_devt(part)),
a782483c 1106 bdev_nr_sectors(part->bdev) >> 1,
f331c029 1107 disk_name(sgp, part->partno, buf));
e71bf0d0 1108 disk_part_iter_exit(&piter);
1da177e4
LT
1109
1110 return 0;
1111}
1112
f500975a 1113static const struct seq_operations partitions_op = {
def4e38d
TH
1114 .start = show_partition_start,
1115 .next = disk_seqf_next,
1116 .stop = disk_seqf_stop,
edfaa7c3 1117 .show = show_partition
1da177e4
LT
1118};
1119#endif
1120
1da177e4
LT
1121static int __init genhd_device_init(void)
1122{
e105b8bf
DW
1123 int error;
1124
1125 block_class.dev_kobj = sysfs_dev_block_kobj;
1126 error = class_register(&block_class);
ee27a558
RM
1127 if (unlikely(error))
1128 return error;
1da177e4 1129 blk_dev_init();
edfaa7c3 1130
561ec68e
ZY
1131 register_blkdev(BLOCK_EXT_MAJOR, "blkext");
1132
edfaa7c3 1133 /* create top-level block dir */
e52eec13
AK
1134 if (!sysfs_deprecated)
1135 block_depr = kobject_create_and_add("block", NULL);
830d3cfb 1136 return 0;
1da177e4
LT
1137}
1138
1139subsys_initcall(genhd_device_init);
1140
edfaa7c3
KS
1141static ssize_t disk_range_show(struct device *dev,
1142 struct device_attribute *attr, char *buf)
1da177e4 1143{
edfaa7c3 1144 struct gendisk *disk = dev_to_disk(dev);
1da177e4 1145
edfaa7c3 1146 return sprintf(buf, "%d\n", disk->minors);
1da177e4
LT
1147}
1148
1f014290
TH
1149static ssize_t disk_ext_range_show(struct device *dev,
1150 struct device_attribute *attr, char *buf)
1151{
1152 struct gendisk *disk = dev_to_disk(dev);
1153
b5d0b9df 1154 return sprintf(buf, "%d\n", disk_max_parts(disk));
1f014290
TH
1155}
1156
edfaa7c3
KS
1157static ssize_t disk_removable_show(struct device *dev,
1158 struct device_attribute *attr, char *buf)
a7fd6706 1159{
edfaa7c3 1160 struct gendisk *disk = dev_to_disk(dev);
a7fd6706 1161
edfaa7c3
KS
1162 return sprintf(buf, "%d\n",
1163 (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
a7fd6706
KS
1164}
1165
8ddcd653
CH
1166static ssize_t disk_hidden_show(struct device *dev,
1167 struct device_attribute *attr, char *buf)
1168{
1169 struct gendisk *disk = dev_to_disk(dev);
1170
1171 return sprintf(buf, "%d\n",
1172 (disk->flags & GENHD_FL_HIDDEN ? 1 : 0));
1173}
1174
1c9ce527
KS
1175static ssize_t disk_ro_show(struct device *dev,
1176 struct device_attribute *attr, char *buf)
1177{
1178 struct gendisk *disk = dev_to_disk(dev);
1179
b7db9956 1180 return sprintf(buf, "%d\n", get_disk_ro(disk) ? 1 : 0);
1c9ce527
KS
1181}
1182
3ad5cee5
CH
1183ssize_t part_size_show(struct device *dev,
1184 struct device_attribute *attr, char *buf)
1185{
1186 struct hd_struct *p = dev_to_part(dev);
1187
a782483c 1188 return sprintf(buf, "%llu\n", bdev_nr_sectors(p->bdev));
3ad5cee5
CH
1189}
1190
1191ssize_t part_stat_show(struct device *dev,
1192 struct device_attribute *attr, char *buf)
1193{
1194 struct hd_struct *p = dev_to_part(dev);
1195 struct request_queue *q = part_to_disk(p)->queue;
ea18e0f0 1196 struct disk_stats stat;
3ad5cee5
CH
1197 unsigned int inflight;
1198
ea18e0f0 1199 part_stat_read_all(p, &stat);
b2f609e1
CH
1200 if (queue_is_mq(q))
1201 inflight = blk_mq_in_flight(q, p);
1202 else
1f06959b 1203 inflight = part_in_flight(p);
ea18e0f0 1204
3ad5cee5
CH
1205 return sprintf(buf,
1206 "%8lu %8lu %8llu %8u "
1207 "%8lu %8lu %8llu %8u "
1208 "%8u %8u %8u "
1209 "%8lu %8lu %8llu %8u "
1210 "%8lu %8u"
1211 "\n",
ea18e0f0
KK
1212 stat.ios[STAT_READ],
1213 stat.merges[STAT_READ],
1214 (unsigned long long)stat.sectors[STAT_READ],
1215 (unsigned int)div_u64(stat.nsecs[STAT_READ], NSEC_PER_MSEC),
1216 stat.ios[STAT_WRITE],
1217 stat.merges[STAT_WRITE],
1218 (unsigned long long)stat.sectors[STAT_WRITE],
1219 (unsigned int)div_u64(stat.nsecs[STAT_WRITE], NSEC_PER_MSEC),
3ad5cee5 1220 inflight,
ea18e0f0 1221 jiffies_to_msecs(stat.io_ticks),
8cd5b8fc
KK
1222 (unsigned int)div_u64(stat.nsecs[STAT_READ] +
1223 stat.nsecs[STAT_WRITE] +
1224 stat.nsecs[STAT_DISCARD] +
1225 stat.nsecs[STAT_FLUSH],
1226 NSEC_PER_MSEC),
ea18e0f0
KK
1227 stat.ios[STAT_DISCARD],
1228 stat.merges[STAT_DISCARD],
1229 (unsigned long long)stat.sectors[STAT_DISCARD],
1230 (unsigned int)div_u64(stat.nsecs[STAT_DISCARD], NSEC_PER_MSEC),
1231 stat.ios[STAT_FLUSH],
1232 (unsigned int)div_u64(stat.nsecs[STAT_FLUSH], NSEC_PER_MSEC));
3ad5cee5
CH
1233}
1234
1235ssize_t part_inflight_show(struct device *dev, struct device_attribute *attr,
1236 char *buf)
1237{
1238 struct hd_struct *p = dev_to_part(dev);
1239 struct request_queue *q = part_to_disk(p)->queue;
1240 unsigned int inflight[2];
1241
b2f609e1
CH
1242 if (queue_is_mq(q))
1243 blk_mq_in_flight_rw(q, p, inflight);
1244 else
1f06959b 1245 part_in_flight_rw(p, inflight);
b2f609e1 1246
3ad5cee5
CH
1247 return sprintf(buf, "%8u %8u\n", inflight[0], inflight[1]);
1248}
1249
edfaa7c3
KS
1250static ssize_t disk_capability_show(struct device *dev,
1251 struct device_attribute *attr, char *buf)
86ce18d7 1252{
edfaa7c3
KS
1253 struct gendisk *disk = dev_to_disk(dev);
1254
1255 return sprintf(buf, "%x\n", disk->flags);
86ce18d7 1256}
edfaa7c3 1257
c72758f3
MP
1258static ssize_t disk_alignment_offset_show(struct device *dev,
1259 struct device_attribute *attr,
1260 char *buf)
1261{
1262 struct gendisk *disk = dev_to_disk(dev);
1263
1264 return sprintf(buf, "%d\n", queue_alignment_offset(disk->queue));
1265}
1266
86b37281
MP
1267static ssize_t disk_discard_alignment_show(struct device *dev,
1268 struct device_attribute *attr,
1269 char *buf)
1270{
1271 struct gendisk *disk = dev_to_disk(dev);
1272
dd3d145d 1273 return sprintf(buf, "%d\n", queue_discard_alignment(disk->queue));
86b37281
MP
1274}
1275
5657a819
JP
1276static DEVICE_ATTR(range, 0444, disk_range_show, NULL);
1277static DEVICE_ATTR(ext_range, 0444, disk_ext_range_show, NULL);
1278static DEVICE_ATTR(removable, 0444, disk_removable_show, NULL);
1279static DEVICE_ATTR(hidden, 0444, disk_hidden_show, NULL);
1280static DEVICE_ATTR(ro, 0444, disk_ro_show, NULL);
1281static DEVICE_ATTR(size, 0444, part_size_show, NULL);
1282static DEVICE_ATTR(alignment_offset, 0444, disk_alignment_offset_show, NULL);
1283static DEVICE_ATTR(discard_alignment, 0444, disk_discard_alignment_show, NULL);
1284static DEVICE_ATTR(capability, 0444, disk_capability_show, NULL);
1285static DEVICE_ATTR(stat, 0444, part_stat_show, NULL);
1286static DEVICE_ATTR(inflight, 0444, part_inflight_show, NULL);
1287static DEVICE_ATTR(badblocks, 0644, disk_badblocks_show, disk_badblocks_store);
3ad5cee5 1288
c17bb495 1289#ifdef CONFIG_FAIL_MAKE_REQUEST
3ad5cee5
CH
1290ssize_t part_fail_show(struct device *dev,
1291 struct device_attribute *attr, char *buf)
1292{
1293 struct hd_struct *p = dev_to_part(dev);
1294
b309e993 1295 return sprintf(buf, "%d\n", p->bdev->bd_make_it_fail);
3ad5cee5
CH
1296}
1297
1298ssize_t part_fail_store(struct device *dev,
1299 struct device_attribute *attr,
1300 const char *buf, size_t count)
1301{
1302 struct hd_struct *p = dev_to_part(dev);
1303 int i;
1304
1305 if (count > 0 && sscanf(buf, "%d", &i) > 0)
b309e993 1306 p->bdev->bd_make_it_fail = i;
3ad5cee5
CH
1307
1308 return count;
1309}
1310
edfaa7c3 1311static struct device_attribute dev_attr_fail =
5657a819 1312 __ATTR(make-it-fail, 0644, part_fail_show, part_fail_store);
3ad5cee5
CH
1313#endif /* CONFIG_FAIL_MAKE_REQUEST */
1314
581d4e28
JA
1315#ifdef CONFIG_FAIL_IO_TIMEOUT
1316static struct device_attribute dev_attr_fail_timeout =
5657a819 1317 __ATTR(io-timeout-fail, 0644, part_timeout_show, part_timeout_store);
581d4e28 1318#endif
edfaa7c3
KS
1319
1320static struct attribute *disk_attrs[] = {
1321 &dev_attr_range.attr,
1f014290 1322 &dev_attr_ext_range.attr,
edfaa7c3 1323 &dev_attr_removable.attr,
8ddcd653 1324 &dev_attr_hidden.attr,
1c9ce527 1325 &dev_attr_ro.attr,
edfaa7c3 1326 &dev_attr_size.attr,
c72758f3 1327 &dev_attr_alignment_offset.attr,
86b37281 1328 &dev_attr_discard_alignment.attr,
edfaa7c3
KS
1329 &dev_attr_capability.attr,
1330 &dev_attr_stat.attr,
316d315b 1331 &dev_attr_inflight.attr,
99e6608c 1332 &dev_attr_badblocks.attr,
edfaa7c3
KS
1333#ifdef CONFIG_FAIL_MAKE_REQUEST
1334 &dev_attr_fail.attr,
581d4e28
JA
1335#endif
1336#ifdef CONFIG_FAIL_IO_TIMEOUT
1337 &dev_attr_fail_timeout.attr,
edfaa7c3
KS
1338#endif
1339 NULL
1340};
1341
9438b3e0
DW
1342static umode_t disk_visible(struct kobject *kobj, struct attribute *a, int n)
1343{
1344 struct device *dev = container_of(kobj, typeof(*dev), kobj);
1345 struct gendisk *disk = dev_to_disk(dev);
1346
1347 if (a == &dev_attr_badblocks.attr && !disk->bb)
1348 return 0;
1349 return a->mode;
1350}
1351
edfaa7c3
KS
1352static struct attribute_group disk_attr_group = {
1353 .attrs = disk_attrs,
9438b3e0 1354 .is_visible = disk_visible,
edfaa7c3
KS
1355};
1356
a4dbd674 1357static const struct attribute_group *disk_attr_groups[] = {
edfaa7c3
KS
1358 &disk_attr_group,
1359 NULL
1da177e4
LT
1360};
1361
540eed56
TH
1362/**
1363 * disk_replace_part_tbl - replace disk->part_tbl in RCU-safe way
1364 * @disk: disk to replace part_tbl for
1365 * @new_ptbl: new part_tbl to install
1366 *
1367 * Replace disk->part_tbl with @new_ptbl in RCU-safe way. The
1368 * original ptbl is freed using RCU callback.
1369 *
1370 * LOCKING:
6d2cf6f2 1371 * Matching bd_mutex locked or the caller is the only user of @disk.
540eed56
TH
1372 */
1373static void disk_replace_part_tbl(struct gendisk *disk,
1374 struct disk_part_tbl *new_ptbl)
1375{
6d2cf6f2
BVA
1376 struct disk_part_tbl *old_ptbl =
1377 rcu_dereference_protected(disk->part_tbl, 1);
540eed56
TH
1378
1379 rcu_assign_pointer(disk->part_tbl, new_ptbl);
a6f23657
JA
1380
1381 if (old_ptbl) {
1382 rcu_assign_pointer(old_ptbl->last_lookup, NULL);
57bdfbf9 1383 kfree_rcu(old_ptbl, rcu_head);
a6f23657 1384 }
540eed56
TH
1385}
1386
1387/**
1388 * disk_expand_part_tbl - expand disk->part_tbl
1389 * @disk: disk to expand part_tbl for
1390 * @partno: expand such that this partno can fit in
1391 *
1392 * Expand disk->part_tbl such that @partno can fit in. disk->part_tbl
1393 * uses RCU to allow unlocked dereferencing for stats and other stuff.
1394 *
1395 * LOCKING:
6d2cf6f2
BVA
1396 * Matching bd_mutex locked or the caller is the only user of @disk.
1397 * Might sleep.
540eed56
TH
1398 *
1399 * RETURNS:
1400 * 0 on success, -errno on failure.
1401 */
1402int disk_expand_part_tbl(struct gendisk *disk, int partno)
1403{
6d2cf6f2
BVA
1404 struct disk_part_tbl *old_ptbl =
1405 rcu_dereference_protected(disk->part_tbl, 1);
540eed56
TH
1406 struct disk_part_tbl *new_ptbl;
1407 int len = old_ptbl ? old_ptbl->len : 0;
5fabcb4c 1408 int i, target;
5fabcb4c
JA
1409
1410 /*
1411 * check for int overflow, since we can get here from blkpg_ioctl()
1412 * with a user passed 'partno'.
1413 */
1414 target = partno + 1;
1415 if (target < 0)
1416 return -EINVAL;
540eed56
TH
1417
1418 /* disk_max_parts() is zero during initialization, ignore if so */
1419 if (disk_max_parts(disk) && target > disk_max_parts(disk))
1420 return -EINVAL;
1421
1422 if (target <= len)
1423 return 0;
1424
78b90a2c
GS
1425 new_ptbl = kzalloc_node(struct_size(new_ptbl, part, target), GFP_KERNEL,
1426 disk->node_id);
540eed56
TH
1427 if (!new_ptbl)
1428 return -ENOMEM;
1429
540eed56
TH
1430 new_ptbl->len = target;
1431
1432 for (i = 0; i < len; i++)
1433 rcu_assign_pointer(new_ptbl->part[i], old_ptbl->part[i]);
1434
1435 disk_replace_part_tbl(disk, new_ptbl);
1436 return 0;
1437}
1438
b5bd357c
LC
1439/**
1440 * disk_release - releases all allocated resources of the gendisk
1441 * @dev: the device representing this disk
1442 *
1443 * This function releases all allocated resources of the gendisk.
1444 *
b5bd357c
LC
1445 * Drivers which used __device_add_disk() have a gendisk with a request_queue
1446 * assigned. Since the request_queue sits on top of the gendisk for these
1447 * drivers we also call blk_put_queue() for them, and we expect the
1448 * request_queue refcount to reach 0 at this point, and so the request_queue
1449 * will also be freed prior to the disk.
e8c7d14a
LC
1450 *
1451 * Context: can sleep
b5bd357c 1452 */
edfaa7c3 1453static void disk_release(struct device *dev)
1da177e4 1454{
edfaa7c3
KS
1455 struct gendisk *disk = dev_to_disk(dev);
1456
e8c7d14a
LC
1457 might_sleep();
1458
2da78092 1459 blk_free_devt(dev->devt);
77ea887e 1460 disk_release_events(disk);
1da177e4 1461 kfree(disk->random);
540eed56 1462 disk_replace_part_tbl(disk, NULL);
b54e5ed8 1463 hd_free_part(&disk->part0);
523e1d39
TH
1464 if (disk->queue)
1465 blk_put_queue(disk->queue);
1da177e4
LT
1466 kfree(disk);
1467}
edfaa7c3
KS
1468struct class block_class = {
1469 .name = "block",
1da177e4
LT
1470};
1471
3c2670e6 1472static char *block_devnode(struct device *dev, umode_t *mode,
4e4098a3 1473 kuid_t *uid, kgid_t *gid)
b03f38b6
KS
1474{
1475 struct gendisk *disk = dev_to_disk(dev);
1476
348e114b
CH
1477 if (disk->fops->devnode)
1478 return disk->fops->devnode(disk, mode);
b03f38b6
KS
1479 return NULL;
1480}
1481
ef45fe47 1482const struct device_type disk_type = {
edfaa7c3
KS
1483 .name = "disk",
1484 .groups = disk_attr_groups,
1485 .release = disk_release,
e454cea2 1486 .devnode = block_devnode,
1da177e4
LT
1487};
1488
a6e2ba88 1489#ifdef CONFIG_PROC_FS
cf771cb5
TH
1490/*
1491 * aggregate disk stat collector. Uses the same stats that the sysfs
1492 * entries do, above, but makes them available through one seq_file.
1493 *
1494 * The output looks suspiciously like /proc/partitions with a bunch of
1495 * extra fields.
1496 */
1497static int diskstats_show(struct seq_file *seqf, void *v)
1da177e4
LT
1498{
1499 struct gendisk *gp = v;
e71bf0d0
TH
1500 struct disk_part_iter piter;
1501 struct hd_struct *hd;
1da177e4 1502 char buf[BDEVNAME_SIZE];
e016b782 1503 unsigned int inflight;
ea18e0f0 1504 struct disk_stats stat;
1da177e4
LT
1505
1506 /*
ed9e1982 1507 if (&disk_to_dev(gp)->kobj.entry == block_class.devices.next)
cf771cb5 1508 seq_puts(seqf, "major minor name"
1da177e4
LT
1509 " rio rmerge rsect ruse wio wmerge "
1510 "wsect wuse running use aveq"
1511 "\n\n");
1512 */
9f5e4865 1513
71982a40 1514 disk_part_iter_init(&piter, gp, DISK_PITER_INCL_EMPTY_PART0);
e71bf0d0 1515 while ((hd = disk_part_iter_next(&piter))) {
ea18e0f0 1516 part_stat_read_all(hd, &stat);
b2f609e1
CH
1517 if (queue_is_mq(gp->queue))
1518 inflight = blk_mq_in_flight(gp->queue, hd);
1519 else
1f06959b 1520 inflight = part_in_flight(hd);
ea18e0f0 1521
bdca3c87
MC
1522 seq_printf(seqf, "%4d %7d %s "
1523 "%lu %lu %lu %u "
1524 "%lu %lu %lu %u "
1525 "%u %u %u "
b6866318
KK
1526 "%lu %lu %lu %u "
1527 "%lu %u"
1528 "\n",
f331c029
TH
1529 MAJOR(part_devt(hd)), MINOR(part_devt(hd)),
1530 disk_name(gp, hd->partno, buf),
ea18e0f0
KK
1531 stat.ios[STAT_READ],
1532 stat.merges[STAT_READ],
1533 stat.sectors[STAT_READ],
1534 (unsigned int)div_u64(stat.nsecs[STAT_READ],
1535 NSEC_PER_MSEC),
1536 stat.ios[STAT_WRITE],
1537 stat.merges[STAT_WRITE],
1538 stat.sectors[STAT_WRITE],
1539 (unsigned int)div_u64(stat.nsecs[STAT_WRITE],
1540 NSEC_PER_MSEC),
e016b782 1541 inflight,
ea18e0f0 1542 jiffies_to_msecs(stat.io_ticks),
8cd5b8fc
KK
1543 (unsigned int)div_u64(stat.nsecs[STAT_READ] +
1544 stat.nsecs[STAT_WRITE] +
1545 stat.nsecs[STAT_DISCARD] +
1546 stat.nsecs[STAT_FLUSH],
1547 NSEC_PER_MSEC),
ea18e0f0
KK
1548 stat.ios[STAT_DISCARD],
1549 stat.merges[STAT_DISCARD],
1550 stat.sectors[STAT_DISCARD],
1551 (unsigned int)div_u64(stat.nsecs[STAT_DISCARD],
1552 NSEC_PER_MSEC),
1553 stat.ios[STAT_FLUSH],
1554 (unsigned int)div_u64(stat.nsecs[STAT_FLUSH],
1555 NSEC_PER_MSEC)
28f39d55 1556 );
1da177e4 1557 }
e71bf0d0 1558 disk_part_iter_exit(&piter);
9f5e4865 1559
1da177e4
LT
1560 return 0;
1561}
1562
31d85ab2 1563static const struct seq_operations diskstats_op = {
def4e38d
TH
1564 .start = disk_seqf_start,
1565 .next = disk_seqf_next,
1566 .stop = disk_seqf_stop,
1da177e4
LT
1567 .show = diskstats_show
1568};
f500975a
AD
1569
1570static int __init proc_genhd_init(void)
1571{
fddda2b7
CH
1572 proc_create_seq("diskstats", 0, NULL, &diskstats_op);
1573 proc_create_seq("partitions", 0, NULL, &partitions_op);
f500975a
AD
1574 return 0;
1575}
1576module_init(proc_genhd_init);
a6e2ba88 1577#endif /* CONFIG_PROC_FS */
1da177e4 1578
cf771cb5 1579dev_t blk_lookup_devt(const char *name, int partno)
a142be85 1580{
def4e38d
TH
1581 dev_t devt = MKDEV(0, 0);
1582 struct class_dev_iter iter;
1583 struct device *dev;
a142be85 1584
def4e38d
TH
1585 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
1586 while ((dev = class_dev_iter_next(&iter))) {
a142be85 1587 struct gendisk *disk = dev_to_disk(dev);
548b10eb 1588 struct hd_struct *part;
a142be85 1589
3ada8b7e 1590 if (strcmp(dev_name(dev), name))
f331c029 1591 continue;
f331c029 1592
41b8c853
NB
1593 if (partno < disk->minors) {
1594 /* We need to return the right devno, even
1595 * if the partition doesn't exist yet.
1596 */
1597 devt = MKDEV(MAJOR(dev->devt),
1598 MINOR(dev->devt) + partno);
1599 break;
1600 }
548b10eb 1601 part = disk_get_part(disk, partno);
2bbedcb4 1602 if (part) {
f331c029 1603 devt = part_devt(part);
e71bf0d0 1604 disk_put_part(part);
548b10eb 1605 break;
def4e38d 1606 }
548b10eb 1607 disk_put_part(part);
5c0ef6d0 1608 }
def4e38d 1609 class_dev_iter_exit(&iter);
edfaa7c3
KS
1610 return devt;
1611}
edfaa7c3 1612
e319e1fb 1613struct gendisk *__alloc_disk_node(int minors, int node_id)
1946089a
CL
1614{
1615 struct gendisk *disk;
6d2cf6f2 1616 struct disk_part_tbl *ptbl;
1946089a 1617
de65b012
CH
1618 if (minors > DISK_MAX_PARTS) {
1619 printk(KERN_ERR
7fb52621 1620 "block: can't allocate more than %d partitions\n",
de65b012
CH
1621 DISK_MAX_PARTS);
1622 minors = DISK_MAX_PARTS;
1623 }
1946089a 1624
c1b511eb 1625 disk = kzalloc_node(sizeof(struct gendisk), GFP_KERNEL, node_id);
f93af2a4
CH
1626 if (!disk)
1627 return NULL;
6c23a968 1628
22ae8ce8
CH
1629 disk->part0.bdev = bdev_alloc(disk, 0);
1630 if (!disk->part0.bdev)
1631 goto out_free_disk;
1632
f93af2a4 1633 disk->node_id = node_id;
22ae8ce8 1634 if (disk_expand_part_tbl(disk, 0))
15e3d2c5 1635 goto out_bdput;
f93af2a4
CH
1636
1637 ptbl = rcu_dereference_protected(disk->part_tbl, 1);
1638 rcu_assign_pointer(ptbl->part[0], &disk->part0);
1639
f93af2a4 1640 if (hd_ref_init(&disk->part0))
15e3d2c5 1641 goto out_bdput;
f93af2a4
CH
1642
1643 disk->minors = minors;
1644 rand_initialize_disk(disk);
1645 disk_to_dev(disk)->class = &block_class;
1646 disk_to_dev(disk)->type = &disk_type;
1647 device_initialize(disk_to_dev(disk));
1da177e4 1648 return disk;
f93af2a4 1649
22ae8ce8
CH
1650out_bdput:
1651 bdput(disk->part0.bdev);
f93af2a4
CH
1652out_free_disk:
1653 kfree(disk);
1654 return NULL;
1da177e4 1655}
e319e1fb 1656EXPORT_SYMBOL(__alloc_disk_node);
1da177e4 1657
b5bd357c
LC
1658/**
1659 * put_disk - decrements the gendisk refcount
0d20dcc2 1660 * @disk: the struct gendisk to decrement the refcount for
b5bd357c
LC
1661 *
1662 * This decrements the refcount for the struct gendisk. When this reaches 0
1663 * we'll have disk_release() called.
e8c7d14a
LC
1664 *
1665 * Context: Any context, but the last reference must not be dropped from
1666 * atomic context.
b5bd357c 1667 */
1da177e4
LT
1668void put_disk(struct gendisk *disk)
1669{
1670 if (disk)
efdc41c8 1671 put_device(disk_to_dev(disk));
1da177e4 1672}
1da177e4
LT
1673EXPORT_SYMBOL(put_disk);
1674
e3264a4d
HR
1675static void set_disk_ro_uevent(struct gendisk *gd, int ro)
1676{
1677 char event[] = "DISK_RO=1";
1678 char *envp[] = { event, NULL };
1679
1680 if (!ro)
1681 event[8] = '0';
1682 kobject_uevent_env(&disk_to_dev(gd)->kobj, KOBJ_CHANGE, envp);
1683}
1684
1da177e4
LT
1685void set_disk_ro(struct gendisk *disk, int flag)
1686{
e71bf0d0
TH
1687 struct disk_part_iter piter;
1688 struct hd_struct *part;
1689
83950d35 1690 if (disk->part0.bdev->bd_read_only != flag) {
e3264a4d 1691 set_disk_ro_uevent(disk, flag);
83950d35 1692 disk->part0.bdev->bd_read_only = flag;
e3264a4d
HR
1693 }
1694
1695 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_EMPTY);
e71bf0d0 1696 while ((part = disk_part_iter_next(&piter)))
83950d35 1697 part->bdev->bd_read_only = flag;
e71bf0d0 1698 disk_part_iter_exit(&piter);
1da177e4
LT
1699}
1700
1701EXPORT_SYMBOL(set_disk_ro);
1702
1703int bdev_read_only(struct block_device *bdev)
1704{
1705 if (!bdev)
1706 return 0;
83950d35 1707 return bdev->bd_read_only;
1da177e4
LT
1708}
1709
1710EXPORT_SYMBOL(bdev_read_only);
1711
77ea887e
TH
1712/*
1713 * Disk events - monitor disk events like media change and eject request.
1714 */
1715struct disk_events {
1716 struct list_head node; /* all disk_event's */
1717 struct gendisk *disk; /* the associated disk */
1718 spinlock_t lock;
1719
fdd514e1 1720 struct mutex block_mutex; /* protects blocking */
77ea887e
TH
1721 int block; /* event blocking depth */
1722 unsigned int pending; /* events already sent out */
1723 unsigned int clearing; /* events being cleared */
1724
1725 long poll_msecs; /* interval, -1 for default */
1726 struct delayed_work dwork;
1727};
1728
1729static const char *disk_events_strs[] = {
1730 [ilog2(DISK_EVENT_MEDIA_CHANGE)] = "media_change",
1731 [ilog2(DISK_EVENT_EJECT_REQUEST)] = "eject_request",
1732};
1733
1734static char *disk_uevents[] = {
1735 [ilog2(DISK_EVENT_MEDIA_CHANGE)] = "DISK_MEDIA_CHANGE=1",
1736 [ilog2(DISK_EVENT_EJECT_REQUEST)] = "DISK_EJECT_REQUEST=1",
1737};
1738
1739/* list of all disk_events */
1740static DEFINE_MUTEX(disk_events_mutex);
1741static LIST_HEAD(disk_events);
1742
1743/* disable in-kernel polling by default */
1fe8f348 1744static unsigned long disk_events_dfl_poll_msecs;
77ea887e
TH
1745
1746static unsigned long disk_events_poll_jiffies(struct gendisk *disk)
1747{
1748 struct disk_events *ev = disk->ev;
1749 long intv_msecs = 0;
1750
1751 /*
1752 * If device-specific poll interval is set, always use it. If
673387a9 1753 * the default is being used, poll if the POLL flag is set.
77ea887e
TH
1754 */
1755 if (ev->poll_msecs >= 0)
1756 intv_msecs = ev->poll_msecs;
c92e2f04 1757 else if (disk->event_flags & DISK_EVENT_FLAG_POLL)
77ea887e
TH
1758 intv_msecs = disk_events_dfl_poll_msecs;
1759
1760 return msecs_to_jiffies(intv_msecs);
1761}
1762
c3af54af
TH
1763/**
1764 * disk_block_events - block and flush disk event checking
1765 * @disk: disk to block events for
1766 *
1767 * On return from this function, it is guaranteed that event checking
1768 * isn't in progress and won't happen until unblocked by
1769 * disk_unblock_events(). Events blocking is counted and the actual
1770 * unblocking happens after the matching number of unblocks are done.
1771 *
1772 * Note that this intentionally does not block event checking from
1773 * disk_clear_events().
1774 *
1775 * CONTEXT:
1776 * Might sleep.
1777 */
1778void disk_block_events(struct gendisk *disk)
77ea887e
TH
1779{
1780 struct disk_events *ev = disk->ev;
1781 unsigned long flags;
1782 bool cancel;
1783
c3af54af
TH
1784 if (!ev)
1785 return;
1786
fdd514e1
TH
1787 /*
1788 * Outer mutex ensures that the first blocker completes canceling
1789 * the event work before further blockers are allowed to finish.
1790 */
1791 mutex_lock(&ev->block_mutex);
1792
77ea887e
TH
1793 spin_lock_irqsave(&ev->lock, flags);
1794 cancel = !ev->block++;
1795 spin_unlock_irqrestore(&ev->lock, flags);
1796
c3af54af
TH
1797 if (cancel)
1798 cancel_delayed_work_sync(&disk->ev->dwork);
fdd514e1
TH
1799
1800 mutex_unlock(&ev->block_mutex);
77ea887e
TH
1801}
1802
1803static void __disk_unblock_events(struct gendisk *disk, bool check_now)
1804{
1805 struct disk_events *ev = disk->ev;
1806 unsigned long intv;
1807 unsigned long flags;
1808
1809 spin_lock_irqsave(&ev->lock, flags);
1810
1811 if (WARN_ON_ONCE(ev->block <= 0))
1812 goto out_unlock;
1813
1814 if (--ev->block)
1815 goto out_unlock;
1816
77ea887e 1817 intv = disk_events_poll_jiffies(disk);
77ea887e 1818 if (check_now)
695588f9
VK
1819 queue_delayed_work(system_freezable_power_efficient_wq,
1820 &ev->dwork, 0);
77ea887e 1821 else if (intv)
695588f9
VK
1822 queue_delayed_work(system_freezable_power_efficient_wq,
1823 &ev->dwork, intv);
77ea887e
TH
1824out_unlock:
1825 spin_unlock_irqrestore(&ev->lock, flags);
1826}
1827
77ea887e
TH
1828/**
1829 * disk_unblock_events - unblock disk event checking
1830 * @disk: disk to unblock events for
1831 *
1832 * Undo disk_block_events(). When the block count reaches zero, it
1833 * starts events polling if configured.
1834 *
1835 * CONTEXT:
1836 * Don't care. Safe to call from irq context.
1837 */
1838void disk_unblock_events(struct gendisk *disk)
1839{
1840 if (disk->ev)
facc31dd 1841 __disk_unblock_events(disk, false);
77ea887e
TH
1842}
1843
1844/**
85ef06d1
TH
1845 * disk_flush_events - schedule immediate event checking and flushing
1846 * @disk: disk to check and flush events for
1847 * @mask: events to flush
77ea887e 1848 *
85ef06d1
TH
1849 * Schedule immediate event checking on @disk if not blocked. Events in
1850 * @mask are scheduled to be cleared from the driver. Note that this
1851 * doesn't clear the events from @disk->ev.
77ea887e
TH
1852 *
1853 * CONTEXT:
85ef06d1 1854 * If @mask is non-zero must be called with bdev->bd_mutex held.
77ea887e 1855 */
85ef06d1 1856void disk_flush_events(struct gendisk *disk, unsigned int mask)
77ea887e 1857{
a9dce2a3 1858 struct disk_events *ev = disk->ev;
a9dce2a3
TH
1859
1860 if (!ev)
1861 return;
1862
85ef06d1
TH
1863 spin_lock_irq(&ev->lock);
1864 ev->clearing |= mask;
41f63c53 1865 if (!ev->block)
695588f9
VK
1866 mod_delayed_work(system_freezable_power_efficient_wq,
1867 &ev->dwork, 0);
85ef06d1 1868 spin_unlock_irq(&ev->lock);
77ea887e 1869}
77ea887e
TH
1870
1871/**
1872 * disk_clear_events - synchronously check, clear and return pending events
1873 * @disk: disk to fetch and clear events from
da3dae54 1874 * @mask: mask of events to be fetched and cleared
77ea887e
TH
1875 *
1876 * Disk events are synchronously checked and pending events in @mask
1877 * are cleared and returned. This ignores the block count.
1878 *
1879 * CONTEXT:
1880 * Might sleep.
1881 */
95f6f3a4 1882static unsigned int disk_clear_events(struct gendisk *disk, unsigned int mask)
77ea887e 1883{
77ea887e
TH
1884 struct disk_events *ev = disk->ev;
1885 unsigned int pending;
12c2bdb2 1886 unsigned int clearing = mask;
77ea887e 1887
a564e23f 1888 if (!ev)
77ea887e 1889 return 0;
77ea887e 1890
12c2bdb2
DB
1891 disk_block_events(disk);
1892
1893 /*
1894 * store the union of mask and ev->clearing on the stack so that the
1895 * race with disk_flush_events does not cause ambiguity (ev->clearing
1896 * can still be modified even if events are blocked).
1897 */
77ea887e 1898 spin_lock_irq(&ev->lock);
12c2bdb2
DB
1899 clearing |= ev->clearing;
1900 ev->clearing = 0;
77ea887e
TH
1901 spin_unlock_irq(&ev->lock);
1902
12c2bdb2 1903 disk_check_events(ev, &clearing);
aea24a8b 1904 /*
12c2bdb2
DB
1905 * if ev->clearing is not 0, the disk_flush_events got called in the
1906 * middle of this function, so we want to run the workfn without delay.
aea24a8b 1907 */
12c2bdb2 1908 __disk_unblock_events(disk, ev->clearing ? true : false);
77ea887e
TH
1909
1910 /* then, fetch and clear pending events */
1911 spin_lock_irq(&ev->lock);
77ea887e
TH
1912 pending = ev->pending & mask;
1913 ev->pending &= ~mask;
1914 spin_unlock_irq(&ev->lock);
12c2bdb2 1915 WARN_ON_ONCE(clearing & mask);
77ea887e
TH
1916
1917 return pending;
1918}
1919
95f6f3a4
CH
1920/**
1921 * bdev_check_media_change - check if a removable media has been changed
1922 * @bdev: block device to check
1923 *
1924 * Check whether a removable media has been changed, and attempt to free all
1925 * dentries and inodes and invalidates all block device page cache entries in
1926 * that case.
1927 *
1928 * Returns %true if the block device changed, or %false if not.
1929 */
1930bool bdev_check_media_change(struct block_device *bdev)
1931{
1932 unsigned int events;
1933
1934 events = disk_clear_events(bdev->bd_disk, DISK_EVENT_MEDIA_CHANGE |
1935 DISK_EVENT_EJECT_REQUEST);
1936 if (!(events & DISK_EVENT_MEDIA_CHANGE))
1937 return false;
1938
1939 if (__invalidate_device(bdev, true))
1940 pr_warn("VFS: busy inodes on changed media %s\n",
1941 bdev->bd_disk->disk_name);
38430f08 1942 set_bit(GD_NEED_PART_SCAN, &bdev->bd_disk->state);
95f6f3a4
CH
1943 return true;
1944}
1945EXPORT_SYMBOL(bdev_check_media_change);
1946
12c2bdb2
DB
1947/*
1948 * Separate this part out so that a different pointer for clearing_ptr can be
1949 * passed in for disk_clear_events.
1950 */
77ea887e
TH
1951static void disk_events_workfn(struct work_struct *work)
1952{
1953 struct delayed_work *dwork = to_delayed_work(work);
1954 struct disk_events *ev = container_of(dwork, struct disk_events, dwork);
12c2bdb2
DB
1955
1956 disk_check_events(ev, &ev->clearing);
1957}
1958
1959static void disk_check_events(struct disk_events *ev,
1960 unsigned int *clearing_ptr)
1961{
77ea887e
TH
1962 struct gendisk *disk = ev->disk;
1963 char *envp[ARRAY_SIZE(disk_uevents) + 1] = { };
12c2bdb2 1964 unsigned int clearing = *clearing_ptr;
77ea887e
TH
1965 unsigned int events;
1966 unsigned long intv;
1967 int nr_events = 0, i;
1968
1969 /* check events */
1970 events = disk->fops->check_events(disk, clearing);
1971
1972 /* accumulate pending events and schedule next poll if necessary */
1973 spin_lock_irq(&ev->lock);
1974
1975 events &= ~ev->pending;
1976 ev->pending |= events;
12c2bdb2 1977 *clearing_ptr &= ~clearing;
77ea887e
TH
1978
1979 intv = disk_events_poll_jiffies(disk);
1980 if (!ev->block && intv)
695588f9
VK
1981 queue_delayed_work(system_freezable_power_efficient_wq,
1982 &ev->dwork, intv);
77ea887e
TH
1983
1984 spin_unlock_irq(&ev->lock);
1985
7c88a168
TH
1986 /*
1987 * Tell userland about new events. Only the events listed in
c92e2f04
MW
1988 * @disk->events are reported, and only if DISK_EVENT_FLAG_UEVENT
1989 * is set. Otherwise, events are processed internally but never
1990 * get reported to userland.
7c88a168 1991 */
77ea887e 1992 for (i = 0; i < ARRAY_SIZE(disk_uevents); i++)
c92e2f04
MW
1993 if ((events & disk->events & (1 << i)) &&
1994 (disk->event_flags & DISK_EVENT_FLAG_UEVENT))
77ea887e
TH
1995 envp[nr_events++] = disk_uevents[i];
1996
1997 if (nr_events)
1998 kobject_uevent_env(&disk_to_dev(disk)->kobj, KOBJ_CHANGE, envp);
1999}
2000
2001/*
2002 * A disk events enabled device has the following sysfs nodes under
2003 * its /sys/block/X/ directory.
2004 *
2005 * events : list of all supported events
2006 * events_async : list of events which can be detected w/o polling
673387a9 2007 * (always empty, only for backwards compatibility)
77ea887e
TH
2008 * events_poll_msecs : polling interval, 0: disable, -1: system default
2009 */
2010static ssize_t __disk_events_show(unsigned int events, char *buf)
2011{
2012 const char *delim = "";
2013 ssize_t pos = 0;
2014 int i;
2015
2016 for (i = 0; i < ARRAY_SIZE(disk_events_strs); i++)
2017 if (events & (1 << i)) {
2018 pos += sprintf(buf + pos, "%s%s",
2019 delim, disk_events_strs[i]);
2020 delim = " ";
2021 }
2022 if (pos)
2023 pos += sprintf(buf + pos, "\n");
2024 return pos;
2025}
2026
2027static ssize_t disk_events_show(struct device *dev,
2028 struct device_attribute *attr, char *buf)
2029{
2030 struct gendisk *disk = dev_to_disk(dev);
2031
c92e2f04
MW
2032 if (!(disk->event_flags & DISK_EVENT_FLAG_UEVENT))
2033 return 0;
2034
77ea887e
TH
2035 return __disk_events_show(disk->events, buf);
2036}
2037
2038static ssize_t disk_events_async_show(struct device *dev,
2039 struct device_attribute *attr, char *buf)
2040{
673387a9 2041 return 0;
77ea887e
TH
2042}
2043
2044static ssize_t disk_events_poll_msecs_show(struct device *dev,
2045 struct device_attribute *attr,
2046 char *buf)
2047{
2048 struct gendisk *disk = dev_to_disk(dev);
2049
cdf3e3de
MW
2050 if (!disk->ev)
2051 return sprintf(buf, "-1\n");
2052
77ea887e
TH
2053 return sprintf(buf, "%ld\n", disk->ev->poll_msecs);
2054}
2055
2056static ssize_t disk_events_poll_msecs_store(struct device *dev,
2057 struct device_attribute *attr,
2058 const char *buf, size_t count)
2059{
2060 struct gendisk *disk = dev_to_disk(dev);
2061 long intv;
2062
2063 if (!count || !sscanf(buf, "%ld", &intv))
2064 return -EINVAL;
2065
2066 if (intv < 0 && intv != -1)
2067 return -EINVAL;
2068
cdf3e3de
MW
2069 if (!disk->ev)
2070 return -ENODEV;
2071
c3af54af 2072 disk_block_events(disk);
77ea887e
TH
2073 disk->ev->poll_msecs = intv;
2074 __disk_unblock_events(disk, true);
2075
2076 return count;
2077}
2078
5657a819
JP
2079static const DEVICE_ATTR(events, 0444, disk_events_show, NULL);
2080static const DEVICE_ATTR(events_async, 0444, disk_events_async_show, NULL);
2081static const DEVICE_ATTR(events_poll_msecs, 0644,
77ea887e
TH
2082 disk_events_poll_msecs_show,
2083 disk_events_poll_msecs_store);
2084
2085static const struct attribute *disk_events_attrs[] = {
2086 &dev_attr_events.attr,
2087 &dev_attr_events_async.attr,
2088 &dev_attr_events_poll_msecs.attr,
2089 NULL,
2090};
2091
2092/*
2093 * The default polling interval can be specified by the kernel
2094 * parameter block.events_dfl_poll_msecs which defaults to 0
2095 * (disable). This can also be modified runtime by writing to
1624b0b2 2096 * /sys/module/block/parameters/events_dfl_poll_msecs.
77ea887e
TH
2097 */
2098static int disk_events_set_dfl_poll_msecs(const char *val,
2099 const struct kernel_param *kp)
2100{
2101 struct disk_events *ev;
2102 int ret;
2103
2104 ret = param_set_ulong(val, kp);
2105 if (ret < 0)
2106 return ret;
2107
2108 mutex_lock(&disk_events_mutex);
2109
2110 list_for_each_entry(ev, &disk_events, node)
85ef06d1 2111 disk_flush_events(ev->disk, 0);
77ea887e
TH
2112
2113 mutex_unlock(&disk_events_mutex);
2114
2115 return 0;
2116}
2117
2118static const struct kernel_param_ops disk_events_dfl_poll_msecs_param_ops = {
2119 .set = disk_events_set_dfl_poll_msecs,
2120 .get = param_get_ulong,
2121};
2122
2123#undef MODULE_PARAM_PREFIX
2124#define MODULE_PARAM_PREFIX "block."
2125
2126module_param_cb(events_dfl_poll_msecs, &disk_events_dfl_poll_msecs_param_ops,
2127 &disk_events_dfl_poll_msecs, 0644);
2128
2129/*
9f53d2fe 2130 * disk_{alloc|add|del|release}_events - initialize and destroy disk_events.
77ea887e 2131 */
9f53d2fe 2132static void disk_alloc_events(struct gendisk *disk)
77ea887e
TH
2133{
2134 struct disk_events *ev;
2135
cdf3e3de 2136 if (!disk->fops->check_events || !disk->events)
77ea887e
TH
2137 return;
2138
2139 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
2140 if (!ev) {
2141 pr_warn("%s: failed to initialize events\n", disk->disk_name);
2142 return;
2143 }
2144
77ea887e
TH
2145 INIT_LIST_HEAD(&ev->node);
2146 ev->disk = disk;
2147 spin_lock_init(&ev->lock);
fdd514e1 2148 mutex_init(&ev->block_mutex);
77ea887e
TH
2149 ev->block = 1;
2150 ev->poll_msecs = -1;
2151 INIT_DELAYED_WORK(&ev->dwork, disk_events_workfn);
2152
9f53d2fe
SG
2153 disk->ev = ev;
2154}
2155
2156static void disk_add_events(struct gendisk *disk)
2157{
9f53d2fe
SG
2158 /* FIXME: error handling */
2159 if (sysfs_create_files(&disk_to_dev(disk)->kobj, disk_events_attrs) < 0)
2160 pr_warn("%s: failed to create sysfs files for events\n",
2161 disk->disk_name);
2162
cdf3e3de
MW
2163 if (!disk->ev)
2164 return;
2165
77ea887e 2166 mutex_lock(&disk_events_mutex);
9f53d2fe 2167 list_add_tail(&disk->ev->node, &disk_events);
77ea887e
TH
2168 mutex_unlock(&disk_events_mutex);
2169
2170 /*
2171 * Block count is initialized to 1 and the following initial
2172 * unblock kicks it into action.
2173 */
2174 __disk_unblock_events(disk, true);
2175}
2176
2177static void disk_del_events(struct gendisk *disk)
2178{
cdf3e3de
MW
2179 if (disk->ev) {
2180 disk_block_events(disk);
77ea887e 2181
cdf3e3de
MW
2182 mutex_lock(&disk_events_mutex);
2183 list_del_init(&disk->ev->node);
2184 mutex_unlock(&disk_events_mutex);
2185 }
77ea887e
TH
2186
2187 sysfs_remove_files(&disk_to_dev(disk)->kobj, disk_events_attrs);
2188}
2189
2190static void disk_release_events(struct gendisk *disk)
2191{
2192 /* the block count should be 1 from disk_del_events() */
2193 WARN_ON_ONCE(disk->ev && disk->ev->block != 1);
2194 kfree(disk->ev);
2195}