1/*
2 * Copyright (c) 2007 Doug Rabson
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28
29/*
30 *	Stand-alone file reading package.
31 */
32
33#include <stand.h>
34#include <sys/disk.h>
35#include <sys/param.h>
36#include <sys/time.h>
37#include <sys/queue.h>
38#include <disk.h>
39#include <part.h>
40#include <stddef.h>
41#include <stdarg.h>
42#include <string.h>
43#include <bootstrap.h>
44#include <inttypes.h>
45
46#include "libzfs.h"
47
48#include "zfsimpl.c"
49
50/* Define the range of indexes to be populated with ZFS Boot Environments */
51#define		ZFS_BE_FIRST	4
52#define		ZFS_BE_LAST	8
53
54static int	zfs_open(const char *, struct open_file *);
55static int	zfs_close(struct open_file *);
56static int	zfs_read(struct open_file *, void *, size_t, size_t *);
57static off_t	zfs_seek(struct open_file *, off_t, int);
58static int	zfs_stat(struct open_file *, struct stat *);
59static int	zfs_readdir(struct open_file *, struct dirent *);
60
61struct devsw zfs_dev;
62
63struct fs_ops zfs_fsops = {
64	"zfs",
65	zfs_open,
66	zfs_close,
67	zfs_read,
68	null_write,
69	zfs_seek,
70	zfs_stat,
71	zfs_readdir
72};
73
74/*
75 * In-core open file.
76 */
77struct file {
78	off_t		f_seekp;	/* seek pointer */
79	dnode_phys_t	f_dnode;
80	uint64_t	f_zap_type;	/* zap type for readdir */
81	uint64_t	f_num_leafs;	/* number of fzap leaf blocks */
82	zap_leaf_phys_t	*f_zap_leaf;	/* zap leaf buffer */
83};
84
85SLIST_HEAD(zfs_be_list, zfs_be_entry) zfs_be_head =
86    SLIST_HEAD_INITIALIZER(zfs_be_head);
87struct zfs_be_list *zfs_be_headp;
88struct zfs_be_entry {
89	const char *name;
90	SLIST_ENTRY(zfs_be_entry) entries;
91} *zfs_be, *zfs_be_tmp;
92
93/*
94 * Open a file.
95 */
96static int
97zfs_open(const char *upath, struct open_file *f)
98{
99	struct zfsmount *mount = (struct zfsmount *)f->f_devdata;
100	struct file *fp;
101	int rc;
102
103	if (f->f_dev != &zfs_dev)
104		return (EINVAL);
105
106	/* allocate file system specific data structure */
107	fp = calloc(1, sizeof (struct file));
108	if (fp == NULL)
109		return (ENOMEM);
110	f->f_fsdata = fp;
111
112	rc = zfs_lookup(mount, upath, &fp->f_dnode);
113	fp->f_seekp = 0;
114	if (rc) {
115		f->f_fsdata = NULL;
116		free(fp);
117	}
118	return (rc);
119}
120
121static int
122zfs_close(struct open_file *f)
123{
124	struct file *fp = (struct file *)f->f_fsdata;
125
126	dnode_cache_obj = 0;
127	f->f_fsdata = NULL;
128
129	free(fp);
130	return (0);
131}
132
133/*
134 * Copy a portion of a file into kernel memory.
135 * Cross block boundaries when necessary.
136 */
137static int
138zfs_read(struct open_file *f, void *start, size_t size, size_t *resid)
139{
140	const spa_t *spa = ((struct zfsmount *)f->f_devdata)->spa;
141	struct file *fp = (struct file *)f->f_fsdata;
142	struct stat sb;
143	size_t n;
144	int rc;
145
146	rc = zfs_stat(f, &sb);
147	if (rc)
148		return (rc);
149	n = size;
150	if (fp->f_seekp + n > sb.st_size)
151		n = sb.st_size - fp->f_seekp;
152
153	rc = dnode_read(spa, &fp->f_dnode, fp->f_seekp, start, n);
154	if (rc)
155		return (rc);
156
157	fp->f_seekp += n;
158	if (resid)
159		*resid = size - n;
160
161	return (0);
162}
163
164static off_t
165zfs_seek(struct open_file *f, off_t offset, int where)
166{
167	struct file *fp = (struct file *)f->f_fsdata;
168	struct stat sb;
169	int error;
170
171
172	switch (where) {
173	case SEEK_SET:
174		fp->f_seekp = offset;
175		break;
176	case SEEK_CUR:
177		fp->f_seekp += offset;
178		break;
179	case SEEK_END:
180		error = zfs_stat(f, &sb);
181		if (error != 0) {
182			errno = error;
183			return (-1);
184		}
185		fp->f_seekp = sb.st_size - offset;
186		break;
187	default:
188		errno = EINVAL;
189		return (-1);
190	}
191	return (fp->f_seekp);
192}
193
194static int
195zfs_stat(struct open_file *f, struct stat *sb)
196{
197	const spa_t *spa = ((struct zfsmount *)f->f_devdata)->spa;
198	struct file *fp = (struct file *)f->f_fsdata;
199
200	return (zfs_dnode_stat(spa, &fp->f_dnode, sb));
201}
202
203static int
204zfs_readdir(struct open_file *f, struct dirent *d)
205{
206	const spa_t *spa = ((struct zfsmount *)f->f_devdata)->spa;
207	struct file *fp = (struct file *)f->f_fsdata;
208	mzap_ent_phys_t mze;
209	struct stat sb;
210	size_t bsize = fp->f_dnode.dn_datablkszsec << SPA_MINBLOCKSHIFT;
211	int rc;
212
213	rc = zfs_stat(f, &sb);
214	if (rc)
215		return (rc);
216	if (!S_ISDIR(sb.st_mode))
217		return (ENOTDIR);
218
219	/*
220	 * If this is the first read, get the zap type.
221	 */
222	if (fp->f_seekp == 0) {
223		rc = dnode_read(spa, &fp->f_dnode, 0, &fp->f_zap_type,
224		    sizeof (fp->f_zap_type));
225		if (rc)
226			return (rc);
227
228		if (fp->f_zap_type == ZBT_MICRO) {
229			fp->f_seekp = offsetof(mzap_phys_t, mz_chunk);
230		} else {
231			rc = dnode_read(spa, &fp->f_dnode,
232			    offsetof(zap_phys_t, zap_num_leafs),
233			    &fp->f_num_leafs, sizeof (fp->f_num_leafs));
234			if (rc)
235				return (rc);
236
237			fp->f_seekp = bsize;
238			fp->f_zap_leaf = malloc(bsize);
239			if (fp->f_zap_leaf == NULL)
240				return (ENOMEM);
241			rc = dnode_read(spa, &fp->f_dnode, fp->f_seekp,
242			    fp->f_zap_leaf, bsize);
243			if (rc)
244				return (rc);
245		}
246	}
247
248	if (fp->f_zap_type == ZBT_MICRO) {
249	mzap_next:
250		if (fp->f_seekp >= bsize)
251			return (ENOENT);
252
253		rc = dnode_read(spa, &fp->f_dnode, fp->f_seekp, &mze,
254		    sizeof (mze));
255		if (rc)
256			return (rc);
257		fp->f_seekp += sizeof (mze);
258
259		if (!mze.mze_name[0])
260			goto mzap_next;
261
262		d->d_fileno = ZFS_DIRENT_OBJ(mze.mze_value);
263		d->d_type = ZFS_DIRENT_TYPE(mze.mze_value);
264		strcpy(d->d_name, mze.mze_name);
265		d->d_namlen = strlen(d->d_name);
266		return (0);
267	} else {
268		zap_leaf_t zl;
269		zap_leaf_chunk_t *zc, *nc;
270		int chunk;
271		size_t namelen;
272		char *p;
273		uint64_t value;
274
275		/*
276		 * Initialise this so we can use the ZAP size
277		 * calculating macros.
278		 */
279		zl.l_bs = ilog2(bsize);
280		zl.l_phys = fp->f_zap_leaf;
281
282		/*
283		 * Figure out which chunk we are currently looking at
284		 * and consider seeking to the next leaf. We use the
285		 * low bits of f_seekp as a simple chunk index.
286		 */
287	fzap_next:
288		chunk = fp->f_seekp & (bsize - 1);
289		if (chunk == ZAP_LEAF_NUMCHUNKS(&zl)) {
290			fp->f_seekp = (fp->f_seekp & ~(bsize - 1)) + bsize;
291			chunk = 0;
292
293			/*
294			 * Check for EOF and read the new leaf.
295			 */
296			if (fp->f_seekp >= bsize * fp->f_num_leafs)
297				return (ENOENT);
298
299			rc = dnode_read(spa, &fp->f_dnode, fp->f_seekp,
300			    fp->f_zap_leaf, bsize);
301			if (rc)
302				return (rc);
303		}
304
305		zc = &ZAP_LEAF_CHUNK(&zl, chunk);
306		fp->f_seekp++;
307		if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
308			goto fzap_next;
309
310		namelen = zc->l_entry.le_name_numints;
311		if (namelen > sizeof (d->d_name))
312			namelen = sizeof (d->d_name);
313
314		/*
315		 * Paste the name back together.
316		 */
317		nc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_name_chunk);
318		p = d->d_name;
319		while (namelen > 0) {
320			int len;
321			len = namelen;
322			if (len > ZAP_LEAF_ARRAY_BYTES)
323				len = ZAP_LEAF_ARRAY_BYTES;
324			memcpy(p, nc->l_array.la_array, len);
325			p += len;
326			namelen -= len;
327			nc = &ZAP_LEAF_CHUNK(&zl, nc->l_array.la_next);
328		}
329		d->d_name[sizeof (d->d_name) - 1] = 0;
330
331		/*
332		 * Assume the first eight bytes of the value are
333		 * a uint64_t.
334		 */
335		value = fzap_leaf_value(&zl, zc);
336
337		d->d_fileno = ZFS_DIRENT_OBJ(value);
338		d->d_type = ZFS_DIRENT_TYPE(value);
339		d->d_namlen = strlen(d->d_name);
340
341		return (0);
342	}
343}
344
345static int
346vdev_read(vdev_t *vdev __unused, void *priv, off_t offset, void *buf,
347    size_t bytes)
348{
349	int fd, ret;
350	size_t res, size, remainder, rb_size, blksz;
351	unsigned secsz;
352	off_t off;
353	char *bouncebuf, *rb_buf;
354
355	fd = (uintptr_t)priv;
356	bouncebuf = NULL;
357
358	ret = ioctl(fd, DIOCGSECTORSIZE, &secsz);
359	if (ret != 0)
360		return (ret);
361
362	off = offset / secsz;
363	remainder = offset % secsz;
364	if (lseek(fd, off * secsz, SEEK_SET) == -1)
365		return (errno);
366
367	rb_buf = buf;
368	rb_size = bytes;
369	size = roundup2(bytes + remainder, secsz);
370	blksz = size;
371	if (remainder != 0 || size != bytes) {
372		bouncebuf = zfs_alloc(secsz);
373		if (bouncebuf == NULL) {
374			printf("vdev_read: out of memory\n");
375			return (ENOMEM);
376		}
377		rb_buf = bouncebuf;
378		blksz = rb_size - remainder;
379	}
380
381	while (bytes > 0) {
382		res = read(fd, rb_buf, rb_size);
383		if (res != rb_size) {
384			ret = EIO;
385			goto error;
386		}
387		if (bytes < blksz)
388			blksz = bytes;
389		if (bouncebuf != NULL)
390			memcpy(buf, rb_buf + remainder, blksz);
391		buf = (void *)((uintptr_t)buf + blksz);
392		bytes -= blksz;
393		remainder = 0;
394		blksz = rb_size;
395	}
396
397	ret = 0;
398error:
399	if (bouncebuf != NULL)
400		zfs_free(bouncebuf, secsz);
401	return (ret);
402}
403
404static int
405zfs_dev_init(void)
406{
407	spa_t *spa;
408	spa_t *next;
409	spa_t *prev;
410
411	zfs_init();
412	if (archsw.arch_zfs_probe == NULL)
413		return (ENXIO);
414	archsw.arch_zfs_probe();
415
416	prev = NULL;
417	spa = STAILQ_FIRST(&zfs_pools);
418	while (spa != NULL) {
419		next = STAILQ_NEXT(spa, spa_link);
420		if (zfs_spa_init(spa)) {
421			if (prev == NULL)
422				STAILQ_REMOVE_HEAD(&zfs_pools, spa_link);
423			else
424				STAILQ_REMOVE_AFTER(&zfs_pools, prev, spa_link);
425		} else
426			prev = spa;
427		spa = next;
428	}
429	return (0);
430}
431
432struct zfs_probe_args {
433	int		fd;
434	const char	*devname;
435	uint64_t	*pool_guid;
436	unsigned	secsz;
437};
438
439static int
440zfs_diskread(void *arg, void *buf, size_t blocks, uint64_t offset)
441{
442	struct zfs_probe_args *ppa;
443
444	ppa = (struct zfs_probe_args *)arg;
445	return (vdev_read(NULL, (void *)(uintptr_t)ppa->fd,
446	    offset * ppa->secsz, buf, blocks * ppa->secsz));
447}
448
449static int
450zfs_probe(int fd, uint64_t *pool_guid)
451{
452	spa_t *spa;
453	int ret;
454
455	spa = NULL;
456	ret = vdev_probe(vdev_read, (void *)(uintptr_t)fd, &spa);
457	if (ret == 0 && pool_guid != NULL)
458		*pool_guid = spa->spa_guid;
459	return (ret);
460}
461
462static int
463zfs_probe_partition(void *arg, const char *partname,
464    const struct ptable_entry *part)
465{
466	struct zfs_probe_args *ppa, pa;
467	struct ptable *table;
468	char devname[32];
469	int ret = 0;
470
471	/* filter out partitions *not* used by zfs */
472	switch (part->type) {
473	case PART_RESERVED:	/* efi reserverd */
474	case PART_VTOC_BOOT:	/* vtoc boot area */
475	case PART_VTOC_SWAP:
476		return (ret);
477	default:
478		break;
479	}
480	ppa = (struct zfs_probe_args *)arg;
481	strncpy(devname, ppa->devname, strlen(ppa->devname) - 1);
482	devname[strlen(ppa->devname) - 1] = '\0';
483	sprintf(devname, "%s%s:", devname, partname);
484	pa.fd = open(devname, O_RDONLY);
485	if (pa.fd == -1)
486		return (ret);
487	ret = zfs_probe(pa.fd, ppa->pool_guid);
488	if (ret == 0)
489		return (ret);
490	if (part->type == PART_SOLARIS2) {
491		pa.devname = devname;
492		pa.pool_guid = ppa->pool_guid;
493		pa.secsz = ppa->secsz;
494		table = ptable_open(&pa, part->end - part->start + 1,
495		    ppa->secsz, zfs_diskread);
496		if (table != NULL) {
497			enum ptable_type pt = ptable_gettype(table);
498
499			if (pt == PTABLE_VTOC8 || pt == PTABLE_VTOC)
500				ptable_iterate(table, &pa, zfs_probe_partition);
501			ptable_close(table);
502		}
503	}
504	close(pa.fd);
505	return (0);
506}
507
508int
509zfs_probe_dev(const char *devname, uint64_t *pool_guid)
510{
511	struct disk_devdesc *dev;
512	struct ptable *table;
513	struct zfs_probe_args pa;
514	uint64_t mediasz;
515	int ret;
516
517	if (pool_guid)
518		*pool_guid = 0;
519	pa.fd = open(devname, O_RDONLY);
520	if (pa.fd == -1)
521		return (ENXIO);
522	/*
523	 * We will not probe the whole disk, we can not boot from such
524	 * disks and some systems will misreport the disk sizes and will
525	 * hang while accessing the disk.
526	 */
527	if (archsw.arch_getdev((void **)&dev, devname, NULL) == 0) {
528		int partition = dev->d_partition;
529		int slice = dev->d_slice;
530
531		free(dev);
532		if (partition != D_PARTNONE && slice != D_SLICENONE) {
533			ret = zfs_probe(pa.fd, pool_guid);
534			if (ret == 0)
535				return (0);
536		}
537	}
538
539	/* Probe each partition */
540	ret = ioctl(pa.fd, DIOCGMEDIASIZE, &mediasz);
541	if (ret == 0)
542		ret = ioctl(pa.fd, DIOCGSECTORSIZE, &pa.secsz);
543	if (ret == 0) {
544		pa.devname = devname;
545		pa.pool_guid = pool_guid;
546		table = ptable_open(&pa, mediasz / pa.secsz, pa.secsz,
547		    zfs_diskread);
548		if (table != NULL) {
549			ptable_iterate(table, &pa, zfs_probe_partition);
550			ptable_close(table);
551		}
552	}
553	close(pa.fd);
554	if (pool_guid && *pool_guid == 0)
555		ret = ENXIO;
556	return (ret);
557}
558
559/*
560 * Print information about ZFS pools
561 */
562static int
563zfs_dev_print(int verbose)
564{
565	spa_t *spa;
566	char line[80];
567	int ret = 0;
568
569	if (STAILQ_EMPTY(&zfs_pools))
570		return (0);
571
572	printf("%s devices:", zfs_dev.dv_name);
573	if ((ret = pager_output("\n")) != 0)
574		return (ret);
575
576	if (verbose) {
577		return (spa_all_status());
578	}
579	STAILQ_FOREACH(spa, &zfs_pools, spa_link) {
580		sprintf(line, "    zfs:%s\n", spa->spa_name);
581		ret = pager_output(line);
582		if (ret != 0)
583			break;
584	}
585	return (ret);
586}
587
588/*
589 * Attempt to open the pool described by (dev) for use by (f).
590 */
591static int
592zfs_dev_open(struct open_file *f, ...)
593{
594	va_list		args;
595	struct zfs_devdesc	*dev;
596	struct zfsmount	*mount;
597	spa_t		*spa;
598	int		rv;
599
600	va_start(args, f);
601	dev = va_arg(args, struct zfs_devdesc *);
602	va_end(args);
603
604	if (dev->pool_guid == 0)
605		spa = STAILQ_FIRST(&zfs_pools);
606	else
607		spa = spa_find_by_guid(dev->pool_guid);
608	if (!spa)
609		return (ENXIO);
610	mount = malloc(sizeof (*mount));
611	if (mount == NULL)
612		rv = ENOMEM;
613	else
614		rv = zfs_mount(spa, dev->root_guid, mount);
615	if (rv != 0) {
616		free(mount);
617		return (rv);
618	}
619	if (mount->objset.os_type != DMU_OST_ZFS) {
620		printf("Unexpected object set type %ju\n",
621		    (uintmax_t)mount->objset.os_type);
622		free(mount);
623		return (EIO);
624	}
625	f->f_devdata = mount;
626	free(dev);
627	return (0);
628}
629
630static int
631zfs_dev_close(struct open_file *f)
632{
633
634	free(f->f_devdata);
635	f->f_devdata = NULL;
636	return (0);
637}
638
639static int
640zfs_dev_strategy(void *devdata __unused, int rw __unused,
641    daddr_t dblk __unused, size_t size __unused,
642    char *buf __unused, size_t *rsize __unused)
643{
644
645	return (ENOSYS);
646}
647
648struct devsw zfs_dev = {
649	.dv_name = "zfs",
650	.dv_type = DEVT_ZFS,
651	.dv_init = zfs_dev_init,
652	.dv_strategy = zfs_dev_strategy,
653	.dv_open = zfs_dev_open,
654	.dv_close = zfs_dev_close,
655	.dv_ioctl = noioctl,
656	.dv_print = zfs_dev_print,
657	.dv_cleanup = NULL
658};
659
660int
661zfs_parsedev(struct zfs_devdesc *dev, const char *devspec, const char **path)
662{
663	static char	rootname[ZFS_MAXNAMELEN];
664	static char	poolname[ZFS_MAXNAMELEN];
665	spa_t		*spa;
666	const char	*end;
667	const char	*np;
668	const char	*sep;
669	int		rv;
670
671	np = devspec;
672	if (*np != ':')
673		return (EINVAL);
674	np++;
675	end = strrchr(np, ':');
676	if (end == NULL)
677		return (EINVAL);
678	sep = strchr(np, '/');
679	if (sep == NULL || sep >= end)
680		sep = end;
681	memcpy(poolname, np, sep - np);
682	poolname[sep - np] = '\0';
683	if (sep < end) {
684		sep++;
685		memcpy(rootname, sep, end - sep);
686		rootname[end - sep] = '\0';
687	}
688	else
689		rootname[0] = '\0';
690
691	spa = spa_find_by_name(poolname);
692	if (!spa)
693		return (ENXIO);
694	dev->pool_guid = spa->spa_guid;
695	rv = zfs_lookup_dataset(spa, rootname, &dev->root_guid);
696	if (rv != 0)
697		return (rv);
698	if (path != NULL)
699		*path = (*end == '\0') ? end : end + 1;
700	dev->dd.d_dev = &zfs_dev;
701	return (0);
702}
703
704char *
705zfs_bootfs(void *zdev)
706{
707	static char		rootname[ZFS_MAXNAMELEN];
708	static char		buf[2 * ZFS_MAXNAMELEN];
709	struct zfs_devdesc	*dev = (struct zfs_devdesc *)zdev;
710	uint64_t		objnum;
711	spa_t			*spa;
712	int			n;
713
714	buf[0] = '\0';
715	if (dev->dd.d_dev->dv_type != DEVT_ZFS)
716		return (buf);
717
718	spa = spa_find_by_guid(dev->pool_guid);
719	if (spa == NULL) {
720		printf("ZFS: can't find pool by guid\n");
721		return (buf);
722	}
723	if (zfs_rlookup(spa, dev->root_guid, rootname)) {
724		printf("ZFS: can't find filesystem by guid\n");
725		return (buf);
726	}
727	if (zfs_lookup_dataset(spa, rootname, &objnum)) {
728		printf("ZFS: can't find filesystem by name\n");
729		return (buf);
730	}
731
732	/* Set the environment. */
733	snprintf(buf, sizeof (buf), "%" PRIu64, dev->pool_guid);
734	setenv("zfs-bootpool", buf, 1);
735	snprintf(buf, sizeof (buf), "%" PRIu64, spa->spa_boot_vdev->v_guid);
736	setenv("zfs-bootvdev", buf, 1);
737	snprintf(buf, sizeof (buf), "%s/%" PRIu64, spa->spa_name, objnum);
738	setenv("zfs-bootfs", buf, 1);
739	if (spa->spa_boot_vdev->v_phys_path != NULL)
740		setenv("bootpath", spa->spa_boot_vdev->v_phys_path, 1);
741	if (spa->spa_boot_vdev->v_devid != NULL)
742		setenv("diskdevid", spa->spa_boot_vdev->v_devid, 1);
743
744	/*
745	 * Build the command line string. Once our kernel will read
746	 * the environment and we can stop caring about old kernels,
747	 * we can remove this part.
748	 */
749	snprintf(buf, sizeof (buf), "zfs-bootfs=%s/%" PRIu64, spa->spa_name,
750	    objnum);
751	n = strlen(buf);
752	if (spa->spa_boot_vdev->v_phys_path != NULL) {
753		snprintf(buf+n, sizeof (buf) - n, ",bootpath=\"%s\"",
754		    spa->spa_boot_vdev->v_phys_path);
755		n = strlen(buf);
756	}
757	if (spa->spa_boot_vdev->v_devid != NULL) {
758		snprintf(buf+n, sizeof (buf) - n, ",diskdevid=\"%s\"",
759		    spa->spa_boot_vdev->v_devid);
760	}
761	return (buf);
762}
763
764char *
765zfs_fmtdev(void *vdev)
766{
767	static char		rootname[ZFS_MAXNAMELEN];
768	static char		buf[2 * ZFS_MAXNAMELEN + 8];
769	struct zfs_devdesc	*dev = (struct zfs_devdesc *)vdev;
770	spa_t			*spa;
771
772	buf[0] = '\0';
773	if (dev->dd.d_dev->dv_type != DEVT_ZFS)
774		return (buf);
775
776	/* Do we have any pools? */
777	spa = STAILQ_FIRST(&zfs_pools);
778	if (spa == NULL)
779		return (buf);
780
781	if (dev->pool_guid == 0)
782		dev->pool_guid = spa->spa_guid;
783	else
784		spa = spa_find_by_guid(dev->pool_guid);
785
786	if (spa == NULL) {
787		printf("ZFS: can't find pool by guid\n");
788		return (buf);
789	}
790	if (dev->root_guid == 0 && zfs_get_root(spa, &dev->root_guid)) {
791		printf("ZFS: can't find root filesystem\n");
792		return (buf);
793	}
794	if (zfs_rlookup(spa, dev->root_guid, rootname)) {
795		printf("ZFS: can't find filesystem by guid\n");
796		return (buf);
797	}
798
799	if (rootname[0] == '\0')
800		sprintf(buf, "%s:%s:", dev->dd.d_dev->dv_name, spa->spa_name);
801	else
802		sprintf(buf, "%s:%s/%s:", dev->dd.d_dev->dv_name, spa->spa_name,
803		    rootname);
804	return (buf);
805}
806
807int
808zfs_list(const char *name)
809{
810	static char	poolname[ZFS_MAXNAMELEN];
811	uint64_t	objid;
812	spa_t		*spa;
813	const char	*dsname;
814	int		len;
815	int		rv;
816
817	len = strlen(name);
818	dsname = strchr(name, '/');
819	if (dsname != NULL) {
820		len = dsname - name;
821		dsname++;
822	} else
823		dsname = "";
824	memcpy(poolname, name, len);
825	poolname[len] = '\0';
826
827	spa = spa_find_by_name(poolname);
828	if (!spa)
829		return (ENXIO);
830	rv = zfs_lookup_dataset(spa, dsname, &objid);
831	if (rv != 0)
832		return (rv);
833
834	return (zfs_list_dataset(spa, objid));
835}
836