xref: /freebsd/stand/libsa/zfs/zfs.c (revision 0ebbda971890c149c5028066fc5ed7ea72228d87)
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 *envname, spa_t *spa,
64 		    const char *name, const char *dsname, int checkpoint);
65 static void	zfs_checkpoints_initial(spa_t *spa, const char *name,
66 		    const char *dsname);
67 
68 struct devsw zfs_dev;
69 
70 struct fs_ops zfs_fsops = {
71 	"zfs",
72 	zfs_open,
73 	zfs_close,
74 	zfs_read,
75 	null_write,
76 	zfs_seek,
77 	zfs_stat,
78 	zfs_readdir
79 };
80 
81 /*
82  * In-core open file.
83  */
84 struct file {
85 	off_t		f_seekp;	/* seek pointer */
86 	dnode_phys_t	f_dnode;
87 	uint64_t	f_zap_type;	/* zap type for readdir */
88 	uint64_t	f_num_leafs;	/* number of fzap leaf blocks */
89 	zap_leaf_phys_t	*f_zap_leaf;	/* zap leaf buffer */
90 };
91 
92 static int	zfs_env_index;
93 static int	zfs_env_count;
94 
95 SLIST_HEAD(zfs_be_list, zfs_be_entry) zfs_be_head = SLIST_HEAD_INITIALIZER(zfs_be_head);
96 struct zfs_be_list *zfs_be_headp;
97 struct zfs_be_entry {
98 	char *name;
99 	SLIST_ENTRY(zfs_be_entry) entries;
100 } *zfs_be, *zfs_be_tmp;
101 
102 /*
103  * Open a file.
104  */
105 static int
106 zfs_open(const char *upath, struct open_file *f)
107 {
108 	struct zfsmount *mount = (struct zfsmount *)f->f_devdata;
109 	struct file *fp;
110 	int rc;
111 
112 	if (f->f_dev != &zfs_dev)
113 		return (EINVAL);
114 
115 	/* allocate file system specific data structure */
116 	fp = calloc(1, sizeof(struct file));
117 	if (fp == NULL)
118 		return (ENOMEM);
119 	f->f_fsdata = fp;
120 
121 	rc = zfs_lookup(mount, upath, &fp->f_dnode);
122 	fp->f_seekp = 0;
123 	if (rc) {
124 		f->f_fsdata = NULL;
125 		free(fp);
126 	}
127 	return (rc);
128 }
129 
130 static int
131 zfs_close(struct open_file *f)
132 {
133 	struct file *fp = (struct file *)f->f_fsdata;
134 
135 	dnode_cache_obj = NULL;
136 	f->f_fsdata = NULL;
137 
138 	free(fp);
139 	return (0);
140 }
141 
142 /*
143  * Copy a portion of a file into kernel memory.
144  * Cross block boundaries when necessary.
145  */
146 static int
147 zfs_read(struct open_file *f, void *start, size_t size, size_t *resid	/* out */)
148 {
149 	const spa_t *spa = ((struct zfsmount *)f->f_devdata)->spa;
150 	struct file *fp = (struct file *)f->f_fsdata;
151 	struct stat sb;
152 	size_t n;
153 	int rc;
154 
155 	rc = zfs_stat(f, &sb);
156 	if (rc)
157 		return (rc);
158 	n = size;
159 	if (fp->f_seekp + n > sb.st_size)
160 		n = sb.st_size - fp->f_seekp;
161 
162 	rc = dnode_read(spa, &fp->f_dnode, fp->f_seekp, start, n);
163 	if (rc)
164 		return (rc);
165 
166 	if (0) {
167 	    int i;
168 	    for (i = 0; i < n; i++)
169 		putchar(((char*) start)[i]);
170 	}
171 	fp->f_seekp += n;
172 	if (resid)
173 		*resid = size - n;
174 
175 	return (0);
176 }
177 
178 static off_t
179 zfs_seek(struct open_file *f, off_t offset, int where)
180 {
181 	struct file *fp = (struct file *)f->f_fsdata;
182 
183 	switch (where) {
184 	case SEEK_SET:
185 		fp->f_seekp = offset;
186 		break;
187 	case SEEK_CUR:
188 		fp->f_seekp += offset;
189 		break;
190 	case SEEK_END:
191 	    {
192 		struct stat sb;
193 		int error;
194 
195 		error = zfs_stat(f, &sb);
196 		if (error != 0) {
197 			errno = error;
198 			return (-1);
199 		}
200 		fp->f_seekp = sb.st_size - offset;
201 		break;
202 	    }
203 	default:
204 		errno = EINVAL;
205 		return (-1);
206 	}
207 	return (fp->f_seekp);
208 }
209 
210 static int
211 zfs_stat(struct open_file *f, struct stat *sb)
212 {
213 	const spa_t *spa = ((struct zfsmount *)f->f_devdata)->spa;
214 	struct file *fp = (struct file *)f->f_fsdata;
215 
216 	return (zfs_dnode_stat(spa, &fp->f_dnode, sb));
217 }
218 
219 static int
220 zfs_readdir(struct open_file *f, struct dirent *d)
221 {
222 	const spa_t *spa = ((struct zfsmount *)f->f_devdata)->spa;
223 	struct file *fp = (struct file *)f->f_fsdata;
224 	mzap_ent_phys_t mze;
225 	struct stat sb;
226 	size_t bsize = fp->f_dnode.dn_datablkszsec << SPA_MINBLOCKSHIFT;
227 	int rc;
228 
229 	rc = zfs_stat(f, &sb);
230 	if (rc)
231 		return (rc);
232 	if (!S_ISDIR(sb.st_mode))
233 		return (ENOTDIR);
234 
235 	/*
236 	 * If this is the first read, get the zap type.
237 	 */
238 	if (fp->f_seekp == 0) {
239 		rc = dnode_read(spa, &fp->f_dnode,
240 				0, &fp->f_zap_type, sizeof(fp->f_zap_type));
241 		if (rc)
242 			return (rc);
243 
244 		if (fp->f_zap_type == ZBT_MICRO) {
245 			fp->f_seekp = offsetof(mzap_phys_t, mz_chunk);
246 		} else {
247 			rc = dnode_read(spa, &fp->f_dnode,
248 					offsetof(zap_phys_t, zap_num_leafs),
249 					&fp->f_num_leafs,
250 					sizeof(fp->f_num_leafs));
251 			if (rc)
252 				return (rc);
253 
254 			fp->f_seekp = bsize;
255 			fp->f_zap_leaf = malloc(bsize);
256 			if (fp->f_zap_leaf == NULL)
257 				return (ENOMEM);
258 			rc = dnode_read(spa, &fp->f_dnode,
259 					fp->f_seekp,
260 					fp->f_zap_leaf,
261 					bsize);
262 			if (rc)
263 				return (rc);
264 		}
265 	}
266 
267 	if (fp->f_zap_type == ZBT_MICRO) {
268 	mzap_next:
269 		if (fp->f_seekp >= bsize)
270 			return (ENOENT);
271 
272 		rc = dnode_read(spa, &fp->f_dnode,
273 				fp->f_seekp, &mze, sizeof(mze));
274 		if (rc)
275 			return (rc);
276 		fp->f_seekp += sizeof(mze);
277 
278 		if (!mze.mze_name[0])
279 			goto mzap_next;
280 
281 		d->d_fileno = ZFS_DIRENT_OBJ(mze.mze_value);
282 		d->d_type = ZFS_DIRENT_TYPE(mze.mze_value);
283 		strcpy(d->d_name, mze.mze_name);
284 		d->d_namlen = strlen(d->d_name);
285 		return (0);
286 	} else {
287 		zap_leaf_t zl;
288 		zap_leaf_chunk_t *zc, *nc;
289 		int chunk;
290 		size_t namelen;
291 		char *p;
292 		uint64_t value;
293 
294 		/*
295 		 * Initialise this so we can use the ZAP size
296 		 * calculating macros.
297 		 */
298 		zl.l_bs = ilog2(bsize);
299 		zl.l_phys = fp->f_zap_leaf;
300 
301 		/*
302 		 * Figure out which chunk we are currently looking at
303 		 * and consider seeking to the next leaf. We use the
304 		 * low bits of f_seekp as a simple chunk index.
305 		 */
306 	fzap_next:
307 		chunk = fp->f_seekp & (bsize - 1);
308 		if (chunk == ZAP_LEAF_NUMCHUNKS(&zl)) {
309 			fp->f_seekp = rounddown2(fp->f_seekp, bsize) + bsize;
310 			chunk = 0;
311 
312 			/*
313 			 * Check for EOF and read the new leaf.
314 			 */
315 			if (fp->f_seekp >= bsize * fp->f_num_leafs)
316 				return (ENOENT);
317 
318 			rc = dnode_read(spa, &fp->f_dnode,
319 					fp->f_seekp,
320 					fp->f_zap_leaf,
321 					bsize);
322 			if (rc)
323 				return (rc);
324 		}
325 
326 		zc = &ZAP_LEAF_CHUNK(&zl, chunk);
327 		fp->f_seekp++;
328 		if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
329 			goto fzap_next;
330 
331 		namelen = zc->l_entry.le_name_numints;
332 		if (namelen > sizeof(d->d_name))
333 			namelen = sizeof(d->d_name);
334 
335 		/*
336 		 * Paste the name back together.
337 		 */
338 		nc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_name_chunk);
339 		p = d->d_name;
340 		while (namelen > 0) {
341 			int len;
342 			len = namelen;
343 			if (len > ZAP_LEAF_ARRAY_BYTES)
344 				len = ZAP_LEAF_ARRAY_BYTES;
345 			memcpy(p, nc->l_array.la_array, len);
346 			p += len;
347 			namelen -= len;
348 			nc = &ZAP_LEAF_CHUNK(&zl, nc->l_array.la_next);
349 		}
350 		d->d_name[sizeof(d->d_name) - 1] = 0;
351 
352 		/*
353 		 * Assume the first eight bytes of the value are
354 		 * a uint64_t.
355 		 */
356 		value = fzap_leaf_value(&zl, zc);
357 
358 		d->d_fileno = ZFS_DIRENT_OBJ(value);
359 		d->d_type = ZFS_DIRENT_TYPE(value);
360 		d->d_namlen = strlen(d->d_name);
361 
362 		return (0);
363 	}
364 }
365 
366 static int
367 vdev_read(vdev_t *vdev, void *priv, off_t offset, void *buf, size_t bytes)
368 {
369 	int fd, ret;
370 	size_t res, head, tail, total_size, full_sec_size;
371 	unsigned secsz, do_tail_read;
372 	off_t start_sec;
373 	char *outbuf, *bouncebuf;
374 
375 	fd = (uintptr_t) priv;
376 	outbuf = (char *) buf;
377 	bouncebuf = NULL;
378 
379 	ret = ioctl(fd, DIOCGSECTORSIZE, &secsz);
380 	if (ret != 0)
381 		return (ret);
382 
383 	/*
384 	 * Handling reads of arbitrary offset and size - multi-sector case
385 	 * and single-sector case.
386 	 *
387 	 *                        Multi-sector Case
388 	 *                (do_tail_read = true if tail > 0)
389 	 *
390 	 *   |<----------------------total_size--------------------->|
391 	 *   |                                                       |
392 	 *   |<--head-->|<--------------bytes------------>|<--tail-->|
393 	 *   |          |                                 |          |
394 	 *   |          |       |<~full_sec_size~>|       |          |
395 	 *   +------------------+                 +------------------+
396 	 *   |          |0101010|     .  .  .     |0101011|          |
397 	 *   +------------------+                 +------------------+
398 	 *         start_sec                         start_sec + n
399 	 *
400 	 *
401 	 *                      Single-sector Case
402 	 *                    (do_tail_read = false)
403 	 *
404 	 *              |<------total_size = secsz----->|
405 	 *              |                               |
406 	 *              |<-head->|<---bytes--->|<-tail->|
407 	 *              +-------------------------------+
408 	 *              |        |0101010101010|        |
409 	 *              +-------------------------------+
410 	 *                          start_sec
411 	 */
412 	start_sec = offset / secsz;
413 	head = offset % secsz;
414 	total_size = roundup2(head + bytes, secsz);
415 	tail = total_size - (head + bytes);
416 	do_tail_read = ((tail > 0) && (head + bytes > secsz));
417 	full_sec_size = total_size;
418 	if (head > 0)
419 		full_sec_size -= secsz;
420 	if (do_tail_read)
421 		full_sec_size -= secsz;
422 
423 	/* Return of partial sector data requires a bounce buffer. */
424 	if ((head > 0) || do_tail_read || bytes < secsz) {
425 		bouncebuf = malloc(secsz);
426 		if (bouncebuf == NULL) {
427 			printf("vdev_read: out of memory\n");
428 			return (ENOMEM);
429 		}
430 	}
431 
432 	if (lseek(fd, start_sec * secsz, SEEK_SET) == -1) {
433 		ret = errno;
434 		goto error;
435 	}
436 
437 	/* Partial data return from first sector */
438 	if (head > 0) {
439 		res = read(fd, bouncebuf, secsz);
440 		if (res != secsz) {
441 			ret = EIO;
442 			goto error;
443 		}
444 		memcpy(outbuf, bouncebuf + head, min(secsz - head, bytes));
445 		outbuf += min(secsz - head, bytes);
446 	}
447 
448 	/*
449 	 * Full data return from read sectors.
450 	 * Note, there is still corner case where we read
451 	 * from sector boundary, but less than sector size, e.g. reading 512B
452 	 * from 4k sector.
453 	 */
454 	if (full_sec_size > 0) {
455 		if (bytes < full_sec_size) {
456 			res = read(fd, bouncebuf, secsz);
457 			if (res != secsz) {
458 				ret = EIO;
459 				goto error;
460 			}
461 			memcpy(outbuf, bouncebuf, bytes);
462 		} else {
463 			res = read(fd, outbuf, full_sec_size);
464 			if (res != full_sec_size) {
465 				ret = EIO;
466 				goto error;
467 			}
468 			outbuf += full_sec_size;
469 		}
470 	}
471 
472 	/* Partial data return from last sector */
473 	if (do_tail_read) {
474 		res = read(fd, bouncebuf, secsz);
475 		if (res != secsz) {
476 			ret = EIO;
477 			goto error;
478 		}
479 		memcpy(outbuf, bouncebuf, secsz - tail);
480 	}
481 
482 	ret = 0;
483 error:
484 	free(bouncebuf);
485 	return (ret);
486 }
487 
488 static int
489 vdev_write(vdev_t *vdev, off_t offset, void *buf, size_t bytes)
490 {
491 	int fd, ret;
492 	size_t head, tail, total_size, full_sec_size;
493 	unsigned secsz, do_tail_write;
494 	off_t start_sec;
495 	ssize_t res;
496 	char *outbuf, *bouncebuf;
497 
498 	fd = (uintptr_t)vdev->v_priv;
499 	outbuf = (char *)buf;
500 	bouncebuf = NULL;
501 
502 	ret = ioctl(fd, DIOCGSECTORSIZE, &secsz);
503 	if (ret != 0)
504 		return (ret);
505 
506 	start_sec = offset / secsz;
507 	head = offset % secsz;
508 	total_size = roundup2(head + bytes, secsz);
509 	tail = total_size - (head + bytes);
510 	do_tail_write = ((tail > 0) && (head + bytes > secsz));
511 	full_sec_size = total_size;
512 	if (head > 0)
513 		full_sec_size -= secsz;
514 	if (do_tail_write)
515 		full_sec_size -= secsz;
516 
517 	/* Partial sector write requires a bounce buffer. */
518 	if ((head > 0) || do_tail_write || bytes < secsz) {
519 		bouncebuf = malloc(secsz);
520 		if (bouncebuf == NULL) {
521 			printf("vdev_write: out of memory\n");
522 			return (ENOMEM);
523 		}
524 	}
525 
526 	if (lseek(fd, start_sec * secsz, SEEK_SET) == -1) {
527 		ret = errno;
528 		goto error;
529 	}
530 
531 	/* Partial data for first sector */
532 	if (head > 0) {
533 		res = read(fd, bouncebuf, secsz);
534 		if ((unsigned)res != secsz) {
535 			ret = EIO;
536 			goto error;
537 		}
538 		memcpy(bouncebuf + head, outbuf, min(secsz - head, bytes));
539 		(void) lseek(fd, -secsz, SEEK_CUR);
540 		res = write(fd, bouncebuf, secsz);
541 		if ((unsigned)res != secsz) {
542 			ret = EIO;
543 			goto error;
544 		}
545 		outbuf += min(secsz - head, bytes);
546 	}
547 
548 	/*
549 	 * Full data write to sectors.
550 	 * Note, there is still corner case where we write
551 	 * to sector boundary, but less than sector size, e.g. write 512B
552 	 * to 4k sector.
553 	 */
554 	if (full_sec_size > 0) {
555 		if (bytes < full_sec_size) {
556 			res = read(fd, bouncebuf, secsz);
557 			if ((unsigned)res != secsz) {
558 				ret = EIO;
559 				goto error;
560 			}
561 			memcpy(bouncebuf, outbuf, bytes);
562 			(void) lseek(fd, -secsz, SEEK_CUR);
563 			res = write(fd, bouncebuf, secsz);
564 			if ((unsigned)res != secsz) {
565 				ret = EIO;
566 				goto error;
567 			}
568 		} else {
569 			res = write(fd, outbuf, full_sec_size);
570 			if ((unsigned)res != full_sec_size) {
571 				ret = EIO;
572 				goto error;
573 			}
574 			outbuf += full_sec_size;
575 		}
576 	}
577 
578 	/* Partial data write to last sector */
579 	if (do_tail_write) {
580 		res = read(fd, bouncebuf, secsz);
581 		if ((unsigned)res != secsz) {
582 			ret = EIO;
583 			goto error;
584 		}
585 		memcpy(bouncebuf, outbuf, secsz - tail);
586 		(void) lseek(fd, -secsz, SEEK_CUR);
587 		res = write(fd, bouncebuf, secsz);
588 		if ((unsigned)res != secsz) {
589 			ret = EIO;
590 			goto error;
591 		}
592 	}
593 
594 	ret = 0;
595 error:
596 	free(bouncebuf);
597 	return (ret);
598 }
599 
600 static int
601 zfs_dev_init(void)
602 {
603 	spa_t *spa;
604 	spa_t *next;
605 	spa_t *prev;
606 
607 	zfs_init();
608 	if (archsw.arch_zfs_probe == NULL)
609 		return (ENXIO);
610 	archsw.arch_zfs_probe();
611 
612 	prev = NULL;
613 	spa = STAILQ_FIRST(&zfs_pools);
614 	while (spa != NULL) {
615 		next = STAILQ_NEXT(spa, spa_link);
616 		if (zfs_spa_init(spa)) {
617 			if (prev == NULL)
618 				STAILQ_REMOVE_HEAD(&zfs_pools, spa_link);
619 			else
620 				STAILQ_REMOVE_AFTER(&zfs_pools, prev, spa_link);
621 		} else
622 			prev = spa;
623 		spa = next;
624 	}
625 	return (0);
626 }
627 
628 struct zfs_probe_args {
629 	int		fd;
630 	const char	*devname;
631 	uint64_t	*pool_guid;
632 	u_int		secsz;
633 };
634 
635 static int
636 zfs_diskread(void *arg, void *buf, size_t blocks, uint64_t offset)
637 {
638 	struct zfs_probe_args *ppa;
639 
640 	ppa = (struct zfs_probe_args *)arg;
641 	return (vdev_read(NULL, (void *)(uintptr_t)ppa->fd,
642 	    offset * ppa->secsz, buf, blocks * ppa->secsz));
643 }
644 
645 static int
646 zfs_probe(int fd, uint64_t *pool_guid)
647 {
648 	spa_t *spa;
649 	int ret;
650 
651 	spa = NULL;
652 	ret = vdev_probe(vdev_read, vdev_write, (void *)(uintptr_t)fd, &spa);
653 	if (ret == 0 && pool_guid != NULL)
654 		*pool_guid = spa->spa_guid;
655 	return (ret);
656 }
657 
658 static int
659 zfs_probe_partition(void *arg, const char *partname,
660     const struct ptable_entry *part)
661 {
662 	struct zfs_probe_args *ppa, pa;
663 	struct ptable *table;
664 	char devname[32];
665 	int ret;
666 
667 	/* Probe only freebsd-zfs and freebsd partitions */
668 	if (part->type != PART_FREEBSD &&
669 	    part->type != PART_FREEBSD_ZFS)
670 		return (0);
671 
672 	ppa = (struct zfs_probe_args *)arg;
673 	strncpy(devname, ppa->devname, strlen(ppa->devname) - 1);
674 	devname[strlen(ppa->devname) - 1] = '\0';
675 	snprintf(devname, sizeof(devname), "%s%s:", devname, partname);
676 	pa.fd = open(devname, O_RDWR);
677 	if (pa.fd == -1)
678 		return (0);
679 	ret = zfs_probe(pa.fd, ppa->pool_guid);
680 	if (ret == 0)
681 		return (0);
682 	/* Do we have BSD label here? */
683 	if (part->type == PART_FREEBSD) {
684 		pa.devname = devname;
685 		pa.pool_guid = ppa->pool_guid;
686 		pa.secsz = ppa->secsz;
687 		table = ptable_open(&pa, part->end - part->start + 1,
688 		    ppa->secsz, zfs_diskread);
689 		if (table != NULL) {
690 			ptable_iterate(table, &pa, zfs_probe_partition);
691 			ptable_close(table);
692 		}
693 	}
694 	close(pa.fd);
695 	return (0);
696 }
697 
698 /*
699  * Return bootenv nvlist from pool label.
700  */
701 int
702 zfs_get_bootenv(void *vdev, nvlist_t **benvp)
703 {
704 	struct zfs_devdesc *dev = (struct zfs_devdesc *)vdev;
705 	nvlist_t *benv = NULL;
706 	vdev_t *vd;
707 	spa_t *spa;
708 
709 	if (dev->dd.d_dev->dv_type != DEVT_ZFS)
710 		return (ENOTSUP);
711 
712 	if ((spa = spa_find_by_dev(dev)) == NULL)
713 		return (ENXIO);
714 
715 	if (spa->spa_bootenv == NULL) {
716 		STAILQ_FOREACH(vd, &spa->spa_root_vdev->v_children,
717 		    v_childlink) {
718 			benv = vdev_read_bootenv(vd);
719 
720 			if (benv != NULL)
721 				break;
722 		}
723 		spa->spa_bootenv = benv;
724 	} else {
725 		benv = spa->spa_bootenv;
726 	}
727 
728 	if (benv == NULL)
729 		return (ENOENT);
730 
731 	*benvp = benv;
732 	return (0);
733 }
734 
735 /*
736  * Store nvlist to pool label bootenv area. Also updates cached pointer in spa.
737  */
738 int
739 zfs_set_bootenv(void *vdev, nvlist_t *benv)
740 {
741 	struct zfs_devdesc *dev = (struct zfs_devdesc *)vdev;
742 	spa_t *spa;
743 	vdev_t *vd;
744 
745 	if (dev->dd.d_dev->dv_type != DEVT_ZFS)
746 		return (ENOTSUP);
747 
748 	if ((spa = spa_find_by_dev(dev)) == NULL)
749 		return (ENXIO);
750 
751 	STAILQ_FOREACH(vd, &spa->spa_root_vdev->v_children, v_childlink) {
752 		vdev_write_bootenv(vd, benv);
753 	}
754 
755 	spa->spa_bootenv = benv;
756 	return (0);
757 }
758 
759 /*
760  * Get bootonce value by key. The bootonce <key, value> pair is removed
761  * from the bootenv nvlist and the remaining nvlist is committed back to disk.
762  */
763 int
764 zfs_get_bootonce(void *vdev, const char *key, char *buf, size_t size)
765 {
766 	nvlist_t *benv;
767 	char *result = NULL;
768 	int result_size, rv;
769 
770 	if ((rv = zfs_get_bootenv(vdev, &benv)) != 0)
771 		return (rv);
772 
773 	if ((rv = nvlist_find(benv, key, DATA_TYPE_STRING, NULL,
774 	    &result, &result_size)) == 0) {
775 		if (result_size == 0) {
776 			/* ignore empty string */
777 			rv = ENOENT;
778 		} else {
779 			size = MIN((size_t)result_size + 1, size);
780 			strlcpy(buf, result, size);
781 		}
782 		(void) nvlist_remove(benv, key, DATA_TYPE_STRING);
783 		(void) zfs_set_bootenv(vdev, benv);
784 	}
785 
786 	return (rv);
787 }
788 
789 /*
790  * nvstore backend.
791  */
792 
793 static int zfs_nvstore_setter(void *, int, const char *,
794     const void *, size_t);
795 static int zfs_nvstore_setter_str(void *, const char *, const char *,
796     const char *);
797 static int zfs_nvstore_unset_impl(void *, const char *, bool);
798 static int zfs_nvstore_setenv(void *, void *);
799 
800 /*
801  * nvstore is only present for current rootfs pool.
802  */
803 static int
804 zfs_nvstore_sethook(struct env_var *ev, int flags __unused, const void *value)
805 {
806 	struct zfs_devdesc *dev;
807 	int rv;
808 
809 	archsw.arch_getdev((void **)&dev, NULL, NULL);
810 	if (dev == NULL)
811 		return (ENXIO);
812 
813 	rv = zfs_nvstore_setter_str(dev, NULL, ev->ev_name, value);
814 
815 	free(dev);
816 	return (rv);
817 }
818 
819 /*
820  * nvstore is only present for current rootfs pool.
821  */
822 static int
823 zfs_nvstore_unsethook(struct env_var *ev)
824 {
825 	struct zfs_devdesc *dev;
826 	int rv;
827 
828 	archsw.arch_getdev((void **)&dev, NULL, NULL);
829 	if (dev == NULL)
830 		return (ENXIO);
831 
832 	rv = zfs_nvstore_unset_impl(dev, ev->ev_name, false);
833 
834 	free(dev);
835 	return (rv);
836 }
837 
838 static int
839 zfs_nvstore_getter(void *vdev, const char *name, void **data)
840 {
841 	struct zfs_devdesc *dev = (struct zfs_devdesc *)vdev;
842 	spa_t *spa;
843 	nvlist_t *nv;
844 	char *str, **ptr;
845 	int size;
846 	int rv;
847 
848 	if (dev->dd.d_dev->dv_type != DEVT_ZFS)
849 		return (ENOTSUP);
850 
851 	if ((spa = spa_find_by_dev(dev)) == NULL)
852 		return (ENXIO);
853 
854 	if (spa->spa_bootenv == NULL)
855 		return (ENXIO);
856 
857 	if (nvlist_find(spa->spa_bootenv, OS_NVSTORE, DATA_TYPE_NVLIST,
858 	    NULL, &nv, NULL) != 0)
859 		return (ENOENT);
860 
861 	rv = nvlist_find(nv, name, DATA_TYPE_STRING, NULL, &str, &size);
862 	if (rv == 0) {
863 		ptr = (char **)data;
864 		asprintf(ptr, "%.*s", size, str);
865 		if (*data == NULL)
866 			rv = ENOMEM;
867 	}
868 	nvlist_destroy(nv);
869 	return (rv);
870 }
871 
872 static int
873 zfs_nvstore_setter(void *vdev, int type, const char *name,
874     const void *data, size_t size)
875 {
876 	struct zfs_devdesc *dev = (struct zfs_devdesc *)vdev;
877 	spa_t *spa;
878 	nvlist_t *nv;
879 	int rv;
880 	bool env_set = true;
881 
882 	if (dev->dd.d_dev->dv_type != DEVT_ZFS)
883 		return (ENOTSUP);
884 
885 	if ((spa = spa_find_by_dev(dev)) == NULL)
886 		return (ENXIO);
887 
888 	if (spa->spa_bootenv == NULL)
889 		return (ENXIO);
890 
891 	if (nvlist_find(spa->spa_bootenv, OS_NVSTORE, DATA_TYPE_NVLIST,
892 	    NULL, &nv, NULL) != 0) {
893 		nv = nvlist_create(NV_UNIQUE_NAME);
894 		if (nv == NULL)
895 			return (ENOMEM);
896 	}
897 
898 	rv = 0;
899 	switch (type) {
900         case DATA_TYPE_INT8:
901 		if (size != sizeof (int8_t)) {
902 			rv = EINVAL;
903 			break;
904 		}
905 		rv = nvlist_add_int8(nv, name, *(int8_t *)data);
906 		break;
907 
908         case DATA_TYPE_INT16:
909 		if (size != sizeof (int16_t)) {
910 			rv = EINVAL;
911 			break;
912 		}
913 		rv = nvlist_add_int16(nv, name, *(int16_t *)data);
914 		break;
915 
916         case DATA_TYPE_INT32:
917 		if (size != sizeof (int32_t)) {
918 			rv = EINVAL;
919 			break;
920 		}
921 		rv = nvlist_add_int32(nv, name, *(int32_t *)data);
922 		break;
923 
924         case DATA_TYPE_INT64:
925 		if (size != sizeof (int64_t)) {
926 			rv = EINVAL;
927 			break;
928 		}
929 		rv = nvlist_add_int64(nv, name, *(int64_t *)data);
930 		break;
931 
932         case DATA_TYPE_BYTE:
933 		if (size != sizeof (uint8_t)) {
934 			rv = EINVAL;
935 			break;
936 		}
937 		rv = nvlist_add_byte(nv, name, *(int8_t *)data);
938 		break;
939 
940         case DATA_TYPE_UINT8:
941 		if (size != sizeof (uint8_t)) {
942 			rv = EINVAL;
943 			break;
944 		}
945 		rv = nvlist_add_uint8(nv, name, *(int8_t *)data);
946 		break;
947 
948         case DATA_TYPE_UINT16:
949 		if (size != sizeof (uint16_t)) {
950 			rv = EINVAL;
951 			break;
952 		}
953 		rv = nvlist_add_uint16(nv, name, *(uint16_t *)data);
954 		break;
955 
956         case DATA_TYPE_UINT32:
957 		if (size != sizeof (uint32_t)) {
958 			rv = EINVAL;
959 			break;
960 		}
961 		rv = nvlist_add_uint32(nv, name, *(uint32_t *)data);
962 		break;
963 
964         case DATA_TYPE_UINT64:
965 		if (size != sizeof (uint64_t)) {
966 			rv = EINVAL;
967 			break;
968 		}
969 		rv = nvlist_add_uint64(nv, name, *(uint64_t *)data);
970 		break;
971 
972         case DATA_TYPE_STRING:
973 		rv = nvlist_add_string(nv, name, data);
974 		break;
975 
976 	case DATA_TYPE_BOOLEAN_VALUE:
977 		if (size != sizeof (boolean_t)) {
978 			rv = EINVAL;
979 			break;
980 		}
981 		rv = nvlist_add_boolean_value(nv, name, *(boolean_t *)data);
982 		break;
983 
984 	default:
985 		rv = EINVAL;
986 		break;
987 	}
988 
989 	if (rv == 0) {
990 		rv = nvlist_add_nvlist(spa->spa_bootenv, OS_NVSTORE, nv);
991 		if (rv == 0) {
992 			rv = zfs_set_bootenv(vdev, spa->spa_bootenv);
993 		}
994 		if (rv == 0) {
995 			if (env_set) {
996 				rv = zfs_nvstore_setenv(vdev,
997 				    nvpair_find(nv, name));
998 			} else {
999 				env_discard(env_getenv(name));
1000 				rv = 0;
1001 			}
1002 		}
1003 	}
1004 
1005 	nvlist_destroy(nv);
1006 	return (rv);
1007 }
1008 
1009 static int
1010 get_int64(const char *data, int64_t *ip)
1011 {
1012 	char *end;
1013 	int64_t val;
1014 
1015 	errno = 0;
1016 	val = strtoll(data, &end, 0);
1017 	if (errno != 0 || *data == '\0' || *end != '\0')
1018 		return (EINVAL);
1019 
1020 	*ip = val;
1021 	return (0);
1022 }
1023 
1024 static int
1025 get_uint64(const char *data, uint64_t *ip)
1026 {
1027 	char *end;
1028 	uint64_t val;
1029 
1030 	errno = 0;
1031 	val = strtoull(data, &end, 0);
1032 	if (errno != 0 || *data == '\0' || *end != '\0')
1033 		return (EINVAL);
1034 
1035 	*ip = val;
1036 	return (0);
1037 }
1038 
1039 /*
1040  * Translate textual data to data type. If type is not set, and we are
1041  * creating new pair, use DATA_TYPE_STRING.
1042  */
1043 static int
1044 zfs_nvstore_setter_str(void *vdev, const char *type, const char *name,
1045     const char *data)
1046 {
1047 	struct zfs_devdesc *dev = (struct zfs_devdesc *)vdev;
1048 	spa_t *spa;
1049 	nvlist_t *nv;
1050 	int rv;
1051 	data_type_t dt;
1052 	int64_t val;
1053 	uint64_t uval;
1054 
1055 	if (dev->dd.d_dev->dv_type != DEVT_ZFS)
1056 		return (ENOTSUP);
1057 
1058 	if ((spa = spa_find_by_dev(dev)) == NULL)
1059 		return (ENXIO);
1060 
1061 	if (spa->spa_bootenv == NULL)
1062 		return (ENXIO);
1063 
1064 	if (nvlist_find(spa->spa_bootenv, OS_NVSTORE, DATA_TYPE_NVLIST,
1065 	    NULL, &nv, NULL) != 0) {
1066 		nv = NULL;
1067 	}
1068 
1069 	if (type == NULL) {
1070 		nvp_header_t *nvh;
1071 
1072 		/*
1073 		 * if there is no existing pair, default to string.
1074 		 * Otherwise, use type from existing pair.
1075 		 */
1076 		nvh = nvpair_find(nv, name);
1077 		if (nvh == NULL) {
1078 			dt = DATA_TYPE_STRING;
1079 		} else {
1080 			nv_string_t *nvp_name;
1081 			nv_pair_data_t *nvp_data;
1082 
1083 			nvp_name = (nv_string_t *)(nvh + 1);
1084 			nvp_data = (nv_pair_data_t *)(&nvp_name->nv_data[0] +
1085 			    NV_ALIGN4(nvp_name->nv_size));
1086 			dt = nvp_data->nv_type;
1087 		}
1088 	} else {
1089 		dt = nvpair_type_from_name(type);
1090 	}
1091 	nvlist_destroy(nv);
1092 
1093 	rv = 0;
1094 	switch (dt) {
1095         case DATA_TYPE_INT8:
1096 		rv = get_int64(data, &val);
1097 		if (rv == 0) {
1098 			int8_t v = val;
1099 
1100 			rv = zfs_nvstore_setter(vdev, dt, name, &v, sizeof (v));
1101 		}
1102 		break;
1103         case DATA_TYPE_INT16:
1104 		rv = get_int64(data, &val);
1105 		if (rv == 0) {
1106 			int16_t v = val;
1107 
1108 			rv = zfs_nvstore_setter(vdev, dt, name, &v, sizeof (v));
1109 		}
1110 		break;
1111         case DATA_TYPE_INT32:
1112 		rv = get_int64(data, &val);
1113 		if (rv == 0) {
1114 			int32_t v = val;
1115 
1116 			rv = zfs_nvstore_setter(vdev, dt, name, &v, sizeof (v));
1117 		}
1118 		break;
1119         case DATA_TYPE_INT64:
1120 		rv = get_int64(data, &val);
1121 		if (rv == 0) {
1122 			rv = zfs_nvstore_setter(vdev, dt, name, &val,
1123 			    sizeof (val));
1124 		}
1125 		break;
1126 
1127         case DATA_TYPE_BYTE:
1128 		rv = get_uint64(data, &uval);
1129 		if (rv == 0) {
1130 			uint8_t v = uval;
1131 
1132 			rv = zfs_nvstore_setter(vdev, dt, name, &v, sizeof (v));
1133 		}
1134 		break;
1135 
1136         case DATA_TYPE_UINT8:
1137 		rv = get_uint64(data, &uval);
1138 		if (rv == 0) {
1139 			uint8_t v = uval;
1140 
1141 			rv = zfs_nvstore_setter(vdev, dt, name, &v, sizeof (v));
1142 		}
1143 		break;
1144 
1145         case DATA_TYPE_UINT16:
1146 		rv = get_uint64(data, &uval);
1147 		if (rv == 0) {
1148 			uint16_t v = uval;
1149 
1150 			rv = zfs_nvstore_setter(vdev, dt, name, &v, sizeof (v));
1151 		}
1152 		break;
1153 
1154         case DATA_TYPE_UINT32:
1155 		rv = get_uint64(data, &uval);
1156 		if (rv == 0) {
1157 			uint32_t v = uval;
1158 
1159 			rv = zfs_nvstore_setter(vdev, dt, name, &v, sizeof (v));
1160 		}
1161 		break;
1162 
1163         case DATA_TYPE_UINT64:
1164 		rv = get_uint64(data, &uval);
1165 		if (rv == 0) {
1166 			rv = zfs_nvstore_setter(vdev, dt, name, &uval,
1167 			    sizeof (uval));
1168 		}
1169 		break;
1170 
1171         case DATA_TYPE_STRING:
1172 		rv = zfs_nvstore_setter(vdev, dt, name, data, strlen(data) + 1);
1173 		break;
1174 
1175 	case DATA_TYPE_BOOLEAN_VALUE:
1176 		rv = get_int64(data, &val);
1177 		if (rv == 0) {
1178 			boolean_t v = val;
1179 
1180 			rv = zfs_nvstore_setter(vdev, dt, name, &v, sizeof (v));
1181 		}
1182 
1183 	default:
1184 		rv = EINVAL;
1185 	}
1186 	return (rv);
1187 }
1188 
1189 static int
1190 zfs_nvstore_unset_impl(void *vdev, const char *name, bool unset_env)
1191 {
1192 	struct zfs_devdesc *dev = (struct zfs_devdesc *)vdev;
1193 	spa_t *spa;
1194 	nvlist_t *nv;
1195 	int rv;
1196 
1197 	if (dev->dd.d_dev->dv_type != DEVT_ZFS)
1198 		return (ENOTSUP);
1199 
1200 	if ((spa = spa_find_by_dev(dev)) == NULL)
1201 		return (ENXIO);
1202 
1203 	if (spa->spa_bootenv == NULL)
1204 		return (ENXIO);
1205 
1206 	if (nvlist_find(spa->spa_bootenv, OS_NVSTORE, DATA_TYPE_NVLIST,
1207 	    NULL, &nv, NULL) != 0)
1208 		return (ENOENT);
1209 
1210 	rv = nvlist_remove(nv, name, DATA_TYPE_UNKNOWN);
1211 	if (rv == 0) {
1212 		if (nvlist_next_nvpair(nv, NULL) == NULL) {
1213 			rv = nvlist_remove(spa->spa_bootenv, OS_NVSTORE,
1214 			    DATA_TYPE_NVLIST);
1215 		} else {
1216 			rv = nvlist_add_nvlist(spa->spa_bootenv,
1217 			    OS_NVSTORE, nv);
1218 		}
1219 		if (rv == 0)
1220 			rv = zfs_set_bootenv(vdev, spa->spa_bootenv);
1221 	}
1222 
1223 	if (unset_env)
1224 		env_discard(env_getenv(name));
1225 	return (rv);
1226 }
1227 
1228 static int
1229 zfs_nvstore_unset(void *vdev, const char *name)
1230 {
1231 	return (zfs_nvstore_unset_impl(vdev, name, true));
1232 }
1233 
1234 static int
1235 zfs_nvstore_print(void *vdev __unused, void *ptr)
1236 {
1237 
1238 	nvpair_print(ptr, 0);
1239 	return (0);
1240 }
1241 
1242 /*
1243  * Create environment variable from nvpair.
1244  * set hook will update nvstore with new value, unset hook will remove
1245  * variable from nvstore.
1246  */
1247 static int
1248 zfs_nvstore_setenv(void *vdev __unused, void *ptr)
1249 {
1250 	nvp_header_t *nvh = ptr;
1251 	nv_string_t *nvp_name, *nvp_value;
1252 	nv_pair_data_t *nvp_data;
1253 	char *name, *value;
1254 	int rv = 0;
1255 
1256 	if (nvh == NULL)
1257 		return (ENOENT);
1258 
1259 	nvp_name = (nv_string_t *)(nvh + 1);
1260 	nvp_data = (nv_pair_data_t *)(&nvp_name->nv_data[0] +
1261 	    NV_ALIGN4(nvp_name->nv_size));
1262 
1263 	if ((name = nvstring_get(nvp_name)) == NULL)
1264 		return (ENOMEM);
1265 
1266 	value = NULL;
1267 	switch (nvp_data->nv_type) {
1268 	case DATA_TYPE_BYTE:
1269 	case DATA_TYPE_UINT8:
1270 		(void) asprintf(&value, "%uc",
1271 		    *(unsigned *)&nvp_data->nv_data[0]);
1272 		if (value == NULL)
1273 			rv = ENOMEM;
1274 		break;
1275 
1276 	case DATA_TYPE_INT8:
1277 		(void) asprintf(&value, "%c", *(int *)&nvp_data->nv_data[0]);
1278 		if (value == NULL)
1279 			rv = ENOMEM;
1280 		break;
1281 
1282 	case DATA_TYPE_INT16:
1283 		(void) asprintf(&value, "%hd", *(short *)&nvp_data->nv_data[0]);
1284 		if (value == NULL)
1285 			rv = ENOMEM;
1286 		break;
1287 
1288 	case DATA_TYPE_UINT16:
1289 		(void) asprintf(&value, "%hu",
1290 		    *(unsigned short *)&nvp_data->nv_data[0]);
1291 		if (value == NULL)
1292 			rv = ENOMEM;
1293 		break;
1294 
1295 	case DATA_TYPE_BOOLEAN_VALUE:
1296 	case DATA_TYPE_INT32:
1297 		(void) asprintf(&value, "%d", *(int *)&nvp_data->nv_data[0]);
1298 		if (value == NULL)
1299 			rv = ENOMEM;
1300 		break;
1301 
1302 	case DATA_TYPE_UINT32:
1303 		(void) asprintf(&value, "%u",
1304 		    *(unsigned *)&nvp_data->nv_data[0]);
1305 		if (value == NULL)
1306 			rv = ENOMEM;
1307 		break;
1308 
1309 	case DATA_TYPE_INT64:
1310 		(void) asprintf(&value, "%jd",
1311 		    (intmax_t)*(int64_t *)&nvp_data->nv_data[0]);
1312 		if (value == NULL)
1313 			rv = ENOMEM;
1314 		break;
1315 
1316 	case DATA_TYPE_UINT64:
1317 		(void) asprintf(&value, "%ju",
1318 		    (uintmax_t)*(uint64_t *)&nvp_data->nv_data[0]);
1319 		if (value == NULL)
1320 			rv = ENOMEM;
1321 		break;
1322 
1323 	case DATA_TYPE_STRING:
1324 		nvp_value = (nv_string_t *)&nvp_data->nv_data[0];
1325 		if ((value = nvstring_get(nvp_value)) == NULL) {
1326 			rv = ENOMEM;
1327 			break;
1328 		}
1329 		break;
1330 
1331 	default:
1332 		rv = EINVAL;
1333 		break;
1334 	}
1335 
1336 	if (value != NULL) {
1337 		rv = env_setenv(name, EV_VOLATILE | EV_NOHOOK, value,
1338 		    zfs_nvstore_sethook, zfs_nvstore_unsethook);
1339 		free(value);
1340 	}
1341 	free(name);
1342 	return (rv);
1343 }
1344 
1345 static int
1346 zfs_nvstore_iterate(void *vdev, int (*cb)(void *, void *))
1347 {
1348 	struct zfs_devdesc *dev = (struct zfs_devdesc *)vdev;
1349 	spa_t *spa;
1350 	nvlist_t *nv;
1351 	nvp_header_t *nvh;
1352 	int rv;
1353 
1354 	if (dev->dd.d_dev->dv_type != DEVT_ZFS)
1355 		return (ENOTSUP);
1356 
1357 	if ((spa = spa_find_by_dev(dev)) == NULL)
1358 		return (ENXIO);
1359 
1360 	if (spa->spa_bootenv == NULL)
1361 		return (ENXIO);
1362 
1363 	if (nvlist_find(spa->spa_bootenv, OS_NVSTORE, DATA_TYPE_NVLIST,
1364 	    NULL, &nv, NULL) != 0)
1365 		return (ENOENT);
1366 
1367 	rv = 0;
1368 	nvh = NULL;
1369 	while ((nvh = nvlist_next_nvpair(nv, nvh)) != NULL) {
1370 		rv = cb(vdev, nvh);
1371 		if (rv != 0)
1372 			break;
1373 	}
1374 	return (rv);
1375 }
1376 
1377 nvs_callbacks_t nvstore_zfs_cb = {
1378 	.nvs_getter = zfs_nvstore_getter,
1379 	.nvs_setter = zfs_nvstore_setter,
1380 	.nvs_setter_str = zfs_nvstore_setter_str,
1381 	.nvs_unset = zfs_nvstore_unset,
1382 	.nvs_print = zfs_nvstore_print,
1383 	.nvs_iterate = zfs_nvstore_iterate
1384 };
1385 
1386 int
1387 zfs_attach_nvstore(void *vdev)
1388 {
1389 	struct zfs_devdesc *dev = vdev;
1390 	spa_t *spa;
1391 	uint64_t version;
1392 	int rv;
1393 
1394 	if (dev->dd.d_dev->dv_type != DEVT_ZFS)
1395 		return (ENOTSUP);
1396 
1397 	if ((spa = spa_find_by_dev(dev)) == NULL)
1398 		return (ENXIO);
1399 
1400 	rv = nvlist_find(spa->spa_bootenv, BOOTENV_VERSION, DATA_TYPE_UINT64,
1401 	    NULL, &version, NULL);
1402 
1403 	if (rv != 0 || version != VB_NVLIST) {
1404 		return (ENXIO);
1405 	}
1406 
1407 	dev = malloc(sizeof (*dev));
1408 	if (dev == NULL)
1409 		return (ENOMEM);
1410 	memcpy(dev, vdev, sizeof (*dev));
1411 
1412 	rv = nvstore_init(spa->spa_name, &nvstore_zfs_cb, dev);
1413 	if (rv != 0)
1414 		free(dev);
1415 	else
1416 		rv = zfs_nvstore_iterate(dev, zfs_nvstore_setenv);
1417 	return (rv);
1418 }
1419 
1420 int
1421 zfs_probe_dev(const char *devname, uint64_t *pool_guid)
1422 {
1423 	struct disk_devdesc *dev;
1424 	struct ptable *table;
1425 	struct zfs_probe_args pa;
1426 	uint64_t mediasz;
1427 	int ret;
1428 
1429 	if (pool_guid)
1430 		*pool_guid = 0;
1431 	pa.fd = open(devname, O_RDWR);
1432 	if (pa.fd == -1)
1433 		return (ENXIO);
1434 	/*
1435 	 * We will not probe the whole disk, we can not boot from such
1436 	 * disks and some systems will misreport the disk sizes and will
1437 	 * hang while accessing the disk.
1438 	 */
1439 	if (archsw.arch_getdev((void **)&dev, devname, NULL) == 0) {
1440 		int partition = dev->d_partition;
1441 		int slice = dev->d_slice;
1442 
1443 		free(dev);
1444 		if (partition != D_PARTNONE && slice != D_SLICENONE) {
1445 			ret = zfs_probe(pa.fd, pool_guid);
1446 			if (ret == 0)
1447 				return (0);
1448 		}
1449 	}
1450 
1451 	/* Probe each partition */
1452 	ret = ioctl(pa.fd, DIOCGMEDIASIZE, &mediasz);
1453 	if (ret == 0)
1454 		ret = ioctl(pa.fd, DIOCGSECTORSIZE, &pa.secsz);
1455 	if (ret == 0) {
1456 		pa.devname = devname;
1457 		pa.pool_guid = pool_guid;
1458 		table = ptable_open(&pa, mediasz / pa.secsz, pa.secsz,
1459 		    zfs_diskread);
1460 		if (table != NULL) {
1461 			ptable_iterate(table, &pa, zfs_probe_partition);
1462 			ptable_close(table);
1463 		}
1464 	}
1465 	close(pa.fd);
1466 	if (pool_guid && *pool_guid == 0)
1467 		ret = ENXIO;
1468 	return (ret);
1469 }
1470 
1471 /*
1472  * Print information about ZFS pools
1473  */
1474 static int
1475 zfs_dev_print(int verbose)
1476 {
1477 	spa_t *spa;
1478 	char line[80];
1479 	int ret = 0;
1480 
1481 	if (STAILQ_EMPTY(&zfs_pools))
1482 		return (0);
1483 
1484 	printf("%s devices:", zfs_dev.dv_name);
1485 	if ((ret = pager_output("\n")) != 0)
1486 		return (ret);
1487 
1488 	if (verbose) {
1489 		return (spa_all_status());
1490 	}
1491 	STAILQ_FOREACH(spa, &zfs_pools, spa_link) {
1492 		snprintf(line, sizeof(line), "    zfs:%s\n", spa->spa_name);
1493 		ret = pager_output(line);
1494 		if (ret != 0)
1495 			break;
1496 	}
1497 	return (ret);
1498 }
1499 
1500 /*
1501  * Attempt to open the pool described by (dev) for use by (f).
1502  */
1503 static int
1504 zfs_dev_open(struct open_file *f, ...)
1505 {
1506 	va_list		args;
1507 	struct zfs_devdesc	*dev;
1508 	struct zfsmount	*mount;
1509 	spa_t		*spa;
1510 	int		rv;
1511 
1512 	va_start(args, f);
1513 	dev = va_arg(args, struct zfs_devdesc *);
1514 	va_end(args);
1515 
1516 	if ((spa = spa_find_by_dev(dev)) == NULL)
1517 		return (ENXIO);
1518 
1519 	mount = malloc(sizeof(*mount));
1520 	if (mount == NULL)
1521 		rv = ENOMEM;
1522 	else
1523 		rv = zfs_mount(spa, dev->root_guid, mount);
1524 	if (rv != 0) {
1525 		free(mount);
1526 		return (rv);
1527 	}
1528 	if (mount->objset.os_type != DMU_OST_ZFS) {
1529 		printf("Unexpected object set type %ju\n",
1530 		    (uintmax_t)mount->objset.os_type);
1531 		free(mount);
1532 		return (EIO);
1533 	}
1534 	f->f_devdata = mount;
1535 	free(dev);
1536 	return (0);
1537 }
1538 
1539 static int
1540 zfs_dev_close(struct open_file *f)
1541 {
1542 
1543 	free(f->f_devdata);
1544 	f->f_devdata = NULL;
1545 	return (0);
1546 }
1547 
1548 static int
1549 zfs_dev_strategy(void *devdata, int rw, daddr_t dblk, size_t size, char *buf, size_t *rsize)
1550 {
1551 
1552 	return (ENOSYS);
1553 }
1554 
1555 struct devsw zfs_dev = {
1556 	.dv_name = "zfs",
1557 	.dv_type = DEVT_ZFS,
1558 	.dv_init = zfs_dev_init,
1559 	.dv_strategy = zfs_dev_strategy,
1560 	.dv_open = zfs_dev_open,
1561 	.dv_close = zfs_dev_close,
1562 	.dv_ioctl = noioctl,
1563 	.dv_print = zfs_dev_print,
1564 	.dv_cleanup = NULL
1565 };
1566 
1567 int
1568 zfs_parsedev(struct zfs_devdesc *dev, const char *devspec, const char **path)
1569 {
1570 	static char	rootname[ZFS_MAXNAMELEN];
1571 	static char	poolname[ZFS_MAXNAMELEN];
1572 	spa_t		*spa;
1573 	const char	*end;
1574 	const char	*np;
1575 	const char	*sep;
1576 	int		rv;
1577 
1578 	np = devspec;
1579 	if (*np != ':')
1580 		return (EINVAL);
1581 	np++;
1582 	end = strrchr(np, ':');
1583 	if (end == NULL)
1584 		return (EINVAL);
1585 	sep = strchr(np, '/');
1586 	if (sep == NULL || sep >= end)
1587 		sep = end;
1588 	memcpy(poolname, np, sep - np);
1589 	poolname[sep - np] = '\0';
1590 	if (sep < end) {
1591 		sep++;
1592 		memcpy(rootname, sep, end - sep);
1593 		rootname[end - sep] = '\0';
1594 	}
1595 	else
1596 		rootname[0] = '\0';
1597 
1598 	spa = spa_find_by_name(poolname);
1599 	if (!spa)
1600 		return (ENXIO);
1601 	dev->pool_guid = spa->spa_guid;
1602 	rv = zfs_lookup_dataset(spa, rootname, &dev->root_guid);
1603 	if (rv != 0)
1604 		return (rv);
1605 	if (path != NULL)
1606 		*path = (*end == '\0') ? end : end + 1;
1607 	dev->dd.d_dev = &zfs_dev;
1608 	return (0);
1609 }
1610 
1611 char *
1612 zfs_fmtdev(void *vdev)
1613 {
1614 	static char		rootname[ZFS_MAXNAMELEN];
1615 	static char		buf[2 * ZFS_MAXNAMELEN + 8];
1616 	struct zfs_devdesc	*dev = (struct zfs_devdesc *)vdev;
1617 	spa_t			*spa;
1618 
1619 	buf[0] = '\0';
1620 	if (dev->dd.d_dev->dv_type != DEVT_ZFS)
1621 		return (buf);
1622 
1623 	/* Do we have any pools? */
1624 	spa = STAILQ_FIRST(&zfs_pools);
1625 	if (spa == NULL)
1626 		return (buf);
1627 
1628 	if (dev->pool_guid == 0)
1629 		dev->pool_guid = spa->spa_guid;
1630 	else
1631 		spa = spa_find_by_guid(dev->pool_guid);
1632 
1633 	if (spa == NULL) {
1634 		printf("ZFS: can't find pool by guid\n");
1635 		return (buf);
1636 	}
1637 	if (dev->root_guid == 0 && zfs_get_root(spa, &dev->root_guid)) {
1638 		printf("ZFS: can't find root filesystem\n");
1639 		return (buf);
1640 	}
1641 	if (zfs_rlookup(spa, dev->root_guid, rootname)) {
1642 		printf("ZFS: can't find filesystem by guid\n");
1643 		return (buf);
1644 	}
1645 
1646 	if (rootname[0] == '\0')
1647 		snprintf(buf, sizeof(buf), "%s:%s:", dev->dd.d_dev->dv_name,
1648 		    spa->spa_name);
1649 	else
1650 		snprintf(buf, sizeof(buf), "%s:%s/%s:", dev->dd.d_dev->dv_name,
1651 		    spa->spa_name, rootname);
1652 	return (buf);
1653 }
1654 
1655 static int
1656 split_devname(const char *name, char *poolname, size_t size,
1657     const char **dsnamep)
1658 {
1659 	const char *dsname;
1660 	size_t len;
1661 
1662 	ASSERT(name != NULL);
1663 	ASSERT(poolname != NULL);
1664 
1665 	len = strlen(name);
1666 	dsname = strchr(name, '/');
1667 	if (dsname != NULL) {
1668 		len = dsname - name;
1669 		dsname++;
1670 	} else
1671 		dsname = "";
1672 
1673 	if (len + 1 > size)
1674 		return (EINVAL);
1675 
1676 	strlcpy(poolname, name, len + 1);
1677 
1678 	if (dsnamep != NULL)
1679 		*dsnamep = dsname;
1680 
1681 	return (0);
1682 }
1683 
1684 int
1685 zfs_list(const char *name)
1686 {
1687 	static char	poolname[ZFS_MAXNAMELEN];
1688 	uint64_t	objid;
1689 	spa_t		*spa;
1690 	const char	*dsname;
1691 	int		rv;
1692 
1693 	if (split_devname(name, poolname, sizeof(poolname), &dsname) != 0)
1694 		return (EINVAL);
1695 
1696 	spa = spa_find_by_name(poolname);
1697 	if (!spa)
1698 		return (ENXIO);
1699 	rv = zfs_lookup_dataset(spa, dsname, &objid);
1700 	if (rv != 0)
1701 		return (rv);
1702 
1703 	return (zfs_list_dataset(spa, objid));
1704 }
1705 
1706 void
1707 init_zfs_boot_options(const char *currdev_in)
1708 {
1709 	char poolname[ZFS_MAXNAMELEN];
1710 	char *beroot, *currdev;
1711 	spa_t *spa;
1712 	int currdev_len;
1713 	const char *dsname;
1714 
1715 	currdev = NULL;
1716 	currdev_len = strlen(currdev_in);
1717 	if (currdev_len == 0)
1718 		return;
1719 	if (strncmp(currdev_in, "zfs:", 4) != 0)
1720 		return;
1721 	currdev = strdup(currdev_in);
1722 	if (currdev == NULL)
1723 		return;
1724 	/* Remove the trailing : */
1725 	currdev[currdev_len - 1] = '\0';
1726 
1727 	setenv("zfs_be_active", currdev, 1);
1728 	setenv("zfs_be_currpage", "1", 1);
1729 	/* Remove the last element (current bootenv) */
1730 	beroot = strrchr(currdev, '/');
1731 	if (beroot != NULL)
1732 		beroot[0] = '\0';
1733 	beroot = strchr(currdev, ':') + 1;
1734 	setenv("zfs_be_root", beroot, 1);
1735 
1736 	if (split_devname(beroot, poolname, sizeof(poolname), &dsname) != 0)
1737 		return;
1738 
1739 	spa = spa_find_by_name(poolname);
1740 	if (spa == NULL)
1741 		return;
1742 
1743 	zfs_bootenv_initial("bootenvs", spa, beroot, dsname, 0);
1744 	zfs_checkpoints_initial(spa, beroot, dsname);
1745 
1746 	free(currdev);
1747 }
1748 
1749 static void
1750 zfs_checkpoints_initial(spa_t *spa, const char *name, const char *dsname)
1751 {
1752 	char envname[32];
1753 
1754 	if (spa->spa_uberblock_checkpoint.ub_checkpoint_txg != 0) {
1755 		snprintf(envname, sizeof(envname), "zpool_checkpoint");
1756 		setenv(envname, name, 1);
1757 
1758 		spa->spa_uberblock = &spa->spa_uberblock_checkpoint;
1759 		spa->spa_mos = &spa->spa_mos_checkpoint;
1760 
1761 		zfs_bootenv_initial("bootenvs_check", spa, name, dsname, 1);
1762 
1763 		spa->spa_uberblock = &spa->spa_uberblock_master;
1764 		spa->spa_mos = &spa->spa_mos_master;
1765 	}
1766 }
1767 
1768 static void
1769 zfs_bootenv_initial(const char *envprefix, spa_t *spa, const char *rootname,
1770    const char *dsname, int checkpoint)
1771 {
1772 	char		envname[32], envval[256];
1773 	uint64_t	objid;
1774 	int		bootenvs_idx, rv;
1775 
1776 	SLIST_INIT(&zfs_be_head);
1777 	zfs_env_count = 0;
1778 
1779 	rv = zfs_lookup_dataset(spa, dsname, &objid);
1780 	if (rv != 0)
1781 		return;
1782 
1783 	rv = zfs_callback_dataset(spa, objid, zfs_belist_add);
1784 	bootenvs_idx = 0;
1785 	/* Populate the initial environment variables */
1786 	SLIST_FOREACH_SAFE(zfs_be, &zfs_be_head, entries, zfs_be_tmp) {
1787 		/* Enumerate all bootenvs for general usage */
1788 		snprintf(envname, sizeof(envname), "%s[%d]",
1789 		    envprefix, bootenvs_idx);
1790 		snprintf(envval, sizeof(envval), "zfs:%s%s/%s",
1791 		    checkpoint ? "!" : "", rootname, zfs_be->name);
1792 		rv = setenv(envname, envval, 1);
1793 		if (rv != 0)
1794 			break;
1795 		bootenvs_idx++;
1796 	}
1797 	snprintf(envname, sizeof(envname), "%s_count", envprefix);
1798 	snprintf(envval, sizeof(envval), "%d", bootenvs_idx);
1799 	setenv(envname, envval, 1);
1800 
1801 	/* Clean up the SLIST of ZFS BEs */
1802 	while (!SLIST_EMPTY(&zfs_be_head)) {
1803 		zfs_be = SLIST_FIRST(&zfs_be_head);
1804 		SLIST_REMOVE_HEAD(&zfs_be_head, entries);
1805 		free(zfs_be->name);
1806 		free(zfs_be);
1807 	}
1808 }
1809 
1810 int
1811 zfs_bootenv(const char *name)
1812 {
1813 	char		poolname[ZFS_MAXNAMELEN], *root;
1814 	const char	*dsname;
1815 	char		becount[4];
1816 	uint64_t	objid;
1817 	spa_t		*spa;
1818 	int		rv, pages, perpage, currpage;
1819 
1820 	if (name == NULL)
1821 		return (EINVAL);
1822 	if ((root = getenv("zfs_be_root")) == NULL)
1823 		return (EINVAL);
1824 
1825 	if (strcmp(name, root) != 0) {
1826 		if (setenv("zfs_be_root", name, 1) != 0)
1827 			return (ENOMEM);
1828 	}
1829 
1830 	SLIST_INIT(&zfs_be_head);
1831 	zfs_env_count = 0;
1832 
1833 	if (split_devname(name, poolname, sizeof(poolname), &dsname) != 0)
1834 		return (EINVAL);
1835 
1836 	spa = spa_find_by_name(poolname);
1837 	if (!spa)
1838 		return (ENXIO);
1839 	rv = zfs_lookup_dataset(spa, dsname, &objid);
1840 	if (rv != 0)
1841 		return (rv);
1842 	rv = zfs_callback_dataset(spa, objid, zfs_belist_add);
1843 
1844 	/* Calculate and store the number of pages of BEs */
1845 	perpage = (ZFS_BE_LAST - ZFS_BE_FIRST + 1);
1846 	pages = (zfs_env_count / perpage) + ((zfs_env_count % perpage) > 0 ? 1 : 0);
1847 	snprintf(becount, 4, "%d", pages);
1848 	if (setenv("zfs_be_pages", becount, 1) != 0)
1849 		return (ENOMEM);
1850 
1851 	/* Roll over the page counter if it has exceeded the maximum */
1852 	currpage = strtol(getenv("zfs_be_currpage"), NULL, 10);
1853 	if (currpage > pages) {
1854 		if (setenv("zfs_be_currpage", "1", 1) != 0)
1855 			return (ENOMEM);
1856 	}
1857 
1858 	/* Populate the menu environment variables */
1859 	zfs_set_env();
1860 
1861 	/* Clean up the SLIST of ZFS BEs */
1862 	while (!SLIST_EMPTY(&zfs_be_head)) {
1863 		zfs_be = SLIST_FIRST(&zfs_be_head);
1864 		SLIST_REMOVE_HEAD(&zfs_be_head, entries);
1865 		free(zfs_be->name);
1866 		free(zfs_be);
1867 	}
1868 
1869 	return (rv);
1870 }
1871 
1872 int
1873 zfs_belist_add(const char *name, uint64_t value __unused)
1874 {
1875 
1876 	/* Skip special datasets that start with a $ character */
1877 	if (strncmp(name, "$", 1) == 0) {
1878 		return (0);
1879 	}
1880 	/* Add the boot environment to the head of the SLIST */
1881 	zfs_be = malloc(sizeof(struct zfs_be_entry));
1882 	if (zfs_be == NULL) {
1883 		return (ENOMEM);
1884 	}
1885 	zfs_be->name = strdup(name);
1886 	if (zfs_be->name == NULL) {
1887 		free(zfs_be);
1888 		return (ENOMEM);
1889 	}
1890 	SLIST_INSERT_HEAD(&zfs_be_head, zfs_be, entries);
1891 	zfs_env_count++;
1892 
1893 	return (0);
1894 }
1895 
1896 int
1897 zfs_set_env(void)
1898 {
1899 	char envname[32], envval[256];
1900 	char *beroot, *pagenum;
1901 	int rv, page, ctr;
1902 
1903 	beroot = getenv("zfs_be_root");
1904 	if (beroot == NULL) {
1905 		return (1);
1906 	}
1907 
1908 	pagenum = getenv("zfs_be_currpage");
1909 	if (pagenum != NULL) {
1910 		page = strtol(pagenum, NULL, 10);
1911 	} else {
1912 		page = 1;
1913 	}
1914 
1915 	ctr = 1;
1916 	rv = 0;
1917 	zfs_env_index = ZFS_BE_FIRST;
1918 	SLIST_FOREACH_SAFE(zfs_be, &zfs_be_head, entries, zfs_be_tmp) {
1919 		/* Skip to the requested page number */
1920 		if (ctr <= ((ZFS_BE_LAST - ZFS_BE_FIRST + 1) * (page - 1))) {
1921 			ctr++;
1922 			continue;
1923 		}
1924 
1925 		snprintf(envname, sizeof(envname), "bootenvmenu_caption[%d]", zfs_env_index);
1926 		snprintf(envval, sizeof(envval), "%s", zfs_be->name);
1927 		rv = setenv(envname, envval, 1);
1928 		if (rv != 0) {
1929 			break;
1930 		}
1931 
1932 		snprintf(envname, sizeof(envname), "bootenvansi_caption[%d]", zfs_env_index);
1933 		rv = setenv(envname, envval, 1);
1934 		if (rv != 0){
1935 			break;
1936 		}
1937 
1938 		snprintf(envname, sizeof(envname), "bootenvmenu_command[%d]", zfs_env_index);
1939 		rv = setenv(envname, "set_bootenv", 1);
1940 		if (rv != 0){
1941 			break;
1942 		}
1943 
1944 		snprintf(envname, sizeof(envname), "bootenv_root[%d]", zfs_env_index);
1945 		snprintf(envval, sizeof(envval), "zfs:%s/%s", beroot, zfs_be->name);
1946 		rv = setenv(envname, envval, 1);
1947 		if (rv != 0){
1948 			break;
1949 		}
1950 
1951 		zfs_env_index++;
1952 		if (zfs_env_index > ZFS_BE_LAST) {
1953 			break;
1954 		}
1955 
1956 	}
1957 
1958 	for (; zfs_env_index <= ZFS_BE_LAST; zfs_env_index++) {
1959 		snprintf(envname, sizeof(envname), "bootenvmenu_caption[%d]", zfs_env_index);
1960 		(void)unsetenv(envname);
1961 		snprintf(envname, sizeof(envname), "bootenvansi_caption[%d]", zfs_env_index);
1962 		(void)unsetenv(envname);
1963 		snprintf(envname, sizeof(envname), "bootenvmenu_command[%d]", zfs_env_index);
1964 		(void)unsetenv(envname);
1965 		snprintf(envname, sizeof(envname), "bootenv_root[%d]", zfs_env_index);
1966 		(void)unsetenv(envname);
1967 	}
1968 
1969 	return (rv);
1970 }
1971