Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | */ |
| 13 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/interrupt.h> |
| 15 | #include <linux/fs.h> |
| 16 | #include <linux/blkpg.h> |
| 17 | |
| 18 | #include <asm/uaccess.h> |
| 19 | |
| 20 | /* This is ugly... */ |
| 21 | #define PRINTK_HEADER "dasd_gendisk:" |
| 22 | |
| 23 | #include "dasd_int.h" |
| 24 | |
| 25 | /* |
| 26 | * Allocate and register gendisk structure for device. |
| 27 | */ |
Stefan Weinhuber | 8e09f21 | 2008-01-26 14:11:23 +0100 | [diff] [blame] | 28 | int dasd_gendisk_alloc(struct dasd_block *block) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | { |
| 30 | struct gendisk *gdp; |
Stefan Weinhuber | 8e09f21 | 2008-01-26 14:11:23 +0100 | [diff] [blame] | 31 | struct dasd_device *base; |
Horst Hummel | c6eb7b7 | 2005-09-03 15:57:58 -0700 | [diff] [blame] | 32 | int len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
| 34 | /* Make sure the minor for this device exists. */ |
Stefan Weinhuber | 8e09f21 | 2008-01-26 14:11:23 +0100 | [diff] [blame] | 35 | base = block->base; |
| 36 | if (base->devindex >= DASD_PER_MAJOR) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | return -EBUSY; |
| 38 | |
| 39 | gdp = alloc_disk(1 << DASD_PARTN_BITS); |
| 40 | if (!gdp) |
| 41 | return -ENOMEM; |
| 42 | |
| 43 | /* Initialize gendisk structure. */ |
| 44 | gdp->major = DASD_MAJOR; |
Stefan Weinhuber | 8e09f21 | 2008-01-26 14:11:23 +0100 | [diff] [blame] | 45 | gdp->first_minor = base->devindex << DASD_PARTN_BITS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | gdp->fops = &dasd_device_operations; |
Stefan Weinhuber | 8e09f21 | 2008-01-26 14:11:23 +0100 | [diff] [blame] | 47 | gdp->driverfs_dev = &base->cdev->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
| 49 | /* |
| 50 | * Set device name. |
| 51 | * dasda - dasdz : 26 devices |
| 52 | * dasdaa - dasdzz : 676 devices, added up = 702 |
| 53 | * dasdaaa - dasdzzz : 17576 devices, added up = 18278 |
| 54 | * dasdaaaa - dasdzzzz : 456976 devices, added up = 475252 |
| 55 | */ |
| 56 | len = sprintf(gdp->disk_name, "dasd"); |
Stefan Weinhuber | 8e09f21 | 2008-01-26 14:11:23 +0100 | [diff] [blame] | 57 | if (base->devindex > 25) { |
| 58 | if (base->devindex > 701) { |
| 59 | if (base->devindex > 18277) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | len += sprintf(gdp->disk_name + len, "%c", |
Stefan Weinhuber | 8e09f21 | 2008-01-26 14:11:23 +0100 | [diff] [blame] | 61 | 'a'+(((base->devindex-18278) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | /17576)%26)); |
| 63 | len += sprintf(gdp->disk_name + len, "%c", |
Stefan Weinhuber | 8e09f21 | 2008-01-26 14:11:23 +0100 | [diff] [blame] | 64 | 'a'+(((base->devindex-702)/676)%26)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | } |
| 66 | len += sprintf(gdp->disk_name + len, "%c", |
Stefan Weinhuber | 8e09f21 | 2008-01-26 14:11:23 +0100 | [diff] [blame] | 67 | 'a'+(((base->devindex-26)/26)%26)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | } |
Stefan Weinhuber | 8e09f21 | 2008-01-26 14:11:23 +0100 | [diff] [blame] | 69 | len += sprintf(gdp->disk_name + len, "%c", 'a'+(base->devindex%26)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
Stefan Weinhuber | 8e09f21 | 2008-01-26 14:11:23 +0100 | [diff] [blame] | 71 | if (block->base->features & DASD_FEATURE_READONLY) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | set_disk_ro(gdp, 1); |
Stefan Weinhuber | 8e09f21 | 2008-01-26 14:11:23 +0100 | [diff] [blame] | 73 | gdp->private_data = block; |
| 74 | gdp->queue = block->request_queue; |
| 75 | block->gdp = gdp; |
| 76 | set_capacity(block->gdp, 0); |
| 77 | add_disk(block->gdp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | /* |
| 82 | * Unregister and free gendisk structure for device. |
| 83 | */ |
Stefan Weinhuber | 8e09f21 | 2008-01-26 14:11:23 +0100 | [diff] [blame] | 84 | void dasd_gendisk_free(struct dasd_block *block) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | { |
Stefan Weinhuber | 8e09f21 | 2008-01-26 14:11:23 +0100 | [diff] [blame] | 86 | if (block->gdp) { |
| 87 | del_gendisk(block->gdp); |
| 88 | block->gdp->queue = NULL; |
| 89 | put_disk(block->gdp); |
| 90 | block->gdp = NULL; |
Horst Hummel | 8f61701 | 2006-08-30 14:33:33 +0200 | [diff] [blame] | 91 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /* |
| 95 | * Trigger a partition detection. |
| 96 | */ |
Stefan Weinhuber | 8e09f21 | 2008-01-26 14:11:23 +0100 | [diff] [blame] | 97 | int dasd_scan_partitions(struct dasd_block *block) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | { |
| 99 | struct block_device *bdev; |
| 100 | |
Stefan Weinhuber | 8e09f21 | 2008-01-26 14:11:23 +0100 | [diff] [blame] | 101 | bdev = bdget_disk(block->gdp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | if (!bdev || blkdev_get(bdev, FMODE_READ, 1) < 0) |
| 103 | return -ENODEV; |
| 104 | /* |
| 105 | * See fs/partition/check.c:register_disk,rescan_partitions |
| 106 | * Can't call rescan_partitions directly. Use ioctl. |
| 107 | */ |
| 108 | ioctl_by_bdev(bdev, BLKRRPART, 0); |
| 109 | /* |
| 110 | * Since the matching blkdev_put call to the blkdev_get in |
| 111 | * this function is not called before dasd_destroy_partitions |
| 112 | * the offline open_count limit needs to be increased from |
| 113 | * 0 to 1. This is done by setting device->bdev (see |
| 114 | * dasd_generic_set_offline). As long as the partition |
| 115 | * detection is running no offline should be allowed. That |
| 116 | * is why the assignment to device->bdev is done AFTER |
| 117 | * the BLKRRPART ioctl. |
| 118 | */ |
Stefan Weinhuber | 8e09f21 | 2008-01-26 14:11:23 +0100 | [diff] [blame] | 119 | block->bdev = bdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | /* |
| 124 | * Remove all inodes in the system for a device, delete the |
| 125 | * partitions and make device unusable by setting its size to zero. |
| 126 | */ |
Stefan Weinhuber | 8e09f21 | 2008-01-26 14:11:23 +0100 | [diff] [blame] | 127 | void dasd_destroy_partitions(struct dasd_block *block) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | { |
| 129 | /* The two structs have 168/176 byte on 31/64 bit. */ |
| 130 | struct blkpg_partition bpart; |
| 131 | struct blkpg_ioctl_arg barg; |
| 132 | struct block_device *bdev; |
| 133 | |
| 134 | /* |
| 135 | * Get the bdev pointer from the device structure and clear |
| 136 | * device->bdev to lower the offline open_count limit again. |
| 137 | */ |
Stefan Weinhuber | 8e09f21 | 2008-01-26 14:11:23 +0100 | [diff] [blame] | 138 | bdev = block->bdev; |
| 139 | block->bdev = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | |
| 141 | /* |
| 142 | * See fs/partition/check.c:delete_partition |
| 143 | * Can't call delete_partitions directly. Use ioctl. |
| 144 | * The ioctl also does locking and invalidation. |
| 145 | */ |
| 146 | memset(&bpart, 0, sizeof(struct blkpg_partition)); |
| 147 | memset(&barg, 0, sizeof(struct blkpg_ioctl_arg)); |
Heiko Carstens | 2b67fc4 | 2007-02-05 21:16:47 +0100 | [diff] [blame] | 148 | barg.data = (void __force __user *) &bpart; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | barg.op = BLKPG_DEL_PARTITION; |
Stefan Weinhuber | 8e09f21 | 2008-01-26 14:11:23 +0100 | [diff] [blame] | 150 | for (bpart.pno = block->gdp->minors - 1; bpart.pno > 0; bpart.pno--) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | ioctl_by_bdev(bdev, BLKPG, (unsigned long) &barg); |
| 152 | |
Stefan Weinhuber | 8e09f21 | 2008-01-26 14:11:23 +0100 | [diff] [blame] | 153 | invalidate_partition(block->gdp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | /* Matching blkdev_put to the blkdev_get in dasd_scan_partitions. */ |
| 155 | blkdev_put(bdev); |
Stefan Weinhuber | 8e09f21 | 2008-01-26 14:11:23 +0100 | [diff] [blame] | 156 | set_capacity(block->gdp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Stefan Weinhuber | 8e09f21 | 2008-01-26 14:11:23 +0100 | [diff] [blame] | 159 | int dasd_gendisk_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | { |
| 161 | int rc; |
| 162 | |
| 163 | /* Register to static dasd major 94 */ |
| 164 | rc = register_blkdev(DASD_MAJOR, "dasd"); |
| 165 | if (rc != 0) { |
| 166 | MESSAGE(KERN_WARNING, |
| 167 | "Couldn't register successfully to " |
| 168 | "major no %d", DASD_MAJOR); |
| 169 | return rc; |
| 170 | } |
| 171 | return 0; |
| 172 | } |
| 173 | |
Stefan Weinhuber | 8e09f21 | 2008-01-26 14:11:23 +0100 | [diff] [blame] | 174 | void dasd_gendisk_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | { |
| 176 | unregister_blkdev(DASD_MAJOR, "dasd"); |
| 177 | } |