]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - block/genhd.c
block: implement extended dev numbers
[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/init.h>
12 #include <linux/spinlock.h>
13 #include <linux/seq_file.h>
14 #include <linux/slab.h>
15 #include <linux/kmod.h>
16 #include <linux/kobj_map.h>
17 #include <linux/buffer_head.h>
18 #include <linux/mutex.h>
19 #include <linux/idr.h>
20
21 #include "blk.h"
22
23 static DEFINE_MUTEX(block_class_lock);
24 #ifndef CONFIG_SYSFS_DEPRECATED
25 struct kobject *block_depr;
26 #endif
27
28 /* for extended dynamic devt allocation, currently only one major is used */
29 #define MAX_EXT_DEVT (1 << MINORBITS)
30
31 /* For extended devt allocation. ext_devt_mutex prevents look up
32 * results from going away underneath its user.
33 */
34 static DEFINE_MUTEX(ext_devt_mutex);
35 static DEFINE_IDR(ext_devt_idr);
36
37 static struct device_type disk_type;
38
39 /**
40 * disk_get_part - get partition
41 * @disk: disk to look partition from
42 * @partno: partition number
43 *
44 * Look for partition @partno from @disk. If found, increment
45 * reference count and return it.
46 *
47 * CONTEXT:
48 * Don't care.
49 *
50 * RETURNS:
51 * Pointer to the found partition on success, NULL if not found.
52 */
53 struct hd_struct *disk_get_part(struct gendisk *disk, int partno)
54 {
55 struct hd_struct *part;
56
57 if (unlikely(partno < 1 || partno > disk_max_parts(disk)))
58 return NULL;
59 rcu_read_lock();
60 part = rcu_dereference(disk->__part[partno - 1]);
61 if (part)
62 get_device(&part->dev);
63 rcu_read_unlock();
64
65 return part;
66 }
67 EXPORT_SYMBOL_GPL(disk_get_part);
68
69 /**
70 * disk_part_iter_init - initialize partition iterator
71 * @piter: iterator to initialize
72 * @disk: disk to iterate over
73 * @flags: DISK_PITER_* flags
74 *
75 * Initialize @piter so that it iterates over partitions of @disk.
76 *
77 * CONTEXT:
78 * Don't care.
79 */
80 void disk_part_iter_init(struct disk_part_iter *piter, struct gendisk *disk,
81 unsigned int flags)
82 {
83 piter->disk = disk;
84 piter->part = NULL;
85
86 if (flags & DISK_PITER_REVERSE)
87 piter->idx = disk_max_parts(piter->disk) - 1;
88 else
89 piter->idx = 0;
90
91 piter->flags = flags;
92 }
93 EXPORT_SYMBOL_GPL(disk_part_iter_init);
94
95 /**
96 * disk_part_iter_next - proceed iterator to the next partition and return it
97 * @piter: iterator of interest
98 *
99 * Proceed @piter to the next partition and return it.
100 *
101 * CONTEXT:
102 * Don't care.
103 */
104 struct hd_struct *disk_part_iter_next(struct disk_part_iter *piter)
105 {
106 int inc, end;
107
108 /* put the last partition */
109 disk_put_part(piter->part);
110 piter->part = NULL;
111
112 rcu_read_lock();
113
114 /* determine iteration parameters */
115 if (piter->flags & DISK_PITER_REVERSE) {
116 inc = -1;
117 end = -1;
118 } else {
119 inc = 1;
120 end = disk_max_parts(piter->disk);
121 }
122
123 /* iterate to the next partition */
124 for (; piter->idx != end; piter->idx += inc) {
125 struct hd_struct *part;
126
127 part = rcu_dereference(piter->disk->__part[piter->idx]);
128 if (!part)
129 continue;
130 if (!(piter->flags & DISK_PITER_INCL_EMPTY) && !part->nr_sects)
131 continue;
132
133 get_device(&part->dev);
134 piter->part = part;
135 piter->idx += inc;
136 break;
137 }
138
139 rcu_read_unlock();
140
141 return piter->part;
142 }
143 EXPORT_SYMBOL_GPL(disk_part_iter_next);
144
145 /**
146 * disk_part_iter_exit - finish up partition iteration
147 * @piter: iter of interest
148 *
149 * Called when iteration is over. Cleans up @piter.
150 *
151 * CONTEXT:
152 * Don't care.
153 */
154 void disk_part_iter_exit(struct disk_part_iter *piter)
155 {
156 disk_put_part(piter->part);
157 piter->part = NULL;
158 }
159 EXPORT_SYMBOL_GPL(disk_part_iter_exit);
160
161 /**
162 * disk_map_sector_rcu - map sector to partition
163 * @disk: gendisk of interest
164 * @sector: sector to map
165 *
166 * Find out which partition @sector maps to on @disk. This is
167 * primarily used for stats accounting.
168 *
169 * CONTEXT:
170 * RCU read locked. The returned partition pointer is valid only
171 * while preemption is disabled.
172 *
173 * RETURNS:
174 * Found partition on success, NULL if there's no matching partition.
175 */
176 struct hd_struct *disk_map_sector_rcu(struct gendisk *disk, sector_t sector)
177 {
178 int i;
179
180 for (i = 0; i < disk_max_parts(disk); i++) {
181 struct hd_struct *part = rcu_dereference(disk->__part[i]);
182
183 if (part && part->start_sect <= sector &&
184 sector < part->start_sect + part->nr_sects)
185 return part;
186 }
187 return NULL;
188 }
189 EXPORT_SYMBOL_GPL(disk_map_sector_rcu);
190
191 /*
192 * Can be deleted altogether. Later.
193 *
194 */
195 static struct blk_major_name {
196 struct blk_major_name *next;
197 int major;
198 char name[16];
199 } *major_names[BLKDEV_MAJOR_HASH_SIZE];
200
201 /* index in the above - for now: assume no multimajor ranges */
202 static inline int major_to_index(int major)
203 {
204 return major % BLKDEV_MAJOR_HASH_SIZE;
205 }
206
207 #ifdef CONFIG_PROC_FS
208 void blkdev_show(struct seq_file *seqf, off_t offset)
209 {
210 struct blk_major_name *dp;
211
212 if (offset < BLKDEV_MAJOR_HASH_SIZE) {
213 mutex_lock(&block_class_lock);
214 for (dp = major_names[offset]; dp; dp = dp->next)
215 seq_printf(seqf, "%3d %s\n", dp->major, dp->name);
216 mutex_unlock(&block_class_lock);
217 }
218 }
219 #endif /* CONFIG_PROC_FS */
220
221 int register_blkdev(unsigned int major, const char *name)
222 {
223 struct blk_major_name **n, *p;
224 int index, ret = 0;
225
226 mutex_lock(&block_class_lock);
227
228 /* temporary */
229 if (major == 0) {
230 for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) {
231 if (major_names[index] == NULL)
232 break;
233 }
234
235 if (index == 0) {
236 printk("register_blkdev: failed to get major for %s\n",
237 name);
238 ret = -EBUSY;
239 goto out;
240 }
241 major = index;
242 ret = major;
243 }
244
245 p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL);
246 if (p == NULL) {
247 ret = -ENOMEM;
248 goto out;
249 }
250
251 p->major = major;
252 strlcpy(p->name, name, sizeof(p->name));
253 p->next = NULL;
254 index = major_to_index(major);
255
256 for (n = &major_names[index]; *n; n = &(*n)->next) {
257 if ((*n)->major == major)
258 break;
259 }
260 if (!*n)
261 *n = p;
262 else
263 ret = -EBUSY;
264
265 if (ret < 0) {
266 printk("register_blkdev: cannot get major %d for %s\n",
267 major, name);
268 kfree(p);
269 }
270 out:
271 mutex_unlock(&block_class_lock);
272 return ret;
273 }
274
275 EXPORT_SYMBOL(register_blkdev);
276
277 void unregister_blkdev(unsigned int major, const char *name)
278 {
279 struct blk_major_name **n;
280 struct blk_major_name *p = NULL;
281 int index = major_to_index(major);
282
283 mutex_lock(&block_class_lock);
284 for (n = &major_names[index]; *n; n = &(*n)->next)
285 if ((*n)->major == major)
286 break;
287 if (!*n || strcmp((*n)->name, name)) {
288 WARN_ON(1);
289 } else {
290 p = *n;
291 *n = p->next;
292 }
293 mutex_unlock(&block_class_lock);
294 kfree(p);
295 }
296
297 EXPORT_SYMBOL(unregister_blkdev);
298
299 static struct kobj_map *bdev_map;
300
301 /**
302 * blk_alloc_devt - allocate a dev_t for a partition
303 * @part: partition to allocate dev_t for
304 * @gfp_mask: memory allocation flag
305 * @devt: out parameter for resulting dev_t
306 *
307 * Allocate a dev_t for block device.
308 *
309 * RETURNS:
310 * 0 on success, allocated dev_t is returned in *@devt. -errno on
311 * failure.
312 *
313 * CONTEXT:
314 * Might sleep.
315 */
316 int blk_alloc_devt(struct hd_struct *part, dev_t *devt)
317 {
318 struct gendisk *disk = part_to_disk(part);
319 int idx, rc;
320
321 /* in consecutive minor range? */
322 if (part->partno < disk->minors) {
323 *devt = MKDEV(disk->major, disk->first_minor + part->partno);
324 return 0;
325 }
326
327 /* allocate ext devt */
328 do {
329 if (!idr_pre_get(&ext_devt_idr, GFP_KERNEL))
330 return -ENOMEM;
331 rc = idr_get_new(&ext_devt_idr, part, &idx);
332 } while (rc == -EAGAIN);
333
334 if (rc)
335 return rc;
336
337 if (idx > MAX_EXT_DEVT) {
338 idr_remove(&ext_devt_idr, idx);
339 return -EBUSY;
340 }
341
342 *devt = MKDEV(BLOCK_EXT_MAJOR, idx);
343 return 0;
344 }
345
346 /**
347 * blk_free_devt - free a dev_t
348 * @devt: dev_t to free
349 *
350 * Free @devt which was allocated using blk_alloc_devt().
351 *
352 * CONTEXT:
353 * Might sleep.
354 */
355 void blk_free_devt(dev_t devt)
356 {
357 might_sleep();
358
359 if (devt == MKDEV(0, 0))
360 return;
361
362 if (MAJOR(devt) == BLOCK_EXT_MAJOR) {
363 mutex_lock(&ext_devt_mutex);
364 idr_remove(&ext_devt_idr, MINOR(devt));
365 mutex_unlock(&ext_devt_mutex);
366 }
367 }
368
369 /*
370 * Register device numbers dev..(dev+range-1)
371 * range must be nonzero
372 * The hash chain is sorted on range, so that subranges can override.
373 */
374 void blk_register_region(dev_t devt, unsigned long range, struct module *module,
375 struct kobject *(*probe)(dev_t, int *, void *),
376 int (*lock)(dev_t, void *), void *data)
377 {
378 kobj_map(bdev_map, devt, range, module, probe, lock, data);
379 }
380
381 EXPORT_SYMBOL(blk_register_region);
382
383 void blk_unregister_region(dev_t devt, unsigned long range)
384 {
385 kobj_unmap(bdev_map, devt, range);
386 }
387
388 EXPORT_SYMBOL(blk_unregister_region);
389
390 static struct kobject *exact_match(dev_t devt, int *partno, void *data)
391 {
392 struct gendisk *p = data;
393
394 return &p->dev.kobj;
395 }
396
397 static int exact_lock(dev_t devt, void *data)
398 {
399 struct gendisk *p = data;
400
401 if (!get_disk(p))
402 return -1;
403 return 0;
404 }
405
406 /**
407 * add_disk - add partitioning information to kernel list
408 * @disk: per-device partitioning information
409 *
410 * This function registers the partitioning information in @disk
411 * with the kernel.
412 */
413 void add_disk(struct gendisk *disk)
414 {
415 struct backing_dev_info *bdi;
416 int retval;
417
418 disk->flags |= GENHD_FL_UP;
419 disk->dev.devt = MKDEV(disk->major, disk->first_minor);
420 blk_register_region(disk_devt(disk), disk->minors, NULL,
421 exact_match, exact_lock, disk);
422 register_disk(disk);
423 blk_register_queue(disk);
424
425 bdi = &disk->queue->backing_dev_info;
426 bdi_register_dev(bdi, disk_devt(disk));
427 retval = sysfs_create_link(&disk->dev.kobj, &bdi->dev->kobj, "bdi");
428 WARN_ON(retval);
429 }
430
431 EXPORT_SYMBOL(add_disk);
432 EXPORT_SYMBOL(del_gendisk); /* in partitions/check.c */
433
434 void unlink_gendisk(struct gendisk *disk)
435 {
436 sysfs_remove_link(&disk->dev.kobj, "bdi");
437 bdi_unregister(&disk->queue->backing_dev_info);
438 blk_unregister_queue(disk);
439 blk_unregister_region(disk_devt(disk), disk->minors);
440 }
441
442 /**
443 * get_gendisk - get partitioning information for a given device
444 * @devt: device to get partitioning information for
445 * @part: returned partition index
446 *
447 * This function gets the structure containing partitioning
448 * information for the given device @devt.
449 */
450 struct gendisk *get_gendisk(dev_t devt, int *partno)
451 {
452 struct gendisk *disk = NULL;
453
454 if (MAJOR(devt) != BLOCK_EXT_MAJOR) {
455 struct kobject *kobj;
456
457 kobj = kobj_lookup(bdev_map, devt, partno);
458 if (kobj)
459 disk = dev_to_disk(kobj_to_dev(kobj));
460 } else {
461 struct hd_struct *part;
462
463 mutex_lock(&ext_devt_mutex);
464 part = idr_find(&ext_devt_idr, MINOR(devt));
465 if (part && get_disk(part_to_disk(part))) {
466 *partno = part->partno;
467 disk = part_to_disk(part);
468 }
469 mutex_unlock(&ext_devt_mutex);
470 }
471
472 return disk;
473 }
474
475 /**
476 * bdget_disk - do bdget() by gendisk and partition number
477 * @disk: gendisk of interest
478 * @partno: partition number
479 *
480 * Find partition @partno from @disk, do bdget() on it.
481 *
482 * CONTEXT:
483 * Don't care.
484 *
485 * RETURNS:
486 * Resulting block_device on success, NULL on failure.
487 */
488 extern struct block_device *bdget_disk(struct gendisk *disk, int partno)
489 {
490 dev_t devt = MKDEV(0, 0);
491
492 if (partno == 0)
493 devt = disk_devt(disk);
494 else {
495 struct hd_struct *part;
496
497 part = disk_get_part(disk, partno);
498 if (part && part->nr_sects)
499 devt = part_devt(part);
500 disk_put_part(part);
501 }
502
503 if (likely(devt != MKDEV(0, 0)))
504 return bdget(devt);
505 return NULL;
506 }
507 EXPORT_SYMBOL(bdget_disk);
508
509 /*
510 * print a full list of all partitions - intended for places where the root
511 * filesystem can't be mounted and thus to give the victim some idea of what
512 * went wrong
513 */
514 void __init printk_all_partitions(void)
515 {
516 struct class_dev_iter iter;
517 struct device *dev;
518
519 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
520 while ((dev = class_dev_iter_next(&iter))) {
521 struct gendisk *disk = dev_to_disk(dev);
522 struct disk_part_iter piter;
523 struct hd_struct *part;
524 char buf[BDEVNAME_SIZE];
525
526 /*
527 * Don't show empty devices or things that have been
528 * surpressed
529 */
530 if (get_capacity(disk) == 0 ||
531 (disk->flags & GENHD_FL_SUPPRESS_PARTITION_INFO))
532 continue;
533
534 /*
535 * Note, unlike /proc/partitions, I am showing the
536 * numbers in hex - the same format as the root=
537 * option takes.
538 */
539 printk("%02x%02x %10llu %s",
540 MAJOR(disk_devt(disk)), MINOR(disk_devt(disk)),
541 (unsigned long long)get_capacity(disk) >> 1,
542 disk_name(disk, 0, buf));
543 if (disk->driverfs_dev != NULL &&
544 disk->driverfs_dev->driver != NULL)
545 printk(" driver: %s\n",
546 disk->driverfs_dev->driver->name);
547 else
548 printk(" (driver?)\n");
549
550 /* now show the partitions */
551 disk_part_iter_init(&piter, disk, 0);
552 while ((part = disk_part_iter_next(&piter)))
553 printk(" %02x%02x %10llu %s\n",
554 MAJOR(part_devt(part)), MINOR(part_devt(part)),
555 (unsigned long long)part->nr_sects >> 1,
556 disk_name(disk, part->partno, buf));
557 disk_part_iter_exit(&piter);
558 }
559 class_dev_iter_exit(&iter);
560 }
561
562 #ifdef CONFIG_PROC_FS
563 /* iterator */
564 static void *disk_seqf_start(struct seq_file *seqf, loff_t *pos)
565 {
566 loff_t skip = *pos;
567 struct class_dev_iter *iter;
568 struct device *dev;
569
570 iter = kmalloc(GFP_KERNEL, sizeof(*iter));
571 if (!iter)
572 return ERR_PTR(-ENOMEM);
573
574 seqf->private = iter;
575 class_dev_iter_init(iter, &block_class, NULL, &disk_type);
576 do {
577 dev = class_dev_iter_next(iter);
578 if (!dev)
579 return NULL;
580 } while (skip--);
581
582 return dev_to_disk(dev);
583 }
584
585 static void *disk_seqf_next(struct seq_file *seqf, void *v, loff_t *pos)
586 {
587 struct device *dev;
588
589 (*pos)++;
590 dev = class_dev_iter_next(seqf->private);
591 if (dev)
592 return dev_to_disk(dev);
593
594 return NULL;
595 }
596
597 static void disk_seqf_stop(struct seq_file *seqf, void *v)
598 {
599 struct class_dev_iter *iter = seqf->private;
600
601 /* stop is called even after start failed :-( */
602 if (iter) {
603 class_dev_iter_exit(iter);
604 kfree(iter);
605 }
606 }
607
608 static void *show_partition_start(struct seq_file *seqf, loff_t *pos)
609 {
610 static void *p;
611
612 p = disk_seqf_start(seqf, pos);
613 if (!IS_ERR(p) && p)
614 seq_puts(seqf, "major minor #blocks name\n\n");
615 return p;
616 }
617
618 static int show_partition(struct seq_file *seqf, void *v)
619 {
620 struct gendisk *sgp = v;
621 struct disk_part_iter piter;
622 struct hd_struct *part;
623 char buf[BDEVNAME_SIZE];
624
625 /* Don't show non-partitionable removeable devices or empty devices */
626 if (!get_capacity(sgp) || (!disk_max_parts(sgp) &&
627 (sgp->flags & GENHD_FL_REMOVABLE)))
628 return 0;
629 if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)
630 return 0;
631
632 /* show the full disk and all non-0 size partitions of it */
633 seq_printf(seqf, "%4d %4d %10llu %s\n",
634 MAJOR(disk_devt(sgp)), MINOR(disk_devt(sgp)),
635 (unsigned long long)get_capacity(sgp) >> 1,
636 disk_name(sgp, 0, buf));
637
638 disk_part_iter_init(&piter, sgp, 0);
639 while ((part = disk_part_iter_next(&piter)))
640 seq_printf(seqf, "%4d %4d %10llu %s\n",
641 MAJOR(part_devt(part)), MINOR(part_devt(part)),
642 (unsigned long long)part->nr_sects >> 1,
643 disk_name(sgp, part->partno, buf));
644 disk_part_iter_exit(&piter);
645
646 return 0;
647 }
648
649 const struct seq_operations partitions_op = {
650 .start = show_partition_start,
651 .next = disk_seqf_next,
652 .stop = disk_seqf_stop,
653 .show = show_partition
654 };
655 #endif
656
657
658 static struct kobject *base_probe(dev_t devt, int *partno, void *data)
659 {
660 if (request_module("block-major-%d-%d", MAJOR(devt), MINOR(devt)) > 0)
661 /* Make old-style 2.4 aliases work */
662 request_module("block-major-%d", MAJOR(devt));
663 return NULL;
664 }
665
666 static int __init genhd_device_init(void)
667 {
668 int error;
669
670 block_class.dev_kobj = sysfs_dev_block_kobj;
671 error = class_register(&block_class);
672 if (unlikely(error))
673 return error;
674 bdev_map = kobj_map_init(base_probe, &block_class_lock);
675 blk_dev_init();
676
677 #ifndef CONFIG_SYSFS_DEPRECATED
678 /* create top-level block dir */
679 block_depr = kobject_create_and_add("block", NULL);
680 #endif
681 return 0;
682 }
683
684 subsys_initcall(genhd_device_init);
685
686 static ssize_t disk_range_show(struct device *dev,
687 struct device_attribute *attr, char *buf)
688 {
689 struct gendisk *disk = dev_to_disk(dev);
690
691 return sprintf(buf, "%d\n", disk->minors);
692 }
693
694 static ssize_t disk_removable_show(struct device *dev,
695 struct device_attribute *attr, char *buf)
696 {
697 struct gendisk *disk = dev_to_disk(dev);
698
699 return sprintf(buf, "%d\n",
700 (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
701 }
702
703 static ssize_t disk_ro_show(struct device *dev,
704 struct device_attribute *attr, char *buf)
705 {
706 struct gendisk *disk = dev_to_disk(dev);
707
708 return sprintf(buf, "%d\n", disk->policy ? 1 : 0);
709 }
710
711 static ssize_t disk_size_show(struct device *dev,
712 struct device_attribute *attr, char *buf)
713 {
714 struct gendisk *disk = dev_to_disk(dev);
715
716 return sprintf(buf, "%llu\n", (unsigned long long)get_capacity(disk));
717 }
718
719 static ssize_t disk_capability_show(struct device *dev,
720 struct device_attribute *attr, char *buf)
721 {
722 struct gendisk *disk = dev_to_disk(dev);
723
724 return sprintf(buf, "%x\n", disk->flags);
725 }
726
727 static ssize_t disk_stat_show(struct device *dev,
728 struct device_attribute *attr, char *buf)
729 {
730 struct gendisk *disk = dev_to_disk(dev);
731 int cpu;
732
733 cpu = disk_stat_lock();
734 disk_round_stats(cpu, disk);
735 disk_stat_unlock();
736 return sprintf(buf,
737 "%8lu %8lu %8llu %8u "
738 "%8lu %8lu %8llu %8u "
739 "%8u %8u %8u"
740 "\n",
741 disk_stat_read(disk, ios[READ]),
742 disk_stat_read(disk, merges[READ]),
743 (unsigned long long)disk_stat_read(disk, sectors[READ]),
744 jiffies_to_msecs(disk_stat_read(disk, ticks[READ])),
745 disk_stat_read(disk, ios[WRITE]),
746 disk_stat_read(disk, merges[WRITE]),
747 (unsigned long long)disk_stat_read(disk, sectors[WRITE]),
748 jiffies_to_msecs(disk_stat_read(disk, ticks[WRITE])),
749 disk->in_flight,
750 jiffies_to_msecs(disk_stat_read(disk, io_ticks)),
751 jiffies_to_msecs(disk_stat_read(disk, time_in_queue)));
752 }
753
754 #ifdef CONFIG_FAIL_MAKE_REQUEST
755 static ssize_t disk_fail_show(struct device *dev,
756 struct device_attribute *attr, char *buf)
757 {
758 struct gendisk *disk = dev_to_disk(dev);
759
760 return sprintf(buf, "%d\n", disk->flags & GENHD_FL_FAIL ? 1 : 0);
761 }
762
763 static ssize_t disk_fail_store(struct device *dev,
764 struct device_attribute *attr,
765 const char *buf, size_t count)
766 {
767 struct gendisk *disk = dev_to_disk(dev);
768 int i;
769
770 if (count > 0 && sscanf(buf, "%d", &i) > 0) {
771 if (i == 0)
772 disk->flags &= ~GENHD_FL_FAIL;
773 else
774 disk->flags |= GENHD_FL_FAIL;
775 }
776
777 return count;
778 }
779
780 #endif
781
782 static DEVICE_ATTR(range, S_IRUGO, disk_range_show, NULL);
783 static DEVICE_ATTR(removable, S_IRUGO, disk_removable_show, NULL);
784 static DEVICE_ATTR(ro, S_IRUGO, disk_ro_show, NULL);
785 static DEVICE_ATTR(size, S_IRUGO, disk_size_show, NULL);
786 static DEVICE_ATTR(capability, S_IRUGO, disk_capability_show, NULL);
787 static DEVICE_ATTR(stat, S_IRUGO, disk_stat_show, NULL);
788 #ifdef CONFIG_FAIL_MAKE_REQUEST
789 static struct device_attribute dev_attr_fail =
790 __ATTR(make-it-fail, S_IRUGO|S_IWUSR, disk_fail_show, disk_fail_store);
791 #endif
792
793 static struct attribute *disk_attrs[] = {
794 &dev_attr_range.attr,
795 &dev_attr_removable.attr,
796 &dev_attr_ro.attr,
797 &dev_attr_size.attr,
798 &dev_attr_capability.attr,
799 &dev_attr_stat.attr,
800 #ifdef CONFIG_FAIL_MAKE_REQUEST
801 &dev_attr_fail.attr,
802 #endif
803 NULL
804 };
805
806 static struct attribute_group disk_attr_group = {
807 .attrs = disk_attrs,
808 };
809
810 static struct attribute_group *disk_attr_groups[] = {
811 &disk_attr_group,
812 NULL
813 };
814
815 static void disk_release(struct device *dev)
816 {
817 struct gendisk *disk = dev_to_disk(dev);
818
819 kfree(disk->random);
820 kfree(disk->__part);
821 free_disk_stats(disk);
822 kfree(disk);
823 }
824 struct class block_class = {
825 .name = "block",
826 };
827
828 static struct device_type disk_type = {
829 .name = "disk",
830 .groups = disk_attr_groups,
831 .release = disk_release,
832 };
833
834 #ifdef CONFIG_PROC_FS
835 /*
836 * aggregate disk stat collector. Uses the same stats that the sysfs
837 * entries do, above, but makes them available through one seq_file.
838 *
839 * The output looks suspiciously like /proc/partitions with a bunch of
840 * extra fields.
841 */
842 static int diskstats_show(struct seq_file *seqf, void *v)
843 {
844 struct gendisk *gp = v;
845 struct disk_part_iter piter;
846 struct hd_struct *hd;
847 char buf[BDEVNAME_SIZE];
848 int cpu;
849
850 /*
851 if (&gp->dev.kobj.entry == block_class.devices.next)
852 seq_puts(seqf, "major minor name"
853 " rio rmerge rsect ruse wio wmerge "
854 "wsect wuse running use aveq"
855 "\n\n");
856 */
857
858 cpu = disk_stat_lock();
859 disk_round_stats(cpu, gp);
860 disk_stat_unlock();
861 seq_printf(seqf, "%4d %4d %s %lu %lu %llu %u %lu %lu %llu %u %u %u %u\n",
862 MAJOR(disk_devt(gp)), MINOR(disk_devt(gp)),
863 disk_name(gp, 0, buf),
864 disk_stat_read(gp, ios[0]), disk_stat_read(gp, merges[0]),
865 (unsigned long long)disk_stat_read(gp, sectors[0]),
866 jiffies_to_msecs(disk_stat_read(gp, ticks[0])),
867 disk_stat_read(gp, ios[1]), disk_stat_read(gp, merges[1]),
868 (unsigned long long)disk_stat_read(gp, sectors[1]),
869 jiffies_to_msecs(disk_stat_read(gp, ticks[1])),
870 gp->in_flight,
871 jiffies_to_msecs(disk_stat_read(gp, io_ticks)),
872 jiffies_to_msecs(disk_stat_read(gp, time_in_queue)));
873
874 /* now show all non-0 size partitions of it */
875 disk_part_iter_init(&piter, gp, 0);
876 while ((hd = disk_part_iter_next(&piter))) {
877 cpu = disk_stat_lock();
878 part_round_stats(cpu, hd);
879 disk_stat_unlock();
880 seq_printf(seqf, "%4d %4d %s %lu %lu %llu "
881 "%u %lu %lu %llu %u %u %u %u\n",
882 MAJOR(part_devt(hd)), MINOR(part_devt(hd)),
883 disk_name(gp, hd->partno, buf),
884 part_stat_read(hd, ios[0]),
885 part_stat_read(hd, merges[0]),
886 (unsigned long long)part_stat_read(hd, sectors[0]),
887 jiffies_to_msecs(part_stat_read(hd, ticks[0])),
888 part_stat_read(hd, ios[1]),
889 part_stat_read(hd, merges[1]),
890 (unsigned long long)part_stat_read(hd, sectors[1]),
891 jiffies_to_msecs(part_stat_read(hd, ticks[1])),
892 hd->in_flight,
893 jiffies_to_msecs(part_stat_read(hd, io_ticks)),
894 jiffies_to_msecs(part_stat_read(hd, time_in_queue))
895 );
896 }
897 disk_part_iter_exit(&piter);
898
899 return 0;
900 }
901
902 const struct seq_operations diskstats_op = {
903 .start = disk_seqf_start,
904 .next = disk_seqf_next,
905 .stop = disk_seqf_stop,
906 .show = diskstats_show
907 };
908 #endif /* CONFIG_PROC_FS */
909
910 static void media_change_notify_thread(struct work_struct *work)
911 {
912 struct gendisk *gd = container_of(work, struct gendisk, async_notify);
913 char event[] = "MEDIA_CHANGE=1";
914 char *envp[] = { event, NULL };
915
916 /*
917 * set enviroment vars to indicate which event this is for
918 * so that user space will know to go check the media status.
919 */
920 kobject_uevent_env(&gd->dev.kobj, KOBJ_CHANGE, envp);
921 put_device(gd->driverfs_dev);
922 }
923
924 #if 0
925 void genhd_media_change_notify(struct gendisk *disk)
926 {
927 get_device(disk->driverfs_dev);
928 schedule_work(&disk->async_notify);
929 }
930 EXPORT_SYMBOL_GPL(genhd_media_change_notify);
931 #endif /* 0 */
932
933 dev_t blk_lookup_devt(const char *name, int partno)
934 {
935 dev_t devt = MKDEV(0, 0);
936 struct class_dev_iter iter;
937 struct device *dev;
938
939 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
940 while ((dev = class_dev_iter_next(&iter))) {
941 struct gendisk *disk = dev_to_disk(dev);
942
943 if (strcmp(dev->bus_id, name))
944 continue;
945 if (partno < 0 || partno > disk_max_parts(disk))
946 continue;
947
948 if (partno == 0)
949 devt = disk_devt(disk);
950 else {
951 struct hd_struct *part;
952
953 part = disk_get_part(disk, partno);
954 if (!part || !part->nr_sects) {
955 disk_put_part(part);
956 continue;
957 }
958
959 devt = part_devt(part);
960 disk_put_part(part);
961 }
962 break;
963 }
964 class_dev_iter_exit(&iter);
965 return devt;
966 }
967 EXPORT_SYMBOL(blk_lookup_devt);
968
969 struct gendisk *alloc_disk(int minors)
970 {
971 return alloc_disk_node(minors, -1);
972 }
973
974 struct gendisk *alloc_disk_node(int minors, int node_id)
975 {
976 return alloc_disk_ext_node(minors, 0, node_id);
977 }
978
979 struct gendisk *alloc_disk_ext(int minors, int ext_minors)
980 {
981 return alloc_disk_ext_node(minors, ext_minors, -1);
982 }
983
984 struct gendisk *alloc_disk_ext_node(int minors, int ext_minors, int node_id)
985 {
986 struct gendisk *disk;
987
988 disk = kmalloc_node(sizeof(struct gendisk),
989 GFP_KERNEL | __GFP_ZERO, node_id);
990 if (disk) {
991 int tot_minors = minors + ext_minors;
992
993 if (!init_disk_stats(disk)) {
994 kfree(disk);
995 return NULL;
996 }
997 if (tot_minors > 1) {
998 int size = (tot_minors - 1) * sizeof(struct hd_struct *);
999 disk->__part = kmalloc_node(size,
1000 GFP_KERNEL | __GFP_ZERO, node_id);
1001 if (!disk->__part) {
1002 free_disk_stats(disk);
1003 kfree(disk);
1004 return NULL;
1005 }
1006 }
1007 disk->minors = minors;
1008 disk->ext_minors = ext_minors;
1009 rand_initialize_disk(disk);
1010 disk->dev.class = &block_class;
1011 disk->dev.type = &disk_type;
1012 device_initialize(&disk->dev);
1013 INIT_WORK(&disk->async_notify,
1014 media_change_notify_thread);
1015 }
1016 return disk;
1017 }
1018
1019 EXPORT_SYMBOL(alloc_disk);
1020 EXPORT_SYMBOL(alloc_disk_node);
1021 EXPORT_SYMBOL(alloc_disk_ext);
1022 EXPORT_SYMBOL(alloc_disk_ext_node);
1023
1024 struct kobject *get_disk(struct gendisk *disk)
1025 {
1026 struct module *owner;
1027 struct kobject *kobj;
1028
1029 if (!disk->fops)
1030 return NULL;
1031 owner = disk->fops->owner;
1032 if (owner && !try_module_get(owner))
1033 return NULL;
1034 kobj = kobject_get(&disk->dev.kobj);
1035 if (kobj == NULL) {
1036 module_put(owner);
1037 return NULL;
1038 }
1039 return kobj;
1040
1041 }
1042
1043 EXPORT_SYMBOL(get_disk);
1044
1045 void put_disk(struct gendisk *disk)
1046 {
1047 if (disk)
1048 kobject_put(&disk->dev.kobj);
1049 }
1050
1051 EXPORT_SYMBOL(put_disk);
1052
1053 void set_device_ro(struct block_device *bdev, int flag)
1054 {
1055 if (bdev->bd_contains != bdev)
1056 bdev->bd_part->policy = flag;
1057 else
1058 bdev->bd_disk->policy = flag;
1059 }
1060
1061 EXPORT_SYMBOL(set_device_ro);
1062
1063 void set_disk_ro(struct gendisk *disk, int flag)
1064 {
1065 struct disk_part_iter piter;
1066 struct hd_struct *part;
1067
1068 disk->policy = flag;
1069 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_EMPTY);
1070 while ((part = disk_part_iter_next(&piter)))
1071 part->policy = flag;
1072 disk_part_iter_exit(&piter);
1073 }
1074
1075 EXPORT_SYMBOL(set_disk_ro);
1076
1077 int bdev_read_only(struct block_device *bdev)
1078 {
1079 if (!bdev)
1080 return 0;
1081 else if (bdev->bd_contains != bdev)
1082 return bdev->bd_part->policy;
1083 else
1084 return bdev->bd_disk->policy;
1085 }
1086
1087 EXPORT_SYMBOL(bdev_read_only);
1088
1089 int invalidate_partition(struct gendisk *disk, int partno)
1090 {
1091 int res = 0;
1092 struct block_device *bdev = bdget_disk(disk, partno);
1093 if (bdev) {
1094 fsync_bdev(bdev);
1095 res = __invalidate_device(bdev);
1096 bdput(bdev);
1097 }
1098 return res;
1099 }
1100
1101 EXPORT_SYMBOL(invalidate_partition);