]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/s390/block/dasd_genhd.c
block: convert to device_add_disk()
[mirror_ubuntu-bionic-kernel.git] / drivers / s390 / block / dasd_genhd.c
CommitLineData
1da177e4 1/*
1da177e4
LT
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>
a53c8fab 7 * Copyright IBM Corp. 1999, 2001
1da177e4
LT
8 *
9 * gendisk related functions for the dasd driver.
10 *
1da177e4
LT
11 */
12
fc19f381
SH
13#define KMSG_COMPONENT "dasd"
14
1da177e4
LT
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 */
8e09f215 29int dasd_gendisk_alloc(struct dasd_block *block)
1da177e4
LT
30{
31 struct gendisk *gdp;
8e09f215 32 struct dasd_device *base;
c6eb7b77 33 int len;
1da177e4
LT
34
35 /* Make sure the minor for this device exists. */
8e09f215
SW
36 base = block->base;
37 if (base->devindex >= DASD_PER_MAJOR)
1da177e4
LT
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;
8e09f215 46 gdp->first_minor = base->devindex << DASD_PARTN_BITS;
1da177e4 47 gdp->fops = &dasd_device_operations;
1da177e4
LT
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");
8e09f215
SW
57 if (base->devindex > 25) {
58 if (base->devindex > 701) {
59 if (base->devindex > 18277)
1da177e4 60 len += sprintf(gdp->disk_name + len, "%c",
8e09f215 61 'a'+(((base->devindex-18278)
1da177e4
LT
62 /17576)%26));
63 len += sprintf(gdp->disk_name + len, "%c",
8e09f215 64 'a'+(((base->devindex-702)/676)%26));
1da177e4
LT
65 }
66 len += sprintf(gdp->disk_name + len, "%c",
8e09f215 67 'a'+(((base->devindex-26)/26)%26));
1da177e4 68 }
8e09f215 69 len += sprintf(gdp->disk_name + len, "%c", 'a'+(base->devindex%26));
1da177e4 70
33b62a30
SW
71 if (base->features & DASD_FEATURE_READONLY ||
72 test_bit(DASD_FLAG_DEVICE_RO, &base->flags))
1da177e4 73 set_disk_ro(gdp, 1);
65f8da47 74 dasd_add_link_to_gendisk(gdp, base);
8e09f215
SW
75 gdp->queue = block->request_queue;
76 block->gdp = gdp;
77 set_capacity(block->gdp, 0);
0d52c756 78 device_add_disk(&base->cdev->dev, block->gdp);
1da177e4
LT
79 return 0;
80}
81
82/*
83 * Unregister and free gendisk structure for device.
84 */
8e09f215 85void dasd_gendisk_free(struct dasd_block *block)
1da177e4 86{
8e09f215
SW
87 if (block->gdp) {
88 del_gendisk(block->gdp);
9eb25122 89 block->gdp->private_data = NULL;
8e09f215
SW
90 put_disk(block->gdp);
91 block->gdp = NULL;
8f61701b 92 }
1da177e4
LT
93}
94
95/*
96 * Trigger a partition detection.
97 */
8e09f215 98int dasd_scan_partitions(struct dasd_block *block)
1da177e4
LT
99{
100 struct block_device *bdev;
a05e5780 101 int rc;
1da177e4 102
8e09f215 103 bdev = bdget_disk(block->gdp, 0);
6ebdf1c7
SH
104 if (!bdev) {
105 DBF_DEV_EVENT(DBF_ERR, block->base, "%s",
106 "scan partitions error, bdget returned NULL");
1da177e4 107 return -ENODEV;
6ebdf1c7
SH
108 }
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 }
6029a06c
ML
117
118 rc = blkdev_reread_part(bdev);
a05e5780 119 if (rc)
6ebdf1c7 120 DBF_DEV_EVENT(DBF_ERR, block->base,
a05e5780 121 "scan partitions error, rc %d", rc);
6ebdf1c7 122
1da177e4
LT
123 /*
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 */
8e09f215 133 block->bdev = bdev;
1da177e4
LT
134 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 */
8e09f215 141void dasd_destroy_partitions(struct dasd_block *block)
1da177e4
LT
142{
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 */
8e09f215
SW
152 bdev = block->bdev;
153 block->bdev = NULL;
1da177e4
LT
154
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));
2b67fc46 162 barg.data = (void __force __user *) &bpart;
1da177e4 163 barg.op = BLKPG_DEL_PARTITION;
8e09f215 164 for (bpart.pno = block->gdp->minors - 1; bpart.pno > 0; bpart.pno--)
1da177e4
LT
165 ioctl_by_bdev(bdev, BLKPG, (unsigned long) &barg);
166
8e09f215 167 invalidate_partition(block->gdp, 0);
1da177e4 168 /* Matching blkdev_put to the blkdev_get in dasd_scan_partitions. */
9a1c3542 169 blkdev_put(bdev, FMODE_READ);
8e09f215 170 set_capacity(block->gdp, 0);
1da177e4
LT
171}
172
8e09f215 173int dasd_gendisk_init(void)
1da177e4
LT
174{
175 int rc;
176
177 /* Register to static dasd major 94 */
178 rc = register_blkdev(DASD_MAJOR, "dasd");
179 if (rc != 0) {
baebc70a
JP
180 pr_warn("Registering the device driver with major number %d failed\n",
181 DASD_MAJOR);
1da177e4
LT
182 return rc;
183 }
184 return 0;
185}
186
8e09f215 187void dasd_gendisk_exit(void)
1da177e4
LT
188{
189 unregister_blkdev(DASD_MAJOR, "dasd");
190}