blob: 83e7c04015e1f920c813917bff937b4735902480 [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>
Jes Sorensen58383af2006-02-06 14:12:43 -080018#include <linux/mutex.h>
Tejun Heobcce3de2008-08-25 19:47:22 +090019#include <linux/idr.h>
Tejun Heo77ea8872010-12-08 20:57:37 +010020#include <linux/log2.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 Heo77ea8872010-12-08 20:57:37 +010038static void disk_add_events(struct gendisk *disk);
39static void disk_del_events(struct gendisk *disk);
40static void disk_release_events(struct gendisk *disk);
41
Tejun Heoe71bf0d2008-09-03 09:03:02 +020042/**
43 * disk_get_part - get partition
44 * @disk: disk to look partition from
45 * @partno: partition number
46 *
47 * Look for partition @partno from @disk. If found, increment
48 * reference count and return it.
49 *
50 * CONTEXT:
51 * Don't care.
52 *
53 * RETURNS:
54 * Pointer to the found partition on success, NULL if not found.
55 */
56struct hd_struct *disk_get_part(struct gendisk *disk, int partno)
57{
Tejun Heo540eed52008-08-25 19:56:15 +090058 struct hd_struct *part = NULL;
59 struct disk_part_tbl *ptbl;
Tejun Heoe71bf0d2008-09-03 09:03:02 +020060
Tejun Heo540eed52008-08-25 19:56:15 +090061 if (unlikely(partno < 0))
Tejun Heoe71bf0d2008-09-03 09:03:02 +020062 return NULL;
Tejun Heo540eed52008-08-25 19:56:15 +090063
Tejun Heoe71bf0d2008-09-03 09:03:02 +020064 rcu_read_lock();
Tejun Heo540eed52008-08-25 19:56:15 +090065
66 ptbl = rcu_dereference(disk->part_tbl);
67 if (likely(partno < ptbl->len)) {
68 part = rcu_dereference(ptbl->part[partno]);
69 if (part)
70 get_device(part_to_dev(part));
71 }
72
Tejun Heoe71bf0d2008-09-03 09:03:02 +020073 rcu_read_unlock();
74
75 return part;
76}
77EXPORT_SYMBOL_GPL(disk_get_part);
78
79/**
80 * disk_part_iter_init - initialize partition iterator
81 * @piter: iterator to initialize
82 * @disk: disk to iterate over
83 * @flags: DISK_PITER_* flags
84 *
85 * Initialize @piter so that it iterates over partitions of @disk.
86 *
87 * CONTEXT:
88 * Don't care.
89 */
90void disk_part_iter_init(struct disk_part_iter *piter, struct gendisk *disk,
91 unsigned int flags)
92{
Tejun Heo540eed52008-08-25 19:56:15 +090093 struct disk_part_tbl *ptbl;
94
95 rcu_read_lock();
96 ptbl = rcu_dereference(disk->part_tbl);
97
Tejun Heoe71bf0d2008-09-03 09:03:02 +020098 piter->disk = disk;
99 piter->part = NULL;
100
101 if (flags & DISK_PITER_REVERSE)
Tejun Heo540eed52008-08-25 19:56:15 +0900102 piter->idx = ptbl->len - 1;
Tejun Heo71982a42009-04-17 08:34:48 +0200103 else if (flags & (DISK_PITER_INCL_PART0 | DISK_PITER_INCL_EMPTY_PART0))
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200104 piter->idx = 0;
Tejun Heob5d0b9d2008-09-03 09:06:42 +0200105 else
106 piter->idx = 1;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200107
108 piter->flags = flags;
Tejun Heo540eed52008-08-25 19:56:15 +0900109
110 rcu_read_unlock();
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200111}
112EXPORT_SYMBOL_GPL(disk_part_iter_init);
113
114/**
115 * disk_part_iter_next - proceed iterator to the next partition and return it
116 * @piter: iterator of interest
117 *
118 * Proceed @piter to the next partition and return it.
119 *
120 * CONTEXT:
121 * Don't care.
122 */
123struct hd_struct *disk_part_iter_next(struct disk_part_iter *piter)
124{
Tejun Heo540eed52008-08-25 19:56:15 +0900125 struct disk_part_tbl *ptbl;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200126 int inc, end;
127
128 /* put the last partition */
129 disk_put_part(piter->part);
130 piter->part = NULL;
131
Tejun Heo540eed52008-08-25 19:56:15 +0900132 /* get part_tbl */
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200133 rcu_read_lock();
Tejun Heo540eed52008-08-25 19:56:15 +0900134 ptbl = rcu_dereference(piter->disk->part_tbl);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200135
136 /* determine iteration parameters */
137 if (piter->flags & DISK_PITER_REVERSE) {
138 inc = -1;
Tejun Heo71982a42009-04-17 08:34:48 +0200139 if (piter->flags & (DISK_PITER_INCL_PART0 |
140 DISK_PITER_INCL_EMPTY_PART0))
Tejun Heob5d0b9d2008-09-03 09:06:42 +0200141 end = -1;
142 else
143 end = 0;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200144 } else {
145 inc = 1;
Tejun Heo540eed52008-08-25 19:56:15 +0900146 end = ptbl->len;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200147 }
148
149 /* iterate to the next partition */
150 for (; piter->idx != end; piter->idx += inc) {
151 struct hd_struct *part;
152
Tejun Heo540eed52008-08-25 19:56:15 +0900153 part = rcu_dereference(ptbl->part[piter->idx]);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200154 if (!part)
155 continue;
Tejun Heo71982a42009-04-17 08:34:48 +0200156 if (!part->nr_sects &&
157 !(piter->flags & DISK_PITER_INCL_EMPTY) &&
158 !(piter->flags & DISK_PITER_INCL_EMPTY_PART0 &&
159 piter->idx == 0))
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200160 continue;
161
Tejun Heoed9e1982008-08-25 19:56:05 +0900162 get_device(part_to_dev(part));
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200163 piter->part = part;
164 piter->idx += inc;
165 break;
166 }
167
168 rcu_read_unlock();
169
170 return piter->part;
171}
172EXPORT_SYMBOL_GPL(disk_part_iter_next);
173
174/**
175 * disk_part_iter_exit - finish up partition iteration
176 * @piter: iter of interest
177 *
178 * Called when iteration is over. Cleans up @piter.
179 *
180 * CONTEXT:
181 * Don't care.
182 */
183void disk_part_iter_exit(struct disk_part_iter *piter)
184{
185 disk_put_part(piter->part);
186 piter->part = NULL;
187}
188EXPORT_SYMBOL_GPL(disk_part_iter_exit);
189
Jens Axboea6f23652008-10-24 12:52:42 +0200190static inline int sector_in_part(struct hd_struct *part, sector_t sector)
191{
192 return part->start_sect <= sector &&
193 sector < part->start_sect + part->nr_sects;
194}
195
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200196/**
197 * disk_map_sector_rcu - map sector to partition
198 * @disk: gendisk of interest
199 * @sector: sector to map
200 *
201 * Find out which partition @sector maps to on @disk. This is
202 * primarily used for stats accounting.
203 *
204 * CONTEXT:
205 * RCU read locked. The returned partition pointer is valid only
206 * while preemption is disabled.
207 *
208 * RETURNS:
Tejun Heo074a7ac2008-08-25 19:56:14 +0900209 * Found partition on success, part0 is returned if no partition matches
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200210 */
211struct hd_struct *disk_map_sector_rcu(struct gendisk *disk, sector_t sector)
212{
Tejun Heo540eed52008-08-25 19:56:15 +0900213 struct disk_part_tbl *ptbl;
Jens Axboea6f23652008-10-24 12:52:42 +0200214 struct hd_struct *part;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200215 int i;
216
Tejun Heo540eed52008-08-25 19:56:15 +0900217 ptbl = rcu_dereference(disk->part_tbl);
218
Jens Axboea6f23652008-10-24 12:52:42 +0200219 part = rcu_dereference(ptbl->last_lookup);
220 if (part && sector_in_part(part, sector))
221 return part;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200222
Jens Axboea6f23652008-10-24 12:52:42 +0200223 for (i = 1; i < ptbl->len; i++) {
224 part = rcu_dereference(ptbl->part[i]);
225
226 if (part && sector_in_part(part, sector)) {
227 rcu_assign_pointer(ptbl->last_lookup, part);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200228 return part;
Jens Axboea6f23652008-10-24 12:52:42 +0200229 }
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200230 }
Tejun Heo074a7ac2008-08-25 19:56:14 +0900231 return &disk->part0;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200232}
233EXPORT_SYMBOL_GPL(disk_map_sector_rcu);
234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235/*
236 * Can be deleted altogether. Later.
237 *
238 */
239static struct blk_major_name {
240 struct blk_major_name *next;
241 int major;
242 char name[16];
Joe Korty68eef3b2006-03-31 02:30:32 -0800243} *major_names[BLKDEV_MAJOR_HASH_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245/* index in the above - for now: assume no multimajor ranges */
Yang Zhange61eb2e2010-12-17 09:00:18 +0100246static inline int major_to_index(unsigned major)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
Joe Korty68eef3b2006-03-31 02:30:32 -0800248 return major % BLKDEV_MAJOR_HASH_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
Joe Korty68eef3b2006-03-31 02:30:32 -0800251#ifdef CONFIG_PROC_FS
Tejun Heocf771cb2008-09-03 09:01:09 +0200252void blkdev_show(struct seq_file *seqf, off_t offset)
Neil Horman7170be52006-01-14 13:20:38 -0800253{
Joe Korty68eef3b2006-03-31 02:30:32 -0800254 struct blk_major_name *dp;
Neil Horman7170be52006-01-14 13:20:38 -0800255
Joe Korty68eef3b2006-03-31 02:30:32 -0800256 if (offset < BLKDEV_MAJOR_HASH_SIZE) {
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200257 mutex_lock(&block_class_lock);
Joe Korty68eef3b2006-03-31 02:30:32 -0800258 for (dp = major_names[offset]; dp; dp = dp->next)
Tejun Heocf771cb2008-09-03 09:01:09 +0200259 seq_printf(seqf, "%3d %s\n", dp->major, dp->name);
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200260 mutex_unlock(&block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262}
Joe Korty68eef3b2006-03-31 02:30:32 -0800263#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Márton Németh9e8c0bc2009-02-20 08:12:51 +0100265/**
266 * register_blkdev - register a new block device
267 *
268 * @major: the requested major device number [1..255]. If @major=0, try to
269 * allocate any unused major number.
270 * @name: the name of the new block device as a zero terminated string
271 *
272 * The @name must be unique within the system.
273 *
274 * The return value depends on the @major input parameter.
275 * - if a major device number was requested in range [1..255] then the
276 * function returns zero on success, or a negative error code
277 * - if any unused major number was requested with @major=0 parameter
278 * then the return value is the allocated major number in range
279 * [1..255] or a negative error code otherwise
280 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281int register_blkdev(unsigned int major, const char *name)
282{
283 struct blk_major_name **n, *p;
284 int index, ret = 0;
285
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200286 mutex_lock(&block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288 /* temporary */
289 if (major == 0) {
290 for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) {
291 if (major_names[index] == NULL)
292 break;
293 }
294
295 if (index == 0) {
296 printk("register_blkdev: failed to get major for %s\n",
297 name);
298 ret = -EBUSY;
299 goto out;
300 }
301 major = index;
302 ret = major;
303 }
304
305 p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL);
306 if (p == NULL) {
307 ret = -ENOMEM;
308 goto out;
309 }
310
311 p->major = major;
312 strlcpy(p->name, name, sizeof(p->name));
313 p->next = NULL;
314 index = major_to_index(major);
315
316 for (n = &major_names[index]; *n; n = &(*n)->next) {
317 if ((*n)->major == major)
318 break;
319 }
320 if (!*n)
321 *n = p;
322 else
323 ret = -EBUSY;
324
325 if (ret < 0) {
326 printk("register_blkdev: cannot get major %d for %s\n",
327 major, name);
328 kfree(p);
329 }
330out:
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200331 mutex_unlock(&block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 return ret;
333}
334
335EXPORT_SYMBOL(register_blkdev);
336
Akinobu Mitaf4480242007-07-17 04:03:47 -0700337void unregister_blkdev(unsigned int major, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
339 struct blk_major_name **n;
340 struct blk_major_name *p = NULL;
341 int index = major_to_index(major);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200343 mutex_lock(&block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 for (n = &major_names[index]; *n; n = &(*n)->next)
345 if ((*n)->major == major)
346 break;
Akinobu Mita294462a2007-07-17 04:03:45 -0700347 if (!*n || strcmp((*n)->name, name)) {
348 WARN_ON(1);
Akinobu Mita294462a2007-07-17 04:03:45 -0700349 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 p = *n;
351 *n = p->next;
352 }
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200353 mutex_unlock(&block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
356
357EXPORT_SYMBOL(unregister_blkdev);
358
359static struct kobj_map *bdev_map;
360
Tejun Heobcce3de2008-08-25 19:47:22 +0900361/**
Tejun Heo870d6652008-08-25 19:47:25 +0900362 * blk_mangle_minor - scatter minor numbers apart
363 * @minor: minor number to mangle
364 *
365 * Scatter consecutively allocated @minor number apart if MANGLE_DEVT
366 * is enabled. Mangling twice gives the original value.
367 *
368 * RETURNS:
369 * Mangled value.
370 *
371 * CONTEXT:
372 * Don't care.
373 */
374static int blk_mangle_minor(int minor)
375{
376#ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT
377 int i;
378
379 for (i = 0; i < MINORBITS / 2; i++) {
380 int low = minor & (1 << i);
381 int high = minor & (1 << (MINORBITS - 1 - i));
382 int distance = MINORBITS - 1 - 2 * i;
383
384 minor ^= low | high; /* clear both bits */
385 low <<= distance; /* swap the positions */
386 high >>= distance;
387 minor |= low | high; /* and set */
388 }
389#endif
390 return minor;
391}
392
393/**
Tejun Heobcce3de2008-08-25 19:47:22 +0900394 * blk_alloc_devt - allocate a dev_t for a partition
395 * @part: partition to allocate dev_t for
Tejun Heobcce3de2008-08-25 19:47:22 +0900396 * @devt: out parameter for resulting dev_t
397 *
398 * Allocate a dev_t for block device.
399 *
400 * RETURNS:
401 * 0 on success, allocated dev_t is returned in *@devt. -errno on
402 * failure.
403 *
404 * CONTEXT:
405 * Might sleep.
406 */
407int blk_alloc_devt(struct hd_struct *part, dev_t *devt)
408{
409 struct gendisk *disk = part_to_disk(part);
410 int idx, rc;
411
412 /* in consecutive minor range? */
413 if (part->partno < disk->minors) {
414 *devt = MKDEV(disk->major, disk->first_minor + part->partno);
415 return 0;
416 }
417
418 /* allocate ext devt */
419 do {
420 if (!idr_pre_get(&ext_devt_idr, GFP_KERNEL))
421 return -ENOMEM;
422 rc = idr_get_new(&ext_devt_idr, part, &idx);
423 } while (rc == -EAGAIN);
424
425 if (rc)
426 return rc;
427
428 if (idx > MAX_EXT_DEVT) {
429 idr_remove(&ext_devt_idr, idx);
430 return -EBUSY;
431 }
432
Tejun Heo870d6652008-08-25 19:47:25 +0900433 *devt = MKDEV(BLOCK_EXT_MAJOR, blk_mangle_minor(idx));
Tejun Heobcce3de2008-08-25 19:47:22 +0900434 return 0;
435}
436
437/**
438 * blk_free_devt - free a dev_t
439 * @devt: dev_t to free
440 *
441 * Free @devt which was allocated using blk_alloc_devt().
442 *
443 * CONTEXT:
444 * Might sleep.
445 */
446void blk_free_devt(dev_t devt)
447{
448 might_sleep();
449
450 if (devt == MKDEV(0, 0))
451 return;
452
453 if (MAJOR(devt) == BLOCK_EXT_MAJOR) {
454 mutex_lock(&ext_devt_mutex);
Tejun Heo870d6652008-08-25 19:47:25 +0900455 idr_remove(&ext_devt_idr, blk_mangle_minor(MINOR(devt)));
Tejun Heobcce3de2008-08-25 19:47:22 +0900456 mutex_unlock(&ext_devt_mutex);
457 }
458}
459
Tejun Heo1f014292008-08-25 19:47:23 +0900460static char *bdevt_str(dev_t devt, char *buf)
461{
462 if (MAJOR(devt) <= 0xff && MINOR(devt) <= 0xff) {
463 char tbuf[BDEVT_SIZE];
464 snprintf(tbuf, BDEVT_SIZE, "%02x%02x", MAJOR(devt), MINOR(devt));
465 snprintf(buf, BDEVT_SIZE, "%-9s", tbuf);
466 } else
467 snprintf(buf, BDEVT_SIZE, "%03x:%05x", MAJOR(devt), MINOR(devt));
468
469 return buf;
470}
471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472/*
473 * Register device numbers dev..(dev+range-1)
474 * range must be nonzero
475 * The hash chain is sorted on range, so that subranges can override.
476 */
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200477void blk_register_region(dev_t devt, unsigned long range, struct module *module,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 struct kobject *(*probe)(dev_t, int *, void *),
479 int (*lock)(dev_t, void *), void *data)
480{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200481 kobj_map(bdev_map, devt, range, module, probe, lock, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482}
483
484EXPORT_SYMBOL(blk_register_region);
485
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200486void blk_unregister_region(dev_t devt, unsigned long range)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200488 kobj_unmap(bdev_map, devt, range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
490
491EXPORT_SYMBOL(blk_unregister_region);
492
Tejun Heocf771cb2008-09-03 09:01:09 +0200493static struct kobject *exact_match(dev_t devt, int *partno, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
495 struct gendisk *p = data;
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200496
Tejun Heoed9e1982008-08-25 19:56:05 +0900497 return &disk_to_dev(p)->kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498}
499
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200500static int exact_lock(dev_t devt, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
502 struct gendisk *p = data;
503
504 if (!get_disk(p))
505 return -1;
506 return 0;
507}
508
Al Viro4752bc32011-09-16 00:21:54 -0400509static void register_disk(struct gendisk *disk)
Tejun Heod2bf1b62010-12-08 20:57:36 +0100510{
511 struct device *ddev = disk_to_dev(disk);
512 struct block_device *bdev;
513 struct disk_part_iter piter;
514 struct hd_struct *part;
515 int err;
516
517 ddev->parent = disk->driverfs_dev;
518
519 dev_set_name(ddev, disk->disk_name);
520
521 /* delay uevents, until we scanned partition table */
522 dev_set_uevent_suppress(ddev, 1);
523
524 if (device_add(ddev))
525 return;
526 if (!sysfs_deprecated) {
527 err = sysfs_create_link(block_depr, &ddev->kobj,
528 kobject_name(&ddev->kobj));
529 if (err) {
530 device_del(ddev);
531 return;
532 }
533 }
534 disk->part0.holder_dir = kobject_create_and_add("holders", &ddev->kobj);
535 disk->slave_dir = kobject_create_and_add("slaves", &ddev->kobj);
536
537 /* No minors to use for partitions */
Tejun Heod27769e2011-08-23 20:01:04 +0200538 if (!disk_part_scan_enabled(disk))
Tejun Heod2bf1b62010-12-08 20:57:36 +0100539 goto exit;
540
541 /* No such device (e.g., media were just removed) */
542 if (!get_capacity(disk))
543 goto exit;
544
545 bdev = bdget_disk(disk, 0);
546 if (!bdev)
547 goto exit;
548
549 bdev->bd_invalidated = 1;
550 err = blkdev_get(bdev, FMODE_READ, NULL);
551 if (err < 0)
552 goto exit;
553 blkdev_put(bdev, FMODE_READ);
554
555exit:
556 /* announce disk after possible partitions are created */
557 dev_set_uevent_suppress(ddev, 0);
558 kobject_uevent(&ddev->kobj, KOBJ_ADD);
559
560 /* announce possible partitions */
561 disk_part_iter_init(&piter, disk, 0);
562 while ((part = disk_part_iter_next(&piter)))
563 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_ADD);
564 disk_part_iter_exit(&piter);
565}
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567/**
568 * add_disk - add partitioning information to kernel list
569 * @disk: per-device partitioning information
570 *
571 * This function registers the partitioning information in @disk
572 * with the kernel.
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900573 *
574 * FIXME: error handling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 */
576void add_disk(struct gendisk *disk)
577{
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700578 struct backing_dev_info *bdi;
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900579 dev_t devt;
Greg Kroah-Hartman6ffeea72008-05-22 17:21:08 -0400580 int retval;
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700581
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900582 /* minors == 0 indicates to use ext devt from part0 and should
583 * be accompanied with EXT_DEVT flag. Make sure all
584 * parameters make sense.
585 */
586 WARN_ON(disk->minors && !(disk->major || disk->first_minor));
587 WARN_ON(!disk->minors && !(disk->flags & GENHD_FL_EXT_DEVT));
588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 disk->flags |= GENHD_FL_UP;
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900590
591 retval = blk_alloc_devt(&disk->part0, &devt);
592 if (retval) {
593 WARN_ON(1);
594 return;
595 }
596 disk_to_dev(disk)->devt = devt;
597
598 /* ->major and ->first_minor aren't supposed to be
599 * dereferenced from here on, but set them just in case.
600 */
601 disk->major = MAJOR(devt);
602 disk->first_minor = MINOR(devt);
603
Wanlong Gao9f5e4862011-06-13 10:45:43 +0200604 /* Register BDI before referencing it from bdev */
Signed-off-by: Jan Kara01ea5062010-09-16 20:36:36 +0200605 bdi = &disk->queue->backing_dev_info;
606 bdi_register_dev(bdi, disk_devt(disk));
607
Tejun Heof331c022008-09-03 09:01:48 +0200608 blk_register_region(disk_devt(disk), disk->minors, NULL,
609 exact_match, exact_lock, disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 register_disk(disk);
611 blk_register_queue(disk);
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700612
Tejun Heo523e1d32011-10-19 14:31:07 +0200613 /*
614 * Take an extra ref on queue which will be put on disk_release()
615 * so that it sticks around as long as @disk is there.
616 */
617 WARN_ON_ONCE(blk_get_queue(disk->queue));
618
Tejun Heoed9e1982008-08-25 19:56:05 +0900619 retval = sysfs_create_link(&disk_to_dev(disk)->kobj, &bdi->dev->kobj,
620 "bdi");
Greg Kroah-Hartman6ffeea72008-05-22 17:21:08 -0400621 WARN_ON(retval);
Tejun Heo77ea8872010-12-08 20:57:37 +0100622
623 disk_add_events(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625EXPORT_SYMBOL(add_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Tejun Heod2bf1b62010-12-08 20:57:36 +0100627void del_gendisk(struct gendisk *disk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
Tejun Heod2bf1b62010-12-08 20:57:36 +0100629 struct disk_part_iter piter;
630 struct hd_struct *part;
631
Tejun Heo77ea8872010-12-08 20:57:37 +0100632 disk_del_events(disk);
633
Tejun Heod2bf1b62010-12-08 20:57:36 +0100634 /* invalidate stuff */
635 disk_part_iter_init(&piter, disk,
636 DISK_PITER_INCL_EMPTY | DISK_PITER_REVERSE);
637 while ((part = disk_part_iter_next(&piter))) {
638 invalidate_partition(disk, part->partno);
639 delete_partition(disk, part->partno);
640 }
641 disk_part_iter_exit(&piter);
642
643 invalidate_partition(disk, 0);
644 blk_free_devt(disk_to_dev(disk)->devt);
645 set_capacity(disk, 0);
646 disk->flags &= ~GENHD_FL_UP;
647
Tejun Heoed9e1982008-08-25 19:56:05 +0900648 sysfs_remove_link(&disk_to_dev(disk)->kobj, "bdi");
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700649 bdi_unregister(&disk->queue->backing_dev_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 blk_unregister_queue(disk);
Tejun Heof331c022008-09-03 09:01:48 +0200651 blk_unregister_region(disk_devt(disk), disk->minors);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100652
653 part_stat_set_all(&disk->part0, 0);
654 disk->part0.stamp = 0;
655
656 kobject_put(disk->part0.holder_dir);
657 kobject_put(disk->slave_dir);
658 disk->driverfs_dev = NULL;
659 if (!sysfs_deprecated)
660 sysfs_remove_link(block_depr, dev_name(disk_to_dev(disk)));
661 device_del(disk_to_dev(disk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662}
Tejun Heod2bf1b62010-12-08 20:57:36 +0100663EXPORT_SYMBOL(del_gendisk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665/**
666 * get_gendisk - get partitioning information for a given device
Randy Dunlap710027a2008-08-19 20:13:11 +0200667 * @devt: device to get partitioning information for
Randy Dunlap496aa8a2008-10-16 07:46:23 +0200668 * @partno: returned partition index
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 *
670 * This function gets the structure containing partitioning
Randy Dunlap710027a2008-08-19 20:13:11 +0200671 * information for the given device @devt.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 */
Tejun Heocf771cb2008-09-03 09:01:09 +0200673struct gendisk *get_gendisk(dev_t devt, int *partno)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
Tejun Heobcce3de2008-08-25 19:47:22 +0900675 struct gendisk *disk = NULL;
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200676
Tejun Heobcce3de2008-08-25 19:47:22 +0900677 if (MAJOR(devt) != BLOCK_EXT_MAJOR) {
678 struct kobject *kobj;
679
680 kobj = kobj_lookup(bdev_map, devt, partno);
681 if (kobj)
682 disk = dev_to_disk(kobj_to_dev(kobj));
683 } else {
684 struct hd_struct *part;
685
686 mutex_lock(&ext_devt_mutex);
Tejun Heo870d6652008-08-25 19:47:25 +0900687 part = idr_find(&ext_devt_idr, blk_mangle_minor(MINOR(devt)));
Tejun Heobcce3de2008-08-25 19:47:22 +0900688 if (part && get_disk(part_to_disk(part))) {
689 *partno = part->partno;
690 disk = part_to_disk(part);
691 }
692 mutex_unlock(&ext_devt_mutex);
693 }
694
695 return disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696}
Divyesh Shahb6ac23af2010-04-15 08:54:59 +0200697EXPORT_SYMBOL(get_gendisk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Tejun Heof331c022008-09-03 09:01:48 +0200699/**
700 * bdget_disk - do bdget() by gendisk and partition number
701 * @disk: gendisk of interest
702 * @partno: partition number
703 *
704 * Find partition @partno from @disk, do bdget() on it.
705 *
706 * CONTEXT:
707 * Don't care.
708 *
709 * RETURNS:
710 * Resulting block_device on success, NULL on failure.
711 */
Harvey Harrisonaeb3d3a2008-08-28 09:27:42 +0200712struct block_device *bdget_disk(struct gendisk *disk, int partno)
Tejun Heof331c022008-09-03 09:01:48 +0200713{
Tejun Heo548b10e2008-08-29 09:01:47 +0200714 struct hd_struct *part;
715 struct block_device *bdev = NULL;
Tejun Heof331c022008-09-03 09:01:48 +0200716
Tejun Heo548b10e2008-08-29 09:01:47 +0200717 part = disk_get_part(disk, partno);
Tejun Heo2bbedcb2008-08-29 11:41:51 +0200718 if (part)
Tejun Heo548b10e2008-08-29 09:01:47 +0200719 bdev = bdget(part_devt(part));
720 disk_put_part(part);
Tejun Heof331c022008-09-03 09:01:48 +0200721
Tejun Heo548b10e2008-08-29 09:01:47 +0200722 return bdev;
Tejun Heof331c022008-09-03 09:01:48 +0200723}
724EXPORT_SYMBOL(bdget_disk);
725
Dave Gilbertdd2a3452007-05-09 02:33:24 -0700726/*
727 * print a full list of all partitions - intended for places where the root
728 * filesystem can't be mounted and thus to give the victim some idea of what
729 * went wrong
730 */
731void __init printk_all_partitions(void)
732{
Tejun Heodef4e382008-09-03 08:57:12 +0200733 struct class_dev_iter iter;
734 struct device *dev;
735
736 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
737 while ((dev = class_dev_iter_next(&iter))) {
738 struct gendisk *disk = dev_to_disk(dev);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200739 struct disk_part_iter piter;
740 struct hd_struct *part;
Tejun Heo1f014292008-08-25 19:47:23 +0900741 char name_buf[BDEVNAME_SIZE];
742 char devt_buf[BDEVT_SIZE];
Will Drewryb5af9212010-08-31 15:47:07 -0500743 u8 uuid[PARTITION_META_INFO_UUIDLTH * 2 + 1];
Tejun Heodef4e382008-09-03 08:57:12 +0200744
745 /*
746 * Don't show empty devices or things that have been
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300747 * suppressed
Tejun Heodef4e382008-09-03 08:57:12 +0200748 */
749 if (get_capacity(disk) == 0 ||
750 (disk->flags & GENHD_FL_SUPPRESS_PARTITION_INFO))
751 continue;
752
753 /*
754 * Note, unlike /proc/partitions, I am showing the
755 * numbers in hex - the same format as the root=
756 * option takes.
757 */
Tejun Heo074a7ac2008-08-25 19:56:14 +0900758 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
759 while ((part = disk_part_iter_next(&piter))) {
760 bool is_part0 = part == &disk->part0;
Tejun Heodef4e382008-09-03 08:57:12 +0200761
Will Drewryb5af9212010-08-31 15:47:07 -0500762 uuid[0] = 0;
763 if (part->info)
764 part_unpack_uuid(part->info->uuid, uuid);
765
766 printk("%s%s %10llu %s %s", is_part0 ? "" : " ",
Tejun Heo1f014292008-08-25 19:47:23 +0900767 bdevt_str(part_devt(part), devt_buf),
Tejun Heof331c022008-09-03 09:01:48 +0200768 (unsigned long long)part->nr_sects >> 1,
Will Drewryb5af9212010-08-31 15:47:07 -0500769 disk_name(disk, part->partno, name_buf), uuid);
Tejun Heo074a7ac2008-08-25 19:56:14 +0900770 if (is_part0) {
771 if (disk->driverfs_dev != NULL &&
772 disk->driverfs_dev->driver != NULL)
773 printk(" driver: %s\n",
774 disk->driverfs_dev->driver->name);
775 else
776 printk(" (driver?)\n");
777 } else
778 printk("\n");
779 }
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200780 disk_part_iter_exit(&piter);
Tejun Heodef4e382008-09-03 08:57:12 +0200781 }
782 class_dev_iter_exit(&iter);
Dave Gilbertdd2a3452007-05-09 02:33:24 -0700783}
784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785#ifdef CONFIG_PROC_FS
786/* iterator */
Tejun Heodef4e382008-09-03 08:57:12 +0200787static void *disk_seqf_start(struct seq_file *seqf, loff_t *pos)
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -0400788{
Tejun Heodef4e382008-09-03 08:57:12 +0200789 loff_t skip = *pos;
790 struct class_dev_iter *iter;
791 struct device *dev;
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -0400792
Harvey Harrisonaeb3d3a2008-08-28 09:27:42 +0200793 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
Tejun Heodef4e382008-09-03 08:57:12 +0200794 if (!iter)
795 return ERR_PTR(-ENOMEM);
796
797 seqf->private = iter;
798 class_dev_iter_init(iter, &block_class, NULL, &disk_type);
799 do {
800 dev = class_dev_iter_next(iter);
801 if (!dev)
802 return NULL;
803 } while (skip--);
804
805 return dev_to_disk(dev);
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -0400806}
807
Tejun Heodef4e382008-09-03 08:57:12 +0200808static void *disk_seqf_next(struct seq_file *seqf, void *v, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200810 struct device *dev;
Greg Kroah-Hartman66c64af2008-05-22 17:21:08 -0400811
Tejun Heodef4e382008-09-03 08:57:12 +0200812 (*pos)++;
813 dev = class_dev_iter_next(seqf->private);
Tejun Heo2ac3cee2008-09-03 08:53:37 +0200814 if (dev)
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -0400815 return dev_to_disk(dev);
Tejun Heo2ac3cee2008-09-03 08:53:37 +0200816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 return NULL;
818}
819
Tejun Heodef4e382008-09-03 08:57:12 +0200820static void disk_seqf_stop(struct seq_file *seqf, void *v)
Greg Kroah-Hartman27f30252008-05-22 17:21:08 -0400821{
Tejun Heodef4e382008-09-03 08:57:12 +0200822 struct class_dev_iter *iter = seqf->private;
Greg Kroah-Hartman27f30252008-05-22 17:21:08 -0400823
Tejun Heodef4e382008-09-03 08:57:12 +0200824 /* stop is called even after start failed :-( */
825 if (iter) {
826 class_dev_iter_exit(iter);
827 kfree(iter);
Kay Sievers5c0ef6d2008-08-16 14:30:30 +0200828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829}
830
Tejun Heodef4e382008-09-03 08:57:12 +0200831static void *show_partition_start(struct seq_file *seqf, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832{
Tejun Heodef4e382008-09-03 08:57:12 +0200833 static void *p;
834
835 p = disk_seqf_start(seqf, pos);
Yang Zhangb9f985b2010-12-17 08:58:36 +0100836 if (!IS_ERR_OR_NULL(p) && !*pos)
Tejun Heodef4e382008-09-03 08:57:12 +0200837 seq_puts(seqf, "major minor #blocks name\n\n");
838 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839}
840
Tejun Heocf771cb2008-09-03 09:01:09 +0200841static int show_partition(struct seq_file *seqf, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
843 struct gendisk *sgp = v;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200844 struct disk_part_iter piter;
845 struct hd_struct *part;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 char buf[BDEVNAME_SIZE];
847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 /* Don't show non-partitionable removeable devices or empty devices */
Tejun Heod27769e2011-08-23 20:01:04 +0200849 if (!get_capacity(sgp) || (!disk_max_parts(sgp) &&
Tejun Heof331c022008-09-03 09:01:48 +0200850 (sgp->flags & GENHD_FL_REMOVABLE)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 return 0;
852 if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)
853 return 0;
854
855 /* show the full disk and all non-0 size partitions of it */
Tejun Heo074a7ac2008-08-25 19:56:14 +0900856 disk_part_iter_init(&piter, sgp, DISK_PITER_INCL_PART0);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200857 while ((part = disk_part_iter_next(&piter)))
Tejun Heo1f014292008-08-25 19:47:23 +0900858 seq_printf(seqf, "%4d %7d %10llu %s\n",
Tejun Heof331c022008-09-03 09:01:48 +0200859 MAJOR(part_devt(part)), MINOR(part_devt(part)),
860 (unsigned long long)part->nr_sects >> 1,
861 disk_name(sgp, part->partno, buf));
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200862 disk_part_iter_exit(&piter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
864 return 0;
865}
866
Alexey Dobriyanf5009752008-10-04 23:53:21 +0400867static const struct seq_operations partitions_op = {
Tejun Heodef4e382008-09-03 08:57:12 +0200868 .start = show_partition_start,
869 .next = disk_seqf_next,
870 .stop = disk_seqf_stop,
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200871 .show = show_partition
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872};
Alexey Dobriyanf5009752008-10-04 23:53:21 +0400873
874static int partitions_open(struct inode *inode, struct file *file)
875{
876 return seq_open(file, &partitions_op);
877}
878
879static const struct file_operations proc_partitions_operations = {
880 .open = partitions_open,
881 .read = seq_read,
882 .llseek = seq_lseek,
883 .release = seq_release,
884};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885#endif
886
887
Tejun Heocf771cb2008-09-03 09:01:09 +0200888static struct kobject *base_probe(dev_t devt, int *partno, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200890 if (request_module("block-major-%d-%d", MAJOR(devt), MINOR(devt)) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 /* Make old-style 2.4 aliases work */
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200892 request_module("block-major-%d", MAJOR(devt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 return NULL;
894}
895
896static int __init genhd_device_init(void)
897{
Dan Williamse105b8b2008-04-21 10:51:07 -0700898 int error;
899
900 block_class.dev_kobj = sysfs_dev_block_kobj;
901 error = class_register(&block_class);
Roland McGrathee27a552008-03-11 17:13:15 -0700902 if (unlikely(error))
903 return error;
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200904 bdev_map = kobj_map_init(base_probe, &block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 blk_dev_init();
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200906
Zhang, Yanmin561ec682008-11-14 08:26:30 +0100907 register_blkdev(BLOCK_EXT_MAJOR, "blkext");
908
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200909 /* create top-level block dir */
Andi Kleene52eec12010-09-08 16:54:17 +0200910 if (!sysfs_deprecated)
911 block_depr = kobject_create_and_add("block", NULL);
Greg Kroah-Hartman830d3cf2007-11-06 10:36:58 -0800912 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913}
914
915subsys_initcall(genhd_device_init);
916
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200917static ssize_t disk_range_show(struct device *dev,
918 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200920 struct gendisk *disk = dev_to_disk(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200922 return sprintf(buf, "%d\n", disk->minors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923}
924
Tejun Heo1f014292008-08-25 19:47:23 +0900925static ssize_t disk_ext_range_show(struct device *dev,
926 struct device_attribute *attr, char *buf)
927{
928 struct gendisk *disk = dev_to_disk(dev);
929
Tejun Heob5d0b9d2008-09-03 09:06:42 +0200930 return sprintf(buf, "%d\n", disk_max_parts(disk));
Tejun Heo1f014292008-08-25 19:47:23 +0900931}
932
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200933static ssize_t disk_removable_show(struct device *dev,
934 struct device_attribute *attr, char *buf)
Kay Sieversa7fd6702005-10-01 14:49:43 +0200935{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200936 struct gendisk *disk = dev_to_disk(dev);
Kay Sieversa7fd6702005-10-01 14:49:43 +0200937
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200938 return sprintf(buf, "%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200940}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Kay Sievers1c9ce522008-06-13 09:41:00 +0200942static ssize_t disk_ro_show(struct device *dev,
943 struct device_attribute *attr, char *buf)
944{
945 struct gendisk *disk = dev_to_disk(dev);
946
Tejun Heob7db9952008-08-25 19:56:10 +0900947 return sprintf(buf, "%d\n", get_disk_ro(disk) ? 1 : 0);
Kay Sievers1c9ce522008-06-13 09:41:00 +0200948}
949
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200950static ssize_t disk_capability_show(struct device *dev,
951 struct device_attribute *attr, char *buf)
Kristen Carlson Accardi86ce18d2007-05-23 13:57:38 -0700952{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200953 struct gendisk *disk = dev_to_disk(dev);
954
955 return sprintf(buf, "%x\n", disk->flags);
Kristen Carlson Accardi86ce18d2007-05-23 13:57:38 -0700956}
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200957
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400958static ssize_t disk_alignment_offset_show(struct device *dev,
959 struct device_attribute *attr,
960 char *buf)
961{
962 struct gendisk *disk = dev_to_disk(dev);
963
964 return sprintf(buf, "%d\n", queue_alignment_offset(disk->queue));
965}
966
Martin K. Petersen86b37282009-11-10 11:50:21 +0100967static ssize_t disk_discard_alignment_show(struct device *dev,
968 struct device_attribute *attr,
969 char *buf)
970{
971 struct gendisk *disk = dev_to_disk(dev);
972
Martin K. Petersendd3d1452010-01-11 03:21:48 -0500973 return sprintf(buf, "%d\n", queue_discard_alignment(disk->queue));
Martin K. Petersen86b37282009-11-10 11:50:21 +0100974}
975
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200976static DEVICE_ATTR(range, S_IRUGO, disk_range_show, NULL);
Tejun Heo1f014292008-08-25 19:47:23 +0900977static DEVICE_ATTR(ext_range, S_IRUGO, disk_ext_range_show, NULL);
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200978static DEVICE_ATTR(removable, S_IRUGO, disk_removable_show, NULL);
Kay Sievers1c9ce522008-06-13 09:41:00 +0200979static DEVICE_ATTR(ro, S_IRUGO, disk_ro_show, NULL);
Tejun Heoe5610522008-08-25 19:56:09 +0900980static DEVICE_ATTR(size, S_IRUGO, part_size_show, NULL);
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400981static DEVICE_ATTR(alignment_offset, S_IRUGO, disk_alignment_offset_show, NULL);
Martin K. Petersen86b37282009-11-10 11:50:21 +0100982static DEVICE_ATTR(discard_alignment, S_IRUGO, disk_discard_alignment_show,
983 NULL);
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200984static DEVICE_ATTR(capability, S_IRUGO, disk_capability_show, NULL);
Tejun Heo074a7ac2008-08-25 19:56:14 +0900985static DEVICE_ATTR(stat, S_IRUGO, part_stat_show, NULL);
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200986static DEVICE_ATTR(inflight, S_IRUGO, part_inflight_show, NULL);
Akinobu Mitac17bb492006-12-08 02:39:46 -0800987#ifdef CONFIG_FAIL_MAKE_REQUEST
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200988static struct device_attribute dev_attr_fail =
Tejun Heoeddb2e22008-08-25 19:56:13 +0900989 __ATTR(make-it-fail, S_IRUGO|S_IWUSR, part_fail_show, part_fail_store);
Akinobu Mitac17bb492006-12-08 02:39:46 -0800990#endif
Jens Axboe581d4e22008-09-14 05:56:33 -0700991#ifdef CONFIG_FAIL_IO_TIMEOUT
992static struct device_attribute dev_attr_fail_timeout =
993 __ATTR(io-timeout-fail, S_IRUGO|S_IWUSR, part_timeout_show,
994 part_timeout_store);
995#endif
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200996
997static struct attribute *disk_attrs[] = {
998 &dev_attr_range.attr,
Tejun Heo1f014292008-08-25 19:47:23 +0900999 &dev_attr_ext_range.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001000 &dev_attr_removable.attr,
Kay Sievers1c9ce522008-06-13 09:41:00 +02001001 &dev_attr_ro.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001002 &dev_attr_size.attr,
Martin K. Petersenc72758f2009-05-22 17:17:53 -04001003 &dev_attr_alignment_offset.attr,
Martin K. Petersen86b37282009-11-10 11:50:21 +01001004 &dev_attr_discard_alignment.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001005 &dev_attr_capability.attr,
1006 &dev_attr_stat.attr,
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001007 &dev_attr_inflight.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001008#ifdef CONFIG_FAIL_MAKE_REQUEST
1009 &dev_attr_fail.attr,
1010#endif
Jens Axboe581d4e22008-09-14 05:56:33 -07001011#ifdef CONFIG_FAIL_IO_TIMEOUT
1012 &dev_attr_fail_timeout.attr,
1013#endif
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001014 NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015};
1016
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001017static struct attribute_group disk_attr_group = {
1018 .attrs = disk_attrs,
1019};
1020
David Brownella4dbd672009-06-24 10:06:31 -07001021static const struct attribute_group *disk_attr_groups[] = {
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001022 &disk_attr_group,
1023 NULL
1024};
1025
Tejun Heo540eed52008-08-25 19:56:15 +09001026/**
1027 * disk_replace_part_tbl - replace disk->part_tbl in RCU-safe way
1028 * @disk: disk to replace part_tbl for
1029 * @new_ptbl: new part_tbl to install
1030 *
1031 * Replace disk->part_tbl with @new_ptbl in RCU-safe way. The
1032 * original ptbl is freed using RCU callback.
1033 *
1034 * LOCKING:
1035 * Matching bd_mutx locked.
1036 */
1037static void disk_replace_part_tbl(struct gendisk *disk,
1038 struct disk_part_tbl *new_ptbl)
1039{
1040 struct disk_part_tbl *old_ptbl = disk->part_tbl;
1041
1042 rcu_assign_pointer(disk->part_tbl, new_ptbl);
Jens Axboea6f23652008-10-24 12:52:42 +02001043
1044 if (old_ptbl) {
1045 rcu_assign_pointer(old_ptbl->last_lookup, NULL);
Lai Jiangshan57bdfbf2011-03-18 11:42:58 +08001046 kfree_rcu(old_ptbl, rcu_head);
Jens Axboea6f23652008-10-24 12:52:42 +02001047 }
Tejun Heo540eed52008-08-25 19:56:15 +09001048}
1049
1050/**
1051 * disk_expand_part_tbl - expand disk->part_tbl
1052 * @disk: disk to expand part_tbl for
1053 * @partno: expand such that this partno can fit in
1054 *
1055 * Expand disk->part_tbl such that @partno can fit in. disk->part_tbl
1056 * uses RCU to allow unlocked dereferencing for stats and other stuff.
1057 *
1058 * LOCKING:
1059 * Matching bd_mutex locked, might sleep.
1060 *
1061 * RETURNS:
1062 * 0 on success, -errno on failure.
1063 */
1064int disk_expand_part_tbl(struct gendisk *disk, int partno)
1065{
1066 struct disk_part_tbl *old_ptbl = disk->part_tbl;
1067 struct disk_part_tbl *new_ptbl;
1068 int len = old_ptbl ? old_ptbl->len : 0;
1069 int target = partno + 1;
1070 size_t size;
1071 int i;
1072
1073 /* disk_max_parts() is zero during initialization, ignore if so */
1074 if (disk_max_parts(disk) && target > disk_max_parts(disk))
1075 return -EINVAL;
1076
1077 if (target <= len)
1078 return 0;
1079
1080 size = sizeof(*new_ptbl) + target * sizeof(new_ptbl->part[0]);
1081 new_ptbl = kzalloc_node(size, GFP_KERNEL, disk->node_id);
1082 if (!new_ptbl)
1083 return -ENOMEM;
1084
Tejun Heo540eed52008-08-25 19:56:15 +09001085 new_ptbl->len = target;
1086
1087 for (i = 0; i < len; i++)
1088 rcu_assign_pointer(new_ptbl->part[i], old_ptbl->part[i]);
1089
1090 disk_replace_part_tbl(disk, new_ptbl);
1091 return 0;
1092}
1093
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001094static void disk_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001096 struct gendisk *disk = dev_to_disk(dev);
1097
Tejun Heo77ea8872010-12-08 20:57:37 +01001098 disk_release_events(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 kfree(disk->random);
Tejun Heo540eed52008-08-25 19:56:15 +09001100 disk_replace_part_tbl(disk, NULL);
Tejun Heo074a7ac2008-08-25 19:56:14 +09001101 free_part_stats(&disk->part0);
Will Drewry6d1d8052010-08-31 15:47:05 -05001102 free_part_info(&disk->part0);
Tejun Heo523e1d32011-10-19 14:31:07 +02001103 if (disk->queue)
1104 blk_put_queue(disk->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 kfree(disk);
1106}
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001107struct class block_class = {
1108 .name = "block",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109};
1110
Al Viro2c9ede52011-07-23 20:24:48 -04001111static char *block_devnode(struct device *dev, umode_t *mode)
Kay Sieversb03f38b2009-04-30 15:23:42 +02001112{
1113 struct gendisk *disk = dev_to_disk(dev);
1114
Kay Sieverse454cea2009-09-18 23:01:12 +02001115 if (disk->devnode)
1116 return disk->devnode(disk, mode);
Kay Sieversb03f38b2009-04-30 15:23:42 +02001117 return NULL;
1118}
1119
Adrian Bunk1826ead2008-03-04 11:23:46 +01001120static struct device_type disk_type = {
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001121 .name = "disk",
1122 .groups = disk_attr_groups,
1123 .release = disk_release,
Kay Sieverse454cea2009-09-18 23:01:12 +02001124 .devnode = block_devnode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125};
1126
Randy Dunlapa6e2ba82008-05-23 09:44:11 -07001127#ifdef CONFIG_PROC_FS
Tejun Heocf771cb2008-09-03 09:01:09 +02001128/*
1129 * aggregate disk stat collector. Uses the same stats that the sysfs
1130 * entries do, above, but makes them available through one seq_file.
1131 *
1132 * The output looks suspiciously like /proc/partitions with a bunch of
1133 * extra fields.
1134 */
1135static int diskstats_show(struct seq_file *seqf, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136{
1137 struct gendisk *gp = v;
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001138 struct disk_part_iter piter;
1139 struct hd_struct *hd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 char buf[BDEVNAME_SIZE];
Tejun Heoc9959052008-08-25 19:47:21 +09001141 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
1143 /*
Tejun Heoed9e1982008-08-25 19:56:05 +09001144 if (&disk_to_dev(gp)->kobj.entry == block_class.devices.next)
Tejun Heocf771cb2008-09-03 09:01:09 +02001145 seq_puts(seqf, "major minor name"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 " rio rmerge rsect ruse wio wmerge "
1147 "wsect wuse running use aveq"
1148 "\n\n");
1149 */
Wanlong Gao9f5e4862011-06-13 10:45:43 +02001150
Tejun Heo71982a42009-04-17 08:34:48 +02001151 disk_part_iter_init(&piter, gp, DISK_PITER_INCL_EMPTY_PART0);
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001152 while ((hd = disk_part_iter_next(&piter))) {
Tejun Heo074a7ac2008-08-25 19:56:14 +09001153 cpu = part_stat_lock();
Tejun Heoc9959052008-08-25 19:47:21 +09001154 part_round_stats(cpu, hd);
Tejun Heo074a7ac2008-08-25 19:56:14 +09001155 part_stat_unlock();
Herbert Poetzlf95fe9c2011-08-02 12:43:50 +02001156 seq_printf(seqf, "%4d %7d %s %lu %lu %lu "
1157 "%u %lu %lu %lu %u %u %u %u\n",
Tejun Heof331c022008-09-03 09:01:48 +02001158 MAJOR(part_devt(hd)), MINOR(part_devt(hd)),
1159 disk_name(gp, hd->partno, buf),
Liu Yuan53f22952011-03-02 11:00:15 -05001160 part_stat_read(hd, ios[READ]),
1161 part_stat_read(hd, merges[READ]),
Herbert Poetzlf95fe9c2011-08-02 12:43:50 +02001162 part_stat_read(hd, sectors[READ]),
Liu Yuan53f22952011-03-02 11:00:15 -05001163 jiffies_to_msecs(part_stat_read(hd, ticks[READ])),
1164 part_stat_read(hd, ios[WRITE]),
1165 part_stat_read(hd, merges[WRITE]),
Herbert Poetzlf95fe9c2011-08-02 12:43:50 +02001166 part_stat_read(hd, sectors[WRITE]),
Liu Yuan53f22952011-03-02 11:00:15 -05001167 jiffies_to_msecs(part_stat_read(hd, ticks[WRITE])),
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001168 part_in_flight(hd),
Jerome Marchand28f39d52008-02-08 11:04:56 +01001169 jiffies_to_msecs(part_stat_read(hd, io_ticks)),
1170 jiffies_to_msecs(part_stat_read(hd, time_in_queue))
1171 );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 }
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001173 disk_part_iter_exit(&piter);
Wanlong Gao9f5e4862011-06-13 10:45:43 +02001174
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 return 0;
1176}
1177
Alexey Dobriyan31d85ab2008-10-06 12:55:38 +04001178static const struct seq_operations diskstats_op = {
Tejun Heodef4e382008-09-03 08:57:12 +02001179 .start = disk_seqf_start,
1180 .next = disk_seqf_next,
1181 .stop = disk_seqf_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 .show = diskstats_show
1183};
Alexey Dobriyanf5009752008-10-04 23:53:21 +04001184
Alexey Dobriyan31d85ab2008-10-06 12:55:38 +04001185static int diskstats_open(struct inode *inode, struct file *file)
1186{
1187 return seq_open(file, &diskstats_op);
1188}
1189
1190static const struct file_operations proc_diskstats_operations = {
1191 .open = diskstats_open,
1192 .read = seq_read,
1193 .llseek = seq_lseek,
1194 .release = seq_release,
1195};
1196
Alexey Dobriyanf5009752008-10-04 23:53:21 +04001197static int __init proc_genhd_init(void)
1198{
Alexey Dobriyan31d85ab2008-10-06 12:55:38 +04001199 proc_create("diskstats", 0, NULL, &proc_diskstats_operations);
Alexey Dobriyanf5009752008-10-04 23:53:21 +04001200 proc_create("partitions", 0, NULL, &proc_partitions_operations);
1201 return 0;
1202}
1203module_init(proc_genhd_init);
Randy Dunlapa6e2ba82008-05-23 09:44:11 -07001204#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Tejun Heocf771cb2008-09-03 09:01:09 +02001206dev_t blk_lookup_devt(const char *name, int partno)
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001207{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001208 dev_t devt = MKDEV(0, 0);
Tejun Heodef4e382008-09-03 08:57:12 +02001209 struct class_dev_iter iter;
1210 struct device *dev;
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001211
Tejun Heodef4e382008-09-03 08:57:12 +02001212 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
1213 while ((dev = class_dev_iter_next(&iter))) {
1214 struct gendisk *disk = dev_to_disk(dev);
Tejun Heo548b10e2008-08-29 09:01:47 +02001215 struct hd_struct *part;
Tejun Heodef4e382008-09-03 08:57:12 +02001216
Kay Sievers3ada8b72009-01-06 10:44:43 -08001217 if (strcmp(dev_name(dev), name))
Tejun Heof331c022008-09-03 09:01:48 +02001218 continue;
Tejun Heof331c022008-09-03 09:01:48 +02001219
Neil Brown41b8c852009-02-18 10:33:59 +01001220 if (partno < disk->minors) {
1221 /* We need to return the right devno, even
1222 * if the partition doesn't exist yet.
1223 */
1224 devt = MKDEV(MAJOR(dev->devt),
1225 MINOR(dev->devt) + partno);
1226 break;
1227 }
Tejun Heo548b10e2008-08-29 09:01:47 +02001228 part = disk_get_part(disk, partno);
Tejun Heo2bbedcb2008-08-29 11:41:51 +02001229 if (part) {
Tejun Heof331c022008-09-03 09:01:48 +02001230 devt = part_devt(part);
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001231 disk_put_part(part);
Tejun Heo548b10e2008-08-29 09:01:47 +02001232 break;
Tejun Heodef4e382008-09-03 08:57:12 +02001233 }
Tejun Heo548b10e2008-08-29 09:01:47 +02001234 disk_put_part(part);
Kay Sievers5c0ef6d2008-08-16 14:30:30 +02001235 }
Tejun Heodef4e382008-09-03 08:57:12 +02001236 class_dev_iter_exit(&iter);
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001237 return devt;
1238}
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001239EXPORT_SYMBOL(blk_lookup_devt);
1240
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241struct gendisk *alloc_disk(int minors)
1242{
Christoph Lameter19460892005-06-23 00:08:19 -07001243 return alloc_disk_node(minors, -1);
1244}
Tejun Heo689d6fa2008-08-25 19:56:16 +09001245EXPORT_SYMBOL(alloc_disk);
Christoph Lameter19460892005-06-23 00:08:19 -07001246
1247struct gendisk *alloc_disk_node(int minors, int node_id)
1248{
1249 struct gendisk *disk;
1250
Christoph Lameter94f60302007-07-17 04:03:29 -07001251 disk = kmalloc_node(sizeof(struct gendisk),
1252 GFP_KERNEL | __GFP_ZERO, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 if (disk) {
Tejun Heo074a7ac2008-08-25 19:56:14 +09001254 if (!init_part_stats(&disk->part0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 kfree(disk);
1256 return NULL;
1257 }
Cheng Renquanbf91db12008-11-20 08:37:37 +01001258 disk->node_id = node_id;
Tejun Heo540eed52008-08-25 19:56:15 +09001259 if (disk_expand_part_tbl(disk, 0)) {
1260 free_part_stats(&disk->part0);
Tejun Heob5d0b9d2008-09-03 09:06:42 +02001261 kfree(disk);
1262 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 }
Tejun Heo540eed52008-08-25 19:56:15 +09001264 disk->part_tbl->part[0] = &disk->part0;
Jens Axboe6c23a962011-01-07 08:43:37 +01001265
1266 hd_ref_init(&disk->part0);
Tejun Heob5d0b9d2008-09-03 09:06:42 +02001267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 disk->minors = minors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 rand_initialize_disk(disk);
Tejun Heoed9e1982008-08-25 19:56:05 +09001270 disk_to_dev(disk)->class = &block_class;
1271 disk_to_dev(disk)->type = &disk_type;
1272 device_initialize(disk_to_dev(disk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 }
1274 return disk;
1275}
Christoph Lameter19460892005-06-23 00:08:19 -07001276EXPORT_SYMBOL(alloc_disk_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
1278struct kobject *get_disk(struct gendisk *disk)
1279{
1280 struct module *owner;
1281 struct kobject *kobj;
1282
1283 if (!disk->fops)
1284 return NULL;
1285 owner = disk->fops->owner;
1286 if (owner && !try_module_get(owner))
1287 return NULL;
Tejun Heoed9e1982008-08-25 19:56:05 +09001288 kobj = kobject_get(&disk_to_dev(disk)->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 if (kobj == NULL) {
1290 module_put(owner);
1291 return NULL;
1292 }
1293 return kobj;
1294
1295}
1296
1297EXPORT_SYMBOL(get_disk);
1298
1299void put_disk(struct gendisk *disk)
1300{
1301 if (disk)
Tejun Heoed9e1982008-08-25 19:56:05 +09001302 kobject_put(&disk_to_dev(disk)->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303}
1304
1305EXPORT_SYMBOL(put_disk);
1306
Hannes Reineckee3264a42009-07-28 09:13:13 +02001307static void set_disk_ro_uevent(struct gendisk *gd, int ro)
1308{
1309 char event[] = "DISK_RO=1";
1310 char *envp[] = { event, NULL };
1311
1312 if (!ro)
1313 event[8] = '0';
1314 kobject_uevent_env(&disk_to_dev(gd)->kobj, KOBJ_CHANGE, envp);
1315}
1316
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317void set_device_ro(struct block_device *bdev, int flag)
1318{
Tejun Heob7db9952008-08-25 19:56:10 +09001319 bdev->bd_part->policy = flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320}
1321
1322EXPORT_SYMBOL(set_device_ro);
1323
1324void set_disk_ro(struct gendisk *disk, int flag)
1325{
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001326 struct disk_part_iter piter;
1327 struct hd_struct *part;
1328
Hannes Reineckee3264a42009-07-28 09:13:13 +02001329 if (disk->part0.policy != flag) {
1330 set_disk_ro_uevent(disk, flag);
1331 disk->part0.policy = flag;
1332 }
1333
1334 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_EMPTY);
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001335 while ((part = disk_part_iter_next(&piter)))
1336 part->policy = flag;
1337 disk_part_iter_exit(&piter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338}
1339
1340EXPORT_SYMBOL(set_disk_ro);
1341
1342int bdev_read_only(struct block_device *bdev)
1343{
1344 if (!bdev)
1345 return 0;
Tejun Heob7db9952008-08-25 19:56:10 +09001346 return bdev->bd_part->policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347}
1348
1349EXPORT_SYMBOL(bdev_read_only);
1350
Tejun Heocf771cb2008-09-03 09:01:09 +02001351int invalidate_partition(struct gendisk *disk, int partno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352{
1353 int res = 0;
Tejun Heocf771cb2008-09-03 09:01:09 +02001354 struct block_device *bdev = bdget_disk(disk, partno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 if (bdev) {
Christoph Hellwig2ef41632005-05-05 16:15:59 -07001356 fsync_bdev(bdev);
NeilBrown93b270f2011-02-24 17:25:47 +11001357 res = __invalidate_device(bdev, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 bdput(bdev);
1359 }
1360 return res;
1361}
1362
1363EXPORT_SYMBOL(invalidate_partition);
Tejun Heo77ea8872010-12-08 20:57:37 +01001364
1365/*
1366 * Disk events - monitor disk events like media change and eject request.
1367 */
1368struct disk_events {
1369 struct list_head node; /* all disk_event's */
1370 struct gendisk *disk; /* the associated disk */
1371 spinlock_t lock;
1372
Tejun Heofdd514e2011-06-09 20:43:59 +02001373 struct mutex block_mutex; /* protects blocking */
Tejun Heo77ea8872010-12-08 20:57:37 +01001374 int block; /* event blocking depth */
1375 unsigned int pending; /* events already sent out */
1376 unsigned int clearing; /* events being cleared */
1377
1378 long poll_msecs; /* interval, -1 for default */
1379 struct delayed_work dwork;
1380};
1381
1382static const char *disk_events_strs[] = {
1383 [ilog2(DISK_EVENT_MEDIA_CHANGE)] = "media_change",
1384 [ilog2(DISK_EVENT_EJECT_REQUEST)] = "eject_request",
1385};
1386
1387static char *disk_uevents[] = {
1388 [ilog2(DISK_EVENT_MEDIA_CHANGE)] = "DISK_MEDIA_CHANGE=1",
1389 [ilog2(DISK_EVENT_EJECT_REQUEST)] = "DISK_EJECT_REQUEST=1",
1390};
1391
1392/* list of all disk_events */
1393static DEFINE_MUTEX(disk_events_mutex);
1394static LIST_HEAD(disk_events);
1395
1396/* disable in-kernel polling by default */
1397static unsigned long disk_events_dfl_poll_msecs = 0;
1398
1399static unsigned long disk_events_poll_jiffies(struct gendisk *disk)
1400{
1401 struct disk_events *ev = disk->ev;
1402 long intv_msecs = 0;
1403
1404 /*
1405 * If device-specific poll interval is set, always use it. If
1406 * the default is being used, poll iff there are events which
1407 * can't be monitored asynchronously.
1408 */
1409 if (ev->poll_msecs >= 0)
1410 intv_msecs = ev->poll_msecs;
1411 else if (disk->events & ~disk->async_events)
1412 intv_msecs = disk_events_dfl_poll_msecs;
1413
1414 return msecs_to_jiffies(intv_msecs);
1415}
1416
Tejun Heoc3af54a2011-06-09 20:43:55 +02001417/**
1418 * disk_block_events - block and flush disk event checking
1419 * @disk: disk to block events for
1420 *
1421 * On return from this function, it is guaranteed that event checking
1422 * isn't in progress and won't happen until unblocked by
1423 * disk_unblock_events(). Events blocking is counted and the actual
1424 * unblocking happens after the matching number of unblocks are done.
1425 *
1426 * Note that this intentionally does not block event checking from
1427 * disk_clear_events().
1428 *
1429 * CONTEXT:
1430 * Might sleep.
1431 */
1432void disk_block_events(struct gendisk *disk)
Tejun Heo77ea8872010-12-08 20:57:37 +01001433{
1434 struct disk_events *ev = disk->ev;
1435 unsigned long flags;
1436 bool cancel;
1437
Tejun Heoc3af54a2011-06-09 20:43:55 +02001438 if (!ev)
1439 return;
1440
Tejun Heofdd514e2011-06-09 20:43:59 +02001441 /*
1442 * Outer mutex ensures that the first blocker completes canceling
1443 * the event work before further blockers are allowed to finish.
1444 */
1445 mutex_lock(&ev->block_mutex);
1446
Tejun Heo77ea8872010-12-08 20:57:37 +01001447 spin_lock_irqsave(&ev->lock, flags);
1448 cancel = !ev->block++;
1449 spin_unlock_irqrestore(&ev->lock, flags);
1450
Tejun Heoc3af54a2011-06-09 20:43:55 +02001451 if (cancel)
1452 cancel_delayed_work_sync(&disk->ev->dwork);
Tejun Heofdd514e2011-06-09 20:43:59 +02001453
1454 mutex_unlock(&ev->block_mutex);
Tejun Heo77ea8872010-12-08 20:57:37 +01001455}
1456
1457static void __disk_unblock_events(struct gendisk *disk, bool check_now)
1458{
1459 struct disk_events *ev = disk->ev;
1460 unsigned long intv;
1461 unsigned long flags;
1462
1463 spin_lock_irqsave(&ev->lock, flags);
1464
1465 if (WARN_ON_ONCE(ev->block <= 0))
1466 goto out_unlock;
1467
1468 if (--ev->block)
1469 goto out_unlock;
1470
1471 /*
1472 * Not exactly a latency critical operation, set poll timer
1473 * slack to 25% and kick event check.
1474 */
1475 intv = disk_events_poll_jiffies(disk);
1476 set_timer_slack(&ev->dwork.timer, intv / 4);
1477 if (check_now)
1478 queue_delayed_work(system_nrt_wq, &ev->dwork, 0);
1479 else if (intv)
1480 queue_delayed_work(system_nrt_wq, &ev->dwork, intv);
1481out_unlock:
1482 spin_unlock_irqrestore(&ev->lock, flags);
1483}
1484
1485/**
Tejun Heo77ea8872010-12-08 20:57:37 +01001486 * disk_unblock_events - unblock disk event checking
1487 * @disk: disk to unblock events for
1488 *
1489 * Undo disk_block_events(). When the block count reaches zero, it
1490 * starts events polling if configured.
1491 *
1492 * CONTEXT:
1493 * Don't care. Safe to call from irq context.
1494 */
1495void disk_unblock_events(struct gendisk *disk)
1496{
1497 if (disk->ev)
Tejun Heofacc31d2011-03-09 19:54:27 +01001498 __disk_unblock_events(disk, false);
Tejun Heo77ea8872010-12-08 20:57:37 +01001499}
1500
1501/**
Tejun Heo85ef06d2011-07-01 16:17:47 +02001502 * disk_flush_events - schedule immediate event checking and flushing
1503 * @disk: disk to check and flush events for
1504 * @mask: events to flush
Tejun Heo77ea8872010-12-08 20:57:37 +01001505 *
Tejun Heo85ef06d2011-07-01 16:17:47 +02001506 * Schedule immediate event checking on @disk if not blocked. Events in
1507 * @mask are scheduled to be cleared from the driver. Note that this
1508 * doesn't clear the events from @disk->ev.
Tejun Heo77ea8872010-12-08 20:57:37 +01001509 *
1510 * CONTEXT:
Tejun Heo85ef06d2011-07-01 16:17:47 +02001511 * If @mask is non-zero must be called with bdev->bd_mutex held.
Tejun Heo77ea8872010-12-08 20:57:37 +01001512 */
Tejun Heo85ef06d2011-07-01 16:17:47 +02001513void disk_flush_events(struct gendisk *disk, unsigned int mask)
Tejun Heo77ea8872010-12-08 20:57:37 +01001514{
Tejun Heoa9dce2a2011-06-09 20:43:54 +02001515 struct disk_events *ev = disk->ev;
Tejun Heoa9dce2a2011-06-09 20:43:54 +02001516
1517 if (!ev)
1518 return;
1519
Tejun Heo85ef06d2011-07-01 16:17:47 +02001520 spin_lock_irq(&ev->lock);
1521 ev->clearing |= mask;
Tejun Heoa9dce2a2011-06-09 20:43:54 +02001522 if (!ev->block) {
1523 cancel_delayed_work(&ev->dwork);
1524 queue_delayed_work(system_nrt_wq, &ev->dwork, 0);
Tejun Heo77ea8872010-12-08 20:57:37 +01001525 }
Tejun Heo85ef06d2011-07-01 16:17:47 +02001526 spin_unlock_irq(&ev->lock);
Tejun Heo77ea8872010-12-08 20:57:37 +01001527}
Tejun Heo77ea8872010-12-08 20:57:37 +01001528
1529/**
1530 * disk_clear_events - synchronously check, clear and return pending events
1531 * @disk: disk to fetch and clear events from
1532 * @mask: mask of events to be fetched and clearted
1533 *
1534 * Disk events are synchronously checked and pending events in @mask
1535 * are cleared and returned. This ignores the block count.
1536 *
1537 * CONTEXT:
1538 * Might sleep.
1539 */
1540unsigned int disk_clear_events(struct gendisk *disk, unsigned int mask)
1541{
1542 const struct block_device_operations *bdops = disk->fops;
1543 struct disk_events *ev = disk->ev;
1544 unsigned int pending;
1545
1546 if (!ev) {
1547 /* for drivers still using the old ->media_changed method */
1548 if ((mask & DISK_EVENT_MEDIA_CHANGE) &&
1549 bdops->media_changed && bdops->media_changed(disk))
1550 return DISK_EVENT_MEDIA_CHANGE;
1551 return 0;
1552 }
1553
1554 /* tell the workfn about the events being cleared */
1555 spin_lock_irq(&ev->lock);
1556 ev->clearing |= mask;
1557 spin_unlock_irq(&ev->lock);
1558
1559 /* uncondtionally schedule event check and wait for it to finish */
Tejun Heoc3af54a2011-06-09 20:43:55 +02001560 disk_block_events(disk);
Tejun Heo77ea8872010-12-08 20:57:37 +01001561 queue_delayed_work(system_nrt_wq, &ev->dwork, 0);
1562 flush_delayed_work(&ev->dwork);
1563 __disk_unblock_events(disk, false);
1564
1565 /* then, fetch and clear pending events */
1566 spin_lock_irq(&ev->lock);
1567 WARN_ON_ONCE(ev->clearing & mask); /* cleared by workfn */
1568 pending = ev->pending & mask;
1569 ev->pending &= ~mask;
1570 spin_unlock_irq(&ev->lock);
1571
1572 return pending;
1573}
1574
1575static void disk_events_workfn(struct work_struct *work)
1576{
1577 struct delayed_work *dwork = to_delayed_work(work);
1578 struct disk_events *ev = container_of(dwork, struct disk_events, dwork);
1579 struct gendisk *disk = ev->disk;
1580 char *envp[ARRAY_SIZE(disk_uevents) + 1] = { };
1581 unsigned int clearing = ev->clearing;
1582 unsigned int events;
1583 unsigned long intv;
1584 int nr_events = 0, i;
1585
1586 /* check events */
1587 events = disk->fops->check_events(disk, clearing);
1588
1589 /* accumulate pending events and schedule next poll if necessary */
1590 spin_lock_irq(&ev->lock);
1591
1592 events &= ~ev->pending;
1593 ev->pending |= events;
1594 ev->clearing &= ~clearing;
1595
1596 intv = disk_events_poll_jiffies(disk);
1597 if (!ev->block && intv)
1598 queue_delayed_work(system_nrt_wq, &ev->dwork, intv);
1599
1600 spin_unlock_irq(&ev->lock);
1601
Tejun Heo7c88a162011-04-21 19:43:58 +02001602 /*
1603 * Tell userland about new events. Only the events listed in
1604 * @disk->events are reported. Unlisted events are processed the
1605 * same internally but never get reported to userland.
1606 */
Tejun Heo77ea8872010-12-08 20:57:37 +01001607 for (i = 0; i < ARRAY_SIZE(disk_uevents); i++)
Tejun Heo7c88a162011-04-21 19:43:58 +02001608 if (events & disk->events & (1 << i))
Tejun Heo77ea8872010-12-08 20:57:37 +01001609 envp[nr_events++] = disk_uevents[i];
1610
1611 if (nr_events)
1612 kobject_uevent_env(&disk_to_dev(disk)->kobj, KOBJ_CHANGE, envp);
1613}
1614
1615/*
1616 * A disk events enabled device has the following sysfs nodes under
1617 * its /sys/block/X/ directory.
1618 *
1619 * events : list of all supported events
1620 * events_async : list of events which can be detected w/o polling
1621 * events_poll_msecs : polling interval, 0: disable, -1: system default
1622 */
1623static ssize_t __disk_events_show(unsigned int events, char *buf)
1624{
1625 const char *delim = "";
1626 ssize_t pos = 0;
1627 int i;
1628
1629 for (i = 0; i < ARRAY_SIZE(disk_events_strs); i++)
1630 if (events & (1 << i)) {
1631 pos += sprintf(buf + pos, "%s%s",
1632 delim, disk_events_strs[i]);
1633 delim = " ";
1634 }
1635 if (pos)
1636 pos += sprintf(buf + pos, "\n");
1637 return pos;
1638}
1639
1640static ssize_t disk_events_show(struct device *dev,
1641 struct device_attribute *attr, char *buf)
1642{
1643 struct gendisk *disk = dev_to_disk(dev);
1644
1645 return __disk_events_show(disk->events, buf);
1646}
1647
1648static ssize_t disk_events_async_show(struct device *dev,
1649 struct device_attribute *attr, char *buf)
1650{
1651 struct gendisk *disk = dev_to_disk(dev);
1652
1653 return __disk_events_show(disk->async_events, buf);
1654}
1655
1656static ssize_t disk_events_poll_msecs_show(struct device *dev,
1657 struct device_attribute *attr,
1658 char *buf)
1659{
1660 struct gendisk *disk = dev_to_disk(dev);
1661
1662 return sprintf(buf, "%ld\n", disk->ev->poll_msecs);
1663}
1664
1665static ssize_t disk_events_poll_msecs_store(struct device *dev,
1666 struct device_attribute *attr,
1667 const char *buf, size_t count)
1668{
1669 struct gendisk *disk = dev_to_disk(dev);
1670 long intv;
1671
1672 if (!count || !sscanf(buf, "%ld", &intv))
1673 return -EINVAL;
1674
1675 if (intv < 0 && intv != -1)
1676 return -EINVAL;
1677
Tejun Heoc3af54a2011-06-09 20:43:55 +02001678 disk_block_events(disk);
Tejun Heo77ea8872010-12-08 20:57:37 +01001679 disk->ev->poll_msecs = intv;
1680 __disk_unblock_events(disk, true);
1681
1682 return count;
1683}
1684
1685static const DEVICE_ATTR(events, S_IRUGO, disk_events_show, NULL);
1686static const DEVICE_ATTR(events_async, S_IRUGO, disk_events_async_show, NULL);
1687static const DEVICE_ATTR(events_poll_msecs, S_IRUGO|S_IWUSR,
1688 disk_events_poll_msecs_show,
1689 disk_events_poll_msecs_store);
1690
1691static const struct attribute *disk_events_attrs[] = {
1692 &dev_attr_events.attr,
1693 &dev_attr_events_async.attr,
1694 &dev_attr_events_poll_msecs.attr,
1695 NULL,
1696};
1697
1698/*
1699 * The default polling interval can be specified by the kernel
1700 * parameter block.events_dfl_poll_msecs which defaults to 0
1701 * (disable). This can also be modified runtime by writing to
1702 * /sys/module/block/events_dfl_poll_msecs.
1703 */
1704static int disk_events_set_dfl_poll_msecs(const char *val,
1705 const struct kernel_param *kp)
1706{
1707 struct disk_events *ev;
1708 int ret;
1709
1710 ret = param_set_ulong(val, kp);
1711 if (ret < 0)
1712 return ret;
1713
1714 mutex_lock(&disk_events_mutex);
1715
1716 list_for_each_entry(ev, &disk_events, node)
Tejun Heo85ef06d2011-07-01 16:17:47 +02001717 disk_flush_events(ev->disk, 0);
Tejun Heo77ea8872010-12-08 20:57:37 +01001718
1719 mutex_unlock(&disk_events_mutex);
1720
1721 return 0;
1722}
1723
1724static const struct kernel_param_ops disk_events_dfl_poll_msecs_param_ops = {
1725 .set = disk_events_set_dfl_poll_msecs,
1726 .get = param_get_ulong,
1727};
1728
1729#undef MODULE_PARAM_PREFIX
1730#define MODULE_PARAM_PREFIX "block."
1731
1732module_param_cb(events_dfl_poll_msecs, &disk_events_dfl_poll_msecs_param_ops,
1733 &disk_events_dfl_poll_msecs, 0644);
1734
1735/*
1736 * disk_{add|del|release}_events - initialize and destroy disk_events.
1737 */
1738static void disk_add_events(struct gendisk *disk)
1739{
1740 struct disk_events *ev;
1741
Tejun Heo75e3f3e2011-05-26 21:06:50 +02001742 if (!disk->fops->check_events)
Tejun Heo77ea8872010-12-08 20:57:37 +01001743 return;
1744
1745 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
1746 if (!ev) {
1747 pr_warn("%s: failed to initialize events\n", disk->disk_name);
1748 return;
1749 }
1750
1751 if (sysfs_create_files(&disk_to_dev(disk)->kobj,
1752 disk_events_attrs) < 0) {
1753 pr_warn("%s: failed to create sysfs files for events\n",
1754 disk->disk_name);
1755 kfree(ev);
1756 return;
1757 }
1758
1759 disk->ev = ev;
1760
1761 INIT_LIST_HEAD(&ev->node);
1762 ev->disk = disk;
1763 spin_lock_init(&ev->lock);
Tejun Heofdd514e2011-06-09 20:43:59 +02001764 mutex_init(&ev->block_mutex);
Tejun Heo77ea8872010-12-08 20:57:37 +01001765 ev->block = 1;
1766 ev->poll_msecs = -1;
1767 INIT_DELAYED_WORK(&ev->dwork, disk_events_workfn);
1768
1769 mutex_lock(&disk_events_mutex);
1770 list_add_tail(&ev->node, &disk_events);
1771 mutex_unlock(&disk_events_mutex);
1772
1773 /*
1774 * Block count is initialized to 1 and the following initial
1775 * unblock kicks it into action.
1776 */
1777 __disk_unblock_events(disk, true);
1778}
1779
1780static void disk_del_events(struct gendisk *disk)
1781{
1782 if (!disk->ev)
1783 return;
1784
Tejun Heoc3af54a2011-06-09 20:43:55 +02001785 disk_block_events(disk);
Tejun Heo77ea8872010-12-08 20:57:37 +01001786
1787 mutex_lock(&disk_events_mutex);
1788 list_del_init(&disk->ev->node);
1789 mutex_unlock(&disk_events_mutex);
1790
1791 sysfs_remove_files(&disk_to_dev(disk)->kobj, disk_events_attrs);
1792}
1793
1794static void disk_release_events(struct gendisk *disk)
1795{
1796 /* the block count should be 1 from disk_del_events() */
1797 WARN_ON_ONCE(disk->ev && disk->ev->block != 1);
1798 kfree(disk->ev);
1799}