blob: 65b7386c26d8b561402e0d4dbd97597a3cf736a7 [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
Tejun Heob5d0b9d2008-09-03 09:06:42 +020057 if (unlikely(partno < 0 || partno >= disk_max_parts(disk)))
Tejun Heoe71bf0d2008-09-03 09:03:02 +020058 return NULL;
59 rcu_read_lock();
Tejun Heob5d0b9d2008-09-03 09:06:42 +020060 part = rcu_dereference(disk->__part[partno]);
Tejun Heoe71bf0d2008-09-03 09:03:02 +020061 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;
Tejun Heob5d0b9d2008-09-03 09:06:42 +020088 else if (flags & DISK_PITER_INCL_PART0)
Tejun Heoe71bf0d2008-09-03 09:03:02 +020089 piter->idx = 0;
Tejun Heob5d0b9d2008-09-03 09:06:42 +020090 else
91 piter->idx = 1;
Tejun Heoe71bf0d2008-09-03 09:03:02 +020092
93 piter->flags = flags;
94}
95EXPORT_SYMBOL_GPL(disk_part_iter_init);
96
97/**
98 * disk_part_iter_next - proceed iterator to the next partition and return it
99 * @piter: iterator of interest
100 *
101 * Proceed @piter to the next partition and return it.
102 *
103 * CONTEXT:
104 * Don't care.
105 */
106struct hd_struct *disk_part_iter_next(struct disk_part_iter *piter)
107{
108 int inc, end;
109
110 /* put the last partition */
111 disk_put_part(piter->part);
112 piter->part = NULL;
113
114 rcu_read_lock();
115
116 /* determine iteration parameters */
117 if (piter->flags & DISK_PITER_REVERSE) {
118 inc = -1;
Tejun Heob5d0b9d2008-09-03 09:06:42 +0200119 if (piter->flags & DISK_PITER_INCL_PART0)
120 end = -1;
121 else
122 end = 0;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200123 } else {
124 inc = 1;
125 end = disk_max_parts(piter->disk);
126 }
127
128 /* iterate to the next partition */
129 for (; piter->idx != end; piter->idx += inc) {
130 struct hd_struct *part;
131
132 part = rcu_dereference(piter->disk->__part[piter->idx]);
133 if (!part)
134 continue;
135 if (!(piter->flags & DISK_PITER_INCL_EMPTY) && !part->nr_sects)
136 continue;
137
Tejun Heoed9e1982008-08-25 19:56:05 +0900138 get_device(part_to_dev(part));
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200139 piter->part = part;
140 piter->idx += inc;
141 break;
142 }
143
144 rcu_read_unlock();
145
146 return piter->part;
147}
148EXPORT_SYMBOL_GPL(disk_part_iter_next);
149
150/**
151 * disk_part_iter_exit - finish up partition iteration
152 * @piter: iter of interest
153 *
154 * Called when iteration is over. Cleans up @piter.
155 *
156 * CONTEXT:
157 * Don't care.
158 */
159void disk_part_iter_exit(struct disk_part_iter *piter)
160{
161 disk_put_part(piter->part);
162 piter->part = NULL;
163}
164EXPORT_SYMBOL_GPL(disk_part_iter_exit);
165
166/**
167 * disk_map_sector_rcu - map sector to partition
168 * @disk: gendisk of interest
169 * @sector: sector to map
170 *
171 * Find out which partition @sector maps to on @disk. This is
172 * primarily used for stats accounting.
173 *
174 * CONTEXT:
175 * RCU read locked. The returned partition pointer is valid only
176 * while preemption is disabled.
177 *
178 * RETURNS:
179 * Found partition on success, NULL if there's no matching partition.
180 */
181struct hd_struct *disk_map_sector_rcu(struct gendisk *disk, sector_t sector)
182{
183 int i;
184
Tejun Heob5d0b9d2008-09-03 09:06:42 +0200185 for (i = 1; i < disk_max_parts(disk); i++) {
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200186 struct hd_struct *part = rcu_dereference(disk->__part[i]);
187
188 if (part && part->start_sect <= sector &&
189 sector < part->start_sect + part->nr_sects)
190 return part;
191 }
192 return NULL;
193}
194EXPORT_SYMBOL_GPL(disk_map_sector_rcu);
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196/*
197 * Can be deleted altogether. Later.
198 *
199 */
200static struct blk_major_name {
201 struct blk_major_name *next;
202 int major;
203 char name[16];
Joe Korty68eef3b2006-03-31 02:30:32 -0800204} *major_names[BLKDEV_MAJOR_HASH_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206/* index in the above - for now: assume no multimajor ranges */
207static inline int major_to_index(int major)
208{
Joe Korty68eef3b2006-03-31 02:30:32 -0800209 return major % BLKDEV_MAJOR_HASH_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
211
Joe Korty68eef3b2006-03-31 02:30:32 -0800212#ifdef CONFIG_PROC_FS
Tejun Heocf771cb2008-09-03 09:01:09 +0200213void blkdev_show(struct seq_file *seqf, off_t offset)
Neil Horman7170be52006-01-14 13:20:38 -0800214{
Joe Korty68eef3b2006-03-31 02:30:32 -0800215 struct blk_major_name *dp;
Neil Horman7170be52006-01-14 13:20:38 -0800216
Joe Korty68eef3b2006-03-31 02:30:32 -0800217 if (offset < BLKDEV_MAJOR_HASH_SIZE) {
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200218 mutex_lock(&block_class_lock);
Joe Korty68eef3b2006-03-31 02:30:32 -0800219 for (dp = major_names[offset]; dp; dp = dp->next)
Tejun Heocf771cb2008-09-03 09:01:09 +0200220 seq_printf(seqf, "%3d %s\n", dp->major, dp->name);
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200221 mutex_unlock(&block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
Joe Korty68eef3b2006-03-31 02:30:32 -0800224#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226int register_blkdev(unsigned int major, const char *name)
227{
228 struct blk_major_name **n, *p;
229 int index, ret = 0;
230
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200231 mutex_lock(&block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 /* temporary */
234 if (major == 0) {
235 for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) {
236 if (major_names[index] == NULL)
237 break;
238 }
239
240 if (index == 0) {
241 printk("register_blkdev: failed to get major for %s\n",
242 name);
243 ret = -EBUSY;
244 goto out;
245 }
246 major = index;
247 ret = major;
248 }
249
250 p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL);
251 if (p == NULL) {
252 ret = -ENOMEM;
253 goto out;
254 }
255
256 p->major = major;
257 strlcpy(p->name, name, sizeof(p->name));
258 p->next = NULL;
259 index = major_to_index(major);
260
261 for (n = &major_names[index]; *n; n = &(*n)->next) {
262 if ((*n)->major == major)
263 break;
264 }
265 if (!*n)
266 *n = p;
267 else
268 ret = -EBUSY;
269
270 if (ret < 0) {
271 printk("register_blkdev: cannot get major %d for %s\n",
272 major, name);
273 kfree(p);
274 }
275out:
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200276 mutex_unlock(&block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 return ret;
278}
279
280EXPORT_SYMBOL(register_blkdev);
281
Akinobu Mitaf4480242007-07-17 04:03:47 -0700282void unregister_blkdev(unsigned int major, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
284 struct blk_major_name **n;
285 struct blk_major_name *p = NULL;
286 int index = major_to_index(major);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200288 mutex_lock(&block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 for (n = &major_names[index]; *n; n = &(*n)->next)
290 if ((*n)->major == major)
291 break;
Akinobu Mita294462a2007-07-17 04:03:45 -0700292 if (!*n || strcmp((*n)->name, name)) {
293 WARN_ON(1);
Akinobu Mita294462a2007-07-17 04:03:45 -0700294 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 p = *n;
296 *n = p->next;
297 }
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200298 mutex_unlock(&block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300}
301
302EXPORT_SYMBOL(unregister_blkdev);
303
304static struct kobj_map *bdev_map;
305
Tejun Heobcce3de2008-08-25 19:47:22 +0900306/**
Tejun Heo870d6652008-08-25 19:47:25 +0900307 * blk_mangle_minor - scatter minor numbers apart
308 * @minor: minor number to mangle
309 *
310 * Scatter consecutively allocated @minor number apart if MANGLE_DEVT
311 * is enabled. Mangling twice gives the original value.
312 *
313 * RETURNS:
314 * Mangled value.
315 *
316 * CONTEXT:
317 * Don't care.
318 */
319static int blk_mangle_minor(int minor)
320{
321#ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT
322 int i;
323
324 for (i = 0; i < MINORBITS / 2; i++) {
325 int low = minor & (1 << i);
326 int high = minor & (1 << (MINORBITS - 1 - i));
327 int distance = MINORBITS - 1 - 2 * i;
328
329 minor ^= low | high; /* clear both bits */
330 low <<= distance; /* swap the positions */
331 high >>= distance;
332 minor |= low | high; /* and set */
333 }
334#endif
335 return minor;
336}
337
338/**
Tejun Heobcce3de2008-08-25 19:47:22 +0900339 * blk_alloc_devt - allocate a dev_t for a partition
340 * @part: partition to allocate dev_t for
341 * @gfp_mask: memory allocation flag
342 * @devt: out parameter for resulting dev_t
343 *
344 * Allocate a dev_t for block device.
345 *
346 * RETURNS:
347 * 0 on success, allocated dev_t is returned in *@devt. -errno on
348 * failure.
349 *
350 * CONTEXT:
351 * Might sleep.
352 */
353int blk_alloc_devt(struct hd_struct *part, dev_t *devt)
354{
355 struct gendisk *disk = part_to_disk(part);
356 int idx, rc;
357
358 /* in consecutive minor range? */
359 if (part->partno < disk->minors) {
360 *devt = MKDEV(disk->major, disk->first_minor + part->partno);
361 return 0;
362 }
363
364 /* allocate ext devt */
365 do {
366 if (!idr_pre_get(&ext_devt_idr, GFP_KERNEL))
367 return -ENOMEM;
368 rc = idr_get_new(&ext_devt_idr, part, &idx);
369 } while (rc == -EAGAIN);
370
371 if (rc)
372 return rc;
373
374 if (idx > MAX_EXT_DEVT) {
375 idr_remove(&ext_devt_idr, idx);
376 return -EBUSY;
377 }
378
Tejun Heo870d6652008-08-25 19:47:25 +0900379 *devt = MKDEV(BLOCK_EXT_MAJOR, blk_mangle_minor(idx));
Tejun Heobcce3de2008-08-25 19:47:22 +0900380 return 0;
381}
382
383/**
384 * blk_free_devt - free a dev_t
385 * @devt: dev_t to free
386 *
387 * Free @devt which was allocated using blk_alloc_devt().
388 *
389 * CONTEXT:
390 * Might sleep.
391 */
392void blk_free_devt(dev_t devt)
393{
394 might_sleep();
395
396 if (devt == MKDEV(0, 0))
397 return;
398
399 if (MAJOR(devt) == BLOCK_EXT_MAJOR) {
400 mutex_lock(&ext_devt_mutex);
Tejun Heo870d6652008-08-25 19:47:25 +0900401 idr_remove(&ext_devt_idr, blk_mangle_minor(MINOR(devt)));
Tejun Heobcce3de2008-08-25 19:47:22 +0900402 mutex_unlock(&ext_devt_mutex);
403 }
404}
405
Tejun Heo1f014292008-08-25 19:47:23 +0900406static char *bdevt_str(dev_t devt, char *buf)
407{
408 if (MAJOR(devt) <= 0xff && MINOR(devt) <= 0xff) {
409 char tbuf[BDEVT_SIZE];
410 snprintf(tbuf, BDEVT_SIZE, "%02x%02x", MAJOR(devt), MINOR(devt));
411 snprintf(buf, BDEVT_SIZE, "%-9s", tbuf);
412 } else
413 snprintf(buf, BDEVT_SIZE, "%03x:%05x", MAJOR(devt), MINOR(devt));
414
415 return buf;
416}
417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418/*
419 * Register device numbers dev..(dev+range-1)
420 * range must be nonzero
421 * The hash chain is sorted on range, so that subranges can override.
422 */
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200423void blk_register_region(dev_t devt, unsigned long range, struct module *module,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 struct kobject *(*probe)(dev_t, int *, void *),
425 int (*lock)(dev_t, void *), void *data)
426{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200427 kobj_map(bdev_map, devt, range, module, probe, lock, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428}
429
430EXPORT_SYMBOL(blk_register_region);
431
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200432void blk_unregister_region(dev_t devt, unsigned long range)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200434 kobj_unmap(bdev_map, devt, range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435}
436
437EXPORT_SYMBOL(blk_unregister_region);
438
Tejun Heocf771cb2008-09-03 09:01:09 +0200439static struct kobject *exact_match(dev_t devt, int *partno, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
441 struct gendisk *p = data;
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200442
Tejun Heoed9e1982008-08-25 19:56:05 +0900443 return &disk_to_dev(p)->kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
445
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200446static int exact_lock(dev_t devt, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
448 struct gendisk *p = data;
449
450 if (!get_disk(p))
451 return -1;
452 return 0;
453}
454
455/**
456 * add_disk - add partitioning information to kernel list
457 * @disk: per-device partitioning information
458 *
459 * This function registers the partitioning information in @disk
460 * with the kernel.
461 */
462void add_disk(struct gendisk *disk)
463{
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700464 struct backing_dev_info *bdi;
Greg Kroah-Hartman6ffeea72008-05-22 17:21:08 -0400465 int retval;
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 disk->flags |= GENHD_FL_UP;
Tejun Heoed9e1982008-08-25 19:56:05 +0900468 disk_to_dev(disk)->devt = MKDEV(disk->major, disk->first_minor);
Tejun Heof331c022008-09-03 09:01:48 +0200469 blk_register_region(disk_devt(disk), disk->minors, NULL,
470 exact_match, exact_lock, disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 register_disk(disk);
472 blk_register_queue(disk);
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700473
474 bdi = &disk->queue->backing_dev_info;
Tejun Heof331c022008-09-03 09:01:48 +0200475 bdi_register_dev(bdi, disk_devt(disk));
Tejun Heoed9e1982008-08-25 19:56:05 +0900476 retval = sysfs_create_link(&disk_to_dev(disk)->kobj, &bdi->dev->kobj,
477 "bdi");
Greg Kroah-Hartman6ffeea72008-05-22 17:21:08 -0400478 WARN_ON(retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479}
480
481EXPORT_SYMBOL(add_disk);
482EXPORT_SYMBOL(del_gendisk); /* in partitions/check.c */
483
484void unlink_gendisk(struct gendisk *disk)
485{
Tejun Heoed9e1982008-08-25 19:56:05 +0900486 sysfs_remove_link(&disk_to_dev(disk)->kobj, "bdi");
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700487 bdi_unregister(&disk->queue->backing_dev_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 blk_unregister_queue(disk);
Tejun Heof331c022008-09-03 09:01:48 +0200489 blk_unregister_region(disk_devt(disk), disk->minors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492/**
493 * get_gendisk - get partitioning information for a given device
Randy Dunlap710027a2008-08-19 20:13:11 +0200494 * @devt: device to get partitioning information for
495 * @part: returned partition index
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 *
497 * This function gets the structure containing partitioning
Randy Dunlap710027a2008-08-19 20:13:11 +0200498 * information for the given device @devt.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 */
Tejun Heocf771cb2008-09-03 09:01:09 +0200500struct gendisk *get_gendisk(dev_t devt, int *partno)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
Tejun Heobcce3de2008-08-25 19:47:22 +0900502 struct gendisk *disk = NULL;
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200503
Tejun Heobcce3de2008-08-25 19:47:22 +0900504 if (MAJOR(devt) != BLOCK_EXT_MAJOR) {
505 struct kobject *kobj;
506
507 kobj = kobj_lookup(bdev_map, devt, partno);
508 if (kobj)
509 disk = dev_to_disk(kobj_to_dev(kobj));
510 } else {
511 struct hd_struct *part;
512
513 mutex_lock(&ext_devt_mutex);
Tejun Heo870d6652008-08-25 19:47:25 +0900514 part = idr_find(&ext_devt_idr, blk_mangle_minor(MINOR(devt)));
Tejun Heobcce3de2008-08-25 19:47:22 +0900515 if (part && get_disk(part_to_disk(part))) {
516 *partno = part->partno;
517 disk = part_to_disk(part);
518 }
519 mutex_unlock(&ext_devt_mutex);
520 }
521
522 return disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523}
524
Tejun Heof331c022008-09-03 09:01:48 +0200525/**
526 * bdget_disk - do bdget() by gendisk and partition number
527 * @disk: gendisk of interest
528 * @partno: partition number
529 *
530 * Find partition @partno from @disk, do bdget() on it.
531 *
532 * CONTEXT:
533 * Don't care.
534 *
535 * RETURNS:
536 * Resulting block_device on success, NULL on failure.
537 */
538extern struct block_device *bdget_disk(struct gendisk *disk, int partno)
539{
540 dev_t devt = MKDEV(0, 0);
541
542 if (partno == 0)
543 devt = disk_devt(disk);
544 else {
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200545 struct hd_struct *part;
Tejun Heof331c022008-09-03 09:01:48 +0200546
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200547 part = disk_get_part(disk, partno);
Tejun Heof331c022008-09-03 09:01:48 +0200548 if (part && part->nr_sects)
549 devt = part_devt(part);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200550 disk_put_part(part);
Tejun Heof331c022008-09-03 09:01:48 +0200551 }
552
553 if (likely(devt != MKDEV(0, 0)))
554 return bdget(devt);
555 return NULL;
556}
557EXPORT_SYMBOL(bdget_disk);
558
Dave Gilbertdd2a3452007-05-09 02:33:24 -0700559/*
560 * print a full list of all partitions - intended for places where the root
561 * filesystem can't be mounted and thus to give the victim some idea of what
562 * went wrong
563 */
564void __init printk_all_partitions(void)
565{
Tejun Heodef4e382008-09-03 08:57:12 +0200566 struct class_dev_iter iter;
567 struct device *dev;
568
569 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
570 while ((dev = class_dev_iter_next(&iter))) {
571 struct gendisk *disk = dev_to_disk(dev);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200572 struct disk_part_iter piter;
573 struct hd_struct *part;
Tejun Heo1f014292008-08-25 19:47:23 +0900574 char name_buf[BDEVNAME_SIZE];
575 char devt_buf[BDEVT_SIZE];
Tejun Heodef4e382008-09-03 08:57:12 +0200576
577 /*
578 * Don't show empty devices or things that have been
579 * surpressed
580 */
581 if (get_capacity(disk) == 0 ||
582 (disk->flags & GENHD_FL_SUPPRESS_PARTITION_INFO))
583 continue;
584
585 /*
586 * Note, unlike /proc/partitions, I am showing the
587 * numbers in hex - the same format as the root=
588 * option takes.
589 */
Tejun Heo1f014292008-08-25 19:47:23 +0900590 printk("%s %10llu %s",
591 bdevt_str(disk_devt(disk), devt_buf),
Tejun Heodef4e382008-09-03 08:57:12 +0200592 (unsigned long long)get_capacity(disk) >> 1,
Tejun Heo1f014292008-08-25 19:47:23 +0900593 disk_name(disk, 0, name_buf));
Tejun Heodef4e382008-09-03 08:57:12 +0200594 if (disk->driverfs_dev != NULL &&
595 disk->driverfs_dev->driver != NULL)
596 printk(" driver: %s\n",
597 disk->driverfs_dev->driver->name);
598 else
599 printk(" (driver?)\n");
600
601 /* now show the partitions */
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200602 disk_part_iter_init(&piter, disk, 0);
603 while ((part = disk_part_iter_next(&piter)))
Tejun Heo1f014292008-08-25 19:47:23 +0900604 printk(" %s %10llu %s\n",
605 bdevt_str(part_devt(part), devt_buf),
Tejun Heof331c022008-09-03 09:01:48 +0200606 (unsigned long long)part->nr_sects >> 1,
Tejun Heo1f014292008-08-25 19:47:23 +0900607 disk_name(disk, part->partno, name_buf));
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200608 disk_part_iter_exit(&piter);
Tejun Heodef4e382008-09-03 08:57:12 +0200609 }
610 class_dev_iter_exit(&iter);
Dave Gilbertdd2a3452007-05-09 02:33:24 -0700611}
612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613#ifdef CONFIG_PROC_FS
614/* iterator */
Tejun Heodef4e382008-09-03 08:57:12 +0200615static void *disk_seqf_start(struct seq_file *seqf, loff_t *pos)
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -0400616{
Tejun Heodef4e382008-09-03 08:57:12 +0200617 loff_t skip = *pos;
618 struct class_dev_iter *iter;
619 struct device *dev;
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -0400620
Tejun Heodef4e382008-09-03 08:57:12 +0200621 iter = kmalloc(GFP_KERNEL, sizeof(*iter));
622 if (!iter)
623 return ERR_PTR(-ENOMEM);
624
625 seqf->private = iter;
626 class_dev_iter_init(iter, &block_class, NULL, &disk_type);
627 do {
628 dev = class_dev_iter_next(iter);
629 if (!dev)
630 return NULL;
631 } while (skip--);
632
633 return dev_to_disk(dev);
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -0400634}
635
Tejun Heodef4e382008-09-03 08:57:12 +0200636static void *disk_seqf_next(struct seq_file *seqf, void *v, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200638 struct device *dev;
Greg Kroah-Hartman66c64af2008-05-22 17:21:08 -0400639
Tejun Heodef4e382008-09-03 08:57:12 +0200640 (*pos)++;
641 dev = class_dev_iter_next(seqf->private);
Tejun Heo2ac3cee2008-09-03 08:53:37 +0200642 if (dev)
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -0400643 return dev_to_disk(dev);
Tejun Heo2ac3cee2008-09-03 08:53:37 +0200644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 return NULL;
646}
647
Tejun Heodef4e382008-09-03 08:57:12 +0200648static void disk_seqf_stop(struct seq_file *seqf, void *v)
Greg Kroah-Hartman27f30252008-05-22 17:21:08 -0400649{
Tejun Heodef4e382008-09-03 08:57:12 +0200650 struct class_dev_iter *iter = seqf->private;
Greg Kroah-Hartman27f30252008-05-22 17:21:08 -0400651
Tejun Heodef4e382008-09-03 08:57:12 +0200652 /* stop is called even after start failed :-( */
653 if (iter) {
654 class_dev_iter_exit(iter);
655 kfree(iter);
Kay Sievers5c0ef6d2008-08-16 14:30:30 +0200656 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657}
658
Tejun Heodef4e382008-09-03 08:57:12 +0200659static void *show_partition_start(struct seq_file *seqf, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
Tejun Heodef4e382008-09-03 08:57:12 +0200661 static void *p;
662
663 p = disk_seqf_start(seqf, pos);
664 if (!IS_ERR(p) && p)
665 seq_puts(seqf, "major minor #blocks name\n\n");
666 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667}
668
Tejun Heocf771cb2008-09-03 09:01:09 +0200669static int show_partition(struct seq_file *seqf, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
671 struct gendisk *sgp = v;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200672 struct disk_part_iter piter;
673 struct hd_struct *part;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 char buf[BDEVNAME_SIZE];
675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 /* Don't show non-partitionable removeable devices or empty devices */
Tejun Heob5d0b9d2008-09-03 09:06:42 +0200677 if (!get_capacity(sgp) || (!disk_partitionable(sgp) &&
Tejun Heof331c022008-09-03 09:01:48 +0200678 (sgp->flags & GENHD_FL_REMOVABLE)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 return 0;
680 if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)
681 return 0;
682
683 /* show the full disk and all non-0 size partitions of it */
Tejun Heo1f014292008-08-25 19:47:23 +0900684 seq_printf(seqf, "%4d %7d %10llu %s\n",
Tejun Heof331c022008-09-03 09:01:48 +0200685 MAJOR(disk_devt(sgp)), MINOR(disk_devt(sgp)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 (unsigned long long)get_capacity(sgp) >> 1,
687 disk_name(sgp, 0, buf));
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200688
689 disk_part_iter_init(&piter, sgp, 0);
690 while ((part = disk_part_iter_next(&piter)))
Tejun Heo1f014292008-08-25 19:47:23 +0900691 seq_printf(seqf, "%4d %7d %10llu %s\n",
Tejun Heof331c022008-09-03 09:01:48 +0200692 MAJOR(part_devt(part)), MINOR(part_devt(part)),
693 (unsigned long long)part->nr_sects >> 1,
694 disk_name(sgp, part->partno, buf));
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200695 disk_part_iter_exit(&piter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697 return 0;
698}
699
Jan Engelhardt12f32bb2008-01-29 20:57:51 +0100700const struct seq_operations partitions_op = {
Tejun Heodef4e382008-09-03 08:57:12 +0200701 .start = show_partition_start,
702 .next = disk_seqf_next,
703 .stop = disk_seqf_stop,
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200704 .show = show_partition
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705};
706#endif
707
708
Tejun Heocf771cb2008-09-03 09:01:09 +0200709static struct kobject *base_probe(dev_t devt, int *partno, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200711 if (request_module("block-major-%d-%d", MAJOR(devt), MINOR(devt)) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 /* Make old-style 2.4 aliases work */
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200713 request_module("block-major-%d", MAJOR(devt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 return NULL;
715}
716
717static int __init genhd_device_init(void)
718{
Dan Williamse105b8b2008-04-21 10:51:07 -0700719 int error;
720
721 block_class.dev_kobj = sysfs_dev_block_kobj;
722 error = class_register(&block_class);
Roland McGrathee27a552008-03-11 17:13:15 -0700723 if (unlikely(error))
724 return error;
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200725 bdev_map = kobj_map_init(base_probe, &block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 blk_dev_init();
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200727
728#ifndef CONFIG_SYSFS_DEPRECATED
729 /* create top-level block dir */
730 block_depr = kobject_create_and_add("block", NULL);
731#endif
Greg Kroah-Hartman830d3cf2007-11-06 10:36:58 -0800732 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733}
734
735subsys_initcall(genhd_device_init);
736
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200737static ssize_t disk_range_show(struct device *dev,
738 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200740 struct gendisk *disk = dev_to_disk(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200742 return sprintf(buf, "%d\n", disk->minors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743}
744
Tejun Heo1f014292008-08-25 19:47:23 +0900745static ssize_t disk_ext_range_show(struct device *dev,
746 struct device_attribute *attr, char *buf)
747{
748 struct gendisk *disk = dev_to_disk(dev);
749
Tejun Heob5d0b9d2008-09-03 09:06:42 +0200750 return sprintf(buf, "%d\n", disk_max_parts(disk));
Tejun Heo1f014292008-08-25 19:47:23 +0900751}
752
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200753static ssize_t disk_removable_show(struct device *dev,
754 struct device_attribute *attr, char *buf)
Kay Sieversa7fd6702005-10-01 14:49:43 +0200755{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200756 struct gendisk *disk = dev_to_disk(dev);
Kay Sieversa7fd6702005-10-01 14:49:43 +0200757
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200758 return sprintf(buf, "%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200760}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Kay Sievers1c9ce522008-06-13 09:41:00 +0200762static ssize_t disk_ro_show(struct device *dev,
763 struct device_attribute *attr, char *buf)
764{
765 struct gendisk *disk = dev_to_disk(dev);
766
767 return sprintf(buf, "%d\n", disk->policy ? 1 : 0);
768}
769
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200770static ssize_t disk_size_show(struct device *dev,
771 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200773 struct gendisk *disk = dev_to_disk(dev);
774
775 return sprintf(buf, "%llu\n", (unsigned long long)get_capacity(disk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776}
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200777
778static ssize_t disk_capability_show(struct device *dev,
779 struct device_attribute *attr, char *buf)
Kristen Carlson Accardi86ce18d2007-05-23 13:57:38 -0700780{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200781 struct gendisk *disk = dev_to_disk(dev);
782
783 return sprintf(buf, "%x\n", disk->flags);
Kristen Carlson Accardi86ce18d2007-05-23 13:57:38 -0700784}
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200785
786static ssize_t disk_stat_show(struct device *dev,
787 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200789 struct gendisk *disk = dev_to_disk(dev);
Tejun Heoc9959052008-08-25 19:47:21 +0900790 int cpu;
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200791
Tejun Heoc9959052008-08-25 19:47:21 +0900792 cpu = disk_stat_lock();
793 disk_round_stats(cpu, disk);
794 disk_stat_unlock();
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200795 return sprintf(buf,
Ben Woodard837c7872006-03-22 08:09:31 +0100796 "%8lu %8lu %8llu %8u "
797 "%8lu %8lu %8llu %8u "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 "%8u %8u %8u"
799 "\n",
Jens Axboe47a00412005-11-09 13:38:47 +0100800 disk_stat_read(disk, ios[READ]),
801 disk_stat_read(disk, merges[READ]),
802 (unsigned long long)disk_stat_read(disk, sectors[READ]),
803 jiffies_to_msecs(disk_stat_read(disk, ticks[READ])),
804 disk_stat_read(disk, ios[WRITE]),
805 disk_stat_read(disk, merges[WRITE]),
806 (unsigned long long)disk_stat_read(disk, sectors[WRITE]),
807 jiffies_to_msecs(disk_stat_read(disk, ticks[WRITE])),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 disk->in_flight,
809 jiffies_to_msecs(disk_stat_read(disk, io_ticks)),
810 jiffies_to_msecs(disk_stat_read(disk, time_in_queue)));
811}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Akinobu Mitac17bb492006-12-08 02:39:46 -0800813#ifdef CONFIG_FAIL_MAKE_REQUEST
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200814static ssize_t disk_fail_show(struct device *dev,
815 struct device_attribute *attr, char *buf)
816{
817 struct gendisk *disk = dev_to_disk(dev);
Akinobu Mitac17bb492006-12-08 02:39:46 -0800818
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200819 return sprintf(buf, "%d\n", disk->flags & GENHD_FL_FAIL ? 1 : 0);
820}
821
822static ssize_t disk_fail_store(struct device *dev,
823 struct device_attribute *attr,
Akinobu Mitac17bb492006-12-08 02:39:46 -0800824 const char *buf, size_t count)
825{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200826 struct gendisk *disk = dev_to_disk(dev);
Akinobu Mitac17bb492006-12-08 02:39:46 -0800827 int i;
828
829 if (count > 0 && sscanf(buf, "%d", &i) > 0) {
830 if (i == 0)
831 disk->flags &= ~GENHD_FL_FAIL;
832 else
833 disk->flags |= GENHD_FL_FAIL;
834 }
835
836 return count;
837}
Akinobu Mitac17bb492006-12-08 02:39:46 -0800838
839#endif
840
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200841static DEVICE_ATTR(range, S_IRUGO, disk_range_show, NULL);
Tejun Heo1f014292008-08-25 19:47:23 +0900842static DEVICE_ATTR(ext_range, S_IRUGO, disk_ext_range_show, NULL);
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200843static DEVICE_ATTR(removable, S_IRUGO, disk_removable_show, NULL);
Kay Sievers1c9ce522008-06-13 09:41:00 +0200844static DEVICE_ATTR(ro, S_IRUGO, disk_ro_show, NULL);
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200845static DEVICE_ATTR(size, S_IRUGO, disk_size_show, NULL);
846static DEVICE_ATTR(capability, S_IRUGO, disk_capability_show, NULL);
847static DEVICE_ATTR(stat, S_IRUGO, disk_stat_show, NULL);
Akinobu Mitac17bb492006-12-08 02:39:46 -0800848#ifdef CONFIG_FAIL_MAKE_REQUEST
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200849static struct device_attribute dev_attr_fail =
850 __ATTR(make-it-fail, S_IRUGO|S_IWUSR, disk_fail_show, disk_fail_store);
Akinobu Mitac17bb492006-12-08 02:39:46 -0800851#endif
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200852
853static struct attribute *disk_attrs[] = {
854 &dev_attr_range.attr,
Tejun Heo1f014292008-08-25 19:47:23 +0900855 &dev_attr_ext_range.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200856 &dev_attr_removable.attr,
Kay Sievers1c9ce522008-06-13 09:41:00 +0200857 &dev_attr_ro.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200858 &dev_attr_size.attr,
859 &dev_attr_capability.attr,
860 &dev_attr_stat.attr,
861#ifdef CONFIG_FAIL_MAKE_REQUEST
862 &dev_attr_fail.attr,
863#endif
864 NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865};
866
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200867static struct attribute_group disk_attr_group = {
868 .attrs = disk_attrs,
869};
870
871static struct attribute_group *disk_attr_groups[] = {
872 &disk_attr_group,
873 NULL
874};
875
876static void disk_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200878 struct gendisk *disk = dev_to_disk(dev);
879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 kfree(disk->random);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200881 kfree(disk->__part);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 free_disk_stats(disk);
883 kfree(disk);
884}
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200885struct class block_class = {
886 .name = "block",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887};
888
Adrian Bunk1826ead2008-03-04 11:23:46 +0100889static struct device_type disk_type = {
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200890 .name = "disk",
891 .groups = disk_attr_groups,
892 .release = disk_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893};
894
Randy Dunlapa6e2ba82008-05-23 09:44:11 -0700895#ifdef CONFIG_PROC_FS
Tejun Heocf771cb2008-09-03 09:01:09 +0200896/*
897 * aggregate disk stat collector. Uses the same stats that the sysfs
898 * entries do, above, but makes them available through one seq_file.
899 *
900 * The output looks suspiciously like /proc/partitions with a bunch of
901 * extra fields.
902 */
903static int diskstats_show(struct seq_file *seqf, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
905 struct gendisk *gp = v;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200906 struct disk_part_iter piter;
907 struct hd_struct *hd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 char buf[BDEVNAME_SIZE];
Tejun Heoc9959052008-08-25 19:47:21 +0900909 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
911 /*
Tejun Heoed9e1982008-08-25 19:56:05 +0900912 if (&disk_to_dev(gp)->kobj.entry == block_class.devices.next)
Tejun Heocf771cb2008-09-03 09:01:09 +0200913 seq_puts(seqf, "major minor name"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 " rio rmerge rsect ruse wio wmerge "
915 "wsect wuse running use aveq"
916 "\n\n");
917 */
918
Tejun Heoc9959052008-08-25 19:47:21 +0900919 cpu = disk_stat_lock();
920 disk_round_stats(cpu, gp);
921 disk_stat_unlock();
Tejun Heo1f014292008-08-25 19:47:23 +0900922 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 +0200923 MAJOR(disk_devt(gp)), MINOR(disk_devt(gp)),
924 disk_name(gp, 0, buf),
Jens Axboea3623572005-11-01 09:26:16 +0100925 disk_stat_read(gp, ios[0]), disk_stat_read(gp, merges[0]),
926 (unsigned long long)disk_stat_read(gp, sectors[0]),
927 jiffies_to_msecs(disk_stat_read(gp, ticks[0])),
928 disk_stat_read(gp, ios[1]), disk_stat_read(gp, merges[1]),
929 (unsigned long long)disk_stat_read(gp, sectors[1]),
930 jiffies_to_msecs(disk_stat_read(gp, ticks[1])),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 gp->in_flight,
932 jiffies_to_msecs(disk_stat_read(gp, io_ticks)),
933 jiffies_to_msecs(disk_stat_read(gp, time_in_queue)));
934
935 /* now show all non-0 size partitions of it */
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200936 disk_part_iter_init(&piter, gp, 0);
937 while ((hd = disk_part_iter_next(&piter))) {
Tejun Heoc9959052008-08-25 19:47:21 +0900938 cpu = disk_stat_lock();
939 part_round_stats(cpu, hd);
940 disk_stat_unlock();
Tejun Heo1f014292008-08-25 19:47:23 +0900941 seq_printf(seqf, "%4d %7d %s %lu %lu %llu "
Jerome Marchand28f39d52008-02-08 11:04:56 +0100942 "%u %lu %lu %llu %u %u %u %u\n",
Tejun Heof331c022008-09-03 09:01:48 +0200943 MAJOR(part_devt(hd)), MINOR(part_devt(hd)),
944 disk_name(gp, hd->partno, buf),
Jerome Marchand28f39d52008-02-08 11:04:56 +0100945 part_stat_read(hd, ios[0]),
946 part_stat_read(hd, merges[0]),
947 (unsigned long long)part_stat_read(hd, sectors[0]),
948 jiffies_to_msecs(part_stat_read(hd, ticks[0])),
949 part_stat_read(hd, ios[1]),
950 part_stat_read(hd, merges[1]),
951 (unsigned long long)part_stat_read(hd, sectors[1]),
952 jiffies_to_msecs(part_stat_read(hd, ticks[1])),
953 hd->in_flight,
954 jiffies_to_msecs(part_stat_read(hd, io_ticks)),
955 jiffies_to_msecs(part_stat_read(hd, time_in_queue))
956 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 }
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200958 disk_part_iter_exit(&piter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
960 return 0;
961}
962
Jan Engelhardt12f32bb2008-01-29 20:57:51 +0100963const struct seq_operations diskstats_op = {
Tejun Heodef4e382008-09-03 08:57:12 +0200964 .start = disk_seqf_start,
965 .next = disk_seqf_next,
966 .stop = disk_seqf_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 .show = diskstats_show
968};
Randy Dunlapa6e2ba82008-05-23 09:44:11 -0700969#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Kristen Carlson Accardi8ce7ad72007-05-23 13:57:38 -0700971static void media_change_notify_thread(struct work_struct *work)
972{
973 struct gendisk *gd = container_of(work, struct gendisk, async_notify);
974 char event[] = "MEDIA_CHANGE=1";
975 char *envp[] = { event, NULL };
976
977 /*
978 * set enviroment vars to indicate which event this is for
979 * so that user space will know to go check the media status.
980 */
Tejun Heoed9e1982008-08-25 19:56:05 +0900981 kobject_uevent_env(&disk_to_dev(gd)->kobj, KOBJ_CHANGE, envp);
Kristen Carlson Accardi8ce7ad72007-05-23 13:57:38 -0700982 put_device(gd->driverfs_dev);
983}
984
Adrian Bunk1826ead2008-03-04 11:23:46 +0100985#if 0
Kristen Carlson Accardi8ce7ad72007-05-23 13:57:38 -0700986void genhd_media_change_notify(struct gendisk *disk)
987{
988 get_device(disk->driverfs_dev);
989 schedule_work(&disk->async_notify);
990}
991EXPORT_SYMBOL_GPL(genhd_media_change_notify);
Adrian Bunk1826ead2008-03-04 11:23:46 +0100992#endif /* 0 */
Kristen Carlson Accardi8ce7ad72007-05-23 13:57:38 -0700993
Tejun Heocf771cb2008-09-03 09:01:09 +0200994dev_t blk_lookup_devt(const char *name, int partno)
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200995{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200996 dev_t devt = MKDEV(0, 0);
Tejun Heodef4e382008-09-03 08:57:12 +0200997 struct class_dev_iter iter;
998 struct device *dev;
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200999
Tejun Heodef4e382008-09-03 08:57:12 +02001000 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
1001 while ((dev = class_dev_iter_next(&iter))) {
1002 struct gendisk *disk = dev_to_disk(dev);
1003
Tejun Heof331c022008-09-03 09:01:48 +02001004 if (strcmp(dev->bus_id, name))
1005 continue;
Tejun Heob5d0b9d2008-09-03 09:06:42 +02001006 if (partno < 0 || partno >= disk_max_parts(disk))
Tejun Heof331c022008-09-03 09:01:48 +02001007 continue;
1008
1009 if (partno == 0)
1010 devt = disk_devt(disk);
1011 else {
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001012 struct hd_struct *part;
Tejun Heof331c022008-09-03 09:01:48 +02001013
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001014 part = disk_get_part(disk, partno);
1015 if (!part || !part->nr_sects) {
1016 disk_put_part(part);
Tejun Heof331c022008-09-03 09:01:48 +02001017 continue;
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001018 }
Tejun Heof331c022008-09-03 09:01:48 +02001019
1020 devt = part_devt(part);
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001021 disk_put_part(part);
Tejun Heodef4e382008-09-03 08:57:12 +02001022 }
Tejun Heof331c022008-09-03 09:01:48 +02001023 break;
Kay Sievers5c0ef6d2008-08-16 14:30:30 +02001024 }
Tejun Heodef4e382008-09-03 08:57:12 +02001025 class_dev_iter_exit(&iter);
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001026 return devt;
1027}
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001028EXPORT_SYMBOL(blk_lookup_devt);
1029
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030struct gendisk *alloc_disk(int minors)
1031{
Christoph Lameter19460892005-06-23 00:08:19 -07001032 return alloc_disk_node(minors, -1);
1033}
1034
1035struct gendisk *alloc_disk_node(int minors, int node_id)
1036{
Tejun Heobcce3de2008-08-25 19:47:22 +09001037 return alloc_disk_ext_node(minors, 0, node_id);
1038}
1039
1040struct gendisk *alloc_disk_ext(int minors, int ext_minors)
1041{
1042 return alloc_disk_ext_node(minors, ext_minors, -1);
1043}
1044
1045struct gendisk *alloc_disk_ext_node(int minors, int ext_minors, int node_id)
1046{
Christoph Lameter19460892005-06-23 00:08:19 -07001047 struct gendisk *disk;
1048
Christoph Lameter94f60302007-07-17 04:03:29 -07001049 disk = kmalloc_node(sizeof(struct gendisk),
1050 GFP_KERNEL | __GFP_ZERO, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 if (disk) {
Tejun Heobcce3de2008-08-25 19:47:22 +09001052 int tot_minors = minors + ext_minors;
Tejun Heob5d0b9d2008-09-03 09:06:42 +02001053 int size = tot_minors * sizeof(struct hd_struct *);
Tejun Heobcce3de2008-08-25 19:47:22 +09001054
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 if (!init_disk_stats(disk)) {
1056 kfree(disk);
1057 return NULL;
1058 }
Tejun Heob5d0b9d2008-09-03 09:06:42 +02001059
1060 disk->__part = kmalloc_node(size, GFP_KERNEL | __GFP_ZERO,
1061 node_id);
1062 if (!disk->__part) {
1063 free_disk_stats(disk);
1064 kfree(disk);
1065 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 }
Tejun Heob5d0b9d2008-09-03 09:06:42 +02001067 disk->__part[0] = &disk->part0;
1068
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 disk->minors = minors;
Tejun Heobcce3de2008-08-25 19:47:22 +09001070 disk->ext_minors = ext_minors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 rand_initialize_disk(disk);
Tejun Heoed9e1982008-08-25 19:56:05 +09001072 disk_to_dev(disk)->class = &block_class;
1073 disk_to_dev(disk)->type = &disk_type;
1074 device_initialize(disk_to_dev(disk));
Kristen Carlson Accardi8ce7ad72007-05-23 13:57:38 -07001075 INIT_WORK(&disk->async_notify,
1076 media_change_notify_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 }
1078 return disk;
1079}
1080
1081EXPORT_SYMBOL(alloc_disk);
Christoph Lameter19460892005-06-23 00:08:19 -07001082EXPORT_SYMBOL(alloc_disk_node);
Tejun Heobcce3de2008-08-25 19:47:22 +09001083EXPORT_SYMBOL(alloc_disk_ext);
1084EXPORT_SYMBOL(alloc_disk_ext_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
1086struct kobject *get_disk(struct gendisk *disk)
1087{
1088 struct module *owner;
1089 struct kobject *kobj;
1090
1091 if (!disk->fops)
1092 return NULL;
1093 owner = disk->fops->owner;
1094 if (owner && !try_module_get(owner))
1095 return NULL;
Tejun Heoed9e1982008-08-25 19:56:05 +09001096 kobj = kobject_get(&disk_to_dev(disk)->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 if (kobj == NULL) {
1098 module_put(owner);
1099 return NULL;
1100 }
1101 return kobj;
1102
1103}
1104
1105EXPORT_SYMBOL(get_disk);
1106
1107void put_disk(struct gendisk *disk)
1108{
1109 if (disk)
Tejun Heoed9e1982008-08-25 19:56:05 +09001110 kobject_put(&disk_to_dev(disk)->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111}
1112
1113EXPORT_SYMBOL(put_disk);
1114
1115void set_device_ro(struct block_device *bdev, int flag)
1116{
1117 if (bdev->bd_contains != bdev)
1118 bdev->bd_part->policy = flag;
1119 else
1120 bdev->bd_disk->policy = flag;
1121}
1122
1123EXPORT_SYMBOL(set_device_ro);
1124
1125void set_disk_ro(struct gendisk *disk, int flag)
1126{
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001127 struct disk_part_iter piter;
1128 struct hd_struct *part;
1129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 disk->policy = flag;
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001131 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_EMPTY);
1132 while ((part = disk_part_iter_next(&piter)))
1133 part->policy = flag;
1134 disk_part_iter_exit(&piter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135}
1136
1137EXPORT_SYMBOL(set_disk_ro);
1138
1139int bdev_read_only(struct block_device *bdev)
1140{
1141 if (!bdev)
1142 return 0;
1143 else if (bdev->bd_contains != bdev)
1144 return bdev->bd_part->policy;
1145 else
1146 return bdev->bd_disk->policy;
1147}
1148
1149EXPORT_SYMBOL(bdev_read_only);
1150
Tejun Heocf771cb2008-09-03 09:01:09 +02001151int invalidate_partition(struct gendisk *disk, int partno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152{
1153 int res = 0;
Tejun Heocf771cb2008-09-03 09:01:09 +02001154 struct block_device *bdev = bdget_disk(disk, partno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 if (bdev) {
Christoph Hellwig2ef41632005-05-05 16:15:59 -07001156 fsync_bdev(bdev);
1157 res = __invalidate_device(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 bdput(bdev);
1159 }
1160 return res;
1161}
1162
1163EXPORT_SYMBOL(invalidate_partition);