blob: fc71aab084603749abc8138ee860c2f17faa05fa [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Horst Hummelef1597d2006-03-24 03:15:23 -08002 * File...........: linux/fs/partitions/ibm.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
4 * Volker Sameske <sameske@de.ibm.com>
5 * Bugreports.to..: <Linux390@de.ibm.com>
6 * (C) IBM Corporation, IBM Deutschland Entwicklung GmbH, 1999,2000
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
8
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/buffer_head.h>
10#include <linux/hdreg.h>
11#include <linux/slab.h>
12#include <asm/dasd.h>
13#include <asm/ebcdic.h>
14#include <asm/uaccess.h>
15#include <asm/vtoc.h>
16
17#include "check.h"
18#include "ibm.h"
19
20/*
Horst Hummelef1597d2006-03-24 03:15:23 -080021 * compute the block number from a
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 * cyl-cyl-head-head structure
23 */
Stefan Weinhuberb44b0ab32009-03-26 15:23:47 +010024static sector_t
Peter Oberparleiter4cd5b9f2005-11-07 00:59:09 -080025cchh2blk (struct vtoc_cchh *ptr, struct hd_geometry *geo) {
Stefan Weinhuberb44b0ab32009-03-26 15:23:47 +010026
27 sector_t cyl;
28 __u16 head;
29
30 /*decode cylinder and heads for large volumes */
31 cyl = ptr->hh & 0xFFF0;
32 cyl <<= 12;
33 cyl |= ptr->cc;
34 head = ptr->hh & 0x000F;
35 return cyl * geo->heads * geo->sectors +
36 head * geo->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037}
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039/*
Horst Hummelef1597d2006-03-24 03:15:23 -080040 * compute the block number from a
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 * cyl-cyl-head-head-block structure
42 */
Stefan Weinhuberb44b0ab32009-03-26 15:23:47 +010043static sector_t
Peter Oberparleiter4cd5b9f2005-11-07 00:59:09 -080044cchhb2blk (struct vtoc_cchhb *ptr, struct hd_geometry *geo) {
Stefan Weinhuberb44b0ab32009-03-26 15:23:47 +010045
46 sector_t cyl;
47 __u16 head;
48
49 /*decode cylinder and heads for large volumes */
50 cyl = ptr->hh & 0xFFF0;
51 cyl <<= 12;
52 cyl |= ptr->cc;
53 head = ptr->hh & 0x000F;
54 return cyl * geo->heads * geo->sectors +
55 head * geo->sectors +
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 ptr->b;
57}
58
59/*
60 */
Horst Hummelef1597d2006-03-24 03:15:23 -080061int
Linus Torvalds1da177e2005-04-16 15:20:36 -070062ibm_partition(struct parsed_partitions *state, struct block_device *bdev)
63{
Stefan Weinhuberb44b0ab32009-03-26 15:23:47 +010064 int blocksize, res;
65 loff_t i_size, offset, size, fmt_size;
Stefan Haberlandbf1a95a2007-07-10 11:24:11 +020066 dasd_information2_t *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 struct hd_geometry *geo;
68 char type[5] = {0,};
69 char name[7] = {0,};
Peter Oberparleiter56dc6a82006-01-06 00:19:09 -080070 union label_t {
Stefan Weinhuberb44b0ab32009-03-26 15:23:47 +010071 struct vtoc_volume_label_cdl vol;
72 struct vtoc_volume_label_ldl lnx;
Peter Oberparleiter56dc6a82006-01-06 00:19:09 -080073 struct vtoc_cms_label cms;
74 } *label;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 unsigned char *data;
76 Sector sect;
77
Suzuki K P57881dd2006-12-06 20:35:16 -080078 res = 0;
Martin K. Petersene1defc42009-05-22 17:17:49 -040079 blocksize = bdev_logical_block_size(bdev);
Horst Hummel90f00942006-03-07 21:55:39 -080080 if (blocksize <= 0)
Suzuki K P57881dd2006-12-06 20:35:16 -080081 goto out_exit;
Horst Hummel90f00942006-03-07 21:55:39 -080082 i_size = i_size_read(bdev->bd_inode);
83 if (i_size == 0)
Suzuki K P57881dd2006-12-06 20:35:16 -080084 goto out_exit;
Horst Hummel90f00942006-03-07 21:55:39 -080085
Stefan Haberlandbf1a95a2007-07-10 11:24:11 +020086 info = kmalloc(sizeof(dasd_information2_t), GFP_KERNEL);
87 if (info == NULL)
Suzuki K P57881dd2006-12-06 20:35:16 -080088 goto out_exit;
Stefan Haberlandbf1a95a2007-07-10 11:24:11 +020089 geo = kmalloc(sizeof(struct hd_geometry), GFP_KERNEL);
90 if (geo == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 goto out_nogeo;
Stefan Haberlandbf1a95a2007-07-10 11:24:11 +020092 label = kmalloc(sizeof(union label_t), GFP_KERNEL);
93 if (label == NULL)
Peter Oberparleiter56dc6a82006-01-06 00:19:09 -080094 goto out_nolab;
Horst Hummelef1597d2006-03-24 03:15:23 -080095
Stefan Haberlandbf1a95a2007-07-10 11:24:11 +020096 if (ioctl_by_bdev(bdev, BIODASDINFO2, (unsigned long)info) != 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 ioctl_by_bdev(bdev, HDIO_GETGEO, (unsigned long)geo) != 0)
Suzuki K P57881dd2006-12-06 20:35:16 -080098 goto out_freeall;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100 /*
101 * Get volume label, extract name and type.
102 */
103 data = read_dev_sector(bdev, info->label_block*(blocksize/512), &sect);
104 if (data == NULL)
105 goto out_readerr;
106
Peter Oberparleiter56dc6a82006-01-06 00:19:09 -0800107 memcpy(label, data, sizeof(union label_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 put_dev_sector(sect);
109
Stefan Weinhuberb44b0ab32009-03-26 15:23:47 +0100110 if ((!info->FBA_layout) && (!strcmp(info->type, "ECKD"))) {
111 strncpy(type, label->vol.vollbl, 4);
112 strncpy(name, label->vol.volid, 6);
113 } else {
114 strncpy(type, label->lnx.vollbl, 4);
115 strncpy(name, label->lnx.volid, 6);
116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 EBCASC(type, 4);
118 EBCASC(name, 6);
119
Suzuki K P57881dd2006-12-06 20:35:16 -0800120 res = 1;
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 /*
Stefan Haberlandbf1a95a2007-07-10 11:24:11 +0200123 * Three different formats: LDL, CDL and unformated disk
124 *
125 * identified by info->format
126 *
127 * unformated disks we do not have to care about
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 */
Stefan Haberlandbf1a95a2007-07-10 11:24:11 +0200129 if (info->format == DASD_FORMAT_LDL) {
130 if (strncmp(type, "CMS1", 4) == 0) {
131 /*
132 * VM style CMS1 labeled disk
133 */
Stefan Weinhuberb44b0ab32009-03-26 15:23:47 +0100134 blocksize = label->cms.block_size;
Stefan Haberlandbf1a95a2007-07-10 11:24:11 +0200135 if (label->cms.disk_offset != 0) {
136 printk("CMS1/%8s(MDSK):", name);
137 /* disk is reserved minidisk */
Stefan Haberlandbf1a95a2007-07-10 11:24:11 +0200138 offset = label->cms.disk_offset;
139 size = (label->cms.block_count - 1)
140 * (blocksize >> 9);
141 } else {
142 printk("CMS1/%8s:", name);
143 offset = (info->label_block + 1);
Stefan Weinhuberb44b0ab32009-03-26 15:23:47 +0100144 size = label->cms.block_count
145 * (blocksize >> 9);
Stefan Haberlandbf1a95a2007-07-10 11:24:11 +0200146 }
Stefan Weinhuberb44b0ab32009-03-26 15:23:47 +0100147 put_partition(state, 1, offset*(blocksize >> 9),
Stefan Haberlandbf1a95a2007-07-10 11:24:11 +0200148 size-offset*(blocksize >> 9));
Stefan Weinhuberb44b0ab32009-03-26 15:23:47 +0100149 } else {
150 if (strncmp(type, "LNX1", 4) == 0) {
151 printk("LNX1/%8s:", name);
152 if (label->lnx.ldl_version == 0xf2) {
153 fmt_size = label->lnx.formatted_blocks
154 * (blocksize >> 9);
155 } else if (!strcmp(info->type, "ECKD")) {
156 /* formated w/o large volume support */
157 fmt_size = geo->cylinders * geo->heads
158 * geo->sectors * (blocksize >> 9);
159 } else {
160 /* old label and no usable disk geometry
161 * (e.g. DIAG) */
162 fmt_size = i_size >> 9;
163 }
164 size = i_size >> 9;
165 if (fmt_size < size)
166 size = fmt_size;
167 offset = (info->label_block + 1);
168 } else {
169 /* unlabeled disk */
170 printk("(nonl)");
171 size = i_size >> 9;
172 offset = (info->label_block + 1);
173 }
174 put_partition(state, 1, offset*(blocksize >> 9),
175 size-offset*(blocksize >> 9));
176 }
Stefan Haberlandbf1a95a2007-07-10 11:24:11 +0200177 } else if (info->format == DASD_FORMAT_CDL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 /*
Stefan Haberlandbf1a95a2007-07-10 11:24:11 +0200179 * New style CDL formatted disk
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 */
Stefan Weinhuberb44b0ab32009-03-26 15:23:47 +0100181 sector_t blk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 int counter;
183
Stefan Haberlandbf1a95a2007-07-10 11:24:11 +0200184 /*
185 * check if VOL1 label is available
186 * if not, something is wrong, skipping partition detection
187 */
188 if (strncmp(type, "VOL1", 4) == 0) {
189 printk("VOL1/%8s:", name);
190 /*
191 * get block number and read then go through format1
192 * labels
193 */
194 blk = cchhb2blk(&label->vol.vtoc, geo) + 1;
195 counter = 0;
196 data = read_dev_sector(bdev, blk * (blocksize/512),
197 &sect);
198 while (data != NULL) {
199 struct vtoc_format1_label f1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Stefan Haberlandbf1a95a2007-07-10 11:24:11 +0200201 memcpy(&f1, data,
202 sizeof(struct vtoc_format1_label));
203 put_dev_sector(sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Stefan Haberlandbf1a95a2007-07-10 11:24:11 +0200205 /* skip FMT4 / FMT5 / FMT7 labels */
206 if (f1.DS1FMTID == _ascebc['4']
207 || f1.DS1FMTID == _ascebc['5']
Stefan Weinhuberb44b0ab32009-03-26 15:23:47 +0100208 || f1.DS1FMTID == _ascebc['7']
209 || f1.DS1FMTID == _ascebc['9']) {
Stefan Haberlandbf1a95a2007-07-10 11:24:11 +0200210 blk++;
211 data = read_dev_sector(bdev, blk *
212 (blocksize/512),
213 &sect);
214 continue;
215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Stefan Weinhuberb44b0ab32009-03-26 15:23:47 +0100217 /* only FMT1 and 8 labels valid at this point */
218 if (f1.DS1FMTID != _ascebc['1'] &&
219 f1.DS1FMTID != _ascebc['8'])
Stefan Haberlandbf1a95a2007-07-10 11:24:11 +0200220 break;
221
222 /* OK, we got valid partition data */
223 offset = cchh2blk(&f1.DS1EXT1.llimit, geo);
224 size = cchh2blk(&f1.DS1EXT1.ulimit, geo) -
225 offset + geo->sectors;
226 if (counter >= state->limit)
227 break;
228 put_partition(state, counter + 1,
229 offset * (blocksize >> 9),
230 size * (blocksize >> 9));
231 counter++;
232 blk++;
233 data = read_dev_sector(bdev,
234 blk * (blocksize/512),
235 &sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 }
237
Stefan Haberlandbf1a95a2007-07-10 11:24:11 +0200238 if (!data)
239 /* Are we not supposed to report this ? */
240 goto out_readerr;
241 } else
242 printk(KERN_WARNING "Warning, expected Label VOL1 not "
243 "found, treating as CDL formated Disk");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 }
246
247 printk("\n");
Suzuki K P57881dd2006-12-06 20:35:16 -0800248 goto out_freeall;
249
Horst Hummelef1597d2006-03-24 03:15:23 -0800250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251out_readerr:
Suzuki K P57881dd2006-12-06 20:35:16 -0800252 res = -1;
253out_freeall:
Peter Oberparleiter56dc6a82006-01-06 00:19:09 -0800254 kfree(label);
255out_nolab:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 kfree(geo);
257out_nogeo:
258 kfree(info);
Suzuki K P57881dd2006-12-06 20:35:16 -0800259out_exit:
260 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}