4 * IBM DASD partition table handling.
6 * Mostly taken from drivers/s390/block/dasd.c
8 * Copyright (c) 2005, Hannes Reinecke, SUSE Linux Products GmbH
9 * Copyright IBM Corporation, 2009
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
31 #include <sys/types.h>
33 #include <sys/ioctl.h>
34 #include <linux/hdreg.h>
38 #include <libdevmapper.h>
39 #include "devmapper.h"
41 #include "byteorder.h"
44 unsigned long long sectors512(unsigned long long sectors, int blocksize)
46 return sectors * (blocksize >> 9);
52 read_dasd_pt(int fd, struct slice all, struct slice *sp, int ns)
57 uint64_t offset, size, fmt_size;
58 dasd_information_t info;
59 struct hd_geometry geo;
61 volume_label_t vlabel;
62 unsigned char *data = NULL;
74 if (fstat(fd, &sbuf) == -1) {
78 devname = dm_mapname(major(sbuf.st_rdev), minor(sbuf.st_rdev));
80 if (devname != NULL) {
81 /* We were passed a handle to a dm device.
82 * Get the first target and operate on that instead.
84 if (!(dev = dm_get_first_dep(devname))) {
90 if ((unsigned int)major(dev) != 94) {
96 * Hard to believe, but there's no simple way to translate
97 * major/minor into an openable device file, so we have
98 * to create one for ourselves.
101 sprintf(pathname, "/dev/.kpartx-node-%u-%u",
102 (unsigned int)major(dev), (unsigned int)minor(dev));
103 if ((fd_dasd = open(pathname, O_RDONLY)) == -1) {
104 /* Devicenode does not exist. Try to create one */
105 if (mknod(pathname, 0600 | S_IFBLK, dev) == -1) {
106 /* Couldn't create a device node */
109 fd_dasd = open(pathname, O_RDONLY);
111 * The file will vanish when the last process (we)
112 * has ceased to access it.
117 /* Couldn't open the device */
124 if (ioctl(fd_dasd, BIODASDINFO, (unsigned long)&info) != 0) {
128 if (ioctl(fd_dasd, HDIO_GETGEO, (unsigned long)&geo) != 0) {
132 if (ioctl(fd_dasd, BLKGETSIZE64, &disksize) != 0)
136 if (ioctl(fd_dasd, BLKSSZGET, &blocksize) != 0)
139 if (blocksize < 512 || blocksize > 4096)
143 * Get volume label, extract name and type.
146 if (!(data = (unsigned char *)malloc(blocksize)))
150 if (lseek(fd_dasd, info.label_block * blocksize, SEEK_SET) == -1)
152 if (read(fd_dasd, data, blocksize) == -1) {
157 if ((!info.FBA_layout) && (!strcmp(info.type, "ECKD")))
158 memcpy (&vlabel, data, sizeof(vlabel));
161 memcpy (&vlabel.vollbl, data, sizeof(vlabel) - 4);
163 vtoc_ebcdic_dec(vlabel.vollbl, type, 4);
166 * Three different types: CMS1, VOL1 and LNX1/unlabeled
168 if (strncmp(type, "CMS1", 4) == 0) {
170 * VM style CMS1 labeled disk
172 unsigned int *label = (unsigned int *) &vlabel;
174 blocksize = label[4];
175 if (label[14] != 0) {
176 /* disk is reserved minidisk */
178 size = sectors512(label[8] - 1, blocksize);
180 offset = info.label_block + 1;
181 size = sectors512(label[8], blocksize);
183 sp[0].start = sectors512(offset, blocksize);
184 sp[0].size = size - sp[0].start;
186 } else if ((strncmp(type, "VOL1", 4) == 0) &&
187 (!info.FBA_layout) && (!strcmp(info.type, "ECKD"))) {
189 * New style VOL1 labeled disk
193 /* get block number and read then go through format1 labels */
194 blk = cchhb2blk(&vlabel.vtoc, &geo) + 1;
196 if (lseek(fd_dasd, blk * blocksize, SEEK_SET) == -1)
199 while (read(fd_dasd, data, blocksize) != -1) {
202 memcpy(&f1, data, sizeof(format1_label_t));
204 /* skip FMT4 / FMT5 / FMT7 labels */
205 if (EBCtoASC[f1.DS1FMTID] == '4'
206 || EBCtoASC[f1.DS1FMTID] == '5'
207 || EBCtoASC[f1.DS1FMTID] == '7'
208 || EBCtoASC[f1.DS1FMTID] == '9') {
213 /* only FMT1 and FMT8 valid at this point */
214 if (EBCtoASC[f1.DS1FMTID] != '1' &&
215 EBCtoASC[f1.DS1FMTID] != '8')
218 /* OK, we got valid partition data */
219 offset = cchh2blk(&f1.DS1EXT1.llimit, &geo);
220 size = cchh2blk(&f1.DS1EXT1.ulimit, &geo) -
221 offset + geo.sectors;
222 sp[counter].start = sectors512(offset, blocksize);
223 sp[counter].size = sectors512(size, blocksize);
230 * Old style LNX1 or unlabeled disk
232 if (strncmp(type, "LNX1", 4) == 0) {
233 if (vlabel.ldl_version == 0xf2) {
234 fmt_size = sectors512(vlabel.formatted_blocks,
236 } else if (!strcmp(info.type, "ECKD")) {
237 /* formated w/o large volume support */
238 fmt_size = geo.cylinders * geo.heads
239 * geo.sectors * (blocksize >> 9);
241 /* old label and no usable disk geometry
251 sp[0].start = sectors512(info.label_block + 1, blocksize);
252 sp[0].size = size - sp[0].start;
259 if (fd_dasd != -1 && fd_dasd != fd)