blob: 94f92a1247f2039b19ae29581b7e7c63e155f418 [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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 */
13
Stefan Haberlandfc19f382009-03-26 15:23:49 +010014#define KMSG_COMPONENT "dasd"
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#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 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010030int dasd_gendisk_alloc(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031{
32 struct gendisk *gdp;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010033 struct dasd_device *base;
Horst Hummelc6eb7b72005-09-03 15:57:58 -070034 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36 /* Make sure the minor for this device exists. */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010037 base = block->base;
38 if (base->devindex >= DASD_PER_MAJOR)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 return -EBUSY;
40
41 gdp = alloc_disk(1 << DASD_PARTN_BITS);
42 if (!gdp)
43 return -ENOMEM;
44
45 /* Initialize gendisk structure. */
46 gdp->major = DASD_MAJOR;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010047 gdp->first_minor = base->devindex << DASD_PARTN_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 gdp->fops = &dasd_device_operations;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010049 gdp->driverfs_dev = &base->cdev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51 /*
52 * Set device name.
53 * dasda - dasdz : 26 devices
54 * dasdaa - dasdzz : 676 devices, added up = 702
55 * dasdaaa - dasdzzz : 17576 devices, added up = 18278
56 * dasdaaaa - dasdzzzz : 456976 devices, added up = 475252
57 */
58 len = sprintf(gdp->disk_name, "dasd");
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010059 if (base->devindex > 25) {
60 if (base->devindex > 701) {
61 if (base->devindex > 18277)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 len += sprintf(gdp->disk_name + len, "%c",
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010063 'a'+(((base->devindex-18278)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 /17576)%26));
65 len += sprintf(gdp->disk_name + len, "%c",
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010066 'a'+(((base->devindex-702)/676)%26));
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 }
68 len += sprintf(gdp->disk_name + len, "%c",
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010069 'a'+(((base->devindex-26)/26)%26));
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010071 len += sprintf(gdp->disk_name + len, "%c", 'a'+(base->devindex%26));
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010073 if (block->base->features & DASD_FEATURE_READONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 set_disk_ro(gdp, 1);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010075 gdp->private_data = block;
76 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);
90 block->gdp->queue = NULL;
Stefan Haberland9eb25122010-02-26 22:37:46 +010091 block->gdp->private_data = NULL;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010092 put_disk(block->gdp);
93 block->gdp = NULL;
Horst Hummel8f617012006-08-30 14:33:33 +020094 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
96
97/*
98 * Trigger a partition detection.
99 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100100int dasd_scan_partitions(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
102 struct block_device *bdev;
103
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100104 bdev = bdget_disk(block->gdp, 0);
Al Viro572c4892007-10-08 13:24:05 -0400105 if (!bdev || blkdev_get(bdev, FMODE_READ) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 return -ENODEV;
107 /*
108 * See fs/partition/check.c:register_disk,rescan_partitions
109 * Can't call rescan_partitions directly. Use ioctl.
110 */
111 ioctl_by_bdev(bdev, BLKRRPART, 0);
112 /*
113 * Since the matching blkdev_put call to the blkdev_get in
114 * this function is not called before dasd_destroy_partitions
115 * the offline open_count limit needs to be increased from
116 * 0 to 1. This is done by setting device->bdev (see
117 * dasd_generic_set_offline). As long as the partition
118 * detection is running no offline should be allowed. That
119 * is why the assignment to device->bdev is done AFTER
120 * the BLKRRPART ioctl.
121 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100122 block->bdev = bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 return 0;
124}
125
126/*
127 * Remove all inodes in the system for a device, delete the
128 * partitions and make device unusable by setting its size to zero.
129 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100130void dasd_destroy_partitions(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
132 /* The two structs have 168/176 byte on 31/64 bit. */
133 struct blkpg_partition bpart;
134 struct blkpg_ioctl_arg barg;
135 struct block_device *bdev;
136
137 /*
138 * Get the bdev pointer from the device structure and clear
139 * device->bdev to lower the offline open_count limit again.
140 */
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100141 bdev = block->bdev;
142 block->bdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 /*
145 * See fs/partition/check.c:delete_partition
146 * Can't call delete_partitions directly. Use ioctl.
147 * The ioctl also does locking and invalidation.
148 */
149 memset(&bpart, 0, sizeof(struct blkpg_partition));
150 memset(&barg, 0, sizeof(struct blkpg_ioctl_arg));
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100151 barg.data = (void __force __user *) &bpart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 barg.op = BLKPG_DEL_PARTITION;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100153 for (bpart.pno = block->gdp->minors - 1; bpart.pno > 0; bpart.pno--)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 ioctl_by_bdev(bdev, BLKPG, (unsigned long) &barg);
155
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100156 invalidate_partition(block->gdp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 /* Matching blkdev_put to the blkdev_get in dasd_scan_partitions. */
Al Viro9a1c3542008-02-22 20:40:24 -0500158 blkdev_put(bdev, FMODE_READ);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100159 set_capacity(block->gdp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}
161
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100162int dasd_gendisk_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
164 int rc;
165
166 /* Register to static dasd major 94 */
167 rc = register_blkdev(DASD_MAJOR, "dasd");
168 if (rc != 0) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100169 pr_warning("Registering the device driver with major number "
170 "%d failed\n", DASD_MAJOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 return rc;
172 }
173 return 0;
174}
175
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100176void dasd_gendisk_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
178 unregister_blkdev(DASD_MAJOR, "dasd");
179}