1 /* 2 * gendisk handling 3 */ 4 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> 24 25 #include "blk.h" 26 27 static DEFINE_MUTEX(block_class_lock); 28 struct kobject *block_depr; 29 30 /* for extended dynamic devt allocation, currently only one major is used */ 31 #define NR_EXT_DEVT (1 << MINORBITS) 32 33 /* For extended devt allocation. ext_devt_lock prevents look up 34 * results from going away underneath its user. 35 */ 36 static DEFINE_SPINLOCK(ext_devt_lock); 37 static DEFINE_IDR(ext_devt_idr); 38 39 static const struct device_type disk_type; 40 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); 47 48 /** 49 * disk_get_part - get partition 50 * @disk: disk to look partition from 51 * @partno: partition number 52 * 53 * Look for partition @partno from @disk. If found, increment 54 * reference count and return it. 55 * 56 * CONTEXT: 57 * Don't care. 58 * 59 * RETURNS: 60 * Pointer to the found partition on success, NULL if not found. 61 */ 62 struct hd_struct *disk_get_part(struct gendisk *disk, int partno) 63 { 64 struct hd_struct *part = NULL; 65 struct disk_part_tbl *ptbl; 66 67 if (unlikely(partno < 0)) 68 return NULL; 69 70 rcu_read_lock(); 71 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)); 77 } 78 79 rcu_read_unlock(); 80 81 return part; 82 } 83 EXPORT_SYMBOL_GPL(disk_get_part); 84 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 90 * 91 * Initialize @piter so that it iterates over partitions of @disk. 92 * 93 * CONTEXT: 94 * Don't care. 95 */ 96 void disk_part_iter_init(struct disk_part_iter *piter, struct gendisk *disk, 97 unsigned int flags) 98 { 99 struct disk_part_tbl *ptbl; 100 101 rcu_read_lock(); 102 ptbl = rcu_dereference(disk->part_tbl); 103 104 piter->disk = disk; 105 piter->part = NULL; 106 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; 113 114 piter->flags = flags; 115 116 rcu_read_unlock(); 117 } 118 EXPORT_SYMBOL_GPL(disk_part_iter_init); 119 120 /** 121 * disk_part_iter_next - proceed iterator to the next partition and return it 122 * @piter: iterator of interest 123 * 124 * Proceed @piter to the next partition and return it. 125 * 126 * CONTEXT: 127 * Don't care. 128 */ 129 struct hd_struct *disk_part_iter_next(struct disk_part_iter *piter) 130 { 131 struct disk_part_tbl *ptbl; 132 int inc, end; 133 134 /* put the last partition */ 135 disk_put_part(piter->part); 136 piter->part = NULL; 137 138 /* get part_tbl */ 139 rcu_read_lock(); 140 ptbl = rcu_dereference(piter->disk->part_tbl); 141 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; 153 } 154 155 /* iterate to the next partition */ 156 for (; piter->idx != end; piter->idx += inc) { 157 struct hd_struct *part; 158 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; 167 168 get_device(part_to_dev(part)); 169 piter->part = part; 170 piter->idx += inc; 171 break; 172 } 173 174 rcu_read_unlock(); 175 176 return piter->part; 177 } 178 EXPORT_SYMBOL_GPL(disk_part_iter_next); 179 180 /** 181 * disk_part_iter_exit - finish up partition iteration 182 * @piter: iter of interest 183 * 184 * Called when iteration is over. Cleans up @piter. 185 * 186 * CONTEXT: 187 * Don't care. 188 */ 189 void disk_part_iter_exit(struct disk_part_iter *piter) 190 { 191 disk_put_part(piter->part); 192 piter->part = NULL; 193 } 194 EXPORT_SYMBOL_GPL(disk_part_iter_exit); 195 196 static inline int sector_in_part(struct hd_struct *part, sector_t sector) 197 { 198 return part->start_sect <= sector && 199 sector < part->start_sect + part_nr_sects_read(part); 200 } 201 202 /** 203 * disk_map_sector_rcu - map sector to partition 204 * @disk: gendisk of interest 205 * @sector: sector to map 206 * 207 * Find out which partition @sector maps to on @disk. This is 208 * primarily used for stats accounting. 209 * 210 * CONTEXT: 211 * RCU read locked. The returned partition pointer is valid only 212 * while preemption is disabled. 213 * 214 * RETURNS: 215 * Found partition on success, part0 is returned if no partition matches 216 */ 217 struct hd_struct *disk_map_sector_rcu(struct gendisk *disk, sector_t sector) 218 { 219 struct disk_part_tbl *ptbl; 220 struct hd_struct *part; 221 int i; 222 223 ptbl = rcu_dereference(disk->part_tbl); 224 225 part = rcu_dereference(ptbl->last_lookup); 226 if (part && sector_in_part(part, sector)) 227 return part; 228 229 for (i = 1; i < ptbl->len; i++) { 230 part = rcu_dereference(ptbl->part[i]); 231 232 if (part && sector_in_part(part, sector)) { 233 rcu_assign_pointer(ptbl->last_lookup, part); 234 return part; 235 } 236 } 237 return &disk->part0; 238 } 239 EXPORT_SYMBOL_GPL(disk_map_sector_rcu); 240 241 /* 242 * Can be deleted altogether. Later. 243 * 244 */ 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]; 251 252 /* index in the above - for now: assume no multimajor ranges */ 253 static inline int major_to_index(unsigned major) 254 { 255 return major % BLKDEV_MAJOR_HASH_SIZE; 256 } 257 258 #ifdef CONFIG_PROC_FS 259 void blkdev_show(struct seq_file *seqf, off_t offset) 260 { 261 struct blk_major_name *dp; 262 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); 268 } 269 #endif /* CONFIG_PROC_FS */ 270 271 /** 272 * register_blkdev - register a new block device 273 * 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 277 * 278 * The @name must be unique within the system. 279 * 280 * The return value depends on the @major input parameter: 281 * 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 287 */ 288 int register_blkdev(unsigned int major, const char *name) 289 { 290 struct blk_major_name **n, *p; 291 int index, ret = 0; 292 293 mutex_lock(&block_class_lock); 294 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; 300 } 301 302 if (index == 0) { 303 printk("register_blkdev: failed to get major for %s\n", 304 name); 305 ret = -EBUSY; 306 goto out; 307 } 308 major = index; 309 ret = major; 310 } 311 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); 315 316 ret = -EINVAL; 317 goto out; 318 } 319 320 p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL); 321 if (p == NULL) { 322 ret = -ENOMEM; 323 goto out; 324 } 325 326 p->major = major; 327 strlcpy(p->name, name, sizeof(p->name)); 328 p->next = NULL; 329 index = major_to_index(major); 330 331 for (n = &major_names[index]; *n; n = &(*n)->next) { 332 if ((*n)->major == major) 333 break; 334 } 335 if (!*n) 336 *n = p; 337 else 338 ret = -EBUSY; 339 340 if (ret < 0) { 341 printk("register_blkdev: cannot get major %d for %s\n", 342 major, name); 343 kfree(p); 344 } 345 out: 346 mutex_unlock(&block_class_lock); 347 return ret; 348 } 349 350 EXPORT_SYMBOL(register_blkdev); 351 352 void unregister_blkdev(unsigned int major, const char *name) 353 { 354 struct blk_major_name **n; 355 struct blk_major_name *p = NULL; 356 int index = major_to_index(major); 357 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; 367 } 368 mutex_unlock(&block_class_lock); 369 kfree(p); 370 } 371 372 EXPORT_SYMBOL(unregister_blkdev); 373 374 static struct kobj_map *bdev_map; 375 376 /** 377 * blk_mangle_minor - scatter minor numbers apart 378 * @minor: minor number to mangle 379 * 380 * Scatter consecutively allocated @minor number apart if MANGLE_DEVT 381 * is enabled. Mangling twice gives the original value. 382 * 383 * RETURNS: 384 * Mangled value. 385 * 386 * CONTEXT: 387 * Don't care. 388 */ 389 static int blk_mangle_minor(int minor) 390 { 391 #ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT 392 int i; 393 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; 398 399 minor ^= low | high; /* clear both bits */ 400 low <<= distance; /* swap the positions */ 401 high >>= distance; 402 minor |= low | high; /* and set */ 403 } 404 #endif 405 return minor; 406 } 407 408 /** 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 412 * 413 * Allocate a dev_t for block device. 414 * 415 * RETURNS: 416 * 0 on success, allocated dev_t is returned in *@devt. -errno on 417 * failure. 418 * 419 * CONTEXT: 420 * Might sleep. 421 */ 422 int blk_alloc_devt(struct hd_struct *part, dev_t *devt) 423 { 424 struct gendisk *disk = part_to_disk(part); 425 int idx; 426 427 /* in consecutive minor range? */ 428 if (part->partno < disk->minors) { 429 *devt = MKDEV(disk->major, disk->first_minor + part->partno); 430 return 0; 431 } 432 433 /* allocate ext devt */ 434 idr_preload(GFP_KERNEL); 435 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); 439 440 idr_preload_end(); 441 if (idx < 0) 442 return idx == -ENOSPC ? -EBUSY : idx; 443 444 *devt = MKDEV(BLOCK_EXT_MAJOR, blk_mangle_minor(idx)); 445 return 0; 446 } 447 448 /** 449 * blk_free_devt - free a dev_t 450 * @devt: dev_t to free 451 * 452 * Free @devt which was allocated using blk_alloc_devt(). 453 * 454 * CONTEXT: 455 * Might sleep. 456 */ 457 void blk_free_devt(dev_t devt) 458 { 459 if (devt == MKDEV(0, 0)) 460 return; 461 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); 466 } 467 } 468 469 static char *bdevt_str(dev_t devt, char *buf) 470 { 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)); 477 478 return buf; 479 } 480 481 /* 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. 485 */ 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) 489 { 490 kobj_map(bdev_map, devt, range, module, probe, lock, data); 491 } 492 493 EXPORT_SYMBOL(blk_register_region); 494 495 void blk_unregister_region(dev_t devt, unsigned long range) 496 { 497 kobj_unmap(bdev_map, devt, range); 498 } 499 500 EXPORT_SYMBOL(blk_unregister_region); 501 502 static struct kobject *exact_match(dev_t devt, int *partno, void *data) 503 { 504 struct gendisk *p = data; 505 506 return &disk_to_dev(p)->kobj; 507 } 508 509 static int exact_lock(dev_t devt, void *data) 510 { 511 struct gendisk *p = data; 512 513 if (!get_disk(p)) 514 return -1; 515 return 0; 516 } 517 518 static void register_disk(struct device *parent, struct gendisk *disk) 519 { 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; 525 526 ddev->parent = parent; 527 528 dev_set_name(ddev, "%s", disk->disk_name); 529 530 /* delay uevents, until we scanned partition table */ 531 dev_set_uevent_suppress(ddev, 1); 532 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; 541 } 542 } 543 544 /* 545 * avoid probable deadlock caused by allocating memory with 546 * GFP_KERNEL in runtime_resume callback of its all ancestor 547 * devices 548 */ 549 pm_runtime_set_memalloc_noio(ddev, true); 550 551 disk->part0.holder_dir = kobject_create_and_add("holders", &ddev->kobj); 552 disk->slave_dir = kobject_create_and_add("slaves", &ddev->kobj); 553 554 /* No minors to use for partitions */ 555 if (!disk_part_scan_enabled(disk)) 556 goto exit; 557 558 /* No such device (e.g., media were just removed) */ 559 if (!get_capacity(disk)) 560 goto exit; 561 562 bdev = bdget_disk(disk, 0); 563 if (!bdev) 564 goto exit; 565 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); 571 572 exit: 573 /* announce disk after possible partitions are created */ 574 dev_set_uevent_suppress(ddev, 0); 575 kobject_uevent(&ddev->kobj, KOBJ_ADD); 576 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); 582 } 583 584 /** 585 * device_add_disk - add partitioning information to kernel list 586 * @parent: parent device for the disk 587 * @disk: per-device partitioning information 588 * 589 * This function registers the partitioning information in @disk 590 * with the kernel. 591 * 592 * FIXME: error handling 593 */ 594 void device_add_disk(struct device *parent, struct gendisk *disk) 595 { 596 struct backing_dev_info *bdi; 597 dev_t devt; 598 int retval; 599 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. 603 */ 604 WARN_ON(disk->minors && !(disk->major || disk->first_minor)); 605 WARN_ON(!disk->minors && !(disk->flags & GENHD_FL_EXT_DEVT)); 606 607 disk->flags |= GENHD_FL_UP; 608 609 retval = blk_alloc_devt(&disk->part0, &devt); 610 if (retval) { 611 WARN_ON(1); 612 return; 613 } 614 disk_to_dev(disk)->devt = devt; 615 616 /* ->major and ->first_minor aren't supposed to be 617 * dereferenced from here on, but set them just in case. 618 */ 619 disk->major = MAJOR(devt); 620 disk->first_minor = MINOR(devt); 621 622 disk_alloc_events(disk); 623 624 /* Register BDI before referencing it from bdev */ 625 bdi = disk->queue->backing_dev_info; 626 bdi_register_owner(bdi, disk_to_dev(disk)); 627 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); 632 633 /* 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. 636 */ 637 WARN_ON_ONCE(!blk_get_queue(disk->queue)); 638 639 retval = sysfs_create_link(&disk_to_dev(disk)->kobj, &bdi->dev->kobj, 640 "bdi"); 641 WARN_ON(retval); 642 643 disk_add_events(disk); 644 blk_integrity_add(disk); 645 } 646 EXPORT_SYMBOL(device_add_disk); 647 648 void del_gendisk(struct gendisk *disk) 649 { 650 struct disk_part_iter piter; 651 struct hd_struct *part; 652 653 blk_integrity_del(disk); 654 disk_del_events(disk); 655 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); 663 } 664 disk_part_iter_exit(&piter); 665 666 invalidate_partition(disk, 0); 667 bdev_unhash_inode(disk_devt(disk)); 668 set_capacity(disk, 0); 669 disk->flags &= ~GENHD_FL_UP; 670 671 sysfs_remove_link(&disk_to_dev(disk)->kobj, "bdi"); 672 if (disk->queue) { 673 /* 674 * Unregister bdi before releasing device numbers (as they can 675 * get reused and we'd get clashes in sysfs). 676 */ 677 bdi_unregister(disk->queue->backing_dev_info); 678 blk_unregister_queue(disk); 679 } else { 680 WARN_ON(1); 681 } 682 blk_unregister_region(disk_devt(disk), disk->minors); 683 684 part_stat_set_all(&disk->part0, 0); 685 disk->part0.stamp = 0; 686 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)); 693 } 694 EXPORT_SYMBOL(del_gendisk); 695 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) 700 { 701 struct gendisk *disk = dev_to_disk(dev); 702 703 if (!disk->bb) 704 return sprintf(page, "\n"); 705 706 return badblocks_show(disk->bb, page, 0); 707 } 708 709 static ssize_t disk_badblocks_store(struct device *dev, 710 struct device_attribute *attr, 711 const char *page, size_t len) 712 { 713 struct gendisk *disk = dev_to_disk(dev); 714 715 if (!disk->bb) 716 return -ENXIO; 717 718 return badblocks_store(disk->bb, page, len, 0); 719 } 720 721 /** 722 * get_gendisk - get partitioning information for a given device 723 * @devt: device to get partitioning information for 724 * @partno: returned partition index 725 * 726 * This function gets the structure containing partitioning 727 * information for the given device @devt. 728 */ 729 struct gendisk *get_gendisk(dev_t devt, int *partno) 730 { 731 struct gendisk *disk = NULL; 732 733 if (MAJOR(devt) != BLOCK_EXT_MAJOR) { 734 struct kobject *kobj; 735 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; 741 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); 747 } 748 spin_unlock_bh(&ext_devt_lock); 749 } 750 751 return disk; 752 } 753 EXPORT_SYMBOL(get_gendisk); 754 755 /** 756 * bdget_disk - do bdget() by gendisk and partition number 757 * @disk: gendisk of interest 758 * @partno: partition number 759 * 760 * Find partition @partno from @disk, do bdget() on it. 761 * 762 * CONTEXT: 763 * Don't care. 764 * 765 * RETURNS: 766 * Resulting block_device on success, NULL on failure. 767 */ 768 struct block_device *bdget_disk(struct gendisk *disk, int partno) 769 { 770 struct hd_struct *part; 771 struct block_device *bdev = NULL; 772 773 part = disk_get_part(disk, partno); 774 if (part) 775 bdev = bdget(part_devt(part)); 776 disk_put_part(part); 777 778 return bdev; 779 } 780 EXPORT_SYMBOL(bdget_disk); 781 782 /* 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 786 */ 787 void __init printk_all_partitions(void) 788 { 789 struct class_dev_iter iter; 790 struct device *dev; 791 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]; 799 800 /* 801 * Don't show empty devices or things that have been 802 * suppressed 803 */ 804 if (get_capacity(disk) == 0 || 805 (disk->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)) 806 continue; 807 808 /* 809 * Note, unlike /proc/partitions, I am showing the 810 * numbers in hex - the same format as the root= 811 * option takes. 812 */ 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; 816 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"); 830 } 831 disk_part_iter_exit(&piter); 832 } 833 class_dev_iter_exit(&iter); 834 } 835 836 #ifdef CONFIG_PROC_FS 837 /* iterator */ 838 static void *disk_seqf_start(struct seq_file *seqf, loff_t *pos) 839 { 840 loff_t skip = *pos; 841 struct class_dev_iter *iter; 842 struct device *dev; 843 844 iter = kmalloc(sizeof(*iter), GFP_KERNEL); 845 if (!iter) 846 return ERR_PTR(-ENOMEM); 847 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--); 855 856 return dev_to_disk(dev); 857 } 858 859 static void *disk_seqf_next(struct seq_file *seqf, void *v, loff_t *pos) 860 { 861 struct device *dev; 862 863 (*pos)++; 864 dev = class_dev_iter_next(seqf->private); 865 if (dev) 866 return dev_to_disk(dev); 867 868 return NULL; 869 } 870 871 static void disk_seqf_stop(struct seq_file *seqf, void *v) 872 { 873 struct class_dev_iter *iter = seqf->private; 874 875 /* stop is called even after start failed :-( */ 876 if (iter) { 877 class_dev_iter_exit(iter); 878 kfree(iter); 879 seqf->private = NULL; 880 } 881 } 882 883 static void *show_partition_start(struct seq_file *seqf, loff_t *pos) 884 { 885 void *p; 886 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; 891 } 892 893 static int show_partition(struct seq_file *seqf, void *v) 894 { 895 struct gendisk *sgp = v; 896 struct disk_part_iter piter; 897 struct hd_struct *part; 898 char buf[BDEVNAME_SIZE]; 899 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; 906 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); 915 916 return 0; 917 } 918 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 924 }; 925 926 static int partitions_open(struct inode *inode, struct file *file) 927 { 928 return seq_open(file, &partitions_op); 929 } 930 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, 936 }; 937 #endif 938 939 940 static struct kobject *base_probe(dev_t devt, int *partno, void *data) 941 { 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; 946 } 947 948 static int __init genhd_device_init(void) 949 { 950 int error; 951 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(); 958 959 register_blkdev(BLOCK_EXT_MAJOR, "blkext"); 960 961 /* create top-level block dir */ 962 if (!sysfs_deprecated) 963 block_depr = kobject_create_and_add("block", NULL); 964 return 0; 965 } 966 967 subsys_initcall(genhd_device_init); 968 969 static ssize_t disk_range_show(struct device *dev, 970 struct device_attribute *attr, char *buf) 971 { 972 struct gendisk *disk = dev_to_disk(dev); 973 974 return sprintf(buf, "%d\n", disk->minors); 975 } 976 977 static ssize_t disk_ext_range_show(struct device *dev, 978 struct device_attribute *attr, char *buf) 979 { 980 struct gendisk *disk = dev_to_disk(dev); 981 982 return sprintf(buf, "%d\n", disk_max_parts(disk)); 983 } 984 985 static ssize_t disk_removable_show(struct device *dev, 986 struct device_attribute *attr, char *buf) 987 { 988 struct gendisk *disk = dev_to_disk(dev); 989 990 return sprintf(buf, "%d\n", 991 (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0)); 992 } 993 994 static ssize_t disk_ro_show(struct device *dev, 995 struct device_attribute *attr, char *buf) 996 { 997 struct gendisk *disk = dev_to_disk(dev); 998 999 return sprintf(buf, "%d\n", get_disk_ro(disk) ? 1 : 0); 1000 } 1001 1002 static ssize_t disk_capability_show(struct device *dev, 1003 struct device_attribute *attr, char *buf) 1004 { 1005 struct gendisk *disk = dev_to_disk(dev); 1006 1007 return sprintf(buf, "%x\n", disk->flags); 1008 } 1009 1010 static ssize_t disk_alignment_offset_show(struct device *dev, 1011 struct device_attribute *attr, 1012 char *buf) 1013 { 1014 struct gendisk *disk = dev_to_disk(dev); 1015 1016 return sprintf(buf, "%d\n", queue_alignment_offset(disk->queue)); 1017 } 1018 1019 static ssize_t disk_discard_alignment_show(struct device *dev, 1020 struct device_attribute *attr, 1021 char *buf) 1022 { 1023 struct gendisk *disk = dev_to_disk(dev); 1024 1025 return sprintf(buf, "%d\n", queue_discard_alignment(disk->queue)); 1026 } 1027 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 1050 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 1070 }; 1071 1072 static umode_t disk_visible(struct kobject *kobj, struct attribute *a, int n) 1073 { 1074 struct device *dev = container_of(kobj, typeof(*dev), kobj); 1075 struct gendisk *disk = dev_to_disk(dev); 1076 1077 if (a == &dev_attr_badblocks.attr && !disk->bb) 1078 return 0; 1079 return a->mode; 1080 } 1081 1082 static struct attribute_group disk_attr_group = { 1083 .attrs = disk_attrs, 1084 .is_visible = disk_visible, 1085 }; 1086 1087 static const struct attribute_group *disk_attr_groups[] = { 1088 &disk_attr_group, 1089 NULL 1090 }; 1091 1092 /** 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 1096 * 1097 * Replace disk->part_tbl with @new_ptbl in RCU-safe way. The 1098 * original ptbl is freed using RCU callback. 1099 * 1100 * LOCKING: 1101 * Matching bd_mutx locked. 1102 */ 1103 static void disk_replace_part_tbl(struct gendisk *disk, 1104 struct disk_part_tbl *new_ptbl) 1105 { 1106 struct disk_part_tbl *old_ptbl = disk->part_tbl; 1107 1108 rcu_assign_pointer(disk->part_tbl, new_ptbl); 1109 1110 if (old_ptbl) { 1111 rcu_assign_pointer(old_ptbl->last_lookup, NULL); 1112 kfree_rcu(old_ptbl, rcu_head); 1113 } 1114 } 1115 1116 /** 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 1120 * 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. 1123 * 1124 * LOCKING: 1125 * Matching bd_mutex locked, might sleep. 1126 * 1127 * RETURNS: 1128 * 0 on success, -errno on failure. 1129 */ 1130 int disk_expand_part_tbl(struct gendisk *disk, int partno) 1131 { 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; 1137 1138 /* 1139 * check for int overflow, since we can get here from blkpg_ioctl() 1140 * with a user passed 'partno'. 1141 */ 1142 target = partno + 1; 1143 if (target < 0) 1144 return -EINVAL; 1145 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; 1149 1150 if (target <= len) 1151 return 0; 1152 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; 1157 1158 new_ptbl->len = target; 1159 1160 for (i = 0; i < len; i++) 1161 rcu_assign_pointer(new_ptbl->part[i], old_ptbl->part[i]); 1162 1163 disk_replace_part_tbl(disk, new_ptbl); 1164 return 0; 1165 } 1166 1167 static void disk_release(struct device *dev) 1168 { 1169 struct gendisk *disk = dev_to_disk(dev); 1170 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); 1179 } 1180 struct class block_class = { 1181 .name = "block", 1182 }; 1183 1184 static char *block_devnode(struct device *dev, umode_t *mode, 1185 kuid_t *uid, kgid_t *gid) 1186 { 1187 struct gendisk *disk = dev_to_disk(dev); 1188 1189 if (disk->devnode) 1190 return disk->devnode(disk, mode); 1191 return NULL; 1192 } 1193 1194 static const struct device_type disk_type = { 1195 .name = "disk", 1196 .groups = disk_attr_groups, 1197 .release = disk_release, 1198 .devnode = block_devnode, 1199 }; 1200 1201 #ifdef CONFIG_PROC_FS 1202 /* 1203 * aggregate disk stat collector. Uses the same stats that the sysfs 1204 * entries do, above, but makes them available through one seq_file. 1205 * 1206 * The output looks suspiciously like /proc/partitions with a bunch of 1207 * extra fields. 1208 */ 1209 static int diskstats_show(struct seq_file *seqf, void *v) 1210 { 1211 struct gendisk *gp = v; 1212 struct disk_part_iter piter; 1213 struct hd_struct *hd; 1214 char buf[BDEVNAME_SIZE]; 1215 int cpu; 1216 1217 /* 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"); 1223 */ 1224 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)) 1245 ); 1246 } 1247 disk_part_iter_exit(&piter); 1248 1249 return 0; 1250 } 1251 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 1257 }; 1258 1259 static int diskstats_open(struct inode *inode, struct file *file) 1260 { 1261 return seq_open(file, &diskstats_op); 1262 } 1263 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, 1269 }; 1270 1271 static int __init proc_genhd_init(void) 1272 { 1273 proc_create("diskstats", 0, NULL, &proc_diskstats_operations); 1274 proc_create("partitions", 0, NULL, &proc_partitions_operations); 1275 return 0; 1276 } 1277 module_init(proc_genhd_init); 1278 #endif /* CONFIG_PROC_FS */ 1279 1280 dev_t blk_lookup_devt(const char *name, int partno) 1281 { 1282 dev_t devt = MKDEV(0, 0); 1283 struct class_dev_iter iter; 1284 struct device *dev; 1285 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; 1290 1291 if (strcmp(dev_name(dev), name)) 1292 continue; 1293 1294 if (partno < disk->minors) { 1295 /* We need to return the right devno, even 1296 * if the partition doesn't exist yet. 1297 */ 1298 devt = MKDEV(MAJOR(dev->devt), 1299 MINOR(dev->devt) + partno); 1300 break; 1301 } 1302 part = disk_get_part(disk, partno); 1303 if (part) { 1304 devt = part_devt(part); 1305 disk_put_part(part); 1306 break; 1307 } 1308 disk_put_part(part); 1309 } 1310 class_dev_iter_exit(&iter); 1311 return devt; 1312 } 1313 EXPORT_SYMBOL(blk_lookup_devt); 1314 1315 struct gendisk *alloc_disk(int minors) 1316 { 1317 return alloc_disk_node(minors, NUMA_NO_NODE); 1318 } 1319 EXPORT_SYMBOL(alloc_disk); 1320 1321 struct gendisk *alloc_disk_node(int minors, int node_id) 1322 { 1323 struct gendisk *disk; 1324 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; 1330 } 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; 1336 } 1337 disk->part_tbl->part[0] = &disk->part0; 1338 1339 /* 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. 1344 * 1345 * TODO: Ideally set_capacity() and get_capacity() should be 1346 * converted to make use of bd_mutex and sequence counters. 1347 */ 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; 1353 } 1354 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)); 1360 } 1361 return disk; 1362 } 1363 EXPORT_SYMBOL(alloc_disk_node); 1364 1365 struct kobject *get_disk(struct gendisk *disk) 1366 { 1367 struct module *owner; 1368 struct kobject *kobj; 1369 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; 1379 } 1380 return kobj; 1381 1382 } 1383 1384 EXPORT_SYMBOL(get_disk); 1385 1386 void put_disk(struct gendisk *disk) 1387 { 1388 if (disk) 1389 kobject_put(&disk_to_dev(disk)->kobj); 1390 } 1391 1392 EXPORT_SYMBOL(put_disk); 1393 1394 static void set_disk_ro_uevent(struct gendisk *gd, int ro) 1395 { 1396 char event[] = "DISK_RO=1"; 1397 char *envp[] = { event, NULL }; 1398 1399 if (!ro) 1400 event[8] = '0'; 1401 kobject_uevent_env(&disk_to_dev(gd)->kobj, KOBJ_CHANGE, envp); 1402 } 1403 1404 void set_device_ro(struct block_device *bdev, int flag) 1405 { 1406 bdev->bd_part->policy = flag; 1407 } 1408 1409 EXPORT_SYMBOL(set_device_ro); 1410 1411 void set_disk_ro(struct gendisk *disk, int flag) 1412 { 1413 struct disk_part_iter piter; 1414 struct hd_struct *part; 1415 1416 if (disk->part0.policy != flag) { 1417 set_disk_ro_uevent(disk, flag); 1418 disk->part0.policy = flag; 1419 } 1420 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); 1425 } 1426 1427 EXPORT_SYMBOL(set_disk_ro); 1428 1429 int bdev_read_only(struct block_device *bdev) 1430 { 1431 if (!bdev) 1432 return 0; 1433 return bdev->bd_part->policy; 1434 } 1435 1436 EXPORT_SYMBOL(bdev_read_only); 1437 1438 int invalidate_partition(struct gendisk *disk, int partno) 1439 { 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); 1446 } 1447 return res; 1448 } 1449 1450 EXPORT_SYMBOL(invalidate_partition); 1451 1452 /* 1453 * Disk events - monitor disk events like media change and eject request. 1454 */ 1455 struct disk_events { 1456 struct list_head node; /* all disk_event's */ 1457 struct gendisk *disk; /* the associated disk */ 1458 spinlock_t lock; 1459 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 */ 1464 1465 long poll_msecs; /* interval, -1 for default */ 1466 struct delayed_work dwork; 1467 }; 1468 1469 static const char *disk_events_strs[] = { 1470 [ilog2(DISK_EVENT_MEDIA_CHANGE)] = "media_change", 1471 [ilog2(DISK_EVENT_EJECT_REQUEST)] = "eject_request", 1472 }; 1473 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", 1477 }; 1478 1479 /* list of all disk_events */ 1480 static DEFINE_MUTEX(disk_events_mutex); 1481 static LIST_HEAD(disk_events); 1482 1483 /* disable in-kernel polling by default */ 1484 static unsigned long disk_events_dfl_poll_msecs; 1485 1486 static unsigned long disk_events_poll_jiffies(struct gendisk *disk) 1487 { 1488 struct disk_events *ev = disk->ev; 1489 long intv_msecs = 0; 1490 1491 /* 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. 1495 */ 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; 1500 1501 return msecs_to_jiffies(intv_msecs); 1502 } 1503 1504 /** 1505 * disk_block_events - block and flush disk event checking 1506 * @disk: disk to block events for 1507 * 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. 1512 * 1513 * Note that this intentionally does not block event checking from 1514 * disk_clear_events(). 1515 * 1516 * CONTEXT: 1517 * Might sleep. 1518 */ 1519 void disk_block_events(struct gendisk *disk) 1520 { 1521 struct disk_events *ev = disk->ev; 1522 unsigned long flags; 1523 bool cancel; 1524 1525 if (!ev) 1526 return; 1527 1528 /* 1529 * Outer mutex ensures that the first blocker completes canceling 1530 * the event work before further blockers are allowed to finish. 1531 */ 1532 mutex_lock(&ev->block_mutex); 1533 1534 spin_lock_irqsave(&ev->lock, flags); 1535 cancel = !ev->block++; 1536 spin_unlock_irqrestore(&ev->lock, flags); 1537 1538 if (cancel) 1539 cancel_delayed_work_sync(&disk->ev->dwork); 1540 1541 mutex_unlock(&ev->block_mutex); 1542 } 1543 1544 static void __disk_unblock_events(struct gendisk *disk, bool check_now) 1545 { 1546 struct disk_events *ev = disk->ev; 1547 unsigned long intv; 1548 unsigned long flags; 1549 1550 spin_lock_irqsave(&ev->lock, flags); 1551 1552 if (WARN_ON_ONCE(ev->block <= 0)) 1553 goto out_unlock; 1554 1555 if (--ev->block) 1556 goto out_unlock; 1557 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); 1567 } 1568 1569 /** 1570 * disk_unblock_events - unblock disk event checking 1571 * @disk: disk to unblock events for 1572 * 1573 * Undo disk_block_events(). When the block count reaches zero, it 1574 * starts events polling if configured. 1575 * 1576 * CONTEXT: 1577 * Don't care. Safe to call from irq context. 1578 */ 1579 void disk_unblock_events(struct gendisk *disk) 1580 { 1581 if (disk->ev) 1582 __disk_unblock_events(disk, false); 1583 } 1584 1585 /** 1586 * disk_flush_events - schedule immediate event checking and flushing 1587 * @disk: disk to check and flush events for 1588 * @mask: events to flush 1589 * 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. 1593 * 1594 * CONTEXT: 1595 * If @mask is non-zero must be called with bdev->bd_mutex held. 1596 */ 1597 void disk_flush_events(struct gendisk *disk, unsigned int mask) 1598 { 1599 struct disk_events *ev = disk->ev; 1600 1601 if (!ev) 1602 return; 1603 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); 1610 } 1611 1612 /** 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 1616 * 1617 * Disk events are synchronously checked and pending events in @mask 1618 * are cleared and returned. This ignores the block count. 1619 * 1620 * CONTEXT: 1621 * Might sleep. 1622 */ 1623 unsigned int disk_clear_events(struct gendisk *disk, unsigned int mask) 1624 { 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; 1629 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; 1636 } 1637 1638 disk_block_events(disk); 1639 1640 /* 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). 1644 */ 1645 spin_lock_irq(&ev->lock); 1646 clearing |= ev->clearing; 1647 ev->clearing = 0; 1648 spin_unlock_irq(&ev->lock); 1649 1650 disk_check_events(ev, &clearing); 1651 /* 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. 1654 */ 1655 __disk_unblock_events(disk, ev->clearing ? true : false); 1656 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); 1663 1664 return pending; 1665 } 1666 1667 /* 1668 * Separate this part out so that a different pointer for clearing_ptr can be 1669 * passed in for disk_clear_events. 1670 */ 1671 static void disk_events_workfn(struct work_struct *work) 1672 { 1673 struct delayed_work *dwork = to_delayed_work(work); 1674 struct disk_events *ev = container_of(dwork, struct disk_events, dwork); 1675 1676 disk_check_events(ev, &ev->clearing); 1677 } 1678 1679 static void disk_check_events(struct disk_events *ev, 1680 unsigned int *clearing_ptr) 1681 { 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; 1688 1689 /* check events */ 1690 events = disk->fops->check_events(disk, clearing); 1691 1692 /* accumulate pending events and schedule next poll if necessary */ 1693 spin_lock_irq(&ev->lock); 1694 1695 events &= ~ev->pending; 1696 ev->pending |= events; 1697 *clearing_ptr &= ~clearing; 1698 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); 1703 1704 spin_unlock_irq(&ev->lock); 1705 1706 /* 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. 1710 */ 1711 for (i = 0; i < ARRAY_SIZE(disk_uevents); i++) 1712 if (events & disk->events & (1 << i)) 1713 envp[nr_events++] = disk_uevents[i]; 1714 1715 if (nr_events) 1716 kobject_uevent_env(&disk_to_dev(disk)->kobj, KOBJ_CHANGE, envp); 1717 } 1718 1719 /* 1720 * A disk events enabled device has the following sysfs nodes under 1721 * its /sys/block/X/ directory. 1722 * 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 1726 */ 1727 static ssize_t __disk_events_show(unsigned int events, char *buf) 1728 { 1729 const char *delim = ""; 1730 ssize_t pos = 0; 1731 int i; 1732 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 = " "; 1738 } 1739 if (pos) 1740 pos += sprintf(buf + pos, "\n"); 1741 return pos; 1742 } 1743 1744 static ssize_t disk_events_show(struct device *dev, 1745 struct device_attribute *attr, char *buf) 1746 { 1747 struct gendisk *disk = dev_to_disk(dev); 1748 1749 return __disk_events_show(disk->events, buf); 1750 } 1751 1752 static ssize_t disk_events_async_show(struct device *dev, 1753 struct device_attribute *attr, char *buf) 1754 { 1755 struct gendisk *disk = dev_to_disk(dev); 1756 1757 return __disk_events_show(disk->async_events, buf); 1758 } 1759 1760 static ssize_t disk_events_poll_msecs_show(struct device *dev, 1761 struct device_attribute *attr, 1762 char *buf) 1763 { 1764 struct gendisk *disk = dev_to_disk(dev); 1765 1766 return sprintf(buf, "%ld\n", disk->ev->poll_msecs); 1767 } 1768 1769 static ssize_t disk_events_poll_msecs_store(struct device *dev, 1770 struct device_attribute *attr, 1771 const char *buf, size_t count) 1772 { 1773 struct gendisk *disk = dev_to_disk(dev); 1774 long intv; 1775 1776 if (!count || !sscanf(buf, "%ld", &intv)) 1777 return -EINVAL; 1778 1779 if (intv < 0 && intv != -1) 1780 return -EINVAL; 1781 1782 disk_block_events(disk); 1783 disk->ev->poll_msecs = intv; 1784 __disk_unblock_events(disk, true); 1785 1786 return count; 1787 } 1788 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); 1794 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, 1800 }; 1801 1802 /* 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. 1807 */ 1808 static int disk_events_set_dfl_poll_msecs(const char *val, 1809 const struct kernel_param *kp) 1810 { 1811 struct disk_events *ev; 1812 int ret; 1813 1814 ret = param_set_ulong(val, kp); 1815 if (ret < 0) 1816 return ret; 1817 1818 mutex_lock(&disk_events_mutex); 1819 1820 list_for_each_entry(ev, &disk_events, node) 1821 disk_flush_events(ev->disk, 0); 1822 1823 mutex_unlock(&disk_events_mutex); 1824 1825 return 0; 1826 } 1827 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, 1831 }; 1832 1833 #undef MODULE_PARAM_PREFIX 1834 #define MODULE_PARAM_PREFIX "block." 1835 1836 module_param_cb(events_dfl_poll_msecs, &disk_events_dfl_poll_msecs_param_ops, 1837 &disk_events_dfl_poll_msecs, 0644); 1838 1839 /* 1840 * disk_{alloc|add|del|release}_events - initialize and destroy disk_events. 1841 */ 1842 static void disk_alloc_events(struct gendisk *disk) 1843 { 1844 struct disk_events *ev; 1845 1846 if (!disk->fops->check_events) 1847 return; 1848 1849 ev = kzalloc(sizeof(*ev), GFP_KERNEL); 1850 if (!ev) { 1851 pr_warn("%s: failed to initialize events\n", disk->disk_name); 1852 return; 1853 } 1854 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); 1862 1863 disk->ev = ev; 1864 } 1865 1866 static void disk_add_events(struct gendisk *disk) 1867 { 1868 if (!disk->ev) 1869 return; 1870 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); 1875 1876 mutex_lock(&disk_events_mutex); 1877 list_add_tail(&disk->ev->node, &disk_events); 1878 mutex_unlock(&disk_events_mutex); 1879 1880 /* 1881 * Block count is initialized to 1 and the following initial 1882 * unblock kicks it into action. 1883 */ 1884 __disk_unblock_events(disk, true); 1885 } 1886 1887 static void disk_del_events(struct gendisk *disk) 1888 { 1889 if (!disk->ev) 1890 return; 1891 1892 disk_block_events(disk); 1893 1894 mutex_lock(&disk_events_mutex); 1895 list_del_init(&disk->ev->node); 1896 mutex_unlock(&disk_events_mutex); 1897 1898 sysfs_remove_files(&disk_to_dev(disk)->kobj, disk_events_attrs); 1899 } 1900 1901 static void disk_release_events(struct gendisk *disk) 1902 { 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); 1906 } 1907