blob: 1104acac780b06305a0b26aa1de06d289d2e1461 [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 */
Tejun Heo1493bf22010-05-15 20:09:30 +020061int ibm_partition(struct parsed_partitions *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
Tejun Heo1493bf22010-05-15 20:09:30 +020063 struct block_device *bdev = state->bdev;
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;
Peter Oberparleitercffab6b2010-07-19 09:22:35 +020077 sector_t labelsect;
Alexey Dobriyan9c867fb2010-08-10 18:03:14 -070078 char tmp[64];
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Suzuki K P57881dd2006-12-06 20:35:16 -080080 res = 0;
Martin K. Petersene1defc42009-05-22 17:17:49 -040081 blocksize = bdev_logical_block_size(bdev);
Horst Hummel90f00942006-03-07 21:55:39 -080082 if (blocksize <= 0)
Suzuki K P57881dd2006-12-06 20:35:16 -080083 goto out_exit;
Horst Hummel90f00942006-03-07 21:55:39 -080084 i_size = i_size_read(bdev->bd_inode);
85 if (i_size == 0)
Suzuki K P57881dd2006-12-06 20:35:16 -080086 goto out_exit;
Horst Hummel90f00942006-03-07 21:55:39 -080087
Stefan Haberlandbf1a95a2007-07-10 11:24:11 +020088 info = kmalloc(sizeof(dasd_information2_t), GFP_KERNEL);
89 if (info == NULL)
Suzuki K P57881dd2006-12-06 20:35:16 -080090 goto out_exit;
Stefan Haberlandbf1a95a2007-07-10 11:24:11 +020091 geo = kmalloc(sizeof(struct hd_geometry), GFP_KERNEL);
92 if (geo == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 goto out_nogeo;
Stefan Haberlandbf1a95a2007-07-10 11:24:11 +020094 label = kmalloc(sizeof(union label_t), GFP_KERNEL);
95 if (label == NULL)
Peter Oberparleiter56dc6a82006-01-06 00:19:09 -080096 goto out_nolab;
Horst Hummelef1597d2006-03-24 03:15:23 -080097
Stefan Haberlandbf1a95a2007-07-10 11:24:11 +020098 if (ioctl_by_bdev(bdev, BIODASDINFO2, (unsigned long)info) != 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 ioctl_by_bdev(bdev, HDIO_GETGEO, (unsigned long)geo) != 0)
Suzuki K P57881dd2006-12-06 20:35:16 -0800100 goto out_freeall;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102 /*
Peter Oberparleitercffab6b2010-07-19 09:22:35 +0200103 * Special case for FBA disks: label sector does not depend on
104 * blocksize.
105 */
106 if ((info->cu_type == 0x6310 && info->dev_type == 0x9336) ||
107 (info->cu_type == 0x3880 && info->dev_type == 0x3370))
108 labelsect = info->label_block;
109 else
110 labelsect = info->label_block * (blocksize >> 9);
111
112 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 * Get volume label, extract name and type.
114 */
Peter Oberparleitercffab6b2010-07-19 09:22:35 +0200115 data = read_part_sector(state, labelsect, &sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 if (data == NULL)
117 goto out_readerr;
118
Peter Oberparleiter56dc6a82006-01-06 00:19:09 -0800119 memcpy(label, data, sizeof(union label_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 put_dev_sector(sect);
121
Stefan Weinhuberb44b0ab32009-03-26 15:23:47 +0100122 if ((!info->FBA_layout) && (!strcmp(info->type, "ECKD"))) {
123 strncpy(type, label->vol.vollbl, 4);
124 strncpy(name, label->vol.volid, 6);
125 } else {
126 strncpy(type, label->lnx.vollbl, 4);
127 strncpy(name, label->lnx.volid, 6);
128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 EBCASC(type, 4);
130 EBCASC(name, 6);
131
Suzuki K P57881dd2006-12-06 20:35:16 -0800132 res = 1;
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 /*
Stefan Haberlandbf1a95a2007-07-10 11:24:11 +0200135 * Three different formats: LDL, CDL and unformated disk
136 *
137 * identified by info->format
138 *
139 * unformated disks we do not have to care about
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 */
Stefan Haberlandbf1a95a2007-07-10 11:24:11 +0200141 if (info->format == DASD_FORMAT_LDL) {
142 if (strncmp(type, "CMS1", 4) == 0) {
143 /*
144 * VM style CMS1 labeled disk
145 */
Stefan Weinhuberb44b0ab32009-03-26 15:23:47 +0100146 blocksize = label->cms.block_size;
Stefan Haberlandbf1a95a2007-07-10 11:24:11 +0200147 if (label->cms.disk_offset != 0) {
Alexey Dobriyan9c867fb2010-08-10 18:03:14 -0700148 snprintf(tmp, sizeof(tmp), "CMS1/%8s(MDSK):", name);
149 strlcat(state->pp_buf, tmp, PAGE_SIZE);
Stefan Haberlandbf1a95a2007-07-10 11:24:11 +0200150 /* disk is reserved minidisk */
Stefan Haberlandbf1a95a2007-07-10 11:24:11 +0200151 offset = label->cms.disk_offset;
152 size = (label->cms.block_count - 1)
153 * (blocksize >> 9);
154 } else {
Alexey Dobriyan9c867fb2010-08-10 18:03:14 -0700155 snprintf(tmp, sizeof(tmp), "CMS1/%8s:", name);
156 strlcat(state->pp_buf, tmp, PAGE_SIZE);
Stefan Haberlandbf1a95a2007-07-10 11:24:11 +0200157 offset = (info->label_block + 1);
Stefan Weinhuberb44b0ab32009-03-26 15:23:47 +0100158 size = label->cms.block_count
159 * (blocksize >> 9);
Stefan Haberlandbf1a95a2007-07-10 11:24:11 +0200160 }
Stefan Weinhuberb44b0ab32009-03-26 15:23:47 +0100161 put_partition(state, 1, offset*(blocksize >> 9),
Stefan Haberlandbf1a95a2007-07-10 11:24:11 +0200162 size-offset*(blocksize >> 9));
Stefan Weinhuberb44b0ab32009-03-26 15:23:47 +0100163 } else {
164 if (strncmp(type, "LNX1", 4) == 0) {
Alexey Dobriyan9c867fb2010-08-10 18:03:14 -0700165 snprintf(tmp, sizeof(tmp), "LNX1/%8s:", name);
166 strlcat(state->pp_buf, tmp, PAGE_SIZE);
Stefan Weinhuberb44b0ab32009-03-26 15:23:47 +0100167 if (label->lnx.ldl_version == 0xf2) {
168 fmt_size = label->lnx.formatted_blocks
169 * (blocksize >> 9);
170 } else if (!strcmp(info->type, "ECKD")) {
171 /* formated w/o large volume support */
172 fmt_size = geo->cylinders * geo->heads
173 * geo->sectors * (blocksize >> 9);
174 } else {
175 /* old label and no usable disk geometry
176 * (e.g. DIAG) */
177 fmt_size = i_size >> 9;
178 }
179 size = i_size >> 9;
180 if (fmt_size < size)
181 size = fmt_size;
182 offset = (info->label_block + 1);
183 } else {
184 /* unlabeled disk */
Heiko Carstens2041f652010-08-13 10:06:43 +0200185 strlcat(state->pp_buf, "(nonl)", PAGE_SIZE);
Stefan Weinhuberb44b0ab32009-03-26 15:23:47 +0100186 size = i_size >> 9;
187 offset = (info->label_block + 1);
188 }
189 put_partition(state, 1, offset*(blocksize >> 9),
190 size-offset*(blocksize >> 9));
191 }
Stefan Haberlandbf1a95a2007-07-10 11:24:11 +0200192 } else if (info->format == DASD_FORMAT_CDL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 /*
Stefan Haberlandbf1a95a2007-07-10 11:24:11 +0200194 * New style CDL formatted disk
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 */
Stefan Weinhuberb44b0ab32009-03-26 15:23:47 +0100196 sector_t blk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 int counter;
198
Stefan Haberlandbf1a95a2007-07-10 11:24:11 +0200199 /*
200 * check if VOL1 label is available
201 * if not, something is wrong, skipping partition detection
202 */
203 if (strncmp(type, "VOL1", 4) == 0) {
Alexey Dobriyan9c867fb2010-08-10 18:03:14 -0700204 snprintf(tmp, sizeof(tmp), "VOL1/%8s:", name);
205 strlcat(state->pp_buf, tmp, PAGE_SIZE);
Stefan Haberlandbf1a95a2007-07-10 11:24:11 +0200206 /*
207 * get block number and read then go through format1
208 * labels
209 */
210 blk = cchhb2blk(&label->vol.vtoc, geo) + 1;
211 counter = 0;
Tejun Heo1493bf22010-05-15 20:09:30 +0200212 data = read_part_sector(state, blk * (blocksize/512),
213 &sect);
Stefan Haberlandbf1a95a2007-07-10 11:24:11 +0200214 while (data != NULL) {
215 struct vtoc_format1_label f1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Stefan Haberlandbf1a95a2007-07-10 11:24:11 +0200217 memcpy(&f1, data,
218 sizeof(struct vtoc_format1_label));
219 put_dev_sector(sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Stefan Haberlandbf1a95a2007-07-10 11:24:11 +0200221 /* skip FMT4 / FMT5 / FMT7 labels */
222 if (f1.DS1FMTID == _ascebc['4']
223 || f1.DS1FMTID == _ascebc['5']
Stefan Weinhuberb44b0ab32009-03-26 15:23:47 +0100224 || f1.DS1FMTID == _ascebc['7']
225 || f1.DS1FMTID == _ascebc['9']) {
Stefan Haberlandbf1a95a2007-07-10 11:24:11 +0200226 blk++;
Tejun Heo1493bf22010-05-15 20:09:30 +0200227 data = read_part_sector(state,
228 blk * (blocksize/512), &sect);
Stefan Haberlandbf1a95a2007-07-10 11:24:11 +0200229 continue;
230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Stefan Weinhuberb44b0ab32009-03-26 15:23:47 +0100232 /* only FMT1 and 8 labels valid at this point */
233 if (f1.DS1FMTID != _ascebc['1'] &&
234 f1.DS1FMTID != _ascebc['8'])
Stefan Haberlandbf1a95a2007-07-10 11:24:11 +0200235 break;
236
237 /* OK, we got valid partition data */
238 offset = cchh2blk(&f1.DS1EXT1.llimit, geo);
239 size = cchh2blk(&f1.DS1EXT1.ulimit, geo) -
240 offset + geo->sectors;
241 if (counter >= state->limit)
242 break;
243 put_partition(state, counter + 1,
244 offset * (blocksize >> 9),
245 size * (blocksize >> 9));
246 counter++;
247 blk++;
Tejun Heo1493bf22010-05-15 20:09:30 +0200248 data = read_part_sector(state,
249 blk * (blocksize/512), &sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 }
251
Stefan Haberlandbf1a95a2007-07-10 11:24:11 +0200252 if (!data)
253 /* Are we not supposed to report this ? */
254 goto out_readerr;
255 } else
Stefan Haberland505e5ec2012-05-16 12:30:52 +0200256 printk(KERN_INFO "Expected Label VOL1 not "
Stefan Haberlandbf1a95a2007-07-10 11:24:11 +0200257 "found, treating as CDL formated Disk");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 }
260
Alexey Dobriyan9c867fb2010-08-10 18:03:14 -0700261 strlcat(state->pp_buf, "\n", PAGE_SIZE);
Suzuki K P57881dd2006-12-06 20:35:16 -0800262 goto out_freeall;
263
Horst Hummelef1597d2006-03-24 03:15:23 -0800264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265out_readerr:
Suzuki K P57881dd2006-12-06 20:35:16 -0800266 res = -1;
267out_freeall:
Peter Oberparleiter56dc6a82006-01-06 00:19:09 -0800268 kfree(label);
269out_nolab:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 kfree(geo);
271out_nogeo:
272 kfree(info);
Suzuki K P57881dd2006-12-06 20:35:16 -0800273out_exit:
274 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}