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
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
30 #include <sys/types.h>
32 #include <sys/ioctl.h>
33 #include <linux/hdreg.h>
37 #include <libdevmapper.h>
38 #include "devmapper.h"
40 #include "byteorder.h"
43 unsigned long sectors512(unsigned long sectors, int blocksize)
45 return sectors * (blocksize >> 9);
51 read_dasd_pt(int fd, struct slice all, struct slice *sp, int ns)
56 unsigned long offset, size;
57 dasd_information_t info;
58 struct hd_geometry geo;
61 unsigned char *label_raw;
62 volume_label_t vlabel;
63 unsigned char *data = NULL;
75 if (fstat(fd, &sbuf) == -1) {
79 devname = dm_mapname(major(sbuf.st_rdev), minor(sbuf.st_rdev));
81 if (devname != NULL) {
82 /* We were passed a handle to a dm device.
83 * Get the first target and operate on that instead.
85 if (!(dev = dm_get_first_dep(devname))) {
91 if ((unsigned int)major(dev) != 94) {
97 * Hard to believe, but there's no simple way to translate
98 * major/minor into an openable device file, so we have
99 * to create one for ourselves.
102 sprintf(pathname, "/dev/.kpartx-node-%u-%u",
103 (unsigned int)major(dev), (unsigned int)minor(dev));
104 if ((fd_dasd = open(pathname, O_RDONLY)) == -1) {
105 /* Devicenode does not exist. Try to create one */
106 if (mknod(pathname, 0600 | S_IFBLK, dev) == -1) {
107 /* Couldn't create a device node */
110 fd_dasd = open(pathname, O_RDONLY);
112 * The file will vanish when the last process (we)
113 * has ceased to access it.
118 /* Couldn't open the device */
125 if (ioctl(fd_dasd, BIODASDINFO, (unsigned long)&info) != 0) {
129 if (ioctl(fd_dasd, HDIO_GETGEO, (unsigned long)&geo) != 0) {
133 if (ioctl(fd_dasd, BLKGETSIZE, &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) {
156 vtoc_ebcdic_dec(data, type, 4);
158 if ((!info.FBA_layout) && (!strcmp(info.type, "ECKD")))
159 label_raw = &data[8];
161 label_raw = &data[4];
164 vtoc_ebcdic_dec(label_raw, name, 6);
166 memcpy (&vlabel, data, sizeof(volume_label_t));
169 * Three different types: CMS1, VOL1 and LNX1/unlabeled
171 if (strncmp(type, "CMS1", 4) == 0) {
173 * VM style CMS1 labeled disk
175 int *label = (int *) &vlabel;
177 if (label[13] != 0) {
178 /* disk is reserved minidisk */
179 blocksize = label[3];
181 size = sectors512(label[7] - 1, blocksize);
183 offset = info.label_block + 1;
186 sp[0].start = sectors512(offset, blocksize);
187 sp[0].size = size - sp[0].start;
189 } else if ((strncmp(type, "VOL1", 4) == 0) &&
190 (!info.FBA_layout) && (!strcmp(info.type, "ECKD"))) {
192 * New style VOL1 labeled disk
196 /* get block number and read then go through format1 labels */
197 blk = cchhb2blk(&vlabel.vtoc, &geo) + 1;
199 if (lseek(fd_dasd, blk * blocksize, SEEK_SET) == -1)
202 while (read(fd_dasd, data, blocksize) != -1) {
205 memcpy(&f1, data, sizeof(format1_label_t));
207 /* skip FMT4 / FMT5 / FMT7 labels */
208 if (EBCtoASC[f1.DS1FMTID] == '4'
209 || EBCtoASC[f1.DS1FMTID] == '5'
210 || EBCtoASC[f1.DS1FMTID] == '7') {
215 /* only FMT1 valid at this point */
216 if (EBCtoASC[f1.DS1FMTID] != '1')
219 /* OK, we got valid partition data */
220 offset = cchh2blk(&f1.DS1EXT1.llimit, &geo);
221 size = cchh2blk(&f1.DS1EXT1.ulimit, &geo) -
222 offset + geo.sectors;
223 sp[counter].start = sectors512(offset, blocksize);
224 sp[counter].size = sectors512(size, blocksize);
231 * Old style LNX1 or unlabeled disk
233 sp[0].start = sectors512(info.label_block + 1, blocksize);
234 sp[0].size = disksize - sp[0].start;
241 if (fd_dasd != -1 && fd_dasd != fd)