dasd_genhd.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. static void dasd_gd_free(struct gendisk *gdp);
  22. module_param(queue_depth, uint, 0444);
  23. MODULE_PARM_DESC(queue_depth, "Default queue depth for new DASD devices");
  24. module_param(nr_hw_queues, uint, 0444);
  25. MODULE_PARM_DESC(nr_hw_queues, "Default number of hardware queues for new DASD devices");
  26. /*
  27. * Set device name.
  28. * dasda - dasdz : 26 devices
  29. * dasdaa - dasdzz : 676 devices, added up = 702
  30. * dasdaaa - dasdzzz : 17576 devices, added up = 18278
  31. * dasdaaaa - dasdzzzz : 456976 devices, added up = 475252
  32. */
  33. static int dasd_name_format(char *prefix, int index, char *buf, int buflen)
  34. {
  35. const int base = 'z' - 'a' + 1;
  36. char *begin = buf + strlen(prefix);
  37. char *end = buf + buflen;
  38. char *p;
  39. int unit;
  40. p = end - 1;
  41. *p = '\0';
  42. unit = base;
  43. do {
  44. if (p == begin)
  45. return -EINVAL;
  46. *--p = 'a' + (index % unit);
  47. index = (index / unit) - 1;
  48. } while (index >= 0);
  49. memmove(begin, p, end - p);
  50. memcpy(buf, prefix, strlen(prefix));
  51. return 0;
  52. }
  53. /*
  54. * Allocate and register gendisk structure for device.
  55. */
  56. int dasd_gendisk_alloc(struct dasd_block *block)
  57. {
  58. struct queue_limits lim = {
  59. /*
  60. * With page sized segments, each segment can be translated into
  61. * one idaw/tidaw.
  62. */
  63. .max_segment_size = PAGE_SIZE,
  64. .seg_boundary_mask = PAGE_SIZE - 1,
  65. .max_segments = USHRT_MAX,
  66. };
  67. struct gendisk *gdp;
  68. struct dasd_device *base;
  69. unsigned int devindex;
  70. int rc;
  71. /* Make sure the minor for this device exists. */
  72. base = block->base;
  73. devindex = base->devindex;
  74. if (devindex >= DASD_PER_MAJOR)
  75. return -EBUSY;
  76. block->tag_set.ops = &dasd_mq_ops;
  77. block->tag_set.cmd_size = sizeof(struct dasd_ccw_req);
  78. block->tag_set.nr_hw_queues = nr_hw_queues;
  79. block->tag_set.queue_depth = queue_depth;
  80. block->tag_set.numa_node = NUMA_NO_NODE;
  81. rc = blk_mq_alloc_tag_set(&block->tag_set);
  82. if (rc)
  83. return rc;
  84. gdp = blk_mq_alloc_disk(&block->tag_set, &lim, block);
  85. if (IS_ERR(gdp)) {
  86. blk_mq_free_tag_set(&block->tag_set);
  87. return PTR_ERR(gdp);
  88. }
  89. /* Initialize gendisk structure. */
  90. gdp->major = DASD_MAJOR;
  91. gdp->first_minor = devindex << DASD_PARTN_BITS;
  92. gdp->minors = 1 << DASD_PARTN_BITS;
  93. gdp->fops = &dasd_device_operations;
  94. rc = dasd_name_format("dasd", devindex, gdp->disk_name, sizeof(gdp->disk_name));
  95. if (rc) {
  96. DBF_DEV_EVENT(DBF_ERR, block->base,
  97. "setting disk name failed, rc %d", rc);
  98. dasd_gd_free(gdp);
  99. return rc;
  100. }
  101. if (base->features & DASD_FEATURE_READONLY ||
  102. test_bit(DASD_FLAG_DEVICE_RO, &base->flags))
  103. set_disk_ro(gdp, 1);
  104. dasd_add_link_to_gendisk(gdp, base);
  105. block->gdp = gdp;
  106. set_capacity(block->gdp, 0);
  107. rc = device_add_disk(&base->cdev->dev, block->gdp, NULL);
  108. if (rc) {
  109. dasd_gendisk_free(block);
  110. return rc;
  111. }
  112. return 0;
  113. }
  114. /*
  115. * Free gendisk structure
  116. */
  117. static void dasd_gd_free(struct gendisk *gd)
  118. {
  119. del_gendisk(gd);
  120. gd->private_data = NULL;
  121. put_disk(gd);
  122. }
  123. /*
  124. * Unregister and free gendisk structure for device.
  125. */
  126. void dasd_gendisk_free(struct dasd_block *block)
  127. {
  128. if (block->gdp) {
  129. dasd_gd_free(block->gdp);
  130. block->gdp = NULL;
  131. blk_mq_free_tag_set(&block->tag_set);
  132. }
  133. }
  134. /*
  135. * Trigger a partition detection.
  136. */
  137. int dasd_scan_partitions(struct dasd_block *block)
  138. {
  139. struct file *bdev_file;
  140. int rc;
  141. bdev_file = bdev_file_open_by_dev(disk_devt(block->gdp), BLK_OPEN_READ,
  142. NULL, NULL);
  143. if (IS_ERR(bdev_file)) {
  144. DBF_DEV_EVENT(DBF_ERR, block->base,
  145. "scan partitions error, blkdev_get returned %ld",
  146. PTR_ERR(bdev_file));
  147. return -ENODEV;
  148. }
  149. mutex_lock(&block->gdp->open_mutex);
  150. rc = bdev_disk_changed(block->gdp, false);
  151. mutex_unlock(&block->gdp->open_mutex);
  152. if (rc)
  153. DBF_DEV_EVENT(DBF_ERR, block->base,
  154. "scan partitions error, rc %d", rc);
  155. /*
  156. * Since the matching fput() call to the
  157. * bdev_file_open_by_path() in this function is not called before
  158. * dasd_destroy_partitions the offline open_count limit needs to be
  159. * increased from 0 to 1. This is done by setting device->bdev_file
  160. * (see dasd_generic_set_offline). As long as the partition detection
  161. * is running no offline should be allowed. That is why the assignment
  162. * to block->bdev_file is done AFTER the BLKRRPART ioctl.
  163. */
  164. block->bdev_file = bdev_file;
  165. return 0;
  166. }
  167. /*
  168. * Remove all inodes in the system for a device, delete the
  169. * partitions and make device unusable by setting its size to zero.
  170. */
  171. void dasd_destroy_partitions(struct dasd_block *block)
  172. {
  173. struct file *bdev_file;
  174. /*
  175. * Get the bdev_file pointer from the device structure and clear
  176. * device->bdev_file to lower the offline open_count limit again.
  177. */
  178. bdev_file = block->bdev_file;
  179. block->bdev_file = NULL;
  180. mutex_lock(&file_bdev(bdev_file)->bd_disk->open_mutex);
  181. bdev_disk_changed(file_bdev(bdev_file)->bd_disk, true);
  182. mutex_unlock(&file_bdev(bdev_file)->bd_disk->open_mutex);
  183. /* Matching blkdev_put to the blkdev_get in dasd_scan_partitions. */
  184. fput(bdev_file);
  185. }
  186. int dasd_gendisk_init(void)
  187. {
  188. int rc;
  189. /* Register to static dasd major 94 */
  190. rc = register_blkdev(DASD_MAJOR, "dasd");
  191. if (rc != 0) {
  192. pr_warn("Registering the device driver with major number %d failed\n",
  193. DASD_MAJOR);
  194. return rc;
  195. }
  196. return 0;
  197. }
  198. void dasd_gendisk_exit(void)
  199. {
  200. unregister_blkdev(DASD_MAJOR, "dasd");
  201. }