xref: /linux/block/genhd.c (revision a5766f11cfd3a0c03450d99c8fe548c2940be884)
1 /*
2  *  gendisk handling
3  */
4 
5 #include <linux/module.h>
6 #include <linux/fs.h>
7 #include <linux/genhd.h>
8 #include <linux/kdev_t.h>
9 #include <linux/kernel.h>
10 #include <linux/blkdev.h>
11 #include <linux/init.h>
12 #include <linux/spinlock.h>
13 #include <linux/seq_file.h>
14 #include <linux/slab.h>
15 #include <linux/kmod.h>
16 #include <linux/kobj_map.h>
17 #include <linux/buffer_head.h>
18 #include <linux/mutex.h>
19 #include <linux/idr.h>
20 
21 #include "blk.h"
22 
23 static DEFINE_MUTEX(block_class_lock);
24 #ifndef CONFIG_SYSFS_DEPRECATED
25 struct kobject *block_depr;
26 #endif
27 
28 /* for extended dynamic devt allocation, currently only one major is used */
29 #define MAX_EXT_DEVT		(1 << MINORBITS)
30 
31 /* For extended devt allocation.  ext_devt_mutex prevents look up
32  * results from going away underneath its user.
33  */
34 static DEFINE_MUTEX(ext_devt_mutex);
35 static DEFINE_IDR(ext_devt_idr);
36 
37 static struct device_type disk_type;
38 
39 /**
40  * disk_get_part - get partition
41  * @disk: disk to look partition from
42  * @partno: partition number
43  *
44  * Look for partition @partno from @disk.  If found, increment
45  * reference count and return it.
46  *
47  * CONTEXT:
48  * Don't care.
49  *
50  * RETURNS:
51  * Pointer to the found partition on success, NULL if not found.
52  */
53 struct hd_struct *disk_get_part(struct gendisk *disk, int partno)
54 {
55 	struct hd_struct *part = NULL;
56 	struct disk_part_tbl *ptbl;
57 
58 	if (unlikely(partno < 0))
59 		return NULL;
60 
61 	rcu_read_lock();
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 
70 	rcu_read_unlock();
71 
72 	return part;
73 }
74 EXPORT_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  */
87 void disk_part_iter_init(struct disk_part_iter *piter, struct gendisk *disk,
88 			  unsigned int flags)
89 {
90 	struct disk_part_tbl *ptbl;
91 
92 	rcu_read_lock();
93 	ptbl = rcu_dereference(disk->part_tbl);
94 
95 	piter->disk = disk;
96 	piter->part = NULL;
97 
98 	if (flags & DISK_PITER_REVERSE)
99 		piter->idx = ptbl->len - 1;
100 	else if (flags & DISK_PITER_INCL_PART0)
101 		piter->idx = 0;
102 	else
103 		piter->idx = 1;
104 
105 	piter->flags = flags;
106 
107 	rcu_read_unlock();
108 }
109 EXPORT_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  */
120 struct hd_struct *disk_part_iter_next(struct disk_part_iter *piter)
121 {
122 	struct disk_part_tbl *ptbl;
123 	int inc, end;
124 
125 	/* put the last partition */
126 	disk_put_part(piter->part);
127 	piter->part = NULL;
128 
129 	/* get part_tbl */
130 	rcu_read_lock();
131 	ptbl = rcu_dereference(piter->disk->part_tbl);
132 
133 	/* determine iteration parameters */
134 	if (piter->flags & DISK_PITER_REVERSE) {
135 		inc = -1;
136 		if (piter->flags & DISK_PITER_INCL_PART0)
137 			end = -1;
138 		else
139 			end = 0;
140 	} else {
141 		inc = 1;
142 		end = ptbl->len;
143 	}
144 
145 	/* iterate to the next partition */
146 	for (; piter->idx != end; piter->idx += inc) {
147 		struct hd_struct *part;
148 
149 		part = rcu_dereference(ptbl->part[piter->idx]);
150 		if (!part)
151 			continue;
152 		if (!(piter->flags & DISK_PITER_INCL_EMPTY) && !part->nr_sects)
153 			continue;
154 
155 		get_device(part_to_dev(part));
156 		piter->part = part;
157 		piter->idx += inc;
158 		break;
159 	}
160 
161 	rcu_read_unlock();
162 
163 	return piter->part;
164 }
165 EXPORT_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  */
176 void disk_part_iter_exit(struct disk_part_iter *piter)
177 {
178 	disk_put_part(piter->part);
179 	piter->part = NULL;
180 }
181 EXPORT_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:
196  * Found partition on success, part0 is returned if no partition matches
197  */
198 struct hd_struct *disk_map_sector_rcu(struct gendisk *disk, sector_t sector)
199 {
200 	struct disk_part_tbl *ptbl;
201 	int i;
202 
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]);
207 
208 		if (part && part->start_sect <= sector &&
209 		    sector < part->start_sect + part->nr_sects)
210 			return part;
211 	}
212 	return &disk->part0;
213 }
214 EXPORT_SYMBOL_GPL(disk_map_sector_rcu);
215 
216 /*
217  * Can be deleted altogether. Later.
218  *
219  */
220 static struct blk_major_name {
221 	struct blk_major_name *next;
222 	int major;
223 	char name[16];
224 } *major_names[BLKDEV_MAJOR_HASH_SIZE];
225 
226 /* index in the above - for now: assume no multimajor ranges */
227 static inline int major_to_index(int major)
228 {
229 	return major % BLKDEV_MAJOR_HASH_SIZE;
230 }
231 
232 #ifdef CONFIG_PROC_FS
233 void blkdev_show(struct seq_file *seqf, off_t offset)
234 {
235 	struct blk_major_name *dp;
236 
237 	if (offset < BLKDEV_MAJOR_HASH_SIZE) {
238 		mutex_lock(&block_class_lock);
239 		for (dp = major_names[offset]; dp; dp = dp->next)
240 			seq_printf(seqf, "%3d %s\n", dp->major, dp->name);
241 		mutex_unlock(&block_class_lock);
242 	}
243 }
244 #endif /* CONFIG_PROC_FS */
245 
246 int register_blkdev(unsigned int major, const char *name)
247 {
248 	struct blk_major_name **n, *p;
249 	int index, ret = 0;
250 
251 	mutex_lock(&block_class_lock);
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 	}
295 out:
296 	mutex_unlock(&block_class_lock);
297 	return ret;
298 }
299 
300 EXPORT_SYMBOL(register_blkdev);
301 
302 void unregister_blkdev(unsigned int major, const char *name)
303 {
304 	struct blk_major_name **n;
305 	struct blk_major_name *p = NULL;
306 	int index = major_to_index(major);
307 
308 	mutex_lock(&block_class_lock);
309 	for (n = &major_names[index]; *n; n = &(*n)->next)
310 		if ((*n)->major == major)
311 			break;
312 	if (!*n || strcmp((*n)->name, name)) {
313 		WARN_ON(1);
314 	} else {
315 		p = *n;
316 		*n = p->next;
317 	}
318 	mutex_unlock(&block_class_lock);
319 	kfree(p);
320 }
321 
322 EXPORT_SYMBOL(unregister_blkdev);
323 
324 static struct kobj_map *bdev_map;
325 
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  */
339 static 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 
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  */
373 int 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 
399 	*devt = MKDEV(BLOCK_EXT_MAJOR, blk_mangle_minor(idx));
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  */
412 void 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);
421 		idr_remove(&ext_devt_idr, blk_mangle_minor(MINOR(devt)));
422 		mutex_unlock(&ext_devt_mutex);
423 	}
424 }
425 
426 static 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 
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  */
443 void blk_register_region(dev_t devt, unsigned long range, struct module *module,
444 			 struct kobject *(*probe)(dev_t, int *, void *),
445 			 int (*lock)(dev_t, void *), void *data)
446 {
447 	kobj_map(bdev_map, devt, range, module, probe, lock, data);
448 }
449 
450 EXPORT_SYMBOL(blk_register_region);
451 
452 void blk_unregister_region(dev_t devt, unsigned long range)
453 {
454 	kobj_unmap(bdev_map, devt, range);
455 }
456 
457 EXPORT_SYMBOL(blk_unregister_region);
458 
459 static struct kobject *exact_match(dev_t devt, int *partno, void *data)
460 {
461 	struct gendisk *p = data;
462 
463 	return &disk_to_dev(p)->kobj;
464 }
465 
466 static int exact_lock(dev_t devt, void *data)
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.
481  *
482  * FIXME: error handling
483  */
484 void add_disk(struct gendisk *disk)
485 {
486 	struct backing_dev_info *bdi;
487 	dev_t devt;
488 	int retval;
489 
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 
497 	disk->flags |= GENHD_FL_UP;
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 
512 	blk_register_region(disk_devt(disk), disk->minors, NULL,
513 			    exact_match, exact_lock, disk);
514 	register_disk(disk);
515 	blk_register_queue(disk);
516 
517 	bdi = &disk->queue->backing_dev_info;
518 	bdi_register_dev(bdi, disk_devt(disk));
519 	retval = sysfs_create_link(&disk_to_dev(disk)->kobj, &bdi->dev->kobj,
520 				   "bdi");
521 	WARN_ON(retval);
522 }
523 
524 EXPORT_SYMBOL(add_disk);
525 EXPORT_SYMBOL(del_gendisk);	/* in partitions/check.c */
526 
527 void unlink_gendisk(struct gendisk *disk)
528 {
529 	sysfs_remove_link(&disk_to_dev(disk)->kobj, "bdi");
530 	bdi_unregister(&disk->queue->backing_dev_info);
531 	blk_unregister_queue(disk);
532 	blk_unregister_region(disk_devt(disk), disk->minors);
533 }
534 
535 /**
536  * get_gendisk - get partitioning information for a given device
537  * @devt: device to get partitioning information for
538  * @part: returned partition index
539  *
540  * This function gets the structure containing partitioning
541  * information for the given device @devt.
542  */
543 struct gendisk *get_gendisk(dev_t devt, int *partno)
544 {
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);
557 		part = idr_find(&ext_devt_idr, blk_mangle_minor(MINOR(devt)));
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 	}
564 
565 	return disk;
566 }
567 
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  */
581 struct block_device *bdget_disk(struct gendisk *disk, int partno)
582 {
583 	struct hd_struct *part;
584 	struct block_device *bdev = NULL;
585 
586 	part = disk_get_part(disk, partno);
587 	if (part)
588 		bdev = bdget(part_devt(part));
589 	disk_put_part(part);
590 
591 	return bdev;
592 }
593 EXPORT_SYMBOL(bdget_disk);
594 
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  */
600 void __init printk_all_partitions(void)
601 {
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);
608 		struct disk_part_iter piter;
609 		struct hd_struct *part;
610 		char name_buf[BDEVNAME_SIZE];
611 		char devt_buf[BDEVT_SIZE];
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 		 */
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;
629 
630 			printk("%s%s %10llu %s", is_part0 ? "" : "  ",
631 			       bdevt_str(part_devt(part), devt_buf),
632 			       (unsigned long long)part->nr_sects >> 1,
633 			       disk_name(disk, part->partno, name_buf));
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 		}
644 		disk_part_iter_exit(&piter);
645 	}
646 	class_dev_iter_exit(&iter);
647 }
648 
649 #ifdef CONFIG_PROC_FS
650 /* iterator */
651 static void *disk_seqf_start(struct seq_file *seqf, loff_t *pos)
652 {
653 	loff_t skip = *pos;
654 	struct class_dev_iter *iter;
655 	struct device *dev;
656 
657 	iter = kmalloc(sizeof(*iter), GFP_KERNEL);
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);
670 }
671 
672 static void *disk_seqf_next(struct seq_file *seqf, void *v, loff_t *pos)
673 {
674 	struct device *dev;
675 
676 	(*pos)++;
677 	dev = class_dev_iter_next(seqf->private);
678 	if (dev)
679 		return dev_to_disk(dev);
680 
681 	return NULL;
682 }
683 
684 static void disk_seqf_stop(struct seq_file *seqf, void *v)
685 {
686 	struct class_dev_iter *iter = seqf->private;
687 
688 	/* stop is called even after start failed :-( */
689 	if (iter) {
690 		class_dev_iter_exit(iter);
691 		kfree(iter);
692 	}
693 }
694 
695 static void *show_partition_start(struct seq_file *seqf, loff_t *pos)
696 {
697 	static void *p;
698 
699 	p = disk_seqf_start(seqf, pos);
700 	if (!IS_ERR(p) && p && !*pos)
701 		seq_puts(seqf, "major minor  #blocks  name\n\n");
702 	return p;
703 }
704 
705 static int show_partition(struct seq_file *seqf, void *v)
706 {
707 	struct gendisk *sgp = v;
708 	struct disk_part_iter piter;
709 	struct hd_struct *part;
710 	char buf[BDEVNAME_SIZE];
711 
712 	/* Don't show non-partitionable removeable devices or empty devices */
713 	if (!get_capacity(sgp) || (!disk_partitionable(sgp) &&
714 				   (sgp->flags & GENHD_FL_REMOVABLE)))
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 */
720 	disk_part_iter_init(&piter, sgp, DISK_PITER_INCL_PART0);
721 	while ((part = disk_part_iter_next(&piter)))
722 		seq_printf(seqf, "%4d  %7d %10llu %s\n",
723 			   MAJOR(part_devt(part)), MINOR(part_devt(part)),
724 			   (unsigned long long)part->nr_sects >> 1,
725 			   disk_name(sgp, part->partno, buf));
726 	disk_part_iter_exit(&piter);
727 
728 	return 0;
729 }
730 
731 const struct seq_operations partitions_op = {
732 	.start	= show_partition_start,
733 	.next	= disk_seqf_next,
734 	.stop	= disk_seqf_stop,
735 	.show	= show_partition
736 };
737 #endif
738 
739 
740 static struct kobject *base_probe(dev_t devt, int *partno, void *data)
741 {
742 	if (request_module("block-major-%d-%d", MAJOR(devt), MINOR(devt)) > 0)
743 		/* Make old-style 2.4 aliases work */
744 		request_module("block-major-%d", MAJOR(devt));
745 	return NULL;
746 }
747 
748 static int __init genhd_device_init(void)
749 {
750 	int error;
751 
752 	block_class.dev_kobj = sysfs_dev_block_kobj;
753 	error = class_register(&block_class);
754 	if (unlikely(error))
755 		return error;
756 	bdev_map = kobj_map_init(base_probe, &block_class_lock);
757 	blk_dev_init();
758 
759 #ifndef CONFIG_SYSFS_DEPRECATED
760 	/* create top-level block dir */
761 	block_depr = kobject_create_and_add("block", NULL);
762 #endif
763 	return 0;
764 }
765 
766 subsys_initcall(genhd_device_init);
767 
768 static ssize_t disk_range_show(struct device *dev,
769 			       struct device_attribute *attr, char *buf)
770 {
771 	struct gendisk *disk = dev_to_disk(dev);
772 
773 	return sprintf(buf, "%d\n", disk->minors);
774 }
775 
776 static 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 
781 	return sprintf(buf, "%d\n", disk_max_parts(disk));
782 }
783 
784 static ssize_t disk_removable_show(struct device *dev,
785 				   struct device_attribute *attr, char *buf)
786 {
787 	struct gendisk *disk = dev_to_disk(dev);
788 
789 	return sprintf(buf, "%d\n",
790 		       (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
791 }
792 
793 static 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 
798 	return sprintf(buf, "%d\n", get_disk_ro(disk) ? 1 : 0);
799 }
800 
801 static ssize_t disk_capability_show(struct device *dev,
802 				    struct device_attribute *attr, char *buf)
803 {
804 	struct gendisk *disk = dev_to_disk(dev);
805 
806 	return sprintf(buf, "%x\n", disk->flags);
807 }
808 
809 static DEVICE_ATTR(range, S_IRUGO, disk_range_show, NULL);
810 static DEVICE_ATTR(ext_range, S_IRUGO, disk_ext_range_show, NULL);
811 static DEVICE_ATTR(removable, S_IRUGO, disk_removable_show, NULL);
812 static DEVICE_ATTR(ro, S_IRUGO, disk_ro_show, NULL);
813 static DEVICE_ATTR(size, S_IRUGO, part_size_show, NULL);
814 static DEVICE_ATTR(capability, S_IRUGO, disk_capability_show, NULL);
815 static DEVICE_ATTR(stat, S_IRUGO, part_stat_show, NULL);
816 #ifdef CONFIG_FAIL_MAKE_REQUEST
817 static struct device_attribute dev_attr_fail =
818 	__ATTR(make-it-fail, S_IRUGO|S_IWUSR, part_fail_show, part_fail_store);
819 #endif
820 #ifdef CONFIG_FAIL_IO_TIMEOUT
821 static struct device_attribute dev_attr_fail_timeout =
822 	__ATTR(io-timeout-fail,  S_IRUGO|S_IWUSR, part_timeout_show,
823 		part_timeout_store);
824 #endif
825 
826 static struct attribute *disk_attrs[] = {
827 	&dev_attr_range.attr,
828 	&dev_attr_ext_range.attr,
829 	&dev_attr_removable.attr,
830 	&dev_attr_ro.attr,
831 	&dev_attr_size.attr,
832 	&dev_attr_capability.attr,
833 	&dev_attr_stat.attr,
834 #ifdef CONFIG_FAIL_MAKE_REQUEST
835 	&dev_attr_fail.attr,
836 #endif
837 #ifdef CONFIG_FAIL_IO_TIMEOUT
838 	&dev_attr_fail_timeout.attr,
839 #endif
840 	NULL
841 };
842 
843 static struct attribute_group disk_attr_group = {
844 	.attrs = disk_attrs,
845 };
846 
847 static struct attribute_group *disk_attr_groups[] = {
848 	&disk_attr_group,
849 	NULL
850 };
851 
852 static void disk_free_ptbl_rcu_cb(struct rcu_head *head)
853 {
854 	struct disk_part_tbl *ptbl =
855 		container_of(head, struct disk_part_tbl, rcu_head);
856 
857 	kfree(ptbl);
858 }
859 
860 /**
861  * disk_replace_part_tbl - replace disk->part_tbl in RCU-safe way
862  * @disk: disk to replace part_tbl for
863  * @new_ptbl: new part_tbl to install
864  *
865  * Replace disk->part_tbl with @new_ptbl in RCU-safe way.  The
866  * original ptbl is freed using RCU callback.
867  *
868  * LOCKING:
869  * Matching bd_mutx locked.
870  */
871 static void disk_replace_part_tbl(struct gendisk *disk,
872 				  struct disk_part_tbl *new_ptbl)
873 {
874 	struct disk_part_tbl *old_ptbl = disk->part_tbl;
875 
876 	rcu_assign_pointer(disk->part_tbl, new_ptbl);
877 	if (old_ptbl)
878 		call_rcu(&old_ptbl->rcu_head, disk_free_ptbl_rcu_cb);
879 }
880 
881 /**
882  * disk_expand_part_tbl - expand disk->part_tbl
883  * @disk: disk to expand part_tbl for
884  * @partno: expand such that this partno can fit in
885  *
886  * Expand disk->part_tbl such that @partno can fit in.  disk->part_tbl
887  * uses RCU to allow unlocked dereferencing for stats and other stuff.
888  *
889  * LOCKING:
890  * Matching bd_mutex locked, might sleep.
891  *
892  * RETURNS:
893  * 0 on success, -errno on failure.
894  */
895 int disk_expand_part_tbl(struct gendisk *disk, int partno)
896 {
897 	struct disk_part_tbl *old_ptbl = disk->part_tbl;
898 	struct disk_part_tbl *new_ptbl;
899 	int len = old_ptbl ? old_ptbl->len : 0;
900 	int target = partno + 1;
901 	size_t size;
902 	int i;
903 
904 	/* disk_max_parts() is zero during initialization, ignore if so */
905 	if (disk_max_parts(disk) && target > disk_max_parts(disk))
906 		return -EINVAL;
907 
908 	if (target <= len)
909 		return 0;
910 
911 	size = sizeof(*new_ptbl) + target * sizeof(new_ptbl->part[0]);
912 	new_ptbl = kzalloc_node(size, GFP_KERNEL, disk->node_id);
913 	if (!new_ptbl)
914 		return -ENOMEM;
915 
916 	INIT_RCU_HEAD(&new_ptbl->rcu_head);
917 	new_ptbl->len = target;
918 
919 	for (i = 0; i < len; i++)
920 		rcu_assign_pointer(new_ptbl->part[i], old_ptbl->part[i]);
921 
922 	disk_replace_part_tbl(disk, new_ptbl);
923 	return 0;
924 }
925 
926 static void disk_release(struct device *dev)
927 {
928 	struct gendisk *disk = dev_to_disk(dev);
929 
930 	kfree(disk->random);
931 	disk_replace_part_tbl(disk, NULL);
932 	free_part_stats(&disk->part0);
933 	kfree(disk);
934 }
935 struct class block_class = {
936 	.name		= "block",
937 };
938 
939 static struct device_type disk_type = {
940 	.name		= "disk",
941 	.groups		= disk_attr_groups,
942 	.release	= disk_release,
943 };
944 
945 #ifdef CONFIG_PROC_FS
946 /*
947  * aggregate disk stat collector.  Uses the same stats that the sysfs
948  * entries do, above, but makes them available through one seq_file.
949  *
950  * The output looks suspiciously like /proc/partitions with a bunch of
951  * extra fields.
952  */
953 static int diskstats_show(struct seq_file *seqf, void *v)
954 {
955 	struct gendisk *gp = v;
956 	struct disk_part_iter piter;
957 	struct hd_struct *hd;
958 	char buf[BDEVNAME_SIZE];
959 	int cpu;
960 
961 	/*
962 	if (&disk_to_dev(gp)->kobj.entry == block_class.devices.next)
963 		seq_puts(seqf,	"major minor name"
964 				"     rio rmerge rsect ruse wio wmerge "
965 				"wsect wuse running use aveq"
966 				"\n\n");
967 	*/
968 
969 	disk_part_iter_init(&piter, gp, DISK_PITER_INCL_PART0);
970 	while ((hd = disk_part_iter_next(&piter))) {
971 		cpu = part_stat_lock();
972 		part_round_stats(cpu, hd);
973 		part_stat_unlock();
974 		seq_printf(seqf, "%4d %7d %s %lu %lu %llu "
975 			   "%u %lu %lu %llu %u %u %u %u\n",
976 			   MAJOR(part_devt(hd)), MINOR(part_devt(hd)),
977 			   disk_name(gp, hd->partno, buf),
978 			   part_stat_read(hd, ios[0]),
979 			   part_stat_read(hd, merges[0]),
980 			   (unsigned long long)part_stat_read(hd, sectors[0]),
981 			   jiffies_to_msecs(part_stat_read(hd, ticks[0])),
982 			   part_stat_read(hd, ios[1]),
983 			   part_stat_read(hd, merges[1]),
984 			   (unsigned long long)part_stat_read(hd, sectors[1]),
985 			   jiffies_to_msecs(part_stat_read(hd, ticks[1])),
986 			   hd->in_flight,
987 			   jiffies_to_msecs(part_stat_read(hd, io_ticks)),
988 			   jiffies_to_msecs(part_stat_read(hd, time_in_queue))
989 			);
990 	}
991 	disk_part_iter_exit(&piter);
992 
993 	return 0;
994 }
995 
996 const struct seq_operations diskstats_op = {
997 	.start	= disk_seqf_start,
998 	.next	= disk_seqf_next,
999 	.stop	= disk_seqf_stop,
1000 	.show	= diskstats_show
1001 };
1002 #endif /* CONFIG_PROC_FS */
1003 
1004 static void media_change_notify_thread(struct work_struct *work)
1005 {
1006 	struct gendisk *gd = container_of(work, struct gendisk, async_notify);
1007 	char event[] = "MEDIA_CHANGE=1";
1008 	char *envp[] = { event, NULL };
1009 
1010 	/*
1011 	 * set enviroment vars to indicate which event this is for
1012 	 * so that user space will know to go check the media status.
1013 	 */
1014 	kobject_uevent_env(&disk_to_dev(gd)->kobj, KOBJ_CHANGE, envp);
1015 	put_device(gd->driverfs_dev);
1016 }
1017 
1018 #if 0
1019 void genhd_media_change_notify(struct gendisk *disk)
1020 {
1021 	get_device(disk->driverfs_dev);
1022 	schedule_work(&disk->async_notify);
1023 }
1024 EXPORT_SYMBOL_GPL(genhd_media_change_notify);
1025 #endif  /*  0  */
1026 
1027 dev_t blk_lookup_devt(const char *name, int partno)
1028 {
1029 	dev_t devt = MKDEV(0, 0);
1030 	struct class_dev_iter iter;
1031 	struct device *dev;
1032 
1033 	class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
1034 	while ((dev = class_dev_iter_next(&iter))) {
1035 		struct gendisk *disk = dev_to_disk(dev);
1036 		struct hd_struct *part;
1037 
1038 		if (strcmp(dev->bus_id, name))
1039 			continue;
1040 
1041 		part = disk_get_part(disk, partno);
1042 		if (part) {
1043 			devt = part_devt(part);
1044 			disk_put_part(part);
1045 			break;
1046 		}
1047 		disk_put_part(part);
1048 	}
1049 	class_dev_iter_exit(&iter);
1050 	return devt;
1051 }
1052 EXPORT_SYMBOL(blk_lookup_devt);
1053 
1054 struct gendisk *alloc_disk(int minors)
1055 {
1056 	return alloc_disk_node(minors, -1);
1057 }
1058 EXPORT_SYMBOL(alloc_disk);
1059 
1060 struct gendisk *alloc_disk_node(int minors, int node_id)
1061 {
1062 	struct gendisk *disk;
1063 
1064 	disk = kmalloc_node(sizeof(struct gendisk),
1065 				GFP_KERNEL | __GFP_ZERO, node_id);
1066 	if (disk) {
1067 		if (!init_part_stats(&disk->part0)) {
1068 			kfree(disk);
1069 			return NULL;
1070 		}
1071 		if (disk_expand_part_tbl(disk, 0)) {
1072 			free_part_stats(&disk->part0);
1073 			kfree(disk);
1074 			return NULL;
1075 		}
1076 		disk->part_tbl->part[0] = &disk->part0;
1077 
1078 		disk->minors = minors;
1079 		rand_initialize_disk(disk);
1080 		disk_to_dev(disk)->class = &block_class;
1081 		disk_to_dev(disk)->type = &disk_type;
1082 		device_initialize(disk_to_dev(disk));
1083 		INIT_WORK(&disk->async_notify,
1084 			media_change_notify_thread);
1085 		disk->node_id = node_id;
1086 	}
1087 	return disk;
1088 }
1089 EXPORT_SYMBOL(alloc_disk_node);
1090 
1091 struct kobject *get_disk(struct gendisk *disk)
1092 {
1093 	struct module *owner;
1094 	struct kobject *kobj;
1095 
1096 	if (!disk->fops)
1097 		return NULL;
1098 	owner = disk->fops->owner;
1099 	if (owner && !try_module_get(owner))
1100 		return NULL;
1101 	kobj = kobject_get(&disk_to_dev(disk)->kobj);
1102 	if (kobj == NULL) {
1103 		module_put(owner);
1104 		return NULL;
1105 	}
1106 	return kobj;
1107 
1108 }
1109 
1110 EXPORT_SYMBOL(get_disk);
1111 
1112 void put_disk(struct gendisk *disk)
1113 {
1114 	if (disk)
1115 		kobject_put(&disk_to_dev(disk)->kobj);
1116 }
1117 
1118 EXPORT_SYMBOL(put_disk);
1119 
1120 void set_device_ro(struct block_device *bdev, int flag)
1121 {
1122 	bdev->bd_part->policy = flag;
1123 }
1124 
1125 EXPORT_SYMBOL(set_device_ro);
1126 
1127 void set_disk_ro(struct gendisk *disk, int flag)
1128 {
1129 	struct disk_part_iter piter;
1130 	struct hd_struct *part;
1131 
1132 	disk_part_iter_init(&piter, disk,
1133 			    DISK_PITER_INCL_EMPTY | DISK_PITER_INCL_PART0);
1134 	while ((part = disk_part_iter_next(&piter)))
1135 		part->policy = flag;
1136 	disk_part_iter_exit(&piter);
1137 }
1138 
1139 EXPORT_SYMBOL(set_disk_ro);
1140 
1141 int bdev_read_only(struct block_device *bdev)
1142 {
1143 	if (!bdev)
1144 		return 0;
1145 	return bdev->bd_part->policy;
1146 }
1147 
1148 EXPORT_SYMBOL(bdev_read_only);
1149 
1150 int invalidate_partition(struct gendisk *disk, int partno)
1151 {
1152 	int res = 0;
1153 	struct block_device *bdev = bdget_disk(disk, partno);
1154 	if (bdev) {
1155 		fsync_bdev(bdev);
1156 		res = __invalidate_device(bdev);
1157 		bdput(bdev);
1158 	}
1159 	return res;
1160 }
1161 
1162 EXPORT_SYMBOL(invalidate_partition);
1163