5 #include <linux/module.h>
7 #include <linux/genhd.h>
8 #include <linux/kdev_t.h>
9 #include <linux/kernel.h>
10 #include <linux/blkdev.h>
11 #include <linux/init.h>
12 #include <linux/spinlock.h>
13 #include <linux/proc_fs.h>
14 #include <linux/seq_file.h>
15 #include <linux/slab.h>
16 #include <linux/kmod.h>
17 #include <linux/kobj_map.h>
18 #include <linux/buffer_head.h>
19 #include <linux/mutex.h>
20 #include <linux/idr.h>
24 static DEFINE_MUTEX(block_class_lock
);
25 struct kobject
*block_depr
;
27 /* for extended dynamic devt allocation, currently only one major is used */
28 #define MAX_EXT_DEVT (1 << MINORBITS)
30 /* For extended devt allocation. ext_devt_mutex prevents look up
31 * results from going away underneath its user.
33 static DEFINE_MUTEX(ext_devt_mutex
);
34 static DEFINE_IDR(ext_devt_idr
);
36 static struct device_type disk_type
;
39 * disk_get_part - get partition
40 * @disk: disk to look partition from
41 * @partno: partition number
43 * Look for partition @partno from @disk. If found, increment
44 * reference count and return it.
50 * Pointer to the found partition on success, NULL if not found.
52 struct hd_struct
*disk_get_part(struct gendisk
*disk
, int partno
)
54 struct hd_struct
*part
= NULL
;
55 struct disk_part_tbl
*ptbl
;
57 if (unlikely(partno
< 0))
62 ptbl
= rcu_dereference(disk
->part_tbl
);
63 if (likely(partno
< ptbl
->len
)) {
64 part
= rcu_dereference(ptbl
->part
[partno
]);
66 get_device(part_to_dev(part
));
73 EXPORT_SYMBOL_GPL(disk_get_part
);
76 * disk_part_iter_init - initialize partition iterator
77 * @piter: iterator to initialize
78 * @disk: disk to iterate over
79 * @flags: DISK_PITER_* flags
81 * Initialize @piter so that it iterates over partitions of @disk.
86 void disk_part_iter_init(struct disk_part_iter
*piter
, struct gendisk
*disk
,
89 struct disk_part_tbl
*ptbl
;
92 ptbl
= rcu_dereference(disk
->part_tbl
);
97 if (flags
& DISK_PITER_REVERSE
)
98 piter
->idx
= ptbl
->len
- 1;
99 else if (flags
& (DISK_PITER_INCL_PART0
| DISK_PITER_INCL_EMPTY_PART0
))
104 piter
->flags
= flags
;
108 EXPORT_SYMBOL_GPL(disk_part_iter_init
);
111 * disk_part_iter_next - proceed iterator to the next partition and return it
112 * @piter: iterator of interest
114 * Proceed @piter to the next partition and return it.
119 struct hd_struct
*disk_part_iter_next(struct disk_part_iter
*piter
)
121 struct disk_part_tbl
*ptbl
;
124 /* put the last partition */
125 disk_put_part(piter
->part
);
130 ptbl
= rcu_dereference(piter
->disk
->part_tbl
);
132 /* determine iteration parameters */
133 if (piter
->flags
& DISK_PITER_REVERSE
) {
135 if (piter
->flags
& (DISK_PITER_INCL_PART0
|
136 DISK_PITER_INCL_EMPTY_PART0
))
145 /* iterate to the next partition */
146 for (; piter
->idx
!= end
; piter
->idx
+= inc
) {
147 struct hd_struct
*part
;
149 part
= rcu_dereference(ptbl
->part
[piter
->idx
]);
152 if (!part
->nr_sects
&&
153 !(piter
->flags
& DISK_PITER_INCL_EMPTY
) &&
154 !(piter
->flags
& DISK_PITER_INCL_EMPTY_PART0
&&
158 get_device(part_to_dev(part
));
168 EXPORT_SYMBOL_GPL(disk_part_iter_next
);
171 * disk_part_iter_exit - finish up partition iteration
172 * @piter: iter of interest
174 * Called when iteration is over. Cleans up @piter.
179 void disk_part_iter_exit(struct disk_part_iter
*piter
)
181 disk_put_part(piter
->part
);
184 EXPORT_SYMBOL_GPL(disk_part_iter_exit
);
186 static inline int sector_in_part(struct hd_struct
*part
, sector_t sector
)
188 return part
->start_sect
<= sector
&&
189 sector
< part
->start_sect
+ part
->nr_sects
;
193 * disk_map_sector_rcu - map sector to partition
194 * @disk: gendisk of interest
195 * @sector: sector to map
197 * Find out which partition @sector maps to on @disk. This is
198 * primarily used for stats accounting.
201 * RCU read locked. The returned partition pointer is valid only
202 * while preemption is disabled.
205 * Found partition on success, part0 is returned if no partition matches
207 struct hd_struct
*disk_map_sector_rcu(struct gendisk
*disk
, sector_t sector
)
209 struct disk_part_tbl
*ptbl
;
210 struct hd_struct
*part
;
213 ptbl
= rcu_dereference(disk
->part_tbl
);
215 part
= rcu_dereference(ptbl
->last_lookup
);
216 if (part
&& sector_in_part(part
, sector
))
219 for (i
= 1; i
< ptbl
->len
; i
++) {
220 part
= rcu_dereference(ptbl
->part
[i
]);
222 if (part
&& sector_in_part(part
, sector
)) {
223 rcu_assign_pointer(ptbl
->last_lookup
, part
);
229 EXPORT_SYMBOL_GPL(disk_map_sector_rcu
);
232 * Can be deleted altogether. Later.
235 static struct blk_major_name
{
236 struct blk_major_name
*next
;
239 } *major_names
[BLKDEV_MAJOR_HASH_SIZE
];
241 /* index in the above - for now: assume no multimajor ranges */
242 static inline int major_to_index(int major
)
244 return major
% BLKDEV_MAJOR_HASH_SIZE
;
247 #ifdef CONFIG_PROC_FS
248 void blkdev_show(struct seq_file
*seqf
, off_t offset
)
250 struct blk_major_name
*dp
;
252 if (offset
< BLKDEV_MAJOR_HASH_SIZE
) {
253 mutex_lock(&block_class_lock
);
254 for (dp
= major_names
[offset
]; dp
; dp
= dp
->next
)
255 seq_printf(seqf
, "%3d %s\n", dp
->major
, dp
->name
);
256 mutex_unlock(&block_class_lock
);
259 #endif /* CONFIG_PROC_FS */
262 * register_blkdev - register a new block device
264 * @major: the requested major device number [1..255]. If @major=0, try to
265 * allocate any unused major number.
266 * @name: the name of the new block device as a zero terminated string
268 * The @name must be unique within the system.
270 * The return value depends on the @major input parameter.
271 * - if a major device number was requested in range [1..255] then the
272 * function returns zero on success, or a negative error code
273 * - if any unused major number was requested with @major=0 parameter
274 * then the return value is the allocated major number in range
275 * [1..255] or a negative error code otherwise
277 int register_blkdev(unsigned int major
, const char *name
)
279 struct blk_major_name
**n
, *p
;
282 mutex_lock(&block_class_lock
);
286 for (index
= ARRAY_SIZE(major_names
)-1; index
> 0; index
--) {
287 if (major_names
[index
] == NULL
)
292 printk("register_blkdev: failed to get major for %s\n",
301 p
= kmalloc(sizeof(struct blk_major_name
), GFP_KERNEL
);
308 strlcpy(p
->name
, name
, sizeof(p
->name
));
310 index
= major_to_index(major
);
312 for (n
= &major_names
[index
]; *n
; n
= &(*n
)->next
) {
313 if ((*n
)->major
== major
)
322 printk("register_blkdev: cannot get major %d for %s\n",
327 mutex_unlock(&block_class_lock
);
331 EXPORT_SYMBOL(register_blkdev
);
333 void unregister_blkdev(unsigned int major
, const char *name
)
335 struct blk_major_name
**n
;
336 struct blk_major_name
*p
= NULL
;
337 int index
= major_to_index(major
);
339 mutex_lock(&block_class_lock
);
340 for (n
= &major_names
[index
]; *n
; n
= &(*n
)->next
)
341 if ((*n
)->major
== major
)
343 if (!*n
|| strcmp((*n
)->name
, name
)) {
349 mutex_unlock(&block_class_lock
);
353 EXPORT_SYMBOL(unregister_blkdev
);
355 static struct kobj_map
*bdev_map
;
358 * blk_mangle_minor - scatter minor numbers apart
359 * @minor: minor number to mangle
361 * Scatter consecutively allocated @minor number apart if MANGLE_DEVT
362 * is enabled. Mangling twice gives the original value.
370 static int blk_mangle_minor(int minor
)
372 #ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT
375 for (i
= 0; i
< MINORBITS
/ 2; i
++) {
376 int low
= minor
& (1 << i
);
377 int high
= minor
& (1 << (MINORBITS
- 1 - i
));
378 int distance
= MINORBITS
- 1 - 2 * i
;
380 minor
^= low
| high
; /* clear both bits */
381 low
<<= distance
; /* swap the positions */
383 minor
|= low
| high
; /* and set */
390 * blk_alloc_devt - allocate a dev_t for a partition
391 * @part: partition to allocate dev_t for
392 * @devt: out parameter for resulting dev_t
394 * Allocate a dev_t for block device.
397 * 0 on success, allocated dev_t is returned in *@devt. -errno on
403 int blk_alloc_devt(struct hd_struct
*part
, dev_t
*devt
)
405 struct gendisk
*disk
= part_to_disk(part
);
408 /* in consecutive minor range? */
409 if (part
->partno
< disk
->minors
) {
410 *devt
= MKDEV(disk
->major
, disk
->first_minor
+ part
->partno
);
414 /* allocate ext devt */
416 if (!idr_pre_get(&ext_devt_idr
, GFP_KERNEL
))
418 rc
= idr_get_new(&ext_devt_idr
, part
, &idx
);
419 } while (rc
== -EAGAIN
);
424 if (idx
> MAX_EXT_DEVT
) {
425 idr_remove(&ext_devt_idr
, idx
);
429 *devt
= MKDEV(BLOCK_EXT_MAJOR
, blk_mangle_minor(idx
));
434 * blk_free_devt - free a dev_t
435 * @devt: dev_t to free
437 * Free @devt which was allocated using blk_alloc_devt().
442 void blk_free_devt(dev_t devt
)
446 if (devt
== MKDEV(0, 0))
449 if (MAJOR(devt
) == BLOCK_EXT_MAJOR
) {
450 mutex_lock(&ext_devt_mutex
);
451 idr_remove(&ext_devt_idr
, blk_mangle_minor(MINOR(devt
)));
452 mutex_unlock(&ext_devt_mutex
);
456 static char *bdevt_str(dev_t devt
, char *buf
)
458 if (MAJOR(devt
) <= 0xff && MINOR(devt
) <= 0xff) {
459 char tbuf
[BDEVT_SIZE
];
460 snprintf(tbuf
, BDEVT_SIZE
, "%02x%02x", MAJOR(devt
), MINOR(devt
));
461 snprintf(buf
, BDEVT_SIZE
, "%-9s", tbuf
);
463 snprintf(buf
, BDEVT_SIZE
, "%03x:%05x", MAJOR(devt
), MINOR(devt
));
469 * Register device numbers dev..(dev+range-1)
470 * range must be nonzero
471 * The hash chain is sorted on range, so that subranges can override.
473 void blk_register_region(dev_t devt
, unsigned long range
, struct module
*module
,
474 struct kobject
*(*probe
)(dev_t
, int *, void *),
475 int (*lock
)(dev_t
, void *), void *data
)
477 kobj_map(bdev_map
, devt
, range
, module
, probe
, lock
, data
);
480 EXPORT_SYMBOL(blk_register_region
);
482 void blk_unregister_region(dev_t devt
, unsigned long range
)
484 kobj_unmap(bdev_map
, devt
, range
);
487 EXPORT_SYMBOL(blk_unregister_region
);
489 static struct kobject
*exact_match(dev_t devt
, int *partno
, void *data
)
491 struct gendisk
*p
= data
;
493 return &disk_to_dev(p
)->kobj
;
496 static int exact_lock(dev_t devt
, void *data
)
498 struct gendisk
*p
= data
;
505 void register_disk(struct gendisk
*disk
)
507 struct device
*ddev
= disk_to_dev(disk
);
508 struct block_device
*bdev
;
509 struct disk_part_iter piter
;
510 struct hd_struct
*part
;
513 ddev
->parent
= disk
->driverfs_dev
;
515 dev_set_name(ddev
, disk
->disk_name
);
517 /* delay uevents, until we scanned partition table */
518 dev_set_uevent_suppress(ddev
, 1);
520 if (device_add(ddev
))
522 if (!sysfs_deprecated
) {
523 err
= sysfs_create_link(block_depr
, &ddev
->kobj
,
524 kobject_name(&ddev
->kobj
));
530 disk
->part0
.holder_dir
= kobject_create_and_add("holders", &ddev
->kobj
);
531 disk
->slave_dir
= kobject_create_and_add("slaves", &ddev
->kobj
);
533 /* No minors to use for partitions */
534 if (!disk_partitionable(disk
))
537 /* No such device (e.g., media were just removed) */
538 if (!get_capacity(disk
))
541 bdev
= bdget_disk(disk
, 0);
545 bdev
->bd_invalidated
= 1;
546 err
= blkdev_get(bdev
, FMODE_READ
, NULL
);
549 blkdev_put(bdev
, FMODE_READ
);
552 /* announce disk after possible partitions are created */
553 dev_set_uevent_suppress(ddev
, 0);
554 kobject_uevent(&ddev
->kobj
, KOBJ_ADD
);
556 /* announce possible partitions */
557 disk_part_iter_init(&piter
, disk
, 0);
558 while ((part
= disk_part_iter_next(&piter
)))
559 kobject_uevent(&part_to_dev(part
)->kobj
, KOBJ_ADD
);
560 disk_part_iter_exit(&piter
);
564 * add_disk - add partitioning information to kernel list
565 * @disk: per-device partitioning information
567 * This function registers the partitioning information in @disk
570 * FIXME: error handling
572 void add_disk(struct gendisk
*disk
)
574 struct backing_dev_info
*bdi
;
578 /* minors == 0 indicates to use ext devt from part0 and should
579 * be accompanied with EXT_DEVT flag. Make sure all
580 * parameters make sense.
582 WARN_ON(disk
->minors
&& !(disk
->major
|| disk
->first_minor
));
583 WARN_ON(!disk
->minors
&& !(disk
->flags
& GENHD_FL_EXT_DEVT
));
585 disk
->flags
|= GENHD_FL_UP
;
587 retval
= blk_alloc_devt(&disk
->part0
, &devt
);
592 disk_to_dev(disk
)->devt
= devt
;
594 /* ->major and ->first_minor aren't supposed to be
595 * dereferenced from here on, but set them just in case.
597 disk
->major
= MAJOR(devt
);
598 disk
->first_minor
= MINOR(devt
);
600 /* Register BDI before referencing it from bdev */
601 bdi
= &disk
->queue
->backing_dev_info
;
602 bdi_register_dev(bdi
, disk_devt(disk
));
604 blk_register_region(disk_devt(disk
), disk
->minors
, NULL
,
605 exact_match
, exact_lock
, disk
);
607 blk_register_queue(disk
);
609 retval
= sysfs_create_link(&disk_to_dev(disk
)->kobj
, &bdi
->dev
->kobj
,
613 EXPORT_SYMBOL(add_disk
);
615 void del_gendisk(struct gendisk
*disk
)
617 struct disk_part_iter piter
;
618 struct hd_struct
*part
;
620 /* invalidate stuff */
621 disk_part_iter_init(&piter
, disk
,
622 DISK_PITER_INCL_EMPTY
| DISK_PITER_REVERSE
);
623 while ((part
= disk_part_iter_next(&piter
))) {
624 invalidate_partition(disk
, part
->partno
);
625 delete_partition(disk
, part
->partno
);
627 disk_part_iter_exit(&piter
);
629 invalidate_partition(disk
, 0);
630 blk_free_devt(disk_to_dev(disk
)->devt
);
631 set_capacity(disk
, 0);
632 disk
->flags
&= ~GENHD_FL_UP
;
634 sysfs_remove_link(&disk_to_dev(disk
)->kobj
, "bdi");
635 bdi_unregister(&disk
->queue
->backing_dev_info
);
636 blk_unregister_queue(disk
);
637 blk_unregister_region(disk_devt(disk
), disk
->minors
);
639 part_stat_set_all(&disk
->part0
, 0);
640 disk
->part0
.stamp
= 0;
642 kobject_put(disk
->part0
.holder_dir
);
643 kobject_put(disk
->slave_dir
);
644 disk
->driverfs_dev
= NULL
;
645 if (!sysfs_deprecated
)
646 sysfs_remove_link(block_depr
, dev_name(disk_to_dev(disk
)));
647 device_del(disk_to_dev(disk
));
649 EXPORT_SYMBOL(del_gendisk
);
652 * get_gendisk - get partitioning information for a given device
653 * @devt: device to get partitioning information for
654 * @partno: returned partition index
656 * This function gets the structure containing partitioning
657 * information for the given device @devt.
659 struct gendisk
*get_gendisk(dev_t devt
, int *partno
)
661 struct gendisk
*disk
= NULL
;
663 if (MAJOR(devt
) != BLOCK_EXT_MAJOR
) {
664 struct kobject
*kobj
;
666 kobj
= kobj_lookup(bdev_map
, devt
, partno
);
668 disk
= dev_to_disk(kobj_to_dev(kobj
));
670 struct hd_struct
*part
;
672 mutex_lock(&ext_devt_mutex
);
673 part
= idr_find(&ext_devt_idr
, blk_mangle_minor(MINOR(devt
)));
674 if (part
&& get_disk(part_to_disk(part
))) {
675 *partno
= part
->partno
;
676 disk
= part_to_disk(part
);
678 mutex_unlock(&ext_devt_mutex
);
683 EXPORT_SYMBOL(get_gendisk
);
686 * bdget_disk - do bdget() by gendisk and partition number
687 * @disk: gendisk of interest
688 * @partno: partition number
690 * Find partition @partno from @disk, do bdget() on it.
696 * Resulting block_device on success, NULL on failure.
698 struct block_device
*bdget_disk(struct gendisk
*disk
, int partno
)
700 struct hd_struct
*part
;
701 struct block_device
*bdev
= NULL
;
703 part
= disk_get_part(disk
, partno
);
705 bdev
= bdget(part_devt(part
));
710 EXPORT_SYMBOL(bdget_disk
);
713 * print a full list of all partitions - intended for places where the root
714 * filesystem can't be mounted and thus to give the victim some idea of what
717 void __init
printk_all_partitions(void)
719 struct class_dev_iter iter
;
722 class_dev_iter_init(&iter
, &block_class
, NULL
, &disk_type
);
723 while ((dev
= class_dev_iter_next(&iter
))) {
724 struct gendisk
*disk
= dev_to_disk(dev
);
725 struct disk_part_iter piter
;
726 struct hd_struct
*part
;
727 char name_buf
[BDEVNAME_SIZE
];
728 char devt_buf
[BDEVT_SIZE
];
729 u8 uuid
[PARTITION_META_INFO_UUIDLTH
* 2 + 1];
732 * Don't show empty devices or things that have been
735 if (get_capacity(disk
) == 0 ||
736 (disk
->flags
& GENHD_FL_SUPPRESS_PARTITION_INFO
))
740 * Note, unlike /proc/partitions, I am showing the
741 * numbers in hex - the same format as the root=
744 disk_part_iter_init(&piter
, disk
, DISK_PITER_INCL_PART0
);
745 while ((part
= disk_part_iter_next(&piter
))) {
746 bool is_part0
= part
== &disk
->part0
;
750 part_unpack_uuid(part
->info
->uuid
, uuid
);
752 printk("%s%s %10llu %s %s", is_part0
? "" : " ",
753 bdevt_str(part_devt(part
), devt_buf
),
754 (unsigned long long)part
->nr_sects
>> 1,
755 disk_name(disk
, part
->partno
, name_buf
), uuid
);
757 if (disk
->driverfs_dev
!= NULL
&&
758 disk
->driverfs_dev
->driver
!= NULL
)
759 printk(" driver: %s\n",
760 disk
->driverfs_dev
->driver
->name
);
762 printk(" (driver?)\n");
766 disk_part_iter_exit(&piter
);
768 class_dev_iter_exit(&iter
);
771 #ifdef CONFIG_PROC_FS
773 static void *disk_seqf_start(struct seq_file
*seqf
, loff_t
*pos
)
776 struct class_dev_iter
*iter
;
779 iter
= kmalloc(sizeof(*iter
), GFP_KERNEL
);
781 return ERR_PTR(-ENOMEM
);
783 seqf
->private = iter
;
784 class_dev_iter_init(iter
, &block_class
, NULL
, &disk_type
);
786 dev
= class_dev_iter_next(iter
);
791 return dev_to_disk(dev
);
794 static void *disk_seqf_next(struct seq_file
*seqf
, void *v
, loff_t
*pos
)
799 dev
= class_dev_iter_next(seqf
->private);
801 return dev_to_disk(dev
);
806 static void disk_seqf_stop(struct seq_file
*seqf
, void *v
)
808 struct class_dev_iter
*iter
= seqf
->private;
810 /* stop is called even after start failed :-( */
812 class_dev_iter_exit(iter
);
817 static void *show_partition_start(struct seq_file
*seqf
, loff_t
*pos
)
821 p
= disk_seqf_start(seqf
, pos
);
822 if (!IS_ERR(p
) && p
&& !*pos
)
823 seq_puts(seqf
, "major minor #blocks name\n\n");
827 static int show_partition(struct seq_file
*seqf
, void *v
)
829 struct gendisk
*sgp
= v
;
830 struct disk_part_iter piter
;
831 struct hd_struct
*part
;
832 char buf
[BDEVNAME_SIZE
];
834 /* Don't show non-partitionable removeable devices or empty devices */
835 if (!get_capacity(sgp
) || (!disk_partitionable(sgp
) &&
836 (sgp
->flags
& GENHD_FL_REMOVABLE
)))
838 if (sgp
->flags
& GENHD_FL_SUPPRESS_PARTITION_INFO
)
841 /* show the full disk and all non-0 size partitions of it */
842 disk_part_iter_init(&piter
, sgp
, DISK_PITER_INCL_PART0
);
843 while ((part
= disk_part_iter_next(&piter
)))
844 seq_printf(seqf
, "%4d %7d %10llu %s\n",
845 MAJOR(part_devt(part
)), MINOR(part_devt(part
)),
846 (unsigned long long)part
->nr_sects
>> 1,
847 disk_name(sgp
, part
->partno
, buf
));
848 disk_part_iter_exit(&piter
);
853 static const struct seq_operations partitions_op
= {
854 .start
= show_partition_start
,
855 .next
= disk_seqf_next
,
856 .stop
= disk_seqf_stop
,
857 .show
= show_partition
860 static int partitions_open(struct inode
*inode
, struct file
*file
)
862 return seq_open(file
, &partitions_op
);
865 static const struct file_operations proc_partitions_operations
= {
866 .open
= partitions_open
,
869 .release
= seq_release
,
874 static struct kobject
*base_probe(dev_t devt
, int *partno
, void *data
)
876 if (request_module("block-major-%d-%d", MAJOR(devt
), MINOR(devt
)) > 0)
877 /* Make old-style 2.4 aliases work */
878 request_module("block-major-%d", MAJOR(devt
));
882 static int __init
genhd_device_init(void)
886 block_class
.dev_kobj
= sysfs_dev_block_kobj
;
887 error
= class_register(&block_class
);
890 bdev_map
= kobj_map_init(base_probe
, &block_class_lock
);
893 register_blkdev(BLOCK_EXT_MAJOR
, "blkext");
895 /* create top-level block dir */
896 if (!sysfs_deprecated
)
897 block_depr
= kobject_create_and_add("block", NULL
);
901 subsys_initcall(genhd_device_init
);
903 static ssize_t
disk_range_show(struct device
*dev
,
904 struct device_attribute
*attr
, char *buf
)
906 struct gendisk
*disk
= dev_to_disk(dev
);
908 return sprintf(buf
, "%d\n", disk
->minors
);
911 static ssize_t
disk_ext_range_show(struct device
*dev
,
912 struct device_attribute
*attr
, char *buf
)
914 struct gendisk
*disk
= dev_to_disk(dev
);
916 return sprintf(buf
, "%d\n", disk_max_parts(disk
));
919 static ssize_t
disk_removable_show(struct device
*dev
,
920 struct device_attribute
*attr
, char *buf
)
922 struct gendisk
*disk
= dev_to_disk(dev
);
924 return sprintf(buf
, "%d\n",
925 (disk
->flags
& GENHD_FL_REMOVABLE
? 1 : 0));
928 static ssize_t
disk_ro_show(struct device
*dev
,
929 struct device_attribute
*attr
, char *buf
)
931 struct gendisk
*disk
= dev_to_disk(dev
);
933 return sprintf(buf
, "%d\n", get_disk_ro(disk
) ? 1 : 0);
936 static ssize_t
disk_capability_show(struct device
*dev
,
937 struct device_attribute
*attr
, char *buf
)
939 struct gendisk
*disk
= dev_to_disk(dev
);
941 return sprintf(buf
, "%x\n", disk
->flags
);
944 static ssize_t
disk_alignment_offset_show(struct device
*dev
,
945 struct device_attribute
*attr
,
948 struct gendisk
*disk
= dev_to_disk(dev
);
950 return sprintf(buf
, "%d\n", queue_alignment_offset(disk
->queue
));
953 static ssize_t
disk_discard_alignment_show(struct device
*dev
,
954 struct device_attribute
*attr
,
957 struct gendisk
*disk
= dev_to_disk(dev
);
959 return sprintf(buf
, "%d\n", queue_discard_alignment(disk
->queue
));
962 static DEVICE_ATTR(range
, S_IRUGO
, disk_range_show
, NULL
);
963 static DEVICE_ATTR(ext_range
, S_IRUGO
, disk_ext_range_show
, NULL
);
964 static DEVICE_ATTR(removable
, S_IRUGO
, disk_removable_show
, NULL
);
965 static DEVICE_ATTR(ro
, S_IRUGO
, disk_ro_show
, NULL
);
966 static DEVICE_ATTR(size
, S_IRUGO
, part_size_show
, NULL
);
967 static DEVICE_ATTR(alignment_offset
, S_IRUGO
, disk_alignment_offset_show
, NULL
);
968 static DEVICE_ATTR(discard_alignment
, S_IRUGO
, disk_discard_alignment_show
,
970 static DEVICE_ATTR(capability
, S_IRUGO
, disk_capability_show
, NULL
);
971 static DEVICE_ATTR(stat
, S_IRUGO
, part_stat_show
, NULL
);
972 static DEVICE_ATTR(inflight
, S_IRUGO
, part_inflight_show
, NULL
);
973 #ifdef CONFIG_FAIL_MAKE_REQUEST
974 static struct device_attribute dev_attr_fail
=
975 __ATTR(make
-it
-fail
, S_IRUGO
|S_IWUSR
, part_fail_show
, part_fail_store
);
977 #ifdef CONFIG_FAIL_IO_TIMEOUT
978 static struct device_attribute dev_attr_fail_timeout
=
979 __ATTR(io
-timeout
-fail
, S_IRUGO
|S_IWUSR
, part_timeout_show
,
983 static struct attribute
*disk_attrs
[] = {
984 &dev_attr_range
.attr
,
985 &dev_attr_ext_range
.attr
,
986 &dev_attr_removable
.attr
,
989 &dev_attr_alignment_offset
.attr
,
990 &dev_attr_discard_alignment
.attr
,
991 &dev_attr_capability
.attr
,
993 &dev_attr_inflight
.attr
,
994 #ifdef CONFIG_FAIL_MAKE_REQUEST
997 #ifdef CONFIG_FAIL_IO_TIMEOUT
998 &dev_attr_fail_timeout
.attr
,
1003 static struct attribute_group disk_attr_group
= {
1004 .attrs
= disk_attrs
,
1007 static const struct attribute_group
*disk_attr_groups
[] = {
1012 static void disk_free_ptbl_rcu_cb(struct rcu_head
*head
)
1014 struct disk_part_tbl
*ptbl
=
1015 container_of(head
, struct disk_part_tbl
, rcu_head
);
1021 * disk_replace_part_tbl - replace disk->part_tbl in RCU-safe way
1022 * @disk: disk to replace part_tbl for
1023 * @new_ptbl: new part_tbl to install
1025 * Replace disk->part_tbl with @new_ptbl in RCU-safe way. The
1026 * original ptbl is freed using RCU callback.
1029 * Matching bd_mutx locked.
1031 static void disk_replace_part_tbl(struct gendisk
*disk
,
1032 struct disk_part_tbl
*new_ptbl
)
1034 struct disk_part_tbl
*old_ptbl
= disk
->part_tbl
;
1036 rcu_assign_pointer(disk
->part_tbl
, new_ptbl
);
1039 rcu_assign_pointer(old_ptbl
->last_lookup
, NULL
);
1040 call_rcu(&old_ptbl
->rcu_head
, disk_free_ptbl_rcu_cb
);
1045 * disk_expand_part_tbl - expand disk->part_tbl
1046 * @disk: disk to expand part_tbl for
1047 * @partno: expand such that this partno can fit in
1049 * Expand disk->part_tbl such that @partno can fit in. disk->part_tbl
1050 * uses RCU to allow unlocked dereferencing for stats and other stuff.
1053 * Matching bd_mutex locked, might sleep.
1056 * 0 on success, -errno on failure.
1058 int disk_expand_part_tbl(struct gendisk
*disk
, int partno
)
1060 struct disk_part_tbl
*old_ptbl
= disk
->part_tbl
;
1061 struct disk_part_tbl
*new_ptbl
;
1062 int len
= old_ptbl
? old_ptbl
->len
: 0;
1063 int target
= partno
+ 1;
1067 /* disk_max_parts() is zero during initialization, ignore if so */
1068 if (disk_max_parts(disk
) && target
> disk_max_parts(disk
))
1074 size
= sizeof(*new_ptbl
) + target
* sizeof(new_ptbl
->part
[0]);
1075 new_ptbl
= kzalloc_node(size
, GFP_KERNEL
, disk
->node_id
);
1079 new_ptbl
->len
= target
;
1081 for (i
= 0; i
< len
; i
++)
1082 rcu_assign_pointer(new_ptbl
->part
[i
], old_ptbl
->part
[i
]);
1084 disk_replace_part_tbl(disk
, new_ptbl
);
1088 static void disk_release(struct device
*dev
)
1090 struct gendisk
*disk
= dev_to_disk(dev
);
1092 kfree(disk
->random
);
1093 disk_replace_part_tbl(disk
, NULL
);
1094 free_part_stats(&disk
->part0
);
1095 free_part_info(&disk
->part0
);
1098 struct class block_class
= {
1102 static char *block_devnode(struct device
*dev
, mode_t
*mode
)
1104 struct gendisk
*disk
= dev_to_disk(dev
);
1107 return disk
->devnode(disk
, mode
);
1111 static struct device_type disk_type
= {
1113 .groups
= disk_attr_groups
,
1114 .release
= disk_release
,
1115 .devnode
= block_devnode
,
1118 #ifdef CONFIG_PROC_FS
1120 * aggregate disk stat collector. Uses the same stats that the sysfs
1121 * entries do, above, but makes them available through one seq_file.
1123 * The output looks suspiciously like /proc/partitions with a bunch of
1126 static int diskstats_show(struct seq_file
*seqf
, void *v
)
1128 struct gendisk
*gp
= v
;
1129 struct disk_part_iter piter
;
1130 struct hd_struct
*hd
;
1131 char buf
[BDEVNAME_SIZE
];
1135 if (&disk_to_dev(gp)->kobj.entry == block_class.devices.next)
1136 seq_puts(seqf, "major minor name"
1137 " rio rmerge rsect ruse wio wmerge "
1138 "wsect wuse running use aveq"
1142 disk_part_iter_init(&piter
, gp
, DISK_PITER_INCL_EMPTY_PART0
);
1143 while ((hd
= disk_part_iter_next(&piter
))) {
1144 cpu
= part_stat_lock();
1145 part_round_stats(cpu
, hd
);
1147 seq_printf(seqf
, "%4d %7d %s %lu %lu %llu "
1148 "%u %lu %lu %llu %u %u %u %u\n",
1149 MAJOR(part_devt(hd
)), MINOR(part_devt(hd
)),
1150 disk_name(gp
, hd
->partno
, buf
),
1151 part_stat_read(hd
, ios
[0]),
1152 part_stat_read(hd
, merges
[0]),
1153 (unsigned long long)part_stat_read(hd
, sectors
[0]),
1154 jiffies_to_msecs(part_stat_read(hd
, ticks
[0])),
1155 part_stat_read(hd
, ios
[1]),
1156 part_stat_read(hd
, merges
[1]),
1157 (unsigned long long)part_stat_read(hd
, sectors
[1]),
1158 jiffies_to_msecs(part_stat_read(hd
, ticks
[1])),
1160 jiffies_to_msecs(part_stat_read(hd
, io_ticks
)),
1161 jiffies_to_msecs(part_stat_read(hd
, time_in_queue
))
1164 disk_part_iter_exit(&piter
);
1169 static const struct seq_operations diskstats_op
= {
1170 .start
= disk_seqf_start
,
1171 .next
= disk_seqf_next
,
1172 .stop
= disk_seqf_stop
,
1173 .show
= diskstats_show
1176 static int diskstats_open(struct inode
*inode
, struct file
*file
)
1178 return seq_open(file
, &diskstats_op
);
1181 static const struct file_operations proc_diskstats_operations
= {
1182 .open
= diskstats_open
,
1184 .llseek
= seq_lseek
,
1185 .release
= seq_release
,
1188 static int __init
proc_genhd_init(void)
1190 proc_create("diskstats", 0, NULL
, &proc_diskstats_operations
);
1191 proc_create("partitions", 0, NULL
, &proc_partitions_operations
);
1194 module_init(proc_genhd_init
);
1195 #endif /* CONFIG_PROC_FS */
1197 dev_t
blk_lookup_devt(const char *name
, int partno
)
1199 dev_t devt
= MKDEV(0, 0);
1200 struct class_dev_iter iter
;
1203 class_dev_iter_init(&iter
, &block_class
, NULL
, &disk_type
);
1204 while ((dev
= class_dev_iter_next(&iter
))) {
1205 struct gendisk
*disk
= dev_to_disk(dev
);
1206 struct hd_struct
*part
;
1208 if (strcmp(dev_name(dev
), name
))
1211 if (partno
< disk
->minors
) {
1212 /* We need to return the right devno, even
1213 * if the partition doesn't exist yet.
1215 devt
= MKDEV(MAJOR(dev
->devt
),
1216 MINOR(dev
->devt
) + partno
);
1219 part
= disk_get_part(disk
, partno
);
1221 devt
= part_devt(part
);
1222 disk_put_part(part
);
1225 disk_put_part(part
);
1227 class_dev_iter_exit(&iter
);
1230 EXPORT_SYMBOL(blk_lookup_devt
);
1232 struct gendisk
*alloc_disk(int minors
)
1234 return alloc_disk_node(minors
, -1);
1236 EXPORT_SYMBOL(alloc_disk
);
1238 struct gendisk
*alloc_disk_node(int minors
, int node_id
)
1240 struct gendisk
*disk
;
1242 disk
= kmalloc_node(sizeof(struct gendisk
),
1243 GFP_KERNEL
| __GFP_ZERO
, node_id
);
1245 if (!init_part_stats(&disk
->part0
)) {
1249 disk
->node_id
= node_id
;
1250 if (disk_expand_part_tbl(disk
, 0)) {
1251 free_part_stats(&disk
->part0
);
1255 disk
->part_tbl
->part
[0] = &disk
->part0
;
1257 disk
->minors
= minors
;
1258 rand_initialize_disk(disk
);
1259 disk_to_dev(disk
)->class = &block_class
;
1260 disk_to_dev(disk
)->type
= &disk_type
;
1261 device_initialize(disk_to_dev(disk
));
1265 EXPORT_SYMBOL(alloc_disk_node
);
1267 struct kobject
*get_disk(struct gendisk
*disk
)
1269 struct module
*owner
;
1270 struct kobject
*kobj
;
1274 owner
= disk
->fops
->owner
;
1275 if (owner
&& !try_module_get(owner
))
1277 kobj
= kobject_get(&disk_to_dev(disk
)->kobj
);
1286 EXPORT_SYMBOL(get_disk
);
1288 void put_disk(struct gendisk
*disk
)
1291 kobject_put(&disk_to_dev(disk
)->kobj
);
1294 EXPORT_SYMBOL(put_disk
);
1296 static void set_disk_ro_uevent(struct gendisk
*gd
, int ro
)
1298 char event
[] = "DISK_RO=1";
1299 char *envp
[] = { event
, NULL
};
1303 kobject_uevent_env(&disk_to_dev(gd
)->kobj
, KOBJ_CHANGE
, envp
);
1306 void set_device_ro(struct block_device
*bdev
, int flag
)
1308 bdev
->bd_part
->policy
= flag
;
1311 EXPORT_SYMBOL(set_device_ro
);
1313 void set_disk_ro(struct gendisk
*disk
, int flag
)
1315 struct disk_part_iter piter
;
1316 struct hd_struct
*part
;
1318 if (disk
->part0
.policy
!= flag
) {
1319 set_disk_ro_uevent(disk
, flag
);
1320 disk
->part0
.policy
= flag
;
1323 disk_part_iter_init(&piter
, disk
, DISK_PITER_INCL_EMPTY
);
1324 while ((part
= disk_part_iter_next(&piter
)))
1325 part
->policy
= flag
;
1326 disk_part_iter_exit(&piter
);
1329 EXPORT_SYMBOL(set_disk_ro
);
1331 int bdev_read_only(struct block_device
*bdev
)
1335 return bdev
->bd_part
->policy
;
1338 EXPORT_SYMBOL(bdev_read_only
);
1340 int invalidate_partition(struct gendisk
*disk
, int partno
)
1343 struct block_device
*bdev
= bdget_disk(disk
, partno
);
1346 res
= __invalidate_device(bdev
);
1352 EXPORT_SYMBOL(invalidate_partition
);