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