]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - block/genhd.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi-misc-2.6
[mirror_ubuntu-artful-kernel.git] / block / genhd.c
CommitLineData
1da177e4
LT
1/*
2 * gendisk handling
3 */
4
1da177e4
LT
5#include <linux/module.h>
6#include <linux/fs.h>
7#include <linux/genhd.h>
b446b60e 8#include <linux/kdev_t.h>
1da177e4
LT
9#include <linux/kernel.h>
10#include <linux/blkdev.h>
11#include <linux/init.h>
12#include <linux/spinlock.h>
f500975a 13#include <linux/proc_fs.h>
1da177e4
LT
14#include <linux/seq_file.h>
15#include <linux/slab.h>
16#include <linux/kmod.h>
17#include <linux/kobj_map.h>
2ef41634 18#include <linux/buffer_head.h>
58383af6 19#include <linux/mutex.h>
bcce3de1 20#include <linux/idr.h>
1da177e4 21
ff88972c
AB
22#include "blk.h"
23
edfaa7c3
KS
24static DEFINE_MUTEX(block_class_lock);
25#ifndef CONFIG_SYSFS_DEPRECATED
26struct kobject *block_depr;
27#endif
1da177e4 28
bcce3de1
TH
29/* for extended dynamic devt allocation, currently only one major is used */
30#define MAX_EXT_DEVT (1 << MINORBITS)
31
32/* For extended devt allocation. ext_devt_mutex prevents look up
33 * results from going away underneath its user.
34 */
35static DEFINE_MUTEX(ext_devt_mutex);
36static DEFINE_IDR(ext_devt_idr);
37
1826eadf
AB
38static struct device_type disk_type;
39
e71bf0d0
TH
40/**
41 * disk_get_part - get partition
42 * @disk: disk to look partition from
43 * @partno: partition number
44 *
45 * Look for partition @partno from @disk. If found, increment
46 * reference count and return it.
47 *
48 * CONTEXT:
49 * Don't care.
50 *
51 * RETURNS:
52 * Pointer to the found partition on success, NULL if not found.
53 */
54struct hd_struct *disk_get_part(struct gendisk *disk, int partno)
55{
540eed56
TH
56 struct hd_struct *part = NULL;
57 struct disk_part_tbl *ptbl;
e71bf0d0 58
540eed56 59 if (unlikely(partno < 0))
e71bf0d0 60 return NULL;
540eed56 61
e71bf0d0 62 rcu_read_lock();
540eed56
TH
63
64 ptbl = rcu_dereference(disk->part_tbl);
65 if (likely(partno < ptbl->len)) {
66 part = rcu_dereference(ptbl->part[partno]);
67 if (part)
68 get_device(part_to_dev(part));
69 }
70
e71bf0d0
TH
71 rcu_read_unlock();
72
73 return part;
74}
75EXPORT_SYMBOL_GPL(disk_get_part);
76
77/**
78 * disk_part_iter_init - initialize partition iterator
79 * @piter: iterator to initialize
80 * @disk: disk to iterate over
81 * @flags: DISK_PITER_* flags
82 *
83 * Initialize @piter so that it iterates over partitions of @disk.
84 *
85 * CONTEXT:
86 * Don't care.
87 */
88void disk_part_iter_init(struct disk_part_iter *piter, struct gendisk *disk,
89 unsigned int flags)
90{
540eed56
TH
91 struct disk_part_tbl *ptbl;
92
93 rcu_read_lock();
94 ptbl = rcu_dereference(disk->part_tbl);
95
e71bf0d0
TH
96 piter->disk = disk;
97 piter->part = NULL;
98
99 if (flags & DISK_PITER_REVERSE)
540eed56 100 piter->idx = ptbl->len - 1;
71982a40 101 else if (flags & (DISK_PITER_INCL_PART0 | DISK_PITER_INCL_EMPTY_PART0))
e71bf0d0 102 piter->idx = 0;
b5d0b9df
TH
103 else
104 piter->idx = 1;
e71bf0d0
TH
105
106 piter->flags = flags;
540eed56
TH
107
108 rcu_read_unlock();
e71bf0d0
TH
109}
110EXPORT_SYMBOL_GPL(disk_part_iter_init);
111
112/**
113 * disk_part_iter_next - proceed iterator to the next partition and return it
114 * @piter: iterator of interest
115 *
116 * Proceed @piter to the next partition and return it.
117 *
118 * CONTEXT:
119 * Don't care.
120 */
121struct hd_struct *disk_part_iter_next(struct disk_part_iter *piter)
122{
540eed56 123 struct disk_part_tbl *ptbl;
e71bf0d0
TH
124 int inc, end;
125
126 /* put the last partition */
127 disk_put_part(piter->part);
128 piter->part = NULL;
129
540eed56 130 /* get part_tbl */
e71bf0d0 131 rcu_read_lock();
540eed56 132 ptbl = rcu_dereference(piter->disk->part_tbl);
e71bf0d0
TH
133
134 /* determine iteration parameters */
135 if (piter->flags & DISK_PITER_REVERSE) {
136 inc = -1;
71982a40
TH
137 if (piter->flags & (DISK_PITER_INCL_PART0 |
138 DISK_PITER_INCL_EMPTY_PART0))
b5d0b9df
TH
139 end = -1;
140 else
141 end = 0;
e71bf0d0
TH
142 } else {
143 inc = 1;
540eed56 144 end = ptbl->len;
e71bf0d0
TH
145 }
146
147 /* iterate to the next partition */
148 for (; piter->idx != end; piter->idx += inc) {
149 struct hd_struct *part;
150
540eed56 151 part = rcu_dereference(ptbl->part[piter->idx]);
e71bf0d0
TH
152 if (!part)
153 continue;
71982a40
TH
154 if (!part->nr_sects &&
155 !(piter->flags & DISK_PITER_INCL_EMPTY) &&
156 !(piter->flags & DISK_PITER_INCL_EMPTY_PART0 &&
157 piter->idx == 0))
e71bf0d0
TH
158 continue;
159
ed9e1982 160 get_device(part_to_dev(part));
e71bf0d0
TH
161 piter->part = part;
162 piter->idx += inc;
163 break;
164 }
165
166 rcu_read_unlock();
167
168 return piter->part;
169}
170EXPORT_SYMBOL_GPL(disk_part_iter_next);
171
172/**
173 * disk_part_iter_exit - finish up partition iteration
174 * @piter: iter of interest
175 *
176 * Called when iteration is over. Cleans up @piter.
177 *
178 * CONTEXT:
179 * Don't care.
180 */
181void disk_part_iter_exit(struct disk_part_iter *piter)
182{
183 disk_put_part(piter->part);
184 piter->part = NULL;
185}
186EXPORT_SYMBOL_GPL(disk_part_iter_exit);
187
a6f23657
JA
188static inline int sector_in_part(struct hd_struct *part, sector_t sector)
189{
190 return part->start_sect <= sector &&
191 sector < part->start_sect + part->nr_sects;
192}
193
e71bf0d0
TH
194/**
195 * disk_map_sector_rcu - map sector to partition
196 * @disk: gendisk of interest
197 * @sector: sector to map
198 *
199 * Find out which partition @sector maps to on @disk. This is
200 * primarily used for stats accounting.
201 *
202 * CONTEXT:
203 * RCU read locked. The returned partition pointer is valid only
204 * while preemption is disabled.
205 *
206 * RETURNS:
074a7aca 207 * Found partition on success, part0 is returned if no partition matches
e71bf0d0
TH
208 */
209struct hd_struct *disk_map_sector_rcu(struct gendisk *disk, sector_t sector)
210{
540eed56 211 struct disk_part_tbl *ptbl;
a6f23657 212 struct hd_struct *part;
e71bf0d0
TH
213 int i;
214
540eed56
TH
215 ptbl = rcu_dereference(disk->part_tbl);
216
a6f23657
JA
217 part = rcu_dereference(ptbl->last_lookup);
218 if (part && sector_in_part(part, sector))
219 return part;
220
540eed56 221 for (i = 1; i < ptbl->len; i++) {
a6f23657 222 part = rcu_dereference(ptbl->part[i]);
e71bf0d0 223
a6f23657
JA
224 if (part && sector_in_part(part, sector)) {
225 rcu_assign_pointer(ptbl->last_lookup, part);
e71bf0d0 226 return part;
a6f23657 227 }
e71bf0d0 228 }
074a7aca 229 return &disk->part0;
e71bf0d0
TH
230}
231EXPORT_SYMBOL_GPL(disk_map_sector_rcu);
232
1da177e4
LT
233/*
234 * Can be deleted altogether. Later.
235 *
236 */
237static struct blk_major_name {
238 struct blk_major_name *next;
239 int major;
240 char name[16];
68eef3b4 241} *major_names[BLKDEV_MAJOR_HASH_SIZE];
1da177e4
LT
242
243/* index in the above - for now: assume no multimajor ranges */
244static inline int major_to_index(int major)
245{
68eef3b4 246 return major % BLKDEV_MAJOR_HASH_SIZE;
7170be5f
NH
247}
248
68eef3b4 249#ifdef CONFIG_PROC_FS
cf771cb5 250void blkdev_show(struct seq_file *seqf, off_t offset)
7170be5f 251{
68eef3b4 252 struct blk_major_name *dp;
7170be5f 253
68eef3b4 254 if (offset < BLKDEV_MAJOR_HASH_SIZE) {
edfaa7c3 255 mutex_lock(&block_class_lock);
68eef3b4 256 for (dp = major_names[offset]; dp; dp = dp->next)
cf771cb5 257 seq_printf(seqf, "%3d %s\n", dp->major, dp->name);
edfaa7c3 258 mutex_unlock(&block_class_lock);
1da177e4 259 }
1da177e4 260}
68eef3b4 261#endif /* CONFIG_PROC_FS */
1da177e4 262
9e8c0bcc
MN
263/**
264 * register_blkdev - register a new block device
265 *
266 * @major: the requested major device number [1..255]. If @major=0, try to
267 * allocate any unused major number.
268 * @name: the name of the new block device as a zero terminated string
269 *
270 * The @name must be unique within the system.
271 *
272 * The return value depends on the @major input parameter.
273 * - if a major device number was requested in range [1..255] then the
274 * function returns zero on success, or a negative error code
275 * - if any unused major number was requested with @major=0 parameter
276 * then the return value is the allocated major number in range
277 * [1..255] or a negative error code otherwise
278 */
1da177e4
LT
279int register_blkdev(unsigned int major, const char *name)
280{
281 struct blk_major_name **n, *p;
282 int index, ret = 0;
283
edfaa7c3 284 mutex_lock(&block_class_lock);
1da177e4
LT
285
286 /* temporary */
287 if (major == 0) {
288 for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) {
289 if (major_names[index] == NULL)
290 break;
291 }
292
293 if (index == 0) {
294 printk("register_blkdev: failed to get major for %s\n",
295 name);
296 ret = -EBUSY;
297 goto out;
298 }
299 major = index;
300 ret = major;
301 }
302
303 p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL);
304 if (p == NULL) {
305 ret = -ENOMEM;
306 goto out;
307 }
308
309 p->major = major;
310 strlcpy(p->name, name, sizeof(p->name));
311 p->next = NULL;
312 index = major_to_index(major);
313
314 for (n = &major_names[index]; *n; n = &(*n)->next) {
315 if ((*n)->major == major)
316 break;
317 }
318 if (!*n)
319 *n = p;
320 else
321 ret = -EBUSY;
322
323 if (ret < 0) {
324 printk("register_blkdev: cannot get major %d for %s\n",
325 major, name);
326 kfree(p);
327 }
328out:
edfaa7c3 329 mutex_unlock(&block_class_lock);
1da177e4
LT
330 return ret;
331}
332
333EXPORT_SYMBOL(register_blkdev);
334
f4480240 335void unregister_blkdev(unsigned int major, const char *name)
1da177e4
LT
336{
337 struct blk_major_name **n;
338 struct blk_major_name *p = NULL;
339 int index = major_to_index(major);
1da177e4 340
edfaa7c3 341 mutex_lock(&block_class_lock);
1da177e4
LT
342 for (n = &major_names[index]; *n; n = &(*n)->next)
343 if ((*n)->major == major)
344 break;
294462a5
AM
345 if (!*n || strcmp((*n)->name, name)) {
346 WARN_ON(1);
294462a5 347 } else {
1da177e4
LT
348 p = *n;
349 *n = p->next;
350 }
edfaa7c3 351 mutex_unlock(&block_class_lock);
1da177e4 352 kfree(p);
1da177e4
LT
353}
354
355EXPORT_SYMBOL(unregister_blkdev);
356
357static struct kobj_map *bdev_map;
358
870d6656
TH
359/**
360 * blk_mangle_minor - scatter minor numbers apart
361 * @minor: minor number to mangle
362 *
363 * Scatter consecutively allocated @minor number apart if MANGLE_DEVT
364 * is enabled. Mangling twice gives the original value.
365 *
366 * RETURNS:
367 * Mangled value.
368 *
369 * CONTEXT:
370 * Don't care.
371 */
372static int blk_mangle_minor(int minor)
373{
374#ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT
375 int i;
376
377 for (i = 0; i < MINORBITS / 2; i++) {
378 int low = minor & (1 << i);
379 int high = minor & (1 << (MINORBITS - 1 - i));
380 int distance = MINORBITS - 1 - 2 * i;
381
382 minor ^= low | high; /* clear both bits */
383 low <<= distance; /* swap the positions */
384 high >>= distance;
385 minor |= low | high; /* and set */
386 }
387#endif
388 return minor;
389}
390
bcce3de1
TH
391/**
392 * blk_alloc_devt - allocate a dev_t for a partition
393 * @part: partition to allocate dev_t for
bcce3de1
TH
394 * @devt: out parameter for resulting dev_t
395 *
396 * Allocate a dev_t for block device.
397 *
398 * RETURNS:
399 * 0 on success, allocated dev_t is returned in *@devt. -errno on
400 * failure.
401 *
402 * CONTEXT:
403 * Might sleep.
404 */
405int blk_alloc_devt(struct hd_struct *part, dev_t *devt)
406{
407 struct gendisk *disk = part_to_disk(part);
408 int idx, rc;
409
410 /* in consecutive minor range? */
411 if (part->partno < disk->minors) {
412 *devt = MKDEV(disk->major, disk->first_minor + part->partno);
413 return 0;
414 }
415
416 /* allocate ext devt */
417 do {
418 if (!idr_pre_get(&ext_devt_idr, GFP_KERNEL))
419 return -ENOMEM;
420 rc = idr_get_new(&ext_devt_idr, part, &idx);
421 } while (rc == -EAGAIN);
422
423 if (rc)
424 return rc;
425
426 if (idx > MAX_EXT_DEVT) {
427 idr_remove(&ext_devt_idr, idx);
428 return -EBUSY;
429 }
430
870d6656 431 *devt = MKDEV(BLOCK_EXT_MAJOR, blk_mangle_minor(idx));
bcce3de1
TH
432 return 0;
433}
434
435/**
436 * blk_free_devt - free a dev_t
437 * @devt: dev_t to free
438 *
439 * Free @devt which was allocated using blk_alloc_devt().
440 *
441 * CONTEXT:
442 * Might sleep.
443 */
444void blk_free_devt(dev_t devt)
445{
446 might_sleep();
447
448 if (devt == MKDEV(0, 0))
449 return;
450
451 if (MAJOR(devt) == BLOCK_EXT_MAJOR) {
452 mutex_lock(&ext_devt_mutex);
870d6656 453 idr_remove(&ext_devt_idr, blk_mangle_minor(MINOR(devt)));
bcce3de1
TH
454 mutex_unlock(&ext_devt_mutex);
455 }
456}
457
1f014290
TH
458static char *bdevt_str(dev_t devt, char *buf)
459{
460 if (MAJOR(devt) <= 0xff && MINOR(devt) <= 0xff) {
461 char tbuf[BDEVT_SIZE];
462 snprintf(tbuf, BDEVT_SIZE, "%02x%02x", MAJOR(devt), MINOR(devt));
463 snprintf(buf, BDEVT_SIZE, "%-9s", tbuf);
464 } else
465 snprintf(buf, BDEVT_SIZE, "%03x:%05x", MAJOR(devt), MINOR(devt));
466
467 return buf;
468}
469
1da177e4
LT
470/*
471 * Register device numbers dev..(dev+range-1)
472 * range must be nonzero
473 * The hash chain is sorted on range, so that subranges can override.
474 */
edfaa7c3 475void blk_register_region(dev_t devt, unsigned long range, struct module *module,
1da177e4
LT
476 struct kobject *(*probe)(dev_t, int *, void *),
477 int (*lock)(dev_t, void *), void *data)
478{
edfaa7c3 479 kobj_map(bdev_map, devt, range, module, probe, lock, data);
1da177e4
LT
480}
481
482EXPORT_SYMBOL(blk_register_region);
483
edfaa7c3 484void blk_unregister_region(dev_t devt, unsigned long range)
1da177e4 485{
edfaa7c3 486 kobj_unmap(bdev_map, devt, range);
1da177e4
LT
487}
488
489EXPORT_SYMBOL(blk_unregister_region);
490
cf771cb5 491static struct kobject *exact_match(dev_t devt, int *partno, void *data)
1da177e4
LT
492{
493 struct gendisk *p = data;
edfaa7c3 494
ed9e1982 495 return &disk_to_dev(p)->kobj;
1da177e4
LT
496}
497
edfaa7c3 498static int exact_lock(dev_t devt, void *data)
1da177e4
LT
499{
500 struct gendisk *p = data;
501
502 if (!get_disk(p))
503 return -1;
504 return 0;
505}
506
507/**
508 * add_disk - add partitioning information to kernel list
509 * @disk: per-device partitioning information
510 *
511 * This function registers the partitioning information in @disk
512 * with the kernel.
3e1a7ff8
TH
513 *
514 * FIXME: error handling
1da177e4
LT
515 */
516void add_disk(struct gendisk *disk)
517{
cf0ca9fe 518 struct backing_dev_info *bdi;
3e1a7ff8 519 dev_t devt;
6ffeea77 520 int retval;
cf0ca9fe 521
3e1a7ff8
TH
522 /* minors == 0 indicates to use ext devt from part0 and should
523 * be accompanied with EXT_DEVT flag. Make sure all
524 * parameters make sense.
525 */
526 WARN_ON(disk->minors && !(disk->major || disk->first_minor));
527 WARN_ON(!disk->minors && !(disk->flags & GENHD_FL_EXT_DEVT));
528
1da177e4 529 disk->flags |= GENHD_FL_UP;
3e1a7ff8
TH
530
531 retval = blk_alloc_devt(&disk->part0, &devt);
532 if (retval) {
533 WARN_ON(1);
534 return;
535 }
536 disk_to_dev(disk)->devt = devt;
537
538 /* ->major and ->first_minor aren't supposed to be
539 * dereferenced from here on, but set them just in case.
540 */
541 disk->major = MAJOR(devt);
542 disk->first_minor = MINOR(devt);
543
01ea5063
SJK
544 /* Register BDI before referencing it from bdev */
545 bdi = &disk->queue->backing_dev_info;
546 bdi_register_dev(bdi, disk_devt(disk));
547
f331c029
TH
548 blk_register_region(disk_devt(disk), disk->minors, NULL,
549 exact_match, exact_lock, disk);
1da177e4
LT
550 register_disk(disk);
551 blk_register_queue(disk);
cf0ca9fe 552
ed9e1982
TH
553 retval = sysfs_create_link(&disk_to_dev(disk)->kobj, &bdi->dev->kobj,
554 "bdi");
6ffeea77 555 WARN_ON(retval);
1da177e4
LT
556}
557
558EXPORT_SYMBOL(add_disk);
559EXPORT_SYMBOL(del_gendisk); /* in partitions/check.c */
560
561void unlink_gendisk(struct gendisk *disk)
562{
ed9e1982 563 sysfs_remove_link(&disk_to_dev(disk)->kobj, "bdi");
cf0ca9fe 564 bdi_unregister(&disk->queue->backing_dev_info);
1da177e4 565 blk_unregister_queue(disk);
f331c029 566 blk_unregister_region(disk_devt(disk), disk->minors);
1da177e4
LT
567}
568
1da177e4
LT
569/**
570 * get_gendisk - get partitioning information for a given device
710027a4 571 * @devt: device to get partitioning information for
496aa8a9 572 * @partno: returned partition index
1da177e4
LT
573 *
574 * This function gets the structure containing partitioning
710027a4 575 * information for the given device @devt.
1da177e4 576 */
cf771cb5 577struct gendisk *get_gendisk(dev_t devt, int *partno)
1da177e4 578{
bcce3de1
TH
579 struct gendisk *disk = NULL;
580
581 if (MAJOR(devt) != BLOCK_EXT_MAJOR) {
582 struct kobject *kobj;
583
584 kobj = kobj_lookup(bdev_map, devt, partno);
585 if (kobj)
586 disk = dev_to_disk(kobj_to_dev(kobj));
587 } else {
588 struct hd_struct *part;
589
590 mutex_lock(&ext_devt_mutex);
870d6656 591 part = idr_find(&ext_devt_idr, blk_mangle_minor(MINOR(devt)));
bcce3de1
TH
592 if (part && get_disk(part_to_disk(part))) {
593 *partno = part->partno;
594 disk = part_to_disk(part);
595 }
596 mutex_unlock(&ext_devt_mutex);
597 }
edfaa7c3 598
bcce3de1 599 return disk;
1da177e4 600}
b6ac23af 601EXPORT_SYMBOL(get_gendisk);
1da177e4 602
f331c029
TH
603/**
604 * bdget_disk - do bdget() by gendisk and partition number
605 * @disk: gendisk of interest
606 * @partno: partition number
607 *
608 * Find partition @partno from @disk, do bdget() on it.
609 *
610 * CONTEXT:
611 * Don't care.
612 *
613 * RETURNS:
614 * Resulting block_device on success, NULL on failure.
615 */
aeb3d3a8 616struct block_device *bdget_disk(struct gendisk *disk, int partno)
f331c029 617{
548b10eb
TH
618 struct hd_struct *part;
619 struct block_device *bdev = NULL;
f331c029 620
548b10eb 621 part = disk_get_part(disk, partno);
2bbedcb4 622 if (part)
548b10eb
TH
623 bdev = bdget(part_devt(part));
624 disk_put_part(part);
f331c029 625
548b10eb 626 return bdev;
f331c029
TH
627}
628EXPORT_SYMBOL(bdget_disk);
629
5c6f35c5
GKH
630/*
631 * print a full list of all partitions - intended for places where the root
632 * filesystem can't be mounted and thus to give the victim some idea of what
633 * went wrong
634 */
635void __init printk_all_partitions(void)
636{
def4e38d
TH
637 struct class_dev_iter iter;
638 struct device *dev;
639
640 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
641 while ((dev = class_dev_iter_next(&iter))) {
642 struct gendisk *disk = dev_to_disk(dev);
e71bf0d0
TH
643 struct disk_part_iter piter;
644 struct hd_struct *part;
1f014290
TH
645 char name_buf[BDEVNAME_SIZE];
646 char devt_buf[BDEVT_SIZE];
b5af921e 647 u8 uuid[PARTITION_META_INFO_UUIDLTH * 2 + 1];
def4e38d
TH
648
649 /*
650 * Don't show empty devices or things that have been
651 * surpressed
652 */
653 if (get_capacity(disk) == 0 ||
654 (disk->flags & GENHD_FL_SUPPRESS_PARTITION_INFO))
655 continue;
656
657 /*
658 * Note, unlike /proc/partitions, I am showing the
659 * numbers in hex - the same format as the root=
660 * option takes.
661 */
074a7aca
TH
662 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
663 while ((part = disk_part_iter_next(&piter))) {
664 bool is_part0 = part == &disk->part0;
def4e38d 665
b5af921e
WD
666 uuid[0] = 0;
667 if (part->info)
668 part_unpack_uuid(part->info->uuid, uuid);
669
670 printk("%s%s %10llu %s %s", is_part0 ? "" : " ",
1f014290 671 bdevt_str(part_devt(part), devt_buf),
f331c029 672 (unsigned long long)part->nr_sects >> 1,
b5af921e 673 disk_name(disk, part->partno, name_buf), uuid);
074a7aca
TH
674 if (is_part0) {
675 if (disk->driverfs_dev != NULL &&
676 disk->driverfs_dev->driver != NULL)
677 printk(" driver: %s\n",
678 disk->driverfs_dev->driver->name);
679 else
680 printk(" (driver?)\n");
681 } else
682 printk("\n");
683 }
e71bf0d0 684 disk_part_iter_exit(&piter);
def4e38d
TH
685 }
686 class_dev_iter_exit(&iter);
dd2a345f
DG
687}
688
1da177e4
LT
689#ifdef CONFIG_PROC_FS
690/* iterator */
def4e38d 691static void *disk_seqf_start(struct seq_file *seqf, loff_t *pos)
68c4d4a7 692{
def4e38d
TH
693 loff_t skip = *pos;
694 struct class_dev_iter *iter;
695 struct device *dev;
68c4d4a7 696
aeb3d3a8 697 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
def4e38d
TH
698 if (!iter)
699 return ERR_PTR(-ENOMEM);
700
701 seqf->private = iter;
702 class_dev_iter_init(iter, &block_class, NULL, &disk_type);
703 do {
704 dev = class_dev_iter_next(iter);
705 if (!dev)
706 return NULL;
707 } while (skip--);
708
709 return dev_to_disk(dev);
68c4d4a7
GKH
710}
711
def4e38d 712static void *disk_seqf_next(struct seq_file *seqf, void *v, loff_t *pos)
1da177e4 713{
edfaa7c3 714 struct device *dev;
1da177e4 715
def4e38d
TH
716 (*pos)++;
717 dev = class_dev_iter_next(seqf->private);
2ac3cee5 718 if (dev)
68c4d4a7 719 return dev_to_disk(dev);
2ac3cee5 720
1da177e4
LT
721 return NULL;
722}
723
def4e38d 724static void disk_seqf_stop(struct seq_file *seqf, void *v)
27f30251 725{
def4e38d 726 struct class_dev_iter *iter = seqf->private;
27f30251 727
def4e38d
TH
728 /* stop is called even after start failed :-( */
729 if (iter) {
730 class_dev_iter_exit(iter);
731 kfree(iter);
5c0ef6d0 732 }
1da177e4
LT
733}
734
def4e38d 735static void *show_partition_start(struct seq_file *seqf, loff_t *pos)
1da177e4 736{
def4e38d
TH
737 static void *p;
738
739 p = disk_seqf_start(seqf, pos);
243294da 740 if (!IS_ERR(p) && p && !*pos)
def4e38d
TH
741 seq_puts(seqf, "major minor #blocks name\n\n");
742 return p;
1da177e4
LT
743}
744
cf771cb5 745static int show_partition(struct seq_file *seqf, void *v)
1da177e4
LT
746{
747 struct gendisk *sgp = v;
e71bf0d0
TH
748 struct disk_part_iter piter;
749 struct hd_struct *part;
1da177e4
LT
750 char buf[BDEVNAME_SIZE];
751
1da177e4 752 /* Don't show non-partitionable removeable devices or empty devices */
b5d0b9df 753 if (!get_capacity(sgp) || (!disk_partitionable(sgp) &&
f331c029 754 (sgp->flags & GENHD_FL_REMOVABLE)))
1da177e4
LT
755 return 0;
756 if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)
757 return 0;
758
759 /* show the full disk and all non-0 size partitions of it */
074a7aca 760 disk_part_iter_init(&piter, sgp, DISK_PITER_INCL_PART0);
e71bf0d0 761 while ((part = disk_part_iter_next(&piter)))
1f014290 762 seq_printf(seqf, "%4d %7d %10llu %s\n",
f331c029
TH
763 MAJOR(part_devt(part)), MINOR(part_devt(part)),
764 (unsigned long long)part->nr_sects >> 1,
765 disk_name(sgp, part->partno, buf));
e71bf0d0 766 disk_part_iter_exit(&piter);
1da177e4
LT
767
768 return 0;
769}
770
f500975a 771static const struct seq_operations partitions_op = {
def4e38d
TH
772 .start = show_partition_start,
773 .next = disk_seqf_next,
774 .stop = disk_seqf_stop,
edfaa7c3 775 .show = show_partition
1da177e4 776};
f500975a
AD
777
778static int partitions_open(struct inode *inode, struct file *file)
779{
780 return seq_open(file, &partitions_op);
781}
782
783static const struct file_operations proc_partitions_operations = {
784 .open = partitions_open,
785 .read = seq_read,
786 .llseek = seq_lseek,
787 .release = seq_release,
788};
1da177e4
LT
789#endif
790
791
cf771cb5 792static struct kobject *base_probe(dev_t devt, int *partno, void *data)
1da177e4 793{
edfaa7c3 794 if (request_module("block-major-%d-%d", MAJOR(devt), MINOR(devt)) > 0)
1da177e4 795 /* Make old-style 2.4 aliases work */
edfaa7c3 796 request_module("block-major-%d", MAJOR(devt));
1da177e4
LT
797 return NULL;
798}
799
800static int __init genhd_device_init(void)
801{
e105b8bf
DW
802 int error;
803
804 block_class.dev_kobj = sysfs_dev_block_kobj;
805 error = class_register(&block_class);
ee27a558
RM
806 if (unlikely(error))
807 return error;
edfaa7c3 808 bdev_map = kobj_map_init(base_probe, &block_class_lock);
1da177e4 809 blk_dev_init();
edfaa7c3 810
561ec68e
ZY
811 register_blkdev(BLOCK_EXT_MAJOR, "blkext");
812
edfaa7c3
KS
813#ifndef CONFIG_SYSFS_DEPRECATED
814 /* create top-level block dir */
815 block_depr = kobject_create_and_add("block", NULL);
816#endif
830d3cfb 817 return 0;
1da177e4
LT
818}
819
820subsys_initcall(genhd_device_init);
821
edfaa7c3
KS
822static ssize_t disk_range_show(struct device *dev,
823 struct device_attribute *attr, char *buf)
1da177e4 824{
edfaa7c3 825 struct gendisk *disk = dev_to_disk(dev);
1da177e4 826
edfaa7c3 827 return sprintf(buf, "%d\n", disk->minors);
1da177e4
LT
828}
829
1f014290
TH
830static ssize_t disk_ext_range_show(struct device *dev,
831 struct device_attribute *attr, char *buf)
832{
833 struct gendisk *disk = dev_to_disk(dev);
834
b5d0b9df 835 return sprintf(buf, "%d\n", disk_max_parts(disk));
1f014290
TH
836}
837
edfaa7c3
KS
838static ssize_t disk_removable_show(struct device *dev,
839 struct device_attribute *attr, char *buf)
a7fd6706 840{
edfaa7c3 841 struct gendisk *disk = dev_to_disk(dev);
a7fd6706 842
edfaa7c3
KS
843 return sprintf(buf, "%d\n",
844 (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
a7fd6706
KS
845}
846
1c9ce527
KS
847static ssize_t disk_ro_show(struct device *dev,
848 struct device_attribute *attr, char *buf)
849{
850 struct gendisk *disk = dev_to_disk(dev);
851
b7db9956 852 return sprintf(buf, "%d\n", get_disk_ro(disk) ? 1 : 0);
1c9ce527
KS
853}
854
edfaa7c3
KS
855static ssize_t disk_capability_show(struct device *dev,
856 struct device_attribute *attr, char *buf)
86ce18d7 857{
edfaa7c3
KS
858 struct gendisk *disk = dev_to_disk(dev);
859
860 return sprintf(buf, "%x\n", disk->flags);
86ce18d7 861}
edfaa7c3 862
c72758f3
MP
863static ssize_t disk_alignment_offset_show(struct device *dev,
864 struct device_attribute *attr,
865 char *buf)
866{
867 struct gendisk *disk = dev_to_disk(dev);
868
869 return sprintf(buf, "%d\n", queue_alignment_offset(disk->queue));
870}
871
86b37281
MP
872static ssize_t disk_discard_alignment_show(struct device *dev,
873 struct device_attribute *attr,
874 char *buf)
875{
876 struct gendisk *disk = dev_to_disk(dev);
877
dd3d145d 878 return sprintf(buf, "%d\n", queue_discard_alignment(disk->queue));
86b37281
MP
879}
880
edfaa7c3 881static DEVICE_ATTR(range, S_IRUGO, disk_range_show, NULL);
1f014290 882static DEVICE_ATTR(ext_range, S_IRUGO, disk_ext_range_show, NULL);
edfaa7c3 883static DEVICE_ATTR(removable, S_IRUGO, disk_removable_show, NULL);
1c9ce527 884static DEVICE_ATTR(ro, S_IRUGO, disk_ro_show, NULL);
e5610521 885static DEVICE_ATTR(size, S_IRUGO, part_size_show, NULL);
c72758f3 886static DEVICE_ATTR(alignment_offset, S_IRUGO, disk_alignment_offset_show, NULL);
86b37281
MP
887static DEVICE_ATTR(discard_alignment, S_IRUGO, disk_discard_alignment_show,
888 NULL);
edfaa7c3 889static DEVICE_ATTR(capability, S_IRUGO, disk_capability_show, NULL);
074a7aca 890static DEVICE_ATTR(stat, S_IRUGO, part_stat_show, NULL);
316d315b 891static DEVICE_ATTR(inflight, S_IRUGO, part_inflight_show, NULL);
c17bb495 892#ifdef CONFIG_FAIL_MAKE_REQUEST
edfaa7c3 893static struct device_attribute dev_attr_fail =
eddb2e26 894 __ATTR(make-it-fail, S_IRUGO|S_IWUSR, part_fail_show, part_fail_store);
c17bb495 895#endif
581d4e28
JA
896#ifdef CONFIG_FAIL_IO_TIMEOUT
897static struct device_attribute dev_attr_fail_timeout =
898 __ATTR(io-timeout-fail, S_IRUGO|S_IWUSR, part_timeout_show,
899 part_timeout_store);
900#endif
edfaa7c3
KS
901
902static struct attribute *disk_attrs[] = {
903 &dev_attr_range.attr,
1f014290 904 &dev_attr_ext_range.attr,
edfaa7c3 905 &dev_attr_removable.attr,
1c9ce527 906 &dev_attr_ro.attr,
edfaa7c3 907 &dev_attr_size.attr,
c72758f3 908 &dev_attr_alignment_offset.attr,
86b37281 909 &dev_attr_discard_alignment.attr,
edfaa7c3
KS
910 &dev_attr_capability.attr,
911 &dev_attr_stat.attr,
316d315b 912 &dev_attr_inflight.attr,
edfaa7c3
KS
913#ifdef CONFIG_FAIL_MAKE_REQUEST
914 &dev_attr_fail.attr,
581d4e28
JA
915#endif
916#ifdef CONFIG_FAIL_IO_TIMEOUT
917 &dev_attr_fail_timeout.attr,
edfaa7c3
KS
918#endif
919 NULL
920};
921
922static struct attribute_group disk_attr_group = {
923 .attrs = disk_attrs,
924};
925
a4dbd674 926static const struct attribute_group *disk_attr_groups[] = {
edfaa7c3
KS
927 &disk_attr_group,
928 NULL
1da177e4
LT
929};
930
540eed56
TH
931static void disk_free_ptbl_rcu_cb(struct rcu_head *head)
932{
933 struct disk_part_tbl *ptbl =
934 container_of(head, struct disk_part_tbl, rcu_head);
7681bfee
YI
935 struct gendisk *disk = ptbl->disk;
936 struct request_queue *q = disk->queue;
937 unsigned long flags;
540eed56
TH
938
939 kfree(ptbl);
7681bfee
YI
940
941 spin_lock_irqsave(q->queue_lock, flags);
942 elv_quiesce_end(q);
943 spin_unlock_irqrestore(q->queue_lock, flags);
540eed56
TH
944}
945
946/**
947 * disk_replace_part_tbl - replace disk->part_tbl in RCU-safe way
948 * @disk: disk to replace part_tbl for
949 * @new_ptbl: new part_tbl to install
950 *
951 * Replace disk->part_tbl with @new_ptbl in RCU-safe way. The
952 * original ptbl is freed using RCU callback.
953 *
954 * LOCKING:
955 * Matching bd_mutx locked.
956 */
957static void disk_replace_part_tbl(struct gendisk *disk,
958 struct disk_part_tbl *new_ptbl)
959{
960 struct disk_part_tbl *old_ptbl = disk->part_tbl;
7681bfee 961 struct request_queue *q = disk->queue;
540eed56
TH
962
963 rcu_assign_pointer(disk->part_tbl, new_ptbl);
a6f23657
JA
964
965 if (old_ptbl) {
966 rcu_assign_pointer(old_ptbl->last_lookup, NULL);
7681bfee
YI
967
968 spin_lock_irq(q->queue_lock);
969 elv_quiesce_start(q);
970 spin_unlock_irq(q->queue_lock);
971
540eed56 972 call_rcu(&old_ptbl->rcu_head, disk_free_ptbl_rcu_cb);
a6f23657 973 }
540eed56
TH
974}
975
976/**
977 * disk_expand_part_tbl - expand disk->part_tbl
978 * @disk: disk to expand part_tbl for
979 * @partno: expand such that this partno can fit in
980 *
981 * Expand disk->part_tbl such that @partno can fit in. disk->part_tbl
982 * uses RCU to allow unlocked dereferencing for stats and other stuff.
983 *
984 * LOCKING:
985 * Matching bd_mutex locked, might sleep.
986 *
987 * RETURNS:
988 * 0 on success, -errno on failure.
989 */
990int disk_expand_part_tbl(struct gendisk *disk, int partno)
991{
992 struct disk_part_tbl *old_ptbl = disk->part_tbl;
993 struct disk_part_tbl *new_ptbl;
994 int len = old_ptbl ? old_ptbl->len : 0;
995 int target = partno + 1;
996 size_t size;
997 int i;
998
999 /* disk_max_parts() is zero during initialization, ignore if so */
1000 if (disk_max_parts(disk) && target > disk_max_parts(disk))
1001 return -EINVAL;
1002
1003 if (target <= len)
1004 return 0;
1005
1006 size = sizeof(*new_ptbl) + target * sizeof(new_ptbl->part[0]);
1007 new_ptbl = kzalloc_node(size, GFP_KERNEL, disk->node_id);
1008 if (!new_ptbl)
1009 return -ENOMEM;
1010
540eed56 1011 new_ptbl->len = target;
7681bfee 1012 new_ptbl->disk = disk;
540eed56
TH
1013
1014 for (i = 0; i < len; i++)
1015 rcu_assign_pointer(new_ptbl->part[i], old_ptbl->part[i]);
1016
1017 disk_replace_part_tbl(disk, new_ptbl);
1018 return 0;
1019}
1020
edfaa7c3 1021static void disk_release(struct device *dev)
1da177e4 1022{
edfaa7c3
KS
1023 struct gendisk *disk = dev_to_disk(dev);
1024
1da177e4 1025 kfree(disk->random);
540eed56 1026 disk_replace_part_tbl(disk, NULL);
074a7aca 1027 free_part_stats(&disk->part0);
6d1d8050 1028 free_part_info(&disk->part0);
1da177e4
LT
1029 kfree(disk);
1030}
edfaa7c3
KS
1031struct class block_class = {
1032 .name = "block",
1da177e4
LT
1033};
1034
e454cea2 1035static char *block_devnode(struct device *dev, mode_t *mode)
b03f38b6
KS
1036{
1037 struct gendisk *disk = dev_to_disk(dev);
1038
e454cea2
KS
1039 if (disk->devnode)
1040 return disk->devnode(disk, mode);
b03f38b6
KS
1041 return NULL;
1042}
1043
1826eadf 1044static struct device_type disk_type = {
edfaa7c3
KS
1045 .name = "disk",
1046 .groups = disk_attr_groups,
1047 .release = disk_release,
e454cea2 1048 .devnode = block_devnode,
1da177e4
LT
1049};
1050
a6e2ba88 1051#ifdef CONFIG_PROC_FS
cf771cb5
TH
1052/*
1053 * aggregate disk stat collector. Uses the same stats that the sysfs
1054 * entries do, above, but makes them available through one seq_file.
1055 *
1056 * The output looks suspiciously like /proc/partitions with a bunch of
1057 * extra fields.
1058 */
1059static int diskstats_show(struct seq_file *seqf, void *v)
1da177e4
LT
1060{
1061 struct gendisk *gp = v;
e71bf0d0
TH
1062 struct disk_part_iter piter;
1063 struct hd_struct *hd;
1da177e4 1064 char buf[BDEVNAME_SIZE];
c9959059 1065 int cpu;
1da177e4
LT
1066
1067 /*
ed9e1982 1068 if (&disk_to_dev(gp)->kobj.entry == block_class.devices.next)
cf771cb5 1069 seq_puts(seqf, "major minor name"
1da177e4
LT
1070 " rio rmerge rsect ruse wio wmerge "
1071 "wsect wuse running use aveq"
1072 "\n\n");
1073 */
1074
71982a40 1075 disk_part_iter_init(&piter, gp, DISK_PITER_INCL_EMPTY_PART0);
e71bf0d0 1076 while ((hd = disk_part_iter_next(&piter))) {
074a7aca 1077 cpu = part_stat_lock();
c9959059 1078 part_round_stats(cpu, hd);
074a7aca 1079 part_stat_unlock();
1f014290 1080 seq_printf(seqf, "%4d %7d %s %lu %lu %llu "
28f39d55 1081 "%u %lu %lu %llu %u %u %u %u\n",
f331c029
TH
1082 MAJOR(part_devt(hd)), MINOR(part_devt(hd)),
1083 disk_name(gp, hd->partno, buf),
28f39d55
JM
1084 part_stat_read(hd, ios[0]),
1085 part_stat_read(hd, merges[0]),
1086 (unsigned long long)part_stat_read(hd, sectors[0]),
1087 jiffies_to_msecs(part_stat_read(hd, ticks[0])),
1088 part_stat_read(hd, ios[1]),
1089 part_stat_read(hd, merges[1]),
1090 (unsigned long long)part_stat_read(hd, sectors[1]),
1091 jiffies_to_msecs(part_stat_read(hd, ticks[1])),
316d315b 1092 part_in_flight(hd),
28f39d55
JM
1093 jiffies_to_msecs(part_stat_read(hd, io_ticks)),
1094 jiffies_to_msecs(part_stat_read(hd, time_in_queue))
1095 );
1da177e4 1096 }
e71bf0d0 1097 disk_part_iter_exit(&piter);
1da177e4
LT
1098
1099 return 0;
1100}
1101
31d85ab2 1102static const struct seq_operations diskstats_op = {
def4e38d
TH
1103 .start = disk_seqf_start,
1104 .next = disk_seqf_next,
1105 .stop = disk_seqf_stop,
1da177e4
LT
1106 .show = diskstats_show
1107};
f500975a 1108
31d85ab2
AD
1109static int diskstats_open(struct inode *inode, struct file *file)
1110{
1111 return seq_open(file, &diskstats_op);
1112}
1113
1114static const struct file_operations proc_diskstats_operations = {
1115 .open = diskstats_open,
1116 .read = seq_read,
1117 .llseek = seq_lseek,
1118 .release = seq_release,
1119};
1120
f500975a
AD
1121static int __init proc_genhd_init(void)
1122{
31d85ab2 1123 proc_create("diskstats", 0, NULL, &proc_diskstats_operations);
f500975a
AD
1124 proc_create("partitions", 0, NULL, &proc_partitions_operations);
1125 return 0;
1126}
1127module_init(proc_genhd_init);
a6e2ba88 1128#endif /* CONFIG_PROC_FS */
1da177e4 1129
8ce7ad7b
KCA
1130static void media_change_notify_thread(struct work_struct *work)
1131{
1132 struct gendisk *gd = container_of(work, struct gendisk, async_notify);
1133 char event[] = "MEDIA_CHANGE=1";
1134 char *envp[] = { event, NULL };
1135
1136 /*
1137 * set enviroment vars to indicate which event this is for
1138 * so that user space will know to go check the media status.
1139 */
ed9e1982 1140 kobject_uevent_env(&disk_to_dev(gd)->kobj, KOBJ_CHANGE, envp);
8ce7ad7b
KCA
1141 put_device(gd->driverfs_dev);
1142}
1143
1826eadf 1144#if 0
8ce7ad7b
KCA
1145void genhd_media_change_notify(struct gendisk *disk)
1146{
1147 get_device(disk->driverfs_dev);
1148 schedule_work(&disk->async_notify);
1149}
1150EXPORT_SYMBOL_GPL(genhd_media_change_notify);
1826eadf 1151#endif /* 0 */
8ce7ad7b 1152
cf771cb5 1153dev_t blk_lookup_devt(const char *name, int partno)
a142be85 1154{
def4e38d
TH
1155 dev_t devt = MKDEV(0, 0);
1156 struct class_dev_iter iter;
1157 struct device *dev;
a142be85 1158
def4e38d
TH
1159 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
1160 while ((dev = class_dev_iter_next(&iter))) {
a142be85 1161 struct gendisk *disk = dev_to_disk(dev);
548b10eb 1162 struct hd_struct *part;
a142be85 1163
3ada8b7e 1164 if (strcmp(dev_name(dev), name))
f331c029 1165 continue;
f331c029 1166
41b8c853
NB
1167 if (partno < disk->minors) {
1168 /* We need to return the right devno, even
1169 * if the partition doesn't exist yet.
1170 */
1171 devt = MKDEV(MAJOR(dev->devt),
1172 MINOR(dev->devt) + partno);
1173 break;
1174 }
548b10eb 1175 part = disk_get_part(disk, partno);
2bbedcb4 1176 if (part) {
f331c029 1177 devt = part_devt(part);
e71bf0d0 1178 disk_put_part(part);
548b10eb 1179 break;
def4e38d 1180 }
548b10eb 1181 disk_put_part(part);
5c0ef6d0 1182 }
def4e38d 1183 class_dev_iter_exit(&iter);
edfaa7c3
KS
1184 return devt;
1185}
edfaa7c3
KS
1186EXPORT_SYMBOL(blk_lookup_devt);
1187
1da177e4
LT
1188struct gendisk *alloc_disk(int minors)
1189{
1946089a
CL
1190 return alloc_disk_node(minors, -1);
1191}
689d6fac 1192EXPORT_SYMBOL(alloc_disk);
1946089a
CL
1193
1194struct gendisk *alloc_disk_node(int minors, int node_id)
1195{
1196 struct gendisk *disk;
1197
94f6030c
CL
1198 disk = kmalloc_node(sizeof(struct gendisk),
1199 GFP_KERNEL | __GFP_ZERO, node_id);
1da177e4 1200 if (disk) {
074a7aca 1201 if (!init_part_stats(&disk->part0)) {
1da177e4
LT
1202 kfree(disk);
1203 return NULL;
1204 }
bf91db18 1205 disk->node_id = node_id;
540eed56
TH
1206 if (disk_expand_part_tbl(disk, 0)) {
1207 free_part_stats(&disk->part0);
b5d0b9df
TH
1208 kfree(disk);
1209 return NULL;
1da177e4 1210 }
540eed56 1211 disk->part_tbl->part[0] = &disk->part0;
b5d0b9df 1212
1da177e4 1213 disk->minors = minors;
1da177e4 1214 rand_initialize_disk(disk);
ed9e1982
TH
1215 disk_to_dev(disk)->class = &block_class;
1216 disk_to_dev(disk)->type = &disk_type;
1217 device_initialize(disk_to_dev(disk));
8ce7ad7b
KCA
1218 INIT_WORK(&disk->async_notify,
1219 media_change_notify_thread);
1da177e4
LT
1220 }
1221 return disk;
1222}
1946089a 1223EXPORT_SYMBOL(alloc_disk_node);
1da177e4
LT
1224
1225struct kobject *get_disk(struct gendisk *disk)
1226{
1227 struct module *owner;
1228 struct kobject *kobj;
1229
1230 if (!disk->fops)
1231 return NULL;
1232 owner = disk->fops->owner;
1233 if (owner && !try_module_get(owner))
1234 return NULL;
ed9e1982 1235 kobj = kobject_get(&disk_to_dev(disk)->kobj);
1da177e4
LT
1236 if (kobj == NULL) {
1237 module_put(owner);
1238 return NULL;
1239 }
1240 return kobj;
1241
1242}
1243
1244EXPORT_SYMBOL(get_disk);
1245
1246void put_disk(struct gendisk *disk)
1247{
1248 if (disk)
ed9e1982 1249 kobject_put(&disk_to_dev(disk)->kobj);
1da177e4
LT
1250}
1251
1252EXPORT_SYMBOL(put_disk);
1253
e3264a4d
HR
1254static void set_disk_ro_uevent(struct gendisk *gd, int ro)
1255{
1256 char event[] = "DISK_RO=1";
1257 char *envp[] = { event, NULL };
1258
1259 if (!ro)
1260 event[8] = '0';
1261 kobject_uevent_env(&disk_to_dev(gd)->kobj, KOBJ_CHANGE, envp);
1262}
1263
1da177e4
LT
1264void set_device_ro(struct block_device *bdev, int flag)
1265{
b7db9956 1266 bdev->bd_part->policy = flag;
1da177e4
LT
1267}
1268
1269EXPORT_SYMBOL(set_device_ro);
1270
1271void set_disk_ro(struct gendisk *disk, int flag)
1272{
e71bf0d0
TH
1273 struct disk_part_iter piter;
1274 struct hd_struct *part;
1275
e3264a4d
HR
1276 if (disk->part0.policy != flag) {
1277 set_disk_ro_uevent(disk, flag);
1278 disk->part0.policy = flag;
1279 }
1280
1281 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_EMPTY);
e71bf0d0
TH
1282 while ((part = disk_part_iter_next(&piter)))
1283 part->policy = flag;
1284 disk_part_iter_exit(&piter);
1da177e4
LT
1285}
1286
1287EXPORT_SYMBOL(set_disk_ro);
1288
1289int bdev_read_only(struct block_device *bdev)
1290{
1291 if (!bdev)
1292 return 0;
b7db9956 1293 return bdev->bd_part->policy;
1da177e4
LT
1294}
1295
1296EXPORT_SYMBOL(bdev_read_only);
1297
cf771cb5 1298int invalidate_partition(struct gendisk *disk, int partno)
1da177e4
LT
1299{
1300 int res = 0;
cf771cb5 1301 struct block_device *bdev = bdget_disk(disk, partno);
1da177e4 1302 if (bdev) {
2ef41634
CH
1303 fsync_bdev(bdev);
1304 res = __invalidate_device(bdev);
1da177e4
LT
1305 bdput(bdev);
1306 }
1307 return res;
1308}
1309
1310EXPORT_SYMBOL(invalidate_partition);