blob: 0a2f16bd54b765b5c9f21a1d78a2923a78a80344 [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)
Tejun Heoed9e1982008-08-25 19:56:05 +090062 get_device(part_to_dev(part));
Tejun Heoe71bf0d2008-09-03 09:03:02 +020063 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
Tejun Heoed9e1982008-08-25 19:56:05 +0900133 get_device(part_to_dev(part));
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200134 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/**
Tejun Heo870d6652008-08-25 19:47:25 +0900302 * blk_mangle_minor - scatter minor numbers apart
303 * @minor: minor number to mangle
304 *
305 * Scatter consecutively allocated @minor number apart if MANGLE_DEVT
306 * is enabled. Mangling twice gives the original value.
307 *
308 * RETURNS:
309 * Mangled value.
310 *
311 * CONTEXT:
312 * Don't care.
313 */
314static int blk_mangle_minor(int minor)
315{
316#ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT
317 int i;
318
319 for (i = 0; i < MINORBITS / 2; i++) {
320 int low = minor & (1 << i);
321 int high = minor & (1 << (MINORBITS - 1 - i));
322 int distance = MINORBITS - 1 - 2 * i;
323
324 minor ^= low | high; /* clear both bits */
325 low <<= distance; /* swap the positions */
326 high >>= distance;
327 minor |= low | high; /* and set */
328 }
329#endif
330 return minor;
331}
332
333/**
Tejun Heobcce3de2008-08-25 19:47:22 +0900334 * blk_alloc_devt - allocate a dev_t for a partition
335 * @part: partition to allocate dev_t for
336 * @gfp_mask: memory allocation flag
337 * @devt: out parameter for resulting dev_t
338 *
339 * Allocate a dev_t for block device.
340 *
341 * RETURNS:
342 * 0 on success, allocated dev_t is returned in *@devt. -errno on
343 * failure.
344 *
345 * CONTEXT:
346 * Might sleep.
347 */
348int blk_alloc_devt(struct hd_struct *part, dev_t *devt)
349{
350 struct gendisk *disk = part_to_disk(part);
351 int idx, rc;
352
353 /* in consecutive minor range? */
354 if (part->partno < disk->minors) {
355 *devt = MKDEV(disk->major, disk->first_minor + part->partno);
356 return 0;
357 }
358
359 /* allocate ext devt */
360 do {
361 if (!idr_pre_get(&ext_devt_idr, GFP_KERNEL))
362 return -ENOMEM;
363 rc = idr_get_new(&ext_devt_idr, part, &idx);
364 } while (rc == -EAGAIN);
365
366 if (rc)
367 return rc;
368
369 if (idx > MAX_EXT_DEVT) {
370 idr_remove(&ext_devt_idr, idx);
371 return -EBUSY;
372 }
373
Tejun Heo870d6652008-08-25 19:47:25 +0900374 *devt = MKDEV(BLOCK_EXT_MAJOR, blk_mangle_minor(idx));
Tejun Heobcce3de2008-08-25 19:47:22 +0900375 return 0;
376}
377
378/**
379 * blk_free_devt - free a dev_t
380 * @devt: dev_t to free
381 *
382 * Free @devt which was allocated using blk_alloc_devt().
383 *
384 * CONTEXT:
385 * Might sleep.
386 */
387void blk_free_devt(dev_t devt)
388{
389 might_sleep();
390
391 if (devt == MKDEV(0, 0))
392 return;
393
394 if (MAJOR(devt) == BLOCK_EXT_MAJOR) {
395 mutex_lock(&ext_devt_mutex);
Tejun Heo870d6652008-08-25 19:47:25 +0900396 idr_remove(&ext_devt_idr, blk_mangle_minor(MINOR(devt)));
Tejun Heobcce3de2008-08-25 19:47:22 +0900397 mutex_unlock(&ext_devt_mutex);
398 }
399}
400
Tejun Heo1f014292008-08-25 19:47:23 +0900401static char *bdevt_str(dev_t devt, char *buf)
402{
403 if (MAJOR(devt) <= 0xff && MINOR(devt) <= 0xff) {
404 char tbuf[BDEVT_SIZE];
405 snprintf(tbuf, BDEVT_SIZE, "%02x%02x", MAJOR(devt), MINOR(devt));
406 snprintf(buf, BDEVT_SIZE, "%-9s", tbuf);
407 } else
408 snprintf(buf, BDEVT_SIZE, "%03x:%05x", MAJOR(devt), MINOR(devt));
409
410 return buf;
411}
412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413/*
414 * Register device numbers dev..(dev+range-1)
415 * range must be nonzero
416 * The hash chain is sorted on range, so that subranges can override.
417 */
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200418void blk_register_region(dev_t devt, unsigned long range, struct module *module,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 struct kobject *(*probe)(dev_t, int *, void *),
420 int (*lock)(dev_t, void *), void *data)
421{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200422 kobj_map(bdev_map, devt, range, module, probe, lock, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423}
424
425EXPORT_SYMBOL(blk_register_region);
426
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200427void blk_unregister_region(dev_t devt, unsigned long range)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200429 kobj_unmap(bdev_map, devt, range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
431
432EXPORT_SYMBOL(blk_unregister_region);
433
Tejun Heocf771cb2008-09-03 09:01:09 +0200434static struct kobject *exact_match(dev_t devt, int *partno, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
436 struct gendisk *p = data;
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200437
Tejun Heoed9e1982008-08-25 19:56:05 +0900438 return &disk_to_dev(p)->kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439}
440
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200441static int exact_lock(dev_t devt, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
443 struct gendisk *p = data;
444
445 if (!get_disk(p))
446 return -1;
447 return 0;
448}
449
450/**
451 * add_disk - add partitioning information to kernel list
452 * @disk: per-device partitioning information
453 *
454 * This function registers the partitioning information in @disk
455 * with the kernel.
456 */
457void add_disk(struct gendisk *disk)
458{
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700459 struct backing_dev_info *bdi;
Greg Kroah-Hartman6ffeea72008-05-22 17:21:08 -0400460 int retval;
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 disk->flags |= GENHD_FL_UP;
Tejun Heoed9e1982008-08-25 19:56:05 +0900463 disk_to_dev(disk)->devt = MKDEV(disk->major, disk->first_minor);
Tejun Heof331c022008-09-03 09:01:48 +0200464 blk_register_region(disk_devt(disk), disk->minors, NULL,
465 exact_match, exact_lock, disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 register_disk(disk);
467 blk_register_queue(disk);
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700468
469 bdi = &disk->queue->backing_dev_info;
Tejun Heof331c022008-09-03 09:01:48 +0200470 bdi_register_dev(bdi, disk_devt(disk));
Tejun Heoed9e1982008-08-25 19:56:05 +0900471 retval = sysfs_create_link(&disk_to_dev(disk)->kobj, &bdi->dev->kobj,
472 "bdi");
Greg Kroah-Hartman6ffeea72008-05-22 17:21:08 -0400473 WARN_ON(retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474}
475
476EXPORT_SYMBOL(add_disk);
477EXPORT_SYMBOL(del_gendisk); /* in partitions/check.c */
478
479void unlink_gendisk(struct gendisk *disk)
480{
Tejun Heoed9e1982008-08-25 19:56:05 +0900481 sysfs_remove_link(&disk_to_dev(disk)->kobj, "bdi");
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700482 bdi_unregister(&disk->queue->backing_dev_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 blk_unregister_queue(disk);
Tejun Heof331c022008-09-03 09:01:48 +0200484 blk_unregister_region(disk_devt(disk), disk->minors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485}
486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487/**
488 * get_gendisk - get partitioning information for a given device
Randy Dunlap710027a2008-08-19 20:13:11 +0200489 * @devt: device to get partitioning information for
490 * @part: returned partition index
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 *
492 * This function gets the structure containing partitioning
Randy Dunlap710027a2008-08-19 20:13:11 +0200493 * information for the given device @devt.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 */
Tejun Heocf771cb2008-09-03 09:01:09 +0200495struct gendisk *get_gendisk(dev_t devt, int *partno)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
Tejun Heobcce3de2008-08-25 19:47:22 +0900497 struct gendisk *disk = NULL;
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200498
Tejun Heobcce3de2008-08-25 19:47:22 +0900499 if (MAJOR(devt) != BLOCK_EXT_MAJOR) {
500 struct kobject *kobj;
501
502 kobj = kobj_lookup(bdev_map, devt, partno);
503 if (kobj)
504 disk = dev_to_disk(kobj_to_dev(kobj));
505 } else {
506 struct hd_struct *part;
507
508 mutex_lock(&ext_devt_mutex);
Tejun Heo870d6652008-08-25 19:47:25 +0900509 part = idr_find(&ext_devt_idr, blk_mangle_minor(MINOR(devt)));
Tejun Heobcce3de2008-08-25 19:47:22 +0900510 if (part && get_disk(part_to_disk(part))) {
511 *partno = part->partno;
512 disk = part_to_disk(part);
513 }
514 mutex_unlock(&ext_devt_mutex);
515 }
516
517 return disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518}
519
Tejun Heof331c022008-09-03 09:01:48 +0200520/**
521 * bdget_disk - do bdget() by gendisk and partition number
522 * @disk: gendisk of interest
523 * @partno: partition number
524 *
525 * Find partition @partno from @disk, do bdget() on it.
526 *
527 * CONTEXT:
528 * Don't care.
529 *
530 * RETURNS:
531 * Resulting block_device on success, NULL on failure.
532 */
533extern struct block_device *bdget_disk(struct gendisk *disk, int partno)
534{
535 dev_t devt = MKDEV(0, 0);
536
537 if (partno == 0)
538 devt = disk_devt(disk);
539 else {
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200540 struct hd_struct *part;
Tejun Heof331c022008-09-03 09:01:48 +0200541
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200542 part = disk_get_part(disk, partno);
Tejun Heof331c022008-09-03 09:01:48 +0200543 if (part && part->nr_sects)
544 devt = part_devt(part);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200545 disk_put_part(part);
Tejun Heof331c022008-09-03 09:01:48 +0200546 }
547
548 if (likely(devt != MKDEV(0, 0)))
549 return bdget(devt);
550 return NULL;
551}
552EXPORT_SYMBOL(bdget_disk);
553
Dave Gilbertdd2a3452007-05-09 02:33:24 -0700554/*
555 * print a full list of all partitions - intended for places where the root
556 * filesystem can't be mounted and thus to give the victim some idea of what
557 * went wrong
558 */
559void __init printk_all_partitions(void)
560{
Tejun Heodef4e382008-09-03 08:57:12 +0200561 struct class_dev_iter iter;
562 struct device *dev;
563
564 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
565 while ((dev = class_dev_iter_next(&iter))) {
566 struct gendisk *disk = dev_to_disk(dev);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200567 struct disk_part_iter piter;
568 struct hd_struct *part;
Tejun Heo1f014292008-08-25 19:47:23 +0900569 char name_buf[BDEVNAME_SIZE];
570 char devt_buf[BDEVT_SIZE];
Tejun Heodef4e382008-09-03 08:57:12 +0200571
572 /*
573 * Don't show empty devices or things that have been
574 * surpressed
575 */
576 if (get_capacity(disk) == 0 ||
577 (disk->flags & GENHD_FL_SUPPRESS_PARTITION_INFO))
578 continue;
579
580 /*
581 * Note, unlike /proc/partitions, I am showing the
582 * numbers in hex - the same format as the root=
583 * option takes.
584 */
Tejun Heo1f014292008-08-25 19:47:23 +0900585 printk("%s %10llu %s",
586 bdevt_str(disk_devt(disk), devt_buf),
Tejun Heodef4e382008-09-03 08:57:12 +0200587 (unsigned long long)get_capacity(disk) >> 1,
Tejun Heo1f014292008-08-25 19:47:23 +0900588 disk_name(disk, 0, name_buf));
Tejun Heodef4e382008-09-03 08:57:12 +0200589 if (disk->driverfs_dev != NULL &&
590 disk->driverfs_dev->driver != NULL)
591 printk(" driver: %s\n",
592 disk->driverfs_dev->driver->name);
593 else
594 printk(" (driver?)\n");
595
596 /* now show the partitions */
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200597 disk_part_iter_init(&piter, disk, 0);
598 while ((part = disk_part_iter_next(&piter)))
Tejun Heo1f014292008-08-25 19:47:23 +0900599 printk(" %s %10llu %s\n",
600 bdevt_str(part_devt(part), devt_buf),
Tejun Heof331c022008-09-03 09:01:48 +0200601 (unsigned long long)part->nr_sects >> 1,
Tejun Heo1f014292008-08-25 19:47:23 +0900602 disk_name(disk, part->partno, name_buf));
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200603 disk_part_iter_exit(&piter);
Tejun Heodef4e382008-09-03 08:57:12 +0200604 }
605 class_dev_iter_exit(&iter);
Dave Gilbertdd2a3452007-05-09 02:33:24 -0700606}
607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608#ifdef CONFIG_PROC_FS
609/* iterator */
Tejun Heodef4e382008-09-03 08:57:12 +0200610static void *disk_seqf_start(struct seq_file *seqf, loff_t *pos)
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -0400611{
Tejun Heodef4e382008-09-03 08:57:12 +0200612 loff_t skip = *pos;
613 struct class_dev_iter *iter;
614 struct device *dev;
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -0400615
Tejun Heodef4e382008-09-03 08:57:12 +0200616 iter = kmalloc(GFP_KERNEL, sizeof(*iter));
617 if (!iter)
618 return ERR_PTR(-ENOMEM);
619
620 seqf->private = iter;
621 class_dev_iter_init(iter, &block_class, NULL, &disk_type);
622 do {
623 dev = class_dev_iter_next(iter);
624 if (!dev)
625 return NULL;
626 } while (skip--);
627
628 return dev_to_disk(dev);
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -0400629}
630
Tejun Heodef4e382008-09-03 08:57:12 +0200631static void *disk_seqf_next(struct seq_file *seqf, void *v, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200633 struct device *dev;
Greg Kroah-Hartman66c64af2008-05-22 17:21:08 -0400634
Tejun Heodef4e382008-09-03 08:57:12 +0200635 (*pos)++;
636 dev = class_dev_iter_next(seqf->private);
Tejun Heo2ac3cee2008-09-03 08:53:37 +0200637 if (dev)
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -0400638 return dev_to_disk(dev);
Tejun Heo2ac3cee2008-09-03 08:53:37 +0200639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 return NULL;
641}
642
Tejun Heodef4e382008-09-03 08:57:12 +0200643static void disk_seqf_stop(struct seq_file *seqf, void *v)
Greg Kroah-Hartman27f30252008-05-22 17:21:08 -0400644{
Tejun Heodef4e382008-09-03 08:57:12 +0200645 struct class_dev_iter *iter = seqf->private;
Greg Kroah-Hartman27f30252008-05-22 17:21:08 -0400646
Tejun Heodef4e382008-09-03 08:57:12 +0200647 /* stop is called even after start failed :-( */
648 if (iter) {
649 class_dev_iter_exit(iter);
650 kfree(iter);
Kay Sievers5c0ef6d2008-08-16 14:30:30 +0200651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652}
653
Tejun Heodef4e382008-09-03 08:57:12 +0200654static void *show_partition_start(struct seq_file *seqf, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
Tejun Heodef4e382008-09-03 08:57:12 +0200656 static void *p;
657
658 p = disk_seqf_start(seqf, pos);
659 if (!IS_ERR(p) && p)
660 seq_puts(seqf, "major minor #blocks name\n\n");
661 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662}
663
Tejun Heocf771cb2008-09-03 09:01:09 +0200664static int show_partition(struct seq_file *seqf, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
666 struct gendisk *sgp = v;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200667 struct disk_part_iter piter;
668 struct hd_struct *part;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 char buf[BDEVNAME_SIZE];
670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 /* Don't show non-partitionable removeable devices or empty devices */
Tejun Heof331c022008-09-03 09:01:48 +0200672 if (!get_capacity(sgp) || (!disk_max_parts(sgp) &&
673 (sgp->flags & GENHD_FL_REMOVABLE)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 return 0;
675 if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)
676 return 0;
677
678 /* show the full disk and all non-0 size partitions of it */
Tejun Heo1f014292008-08-25 19:47:23 +0900679 seq_printf(seqf, "%4d %7d %10llu %s\n",
Tejun Heof331c022008-09-03 09:01:48 +0200680 MAJOR(disk_devt(sgp)), MINOR(disk_devt(sgp)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 (unsigned long long)get_capacity(sgp) >> 1,
682 disk_name(sgp, 0, buf));
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200683
684 disk_part_iter_init(&piter, sgp, 0);
685 while ((part = disk_part_iter_next(&piter)))
Tejun Heo1f014292008-08-25 19:47:23 +0900686 seq_printf(seqf, "%4d %7d %10llu %s\n",
Tejun Heof331c022008-09-03 09:01:48 +0200687 MAJOR(part_devt(part)), MINOR(part_devt(part)),
688 (unsigned long long)part->nr_sects >> 1,
689 disk_name(sgp, part->partno, buf));
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200690 disk_part_iter_exit(&piter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692 return 0;
693}
694
Jan Engelhardt12f32bb2008-01-29 20:57:51 +0100695const struct seq_operations partitions_op = {
Tejun Heodef4e382008-09-03 08:57:12 +0200696 .start = show_partition_start,
697 .next = disk_seqf_next,
698 .stop = disk_seqf_stop,
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200699 .show = show_partition
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700};
701#endif
702
703
Tejun Heocf771cb2008-09-03 09:01:09 +0200704static struct kobject *base_probe(dev_t devt, int *partno, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200706 if (request_module("block-major-%d-%d", MAJOR(devt), MINOR(devt)) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 /* Make old-style 2.4 aliases work */
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200708 request_module("block-major-%d", MAJOR(devt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 return NULL;
710}
711
712static int __init genhd_device_init(void)
713{
Dan Williamse105b8b2008-04-21 10:51:07 -0700714 int error;
715
716 block_class.dev_kobj = sysfs_dev_block_kobj;
717 error = class_register(&block_class);
Roland McGrathee27a552008-03-11 17:13:15 -0700718 if (unlikely(error))
719 return error;
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200720 bdev_map = kobj_map_init(base_probe, &block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 blk_dev_init();
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200722
723#ifndef CONFIG_SYSFS_DEPRECATED
724 /* create top-level block dir */
725 block_depr = kobject_create_and_add("block", NULL);
726#endif
Greg Kroah-Hartman830d3cf2007-11-06 10:36:58 -0800727 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728}
729
730subsys_initcall(genhd_device_init);
731
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200732static ssize_t disk_range_show(struct device *dev,
733 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200735 struct gendisk *disk = dev_to_disk(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200737 return sprintf(buf, "%d\n", disk->minors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738}
739
Tejun Heo1f014292008-08-25 19:47:23 +0900740static ssize_t disk_ext_range_show(struct device *dev,
741 struct device_attribute *attr, char *buf)
742{
743 struct gendisk *disk = dev_to_disk(dev);
744
745 return sprintf(buf, "%d\n", disk_max_parts(disk) + 1);
746}
747
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200748static ssize_t disk_removable_show(struct device *dev,
749 struct device_attribute *attr, char *buf)
Kay Sieversa7fd6702005-10-01 14:49:43 +0200750{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200751 struct gendisk *disk = dev_to_disk(dev);
Kay Sieversa7fd6702005-10-01 14:49:43 +0200752
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200753 return sprintf(buf, "%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200755}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Kay Sievers1c9ce522008-06-13 09:41:00 +0200757static ssize_t disk_ro_show(struct device *dev,
758 struct device_attribute *attr, char *buf)
759{
760 struct gendisk *disk = dev_to_disk(dev);
761
762 return sprintf(buf, "%d\n", disk->policy ? 1 : 0);
763}
764
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200765static ssize_t disk_size_show(struct device *dev,
766 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200768 struct gendisk *disk = dev_to_disk(dev);
769
770 return sprintf(buf, "%llu\n", (unsigned long long)get_capacity(disk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771}
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200772
773static ssize_t disk_capability_show(struct device *dev,
774 struct device_attribute *attr, char *buf)
Kristen Carlson Accardi86ce18d2007-05-23 13:57:38 -0700775{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200776 struct gendisk *disk = dev_to_disk(dev);
777
778 return sprintf(buf, "%x\n", disk->flags);
Kristen Carlson Accardi86ce18d2007-05-23 13:57:38 -0700779}
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200780
781static ssize_t disk_stat_show(struct device *dev,
782 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200784 struct gendisk *disk = dev_to_disk(dev);
Tejun Heoc9959052008-08-25 19:47:21 +0900785 int cpu;
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200786
Tejun Heoc9959052008-08-25 19:47:21 +0900787 cpu = disk_stat_lock();
788 disk_round_stats(cpu, disk);
789 disk_stat_unlock();
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200790 return sprintf(buf,
Ben Woodard837c7872006-03-22 08:09:31 +0100791 "%8lu %8lu %8llu %8u "
792 "%8lu %8lu %8llu %8u "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 "%8u %8u %8u"
794 "\n",
Jens Axboe47a00412005-11-09 13:38:47 +0100795 disk_stat_read(disk, ios[READ]),
796 disk_stat_read(disk, merges[READ]),
797 (unsigned long long)disk_stat_read(disk, sectors[READ]),
798 jiffies_to_msecs(disk_stat_read(disk, ticks[READ])),
799 disk_stat_read(disk, ios[WRITE]),
800 disk_stat_read(disk, merges[WRITE]),
801 (unsigned long long)disk_stat_read(disk, sectors[WRITE]),
802 jiffies_to_msecs(disk_stat_read(disk, ticks[WRITE])),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 disk->in_flight,
804 jiffies_to_msecs(disk_stat_read(disk, io_ticks)),
805 jiffies_to_msecs(disk_stat_read(disk, time_in_queue)));
806}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Akinobu Mitac17bb492006-12-08 02:39:46 -0800808#ifdef CONFIG_FAIL_MAKE_REQUEST
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200809static ssize_t disk_fail_show(struct device *dev,
810 struct device_attribute *attr, char *buf)
811{
812 struct gendisk *disk = dev_to_disk(dev);
Akinobu Mitac17bb492006-12-08 02:39:46 -0800813
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200814 return sprintf(buf, "%d\n", disk->flags & GENHD_FL_FAIL ? 1 : 0);
815}
816
817static ssize_t disk_fail_store(struct device *dev,
818 struct device_attribute *attr,
Akinobu Mitac17bb492006-12-08 02:39:46 -0800819 const char *buf, size_t count)
820{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200821 struct gendisk *disk = dev_to_disk(dev);
Akinobu Mitac17bb492006-12-08 02:39:46 -0800822 int i;
823
824 if (count > 0 && sscanf(buf, "%d", &i) > 0) {
825 if (i == 0)
826 disk->flags &= ~GENHD_FL_FAIL;
827 else
828 disk->flags |= GENHD_FL_FAIL;
829 }
830
831 return count;
832}
Akinobu Mitac17bb492006-12-08 02:39:46 -0800833
834#endif
835
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200836static DEVICE_ATTR(range, S_IRUGO, disk_range_show, NULL);
Tejun Heo1f014292008-08-25 19:47:23 +0900837static DEVICE_ATTR(ext_range, S_IRUGO, disk_ext_range_show, NULL);
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200838static DEVICE_ATTR(removable, S_IRUGO, disk_removable_show, NULL);
Kay Sievers1c9ce522008-06-13 09:41:00 +0200839static DEVICE_ATTR(ro, S_IRUGO, disk_ro_show, NULL);
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200840static DEVICE_ATTR(size, S_IRUGO, disk_size_show, NULL);
841static DEVICE_ATTR(capability, S_IRUGO, disk_capability_show, NULL);
842static DEVICE_ATTR(stat, S_IRUGO, disk_stat_show, NULL);
Akinobu Mitac17bb492006-12-08 02:39:46 -0800843#ifdef CONFIG_FAIL_MAKE_REQUEST
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200844static struct device_attribute dev_attr_fail =
845 __ATTR(make-it-fail, S_IRUGO|S_IWUSR, disk_fail_show, disk_fail_store);
Akinobu Mitac17bb492006-12-08 02:39:46 -0800846#endif
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200847
848static struct attribute *disk_attrs[] = {
849 &dev_attr_range.attr,
Tejun Heo1f014292008-08-25 19:47:23 +0900850 &dev_attr_ext_range.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200851 &dev_attr_removable.attr,
Kay Sievers1c9ce522008-06-13 09:41:00 +0200852 &dev_attr_ro.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200853 &dev_attr_size.attr,
854 &dev_attr_capability.attr,
855 &dev_attr_stat.attr,
856#ifdef CONFIG_FAIL_MAKE_REQUEST
857 &dev_attr_fail.attr,
858#endif
859 NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860};
861
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200862static struct attribute_group disk_attr_group = {
863 .attrs = disk_attrs,
864};
865
866static struct attribute_group *disk_attr_groups[] = {
867 &disk_attr_group,
868 NULL
869};
870
871static void disk_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200873 struct gendisk *disk = dev_to_disk(dev);
874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 kfree(disk->random);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200876 kfree(disk->__part);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 free_disk_stats(disk);
878 kfree(disk);
879}
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200880struct class block_class = {
881 .name = "block",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882};
883
Adrian Bunk1826ead2008-03-04 11:23:46 +0100884static struct device_type disk_type = {
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200885 .name = "disk",
886 .groups = disk_attr_groups,
887 .release = disk_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888};
889
Randy Dunlapa6e2ba82008-05-23 09:44:11 -0700890#ifdef CONFIG_PROC_FS
Tejun Heocf771cb2008-09-03 09:01:09 +0200891/*
892 * aggregate disk stat collector. Uses the same stats that the sysfs
893 * entries do, above, but makes them available through one seq_file.
894 *
895 * The output looks suspiciously like /proc/partitions with a bunch of
896 * extra fields.
897 */
898static int diskstats_show(struct seq_file *seqf, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899{
900 struct gendisk *gp = v;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200901 struct disk_part_iter piter;
902 struct hd_struct *hd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 char buf[BDEVNAME_SIZE];
Tejun Heoc9959052008-08-25 19:47:21 +0900904 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
906 /*
Tejun Heoed9e1982008-08-25 19:56:05 +0900907 if (&disk_to_dev(gp)->kobj.entry == block_class.devices.next)
Tejun Heocf771cb2008-09-03 09:01:09 +0200908 seq_puts(seqf, "major minor name"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 " rio rmerge rsect ruse wio wmerge "
910 "wsect wuse running use aveq"
911 "\n\n");
912 */
913
Tejun Heoc9959052008-08-25 19:47:21 +0900914 cpu = disk_stat_lock();
915 disk_round_stats(cpu, gp);
916 disk_stat_unlock();
Tejun Heo1f014292008-08-25 19:47:23 +0900917 seq_printf(seqf, "%4d %7d %s %lu %lu %llu %u %lu %lu %llu %u %u %u %u\n",
Tejun Heof331c022008-09-03 09:01:48 +0200918 MAJOR(disk_devt(gp)), MINOR(disk_devt(gp)),
919 disk_name(gp, 0, buf),
Jens Axboea3623572005-11-01 09:26:16 +0100920 disk_stat_read(gp, ios[0]), disk_stat_read(gp, merges[0]),
921 (unsigned long long)disk_stat_read(gp, sectors[0]),
922 jiffies_to_msecs(disk_stat_read(gp, ticks[0])),
923 disk_stat_read(gp, ios[1]), disk_stat_read(gp, merges[1]),
924 (unsigned long long)disk_stat_read(gp, sectors[1]),
925 jiffies_to_msecs(disk_stat_read(gp, ticks[1])),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 gp->in_flight,
927 jiffies_to_msecs(disk_stat_read(gp, io_ticks)),
928 jiffies_to_msecs(disk_stat_read(gp, time_in_queue)));
929
930 /* now show all non-0 size partitions of it */
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200931 disk_part_iter_init(&piter, gp, 0);
932 while ((hd = disk_part_iter_next(&piter))) {
Tejun Heoc9959052008-08-25 19:47:21 +0900933 cpu = disk_stat_lock();
934 part_round_stats(cpu, hd);
935 disk_stat_unlock();
Tejun Heo1f014292008-08-25 19:47:23 +0900936 seq_printf(seqf, "%4d %7d %s %lu %lu %llu "
Jerome Marchand28f39d52008-02-08 11:04:56 +0100937 "%u %lu %lu %llu %u %u %u %u\n",
Tejun Heof331c022008-09-03 09:01:48 +0200938 MAJOR(part_devt(hd)), MINOR(part_devt(hd)),
939 disk_name(gp, hd->partno, buf),
Jerome Marchand28f39d52008-02-08 11:04:56 +0100940 part_stat_read(hd, ios[0]),
941 part_stat_read(hd, merges[0]),
942 (unsigned long long)part_stat_read(hd, sectors[0]),
943 jiffies_to_msecs(part_stat_read(hd, ticks[0])),
944 part_stat_read(hd, ios[1]),
945 part_stat_read(hd, merges[1]),
946 (unsigned long long)part_stat_read(hd, sectors[1]),
947 jiffies_to_msecs(part_stat_read(hd, ticks[1])),
948 hd->in_flight,
949 jiffies_to_msecs(part_stat_read(hd, io_ticks)),
950 jiffies_to_msecs(part_stat_read(hd, time_in_queue))
951 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 }
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200953 disk_part_iter_exit(&piter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
955 return 0;
956}
957
Jan Engelhardt12f32bb2008-01-29 20:57:51 +0100958const struct seq_operations diskstats_op = {
Tejun Heodef4e382008-09-03 08:57:12 +0200959 .start = disk_seqf_start,
960 .next = disk_seqf_next,
961 .stop = disk_seqf_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 .show = diskstats_show
963};
Randy Dunlapa6e2ba82008-05-23 09:44:11 -0700964#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Kristen Carlson Accardi8ce7ad72007-05-23 13:57:38 -0700966static void media_change_notify_thread(struct work_struct *work)
967{
968 struct gendisk *gd = container_of(work, struct gendisk, async_notify);
969 char event[] = "MEDIA_CHANGE=1";
970 char *envp[] = { event, NULL };
971
972 /*
973 * set enviroment vars to indicate which event this is for
974 * so that user space will know to go check the media status.
975 */
Tejun Heoed9e1982008-08-25 19:56:05 +0900976 kobject_uevent_env(&disk_to_dev(gd)->kobj, KOBJ_CHANGE, envp);
Kristen Carlson Accardi8ce7ad72007-05-23 13:57:38 -0700977 put_device(gd->driverfs_dev);
978}
979
Adrian Bunk1826ead2008-03-04 11:23:46 +0100980#if 0
Kristen Carlson Accardi8ce7ad72007-05-23 13:57:38 -0700981void genhd_media_change_notify(struct gendisk *disk)
982{
983 get_device(disk->driverfs_dev);
984 schedule_work(&disk->async_notify);
985}
986EXPORT_SYMBOL_GPL(genhd_media_change_notify);
Adrian Bunk1826ead2008-03-04 11:23:46 +0100987#endif /* 0 */
Kristen Carlson Accardi8ce7ad72007-05-23 13:57:38 -0700988
Tejun Heocf771cb2008-09-03 09:01:09 +0200989dev_t blk_lookup_devt(const char *name, int partno)
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200990{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200991 dev_t devt = MKDEV(0, 0);
Tejun Heodef4e382008-09-03 08:57:12 +0200992 struct class_dev_iter iter;
993 struct device *dev;
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200994
Tejun Heodef4e382008-09-03 08:57:12 +0200995 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
996 while ((dev = class_dev_iter_next(&iter))) {
997 struct gendisk *disk = dev_to_disk(dev);
998
Tejun Heof331c022008-09-03 09:01:48 +0200999 if (strcmp(dev->bus_id, name))
1000 continue;
1001 if (partno < 0 || partno > disk_max_parts(disk))
1002 continue;
1003
1004 if (partno == 0)
1005 devt = disk_devt(disk);
1006 else {
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001007 struct hd_struct *part;
Tejun Heof331c022008-09-03 09:01:48 +02001008
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001009 part = disk_get_part(disk, partno);
1010 if (!part || !part->nr_sects) {
1011 disk_put_part(part);
Tejun Heof331c022008-09-03 09:01:48 +02001012 continue;
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001013 }
Tejun Heof331c022008-09-03 09:01:48 +02001014
1015 devt = part_devt(part);
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001016 disk_put_part(part);
Tejun Heodef4e382008-09-03 08:57:12 +02001017 }
Tejun Heof331c022008-09-03 09:01:48 +02001018 break;
Kay Sievers5c0ef6d2008-08-16 14:30:30 +02001019 }
Tejun Heodef4e382008-09-03 08:57:12 +02001020 class_dev_iter_exit(&iter);
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001021 return devt;
1022}
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001023EXPORT_SYMBOL(blk_lookup_devt);
1024
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025struct gendisk *alloc_disk(int minors)
1026{
Christoph Lameter19460892005-06-23 00:08:19 -07001027 return alloc_disk_node(minors, -1);
1028}
1029
1030struct gendisk *alloc_disk_node(int minors, int node_id)
1031{
Tejun Heobcce3de2008-08-25 19:47:22 +09001032 return alloc_disk_ext_node(minors, 0, node_id);
1033}
1034
1035struct gendisk *alloc_disk_ext(int minors, int ext_minors)
1036{
1037 return alloc_disk_ext_node(minors, ext_minors, -1);
1038}
1039
1040struct gendisk *alloc_disk_ext_node(int minors, int ext_minors, int node_id)
1041{
Christoph Lameter19460892005-06-23 00:08:19 -07001042 struct gendisk *disk;
1043
Christoph Lameter94f60302007-07-17 04:03:29 -07001044 disk = kmalloc_node(sizeof(struct gendisk),
1045 GFP_KERNEL | __GFP_ZERO, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 if (disk) {
Tejun Heobcce3de2008-08-25 19:47:22 +09001047 int tot_minors = minors + ext_minors;
1048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 if (!init_disk_stats(disk)) {
1050 kfree(disk);
1051 return NULL;
1052 }
Tejun Heobcce3de2008-08-25 19:47:22 +09001053 if (tot_minors > 1) {
1054 int size = (tot_minors - 1) * sizeof(struct hd_struct *);
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001055 disk->__part = kmalloc_node(size,
Christoph Lameter94f60302007-07-17 04:03:29 -07001056 GFP_KERNEL | __GFP_ZERO, node_id);
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001057 if (!disk->__part) {
Jerome Marchandc7674032007-11-23 09:17:53 +01001058 free_disk_stats(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 kfree(disk);
1060 return NULL;
1061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 }
1063 disk->minors = minors;
Tejun Heobcce3de2008-08-25 19:47:22 +09001064 disk->ext_minors = ext_minors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 rand_initialize_disk(disk);
Tejun Heoed9e1982008-08-25 19:56:05 +09001066 disk_to_dev(disk)->class = &block_class;
1067 disk_to_dev(disk)->type = &disk_type;
1068 device_initialize(disk_to_dev(disk));
Kristen Carlson Accardi8ce7ad72007-05-23 13:57:38 -07001069 INIT_WORK(&disk->async_notify,
1070 media_change_notify_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 }
1072 return disk;
1073}
1074
1075EXPORT_SYMBOL(alloc_disk);
Christoph Lameter19460892005-06-23 00:08:19 -07001076EXPORT_SYMBOL(alloc_disk_node);
Tejun Heobcce3de2008-08-25 19:47:22 +09001077EXPORT_SYMBOL(alloc_disk_ext);
1078EXPORT_SYMBOL(alloc_disk_ext_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
1080struct kobject *get_disk(struct gendisk *disk)
1081{
1082 struct module *owner;
1083 struct kobject *kobj;
1084
1085 if (!disk->fops)
1086 return NULL;
1087 owner = disk->fops->owner;
1088 if (owner && !try_module_get(owner))
1089 return NULL;
Tejun Heoed9e1982008-08-25 19:56:05 +09001090 kobj = kobject_get(&disk_to_dev(disk)->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 if (kobj == NULL) {
1092 module_put(owner);
1093 return NULL;
1094 }
1095 return kobj;
1096
1097}
1098
1099EXPORT_SYMBOL(get_disk);
1100
1101void put_disk(struct gendisk *disk)
1102{
1103 if (disk)
Tejun Heoed9e1982008-08-25 19:56:05 +09001104 kobject_put(&disk_to_dev(disk)->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105}
1106
1107EXPORT_SYMBOL(put_disk);
1108
1109void set_device_ro(struct block_device *bdev, int flag)
1110{
1111 if (bdev->bd_contains != bdev)
1112 bdev->bd_part->policy = flag;
1113 else
1114 bdev->bd_disk->policy = flag;
1115}
1116
1117EXPORT_SYMBOL(set_device_ro);
1118
1119void set_disk_ro(struct gendisk *disk, int flag)
1120{
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001121 struct disk_part_iter piter;
1122 struct hd_struct *part;
1123
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 disk->policy = flag;
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001125 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_EMPTY);
1126 while ((part = disk_part_iter_next(&piter)))
1127 part->policy = flag;
1128 disk_part_iter_exit(&piter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129}
1130
1131EXPORT_SYMBOL(set_disk_ro);
1132
1133int bdev_read_only(struct block_device *bdev)
1134{
1135 if (!bdev)
1136 return 0;
1137 else if (bdev->bd_contains != bdev)
1138 return bdev->bd_part->policy;
1139 else
1140 return bdev->bd_disk->policy;
1141}
1142
1143EXPORT_SYMBOL(bdev_read_only);
1144
Tejun Heocf771cb2008-09-03 09:01:09 +02001145int invalidate_partition(struct gendisk *disk, int partno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146{
1147 int res = 0;
Tejun Heocf771cb2008-09-03 09:01:09 +02001148 struct block_device *bdev = bdget_disk(disk, partno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 if (bdev) {
Christoph Hellwig2ef41632005-05-05 16:15:59 -07001150 fsync_bdev(bdev);
1151 res = __invalidate_device(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 bdput(bdev);
1153 }
1154 return res;
1155}
1156
1157EXPORT_SYMBOL(invalidate_partition);