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