11d452cf5Sahrens /* 21d452cf5Sahrens * CDDL HEADER START 31d452cf5Sahrens * 41d452cf5Sahrens * The contents of this file are subject to the terms of the 51d452cf5Sahrens * Common Development and Distribution License (the "License"). 61d452cf5Sahrens * You may not use this file except in compliance with the License. 71d452cf5Sahrens * 81d452cf5Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 91d452cf5Sahrens * or http://www.opensolaris.org/os/licensing. 101d452cf5Sahrens * See the License for the specific language governing permissions 111d452cf5Sahrens * and limitations under the License. 121d452cf5Sahrens * 131d452cf5Sahrens * When distributing Covered Code, include this CDDL HEADER in each 141d452cf5Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 151d452cf5Sahrens * If applicable, add the following below this CDDL HEADER, with the 161d452cf5Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 171d452cf5Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 181d452cf5Sahrens * 191d452cf5Sahrens * CDDL HEADER END 201d452cf5Sahrens */ 211d452cf5Sahrens /* 223f9d6ad7SLin Ling * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 237d46dc6cSMatthew Ahrens * Copyright (c) 2012, 2014 by Delphix. All rights reserved. 241d452cf5Sahrens */ 251d452cf5Sahrens 261d452cf5Sahrens #include <sys/dmu.h> 271d452cf5Sahrens #include <sys/dmu_tx.h> 281d452cf5Sahrens #include <sys/dsl_pool.h> 291d452cf5Sahrens #include <sys/dsl_dir.h> 301d452cf5Sahrens #include <sys/dsl_synctask.h> 3118b4d8beSMatthew Ahrens #include <sys/metaslab.h> 321d452cf5Sahrens 331d452cf5Sahrens #define DST_AVG_BLKSHIFT 14 341d452cf5Sahrens 351d452cf5Sahrens /* ARGSUSED */ 361d452cf5Sahrens static int 373b2aab18SMatthew Ahrens dsl_null_checkfunc(void *arg, dmu_tx_t *tx) 381d452cf5Sahrens { 391d452cf5Sahrens return (0); 401d452cf5Sahrens } 411d452cf5Sahrens 423b2aab18SMatthew Ahrens /* 433b2aab18SMatthew Ahrens * Called from open context to perform a callback in syncing context. Waits 443b2aab18SMatthew Ahrens * for the operation to complete. 453b2aab18SMatthew Ahrens * 463b2aab18SMatthew Ahrens * The checkfunc will be called from open context as a preliminary check 473b2aab18SMatthew Ahrens * which can quickly fail. If it succeeds, it will be called again from 483b2aab18SMatthew Ahrens * syncing context. The checkfunc should generally be designed to work 493b2aab18SMatthew Ahrens * properly in either context, but if necessary it can check 503b2aab18SMatthew Ahrens * dmu_tx_is_syncing(tx). 513b2aab18SMatthew Ahrens * 523b2aab18SMatthew Ahrens * The synctask infrastructure enforces proper locking strategy with respect 533b2aab18SMatthew Ahrens * to the dp_config_rwlock -- the lock will always be held when the callbacks 543b2aab18SMatthew Ahrens * are called. It will be held for read during the open-context (preliminary) 553b2aab18SMatthew Ahrens * call to the checkfunc, and then held for write from syncing context during 563b2aab18SMatthew Ahrens * the calls to the check and sync funcs. 573b2aab18SMatthew Ahrens * 583b2aab18SMatthew Ahrens * A dataset or pool name can be passed as the first argument. Typically, 593b2aab18SMatthew Ahrens * the check func will hold, check the return value of the hold, and then 603b2aab18SMatthew Ahrens * release the dataset. The sync func will VERIFYO(hold()) the dataset. 613b2aab18SMatthew Ahrens * This is safe because no changes can be made between the check and sync funcs, 623b2aab18SMatthew Ahrens * and the sync func will only be called if the check func successfully opened 633b2aab18SMatthew Ahrens * the dataset. 643b2aab18SMatthew Ahrens */ 651d452cf5Sahrens int 663b2aab18SMatthew Ahrens dsl_sync_task(const char *pool, dsl_checkfunc_t *checkfunc, 677d46dc6cSMatthew Ahrens dsl_syncfunc_t *syncfunc, void *arg, 687d46dc6cSMatthew Ahrens int blocks_modified, zfs_space_check_t space_check) 691d452cf5Sahrens { 703b2aab18SMatthew Ahrens spa_t *spa; 711d452cf5Sahrens dmu_tx_t *tx; 723b2aab18SMatthew Ahrens int err; 733b2aab18SMatthew Ahrens dsl_sync_task_t dst = { 0 }; 743b2aab18SMatthew Ahrens dsl_pool_t *dp; 753b2aab18SMatthew Ahrens 763b2aab18SMatthew Ahrens err = spa_open(pool, &spa, FTAG); 773b2aab18SMatthew Ahrens if (err != 0) 783b2aab18SMatthew Ahrens return (err); 793b2aab18SMatthew Ahrens dp = spa_get_dsl(spa); 801d452cf5Sahrens 811d452cf5Sahrens top: 823b2aab18SMatthew Ahrens tx = dmu_tx_create_dd(dp->dp_mos_dir); 833b2aab18SMatthew Ahrens VERIFY0(dmu_tx_assign(tx, TXG_WAIT)); 841d452cf5Sahrens 853b2aab18SMatthew Ahrens dst.dst_pool = dp; 863b2aab18SMatthew Ahrens dst.dst_txg = dmu_tx_get_txg(tx); 873b2aab18SMatthew Ahrens dst.dst_space = blocks_modified << DST_AVG_BLKSHIFT; 887d46dc6cSMatthew Ahrens dst.dst_space_check = space_check; 893b2aab18SMatthew Ahrens dst.dst_checkfunc = checkfunc != NULL ? checkfunc : dsl_null_checkfunc; 903b2aab18SMatthew Ahrens dst.dst_syncfunc = syncfunc; 913b2aab18SMatthew Ahrens dst.dst_arg = arg; 923b2aab18SMatthew Ahrens dst.dst_error = 0; 933b2aab18SMatthew Ahrens dst.dst_nowaiter = B_FALSE; 941d452cf5Sahrens 953b2aab18SMatthew Ahrens dsl_pool_config_enter(dp, FTAG); 963b2aab18SMatthew Ahrens err = dst.dst_checkfunc(arg, tx); 973b2aab18SMatthew Ahrens dsl_pool_config_exit(dp, FTAG); 981d452cf5Sahrens 993b2aab18SMatthew Ahrens if (err != 0) { 1001d452cf5Sahrens dmu_tx_commit(tx); 1013b2aab18SMatthew Ahrens spa_close(spa, FTAG); 1023b2aab18SMatthew Ahrens return (err); 1031d452cf5Sahrens } 1041d452cf5Sahrens 1053b2aab18SMatthew Ahrens VERIFY(txg_list_add_tail(&dp->dp_sync_tasks, &dst, dst.dst_txg)); 1061d452cf5Sahrens 1071d452cf5Sahrens dmu_tx_commit(tx); 1081d452cf5Sahrens 1093b2aab18SMatthew Ahrens txg_wait_synced(dp, dst.dst_txg); 1101d452cf5Sahrens 1113b2aab18SMatthew Ahrens if (dst.dst_error == EAGAIN) { 1123b2aab18SMatthew Ahrens txg_wait_synced(dp, dst.dst_txg + TXG_DEFER_SIZE); 1131d452cf5Sahrens goto top; 114b24ab676SJeff Bonwick } 1151d452cf5Sahrens 1163b2aab18SMatthew Ahrens spa_close(spa, FTAG); 1173b2aab18SMatthew Ahrens return (dst.dst_error); 1181d452cf5Sahrens } 1191d452cf5Sahrens 1201d452cf5Sahrens void 1213b2aab18SMatthew Ahrens dsl_sync_task_nowait(dsl_pool_t *dp, dsl_syncfunc_t *syncfunc, void *arg, 1227d46dc6cSMatthew Ahrens int blocks_modified, zfs_space_check_t space_check, dmu_tx_t *tx) 123e7437265Sahrens { 1243b2aab18SMatthew Ahrens dsl_sync_task_t *dst = kmem_zalloc(sizeof (*dst), KM_SLEEP); 125e7437265Sahrens 1263b2aab18SMatthew Ahrens dst->dst_pool = dp; 1273b2aab18SMatthew Ahrens dst->dst_txg = dmu_tx_get_txg(tx); 1283b2aab18SMatthew Ahrens dst->dst_space = blocks_modified << DST_AVG_BLKSHIFT; 1297d46dc6cSMatthew Ahrens dst->dst_space_check = space_check; 1303b2aab18SMatthew Ahrens dst->dst_checkfunc = dsl_null_checkfunc; 1313b2aab18SMatthew Ahrens dst->dst_syncfunc = syncfunc; 1323b2aab18SMatthew Ahrens dst->dst_arg = arg; 1333b2aab18SMatthew Ahrens dst->dst_error = 0; 1343b2aab18SMatthew Ahrens dst->dst_nowaiter = B_TRUE; 1353b2aab18SMatthew Ahrens 1363b2aab18SMatthew Ahrens VERIFY(txg_list_add_tail(&dp->dp_sync_tasks, dst, dst->dst_txg)); 1373b2aab18SMatthew Ahrens } 1383b2aab18SMatthew Ahrens 139495807d7SMatthew Ahrens /* 1403b2aab18SMatthew Ahrens * Called in syncing context to execute the synctask. 141495807d7SMatthew Ahrens */ 142e7437265Sahrens void 1433b2aab18SMatthew Ahrens dsl_sync_task_sync(dsl_sync_task_t *dst, dmu_tx_t *tx) 1441d452cf5Sahrens { 1453b2aab18SMatthew Ahrens dsl_pool_t *dp = dst->dst_pool; 1461d452cf5Sahrens 1473b2aab18SMatthew Ahrens ASSERT0(dst->dst_error); 1481d452cf5Sahrens 1491d452cf5Sahrens /* 1507d46dc6cSMatthew Ahrens * Check for sufficient space. 1517d46dc6cSMatthew Ahrens * 1527d46dc6cSMatthew Ahrens * When the sync task was created, the caller specified the 1537d46dc6cSMatthew Ahrens * type of space checking required. See the comment in 1547d46dc6cSMatthew Ahrens * zfs_space_check_t for details on the semantics of each 1557d46dc6cSMatthew Ahrens * type of space checking. 1567d46dc6cSMatthew Ahrens * 1577d46dc6cSMatthew Ahrens * We just check against what's on-disk; we don't want any 1587d46dc6cSMatthew Ahrens * in-flight accounting to get in our way, because open context 1597d46dc6cSMatthew Ahrens * may have already used up various in-core limits 1607d46dc6cSMatthew Ahrens * (arc_tempreserve, dsl_pool_tempreserve). 1611d452cf5Sahrens */ 1627d46dc6cSMatthew Ahrens if (dst->dst_space_check != ZFS_SPACE_CHECK_NONE) { 1637d46dc6cSMatthew Ahrens uint64_t quota = dsl_pool_adjustedsize(dp, 1647d46dc6cSMatthew Ahrens dst->dst_space_check == ZFS_SPACE_CHECK_RESERVED) - 16518b4d8beSMatthew Ahrens metaslab_class_get_deferred(spa_normal_class(dp->dp_spa)); 166*c1379625SJustin T. Gibbs uint64_t used = dsl_dir_phys(dp->dp_root_dir)->dd_used_bytes; 16718b4d8beSMatthew Ahrens /* MOS space is triple-dittoed, so we multiply by 3. */ 1683b2aab18SMatthew Ahrens if (dst->dst_space > 0 && used + dst->dst_space * 3 > quota) { 169be6fd75aSMatthew Ahrens dst->dst_error = SET_ERROR(ENOSPC); 1703b2aab18SMatthew Ahrens if (dst->dst_nowaiter) 1713b2aab18SMatthew Ahrens kmem_free(dst, sizeof (*dst)); 1721d452cf5Sahrens return; 17318b4d8beSMatthew Ahrens } 1747d46dc6cSMatthew Ahrens } 1751d452cf5Sahrens 1761d452cf5Sahrens /* 1773b2aab18SMatthew Ahrens * Check for errors by calling checkfunc. 1781d452cf5Sahrens */ 1793b2aab18SMatthew Ahrens rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG); 1803b2aab18SMatthew Ahrens dst->dst_error = dst->dst_checkfunc(dst->dst_arg, tx); 1813b2aab18SMatthew Ahrens if (dst->dst_error == 0) 1823b2aab18SMatthew Ahrens dst->dst_syncfunc(dst->dst_arg, tx); 1833b2aab18SMatthew Ahrens rrw_exit(&dp->dp_config_rwlock, FTAG); 1843b2aab18SMatthew Ahrens if (dst->dst_nowaiter) 1853b2aab18SMatthew Ahrens kmem_free(dst, sizeof (*dst)); 186e7437265Sahrens } 187