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