blob: e2fa759bf2ad42683b571e8a44a434ff3936b408 [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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
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 Weinhuber8e09f212008-01-26 14:11:23 +010057 if (base->devindex > 25) {
58 if (base->devindex > 701) {
59 if (base->devindex > 18277)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 len += sprintf(gdp->disk_name + len, "%c",
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010061 'a'+(((base->devindex-18278)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 /17576)%26));
63 len += sprintf(gdp->disk_name + len, "%c",
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010064 'a'+(((base->devindex-702)/676)%26));
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 }
66 len += sprintf(gdp->disk_name + len, "%c",
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010067 'a'+(((base->devindex-26)/26)%26));
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010069 len += sprintf(gdp->disk_name + len, "%c", 'a'+(base->devindex%26));
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Stefan Weinhuber33b62a32010-03-08 12:26:24 +010071 if (base->features & DASD_FEATURE_READONLY ||
72 test_bit(DASD_FLAG_DEVICE_RO, &base->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 set_disk_ro(gdp, 1);
Stefan Weinhuber65f8da42011-04-20 10:15:30 +020074 dasd_add_link_to_gendisk(gdp, base);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010075 gdp->queue = block->request_queue;
76 block->gdp = gdp;
77 set_capacity(block->gdp, 0);
Dan Williams0d52c7562016-06-15 19:44:20 -070078 device_add_disk(&base->cdev->dev, block->gdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 return 0;
80}
81
82/*
83 * Unregister and free gendisk structure for device.
84 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010085void dasd_gendisk_free(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010087 if (block->gdp) {
88 del_gendisk(block->gdp);
Stefan Haberland9eb25122010-02-26 22:37:46 +010089 block->gdp->private_data = NULL;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010090 put_disk(block->gdp);
91 block->gdp = NULL;
Horst Hummel8f617012006-08-30 14:33:33 +020092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
94
95/*
96 * Trigger a partition detection.
97 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010098int dasd_scan_partitions(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
100 struct block_device *bdev;
Jarod Wilsona05e5782015-05-06 12:26:28 +0800101 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100103 bdev = bdget_disk(block->gdp, 0);
Stefan Haberland6ebdf1c2014-11-24 15:04:09 +0100104 if (!bdev) {
105 DBF_DEV_EVENT(DBF_ERR, block->base, "%s",
106 "scan partitions error, bdget returned NULL");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 return -ENODEV;
Stefan Haberland6ebdf1c2014-11-24 15:04:09 +0100108 }
109
110 rc = blkdev_get(bdev, FMODE_READ, NULL);
111 if (rc < 0) {
112 DBF_DEV_EVENT(DBF_ERR, block->base,
113 "scan partitions error, blkdev_get returned %d",
114 rc);
115 return -ENODEV;
116 }
Ming Lei6029a062015-05-06 12:26:26 +0800117
118 rc = blkdev_reread_part(bdev);
Jarod Wilsona05e5782015-05-06 12:26:28 +0800119 if (rc)
Stefan Haberland6ebdf1c2014-11-24 15:04:09 +0100120 DBF_DEV_EVENT(DBF_ERR, block->base,
Jarod Wilsona05e5782015-05-06 12:26:28 +0800121 "scan partitions error, rc %d", rc);
Stefan Haberland6ebdf1c2014-11-24 15:04:09 +0100122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 /*
124 * Since the matching blkdev_put call to the blkdev_get in
125 * this function is not called before dasd_destroy_partitions
126 * the offline open_count limit needs to be increased from
127 * 0 to 1. This is done by setting device->bdev (see
128 * dasd_generic_set_offline). As long as the partition
129 * detection is running no offline should be allowed. That
130 * is why the assignment to device->bdev is done AFTER
131 * the BLKRRPART ioctl.
132 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100133 block->bdev = bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 return 0;
135}
136
137/*
138 * Remove all inodes in the system for a device, delete the
139 * partitions and make device unusable by setting its size to zero.
140 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100141void dasd_destroy_partitions(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
143 /* The two structs have 168/176 byte on 31/64 bit. */
144 struct blkpg_partition bpart;
145 struct blkpg_ioctl_arg barg;
146 struct block_device *bdev;
147
148 /*
149 * Get the bdev pointer from the device structure and clear
150 * device->bdev to lower the offline open_count limit again.
151 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100152 bdev = block->bdev;
153 block->bdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155 /*
156 * See fs/partition/check.c:delete_partition
157 * Can't call delete_partitions directly. Use ioctl.
158 * The ioctl also does locking and invalidation.
159 */
160 memset(&bpart, 0, sizeof(struct blkpg_partition));
161 memset(&barg, 0, sizeof(struct blkpg_ioctl_arg));
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100162 barg.data = (void __force __user *) &bpart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 barg.op = BLKPG_DEL_PARTITION;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100164 for (bpart.pno = block->gdp->minors - 1; bpart.pno > 0; bpart.pno--)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 ioctl_by_bdev(bdev, BLKPG, (unsigned long) &barg);
166
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100167 invalidate_partition(block->gdp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 /* Matching blkdev_put to the blkdev_get in dasd_scan_partitions. */
Al Viro9a1c3542008-02-22 20:40:24 -0500169 blkdev_put(bdev, FMODE_READ);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100170 set_capacity(block->gdp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100173int dasd_gendisk_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
175 int rc;
176
177 /* Register to static dasd major 94 */
178 rc = register_blkdev(DASD_MAJOR, "dasd");
179 if (rc != 0) {
Joe Perchesbaebc702016-03-03 20:49:57 -0800180 pr_warn("Registering the device driver with major number %d failed\n",
181 DASD_MAJOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 return rc;
183 }
184 return 0;
185}
186
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100187void dasd_gendisk_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
189 unregister_blkdev(DASD_MAJOR, "dasd");
190}