Path: blob/main/sys/contrib/openzfs/module/zfs/dsl_synctask.c
48383 views
// SPDX-License-Identifier: CDDL-1.01/*2* CDDL HEADER START3*4* The contents of this file are subject to the terms of the5* Common Development and Distribution License (the "License").6* You may not use this file except in compliance with the License.7*8* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE9* or https://opensource.org/licenses/CDDL-1.0.10* See the License for the specific language governing permissions11* and limitations under the License.12*13* When distributing Covered Code, include this CDDL HEADER in each14* file and include the License file at usr/src/OPENSOLARIS.LICENSE.15* If applicable, add the following below this CDDL HEADER, with the16* fields enclosed by brackets "[]" replaced with your own identifying17* information: Portions Copyright [yyyy] [name of copyright owner]18*19* CDDL HEADER END20*/21/*22* Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.23* Copyright (c) 2012, 2017 by Delphix. All rights reserved.24*/2526#include <sys/dmu.h>27#include <sys/dmu_tx.h>28#include <sys/dsl_pool.h>29#include <sys/dsl_dir.h>30#include <sys/dsl_synctask.h>31#include <sys/metaslab.h>3233#define DST_AVG_BLKSHIFT 143435static int36dsl_null_checkfunc(void *arg, dmu_tx_t *tx)37{38(void) arg, (void) tx;39return (0);40}4142static int43dsl_sync_task_common(const char *pool, dsl_checkfunc_t *checkfunc,44dsl_syncfunc_t *syncfunc, dsl_sigfunc_t *sigfunc, void *arg,45int blocks_modified, zfs_space_check_t space_check, boolean_t early)46{47spa_t *spa;48dmu_tx_t *tx;49int err;50dsl_sync_task_t dst = { { { NULL } } };51dsl_pool_t *dp;5253err = spa_open(pool, &spa, FTAG);54if (err != 0)55return (err);56dp = spa_get_dsl(spa);5758top:59tx = dmu_tx_create_dd(dp->dp_mos_dir);60VERIFY0(dmu_tx_assign(tx, DMU_TX_WAIT | DMU_TX_SUSPEND));6162dst.dst_pool = dp;63dst.dst_txg = dmu_tx_get_txg(tx);64dst.dst_space = blocks_modified << DST_AVG_BLKSHIFT;65dst.dst_space_check = space_check;66dst.dst_checkfunc = checkfunc != NULL ? checkfunc : dsl_null_checkfunc;67dst.dst_syncfunc = syncfunc;68dst.dst_arg = arg;69dst.dst_error = 0;70dst.dst_nowaiter = B_FALSE;7172dsl_pool_config_enter(dp, FTAG);73err = dst.dst_checkfunc(arg, tx);74dsl_pool_config_exit(dp, FTAG);7576if (err != 0) {77dmu_tx_commit(tx);78spa_close(spa, FTAG);79return (err);80}8182txg_list_t *task_list = (early) ?83&dp->dp_early_sync_tasks : &dp->dp_sync_tasks;84VERIFY(txg_list_add_tail(task_list, &dst, dst.dst_txg));8586dmu_tx_commit(tx);8788if (sigfunc != NULL) {89err = txg_wait_synced_flags(dp, dst.dst_txg, TXG_WAIT_SIGNAL);90if (err != 0) {91VERIFY3U(err, ==, EINTR);92/* current contract is to call func once */93sigfunc(arg, tx);94/* in case we're performing an EAGAIN retry */95sigfunc = NULL;9697txg_wait_synced(dp, dst.dst_txg);98}99} else100txg_wait_synced(dp, dst.dst_txg);101102if (dst.dst_error == EAGAIN) {103txg_wait_synced(dp, dst.dst_txg + TXG_DEFER_SIZE);104goto top;105}106107spa_close(spa, FTAG);108return (dst.dst_error);109}110111/*112* Called from open context to perform a callback in syncing context. Waits113* for the operation to complete.114*115* The checkfunc will be called from open context as a preliminary check116* which can quickly fail. If it succeeds, it will be called again from117* syncing context. The checkfunc should generally be designed to work118* properly in either context, but if necessary it can check119* dmu_tx_is_syncing(tx).120*121* The synctask infrastructure enforces proper locking strategy with respect122* to the dp_config_rwlock -- the lock will always be held when the callbacks123* are called. It will be held for read during the open-context (preliminary)124* call to the checkfunc, and then held for write from syncing context during125* the calls to the check and sync funcs.126*127* A dataset or pool name can be passed as the first argument. Typically,128* the check func will hold, check the return value of the hold, and then129* release the dataset. The sync func will VERIFYO(hold()) the dataset.130* This is safe because no changes can be made between the check and sync funcs,131* and the sync func will only be called if the check func successfully opened132* the dataset.133*/134int135dsl_sync_task(const char *pool, dsl_checkfunc_t *checkfunc,136dsl_syncfunc_t *syncfunc, void *arg,137int blocks_modified, zfs_space_check_t space_check)138{139return (dsl_sync_task_common(pool, checkfunc, syncfunc, NULL, arg,140blocks_modified, space_check, B_FALSE));141}142143/*144* An early synctask works exactly as a standard synctask with one important145* difference on the way it is handled during syncing context. Standard146* synctasks run after we've written out all the dirty blocks of dirty147* datasets. Early synctasks are executed before writing out any dirty data,148* and thus before standard synctasks.149*150* For that reason, early synctasks can affect the process of writing dirty151* changes to disk for the txg that they run and should be used with caution.152* In addition, early synctasks should not dirty any metaslabs as this would153* invalidate the precondition/invariant for subsequent early synctasks.154* [see dsl_pool_sync() and dsl_early_sync_task_verify()]155*/156int157dsl_early_sync_task(const char *pool, dsl_checkfunc_t *checkfunc,158dsl_syncfunc_t *syncfunc, void *arg,159int blocks_modified, zfs_space_check_t space_check)160{161return (dsl_sync_task_common(pool, checkfunc, syncfunc, NULL, arg,162blocks_modified, space_check, B_TRUE));163}164165/*166* A standard synctask that can be interrupted from a signal. The sigfunc167* is called once if a signal occurred while waiting for the task to sync.168*/169int170dsl_sync_task_sig(const char *pool, dsl_checkfunc_t *checkfunc,171dsl_syncfunc_t *syncfunc, dsl_sigfunc_t *sigfunc, void *arg,172int blocks_modified, zfs_space_check_t space_check)173{174return (dsl_sync_task_common(pool, checkfunc, syncfunc, sigfunc, arg,175blocks_modified, space_check, B_FALSE));176}177178static void179dsl_sync_task_nowait_common(dsl_pool_t *dp, dsl_syncfunc_t *syncfunc, void *arg,180dmu_tx_t *tx, boolean_t early)181{182dsl_sync_task_t *dst = kmem_zalloc(sizeof (*dst), KM_SLEEP);183184dst->dst_pool = dp;185dst->dst_txg = dmu_tx_get_txg(tx);186dst->dst_space_check = ZFS_SPACE_CHECK_NONE;187dst->dst_checkfunc = dsl_null_checkfunc;188dst->dst_syncfunc = syncfunc;189dst->dst_arg = arg;190dst->dst_error = 0;191dst->dst_nowaiter = B_TRUE;192193txg_list_t *task_list = (early) ?194&dp->dp_early_sync_tasks : &dp->dp_sync_tasks;195VERIFY(txg_list_add_tail(task_list, dst, dst->dst_txg));196}197198void199dsl_sync_task_nowait(dsl_pool_t *dp, dsl_syncfunc_t *syncfunc, void *arg,200dmu_tx_t *tx)201{202dsl_sync_task_nowait_common(dp, syncfunc, arg, tx, B_FALSE);203}204205void206dsl_early_sync_task_nowait(dsl_pool_t *dp, dsl_syncfunc_t *syncfunc, void *arg,207dmu_tx_t *tx)208{209dsl_sync_task_nowait_common(dp, syncfunc, arg, tx, B_TRUE);210}211212/*213* Called in syncing context to execute the synctask.214*/215void216dsl_sync_task_sync(dsl_sync_task_t *dst, dmu_tx_t *tx)217{218dsl_pool_t *dp = dst->dst_pool;219220ASSERT0(dst->dst_error);221222/*223* Check for sufficient space.224*225* When the sync task was created, the caller specified the226* type of space checking required. See the comment in227* zfs_space_check_t for details on the semantics of each228* type of space checking.229*230* We just check against what's on-disk; we don't want any231* in-flight accounting to get in our way, because open context232* may have already used up various in-core limits233* (arc_tempreserve, dsl_pool_tempreserve).234*/235if (dst->dst_space_check != ZFS_SPACE_CHECK_NONE) {236uint64_t quota = dsl_pool_unreserved_space(dp,237dst->dst_space_check);238uint64_t used = dsl_dir_phys(dp->dp_root_dir)->dd_used_bytes;239240/* MOS space is triple-dittoed, so we multiply by 3. */241if (used + dst->dst_space * 3 > quota) {242dst->dst_error = SET_ERROR(ENOSPC);243if (dst->dst_nowaiter)244kmem_free(dst, sizeof (*dst));245return;246}247}248249/*250* Check for errors by calling checkfunc.251*/252rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG);253dst->dst_error = dst->dst_checkfunc(dst->dst_arg, tx);254if (dst->dst_error == 0)255dst->dst_syncfunc(dst->dst_arg, tx);256rrw_exit(&dp->dp_config_rwlock, FTAG);257if (dst->dst_nowaiter)258kmem_free(dst, sizeof (*dst));259}260261#if defined(_KERNEL)262EXPORT_SYMBOL(dsl_sync_task);263EXPORT_SYMBOL(dsl_sync_task_nowait);264#endif265266267