git: 15f0b8c309de - main - zfs: merge openzfs/zfs@9cd71c860 (master)
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Wed, 25 Jan 2023 19:17:31 UTC
The branch main has been updated by mm:
URL: https://cgit.FreeBSD.org/src/commit/?id=15f0b8c309dea1dcb14d3e374686576ff68ac43f
commit 15f0b8c309dea1dcb14d3e374686576ff68ac43f
Merge: da52fc464a4c 9cd71c8604d5
Author: Martin Matuska <mm@FreeBSD.org>
AuthorDate: 2023-01-25 18:50:29 +0000
Commit: Martin Matuska <mm@FreeBSD.org>
CommitDate: 2023-01-25 18:50:29 +0000
zfs: merge openzfs/zfs@9cd71c860 (master)
Notable upstream pull request merges:
#13805 Configure zed's diagnosis engine with vdev properties
#14110 zfs list: Allow more fields in ZFS_ITER_SIMPLE mode
#14121 Batch enqueue/dequeue for bqueue
#14123 arc_read()/arc_access() refactoring and cleanup
#14159 Bypass metaslab throttle for removal allocations
#14243 Implement uncached prefetch
#14251 Cache dbuf_hash() calculation
#14253 Allow reciever to override encryption property in case of replication
#14254 Restrict visibility of per-dataset kstats inside FreeBSD jails
#14255 Zero end of embedded block buffer in dump_write_embedded()
#14263 Cleanups identified by CodeQL and Coverity
#14264 Miscellaneous fixes
#14272 Change ZEVENT_POOL_GUID to ZEVENT_POOL to display pool names
#14287 FreeBSD: Remove stray debug printf
#14288 Colorize zfs diff output
#14289 deadlock between spa_errlog_lock and dp_config_rwlock
#14291 FreeBSD: Fix potential boot panic with bad label
#14292 Add tunable to allow changing micro ZAP's max size
#14293 Turn default_bs and default_ibs into ZFS_MODULE_PARAMs
#14295 zed: add hotplug support for spare vdevs
#14304 Activate filesystem features only in syncing context
#14311 zpool: do guid-based comparison in is_vdev_cb()
#14317 Pack zrlock_t by 8 bytes
#14320 Update arc_summary and arcstat outputs
#14328 FreeBSD: catch up to 1400077
#14376 Use setproctitle to report progress of zfs send
#14340 Remove some dead ARC code
#14358 Wait for txg sync if the last DRR_FREEOBJECTS might result in a hole
#14360 libzpool: fix ddi_strtoull to update nptr
#14364 Fix unprotected zfs_znode_dmu_fini
#14379 zfs_receive_one: Check for the more likely error first
#14380 Cleanup of dead code suggested by Clang Static Analyzer
#14397 Avoid passing an uninitialized index to dsl_prop_known_index
#14404 Fix reading uninitialized variable in receive_read
#14407 free_blocks(): Fix reports from 2016 PVS Studio FreeBSD report
#14418 Introduce minimal ZIL block commit delay
#14422 x86 assembly: fix .size placement and replace .align with .balign
Obtained from: OpenZFS
OpenZFS commit: 9cd71c8604d52def22ffaddc35755712f0fb9349
lib/libbe/be.c | 8 +-
lib/libbe/be_access.c | 6 +-
lib/libbe/be_info.c | 4 +-
.../.github/workflows/build-dependencies.txt | 6 +
.../.github/workflows/zfs-tests-functional.yml | 17 +-
.../openzfs/.github/workflows/zfs-tests-sanity.yml | 17 +-
sys/contrib/openzfs/CODE_OF_CONDUCT.md | 2 +-
sys/contrib/openzfs/META | 2 +-
sys/contrib/openzfs/cmd/arc_summary | 173 +++--
sys/contrib/openzfs/cmd/arcstat.in | 119 ++-
sys/contrib/openzfs/cmd/zdb/zdb.c | 44 +-
sys/contrib/openzfs/cmd/zed/agents/zfs_agents.c | 2 +-
sys/contrib/openzfs/cmd/zed/agents/zfs_diagnosis.c | 40 +-
sys/contrib/openzfs/cmd/zed/agents/zfs_mod.c | 21 +-
sys/contrib/openzfs/cmd/zed/agents/zfs_retire.c | 89 ++-
.../openzfs/cmd/zed/zed.d/statechange-notify.sh | 4 +-
sys/contrib/openzfs/cmd/zfs/zfs_iter.c | 65 +-
sys/contrib/openzfs/cmd/zfs/zfs_iter.h | 12 +-
sys/contrib/openzfs/cmd/zfs/zfs_main.c | 65 +-
sys/contrib/openzfs/cmd/zhack.c | 3 +-
sys/contrib/openzfs/cmd/zpool/zpool_main.c | 96 +--
.../openzfs/cmd/zstream/zstream_decompress.c | 22 +-
.../openzfs/cmd/zstream/zstream_recompress.c | 22 +-
sys/contrib/openzfs/cmd/zstream/zstream_redup.c | 22 +-
sys/contrib/openzfs/cmd/ztest.c | 48 +-
sys/contrib/openzfs/config/ax_python_devel.m4 | 71 +-
sys/contrib/openzfs/config/deb.am | 28 +-
sys/contrib/openzfs/config/kernel-acl.m4 | 50 +-
sys/contrib/openzfs/config/kernel-tmpfile.m4 | 32 +-
sys/contrib/openzfs/config/kernel.m4 | 32 +
sys/contrib/openzfs/config/zfs-build.m4 | 1 +
sys/contrib/openzfs/configure.ac | 2 +
sys/contrib/openzfs/contrib/debian/.gitignore | 1 +
sys/contrib/openzfs/contrib/debian/control | 3 +-
.../contrib/debian/openzfs-libpam-zfs.postinst | 4 +-
.../contrib/debian/openzfs-zfs-zed.postinst | 7 -
.../openzfs/contrib/debian/openzfs-zfs-zed.prerm | 16 -
.../contrib/debian/openzfs-zfsutils.install | 1 -
.../contrib/debian/openzfs-zfsutils.postinst | 15 -
.../openzfs/contrib/debian/{rules => rules.in} | 4 +-
.../contrib/dracut/90zfs/zfs-load-key.sh.in | 2 +-
sys/contrib/openzfs/contrib/initramfs/scripts/zfs | 22 +-
.../openzfs/contrib/pam_zfs_key/pam_zfs_key.c | 2 +-
.../system-generators/zfs-mount-generator.c | 9 +-
.../openzfs/etc/systemd/system/zfs-zed.service.in | 2 +-
sys/contrib/openzfs/include/Makefile.am | 1 +
sys/contrib/openzfs/include/libzfs.h | 25 +-
sys/contrib/openzfs/include/libzutil.h | 9 +
sys/contrib/openzfs/include/os/freebsd/Makefile.am | 3 +
.../include/os/freebsd/spl/sys/ia32/asm_linkage.h | 178 +++++
sys/contrib/openzfs/include/os/linux/Makefile.am | 4 +
.../include/os/linux/kernel/linux/blkdev_compat.h | 4 +
.../include/os/linux/kernel/linux/dcache_compat.h | 8 +-
.../include/os/linux/kernel/linux/simd_powerpc.h | 15 +
.../include/os/linux/spl/sys/ia32/asm_linkage.h | 212 ++++++
.../openzfs/include/os/linux/spl/sys/kmem.h | 3 +
.../openzfs/include/os/linux/spl/sys/kmem_cache.h | 3 -
.../openzfs/include/os/linux/zfs/sys/trace_arc.h | 6 +-
sys/contrib/openzfs/include/os/linux/zfs/sys/zpl.h | 5 +-
sys/contrib/openzfs/include/sys/arc.h | 8 +-
sys/contrib/openzfs/include/sys/arc_impl.h | 63 +-
sys/contrib/openzfs/include/sys/asm_linkage.h | 48 ++
sys/contrib/openzfs/include/sys/bqueue.h | 7 +-
sys/contrib/openzfs/include/sys/dbuf.h | 9 +-
sys/contrib/openzfs/include/sys/dnode.h | 8 +-
sys/contrib/openzfs/include/sys/dsl_dir.h | 2 +-
sys/contrib/openzfs/include/sys/fm/fs/zfs.h | 4 +
sys/contrib/openzfs/include/sys/fs/zfs.h | 5 +
sys/contrib/openzfs/include/sys/spa.h | 2 +-
sys/contrib/openzfs/include/sys/vdev_impl.h | 8 +
sys/contrib/openzfs/include/sys/zfs_znode.h | 2 +
sys/contrib/openzfs/include/sys/zrlock.h | 3 +-
sys/contrib/openzfs/lib/libicp/Makefile.am | 1 +
sys/contrib/openzfs/lib/libspl/include/Makefile.am | 10 +
.../include/os/freebsd/sys/ia32/asm_linkage.h | 184 +++++
.../include/os/linux}/sys/ia32/asm_linkage.h | 31 +-
.../icp => lib/libspl}/include/sys/asm_linkage.h | 0
sys/contrib/openzfs/lib/libzfs/libzfs.abi | 27 +-
sys/contrib/openzfs/lib/libzfs/libzfs_changelist.c | 6 +-
sys/contrib/openzfs/lib/libzfs/libzfs_crypto.c | 2 +-
sys/contrib/openzfs/lib/libzfs/libzfs_dataset.c | 72 +-
sys/contrib/openzfs/lib/libzfs/libzfs_diff.c | 40 +
sys/contrib/openzfs/lib/libzfs/libzfs_iter.c | 53 +-
sys/contrib/openzfs/lib/libzfs/libzfs_mount.c | 4 +-
sys/contrib/openzfs/lib/libzfs/libzfs_pool.c | 62 +-
sys/contrib/openzfs/lib/libzfs/libzfs_sendrecv.c | 114 ++-
sys/contrib/openzfs/lib/libzfs/libzfs_status.c | 2 +-
sys/contrib/openzfs/lib/libzfs/libzfs_util.c | 21 +-
sys/contrib/openzfs/lib/libzpool/kernel.c | 6 +-
sys/contrib/openzfs/lib/libzutil/Makefile.am | 1 +
.../lib/libzutil/os/linux/zutil_setproctitle.c | 299 ++++++++
sys/contrib/openzfs/man/man1/arcstat.1 | 82 +-
sys/contrib/openzfs/man/man4/zfs.4 | 26 +-
sys/contrib/openzfs/man/man7/vdevprops.7 | 17 +-
sys/contrib/openzfs/man/man8/zfs-send.8 | 24 +-
sys/contrib/openzfs/man/man8/zfs.8 | 4 +
sys/contrib/openzfs/module/Kbuild.in | 4 +-
sys/contrib/openzfs/module/icp/algs/aes/aes_impl.c | 2 +-
.../openzfs/module/icp/algs/aes/aes_impl_aesni.c | 9 +-
.../openzfs/module/icp/algs/blake3/blake3_impl.h | 1 +
.../openzfs/module/icp/algs/blake3/blake3_x86-64.c | 20 +-
sys/contrib/openzfs/module/icp/algs/modes/ccm.c | 2 +-
sys/contrib/openzfs/module/icp/algs/modes/gcm.c | 20 +-
.../openzfs/module/icp/algs/modes/gcm_pclmulqdq.c | 3 +-
sys/contrib/openzfs/module/icp/algs/sha2/sha2.c | 5 +-
sys/contrib/openzfs/module/icp/api/kcf_ctxops.c | 2 +-
.../openzfs/module/icp/asm-x86_64/aes/aes_aesni.S | 16 +-
.../openzfs/module/icp/asm-x86_64/aes/aes_amd64.S | 60 +-
.../module/icp/asm-x86_64/blake3/blake3_avx2.S | 12 +-
.../module/icp/asm-x86_64/blake3/blake3_avx512.S | 30 +-
.../module/icp/asm-x86_64/blake3/blake3_sse2.S | 32 +-
.../module/icp/asm-x86_64/blake3/blake3_sse41.S | 31 +-
.../module/icp/asm-x86_64/modes/aesni-gcm-x86_64.S | 86 +--
.../module/icp/asm-x86_64/modes/gcm_pclmulqdq.S | 2 +-
.../module/icp/asm-x86_64/modes/ghash-x86_64.S | 58 +-
.../module/icp/asm-x86_64/sha2/sha256_impl.S | 8 +-
.../module/icp/asm-x86_64/sha2/sha512_impl.S | 11 +-
sys/contrib/openzfs/module/icp/core/kcf_callprov.c | 2 +-
.../openzfs/module/icp/include/aes/aes_impl.h | 9 +-
.../openzfs/module/icp/include/sys/ia32/stack.h | 160 ----
.../openzfs/module/icp/include/sys/ia32/trap.h | 107 ---
sys/contrib/openzfs/module/lua/ldo.c | 6 +-
sys/contrib/openzfs/module/lua/lfunc.h | 4 +-
sys/contrib/openzfs/module/lua/lgc.c | 2 +-
sys/contrib/openzfs/module/lua/lgc.h | 2 +-
sys/contrib/openzfs/module/lua/lobject.h | 4 +-
sys/contrib/openzfs/module/lua/lvm.c | 2 +-
.../openzfs/module/lua/setjmp/setjmp_aarch64.S | 2 +-
sys/contrib/openzfs/module/lua/setjmp/setjmp_arm.S | 2 +-
.../openzfs/module/lua/setjmp/setjmp_i386.S | 2 +-
sys/contrib/openzfs/module/lua/setjmp/setjmp_ppc.S | 4 +-
.../openzfs/module/lua/setjmp/setjmp_sparc64.S | 2 +-
.../openzfs/module/lua/setjmp/setjmp_x86_64.S | 22 +-
sys/contrib/openzfs/module/os/freebsd/spl/callb.c | 4 +-
.../openzfs/module/os/freebsd/spl/spl_kstat.c | 89 ++-
.../openzfs/module/os/freebsd/zfs/kmod_core.c | 7 +-
sys/contrib/openzfs/module/os/freebsd/zfs/spa_os.c | 2 +
.../openzfs/module/os/freebsd/zfs/sysctl_os.c | 31 +-
.../openzfs/module/os/freebsd/zfs/zfs_acl.c | 4 +-
.../openzfs/module/os/freebsd/zfs/zfs_debug.c | 4 +-
.../openzfs/module/os/freebsd/zfs/zfs_dir.c | 7 +
.../openzfs/module/os/freebsd/zfs/zfs_znode.c | 4 +-
.../openzfs/module/os/freebsd/zfs/zio_crypt.c | 3 +-
.../openzfs/module/os/linux/spl/spl-kmem-cache.c | 10 +-
.../openzfs/module/os/linux/spl/spl-thread.c | 5 +-
sys/contrib/openzfs/module/os/linux/spl/spl-zone.c | 2 +-
.../openzfs/module/os/linux/zfs/vdev_disk.c | 13 +-
sys/contrib/openzfs/module/os/linux/zfs/zfs_acl.c | 6 +-
.../openzfs/module/os/linux/zfs/zfs_debug.c | 6 +-
sys/contrib/openzfs/module/os/linux/zfs/zfs_dir.c | 13 +-
.../openzfs/module/os/linux/zfs/zfs_vnops_os.c | 6 +-
.../openzfs/module/os/linux/zfs/zfs_znode.c | 9 +-
.../openzfs/module/os/linux/zfs/zpl_inode.c | 27 +
.../openzfs/module/os/linux/zfs/zpl_xattr.c | 11 +-
sys/contrib/openzfs/module/unicode/u8_textprep.c | 21 +-
sys/contrib/openzfs/module/zcommon/zfs_fletcher.c | 4 +-
sys/contrib/openzfs/module/zcommon/zpool_prop.c | 12 +
sys/contrib/openzfs/module/zfs/arc.c | 848 +++++++++++----------
sys/contrib/openzfs/module/zfs/blake3_zfs.c | 4 +-
sys/contrib/openzfs/module/zfs/bqueue.c | 117 +--
sys/contrib/openzfs/module/zfs/dbuf.c | 136 ++--
sys/contrib/openzfs/module/zfs/dmu.c | 18 +-
sys/contrib/openzfs/module/zfs/dmu_objset.c | 9 +-
sys/contrib/openzfs/module/zfs/dmu_recv.c | 47 +-
sys/contrib/openzfs/module/zfs/dmu_send.c | 15 +-
sys/contrib/openzfs/module/zfs/dmu_traverse.c | 3 +-
sys/contrib/openzfs/module/zfs/dmu_tx.c | 10 +-
sys/contrib/openzfs/module/zfs/dmu_zfetch.c | 6 +-
sys/contrib/openzfs/module/zfs/dnode.c | 5 +
sys/contrib/openzfs/module/zfs/dnode_sync.c | 31 +-
sys/contrib/openzfs/module/zfs/dsl_dataset.c | 23 +-
sys/contrib/openzfs/module/zfs/dsl_dir.c | 5 +-
sys/contrib/openzfs/module/zfs/dsl_prop.c | 2 +-
sys/contrib/openzfs/module/zfs/dsl_scan.c | 10 +-
sys/contrib/openzfs/module/zfs/fm.c | 3 +-
sys/contrib/openzfs/module/zfs/metaslab.c | 13 +-
sys/contrib/openzfs/module/zfs/spa.c | 2 +-
sys/contrib/openzfs/module/zfs/spa_config.c | 2 +
sys/contrib/openzfs/module/zfs/spa_errlog.c | 267 +++----
sys/contrib/openzfs/module/zfs/vdev.c | 137 +++-
sys/contrib/openzfs/module/zfs/vdev_indirect.c | 2 +-
.../zfs/vdev_raidz_math_powerpc_altivec_common.h | 44 +-
sys/contrib/openzfs/module/zfs/vdev_removal.c | 6 +-
sys/contrib/openzfs/module/zfs/zap.c | 4 +-
sys/contrib/openzfs/module/zfs/zap_micro.c | 8 +-
sys/contrib/openzfs/module/zfs/zcp.c | 10 +-
sys/contrib/openzfs/module/zfs/zfs_chksum.c | 2 +-
sys/contrib/openzfs/module/zfs/zfs_fm.c | 79 ++
sys/contrib/openzfs/module/zfs/zfs_fuid.c | 2 +-
sys/contrib/openzfs/module/zfs/zfs_ioctl.c | 24 +-
sys/contrib/openzfs/module/zfs/zfs_vnops.c | 2 +-
sys/contrib/openzfs/module/zfs/zil.c | 36 +-
sys/contrib/openzfs/module/zfs/zio.c | 2 +-
sys/contrib/openzfs/module/zfs/zvol.c | 4 +-
.../module/zstd/include/zstd_compat_wrapper.h | 2 -
.../module/zstd/lib/common/fse_decompress.c | 11 -
sys/contrib/openzfs/module/zstd/zfs_zstd.c | 4 +-
sys/contrib/openzfs/scripts/debian-packaging.sh | 36 -
sys/contrib/openzfs/tests/runfiles/common.run | 1 +
sys/contrib/openzfs/tests/runfiles/linux.run | 2 +-
sys/contrib/openzfs/tests/runfiles/sanity.run | 3 +
.../openzfs/tests/test-runner/bin/zts-report.py.in | 10 +-
.../openzfs/tests/zfs-tests/cmd/mmapwrite.c | 12 +-
.../openzfs/tests/zfs-tests/tests/Makefile.am | 12 +-
.../tests/functional/acl/posix/posix_004_pos.ksh | 3 +
.../zfs_property/zfs_written_property_001_pos.ksh | 1 +
.../zfs_receive/zfs_receive_to_encrypted.ksh | 14 +
.../cli_root/zfs_send/zfs_send_006_pos.ksh | 32 +-
.../tests/functional/events/zed_cksum_config.ksh | 158 ++++
.../tests/functional/events/zed_io_config.ksh | 150 ++++
.../tests/functional/l2arc/l2arc_mfuonly_pos.ksh | 2 +-
.../tests/functional/renameat2/Makefile.am | 7 -
.../rsend/send_encrypted_freeobjects.ksh | 87 +++
.../rsend/send_encrypted_incremental.ksh | 93 +++
.../functional/rsend/send_raw_large_blocks.ksh | 2 +-
.../tests/functional/trim/autotrim_config.ksh | 1 +
.../tests/functional/trim/trim_config.ksh | 1 +
.../functional/zvol/zvol_misc/zvol_misc_trim.ksh | 1 +
sys/modules/zfs/zfs_config.h | 17 +-
sys/modules/zfs/zfs_gitrev.h | 2 +-
220 files changed, 4467 insertions(+), 2127 deletions(-)
diff --cc lib/libbe/be.c
index 13f7a59d5215,000000000000..cc6b8533a8f9
mode 100644,000000..100644
--- a/lib/libbe/be.c
+++ b/lib/libbe/be.c
@@@ -1,1309 -1,0 +1,1309 @@@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+ *
+ * Copyright (c) 2017 Kyle J. Kneitinger <kyle@kneit.in>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/param.h>
+#include <sys/mount.h>
+#include <sys/stat.h>
+#include <sys/ucred.h>
+#include <sys/queue.h>
+#include <sys/zfs_context.h>
+#include <sys/mntent.h>
+#include <sys/zfs_ioctl.h>
+
+#include <libzutil.h>
+#include <ctype.h>
+#include <libgen.h>
+#include <libzfs_core.h>
+#include <libzfs_impl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+#include <libzfsbootenv.h>
+
+#include "be.h"
+#include "be_impl.h"
+
+struct promote_entry {
+ char name[BE_MAXPATHLEN];
+ SLIST_ENTRY(promote_entry) link;
+};
+
+struct be_destroy_data {
+ libbe_handle_t *lbh;
+ char target_name[BE_MAXPATHLEN];
+ char *snapname;
+ SLIST_HEAD(, promote_entry) promotelist;
+};
+
+#if SOON
+static int be_create_child_noent(libbe_handle_t *lbh, const char *active,
+ const char *child_path);
+static int be_create_child_cloned(libbe_handle_t *lbh, const char *active);
+#endif
+
+/* Arbitrary... should tune */
+#define BE_SNAP_SERIAL_MAX 1024
+
+/*
+ * Iterator function for locating the rootfs amongst the children of the
+ * zfs_be_root set by loader(8). data is expected to be a libbe_handle_t *.
+ */
+static int
+be_locate_rootfs(libbe_handle_t *lbh)
+{
+ struct statfs sfs;
+ struct mnttab entry;
+ zfs_handle_t *zfs;
+
+ /*
+ * Check first if root is ZFS; if not, we'll bail on rootfs capture.
+ * Unfortunately needed because zfs_path_to_zhandle will emit to
+ * stderr if / isn't actually a ZFS filesystem, which we'd like
+ * to avoid.
+ */
+ if (statfs("/", &sfs) == 0) {
+ statfs2mnttab(&sfs, &entry);
+ if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
+ return (1);
+ } else
+ return (1);
+ zfs = zfs_path_to_zhandle(lbh->lzh, "/", ZFS_TYPE_FILESYSTEM);
+ if (zfs == NULL)
+ return (1);
+
+ strlcpy(lbh->rootfs, zfs_get_name(zfs), sizeof(lbh->rootfs));
+ zfs_close(zfs);
+ return (0);
+}
+
+/*
+ * Initializes the libbe context to operate in the root boot environment
+ * dataset, for example, zroot/ROOT.
+ */
+libbe_handle_t *
+libbe_init(const char *root)
+{
+ char altroot[MAXPATHLEN];
+ libbe_handle_t *lbh;
+ char *poolname, *pos;
+ int pnamelen;
+
+ lbh = NULL;
+ poolname = pos = NULL;
+
+ if ((lbh = calloc(1, sizeof(libbe_handle_t))) == NULL)
+ goto err;
+
+ if ((lbh->lzh = libzfs_init()) == NULL)
+ goto err;
+
+ /*
+ * Grab rootfs, we'll work backwards from there if an optional BE root
+ * has not been passed in.
+ */
+ if (be_locate_rootfs(lbh) != 0) {
+ if (root == NULL)
+ goto err;
+ *lbh->rootfs = '\0';
+ }
+ if (root == NULL) {
+ /* Strip off the final slash from rootfs to get the be root */
+ strlcpy(lbh->root, lbh->rootfs, sizeof(lbh->root));
+ pos = strrchr(lbh->root, '/');
+ if (pos == NULL)
+ goto err;
+ *pos = '\0';
+ } else
+ strlcpy(lbh->root, root, sizeof(lbh->root));
+
+ if ((pos = strchr(lbh->root, '/')) == NULL)
+ goto err;
+
+ pnamelen = pos - lbh->root;
+ poolname = malloc(pnamelen + 1);
+ if (poolname == NULL)
+ goto err;
+
+ strlcpy(poolname, lbh->root, pnamelen + 1);
+ if ((lbh->active_phandle = zpool_open(lbh->lzh, poolname)) == NULL)
+ goto err;
+ free(poolname);
+ poolname = NULL;
+
+ if (zpool_get_prop(lbh->active_phandle, ZPOOL_PROP_BOOTFS, lbh->bootfs,
+ sizeof(lbh->bootfs), NULL, true) != 0)
+ goto err;
+
+ if (zpool_get_prop(lbh->active_phandle, ZPOOL_PROP_ALTROOT,
+ altroot, sizeof(altroot), NULL, true) == 0 &&
+ strcmp(altroot, "-") != 0)
+ lbh->altroot_len = strlen(altroot);
+
+ return (lbh);
+err:
+ if (lbh != NULL) {
+ if (lbh->active_phandle != NULL)
+ zpool_close(lbh->active_phandle);
+ if (lbh->lzh != NULL)
+ libzfs_fini(lbh->lzh);
+ free(lbh);
+ }
+ free(poolname);
+ return (NULL);
+}
+
+
+/*
+ * Free memory allocated by libbe_init()
+ */
+void
+libbe_close(libbe_handle_t *lbh)
+{
+
+ if (lbh->active_phandle != NULL)
+ zpool_close(lbh->active_phandle);
+ libzfs_fini(lbh->lzh);
+ free(lbh);
+}
+
+/*
+ * Proxy through to libzfs for the moment.
+ */
+void
+be_nicenum(uint64_t num, char *buf, size_t buflen)
+{
+
+ zfs_nicenum(num, buf, buflen);
+}
+
+static bool
+be_should_promote_clones(zfs_handle_t *zfs_hdl, struct be_destroy_data *bdd)
+{
+ char *atpos;
+
+ if (zfs_get_type(zfs_hdl) != ZFS_TYPE_SNAPSHOT)
+ return (false);
+
+ /*
+ * If we're deleting a snapshot, we need to make sure we only promote
+ * clones that are derived from one of the snapshots we're deleting,
+ * rather than that of a snapshot we're not touching. This keeps stuff
+ * in a consistent state, making sure that we don't error out unless
+ * we really need to.
+ */
+ if (bdd->snapname == NULL)
+ return (true);
+
+ atpos = strchr(zfs_get_name(zfs_hdl), '@');
+ return (strcmp(atpos + 1, bdd->snapname) == 0);
+}
+
+/*
+ * This is executed from be_promote_dependent_clones via zfs_iter_dependents,
+ * It checks if the dependent type is a snapshot then attempts to find any
+ * clones associated with it. Any clones not related to the destroy target are
+ * added to the promote list.
+ */
+static int
+be_dependent_clone_cb(zfs_handle_t *zfs_hdl, void *data)
+{
+ int err;
+ bool found;
+ char *name;
+ struct nvlist *nvl;
+ struct nvpair *nvp;
+ struct be_destroy_data *bdd;
+ struct promote_entry *entry, *newentry;
+
+ nvp = NULL;
+ err = 0;
+ bdd = (struct be_destroy_data *)data;
+
+ if (be_should_promote_clones(zfs_hdl, bdd) &&
+ (nvl = zfs_get_clones_nvl(zfs_hdl)) != NULL) {
+ while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
+ name = nvpair_name(nvp);
+
+ /*
+ * Skip if the clone is equal to, or a child of, the
+ * destroy target.
+ */
+ if (strncmp(name, bdd->target_name,
+ strlen(bdd->target_name)) == 0 ||
+ strstr(name, bdd->target_name) == name) {
+ continue;
+ }
+
+ found = false;
+ SLIST_FOREACH(entry, &bdd->promotelist, link) {
+ if (strcmp(entry->name, name) == 0) {
+ found = true;
+ break;
+ }
+ }
+
+ if (found)
+ continue;
+
+ newentry = malloc(sizeof(struct promote_entry));
+ if (newentry == NULL) {
+ err = ENOMEM;
+ break;
+ }
+
+#define BE_COPY_NAME(entry, src) \
+ strlcpy((entry)->name, (src), sizeof((entry)->name))
+ if (BE_COPY_NAME(newentry, name) >=
+ sizeof(newentry->name)) {
+ /* Shouldn't happen. */
+ free(newentry);
+ err = ENAMETOOLONG;
+ break;
+ }
+#undef BE_COPY_NAME
+
+ /*
+ * We're building up a SLIST here to make sure both that
+ * we get the order right and so that we don't
+ * inadvertently observe the wrong state by promoting
+ * datasets while we're still walking the tree. The
+ * latter can lead to situations where we promote a BE
+ * then effectively demote it again.
+ */
+ SLIST_INSERT_HEAD(&bdd->promotelist, newentry, link);
+ }
+ nvlist_free(nvl);
+ }
+ zfs_close(zfs_hdl);
+ return (err);
+}
+
+/*
+ * This is called before a destroy, so that any datasets(environments) that are
+ * dependent on this one get promoted before destroying the target.
+ */
+static int
+be_promote_dependent_clones(zfs_handle_t *zfs_hdl, struct be_destroy_data *bdd)
+{
+ int err;
+ zfs_handle_t *clone;
+ struct promote_entry *entry;
+
+ snprintf(bdd->target_name, BE_MAXPATHLEN, "%s/", zfs_get_name(zfs_hdl));
- err = zfs_iter_dependents(zfs_hdl, true, be_dependent_clone_cb, bdd);
++ err = zfs_iter_dependents(zfs_hdl, 0, true, be_dependent_clone_cb, bdd);
+
+ /*
+ * Drain the list and walk away from it if we're only deleting a
+ * snapshot.
+ */
+ if (bdd->snapname != NULL && !SLIST_EMPTY(&bdd->promotelist))
+ err = BE_ERR_HASCLONES;
+ while (!SLIST_EMPTY(&bdd->promotelist)) {
+ entry = SLIST_FIRST(&bdd->promotelist);
+ SLIST_REMOVE_HEAD(&bdd->promotelist, link);
+
+#define ZFS_GRAB_CLONE() \
+ zfs_open(bdd->lbh->lzh, entry->name, ZFS_TYPE_FILESYSTEM)
+ /*
+ * Just skip this part on error, we still want to clean up the
+ * promotion list after the first error. We'll then preserve it
+ * all the way back.
+ */
+ if (err == 0 && (clone = ZFS_GRAB_CLONE()) != NULL) {
+ err = zfs_promote(clone);
+ if (err != 0)
+ err = BE_ERR_DESTROYMNT;
+ zfs_close(clone);
+ }
+#undef ZFS_GRAB_CLONE
+ free(entry);
+ }
+
+ return (err);
+}
+
+static int
+be_destroy_cb(zfs_handle_t *zfs_hdl, void *data)
+{
+ char path[BE_MAXPATHLEN];
+ struct be_destroy_data *bdd;
+ zfs_handle_t *snap;
+ int err;
+
+ bdd = (struct be_destroy_data *)data;
+ if (bdd->snapname == NULL) {
- err = zfs_iter_children(zfs_hdl, be_destroy_cb, data);
++ err = zfs_iter_children(zfs_hdl, 0, be_destroy_cb, data);
+ if (err != 0)
+ return (err);
+ return (zfs_destroy(zfs_hdl, false));
+ }
+ /* If we're dealing with snapshots instead, delete that one alone */
- err = zfs_iter_filesystems(zfs_hdl, be_destroy_cb, data);
++ err = zfs_iter_filesystems(zfs_hdl, 0, be_destroy_cb, data);
+ if (err != 0)
+ return (err);
+ /*
+ * This part is intentionally glossing over any potential errors,
+ * because there's a lot less potential for errors when we're cleaning
+ * up snapshots rather than a full deep BE. The primary error case
+ * here being if the snapshot doesn't exist in the first place, which
+ * the caller will likely deem insignificant as long as it doesn't
+ * exist after the call. Thus, such a missing snapshot shouldn't jam
+ * up the destruction.
+ */
+ snprintf(path, sizeof(path), "%s@%s", zfs_get_name(zfs_hdl),
+ bdd->snapname);
+ if (!zfs_dataset_exists(bdd->lbh->lzh, path, ZFS_TYPE_SNAPSHOT))
+ return (0);
+ snap = zfs_open(bdd->lbh->lzh, path, ZFS_TYPE_SNAPSHOT);
+ if (snap != NULL)
+ zfs_destroy(snap, false);
+ return (0);
+}
+
+#define BE_DESTROY_WANTORIGIN (BE_DESTROY_ORIGIN | BE_DESTROY_AUTOORIGIN)
+/*
+ * Destroy the boot environment or snapshot specified by the name
+ * parameter. Options are or'd together with the possible values:
+ * BE_DESTROY_FORCE : forces operation on mounted datasets
+ * BE_DESTROY_ORIGIN: destroy the origin snapshot as well
+ */
+static int
+be_destroy_internal(libbe_handle_t *lbh, const char *name, int options,
+ bool odestroyer)
+{
+ struct be_destroy_data bdd;
+ char origin[BE_MAXPATHLEN], path[BE_MAXPATHLEN];
+ zfs_handle_t *fs;
+ char *snapdelim;
+ int err, force, mounted;
+ size_t rootlen;
+
+ bdd.lbh = lbh;
+ bdd.snapname = NULL;
+ SLIST_INIT(&bdd.promotelist);
+ force = options & BE_DESTROY_FORCE;
+ *origin = '\0';
+
+ be_root_concat(lbh, name, path);
+
+ if ((snapdelim = strchr(path, '@')) == NULL) {
+ if (!zfs_dataset_exists(lbh->lzh, path, ZFS_TYPE_FILESYSTEM))
+ return (set_error(lbh, BE_ERR_NOENT));
+
+ if (strcmp(path, lbh->rootfs) == 0 ||
+ strcmp(path, lbh->bootfs) == 0)
+ return (set_error(lbh, BE_ERR_DESTROYACT));
+
+ fs = zfs_open(lbh->lzh, path, ZFS_TYPE_FILESYSTEM);
+ if (fs == NULL)
+ return (set_error(lbh, BE_ERR_ZFSOPEN));
+
+ /* Don't destroy a mounted dataset unless force is specified */
+ if ((mounted = zfs_is_mounted(fs, NULL)) != 0) {
+ if (force) {
+ zfs_unmount(fs, NULL, 0);
+ } else {
+ free(bdd.snapname);
+ return (set_error(lbh, BE_ERR_DESTROYMNT));
+ }
+ }
+ } else {
+ /*
+ * If we're initially destroying a snapshot, origin options do
+ * not make sense. If we're destroying the origin snapshot of
+ * a BE, we want to maintain the options in case we need to
+ * fake success after failing to promote.
+ */
+ if (!odestroyer)
+ options &= ~BE_DESTROY_WANTORIGIN;
+ if (!zfs_dataset_exists(lbh->lzh, path, ZFS_TYPE_SNAPSHOT))
+ return (set_error(lbh, BE_ERR_NOENT));
+
+ bdd.snapname = strdup(snapdelim + 1);
+ if (bdd.snapname == NULL)
+ return (set_error(lbh, BE_ERR_NOMEM));
+ *snapdelim = '\0';
+ fs = zfs_open(lbh->lzh, path, ZFS_TYPE_DATASET);
+ if (fs == NULL) {
+ free(bdd.snapname);
+ return (set_error(lbh, BE_ERR_ZFSOPEN));
+ }
+ }
+
+ /*
+ * Whether we're destroying a BE or a single snapshot, we need to walk
+ * the tree of what we're going to destroy and promote everything in our
+ * path so that we can make it happen.
+ */
+ if ((err = be_promote_dependent_clones(fs, &bdd)) != 0) {
+ free(bdd.snapname);
+
+ /*
+ * If we're just destroying the origin of some other dataset
+ * we were invoked to destroy, then we just ignore
+ * BE_ERR_HASCLONES and return success unless the caller wanted
+ * to force the issue.
+ */
+ if (odestroyer && err == BE_ERR_HASCLONES &&
+ (options & BE_DESTROY_AUTOORIGIN) != 0)
+ return (0);
+ return (set_error(lbh, err));
+ }
+
+ /*
+ * This was deferred until after we promote all of the derivatives so
+ * that we grab the new origin after everything's settled down.
+ */
+ if ((options & BE_DESTROY_WANTORIGIN) != 0 &&
+ zfs_prop_get(fs, ZFS_PROP_ORIGIN, origin, sizeof(origin),
+ NULL, NULL, 0, 1) != 0 &&
+ (options & BE_DESTROY_ORIGIN) != 0)
+ return (set_error(lbh, BE_ERR_NOORIGIN));
+
+ /*
+ * If the caller wants auto-origin destruction and the origin
+ * name matches one of our automatically created snapshot names
+ * (i.e. strftime("%F-%T") with a serial at the end), then
+ * we'll set the DESTROY_ORIGIN flag and nuke it
+ * be_is_auto_snapshot_name is exported from libbe(3) so that
+ * the caller can determine if it needs to warn about the origin
+ * not being destroyed or not.
+ */
+ if ((options & BE_DESTROY_AUTOORIGIN) != 0 && *origin != '\0' &&
+ be_is_auto_snapshot_name(lbh, origin))
+ options |= BE_DESTROY_ORIGIN;
+
+ err = be_destroy_cb(fs, &bdd);
+ zfs_close(fs);
+ free(bdd.snapname);
+ if (err != 0) {
+ /* Children are still present or the mount is referenced */
+ if (err == EBUSY)
+ return (set_error(lbh, BE_ERR_DESTROYMNT));
+ return (set_error(lbh, BE_ERR_UNKNOWN));
+ }
+
+ if ((options & BE_DESTROY_ORIGIN) == 0)
+ return (0);
+
+ /* The origin can't possibly be shorter than the BE root */
+ rootlen = strlen(lbh->root);
+ if (*origin == '\0' || strlen(origin) <= rootlen + 1)
+ return (set_error(lbh, BE_ERR_INVORIGIN));
+
+ /*
+ * We'll be chopping off the BE root and running this back through
+ * be_destroy, so that we properly handle the origin snapshot whether
+ * it be that of a deep BE or not.
+ */
+ if (strncmp(origin, lbh->root, rootlen) != 0 || origin[rootlen] != '/')
+ return (0);
+
+ return (be_destroy_internal(lbh, origin + rootlen + 1,
+ options & ~BE_DESTROY_ORIGIN, true));
+}
+
+int
+be_destroy(libbe_handle_t *lbh, const char *name, int options)
+{
+
+ /*
+ * The consumer must not set both BE_DESTROY_AUTOORIGIN and
+ * BE_DESTROY_ORIGIN. Internally, we'll set the latter from the former.
+ * The latter should imply that we must succeed at destroying the
+ * origin, or complain otherwise.
+ */
+ if ((options & BE_DESTROY_WANTORIGIN) == BE_DESTROY_WANTORIGIN)
+ return (set_error(lbh, BE_ERR_UNKNOWN));
+ return (be_destroy_internal(lbh, name, options, false));
+}
+
+static void
+be_setup_snapshot_name(libbe_handle_t *lbh, char *buf, size_t buflen)
+{
+ time_t rawtime;
+ int len, serial;
+
+ time(&rawtime);
+ len = strlen(buf);
+ len += strftime(buf + len, buflen - len, "@%F-%T", localtime(&rawtime));
+ /* No room for serial... caller will do its best */
+ if (buflen - len < 2)
+ return;
+
+ for (serial = 0; serial < BE_SNAP_SERIAL_MAX; ++serial) {
+ snprintf(buf + len, buflen - len, "-%d", serial);
+ if (!zfs_dataset_exists(lbh->lzh, buf, ZFS_TYPE_SNAPSHOT))
+ return;
+ }
+}
+
+bool
+be_is_auto_snapshot_name(libbe_handle_t *lbh __unused, const char *name)
+{
+ const char *snap;
+ int day, hour, minute, month, second, serial, year;
+
+ if ((snap = strchr(name, '@')) == NULL)
+ return (false);
+ ++snap;
+ /* We'll grab the individual components and do some light validation. */
+ if (sscanf(snap, "%d-%d-%d-%d:%d:%d-%d", &year, &month, &day, &hour,
+ &minute, &second, &serial) != 7)
+ return (false);
+ return (year >= 1970) && (month >= 1 && month <= 12) &&
+ (day >= 1 && day <= 31) && (hour >= 0 && hour <= 23) &&
+ (minute >= 0 && minute <= 59) && (second >= 0 && second <= 60) &&
+ serial >= 0;
+}
+
+int
+be_snapshot(libbe_handle_t *lbh, const char *source, const char *snap_name,
+ bool recursive, char *result)
+{
+ char buf[BE_MAXPATHLEN];
+ int err;
+
+ be_root_concat(lbh, source, buf);
+
+ if ((err = be_exists(lbh, buf)) != 0)
+ return (set_error(lbh, err));
+
+ if (snap_name != NULL) {
+ if (strlcat(buf, "@", sizeof(buf)) >= sizeof(buf))
+ return (set_error(lbh, BE_ERR_INVALIDNAME));
+
+ if (strlcat(buf, snap_name, sizeof(buf)) >= sizeof(buf))
+ return (set_error(lbh, BE_ERR_INVALIDNAME));
+
+ if (result != NULL)
+ snprintf(result, BE_MAXPATHLEN, "%s@%s", source,
+ snap_name);
+ } else {
+ be_setup_snapshot_name(lbh, buf, sizeof(buf));
+
+ if (result != NULL && strlcpy(result, strrchr(buf, '/') + 1,
+ sizeof(buf)) >= sizeof(buf))
+ return (set_error(lbh, BE_ERR_INVALIDNAME));
+ }
+ if ((err = zfs_snapshot(lbh->lzh, buf, recursive, NULL)) != 0) {
+ switch (err) {
+ case EZFS_INVALIDNAME:
+ return (set_error(lbh, BE_ERR_INVALIDNAME));
+
+ default:
+ /*
+ * The other errors that zfs_ioc_snapshot might return
+ * shouldn't happen if we've set things up properly, so
+ * we'll gloss over them and call it UNKNOWN as it will
+ * require further triage.
+ */
+ if (errno == ENOTSUP)
+ return (set_error(lbh, BE_ERR_NOPOOL));
+ return (set_error(lbh, BE_ERR_UNKNOWN));
+ }
+ }
+
+ return (BE_ERR_SUCCESS);
+}
+
+
+/*
+ * Create the boot environment specified by the name parameter
+ */
+int
+be_create(libbe_handle_t *lbh, const char *name)
+{
+ int err;
+
+ err = be_create_from_existing(lbh, name, be_active_path(lbh));
+
+ return (set_error(lbh, err));
+}
+
+static int
+be_deep_clone_prop(int prop, void *cb)
+{
+ int err;
+ struct libbe_dccb *dccb;
+ zprop_source_t src;
+ char pval[BE_MAXPATHLEN];
+ char source[BE_MAXPATHLEN];
+ char *val;
+
+ dccb = cb;
+ /* Skip some properties we don't want to touch */
+ if (prop == ZFS_PROP_CANMOUNT)
+ return (ZPROP_CONT);
+
+ /* Don't copy readonly properties */
+ if (zfs_prop_readonly(prop))
+ return (ZPROP_CONT);
+
+ if ((err = zfs_prop_get(dccb->zhp, prop, (char *)&pval,
+ sizeof(pval), &src, (char *)&source, sizeof(source), false)))
+ /* Just continue if we fail to read a property */
+ return (ZPROP_CONT);
+
+ /*
+ * Only copy locally defined or received properties. This continues
+ * to avoid temporary/default/local properties intentionally without
+ * breaking received datasets.
+ */
+ if (src != ZPROP_SRC_LOCAL && src != ZPROP_SRC_RECEIVED)
+ return (ZPROP_CONT);
+
+ /* Augment mountpoint with altroot, if needed */
+ val = pval;
+ if (prop == ZFS_PROP_MOUNTPOINT)
+ val = be_mountpoint_augmented(dccb->lbh, val);
+
+ nvlist_add_string(dccb->props, zfs_prop_to_name(prop), val);
+
+ return (ZPROP_CONT);
+}
+
+/*
+ * Return the corresponding boot environment path for a given
+ * dataset path, the constructed path is placed in 'result'.
+ *
+ * example: say our new boot environment name is 'bootenv' and
+ * the dataset path is 'zroot/ROOT/default/data/set'.
+ *
+ * result should produce: 'zroot/ROOT/bootenv/data/set'
*** 2396 LINES SKIPPED ***