From 475eca4908731a87fff1be500ba4d7b011d392e4 Mon Sep 17 00:00:00 2001 From: Chris Williamson Date: Thu, 8 Feb 2018 09:17:52 -0700 Subject: [PATCH] OpenZFS 8605 - zfs channel programs fix zfs.exists Authored by: Chris Williamson Reviewed by: Paul Dagnelie Reviewed by: Dan Kimmel Reviewed by: Matt Ahrens Approved by: Robert Mustacchi Ported-by: Don Brady zfs.exists() in channel programs doesn't return any result, and should have a man page entry. This patch corrects zfs.exists so that it returns a value indicating if the dataset exists or not. It also adds documentation about it in the man page. OpenZFS-issue: https://www.illumos.org/issues/8605 OpenZFS-commit: https://github.com/openzfs/openzfs/commit/1e85e111 --- man/man8/zfs-program.8 | 12 +++++ module/zfs/zcp.c | 2 +- tests/runfiles/linux.run | 4 +- .../channel_program/lua_core/Makefile.am | 2 + .../channel_program/lua_core/tst.exists.ksh | 45 +++++++++++++++++++ .../channel_program/lua_core/tst.exists.zcp | 26 +++++++++++ 6 files changed, 88 insertions(+), 3 deletions(-) create mode 100755 tests/zfs-tests/tests/functional/channel_program/lua_core/tst.exists.ksh create mode 100644 tests/zfs-tests/tests/functional/channel_program/lua_core/tst.exists.zcp diff --git a/man/man8/zfs-program.8 b/man/man8/zfs-program.8 index d84990ca1610..2e4e9587d593 100644 --- a/man/man8/zfs-program.8 +++ b/man/man8/zfs-program.8 @@ -289,6 +289,18 @@ msg (string) .Bd -ragged -compact -offset "xxxx" Debug message to be printed. .Ed +.It Em zfs.exists(dataset) +Returns true if the given dataset exists, or false if it doesn't. +A fatal error will be thrown if the dataset is not in the target pool. +That is, in a channel program running on rpool, +zfs.exists("rpool/nonexistent_fs") returns false, but +zfs.exists("somepool/fs_that_may_exist") will error. +.Pp +dataset (string) +.Bd -ragged -compact -offset "xxxx" +Dataset to check for existence. +Must be in the target pool. +.Ed .It Em zfs.get_prop(dataset, property) Returns two values. First, a string, number or table containing the property value for the given diff --git a/module/zfs/zcp.c b/module/zfs/zcp.c index 07f96e298ddf..83560309b3ad 100644 --- a/module/zfs/zcp.c +++ b/module/zfs/zcp.c @@ -714,7 +714,7 @@ zcp_exists(lua_State *state) return (luaL_error(state, "unexpected error %d", error)); } - return (0); + return (1); } /* diff --git a/tests/runfiles/linux.run b/tests/runfiles/linux.run index 184dd2f9e801..04f0163f3fc4 100644 --- a/tests/runfiles/linux.run +++ b/tests/runfiles/linux.run @@ -63,8 +63,8 @@ tests = ['case_all_values', 'norm_all_values'] tags = ['functional', 'casenorm'] [tests/functional/channel_program/lua_core] -tests = ['tst.args_to_lua', 'tst.divide_by_zero', 'tst.integer_illegal', - 'tst.integer_overflow', 'tst.language_functions_neg', +tests = ['tst.args_to_lua', 'tst.divide_by_zero', 'tst.exists', + 'tst.integer_illegal', 'tst.integer_overflow', 'tst.language_functions_neg', 'tst.language_functions_pos', 'tst.large_prog', 'tst.memory_limit', 'tst.nested_neg', 'tst.nested_pos', 'tst.nvlist_to_lua', 'tst.recursive_neg', 'tst.recursive_pos', 'tst.return_nvlist_neg', diff --git a/tests/zfs-tests/tests/functional/channel_program/lua_core/Makefile.am b/tests/zfs-tests/tests/functional/channel_program/lua_core/Makefile.am index ff65c2c2f8b7..d733acb0b5df 100644 --- a/tests/zfs-tests/tests/functional/channel_program/lua_core/Makefile.am +++ b/tests/zfs-tests/tests/functional/channel_program/lua_core/Makefile.am @@ -8,6 +8,8 @@ dist_pkgdata_SCRIPTS = \ tst.divide_by_zero.err \ tst.divide_by_zero.ksh \ tst.divide_by_zero.zcp \ + tst.exists.ksh \ + tst.exists.zcp \ tst.integer_illegal.ksh \ tst.integer_overflow.ksh \ tst.language_functions_neg.ksh \ diff --git a/tests/zfs-tests/tests/functional/channel_program/lua_core/tst.exists.ksh b/tests/zfs-tests/tests/functional/channel_program/lua_core/tst.exists.ksh new file mode 100755 index 000000000000..e46fe2131589 --- /dev/null +++ b/tests/zfs-tests/tests/functional/channel_program/lua_core/tst.exists.ksh @@ -0,0 +1,45 @@ +#!/bin/ksh -p +# +# 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 of the CDDL is also available via the Internet at +# http://www.illumos.org/license/CDDL. +# + +# +# Copyright (c) 2017 by Delphix. All rights reserved. +# + +. $STF_SUITE/tests/functional/channel_program/channel_common.kshlib + +# +# DESCRIPTION: +# zfs.exists should accurately report whether a dataset exists, and +# report an error if a dataset is in another pool. + +verify_runnable "global" + +# create $TESTSNAP and $TESTCLONE +create_snapshot +create_clone + +function cleanup +{ + datasetexists $TESTPOOL/$TESTFS@$TESTSNAP && \ + log_must zfs destroy -R $TESTPOOL/$TESTFS@$TESTSNAP +} + +log_must_program $TESTPOOL $ZCP_ROOT/lua_core/tst.exists.zcp \ + $TESTPOOL $TESTPOOL/$TESTFS $TESTPOOL/$TESTFS@$TESTSNAP \ + $TESTPOOL/$TESTCLONE + +log_mustnot_checkerr_program "not in the target pool" \ + $TESTPOOL - <<-EOF + return zfs.exists('rpool') +EOF + +log_pass "zfs.exists() gives correct results" diff --git a/tests/zfs-tests/tests/functional/channel_program/lua_core/tst.exists.zcp b/tests/zfs-tests/tests/functional/channel_program/lua_core/tst.exists.zcp new file mode 100644 index 000000000000..e44cf4560502 --- /dev/null +++ b/tests/zfs-tests/tests/functional/channel_program/lua_core/tst.exists.zcp @@ -0,0 +1,26 @@ +-- +-- 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 of the CDDL is also available via the Internet at +-- http://www.illumos.org/license/CDDL. +-- + +-- +-- Copyright (c) 2017 by Delphix. All rights reserved. +-- + +-- ensure zfs.exists works as expected. + +args = ... +argv = args['argv'] +pool = argv[1] + +for i = 1,4 do + assert(zfs.exists(argv[i])) +end + +assert(not zfs.exists(pool .. '/notadataset'))