dasd_genhd.c 5.4 KB

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