From e60e158eff920825311c1e18b3631876eaaacb54 Mon Sep 17 00:00:00 2001 From: John Gallagher Date: Fri, 13 Sep 2019 18:09:06 -0700 Subject: Add subcommand to wait for background zfs activity to complete Currently the best way to wait for the completion of a long-running operation in a pool, like a scrub or device removal, is to poll 'zpool status' and parse its output, which is neither efficient nor convenient. This change adds a 'wait' subcommand to the zpool command. When invoked, 'zpool wait' will block until a specified type of background activity completes. Currently, this subcommand can wait for any of the following: - Scrubs or resilvers to complete - Devices to initialized - Devices to be replaced - Devices to be removed - Checkpoints to be discarded - Background freeing to complete For example, a scrub that is in progress could be waited for by running zpool wait -t scrub This also adds a -w flag to the attach, checkpoint, initialize, replace, remove, and scrub subcommands. When used, this flag makes the operations kicked off by these subcommands synchronous instead of asynchronous. This functionality is implemented using a new ioctl. The type of activity to wait for is provided as input to the ioctl, and the ioctl blocks until all activity of that type has completed. An ioctl was used over other methods of kernel-userspace communiction primarily for the sake of portability. Porting Notes: This is ported from Delphix OS change DLPX-44432. The following changes were made while porting: - Added ZoL-style ioctl input declaration. - Reorganized error handling in zpool_initialize in libzfs to integrate better with changes made for TRIM support. - Fixed check for whether a checkpoint discard is in progress. Previously it also waited if the pool had a checkpoint, instead of just if a checkpoint was being discarded. - Exposed zfs_initialize_chunk_size as a ZoL-style tunable. - Updated more existing tests to make use of new 'zpool wait' functionality, tests that don't exist in Delphix OS. - Used existing ZoL tunable zfs_scan_suspend_progress, together with zinject, in place of a new tunable zfs_scan_max_blks_per_txg. - Added support for a non-integral interval argument to zpool wait. Future work: ZoL has support for trimming devices, which Delphix OS does not. In the future, 'zpool wait' could be extended to add the ability to wait for trim operations to complete. Reviewed-by: Matt Ahrens Reviewed-by: John Kennedy Reviewed-by: Brian Behlendorf Signed-off-by: John Gallagher Closes #9162 --- module/zfs/zfs_ioctl.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) (limited to 'module/zfs/zfs_ioctl.c') diff --git a/module/zfs/zfs_ioctl.c b/module/zfs/zfs_ioctl.c index c5093fd44..fce074147 100644 --- a/module/zfs/zfs_ioctl.c +++ b/module/zfs/zfs_ioctl.c @@ -4069,6 +4069,56 @@ zfs_ioc_pool_trim(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl) return (total_errors > 0 ? EINVAL : 0); } +/* + * This ioctl waits for activity of a particular type to complete. If there is + * no activity of that type in progress, it returns immediately, and the + * returned value "waited" is false. If there is activity in progress, and no + * tag is passed in, the ioctl blocks until all activity of that type is + * complete, and then returns with "waited" set to true. + * + * If a tag is provided, it identifies a particular instance of an activity to + * wait for. Currently, this is only valid for use with 'initialize', because + * that is the only activity for which there can be multiple instances running + * concurrently. In the case of 'initialize', the tag corresponds to the guid of + * the vdev on which to wait. + * + * If a thread waiting in the ioctl receives a signal, the call will return + * immediately, and the return value will be EINTR. + * + * innvl: { + * "wait_activity" -> int32_t + * (optional) "wait_tag" -> uint64_t + * } + * + * outnvl: "waited" -> boolean_t + */ +static const zfs_ioc_key_t zfs_keys_pool_wait[] = { + {ZPOOL_WAIT_ACTIVITY, DATA_TYPE_INT32, 0}, + {ZPOOL_WAIT_TAG, DATA_TYPE_UINT64, ZK_OPTIONAL}, +}; + +static int +zfs_ioc_wait(const char *name, nvlist_t *innvl, nvlist_t *outnvl) +{ + int32_t activity; + uint64_t tag; + boolean_t waited; + int error; + + if (nvlist_lookup_int32(innvl, ZPOOL_WAIT_ACTIVITY, &activity) != 0) + return (EINVAL); + + if (nvlist_lookup_uint64(innvl, ZPOOL_WAIT_TAG, &tag) == 0) + error = spa_wait_tag(name, activity, tag, &waited); + else + error = spa_wait(name, activity, &waited); + + if (error == 0) + fnvlist_add_boolean_value(outnvl, ZPOOL_WAIT_WAITED, waited); + + return (error); +} + /* * fsname is name of dataset to rollback (to most recent snapshot) * @@ -6894,6 +6944,11 @@ zfs_ioctl_init(void) POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE, zfs_keys_pool_trim, ARRAY_SIZE(zfs_keys_pool_trim)); + zfs_ioctl_register("wait", ZFS_IOC_WAIT, + zfs_ioc_wait, zfs_secpolicy_none, POOL_NAME, + POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE, + zfs_keys_pool_wait, ARRAY_SIZE(zfs_keys_pool_wait)); + /* IOCTLS that use the legacy function signature */ zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE, zfs_ioc_pool_freeze, -- cgit v1.2.3