blob: 7923e720ddf52f1626cf5422ef5271fd9c7395d6 [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>
Alexey Dobriyanf5009752008-10-04 23:53:21 +040013#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/seq_file.h>
15#include <linux/slab.h>
16#include <linux/kmod.h>
17#include <linux/kobj_map.h>
Christoph Hellwig2ef41632005-05-05 16:15:59 -070018#include <linux/buffer_head.h>
Jes Sorensen58383af2006-02-06 14:12:43 -080019#include <linux/mutex.h>
Tejun Heobcce3de2008-08-25 19:47:22 +090020#include <linux/idr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Adrian Bunkff889722008-03-04 11:23:45 +010022#include "blk.h"
23
Kay Sieversedfaa7c2007-05-21 22:08:01 +020024static DEFINE_MUTEX(block_class_lock);
25#ifndef CONFIG_SYSFS_DEPRECATED
26struct kobject *block_depr;
27#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Tejun Heobcce3de2008-08-25 19:47:22 +090029/* for extended dynamic devt allocation, currently only one major is used */
30#define MAX_EXT_DEVT (1 << MINORBITS)
31
32/* For extended devt allocation. ext_devt_mutex prevents look up
33 * results from going away underneath its user.
34 */
35static DEFINE_MUTEX(ext_devt_mutex);
36static DEFINE_IDR(ext_devt_idr);
37
Adrian Bunk1826ead2008-03-04 11:23:46 +010038static struct device_type disk_type;
39
Tejun Heoe71bf0d2008-09-03 09:03:02 +020040/**
41 * disk_get_part - get partition
42 * @disk: disk to look partition from
43 * @partno: partition number
44 *
45 * Look for partition @partno from @disk. If found, increment
46 * reference count and return it.
47 *
48 * CONTEXT:
49 * Don't care.
50 *
51 * RETURNS:
52 * Pointer to the found partition on success, NULL if not found.
53 */
54struct hd_struct *disk_get_part(struct gendisk *disk, int partno)
55{
Tejun Heo540eed52008-08-25 19:56:15 +090056 struct hd_struct *part = NULL;
57 struct disk_part_tbl *ptbl;
Tejun Heoe71bf0d2008-09-03 09:03:02 +020058
Tejun Heo540eed52008-08-25 19:56:15 +090059 if (unlikely(partno < 0))
Tejun Heoe71bf0d2008-09-03 09:03:02 +020060 return NULL;
Tejun Heo540eed52008-08-25 19:56:15 +090061
Tejun Heoe71bf0d2008-09-03 09:03:02 +020062 rcu_read_lock();
Tejun Heo540eed52008-08-25 19:56:15 +090063
64 ptbl = rcu_dereference(disk->part_tbl);
65 if (likely(partno < ptbl->len)) {
66 part = rcu_dereference(ptbl->part[partno]);
67 if (part)
68 get_device(part_to_dev(part));
69 }
70
Tejun Heoe71bf0d2008-09-03 09:03:02 +020071 rcu_read_unlock();
72
73 return part;
74}
75EXPORT_SYMBOL_GPL(disk_get_part);
76
77/**
78 * disk_part_iter_init - initialize partition iterator
79 * @piter: iterator to initialize
80 * @disk: disk to iterate over
81 * @flags: DISK_PITER_* flags
82 *
83 * Initialize @piter so that it iterates over partitions of @disk.
84 *
85 * CONTEXT:
86 * Don't care.
87 */
88void disk_part_iter_init(struct disk_part_iter *piter, struct gendisk *disk,
89 unsigned int flags)
90{
Tejun Heo540eed52008-08-25 19:56:15 +090091 struct disk_part_tbl *ptbl;
92
93 rcu_read_lock();
94 ptbl = rcu_dereference(disk->part_tbl);
95
Tejun Heoe71bf0d2008-09-03 09:03:02 +020096 piter->disk = disk;
97 piter->part = NULL;
98
99 if (flags & DISK_PITER_REVERSE)
Tejun Heo540eed52008-08-25 19:56:15 +0900100 piter->idx = ptbl->len - 1;
Tejun Heo71982a42009-04-17 08:34:48 +0200101 else if (flags & (DISK_PITER_INCL_PART0 | DISK_PITER_INCL_EMPTY_PART0))
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200102 piter->idx = 0;
Tejun Heob5d0b9d2008-09-03 09:06:42 +0200103 else
104 piter->idx = 1;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200105
106 piter->flags = flags;
Tejun Heo540eed52008-08-25 19:56:15 +0900107
108 rcu_read_unlock();
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200109}
110EXPORT_SYMBOL_GPL(disk_part_iter_init);
111
112/**
113 * disk_part_iter_next - proceed iterator to the next partition and return it
114 * @piter: iterator of interest
115 *
116 * Proceed @piter to the next partition and return it.
117 *
118 * CONTEXT:
119 * Don't care.
120 */
121struct hd_struct *disk_part_iter_next(struct disk_part_iter *piter)
122{
Tejun Heo540eed52008-08-25 19:56:15 +0900123 struct disk_part_tbl *ptbl;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200124 int inc, end;
125
126 /* put the last partition */
127 disk_put_part(piter->part);
128 piter->part = NULL;
129
Tejun Heo540eed52008-08-25 19:56:15 +0900130 /* get part_tbl */
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200131 rcu_read_lock();
Tejun Heo540eed52008-08-25 19:56:15 +0900132 ptbl = rcu_dereference(piter->disk->part_tbl);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200133
134 /* determine iteration parameters */
135 if (piter->flags & DISK_PITER_REVERSE) {
136 inc = -1;
Tejun Heo71982a42009-04-17 08:34:48 +0200137 if (piter->flags & (DISK_PITER_INCL_PART0 |
138 DISK_PITER_INCL_EMPTY_PART0))
Tejun Heob5d0b9d2008-09-03 09:06:42 +0200139 end = -1;
140 else
141 end = 0;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200142 } else {
143 inc = 1;
Tejun Heo540eed52008-08-25 19:56:15 +0900144 end = ptbl->len;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200145 }
146
147 /* iterate to the next partition */
148 for (; piter->idx != end; piter->idx += inc) {
149 struct hd_struct *part;
150
Tejun Heo540eed52008-08-25 19:56:15 +0900151 part = rcu_dereference(ptbl->part[piter->idx]);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200152 if (!part)
153 continue;
Tejun Heo71982a42009-04-17 08:34:48 +0200154 if (!part->nr_sects &&
155 !(piter->flags & DISK_PITER_INCL_EMPTY) &&
156 !(piter->flags & DISK_PITER_INCL_EMPTY_PART0 &&
157 piter->idx == 0))
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200158 continue;
159
Tejun Heoed9e1982008-08-25 19:56:05 +0900160 get_device(part_to_dev(part));
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200161 piter->part = part;
162 piter->idx += inc;
163 break;
164 }
165
166 rcu_read_unlock();
167
168 return piter->part;
169}
170EXPORT_SYMBOL_GPL(disk_part_iter_next);
171
172/**
173 * disk_part_iter_exit - finish up partition iteration
174 * @piter: iter of interest
175 *
176 * Called when iteration is over. Cleans up @piter.
177 *
178 * CONTEXT:
179 * Don't care.
180 */
181void disk_part_iter_exit(struct disk_part_iter *piter)
182{
183 disk_put_part(piter->part);
184 piter->part = NULL;
185}
186EXPORT_SYMBOL_GPL(disk_part_iter_exit);
187
Jens Axboea6f23652008-10-24 12:52:42 +0200188static inline int sector_in_part(struct hd_struct *part, sector_t sector)
189{
190 return part->start_sect <= sector &&
191 sector < part->start_sect + part->nr_sects;
192}
193
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200194/**
195 * disk_map_sector_rcu - map sector to partition
196 * @disk: gendisk of interest
197 * @sector: sector to map
198 *
199 * Find out which partition @sector maps to on @disk. This is
200 * primarily used for stats accounting.
201 *
202 * CONTEXT:
203 * RCU read locked. The returned partition pointer is valid only
204 * while preemption is disabled.
205 *
206 * RETURNS:
Tejun Heo074a7ac2008-08-25 19:56:14 +0900207 * Found partition on success, part0 is returned if no partition matches
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200208 */
209struct hd_struct *disk_map_sector_rcu(struct gendisk *disk, sector_t sector)
210{
Tejun Heo540eed52008-08-25 19:56:15 +0900211 struct disk_part_tbl *ptbl;
Jens Axboea6f23652008-10-24 12:52:42 +0200212 struct hd_struct *part;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200213 int i;
214
Tejun Heo540eed52008-08-25 19:56:15 +0900215 ptbl = rcu_dereference(disk->part_tbl);
216
Jens Axboea6f23652008-10-24 12:52:42 +0200217 part = rcu_dereference(ptbl->last_lookup);
218 if (part && sector_in_part(part, sector))
219 return part;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200220
Jens Axboea6f23652008-10-24 12:52:42 +0200221 for (i = 1; i < ptbl->len; i++) {
222 part = rcu_dereference(ptbl->part[i]);
223
224 if (part && sector_in_part(part, sector)) {
225 rcu_assign_pointer(ptbl->last_lookup, part);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200226 return part;
Jens Axboea6f23652008-10-24 12:52:42 +0200227 }
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200228 }
Tejun Heo074a7ac2008-08-25 19:56:14 +0900229 return &disk->part0;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200230}
231EXPORT_SYMBOL_GPL(disk_map_sector_rcu);
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233/*
234 * Can be deleted altogether. Later.
235 *
236 */
237static struct blk_major_name {
238 struct blk_major_name *next;
239 int major;
240 char name[16];
Joe Korty68eef3b2006-03-31 02:30:32 -0800241} *major_names[BLKDEV_MAJOR_HASH_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243/* index in the above - for now: assume no multimajor ranges */
244static inline int major_to_index(int major)
245{
Joe Korty68eef3b2006-03-31 02:30:32 -0800246 return major % BLKDEV_MAJOR_HASH_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247}
248
Joe Korty68eef3b2006-03-31 02:30:32 -0800249#ifdef CONFIG_PROC_FS
Tejun Heocf771cb2008-09-03 09:01:09 +0200250void blkdev_show(struct seq_file *seqf, off_t offset)
Neil Horman7170be52006-01-14 13:20:38 -0800251{
Joe Korty68eef3b2006-03-31 02:30:32 -0800252 struct blk_major_name *dp;
Neil Horman7170be52006-01-14 13:20:38 -0800253
Joe Korty68eef3b2006-03-31 02:30:32 -0800254 if (offset < BLKDEV_MAJOR_HASH_SIZE) {
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200255 mutex_lock(&block_class_lock);
Joe Korty68eef3b2006-03-31 02:30:32 -0800256 for (dp = major_names[offset]; dp; dp = dp->next)
Tejun Heocf771cb2008-09-03 09:01:09 +0200257 seq_printf(seqf, "%3d %s\n", dp->major, dp->name);
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200258 mutex_unlock(&block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
Joe Korty68eef3b2006-03-31 02:30:32 -0800261#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Márton Németh9e8c0bc2009-02-20 08:12:51 +0100263/**
264 * register_blkdev - register a new block device
265 *
266 * @major: the requested major device number [1..255]. If @major=0, try to
267 * allocate any unused major number.
268 * @name: the name of the new block device as a zero terminated string
269 *
270 * The @name must be unique within the system.
271 *
272 * The return value depends on the @major input parameter.
273 * - if a major device number was requested in range [1..255] then the
274 * function returns zero on success, or a negative error code
275 * - if any unused major number was requested with @major=0 parameter
276 * then the return value is the allocated major number in range
277 * [1..255] or a negative error code otherwise
278 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279int register_blkdev(unsigned int major, const char *name)
280{
281 struct blk_major_name **n, *p;
282 int index, ret = 0;
283
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200284 mutex_lock(&block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 /* temporary */
287 if (major == 0) {
288 for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) {
289 if (major_names[index] == NULL)
290 break;
291 }
292
293 if (index == 0) {
294 printk("register_blkdev: failed to get major for %s\n",
295 name);
296 ret = -EBUSY;
297 goto out;
298 }
299 major = index;
300 ret = major;
301 }
302
303 p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL);
304 if (p == NULL) {
305 ret = -ENOMEM;
306 goto out;
307 }
308
309 p->major = major;
310 strlcpy(p->name, name, sizeof(p->name));
311 p->next = NULL;
312 index = major_to_index(major);
313
314 for (n = &major_names[index]; *n; n = &(*n)->next) {
315 if ((*n)->major == major)
316 break;
317 }
318 if (!*n)
319 *n = p;
320 else
321 ret = -EBUSY;
322
323 if (ret < 0) {
324 printk("register_blkdev: cannot get major %d for %s\n",
325 major, name);
326 kfree(p);
327 }
328out:
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200329 mutex_unlock(&block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 return ret;
331}
332
333EXPORT_SYMBOL(register_blkdev);
334
Akinobu Mitaf4480242007-07-17 04:03:47 -0700335void unregister_blkdev(unsigned int major, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
337 struct blk_major_name **n;
338 struct blk_major_name *p = NULL;
339 int index = major_to_index(major);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200341 mutex_lock(&block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 for (n = &major_names[index]; *n; n = &(*n)->next)
343 if ((*n)->major == major)
344 break;
Akinobu Mita294462a2007-07-17 04:03:45 -0700345 if (!*n || strcmp((*n)->name, name)) {
346 WARN_ON(1);
Akinobu Mita294462a2007-07-17 04:03:45 -0700347 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 p = *n;
349 *n = p->next;
350 }
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200351 mutex_unlock(&block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
354
355EXPORT_SYMBOL(unregister_blkdev);
356
357static struct kobj_map *bdev_map;
358
Tejun Heobcce3de2008-08-25 19:47:22 +0900359/**
Tejun Heo870d6652008-08-25 19:47:25 +0900360 * blk_mangle_minor - scatter minor numbers apart
361 * @minor: minor number to mangle
362 *
363 * Scatter consecutively allocated @minor number apart if MANGLE_DEVT
364 * is enabled. Mangling twice gives the original value.
365 *
366 * RETURNS:
367 * Mangled value.
368 *
369 * CONTEXT:
370 * Don't care.
371 */
372static int blk_mangle_minor(int minor)
373{
374#ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT
375 int i;
376
377 for (i = 0; i < MINORBITS / 2; i++) {
378 int low = minor & (1 << i);
379 int high = minor & (1 << (MINORBITS - 1 - i));
380 int distance = MINORBITS - 1 - 2 * i;
381
382 minor ^= low | high; /* clear both bits */
383 low <<= distance; /* swap the positions */
384 high >>= distance;
385 minor |= low | high; /* and set */
386 }
387#endif
388 return minor;
389}
390
391/**
Tejun Heobcce3de2008-08-25 19:47:22 +0900392 * blk_alloc_devt - allocate a dev_t for a partition
393 * @part: partition to allocate dev_t for
Tejun Heobcce3de2008-08-25 19:47:22 +0900394 * @devt: out parameter for resulting dev_t
395 *
396 * Allocate a dev_t for block device.
397 *
398 * RETURNS:
399 * 0 on success, allocated dev_t is returned in *@devt. -errno on
400 * failure.
401 *
402 * CONTEXT:
403 * Might sleep.
404 */
405int blk_alloc_devt(struct hd_struct *part, dev_t *devt)
406{
407 struct gendisk *disk = part_to_disk(part);
408 int idx, rc;
409
410 /* in consecutive minor range? */
411 if (part->partno < disk->minors) {
412 *devt = MKDEV(disk->major, disk->first_minor + part->partno);
413 return 0;
414 }
415
416 /* allocate ext devt */
417 do {
418 if (!idr_pre_get(&ext_devt_idr, GFP_KERNEL))
419 return -ENOMEM;
420 rc = idr_get_new(&ext_devt_idr, part, &idx);
421 } while (rc == -EAGAIN);
422
423 if (rc)
424 return rc;
425
426 if (idx > MAX_EXT_DEVT) {
427 idr_remove(&ext_devt_idr, idx);
428 return -EBUSY;
429 }
430
Tejun Heo870d6652008-08-25 19:47:25 +0900431 *devt = MKDEV(BLOCK_EXT_MAJOR, blk_mangle_minor(idx));
Tejun Heobcce3de2008-08-25 19:47:22 +0900432 return 0;
433}
434
435/**
436 * blk_free_devt - free a dev_t
437 * @devt: dev_t to free
438 *
439 * Free @devt which was allocated using blk_alloc_devt().
440 *
441 * CONTEXT:
442 * Might sleep.
443 */
444void blk_free_devt(dev_t devt)
445{
446 might_sleep();
447
448 if (devt == MKDEV(0, 0))
449 return;
450
451 if (MAJOR(devt) == BLOCK_EXT_MAJOR) {
452 mutex_lock(&ext_devt_mutex);
Tejun Heo870d6652008-08-25 19:47:25 +0900453 idr_remove(&ext_devt_idr, blk_mangle_minor(MINOR(devt)));
Tejun Heobcce3de2008-08-25 19:47:22 +0900454 mutex_unlock(&ext_devt_mutex);
455 }
456}
457
Tejun Heo1f014292008-08-25 19:47:23 +0900458static char *bdevt_str(dev_t devt, char *buf)
459{
460 if (MAJOR(devt) <= 0xff && MINOR(devt) <= 0xff) {
461 char tbuf[BDEVT_SIZE];
462 snprintf(tbuf, BDEVT_SIZE, "%02x%02x", MAJOR(devt), MINOR(devt));
463 snprintf(buf, BDEVT_SIZE, "%-9s", tbuf);
464 } else
465 snprintf(buf, BDEVT_SIZE, "%03x:%05x", MAJOR(devt), MINOR(devt));
466
467 return buf;
468}
469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470/*
471 * Register device numbers dev..(dev+range-1)
472 * range must be nonzero
473 * The hash chain is sorted on range, so that subranges can override.
474 */
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200475void blk_register_region(dev_t devt, unsigned long range, struct module *module,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 struct kobject *(*probe)(dev_t, int *, void *),
477 int (*lock)(dev_t, void *), void *data)
478{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200479 kobj_map(bdev_map, devt, range, module, probe, lock, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480}
481
482EXPORT_SYMBOL(blk_register_region);
483
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200484void blk_unregister_region(dev_t devt, unsigned long range)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200486 kobj_unmap(bdev_map, devt, range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487}
488
489EXPORT_SYMBOL(blk_unregister_region);
490
Tejun Heocf771cb2008-09-03 09:01:09 +0200491static struct kobject *exact_match(dev_t devt, int *partno, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
493 struct gendisk *p = data;
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200494
Tejun Heoed9e1982008-08-25 19:56:05 +0900495 return &disk_to_dev(p)->kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496}
497
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200498static int exact_lock(dev_t devt, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
500 struct gendisk *p = data;
501
502 if (!get_disk(p))
503 return -1;
504 return 0;
505}
506
507/**
508 * add_disk - add partitioning information to kernel list
509 * @disk: per-device partitioning information
510 *
511 * This function registers the partitioning information in @disk
512 * with the kernel.
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900513 *
514 * FIXME: error handling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 */
516void add_disk(struct gendisk *disk)
517{
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700518 struct backing_dev_info *bdi;
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900519 dev_t devt;
Greg Kroah-Hartman6ffeea72008-05-22 17:21:08 -0400520 int retval;
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700521
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900522 /* minors == 0 indicates to use ext devt from part0 and should
523 * be accompanied with EXT_DEVT flag. Make sure all
524 * parameters make sense.
525 */
526 WARN_ON(disk->minors && !(disk->major || disk->first_minor));
527 WARN_ON(!disk->minors && !(disk->flags & GENHD_FL_EXT_DEVT));
528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 disk->flags |= GENHD_FL_UP;
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900530
531 retval = blk_alloc_devt(&disk->part0, &devt);
532 if (retval) {
533 WARN_ON(1);
534 return;
535 }
536 disk_to_dev(disk)->devt = devt;
537
538 /* ->major and ->first_minor aren't supposed to be
539 * dereferenced from here on, but set them just in case.
540 */
541 disk->major = MAJOR(devt);
542 disk->first_minor = MINOR(devt);
543
Signed-off-by: Jan Kara01ea5062010-09-16 20:36:36 +0200544 /* Register BDI before referencing it from bdev */
545 bdi = &disk->queue->backing_dev_info;
546 bdi_register_dev(bdi, disk_devt(disk));
547
Tejun Heof331c022008-09-03 09:01:48 +0200548 blk_register_region(disk_devt(disk), disk->minors, NULL,
549 exact_match, exact_lock, disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 register_disk(disk);
551 blk_register_queue(disk);
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700552
Tejun Heoed9e1982008-08-25 19:56:05 +0900553 retval = sysfs_create_link(&disk_to_dev(disk)->kobj, &bdi->dev->kobj,
554 "bdi");
Greg Kroah-Hartman6ffeea72008-05-22 17:21:08 -0400555 WARN_ON(retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556}
557
558EXPORT_SYMBOL(add_disk);
559EXPORT_SYMBOL(del_gendisk); /* in partitions/check.c */
560
561void unlink_gendisk(struct gendisk *disk)
562{
Tejun Heoed9e1982008-08-25 19:56:05 +0900563 sysfs_remove_link(&disk_to_dev(disk)->kobj, "bdi");
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700564 bdi_unregister(&disk->queue->backing_dev_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 blk_unregister_queue(disk);
Tejun Heof331c022008-09-03 09:01:48 +0200566 blk_unregister_region(disk_devt(disk), disk->minors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567}
568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569/**
570 * get_gendisk - get partitioning information for a given device
Randy Dunlap710027a2008-08-19 20:13:11 +0200571 * @devt: device to get partitioning information for
Randy Dunlap496aa8a2008-10-16 07:46:23 +0200572 * @partno: returned partition index
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 *
574 * This function gets the structure containing partitioning
Randy Dunlap710027a2008-08-19 20:13:11 +0200575 * information for the given device @devt.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 */
Tejun Heocf771cb2008-09-03 09:01:09 +0200577struct gendisk *get_gendisk(dev_t devt, int *partno)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
Tejun Heobcce3de2008-08-25 19:47:22 +0900579 struct gendisk *disk = NULL;
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200580
Tejun Heobcce3de2008-08-25 19:47:22 +0900581 if (MAJOR(devt) != BLOCK_EXT_MAJOR) {
582 struct kobject *kobj;
583
584 kobj = kobj_lookup(bdev_map, devt, partno);
585 if (kobj)
586 disk = dev_to_disk(kobj_to_dev(kobj));
587 } else {
588 struct hd_struct *part;
589
590 mutex_lock(&ext_devt_mutex);
Tejun Heo870d6652008-08-25 19:47:25 +0900591 part = idr_find(&ext_devt_idr, blk_mangle_minor(MINOR(devt)));
Tejun Heobcce3de2008-08-25 19:47:22 +0900592 if (part && get_disk(part_to_disk(part))) {
593 *partno = part->partno;
594 disk = part_to_disk(part);
595 }
596 mutex_unlock(&ext_devt_mutex);
597 }
598
599 return disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600}
Divyesh Shahb6ac23af2010-04-15 08:54:59 +0200601EXPORT_SYMBOL(get_gendisk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Tejun Heof331c022008-09-03 09:01:48 +0200603/**
604 * bdget_disk - do bdget() by gendisk and partition number
605 * @disk: gendisk of interest
606 * @partno: partition number
607 *
608 * Find partition @partno from @disk, do bdget() on it.
609 *
610 * CONTEXT:
611 * Don't care.
612 *
613 * RETURNS:
614 * Resulting block_device on success, NULL on failure.
615 */
Harvey Harrisonaeb3d3a2008-08-28 09:27:42 +0200616struct block_device *bdget_disk(struct gendisk *disk, int partno)
Tejun Heof331c022008-09-03 09:01:48 +0200617{
Tejun Heo548b10e2008-08-29 09:01:47 +0200618 struct hd_struct *part;
619 struct block_device *bdev = NULL;
Tejun Heof331c022008-09-03 09:01:48 +0200620
Tejun Heo548b10e2008-08-29 09:01:47 +0200621 part = disk_get_part(disk, partno);
Tejun Heo2bbedcb2008-08-29 11:41:51 +0200622 if (part)
Tejun Heo548b10e2008-08-29 09:01:47 +0200623 bdev = bdget(part_devt(part));
624 disk_put_part(part);
Tejun Heof331c022008-09-03 09:01:48 +0200625
Tejun Heo548b10e2008-08-29 09:01:47 +0200626 return bdev;
Tejun Heof331c022008-09-03 09:01:48 +0200627}
628EXPORT_SYMBOL(bdget_disk);
629
Dave Gilbertdd2a3452007-05-09 02:33:24 -0700630/*
631 * print a full list of all partitions - intended for places where the root
632 * filesystem can't be mounted and thus to give the victim some idea of what
633 * went wrong
634 */
635void __init printk_all_partitions(void)
636{
Tejun Heodef4e382008-09-03 08:57:12 +0200637 struct class_dev_iter iter;
638 struct device *dev;
639
640 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
641 while ((dev = class_dev_iter_next(&iter))) {
642 struct gendisk *disk = dev_to_disk(dev);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200643 struct disk_part_iter piter;
644 struct hd_struct *part;
Tejun Heo1f014292008-08-25 19:47:23 +0900645 char name_buf[BDEVNAME_SIZE];
646 char devt_buf[BDEVT_SIZE];
Will Drewryb5af9212010-08-31 15:47:07 -0500647 u8 uuid[PARTITION_META_INFO_UUIDLTH * 2 + 1];
Tejun Heodef4e382008-09-03 08:57:12 +0200648
649 /*
650 * Don't show empty devices or things that have been
651 * surpressed
652 */
653 if (get_capacity(disk) == 0 ||
654 (disk->flags & GENHD_FL_SUPPRESS_PARTITION_INFO))
655 continue;
656
657 /*
658 * Note, unlike /proc/partitions, I am showing the
659 * numbers in hex - the same format as the root=
660 * option takes.
661 */
Tejun Heo074a7ac2008-08-25 19:56:14 +0900662 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
663 while ((part = disk_part_iter_next(&piter))) {
664 bool is_part0 = part == &disk->part0;
Tejun Heodef4e382008-09-03 08:57:12 +0200665
Will Drewryb5af9212010-08-31 15:47:07 -0500666 uuid[0] = 0;
667 if (part->info)
668 part_unpack_uuid(part->info->uuid, uuid);
669
670 printk("%s%s %10llu %s %s", is_part0 ? "" : " ",
Tejun Heo1f014292008-08-25 19:47:23 +0900671 bdevt_str(part_devt(part), devt_buf),
Tejun Heof331c022008-09-03 09:01:48 +0200672 (unsigned long long)part->nr_sects >> 1,
Will Drewryb5af9212010-08-31 15:47:07 -0500673 disk_name(disk, part->partno, name_buf), uuid);
Tejun Heo074a7ac2008-08-25 19:56:14 +0900674 if (is_part0) {
675 if (disk->driverfs_dev != NULL &&
676 disk->driverfs_dev->driver != NULL)
677 printk(" driver: %s\n",
678 disk->driverfs_dev->driver->name);
679 else
680 printk(" (driver?)\n");
681 } else
682 printk("\n");
683 }
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200684 disk_part_iter_exit(&piter);
Tejun Heodef4e382008-09-03 08:57:12 +0200685 }
686 class_dev_iter_exit(&iter);
Dave Gilbertdd2a3452007-05-09 02:33:24 -0700687}
688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689#ifdef CONFIG_PROC_FS
690/* iterator */
Tejun Heodef4e382008-09-03 08:57:12 +0200691static void *disk_seqf_start(struct seq_file *seqf, loff_t *pos)
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -0400692{
Tejun Heodef4e382008-09-03 08:57:12 +0200693 loff_t skip = *pos;
694 struct class_dev_iter *iter;
695 struct device *dev;
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -0400696
Harvey Harrisonaeb3d3a2008-08-28 09:27:42 +0200697 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
Tejun Heodef4e382008-09-03 08:57:12 +0200698 if (!iter)
699 return ERR_PTR(-ENOMEM);
700
701 seqf->private = iter;
702 class_dev_iter_init(iter, &block_class, NULL, &disk_type);
703 do {
704 dev = class_dev_iter_next(iter);
705 if (!dev)
706 return NULL;
707 } while (skip--);
708
709 return dev_to_disk(dev);
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -0400710}
711
Tejun Heodef4e382008-09-03 08:57:12 +0200712static void *disk_seqf_next(struct seq_file *seqf, void *v, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200714 struct device *dev;
Greg Kroah-Hartman66c64af2008-05-22 17:21:08 -0400715
Tejun Heodef4e382008-09-03 08:57:12 +0200716 (*pos)++;
717 dev = class_dev_iter_next(seqf->private);
Tejun Heo2ac3cee2008-09-03 08:53:37 +0200718 if (dev)
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -0400719 return dev_to_disk(dev);
Tejun Heo2ac3cee2008-09-03 08:53:37 +0200720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 return NULL;
722}
723
Tejun Heodef4e382008-09-03 08:57:12 +0200724static void disk_seqf_stop(struct seq_file *seqf, void *v)
Greg Kroah-Hartman27f30252008-05-22 17:21:08 -0400725{
Tejun Heodef4e382008-09-03 08:57:12 +0200726 struct class_dev_iter *iter = seqf->private;
Greg Kroah-Hartman27f30252008-05-22 17:21:08 -0400727
Tejun Heodef4e382008-09-03 08:57:12 +0200728 /* stop is called even after start failed :-( */
729 if (iter) {
730 class_dev_iter_exit(iter);
731 kfree(iter);
Kay Sievers5c0ef6d2008-08-16 14:30:30 +0200732 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733}
734
Tejun Heodef4e382008-09-03 08:57:12 +0200735static void *show_partition_start(struct seq_file *seqf, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
Tejun Heodef4e382008-09-03 08:57:12 +0200737 static void *p;
738
739 p = disk_seqf_start(seqf, pos);
Tejun Heo243294d2008-09-04 09:17:31 +0200740 if (!IS_ERR(p) && p && !*pos)
Tejun Heodef4e382008-09-03 08:57:12 +0200741 seq_puts(seqf, "major minor #blocks name\n\n");
742 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743}
744
Tejun Heocf771cb2008-09-03 09:01:09 +0200745static int show_partition(struct seq_file *seqf, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
747 struct gendisk *sgp = v;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200748 struct disk_part_iter piter;
749 struct hd_struct *part;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 char buf[BDEVNAME_SIZE];
751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 /* Don't show non-partitionable removeable devices or empty devices */
Tejun Heob5d0b9d2008-09-03 09:06:42 +0200753 if (!get_capacity(sgp) || (!disk_partitionable(sgp) &&
Tejun Heof331c022008-09-03 09:01:48 +0200754 (sgp->flags & GENHD_FL_REMOVABLE)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 return 0;
756 if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)
757 return 0;
758
759 /* show the full disk and all non-0 size partitions of it */
Tejun Heo074a7ac2008-08-25 19:56:14 +0900760 disk_part_iter_init(&piter, sgp, DISK_PITER_INCL_PART0);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200761 while ((part = disk_part_iter_next(&piter)))
Tejun Heo1f014292008-08-25 19:47:23 +0900762 seq_printf(seqf, "%4d %7d %10llu %s\n",
Tejun Heof331c022008-09-03 09:01:48 +0200763 MAJOR(part_devt(part)), MINOR(part_devt(part)),
764 (unsigned long long)part->nr_sects >> 1,
765 disk_name(sgp, part->partno, buf));
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200766 disk_part_iter_exit(&piter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
768 return 0;
769}
770
Alexey Dobriyanf5009752008-10-04 23:53:21 +0400771static const struct seq_operations partitions_op = {
Tejun Heodef4e382008-09-03 08:57:12 +0200772 .start = show_partition_start,
773 .next = disk_seqf_next,
774 .stop = disk_seqf_stop,
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200775 .show = show_partition
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776};
Alexey Dobriyanf5009752008-10-04 23:53:21 +0400777
778static int partitions_open(struct inode *inode, struct file *file)
779{
780 return seq_open(file, &partitions_op);
781}
782
783static const struct file_operations proc_partitions_operations = {
784 .open = partitions_open,
785 .read = seq_read,
786 .llseek = seq_lseek,
787 .release = seq_release,
788};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789#endif
790
791
Tejun Heocf771cb2008-09-03 09:01:09 +0200792static struct kobject *base_probe(dev_t devt, int *partno, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200794 if (request_module("block-major-%d-%d", MAJOR(devt), MINOR(devt)) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 /* Make old-style 2.4 aliases work */
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200796 request_module("block-major-%d", MAJOR(devt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 return NULL;
798}
799
800static int __init genhd_device_init(void)
801{
Dan Williamse105b8b2008-04-21 10:51:07 -0700802 int error;
803
804 block_class.dev_kobj = sysfs_dev_block_kobj;
805 error = class_register(&block_class);
Roland McGrathee27a552008-03-11 17:13:15 -0700806 if (unlikely(error))
807 return error;
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200808 bdev_map = kobj_map_init(base_probe, &block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 blk_dev_init();
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200810
Zhang, Yanmin561ec682008-11-14 08:26:30 +0100811 register_blkdev(BLOCK_EXT_MAJOR, "blkext");
812
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200813#ifndef CONFIG_SYSFS_DEPRECATED
814 /* create top-level block dir */
815 block_depr = kobject_create_and_add("block", NULL);
816#endif
Greg Kroah-Hartman830d3cf2007-11-06 10:36:58 -0800817 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818}
819
820subsys_initcall(genhd_device_init);
821
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200822static ssize_t disk_range_show(struct device *dev,
823 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200825 struct gendisk *disk = dev_to_disk(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200827 return sprintf(buf, "%d\n", disk->minors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828}
829
Tejun Heo1f014292008-08-25 19:47:23 +0900830static ssize_t disk_ext_range_show(struct device *dev,
831 struct device_attribute *attr, char *buf)
832{
833 struct gendisk *disk = dev_to_disk(dev);
834
Tejun Heob5d0b9d2008-09-03 09:06:42 +0200835 return sprintf(buf, "%d\n", disk_max_parts(disk));
Tejun Heo1f014292008-08-25 19:47:23 +0900836}
837
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200838static ssize_t disk_removable_show(struct device *dev,
839 struct device_attribute *attr, char *buf)
Kay Sieversa7fd6702005-10-01 14:49:43 +0200840{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200841 struct gendisk *disk = dev_to_disk(dev);
Kay Sieversa7fd6702005-10-01 14:49:43 +0200842
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200843 return sprintf(buf, "%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200845}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Kay Sievers1c9ce522008-06-13 09:41:00 +0200847static ssize_t disk_ro_show(struct device *dev,
848 struct device_attribute *attr, char *buf)
849{
850 struct gendisk *disk = dev_to_disk(dev);
851
Tejun Heob7db9952008-08-25 19:56:10 +0900852 return sprintf(buf, "%d\n", get_disk_ro(disk) ? 1 : 0);
Kay Sievers1c9ce522008-06-13 09:41:00 +0200853}
854
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200855static ssize_t disk_capability_show(struct device *dev,
856 struct device_attribute *attr, char *buf)
Kristen Carlson Accardi86ce18d2007-05-23 13:57:38 -0700857{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200858 struct gendisk *disk = dev_to_disk(dev);
859
860 return sprintf(buf, "%x\n", disk->flags);
Kristen Carlson Accardi86ce18d2007-05-23 13:57:38 -0700861}
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200862
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400863static ssize_t disk_alignment_offset_show(struct device *dev,
864 struct device_attribute *attr,
865 char *buf)
866{
867 struct gendisk *disk = dev_to_disk(dev);
868
869 return sprintf(buf, "%d\n", queue_alignment_offset(disk->queue));
870}
871
Martin K. Petersen86b37282009-11-10 11:50:21 +0100872static ssize_t disk_discard_alignment_show(struct device *dev,
873 struct device_attribute *attr,
874 char *buf)
875{
876 struct gendisk *disk = dev_to_disk(dev);
877
Martin K. Petersendd3d1452010-01-11 03:21:48 -0500878 return sprintf(buf, "%d\n", queue_discard_alignment(disk->queue));
Martin K. Petersen86b37282009-11-10 11:50:21 +0100879}
880
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200881static DEVICE_ATTR(range, S_IRUGO, disk_range_show, NULL);
Tejun Heo1f014292008-08-25 19:47:23 +0900882static DEVICE_ATTR(ext_range, S_IRUGO, disk_ext_range_show, NULL);
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200883static DEVICE_ATTR(removable, S_IRUGO, disk_removable_show, NULL);
Kay Sievers1c9ce522008-06-13 09:41:00 +0200884static DEVICE_ATTR(ro, S_IRUGO, disk_ro_show, NULL);
Tejun Heoe5610522008-08-25 19:56:09 +0900885static DEVICE_ATTR(size, S_IRUGO, part_size_show, NULL);
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400886static DEVICE_ATTR(alignment_offset, S_IRUGO, disk_alignment_offset_show, NULL);
Martin K. Petersen86b37282009-11-10 11:50:21 +0100887static DEVICE_ATTR(discard_alignment, S_IRUGO, disk_discard_alignment_show,
888 NULL);
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200889static DEVICE_ATTR(capability, S_IRUGO, disk_capability_show, NULL);
Tejun Heo074a7ac2008-08-25 19:56:14 +0900890static DEVICE_ATTR(stat, S_IRUGO, part_stat_show, NULL);
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200891static DEVICE_ATTR(inflight, S_IRUGO, part_inflight_show, NULL);
Akinobu Mitac17bb492006-12-08 02:39:46 -0800892#ifdef CONFIG_FAIL_MAKE_REQUEST
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200893static struct device_attribute dev_attr_fail =
Tejun Heoeddb2e22008-08-25 19:56:13 +0900894 __ATTR(make-it-fail, S_IRUGO|S_IWUSR, part_fail_show, part_fail_store);
Akinobu Mitac17bb492006-12-08 02:39:46 -0800895#endif
Jens Axboe581d4e22008-09-14 05:56:33 -0700896#ifdef CONFIG_FAIL_IO_TIMEOUT
897static struct device_attribute dev_attr_fail_timeout =
898 __ATTR(io-timeout-fail, S_IRUGO|S_IWUSR, part_timeout_show,
899 part_timeout_store);
900#endif
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200901
902static struct attribute *disk_attrs[] = {
903 &dev_attr_range.attr,
Tejun Heo1f014292008-08-25 19:47:23 +0900904 &dev_attr_ext_range.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200905 &dev_attr_removable.attr,
Kay Sievers1c9ce522008-06-13 09:41:00 +0200906 &dev_attr_ro.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200907 &dev_attr_size.attr,
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400908 &dev_attr_alignment_offset.attr,
Martin K. Petersen86b37282009-11-10 11:50:21 +0100909 &dev_attr_discard_alignment.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200910 &dev_attr_capability.attr,
911 &dev_attr_stat.attr,
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200912 &dev_attr_inflight.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200913#ifdef CONFIG_FAIL_MAKE_REQUEST
914 &dev_attr_fail.attr,
915#endif
Jens Axboe581d4e22008-09-14 05:56:33 -0700916#ifdef CONFIG_FAIL_IO_TIMEOUT
917 &dev_attr_fail_timeout.attr,
918#endif
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200919 NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920};
921
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200922static struct attribute_group disk_attr_group = {
923 .attrs = disk_attrs,
924};
925
David Brownella4dbd672009-06-24 10:06:31 -0700926static const struct attribute_group *disk_attr_groups[] = {
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200927 &disk_attr_group,
928 NULL
929};
930
Tejun Heo540eed52008-08-25 19:56:15 +0900931static void disk_free_ptbl_rcu_cb(struct rcu_head *head)
932{
933 struct disk_part_tbl *ptbl =
934 container_of(head, struct disk_part_tbl, rcu_head);
935
936 kfree(ptbl);
937}
938
939/**
940 * disk_replace_part_tbl - replace disk->part_tbl in RCU-safe way
941 * @disk: disk to replace part_tbl for
942 * @new_ptbl: new part_tbl to install
943 *
944 * Replace disk->part_tbl with @new_ptbl in RCU-safe way. The
945 * original ptbl is freed using RCU callback.
946 *
947 * LOCKING:
948 * Matching bd_mutx locked.
949 */
950static void disk_replace_part_tbl(struct gendisk *disk,
951 struct disk_part_tbl *new_ptbl)
952{
953 struct disk_part_tbl *old_ptbl = disk->part_tbl;
954
955 rcu_assign_pointer(disk->part_tbl, new_ptbl);
Jens Axboea6f23652008-10-24 12:52:42 +0200956
957 if (old_ptbl) {
958 rcu_assign_pointer(old_ptbl->last_lookup, NULL);
Tejun Heo540eed52008-08-25 19:56:15 +0900959 call_rcu(&old_ptbl->rcu_head, disk_free_ptbl_rcu_cb);
Jens Axboea6f23652008-10-24 12:52:42 +0200960 }
Tejun Heo540eed52008-08-25 19:56:15 +0900961}
962
963/**
964 * disk_expand_part_tbl - expand disk->part_tbl
965 * @disk: disk to expand part_tbl for
966 * @partno: expand such that this partno can fit in
967 *
968 * Expand disk->part_tbl such that @partno can fit in. disk->part_tbl
969 * uses RCU to allow unlocked dereferencing for stats and other stuff.
970 *
971 * LOCKING:
972 * Matching bd_mutex locked, might sleep.
973 *
974 * RETURNS:
975 * 0 on success, -errno on failure.
976 */
977int disk_expand_part_tbl(struct gendisk *disk, int partno)
978{
979 struct disk_part_tbl *old_ptbl = disk->part_tbl;
980 struct disk_part_tbl *new_ptbl;
981 int len = old_ptbl ? old_ptbl->len : 0;
982 int target = partno + 1;
983 size_t size;
984 int i;
985
986 /* disk_max_parts() is zero during initialization, ignore if so */
987 if (disk_max_parts(disk) && target > disk_max_parts(disk))
988 return -EINVAL;
989
990 if (target <= len)
991 return 0;
992
993 size = sizeof(*new_ptbl) + target * sizeof(new_ptbl->part[0]);
994 new_ptbl = kzalloc_node(size, GFP_KERNEL, disk->node_id);
995 if (!new_ptbl)
996 return -ENOMEM;
997
Tejun Heo540eed52008-08-25 19:56:15 +0900998 new_ptbl->len = target;
999
1000 for (i = 0; i < len; i++)
1001 rcu_assign_pointer(new_ptbl->part[i], old_ptbl->part[i]);
1002
1003 disk_replace_part_tbl(disk, new_ptbl);
1004 return 0;
1005}
1006
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001007static void disk_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001009 struct gendisk *disk = dev_to_disk(dev);
1010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 kfree(disk->random);
Tejun Heo540eed52008-08-25 19:56:15 +09001012 disk_replace_part_tbl(disk, NULL);
Tejun Heo074a7ac2008-08-25 19:56:14 +09001013 free_part_stats(&disk->part0);
Will Drewry6d1d8052010-08-31 15:47:05 -05001014 free_part_info(&disk->part0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 kfree(disk);
1016}
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001017struct class block_class = {
1018 .name = "block",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019};
1020
Kay Sieverse454cea2009-09-18 23:01:12 +02001021static char *block_devnode(struct device *dev, mode_t *mode)
Kay Sieversb03f38b2009-04-30 15:23:42 +02001022{
1023 struct gendisk *disk = dev_to_disk(dev);
1024
Kay Sieverse454cea2009-09-18 23:01:12 +02001025 if (disk->devnode)
1026 return disk->devnode(disk, mode);
Kay Sieversb03f38b2009-04-30 15:23:42 +02001027 return NULL;
1028}
1029
Adrian Bunk1826ead2008-03-04 11:23:46 +01001030static struct device_type disk_type = {
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001031 .name = "disk",
1032 .groups = disk_attr_groups,
1033 .release = disk_release,
Kay Sieverse454cea2009-09-18 23:01:12 +02001034 .devnode = block_devnode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035};
1036
Randy Dunlapa6e2ba82008-05-23 09:44:11 -07001037#ifdef CONFIG_PROC_FS
Tejun Heocf771cb2008-09-03 09:01:09 +02001038/*
1039 * aggregate disk stat collector. Uses the same stats that the sysfs
1040 * entries do, above, but makes them available through one seq_file.
1041 *
1042 * The output looks suspiciously like /proc/partitions with a bunch of
1043 * extra fields.
1044 */
1045static int diskstats_show(struct seq_file *seqf, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
1047 struct gendisk *gp = v;
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001048 struct disk_part_iter piter;
1049 struct hd_struct *hd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 char buf[BDEVNAME_SIZE];
Tejun Heoc9959052008-08-25 19:47:21 +09001051 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
1053 /*
Tejun Heoed9e1982008-08-25 19:56:05 +09001054 if (&disk_to_dev(gp)->kobj.entry == block_class.devices.next)
Tejun Heocf771cb2008-09-03 09:01:09 +02001055 seq_puts(seqf, "major minor name"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 " rio rmerge rsect ruse wio wmerge "
1057 "wsect wuse running use aveq"
1058 "\n\n");
1059 */
1060
Tejun Heo71982a42009-04-17 08:34:48 +02001061 disk_part_iter_init(&piter, gp, DISK_PITER_INCL_EMPTY_PART0);
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001062 while ((hd = disk_part_iter_next(&piter))) {
Tejun Heo074a7ac2008-08-25 19:56:14 +09001063 cpu = part_stat_lock();
Tejun Heoc9959052008-08-25 19:47:21 +09001064 part_round_stats(cpu, hd);
Tejun Heo074a7ac2008-08-25 19:56:14 +09001065 part_stat_unlock();
Tejun Heo1f014292008-08-25 19:47:23 +09001066 seq_printf(seqf, "%4d %7d %s %lu %lu %llu "
Jerome Marchand28f39d52008-02-08 11:04:56 +01001067 "%u %lu %lu %llu %u %u %u %u\n",
Tejun Heof331c022008-09-03 09:01:48 +02001068 MAJOR(part_devt(hd)), MINOR(part_devt(hd)),
1069 disk_name(gp, hd->partno, buf),
Jerome Marchand28f39d52008-02-08 11:04:56 +01001070 part_stat_read(hd, ios[0]),
1071 part_stat_read(hd, merges[0]),
1072 (unsigned long long)part_stat_read(hd, sectors[0]),
1073 jiffies_to_msecs(part_stat_read(hd, ticks[0])),
1074 part_stat_read(hd, ios[1]),
1075 part_stat_read(hd, merges[1]),
1076 (unsigned long long)part_stat_read(hd, sectors[1]),
1077 jiffies_to_msecs(part_stat_read(hd, ticks[1])),
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001078 part_in_flight(hd),
Jerome Marchand28f39d52008-02-08 11:04:56 +01001079 jiffies_to_msecs(part_stat_read(hd, io_ticks)),
1080 jiffies_to_msecs(part_stat_read(hd, time_in_queue))
1081 );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 }
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001083 disk_part_iter_exit(&piter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
1085 return 0;
1086}
1087
Alexey Dobriyan31d85ab2008-10-06 12:55:38 +04001088static const struct seq_operations diskstats_op = {
Tejun Heodef4e382008-09-03 08:57:12 +02001089 .start = disk_seqf_start,
1090 .next = disk_seqf_next,
1091 .stop = disk_seqf_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 .show = diskstats_show
1093};
Alexey Dobriyanf5009752008-10-04 23:53:21 +04001094
Alexey Dobriyan31d85ab2008-10-06 12:55:38 +04001095static int diskstats_open(struct inode *inode, struct file *file)
1096{
1097 return seq_open(file, &diskstats_op);
1098}
1099
1100static const struct file_operations proc_diskstats_operations = {
1101 .open = diskstats_open,
1102 .read = seq_read,
1103 .llseek = seq_lseek,
1104 .release = seq_release,
1105};
1106
Alexey Dobriyanf5009752008-10-04 23:53:21 +04001107static int __init proc_genhd_init(void)
1108{
Alexey Dobriyan31d85ab2008-10-06 12:55:38 +04001109 proc_create("diskstats", 0, NULL, &proc_diskstats_operations);
Alexey Dobriyanf5009752008-10-04 23:53:21 +04001110 proc_create("partitions", 0, NULL, &proc_partitions_operations);
1111 return 0;
1112}
1113module_init(proc_genhd_init);
Randy Dunlapa6e2ba82008-05-23 09:44:11 -07001114#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
Kristen Carlson Accardi8ce7ad7b2007-05-23 13:57:38 -07001116static void media_change_notify_thread(struct work_struct *work)
1117{
1118 struct gendisk *gd = container_of(work, struct gendisk, async_notify);
1119 char event[] = "MEDIA_CHANGE=1";
1120 char *envp[] = { event, NULL };
1121
1122 /*
1123 * set enviroment vars to indicate which event this is for
1124 * so that user space will know to go check the media status.
1125 */
Tejun Heoed9e1982008-08-25 19:56:05 +09001126 kobject_uevent_env(&disk_to_dev(gd)->kobj, KOBJ_CHANGE, envp);
Kristen Carlson Accardi8ce7ad7b2007-05-23 13:57:38 -07001127 put_device(gd->driverfs_dev);
1128}
1129
Adrian Bunk1826ead2008-03-04 11:23:46 +01001130#if 0
Kristen Carlson Accardi8ce7ad7b2007-05-23 13:57:38 -07001131void genhd_media_change_notify(struct gendisk *disk)
1132{
1133 get_device(disk->driverfs_dev);
1134 schedule_work(&disk->async_notify);
1135}
1136EXPORT_SYMBOL_GPL(genhd_media_change_notify);
Adrian Bunk1826ead2008-03-04 11:23:46 +01001137#endif /* 0 */
Kristen Carlson Accardi8ce7ad7b2007-05-23 13:57:38 -07001138
Tejun Heocf771cb2008-09-03 09:01:09 +02001139dev_t blk_lookup_devt(const char *name, int partno)
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001140{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001141 dev_t devt = MKDEV(0, 0);
Tejun Heodef4e382008-09-03 08:57:12 +02001142 struct class_dev_iter iter;
1143 struct device *dev;
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001144
Tejun Heodef4e382008-09-03 08:57:12 +02001145 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
1146 while ((dev = class_dev_iter_next(&iter))) {
1147 struct gendisk *disk = dev_to_disk(dev);
Tejun Heo548b10e2008-08-29 09:01:47 +02001148 struct hd_struct *part;
Tejun Heodef4e382008-09-03 08:57:12 +02001149
Kay Sievers3ada8b72009-01-06 10:44:43 -08001150 if (strcmp(dev_name(dev), name))
Tejun Heof331c022008-09-03 09:01:48 +02001151 continue;
Tejun Heof331c022008-09-03 09:01:48 +02001152
Neil Brown41b8c852009-02-18 10:33:59 +01001153 if (partno < disk->minors) {
1154 /* We need to return the right devno, even
1155 * if the partition doesn't exist yet.
1156 */
1157 devt = MKDEV(MAJOR(dev->devt),
1158 MINOR(dev->devt) + partno);
1159 break;
1160 }
Tejun Heo548b10e2008-08-29 09:01:47 +02001161 part = disk_get_part(disk, partno);
Tejun Heo2bbedcb2008-08-29 11:41:51 +02001162 if (part) {
Tejun Heof331c022008-09-03 09:01:48 +02001163 devt = part_devt(part);
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001164 disk_put_part(part);
Tejun Heo548b10e2008-08-29 09:01:47 +02001165 break;
Tejun Heodef4e382008-09-03 08:57:12 +02001166 }
Tejun Heo548b10e2008-08-29 09:01:47 +02001167 disk_put_part(part);
Kay Sievers5c0ef6d2008-08-16 14:30:30 +02001168 }
Tejun Heodef4e382008-09-03 08:57:12 +02001169 class_dev_iter_exit(&iter);
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001170 return devt;
1171}
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001172EXPORT_SYMBOL(blk_lookup_devt);
1173
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174struct gendisk *alloc_disk(int minors)
1175{
Christoph Lameter19460892005-06-23 00:08:19 -07001176 return alloc_disk_node(minors, -1);
1177}
Tejun Heo689d6fa2008-08-25 19:56:16 +09001178EXPORT_SYMBOL(alloc_disk);
Christoph Lameter19460892005-06-23 00:08:19 -07001179
1180struct gendisk *alloc_disk_node(int minors, int node_id)
1181{
1182 struct gendisk *disk;
1183
Christoph Lameter94f60302007-07-17 04:03:29 -07001184 disk = kmalloc_node(sizeof(struct gendisk),
1185 GFP_KERNEL | __GFP_ZERO, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 if (disk) {
Tejun Heo074a7ac2008-08-25 19:56:14 +09001187 if (!init_part_stats(&disk->part0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 kfree(disk);
1189 return NULL;
1190 }
Cheng Renquanbf91db12008-11-20 08:37:37 +01001191 disk->node_id = node_id;
Tejun Heo540eed52008-08-25 19:56:15 +09001192 if (disk_expand_part_tbl(disk, 0)) {
1193 free_part_stats(&disk->part0);
Tejun Heob5d0b9d2008-09-03 09:06:42 +02001194 kfree(disk);
1195 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 }
Tejun Heo540eed52008-08-25 19:56:15 +09001197 disk->part_tbl->part[0] = &disk->part0;
Tejun Heob5d0b9d2008-09-03 09:06:42 +02001198
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 disk->minors = minors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 rand_initialize_disk(disk);
Tejun Heoed9e1982008-08-25 19:56:05 +09001201 disk_to_dev(disk)->class = &block_class;
1202 disk_to_dev(disk)->type = &disk_type;
1203 device_initialize(disk_to_dev(disk));
Kristen Carlson Accardi8ce7ad7b2007-05-23 13:57:38 -07001204 INIT_WORK(&disk->async_notify,
1205 media_change_notify_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 }
1207 return disk;
1208}
Christoph Lameter19460892005-06-23 00:08:19 -07001209EXPORT_SYMBOL(alloc_disk_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
1211struct kobject *get_disk(struct gendisk *disk)
1212{
1213 struct module *owner;
1214 struct kobject *kobj;
1215
1216 if (!disk->fops)
1217 return NULL;
1218 owner = disk->fops->owner;
1219 if (owner && !try_module_get(owner))
1220 return NULL;
Tejun Heoed9e1982008-08-25 19:56:05 +09001221 kobj = kobject_get(&disk_to_dev(disk)->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 if (kobj == NULL) {
1223 module_put(owner);
1224 return NULL;
1225 }
1226 return kobj;
1227
1228}
1229
1230EXPORT_SYMBOL(get_disk);
1231
1232void put_disk(struct gendisk *disk)
1233{
1234 if (disk)
Tejun Heoed9e1982008-08-25 19:56:05 +09001235 kobject_put(&disk_to_dev(disk)->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236}
1237
1238EXPORT_SYMBOL(put_disk);
1239
Hannes Reineckee3264a42009-07-28 09:13:13 +02001240static void set_disk_ro_uevent(struct gendisk *gd, int ro)
1241{
1242 char event[] = "DISK_RO=1";
1243 char *envp[] = { event, NULL };
1244
1245 if (!ro)
1246 event[8] = '0';
1247 kobject_uevent_env(&disk_to_dev(gd)->kobj, KOBJ_CHANGE, envp);
1248}
1249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250void set_device_ro(struct block_device *bdev, int flag)
1251{
Tejun Heob7db9952008-08-25 19:56:10 +09001252 bdev->bd_part->policy = flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253}
1254
1255EXPORT_SYMBOL(set_device_ro);
1256
1257void set_disk_ro(struct gendisk *disk, int flag)
1258{
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001259 struct disk_part_iter piter;
1260 struct hd_struct *part;
1261
Hannes Reineckee3264a42009-07-28 09:13:13 +02001262 if (disk->part0.policy != flag) {
1263 set_disk_ro_uevent(disk, flag);
1264 disk->part0.policy = flag;
1265 }
1266
1267 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_EMPTY);
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001268 while ((part = disk_part_iter_next(&piter)))
1269 part->policy = flag;
1270 disk_part_iter_exit(&piter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271}
1272
1273EXPORT_SYMBOL(set_disk_ro);
1274
1275int bdev_read_only(struct block_device *bdev)
1276{
1277 if (!bdev)
1278 return 0;
Tejun Heob7db9952008-08-25 19:56:10 +09001279 return bdev->bd_part->policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280}
1281
1282EXPORT_SYMBOL(bdev_read_only);
1283
Tejun Heocf771cb2008-09-03 09:01:09 +02001284int invalidate_partition(struct gendisk *disk, int partno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285{
1286 int res = 0;
Tejun Heocf771cb2008-09-03 09:01:09 +02001287 struct block_device *bdev = bdget_disk(disk, partno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 if (bdev) {
Christoph Hellwig2ef41632005-05-05 16:15:59 -07001289 fsync_bdev(bdev);
1290 res = __invalidate_device(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 bdput(bdev);
1292 }
1293 return res;
1294}
1295
1296EXPORT_SYMBOL(invalidate_partition);