dasd_genhd.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * File...........: linux/drivers/s390/block/dasd_genhd.c
  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. * (C) IBM Corporation, IBM Deutschland Entwicklung GmbH, 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 <asm/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. gdp->driverfs_dev = &base->cdev->dev;
  41. /*
  42. * Set device name.
  43. * dasda - dasdz : 26 devices
  44. * dasdaa - dasdzz : 676 devices, added up = 702
  45. * dasdaaa - dasdzzz : 17576 devices, added up = 18278
  46. * dasdaaaa - dasdzzzz : 456976 devices, added up = 475252
  47. */
  48. len = sprintf(gdp->disk_name, "dasd");
  49. if (base->devindex > 25) {
  50. if (base->devindex > 701) {
  51. if (base->devindex > 18277)
  52. len += sprintf(gdp->disk_name + len, "%c",
  53. 'a'+(((base->devindex-18278)
  54. /17576)%26));
  55. len += sprintf(gdp->disk_name + len, "%c",
  56. 'a'+(((base->devindex-702)/676)%26));
  57. }
  58. len += sprintf(gdp->disk_name + len, "%c",
  59. 'a'+(((base->devindex-26)/26)%26));
  60. }
  61. len += sprintf(gdp->disk_name + len, "%c", 'a'+(base->devindex%26));
  62. if (base->features & DASD_FEATURE_READONLY ||
  63. test_bit(DASD_FLAG_DEVICE_RO, &base->flags))
  64. set_disk_ro(gdp, 1);
  65. dasd_add_link_to_gendisk(gdp, base);
  66. gdp->queue = block->request_queue;
  67. block->gdp = gdp;
  68. set_capacity(block->gdp, 0);
  69. add_disk(block->gdp);
  70. return 0;
  71. }
  72. /*
  73. * Unregister and free gendisk structure for device.
  74. */
  75. void dasd_gendisk_free(struct dasd_block *block)
  76. {
  77. if (block->gdp) {
  78. del_gendisk(block->gdp);
  79. block->gdp->queue = NULL;
  80. block->gdp->private_data = NULL;
  81. put_disk(block->gdp);
  82. block->gdp = NULL;
  83. }
  84. }
  85. /*
  86. * Trigger a partition detection.
  87. */
  88. int dasd_scan_partitions(struct dasd_block *block)
  89. {
  90. struct block_device *bdev;
  91. bdev = bdget_disk(block->gdp, 0);
  92. if (!bdev || blkdev_get(bdev, FMODE_READ, NULL) < 0)
  93. return -ENODEV;
  94. /*
  95. * See fs/partition/check.c:register_disk,rescan_partitions
  96. * Can't call rescan_partitions directly. Use ioctl.
  97. */
  98. ioctl_by_bdev(bdev, BLKRRPART, 0);
  99. /*
  100. * Since the matching blkdev_put call to the blkdev_get in
  101. * this function is not called before dasd_destroy_partitions
  102. * the offline open_count limit needs to be increased from
  103. * 0 to 1. This is done by setting device->bdev (see
  104. * dasd_generic_set_offline). As long as the partition
  105. * detection is running no offline should be allowed. That
  106. * is why the assignment to device->bdev is done AFTER
  107. * the BLKRRPART ioctl.
  108. */
  109. block->bdev = bdev;
  110. return 0;
  111. }
  112. /*
  113. * Remove all inodes in the system for a device, delete the
  114. * partitions and make device unusable by setting its size to zero.
  115. */
  116. void dasd_destroy_partitions(struct dasd_block *block)
  117. {
  118. /* The two structs have 168/176 byte on 31/64 bit. */
  119. struct blkpg_partition bpart;
  120. struct blkpg_ioctl_arg barg;
  121. struct block_device *bdev;
  122. /*
  123. * Get the bdev pointer from the device structure and clear
  124. * device->bdev to lower the offline open_count limit again.
  125. */
  126. bdev = block->bdev;
  127. block->bdev = NULL;
  128. /*
  129. * See fs/partition/check.c:delete_partition
  130. * Can't call delete_partitions directly. Use ioctl.
  131. * The ioctl also does locking and invalidation.
  132. */
  133. memset(&bpart, 0, sizeof(struct blkpg_partition));
  134. memset(&barg, 0, sizeof(struct blkpg_ioctl_arg));
  135. barg.data = (void __force __user *) &bpart;
  136. barg.op = BLKPG_DEL_PARTITION;
  137. for (bpart.pno = block->gdp->minors - 1; bpart.pno > 0; bpart.pno--)
  138. ioctl_by_bdev(bdev, BLKPG, (unsigned long) &barg);
  139. invalidate_partition(block->gdp, 0);
  140. /* Matching blkdev_put to the blkdev_get in dasd_scan_partitions. */
  141. blkdev_put(bdev, FMODE_READ);
  142. set_capacity(block->gdp, 0);
  143. }
  144. int dasd_gendisk_init(void)
  145. {
  146. int rc;
  147. /* Register to static dasd major 94 */
  148. rc = register_blkdev(DASD_MAJOR, "dasd");
  149. if (rc != 0) {
  150. pr_warning("Registering the device driver with major number "
  151. "%d failed\n", DASD_MAJOR);
  152. return rc;
  153. }
  154. return 0;
  155. }
  156. void dasd_gendisk_exit(void)
  157. {
  158. unregister_blkdev(DASD_MAJOR, "dasd");
  159. }