]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - block/genhd.c
block: add bio_kmalloc()
[mirror_ubuntu-bionic-kernel.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>
13#include <linux/seq_file.h>
14#include <linux/slab.h>
15#include <linux/kmod.h>
16#include <linux/kobj_map.h>
2ef41634 17#include <linux/buffer_head.h>
58383af6 18#include <linux/mutex.h>
bcce3de1 19#include <linux/idr.h>
1da177e4 20
ff88972c
AB
21#include "blk.h"
22
edfaa7c3
KS
23static DEFINE_MUTEX(block_class_lock);
24#ifndef CONFIG_SYSFS_DEPRECATED
25struct kobject *block_depr;
26#endif
1da177e4 27
bcce3de1
TH
28/* for extended dynamic devt allocation, currently only one major is used */
29#define MAX_EXT_DEVT (1 << MINORBITS)
30
31/* For extended devt allocation. ext_devt_mutex prevents look up
32 * results from going away underneath its user.
33 */
34static DEFINE_MUTEX(ext_devt_mutex);
35static DEFINE_IDR(ext_devt_idr);
36
1826eadf
AB
37static struct device_type disk_type;
38
e71bf0d0
TH
39/**
40 * disk_get_part - get partition
41 * @disk: disk to look partition from
42 * @partno: partition number
43 *
44 * Look for partition @partno from @disk. If found, increment
45 * reference count and return it.
46 *
47 * CONTEXT:
48 * Don't care.
49 *
50 * RETURNS:
51 * Pointer to the found partition on success, NULL if not found.
52 */
53struct hd_struct *disk_get_part(struct gendisk *disk, int partno)
54{
540eed56
TH
55 struct hd_struct *part = NULL;
56 struct disk_part_tbl *ptbl;
e71bf0d0 57
540eed56 58 if (unlikely(partno < 0))
e71bf0d0 59 return NULL;
540eed56 60
e71bf0d0 61 rcu_read_lock();
540eed56
TH
62
63 ptbl = rcu_dereference(disk->part_tbl);
64 if (likely(partno < ptbl->len)) {
65 part = rcu_dereference(ptbl->part[partno]);
66 if (part)
67 get_device(part_to_dev(part));
68 }
69
e71bf0d0
TH
70 rcu_read_unlock();
71
72 return part;
73}
74EXPORT_SYMBOL_GPL(disk_get_part);
75
76/**
77 * disk_part_iter_init - initialize partition iterator
78 * @piter: iterator to initialize
79 * @disk: disk to iterate over
80 * @flags: DISK_PITER_* flags
81 *
82 * Initialize @piter so that it iterates over partitions of @disk.
83 *
84 * CONTEXT:
85 * Don't care.
86 */
87void disk_part_iter_init(struct disk_part_iter *piter, struct gendisk *disk,
88 unsigned int flags)
89{
540eed56
TH
90 struct disk_part_tbl *ptbl;
91
92 rcu_read_lock();
93 ptbl = rcu_dereference(disk->part_tbl);
94
e71bf0d0
TH
95 piter->disk = disk;
96 piter->part = NULL;
97
98 if (flags & DISK_PITER_REVERSE)
540eed56 99 piter->idx = ptbl->len - 1;
b5d0b9df 100 else if (flags & DISK_PITER_INCL_PART0)
e71bf0d0 101 piter->idx = 0;
b5d0b9df
TH
102 else
103 piter->idx = 1;
e71bf0d0
TH
104
105 piter->flags = flags;
540eed56
TH
106
107 rcu_read_unlock();
e71bf0d0
TH
108}
109EXPORT_SYMBOL_GPL(disk_part_iter_init);
110
111/**
112 * disk_part_iter_next - proceed iterator to the next partition and return it
113 * @piter: iterator of interest
114 *
115 * Proceed @piter to the next partition and return it.
116 *
117 * CONTEXT:
118 * Don't care.
119 */
120struct hd_struct *disk_part_iter_next(struct disk_part_iter *piter)
121{
540eed56 122 struct disk_part_tbl *ptbl;
e71bf0d0
TH
123 int inc, end;
124
125 /* put the last partition */
126 disk_put_part(piter->part);
127 piter->part = NULL;
128
540eed56 129 /* get part_tbl */
e71bf0d0 130 rcu_read_lock();
540eed56 131 ptbl = rcu_dereference(piter->disk->part_tbl);
e71bf0d0
TH
132
133 /* determine iteration parameters */
134 if (piter->flags & DISK_PITER_REVERSE) {
135 inc = -1;
b5d0b9df
TH
136 if (piter->flags & DISK_PITER_INCL_PART0)
137 end = -1;
138 else
139 end = 0;
e71bf0d0
TH
140 } else {
141 inc = 1;
540eed56 142 end = ptbl->len;
e71bf0d0
TH
143 }
144
145 /* iterate to the next partition */
146 for (; piter->idx != end; piter->idx += inc) {
147 struct hd_struct *part;
148
540eed56 149 part = rcu_dereference(ptbl->part[piter->idx]);
e71bf0d0
TH
150 if (!part)
151 continue;
152 if (!(piter->flags & DISK_PITER_INCL_EMPTY) && !part->nr_sects)
153 continue;
154
ed9e1982 155 get_device(part_to_dev(part));
e71bf0d0
TH
156 piter->part = part;
157 piter->idx += inc;
158 break;
159 }
160
161 rcu_read_unlock();
162
163 return piter->part;
164}
165EXPORT_SYMBOL_GPL(disk_part_iter_next);
166
167/**
168 * disk_part_iter_exit - finish up partition iteration
169 * @piter: iter of interest
170 *
171 * Called when iteration is over. Cleans up @piter.
172 *
173 * CONTEXT:
174 * Don't care.
175 */
176void disk_part_iter_exit(struct disk_part_iter *piter)
177{
178 disk_put_part(piter->part);
179 piter->part = NULL;
180}
181EXPORT_SYMBOL_GPL(disk_part_iter_exit);
182
183/**
184 * disk_map_sector_rcu - map sector to partition
185 * @disk: gendisk of interest
186 * @sector: sector to map
187 *
188 * Find out which partition @sector maps to on @disk. This is
189 * primarily used for stats accounting.
190 *
191 * CONTEXT:
192 * RCU read locked. The returned partition pointer is valid only
193 * while preemption is disabled.
194 *
195 * RETURNS:
074a7aca 196 * Found partition on success, part0 is returned if no partition matches
e71bf0d0
TH
197 */
198struct hd_struct *disk_map_sector_rcu(struct gendisk *disk, sector_t sector)
199{
540eed56 200 struct disk_part_tbl *ptbl;
e71bf0d0
TH
201 int i;
202
540eed56
TH
203 ptbl = rcu_dereference(disk->part_tbl);
204
205 for (i = 1; i < ptbl->len; i++) {
206 struct hd_struct *part = rcu_dereference(ptbl->part[i]);
e71bf0d0
TH
207
208 if (part && part->start_sect <= sector &&
209 sector < part->start_sect + part->nr_sects)
210 return part;
211 }
074a7aca 212 return &disk->part0;
e71bf0d0
TH
213}
214EXPORT_SYMBOL_GPL(disk_map_sector_rcu);
215
1da177e4
LT
216/*
217 * Can be deleted altogether. Later.
218 *
219 */
220static struct blk_major_name {
221 struct blk_major_name *next;
222 int major;
223 char name[16];
68eef3b4 224} *major_names[BLKDEV_MAJOR_HASH_SIZE];
1da177e4
LT
225
226/* index in the above - for now: assume no multimajor ranges */
227static inline int major_to_index(int major)
228{
68eef3b4 229 return major % BLKDEV_MAJOR_HASH_SIZE;
7170be5f
NH
230}
231
68eef3b4 232#ifdef CONFIG_PROC_FS
cf771cb5 233void blkdev_show(struct seq_file *seqf, off_t offset)
7170be5f 234{
68eef3b4 235 struct blk_major_name *dp;
7170be5f 236
68eef3b4 237 if (offset < BLKDEV_MAJOR_HASH_SIZE) {
edfaa7c3 238 mutex_lock(&block_class_lock);
68eef3b4 239 for (dp = major_names[offset]; dp; dp = dp->next)
cf771cb5 240 seq_printf(seqf, "%3d %s\n", dp->major, dp->name);
edfaa7c3 241 mutex_unlock(&block_class_lock);
1da177e4 242 }
1da177e4 243}
68eef3b4 244#endif /* CONFIG_PROC_FS */
1da177e4
LT
245
246int register_blkdev(unsigned int major, const char *name)
247{
248 struct blk_major_name **n, *p;
249 int index, ret = 0;
250
edfaa7c3 251 mutex_lock(&block_class_lock);
1da177e4
LT
252
253 /* temporary */
254 if (major == 0) {
255 for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) {
256 if (major_names[index] == NULL)
257 break;
258 }
259
260 if (index == 0) {
261 printk("register_blkdev: failed to get major for %s\n",
262 name);
263 ret = -EBUSY;
264 goto out;
265 }
266 major = index;
267 ret = major;
268 }
269
270 p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL);
271 if (p == NULL) {
272 ret = -ENOMEM;
273 goto out;
274 }
275
276 p->major = major;
277 strlcpy(p->name, name, sizeof(p->name));
278 p->next = NULL;
279 index = major_to_index(major);
280
281 for (n = &major_names[index]; *n; n = &(*n)->next) {
282 if ((*n)->major == major)
283 break;
284 }
285 if (!*n)
286 *n = p;
287 else
288 ret = -EBUSY;
289
290 if (ret < 0) {
291 printk("register_blkdev: cannot get major %d for %s\n",
292 major, name);
293 kfree(p);
294 }
295out:
edfaa7c3 296 mutex_unlock(&block_class_lock);
1da177e4
LT
297 return ret;
298}
299
300EXPORT_SYMBOL(register_blkdev);
301
f4480240 302void unregister_blkdev(unsigned int major, const char *name)
1da177e4
LT
303{
304 struct blk_major_name **n;
305 struct blk_major_name *p = NULL;
306 int index = major_to_index(major);
1da177e4 307
edfaa7c3 308 mutex_lock(&block_class_lock);
1da177e4
LT
309 for (n = &major_names[index]; *n; n = &(*n)->next)
310 if ((*n)->major == major)
311 break;
294462a5
AM
312 if (!*n || strcmp((*n)->name, name)) {
313 WARN_ON(1);
294462a5 314 } else {
1da177e4
LT
315 p = *n;
316 *n = p->next;
317 }
edfaa7c3 318 mutex_unlock(&block_class_lock);
1da177e4 319 kfree(p);
1da177e4
LT
320}
321
322EXPORT_SYMBOL(unregister_blkdev);
323
324static struct kobj_map *bdev_map;
325
870d6656
TH
326/**
327 * blk_mangle_minor - scatter minor numbers apart
328 * @minor: minor number to mangle
329 *
330 * Scatter consecutively allocated @minor number apart if MANGLE_DEVT
331 * is enabled. Mangling twice gives the original value.
332 *
333 * RETURNS:
334 * Mangled value.
335 *
336 * CONTEXT:
337 * Don't care.
338 */
339static int blk_mangle_minor(int minor)
340{
341#ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT
342 int i;
343
344 for (i = 0; i < MINORBITS / 2; i++) {
345 int low = minor & (1 << i);
346 int high = minor & (1 << (MINORBITS - 1 - i));
347 int distance = MINORBITS - 1 - 2 * i;
348
349 minor ^= low | high; /* clear both bits */
350 low <<= distance; /* swap the positions */
351 high >>= distance;
352 minor |= low | high; /* and set */
353 }
354#endif
355 return minor;
356}
357
bcce3de1
TH
358/**
359 * blk_alloc_devt - allocate a dev_t for a partition
360 * @part: partition to allocate dev_t for
361 * @gfp_mask: memory allocation flag
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
RD
537 * @devt: device to get partitioning information for
538 * @part: 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
12f32bb3 731const 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
LT
736};
737#endif
738
739
cf771cb5 740static struct kobject *base_probe(dev_t devt, int *partno, void *data)
1da177e4 741{
edfaa7c3 742 if (request_module("block-major-%d-%d", MAJOR(devt), MINOR(devt)) > 0)
1da177e4 743 /* Make old-style 2.4 aliases work */
edfaa7c3 744 request_module("block-major-%d", MAJOR(devt));
1da177e4
LT
745 return NULL;
746}
747
748static int __init genhd_device_init(void)
749{
e105b8bf
DW
750 int error;
751
752 block_class.dev_kobj = sysfs_dev_block_kobj;
753 error = class_register(&block_class);
ee27a558
RM
754 if (unlikely(error))
755 return error;
edfaa7c3 756 bdev_map = kobj_map_init(base_probe, &block_class_lock);
1da177e4 757 blk_dev_init();
edfaa7c3
KS
758
759#ifndef CONFIG_SYSFS_DEPRECATED
760 /* create top-level block dir */
761 block_depr = kobject_create_and_add("block", NULL);
762#endif
830d3cfb 763 return 0;
1da177e4
LT
764}
765
766subsys_initcall(genhd_device_init);
767
edfaa7c3
KS
768static ssize_t disk_range_show(struct device *dev,
769 struct device_attribute *attr, char *buf)
1da177e4 770{
edfaa7c3 771 struct gendisk *disk = dev_to_disk(dev);
1da177e4 772
edfaa7c3 773 return sprintf(buf, "%d\n", disk->minors);
1da177e4
LT
774}
775
1f014290
TH
776static ssize_t disk_ext_range_show(struct device *dev,
777 struct device_attribute *attr, char *buf)
778{
779 struct gendisk *disk = dev_to_disk(dev);
780
b5d0b9df 781 return sprintf(buf, "%d\n", disk_max_parts(disk));
1f014290
TH
782}
783
edfaa7c3
KS
784static ssize_t disk_removable_show(struct device *dev,
785 struct device_attribute *attr, char *buf)
a7fd6706 786{
edfaa7c3 787 struct gendisk *disk = dev_to_disk(dev);
a7fd6706 788
edfaa7c3
KS
789 return sprintf(buf, "%d\n",
790 (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
a7fd6706
KS
791}
792
1c9ce527
KS
793static ssize_t disk_ro_show(struct device *dev,
794 struct device_attribute *attr, char *buf)
795{
796 struct gendisk *disk = dev_to_disk(dev);
797
b7db9956 798 return sprintf(buf, "%d\n", get_disk_ro(disk) ? 1 : 0);
1c9ce527
KS
799}
800
edfaa7c3
KS
801static ssize_t disk_capability_show(struct device *dev,
802 struct device_attribute *attr, char *buf)
86ce18d7 803{
edfaa7c3
KS
804 struct gendisk *disk = dev_to_disk(dev);
805
806 return sprintf(buf, "%x\n", disk->flags);
86ce18d7 807}
edfaa7c3 808
edfaa7c3 809static DEVICE_ATTR(range, S_IRUGO, disk_range_show, NULL);
1f014290 810static DEVICE_ATTR(ext_range, S_IRUGO, disk_ext_range_show, NULL);
edfaa7c3 811static DEVICE_ATTR(removable, S_IRUGO, disk_removable_show, NULL);
1c9ce527 812static DEVICE_ATTR(ro, S_IRUGO, disk_ro_show, NULL);
e5610521 813static DEVICE_ATTR(size, S_IRUGO, part_size_show, NULL);
edfaa7c3 814static DEVICE_ATTR(capability, S_IRUGO, disk_capability_show, NULL);
074a7aca 815static DEVICE_ATTR(stat, S_IRUGO, part_stat_show, NULL);
c17bb495 816#ifdef CONFIG_FAIL_MAKE_REQUEST
edfaa7c3 817static struct device_attribute dev_attr_fail =
eddb2e26 818 __ATTR(make-it-fail, S_IRUGO|S_IWUSR, part_fail_show, part_fail_store);
c17bb495 819#endif
edfaa7c3
KS
820
821static struct attribute *disk_attrs[] = {
822 &dev_attr_range.attr,
1f014290 823 &dev_attr_ext_range.attr,
edfaa7c3 824 &dev_attr_removable.attr,
1c9ce527 825 &dev_attr_ro.attr,
edfaa7c3
KS
826 &dev_attr_size.attr,
827 &dev_attr_capability.attr,
828 &dev_attr_stat.attr,
829#ifdef CONFIG_FAIL_MAKE_REQUEST
830 &dev_attr_fail.attr,
831#endif
832 NULL
833};
834
835static struct attribute_group disk_attr_group = {
836 .attrs = disk_attrs,
837};
838
839static struct attribute_group *disk_attr_groups[] = {
840 &disk_attr_group,
841 NULL
1da177e4
LT
842};
843
540eed56
TH
844static void disk_free_ptbl_rcu_cb(struct rcu_head *head)
845{
846 struct disk_part_tbl *ptbl =
847 container_of(head, struct disk_part_tbl, rcu_head);
848
849 kfree(ptbl);
850}
851
852/**
853 * disk_replace_part_tbl - replace disk->part_tbl in RCU-safe way
854 * @disk: disk to replace part_tbl for
855 * @new_ptbl: new part_tbl to install
856 *
857 * Replace disk->part_tbl with @new_ptbl in RCU-safe way. The
858 * original ptbl is freed using RCU callback.
859 *
860 * LOCKING:
861 * Matching bd_mutx locked.
862 */
863static void disk_replace_part_tbl(struct gendisk *disk,
864 struct disk_part_tbl *new_ptbl)
865{
866 struct disk_part_tbl *old_ptbl = disk->part_tbl;
867
868 rcu_assign_pointer(disk->part_tbl, new_ptbl);
869 if (old_ptbl)
870 call_rcu(&old_ptbl->rcu_head, disk_free_ptbl_rcu_cb);
871}
872
873/**
874 * disk_expand_part_tbl - expand disk->part_tbl
875 * @disk: disk to expand part_tbl for
876 * @partno: expand such that this partno can fit in
877 *
878 * Expand disk->part_tbl such that @partno can fit in. disk->part_tbl
879 * uses RCU to allow unlocked dereferencing for stats and other stuff.
880 *
881 * LOCKING:
882 * Matching bd_mutex locked, might sleep.
883 *
884 * RETURNS:
885 * 0 on success, -errno on failure.
886 */
887int disk_expand_part_tbl(struct gendisk *disk, int partno)
888{
889 struct disk_part_tbl *old_ptbl = disk->part_tbl;
890 struct disk_part_tbl *new_ptbl;
891 int len = old_ptbl ? old_ptbl->len : 0;
892 int target = partno + 1;
893 size_t size;
894 int i;
895
896 /* disk_max_parts() is zero during initialization, ignore if so */
897 if (disk_max_parts(disk) && target > disk_max_parts(disk))
898 return -EINVAL;
899
900 if (target <= len)
901 return 0;
902
903 size = sizeof(*new_ptbl) + target * sizeof(new_ptbl->part[0]);
904 new_ptbl = kzalloc_node(size, GFP_KERNEL, disk->node_id);
905 if (!new_ptbl)
906 return -ENOMEM;
907
908 INIT_RCU_HEAD(&new_ptbl->rcu_head);
909 new_ptbl->len = target;
910
911 for (i = 0; i < len; i++)
912 rcu_assign_pointer(new_ptbl->part[i], old_ptbl->part[i]);
913
914 disk_replace_part_tbl(disk, new_ptbl);
915 return 0;
916}
917
edfaa7c3 918static void disk_release(struct device *dev)
1da177e4 919{
edfaa7c3
KS
920 struct gendisk *disk = dev_to_disk(dev);
921
1da177e4 922 kfree(disk->random);
540eed56 923 disk_replace_part_tbl(disk, NULL);
074a7aca 924 free_part_stats(&disk->part0);
1da177e4
LT
925 kfree(disk);
926}
edfaa7c3
KS
927struct class block_class = {
928 .name = "block",
1da177e4
LT
929};
930
1826eadf 931static struct device_type disk_type = {
edfaa7c3
KS
932 .name = "disk",
933 .groups = disk_attr_groups,
934 .release = disk_release,
1da177e4
LT
935};
936
a6e2ba88 937#ifdef CONFIG_PROC_FS
cf771cb5
TH
938/*
939 * aggregate disk stat collector. Uses the same stats that the sysfs
940 * entries do, above, but makes them available through one seq_file.
941 *
942 * The output looks suspiciously like /proc/partitions with a bunch of
943 * extra fields.
944 */
945static int diskstats_show(struct seq_file *seqf, void *v)
1da177e4
LT
946{
947 struct gendisk *gp = v;
e71bf0d0
TH
948 struct disk_part_iter piter;
949 struct hd_struct *hd;
1da177e4 950 char buf[BDEVNAME_SIZE];
c9959059 951 int cpu;
1da177e4
LT
952
953 /*
ed9e1982 954 if (&disk_to_dev(gp)->kobj.entry == block_class.devices.next)
cf771cb5 955 seq_puts(seqf, "major minor name"
1da177e4
LT
956 " rio rmerge rsect ruse wio wmerge "
957 "wsect wuse running use aveq"
958 "\n\n");
959 */
960
074a7aca 961 disk_part_iter_init(&piter, gp, DISK_PITER_INCL_PART0);
e71bf0d0 962 while ((hd = disk_part_iter_next(&piter))) {
074a7aca 963 cpu = part_stat_lock();
c9959059 964 part_round_stats(cpu, hd);
074a7aca 965 part_stat_unlock();
1f014290 966 seq_printf(seqf, "%4d %7d %s %lu %lu %llu "
28f39d55 967 "%u %lu %lu %llu %u %u %u %u\n",
f331c029
TH
968 MAJOR(part_devt(hd)), MINOR(part_devt(hd)),
969 disk_name(gp, hd->partno, buf),
28f39d55
JM
970 part_stat_read(hd, ios[0]),
971 part_stat_read(hd, merges[0]),
972 (unsigned long long)part_stat_read(hd, sectors[0]),
973 jiffies_to_msecs(part_stat_read(hd, ticks[0])),
974 part_stat_read(hd, ios[1]),
975 part_stat_read(hd, merges[1]),
976 (unsigned long long)part_stat_read(hd, sectors[1]),
977 jiffies_to_msecs(part_stat_read(hd, ticks[1])),
978 hd->in_flight,
979 jiffies_to_msecs(part_stat_read(hd, io_ticks)),
980 jiffies_to_msecs(part_stat_read(hd, time_in_queue))
981 );
1da177e4 982 }
e71bf0d0 983 disk_part_iter_exit(&piter);
1da177e4
LT
984
985 return 0;
986}
987
12f32bb3 988const struct seq_operations diskstats_op = {
def4e38d
TH
989 .start = disk_seqf_start,
990 .next = disk_seqf_next,
991 .stop = disk_seqf_stop,
1da177e4
LT
992 .show = diskstats_show
993};
a6e2ba88 994#endif /* CONFIG_PROC_FS */
1da177e4 995
8ce7ad7b
KCA
996static void media_change_notify_thread(struct work_struct *work)
997{
998 struct gendisk *gd = container_of(work, struct gendisk, async_notify);
999 char event[] = "MEDIA_CHANGE=1";
1000 char *envp[] = { event, NULL };
1001
1002 /*
1003 * set enviroment vars to indicate which event this is for
1004 * so that user space will know to go check the media status.
1005 */
ed9e1982 1006 kobject_uevent_env(&disk_to_dev(gd)->kobj, KOBJ_CHANGE, envp);
8ce7ad7b
KCA
1007 put_device(gd->driverfs_dev);
1008}
1009
1826eadf 1010#if 0
8ce7ad7b
KCA
1011void genhd_media_change_notify(struct gendisk *disk)
1012{
1013 get_device(disk->driverfs_dev);
1014 schedule_work(&disk->async_notify);
1015}
1016EXPORT_SYMBOL_GPL(genhd_media_change_notify);
1826eadf 1017#endif /* 0 */
8ce7ad7b 1018
cf771cb5 1019dev_t blk_lookup_devt(const char *name, int partno)
a142be85 1020{
def4e38d
TH
1021 dev_t devt = MKDEV(0, 0);
1022 struct class_dev_iter iter;
1023 struct device *dev;
a142be85 1024
def4e38d
TH
1025 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
1026 while ((dev = class_dev_iter_next(&iter))) {
a142be85 1027 struct gendisk *disk = dev_to_disk(dev);
548b10eb 1028 struct hd_struct *part;
a142be85 1029
f331c029
TH
1030 if (strcmp(dev->bus_id, name))
1031 continue;
f331c029 1032
548b10eb 1033 part = disk_get_part(disk, partno);
2bbedcb4 1034 if (part) {
f331c029 1035 devt = part_devt(part);
e71bf0d0 1036 disk_put_part(part);
548b10eb 1037 break;
def4e38d 1038 }
548b10eb 1039 disk_put_part(part);
5c0ef6d0 1040 }
def4e38d 1041 class_dev_iter_exit(&iter);
edfaa7c3
KS
1042 return devt;
1043}
edfaa7c3
KS
1044EXPORT_SYMBOL(blk_lookup_devt);
1045
1da177e4
LT
1046struct gendisk *alloc_disk(int minors)
1047{
1946089a
CL
1048 return alloc_disk_node(minors, -1);
1049}
689d6fac 1050EXPORT_SYMBOL(alloc_disk);
1946089a
CL
1051
1052struct gendisk *alloc_disk_node(int minors, int node_id)
1053{
1054 struct gendisk *disk;
1055
94f6030c
CL
1056 disk = kmalloc_node(sizeof(struct gendisk),
1057 GFP_KERNEL | __GFP_ZERO, node_id);
1da177e4 1058 if (disk) {
074a7aca 1059 if (!init_part_stats(&disk->part0)) {
1da177e4
LT
1060 kfree(disk);
1061 return NULL;
1062 }
540eed56
TH
1063 if (disk_expand_part_tbl(disk, 0)) {
1064 free_part_stats(&disk->part0);
b5d0b9df
TH
1065 kfree(disk);
1066 return NULL;
1da177e4 1067 }
540eed56 1068 disk->part_tbl->part[0] = &disk->part0;
b5d0b9df 1069
1da177e4 1070 disk->minors = minors;
1da177e4 1071 rand_initialize_disk(disk);
ed9e1982
TH
1072 disk_to_dev(disk)->class = &block_class;
1073 disk_to_dev(disk)->type = &disk_type;
1074 device_initialize(disk_to_dev(disk));
8ce7ad7b
KCA
1075 INIT_WORK(&disk->async_notify,
1076 media_change_notify_thread);
540eed56 1077 disk->node_id = node_id;
1da177e4
LT
1078 }
1079 return disk;
1080}
1946089a 1081EXPORT_SYMBOL(alloc_disk_node);
1da177e4
LT
1082
1083struct kobject *get_disk(struct gendisk *disk)
1084{
1085 struct module *owner;
1086 struct kobject *kobj;
1087
1088 if (!disk->fops)
1089 return NULL;
1090 owner = disk->fops->owner;
1091 if (owner && !try_module_get(owner))
1092 return NULL;
ed9e1982 1093 kobj = kobject_get(&disk_to_dev(disk)->kobj);
1da177e4
LT
1094 if (kobj == NULL) {
1095 module_put(owner);
1096 return NULL;
1097 }
1098 return kobj;
1099
1100}
1101
1102EXPORT_SYMBOL(get_disk);
1103
1104void put_disk(struct gendisk *disk)
1105{
1106 if (disk)
ed9e1982 1107 kobject_put(&disk_to_dev(disk)->kobj);
1da177e4
LT
1108}
1109
1110EXPORT_SYMBOL(put_disk);
1111
1112void set_device_ro(struct block_device *bdev, int flag)
1113{
b7db9956 1114 bdev->bd_part->policy = flag;
1da177e4
LT
1115}
1116
1117EXPORT_SYMBOL(set_device_ro);
1118
1119void set_disk_ro(struct gendisk *disk, int flag)
1120{
e71bf0d0
TH
1121 struct disk_part_iter piter;
1122 struct hd_struct *part;
1123
b7db9956
TH
1124 disk_part_iter_init(&piter, disk,
1125 DISK_PITER_INCL_EMPTY | DISK_PITER_INCL_PART0);
e71bf0d0
TH
1126 while ((part = disk_part_iter_next(&piter)))
1127 part->policy = flag;
1128 disk_part_iter_exit(&piter);
1da177e4
LT
1129}
1130
1131EXPORT_SYMBOL(set_disk_ro);
1132
1133int bdev_read_only(struct block_device *bdev)
1134{
1135 if (!bdev)
1136 return 0;
b7db9956 1137 return bdev->bd_part->policy;
1da177e4
LT
1138}
1139
1140EXPORT_SYMBOL(bdev_read_only);
1141
cf771cb5 1142int invalidate_partition(struct gendisk *disk, int partno)
1da177e4
LT
1143{
1144 int res = 0;
cf771cb5 1145 struct block_device *bdev = bdget_disk(disk, partno);
1da177e4 1146 if (bdev) {
2ef41634
CH
1147 fsync_bdev(bdev);
1148 res = __invalidate_device(bdev);
1da177e4
LT
1149 bdput(bdev);
1150 }
1151 return res;
1152}
1153
1154EXPORT_SYMBOL(invalidate_partition);