Imported Upstream version 0.6.1
[platform/upstream/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, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
24  * USA.
25  */
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <inttypes.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <sys/ioctl.h>
34 #include <linux/hdreg.h>
35 #include <errno.h>
36 #include <string.h>
37 #include <fcntl.h>
38 #include <libdevmapper.h>
39 #include "devmapper.h"
40 #include "kpartx.h"
41 #include "byteorder.h"
42 #include "dasd.h"
43
44 unsigned long long sectors512(unsigned long long sectors, int blocksize)
45 {
46         return sectors * (blocksize >> 9);
47 }
48
49 /*
50  * Magic records per track calculation, copied from fdasd.c
51  */
52 static unsigned int ceil_quot(unsigned int d1, unsigned int d2)
53 {
54         return (d1 + (d2 - 1)) / d2;
55 }
56
57 unsigned int recs_per_track(unsigned int dl)
58 {
59         int dn = ceil_quot(dl + 6, 232) + 1;
60         return 1729 / (10 + 9 + ceil_quot(dl + 6 * dn, 34));
61 }
62
63
64 typedef unsigned int __attribute__((__may_alias__)) label_ints_t;
65
66 /*
67  */
68 int 
69 read_dasd_pt(int fd, struct slice all, struct slice *sp, 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) {
134                         /* Couldn't open the device */
135                         return -1;
136                 }
137         } else {
138                 fd_dasd = fd;
139         }
140
141         if (ioctl(fd_dasd, BIODASDINFO, (unsigned long)&info) != 0) {
142                 info.label_block = 2;
143                 info.FBA_layout = 0;
144                 memcpy(info.type, "ECKD", sizeof(info.type));
145         }
146
147         if (ioctl(fd_dasd, BLKSSZGET, &blocksize) != 0)
148                 goto out;
149
150         if (ioctl(fd_dasd, BLKGETSIZE64, &disksize) != 0)
151                 goto out;
152
153         if (ioctl(fd_dasd, HDIO_GETGEO, (unsigned long)&geo) != 0) {
154                 unsigned int cyl;
155
156                 geo.heads = 15;
157                 geo.sectors = recs_per_track(blocksize);
158                 cyl = disksize / (blocksize * geo.heads * geo.sectors);
159                 if (cyl < LV_COMPAT_CYL)
160                         geo.cylinders = cyl;
161                 else
162                         geo.cylinders = LV_COMPAT_CYL;
163                 geo.start = 0;
164         }
165
166         disksize >>= 9;
167
168         if (blocksize < 512 || blocksize > 4096)
169                 goto out;
170
171         /*
172          * Get volume label, extract name and type.
173          */
174
175         if (!(data = (unsigned char *)malloc(blocksize)))
176                 goto out;
177
178
179         if (lseek(fd_dasd, info.label_block * blocksize, SEEK_SET) == -1)
180                 goto out;
181         if (read(fd_dasd, data, blocksize) == -1) {
182                 perror("read");
183                 goto out;
184         }
185
186         if ((!info.FBA_layout) && (!strcmp(info.type, "ECKD")))
187                 memcpy (&vlabel, data, sizeof(vlabel));
188         else {
189                 bzero(&vlabel,4);
190                 memcpy (&vlabel.vollbl, data, sizeof(vlabel) - 4);
191         }
192         vtoc_ebcdic_dec(vlabel.vollbl, type, 4);
193
194         /*
195          * Three different types: CMS1, VOL1 and LNX1/unlabeled
196          */
197         if (strncmp(type, "CMS1", 4) == 0) {
198                 /*
199                  * VM style CMS1 labeled disk
200                  */
201                 label_ints_t *label = (label_ints_t *) &vlabel;
202
203                 blocksize = label[4];
204                 if (label[14] != 0) {
205                         /* disk is reserved minidisk */
206                         offset = label[14];
207                         size   = sectors512(label[8] - 1, blocksize);
208                 } else {
209                         offset = info.label_block + 1;
210                         size   = sectors512(label[8], blocksize);
211                 }
212                 sp[0].start = sectors512(offset, blocksize);
213                 sp[0].size  = size - sp[0].start;
214                 retval = 1;
215         } else if ((strncmp(type, "VOL1", 4) == 0) &&
216                 (!info.FBA_layout) && (!strcmp(info.type, "ECKD"))) {
217                 /*
218                  * New style VOL1 labeled disk
219                  */
220                 int counter;
221
222                 /* get block number and read then go through format1 labels */
223                 blk = cchhb2blk(&vlabel.vtoc, &geo) + 1;
224                 counter = 0;
225                 if (lseek(fd_dasd, blk * blocksize, SEEK_SET) == -1)
226                         goto out;
227
228                 while (read(fd_dasd, data, blocksize) != -1) {
229                         format1_label_t f1;
230
231                         memcpy(&f1, data, sizeof(format1_label_t));
232
233                         /* skip FMT4 / FMT5 / FMT7 labels */
234                         if (EBCtoASC[f1.DS1FMTID] == '4'
235                             || EBCtoASC[f1.DS1FMTID] == '5'
236                             || EBCtoASC[f1.DS1FMTID] == '7'
237                             || EBCtoASC[f1.DS1FMTID] == '9') {
238                                 blk++;
239                                 continue;
240                         }
241
242                         /* only FMT1 and FMT8 valid at this point */
243                         if (EBCtoASC[f1.DS1FMTID] != '1' &&
244                             EBCtoASC[f1.DS1FMTID] != '8')
245                                 break;
246
247                         /* OK, we got valid partition data */
248                         offset = cchh2blk(&f1.DS1EXT1.llimit, &geo);
249                         size  = cchh2blk(&f1.DS1EXT1.ulimit, &geo) -
250                                 offset + geo.sectors;
251                         sp[counter].start = sectors512(offset, blocksize);
252                         sp[counter].size  = sectors512(size, blocksize);
253                         counter++;
254                         blk++;
255                 }
256                 retval = counter;
257         } else {
258                 /*
259                  * Old style LNX1 or unlabeled disk
260                  */
261                 if (strncmp(type, "LNX1", 4) == 0) {
262                         if (vlabel.ldl_version == 0xf2) {
263                                 fmt_size = sectors512(vlabel.formatted_blocks,
264                                                       blocksize);
265                         } else if (!strcmp(info.type, "ECKD")) {
266                                 /* formated w/o large volume support */
267                                 fmt_size = geo.cylinders * geo.heads
268                                         * geo.sectors * (blocksize >> 9);
269                         } else {
270                                 /* old label and no usable disk geometry
271                                  * (e.g. DIAG) */
272                                 fmt_size = disksize;
273                         }
274                         size = disksize;
275                         if (fmt_size < size)
276                                 size = fmt_size;
277                 } else
278                         size = disksize;
279
280                 sp[0].start = sectors512(info.label_block + 1, blocksize);
281                 sp[0].size  = size - sp[0].start;
282                 retval = 1;
283         }
284
285  out:
286         if (data != NULL)
287                 free(data);
288         if (fd_dasd != -1 && fd_dasd != fd)
289                 close(fd_dasd);
290         return retval;
291 }