netfilter: ipvs: do not create conn for ABORT packet in sctp_conn_schedule
[linux-2.6/btrfs-unstable.git] / block / genhd.c
blob51c1d407d93cc79ad545bf9f664e845e4b147c59
1 /*
2 * gendisk handling
3 */
5 #include <linux/module.h>
6 #include <linux/fs.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/backing-dev.h>
12 #include <linux/init.h>
13 #include <linux/spinlock.h>
14 #include <linux/proc_fs.h>
15 #include <linux/seq_file.h>
16 #include <linux/slab.h>
17 #include <linux/kmod.h>
18 #include <linux/kobj_map.h>
19 #include <linux/mutex.h>
20 #include <linux/idr.h>
21 #include <linux/log2.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/badblocks.h>
25 #include "blk.h"
27 static DEFINE_MUTEX(block_class_lock);
28 struct kobject *block_depr;
30 /* for extended dynamic devt allocation, currently only one major is used */
31 #define NR_EXT_DEVT (1 << MINORBITS)
33 /* For extended devt allocation. ext_devt_lock prevents look up
34 * results from going away underneath its user.
36 static DEFINE_SPINLOCK(ext_devt_lock);
37 static DEFINE_IDR(ext_devt_idr);
39 static const struct device_type disk_type;
41 static void disk_check_events(struct disk_events *ev,
42 unsigned int *clearing_ptr);
43 static void disk_alloc_events(struct gendisk *disk);
44 static void disk_add_events(struct gendisk *disk);
45 static void disk_del_events(struct gendisk *disk);
46 static void disk_release_events(struct gendisk *disk);
48 /**
49 * disk_get_part - get partition
50 * @disk: disk to look partition from
51 * @partno: partition number
53 * Look for partition @partno from @disk. If found, increment
54 * reference count and return it.
56 * CONTEXT:
57 * Don't care.
59 * RETURNS:
60 * Pointer to the found partition on success, NULL if not found.
62 struct hd_struct *disk_get_part(struct gendisk *disk, int partno)
64 struct hd_struct *part = NULL;
65 struct disk_part_tbl *ptbl;
67 if (unlikely(partno < 0))
68 return NULL;
70 rcu_read_lock();
72 ptbl = rcu_dereference(disk->part_tbl);
73 if (likely(partno < ptbl->len)) {
74 part = rcu_dereference(ptbl->part[partno]);
75 if (part)
76 get_device(part_to_dev(part));
79 rcu_read_unlock();
81 return part;
83 EXPORT_SYMBOL_GPL(disk_get_part);
85 /**
86 * disk_part_iter_init - initialize partition iterator
87 * @piter: iterator to initialize
88 * @disk: disk to iterate over
89 * @flags: DISK_PITER_* flags
91 * Initialize @piter so that it iterates over partitions of @disk.
93 * CONTEXT:
94 * Don't care.
96 void disk_part_iter_init(struct disk_part_iter *piter, struct gendisk *disk,
97 unsigned int flags)
99 struct disk_part_tbl *ptbl;
101 rcu_read_lock();
102 ptbl = rcu_dereference(disk->part_tbl);
104 piter->disk = disk;
105 piter->part = NULL;
107 if (flags & DISK_PITER_REVERSE)
108 piter->idx = ptbl->len - 1;
109 else if (flags & (DISK_PITER_INCL_PART0 | DISK_PITER_INCL_EMPTY_PART0))
110 piter->idx = 0;
111 else
112 piter->idx = 1;
114 piter->flags = flags;
116 rcu_read_unlock();
118 EXPORT_SYMBOL_GPL(disk_part_iter_init);
121 * disk_part_iter_next - proceed iterator to the next partition and return it
122 * @piter: iterator of interest
124 * Proceed @piter to the next partition and return it.
126 * CONTEXT:
127 * Don't care.
129 struct hd_struct *disk_part_iter_next(struct disk_part_iter *piter)
131 struct disk_part_tbl *ptbl;
132 int inc, end;
134 /* put the last partition */
135 disk_put_part(piter->part);
136 piter->part = NULL;
138 /* get part_tbl */
139 rcu_read_lock();
140 ptbl = rcu_dereference(piter->disk->part_tbl);
142 /* determine iteration parameters */
143 if (piter->flags & DISK_PITER_REVERSE) {
144 inc = -1;
145 if (piter->flags & (DISK_PITER_INCL_PART0 |
146 DISK_PITER_INCL_EMPTY_PART0))
147 end = -1;
148 else
149 end = 0;
150 } else {
151 inc = 1;
152 end = ptbl->len;
155 /* iterate to the next partition */
156 for (; piter->idx != end; piter->idx += inc) {
157 struct hd_struct *part;
159 part = rcu_dereference(ptbl->part[piter->idx]);
160 if (!part)
161 continue;
162 if (!part_nr_sects_read(part) &&
163 !(piter->flags & DISK_PITER_INCL_EMPTY) &&
164 !(piter->flags & DISK_PITER_INCL_EMPTY_PART0 &&
165 piter->idx == 0))
166 continue;
168 get_device(part_to_dev(part));
169 piter->part = part;
170 piter->idx += inc;
171 break;
174 rcu_read_unlock();
176 return piter->part;
178 EXPORT_SYMBOL_GPL(disk_part_iter_next);
181 * disk_part_iter_exit - finish up partition iteration
182 * @piter: iter of interest
184 * Called when iteration is over. Cleans up @piter.
186 * CONTEXT:
187 * Don't care.
189 void disk_part_iter_exit(struct disk_part_iter *piter)
191 disk_put_part(piter->part);
192 piter->part = NULL;
194 EXPORT_SYMBOL_GPL(disk_part_iter_exit);
196 static inline int sector_in_part(struct hd_struct *part, sector_t sector)
198 return part->start_sect <= sector &&
199 sector < part->start_sect + part_nr_sects_read(part);
203 * disk_map_sector_rcu - map sector to partition
204 * @disk: gendisk of interest
205 * @sector: sector to map
207 * Find out which partition @sector maps to on @disk. This is
208 * primarily used for stats accounting.
210 * CONTEXT:
211 * RCU read locked. The returned partition pointer is valid only
212 * while preemption is disabled.
214 * RETURNS:
215 * Found partition on success, part0 is returned if no partition matches
217 struct hd_struct *disk_map_sector_rcu(struct gendisk *disk, sector_t sector)
219 struct disk_part_tbl *ptbl;
220 struct hd_struct *part;
221 int i;
223 ptbl = rcu_dereference(disk->part_tbl);
225 part = rcu_dereference(ptbl->last_lookup);
226 if (part && sector_in_part(part, sector))
227 return part;
229 for (i = 1; i < ptbl->len; i++) {
230 part = rcu_dereference(ptbl->part[i]);
232 if (part && sector_in_part(part, sector)) {
233 rcu_assign_pointer(ptbl->last_lookup, part);
234 return part;
237 return &disk->part0;
239 EXPORT_SYMBOL_GPL(disk_map_sector_rcu);
242 * Can be deleted altogether. Later.
245 #define BLKDEV_MAJOR_HASH_SIZE 255
246 static struct blk_major_name {
247 struct blk_major_name *next;
248 int major;
249 char name[16];
250 } *major_names[BLKDEV_MAJOR_HASH_SIZE];
252 /* index in the above - for now: assume no multimajor ranges */
253 static inline int major_to_index(unsigned major)
255 return major % BLKDEV_MAJOR_HASH_SIZE;
258 #ifdef CONFIG_PROC_FS
259 void blkdev_show(struct seq_file *seqf, off_t offset)
261 struct blk_major_name *dp;
263 mutex_lock(&block_class_lock);
264 for (dp = major_names[major_to_index(offset)]; dp; dp = dp->next)
265 if (dp->major == offset)
266 seq_printf(seqf, "%3d %s\n", dp->major, dp->name);
267 mutex_unlock(&block_class_lock);
269 #endif /* CONFIG_PROC_FS */
272 * register_blkdev - register a new block device
274 * @major: the requested major device number [1..255]. If @major = 0, try to
275 * allocate any unused major number.
276 * @name: the name of the new block device as a zero terminated string
278 * The @name must be unique within the system.
280 * The return value depends on the @major input parameter:
282 * - if a major device number was requested in range [1..255] then the
283 * function returns zero on success, or a negative error code
284 * - if any unused major number was requested with @major = 0 parameter
285 * then the return value is the allocated major number in range
286 * [1..255] or a negative error code otherwise
288 int register_blkdev(unsigned int major, const char *name)
290 struct blk_major_name **n, *p;
291 int index, ret = 0;
293 mutex_lock(&block_class_lock);
295 /* temporary */
296 if (major == 0) {
297 for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) {
298 if (major_names[index] == NULL)
299 break;
302 if (index == 0) {
303 printk("register_blkdev: failed to get major for %s\n",
304 name);
305 ret = -EBUSY;
306 goto out;
308 major = index;
309 ret = major;
312 if (major >= BLKDEV_MAJOR_MAX) {
313 pr_err("register_blkdev: major requested (%d) is greater than the maximum (%d) for %s\n",
314 major, BLKDEV_MAJOR_MAX, name);
316 ret = -EINVAL;
317 goto out;
320 p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL);
321 if (p == NULL) {
322 ret = -ENOMEM;
323 goto out;
326 p->major = major;
327 strlcpy(p->name, name, sizeof(p->name));
328 p->next = NULL;
329 index = major_to_index(major);
331 for (n = &major_names[index]; *n; n = &(*n)->next) {
332 if ((*n)->major == major)
333 break;
335 if (!*n)
336 *n = p;
337 else
338 ret = -EBUSY;
340 if (ret < 0) {
341 printk("register_blkdev: cannot get major %d for %s\n",
342 major, name);
343 kfree(p);
345 out:
346 mutex_unlock(&block_class_lock);
347 return ret;
350 EXPORT_SYMBOL(register_blkdev);
352 void unregister_blkdev(unsigned int major, const char *name)
354 struct blk_major_name **n;
355 struct blk_major_name *p = NULL;
356 int index = major_to_index(major);
358 mutex_lock(&block_class_lock);
359 for (n = &major_names[index]; *n; n = &(*n)->next)
360 if ((*n)->major == major)
361 break;
362 if (!*n || strcmp((*n)->name, name)) {
363 WARN_ON(1);
364 } else {
365 p = *n;
366 *n = p->next;
368 mutex_unlock(&block_class_lock);
369 kfree(p);
372 EXPORT_SYMBOL(unregister_blkdev);
374 static struct kobj_map *bdev_map;
377 * blk_mangle_minor - scatter minor numbers apart
378 * @minor: minor number to mangle
380 * Scatter consecutively allocated @minor number apart if MANGLE_DEVT
381 * is enabled. Mangling twice gives the original value.
383 * RETURNS:
384 * Mangled value.
386 * CONTEXT:
387 * Don't care.
389 static int blk_mangle_minor(int minor)
391 #ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT
392 int i;
394 for (i = 0; i < MINORBITS / 2; i++) {
395 int low = minor & (1 << i);
396 int high = minor & (1 << (MINORBITS - 1 - i));
397 int distance = MINORBITS - 1 - 2 * i;
399 minor ^= low | high; /* clear both bits */
400 low <<= distance; /* swap the positions */
401 high >>= distance;
402 minor |= low | high; /* and set */
404 #endif
405 return minor;
409 * blk_alloc_devt - allocate a dev_t for a partition
410 * @part: partition to allocate dev_t for
411 * @devt: out parameter for resulting dev_t
413 * Allocate a dev_t for block device.
415 * RETURNS:
416 * 0 on success, allocated dev_t is returned in *@devt. -errno on
417 * failure.
419 * CONTEXT:
420 * Might sleep.
422 int blk_alloc_devt(struct hd_struct *part, dev_t *devt)
424 struct gendisk *disk = part_to_disk(part);
425 int idx;
427 /* in consecutive minor range? */
428 if (part->partno < disk->minors) {
429 *devt = MKDEV(disk->major, disk->first_minor + part->partno);
430 return 0;
433 /* allocate ext devt */
434 idr_preload(GFP_KERNEL);
436 spin_lock_bh(&ext_devt_lock);
437 idx = idr_alloc(&ext_devt_idr, part, 0, NR_EXT_DEVT, GFP_NOWAIT);
438 spin_unlock_bh(&ext_devt_lock);
440 idr_preload_end();
441 if (idx < 0)
442 return idx == -ENOSPC ? -EBUSY : idx;
444 *devt = MKDEV(BLOCK_EXT_MAJOR, blk_mangle_minor(idx));
445 return 0;
449 * blk_free_devt - free a dev_t
450 * @devt: dev_t to free
452 * Free @devt which was allocated using blk_alloc_devt().
454 * CONTEXT:
455 * Might sleep.
457 void blk_free_devt(dev_t devt)
459 if (devt == MKDEV(0, 0))
460 return;
462 if (MAJOR(devt) == BLOCK_EXT_MAJOR) {
463 spin_lock_bh(&ext_devt_lock);
464 idr_remove(&ext_devt_idr, blk_mangle_minor(MINOR(devt)));
465 spin_unlock_bh(&ext_devt_lock);
469 static char *bdevt_str(dev_t devt, char *buf)
471 if (MAJOR(devt) <= 0xff && MINOR(devt) <= 0xff) {
472 char tbuf[BDEVT_SIZE];
473 snprintf(tbuf, BDEVT_SIZE, "%02x%02x", MAJOR(devt), MINOR(devt));
474 snprintf(buf, BDEVT_SIZE, "%-9s", tbuf);
475 } else
476 snprintf(buf, BDEVT_SIZE, "%03x:%05x", MAJOR(devt), MINOR(devt));
478 return buf;
482 * Register device numbers dev..(dev+range-1)
483 * range must be nonzero
484 * The hash chain is sorted on range, so that subranges can override.
486 void blk_register_region(dev_t devt, unsigned long range, struct module *module,
487 struct kobject *(*probe)(dev_t, int *, void *),
488 int (*lock)(dev_t, void *), void *data)
490 kobj_map(bdev_map, devt, range, module, probe, lock, data);
493 EXPORT_SYMBOL(blk_register_region);
495 void blk_unregister_region(dev_t devt, unsigned long range)
497 kobj_unmap(bdev_map, devt, range);
500 EXPORT_SYMBOL(blk_unregister_region);
502 static struct kobject *exact_match(dev_t devt, int *partno, void *data)
504 struct gendisk *p = data;
506 return &disk_to_dev(p)->kobj;
509 static int exact_lock(dev_t devt, void *data)
511 struct gendisk *p = data;
513 if (!get_disk(p))
514 return -1;
515 return 0;
518 static void register_disk(struct device *parent, struct gendisk *disk)
520 struct device *ddev = disk_to_dev(disk);
521 struct block_device *bdev;
522 struct disk_part_iter piter;
523 struct hd_struct *part;
524 int err;
526 ddev->parent = parent;
528 dev_set_name(ddev, "%s", disk->disk_name);
530 /* delay uevents, until we scanned partition table */
531 dev_set_uevent_suppress(ddev, 1);
533 if (device_add(ddev))
534 return;
535 if (!sysfs_deprecated) {
536 err = sysfs_create_link(block_depr, &ddev->kobj,
537 kobject_name(&ddev->kobj));
538 if (err) {
539 device_del(ddev);
540 return;
545 * avoid probable deadlock caused by allocating memory with
546 * GFP_KERNEL in runtime_resume callback of its all ancestor
547 * devices
549 pm_runtime_set_memalloc_noio(ddev, true);
551 disk->part0.holder_dir = kobject_create_and_add("holders", &ddev->kobj);
552 disk->slave_dir = kobject_create_and_add("slaves", &ddev->kobj);
554 /* No minors to use for partitions */
555 if (!disk_part_scan_enabled(disk))
556 goto exit;
558 /* No such device (e.g., media were just removed) */
559 if (!get_capacity(disk))
560 goto exit;
562 bdev = bdget_disk(disk, 0);
563 if (!bdev)
564 goto exit;
566 bdev->bd_invalidated = 1;
567 err = blkdev_get(bdev, FMODE_READ, NULL);
568 if (err < 0)
569 goto exit;
570 blkdev_put(bdev, FMODE_READ);
572 exit:
573 /* announce disk after possible partitions are created */
574 dev_set_uevent_suppress(ddev, 0);
575 kobject_uevent(&ddev->kobj, KOBJ_ADD);
577 /* announce possible partitions */
578 disk_part_iter_init(&piter, disk, 0);
579 while ((part = disk_part_iter_next(&piter)))
580 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_ADD);
581 disk_part_iter_exit(&piter);
585 * device_add_disk - add partitioning information to kernel list
586 * @parent: parent device for the disk
587 * @disk: per-device partitioning information
589 * This function registers the partitioning information in @disk
590 * with the kernel.
592 * FIXME: error handling
594 void device_add_disk(struct device *parent, struct gendisk *disk)
596 struct backing_dev_info *bdi;
597 dev_t devt;
598 int retval;
600 /* minors == 0 indicates to use ext devt from part0 and should
601 * be accompanied with EXT_DEVT flag. Make sure all
602 * parameters make sense.
604 WARN_ON(disk->minors && !(disk->major || disk->first_minor));
605 WARN_ON(!disk->minors && !(disk->flags & GENHD_FL_EXT_DEVT));
607 disk->flags |= GENHD_FL_UP;
609 retval = blk_alloc_devt(&disk->part0, &devt);
610 if (retval) {
611 WARN_ON(1);
612 return;
614 disk_to_dev(disk)->devt = devt;
616 /* ->major and ->first_minor aren't supposed to be
617 * dereferenced from here on, but set them just in case.
619 disk->major = MAJOR(devt);
620 disk->first_minor = MINOR(devt);
622 disk_alloc_events(disk);
624 /* Register BDI before referencing it from bdev */
625 bdi = disk->queue->backing_dev_info;
626 bdi_register_owner(bdi, disk_to_dev(disk));
628 blk_register_region(disk_devt(disk), disk->minors, NULL,
629 exact_match, exact_lock, disk);
630 register_disk(parent, disk);
631 blk_register_queue(disk);
634 * Take an extra ref on queue which will be put on disk_release()
635 * so that it sticks around as long as @disk is there.
637 WARN_ON_ONCE(!blk_get_queue(disk->queue));
639 retval = sysfs_create_link(&disk_to_dev(disk)->kobj, &bdi->dev->kobj,
640 "bdi");
641 WARN_ON(retval);
643 disk_add_events(disk);
644 blk_integrity_add(disk);
646 EXPORT_SYMBOL(device_add_disk);
648 void del_gendisk(struct gendisk *disk)
650 struct disk_part_iter piter;
651 struct hd_struct *part;
653 blk_integrity_del(disk);
654 disk_del_events(disk);
656 /* invalidate stuff */
657 disk_part_iter_init(&piter, disk,
658 DISK_PITER_INCL_EMPTY | DISK_PITER_REVERSE);
659 while ((part = disk_part_iter_next(&piter))) {
660 invalidate_partition(disk, part->partno);
661 bdev_unhash_inode(part_devt(part));
662 delete_partition(disk, part->partno);
664 disk_part_iter_exit(&piter);
666 invalidate_partition(disk, 0);
667 bdev_unhash_inode(disk_devt(disk));
668 set_capacity(disk, 0);
669 disk->flags &= ~GENHD_FL_UP;
671 sysfs_remove_link(&disk_to_dev(disk)->kobj, "bdi");
672 if (disk->queue) {
674 * Unregister bdi before releasing device numbers (as they can
675 * get reused and we'd get clashes in sysfs).
677 bdi_unregister(disk->queue->backing_dev_info);
678 blk_unregister_queue(disk);
679 } else {
680 WARN_ON(1);
682 blk_unregister_region(disk_devt(disk), disk->minors);
684 part_stat_set_all(&disk->part0, 0);
685 disk->part0.stamp = 0;
687 kobject_put(disk->part0.holder_dir);
688 kobject_put(disk->slave_dir);
689 if (!sysfs_deprecated)
690 sysfs_remove_link(block_depr, dev_name(disk_to_dev(disk)));
691 pm_runtime_set_memalloc_noio(disk_to_dev(disk), false);
692 device_del(disk_to_dev(disk));
694 EXPORT_SYMBOL(del_gendisk);
696 /* sysfs access to bad-blocks list. */
697 static ssize_t disk_badblocks_show(struct device *dev,
698 struct device_attribute *attr,
699 char *page)
701 struct gendisk *disk = dev_to_disk(dev);
703 if (!disk->bb)
704 return sprintf(page, "\n");
706 return badblocks_show(disk->bb, page, 0);
709 static ssize_t disk_badblocks_store(struct device *dev,
710 struct device_attribute *attr,
711 const char *page, size_t len)
713 struct gendisk *disk = dev_to_disk(dev);
715 if (!disk->bb)
716 return -ENXIO;
718 return badblocks_store(disk->bb, page, len, 0);
722 * get_gendisk - get partitioning information for a given device
723 * @devt: device to get partitioning information for
724 * @partno: returned partition index
726 * This function gets the structure containing partitioning
727 * information for the given device @devt.
729 struct gendisk *get_gendisk(dev_t devt, int *partno)
731 struct gendisk *disk = NULL;
733 if (MAJOR(devt) != BLOCK_EXT_MAJOR) {
734 struct kobject *kobj;
736 kobj = kobj_lookup(bdev_map, devt, partno);
737 if (kobj)
738 disk = dev_to_disk(kobj_to_dev(kobj));
739 } else {
740 struct hd_struct *part;
742 spin_lock_bh(&ext_devt_lock);
743 part = idr_find(&ext_devt_idr, blk_mangle_minor(MINOR(devt)));
744 if (part && get_disk(part_to_disk(part))) {
745 *partno = part->partno;
746 disk = part_to_disk(part);
748 spin_unlock_bh(&ext_devt_lock);
751 return disk;
753 EXPORT_SYMBOL(get_gendisk);
756 * bdget_disk - do bdget() by gendisk and partition number
757 * @disk: gendisk of interest
758 * @partno: partition number
760 * Find partition @partno from @disk, do bdget() on it.
762 * CONTEXT:
763 * Don't care.
765 * RETURNS:
766 * Resulting block_device on success, NULL on failure.
768 struct block_device *bdget_disk(struct gendisk *disk, int partno)
770 struct hd_struct *part;
771 struct block_device *bdev = NULL;
773 part = disk_get_part(disk, partno);
774 if (part)
775 bdev = bdget(part_devt(part));
776 disk_put_part(part);
778 return bdev;
780 EXPORT_SYMBOL(bdget_disk);
783 * print a full list of all partitions - intended for places where the root
784 * filesystem can't be mounted and thus to give the victim some idea of what
785 * went wrong
787 void __init printk_all_partitions(void)
789 struct class_dev_iter iter;
790 struct device *dev;
792 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
793 while ((dev = class_dev_iter_next(&iter))) {
794 struct gendisk *disk = dev_to_disk(dev);
795 struct disk_part_iter piter;
796 struct hd_struct *part;
797 char name_buf[BDEVNAME_SIZE];
798 char devt_buf[BDEVT_SIZE];
801 * Don't show empty devices or things that have been
802 * suppressed
804 if (get_capacity(disk) == 0 ||
805 (disk->flags & GENHD_FL_SUPPRESS_PARTITION_INFO))
806 continue;
809 * Note, unlike /proc/partitions, I am showing the
810 * numbers in hex - the same format as the root=
811 * option takes.
813 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
814 while ((part = disk_part_iter_next(&piter))) {
815 bool is_part0 = part == &disk->part0;
817 printk("%s%s %10llu %s %s", is_part0 ? "" : " ",
818 bdevt_str(part_devt(part), devt_buf),
819 (unsigned long long)part_nr_sects_read(part) >> 1
820 , disk_name(disk, part->partno, name_buf),
821 part->info ? part->info->uuid : "");
822 if (is_part0) {
823 if (dev->parent && dev->parent->driver)
824 printk(" driver: %s\n",
825 dev->parent->driver->name);
826 else
827 printk(" (driver?)\n");
828 } else
829 printk("\n");
831 disk_part_iter_exit(&piter);
833 class_dev_iter_exit(&iter);
836 #ifdef CONFIG_PROC_FS
837 /* iterator */
838 static void *disk_seqf_start(struct seq_file *seqf, loff_t *pos)
840 loff_t skip = *pos;
841 struct class_dev_iter *iter;
842 struct device *dev;
844 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
845 if (!iter)
846 return ERR_PTR(-ENOMEM);
848 seqf->private = iter;
849 class_dev_iter_init(iter, &block_class, NULL, &disk_type);
850 do {
851 dev = class_dev_iter_next(iter);
852 if (!dev)
853 return NULL;
854 } while (skip--);
856 return dev_to_disk(dev);
859 static void *disk_seqf_next(struct seq_file *seqf, void *v, loff_t *pos)
861 struct device *dev;
863 (*pos)++;
864 dev = class_dev_iter_next(seqf->private);
865 if (dev)
866 return dev_to_disk(dev);
868 return NULL;
871 static void disk_seqf_stop(struct seq_file *seqf, void *v)
873 struct class_dev_iter *iter = seqf->private;
875 /* stop is called even after start failed :-( */
876 if (iter) {
877 class_dev_iter_exit(iter);
878 kfree(iter);
879 seqf->private = NULL;
883 static void *show_partition_start(struct seq_file *seqf, loff_t *pos)
885 void *p;
887 p = disk_seqf_start(seqf, pos);
888 if (!IS_ERR_OR_NULL(p) && !*pos)
889 seq_puts(seqf, "major minor #blocks name\n\n");
890 return p;
893 static int show_partition(struct seq_file *seqf, void *v)
895 struct gendisk *sgp = v;
896 struct disk_part_iter piter;
897 struct hd_struct *part;
898 char buf[BDEVNAME_SIZE];
900 /* Don't show non-partitionable removeable devices or empty devices */
901 if (!get_capacity(sgp) || (!disk_max_parts(sgp) &&
902 (sgp->flags & GENHD_FL_REMOVABLE)))
903 return 0;
904 if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)
905 return 0;
907 /* show the full disk and all non-0 size partitions of it */
908 disk_part_iter_init(&piter, sgp, DISK_PITER_INCL_PART0);
909 while ((part = disk_part_iter_next(&piter)))
910 seq_printf(seqf, "%4d %7d %10llu %s\n",
911 MAJOR(part_devt(part)), MINOR(part_devt(part)),
912 (unsigned long long)part_nr_sects_read(part) >> 1,
913 disk_name(sgp, part->partno, buf));
914 disk_part_iter_exit(&piter);
916 return 0;
919 static const struct seq_operations partitions_op = {
920 .start = show_partition_start,
921 .next = disk_seqf_next,
922 .stop = disk_seqf_stop,
923 .show = show_partition
926 static int partitions_open(struct inode *inode, struct file *file)
928 return seq_open(file, &partitions_op);
931 static const struct file_operations proc_partitions_operations = {
932 .open = partitions_open,
933 .read = seq_read,
934 .llseek = seq_lseek,
935 .release = seq_release,
937 #endif
940 static struct kobject *base_probe(dev_t devt, int *partno, void *data)
942 if (request_module("block-major-%d-%d", MAJOR(devt), MINOR(devt)) > 0)
943 /* Make old-style 2.4 aliases work */
944 request_module("block-major-%d", MAJOR(devt));
945 return NULL;
948 static int __init genhd_device_init(void)
950 int error;
952 block_class.dev_kobj = sysfs_dev_block_kobj;
953 error = class_register(&block_class);
954 if (unlikely(error))
955 return error;
956 bdev_map = kobj_map_init(base_probe, &block_class_lock);
957 blk_dev_init();
959 register_blkdev(BLOCK_EXT_MAJOR, "blkext");
961 /* create top-level block dir */
962 if (!sysfs_deprecated)
963 block_depr = kobject_create_and_add("block", NULL);
964 return 0;
967 subsys_initcall(genhd_device_init);
969 static ssize_t disk_range_show(struct device *dev,
970 struct device_attribute *attr, char *buf)
972 struct gendisk *disk = dev_to_disk(dev);
974 return sprintf(buf, "%d\n", disk->minors);
977 static ssize_t disk_ext_range_show(struct device *dev,
978 struct device_attribute *attr, char *buf)
980 struct gendisk *disk = dev_to_disk(dev);
982 return sprintf(buf, "%d\n", disk_max_parts(disk));
985 static ssize_t disk_removable_show(struct device *dev,
986 struct device_attribute *attr, char *buf)
988 struct gendisk *disk = dev_to_disk(dev);
990 return sprintf(buf, "%d\n",
991 (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
994 static ssize_t disk_ro_show(struct device *dev,
995 struct device_attribute *attr, char *buf)
997 struct gendisk *disk = dev_to_disk(dev);
999 return sprintf(buf, "%d\n", get_disk_ro(disk) ? 1 : 0);
1002 static ssize_t disk_capability_show(struct device *dev,
1003 struct device_attribute *attr, char *buf)
1005 struct gendisk *disk = dev_to_disk(dev);
1007 return sprintf(buf, "%x\n", disk->flags);
1010 static ssize_t disk_alignment_offset_show(struct device *dev,
1011 struct device_attribute *attr,
1012 char *buf)
1014 struct gendisk *disk = dev_to_disk(dev);
1016 return sprintf(buf, "%d\n", queue_alignment_offset(disk->queue));
1019 static ssize_t disk_discard_alignment_show(struct device *dev,
1020 struct device_attribute *attr,
1021 char *buf)
1023 struct gendisk *disk = dev_to_disk(dev);
1025 return sprintf(buf, "%d\n", queue_discard_alignment(disk->queue));
1028 static DEVICE_ATTR(range, S_IRUGO, disk_range_show, NULL);
1029 static DEVICE_ATTR(ext_range, S_IRUGO, disk_ext_range_show, NULL);
1030 static DEVICE_ATTR(removable, S_IRUGO, disk_removable_show, NULL);
1031 static DEVICE_ATTR(ro, S_IRUGO, disk_ro_show, NULL);
1032 static DEVICE_ATTR(size, S_IRUGO, part_size_show, NULL);
1033 static DEVICE_ATTR(alignment_offset, S_IRUGO, disk_alignment_offset_show, NULL);
1034 static DEVICE_ATTR(discard_alignment, S_IRUGO, disk_discard_alignment_show,
1035 NULL);
1036 static DEVICE_ATTR(capability, S_IRUGO, disk_capability_show, NULL);
1037 static DEVICE_ATTR(stat, S_IRUGO, part_stat_show, NULL);
1038 static DEVICE_ATTR(inflight, S_IRUGO, part_inflight_show, NULL);
1039 static DEVICE_ATTR(badblocks, S_IRUGO | S_IWUSR, disk_badblocks_show,
1040 disk_badblocks_store);
1041 #ifdef CONFIG_FAIL_MAKE_REQUEST
1042 static struct device_attribute dev_attr_fail =
1043 __ATTR(make-it-fail, S_IRUGO|S_IWUSR, part_fail_show, part_fail_store);
1044 #endif
1045 #ifdef CONFIG_FAIL_IO_TIMEOUT
1046 static struct device_attribute dev_attr_fail_timeout =
1047 __ATTR(io-timeout-fail, S_IRUGO|S_IWUSR, part_timeout_show,
1048 part_timeout_store);
1049 #endif
1051 static struct attribute *disk_attrs[] = {
1052 &dev_attr_range.attr,
1053 &dev_attr_ext_range.attr,
1054 &dev_attr_removable.attr,
1055 &dev_attr_ro.attr,
1056 &dev_attr_size.attr,
1057 &dev_attr_alignment_offset.attr,
1058 &dev_attr_discard_alignment.attr,
1059 &dev_attr_capability.attr,
1060 &dev_attr_stat.attr,
1061 &dev_attr_inflight.attr,
1062 &dev_attr_badblocks.attr,
1063 #ifdef CONFIG_FAIL_MAKE_REQUEST
1064 &dev_attr_fail.attr,
1065 #endif
1066 #ifdef CONFIG_FAIL_IO_TIMEOUT
1067 &dev_attr_fail_timeout.attr,
1068 #endif
1069 NULL
1072 static umode_t disk_visible(struct kobject *kobj, struct attribute *a, int n)
1074 struct device *dev = container_of(kobj, typeof(*dev), kobj);
1075 struct gendisk *disk = dev_to_disk(dev);
1077 if (a == &dev_attr_badblocks.attr && !disk->bb)
1078 return 0;
1079 return a->mode;
1082 static struct attribute_group disk_attr_group = {
1083 .attrs = disk_attrs,
1084 .is_visible = disk_visible,
1087 static const struct attribute_group *disk_attr_groups[] = {
1088 &disk_attr_group,
1089 NULL
1093 * disk_replace_part_tbl - replace disk->part_tbl in RCU-safe way
1094 * @disk: disk to replace part_tbl for
1095 * @new_ptbl: new part_tbl to install
1097 * Replace disk->part_tbl with @new_ptbl in RCU-safe way. The
1098 * original ptbl is freed using RCU callback.
1100 * LOCKING:
1101 * Matching bd_mutx locked.
1103 static void disk_replace_part_tbl(struct gendisk *disk,
1104 struct disk_part_tbl *new_ptbl)
1106 struct disk_part_tbl *old_ptbl = disk->part_tbl;
1108 rcu_assign_pointer(disk->part_tbl, new_ptbl);
1110 if (old_ptbl) {
1111 rcu_assign_pointer(old_ptbl->last_lookup, NULL);
1112 kfree_rcu(old_ptbl, rcu_head);
1117 * disk_expand_part_tbl - expand disk->part_tbl
1118 * @disk: disk to expand part_tbl for
1119 * @partno: expand such that this partno can fit in
1121 * Expand disk->part_tbl such that @partno can fit in. disk->part_tbl
1122 * uses RCU to allow unlocked dereferencing for stats and other stuff.
1124 * LOCKING:
1125 * Matching bd_mutex locked, might sleep.
1127 * RETURNS:
1128 * 0 on success, -errno on failure.
1130 int disk_expand_part_tbl(struct gendisk *disk, int partno)
1132 struct disk_part_tbl *old_ptbl = disk->part_tbl;
1133 struct disk_part_tbl *new_ptbl;
1134 int len = old_ptbl ? old_ptbl->len : 0;
1135 int i, target;
1136 size_t size;
1139 * check for int overflow, since we can get here from blkpg_ioctl()
1140 * with a user passed 'partno'.
1142 target = partno + 1;
1143 if (target < 0)
1144 return -EINVAL;
1146 /* disk_max_parts() is zero during initialization, ignore if so */
1147 if (disk_max_parts(disk) && target > disk_max_parts(disk))
1148 return -EINVAL;
1150 if (target <= len)
1151 return 0;
1153 size = sizeof(*new_ptbl) + target * sizeof(new_ptbl->part[0]);
1154 new_ptbl = kzalloc_node(size, GFP_KERNEL, disk->node_id);
1155 if (!new_ptbl)
1156 return -ENOMEM;
1158 new_ptbl->len = target;
1160 for (i = 0; i < len; i++)
1161 rcu_assign_pointer(new_ptbl->part[i], old_ptbl->part[i]);
1163 disk_replace_part_tbl(disk, new_ptbl);
1164 return 0;
1167 static void disk_release(struct device *dev)
1169 struct gendisk *disk = dev_to_disk(dev);
1171 blk_free_devt(dev->devt);
1172 disk_release_events(disk);
1173 kfree(disk->random);
1174 disk_replace_part_tbl(disk, NULL);
1175 hd_free_part(&disk->part0);
1176 if (disk->queue)
1177 blk_put_queue(disk->queue);
1178 kfree(disk);
1180 struct class block_class = {
1181 .name = "block",
1184 static char *block_devnode(struct device *dev, umode_t *mode,
1185 kuid_t *uid, kgid_t *gid)
1187 struct gendisk *disk = dev_to_disk(dev);
1189 if (disk->devnode)
1190 return disk->devnode(disk, mode);
1191 return NULL;
1194 static const struct device_type disk_type = {
1195 .name = "disk",
1196 .groups = disk_attr_groups,
1197 .release = disk_release,
1198 .devnode = block_devnode,
1201 #ifdef CONFIG_PROC_FS
1203 * aggregate disk stat collector. Uses the same stats that the sysfs
1204 * entries do, above, but makes them available through one seq_file.
1206 * The output looks suspiciously like /proc/partitions with a bunch of
1207 * extra fields.
1209 static int diskstats_show(struct seq_file *seqf, void *v)
1211 struct gendisk *gp = v;
1212 struct disk_part_iter piter;
1213 struct hd_struct *hd;
1214 char buf[BDEVNAME_SIZE];
1215 int cpu;
1218 if (&disk_to_dev(gp)->kobj.entry == block_class.devices.next)
1219 seq_puts(seqf, "major minor name"
1220 " rio rmerge rsect ruse wio wmerge "
1221 "wsect wuse running use aveq"
1222 "\n\n");
1225 disk_part_iter_init(&piter, gp, DISK_PITER_INCL_EMPTY_PART0);
1226 while ((hd = disk_part_iter_next(&piter))) {
1227 cpu = part_stat_lock();
1228 part_round_stats(cpu, hd);
1229 part_stat_unlock();
1230 seq_printf(seqf, "%4d %7d %s %lu %lu %lu "
1231 "%u %lu %lu %lu %u %u %u %u\n",
1232 MAJOR(part_devt(hd)), MINOR(part_devt(hd)),
1233 disk_name(gp, hd->partno, buf),
1234 part_stat_read(hd, ios[READ]),
1235 part_stat_read(hd, merges[READ]),
1236 part_stat_read(hd, sectors[READ]),
1237 jiffies_to_msecs(part_stat_read(hd, ticks[READ])),
1238 part_stat_read(hd, ios[WRITE]),
1239 part_stat_read(hd, merges[WRITE]),
1240 part_stat_read(hd, sectors[WRITE]),
1241 jiffies_to_msecs(part_stat_read(hd, ticks[WRITE])),
1242 part_in_flight(hd),
1243 jiffies_to_msecs(part_stat_read(hd, io_ticks)),
1244 jiffies_to_msecs(part_stat_read(hd, time_in_queue))
1247 disk_part_iter_exit(&piter);
1249 return 0;
1252 static const struct seq_operations diskstats_op = {
1253 .start = disk_seqf_start,
1254 .next = disk_seqf_next,
1255 .stop = disk_seqf_stop,
1256 .show = diskstats_show
1259 static int diskstats_open(struct inode *inode, struct file *file)
1261 return seq_open(file, &diskstats_op);
1264 static const struct file_operations proc_diskstats_operations = {
1265 .open = diskstats_open,
1266 .read = seq_read,
1267 .llseek = seq_lseek,
1268 .release = seq_release,
1271 static int __init proc_genhd_init(void)
1273 proc_create("diskstats", 0, NULL, &proc_diskstats_operations);
1274 proc_create("partitions", 0, NULL, &proc_partitions_operations);
1275 return 0;
1277 module_init(proc_genhd_init);
1278 #endif /* CONFIG_PROC_FS */
1280 dev_t blk_lookup_devt(const char *name, int partno)
1282 dev_t devt = MKDEV(0, 0);
1283 struct class_dev_iter iter;
1284 struct device *dev;
1286 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
1287 while ((dev = class_dev_iter_next(&iter))) {
1288 struct gendisk *disk = dev_to_disk(dev);
1289 struct hd_struct *part;
1291 if (strcmp(dev_name(dev), name))
1292 continue;
1294 if (partno < disk->minors) {
1295 /* We need to return the right devno, even
1296 * if the partition doesn't exist yet.
1298 devt = MKDEV(MAJOR(dev->devt),
1299 MINOR(dev->devt) + partno);
1300 break;
1302 part = disk_get_part(disk, partno);
1303 if (part) {
1304 devt = part_devt(part);
1305 disk_put_part(part);
1306 break;
1308 disk_put_part(part);
1310 class_dev_iter_exit(&iter);
1311 return devt;
1313 EXPORT_SYMBOL(blk_lookup_devt);
1315 struct gendisk *alloc_disk(int minors)
1317 return alloc_disk_node(minors, NUMA_NO_NODE);
1319 EXPORT_SYMBOL(alloc_disk);
1321 struct gendisk *alloc_disk_node(int minors, int node_id)
1323 struct gendisk *disk;
1325 disk = kzalloc_node(sizeof(struct gendisk), GFP_KERNEL, node_id);
1326 if (disk) {
1327 if (!init_part_stats(&disk->part0)) {
1328 kfree(disk);
1329 return NULL;
1331 disk->node_id = node_id;
1332 if (disk_expand_part_tbl(disk, 0)) {
1333 free_part_stats(&disk->part0);
1334 kfree(disk);
1335 return NULL;
1337 disk->part_tbl->part[0] = &disk->part0;
1340 * set_capacity() and get_capacity() currently don't use
1341 * seqcounter to read/update the part0->nr_sects. Still init
1342 * the counter as we can read the sectors in IO submission
1343 * patch using seqence counters.
1345 * TODO: Ideally set_capacity() and get_capacity() should be
1346 * converted to make use of bd_mutex and sequence counters.
1348 seqcount_init(&disk->part0.nr_sects_seq);
1349 if (hd_ref_init(&disk->part0)) {
1350 hd_free_part(&disk->part0);
1351 kfree(disk);
1352 return NULL;
1355 disk->minors = minors;
1356 rand_initialize_disk(disk);
1357 disk_to_dev(disk)->class = &block_class;
1358 disk_to_dev(disk)->type = &disk_type;
1359 device_initialize(disk_to_dev(disk));
1361 return disk;
1363 EXPORT_SYMBOL(alloc_disk_node);
1365 struct kobject *get_disk(struct gendisk *disk)
1367 struct module *owner;
1368 struct kobject *kobj;
1370 if (!disk->fops)
1371 return NULL;
1372 owner = disk->fops->owner;
1373 if (owner && !try_module_get(owner))
1374 return NULL;
1375 kobj = kobject_get_unless_zero(&disk_to_dev(disk)->kobj);
1376 if (kobj == NULL) {
1377 module_put(owner);
1378 return NULL;
1380 return kobj;
1384 EXPORT_SYMBOL(get_disk);
1386 void put_disk(struct gendisk *disk)
1388 if (disk)
1389 kobject_put(&disk_to_dev(disk)->kobj);
1392 EXPORT_SYMBOL(put_disk);
1394 static void set_disk_ro_uevent(struct gendisk *gd, int ro)
1396 char event[] = "DISK_RO=1";
1397 char *envp[] = { event, NULL };
1399 if (!ro)
1400 event[8] = '0';
1401 kobject_uevent_env(&disk_to_dev(gd)->kobj, KOBJ_CHANGE, envp);
1404 void set_device_ro(struct block_device *bdev, int flag)
1406 bdev->bd_part->policy = flag;
1409 EXPORT_SYMBOL(set_device_ro);
1411 void set_disk_ro(struct gendisk *disk, int flag)
1413 struct disk_part_iter piter;
1414 struct hd_struct *part;
1416 if (disk->part0.policy != flag) {
1417 set_disk_ro_uevent(disk, flag);
1418 disk->part0.policy = flag;
1421 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_EMPTY);
1422 while ((part = disk_part_iter_next(&piter)))
1423 part->policy = flag;
1424 disk_part_iter_exit(&piter);
1427 EXPORT_SYMBOL(set_disk_ro);
1429 int bdev_read_only(struct block_device *bdev)
1431 if (!bdev)
1432 return 0;
1433 return bdev->bd_part->policy;
1436 EXPORT_SYMBOL(bdev_read_only);
1438 int invalidate_partition(struct gendisk *disk, int partno)
1440 int res = 0;
1441 struct block_device *bdev = bdget_disk(disk, partno);
1442 if (bdev) {
1443 fsync_bdev(bdev);
1444 res = __invalidate_device(bdev, true);
1445 bdput(bdev);
1447 return res;
1450 EXPORT_SYMBOL(invalidate_partition);
1453 * Disk events - monitor disk events like media change and eject request.
1455 struct disk_events {
1456 struct list_head node; /* all disk_event's */
1457 struct gendisk *disk; /* the associated disk */
1458 spinlock_t lock;
1460 struct mutex block_mutex; /* protects blocking */
1461 int block; /* event blocking depth */
1462 unsigned int pending; /* events already sent out */
1463 unsigned int clearing; /* events being cleared */
1465 long poll_msecs; /* interval, -1 for default */
1466 struct delayed_work dwork;
1469 static const char *disk_events_strs[] = {
1470 [ilog2(DISK_EVENT_MEDIA_CHANGE)] = "media_change",
1471 [ilog2(DISK_EVENT_EJECT_REQUEST)] = "eject_request",
1474 static char *disk_uevents[] = {
1475 [ilog2(DISK_EVENT_MEDIA_CHANGE)] = "DISK_MEDIA_CHANGE=1",
1476 [ilog2(DISK_EVENT_EJECT_REQUEST)] = "DISK_EJECT_REQUEST=1",
1479 /* list of all disk_events */
1480 static DEFINE_MUTEX(disk_events_mutex);
1481 static LIST_HEAD(disk_events);
1483 /* disable in-kernel polling by default */
1484 static unsigned long disk_events_dfl_poll_msecs;
1486 static unsigned long disk_events_poll_jiffies(struct gendisk *disk)
1488 struct disk_events *ev = disk->ev;
1489 long intv_msecs = 0;
1492 * If device-specific poll interval is set, always use it. If
1493 * the default is being used, poll iff there are events which
1494 * can't be monitored asynchronously.
1496 if (ev->poll_msecs >= 0)
1497 intv_msecs = ev->poll_msecs;
1498 else if (disk->events & ~disk->async_events)
1499 intv_msecs = disk_events_dfl_poll_msecs;
1501 return msecs_to_jiffies(intv_msecs);
1505 * disk_block_events - block and flush disk event checking
1506 * @disk: disk to block events for
1508 * On return from this function, it is guaranteed that event checking
1509 * isn't in progress and won't happen until unblocked by
1510 * disk_unblock_events(). Events blocking is counted and the actual
1511 * unblocking happens after the matching number of unblocks are done.
1513 * Note that this intentionally does not block event checking from
1514 * disk_clear_events().
1516 * CONTEXT:
1517 * Might sleep.
1519 void disk_block_events(struct gendisk *disk)
1521 struct disk_events *ev = disk->ev;
1522 unsigned long flags;
1523 bool cancel;
1525 if (!ev)
1526 return;
1529 * Outer mutex ensures that the first blocker completes canceling
1530 * the event work before further blockers are allowed to finish.
1532 mutex_lock(&ev->block_mutex);
1534 spin_lock_irqsave(&ev->lock, flags);
1535 cancel = !ev->block++;
1536 spin_unlock_irqrestore(&ev->lock, flags);
1538 if (cancel)
1539 cancel_delayed_work_sync(&disk->ev->dwork);
1541 mutex_unlock(&ev->block_mutex);
1544 static void __disk_unblock_events(struct gendisk *disk, bool check_now)
1546 struct disk_events *ev = disk->ev;
1547 unsigned long intv;
1548 unsigned long flags;
1550 spin_lock_irqsave(&ev->lock, flags);
1552 if (WARN_ON_ONCE(ev->block <= 0))
1553 goto out_unlock;
1555 if (--ev->block)
1556 goto out_unlock;
1558 intv = disk_events_poll_jiffies(disk);
1559 if (check_now)
1560 queue_delayed_work(system_freezable_power_efficient_wq,
1561 &ev->dwork, 0);
1562 else if (intv)
1563 queue_delayed_work(system_freezable_power_efficient_wq,
1564 &ev->dwork, intv);
1565 out_unlock:
1566 spin_unlock_irqrestore(&ev->lock, flags);
1570 * disk_unblock_events - unblock disk event checking
1571 * @disk: disk to unblock events for
1573 * Undo disk_block_events(). When the block count reaches zero, it
1574 * starts events polling if configured.
1576 * CONTEXT:
1577 * Don't care. Safe to call from irq context.
1579 void disk_unblock_events(struct gendisk *disk)
1581 if (disk->ev)
1582 __disk_unblock_events(disk, false);
1586 * disk_flush_events - schedule immediate event checking and flushing
1587 * @disk: disk to check and flush events for
1588 * @mask: events to flush
1590 * Schedule immediate event checking on @disk if not blocked. Events in
1591 * @mask are scheduled to be cleared from the driver. Note that this
1592 * doesn't clear the events from @disk->ev.
1594 * CONTEXT:
1595 * If @mask is non-zero must be called with bdev->bd_mutex held.
1597 void disk_flush_events(struct gendisk *disk, unsigned int mask)
1599 struct disk_events *ev = disk->ev;
1601 if (!ev)
1602 return;
1604 spin_lock_irq(&ev->lock);
1605 ev->clearing |= mask;
1606 if (!ev->block)
1607 mod_delayed_work(system_freezable_power_efficient_wq,
1608 &ev->dwork, 0);
1609 spin_unlock_irq(&ev->lock);
1613 * disk_clear_events - synchronously check, clear and return pending events
1614 * @disk: disk to fetch and clear events from
1615 * @mask: mask of events to be fetched and cleared
1617 * Disk events are synchronously checked and pending events in @mask
1618 * are cleared and returned. This ignores the block count.
1620 * CONTEXT:
1621 * Might sleep.
1623 unsigned int disk_clear_events(struct gendisk *disk, unsigned int mask)
1625 const struct block_device_operations *bdops = disk->fops;
1626 struct disk_events *ev = disk->ev;
1627 unsigned int pending;
1628 unsigned int clearing = mask;
1630 if (!ev) {
1631 /* for drivers still using the old ->media_changed method */
1632 if ((mask & DISK_EVENT_MEDIA_CHANGE) &&
1633 bdops->media_changed && bdops->media_changed(disk))
1634 return DISK_EVENT_MEDIA_CHANGE;
1635 return 0;
1638 disk_block_events(disk);
1641 * store the union of mask and ev->clearing on the stack so that the
1642 * race with disk_flush_events does not cause ambiguity (ev->clearing
1643 * can still be modified even if events are blocked).
1645 spin_lock_irq(&ev->lock);
1646 clearing |= ev->clearing;
1647 ev->clearing = 0;
1648 spin_unlock_irq(&ev->lock);
1650 disk_check_events(ev, &clearing);
1652 * if ev->clearing is not 0, the disk_flush_events got called in the
1653 * middle of this function, so we want to run the workfn without delay.
1655 __disk_unblock_events(disk, ev->clearing ? true : false);
1657 /* then, fetch and clear pending events */
1658 spin_lock_irq(&ev->lock);
1659 pending = ev->pending & mask;
1660 ev->pending &= ~mask;
1661 spin_unlock_irq(&ev->lock);
1662 WARN_ON_ONCE(clearing & mask);
1664 return pending;
1668 * Separate this part out so that a different pointer for clearing_ptr can be
1669 * passed in for disk_clear_events.
1671 static void disk_events_workfn(struct work_struct *work)
1673 struct delayed_work *dwork = to_delayed_work(work);
1674 struct disk_events *ev = container_of(dwork, struct disk_events, dwork);
1676 disk_check_events(ev, &ev->clearing);
1679 static void disk_check_events(struct disk_events *ev,
1680 unsigned int *clearing_ptr)
1682 struct gendisk *disk = ev->disk;
1683 char *envp[ARRAY_SIZE(disk_uevents) + 1] = { };
1684 unsigned int clearing = *clearing_ptr;
1685 unsigned int events;
1686 unsigned long intv;
1687 int nr_events = 0, i;
1689 /* check events */
1690 events = disk->fops->check_events(disk, clearing);
1692 /* accumulate pending events and schedule next poll if necessary */
1693 spin_lock_irq(&ev->lock);
1695 events &= ~ev->pending;
1696 ev->pending |= events;
1697 *clearing_ptr &= ~clearing;
1699 intv = disk_events_poll_jiffies(disk);
1700 if (!ev->block && intv)
1701 queue_delayed_work(system_freezable_power_efficient_wq,
1702 &ev->dwork, intv);
1704 spin_unlock_irq(&ev->lock);
1707 * Tell userland about new events. Only the events listed in
1708 * @disk->events are reported. Unlisted events are processed the
1709 * same internally but never get reported to userland.
1711 for (i = 0; i < ARRAY_SIZE(disk_uevents); i++)
1712 if (events & disk->events & (1 << i))
1713 envp[nr_events++] = disk_uevents[i];
1715 if (nr_events)
1716 kobject_uevent_env(&disk_to_dev(disk)->kobj, KOBJ_CHANGE, envp);
1720 * A disk events enabled device has the following sysfs nodes under
1721 * its /sys/block/X/ directory.
1723 * events : list of all supported events
1724 * events_async : list of events which can be detected w/o polling
1725 * events_poll_msecs : polling interval, 0: disable, -1: system default
1727 static ssize_t __disk_events_show(unsigned int events, char *buf)
1729 const char *delim = "";
1730 ssize_t pos = 0;
1731 int i;
1733 for (i = 0; i < ARRAY_SIZE(disk_events_strs); i++)
1734 if (events & (1 << i)) {
1735 pos += sprintf(buf + pos, "%s%s",
1736 delim, disk_events_strs[i]);
1737 delim = " ";
1739 if (pos)
1740 pos += sprintf(buf + pos, "\n");
1741 return pos;
1744 static ssize_t disk_events_show(struct device *dev,
1745 struct device_attribute *attr, char *buf)
1747 struct gendisk *disk = dev_to_disk(dev);
1749 return __disk_events_show(disk->events, buf);
1752 static ssize_t disk_events_async_show(struct device *dev,
1753 struct device_attribute *attr, char *buf)
1755 struct gendisk *disk = dev_to_disk(dev);
1757 return __disk_events_show(disk->async_events, buf);
1760 static ssize_t disk_events_poll_msecs_show(struct device *dev,
1761 struct device_attribute *attr,
1762 char *buf)
1764 struct gendisk *disk = dev_to_disk(dev);
1766 return sprintf(buf, "%ld\n", disk->ev->poll_msecs);
1769 static ssize_t disk_events_poll_msecs_store(struct device *dev,
1770 struct device_attribute *attr,
1771 const char *buf, size_t count)
1773 struct gendisk *disk = dev_to_disk(dev);
1774 long intv;
1776 if (!count || !sscanf(buf, "%ld", &intv))
1777 return -EINVAL;
1779 if (intv < 0 && intv != -1)
1780 return -EINVAL;
1782 disk_block_events(disk);
1783 disk->ev->poll_msecs = intv;
1784 __disk_unblock_events(disk, true);
1786 return count;
1789 static const DEVICE_ATTR(events, S_IRUGO, disk_events_show, NULL);
1790 static const DEVICE_ATTR(events_async, S_IRUGO, disk_events_async_show, NULL);
1791 static const DEVICE_ATTR(events_poll_msecs, S_IRUGO|S_IWUSR,
1792 disk_events_poll_msecs_show,
1793 disk_events_poll_msecs_store);
1795 static const struct attribute *disk_events_attrs[] = {
1796 &dev_attr_events.attr,
1797 &dev_attr_events_async.attr,
1798 &dev_attr_events_poll_msecs.attr,
1799 NULL,
1803 * The default polling interval can be specified by the kernel
1804 * parameter block.events_dfl_poll_msecs which defaults to 0
1805 * (disable). This can also be modified runtime by writing to
1806 * /sys/module/block/events_dfl_poll_msecs.
1808 static int disk_events_set_dfl_poll_msecs(const char *val,
1809 const struct kernel_param *kp)
1811 struct disk_events *ev;
1812 int ret;
1814 ret = param_set_ulong(val, kp);
1815 if (ret < 0)
1816 return ret;
1818 mutex_lock(&disk_events_mutex);
1820 list_for_each_entry(ev, &disk_events, node)
1821 disk_flush_events(ev->disk, 0);
1823 mutex_unlock(&disk_events_mutex);
1825 return 0;
1828 static const struct kernel_param_ops disk_events_dfl_poll_msecs_param_ops = {
1829 .set = disk_events_set_dfl_poll_msecs,
1830 .get = param_get_ulong,
1833 #undef MODULE_PARAM_PREFIX
1834 #define MODULE_PARAM_PREFIX "block."
1836 module_param_cb(events_dfl_poll_msecs, &disk_events_dfl_poll_msecs_param_ops,
1837 &disk_events_dfl_poll_msecs, 0644);
1840 * disk_{alloc|add|del|release}_events - initialize and destroy disk_events.
1842 static void disk_alloc_events(struct gendisk *disk)
1844 struct disk_events *ev;
1846 if (!disk->fops->check_events)
1847 return;
1849 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
1850 if (!ev) {
1851 pr_warn("%s: failed to initialize events\n", disk->disk_name);
1852 return;
1855 INIT_LIST_HEAD(&ev->node);
1856 ev->disk = disk;
1857 spin_lock_init(&ev->lock);
1858 mutex_init(&ev->block_mutex);
1859 ev->block = 1;
1860 ev->poll_msecs = -1;
1861 INIT_DELAYED_WORK(&ev->dwork, disk_events_workfn);
1863 disk->ev = ev;
1866 static void disk_add_events(struct gendisk *disk)
1868 if (!disk->ev)
1869 return;
1871 /* FIXME: error handling */
1872 if (sysfs_create_files(&disk_to_dev(disk)->kobj, disk_events_attrs) < 0)
1873 pr_warn("%s: failed to create sysfs files for events\n",
1874 disk->disk_name);
1876 mutex_lock(&disk_events_mutex);
1877 list_add_tail(&disk->ev->node, &disk_events);
1878 mutex_unlock(&disk_events_mutex);
1881 * Block count is initialized to 1 and the following initial
1882 * unblock kicks it into action.
1884 __disk_unblock_events(disk, true);
1887 static void disk_del_events(struct gendisk *disk)
1889 if (!disk->ev)
1890 return;
1892 disk_block_events(disk);
1894 mutex_lock(&disk_events_mutex);
1895 list_del_init(&disk->ev->node);
1896 mutex_unlock(&disk_events_mutex);
1898 sysfs_remove_files(&disk_to_dev(disk)->kobj, disk_events_attrs);
1901 static void disk_release_events(struct gendisk *disk)
1903 /* the block count should be 1 from disk_del_events() */
1904 WARN_ON_ONCE(disk->ev && disk->ev->block != 1);
1905 kfree(disk->ev);