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,
71 struct fs_ops zfs_fsops = {
74 .fo_close = zfs_close,
76 .fo_write = null_write,
79 .fo_readdir = zfs_readdir,
80 .fo_mount = zfs_mount,
81 .fo_unmount = zfs_unmount
88 off_t f_seekp; /* seek pointer */
90 uint64_t f_zap_type; /* zap type for readdir */
91 uint64_t f_num_leafs; /* number of fzap leaf blocks */
92 zap_leaf_phys_t *f_zap_leaf; /* zap leaf buffer */
95 static int zfs_env_index;
96 static int zfs_env_count;
98 SLIST_HEAD(zfs_be_list, zfs_be_entry) zfs_be_head = SLIST_HEAD_INITIALIZER(zfs_be_head);
99 struct zfs_be_list *zfs_be_headp;
100 struct zfs_be_entry {
102 SLIST_ENTRY(zfs_be_entry) entries;
103 } *zfs_be, *zfs_be_tmp;
109 zfs_open(const char *upath, struct open_file *f)
111 struct zfsmount *mount = (struct zfsmount *)f->f_devdata;
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 const spa_t *spa = ((struct zfsmount *)f->f_devdata)->spa;
153 struct file *fp = (struct file *)f->f_fsdata;
158 rc = zfs_stat(f, &sb);
162 if (fp->f_seekp + n > sb.st_size)
163 n = sb.st_size - fp->f_seekp;
165 rc = dnode_read(spa, &fp->f_dnode, fp->f_seekp, start, n);
171 for (i = 0; i < n; i++)
172 putchar(((char*) start)[i]);
182 zfs_seek(struct open_file *f, off_t offset, int where)
184 struct file *fp = (struct file *)f->f_fsdata;
188 fp->f_seekp = offset;
191 fp->f_seekp += offset;
198 error = zfs_stat(f, &sb);
203 fp->f_seekp = sb.st_size - offset;
210 return (fp->f_seekp);
214 zfs_stat(struct open_file *f, struct stat *sb)
216 const spa_t *spa = ((struct zfsmount *)f->f_devdata)->spa;
217 struct file *fp = (struct file *)f->f_fsdata;
219 return (zfs_dnode_stat(spa, &fp->f_dnode, sb));
223 zfs_readdir(struct open_file *f, struct dirent *d)
225 const spa_t *spa = ((struct zfsmount *)f->f_devdata)->spa;
226 struct file *fp = (struct file *)f->f_fsdata;
229 size_t bsize = fp->f_dnode.dn_datablkszsec << SPA_MINBLOCKSHIFT;
232 rc = zfs_stat(f, &sb);
235 if (!S_ISDIR(sb.st_mode))
239 * If this is the first read, get the zap type.
241 if (fp->f_seekp == 0) {
242 rc = dnode_read(spa, &fp->f_dnode,
243 0, &fp->f_zap_type, sizeof(fp->f_zap_type));
247 if (fp->f_zap_type == ZBT_MICRO) {
248 fp->f_seekp = offsetof(mzap_phys_t, mz_chunk);
250 rc = dnode_read(spa, &fp->f_dnode,
251 offsetof(zap_phys_t, zap_num_leafs),
253 sizeof(fp->f_num_leafs));
258 fp->f_zap_leaf = malloc(bsize);
259 if (fp->f_zap_leaf == NULL)
261 rc = dnode_read(spa, &fp->f_dnode,
270 if (fp->f_zap_type == ZBT_MICRO) {
272 if (fp->f_seekp >= bsize)
275 rc = dnode_read(spa, &fp->f_dnode,
276 fp->f_seekp, &mze, sizeof(mze));
279 fp->f_seekp += sizeof(mze);
281 if (!mze.mze_name[0])
284 d->d_fileno = ZFS_DIRENT_OBJ(mze.mze_value);
285 d->d_type = ZFS_DIRENT_TYPE(mze.mze_value);
286 strcpy(d->d_name, mze.mze_name);
287 d->d_namlen = strlen(d->d_name);
291 zap_leaf_chunk_t *zc, *nc;
298 * Initialise this so we can use the ZAP size
299 * calculating macros.
301 zl.l_bs = ilog2(bsize);
302 zl.l_phys = fp->f_zap_leaf;
305 * Figure out which chunk we are currently looking at
306 * and consider seeking to the next leaf. We use the
307 * low bits of f_seekp as a simple chunk index.
310 chunk = fp->f_seekp & (bsize - 1);
311 if (chunk == ZAP_LEAF_NUMCHUNKS(&zl)) {
312 fp->f_seekp = rounddown2(fp->f_seekp, bsize) + bsize;
316 * Check for EOF and read the new leaf.
318 if (fp->f_seekp >= bsize * fp->f_num_leafs)
321 rc = dnode_read(spa, &fp->f_dnode,
329 zc = &ZAP_LEAF_CHUNK(&zl, chunk);
331 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
334 namelen = zc->l_entry.le_name_numints;
335 if (namelen > sizeof(d->d_name))
336 namelen = sizeof(d->d_name);
339 * Paste the name back together.
341 nc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_name_chunk);
343 while (namelen > 0) {
346 if (len > ZAP_LEAF_ARRAY_BYTES)
347 len = ZAP_LEAF_ARRAY_BYTES;
348 memcpy(p, nc->l_array.la_array, len);
351 nc = &ZAP_LEAF_CHUNK(&zl, nc->l_array.la_next);
353 d->d_name[sizeof(d->d_name) - 1] = 0;
356 * Assume the first eight bytes of the value are
359 value = fzap_leaf_value(&zl, zc);
361 d->d_fileno = ZFS_DIRENT_OBJ(value);
362 d->d_type = ZFS_DIRENT_TYPE(value);
363 d->d_namlen = strlen(d->d_name);
370 * if path is NULL, create mount structure, but do not add it to list.
373 zfs_mount(const char *dev, const char *path, void **data)
375 struct zfs_devdesc *zfsdev;
377 struct zfsmount *mnt;
381 zfsdev = malloc(sizeof(*zfsdev));
385 rv = zfs_parsedev(zfsdev, dev + 3, NULL);
391 spa = spa_find_by_dev(zfsdev);
395 mnt = calloc(1, sizeof(*mnt));
396 if (mnt != NULL && path != NULL)
397 mnt->path = strdup(path);
401 rv = zfs_mount_impl(spa, zfsdev->root_guid, mnt);
404 if (rv == 0 && mnt != NULL && mnt->objset.os_type != DMU_OST_ZFS) {
405 printf("Unexpected object set type %ju\n",
406 (uintmax_t)mnt->objset.os_type);
420 STAILQ_INSERT_TAIL(&zfsmount, mnt, next);
427 zfs_unmount(const char *dev, void *data)
429 struct zfsmount *mnt = data;
431 STAILQ_REMOVE(&zfsmount, mnt, zfsmount, next);
438 vdev_read(vdev_t *vdev, void *priv, off_t offset, void *buf, size_t bytes)
441 size_t res, head, tail, total_size, full_sec_size;
442 unsigned secsz, do_tail_read;
444 char *outbuf, *bouncebuf;
446 fd = (uintptr_t) priv;
447 outbuf = (char *) buf;
450 ret = ioctl(fd, DIOCGSECTORSIZE, &secsz);
455 * Handling reads of arbitrary offset and size - multi-sector case
456 * and single-sector case.
459 * (do_tail_read = true if tail > 0)
461 * |<----------------------total_size--------------------->|
463 * |<--head-->|<--------------bytes------------>|<--tail-->|
465 * | | |<~full_sec_size~>| | |
466 * +------------------+ +------------------+
467 * | |0101010| . . . |0101011| |
468 * +------------------+ +------------------+
469 * start_sec start_sec + n
473 * (do_tail_read = false)
475 * |<------total_size = secsz----->|
477 * |<-head->|<---bytes--->|<-tail->|
478 * +-------------------------------+
479 * | |0101010101010| |
480 * +-------------------------------+
483 start_sec = offset / secsz;
484 head = offset % secsz;
485 total_size = roundup2(head + bytes, secsz);
486 tail = total_size - (head + bytes);
487 do_tail_read = ((tail > 0) && (head + bytes > secsz));
488 full_sec_size = total_size;
490 full_sec_size -= secsz;
492 full_sec_size -= secsz;
494 /* Return of partial sector data requires a bounce buffer. */
495 if ((head > 0) || do_tail_read || bytes < secsz) {
496 bouncebuf = malloc(secsz);
497 if (bouncebuf == NULL) {
498 printf("vdev_read: out of memory\n");
503 if (lseek(fd, start_sec * secsz, SEEK_SET) == -1) {
508 /* Partial data return from first sector */
510 res = read(fd, bouncebuf, secsz);
515 memcpy(outbuf, bouncebuf + head, min(secsz - head, bytes));
516 outbuf += min(secsz - head, bytes);
520 * Full data return from read sectors.
521 * Note, there is still corner case where we read
522 * from sector boundary, but less than sector size, e.g. reading 512B
525 if (full_sec_size > 0) {
526 if (bytes < full_sec_size) {
527 res = read(fd, bouncebuf, secsz);
532 memcpy(outbuf, bouncebuf, bytes);
534 res = read(fd, outbuf, full_sec_size);
535 if (res != full_sec_size) {
539 outbuf += full_sec_size;
543 /* Partial data return from last sector */
545 res = read(fd, bouncebuf, secsz);
550 memcpy(outbuf, bouncebuf, secsz - tail);
560 vdev_write(vdev_t *vdev, off_t offset, void *buf, size_t bytes)
563 size_t head, tail, total_size, full_sec_size;
564 unsigned secsz, do_tail_write;
567 char *outbuf, *bouncebuf;
569 fd = (uintptr_t)vdev->v_priv;
570 outbuf = (char *)buf;
573 ret = ioctl(fd, DIOCGSECTORSIZE, &secsz);
577 start_sec = offset / secsz;
578 head = offset % secsz;
579 total_size = roundup2(head + bytes, secsz);
580 tail = total_size - (head + bytes);
581 do_tail_write = ((tail > 0) && (head + bytes > secsz));
582 full_sec_size = total_size;
584 full_sec_size -= secsz;
586 full_sec_size -= secsz;
588 /* Partial sector write requires a bounce buffer. */
589 if ((head > 0) || do_tail_write || bytes < secsz) {
590 bouncebuf = malloc(secsz);
591 if (bouncebuf == NULL) {
592 printf("vdev_write: out of memory\n");
597 if (lseek(fd, start_sec * secsz, SEEK_SET) == -1) {
602 /* Partial data for first sector */
604 res = read(fd, bouncebuf, secsz);
605 if ((unsigned)res != secsz) {
609 memcpy(bouncebuf + head, outbuf, min(secsz - head, bytes));
610 (void) lseek(fd, -secsz, SEEK_CUR);
611 res = write(fd, bouncebuf, secsz);
612 if ((unsigned)res != secsz) {
616 outbuf += min(secsz - head, bytes);
620 * Full data write to sectors.
621 * Note, there is still corner case where we write
622 * to sector boundary, but less than sector size, e.g. write 512B
625 if (full_sec_size > 0) {
626 if (bytes < full_sec_size) {
627 res = read(fd, bouncebuf, secsz);
628 if ((unsigned)res != secsz) {
632 memcpy(bouncebuf, outbuf, bytes);
633 (void) lseek(fd, -secsz, SEEK_CUR);
634 res = write(fd, bouncebuf, secsz);
635 if ((unsigned)res != secsz) {
640 res = write(fd, outbuf, full_sec_size);
641 if ((unsigned)res != full_sec_size) {
645 outbuf += full_sec_size;
649 /* Partial data write to last sector */
651 res = read(fd, bouncebuf, secsz);
652 if ((unsigned)res != secsz) {
656 memcpy(bouncebuf, outbuf, secsz - tail);
657 (void) lseek(fd, -secsz, SEEK_CUR);
658 res = write(fd, bouncebuf, secsz);
659 if ((unsigned)res != secsz) {
679 if (archsw.arch_zfs_probe == NULL)
681 archsw.arch_zfs_probe();
684 spa = STAILQ_FIRST(&zfs_pools);
685 while (spa != NULL) {
686 next = STAILQ_NEXT(spa, spa_link);
687 if (zfs_spa_init(spa)) {
689 STAILQ_REMOVE_HEAD(&zfs_pools, spa_link);
691 STAILQ_REMOVE_AFTER(&zfs_pools, prev, spa_link);
699 struct zfs_probe_args {
707 zfs_diskread(void *arg, void *buf, size_t blocks, uint64_t offset)
709 struct zfs_probe_args *ppa;
711 ppa = (struct zfs_probe_args *)arg;
712 return (vdev_read(NULL, (void *)(uintptr_t)ppa->fd,
713 offset * ppa->secsz, buf, blocks * ppa->secsz));
717 zfs_probe(int fd, uint64_t *pool_guid)
723 ret = vdev_probe(vdev_read, vdev_write, (void *)(uintptr_t)fd, &spa);
724 if (ret == 0 && pool_guid != NULL)
726 *pool_guid = spa->spa_guid;
731 zfs_probe_partition(void *arg, const char *partname,
732 const struct ptable_entry *part)
734 struct zfs_probe_args *ppa, pa;
735 struct ptable *table;
739 /* Probe only freebsd-zfs and freebsd partitions */
740 if (part->type != PART_FREEBSD &&
741 part->type != PART_FREEBSD_ZFS)
744 ppa = (struct zfs_probe_args *)arg;
745 strncpy(devname, ppa->devname, strlen(ppa->devname) - 1);
746 devname[strlen(ppa->devname) - 1] = '\0';
747 snprintf(devname, sizeof(devname), "%s%s:", devname, partname);
748 pa.fd = open(devname, O_RDWR);
751 ret = zfs_probe(pa.fd, ppa->pool_guid);
754 /* Do we have BSD label here? */
755 if (part->type == PART_FREEBSD) {
756 pa.devname = devname;
757 pa.pool_guid = ppa->pool_guid;
758 pa.secsz = ppa->secsz;
759 table = ptable_open(&pa, part->end - part->start + 1,
760 ppa->secsz, zfs_diskread);
762 ptable_iterate(table, &pa, zfs_probe_partition);
771 * Return bootenv nvlist from pool label.
774 zfs_get_bootenv(void *vdev, nvlist_t **benvp)
776 struct zfs_devdesc *dev = (struct zfs_devdesc *)vdev;
777 nvlist_t *benv = NULL;
781 if (dev->dd.d_dev->dv_type != DEVT_ZFS)
784 if ((spa = spa_find_by_dev(dev)) == NULL)
787 if (spa->spa_bootenv == NULL) {
788 STAILQ_FOREACH(vd, &spa->spa_root_vdev->v_children,
790 benv = vdev_read_bootenv(vd);
795 spa->spa_bootenv = benv;
797 benv = spa->spa_bootenv;
808 * Store nvlist to pool label bootenv area. Also updates cached pointer in spa.
811 zfs_set_bootenv(void *vdev, nvlist_t *benv)
813 struct zfs_devdesc *dev = (struct zfs_devdesc *)vdev;
817 if (dev->dd.d_dev->dv_type != DEVT_ZFS)
820 if ((spa = spa_find_by_dev(dev)) == NULL)
823 STAILQ_FOREACH(vd, &spa->spa_root_vdev->v_children, v_childlink) {
824 vdev_write_bootenv(vd, benv);
827 spa->spa_bootenv = benv;
832 * Get bootonce value by key. The bootonce <key, value> pair is removed
833 * from the bootenv nvlist and the remaining nvlist is committed back to disk.
836 zfs_get_bootonce(void *vdev, const char *key, char *buf, size_t size)
842 if ((rv = zfs_get_bootenv(vdev, &benv)) != 0)
845 if ((rv = nvlist_find(benv, key, DATA_TYPE_STRING, NULL,
846 &result, &result_size)) == 0) {
847 if (result_size == 0) {
848 /* ignore empty string */
851 size = MIN((size_t)result_size + 1, size);
852 strlcpy(buf, result, size);
854 (void) nvlist_remove(benv, key, DATA_TYPE_STRING);
855 (void) zfs_set_bootenv(vdev, benv);
865 static int zfs_nvstore_setter(void *, int, const char *,
866 const void *, size_t);
867 static int zfs_nvstore_setter_str(void *, const char *, const char *,
869 static int zfs_nvstore_unset_impl(void *, const char *, bool);
870 static int zfs_nvstore_setenv(void *, void *);
873 * nvstore is only present for current rootfs pool.
876 zfs_nvstore_sethook(struct env_var *ev, int flags __unused, const void *value)
878 struct zfs_devdesc *dev;
881 archsw.arch_getdev((void **)&dev, NULL, NULL);
885 rv = zfs_nvstore_setter_str(dev, NULL, ev->ev_name, value);
892 * nvstore is only present for current rootfs pool.
895 zfs_nvstore_unsethook(struct env_var *ev)
897 struct zfs_devdesc *dev;
900 archsw.arch_getdev((void **)&dev, NULL, NULL);
904 rv = zfs_nvstore_unset_impl(dev, ev->ev_name, false);
911 zfs_nvstore_getter(void *vdev, const char *name, void **data)
913 struct zfs_devdesc *dev = (struct zfs_devdesc *)vdev;
920 if (dev->dd.d_dev->dv_type != DEVT_ZFS)
923 if ((spa = spa_find_by_dev(dev)) == NULL)
926 if (spa->spa_bootenv == NULL)
929 if (nvlist_find(spa->spa_bootenv, OS_NVSTORE, DATA_TYPE_NVLIST,
930 NULL, &nv, NULL) != 0)
933 rv = nvlist_find(nv, name, DATA_TYPE_STRING, NULL, &str, &size);
936 asprintf(ptr, "%.*s", size, str);
945 zfs_nvstore_setter(void *vdev, int type, const char *name,
946 const void *data, size_t size)
948 struct zfs_devdesc *dev = (struct zfs_devdesc *)vdev;
954 if (dev->dd.d_dev->dv_type != DEVT_ZFS)
957 if ((spa = spa_find_by_dev(dev)) == NULL)
960 if (spa->spa_bootenv == NULL)
963 if (nvlist_find(spa->spa_bootenv, OS_NVSTORE, DATA_TYPE_NVLIST,
964 NULL, &nv, NULL) != 0) {
965 nv = nvlist_create(NV_UNIQUE_NAME);
973 if (size != sizeof (int8_t)) {
977 rv = nvlist_add_int8(nv, name, *(int8_t *)data);
980 case DATA_TYPE_INT16:
981 if (size != sizeof (int16_t)) {
985 rv = nvlist_add_int16(nv, name, *(int16_t *)data);
988 case DATA_TYPE_INT32:
989 if (size != sizeof (int32_t)) {
993 rv = nvlist_add_int32(nv, name, *(int32_t *)data);
996 case DATA_TYPE_INT64:
997 if (size != sizeof (int64_t)) {
1001 rv = nvlist_add_int64(nv, name, *(int64_t *)data);
1004 case DATA_TYPE_BYTE:
1005 if (size != sizeof (uint8_t)) {
1009 rv = nvlist_add_byte(nv, name, *(int8_t *)data);
1012 case DATA_TYPE_UINT8:
1013 if (size != sizeof (uint8_t)) {
1017 rv = nvlist_add_uint8(nv, name, *(int8_t *)data);
1020 case DATA_TYPE_UINT16:
1021 if (size != sizeof (uint16_t)) {
1025 rv = nvlist_add_uint16(nv, name, *(uint16_t *)data);
1028 case DATA_TYPE_UINT32:
1029 if (size != sizeof (uint32_t)) {
1033 rv = nvlist_add_uint32(nv, name, *(uint32_t *)data);
1036 case DATA_TYPE_UINT64:
1037 if (size != sizeof (uint64_t)) {
1041 rv = nvlist_add_uint64(nv, name, *(uint64_t *)data);
1044 case DATA_TYPE_STRING:
1045 rv = nvlist_add_string(nv, name, data);
1048 case DATA_TYPE_BOOLEAN_VALUE:
1049 if (size != sizeof (boolean_t)) {
1053 rv = nvlist_add_boolean_value(nv, name, *(boolean_t *)data);
1062 rv = nvlist_add_nvlist(spa->spa_bootenv, OS_NVSTORE, nv);
1064 rv = zfs_set_bootenv(vdev, spa->spa_bootenv);
1068 rv = zfs_nvstore_setenv(vdev,
1069 nvpair_find(nv, name));
1071 env_discard(env_getenv(name));
1082 get_int64(const char *data, int64_t *ip)
1088 val = strtoll(data, &end, 0);
1089 if (errno != 0 || *data == '\0' || *end != '\0')
1097 get_uint64(const char *data, uint64_t *ip)
1103 val = strtoull(data, &end, 0);
1104 if (errno != 0 || *data == '\0' || *end != '\0')
1112 * Translate textual data to data type. If type is not set, and we are
1113 * creating new pair, use DATA_TYPE_STRING.
1116 zfs_nvstore_setter_str(void *vdev, const char *type, const char *name,
1119 struct zfs_devdesc *dev = (struct zfs_devdesc *)vdev;
1127 if (dev->dd.d_dev->dv_type != DEVT_ZFS)
1130 if ((spa = spa_find_by_dev(dev)) == NULL)
1133 if (spa->spa_bootenv == NULL)
1136 if (nvlist_find(spa->spa_bootenv, OS_NVSTORE, DATA_TYPE_NVLIST,
1137 NULL, &nv, NULL) != 0) {
1145 * if there is no existing pair, default to string.
1146 * Otherwise, use type from existing pair.
1148 nvh = nvpair_find(nv, name);
1150 dt = DATA_TYPE_STRING;
1152 nv_string_t *nvp_name;
1153 nv_pair_data_t *nvp_data;
1155 nvp_name = (nv_string_t *)(nvh + 1);
1156 nvp_data = (nv_pair_data_t *)(&nvp_name->nv_data[0] +
1157 NV_ALIGN4(nvp_name->nv_size));
1158 dt = nvp_data->nv_type;
1161 dt = nvpair_type_from_name(type);
1167 case DATA_TYPE_INT8:
1168 rv = get_int64(data, &val);
1172 rv = zfs_nvstore_setter(vdev, dt, name, &v, sizeof (v));
1175 case DATA_TYPE_INT16:
1176 rv = get_int64(data, &val);
1180 rv = zfs_nvstore_setter(vdev, dt, name, &v, sizeof (v));
1183 case DATA_TYPE_INT32:
1184 rv = get_int64(data, &val);
1188 rv = zfs_nvstore_setter(vdev, dt, name, &v, sizeof (v));
1191 case DATA_TYPE_INT64:
1192 rv = get_int64(data, &val);
1194 rv = zfs_nvstore_setter(vdev, dt, name, &val,
1199 case DATA_TYPE_BYTE:
1200 rv = get_uint64(data, &uval);
1204 rv = zfs_nvstore_setter(vdev, dt, name, &v, sizeof (v));
1208 case DATA_TYPE_UINT8:
1209 rv = get_uint64(data, &uval);
1213 rv = zfs_nvstore_setter(vdev, dt, name, &v, sizeof (v));
1217 case DATA_TYPE_UINT16:
1218 rv = get_uint64(data, &uval);
1222 rv = zfs_nvstore_setter(vdev, dt, name, &v, sizeof (v));
1226 case DATA_TYPE_UINT32:
1227 rv = get_uint64(data, &uval);
1231 rv = zfs_nvstore_setter(vdev, dt, name, &v, sizeof (v));
1235 case DATA_TYPE_UINT64:
1236 rv = get_uint64(data, &uval);
1238 rv = zfs_nvstore_setter(vdev, dt, name, &uval,
1243 case DATA_TYPE_STRING:
1244 rv = zfs_nvstore_setter(vdev, dt, name, data, strlen(data) + 1);
1247 case DATA_TYPE_BOOLEAN_VALUE:
1248 rv = get_int64(data, &val);
1252 rv = zfs_nvstore_setter(vdev, dt, name, &v, sizeof (v));
1262 zfs_nvstore_unset_impl(void *vdev, const char *name, bool unset_env)
1264 struct zfs_devdesc *dev = (struct zfs_devdesc *)vdev;
1269 if (dev->dd.d_dev->dv_type != DEVT_ZFS)
1272 if ((spa = spa_find_by_dev(dev)) == NULL)
1275 if (spa->spa_bootenv == NULL)
1278 if (nvlist_find(spa->spa_bootenv, OS_NVSTORE, DATA_TYPE_NVLIST,
1279 NULL, &nv, NULL) != 0)
1282 rv = nvlist_remove(nv, name, DATA_TYPE_UNKNOWN);
1284 if (nvlist_next_nvpair(nv, NULL) == NULL) {
1285 rv = nvlist_remove(spa->spa_bootenv, OS_NVSTORE,
1288 rv = nvlist_add_nvlist(spa->spa_bootenv,
1292 rv = zfs_set_bootenv(vdev, spa->spa_bootenv);
1296 env_discard(env_getenv(name));
1301 zfs_nvstore_unset(void *vdev, const char *name)
1303 return (zfs_nvstore_unset_impl(vdev, name, true));
1307 zfs_nvstore_print(void *vdev __unused, void *ptr)
1310 nvpair_print(ptr, 0);
1315 * Create environment variable from nvpair.
1316 * set hook will update nvstore with new value, unset hook will remove
1317 * variable from nvstore.
1320 zfs_nvstore_setenv(void *vdev __unused, void *ptr)
1322 nvp_header_t *nvh = ptr;
1323 nv_string_t *nvp_name, *nvp_value;
1324 nv_pair_data_t *nvp_data;
1331 nvp_name = (nv_string_t *)(nvh + 1);
1332 nvp_data = (nv_pair_data_t *)(&nvp_name->nv_data[0] +
1333 NV_ALIGN4(nvp_name->nv_size));
1335 if ((name = nvstring_get(nvp_name)) == NULL)
1339 switch (nvp_data->nv_type) {
1340 case DATA_TYPE_BYTE:
1341 case DATA_TYPE_UINT8:
1342 (void) asprintf(&value, "%uc",
1343 *(unsigned *)&nvp_data->nv_data[0]);
1348 case DATA_TYPE_INT8:
1349 (void) asprintf(&value, "%c", *(int *)&nvp_data->nv_data[0]);
1354 case DATA_TYPE_INT16:
1355 (void) asprintf(&value, "%hd", *(short *)&nvp_data->nv_data[0]);
1360 case DATA_TYPE_UINT16:
1361 (void) asprintf(&value, "%hu",
1362 *(unsigned short *)&nvp_data->nv_data[0]);
1367 case DATA_TYPE_BOOLEAN_VALUE:
1368 case DATA_TYPE_INT32:
1369 (void) asprintf(&value, "%d", *(int *)&nvp_data->nv_data[0]);
1374 case DATA_TYPE_UINT32:
1375 (void) asprintf(&value, "%u",
1376 *(unsigned *)&nvp_data->nv_data[0]);
1381 case DATA_TYPE_INT64:
1382 (void) asprintf(&value, "%jd",
1383 (intmax_t)*(int64_t *)&nvp_data->nv_data[0]);
1388 case DATA_TYPE_UINT64:
1389 (void) asprintf(&value, "%ju",
1390 (uintmax_t)*(uint64_t *)&nvp_data->nv_data[0]);
1395 case DATA_TYPE_STRING:
1396 nvp_value = (nv_string_t *)&nvp_data->nv_data[0];
1397 if ((value = nvstring_get(nvp_value)) == NULL) {
1408 if (value != NULL) {
1409 rv = env_setenv(name, EV_VOLATILE | EV_NOHOOK, value,
1410 zfs_nvstore_sethook, zfs_nvstore_unsethook);
1418 zfs_nvstore_iterate(void *vdev, int (*cb)(void *, void *))
1420 struct zfs_devdesc *dev = (struct zfs_devdesc *)vdev;
1426 if (dev->dd.d_dev->dv_type != DEVT_ZFS)
1429 if ((spa = spa_find_by_dev(dev)) == NULL)
1432 if (spa->spa_bootenv == NULL)
1435 if (nvlist_find(spa->spa_bootenv, OS_NVSTORE, DATA_TYPE_NVLIST,
1436 NULL, &nv, NULL) != 0)
1441 while ((nvh = nvlist_next_nvpair(nv, nvh)) != NULL) {
1449 nvs_callbacks_t nvstore_zfs_cb = {
1450 .nvs_getter = zfs_nvstore_getter,
1451 .nvs_setter = zfs_nvstore_setter,
1452 .nvs_setter_str = zfs_nvstore_setter_str,
1453 .nvs_unset = zfs_nvstore_unset,
1454 .nvs_print = zfs_nvstore_print,
1455 .nvs_iterate = zfs_nvstore_iterate
1459 zfs_attach_nvstore(void *vdev)
1461 struct zfs_devdesc *dev = vdev;
1466 if (dev->dd.d_dev->dv_type != DEVT_ZFS)
1469 if ((spa = spa_find_by_dev(dev)) == NULL)
1472 rv = nvlist_find(spa->spa_bootenv, BOOTENV_VERSION, DATA_TYPE_UINT64,
1473 NULL, &version, NULL);
1475 if (rv != 0 || version != VB_NVLIST) {
1479 dev = malloc(sizeof (*dev));
1482 memcpy(dev, vdev, sizeof (*dev));
1484 rv = nvstore_init(spa->spa_name, &nvstore_zfs_cb, dev);
1488 rv = zfs_nvstore_iterate(dev, zfs_nvstore_setenv);
1493 zfs_probe_dev(const char *devname, uint64_t *pool_guid)
1495 struct ptable *table;
1496 struct zfs_probe_args pa;
1502 pa.fd = open(devname, O_RDWR);
1505 /* Probe the whole disk */
1506 ret = zfs_probe(pa.fd, pool_guid);
1510 /* Probe each partition */
1511 ret = ioctl(pa.fd, DIOCGMEDIASIZE, &mediasz);
1513 ret = ioctl(pa.fd, DIOCGSECTORSIZE, &pa.secsz);
1515 pa.devname = devname;
1516 pa.pool_guid = pool_guid;
1517 table = ptable_open(&pa, mediasz / pa.secsz, pa.secsz,
1519 if (table != NULL) {
1520 ptable_iterate(table, &pa, zfs_probe_partition);
1521 ptable_close(table);
1525 if (pool_guid && *pool_guid == 0)
1531 * Print information about ZFS pools
1534 zfs_dev_print(int verbose)
1540 if (STAILQ_EMPTY(&zfs_pools))
1543 printf("%s devices:", zfs_dev.dv_name);
1544 if ((ret = pager_output("\n")) != 0)
1548 return (spa_all_status());
1550 STAILQ_FOREACH(spa, &zfs_pools, spa_link) {
1551 snprintf(line, sizeof(line), " zfs:%s\n", spa->spa_name);
1552 ret = pager_output(line);
1560 * Attempt to open the pool described by (dev) for use by (f).
1563 zfs_dev_open(struct open_file *f, ...)
1566 struct zfs_devdesc *dev;
1567 struct zfsmount *mount;
1572 dev = va_arg(args, struct zfs_devdesc *);
1575 if ((spa = spa_find_by_dev(dev)) == NULL)
1578 STAILQ_FOREACH(mount, &zfsmount, next) {
1579 if (spa->spa_guid == mount->spa->spa_guid)
1584 /* This device is not set as currdev, mount us private copy. */
1586 rv = zfs_mount(zfs_fmtdev(dev), NULL, (void **)&mount);
1589 f->f_devdata = mount;
1596 zfs_dev_close(struct open_file *f)
1598 struct zfsmount *mnt, *mount;
1602 STAILQ_FOREACH(mount, &zfsmount, next) {
1603 if (mnt->spa->spa_guid == mount->spa->spa_guid)
1608 * devclose() will free f->f_devdata, but since we do have
1609 * pointer to zfsmount structure in f->f_devdata, and
1610 * zfs_unmount() will also free the zfsmount structure,
1611 * we will get double free. To prevent double free,
1612 * we must set f_devdata to NULL there.
1615 f->f_devdata = NULL;
1621 zfs_dev_strategy(void *devdata, int rw, daddr_t dblk, size_t size, char *buf, size_t *rsize)
1627 struct devsw zfs_dev = {
1629 .dv_type = DEVT_ZFS,
1630 .dv_init = zfs_dev_init,
1631 .dv_strategy = zfs_dev_strategy,
1632 .dv_open = zfs_dev_open,
1633 .dv_close = zfs_dev_close,
1634 .dv_ioctl = noioctl,
1635 .dv_print = zfs_dev_print,
1640 zfs_parsedev(struct zfs_devdesc *dev, const char *devspec, const char **path)
1642 static char rootname[ZFS_MAXNAMELEN];
1643 static char poolname[ZFS_MAXNAMELEN];
1654 end = strrchr(np, ':');
1657 sep = strchr(np, '/');
1658 if (sep == NULL || sep >= end)
1660 memcpy(poolname, np, sep - np);
1661 poolname[sep - np] = '\0';
1664 memcpy(rootname, sep, end - sep);
1665 rootname[end - sep] = '\0';
1670 spa = spa_find_by_name(poolname);
1673 dev->pool_guid = spa->spa_guid;
1674 rv = zfs_lookup_dataset(spa, rootname, &dev->root_guid);
1678 *path = (*end == '\0') ? end : end + 1;
1679 dev->dd.d_dev = &zfs_dev;
1684 zfs_fmtdev(void *vdev)
1686 static char rootname[ZFS_MAXNAMELEN];
1687 static char buf[2 * ZFS_MAXNAMELEN + 8];
1688 struct zfs_devdesc *dev = (struct zfs_devdesc *)vdev;
1692 if (dev->dd.d_dev->dv_type != DEVT_ZFS)
1695 /* Do we have any pools? */
1696 spa = STAILQ_FIRST(&zfs_pools);
1700 if (dev->pool_guid == 0)
1701 dev->pool_guid = spa->spa_guid;
1703 spa = spa_find_by_guid(dev->pool_guid);
1706 printf("ZFS: can't find pool by guid\n");
1709 if (dev->root_guid == 0 && zfs_get_root(spa, &dev->root_guid)) {
1710 printf("ZFS: can't find root filesystem\n");
1713 if (zfs_rlookup(spa, dev->root_guid, rootname)) {
1714 printf("ZFS: can't find filesystem by guid\n");
1718 if (rootname[0] == '\0')
1719 snprintf(buf, sizeof(buf), "%s:%s:", dev->dd.d_dev->dv_name,
1722 snprintf(buf, sizeof(buf), "%s:%s/%s:", dev->dd.d_dev->dv_name,
1723 spa->spa_name, rootname);
1728 split_devname(const char *name, char *poolname, size_t size,
1729 const char **dsnamep)
1734 ASSERT(name != NULL);
1735 ASSERT(poolname != NULL);
1738 dsname = strchr(name, '/');
1739 if (dsname != NULL) {
1740 len = dsname - name;
1748 strlcpy(poolname, name, len + 1);
1750 if (dsnamep != NULL)
1757 zfs_list(const char *name)
1759 static char poolname[ZFS_MAXNAMELEN];
1765 if (split_devname(name, poolname, sizeof(poolname), &dsname) != 0)
1768 spa = spa_find_by_name(poolname);
1771 rv = zfs_lookup_dataset(spa, dsname, &objid);
1775 return (zfs_list_dataset(spa, objid));
1779 init_zfs_boot_options(const char *currdev_in)
1781 char poolname[ZFS_MAXNAMELEN];
1782 char *beroot, *currdev;
1788 currdev_len = strlen(currdev_in);
1789 if (currdev_len == 0)
1791 if (strncmp(currdev_in, "zfs:", 4) != 0)
1793 currdev = strdup(currdev_in);
1794 if (currdev == NULL)
1796 /* Remove the trailing : */
1797 currdev[currdev_len - 1] = '\0';
1799 setenv("zfs_be_active", currdev, 1);
1800 setenv("zfs_be_currpage", "1", 1);
1801 /* Remove the last element (current bootenv) */
1802 beroot = strrchr(currdev, '/');
1805 beroot = strchr(currdev, ':') + 1;
1806 setenv("zfs_be_root", beroot, 1);
1808 if (split_devname(beroot, poolname, sizeof(poolname), &dsname) != 0)
1811 spa = spa_find_by_name(poolname);
1815 zfs_bootenv_initial("bootenvs", spa, beroot, dsname, 0);
1816 zfs_checkpoints_initial(spa, beroot, dsname);
1822 zfs_checkpoints_initial(spa_t *spa, const char *name, const char *dsname)
1826 if (spa->spa_uberblock_checkpoint.ub_checkpoint_txg != 0) {
1827 snprintf(envname, sizeof(envname), "zpool_checkpoint");
1828 setenv(envname, name, 1);
1830 spa->spa_uberblock = &spa->spa_uberblock_checkpoint;
1831 spa->spa_mos = &spa->spa_mos_checkpoint;
1833 zfs_bootenv_initial("bootenvs_check", spa, name, dsname, 1);
1835 spa->spa_uberblock = &spa->spa_uberblock_master;
1836 spa->spa_mos = &spa->spa_mos_master;
1841 zfs_bootenv_initial(const char *envprefix, spa_t *spa, const char *rootname,
1842 const char *dsname, int checkpoint)
1844 char envname[32], envval[256];
1846 int bootenvs_idx, rv;
1848 SLIST_INIT(&zfs_be_head);
1851 rv = zfs_lookup_dataset(spa, dsname, &objid);
1855 rv = zfs_callback_dataset(spa, objid, zfs_belist_add);
1857 /* Populate the initial environment variables */
1858 SLIST_FOREACH_SAFE(zfs_be, &zfs_be_head, entries, zfs_be_tmp) {
1859 /* Enumerate all bootenvs for general usage */
1860 snprintf(envname, sizeof(envname), "%s[%d]",
1861 envprefix, bootenvs_idx);
1862 snprintf(envval, sizeof(envval), "zfs:%s%s/%s",
1863 checkpoint ? "!" : "", rootname, zfs_be->name);
1864 rv = setenv(envname, envval, 1);
1869 snprintf(envname, sizeof(envname), "%s_count", envprefix);
1870 snprintf(envval, sizeof(envval), "%d", bootenvs_idx);
1871 setenv(envname, envval, 1);
1873 /* Clean up the SLIST of ZFS BEs */
1874 while (!SLIST_EMPTY(&zfs_be_head)) {
1875 zfs_be = SLIST_FIRST(&zfs_be_head);
1876 SLIST_REMOVE_HEAD(&zfs_be_head, entries);
1883 zfs_bootenv(const char *name)
1885 char poolname[ZFS_MAXNAMELEN], *root;
1890 int rv, pages, perpage, currpage;
1894 if ((root = getenv("zfs_be_root")) == NULL)
1897 if (strcmp(name, root) != 0) {
1898 if (setenv("zfs_be_root", name, 1) != 0)
1902 SLIST_INIT(&zfs_be_head);
1905 if (split_devname(name, poolname, sizeof(poolname), &dsname) != 0)
1908 spa = spa_find_by_name(poolname);
1911 rv = zfs_lookup_dataset(spa, dsname, &objid);
1914 rv = zfs_callback_dataset(spa, objid, zfs_belist_add);
1916 /* Calculate and store the number of pages of BEs */
1917 perpage = (ZFS_BE_LAST - ZFS_BE_FIRST + 1);
1918 pages = (zfs_env_count / perpage) + ((zfs_env_count % perpage) > 0 ? 1 : 0);
1919 snprintf(becount, 4, "%d", pages);
1920 if (setenv("zfs_be_pages", becount, 1) != 0)
1923 /* Roll over the page counter if it has exceeded the maximum */
1924 currpage = strtol(getenv("zfs_be_currpage"), NULL, 10);
1925 if (currpage > pages) {
1926 if (setenv("zfs_be_currpage", "1", 1) != 0)
1930 /* Populate the menu environment variables */
1933 /* Clean up the SLIST of ZFS BEs */
1934 while (!SLIST_EMPTY(&zfs_be_head)) {
1935 zfs_be = SLIST_FIRST(&zfs_be_head);
1936 SLIST_REMOVE_HEAD(&zfs_be_head, entries);
1945 zfs_belist_add(const char *name, uint64_t value __unused)
1948 /* Skip special datasets that start with a $ character */
1949 if (strncmp(name, "$", 1) == 0) {
1952 /* Add the boot environment to the head of the SLIST */
1953 zfs_be = malloc(sizeof(struct zfs_be_entry));
1954 if (zfs_be == NULL) {
1957 zfs_be->name = strdup(name);
1958 if (zfs_be->name == NULL) {
1962 SLIST_INSERT_HEAD(&zfs_be_head, zfs_be, entries);
1971 char envname[32], envval[256];
1972 char *beroot, *pagenum;
1975 beroot = getenv("zfs_be_root");
1976 if (beroot == NULL) {
1980 pagenum = getenv("zfs_be_currpage");
1981 if (pagenum != NULL) {
1982 page = strtol(pagenum, NULL, 10);
1989 zfs_env_index = ZFS_BE_FIRST;
1990 SLIST_FOREACH_SAFE(zfs_be, &zfs_be_head, entries, zfs_be_tmp) {
1991 /* Skip to the requested page number */
1992 if (ctr <= ((ZFS_BE_LAST - ZFS_BE_FIRST + 1) * (page - 1))) {
1997 snprintf(envname, sizeof(envname), "bootenvmenu_caption[%d]", zfs_env_index);
1998 snprintf(envval, sizeof(envval), "%s", zfs_be->name);
1999 rv = setenv(envname, envval, 1);
2004 snprintf(envname, sizeof(envname), "bootenvansi_caption[%d]", zfs_env_index);
2005 rv = setenv(envname, envval, 1);
2010 snprintf(envname, sizeof(envname), "bootenvmenu_command[%d]", zfs_env_index);
2011 rv = setenv(envname, "set_bootenv", 1);
2016 snprintf(envname, sizeof(envname), "bootenv_root[%d]", zfs_env_index);
2017 snprintf(envval, sizeof(envval), "zfs:%s/%s", beroot, zfs_be->name);
2018 rv = setenv(envname, envval, 1);
2024 if (zfs_env_index > ZFS_BE_LAST) {
2030 for (; zfs_env_index <= ZFS_BE_LAST; zfs_env_index++) {
2031 snprintf(envname, sizeof(envname), "bootenvmenu_caption[%d]", zfs_env_index);
2032 (void)unsetenv(envname);
2033 snprintf(envname, sizeof(envname), "bootenvansi_caption[%d]", zfs_env_index);
2034 (void)unsetenv(envname);
2035 snprintf(envname, sizeof(envname), "bootenvmenu_command[%d]", zfs_env_index);
2036 (void)unsetenv(envname);
2037 snprintf(envname, sizeof(envname), "bootenv_root[%d]", zfs_env_index);
2038 (void)unsetenv(envname);