From f98e1a50b395244a2b9b98c545b382ba18ff70f2 Mon Sep 17 00:00:00 2001 From: Sara Hartse Date: Mon, 3 Feb 2020 15:06:13 -0600 Subject: 12308 Support setting user properties in a channel program Portions contributed by: Jason King Reviewed by: Brian Behlendorf Reviewed by: Matt Ahrens Reviewed by: C Fraire Reviewed by: Jerry Jelinek --- usr/src/man/man1m/zfs-program.1m | 25 ++++- usr/src/pkg/manifests/system-test-zfstest.mf | 6 ++ .../synctask_core/tst.set_props.ksh | 40 ++++++++ .../synctask_core/tst.set_props.zcp | 109 +++++++++++++++++++++ usr/src/uts/common/Makefile.files | 1 + usr/src/uts/common/fs/zfs/sys/zcp_set.h | 48 +++++++++ usr/src/uts/common/fs/zfs/zcp_set.c | 100 +++++++++++++++++++ usr/src/uts/common/fs/zfs/zcp_synctask.c | 41 ++++++++ 8 files changed, 368 insertions(+), 2 deletions(-) create mode 100644 usr/src/test/zfs-tests/tests/functional/channel_program/synctask_core/tst.set_props.ksh create mode 100644 usr/src/test/zfs-tests/tests/functional/channel_program/synctask_core/tst.set_props.zcp create mode 100644 usr/src/uts/common/fs/zfs/sys/zcp_set.h create mode 100644 usr/src/uts/common/fs/zfs/zcp_set.c diff --git a/usr/src/man/man1m/zfs-program.1m b/usr/src/man/man1m/zfs-program.1m index 12802319b0..99fa887aa8 100644 --- a/usr/src/man/man1m/zfs-program.1m +++ b/usr/src/man/man1m/zfs-program.1m @@ -12,7 +12,7 @@ .\" Copyright (c) 2018 Datto Inc. .\" Copyright 2020 Joyent, Inc. .\" -.Dd January 15, 2020 +.Dd February 3, 2020 .Dt ZFS-PROGRAM 1M .Os .Sh NAME @@ -156,7 +156,7 @@ can guarantee that it will finish successfully against a similar size system. If a channel program attempts to return too large a value, the program will fully execute but exit with a nonzero status code and no return value. .Pp -.Em Note: +.Em Note : ZFS API functions do not generate Fatal Errors when correctly invoked, they return an error code and the channel program continues executing. See the @@ -409,6 +409,26 @@ filesystem (string) .Bd -ragged -compact -offset "xxxx" Filesystem to rollback. .Ed +.It Em zfs.sync.set_prop(dataset, property, value) +Sets the given property on a dataset. +Currently only user properties are supported. +Returns 0 if the property was set, or a nonzero error code otherwise. +.Pp +dataset (string) +.Bd -ragged -compact -offset "xxxx" +The dataset where the property will be set. +.Ed +.Pp +property (string) +.Bd -ragged -compact -offset "xxxx" +The property to set. +Only user properties are supported. +.Ed +.Pp +value (string) +.Bd -ragged -compact -offset "xxxx" +The value of the property to be set. +.Ed .It Em zfs.sync.snapshot(dataset) Create a snapshot of a filesystem. Returns 0 if the snapshot was successfully created, @@ -441,6 +461,7 @@ The available zfs.check functions are: .It Em zfs.check.destroy(dataset, [defer=true|false]) .It Em zfs.check.promote(dataset) .It Em zfs.check.rollback(filesystem) +.It Em zfs.check.set_property(dataset, property, value) .It Em zfs.check.snapshot(dataset) .El .It Sy zfs.list submodule diff --git a/usr/src/pkg/manifests/system-test-zfstest.mf b/usr/src/pkg/manifests/system-test-zfstest.mf index 10436a8fdf..fd373bc8ca 100644 --- a/usr/src/pkg/manifests/system-test-zfstest.mf +++ b/usr/src/pkg/manifests/system-test-zfstest.mf @@ -637,6 +637,12 @@ file \ file \ path=opt/zfs-tests/tests/functional/channel_program/synctask_core/tst.rollback_one \ mode=0555 +file \ + path=opt/zfs-tests/tests/functional/channel_program/synctask_core/tst.set_props \ + mode=0555 +file \ + path=opt/zfs-tests/tests/functional/channel_program/synctask_core/tst.set_props.zcp \ + mode=0444 file \ path=opt/zfs-tests/tests/functional/channel_program/synctask_core/tst.snapshot_destroy \ mode=0555 diff --git a/usr/src/test/zfs-tests/tests/functional/channel_program/synctask_core/tst.set_props.ksh b/usr/src/test/zfs-tests/tests/functional/channel_program/synctask_core/tst.set_props.ksh new file mode 100644 index 0000000000..062b2ab579 --- /dev/null +++ b/usr/src/test/zfs-tests/tests/functional/channel_program/synctask_core/tst.set_props.ksh @@ -0,0 +1,40 @@ +#!/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) 2016 by Delphix. All rights reserved. +# + +. $STF_SUITE/tests/functional/channel_program/channel_common.kshlib + +# +# DESCRIPTION: +# Getting number props should work correctly on filesystems, +# snapshots and volumes. +# + +verify_runnable "global" + +fs=$TESTPOOL/$TESTFS/testchild + +function cleanup +{ + datasetexists $fs && log_must zfs destroy -R $fs +} + +log_onexit cleanup + +log_must zfs create $fs + +log_must_program_sync $TESTPOOL $ZCP_ROOT/synctask_core/tst.set_props.zcp $fs + +log_pass "Setting props from channel program works correctly." diff --git a/usr/src/test/zfs-tests/tests/functional/channel_program/synctask_core/tst.set_props.zcp b/usr/src/test/zfs-tests/tests/functional/channel_program/synctask_core/tst.set_props.zcp new file mode 100644 index 0000000000..6d2a92d6b2 --- /dev/null +++ b/usr/src/test/zfs-tests/tests/functional/channel_program/synctask_core/tst.set_props.zcp @@ -0,0 +1,109 @@ +-- +-- 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. +-- Copyright 2020 Joyent, Inc. +-- + +arg = ... +fs = arg["argv"][1] + +-- values from zfs.h +maxname = 256 -- ZAP_MAXNAMELEN +maxvalue = 8192 -- ZAP_MAXVALUELEN + +pos_props = {} +neg_props = {} + +-- In lua, strings are immutable, so to avoid a bunch of copies, we +-- build the value in a table and use concat (which appears to be the +-- recommended method for such things). +largeprop = {} +for i = 0,maxvalue,8 +do + table.insert(largeprop, "aaaaaaaa") +end +-- add an extra character so we spill over the limit +table.insert(largeprop, "b") + +largepropv = table.concat(largeprop) + +largepropname = { "b:" } +for i = 0,maxname,8 +do + table.insert(largepropname, "aaaaaaaa") +end +largepropnamev = table.concat(largepropname) + +pos_props["a:prop"] = {"hello"} + +-- For neg_props, an optional expected error value can be added after the +-- property value as seen below. +neg_props["notaproperty"] = {"hello", EINVAL} +neg_props["a:very.long.property.value"] = { largepropv, E2BIG } +neg_props[largepropnamev] = {"greetings", ENAMETOOLONG } + +-- non-user properties aren't currently supported +-- Even if they were, the argument must be a string due to requirements of +-- the ZCP api. +neg_props["mountpoint"] = {"/foo/bar"} +neg_props["copies"] = { "2" } + +-- read-only properties should never succeed +neg_props["guid"] = { "12345" } + +set_fail = {} +val_fail = {} + +-- Test properties that should work +for prop, values in pairs(pos_props) do + for i, val in ipairs(values) do + old_val, src = zfs.get_prop(fs, prop) + + -- Attempt to set the property to the specified value + err = zfs.sync.set_prop(fs, prop, val) + + if (err ~= 0) then + set_fail[prop] = err -- tuple of prop, val that resulted in error + else + -- use get_prop to check that the set took effect + new_val, src = zfs.get_prop(fs, prop) + if (tostring(new_val) ~= tostring(val)) then + val_fail[prop] = new_val + end + + -- We modified the prop, restore old value (if one existed) + if (old_val ~= nil) then + err = zfs.sync.set_prop(fs, prop, old_val) + if (err ~= 0) then return err end + else + -- Didn't have an old value, delete (inherit) instead + err = zfs.sync.inherit(fs, prop) + if (err ~= 0) then return err end + end + end + end +end + +-- Test properties that should fail +for prop, expected in pairs(neg_props) do + exp_val = expected[1] + exp_err = expected[2] + + -- Attempt to set the property to the specified value + err = zfs.sync.set_prop(fs, prop, exp_val) + if (err == 0 or (exp_err ~= nil and err ~= exp_err)) then + set_fail[prop] = err -- tuple of prop, val that resulted in error + end +end + +return {set_fail, val_fail} diff --git a/usr/src/uts/common/Makefile.files b/usr/src/uts/common/Makefile.files index 5911b561c9..060036e676 100644 --- a/usr/src/uts/common/Makefile.files +++ b/usr/src/uts/common/Makefile.files @@ -1442,6 +1442,7 @@ ZFS_COMMON_OBJS += \ zcp.o \ zcp_change_key.o \ zcp_get.o \ + zcp_set.o \ zcp_global.o \ zcp_iter.o \ zcp_synctask.o \ diff --git a/usr/src/uts/common/fs/zfs/sys/zcp_set.h b/usr/src/uts/common/fs/zfs/sys/zcp_set.h new file mode 100644 index 0000000000..3a31da8a05 --- /dev/null +++ b/usr/src/uts/common/fs/zfs/sys/zcp_set.h @@ -0,0 +1,48 @@ +/* + * 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 of the CDDL is also available via the Internet at + * http://www.illumos.org/license/CDDL. + * + * CDDL HEADER END + */ + +/* + * Copyright 2019 Joyent, Inc. + */ + +#ifndef _SYS_ZCP_SET_H +#define _SYS_ZCP_SET_H + +#include +#include + +#include "lua.h" +#include "lualib.h" +#include "lauxlib.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct zcp_set_prop_arg { + lua_State *state; + const char *dsname; + const char *prop; + const char *val; +} zcp_set_prop_arg_t; + +int zcp_set_prop_check(void *arg, dmu_tx_t *tx); +void zcp_set_prop_sync(void *arg, dmu_tx_t *tx); + +#ifdef __cplusplus +} +#endif + +#endif /* _SYS_ZCP_SET_H */ diff --git a/usr/src/uts/common/fs/zfs/zcp_set.c b/usr/src/uts/common/fs/zfs/zcp_set.c new file mode 100644 index 0000000000..17582421c3 --- /dev/null +++ b/usr/src/uts/common/fs/zfs/zcp_set.c @@ -0,0 +1,100 @@ +/* + * 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 of the CDDL is also available via the Internet at + * http://www.illumos.org/license/CDDL. + * + * CDDL HEADER END + */ + +/* + * Copyright (c) 2016 by Delphix. All rights reserved. + * Copyrigh 2020 Joyent, Inc. + */ + +#include "lua.h" +#include "lualib.h" +#include "lauxlib.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +static void +zcp_set_user_prop(lua_State *state, dsl_pool_t *dp, const char *dsname, + const char *prop_name, const char *prop_val, dmu_tx_t *tx) +{ + dsl_dataset_t *ds = zcp_dataset_hold(state, dp, dsname, FTAG); + if (ds == NULL) + return; /* not reached; zcp_dataset_hold() longjmp'd */ + + nvlist_t *nvl = fnvlist_alloc(); + fnvlist_add_string(nvl, prop_name, prop_val); + + dsl_props_set_sync_impl(ds, ZPROP_SRC_LOCAL, nvl, tx); + + fnvlist_free(nvl); + dsl_dataset_rele(ds, FTAG); +} + +int +zcp_set_prop_check(void *arg, dmu_tx_t *tx) +{ + zcp_set_prop_arg_t *args = arg; + const char *prop_name = args->prop; + dsl_props_set_arg_t dpsa = { + .dpsa_dsname = args->dsname, + .dpsa_source = ZPROP_SRC_LOCAL, + }; + nvlist_t *nvl = NULL; + int ret = 0; + + /* + * Only user properties are currently supported. When non-user + * properties are supported, we will want to use + * zfs_valid_proplist() to verify the properties. + */ + if (!zfs_prop_user(prop_name)) { + return (EINVAL); + } + + nvl = fnvlist_alloc(); + fnvlist_add_string(nvl, args->prop, args->val); + dpsa.dpsa_props = nvl; + + ret = dsl_props_set_check(&dpsa, tx); + nvlist_free(nvl); + + return (ret); +} + +void +zcp_set_prop_sync(void *arg, dmu_tx_t *tx) +{ + zcp_set_prop_arg_t *args = arg; + zcp_run_info_t *ri = zcp_run_info(args->state); + dsl_pool_t *dp = ri->zri_pool; + + const char *dsname = args->dsname; + const char *prop_name = args->prop; + const char *prop_val = args->val; + + if (zfs_prop_user(prop_name)) { + zcp_set_user_prop(args->state, dp, dsname, prop_name, + prop_val, tx); + } +} diff --git a/usr/src/uts/common/fs/zfs/zcp_synctask.c b/usr/src/uts/common/fs/zfs/zcp_synctask.c index 2dd7a80da6..5c04fd4368 100644 --- a/usr/src/uts/common/fs/zfs/zcp_synctask.c +++ b/usr/src/uts/common/fs/zfs/zcp_synctask.c @@ -22,8 +22,10 @@ #include "lauxlib.h" #include +#include #include #include +#include #include #include #include @@ -430,6 +432,44 @@ done: return (err); } +static int zcp_synctask_set_prop(lua_State *, boolean_t, nvlist_t *err_details); +static zcp_synctask_info_t zcp_synctask_set_prop_info = { + .name = "set_prop", + .func = zcp_synctask_set_prop, + .space_check = ZFS_SPACE_CHECK_RESERVED, + .blocks_modified = 2, + .pargs = { + { .za_name = "dataset", .za_lua_type = LUA_TSTRING}, + { .za_name = "property", .za_lua_type = LUA_TSTRING}, + { .za_name = "value", .za_lua_type = LUA_TSTRING}, + { NULL, 0 } + }, + .kwargs = { + { NULL, 0 } + } +}; + +static int +zcp_synctask_set_prop(lua_State *state, boolean_t sync, nvlist_t *err_details) +{ + int err; + zcp_set_prop_arg_t args = { 0 }; + + const char *dsname = lua_tostring(state, 1); + const char *prop = lua_tostring(state, 2); + const char *val = lua_tostring(state, 3); + + args.state = state; + args.dsname = dsname; + args.prop = prop; + args.val = val; + + err = zcp_sync_task(state, zcp_set_prop_check, zcp_set_prop_sync, + &args, sync, dsname); + + return (err); +} + static int zcp_synctask_wrapper(lua_State *state) { @@ -499,6 +539,7 @@ zcp_load_synctask_lib(lua_State *state, boolean_t sync) &zcp_synctask_snapshot_info, &zcp_synctask_inherit_prop_info, &zcp_synctask_change_key_info, + &zcp_synctask_set_prop_info, NULL }; -- cgit v1.2.3