Linux-2.6.12-rc2
[linux-flexiantxendom0-natty.git] / drivers / s390 / block / dasd_genhd.c
1 /*
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  *
12  * $Revision: 1.48 $
13  */
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  */
30 int
31 dasd_gendisk_alloc(struct dasd_device *device)
32 {
33         struct gendisk *gdp;
34         int len;
35
36         /* Make sure the minor for this device exists. */
37         if (device->devindex >= DASD_PER_MAJOR)
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;
46         gdp->first_minor = device->devindex << DASD_PARTN_BITS;
47         gdp->fops = &dasd_device_operations;
48         gdp->driverfs_dev = &device->cdev->dev;
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");
58         if (device->devindex > 25) {
59                 if (device->devindex > 701) {
60                         if (device->devindex > 18277)
61                                 len += sprintf(gdp->disk_name + len, "%c",
62                                                'a'+(((device->devindex-18278)
63                                                      /17576)%26));
64                         len += sprintf(gdp->disk_name + len, "%c",
65                                        'a'+(((device->devindex-702)/676)%26));
66                 }
67                 len += sprintf(gdp->disk_name + len, "%c",
68                                'a'+(((device->devindex-26)/26)%26));
69         }
70         len += sprintf(gdp->disk_name + len, "%c", 'a'+(device->devindex%26));
71
72         sprintf(gdp->devfs_name, "dasd/%s", device->cdev->dev.bus_id);
73
74         if (test_bit(DASD_FLAG_RO, &device->flags))
75                 set_disk_ro(gdp, 1);
76         gdp->private_data = device;
77         gdp->queue = device->request_queue;
78         device->gdp = gdp;
79         set_capacity(device->gdp, 0);
80         add_disk(device->gdp);
81         return 0;
82 }
83
84 /*
85  * Unregister and free gendisk structure for device.
86  */
87 void
88 dasd_gendisk_free(struct dasd_device *device)
89 {
90         del_gendisk(device->gdp);
91         device->gdp->queue = 0;
92         put_disk(device->gdp);
93         device->gdp = 0;
94 }
95
96 /*
97  * Trigger a partition detection.
98  */
99 int
100 dasd_scan_partitions(struct dasd_device * device)
101 {
102         struct block_device *bdev;
103
104         /* Make the disk known. */
105         set_capacity(device->gdp, device->blocks << device->s2b_shift);
106         bdev = bdget_disk(device->gdp, 0);
107         if (!bdev || blkdev_get(bdev, FMODE_READ, 1) < 0)
108                 return -ENODEV;
109         /*
110          * See fs/partition/check.c:register_disk,rescan_partitions
111          * Can't call rescan_partitions directly. Use ioctl.
112          */
113         ioctl_by_bdev(bdev, BLKRRPART, 0);
114         /*
115          * Since the matching blkdev_put call to the blkdev_get in
116          * this function is not called before dasd_destroy_partitions
117          * the offline open_count limit needs to be increased from
118          * 0 to 1. This is done by setting device->bdev (see
119          * dasd_generic_set_offline). As long as the partition
120          * detection is running no offline should be allowed. That
121          * is why the assignment to device->bdev is done AFTER
122          * the BLKRRPART ioctl.
123          */
124         device->bdev = bdev;
125         return 0;
126 }
127
128 /*
129  * Remove all inodes in the system for a device, delete the
130  * partitions and make device unusable by setting its size to zero.
131  */
132 void
133 dasd_destroy_partitions(struct dasd_device * device)
134 {
135         /* The two structs have 168/176 byte on 31/64 bit. */
136         struct blkpg_partition bpart;
137         struct blkpg_ioctl_arg barg;
138         struct block_device *bdev;
139
140         /*
141          * Get the bdev pointer from the device structure and clear
142          * device->bdev to lower the offline open_count limit again.
143          */
144         bdev = device->bdev;
145         device->bdev = 0;
146
147         /*
148          * See fs/partition/check.c:delete_partition
149          * Can't call delete_partitions directly. Use ioctl.
150          * The ioctl also does locking and invalidation.
151          */
152         memset(&bpart, 0, sizeof(struct blkpg_partition));
153         memset(&barg, 0, sizeof(struct blkpg_ioctl_arg));
154         barg.data = &bpart;
155         barg.op = BLKPG_DEL_PARTITION;
156         for (bpart.pno = device->gdp->minors - 1; bpart.pno > 0; bpart.pno--)
157                 ioctl_by_bdev(bdev, BLKPG, (unsigned long) &barg);
158
159         invalidate_partition(device->gdp, 0);
160         /* Matching blkdev_put to the blkdev_get in dasd_scan_partitions. */
161         blkdev_put(bdev);
162         set_capacity(device->gdp, 0);
163 }
164
165 int
166 dasd_gendisk_init(void)
167 {
168         int rc;
169
170         /* Register to static dasd major 94 */
171         rc = register_blkdev(DASD_MAJOR, "dasd");
172         if (rc != 0) {
173                 MESSAGE(KERN_WARNING,
174                         "Couldn't register successfully to "
175                         "major no %d", DASD_MAJOR);
176                 return rc;
177         }
178         return 0;
179 }
180
181 void
182 dasd_gendisk_exit(void)
183 {
184         unregister_blkdev(DASD_MAJOR, "dasd");
185 }