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