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