2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3 * Copyright (c) 2012 Andrey V. Elsukov <ae@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
32 #include <sys/queue.h>
35 #include <bootstrap.h>
41 # define DEBUG(fmt, args...) printf("%s: " fmt "\n" , __func__ , ## args)
43 # define DEBUG(fmt, args...)
56 struct disk_devdesc *dev;
62 const struct devsw *d_dev;
69 STAILQ_ENTRY(dentry) entry;
75 static STAILQ_HEAD(, dentry) opened_disks =
76 STAILQ_HEAD_INITIALIZER(opened_disks);
79 disk_lookup(struct disk_devdesc *dev)
85 STAILQ_FOREACH(entry, &opened_disks, entry) {
86 if (entry->d_dev != dev->d_dev ||
87 entry->d_unit != dev->d_unit)
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);
106 disk_insert(struct disk_devdesc *dev)
108 struct dentry *entry;
110 entry = (struct dentry *)malloc(sizeof(struct dentry));
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;
121 entry->d_offset = dev->d_offset;
125 STAILQ_INSERT_TAIL(&opened_disks, entry, entry);
126 DEBUG("%s cached", disk_fmtdev(dev));
130 COMMAND_SET(dcachestat, "dcachestat", "get disk cache stats",
134 command_dcachestat(int argc, char *argv[])
136 struct disk_devdesc dev;
137 struct dentry *entry;
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);
149 #endif /* DISK_DEBUG */
151 /* Convert size to a human-readable number. */
153 display_size(uint64_t size, u_int sectorsize)
158 size = size * sectorsize / 1024;
160 if (size >= 10485760000LL) {
163 } else if (size >= 10240000) {
166 } else if (size >= 10000) {
170 sprintf(buf, "%ld%cB", (long)size, unit);
175 ptblread(void *d, void *buf, size_t blocks, uint64_t offset)
177 struct disk_devdesc *dev;
178 struct open_disk *od;
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));
188 ptable_print(void *arg, const char *pname, const struct ptable_entry *part)
190 struct print_args *pa, bsd;
191 struct open_disk *od;
192 struct ptable *table;
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));
201 sprintf(line, "%-*s%s", PWIDTH, line,
202 display_size(part->end - part->start + 1,
205 if (pager_output(line))
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);
215 sprintf(line, " %s%s", pa->prefix, pname);
218 bsd.verbose = pa->verbose;
219 res = ptable_iterate(table, &bsd, ptable_print);
228 disk_print(struct disk_devdesc *dev, char *prefix, int verbose)
230 struct open_disk *od;
231 struct print_args pa;
233 /* Disk should be opened */
234 od = (struct open_disk *)dev->d_opendata;
237 pa.verbose = verbose;
238 return (ptable_iterate(od->table, &pa, ptable_print));
242 disk_read(struct disk_devdesc *dev, void *buf, uint64_t offset, u_int blocks)
244 struct open_disk *od;
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);
255 disk_write(struct disk_devdesc *dev, void *buf, uint64_t offset, u_int blocks)
257 struct open_disk *od;
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);
268 disk_ioctl(struct disk_devdesc *dev, u_long cmd, void *data)
270 struct open_disk *od = dev->d_opendata;
276 case DIOCGSECTORSIZE:
277 *(u_int *)data = od->sectorsize;
280 if (dev->d_offset == 0)
281 *(uint64_t *)data = od->mediasize;
283 *(uint64_t *)data = od->entrysize * od->sectorsize;
293 disk_open(struct disk_devdesc *dev, uint64_t mediasize, u_int sectorsize,
296 struct open_disk *od;
297 struct ptable *table;
298 struct ptable_entry part;
299 int rc, slice, partition;
302 if ((flags & DISK_F_NOCACHE) == 0) {
303 rc = disk_lookup(dev);
308 * While we are reading disk metadata, make sure we do it relative
309 * to the start of the disk
313 slice = dev->d_slice;
314 partition = dev->d_partition;
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.
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);
327 od = (struct open_disk *)malloc(sizeof(struct open_disk));
332 dev->d_opendata = od;
336 od->mediasize = mediasize;
337 od->sectorsize = sectorsize;
339 DEBUG("%s unit %d, slice %d, partition %d => %p",
340 disk_fmtdev(dev), dev->d_unit, dev->d_slice, dev->d_partition, od);
342 /* Determine disk layout. */
343 od->table = ptable_open(dev, mediasize / sectorsize, sectorsize,
345 if (od->table == NULL) {
346 DEBUG("Can't read partition table");
351 if (ptable_getsize(od->table, &mediasize) != 0) {
355 if (mediasize > od->mediasize) {
356 od->mediasize = mediasize;
360 if (ptable_gettype(od->table) == PTABLE_BSD &&
362 /* It doesn't matter what value has d_slice */
363 rc = ptable_getpart(od->table, &part, partition);
365 dev->d_offset = part.start;
366 od->entrysize = part.end - part.start + 1;
368 } else if (slice >= 0) {
369 /* Try to get information about partition */
371 rc = ptable_getbestpart(od->table, &part);
373 rc = ptable_getpart(od->table, &part, slice);
374 if (rc != 0) /* Partition doesn't exist */
376 dev->d_offset = part.start;
377 od->entrysize = part.end - part.start + 1;
379 if (ptable_gettype(od->table) == PTABLE_GPT) {
381 goto out; /* Nothing more to do */
382 } else if (partition == 255) {
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.
391 * If d_partition < 0 and we are looking at a BSD slice,
392 * then try to read BSD label, otherwise return the
395 if (partition == -1 &&
396 part.type != PART_FREEBSD)
398 /* Try to read BSD label */
399 table = ptable_open(dev, part.end - part.start + 1,
400 od->sectorsize, ptblread);
402 DEBUG("Can't read BSD label");
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.
412 if (ptable_gettype(table) != PTABLE_BSD)
416 rc = ptable_getpart(table, &part, partition);
419 dev->d_offset += part.start;
420 od->entrysize = part.end - part.start + 1;
428 if (od->table != NULL)
429 ptable_close(od->table);
432 DEBUG("%s could not open", disk_fmtdev(dev));
434 if ((flags & DISK_F_NOCACHE) == 0)
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);
446 disk_close(struct disk_devdesc *dev)
448 struct open_disk *od;
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);
460 disk_cleanup(const struct devsw *d_dev)
463 struct disk_devdesc dev;
465 struct dentry *entry, *tmp;
467 STAILQ_FOREACH_SAFE(entry, &opened_disks, entry, tmp) {
468 if (entry->d_dev != d_dev)
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);
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);
490 disk_fmtdev(struct disk_devdesc *dev)
492 static char buf[128];
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);
503 #ifdef LOADER_MBR_SUPPORT
504 cp += sprintf(cp, "s%d", dev->d_slice);
507 if (dev->d_partition >= 0)
508 cp += sprintf(cp, "%c", dev->d_partition + 'a');
514 disk_parsedev(struct disk_devdesc *dev, const char *devspec, const char **path)
516 int unit, slice, partition;
521 unit = slice = partition = -1;
522 if (*np != '\0' && *np != ':') {
523 unit = strtol(np, &cp, 10);
526 #ifdef LOADER_GPT_SUPPORT
529 slice = strtol(np, &cp, 10);
532 /* we don't support nested partitions on GPT */
533 if (*cp != '\0' && *cp != ':')
538 #ifdef LOADER_MBR_SUPPORT
541 slice = strtol(np, &cp, 10);
546 if (*cp != '\0' && *cp != ':') {
547 partition = *cp - 'a';
555 if (*cp != '\0' && *cp != ':')
558 dev->d_slice = slice;
559 dev->d_partition = partition;
561 *path = (*cp == '\0') ? cp: cp + 1;