diff --git a/lib/libzfs/libzfs_iter.c b/lib/libzfs/libzfs_iter.c index 7806e21cd9a9..3c537be79487 100644 --- a/lib/libzfs/libzfs_iter.c +++ b/lib/libzfs/libzfs_iter.c @@ -1,600 +1,603 @@ /* * CDDL HEADER START * * The contents of this file are subject to the terms of the * Common Development and Distribution License (the "License"). * You may not use this file except in compliance with the License. * * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE * or http://www.opensolaris.org/os/licensing. * See the License for the specific language governing permissions * and limitations under the License. * * When distributing Covered Code, include this CDDL HEADER in each * file and include the License file at usr/src/OPENSOLARIS.LICENSE. * If applicable, add the following below this CDDL HEADER, with the * fields enclosed by brackets "[]" replaced with your own identifying * information: Portions Copyright [yyyy] [name of copyright owner] * * CDDL HEADER END */ /* * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2013, 2019 by Delphix. All rights reserved. * Copyright 2014 Nexenta Systems, Inc. All rights reserved. * Copyright (c) 2019 Datto Inc. */ #include #include #include #include #include #include #include #include #include #include "libzfs_impl.h" static int zfs_iter_clones(zfs_handle_t *zhp, zfs_iter_f func, void *data) { nvlist_t *nvl = zfs_get_clones_nvl(zhp); nvpair_t *pair; if (nvl == NULL) return (0); for (pair = nvlist_next_nvpair(nvl, NULL); pair != NULL; pair = nvlist_next_nvpair(nvl, pair)) { zfs_handle_t *clone = zfs_open(zhp->zfs_hdl, nvpair_name(pair), ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); if (clone != NULL) { int err = func(clone, data); if (err != 0) return (err); } } return (0); } static int zfs_do_list_ioctl(zfs_handle_t *zhp, int arg, zfs_cmd_t *zc) { int rc; uint64_t orig_cookie; orig_cookie = zc->zc_cookie; top: (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name)); rc = zfs_ioctl(zhp->zfs_hdl, arg, zc); if (rc == -1) { switch (errno) { case ENOMEM: /* expand nvlist memory and try again */ if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, zc) != 0) { zcmd_free_nvlists(zc); return (-1); } zc->zc_cookie = orig_cookie; goto top; /* * An errno value of ESRCH indicates normal completion. * If ENOENT is returned, then the underlying dataset * has been removed since we obtained the handle. */ case ESRCH: case ENOENT: rc = 1; break; default: rc = zfs_standard_error(zhp->zfs_hdl, errno, dgettext(TEXT_DOMAIN, "cannot iterate filesystems")); break; } } return (rc); } /* * Iterate over all child filesystems */ int zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data) { zfs_cmd_t zc = {"\0"}; zfs_handle_t *nzhp; int ret; if (zhp->zfs_type != ZFS_TYPE_FILESYSTEM) return (0); if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) return (-1); while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_DATASET_LIST_NEXT, &zc)) == 0) { /* * Silently ignore errors, as the only plausible explanation is * that the pool has since been removed. */ if ((nzhp = make_dataset_handle_zc(zhp->zfs_hdl, &zc)) == NULL) { continue; } if ((ret = func(nzhp, data)) != 0) { zcmd_free_nvlists(&zc); return (ret); } } zcmd_free_nvlists(&zc); return ((ret < 0) ? ret : 0); } /* * Iterate over all snapshots */ int zfs_iter_snapshots(zfs_handle_t *zhp, boolean_t simple, zfs_iter_f func, void *data, uint64_t min_txg, uint64_t max_txg) { zfs_cmd_t zc = {"\0"}; zfs_handle_t *nzhp; int ret; nvlist_t *range_nvl = NULL; if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT || zhp->zfs_type == ZFS_TYPE_BOOKMARK) return (0); zc.zc_simple = simple; if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) return (-1); if (min_txg != 0) { range_nvl = fnvlist_alloc(); fnvlist_add_uint64(range_nvl, SNAP_ITER_MIN_TXG, min_txg); } if (max_txg != 0) { if (range_nvl == NULL) range_nvl = fnvlist_alloc(); fnvlist_add_uint64(range_nvl, SNAP_ITER_MAX_TXG, max_txg); } if (range_nvl != NULL && zcmd_write_src_nvlist(zhp->zfs_hdl, &zc, range_nvl) != 0) { zcmd_free_nvlists(&zc); fnvlist_free(range_nvl); return (-1); } while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_SNAPSHOT_LIST_NEXT, &zc)) == 0) { if (simple) nzhp = make_dataset_simple_handle_zc(zhp, &zc); else nzhp = make_dataset_handle_zc(zhp->zfs_hdl, &zc); if (nzhp == NULL) continue; if ((ret = func(nzhp, data)) != 0) { zcmd_free_nvlists(&zc); fnvlist_free(range_nvl); return (ret); } } zcmd_free_nvlists(&zc); fnvlist_free(range_nvl); return ((ret < 0) ? ret : 0); } /* * Iterate over all bookmarks */ int zfs_iter_bookmarks(zfs_handle_t *zhp, zfs_iter_f func, void *data) { zfs_handle_t *nzhp; nvlist_t *props = NULL; nvlist_t *bmarks = NULL; int err; nvpair_t *pair; if ((zfs_get_type(zhp) & (ZFS_TYPE_SNAPSHOT | ZFS_TYPE_BOOKMARK)) != 0) return (0); /* Setup the requested properties nvlist. */ props = fnvlist_alloc(); for (zfs_prop_t p = 0; p < ZFS_NUM_PROPS; p++) { if (zfs_prop_valid_for_type(p, ZFS_TYPE_BOOKMARK, B_FALSE)) { fnvlist_add_boolean(props, zfs_prop_to_name(p)); } } fnvlist_add_boolean(props, "redact_complete"); if ((err = lzc_get_bookmarks(zhp->zfs_name, props, &bmarks)) != 0) goto out; for (pair = nvlist_next_nvpair(bmarks, NULL); pair != NULL; pair = nvlist_next_nvpair(bmarks, pair)) { char name[ZFS_MAX_DATASET_NAME_LEN]; char *bmark_name; nvlist_t *bmark_props; bmark_name = nvpair_name(pair); bmark_props = fnvpair_value_nvlist(pair); if (snprintf(name, sizeof (name), "%s#%s", zhp->zfs_name, bmark_name) >= sizeof (name)) { err = EINVAL; goto out; } nzhp = make_bookmark_handle(zhp, name, bmark_props); if (nzhp == NULL) continue; if ((err = func(nzhp, data)) != 0) goto out; } out: fnvlist_free(props); fnvlist_free(bmarks); return (err); } /* * Routines for dealing with the sorted snapshot functionality */ typedef struct zfs_node { zfs_handle_t *zn_handle; avl_node_t zn_avlnode; } zfs_node_t; static int zfs_sort_snaps(zfs_handle_t *zhp, void *data) { avl_tree_t *avl = data; zfs_node_t *node; zfs_node_t search; search.zn_handle = zhp; node = avl_find(avl, &search, NULL); if (node) { /* * If this snapshot was renamed while we were creating the * AVL tree, it's possible that we already inserted it under * its old name. Remove the old handle before adding the new * one. */ zfs_close(node->zn_handle); avl_remove(avl, node); free(node); } node = zfs_alloc(zhp->zfs_hdl, sizeof (zfs_node_t)); node->zn_handle = zhp; avl_add(avl, node); return (0); } static int zfs_snapshot_compare(const void *larg, const void *rarg) { zfs_handle_t *l = ((zfs_node_t *)larg)->zn_handle; zfs_handle_t *r = ((zfs_node_t *)rarg)->zn_handle; uint64_t lcreate, rcreate; /* * Sort them according to creation time. We use the hidden * CREATETXG property to get an absolute ordering of snapshots. */ lcreate = zfs_prop_get_int(l, ZFS_PROP_CREATETXG); rcreate = zfs_prop_get_int(r, ZFS_PROP_CREATETXG); return (TREE_CMP(lcreate, rcreate)); } int zfs_iter_snapshots_sorted(zfs_handle_t *zhp, zfs_iter_f callback, void *data, uint64_t min_txg, uint64_t max_txg) { int ret = 0; zfs_node_t *node; avl_tree_t avl; void *cookie = NULL; avl_create(&avl, zfs_snapshot_compare, sizeof (zfs_node_t), offsetof(zfs_node_t, zn_avlnode)); ret = zfs_iter_snapshots(zhp, B_FALSE, zfs_sort_snaps, &avl, min_txg, max_txg); for (node = avl_first(&avl); node != NULL; node = AVL_NEXT(&avl, node)) ret |= callback(node->zn_handle, data); while ((node = avl_destroy_nodes(&avl, &cookie)) != NULL) free(node); avl_destroy(&avl); return (ret); } typedef struct { char *ssa_first; char *ssa_last; boolean_t ssa_seenfirst; boolean_t ssa_seenlast; zfs_iter_f ssa_func; void *ssa_arg; } snapspec_arg_t; static int snapspec_cb(zfs_handle_t *zhp, void *arg) { snapspec_arg_t *ssa = arg; const char *shortsnapname; int err = 0; if (ssa->ssa_seenlast) return (0); shortsnapname = strchr(zfs_get_name(zhp), '@') + 1; if (!ssa->ssa_seenfirst && strcmp(shortsnapname, ssa->ssa_first) == 0) ssa->ssa_seenfirst = B_TRUE; if (strcmp(shortsnapname, ssa->ssa_last) == 0) ssa->ssa_seenlast = B_TRUE; if (ssa->ssa_seenfirst) { err = ssa->ssa_func(zhp, ssa->ssa_arg); } else { zfs_close(zhp); } return (err); } /* * spec is a string like "A,B%C,D" * * , where can be: * (single snapshot) * % (range of snapshots, inclusive) * % (range of snapshots, starting with earliest) * % (range of snapshots, ending with last) * % (all snapshots) * [,...] (comma separated list of the above) * * If a snapshot can not be opened, continue trying to open the others, but * return ENOENT at the end. */ int zfs_iter_snapspec(zfs_handle_t *fs_zhp, const char *spec_orig, zfs_iter_f func, void *arg) { char *buf, *comma_separated, *cp; int err = 0; int ret = 0; buf = zfs_strdup(fs_zhp->zfs_hdl, spec_orig); cp = buf; while ((comma_separated = strsep(&cp, ",")) != NULL) { char *pct = strchr(comma_separated, '%'); if (pct != NULL) { snapspec_arg_t ssa = { 0 }; ssa.ssa_func = func; ssa.ssa_arg = arg; if (pct == comma_separated) ssa.ssa_seenfirst = B_TRUE; else ssa.ssa_first = comma_separated; *pct = '\0'; ssa.ssa_last = pct + 1; /* * If there is a lastname specified, make sure it * exists. */ if (ssa.ssa_last[0] != '\0') { char snapname[ZFS_MAX_DATASET_NAME_LEN]; (void) snprintf(snapname, sizeof (snapname), "%s@%s", zfs_get_name(fs_zhp), ssa.ssa_last); if (!zfs_dataset_exists(fs_zhp->zfs_hdl, snapname, ZFS_TYPE_SNAPSHOT)) { ret = ENOENT; continue; } } err = zfs_iter_snapshots_sorted(fs_zhp, snapspec_cb, &ssa, 0, 0); if (ret == 0) ret = err; if (ret == 0 && (!ssa.ssa_seenfirst || (ssa.ssa_last[0] != '\0' && !ssa.ssa_seenlast))) { ret = ENOENT; } } else { char snapname[ZFS_MAX_DATASET_NAME_LEN]; zfs_handle_t *snap_zhp; (void) snprintf(snapname, sizeof (snapname), "%s@%s", zfs_get_name(fs_zhp), comma_separated); snap_zhp = make_dataset_handle(fs_zhp->zfs_hdl, snapname); if (snap_zhp == NULL) { ret = ENOENT; continue; } err = func(snap_zhp, arg); if (ret == 0) ret = err; } } free(buf); return (ret); } /* * Iterate over all children, snapshots and filesystems * Process snapshots before filesystems because they are nearer the input * handle: this is extremely important when used with zfs_iter_f functions * looking for data, following the logic that we would like to find it as soon * and as close as possible. */ int zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data) { int ret; if ((ret = zfs_iter_snapshots(zhp, B_FALSE, func, data, 0, 0)) != 0) return (ret); return (zfs_iter_filesystems(zhp, func, data)); } typedef struct iter_stack_frame { struct iter_stack_frame *next; zfs_handle_t *zhp; } iter_stack_frame_t; typedef struct iter_dependents_arg { boolean_t first; boolean_t allowrecursion; iter_stack_frame_t *stack; zfs_iter_f func; void *data; } iter_dependents_arg_t; static int iter_dependents_cb(zfs_handle_t *zhp, void *arg) { iter_dependents_arg_t *ida = arg; int err = 0; boolean_t first = ida->first; ida->first = B_FALSE; if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { err = zfs_iter_clones(zhp, iter_dependents_cb, ida); } else if (zhp->zfs_type != ZFS_TYPE_BOOKMARK) { iter_stack_frame_t isf; iter_stack_frame_t *f; /* * check if there is a cycle by seeing if this fs is already * on the stack. */ for (f = ida->stack; f != NULL; f = f->next) { if (f->zhp->zfs_dmustats.dds_guid == zhp->zfs_dmustats.dds_guid) { if (ida->allowrecursion) { zfs_close(zhp); return (0); } else { zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, "recursive dependency at '%s'"), zfs_get_name(zhp)); err = zfs_error(zhp->zfs_hdl, EZFS_RECURSIVE, dgettext(TEXT_DOMAIN, "cannot determine dependent " "datasets")); zfs_close(zhp); return (err); } } } isf.zhp = zhp; isf.next = ida->stack; ida->stack = &isf; err = zfs_iter_filesystems(zhp, iter_dependents_cb, ida); if (err == 0) err = zfs_iter_snapshots(zhp, B_FALSE, iter_dependents_cb, ida, 0, 0); ida->stack = isf.next; } if (!first && err == 0) err = ida->func(zhp, ida->data); else zfs_close(zhp); return (err); } int zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion, zfs_iter_f func, void *data) { iter_dependents_arg_t ida; ida.allowrecursion = allowrecursion; ida.stack = NULL; ida.func = func; ida.data = data; ida.first = B_TRUE; return (iter_dependents_cb(zfs_handle_dup(zhp), &ida)); } /* * Iterate over mounted children of the specified dataset */ int zfs_iter_mounted(zfs_handle_t *zhp, zfs_iter_f func, void *data) { char mnt_prop[ZFS_MAXPROPLEN]; struct mnttab entry; zfs_handle_t *mtab_zhp; size_t namelen = strlen(zhp->zfs_name); FILE *mnttab; int err = 0; if ((mnttab = fopen(MNTTAB, "re")) == NULL) return (ENOENT); while (err == 0 && getmntent(mnttab, &entry) == 0) { /* Ignore non-ZFS entries */ if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) continue; /* Ignore datasets not within the provided dataset */ if (strncmp(entry.mnt_special, zhp->zfs_name, namelen) != 0 || - (entry.mnt_special[namelen] != '/' && - entry.mnt_special[namelen] != '@')) + entry.mnt_special[namelen] != '/') + continue; + + /* Skip snapshot of any child dataset */ + if (strchr(entry.mnt_special, '@') != NULL) continue; if ((mtab_zhp = zfs_open(zhp->zfs_hdl, entry.mnt_special, ZFS_TYPE_FILESYSTEM)) == NULL) continue; /* Ignore legacy mounts as they are user managed */ verify(zfs_prop_get(mtab_zhp, ZFS_PROP_MOUNTPOINT, mnt_prop, sizeof (mnt_prop), NULL, NULL, 0, B_FALSE) == 0); if (strcmp(mnt_prop, "legacy") == 0) { zfs_close(mtab_zhp); continue; } err = func(mtab_zhp, data); } fclose(mnttab); return (err); } diff --git a/tests/test-runner/include/logapi.shlib b/tests/test-runner/include/logapi.shlib index 5a7e76c0ddbf..c9c01ab752ea 100644 --- a/tests/test-runner/include/logapi.shlib +++ b/tests/test-runner/include/logapi.shlib @@ -1,530 +1,580 @@ # # CDDL HEADER START # # The contents of this file are subject to the terms of the # Common Development and Distribution License (the "License"). # You may not use this file except in compliance with the License. # # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE # or http://www.opensolaris.org/os/licensing. # See the License for the specific language governing permissions # and limitations under the License. # # When distributing Covered Code, include this CDDL HEADER in each # file and include the License file at usr/src/OPENSOLARIS.LICENSE. # If applicable, add the following below this CDDL HEADER, with the # fields enclosed by brackets "[]" replaced with your own identifying # information: Portions Copyright [yyyy] [name of copyright owner] # # CDDL HEADER END # # # Copyright 2007 Sun Microsystems, Inc. All rights reserved. # Use is subject to license terms. # # Copyright (c) 2012, 2020 by Delphix. All rights reserved. # . ${STF_TOOLS}/include/stf.shlib # Output an assertion # # $@ - assertion text function log_assert { _printline ASSERTION: "$@" } # Output a comment # # $@ - comment text function log_note { _printline NOTE: "$@" } # Execute and print command with status where success equals non-zero result # # $@ - command to execute # # return 0 if command fails, otherwise return 1 function log_neg { log_neg_expect "" "$@" return $? } # Execute a positive test and exit $STF_FAIL is test fails # # $@ - command to execute function log_must { log_pos "$@" (( $? != 0 )) && log_fail } +# Execute a positive test (expecting no stderr) and exit $STF_FAIL +# if test fails +# $@ - command to execute + +function log_must_nostderr +{ + log_pos_nostderr "$@" + (( $? != 0 )) && log_fail +} + # Execute a positive test but retry the command on failure if the output # matches an expected pattern. Otherwise behave like log_must and exit # $STF_FAIL is test fails. # # $1 - retry keyword # $2 - retry attempts # $3-$@ - command to execute # function log_must_retry { typeset out="" typeset logfile="/tmp/log.$$" typeset status=1 typeset expect=$1 typeset retry=$2 typeset delay=1 shift 2 while [[ -e $logfile ]]; do logfile="$logfile.$$" done while (( $retry > 0 )); do "$@" 2>$logfile status=$? out="cat $logfile" if (( $status == 0 )); then $out | egrep -i "internal error|assertion failed" \ > /dev/null 2>&1 # internal error or assertion failed if [[ $? -eq 0 ]]; then print -u2 $($out) _printerror "$@" "internal error or" \ " assertion failure exited $status" status=1 else [[ -n $LOGAPI_DEBUG ]] && cat $logfile _printsuccess "$@" fi break else $out | grep -i "$expect" > /dev/null 2>&1 if (( $? == 0 )); then print -u2 $($out) _printerror "$@" "Retry in $delay seconds" sleep $delay (( retry=retry - 1 )) (( delay=delay * 2 )) else break; fi fi done if (( $status != 0 )) ; then print -u2 $($out) _printerror "$@" "exited $status" fi _recursive_output $logfile "false" return $status } # Execute a positive test and exit $STF_FAIL is test fails after being # retried up to 5 times when the command returns the keyword "busy". # # $@ - command to execute function log_must_busy { log_must_retry "busy" 5 "$@" (( $? != 0 )) && log_fail } # Execute a negative test and exit $STF_FAIL if test passes # # $@ - command to execute function log_mustnot { log_neg "$@" (( $? != 0 )) && log_fail } # Execute a negative test with keyword expected, and exit # $STF_FAIL if test passes # # $1 - keyword expected # $2-$@ - command to execute function log_mustnot_expect { log_neg_expect "$@" (( $? != 0 )) && log_fail } # Signal numbers are platform-dependent case $(uname) in Darwin|FreeBSD) SIGBUS=10 SIGSEGV=11 ;; illumos|Linux|*) SIGBUS=7 SIGSEGV=11 ;; esac EXIT_SUCCESS=0 EXIT_NOTFOUND=127 EXIT_SIGNAL=256 EXIT_SIGBUS=$((EXIT_SIGNAL + SIGBUS)) EXIT_SIGSEGV=$((EXIT_SIGNAL + SIGSEGV)) # Execute and print command with status where success equals non-zero result # or output includes expected keyword # # $1 - keyword expected # $2-$@ - command to execute # # return 0 if command fails, or the output contains the keyword expected, # return 1 otherwise function log_neg_expect { typeset out="" typeset logfile="/tmp/log.$$" typeset ret=1 typeset expect=$1 shift while [[ -e $logfile ]]; do logfile="$logfile.$$" done "$@" 2>$logfile typeset status=$? out="cat $logfile" # unexpected status if (( $status == EXIT_SUCCESS )); then print -u2 $($out) _printerror "$@" "unexpectedly exited $status" # missing binary elif (( $status == EXIT_NOTFOUND )); then print -u2 $($out) _printerror "$@" "unexpectedly exited $status (File not found)" # bus error - core dump elif (( $status == EXIT_SIGBUS )); then print -u2 $($out) _printerror "$@" "unexpectedly exited $status (Bus Error)" # segmentation violation - core dump elif (( $status == EXIT_SIGSEGV )); then print -u2 $($out) _printerror "$@" "unexpectedly exited $status (SEGV)" else $out | egrep -i "internal error|assertion failed" \ > /dev/null 2>&1 # internal error or assertion failed if (( $? == 0 )); then print -u2 $($out) _printerror "$@" "internal error or assertion failure" \ " exited $status" elif [[ -n $expect ]] ; then $out | grep -i "$expect" > /dev/null 2>&1 if (( $? == 0 )); then ret=0 else print -u2 $($out) _printerror "$@" "unexpectedly exited $status" fi else ret=0 fi if (( $ret == 0 )); then [[ -n $LOGAPI_DEBUG ]] && cat $logfile _printsuccess "$@" "exited $status" fi fi _recursive_output $logfile "false" return $ret } # Execute and print command with status where success equals zero result # # $@ command to execute # # return command exit status function log_pos { typeset out="" typeset logfile="/tmp/log.$$" while [[ -e $logfile ]]; do logfile="$logfile.$$" done "$@" 2>$logfile typeset status=$? out="cat $logfile" if (( $status != 0 )) ; then print -u2 $($out) _printerror "$@" "exited $status" else $out | egrep -i "internal error|assertion failed" \ > /dev/null 2>&1 # internal error or assertion failed if [[ $? -eq 0 ]]; then print -u2 $($out) _printerror "$@" "internal error or assertion failure" \ " exited $status" status=1 else [[ -n $LOGAPI_DEBUG ]] && cat $logfile _printsuccess "$@" fi fi _recursive_output $logfile "false" return $status } +# Execute and print command with status where success equals zero result +# and no stderr output +# +# $@ command to execute +# +# return 0 if command succeeds and no stderr output +# return 1 othersie + +function log_pos_nostderr +{ + typeset out="" + typeset logfile="/tmp/log.$$" + + while [[ -e $logfile ]]; do + logfile="$logfile.$$" + done + + "$@" 2>$logfile + typeset status=$? + out="cat $logfile" + typeset out_msg=$($out) + + if (( $status != 0 )) ; then + print -u2 $out_msg + _printerror "$@" "exited $status" + else + if [[ ! -z "$out_msg" ]]; then + print -u2 $out_msg + _printerror "$@" "message in stderr" \ + " exited $status" + status=1 + else + [[ -n $LOGAPI_DEBUG ]] && cat $logfile + _printsuccess "$@" + fi + fi + _recursive_output $logfile "false" + return $status +} + # Set an exit handler # # $@ - function(s) to perform on exit function log_onexit { _CLEANUP=("$*") } # Push an exit handler on the cleanup stack # # $@ - function(s) to perform on exit function log_onexit_push { _CLEANUP+=("$*") } # Pop an exit handler off the cleanup stack function log_onexit_pop { _CLEANUP=("${_CLEANUP[@]:0:${#_CLEANUP[@]}-1}") } # # Exit functions # # Perform cleanup and exit $STF_PASS # # $@ - message text function log_pass { _endlog $STF_PASS "$@" } # Perform cleanup and exit $STF_FAIL # # $@ - message text function log_fail { _endlog $STF_FAIL "$@" } # Perform cleanup and exit $STF_UNRESOLVED # # $@ - message text function log_unresolved { _endlog $STF_UNRESOLVED "$@" } # Perform cleanup and exit $STF_NOTINUSE # # $@ - message text function log_notinuse { _endlog $STF_NOTINUSE "$@" } # Perform cleanup and exit $STF_UNSUPPORTED # # $@ - message text function log_unsupported { _endlog $STF_UNSUPPORTED "$@" } # Perform cleanup and exit $STF_UNTESTED # # $@ - message text function log_untested { _endlog $STF_UNTESTED "$@" } # Perform cleanup and exit $STF_UNINITIATED # # $@ - message text function log_uninitiated { _endlog $STF_UNINITIATED "$@" } # Perform cleanup and exit $STF_NORESULT # # $@ - message text function log_noresult { _endlog $STF_NORESULT "$@" } # Perform cleanup and exit $STF_WARNING # # $@ - message text function log_warning { _endlog $STF_WARNING "$@" } # Perform cleanup and exit $STF_TIMED_OUT # # $@ - message text function log_timed_out { _endlog $STF_TIMED_OUT "$@" } # Perform cleanup and exit $STF_OTHER # # $@ - message text function log_other { _endlog $STF_OTHER "$@" } function set_main_pid { _MAINPID=$1 } # # Internal functions # # Execute custom callback scripts on test failure # # callback script paths are stored in TESTFAIL_CALLBACKS, delimited by ':'. function _execute_testfail_callbacks { typeset callback print "$TESTFAIL_CALLBACKS:" | while read -d ":" callback; do if [[ -n "$callback" ]] ; then log_note "Performing test-fail callback ($callback)" $callback fi done } # Perform cleanup and exit # # $1 - stf exit code # $2-$n - message text function _endlog { typeset logfile="/tmp/log.$$" _recursive_output $logfile typeset exitcode=$1 shift (( ${#@} > 0 )) && _printline "$@" # # If we're running in a subshell then just exit and let # the parent handle the failures # if [[ -n "$_MAINPID" && $$ != "$_MAINPID" ]]; then log_note "subshell exited: "$_MAINPID exit $exitcode fi if [[ $exitcode == $STF_FAIL ]] ; then _execute_testfail_callbacks fi typeset stack=("${_CLEANUP[@]}") log_onexit "" typeset i=${#stack[@]} while (( i-- )); do typeset cleanup="${stack[i]}" log_note "Performing local cleanup via log_onexit ($cleanup)" $cleanup done exit $exitcode } # Output a formatted line # # $@ - message text function _printline { print "$@" } # Output an error message # # $@ - message text function _printerror { _printline ERROR: "$@" } # Output a success message # # $@ - message text function _printsuccess { _printline SUCCESS: "$@" } # Output logfiles recursively # # $1 - start file # $2 - indicate whether output the start file itself, default as yes. function _recursive_output #logfile { typeset logfile=$1 while [[ -e $logfile ]]; do if [[ -z $2 || $logfile != $1 ]]; then cat $logfile fi rm -f $logfile logfile="$logfile.$$" done } diff --git a/tests/zfs-tests/tests/functional/cli_root/zfs_unmount/zfs_unmount_nested.ksh b/tests/zfs-tests/tests/functional/cli_root/zfs_unmount/zfs_unmount_nested.ksh index 987ecca31396..7da8be3d17eb 100755 --- a/tests/zfs-tests/tests/functional/cli_root/zfs_unmount/zfs_unmount_nested.ksh +++ b/tests/zfs-tests/tests/functional/cli_root/zfs_unmount/zfs_unmount_nested.ksh @@ -1,193 +1,193 @@ #!/bin/ksh -p # # CDDL HEADER START # # This file and its contents are supplied under the terms of the # Common Development and Distribution License ("CDDL"), version 1.0. # You may only use this file in accordance with the terms of version # 1.0 of the CDDL. # # A full copy of the text of the CDDL should have accompanied this # source. A copy is of the CDDL is also available via the Internet # at http://www.illumos.org/license/CDDL. # # CDDL HEADER END # # # Copyright (c) 2018 Datto Inc. # . $STF_SUITE/include/libtest.shlib # # DESCRIPTION: # zfs unmount should work on nested datasets # # STRATEGY: # 1. Create a set of nested datasets # 2. Unmount a nested dataset and make sure it is unmounted # 3. Ensure the dataset deeper than the one above is also unmounted # 4. Ensure the datasets shallower than the unmounted one is still mounted # 5. Repeat from step 2 with other mountpoint values and shallower nesting # verify_runnable "both" function nesting_cleanup { log_must zfs destroy -fR $TESTPOOL/a log_must zfs destroy -fR $TESTPOOL/b log_must zfs destroy -fR $TESTPOOL/c log_must zfs destroy -fR $TESTPOOL/d } log_onexit nesting_cleanup set -A test_depths 30 16 3 typeset mountpoint=/$TESTPOOL/mnt dsA32=$(printf 'a/%.0s' {1..32})"a" log_must zfs create -p $TESTPOOL/$dsA32 dsB32=$(printf 'b/%.0s' {1..32})"b" log_must zfs create -o mountpoint=none -p $TESTPOOL/$dsB32 # FreeBSD's mount command ignores the mountpoint property. if ! is_freebsd; then log_mustnot mount -t zfs $TESTPOOL/$dsB32 /mnt fi dsC32=$(printf 'c/%.0s' {1..32})"c" log_must zfs create -o mountpoint=legacy -p $TESTPOOL/$dsC32 log_must mount -t zfs $TESTPOOL/$dsC32 /mnt dsD32=$(printf 'd/%.0s' {1..32})"d" log_must zfs create -o mountpoint=$mountpoint -p $TESTPOOL/$dsD32 for d in ${test_depths[@]}; do # default mountpoint ds_pre=$(printf 'a/%.0s' {1..$(($d-2))})"a" ds=$(printf 'a/%.0s' {1..$(($d-1))})"a" ds_post=$(printf 'a/%.0s' {1..$(($d))})"a" if ! ismounted $TESTPOOL/$ds_pre; then log_fail "$TESTPOOL/$ds_pre (pre) not initially mounted" fi if ! ismounted $TESTPOOL/$ds; then log_fail "$TESTPOOL/$ds not initially mounted" fi if ! ismounted $TESTPOOL/$ds_post; then log_fail "$TESTPOOL/$ds_post (post) not initially mounted" fi log_must zfs snapshot $TESTPOOL/$ds@snap # force snapshot mount in .zfs log_must ls /$TESTPOOL/$ds/.zfs/snapshot/snap - log_must zfs unmount $TESTPOOL/$ds + log_must_nostderr zfs unmount $TESTPOOL/$ds if ! ismounted $TESTPOOL/$ds_pre; then log_fail "$ds_pre is not mounted" fi if ismounted $TESTPOOL/$ds; then log_fail "$ds is mounted" fi if ismounted $TESTPOOL/$ds_post; then log_fail "$ds_post (post) is mounted" fi # mountpoint=none ds_pre=$(printf 'b/%.0s' {1..$(($d-2))})"b" ds=$(printf 'b/%.0s' {1..$(($d-1))})"b" ds_post=$(printf 'b/%.0s' {1..$(($d))})"b" if ! ismounted $TESTPOOL/$ds_pre; then log_fail "$TESTPOOL/$ds_pre (pre) not initially mounted" fi if ! ismounted $TESTPOOL/$ds; then log_fail "$TESTPOOL/$ds not initially mounted" fi if ! ismounted $TESTPOOL/$ds_post; then log_fail "$TESTPOOL/$ds_post (post) not initially mounted" fi log_must zfs snapshot $TESTPOOL/$ds@snap # force snapshot mount in .zfs log_must ls /$TESTPOOL/$ds/.zfs/snapshot/snap - log_must zfs unmount $TESTPOOL/$ds + log_must_nostderr zfs unmount $TESTPOOL/$ds if ! ismounted $TESTPOOL/$ds_pre; then log_fail "$TESTPOOL/$ds_pre (pre) not mounted" fi if ismounted $TESTPOOL/$ds; then log_fail "$TESTPOOL/$ds is mounted" fi if ismounted $TESTPOOL/$ds_post; then log_fail "$TESTPOOL/$ds_post (post) is mounted" fi # mountpoint=legacy ds_pre=$(printf 'c/%.0s' {1..$(($d-2))})"c" ds=$(printf 'c/%.0s' {1..$(($d-1))})"c" ds_post=$(printf 'c/%.0s' {1..$(($d))})"c" if ! ismounted $TESTPOOL/$ds_pre; then log_fail "$TESTPOOL/$ds_pre (pre) not initially mounted" fi if ! ismounted $TESTPOOL/$ds; then log_fail "$TESTPOOL/$ds not initially mounted" fi if ! ismounted $TESTPOOL/$ds_post; then log_fail "$TESTPOOL/$ds_post (post) not initially mounted" fi log_must zfs snapshot $TESTPOOL/$ds@snap # force snapshot mount in .zfs log_must ls /$TESTPOOL/$ds/.zfs/snapshot/snap - log_must zfs unmount $TESTPOOL/$ds + log_must_nostderr zfs unmount $TESTPOOL/$ds if ! ismounted $TESTPOOL/$ds_pre; then log_fail "$TESTPOOL/$ds_pre (pre) not mounted" fi if ismounted $TESTPOOL/$ds; then log_fail "$TESTPOOL/$ds is mounted" fi if ismounted $TESTPOOL/$ds_post; then log_fail "$TESTPOOL/$ds_post (post) is mounted" fi # mountpoint=/testpool/mnt ds_pre=$(printf 'd/%.0s' {1..$(($d-2))})"d" ds=$(printf 'd/%.0s' {1..$(($d-1))})"d" ds_post=$(printf 'd/%.0s' {1..$(($d))})"d" if ! ismounted $TESTPOOL/$ds_pre; then log_fail "$TESTPOOL/$ds_pre (pre) not initially mounted" fi if ! ismounted $TESTPOOL/$ds; then log_fail "$TESTPOOL/$ds not initially mounted" fi if ! ismounted $TESTPOOL/$ds_post; then log_fail "$TESTPOOL/$ds_post (post) not initially mounted" fi log_must zfs snapshot $TESTPOOL/$ds@snap # force snapshot mount in .zfs log_must ls /$TESTPOOL/$ds/.zfs/snapshot/snap - log_must zfs unmount $TESTPOOL/$ds + log_must_nostderr zfs unmount $TESTPOOL/$ds if ! ismounted $TESTPOOL/$ds_pre; then log_fail "$ds_pre is not mounted" fi if ismounted $TESTPOOL/$ds; then log_fail "$ds is mounted" fi if ismounted $TESTPOOL/$ds_post; then log_fail "$ds_post (post) is mounted" fi done log_must rmdir $mountpoint # remove the mountpoint we created log_must zpool export $TESTPOOL log_must zpool import $TESTPOOL log_pass "Verified nested dataset are unmounted."