zfs.c revision 8eef2ab6218419d91e0aefc223c9f2cd1c8e9a2c
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 = malloc(sizeof(struct file));
112	bzero(fp, sizeof(struct file));
113	f->f_fsdata = (void *)fp;
114
115	rc = zfs_lookup(mount, upath, &fp->f_dnode);
116	fp->f_seekp = 0;
117	if (rc) {
118		f->f_fsdata = NULL;
119		free(fp);
120	}
121	return (rc);
122}
123
124static int
125zfs_close(struct open_file *f)
126{
127	struct file *fp = (struct file *)f->f_fsdata;
128
129	dnode_cache_obj = 0;
130	f->f_fsdata = (void *)0;
131	if (fp == (struct file *)0)
132		return (0);
133
134	free(fp);
135	return (0);
136}
137
138/*
139 * Copy a portion of a file into kernel memory.
140 * Cross block boundaries when necessary.
141 */
142static int
143zfs_read(struct open_file *f, void *start, size_t size, size_t *resid	/* out */)
144{
145	const spa_t *spa = ((struct zfsmount *)f->f_devdata)->spa;
146	struct file *fp = (struct file *)f->f_fsdata;
147	struct stat sb;
148	size_t n;
149	int rc;
150
151	rc = zfs_stat(f, &sb);
152	if (rc)
153		return (rc);
154	n = size;
155	if (fp->f_seekp + n > sb.st_size)
156		n = sb.st_size - fp->f_seekp;
157
158	rc = dnode_read(spa, &fp->f_dnode, fp->f_seekp, start, n);
159	if (rc)
160		return (rc);
161
162	if (0) {
163	    int i;
164	    for (i = 0; i < n; i++)
165		putchar(((char*) start)[i]);
166	}
167	fp->f_seekp += n;
168	if (resid)
169		*resid = size - n;
170
171	return (0);
172}
173
174static off_t
175zfs_seek(struct open_file *f, off_t offset, int where)
176{
177	struct file *fp = (struct file *)f->f_fsdata;
178
179	switch (where) {
180	case SEEK_SET:
181		fp->f_seekp = offset;
182		break;
183	case SEEK_CUR:
184		fp->f_seekp += offset;
185		break;
186	case SEEK_END:
187	    {
188		struct stat sb;
189		int error;
190
191		error = zfs_stat(f, &sb);
192		if (error != 0) {
193			errno = error;
194			return (-1);
195		}
196		fp->f_seekp = sb.st_size - offset;
197		break;
198	    }
199	default:
200		errno = EINVAL;
201		return (-1);
202	}
203	return (fp->f_seekp);
204}
205
206static int
207zfs_stat(struct open_file *f, struct stat *sb)
208{
209	const spa_t *spa = ((struct zfsmount *)f->f_devdata)->spa;
210	struct file *fp = (struct file *)f->f_fsdata;
211
212	return (zfs_dnode_stat(spa, &fp->f_dnode, sb));
213}
214
215static int
216zfs_readdir(struct open_file *f, struct dirent *d)
217{
218	const spa_t *spa = ((struct zfsmount *)f->f_devdata)->spa;
219	struct file *fp = (struct file *)f->f_fsdata;
220	mzap_ent_phys_t mze;
221	struct stat sb;
222	size_t bsize = fp->f_dnode.dn_datablkszsec << SPA_MINBLOCKSHIFT;
223	int rc;
224
225	rc = zfs_stat(f, &sb);
226	if (rc)
227		return (rc);
228	if (!S_ISDIR(sb.st_mode))
229		return (ENOTDIR);
230
231	/*
232	 * If this is the first read, get the zap type.
233	 */
234	if (fp->f_seekp == 0) {
235		rc = dnode_read(spa, &fp->f_dnode,
236				0, &fp->f_zap_type, sizeof(fp->f_zap_type));
237		if (rc)
238			return (rc);
239
240		if (fp->f_zap_type == ZBT_MICRO) {
241			fp->f_seekp = offsetof(mzap_phys_t, mz_chunk);
242		} else {
243			rc = dnode_read(spa, &fp->f_dnode,
244					offsetof(zap_phys_t, zap_num_leafs),
245					&fp->f_num_leafs,
246					sizeof(fp->f_num_leafs));
247			if (rc)
248				return (rc);
249
250			fp->f_seekp = bsize;
251			fp->f_zap_leaf = (zap_leaf_phys_t *)malloc(bsize);
252			rc = dnode_read(spa, &fp->f_dnode,
253					fp->f_seekp,
254					fp->f_zap_leaf,
255					bsize);
256			if (rc)
257				return (rc);
258		}
259	}
260
261	if (fp->f_zap_type == ZBT_MICRO) {
262	mzap_next:
263		if (fp->f_seekp >= bsize)
264			return (ENOENT);
265
266		rc = dnode_read(spa, &fp->f_dnode,
267				fp->f_seekp, &mze, sizeof(mze));
268		if (rc)
269			return (rc);
270		fp->f_seekp += sizeof(mze);
271
272		if (!mze.mze_name[0])
273			goto mzap_next;
274
275		d->d_fileno = ZFS_DIRENT_OBJ(mze.mze_value);
276		d->d_type = ZFS_DIRENT_TYPE(mze.mze_value);
277		strcpy(d->d_name, mze.mze_name);
278		d->d_namlen = strlen(d->d_name);
279		return (0);
280	} else {
281		zap_leaf_t zl;
282		zap_leaf_chunk_t *zc, *nc;
283		int chunk;
284		size_t namelen;
285		char *p;
286		uint64_t value;
287
288		/*
289		 * Initialise this so we can use the ZAP size
290		 * calculating macros.
291		 */
292		zl.l_bs = ilog2(bsize);
293		zl.l_phys = fp->f_zap_leaf;
294
295		/*
296		 * Figure out which chunk we are currently looking at
297		 * and consider seeking to the next leaf. We use the
298		 * low bits of f_seekp as a simple chunk index.
299		 */
300	fzap_next:
301		chunk = fp->f_seekp & (bsize - 1);
302		if (chunk == ZAP_LEAF_NUMCHUNKS(&zl)) {
303			fp->f_seekp = (fp->f_seekp & ~(bsize - 1)) + bsize;
304			chunk = 0;
305
306			/*
307			 * Check for EOF and read the new leaf.
308			 */
309			if (fp->f_seekp >= bsize * fp->f_num_leafs)
310				return (ENOENT);
311
312			rc = dnode_read(spa, &fp->f_dnode,
313					fp->f_seekp,
314					fp->f_zap_leaf,
315					bsize);
316			if (rc)
317				return (rc);
318		}
319
320		zc = &ZAP_LEAF_CHUNK(&zl, chunk);
321		fp->f_seekp++;
322		if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
323			goto fzap_next;
324
325		namelen = zc->l_entry.le_name_numints;
326		if (namelen > sizeof(d->d_name))
327			namelen = sizeof(d->d_name);
328
329		/*
330		 * Paste the name back together.
331		 */
332		nc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_name_chunk);
333		p = d->d_name;
334		while (namelen > 0) {
335			int len;
336			len = namelen;
337			if (len > ZAP_LEAF_ARRAY_BYTES)
338				len = ZAP_LEAF_ARRAY_BYTES;
339			memcpy(p, nc->l_array.la_array, len);
340			p += len;
341			namelen -= len;
342			nc = &ZAP_LEAF_CHUNK(&zl, nc->l_array.la_next);
343		}
344		d->d_name[sizeof(d->d_name) - 1] = 0;
345
346		/*
347		 * Assume the first eight bytes of the value are
348		 * a uint64_t.
349		 */
350		value = fzap_leaf_value(&zl, zc);
351
352		d->d_fileno = ZFS_DIRENT_OBJ(value);
353		d->d_type = ZFS_DIRENT_TYPE(value);
354		d->d_namlen = strlen(d->d_name);
355
356		return (0);
357	}
358}
359
360static int
361vdev_read(vdev_t *vdev __unused, void *priv, off_t offset, void *buf,
362    size_t bytes)
363{
364	int fd, ret;
365	size_t res, size, remainder, rb_size, blksz;
366	unsigned secsz;
367	off_t off;
368	char *bouncebuf, *rb_buf;
369
370	fd = (uintptr_t) priv;
371	bouncebuf = NULL;
372
373	ret = ioctl(fd, DIOCGSECTORSIZE, &secsz);
374	if (ret != 0)
375		return (ret);
376
377	off = offset / secsz;
378	remainder = offset % secsz;
379	if (lseek(fd, off * secsz, SEEK_SET) == -1)
380		return (errno);
381
382	rb_buf = buf;
383	rb_size = bytes;
384	size = roundup2(bytes + remainder, secsz);
385	blksz = size;
386	if (remainder != 0 || size != bytes) {
387		bouncebuf = zfs_alloc(secsz);
388		if (bouncebuf == NULL) {
389			printf("vdev_read: out of memory\n");
390			return (ENOMEM);
391		}
392		rb_buf = bouncebuf;
393		blksz = rb_size - remainder;
394	}
395
396	while (bytes > 0) {
397		res = read(fd, rb_buf, rb_size);
398		if (res != rb_size) {
399			ret = EIO;
400			goto error;
401		}
402		if (bytes < blksz)
403			blksz = bytes;
404		if (bouncebuf != NULL)
405			memcpy(buf, rb_buf + remainder, blksz);
406		buf = (void *)((uintptr_t)buf + blksz);
407		bytes -= blksz;
408		remainder = 0;
409		blksz = rb_size;
410	}
411
412	ret = 0;
413error:
414	if (bouncebuf != NULL)
415		zfs_free(bouncebuf, secsz);
416	return (ret);
417}
418
419static int
420zfs_dev_init(void)
421{
422	spa_t *spa;
423	spa_t *next;
424	spa_t *prev;
425
426	zfs_init();
427	if (archsw.arch_zfs_probe == NULL)
428		return (ENXIO);
429	archsw.arch_zfs_probe();
430
431	prev = NULL;
432	spa = STAILQ_FIRST(&zfs_pools);
433	while (spa != NULL) {
434		next = STAILQ_NEXT(spa, spa_link);
435		if (zfs_spa_init(spa)) {
436			if (prev == NULL)
437				STAILQ_REMOVE_HEAD(&zfs_pools, spa_link);
438			else
439				STAILQ_REMOVE_AFTER(&zfs_pools, prev, spa_link);
440		} else
441			prev = spa;
442		spa = next;
443	}
444	return (0);
445}
446
447struct zfs_probe_args {
448	int		fd;
449	const char	*devname;
450	uint64_t	*pool_guid;
451	u_int		secsz;
452};
453
454static int
455zfs_diskread(void *arg, void *buf, size_t blocks, uint64_t offset)
456{
457	struct zfs_probe_args *ppa;
458
459	ppa = (struct zfs_probe_args *)arg;
460	return (vdev_read(NULL, (void *)(uintptr_t)ppa->fd,
461	    offset * ppa->secsz, buf, blocks * ppa->secsz));
462}
463
464static int
465zfs_probe(int fd, uint64_t *pool_guid)
466{
467	spa_t *spa;
468	int ret;
469
470	spa = NULL;
471	ret = vdev_probe(vdev_read, (void *)(uintptr_t)fd, &spa);
472	if (ret == 0 && pool_guid != NULL)
473		*pool_guid = spa->spa_guid;
474	return (ret);
475}
476
477static int
478zfs_probe_partition(void *arg, const char *partname,
479    const struct ptable_entry *part)
480{
481	struct zfs_probe_args *ppa, pa;
482	struct ptable *table;
483	char devname[32];
484	int ret = 0;
485
486	/* filter out partitions *not* used by zfs */
487	switch (part->type) {
488	case PART_RESERVED:	/* efi reserverd */
489	case PART_VTOC_BOOT:	/* vtoc boot area */
490	case PART_VTOC_SWAP:
491		return (ret);
492	default:
493		break;
494	}
495	ppa = (struct zfs_probe_args *)arg;
496	strncpy(devname, ppa->devname, strlen(ppa->devname) - 1);
497	devname[strlen(ppa->devname) - 1] = '\0';
498	sprintf(devname, "%s%s:", devname, partname);
499	pa.fd = open(devname, O_RDONLY);
500	if (pa.fd == -1)
501		return (ret);
502	ret = zfs_probe(pa.fd, ppa->pool_guid);
503	if (ret == 0)
504		return (ret);
505	if (part->type == PART_SOLARIS2) {
506		pa.devname = devname;
507		pa.pool_guid = ppa->pool_guid;
508		pa.secsz = ppa->secsz;
509		table = ptable_open(&pa, part->end - part->start + 1,
510		    ppa->secsz, zfs_diskread);
511		if (table != NULL) {
512			enum ptable_type pt = ptable_gettype(table);
513
514			if (pt == PTABLE_VTOC8 || pt == PTABLE_VTOC)
515				ptable_iterate(table, &pa, zfs_probe_partition);
516			ptable_close(table);
517		}
518	}
519	close(pa.fd);
520	return (0);
521}
522
523int
524zfs_probe_dev(const char *devname, uint64_t *pool_guid)
525{
526	struct disk_devdesc *dev;
527	struct ptable *table;
528	struct zfs_probe_args pa;
529	uint64_t mediasz;
530	int ret;
531
532	if (pool_guid)
533		*pool_guid = 0;
534	pa.fd = open(devname, O_RDONLY);
535	if (pa.fd == -1)
536		return (ENXIO);
537	/*
538	 * We will not probe the whole disk, we can not boot from such
539	 * disks and some systems will misreport the disk sizes and will
540	 * hang while accessing the disk.
541	 */
542	if (archsw.arch_getdev((void **)&dev, devname, NULL) == 0) {
543		int partition = dev->d_partition;
544		int slice = dev->d_slice;
545
546		free(dev);
547		if (partition != D_PARTNONE && slice != D_SLICENONE) {
548			ret = zfs_probe(pa.fd, pool_guid);
549			if (ret == 0)
550				return (0);
551		}
552	}
553
554	/* Probe each partition */
555	ret = ioctl(pa.fd, DIOCGMEDIASIZE, &mediasz);
556	if (ret == 0)
557		ret = ioctl(pa.fd, DIOCGSECTORSIZE, &pa.secsz);
558	if (ret == 0) {
559		pa.devname = devname;
560		pa.pool_guid = pool_guid;
561		table = ptable_open(&pa, mediasz / pa.secsz, pa.secsz,
562		    zfs_diskread);
563		if (table != NULL) {
564			ptable_iterate(table, &pa, zfs_probe_partition);
565			ptable_close(table);
566		}
567	}
568	close(pa.fd);
569	if (pool_guid && *pool_guid == 0)
570		ret = ENXIO;
571	return (ret);
572}
573
574/*
575 * Print information about ZFS pools
576 */
577static int
578zfs_dev_print(int verbose)
579{
580	spa_t *spa;
581	char line[80];
582	int ret = 0;
583
584	if (STAILQ_EMPTY(&zfs_pools))
585		return (0);
586
587	printf("%s devices:", zfs_dev.dv_name);
588	if ((ret = pager_output("\n")) != 0)
589		return (ret);
590
591	if (verbose) {
592		return (spa_all_status());
593	}
594	STAILQ_FOREACH(spa, &zfs_pools, spa_link) {
595		sprintf(line, "    zfs:%s\n", spa->spa_name);
596		ret = pager_output(line);
597		if (ret != 0)
598			break;
599	}
600	return (ret);
601}
602
603/*
604 * Attempt to open the pool described by (dev) for use by (f).
605 */
606static int
607zfs_dev_open(struct open_file *f, ...)
608{
609	va_list		args;
610	struct zfs_devdesc	*dev;
611	struct zfsmount	*mount;
612	spa_t		*spa;
613	int		rv;
614
615	va_start(args, f);
616	dev = va_arg(args, struct zfs_devdesc *);
617	va_end(args);
618
619	if (dev->pool_guid == 0)
620		spa = STAILQ_FIRST(&zfs_pools);
621	else
622		spa = spa_find_by_guid(dev->pool_guid);
623	if (!spa)
624		return (ENXIO);
625	mount = malloc(sizeof(*mount));
626	rv = zfs_mount(spa, dev->root_guid, mount);
627	if (rv != 0) {
628		free(mount);
629		return (rv);
630	}
631	if (mount->objset.os_type != DMU_OST_ZFS) {
632		printf("Unexpected object set type %ju\n",
633		    (uintmax_t)mount->objset.os_type);
634		free(mount);
635		return (EIO);
636	}
637	f->f_devdata = mount;
638	free(dev);
639	return (0);
640}
641
642static int
643zfs_dev_close(struct open_file *f)
644{
645
646	free(f->f_devdata);
647	f->f_devdata = NULL;
648	return (0);
649}
650
651static int
652zfs_dev_strategy(void *devdata __unused, int rw __unused,
653    daddr_t dblk __unused, size_t size __unused,
654    char *buf __unused, size_t *rsize __unused)
655{
656
657	return (ENOSYS);
658}
659
660struct devsw zfs_dev = {
661	.dv_name = "zfs",
662	.dv_type = DEVT_ZFS,
663	.dv_init = zfs_dev_init,
664	.dv_strategy = zfs_dev_strategy,
665	.dv_open = zfs_dev_open,
666	.dv_close = zfs_dev_close,
667	.dv_ioctl = noioctl,
668	.dv_print = zfs_dev_print,
669	.dv_cleanup = NULL
670};
671
672int
673zfs_parsedev(struct zfs_devdesc *dev, const char *devspec, const char **path)
674{
675	static char	rootname[ZFS_MAXNAMELEN];
676	static char	poolname[ZFS_MAXNAMELEN];
677	spa_t		*spa;
678	const char	*end;
679	const char	*np;
680	const char	*sep;
681	int		rv;
682
683	np = devspec;
684	if (*np != ':')
685		return (EINVAL);
686	np++;
687	end = strrchr(np, ':');
688	if (end == NULL)
689		return (EINVAL);
690	sep = strchr(np, '/');
691	if (sep == NULL || sep >= end)
692		sep = end;
693	memcpy(poolname, np, sep - np);
694	poolname[sep - np] = '\0';
695	if (sep < end) {
696		sep++;
697		memcpy(rootname, sep, end - sep);
698		rootname[end - sep] = '\0';
699	}
700	else
701		rootname[0] = '\0';
702
703	spa = spa_find_by_name(poolname);
704	if (!spa)
705		return (ENXIO);
706	dev->pool_guid = spa->spa_guid;
707	rv = zfs_lookup_dataset(spa, rootname, &dev->root_guid);
708	if (rv != 0)
709		return (rv);
710	if (path != NULL)
711		*path = (*end == '\0') ? end : end + 1;
712	dev->dd.d_dev = &zfs_dev;
713	return (0);
714}
715
716char *
717zfs_bootfs(void *zdev)
718{
719	static char		rootname[ZFS_MAXNAMELEN];
720	static char		buf[2 * ZFS_MAXNAMELEN];
721	struct zfs_devdesc	*dev = (struct zfs_devdesc *)zdev;
722	uint64_t		objnum;
723	spa_t			*spa;
724	int			n;
725
726	buf[0] = '\0';
727	if (dev->dd.d_dev->dv_type != DEVT_ZFS)
728		return (buf);
729
730	spa = spa_find_by_guid(dev->pool_guid);
731	if (spa == NULL) {
732		printf("ZFS: can't find pool by guid\n");
733		return (buf);
734	}
735	if (zfs_rlookup(spa, dev->root_guid, rootname)) {
736		printf("ZFS: can't find filesystem by guid\n");
737		return (buf);
738	}
739	if (zfs_lookup_dataset(spa, rootname, &objnum)) {
740		printf("ZFS: can't find filesystem by name\n");
741		return (buf);
742	}
743
744	/* Set the environment. */
745	snprintf(buf, sizeof (buf), "%" PRIu64, dev->pool_guid);
746	setenv("zfs-bootpool", buf, 1);
747	snprintf(buf, sizeof (buf), "%" PRIu64, spa->spa_boot_vdev->v_guid);
748	setenv("zfs-bootvdev", buf, 1);
749	snprintf(buf, sizeof (buf), "%s/%" PRIu64, spa->spa_name, objnum);
750	setenv("zfs-bootfs", buf, 1);
751	if (spa->spa_boot_vdev->v_phys_path != NULL)
752		setenv("bootpath", spa->spa_boot_vdev->v_phys_path, 1);
753	if (spa->spa_boot_vdev->v_devid != NULL)
754		setenv("diskdevid", spa->spa_boot_vdev->v_devid, 1);
755
756	/*
757	 * Build the command line string. Once our kernel will read
758	 * the environment and we can stop caring about old kernels,
759	 * we can remove this part.
760	 */
761	snprintf(buf, sizeof(buf), "zfs-bootfs=%s/%" PRIu64, spa->spa_name,
762	    objnum);
763	n = strlen(buf);
764	if (spa->spa_boot_vdev->v_phys_path != NULL) {
765		snprintf(buf+n, sizeof (buf) - n, ",bootpath=\"%s\"",
766		    spa->spa_boot_vdev->v_phys_path);
767		n = strlen(buf);
768	}
769	if (spa->spa_boot_vdev->v_devid != NULL) {
770		snprintf(buf+n, sizeof (buf) - n, ",diskdevid=\"%s\"",
771		    spa->spa_boot_vdev->v_devid);
772	}
773	return (buf);
774}
775
776char *
777zfs_fmtdev(void *vdev)
778{
779	static char		rootname[ZFS_MAXNAMELEN];
780	static char		buf[2 * ZFS_MAXNAMELEN + 8];
781	struct zfs_devdesc	*dev = (struct zfs_devdesc *)vdev;
782	spa_t			*spa;
783
784	buf[0] = '\0';
785	if (dev->dd.d_dev->dv_type != DEVT_ZFS)
786		return (buf);
787
788	if (dev->pool_guid == 0) {
789		spa = STAILQ_FIRST(&zfs_pools);
790		dev->pool_guid = spa->spa_guid;
791	} else
792		spa = spa_find_by_guid(dev->pool_guid);
793	if (spa == NULL) {
794		printf("ZFS: can't find pool by guid\n");
795		return (buf);
796	}
797	if (dev->root_guid == 0 && zfs_get_root(spa, &dev->root_guid)) {
798		printf("ZFS: can't find root filesystem\n");
799		return (buf);
800	}
801	if (zfs_rlookup(spa, dev->root_guid, rootname)) {
802		printf("ZFS: can't find filesystem by guid\n");
803		return (buf);
804	}
805
806	if (rootname[0] == '\0')
807		sprintf(buf, "%s:%s:", dev->dd.d_dev->dv_name, spa->spa_name);
808	else
809		sprintf(buf, "%s:%s/%s:", dev->dd.d_dev->dv_name, spa->spa_name,
810		    rootname);
811	return (buf);
812}
813
814int
815zfs_list(const char *name)
816{
817	static char	poolname[ZFS_MAXNAMELEN];
818	uint64_t	objid;
819	spa_t		*spa;
820	const char	*dsname;
821	int		len;
822	int		rv;
823
824	len = strlen(name);
825	dsname = strchr(name, '/');
826	if (dsname != NULL) {
827		len = dsname - name;
828		dsname++;
829	} else
830		dsname = "";
831	memcpy(poolname, name, len);
832	poolname[len] = '\0';
833
834	spa = spa_find_by_name(poolname);
835	if (!spa)
836		return (ENXIO);
837	rv = zfs_lookup_dataset(spa, dsname, &objid);
838	if (rv != 0)
839		return (rv);
840
841	return (zfs_list_dataset(spa, objid));
842}
843
844#ifdef __FreeBSD__
845void
846init_zfs_bootenv(char *currdev)
847{
848	char *beroot;
849
850	if (strlen(currdev) == 0)
851		return;
852	if(strncmp(currdev, "zfs:", 4) != 0)
853		return;
854	/* Remove the trailing : */
855	currdev[strlen(currdev) - 1] = '\0';
856	setenv("zfs_be_active", currdev, 1);
857	setenv("zfs_be_currpage", "1", 1);
858	/* Forward past zfs: */
859	currdev = strchr(currdev, ':');
860	currdev++;
861	/* Remove the last element (current bootenv) */
862	beroot = strrchr(currdev, '/');
863	if (beroot != NULL)
864		beroot[0] = '\0';
865	beroot = currdev;
866	setenv("zfs_be_root", beroot, 1);
867}
868
869int
870zfs_bootenv(const char *name)
871{
872	static char	poolname[ZFS_MAXNAMELEN], *dsname, *root;
873	char		becount[4];
874	uint64_t	objid;
875	spa_t		*spa;
876	int		len, rv, pages, perpage, currpage;
877
878	if (name == NULL)
879		return (EINVAL);
880	if ((root = getenv("zfs_be_root")) == NULL)
881		return (EINVAL);
882
883	if (strcmp(name, root) != 0) {
884		if (setenv("zfs_be_root", name, 1) != 0)
885			return (ENOMEM);
886	}
887
888	SLIST_INIT(&zfs_be_head);
889	zfs_env_count = 0;
890	len = strlen(name);
891	dsname = strchr(name, '/');
892	if (dsname != NULL) {
893		len = dsname - name;
894		dsname++;
895	} else
896		dsname = "";
897	memcpy(poolname, name, len);
898	poolname[len] = '\0';
899
900	spa = spa_find_by_name(poolname);
901	if (!spa)
902		return (ENXIO);
903	rv = zfs_lookup_dataset(spa, dsname, &objid);
904	if (rv != 0)
905		return (rv);
906	rv = zfs_callback_dataset(spa, objid, zfs_belist_add);
907
908	/* Calculate and store the number of pages of BEs */
909	perpage = (ZFS_BE_LAST - ZFS_BE_FIRST + 1);
910	pages = (zfs_env_count / perpage) + ((zfs_env_count % perpage) > 0 ? 1 : 0);
911	snprintf(becount, 4, "%d", pages);
912	if (setenv("zfs_be_pages", becount, 1) != 0)
913		return (ENOMEM);
914
915	/* Roll over the page counter if it has exceeded the maximum */
916	currpage = strtol(getenv("zfs_be_currpage"), NULL, 10);
917	if (currpage > pages) {
918		if (setenv("zfs_be_currpage", "1", 1) != 0)
919			return (ENOMEM);
920	}
921
922	/* Populate the menu environment variables */
923	zfs_set_env();
924
925	/* Clean up the SLIST of ZFS BEs */
926	while (!SLIST_EMPTY(&zfs_be_head)) {
927		zfs_be = SLIST_FIRST(&zfs_be_head);
928		SLIST_REMOVE_HEAD(&zfs_be_head, entries);
929		free(zfs_be);
930	}
931
932	return (rv);
933}
934
935int
936zfs_belist_add(const char *name, uint64_t value __unused)
937{
938
939	/* Skip special datasets that start with a $ character */
940	if (strncmp(name, "$", 1) == 0) {
941		return (0);
942	}
943	/* Add the boot environment to the head of the SLIST */
944	zfs_be = malloc(sizeof(struct zfs_be_entry));
945	if (zfs_be == NULL) {
946		return (ENOMEM);
947	}
948	zfs_be->name = name;
949	SLIST_INSERT_HEAD(&zfs_be_head, zfs_be, entries);
950	zfs_env_count++;
951
952	return (0);
953}
954
955int
956zfs_set_env(void)
957{
958	char envname[32], envval[256];
959	char *beroot, *pagenum;
960	int rv, page, ctr;
961
962	beroot = getenv("zfs_be_root");
963	if (beroot == NULL) {
964		return (1);
965	}
966
967	pagenum = getenv("zfs_be_currpage");
968	if (pagenum != NULL) {
969		page = strtol(pagenum, NULL, 10);
970	} else {
971		page = 1;
972	}
973
974	ctr = 1;
975	rv = 0;
976	zfs_env_index = ZFS_BE_FIRST;
977	SLIST_FOREACH_SAFE(zfs_be, &zfs_be_head, entries, zfs_be_tmp) {
978		/* Skip to the requested page number */
979		if (ctr <= ((ZFS_BE_LAST - ZFS_BE_FIRST + 1) * (page - 1))) {
980			ctr++;
981			continue;
982		}
983
984		snprintf(envname, sizeof(envname), "bootenvmenu_caption[%d]", zfs_env_index);
985		snprintf(envval, sizeof(envval), "%s", zfs_be->name);
986		rv = setenv(envname, envval, 1);
987		if (rv != 0) {
988			break;
989		}
990
991		snprintf(envname, sizeof(envname), "bootenvansi_caption[%d]", zfs_env_index);
992		rv = setenv(envname, envval, 1);
993		if (rv != 0){
994			break;
995		}
996
997		snprintf(envname, sizeof(envname), "bootenvmenu_command[%d]", zfs_env_index);
998		rv = setenv(envname, "set_bootenv", 1);
999		if (rv != 0){
1000			break;
1001		}
1002
1003		snprintf(envname, sizeof(envname), "bootenv_root[%d]", zfs_env_index);
1004		snprintf(envval, sizeof(envval), "zfs:%s/%s", beroot, zfs_be->name);
1005		rv = setenv(envname, envval, 1);
1006		if (rv != 0){
1007			break;
1008		}
1009
1010		zfs_env_index++;
1011		if (zfs_env_index > ZFS_BE_LAST) {
1012			break;
1013		}
1014
1015	}
1016
1017	for (; zfs_env_index <= ZFS_BE_LAST; zfs_env_index++) {
1018		snprintf(envname, sizeof(envname), "bootenvmenu_caption[%d]", zfs_env_index);
1019		(void)unsetenv(envname);
1020		snprintf(envname, sizeof(envname), "bootenvansi_caption[%d]", zfs_env_index);
1021		(void)unsetenv(envname);
1022		snprintf(envname, sizeof(envname), "bootenvmenu_command[%d]", zfs_env_index);
1023		(void)unsetenv(envname);
1024		snprintf(envname, sizeof(envname), "bootenv_root[%d]", zfs_env_index);
1025		(void)unsetenv(envname);
1026	}
1027
1028	return (rv);
1029}
1030#endif
1031