1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * gendisk handling 4 * 5 * Portions Copyright (C) 2020 Christoph Hellwig 6 */ 7 8 #include <linux/module.h> 9 #include <linux/ctype.h> 10 #include <linux/fs.h> 11 #include <linux/kdev_t.h> 12 #include <linux/kernel.h> 13 #include <linux/blkdev.h> 14 #include <linux/backing-dev.h> 15 #include <linux/init.h> 16 #include <linux/spinlock.h> 17 #include <linux/proc_fs.h> 18 #include <linux/seq_file.h> 19 #include <linux/slab.h> 20 #include <linux/kmod.h> 21 #include <linux/major.h> 22 #include <linux/mutex.h> 23 #include <linux/idr.h> 24 #include <linux/log2.h> 25 #include <linux/pm_runtime.h> 26 #include <linux/badblocks.h> 27 #include <linux/part_stat.h> 28 #include <linux/blktrace_api.h> 29 30 #include "blk-throttle.h" 31 #include "blk.h" 32 #include "blk-mq-sched.h" 33 #include "blk-rq-qos.h" 34 #include "blk-cgroup.h" 35 36 static struct kobject *block_depr; 37 38 /* 39 * Unique, monotonically increasing sequential number associated with block 40 * devices instances (i.e. incremented each time a device is attached). 41 * Associating uevents with block devices in userspace is difficult and racy: 42 * the uevent netlink socket is lossy, and on slow and overloaded systems has 43 * a very high latency. 44 * Block devices do not have exclusive owners in userspace, any process can set 45 * one up (e.g. loop devices). Moreover, device names can be reused (e.g. loop0 46 * can be reused again and again). 47 * A userspace process setting up a block device and watching for its events 48 * cannot thus reliably tell whether an event relates to the device it just set 49 * up or another earlier instance with the same name. 50 * This sequential number allows userspace processes to solve this problem, and 51 * uniquely associate an uevent to the lifetime to a device. 52 */ 53 static atomic64_t diskseq; 54 55 /* for extended dynamic devt allocation, currently only one major is used */ 56 #define NR_EXT_DEVT (1 << MINORBITS) 57 static DEFINE_IDA(ext_devt_ida); 58 59 void set_capacity(struct gendisk *disk, sector_t sectors) 60 { 61 if (sectors > BLK_DEV_MAX_SECTORS) { 62 pr_warn_once("%s: truncate capacity from %lld to %lld\n", 63 disk->disk_name, sectors, 64 BLK_DEV_MAX_SECTORS); 65 sectors = BLK_DEV_MAX_SECTORS; 66 } 67 68 bdev_set_nr_sectors(disk->part0, sectors); 69 } 70 EXPORT_SYMBOL(set_capacity); 71 72 /* 73 * Set disk capacity and notify if the size is not currently zero and will not 74 * be set to zero. Returns true if a uevent was sent, otherwise false. 75 */ 76 bool set_capacity_and_notify(struct gendisk *disk, sector_t size) 77 { 78 sector_t capacity = get_capacity(disk); 79 char *envp[] = { "RESIZE=1", NULL }; 80 81 set_capacity(disk, size); 82 83 /* 84 * Only print a message and send a uevent if the gendisk is user visible 85 * and alive. This avoids spamming the log and udev when setting the 86 * initial capacity during probing. 87 */ 88 if (size == capacity || 89 !disk_live(disk) || 90 (disk->flags & GENHD_FL_HIDDEN)) 91 return false; 92 93 pr_info("%s: detected capacity change from %lld to %lld\n", 94 disk->disk_name, capacity, size); 95 96 /* 97 * Historically we did not send a uevent for changes to/from an empty 98 * device. 99 */ 100 if (!capacity || !size) 101 return false; 102 kobject_uevent_env(&disk_to_dev(disk)->kobj, KOBJ_CHANGE, envp); 103 return true; 104 } 105 EXPORT_SYMBOL_GPL(set_capacity_and_notify); 106 107 static void part_stat_read_all(struct block_device *part, 108 struct disk_stats *stat) 109 { 110 int cpu; 111 112 memset(stat, 0, sizeof(struct disk_stats)); 113 for_each_possible_cpu(cpu) { 114 struct disk_stats *ptr = per_cpu_ptr(part->bd_stats, cpu); 115 int group; 116 117 for (group = 0; group < NR_STAT_GROUPS; group++) { 118 stat->nsecs[group] += ptr->nsecs[group]; 119 stat->sectors[group] += ptr->sectors[group]; 120 stat->ios[group] += ptr->ios[group]; 121 stat->merges[group] += ptr->merges[group]; 122 } 123 124 stat->io_ticks += ptr->io_ticks; 125 } 126 } 127 128 unsigned int part_in_flight(struct block_device *part) 129 { 130 unsigned int inflight = 0; 131 int cpu; 132 133 for_each_possible_cpu(cpu) { 134 inflight += part_stat_local_read_cpu(part, in_flight[0], cpu) + 135 part_stat_local_read_cpu(part, in_flight[1], cpu); 136 } 137 if ((int)inflight < 0) 138 inflight = 0; 139 140 return inflight; 141 } 142 143 static void part_in_flight_rw(struct block_device *part, 144 unsigned int inflight[2]) 145 { 146 int cpu; 147 148 inflight[0] = 0; 149 inflight[1] = 0; 150 for_each_possible_cpu(cpu) { 151 inflight[0] += part_stat_local_read_cpu(part, in_flight[0], cpu); 152 inflight[1] += part_stat_local_read_cpu(part, in_flight[1], cpu); 153 } 154 if ((int)inflight[0] < 0) 155 inflight[0] = 0; 156 if ((int)inflight[1] < 0) 157 inflight[1] = 0; 158 } 159 160 /* 161 * Can be deleted altogether. Later. 162 * 163 */ 164 #define BLKDEV_MAJOR_HASH_SIZE 255 165 static struct blk_major_name { 166 struct blk_major_name *next; 167 int major; 168 char name[16]; 169 #ifdef CONFIG_BLOCK_LEGACY_AUTOLOAD 170 void (*probe)(dev_t devt); 171 #endif 172 } *major_names[BLKDEV_MAJOR_HASH_SIZE]; 173 static DEFINE_MUTEX(major_names_lock); 174 static DEFINE_SPINLOCK(major_names_spinlock); 175 176 /* index in the above - for now: assume no multimajor ranges */ 177 static inline int major_to_index(unsigned major) 178 { 179 return major % BLKDEV_MAJOR_HASH_SIZE; 180 } 181 182 #ifdef CONFIG_PROC_FS 183 void blkdev_show(struct seq_file *seqf, off_t offset) 184 { 185 struct blk_major_name *dp; 186 187 spin_lock(&major_names_spinlock); 188 for (dp = major_names[major_to_index(offset)]; dp; dp = dp->next) 189 if (dp->major == offset) 190 seq_printf(seqf, "%3d %s\n", dp->major, dp->name); 191 spin_unlock(&major_names_spinlock); 192 } 193 #endif /* CONFIG_PROC_FS */ 194 195 /** 196 * __register_blkdev - register a new block device 197 * 198 * @major: the requested major device number [1..BLKDEV_MAJOR_MAX-1]. If 199 * @major = 0, try to allocate any unused major number. 200 * @name: the name of the new block device as a zero terminated string 201 * @probe: pre-devtmpfs / pre-udev callback used to create disks when their 202 * pre-created device node is accessed. When a probe call uses 203 * add_disk() and it fails the driver must cleanup resources. This 204 * interface may soon be removed. 205 * 206 * The @name must be unique within the system. 207 * 208 * The return value depends on the @major input parameter: 209 * 210 * - if a major device number was requested in range [1..BLKDEV_MAJOR_MAX-1] 211 * then the function returns zero on success, or a negative error code 212 * - if any unused major number was requested with @major = 0 parameter 213 * then the return value is the allocated major number in range 214 * [1..BLKDEV_MAJOR_MAX-1] or a negative error code otherwise 215 * 216 * See Documentation/admin-guide/devices.txt for the list of allocated 217 * major numbers. 218 * 219 * Use register_blkdev instead for any new code. 220 */ 221 int __register_blkdev(unsigned int major, const char *name, 222 void (*probe)(dev_t devt)) 223 { 224 struct blk_major_name **n, *p; 225 int index, ret = 0; 226 227 mutex_lock(&major_names_lock); 228 229 /* temporary */ 230 if (major == 0) { 231 for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) { 232 if (major_names[index] == NULL) 233 break; 234 } 235 236 if (index == 0) { 237 printk("%s: failed to get major for %s\n", 238 __func__, name); 239 ret = -EBUSY; 240 goto out; 241 } 242 major = index; 243 ret = major; 244 } 245 246 if (major >= BLKDEV_MAJOR_MAX) { 247 pr_err("%s: major requested (%u) is greater than the maximum (%u) for %s\n", 248 __func__, major, BLKDEV_MAJOR_MAX-1, name); 249 250 ret = -EINVAL; 251 goto out; 252 } 253 254 p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL); 255 if (p == NULL) { 256 ret = -ENOMEM; 257 goto out; 258 } 259 260 p->major = major; 261 #ifdef CONFIG_BLOCK_LEGACY_AUTOLOAD 262 p->probe = probe; 263 #endif 264 strscpy(p->name, name, sizeof(p->name)); 265 p->next = NULL; 266 index = major_to_index(major); 267 268 spin_lock(&major_names_spinlock); 269 for (n = &major_names[index]; *n; n = &(*n)->next) { 270 if ((*n)->major == major) 271 break; 272 } 273 if (!*n) 274 *n = p; 275 else 276 ret = -EBUSY; 277 spin_unlock(&major_names_spinlock); 278 279 if (ret < 0) { 280 printk("register_blkdev: cannot get major %u for %s\n", 281 major, name); 282 kfree(p); 283 } 284 out: 285 mutex_unlock(&major_names_lock); 286 return ret; 287 } 288 EXPORT_SYMBOL(__register_blkdev); 289 290 void unregister_blkdev(unsigned int major, const char *name) 291 { 292 struct blk_major_name **n; 293 struct blk_major_name *p = NULL; 294 int index = major_to_index(major); 295 296 mutex_lock(&major_names_lock); 297 spin_lock(&major_names_spinlock); 298 for (n = &major_names[index]; *n; n = &(*n)->next) 299 if ((*n)->major == major) 300 break; 301 if (!*n || strcmp((*n)->name, name)) { 302 WARN_ON(1); 303 } else { 304 p = *n; 305 *n = p->next; 306 } 307 spin_unlock(&major_names_spinlock); 308 mutex_unlock(&major_names_lock); 309 kfree(p); 310 } 311 312 EXPORT_SYMBOL(unregister_blkdev); 313 314 int blk_alloc_ext_minor(void) 315 { 316 int idx; 317 318 idx = ida_alloc_range(&ext_devt_ida, 0, NR_EXT_DEVT - 1, GFP_KERNEL); 319 if (idx == -ENOSPC) 320 return -EBUSY; 321 return idx; 322 } 323 324 void blk_free_ext_minor(unsigned int minor) 325 { 326 ida_free(&ext_devt_ida, minor); 327 } 328 329 void disk_uevent(struct gendisk *disk, enum kobject_action action) 330 { 331 struct block_device *part; 332 unsigned long idx; 333 334 rcu_read_lock(); 335 xa_for_each(&disk->part_tbl, idx, part) { 336 if (bdev_is_partition(part) && !bdev_nr_sectors(part)) 337 continue; 338 if (!kobject_get_unless_zero(&part->bd_device.kobj)) 339 continue; 340 341 rcu_read_unlock(); 342 kobject_uevent(bdev_kobj(part), action); 343 put_device(&part->bd_device); 344 rcu_read_lock(); 345 } 346 rcu_read_unlock(); 347 } 348 EXPORT_SYMBOL_GPL(disk_uevent); 349 350 int disk_scan_partitions(struct gendisk *disk, blk_mode_t mode) 351 { 352 struct file *file; 353 int ret = 0; 354 355 if (!disk_has_partscan(disk)) 356 return -EINVAL; 357 if (disk->open_partitions) 358 return -EBUSY; 359 360 /* 361 * If the device is opened exclusively by current thread already, it's 362 * safe to scan partitons, otherwise, use bd_prepare_to_claim() to 363 * synchronize with other exclusive openers and other partition 364 * scanners. 365 */ 366 if (!(mode & BLK_OPEN_EXCL)) { 367 ret = bd_prepare_to_claim(disk->part0, disk_scan_partitions, 368 NULL); 369 if (ret) 370 return ret; 371 } 372 373 set_bit(GD_NEED_PART_SCAN, &disk->state); 374 file = bdev_file_open_by_dev(disk_devt(disk), mode & ~BLK_OPEN_EXCL, 375 NULL, NULL); 376 if (IS_ERR(file)) 377 ret = PTR_ERR(file); 378 else 379 fput(file); 380 381 /* 382 * If blkdev_get_by_dev() failed early, GD_NEED_PART_SCAN is still set, 383 * and this will cause that re-assemble partitioned raid device will 384 * creat partition for underlying disk. 385 */ 386 clear_bit(GD_NEED_PART_SCAN, &disk->state); 387 if (!(mode & BLK_OPEN_EXCL)) 388 bd_abort_claiming(disk->part0, disk_scan_partitions); 389 return ret; 390 } 391 392 /** 393 * add_disk_fwnode - add disk information to kernel list with fwnode 394 * @parent: parent device for the disk 395 * @disk: per-device partitioning information 396 * @groups: Additional per-device sysfs groups 397 * @fwnode: attached disk fwnode 398 * 399 * This function registers the partitioning information in @disk 400 * with the kernel. Also attach a fwnode to the disk device. 401 */ 402 int __must_check add_disk_fwnode(struct device *parent, struct gendisk *disk, 403 const struct attribute_group **groups, 404 struct fwnode_handle *fwnode) 405 406 { 407 struct device *ddev = disk_to_dev(disk); 408 int ret; 409 410 if (WARN_ON_ONCE(bdev_nr_sectors(disk->part0) > BLK_DEV_MAX_SECTORS)) 411 return -EINVAL; 412 413 if (queue_is_mq(disk->queue)) { 414 /* 415 * ->submit_bio and ->poll_bio are bypassed for blk-mq drivers. 416 */ 417 if (disk->fops->submit_bio || disk->fops->poll_bio) 418 return -EINVAL; 419 420 /* 421 * Initialize the I/O scheduler code and pick a default one if 422 * needed. 423 */ 424 elevator_init_mq(disk->queue); 425 } else { 426 if (!disk->fops->submit_bio) 427 return -EINVAL; 428 bdev_set_flag(disk->part0, BD_HAS_SUBMIT_BIO); 429 } 430 431 /* 432 * If the driver provides an explicit major number it also must provide 433 * the number of minors numbers supported, and those will be used to 434 * setup the gendisk. 435 * Otherwise just allocate the device numbers for both the whole device 436 * and all partitions from the extended dev_t space. 437 */ 438 ret = -EINVAL; 439 if (disk->major) { 440 if (WARN_ON(!disk->minors)) 441 goto out_exit_elevator; 442 443 if (disk->minors > DISK_MAX_PARTS) { 444 pr_err("block: can't allocate more than %d partitions\n", 445 DISK_MAX_PARTS); 446 disk->minors = DISK_MAX_PARTS; 447 } 448 if (disk->first_minor > MINORMASK || 449 disk->minors > MINORMASK + 1 || 450 disk->first_minor + disk->minors > MINORMASK + 1) 451 goto out_exit_elevator; 452 } else { 453 if (WARN_ON(disk->minors)) 454 goto out_exit_elevator; 455 456 ret = blk_alloc_ext_minor(); 457 if (ret < 0) 458 goto out_exit_elevator; 459 disk->major = BLOCK_EXT_MAJOR; 460 disk->first_minor = ret; 461 } 462 463 /* delay uevents, until we scanned partition table */ 464 dev_set_uevent_suppress(ddev, 1); 465 466 ddev->parent = parent; 467 ddev->groups = groups; 468 dev_set_name(ddev, "%s", disk->disk_name); 469 if (fwnode) 470 device_set_node(ddev, fwnode); 471 if (!(disk->flags & GENHD_FL_HIDDEN)) 472 ddev->devt = MKDEV(disk->major, disk->first_minor); 473 ret = device_add(ddev); 474 if (ret) 475 goto out_free_ext_minor; 476 477 ret = disk_alloc_events(disk); 478 if (ret) 479 goto out_device_del; 480 481 ret = sysfs_create_link(block_depr, &ddev->kobj, 482 kobject_name(&ddev->kobj)); 483 if (ret) 484 goto out_device_del; 485 486 /* 487 * avoid probable deadlock caused by allocating memory with 488 * GFP_KERNEL in runtime_resume callback of its all ancestor 489 * devices 490 */ 491 pm_runtime_set_memalloc_noio(ddev, true); 492 493 disk->part0->bd_holder_dir = 494 kobject_create_and_add("holders", &ddev->kobj); 495 if (!disk->part0->bd_holder_dir) { 496 ret = -ENOMEM; 497 goto out_del_block_link; 498 } 499 disk->slave_dir = kobject_create_and_add("slaves", &ddev->kobj); 500 if (!disk->slave_dir) { 501 ret = -ENOMEM; 502 goto out_put_holder_dir; 503 } 504 505 ret = blk_register_queue(disk); 506 if (ret) 507 goto out_put_slave_dir; 508 509 if (!(disk->flags & GENHD_FL_HIDDEN)) { 510 ret = bdi_register(disk->bdi, "%u:%u", 511 disk->major, disk->first_minor); 512 if (ret) 513 goto out_unregister_queue; 514 bdi_set_owner(disk->bdi, ddev); 515 ret = sysfs_create_link(&ddev->kobj, 516 &disk->bdi->dev->kobj, "bdi"); 517 if (ret) 518 goto out_unregister_bdi; 519 520 /* Make sure the first partition scan will be proceed */ 521 if (get_capacity(disk) && disk_has_partscan(disk)) 522 set_bit(GD_NEED_PART_SCAN, &disk->state); 523 524 bdev_add(disk->part0, ddev->devt); 525 if (get_capacity(disk)) 526 disk_scan_partitions(disk, BLK_OPEN_READ); 527 528 /* 529 * Announce the disk and partitions after all partitions are 530 * created. (for hidden disks uevents remain suppressed forever) 531 */ 532 dev_set_uevent_suppress(ddev, 0); 533 disk_uevent(disk, KOBJ_ADD); 534 } else { 535 /* 536 * Even if the block_device for a hidden gendisk is not 537 * registered, it needs to have a valid bd_dev so that the 538 * freeing of the dynamic major works. 539 */ 540 disk->part0->bd_dev = MKDEV(disk->major, disk->first_minor); 541 } 542 543 blk_apply_bdi_limits(disk->bdi, &disk->queue->limits); 544 disk_add_events(disk); 545 set_bit(GD_ADDED, &disk->state); 546 return 0; 547 548 out_unregister_bdi: 549 if (!(disk->flags & GENHD_FL_HIDDEN)) 550 bdi_unregister(disk->bdi); 551 out_unregister_queue: 552 blk_unregister_queue(disk); 553 rq_qos_exit(disk->queue); 554 out_put_slave_dir: 555 kobject_put(disk->slave_dir); 556 disk->slave_dir = NULL; 557 out_put_holder_dir: 558 kobject_put(disk->part0->bd_holder_dir); 559 out_del_block_link: 560 sysfs_remove_link(block_depr, dev_name(ddev)); 561 pm_runtime_set_memalloc_noio(ddev, false); 562 out_device_del: 563 device_del(ddev); 564 out_free_ext_minor: 565 if (disk->major == BLOCK_EXT_MAJOR) 566 blk_free_ext_minor(disk->first_minor); 567 out_exit_elevator: 568 if (disk->queue->elevator) { 569 mutex_lock(&disk->queue->elevator_lock); 570 elevator_exit(disk->queue); 571 mutex_unlock(&disk->queue->elevator_lock); 572 } 573 return ret; 574 } 575 EXPORT_SYMBOL_GPL(add_disk_fwnode); 576 577 /** 578 * device_add_disk - add disk information to kernel list 579 * @parent: parent device for the disk 580 * @disk: per-device partitioning information 581 * @groups: Additional per-device sysfs groups 582 * 583 * This function registers the partitioning information in @disk 584 * with the kernel. 585 */ 586 int __must_check device_add_disk(struct device *parent, struct gendisk *disk, 587 const struct attribute_group **groups) 588 { 589 return add_disk_fwnode(parent, disk, groups, NULL); 590 } 591 EXPORT_SYMBOL(device_add_disk); 592 593 static void blk_report_disk_dead(struct gendisk *disk, bool surprise) 594 { 595 struct block_device *bdev; 596 unsigned long idx; 597 598 /* 599 * On surprise disk removal, bdev_mark_dead() may call into file 600 * systems below. Make it clear that we're expecting to not hold 601 * disk->open_mutex. 602 */ 603 lockdep_assert_not_held(&disk->open_mutex); 604 605 rcu_read_lock(); 606 xa_for_each(&disk->part_tbl, idx, bdev) { 607 if (!kobject_get_unless_zero(&bdev->bd_device.kobj)) 608 continue; 609 rcu_read_unlock(); 610 611 bdev_mark_dead(bdev, surprise); 612 613 put_device(&bdev->bd_device); 614 rcu_read_lock(); 615 } 616 rcu_read_unlock(); 617 } 618 619 static bool __blk_mark_disk_dead(struct gendisk *disk) 620 { 621 /* 622 * Fail any new I/O. 623 */ 624 if (test_and_set_bit(GD_DEAD, &disk->state)) 625 return false; 626 627 if (test_bit(GD_OWNS_QUEUE, &disk->state)) 628 blk_queue_flag_set(QUEUE_FLAG_DYING, disk->queue); 629 630 /* 631 * Stop buffered writers from dirtying pages that can't be written out. 632 */ 633 set_capacity(disk, 0); 634 635 /* 636 * Prevent new I/O from crossing bio_queue_enter(). 637 */ 638 return blk_queue_start_drain(disk->queue); 639 } 640 641 /** 642 * blk_mark_disk_dead - mark a disk as dead 643 * @disk: disk to mark as dead 644 * 645 * Mark as disk as dead (e.g. surprise removed) and don't accept any new I/O 646 * to this disk. 647 */ 648 void blk_mark_disk_dead(struct gendisk *disk) 649 { 650 __blk_mark_disk_dead(disk); 651 blk_report_disk_dead(disk, true); 652 } 653 EXPORT_SYMBOL_GPL(blk_mark_disk_dead); 654 655 /** 656 * del_gendisk - remove the gendisk 657 * @disk: the struct gendisk to remove 658 * 659 * Removes the gendisk and all its associated resources. This deletes the 660 * partitions associated with the gendisk, and unregisters the associated 661 * request_queue. 662 * 663 * This is the counter to the respective __device_add_disk() call. 664 * 665 * The final removal of the struct gendisk happens when its refcount reaches 0 666 * with put_disk(), which should be called after del_gendisk(), if 667 * __device_add_disk() was used. 668 * 669 * Drivers exist which depend on the release of the gendisk to be synchronous, 670 * it should not be deferred. 671 * 672 * Context: can sleep 673 */ 674 void del_gendisk(struct gendisk *disk) 675 { 676 struct request_queue *q = disk->queue; 677 struct block_device *part; 678 unsigned long idx; 679 bool start_drain; 680 681 might_sleep(); 682 683 if (WARN_ON_ONCE(!disk_live(disk) && !(disk->flags & GENHD_FL_HIDDEN))) 684 return; 685 686 disk_del_events(disk); 687 688 /* 689 * Prevent new openers by unlinked the bdev inode. 690 */ 691 mutex_lock(&disk->open_mutex); 692 xa_for_each(&disk->part_tbl, idx, part) 693 bdev_unhash(part); 694 mutex_unlock(&disk->open_mutex); 695 696 /* 697 * Tell the file system to write back all dirty data and shut down if 698 * it hasn't been notified earlier. 699 */ 700 if (!test_bit(GD_DEAD, &disk->state)) 701 blk_report_disk_dead(disk, false); 702 703 /* 704 * Drop all partitions now that the disk is marked dead. 705 */ 706 mutex_lock(&disk->open_mutex); 707 start_drain = __blk_mark_disk_dead(disk); 708 if (start_drain) 709 blk_freeze_acquire_lock(q); 710 xa_for_each_start(&disk->part_tbl, idx, part, 1) 711 drop_partition(part); 712 mutex_unlock(&disk->open_mutex); 713 714 if (!(disk->flags & GENHD_FL_HIDDEN)) { 715 sysfs_remove_link(&disk_to_dev(disk)->kobj, "bdi"); 716 717 /* 718 * Unregister bdi before releasing device numbers (as they can 719 * get reused and we'd get clashes in sysfs). 720 */ 721 bdi_unregister(disk->bdi); 722 } 723 724 blk_unregister_queue(disk); 725 726 kobject_put(disk->part0->bd_holder_dir); 727 kobject_put(disk->slave_dir); 728 disk->slave_dir = NULL; 729 730 part_stat_set_all(disk->part0, 0); 731 disk->part0->bd_stamp = 0; 732 sysfs_remove_link(block_depr, dev_name(disk_to_dev(disk))); 733 pm_runtime_set_memalloc_noio(disk_to_dev(disk), false); 734 device_del(disk_to_dev(disk)); 735 736 blk_mq_freeze_queue_wait(q); 737 738 blk_throtl_cancel_bios(disk); 739 740 blk_sync_queue(q); 741 blk_flush_integrity(); 742 743 if (queue_is_mq(q)) 744 blk_mq_cancel_work_sync(q); 745 746 blk_mq_quiesce_queue(q); 747 if (q->elevator) { 748 mutex_lock(&q->elevator_lock); 749 elevator_exit(q); 750 mutex_unlock(&q->elevator_lock); 751 } 752 rq_qos_exit(q); 753 blk_mq_unquiesce_queue(q); 754 755 /* 756 * If the disk does not own the queue, allow using passthrough requests 757 * again. Else leave the queue frozen to fail all I/O. 758 */ 759 if (!test_bit(GD_OWNS_QUEUE, &disk->state)) 760 __blk_mq_unfreeze_queue(q, true); 761 else if (queue_is_mq(q)) 762 blk_mq_exit_queue(q); 763 764 if (start_drain) 765 blk_unfreeze_release_lock(q); 766 } 767 EXPORT_SYMBOL(del_gendisk); 768 769 /** 770 * invalidate_disk - invalidate the disk 771 * @disk: the struct gendisk to invalidate 772 * 773 * A helper to invalidates the disk. It will clean the disk's associated 774 * buffer/page caches and reset its internal states so that the disk 775 * can be reused by the drivers. 776 * 777 * Context: can sleep 778 */ 779 void invalidate_disk(struct gendisk *disk) 780 { 781 struct block_device *bdev = disk->part0; 782 783 invalidate_bdev(bdev); 784 bdev->bd_mapping->wb_err = 0; 785 set_capacity(disk, 0); 786 } 787 EXPORT_SYMBOL(invalidate_disk); 788 789 /* sysfs access to bad-blocks list. */ 790 static ssize_t disk_badblocks_show(struct device *dev, 791 struct device_attribute *attr, 792 char *page) 793 { 794 struct gendisk *disk = dev_to_disk(dev); 795 796 if (!disk->bb) 797 return sysfs_emit(page, "\n"); 798 799 return badblocks_show(disk->bb, page, 0); 800 } 801 802 static ssize_t disk_badblocks_store(struct device *dev, 803 struct device_attribute *attr, 804 const char *page, size_t len) 805 { 806 struct gendisk *disk = dev_to_disk(dev); 807 808 if (!disk->bb) 809 return -ENXIO; 810 811 return badblocks_store(disk->bb, page, len, 0); 812 } 813 814 #ifdef CONFIG_BLOCK_LEGACY_AUTOLOAD 815 static bool blk_probe_dev(dev_t devt) 816 { 817 unsigned int major = MAJOR(devt); 818 struct blk_major_name **n; 819 820 mutex_lock(&major_names_lock); 821 for (n = &major_names[major_to_index(major)]; *n; n = &(*n)->next) { 822 if ((*n)->major == major && (*n)->probe) { 823 (*n)->probe(devt); 824 mutex_unlock(&major_names_lock); 825 return true; 826 } 827 } 828 mutex_unlock(&major_names_lock); 829 return false; 830 } 831 832 void blk_request_module(dev_t devt) 833 { 834 int error; 835 836 if (blk_probe_dev(devt)) 837 return; 838 839 error = request_module("block-major-%d-%d", MAJOR(devt), MINOR(devt)); 840 /* Make old-style 2.4 aliases work */ 841 if (error > 0) 842 error = request_module("block-major-%d", MAJOR(devt)); 843 if (!error) 844 blk_probe_dev(devt); 845 } 846 #endif /* CONFIG_BLOCK_LEGACY_AUTOLOAD */ 847 848 #ifdef CONFIG_PROC_FS 849 /* iterator */ 850 static void *disk_seqf_start(struct seq_file *seqf, loff_t *pos) 851 { 852 loff_t skip = *pos; 853 struct class_dev_iter *iter; 854 struct device *dev; 855 856 iter = kmalloc(sizeof(*iter), GFP_KERNEL); 857 if (!iter) 858 return ERR_PTR(-ENOMEM); 859 860 seqf->private = iter; 861 class_dev_iter_init(iter, &block_class, NULL, &disk_type); 862 do { 863 dev = class_dev_iter_next(iter); 864 if (!dev) 865 return NULL; 866 } while (skip--); 867 868 return dev_to_disk(dev); 869 } 870 871 static void *disk_seqf_next(struct seq_file *seqf, void *v, loff_t *pos) 872 { 873 struct device *dev; 874 875 (*pos)++; 876 dev = class_dev_iter_next(seqf->private); 877 if (dev) 878 return dev_to_disk(dev); 879 880 return NULL; 881 } 882 883 static void disk_seqf_stop(struct seq_file *seqf, void *v) 884 { 885 struct class_dev_iter *iter = seqf->private; 886 887 /* stop is called even after start failed :-( */ 888 if (iter) { 889 class_dev_iter_exit(iter); 890 kfree(iter); 891 seqf->private = NULL; 892 } 893 } 894 895 static void *show_partition_start(struct seq_file *seqf, loff_t *pos) 896 { 897 void *p; 898 899 p = disk_seqf_start(seqf, pos); 900 if (!IS_ERR_OR_NULL(p) && !*pos) 901 seq_puts(seqf, "major minor #blocks name\n\n"); 902 return p; 903 } 904 905 static int show_partition(struct seq_file *seqf, void *v) 906 { 907 struct gendisk *sgp = v; 908 struct block_device *part; 909 unsigned long idx; 910 911 if (!get_capacity(sgp) || (sgp->flags & GENHD_FL_HIDDEN)) 912 return 0; 913 914 rcu_read_lock(); 915 xa_for_each(&sgp->part_tbl, idx, part) { 916 if (!bdev_nr_sectors(part)) 917 continue; 918 seq_printf(seqf, "%4d %7d %10llu %pg\n", 919 MAJOR(part->bd_dev), MINOR(part->bd_dev), 920 bdev_nr_sectors(part) >> 1, part); 921 } 922 rcu_read_unlock(); 923 return 0; 924 } 925 926 static const struct seq_operations partitions_op = { 927 .start = show_partition_start, 928 .next = disk_seqf_next, 929 .stop = disk_seqf_stop, 930 .show = show_partition 931 }; 932 #endif 933 934 static int __init genhd_device_init(void) 935 { 936 int error; 937 938 error = class_register(&block_class); 939 if (unlikely(error)) 940 return error; 941 blk_dev_init(); 942 943 register_blkdev(BLOCK_EXT_MAJOR, "blkext"); 944 945 /* create top-level block dir */ 946 block_depr = kobject_create_and_add("block", NULL); 947 return 0; 948 } 949 950 subsys_initcall(genhd_device_init); 951 952 static ssize_t disk_range_show(struct device *dev, 953 struct device_attribute *attr, char *buf) 954 { 955 struct gendisk *disk = dev_to_disk(dev); 956 957 return sysfs_emit(buf, "%d\n", disk->minors); 958 } 959 960 static ssize_t disk_ext_range_show(struct device *dev, 961 struct device_attribute *attr, char *buf) 962 { 963 struct gendisk *disk = dev_to_disk(dev); 964 965 return sysfs_emit(buf, "%d\n", 966 (disk->flags & GENHD_FL_NO_PART) ? 1 : DISK_MAX_PARTS); 967 } 968 969 static ssize_t disk_removable_show(struct device *dev, 970 struct device_attribute *attr, char *buf) 971 { 972 struct gendisk *disk = dev_to_disk(dev); 973 974 return sysfs_emit(buf, "%d\n", 975 (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0)); 976 } 977 978 static ssize_t disk_hidden_show(struct device *dev, 979 struct device_attribute *attr, char *buf) 980 { 981 struct gendisk *disk = dev_to_disk(dev); 982 983 return sysfs_emit(buf, "%d\n", 984 (disk->flags & GENHD_FL_HIDDEN ? 1 : 0)); 985 } 986 987 static ssize_t disk_ro_show(struct device *dev, 988 struct device_attribute *attr, char *buf) 989 { 990 struct gendisk *disk = dev_to_disk(dev); 991 992 return sysfs_emit(buf, "%d\n", get_disk_ro(disk) ? 1 : 0); 993 } 994 995 ssize_t part_size_show(struct device *dev, 996 struct device_attribute *attr, char *buf) 997 { 998 return sysfs_emit(buf, "%llu\n", bdev_nr_sectors(dev_to_bdev(dev))); 999 } 1000 1001 ssize_t part_stat_show(struct device *dev, 1002 struct device_attribute *attr, char *buf) 1003 { 1004 struct block_device *bdev = dev_to_bdev(dev); 1005 struct disk_stats stat; 1006 unsigned int inflight; 1007 1008 inflight = part_in_flight(bdev); 1009 if (inflight) { 1010 part_stat_lock(); 1011 update_io_ticks(bdev, jiffies, true); 1012 part_stat_unlock(); 1013 } 1014 part_stat_read_all(bdev, &stat); 1015 return sysfs_emit(buf, 1016 "%8lu %8lu %8llu %8u " 1017 "%8lu %8lu %8llu %8u " 1018 "%8u %8u %8u " 1019 "%8lu %8lu %8llu %8u " 1020 "%8lu %8u" 1021 "\n", 1022 stat.ios[STAT_READ], 1023 stat.merges[STAT_READ], 1024 (unsigned long long)stat.sectors[STAT_READ], 1025 (unsigned int)div_u64(stat.nsecs[STAT_READ], NSEC_PER_MSEC), 1026 stat.ios[STAT_WRITE], 1027 stat.merges[STAT_WRITE], 1028 (unsigned long long)stat.sectors[STAT_WRITE], 1029 (unsigned int)div_u64(stat.nsecs[STAT_WRITE], NSEC_PER_MSEC), 1030 inflight, 1031 jiffies_to_msecs(stat.io_ticks), 1032 (unsigned int)div_u64(stat.nsecs[STAT_READ] + 1033 stat.nsecs[STAT_WRITE] + 1034 stat.nsecs[STAT_DISCARD] + 1035 stat.nsecs[STAT_FLUSH], 1036 NSEC_PER_MSEC), 1037 stat.ios[STAT_DISCARD], 1038 stat.merges[STAT_DISCARD], 1039 (unsigned long long)stat.sectors[STAT_DISCARD], 1040 (unsigned int)div_u64(stat.nsecs[STAT_DISCARD], NSEC_PER_MSEC), 1041 stat.ios[STAT_FLUSH], 1042 (unsigned int)div_u64(stat.nsecs[STAT_FLUSH], NSEC_PER_MSEC)); 1043 } 1044 1045 ssize_t part_inflight_show(struct device *dev, struct device_attribute *attr, 1046 char *buf) 1047 { 1048 struct block_device *bdev = dev_to_bdev(dev); 1049 struct request_queue *q = bdev_get_queue(bdev); 1050 unsigned int inflight[2]; 1051 1052 if (queue_is_mq(q)) 1053 blk_mq_in_flight_rw(q, bdev, inflight); 1054 else 1055 part_in_flight_rw(bdev, inflight); 1056 1057 return sysfs_emit(buf, "%8u %8u\n", inflight[0], inflight[1]); 1058 } 1059 1060 static ssize_t disk_capability_show(struct device *dev, 1061 struct device_attribute *attr, char *buf) 1062 { 1063 dev_warn_once(dev, "the capability attribute has been deprecated.\n"); 1064 return sysfs_emit(buf, "0\n"); 1065 } 1066 1067 static ssize_t disk_alignment_offset_show(struct device *dev, 1068 struct device_attribute *attr, 1069 char *buf) 1070 { 1071 struct gendisk *disk = dev_to_disk(dev); 1072 1073 return sysfs_emit(buf, "%d\n", bdev_alignment_offset(disk->part0)); 1074 } 1075 1076 static ssize_t disk_discard_alignment_show(struct device *dev, 1077 struct device_attribute *attr, 1078 char *buf) 1079 { 1080 struct gendisk *disk = dev_to_disk(dev); 1081 1082 return sysfs_emit(buf, "%d\n", bdev_alignment_offset(disk->part0)); 1083 } 1084 1085 static ssize_t diskseq_show(struct device *dev, 1086 struct device_attribute *attr, char *buf) 1087 { 1088 struct gendisk *disk = dev_to_disk(dev); 1089 1090 return sysfs_emit(buf, "%llu\n", disk->diskseq); 1091 } 1092 1093 static ssize_t partscan_show(struct device *dev, 1094 struct device_attribute *attr, char *buf) 1095 { 1096 return sysfs_emit(buf, "%u\n", disk_has_partscan(dev_to_disk(dev))); 1097 } 1098 1099 static DEVICE_ATTR(range, 0444, disk_range_show, NULL); 1100 static DEVICE_ATTR(ext_range, 0444, disk_ext_range_show, NULL); 1101 static DEVICE_ATTR(removable, 0444, disk_removable_show, NULL); 1102 static DEVICE_ATTR(hidden, 0444, disk_hidden_show, NULL); 1103 static DEVICE_ATTR(ro, 0444, disk_ro_show, NULL); 1104 static DEVICE_ATTR(size, 0444, part_size_show, NULL); 1105 static DEVICE_ATTR(alignment_offset, 0444, disk_alignment_offset_show, NULL); 1106 static DEVICE_ATTR(discard_alignment, 0444, disk_discard_alignment_show, NULL); 1107 static DEVICE_ATTR(capability, 0444, disk_capability_show, NULL); 1108 static DEVICE_ATTR(stat, 0444, part_stat_show, NULL); 1109 static DEVICE_ATTR(inflight, 0444, part_inflight_show, NULL); 1110 static DEVICE_ATTR(badblocks, 0644, disk_badblocks_show, disk_badblocks_store); 1111 static DEVICE_ATTR(diskseq, 0444, diskseq_show, NULL); 1112 static DEVICE_ATTR(partscan, 0444, partscan_show, NULL); 1113 1114 #ifdef CONFIG_FAIL_MAKE_REQUEST 1115 ssize_t part_fail_show(struct device *dev, 1116 struct device_attribute *attr, char *buf) 1117 { 1118 return sysfs_emit(buf, "%d\n", 1119 bdev_test_flag(dev_to_bdev(dev), BD_MAKE_IT_FAIL)); 1120 } 1121 1122 ssize_t part_fail_store(struct device *dev, 1123 struct device_attribute *attr, 1124 const char *buf, size_t count) 1125 { 1126 int i; 1127 1128 if (count > 0 && sscanf(buf, "%d", &i) > 0) { 1129 if (i) 1130 bdev_set_flag(dev_to_bdev(dev), BD_MAKE_IT_FAIL); 1131 else 1132 bdev_clear_flag(dev_to_bdev(dev), BD_MAKE_IT_FAIL); 1133 } 1134 return count; 1135 } 1136 1137 static struct device_attribute dev_attr_fail = 1138 __ATTR(make-it-fail, 0644, part_fail_show, part_fail_store); 1139 #endif /* CONFIG_FAIL_MAKE_REQUEST */ 1140 1141 #ifdef CONFIG_FAIL_IO_TIMEOUT 1142 static struct device_attribute dev_attr_fail_timeout = 1143 __ATTR(io-timeout-fail, 0644, part_timeout_show, part_timeout_store); 1144 #endif 1145 1146 static struct attribute *disk_attrs[] = { 1147 &dev_attr_range.attr, 1148 &dev_attr_ext_range.attr, 1149 &dev_attr_removable.attr, 1150 &dev_attr_hidden.attr, 1151 &dev_attr_ro.attr, 1152 &dev_attr_size.attr, 1153 &dev_attr_alignment_offset.attr, 1154 &dev_attr_discard_alignment.attr, 1155 &dev_attr_capability.attr, 1156 &dev_attr_stat.attr, 1157 &dev_attr_inflight.attr, 1158 &dev_attr_badblocks.attr, 1159 &dev_attr_events.attr, 1160 &dev_attr_events_async.attr, 1161 &dev_attr_events_poll_msecs.attr, 1162 &dev_attr_diskseq.attr, 1163 &dev_attr_partscan.attr, 1164 #ifdef CONFIG_FAIL_MAKE_REQUEST 1165 &dev_attr_fail.attr, 1166 #endif 1167 #ifdef CONFIG_FAIL_IO_TIMEOUT 1168 &dev_attr_fail_timeout.attr, 1169 #endif 1170 NULL 1171 }; 1172 1173 static umode_t disk_visible(struct kobject *kobj, struct attribute *a, int n) 1174 { 1175 struct device *dev = container_of(kobj, typeof(*dev), kobj); 1176 struct gendisk *disk = dev_to_disk(dev); 1177 1178 if (a == &dev_attr_badblocks.attr && !disk->bb) 1179 return 0; 1180 return a->mode; 1181 } 1182 1183 static struct attribute_group disk_attr_group = { 1184 .attrs = disk_attrs, 1185 .is_visible = disk_visible, 1186 }; 1187 1188 static const struct attribute_group *disk_attr_groups[] = { 1189 &disk_attr_group, 1190 #ifdef CONFIG_BLK_DEV_IO_TRACE 1191 &blk_trace_attr_group, 1192 #endif 1193 #ifdef CONFIG_BLK_DEV_INTEGRITY 1194 &blk_integrity_attr_group, 1195 #endif 1196 NULL 1197 }; 1198 1199 /** 1200 * disk_release - releases all allocated resources of the gendisk 1201 * @dev: the device representing this disk 1202 * 1203 * This function releases all allocated resources of the gendisk. 1204 * 1205 * Drivers which used __device_add_disk() have a gendisk with a request_queue 1206 * assigned. Since the request_queue sits on top of the gendisk for these 1207 * drivers we also call blk_put_queue() for them, and we expect the 1208 * request_queue refcount to reach 0 at this point, and so the request_queue 1209 * will also be freed prior to the disk. 1210 * 1211 * Context: can sleep 1212 */ 1213 static void disk_release(struct device *dev) 1214 { 1215 struct gendisk *disk = dev_to_disk(dev); 1216 1217 might_sleep(); 1218 WARN_ON_ONCE(disk_live(disk)); 1219 1220 blk_trace_remove(disk->queue); 1221 1222 /* 1223 * To undo the all initialization from blk_mq_init_allocated_queue in 1224 * case of a probe failure where add_disk is never called we have to 1225 * call blk_mq_exit_queue here. We can't do this for the more common 1226 * teardown case (yet) as the tagset can be gone by the time the disk 1227 * is released once it was added. 1228 */ 1229 if (queue_is_mq(disk->queue) && 1230 test_bit(GD_OWNS_QUEUE, &disk->state) && 1231 !test_bit(GD_ADDED, &disk->state)) 1232 blk_mq_exit_queue(disk->queue); 1233 1234 blkcg_exit_disk(disk); 1235 1236 bioset_exit(&disk->bio_split); 1237 1238 disk_release_events(disk); 1239 kfree(disk->random); 1240 disk_free_zone_resources(disk); 1241 xa_destroy(&disk->part_tbl); 1242 1243 disk->queue->disk = NULL; 1244 blk_put_queue(disk->queue); 1245 1246 if (test_bit(GD_ADDED, &disk->state) && disk->fops->free_disk) 1247 disk->fops->free_disk(disk); 1248 1249 bdev_drop(disk->part0); /* frees the disk */ 1250 } 1251 1252 static int block_uevent(const struct device *dev, struct kobj_uevent_env *env) 1253 { 1254 const struct gendisk *disk = dev_to_disk(dev); 1255 1256 return add_uevent_var(env, "DISKSEQ=%llu", disk->diskseq); 1257 } 1258 1259 const struct class block_class = { 1260 .name = "block", 1261 .dev_uevent = block_uevent, 1262 }; 1263 1264 static char *block_devnode(const struct device *dev, umode_t *mode, 1265 kuid_t *uid, kgid_t *gid) 1266 { 1267 struct gendisk *disk = dev_to_disk(dev); 1268 1269 if (disk->fops->devnode) 1270 return disk->fops->devnode(disk, mode); 1271 return NULL; 1272 } 1273 1274 const struct device_type disk_type = { 1275 .name = "disk", 1276 .groups = disk_attr_groups, 1277 .release = disk_release, 1278 .devnode = block_devnode, 1279 }; 1280 1281 #ifdef CONFIG_PROC_FS 1282 /* 1283 * aggregate disk stat collector. Uses the same stats that the sysfs 1284 * entries do, above, but makes them available through one seq_file. 1285 * 1286 * The output looks suspiciously like /proc/partitions with a bunch of 1287 * extra fields. 1288 */ 1289 static int diskstats_show(struct seq_file *seqf, void *v) 1290 { 1291 struct gendisk *gp = v; 1292 struct block_device *hd; 1293 unsigned int inflight; 1294 struct disk_stats stat; 1295 unsigned long idx; 1296 1297 /* 1298 if (&disk_to_dev(gp)->kobj.entry == block_class.devices.next) 1299 seq_puts(seqf, "major minor name" 1300 " rio rmerge rsect ruse wio wmerge " 1301 "wsect wuse running use aveq" 1302 "\n\n"); 1303 */ 1304 1305 rcu_read_lock(); 1306 xa_for_each(&gp->part_tbl, idx, hd) { 1307 if (bdev_is_partition(hd) && !bdev_nr_sectors(hd)) 1308 continue; 1309 1310 inflight = part_in_flight(hd); 1311 if (inflight) { 1312 part_stat_lock(); 1313 update_io_ticks(hd, jiffies, true); 1314 part_stat_unlock(); 1315 } 1316 part_stat_read_all(hd, &stat); 1317 seq_put_decimal_ull_width(seqf, "", MAJOR(hd->bd_dev), 4); 1318 seq_put_decimal_ull_width(seqf, " ", MINOR(hd->bd_dev), 7); 1319 seq_printf(seqf, " %pg", hd); 1320 seq_put_decimal_ull(seqf, " ", stat.ios[STAT_READ]); 1321 seq_put_decimal_ull(seqf, " ", stat.merges[STAT_READ]); 1322 seq_put_decimal_ull(seqf, " ", stat.sectors[STAT_READ]); 1323 seq_put_decimal_ull(seqf, " ", (unsigned int)div_u64(stat.nsecs[STAT_READ], 1324 NSEC_PER_MSEC)); 1325 seq_put_decimal_ull(seqf, " ", stat.ios[STAT_WRITE]); 1326 seq_put_decimal_ull(seqf, " ", stat.merges[STAT_WRITE]); 1327 seq_put_decimal_ull(seqf, " ", stat.sectors[STAT_WRITE]); 1328 seq_put_decimal_ull(seqf, " ", (unsigned int)div_u64(stat.nsecs[STAT_WRITE], 1329 NSEC_PER_MSEC)); 1330 seq_put_decimal_ull(seqf, " ", inflight); 1331 seq_put_decimal_ull(seqf, " ", jiffies_to_msecs(stat.io_ticks)); 1332 seq_put_decimal_ull(seqf, " ", (unsigned int)div_u64(stat.nsecs[STAT_READ] + 1333 stat.nsecs[STAT_WRITE] + 1334 stat.nsecs[STAT_DISCARD] + 1335 stat.nsecs[STAT_FLUSH], 1336 NSEC_PER_MSEC)); 1337 seq_put_decimal_ull(seqf, " ", stat.ios[STAT_DISCARD]); 1338 seq_put_decimal_ull(seqf, " ", stat.merges[STAT_DISCARD]); 1339 seq_put_decimal_ull(seqf, " ", stat.sectors[STAT_DISCARD]); 1340 seq_put_decimal_ull(seqf, " ", (unsigned int)div_u64(stat.nsecs[STAT_DISCARD], 1341 NSEC_PER_MSEC)); 1342 seq_put_decimal_ull(seqf, " ", stat.ios[STAT_FLUSH]); 1343 seq_put_decimal_ull(seqf, " ", (unsigned int)div_u64(stat.nsecs[STAT_FLUSH], 1344 NSEC_PER_MSEC)); 1345 seq_putc(seqf, '\n'); 1346 } 1347 rcu_read_unlock(); 1348 1349 return 0; 1350 } 1351 1352 static const struct seq_operations diskstats_op = { 1353 .start = disk_seqf_start, 1354 .next = disk_seqf_next, 1355 .stop = disk_seqf_stop, 1356 .show = diskstats_show 1357 }; 1358 1359 static int __init proc_genhd_init(void) 1360 { 1361 proc_create_seq("diskstats", 0, NULL, &diskstats_op); 1362 proc_create_seq("partitions", 0, NULL, &partitions_op); 1363 return 0; 1364 } 1365 module_init(proc_genhd_init); 1366 #endif /* CONFIG_PROC_FS */ 1367 1368 dev_t part_devt(struct gendisk *disk, u8 partno) 1369 { 1370 struct block_device *part; 1371 dev_t devt = 0; 1372 1373 rcu_read_lock(); 1374 part = xa_load(&disk->part_tbl, partno); 1375 if (part) 1376 devt = part->bd_dev; 1377 rcu_read_unlock(); 1378 1379 return devt; 1380 } 1381 1382 struct gendisk *__alloc_disk_node(struct request_queue *q, int node_id, 1383 struct lock_class_key *lkclass) 1384 { 1385 struct gendisk *disk; 1386 1387 disk = kzalloc_node(sizeof(struct gendisk), GFP_KERNEL, node_id); 1388 if (!disk) 1389 return NULL; 1390 1391 if (bioset_init(&disk->bio_split, BIO_POOL_SIZE, 0, 0)) 1392 goto out_free_disk; 1393 1394 disk->bdi = bdi_alloc(node_id); 1395 if (!disk->bdi) 1396 goto out_free_bioset; 1397 1398 /* bdev_alloc() might need the queue, set before the first call */ 1399 disk->queue = q; 1400 1401 disk->part0 = bdev_alloc(disk, 0); 1402 if (!disk->part0) 1403 goto out_free_bdi; 1404 1405 disk->node_id = node_id; 1406 mutex_init(&disk->open_mutex); 1407 xa_init(&disk->part_tbl); 1408 if (xa_insert(&disk->part_tbl, 0, disk->part0, GFP_KERNEL)) 1409 goto out_destroy_part_tbl; 1410 1411 if (blkcg_init_disk(disk)) 1412 goto out_erase_part0; 1413 1414 disk_init_zone_resources(disk); 1415 rand_initialize_disk(disk); 1416 disk_to_dev(disk)->class = &block_class; 1417 disk_to_dev(disk)->type = &disk_type; 1418 device_initialize(disk_to_dev(disk)); 1419 inc_diskseq(disk); 1420 q->disk = disk; 1421 lockdep_init_map(&disk->lockdep_map, "(bio completion)", lkclass, 0); 1422 #ifdef CONFIG_BLOCK_HOLDER_DEPRECATED 1423 INIT_LIST_HEAD(&disk->slave_bdevs); 1424 #endif 1425 return disk; 1426 1427 out_erase_part0: 1428 xa_erase(&disk->part_tbl, 0); 1429 out_destroy_part_tbl: 1430 xa_destroy(&disk->part_tbl); 1431 disk->part0->bd_disk = NULL; 1432 bdev_drop(disk->part0); 1433 out_free_bdi: 1434 bdi_put(disk->bdi); 1435 out_free_bioset: 1436 bioset_exit(&disk->bio_split); 1437 out_free_disk: 1438 kfree(disk); 1439 return NULL; 1440 } 1441 1442 struct gendisk *__blk_alloc_disk(struct queue_limits *lim, int node, 1443 struct lock_class_key *lkclass) 1444 { 1445 struct queue_limits default_lim = { }; 1446 struct request_queue *q; 1447 struct gendisk *disk; 1448 1449 q = blk_alloc_queue(lim ? lim : &default_lim, node); 1450 if (IS_ERR(q)) 1451 return ERR_CAST(q); 1452 1453 disk = __alloc_disk_node(q, node, lkclass); 1454 if (!disk) { 1455 blk_put_queue(q); 1456 return ERR_PTR(-ENOMEM); 1457 } 1458 set_bit(GD_OWNS_QUEUE, &disk->state); 1459 return disk; 1460 } 1461 EXPORT_SYMBOL(__blk_alloc_disk); 1462 1463 /** 1464 * put_disk - decrements the gendisk refcount 1465 * @disk: the struct gendisk to decrement the refcount for 1466 * 1467 * This decrements the refcount for the struct gendisk. When this reaches 0 1468 * we'll have disk_release() called. 1469 * 1470 * Note: for blk-mq disk put_disk must be called before freeing the tag_set 1471 * when handling probe errors (that is before add_disk() is called). 1472 * 1473 * Context: Any context, but the last reference must not be dropped from 1474 * atomic context. 1475 */ 1476 void put_disk(struct gendisk *disk) 1477 { 1478 if (disk) 1479 put_device(disk_to_dev(disk)); 1480 } 1481 EXPORT_SYMBOL(put_disk); 1482 1483 static void set_disk_ro_uevent(struct gendisk *gd, int ro) 1484 { 1485 char event[] = "DISK_RO=1"; 1486 char *envp[] = { event, NULL }; 1487 1488 if (!ro) 1489 event[8] = '0'; 1490 kobject_uevent_env(&disk_to_dev(gd)->kobj, KOBJ_CHANGE, envp); 1491 } 1492 1493 /** 1494 * set_disk_ro - set a gendisk read-only 1495 * @disk: gendisk to operate on 1496 * @read_only: %true to set the disk read-only, %false set the disk read/write 1497 * 1498 * This function is used to indicate whether a given disk device should have its 1499 * read-only flag set. set_disk_ro() is typically used by device drivers to 1500 * indicate whether the underlying physical device is write-protected. 1501 */ 1502 void set_disk_ro(struct gendisk *disk, bool read_only) 1503 { 1504 if (read_only) { 1505 if (test_and_set_bit(GD_READ_ONLY, &disk->state)) 1506 return; 1507 } else { 1508 if (!test_and_clear_bit(GD_READ_ONLY, &disk->state)) 1509 return; 1510 } 1511 set_disk_ro_uevent(disk, read_only); 1512 } 1513 EXPORT_SYMBOL(set_disk_ro); 1514 1515 void inc_diskseq(struct gendisk *disk) 1516 { 1517 disk->diskseq = atomic64_inc_return(&diskseq); 1518 } 1519