blob: 2e5e4c0a11333dbd27d6c3d68a8a538d9a57ab41 [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);
Kay Sieversedfaa7c2007-05-21 22:08:01 +020025struct kobject *block_depr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Tejun Heobcce3de2008-08-25 19:47:22 +090027/* for extended dynamic devt allocation, currently only one major is used */
28#define MAX_EXT_DEVT (1 << MINORBITS)
29
30/* For extended devt allocation. ext_devt_mutex prevents look up
31 * results from going away underneath its user.
32 */
33static DEFINE_MUTEX(ext_devt_mutex);
34static DEFINE_IDR(ext_devt_idr);
35
Adrian Bunk1826ead2008-03-04 11:23:46 +010036static struct device_type disk_type;
37
Tejun Heoe71bf0d2008-09-03 09:03:02 +020038/**
39 * disk_get_part - get partition
40 * @disk: disk to look partition from
41 * @partno: partition number
42 *
43 * Look for partition @partno from @disk. If found, increment
44 * reference count and return it.
45 *
46 * CONTEXT:
47 * Don't care.
48 *
49 * RETURNS:
50 * Pointer to the found partition on success, NULL if not found.
51 */
52struct hd_struct *disk_get_part(struct gendisk *disk, int partno)
53{
Tejun Heo540eed52008-08-25 19:56:15 +090054 struct hd_struct *part = NULL;
55 struct disk_part_tbl *ptbl;
Tejun Heoe71bf0d2008-09-03 09:03:02 +020056
Tejun Heo540eed52008-08-25 19:56:15 +090057 if (unlikely(partno < 0))
Tejun Heoe71bf0d2008-09-03 09:03:02 +020058 return NULL;
Tejun Heo540eed52008-08-25 19:56:15 +090059
Tejun Heoe71bf0d2008-09-03 09:03:02 +020060 rcu_read_lock();
Tejun Heo540eed52008-08-25 19:56:15 +090061
62 ptbl = rcu_dereference(disk->part_tbl);
63 if (likely(partno < ptbl->len)) {
64 part = rcu_dereference(ptbl->part[partno]);
65 if (part)
66 get_device(part_to_dev(part));
67 }
68
Tejun Heoe71bf0d2008-09-03 09:03:02 +020069 rcu_read_unlock();
70
71 return part;
72}
73EXPORT_SYMBOL_GPL(disk_get_part);
74
75/**
76 * disk_part_iter_init - initialize partition iterator
77 * @piter: iterator to initialize
78 * @disk: disk to iterate over
79 * @flags: DISK_PITER_* flags
80 *
81 * Initialize @piter so that it iterates over partitions of @disk.
82 *
83 * CONTEXT:
84 * Don't care.
85 */
86void disk_part_iter_init(struct disk_part_iter *piter, struct gendisk *disk,
87 unsigned int flags)
88{
Tejun Heo540eed52008-08-25 19:56:15 +090089 struct disk_part_tbl *ptbl;
90
91 rcu_read_lock();
92 ptbl = rcu_dereference(disk->part_tbl);
93
Tejun Heoe71bf0d2008-09-03 09:03:02 +020094 piter->disk = disk;
95 piter->part = NULL;
96
97 if (flags & DISK_PITER_REVERSE)
Tejun Heo540eed52008-08-25 19:56:15 +090098 piter->idx = ptbl->len - 1;
Tejun Heo71982a42009-04-17 08:34:48 +020099 else if (flags & (DISK_PITER_INCL_PART0 | DISK_PITER_INCL_EMPTY_PART0))
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200100 piter->idx = 0;
Tejun Heob5d0b9d2008-09-03 09:06:42 +0200101 else
102 piter->idx = 1;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200103
104 piter->flags = flags;
Tejun Heo540eed52008-08-25 19:56:15 +0900105
106 rcu_read_unlock();
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200107}
108EXPORT_SYMBOL_GPL(disk_part_iter_init);
109
110/**
111 * disk_part_iter_next - proceed iterator to the next partition and return it
112 * @piter: iterator of interest
113 *
114 * Proceed @piter to the next partition and return it.
115 *
116 * CONTEXT:
117 * Don't care.
118 */
119struct hd_struct *disk_part_iter_next(struct disk_part_iter *piter)
120{
Tejun Heo540eed52008-08-25 19:56:15 +0900121 struct disk_part_tbl *ptbl;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200122 int inc, end;
123
124 /* put the last partition */
125 disk_put_part(piter->part);
126 piter->part = NULL;
127
Tejun Heo540eed52008-08-25 19:56:15 +0900128 /* get part_tbl */
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200129 rcu_read_lock();
Tejun Heo540eed52008-08-25 19:56:15 +0900130 ptbl = rcu_dereference(piter->disk->part_tbl);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200131
132 /* determine iteration parameters */
133 if (piter->flags & DISK_PITER_REVERSE) {
134 inc = -1;
Tejun Heo71982a42009-04-17 08:34:48 +0200135 if (piter->flags & (DISK_PITER_INCL_PART0 |
136 DISK_PITER_INCL_EMPTY_PART0))
Tejun Heob5d0b9d2008-09-03 09:06:42 +0200137 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;
Tejun Heo71982a42009-04-17 08:34:48 +0200152 if (!part->nr_sects &&
153 !(piter->flags & DISK_PITER_INCL_EMPTY) &&
154 !(piter->flags & DISK_PITER_INCL_EMPTY_PART0 &&
155 piter->idx == 0))
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200156 continue;
157
Tejun Heoed9e1982008-08-25 19:56:05 +0900158 get_device(part_to_dev(part));
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200159 piter->part = part;
160 piter->idx += inc;
161 break;
162 }
163
164 rcu_read_unlock();
165
166 return piter->part;
167}
168EXPORT_SYMBOL_GPL(disk_part_iter_next);
169
170/**
171 * disk_part_iter_exit - finish up partition iteration
172 * @piter: iter of interest
173 *
174 * Called when iteration is over. Cleans up @piter.
175 *
176 * CONTEXT:
177 * Don't care.
178 */
179void disk_part_iter_exit(struct disk_part_iter *piter)
180{
181 disk_put_part(piter->part);
182 piter->part = NULL;
183}
184EXPORT_SYMBOL_GPL(disk_part_iter_exit);
185
Jens Axboea6f23652008-10-24 12:52:42 +0200186static inline int sector_in_part(struct hd_struct *part, sector_t sector)
187{
188 return part->start_sect <= sector &&
189 sector < part->start_sect + part->nr_sects;
190}
191
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200192/**
193 * disk_map_sector_rcu - map sector to partition
194 * @disk: gendisk of interest
195 * @sector: sector to map
196 *
197 * Find out which partition @sector maps to on @disk. This is
198 * primarily used for stats accounting.
199 *
200 * CONTEXT:
201 * RCU read locked. The returned partition pointer is valid only
202 * while preemption is disabled.
203 *
204 * RETURNS:
Tejun Heo074a7ac2008-08-25 19:56:14 +0900205 * Found partition on success, part0 is returned if no partition matches
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200206 */
207struct hd_struct *disk_map_sector_rcu(struct gendisk *disk, sector_t sector)
208{
Tejun Heo540eed52008-08-25 19:56:15 +0900209 struct disk_part_tbl *ptbl;
Jens Axboea6f23652008-10-24 12:52:42 +0200210 struct hd_struct *part;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200211 int i;
212
Tejun Heo540eed52008-08-25 19:56:15 +0900213 ptbl = rcu_dereference(disk->part_tbl);
214
Jens Axboea6f23652008-10-24 12:52:42 +0200215 part = rcu_dereference(ptbl->last_lookup);
216 if (part && sector_in_part(part, sector))
217 return part;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200218
Jens Axboea6f23652008-10-24 12:52:42 +0200219 for (i = 1; i < ptbl->len; i++) {
220 part = rcu_dereference(ptbl->part[i]);
221
222 if (part && sector_in_part(part, sector)) {
223 rcu_assign_pointer(ptbl->last_lookup, part);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200224 return part;
Jens Axboea6f23652008-10-24 12:52:42 +0200225 }
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200226 }
Tejun Heo074a7ac2008-08-25 19:56:14 +0900227 return &disk->part0;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200228}
229EXPORT_SYMBOL_GPL(disk_map_sector_rcu);
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231/*
232 * Can be deleted altogether. Later.
233 *
234 */
235static struct blk_major_name {
236 struct blk_major_name *next;
237 int major;
238 char name[16];
Joe Korty68eef3b2006-03-31 02:30:32 -0800239} *major_names[BLKDEV_MAJOR_HASH_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241/* index in the above - for now: assume no multimajor ranges */
242static inline int major_to_index(int major)
243{
Joe Korty68eef3b2006-03-31 02:30:32 -0800244 return major % BLKDEV_MAJOR_HASH_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245}
246
Joe Korty68eef3b2006-03-31 02:30:32 -0800247#ifdef CONFIG_PROC_FS
Tejun Heocf771cb2008-09-03 09:01:09 +0200248void blkdev_show(struct seq_file *seqf, off_t offset)
Neil Horman7170be52006-01-14 13:20:38 -0800249{
Joe Korty68eef3b2006-03-31 02:30:32 -0800250 struct blk_major_name *dp;
Neil Horman7170be52006-01-14 13:20:38 -0800251
Joe Korty68eef3b2006-03-31 02:30:32 -0800252 if (offset < BLKDEV_MAJOR_HASH_SIZE) {
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200253 mutex_lock(&block_class_lock);
Joe Korty68eef3b2006-03-31 02:30:32 -0800254 for (dp = major_names[offset]; dp; dp = dp->next)
Tejun Heocf771cb2008-09-03 09:01:09 +0200255 seq_printf(seqf, "%3d %s\n", dp->major, dp->name);
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200256 mutex_unlock(&block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}
Joe Korty68eef3b2006-03-31 02:30:32 -0800259#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Márton Németh9e8c0bc2009-02-20 08:12:51 +0100261/**
262 * register_blkdev - register a new block device
263 *
264 * @major: the requested major device number [1..255]. If @major=0, try to
265 * allocate any unused major number.
266 * @name: the name of the new block device as a zero terminated string
267 *
268 * The @name must be unique within the system.
269 *
270 * The return value depends on the @major input parameter.
271 * - if a major device number was requested in range [1..255] then the
272 * function returns zero on success, or a negative error code
273 * - if any unused major number was requested with @major=0 parameter
274 * then the return value is the allocated major number in range
275 * [1..255] or a negative error code otherwise
276 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277int register_blkdev(unsigned int major, const char *name)
278{
279 struct blk_major_name **n, *p;
280 int index, ret = 0;
281
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200282 mutex_lock(&block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 /* temporary */
285 if (major == 0) {
286 for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) {
287 if (major_names[index] == NULL)
288 break;
289 }
290
291 if (index == 0) {
292 printk("register_blkdev: failed to get major for %s\n",
293 name);
294 ret = -EBUSY;
295 goto out;
296 }
297 major = index;
298 ret = major;
299 }
300
301 p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL);
302 if (p == NULL) {
303 ret = -ENOMEM;
304 goto out;
305 }
306
307 p->major = major;
308 strlcpy(p->name, name, sizeof(p->name));
309 p->next = NULL;
310 index = major_to_index(major);
311
312 for (n = &major_names[index]; *n; n = &(*n)->next) {
313 if ((*n)->major == major)
314 break;
315 }
316 if (!*n)
317 *n = p;
318 else
319 ret = -EBUSY;
320
321 if (ret < 0) {
322 printk("register_blkdev: cannot get major %d for %s\n",
323 major, name);
324 kfree(p);
325 }
326out:
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200327 mutex_unlock(&block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 return ret;
329}
330
331EXPORT_SYMBOL(register_blkdev);
332
Akinobu Mitaf4480242007-07-17 04:03:47 -0700333void unregister_blkdev(unsigned int major, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
335 struct blk_major_name **n;
336 struct blk_major_name *p = NULL;
337 int index = major_to_index(major);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200339 mutex_lock(&block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 for (n = &major_names[index]; *n; n = &(*n)->next)
341 if ((*n)->major == major)
342 break;
Akinobu Mita294462a2007-07-17 04:03:45 -0700343 if (!*n || strcmp((*n)->name, name)) {
344 WARN_ON(1);
Akinobu Mita294462a2007-07-17 04:03:45 -0700345 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 p = *n;
347 *n = p->next;
348 }
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200349 mutex_unlock(&block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351}
352
353EXPORT_SYMBOL(unregister_blkdev);
354
355static struct kobj_map *bdev_map;
356
Tejun Heobcce3de2008-08-25 19:47:22 +0900357/**
Tejun Heo870d6652008-08-25 19:47:25 +0900358 * blk_mangle_minor - scatter minor numbers apart
359 * @minor: minor number to mangle
360 *
361 * Scatter consecutively allocated @minor number apart if MANGLE_DEVT
362 * is enabled. Mangling twice gives the original value.
363 *
364 * RETURNS:
365 * Mangled value.
366 *
367 * CONTEXT:
368 * Don't care.
369 */
370static int blk_mangle_minor(int minor)
371{
372#ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT
373 int i;
374
375 for (i = 0; i < MINORBITS / 2; i++) {
376 int low = minor & (1 << i);
377 int high = minor & (1 << (MINORBITS - 1 - i));
378 int distance = MINORBITS - 1 - 2 * i;
379
380 minor ^= low | high; /* clear both bits */
381 low <<= distance; /* swap the positions */
382 high >>= distance;
383 minor |= low | high; /* and set */
384 }
385#endif
386 return minor;
387}
388
389/**
Tejun Heobcce3de2008-08-25 19:47:22 +0900390 * blk_alloc_devt - allocate a dev_t for a partition
391 * @part: partition to allocate dev_t for
Tejun Heobcce3de2008-08-25 19:47:22 +0900392 * @devt: out parameter for resulting dev_t
393 *
394 * Allocate a dev_t for block device.
395 *
396 * RETURNS:
397 * 0 on success, allocated dev_t is returned in *@devt. -errno on
398 * failure.
399 *
400 * CONTEXT:
401 * Might sleep.
402 */
403int blk_alloc_devt(struct hd_struct *part, dev_t *devt)
404{
405 struct gendisk *disk = part_to_disk(part);
406 int idx, rc;
407
408 /* in consecutive minor range? */
409 if (part->partno < disk->minors) {
410 *devt = MKDEV(disk->major, disk->first_minor + part->partno);
411 return 0;
412 }
413
414 /* allocate ext devt */
415 do {
416 if (!idr_pre_get(&ext_devt_idr, GFP_KERNEL))
417 return -ENOMEM;
418 rc = idr_get_new(&ext_devt_idr, part, &idx);
419 } while (rc == -EAGAIN);
420
421 if (rc)
422 return rc;
423
424 if (idx > MAX_EXT_DEVT) {
425 idr_remove(&ext_devt_idr, idx);
426 return -EBUSY;
427 }
428
Tejun Heo870d6652008-08-25 19:47:25 +0900429 *devt = MKDEV(BLOCK_EXT_MAJOR, blk_mangle_minor(idx));
Tejun Heobcce3de2008-08-25 19:47:22 +0900430 return 0;
431}
432
433/**
434 * blk_free_devt - free a dev_t
435 * @devt: dev_t to free
436 *
437 * Free @devt which was allocated using blk_alloc_devt().
438 *
439 * CONTEXT:
440 * Might sleep.
441 */
442void blk_free_devt(dev_t devt)
443{
444 might_sleep();
445
446 if (devt == MKDEV(0, 0))
447 return;
448
449 if (MAJOR(devt) == BLOCK_EXT_MAJOR) {
450 mutex_lock(&ext_devt_mutex);
Tejun Heo870d6652008-08-25 19:47:25 +0900451 idr_remove(&ext_devt_idr, blk_mangle_minor(MINOR(devt)));
Tejun Heobcce3de2008-08-25 19:47:22 +0900452 mutex_unlock(&ext_devt_mutex);
453 }
454}
455
Tejun Heo1f014292008-08-25 19:47:23 +0900456static char *bdevt_str(dev_t devt, char *buf)
457{
458 if (MAJOR(devt) <= 0xff && MINOR(devt) <= 0xff) {
459 char tbuf[BDEVT_SIZE];
460 snprintf(tbuf, BDEVT_SIZE, "%02x%02x", MAJOR(devt), MINOR(devt));
461 snprintf(buf, BDEVT_SIZE, "%-9s", tbuf);
462 } else
463 snprintf(buf, BDEVT_SIZE, "%03x:%05x", MAJOR(devt), MINOR(devt));
464
465 return buf;
466}
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468/*
469 * Register device numbers dev..(dev+range-1)
470 * range must be nonzero
471 * The hash chain is sorted on range, so that subranges can override.
472 */
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200473void blk_register_region(dev_t devt, unsigned long range, struct module *module,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 struct kobject *(*probe)(dev_t, int *, void *),
475 int (*lock)(dev_t, void *), void *data)
476{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200477 kobj_map(bdev_map, devt, range, module, probe, lock, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478}
479
480EXPORT_SYMBOL(blk_register_region);
481
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200482void blk_unregister_region(dev_t devt, unsigned long range)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200484 kobj_unmap(bdev_map, devt, range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485}
486
487EXPORT_SYMBOL(blk_unregister_region);
488
Tejun Heocf771cb2008-09-03 09:01:09 +0200489static struct kobject *exact_match(dev_t devt, int *partno, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
491 struct gendisk *p = data;
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200492
Tejun Heoed9e1982008-08-25 19:56:05 +0900493 return &disk_to_dev(p)->kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494}
495
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200496static int exact_lock(dev_t devt, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
498 struct gendisk *p = data;
499
500 if (!get_disk(p))
501 return -1;
502 return 0;
503}
504
Tejun Heod2bf1b62010-12-08 20:57:36 +0100505void register_disk(struct gendisk *disk)
506{
507 struct device *ddev = disk_to_dev(disk);
508 struct block_device *bdev;
509 struct disk_part_iter piter;
510 struct hd_struct *part;
511 int err;
512
513 ddev->parent = disk->driverfs_dev;
514
515 dev_set_name(ddev, disk->disk_name);
516
517 /* delay uevents, until we scanned partition table */
518 dev_set_uevent_suppress(ddev, 1);
519
520 if (device_add(ddev))
521 return;
522 if (!sysfs_deprecated) {
523 err = sysfs_create_link(block_depr, &ddev->kobj,
524 kobject_name(&ddev->kobj));
525 if (err) {
526 device_del(ddev);
527 return;
528 }
529 }
530 disk->part0.holder_dir = kobject_create_and_add("holders", &ddev->kobj);
531 disk->slave_dir = kobject_create_and_add("slaves", &ddev->kobj);
532
533 /* No minors to use for partitions */
534 if (!disk_partitionable(disk))
535 goto exit;
536
537 /* No such device (e.g., media were just removed) */
538 if (!get_capacity(disk))
539 goto exit;
540
541 bdev = bdget_disk(disk, 0);
542 if (!bdev)
543 goto exit;
544
545 bdev->bd_invalidated = 1;
546 err = blkdev_get(bdev, FMODE_READ, NULL);
547 if (err < 0)
548 goto exit;
549 blkdev_put(bdev, FMODE_READ);
550
551exit:
552 /* announce disk after possible partitions are created */
553 dev_set_uevent_suppress(ddev, 0);
554 kobject_uevent(&ddev->kobj, KOBJ_ADD);
555
556 /* announce possible partitions */
557 disk_part_iter_init(&piter, disk, 0);
558 while ((part = disk_part_iter_next(&piter)))
559 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_ADD);
560 disk_part_iter_exit(&piter);
561}
562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563/**
564 * add_disk - add partitioning information to kernel list
565 * @disk: per-device partitioning information
566 *
567 * This function registers the partitioning information in @disk
568 * with the kernel.
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900569 *
570 * FIXME: error handling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 */
572void add_disk(struct gendisk *disk)
573{
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700574 struct backing_dev_info *bdi;
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900575 dev_t devt;
Greg Kroah-Hartman6ffeea72008-05-22 17:21:08 -0400576 int retval;
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700577
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900578 /* minors == 0 indicates to use ext devt from part0 and should
579 * be accompanied with EXT_DEVT flag. Make sure all
580 * parameters make sense.
581 */
582 WARN_ON(disk->minors && !(disk->major || disk->first_minor));
583 WARN_ON(!disk->minors && !(disk->flags & GENHD_FL_EXT_DEVT));
584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 disk->flags |= GENHD_FL_UP;
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900586
587 retval = blk_alloc_devt(&disk->part0, &devt);
588 if (retval) {
589 WARN_ON(1);
590 return;
591 }
592 disk_to_dev(disk)->devt = devt;
593
594 /* ->major and ->first_minor aren't supposed to be
595 * dereferenced from here on, but set them just in case.
596 */
597 disk->major = MAJOR(devt);
598 disk->first_minor = MINOR(devt);
599
Signed-off-by: Jan Kara01ea5062010-09-16 20:36:36 +0200600 /* Register BDI before referencing it from bdev */
601 bdi = &disk->queue->backing_dev_info;
602 bdi_register_dev(bdi, disk_devt(disk));
603
Tejun Heof331c022008-09-03 09:01:48 +0200604 blk_register_region(disk_devt(disk), disk->minors, NULL,
605 exact_match, exact_lock, disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 register_disk(disk);
607 blk_register_queue(disk);
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700608
Tejun Heoed9e1982008-08-25 19:56:05 +0900609 retval = sysfs_create_link(&disk_to_dev(disk)->kobj, &bdi->dev->kobj,
610 "bdi");
Greg Kroah-Hartman6ffeea72008-05-22 17:21:08 -0400611 WARN_ON(retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613EXPORT_SYMBOL(add_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Tejun Heod2bf1b62010-12-08 20:57:36 +0100615void del_gendisk(struct gendisk *disk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
Tejun Heod2bf1b62010-12-08 20:57:36 +0100617 struct disk_part_iter piter;
618 struct hd_struct *part;
619
620 /* invalidate stuff */
621 disk_part_iter_init(&piter, disk,
622 DISK_PITER_INCL_EMPTY | DISK_PITER_REVERSE);
623 while ((part = disk_part_iter_next(&piter))) {
624 invalidate_partition(disk, part->partno);
625 delete_partition(disk, part->partno);
626 }
627 disk_part_iter_exit(&piter);
628
629 invalidate_partition(disk, 0);
630 blk_free_devt(disk_to_dev(disk)->devt);
631 set_capacity(disk, 0);
632 disk->flags &= ~GENHD_FL_UP;
633
Tejun Heoed9e1982008-08-25 19:56:05 +0900634 sysfs_remove_link(&disk_to_dev(disk)->kobj, "bdi");
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700635 bdi_unregister(&disk->queue->backing_dev_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 blk_unregister_queue(disk);
Tejun Heof331c022008-09-03 09:01:48 +0200637 blk_unregister_region(disk_devt(disk), disk->minors);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100638
639 part_stat_set_all(&disk->part0, 0);
640 disk->part0.stamp = 0;
641
642 kobject_put(disk->part0.holder_dir);
643 kobject_put(disk->slave_dir);
644 disk->driverfs_dev = NULL;
645 if (!sysfs_deprecated)
646 sysfs_remove_link(block_depr, dev_name(disk_to_dev(disk)));
647 device_del(disk_to_dev(disk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648}
Tejun Heod2bf1b62010-12-08 20:57:36 +0100649EXPORT_SYMBOL(del_gendisk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651/**
652 * get_gendisk - get partitioning information for a given device
Randy Dunlap710027a2008-08-19 20:13:11 +0200653 * @devt: device to get partitioning information for
Randy Dunlap496aa8a2008-10-16 07:46:23 +0200654 * @partno: returned partition index
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 *
656 * This function gets the structure containing partitioning
Randy Dunlap710027a2008-08-19 20:13:11 +0200657 * information for the given device @devt.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 */
Tejun Heocf771cb2008-09-03 09:01:09 +0200659struct gendisk *get_gendisk(dev_t devt, int *partno)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
Tejun Heobcce3de2008-08-25 19:47:22 +0900661 struct gendisk *disk = NULL;
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200662
Tejun Heobcce3de2008-08-25 19:47:22 +0900663 if (MAJOR(devt) != BLOCK_EXT_MAJOR) {
664 struct kobject *kobj;
665
666 kobj = kobj_lookup(bdev_map, devt, partno);
667 if (kobj)
668 disk = dev_to_disk(kobj_to_dev(kobj));
669 } else {
670 struct hd_struct *part;
671
672 mutex_lock(&ext_devt_mutex);
Tejun Heo870d6652008-08-25 19:47:25 +0900673 part = idr_find(&ext_devt_idr, blk_mangle_minor(MINOR(devt)));
Tejun Heobcce3de2008-08-25 19:47:22 +0900674 if (part && get_disk(part_to_disk(part))) {
675 *partno = part->partno;
676 disk = part_to_disk(part);
677 }
678 mutex_unlock(&ext_devt_mutex);
679 }
680
681 return disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682}
Divyesh Shahb6ac23af2010-04-15 08:54:59 +0200683EXPORT_SYMBOL(get_gendisk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Tejun Heof331c022008-09-03 09:01:48 +0200685/**
686 * bdget_disk - do bdget() by gendisk and partition number
687 * @disk: gendisk of interest
688 * @partno: partition number
689 *
690 * Find partition @partno from @disk, do bdget() on it.
691 *
692 * CONTEXT:
693 * Don't care.
694 *
695 * RETURNS:
696 * Resulting block_device on success, NULL on failure.
697 */
Harvey Harrisonaeb3d3a2008-08-28 09:27:42 +0200698struct block_device *bdget_disk(struct gendisk *disk, int partno)
Tejun Heof331c022008-09-03 09:01:48 +0200699{
Tejun Heo548b10e2008-08-29 09:01:47 +0200700 struct hd_struct *part;
701 struct block_device *bdev = NULL;
Tejun Heof331c022008-09-03 09:01:48 +0200702
Tejun Heo548b10e2008-08-29 09:01:47 +0200703 part = disk_get_part(disk, partno);
Tejun Heo2bbedcb2008-08-29 11:41:51 +0200704 if (part)
Tejun Heo548b10e2008-08-29 09:01:47 +0200705 bdev = bdget(part_devt(part));
706 disk_put_part(part);
Tejun Heof331c022008-09-03 09:01:48 +0200707
Tejun Heo548b10e2008-08-29 09:01:47 +0200708 return bdev;
Tejun Heof331c022008-09-03 09:01:48 +0200709}
710EXPORT_SYMBOL(bdget_disk);
711
Dave Gilbertdd2a3452007-05-09 02:33:24 -0700712/*
713 * print a full list of all partitions - intended for places where the root
714 * filesystem can't be mounted and thus to give the victim some idea of what
715 * went wrong
716 */
717void __init printk_all_partitions(void)
718{
Tejun Heodef4e382008-09-03 08:57:12 +0200719 struct class_dev_iter iter;
720 struct device *dev;
721
722 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
723 while ((dev = class_dev_iter_next(&iter))) {
724 struct gendisk *disk = dev_to_disk(dev);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200725 struct disk_part_iter piter;
726 struct hd_struct *part;
Tejun Heo1f014292008-08-25 19:47:23 +0900727 char name_buf[BDEVNAME_SIZE];
728 char devt_buf[BDEVT_SIZE];
Will Drewryb5af9212010-08-31 15:47:07 -0500729 u8 uuid[PARTITION_META_INFO_UUIDLTH * 2 + 1];
Tejun Heodef4e382008-09-03 08:57:12 +0200730
731 /*
732 * Don't show empty devices or things that have been
733 * surpressed
734 */
735 if (get_capacity(disk) == 0 ||
736 (disk->flags & GENHD_FL_SUPPRESS_PARTITION_INFO))
737 continue;
738
739 /*
740 * Note, unlike /proc/partitions, I am showing the
741 * numbers in hex - the same format as the root=
742 * option takes.
743 */
Tejun Heo074a7ac2008-08-25 19:56:14 +0900744 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
745 while ((part = disk_part_iter_next(&piter))) {
746 bool is_part0 = part == &disk->part0;
Tejun Heodef4e382008-09-03 08:57:12 +0200747
Will Drewryb5af9212010-08-31 15:47:07 -0500748 uuid[0] = 0;
749 if (part->info)
750 part_unpack_uuid(part->info->uuid, uuid);
751
752 printk("%s%s %10llu %s %s", is_part0 ? "" : " ",
Tejun Heo1f014292008-08-25 19:47:23 +0900753 bdevt_str(part_devt(part), devt_buf),
Tejun Heof331c022008-09-03 09:01:48 +0200754 (unsigned long long)part->nr_sects >> 1,
Will Drewryb5af9212010-08-31 15:47:07 -0500755 disk_name(disk, part->partno, name_buf), uuid);
Tejun Heo074a7ac2008-08-25 19:56:14 +0900756 if (is_part0) {
757 if (disk->driverfs_dev != NULL &&
758 disk->driverfs_dev->driver != NULL)
759 printk(" driver: %s\n",
760 disk->driverfs_dev->driver->name);
761 else
762 printk(" (driver?)\n");
763 } else
764 printk("\n");
765 }
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200766 disk_part_iter_exit(&piter);
Tejun Heodef4e382008-09-03 08:57:12 +0200767 }
768 class_dev_iter_exit(&iter);
Dave Gilbertdd2a3452007-05-09 02:33:24 -0700769}
770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771#ifdef CONFIG_PROC_FS
772/* iterator */
Tejun Heodef4e382008-09-03 08:57:12 +0200773static void *disk_seqf_start(struct seq_file *seqf, loff_t *pos)
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -0400774{
Tejun Heodef4e382008-09-03 08:57:12 +0200775 loff_t skip = *pos;
776 struct class_dev_iter *iter;
777 struct device *dev;
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -0400778
Harvey Harrisonaeb3d3a2008-08-28 09:27:42 +0200779 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
Tejun Heodef4e382008-09-03 08:57:12 +0200780 if (!iter)
781 return ERR_PTR(-ENOMEM);
782
783 seqf->private = iter;
784 class_dev_iter_init(iter, &block_class, NULL, &disk_type);
785 do {
786 dev = class_dev_iter_next(iter);
787 if (!dev)
788 return NULL;
789 } while (skip--);
790
791 return dev_to_disk(dev);
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -0400792}
793
Tejun Heodef4e382008-09-03 08:57:12 +0200794static void *disk_seqf_next(struct seq_file *seqf, void *v, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200796 struct device *dev;
Greg Kroah-Hartman66c64af2008-05-22 17:21:08 -0400797
Tejun Heodef4e382008-09-03 08:57:12 +0200798 (*pos)++;
799 dev = class_dev_iter_next(seqf->private);
Tejun Heo2ac3cee2008-09-03 08:53:37 +0200800 if (dev)
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -0400801 return dev_to_disk(dev);
Tejun Heo2ac3cee2008-09-03 08:53:37 +0200802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 return NULL;
804}
805
Tejun Heodef4e382008-09-03 08:57:12 +0200806static void disk_seqf_stop(struct seq_file *seqf, void *v)
Greg Kroah-Hartman27f30252008-05-22 17:21:08 -0400807{
Tejun Heodef4e382008-09-03 08:57:12 +0200808 struct class_dev_iter *iter = seqf->private;
Greg Kroah-Hartman27f30252008-05-22 17:21:08 -0400809
Tejun Heodef4e382008-09-03 08:57:12 +0200810 /* stop is called even after start failed :-( */
811 if (iter) {
812 class_dev_iter_exit(iter);
813 kfree(iter);
Kay Sievers5c0ef6d2008-08-16 14:30:30 +0200814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815}
816
Tejun Heodef4e382008-09-03 08:57:12 +0200817static void *show_partition_start(struct seq_file *seqf, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
Tejun Heodef4e382008-09-03 08:57:12 +0200819 static void *p;
820
821 p = disk_seqf_start(seqf, pos);
Tejun Heo243294d2008-09-04 09:17:31 +0200822 if (!IS_ERR(p) && p && !*pos)
Tejun Heodef4e382008-09-03 08:57:12 +0200823 seq_puts(seqf, "major minor #blocks name\n\n");
824 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825}
826
Tejun Heocf771cb2008-09-03 09:01:09 +0200827static int show_partition(struct seq_file *seqf, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828{
829 struct gendisk *sgp = v;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200830 struct disk_part_iter piter;
831 struct hd_struct *part;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 char buf[BDEVNAME_SIZE];
833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 /* Don't show non-partitionable removeable devices or empty devices */
Tejun Heob5d0b9d2008-09-03 09:06:42 +0200835 if (!get_capacity(sgp) || (!disk_partitionable(sgp) &&
Tejun Heof331c022008-09-03 09:01:48 +0200836 (sgp->flags & GENHD_FL_REMOVABLE)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 return 0;
838 if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)
839 return 0;
840
841 /* show the full disk and all non-0 size partitions of it */
Tejun Heo074a7ac2008-08-25 19:56:14 +0900842 disk_part_iter_init(&piter, sgp, DISK_PITER_INCL_PART0);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200843 while ((part = disk_part_iter_next(&piter)))
Tejun Heo1f014292008-08-25 19:47:23 +0900844 seq_printf(seqf, "%4d %7d %10llu %s\n",
Tejun Heof331c022008-09-03 09:01:48 +0200845 MAJOR(part_devt(part)), MINOR(part_devt(part)),
846 (unsigned long long)part->nr_sects >> 1,
847 disk_name(sgp, part->partno, buf));
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200848 disk_part_iter_exit(&piter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
850 return 0;
851}
852
Alexey Dobriyanf5009752008-10-04 23:53:21 +0400853static const struct seq_operations partitions_op = {
Tejun Heodef4e382008-09-03 08:57:12 +0200854 .start = show_partition_start,
855 .next = disk_seqf_next,
856 .stop = disk_seqf_stop,
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200857 .show = show_partition
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858};
Alexey Dobriyanf5009752008-10-04 23:53:21 +0400859
860static int partitions_open(struct inode *inode, struct file *file)
861{
862 return seq_open(file, &partitions_op);
863}
864
865static const struct file_operations proc_partitions_operations = {
866 .open = partitions_open,
867 .read = seq_read,
868 .llseek = seq_lseek,
869 .release = seq_release,
870};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871#endif
872
873
Tejun Heocf771cb2008-09-03 09:01:09 +0200874static struct kobject *base_probe(dev_t devt, int *partno, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200876 if (request_module("block-major-%d-%d", MAJOR(devt), MINOR(devt)) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 /* Make old-style 2.4 aliases work */
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200878 request_module("block-major-%d", MAJOR(devt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 return NULL;
880}
881
882static int __init genhd_device_init(void)
883{
Dan Williamse105b8b2008-04-21 10:51:07 -0700884 int error;
885
886 block_class.dev_kobj = sysfs_dev_block_kobj;
887 error = class_register(&block_class);
Roland McGrathee27a552008-03-11 17:13:15 -0700888 if (unlikely(error))
889 return error;
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200890 bdev_map = kobj_map_init(base_probe, &block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 blk_dev_init();
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200892
Zhang, Yanmin561ec682008-11-14 08:26:30 +0100893 register_blkdev(BLOCK_EXT_MAJOR, "blkext");
894
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200895 /* create top-level block dir */
Andi Kleene52eec12010-09-08 16:54:17 +0200896 if (!sysfs_deprecated)
897 block_depr = kobject_create_and_add("block", NULL);
Greg Kroah-Hartman830d3cf2007-11-06 10:36:58 -0800898 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899}
900
901subsys_initcall(genhd_device_init);
902
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200903static ssize_t disk_range_show(struct device *dev,
904 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200906 struct gendisk *disk = dev_to_disk(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200908 return sprintf(buf, "%d\n", disk->minors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909}
910
Tejun Heo1f014292008-08-25 19:47:23 +0900911static ssize_t disk_ext_range_show(struct device *dev,
912 struct device_attribute *attr, char *buf)
913{
914 struct gendisk *disk = dev_to_disk(dev);
915
Tejun Heob5d0b9d2008-09-03 09:06:42 +0200916 return sprintf(buf, "%d\n", disk_max_parts(disk));
Tejun Heo1f014292008-08-25 19:47:23 +0900917}
918
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200919static ssize_t disk_removable_show(struct device *dev,
920 struct device_attribute *attr, char *buf)
Kay Sieversa7fd6702005-10-01 14:49:43 +0200921{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200922 struct gendisk *disk = dev_to_disk(dev);
Kay Sieversa7fd6702005-10-01 14:49:43 +0200923
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200924 return sprintf(buf, "%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200926}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Kay Sievers1c9ce522008-06-13 09:41:00 +0200928static ssize_t disk_ro_show(struct device *dev,
929 struct device_attribute *attr, char *buf)
930{
931 struct gendisk *disk = dev_to_disk(dev);
932
Tejun Heob7db9952008-08-25 19:56:10 +0900933 return sprintf(buf, "%d\n", get_disk_ro(disk) ? 1 : 0);
Kay Sievers1c9ce522008-06-13 09:41:00 +0200934}
935
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200936static ssize_t disk_capability_show(struct device *dev,
937 struct device_attribute *attr, char *buf)
Kristen Carlson Accardi86ce18d2007-05-23 13:57:38 -0700938{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200939 struct gendisk *disk = dev_to_disk(dev);
940
941 return sprintf(buf, "%x\n", disk->flags);
Kristen Carlson Accardi86ce18d2007-05-23 13:57:38 -0700942}
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200943
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400944static ssize_t disk_alignment_offset_show(struct device *dev,
945 struct device_attribute *attr,
946 char *buf)
947{
948 struct gendisk *disk = dev_to_disk(dev);
949
950 return sprintf(buf, "%d\n", queue_alignment_offset(disk->queue));
951}
952
Martin K. Petersen86b37282009-11-10 11:50:21 +0100953static ssize_t disk_discard_alignment_show(struct device *dev,
954 struct device_attribute *attr,
955 char *buf)
956{
957 struct gendisk *disk = dev_to_disk(dev);
958
Martin K. Petersendd3d1452010-01-11 03:21:48 -0500959 return sprintf(buf, "%d\n", queue_discard_alignment(disk->queue));
Martin K. Petersen86b37282009-11-10 11:50:21 +0100960}
961
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200962static DEVICE_ATTR(range, S_IRUGO, disk_range_show, NULL);
Tejun Heo1f014292008-08-25 19:47:23 +0900963static DEVICE_ATTR(ext_range, S_IRUGO, disk_ext_range_show, NULL);
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200964static DEVICE_ATTR(removable, S_IRUGO, disk_removable_show, NULL);
Kay Sievers1c9ce522008-06-13 09:41:00 +0200965static DEVICE_ATTR(ro, S_IRUGO, disk_ro_show, NULL);
Tejun Heoe5610522008-08-25 19:56:09 +0900966static DEVICE_ATTR(size, S_IRUGO, part_size_show, NULL);
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400967static DEVICE_ATTR(alignment_offset, S_IRUGO, disk_alignment_offset_show, NULL);
Martin K. Petersen86b37282009-11-10 11:50:21 +0100968static DEVICE_ATTR(discard_alignment, S_IRUGO, disk_discard_alignment_show,
969 NULL);
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200970static DEVICE_ATTR(capability, S_IRUGO, disk_capability_show, NULL);
Tejun Heo074a7ac2008-08-25 19:56:14 +0900971static DEVICE_ATTR(stat, S_IRUGO, part_stat_show, NULL);
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200972static DEVICE_ATTR(inflight, S_IRUGO, part_inflight_show, NULL);
Akinobu Mitac17bb492006-12-08 02:39:46 -0800973#ifdef CONFIG_FAIL_MAKE_REQUEST
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200974static struct device_attribute dev_attr_fail =
Tejun Heoeddb2e22008-08-25 19:56:13 +0900975 __ATTR(make-it-fail, S_IRUGO|S_IWUSR, part_fail_show, part_fail_store);
Akinobu Mitac17bb492006-12-08 02:39:46 -0800976#endif
Jens Axboe581d4e22008-09-14 05:56:33 -0700977#ifdef CONFIG_FAIL_IO_TIMEOUT
978static struct device_attribute dev_attr_fail_timeout =
979 __ATTR(io-timeout-fail, S_IRUGO|S_IWUSR, part_timeout_show,
980 part_timeout_store);
981#endif
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200982
983static struct attribute *disk_attrs[] = {
984 &dev_attr_range.attr,
Tejun Heo1f014292008-08-25 19:47:23 +0900985 &dev_attr_ext_range.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200986 &dev_attr_removable.attr,
Kay Sievers1c9ce522008-06-13 09:41:00 +0200987 &dev_attr_ro.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200988 &dev_attr_size.attr,
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400989 &dev_attr_alignment_offset.attr,
Martin K. Petersen86b37282009-11-10 11:50:21 +0100990 &dev_attr_discard_alignment.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200991 &dev_attr_capability.attr,
992 &dev_attr_stat.attr,
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200993 &dev_attr_inflight.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200994#ifdef CONFIG_FAIL_MAKE_REQUEST
995 &dev_attr_fail.attr,
996#endif
Jens Axboe581d4e22008-09-14 05:56:33 -0700997#ifdef CONFIG_FAIL_IO_TIMEOUT
998 &dev_attr_fail_timeout.attr,
999#endif
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001000 NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001};
1002
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001003static struct attribute_group disk_attr_group = {
1004 .attrs = disk_attrs,
1005};
1006
David Brownella4dbd672009-06-24 10:06:31 -07001007static const struct attribute_group *disk_attr_groups[] = {
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001008 &disk_attr_group,
1009 NULL
1010};
1011
Tejun Heo540eed52008-08-25 19:56:15 +09001012static void disk_free_ptbl_rcu_cb(struct rcu_head *head)
1013{
1014 struct disk_part_tbl *ptbl =
1015 container_of(head, struct disk_part_tbl, rcu_head);
1016
1017 kfree(ptbl);
1018}
1019
1020/**
1021 * disk_replace_part_tbl - replace disk->part_tbl in RCU-safe way
1022 * @disk: disk to replace part_tbl for
1023 * @new_ptbl: new part_tbl to install
1024 *
1025 * Replace disk->part_tbl with @new_ptbl in RCU-safe way. The
1026 * original ptbl is freed using RCU callback.
1027 *
1028 * LOCKING:
1029 * Matching bd_mutx locked.
1030 */
1031static void disk_replace_part_tbl(struct gendisk *disk,
1032 struct disk_part_tbl *new_ptbl)
1033{
1034 struct disk_part_tbl *old_ptbl = disk->part_tbl;
1035
1036 rcu_assign_pointer(disk->part_tbl, new_ptbl);
Jens Axboea6f23652008-10-24 12:52:42 +02001037
1038 if (old_ptbl) {
1039 rcu_assign_pointer(old_ptbl->last_lookup, NULL);
Tejun Heo540eed52008-08-25 19:56:15 +09001040 call_rcu(&old_ptbl->rcu_head, disk_free_ptbl_rcu_cb);
Jens Axboea6f23652008-10-24 12:52:42 +02001041 }
Tejun Heo540eed52008-08-25 19:56:15 +09001042}
1043
1044/**
1045 * disk_expand_part_tbl - expand disk->part_tbl
1046 * @disk: disk to expand part_tbl for
1047 * @partno: expand such that this partno can fit in
1048 *
1049 * Expand disk->part_tbl such that @partno can fit in. disk->part_tbl
1050 * uses RCU to allow unlocked dereferencing for stats and other stuff.
1051 *
1052 * LOCKING:
1053 * Matching bd_mutex locked, might sleep.
1054 *
1055 * RETURNS:
1056 * 0 on success, -errno on failure.
1057 */
1058int disk_expand_part_tbl(struct gendisk *disk, int partno)
1059{
1060 struct disk_part_tbl *old_ptbl = disk->part_tbl;
1061 struct disk_part_tbl *new_ptbl;
1062 int len = old_ptbl ? old_ptbl->len : 0;
1063 int target = partno + 1;
1064 size_t size;
1065 int i;
1066
1067 /* disk_max_parts() is zero during initialization, ignore if so */
1068 if (disk_max_parts(disk) && target > disk_max_parts(disk))
1069 return -EINVAL;
1070
1071 if (target <= len)
1072 return 0;
1073
1074 size = sizeof(*new_ptbl) + target * sizeof(new_ptbl->part[0]);
1075 new_ptbl = kzalloc_node(size, GFP_KERNEL, disk->node_id);
1076 if (!new_ptbl)
1077 return -ENOMEM;
1078
Tejun Heo540eed52008-08-25 19:56:15 +09001079 new_ptbl->len = target;
1080
1081 for (i = 0; i < len; i++)
1082 rcu_assign_pointer(new_ptbl->part[i], old_ptbl->part[i]);
1083
1084 disk_replace_part_tbl(disk, new_ptbl);
1085 return 0;
1086}
1087
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001088static void disk_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001090 struct gendisk *disk = dev_to_disk(dev);
1091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 kfree(disk->random);
Tejun Heo540eed52008-08-25 19:56:15 +09001093 disk_replace_part_tbl(disk, NULL);
Tejun Heo074a7ac2008-08-25 19:56:14 +09001094 free_part_stats(&disk->part0);
Will Drewry6d1d8052010-08-31 15:47:05 -05001095 free_part_info(&disk->part0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 kfree(disk);
1097}
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001098struct class block_class = {
1099 .name = "block",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100};
1101
Kay Sieverse454cea2009-09-18 23:01:12 +02001102static char *block_devnode(struct device *dev, mode_t *mode)
Kay Sieversb03f38b2009-04-30 15:23:42 +02001103{
1104 struct gendisk *disk = dev_to_disk(dev);
1105
Kay Sieverse454cea2009-09-18 23:01:12 +02001106 if (disk->devnode)
1107 return disk->devnode(disk, mode);
Kay Sieversb03f38b2009-04-30 15:23:42 +02001108 return NULL;
1109}
1110
Adrian Bunk1826ead2008-03-04 11:23:46 +01001111static struct device_type disk_type = {
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001112 .name = "disk",
1113 .groups = disk_attr_groups,
1114 .release = disk_release,
Kay Sieverse454cea2009-09-18 23:01:12 +02001115 .devnode = block_devnode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116};
1117
Randy Dunlapa6e2ba82008-05-23 09:44:11 -07001118#ifdef CONFIG_PROC_FS
Tejun Heocf771cb2008-09-03 09:01:09 +02001119/*
1120 * aggregate disk stat collector. Uses the same stats that the sysfs
1121 * entries do, above, but makes them available through one seq_file.
1122 *
1123 * The output looks suspiciously like /proc/partitions with a bunch of
1124 * extra fields.
1125 */
1126static int diskstats_show(struct seq_file *seqf, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127{
1128 struct gendisk *gp = v;
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001129 struct disk_part_iter piter;
1130 struct hd_struct *hd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 char buf[BDEVNAME_SIZE];
Tejun Heoc9959052008-08-25 19:47:21 +09001132 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
1134 /*
Tejun Heoed9e1982008-08-25 19:56:05 +09001135 if (&disk_to_dev(gp)->kobj.entry == block_class.devices.next)
Tejun Heocf771cb2008-09-03 09:01:09 +02001136 seq_puts(seqf, "major minor name"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 " rio rmerge rsect ruse wio wmerge "
1138 "wsect wuse running use aveq"
1139 "\n\n");
1140 */
1141
Tejun Heo71982a42009-04-17 08:34:48 +02001142 disk_part_iter_init(&piter, gp, DISK_PITER_INCL_EMPTY_PART0);
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001143 while ((hd = disk_part_iter_next(&piter))) {
Tejun Heo074a7ac2008-08-25 19:56:14 +09001144 cpu = part_stat_lock();
Tejun Heoc9959052008-08-25 19:47:21 +09001145 part_round_stats(cpu, hd);
Tejun Heo074a7ac2008-08-25 19:56:14 +09001146 part_stat_unlock();
Tejun Heo1f014292008-08-25 19:47:23 +09001147 seq_printf(seqf, "%4d %7d %s %lu %lu %llu "
Jerome Marchand28f39d52008-02-08 11:04:56 +01001148 "%u %lu %lu %llu %u %u %u %u\n",
Tejun Heof331c022008-09-03 09:01:48 +02001149 MAJOR(part_devt(hd)), MINOR(part_devt(hd)),
1150 disk_name(gp, hd->partno, buf),
Jerome Marchand28f39d52008-02-08 11:04:56 +01001151 part_stat_read(hd, ios[0]),
1152 part_stat_read(hd, merges[0]),
1153 (unsigned long long)part_stat_read(hd, sectors[0]),
1154 jiffies_to_msecs(part_stat_read(hd, ticks[0])),
1155 part_stat_read(hd, ios[1]),
1156 part_stat_read(hd, merges[1]),
1157 (unsigned long long)part_stat_read(hd, sectors[1]),
1158 jiffies_to_msecs(part_stat_read(hd, ticks[1])),
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001159 part_in_flight(hd),
Jerome Marchand28f39d52008-02-08 11:04:56 +01001160 jiffies_to_msecs(part_stat_read(hd, io_ticks)),
1161 jiffies_to_msecs(part_stat_read(hd, time_in_queue))
1162 );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 }
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001164 disk_part_iter_exit(&piter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
1166 return 0;
1167}
1168
Alexey Dobriyan31d85ab2008-10-06 12:55:38 +04001169static const struct seq_operations diskstats_op = {
Tejun Heodef4e382008-09-03 08:57:12 +02001170 .start = disk_seqf_start,
1171 .next = disk_seqf_next,
1172 .stop = disk_seqf_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 .show = diskstats_show
1174};
Alexey Dobriyanf5009752008-10-04 23:53:21 +04001175
Alexey Dobriyan31d85ab2008-10-06 12:55:38 +04001176static int diskstats_open(struct inode *inode, struct file *file)
1177{
1178 return seq_open(file, &diskstats_op);
1179}
1180
1181static const struct file_operations proc_diskstats_operations = {
1182 .open = diskstats_open,
1183 .read = seq_read,
1184 .llseek = seq_lseek,
1185 .release = seq_release,
1186};
1187
Alexey Dobriyanf5009752008-10-04 23:53:21 +04001188static int __init proc_genhd_init(void)
1189{
Alexey Dobriyan31d85ab2008-10-06 12:55:38 +04001190 proc_create("diskstats", 0, NULL, &proc_diskstats_operations);
Alexey Dobriyanf5009752008-10-04 23:53:21 +04001191 proc_create("partitions", 0, NULL, &proc_partitions_operations);
1192 return 0;
1193}
1194module_init(proc_genhd_init);
Randy Dunlapa6e2ba82008-05-23 09:44:11 -07001195#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Tejun Heocf771cb2008-09-03 09:01:09 +02001197dev_t blk_lookup_devt(const char *name, int partno)
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001198{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001199 dev_t devt = MKDEV(0, 0);
Tejun Heodef4e382008-09-03 08:57:12 +02001200 struct class_dev_iter iter;
1201 struct device *dev;
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001202
Tejun Heodef4e382008-09-03 08:57:12 +02001203 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
1204 while ((dev = class_dev_iter_next(&iter))) {
1205 struct gendisk *disk = dev_to_disk(dev);
Tejun Heo548b10e2008-08-29 09:01:47 +02001206 struct hd_struct *part;
Tejun Heodef4e382008-09-03 08:57:12 +02001207
Kay Sievers3ada8b72009-01-06 10:44:43 -08001208 if (strcmp(dev_name(dev), name))
Tejun Heof331c022008-09-03 09:01:48 +02001209 continue;
Tejun Heof331c022008-09-03 09:01:48 +02001210
Neil Brown41b8c852009-02-18 10:33:59 +01001211 if (partno < disk->minors) {
1212 /* We need to return the right devno, even
1213 * if the partition doesn't exist yet.
1214 */
1215 devt = MKDEV(MAJOR(dev->devt),
1216 MINOR(dev->devt) + partno);
1217 break;
1218 }
Tejun Heo548b10e2008-08-29 09:01:47 +02001219 part = disk_get_part(disk, partno);
Tejun Heo2bbedcb2008-08-29 11:41:51 +02001220 if (part) {
Tejun Heof331c022008-09-03 09:01:48 +02001221 devt = part_devt(part);
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001222 disk_put_part(part);
Tejun Heo548b10e2008-08-29 09:01:47 +02001223 break;
Tejun Heodef4e382008-09-03 08:57:12 +02001224 }
Tejun Heo548b10e2008-08-29 09:01:47 +02001225 disk_put_part(part);
Kay Sievers5c0ef6d2008-08-16 14:30:30 +02001226 }
Tejun Heodef4e382008-09-03 08:57:12 +02001227 class_dev_iter_exit(&iter);
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001228 return devt;
1229}
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001230EXPORT_SYMBOL(blk_lookup_devt);
1231
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232struct gendisk *alloc_disk(int minors)
1233{
Christoph Lameter19460892005-06-23 00:08:19 -07001234 return alloc_disk_node(minors, -1);
1235}
Tejun Heo689d6fa2008-08-25 19:56:16 +09001236EXPORT_SYMBOL(alloc_disk);
Christoph Lameter19460892005-06-23 00:08:19 -07001237
1238struct gendisk *alloc_disk_node(int minors, int node_id)
1239{
1240 struct gendisk *disk;
1241
Christoph Lameter94f60302007-07-17 04:03:29 -07001242 disk = kmalloc_node(sizeof(struct gendisk),
1243 GFP_KERNEL | __GFP_ZERO, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 if (disk) {
Tejun Heo074a7ac2008-08-25 19:56:14 +09001245 if (!init_part_stats(&disk->part0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 kfree(disk);
1247 return NULL;
1248 }
Cheng Renquanbf91db12008-11-20 08:37:37 +01001249 disk->node_id = node_id;
Tejun Heo540eed52008-08-25 19:56:15 +09001250 if (disk_expand_part_tbl(disk, 0)) {
1251 free_part_stats(&disk->part0);
Tejun Heob5d0b9d2008-09-03 09:06:42 +02001252 kfree(disk);
1253 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 }
Tejun Heo540eed52008-08-25 19:56:15 +09001255 disk->part_tbl->part[0] = &disk->part0;
Tejun Heob5d0b9d2008-09-03 09:06:42 +02001256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 disk->minors = minors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 rand_initialize_disk(disk);
Tejun Heoed9e1982008-08-25 19:56:05 +09001259 disk_to_dev(disk)->class = &block_class;
1260 disk_to_dev(disk)->type = &disk_type;
1261 device_initialize(disk_to_dev(disk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 }
1263 return disk;
1264}
Christoph Lameter19460892005-06-23 00:08:19 -07001265EXPORT_SYMBOL(alloc_disk_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
1267struct kobject *get_disk(struct gendisk *disk)
1268{
1269 struct module *owner;
1270 struct kobject *kobj;
1271
1272 if (!disk->fops)
1273 return NULL;
1274 owner = disk->fops->owner;
1275 if (owner && !try_module_get(owner))
1276 return NULL;
Tejun Heoed9e1982008-08-25 19:56:05 +09001277 kobj = kobject_get(&disk_to_dev(disk)->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 if (kobj == NULL) {
1279 module_put(owner);
1280 return NULL;
1281 }
1282 return kobj;
1283
1284}
1285
1286EXPORT_SYMBOL(get_disk);
1287
1288void put_disk(struct gendisk *disk)
1289{
1290 if (disk)
Tejun Heoed9e1982008-08-25 19:56:05 +09001291 kobject_put(&disk_to_dev(disk)->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292}
1293
1294EXPORT_SYMBOL(put_disk);
1295
Hannes Reineckee3264a42009-07-28 09:13:13 +02001296static void set_disk_ro_uevent(struct gendisk *gd, int ro)
1297{
1298 char event[] = "DISK_RO=1";
1299 char *envp[] = { event, NULL };
1300
1301 if (!ro)
1302 event[8] = '0';
1303 kobject_uevent_env(&disk_to_dev(gd)->kobj, KOBJ_CHANGE, envp);
1304}
1305
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306void set_device_ro(struct block_device *bdev, int flag)
1307{
Tejun Heob7db9952008-08-25 19:56:10 +09001308 bdev->bd_part->policy = flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309}
1310
1311EXPORT_SYMBOL(set_device_ro);
1312
1313void set_disk_ro(struct gendisk *disk, int flag)
1314{
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001315 struct disk_part_iter piter;
1316 struct hd_struct *part;
1317
Hannes Reineckee3264a42009-07-28 09:13:13 +02001318 if (disk->part0.policy != flag) {
1319 set_disk_ro_uevent(disk, flag);
1320 disk->part0.policy = flag;
1321 }
1322
1323 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_EMPTY);
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001324 while ((part = disk_part_iter_next(&piter)))
1325 part->policy = flag;
1326 disk_part_iter_exit(&piter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327}
1328
1329EXPORT_SYMBOL(set_disk_ro);
1330
1331int bdev_read_only(struct block_device *bdev)
1332{
1333 if (!bdev)
1334 return 0;
Tejun Heob7db9952008-08-25 19:56:10 +09001335 return bdev->bd_part->policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336}
1337
1338EXPORT_SYMBOL(bdev_read_only);
1339
Tejun Heocf771cb2008-09-03 09:01:09 +02001340int invalidate_partition(struct gendisk *disk, int partno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341{
1342 int res = 0;
Tejun Heocf771cb2008-09-03 09:01:09 +02001343 struct block_device *bdev = bdget_disk(disk, partno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 if (bdev) {
Christoph Hellwig2ef41632005-05-05 16:15:59 -07001345 fsync_bdev(bdev);
1346 res = __invalidate_device(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 bdput(bdev);
1348 }
1349 return res;
1350}
1351
1352EXPORT_SYMBOL(invalidate_partition);