dasd_genhd.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
  3. * Horst Hummel <Horst.Hummel@de.ibm.com>
  4. * Carsten Otte <Cotte@de.ibm.com>
  5. * Martin Schwidefsky <schwidefsky@de.ibm.com>
  6. * Bugreports.to..: <Linux390@de.ibm.com>
  7. * Copyright IBM Corp. 1999, 2001
  8. *
  9. * gendisk related functions for the dasd driver.
  10. *
  11. */
  12. #define KMSG_COMPONENT "dasd"
  13. #include <linux/interrupt.h>
  14. #include <linux/fs.h>
  15. #include <linux/blkpg.h>
  16. #include <asm/uaccess.h>
  17. /* This is ugly... */
  18. #define PRINTK_HEADER "dasd_gendisk:"
  19. #include "dasd_int.h"
  20. /*
  21. * Allocate and register gendisk structure for device.
  22. */
  23. int dasd_gendisk_alloc(struct dasd_block *block)
  24. {
  25. struct gendisk *gdp;
  26. struct dasd_device *base;
  27. int len;
  28. /* Make sure the minor for this device exists. */
  29. base = block->base;
  30. if (base->devindex >= DASD_PER_MAJOR)
  31. return -EBUSY;
  32. gdp = alloc_disk(1 << DASD_PARTN_BITS);
  33. if (!gdp)
  34. return -ENOMEM;
  35. /* Initialize gendisk structure. */
  36. gdp->major = DASD_MAJOR;
  37. gdp->first_minor = base->devindex << DASD_PARTN_BITS;
  38. gdp->fops = &dasd_device_operations;
  39. /*
  40. * Set device name.
  41. * dasda - dasdz : 26 devices
  42. * dasdaa - dasdzz : 676 devices, added up = 702
  43. * dasdaaa - dasdzzz : 17576 devices, added up = 18278
  44. * dasdaaaa - dasdzzzz : 456976 devices, added up = 475252
  45. */
  46. len = sprintf(gdp->disk_name, "dasd");
  47. if (base->devindex > 25) {
  48. if (base->devindex > 701) {
  49. if (base->devindex > 18277)
  50. len += sprintf(gdp->disk_name + len, "%c",
  51. 'a'+(((base->devindex-18278)
  52. /17576)%26));
  53. len += sprintf(gdp->disk_name + len, "%c",
  54. 'a'+(((base->devindex-702)/676)%26));
  55. }
  56. len += sprintf(gdp->disk_name + len, "%c",
  57. 'a'+(((base->devindex-26)/26)%26));
  58. }
  59. len += sprintf(gdp->disk_name + len, "%c", 'a'+(base->devindex%26));
  60. if (base->features & DASD_FEATURE_READONLY ||
  61. test_bit(DASD_FLAG_DEVICE_RO, &base->flags))
  62. set_disk_ro(gdp, 1);
  63. dasd_add_link_to_gendisk(gdp, base);
  64. gdp->queue = block->request_queue;
  65. block->gdp = gdp;
  66. set_capacity(block->gdp, 0);
  67. device_add_disk(&base->cdev->dev, block->gdp);
  68. return 0;
  69. }
  70. /*
  71. * Unregister and free gendisk structure for device.
  72. */
  73. void dasd_gendisk_free(struct dasd_block *block)
  74. {
  75. if (block->gdp) {
  76. del_gendisk(block->gdp);
  77. block->gdp->private_data = NULL;
  78. put_disk(block->gdp);
  79. block->gdp = NULL;
  80. }
  81. }
  82. /*
  83. * Trigger a partition detection.
  84. */
  85. int dasd_scan_partitions(struct dasd_block *block)
  86. {
  87. struct block_device *bdev;
  88. int rc;
  89. bdev = bdget_disk(block->gdp, 0);
  90. if (!bdev) {
  91. DBF_DEV_EVENT(DBF_ERR, block->base, "%s",
  92. "scan partitions error, bdget returned NULL");
  93. return -ENODEV;
  94. }
  95. rc = blkdev_get(bdev, FMODE_READ, NULL);
  96. if (rc < 0) {
  97. DBF_DEV_EVENT(DBF_ERR, block->base,
  98. "scan partitions error, blkdev_get returned %d",
  99. rc);
  100. return -ENODEV;
  101. }
  102. rc = blkdev_reread_part(bdev);
  103. if (rc)
  104. DBF_DEV_EVENT(DBF_ERR, block->base,
  105. "scan partitions error, rc %d", rc);
  106. /*
  107. * Since the matching blkdev_put call to the blkdev_get in
  108. * this function is not called before dasd_destroy_partitions
  109. * the offline open_count limit needs to be increased from
  110. * 0 to 1. This is done by setting device->bdev (see
  111. * dasd_generic_set_offline). As long as the partition
  112. * detection is running no offline should be allowed. That
  113. * is why the assignment to device->bdev is done AFTER
  114. * the BLKRRPART ioctl.
  115. */
  116. block->bdev = bdev;
  117. return 0;
  118. }
  119. /*
  120. * Remove all inodes in the system for a device, delete the
  121. * partitions and make device unusable by setting its size to zero.
  122. */
  123. void dasd_destroy_partitions(struct dasd_block *block)
  124. {
  125. /* The two structs have 168/176 byte on 31/64 bit. */
  126. struct blkpg_partition bpart;
  127. struct blkpg_ioctl_arg barg;
  128. struct block_device *bdev;
  129. /*
  130. * Get the bdev pointer from the device structure and clear
  131. * device->bdev to lower the offline open_count limit again.
  132. */
  133. bdev = block->bdev;
  134. block->bdev = NULL;
  135. /*
  136. * See fs/partition/check.c:delete_partition
  137. * Can't call delete_partitions directly. Use ioctl.
  138. * The ioctl also does locking and invalidation.
  139. */
  140. memset(&bpart, 0, sizeof(struct blkpg_partition));
  141. memset(&barg, 0, sizeof(struct blkpg_ioctl_arg));
  142. barg.data = (void __force __user *) &bpart;
  143. barg.op = BLKPG_DEL_PARTITION;
  144. for (bpart.pno = block->gdp->minors - 1; bpart.pno > 0; bpart.pno--)
  145. ioctl_by_bdev(bdev, BLKPG, (unsigned long) &barg);
  146. invalidate_partition(block->gdp, 0);
  147. /* Matching blkdev_put to the blkdev_get in dasd_scan_partitions. */
  148. blkdev_put(bdev, FMODE_READ);
  149. set_capacity(block->gdp, 0);
  150. }
  151. int dasd_gendisk_init(void)
  152. {
  153. int rc;
  154. /* Register to static dasd major 94 */
  155. rc = register_blkdev(DASD_MAJOR, "dasd");
  156. if (rc != 0) {
  157. pr_warn("Registering the device driver with major number %d failed\n",
  158. DASD_MAJOR);
  159. return rc;
  160. }
  161. return 0;
  162. }
  163. void dasd_gendisk_exit(void)
  164. {
  165. unregister_blkdev(DASD_MAJOR, "dasd");
  166. }