blob: 90f39f79f5d75ae2ad1700223adb519e101abd33 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * 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 Carstensa53c8fa2012-07-20 11:15:04 +02007 * Copyright IBM Corp. 1999, 2001
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * gendisk related functions for the dasd driver.
10 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
Stefan Haberlandfc19f382009-03-26 15:23:49 +010013#define KMSG_COMPONENT "dasd"
14
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#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 Weinhuber8e09f212008-01-26 14:11:23 +010029int dasd_gendisk_alloc(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030{
31 struct gendisk *gdp;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010032 struct dasd_device *base;
Horst Hummelc6eb7b72005-09-03 15:57:58 -070033 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35 /* Make sure the minor for this device exists. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010036 base = block->base;
37 if (base->devindex >= DASD_PER_MAJOR)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 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 Weinhuber8e09f212008-01-26 14:11:23 +010046 gdp->first_minor = base->devindex << DASD_PARTN_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 gdp->fops = &dasd_device_operations;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010048 gdp->driverfs_dev = &base->cdev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
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 Weinhuber8e09f212008-01-26 14:11:23 +010058 if (base->devindex > 25) {
59 if (base->devindex > 701) {
60 if (base->devindex > 18277)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 len += sprintf(gdp->disk_name + len, "%c",
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010062 'a'+(((base->devindex-18278)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 /17576)%26));
64 len += sprintf(gdp->disk_name + len, "%c",
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010065 'a'+(((base->devindex-702)/676)%26));
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 }
67 len += sprintf(gdp->disk_name + len, "%c",
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010068 'a'+(((base->devindex-26)/26)%26));
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010070 len += sprintf(gdp->disk_name + len, "%c", 'a'+(base->devindex%26));
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Stefan Weinhuber33b62a32010-03-08 12:26:24 +010072 if (base->features & DASD_FEATURE_READONLY ||
73 test_bit(DASD_FLAG_DEVICE_RO, &base->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 set_disk_ro(gdp, 1);
Stefan Weinhuber65f8da42011-04-20 10:15:30 +020075 dasd_add_link_to_gendisk(gdp, base);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010076 gdp->queue = block->request_queue;
77 block->gdp = gdp;
78 set_capacity(block->gdp, 0);
79 add_disk(block->gdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 return 0;
81}
82
83/*
84 * Unregister and free gendisk structure for device.
85 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010086void dasd_gendisk_free(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010088 if (block->gdp) {
89 del_gendisk(block->gdp);
Stefan Haberland9eb25122010-02-26 22:37:46 +010090 block->gdp->private_data = NULL;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010091 put_disk(block->gdp);
92 block->gdp = NULL;
Horst Hummel8f617012006-08-30 14:33:33 +020093 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070094}
95
96/*
97 * Trigger a partition detection.
98 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010099int dasd_scan_partitions(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
101 struct block_device *bdev;
Stefan Haberland6ebdf1c2014-11-24 15:04:09 +0100102 int retry, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Stefan Haberland6ebdf1c2014-11-24 15:04:09 +0100104 retry = 5;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100105 bdev = bdget_disk(block->gdp, 0);
Stefan Haberland6ebdf1c2014-11-24 15:04:09 +0100106 if (!bdev) {
107 DBF_DEV_EVENT(DBF_ERR, block->base, "%s",
108 "scan partitions error, bdget returned NULL");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 return -ENODEV;
Stefan Haberland6ebdf1c2014-11-24 15:04:09 +0100110 }
111
112 rc = blkdev_get(bdev, FMODE_READ, NULL);
113 if (rc < 0) {
114 DBF_DEV_EVENT(DBF_ERR, block->base,
115 "scan partitions error, blkdev_get returned %d",
116 rc);
117 return -ENODEV;
118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 /*
120 * See fs/partition/check.c:register_disk,rescan_partitions
121 * Can't call rescan_partitions directly. Use ioctl.
122 */
Stefan Haberland6ebdf1c2014-11-24 15:04:09 +0100123 rc = ioctl_by_bdev(bdev, BLKRRPART, 0);
124 while (rc == -EBUSY && retry > 0) {
125 schedule();
126 rc = ioctl_by_bdev(bdev, BLKRRPART, 0);
127 retry--;
128 DBF_DEV_EVENT(DBF_ERR, block->base,
129 "scan partitions error, retry %d rc %d",
130 retry, rc);
131 }
132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 /*
134 * Since the matching blkdev_put call to the blkdev_get in
135 * this function is not called before dasd_destroy_partitions
136 * the offline open_count limit needs to be increased from
137 * 0 to 1. This is done by setting device->bdev (see
138 * dasd_generic_set_offline). As long as the partition
139 * detection is running no offline should be allowed. That
140 * is why the assignment to device->bdev is done AFTER
141 * the BLKRRPART ioctl.
142 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100143 block->bdev = bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 return 0;
145}
146
147/*
148 * Remove all inodes in the system for a device, delete the
149 * partitions and make device unusable by setting its size to zero.
150 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100151void dasd_destroy_partitions(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
153 /* The two structs have 168/176 byte on 31/64 bit. */
154 struct blkpg_partition bpart;
155 struct blkpg_ioctl_arg barg;
156 struct block_device *bdev;
157
158 /*
159 * Get the bdev pointer from the device structure and clear
160 * device->bdev to lower the offline open_count limit again.
161 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100162 bdev = block->bdev;
163 block->bdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165 /*
166 * See fs/partition/check.c:delete_partition
167 * Can't call delete_partitions directly. Use ioctl.
168 * The ioctl also does locking and invalidation.
169 */
170 memset(&bpart, 0, sizeof(struct blkpg_partition));
171 memset(&barg, 0, sizeof(struct blkpg_ioctl_arg));
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100172 barg.data = (void __force __user *) &bpart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 barg.op = BLKPG_DEL_PARTITION;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100174 for (bpart.pno = block->gdp->minors - 1; bpart.pno > 0; bpart.pno--)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 ioctl_by_bdev(bdev, BLKPG, (unsigned long) &barg);
176
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100177 invalidate_partition(block->gdp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 /* Matching blkdev_put to the blkdev_get in dasd_scan_partitions. */
Al Viro9a1c3542008-02-22 20:40:24 -0500179 blkdev_put(bdev, FMODE_READ);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100180 set_capacity(block->gdp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181}
182
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100183int dasd_gendisk_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
185 int rc;
186
187 /* Register to static dasd major 94 */
188 rc = register_blkdev(DASD_MAJOR, "dasd");
189 if (rc != 0) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100190 pr_warning("Registering the device driver with major number "
191 "%d failed\n", DASD_MAJOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 return rc;
193 }
194 return 0;
195}
196
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100197void dasd_gendisk_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
199 unregister_blkdev(DASD_MAJOR, "dasd");
200}