1486ccaaffd9a0b20eb1a2dd1830b4304299a17e
[multipath-tools/.git] / kpartx / dasd.c
1 /*
2  * dasd.c
3  *
4  * IBM DASD partition table handling.
5  *
6  * Mostly taken from drivers/s390/block/dasd.c
7  *
8  * Copyright (c) 2005, Hannes Reinecke, SUSE Linux Products GmbH
9  * Copyright IBM Corporation, 2009
10  *
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.
15  *
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.
20  *
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/>.
23  */
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <inttypes.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <sys/sysmacros.h>
32 #include <sys/ioctl.h>
33 #include <linux/hdreg.h>
34 #include <errno.h>
35 #include <string.h>
36 #include <fcntl.h>
37 #include <libdevmapper.h>
38 #include "devmapper.h"
39 #include "kpartx.h"
40 #include "byteorder.h"
41 #include "dasd.h"
42
43 unsigned long long sectors512(unsigned long long sectors, int blocksize)
44 {
45         return sectors * (blocksize >> 9);
46 }
47
48 /*
49  * Magic records per track calculation, copied from fdasd.c
50  */
51 static unsigned int ceil_quot(unsigned int d1, unsigned int d2)
52 {
53         return (d1 + (d2 - 1)) / d2;
54 }
55
56 unsigned int recs_per_track(unsigned int dl)
57 {
58         int dn = ceil_quot(dl + 6, 232) + 1;
59         return 1729 / (10 + 9 + ceil_quot(dl + 6 * dn, 34));
60 }
61
62
63 typedef unsigned int __attribute__((__may_alias__)) label_ints_t;
64
65 /*
66  */
67 int
68 read_dasd_pt(int fd, __attribute__((unused)) struct slice all,
69              struct slice *sp, __attribute__((unused)) unsigned int ns)
70 {
71         int retval = -1;
72         int blocksize;
73         uint64_t disksize;
74         uint64_t offset, size, fmt_size;
75         dasd_information_t info;
76         struct hd_geometry geo;
77         char type[5] = {0,};
78         volume_label_t vlabel;
79         unsigned char *data = NULL;
80         uint64_t blk;
81         int fd_dasd = -1;
82         struct stat sbuf;
83         dev_t dev;
84         char *devname;
85         char pathname[256];
86
87         if (fd < 0) {
88                 return -1;
89         }
90
91         if (fstat(fd, &sbuf) == -1) {
92                 return -1;
93         }
94
95         devname = dm_mapname(major(sbuf.st_rdev), minor(sbuf.st_rdev));
96
97         if (devname != NULL) {
98                 /* We were passed a handle to a dm device.
99                  * Get the first target and operate on that instead.
100                  */
101                 if (!(dev = dm_get_first_dep(devname))) {
102                         free(devname);
103                         return -1;
104                 }
105                 free(devname);
106
107                 if ((unsigned int)major(dev) != 94) {
108                         /* Not a DASD */
109                         return -1;
110                 }
111
112                 /*
113                  * Hard to believe, but there's no simple way to translate
114                  * major/minor into an openable device file, so we have
115                  * to create one for ourselves.
116                  */
117
118                 sprintf(pathname, "/dev/.kpartx-node-%u-%u",
119                         (unsigned int)major(dev), (unsigned int)minor(dev));
120                 if ((fd_dasd = open(pathname, O_RDONLY)) == -1) {
121                         /* Devicenode does not exist. Try to create one */
122                         if (mknod(pathname, 0600 | S_IFBLK, dev) == -1) {
123                                 /* Couldn't create a device node */
124                                 return -1;
125                         }
126                         fd_dasd = open(pathname, O_RDONLY);
127                         /*
128                          * The file will vanish when the last process (we)
129                          * has ceased to access it.
130                          */
131                         unlink(pathname);
132                 }
133                 if (fd_dasd < 0) {
134                         /* Couldn't open the device */
135                         return -1;
136                 }
137         } else {
138                 fd_dasd = dup(fd);
139                 if (fd_dasd < 0)
140                         return -1;
141         }
142
143         if (ioctl(fd_dasd, BIODASDINFO, (unsigned long)&info) != 0) {
144                 info.label_block = 2;
145                 info.FBA_layout = 0;
146                 memcpy(info.type, "ECKD", sizeof(info.type));
147         }
148
149         if (ioctl(fd_dasd, BLKSSZGET, &blocksize) != 0)
150                 goto out;
151
152         if (ioctl(fd_dasd, BLKGETSIZE64, &disksize) != 0)
153                 goto out;
154
155         if (ioctl(fd_dasd, HDIO_GETGEO, (unsigned long)&geo) != 0) {
156                 unsigned int cyl;
157
158                 geo.heads = 15;
159                 geo.sectors = recs_per_track(blocksize);
160                 cyl = disksize / ((uint64_t)blocksize * geo.heads *
161                                   geo.sectors);
162                 if (cyl < LV_COMPAT_CYL)
163                         geo.cylinders = cyl;
164                 else
165                         geo.cylinders = LV_COMPAT_CYL;
166                 geo.start = 0;
167         }
168
169         disksize >>= 9;
170
171         if (blocksize < 512 || blocksize > 4096)
172                 goto out;
173
174         /*
175          * Get volume label, extract name and type.
176          */
177
178         if (!(data = (unsigned char *)malloc(blocksize)))
179                 goto out;
180
181
182         if (lseek(fd_dasd, info.label_block * blocksize, SEEK_SET) == -1)
183                 goto out;
184         if (read(fd_dasd, data, blocksize) == -1) {
185                 perror("read");
186                 goto out;
187         }
188
189         if ((!info.FBA_layout) && (!strcmp(info.type, "ECKD")))
190                 memcpy (&vlabel, data, sizeof(vlabel));
191         else {
192                 bzero(&vlabel,4);
193                 memcpy ((char *)&vlabel + 4, data, sizeof(vlabel) - 4);
194         }
195         vtoc_ebcdic_dec(vlabel.vollbl, type, 4);
196
197         /*
198          * Three different types: CMS1, VOL1 and LNX1/unlabeled
199          */
200         if (strncmp(type, "CMS1", 4) == 0) {
201                 /*
202                  * VM style CMS1 labeled disk
203                  */
204                 label_ints_t *label = (label_ints_t *) &vlabel;
205
206                 blocksize = label[4];
207                 if (label[14] != 0) {
208                         /* disk is reserved minidisk */
209                         offset = label[14];
210                         size   = sectors512(label[8] - 1, blocksize);
211                 } else {
212                         offset = info.label_block + 1;
213                         size   = sectors512(label[8], blocksize);
214                 }
215                 sp[0].start = sectors512(offset, blocksize);
216                 sp[0].size  = size - sp[0].start;
217                 retval = 1;
218         } else if ((strncmp(type, "VOL1", 4) == 0) &&
219                 (!info.FBA_layout) && (!strcmp(info.type, "ECKD"))) {
220                 /*
221                  * New style VOL1 labeled disk
222                  */
223                 int counter;
224
225                 /* get block number and read then go through format1 labels */
226                 blk = cchhb2blk(&vlabel.vtoc, &geo) + 1;
227                 counter = 0;
228                 if (lseek(fd_dasd, blk * blocksize, SEEK_SET) == -1)
229                         goto out;
230
231                 while (read(fd_dasd, data, blocksize) != -1) {
232                         format1_label_t f1;
233
234                         memcpy(&f1, data, sizeof(format1_label_t));
235
236                         /* skip FMT4 / FMT5 / FMT7 labels */
237                         if (EBCtoASC[f1.DS1FMTID] == '4'
238                             || EBCtoASC[f1.DS1FMTID] == '5'
239                             || EBCtoASC[f1.DS1FMTID] == '7'
240                             || EBCtoASC[f1.DS1FMTID] == '9') {
241                                 blk++;
242                                 continue;
243                         }
244
245                         /* only FMT1 and FMT8 valid at this point */
246                         if (EBCtoASC[f1.DS1FMTID] != '1' &&
247                             EBCtoASC[f1.DS1FMTID] != '8')
248                                 break;
249
250                         /* OK, we got valid partition data */
251                         offset = cchh2blk(&f1.DS1EXT1.llimit, &geo);
252                         size  = cchh2blk(&f1.DS1EXT1.ulimit, &geo) -
253                                 offset + geo.sectors;
254                         sp[counter].start = sectors512(offset, blocksize);
255                         sp[counter].size  = sectors512(size, blocksize);
256                         counter++;
257                         blk++;
258                 }
259                 retval = counter;
260         } else {
261                 /*
262                  * Old style LNX1 or unlabeled disk
263                  */
264                 if (strncmp(type, "LNX1", 4) == 0) {
265                         if (vlabel.ldl_version == 0xf2) {
266                                 fmt_size = sectors512(vlabel.formatted_blocks,
267                                                       blocksize);
268                         } else if (!strcmp(info.type, "ECKD")) {
269                                 /* formatted w/o large volume support */
270                                 fmt_size = geo.cylinders * geo.heads
271                                         * geo.sectors * (blocksize >> 9);
272                         } else {
273                                 /* old label and no usable disk geometry
274                                  * (e.g. DIAG) */
275                                 fmt_size = disksize;
276                         }
277                         size = disksize;
278                         if (fmt_size < size)
279                                 size = fmt_size;
280                 } else if ((unsigned int)major(sbuf.st_rdev) != 94) {
281                         /* Not a DASD */
282                         retval = -1;
283                         goto out;
284                 } else
285                         size = disksize;
286
287                 sp[0].start = sectors512(info.label_block + 1, blocksize);
288                 sp[0].size  = size - sp[0].start;
289                 retval = 1;
290         }
291
292 out:
293         if (data != NULL)
294                 free(data);
295         close(fd_dasd);
296         return retval;
297 }