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