blob: 3993ebf4135fb1adb8e25c9562dadd996d1c6be2 [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
Derek Basehore12c2bdb2012-12-18 12:27:20 -080038static void disk_check_events(struct disk_events *ev,
39 unsigned int *clearing_ptr);
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +010040static void disk_alloc_events(struct gendisk *disk);
Tejun Heo77ea8872010-12-08 20:57:37 +010041static void disk_add_events(struct gendisk *disk);
42static void disk_del_events(struct gendisk *disk);
43static void disk_release_events(struct gendisk *disk);
44
Tejun Heoe71bf0d2008-09-03 09:03:02 +020045/**
46 * disk_get_part - get partition
47 * @disk: disk to look partition from
48 * @partno: partition number
49 *
50 * Look for partition @partno from @disk. If found, increment
51 * reference count and return it.
52 *
53 * CONTEXT:
54 * Don't care.
55 *
56 * RETURNS:
57 * Pointer to the found partition on success, NULL if not found.
58 */
59struct hd_struct *disk_get_part(struct gendisk *disk, int partno)
60{
Tejun Heo540eed52008-08-25 19:56:15 +090061 struct hd_struct *part = NULL;
62 struct disk_part_tbl *ptbl;
Tejun Heoe71bf0d2008-09-03 09:03:02 +020063
Tejun Heo540eed52008-08-25 19:56:15 +090064 if (unlikely(partno < 0))
Tejun Heoe71bf0d2008-09-03 09:03:02 +020065 return NULL;
Tejun Heo540eed52008-08-25 19:56:15 +090066
Tejun Heoe71bf0d2008-09-03 09:03:02 +020067 rcu_read_lock();
Tejun Heo540eed52008-08-25 19:56:15 +090068
69 ptbl = rcu_dereference(disk->part_tbl);
70 if (likely(partno < ptbl->len)) {
71 part = rcu_dereference(ptbl->part[partno]);
72 if (part)
73 get_device(part_to_dev(part));
74 }
75
Tejun Heoe71bf0d2008-09-03 09:03:02 +020076 rcu_read_unlock();
77
78 return part;
79}
80EXPORT_SYMBOL_GPL(disk_get_part);
81
82/**
83 * disk_part_iter_init - initialize partition iterator
84 * @piter: iterator to initialize
85 * @disk: disk to iterate over
86 * @flags: DISK_PITER_* flags
87 *
88 * Initialize @piter so that it iterates over partitions of @disk.
89 *
90 * CONTEXT:
91 * Don't care.
92 */
93void disk_part_iter_init(struct disk_part_iter *piter, struct gendisk *disk,
94 unsigned int flags)
95{
Tejun Heo540eed52008-08-25 19:56:15 +090096 struct disk_part_tbl *ptbl;
97
98 rcu_read_lock();
99 ptbl = rcu_dereference(disk->part_tbl);
100
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200101 piter->disk = disk;
102 piter->part = NULL;
103
104 if (flags & DISK_PITER_REVERSE)
Tejun Heo540eed52008-08-25 19:56:15 +0900105 piter->idx = ptbl->len - 1;
Tejun Heo71982a42009-04-17 08:34:48 +0200106 else if (flags & (DISK_PITER_INCL_PART0 | DISK_PITER_INCL_EMPTY_PART0))
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200107 piter->idx = 0;
Tejun Heob5d0b9d2008-09-03 09:06:42 +0200108 else
109 piter->idx = 1;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200110
111 piter->flags = flags;
Tejun Heo540eed52008-08-25 19:56:15 +0900112
113 rcu_read_unlock();
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200114}
115EXPORT_SYMBOL_GPL(disk_part_iter_init);
116
117/**
118 * disk_part_iter_next - proceed iterator to the next partition and return it
119 * @piter: iterator of interest
120 *
121 * Proceed @piter to the next partition and return it.
122 *
123 * CONTEXT:
124 * Don't care.
125 */
126struct hd_struct *disk_part_iter_next(struct disk_part_iter *piter)
127{
Tejun Heo540eed52008-08-25 19:56:15 +0900128 struct disk_part_tbl *ptbl;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200129 int inc, end;
130
131 /* put the last partition */
132 disk_put_part(piter->part);
133 piter->part = NULL;
134
Tejun Heo540eed52008-08-25 19:56:15 +0900135 /* get part_tbl */
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200136 rcu_read_lock();
Tejun Heo540eed52008-08-25 19:56:15 +0900137 ptbl = rcu_dereference(piter->disk->part_tbl);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200138
139 /* determine iteration parameters */
140 if (piter->flags & DISK_PITER_REVERSE) {
141 inc = -1;
Tejun Heo71982a42009-04-17 08:34:48 +0200142 if (piter->flags & (DISK_PITER_INCL_PART0 |
143 DISK_PITER_INCL_EMPTY_PART0))
Tejun Heob5d0b9d2008-09-03 09:06:42 +0200144 end = -1;
145 else
146 end = 0;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200147 } else {
148 inc = 1;
Tejun Heo540eed52008-08-25 19:56:15 +0900149 end = ptbl->len;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200150 }
151
152 /* iterate to the next partition */
153 for (; piter->idx != end; piter->idx += inc) {
154 struct hd_struct *part;
155
Tejun Heo540eed52008-08-25 19:56:15 +0900156 part = rcu_dereference(ptbl->part[piter->idx]);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200157 if (!part)
158 continue;
Vivek Goyalc83f6bf2012-08-01 12:24:18 +0200159 if (!part_nr_sects_read(part) &&
Tejun Heo71982a42009-04-17 08:34:48 +0200160 !(piter->flags & DISK_PITER_INCL_EMPTY) &&
161 !(piter->flags & DISK_PITER_INCL_EMPTY_PART0 &&
162 piter->idx == 0))
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200163 continue;
164
Tejun Heoed9e1982008-08-25 19:56:05 +0900165 get_device(part_to_dev(part));
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200166 piter->part = part;
167 piter->idx += inc;
168 break;
169 }
170
171 rcu_read_unlock();
172
173 return piter->part;
174}
175EXPORT_SYMBOL_GPL(disk_part_iter_next);
176
177/**
178 * disk_part_iter_exit - finish up partition iteration
179 * @piter: iter of interest
180 *
181 * Called when iteration is over. Cleans up @piter.
182 *
183 * CONTEXT:
184 * Don't care.
185 */
186void disk_part_iter_exit(struct disk_part_iter *piter)
187{
188 disk_put_part(piter->part);
189 piter->part = NULL;
190}
191EXPORT_SYMBOL_GPL(disk_part_iter_exit);
192
Jens Axboea6f23652008-10-24 12:52:42 +0200193static inline int sector_in_part(struct hd_struct *part, sector_t sector)
194{
195 return part->start_sect <= sector &&
Vivek Goyalc83f6bf2012-08-01 12:24:18 +0200196 sector < part->start_sect + part_nr_sects_read(part);
Jens Axboea6f23652008-10-24 12:52:42 +0200197}
198
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200199/**
200 * disk_map_sector_rcu - map sector to partition
201 * @disk: gendisk of interest
202 * @sector: sector to map
203 *
204 * Find out which partition @sector maps to on @disk. This is
205 * primarily used for stats accounting.
206 *
207 * CONTEXT:
208 * RCU read locked. The returned partition pointer is valid only
209 * while preemption is disabled.
210 *
211 * RETURNS:
Tejun Heo074a7ac2008-08-25 19:56:14 +0900212 * Found partition on success, part0 is returned if no partition matches
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200213 */
214struct hd_struct *disk_map_sector_rcu(struct gendisk *disk, sector_t sector)
215{
Tejun Heo540eed52008-08-25 19:56:15 +0900216 struct disk_part_tbl *ptbl;
Jens Axboea6f23652008-10-24 12:52:42 +0200217 struct hd_struct *part;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200218 int i;
219
Tejun Heo540eed52008-08-25 19:56:15 +0900220 ptbl = rcu_dereference(disk->part_tbl);
221
Jens Axboea6f23652008-10-24 12:52:42 +0200222 part = rcu_dereference(ptbl->last_lookup);
223 if (part && sector_in_part(part, sector))
224 return part;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200225
Jens Axboea6f23652008-10-24 12:52:42 +0200226 for (i = 1; i < ptbl->len; i++) {
227 part = rcu_dereference(ptbl->part[i]);
228
229 if (part && sector_in_part(part, sector)) {
230 rcu_assign_pointer(ptbl->last_lookup, part);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200231 return part;
Jens Axboea6f23652008-10-24 12:52:42 +0200232 }
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200233 }
Tejun Heo074a7ac2008-08-25 19:56:14 +0900234 return &disk->part0;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200235}
236EXPORT_SYMBOL_GPL(disk_map_sector_rcu);
237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238/*
239 * Can be deleted altogether. Later.
240 *
241 */
242static struct blk_major_name {
243 struct blk_major_name *next;
244 int major;
245 char name[16];
Joe Korty68eef3b2006-03-31 02:30:32 -0800246} *major_names[BLKDEV_MAJOR_HASH_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248/* index in the above - for now: assume no multimajor ranges */
Yang Zhange61eb2e2010-12-17 09:00:18 +0100249static inline int major_to_index(unsigned major)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
Joe Korty68eef3b2006-03-31 02:30:32 -0800251 return major % BLKDEV_MAJOR_HASH_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
253
Joe Korty68eef3b2006-03-31 02:30:32 -0800254#ifdef CONFIG_PROC_FS
Tejun Heocf771cb2008-09-03 09:01:09 +0200255void blkdev_show(struct seq_file *seqf, off_t offset)
Neil Horman7170be52006-01-14 13:20:38 -0800256{
Joe Korty68eef3b2006-03-31 02:30:32 -0800257 struct blk_major_name *dp;
Neil Horman7170be52006-01-14 13:20:38 -0800258
Joe Korty68eef3b2006-03-31 02:30:32 -0800259 if (offset < BLKDEV_MAJOR_HASH_SIZE) {
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200260 mutex_lock(&block_class_lock);
Joe Korty68eef3b2006-03-31 02:30:32 -0800261 for (dp = major_names[offset]; dp; dp = dp->next)
Tejun Heocf771cb2008-09-03 09:01:09 +0200262 seq_printf(seqf, "%3d %s\n", dp->major, dp->name);
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200263 mutex_unlock(&block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
Joe Korty68eef3b2006-03-31 02:30:32 -0800266#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Márton Németh9e8c0bc2009-02-20 08:12:51 +0100268/**
269 * register_blkdev - register a new block device
270 *
271 * @major: the requested major device number [1..255]. If @major=0, try to
272 * allocate any unused major number.
273 * @name: the name of the new block device as a zero terminated string
274 *
275 * The @name must be unique within the system.
276 *
277 * The return value depends on the @major input parameter.
278 * - if a major device number was requested in range [1..255] then the
279 * function returns zero on success, or a negative error code
280 * - if any unused major number was requested with @major=0 parameter
281 * then the return value is the allocated major number in range
282 * [1..255] or a negative error code otherwise
283 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284int register_blkdev(unsigned int major, const char *name)
285{
286 struct blk_major_name **n, *p;
287 int index, ret = 0;
288
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200289 mutex_lock(&block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 /* temporary */
292 if (major == 0) {
293 for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) {
294 if (major_names[index] == NULL)
295 break;
296 }
297
298 if (index == 0) {
299 printk("register_blkdev: failed to get major for %s\n",
300 name);
301 ret = -EBUSY;
302 goto out;
303 }
304 major = index;
305 ret = major;
306 }
307
308 p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL);
309 if (p == NULL) {
310 ret = -ENOMEM;
311 goto out;
312 }
313
314 p->major = major;
315 strlcpy(p->name, name, sizeof(p->name));
316 p->next = NULL;
317 index = major_to_index(major);
318
319 for (n = &major_names[index]; *n; n = &(*n)->next) {
320 if ((*n)->major == major)
321 break;
322 }
323 if (!*n)
324 *n = p;
325 else
326 ret = -EBUSY;
327
328 if (ret < 0) {
329 printk("register_blkdev: cannot get major %d for %s\n",
330 major, name);
331 kfree(p);
332 }
333out:
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200334 mutex_unlock(&block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 return ret;
336}
337
338EXPORT_SYMBOL(register_blkdev);
339
Akinobu Mitaf4480242007-07-17 04:03:47 -0700340void unregister_blkdev(unsigned int major, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
342 struct blk_major_name **n;
343 struct blk_major_name *p = NULL;
344 int index = major_to_index(major);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200346 mutex_lock(&block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 for (n = &major_names[index]; *n; n = &(*n)->next)
348 if ((*n)->major == major)
349 break;
Akinobu Mita294462a2007-07-17 04:03:45 -0700350 if (!*n || strcmp((*n)->name, name)) {
351 WARN_ON(1);
Akinobu Mita294462a2007-07-17 04:03:45 -0700352 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 p = *n;
354 *n = p->next;
355 }
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200356 mutex_unlock(&block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358}
359
360EXPORT_SYMBOL(unregister_blkdev);
361
362static struct kobj_map *bdev_map;
363
Tejun Heobcce3de2008-08-25 19:47:22 +0900364/**
Tejun Heo870d6652008-08-25 19:47:25 +0900365 * blk_mangle_minor - scatter minor numbers apart
366 * @minor: minor number to mangle
367 *
368 * Scatter consecutively allocated @minor number apart if MANGLE_DEVT
369 * is enabled. Mangling twice gives the original value.
370 *
371 * RETURNS:
372 * Mangled value.
373 *
374 * CONTEXT:
375 * Don't care.
376 */
377static int blk_mangle_minor(int minor)
378{
379#ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT
380 int i;
381
382 for (i = 0; i < MINORBITS / 2; i++) {
383 int low = minor & (1 << i);
384 int high = minor & (1 << (MINORBITS - 1 - i));
385 int distance = MINORBITS - 1 - 2 * i;
386
387 minor ^= low | high; /* clear both bits */
388 low <<= distance; /* swap the positions */
389 high >>= distance;
390 minor |= low | high; /* and set */
391 }
392#endif
393 return minor;
394}
395
396/**
Tejun Heobcce3de2008-08-25 19:47:22 +0900397 * blk_alloc_devt - allocate a dev_t for a partition
398 * @part: partition to allocate dev_t for
Tejun Heobcce3de2008-08-25 19:47:22 +0900399 * @devt: out parameter for resulting dev_t
400 *
401 * Allocate a dev_t for block device.
402 *
403 * RETURNS:
404 * 0 on success, allocated dev_t is returned in *@devt. -errno on
405 * failure.
406 *
407 * CONTEXT:
408 * Might sleep.
409 */
410int blk_alloc_devt(struct hd_struct *part, dev_t *devt)
411{
412 struct gendisk *disk = part_to_disk(part);
413 int idx, rc;
414
415 /* in consecutive minor range? */
416 if (part->partno < disk->minors) {
417 *devt = MKDEV(disk->major, disk->first_minor + part->partno);
418 return 0;
419 }
420
421 /* allocate ext devt */
422 do {
423 if (!idr_pre_get(&ext_devt_idr, GFP_KERNEL))
424 return -ENOMEM;
425 rc = idr_get_new(&ext_devt_idr, part, &idx);
426 } while (rc == -EAGAIN);
427
428 if (rc)
429 return rc;
430
431 if (idx > MAX_EXT_DEVT) {
432 idr_remove(&ext_devt_idr, idx);
433 return -EBUSY;
434 }
435
Tejun Heo870d6652008-08-25 19:47:25 +0900436 *devt = MKDEV(BLOCK_EXT_MAJOR, blk_mangle_minor(idx));
Tejun Heobcce3de2008-08-25 19:47:22 +0900437 return 0;
438}
439
440/**
441 * blk_free_devt - free a dev_t
442 * @devt: dev_t to free
443 *
444 * Free @devt which was allocated using blk_alloc_devt().
445 *
446 * CONTEXT:
447 * Might sleep.
448 */
449void blk_free_devt(dev_t devt)
450{
451 might_sleep();
452
453 if (devt == MKDEV(0, 0))
454 return;
455
456 if (MAJOR(devt) == BLOCK_EXT_MAJOR) {
457 mutex_lock(&ext_devt_mutex);
Tejun Heo870d6652008-08-25 19:47:25 +0900458 idr_remove(&ext_devt_idr, blk_mangle_minor(MINOR(devt)));
Tejun Heobcce3de2008-08-25 19:47:22 +0900459 mutex_unlock(&ext_devt_mutex);
460 }
461}
462
Tejun Heo1f014292008-08-25 19:47:23 +0900463static char *bdevt_str(dev_t devt, char *buf)
464{
465 if (MAJOR(devt) <= 0xff && MINOR(devt) <= 0xff) {
466 char tbuf[BDEVT_SIZE];
467 snprintf(tbuf, BDEVT_SIZE, "%02x%02x", MAJOR(devt), MINOR(devt));
468 snprintf(buf, BDEVT_SIZE, "%-9s", tbuf);
469 } else
470 snprintf(buf, BDEVT_SIZE, "%03x:%05x", MAJOR(devt), MINOR(devt));
471
472 return buf;
473}
474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475/*
476 * Register device numbers dev..(dev+range-1)
477 * range must be nonzero
478 * The hash chain is sorted on range, so that subranges can override.
479 */
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200480void blk_register_region(dev_t devt, unsigned long range, struct module *module,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 struct kobject *(*probe)(dev_t, int *, void *),
482 int (*lock)(dev_t, void *), void *data)
483{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200484 kobj_map(bdev_map, devt, range, module, probe, lock, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485}
486
487EXPORT_SYMBOL(blk_register_region);
488
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200489void blk_unregister_region(dev_t devt, unsigned long range)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200491 kobj_unmap(bdev_map, devt, range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492}
493
494EXPORT_SYMBOL(blk_unregister_region);
495
Tejun Heocf771cb2008-09-03 09:01:09 +0200496static struct kobject *exact_match(dev_t devt, int *partno, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
498 struct gendisk *p = data;
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200499
Tejun Heoed9e1982008-08-25 19:56:05 +0900500 return &disk_to_dev(p)->kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501}
502
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200503static int exact_lock(dev_t devt, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
505 struct gendisk *p = data;
506
507 if (!get_disk(p))
508 return -1;
509 return 0;
510}
511
Al Viro4752bc32011-09-16 00:21:54 -0400512static void register_disk(struct gendisk *disk)
Tejun Heod2bf1b62010-12-08 20:57:36 +0100513{
514 struct device *ddev = disk_to_dev(disk);
515 struct block_device *bdev;
516 struct disk_part_iter piter;
517 struct hd_struct *part;
518 int err;
519
520 ddev->parent = disk->driverfs_dev;
521
522 dev_set_name(ddev, disk->disk_name);
523
524 /* delay uevents, until we scanned partition table */
525 dev_set_uevent_suppress(ddev, 1);
526
527 if (device_add(ddev))
528 return;
529 if (!sysfs_deprecated) {
530 err = sysfs_create_link(block_depr, &ddev->kobj,
531 kobject_name(&ddev->kobj));
532 if (err) {
533 device_del(ddev);
534 return;
535 }
536 }
537 disk->part0.holder_dir = kobject_create_and_add("holders", &ddev->kobj);
538 disk->slave_dir = kobject_create_and_add("slaves", &ddev->kobj);
539
540 /* No minors to use for partitions */
Tejun Heod27769e2011-08-23 20:01:04 +0200541 if (!disk_part_scan_enabled(disk))
Tejun Heod2bf1b62010-12-08 20:57:36 +0100542 goto exit;
543
544 /* No such device (e.g., media were just removed) */
545 if (!get_capacity(disk))
546 goto exit;
547
548 bdev = bdget_disk(disk, 0);
549 if (!bdev)
550 goto exit;
551
552 bdev->bd_invalidated = 1;
553 err = blkdev_get(bdev, FMODE_READ, NULL);
554 if (err < 0)
555 goto exit;
556 blkdev_put(bdev, FMODE_READ);
557
558exit:
559 /* announce disk after possible partitions are created */
560 dev_set_uevent_suppress(ddev, 0);
561 kobject_uevent(&ddev->kobj, KOBJ_ADD);
562
563 /* announce possible partitions */
564 disk_part_iter_init(&piter, disk, 0);
565 while ((part = disk_part_iter_next(&piter)))
566 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_ADD);
567 disk_part_iter_exit(&piter);
568}
569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570/**
571 * add_disk - add partitioning information to kernel list
572 * @disk: per-device partitioning information
573 *
574 * This function registers the partitioning information in @disk
575 * with the kernel.
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900576 *
577 * FIXME: error handling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 */
579void add_disk(struct gendisk *disk)
580{
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700581 struct backing_dev_info *bdi;
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900582 dev_t devt;
Greg Kroah-Hartman6ffeea72008-05-22 17:21:08 -0400583 int retval;
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700584
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900585 /* minors == 0 indicates to use ext devt from part0 and should
586 * be accompanied with EXT_DEVT flag. Make sure all
587 * parameters make sense.
588 */
589 WARN_ON(disk->minors && !(disk->major || disk->first_minor));
590 WARN_ON(!disk->minors && !(disk->flags & GENHD_FL_EXT_DEVT));
591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 disk->flags |= GENHD_FL_UP;
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900593
594 retval = blk_alloc_devt(&disk->part0, &devt);
595 if (retval) {
596 WARN_ON(1);
597 return;
598 }
599 disk_to_dev(disk)->devt = devt;
600
601 /* ->major and ->first_minor aren't supposed to be
602 * dereferenced from here on, but set them just in case.
603 */
604 disk->major = MAJOR(devt);
605 disk->first_minor = MINOR(devt);
606
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +0100607 disk_alloc_events(disk);
608
Wanlong Gao9f5e4862011-06-13 10:45:43 +0200609 /* Register BDI before referencing it from bdev */
Signed-off-by: Jan Kara01ea5062010-09-16 20:36:36 +0200610 bdi = &disk->queue->backing_dev_info;
611 bdi_register_dev(bdi, disk_devt(disk));
612
Tejun Heof331c022008-09-03 09:01:48 +0200613 blk_register_region(disk_devt(disk), disk->minors, NULL,
614 exact_match, exact_lock, disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 register_disk(disk);
616 blk_register_queue(disk);
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700617
Tejun Heo523e1d32011-10-19 14:31:07 +0200618 /*
619 * Take an extra ref on queue which will be put on disk_release()
620 * so that it sticks around as long as @disk is there.
621 */
Tejun Heo09ac46c2011-12-14 00:33:38 +0100622 WARN_ON_ONCE(!blk_get_queue(disk->queue));
Tejun Heo523e1d32011-10-19 14:31:07 +0200623
Tejun Heoed9e1982008-08-25 19:56:05 +0900624 retval = sysfs_create_link(&disk_to_dev(disk)->kobj, &bdi->dev->kobj,
625 "bdi");
Greg Kroah-Hartman6ffeea72008-05-22 17:21:08 -0400626 WARN_ON(retval);
Tejun Heo77ea8872010-12-08 20:57:37 +0100627
628 disk_add_events(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630EXPORT_SYMBOL(add_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Tejun Heod2bf1b62010-12-08 20:57:36 +0100632void del_gendisk(struct gendisk *disk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
Tejun Heod2bf1b62010-12-08 20:57:36 +0100634 struct disk_part_iter piter;
635 struct hd_struct *part;
636
Tejun Heo77ea8872010-12-08 20:57:37 +0100637 disk_del_events(disk);
638
Tejun Heod2bf1b62010-12-08 20:57:36 +0100639 /* invalidate stuff */
640 disk_part_iter_init(&piter, disk,
641 DISK_PITER_INCL_EMPTY | DISK_PITER_REVERSE);
642 while ((part = disk_part_iter_next(&piter))) {
643 invalidate_partition(disk, part->partno);
644 delete_partition(disk, part->partno);
645 }
646 disk_part_iter_exit(&piter);
647
648 invalidate_partition(disk, 0);
649 blk_free_devt(disk_to_dev(disk)->devt);
650 set_capacity(disk, 0);
651 disk->flags &= ~GENHD_FL_UP;
652
Tejun Heoed9e1982008-08-25 19:56:05 +0900653 sysfs_remove_link(&disk_to_dev(disk)->kobj, "bdi");
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700654 bdi_unregister(&disk->queue->backing_dev_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 blk_unregister_queue(disk);
Tejun Heof331c022008-09-03 09:01:48 +0200656 blk_unregister_region(disk_devt(disk), disk->minors);
Tejun Heod2bf1b62010-12-08 20:57:36 +0100657
658 part_stat_set_all(&disk->part0, 0);
659 disk->part0.stamp = 0;
660
661 kobject_put(disk->part0.holder_dir);
662 kobject_put(disk->slave_dir);
663 disk->driverfs_dev = NULL;
664 if (!sysfs_deprecated)
665 sysfs_remove_link(block_depr, dev_name(disk_to_dev(disk)));
666 device_del(disk_to_dev(disk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667}
Tejun Heod2bf1b62010-12-08 20:57:36 +0100668EXPORT_SYMBOL(del_gendisk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670/**
671 * get_gendisk - get partitioning information for a given device
Randy Dunlap710027a2008-08-19 20:13:11 +0200672 * @devt: device to get partitioning information for
Randy Dunlap496aa8a2008-10-16 07:46:23 +0200673 * @partno: returned partition index
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 *
675 * This function gets the structure containing partitioning
Randy Dunlap710027a2008-08-19 20:13:11 +0200676 * information for the given device @devt.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 */
Tejun Heocf771cb2008-09-03 09:01:09 +0200678struct gendisk *get_gendisk(dev_t devt, int *partno)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
Tejun Heobcce3de2008-08-25 19:47:22 +0900680 struct gendisk *disk = NULL;
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200681
Tejun Heobcce3de2008-08-25 19:47:22 +0900682 if (MAJOR(devt) != BLOCK_EXT_MAJOR) {
683 struct kobject *kobj;
684
685 kobj = kobj_lookup(bdev_map, devt, partno);
686 if (kobj)
687 disk = dev_to_disk(kobj_to_dev(kobj));
688 } else {
689 struct hd_struct *part;
690
691 mutex_lock(&ext_devt_mutex);
Tejun Heo870d6652008-08-25 19:47:25 +0900692 part = idr_find(&ext_devt_idr, blk_mangle_minor(MINOR(devt)));
Tejun Heobcce3de2008-08-25 19:47:22 +0900693 if (part && get_disk(part_to_disk(part))) {
694 *partno = part->partno;
695 disk = part_to_disk(part);
696 }
697 mutex_unlock(&ext_devt_mutex);
698 }
699
700 return disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701}
Divyesh Shahb6ac23af2010-04-15 08:54:59 +0200702EXPORT_SYMBOL(get_gendisk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Tejun Heof331c022008-09-03 09:01:48 +0200704/**
705 * bdget_disk - do bdget() by gendisk and partition number
706 * @disk: gendisk of interest
707 * @partno: partition number
708 *
709 * Find partition @partno from @disk, do bdget() on it.
710 *
711 * CONTEXT:
712 * Don't care.
713 *
714 * RETURNS:
715 * Resulting block_device on success, NULL on failure.
716 */
Harvey Harrisonaeb3d3a2008-08-28 09:27:42 +0200717struct block_device *bdget_disk(struct gendisk *disk, int partno)
Tejun Heof331c022008-09-03 09:01:48 +0200718{
Tejun Heo548b10e2008-08-29 09:01:47 +0200719 struct hd_struct *part;
720 struct block_device *bdev = NULL;
Tejun Heof331c022008-09-03 09:01:48 +0200721
Tejun Heo548b10e2008-08-29 09:01:47 +0200722 part = disk_get_part(disk, partno);
Tejun Heo2bbedcb2008-08-29 11:41:51 +0200723 if (part)
Tejun Heo548b10e2008-08-29 09:01:47 +0200724 bdev = bdget(part_devt(part));
725 disk_put_part(part);
Tejun Heof331c022008-09-03 09:01:48 +0200726
Tejun Heo548b10e2008-08-29 09:01:47 +0200727 return bdev;
Tejun Heof331c022008-09-03 09:01:48 +0200728}
729EXPORT_SYMBOL(bdget_disk);
730
Dave Gilbertdd2a3452007-05-09 02:33:24 -0700731/*
732 * print a full list of all partitions - intended for places where the root
733 * filesystem can't be mounted and thus to give the victim some idea of what
734 * went wrong
735 */
736void __init printk_all_partitions(void)
737{
Tejun Heodef4e382008-09-03 08:57:12 +0200738 struct class_dev_iter iter;
739 struct device *dev;
740
741 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
742 while ((dev = class_dev_iter_next(&iter))) {
743 struct gendisk *disk = dev_to_disk(dev);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200744 struct disk_part_iter piter;
745 struct hd_struct *part;
Tejun Heo1f014292008-08-25 19:47:23 +0900746 char name_buf[BDEVNAME_SIZE];
747 char devt_buf[BDEVT_SIZE];
Tejun Heodef4e382008-09-03 08:57:12 +0200748
749 /*
750 * Don't show empty devices or things that have been
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300751 * suppressed
Tejun Heodef4e382008-09-03 08:57:12 +0200752 */
753 if (get_capacity(disk) == 0 ||
754 (disk->flags & GENHD_FL_SUPPRESS_PARTITION_INFO))
755 continue;
756
757 /*
758 * Note, unlike /proc/partitions, I am showing the
759 * numbers in hex - the same format as the root=
760 * option takes.
761 */
Tejun Heo074a7ac2008-08-25 19:56:14 +0900762 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
763 while ((part = disk_part_iter_next(&piter))) {
764 bool is_part0 = part == &disk->part0;
Tejun Heodef4e382008-09-03 08:57:12 +0200765
Will Drewryb5af9212010-08-31 15:47:07 -0500766 printk("%s%s %10llu %s %s", is_part0 ? "" : " ",
Tejun Heo1f014292008-08-25 19:47:23 +0900767 bdevt_str(part_devt(part), devt_buf),
Vivek Goyalc83f6bf2012-08-01 12:24:18 +0200768 (unsigned long long)part_nr_sects_read(part) >> 1
769 , disk_name(disk, part->partno, name_buf),
Stephen Warren1ad7e892012-11-08 16:12:25 -0800770 part->info ? part->info->uuid : "");
Tejun Heo074a7ac2008-08-25 19:56:14 +0900771 if (is_part0) {
772 if (disk->driverfs_dev != NULL &&
773 disk->driverfs_dev->driver != NULL)
774 printk(" driver: %s\n",
775 disk->driverfs_dev->driver->name);
776 else
777 printk(" (driver?)\n");
778 } else
779 printk("\n");
780 }
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200781 disk_part_iter_exit(&piter);
Tejun Heodef4e382008-09-03 08:57:12 +0200782 }
783 class_dev_iter_exit(&iter);
Dave Gilbertdd2a3452007-05-09 02:33:24 -0700784}
785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786#ifdef CONFIG_PROC_FS
787/* iterator */
Tejun Heodef4e382008-09-03 08:57:12 +0200788static void *disk_seqf_start(struct seq_file *seqf, loff_t *pos)
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -0400789{
Tejun Heodef4e382008-09-03 08:57:12 +0200790 loff_t skip = *pos;
791 struct class_dev_iter *iter;
792 struct device *dev;
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -0400793
Harvey Harrisonaeb3d3a2008-08-28 09:27:42 +0200794 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
Tejun Heodef4e382008-09-03 08:57:12 +0200795 if (!iter)
796 return ERR_PTR(-ENOMEM);
797
798 seqf->private = iter;
799 class_dev_iter_init(iter, &block_class, NULL, &disk_type);
800 do {
801 dev = class_dev_iter_next(iter);
802 if (!dev)
803 return NULL;
804 } while (skip--);
805
806 return dev_to_disk(dev);
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -0400807}
808
Tejun Heodef4e382008-09-03 08:57:12 +0200809static void *disk_seqf_next(struct seq_file *seqf, void *v, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200811 struct device *dev;
Greg Kroah-Hartman66c64af2008-05-22 17:21:08 -0400812
Tejun Heodef4e382008-09-03 08:57:12 +0200813 (*pos)++;
814 dev = class_dev_iter_next(seqf->private);
Tejun Heo2ac3cee2008-09-03 08:53:37 +0200815 if (dev)
Greg Kroah-Hartman68c4d4a2008-05-22 17:21:08 -0400816 return dev_to_disk(dev);
Tejun Heo2ac3cee2008-09-03 08:53:37 +0200817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 return NULL;
819}
820
Tejun Heodef4e382008-09-03 08:57:12 +0200821static void disk_seqf_stop(struct seq_file *seqf, void *v)
Greg Kroah-Hartman27f30252008-05-22 17:21:08 -0400822{
Tejun Heodef4e382008-09-03 08:57:12 +0200823 struct class_dev_iter *iter = seqf->private;
Greg Kroah-Hartman27f30252008-05-22 17:21:08 -0400824
Tejun Heodef4e382008-09-03 08:57:12 +0200825 /* stop is called even after start failed :-( */
826 if (iter) {
827 class_dev_iter_exit(iter);
828 kfree(iter);
Kay Sievers5c0ef6d2008-08-16 14:30:30 +0200829 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830}
831
Tejun Heodef4e382008-09-03 08:57:12 +0200832static void *show_partition_start(struct seq_file *seqf, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
Jianpeng Ma06768062012-08-03 10:42:00 +0200834 void *p;
Tejun Heodef4e382008-09-03 08:57:12 +0200835
836 p = disk_seqf_start(seqf, pos);
Yang Zhangb9f985b2010-12-17 08:58:36 +0100837 if (!IS_ERR_OR_NULL(p) && !*pos)
Tejun Heodef4e382008-09-03 08:57:12 +0200838 seq_puts(seqf, "major minor #blocks name\n\n");
839 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840}
841
Tejun Heocf771cb2008-09-03 09:01:09 +0200842static int show_partition(struct seq_file *seqf, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
844 struct gendisk *sgp = v;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200845 struct disk_part_iter piter;
846 struct hd_struct *part;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 char buf[BDEVNAME_SIZE];
848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 /* Don't show non-partitionable removeable devices or empty devices */
Tejun Heod27769e2011-08-23 20:01:04 +0200850 if (!get_capacity(sgp) || (!disk_max_parts(sgp) &&
Tejun Heof331c022008-09-03 09:01:48 +0200851 (sgp->flags & GENHD_FL_REMOVABLE)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 return 0;
853 if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)
854 return 0;
855
856 /* show the full disk and all non-0 size partitions of it */
Tejun Heo074a7ac2008-08-25 19:56:14 +0900857 disk_part_iter_init(&piter, sgp, DISK_PITER_INCL_PART0);
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200858 while ((part = disk_part_iter_next(&piter)))
Tejun Heo1f014292008-08-25 19:47:23 +0900859 seq_printf(seqf, "%4d %7d %10llu %s\n",
Tejun Heof331c022008-09-03 09:01:48 +0200860 MAJOR(part_devt(part)), MINOR(part_devt(part)),
Vivek Goyalc83f6bf2012-08-01 12:24:18 +0200861 (unsigned long long)part_nr_sects_read(part) >> 1,
Tejun Heof331c022008-09-03 09:01:48 +0200862 disk_name(sgp, part->partno, buf));
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200863 disk_part_iter_exit(&piter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
865 return 0;
866}
867
Alexey Dobriyanf5009752008-10-04 23:53:21 +0400868static const struct seq_operations partitions_op = {
Tejun Heodef4e382008-09-03 08:57:12 +0200869 .start = show_partition_start,
870 .next = disk_seqf_next,
871 .stop = disk_seqf_stop,
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200872 .show = show_partition
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873};
Alexey Dobriyanf5009752008-10-04 23:53:21 +0400874
875static int partitions_open(struct inode *inode, struct file *file)
876{
877 return seq_open(file, &partitions_op);
878}
879
880static const struct file_operations proc_partitions_operations = {
881 .open = partitions_open,
882 .read = seq_read,
883 .llseek = seq_lseek,
884 .release = seq_release,
885};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886#endif
887
888
Tejun Heocf771cb2008-09-03 09:01:09 +0200889static struct kobject *base_probe(dev_t devt, int *partno, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200891 if (request_module("block-major-%d-%d", MAJOR(devt), MINOR(devt)) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 /* Make old-style 2.4 aliases work */
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200893 request_module("block-major-%d", MAJOR(devt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 return NULL;
895}
896
897static int __init genhd_device_init(void)
898{
Dan Williamse105b8b2008-04-21 10:51:07 -0700899 int error;
900
901 block_class.dev_kobj = sysfs_dev_block_kobj;
902 error = class_register(&block_class);
Roland McGrathee27a552008-03-11 17:13:15 -0700903 if (unlikely(error))
904 return error;
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200905 bdev_map = kobj_map_init(base_probe, &block_class_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 blk_dev_init();
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200907
Zhang, Yanmin561ec682008-11-14 08:26:30 +0100908 register_blkdev(BLOCK_EXT_MAJOR, "blkext");
909
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200910 /* create top-level block dir */
Andi Kleene52eec12010-09-08 16:54:17 +0200911 if (!sysfs_deprecated)
912 block_depr = kobject_create_and_add("block", NULL);
Greg Kroah-Hartman830d3cf2007-11-06 10:36:58 -0800913 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914}
915
916subsys_initcall(genhd_device_init);
917
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200918static ssize_t disk_range_show(struct device *dev,
919 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200921 struct gendisk *disk = dev_to_disk(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200923 return sprintf(buf, "%d\n", disk->minors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924}
925
Tejun Heo1f014292008-08-25 19:47:23 +0900926static ssize_t disk_ext_range_show(struct device *dev,
927 struct device_attribute *attr, char *buf)
928{
929 struct gendisk *disk = dev_to_disk(dev);
930
Tejun Heob5d0b9d2008-09-03 09:06:42 +0200931 return sprintf(buf, "%d\n", disk_max_parts(disk));
Tejun Heo1f014292008-08-25 19:47:23 +0900932}
933
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200934static ssize_t disk_removable_show(struct device *dev,
935 struct device_attribute *attr, char *buf)
Kay Sieversa7fd6702005-10-01 14:49:43 +0200936{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200937 struct gendisk *disk = dev_to_disk(dev);
Kay Sieversa7fd6702005-10-01 14:49:43 +0200938
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200939 return sprintf(buf, "%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200941}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Kay Sievers1c9ce522008-06-13 09:41:00 +0200943static ssize_t disk_ro_show(struct device *dev,
944 struct device_attribute *attr, char *buf)
945{
946 struct gendisk *disk = dev_to_disk(dev);
947
Tejun Heob7db9952008-08-25 19:56:10 +0900948 return sprintf(buf, "%d\n", get_disk_ro(disk) ? 1 : 0);
Kay Sievers1c9ce522008-06-13 09:41:00 +0200949}
950
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200951static ssize_t disk_capability_show(struct device *dev,
952 struct device_attribute *attr, char *buf)
Kristen Carlson Accardi86ce18d2007-05-23 13:57:38 -0700953{
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200954 struct gendisk *disk = dev_to_disk(dev);
955
956 return sprintf(buf, "%x\n", disk->flags);
Kristen Carlson Accardi86ce18d2007-05-23 13:57:38 -0700957}
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200958
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400959static ssize_t disk_alignment_offset_show(struct device *dev,
960 struct device_attribute *attr,
961 char *buf)
962{
963 struct gendisk *disk = dev_to_disk(dev);
964
965 return sprintf(buf, "%d\n", queue_alignment_offset(disk->queue));
966}
967
Martin K. Petersen86b37282009-11-10 11:50:21 +0100968static ssize_t disk_discard_alignment_show(struct device *dev,
969 struct device_attribute *attr,
970 char *buf)
971{
972 struct gendisk *disk = dev_to_disk(dev);
973
Martin K. Petersendd3d1452010-01-11 03:21:48 -0500974 return sprintf(buf, "%d\n", queue_discard_alignment(disk->queue));
Martin K. Petersen86b37282009-11-10 11:50:21 +0100975}
976
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200977static DEVICE_ATTR(range, S_IRUGO, disk_range_show, NULL);
Tejun Heo1f014292008-08-25 19:47:23 +0900978static DEVICE_ATTR(ext_range, S_IRUGO, disk_ext_range_show, NULL);
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200979static DEVICE_ATTR(removable, S_IRUGO, disk_removable_show, NULL);
Kay Sievers1c9ce522008-06-13 09:41:00 +0200980static DEVICE_ATTR(ro, S_IRUGO, disk_ro_show, NULL);
Tejun Heoe5610522008-08-25 19:56:09 +0900981static DEVICE_ATTR(size, S_IRUGO, part_size_show, NULL);
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400982static DEVICE_ATTR(alignment_offset, S_IRUGO, disk_alignment_offset_show, NULL);
Martin K. Petersen86b37282009-11-10 11:50:21 +0100983static DEVICE_ATTR(discard_alignment, S_IRUGO, disk_discard_alignment_show,
984 NULL);
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200985static DEVICE_ATTR(capability, S_IRUGO, disk_capability_show, NULL);
Tejun Heo074a7ac2008-08-25 19:56:14 +0900986static DEVICE_ATTR(stat, S_IRUGO, part_stat_show, NULL);
Nikanth Karthikesan316d3152009-10-06 20:16:55 +0200987static DEVICE_ATTR(inflight, S_IRUGO, part_inflight_show, NULL);
Akinobu Mitac17bb492006-12-08 02:39:46 -0800988#ifdef CONFIG_FAIL_MAKE_REQUEST
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200989static struct device_attribute dev_attr_fail =
Tejun Heoeddb2e22008-08-25 19:56:13 +0900990 __ATTR(make-it-fail, S_IRUGO|S_IWUSR, part_fail_show, part_fail_store);
Akinobu Mitac17bb492006-12-08 02:39:46 -0800991#endif
Jens Axboe581d4e22008-09-14 05:56:33 -0700992#ifdef CONFIG_FAIL_IO_TIMEOUT
993static struct device_attribute dev_attr_fail_timeout =
994 __ATTR(io-timeout-fail, S_IRUGO|S_IWUSR, part_timeout_show,
995 part_timeout_store);
996#endif
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200997
998static struct attribute *disk_attrs[] = {
999 &dev_attr_range.attr,
Tejun Heo1f014292008-08-25 19:47:23 +09001000 &dev_attr_ext_range.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001001 &dev_attr_removable.attr,
Kay Sievers1c9ce522008-06-13 09:41:00 +02001002 &dev_attr_ro.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001003 &dev_attr_size.attr,
Martin K. Petersenc72758f2009-05-22 17:17:53 -04001004 &dev_attr_alignment_offset.attr,
Martin K. Petersen86b37282009-11-10 11:50:21 +01001005 &dev_attr_discard_alignment.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001006 &dev_attr_capability.attr,
1007 &dev_attr_stat.attr,
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001008 &dev_attr_inflight.attr,
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001009#ifdef CONFIG_FAIL_MAKE_REQUEST
1010 &dev_attr_fail.attr,
1011#endif
Jens Axboe581d4e22008-09-14 05:56:33 -07001012#ifdef CONFIG_FAIL_IO_TIMEOUT
1013 &dev_attr_fail_timeout.attr,
1014#endif
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001015 NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016};
1017
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001018static struct attribute_group disk_attr_group = {
1019 .attrs = disk_attrs,
1020};
1021
David Brownella4dbd672009-06-24 10:06:31 -07001022static const struct attribute_group *disk_attr_groups[] = {
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001023 &disk_attr_group,
1024 NULL
1025};
1026
Tejun Heo540eed52008-08-25 19:56:15 +09001027/**
1028 * disk_replace_part_tbl - replace disk->part_tbl in RCU-safe way
1029 * @disk: disk to replace part_tbl for
1030 * @new_ptbl: new part_tbl to install
1031 *
1032 * Replace disk->part_tbl with @new_ptbl in RCU-safe way. The
1033 * original ptbl is freed using RCU callback.
1034 *
1035 * LOCKING:
1036 * Matching bd_mutx locked.
1037 */
1038static void disk_replace_part_tbl(struct gendisk *disk,
1039 struct disk_part_tbl *new_ptbl)
1040{
1041 struct disk_part_tbl *old_ptbl = disk->part_tbl;
1042
1043 rcu_assign_pointer(disk->part_tbl, new_ptbl);
Jens Axboea6f23652008-10-24 12:52:42 +02001044
1045 if (old_ptbl) {
1046 rcu_assign_pointer(old_ptbl->last_lookup, NULL);
Lai Jiangshan57bdfbf2011-03-18 11:42:58 +08001047 kfree_rcu(old_ptbl, rcu_head);
Jens Axboea6f23652008-10-24 12:52:42 +02001048 }
Tejun Heo540eed52008-08-25 19:56:15 +09001049}
1050
1051/**
1052 * disk_expand_part_tbl - expand disk->part_tbl
1053 * @disk: disk to expand part_tbl for
1054 * @partno: expand such that this partno can fit in
1055 *
1056 * Expand disk->part_tbl such that @partno can fit in. disk->part_tbl
1057 * uses RCU to allow unlocked dereferencing for stats and other stuff.
1058 *
1059 * LOCKING:
1060 * Matching bd_mutex locked, might sleep.
1061 *
1062 * RETURNS:
1063 * 0 on success, -errno on failure.
1064 */
1065int disk_expand_part_tbl(struct gendisk *disk, int partno)
1066{
1067 struct disk_part_tbl *old_ptbl = disk->part_tbl;
1068 struct disk_part_tbl *new_ptbl;
1069 int len = old_ptbl ? old_ptbl->len : 0;
1070 int target = partno + 1;
1071 size_t size;
1072 int i;
1073
1074 /* disk_max_parts() is zero during initialization, ignore if so */
1075 if (disk_max_parts(disk) && target > disk_max_parts(disk))
1076 return -EINVAL;
1077
1078 if (target <= len)
1079 return 0;
1080
1081 size = sizeof(*new_ptbl) + target * sizeof(new_ptbl->part[0]);
1082 new_ptbl = kzalloc_node(size, GFP_KERNEL, disk->node_id);
1083 if (!new_ptbl)
1084 return -ENOMEM;
1085
Tejun Heo540eed52008-08-25 19:56:15 +09001086 new_ptbl->len = target;
1087
1088 for (i = 0; i < len; i++)
1089 rcu_assign_pointer(new_ptbl->part[i], old_ptbl->part[i]);
1090
1091 disk_replace_part_tbl(disk, new_ptbl);
1092 return 0;
1093}
1094
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001095static void disk_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001097 struct gendisk *disk = dev_to_disk(dev);
1098
Tejun Heo77ea8872010-12-08 20:57:37 +01001099 disk_release_events(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 kfree(disk->random);
Tejun Heo540eed52008-08-25 19:56:15 +09001101 disk_replace_part_tbl(disk, NULL);
Tejun Heo074a7ac2008-08-25 19:56:14 +09001102 free_part_stats(&disk->part0);
Will Drewry6d1d8052010-08-31 15:47:05 -05001103 free_part_info(&disk->part0);
Tejun Heo523e1d32011-10-19 14:31:07 +02001104 if (disk->queue)
1105 blk_put_queue(disk->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 kfree(disk);
1107}
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001108struct class block_class = {
1109 .name = "block",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110};
1111
Al Viro2c9ede52011-07-23 20:24:48 -04001112static char *block_devnode(struct device *dev, umode_t *mode)
Kay Sieversb03f38b2009-04-30 15:23:42 +02001113{
1114 struct gendisk *disk = dev_to_disk(dev);
1115
Kay Sieverse454cea2009-09-18 23:01:12 +02001116 if (disk->devnode)
1117 return disk->devnode(disk, mode);
Kay Sieversb03f38b2009-04-30 15:23:42 +02001118 return NULL;
1119}
1120
Adrian Bunk1826ead2008-03-04 11:23:46 +01001121static struct device_type disk_type = {
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001122 .name = "disk",
1123 .groups = disk_attr_groups,
1124 .release = disk_release,
Kay Sieverse454cea2009-09-18 23:01:12 +02001125 .devnode = block_devnode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126};
1127
Randy Dunlapa6e2ba82008-05-23 09:44:11 -07001128#ifdef CONFIG_PROC_FS
Tejun Heocf771cb2008-09-03 09:01:09 +02001129/*
1130 * aggregate disk stat collector. Uses the same stats that the sysfs
1131 * entries do, above, but makes them available through one seq_file.
1132 *
1133 * The output looks suspiciously like /proc/partitions with a bunch of
1134 * extra fields.
1135 */
1136static int diskstats_show(struct seq_file *seqf, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137{
1138 struct gendisk *gp = v;
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001139 struct disk_part_iter piter;
1140 struct hd_struct *hd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 char buf[BDEVNAME_SIZE];
Tejun Heoc9959052008-08-25 19:47:21 +09001142 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
1144 /*
Tejun Heoed9e1982008-08-25 19:56:05 +09001145 if (&disk_to_dev(gp)->kobj.entry == block_class.devices.next)
Tejun Heocf771cb2008-09-03 09:01:09 +02001146 seq_puts(seqf, "major minor name"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 " rio rmerge rsect ruse wio wmerge "
1148 "wsect wuse running use aveq"
1149 "\n\n");
1150 */
Wanlong Gao9f5e4862011-06-13 10:45:43 +02001151
Tejun Heo71982a42009-04-17 08:34:48 +02001152 disk_part_iter_init(&piter, gp, DISK_PITER_INCL_EMPTY_PART0);
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001153 while ((hd = disk_part_iter_next(&piter))) {
Tejun Heo074a7ac2008-08-25 19:56:14 +09001154 cpu = part_stat_lock();
Tejun Heoc9959052008-08-25 19:47:21 +09001155 part_round_stats(cpu, hd);
Tejun Heo074a7ac2008-08-25 19:56:14 +09001156 part_stat_unlock();
Herbert Poetzlf95fe9c2011-08-02 12:43:50 +02001157 seq_printf(seqf, "%4d %7d %s %lu %lu %lu "
1158 "%u %lu %lu %lu %u %u %u %u\n",
Tejun Heof331c022008-09-03 09:01:48 +02001159 MAJOR(part_devt(hd)), MINOR(part_devt(hd)),
1160 disk_name(gp, hd->partno, buf),
Liu Yuan53f22952011-03-02 11:00:15 -05001161 part_stat_read(hd, ios[READ]),
1162 part_stat_read(hd, merges[READ]),
Herbert Poetzlf95fe9c2011-08-02 12:43:50 +02001163 part_stat_read(hd, sectors[READ]),
Liu Yuan53f22952011-03-02 11:00:15 -05001164 jiffies_to_msecs(part_stat_read(hd, ticks[READ])),
1165 part_stat_read(hd, ios[WRITE]),
1166 part_stat_read(hd, merges[WRITE]),
Herbert Poetzlf95fe9c2011-08-02 12:43:50 +02001167 part_stat_read(hd, sectors[WRITE]),
Liu Yuan53f22952011-03-02 11:00:15 -05001168 jiffies_to_msecs(part_stat_read(hd, ticks[WRITE])),
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001169 part_in_flight(hd),
Jerome Marchand28f39d52008-02-08 11:04:56 +01001170 jiffies_to_msecs(part_stat_read(hd, io_ticks)),
1171 jiffies_to_msecs(part_stat_read(hd, time_in_queue))
1172 );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 }
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001174 disk_part_iter_exit(&piter);
Wanlong Gao9f5e4862011-06-13 10:45:43 +02001175
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 return 0;
1177}
1178
Alexey Dobriyan31d85ab2008-10-06 12:55:38 +04001179static const struct seq_operations diskstats_op = {
Tejun Heodef4e382008-09-03 08:57:12 +02001180 .start = disk_seqf_start,
1181 .next = disk_seqf_next,
1182 .stop = disk_seqf_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 .show = diskstats_show
1184};
Alexey Dobriyanf5009752008-10-04 23:53:21 +04001185
Alexey Dobriyan31d85ab2008-10-06 12:55:38 +04001186static int diskstats_open(struct inode *inode, struct file *file)
1187{
1188 return seq_open(file, &diskstats_op);
1189}
1190
1191static const struct file_operations proc_diskstats_operations = {
1192 .open = diskstats_open,
1193 .read = seq_read,
1194 .llseek = seq_lseek,
1195 .release = seq_release,
1196};
1197
Alexey Dobriyanf5009752008-10-04 23:53:21 +04001198static int __init proc_genhd_init(void)
1199{
Alexey Dobriyan31d85ab2008-10-06 12:55:38 +04001200 proc_create("diskstats", 0, NULL, &proc_diskstats_operations);
Alexey Dobriyanf5009752008-10-04 23:53:21 +04001201 proc_create("partitions", 0, NULL, &proc_partitions_operations);
1202 return 0;
1203}
1204module_init(proc_genhd_init);
Randy Dunlapa6e2ba82008-05-23 09:44:11 -07001205#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Tejun Heocf771cb2008-09-03 09:01:09 +02001207dev_t blk_lookup_devt(const char *name, int partno)
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001208{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001209 dev_t devt = MKDEV(0, 0);
Tejun Heodef4e382008-09-03 08:57:12 +02001210 struct class_dev_iter iter;
1211 struct device *dev;
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001212
Tejun Heodef4e382008-09-03 08:57:12 +02001213 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
1214 while ((dev = class_dev_iter_next(&iter))) {
1215 struct gendisk *disk = dev_to_disk(dev);
Tejun Heo548b10e2008-08-29 09:01:47 +02001216 struct hd_struct *part;
Tejun Heodef4e382008-09-03 08:57:12 +02001217
Kay Sievers3ada8b72009-01-06 10:44:43 -08001218 if (strcmp(dev_name(dev), name))
Tejun Heof331c022008-09-03 09:01:48 +02001219 continue;
Tejun Heof331c022008-09-03 09:01:48 +02001220
Neil Brown41b8c852009-02-18 10:33:59 +01001221 if (partno < disk->minors) {
1222 /* We need to return the right devno, even
1223 * if the partition doesn't exist yet.
1224 */
1225 devt = MKDEV(MAJOR(dev->devt),
1226 MINOR(dev->devt) + partno);
1227 break;
1228 }
Tejun Heo548b10e2008-08-29 09:01:47 +02001229 part = disk_get_part(disk, partno);
Tejun Heo2bbedcb2008-08-29 11:41:51 +02001230 if (part) {
Tejun Heof331c022008-09-03 09:01:48 +02001231 devt = part_devt(part);
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001232 disk_put_part(part);
Tejun Heo548b10e2008-08-29 09:01:47 +02001233 break;
Tejun Heodef4e382008-09-03 08:57:12 +02001234 }
Tejun Heo548b10e2008-08-29 09:01:47 +02001235 disk_put_part(part);
Kay Sievers5c0ef6d2008-08-16 14:30:30 +02001236 }
Tejun Heodef4e382008-09-03 08:57:12 +02001237 class_dev_iter_exit(&iter);
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001238 return devt;
1239}
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001240EXPORT_SYMBOL(blk_lookup_devt);
1241
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242struct gendisk *alloc_disk(int minors)
1243{
Ezequiel Garciac304a512012-11-10 10:39:44 +01001244 return alloc_disk_node(minors, NUMA_NO_NODE);
Christoph Lameter19460892005-06-23 00:08:19 -07001245}
Tejun Heo689d6fa2008-08-25 19:56:16 +09001246EXPORT_SYMBOL(alloc_disk);
Christoph Lameter19460892005-06-23 00:08:19 -07001247
1248struct gendisk *alloc_disk_node(int minors, int node_id)
1249{
1250 struct gendisk *disk;
1251
Christoph Lameter94f60302007-07-17 04:03:29 -07001252 disk = kmalloc_node(sizeof(struct gendisk),
1253 GFP_KERNEL | __GFP_ZERO, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 if (disk) {
Tejun Heo074a7ac2008-08-25 19:56:14 +09001255 if (!init_part_stats(&disk->part0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 kfree(disk);
1257 return NULL;
1258 }
Cheng Renquanbf91db12008-11-20 08:37:37 +01001259 disk->node_id = node_id;
Tejun Heo540eed52008-08-25 19:56:15 +09001260 if (disk_expand_part_tbl(disk, 0)) {
1261 free_part_stats(&disk->part0);
Tejun Heob5d0b9d2008-09-03 09:06:42 +02001262 kfree(disk);
1263 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 }
Tejun Heo540eed52008-08-25 19:56:15 +09001265 disk->part_tbl->part[0] = &disk->part0;
Jens Axboe6c23a962011-01-07 08:43:37 +01001266
Vivek Goyalc83f6bf2012-08-01 12:24:18 +02001267 /*
1268 * set_capacity() and get_capacity() currently don't use
1269 * seqcounter to read/update the part0->nr_sects. Still init
1270 * the counter as we can read the sectors in IO submission
1271 * patch using seqence counters.
1272 *
1273 * TODO: Ideally set_capacity() and get_capacity() should be
1274 * converted to make use of bd_mutex and sequence counters.
1275 */
1276 seqcount_init(&disk->part0.nr_sects_seq);
Jens Axboe6c23a962011-01-07 08:43:37 +01001277 hd_ref_init(&disk->part0);
Tejun Heob5d0b9d2008-09-03 09:06:42 +02001278
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 disk->minors = minors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 rand_initialize_disk(disk);
Tejun Heoed9e1982008-08-25 19:56:05 +09001281 disk_to_dev(disk)->class = &block_class;
1282 disk_to_dev(disk)->type = &disk_type;
1283 device_initialize(disk_to_dev(disk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 }
1285 return disk;
1286}
Christoph Lameter19460892005-06-23 00:08:19 -07001287EXPORT_SYMBOL(alloc_disk_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
1289struct kobject *get_disk(struct gendisk *disk)
1290{
1291 struct module *owner;
1292 struct kobject *kobj;
1293
1294 if (!disk->fops)
1295 return NULL;
1296 owner = disk->fops->owner;
1297 if (owner && !try_module_get(owner))
1298 return NULL;
Tejun Heoed9e1982008-08-25 19:56:05 +09001299 kobj = kobject_get(&disk_to_dev(disk)->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 if (kobj == NULL) {
1301 module_put(owner);
1302 return NULL;
1303 }
1304 return kobj;
1305
1306}
1307
1308EXPORT_SYMBOL(get_disk);
1309
1310void put_disk(struct gendisk *disk)
1311{
1312 if (disk)
Tejun Heoed9e1982008-08-25 19:56:05 +09001313 kobject_put(&disk_to_dev(disk)->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314}
1315
1316EXPORT_SYMBOL(put_disk);
1317
Hannes Reineckee3264a42009-07-28 09:13:13 +02001318static void set_disk_ro_uevent(struct gendisk *gd, int ro)
1319{
1320 char event[] = "DISK_RO=1";
1321 char *envp[] = { event, NULL };
1322
1323 if (!ro)
1324 event[8] = '0';
1325 kobject_uevent_env(&disk_to_dev(gd)->kobj, KOBJ_CHANGE, envp);
1326}
1327
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328void set_device_ro(struct block_device *bdev, int flag)
1329{
Tejun Heob7db9952008-08-25 19:56:10 +09001330 bdev->bd_part->policy = flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331}
1332
1333EXPORT_SYMBOL(set_device_ro);
1334
1335void set_disk_ro(struct gendisk *disk, int flag)
1336{
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001337 struct disk_part_iter piter;
1338 struct hd_struct *part;
1339
Hannes Reineckee3264a42009-07-28 09:13:13 +02001340 if (disk->part0.policy != flag) {
1341 set_disk_ro_uevent(disk, flag);
1342 disk->part0.policy = flag;
1343 }
1344
1345 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_EMPTY);
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001346 while ((part = disk_part_iter_next(&piter)))
1347 part->policy = flag;
1348 disk_part_iter_exit(&piter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349}
1350
1351EXPORT_SYMBOL(set_disk_ro);
1352
1353int bdev_read_only(struct block_device *bdev)
1354{
1355 if (!bdev)
1356 return 0;
Tejun Heob7db9952008-08-25 19:56:10 +09001357 return bdev->bd_part->policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358}
1359
1360EXPORT_SYMBOL(bdev_read_only);
1361
Tejun Heocf771cb2008-09-03 09:01:09 +02001362int invalidate_partition(struct gendisk *disk, int partno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363{
1364 int res = 0;
Tejun Heocf771cb2008-09-03 09:01:09 +02001365 struct block_device *bdev = bdget_disk(disk, partno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 if (bdev) {
Christoph Hellwig2ef41632005-05-05 16:15:59 -07001367 fsync_bdev(bdev);
NeilBrown93b270f2011-02-24 17:25:47 +11001368 res = __invalidate_device(bdev, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 bdput(bdev);
1370 }
1371 return res;
1372}
1373
1374EXPORT_SYMBOL(invalidate_partition);
Tejun Heo77ea8872010-12-08 20:57:37 +01001375
1376/*
1377 * Disk events - monitor disk events like media change and eject request.
1378 */
1379struct disk_events {
1380 struct list_head node; /* all disk_event's */
1381 struct gendisk *disk; /* the associated disk */
1382 spinlock_t lock;
1383
Tejun Heofdd514e2011-06-09 20:43:59 +02001384 struct mutex block_mutex; /* protects blocking */
Tejun Heo77ea8872010-12-08 20:57:37 +01001385 int block; /* event blocking depth */
1386 unsigned int pending; /* events already sent out */
1387 unsigned int clearing; /* events being cleared */
1388
1389 long poll_msecs; /* interval, -1 for default */
1390 struct delayed_work dwork;
1391};
1392
1393static const char *disk_events_strs[] = {
1394 [ilog2(DISK_EVENT_MEDIA_CHANGE)] = "media_change",
1395 [ilog2(DISK_EVENT_EJECT_REQUEST)] = "eject_request",
1396};
1397
1398static char *disk_uevents[] = {
1399 [ilog2(DISK_EVENT_MEDIA_CHANGE)] = "DISK_MEDIA_CHANGE=1",
1400 [ilog2(DISK_EVENT_EJECT_REQUEST)] = "DISK_EJECT_REQUEST=1",
1401};
1402
1403/* list of all disk_events */
1404static DEFINE_MUTEX(disk_events_mutex);
1405static LIST_HEAD(disk_events);
1406
1407/* disable in-kernel polling by default */
1408static unsigned long disk_events_dfl_poll_msecs = 0;
1409
1410static unsigned long disk_events_poll_jiffies(struct gendisk *disk)
1411{
1412 struct disk_events *ev = disk->ev;
1413 long intv_msecs = 0;
1414
1415 /*
1416 * If device-specific poll interval is set, always use it. If
1417 * the default is being used, poll iff there are events which
1418 * can't be monitored asynchronously.
1419 */
1420 if (ev->poll_msecs >= 0)
1421 intv_msecs = ev->poll_msecs;
1422 else if (disk->events & ~disk->async_events)
1423 intv_msecs = disk_events_dfl_poll_msecs;
1424
1425 return msecs_to_jiffies(intv_msecs);
1426}
1427
Tejun Heoc3af54a2011-06-09 20:43:55 +02001428/**
1429 * disk_block_events - block and flush disk event checking
1430 * @disk: disk to block events for
1431 *
1432 * On return from this function, it is guaranteed that event checking
1433 * isn't in progress and won't happen until unblocked by
1434 * disk_unblock_events(). Events blocking is counted and the actual
1435 * unblocking happens after the matching number of unblocks are done.
1436 *
1437 * Note that this intentionally does not block event checking from
1438 * disk_clear_events().
1439 *
1440 * CONTEXT:
1441 * Might sleep.
1442 */
1443void disk_block_events(struct gendisk *disk)
Tejun Heo77ea8872010-12-08 20:57:37 +01001444{
1445 struct disk_events *ev = disk->ev;
1446 unsigned long flags;
1447 bool cancel;
1448
Tejun Heoc3af54a2011-06-09 20:43:55 +02001449 if (!ev)
1450 return;
1451
Tejun Heofdd514e2011-06-09 20:43:59 +02001452 /*
1453 * Outer mutex ensures that the first blocker completes canceling
1454 * the event work before further blockers are allowed to finish.
1455 */
1456 mutex_lock(&ev->block_mutex);
1457
Tejun Heo77ea8872010-12-08 20:57:37 +01001458 spin_lock_irqsave(&ev->lock, flags);
1459 cancel = !ev->block++;
1460 spin_unlock_irqrestore(&ev->lock, flags);
1461
Tejun Heoc3af54a2011-06-09 20:43:55 +02001462 if (cancel)
1463 cancel_delayed_work_sync(&disk->ev->dwork);
Tejun Heofdd514e2011-06-09 20:43:59 +02001464
1465 mutex_unlock(&ev->block_mutex);
Tejun Heo77ea8872010-12-08 20:57:37 +01001466}
1467
1468static void __disk_unblock_events(struct gendisk *disk, bool check_now)
1469{
1470 struct disk_events *ev = disk->ev;
1471 unsigned long intv;
1472 unsigned long flags;
1473
1474 spin_lock_irqsave(&ev->lock, flags);
1475
1476 if (WARN_ON_ONCE(ev->block <= 0))
1477 goto out_unlock;
1478
1479 if (--ev->block)
1480 goto out_unlock;
1481
1482 /*
1483 * Not exactly a latency critical operation, set poll timer
1484 * slack to 25% and kick event check.
1485 */
1486 intv = disk_events_poll_jiffies(disk);
1487 set_timer_slack(&ev->dwork.timer, intv / 4);
1488 if (check_now)
Tejun Heo3b07e9c2012-08-20 14:51:24 -07001489 queue_delayed_work(system_freezable_wq, &ev->dwork, 0);
Tejun Heo77ea8872010-12-08 20:57:37 +01001490 else if (intv)
Tejun Heo3b07e9c2012-08-20 14:51:24 -07001491 queue_delayed_work(system_freezable_wq, &ev->dwork, intv);
Tejun Heo77ea8872010-12-08 20:57:37 +01001492out_unlock:
1493 spin_unlock_irqrestore(&ev->lock, flags);
1494}
1495
1496/**
Tejun Heo77ea8872010-12-08 20:57:37 +01001497 * disk_unblock_events - unblock disk event checking
1498 * @disk: disk to unblock events for
1499 *
1500 * Undo disk_block_events(). When the block count reaches zero, it
1501 * starts events polling if configured.
1502 *
1503 * CONTEXT:
1504 * Don't care. Safe to call from irq context.
1505 */
1506void disk_unblock_events(struct gendisk *disk)
1507{
1508 if (disk->ev)
Tejun Heofacc31d2011-03-09 19:54:27 +01001509 __disk_unblock_events(disk, false);
Tejun Heo77ea8872010-12-08 20:57:37 +01001510}
1511
1512/**
Tejun Heo85ef06d2011-07-01 16:17:47 +02001513 * disk_flush_events - schedule immediate event checking and flushing
1514 * @disk: disk to check and flush events for
1515 * @mask: events to flush
Tejun Heo77ea8872010-12-08 20:57:37 +01001516 *
Tejun Heo85ef06d2011-07-01 16:17:47 +02001517 * Schedule immediate event checking on @disk if not blocked. Events in
1518 * @mask are scheduled to be cleared from the driver. Note that this
1519 * doesn't clear the events from @disk->ev.
Tejun Heo77ea8872010-12-08 20:57:37 +01001520 *
1521 * CONTEXT:
Tejun Heo85ef06d2011-07-01 16:17:47 +02001522 * If @mask is non-zero must be called with bdev->bd_mutex held.
Tejun Heo77ea8872010-12-08 20:57:37 +01001523 */
Tejun Heo85ef06d2011-07-01 16:17:47 +02001524void disk_flush_events(struct gendisk *disk, unsigned int mask)
Tejun Heo77ea8872010-12-08 20:57:37 +01001525{
Tejun Heoa9dce2a2011-06-09 20:43:54 +02001526 struct disk_events *ev = disk->ev;
Tejun Heoa9dce2a2011-06-09 20:43:54 +02001527
1528 if (!ev)
1529 return;
1530
Tejun Heo85ef06d2011-07-01 16:17:47 +02001531 spin_lock_irq(&ev->lock);
1532 ev->clearing |= mask;
Tejun Heo41f63c52012-08-03 10:30:47 -07001533 if (!ev->block)
Tejun Heo3b07e9c2012-08-20 14:51:24 -07001534 mod_delayed_work(system_freezable_wq, &ev->dwork, 0);
Tejun Heo85ef06d2011-07-01 16:17:47 +02001535 spin_unlock_irq(&ev->lock);
Tejun Heo77ea8872010-12-08 20:57:37 +01001536}
Tejun Heo77ea8872010-12-08 20:57:37 +01001537
1538/**
1539 * disk_clear_events - synchronously check, clear and return pending events
1540 * @disk: disk to fetch and clear events from
1541 * @mask: mask of events to be fetched and clearted
1542 *
1543 * Disk events are synchronously checked and pending events in @mask
1544 * are cleared and returned. This ignores the block count.
1545 *
1546 * CONTEXT:
1547 * Might sleep.
1548 */
1549unsigned int disk_clear_events(struct gendisk *disk, unsigned int mask)
1550{
1551 const struct block_device_operations *bdops = disk->fops;
1552 struct disk_events *ev = disk->ev;
1553 unsigned int pending;
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001554 unsigned int clearing = mask;
Tejun Heo77ea8872010-12-08 20:57:37 +01001555
1556 if (!ev) {
1557 /* for drivers still using the old ->media_changed method */
1558 if ((mask & DISK_EVENT_MEDIA_CHANGE) &&
1559 bdops->media_changed && bdops->media_changed(disk))
1560 return DISK_EVENT_MEDIA_CHANGE;
1561 return 0;
1562 }
1563
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001564 disk_block_events(disk);
1565
1566 /*
1567 * store the union of mask and ev->clearing on the stack so that the
1568 * race with disk_flush_events does not cause ambiguity (ev->clearing
1569 * can still be modified even if events are blocked).
1570 */
Tejun Heo77ea8872010-12-08 20:57:37 +01001571 spin_lock_irq(&ev->lock);
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001572 clearing |= ev->clearing;
1573 ev->clearing = 0;
Tejun Heo77ea8872010-12-08 20:57:37 +01001574 spin_unlock_irq(&ev->lock);
1575
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001576 disk_check_events(ev, &clearing);
Derek Basehoreaea24a82012-12-18 12:27:18 -08001577 /*
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001578 * if ev->clearing is not 0, the disk_flush_events got called in the
1579 * middle of this function, so we want to run the workfn without delay.
Derek Basehoreaea24a82012-12-18 12:27:18 -08001580 */
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001581 __disk_unblock_events(disk, ev->clearing ? true : false);
Tejun Heo77ea8872010-12-08 20:57:37 +01001582
1583 /* then, fetch and clear pending events */
1584 spin_lock_irq(&ev->lock);
Tejun Heo77ea8872010-12-08 20:57:37 +01001585 pending = ev->pending & mask;
1586 ev->pending &= ~mask;
1587 spin_unlock_irq(&ev->lock);
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001588 WARN_ON_ONCE(clearing & mask);
Tejun Heo77ea8872010-12-08 20:57:37 +01001589
1590 return pending;
1591}
1592
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001593/*
1594 * Separate this part out so that a different pointer for clearing_ptr can be
1595 * passed in for disk_clear_events.
1596 */
Tejun Heo77ea8872010-12-08 20:57:37 +01001597static void disk_events_workfn(struct work_struct *work)
1598{
1599 struct delayed_work *dwork = to_delayed_work(work);
1600 struct disk_events *ev = container_of(dwork, struct disk_events, dwork);
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001601
1602 disk_check_events(ev, &ev->clearing);
1603}
1604
1605static void disk_check_events(struct disk_events *ev,
1606 unsigned int *clearing_ptr)
1607{
Tejun Heo77ea8872010-12-08 20:57:37 +01001608 struct gendisk *disk = ev->disk;
1609 char *envp[ARRAY_SIZE(disk_uevents) + 1] = { };
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001610 unsigned int clearing = *clearing_ptr;
Tejun Heo77ea8872010-12-08 20:57:37 +01001611 unsigned int events;
1612 unsigned long intv;
1613 int nr_events = 0, i;
1614
1615 /* check events */
1616 events = disk->fops->check_events(disk, clearing);
1617
1618 /* accumulate pending events and schedule next poll if necessary */
1619 spin_lock_irq(&ev->lock);
1620
1621 events &= ~ev->pending;
1622 ev->pending |= events;
Derek Basehore12c2bdb2012-12-18 12:27:20 -08001623 *clearing_ptr &= ~clearing;
Tejun Heo77ea8872010-12-08 20:57:37 +01001624
1625 intv = disk_events_poll_jiffies(disk);
1626 if (!ev->block && intv)
Tejun Heo3b07e9c2012-08-20 14:51:24 -07001627 queue_delayed_work(system_freezable_wq, &ev->dwork, intv);
Tejun Heo77ea8872010-12-08 20:57:37 +01001628
1629 spin_unlock_irq(&ev->lock);
1630
Tejun Heo7c88a162011-04-21 19:43:58 +02001631 /*
1632 * Tell userland about new events. Only the events listed in
1633 * @disk->events are reported. Unlisted events are processed the
1634 * same internally but never get reported to userland.
1635 */
Tejun Heo77ea8872010-12-08 20:57:37 +01001636 for (i = 0; i < ARRAY_SIZE(disk_uevents); i++)
Tejun Heo7c88a162011-04-21 19:43:58 +02001637 if (events & disk->events & (1 << i))
Tejun Heo77ea8872010-12-08 20:57:37 +01001638 envp[nr_events++] = disk_uevents[i];
1639
1640 if (nr_events)
1641 kobject_uevent_env(&disk_to_dev(disk)->kobj, KOBJ_CHANGE, envp);
1642}
1643
1644/*
1645 * A disk events enabled device has the following sysfs nodes under
1646 * its /sys/block/X/ directory.
1647 *
1648 * events : list of all supported events
1649 * events_async : list of events which can be detected w/o polling
1650 * events_poll_msecs : polling interval, 0: disable, -1: system default
1651 */
1652static ssize_t __disk_events_show(unsigned int events, char *buf)
1653{
1654 const char *delim = "";
1655 ssize_t pos = 0;
1656 int i;
1657
1658 for (i = 0; i < ARRAY_SIZE(disk_events_strs); i++)
1659 if (events & (1 << i)) {
1660 pos += sprintf(buf + pos, "%s%s",
1661 delim, disk_events_strs[i]);
1662 delim = " ";
1663 }
1664 if (pos)
1665 pos += sprintf(buf + pos, "\n");
1666 return pos;
1667}
1668
1669static ssize_t disk_events_show(struct device *dev,
1670 struct device_attribute *attr, char *buf)
1671{
1672 struct gendisk *disk = dev_to_disk(dev);
1673
1674 return __disk_events_show(disk->events, buf);
1675}
1676
1677static ssize_t disk_events_async_show(struct device *dev,
1678 struct device_attribute *attr, char *buf)
1679{
1680 struct gendisk *disk = dev_to_disk(dev);
1681
1682 return __disk_events_show(disk->async_events, buf);
1683}
1684
1685static ssize_t disk_events_poll_msecs_show(struct device *dev,
1686 struct device_attribute *attr,
1687 char *buf)
1688{
1689 struct gendisk *disk = dev_to_disk(dev);
1690
1691 return sprintf(buf, "%ld\n", disk->ev->poll_msecs);
1692}
1693
1694static ssize_t disk_events_poll_msecs_store(struct device *dev,
1695 struct device_attribute *attr,
1696 const char *buf, size_t count)
1697{
1698 struct gendisk *disk = dev_to_disk(dev);
1699 long intv;
1700
1701 if (!count || !sscanf(buf, "%ld", &intv))
1702 return -EINVAL;
1703
1704 if (intv < 0 && intv != -1)
1705 return -EINVAL;
1706
Tejun Heoc3af54a2011-06-09 20:43:55 +02001707 disk_block_events(disk);
Tejun Heo77ea8872010-12-08 20:57:37 +01001708 disk->ev->poll_msecs = intv;
1709 __disk_unblock_events(disk, true);
1710
1711 return count;
1712}
1713
1714static const DEVICE_ATTR(events, S_IRUGO, disk_events_show, NULL);
1715static const DEVICE_ATTR(events_async, S_IRUGO, disk_events_async_show, NULL);
1716static const DEVICE_ATTR(events_poll_msecs, S_IRUGO|S_IWUSR,
1717 disk_events_poll_msecs_show,
1718 disk_events_poll_msecs_store);
1719
1720static const struct attribute *disk_events_attrs[] = {
1721 &dev_attr_events.attr,
1722 &dev_attr_events_async.attr,
1723 &dev_attr_events_poll_msecs.attr,
1724 NULL,
1725};
1726
1727/*
1728 * The default polling interval can be specified by the kernel
1729 * parameter block.events_dfl_poll_msecs which defaults to 0
1730 * (disable). This can also be modified runtime by writing to
1731 * /sys/module/block/events_dfl_poll_msecs.
1732 */
1733static int disk_events_set_dfl_poll_msecs(const char *val,
1734 const struct kernel_param *kp)
1735{
1736 struct disk_events *ev;
1737 int ret;
1738
1739 ret = param_set_ulong(val, kp);
1740 if (ret < 0)
1741 return ret;
1742
1743 mutex_lock(&disk_events_mutex);
1744
1745 list_for_each_entry(ev, &disk_events, node)
Tejun Heo85ef06d2011-07-01 16:17:47 +02001746 disk_flush_events(ev->disk, 0);
Tejun Heo77ea8872010-12-08 20:57:37 +01001747
1748 mutex_unlock(&disk_events_mutex);
1749
1750 return 0;
1751}
1752
1753static const struct kernel_param_ops disk_events_dfl_poll_msecs_param_ops = {
1754 .set = disk_events_set_dfl_poll_msecs,
1755 .get = param_get_ulong,
1756};
1757
1758#undef MODULE_PARAM_PREFIX
1759#define MODULE_PARAM_PREFIX "block."
1760
1761module_param_cb(events_dfl_poll_msecs, &disk_events_dfl_poll_msecs_param_ops,
1762 &disk_events_dfl_poll_msecs, 0644);
1763
1764/*
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +01001765 * disk_{alloc|add|del|release}_events - initialize and destroy disk_events.
Tejun Heo77ea8872010-12-08 20:57:37 +01001766 */
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +01001767static void disk_alloc_events(struct gendisk *disk)
Tejun Heo77ea8872010-12-08 20:57:37 +01001768{
1769 struct disk_events *ev;
1770
Tejun Heo75e3f3e2011-05-26 21:06:50 +02001771 if (!disk->fops->check_events)
Tejun Heo77ea8872010-12-08 20:57:37 +01001772 return;
1773
1774 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
1775 if (!ev) {
1776 pr_warn("%s: failed to initialize events\n", disk->disk_name);
1777 return;
1778 }
1779
Tejun Heo77ea8872010-12-08 20:57:37 +01001780 INIT_LIST_HEAD(&ev->node);
1781 ev->disk = disk;
1782 spin_lock_init(&ev->lock);
Tejun Heofdd514e2011-06-09 20:43:59 +02001783 mutex_init(&ev->block_mutex);
Tejun Heo77ea8872010-12-08 20:57:37 +01001784 ev->block = 1;
1785 ev->poll_msecs = -1;
1786 INIT_DELAYED_WORK(&ev->dwork, disk_events_workfn);
1787
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +01001788 disk->ev = ev;
1789}
1790
1791static void disk_add_events(struct gendisk *disk)
1792{
1793 if (!disk->ev)
1794 return;
1795
1796 /* FIXME: error handling */
1797 if (sysfs_create_files(&disk_to_dev(disk)->kobj, disk_events_attrs) < 0)
1798 pr_warn("%s: failed to create sysfs files for events\n",
1799 disk->disk_name);
1800
Tejun Heo77ea8872010-12-08 20:57:37 +01001801 mutex_lock(&disk_events_mutex);
Stanislaw Gruszka9f53d2fe2012-03-02 10:43:28 +01001802 list_add_tail(&disk->ev->node, &disk_events);
Tejun Heo77ea8872010-12-08 20:57:37 +01001803 mutex_unlock(&disk_events_mutex);
1804
1805 /*
1806 * Block count is initialized to 1 and the following initial
1807 * unblock kicks it into action.
1808 */
1809 __disk_unblock_events(disk, true);
1810}
1811
1812static void disk_del_events(struct gendisk *disk)
1813{
1814 if (!disk->ev)
1815 return;
1816
Tejun Heoc3af54a2011-06-09 20:43:55 +02001817 disk_block_events(disk);
Tejun Heo77ea8872010-12-08 20:57:37 +01001818
1819 mutex_lock(&disk_events_mutex);
1820 list_del_init(&disk->ev->node);
1821 mutex_unlock(&disk_events_mutex);
1822
1823 sysfs_remove_files(&disk_to_dev(disk)->kobj, disk_events_attrs);
1824}
1825
1826static void disk_release_events(struct gendisk *disk)
1827{
1828 /* the block count should be 1 from disk_del_events() */
1829 WARN_ON_ONCE(disk->ev && disk->ev->block != 1);
1830 kfree(disk->ev);
1831}