]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/boot/common/disk.c
Import mandoc 1.4.1rc2
[FreeBSD/FreeBSD.git] / sys / boot / 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         u_int                   flags;
52         int                     rcnt;
53 };
54
55 struct print_args {
56         struct disk_devdesc     *dev;
57         const char              *prefix;
58         int                     verbose;
59 };
60
61 struct dentry {
62         const struct devsw      *d_dev;
63         int                     d_unit;
64         int                     d_slice;
65         int                     d_partition;
66
67         struct open_disk        *od;
68         uint64_t                d_offset;
69         STAILQ_ENTRY(dentry)    entry;
70 #ifdef DISK_DEBUG
71         uint32_t                count;
72 #endif
73 };
74
75 static STAILQ_HEAD(, dentry) opened_disks =
76     STAILQ_HEAD_INITIALIZER(opened_disks);
77
78 static int
79 disk_lookup(struct disk_devdesc *dev)
80 {
81         struct dentry *entry;
82         int rc;
83
84         rc = ENOENT;
85         STAILQ_FOREACH(entry, &opened_disks, entry) {
86                 if (entry->d_dev != dev->d_dev ||
87                     entry->d_unit != dev->d_unit)
88                         continue;
89                 dev->d_opendata = entry->od;
90                 if (entry->d_slice == dev->d_slice &&
91                     entry->d_partition == dev->d_partition) {
92                         dev->d_offset = entry->d_offset;
93                         DEBUG("%s offset %lld", disk_fmtdev(dev),
94                             (long long)dev->d_offset);
95 #ifdef DISK_DEBUG
96                         entry->count++;
97 #endif
98                         return (0);
99                 }
100                 rc = EAGAIN;
101         }
102         return (rc);
103 }
104
105 static void
106 disk_insert(struct disk_devdesc *dev)
107 {
108         struct dentry *entry;
109
110         entry = (struct dentry *)malloc(sizeof(struct dentry));
111         if (entry == NULL) {
112                 DEBUG("no memory");
113                 return;
114         }
115         entry->d_dev = dev->d_dev;
116         entry->d_unit = dev->d_unit;
117         entry->d_slice = dev->d_slice;
118         entry->d_partition = dev->d_partition;
119         entry->od = (struct open_disk *)dev->d_opendata;
120         entry->od->rcnt++;
121         entry->d_offset = dev->d_offset;
122 #ifdef DISK_DEBUG
123         entry->count = 1;
124 #endif
125         STAILQ_INSERT_TAIL(&opened_disks, entry, entry);
126         DEBUG("%s cached", disk_fmtdev(dev));
127 }
128
129 #ifdef DISK_DEBUG
130 COMMAND_SET(dcachestat, "dcachestat", "get disk cache stats",
131     command_dcachestat);
132
133 static int
134 command_dcachestat(int argc, char *argv[])
135 {
136         struct disk_devdesc dev;
137         struct dentry *entry;
138
139         STAILQ_FOREACH(entry, &opened_disks, entry) {
140                 dev.d_dev = (struct devsw *)entry->d_dev;
141                 dev.d_unit = entry->d_unit;
142                 dev.d_slice = entry->d_slice;
143                 dev.d_partition = entry->d_partition;
144                 printf("%s %d => %p [%d]\n", disk_fmtdev(&dev), entry->count,
145                     entry->od, entry->od->rcnt);
146         }
147         return (CMD_OK);
148 }
149 #endif /* DISK_DEBUG */
150
151 /* Convert size to a human-readable number. */
152 static char *
153 display_size(uint64_t size, u_int sectorsize)
154 {
155         static char buf[80];
156         char unit;
157
158         size = size * sectorsize / 1024;
159         unit = 'K';
160         if (size >= 10485760000LL) {
161                 size /= 1073741824;
162                 unit = 'T';
163         } else if (size >= 10240000) {
164                 size /= 1048576;
165                 unit = 'G';
166         } else if (size >= 10000) {
167                 size /= 1024;
168                 unit = 'M';
169         }
170         sprintf(buf, "%ld%cB", (long)size, unit);
171         return (buf);
172 }
173
174 int
175 ptblread(void *d, void *buf, size_t blocks, uint64_t offset)
176 {
177         struct disk_devdesc *dev;
178         struct open_disk *od;
179
180         dev = (struct disk_devdesc *)d;
181         od = (struct open_disk *)dev->d_opendata;
182         return (dev->d_dev->dv_strategy(dev, F_READ, offset,
183             blocks * od->sectorsize, (char *)buf, NULL));
184 }
185
186 #define PWIDTH  35
187 static int
188 ptable_print(void *arg, const char *pname, const struct ptable_entry *part)
189 {
190         struct print_args *pa, bsd;
191         struct open_disk *od;
192         struct ptable *table;
193         char line[80];
194         int res;
195
196         pa = (struct print_args *)arg;
197         od = (struct open_disk *)pa->dev->d_opendata;
198         sprintf(line, "  %s%s: %s", pa->prefix, pname,
199             parttype2str(part->type));
200         if (pa->verbose)
201                 sprintf(line, "%-*s%s", PWIDTH, line,
202                     display_size(part->end - part->start + 1,
203                     od->sectorsize));
204         strcat(line, "\n");
205         if (pager_output(line))
206                 return 1;
207         res = 0;
208         if (part->type == PART_FREEBSD) {
209                 /* Open slice with BSD label */
210                 pa->dev->d_offset = part->start;
211                 table = ptable_open(pa->dev, part->end - part->start + 1,
212                     od->sectorsize, ptblread);
213                 if (table == NULL)
214                         return 0;
215                 sprintf(line, "  %s%s", pa->prefix, pname);
216                 bsd.dev = pa->dev;
217                 bsd.prefix = line;
218                 bsd.verbose = pa->verbose;
219                 res = ptable_iterate(table, &bsd, ptable_print);
220                 ptable_close(table);
221         }
222
223         return (res);
224 }
225 #undef PWIDTH
226
227 int
228 disk_print(struct disk_devdesc *dev, char *prefix, int verbose)
229 {
230         struct open_disk *od;
231         struct print_args pa;
232
233         /* Disk should be opened */
234         od = (struct open_disk *)dev->d_opendata;
235         pa.dev = dev;
236         pa.prefix = prefix;
237         pa.verbose = verbose;
238         return (ptable_iterate(od->table, &pa, ptable_print));
239 }
240
241 int
242 disk_read(struct disk_devdesc *dev, void *buf, uint64_t offset, u_int blocks)
243 {
244         struct open_disk *od;
245         int ret;
246
247         od = (struct open_disk *)dev->d_opendata;
248         ret = dev->d_dev->dv_strategy(dev, F_READ, dev->d_offset + offset,
249             blocks * od->sectorsize, buf, NULL);
250
251         return (ret);
252 }
253
254 int
255 disk_write(struct disk_devdesc *dev, void *buf, uint64_t offset, u_int blocks)
256 {
257         struct open_disk *od;
258         int ret;
259
260         od = (struct open_disk *)dev->d_opendata;
261         ret = dev->d_dev->dv_strategy(dev, F_WRITE, dev->d_offset + offset,
262             blocks * od->sectorsize, buf, NULL);
263
264         return (ret);
265 }
266
267 int
268 disk_ioctl(struct disk_devdesc *dev, u_long cmd, void *data)
269 {
270         struct open_disk *od = dev->d_opendata;
271
272         if (od == NULL)
273                 return (ENOTTY);
274
275         switch (cmd) {
276         case DIOCGSECTORSIZE:
277                 *(u_int *)data = od->sectorsize;
278                 break;
279         case DIOCGMEDIASIZE:
280                 if (dev->d_offset == 0)
281                         *(uint64_t *)data = od->mediasize;
282                 else
283                         *(uint64_t *)data = od->entrysize * od->sectorsize;
284                 break;
285         default:
286                 return (ENOTTY);
287         }
288
289         return (0);
290 }
291
292 int
293 disk_open(struct disk_devdesc *dev, uint64_t mediasize, u_int sectorsize,
294     u_int flags)
295 {
296         struct open_disk *od;
297         struct ptable *table;
298         struct ptable_entry part;
299         int rc, slice, partition;
300
301         rc = 0;
302         if ((flags & DISK_F_NOCACHE) == 0) {
303                 rc = disk_lookup(dev);
304                 if (rc == 0)
305                         return (0);
306         }
307         /*
308          * While we are reading disk metadata, make sure we do it relative
309          * to the start of the disk
310          */
311         dev->d_offset = 0;
312         table = NULL;
313         slice = dev->d_slice;
314         partition = dev->d_partition;
315         if (rc == EAGAIN) {
316                 /*
317                  * This entire disk was already opened and there is no
318                  * need to allocate new open_disk structure and open the
319                  * main partition table.
320                  */
321                 od = (struct open_disk *)dev->d_opendata;
322                 DEBUG("%s unit %d, slice %d, partition %d => %p (cached)",
323                     disk_fmtdev(dev), dev->d_unit, dev->d_slice,
324                     dev->d_partition, od);
325                 goto opened;
326         } else {
327                 od = (struct open_disk *)malloc(sizeof(struct open_disk));
328                 if (od == NULL) {
329                         DEBUG("no memory");
330                         return (ENOMEM);
331                 }
332                 dev->d_opendata = od;
333                 od->rcnt = 0;
334                 od->entrysize = 0;
335         }
336         od->mediasize = mediasize;
337         od->sectorsize = sectorsize;
338         od->flags = flags;
339         DEBUG("%s unit %d, slice %d, partition %d => %p",
340             disk_fmtdev(dev), dev->d_unit, dev->d_slice, dev->d_partition, od);
341
342         /* Determine disk layout. */
343         od->table = ptable_open(dev, mediasize / sectorsize, sectorsize,
344             ptblread);
345         if (od->table == NULL) {
346                 DEBUG("Can't read partition table");
347                 rc = ENXIO;
348                 goto out;
349         }
350
351         if (ptable_getsize(od->table, &mediasize) != 0) {
352                 rc = ENXIO;
353                 goto out;
354         }
355         if (mediasize > od->mediasize) {
356                 od->mediasize = mediasize;
357         }
358 opened:
359         rc = 0;
360         if (ptable_gettype(od->table) == PTABLE_BSD &&
361             partition >= 0) {
362                 /* It doesn't matter what value has d_slice */
363                 rc = ptable_getpart(od->table, &part, partition);
364                 if (rc == 0) {
365                         dev->d_offset = part.start;
366                         od->entrysize = part.end - part.start + 1;
367                 }
368         } else if (slice >= 0) {
369                 /* Try to get information about partition */
370                 if (slice == 0)
371                         rc = ptable_getbestpart(od->table, &part);
372                 else
373                         rc = ptable_getpart(od->table, &part, slice);
374                 if (rc != 0) /* Partition doesn't exist */
375                         goto out;
376                 dev->d_offset = part.start;
377                 od->entrysize = part.end - part.start + 1;
378                 slice = part.index;
379                 if (ptable_gettype(od->table) == PTABLE_GPT) {
380                         partition = 255;
381                         goto out; /* Nothing more to do */
382                 } else if (partition == 255) {
383                         /*
384                          * When we try to open GPT partition, but partition
385                          * table isn't GPT, reset d_partition value to -1
386                          * and try to autodetect appropriate value.
387                          */
388                         partition = -1;
389                 }
390                 /*
391                  * If d_partition < 0 and we are looking at a BSD slice,
392                  * then try to read BSD label, otherwise return the
393                  * whole MBR slice.
394                  */
395                 if (partition == -1 &&
396                     part.type != PART_FREEBSD)
397                         goto out;
398                 /* Try to read BSD label */
399                 table = ptable_open(dev, part.end - part.start + 1,
400                     od->sectorsize, ptblread);
401                 if (table == NULL) {
402                         DEBUG("Can't read BSD label");
403                         rc = ENXIO;
404                         goto out;
405                 }
406                 /*
407                  * If slice contains BSD label and d_partition < 0, then
408                  * assume the 'a' partition. Otherwise just return the
409                  * whole MBR slice, because it can contain ZFS.
410                  */
411                 if (partition < 0) {
412                         if (ptable_gettype(table) != PTABLE_BSD)
413                                 goto out;
414                         partition = 0;
415                 }
416                 rc = ptable_getpart(table, &part, partition);
417                 if (rc != 0)
418                         goto out;
419                 dev->d_offset += part.start;
420                 od->entrysize = part.end - part.start + 1;
421         }
422 out:
423         if (table != NULL)
424                 ptable_close(table);
425
426         if (rc != 0) {
427                 if (od->rcnt < 1) {
428                         if (od->table != NULL)
429                                 ptable_close(od->table);
430                         free(od);
431                 }
432                 DEBUG("%s could not open", disk_fmtdev(dev));
433         } else {
434                 if ((flags & DISK_F_NOCACHE) == 0)
435                         disk_insert(dev);
436                 /* Save the slice and partition number to the dev */
437                 dev->d_slice = slice;
438                 dev->d_partition = partition;
439                 DEBUG("%s offset %lld => %p", disk_fmtdev(dev),
440                     (long long)dev->d_offset, od);
441         }
442         return (rc);
443 }
444
445 int
446 disk_close(struct disk_devdesc *dev)
447 {
448         struct open_disk *od;
449
450         od = (struct open_disk *)dev->d_opendata;
451         DEBUG("%s closed => %p [%d]", disk_fmtdev(dev), od, od->rcnt);
452         if (od->flags & DISK_F_NOCACHE) {
453                 ptable_close(od->table);
454                 free(od);
455         }
456         return (0);
457 }
458
459 void
460 disk_cleanup(const struct devsw *d_dev)
461 {
462 #ifdef DISK_DEBUG
463         struct disk_devdesc dev;
464 #endif
465         struct dentry *entry, *tmp;
466
467         STAILQ_FOREACH_SAFE(entry, &opened_disks, entry, tmp) {
468                 if (entry->d_dev != d_dev)
469                         continue;
470                 entry->od->rcnt--;
471 #ifdef DISK_DEBUG
472                 dev.d_dev = (struct devsw *)entry->d_dev;
473                 dev.d_unit = entry->d_unit;
474                 dev.d_slice = entry->d_slice;
475                 dev.d_partition = entry->d_partition;
476                 DEBUG("%s was freed => %p [%d]", disk_fmtdev(&dev),
477                     entry->od, entry->od->rcnt);
478 #endif
479                 STAILQ_REMOVE(&opened_disks, entry, dentry, entry);
480                 if (entry->od->rcnt < 1) {
481                         if (entry->od->table != NULL)
482                                 ptable_close(entry->od->table);
483                         free(entry->od);
484                 }
485                 free(entry);
486         }
487 }
488
489 char*
490 disk_fmtdev(struct disk_devdesc *dev)
491 {
492         static char buf[128];
493         char *cp;
494
495         cp = buf + sprintf(buf, "%s%d", dev->d_dev->dv_name, dev->d_unit);
496         if (dev->d_slice >= 0) {
497 #ifdef LOADER_GPT_SUPPORT
498                 if (dev->d_partition == 255) {
499                         sprintf(cp, "p%d:", dev->d_slice);
500                         return (buf);
501                 } else
502 #endif
503 #ifdef LOADER_MBR_SUPPORT
504                         cp += sprintf(cp, "s%d", dev->d_slice);
505 #endif
506         }
507         if (dev->d_partition >= 0)
508                 cp += sprintf(cp, "%c", dev->d_partition + 'a');
509         strcat(cp, ":");
510         return (buf);
511 }
512
513 int
514 disk_parsedev(struct disk_devdesc *dev, const char *devspec, const char **path)
515 {
516         int unit, slice, partition;
517         const char *np;
518         char *cp;
519
520         np = devspec;
521         unit = slice = partition = -1;
522         if (*np != '\0' && *np != ':') {
523                 unit = strtol(np, &cp, 10);
524                 if (cp == np)
525                         return (EUNIT);
526 #ifdef LOADER_GPT_SUPPORT
527                 if (*cp == 'p') {
528                         np = cp + 1;
529                         slice = strtol(np, &cp, 10);
530                         if (np == cp)
531                                 return (ESLICE);
532                         /* we don't support nested partitions on GPT */
533                         if (*cp != '\0' && *cp != ':')
534                                 return (EINVAL);
535                         partition = 255;
536                 } else
537 #endif
538 #ifdef LOADER_MBR_SUPPORT
539                 if (*cp == 's') {
540                         np = cp + 1;
541                         slice = strtol(np, &cp, 10);
542                         if (np == cp)
543                                 return (ESLICE);
544                 }
545 #endif
546                 if (*cp != '\0' && *cp != ':') {
547                         partition = *cp - 'a';
548                         if (partition < 0)
549                                 return (EPART);
550                         cp++;
551                 }
552         } else
553                 return (EINVAL);
554
555         if (*cp != '\0' && *cp != ':')
556                 return (EINVAL);
557         dev->d_unit = unit;
558         dev->d_slice = slice;
559         dev->d_partition = partition;
560         if (path != NULL)
561                 *path = (*cp == '\0') ? cp: cp + 1;
562         return (0);
563 }