199767f8919635c4928607450d9e0abb932109ceToomas Soome/*-
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 2012 Andrey V. Elsukov <ae@FreeBSD.org>
199767f8919635c4928607450d9e0abb932109ceToomas Soome * All rights reserved.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Redistribution and use in source and binary forms, with or without
199767f8919635c4928607450d9e0abb932109ceToomas Soome * modification, are permitted provided that the following conditions
199767f8919635c4928607450d9e0abb932109ceToomas Soome * are met:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 1. Redistributions of source code must retain the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer in the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * documentation and/or other materials provided with the distribution.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
199767f8919635c4928607450d9e0abb932109ceToomas Soome * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
199767f8919635c4928607450d9e0abb932109ceToomas Soome * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
199767f8919635c4928607450d9e0abb932109ceToomas Soome * SUCH DAMAGE.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/cdefs.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome__FBSDID("$FreeBSD$");
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/disk.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/queue.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <stand.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <stdarg.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <bootstrap.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <part.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "disk.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef DISK_DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome# define DEBUG(fmt, args...) printf("%s: " fmt "\n" , __func__ , ## args)
199767f8919635c4928607450d9e0abb932109ceToomas Soome#else
199767f8919635c4928607450d9e0abb932109ceToomas Soome# define DEBUG(fmt, args...)
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct open_disk {
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct ptable *table;
199767f8919635c4928607450d9e0abb932109ceToomas Soome off_t mediasize;
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int sectorsize;
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int flags;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int rcnt;
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct print_args {
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct disk_devdesc *dev;
199767f8919635c4928607450d9e0abb932109ceToomas Soome const char *prefix;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int verbose;
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct dentry {
199767f8919635c4928607450d9e0abb932109ceToomas Soome const struct devsw *d_dev;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int d_unit;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int d_slice;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int d_partition;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct open_disk *od;
199767f8919635c4928607450d9e0abb932109ceToomas Soome off_t d_offset;
199767f8919635c4928607450d9e0abb932109ceToomas Soome STAILQ_ENTRY(dentry) entry;
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef DISK_DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t count;
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic STAILQ_HEAD(, dentry) opened_disks =
199767f8919635c4928607450d9e0abb932109ceToomas Soome STAILQ_HEAD_INITIALIZER(opened_disks);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomedisk_lookup(struct disk_devdesc *dev)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct dentry *entry;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int rc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome rc = ENOENT;
199767f8919635c4928607450d9e0abb932109ceToomas Soome STAILQ_FOREACH(entry, &opened_disks, entry) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (entry->d_dev != dev->d_dev ||
199767f8919635c4928607450d9e0abb932109ceToomas Soome entry->d_unit != dev->d_unit)
199767f8919635c4928607450d9e0abb932109ceToomas Soome continue;
199767f8919635c4928607450d9e0abb932109ceToomas Soome dev->d_opendata = entry->od;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (entry->d_slice == dev->d_slice &&
199767f8919635c4928607450d9e0abb932109ceToomas Soome entry->d_partition == dev->d_partition) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome dev->d_offset = entry->d_offset;
199767f8919635c4928607450d9e0abb932109ceToomas Soome DEBUG("%s offset %lld", disk_fmtdev(dev),
199767f8919635c4928607450d9e0abb932109ceToomas Soome (long long)dev->d_offset);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef DISK_DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome entry->count++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome rc = EAGAIN;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (rc);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void
199767f8919635c4928607450d9e0abb932109ceToomas Soomedisk_insert(struct disk_devdesc *dev)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct dentry *entry;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome entry = (struct dentry *)malloc(sizeof(struct dentry));
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (entry == NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome DEBUG("no memory");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome entry->d_dev = dev->d_dev;
199767f8919635c4928607450d9e0abb932109ceToomas Soome entry->d_unit = dev->d_unit;
199767f8919635c4928607450d9e0abb932109ceToomas Soome entry->d_slice = dev->d_slice;
199767f8919635c4928607450d9e0abb932109ceToomas Soome entry->d_partition = dev->d_partition;
199767f8919635c4928607450d9e0abb932109ceToomas Soome entry->od = (struct open_disk *)dev->d_opendata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome entry->od->rcnt++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome entry->d_offset = dev->d_offset;
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef DISK_DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome entry->count = 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome STAILQ_INSERT_TAIL(&opened_disks, entry, entry);
199767f8919635c4928607450d9e0abb932109ceToomas Soome DEBUG("%s cached", disk_fmtdev(dev));
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef DISK_DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas SoomeCOMMAND_SET(dcachestat, "dcachestat", "get disk cache stats",
199767f8919635c4928607450d9e0abb932109ceToomas Soome command_dcachestat);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomecommand_dcachestat(int argc, char *argv[])
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct disk_devdesc dev;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct dentry *entry;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome STAILQ_FOREACH(entry, &opened_disks, entry) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome dev.d_dev = (struct devsw *)entry->d_dev;
199767f8919635c4928607450d9e0abb932109ceToomas Soome dev.d_unit = entry->d_unit;
199767f8919635c4928607450d9e0abb932109ceToomas Soome dev.d_slice = entry->d_slice;
199767f8919635c4928607450d9e0abb932109ceToomas Soome dev.d_partition = entry->d_partition;
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("%s %d => %p [%d]\n", disk_fmtdev(&dev), entry->count,
199767f8919635c4928607450d9e0abb932109ceToomas Soome entry->od, entry->od->rcnt);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_OK);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif /* DISK_DEBUG */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Convert size to a human-readable number. */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic char *
199767f8919635c4928607450d9e0abb932109ceToomas Soomedisplay_size(uint64_t size, u_int sectorsize)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome static char buf[80];
199767f8919635c4928607450d9e0abb932109ceToomas Soome char unit;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome size = size * sectorsize / 1024;
199767f8919635c4928607450d9e0abb932109ceToomas Soome unit = 'K';
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (size >= 10485760000LL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome size /= 1073741824;
199767f8919635c4928607450d9e0abb932109ceToomas Soome unit = 'T';
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else if (size >= 10240000) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome size /= 1048576;
199767f8919635c4928607450d9e0abb932109ceToomas Soome unit = 'G';
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else if (size >= 10000) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome size /= 1024;
199767f8919635c4928607450d9e0abb932109ceToomas Soome unit = 'M';
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(buf, "%ld%cB", (long)size, unit);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (buf);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomeptblread(void *d, void *buf, size_t blocks, off_t offset)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct disk_devdesc *dev;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct open_disk *od;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome dev = (struct disk_devdesc *)d;
199767f8919635c4928607450d9e0abb932109ceToomas Soome od = (struct open_disk *)dev->d_opendata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (dev->d_dev->dv_strategy(dev, F_READ, offset, 0,
199767f8919635c4928607450d9e0abb932109ceToomas Soome blocks * od->sectorsize, (char *)buf, NULL));
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define PWIDTH 35
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomeptable_print(void *arg, const char *pname, const struct ptable_entry *part)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct print_args *pa, bsd;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct open_disk *od;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct ptable *table;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char line[80];
199767f8919635c4928607450d9e0abb932109ceToomas Soome int ret = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome pa = (struct print_args *)arg;
199767f8919635c4928607450d9e0abb932109ceToomas Soome od = (struct open_disk *)pa->dev->d_opendata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(line, " %s%s: %s", pa->prefix, pname,
199767f8919635c4928607450d9e0abb932109ceToomas Soome parttype2str(part->type));
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (pa->verbose)
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(line, "%-*s%s", PWIDTH, line,
199767f8919635c4928607450d9e0abb932109ceToomas Soome display_size(part->end - part->start + 1,
199767f8919635c4928607450d9e0abb932109ceToomas Soome od->sectorsize));
199767f8919635c4928607450d9e0abb932109ceToomas Soome strcat(line, "\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome ret = pager_output(line);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ret != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ret);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (part->type == PART_FREEBSD || part->type == PART_SOLARIS2) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Open slice with BSD or VTOC label */
199767f8919635c4928607450d9e0abb932109ceToomas Soome pa->dev->d_offset = part->start;
199767f8919635c4928607450d9e0abb932109ceToomas Soome table = ptable_open(pa->dev, part->end - part->start + 1,
199767f8919635c4928607450d9e0abb932109ceToomas Soome od->sectorsize, ptblread);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (table == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ret);
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(line, " %s%s", pa->prefix, pname);
199767f8919635c4928607450d9e0abb932109ceToomas Soome bsd.dev = pa->dev;
199767f8919635c4928607450d9e0abb932109ceToomas Soome bsd.prefix = line;
199767f8919635c4928607450d9e0abb932109ceToomas Soome bsd.verbose = pa->verbose;
199767f8919635c4928607450d9e0abb932109ceToomas Soome ret = ptable_iterate(table, &bsd, ptable_print);
199767f8919635c4928607450d9e0abb932109ceToomas Soome ptable_close(table);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ret);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome#undef PWIDTH
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soomedisk_print(struct disk_devdesc *dev, char *prefix, int verbose)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct open_disk *od;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct print_args pa;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Disk should be opened */
199767f8919635c4928607450d9e0abb932109ceToomas Soome od = (struct open_disk *)dev->d_opendata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome pa.dev = dev;
199767f8919635c4928607450d9e0abb932109ceToomas Soome pa.prefix = prefix;
199767f8919635c4928607450d9e0abb932109ceToomas Soome pa.verbose = verbose;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ptable_iterate(od->table, &pa, ptable_print));
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soomedisk_read(struct disk_devdesc *dev, void *buf, off_t offset, u_int blocks)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct open_disk *od;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int ret;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome od = (struct open_disk *)dev->d_opendata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome ret = dev->d_dev->dv_strategy(dev, F_READ, dev->d_offset + offset, 0,
199767f8919635c4928607450d9e0abb932109ceToomas Soome blocks * od->sectorsize, buf, NULL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ret);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soomedisk_write(struct disk_devdesc *dev, void *buf, off_t offset, u_int blocks)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct open_disk *od;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int ret;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome od = (struct open_disk *)dev->d_opendata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome ret = dev->d_dev->dv_strategy(dev, F_WRITE, dev->d_offset + offset, 0,
199767f8919635c4928607450d9e0abb932109ceToomas Soome blocks * od->sectorsize, buf, NULL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ret);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soomedisk_ioctl(struct disk_devdesc *dev, u_long cmd, void *buf)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (dev->d_dev->dv_ioctl)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return ((*dev->d_dev->dv_ioctl)(dev->d_opendata, cmd, buf));
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ENXIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soomedisk_open(struct disk_devdesc *dev, off_t mediasize, u_int sectorsize,
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int flags)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct open_disk *od;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct ptable *table;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct ptable_entry part;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int rc, slice, partition;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome rc = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((flags & DISK_F_NOCACHE) == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome rc = disk_lookup(dev);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rc == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * While we are reading disk metadata, make sure we do it relative
199767f8919635c4928607450d9e0abb932109ceToomas Soome * to the start of the disk
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome dev->d_offset = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome table = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome slice = dev->d_slice;
199767f8919635c4928607450d9e0abb932109ceToomas Soome partition = dev->d_partition;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rc == EAGAIN) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * This entire disk was already opened and there is no
199767f8919635c4928607450d9e0abb932109ceToomas Soome * need to allocate new open_disk structure and open the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * main partition table.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome od = (struct open_disk *)dev->d_opendata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome DEBUG("%s unit %d, slice %d, partition %d => %p (cached)",
199767f8919635c4928607450d9e0abb932109ceToomas Soome disk_fmtdev(dev), dev->d_unit, dev->d_slice,
199767f8919635c4928607450d9e0abb932109ceToomas Soome dev->d_partition, od);
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto opened;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome od = (struct open_disk *)malloc(sizeof(struct open_disk));
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (od == NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome DEBUG("no memory");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ENOMEM);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome dev->d_opendata = od;
199767f8919635c4928607450d9e0abb932109ceToomas Soome od->rcnt = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome od->mediasize = mediasize;
199767f8919635c4928607450d9e0abb932109ceToomas Soome od->sectorsize = sectorsize;
199767f8919635c4928607450d9e0abb932109ceToomas Soome od->flags = flags;
199767f8919635c4928607450d9e0abb932109ceToomas Soome DEBUG("%s unit %d, slice %d, partition %d => %p",
199767f8919635c4928607450d9e0abb932109ceToomas Soome disk_fmtdev(dev), dev->d_unit, dev->d_slice, dev->d_partition, od);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Determine disk layout. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome od->table = ptable_open(dev, mediasize / sectorsize, sectorsize,
199767f8919635c4928607450d9e0abb932109ceToomas Soome ptblread);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (od->table == NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome DEBUG("Can't read partition table");
199767f8919635c4928607450d9e0abb932109ceToomas Soome rc = ENXIO;
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soomeopened:
199767f8919635c4928607450d9e0abb932109ceToomas Soome rc = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ptable_gettype(od->table) == PTABLE_BSD &&
199767f8919635c4928607450d9e0abb932109ceToomas Soome partition >= 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* It doesn't matter what value has d_slice */
199767f8919635c4928607450d9e0abb932109ceToomas Soome rc = ptable_getpart(od->table, &part, partition);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rc == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome dev->d_offset = part.start;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else if (slice >= 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Try to get information about partition */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (slice == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome rc = ptable_getbestpart(od->table, &part);
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome rc = ptable_getpart(od->table, &part, slice);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rc != 0) /* Partition doesn't exist */
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome dev->d_offset = part.start;
199767f8919635c4928607450d9e0abb932109ceToomas Soome slice = part.index;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ptable_gettype(od->table) == PTABLE_GPT) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome partition = 255;
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out; /* Nothing more to do */
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else if (partition == 255) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * When we try to open GPT partition, but partition
199767f8919635c4928607450d9e0abb932109ceToomas Soome * table isn't GPT, reset d_partition value to -1
199767f8919635c4928607450d9e0abb932109ceToomas Soome * and try to autodetect appropriate value.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome partition = -1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * If d_partition < 0 and we are looking at a BSD/VTOC slice,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * then try to read label, otherwise return the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * whole MBR slice.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (partition == -1 &&
199767f8919635c4928607450d9e0abb932109ceToomas Soome (part.type != PART_FREEBSD || part.type != PART_SOLARIS2))
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Try to read label */
199767f8919635c4928607450d9e0abb932109ceToomas Soome table = ptable_open(dev, part.end - part.start + 1,
199767f8919635c4928607450d9e0abb932109ceToomas Soome od->sectorsize, ptblread);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (table == NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome DEBUG("Can't read BSD/VTOC label");
199767f8919635c4928607450d9e0abb932109ceToomas Soome rc = ENXIO;
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * If slice contains BSD/VTOC label and d_partition < 0, then
199767f8919635c4928607450d9e0abb932109ceToomas Soome * assume the 'a' partition. Otherwise just return the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * whole MBR slice, because it can contain ZFS.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (partition < 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ptable_gettype(table) != PTABLE_BSD ||
199767f8919635c4928607450d9e0abb932109ceToomas Soome ptable_gettype(table) != PTABLE_VTOC)
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome partition = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome rc = ptable_getpart(table, &part, partition);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rc != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome dev->d_offset += part.start;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soomeout:
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (table != NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome ptable_close(table);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rc != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (od->rcnt < 1) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (od->table != NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome ptable_close(od->table);
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(od);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome DEBUG("%s could not open", disk_fmtdev(dev));
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((flags & DISK_F_NOCACHE) == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome disk_insert(dev);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Save the slice and partition number to the dev */
199767f8919635c4928607450d9e0abb932109ceToomas Soome dev->d_slice = slice;
199767f8919635c4928607450d9e0abb932109ceToomas Soome dev->d_partition = partition;
199767f8919635c4928607450d9e0abb932109ceToomas Soome DEBUG("%s offset %lld => %p", disk_fmtdev(dev),
199767f8919635c4928607450d9e0abb932109ceToomas Soome (long long)dev->d_offset, od);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (rc);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soomedisk_close(struct disk_devdesc *dev)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct open_disk *od;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome od = (struct open_disk *)dev->d_opendata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome DEBUG("%s closed => %p [%d]", disk_fmtdev(dev), od, od->rcnt);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (od->flags & DISK_F_NOCACHE) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome ptable_close(od->table);
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(od);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid
199767f8919635c4928607450d9e0abb932109ceToomas Soomedisk_cleanup(const struct devsw *d_dev)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef DISK_DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct disk_devdesc dev;
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct dentry *entry, *tmp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome STAILQ_FOREACH_SAFE(entry, &opened_disks, entry, tmp) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (entry->d_dev != d_dev)
199767f8919635c4928607450d9e0abb932109ceToomas Soome continue;
199767f8919635c4928607450d9e0abb932109ceToomas Soome entry->od->rcnt--;
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef DISK_DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome dev.d_dev = (struct devsw *)entry->d_dev;
199767f8919635c4928607450d9e0abb932109ceToomas Soome dev.d_unit = entry->d_unit;
199767f8919635c4928607450d9e0abb932109ceToomas Soome dev.d_slice = entry->d_slice;
199767f8919635c4928607450d9e0abb932109ceToomas Soome dev.d_partition = entry->d_partition;
199767f8919635c4928607450d9e0abb932109ceToomas Soome DEBUG("%s was freed => %p [%d]", disk_fmtdev(&dev),
199767f8919635c4928607450d9e0abb932109ceToomas Soome entry->od, entry->od->rcnt);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome STAILQ_REMOVE(&opened_disks, entry, dentry, entry);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (entry->od->rcnt < 1) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (entry->od->table != NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome ptable_close(entry->od->table);
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(entry->od);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(entry);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomechar*
199767f8919635c4928607450d9e0abb932109ceToomas Soomedisk_fmtdev(struct disk_devdesc *dev)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome static char buf[128];
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *cp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome cp = buf + sprintf(buf, "%s%d", dev->d_dev->dv_name, dev->d_unit);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (dev->d_slice >= 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef LOADER_GPT_SUPPORT
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (dev->d_partition == 255) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(cp, "p%d:", dev->d_slice);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (buf);
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef LOADER_MBR_SUPPORT
199767f8919635c4928607450d9e0abb932109ceToomas Soome cp += sprintf(cp, "s%d", dev->d_slice);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (dev->d_partition >= 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome cp += sprintf(cp, "%c", dev->d_partition + 'a');
199767f8919635c4928607450d9e0abb932109ceToomas Soome strcat(cp, ":");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (buf);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soomedisk_parsedev(struct disk_devdesc *dev, const char *devspec, const char **path)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int unit, slice, partition;
199767f8919635c4928607450d9e0abb932109ceToomas Soome const char *np;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *cp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome np = devspec;
199767f8919635c4928607450d9e0abb932109ceToomas Soome unit = slice = partition = -1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (*np != '\0' && *np != ':') {
199767f8919635c4928607450d9e0abb932109ceToomas Soome unit = strtol(np, &cp, 10);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (cp == np)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EUNIT);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef LOADER_GPT_SUPPORT
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (*cp == 'p') {
199767f8919635c4928607450d9e0abb932109ceToomas Soome np = cp + 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome slice = strtol(np, &cp, 10);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (np == cp)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ESLICE);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* we don't support nested partitions on GPT */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (*cp != '\0' && *cp != ':')
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EINVAL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome partition = 255;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef LOADER_MBR_SUPPORT
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (*cp == 's') {
199767f8919635c4928607450d9e0abb932109ceToomas Soome np = cp + 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome slice = strtol(np, &cp, 10);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (np == cp)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ESLICE);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (*cp != '\0' && *cp != ':') {
199767f8919635c4928607450d9e0abb932109ceToomas Soome partition = *cp - 'a';
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (partition < 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EPART);
199767f8919635c4928607450d9e0abb932109ceToomas Soome cp++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EINVAL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (*cp != '\0' && *cp != ':')
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EINVAL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome dev->d_unit = unit;
199767f8919635c4928607450d9e0abb932109ceToomas Soome dev->d_slice = slice;
199767f8919635c4928607450d9e0abb932109ceToomas Soome dev->d_partition = partition;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (path != NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome *path = (*cp == '\0') ? cp: cp + 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}