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