blob: c2b14aa69d582961ee8c0b1147396632afdae81c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * gendisk handling
3 */
4
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#include <linux/module.h>
6#include <linux/fs.h>
7#include <linux/genhd.h>
Andrew Mortonb446b602007-02-20 13:57:48 -08008#include <linux/kdev_t.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/kernel.h>
10#include <linux/blkdev.h>
11#include <linux/init.h>
12#include <linux/spinlock.h>
13#include <linux/seq_file.h>
14#include <linux/slab.h>
15#include <linux/kmod.h>
16#include <linux/kobj_map.h>
Christoph Hellwig2ef41632005-05-05 16:15:59 -070017#include <linux/buffer_head.h>
Jes Sorensen58383af2006-02-06 14:12:43 -080018#include <linux/mutex.h>
Tejun Heobcce3de2008-08-25 19:47:22 +090019#include <linux/idr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Adrian Bunkff889722008-03-04 11:23:45 +010021#include "blk.h"
22
Kay Sieversedfaa7c2007-05-21 22:08:01 +020023static DEFINE_MUTEX(block_class_lock);
24#ifndef CONFIG_SYSFS_DEPRECATED
25struct kobject *block_depr;
26#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Tejun Heobcce3de2008-08-25 19:47:22 +090028/* for extended dynamic devt allocation, currently only one major is used */
29#define MAX_EXT_DEVT (1 << MINORBITS)
30
31/* For extended devt allocation. ext_devt_mutex prevents look up
32 * results from going away underneath its user.
33 */
34static DEFINE_MUTEX(ext_devt_mutex);
35static DEFINE_IDR(ext_devt_idr);
36
Adrian Bunk1826ead2008-03-04 11:23:46 +010037static struct device_type disk_type;
38
Tejun Heoe71bf0d2008-09-03 09:03:02 +020039/**
40 * disk_get_part - get partition
41 * @disk: disk to look partition from
42 * @partno: partition number
43 *
44 * Look for partition @partno from @disk. If found, increment
45 * reference count and return it.
46 *
47 * CONTEXT:
48 * Don't care.
49 *
50 * RETURNS:
51 * Pointer to the found partition on success, NULL if not found.
52 */
53struct hd_struct *disk_get_part(struct gendisk *disk, int partno)
54{
Tejun Heo540eed52008-08-25 19:56:15 +090055 struct hd_struct *part = NULL;
56 struct disk_part_tbl *ptbl;
Tejun Heoe71bf0d2008-09-03 09:03:02 +020057
Tejun Heo540eed52008-08-25 19:56:15 +090058 if (unlikely(partno < 0))
Tejun Heoe71bf0d2008-09-03 09:03:02 +020059 return NULL;
Tejun Heo540eed52008-08-25 19:56:15 +090060
Tejun Heoe71bf0d2008-09-03 09:03:02 +020061 rcu_read_lock();
Tejun Heo540eed52008-08-25 19:56:15 +090062
63 ptbl = rcu_dereference(disk->part_tbl);
64 if (likely(partno < ptbl->len)) {
65 part = rcu_dereference(ptbl->part[partno]);
66 if (part)
67 get_device(part_to_dev(part));
68 }
69
Tejun Heoe71bf0d2008-09-03 09:03:02 +020070 rcu_read_unlock();
71
72 return part;
73}
74EXPORT_SYMBOL_GPL(disk_get_part);
75
76/**
77 * disk_part_iter_init - initialize partition iterator
78 * @piter: iterator to initialize
79 * @disk: disk to iterate over
80 * @flags: DISK_PITER_* flags
81 *
82 * Initialize @piter so that it iterates over partitions of @disk.
83 *
84 * CONTEXT:
85 * Don't care.
86 */
87void disk_part_iter_init(struct disk_part_iter *piter, struct gendisk *disk,
88 unsigned int flags)
89{
Tejun Heo540eed52008-08-25 19:56:15 +090090 struct disk_part_tbl *ptbl;
91
92 rcu_read_lock();
93 ptbl = rcu_dereference(disk->part_tbl);
94
Tejun Heoe71bf0d2008-09-03 09:03:02 +020095 piter->disk = disk;
96 piter->part = NULL;
97
98 if (flags & DISK_PITER_REVERSE)
Tejun Heo540eed52008-08-25 19:56:15 +090099 piter->idx = ptbl->len - 1;
Tejun Heob5d0b9d2008-09-03 09:06:42 +0200100 else if (flags & DISK_PITER_INCL_PART0)
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200101 piter->idx = 0;
Tejun Heob5d0b9d2008-09-03 09:06:42 +0200102 else
103 piter->idx = 1;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200104
105 piter->flags = flags;
Tejun Heo540eed52008-08-25 19:56:15 +0900106
107 rcu_read_unlock();
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200108}
109EXPORT_SYMBOL_GPL(disk_part_iter_init);
110
111/**
112 * disk_part_iter_next - proceed iterator to the next partition and return it
113 * @piter: iterator of interest
114 *
115 * Proceed @piter to the next partition and return it.
116 *
117 * CONTEXT:
118 * Don't care.
119 */
120struct hd_struct *disk_part_iter_next(struct disk_part_iter *piter)
121{
Tejun Heo540eed52008-08-25 19:56:15 +0900122 struct disk_part_tbl *ptbl;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200123 int inc, end;
124
125 /* put the last partition */
126 disk_put_part(piter->part);
127 piter->part = NULL;
128
Tejun Heo540eed52008-08-25 19:56:15 +0900129 /* get part_tbl */
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200130 rcu_read_lock();
Tejun Heo540eed52008-08-25 19:56:15 +0900131 ptbl = rcu_dereference(piter->disk->part_tbl);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200132
133 /* determine iteration parameters */
134 if (piter->flags & DISK_PITER_REVERSE) {
135 inc = -1;
Tejun Heob5d0b9d2008-09-03 09:06:42 +0200136 if (piter->flags & DISK_PITER_INCL_PART0)
137 end = -1;
138 else
139 end = 0;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200140 } else {
141 inc = 1;
Tejun Heo540eed52008-08-25 19:56:15 +0900142 end = ptbl->len;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200143 }
144
145 /* iterate to the next partition */
146 for (; piter->idx != end; piter->idx += inc) {
147 struct hd_struct *part;
148
Tejun Heo540eed52008-08-25 19:56:15 +0900149 part = rcu_dereference(ptbl->part[piter->idx]);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200150 if (!part)
151 continue;
152 if (!(piter->flags & DISK_PITER_INCL_EMPTY) && !part->nr_sects)
153 continue;
154
Tejun Heoed9e1982008-08-25 19:56:05 +0900155 get_device(part_to_dev(part));
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200156 piter->part = part;
157 piter->idx += inc;
158 break;
159 }
160
161 rcu_read_unlock();
162
163 return piter->part;
164}
165EXPORT_SYMBOL_GPL(disk_part_iter_next);
166
167/**
168 * disk_part_iter_exit - finish up partition iteration
169 * @piter: iter of interest
170 *
171 * Called when iteration is over. Cleans up @piter.
172 *
173 * CONTEXT:
174 * Don't care.
175 */
176void disk_part_iter_exit(struct disk_part_iter *piter)
177{
178 disk_put_part(piter->part);
179 piter->part = NULL;
180}
181EXPORT_SYMBOL_GPL(disk_part_iter_exit);
182
183/**
184 * disk_map_sector_rcu - map sector to partition
185 * @disk: gendisk of interest
186 * @sector: sector to map
187 *
188 * Find out which partition @sector maps to on @disk. This is
189 * primarily used for stats accounting.
190 *
191 * CONTEXT:
192 * RCU read locked. The returned partition pointer is valid only
193 * while preemption is disabled.
194 *
195 * RETURNS:
Tejun Heo074a7ac2008-08-25 19:56:14 +0900196 * Found partition on success, part0 is returned if no partition matches
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200197 */
198struct hd_struct *disk_map_sector_rcu(struct gendisk *disk, sector_t sector)
199{
Tejun Heo540eed52008-08-25 19:56:15 +0900200 struct disk_part_tbl *ptbl;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200201 int i;
202
Tejun Heo540eed52008-08-25 19:56:15 +0900203 ptbl = rcu_dereference(disk->part_tbl);
204
205 for (i = 1; i < ptbl->len; i++) {
206 struct hd_struct *part = rcu_dereference(ptbl->part[i]);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200207
208 if (part && part->start_sect <= sector &&
209 sector < part->start_sect + part->nr_sects)
210 return part;
211 }
Tejun Heo074a7ac2008-08-25 19:56:14 +0900212 return &disk->part0;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200213}
214EXPORT_SYMBOL_GPL(disk_map_sector_rcu);
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216/*
217 * Can be deleted altogether. Later.
218 *
219 */
220static struct blk_major_name {
221 struct blk_major_name *next;
222 int major;
223 char name[16];
Joe Korty68eef3b2006-03-31 02:30:32 -0800224} *major_names[BLKDEV_MAJOR_HASH_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226/* index in the above - for now: assume no multimajor ranges */
227static inline int major_to_index(int major)
228{
Joe Korty68eef3b2006-03-31 02:30:32 -0800229 return major % BLKDEV_MAJOR_HASH_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
Joe Korty68eef3b2006-03-31 02:30:32 -0800232#ifdef CONFIG_PROC_FS
Tejun Heocf771cb2008-09-03 09:01:09 +0200233void blkdev_show(struct seq_file *seqf, off_t offset)
Neil Horman7170be52006-01-14 13:20:38 -0800234{
Joe Korty68eef3b2006-03-31 02:30:32 -0800235 struct blk_major_name *dp;
Neil Horman7170be52006-01-14 13:20:38 -0800236
Joe Korty68eef3b2006-03-31 02:30:32 -0800237 if (offset < BLKDEV_MAJOR_HASH_SIZE) {
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200238 mutex_lock(&block_class_lock);
Joe Korty68eef3b2006-03-31 02:30:32 -0800239 for (dp = major_names[offset]; dp; dp = dp->next)
Tejun Heocf771cb2008-09-03 09:01:09 +0200240 seq_printf(seqf, "%3d %s\n", dp->major, dp->name);
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200241 mutex_unlock(&block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
Joe Korty68eef3b2006-03-31 02:30:32 -0800244#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246int register_blkdev(unsigned int major, const char *name)
247{
248 struct blk_major_name **n, *p;
249 int index, ret = 0;
250
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200251 mutex_lock(&block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 /* temporary */
254 if (major == 0) {
255 for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) {
256 if (major_names[index] == NULL)
257 break;
258 }
259
260 if (index == 0) {
261 printk("register_blkdev: failed to get major for %s\n",
262 name);
263 ret = -EBUSY;
264 goto out;
265 }
266 major = index;
267 ret = major;
268 }
269
270 p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL);
271 if (p == NULL) {
272 ret = -ENOMEM;
273 goto out;
274 }
275
276 p->major = major;
277 strlcpy(p->name, name, sizeof(p->name));
278 p->next = NULL;
279 index = major_to_index(major);
280
281 for (n = &major_names[index]; *n; n = &(*n)->next) {
282 if ((*n)->major == major)
283 break;
284 }
285 if (!*n)
286 *n = p;
287 else
288 ret = -EBUSY;
289
290 if (ret < 0) {
291 printk("register_blkdev: cannot get major %d for %s\n",
292 major, name);
293 kfree(p);
294 }
295out:
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200296 mutex_unlock(&block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 return ret;
298}
299
300EXPORT_SYMBOL(register_blkdev);
301
Akinobu Mitaf4480242007-07-17 04:03:47 -0700302void unregister_blkdev(unsigned int major, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
304 struct blk_major_name **n;
305 struct blk_major_name *p = NULL;
306 int index = major_to_index(major);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200308 mutex_lock(&block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 for (n = &major_names[index]; *n; n = &(*n)->next)
310 if ((*n)->major == major)
311 break;
Akinobu Mita294462a2007-07-17 04:03:45 -0700312 if (!*n || strcmp((*n)->name, name)) {
313 WARN_ON(1);
Akinobu Mita294462a2007-07-17 04:03:45 -0700314 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 p = *n;
316 *n = p->next;
317 }
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200318 mutex_unlock(&block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320}
321
322EXPORT_SYMBOL(unregister_blkdev);
323
324static struct kobj_map *bdev_map;
325
Tejun Heobcce3de2008-08-25 19:47:22 +0900326/**
Tejun Heo870d6652008-08-25 19:47:25 +0900327 * blk_mangle_minor - scatter minor numbers apart
328 * @minor: minor number to mangle
329 *
330 * Scatter consecutively allocated @minor number apart if MANGLE_DEVT
331 * is enabled. Mangling twice gives the original value.
332 *
333 * RETURNS:
334 * Mangled value.
335 *
336 * CONTEXT:
337 * Don't care.
338 */
339static int blk_mangle_minor(int minor)
340{
341#ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT
342 int i;
343
344 for (i = 0; i < MINORBITS / 2; i++) {
345 int low = minor & (1 << i);
346 int high = minor & (1 << (MINORBITS - 1 - i));
347 int distance = MINORBITS - 1 - 2 * i;
348
349 minor ^= low | high; /* clear both bits */
350 low <<= distance; /* swap the positions */
351 high >>= distance;
352 minor |= low | high; /* and set */
353 }
354#endif
355 return minor;
356}
357
358/**
Tejun Heobcce3de2008-08-25 19:47:22 +0900359 * blk_alloc_devt - allocate a dev_t for a partition
360 * @part: partition to allocate dev_t for
361 * @gfp_mask: memory allocation flag
362 * @devt: out parameter for resulting dev_t
363 *
364 * Allocate a dev_t for block device.
365 *
366 * RETURNS:
367 * 0 on success, allocated dev_t is returned in *@devt. -errno on
368 * failure.
369 *
370 * CONTEXT:
371 * Might sleep.
372 */
373int blk_alloc_devt(struct hd_struct *part, dev_t *devt)
374{
375 struct gendisk *disk = part_to_disk(part);
376 int idx, rc;
377
378 /* in consecutive minor range? */
379 if (part->partno < disk->minors) {
380 *devt = MKDEV(disk->major, disk->first_minor + part->partno);
381 return 0;
382 }
383
384 /* allocate ext devt */
385 do {
386 if (!idr_pre_get(&ext_devt_idr, GFP_KERNEL))
387 return -ENOMEM;
388 rc = idr_get_new(&ext_devt_idr, part, &idx);
389 } while (rc == -EAGAIN);
390
391 if (rc)
392 return rc;
393
394 if (idx > MAX_EXT_DEVT) {
395 idr_remove(&ext_devt_idr, idx);
396 return -EBUSY;
397 }
398
Tejun Heo870d6652008-08-25 19:47:25 +0900399 *devt = MKDEV(BLOCK_EXT_MAJOR, blk_mangle_minor(idx));
Tejun Heobcce3de2008-08-25 19:47:22 +0900400 return 0;
401}
402
403/**
404 * blk_free_devt - free a dev_t
405 * @devt: dev_t to free
406 *
407 * Free @devt which was allocated using blk_alloc_devt().
408 *
409 * CONTEXT:
410 * Might sleep.
411 */
412void blk_free_devt(dev_t devt)
413{
414 might_sleep();
415
416 if (devt == MKDEV(0, 0))
417 return;
418
419 if (MAJOR(devt) == BLOCK_EXT_MAJOR) {
420 mutex_lock(&ext_devt_mutex);
Tejun Heo870d6652008-08-25 19:47:25 +0900421 idr_remove(&ext_devt_idr, blk_mangle_minor(MINOR(devt)));
Tejun Heobcce3de2008-08-25 19:47:22 +0900422 mutex_unlock(&ext_devt_mutex);
423 }
424}
425
Tejun Heo1f014292008-08-25 19:47:23 +0900426static char *bdevt_str(dev_t devt, char *buf)
427{
428 if (MAJOR(devt) <= 0xff && MINOR(devt) <= 0xff) {
429 char tbuf[BDEVT_SIZE];
430 snprintf(tbuf, BDEVT_SIZE, "%02x%02x", MAJOR(devt), MINOR(devt));
431 snprintf(buf, BDEVT_SIZE, "%-9s", tbuf);
432 } else
433 snprintf(buf, BDEVT_SIZE, "%03x:%05x", MAJOR(devt), MINOR(devt));
434
435 return buf;
436}
437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438/*
439 * Register device numbers dev..(dev+range-1)
440 * range must be nonzero
441 * The hash chain is sorted on range, so that subranges can override.
442 */
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200443void blk_register_region(dev_t devt, unsigned long range, struct module *module,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 struct kobject *(*probe)(dev_t, int *, void *),
445 int (*lock)(dev_t, void *), void *data)
446{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200447 kobj_map(bdev_map, devt, range, module, probe, lock, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
449
450EXPORT_SYMBOL(blk_register_region);
451
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200452void blk_unregister_region(dev_t devt, unsigned long range)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200454 kobj_unmap(bdev_map, devt, range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
457EXPORT_SYMBOL(blk_unregister_region);
458
Tejun Heocf771cb2008-09-03 09:01:09 +0200459static struct kobject *exact_match(dev_t devt, int *partno, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
461 struct gendisk *p = data;
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200462
Tejun Heoed9e1982008-08-25 19:56:05 +0900463 return &disk_to_dev(p)->kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
465
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200466static int exact_lock(dev_t devt, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
468 struct gendisk *p = data;
469
470 if (!get_disk(p))
471 return -1;
472 return 0;
473}
474
475/**
476 * add_disk - add partitioning information to kernel list
477 * @disk: per-device partitioning information
478 *
479 * This function registers the partitioning information in @disk
480 * with the kernel.
481 */
482void add_disk(struct gendisk *disk)
483{
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700484 struct backing_dev_info *bdi;
Greg Kroah-Hartman6ffeea72008-05-22 17:21:08 -0400485 int retval;
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 disk->flags |= GENHD_FL_UP;
Tejun Heoed9e1982008-08-25 19:56:05 +0900488 disk_to_dev(disk)->devt = MKDEV(disk->major, disk->first_minor);
Tejun Heof331c022008-09-03 09:01:48 +0200489 blk_register_region(disk_devt(disk), disk->minors, NULL,
490 exact_match, exact_lock, disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 register_disk(disk);
492 blk_register_queue(disk);
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700493
494 bdi = &disk->queue->backing_dev_info;
Tejun Heof331c022008-09-03 09:01:48 +0200495 bdi_register_dev(bdi, disk_devt(disk));
Tejun Heoed9e1982008-08-25 19:56:05 +0900496 retval = sysfs_create_link(&disk_to_dev(disk)->kobj, &bdi->dev->kobj,
497 "bdi");
Greg Kroah-Hartman6ffeea72008-05-22 17:21:08 -0400498 WARN_ON(retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499}
500
501EXPORT_SYMBOL(add_disk);
502EXPORT_SYMBOL(del_gendisk); /* in partitions/check.c */
503
504void unlink_gendisk(struct gendisk *disk)
505{
Tejun Heoed9e1982008-08-25 19:56:05 +0900506 sysfs_remove_link(&disk_to_dev(disk)->kobj, "bdi");
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700507 bdi_unregister(&disk->queue->backing_dev_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 blk_unregister_queue(disk);
Tejun Heof331c022008-09-03 09:01:48 +0200509 blk_unregister_region(disk_devt(disk), disk->minors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510}
511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512/**
513 * get_gendisk - get partitioning information for a given device
Randy Dunlap710027a2008-08-19 20:13:11 +0200514 * @devt: device to get partitioning information for
515 * @part: returned partition index
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 *
517 * This function gets the structure containing partitioning
Randy Dunlap710027a2008-08-19 20:13:11 +0200518 * information for the given device @devt.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 */
Tejun Heocf771cb2008-09-03 09:01:09 +0200520struct gendisk *get_gendisk(dev_t devt, int *partno)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
Tejun Heobcce3de2008-08-25 19:47:22 +0900522 struct gendisk *disk = NULL;
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200523
Tejun Heobcce3de2008-08-25 19:47:22 +0900524 if (MAJOR(devt) != BLOCK_EXT_MAJOR) {
525 struct kobject *kobj;
526
527 kobj = kobj_lookup(bdev_map, devt, partno);
528 if (kobj)
529 disk = dev_to_disk(kobj_to_dev(kobj));
530 } else {
531 struct hd_struct *part;
532
533 mutex_lock(&ext_devt_mutex);
Tejun Heo870d6652008-08-25 19:47:25 +0900534 part = idr_find(&ext_devt_idr, blk_mangle_minor(MINOR(devt)));
Tejun Heobcce3de2008-08-25 19:47:22 +0900535 if (part && get_disk(part_to_disk(part))) {
536 *partno = part->partno;
537 disk = part_to_disk(part);
538 }
539 mutex_unlock(&ext_devt_mutex);
540 }
541
542 return disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
Tejun Heof331c022008-09-03 09:01:48 +0200545/**
546 * bdget_disk - do bdget() by gendisk and partition number
547 * @disk: gendisk of interest
548 * @partno: partition number
549 *
550 * Find partition @partno from @disk, do bdget() on it.
551 *
552 * CONTEXT:
553 * Don't care.
554 *
555 * RETURNS:
556 * Resulting block_device on success, NULL on failure.
557 */
558extern struct block_device *bdget_disk(struct gendisk *disk, int partno)
559{
Tejun Heo548b10e2008-08-29 09:01:47 +0200560 struct hd_struct *part;
561 struct block_device *bdev = NULL;
Tejun Heof331c022008-09-03 09:01:48 +0200562
Tejun Heo548b10e2008-08-29 09:01:47 +0200563 part = disk_get_part(disk, partno);
564 if (part && (part->nr_sects || partno == 0))
565 bdev = bdget(part_devt(part));
566 disk_put_part(part);
Tejun Heof331c022008-09-03 09:01:48 +0200567
Tejun Heo548b10e2008-08-29 09:01:47 +0200568 return bdev;
Tejun Heof331c022008-09-03 09:01:48 +0200569}
570EXPORT_SYMBOL(bdget_disk);
571
Dave Gilbertdd2a3452007-05-09 02:33:24 -0700572/*
573 * print a full list of all partitions - intended for places where the root
574 * filesystem can't be mounted and thus to give the victim some idea of what
575 * went wrong
576 */
577void __init printk_all_partitions(void)
578{
Tejun Heodef4e382008-09-03 08:57:12 +0200579 struct class_dev_iter iter;
580 struct device *dev;
581
582 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
583 while ((dev = class_dev_iter_next(&iter))) {
584 struct gendisk *disk = dev_to_disk(dev);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200585 struct disk_part_iter piter;
586 struct hd_struct *part;
Tejun Heo1f014292008-08-25 19:47:23 +0900587 char name_buf[BDEVNAME_SIZE];
588 char devt_buf[BDEVT_SIZE];
Tejun Heodef4e382008-09-03 08:57:12 +0200589
590 /*
591 * Don't show empty devices or things that have been
592 * surpressed
593 */
594 if (get_capacity(disk) == 0 ||
595 (disk->flags & GENHD_FL_SUPPRESS_PARTITION_INFO))
596 continue;
597
598 /*
599 * Note, unlike /proc/partitions, I am showing the
600 * numbers in hex - the same format as the root=
601 * option takes.
602 */
Tejun Heo074a7ac2008-08-25 19:56:14 +0900603 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
604 while ((part = disk_part_iter_next(&piter))) {
605 bool is_part0 = part == &disk->part0;
Tejun Heodef4e382008-09-03 08:57:12 +0200606
Tejun Heo074a7ac2008-08-25 19:56:14 +0900607 printk("%s%s %10llu %s", is_part0 ? "" : " ",
Tejun Heo1f014292008-08-25 19:47:23 +0900608 bdevt_str(part_devt(part), devt_buf),
Tejun Heof331c022008-09-03 09:01:48 +0200609 (unsigned long long)part->nr_sects >> 1,
Tejun Heo1f014292008-08-25 19:47:23 +0900610 disk_name(disk, part->partno, name_buf));
Tejun Heo074a7ac2008-08-25 19:56:14 +0900611 if (is_part0) {
612 if (disk->driverfs_dev != NULL &&
613 disk->driverfs_dev->driver != NULL)
614 printk(" driver: %s\n",
615 disk->driverfs_dev->driver->name);
616 else
617 printk(" (driver?)\n");
618 } else
619 printk("\n");
620 }
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200621 disk_part_iter_exit(&piter);
Tejun Heodef4e382008-09-03 08:57:12 +0200622 }
623 class_dev_iter_exit(&iter);
Dave Gilbertdd2a3452007-05-09 02:33:24 -0700624}
625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626#ifdef CONFIG_PROC_FS
627/* iterator */
Tejun Heodef4e382008-09-03 08:57:12 +0200628static void *disk_seqf_start(struct seq_file *seqf, loff_t *pos)
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -0400629{
Tejun Heodef4e382008-09-03 08:57:12 +0200630 loff_t skip = *pos;
631 struct class_dev_iter *iter;
632 struct device *dev;
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -0400633
Tejun Heodef4e382008-09-03 08:57:12 +0200634 iter = kmalloc(GFP_KERNEL, sizeof(*iter));
635 if (!iter)
636 return ERR_PTR(-ENOMEM);
637
638 seqf->private = iter;
639 class_dev_iter_init(iter, &block_class, NULL, &disk_type);
640 do {
641 dev = class_dev_iter_next(iter);
642 if (!dev)
643 return NULL;
644 } while (skip--);
645
646 return dev_to_disk(dev);
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -0400647}
648
Tejun Heodef4e382008-09-03 08:57:12 +0200649static void *disk_seqf_next(struct seq_file *seqf, void *v, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200651 struct device *dev;
Greg Kroah-Hartman66c64af2008-05-22 17:21:08 -0400652
Tejun Heodef4e382008-09-03 08:57:12 +0200653 (*pos)++;
654 dev = class_dev_iter_next(seqf->private);
Tejun Heo2ac3cee2008-09-03 08:53:37 +0200655 if (dev)
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -0400656 return dev_to_disk(dev);
Tejun Heo2ac3cee2008-09-03 08:53:37 +0200657
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 return NULL;
659}
660
Tejun Heodef4e382008-09-03 08:57:12 +0200661static void disk_seqf_stop(struct seq_file *seqf, void *v)
Greg Kroah-Hartman27f30252008-05-22 17:21:08 -0400662{
Tejun Heodef4e382008-09-03 08:57:12 +0200663 struct class_dev_iter *iter = seqf->private;
Greg Kroah-Hartman27f30252008-05-22 17:21:08 -0400664
Tejun Heodef4e382008-09-03 08:57:12 +0200665 /* stop is called even after start failed :-( */
666 if (iter) {
667 class_dev_iter_exit(iter);
668 kfree(iter);
Kay Sievers5c0ef6d2008-08-16 14:30:30 +0200669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670}
671
Tejun Heodef4e382008-09-03 08:57:12 +0200672static void *show_partition_start(struct seq_file *seqf, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
Tejun Heodef4e382008-09-03 08:57:12 +0200674 static void *p;
675
676 p = disk_seqf_start(seqf, pos);
677 if (!IS_ERR(p) && p)
678 seq_puts(seqf, "major minor #blocks name\n\n");
679 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680}
681
Tejun Heocf771cb2008-09-03 09:01:09 +0200682static int show_partition(struct seq_file *seqf, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
684 struct gendisk *sgp = v;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200685 struct disk_part_iter piter;
686 struct hd_struct *part;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 char buf[BDEVNAME_SIZE];
688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 /* Don't show non-partitionable removeable devices or empty devices */
Tejun Heob5d0b9d2008-09-03 09:06:42 +0200690 if (!get_capacity(sgp) || (!disk_partitionable(sgp) &&
Tejun Heof331c022008-09-03 09:01:48 +0200691 (sgp->flags & GENHD_FL_REMOVABLE)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 return 0;
693 if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)
694 return 0;
695
696 /* show the full disk and all non-0 size partitions of it */
Tejun Heo074a7ac2008-08-25 19:56:14 +0900697 disk_part_iter_init(&piter, sgp, DISK_PITER_INCL_PART0);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200698 while ((part = disk_part_iter_next(&piter)))
Tejun Heo1f014292008-08-25 19:47:23 +0900699 seq_printf(seqf, "%4d %7d %10llu %s\n",
Tejun Heof331c022008-09-03 09:01:48 +0200700 MAJOR(part_devt(part)), MINOR(part_devt(part)),
701 (unsigned long long)part->nr_sects >> 1,
702 disk_name(sgp, part->partno, buf));
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200703 disk_part_iter_exit(&piter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 return 0;
706}
707
Jan Engelhardt12f32bb2008-01-29 20:57:51 +0100708const struct seq_operations partitions_op = {
Tejun Heodef4e382008-09-03 08:57:12 +0200709 .start = show_partition_start,
710 .next = disk_seqf_next,
711 .stop = disk_seqf_stop,
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200712 .show = show_partition
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713};
714#endif
715
716
Tejun Heocf771cb2008-09-03 09:01:09 +0200717static struct kobject *base_probe(dev_t devt, int *partno, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200719 if (request_module("block-major-%d-%d", MAJOR(devt), MINOR(devt)) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 /* Make old-style 2.4 aliases work */
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200721 request_module("block-major-%d", MAJOR(devt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 return NULL;
723}
724
725static int __init genhd_device_init(void)
726{
Dan Williamse105b8b2008-04-21 10:51:07 -0700727 int error;
728
729 block_class.dev_kobj = sysfs_dev_block_kobj;
730 error = class_register(&block_class);
Roland McGrathee27a552008-03-11 17:13:15 -0700731 if (unlikely(error))
732 return error;
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200733 bdev_map = kobj_map_init(base_probe, &block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 blk_dev_init();
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200735
736#ifndef CONFIG_SYSFS_DEPRECATED
737 /* create top-level block dir */
738 block_depr = kobject_create_and_add("block", NULL);
739#endif
Greg Kroah-Hartman830d3cf2007-11-06 10:36:58 -0800740 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741}
742
743subsys_initcall(genhd_device_init);
744
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200745static ssize_t disk_range_show(struct device *dev,
746 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200748 struct gendisk *disk = dev_to_disk(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200750 return sprintf(buf, "%d\n", disk->minors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751}
752
Tejun Heo1f014292008-08-25 19:47:23 +0900753static ssize_t disk_ext_range_show(struct device *dev,
754 struct device_attribute *attr, char *buf)
755{
756 struct gendisk *disk = dev_to_disk(dev);
757
Tejun Heob5d0b9d2008-09-03 09:06:42 +0200758 return sprintf(buf, "%d\n", disk_max_parts(disk));
Tejun Heo1f014292008-08-25 19:47:23 +0900759}
760
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200761static ssize_t disk_removable_show(struct device *dev,
762 struct device_attribute *attr, char *buf)
Kay Sieversa7fd6702005-10-01 14:49:43 +0200763{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200764 struct gendisk *disk = dev_to_disk(dev);
Kay Sieversa7fd6702005-10-01 14:49:43 +0200765
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200766 return sprintf(buf, "%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200768}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Kay Sievers1c9ce522008-06-13 09:41:00 +0200770static ssize_t disk_ro_show(struct device *dev,
771 struct device_attribute *attr, char *buf)
772{
773 struct gendisk *disk = dev_to_disk(dev);
774
Tejun Heob7db9952008-08-25 19:56:10 +0900775 return sprintf(buf, "%d\n", get_disk_ro(disk) ? 1 : 0);
Kay Sievers1c9ce522008-06-13 09:41:00 +0200776}
777
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200778static ssize_t disk_capability_show(struct device *dev,
779 struct device_attribute *attr, char *buf)
Kristen Carlson Accardi86ce18d2007-05-23 13:57:38 -0700780{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200781 struct gendisk *disk = dev_to_disk(dev);
782
783 return sprintf(buf, "%x\n", disk->flags);
Kristen Carlson Accardi86ce18d2007-05-23 13:57:38 -0700784}
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200785
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200786static DEVICE_ATTR(range, S_IRUGO, disk_range_show, NULL);
Tejun Heo1f014292008-08-25 19:47:23 +0900787static DEVICE_ATTR(ext_range, S_IRUGO, disk_ext_range_show, NULL);
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200788static DEVICE_ATTR(removable, S_IRUGO, disk_removable_show, NULL);
Kay Sievers1c9ce522008-06-13 09:41:00 +0200789static DEVICE_ATTR(ro, S_IRUGO, disk_ro_show, NULL);
Tejun Heoe5610522008-08-25 19:56:09 +0900790static DEVICE_ATTR(size, S_IRUGO, part_size_show, NULL);
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200791static DEVICE_ATTR(capability, S_IRUGO, disk_capability_show, NULL);
Tejun Heo074a7ac2008-08-25 19:56:14 +0900792static DEVICE_ATTR(stat, S_IRUGO, part_stat_show, NULL);
Akinobu Mitac17bb492006-12-08 02:39:46 -0800793#ifdef CONFIG_FAIL_MAKE_REQUEST
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200794static struct device_attribute dev_attr_fail =
Tejun Heoeddb2e22008-08-25 19:56:13 +0900795 __ATTR(make-it-fail, S_IRUGO|S_IWUSR, part_fail_show, part_fail_store);
Akinobu Mitac17bb492006-12-08 02:39:46 -0800796#endif
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200797
798static struct attribute *disk_attrs[] = {
799 &dev_attr_range.attr,
Tejun Heo1f014292008-08-25 19:47:23 +0900800 &dev_attr_ext_range.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200801 &dev_attr_removable.attr,
Kay Sievers1c9ce522008-06-13 09:41:00 +0200802 &dev_attr_ro.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200803 &dev_attr_size.attr,
804 &dev_attr_capability.attr,
805 &dev_attr_stat.attr,
806#ifdef CONFIG_FAIL_MAKE_REQUEST
807 &dev_attr_fail.attr,
808#endif
809 NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810};
811
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200812static struct attribute_group disk_attr_group = {
813 .attrs = disk_attrs,
814};
815
816static struct attribute_group *disk_attr_groups[] = {
817 &disk_attr_group,
818 NULL
819};
820
Tejun Heo540eed52008-08-25 19:56:15 +0900821static void disk_free_ptbl_rcu_cb(struct rcu_head *head)
822{
823 struct disk_part_tbl *ptbl =
824 container_of(head, struct disk_part_tbl, rcu_head);
825
826 kfree(ptbl);
827}
828
829/**
830 * disk_replace_part_tbl - replace disk->part_tbl in RCU-safe way
831 * @disk: disk to replace part_tbl for
832 * @new_ptbl: new part_tbl to install
833 *
834 * Replace disk->part_tbl with @new_ptbl in RCU-safe way. The
835 * original ptbl is freed using RCU callback.
836 *
837 * LOCKING:
838 * Matching bd_mutx locked.
839 */
840static void disk_replace_part_tbl(struct gendisk *disk,
841 struct disk_part_tbl *new_ptbl)
842{
843 struct disk_part_tbl *old_ptbl = disk->part_tbl;
844
845 rcu_assign_pointer(disk->part_tbl, new_ptbl);
846 if (old_ptbl)
847 call_rcu(&old_ptbl->rcu_head, disk_free_ptbl_rcu_cb);
848}
849
850/**
851 * disk_expand_part_tbl - expand disk->part_tbl
852 * @disk: disk to expand part_tbl for
853 * @partno: expand such that this partno can fit in
854 *
855 * Expand disk->part_tbl such that @partno can fit in. disk->part_tbl
856 * uses RCU to allow unlocked dereferencing for stats and other stuff.
857 *
858 * LOCKING:
859 * Matching bd_mutex locked, might sleep.
860 *
861 * RETURNS:
862 * 0 on success, -errno on failure.
863 */
864int disk_expand_part_tbl(struct gendisk *disk, int partno)
865{
866 struct disk_part_tbl *old_ptbl = disk->part_tbl;
867 struct disk_part_tbl *new_ptbl;
868 int len = old_ptbl ? old_ptbl->len : 0;
869 int target = partno + 1;
870 size_t size;
871 int i;
872
873 /* disk_max_parts() is zero during initialization, ignore if so */
874 if (disk_max_parts(disk) && target > disk_max_parts(disk))
875 return -EINVAL;
876
877 if (target <= len)
878 return 0;
879
880 size = sizeof(*new_ptbl) + target * sizeof(new_ptbl->part[0]);
881 new_ptbl = kzalloc_node(size, GFP_KERNEL, disk->node_id);
882 if (!new_ptbl)
883 return -ENOMEM;
884
885 INIT_RCU_HEAD(&new_ptbl->rcu_head);
886 new_ptbl->len = target;
887
888 for (i = 0; i < len; i++)
889 rcu_assign_pointer(new_ptbl->part[i], old_ptbl->part[i]);
890
891 disk_replace_part_tbl(disk, new_ptbl);
892 return 0;
893}
894
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200895static void disk_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200897 struct gendisk *disk = dev_to_disk(dev);
898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 kfree(disk->random);
Tejun Heo540eed52008-08-25 19:56:15 +0900900 disk_replace_part_tbl(disk, NULL);
Tejun Heo074a7ac2008-08-25 19:56:14 +0900901 free_part_stats(&disk->part0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 kfree(disk);
903}
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200904struct class block_class = {
905 .name = "block",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906};
907
Adrian Bunk1826ead2008-03-04 11:23:46 +0100908static struct device_type disk_type = {
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200909 .name = "disk",
910 .groups = disk_attr_groups,
911 .release = disk_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912};
913
Randy Dunlapa6e2ba82008-05-23 09:44:11 -0700914#ifdef CONFIG_PROC_FS
Tejun Heocf771cb2008-09-03 09:01:09 +0200915/*
916 * aggregate disk stat collector. Uses the same stats that the sysfs
917 * entries do, above, but makes them available through one seq_file.
918 *
919 * The output looks suspiciously like /proc/partitions with a bunch of
920 * extra fields.
921 */
922static int diskstats_show(struct seq_file *seqf, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923{
924 struct gendisk *gp = v;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200925 struct disk_part_iter piter;
926 struct hd_struct *hd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 char buf[BDEVNAME_SIZE];
Tejun Heoc9959052008-08-25 19:47:21 +0900928 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
930 /*
Tejun Heoed9e1982008-08-25 19:56:05 +0900931 if (&disk_to_dev(gp)->kobj.entry == block_class.devices.next)
Tejun Heocf771cb2008-09-03 09:01:09 +0200932 seq_puts(seqf, "major minor name"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 " rio rmerge rsect ruse wio wmerge "
934 "wsect wuse running use aveq"
935 "\n\n");
936 */
937
Tejun Heo074a7ac2008-08-25 19:56:14 +0900938 disk_part_iter_init(&piter, gp, DISK_PITER_INCL_PART0);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200939 while ((hd = disk_part_iter_next(&piter))) {
Tejun Heo074a7ac2008-08-25 19:56:14 +0900940 cpu = part_stat_lock();
Tejun Heoc9959052008-08-25 19:47:21 +0900941 part_round_stats(cpu, hd);
Tejun Heo074a7ac2008-08-25 19:56:14 +0900942 part_stat_unlock();
Tejun Heo1f014292008-08-25 19:47:23 +0900943 seq_printf(seqf, "%4d %7d %s %lu %lu %llu "
Jerome Marchand28f39d52008-02-08 11:04:56 +0100944 "%u %lu %lu %llu %u %u %u %u\n",
Tejun Heof331c022008-09-03 09:01:48 +0200945 MAJOR(part_devt(hd)), MINOR(part_devt(hd)),
946 disk_name(gp, hd->partno, buf),
Jerome Marchand28f39d52008-02-08 11:04:56 +0100947 part_stat_read(hd, ios[0]),
948 part_stat_read(hd, merges[0]),
949 (unsigned long long)part_stat_read(hd, sectors[0]),
950 jiffies_to_msecs(part_stat_read(hd, ticks[0])),
951 part_stat_read(hd, ios[1]),
952 part_stat_read(hd, merges[1]),
953 (unsigned long long)part_stat_read(hd, sectors[1]),
954 jiffies_to_msecs(part_stat_read(hd, ticks[1])),
955 hd->in_flight,
956 jiffies_to_msecs(part_stat_read(hd, io_ticks)),
957 jiffies_to_msecs(part_stat_read(hd, time_in_queue))
958 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 }
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200960 disk_part_iter_exit(&piter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
962 return 0;
963}
964
Jan Engelhardt12f32bb2008-01-29 20:57:51 +0100965const struct seq_operations diskstats_op = {
Tejun Heodef4e382008-09-03 08:57:12 +0200966 .start = disk_seqf_start,
967 .next = disk_seqf_next,
968 .stop = disk_seqf_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 .show = diskstats_show
970};
Randy Dunlapa6e2ba82008-05-23 09:44:11 -0700971#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
Kristen Carlson Accardi8ce7ad72007-05-23 13:57:38 -0700973static void media_change_notify_thread(struct work_struct *work)
974{
975 struct gendisk *gd = container_of(work, struct gendisk, async_notify);
976 char event[] = "MEDIA_CHANGE=1";
977 char *envp[] = { event, NULL };
978
979 /*
980 * set enviroment vars to indicate which event this is for
981 * so that user space will know to go check the media status.
982 */
Tejun Heoed9e1982008-08-25 19:56:05 +0900983 kobject_uevent_env(&disk_to_dev(gd)->kobj, KOBJ_CHANGE, envp);
Kristen Carlson Accardi8ce7ad72007-05-23 13:57:38 -0700984 put_device(gd->driverfs_dev);
985}
986
Adrian Bunk1826ead2008-03-04 11:23:46 +0100987#if 0
Kristen Carlson Accardi8ce7ad72007-05-23 13:57:38 -0700988void genhd_media_change_notify(struct gendisk *disk)
989{
990 get_device(disk->driverfs_dev);
991 schedule_work(&disk->async_notify);
992}
993EXPORT_SYMBOL_GPL(genhd_media_change_notify);
Adrian Bunk1826ead2008-03-04 11:23:46 +0100994#endif /* 0 */
Kristen Carlson Accardi8ce7ad72007-05-23 13:57:38 -0700995
Tejun Heocf771cb2008-09-03 09:01:09 +0200996dev_t blk_lookup_devt(const char *name, int partno)
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200997{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200998 dev_t devt = MKDEV(0, 0);
Tejun Heodef4e382008-09-03 08:57:12 +0200999 struct class_dev_iter iter;
1000 struct device *dev;
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001001
Tejun Heodef4e382008-09-03 08:57:12 +02001002 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
1003 while ((dev = class_dev_iter_next(&iter))) {
1004 struct gendisk *disk = dev_to_disk(dev);
Tejun Heo548b10e2008-08-29 09:01:47 +02001005 struct hd_struct *part;
Tejun Heodef4e382008-09-03 08:57:12 +02001006
Tejun Heof331c022008-09-03 09:01:48 +02001007 if (strcmp(dev->bus_id, name))
1008 continue;
Tejun Heof331c022008-09-03 09:01:48 +02001009
Tejun Heo548b10e2008-08-29 09:01:47 +02001010 part = disk_get_part(disk, partno);
1011 if (part && (part->nr_sects || partno == 0)) {
Tejun Heof331c022008-09-03 09:01:48 +02001012 devt = part_devt(part);
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001013 disk_put_part(part);
Tejun Heo548b10e2008-08-29 09:01:47 +02001014 break;
Tejun Heodef4e382008-09-03 08:57:12 +02001015 }
Tejun Heo548b10e2008-08-29 09:01:47 +02001016 disk_put_part(part);
Kay Sievers5c0ef6d2008-08-16 14:30:30 +02001017 }
Tejun Heodef4e382008-09-03 08:57:12 +02001018 class_dev_iter_exit(&iter);
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001019 return devt;
1020}
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001021EXPORT_SYMBOL(blk_lookup_devt);
1022
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023struct gendisk *alloc_disk(int minors)
1024{
Christoph Lameter19460892005-06-23 00:08:19 -07001025 return alloc_disk_node(minors, -1);
1026}
1027
1028struct gendisk *alloc_disk_node(int minors, int node_id)
1029{
Tejun Heobcce3de2008-08-25 19:47:22 +09001030 return alloc_disk_ext_node(minors, 0, node_id);
1031}
1032
1033struct gendisk *alloc_disk_ext(int minors, int ext_minors)
1034{
1035 return alloc_disk_ext_node(minors, ext_minors, -1);
1036}
1037
1038struct gendisk *alloc_disk_ext_node(int minors, int ext_minors, int node_id)
1039{
Christoph Lameter19460892005-06-23 00:08:19 -07001040 struct gendisk *disk;
1041
Christoph Lameter94f60302007-07-17 04:03:29 -07001042 disk = kmalloc_node(sizeof(struct gendisk),
1043 GFP_KERNEL | __GFP_ZERO, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 if (disk) {
Tejun Heo074a7ac2008-08-25 19:56:14 +09001045 if (!init_part_stats(&disk->part0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 kfree(disk);
1047 return NULL;
1048 }
Tejun Heo540eed52008-08-25 19:56:15 +09001049 if (disk_expand_part_tbl(disk, 0)) {
1050 free_part_stats(&disk->part0);
Tejun Heob5d0b9d2008-09-03 09:06:42 +02001051 kfree(disk);
1052 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 }
Tejun Heo540eed52008-08-25 19:56:15 +09001054 disk->part_tbl->part[0] = &disk->part0;
Tejun Heob5d0b9d2008-09-03 09:06:42 +02001055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 disk->minors = minors;
Tejun Heobcce3de2008-08-25 19:47:22 +09001057 disk->ext_minors = ext_minors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 rand_initialize_disk(disk);
Tejun Heoed9e1982008-08-25 19:56:05 +09001059 disk_to_dev(disk)->class = &block_class;
1060 disk_to_dev(disk)->type = &disk_type;
1061 device_initialize(disk_to_dev(disk));
Kristen Carlson Accardi8ce7ad72007-05-23 13:57:38 -07001062 INIT_WORK(&disk->async_notify,
1063 media_change_notify_thread);
Tejun Heo540eed52008-08-25 19:56:15 +09001064 disk->node_id = node_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 }
1066 return disk;
1067}
1068
1069EXPORT_SYMBOL(alloc_disk);
Christoph Lameter19460892005-06-23 00:08:19 -07001070EXPORT_SYMBOL(alloc_disk_node);
Tejun Heobcce3de2008-08-25 19:47:22 +09001071EXPORT_SYMBOL(alloc_disk_ext);
1072EXPORT_SYMBOL(alloc_disk_ext_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
1074struct kobject *get_disk(struct gendisk *disk)
1075{
1076 struct module *owner;
1077 struct kobject *kobj;
1078
1079 if (!disk->fops)
1080 return NULL;
1081 owner = disk->fops->owner;
1082 if (owner && !try_module_get(owner))
1083 return NULL;
Tejun Heoed9e1982008-08-25 19:56:05 +09001084 kobj = kobject_get(&disk_to_dev(disk)->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 if (kobj == NULL) {
1086 module_put(owner);
1087 return NULL;
1088 }
1089 return kobj;
1090
1091}
1092
1093EXPORT_SYMBOL(get_disk);
1094
1095void put_disk(struct gendisk *disk)
1096{
1097 if (disk)
Tejun Heoed9e1982008-08-25 19:56:05 +09001098 kobject_put(&disk_to_dev(disk)->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099}
1100
1101EXPORT_SYMBOL(put_disk);
1102
1103void set_device_ro(struct block_device *bdev, int flag)
1104{
Tejun Heob7db9952008-08-25 19:56:10 +09001105 bdev->bd_part->policy = flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106}
1107
1108EXPORT_SYMBOL(set_device_ro);
1109
1110void set_disk_ro(struct gendisk *disk, int flag)
1111{
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001112 struct disk_part_iter piter;
1113 struct hd_struct *part;
1114
Tejun Heob7db9952008-08-25 19:56:10 +09001115 disk_part_iter_init(&piter, disk,
1116 DISK_PITER_INCL_EMPTY | DISK_PITER_INCL_PART0);
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001117 while ((part = disk_part_iter_next(&piter)))
1118 part->policy = flag;
1119 disk_part_iter_exit(&piter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120}
1121
1122EXPORT_SYMBOL(set_disk_ro);
1123
1124int bdev_read_only(struct block_device *bdev)
1125{
1126 if (!bdev)
1127 return 0;
Tejun Heob7db9952008-08-25 19:56:10 +09001128 return bdev->bd_part->policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129}
1130
1131EXPORT_SYMBOL(bdev_read_only);
1132
Tejun Heocf771cb2008-09-03 09:01:09 +02001133int invalidate_partition(struct gendisk *disk, int partno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134{
1135 int res = 0;
Tejun Heocf771cb2008-09-03 09:01:09 +02001136 struct block_device *bdev = bdget_disk(disk, partno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 if (bdev) {
Christoph Hellwig2ef41632005-05-05 16:15:59 -07001138 fsync_bdev(bdev);
1139 res = __invalidate_device(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 bdput(bdev);
1141 }
1142 return res;
1143}
1144
1145EXPORT_SYMBOL(invalidate_partition);