xref: /freebsd/stand/libsa/zfs/zfs.c (revision 2f513db72b034fd5ef7f080b11be5c711c15186a)
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  *	$FreeBSD$
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 /*
33  *	Stand-alone file reading package.
34  */
35 
36 #include <stand.h>
37 #include <sys/disk.h>
38 #include <sys/param.h>
39 #include <sys/time.h>
40 #include <sys/queue.h>
41 #include <disk.h>
42 #include <part.h>
43 #include <stddef.h>
44 #include <stdarg.h>
45 #include <string.h>
46 #include <bootstrap.h>
47 
48 #include "libzfs.h"
49 
50 #include "zfsimpl.c"
51 
52 /* Define the range of indexes to be populated with ZFS Boot Environments */
53 #define		ZFS_BE_FIRST	4
54 #define		ZFS_BE_LAST	8
55 
56 static int	zfs_open(const char *path, struct open_file *f);
57 static int	zfs_close(struct open_file *f);
58 static int	zfs_read(struct open_file *f, void *buf, size_t size, size_t *resid);
59 static off_t	zfs_seek(struct open_file *f, off_t offset, int where);
60 static int	zfs_stat(struct open_file *f, struct stat *sb);
61 static int	zfs_readdir(struct open_file *f, struct dirent *d);
62 
63 static void	zfs_bootenv_initial(const char *);
64 
65 struct devsw zfs_dev;
66 
67 struct fs_ops zfs_fsops = {
68 	"zfs",
69 	zfs_open,
70 	zfs_close,
71 	zfs_read,
72 	null_write,
73 	zfs_seek,
74 	zfs_stat,
75 	zfs_readdir
76 };
77 
78 /*
79  * In-core open file.
80  */
81 struct file {
82 	off_t		f_seekp;	/* seek pointer */
83 	dnode_phys_t	f_dnode;
84 	uint64_t	f_zap_type;	/* zap type for readdir */
85 	uint64_t	f_num_leafs;	/* number of fzap leaf blocks */
86 	zap_leaf_phys_t	*f_zap_leaf;	/* zap leaf buffer */
87 };
88 
89 static int	zfs_env_index;
90 static int	zfs_env_count;
91 
92 SLIST_HEAD(zfs_be_list, zfs_be_entry) zfs_be_head = SLIST_HEAD_INITIALIZER(zfs_be_head);
93 struct zfs_be_list *zfs_be_headp;
94 struct zfs_be_entry {
95 	const char *name;
96 	SLIST_ENTRY(zfs_be_entry) entries;
97 } *zfs_be, *zfs_be_tmp;
98 
99 /*
100  * Open a file.
101  */
102 static int
103 zfs_open(const char *upath, struct open_file *f)
104 {
105 	struct zfsmount *mount = (struct zfsmount *)f->f_devdata;
106 	struct file *fp;
107 	int rc;
108 
109 	if (f->f_dev != &zfs_dev)
110 		return (EINVAL);
111 
112 	/* allocate file system specific data structure */
113 	fp = calloc(1, sizeof(struct file));
114 	if (fp == NULL)
115 		return (ENOMEM);
116 	f->f_fsdata = fp;
117 
118 	rc = zfs_lookup(mount, upath, &fp->f_dnode);
119 	fp->f_seekp = 0;
120 	if (rc) {
121 		f->f_fsdata = NULL;
122 		free(fp);
123 	}
124 	return (rc);
125 }
126 
127 static int
128 zfs_close(struct open_file *f)
129 {
130 	struct file *fp = (struct file *)f->f_fsdata;
131 
132 	dnode_cache_obj = NULL;
133 	f->f_fsdata = NULL;
134 
135 	free(fp);
136 	return (0);
137 }
138 
139 /*
140  * Copy a portion of a file into kernel memory.
141  * Cross block boundaries when necessary.
142  */
143 static int
144 zfs_read(struct open_file *f, void *start, size_t size, size_t *resid	/* out */)
145 {
146 	const spa_t *spa = ((struct zfsmount *)f->f_devdata)->spa;
147 	struct file *fp = (struct file *)f->f_fsdata;
148 	struct stat sb;
149 	size_t n;
150 	int rc;
151 
152 	rc = zfs_stat(f, &sb);
153 	if (rc)
154 		return (rc);
155 	n = size;
156 	if (fp->f_seekp + n > sb.st_size)
157 		n = sb.st_size - fp->f_seekp;
158 
159 	rc = dnode_read(spa, &fp->f_dnode, fp->f_seekp, start, n);
160 	if (rc)
161 		return (rc);
162 
163 	if (0) {
164 	    int i;
165 	    for (i = 0; i < n; i++)
166 		putchar(((char*) start)[i]);
167 	}
168 	fp->f_seekp += n;
169 	if (resid)
170 		*resid = size - n;
171 
172 	return (0);
173 }
174 
175 static off_t
176 zfs_seek(struct open_file *f, off_t offset, int where)
177 {
178 	struct file *fp = (struct file *)f->f_fsdata;
179 
180 	switch (where) {
181 	case SEEK_SET:
182 		fp->f_seekp = offset;
183 		break;
184 	case SEEK_CUR:
185 		fp->f_seekp += offset;
186 		break;
187 	case SEEK_END:
188 	    {
189 		struct stat sb;
190 		int error;
191 
192 		error = zfs_stat(f, &sb);
193 		if (error != 0) {
194 			errno = error;
195 			return (-1);
196 		}
197 		fp->f_seekp = sb.st_size - offset;
198 		break;
199 	    }
200 	default:
201 		errno = EINVAL;
202 		return (-1);
203 	}
204 	return (fp->f_seekp);
205 }
206 
207 static int
208 zfs_stat(struct open_file *f, struct stat *sb)
209 {
210 	const spa_t *spa = ((struct zfsmount *)f->f_devdata)->spa;
211 	struct file *fp = (struct file *)f->f_fsdata;
212 
213 	return (zfs_dnode_stat(spa, &fp->f_dnode, sb));
214 }
215 
216 static int
217 zfs_readdir(struct open_file *f, struct dirent *d)
218 {
219 	const spa_t *spa = ((struct zfsmount *)f->f_devdata)->spa;
220 	struct file *fp = (struct file *)f->f_fsdata;
221 	mzap_ent_phys_t mze;
222 	struct stat sb;
223 	size_t bsize = fp->f_dnode.dn_datablkszsec << SPA_MINBLOCKSHIFT;
224 	int rc;
225 
226 	rc = zfs_stat(f, &sb);
227 	if (rc)
228 		return (rc);
229 	if (!S_ISDIR(sb.st_mode))
230 		return (ENOTDIR);
231 
232 	/*
233 	 * If this is the first read, get the zap type.
234 	 */
235 	if (fp->f_seekp == 0) {
236 		rc = dnode_read(spa, &fp->f_dnode,
237 				0, &fp->f_zap_type, sizeof(fp->f_zap_type));
238 		if (rc)
239 			return (rc);
240 
241 		if (fp->f_zap_type == ZBT_MICRO) {
242 			fp->f_seekp = offsetof(mzap_phys_t, mz_chunk);
243 		} else {
244 			rc = dnode_read(spa, &fp->f_dnode,
245 					offsetof(zap_phys_t, zap_num_leafs),
246 					&fp->f_num_leafs,
247 					sizeof(fp->f_num_leafs));
248 			if (rc)
249 				return (rc);
250 
251 			fp->f_seekp = bsize;
252 			fp->f_zap_leaf = malloc(bsize);
253 			if (fp->f_zap_leaf == NULL)
254 				return (ENOMEM);
255 			rc = dnode_read(spa, &fp->f_dnode,
256 					fp->f_seekp,
257 					fp->f_zap_leaf,
258 					bsize);
259 			if (rc)
260 				return (rc);
261 		}
262 	}
263 
264 	if (fp->f_zap_type == ZBT_MICRO) {
265 	mzap_next:
266 		if (fp->f_seekp >= bsize)
267 			return (ENOENT);
268 
269 		rc = dnode_read(spa, &fp->f_dnode,
270 				fp->f_seekp, &mze, sizeof(mze));
271 		if (rc)
272 			return (rc);
273 		fp->f_seekp += sizeof(mze);
274 
275 		if (!mze.mze_name[0])
276 			goto mzap_next;
277 
278 		d->d_fileno = ZFS_DIRENT_OBJ(mze.mze_value);
279 		d->d_type = ZFS_DIRENT_TYPE(mze.mze_value);
280 		strcpy(d->d_name, mze.mze_name);
281 		d->d_namlen = strlen(d->d_name);
282 		return (0);
283 	} else {
284 		zap_leaf_t zl;
285 		zap_leaf_chunk_t *zc, *nc;
286 		int chunk;
287 		size_t namelen;
288 		char *p;
289 		uint64_t value;
290 
291 		/*
292 		 * Initialise this so we can use the ZAP size
293 		 * calculating macros.
294 		 */
295 		zl.l_bs = ilog2(bsize);
296 		zl.l_phys = fp->f_zap_leaf;
297 
298 		/*
299 		 * Figure out which chunk we are currently looking at
300 		 * and consider seeking to the next leaf. We use the
301 		 * low bits of f_seekp as a simple chunk index.
302 		 */
303 	fzap_next:
304 		chunk = fp->f_seekp & (bsize - 1);
305 		if (chunk == ZAP_LEAF_NUMCHUNKS(&zl)) {
306 			fp->f_seekp = rounddown2(fp->f_seekp, bsize) + bsize;
307 			chunk = 0;
308 
309 			/*
310 			 * Check for EOF and read the new leaf.
311 			 */
312 			if (fp->f_seekp >= bsize * fp->f_num_leafs)
313 				return (ENOENT);
314 
315 			rc = dnode_read(spa, &fp->f_dnode,
316 					fp->f_seekp,
317 					fp->f_zap_leaf,
318 					bsize);
319 			if (rc)
320 				return (rc);
321 		}
322 
323 		zc = &ZAP_LEAF_CHUNK(&zl, chunk);
324 		fp->f_seekp++;
325 		if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
326 			goto fzap_next;
327 
328 		namelen = zc->l_entry.le_name_numints;
329 		if (namelen > sizeof(d->d_name))
330 			namelen = sizeof(d->d_name);
331 
332 		/*
333 		 * Paste the name back together.
334 		 */
335 		nc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_name_chunk);
336 		p = d->d_name;
337 		while (namelen > 0) {
338 			int len;
339 			len = namelen;
340 			if (len > ZAP_LEAF_ARRAY_BYTES)
341 				len = ZAP_LEAF_ARRAY_BYTES;
342 			memcpy(p, nc->l_array.la_array, len);
343 			p += len;
344 			namelen -= len;
345 			nc = &ZAP_LEAF_CHUNK(&zl, nc->l_array.la_next);
346 		}
347 		d->d_name[sizeof(d->d_name) - 1] = 0;
348 
349 		/*
350 		 * Assume the first eight bytes of the value are
351 		 * a uint64_t.
352 		 */
353 		value = fzap_leaf_value(&zl, zc);
354 
355 		d->d_fileno = ZFS_DIRENT_OBJ(value);
356 		d->d_type = ZFS_DIRENT_TYPE(value);
357 		d->d_namlen = strlen(d->d_name);
358 
359 		return (0);
360 	}
361 }
362 
363 static int
364 vdev_read(vdev_t *vdev, void *priv, off_t offset, void *buf, size_t bytes)
365 {
366 	int fd, ret;
367 	size_t res, head, tail, total_size, full_sec_size;
368 	unsigned secsz, do_tail_read;
369 	off_t start_sec;
370 	char *outbuf, *bouncebuf;
371 
372 	fd = (uintptr_t) priv;
373 	outbuf = (char *) buf;
374 	bouncebuf = NULL;
375 
376 	ret = ioctl(fd, DIOCGSECTORSIZE, &secsz);
377 	if (ret != 0)
378 		return (ret);
379 
380 	/*
381 	 * Handling reads of arbitrary offset and size - multi-sector case
382 	 * and single-sector case.
383 	 *
384 	 *                        Multi-sector Case
385 	 *                (do_tail_read = true if tail > 0)
386 	 *
387 	 *   |<----------------------total_size--------------------->|
388 	 *   |                                                       |
389 	 *   |<--head-->|<--------------bytes------------>|<--tail-->|
390 	 *   |          |                                 |          |
391 	 *   |          |       |<~full_sec_size~>|       |          |
392 	 *   +------------------+                 +------------------+
393 	 *   |          |0101010|     .  .  .     |0101011|          |
394 	 *   +------------------+                 +------------------+
395 	 *         start_sec                         start_sec + n
396 	 *
397 	 *
398 	 *                      Single-sector Case
399 	 *                    (do_tail_read = false)
400 	 *
401 	 *              |<------total_size = secsz----->|
402 	 *              |                               |
403 	 *              |<-head->|<---bytes--->|<-tail->|
404 	 *              +-------------------------------+
405 	 *              |        |0101010101010|        |
406 	 *              +-------------------------------+
407 	 *                          start_sec
408 	 */
409 	start_sec = offset / secsz;
410 	head = offset % secsz;
411 	total_size = roundup2(head + bytes, secsz);
412 	tail = total_size - (head + bytes);
413 	do_tail_read = ((tail > 0) && (head + bytes > secsz));
414 	full_sec_size = total_size;
415 	if (head > 0)
416 		full_sec_size -= secsz;
417 	if (do_tail_read)
418 		full_sec_size -= secsz;
419 
420 	/* Return of partial sector data requires a bounce buffer. */
421 	if ((head > 0) || do_tail_read) {
422 		bouncebuf = zfs_alloc(secsz);
423 		if (bouncebuf == NULL) {
424 			printf("vdev_read: out of memory\n");
425 			return (ENOMEM);
426 		}
427 	}
428 
429 	if (lseek(fd, start_sec * secsz, SEEK_SET) == -1) {
430 		ret = errno;
431 		goto error;
432 	}
433 
434 	/* Partial data return from first sector */
435 	if (head > 0) {
436 		res = read(fd, bouncebuf, secsz);
437 		if (res != secsz) {
438 			ret = EIO;
439 			goto error;
440 		}
441 		memcpy(outbuf, bouncebuf + head, min(secsz - head, bytes));
442 		outbuf += min(secsz - head, bytes);
443 	}
444 
445 	/* Full data return from read sectors */
446 	if (full_sec_size > 0) {
447 		res = read(fd, outbuf, full_sec_size);
448 		if (res != full_sec_size) {
449 			ret = EIO;
450 			goto error;
451 		}
452 		outbuf += full_sec_size;
453 	}
454 
455 	/* Partial data return from last sector */
456 	if (do_tail_read) {
457 		res = read(fd, bouncebuf, secsz);
458 		if (res != secsz) {
459 			ret = EIO;
460 			goto error;
461 		}
462 		memcpy(outbuf, bouncebuf, secsz - tail);
463 	}
464 
465 	ret = 0;
466 error:
467 	if (bouncebuf != NULL)
468 		zfs_free(bouncebuf, secsz);
469 	return (ret);
470 }
471 
472 static int
473 zfs_dev_init(void)
474 {
475 	spa_t *spa;
476 	spa_t *next;
477 	spa_t *prev;
478 
479 	zfs_init();
480 	if (archsw.arch_zfs_probe == NULL)
481 		return (ENXIO);
482 	archsw.arch_zfs_probe();
483 
484 	prev = NULL;
485 	spa = STAILQ_FIRST(&zfs_pools);
486 	while (spa != NULL) {
487 		next = STAILQ_NEXT(spa, spa_link);
488 		if (zfs_spa_init(spa)) {
489 			if (prev == NULL)
490 				STAILQ_REMOVE_HEAD(&zfs_pools, spa_link);
491 			else
492 				STAILQ_REMOVE_AFTER(&zfs_pools, prev, spa_link);
493 		} else
494 			prev = spa;
495 		spa = next;
496 	}
497 	return (0);
498 }
499 
500 struct zfs_probe_args {
501 	int		fd;
502 	const char	*devname;
503 	uint64_t	*pool_guid;
504 	u_int		secsz;
505 };
506 
507 static int
508 zfs_diskread(void *arg, void *buf, size_t blocks, uint64_t offset)
509 {
510 	struct zfs_probe_args *ppa;
511 
512 	ppa = (struct zfs_probe_args *)arg;
513 	return (vdev_read(NULL, (void *)(uintptr_t)ppa->fd,
514 	    offset * ppa->secsz, buf, blocks * ppa->secsz));
515 }
516 
517 static int
518 zfs_probe(int fd, uint64_t *pool_guid)
519 {
520 	spa_t *spa;
521 	int ret;
522 
523 	spa = NULL;
524 	ret = vdev_probe(vdev_read, (void *)(uintptr_t)fd, &spa);
525 	if (ret == 0 && pool_guid != NULL)
526 		*pool_guid = spa->spa_guid;
527 	return (ret);
528 }
529 
530 static int
531 zfs_probe_partition(void *arg, const char *partname,
532     const struct ptable_entry *part)
533 {
534 	struct zfs_probe_args *ppa, pa;
535 	struct ptable *table;
536 	char devname[32];
537 	int ret;
538 
539 	/* Probe only freebsd-zfs and freebsd partitions */
540 	if (part->type != PART_FREEBSD &&
541 	    part->type != PART_FREEBSD_ZFS)
542 		return (0);
543 
544 	ppa = (struct zfs_probe_args *)arg;
545 	strncpy(devname, ppa->devname, strlen(ppa->devname) - 1);
546 	devname[strlen(ppa->devname) - 1] = '\0';
547 	sprintf(devname, "%s%s:", devname, partname);
548 	pa.fd = open(devname, O_RDONLY);
549 	if (pa.fd == -1)
550 		return (0);
551 	ret = zfs_probe(pa.fd, ppa->pool_guid);
552 	if (ret == 0)
553 		return (0);
554 	/* Do we have BSD label here? */
555 	if (part->type == PART_FREEBSD) {
556 		pa.devname = devname;
557 		pa.pool_guid = ppa->pool_guid;
558 		pa.secsz = ppa->secsz;
559 		table = ptable_open(&pa, part->end - part->start + 1,
560 		    ppa->secsz, zfs_diskread);
561 		if (table != NULL) {
562 			ptable_iterate(table, &pa, zfs_probe_partition);
563 			ptable_close(table);
564 		}
565 	}
566 	close(pa.fd);
567 	return (0);
568 }
569 
570 int
571 zfs_probe_dev(const char *devname, uint64_t *pool_guid)
572 {
573 	struct disk_devdesc *dev;
574 	struct ptable *table;
575 	struct zfs_probe_args pa;
576 	uint64_t mediasz;
577 	int ret;
578 
579 	if (pool_guid)
580 		*pool_guid = 0;
581 	pa.fd = open(devname, O_RDONLY);
582 	if (pa.fd == -1)
583 		return (ENXIO);
584 	/*
585 	 * We will not probe the whole disk, we can not boot from such
586 	 * disks and some systems will misreport the disk sizes and will
587 	 * hang while accessing the disk.
588 	 */
589 	if (archsw.arch_getdev((void **)&dev, devname, NULL) == 0) {
590 		int partition = dev->d_partition;
591 		int slice = dev->d_slice;
592 
593 		free(dev);
594 		if (partition != D_PARTNONE && slice != D_SLICENONE) {
595 			ret = zfs_probe(pa.fd, pool_guid);
596 			if (ret == 0)
597 				return (0);
598 		}
599 	}
600 
601 	/* Probe each partition */
602 	ret = ioctl(pa.fd, DIOCGMEDIASIZE, &mediasz);
603 	if (ret == 0)
604 		ret = ioctl(pa.fd, DIOCGSECTORSIZE, &pa.secsz);
605 	if (ret == 0) {
606 		pa.devname = devname;
607 		pa.pool_guid = pool_guid;
608 		table = ptable_open(&pa, mediasz / pa.secsz, pa.secsz,
609 		    zfs_diskread);
610 		if (table != NULL) {
611 			ptable_iterate(table, &pa, zfs_probe_partition);
612 			ptable_close(table);
613 		}
614 	}
615 	close(pa.fd);
616 	if (pool_guid && *pool_guid == 0)
617 		ret = ENXIO;
618 	return (ret);
619 }
620 
621 /*
622  * Print information about ZFS pools
623  */
624 static int
625 zfs_dev_print(int verbose)
626 {
627 	spa_t *spa;
628 	char line[80];
629 	int ret = 0;
630 
631 	if (STAILQ_EMPTY(&zfs_pools))
632 		return (0);
633 
634 	printf("%s devices:", zfs_dev.dv_name);
635 	if ((ret = pager_output("\n")) != 0)
636 		return (ret);
637 
638 	if (verbose) {
639 		return (spa_all_status());
640 	}
641 	STAILQ_FOREACH(spa, &zfs_pools, spa_link) {
642 		snprintf(line, sizeof(line), "    zfs:%s\n", spa->spa_name);
643 		ret = pager_output(line);
644 		if (ret != 0)
645 			break;
646 	}
647 	return (ret);
648 }
649 
650 /*
651  * Attempt to open the pool described by (dev) for use by (f).
652  */
653 static int
654 zfs_dev_open(struct open_file *f, ...)
655 {
656 	va_list		args;
657 	struct zfs_devdesc	*dev;
658 	struct zfsmount	*mount;
659 	spa_t		*spa;
660 	int		rv;
661 
662 	va_start(args, f);
663 	dev = va_arg(args, struct zfs_devdesc *);
664 	va_end(args);
665 
666 	if (dev->pool_guid == 0)
667 		spa = STAILQ_FIRST(&zfs_pools);
668 	else
669 		spa = spa_find_by_guid(dev->pool_guid);
670 	if (!spa)
671 		return (ENXIO);
672 	mount = malloc(sizeof(*mount));
673 	if (mount == NULL)
674 		rv = ENOMEM;
675 	else
676 		rv = zfs_mount(spa, dev->root_guid, mount);
677 	if (rv != 0) {
678 		free(mount);
679 		return (rv);
680 	}
681 	if (mount->objset.os_type != DMU_OST_ZFS) {
682 		printf("Unexpected object set type %ju\n",
683 		    (uintmax_t)mount->objset.os_type);
684 		free(mount);
685 		return (EIO);
686 	}
687 	f->f_devdata = mount;
688 	free(dev);
689 	return (0);
690 }
691 
692 static int
693 zfs_dev_close(struct open_file *f)
694 {
695 
696 	free(f->f_devdata);
697 	f->f_devdata = NULL;
698 	return (0);
699 }
700 
701 static int
702 zfs_dev_strategy(void *devdata, int rw, daddr_t dblk, size_t size, char *buf, size_t *rsize)
703 {
704 
705 	return (ENOSYS);
706 }
707 
708 struct devsw zfs_dev = {
709 	.dv_name = "zfs",
710 	.dv_type = DEVT_ZFS,
711 	.dv_init = zfs_dev_init,
712 	.dv_strategy = zfs_dev_strategy,
713 	.dv_open = zfs_dev_open,
714 	.dv_close = zfs_dev_close,
715 	.dv_ioctl = noioctl,
716 	.dv_print = zfs_dev_print,
717 	.dv_cleanup = NULL
718 };
719 
720 int
721 zfs_parsedev(struct zfs_devdesc *dev, const char *devspec, const char **path)
722 {
723 	static char	rootname[ZFS_MAXNAMELEN];
724 	static char	poolname[ZFS_MAXNAMELEN];
725 	spa_t		*spa;
726 	const char	*end;
727 	const char	*np;
728 	const char	*sep;
729 	int		rv;
730 
731 	np = devspec;
732 	if (*np != ':')
733 		return (EINVAL);
734 	np++;
735 	end = strrchr(np, ':');
736 	if (end == NULL)
737 		return (EINVAL);
738 	sep = strchr(np, '/');
739 	if (sep == NULL || sep >= end)
740 		sep = end;
741 	memcpy(poolname, np, sep - np);
742 	poolname[sep - np] = '\0';
743 	if (sep < end) {
744 		sep++;
745 		memcpy(rootname, sep, end - sep);
746 		rootname[end - sep] = '\0';
747 	}
748 	else
749 		rootname[0] = '\0';
750 
751 	spa = spa_find_by_name(poolname);
752 	if (!spa)
753 		return (ENXIO);
754 	dev->pool_guid = spa->spa_guid;
755 	rv = zfs_lookup_dataset(spa, rootname, &dev->root_guid);
756 	if (rv != 0)
757 		return (rv);
758 	if (path != NULL)
759 		*path = (*end == '\0') ? end : end + 1;
760 	dev->dd.d_dev = &zfs_dev;
761 	return (0);
762 }
763 
764 char *
765 zfs_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 
807 int
808 zfs_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 
837 void
838 init_zfs_bootenv(const char *currdev_in)
839 {
840 	char *beroot, *currdev;
841 	int currdev_len;
842 
843 	currdev = NULL;
844 	currdev_len = strlen(currdev_in);
845 	if (currdev_len == 0)
846 		return;
847 	if (strncmp(currdev_in, "zfs:", 4) != 0)
848 		return;
849 	currdev = strdup(currdev_in);
850 	if (currdev == NULL)
851 		return;
852 	/* Remove the trailing : */
853 	currdev[currdev_len - 1] = '\0';
854 	setenv("zfs_be_active", currdev, 1);
855 	setenv("zfs_be_currpage", "1", 1);
856 	/* Remove the last element (current bootenv) */
857 	beroot = strrchr(currdev, '/');
858 	if (beroot != NULL)
859 		beroot[0] = '\0';
860 	beroot = strchr(currdev, ':') + 1;
861 	setenv("zfs_be_root", beroot, 1);
862 	zfs_bootenv_initial(beroot);
863 	free(currdev);
864 }
865 
866 static void
867 zfs_bootenv_initial(const char *name)
868 {
869 	char		poolname[ZFS_MAXNAMELEN], *dsname;
870 	char envname[32], envval[256];
871 	uint64_t	objid;
872 	spa_t		*spa;
873 	int		bootenvs_idx, len, rv;
874 
875 	SLIST_INIT(&zfs_be_head);
876 	zfs_env_count = 0;
877 	len = strlen(name);
878 	dsname = strchr(name, '/');
879 	if (dsname != NULL) {
880 		len = dsname - name;
881 		dsname++;
882 	} else
883 		dsname = "";
884 	strlcpy(poolname, name, len + 1);
885 	spa = spa_find_by_name(poolname);
886 	if (spa == NULL)
887 		return;
888 	rv = zfs_lookup_dataset(spa, dsname, &objid);
889 	if (rv != 0)
890 		return;
891 	rv = zfs_callback_dataset(spa, objid, zfs_belist_add);
892 	bootenvs_idx = 0;
893 	/* Populate the initial environment variables */
894 	SLIST_FOREACH_SAFE(zfs_be, &zfs_be_head, entries, zfs_be_tmp) {
895 		/* Enumerate all bootenvs for general usage */
896 		snprintf(envname, sizeof(envname), "bootenvs[%d]", bootenvs_idx);
897 		snprintf(envval, sizeof(envval), "zfs:%s/%s", name, zfs_be->name);
898 		rv = setenv(envname, envval, 1);
899 		if (rv != 0)
900 			break;
901 		bootenvs_idx++;
902 	}
903 	snprintf(envval, sizeof(envval), "%d", bootenvs_idx);
904 	setenv("bootenvs_count", envval, 1);
905 
906 	/* Clean up the SLIST of ZFS BEs */
907 	while (!SLIST_EMPTY(&zfs_be_head)) {
908 		zfs_be = SLIST_FIRST(&zfs_be_head);
909 		SLIST_REMOVE_HEAD(&zfs_be_head, entries);
910 		free(zfs_be);
911 	}
912 
913 	return;
914 
915 }
916 
917 int
918 zfs_bootenv(const char *name)
919 {
920 	static char	poolname[ZFS_MAXNAMELEN], *dsname, *root;
921 	char		becount[4];
922 	uint64_t	objid;
923 	spa_t		*spa;
924 	int		len, rv, pages, perpage, currpage;
925 
926 	if (name == NULL)
927 		return (EINVAL);
928 	if ((root = getenv("zfs_be_root")) == NULL)
929 		return (EINVAL);
930 
931 	if (strcmp(name, root) != 0) {
932 		if (setenv("zfs_be_root", name, 1) != 0)
933 			return (ENOMEM);
934 	}
935 
936 	SLIST_INIT(&zfs_be_head);
937 	zfs_env_count = 0;
938 	len = strlen(name);
939 	dsname = strchr(name, '/');
940 	if (dsname != NULL) {
941 		len = dsname - name;
942 		dsname++;
943 	} else
944 		dsname = "";
945 	memcpy(poolname, name, len);
946 	poolname[len] = '\0';
947 
948 	spa = spa_find_by_name(poolname);
949 	if (!spa)
950 		return (ENXIO);
951 	rv = zfs_lookup_dataset(spa, dsname, &objid);
952 	if (rv != 0)
953 		return (rv);
954 	rv = zfs_callback_dataset(spa, objid, zfs_belist_add);
955 
956 	/* Calculate and store the number of pages of BEs */
957 	perpage = (ZFS_BE_LAST - ZFS_BE_FIRST + 1);
958 	pages = (zfs_env_count / perpage) + ((zfs_env_count % perpage) > 0 ? 1 : 0);
959 	snprintf(becount, 4, "%d", pages);
960 	if (setenv("zfs_be_pages", becount, 1) != 0)
961 		return (ENOMEM);
962 
963 	/* Roll over the page counter if it has exceeded the maximum */
964 	currpage = strtol(getenv("zfs_be_currpage"), NULL, 10);
965 	if (currpage > pages) {
966 		if (setenv("zfs_be_currpage", "1", 1) != 0)
967 			return (ENOMEM);
968 	}
969 
970 	/* Populate the menu environment variables */
971 	zfs_set_env();
972 
973 	/* Clean up the SLIST of ZFS BEs */
974 	while (!SLIST_EMPTY(&zfs_be_head)) {
975 		zfs_be = SLIST_FIRST(&zfs_be_head);
976 		SLIST_REMOVE_HEAD(&zfs_be_head, entries);
977 		free(zfs_be);
978 	}
979 
980 	return (rv);
981 }
982 
983 int
984 zfs_belist_add(const char *name, uint64_t value __unused)
985 {
986 
987 	/* Skip special datasets that start with a $ character */
988 	if (strncmp(name, "$", 1) == 0) {
989 		return (0);
990 	}
991 	/* Add the boot environment to the head of the SLIST */
992 	zfs_be = malloc(sizeof(struct zfs_be_entry));
993 	if (zfs_be == NULL) {
994 		return (ENOMEM);
995 	}
996 	zfs_be->name = name;
997 	SLIST_INSERT_HEAD(&zfs_be_head, zfs_be, entries);
998 	zfs_env_count++;
999 
1000 	return (0);
1001 }
1002 
1003 int
1004 zfs_set_env(void)
1005 {
1006 	char envname[32], envval[256];
1007 	char *beroot, *pagenum;
1008 	int rv, page, ctr;
1009 
1010 	beroot = getenv("zfs_be_root");
1011 	if (beroot == NULL) {
1012 		return (1);
1013 	}
1014 
1015 	pagenum = getenv("zfs_be_currpage");
1016 	if (pagenum != NULL) {
1017 		page = strtol(pagenum, NULL, 10);
1018 	} else {
1019 		page = 1;
1020 	}
1021 
1022 	ctr = 1;
1023 	rv = 0;
1024 	zfs_env_index = ZFS_BE_FIRST;
1025 	SLIST_FOREACH_SAFE(zfs_be, &zfs_be_head, entries, zfs_be_tmp) {
1026 		/* Skip to the requested page number */
1027 		if (ctr <= ((ZFS_BE_LAST - ZFS_BE_FIRST + 1) * (page - 1))) {
1028 			ctr++;
1029 			continue;
1030 		}
1031 
1032 		snprintf(envname, sizeof(envname), "bootenvmenu_caption[%d]", zfs_env_index);
1033 		snprintf(envval, sizeof(envval), "%s", zfs_be->name);
1034 		rv = setenv(envname, envval, 1);
1035 		if (rv != 0) {
1036 			break;
1037 		}
1038 
1039 		snprintf(envname, sizeof(envname), "bootenvansi_caption[%d]", zfs_env_index);
1040 		rv = setenv(envname, envval, 1);
1041 		if (rv != 0){
1042 			break;
1043 		}
1044 
1045 		snprintf(envname, sizeof(envname), "bootenvmenu_command[%d]", zfs_env_index);
1046 		rv = setenv(envname, "set_bootenv", 1);
1047 		if (rv != 0){
1048 			break;
1049 		}
1050 
1051 		snprintf(envname, sizeof(envname), "bootenv_root[%d]", zfs_env_index);
1052 		snprintf(envval, sizeof(envval), "zfs:%s/%s", beroot, zfs_be->name);
1053 		rv = setenv(envname, envval, 1);
1054 		if (rv != 0){
1055 			break;
1056 		}
1057 
1058 		zfs_env_index++;
1059 		if (zfs_env_index > ZFS_BE_LAST) {
1060 			break;
1061 		}
1062 
1063 	}
1064 
1065 	for (; zfs_env_index <= ZFS_BE_LAST; zfs_env_index++) {
1066 		snprintf(envname, sizeof(envname), "bootenvmenu_caption[%d]", zfs_env_index);
1067 		(void)unsetenv(envname);
1068 		snprintf(envname, sizeof(envname), "bootenvansi_caption[%d]", zfs_env_index);
1069 		(void)unsetenv(envname);
1070 		snprintf(envname, sizeof(envname), "bootenvmenu_command[%d]", zfs_env_index);
1071 		(void)unsetenv(envname);
1072 		snprintf(envname, sizeof(envname), "bootenv_root[%d]", zfs_env_index);
1073 		(void)unsetenv(envname);
1074 	}
1075 
1076 	return (rv);
1077 }
1078