[kpartx] add DASD large volume support
[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  */
51 int 
52 read_dasd_pt(int fd, struct slice all, struct slice *sp, int ns)
53 {
54         int retval = -1;
55         int blocksize;
56         uint64_t disksize;
57         uint64_t offset, size, fmt_size;
58         dasd_information_t info;
59         struct hd_geometry geo;
60         char type[5] = {0,};
61         volume_label_t vlabel;
62         unsigned char *data = NULL;
63         uint64_t blk;
64         int fd_dasd = -1;
65         struct stat sbuf;
66         dev_t dev;
67         char *devname;
68         char pathname[256];
69
70         if (fd < 0) {
71                 return -1;
72         }
73
74         if (fstat(fd, &sbuf) == -1) {
75                 return -1;
76         }
77
78         devname = dm_mapname(major(sbuf.st_rdev), minor(sbuf.st_rdev));
79
80         if (devname != NULL) {
81                 /* We were passed a handle to a dm device.
82                  * Get the first target and operate on that instead.
83                  */
84                 if (!(dev = dm_get_first_dep(devname))) {
85                         free(devname);
86                         return -1;
87                 }
88                 free(devname);
89
90                 if ((unsigned int)major(dev) != 94) {
91                         /* Not a DASD */
92                         return -1;
93                 }
94
95                 /*
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.
99                  */
100                 
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 */
107                                 return -1;
108                         }
109                         fd_dasd = open(pathname, O_RDONLY);
110                         /*
111                          * The file will vanish when the last process (we)
112                          * has ceased to access it.
113                          */
114                         unlink(pathname);
115                 }
116                 if (!fd_dasd) {
117                         /* Couldn't open the device */
118                         return -1;
119                 }
120         } else {
121                 fd_dasd = fd;
122         }
123
124         if (ioctl(fd_dasd, BIODASDINFO, (unsigned long)&info) != 0) {
125                 goto out;
126         }
127
128         if (ioctl(fd_dasd, HDIO_GETGEO, (unsigned long)&geo) != 0) {
129                 goto out;
130         }
131
132         if (ioctl(fd_dasd, BLKGETSIZE64, &disksize) != 0)
133                 goto out;
134         disksize >>= 9;
135
136         if (ioctl(fd_dasd, BLKSSZGET, &blocksize) != 0)
137                 goto out;
138
139         if (blocksize < 512 || blocksize > 4096)
140                 goto out;
141
142         /*
143          * Get volume label, extract name and type.
144          */
145
146         if (!(data = (unsigned char *)malloc(blocksize)))
147                 goto out;
148
149
150         if (lseek(fd_dasd, info.label_block * blocksize, SEEK_SET) == -1)
151                 goto out;
152         if (read(fd_dasd, data, blocksize) == -1) {
153                 perror("read");
154                 goto out;
155         }
156
157         if ((!info.FBA_layout) && (!strcmp(info.type, "ECKD")))
158                 memcpy (&vlabel, data, sizeof(vlabel));
159         else {
160                 bzero(&vlabel,4);
161                 memcpy (&vlabel.vollbl, data, sizeof(vlabel) - 4);
162         }
163         vtoc_ebcdic_dec(vlabel.vollbl, type, 4);
164
165         /*
166          * Three different types: CMS1, VOL1 and LNX1/unlabeled
167          */
168         if (strncmp(type, "CMS1", 4) == 0) {
169                 /*
170                  * VM style CMS1 labeled disk
171                  */
172                 unsigned int *label = (unsigned int *) &vlabel;
173
174                 blocksize = label[4];
175                 if (label[14] != 0) {
176                         /* disk is reserved minidisk */
177                         offset = label[14];
178                         size   = sectors512(label[8] - 1, blocksize);
179                 } else {
180                         offset = info.label_block + 1;
181                         size   = sectors512(label[8], blocksize);
182                 }
183                 sp[0].start = sectors512(offset, blocksize);
184                 sp[0].size  = size - sp[0].start;
185                 retval = 1;
186         } else if ((strncmp(type, "VOL1", 4) == 0) &&
187                 (!info.FBA_layout) && (!strcmp(info.type, "ECKD"))) {
188                 /*
189                  * New style VOL1 labeled disk
190                  */
191                 int counter;
192
193                 /* get block number and read then go through format1 labels */
194                 blk = cchhb2blk(&vlabel.vtoc, &geo) + 1;
195                 counter = 0;
196                 if (lseek(fd_dasd, blk * blocksize, SEEK_SET) == -1)
197                         goto out;
198
199                 while (read(fd_dasd, data, blocksize) != -1) {
200                         format1_label_t f1;
201
202                         memcpy(&f1, data, sizeof(format1_label_t));
203
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') {
209                                 blk++;
210                                 continue;
211                         }
212
213                         /* only FMT1 and FMT8 valid at this point */
214                         if (EBCtoASC[f1.DS1FMTID] != '1' &&
215                             EBCtoASC[f1.DS1FMTID] != '8')
216                                 break;
217
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);
224                         counter++;
225                         blk++;
226                 }
227                 retval = counter;
228         } else {
229                 /*
230                  * Old style LNX1 or unlabeled disk
231                  */
232                 if (strncmp(type, "LNX1", 4) == 0) {
233                         if (vlabel.ldl_version == 0xf2) {
234                                 fmt_size = sectors512(vlabel.formatted_blocks,
235                                                       blocksize);
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);
240                         } else {
241                                 /* old label and no usable disk geometry
242                                  * (e.g. DIAG) */
243                                 fmt_size = disksize;
244                         }
245                         size = disksize;
246                         if (fmt_size < size)
247                                 size = fmt_size;
248                 } else
249                         size = disksize;
250
251                 sp[0].start = sectors512(info.label_block + 1, blocksize);
252                 sp[0].size  = size - sp[0].start;
253                 retval = 1;
254         }
255
256  out:
257         if (data != NULL)
258                 free(data);
259         if (fd_dasd != -1 && fd_dasd != fd)
260                 close(fd_dasd);
261         return retval;
262 }