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, see <http://www.gnu.org/licenses/>.
29 #include <sys/types.h>
31 #include <sys/ioctl.h>
32 #include <linux/hdreg.h>
36 #include <libdevmapper.h>
37 #include "devmapper.h"
39 #include "byteorder.h"
42 unsigned long long sectors512(unsigned long long sectors, int blocksize)
44 return sectors * (blocksize >> 9);
48 * Magic records per track calculation, copied from fdasd.c
50 static unsigned int ceil_quot(unsigned int d1, unsigned int d2)
52 return (d1 + (d2 - 1)) / d2;
55 unsigned int recs_per_track(unsigned int dl)
57 int dn = ceil_quot(dl + 6, 232) + 1;
58 return 1729 / (10 + 9 + ceil_quot(dl + 6 * dn, 34));
62 typedef unsigned int __attribute__((__may_alias__)) label_ints_t;
67 read_dasd_pt(int fd, struct slice all, struct slice *sp, int ns)
72 uint64_t offset, size, fmt_size;
73 dasd_information_t info;
74 struct hd_geometry geo;
76 volume_label_t vlabel;
77 unsigned char *data = NULL;
89 if (fstat(fd, &sbuf) == -1) {
93 devname = dm_mapname(major(sbuf.st_rdev), minor(sbuf.st_rdev));
95 if (devname != NULL) {
96 /* We were passed a handle to a dm device.
97 * Get the first target and operate on that instead.
99 if (!(dev = dm_get_first_dep(devname))) {
105 if ((unsigned int)major(dev) != 94) {
111 * Hard to believe, but there's no simple way to translate
112 * major/minor into an openable device file, so we have
113 * to create one for ourselves.
116 sprintf(pathname, "/dev/.kpartx-node-%u-%u",
117 (unsigned int)major(dev), (unsigned int)minor(dev));
118 if ((fd_dasd = open(pathname, O_RDONLY)) == -1) {
119 /* Devicenode does not exist. Try to create one */
120 if (mknod(pathname, 0600 | S_IFBLK, dev) == -1) {
121 /* Couldn't create a device node */
124 fd_dasd = open(pathname, O_RDONLY);
126 * The file will vanish when the last process (we)
127 * has ceased to access it.
132 /* Couldn't open the device */
139 if (ioctl(fd_dasd, BIODASDINFO, (unsigned long)&info) != 0) {
140 info.label_block = 2;
142 memcpy(info.type, "ECKD", sizeof(info.type));
145 if (ioctl(fd_dasd, BLKSSZGET, &blocksize) != 0)
148 if (ioctl(fd_dasd, BLKGETSIZE64, &disksize) != 0)
151 if (ioctl(fd_dasd, HDIO_GETGEO, (unsigned long)&geo) != 0) {
155 geo.sectors = recs_per_track(blocksize);
156 cyl = disksize / (blocksize * geo.heads * geo.sectors);
157 if (cyl < LV_COMPAT_CYL)
160 geo.cylinders = LV_COMPAT_CYL;
166 if (blocksize < 512 || blocksize > 4096)
170 * Get volume label, extract name and type.
173 if (!(data = (unsigned char *)malloc(blocksize)))
177 if (lseek(fd_dasd, info.label_block * blocksize, SEEK_SET) == -1)
179 if (read(fd_dasd, data, blocksize) == -1) {
184 if ((!info.FBA_layout) && (!strcmp(info.type, "ECKD")))
185 memcpy (&vlabel, data, sizeof(vlabel));
188 memcpy (&vlabel.vollbl, data, sizeof(vlabel) - 4);
190 vtoc_ebcdic_dec(vlabel.vollbl, type, 4);
193 * Three different types: CMS1, VOL1 and LNX1/unlabeled
195 if (strncmp(type, "CMS1", 4) == 0) {
197 * VM style CMS1 labeled disk
199 label_ints_t *label = (label_ints_t *) &vlabel;
201 blocksize = label[4];
202 if (label[14] != 0) {
203 /* disk is reserved minidisk */
205 size = sectors512(label[8] - 1, blocksize);
207 offset = info.label_block + 1;
208 size = sectors512(label[8], blocksize);
210 sp[0].start = sectors512(offset, blocksize);
211 sp[0].size = size - sp[0].start;
213 } else if ((strncmp(type, "VOL1", 4) == 0) &&
214 (!info.FBA_layout) && (!strcmp(info.type, "ECKD"))) {
216 * New style VOL1 labeled disk
220 /* get block number and read then go through format1 labels */
221 blk = cchhb2blk(&vlabel.vtoc, &geo) + 1;
223 if (lseek(fd_dasd, blk * blocksize, SEEK_SET) == -1)
226 while (read(fd_dasd, data, blocksize) != -1) {
229 memcpy(&f1, data, sizeof(format1_label_t));
231 /* skip FMT4 / FMT5 / FMT7 labels */
232 if (EBCtoASC[f1.DS1FMTID] == '4'
233 || EBCtoASC[f1.DS1FMTID] == '5'
234 || EBCtoASC[f1.DS1FMTID] == '7'
235 || EBCtoASC[f1.DS1FMTID] == '9') {
240 /* only FMT1 and FMT8 valid at this point */
241 if (EBCtoASC[f1.DS1FMTID] != '1' &&
242 EBCtoASC[f1.DS1FMTID] != '8')
245 /* OK, we got valid partition data */
246 offset = cchh2blk(&f1.DS1EXT1.llimit, &geo);
247 size = cchh2blk(&f1.DS1EXT1.ulimit, &geo) -
248 offset + geo.sectors;
249 sp[counter].start = sectors512(offset, blocksize);
250 sp[counter].size = sectors512(size, blocksize);
257 * Old style LNX1 or unlabeled disk
259 if (strncmp(type, "LNX1", 4) == 0) {
260 if (vlabel.ldl_version == 0xf2) {
261 fmt_size = sectors512(vlabel.formatted_blocks,
263 } else if (!strcmp(info.type, "ECKD")) {
264 /* formated w/o large volume support */
265 fmt_size = geo.cylinders * geo.heads
266 * geo.sectors * (blocksize >> 9);
268 /* old label and no usable disk geometry
278 sp[0].start = sectors512(info.label_block + 1, blocksize);
279 sp[0].size = size - sp[0].start;
286 if (fd_dasd != -1 && fd_dasd != fd)