]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame_incremental - block/genhd.c
sd/ide-disk: apply extended minors to sd and ide
[mirror_ubuntu-bionic-kernel.git] / block / genhd.c
... / ...
CommitLineData
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
23static DEFINE_MUTEX(block_class_lock);
24#ifndef CONFIG_SYSFS_DEPRECATED
25struct 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 */
34static DEFINE_MUTEX(ext_devt_mutex);
35static DEFINE_IDR(ext_devt_idr);
36
37static 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 */
53struct 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}
67EXPORT_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 */
80void 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}
93EXPORT_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 */
104struct 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}
143EXPORT_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 */
154void disk_part_iter_exit(struct disk_part_iter *piter)
155{
156 disk_put_part(piter->part);
157 piter->part = NULL;
158}
159EXPORT_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 */
176struct 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}
189EXPORT_SYMBOL_GPL(disk_map_sector_rcu);
190
191/*
192 * Can be deleted altogether. Later.
193 *
194 */
195static 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 */
202static inline int major_to_index(int major)
203{
204 return major % BLKDEV_MAJOR_HASH_SIZE;
205}
206
207#ifdef CONFIG_PROC_FS
208void 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
221int 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 }
270out:
271 mutex_unlock(&block_class_lock);
272 return ret;
273}
274
275EXPORT_SYMBOL(register_blkdev);
276
277void 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
297EXPORT_SYMBOL(unregister_blkdev);
298
299static 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 */
316int 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 */
355void 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
369static char *bdevt_str(dev_t devt, char *buf)
370{
371 if (MAJOR(devt) <= 0xff && MINOR(devt) <= 0xff) {
372 char tbuf[BDEVT_SIZE];
373 snprintf(tbuf, BDEVT_SIZE, "%02x%02x", MAJOR(devt), MINOR(devt));
374 snprintf(buf, BDEVT_SIZE, "%-9s", tbuf);
375 } else
376 snprintf(buf, BDEVT_SIZE, "%03x:%05x", MAJOR(devt), MINOR(devt));
377
378 return buf;
379}
380
381/*
382 * Register device numbers dev..(dev+range-1)
383 * range must be nonzero
384 * The hash chain is sorted on range, so that subranges can override.
385 */
386void blk_register_region(dev_t devt, unsigned long range, struct module *module,
387 struct kobject *(*probe)(dev_t, int *, void *),
388 int (*lock)(dev_t, void *), void *data)
389{
390 kobj_map(bdev_map, devt, range, module, probe, lock, data);
391}
392
393EXPORT_SYMBOL(blk_register_region);
394
395void blk_unregister_region(dev_t devt, unsigned long range)
396{
397 kobj_unmap(bdev_map, devt, range);
398}
399
400EXPORT_SYMBOL(blk_unregister_region);
401
402static struct kobject *exact_match(dev_t devt, int *partno, void *data)
403{
404 struct gendisk *p = data;
405
406 return &p->dev.kobj;
407}
408
409static int exact_lock(dev_t devt, void *data)
410{
411 struct gendisk *p = data;
412
413 if (!get_disk(p))
414 return -1;
415 return 0;
416}
417
418/**
419 * add_disk - add partitioning information to kernel list
420 * @disk: per-device partitioning information
421 *
422 * This function registers the partitioning information in @disk
423 * with the kernel.
424 */
425void add_disk(struct gendisk *disk)
426{
427 struct backing_dev_info *bdi;
428 int retval;
429
430 disk->flags |= GENHD_FL_UP;
431 disk->dev.devt = MKDEV(disk->major, disk->first_minor);
432 blk_register_region(disk_devt(disk), disk->minors, NULL,
433 exact_match, exact_lock, disk);
434 register_disk(disk);
435 blk_register_queue(disk);
436
437 bdi = &disk->queue->backing_dev_info;
438 bdi_register_dev(bdi, disk_devt(disk));
439 retval = sysfs_create_link(&disk->dev.kobj, &bdi->dev->kobj, "bdi");
440 WARN_ON(retval);
441}
442
443EXPORT_SYMBOL(add_disk);
444EXPORT_SYMBOL(del_gendisk); /* in partitions/check.c */
445
446void unlink_gendisk(struct gendisk *disk)
447{
448 sysfs_remove_link(&disk->dev.kobj, "bdi");
449 bdi_unregister(&disk->queue->backing_dev_info);
450 blk_unregister_queue(disk);
451 blk_unregister_region(disk_devt(disk), disk->minors);
452}
453
454/**
455 * get_gendisk - get partitioning information for a given device
456 * @devt: device to get partitioning information for
457 * @part: returned partition index
458 *
459 * This function gets the structure containing partitioning
460 * information for the given device @devt.
461 */
462struct gendisk *get_gendisk(dev_t devt, int *partno)
463{
464 struct gendisk *disk = NULL;
465
466 if (MAJOR(devt) != BLOCK_EXT_MAJOR) {
467 struct kobject *kobj;
468
469 kobj = kobj_lookup(bdev_map, devt, partno);
470 if (kobj)
471 disk = dev_to_disk(kobj_to_dev(kobj));
472 } else {
473 struct hd_struct *part;
474
475 mutex_lock(&ext_devt_mutex);
476 part = idr_find(&ext_devt_idr, MINOR(devt));
477 if (part && get_disk(part_to_disk(part))) {
478 *partno = part->partno;
479 disk = part_to_disk(part);
480 }
481 mutex_unlock(&ext_devt_mutex);
482 }
483
484 return disk;
485}
486
487/**
488 * bdget_disk - do bdget() by gendisk and partition number
489 * @disk: gendisk of interest
490 * @partno: partition number
491 *
492 * Find partition @partno from @disk, do bdget() on it.
493 *
494 * CONTEXT:
495 * Don't care.
496 *
497 * RETURNS:
498 * Resulting block_device on success, NULL on failure.
499 */
500extern struct block_device *bdget_disk(struct gendisk *disk, int partno)
501{
502 dev_t devt = MKDEV(0, 0);
503
504 if (partno == 0)
505 devt = disk_devt(disk);
506 else {
507 struct hd_struct *part;
508
509 part = disk_get_part(disk, partno);
510 if (part && part->nr_sects)
511 devt = part_devt(part);
512 disk_put_part(part);
513 }
514
515 if (likely(devt != MKDEV(0, 0)))
516 return bdget(devt);
517 return NULL;
518}
519EXPORT_SYMBOL(bdget_disk);
520
521/*
522 * print a full list of all partitions - intended for places where the root
523 * filesystem can't be mounted and thus to give the victim some idea of what
524 * went wrong
525 */
526void __init printk_all_partitions(void)
527{
528 struct class_dev_iter iter;
529 struct device *dev;
530
531 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
532 while ((dev = class_dev_iter_next(&iter))) {
533 struct gendisk *disk = dev_to_disk(dev);
534 struct disk_part_iter piter;
535 struct hd_struct *part;
536 char name_buf[BDEVNAME_SIZE];
537 char devt_buf[BDEVT_SIZE];
538
539 /*
540 * Don't show empty devices or things that have been
541 * surpressed
542 */
543 if (get_capacity(disk) == 0 ||
544 (disk->flags & GENHD_FL_SUPPRESS_PARTITION_INFO))
545 continue;
546
547 /*
548 * Note, unlike /proc/partitions, I am showing the
549 * numbers in hex - the same format as the root=
550 * option takes.
551 */
552 printk("%s %10llu %s",
553 bdevt_str(disk_devt(disk), devt_buf),
554 (unsigned long long)get_capacity(disk) >> 1,
555 disk_name(disk, 0, name_buf));
556 if (disk->driverfs_dev != NULL &&
557 disk->driverfs_dev->driver != NULL)
558 printk(" driver: %s\n",
559 disk->driverfs_dev->driver->name);
560 else
561 printk(" (driver?)\n");
562
563 /* now show the partitions */
564 disk_part_iter_init(&piter, disk, 0);
565 while ((part = disk_part_iter_next(&piter)))
566 printk(" %s %10llu %s\n",
567 bdevt_str(part_devt(part), devt_buf),
568 (unsigned long long)part->nr_sects >> 1,
569 disk_name(disk, part->partno, name_buf));
570 disk_part_iter_exit(&piter);
571 }
572 class_dev_iter_exit(&iter);
573}
574
575#ifdef CONFIG_PROC_FS
576/* iterator */
577static void *disk_seqf_start(struct seq_file *seqf, loff_t *pos)
578{
579 loff_t skip = *pos;
580 struct class_dev_iter *iter;
581 struct device *dev;
582
583 iter = kmalloc(GFP_KERNEL, sizeof(*iter));
584 if (!iter)
585 return ERR_PTR(-ENOMEM);
586
587 seqf->private = iter;
588 class_dev_iter_init(iter, &block_class, NULL, &disk_type);
589 do {
590 dev = class_dev_iter_next(iter);
591 if (!dev)
592 return NULL;
593 } while (skip--);
594
595 return dev_to_disk(dev);
596}
597
598static void *disk_seqf_next(struct seq_file *seqf, void *v, loff_t *pos)
599{
600 struct device *dev;
601
602 (*pos)++;
603 dev = class_dev_iter_next(seqf->private);
604 if (dev)
605 return dev_to_disk(dev);
606
607 return NULL;
608}
609
610static void disk_seqf_stop(struct seq_file *seqf, void *v)
611{
612 struct class_dev_iter *iter = seqf->private;
613
614 /* stop is called even after start failed :-( */
615 if (iter) {
616 class_dev_iter_exit(iter);
617 kfree(iter);
618 }
619}
620
621static void *show_partition_start(struct seq_file *seqf, loff_t *pos)
622{
623 static void *p;
624
625 p = disk_seqf_start(seqf, pos);
626 if (!IS_ERR(p) && p)
627 seq_puts(seqf, "major minor #blocks name\n\n");
628 return p;
629}
630
631static int show_partition(struct seq_file *seqf, void *v)
632{
633 struct gendisk *sgp = v;
634 struct disk_part_iter piter;
635 struct hd_struct *part;
636 char buf[BDEVNAME_SIZE];
637
638 /* Don't show non-partitionable removeable devices or empty devices */
639 if (!get_capacity(sgp) || (!disk_max_parts(sgp) &&
640 (sgp->flags & GENHD_FL_REMOVABLE)))
641 return 0;
642 if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)
643 return 0;
644
645 /* show the full disk and all non-0 size partitions of it */
646 seq_printf(seqf, "%4d %7d %10llu %s\n",
647 MAJOR(disk_devt(sgp)), MINOR(disk_devt(sgp)),
648 (unsigned long long)get_capacity(sgp) >> 1,
649 disk_name(sgp, 0, buf));
650
651 disk_part_iter_init(&piter, sgp, 0);
652 while ((part = disk_part_iter_next(&piter)))
653 seq_printf(seqf, "%4d %7d %10llu %s\n",
654 MAJOR(part_devt(part)), MINOR(part_devt(part)),
655 (unsigned long long)part->nr_sects >> 1,
656 disk_name(sgp, part->partno, buf));
657 disk_part_iter_exit(&piter);
658
659 return 0;
660}
661
662const struct seq_operations partitions_op = {
663 .start = show_partition_start,
664 .next = disk_seqf_next,
665 .stop = disk_seqf_stop,
666 .show = show_partition
667};
668#endif
669
670
671static struct kobject *base_probe(dev_t devt, int *partno, void *data)
672{
673 if (request_module("block-major-%d-%d", MAJOR(devt), MINOR(devt)) > 0)
674 /* Make old-style 2.4 aliases work */
675 request_module("block-major-%d", MAJOR(devt));
676 return NULL;
677}
678
679static int __init genhd_device_init(void)
680{
681 int error;
682
683 block_class.dev_kobj = sysfs_dev_block_kobj;
684 error = class_register(&block_class);
685 if (unlikely(error))
686 return error;
687 bdev_map = kobj_map_init(base_probe, &block_class_lock);
688 blk_dev_init();
689
690#ifndef CONFIG_SYSFS_DEPRECATED
691 /* create top-level block dir */
692 block_depr = kobject_create_and_add("block", NULL);
693#endif
694 return 0;
695}
696
697subsys_initcall(genhd_device_init);
698
699static ssize_t disk_range_show(struct device *dev,
700 struct device_attribute *attr, char *buf)
701{
702 struct gendisk *disk = dev_to_disk(dev);
703
704 return sprintf(buf, "%d\n", disk->minors);
705}
706
707static ssize_t disk_ext_range_show(struct device *dev,
708 struct device_attribute *attr, char *buf)
709{
710 struct gendisk *disk = dev_to_disk(dev);
711
712 return sprintf(buf, "%d\n", disk_max_parts(disk) + 1);
713}
714
715static ssize_t disk_removable_show(struct device *dev,
716 struct device_attribute *attr, char *buf)
717{
718 struct gendisk *disk = dev_to_disk(dev);
719
720 return sprintf(buf, "%d\n",
721 (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
722}
723
724static ssize_t disk_ro_show(struct device *dev,
725 struct device_attribute *attr, char *buf)
726{
727 struct gendisk *disk = dev_to_disk(dev);
728
729 return sprintf(buf, "%d\n", disk->policy ? 1 : 0);
730}
731
732static ssize_t disk_size_show(struct device *dev,
733 struct device_attribute *attr, char *buf)
734{
735 struct gendisk *disk = dev_to_disk(dev);
736
737 return sprintf(buf, "%llu\n", (unsigned long long)get_capacity(disk));
738}
739
740static ssize_t disk_capability_show(struct device *dev,
741 struct device_attribute *attr, char *buf)
742{
743 struct gendisk *disk = dev_to_disk(dev);
744
745 return sprintf(buf, "%x\n", disk->flags);
746}
747
748static ssize_t disk_stat_show(struct device *dev,
749 struct device_attribute *attr, char *buf)
750{
751 struct gendisk *disk = dev_to_disk(dev);
752 int cpu;
753
754 cpu = disk_stat_lock();
755 disk_round_stats(cpu, disk);
756 disk_stat_unlock();
757 return sprintf(buf,
758 "%8lu %8lu %8llu %8u "
759 "%8lu %8lu %8llu %8u "
760 "%8u %8u %8u"
761 "\n",
762 disk_stat_read(disk, ios[READ]),
763 disk_stat_read(disk, merges[READ]),
764 (unsigned long long)disk_stat_read(disk, sectors[READ]),
765 jiffies_to_msecs(disk_stat_read(disk, ticks[READ])),
766 disk_stat_read(disk, ios[WRITE]),
767 disk_stat_read(disk, merges[WRITE]),
768 (unsigned long long)disk_stat_read(disk, sectors[WRITE]),
769 jiffies_to_msecs(disk_stat_read(disk, ticks[WRITE])),
770 disk->in_flight,
771 jiffies_to_msecs(disk_stat_read(disk, io_ticks)),
772 jiffies_to_msecs(disk_stat_read(disk, time_in_queue)));
773}
774
775#ifdef CONFIG_FAIL_MAKE_REQUEST
776static ssize_t disk_fail_show(struct device *dev,
777 struct device_attribute *attr, char *buf)
778{
779 struct gendisk *disk = dev_to_disk(dev);
780
781 return sprintf(buf, "%d\n", disk->flags & GENHD_FL_FAIL ? 1 : 0);
782}
783
784static ssize_t disk_fail_store(struct device *dev,
785 struct device_attribute *attr,
786 const char *buf, size_t count)
787{
788 struct gendisk *disk = dev_to_disk(dev);
789 int i;
790
791 if (count > 0 && sscanf(buf, "%d", &i) > 0) {
792 if (i == 0)
793 disk->flags &= ~GENHD_FL_FAIL;
794 else
795 disk->flags |= GENHD_FL_FAIL;
796 }
797
798 return count;
799}
800
801#endif
802
803static DEVICE_ATTR(range, S_IRUGO, disk_range_show, NULL);
804static DEVICE_ATTR(ext_range, S_IRUGO, disk_ext_range_show, NULL);
805static DEVICE_ATTR(removable, S_IRUGO, disk_removable_show, NULL);
806static DEVICE_ATTR(ro, S_IRUGO, disk_ro_show, NULL);
807static DEVICE_ATTR(size, S_IRUGO, disk_size_show, NULL);
808static DEVICE_ATTR(capability, S_IRUGO, disk_capability_show, NULL);
809static DEVICE_ATTR(stat, S_IRUGO, disk_stat_show, NULL);
810#ifdef CONFIG_FAIL_MAKE_REQUEST
811static struct device_attribute dev_attr_fail =
812 __ATTR(make-it-fail, S_IRUGO|S_IWUSR, disk_fail_show, disk_fail_store);
813#endif
814
815static struct attribute *disk_attrs[] = {
816 &dev_attr_range.attr,
817 &dev_attr_ext_range.attr,
818 &dev_attr_removable.attr,
819 &dev_attr_ro.attr,
820 &dev_attr_size.attr,
821 &dev_attr_capability.attr,
822 &dev_attr_stat.attr,
823#ifdef CONFIG_FAIL_MAKE_REQUEST
824 &dev_attr_fail.attr,
825#endif
826 NULL
827};
828
829static struct attribute_group disk_attr_group = {
830 .attrs = disk_attrs,
831};
832
833static struct attribute_group *disk_attr_groups[] = {
834 &disk_attr_group,
835 NULL
836};
837
838static void disk_release(struct device *dev)
839{
840 struct gendisk *disk = dev_to_disk(dev);
841
842 kfree(disk->random);
843 kfree(disk->__part);
844 free_disk_stats(disk);
845 kfree(disk);
846}
847struct class block_class = {
848 .name = "block",
849};
850
851static struct device_type disk_type = {
852 .name = "disk",
853 .groups = disk_attr_groups,
854 .release = disk_release,
855};
856
857#ifdef CONFIG_PROC_FS
858/*
859 * aggregate disk stat collector. Uses the same stats that the sysfs
860 * entries do, above, but makes them available through one seq_file.
861 *
862 * The output looks suspiciously like /proc/partitions with a bunch of
863 * extra fields.
864 */
865static int diskstats_show(struct seq_file *seqf, void *v)
866{
867 struct gendisk *gp = v;
868 struct disk_part_iter piter;
869 struct hd_struct *hd;
870 char buf[BDEVNAME_SIZE];
871 int cpu;
872
873 /*
874 if (&gp->dev.kobj.entry == block_class.devices.next)
875 seq_puts(seqf, "major minor name"
876 " rio rmerge rsect ruse wio wmerge "
877 "wsect wuse running use aveq"
878 "\n\n");
879 */
880
881 cpu = disk_stat_lock();
882 disk_round_stats(cpu, gp);
883 disk_stat_unlock();
884 seq_printf(seqf, "%4d %7d %s %lu %lu %llu %u %lu %lu %llu %u %u %u %u\n",
885 MAJOR(disk_devt(gp)), MINOR(disk_devt(gp)),
886 disk_name(gp, 0, buf),
887 disk_stat_read(gp, ios[0]), disk_stat_read(gp, merges[0]),
888 (unsigned long long)disk_stat_read(gp, sectors[0]),
889 jiffies_to_msecs(disk_stat_read(gp, ticks[0])),
890 disk_stat_read(gp, ios[1]), disk_stat_read(gp, merges[1]),
891 (unsigned long long)disk_stat_read(gp, sectors[1]),
892 jiffies_to_msecs(disk_stat_read(gp, ticks[1])),
893 gp->in_flight,
894 jiffies_to_msecs(disk_stat_read(gp, io_ticks)),
895 jiffies_to_msecs(disk_stat_read(gp, time_in_queue)));
896
897 /* now show all non-0 size partitions of it */
898 disk_part_iter_init(&piter, gp, 0);
899 while ((hd = disk_part_iter_next(&piter))) {
900 cpu = disk_stat_lock();
901 part_round_stats(cpu, hd);
902 disk_stat_unlock();
903 seq_printf(seqf, "%4d %7d %s %lu %lu %llu "
904 "%u %lu %lu %llu %u %u %u %u\n",
905 MAJOR(part_devt(hd)), MINOR(part_devt(hd)),
906 disk_name(gp, hd->partno, buf),
907 part_stat_read(hd, ios[0]),
908 part_stat_read(hd, merges[0]),
909 (unsigned long long)part_stat_read(hd, sectors[0]),
910 jiffies_to_msecs(part_stat_read(hd, ticks[0])),
911 part_stat_read(hd, ios[1]),
912 part_stat_read(hd, merges[1]),
913 (unsigned long long)part_stat_read(hd, sectors[1]),
914 jiffies_to_msecs(part_stat_read(hd, ticks[1])),
915 hd->in_flight,
916 jiffies_to_msecs(part_stat_read(hd, io_ticks)),
917 jiffies_to_msecs(part_stat_read(hd, time_in_queue))
918 );
919 }
920 disk_part_iter_exit(&piter);
921
922 return 0;
923}
924
925const struct seq_operations diskstats_op = {
926 .start = disk_seqf_start,
927 .next = disk_seqf_next,
928 .stop = disk_seqf_stop,
929 .show = diskstats_show
930};
931#endif /* CONFIG_PROC_FS */
932
933static void media_change_notify_thread(struct work_struct *work)
934{
935 struct gendisk *gd = container_of(work, struct gendisk, async_notify);
936 char event[] = "MEDIA_CHANGE=1";
937 char *envp[] = { event, NULL };
938
939 /*
940 * set enviroment vars to indicate which event this is for
941 * so that user space will know to go check the media status.
942 */
943 kobject_uevent_env(&gd->dev.kobj, KOBJ_CHANGE, envp);
944 put_device(gd->driverfs_dev);
945}
946
947#if 0
948void genhd_media_change_notify(struct gendisk *disk)
949{
950 get_device(disk->driverfs_dev);
951 schedule_work(&disk->async_notify);
952}
953EXPORT_SYMBOL_GPL(genhd_media_change_notify);
954#endif /* 0 */
955
956dev_t blk_lookup_devt(const char *name, int partno)
957{
958 dev_t devt = MKDEV(0, 0);
959 struct class_dev_iter iter;
960 struct device *dev;
961
962 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
963 while ((dev = class_dev_iter_next(&iter))) {
964 struct gendisk *disk = dev_to_disk(dev);
965
966 if (strcmp(dev->bus_id, name))
967 continue;
968 if (partno < 0 || partno > disk_max_parts(disk))
969 continue;
970
971 if (partno == 0)
972 devt = disk_devt(disk);
973 else {
974 struct hd_struct *part;
975
976 part = disk_get_part(disk, partno);
977 if (!part || !part->nr_sects) {
978 disk_put_part(part);
979 continue;
980 }
981
982 devt = part_devt(part);
983 disk_put_part(part);
984 }
985 break;
986 }
987 class_dev_iter_exit(&iter);
988 return devt;
989}
990EXPORT_SYMBOL(blk_lookup_devt);
991
992struct gendisk *alloc_disk(int minors)
993{
994 return alloc_disk_node(minors, -1);
995}
996
997struct gendisk *alloc_disk_node(int minors, int node_id)
998{
999 return alloc_disk_ext_node(minors, 0, node_id);
1000}
1001
1002struct gendisk *alloc_disk_ext(int minors, int ext_minors)
1003{
1004 return alloc_disk_ext_node(minors, ext_minors, -1);
1005}
1006
1007struct gendisk *alloc_disk_ext_node(int minors, int ext_minors, int node_id)
1008{
1009 struct gendisk *disk;
1010
1011 disk = kmalloc_node(sizeof(struct gendisk),
1012 GFP_KERNEL | __GFP_ZERO, node_id);
1013 if (disk) {
1014 int tot_minors = minors + ext_minors;
1015
1016 if (!init_disk_stats(disk)) {
1017 kfree(disk);
1018 return NULL;
1019 }
1020 if (tot_minors > 1) {
1021 int size = (tot_minors - 1) * sizeof(struct hd_struct *);
1022 disk->__part = kmalloc_node(size,
1023 GFP_KERNEL | __GFP_ZERO, node_id);
1024 if (!disk->__part) {
1025 free_disk_stats(disk);
1026 kfree(disk);
1027 return NULL;
1028 }
1029 }
1030 disk->minors = minors;
1031 disk->ext_minors = ext_minors;
1032 rand_initialize_disk(disk);
1033 disk->dev.class = &block_class;
1034 disk->dev.type = &disk_type;
1035 device_initialize(&disk->dev);
1036 INIT_WORK(&disk->async_notify,
1037 media_change_notify_thread);
1038 }
1039 return disk;
1040}
1041
1042EXPORT_SYMBOL(alloc_disk);
1043EXPORT_SYMBOL(alloc_disk_node);
1044EXPORT_SYMBOL(alloc_disk_ext);
1045EXPORT_SYMBOL(alloc_disk_ext_node);
1046
1047struct kobject *get_disk(struct gendisk *disk)
1048{
1049 struct module *owner;
1050 struct kobject *kobj;
1051
1052 if (!disk->fops)
1053 return NULL;
1054 owner = disk->fops->owner;
1055 if (owner && !try_module_get(owner))
1056 return NULL;
1057 kobj = kobject_get(&disk->dev.kobj);
1058 if (kobj == NULL) {
1059 module_put(owner);
1060 return NULL;
1061 }
1062 return kobj;
1063
1064}
1065
1066EXPORT_SYMBOL(get_disk);
1067
1068void put_disk(struct gendisk *disk)
1069{
1070 if (disk)
1071 kobject_put(&disk->dev.kobj);
1072}
1073
1074EXPORT_SYMBOL(put_disk);
1075
1076void set_device_ro(struct block_device *bdev, int flag)
1077{
1078 if (bdev->bd_contains != bdev)
1079 bdev->bd_part->policy = flag;
1080 else
1081 bdev->bd_disk->policy = flag;
1082}
1083
1084EXPORT_SYMBOL(set_device_ro);
1085
1086void set_disk_ro(struct gendisk *disk, int flag)
1087{
1088 struct disk_part_iter piter;
1089 struct hd_struct *part;
1090
1091 disk->policy = flag;
1092 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_EMPTY);
1093 while ((part = disk_part_iter_next(&piter)))
1094 part->policy = flag;
1095 disk_part_iter_exit(&piter);
1096}
1097
1098EXPORT_SYMBOL(set_disk_ro);
1099
1100int bdev_read_only(struct block_device *bdev)
1101{
1102 if (!bdev)
1103 return 0;
1104 else if (bdev->bd_contains != bdev)
1105 return bdev->bd_part->policy;
1106 else
1107 return bdev->bd_disk->policy;
1108}
1109
1110EXPORT_SYMBOL(bdev_read_only);
1111
1112int invalidate_partition(struct gendisk *disk, int partno)
1113{
1114 int res = 0;
1115 struct block_device *bdev = bdget_disk(disk, partno);
1116 if (bdev) {
1117 fsync_bdev(bdev);
1118 res = __invalidate_device(bdev);
1119 bdput(bdev);
1120 }
1121 return res;
1122}
1123
1124EXPORT_SYMBOL(invalidate_partition);