ACPI: thinkpad-acpi: add development version tag
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / block / genhd.c
blob3b743821477f4c1d0df74bc92f9b63c9d621ab54
1 /*
2 * gendisk handling
3 */
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/proc_fs.h>
14 #include <linux/seq_file.h>
15 #include <linux/slab.h>
16 #include <linux/kmod.h>
17 #include <linux/kobj_map.h>
18 #include <linux/buffer_head.h>
19 #include <linux/mutex.h>
20 #include <linux/idr.h>
22 #include "blk.h"
24 static DEFINE_MUTEX(block_class_lock);
25 #ifndef CONFIG_SYSFS_DEPRECATED
26 struct kobject *block_depr;
27 #endif
29 /* for extended dynamic devt allocation, currently only one major is used */
30 #define MAX_EXT_DEVT (1 << MINORBITS)
32 /* For extended devt allocation. ext_devt_mutex prevents look up
33 * results from going away underneath its user.
35 static DEFINE_MUTEX(ext_devt_mutex);
36 static DEFINE_IDR(ext_devt_idr);
38 static struct device_type disk_type;
40 /**
41 * disk_get_part - get partition
42 * @disk: disk to look partition from
43 * @partno: partition number
45 * Look for partition @partno from @disk. If found, increment
46 * reference count and return it.
48 * CONTEXT:
49 * Don't care.
51 * RETURNS:
52 * Pointer to the found partition on success, NULL if not found.
54 struct hd_struct *disk_get_part(struct gendisk *disk, int partno)
56 struct hd_struct *part = NULL;
57 struct disk_part_tbl *ptbl;
59 if (unlikely(partno < 0))
60 return NULL;
62 rcu_read_lock();
64 ptbl = rcu_dereference(disk->part_tbl);
65 if (likely(partno < ptbl->len)) {
66 part = rcu_dereference(ptbl->part[partno]);
67 if (part)
68 get_device(part_to_dev(part));
71 rcu_read_unlock();
73 return part;
75 EXPORT_SYMBOL_GPL(disk_get_part);
77 /**
78 * disk_part_iter_init - initialize partition iterator
79 * @piter: iterator to initialize
80 * @disk: disk to iterate over
81 * @flags: DISK_PITER_* flags
83 * Initialize @piter so that it iterates over partitions of @disk.
85 * CONTEXT:
86 * Don't care.
88 void disk_part_iter_init(struct disk_part_iter *piter, struct gendisk *disk,
89 unsigned int flags)
91 struct disk_part_tbl *ptbl;
93 rcu_read_lock();
94 ptbl = rcu_dereference(disk->part_tbl);
96 piter->disk = disk;
97 piter->part = NULL;
99 if (flags & DISK_PITER_REVERSE)
100 piter->idx = ptbl->len - 1;
101 else if (flags & (DISK_PITER_INCL_PART0 | DISK_PITER_INCL_EMPTY_PART0))
102 piter->idx = 0;
103 else
104 piter->idx = 1;
106 piter->flags = flags;
108 rcu_read_unlock();
110 EXPORT_SYMBOL_GPL(disk_part_iter_init);
113 * disk_part_iter_next - proceed iterator to the next partition and return it
114 * @piter: iterator of interest
116 * Proceed @piter to the next partition and return it.
118 * CONTEXT:
119 * Don't care.
121 struct hd_struct *disk_part_iter_next(struct disk_part_iter *piter)
123 struct disk_part_tbl *ptbl;
124 int inc, end;
126 /* put the last partition */
127 disk_put_part(piter->part);
128 piter->part = NULL;
130 /* get part_tbl */
131 rcu_read_lock();
132 ptbl = rcu_dereference(piter->disk->part_tbl);
134 /* determine iteration parameters */
135 if (piter->flags & DISK_PITER_REVERSE) {
136 inc = -1;
137 if (piter->flags & (DISK_PITER_INCL_PART0 |
138 DISK_PITER_INCL_EMPTY_PART0))
139 end = -1;
140 else
141 end = 0;
142 } else {
143 inc = 1;
144 end = ptbl->len;
147 /* iterate to the next partition */
148 for (; piter->idx != end; piter->idx += inc) {
149 struct hd_struct *part;
151 part = rcu_dereference(ptbl->part[piter->idx]);
152 if (!part)
153 continue;
154 if (!part->nr_sects &&
155 !(piter->flags & DISK_PITER_INCL_EMPTY) &&
156 !(piter->flags & DISK_PITER_INCL_EMPTY_PART0 &&
157 piter->idx == 0))
158 continue;
160 get_device(part_to_dev(part));
161 piter->part = part;
162 piter->idx += inc;
163 break;
166 rcu_read_unlock();
168 return piter->part;
170 EXPORT_SYMBOL_GPL(disk_part_iter_next);
173 * disk_part_iter_exit - finish up partition iteration
174 * @piter: iter of interest
176 * Called when iteration is over. Cleans up @piter.
178 * CONTEXT:
179 * Don't care.
181 void disk_part_iter_exit(struct disk_part_iter *piter)
183 disk_put_part(piter->part);
184 piter->part = NULL;
186 EXPORT_SYMBOL_GPL(disk_part_iter_exit);
189 * disk_map_sector_rcu - map sector to partition
190 * @disk: gendisk of interest
191 * @sector: sector to map
193 * Find out which partition @sector maps to on @disk. This is
194 * primarily used for stats accounting.
196 * CONTEXT:
197 * RCU read locked. The returned partition pointer is valid only
198 * while preemption is disabled.
200 * RETURNS:
201 * Found partition on success, part0 is returned if no partition matches
203 struct hd_struct *disk_map_sector_rcu(struct gendisk *disk, sector_t sector)
205 struct disk_part_tbl *ptbl;
206 int i;
208 ptbl = rcu_dereference(disk->part_tbl);
210 for (i = 1; i < ptbl->len; i++) {
211 struct hd_struct *part = rcu_dereference(ptbl->part[i]);
213 if (part && part->start_sect <= sector &&
214 sector < part->start_sect + part->nr_sects)
215 return part;
217 return &disk->part0;
219 EXPORT_SYMBOL_GPL(disk_map_sector_rcu);
222 * Can be deleted altogether. Later.
225 static struct blk_major_name {
226 struct blk_major_name *next;
227 int major;
228 char name[16];
229 } *major_names[BLKDEV_MAJOR_HASH_SIZE];
231 /* index in the above - for now: assume no multimajor ranges */
232 static inline int major_to_index(int major)
234 return major % BLKDEV_MAJOR_HASH_SIZE;
237 #ifdef CONFIG_PROC_FS
238 void blkdev_show(struct seq_file *seqf, off_t offset)
240 struct blk_major_name *dp;
242 if (offset < BLKDEV_MAJOR_HASH_SIZE) {
243 mutex_lock(&block_class_lock);
244 for (dp = major_names[offset]; dp; dp = dp->next)
245 seq_printf(seqf, "%3d %s\n", dp->major, dp->name);
246 mutex_unlock(&block_class_lock);
249 #endif /* CONFIG_PROC_FS */
251 int register_blkdev(unsigned int major, const char *name)
253 struct blk_major_name **n, *p;
254 int index, ret = 0;
256 mutex_lock(&block_class_lock);
258 /* temporary */
259 if (major == 0) {
260 for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) {
261 if (major_names[index] == NULL)
262 break;
265 if (index == 0) {
266 printk("register_blkdev: failed to get major for %s\n",
267 name);
268 ret = -EBUSY;
269 goto out;
271 major = index;
272 ret = major;
275 p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL);
276 if (p == NULL) {
277 ret = -ENOMEM;
278 goto out;
281 p->major = major;
282 strlcpy(p->name, name, sizeof(p->name));
283 p->next = NULL;
284 index = major_to_index(major);
286 for (n = &major_names[index]; *n; n = &(*n)->next) {
287 if ((*n)->major == major)
288 break;
290 if (!*n)
291 *n = p;
292 else
293 ret = -EBUSY;
295 if (ret < 0) {
296 printk("register_blkdev: cannot get major %d for %s\n",
297 major, name);
298 kfree(p);
300 out:
301 mutex_unlock(&block_class_lock);
302 return ret;
305 EXPORT_SYMBOL(register_blkdev);
307 void unregister_blkdev(unsigned int major, const char *name)
309 struct blk_major_name **n;
310 struct blk_major_name *p = NULL;
311 int index = major_to_index(major);
313 mutex_lock(&block_class_lock);
314 for (n = &major_names[index]; *n; n = &(*n)->next)
315 if ((*n)->major == major)
316 break;
317 if (!*n || strcmp((*n)->name, name)) {
318 WARN_ON(1);
319 } else {
320 p = *n;
321 *n = p->next;
323 mutex_unlock(&block_class_lock);
324 kfree(p);
327 EXPORT_SYMBOL(unregister_blkdev);
329 static struct kobj_map *bdev_map;
332 * blk_mangle_minor - scatter minor numbers apart
333 * @minor: minor number to mangle
335 * Scatter consecutively allocated @minor number apart if MANGLE_DEVT
336 * is enabled. Mangling twice gives the original value.
338 * RETURNS:
339 * Mangled value.
341 * CONTEXT:
342 * Don't care.
344 static int blk_mangle_minor(int minor)
346 #ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT
347 int i;
349 for (i = 0; i < MINORBITS / 2; i++) {
350 int low = minor & (1 << i);
351 int high = minor & (1 << (MINORBITS - 1 - i));
352 int distance = MINORBITS - 1 - 2 * i;
354 minor ^= low | high; /* clear both bits */
355 low <<= distance; /* swap the positions */
356 high >>= distance;
357 minor |= low | high; /* and set */
359 #endif
360 return minor;
364 * blk_alloc_devt - allocate a dev_t for a partition
365 * @part: partition to allocate dev_t for
366 * @devt: out parameter for resulting dev_t
368 * Allocate a dev_t for block device.
370 * RETURNS:
371 * 0 on success, allocated dev_t is returned in *@devt. -errno on
372 * failure.
374 * CONTEXT:
375 * Might sleep.
377 int blk_alloc_devt(struct hd_struct *part, dev_t *devt)
379 struct gendisk *disk = part_to_disk(part);
380 int idx, rc;
382 /* in consecutive minor range? */
383 if (part->partno < disk->minors) {
384 *devt = MKDEV(disk->major, disk->first_minor + part->partno);
385 return 0;
388 /* allocate ext devt */
389 do {
390 if (!idr_pre_get(&ext_devt_idr, GFP_KERNEL))
391 return -ENOMEM;
392 rc = idr_get_new(&ext_devt_idr, part, &idx);
393 } while (rc == -EAGAIN);
395 if (rc)
396 return rc;
398 if (idx > MAX_EXT_DEVT) {
399 idr_remove(&ext_devt_idr, idx);
400 return -EBUSY;
403 *devt = MKDEV(BLOCK_EXT_MAJOR, blk_mangle_minor(idx));
404 return 0;
408 * blk_free_devt - free a dev_t
409 * @devt: dev_t to free
411 * Free @devt which was allocated using blk_alloc_devt().
413 * CONTEXT:
414 * Might sleep.
416 void blk_free_devt(dev_t devt)
418 might_sleep();
420 if (devt == MKDEV(0, 0))
421 return;
423 if (MAJOR(devt) == BLOCK_EXT_MAJOR) {
424 mutex_lock(&ext_devt_mutex);
425 idr_remove(&ext_devt_idr, blk_mangle_minor(MINOR(devt)));
426 mutex_unlock(&ext_devt_mutex);
430 static char *bdevt_str(dev_t devt, char *buf)
432 if (MAJOR(devt) <= 0xff && MINOR(devt) <= 0xff) {
433 char tbuf[BDEVT_SIZE];
434 snprintf(tbuf, BDEVT_SIZE, "%02x%02x", MAJOR(devt), MINOR(devt));
435 snprintf(buf, BDEVT_SIZE, "%-9s", tbuf);
436 } else
437 snprintf(buf, BDEVT_SIZE, "%03x:%05x", MAJOR(devt), MINOR(devt));
439 return buf;
443 * Register device numbers dev..(dev+range-1)
444 * range must be nonzero
445 * The hash chain is sorted on range, so that subranges can override.
447 void blk_register_region(dev_t devt, unsigned long range, struct module *module,
448 struct kobject *(*probe)(dev_t, int *, void *),
449 int (*lock)(dev_t, void *), void *data)
451 kobj_map(bdev_map, devt, range, module, probe, lock, data);
454 EXPORT_SYMBOL(blk_register_region);
456 void blk_unregister_region(dev_t devt, unsigned long range)
458 kobj_unmap(bdev_map, devt, range);
461 EXPORT_SYMBOL(blk_unregister_region);
463 static struct kobject *exact_match(dev_t devt, int *partno, void *data)
465 struct gendisk *p = data;
467 return &disk_to_dev(p)->kobj;
470 static int exact_lock(dev_t devt, void *data)
472 struct gendisk *p = data;
474 if (!get_disk(p))
475 return -1;
476 return 0;
480 * add_disk - add partitioning information to kernel list
481 * @disk: per-device partitioning information
483 * This function registers the partitioning information in @disk
484 * with the kernel.
486 * FIXME: error handling
488 void add_disk(struct gendisk *disk)
490 struct backing_dev_info *bdi;
491 dev_t devt;
492 int retval;
494 /* minors == 0 indicates to use ext devt from part0 and should
495 * be accompanied with EXT_DEVT flag. Make sure all
496 * parameters make sense.
498 WARN_ON(disk->minors && !(disk->major || disk->first_minor));
499 WARN_ON(!disk->minors && !(disk->flags & GENHD_FL_EXT_DEVT));
501 disk->flags |= GENHD_FL_UP;
503 retval = blk_alloc_devt(&disk->part0, &devt);
504 if (retval) {
505 WARN_ON(1);
506 return;
508 disk_to_dev(disk)->devt = devt;
510 /* ->major and ->first_minor aren't supposed to be
511 * dereferenced from here on, but set them just in case.
513 disk->major = MAJOR(devt);
514 disk->first_minor = MINOR(devt);
516 blk_register_region(disk_devt(disk), disk->minors, NULL,
517 exact_match, exact_lock, disk);
518 register_disk(disk);
519 blk_register_queue(disk);
521 bdi = &disk->queue->backing_dev_info;
522 bdi_register_dev(bdi, disk_devt(disk));
523 retval = sysfs_create_link(&disk_to_dev(disk)->kobj, &bdi->dev->kobj,
524 "bdi");
525 WARN_ON(retval);
528 EXPORT_SYMBOL(add_disk);
529 EXPORT_SYMBOL(del_gendisk); /* in partitions/check.c */
531 void unlink_gendisk(struct gendisk *disk)
533 sysfs_remove_link(&disk_to_dev(disk)->kobj, "bdi");
534 bdi_unregister(&disk->queue->backing_dev_info);
535 blk_unregister_queue(disk);
536 blk_unregister_region(disk_devt(disk), disk->minors);
540 * get_gendisk - get partitioning information for a given device
541 * @devt: device to get partitioning information for
542 * @partno: returned partition index
544 * This function gets the structure containing partitioning
545 * information for the given device @devt.
547 struct gendisk *get_gendisk(dev_t devt, int *partno)
549 struct gendisk *disk = NULL;
551 if (MAJOR(devt) != BLOCK_EXT_MAJOR) {
552 struct kobject *kobj;
554 kobj = kobj_lookup(bdev_map, devt, partno);
555 if (kobj)
556 disk = dev_to_disk(kobj_to_dev(kobj));
557 } else {
558 struct hd_struct *part;
560 mutex_lock(&ext_devt_mutex);
561 part = idr_find(&ext_devt_idr, blk_mangle_minor(MINOR(devt)));
562 if (part && get_disk(part_to_disk(part))) {
563 *partno = part->partno;
564 disk = part_to_disk(part);
566 mutex_unlock(&ext_devt_mutex);
569 return disk;
573 * bdget_disk - do bdget() by gendisk and partition number
574 * @disk: gendisk of interest
575 * @partno: partition number
577 * Find partition @partno from @disk, do bdget() on it.
579 * CONTEXT:
580 * Don't care.
582 * RETURNS:
583 * Resulting block_device on success, NULL on failure.
585 struct block_device *bdget_disk(struct gendisk *disk, int partno)
587 struct hd_struct *part;
588 struct block_device *bdev = NULL;
590 part = disk_get_part(disk, partno);
591 if (part)
592 bdev = bdget(part_devt(part));
593 disk_put_part(part);
595 return bdev;
597 EXPORT_SYMBOL(bdget_disk);
600 * print a full list of all partitions - intended for places where the root
601 * filesystem can't be mounted and thus to give the victim some idea of what
602 * went wrong
604 void __init printk_all_partitions(void)
606 struct class_dev_iter iter;
607 struct device *dev;
609 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
610 while ((dev = class_dev_iter_next(&iter))) {
611 struct gendisk *disk = dev_to_disk(dev);
612 struct disk_part_iter piter;
613 struct hd_struct *part;
614 char name_buf[BDEVNAME_SIZE];
615 char devt_buf[BDEVT_SIZE];
618 * Don't show empty devices or things that have been
619 * surpressed
621 if (get_capacity(disk) == 0 ||
622 (disk->flags & GENHD_FL_SUPPRESS_PARTITION_INFO))
623 continue;
626 * Note, unlike /proc/partitions, I am showing the
627 * numbers in hex - the same format as the root=
628 * option takes.
630 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
631 while ((part = disk_part_iter_next(&piter))) {
632 bool is_part0 = part == &disk->part0;
634 printk("%s%s %10llu %s", is_part0 ? "" : " ",
635 bdevt_str(part_devt(part), devt_buf),
636 (unsigned long long)part->nr_sects >> 1,
637 disk_name(disk, part->partno, name_buf));
638 if (is_part0) {
639 if (disk->driverfs_dev != NULL &&
640 disk->driverfs_dev->driver != NULL)
641 printk(" driver: %s\n",
642 disk->driverfs_dev->driver->name);
643 else
644 printk(" (driver?)\n");
645 } else
646 printk("\n");
648 disk_part_iter_exit(&piter);
650 class_dev_iter_exit(&iter);
653 #ifdef CONFIG_PROC_FS
654 /* iterator */
655 static void *disk_seqf_start(struct seq_file *seqf, loff_t *pos)
657 loff_t skip = *pos;
658 struct class_dev_iter *iter;
659 struct device *dev;
661 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
662 if (!iter)
663 return ERR_PTR(-ENOMEM);
665 seqf->private = iter;
666 class_dev_iter_init(iter, &block_class, NULL, &disk_type);
667 do {
668 dev = class_dev_iter_next(iter);
669 if (!dev)
670 return NULL;
671 } while (skip--);
673 return dev_to_disk(dev);
676 static void *disk_seqf_next(struct seq_file *seqf, void *v, loff_t *pos)
678 struct device *dev;
680 (*pos)++;
681 dev = class_dev_iter_next(seqf->private);
682 if (dev)
683 return dev_to_disk(dev);
685 return NULL;
688 static void disk_seqf_stop(struct seq_file *seqf, void *v)
690 struct class_dev_iter *iter = seqf->private;
692 /* stop is called even after start failed :-( */
693 if (iter) {
694 class_dev_iter_exit(iter);
695 kfree(iter);
699 static void *show_partition_start(struct seq_file *seqf, loff_t *pos)
701 static void *p;
703 p = disk_seqf_start(seqf, pos);
704 if (!IS_ERR(p) && p && !*pos)
705 seq_puts(seqf, "major minor #blocks name\n\n");
706 return p;
709 static int show_partition(struct seq_file *seqf, void *v)
711 struct gendisk *sgp = v;
712 struct disk_part_iter piter;
713 struct hd_struct *part;
714 char buf[BDEVNAME_SIZE];
716 /* Don't show non-partitionable removeable devices or empty devices */
717 if (!get_capacity(sgp) || (!disk_partitionable(sgp) &&
718 (sgp->flags & GENHD_FL_REMOVABLE)))
719 return 0;
720 if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)
721 return 0;
723 /* show the full disk and all non-0 size partitions of it */
724 disk_part_iter_init(&piter, sgp, DISK_PITER_INCL_PART0);
725 while ((part = disk_part_iter_next(&piter)))
726 seq_printf(seqf, "%4d %7d %10llu %s\n",
727 MAJOR(part_devt(part)), MINOR(part_devt(part)),
728 (unsigned long long)part->nr_sects >> 1,
729 disk_name(sgp, part->partno, buf));
730 disk_part_iter_exit(&piter);
732 return 0;
735 static const struct seq_operations partitions_op = {
736 .start = show_partition_start,
737 .next = disk_seqf_next,
738 .stop = disk_seqf_stop,
739 .show = show_partition
742 static int partitions_open(struct inode *inode, struct file *file)
744 return seq_open(file, &partitions_op);
747 static const struct file_operations proc_partitions_operations = {
748 .open = partitions_open,
749 .read = seq_read,
750 .llseek = seq_lseek,
751 .release = seq_release,
753 #endif
756 static struct kobject *base_probe(dev_t devt, int *partno, void *data)
758 if (request_module("block-major-%d-%d", MAJOR(devt), MINOR(devt)) > 0)
759 /* Make old-style 2.4 aliases work */
760 request_module("block-major-%d", MAJOR(devt));
761 return NULL;
764 static int __init genhd_device_init(void)
766 int error;
768 block_class.dev_kobj = sysfs_dev_block_kobj;
769 error = class_register(&block_class);
770 if (unlikely(error))
771 return error;
772 bdev_map = kobj_map_init(base_probe, &block_class_lock);
773 blk_dev_init();
775 register_blkdev(BLOCK_EXT_MAJOR, "blkext");
777 #ifndef CONFIG_SYSFS_DEPRECATED
778 /* create top-level block dir */
779 block_depr = kobject_create_and_add("block", NULL);
780 #endif
781 return 0;
784 subsys_initcall(genhd_device_init);
786 static ssize_t disk_range_show(struct device *dev,
787 struct device_attribute *attr, char *buf)
789 struct gendisk *disk = dev_to_disk(dev);
791 return sprintf(buf, "%d\n", disk->minors);
794 static ssize_t disk_ext_range_show(struct device *dev,
795 struct device_attribute *attr, char *buf)
797 struct gendisk *disk = dev_to_disk(dev);
799 return sprintf(buf, "%d\n", disk_max_parts(disk));
802 static ssize_t disk_removable_show(struct device *dev,
803 struct device_attribute *attr, char *buf)
805 struct gendisk *disk = dev_to_disk(dev);
807 return sprintf(buf, "%d\n",
808 (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
811 static ssize_t disk_ro_show(struct device *dev,
812 struct device_attribute *attr, char *buf)
814 struct gendisk *disk = dev_to_disk(dev);
816 return sprintf(buf, "%d\n", get_disk_ro(disk) ? 1 : 0);
819 static ssize_t disk_capability_show(struct device *dev,
820 struct device_attribute *attr, char *buf)
822 struct gendisk *disk = dev_to_disk(dev);
824 return sprintf(buf, "%x\n", disk->flags);
827 static DEVICE_ATTR(range, S_IRUGO, disk_range_show, NULL);
828 static DEVICE_ATTR(ext_range, S_IRUGO, disk_ext_range_show, NULL);
829 static DEVICE_ATTR(removable, S_IRUGO, disk_removable_show, NULL);
830 static DEVICE_ATTR(ro, S_IRUGO, disk_ro_show, NULL);
831 static DEVICE_ATTR(size, S_IRUGO, part_size_show, NULL);
832 static DEVICE_ATTR(capability, S_IRUGO, disk_capability_show, NULL);
833 static DEVICE_ATTR(stat, S_IRUGO, part_stat_show, NULL);
834 #ifdef CONFIG_FAIL_MAKE_REQUEST
835 static struct device_attribute dev_attr_fail =
836 __ATTR(make-it-fail, S_IRUGO|S_IWUSR, part_fail_show, part_fail_store);
837 #endif
838 #ifdef CONFIG_FAIL_IO_TIMEOUT
839 static struct device_attribute dev_attr_fail_timeout =
840 __ATTR(io-timeout-fail, S_IRUGO|S_IWUSR, part_timeout_show,
841 part_timeout_store);
842 #endif
844 static struct attribute *disk_attrs[] = {
845 &dev_attr_range.attr,
846 &dev_attr_ext_range.attr,
847 &dev_attr_removable.attr,
848 &dev_attr_ro.attr,
849 &dev_attr_size.attr,
850 &dev_attr_capability.attr,
851 &dev_attr_stat.attr,
852 #ifdef CONFIG_FAIL_MAKE_REQUEST
853 &dev_attr_fail.attr,
854 #endif
855 #ifdef CONFIG_FAIL_IO_TIMEOUT
856 &dev_attr_fail_timeout.attr,
857 #endif
858 NULL
861 static struct attribute_group disk_attr_group = {
862 .attrs = disk_attrs,
865 static struct attribute_group *disk_attr_groups[] = {
866 &disk_attr_group,
867 NULL
870 static void disk_free_ptbl_rcu_cb(struct rcu_head *head)
872 struct disk_part_tbl *ptbl =
873 container_of(head, struct disk_part_tbl, rcu_head);
875 kfree(ptbl);
879 * disk_replace_part_tbl - replace disk->part_tbl in RCU-safe way
880 * @disk: disk to replace part_tbl for
881 * @new_ptbl: new part_tbl to install
883 * Replace disk->part_tbl with @new_ptbl in RCU-safe way. The
884 * original ptbl is freed using RCU callback.
886 * LOCKING:
887 * Matching bd_mutx locked.
889 static void disk_replace_part_tbl(struct gendisk *disk,
890 struct disk_part_tbl *new_ptbl)
892 struct disk_part_tbl *old_ptbl = disk->part_tbl;
894 rcu_assign_pointer(disk->part_tbl, new_ptbl);
895 if (old_ptbl)
896 call_rcu(&old_ptbl->rcu_head, disk_free_ptbl_rcu_cb);
900 * disk_expand_part_tbl - expand disk->part_tbl
901 * @disk: disk to expand part_tbl for
902 * @partno: expand such that this partno can fit in
904 * Expand disk->part_tbl such that @partno can fit in. disk->part_tbl
905 * uses RCU to allow unlocked dereferencing for stats and other stuff.
907 * LOCKING:
908 * Matching bd_mutex locked, might sleep.
910 * RETURNS:
911 * 0 on success, -errno on failure.
913 int disk_expand_part_tbl(struct gendisk *disk, int partno)
915 struct disk_part_tbl *old_ptbl = disk->part_tbl;
916 struct disk_part_tbl *new_ptbl;
917 int len = old_ptbl ? old_ptbl->len : 0;
918 int target = partno + 1;
919 size_t size;
920 int i;
922 /* disk_max_parts() is zero during initialization, ignore if so */
923 if (disk_max_parts(disk) && target > disk_max_parts(disk))
924 return -EINVAL;
926 if (target <= len)
927 return 0;
929 size = sizeof(*new_ptbl) + target * sizeof(new_ptbl->part[0]);
930 new_ptbl = kzalloc_node(size, GFP_KERNEL, disk->node_id);
931 if (!new_ptbl)
932 return -ENOMEM;
934 INIT_RCU_HEAD(&new_ptbl->rcu_head);
935 new_ptbl->len = target;
937 for (i = 0; i < len; i++)
938 rcu_assign_pointer(new_ptbl->part[i], old_ptbl->part[i]);
940 disk_replace_part_tbl(disk, new_ptbl);
941 return 0;
944 static void disk_release(struct device *dev)
946 struct gendisk *disk = dev_to_disk(dev);
948 kfree(disk->random);
949 disk_replace_part_tbl(disk, NULL);
950 free_part_stats(&disk->part0);
951 kfree(disk);
953 struct class block_class = {
954 .name = "block",
957 static struct device_type disk_type = {
958 .name = "disk",
959 .groups = disk_attr_groups,
960 .release = disk_release,
963 #ifdef CONFIG_PROC_FS
965 * aggregate disk stat collector. Uses the same stats that the sysfs
966 * entries do, above, but makes them available through one seq_file.
968 * The output looks suspiciously like /proc/partitions with a bunch of
969 * extra fields.
971 static int diskstats_show(struct seq_file *seqf, void *v)
973 struct gendisk *gp = v;
974 struct disk_part_iter piter;
975 struct hd_struct *hd;
976 char buf[BDEVNAME_SIZE];
977 int cpu;
980 if (&disk_to_dev(gp)->kobj.entry == block_class.devices.next)
981 seq_puts(seqf, "major minor name"
982 " rio rmerge rsect ruse wio wmerge "
983 "wsect wuse running use aveq"
984 "\n\n");
987 disk_part_iter_init(&piter, gp, DISK_PITER_INCL_EMPTY_PART0);
988 while ((hd = disk_part_iter_next(&piter))) {
989 cpu = part_stat_lock();
990 part_round_stats(cpu, hd);
991 part_stat_unlock();
992 seq_printf(seqf, "%4d %7d %s %lu %lu %llu "
993 "%u %lu %lu %llu %u %u %u %u\n",
994 MAJOR(part_devt(hd)), MINOR(part_devt(hd)),
995 disk_name(gp, hd->partno, buf),
996 part_stat_read(hd, ios[0]),
997 part_stat_read(hd, merges[0]),
998 (unsigned long long)part_stat_read(hd, sectors[0]),
999 jiffies_to_msecs(part_stat_read(hd, ticks[0])),
1000 part_stat_read(hd, ios[1]),
1001 part_stat_read(hd, merges[1]),
1002 (unsigned long long)part_stat_read(hd, sectors[1]),
1003 jiffies_to_msecs(part_stat_read(hd, ticks[1])),
1004 hd->in_flight,
1005 jiffies_to_msecs(part_stat_read(hd, io_ticks)),
1006 jiffies_to_msecs(part_stat_read(hd, time_in_queue))
1009 disk_part_iter_exit(&piter);
1011 return 0;
1014 static const struct seq_operations diskstats_op = {
1015 .start = disk_seqf_start,
1016 .next = disk_seqf_next,
1017 .stop = disk_seqf_stop,
1018 .show = diskstats_show
1021 static int diskstats_open(struct inode *inode, struct file *file)
1023 return seq_open(file, &diskstats_op);
1026 static const struct file_operations proc_diskstats_operations = {
1027 .open = diskstats_open,
1028 .read = seq_read,
1029 .llseek = seq_lseek,
1030 .release = seq_release,
1033 static int __init proc_genhd_init(void)
1035 proc_create("diskstats", 0, NULL, &proc_diskstats_operations);
1036 proc_create("partitions", 0, NULL, &proc_partitions_operations);
1037 return 0;
1039 module_init(proc_genhd_init);
1040 #endif /* CONFIG_PROC_FS */
1042 static void media_change_notify_thread(struct work_struct *work)
1044 struct gendisk *gd = container_of(work, struct gendisk, async_notify);
1045 char event[] = "MEDIA_CHANGE=1";
1046 char *envp[] = { event, NULL };
1049 * set enviroment vars to indicate which event this is for
1050 * so that user space will know to go check the media status.
1052 kobject_uevent_env(&disk_to_dev(gd)->kobj, KOBJ_CHANGE, envp);
1053 put_device(gd->driverfs_dev);
1056 #if 0
1057 void genhd_media_change_notify(struct gendisk *disk)
1059 get_device(disk->driverfs_dev);
1060 schedule_work(&disk->async_notify);
1062 EXPORT_SYMBOL_GPL(genhd_media_change_notify);
1063 #endif /* 0 */
1065 dev_t blk_lookup_devt(const char *name, int partno)
1067 dev_t devt = MKDEV(0, 0);
1068 struct class_dev_iter iter;
1069 struct device *dev;
1071 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
1072 while ((dev = class_dev_iter_next(&iter))) {
1073 struct gendisk *disk = dev_to_disk(dev);
1074 struct hd_struct *part;
1076 if (strcmp(dev->bus_id, name))
1077 continue;
1079 part = disk_get_part(disk, partno);
1080 if (part) {
1081 devt = part_devt(part);
1082 disk_put_part(part);
1083 break;
1085 disk_put_part(part);
1087 class_dev_iter_exit(&iter);
1088 return devt;
1090 EXPORT_SYMBOL(blk_lookup_devt);
1092 struct gendisk *alloc_disk(int minors)
1094 return alloc_disk_node(minors, -1);
1096 EXPORT_SYMBOL(alloc_disk);
1098 struct gendisk *alloc_disk_node(int minors, int node_id)
1100 struct gendisk *disk;
1102 disk = kmalloc_node(sizeof(struct gendisk),
1103 GFP_KERNEL | __GFP_ZERO, node_id);
1104 if (disk) {
1105 if (!init_part_stats(&disk->part0)) {
1106 kfree(disk);
1107 return NULL;
1109 disk->node_id = node_id;
1110 if (disk_expand_part_tbl(disk, 0)) {
1111 free_part_stats(&disk->part0);
1112 kfree(disk);
1113 return NULL;
1115 disk->part_tbl->part[0] = &disk->part0;
1117 disk->minors = minors;
1118 rand_initialize_disk(disk);
1119 disk_to_dev(disk)->class = &block_class;
1120 disk_to_dev(disk)->type = &disk_type;
1121 device_initialize(disk_to_dev(disk));
1122 INIT_WORK(&disk->async_notify,
1123 media_change_notify_thread);
1125 return disk;
1127 EXPORT_SYMBOL(alloc_disk_node);
1129 struct kobject *get_disk(struct gendisk *disk)
1131 struct module *owner;
1132 struct kobject *kobj;
1134 if (!disk->fops)
1135 return NULL;
1136 owner = disk->fops->owner;
1137 if (owner && !try_module_get(owner))
1138 return NULL;
1139 kobj = kobject_get(&disk_to_dev(disk)->kobj);
1140 if (kobj == NULL) {
1141 module_put(owner);
1142 return NULL;
1144 return kobj;
1148 EXPORT_SYMBOL(get_disk);
1150 void put_disk(struct gendisk *disk)
1152 if (disk)
1153 kobject_put(&disk_to_dev(disk)->kobj);
1156 EXPORT_SYMBOL(put_disk);
1158 void set_device_ro(struct block_device *bdev, int flag)
1160 bdev->bd_part->policy = flag;
1163 EXPORT_SYMBOL(set_device_ro);
1165 void set_disk_ro(struct gendisk *disk, int flag)
1167 struct disk_part_iter piter;
1168 struct hd_struct *part;
1170 disk_part_iter_init(&piter, disk,
1171 DISK_PITER_INCL_EMPTY | DISK_PITER_INCL_PART0);
1172 while ((part = disk_part_iter_next(&piter)))
1173 part->policy = flag;
1174 disk_part_iter_exit(&piter);
1177 EXPORT_SYMBOL(set_disk_ro);
1179 int bdev_read_only(struct block_device *bdev)
1181 if (!bdev)
1182 return 0;
1183 return bdev->bd_part->policy;
1186 EXPORT_SYMBOL(bdev_read_only);
1188 int invalidate_partition(struct gendisk *disk, int partno)
1190 int res = 0;
1191 struct block_device *bdev = bdget_disk(disk, partno);
1192 if (bdev) {
1193 fsync_bdev(bdev);
1194 res = __invalidate_device(bdev);
1195 bdput(bdev);
1197 return res;
1200 EXPORT_SYMBOL(invalidate_partition);