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
27 #include <sys/cdefs.h>
29 * Stand-alone file reading package.
34 #include <sys/param.h>
36 #include <sys/queue.h>
41 #include <bootstrap.h>
47 /* Define the range of indexes to be populated with ZFS Boot Environments */
48 #define ZFS_BE_FIRST 4
51 static int zfs_open(const char *path, struct open_file *f);
52 static int zfs_close(struct open_file *f);
53 static int zfs_read(struct open_file *f, void *buf, size_t size, size_t *resid);
54 static off_t zfs_seek(struct open_file *f, off_t offset, int where);
55 static int zfs_stat(struct open_file *f, struct stat *sb);
56 static int zfs_readdir(struct open_file *f, struct dirent *d);
57 static int zfs_mount(const char *dev, const char *path, void **data);
58 static int zfs_unmount(const char *dev, void *data);
60 static void zfs_bootenv_initial(const char *envname, spa_t *spa,
61 const char *name, const char *dsname, int checkpoint);
62 static void zfs_checkpoints_initial(spa_t *spa, const char *name,
65 static int zfs_parsedev(struct devdesc **idev, const char *devspec,
70 struct fs_ops zfs_fsops = {
73 .fo_close = zfs_close,
75 .fo_write = null_write,
78 .fo_readdir = zfs_readdir,
79 .fo_mount = zfs_mount,
80 .fo_unmount = zfs_unmount
87 off_t f_seekp; /* seek pointer */
89 uint64_t f_zap_type; /* zap type for readdir */
90 uint64_t f_num_leafs; /* number of fzap leaf blocks */
91 zap_leaf_phys_t *f_zap_leaf; /* zap leaf buffer */
94 static int zfs_env_index;
95 static int zfs_env_count;
97 SLIST_HEAD(zfs_be_list, zfs_be_entry) zfs_be_head = SLIST_HEAD_INITIALIZER(zfs_be_head);
98 struct zfs_be_list *zfs_be_headp;
101 SLIST_ENTRY(zfs_be_entry) entries;
102 } *zfs_be, *zfs_be_tmp;
108 zfs_open(const char *upath, struct open_file *f)
110 struct devdesc *dev = f->f_devdata;
111 struct zfsmount *mount = dev->d_opendata;
115 if (f->f_dev != &zfs_dev)
118 /* allocate file system specific data structure */
119 fp = calloc(1, sizeof(struct file));
124 rc = zfs_lookup(mount, upath, &fp->f_dnode);
134 zfs_close(struct open_file *f)
136 struct file *fp = (struct file *)f->f_fsdata;
138 dnode_cache_obj = NULL;
146 * Copy a portion of a file into kernel memory.
147 * Cross block boundaries when necessary.
150 zfs_read(struct open_file *f, void *start, size_t size, size_t *resid /* out */)
152 struct devdesc *dev = f->f_devdata;
153 const spa_t *spa = ((struct zfsmount *)dev->d_opendata)->spa;
154 struct file *fp = (struct file *)f->f_fsdata;
159 rc = zfs_stat(f, &sb);
163 if (fp->f_seekp + n > sb.st_size)
164 n = sb.st_size - fp->f_seekp;
166 rc = dnode_read(spa, &fp->f_dnode, fp->f_seekp, start, n);
172 for (i = 0; i < n; i++)
173 putchar(((char*) start)[i]);
183 zfs_seek(struct open_file *f, off_t offset, int where)
185 struct file *fp = (struct file *)f->f_fsdata;
189 fp->f_seekp = offset;
192 fp->f_seekp += offset;
199 error = zfs_stat(f, &sb);
204 fp->f_seekp = sb.st_size - offset;
211 return (fp->f_seekp);
215 zfs_stat(struct open_file *f, struct stat *sb)
217 struct devdesc *dev = f->f_devdata;
218 const spa_t *spa = ((struct zfsmount *)dev->d_opendata)->spa;
219 struct file *fp = (struct file *)f->f_fsdata;
221 return (zfs_dnode_stat(spa, &fp->f_dnode, sb));
225 zfs_readdir(struct open_file *f, struct dirent *d)
227 struct devdesc *dev = f->f_devdata;
228 const spa_t *spa = ((struct zfsmount *)dev->d_opendata)->spa;
229 struct file *fp = (struct file *)f->f_fsdata;
232 size_t bsize = fp->f_dnode.dn_datablkszsec << SPA_MINBLOCKSHIFT;
235 rc = zfs_stat(f, &sb);
238 if (!S_ISDIR(sb.st_mode))
242 * If this is the first read, get the zap type.
244 if (fp->f_seekp == 0) {
245 rc = dnode_read(spa, &fp->f_dnode,
246 0, &fp->f_zap_type, sizeof(fp->f_zap_type));
250 if (fp->f_zap_type == ZBT_MICRO) {
251 fp->f_seekp = offsetof(mzap_phys_t, mz_chunk);
253 rc = dnode_read(spa, &fp->f_dnode,
254 offsetof(zap_phys_t, zap_num_leafs),
256 sizeof(fp->f_num_leafs));
261 fp->f_zap_leaf = malloc(bsize);
262 if (fp->f_zap_leaf == NULL)
264 rc = dnode_read(spa, &fp->f_dnode,
273 if (fp->f_zap_type == ZBT_MICRO) {
275 if (fp->f_seekp >= bsize)
278 rc = dnode_read(spa, &fp->f_dnode,
279 fp->f_seekp, &mze, sizeof(mze));
282 fp->f_seekp += sizeof(mze);
284 if (!mze.mze_name[0])
287 d->d_fileno = ZFS_DIRENT_OBJ(mze.mze_value);
288 d->d_type = ZFS_DIRENT_TYPE(mze.mze_value);
289 strcpy(d->d_name, mze.mze_name);
290 d->d_namlen = strlen(d->d_name);
294 zap_leaf_chunk_t *zc, *nc;
301 * Initialise this so we can use the ZAP size
302 * calculating macros.
304 zl.l_bs = ilog2(bsize);
305 zl.l_phys = fp->f_zap_leaf;
308 * Figure out which chunk we are currently looking at
309 * and consider seeking to the next leaf. We use the
310 * low bits of f_seekp as a simple chunk index.
313 chunk = fp->f_seekp & (bsize - 1);
314 if (chunk == ZAP_LEAF_NUMCHUNKS(&zl)) {
315 fp->f_seekp = rounddown2(fp->f_seekp, bsize) + bsize;
319 * Check for EOF and read the new leaf.
321 if (fp->f_seekp >= bsize * fp->f_num_leafs)
324 rc = dnode_read(spa, &fp->f_dnode,
332 zc = &ZAP_LEAF_CHUNK(&zl, chunk);
334 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
337 namelen = zc->l_entry.le_name_numints;
338 if (namelen > sizeof(d->d_name))
339 namelen = sizeof(d->d_name);
342 * Paste the name back together.
344 nc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_name_chunk);
346 while (namelen > 0) {
349 if (len > ZAP_LEAF_ARRAY_BYTES)
350 len = ZAP_LEAF_ARRAY_BYTES;
351 memcpy(p, nc->l_array.la_array, len);
354 nc = &ZAP_LEAF_CHUNK(&zl, nc->l_array.la_next);
356 d->d_name[sizeof(d->d_name) - 1] = 0;
359 * Assume the first eight bytes of the value are
362 value = fzap_leaf_value(&zl, zc);
364 d->d_fileno = ZFS_DIRENT_OBJ(value);
365 d->d_type = ZFS_DIRENT_TYPE(value);
366 d->d_namlen = strlen(d->d_name);
373 spa_find_by_dev(struct zfs_devdesc *dev)
376 if (dev->dd.d_dev->dv_type != DEVT_ZFS)
379 if (dev->pool_guid == 0)
380 return (STAILQ_FIRST(&zfs_pools));
382 return (spa_find_by_guid(dev->pool_guid));
386 * if path is NULL, create mount structure, but do not add it to list.
389 zfs_mount(const char *dev, const char *path, void **data)
391 struct zfs_devdesc *zfsdev = NULL;
393 struct zfsmount *mnt = NULL;
397 rv = zfs_parsedev((struct devdesc **)&zfsdev, dev, NULL);
402 spa = spa_find_by_dev(zfsdev);
408 mnt = calloc(1, sizeof(*mnt));
414 if (mnt->path != NULL) {
415 mnt->path = strdup(path);
416 if (mnt->path == NULL) {
422 rv = zfs_mount_impl(spa, zfsdev->root_guid, mnt);
424 if (rv == 0 && mnt->objset.os_type != DMU_OST_ZFS) {
425 printf("Unexpected object set type %ju\n",
426 (uintmax_t)mnt->objset.os_type);
440 STAILQ_INSERT_TAIL(&zfsmount, mnt, next);
448 zfs_unmount(const char *dev, void *data)
450 struct zfsmount *mnt = data;
452 STAILQ_REMOVE(&zfsmount, mnt, zfsmount, next);
459 vdev_read(vdev_t *vdev, void *priv, off_t offset, void *buf, size_t bytes)
462 size_t res, head, tail, total_size, full_sec_size;
463 unsigned secsz, do_tail_read;
465 char *outbuf, *bouncebuf;
467 fd = (uintptr_t) priv;
468 outbuf = (char *) buf;
471 ret = ioctl(fd, DIOCGSECTORSIZE, &secsz);
476 * Handling reads of arbitrary offset and size - multi-sector case
477 * and single-sector case.
480 * (do_tail_read = true if tail > 0)
482 * |<----------------------total_size--------------------->|
484 * |<--head-->|<--------------bytes------------>|<--tail-->|
486 * | | |<~full_sec_size~>| | |
487 * +------------------+ +------------------+
488 * | |0101010| . . . |0101011| |
489 * +------------------+ +------------------+
490 * start_sec start_sec + n
494 * (do_tail_read = false)
496 * |<------total_size = secsz----->|
498 * |<-head->|<---bytes--->|<-tail->|
499 * +-------------------------------+
500 * | |0101010101010| |
501 * +-------------------------------+
504 start_sec = offset / secsz;
505 head = offset % secsz;
506 total_size = roundup2(head + bytes, secsz);
507 tail = total_size - (head + bytes);
508 do_tail_read = ((tail > 0) && (head + bytes > secsz));
509 full_sec_size = total_size;
511 full_sec_size -= secsz;
513 full_sec_size -= secsz;
515 /* Return of partial sector data requires a bounce buffer. */
516 if ((head > 0) || do_tail_read || bytes < secsz) {
517 bouncebuf = malloc(secsz);
518 if (bouncebuf == NULL) {
519 printf("vdev_read: out of memory\n");
524 if (lseek(fd, start_sec * secsz, SEEK_SET) == -1) {
529 /* Partial data return from first sector */
531 res = read(fd, bouncebuf, secsz);
536 memcpy(outbuf, bouncebuf + head, min(secsz - head, bytes));
537 outbuf += min(secsz - head, bytes);
541 * Full data return from read sectors.
542 * Note, there is still corner case where we read
543 * from sector boundary, but less than sector size, e.g. reading 512B
546 if (full_sec_size > 0) {
547 if (bytes < full_sec_size) {
548 res = read(fd, bouncebuf, secsz);
553 memcpy(outbuf, bouncebuf, bytes);
555 res = read(fd, outbuf, full_sec_size);
556 if (res != full_sec_size) {
560 outbuf += full_sec_size;
564 /* Partial data return from last sector */
566 res = read(fd, bouncebuf, secsz);
571 memcpy(outbuf, bouncebuf, secsz - tail);
581 vdev_write(vdev_t *vdev, off_t offset, void *buf, size_t bytes)
584 size_t head, tail, total_size, full_sec_size;
585 unsigned secsz, do_tail_write;
588 char *outbuf, *bouncebuf;
590 fd = (uintptr_t)vdev->v_priv;
591 outbuf = (char *)buf;
594 ret = ioctl(fd, DIOCGSECTORSIZE, &secsz);
598 start_sec = offset / secsz;
599 head = offset % secsz;
600 total_size = roundup2(head + bytes, secsz);
601 tail = total_size - (head + bytes);
602 do_tail_write = ((tail > 0) && (head + bytes > secsz));
603 full_sec_size = total_size;
605 full_sec_size -= secsz;
607 full_sec_size -= secsz;
609 /* Partial sector write requires a bounce buffer. */
610 if ((head > 0) || do_tail_write || bytes < secsz) {
611 bouncebuf = malloc(secsz);
612 if (bouncebuf == NULL) {
613 printf("vdev_write: out of memory\n");
618 if (lseek(fd, start_sec * secsz, SEEK_SET) == -1) {
623 /* Partial data for first sector */
625 res = read(fd, bouncebuf, secsz);
626 if ((unsigned)res != secsz) {
630 memcpy(bouncebuf + head, outbuf, min(secsz - head, bytes));
631 (void) lseek(fd, -secsz, SEEK_CUR);
632 res = write(fd, bouncebuf, secsz);
633 if ((unsigned)res != secsz) {
637 outbuf += min(secsz - head, bytes);
641 * Full data write to sectors.
642 * Note, there is still corner case where we write
643 * to sector boundary, but less than sector size, e.g. write 512B
646 if (full_sec_size > 0) {
647 if (bytes < full_sec_size) {
648 res = read(fd, bouncebuf, secsz);
649 if ((unsigned)res != secsz) {
653 memcpy(bouncebuf, outbuf, bytes);
654 (void) lseek(fd, -secsz, SEEK_CUR);
655 res = write(fd, bouncebuf, secsz);
656 if ((unsigned)res != secsz) {
661 res = write(fd, outbuf, full_sec_size);
662 if ((unsigned)res != full_sec_size) {
666 outbuf += full_sec_size;
670 /* Partial data write to last sector */
672 res = read(fd, bouncebuf, secsz);
673 if ((unsigned)res != secsz) {
677 memcpy(bouncebuf, outbuf, secsz - tail);
678 (void) lseek(fd, -secsz, SEEK_CUR);
679 res = write(fd, bouncebuf, secsz);
680 if ((unsigned)res != secsz) {
700 if (archsw.arch_zfs_probe == NULL)
702 archsw.arch_zfs_probe();
705 spa = STAILQ_FIRST(&zfs_pools);
706 while (spa != NULL) {
707 next = STAILQ_NEXT(spa, spa_link);
708 if (zfs_spa_init(spa)) {
710 STAILQ_REMOVE_HEAD(&zfs_pools, spa_link);
712 STAILQ_REMOVE_AFTER(&zfs_pools, prev, spa_link);
720 struct zfs_probe_args {
728 zfs_diskread(void *arg, void *buf, size_t blocks, uint64_t offset)
730 struct zfs_probe_args *ppa;
732 ppa = (struct zfs_probe_args *)arg;
733 return (vdev_read(NULL, (void *)(uintptr_t)ppa->fd,
734 offset * ppa->secsz, buf, blocks * ppa->secsz));
738 zfs_probe(int fd, uint64_t *pool_guid)
744 ret = vdev_probe(vdev_read, vdev_write, (void *)(uintptr_t)fd, &spa);
745 if (ret == 0 && pool_guid != NULL)
747 *pool_guid = spa->spa_guid;
752 zfs_probe_partition(void *arg, const char *partname,
753 const struct ptable_entry *part)
755 struct zfs_probe_args *ppa, pa;
756 struct ptable *table;
760 /* Probe only freebsd-zfs and freebsd partitions */
761 if (part->type != PART_FREEBSD &&
762 part->type != PART_FREEBSD_ZFS)
765 ppa = (struct zfs_probe_args *)arg;
766 strncpy(devname, ppa->devname, strlen(ppa->devname) - 1);
767 devname[strlen(ppa->devname) - 1] = '\0';
768 snprintf(devname, sizeof(devname), "%s%s:", devname, partname);
769 pa.fd = open(devname, O_RDWR);
772 ret = zfs_probe(pa.fd, ppa->pool_guid);
775 /* Do we have BSD label here? */
776 if (part->type == PART_FREEBSD) {
777 pa.devname = devname;
778 pa.pool_guid = ppa->pool_guid;
779 pa.secsz = ppa->secsz;
780 table = ptable_open(&pa, part->end - part->start + 1,
781 ppa->secsz, zfs_diskread);
783 ptable_iterate(table, &pa, zfs_probe_partition);
792 * Return bootenv nvlist from pool label.
795 zfs_get_bootenv(void *vdev, nvlist_t **benvp)
799 if ((spa = spa_find_by_dev((struct zfs_devdesc *)vdev)) == NULL)
802 return (zfs_get_bootenv_spa(spa, benvp));
806 * Store nvlist to pool label bootenv area. Also updates cached pointer in spa.
809 zfs_set_bootenv(void *vdev, nvlist_t *benv)
813 if ((spa = spa_find_by_dev((struct zfs_devdesc *)vdev)) == NULL)
816 return (zfs_set_bootenv_spa(spa, benv));
820 * Get bootonce value by key. The bootonce <key, value> pair is removed
821 * from the bootenv nvlist and the remaining nvlist is committed back to disk.
824 zfs_get_bootonce(void *vdev, const char *key, char *buf, size_t size)
828 if ((spa = spa_find_by_dev((struct zfs_devdesc *)vdev)) == NULL)
831 return (zfs_get_bootonce_spa(spa, key, buf, size));
838 static int zfs_nvstore_setter(void *, int, const char *,
839 const void *, size_t);
840 static int zfs_nvstore_setter_str(void *, const char *, const char *,
842 static int zfs_nvstore_unset_impl(void *, const char *, bool);
843 static int zfs_nvstore_setenv(void *, void *);
846 * nvstore is only present for current rootfs pool.
849 zfs_nvstore_sethook(struct env_var *ev, int flags __unused, const void *value)
851 struct zfs_devdesc *dev;
854 archsw.arch_getdev((void **)&dev, NULL, NULL);
858 rv = zfs_nvstore_setter_str(dev, NULL, ev->ev_name, value);
865 * nvstore is only present for current rootfs pool.
868 zfs_nvstore_unsethook(struct env_var *ev)
870 struct zfs_devdesc *dev;
873 archsw.arch_getdev((void **)&dev, NULL, NULL);
877 rv = zfs_nvstore_unset_impl(dev, ev->ev_name, false);
884 zfs_nvstore_getter(void *vdev, const char *name, void **data)
886 struct zfs_devdesc *dev = (struct zfs_devdesc *)vdev;
893 if (dev->dd.d_dev->dv_type != DEVT_ZFS)
896 if ((spa = spa_find_by_dev(dev)) == NULL)
899 if (spa->spa_bootenv == NULL)
902 if (nvlist_find(spa->spa_bootenv, OS_NVSTORE, DATA_TYPE_NVLIST,
903 NULL, &nv, NULL) != 0)
906 rv = nvlist_find(nv, name, DATA_TYPE_STRING, NULL, &str, &size);
909 asprintf(ptr, "%.*s", size, str);
918 zfs_nvstore_setter(void *vdev, int type, const char *name,
919 const void *data, size_t size)
921 struct zfs_devdesc *dev = (struct zfs_devdesc *)vdev;
927 if (dev->dd.d_dev->dv_type != DEVT_ZFS)
930 if ((spa = spa_find_by_dev(dev)) == NULL)
933 if (spa->spa_bootenv == NULL)
936 if (nvlist_find(spa->spa_bootenv, OS_NVSTORE, DATA_TYPE_NVLIST,
937 NULL, &nv, NULL) != 0) {
938 nv = nvlist_create(NV_UNIQUE_NAME);
946 if (size != sizeof (int8_t)) {
950 rv = nvlist_add_int8(nv, name, *(int8_t *)data);
953 case DATA_TYPE_INT16:
954 if (size != sizeof (int16_t)) {
958 rv = nvlist_add_int16(nv, name, *(int16_t *)data);
961 case DATA_TYPE_INT32:
962 if (size != sizeof (int32_t)) {
966 rv = nvlist_add_int32(nv, name, *(int32_t *)data);
969 case DATA_TYPE_INT64:
970 if (size != sizeof (int64_t)) {
974 rv = nvlist_add_int64(nv, name, *(int64_t *)data);
978 if (size != sizeof (uint8_t)) {
982 rv = nvlist_add_byte(nv, name, *(int8_t *)data);
985 case DATA_TYPE_UINT8:
986 if (size != sizeof (uint8_t)) {
990 rv = nvlist_add_uint8(nv, name, *(int8_t *)data);
993 case DATA_TYPE_UINT16:
994 if (size != sizeof (uint16_t)) {
998 rv = nvlist_add_uint16(nv, name, *(uint16_t *)data);
1001 case DATA_TYPE_UINT32:
1002 if (size != sizeof (uint32_t)) {
1006 rv = nvlist_add_uint32(nv, name, *(uint32_t *)data);
1009 case DATA_TYPE_UINT64:
1010 if (size != sizeof (uint64_t)) {
1014 rv = nvlist_add_uint64(nv, name, *(uint64_t *)data);
1017 case DATA_TYPE_STRING:
1018 rv = nvlist_add_string(nv, name, data);
1021 case DATA_TYPE_BOOLEAN_VALUE:
1022 if (size != sizeof (boolean_t)) {
1026 rv = nvlist_add_boolean_value(nv, name, *(boolean_t *)data);
1035 rv = nvlist_add_nvlist(spa->spa_bootenv, OS_NVSTORE, nv);
1037 rv = zfs_set_bootenv(vdev, spa->spa_bootenv);
1041 rv = zfs_nvstore_setenv(vdev,
1042 nvpair_find(nv, name));
1044 env_discard(env_getenv(name));
1055 get_int64(const char *data, int64_t *ip)
1061 val = strtoll(data, &end, 0);
1062 if (errno != 0 || *data == '\0' || *end != '\0')
1070 get_uint64(const char *data, uint64_t *ip)
1076 val = strtoull(data, &end, 0);
1077 if (errno != 0 || *data == '\0' || *end != '\0')
1085 * Translate textual data to data type. If type is not set, and we are
1086 * creating new pair, use DATA_TYPE_STRING.
1089 zfs_nvstore_setter_str(void *vdev, const char *type, const char *name,
1092 struct zfs_devdesc *dev = (struct zfs_devdesc *)vdev;
1100 if (dev->dd.d_dev->dv_type != DEVT_ZFS)
1103 if ((spa = spa_find_by_dev(dev)) == NULL)
1106 if (spa->spa_bootenv == NULL)
1109 if (nvlist_find(spa->spa_bootenv, OS_NVSTORE, DATA_TYPE_NVLIST,
1110 NULL, &nv, NULL) != 0) {
1118 * if there is no existing pair, default to string.
1119 * Otherwise, use type from existing pair.
1121 nvh = nvpair_find(nv, name);
1123 dt = DATA_TYPE_STRING;
1125 nv_string_t *nvp_name;
1126 nv_pair_data_t *nvp_data;
1128 nvp_name = (nv_string_t *)(nvh + 1);
1129 nvp_data = (nv_pair_data_t *)(&nvp_name->nv_data[0] +
1130 NV_ALIGN4(nvp_name->nv_size));
1131 dt = nvp_data->nv_type;
1134 dt = nvpair_type_from_name(type);
1140 case DATA_TYPE_INT8:
1141 rv = get_int64(data, &val);
1145 rv = zfs_nvstore_setter(vdev, dt, name, &v, sizeof (v));
1148 case DATA_TYPE_INT16:
1149 rv = get_int64(data, &val);
1153 rv = zfs_nvstore_setter(vdev, dt, name, &v, sizeof (v));
1156 case DATA_TYPE_INT32:
1157 rv = get_int64(data, &val);
1161 rv = zfs_nvstore_setter(vdev, dt, name, &v, sizeof (v));
1164 case DATA_TYPE_INT64:
1165 rv = get_int64(data, &val);
1167 rv = zfs_nvstore_setter(vdev, dt, name, &val,
1172 case DATA_TYPE_BYTE:
1173 rv = get_uint64(data, &uval);
1177 rv = zfs_nvstore_setter(vdev, dt, name, &v, sizeof (v));
1181 case DATA_TYPE_UINT8:
1182 rv = get_uint64(data, &uval);
1186 rv = zfs_nvstore_setter(vdev, dt, name, &v, sizeof (v));
1190 case DATA_TYPE_UINT16:
1191 rv = get_uint64(data, &uval);
1195 rv = zfs_nvstore_setter(vdev, dt, name, &v, sizeof (v));
1199 case DATA_TYPE_UINT32:
1200 rv = get_uint64(data, &uval);
1204 rv = zfs_nvstore_setter(vdev, dt, name, &v, sizeof (v));
1208 case DATA_TYPE_UINT64:
1209 rv = get_uint64(data, &uval);
1211 rv = zfs_nvstore_setter(vdev, dt, name, &uval,
1216 case DATA_TYPE_STRING:
1217 rv = zfs_nvstore_setter(vdev, dt, name, data, strlen(data) + 1);
1220 case DATA_TYPE_BOOLEAN_VALUE:
1221 rv = get_int64(data, &val);
1225 rv = zfs_nvstore_setter(vdev, dt, name, &v, sizeof (v));
1235 zfs_nvstore_unset_impl(void *vdev, const char *name, bool unset_env)
1237 struct zfs_devdesc *dev = (struct zfs_devdesc *)vdev;
1242 if (dev->dd.d_dev->dv_type != DEVT_ZFS)
1245 if ((spa = spa_find_by_dev(dev)) == NULL)
1248 if (spa->spa_bootenv == NULL)
1251 if (nvlist_find(spa->spa_bootenv, OS_NVSTORE, DATA_TYPE_NVLIST,
1252 NULL, &nv, NULL) != 0)
1255 rv = nvlist_remove(nv, name, DATA_TYPE_UNKNOWN);
1257 if (nvlist_next_nvpair(nv, NULL) == NULL) {
1258 rv = nvlist_remove(spa->spa_bootenv, OS_NVSTORE,
1261 rv = nvlist_add_nvlist(spa->spa_bootenv,
1265 rv = zfs_set_bootenv(vdev, spa->spa_bootenv);
1269 env_discard(env_getenv(name));
1274 zfs_nvstore_unset(void *vdev, const char *name)
1276 return (zfs_nvstore_unset_impl(vdev, name, true));
1280 zfs_nvstore_print(void *vdev __unused, void *ptr)
1283 nvpair_print(ptr, 0);
1288 * Create environment variable from nvpair.
1289 * set hook will update nvstore with new value, unset hook will remove
1290 * variable from nvstore.
1293 zfs_nvstore_setenv(void *vdev __unused, void *ptr)
1295 nvp_header_t *nvh = ptr;
1296 nv_string_t *nvp_name, *nvp_value;
1297 nv_pair_data_t *nvp_data;
1304 nvp_name = (nv_string_t *)(nvh + 1);
1305 nvp_data = (nv_pair_data_t *)(&nvp_name->nv_data[0] +
1306 NV_ALIGN4(nvp_name->nv_size));
1308 if ((name = nvstring_get(nvp_name)) == NULL)
1312 switch (nvp_data->nv_type) {
1313 case DATA_TYPE_BYTE:
1314 case DATA_TYPE_UINT8:
1315 (void) asprintf(&value, "%uc",
1316 *(unsigned *)&nvp_data->nv_data[0]);
1321 case DATA_TYPE_INT8:
1322 (void) asprintf(&value, "%c", *(int *)&nvp_data->nv_data[0]);
1327 case DATA_TYPE_INT16:
1328 (void) asprintf(&value, "%hd", *(short *)&nvp_data->nv_data[0]);
1333 case DATA_TYPE_UINT16:
1334 (void) asprintf(&value, "%hu",
1335 *(unsigned short *)&nvp_data->nv_data[0]);
1340 case DATA_TYPE_BOOLEAN_VALUE:
1341 case DATA_TYPE_INT32:
1342 (void) asprintf(&value, "%d", *(int *)&nvp_data->nv_data[0]);
1347 case DATA_TYPE_UINT32:
1348 (void) asprintf(&value, "%u",
1349 *(unsigned *)&nvp_data->nv_data[0]);
1354 case DATA_TYPE_INT64:
1355 (void) asprintf(&value, "%jd",
1356 (intmax_t)*(int64_t *)&nvp_data->nv_data[0]);
1361 case DATA_TYPE_UINT64:
1362 (void) asprintf(&value, "%ju",
1363 (uintmax_t)*(uint64_t *)&nvp_data->nv_data[0]);
1368 case DATA_TYPE_STRING:
1369 nvp_value = (nv_string_t *)&nvp_data->nv_data[0];
1370 if ((value = nvstring_get(nvp_value)) == NULL) {
1381 if (value != NULL) {
1382 rv = env_setenv(name, EV_VOLATILE | EV_NOHOOK, value,
1383 zfs_nvstore_sethook, zfs_nvstore_unsethook);
1391 zfs_nvstore_iterate(void *vdev, int (*cb)(void *, void *))
1393 struct zfs_devdesc *dev = (struct zfs_devdesc *)vdev;
1399 if (dev->dd.d_dev->dv_type != DEVT_ZFS)
1402 if ((spa = spa_find_by_dev(dev)) == NULL)
1405 if (spa->spa_bootenv == NULL)
1408 if (nvlist_find(spa->spa_bootenv, OS_NVSTORE, DATA_TYPE_NVLIST,
1409 NULL, &nv, NULL) != 0)
1414 while ((nvh = nvlist_next_nvpair(nv, nvh)) != NULL) {
1422 nvs_callbacks_t nvstore_zfs_cb = {
1423 .nvs_getter = zfs_nvstore_getter,
1424 .nvs_setter = zfs_nvstore_setter,
1425 .nvs_setter_str = zfs_nvstore_setter_str,
1426 .nvs_unset = zfs_nvstore_unset,
1427 .nvs_print = zfs_nvstore_print,
1428 .nvs_iterate = zfs_nvstore_iterate
1432 zfs_attach_nvstore(void *vdev)
1434 struct zfs_devdesc *dev = vdev;
1439 if (dev->dd.d_dev->dv_type != DEVT_ZFS)
1442 if ((spa = spa_find_by_dev(dev)) == NULL)
1445 rv = nvlist_find(spa->spa_bootenv, BOOTENV_VERSION, DATA_TYPE_UINT64,
1446 NULL, &version, NULL);
1448 if (rv != 0 || version != VB_NVLIST) {
1452 dev = malloc(sizeof (*dev));
1455 memcpy(dev, vdev, sizeof (*dev));
1457 rv = nvstore_init(spa->spa_name, &nvstore_zfs_cb, dev);
1461 rv = zfs_nvstore_iterate(dev, zfs_nvstore_setenv);
1466 zfs_probe_dev(const char *devname, uint64_t *pool_guid, bool parts_too)
1468 struct ptable *table;
1469 struct zfs_probe_args pa;
1475 pa.fd = open(devname, O_RDWR);
1478 /* Probe the whole disk */
1479 ret = zfs_probe(pa.fd, pool_guid);
1485 /* Probe each partition */
1486 ret = ioctl(pa.fd, DIOCGMEDIASIZE, &mediasz);
1488 ret = ioctl(pa.fd, DIOCGSECTORSIZE, &pa.secsz);
1490 pa.devname = devname;
1491 pa.pool_guid = pool_guid;
1492 table = ptable_open(&pa, mediasz / pa.secsz, pa.secsz,
1494 if (table != NULL) {
1495 ptable_iterate(table, &pa, zfs_probe_partition);
1496 ptable_close(table);
1500 if (pool_guid && *pool_guid == 0)
1506 * Print information about ZFS pools
1509 zfs_dev_print(int verbose)
1515 if (STAILQ_EMPTY(&zfs_pools))
1518 printf("%s devices:", zfs_dev.dv_name);
1519 if ((ret = pager_output("\n")) != 0)
1523 return (spa_all_status());
1525 STAILQ_FOREACH(spa, &zfs_pools, spa_link) {
1526 snprintf(line, sizeof(line), " zfs:%s\n", spa->spa_name);
1527 ret = pager_output(line);
1535 * Attempt to open the pool described by (dev) for use by (f).
1538 zfs_dev_open(struct open_file *f, ...)
1541 struct zfs_devdesc *dev;
1542 struct zfsmount *mount;
1547 dev = va_arg(args, struct zfs_devdesc *);
1550 if ((spa = spa_find_by_dev(dev)) == NULL)
1553 STAILQ_FOREACH(mount, &zfsmount, next) {
1554 if (spa->spa_guid == mount->spa->spa_guid)
1559 /* This device is not set as currdev, mount us private copy. */
1561 rv = zfs_mount(devformat(&dev->dd), NULL, (void **)&mount);
1564 dev->dd.d_opendata = mount;
1570 zfs_dev_close(struct open_file *f)
1572 struct devdesc *dev;
1573 struct zfsmount *mnt, *mount;
1576 mnt = dev->d_opendata;
1578 STAILQ_FOREACH(mount, &zfsmount, next) {
1579 if (mnt->spa->spa_guid == mount->spa->spa_guid)
1588 zfs_dev_strategy(void *devdata, int rw, daddr_t dblk, size_t size, char *buf, size_t *rsize)
1594 struct devsw zfs_dev = {
1596 .dv_type = DEVT_ZFS,
1597 .dv_init = zfs_dev_init,
1598 .dv_strategy = zfs_dev_strategy,
1599 .dv_open = zfs_dev_open,
1600 .dv_close = zfs_dev_close,
1601 .dv_ioctl = noioctl,
1602 .dv_print = zfs_dev_print,
1603 .dv_cleanup = nullsys,
1604 .dv_fmtdev = zfs_fmtdev,
1605 .dv_parsedev = zfs_parsedev,
1609 zfs_parsedev(struct devdesc **idev, const char *devspec, const char **path)
1611 static char rootname[ZFS_MAXNAMELEN];
1612 static char poolname[ZFS_MAXNAMELEN];
1618 struct zfs_devdesc *dev;
1620 np = devspec + 3; /* Skip the leading 'zfs' */
1624 end = strrchr(np, ':');
1627 sep = strchr(np, '/');
1628 if (sep == NULL || sep >= end)
1630 memcpy(poolname, np, sep - np);
1631 poolname[sep - np] = '\0';
1634 memcpy(rootname, sep, end - sep);
1635 rootname[end - sep] = '\0';
1640 spa = spa_find_by_name(poolname);
1643 dev = malloc(sizeof(*dev));
1646 dev->pool_guid = spa->spa_guid;
1647 rv = zfs_lookup_dataset(spa, rootname, &dev->root_guid);
1653 *path = (*end == '\0') ? end : end + 1;
1654 dev->dd.d_dev = &zfs_dev;
1660 zfs_fmtdev(struct devdesc *vdev)
1662 static char rootname[ZFS_MAXNAMELEN];
1663 static char buf[2 * ZFS_MAXNAMELEN + 8];
1664 struct zfs_devdesc *dev = (struct zfs_devdesc *)vdev;
1668 if (vdev->d_dev->dv_type != DEVT_ZFS)
1671 /* Do we have any pools? */
1672 spa = STAILQ_FIRST(&zfs_pools);
1676 if (dev->pool_guid == 0)
1677 dev->pool_guid = spa->spa_guid;
1679 spa = spa_find_by_guid(dev->pool_guid);
1682 printf("ZFS: can't find pool by guid\n");
1685 if (dev->root_guid == 0 && zfs_get_root(spa, &dev->root_guid)) {
1686 printf("ZFS: can't find root filesystem\n");
1689 if (zfs_rlookup(spa, dev->root_guid, rootname)) {
1690 printf("ZFS: can't find filesystem by guid\n");
1694 if (rootname[0] == '\0')
1695 snprintf(buf, sizeof(buf), "%s:%s:", dev->dd.d_dev->dv_name,
1698 snprintf(buf, sizeof(buf), "%s:%s/%s:", dev->dd.d_dev->dv_name,
1699 spa->spa_name, rootname);
1704 split_devname(const char *name, char *poolname, size_t size,
1705 const char **dsnamep)
1710 ASSERT(name != NULL);
1711 ASSERT(poolname != NULL);
1714 dsname = strchr(name, '/');
1715 if (dsname != NULL) {
1716 len = dsname - name;
1724 strlcpy(poolname, name, len + 1);
1726 if (dsnamep != NULL)
1733 zfs_list(const char *name)
1735 static char poolname[ZFS_MAXNAMELEN];
1741 if (split_devname(name, poolname, sizeof(poolname), &dsname) != 0)
1744 spa = spa_find_by_name(poolname);
1747 rv = zfs_lookup_dataset(spa, dsname, &objid);
1751 return (zfs_list_dataset(spa, objid));
1755 init_zfs_boot_options(const char *currdev_in)
1757 char poolname[ZFS_MAXNAMELEN];
1758 char *beroot, *currdev;
1764 currdev_len = strlen(currdev_in);
1765 if (currdev_len == 0)
1767 if (strncmp(currdev_in, "zfs:", 4) != 0)
1769 currdev = strdup(currdev_in);
1770 if (currdev == NULL)
1772 /* Remove the trailing : */
1773 currdev[currdev_len - 1] = '\0';
1775 setenv("zfs_be_active", currdev, 1);
1776 setenv("zfs_be_currpage", "1", 1);
1777 /* Remove the last element (current bootenv) */
1778 beroot = strrchr(currdev, '/');
1781 beroot = strchr(currdev, ':') + 1;
1782 setenv("zfs_be_root", beroot, 1);
1784 if (split_devname(beroot, poolname, sizeof(poolname), &dsname) != 0)
1787 spa = spa_find_by_name(poolname);
1791 zfs_bootenv_initial("bootenvs", spa, beroot, dsname, 0);
1792 zfs_checkpoints_initial(spa, beroot, dsname);
1798 zfs_checkpoints_initial(spa_t *spa, const char *name, const char *dsname)
1802 if (spa->spa_uberblock_checkpoint.ub_checkpoint_txg != 0) {
1803 snprintf(envname, sizeof(envname), "zpool_checkpoint");
1804 setenv(envname, name, 1);
1806 spa->spa_uberblock = &spa->spa_uberblock_checkpoint;
1807 spa->spa_mos = &spa->spa_mos_checkpoint;
1809 zfs_bootenv_initial("bootenvs_check", spa, name, dsname, 1);
1811 spa->spa_uberblock = &spa->spa_uberblock_master;
1812 spa->spa_mos = &spa->spa_mos_master;
1817 zfs_bootenv_initial(const char *envprefix, spa_t *spa, const char *rootname,
1818 const char *dsname, int checkpoint)
1820 char envname[32], envval[256];
1822 int bootenvs_idx, rv;
1824 SLIST_INIT(&zfs_be_head);
1827 rv = zfs_lookup_dataset(spa, dsname, &objid);
1831 rv = zfs_callback_dataset(spa, objid, zfs_belist_add);
1833 /* Populate the initial environment variables */
1834 SLIST_FOREACH_SAFE(zfs_be, &zfs_be_head, entries, zfs_be_tmp) {
1835 /* Enumerate all bootenvs for general usage */
1836 snprintf(envname, sizeof(envname), "%s[%d]",
1837 envprefix, bootenvs_idx);
1838 snprintf(envval, sizeof(envval), "zfs:%s%s/%s",
1839 checkpoint ? "!" : "", rootname, zfs_be->name);
1840 rv = setenv(envname, envval, 1);
1845 snprintf(envname, sizeof(envname), "%s_count", envprefix);
1846 snprintf(envval, sizeof(envval), "%d", bootenvs_idx);
1847 setenv(envname, envval, 1);
1849 /* Clean up the SLIST of ZFS BEs */
1850 while (!SLIST_EMPTY(&zfs_be_head)) {
1851 zfs_be = SLIST_FIRST(&zfs_be_head);
1852 SLIST_REMOVE_HEAD(&zfs_be_head, entries);
1859 zfs_bootenv(const char *name)
1861 char poolname[ZFS_MAXNAMELEN], *root;
1866 int rv, pages, perpage, currpage;
1870 if ((root = getenv("zfs_be_root")) == NULL)
1873 if (strcmp(name, root) != 0) {
1874 if (setenv("zfs_be_root", name, 1) != 0)
1878 SLIST_INIT(&zfs_be_head);
1881 if (split_devname(name, poolname, sizeof(poolname), &dsname) != 0)
1884 spa = spa_find_by_name(poolname);
1887 rv = zfs_lookup_dataset(spa, dsname, &objid);
1890 rv = zfs_callback_dataset(spa, objid, zfs_belist_add);
1892 /* Calculate and store the number of pages of BEs */
1893 perpage = (ZFS_BE_LAST - ZFS_BE_FIRST + 1);
1894 pages = (zfs_env_count / perpage) + ((zfs_env_count % perpage) > 0 ? 1 : 0);
1895 snprintf(becount, 4, "%d", pages);
1896 if (setenv("zfs_be_pages", becount, 1) != 0)
1899 /* Roll over the page counter if it has exceeded the maximum */
1900 currpage = strtol(getenv("zfs_be_currpage"), NULL, 10);
1901 if (currpage > pages) {
1902 if (setenv("zfs_be_currpage", "1", 1) != 0)
1906 /* Populate the menu environment variables */
1909 /* Clean up the SLIST of ZFS BEs */
1910 while (!SLIST_EMPTY(&zfs_be_head)) {
1911 zfs_be = SLIST_FIRST(&zfs_be_head);
1912 SLIST_REMOVE_HEAD(&zfs_be_head, entries);
1921 zfs_belist_add(const char *name, uint64_t value __unused)
1924 /* Skip special datasets that start with a $ character */
1925 if (strncmp(name, "$", 1) == 0) {
1928 /* Add the boot environment to the head of the SLIST */
1929 zfs_be = malloc(sizeof(struct zfs_be_entry));
1930 if (zfs_be == NULL) {
1933 zfs_be->name = strdup(name);
1934 if (zfs_be->name == NULL) {
1938 SLIST_INSERT_HEAD(&zfs_be_head, zfs_be, entries);
1947 char envname[32], envval[256];
1948 char *beroot, *pagenum;
1951 beroot = getenv("zfs_be_root");
1952 if (beroot == NULL) {
1956 pagenum = getenv("zfs_be_currpage");
1957 if (pagenum != NULL) {
1958 page = strtol(pagenum, NULL, 10);
1965 zfs_env_index = ZFS_BE_FIRST;
1966 SLIST_FOREACH_SAFE(zfs_be, &zfs_be_head, entries, zfs_be_tmp) {
1967 /* Skip to the requested page number */
1968 if (ctr <= ((ZFS_BE_LAST - ZFS_BE_FIRST + 1) * (page - 1))) {
1973 snprintf(envname, sizeof(envname), "bootenvmenu_caption[%d]", zfs_env_index);
1974 snprintf(envval, sizeof(envval), "%s", zfs_be->name);
1975 rv = setenv(envname, envval, 1);
1980 snprintf(envname, sizeof(envname), "bootenvansi_caption[%d]", zfs_env_index);
1981 rv = setenv(envname, envval, 1);
1986 snprintf(envname, sizeof(envname), "bootenvmenu_command[%d]", zfs_env_index);
1987 rv = setenv(envname, "set_bootenv", 1);
1992 snprintf(envname, sizeof(envname), "bootenv_root[%d]", zfs_env_index);
1993 snprintf(envval, sizeof(envval), "zfs:%s/%s", beroot, zfs_be->name);
1994 rv = setenv(envname, envval, 1);
2000 if (zfs_env_index > ZFS_BE_LAST) {
2006 for (; zfs_env_index <= ZFS_BE_LAST; zfs_env_index++) {
2007 snprintf(envname, sizeof(envname), "bootenvmenu_caption[%d]", zfs_env_index);
2008 (void)unsetenv(envname);
2009 snprintf(envname, sizeof(envname), "bootenvansi_caption[%d]", zfs_env_index);
2010 (void)unsetenv(envname);
2011 snprintf(envname, sizeof(envname), "bootenvmenu_command[%d]", zfs_env_index);
2012 (void)unsetenv(envname);
2013 snprintf(envname, sizeof(envname), "bootenv_root[%d]", zfs_env_index);
2014 (void)unsetenv(envname);