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 elevator_exit(disk->queue); 570 return ret; 571 } 572 EXPORT_SYMBOL_GPL(add_disk_fwnode); 573 574 /** 575 * device_add_disk - add disk information to kernel list 576 * @parent: parent device for the disk 577 * @disk: per-device partitioning information 578 * @groups: Additional per-device sysfs groups 579 * 580 * This function registers the partitioning information in @disk 581 * with the kernel. 582 */ 583 int __must_check device_add_disk(struct device *parent, struct gendisk *disk, 584 const struct attribute_group **groups) 585 { 586 return add_disk_fwnode(parent, disk, groups, NULL); 587 } 588 EXPORT_SYMBOL(device_add_disk); 589 590 static void blk_report_disk_dead(struct gendisk *disk, bool surprise) 591 { 592 struct block_device *bdev; 593 unsigned long idx; 594 595 /* 596 * On surprise disk removal, bdev_mark_dead() may call into file 597 * systems below. Make it clear that we're expecting to not hold 598 * disk->open_mutex. 599 */ 600 lockdep_assert_not_held(&disk->open_mutex); 601 602 rcu_read_lock(); 603 xa_for_each(&disk->part_tbl, idx, bdev) { 604 if (!kobject_get_unless_zero(&bdev->bd_device.kobj)) 605 continue; 606 rcu_read_unlock(); 607 608 bdev_mark_dead(bdev, surprise); 609 610 put_device(&bdev->bd_device); 611 rcu_read_lock(); 612 } 613 rcu_read_unlock(); 614 } 615 616 static bool __blk_mark_disk_dead(struct gendisk *disk) 617 { 618 /* 619 * Fail any new I/O. 620 */ 621 if (test_and_set_bit(GD_DEAD, &disk->state)) 622 return false; 623 624 if (test_bit(GD_OWNS_QUEUE, &disk->state)) 625 blk_queue_flag_set(QUEUE_FLAG_DYING, disk->queue); 626 627 /* 628 * Stop buffered writers from dirtying pages that can't be written out. 629 */ 630 set_capacity(disk, 0); 631 632 /* 633 * Prevent new I/O from crossing bio_queue_enter(). 634 */ 635 return blk_queue_start_drain(disk->queue); 636 } 637 638 /** 639 * blk_mark_disk_dead - mark a disk as dead 640 * @disk: disk to mark as dead 641 * 642 * Mark as disk as dead (e.g. surprise removed) and don't accept any new I/O 643 * to this disk. 644 */ 645 void blk_mark_disk_dead(struct gendisk *disk) 646 { 647 __blk_mark_disk_dead(disk); 648 blk_report_disk_dead(disk, true); 649 } 650 EXPORT_SYMBOL_GPL(blk_mark_disk_dead); 651 652 /** 653 * del_gendisk - remove the gendisk 654 * @disk: the struct gendisk to remove 655 * 656 * Removes the gendisk and all its associated resources. This deletes the 657 * partitions associated with the gendisk, and unregisters the associated 658 * request_queue. 659 * 660 * This is the counter to the respective __device_add_disk() call. 661 * 662 * The final removal of the struct gendisk happens when its refcount reaches 0 663 * with put_disk(), which should be called after del_gendisk(), if 664 * __device_add_disk() was used. 665 * 666 * Drivers exist which depend on the release of the gendisk to be synchronous, 667 * it should not be deferred. 668 * 669 * Context: can sleep 670 */ 671 void del_gendisk(struct gendisk *disk) 672 { 673 struct request_queue *q = disk->queue; 674 struct block_device *part; 675 unsigned long idx; 676 bool start_drain; 677 678 might_sleep(); 679 680 if (WARN_ON_ONCE(!disk_live(disk) && !(disk->flags & GENHD_FL_HIDDEN))) 681 return; 682 683 disk_del_events(disk); 684 685 /* 686 * Prevent new openers by unlinked the bdev inode. 687 */ 688 mutex_lock(&disk->open_mutex); 689 xa_for_each(&disk->part_tbl, idx, part) 690 bdev_unhash(part); 691 mutex_unlock(&disk->open_mutex); 692 693 /* 694 * Tell the file system to write back all dirty data and shut down if 695 * it hasn't been notified earlier. 696 */ 697 if (!test_bit(GD_DEAD, &disk->state)) 698 blk_report_disk_dead(disk, false); 699 700 /* 701 * Drop all partitions now that the disk is marked dead. 702 */ 703 mutex_lock(&disk->open_mutex); 704 start_drain = __blk_mark_disk_dead(disk); 705 if (start_drain) 706 blk_freeze_acquire_lock(q); 707 xa_for_each_start(&disk->part_tbl, idx, part, 1) 708 drop_partition(part); 709 mutex_unlock(&disk->open_mutex); 710 711 if (!(disk->flags & GENHD_FL_HIDDEN)) { 712 sysfs_remove_link(&disk_to_dev(disk)->kobj, "bdi"); 713 714 /* 715 * Unregister bdi before releasing device numbers (as they can 716 * get reused and we'd get clashes in sysfs). 717 */ 718 bdi_unregister(disk->bdi); 719 } 720 721 blk_unregister_queue(disk); 722 723 kobject_put(disk->part0->bd_holder_dir); 724 kobject_put(disk->slave_dir); 725 disk->slave_dir = NULL; 726 727 part_stat_set_all(disk->part0, 0); 728 disk->part0->bd_stamp = 0; 729 sysfs_remove_link(block_depr, dev_name(disk_to_dev(disk))); 730 pm_runtime_set_memalloc_noio(disk_to_dev(disk), false); 731 device_del(disk_to_dev(disk)); 732 733 blk_mq_freeze_queue_wait(q); 734 735 blk_throtl_cancel_bios(disk); 736 737 blk_sync_queue(q); 738 blk_flush_integrity(); 739 740 if (queue_is_mq(q)) 741 blk_mq_cancel_work_sync(q); 742 743 blk_mq_quiesce_queue(q); 744 if (q->elevator) { 745 mutex_lock(&q->sysfs_lock); 746 elevator_exit(q); 747 mutex_unlock(&q->sysfs_lock); 748 } 749 rq_qos_exit(q); 750 blk_mq_unquiesce_queue(q); 751 752 /* 753 * If the disk does not own the queue, allow using passthrough requests 754 * again. Else leave the queue frozen to fail all I/O. 755 */ 756 if (!test_bit(GD_OWNS_QUEUE, &disk->state)) 757 __blk_mq_unfreeze_queue(q, true); 758 else if (queue_is_mq(q)) 759 blk_mq_exit_queue(q); 760 761 if (start_drain) 762 blk_unfreeze_release_lock(q); 763 } 764 EXPORT_SYMBOL(del_gendisk); 765 766 /** 767 * invalidate_disk - invalidate the disk 768 * @disk: the struct gendisk to invalidate 769 * 770 * A helper to invalidates the disk. It will clean the disk's associated 771 * buffer/page caches and reset its internal states so that the disk 772 * can be reused by the drivers. 773 * 774 * Context: can sleep 775 */ 776 void invalidate_disk(struct gendisk *disk) 777 { 778 struct block_device *bdev = disk->part0; 779 780 invalidate_bdev(bdev); 781 bdev->bd_mapping->wb_err = 0; 782 set_capacity(disk, 0); 783 } 784 EXPORT_SYMBOL(invalidate_disk); 785 786 /* sysfs access to bad-blocks list. */ 787 static ssize_t disk_badblocks_show(struct device *dev, 788 struct device_attribute *attr, 789 char *page) 790 { 791 struct gendisk *disk = dev_to_disk(dev); 792 793 if (!disk->bb) 794 return sysfs_emit(page, "\n"); 795 796 return badblocks_show(disk->bb, page, 0); 797 } 798 799 static ssize_t disk_badblocks_store(struct device *dev, 800 struct device_attribute *attr, 801 const char *page, size_t len) 802 { 803 struct gendisk *disk = dev_to_disk(dev); 804 805 if (!disk->bb) 806 return -ENXIO; 807 808 return badblocks_store(disk->bb, page, len, 0); 809 } 810 811 #ifdef CONFIG_BLOCK_LEGACY_AUTOLOAD 812 static bool blk_probe_dev(dev_t devt) 813 { 814 unsigned int major = MAJOR(devt); 815 struct blk_major_name **n; 816 817 mutex_lock(&major_names_lock); 818 for (n = &major_names[major_to_index(major)]; *n; n = &(*n)->next) { 819 if ((*n)->major == major && (*n)->probe) { 820 (*n)->probe(devt); 821 mutex_unlock(&major_names_lock); 822 return true; 823 } 824 } 825 mutex_unlock(&major_names_lock); 826 return false; 827 } 828 829 void blk_request_module(dev_t devt) 830 { 831 int error; 832 833 if (blk_probe_dev(devt)) 834 return; 835 836 error = request_module("block-major-%d-%d", MAJOR(devt), MINOR(devt)); 837 /* Make old-style 2.4 aliases work */ 838 if (error > 0) 839 error = request_module("block-major-%d", MAJOR(devt)); 840 if (!error) 841 blk_probe_dev(devt); 842 } 843 #endif /* CONFIG_BLOCK_LEGACY_AUTOLOAD */ 844 845 #ifdef CONFIG_PROC_FS 846 /* iterator */ 847 static void *disk_seqf_start(struct seq_file *seqf, loff_t *pos) 848 { 849 loff_t skip = *pos; 850 struct class_dev_iter *iter; 851 struct device *dev; 852 853 iter = kmalloc(sizeof(*iter), GFP_KERNEL); 854 if (!iter) 855 return ERR_PTR(-ENOMEM); 856 857 seqf->private = iter; 858 class_dev_iter_init(iter, &block_class, NULL, &disk_type); 859 do { 860 dev = class_dev_iter_next(iter); 861 if (!dev) 862 return NULL; 863 } while (skip--); 864 865 return dev_to_disk(dev); 866 } 867 868 static void *disk_seqf_next(struct seq_file *seqf, void *v, loff_t *pos) 869 { 870 struct device *dev; 871 872 (*pos)++; 873 dev = class_dev_iter_next(seqf->private); 874 if (dev) 875 return dev_to_disk(dev); 876 877 return NULL; 878 } 879 880 static void disk_seqf_stop(struct seq_file *seqf, void *v) 881 { 882 struct class_dev_iter *iter = seqf->private; 883 884 /* stop is called even after start failed :-( */ 885 if (iter) { 886 class_dev_iter_exit(iter); 887 kfree(iter); 888 seqf->private = NULL; 889 } 890 } 891 892 static void *show_partition_start(struct seq_file *seqf, loff_t *pos) 893 { 894 void *p; 895 896 p = disk_seqf_start(seqf, pos); 897 if (!IS_ERR_OR_NULL(p) && !*pos) 898 seq_puts(seqf, "major minor #blocks name\n\n"); 899 return p; 900 } 901 902 static int show_partition(struct seq_file *seqf, void *v) 903 { 904 struct gendisk *sgp = v; 905 struct block_device *part; 906 unsigned long idx; 907 908 if (!get_capacity(sgp) || (sgp->flags & GENHD_FL_HIDDEN)) 909 return 0; 910 911 rcu_read_lock(); 912 xa_for_each(&sgp->part_tbl, idx, part) { 913 if (!bdev_nr_sectors(part)) 914 continue; 915 seq_printf(seqf, "%4d %7d %10llu %pg\n", 916 MAJOR(part->bd_dev), MINOR(part->bd_dev), 917 bdev_nr_sectors(part) >> 1, part); 918 } 919 rcu_read_unlock(); 920 return 0; 921 } 922 923 static const struct seq_operations partitions_op = { 924 .start = show_partition_start, 925 .next = disk_seqf_next, 926 .stop = disk_seqf_stop, 927 .show = show_partition 928 }; 929 #endif 930 931 static int __init genhd_device_init(void) 932 { 933 int error; 934 935 error = class_register(&block_class); 936 if (unlikely(error)) 937 return error; 938 blk_dev_init(); 939 940 register_blkdev(BLOCK_EXT_MAJOR, "blkext"); 941 942 /* create top-level block dir */ 943 block_depr = kobject_create_and_add("block", NULL); 944 return 0; 945 } 946 947 subsys_initcall(genhd_device_init); 948 949 static ssize_t disk_range_show(struct device *dev, 950 struct device_attribute *attr, char *buf) 951 { 952 struct gendisk *disk = dev_to_disk(dev); 953 954 return sysfs_emit(buf, "%d\n", disk->minors); 955 } 956 957 static ssize_t disk_ext_range_show(struct device *dev, 958 struct device_attribute *attr, char *buf) 959 { 960 struct gendisk *disk = dev_to_disk(dev); 961 962 return sysfs_emit(buf, "%d\n", 963 (disk->flags & GENHD_FL_NO_PART) ? 1 : DISK_MAX_PARTS); 964 } 965 966 static ssize_t disk_removable_show(struct device *dev, 967 struct device_attribute *attr, char *buf) 968 { 969 struct gendisk *disk = dev_to_disk(dev); 970 971 return sysfs_emit(buf, "%d\n", 972 (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0)); 973 } 974 975 static ssize_t disk_hidden_show(struct device *dev, 976 struct device_attribute *attr, char *buf) 977 { 978 struct gendisk *disk = dev_to_disk(dev); 979 980 return sysfs_emit(buf, "%d\n", 981 (disk->flags & GENHD_FL_HIDDEN ? 1 : 0)); 982 } 983 984 static ssize_t disk_ro_show(struct device *dev, 985 struct device_attribute *attr, char *buf) 986 { 987 struct gendisk *disk = dev_to_disk(dev); 988 989 return sysfs_emit(buf, "%d\n", get_disk_ro(disk) ? 1 : 0); 990 } 991 992 ssize_t part_size_show(struct device *dev, 993 struct device_attribute *attr, char *buf) 994 { 995 return sysfs_emit(buf, "%llu\n", bdev_nr_sectors(dev_to_bdev(dev))); 996 } 997 998 ssize_t part_stat_show(struct device *dev, 999 struct device_attribute *attr, char *buf) 1000 { 1001 struct block_device *bdev = dev_to_bdev(dev); 1002 struct disk_stats stat; 1003 unsigned int inflight; 1004 1005 inflight = part_in_flight(bdev); 1006 if (inflight) { 1007 part_stat_lock(); 1008 update_io_ticks(bdev, jiffies, true); 1009 part_stat_unlock(); 1010 } 1011 part_stat_read_all(bdev, &stat); 1012 return sysfs_emit(buf, 1013 "%8lu %8lu %8llu %8u " 1014 "%8lu %8lu %8llu %8u " 1015 "%8u %8u %8u " 1016 "%8lu %8lu %8llu %8u " 1017 "%8lu %8u" 1018 "\n", 1019 stat.ios[STAT_READ], 1020 stat.merges[STAT_READ], 1021 (unsigned long long)stat.sectors[STAT_READ], 1022 (unsigned int)div_u64(stat.nsecs[STAT_READ], NSEC_PER_MSEC), 1023 stat.ios[STAT_WRITE], 1024 stat.merges[STAT_WRITE], 1025 (unsigned long long)stat.sectors[STAT_WRITE], 1026 (unsigned int)div_u64(stat.nsecs[STAT_WRITE], NSEC_PER_MSEC), 1027 inflight, 1028 jiffies_to_msecs(stat.io_ticks), 1029 (unsigned int)div_u64(stat.nsecs[STAT_READ] + 1030 stat.nsecs[STAT_WRITE] + 1031 stat.nsecs[STAT_DISCARD] + 1032 stat.nsecs[STAT_FLUSH], 1033 NSEC_PER_MSEC), 1034 stat.ios[STAT_DISCARD], 1035 stat.merges[STAT_DISCARD], 1036 (unsigned long long)stat.sectors[STAT_DISCARD], 1037 (unsigned int)div_u64(stat.nsecs[STAT_DISCARD], NSEC_PER_MSEC), 1038 stat.ios[STAT_FLUSH], 1039 (unsigned int)div_u64(stat.nsecs[STAT_FLUSH], NSEC_PER_MSEC)); 1040 } 1041 1042 ssize_t part_inflight_show(struct device *dev, struct device_attribute *attr, 1043 char *buf) 1044 { 1045 struct block_device *bdev = dev_to_bdev(dev); 1046 struct request_queue *q = bdev_get_queue(bdev); 1047 unsigned int inflight[2]; 1048 1049 if (queue_is_mq(q)) 1050 blk_mq_in_flight_rw(q, bdev, inflight); 1051 else 1052 part_in_flight_rw(bdev, inflight); 1053 1054 return sysfs_emit(buf, "%8u %8u\n", inflight[0], inflight[1]); 1055 } 1056 1057 static ssize_t disk_capability_show(struct device *dev, 1058 struct device_attribute *attr, char *buf) 1059 { 1060 dev_warn_once(dev, "the capability attribute has been deprecated.\n"); 1061 return sysfs_emit(buf, "0\n"); 1062 } 1063 1064 static ssize_t disk_alignment_offset_show(struct device *dev, 1065 struct device_attribute *attr, 1066 char *buf) 1067 { 1068 struct gendisk *disk = dev_to_disk(dev); 1069 1070 return sysfs_emit(buf, "%d\n", bdev_alignment_offset(disk->part0)); 1071 } 1072 1073 static ssize_t disk_discard_alignment_show(struct device *dev, 1074 struct device_attribute *attr, 1075 char *buf) 1076 { 1077 struct gendisk *disk = dev_to_disk(dev); 1078 1079 return sysfs_emit(buf, "%d\n", bdev_alignment_offset(disk->part0)); 1080 } 1081 1082 static ssize_t diskseq_show(struct device *dev, 1083 struct device_attribute *attr, char *buf) 1084 { 1085 struct gendisk *disk = dev_to_disk(dev); 1086 1087 return sysfs_emit(buf, "%llu\n", disk->diskseq); 1088 } 1089 1090 static ssize_t partscan_show(struct device *dev, 1091 struct device_attribute *attr, char *buf) 1092 { 1093 return sysfs_emit(buf, "%u\n", disk_has_partscan(dev_to_disk(dev))); 1094 } 1095 1096 static DEVICE_ATTR(range, 0444, disk_range_show, NULL); 1097 static DEVICE_ATTR(ext_range, 0444, disk_ext_range_show, NULL); 1098 static DEVICE_ATTR(removable, 0444, disk_removable_show, NULL); 1099 static DEVICE_ATTR(hidden, 0444, disk_hidden_show, NULL); 1100 static DEVICE_ATTR(ro, 0444, disk_ro_show, NULL); 1101 static DEVICE_ATTR(size, 0444, part_size_show, NULL); 1102 static DEVICE_ATTR(alignment_offset, 0444, disk_alignment_offset_show, NULL); 1103 static DEVICE_ATTR(discard_alignment, 0444, disk_discard_alignment_show, NULL); 1104 static DEVICE_ATTR(capability, 0444, disk_capability_show, NULL); 1105 static DEVICE_ATTR(stat, 0444, part_stat_show, NULL); 1106 static DEVICE_ATTR(inflight, 0444, part_inflight_show, NULL); 1107 static DEVICE_ATTR(badblocks, 0644, disk_badblocks_show, disk_badblocks_store); 1108 static DEVICE_ATTR(diskseq, 0444, diskseq_show, NULL); 1109 static DEVICE_ATTR(partscan, 0444, partscan_show, NULL); 1110 1111 #ifdef CONFIG_FAIL_MAKE_REQUEST 1112 ssize_t part_fail_show(struct device *dev, 1113 struct device_attribute *attr, char *buf) 1114 { 1115 return sysfs_emit(buf, "%d\n", 1116 bdev_test_flag(dev_to_bdev(dev), BD_MAKE_IT_FAIL)); 1117 } 1118 1119 ssize_t part_fail_store(struct device *dev, 1120 struct device_attribute *attr, 1121 const char *buf, size_t count) 1122 { 1123 int i; 1124 1125 if (count > 0 && sscanf(buf, "%d", &i) > 0) { 1126 if (i) 1127 bdev_set_flag(dev_to_bdev(dev), BD_MAKE_IT_FAIL); 1128 else 1129 bdev_clear_flag(dev_to_bdev(dev), BD_MAKE_IT_FAIL); 1130 } 1131 return count; 1132 } 1133 1134 static struct device_attribute dev_attr_fail = 1135 __ATTR(make-it-fail, 0644, part_fail_show, part_fail_store); 1136 #endif /* CONFIG_FAIL_MAKE_REQUEST */ 1137 1138 #ifdef CONFIG_FAIL_IO_TIMEOUT 1139 static struct device_attribute dev_attr_fail_timeout = 1140 __ATTR(io-timeout-fail, 0644, part_timeout_show, part_timeout_store); 1141 #endif 1142 1143 static struct attribute *disk_attrs[] = { 1144 &dev_attr_range.attr, 1145 &dev_attr_ext_range.attr, 1146 &dev_attr_removable.attr, 1147 &dev_attr_hidden.attr, 1148 &dev_attr_ro.attr, 1149 &dev_attr_size.attr, 1150 &dev_attr_alignment_offset.attr, 1151 &dev_attr_discard_alignment.attr, 1152 &dev_attr_capability.attr, 1153 &dev_attr_stat.attr, 1154 &dev_attr_inflight.attr, 1155 &dev_attr_badblocks.attr, 1156 &dev_attr_events.attr, 1157 &dev_attr_events_async.attr, 1158 &dev_attr_events_poll_msecs.attr, 1159 &dev_attr_diskseq.attr, 1160 &dev_attr_partscan.attr, 1161 #ifdef CONFIG_FAIL_MAKE_REQUEST 1162 &dev_attr_fail.attr, 1163 #endif 1164 #ifdef CONFIG_FAIL_IO_TIMEOUT 1165 &dev_attr_fail_timeout.attr, 1166 #endif 1167 NULL 1168 }; 1169 1170 static umode_t disk_visible(struct kobject *kobj, struct attribute *a, int n) 1171 { 1172 struct device *dev = container_of(kobj, typeof(*dev), kobj); 1173 struct gendisk *disk = dev_to_disk(dev); 1174 1175 if (a == &dev_attr_badblocks.attr && !disk->bb) 1176 return 0; 1177 return a->mode; 1178 } 1179 1180 static struct attribute_group disk_attr_group = { 1181 .attrs = disk_attrs, 1182 .is_visible = disk_visible, 1183 }; 1184 1185 static const struct attribute_group *disk_attr_groups[] = { 1186 &disk_attr_group, 1187 #ifdef CONFIG_BLK_DEV_IO_TRACE 1188 &blk_trace_attr_group, 1189 #endif 1190 #ifdef CONFIG_BLK_DEV_INTEGRITY 1191 &blk_integrity_attr_group, 1192 #endif 1193 NULL 1194 }; 1195 1196 /** 1197 * disk_release - releases all allocated resources of the gendisk 1198 * @dev: the device representing this disk 1199 * 1200 * This function releases all allocated resources of the gendisk. 1201 * 1202 * Drivers which used __device_add_disk() have a gendisk with a request_queue 1203 * assigned. Since the request_queue sits on top of the gendisk for these 1204 * drivers we also call blk_put_queue() for them, and we expect the 1205 * request_queue refcount to reach 0 at this point, and so the request_queue 1206 * will also be freed prior to the disk. 1207 * 1208 * Context: can sleep 1209 */ 1210 static void disk_release(struct device *dev) 1211 { 1212 struct gendisk *disk = dev_to_disk(dev); 1213 1214 might_sleep(); 1215 WARN_ON_ONCE(disk_live(disk)); 1216 1217 blk_trace_remove(disk->queue); 1218 1219 /* 1220 * To undo the all initialization from blk_mq_init_allocated_queue in 1221 * case of a probe failure where add_disk is never called we have to 1222 * call blk_mq_exit_queue here. We can't do this for the more common 1223 * teardown case (yet) as the tagset can be gone by the time the disk 1224 * is released once it was added. 1225 */ 1226 if (queue_is_mq(disk->queue) && 1227 test_bit(GD_OWNS_QUEUE, &disk->state) && 1228 !test_bit(GD_ADDED, &disk->state)) 1229 blk_mq_exit_queue(disk->queue); 1230 1231 blkcg_exit_disk(disk); 1232 1233 bioset_exit(&disk->bio_split); 1234 1235 disk_release_events(disk); 1236 kfree(disk->random); 1237 disk_free_zone_resources(disk); 1238 xa_destroy(&disk->part_tbl); 1239 1240 disk->queue->disk = NULL; 1241 blk_put_queue(disk->queue); 1242 1243 if (test_bit(GD_ADDED, &disk->state) && disk->fops->free_disk) 1244 disk->fops->free_disk(disk); 1245 1246 bdev_drop(disk->part0); /* frees the disk */ 1247 } 1248 1249 static int block_uevent(const struct device *dev, struct kobj_uevent_env *env) 1250 { 1251 const struct gendisk *disk = dev_to_disk(dev); 1252 1253 return add_uevent_var(env, "DISKSEQ=%llu", disk->diskseq); 1254 } 1255 1256 const struct class block_class = { 1257 .name = "block", 1258 .dev_uevent = block_uevent, 1259 }; 1260 1261 static char *block_devnode(const struct device *dev, umode_t *mode, 1262 kuid_t *uid, kgid_t *gid) 1263 { 1264 struct gendisk *disk = dev_to_disk(dev); 1265 1266 if (disk->fops->devnode) 1267 return disk->fops->devnode(disk, mode); 1268 return NULL; 1269 } 1270 1271 const struct device_type disk_type = { 1272 .name = "disk", 1273 .groups = disk_attr_groups, 1274 .release = disk_release, 1275 .devnode = block_devnode, 1276 }; 1277 1278 #ifdef CONFIG_PROC_FS 1279 /* 1280 * aggregate disk stat collector. Uses the same stats that the sysfs 1281 * entries do, above, but makes them available through one seq_file. 1282 * 1283 * The output looks suspiciously like /proc/partitions with a bunch of 1284 * extra fields. 1285 */ 1286 static int diskstats_show(struct seq_file *seqf, void *v) 1287 { 1288 struct gendisk *gp = v; 1289 struct block_device *hd; 1290 unsigned int inflight; 1291 struct disk_stats stat; 1292 unsigned long idx; 1293 1294 /* 1295 if (&disk_to_dev(gp)->kobj.entry == block_class.devices.next) 1296 seq_puts(seqf, "major minor name" 1297 " rio rmerge rsect ruse wio wmerge " 1298 "wsect wuse running use aveq" 1299 "\n\n"); 1300 */ 1301 1302 rcu_read_lock(); 1303 xa_for_each(&gp->part_tbl, idx, hd) { 1304 if (bdev_is_partition(hd) && !bdev_nr_sectors(hd)) 1305 continue; 1306 1307 inflight = part_in_flight(hd); 1308 if (inflight) { 1309 part_stat_lock(); 1310 update_io_ticks(hd, jiffies, true); 1311 part_stat_unlock(); 1312 } 1313 part_stat_read_all(hd, &stat); 1314 seq_put_decimal_ull_width(seqf, "", MAJOR(hd->bd_dev), 4); 1315 seq_put_decimal_ull_width(seqf, " ", MINOR(hd->bd_dev), 7); 1316 seq_printf(seqf, " %pg", hd); 1317 seq_put_decimal_ull(seqf, " ", stat.ios[STAT_READ]); 1318 seq_put_decimal_ull(seqf, " ", stat.merges[STAT_READ]); 1319 seq_put_decimal_ull(seqf, " ", stat.sectors[STAT_READ]); 1320 seq_put_decimal_ull(seqf, " ", (unsigned int)div_u64(stat.nsecs[STAT_READ], 1321 NSEC_PER_MSEC)); 1322 seq_put_decimal_ull(seqf, " ", stat.ios[STAT_WRITE]); 1323 seq_put_decimal_ull(seqf, " ", stat.merges[STAT_WRITE]); 1324 seq_put_decimal_ull(seqf, " ", stat.sectors[STAT_WRITE]); 1325 seq_put_decimal_ull(seqf, " ", (unsigned int)div_u64(stat.nsecs[STAT_WRITE], 1326 NSEC_PER_MSEC)); 1327 seq_put_decimal_ull(seqf, " ", inflight); 1328 seq_put_decimal_ull(seqf, " ", jiffies_to_msecs(stat.io_ticks)); 1329 seq_put_decimal_ull(seqf, " ", (unsigned int)div_u64(stat.nsecs[STAT_READ] + 1330 stat.nsecs[STAT_WRITE] + 1331 stat.nsecs[STAT_DISCARD] + 1332 stat.nsecs[STAT_FLUSH], 1333 NSEC_PER_MSEC)); 1334 seq_put_decimal_ull(seqf, " ", stat.ios[STAT_DISCARD]); 1335 seq_put_decimal_ull(seqf, " ", stat.merges[STAT_DISCARD]); 1336 seq_put_decimal_ull(seqf, " ", stat.sectors[STAT_DISCARD]); 1337 seq_put_decimal_ull(seqf, " ", (unsigned int)div_u64(stat.nsecs[STAT_DISCARD], 1338 NSEC_PER_MSEC)); 1339 seq_put_decimal_ull(seqf, " ", stat.ios[STAT_FLUSH]); 1340 seq_put_decimal_ull(seqf, " ", (unsigned int)div_u64(stat.nsecs[STAT_FLUSH], 1341 NSEC_PER_MSEC)); 1342 seq_putc(seqf, '\n'); 1343 } 1344 rcu_read_unlock(); 1345 1346 return 0; 1347 } 1348 1349 static const struct seq_operations diskstats_op = { 1350 .start = disk_seqf_start, 1351 .next = disk_seqf_next, 1352 .stop = disk_seqf_stop, 1353 .show = diskstats_show 1354 }; 1355 1356 static int __init proc_genhd_init(void) 1357 { 1358 proc_create_seq("diskstats", 0, NULL, &diskstats_op); 1359 proc_create_seq("partitions", 0, NULL, &partitions_op); 1360 return 0; 1361 } 1362 module_init(proc_genhd_init); 1363 #endif /* CONFIG_PROC_FS */ 1364 1365 dev_t part_devt(struct gendisk *disk, u8 partno) 1366 { 1367 struct block_device *part; 1368 dev_t devt = 0; 1369 1370 rcu_read_lock(); 1371 part = xa_load(&disk->part_tbl, partno); 1372 if (part) 1373 devt = part->bd_dev; 1374 rcu_read_unlock(); 1375 1376 return devt; 1377 } 1378 1379 struct gendisk *__alloc_disk_node(struct request_queue *q, int node_id, 1380 struct lock_class_key *lkclass) 1381 { 1382 struct gendisk *disk; 1383 1384 disk = kzalloc_node(sizeof(struct gendisk), GFP_KERNEL, node_id); 1385 if (!disk) 1386 return NULL; 1387 1388 if (bioset_init(&disk->bio_split, BIO_POOL_SIZE, 0, 0)) 1389 goto out_free_disk; 1390 1391 disk->bdi = bdi_alloc(node_id); 1392 if (!disk->bdi) 1393 goto out_free_bioset; 1394 1395 /* bdev_alloc() might need the queue, set before the first call */ 1396 disk->queue = q; 1397 1398 disk->part0 = bdev_alloc(disk, 0); 1399 if (!disk->part0) 1400 goto out_free_bdi; 1401 1402 disk->node_id = node_id; 1403 mutex_init(&disk->open_mutex); 1404 xa_init(&disk->part_tbl); 1405 if (xa_insert(&disk->part_tbl, 0, disk->part0, GFP_KERNEL)) 1406 goto out_destroy_part_tbl; 1407 1408 if (blkcg_init_disk(disk)) 1409 goto out_erase_part0; 1410 1411 disk_init_zone_resources(disk); 1412 rand_initialize_disk(disk); 1413 disk_to_dev(disk)->class = &block_class; 1414 disk_to_dev(disk)->type = &disk_type; 1415 device_initialize(disk_to_dev(disk)); 1416 inc_diskseq(disk); 1417 q->disk = disk; 1418 lockdep_init_map(&disk->lockdep_map, "(bio completion)", lkclass, 0); 1419 #ifdef CONFIG_BLOCK_HOLDER_DEPRECATED 1420 INIT_LIST_HEAD(&disk->slave_bdevs); 1421 #endif 1422 return disk; 1423 1424 out_erase_part0: 1425 xa_erase(&disk->part_tbl, 0); 1426 out_destroy_part_tbl: 1427 xa_destroy(&disk->part_tbl); 1428 disk->part0->bd_disk = NULL; 1429 bdev_drop(disk->part0); 1430 out_free_bdi: 1431 bdi_put(disk->bdi); 1432 out_free_bioset: 1433 bioset_exit(&disk->bio_split); 1434 out_free_disk: 1435 kfree(disk); 1436 return NULL; 1437 } 1438 1439 struct gendisk *__blk_alloc_disk(struct queue_limits *lim, int node, 1440 struct lock_class_key *lkclass) 1441 { 1442 struct queue_limits default_lim = { }; 1443 struct request_queue *q; 1444 struct gendisk *disk; 1445 1446 q = blk_alloc_queue(lim ? lim : &default_lim, node); 1447 if (IS_ERR(q)) 1448 return ERR_CAST(q); 1449 1450 disk = __alloc_disk_node(q, node, lkclass); 1451 if (!disk) { 1452 blk_put_queue(q); 1453 return ERR_PTR(-ENOMEM); 1454 } 1455 set_bit(GD_OWNS_QUEUE, &disk->state); 1456 return disk; 1457 } 1458 EXPORT_SYMBOL(__blk_alloc_disk); 1459 1460 /** 1461 * put_disk - decrements the gendisk refcount 1462 * @disk: the struct gendisk to decrement the refcount for 1463 * 1464 * This decrements the refcount for the struct gendisk. When this reaches 0 1465 * we'll have disk_release() called. 1466 * 1467 * Note: for blk-mq disk put_disk must be called before freeing the tag_set 1468 * when handling probe errors (that is before add_disk() is called). 1469 * 1470 * Context: Any context, but the last reference must not be dropped from 1471 * atomic context. 1472 */ 1473 void put_disk(struct gendisk *disk) 1474 { 1475 if (disk) 1476 put_device(disk_to_dev(disk)); 1477 } 1478 EXPORT_SYMBOL(put_disk); 1479 1480 static void set_disk_ro_uevent(struct gendisk *gd, int ro) 1481 { 1482 char event[] = "DISK_RO=1"; 1483 char *envp[] = { event, NULL }; 1484 1485 if (!ro) 1486 event[8] = '0'; 1487 kobject_uevent_env(&disk_to_dev(gd)->kobj, KOBJ_CHANGE, envp); 1488 } 1489 1490 /** 1491 * set_disk_ro - set a gendisk read-only 1492 * @disk: gendisk to operate on 1493 * @read_only: %true to set the disk read-only, %false set the disk read/write 1494 * 1495 * This function is used to indicate whether a given disk device should have its 1496 * read-only flag set. set_disk_ro() is typically used by device drivers to 1497 * indicate whether the underlying physical device is write-protected. 1498 */ 1499 void set_disk_ro(struct gendisk *disk, bool read_only) 1500 { 1501 if (read_only) { 1502 if (test_and_set_bit(GD_READ_ONLY, &disk->state)) 1503 return; 1504 } else { 1505 if (!test_and_clear_bit(GD_READ_ONLY, &disk->state)) 1506 return; 1507 } 1508 set_disk_ro_uevent(disk, read_only); 1509 } 1510 EXPORT_SYMBOL(set_disk_ro); 1511 1512 void inc_diskseq(struct gendisk *disk) 1513 { 1514 disk->diskseq = atomic64_inc_return(&diskseq); 1515 } 1516