dasd_genhd.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. #define KMSG_COMPONENT "dasd"
  14. #include <linux/interrupt.h>
  15. #include <linux/fs.h>
  16. #include <linux/blkpg.h>
  17. #include <linux/uaccess.h>
  18. /* This is ugly... */
  19. #define PRINTK_HEADER "dasd_gendisk:"
  20. #include "dasd_int.h"
  21. /*
  22. * Allocate and register gendisk structure for device.
  23. */
  24. int dasd_gendisk_alloc(struct dasd_block *block)
  25. {
  26. struct gendisk *gdp;
  27. struct dasd_device *base;
  28. int len;
  29. /* Make sure the minor for this device exists. */
  30. base = block->base;
  31. if (base->devindex >= DASD_PER_MAJOR)
  32. return -EBUSY;
  33. gdp = alloc_disk(1 << DASD_PARTN_BITS);
  34. if (!gdp)
  35. return -ENOMEM;
  36. /* Initialize gendisk structure. */
  37. gdp->major = DASD_MAJOR;
  38. gdp->first_minor = base->devindex << DASD_PARTN_BITS;
  39. gdp->fops = &dasd_device_operations;
  40. /*
  41. * Set device name.
  42. * dasda - dasdz : 26 devices
  43. * dasdaa - dasdzz : 676 devices, added up = 702
  44. * dasdaaa - dasdzzz : 17576 devices, added up = 18278
  45. * dasdaaaa - dasdzzzz : 456976 devices, added up = 475252
  46. */
  47. len = sprintf(gdp->disk_name, "dasd");
  48. if (base->devindex > 25) {
  49. if (base->devindex > 701) {
  50. if (base->devindex > 18277)
  51. len += sprintf(gdp->disk_name + len, "%c",
  52. 'a'+(((base->devindex-18278)
  53. /17576)%26));
  54. len += sprintf(gdp->disk_name + len, "%c",
  55. 'a'+(((base->devindex-702)/676)%26));
  56. }
  57. len += sprintf(gdp->disk_name + len, "%c",
  58. 'a'+(((base->devindex-26)/26)%26));
  59. }
  60. len += sprintf(gdp->disk_name + len, "%c", 'a'+(base->devindex%26));
  61. if (base->features & DASD_FEATURE_READONLY ||
  62. test_bit(DASD_FLAG_DEVICE_RO, &base->flags))
  63. set_disk_ro(gdp, 1);
  64. dasd_add_link_to_gendisk(gdp, base);
  65. gdp->queue = block->request_queue;
  66. block->gdp = gdp;
  67. set_capacity(block->gdp, 0);
  68. device_add_disk(&base->cdev->dev, block->gdp, NULL);
  69. return 0;
  70. }
  71. /*
  72. * Unregister and free gendisk structure for device.
  73. */
  74. void dasd_gendisk_free(struct dasd_block *block)
  75. {
  76. if (block->gdp) {
  77. del_gendisk(block->gdp);
  78. block->gdp->private_data = NULL;
  79. put_disk(block->gdp);
  80. block->gdp = NULL;
  81. }
  82. }
  83. /*
  84. * Trigger a partition detection.
  85. */
  86. int dasd_scan_partitions(struct dasd_block *block)
  87. {
  88. struct block_device *bdev;
  89. int rc;
  90. bdev = blkdev_get_by_dev(disk_devt(block->gdp), FMODE_READ, NULL);
  91. if (IS_ERR(bdev)) {
  92. DBF_DEV_EVENT(DBF_ERR, block->base,
  93. "scan partitions error, blkdev_get returned %ld",
  94. PTR_ERR(bdev));
  95. return -ENODEV;
  96. }
  97. mutex_lock(&bdev->bd_mutex);
  98. rc = bdev_disk_changed(bdev, false);
  99. mutex_unlock(&bdev->bd_mutex);
  100. if (rc)
  101. DBF_DEV_EVENT(DBF_ERR, block->base,
  102. "scan partitions error, rc %d", rc);
  103. /*
  104. * Since the matching blkdev_put call to the blkdev_get in
  105. * this function is not called before dasd_destroy_partitions
  106. * the offline open_count limit needs to be increased from
  107. * 0 to 1. This is done by setting device->bdev (see
  108. * dasd_generic_set_offline). As long as the partition
  109. * detection is running no offline should be allowed. That
  110. * is why the assignment to device->bdev is done AFTER
  111. * the BLKRRPART ioctl.
  112. */
  113. block->bdev = bdev;
  114. return 0;
  115. }
  116. /*
  117. * Remove all inodes in the system for a device, delete the
  118. * partitions and make device unusable by setting its size to zero.
  119. */
  120. void dasd_destroy_partitions(struct dasd_block *block)
  121. {
  122. struct block_device *bdev;
  123. /*
  124. * Get the bdev pointer from the device structure and clear
  125. * device->bdev to lower the offline open_count limit again.
  126. */
  127. bdev = block->bdev;
  128. block->bdev = NULL;
  129. mutex_lock(&bdev->bd_mutex);
  130. blk_drop_partitions(bdev);
  131. mutex_unlock(&bdev->bd_mutex);
  132. /* Matching blkdev_put to the blkdev_get in dasd_scan_partitions. */
  133. blkdev_put(bdev, FMODE_READ);
  134. set_capacity(block->gdp, 0);
  135. }
  136. int dasd_gendisk_init(void)
  137. {
  138. int rc;
  139. /* Register to static dasd major 94 */
  140. rc = register_blkdev(DASD_MAJOR, "dasd");
  141. if (rc != 0) {
  142. pr_warn("Registering the device driver with major number %d failed\n",
  143. DASD_MAJOR);
  144. return rc;
  145. }
  146. return 0;
  147. }
  148. void dasd_gendisk_exit(void)
  149. {
  150. unregister_blkdev(DASD_MAJOR, "dasd");
  151. }