blob: 646e1d2507c70d2ba88824c3dd84d91874ecb192 [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{
Tejun Heo540eed52008-08-25 19:56:15 +090055 struct hd_struct *part = NULL;
56 struct disk_part_tbl *ptbl;
Tejun Heoe71bf0d2008-09-03 09:03:02 +020057
Tejun Heo540eed52008-08-25 19:56:15 +090058 if (unlikely(partno < 0))
Tejun Heoe71bf0d2008-09-03 09:03:02 +020059 return NULL;
Tejun Heo540eed52008-08-25 19:56:15 +090060
Tejun Heoe71bf0d2008-09-03 09:03:02 +020061 rcu_read_lock();
Tejun Heo540eed52008-08-25 19:56:15 +090062
63 ptbl = rcu_dereference(disk->part_tbl);
64 if (likely(partno < ptbl->len)) {
65 part = rcu_dereference(ptbl->part[partno]);
66 if (part)
67 get_device(part_to_dev(part));
68 }
69
Tejun Heoe71bf0d2008-09-03 09:03:02 +020070 rcu_read_unlock();
71
72 return part;
73}
74EXPORT_SYMBOL_GPL(disk_get_part);
75
76/**
77 * disk_part_iter_init - initialize partition iterator
78 * @piter: iterator to initialize
79 * @disk: disk to iterate over
80 * @flags: DISK_PITER_* flags
81 *
82 * Initialize @piter so that it iterates over partitions of @disk.
83 *
84 * CONTEXT:
85 * Don't care.
86 */
87void disk_part_iter_init(struct disk_part_iter *piter, struct gendisk *disk,
88 unsigned int flags)
89{
Tejun Heo540eed52008-08-25 19:56:15 +090090 struct disk_part_tbl *ptbl;
91
92 rcu_read_lock();
93 ptbl = rcu_dereference(disk->part_tbl);
94
Tejun Heoe71bf0d2008-09-03 09:03:02 +020095 piter->disk = disk;
96 piter->part = NULL;
97
98 if (flags & DISK_PITER_REVERSE)
Tejun Heo540eed52008-08-25 19:56:15 +090099 piter->idx = ptbl->len - 1;
Tejun Heob5d0b9d2008-09-03 09:06:42 +0200100 else if (flags & DISK_PITER_INCL_PART0)
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200101 piter->idx = 0;
Tejun Heob5d0b9d2008-09-03 09:06:42 +0200102 else
103 piter->idx = 1;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200104
105 piter->flags = flags;
Tejun Heo540eed52008-08-25 19:56:15 +0900106
107 rcu_read_unlock();
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200108}
109EXPORT_SYMBOL_GPL(disk_part_iter_init);
110
111/**
112 * disk_part_iter_next - proceed iterator to the next partition and return it
113 * @piter: iterator of interest
114 *
115 * Proceed @piter to the next partition and return it.
116 *
117 * CONTEXT:
118 * Don't care.
119 */
120struct hd_struct *disk_part_iter_next(struct disk_part_iter *piter)
121{
Tejun Heo540eed52008-08-25 19:56:15 +0900122 struct disk_part_tbl *ptbl;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200123 int inc, end;
124
125 /* put the last partition */
126 disk_put_part(piter->part);
127 piter->part = NULL;
128
Tejun Heo540eed52008-08-25 19:56:15 +0900129 /* get part_tbl */
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200130 rcu_read_lock();
Tejun Heo540eed52008-08-25 19:56:15 +0900131 ptbl = rcu_dereference(piter->disk->part_tbl);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200132
133 /* determine iteration parameters */
134 if (piter->flags & DISK_PITER_REVERSE) {
135 inc = -1;
Tejun Heob5d0b9d2008-09-03 09:06:42 +0200136 if (piter->flags & DISK_PITER_INCL_PART0)
137 end = -1;
138 else
139 end = 0;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200140 } else {
141 inc = 1;
Tejun Heo540eed52008-08-25 19:56:15 +0900142 end = ptbl->len;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200143 }
144
145 /* iterate to the next partition */
146 for (; piter->idx != end; piter->idx += inc) {
147 struct hd_struct *part;
148
Tejun Heo540eed52008-08-25 19:56:15 +0900149 part = rcu_dereference(ptbl->part[piter->idx]);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200150 if (!part)
151 continue;
152 if (!(piter->flags & DISK_PITER_INCL_EMPTY) && !part->nr_sects)
153 continue;
154
Tejun Heoed9e1982008-08-25 19:56:05 +0900155 get_device(part_to_dev(part));
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200156 piter->part = part;
157 piter->idx += inc;
158 break;
159 }
160
161 rcu_read_unlock();
162
163 return piter->part;
164}
165EXPORT_SYMBOL_GPL(disk_part_iter_next);
166
167/**
168 * disk_part_iter_exit - finish up partition iteration
169 * @piter: iter of interest
170 *
171 * Called when iteration is over. Cleans up @piter.
172 *
173 * CONTEXT:
174 * Don't care.
175 */
176void disk_part_iter_exit(struct disk_part_iter *piter)
177{
178 disk_put_part(piter->part);
179 piter->part = NULL;
180}
181EXPORT_SYMBOL_GPL(disk_part_iter_exit);
182
183/**
184 * disk_map_sector_rcu - map sector to partition
185 * @disk: gendisk of interest
186 * @sector: sector to map
187 *
188 * Find out which partition @sector maps to on @disk. This is
189 * primarily used for stats accounting.
190 *
191 * CONTEXT:
192 * RCU read locked. The returned partition pointer is valid only
193 * while preemption is disabled.
194 *
195 * RETURNS:
Tejun Heo074a7ac2008-08-25 19:56:14 +0900196 * Found partition on success, part0 is returned if no partition matches
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200197 */
198struct hd_struct *disk_map_sector_rcu(struct gendisk *disk, sector_t sector)
199{
Tejun Heo540eed52008-08-25 19:56:15 +0900200 struct disk_part_tbl *ptbl;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200201 int i;
202
Tejun Heo540eed52008-08-25 19:56:15 +0900203 ptbl = rcu_dereference(disk->part_tbl);
204
205 for (i = 1; i < ptbl->len; i++) {
206 struct hd_struct *part = rcu_dereference(ptbl->part[i]);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200207
208 if (part && part->start_sect <= sector &&
209 sector < part->start_sect + part->nr_sects)
210 return part;
211 }
Tejun Heo074a7ac2008-08-25 19:56:14 +0900212 return &disk->part0;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200213}
214EXPORT_SYMBOL_GPL(disk_map_sector_rcu);
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216/*
217 * Can be deleted altogether. Later.
218 *
219 */
220static struct blk_major_name {
221 struct blk_major_name *next;
222 int major;
223 char name[16];
Joe Korty68eef3b2006-03-31 02:30:32 -0800224} *major_names[BLKDEV_MAJOR_HASH_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226/* index in the above - for now: assume no multimajor ranges */
227static inline int major_to_index(int major)
228{
Joe Korty68eef3b2006-03-31 02:30:32 -0800229 return major % BLKDEV_MAJOR_HASH_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
Joe Korty68eef3b2006-03-31 02:30:32 -0800232#ifdef CONFIG_PROC_FS
Tejun Heocf771cb2008-09-03 09:01:09 +0200233void blkdev_show(struct seq_file *seqf, off_t offset)
Neil Horman7170be52006-01-14 13:20:38 -0800234{
Joe Korty68eef3b2006-03-31 02:30:32 -0800235 struct blk_major_name *dp;
Neil Horman7170be52006-01-14 13:20:38 -0800236
Joe Korty68eef3b2006-03-31 02:30:32 -0800237 if (offset < BLKDEV_MAJOR_HASH_SIZE) {
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200238 mutex_lock(&block_class_lock);
Joe Korty68eef3b2006-03-31 02:30:32 -0800239 for (dp = major_names[offset]; dp; dp = dp->next)
Tejun Heocf771cb2008-09-03 09:01:09 +0200240 seq_printf(seqf, "%3d %s\n", dp->major, dp->name);
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200241 mutex_unlock(&block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
Joe Korty68eef3b2006-03-31 02:30:32 -0800244#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246int register_blkdev(unsigned int major, const char *name)
247{
248 struct blk_major_name **n, *p;
249 int index, ret = 0;
250
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200251 mutex_lock(&block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 /* temporary */
254 if (major == 0) {
255 for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) {
256 if (major_names[index] == NULL)
257 break;
258 }
259
260 if (index == 0) {
261 printk("register_blkdev: failed to get major for %s\n",
262 name);
263 ret = -EBUSY;
264 goto out;
265 }
266 major = index;
267 ret = major;
268 }
269
270 p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL);
271 if (p == NULL) {
272 ret = -ENOMEM;
273 goto out;
274 }
275
276 p->major = major;
277 strlcpy(p->name, name, sizeof(p->name));
278 p->next = NULL;
279 index = major_to_index(major);
280
281 for (n = &major_names[index]; *n; n = &(*n)->next) {
282 if ((*n)->major == major)
283 break;
284 }
285 if (!*n)
286 *n = p;
287 else
288 ret = -EBUSY;
289
290 if (ret < 0) {
291 printk("register_blkdev: cannot get major %d for %s\n",
292 major, name);
293 kfree(p);
294 }
295out:
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200296 mutex_unlock(&block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 return ret;
298}
299
300EXPORT_SYMBOL(register_blkdev);
301
Akinobu Mitaf4480242007-07-17 04:03:47 -0700302void unregister_blkdev(unsigned int major, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
304 struct blk_major_name **n;
305 struct blk_major_name *p = NULL;
306 int index = major_to_index(major);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200308 mutex_lock(&block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 for (n = &major_names[index]; *n; n = &(*n)->next)
310 if ((*n)->major == major)
311 break;
Akinobu Mita294462a2007-07-17 04:03:45 -0700312 if (!*n || strcmp((*n)->name, name)) {
313 WARN_ON(1);
Akinobu Mita294462a2007-07-17 04:03:45 -0700314 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 p = *n;
316 *n = p->next;
317 }
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200318 mutex_unlock(&block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320}
321
322EXPORT_SYMBOL(unregister_blkdev);
323
324static struct kobj_map *bdev_map;
325
Tejun Heobcce3de2008-08-25 19:47:22 +0900326/**
Tejun Heo870d6652008-08-25 19:47:25 +0900327 * blk_mangle_minor - scatter minor numbers apart
328 * @minor: minor number to mangle
329 *
330 * Scatter consecutively allocated @minor number apart if MANGLE_DEVT
331 * is enabled. Mangling twice gives the original value.
332 *
333 * RETURNS:
334 * Mangled value.
335 *
336 * CONTEXT:
337 * Don't care.
338 */
339static int blk_mangle_minor(int minor)
340{
341#ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT
342 int i;
343
344 for (i = 0; i < MINORBITS / 2; i++) {
345 int low = minor & (1 << i);
346 int high = minor & (1 << (MINORBITS - 1 - i));
347 int distance = MINORBITS - 1 - 2 * i;
348
349 minor ^= low | high; /* clear both bits */
350 low <<= distance; /* swap the positions */
351 high >>= distance;
352 minor |= low | high; /* and set */
353 }
354#endif
355 return minor;
356}
357
358/**
Tejun Heobcce3de2008-08-25 19:47:22 +0900359 * blk_alloc_devt - allocate a dev_t for a partition
360 * @part: partition to allocate dev_t for
Tejun Heobcce3de2008-08-25 19:47:22 +0900361 * @devt: out parameter for resulting dev_t
362 *
363 * Allocate a dev_t for block device.
364 *
365 * RETURNS:
366 * 0 on success, allocated dev_t is returned in *@devt. -errno on
367 * failure.
368 *
369 * CONTEXT:
370 * Might sleep.
371 */
372int blk_alloc_devt(struct hd_struct *part, dev_t *devt)
373{
374 struct gendisk *disk = part_to_disk(part);
375 int idx, rc;
376
377 /* in consecutive minor range? */
378 if (part->partno < disk->minors) {
379 *devt = MKDEV(disk->major, disk->first_minor + part->partno);
380 return 0;
381 }
382
383 /* allocate ext devt */
384 do {
385 if (!idr_pre_get(&ext_devt_idr, GFP_KERNEL))
386 return -ENOMEM;
387 rc = idr_get_new(&ext_devt_idr, part, &idx);
388 } while (rc == -EAGAIN);
389
390 if (rc)
391 return rc;
392
393 if (idx > MAX_EXT_DEVT) {
394 idr_remove(&ext_devt_idr, idx);
395 return -EBUSY;
396 }
397
Tejun Heo870d6652008-08-25 19:47:25 +0900398 *devt = MKDEV(BLOCK_EXT_MAJOR, blk_mangle_minor(idx));
Tejun Heobcce3de2008-08-25 19:47:22 +0900399 return 0;
400}
401
402/**
403 * blk_free_devt - free a dev_t
404 * @devt: dev_t to free
405 *
406 * Free @devt which was allocated using blk_alloc_devt().
407 *
408 * CONTEXT:
409 * Might sleep.
410 */
411void blk_free_devt(dev_t devt)
412{
413 might_sleep();
414
415 if (devt == MKDEV(0, 0))
416 return;
417
418 if (MAJOR(devt) == BLOCK_EXT_MAJOR) {
419 mutex_lock(&ext_devt_mutex);
Tejun Heo870d6652008-08-25 19:47:25 +0900420 idr_remove(&ext_devt_idr, blk_mangle_minor(MINOR(devt)));
Tejun Heobcce3de2008-08-25 19:47:22 +0900421 mutex_unlock(&ext_devt_mutex);
422 }
423}
424
Tejun Heo1f014292008-08-25 19:47:23 +0900425static char *bdevt_str(dev_t devt, char *buf)
426{
427 if (MAJOR(devt) <= 0xff && MINOR(devt) <= 0xff) {
428 char tbuf[BDEVT_SIZE];
429 snprintf(tbuf, BDEVT_SIZE, "%02x%02x", MAJOR(devt), MINOR(devt));
430 snprintf(buf, BDEVT_SIZE, "%-9s", tbuf);
431 } else
432 snprintf(buf, BDEVT_SIZE, "%03x:%05x", MAJOR(devt), MINOR(devt));
433
434 return buf;
435}
436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437/*
438 * Register device numbers dev..(dev+range-1)
439 * range must be nonzero
440 * The hash chain is sorted on range, so that subranges can override.
441 */
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200442void blk_register_region(dev_t devt, unsigned long range, struct module *module,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 struct kobject *(*probe)(dev_t, int *, void *),
444 int (*lock)(dev_t, void *), void *data)
445{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200446 kobj_map(bdev_map, devt, range, module, probe, lock, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447}
448
449EXPORT_SYMBOL(blk_register_region);
450
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200451void blk_unregister_region(dev_t devt, unsigned long range)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200453 kobj_unmap(bdev_map, devt, range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
455
456EXPORT_SYMBOL(blk_unregister_region);
457
Tejun Heocf771cb2008-09-03 09:01:09 +0200458static struct kobject *exact_match(dev_t devt, int *partno, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
460 struct gendisk *p = data;
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200461
Tejun Heoed9e1982008-08-25 19:56:05 +0900462 return &disk_to_dev(p)->kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463}
464
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200465static int exact_lock(dev_t devt, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
467 struct gendisk *p = data;
468
469 if (!get_disk(p))
470 return -1;
471 return 0;
472}
473
474/**
475 * add_disk - add partitioning information to kernel list
476 * @disk: per-device partitioning information
477 *
478 * This function registers the partitioning information in @disk
479 * with the kernel.
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900480 *
481 * FIXME: error handling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 */
483void add_disk(struct gendisk *disk)
484{
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700485 struct backing_dev_info *bdi;
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900486 dev_t devt;
Greg Kroah-Hartman6ffeea72008-05-22 17:21:08 -0400487 int retval;
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700488
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900489 /* minors == 0 indicates to use ext devt from part0 and should
490 * be accompanied with EXT_DEVT flag. Make sure all
491 * parameters make sense.
492 */
493 WARN_ON(disk->minors && !(disk->major || disk->first_minor));
494 WARN_ON(!disk->minors && !(disk->flags & GENHD_FL_EXT_DEVT));
495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 disk->flags |= GENHD_FL_UP;
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900497
498 retval = blk_alloc_devt(&disk->part0, &devt);
499 if (retval) {
500 WARN_ON(1);
501 return;
502 }
503 disk_to_dev(disk)->devt = devt;
504
505 /* ->major and ->first_minor aren't supposed to be
506 * dereferenced from here on, but set them just in case.
507 */
508 disk->major = MAJOR(devt);
509 disk->first_minor = MINOR(devt);
510
Tejun Heof331c022008-09-03 09:01:48 +0200511 blk_register_region(disk_devt(disk), disk->minors, NULL,
512 exact_match, exact_lock, disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 register_disk(disk);
514 blk_register_queue(disk);
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700515
516 bdi = &disk->queue->backing_dev_info;
Tejun Heof331c022008-09-03 09:01:48 +0200517 bdi_register_dev(bdi, disk_devt(disk));
Tejun Heoed9e1982008-08-25 19:56:05 +0900518 retval = sysfs_create_link(&disk_to_dev(disk)->kobj, &bdi->dev->kobj,
519 "bdi");
Greg Kroah-Hartman6ffeea72008-05-22 17:21:08 -0400520 WARN_ON(retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521}
522
523EXPORT_SYMBOL(add_disk);
524EXPORT_SYMBOL(del_gendisk); /* in partitions/check.c */
525
526void unlink_gendisk(struct gendisk *disk)
527{
Tejun Heoed9e1982008-08-25 19:56:05 +0900528 sysfs_remove_link(&disk_to_dev(disk)->kobj, "bdi");
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700529 bdi_unregister(&disk->queue->backing_dev_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 blk_unregister_queue(disk);
Tejun Heof331c022008-09-03 09:01:48 +0200531 blk_unregister_region(disk_devt(disk), disk->minors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532}
533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534/**
535 * get_gendisk - get partitioning information for a given device
Randy Dunlap710027a2008-08-19 20:13:11 +0200536 * @devt: device to get partitioning information for
Randy Dunlap496aa8a2008-10-16 07:46:23 +0200537 * @partno: returned partition index
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 *
539 * This function gets the structure containing partitioning
Randy Dunlap710027a2008-08-19 20:13:11 +0200540 * information for the given device @devt.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 */
Tejun Heocf771cb2008-09-03 09:01:09 +0200542struct gendisk *get_gendisk(dev_t devt, int *partno)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
Tejun Heobcce3de2008-08-25 19:47:22 +0900544 struct gendisk *disk = NULL;
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200545
Tejun Heobcce3de2008-08-25 19:47:22 +0900546 if (MAJOR(devt) != BLOCK_EXT_MAJOR) {
547 struct kobject *kobj;
548
549 kobj = kobj_lookup(bdev_map, devt, partno);
550 if (kobj)
551 disk = dev_to_disk(kobj_to_dev(kobj));
552 } else {
553 struct hd_struct *part;
554
555 mutex_lock(&ext_devt_mutex);
Tejun Heo870d6652008-08-25 19:47:25 +0900556 part = idr_find(&ext_devt_idr, blk_mangle_minor(MINOR(devt)));
Tejun Heobcce3de2008-08-25 19:47:22 +0900557 if (part && get_disk(part_to_disk(part))) {
558 *partno = part->partno;
559 disk = part_to_disk(part);
560 }
561 mutex_unlock(&ext_devt_mutex);
562 }
563
564 return disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565}
566
Tejun Heof331c022008-09-03 09:01:48 +0200567/**
568 * bdget_disk - do bdget() by gendisk and partition number
569 * @disk: gendisk of interest
570 * @partno: partition number
571 *
572 * Find partition @partno from @disk, do bdget() on it.
573 *
574 * CONTEXT:
575 * Don't care.
576 *
577 * RETURNS:
578 * Resulting block_device on success, NULL on failure.
579 */
Harvey Harrisonaeb3d3a2008-08-28 09:27:42 +0200580struct block_device *bdget_disk(struct gendisk *disk, int partno)
Tejun Heof331c022008-09-03 09:01:48 +0200581{
Tejun Heo548b10e2008-08-29 09:01:47 +0200582 struct hd_struct *part;
583 struct block_device *bdev = NULL;
Tejun Heof331c022008-09-03 09:01:48 +0200584
Tejun Heo548b10e2008-08-29 09:01:47 +0200585 part = disk_get_part(disk, partno);
Tejun Heo2bbedcb2008-08-29 11:41:51 +0200586 if (part)
Tejun Heo548b10e2008-08-29 09:01:47 +0200587 bdev = bdget(part_devt(part));
588 disk_put_part(part);
Tejun Heof331c022008-09-03 09:01:48 +0200589
Tejun Heo548b10e2008-08-29 09:01:47 +0200590 return bdev;
Tejun Heof331c022008-09-03 09:01:48 +0200591}
592EXPORT_SYMBOL(bdget_disk);
593
Dave Gilbertdd2a3452007-05-09 02:33:24 -0700594/*
595 * print a full list of all partitions - intended for places where the root
596 * filesystem can't be mounted and thus to give the victim some idea of what
597 * went wrong
598 */
599void __init printk_all_partitions(void)
600{
Tejun Heodef4e382008-09-03 08:57:12 +0200601 struct class_dev_iter iter;
602 struct device *dev;
603
604 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
605 while ((dev = class_dev_iter_next(&iter))) {
606 struct gendisk *disk = dev_to_disk(dev);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200607 struct disk_part_iter piter;
608 struct hd_struct *part;
Tejun Heo1f014292008-08-25 19:47:23 +0900609 char name_buf[BDEVNAME_SIZE];
610 char devt_buf[BDEVT_SIZE];
Tejun Heodef4e382008-09-03 08:57:12 +0200611
612 /*
613 * Don't show empty devices or things that have been
614 * surpressed
615 */
616 if (get_capacity(disk) == 0 ||
617 (disk->flags & GENHD_FL_SUPPRESS_PARTITION_INFO))
618 continue;
619
620 /*
621 * Note, unlike /proc/partitions, I am showing the
622 * numbers in hex - the same format as the root=
623 * option takes.
624 */
Tejun Heo074a7ac2008-08-25 19:56:14 +0900625 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
626 while ((part = disk_part_iter_next(&piter))) {
627 bool is_part0 = part == &disk->part0;
Tejun Heodef4e382008-09-03 08:57:12 +0200628
Tejun Heo074a7ac2008-08-25 19:56:14 +0900629 printk("%s%s %10llu %s", is_part0 ? "" : " ",
Tejun Heo1f014292008-08-25 19:47:23 +0900630 bdevt_str(part_devt(part), devt_buf),
Tejun Heof331c022008-09-03 09:01:48 +0200631 (unsigned long long)part->nr_sects >> 1,
Tejun Heo1f014292008-08-25 19:47:23 +0900632 disk_name(disk, part->partno, name_buf));
Tejun Heo074a7ac2008-08-25 19:56:14 +0900633 if (is_part0) {
634 if (disk->driverfs_dev != NULL &&
635 disk->driverfs_dev->driver != NULL)
636 printk(" driver: %s\n",
637 disk->driverfs_dev->driver->name);
638 else
639 printk(" (driver?)\n");
640 } else
641 printk("\n");
642 }
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200643 disk_part_iter_exit(&piter);
Tejun Heodef4e382008-09-03 08:57:12 +0200644 }
645 class_dev_iter_exit(&iter);
Dave Gilbertdd2a3452007-05-09 02:33:24 -0700646}
647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648#ifdef CONFIG_PROC_FS
649/* iterator */
Tejun Heodef4e382008-09-03 08:57:12 +0200650static void *disk_seqf_start(struct seq_file *seqf, loff_t *pos)
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -0400651{
Tejun Heodef4e382008-09-03 08:57:12 +0200652 loff_t skip = *pos;
653 struct class_dev_iter *iter;
654 struct device *dev;
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -0400655
Harvey Harrisonaeb3d3a2008-08-28 09:27:42 +0200656 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
Tejun Heodef4e382008-09-03 08:57:12 +0200657 if (!iter)
658 return ERR_PTR(-ENOMEM);
659
660 seqf->private = iter;
661 class_dev_iter_init(iter, &block_class, NULL, &disk_type);
662 do {
663 dev = class_dev_iter_next(iter);
664 if (!dev)
665 return NULL;
666 } while (skip--);
667
668 return dev_to_disk(dev);
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -0400669}
670
Tejun Heodef4e382008-09-03 08:57:12 +0200671static void *disk_seqf_next(struct seq_file *seqf, void *v, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200673 struct device *dev;
Greg Kroah-Hartman66c64af2008-05-22 17:21:08 -0400674
Tejun Heodef4e382008-09-03 08:57:12 +0200675 (*pos)++;
676 dev = class_dev_iter_next(seqf->private);
Tejun Heo2ac3cee2008-09-03 08:53:37 +0200677 if (dev)
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -0400678 return dev_to_disk(dev);
Tejun Heo2ac3cee2008-09-03 08:53:37 +0200679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 return NULL;
681}
682
Tejun Heodef4e382008-09-03 08:57:12 +0200683static void disk_seqf_stop(struct seq_file *seqf, void *v)
Greg Kroah-Hartman27f30252008-05-22 17:21:08 -0400684{
Tejun Heodef4e382008-09-03 08:57:12 +0200685 struct class_dev_iter *iter = seqf->private;
Greg Kroah-Hartman27f30252008-05-22 17:21:08 -0400686
Tejun Heodef4e382008-09-03 08:57:12 +0200687 /* stop is called even after start failed :-( */
688 if (iter) {
689 class_dev_iter_exit(iter);
690 kfree(iter);
Kay Sievers5c0ef6d2008-08-16 14:30:30 +0200691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692}
693
Tejun Heodef4e382008-09-03 08:57:12 +0200694static void *show_partition_start(struct seq_file *seqf, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
Tejun Heodef4e382008-09-03 08:57:12 +0200696 static void *p;
697
698 p = disk_seqf_start(seqf, pos);
Tejun Heo243294d2008-09-04 09:17:31 +0200699 if (!IS_ERR(p) && p && !*pos)
Tejun Heodef4e382008-09-03 08:57:12 +0200700 seq_puts(seqf, "major minor #blocks name\n\n");
701 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702}
703
Tejun Heocf771cb2008-09-03 09:01:09 +0200704static int show_partition(struct seq_file *seqf, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
706 struct gendisk *sgp = v;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200707 struct disk_part_iter piter;
708 struct hd_struct *part;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 char buf[BDEVNAME_SIZE];
710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 /* Don't show non-partitionable removeable devices or empty devices */
Tejun Heob5d0b9d2008-09-03 09:06:42 +0200712 if (!get_capacity(sgp) || (!disk_partitionable(sgp) &&
Tejun Heof331c022008-09-03 09:01:48 +0200713 (sgp->flags & GENHD_FL_REMOVABLE)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 return 0;
715 if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)
716 return 0;
717
718 /* show the full disk and all non-0 size partitions of it */
Tejun Heo074a7ac2008-08-25 19:56:14 +0900719 disk_part_iter_init(&piter, sgp, DISK_PITER_INCL_PART0);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200720 while ((part = disk_part_iter_next(&piter)))
Tejun Heo1f014292008-08-25 19:47:23 +0900721 seq_printf(seqf, "%4d %7d %10llu %s\n",
Tejun Heof331c022008-09-03 09:01:48 +0200722 MAJOR(part_devt(part)), MINOR(part_devt(part)),
723 (unsigned long long)part->nr_sects >> 1,
724 disk_name(sgp, part->partno, buf));
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200725 disk_part_iter_exit(&piter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
727 return 0;
728}
729
Jan Engelhardt12f32bb2008-01-29 20:57:51 +0100730const struct seq_operations partitions_op = {
Tejun Heodef4e382008-09-03 08:57:12 +0200731 .start = show_partition_start,
732 .next = disk_seqf_next,
733 .stop = disk_seqf_stop,
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200734 .show = show_partition
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735};
736#endif
737
738
Tejun Heocf771cb2008-09-03 09:01:09 +0200739static struct kobject *base_probe(dev_t devt, int *partno, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200741 if (request_module("block-major-%d-%d", MAJOR(devt), MINOR(devt)) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 /* Make old-style 2.4 aliases work */
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200743 request_module("block-major-%d", MAJOR(devt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 return NULL;
745}
746
747static int __init genhd_device_init(void)
748{
Dan Williamse105b8b2008-04-21 10:51:07 -0700749 int error;
750
751 block_class.dev_kobj = sysfs_dev_block_kobj;
752 error = class_register(&block_class);
Roland McGrathee27a552008-03-11 17:13:15 -0700753 if (unlikely(error))
754 return error;
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200755 bdev_map = kobj_map_init(base_probe, &block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 blk_dev_init();
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200757
758#ifndef CONFIG_SYSFS_DEPRECATED
759 /* create top-level block dir */
760 block_depr = kobject_create_and_add("block", NULL);
761#endif
Greg Kroah-Hartman830d3cf2007-11-06 10:36:58 -0800762 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763}
764
765subsys_initcall(genhd_device_init);
766
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200767static ssize_t disk_range_show(struct device *dev,
768 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200770 struct gendisk *disk = dev_to_disk(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200772 return sprintf(buf, "%d\n", disk->minors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773}
774
Tejun Heo1f014292008-08-25 19:47:23 +0900775static ssize_t disk_ext_range_show(struct device *dev,
776 struct device_attribute *attr, char *buf)
777{
778 struct gendisk *disk = dev_to_disk(dev);
779
Tejun Heob5d0b9d2008-09-03 09:06:42 +0200780 return sprintf(buf, "%d\n", disk_max_parts(disk));
Tejun Heo1f014292008-08-25 19:47:23 +0900781}
782
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200783static ssize_t disk_removable_show(struct device *dev,
784 struct device_attribute *attr, char *buf)
Kay Sieversa7fd6702005-10-01 14:49:43 +0200785{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200786 struct gendisk *disk = dev_to_disk(dev);
Kay Sieversa7fd6702005-10-01 14:49:43 +0200787
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200788 return sprintf(buf, "%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200790}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Kay Sievers1c9ce522008-06-13 09:41:00 +0200792static ssize_t disk_ro_show(struct device *dev,
793 struct device_attribute *attr, char *buf)
794{
795 struct gendisk *disk = dev_to_disk(dev);
796
Tejun Heob7db9952008-08-25 19:56:10 +0900797 return sprintf(buf, "%d\n", get_disk_ro(disk) ? 1 : 0);
Kay Sievers1c9ce522008-06-13 09:41:00 +0200798}
799
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200800static ssize_t disk_capability_show(struct device *dev,
801 struct device_attribute *attr, char *buf)
Kristen Carlson Accardi86ce18d2007-05-23 13:57:38 -0700802{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200803 struct gendisk *disk = dev_to_disk(dev);
804
805 return sprintf(buf, "%x\n", disk->flags);
Kristen Carlson Accardi86ce18d2007-05-23 13:57:38 -0700806}
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200807
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200808static DEVICE_ATTR(range, S_IRUGO, disk_range_show, NULL);
Tejun Heo1f014292008-08-25 19:47:23 +0900809static DEVICE_ATTR(ext_range, S_IRUGO, disk_ext_range_show, NULL);
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200810static DEVICE_ATTR(removable, S_IRUGO, disk_removable_show, NULL);
Kay Sievers1c9ce522008-06-13 09:41:00 +0200811static DEVICE_ATTR(ro, S_IRUGO, disk_ro_show, NULL);
Tejun Heoe5610522008-08-25 19:56:09 +0900812static DEVICE_ATTR(size, S_IRUGO, part_size_show, NULL);
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200813static DEVICE_ATTR(capability, S_IRUGO, disk_capability_show, NULL);
Tejun Heo074a7ac2008-08-25 19:56:14 +0900814static DEVICE_ATTR(stat, S_IRUGO, part_stat_show, NULL);
Akinobu Mitac17bb492006-12-08 02:39:46 -0800815#ifdef CONFIG_FAIL_MAKE_REQUEST
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200816static struct device_attribute dev_attr_fail =
Tejun Heoeddb2e22008-08-25 19:56:13 +0900817 __ATTR(make-it-fail, S_IRUGO|S_IWUSR, part_fail_show, part_fail_store);
Akinobu Mitac17bb492006-12-08 02:39:46 -0800818#endif
Jens Axboe581d4e22008-09-14 05:56:33 -0700819#ifdef CONFIG_FAIL_IO_TIMEOUT
820static struct device_attribute dev_attr_fail_timeout =
821 __ATTR(io-timeout-fail, S_IRUGO|S_IWUSR, part_timeout_show,
822 part_timeout_store);
823#endif
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200824
825static struct attribute *disk_attrs[] = {
826 &dev_attr_range.attr,
Tejun Heo1f014292008-08-25 19:47:23 +0900827 &dev_attr_ext_range.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200828 &dev_attr_removable.attr,
Kay Sievers1c9ce522008-06-13 09:41:00 +0200829 &dev_attr_ro.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200830 &dev_attr_size.attr,
831 &dev_attr_capability.attr,
832 &dev_attr_stat.attr,
833#ifdef CONFIG_FAIL_MAKE_REQUEST
834 &dev_attr_fail.attr,
835#endif
Jens Axboe581d4e22008-09-14 05:56:33 -0700836#ifdef CONFIG_FAIL_IO_TIMEOUT
837 &dev_attr_fail_timeout.attr,
838#endif
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200839 NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840};
841
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200842static struct attribute_group disk_attr_group = {
843 .attrs = disk_attrs,
844};
845
846static struct attribute_group *disk_attr_groups[] = {
847 &disk_attr_group,
848 NULL
849};
850
Tejun Heo540eed52008-08-25 19:56:15 +0900851static void disk_free_ptbl_rcu_cb(struct rcu_head *head)
852{
853 struct disk_part_tbl *ptbl =
854 container_of(head, struct disk_part_tbl, rcu_head);
855
856 kfree(ptbl);
857}
858
859/**
860 * disk_replace_part_tbl - replace disk->part_tbl in RCU-safe way
861 * @disk: disk to replace part_tbl for
862 * @new_ptbl: new part_tbl to install
863 *
864 * Replace disk->part_tbl with @new_ptbl in RCU-safe way. The
865 * original ptbl is freed using RCU callback.
866 *
867 * LOCKING:
868 * Matching bd_mutx locked.
869 */
870static void disk_replace_part_tbl(struct gendisk *disk,
871 struct disk_part_tbl *new_ptbl)
872{
873 struct disk_part_tbl *old_ptbl = disk->part_tbl;
874
875 rcu_assign_pointer(disk->part_tbl, new_ptbl);
876 if (old_ptbl)
877 call_rcu(&old_ptbl->rcu_head, disk_free_ptbl_rcu_cb);
878}
879
880/**
881 * disk_expand_part_tbl - expand disk->part_tbl
882 * @disk: disk to expand part_tbl for
883 * @partno: expand such that this partno can fit in
884 *
885 * Expand disk->part_tbl such that @partno can fit in. disk->part_tbl
886 * uses RCU to allow unlocked dereferencing for stats and other stuff.
887 *
888 * LOCKING:
889 * Matching bd_mutex locked, might sleep.
890 *
891 * RETURNS:
892 * 0 on success, -errno on failure.
893 */
894int disk_expand_part_tbl(struct gendisk *disk, int partno)
895{
896 struct disk_part_tbl *old_ptbl = disk->part_tbl;
897 struct disk_part_tbl *new_ptbl;
898 int len = old_ptbl ? old_ptbl->len : 0;
899 int target = partno + 1;
900 size_t size;
901 int i;
902
903 /* disk_max_parts() is zero during initialization, ignore if so */
904 if (disk_max_parts(disk) && target > disk_max_parts(disk))
905 return -EINVAL;
906
907 if (target <= len)
908 return 0;
909
910 size = sizeof(*new_ptbl) + target * sizeof(new_ptbl->part[0]);
911 new_ptbl = kzalloc_node(size, GFP_KERNEL, disk->node_id);
912 if (!new_ptbl)
913 return -ENOMEM;
914
915 INIT_RCU_HEAD(&new_ptbl->rcu_head);
916 new_ptbl->len = target;
917
918 for (i = 0; i < len; i++)
919 rcu_assign_pointer(new_ptbl->part[i], old_ptbl->part[i]);
920
921 disk_replace_part_tbl(disk, new_ptbl);
922 return 0;
923}
924
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200925static void disk_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200927 struct gendisk *disk = dev_to_disk(dev);
928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 kfree(disk->random);
Tejun Heo540eed52008-08-25 19:56:15 +0900930 disk_replace_part_tbl(disk, NULL);
Tejun Heo074a7ac2008-08-25 19:56:14 +0900931 free_part_stats(&disk->part0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 kfree(disk);
933}
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200934struct class block_class = {
935 .name = "block",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936};
937
Adrian Bunk1826ead2008-03-04 11:23:46 +0100938static struct device_type disk_type = {
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200939 .name = "disk",
940 .groups = disk_attr_groups,
941 .release = disk_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942};
943
Randy Dunlapa6e2ba82008-05-23 09:44:11 -0700944#ifdef CONFIG_PROC_FS
Tejun Heocf771cb2008-09-03 09:01:09 +0200945/*
946 * aggregate disk stat collector. Uses the same stats that the sysfs
947 * entries do, above, but makes them available through one seq_file.
948 *
949 * The output looks suspiciously like /proc/partitions with a bunch of
950 * extra fields.
951 */
952static int diskstats_show(struct seq_file *seqf, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
954 struct gendisk *gp = v;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200955 struct disk_part_iter piter;
956 struct hd_struct *hd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 char buf[BDEVNAME_SIZE];
Tejun Heoc9959052008-08-25 19:47:21 +0900958 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
960 /*
Tejun Heoed9e1982008-08-25 19:56:05 +0900961 if (&disk_to_dev(gp)->kobj.entry == block_class.devices.next)
Tejun Heocf771cb2008-09-03 09:01:09 +0200962 seq_puts(seqf, "major minor name"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 " rio rmerge rsect ruse wio wmerge "
964 "wsect wuse running use aveq"
965 "\n\n");
966 */
967
Tejun Heo074a7ac2008-08-25 19:56:14 +0900968 disk_part_iter_init(&piter, gp, DISK_PITER_INCL_PART0);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200969 while ((hd = disk_part_iter_next(&piter))) {
Tejun Heo074a7ac2008-08-25 19:56:14 +0900970 cpu = part_stat_lock();
Tejun Heoc9959052008-08-25 19:47:21 +0900971 part_round_stats(cpu, hd);
Tejun Heo074a7ac2008-08-25 19:56:14 +0900972 part_stat_unlock();
Tejun Heo1f014292008-08-25 19:47:23 +0900973 seq_printf(seqf, "%4d %7d %s %lu %lu %llu "
Jerome Marchand28f39d52008-02-08 11:04:56 +0100974 "%u %lu %lu %llu %u %u %u %u\n",
Tejun Heof331c022008-09-03 09:01:48 +0200975 MAJOR(part_devt(hd)), MINOR(part_devt(hd)),
976 disk_name(gp, hd->partno, buf),
Jerome Marchand28f39d52008-02-08 11:04:56 +0100977 part_stat_read(hd, ios[0]),
978 part_stat_read(hd, merges[0]),
979 (unsigned long long)part_stat_read(hd, sectors[0]),
980 jiffies_to_msecs(part_stat_read(hd, ticks[0])),
981 part_stat_read(hd, ios[1]),
982 part_stat_read(hd, merges[1]),
983 (unsigned long long)part_stat_read(hd, sectors[1]),
984 jiffies_to_msecs(part_stat_read(hd, ticks[1])),
985 hd->in_flight,
986 jiffies_to_msecs(part_stat_read(hd, io_ticks)),
987 jiffies_to_msecs(part_stat_read(hd, time_in_queue))
988 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 }
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200990 disk_part_iter_exit(&piter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
992 return 0;
993}
994
Jan Engelhardt12f32bb2008-01-29 20:57:51 +0100995const struct seq_operations diskstats_op = {
Tejun Heodef4e382008-09-03 08:57:12 +0200996 .start = disk_seqf_start,
997 .next = disk_seqf_next,
998 .stop = disk_seqf_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 .show = diskstats_show
1000};
Randy Dunlapa6e2ba82008-05-23 09:44:11 -07001001#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Kristen Carlson Accardi8ce7ad72007-05-23 13:57:38 -07001003static void media_change_notify_thread(struct work_struct *work)
1004{
1005 struct gendisk *gd = container_of(work, struct gendisk, async_notify);
1006 char event[] = "MEDIA_CHANGE=1";
1007 char *envp[] = { event, NULL };
1008
1009 /*
1010 * set enviroment vars to indicate which event this is for
1011 * so that user space will know to go check the media status.
1012 */
Tejun Heoed9e1982008-08-25 19:56:05 +09001013 kobject_uevent_env(&disk_to_dev(gd)->kobj, KOBJ_CHANGE, envp);
Kristen Carlson Accardi8ce7ad72007-05-23 13:57:38 -07001014 put_device(gd->driverfs_dev);
1015}
1016
Adrian Bunk1826ead2008-03-04 11:23:46 +01001017#if 0
Kristen Carlson Accardi8ce7ad72007-05-23 13:57:38 -07001018void genhd_media_change_notify(struct gendisk *disk)
1019{
1020 get_device(disk->driverfs_dev);
1021 schedule_work(&disk->async_notify);
1022}
1023EXPORT_SYMBOL_GPL(genhd_media_change_notify);
Adrian Bunk1826ead2008-03-04 11:23:46 +01001024#endif /* 0 */
Kristen Carlson Accardi8ce7ad72007-05-23 13:57:38 -07001025
Tejun Heocf771cb2008-09-03 09:01:09 +02001026dev_t blk_lookup_devt(const char *name, int partno)
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001027{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001028 dev_t devt = MKDEV(0, 0);
Tejun Heodef4e382008-09-03 08:57:12 +02001029 struct class_dev_iter iter;
1030 struct device *dev;
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001031
Tejun Heodef4e382008-09-03 08:57:12 +02001032 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
1033 while ((dev = class_dev_iter_next(&iter))) {
1034 struct gendisk *disk = dev_to_disk(dev);
Tejun Heo548b10e2008-08-29 09:01:47 +02001035 struct hd_struct *part;
Tejun Heodef4e382008-09-03 08:57:12 +02001036
Tejun Heof331c022008-09-03 09:01:48 +02001037 if (strcmp(dev->bus_id, name))
1038 continue;
Tejun Heof331c022008-09-03 09:01:48 +02001039
Tejun Heo548b10e2008-08-29 09:01:47 +02001040 part = disk_get_part(disk, partno);
Tejun Heo2bbedcb2008-08-29 11:41:51 +02001041 if (part) {
Tejun Heof331c022008-09-03 09:01:48 +02001042 devt = part_devt(part);
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001043 disk_put_part(part);
Tejun Heo548b10e2008-08-29 09:01:47 +02001044 break;
Tejun Heodef4e382008-09-03 08:57:12 +02001045 }
Tejun Heo548b10e2008-08-29 09:01:47 +02001046 disk_put_part(part);
Kay Sievers5c0ef6d2008-08-16 14:30:30 +02001047 }
Tejun Heodef4e382008-09-03 08:57:12 +02001048 class_dev_iter_exit(&iter);
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001049 return devt;
1050}
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001051EXPORT_SYMBOL(blk_lookup_devt);
1052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053struct gendisk *alloc_disk(int minors)
1054{
Christoph Lameter19460892005-06-23 00:08:19 -07001055 return alloc_disk_node(minors, -1);
1056}
Tejun Heo689d6fa2008-08-25 19:56:16 +09001057EXPORT_SYMBOL(alloc_disk);
Christoph Lameter19460892005-06-23 00:08:19 -07001058
1059struct gendisk *alloc_disk_node(int minors, int node_id)
1060{
1061 struct gendisk *disk;
1062
Christoph Lameter94f60302007-07-17 04:03:29 -07001063 disk = kmalloc_node(sizeof(struct gendisk),
1064 GFP_KERNEL | __GFP_ZERO, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 if (disk) {
Tejun Heo074a7ac2008-08-25 19:56:14 +09001066 if (!init_part_stats(&disk->part0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 kfree(disk);
1068 return NULL;
1069 }
Tejun Heo540eed52008-08-25 19:56:15 +09001070 if (disk_expand_part_tbl(disk, 0)) {
1071 free_part_stats(&disk->part0);
Tejun Heob5d0b9d2008-09-03 09:06:42 +02001072 kfree(disk);
1073 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 }
Tejun Heo540eed52008-08-25 19:56:15 +09001075 disk->part_tbl->part[0] = &disk->part0;
Tejun Heob5d0b9d2008-09-03 09:06:42 +02001076
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 disk->minors = minors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 rand_initialize_disk(disk);
Tejun Heoed9e1982008-08-25 19:56:05 +09001079 disk_to_dev(disk)->class = &block_class;
1080 disk_to_dev(disk)->type = &disk_type;
1081 device_initialize(disk_to_dev(disk));
Kristen Carlson Accardi8ce7ad72007-05-23 13:57:38 -07001082 INIT_WORK(&disk->async_notify,
1083 media_change_notify_thread);
Tejun Heo540eed52008-08-25 19:56:15 +09001084 disk->node_id = node_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 }
1086 return disk;
1087}
Christoph Lameter19460892005-06-23 00:08:19 -07001088EXPORT_SYMBOL(alloc_disk_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
1090struct kobject *get_disk(struct gendisk *disk)
1091{
1092 struct module *owner;
1093 struct kobject *kobj;
1094
1095 if (!disk->fops)
1096 return NULL;
1097 owner = disk->fops->owner;
1098 if (owner && !try_module_get(owner))
1099 return NULL;
Tejun Heoed9e1982008-08-25 19:56:05 +09001100 kobj = kobject_get(&disk_to_dev(disk)->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 if (kobj == NULL) {
1102 module_put(owner);
1103 return NULL;
1104 }
1105 return kobj;
1106
1107}
1108
1109EXPORT_SYMBOL(get_disk);
1110
1111void put_disk(struct gendisk *disk)
1112{
1113 if (disk)
Tejun Heoed9e1982008-08-25 19:56:05 +09001114 kobject_put(&disk_to_dev(disk)->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115}
1116
1117EXPORT_SYMBOL(put_disk);
1118
1119void set_device_ro(struct block_device *bdev, int flag)
1120{
Tejun Heob7db9952008-08-25 19:56:10 +09001121 bdev->bd_part->policy = flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122}
1123
1124EXPORT_SYMBOL(set_device_ro);
1125
1126void set_disk_ro(struct gendisk *disk, int flag)
1127{
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001128 struct disk_part_iter piter;
1129 struct hd_struct *part;
1130
Tejun Heob7db9952008-08-25 19:56:10 +09001131 disk_part_iter_init(&piter, disk,
1132 DISK_PITER_INCL_EMPTY | DISK_PITER_INCL_PART0);
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001133 while ((part = disk_part_iter_next(&piter)))
1134 part->policy = flag;
1135 disk_part_iter_exit(&piter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136}
1137
1138EXPORT_SYMBOL(set_disk_ro);
1139
1140int bdev_read_only(struct block_device *bdev)
1141{
1142 if (!bdev)
1143 return 0;
Tejun Heob7db9952008-08-25 19:56:10 +09001144 return bdev->bd_part->policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145}
1146
1147EXPORT_SYMBOL(bdev_read_only);
1148
Tejun Heocf771cb2008-09-03 09:01:09 +02001149int invalidate_partition(struct gendisk *disk, int partno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150{
1151 int res = 0;
Tejun Heocf771cb2008-09-03 09:01:09 +02001152 struct block_device *bdev = bdget_disk(disk, partno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 if (bdev) {
Christoph Hellwig2ef41632005-05-05 16:15:59 -07001154 fsync_bdev(bdev);
1155 res = __invalidate_device(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 bdput(bdev);
1157 }
1158 return res;
1159}
1160
1161EXPORT_SYMBOL(invalidate_partition);