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