2 * Copyright (c) 2007 Doug Rabson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
33 * Stand-alone file reading package.
38 #include <sys/param.h>
40 #include <sys/queue.h>
45 #include <bootstrap.h>
51 /* Define the range of indexes to be populated with ZFS Boot Environments */
52 #define ZFS_BE_FIRST 4
55 static int zfs_open(const char *path, struct open_file *f);
56 static int zfs_close(struct open_file *f);
57 static int zfs_read(struct open_file *f, void *buf, size_t size, size_t *resid);
58 static off_t zfs_seek(struct open_file *f, off_t offset, int where);
59 static int zfs_stat(struct open_file *f, struct stat *sb);
60 static int zfs_readdir(struct open_file *f, struct dirent *d);
62 static void zfs_bootenv_initial(const char *envname, spa_t *spa,
63 const char *name, const char *dsname, int checkpoint);
64 static void zfs_checkpoints_initial(spa_t *spa, const char *name,
69 struct fs_ops zfs_fsops = {
84 off_t f_seekp; /* seek pointer */
86 uint64_t f_zap_type; /* zap type for readdir */
87 uint64_t f_num_leafs; /* number of fzap leaf blocks */
88 zap_leaf_phys_t *f_zap_leaf; /* zap leaf buffer */
91 static int zfs_env_index;
92 static int zfs_env_count;
94 SLIST_HEAD(zfs_be_list, zfs_be_entry) zfs_be_head = SLIST_HEAD_INITIALIZER(zfs_be_head);
95 struct zfs_be_list *zfs_be_headp;
98 SLIST_ENTRY(zfs_be_entry) entries;
99 } *zfs_be, *zfs_be_tmp;
105 zfs_open(const char *upath, struct open_file *f)
107 struct zfsmount *mount = (struct zfsmount *)f->f_devdata;
111 if (f->f_dev != &zfs_dev)
114 /* allocate file system specific data structure */
115 fp = calloc(1, sizeof(struct file));
120 rc = zfs_lookup(mount, upath, &fp->f_dnode);
130 zfs_close(struct open_file *f)
132 struct file *fp = (struct file *)f->f_fsdata;
134 dnode_cache_obj = NULL;
142 * Copy a portion of a file into kernel memory.
143 * Cross block boundaries when necessary.
146 zfs_read(struct open_file *f, void *start, size_t size, size_t *resid /* out */)
148 const spa_t *spa = ((struct zfsmount *)f->f_devdata)->spa;
149 struct file *fp = (struct file *)f->f_fsdata;
154 rc = zfs_stat(f, &sb);
158 if (fp->f_seekp + n > sb.st_size)
159 n = sb.st_size - fp->f_seekp;
161 rc = dnode_read(spa, &fp->f_dnode, fp->f_seekp, start, n);
167 for (i = 0; i < n; i++)
168 putchar(((char*) start)[i]);
178 zfs_seek(struct open_file *f, off_t offset, int where)
180 struct file *fp = (struct file *)f->f_fsdata;
184 fp->f_seekp = offset;
187 fp->f_seekp += offset;
194 error = zfs_stat(f, &sb);
199 fp->f_seekp = sb.st_size - offset;
206 return (fp->f_seekp);
210 zfs_stat(struct open_file *f, struct stat *sb)
212 const spa_t *spa = ((struct zfsmount *)f->f_devdata)->spa;
213 struct file *fp = (struct file *)f->f_fsdata;
215 return (zfs_dnode_stat(spa, &fp->f_dnode, sb));
219 zfs_readdir(struct open_file *f, struct dirent *d)
221 const spa_t *spa = ((struct zfsmount *)f->f_devdata)->spa;
222 struct file *fp = (struct file *)f->f_fsdata;
225 size_t bsize = fp->f_dnode.dn_datablkszsec << SPA_MINBLOCKSHIFT;
228 rc = zfs_stat(f, &sb);
231 if (!S_ISDIR(sb.st_mode))
235 * If this is the first read, get the zap type.
237 if (fp->f_seekp == 0) {
238 rc = dnode_read(spa, &fp->f_dnode,
239 0, &fp->f_zap_type, sizeof(fp->f_zap_type));
243 if (fp->f_zap_type == ZBT_MICRO) {
244 fp->f_seekp = offsetof(mzap_phys_t, mz_chunk);
246 rc = dnode_read(spa, &fp->f_dnode,
247 offsetof(zap_phys_t, zap_num_leafs),
249 sizeof(fp->f_num_leafs));
254 fp->f_zap_leaf = malloc(bsize);
255 if (fp->f_zap_leaf == NULL)
257 rc = dnode_read(spa, &fp->f_dnode,
266 if (fp->f_zap_type == ZBT_MICRO) {
268 if (fp->f_seekp >= bsize)
271 rc = dnode_read(spa, &fp->f_dnode,
272 fp->f_seekp, &mze, sizeof(mze));
275 fp->f_seekp += sizeof(mze);
277 if (!mze.mze_name[0])
280 d->d_fileno = ZFS_DIRENT_OBJ(mze.mze_value);
281 d->d_type = ZFS_DIRENT_TYPE(mze.mze_value);
282 strcpy(d->d_name, mze.mze_name);
283 d->d_namlen = strlen(d->d_name);
287 zap_leaf_chunk_t *zc, *nc;
294 * Initialise this so we can use the ZAP size
295 * calculating macros.
297 zl.l_bs = ilog2(bsize);
298 zl.l_phys = fp->f_zap_leaf;
301 * Figure out which chunk we are currently looking at
302 * and consider seeking to the next leaf. We use the
303 * low bits of f_seekp as a simple chunk index.
306 chunk = fp->f_seekp & (bsize - 1);
307 if (chunk == ZAP_LEAF_NUMCHUNKS(&zl)) {
308 fp->f_seekp = rounddown2(fp->f_seekp, bsize) + bsize;
312 * Check for EOF and read the new leaf.
314 if (fp->f_seekp >= bsize * fp->f_num_leafs)
317 rc = dnode_read(spa, &fp->f_dnode,
325 zc = &ZAP_LEAF_CHUNK(&zl, chunk);
327 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
330 namelen = zc->l_entry.le_name_numints;
331 if (namelen > sizeof(d->d_name))
332 namelen = sizeof(d->d_name);
335 * Paste the name back together.
337 nc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_name_chunk);
339 while (namelen > 0) {
342 if (len > ZAP_LEAF_ARRAY_BYTES)
343 len = ZAP_LEAF_ARRAY_BYTES;
344 memcpy(p, nc->l_array.la_array, len);
347 nc = &ZAP_LEAF_CHUNK(&zl, nc->l_array.la_next);
349 d->d_name[sizeof(d->d_name) - 1] = 0;
352 * Assume the first eight bytes of the value are
355 value = fzap_leaf_value(&zl, zc);
357 d->d_fileno = ZFS_DIRENT_OBJ(value);
358 d->d_type = ZFS_DIRENT_TYPE(value);
359 d->d_namlen = strlen(d->d_name);
366 vdev_read(vdev_t *vdev, void *priv, off_t offset, void *buf, size_t bytes)
369 size_t res, head, tail, total_size, full_sec_size;
370 unsigned secsz, do_tail_read;
372 char *outbuf, *bouncebuf;
374 fd = (uintptr_t) priv;
375 outbuf = (char *) buf;
378 ret = ioctl(fd, DIOCGSECTORSIZE, &secsz);
383 * Handling reads of arbitrary offset and size - multi-sector case
384 * and single-sector case.
387 * (do_tail_read = true if tail > 0)
389 * |<----------------------total_size--------------------->|
391 * |<--head-->|<--------------bytes------------>|<--tail-->|
393 * | | |<~full_sec_size~>| | |
394 * +------------------+ +------------------+
395 * | |0101010| . . . |0101011| |
396 * +------------------+ +------------------+
397 * start_sec start_sec + n
401 * (do_tail_read = false)
403 * |<------total_size = secsz----->|
405 * |<-head->|<---bytes--->|<-tail->|
406 * +-------------------------------+
407 * | |0101010101010| |
408 * +-------------------------------+
411 start_sec = offset / secsz;
412 head = offset % secsz;
413 total_size = roundup2(head + bytes, secsz);
414 tail = total_size - (head + bytes);
415 do_tail_read = ((tail > 0) && (head + bytes > secsz));
416 full_sec_size = total_size;
418 full_sec_size -= secsz;
420 full_sec_size -= secsz;
422 /* Return of partial sector data requires a bounce buffer. */
423 if ((head > 0) || do_tail_read || bytes < secsz) {
424 bouncebuf = malloc(secsz);
425 if (bouncebuf == NULL) {
426 printf("vdev_read: out of memory\n");
431 if (lseek(fd, start_sec * secsz, SEEK_SET) == -1) {
436 /* Partial data return from first sector */
438 res = read(fd, bouncebuf, secsz);
443 memcpy(outbuf, bouncebuf + head, min(secsz - head, bytes));
444 outbuf += min(secsz - head, bytes);
448 * Full data return from read sectors.
449 * Note, there is still corner case where we read
450 * from sector boundary, but less than sector size, e.g. reading 512B
453 if (full_sec_size > 0) {
454 if (bytes < full_sec_size) {
455 res = read(fd, bouncebuf, secsz);
460 memcpy(outbuf, bouncebuf, bytes);
462 res = read(fd, outbuf, full_sec_size);
463 if (res != full_sec_size) {
467 outbuf += full_sec_size;
471 /* Partial data return from last sector */
473 res = read(fd, bouncebuf, secsz);
478 memcpy(outbuf, bouncebuf, secsz - tail);
488 vdev_write(vdev_t *vdev, off_t offset, void *buf, size_t bytes)
491 size_t head, tail, total_size, full_sec_size;
492 unsigned secsz, do_tail_write;
495 char *outbuf, *bouncebuf;
497 fd = (uintptr_t)vdev->v_priv;
498 outbuf = (char *)buf;
501 ret = ioctl(fd, DIOCGSECTORSIZE, &secsz);
505 start_sec = offset / secsz;
506 head = offset % secsz;
507 total_size = roundup2(head + bytes, secsz);
508 tail = total_size - (head + bytes);
509 do_tail_write = ((tail > 0) && (head + bytes > secsz));
510 full_sec_size = total_size;
512 full_sec_size -= secsz;
514 full_sec_size -= secsz;
516 /* Partial sector write requires a bounce buffer. */
517 if ((head > 0) || do_tail_write || bytes < secsz) {
518 bouncebuf = malloc(secsz);
519 if (bouncebuf == NULL) {
520 printf("vdev_write: out of memory\n");
525 if (lseek(fd, start_sec * secsz, SEEK_SET) == -1) {
530 /* Partial data for first sector */
532 res = read(fd, bouncebuf, secsz);
533 if ((unsigned)res != secsz) {
537 memcpy(bouncebuf + head, outbuf, min(secsz - head, bytes));
538 (void) lseek(fd, -secsz, SEEK_CUR);
539 res = write(fd, bouncebuf, secsz);
540 if ((unsigned)res != secsz) {
544 outbuf += min(secsz - head, bytes);
548 * Full data write to sectors.
549 * Note, there is still corner case where we write
550 * to sector boundary, but less than sector size, e.g. write 512B
553 if (full_sec_size > 0) {
554 if (bytes < full_sec_size) {
555 res = read(fd, bouncebuf, secsz);
556 if ((unsigned)res != secsz) {
560 memcpy(bouncebuf, outbuf, bytes);
561 (void) lseek(fd, -secsz, SEEK_CUR);
562 res = write(fd, bouncebuf, secsz);
563 if ((unsigned)res != secsz) {
568 res = write(fd, outbuf, full_sec_size);
569 if ((unsigned)res != full_sec_size) {
573 outbuf += full_sec_size;
577 /* Partial data write to last sector */
579 res = read(fd, bouncebuf, secsz);
580 if ((unsigned)res != secsz) {
584 memcpy(bouncebuf, outbuf, secsz - tail);
585 (void) lseek(fd, -secsz, SEEK_CUR);
586 res = write(fd, bouncebuf, secsz);
587 if ((unsigned)res != secsz) {
607 if (archsw.arch_zfs_probe == NULL)
609 archsw.arch_zfs_probe();
612 spa = STAILQ_FIRST(&zfs_pools);
613 while (spa != NULL) {
614 next = STAILQ_NEXT(spa, spa_link);
615 if (zfs_spa_init(spa)) {
617 STAILQ_REMOVE_HEAD(&zfs_pools, spa_link);
619 STAILQ_REMOVE_AFTER(&zfs_pools, prev, spa_link);
627 struct zfs_probe_args {
635 zfs_diskread(void *arg, void *buf, size_t blocks, uint64_t offset)
637 struct zfs_probe_args *ppa;
639 ppa = (struct zfs_probe_args *)arg;
640 return (vdev_read(NULL, (void *)(uintptr_t)ppa->fd,
641 offset * ppa->secsz, buf, blocks * ppa->secsz));
645 zfs_probe(int fd, uint64_t *pool_guid)
651 ret = vdev_probe(vdev_read, vdev_write, (void *)(uintptr_t)fd, &spa);
652 if (ret == 0 && pool_guid != NULL)
654 *pool_guid = spa->spa_guid;
659 zfs_probe_partition(void *arg, const char *partname,
660 const struct ptable_entry *part)
662 struct zfs_probe_args *ppa, pa;
663 struct ptable *table;
667 /* Probe only freebsd-zfs and freebsd partitions */
668 if (part->type != PART_FREEBSD &&
669 part->type != PART_FREEBSD_ZFS)
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);
679 ret = zfs_probe(pa.fd, ppa->pool_guid);
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);
690 ptable_iterate(table, &pa, zfs_probe_partition);
699 * Return bootenv nvlist from pool label.
702 zfs_get_bootenv(void *vdev, nvlist_t **benvp)
704 struct zfs_devdesc *dev = (struct zfs_devdesc *)vdev;
705 nvlist_t *benv = NULL;
709 if (dev->dd.d_dev->dv_type != DEVT_ZFS)
712 if ((spa = spa_find_by_dev(dev)) == NULL)
715 if (spa->spa_bootenv == NULL) {
716 STAILQ_FOREACH(vd, &spa->spa_root_vdev->v_children,
718 benv = vdev_read_bootenv(vd);
723 spa->spa_bootenv = benv;
725 benv = spa->spa_bootenv;
736 * Store nvlist to pool label bootenv area. Also updates cached pointer in spa.
739 zfs_set_bootenv(void *vdev, nvlist_t *benv)
741 struct zfs_devdesc *dev = (struct zfs_devdesc *)vdev;
745 if (dev->dd.d_dev->dv_type != DEVT_ZFS)
748 if ((spa = spa_find_by_dev(dev)) == NULL)
751 STAILQ_FOREACH(vd, &spa->spa_root_vdev->v_children, v_childlink) {
752 vdev_write_bootenv(vd, benv);
755 spa->spa_bootenv = benv;
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.
764 zfs_get_bootonce(void *vdev, const char *key, char *buf, size_t size)
770 if ((rv = zfs_get_bootenv(vdev, &benv)) != 0)
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 */
779 size = MIN((size_t)result_size + 1, size);
780 strlcpy(buf, result, size);
782 (void) nvlist_remove(benv, key, DATA_TYPE_STRING);
783 (void) zfs_set_bootenv(vdev, benv);
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 *,
797 static int zfs_nvstore_unset_impl(void *, const char *, bool);
798 static int zfs_nvstore_setenv(void *, void *);
801 * nvstore is only present for current rootfs pool.
804 zfs_nvstore_sethook(struct env_var *ev, int flags __unused, const void *value)
806 struct zfs_devdesc *dev;
809 archsw.arch_getdev((void **)&dev, NULL, NULL);
813 rv = zfs_nvstore_setter_str(dev, NULL, ev->ev_name, value);
820 * nvstore is only present for current rootfs pool.
823 zfs_nvstore_unsethook(struct env_var *ev)
825 struct zfs_devdesc *dev;
828 archsw.arch_getdev((void **)&dev, NULL, NULL);
832 rv = zfs_nvstore_unset_impl(dev, ev->ev_name, false);
839 zfs_nvstore_getter(void *vdev, const char *name, void **data)
841 struct zfs_devdesc *dev = (struct zfs_devdesc *)vdev;
848 if (dev->dd.d_dev->dv_type != DEVT_ZFS)
851 if ((spa = spa_find_by_dev(dev)) == NULL)
854 if (spa->spa_bootenv == NULL)
857 if (nvlist_find(spa->spa_bootenv, OS_NVSTORE, DATA_TYPE_NVLIST,
858 NULL, &nv, NULL) != 0)
861 rv = nvlist_find(nv, name, DATA_TYPE_STRING, NULL, &str, &size);
864 asprintf(ptr, "%.*s", size, str);
873 zfs_nvstore_setter(void *vdev, int type, const char *name,
874 const void *data, size_t size)
876 struct zfs_devdesc *dev = (struct zfs_devdesc *)vdev;
882 if (dev->dd.d_dev->dv_type != DEVT_ZFS)
885 if ((spa = spa_find_by_dev(dev)) == NULL)
888 if (spa->spa_bootenv == NULL)
891 if (nvlist_find(spa->spa_bootenv, OS_NVSTORE, DATA_TYPE_NVLIST,
892 NULL, &nv, NULL) != 0) {
893 nv = nvlist_create(NV_UNIQUE_NAME);
901 if (size != sizeof (int8_t)) {
905 rv = nvlist_add_int8(nv, name, *(int8_t *)data);
908 case DATA_TYPE_INT16:
909 if (size != sizeof (int16_t)) {
913 rv = nvlist_add_int16(nv, name, *(int16_t *)data);
916 case DATA_TYPE_INT32:
917 if (size != sizeof (int32_t)) {
921 rv = nvlist_add_int32(nv, name, *(int32_t *)data);
924 case DATA_TYPE_INT64:
925 if (size != sizeof (int64_t)) {
929 rv = nvlist_add_int64(nv, name, *(int64_t *)data);
933 if (size != sizeof (uint8_t)) {
937 rv = nvlist_add_byte(nv, name, *(int8_t *)data);
940 case DATA_TYPE_UINT8:
941 if (size != sizeof (uint8_t)) {
945 rv = nvlist_add_uint8(nv, name, *(int8_t *)data);
948 case DATA_TYPE_UINT16:
949 if (size != sizeof (uint16_t)) {
953 rv = nvlist_add_uint16(nv, name, *(uint16_t *)data);
956 case DATA_TYPE_UINT32:
957 if (size != sizeof (uint32_t)) {
961 rv = nvlist_add_uint32(nv, name, *(uint32_t *)data);
964 case DATA_TYPE_UINT64:
965 if (size != sizeof (uint64_t)) {
969 rv = nvlist_add_uint64(nv, name, *(uint64_t *)data);
972 case DATA_TYPE_STRING:
973 rv = nvlist_add_string(nv, name, data);
976 case DATA_TYPE_BOOLEAN_VALUE:
977 if (size != sizeof (boolean_t)) {
981 rv = nvlist_add_boolean_value(nv, name, *(boolean_t *)data);
990 rv = nvlist_add_nvlist(spa->spa_bootenv, OS_NVSTORE, nv);
992 rv = zfs_set_bootenv(vdev, spa->spa_bootenv);
996 rv = zfs_nvstore_setenv(vdev,
997 nvpair_find(nv, name));
999 env_discard(env_getenv(name));
1010 get_int64(const char *data, int64_t *ip)
1016 val = strtoll(data, &end, 0);
1017 if (errno != 0 || *data == '\0' || *end != '\0')
1025 get_uint64(const char *data, uint64_t *ip)
1031 val = strtoull(data, &end, 0);
1032 if (errno != 0 || *data == '\0' || *end != '\0')
1040 * Translate textual data to data type. If type is not set, and we are
1041 * creating new pair, use DATA_TYPE_STRING.
1044 zfs_nvstore_setter_str(void *vdev, const char *type, const char *name,
1047 struct zfs_devdesc *dev = (struct zfs_devdesc *)vdev;
1055 if (dev->dd.d_dev->dv_type != DEVT_ZFS)
1058 if ((spa = spa_find_by_dev(dev)) == NULL)
1061 if (spa->spa_bootenv == NULL)
1064 if (nvlist_find(spa->spa_bootenv, OS_NVSTORE, DATA_TYPE_NVLIST,
1065 NULL, &nv, NULL) != 0) {
1073 * if there is no existing pair, default to string.
1074 * Otherwise, use type from existing pair.
1076 nvh = nvpair_find(nv, name);
1078 dt = DATA_TYPE_STRING;
1080 nv_string_t *nvp_name;
1081 nv_pair_data_t *nvp_data;
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;
1089 dt = nvpair_type_from_name(type);
1095 case DATA_TYPE_INT8:
1096 rv = get_int64(data, &val);
1100 rv = zfs_nvstore_setter(vdev, dt, name, &v, sizeof (v));
1103 case DATA_TYPE_INT16:
1104 rv = get_int64(data, &val);
1108 rv = zfs_nvstore_setter(vdev, dt, name, &v, sizeof (v));
1111 case DATA_TYPE_INT32:
1112 rv = get_int64(data, &val);
1116 rv = zfs_nvstore_setter(vdev, dt, name, &v, sizeof (v));
1119 case DATA_TYPE_INT64:
1120 rv = get_int64(data, &val);
1122 rv = zfs_nvstore_setter(vdev, dt, name, &val,
1127 case DATA_TYPE_BYTE:
1128 rv = get_uint64(data, &uval);
1132 rv = zfs_nvstore_setter(vdev, dt, name, &v, sizeof (v));
1136 case DATA_TYPE_UINT8:
1137 rv = get_uint64(data, &uval);
1141 rv = zfs_nvstore_setter(vdev, dt, name, &v, sizeof (v));
1145 case DATA_TYPE_UINT16:
1146 rv = get_uint64(data, &uval);
1150 rv = zfs_nvstore_setter(vdev, dt, name, &v, sizeof (v));
1154 case DATA_TYPE_UINT32:
1155 rv = get_uint64(data, &uval);
1159 rv = zfs_nvstore_setter(vdev, dt, name, &v, sizeof (v));
1163 case DATA_TYPE_UINT64:
1164 rv = get_uint64(data, &uval);
1166 rv = zfs_nvstore_setter(vdev, dt, name, &uval,
1171 case DATA_TYPE_STRING:
1172 rv = zfs_nvstore_setter(vdev, dt, name, data, strlen(data) + 1);
1175 case DATA_TYPE_BOOLEAN_VALUE:
1176 rv = get_int64(data, &val);
1180 rv = zfs_nvstore_setter(vdev, dt, name, &v, sizeof (v));
1190 zfs_nvstore_unset_impl(void *vdev, const char *name, bool unset_env)
1192 struct zfs_devdesc *dev = (struct zfs_devdesc *)vdev;
1197 if (dev->dd.d_dev->dv_type != DEVT_ZFS)
1200 if ((spa = spa_find_by_dev(dev)) == NULL)
1203 if (spa->spa_bootenv == NULL)
1206 if (nvlist_find(spa->spa_bootenv, OS_NVSTORE, DATA_TYPE_NVLIST,
1207 NULL, &nv, NULL) != 0)
1210 rv = nvlist_remove(nv, name, DATA_TYPE_UNKNOWN);
1212 if (nvlist_next_nvpair(nv, NULL) == NULL) {
1213 rv = nvlist_remove(spa->spa_bootenv, OS_NVSTORE,
1216 rv = nvlist_add_nvlist(spa->spa_bootenv,
1220 rv = zfs_set_bootenv(vdev, spa->spa_bootenv);
1224 env_discard(env_getenv(name));
1229 zfs_nvstore_unset(void *vdev, const char *name)
1231 return (zfs_nvstore_unset_impl(vdev, name, true));
1235 zfs_nvstore_print(void *vdev __unused, void *ptr)
1238 nvpair_print(ptr, 0);
1243 * Create environment variable from nvpair.
1244 * set hook will update nvstore with new value, unset hook will remove
1245 * variable from nvstore.
1248 zfs_nvstore_setenv(void *vdev __unused, void *ptr)
1250 nvp_header_t *nvh = ptr;
1251 nv_string_t *nvp_name, *nvp_value;
1252 nv_pair_data_t *nvp_data;
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));
1263 if ((name = nvstring_get(nvp_name)) == 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]);
1276 case DATA_TYPE_INT8:
1277 (void) asprintf(&value, "%c", *(int *)&nvp_data->nv_data[0]);
1282 case DATA_TYPE_INT16:
1283 (void) asprintf(&value, "%hd", *(short *)&nvp_data->nv_data[0]);
1288 case DATA_TYPE_UINT16:
1289 (void) asprintf(&value, "%hu",
1290 *(unsigned short *)&nvp_data->nv_data[0]);
1295 case DATA_TYPE_BOOLEAN_VALUE:
1296 case DATA_TYPE_INT32:
1297 (void) asprintf(&value, "%d", *(int *)&nvp_data->nv_data[0]);
1302 case DATA_TYPE_UINT32:
1303 (void) asprintf(&value, "%u",
1304 *(unsigned *)&nvp_data->nv_data[0]);
1309 case DATA_TYPE_INT64:
1310 (void) asprintf(&value, "%jd",
1311 (intmax_t)*(int64_t *)&nvp_data->nv_data[0]);
1316 case DATA_TYPE_UINT64:
1317 (void) asprintf(&value, "%ju",
1318 (uintmax_t)*(uint64_t *)&nvp_data->nv_data[0]);
1323 case DATA_TYPE_STRING:
1324 nvp_value = (nv_string_t *)&nvp_data->nv_data[0];
1325 if ((value = nvstring_get(nvp_value)) == NULL) {
1336 if (value != NULL) {
1337 rv = env_setenv(name, EV_VOLATILE | EV_NOHOOK, value,
1338 zfs_nvstore_sethook, zfs_nvstore_unsethook);
1346 zfs_nvstore_iterate(void *vdev, int (*cb)(void *, void *))
1348 struct zfs_devdesc *dev = (struct zfs_devdesc *)vdev;
1354 if (dev->dd.d_dev->dv_type != DEVT_ZFS)
1357 if ((spa = spa_find_by_dev(dev)) == NULL)
1360 if (spa->spa_bootenv == NULL)
1363 if (nvlist_find(spa->spa_bootenv, OS_NVSTORE, DATA_TYPE_NVLIST,
1364 NULL, &nv, NULL) != 0)
1369 while ((nvh = nvlist_next_nvpair(nv, nvh)) != NULL) {
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
1387 zfs_attach_nvstore(void *vdev)
1389 struct zfs_devdesc *dev = vdev;
1394 if (dev->dd.d_dev->dv_type != DEVT_ZFS)
1397 if ((spa = spa_find_by_dev(dev)) == NULL)
1400 rv = nvlist_find(spa->spa_bootenv, BOOTENV_VERSION, DATA_TYPE_UINT64,
1401 NULL, &version, NULL);
1403 if (rv != 0 || version != VB_NVLIST) {
1407 dev = malloc(sizeof (*dev));
1410 memcpy(dev, vdev, sizeof (*dev));
1412 rv = nvstore_init(spa->spa_name, &nvstore_zfs_cb, dev);
1416 rv = zfs_nvstore_iterate(dev, zfs_nvstore_setenv);
1421 zfs_probe_dev(const char *devname, uint64_t *pool_guid)
1423 struct ptable *table;
1424 struct zfs_probe_args pa;
1430 pa.fd = open(devname, O_RDWR);
1433 /* Probe the whole disk */
1434 ret = zfs_probe(pa.fd, pool_guid);
1438 /* Probe each partition */
1439 ret = ioctl(pa.fd, DIOCGMEDIASIZE, &mediasz);
1441 ret = ioctl(pa.fd, DIOCGSECTORSIZE, &pa.secsz);
1443 pa.devname = devname;
1444 pa.pool_guid = pool_guid;
1445 table = ptable_open(&pa, mediasz / pa.secsz, pa.secsz,
1447 if (table != NULL) {
1448 ptable_iterate(table, &pa, zfs_probe_partition);
1449 ptable_close(table);
1453 if (pool_guid && *pool_guid == 0)
1459 * Print information about ZFS pools
1462 zfs_dev_print(int verbose)
1468 if (STAILQ_EMPTY(&zfs_pools))
1471 printf("%s devices:", zfs_dev.dv_name);
1472 if ((ret = pager_output("\n")) != 0)
1476 return (spa_all_status());
1478 STAILQ_FOREACH(spa, &zfs_pools, spa_link) {
1479 snprintf(line, sizeof(line), " zfs:%s\n", spa->spa_name);
1480 ret = pager_output(line);
1488 * Attempt to open the pool described by (dev) for use by (f).
1491 zfs_dev_open(struct open_file *f, ...)
1494 struct zfs_devdesc *dev;
1495 struct zfsmount *mount;
1500 dev = va_arg(args, struct zfs_devdesc *);
1503 if ((spa = spa_find_by_dev(dev)) == NULL)
1506 mount = malloc(sizeof(*mount));
1510 rv = zfs_mount(spa, dev->root_guid, mount);
1515 if (mount->objset.os_type != DMU_OST_ZFS) {
1516 printf("Unexpected object set type %ju\n",
1517 (uintmax_t)mount->objset.os_type);
1521 f->f_devdata = mount;
1527 zfs_dev_close(struct open_file *f)
1531 f->f_devdata = NULL;
1536 zfs_dev_strategy(void *devdata, int rw, daddr_t dblk, size_t size, char *buf, size_t *rsize)
1542 struct devsw zfs_dev = {
1544 .dv_type = DEVT_ZFS,
1545 .dv_init = zfs_dev_init,
1546 .dv_strategy = zfs_dev_strategy,
1547 .dv_open = zfs_dev_open,
1548 .dv_close = zfs_dev_close,
1549 .dv_ioctl = noioctl,
1550 .dv_print = zfs_dev_print,
1555 zfs_parsedev(struct zfs_devdesc *dev, const char *devspec, const char **path)
1557 static char rootname[ZFS_MAXNAMELEN];
1558 static char poolname[ZFS_MAXNAMELEN];
1569 end = strrchr(np, ':');
1572 sep = strchr(np, '/');
1573 if (sep == NULL || sep >= end)
1575 memcpy(poolname, np, sep - np);
1576 poolname[sep - np] = '\0';
1579 memcpy(rootname, sep, end - sep);
1580 rootname[end - sep] = '\0';
1585 spa = spa_find_by_name(poolname);
1588 dev->pool_guid = spa->spa_guid;
1589 rv = zfs_lookup_dataset(spa, rootname, &dev->root_guid);
1593 *path = (*end == '\0') ? end : end + 1;
1594 dev->dd.d_dev = &zfs_dev;
1599 zfs_fmtdev(void *vdev)
1601 static char rootname[ZFS_MAXNAMELEN];
1602 static char buf[2 * ZFS_MAXNAMELEN + 8];
1603 struct zfs_devdesc *dev = (struct zfs_devdesc *)vdev;
1607 if (dev->dd.d_dev->dv_type != DEVT_ZFS)
1610 /* Do we have any pools? */
1611 spa = STAILQ_FIRST(&zfs_pools);
1615 if (dev->pool_guid == 0)
1616 dev->pool_guid = spa->spa_guid;
1618 spa = spa_find_by_guid(dev->pool_guid);
1621 printf("ZFS: can't find pool by guid\n");
1624 if (dev->root_guid == 0 && zfs_get_root(spa, &dev->root_guid)) {
1625 printf("ZFS: can't find root filesystem\n");
1628 if (zfs_rlookup(spa, dev->root_guid, rootname)) {
1629 printf("ZFS: can't find filesystem by guid\n");
1633 if (rootname[0] == '\0')
1634 snprintf(buf, sizeof(buf), "%s:%s:", dev->dd.d_dev->dv_name,
1637 snprintf(buf, sizeof(buf), "%s:%s/%s:", dev->dd.d_dev->dv_name,
1638 spa->spa_name, rootname);
1643 split_devname(const char *name, char *poolname, size_t size,
1644 const char **dsnamep)
1649 ASSERT(name != NULL);
1650 ASSERT(poolname != NULL);
1653 dsname = strchr(name, '/');
1654 if (dsname != NULL) {
1655 len = dsname - name;
1663 strlcpy(poolname, name, len + 1);
1665 if (dsnamep != NULL)
1672 zfs_list(const char *name)
1674 static char poolname[ZFS_MAXNAMELEN];
1680 if (split_devname(name, poolname, sizeof(poolname), &dsname) != 0)
1683 spa = spa_find_by_name(poolname);
1686 rv = zfs_lookup_dataset(spa, dsname, &objid);
1690 return (zfs_list_dataset(spa, objid));
1694 init_zfs_boot_options(const char *currdev_in)
1696 char poolname[ZFS_MAXNAMELEN];
1697 char *beroot, *currdev;
1703 currdev_len = strlen(currdev_in);
1704 if (currdev_len == 0)
1706 if (strncmp(currdev_in, "zfs:", 4) != 0)
1708 currdev = strdup(currdev_in);
1709 if (currdev == NULL)
1711 /* Remove the trailing : */
1712 currdev[currdev_len - 1] = '\0';
1714 setenv("zfs_be_active", currdev, 1);
1715 setenv("zfs_be_currpage", "1", 1);
1716 /* Remove the last element (current bootenv) */
1717 beroot = strrchr(currdev, '/');
1720 beroot = strchr(currdev, ':') + 1;
1721 setenv("zfs_be_root", beroot, 1);
1723 if (split_devname(beroot, poolname, sizeof(poolname), &dsname) != 0)
1726 spa = spa_find_by_name(poolname);
1730 zfs_bootenv_initial("bootenvs", spa, beroot, dsname, 0);
1731 zfs_checkpoints_initial(spa, beroot, dsname);
1737 zfs_checkpoints_initial(spa_t *spa, const char *name, const char *dsname)
1741 if (spa->spa_uberblock_checkpoint.ub_checkpoint_txg != 0) {
1742 snprintf(envname, sizeof(envname), "zpool_checkpoint");
1743 setenv(envname, name, 1);
1745 spa->spa_uberblock = &spa->spa_uberblock_checkpoint;
1746 spa->spa_mos = &spa->spa_mos_checkpoint;
1748 zfs_bootenv_initial("bootenvs_check", spa, name, dsname, 1);
1750 spa->spa_uberblock = &spa->spa_uberblock_master;
1751 spa->spa_mos = &spa->spa_mos_master;
1756 zfs_bootenv_initial(const char *envprefix, spa_t *spa, const char *rootname,
1757 const char *dsname, int checkpoint)
1759 char envname[32], envval[256];
1761 int bootenvs_idx, rv;
1763 SLIST_INIT(&zfs_be_head);
1766 rv = zfs_lookup_dataset(spa, dsname, &objid);
1770 rv = zfs_callback_dataset(spa, objid, zfs_belist_add);
1772 /* Populate the initial environment variables */
1773 SLIST_FOREACH_SAFE(zfs_be, &zfs_be_head, entries, zfs_be_tmp) {
1774 /* Enumerate all bootenvs for general usage */
1775 snprintf(envname, sizeof(envname), "%s[%d]",
1776 envprefix, bootenvs_idx);
1777 snprintf(envval, sizeof(envval), "zfs:%s%s/%s",
1778 checkpoint ? "!" : "", rootname, zfs_be->name);
1779 rv = setenv(envname, envval, 1);
1784 snprintf(envname, sizeof(envname), "%s_count", envprefix);
1785 snprintf(envval, sizeof(envval), "%d", bootenvs_idx);
1786 setenv(envname, envval, 1);
1788 /* Clean up the SLIST of ZFS BEs */
1789 while (!SLIST_EMPTY(&zfs_be_head)) {
1790 zfs_be = SLIST_FIRST(&zfs_be_head);
1791 SLIST_REMOVE_HEAD(&zfs_be_head, entries);
1798 zfs_bootenv(const char *name)
1800 char poolname[ZFS_MAXNAMELEN], *root;
1805 int rv, pages, perpage, currpage;
1809 if ((root = getenv("zfs_be_root")) == NULL)
1812 if (strcmp(name, root) != 0) {
1813 if (setenv("zfs_be_root", name, 1) != 0)
1817 SLIST_INIT(&zfs_be_head);
1820 if (split_devname(name, poolname, sizeof(poolname), &dsname) != 0)
1823 spa = spa_find_by_name(poolname);
1826 rv = zfs_lookup_dataset(spa, dsname, &objid);
1829 rv = zfs_callback_dataset(spa, objid, zfs_belist_add);
1831 /* Calculate and store the number of pages of BEs */
1832 perpage = (ZFS_BE_LAST - ZFS_BE_FIRST + 1);
1833 pages = (zfs_env_count / perpage) + ((zfs_env_count % perpage) > 0 ? 1 : 0);
1834 snprintf(becount, 4, "%d", pages);
1835 if (setenv("zfs_be_pages", becount, 1) != 0)
1838 /* Roll over the page counter if it has exceeded the maximum */
1839 currpage = strtol(getenv("zfs_be_currpage"), NULL, 10);
1840 if (currpage > pages) {
1841 if (setenv("zfs_be_currpage", "1", 1) != 0)
1845 /* Populate the menu environment variables */
1848 /* Clean up the SLIST of ZFS BEs */
1849 while (!SLIST_EMPTY(&zfs_be_head)) {
1850 zfs_be = SLIST_FIRST(&zfs_be_head);
1851 SLIST_REMOVE_HEAD(&zfs_be_head, entries);
1860 zfs_belist_add(const char *name, uint64_t value __unused)
1863 /* Skip special datasets that start with a $ character */
1864 if (strncmp(name, "$", 1) == 0) {
1867 /* Add the boot environment to the head of the SLIST */
1868 zfs_be = malloc(sizeof(struct zfs_be_entry));
1869 if (zfs_be == NULL) {
1872 zfs_be->name = strdup(name);
1873 if (zfs_be->name == NULL) {
1877 SLIST_INSERT_HEAD(&zfs_be_head, zfs_be, entries);
1886 char envname[32], envval[256];
1887 char *beroot, *pagenum;
1890 beroot = getenv("zfs_be_root");
1891 if (beroot == NULL) {
1895 pagenum = getenv("zfs_be_currpage");
1896 if (pagenum != NULL) {
1897 page = strtol(pagenum, NULL, 10);
1904 zfs_env_index = ZFS_BE_FIRST;
1905 SLIST_FOREACH_SAFE(zfs_be, &zfs_be_head, entries, zfs_be_tmp) {
1906 /* Skip to the requested page number */
1907 if (ctr <= ((ZFS_BE_LAST - ZFS_BE_FIRST + 1) * (page - 1))) {
1912 snprintf(envname, sizeof(envname), "bootenvmenu_caption[%d]", zfs_env_index);
1913 snprintf(envval, sizeof(envval), "%s", zfs_be->name);
1914 rv = setenv(envname, envval, 1);
1919 snprintf(envname, sizeof(envname), "bootenvansi_caption[%d]", zfs_env_index);
1920 rv = setenv(envname, envval, 1);
1925 snprintf(envname, sizeof(envname), "bootenvmenu_command[%d]", zfs_env_index);
1926 rv = setenv(envname, "set_bootenv", 1);
1931 snprintf(envname, sizeof(envname), "bootenv_root[%d]", zfs_env_index);
1932 snprintf(envval, sizeof(envval), "zfs:%s/%s", beroot, zfs_be->name);
1933 rv = setenv(envname, envval, 1);
1939 if (zfs_env_index > ZFS_BE_LAST) {
1945 for (; zfs_env_index <= ZFS_BE_LAST; zfs_env_index++) {
1946 snprintf(envname, sizeof(envname), "bootenvmenu_caption[%d]", zfs_env_index);
1947 (void)unsetenv(envname);
1948 snprintf(envname, sizeof(envname), "bootenvansi_caption[%d]", zfs_env_index);
1949 (void)unsetenv(envname);
1950 snprintf(envname, sizeof(envname), "bootenvmenu_command[%d]", zfs_env_index);
1951 (void)unsetenv(envname);
1952 snprintf(envname, sizeof(envname), "bootenv_root[%d]", zfs_env_index);
1953 (void)unsetenv(envname);