dasd_genhd.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Author(s)......: Holger Smolinski <[email protected]>
  4. * Horst Hummel <[email protected]>
  5. * Carsten Otte <[email protected]>
  6. * Martin Schwidefsky <[email protected]>
  7. * Bugreports.to..: <[email protected]>
  8. * Copyright IBM Corp. 1999, 2001
  9. *
  10. * gendisk related functions for the dasd driver.
  11. *
  12. */
  13. #define KMSG_COMPONENT "dasd"
  14. #include <linux/interrupt.h>
  15. #include <linux/major.h>
  16. #include <linux/fs.h>
  17. #include <linux/blkpg.h>
  18. #include <linux/uaccess.h>
  19. /* This is ugly... */
  20. #define PRINTK_HEADER "dasd_gendisk:"
  21. #include "dasd_int.h"
  22. static unsigned int queue_depth = 32;
  23. static unsigned int nr_hw_queues = 4;
  24. module_param(queue_depth, uint, 0444);
  25. MODULE_PARM_DESC(queue_depth, "Default queue depth for new DASD devices");
  26. module_param(nr_hw_queues, uint, 0444);
  27. MODULE_PARM_DESC(nr_hw_queues, "Default number of hardware queues for new DASD devices");
  28. /*
  29. * Allocate and register gendisk structure for device.
  30. */
  31. int dasd_gendisk_alloc(struct dasd_block *block)
  32. {
  33. struct gendisk *gdp;
  34. struct dasd_device *base;
  35. int len, rc;
  36. /* Make sure the minor for this device exists. */
  37. base = block->base;
  38. if (base->devindex >= DASD_PER_MAJOR)
  39. return -EBUSY;
  40. block->tag_set.ops = &dasd_mq_ops;
  41. block->tag_set.cmd_size = sizeof(struct dasd_ccw_req);
  42. block->tag_set.nr_hw_queues = nr_hw_queues;
  43. block->tag_set.queue_depth = queue_depth;
  44. block->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
  45. block->tag_set.numa_node = NUMA_NO_NODE;
  46. rc = blk_mq_alloc_tag_set(&block->tag_set);
  47. if (rc)
  48. return rc;
  49. gdp = blk_mq_alloc_disk(&block->tag_set, block);
  50. if (IS_ERR(gdp)) {
  51. blk_mq_free_tag_set(&block->tag_set);
  52. return PTR_ERR(gdp);
  53. }
  54. /* Initialize gendisk structure. */
  55. gdp->major = DASD_MAJOR;
  56. gdp->first_minor = base->devindex << DASD_PARTN_BITS;
  57. gdp->minors = 1 << DASD_PARTN_BITS;
  58. gdp->fops = &dasd_device_operations;
  59. /*
  60. * Set device name.
  61. * dasda - dasdz : 26 devices
  62. * dasdaa - dasdzz : 676 devices, added up = 702
  63. * dasdaaa - dasdzzz : 17576 devices, added up = 18278
  64. * dasdaaaa - dasdzzzz : 456976 devices, added up = 475252
  65. */
  66. len = sprintf(gdp->disk_name, "dasd");
  67. if (base->devindex > 25) {
  68. if (base->devindex > 701) {
  69. if (base->devindex > 18277)
  70. len += sprintf(gdp->disk_name + len, "%c",
  71. 'a'+(((base->devindex-18278)
  72. /17576)%26));
  73. len += sprintf(gdp->disk_name + len, "%c",
  74. 'a'+(((base->devindex-702)/676)%26));
  75. }
  76. len += sprintf(gdp->disk_name + len, "%c",
  77. 'a'+(((base->devindex-26)/26)%26));
  78. }
  79. len += sprintf(gdp->disk_name + len, "%c", 'a'+(base->devindex%26));
  80. if (base->features & DASD_FEATURE_READONLY ||
  81. test_bit(DASD_FLAG_DEVICE_RO, &base->flags))
  82. set_disk_ro(gdp, 1);
  83. dasd_add_link_to_gendisk(gdp, base);
  84. block->gdp = gdp;
  85. set_capacity(block->gdp, 0);
  86. rc = device_add_disk(&base->cdev->dev, block->gdp, NULL);
  87. if (rc) {
  88. dasd_gendisk_free(block);
  89. return rc;
  90. }
  91. return 0;
  92. }
  93. /*
  94. * Unregister and free gendisk structure for device.
  95. */
  96. void dasd_gendisk_free(struct dasd_block *block)
  97. {
  98. if (block->gdp) {
  99. del_gendisk(block->gdp);
  100. block->gdp->private_data = NULL;
  101. put_disk(block->gdp);
  102. block->gdp = NULL;
  103. blk_mq_free_tag_set(&block->tag_set);
  104. }
  105. }
  106. /*
  107. * Trigger a partition detection.
  108. */
  109. int dasd_scan_partitions(struct dasd_block *block)
  110. {
  111. struct block_device *bdev;
  112. int rc;
  113. bdev = blkdev_get_by_dev(disk_devt(block->gdp), FMODE_READ, NULL);
  114. if (IS_ERR(bdev)) {
  115. DBF_DEV_EVENT(DBF_ERR, block->base,
  116. "scan partitions error, blkdev_get returned %ld",
  117. PTR_ERR(bdev));
  118. return -ENODEV;
  119. }
  120. mutex_lock(&block->gdp->open_mutex);
  121. rc = bdev_disk_changed(block->gdp, false);
  122. mutex_unlock(&block->gdp->open_mutex);
  123. if (rc)
  124. DBF_DEV_EVENT(DBF_ERR, block->base,
  125. "scan partitions error, rc %d", rc);
  126. /*
  127. * Since the matching blkdev_put call to the blkdev_get in
  128. * this function is not called before dasd_destroy_partitions
  129. * the offline open_count limit needs to be increased from
  130. * 0 to 1. This is done by setting device->bdev (see
  131. * dasd_generic_set_offline). As long as the partition
  132. * detection is running no offline should be allowed. That
  133. * is why the assignment to device->bdev is done AFTER
  134. * the BLKRRPART ioctl.
  135. */
  136. block->bdev = bdev;
  137. return 0;
  138. }
  139. /*
  140. * Remove all inodes in the system for a device, delete the
  141. * partitions and make device unusable by setting its size to zero.
  142. */
  143. void dasd_destroy_partitions(struct dasd_block *block)
  144. {
  145. struct block_device *bdev;
  146. /*
  147. * Get the bdev pointer from the device structure and clear
  148. * device->bdev to lower the offline open_count limit again.
  149. */
  150. bdev = block->bdev;
  151. block->bdev = NULL;
  152. mutex_lock(&bdev->bd_disk->open_mutex);
  153. bdev_disk_changed(bdev->bd_disk, true);
  154. mutex_unlock(&bdev->bd_disk->open_mutex);
  155. /* Matching blkdev_put to the blkdev_get in dasd_scan_partitions. */
  156. blkdev_put(bdev, FMODE_READ);
  157. }
  158. int dasd_gendisk_init(void)
  159. {
  160. int rc;
  161. /* Register to static dasd major 94 */
  162. rc = register_blkdev(DASD_MAJOR, "dasd");
  163. if (rc != 0) {
  164. pr_warn("Registering the device driver with major number %d failed\n",
  165. DASD_MAJOR);
  166. return rc;
  167. }
  168. return 0;
  169. }
  170. void dasd_gendisk_exit(void)
  171. {
  172. unregister_blkdev(DASD_MAJOR, "dasd");
  173. }