]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - stand/common/disk.c
Make lsdev -v output line up in neat columns by using a fixed width for
[FreeBSD/FreeBSD.git] / stand / common / disk.c
1 /*-
2  * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3  * Copyright (c) 2012 Andrey V. Elsukov <ae@FreeBSD.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include <sys/disk.h>
32 #include <sys/queue.h>
33 #include <stand.h>
34 #include <stdarg.h>
35 #include <bootstrap.h>
36 #include <part.h>
37
38 #include "disk.h"
39
40 #ifdef DISK_DEBUG
41 # define DEBUG(fmt, args...)    printf("%s: " fmt "\n" , __func__ , ## args)
42 #else
43 # define DEBUG(fmt, args...)
44 #endif
45
46 struct open_disk {
47         struct ptable           *table;
48         uint64_t                mediasize;
49         uint64_t                entrysize;
50         u_int                   sectorsize;
51 };
52
53 struct print_args {
54         struct disk_devdesc     *dev;
55         const char              *prefix;
56         int                     verbose;
57 };
58
59 /* Convert size to a human-readable number. */
60 static char *
61 display_size(uint64_t size, u_int sectorsize)
62 {
63         static char buf[80];
64         char unit;
65
66         size = size * sectorsize / 1024;
67         unit = 'K';
68         if (size >= 10485760000LL) {
69                 size /= 1073741824;
70                 unit = 'T';
71         } else if (size >= 10240000) {
72                 size /= 1048576;
73                 unit = 'G';
74         } else if (size >= 10000) {
75                 size /= 1024;
76                 unit = 'M';
77         }
78         sprintf(buf, "%4ld%cB", (long)size, unit);
79         return (buf);
80 }
81
82 int
83 ptblread(void *d, void *buf, size_t blocks, uint64_t offset)
84 {
85         struct disk_devdesc *dev;
86         struct open_disk *od;
87
88         dev = (struct disk_devdesc *)d;
89         od = (struct open_disk *)dev->dd.d_opendata;
90
91         /*
92          * The strategy function assumes the offset is in units of 512 byte
93          * sectors. For larger sector sizes, we need to adjust the offset to
94          * match the actual sector size.
95          */
96         offset *= (od->sectorsize / 512);
97         /*
98          * As the GPT backup partition is located at the end of the disk,
99          * to avoid reading past disk end, flag bcache not to use RA.
100          */
101         return (dev->dd.d_dev->dv_strategy(dev, F_READ | F_NORA, offset,
102             blocks * od->sectorsize, (char *)buf, NULL));
103 }
104
105 #define PWIDTH  35
106 static int
107 ptable_print(void *arg, const char *pname, const struct ptable_entry *part)
108 {
109         struct disk_devdesc dev;
110         struct print_args *pa, bsd;
111         struct open_disk *od;
112         struct ptable *table;
113         char line[80];
114         int res;
115         u_int sectsize;
116         uint64_t partsize;
117
118         pa = (struct print_args *)arg;
119         od = (struct open_disk *)pa->dev->dd.d_opendata;
120         sectsize = od->sectorsize;
121         partsize = part->end - part->start + 1;
122         sprintf(line, "  %s%s: %s\t%s\n", pa->prefix, pname,
123             parttype2str(part->type),
124             pa->verbose ? display_size(partsize, sectsize) : "");
125         if (pager_output(line))
126                 return 1;
127         res = 0;
128         if (part->type == PART_FREEBSD) {
129                 /* Open slice with BSD label */
130                 dev.dd.d_dev = pa->dev->dd.d_dev;
131                 dev.dd.d_unit = pa->dev->dd.d_unit;
132                 dev.d_slice = part->index;
133                 dev.d_partition = -1;
134                 if (disk_open(&dev, partsize, sectsize) == 0) {
135                         /*
136                          * disk_open() for partition -1 on a bsd slice assumes
137                          * you want the first bsd partition.  Reset things so
138                          * that we're looking at the start of the raw slice.
139                          */
140                         dev.d_partition = -1;
141                         dev.d_offset = part->start;
142                         table = ptable_open(&dev, partsize, sectsize, ptblread);
143                         if (table != NULL) {
144                                 sprintf(line, "  %s%s", pa->prefix, pname);
145                                 bsd.dev = pa->dev;
146                                 bsd.prefix = line;
147                                 bsd.verbose = pa->verbose;
148                                 res = ptable_iterate(table, &bsd, ptable_print);
149                                 ptable_close(table);
150                         }
151                         disk_close(&dev);
152                 }
153         }
154
155         return (res);
156 }
157 #undef PWIDTH
158
159 int
160 disk_print(struct disk_devdesc *dev, char *prefix, int verbose)
161 {
162         struct open_disk *od;
163         struct print_args pa;
164
165         /* Disk should be opened */
166         od = (struct open_disk *)dev->dd.d_opendata;
167         pa.dev = dev;
168         pa.prefix = prefix;
169         pa.verbose = verbose;
170         return (ptable_iterate(od->table, &pa, ptable_print));
171 }
172
173 int
174 disk_read(struct disk_devdesc *dev, void *buf, uint64_t offset, u_int blocks)
175 {
176         struct open_disk *od;
177         int ret;
178
179         od = (struct open_disk *)dev->dd.d_opendata;
180         ret = dev->dd.d_dev->dv_strategy(dev, F_READ, dev->d_offset + offset,
181             blocks * od->sectorsize, buf, NULL);
182
183         return (ret);
184 }
185
186 int
187 disk_write(struct disk_devdesc *dev, void *buf, uint64_t offset, u_int blocks)
188 {
189         struct open_disk *od;
190         int ret;
191
192         od = (struct open_disk *)dev->dd.d_opendata;
193         ret = dev->dd.d_dev->dv_strategy(dev, F_WRITE, dev->d_offset + offset,
194             blocks * od->sectorsize, buf, NULL);
195
196         return (ret);
197 }
198
199 int
200 disk_ioctl(struct disk_devdesc *dev, u_long cmd, void *data)
201 {
202         struct open_disk *od = dev->dd.d_opendata;
203
204         if (od == NULL)
205                 return (ENOTTY);
206
207         switch (cmd) {
208         case DIOCGSECTORSIZE:
209                 *(u_int *)data = od->sectorsize;
210                 break;
211         case DIOCGMEDIASIZE:
212                 if (dev->d_offset == 0)
213                         *(uint64_t *)data = od->mediasize;
214                 else
215                         *(uint64_t *)data = od->entrysize * od->sectorsize;
216                 break;
217         default:
218                 return (ENOTTY);
219         }
220
221         return (0);
222 }
223
224 int
225 disk_open(struct disk_devdesc *dev, uint64_t mediasize, u_int sectorsize)
226 {
227         struct disk_devdesc partdev;
228         struct open_disk *od;
229         struct ptable *table;
230         struct ptable_entry part;
231         int rc, slice, partition;
232
233         rc = 0;
234         od = (struct open_disk *)malloc(sizeof(struct open_disk));
235         if (od == NULL) {
236                 DEBUG("no memory");
237                 return (ENOMEM);
238         }
239         dev->dd.d_opendata = od;
240         od->entrysize = 0;
241         od->mediasize = mediasize;
242         od->sectorsize = sectorsize;
243         /*
244          * While we are reading disk metadata, make sure we do it relative
245          * to the start of the disk
246          */
247         memcpy(&partdev, dev, sizeof(partdev));
248         partdev.d_offset = 0;
249         partdev.d_slice = -1;
250         partdev.d_partition = -1;
251
252         dev->d_offset = 0;
253         table = NULL;
254         slice = dev->d_slice;
255         partition = dev->d_partition;
256
257         DEBUG("%s unit %d, slice %d, partition %d => %p",
258             disk_fmtdev(dev), dev->dd.d_unit, dev->d_slice, dev->d_partition, od);
259
260         /* Determine disk layout. */
261         od->table = ptable_open(&partdev, mediasize / sectorsize, sectorsize,
262             ptblread);
263         if (od->table == NULL) {
264                 DEBUG("Can't read partition table");
265                 rc = ENXIO;
266                 goto out;
267         }
268
269         if (ptable_getsize(od->table, &mediasize) != 0) {
270                 rc = ENXIO;
271                 goto out;
272         }
273         od->mediasize = mediasize;
274
275         if (ptable_gettype(od->table) == PTABLE_BSD &&
276             partition >= 0) {
277                 /* It doesn't matter what value has d_slice */
278                 rc = ptable_getpart(od->table, &part, partition);
279                 if (rc == 0) {
280                         dev->d_offset = part.start;
281                         od->entrysize = part.end - part.start + 1;
282                 }
283         } else if (ptable_gettype(od->table) == PTABLE_ISO9660) {
284                 dev->d_offset = 0;
285                 od->entrysize = mediasize;
286         } else if (slice >= 0) {
287                 /* Try to get information about partition */
288                 if (slice == 0)
289                         rc = ptable_getbestpart(od->table, &part);
290                 else
291                         rc = ptable_getpart(od->table, &part, slice);
292                 if (rc != 0) /* Partition doesn't exist */
293                         goto out;
294                 dev->d_offset = part.start;
295                 od->entrysize = part.end - part.start + 1;
296                 slice = part.index;
297                 if (ptable_gettype(od->table) == PTABLE_GPT) {
298                         partition = 255;
299                         goto out; /* Nothing more to do */
300                 } else if (partition == 255) {
301                         /*
302                          * When we try to open GPT partition, but partition
303                          * table isn't GPT, reset d_partition value to -1
304                          * and try to autodetect appropriate value.
305                          */
306                         partition = -1;
307                 }
308                 /*
309                  * If d_partition < 0 and we are looking at a BSD slice,
310                  * then try to read BSD label, otherwise return the
311                  * whole MBR slice.
312                  */
313                 if (partition == -1 &&
314                     part.type != PART_FREEBSD)
315                         goto out;
316                 /* Try to read BSD label */
317                 table = ptable_open(dev, part.end - part.start + 1,
318                     od->sectorsize, ptblread);
319                 if (table == NULL) {
320                         DEBUG("Can't read BSD label");
321                         rc = ENXIO;
322                         goto out;
323                 }
324                 /*
325                  * If slice contains BSD label and d_partition < 0, then
326                  * assume the 'a' partition. Otherwise just return the
327                  * whole MBR slice, because it can contain ZFS.
328                  */
329                 if (partition < 0) {
330                         if (ptable_gettype(table) != PTABLE_BSD)
331                                 goto out;
332                         partition = 0;
333                 }
334                 rc = ptable_getpart(table, &part, partition);
335                 if (rc != 0)
336                         goto out;
337                 dev->d_offset += part.start;
338                 od->entrysize = part.end - part.start + 1;
339         }
340 out:
341         if (table != NULL)
342                 ptable_close(table);
343
344         if (rc != 0) {
345                 if (od->table != NULL)
346                         ptable_close(od->table);
347                 free(od);
348                 DEBUG("%s could not open", disk_fmtdev(dev));
349         } else {
350                 /* Save the slice and partition number to the dev */
351                 dev->d_slice = slice;
352                 dev->d_partition = partition;
353                 DEBUG("%s offset %lld => %p", disk_fmtdev(dev),
354                     (long long)dev->d_offset, od);
355         }
356         return (rc);
357 }
358
359 int
360 disk_close(struct disk_devdesc *dev)
361 {
362         struct open_disk *od;
363
364         od = (struct open_disk *)dev->dd.d_opendata;
365         DEBUG("%s closed => %p", disk_fmtdev(dev), od);
366         ptable_close(od->table);
367         free(od);
368         return (0);
369 }
370
371 char*
372 disk_fmtdev(struct disk_devdesc *dev)
373 {
374         static char buf[128];
375         char *cp;
376
377         cp = buf + sprintf(buf, "%s%d", dev->dd.d_dev->dv_name, dev->dd.d_unit);
378         if (dev->d_slice >= 0) {
379 #ifdef LOADER_GPT_SUPPORT
380                 if (dev->d_partition == 255) {
381                         sprintf(cp, "p%d:", dev->d_slice);
382                         return (buf);
383                 } else
384 #endif
385 #ifdef LOADER_MBR_SUPPORT
386                         cp += sprintf(cp, "s%d", dev->d_slice);
387 #endif
388         }
389         if (dev->d_partition >= 0)
390                 cp += sprintf(cp, "%c", dev->d_partition + 'a');
391         strcat(cp, ":");
392         return (buf);
393 }
394
395 int
396 disk_parsedev(struct disk_devdesc *dev, const char *devspec, const char **path)
397 {
398         int unit, slice, partition;
399         const char *np;
400         char *cp;
401
402         np = devspec;
403         unit = slice = partition = -1;
404         if (*np != '\0' && *np != ':') {
405                 unit = strtol(np, &cp, 10);
406                 if (cp == np)
407                         return (EUNIT);
408 #ifdef LOADER_GPT_SUPPORT
409                 if (*cp == 'p') {
410                         np = cp + 1;
411                         slice = strtol(np, &cp, 10);
412                         if (np == cp)
413                                 return (ESLICE);
414                         /* we don't support nested partitions on GPT */
415                         if (*cp != '\0' && *cp != ':')
416                                 return (EINVAL);
417                         partition = 255;
418                 } else
419 #endif
420 #ifdef LOADER_MBR_SUPPORT
421                 if (*cp == 's') {
422                         np = cp + 1;
423                         slice = strtol(np, &cp, 10);
424                         if (np == cp)
425                                 return (ESLICE);
426                 }
427 #endif
428                 if (*cp != '\0' && *cp != ':') {
429                         partition = *cp - 'a';
430                         if (partition < 0)
431                                 return (EPART);
432                         cp++;
433                 }
434         } else
435                 return (EINVAL);
436
437         if (*cp != '\0' && *cp != ':')
438                 return (EINVAL);
439         dev->dd.d_unit = unit;
440         dev->d_slice = slice;
441         dev->d_partition = partition;
442         if (path != NULL)
443                 *path = (*cp == '\0') ? cp: cp + 1;
444         return (0);
445 }