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, head, tail, total_size, full_sec_size;
351	unsigned secsz, do_tail_read;
352	off_t start_sec;
353	char *outbuf, *bouncebuf;
354
355	fd = (uintptr_t)priv;
356	outbuf = (char *)buf;
357	bouncebuf = NULL;
358
359	ret = ioctl(fd, DIOCGSECTORSIZE, &secsz);
360	if (ret != 0)
361		return (ret);
362
363	/*
364	 * Handling reads of arbitrary offset and size - multi-sector case
365	 * and single-sector case.
366	 *
367	 *                        Multi-sector Case
368	 *                (do_tail_read = true if tail > 0)
369	 *
370	 *   |<----------------------total_size--------------------->|
371	 *   |                                                       |
372	 *   |<--head-->|<--------------bytes------------>|<--tail-->|
373	 *   |          |                                 |          |
374	 *   |          |       |<~full_sec_size~>|       |          |
375	 *   +------------------+                 +------------------+
376	 *   |          |0101010|     .  .  .     |0101011|          |
377	 *   +------------------+                 +------------------+
378	 *         start_sec                         start_sec + n
379	 *
380	 *
381	 *                      Single-sector Case
382	 *                    (do_tail_read = false)
383	 *
384	 *              |<------total_size = secsz----->|
385	 *              |                               |
386	 *              |<-head->|<---bytes--->|<-tail->|
387	 *              +-------------------------------+
388	 *              |        |0101010101010|        |
389	 *              +-------------------------------+
390	 *                          start_sec
391	 */
392	start_sec = offset / secsz;
393	head = offset % secsz;
394	total_size = roundup2(head + bytes, secsz);
395	tail = total_size - (head + bytes);
396	do_tail_read = ((tail > 0) && (head + bytes > secsz));
397	full_sec_size = total_size;
398	if (head > 0)
399		full_sec_size -= secsz;
400	if (do_tail_read)
401		full_sec_size -= secsz;
402
403	/* Return of partial sector data requires a bounce buffer. */
404	if ((head > 0) || do_tail_read || bytes < secsz) {
405		bouncebuf = malloc(secsz);
406		if (bouncebuf == NULL) {
407			printf("vdev_read: out of memory\n");
408			return (ENOMEM);
409		}
410	}
411
412	if (lseek(fd, start_sec * secsz, SEEK_SET) == -1) {
413		ret = errno;
414		goto error;
415	}
416
417	/* Partial data return from first sector */
418	if (head > 0) {
419		res = read(fd, bouncebuf, secsz);
420		if (res != secsz) {
421			ret = EIO;
422			goto error;
423		}
424		memcpy(outbuf, bouncebuf + head, min(secsz - head, bytes));
425		outbuf += min(secsz - head, bytes);
426	}
427
428	/* Full data return from read sectors */
429	if (full_sec_size > 0) {
430		if (bytes < full_sec_size) {
431			res = read(fd, bouncebuf, secsz);
432			if (res != secsz) {
433				ret = EIO;
434				goto error;
435			}
436			memcpy(outbuf, bouncebuf, bytes);
437		} else {
438			res = read(fd, outbuf, full_sec_size);
439			if (res != full_sec_size) {
440				ret = EIO;
441				goto error;
442			}
443			outbuf += full_sec_size;
444		}
445	}
446
447	/* Partial data return from last sector */
448	if (do_tail_read) {
449		res = read(fd, bouncebuf, secsz);
450		if (res != secsz) {
451			ret = EIO;
452			goto error;
453		}
454		memcpy(outbuf, bouncebuf, secsz - tail);
455	}
456
457	ret = 0;
458error:
459	free(bouncebuf);
460	return (ret);
461}
462
463static int
464zfs_dev_init(void)
465{
466	spa_t *spa;
467	spa_t *next;
468	spa_t *prev;
469
470	zfs_init();
471	if (archsw.arch_zfs_probe == NULL)
472		return (ENXIO);
473	archsw.arch_zfs_probe();
474
475	prev = NULL;
476	spa = STAILQ_FIRST(&zfs_pools);
477	while (spa != NULL) {
478		next = STAILQ_NEXT(spa, spa_link);
479		if (zfs_spa_init(spa)) {
480			if (prev == NULL)
481				STAILQ_REMOVE_HEAD(&zfs_pools, spa_link);
482			else
483				STAILQ_REMOVE_AFTER(&zfs_pools, prev, spa_link);
484		} else
485			prev = spa;
486		spa = next;
487	}
488	return (0);
489}
490
491struct zfs_probe_args {
492	int		fd;
493	const char	*devname;
494	uint64_t	*pool_guid;
495	unsigned	secsz;
496};
497
498static int
499zfs_diskread(void *arg, void *buf, size_t blocks, uint64_t offset)
500{
501	struct zfs_probe_args *ppa;
502
503	ppa = (struct zfs_probe_args *)arg;
504	return (vdev_read(NULL, (void *)(uintptr_t)ppa->fd,
505	    offset * ppa->secsz, buf, blocks * ppa->secsz));
506}
507
508static int
509zfs_probe(int fd, uint64_t *pool_guid)
510{
511	spa_t *spa;
512	int ret;
513
514	spa = NULL;
515	ret = vdev_probe(vdev_read, (void *)(uintptr_t)fd, &spa);
516	if (ret == 0 && pool_guid != NULL)
517		*pool_guid = spa->spa_guid;
518	return (ret);
519}
520
521static int
522zfs_probe_partition(void *arg, const char *partname,
523    const struct ptable_entry *part)
524{
525	struct zfs_probe_args *ppa, pa;
526	struct ptable *table;
527	char devname[32];
528	int ret = 0;
529
530	/* filter out partitions *not* used by zfs */
531	switch (part->type) {
532	case PART_RESERVED:	/* efi reserverd */
533	case PART_VTOC_BOOT:	/* vtoc boot area */
534	case PART_VTOC_SWAP:
535		return (ret);
536	default:
537		break;
538	}
539	ppa = (struct zfs_probe_args *)arg;
540	strncpy(devname, ppa->devname, strlen(ppa->devname) - 1);
541	devname[strlen(ppa->devname) - 1] = '\0';
542	sprintf(devname, "%s%s:", devname, partname);
543	pa.fd = open(devname, O_RDONLY);
544	if (pa.fd == -1)
545		return (ret);
546	ret = zfs_probe(pa.fd, ppa->pool_guid);
547	if (ret == 0)
548		return (ret);
549	if (part->type == PART_SOLARIS2) {
550		pa.devname = devname;
551		pa.pool_guid = ppa->pool_guid;
552		pa.secsz = ppa->secsz;
553		table = ptable_open(&pa, part->end - part->start + 1,
554		    ppa->secsz, zfs_diskread);
555		if (table != NULL) {
556			enum ptable_type pt = ptable_gettype(table);
557
558			if (pt == PTABLE_VTOC8 || pt == PTABLE_VTOC)
559				ptable_iterate(table, &pa, zfs_probe_partition);
560			ptable_close(table);
561		}
562	}
563	close(pa.fd);
564	return (0);
565}
566
567int
568zfs_probe_dev(const char *devname, uint64_t *pool_guid)
569{
570	struct disk_devdesc *dev;
571	struct ptable *table;
572	struct zfs_probe_args pa;
573	uint64_t mediasz;
574	int ret;
575
576	if (pool_guid)
577		*pool_guid = 0;
578	pa.fd = open(devname, O_RDONLY);
579	if (pa.fd == -1)
580		return (ENXIO);
581	/*
582	 * We will not probe the whole disk, we can not boot from such
583	 * disks and some systems will misreport the disk sizes and will
584	 * hang while accessing the disk.
585	 */
586	if (archsw.arch_getdev((void **)&dev, devname, NULL) == 0) {
587		int partition = dev->d_partition;
588		int slice = dev->d_slice;
589
590		free(dev);
591		if (partition != D_PARTNONE && slice != D_SLICENONE) {
592			ret = zfs_probe(pa.fd, pool_guid);
593			if (ret == 0)
594				return (0);
595		}
596	}
597
598	/* Probe each partition */
599	ret = ioctl(pa.fd, DIOCGMEDIASIZE, &mediasz);
600	if (ret == 0)
601		ret = ioctl(pa.fd, DIOCGSECTORSIZE, &pa.secsz);
602	if (ret == 0) {
603		pa.devname = devname;
604		pa.pool_guid = pool_guid;
605		table = ptable_open(&pa, mediasz / pa.secsz, pa.secsz,
606		    zfs_diskread);
607		if (table != NULL) {
608			ptable_iterate(table, &pa, zfs_probe_partition);
609			ptable_close(table);
610		}
611	}
612	close(pa.fd);
613	if (pool_guid && *pool_guid == 0)
614		ret = ENXIO;
615	return (ret);
616}
617
618/*
619 * Print information about ZFS pools
620 */
621static int
622zfs_dev_print(int verbose)
623{
624	spa_t *spa;
625	char line[80];
626	int ret = 0;
627
628	if (STAILQ_EMPTY(&zfs_pools))
629		return (0);
630
631	printf("%s devices:", zfs_dev.dv_name);
632	if ((ret = pager_output("\n")) != 0)
633		return (ret);
634
635	if (verbose) {
636		return (spa_all_status());
637	}
638	STAILQ_FOREACH(spa, &zfs_pools, spa_link) {
639		sprintf(line, "    zfs:%s\n", spa->spa_name);
640		ret = pager_output(line);
641		if (ret != 0)
642			break;
643	}
644	return (ret);
645}
646
647/*
648 * Attempt to open the pool described by (dev) for use by (f).
649 */
650static int
651zfs_dev_open(struct open_file *f, ...)
652{
653	va_list		args;
654	struct zfs_devdesc	*dev;
655	struct zfsmount	*mount;
656	spa_t		*spa;
657	int		rv;
658
659	va_start(args, f);
660	dev = va_arg(args, struct zfs_devdesc *);
661	va_end(args);
662
663	if (dev->pool_guid == 0)
664		spa = STAILQ_FIRST(&zfs_pools);
665	else
666		spa = spa_find_by_guid(dev->pool_guid);
667	if (!spa)
668		return (ENXIO);
669	mount = malloc(sizeof (*mount));
670	if (mount == NULL)
671		rv = ENOMEM;
672	else
673		rv = zfs_mount(spa, dev->root_guid, mount);
674	if (rv != 0) {
675		free(mount);
676		return (rv);
677	}
678	if (mount->objset.os_type != DMU_OST_ZFS) {
679		printf("Unexpected object set type %ju\n",
680		    (uintmax_t)mount->objset.os_type);
681		free(mount);
682		return (EIO);
683	}
684	f->f_devdata = mount;
685	free(dev);
686	return (0);
687}
688
689static int
690zfs_dev_close(struct open_file *f)
691{
692
693	free(f->f_devdata);
694	f->f_devdata = NULL;
695	return (0);
696}
697
698static int
699zfs_dev_strategy(void *devdata __unused, int rw __unused,
700    daddr_t dblk __unused, size_t size __unused,
701    char *buf __unused, size_t *rsize __unused)
702{
703
704	return (ENOSYS);
705}
706
707struct devsw zfs_dev = {
708	.dv_name = "zfs",
709	.dv_type = DEVT_ZFS,
710	.dv_init = zfs_dev_init,
711	.dv_strategy = zfs_dev_strategy,
712	.dv_open = zfs_dev_open,
713	.dv_close = zfs_dev_close,
714	.dv_ioctl = noioctl,
715	.dv_print = zfs_dev_print,
716	.dv_cleanup = NULL
717};
718
719int
720zfs_parsedev(struct zfs_devdesc *dev, const char *devspec, const char **path)
721{
722	static char	rootname[ZFS_MAXNAMELEN];
723	static char	poolname[ZFS_MAXNAMELEN];
724	spa_t		*spa;
725	const char	*end;
726	const char	*np;
727	const char	*sep;
728	int		rv;
729
730	np = devspec;
731	if (*np != ':')
732		return (EINVAL);
733	np++;
734	end = strrchr(np, ':');
735	if (end == NULL)
736		return (EINVAL);
737	sep = strchr(np, '/');
738	if (sep == NULL || sep >= end)
739		sep = end;
740	memcpy(poolname, np, sep - np);
741	poolname[sep - np] = '\0';
742	if (sep < end) {
743		sep++;
744		memcpy(rootname, sep, end - sep);
745		rootname[end - sep] = '\0';
746	}
747	else
748		rootname[0] = '\0';
749
750	spa = spa_find_by_name(poolname);
751	if (!spa)
752		return (ENXIO);
753	dev->pool_guid = spa->spa_guid;
754	rv = zfs_lookup_dataset(spa, rootname, &dev->root_guid);
755	if (rv != 0)
756		return (rv);
757	if (path != NULL)
758		*path = (*end == '\0') ? end : end + 1;
759	dev->dd.d_dev = &zfs_dev;
760	return (0);
761}
762
763char *
764zfs_bootfs(void *zdev)
765{
766	static char		rootname[ZFS_MAXNAMELEN];
767	static char		buf[2 * ZFS_MAXNAMELEN];
768	struct zfs_devdesc	*dev = (struct zfs_devdesc *)zdev;
769	uint64_t		objnum;
770	spa_t			*spa;
771	int			n;
772
773	buf[0] = '\0';
774	if (dev->dd.d_dev->dv_type != DEVT_ZFS)
775		return (buf);
776
777	spa = spa_find_by_guid(dev->pool_guid);
778	if (spa == NULL) {
779		printf("ZFS: can't find pool by guid\n");
780		return (buf);
781	}
782	if (zfs_rlookup(spa, dev->root_guid, rootname)) {
783		printf("ZFS: can't find filesystem by guid\n");
784		return (buf);
785	}
786	if (zfs_lookup_dataset(spa, rootname, &objnum)) {
787		printf("ZFS: can't find filesystem by name\n");
788		return (buf);
789	}
790
791	/* Set the environment. */
792	snprintf(buf, sizeof (buf), "%" PRIu64, dev->pool_guid);
793	setenv("zfs-bootpool", buf, 1);
794	snprintf(buf, sizeof (buf), "%" PRIu64, spa->spa_boot_vdev->v_guid);
795	setenv("zfs-bootvdev", buf, 1);
796	snprintf(buf, sizeof (buf), "%s/%" PRIu64, spa->spa_name, objnum);
797	setenv("zfs-bootfs", buf, 1);
798	if (spa->spa_boot_vdev->v_phys_path != NULL)
799		setenv("bootpath", spa->spa_boot_vdev->v_phys_path, 1);
800	if (spa->spa_boot_vdev->v_devid != NULL)
801		setenv("diskdevid", spa->spa_boot_vdev->v_devid, 1);
802
803	/*
804	 * Build the command line string. Once our kernel will read
805	 * the environment and we can stop caring about old kernels,
806	 * we can remove this part.
807	 */
808	snprintf(buf, sizeof (buf), "zfs-bootfs=%s/%" PRIu64, spa->spa_name,
809	    objnum);
810	n = strlen(buf);
811	if (spa->spa_boot_vdev->v_phys_path != NULL) {
812		snprintf(buf+n, sizeof (buf) - n, ",bootpath=\"%s\"",
813		    spa->spa_boot_vdev->v_phys_path);
814		n = strlen(buf);
815	}
816	if (spa->spa_boot_vdev->v_devid != NULL) {
817		snprintf(buf+n, sizeof (buf) - n, ",diskdevid=\"%s\"",
818		    spa->spa_boot_vdev->v_devid);
819	}
820	return (buf);
821}
822
823char *
824zfs_fmtdev(void *vdev)
825{
826	static char		rootname[ZFS_MAXNAMELEN];
827	static char		buf[2 * ZFS_MAXNAMELEN + 8];
828	struct zfs_devdesc	*dev = (struct zfs_devdesc *)vdev;
829	spa_t			*spa;
830
831	buf[0] = '\0';
832	if (dev->dd.d_dev->dv_type != DEVT_ZFS)
833		return (buf);
834
835	/* Do we have any pools? */
836	spa = STAILQ_FIRST(&zfs_pools);
837	if (spa == NULL)
838		return (buf);
839
840	if (dev->pool_guid == 0)
841		dev->pool_guid = spa->spa_guid;
842	else
843		spa = spa_find_by_guid(dev->pool_guid);
844
845	if (spa == NULL) {
846		printf("ZFS: can't find pool by guid\n");
847		return (buf);
848	}
849	if (dev->root_guid == 0 && zfs_get_root(spa, &dev->root_guid)) {
850		printf("ZFS: can't find root filesystem\n");
851		return (buf);
852	}
853	if (zfs_rlookup(spa, dev->root_guid, rootname)) {
854		printf("ZFS: can't find filesystem by guid\n");
855		return (buf);
856	}
857
858	if (rootname[0] == '\0')
859		sprintf(buf, "%s:%s:", dev->dd.d_dev->dv_name, spa->spa_name);
860	else
861		sprintf(buf, "%s:%s/%s:", dev->dd.d_dev->dv_name, spa->spa_name,
862		    rootname);
863	return (buf);
864}
865
866int
867zfs_list(const char *name)
868{
869	static char	poolname[ZFS_MAXNAMELEN];
870	uint64_t	objid;
871	spa_t		*spa;
872	const char	*dsname;
873	int		len;
874	int		rv;
875
876	len = strlen(name);
877	dsname = strchr(name, '/');
878	if (dsname != NULL) {
879		len = dsname - name;
880		dsname++;
881	} else
882		dsname = "";
883	memcpy(poolname, name, len);
884	poolname[len] = '\0';
885
886	spa = spa_find_by_name(poolname);
887	if (!spa)
888		return (ENXIO);
889	rv = zfs_lookup_dataset(spa, dsname, &objid);
890	if (rv != 0)
891		return (rv);
892
893	return (zfs_list_dataset(spa, objid));
894}
895