Input: gpio-keys - allow disabling individual buttons in DT
[linux-2.6/btrfs-unstable.git] / block / genhd.c
blobe5cafa51567c9d589147523c8ab7b43504f9d725
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>
24 #include "blk.h"
26 static DEFINE_MUTEX(block_class_lock);
27 struct kobject *block_depr;
29 /* for extended dynamic devt allocation, currently only one major is used */
30 #define NR_EXT_DEVT (1 << MINORBITS)
32 /* For extended devt allocation. ext_devt_lock prevents look up
33 * results from going away underneath its user.
35 static DEFINE_SPINLOCK(ext_devt_lock);
36 static DEFINE_IDR(ext_devt_idr);
38 static struct device_type disk_type;
40 static void disk_check_events(struct disk_events *ev,
41 unsigned int *clearing_ptr);
42 static void disk_alloc_events(struct gendisk *disk);
43 static void disk_add_events(struct gendisk *disk);
44 static void disk_del_events(struct gendisk *disk);
45 static void disk_release_events(struct gendisk *disk);
47 /**
48 * disk_get_part - get partition
49 * @disk: disk to look partition from
50 * @partno: partition number
52 * Look for partition @partno from @disk. If found, increment
53 * reference count and return it.
55 * CONTEXT:
56 * Don't care.
58 * RETURNS:
59 * Pointer to the found partition on success, NULL if not found.
61 struct hd_struct *disk_get_part(struct gendisk *disk, int partno)
63 struct hd_struct *part = NULL;
64 struct disk_part_tbl *ptbl;
66 if (unlikely(partno < 0))
67 return NULL;
69 rcu_read_lock();
71 ptbl = rcu_dereference(disk->part_tbl);
72 if (likely(partno < ptbl->len)) {
73 part = rcu_dereference(ptbl->part[partno]);
74 if (part)
75 get_device(part_to_dev(part));
78 rcu_read_unlock();
80 return part;
82 EXPORT_SYMBOL_GPL(disk_get_part);
84 /**
85 * disk_part_iter_init - initialize partition iterator
86 * @piter: iterator to initialize
87 * @disk: disk to iterate over
88 * @flags: DISK_PITER_* flags
90 * Initialize @piter so that it iterates over partitions of @disk.
92 * CONTEXT:
93 * Don't care.
95 void disk_part_iter_init(struct disk_part_iter *piter, struct gendisk *disk,
96 unsigned int flags)
98 struct disk_part_tbl *ptbl;
100 rcu_read_lock();
101 ptbl = rcu_dereference(disk->part_tbl);
103 piter->disk = disk;
104 piter->part = NULL;
106 if (flags & DISK_PITER_REVERSE)
107 piter->idx = ptbl->len - 1;
108 else if (flags & (DISK_PITER_INCL_PART0 | DISK_PITER_INCL_EMPTY_PART0))
109 piter->idx = 0;
110 else
111 piter->idx = 1;
113 piter->flags = flags;
115 rcu_read_unlock();
117 EXPORT_SYMBOL_GPL(disk_part_iter_init);
120 * disk_part_iter_next - proceed iterator to the next partition and return it
121 * @piter: iterator of interest
123 * Proceed @piter to the next partition and return it.
125 * CONTEXT:
126 * Don't care.
128 struct hd_struct *disk_part_iter_next(struct disk_part_iter *piter)
130 struct disk_part_tbl *ptbl;
131 int inc, end;
133 /* put the last partition */
134 disk_put_part(piter->part);
135 piter->part = NULL;
137 /* get part_tbl */
138 rcu_read_lock();
139 ptbl = rcu_dereference(piter->disk->part_tbl);
141 /* determine iteration parameters */
142 if (piter->flags & DISK_PITER_REVERSE) {
143 inc = -1;
144 if (piter->flags & (DISK_PITER_INCL_PART0 |
145 DISK_PITER_INCL_EMPTY_PART0))
146 end = -1;
147 else
148 end = 0;
149 } else {
150 inc = 1;
151 end = ptbl->len;
154 /* iterate to the next partition */
155 for (; piter->idx != end; piter->idx += inc) {
156 struct hd_struct *part;
158 part = rcu_dereference(ptbl->part[piter->idx]);
159 if (!part)
160 continue;
161 if (!part_nr_sects_read(part) &&
162 !(piter->flags & DISK_PITER_INCL_EMPTY) &&
163 !(piter->flags & DISK_PITER_INCL_EMPTY_PART0 &&
164 piter->idx == 0))
165 continue;
167 get_device(part_to_dev(part));
168 piter->part = part;
169 piter->idx += inc;
170 break;
173 rcu_read_unlock();
175 return piter->part;
177 EXPORT_SYMBOL_GPL(disk_part_iter_next);
180 * disk_part_iter_exit - finish up partition iteration
181 * @piter: iter of interest
183 * Called when iteration is over. Cleans up @piter.
185 * CONTEXT:
186 * Don't care.
188 void disk_part_iter_exit(struct disk_part_iter *piter)
190 disk_put_part(piter->part);
191 piter->part = NULL;
193 EXPORT_SYMBOL_GPL(disk_part_iter_exit);
195 static inline int sector_in_part(struct hd_struct *part, sector_t sector)
197 return part->start_sect <= sector &&
198 sector < part->start_sect + part_nr_sects_read(part);
202 * disk_map_sector_rcu - map sector to partition
203 * @disk: gendisk of interest
204 * @sector: sector to map
206 * Find out which partition @sector maps to on @disk. This is
207 * primarily used for stats accounting.
209 * CONTEXT:
210 * RCU read locked. The returned partition pointer is valid only
211 * while preemption is disabled.
213 * RETURNS:
214 * Found partition on success, part0 is returned if no partition matches
216 struct hd_struct *disk_map_sector_rcu(struct gendisk *disk, sector_t sector)
218 struct disk_part_tbl *ptbl;
219 struct hd_struct *part;
220 int i;
222 ptbl = rcu_dereference(disk->part_tbl);
224 part = rcu_dereference(ptbl->last_lookup);
225 if (part && sector_in_part(part, sector))
226 return part;
228 for (i = 1; i < ptbl->len; i++) {
229 part = rcu_dereference(ptbl->part[i]);
231 if (part && sector_in_part(part, sector)) {
232 rcu_assign_pointer(ptbl->last_lookup, part);
233 return part;
236 return &disk->part0;
238 EXPORT_SYMBOL_GPL(disk_map_sector_rcu);
241 * Can be deleted altogether. Later.
244 static struct blk_major_name {
245 struct blk_major_name *next;
246 int major;
247 char name[16];
248 } *major_names[BLKDEV_MAJOR_HASH_SIZE];
250 /* index in the above - for now: assume no multimajor ranges */
251 static inline int major_to_index(unsigned major)
253 return major % BLKDEV_MAJOR_HASH_SIZE;
256 #ifdef CONFIG_PROC_FS
257 void blkdev_show(struct seq_file *seqf, off_t offset)
259 struct blk_major_name *dp;
261 if (offset < BLKDEV_MAJOR_HASH_SIZE) {
262 mutex_lock(&block_class_lock);
263 for (dp = major_names[offset]; dp; dp = dp->next)
264 seq_printf(seqf, "%3d %s\n", dp->major, dp->name);
265 mutex_unlock(&block_class_lock);
268 #endif /* CONFIG_PROC_FS */
271 * register_blkdev - register a new block device
273 * @major: the requested major device number [1..255]. If @major=0, try to
274 * allocate any unused major number.
275 * @name: the name of the new block device as a zero terminated string
277 * The @name must be unique within the system.
279 * The return value depends on the @major input parameter.
280 * - if a major device number was requested in range [1..255] then the
281 * function returns zero on success, or a negative error code
282 * - if any unused major number was requested with @major=0 parameter
283 * then the return value is the allocated major number in range
284 * [1..255] or a negative error code otherwise
286 int register_blkdev(unsigned int major, const char *name)
288 struct blk_major_name **n, *p;
289 int index, ret = 0;
291 mutex_lock(&block_class_lock);
293 /* temporary */
294 if (major == 0) {
295 for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) {
296 if (major_names[index] == NULL)
297 break;
300 if (index == 0) {
301 printk("register_blkdev: failed to get major for %s\n",
302 name);
303 ret = -EBUSY;
304 goto out;
306 major = index;
307 ret = major;
310 p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL);
311 if (p == NULL) {
312 ret = -ENOMEM;
313 goto out;
316 p->major = major;
317 strlcpy(p->name, name, sizeof(p->name));
318 p->next = NULL;
319 index = major_to_index(major);
321 for (n = &major_names[index]; *n; n = &(*n)->next) {
322 if ((*n)->major == major)
323 break;
325 if (!*n)
326 *n = p;
327 else
328 ret = -EBUSY;
330 if (ret < 0) {
331 printk("register_blkdev: cannot get major %d for %s\n",
332 major, name);
333 kfree(p);
335 out:
336 mutex_unlock(&block_class_lock);
337 return ret;
340 EXPORT_SYMBOL(register_blkdev);
342 void unregister_blkdev(unsigned int major, const char *name)
344 struct blk_major_name **n;
345 struct blk_major_name *p = NULL;
346 int index = major_to_index(major);
348 mutex_lock(&block_class_lock);
349 for (n = &major_names[index]; *n; n = &(*n)->next)
350 if ((*n)->major == major)
351 break;
352 if (!*n || strcmp((*n)->name, name)) {
353 WARN_ON(1);
354 } else {
355 p = *n;
356 *n = p->next;
358 mutex_unlock(&block_class_lock);
359 kfree(p);
362 EXPORT_SYMBOL(unregister_blkdev);
364 static struct kobj_map *bdev_map;
367 * blk_mangle_minor - scatter minor numbers apart
368 * @minor: minor number to mangle
370 * Scatter consecutively allocated @minor number apart if MANGLE_DEVT
371 * is enabled. Mangling twice gives the original value.
373 * RETURNS:
374 * Mangled value.
376 * CONTEXT:
377 * Don't care.
379 static int blk_mangle_minor(int minor)
381 #ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT
382 int i;
384 for (i = 0; i < MINORBITS / 2; i++) {
385 int low = minor & (1 << i);
386 int high = minor & (1 << (MINORBITS - 1 - i));
387 int distance = MINORBITS - 1 - 2 * i;
389 minor ^= low | high; /* clear both bits */
390 low <<= distance; /* swap the positions */
391 high >>= distance;
392 minor |= low | high; /* and set */
394 #endif
395 return minor;
399 * blk_alloc_devt - allocate a dev_t for a partition
400 * @part: partition to allocate dev_t for
401 * @devt: out parameter for resulting dev_t
403 * Allocate a dev_t for block device.
405 * RETURNS:
406 * 0 on success, allocated dev_t is returned in *@devt. -errno on
407 * failure.
409 * CONTEXT:
410 * Might sleep.
412 int blk_alloc_devt(struct hd_struct *part, dev_t *devt)
414 struct gendisk *disk = part_to_disk(part);
415 int idx;
417 /* in consecutive minor range? */
418 if (part->partno < disk->minors) {
419 *devt = MKDEV(disk->major, disk->first_minor + part->partno);
420 return 0;
423 /* allocate ext devt */
424 idr_preload(GFP_KERNEL);
426 spin_lock_bh(&ext_devt_lock);
427 idx = idr_alloc(&ext_devt_idr, part, 0, NR_EXT_DEVT, GFP_NOWAIT);
428 spin_unlock_bh(&ext_devt_lock);
430 idr_preload_end();
431 if (idx < 0)
432 return idx == -ENOSPC ? -EBUSY : idx;
434 *devt = MKDEV(BLOCK_EXT_MAJOR, blk_mangle_minor(idx));
435 return 0;
439 * blk_free_devt - free a dev_t
440 * @devt: dev_t to free
442 * Free @devt which was allocated using blk_alloc_devt().
444 * CONTEXT:
445 * Might sleep.
447 void blk_free_devt(dev_t devt)
449 if (devt == MKDEV(0, 0))
450 return;
452 if (MAJOR(devt) == BLOCK_EXT_MAJOR) {
453 spin_lock_bh(&ext_devt_lock);
454 idr_remove(&ext_devt_idr, blk_mangle_minor(MINOR(devt)));
455 spin_unlock_bh(&ext_devt_lock);
459 static char *bdevt_str(dev_t devt, char *buf)
461 if (MAJOR(devt) <= 0xff && MINOR(devt) <= 0xff) {
462 char tbuf[BDEVT_SIZE];
463 snprintf(tbuf, BDEVT_SIZE, "%02x%02x", MAJOR(devt), MINOR(devt));
464 snprintf(buf, BDEVT_SIZE, "%-9s", tbuf);
465 } else
466 snprintf(buf, BDEVT_SIZE, "%03x:%05x", MAJOR(devt), MINOR(devt));
468 return buf;
472 * Register device numbers dev..(dev+range-1)
473 * range must be nonzero
474 * The hash chain is sorted on range, so that subranges can override.
476 void blk_register_region(dev_t devt, unsigned long range, struct module *module,
477 struct kobject *(*probe)(dev_t, int *, void *),
478 int (*lock)(dev_t, void *), void *data)
480 kobj_map(bdev_map, devt, range, module, probe, lock, data);
483 EXPORT_SYMBOL(blk_register_region);
485 void blk_unregister_region(dev_t devt, unsigned long range)
487 kobj_unmap(bdev_map, devt, range);
490 EXPORT_SYMBOL(blk_unregister_region);
492 static struct kobject *exact_match(dev_t devt, int *partno, void *data)
494 struct gendisk *p = data;
496 return &disk_to_dev(p)->kobj;
499 static int exact_lock(dev_t devt, void *data)
501 struct gendisk *p = data;
503 if (!get_disk(p))
504 return -1;
505 return 0;
508 static void register_disk(struct gendisk *disk)
510 struct device *ddev = disk_to_dev(disk);
511 struct block_device *bdev;
512 struct disk_part_iter piter;
513 struct hd_struct *part;
514 int err;
516 ddev->parent = disk->driverfs_dev;
518 dev_set_name(ddev, "%s", disk->disk_name);
520 /* delay uevents, until we scanned partition table */
521 dev_set_uevent_suppress(ddev, 1);
523 if (device_add(ddev))
524 return;
525 if (!sysfs_deprecated) {
526 err = sysfs_create_link(block_depr, &ddev->kobj,
527 kobject_name(&ddev->kobj));
528 if (err) {
529 device_del(ddev);
530 return;
535 * avoid probable deadlock caused by allocating memory with
536 * GFP_KERNEL in runtime_resume callback of its all ancestor
537 * devices
539 pm_runtime_set_memalloc_noio(ddev, true);
541 disk->part0.holder_dir = kobject_create_and_add("holders", &ddev->kobj);
542 disk->slave_dir = kobject_create_and_add("slaves", &ddev->kobj);
544 /* No minors to use for partitions */
545 if (!disk_part_scan_enabled(disk))
546 goto exit;
548 /* No such device (e.g., media were just removed) */
549 if (!get_capacity(disk))
550 goto exit;
552 bdev = bdget_disk(disk, 0);
553 if (!bdev)
554 goto exit;
556 bdev->bd_invalidated = 1;
557 err = blkdev_get(bdev, FMODE_READ, NULL);
558 if (err < 0)
559 goto exit;
560 blkdev_put(bdev, FMODE_READ);
562 exit:
563 /* announce disk after possible partitions are created */
564 dev_set_uevent_suppress(ddev, 0);
565 kobject_uevent(&ddev->kobj, KOBJ_ADD);
567 /* announce possible partitions */
568 disk_part_iter_init(&piter, disk, 0);
569 while ((part = disk_part_iter_next(&piter)))
570 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_ADD);
571 disk_part_iter_exit(&piter);
575 * add_disk - add partitioning information to kernel list
576 * @disk: per-device partitioning information
578 * This function registers the partitioning information in @disk
579 * with the kernel.
581 * FIXME: error handling
583 void add_disk(struct gendisk *disk)
585 struct backing_dev_info *bdi;
586 dev_t devt;
587 int retval;
589 /* minors == 0 indicates to use ext devt from part0 and should
590 * be accompanied with EXT_DEVT flag. Make sure all
591 * parameters make sense.
593 WARN_ON(disk->minors && !(disk->major || disk->first_minor));
594 WARN_ON(!disk->minors && !(disk->flags & GENHD_FL_EXT_DEVT));
596 disk->flags |= GENHD_FL_UP;
598 retval = blk_alloc_devt(&disk->part0, &devt);
599 if (retval) {
600 WARN_ON(1);
601 return;
603 disk_to_dev(disk)->devt = devt;
605 /* ->major and ->first_minor aren't supposed to be
606 * dereferenced from here on, but set them just in case.
608 disk->major = MAJOR(devt);
609 disk->first_minor = MINOR(devt);
611 disk_alloc_events(disk);
613 /* Register BDI before referencing it from bdev */
614 bdi = &disk->queue->backing_dev_info;
615 bdi_register_dev(bdi, disk_devt(disk));
617 blk_register_region(disk_devt(disk), disk->minors, NULL,
618 exact_match, exact_lock, disk);
619 register_disk(disk);
620 blk_register_queue(disk);
623 * Take an extra ref on queue which will be put on disk_release()
624 * so that it sticks around as long as @disk is there.
626 WARN_ON_ONCE(!blk_get_queue(disk->queue));
628 retval = sysfs_create_link(&disk_to_dev(disk)->kobj, &bdi->dev->kobj,
629 "bdi");
630 WARN_ON(retval);
632 disk_add_events(disk);
633 blk_integrity_add(disk);
635 EXPORT_SYMBOL(add_disk);
637 void del_gendisk(struct gendisk *disk)
639 struct disk_part_iter piter;
640 struct hd_struct *part;
642 blk_integrity_del(disk);
643 disk_del_events(disk);
645 /* invalidate stuff */
646 disk_part_iter_init(&piter, disk,
647 DISK_PITER_INCL_EMPTY | DISK_PITER_REVERSE);
648 while ((part = disk_part_iter_next(&piter))) {
649 invalidate_partition(disk, part->partno);
650 delete_partition(disk, part->partno);
652 disk_part_iter_exit(&piter);
654 invalidate_partition(disk, 0);
655 set_capacity(disk, 0);
656 disk->flags &= ~GENHD_FL_UP;
658 sysfs_remove_link(&disk_to_dev(disk)->kobj, "bdi");
659 blk_unregister_queue(disk);
660 blk_unregister_region(disk_devt(disk), disk->minors);
662 part_stat_set_all(&disk->part0, 0);
663 disk->part0.stamp = 0;
665 kobject_put(disk->part0.holder_dir);
666 kobject_put(disk->slave_dir);
667 disk->driverfs_dev = NULL;
668 if (!sysfs_deprecated)
669 sysfs_remove_link(block_depr, dev_name(disk_to_dev(disk)));
670 pm_runtime_set_memalloc_noio(disk_to_dev(disk), false);
671 device_del(disk_to_dev(disk));
673 EXPORT_SYMBOL(del_gendisk);
676 * get_gendisk - get partitioning information for a given device
677 * @devt: device to get partitioning information for
678 * @partno: returned partition index
680 * This function gets the structure containing partitioning
681 * information for the given device @devt.
683 struct gendisk *get_gendisk(dev_t devt, int *partno)
685 struct gendisk *disk = NULL;
687 if (MAJOR(devt) != BLOCK_EXT_MAJOR) {
688 struct kobject *kobj;
690 kobj = kobj_lookup(bdev_map, devt, partno);
691 if (kobj)
692 disk = dev_to_disk(kobj_to_dev(kobj));
693 } else {
694 struct hd_struct *part;
696 spin_lock_bh(&ext_devt_lock);
697 part = idr_find(&ext_devt_idr, blk_mangle_minor(MINOR(devt)));
698 if (part && get_disk(part_to_disk(part))) {
699 *partno = part->partno;
700 disk = part_to_disk(part);
702 spin_unlock_bh(&ext_devt_lock);
705 return disk;
707 EXPORT_SYMBOL(get_gendisk);
710 * bdget_disk - do bdget() by gendisk and partition number
711 * @disk: gendisk of interest
712 * @partno: partition number
714 * Find partition @partno from @disk, do bdget() on it.
716 * CONTEXT:
717 * Don't care.
719 * RETURNS:
720 * Resulting block_device on success, NULL on failure.
722 struct block_device *bdget_disk(struct gendisk *disk, int partno)
724 struct hd_struct *part;
725 struct block_device *bdev = NULL;
727 part = disk_get_part(disk, partno);
728 if (part)
729 bdev = bdget(part_devt(part));
730 disk_put_part(part);
732 return bdev;
734 EXPORT_SYMBOL(bdget_disk);
737 * print a full list of all partitions - intended for places where the root
738 * filesystem can't be mounted and thus to give the victim some idea of what
739 * went wrong
741 void __init printk_all_partitions(void)
743 struct class_dev_iter iter;
744 struct device *dev;
746 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
747 while ((dev = class_dev_iter_next(&iter))) {
748 struct gendisk *disk = dev_to_disk(dev);
749 struct disk_part_iter piter;
750 struct hd_struct *part;
751 char name_buf[BDEVNAME_SIZE];
752 char devt_buf[BDEVT_SIZE];
755 * Don't show empty devices or things that have been
756 * suppressed
758 if (get_capacity(disk) == 0 ||
759 (disk->flags & GENHD_FL_SUPPRESS_PARTITION_INFO))
760 continue;
763 * Note, unlike /proc/partitions, I am showing the
764 * numbers in hex - the same format as the root=
765 * option takes.
767 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
768 while ((part = disk_part_iter_next(&piter))) {
769 bool is_part0 = part == &disk->part0;
771 printk("%s%s %10llu %s %s", is_part0 ? "" : " ",
772 bdevt_str(part_devt(part), devt_buf),
773 (unsigned long long)part_nr_sects_read(part) >> 1
774 , disk_name(disk, part->partno, name_buf),
775 part->info ? part->info->uuid : "");
776 if (is_part0) {
777 if (disk->driverfs_dev != NULL &&
778 disk->driverfs_dev->driver != NULL)
779 printk(" driver: %s\n",
780 disk->driverfs_dev->driver->name);
781 else
782 printk(" (driver?)\n");
783 } else
784 printk("\n");
786 disk_part_iter_exit(&piter);
788 class_dev_iter_exit(&iter);
791 #ifdef CONFIG_PROC_FS
792 /* iterator */
793 static void *disk_seqf_start(struct seq_file *seqf, loff_t *pos)
795 loff_t skip = *pos;
796 struct class_dev_iter *iter;
797 struct device *dev;
799 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
800 if (!iter)
801 return ERR_PTR(-ENOMEM);
803 seqf->private = iter;
804 class_dev_iter_init(iter, &block_class, NULL, &disk_type);
805 do {
806 dev = class_dev_iter_next(iter);
807 if (!dev)
808 return NULL;
809 } while (skip--);
811 return dev_to_disk(dev);
814 static void *disk_seqf_next(struct seq_file *seqf, void *v, loff_t *pos)
816 struct device *dev;
818 (*pos)++;
819 dev = class_dev_iter_next(seqf->private);
820 if (dev)
821 return dev_to_disk(dev);
823 return NULL;
826 static void disk_seqf_stop(struct seq_file *seqf, void *v)
828 struct class_dev_iter *iter = seqf->private;
830 /* stop is called even after start failed :-( */
831 if (iter) {
832 class_dev_iter_exit(iter);
833 kfree(iter);
837 static void *show_partition_start(struct seq_file *seqf, loff_t *pos)
839 void *p;
841 p = disk_seqf_start(seqf, pos);
842 if (!IS_ERR_OR_NULL(p) && !*pos)
843 seq_puts(seqf, "major minor #blocks name\n\n");
844 return p;
847 static int show_partition(struct seq_file *seqf, void *v)
849 struct gendisk *sgp = v;
850 struct disk_part_iter piter;
851 struct hd_struct *part;
852 char buf[BDEVNAME_SIZE];
854 /* Don't show non-partitionable removeable devices or empty devices */
855 if (!get_capacity(sgp) || (!disk_max_parts(sgp) &&
856 (sgp->flags & GENHD_FL_REMOVABLE)))
857 return 0;
858 if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)
859 return 0;
861 /* show the full disk and all non-0 size partitions of it */
862 disk_part_iter_init(&piter, sgp, DISK_PITER_INCL_PART0);
863 while ((part = disk_part_iter_next(&piter)))
864 seq_printf(seqf, "%4d %7d %10llu %s\n",
865 MAJOR(part_devt(part)), MINOR(part_devt(part)),
866 (unsigned long long)part_nr_sects_read(part) >> 1,
867 disk_name(sgp, part->partno, buf));
868 disk_part_iter_exit(&piter);
870 return 0;
873 static const struct seq_operations partitions_op = {
874 .start = show_partition_start,
875 .next = disk_seqf_next,
876 .stop = disk_seqf_stop,
877 .show = show_partition
880 static int partitions_open(struct inode *inode, struct file *file)
882 return seq_open(file, &partitions_op);
885 static const struct file_operations proc_partitions_operations = {
886 .open = partitions_open,
887 .read = seq_read,
888 .llseek = seq_lseek,
889 .release = seq_release,
891 #endif
894 static struct kobject *base_probe(dev_t devt, int *partno, void *data)
896 if (request_module("block-major-%d-%d", MAJOR(devt), MINOR(devt)) > 0)
897 /* Make old-style 2.4 aliases work */
898 request_module("block-major-%d", MAJOR(devt));
899 return NULL;
902 static int __init genhd_device_init(void)
904 int error;
906 block_class.dev_kobj = sysfs_dev_block_kobj;
907 error = class_register(&block_class);
908 if (unlikely(error))
909 return error;
910 bdev_map = kobj_map_init(base_probe, &block_class_lock);
911 blk_dev_init();
913 register_blkdev(BLOCK_EXT_MAJOR, "blkext");
915 /* create top-level block dir */
916 if (!sysfs_deprecated)
917 block_depr = kobject_create_and_add("block", NULL);
918 return 0;
921 subsys_initcall(genhd_device_init);
923 static ssize_t disk_range_show(struct device *dev,
924 struct device_attribute *attr, char *buf)
926 struct gendisk *disk = dev_to_disk(dev);
928 return sprintf(buf, "%d\n", disk->minors);
931 static ssize_t disk_ext_range_show(struct device *dev,
932 struct device_attribute *attr, char *buf)
934 struct gendisk *disk = dev_to_disk(dev);
936 return sprintf(buf, "%d\n", disk_max_parts(disk));
939 static ssize_t disk_removable_show(struct device *dev,
940 struct device_attribute *attr, char *buf)
942 struct gendisk *disk = dev_to_disk(dev);
944 return sprintf(buf, "%d\n",
945 (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
948 static ssize_t disk_ro_show(struct device *dev,
949 struct device_attribute *attr, char *buf)
951 struct gendisk *disk = dev_to_disk(dev);
953 return sprintf(buf, "%d\n", get_disk_ro(disk) ? 1 : 0);
956 static ssize_t disk_capability_show(struct device *dev,
957 struct device_attribute *attr, char *buf)
959 struct gendisk *disk = dev_to_disk(dev);
961 return sprintf(buf, "%x\n", disk->flags);
964 static ssize_t disk_alignment_offset_show(struct device *dev,
965 struct device_attribute *attr,
966 char *buf)
968 struct gendisk *disk = dev_to_disk(dev);
970 return sprintf(buf, "%d\n", queue_alignment_offset(disk->queue));
973 static ssize_t disk_discard_alignment_show(struct device *dev,
974 struct device_attribute *attr,
975 char *buf)
977 struct gendisk *disk = dev_to_disk(dev);
979 return sprintf(buf, "%d\n", queue_discard_alignment(disk->queue));
982 static DEVICE_ATTR(range, S_IRUGO, disk_range_show, NULL);
983 static DEVICE_ATTR(ext_range, S_IRUGO, disk_ext_range_show, NULL);
984 static DEVICE_ATTR(removable, S_IRUGO, disk_removable_show, NULL);
985 static DEVICE_ATTR(ro, S_IRUGO, disk_ro_show, NULL);
986 static DEVICE_ATTR(size, S_IRUGO, part_size_show, NULL);
987 static DEVICE_ATTR(alignment_offset, S_IRUGO, disk_alignment_offset_show, NULL);
988 static DEVICE_ATTR(discard_alignment, S_IRUGO, disk_discard_alignment_show,
989 NULL);
990 static DEVICE_ATTR(capability, S_IRUGO, disk_capability_show, NULL);
991 static DEVICE_ATTR(stat, S_IRUGO, part_stat_show, NULL);
992 static DEVICE_ATTR(inflight, S_IRUGO, part_inflight_show, NULL);
993 #ifdef CONFIG_FAIL_MAKE_REQUEST
994 static struct device_attribute dev_attr_fail =
995 __ATTR(make-it-fail, S_IRUGO|S_IWUSR, part_fail_show, part_fail_store);
996 #endif
997 #ifdef CONFIG_FAIL_IO_TIMEOUT
998 static struct device_attribute dev_attr_fail_timeout =
999 __ATTR(io-timeout-fail, S_IRUGO|S_IWUSR, part_timeout_show,
1000 part_timeout_store);
1001 #endif
1003 static struct attribute *disk_attrs[] = {
1004 &dev_attr_range.attr,
1005 &dev_attr_ext_range.attr,
1006 &dev_attr_removable.attr,
1007 &dev_attr_ro.attr,
1008 &dev_attr_size.attr,
1009 &dev_attr_alignment_offset.attr,
1010 &dev_attr_discard_alignment.attr,
1011 &dev_attr_capability.attr,
1012 &dev_attr_stat.attr,
1013 &dev_attr_inflight.attr,
1014 #ifdef CONFIG_FAIL_MAKE_REQUEST
1015 &dev_attr_fail.attr,
1016 #endif
1017 #ifdef CONFIG_FAIL_IO_TIMEOUT
1018 &dev_attr_fail_timeout.attr,
1019 #endif
1020 NULL
1023 static struct attribute_group disk_attr_group = {
1024 .attrs = disk_attrs,
1027 static const struct attribute_group *disk_attr_groups[] = {
1028 &disk_attr_group,
1029 NULL
1033 * disk_replace_part_tbl - replace disk->part_tbl in RCU-safe way
1034 * @disk: disk to replace part_tbl for
1035 * @new_ptbl: new part_tbl to install
1037 * Replace disk->part_tbl with @new_ptbl in RCU-safe way. The
1038 * original ptbl is freed using RCU callback.
1040 * LOCKING:
1041 * Matching bd_mutx locked.
1043 static void disk_replace_part_tbl(struct gendisk *disk,
1044 struct disk_part_tbl *new_ptbl)
1046 struct disk_part_tbl *old_ptbl = disk->part_tbl;
1048 rcu_assign_pointer(disk->part_tbl, new_ptbl);
1050 if (old_ptbl) {
1051 rcu_assign_pointer(old_ptbl->last_lookup, NULL);
1052 kfree_rcu(old_ptbl, rcu_head);
1057 * disk_expand_part_tbl - expand disk->part_tbl
1058 * @disk: disk to expand part_tbl for
1059 * @partno: expand such that this partno can fit in
1061 * Expand disk->part_tbl such that @partno can fit in. disk->part_tbl
1062 * uses RCU to allow unlocked dereferencing for stats and other stuff.
1064 * LOCKING:
1065 * Matching bd_mutex locked, might sleep.
1067 * RETURNS:
1068 * 0 on success, -errno on failure.
1070 int disk_expand_part_tbl(struct gendisk *disk, int partno)
1072 struct disk_part_tbl *old_ptbl = disk->part_tbl;
1073 struct disk_part_tbl *new_ptbl;
1074 int len = old_ptbl ? old_ptbl->len : 0;
1075 int i, target;
1076 size_t size;
1079 * check for int overflow, since we can get here from blkpg_ioctl()
1080 * with a user passed 'partno'.
1082 target = partno + 1;
1083 if (target < 0)
1084 return -EINVAL;
1086 /* disk_max_parts() is zero during initialization, ignore if so */
1087 if (disk_max_parts(disk) && target > disk_max_parts(disk))
1088 return -EINVAL;
1090 if (target <= len)
1091 return 0;
1093 size = sizeof(*new_ptbl) + target * sizeof(new_ptbl->part[0]);
1094 new_ptbl = kzalloc_node(size, GFP_KERNEL, disk->node_id);
1095 if (!new_ptbl)
1096 return -ENOMEM;
1098 new_ptbl->len = target;
1100 for (i = 0; i < len; i++)
1101 rcu_assign_pointer(new_ptbl->part[i], old_ptbl->part[i]);
1103 disk_replace_part_tbl(disk, new_ptbl);
1104 return 0;
1107 static void disk_release(struct device *dev)
1109 struct gendisk *disk = dev_to_disk(dev);
1111 blk_free_devt(dev->devt);
1112 disk_release_events(disk);
1113 kfree(disk->random);
1114 disk_replace_part_tbl(disk, NULL);
1115 hd_free_part(&disk->part0);
1116 if (disk->queue)
1117 blk_put_queue(disk->queue);
1118 kfree(disk);
1120 struct class block_class = {
1121 .name = "block",
1124 static char *block_devnode(struct device *dev, umode_t *mode,
1125 kuid_t *uid, kgid_t *gid)
1127 struct gendisk *disk = dev_to_disk(dev);
1129 if (disk->devnode)
1130 return disk->devnode(disk, mode);
1131 return NULL;
1134 static struct device_type disk_type = {
1135 .name = "disk",
1136 .groups = disk_attr_groups,
1137 .release = disk_release,
1138 .devnode = block_devnode,
1141 #ifdef CONFIG_PROC_FS
1143 * aggregate disk stat collector. Uses the same stats that the sysfs
1144 * entries do, above, but makes them available through one seq_file.
1146 * The output looks suspiciously like /proc/partitions with a bunch of
1147 * extra fields.
1149 static int diskstats_show(struct seq_file *seqf, void *v)
1151 struct gendisk *gp = v;
1152 struct disk_part_iter piter;
1153 struct hd_struct *hd;
1154 char buf[BDEVNAME_SIZE];
1155 int cpu;
1158 if (&disk_to_dev(gp)->kobj.entry == block_class.devices.next)
1159 seq_puts(seqf, "major minor name"
1160 " rio rmerge rsect ruse wio wmerge "
1161 "wsect wuse running use aveq"
1162 "\n\n");
1165 disk_part_iter_init(&piter, gp, DISK_PITER_INCL_EMPTY_PART0);
1166 while ((hd = disk_part_iter_next(&piter))) {
1167 cpu = part_stat_lock();
1168 part_round_stats(cpu, hd);
1169 part_stat_unlock();
1170 seq_printf(seqf, "%4d %7d %s %lu %lu %lu "
1171 "%u %lu %lu %lu %u %u %u %u\n",
1172 MAJOR(part_devt(hd)), MINOR(part_devt(hd)),
1173 disk_name(gp, hd->partno, buf),
1174 part_stat_read(hd, ios[READ]),
1175 part_stat_read(hd, merges[READ]),
1176 part_stat_read(hd, sectors[READ]),
1177 jiffies_to_msecs(part_stat_read(hd, ticks[READ])),
1178 part_stat_read(hd, ios[WRITE]),
1179 part_stat_read(hd, merges[WRITE]),
1180 part_stat_read(hd, sectors[WRITE]),
1181 jiffies_to_msecs(part_stat_read(hd, ticks[WRITE])),
1182 part_in_flight(hd),
1183 jiffies_to_msecs(part_stat_read(hd, io_ticks)),
1184 jiffies_to_msecs(part_stat_read(hd, time_in_queue))
1187 disk_part_iter_exit(&piter);
1189 return 0;
1192 static const struct seq_operations diskstats_op = {
1193 .start = disk_seqf_start,
1194 .next = disk_seqf_next,
1195 .stop = disk_seqf_stop,
1196 .show = diskstats_show
1199 static int diskstats_open(struct inode *inode, struct file *file)
1201 return seq_open(file, &diskstats_op);
1204 static const struct file_operations proc_diskstats_operations = {
1205 .open = diskstats_open,
1206 .read = seq_read,
1207 .llseek = seq_lseek,
1208 .release = seq_release,
1211 static int __init proc_genhd_init(void)
1213 proc_create("diskstats", 0, NULL, &proc_diskstats_operations);
1214 proc_create("partitions", 0, NULL, &proc_partitions_operations);
1215 return 0;
1217 module_init(proc_genhd_init);
1218 #endif /* CONFIG_PROC_FS */
1220 dev_t blk_lookup_devt(const char *name, int partno)
1222 dev_t devt = MKDEV(0, 0);
1223 struct class_dev_iter iter;
1224 struct device *dev;
1226 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
1227 while ((dev = class_dev_iter_next(&iter))) {
1228 struct gendisk *disk = dev_to_disk(dev);
1229 struct hd_struct *part;
1231 if (strcmp(dev_name(dev), name))
1232 continue;
1234 if (partno < disk->minors) {
1235 /* We need to return the right devno, even
1236 * if the partition doesn't exist yet.
1238 devt = MKDEV(MAJOR(dev->devt),
1239 MINOR(dev->devt) + partno);
1240 break;
1242 part = disk_get_part(disk, partno);
1243 if (part) {
1244 devt = part_devt(part);
1245 disk_put_part(part);
1246 break;
1248 disk_put_part(part);
1250 class_dev_iter_exit(&iter);
1251 return devt;
1253 EXPORT_SYMBOL(blk_lookup_devt);
1255 struct gendisk *alloc_disk(int minors)
1257 return alloc_disk_node(minors, NUMA_NO_NODE);
1259 EXPORT_SYMBOL(alloc_disk);
1261 struct gendisk *alloc_disk_node(int minors, int node_id)
1263 struct gendisk *disk;
1265 disk = kzalloc_node(sizeof(struct gendisk), GFP_KERNEL, node_id);
1266 if (disk) {
1267 if (!init_part_stats(&disk->part0)) {
1268 kfree(disk);
1269 return NULL;
1271 disk->node_id = node_id;
1272 if (disk_expand_part_tbl(disk, 0)) {
1273 free_part_stats(&disk->part0);
1274 kfree(disk);
1275 return NULL;
1277 disk->part_tbl->part[0] = &disk->part0;
1280 * set_capacity() and get_capacity() currently don't use
1281 * seqcounter to read/update the part0->nr_sects. Still init
1282 * the counter as we can read the sectors in IO submission
1283 * patch using seqence counters.
1285 * TODO: Ideally set_capacity() and get_capacity() should be
1286 * converted to make use of bd_mutex and sequence counters.
1288 seqcount_init(&disk->part0.nr_sects_seq);
1289 if (hd_ref_init(&disk->part0)) {
1290 hd_free_part(&disk->part0);
1291 kfree(disk);
1292 return NULL;
1295 disk->minors = minors;
1296 rand_initialize_disk(disk);
1297 disk_to_dev(disk)->class = &block_class;
1298 disk_to_dev(disk)->type = &disk_type;
1299 device_initialize(disk_to_dev(disk));
1301 return disk;
1303 EXPORT_SYMBOL(alloc_disk_node);
1305 struct kobject *get_disk(struct gendisk *disk)
1307 struct module *owner;
1308 struct kobject *kobj;
1310 if (!disk->fops)
1311 return NULL;
1312 owner = disk->fops->owner;
1313 if (owner && !try_module_get(owner))
1314 return NULL;
1315 kobj = kobject_get(&disk_to_dev(disk)->kobj);
1316 if (kobj == NULL) {
1317 module_put(owner);
1318 return NULL;
1320 return kobj;
1324 EXPORT_SYMBOL(get_disk);
1326 void put_disk(struct gendisk *disk)
1328 if (disk)
1329 kobject_put(&disk_to_dev(disk)->kobj);
1332 EXPORT_SYMBOL(put_disk);
1334 static void set_disk_ro_uevent(struct gendisk *gd, int ro)
1336 char event[] = "DISK_RO=1";
1337 char *envp[] = { event, NULL };
1339 if (!ro)
1340 event[8] = '0';
1341 kobject_uevent_env(&disk_to_dev(gd)->kobj, KOBJ_CHANGE, envp);
1344 void set_device_ro(struct block_device *bdev, int flag)
1346 bdev->bd_part->policy = flag;
1349 EXPORT_SYMBOL(set_device_ro);
1351 void set_disk_ro(struct gendisk *disk, int flag)
1353 struct disk_part_iter piter;
1354 struct hd_struct *part;
1356 if (disk->part0.policy != flag) {
1357 set_disk_ro_uevent(disk, flag);
1358 disk->part0.policy = flag;
1361 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_EMPTY);
1362 while ((part = disk_part_iter_next(&piter)))
1363 part->policy = flag;
1364 disk_part_iter_exit(&piter);
1367 EXPORT_SYMBOL(set_disk_ro);
1369 int bdev_read_only(struct block_device *bdev)
1371 if (!bdev)
1372 return 0;
1373 return bdev->bd_part->policy;
1376 EXPORT_SYMBOL(bdev_read_only);
1378 int invalidate_partition(struct gendisk *disk, int partno)
1380 int res = 0;
1381 struct block_device *bdev = bdget_disk(disk, partno);
1382 if (bdev) {
1383 fsync_bdev(bdev);
1384 res = __invalidate_device(bdev, true);
1385 bdput(bdev);
1387 return res;
1390 EXPORT_SYMBOL(invalidate_partition);
1393 * Disk events - monitor disk events like media change and eject request.
1395 struct disk_events {
1396 struct list_head node; /* all disk_event's */
1397 struct gendisk *disk; /* the associated disk */
1398 spinlock_t lock;
1400 struct mutex block_mutex; /* protects blocking */
1401 int block; /* event blocking depth */
1402 unsigned int pending; /* events already sent out */
1403 unsigned int clearing; /* events being cleared */
1405 long poll_msecs; /* interval, -1 for default */
1406 struct delayed_work dwork;
1409 static const char *disk_events_strs[] = {
1410 [ilog2(DISK_EVENT_MEDIA_CHANGE)] = "media_change",
1411 [ilog2(DISK_EVENT_EJECT_REQUEST)] = "eject_request",
1414 static char *disk_uevents[] = {
1415 [ilog2(DISK_EVENT_MEDIA_CHANGE)] = "DISK_MEDIA_CHANGE=1",
1416 [ilog2(DISK_EVENT_EJECT_REQUEST)] = "DISK_EJECT_REQUEST=1",
1419 /* list of all disk_events */
1420 static DEFINE_MUTEX(disk_events_mutex);
1421 static LIST_HEAD(disk_events);
1423 /* disable in-kernel polling by default */
1424 static unsigned long disk_events_dfl_poll_msecs = 0;
1426 static unsigned long disk_events_poll_jiffies(struct gendisk *disk)
1428 struct disk_events *ev = disk->ev;
1429 long intv_msecs = 0;
1432 * If device-specific poll interval is set, always use it. If
1433 * the default is being used, poll iff there are events which
1434 * can't be monitored asynchronously.
1436 if (ev->poll_msecs >= 0)
1437 intv_msecs = ev->poll_msecs;
1438 else if (disk->events & ~disk->async_events)
1439 intv_msecs = disk_events_dfl_poll_msecs;
1441 return msecs_to_jiffies(intv_msecs);
1445 * disk_block_events - block and flush disk event checking
1446 * @disk: disk to block events for
1448 * On return from this function, it is guaranteed that event checking
1449 * isn't in progress and won't happen until unblocked by
1450 * disk_unblock_events(). Events blocking is counted and the actual
1451 * unblocking happens after the matching number of unblocks are done.
1453 * Note that this intentionally does not block event checking from
1454 * disk_clear_events().
1456 * CONTEXT:
1457 * Might sleep.
1459 void disk_block_events(struct gendisk *disk)
1461 struct disk_events *ev = disk->ev;
1462 unsigned long flags;
1463 bool cancel;
1465 if (!ev)
1466 return;
1469 * Outer mutex ensures that the first blocker completes canceling
1470 * the event work before further blockers are allowed to finish.
1472 mutex_lock(&ev->block_mutex);
1474 spin_lock_irqsave(&ev->lock, flags);
1475 cancel = !ev->block++;
1476 spin_unlock_irqrestore(&ev->lock, flags);
1478 if (cancel)
1479 cancel_delayed_work_sync(&disk->ev->dwork);
1481 mutex_unlock(&ev->block_mutex);
1484 static void __disk_unblock_events(struct gendisk *disk, bool check_now)
1486 struct disk_events *ev = disk->ev;
1487 unsigned long intv;
1488 unsigned long flags;
1490 spin_lock_irqsave(&ev->lock, flags);
1492 if (WARN_ON_ONCE(ev->block <= 0))
1493 goto out_unlock;
1495 if (--ev->block)
1496 goto out_unlock;
1499 * Not exactly a latency critical operation, set poll timer
1500 * slack to 25% and kick event check.
1502 intv = disk_events_poll_jiffies(disk);
1503 set_timer_slack(&ev->dwork.timer, intv / 4);
1504 if (check_now)
1505 queue_delayed_work(system_freezable_power_efficient_wq,
1506 &ev->dwork, 0);
1507 else if (intv)
1508 queue_delayed_work(system_freezable_power_efficient_wq,
1509 &ev->dwork, intv);
1510 out_unlock:
1511 spin_unlock_irqrestore(&ev->lock, flags);
1515 * disk_unblock_events - unblock disk event checking
1516 * @disk: disk to unblock events for
1518 * Undo disk_block_events(). When the block count reaches zero, it
1519 * starts events polling if configured.
1521 * CONTEXT:
1522 * Don't care. Safe to call from irq context.
1524 void disk_unblock_events(struct gendisk *disk)
1526 if (disk->ev)
1527 __disk_unblock_events(disk, false);
1531 * disk_flush_events - schedule immediate event checking and flushing
1532 * @disk: disk to check and flush events for
1533 * @mask: events to flush
1535 * Schedule immediate event checking on @disk if not blocked. Events in
1536 * @mask are scheduled to be cleared from the driver. Note that this
1537 * doesn't clear the events from @disk->ev.
1539 * CONTEXT:
1540 * If @mask is non-zero must be called with bdev->bd_mutex held.
1542 void disk_flush_events(struct gendisk *disk, unsigned int mask)
1544 struct disk_events *ev = disk->ev;
1546 if (!ev)
1547 return;
1549 spin_lock_irq(&ev->lock);
1550 ev->clearing |= mask;
1551 if (!ev->block)
1552 mod_delayed_work(system_freezable_power_efficient_wq,
1553 &ev->dwork, 0);
1554 spin_unlock_irq(&ev->lock);
1558 * disk_clear_events - synchronously check, clear and return pending events
1559 * @disk: disk to fetch and clear events from
1560 * @mask: mask of events to be fetched and cleared
1562 * Disk events are synchronously checked and pending events in @mask
1563 * are cleared and returned. This ignores the block count.
1565 * CONTEXT:
1566 * Might sleep.
1568 unsigned int disk_clear_events(struct gendisk *disk, unsigned int mask)
1570 const struct block_device_operations *bdops = disk->fops;
1571 struct disk_events *ev = disk->ev;
1572 unsigned int pending;
1573 unsigned int clearing = mask;
1575 if (!ev) {
1576 /* for drivers still using the old ->media_changed method */
1577 if ((mask & DISK_EVENT_MEDIA_CHANGE) &&
1578 bdops->media_changed && bdops->media_changed(disk))
1579 return DISK_EVENT_MEDIA_CHANGE;
1580 return 0;
1583 disk_block_events(disk);
1586 * store the union of mask and ev->clearing on the stack so that the
1587 * race with disk_flush_events does not cause ambiguity (ev->clearing
1588 * can still be modified even if events are blocked).
1590 spin_lock_irq(&ev->lock);
1591 clearing |= ev->clearing;
1592 ev->clearing = 0;
1593 spin_unlock_irq(&ev->lock);
1595 disk_check_events(ev, &clearing);
1597 * if ev->clearing is not 0, the disk_flush_events got called in the
1598 * middle of this function, so we want to run the workfn without delay.
1600 __disk_unblock_events(disk, ev->clearing ? true : false);
1602 /* then, fetch and clear pending events */
1603 spin_lock_irq(&ev->lock);
1604 pending = ev->pending & mask;
1605 ev->pending &= ~mask;
1606 spin_unlock_irq(&ev->lock);
1607 WARN_ON_ONCE(clearing & mask);
1609 return pending;
1613 * Separate this part out so that a different pointer for clearing_ptr can be
1614 * passed in for disk_clear_events.
1616 static void disk_events_workfn(struct work_struct *work)
1618 struct delayed_work *dwork = to_delayed_work(work);
1619 struct disk_events *ev = container_of(dwork, struct disk_events, dwork);
1621 disk_check_events(ev, &ev->clearing);
1624 static void disk_check_events(struct disk_events *ev,
1625 unsigned int *clearing_ptr)
1627 struct gendisk *disk = ev->disk;
1628 char *envp[ARRAY_SIZE(disk_uevents) + 1] = { };
1629 unsigned int clearing = *clearing_ptr;
1630 unsigned int events;
1631 unsigned long intv;
1632 int nr_events = 0, i;
1634 /* check events */
1635 events = disk->fops->check_events(disk, clearing);
1637 /* accumulate pending events and schedule next poll if necessary */
1638 spin_lock_irq(&ev->lock);
1640 events &= ~ev->pending;
1641 ev->pending |= events;
1642 *clearing_ptr &= ~clearing;
1644 intv = disk_events_poll_jiffies(disk);
1645 if (!ev->block && intv)
1646 queue_delayed_work(system_freezable_power_efficient_wq,
1647 &ev->dwork, intv);
1649 spin_unlock_irq(&ev->lock);
1652 * Tell userland about new events. Only the events listed in
1653 * @disk->events are reported. Unlisted events are processed the
1654 * same internally but never get reported to userland.
1656 for (i = 0; i < ARRAY_SIZE(disk_uevents); i++)
1657 if (events & disk->events & (1 << i))
1658 envp[nr_events++] = disk_uevents[i];
1660 if (nr_events)
1661 kobject_uevent_env(&disk_to_dev(disk)->kobj, KOBJ_CHANGE, envp);
1665 * A disk events enabled device has the following sysfs nodes under
1666 * its /sys/block/X/ directory.
1668 * events : list of all supported events
1669 * events_async : list of events which can be detected w/o polling
1670 * events_poll_msecs : polling interval, 0: disable, -1: system default
1672 static ssize_t __disk_events_show(unsigned int events, char *buf)
1674 const char *delim = "";
1675 ssize_t pos = 0;
1676 int i;
1678 for (i = 0; i < ARRAY_SIZE(disk_events_strs); i++)
1679 if (events & (1 << i)) {
1680 pos += sprintf(buf + pos, "%s%s",
1681 delim, disk_events_strs[i]);
1682 delim = " ";
1684 if (pos)
1685 pos += sprintf(buf + pos, "\n");
1686 return pos;
1689 static ssize_t disk_events_show(struct device *dev,
1690 struct device_attribute *attr, char *buf)
1692 struct gendisk *disk = dev_to_disk(dev);
1694 return __disk_events_show(disk->events, buf);
1697 static ssize_t disk_events_async_show(struct device *dev,
1698 struct device_attribute *attr, char *buf)
1700 struct gendisk *disk = dev_to_disk(dev);
1702 return __disk_events_show(disk->async_events, buf);
1705 static ssize_t disk_events_poll_msecs_show(struct device *dev,
1706 struct device_attribute *attr,
1707 char *buf)
1709 struct gendisk *disk = dev_to_disk(dev);
1711 return sprintf(buf, "%ld\n", disk->ev->poll_msecs);
1714 static ssize_t disk_events_poll_msecs_store(struct device *dev,
1715 struct device_attribute *attr,
1716 const char *buf, size_t count)
1718 struct gendisk *disk = dev_to_disk(dev);
1719 long intv;
1721 if (!count || !sscanf(buf, "%ld", &intv))
1722 return -EINVAL;
1724 if (intv < 0 && intv != -1)
1725 return -EINVAL;
1727 disk_block_events(disk);
1728 disk->ev->poll_msecs = intv;
1729 __disk_unblock_events(disk, true);
1731 return count;
1734 static const DEVICE_ATTR(events, S_IRUGO, disk_events_show, NULL);
1735 static const DEVICE_ATTR(events_async, S_IRUGO, disk_events_async_show, NULL);
1736 static const DEVICE_ATTR(events_poll_msecs, S_IRUGO|S_IWUSR,
1737 disk_events_poll_msecs_show,
1738 disk_events_poll_msecs_store);
1740 static const struct attribute *disk_events_attrs[] = {
1741 &dev_attr_events.attr,
1742 &dev_attr_events_async.attr,
1743 &dev_attr_events_poll_msecs.attr,
1744 NULL,
1748 * The default polling interval can be specified by the kernel
1749 * parameter block.events_dfl_poll_msecs which defaults to 0
1750 * (disable). This can also be modified runtime by writing to
1751 * /sys/module/block/events_dfl_poll_msecs.
1753 static int disk_events_set_dfl_poll_msecs(const char *val,
1754 const struct kernel_param *kp)
1756 struct disk_events *ev;
1757 int ret;
1759 ret = param_set_ulong(val, kp);
1760 if (ret < 0)
1761 return ret;
1763 mutex_lock(&disk_events_mutex);
1765 list_for_each_entry(ev, &disk_events, node)
1766 disk_flush_events(ev->disk, 0);
1768 mutex_unlock(&disk_events_mutex);
1770 return 0;
1773 static const struct kernel_param_ops disk_events_dfl_poll_msecs_param_ops = {
1774 .set = disk_events_set_dfl_poll_msecs,
1775 .get = param_get_ulong,
1778 #undef MODULE_PARAM_PREFIX
1779 #define MODULE_PARAM_PREFIX "block."
1781 module_param_cb(events_dfl_poll_msecs, &disk_events_dfl_poll_msecs_param_ops,
1782 &disk_events_dfl_poll_msecs, 0644);
1785 * disk_{alloc|add|del|release}_events - initialize and destroy disk_events.
1787 static void disk_alloc_events(struct gendisk *disk)
1789 struct disk_events *ev;
1791 if (!disk->fops->check_events)
1792 return;
1794 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
1795 if (!ev) {
1796 pr_warn("%s: failed to initialize events\n", disk->disk_name);
1797 return;
1800 INIT_LIST_HEAD(&ev->node);
1801 ev->disk = disk;
1802 spin_lock_init(&ev->lock);
1803 mutex_init(&ev->block_mutex);
1804 ev->block = 1;
1805 ev->poll_msecs = -1;
1806 INIT_DELAYED_WORK(&ev->dwork, disk_events_workfn);
1808 disk->ev = ev;
1811 static void disk_add_events(struct gendisk *disk)
1813 if (!disk->ev)
1814 return;
1816 /* FIXME: error handling */
1817 if (sysfs_create_files(&disk_to_dev(disk)->kobj, disk_events_attrs) < 0)
1818 pr_warn("%s: failed to create sysfs files for events\n",
1819 disk->disk_name);
1821 mutex_lock(&disk_events_mutex);
1822 list_add_tail(&disk->ev->node, &disk_events);
1823 mutex_unlock(&disk_events_mutex);
1826 * Block count is initialized to 1 and the following initial
1827 * unblock kicks it into action.
1829 __disk_unblock_events(disk, true);
1832 static void disk_del_events(struct gendisk *disk)
1834 if (!disk->ev)
1835 return;
1837 disk_block_events(disk);
1839 mutex_lock(&disk_events_mutex);
1840 list_del_init(&disk->ev->node);
1841 mutex_unlock(&disk_events_mutex);
1843 sysfs_remove_files(&disk_to_dev(disk)->kobj, disk_events_attrs);
1846 static void disk_release_events(struct gendisk *disk)
1848 /* the block count should be 1 from disk_del_events() */
1849 WARN_ON_ONCE(disk->ev && disk->ev->block != 1);
1850 kfree(disk->ev);