blob: 7bbfed05cecb1a0eab2cab78c29c899e9b548c7c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * gendisk handling
3 */
4
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#include <linux/module.h>
6#include <linux/fs.h>
7#include <linux/genhd.h>
Andrew Mortonb446b602007-02-20 13:57:48 -08008#include <linux/kdev_t.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#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>
Christoph Hellwig2ef41632005-05-05 16:15:59 -070017#include <linux/buffer_head.h>
Jes Sorensen58383af2006-02-06 14:12:43 -080018#include <linux/mutex.h>
Tejun Heobcce3de2008-08-25 19:47:22 +090019#include <linux/idr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Adrian Bunkff889722008-03-04 11:23:45 +010021#include "blk.h"
22
Kay Sieversedfaa7c2007-05-21 22:08:01 +020023static DEFINE_MUTEX(block_class_lock);
24#ifndef CONFIG_SYSFS_DEPRECATED
25struct kobject *block_depr;
26#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Tejun Heobcce3de2008-08-25 19:47:22 +090028/* 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
Adrian Bunk1826ead2008-03-04 11:23:46 +010037static struct device_type disk_type;
38
Tejun Heoe71bf0d2008-09-03 09:03:02 +020039/**
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{
55 struct hd_struct *part;
56
57 if (unlikely(partno < 1 || partno > disk_max_parts(disk)))
58 return NULL;
59 rcu_read_lock();
60 part = rcu_dereference(disk->__part[partno - 1]);
61 if (part)
62 get_device(&part->dev);
63 rcu_read_unlock();
64
65 return part;
66}
67EXPORT_SYMBOL_GPL(disk_get_part);
68
69/**
70 * disk_part_iter_init - initialize partition iterator
71 * @piter: iterator to initialize
72 * @disk: disk to iterate over
73 * @flags: DISK_PITER_* flags
74 *
75 * Initialize @piter so that it iterates over partitions of @disk.
76 *
77 * CONTEXT:
78 * Don't care.
79 */
80void disk_part_iter_init(struct disk_part_iter *piter, struct gendisk *disk,
81 unsigned int flags)
82{
83 piter->disk = disk;
84 piter->part = NULL;
85
86 if (flags & DISK_PITER_REVERSE)
87 piter->idx = disk_max_parts(piter->disk) - 1;
88 else
89 piter->idx = 0;
90
91 piter->flags = flags;
92}
93EXPORT_SYMBOL_GPL(disk_part_iter_init);
94
95/**
96 * disk_part_iter_next - proceed iterator to the next partition and return it
97 * @piter: iterator of interest
98 *
99 * Proceed @piter to the next partition and return it.
100 *
101 * CONTEXT:
102 * Don't care.
103 */
104struct hd_struct *disk_part_iter_next(struct disk_part_iter *piter)
105{
106 int inc, end;
107
108 /* put the last partition */
109 disk_put_part(piter->part);
110 piter->part = NULL;
111
112 rcu_read_lock();
113
114 /* determine iteration parameters */
115 if (piter->flags & DISK_PITER_REVERSE) {
116 inc = -1;
117 end = -1;
118 } else {
119 inc = 1;
120 end = disk_max_parts(piter->disk);
121 }
122
123 /* iterate to the next partition */
124 for (; piter->idx != end; piter->idx += inc) {
125 struct hd_struct *part;
126
127 part = rcu_dereference(piter->disk->__part[piter->idx]);
128 if (!part)
129 continue;
130 if (!(piter->flags & DISK_PITER_INCL_EMPTY) && !part->nr_sects)
131 continue;
132
133 get_device(&part->dev);
134 piter->part = part;
135 piter->idx += inc;
136 break;
137 }
138
139 rcu_read_unlock();
140
141 return piter->part;
142}
143EXPORT_SYMBOL_GPL(disk_part_iter_next);
144
145/**
146 * disk_part_iter_exit - finish up partition iteration
147 * @piter: iter of interest
148 *
149 * Called when iteration is over. Cleans up @piter.
150 *
151 * CONTEXT:
152 * Don't care.
153 */
154void disk_part_iter_exit(struct disk_part_iter *piter)
155{
156 disk_put_part(piter->part);
157 piter->part = NULL;
158}
159EXPORT_SYMBOL_GPL(disk_part_iter_exit);
160
161/**
162 * disk_map_sector_rcu - map sector to partition
163 * @disk: gendisk of interest
164 * @sector: sector to map
165 *
166 * Find out which partition @sector maps to on @disk. This is
167 * primarily used for stats accounting.
168 *
169 * CONTEXT:
170 * RCU read locked. The returned partition pointer is valid only
171 * while preemption is disabled.
172 *
173 * RETURNS:
174 * Found partition on success, NULL if there's no matching partition.
175 */
176struct hd_struct *disk_map_sector_rcu(struct gendisk *disk, sector_t sector)
177{
178 int i;
179
180 for (i = 0; i < disk_max_parts(disk); i++) {
181 struct hd_struct *part = rcu_dereference(disk->__part[i]);
182
183 if (part && part->start_sect <= sector &&
184 sector < part->start_sect + part->nr_sects)
185 return part;
186 }
187 return NULL;
188}
189EXPORT_SYMBOL_GPL(disk_map_sector_rcu);
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191/*
192 * Can be deleted altogether. Later.
193 *
194 */
195static struct blk_major_name {
196 struct blk_major_name *next;
197 int major;
198 char name[16];
Joe Korty68eef3b2006-03-31 02:30:32 -0800199} *major_names[BLKDEV_MAJOR_HASH_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201/* index in the above - for now: assume no multimajor ranges */
202static inline int major_to_index(int major)
203{
Joe Korty68eef3b2006-03-31 02:30:32 -0800204 return major % BLKDEV_MAJOR_HASH_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205}
206
Joe Korty68eef3b2006-03-31 02:30:32 -0800207#ifdef CONFIG_PROC_FS
Tejun Heocf771cb2008-09-03 09:01:09 +0200208void blkdev_show(struct seq_file *seqf, off_t offset)
Neil Horman7170be52006-01-14 13:20:38 -0800209{
Joe Korty68eef3b2006-03-31 02:30:32 -0800210 struct blk_major_name *dp;
Neil Horman7170be52006-01-14 13:20:38 -0800211
Joe Korty68eef3b2006-03-31 02:30:32 -0800212 if (offset < BLKDEV_MAJOR_HASH_SIZE) {
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200213 mutex_lock(&block_class_lock);
Joe Korty68eef3b2006-03-31 02:30:32 -0800214 for (dp = major_names[offset]; dp; dp = dp->next)
Tejun Heocf771cb2008-09-03 09:01:09 +0200215 seq_printf(seqf, "%3d %s\n", dp->major, dp->name);
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200216 mutex_unlock(&block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218}
Joe Korty68eef3b2006-03-31 02:30:32 -0800219#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221int register_blkdev(unsigned int major, const char *name)
222{
223 struct blk_major_name **n, *p;
224 int index, ret = 0;
225
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200226 mutex_lock(&block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 /* temporary */
229 if (major == 0) {
230 for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) {
231 if (major_names[index] == NULL)
232 break;
233 }
234
235 if (index == 0) {
236 printk("register_blkdev: failed to get major for %s\n",
237 name);
238 ret = -EBUSY;
239 goto out;
240 }
241 major = index;
242 ret = major;
243 }
244
245 p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL);
246 if (p == NULL) {
247 ret = -ENOMEM;
248 goto out;
249 }
250
251 p->major = major;
252 strlcpy(p->name, name, sizeof(p->name));
253 p->next = NULL;
254 index = major_to_index(major);
255
256 for (n = &major_names[index]; *n; n = &(*n)->next) {
257 if ((*n)->major == major)
258 break;
259 }
260 if (!*n)
261 *n = p;
262 else
263 ret = -EBUSY;
264
265 if (ret < 0) {
266 printk("register_blkdev: cannot get major %d for %s\n",
267 major, name);
268 kfree(p);
269 }
270out:
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200271 mutex_unlock(&block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 return ret;
273}
274
275EXPORT_SYMBOL(register_blkdev);
276
Akinobu Mitaf4480242007-07-17 04:03:47 -0700277void unregister_blkdev(unsigned int major, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
279 struct blk_major_name **n;
280 struct blk_major_name *p = NULL;
281 int index = major_to_index(major);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200283 mutex_lock(&block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 for (n = &major_names[index]; *n; n = &(*n)->next)
285 if ((*n)->major == major)
286 break;
Akinobu Mita294462a2007-07-17 04:03:45 -0700287 if (!*n || strcmp((*n)->name, name)) {
288 WARN_ON(1);
Akinobu Mita294462a2007-07-17 04:03:45 -0700289 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 p = *n;
291 *n = p->next;
292 }
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200293 mutex_unlock(&block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
296
297EXPORT_SYMBOL(unregister_blkdev);
298
299static struct kobj_map *bdev_map;
300
Tejun Heobcce3de2008-08-25 19:47:22 +0900301/**
302 * blk_alloc_devt - allocate a dev_t for a partition
303 * @part: partition to allocate dev_t for
304 * @gfp_mask: memory allocation flag
305 * @devt: out parameter for resulting dev_t
306 *
307 * Allocate a dev_t for block device.
308 *
309 * RETURNS:
310 * 0 on success, allocated dev_t is returned in *@devt. -errno on
311 * failure.
312 *
313 * CONTEXT:
314 * Might sleep.
315 */
316int blk_alloc_devt(struct hd_struct *part, dev_t *devt)
317{
318 struct gendisk *disk = part_to_disk(part);
319 int idx, rc;
320
321 /* in consecutive minor range? */
322 if (part->partno < disk->minors) {
323 *devt = MKDEV(disk->major, disk->first_minor + part->partno);
324 return 0;
325 }
326
327 /* allocate ext devt */
328 do {
329 if (!idr_pre_get(&ext_devt_idr, GFP_KERNEL))
330 return -ENOMEM;
331 rc = idr_get_new(&ext_devt_idr, part, &idx);
332 } while (rc == -EAGAIN);
333
334 if (rc)
335 return rc;
336
337 if (idx > MAX_EXT_DEVT) {
338 idr_remove(&ext_devt_idr, idx);
339 return -EBUSY;
340 }
341
342 *devt = MKDEV(BLOCK_EXT_MAJOR, idx);
343 return 0;
344}
345
346/**
347 * blk_free_devt - free a dev_t
348 * @devt: dev_t to free
349 *
350 * Free @devt which was allocated using blk_alloc_devt().
351 *
352 * CONTEXT:
353 * Might sleep.
354 */
355void blk_free_devt(dev_t devt)
356{
357 might_sleep();
358
359 if (devt == MKDEV(0, 0))
360 return;
361
362 if (MAJOR(devt) == BLOCK_EXT_MAJOR) {
363 mutex_lock(&ext_devt_mutex);
364 idr_remove(&ext_devt_idr, MINOR(devt));
365 mutex_unlock(&ext_devt_mutex);
366 }
367}
368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369/*
370 * Register device numbers dev..(dev+range-1)
371 * range must be nonzero
372 * The hash chain is sorted on range, so that subranges can override.
373 */
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200374void blk_register_region(dev_t devt, unsigned long range, struct module *module,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 struct kobject *(*probe)(dev_t, int *, void *),
376 int (*lock)(dev_t, void *), void *data)
377{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200378 kobj_map(bdev_map, devt, range, module, probe, lock, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379}
380
381EXPORT_SYMBOL(blk_register_region);
382
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200383void blk_unregister_region(dev_t devt, unsigned long range)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200385 kobj_unmap(bdev_map, devt, range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
387
388EXPORT_SYMBOL(blk_unregister_region);
389
Tejun Heocf771cb2008-09-03 09:01:09 +0200390static struct kobject *exact_match(dev_t devt, int *partno, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
392 struct gendisk *p = data;
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200393
394 return &p->dev.kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395}
396
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200397static int exact_lock(dev_t devt, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
399 struct gendisk *p = data;
400
401 if (!get_disk(p))
402 return -1;
403 return 0;
404}
405
406/**
407 * add_disk - add partitioning information to kernel list
408 * @disk: per-device partitioning information
409 *
410 * This function registers the partitioning information in @disk
411 * with the kernel.
412 */
413void add_disk(struct gendisk *disk)
414{
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700415 struct backing_dev_info *bdi;
Greg Kroah-Hartman6ffeea72008-05-22 17:21:08 -0400416 int retval;
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 disk->flags |= GENHD_FL_UP;
Tejun Heof331c022008-09-03 09:01:48 +0200419 disk->dev.devt = MKDEV(disk->major, disk->first_minor);
420 blk_register_region(disk_devt(disk), disk->minors, NULL,
421 exact_match, exact_lock, disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 register_disk(disk);
423 blk_register_queue(disk);
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700424
425 bdi = &disk->queue->backing_dev_info;
Tejun Heof331c022008-09-03 09:01:48 +0200426 bdi_register_dev(bdi, disk_devt(disk));
Greg Kroah-Hartman6ffeea72008-05-22 17:21:08 -0400427 retval = sysfs_create_link(&disk->dev.kobj, &bdi->dev->kobj, "bdi");
428 WARN_ON(retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429}
430
431EXPORT_SYMBOL(add_disk);
432EXPORT_SYMBOL(del_gendisk); /* in partitions/check.c */
433
434void unlink_gendisk(struct gendisk *disk)
435{
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700436 sysfs_remove_link(&disk->dev.kobj, "bdi");
437 bdi_unregister(&disk->queue->backing_dev_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 blk_unregister_queue(disk);
Tejun Heof331c022008-09-03 09:01:48 +0200439 blk_unregister_region(disk_devt(disk), disk->minors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440}
441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442/**
443 * get_gendisk - get partitioning information for a given device
Randy Dunlap710027a2008-08-19 20:13:11 +0200444 * @devt: device to get partitioning information for
445 * @part: returned partition index
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 *
447 * This function gets the structure containing partitioning
Randy Dunlap710027a2008-08-19 20:13:11 +0200448 * information for the given device @devt.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 */
Tejun Heocf771cb2008-09-03 09:01:09 +0200450struct gendisk *get_gendisk(dev_t devt, int *partno)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
Tejun Heobcce3de2008-08-25 19:47:22 +0900452 struct gendisk *disk = NULL;
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200453
Tejun Heobcce3de2008-08-25 19:47:22 +0900454 if (MAJOR(devt) != BLOCK_EXT_MAJOR) {
455 struct kobject *kobj;
456
457 kobj = kobj_lookup(bdev_map, devt, partno);
458 if (kobj)
459 disk = dev_to_disk(kobj_to_dev(kobj));
460 } else {
461 struct hd_struct *part;
462
463 mutex_lock(&ext_devt_mutex);
464 part = idr_find(&ext_devt_idr, MINOR(devt));
465 if (part && get_disk(part_to_disk(part))) {
466 *partno = part->partno;
467 disk = part_to_disk(part);
468 }
469 mutex_unlock(&ext_devt_mutex);
470 }
471
472 return disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473}
474
Tejun Heof331c022008-09-03 09:01:48 +0200475/**
476 * bdget_disk - do bdget() by gendisk and partition number
477 * @disk: gendisk of interest
478 * @partno: partition number
479 *
480 * Find partition @partno from @disk, do bdget() on it.
481 *
482 * CONTEXT:
483 * Don't care.
484 *
485 * RETURNS:
486 * Resulting block_device on success, NULL on failure.
487 */
488extern struct block_device *bdget_disk(struct gendisk *disk, int partno)
489{
490 dev_t devt = MKDEV(0, 0);
491
492 if (partno == 0)
493 devt = disk_devt(disk);
494 else {
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200495 struct hd_struct *part;
Tejun Heof331c022008-09-03 09:01:48 +0200496
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200497 part = disk_get_part(disk, partno);
Tejun Heof331c022008-09-03 09:01:48 +0200498 if (part && part->nr_sects)
499 devt = part_devt(part);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200500 disk_put_part(part);
Tejun Heof331c022008-09-03 09:01:48 +0200501 }
502
503 if (likely(devt != MKDEV(0, 0)))
504 return bdget(devt);
505 return NULL;
506}
507EXPORT_SYMBOL(bdget_disk);
508
Dave Gilbertdd2a3452007-05-09 02:33:24 -0700509/*
510 * print a full list of all partitions - intended for places where the root
511 * filesystem can't be mounted and thus to give the victim some idea of what
512 * went wrong
513 */
514void __init printk_all_partitions(void)
515{
Tejun Heodef4e382008-09-03 08:57:12 +0200516 struct class_dev_iter iter;
517 struct device *dev;
518
519 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
520 while ((dev = class_dev_iter_next(&iter))) {
521 struct gendisk *disk = dev_to_disk(dev);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200522 struct disk_part_iter piter;
523 struct hd_struct *part;
Tejun Heodef4e382008-09-03 08:57:12 +0200524 char buf[BDEVNAME_SIZE];
Tejun Heodef4e382008-09-03 08:57:12 +0200525
526 /*
527 * Don't show empty devices or things that have been
528 * surpressed
529 */
530 if (get_capacity(disk) == 0 ||
531 (disk->flags & GENHD_FL_SUPPRESS_PARTITION_INFO))
532 continue;
533
534 /*
535 * Note, unlike /proc/partitions, I am showing the
536 * numbers in hex - the same format as the root=
537 * option takes.
538 */
539 printk("%02x%02x %10llu %s",
Tejun Heof331c022008-09-03 09:01:48 +0200540 MAJOR(disk_devt(disk)), MINOR(disk_devt(disk)),
Tejun Heodef4e382008-09-03 08:57:12 +0200541 (unsigned long long)get_capacity(disk) >> 1,
542 disk_name(disk, 0, buf));
543 if (disk->driverfs_dev != NULL &&
544 disk->driverfs_dev->driver != NULL)
545 printk(" driver: %s\n",
546 disk->driverfs_dev->driver->name);
547 else
548 printk(" (driver?)\n");
549
550 /* now show the partitions */
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200551 disk_part_iter_init(&piter, disk, 0);
552 while ((part = disk_part_iter_next(&piter)))
Tejun Heodef4e382008-09-03 08:57:12 +0200553 printk(" %02x%02x %10llu %s\n",
Tejun Heof331c022008-09-03 09:01:48 +0200554 MAJOR(part_devt(part)), MINOR(part_devt(part)),
555 (unsigned long long)part->nr_sects >> 1,
556 disk_name(disk, part->partno, buf));
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200557 disk_part_iter_exit(&piter);
Tejun Heodef4e382008-09-03 08:57:12 +0200558 }
559 class_dev_iter_exit(&iter);
Dave Gilbertdd2a3452007-05-09 02:33:24 -0700560}
561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562#ifdef CONFIG_PROC_FS
563/* iterator */
Tejun Heodef4e382008-09-03 08:57:12 +0200564static void *disk_seqf_start(struct seq_file *seqf, loff_t *pos)
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -0400565{
Tejun Heodef4e382008-09-03 08:57:12 +0200566 loff_t skip = *pos;
567 struct class_dev_iter *iter;
568 struct device *dev;
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -0400569
Tejun Heodef4e382008-09-03 08:57:12 +0200570 iter = kmalloc(GFP_KERNEL, sizeof(*iter));
571 if (!iter)
572 return ERR_PTR(-ENOMEM);
573
574 seqf->private = iter;
575 class_dev_iter_init(iter, &block_class, NULL, &disk_type);
576 do {
577 dev = class_dev_iter_next(iter);
578 if (!dev)
579 return NULL;
580 } while (skip--);
581
582 return dev_to_disk(dev);
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -0400583}
584
Tejun Heodef4e382008-09-03 08:57:12 +0200585static void *disk_seqf_next(struct seq_file *seqf, void *v, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200587 struct device *dev;
Greg Kroah-Hartman66c64af2008-05-22 17:21:08 -0400588
Tejun Heodef4e382008-09-03 08:57:12 +0200589 (*pos)++;
590 dev = class_dev_iter_next(seqf->private);
Tejun Heo2ac3cee2008-09-03 08:53:37 +0200591 if (dev)
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -0400592 return dev_to_disk(dev);
Tejun Heo2ac3cee2008-09-03 08:53:37 +0200593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 return NULL;
595}
596
Tejun Heodef4e382008-09-03 08:57:12 +0200597static void disk_seqf_stop(struct seq_file *seqf, void *v)
Greg Kroah-Hartman27f30252008-05-22 17:21:08 -0400598{
Tejun Heodef4e382008-09-03 08:57:12 +0200599 struct class_dev_iter *iter = seqf->private;
Greg Kroah-Hartman27f30252008-05-22 17:21:08 -0400600
Tejun Heodef4e382008-09-03 08:57:12 +0200601 /* stop is called even after start failed :-( */
602 if (iter) {
603 class_dev_iter_exit(iter);
604 kfree(iter);
Kay Sievers5c0ef6d2008-08-16 14:30:30 +0200605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606}
607
Tejun Heodef4e382008-09-03 08:57:12 +0200608static void *show_partition_start(struct seq_file *seqf, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
Tejun Heodef4e382008-09-03 08:57:12 +0200610 static void *p;
611
612 p = disk_seqf_start(seqf, pos);
613 if (!IS_ERR(p) && p)
614 seq_puts(seqf, "major minor #blocks name\n\n");
615 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616}
617
Tejun Heocf771cb2008-09-03 09:01:09 +0200618static int show_partition(struct seq_file *seqf, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
620 struct gendisk *sgp = v;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200621 struct disk_part_iter piter;
622 struct hd_struct *part;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 char buf[BDEVNAME_SIZE];
624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 /* Don't show non-partitionable removeable devices or empty devices */
Tejun Heof331c022008-09-03 09:01:48 +0200626 if (!get_capacity(sgp) || (!disk_max_parts(sgp) &&
627 (sgp->flags & GENHD_FL_REMOVABLE)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 return 0;
629 if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)
630 return 0;
631
632 /* show the full disk and all non-0 size partitions of it */
Tejun Heocf771cb2008-09-03 09:01:09 +0200633 seq_printf(seqf, "%4d %4d %10llu %s\n",
Tejun Heof331c022008-09-03 09:01:48 +0200634 MAJOR(disk_devt(sgp)), MINOR(disk_devt(sgp)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 (unsigned long long)get_capacity(sgp) >> 1,
636 disk_name(sgp, 0, buf));
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200637
638 disk_part_iter_init(&piter, sgp, 0);
639 while ((part = disk_part_iter_next(&piter)))
Tejun Heocf771cb2008-09-03 09:01:09 +0200640 seq_printf(seqf, "%4d %4d %10llu %s\n",
Tejun Heof331c022008-09-03 09:01:48 +0200641 MAJOR(part_devt(part)), MINOR(part_devt(part)),
642 (unsigned long long)part->nr_sects >> 1,
643 disk_name(sgp, part->partno, buf));
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200644 disk_part_iter_exit(&piter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
646 return 0;
647}
648
Jan Engelhardt12f32bb2008-01-29 20:57:51 +0100649const struct seq_operations partitions_op = {
Tejun Heodef4e382008-09-03 08:57:12 +0200650 .start = show_partition_start,
651 .next = disk_seqf_next,
652 .stop = disk_seqf_stop,
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200653 .show = show_partition
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654};
655#endif
656
657
Tejun Heocf771cb2008-09-03 09:01:09 +0200658static struct kobject *base_probe(dev_t devt, int *partno, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200660 if (request_module("block-major-%d-%d", MAJOR(devt), MINOR(devt)) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 /* Make old-style 2.4 aliases work */
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200662 request_module("block-major-%d", MAJOR(devt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 return NULL;
664}
665
666static int __init genhd_device_init(void)
667{
Dan Williamse105b8b2008-04-21 10:51:07 -0700668 int error;
669
670 block_class.dev_kobj = sysfs_dev_block_kobj;
671 error = class_register(&block_class);
Roland McGrathee27a552008-03-11 17:13:15 -0700672 if (unlikely(error))
673 return error;
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200674 bdev_map = kobj_map_init(base_probe, &block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 blk_dev_init();
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200676
677#ifndef CONFIG_SYSFS_DEPRECATED
678 /* create top-level block dir */
679 block_depr = kobject_create_and_add("block", NULL);
680#endif
Greg Kroah-Hartman830d3cf2007-11-06 10:36:58 -0800681 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682}
683
684subsys_initcall(genhd_device_init);
685
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200686static ssize_t disk_range_show(struct device *dev,
687 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200689 struct gendisk *disk = dev_to_disk(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200691 return sprintf(buf, "%d\n", disk->minors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692}
693
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200694static ssize_t disk_removable_show(struct device *dev,
695 struct device_attribute *attr, char *buf)
Kay Sieversa7fd6702005-10-01 14:49:43 +0200696{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200697 struct gendisk *disk = dev_to_disk(dev);
Kay Sieversa7fd6702005-10-01 14:49:43 +0200698
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200699 return sprintf(buf, "%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200701}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Kay Sievers1c9ce522008-06-13 09:41:00 +0200703static ssize_t disk_ro_show(struct device *dev,
704 struct device_attribute *attr, char *buf)
705{
706 struct gendisk *disk = dev_to_disk(dev);
707
708 return sprintf(buf, "%d\n", disk->policy ? 1 : 0);
709}
710
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200711static ssize_t disk_size_show(struct device *dev,
712 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200714 struct gendisk *disk = dev_to_disk(dev);
715
716 return sprintf(buf, "%llu\n", (unsigned long long)get_capacity(disk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717}
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200718
719static ssize_t disk_capability_show(struct device *dev,
720 struct device_attribute *attr, char *buf)
Kristen Carlson Accardi86ce18d2007-05-23 13:57:38 -0700721{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200722 struct gendisk *disk = dev_to_disk(dev);
723
724 return sprintf(buf, "%x\n", disk->flags);
Kristen Carlson Accardi86ce18d2007-05-23 13:57:38 -0700725}
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200726
727static ssize_t disk_stat_show(struct device *dev,
728 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200730 struct gendisk *disk = dev_to_disk(dev);
Tejun Heoc9959052008-08-25 19:47:21 +0900731 int cpu;
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200732
Tejun Heoc9959052008-08-25 19:47:21 +0900733 cpu = disk_stat_lock();
734 disk_round_stats(cpu, disk);
735 disk_stat_unlock();
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200736 return sprintf(buf,
Ben Woodard837c7872006-03-22 08:09:31 +0100737 "%8lu %8lu %8llu %8u "
738 "%8lu %8lu %8llu %8u "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 "%8u %8u %8u"
740 "\n",
Jens Axboe47a00412005-11-09 13:38:47 +0100741 disk_stat_read(disk, ios[READ]),
742 disk_stat_read(disk, merges[READ]),
743 (unsigned long long)disk_stat_read(disk, sectors[READ]),
744 jiffies_to_msecs(disk_stat_read(disk, ticks[READ])),
745 disk_stat_read(disk, ios[WRITE]),
746 disk_stat_read(disk, merges[WRITE]),
747 (unsigned long long)disk_stat_read(disk, sectors[WRITE]),
748 jiffies_to_msecs(disk_stat_read(disk, ticks[WRITE])),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 disk->in_flight,
750 jiffies_to_msecs(disk_stat_read(disk, io_ticks)),
751 jiffies_to_msecs(disk_stat_read(disk, time_in_queue)));
752}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Akinobu Mitac17bb492006-12-08 02:39:46 -0800754#ifdef CONFIG_FAIL_MAKE_REQUEST
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200755static ssize_t disk_fail_show(struct device *dev,
756 struct device_attribute *attr, char *buf)
757{
758 struct gendisk *disk = dev_to_disk(dev);
Akinobu Mitac17bb492006-12-08 02:39:46 -0800759
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200760 return sprintf(buf, "%d\n", disk->flags & GENHD_FL_FAIL ? 1 : 0);
761}
762
763static ssize_t disk_fail_store(struct device *dev,
764 struct device_attribute *attr,
Akinobu Mitac17bb492006-12-08 02:39:46 -0800765 const char *buf, size_t count)
766{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200767 struct gendisk *disk = dev_to_disk(dev);
Akinobu Mitac17bb492006-12-08 02:39:46 -0800768 int i;
769
770 if (count > 0 && sscanf(buf, "%d", &i) > 0) {
771 if (i == 0)
772 disk->flags &= ~GENHD_FL_FAIL;
773 else
774 disk->flags |= GENHD_FL_FAIL;
775 }
776
777 return count;
778}
Akinobu Mitac17bb492006-12-08 02:39:46 -0800779
780#endif
781
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200782static DEVICE_ATTR(range, S_IRUGO, disk_range_show, NULL);
783static DEVICE_ATTR(removable, S_IRUGO, disk_removable_show, NULL);
Kay Sievers1c9ce522008-06-13 09:41:00 +0200784static DEVICE_ATTR(ro, S_IRUGO, disk_ro_show, NULL);
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200785static DEVICE_ATTR(size, S_IRUGO, disk_size_show, NULL);
786static DEVICE_ATTR(capability, S_IRUGO, disk_capability_show, NULL);
787static DEVICE_ATTR(stat, S_IRUGO, disk_stat_show, NULL);
Akinobu Mitac17bb492006-12-08 02:39:46 -0800788#ifdef CONFIG_FAIL_MAKE_REQUEST
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200789static struct device_attribute dev_attr_fail =
790 __ATTR(make-it-fail, S_IRUGO|S_IWUSR, disk_fail_show, disk_fail_store);
Akinobu Mitac17bb492006-12-08 02:39:46 -0800791#endif
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200792
793static struct attribute *disk_attrs[] = {
794 &dev_attr_range.attr,
795 &dev_attr_removable.attr,
Kay Sievers1c9ce522008-06-13 09:41:00 +0200796 &dev_attr_ro.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200797 &dev_attr_size.attr,
798 &dev_attr_capability.attr,
799 &dev_attr_stat.attr,
800#ifdef CONFIG_FAIL_MAKE_REQUEST
801 &dev_attr_fail.attr,
802#endif
803 NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804};
805
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200806static struct attribute_group disk_attr_group = {
807 .attrs = disk_attrs,
808};
809
810static struct attribute_group *disk_attr_groups[] = {
811 &disk_attr_group,
812 NULL
813};
814
815static void disk_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200817 struct gendisk *disk = dev_to_disk(dev);
818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 kfree(disk->random);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200820 kfree(disk->__part);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 free_disk_stats(disk);
822 kfree(disk);
823}
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200824struct class block_class = {
825 .name = "block",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826};
827
Adrian Bunk1826ead2008-03-04 11:23:46 +0100828static struct device_type disk_type = {
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200829 .name = "disk",
830 .groups = disk_attr_groups,
831 .release = disk_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832};
833
Randy Dunlapa6e2ba82008-05-23 09:44:11 -0700834#ifdef CONFIG_PROC_FS
Tejun Heocf771cb2008-09-03 09:01:09 +0200835/*
836 * aggregate disk stat collector. Uses the same stats that the sysfs
837 * entries do, above, but makes them available through one seq_file.
838 *
839 * The output looks suspiciously like /proc/partitions with a bunch of
840 * extra fields.
841 */
842static int diskstats_show(struct seq_file *seqf, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
844 struct gendisk *gp = v;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200845 struct disk_part_iter piter;
846 struct hd_struct *hd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 char buf[BDEVNAME_SIZE];
Tejun Heoc9959052008-08-25 19:47:21 +0900848 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
850 /*
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200851 if (&gp->dev.kobj.entry == block_class.devices.next)
Tejun Heocf771cb2008-09-03 09:01:09 +0200852 seq_puts(seqf, "major minor name"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 " rio rmerge rsect ruse wio wmerge "
854 "wsect wuse running use aveq"
855 "\n\n");
856 */
857
Tejun Heoc9959052008-08-25 19:47:21 +0900858 cpu = disk_stat_lock();
859 disk_round_stats(cpu, gp);
860 disk_stat_unlock();
Tejun Heocf771cb2008-09-03 09:01:09 +0200861 seq_printf(seqf, "%4d %4d %s %lu %lu %llu %u %lu %lu %llu %u %u %u %u\n",
Tejun Heof331c022008-09-03 09:01:48 +0200862 MAJOR(disk_devt(gp)), MINOR(disk_devt(gp)),
863 disk_name(gp, 0, buf),
Jens Axboea3623572005-11-01 09:26:16 +0100864 disk_stat_read(gp, ios[0]), disk_stat_read(gp, merges[0]),
865 (unsigned long long)disk_stat_read(gp, sectors[0]),
866 jiffies_to_msecs(disk_stat_read(gp, ticks[0])),
867 disk_stat_read(gp, ios[1]), disk_stat_read(gp, merges[1]),
868 (unsigned long long)disk_stat_read(gp, sectors[1]),
869 jiffies_to_msecs(disk_stat_read(gp, ticks[1])),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 gp->in_flight,
871 jiffies_to_msecs(disk_stat_read(gp, io_ticks)),
872 jiffies_to_msecs(disk_stat_read(gp, time_in_queue)));
873
874 /* now show all non-0 size partitions of it */
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200875 disk_part_iter_init(&piter, gp, 0);
876 while ((hd = disk_part_iter_next(&piter))) {
Tejun Heoc9959052008-08-25 19:47:21 +0900877 cpu = disk_stat_lock();
878 part_round_stats(cpu, hd);
879 disk_stat_unlock();
Tejun Heocf771cb2008-09-03 09:01:09 +0200880 seq_printf(seqf, "%4d %4d %s %lu %lu %llu "
Jerome Marchand28f39d52008-02-08 11:04:56 +0100881 "%u %lu %lu %llu %u %u %u %u\n",
Tejun Heof331c022008-09-03 09:01:48 +0200882 MAJOR(part_devt(hd)), MINOR(part_devt(hd)),
883 disk_name(gp, hd->partno, buf),
Jerome Marchand28f39d52008-02-08 11:04:56 +0100884 part_stat_read(hd, ios[0]),
885 part_stat_read(hd, merges[0]),
886 (unsigned long long)part_stat_read(hd, sectors[0]),
887 jiffies_to_msecs(part_stat_read(hd, ticks[0])),
888 part_stat_read(hd, ios[1]),
889 part_stat_read(hd, merges[1]),
890 (unsigned long long)part_stat_read(hd, sectors[1]),
891 jiffies_to_msecs(part_stat_read(hd, ticks[1])),
892 hd->in_flight,
893 jiffies_to_msecs(part_stat_read(hd, io_ticks)),
894 jiffies_to_msecs(part_stat_read(hd, time_in_queue))
895 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 }
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200897 disk_part_iter_exit(&piter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
899 return 0;
900}
901
Jan Engelhardt12f32bb2008-01-29 20:57:51 +0100902const struct seq_operations diskstats_op = {
Tejun Heodef4e382008-09-03 08:57:12 +0200903 .start = disk_seqf_start,
904 .next = disk_seqf_next,
905 .stop = disk_seqf_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 .show = diskstats_show
907};
Randy Dunlapa6e2ba82008-05-23 09:44:11 -0700908#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
Kristen Carlson Accardi8ce7ad72007-05-23 13:57:38 -0700910static void media_change_notify_thread(struct work_struct *work)
911{
912 struct gendisk *gd = container_of(work, struct gendisk, async_notify);
913 char event[] = "MEDIA_CHANGE=1";
914 char *envp[] = { event, NULL };
915
916 /*
917 * set enviroment vars to indicate which event this is for
918 * so that user space will know to go check the media status.
919 */
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200920 kobject_uevent_env(&gd->dev.kobj, KOBJ_CHANGE, envp);
Kristen Carlson Accardi8ce7ad72007-05-23 13:57:38 -0700921 put_device(gd->driverfs_dev);
922}
923
Adrian Bunk1826ead2008-03-04 11:23:46 +0100924#if 0
Kristen Carlson Accardi8ce7ad72007-05-23 13:57:38 -0700925void genhd_media_change_notify(struct gendisk *disk)
926{
927 get_device(disk->driverfs_dev);
928 schedule_work(&disk->async_notify);
929}
930EXPORT_SYMBOL_GPL(genhd_media_change_notify);
Adrian Bunk1826ead2008-03-04 11:23:46 +0100931#endif /* 0 */
Kristen Carlson Accardi8ce7ad72007-05-23 13:57:38 -0700932
Tejun Heocf771cb2008-09-03 09:01:09 +0200933dev_t blk_lookup_devt(const char *name, int partno)
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200934{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200935 dev_t devt = MKDEV(0, 0);
Tejun Heodef4e382008-09-03 08:57:12 +0200936 struct class_dev_iter iter;
937 struct device *dev;
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200938
Tejun Heodef4e382008-09-03 08:57:12 +0200939 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
940 while ((dev = class_dev_iter_next(&iter))) {
941 struct gendisk *disk = dev_to_disk(dev);
942
Tejun Heof331c022008-09-03 09:01:48 +0200943 if (strcmp(dev->bus_id, name))
944 continue;
945 if (partno < 0 || partno > disk_max_parts(disk))
946 continue;
947
948 if (partno == 0)
949 devt = disk_devt(disk);
950 else {
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200951 struct hd_struct *part;
Tejun Heof331c022008-09-03 09:01:48 +0200952
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200953 part = disk_get_part(disk, partno);
954 if (!part || !part->nr_sects) {
955 disk_put_part(part);
Tejun Heof331c022008-09-03 09:01:48 +0200956 continue;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200957 }
Tejun Heof331c022008-09-03 09:01:48 +0200958
959 devt = part_devt(part);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200960 disk_put_part(part);
Tejun Heodef4e382008-09-03 08:57:12 +0200961 }
Tejun Heof331c022008-09-03 09:01:48 +0200962 break;
Kay Sievers5c0ef6d2008-08-16 14:30:30 +0200963 }
Tejun Heodef4e382008-09-03 08:57:12 +0200964 class_dev_iter_exit(&iter);
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200965 return devt;
966}
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200967EXPORT_SYMBOL(blk_lookup_devt);
968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969struct gendisk *alloc_disk(int minors)
970{
Christoph Lameter19460892005-06-23 00:08:19 -0700971 return alloc_disk_node(minors, -1);
972}
973
974struct gendisk *alloc_disk_node(int minors, int node_id)
975{
Tejun Heobcce3de2008-08-25 19:47:22 +0900976 return alloc_disk_ext_node(minors, 0, node_id);
977}
978
979struct gendisk *alloc_disk_ext(int minors, int ext_minors)
980{
981 return alloc_disk_ext_node(minors, ext_minors, -1);
982}
983
984struct gendisk *alloc_disk_ext_node(int minors, int ext_minors, int node_id)
985{
Christoph Lameter19460892005-06-23 00:08:19 -0700986 struct gendisk *disk;
987
Christoph Lameter94f60302007-07-17 04:03:29 -0700988 disk = kmalloc_node(sizeof(struct gendisk),
989 GFP_KERNEL | __GFP_ZERO, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 if (disk) {
Tejun Heobcce3de2008-08-25 19:47:22 +0900991 int tot_minors = minors + ext_minors;
992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 if (!init_disk_stats(disk)) {
994 kfree(disk);
995 return NULL;
996 }
Tejun Heobcce3de2008-08-25 19:47:22 +0900997 if (tot_minors > 1) {
998 int size = (tot_minors - 1) * sizeof(struct hd_struct *);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200999 disk->__part = kmalloc_node(size,
Christoph Lameter94f60302007-07-17 04:03:29 -07001000 GFP_KERNEL | __GFP_ZERO, node_id);
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001001 if (!disk->__part) {
Jerome Marchandc7674032007-11-23 09:17:53 +01001002 free_disk_stats(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 kfree(disk);
1004 return NULL;
1005 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 }
1007 disk->minors = minors;
Tejun Heobcce3de2008-08-25 19:47:22 +09001008 disk->ext_minors = ext_minors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 rand_initialize_disk(disk);
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001010 disk->dev.class = &block_class;
1011 disk->dev.type = &disk_type;
1012 device_initialize(&disk->dev);
Kristen Carlson Accardi8ce7ad72007-05-23 13:57:38 -07001013 INIT_WORK(&disk->async_notify,
1014 media_change_notify_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 }
1016 return disk;
1017}
1018
1019EXPORT_SYMBOL(alloc_disk);
Christoph Lameter19460892005-06-23 00:08:19 -07001020EXPORT_SYMBOL(alloc_disk_node);
Tejun Heobcce3de2008-08-25 19:47:22 +09001021EXPORT_SYMBOL(alloc_disk_ext);
1022EXPORT_SYMBOL(alloc_disk_ext_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
1024struct kobject *get_disk(struct gendisk *disk)
1025{
1026 struct module *owner;
1027 struct kobject *kobj;
1028
1029 if (!disk->fops)
1030 return NULL;
1031 owner = disk->fops->owner;
1032 if (owner && !try_module_get(owner))
1033 return NULL;
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001034 kobj = kobject_get(&disk->dev.kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 if (kobj == NULL) {
1036 module_put(owner);
1037 return NULL;
1038 }
1039 return kobj;
1040
1041}
1042
1043EXPORT_SYMBOL(get_disk);
1044
1045void put_disk(struct gendisk *disk)
1046{
1047 if (disk)
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001048 kobject_put(&disk->dev.kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049}
1050
1051EXPORT_SYMBOL(put_disk);
1052
1053void set_device_ro(struct block_device *bdev, int flag)
1054{
1055 if (bdev->bd_contains != bdev)
1056 bdev->bd_part->policy = flag;
1057 else
1058 bdev->bd_disk->policy = flag;
1059}
1060
1061EXPORT_SYMBOL(set_device_ro);
1062
1063void set_disk_ro(struct gendisk *disk, int flag)
1064{
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001065 struct disk_part_iter piter;
1066 struct hd_struct *part;
1067
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 disk->policy = flag;
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001069 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_EMPTY);
1070 while ((part = disk_part_iter_next(&piter)))
1071 part->policy = flag;
1072 disk_part_iter_exit(&piter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073}
1074
1075EXPORT_SYMBOL(set_disk_ro);
1076
1077int bdev_read_only(struct block_device *bdev)
1078{
1079 if (!bdev)
1080 return 0;
1081 else if (bdev->bd_contains != bdev)
1082 return bdev->bd_part->policy;
1083 else
1084 return bdev->bd_disk->policy;
1085}
1086
1087EXPORT_SYMBOL(bdev_read_only);
1088
Tejun Heocf771cb2008-09-03 09:01:09 +02001089int invalidate_partition(struct gendisk *disk, int partno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
1091 int res = 0;
Tejun Heocf771cb2008-09-03 09:01:09 +02001092 struct block_device *bdev = bdget_disk(disk, partno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 if (bdev) {
Christoph Hellwig2ef41632005-05-05 16:15:59 -07001094 fsync_bdev(bdev);
1095 res = __invalidate_device(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 bdput(bdev);
1097 }
1098 return res;
1099}
1100
1101EXPORT_SYMBOL(invalidate_partition);