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);
61 static int zfs_mount(const char *dev, const char *path, void **data);
62 static int zfs_unmount(const char *dev, void *data);
64 static void zfs_bootenv_initial(const char *envname, spa_t *spa,
65 const char *name, const char *dsname, int checkpoint);
66 static void zfs_checkpoints_initial(spa_t *spa, const char *name,
69 static int zfs_parsedev(struct devdesc **idev, const char *devspec,
74 struct fs_ops zfs_fsops = {
77 .fo_close = zfs_close,
79 .fo_write = null_write,
82 .fo_readdir = zfs_readdir,
83 .fo_mount = zfs_mount,
84 .fo_unmount = zfs_unmount
91 off_t f_seekp; /* seek pointer */
93 uint64_t f_zap_type; /* zap type for readdir */
94 uint64_t f_num_leafs; /* number of fzap leaf blocks */
95 zap_leaf_phys_t *f_zap_leaf; /* zap leaf buffer */
98 static int zfs_env_index;
99 static int zfs_env_count;
101 SLIST_HEAD(zfs_be_list, zfs_be_entry) zfs_be_head = SLIST_HEAD_INITIALIZER(zfs_be_head);
102 struct zfs_be_list *zfs_be_headp;
103 struct zfs_be_entry {
105 SLIST_ENTRY(zfs_be_entry) entries;
106 } *zfs_be, *zfs_be_tmp;
112 zfs_open(const char *upath, struct open_file *f)
114 struct devdesc *dev = f->f_devdata;
115 struct zfsmount *mount = dev->d_opendata;
119 if (f->f_dev != &zfs_dev)
122 /* allocate file system specific data structure */
123 fp = calloc(1, sizeof(struct file));
128 rc = zfs_lookup(mount, upath, &fp->f_dnode);
138 zfs_close(struct open_file *f)
140 struct file *fp = (struct file *)f->f_fsdata;
142 dnode_cache_obj = NULL;
150 * Copy a portion of a file into kernel memory.
151 * Cross block boundaries when necessary.
154 zfs_read(struct open_file *f, void *start, size_t size, size_t *resid /* out */)
156 struct devdesc *dev = f->f_devdata;
157 const spa_t *spa = ((struct zfsmount *)dev->d_opendata)->spa;
158 struct file *fp = (struct file *)f->f_fsdata;
163 rc = zfs_stat(f, &sb);
167 if (fp->f_seekp + n > sb.st_size)
168 n = sb.st_size - fp->f_seekp;
170 rc = dnode_read(spa, &fp->f_dnode, fp->f_seekp, start, n);
176 for (i = 0; i < n; i++)
177 putchar(((char*) start)[i]);
187 zfs_seek(struct open_file *f, off_t offset, int where)
189 struct file *fp = (struct file *)f->f_fsdata;
193 fp->f_seekp = offset;
196 fp->f_seekp += offset;
203 error = zfs_stat(f, &sb);
208 fp->f_seekp = sb.st_size - offset;
215 return (fp->f_seekp);
219 zfs_stat(struct open_file *f, struct stat *sb)
221 struct devdesc *dev = f->f_devdata;
222 const spa_t *spa = ((struct zfsmount *)dev->d_opendata)->spa;
223 struct file *fp = (struct file *)f->f_fsdata;
225 return (zfs_dnode_stat(spa, &fp->f_dnode, sb));
229 zfs_readdir(struct open_file *f, struct dirent *d)
231 struct devdesc *dev = f->f_devdata;
232 const spa_t *spa = ((struct zfsmount *)dev->d_opendata)->spa;
233 struct file *fp = (struct file *)f->f_fsdata;
236 size_t bsize = fp->f_dnode.dn_datablkszsec << SPA_MINBLOCKSHIFT;
239 rc = zfs_stat(f, &sb);
242 if (!S_ISDIR(sb.st_mode))
246 * If this is the first read, get the zap type.
248 if (fp->f_seekp == 0) {
249 rc = dnode_read(spa, &fp->f_dnode,
250 0, &fp->f_zap_type, sizeof(fp->f_zap_type));
254 if (fp->f_zap_type == ZBT_MICRO) {
255 fp->f_seekp = offsetof(mzap_phys_t, mz_chunk);
257 rc = dnode_read(spa, &fp->f_dnode,
258 offsetof(zap_phys_t, zap_num_leafs),
260 sizeof(fp->f_num_leafs));
265 fp->f_zap_leaf = malloc(bsize);
266 if (fp->f_zap_leaf == NULL)
268 rc = dnode_read(spa, &fp->f_dnode,
277 if (fp->f_zap_type == ZBT_MICRO) {
279 if (fp->f_seekp >= bsize)
282 rc = dnode_read(spa, &fp->f_dnode,
283 fp->f_seekp, &mze, sizeof(mze));
286 fp->f_seekp += sizeof(mze);
288 if (!mze.mze_name[0])
291 d->d_fileno = ZFS_DIRENT_OBJ(mze.mze_value);
292 d->d_type = ZFS_DIRENT_TYPE(mze.mze_value);
293 strcpy(d->d_name, mze.mze_name);
294 d->d_namlen = strlen(d->d_name);
298 zap_leaf_chunk_t *zc, *nc;
305 * Initialise this so we can use the ZAP size
306 * calculating macros.
308 zl.l_bs = ilog2(bsize);
309 zl.l_phys = fp->f_zap_leaf;
312 * Figure out which chunk we are currently looking at
313 * and consider seeking to the next leaf. We use the
314 * low bits of f_seekp as a simple chunk index.
317 chunk = fp->f_seekp & (bsize - 1);
318 if (chunk == ZAP_LEAF_NUMCHUNKS(&zl)) {
319 fp->f_seekp = rounddown2(fp->f_seekp, bsize) + bsize;
323 * Check for EOF and read the new leaf.
325 if (fp->f_seekp >= bsize * fp->f_num_leafs)
328 rc = dnode_read(spa, &fp->f_dnode,
336 zc = &ZAP_LEAF_CHUNK(&zl, chunk);
338 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
341 namelen = zc->l_entry.le_name_numints;
342 if (namelen > sizeof(d->d_name))
343 namelen = sizeof(d->d_name);
346 * Paste the name back together.
348 nc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_name_chunk);
350 while (namelen > 0) {
353 if (len > ZAP_LEAF_ARRAY_BYTES)
354 len = ZAP_LEAF_ARRAY_BYTES;
355 memcpy(p, nc->l_array.la_array, len);
358 nc = &ZAP_LEAF_CHUNK(&zl, nc->l_array.la_next);
360 d->d_name[sizeof(d->d_name) - 1] = 0;
363 * Assume the first eight bytes of the value are
366 value = fzap_leaf_value(&zl, zc);
368 d->d_fileno = ZFS_DIRENT_OBJ(value);
369 d->d_type = ZFS_DIRENT_TYPE(value);
370 d->d_namlen = strlen(d->d_name);
377 * if path is NULL, create mount structure, but do not add it to list.
380 zfs_mount(const char *dev, const char *path, void **data)
382 struct zfs_devdesc *zfsdev = NULL;
384 struct zfsmount *mnt = NULL;
388 rv = zfs_parsedev((struct devdesc **)&zfsdev, dev, NULL);
393 spa = spa_find_by_dev(zfsdev);
399 mnt = calloc(1, sizeof(*mnt));
405 if (mnt->path != NULL) {
406 mnt->path = strdup(path);
407 if (mnt->path == NULL) {
413 rv = zfs_mount_impl(spa, zfsdev->root_guid, mnt);
415 if (rv == 0 && mnt->objset.os_type != DMU_OST_ZFS) {
416 printf("Unexpected object set type %ju\n",
417 (uintmax_t)mnt->objset.os_type);
431 STAILQ_INSERT_TAIL(&zfsmount, mnt, next);
439 zfs_unmount(const char *dev, void *data)
441 struct zfsmount *mnt = data;
443 STAILQ_REMOVE(&zfsmount, mnt, zfsmount, next);
450 vdev_read(vdev_t *vdev, void *priv, off_t offset, void *buf, size_t bytes)
453 size_t res, head, tail, total_size, full_sec_size;
454 unsigned secsz, do_tail_read;
456 char *outbuf, *bouncebuf;
458 fd = (uintptr_t) priv;
459 outbuf = (char *) buf;
462 ret = ioctl(fd, DIOCGSECTORSIZE, &secsz);
467 * Handling reads of arbitrary offset and size - multi-sector case
468 * and single-sector case.
471 * (do_tail_read = true if tail > 0)
473 * |<----------------------total_size--------------------->|
475 * |<--head-->|<--------------bytes------------>|<--tail-->|
477 * | | |<~full_sec_size~>| | |
478 * +------------------+ +------------------+
479 * | |0101010| . . . |0101011| |
480 * +------------------+ +------------------+
481 * start_sec start_sec + n
485 * (do_tail_read = false)
487 * |<------total_size = secsz----->|
489 * |<-head->|<---bytes--->|<-tail->|
490 * +-------------------------------+
491 * | |0101010101010| |
492 * +-------------------------------+
495 start_sec = offset / secsz;
496 head = offset % secsz;
497 total_size = roundup2(head + bytes, secsz);
498 tail = total_size - (head + bytes);
499 do_tail_read = ((tail > 0) && (head + bytes > secsz));
500 full_sec_size = total_size;
502 full_sec_size -= secsz;
504 full_sec_size -= secsz;
506 /* Return of partial sector data requires a bounce buffer. */
507 if ((head > 0) || do_tail_read || bytes < secsz) {
508 bouncebuf = malloc(secsz);
509 if (bouncebuf == NULL) {
510 printf("vdev_read: out of memory\n");
515 if (lseek(fd, start_sec * secsz, SEEK_SET) == -1) {
520 /* Partial data return from first sector */
522 res = read(fd, bouncebuf, secsz);
527 memcpy(outbuf, bouncebuf + head, min(secsz - head, bytes));
528 outbuf += min(secsz - head, bytes);
532 * Full data return from read sectors.
533 * Note, there is still corner case where we read
534 * from sector boundary, but less than sector size, e.g. reading 512B
537 if (full_sec_size > 0) {
538 if (bytes < full_sec_size) {
539 res = read(fd, bouncebuf, secsz);
544 memcpy(outbuf, bouncebuf, bytes);
546 res = read(fd, outbuf, full_sec_size);
547 if (res != full_sec_size) {
551 outbuf += full_sec_size;
555 /* Partial data return from last sector */
557 res = read(fd, bouncebuf, secsz);
562 memcpy(outbuf, bouncebuf, secsz - tail);
572 vdev_write(vdev_t *vdev, off_t offset, void *buf, size_t bytes)
575 size_t head, tail, total_size, full_sec_size;
576 unsigned secsz, do_tail_write;
579 char *outbuf, *bouncebuf;
581 fd = (uintptr_t)vdev->v_priv;
582 outbuf = (char *)buf;
585 ret = ioctl(fd, DIOCGSECTORSIZE, &secsz);
589 start_sec = offset / secsz;
590 head = offset % secsz;
591 total_size = roundup2(head + bytes, secsz);
592 tail = total_size - (head + bytes);
593 do_tail_write = ((tail > 0) && (head + bytes > secsz));
594 full_sec_size = total_size;
596 full_sec_size -= secsz;
598 full_sec_size -= secsz;
600 /* Partial sector write requires a bounce buffer. */
601 if ((head > 0) || do_tail_write || bytes < secsz) {
602 bouncebuf = malloc(secsz);
603 if (bouncebuf == NULL) {
604 printf("vdev_write: out of memory\n");
609 if (lseek(fd, start_sec * secsz, SEEK_SET) == -1) {
614 /* Partial data for first sector */
616 res = read(fd, bouncebuf, secsz);
617 if ((unsigned)res != secsz) {
621 memcpy(bouncebuf + head, outbuf, min(secsz - head, bytes));
622 (void) lseek(fd, -secsz, SEEK_CUR);
623 res = write(fd, bouncebuf, secsz);
624 if ((unsigned)res != secsz) {
628 outbuf += min(secsz - head, bytes);
632 * Full data write to sectors.
633 * Note, there is still corner case where we write
634 * to sector boundary, but less than sector size, e.g. write 512B
637 if (full_sec_size > 0) {
638 if (bytes < full_sec_size) {
639 res = read(fd, bouncebuf, secsz);
640 if ((unsigned)res != secsz) {
644 memcpy(bouncebuf, outbuf, bytes);
645 (void) lseek(fd, -secsz, SEEK_CUR);
646 res = write(fd, bouncebuf, secsz);
647 if ((unsigned)res != secsz) {
652 res = write(fd, outbuf, full_sec_size);
653 if ((unsigned)res != full_sec_size) {
657 outbuf += full_sec_size;
661 /* Partial data write to last sector */
663 res = read(fd, bouncebuf, secsz);
664 if ((unsigned)res != secsz) {
668 memcpy(bouncebuf, outbuf, secsz - tail);
669 (void) lseek(fd, -secsz, SEEK_CUR);
670 res = write(fd, bouncebuf, secsz);
671 if ((unsigned)res != secsz) {
691 if (archsw.arch_zfs_probe == NULL)
693 archsw.arch_zfs_probe();
696 spa = STAILQ_FIRST(&zfs_pools);
697 while (spa != NULL) {
698 next = STAILQ_NEXT(spa, spa_link);
699 if (zfs_spa_init(spa)) {
701 STAILQ_REMOVE_HEAD(&zfs_pools, spa_link);
703 STAILQ_REMOVE_AFTER(&zfs_pools, prev, spa_link);
711 struct zfs_probe_args {
719 zfs_diskread(void *arg, void *buf, size_t blocks, uint64_t offset)
721 struct zfs_probe_args *ppa;
723 ppa = (struct zfs_probe_args *)arg;
724 return (vdev_read(NULL, (void *)(uintptr_t)ppa->fd,
725 offset * ppa->secsz, buf, blocks * ppa->secsz));
729 zfs_probe(int fd, uint64_t *pool_guid)
735 ret = vdev_probe(vdev_read, vdev_write, (void *)(uintptr_t)fd, &spa);
736 if (ret == 0 && pool_guid != NULL)
738 *pool_guid = spa->spa_guid;
743 zfs_probe_partition(void *arg, const char *partname,
744 const struct ptable_entry *part)
746 struct zfs_probe_args *ppa, pa;
747 struct ptable *table;
751 /* Probe only freebsd-zfs and freebsd partitions */
752 if (part->type != PART_FREEBSD &&
753 part->type != PART_FREEBSD_ZFS)
756 ppa = (struct zfs_probe_args *)arg;
757 strncpy(devname, ppa->devname, strlen(ppa->devname) - 1);
758 devname[strlen(ppa->devname) - 1] = '\0';
759 snprintf(devname, sizeof(devname), "%s%s:", devname, partname);
760 pa.fd = open(devname, O_RDWR);
763 ret = zfs_probe(pa.fd, ppa->pool_guid);
766 /* Do we have BSD label here? */
767 if (part->type == PART_FREEBSD) {
768 pa.devname = devname;
769 pa.pool_guid = ppa->pool_guid;
770 pa.secsz = ppa->secsz;
771 table = ptable_open(&pa, part->end - part->start + 1,
772 ppa->secsz, zfs_diskread);
774 ptable_iterate(table, &pa, zfs_probe_partition);
783 * Return bootenv nvlist from pool label.
786 zfs_get_bootenv(void *vdev, nvlist_t **benvp)
788 struct zfs_devdesc *dev = (struct zfs_devdesc *)vdev;
789 nvlist_t *benv = NULL;
793 if (dev->dd.d_dev->dv_type != DEVT_ZFS)
796 if ((spa = spa_find_by_dev(dev)) == NULL)
799 if (spa->spa_bootenv == NULL) {
800 STAILQ_FOREACH(vd, &spa->spa_root_vdev->v_children,
802 benv = vdev_read_bootenv(vd);
807 spa->spa_bootenv = benv;
809 benv = spa->spa_bootenv;
820 * Store nvlist to pool label bootenv area. Also updates cached pointer in spa.
823 zfs_set_bootenv(void *vdev, nvlist_t *benv)
825 struct zfs_devdesc *dev = (struct zfs_devdesc *)vdev;
829 if (dev->dd.d_dev->dv_type != DEVT_ZFS)
832 if ((spa = spa_find_by_dev(dev)) == NULL)
835 STAILQ_FOREACH(vd, &spa->spa_root_vdev->v_children, v_childlink) {
836 vdev_write_bootenv(vd, benv);
839 spa->spa_bootenv = benv;
844 * Get bootonce value by key. The bootonce <key, value> pair is removed
845 * from the bootenv nvlist and the remaining nvlist is committed back to disk.
848 zfs_get_bootonce(void *vdev, const char *key, char *buf, size_t size)
854 if ((rv = zfs_get_bootenv(vdev, &benv)) != 0)
857 if ((rv = nvlist_find(benv, key, DATA_TYPE_STRING, NULL,
858 &result, &result_size)) == 0) {
859 if (result_size == 0) {
860 /* ignore empty string */
863 size = MIN((size_t)result_size + 1, size);
864 strlcpy(buf, result, size);
866 (void) nvlist_remove(benv, key, DATA_TYPE_STRING);
867 (void) zfs_set_bootenv(vdev, benv);
877 static int zfs_nvstore_setter(void *, int, const char *,
878 const void *, size_t);
879 static int zfs_nvstore_setter_str(void *, const char *, const char *,
881 static int zfs_nvstore_unset_impl(void *, const char *, bool);
882 static int zfs_nvstore_setenv(void *, void *);
885 * nvstore is only present for current rootfs pool.
888 zfs_nvstore_sethook(struct env_var *ev, int flags __unused, const void *value)
890 struct zfs_devdesc *dev;
893 archsw.arch_getdev((void **)&dev, NULL, NULL);
897 rv = zfs_nvstore_setter_str(dev, NULL, ev->ev_name, value);
904 * nvstore is only present for current rootfs pool.
907 zfs_nvstore_unsethook(struct env_var *ev)
909 struct zfs_devdesc *dev;
912 archsw.arch_getdev((void **)&dev, NULL, NULL);
916 rv = zfs_nvstore_unset_impl(dev, ev->ev_name, false);
923 zfs_nvstore_getter(void *vdev, const char *name, void **data)
925 struct zfs_devdesc *dev = (struct zfs_devdesc *)vdev;
932 if (dev->dd.d_dev->dv_type != DEVT_ZFS)
935 if ((spa = spa_find_by_dev(dev)) == NULL)
938 if (spa->spa_bootenv == NULL)
941 if (nvlist_find(spa->spa_bootenv, OS_NVSTORE, DATA_TYPE_NVLIST,
942 NULL, &nv, NULL) != 0)
945 rv = nvlist_find(nv, name, DATA_TYPE_STRING, NULL, &str, &size);
948 asprintf(ptr, "%.*s", size, str);
957 zfs_nvstore_setter(void *vdev, int type, const char *name,
958 const void *data, size_t size)
960 struct zfs_devdesc *dev = (struct zfs_devdesc *)vdev;
966 if (dev->dd.d_dev->dv_type != DEVT_ZFS)
969 if ((spa = spa_find_by_dev(dev)) == NULL)
972 if (spa->spa_bootenv == NULL)
975 if (nvlist_find(spa->spa_bootenv, OS_NVSTORE, DATA_TYPE_NVLIST,
976 NULL, &nv, NULL) != 0) {
977 nv = nvlist_create(NV_UNIQUE_NAME);
985 if (size != sizeof (int8_t)) {
989 rv = nvlist_add_int8(nv, name, *(int8_t *)data);
992 case DATA_TYPE_INT16:
993 if (size != sizeof (int16_t)) {
997 rv = nvlist_add_int16(nv, name, *(int16_t *)data);
1000 case DATA_TYPE_INT32:
1001 if (size != sizeof (int32_t)) {
1005 rv = nvlist_add_int32(nv, name, *(int32_t *)data);
1008 case DATA_TYPE_INT64:
1009 if (size != sizeof (int64_t)) {
1013 rv = nvlist_add_int64(nv, name, *(int64_t *)data);
1016 case DATA_TYPE_BYTE:
1017 if (size != sizeof (uint8_t)) {
1021 rv = nvlist_add_byte(nv, name, *(int8_t *)data);
1024 case DATA_TYPE_UINT8:
1025 if (size != sizeof (uint8_t)) {
1029 rv = nvlist_add_uint8(nv, name, *(int8_t *)data);
1032 case DATA_TYPE_UINT16:
1033 if (size != sizeof (uint16_t)) {
1037 rv = nvlist_add_uint16(nv, name, *(uint16_t *)data);
1040 case DATA_TYPE_UINT32:
1041 if (size != sizeof (uint32_t)) {
1045 rv = nvlist_add_uint32(nv, name, *(uint32_t *)data);
1048 case DATA_TYPE_UINT64:
1049 if (size != sizeof (uint64_t)) {
1053 rv = nvlist_add_uint64(nv, name, *(uint64_t *)data);
1056 case DATA_TYPE_STRING:
1057 rv = nvlist_add_string(nv, name, data);
1060 case DATA_TYPE_BOOLEAN_VALUE:
1061 if (size != sizeof (boolean_t)) {
1065 rv = nvlist_add_boolean_value(nv, name, *(boolean_t *)data);
1074 rv = nvlist_add_nvlist(spa->spa_bootenv, OS_NVSTORE, nv);
1076 rv = zfs_set_bootenv(vdev, spa->spa_bootenv);
1080 rv = zfs_nvstore_setenv(vdev,
1081 nvpair_find(nv, name));
1083 env_discard(env_getenv(name));
1094 get_int64(const char *data, int64_t *ip)
1100 val = strtoll(data, &end, 0);
1101 if (errno != 0 || *data == '\0' || *end != '\0')
1109 get_uint64(const char *data, uint64_t *ip)
1115 val = strtoull(data, &end, 0);
1116 if (errno != 0 || *data == '\0' || *end != '\0')
1124 * Translate textual data to data type. If type is not set, and we are
1125 * creating new pair, use DATA_TYPE_STRING.
1128 zfs_nvstore_setter_str(void *vdev, const char *type, const char *name,
1131 struct zfs_devdesc *dev = (struct zfs_devdesc *)vdev;
1139 if (dev->dd.d_dev->dv_type != DEVT_ZFS)
1142 if ((spa = spa_find_by_dev(dev)) == NULL)
1145 if (spa->spa_bootenv == NULL)
1148 if (nvlist_find(spa->spa_bootenv, OS_NVSTORE, DATA_TYPE_NVLIST,
1149 NULL, &nv, NULL) != 0) {
1157 * if there is no existing pair, default to string.
1158 * Otherwise, use type from existing pair.
1160 nvh = nvpair_find(nv, name);
1162 dt = DATA_TYPE_STRING;
1164 nv_string_t *nvp_name;
1165 nv_pair_data_t *nvp_data;
1167 nvp_name = (nv_string_t *)(nvh + 1);
1168 nvp_data = (nv_pair_data_t *)(&nvp_name->nv_data[0] +
1169 NV_ALIGN4(nvp_name->nv_size));
1170 dt = nvp_data->nv_type;
1173 dt = nvpair_type_from_name(type);
1179 case DATA_TYPE_INT8:
1180 rv = get_int64(data, &val);
1184 rv = zfs_nvstore_setter(vdev, dt, name, &v, sizeof (v));
1187 case DATA_TYPE_INT16:
1188 rv = get_int64(data, &val);
1192 rv = zfs_nvstore_setter(vdev, dt, name, &v, sizeof (v));
1195 case DATA_TYPE_INT32:
1196 rv = get_int64(data, &val);
1200 rv = zfs_nvstore_setter(vdev, dt, name, &v, sizeof (v));
1203 case DATA_TYPE_INT64:
1204 rv = get_int64(data, &val);
1206 rv = zfs_nvstore_setter(vdev, dt, name, &val,
1211 case DATA_TYPE_BYTE:
1212 rv = get_uint64(data, &uval);
1216 rv = zfs_nvstore_setter(vdev, dt, name, &v, sizeof (v));
1220 case DATA_TYPE_UINT8:
1221 rv = get_uint64(data, &uval);
1225 rv = zfs_nvstore_setter(vdev, dt, name, &v, sizeof (v));
1229 case DATA_TYPE_UINT16:
1230 rv = get_uint64(data, &uval);
1234 rv = zfs_nvstore_setter(vdev, dt, name, &v, sizeof (v));
1238 case DATA_TYPE_UINT32:
1239 rv = get_uint64(data, &uval);
1243 rv = zfs_nvstore_setter(vdev, dt, name, &v, sizeof (v));
1247 case DATA_TYPE_UINT64:
1248 rv = get_uint64(data, &uval);
1250 rv = zfs_nvstore_setter(vdev, dt, name, &uval,
1255 case DATA_TYPE_STRING:
1256 rv = zfs_nvstore_setter(vdev, dt, name, data, strlen(data) + 1);
1259 case DATA_TYPE_BOOLEAN_VALUE:
1260 rv = get_int64(data, &val);
1264 rv = zfs_nvstore_setter(vdev, dt, name, &v, sizeof (v));
1274 zfs_nvstore_unset_impl(void *vdev, const char *name, bool unset_env)
1276 struct zfs_devdesc *dev = (struct zfs_devdesc *)vdev;
1281 if (dev->dd.d_dev->dv_type != DEVT_ZFS)
1284 if ((spa = spa_find_by_dev(dev)) == NULL)
1287 if (spa->spa_bootenv == NULL)
1290 if (nvlist_find(spa->spa_bootenv, OS_NVSTORE, DATA_TYPE_NVLIST,
1291 NULL, &nv, NULL) != 0)
1294 rv = nvlist_remove(nv, name, DATA_TYPE_UNKNOWN);
1296 if (nvlist_next_nvpair(nv, NULL) == NULL) {
1297 rv = nvlist_remove(spa->spa_bootenv, OS_NVSTORE,
1300 rv = nvlist_add_nvlist(spa->spa_bootenv,
1304 rv = zfs_set_bootenv(vdev, spa->spa_bootenv);
1308 env_discard(env_getenv(name));
1313 zfs_nvstore_unset(void *vdev, const char *name)
1315 return (zfs_nvstore_unset_impl(vdev, name, true));
1319 zfs_nvstore_print(void *vdev __unused, void *ptr)
1322 nvpair_print(ptr, 0);
1327 * Create environment variable from nvpair.
1328 * set hook will update nvstore with new value, unset hook will remove
1329 * variable from nvstore.
1332 zfs_nvstore_setenv(void *vdev __unused, void *ptr)
1334 nvp_header_t *nvh = ptr;
1335 nv_string_t *nvp_name, *nvp_value;
1336 nv_pair_data_t *nvp_data;
1343 nvp_name = (nv_string_t *)(nvh + 1);
1344 nvp_data = (nv_pair_data_t *)(&nvp_name->nv_data[0] +
1345 NV_ALIGN4(nvp_name->nv_size));
1347 if ((name = nvstring_get(nvp_name)) == NULL)
1351 switch (nvp_data->nv_type) {
1352 case DATA_TYPE_BYTE:
1353 case DATA_TYPE_UINT8:
1354 (void) asprintf(&value, "%uc",
1355 *(unsigned *)&nvp_data->nv_data[0]);
1360 case DATA_TYPE_INT8:
1361 (void) asprintf(&value, "%c", *(int *)&nvp_data->nv_data[0]);
1366 case DATA_TYPE_INT16:
1367 (void) asprintf(&value, "%hd", *(short *)&nvp_data->nv_data[0]);
1372 case DATA_TYPE_UINT16:
1373 (void) asprintf(&value, "%hu",
1374 *(unsigned short *)&nvp_data->nv_data[0]);
1379 case DATA_TYPE_BOOLEAN_VALUE:
1380 case DATA_TYPE_INT32:
1381 (void) asprintf(&value, "%d", *(int *)&nvp_data->nv_data[0]);
1386 case DATA_TYPE_UINT32:
1387 (void) asprintf(&value, "%u",
1388 *(unsigned *)&nvp_data->nv_data[0]);
1393 case DATA_TYPE_INT64:
1394 (void) asprintf(&value, "%jd",
1395 (intmax_t)*(int64_t *)&nvp_data->nv_data[0]);
1400 case DATA_TYPE_UINT64:
1401 (void) asprintf(&value, "%ju",
1402 (uintmax_t)*(uint64_t *)&nvp_data->nv_data[0]);
1407 case DATA_TYPE_STRING:
1408 nvp_value = (nv_string_t *)&nvp_data->nv_data[0];
1409 if ((value = nvstring_get(nvp_value)) == NULL) {
1420 if (value != NULL) {
1421 rv = env_setenv(name, EV_VOLATILE | EV_NOHOOK, value,
1422 zfs_nvstore_sethook, zfs_nvstore_unsethook);
1430 zfs_nvstore_iterate(void *vdev, int (*cb)(void *, void *))
1432 struct zfs_devdesc *dev = (struct zfs_devdesc *)vdev;
1438 if (dev->dd.d_dev->dv_type != DEVT_ZFS)
1441 if ((spa = spa_find_by_dev(dev)) == NULL)
1444 if (spa->spa_bootenv == NULL)
1447 if (nvlist_find(spa->spa_bootenv, OS_NVSTORE, DATA_TYPE_NVLIST,
1448 NULL, &nv, NULL) != 0)
1453 while ((nvh = nvlist_next_nvpair(nv, nvh)) != NULL) {
1461 nvs_callbacks_t nvstore_zfs_cb = {
1462 .nvs_getter = zfs_nvstore_getter,
1463 .nvs_setter = zfs_nvstore_setter,
1464 .nvs_setter_str = zfs_nvstore_setter_str,
1465 .nvs_unset = zfs_nvstore_unset,
1466 .nvs_print = zfs_nvstore_print,
1467 .nvs_iterate = zfs_nvstore_iterate
1471 zfs_attach_nvstore(void *vdev)
1473 struct zfs_devdesc *dev = vdev;
1478 if (dev->dd.d_dev->dv_type != DEVT_ZFS)
1481 if ((spa = spa_find_by_dev(dev)) == NULL)
1484 rv = nvlist_find(spa->spa_bootenv, BOOTENV_VERSION, DATA_TYPE_UINT64,
1485 NULL, &version, NULL);
1487 if (rv != 0 || version != VB_NVLIST) {
1491 dev = malloc(sizeof (*dev));
1494 memcpy(dev, vdev, sizeof (*dev));
1496 rv = nvstore_init(spa->spa_name, &nvstore_zfs_cb, dev);
1500 rv = zfs_nvstore_iterate(dev, zfs_nvstore_setenv);
1505 zfs_probe_dev(const char *devname, uint64_t *pool_guid, bool parts_too)
1507 struct ptable *table;
1508 struct zfs_probe_args pa;
1514 pa.fd = open(devname, O_RDWR);
1517 /* Probe the whole disk */
1518 ret = zfs_probe(pa.fd, pool_guid);
1524 /* Probe each partition */
1525 ret = ioctl(pa.fd, DIOCGMEDIASIZE, &mediasz);
1527 ret = ioctl(pa.fd, DIOCGSECTORSIZE, &pa.secsz);
1529 pa.devname = devname;
1530 pa.pool_guid = pool_guid;
1531 table = ptable_open(&pa, mediasz / pa.secsz, pa.secsz,
1533 if (table != NULL) {
1534 ptable_iterate(table, &pa, zfs_probe_partition);
1535 ptable_close(table);
1539 if (pool_guid && *pool_guid == 0)
1545 * Print information about ZFS pools
1548 zfs_dev_print(int verbose)
1554 if (STAILQ_EMPTY(&zfs_pools))
1557 printf("%s devices:", zfs_dev.dv_name);
1558 if ((ret = pager_output("\n")) != 0)
1562 return (spa_all_status());
1564 STAILQ_FOREACH(spa, &zfs_pools, spa_link) {
1565 snprintf(line, sizeof(line), " zfs:%s\n", spa->spa_name);
1566 ret = pager_output(line);
1574 * Attempt to open the pool described by (dev) for use by (f).
1577 zfs_dev_open(struct open_file *f, ...)
1580 struct zfs_devdesc *dev;
1581 struct zfsmount *mount;
1586 dev = va_arg(args, struct zfs_devdesc *);
1589 if ((spa = spa_find_by_dev(dev)) == NULL)
1592 STAILQ_FOREACH(mount, &zfsmount, next) {
1593 if (spa->spa_guid == mount->spa->spa_guid)
1598 /* This device is not set as currdev, mount us private copy. */
1600 rv = zfs_mount(devformat(&dev->dd), NULL, (void **)&mount);
1603 dev->dd.d_opendata = mount;
1609 zfs_dev_close(struct open_file *f)
1611 struct devdesc *dev;
1612 struct zfsmount *mnt, *mount;
1615 mnt = dev->d_opendata;
1617 STAILQ_FOREACH(mount, &zfsmount, next) {
1618 if (mnt->spa->spa_guid == mount->spa->spa_guid)
1627 zfs_dev_strategy(void *devdata, int rw, daddr_t dblk, size_t size, char *buf, size_t *rsize)
1633 struct devsw zfs_dev = {
1635 .dv_type = DEVT_ZFS,
1636 .dv_init = zfs_dev_init,
1637 .dv_strategy = zfs_dev_strategy,
1638 .dv_open = zfs_dev_open,
1639 .dv_close = zfs_dev_close,
1640 .dv_ioctl = noioctl,
1641 .dv_print = zfs_dev_print,
1642 .dv_cleanup = nullsys,
1643 .dv_fmtdev = zfs_fmtdev,
1644 .dv_parsedev = zfs_parsedev,
1648 zfs_parsedev(struct devdesc **idev, const char *devspec, const char **path)
1650 static char rootname[ZFS_MAXNAMELEN];
1651 static char poolname[ZFS_MAXNAMELEN];
1657 struct zfs_devdesc *dev;
1659 np = devspec + 3; /* Skip the leading 'zfs' */
1663 end = strrchr(np, ':');
1666 sep = strchr(np, '/');
1667 if (sep == NULL || sep >= end)
1669 memcpy(poolname, np, sep - np);
1670 poolname[sep - np] = '\0';
1673 memcpy(rootname, sep, end - sep);
1674 rootname[end - sep] = '\0';
1679 spa = spa_find_by_name(poolname);
1682 dev = malloc(sizeof(*dev));
1685 dev->pool_guid = spa->spa_guid;
1686 rv = zfs_lookup_dataset(spa, rootname, &dev->root_guid);
1692 *path = (*end == '\0') ? end : end + 1;
1693 dev->dd.d_dev = &zfs_dev;
1699 zfs_fmtdev(struct devdesc *vdev)
1701 static char rootname[ZFS_MAXNAMELEN];
1702 static char buf[2 * ZFS_MAXNAMELEN + 8];
1703 struct zfs_devdesc *dev = (struct zfs_devdesc *)vdev;
1707 if (vdev->d_dev->dv_type != DEVT_ZFS)
1710 /* Do we have any pools? */
1711 spa = STAILQ_FIRST(&zfs_pools);
1715 if (dev->pool_guid == 0)
1716 dev->pool_guid = spa->spa_guid;
1718 spa = spa_find_by_guid(dev->pool_guid);
1721 printf("ZFS: can't find pool by guid\n");
1724 if (dev->root_guid == 0 && zfs_get_root(spa, &dev->root_guid)) {
1725 printf("ZFS: can't find root filesystem\n");
1728 if (zfs_rlookup(spa, dev->root_guid, rootname)) {
1729 printf("ZFS: can't find filesystem by guid\n");
1733 if (rootname[0] == '\0')
1734 snprintf(buf, sizeof(buf), "%s:%s:", dev->dd.d_dev->dv_name,
1737 snprintf(buf, sizeof(buf), "%s:%s/%s:", dev->dd.d_dev->dv_name,
1738 spa->spa_name, rootname);
1743 split_devname(const char *name, char *poolname, size_t size,
1744 const char **dsnamep)
1749 ASSERT(name != NULL);
1750 ASSERT(poolname != NULL);
1753 dsname = strchr(name, '/');
1754 if (dsname != NULL) {
1755 len = dsname - name;
1763 strlcpy(poolname, name, len + 1);
1765 if (dsnamep != NULL)
1772 zfs_list(const char *name)
1774 static char poolname[ZFS_MAXNAMELEN];
1780 if (split_devname(name, poolname, sizeof(poolname), &dsname) != 0)
1783 spa = spa_find_by_name(poolname);
1786 rv = zfs_lookup_dataset(spa, dsname, &objid);
1790 return (zfs_list_dataset(spa, objid));
1794 init_zfs_boot_options(const char *currdev_in)
1796 char poolname[ZFS_MAXNAMELEN];
1797 char *beroot, *currdev;
1803 currdev_len = strlen(currdev_in);
1804 if (currdev_len == 0)
1806 if (strncmp(currdev_in, "zfs:", 4) != 0)
1808 currdev = strdup(currdev_in);
1809 if (currdev == NULL)
1811 /* Remove the trailing : */
1812 currdev[currdev_len - 1] = '\0';
1814 setenv("zfs_be_active", currdev, 1);
1815 setenv("zfs_be_currpage", "1", 1);
1816 /* Remove the last element (current bootenv) */
1817 beroot = strrchr(currdev, '/');
1820 beroot = strchr(currdev, ':') + 1;
1821 setenv("zfs_be_root", beroot, 1);
1823 if (split_devname(beroot, poolname, sizeof(poolname), &dsname) != 0)
1826 spa = spa_find_by_name(poolname);
1830 zfs_bootenv_initial("bootenvs", spa, beroot, dsname, 0);
1831 zfs_checkpoints_initial(spa, beroot, dsname);
1837 zfs_checkpoints_initial(spa_t *spa, const char *name, const char *dsname)
1841 if (spa->spa_uberblock_checkpoint.ub_checkpoint_txg != 0) {
1842 snprintf(envname, sizeof(envname), "zpool_checkpoint");
1843 setenv(envname, name, 1);
1845 spa->spa_uberblock = &spa->spa_uberblock_checkpoint;
1846 spa->spa_mos = &spa->spa_mos_checkpoint;
1848 zfs_bootenv_initial("bootenvs_check", spa, name, dsname, 1);
1850 spa->spa_uberblock = &spa->spa_uberblock_master;
1851 spa->spa_mos = &spa->spa_mos_master;
1856 zfs_bootenv_initial(const char *envprefix, spa_t *spa, const char *rootname,
1857 const char *dsname, int checkpoint)
1859 char envname[32], envval[256];
1861 int bootenvs_idx, rv;
1863 SLIST_INIT(&zfs_be_head);
1866 rv = zfs_lookup_dataset(spa, dsname, &objid);
1870 rv = zfs_callback_dataset(spa, objid, zfs_belist_add);
1872 /* Populate the initial environment variables */
1873 SLIST_FOREACH_SAFE(zfs_be, &zfs_be_head, entries, zfs_be_tmp) {
1874 /* Enumerate all bootenvs for general usage */
1875 snprintf(envname, sizeof(envname), "%s[%d]",
1876 envprefix, bootenvs_idx);
1877 snprintf(envval, sizeof(envval), "zfs:%s%s/%s",
1878 checkpoint ? "!" : "", rootname, zfs_be->name);
1879 rv = setenv(envname, envval, 1);
1884 snprintf(envname, sizeof(envname), "%s_count", envprefix);
1885 snprintf(envval, sizeof(envval), "%d", bootenvs_idx);
1886 setenv(envname, envval, 1);
1888 /* Clean up the SLIST of ZFS BEs */
1889 while (!SLIST_EMPTY(&zfs_be_head)) {
1890 zfs_be = SLIST_FIRST(&zfs_be_head);
1891 SLIST_REMOVE_HEAD(&zfs_be_head, entries);
1898 zfs_bootenv(const char *name)
1900 char poolname[ZFS_MAXNAMELEN], *root;
1905 int rv, pages, perpage, currpage;
1909 if ((root = getenv("zfs_be_root")) == NULL)
1912 if (strcmp(name, root) != 0) {
1913 if (setenv("zfs_be_root", name, 1) != 0)
1917 SLIST_INIT(&zfs_be_head);
1920 if (split_devname(name, poolname, sizeof(poolname), &dsname) != 0)
1923 spa = spa_find_by_name(poolname);
1926 rv = zfs_lookup_dataset(spa, dsname, &objid);
1929 rv = zfs_callback_dataset(spa, objid, zfs_belist_add);
1931 /* Calculate and store the number of pages of BEs */
1932 perpage = (ZFS_BE_LAST - ZFS_BE_FIRST + 1);
1933 pages = (zfs_env_count / perpage) + ((zfs_env_count % perpage) > 0 ? 1 : 0);
1934 snprintf(becount, 4, "%d", pages);
1935 if (setenv("zfs_be_pages", becount, 1) != 0)
1938 /* Roll over the page counter if it has exceeded the maximum */
1939 currpage = strtol(getenv("zfs_be_currpage"), NULL, 10);
1940 if (currpage > pages) {
1941 if (setenv("zfs_be_currpage", "1", 1) != 0)
1945 /* Populate the menu environment variables */
1948 /* Clean up the SLIST of ZFS BEs */
1949 while (!SLIST_EMPTY(&zfs_be_head)) {
1950 zfs_be = SLIST_FIRST(&zfs_be_head);
1951 SLIST_REMOVE_HEAD(&zfs_be_head, entries);
1960 zfs_belist_add(const char *name, uint64_t value __unused)
1963 /* Skip special datasets that start with a $ character */
1964 if (strncmp(name, "$", 1) == 0) {
1967 /* Add the boot environment to the head of the SLIST */
1968 zfs_be = malloc(sizeof(struct zfs_be_entry));
1969 if (zfs_be == NULL) {
1972 zfs_be->name = strdup(name);
1973 if (zfs_be->name == NULL) {
1977 SLIST_INSERT_HEAD(&zfs_be_head, zfs_be, entries);
1986 char envname[32], envval[256];
1987 char *beroot, *pagenum;
1990 beroot = getenv("zfs_be_root");
1991 if (beroot == NULL) {
1995 pagenum = getenv("zfs_be_currpage");
1996 if (pagenum != NULL) {
1997 page = strtol(pagenum, NULL, 10);
2004 zfs_env_index = ZFS_BE_FIRST;
2005 SLIST_FOREACH_SAFE(zfs_be, &zfs_be_head, entries, zfs_be_tmp) {
2006 /* Skip to the requested page number */
2007 if (ctr <= ((ZFS_BE_LAST - ZFS_BE_FIRST + 1) * (page - 1))) {
2012 snprintf(envname, sizeof(envname), "bootenvmenu_caption[%d]", zfs_env_index);
2013 snprintf(envval, sizeof(envval), "%s", zfs_be->name);
2014 rv = setenv(envname, envval, 1);
2019 snprintf(envname, sizeof(envname), "bootenvansi_caption[%d]", zfs_env_index);
2020 rv = setenv(envname, envval, 1);
2025 snprintf(envname, sizeof(envname), "bootenvmenu_command[%d]", zfs_env_index);
2026 rv = setenv(envname, "set_bootenv", 1);
2031 snprintf(envname, sizeof(envname), "bootenv_root[%d]", zfs_env_index);
2032 snprintf(envval, sizeof(envval), "zfs:%s/%s", beroot, zfs_be->name);
2033 rv = setenv(envname, envval, 1);
2039 if (zfs_env_index > ZFS_BE_LAST) {
2045 for (; zfs_env_index <= ZFS_BE_LAST; zfs_env_index++) {
2046 snprintf(envname, sizeof(envname), "bootenvmenu_caption[%d]", zfs_env_index);
2047 (void)unsetenv(envname);
2048 snprintf(envname, sizeof(envname), "bootenvansi_caption[%d]", zfs_env_index);
2049 (void)unsetenv(envname);
2050 snprintf(envname, sizeof(envname), "bootenvmenu_command[%d]", zfs_env_index);
2051 (void)unsetenv(envname);
2052 snprintf(envname, sizeof(envname), "bootenv_root[%d]", zfs_env_index);
2053 (void)unsetenv(envname);