dsl_pool.c revision 3b2aab18808792cbd248a12f1edf139b89833c13
fa9e4066f08beec538e775443c5be79dd423fcabahrens/*
fa9e4066f08beec538e775443c5be79dd423fcabahrens * CDDL HEADER START
fa9e4066f08beec538e775443c5be79dd423fcabahrens *
fa9e4066f08beec538e775443c5be79dd423fcabahrens * The contents of this file are subject to the terms of the
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock * Common Development and Distribution License (the "License").
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock * You may not use this file except in compliance with the License.
fa9e4066f08beec538e775443c5be79dd423fcabahrens *
fa9e4066f08beec538e775443c5be79dd423fcabahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
fa9e4066f08beec538e775443c5be79dd423fcabahrens * or http://www.opensolaris.org/os/licensing.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * See the License for the specific language governing permissions
fa9e4066f08beec538e775443c5be79dd423fcabahrens * and limitations under the License.
fa9e4066f08beec538e775443c5be79dd423fcabahrens *
fa9e4066f08beec538e775443c5be79dd423fcabahrens * When distributing Covered Code, include this CDDL HEADER in each
fa9e4066f08beec538e775443c5be79dd423fcabahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * If applicable, add the following below this CDDL HEADER, with the
fa9e4066f08beec538e775443c5be79dd423fcabahrens * fields enclosed by brackets "[]" replaced with your own identifying
fa9e4066f08beec538e775443c5be79dd423fcabahrens * information: Portions Copyright [yyyy] [name of copyright owner]
fa9e4066f08beec538e775443c5be79dd423fcabahrens *
fa9e4066f08beec538e775443c5be79dd423fcabahrens * CDDL HEADER END
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
fa9e4066f08beec538e775443c5be79dd423fcabahrens/*
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Ling * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden * Copyright (c) 2012 by Delphix. All rights reserved.
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <sys/dsl_pool.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <sys/dsl_dataset.h>
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Ling#include <sys/dsl_prop.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <sys/dsl_dir.h>
1d452cf5123cb6ac0a013a4dbd4dcceeb0da314dahrens#include <sys/dsl_synctask.h>
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Ling#include <sys/dsl_scan.h>
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Ling#include <sys/dnode.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <sys/dmu_tx.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <sys/dmu_objset.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <sys/arc.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <sys/zap.h>
c717a56157ae0e6fca6a1e3689ae1edc385716a3maybee#include <sys/zio.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <sys/zfs_context.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <sys/fs/zfs.h>
088f389458728c464569a5506b58070254fa4f7dahrens#include <sys/zfs_znode.h>
088f389458728c464569a5506b58070254fa4f7dahrens#include <sys/spa_impl.h>
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens#include <sys/dsl_deadlist.h>
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden#include <sys/bptree.h>
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden#include <sys/zfeature.h>
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens#include <sys/zil_impl.h>
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens#include <sys/dsl_userhold.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybeeint zfs_no_write_throttle = 0;
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybeeint zfs_write_limit_shift = 3; /* 1/8th of physical memory */
44ecc5327ab4ce0750dcca2a17e05566bf2812e2George Wilsonint zfs_txg_synctime_ms = 1000; /* target millisecs to sync a txg */
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybeeuint64_t zfs_write_limit_min = 32 << 20; /* min write limit is 32MB */
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybeeuint64_t zfs_write_limit_max = 0; /* max data payload per txg */
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybeeuint64_t zfs_write_limit_inflated = 0;
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybeeuint64_t zfs_write_limit_override = 0;
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybeekmutex_t zfs_write_limit_lock;
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybeestatic pgcnt_t old_physmem = 0;
088f389458728c464569a5506b58070254fa4f7dahrens
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Lingint
088f389458728c464569a5506b58070254fa4f7dahrensdsl_pool_open_special_dir(dsl_pool_t *dp, const char *name, dsl_dir_t **ddp)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens uint64_t obj;
fa9e4066f08beec538e775443c5be79dd423fcabahrens int err;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens err = zap_lookup(dp->dp_meta_objset,
fa9e4066f08beec538e775443c5be79dd423fcabahrens dp->dp_root_dir->dd_phys->dd_child_dir_zapobj,
088f389458728c464569a5506b58070254fa4f7dahrens name, sizeof (obj), 1, &obj);
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock if (err)
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock return (err);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (dsl_dir_hold_obj(dp, obj, name, dp, ddp));
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrensstatic dsl_pool_t *
fa9e4066f08beec538e775443c5be79dd423fcabahrensdsl_pool_open_impl(spa_t *spa, uint64_t txg)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens dsl_pool_t *dp;
fa9e4066f08beec538e775443c5be79dd423fcabahrens blkptr_t *bp = spa_get_rootblkptr(spa);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens dp = kmem_zalloc(sizeof (dsl_pool_t), KM_SLEEP);
fa9e4066f08beec538e775443c5be79dd423fcabahrens dp->dp_spa = spa;
fa9e4066f08beec538e775443c5be79dd423fcabahrens dp->dp_meta_rootbp = *bp;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens rrw_init(&dp->dp_config_rwlock, B_TRUE);
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee dp->dp_write_limit = zfs_write_limit_min;
fa9e4066f08beec538e775443c5be79dd423fcabahrens txg_init(dp, txg);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens txg_list_create(&dp->dp_dirty_datasets,
fa9e4066f08beec538e775443c5be79dd423fcabahrens offsetof(dsl_dataset_t, ds_dirty_link));
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens txg_list_create(&dp->dp_dirty_zilogs,
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens offsetof(zilog_t, zl_dirty_link));
fa9e4066f08beec538e775443c5be79dd423fcabahrens txg_list_create(&dp->dp_dirty_dirs,
fa9e4066f08beec538e775443c5be79dd423fcabahrens offsetof(dsl_dir_t, dd_dirty_link));
1d452cf5123cb6ac0a013a4dbd4dcceeb0da314dahrens txg_list_create(&dp->dp_sync_tasks,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens offsetof(dsl_sync_task_t, dst_node));
fa9e4066f08beec538e775443c5be79dd423fcabahrens
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee mutex_init(&dp->dp_lock, NULL, MUTEX_DEFAULT, NULL);
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee
9d3574bff0b382b89dd380c1e23d742ebc156772Neil Perrin dp->dp_vnrele_taskq = taskq_create("zfs_vn_rele_taskq", 1, minclsyspri,
9d3574bff0b382b89dd380c1e23d742ebc156772Neil Perrin 1, 4, 0);
9d3574bff0b382b89dd380c1e23d742ebc156772Neil Perrin
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (dp);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrockint
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Sidendsl_pool_init(spa_t *spa, uint64_t txg, dsl_pool_t **dpp)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens int err;
fa9e4066f08beec538e775443c5be79dd423fcabahrens dsl_pool_t *dp = dsl_pool_open_impl(spa, txg);
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden err = dmu_objset_open_impl(spa, NULL, &dp->dp_meta_rootbp,
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden &dp->dp_meta_objset);
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden if (err != 0)
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden dsl_pool_close(dp);
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden else
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden *dpp = dp;
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden return (err);
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden}
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Sidenint
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Sidendsl_pool_open(dsl_pool_t *dp)
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden{
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden int err;
088f389458728c464569a5506b58070254fa4f7dahrens dsl_dir_t *dd;
088f389458728c464569a5506b58070254fa4f7dahrens dsl_dataset_t *ds;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens uint64_t obj;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG);
fa9e4066f08beec538e775443c5be79dd423fcabahrens err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
fa9e4066f08beec538e775443c5be79dd423fcabahrens DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1,
fa9e4066f08beec538e775443c5be79dd423fcabahrens &dp->dp_root_dir_obj);
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock if (err)
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock goto out;
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens err = dsl_dir_hold_obj(dp, dp->dp_root_dir_obj,
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock NULL, dp, &dp->dp_root_dir);
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock if (err)
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock goto out;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
088f389458728c464569a5506b58070254fa4f7dahrens err = dsl_pool_open_special_dir(dp, MOS_DIR_NAME, &dp->dp_mos_dir);
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock if (err)
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock goto out;
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden if (spa_version(dp->dp_spa) >= SPA_VERSION_ORIGIN) {
088f389458728c464569a5506b58070254fa4f7dahrens err = dsl_pool_open_special_dir(dp, ORIGIN_DIR_NAME, &dd);
088f389458728c464569a5506b58070254fa4f7dahrens if (err)
088f389458728c464569a5506b58070254fa4f7dahrens goto out;
088f389458728c464569a5506b58070254fa4f7dahrens err = dsl_dataset_hold_obj(dp, dd->dd_phys->dd_head_dataset_obj,
088f389458728c464569a5506b58070254fa4f7dahrens FTAG, &ds);
8f63aa464994c24e12bd657bac93d1b2e2df2d1cLin Ling if (err == 0) {
8f63aa464994c24e12bd657bac93d1b2e2df2d1cLin Ling err = dsl_dataset_hold_obj(dp,
8f63aa464994c24e12bd657bac93d1b2e2df2d1cLin Ling ds->ds_phys->ds_prev_snap_obj, dp,
8f63aa464994c24e12bd657bac93d1b2e2df2d1cLin Ling &dp->dp_origin_snap);
8f63aa464994c24e12bd657bac93d1b2e2df2d1cLin Ling dsl_dataset_rele(ds, FTAG);
8f63aa464994c24e12bd657bac93d1b2e2df2d1cLin Ling }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dir_rele(dd, dp);
088f389458728c464569a5506b58070254fa4f7dahrens if (err)
088f389458728c464569a5506b58070254fa4f7dahrens goto out;
088f389458728c464569a5506b58070254fa4f7dahrens }
088f389458728c464569a5506b58070254fa4f7dahrens
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden if (spa_version(dp->dp_spa) >= SPA_VERSION_DEADLISTS) {
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens err = dsl_pool_open_special_dir(dp, FREE_DIR_NAME,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens &dp->dp_free_dir);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (err)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens goto out;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens DMU_POOL_FREE_BPOBJ, sizeof (uint64_t), 1, &obj);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (err)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens goto out;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(bpobj_open(&dp->dp_free_bpobj,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dp->dp_meta_objset, obj));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens }
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden if (spa_feature_is_active(dp->dp_spa,
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden &spa_feature_table[SPA_FEATURE_ASYNC_DESTROY])) {
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden DMU_POOL_BPTREE_OBJ, sizeof (uint64_t), 1,
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden &dp->dp_bptree_obj);
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden if (err != 0)
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden goto out;
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden }
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens if (spa_feature_is_active(dp->dp_spa,
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens &spa_feature_table[SPA_FEATURE_EMPTY_BPOBJ])) {
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens DMU_POOL_EMPTY_BPOBJ, sizeof (uint64_t), 1,
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens &dp->dp_empty_bpobj);
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens if (err != 0)
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens goto out;
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens }
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens
ca45db4129beff691dc46576c328149443788af2Chris Kirby err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
ca45db4129beff691dc46576c328149443788af2Chris Kirby DMU_POOL_TMP_USERREFS, sizeof (uint64_t), 1,
ca45db4129beff691dc46576c328149443788af2Chris Kirby &dp->dp_tmp_userrefs_obj);
ca45db4129beff691dc46576c328149443788af2Chris Kirby if (err == ENOENT)
ca45db4129beff691dc46576c328149443788af2Chris Kirby err = 0;
ca45db4129beff691dc46576c328149443788af2Chris Kirby if (err)
ca45db4129beff691dc46576c328149443788af2Chris Kirby goto out;
ca45db4129beff691dc46576c328149443788af2Chris Kirby
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden err = dsl_scan_init(dp, dp->dp_tx.tx_open_txg);
088f389458728c464569a5506b58070254fa4f7dahrens
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrockout:
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens rrw_exit(&dp->dp_config_rwlock, FTAG);
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock return (err);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrensvoid
fa9e4066f08beec538e775443c5be79dd423fcabahrensdsl_pool_close(dsl_pool_t *dp)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
088f389458728c464569a5506b58070254fa4f7dahrens /* drop our references from dsl_pool_open() */
088f389458728c464569a5506b58070254fa4f7dahrens
088f389458728c464569a5506b58070254fa4f7dahrens /*
088f389458728c464569a5506b58070254fa4f7dahrens * Since we held the origin_snap from "syncing" context (which
088f389458728c464569a5506b58070254fa4f7dahrens * includes pool-opening context), it actually only got a "ref"
088f389458728c464569a5506b58070254fa4f7dahrens * and not a hold, so just drop that here.
088f389458728c464569a5506b58070254fa4f7dahrens */
088f389458728c464569a5506b58070254fa4f7dahrens if (dp->dp_origin_snap)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(dp->dp_origin_snap, dp);
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock if (dp->dp_mos_dir)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dir_rele(dp->dp_mos_dir, dp);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (dp->dp_free_dir)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dir_rele(dp->dp_free_dir, dp);
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock if (dp->dp_root_dir)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dir_rele(dp->dp_root_dir, dp);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bpobj_close(&dp->dp_free_bpobj);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens /* undo the dmu_objset_open_impl(mos) from dsl_pool_open() */
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock if (dp->dp_meta_objset)
503ad85c168c7992ccc310af845a581cff3c72b5Matthew Ahrens dmu_objset_evict(dp->dp_meta_objset);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens txg_list_destroy(&dp->dp_dirty_datasets);
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens txg_list_destroy(&dp->dp_dirty_zilogs);
54a91118eee5bfd63eb614a44e1b68f1571a99eaChris Kirby txg_list_destroy(&dp->dp_sync_tasks);
fa9e4066f08beec538e775443c5be79dd423fcabahrens txg_list_destroy(&dp->dp_dirty_dirs);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
874395d5f8cae2b9cd2d1fcbfcfe963a0c23966dmaybee arc_flush(dp->dp_spa);
fa9e4066f08beec538e775443c5be79dd423fcabahrens txg_fini(dp);
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Ling dsl_scan_fini(dp);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens rrw_destroy(&dp->dp_config_rwlock);
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee mutex_destroy(&dp->dp_lock);
9d3574bff0b382b89dd380c1e23d742ebc156772Neil Perrin taskq_destroy(dp->dp_vnrele_taskq);
88b7b0f29b20b808b9e06071885b1d6a3ddb6328Matthew Ahrens if (dp->dp_blkstats)
88b7b0f29b20b808b9e06071885b1d6a3ddb6328Matthew Ahrens kmem_free(dp->dp_blkstats, sizeof (zfs_all_blkstats_t));
fa9e4066f08beec538e775443c5be79dd423fcabahrens kmem_free(dp, sizeof (dsl_pool_t));
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrensdsl_pool_t *
0a48a24e663a04e34e2ed4e55390ad96f178dbeatimhdsl_pool_create(spa_t *spa, nvlist_t *zplprops, uint64_t txg)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens int err;
fa9e4066f08beec538e775443c5be79dd423fcabahrens dsl_pool_t *dp = dsl_pool_open_impl(spa, txg);
fa9e4066f08beec538e775443c5be79dd423fcabahrens dmu_tx_t *tx = dmu_tx_create_assigned(dp, txg);
503ad85c168c7992ccc310af845a581cff3c72b5Matthew Ahrens objset_t *os;
088f389458728c464569a5506b58070254fa4f7dahrens dsl_dataset_t *ds;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens uint64_t obj;
088f389458728c464569a5506b58070254fa4f7dahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
088f389458728c464569a5506b58070254fa4f7dahrens /* create and open the MOS (meta-objset) */
503ad85c168c7992ccc310af845a581cff3c72b5Matthew Ahrens dp->dp_meta_objset = dmu_objset_create_impl(spa,
503ad85c168c7992ccc310af845a581cff3c72b5Matthew Ahrens NULL, &dp->dp_meta_rootbp, DMU_OST_META, tx);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens /* create the pool directory */
fa9e4066f08beec538e775443c5be79dd423fcabahrens err = zap_create_claim(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
fa9e4066f08beec538e775443c5be79dd423fcabahrens DMU_OT_OBJECT_DIRECTORY, DMU_OT_NONE, 0, tx);
fb09f5aad449c97fe309678f3f604982b563a96fMadhav Suresh ASSERT0(err);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Ling /* Initialize scan structures */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dsl_scan_init(dp, txg));
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Ling
fa9e4066f08beec538e775443c5be79dd423fcabahrens /* create and open the root dir */
088f389458728c464569a5506b58070254fa4f7dahrens dp->dp_root_dir_obj = dsl_dir_create_sync(dp, NULL, NULL, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dsl_dir_hold_obj(dp, dp->dp_root_dir_obj,
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock NULL, dp, &dp->dp_root_dir));
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens /* create and open the meta-objset dir */
088f389458728c464569a5506b58070254fa4f7dahrens (void) dsl_dir_create_sync(dp, dp->dp_root_dir, MOS_DIR_NAME, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dsl_pool_open_special_dir(dp,
088f389458728c464569a5506b58070254fa4f7dahrens MOS_DIR_NAME, &dp->dp_mos_dir));
088f389458728c464569a5506b58070254fa4f7dahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens /* create and open the free dir */
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens (void) dsl_dir_create_sync(dp, dp->dp_root_dir,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens FREE_DIR_NAME, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dsl_pool_open_special_dir(dp,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens FREE_DIR_NAME, &dp->dp_free_dir));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens /* create and open the free_bplist */
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens obj = bpobj_alloc(dp->dp_meta_objset, SPA_MAXBLOCKSIZE, tx);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens VERIFY(zap_add(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens DMU_POOL_FREE_BPOBJ, sizeof (uint64_t), 1, &obj, tx) == 0);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(bpobj_open(&dp->dp_free_bpobj,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dp->dp_meta_objset, obj));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens }
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
088f389458728c464569a5506b58070254fa4f7dahrens if (spa_version(spa) >= SPA_VERSION_DSL_SCRUB)
088f389458728c464569a5506b58070254fa4f7dahrens dsl_pool_create_origin(dp, tx);
088f389458728c464569a5506b58070254fa4f7dahrens
088f389458728c464569a5506b58070254fa4f7dahrens /* create the root dataset */
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens obj = dsl_dataset_create_sync_dd(dp->dp_root_dir, NULL, 0, tx);
088f389458728c464569a5506b58070254fa4f7dahrens
088f389458728c464569a5506b58070254fa4f7dahrens /* create the root objset */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, obj, FTAG, &ds));
503ad85c168c7992ccc310af845a581cff3c72b5Matthew Ahrens os = dmu_objset_create_impl(dp->dp_spa, ds,
088f389458728c464569a5506b58070254fa4f7dahrens dsl_dataset_get_blkptr(ds), DMU_OST_ZFS, tx);
088f389458728c464569a5506b58070254fa4f7dahrens#ifdef _KERNEL
503ad85c168c7992ccc310af845a581cff3c72b5Matthew Ahrens zfs_create_fs(os, kcred, zplprops, tx);
088f389458728c464569a5506b58070254fa4f7dahrens#endif
088f389458728c464569a5506b58070254fa4f7dahrens dsl_dataset_rele(ds, FTAG);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens dmu_tx_commit(tx);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens rrw_exit(&dp->dp_config_rwlock, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (dp);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens/*
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens * Account for the meta-objset space in its placeholder dsl_dir.
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens */
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrensvoid
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrensdsl_pool_mos_diduse_space(dsl_pool_t *dp,
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens int64_t used, int64_t comp, int64_t uncomp)
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens{
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens ASSERT3U(comp, ==, uncomp); /* it's all metadata */
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens mutex_enter(&dp->dp_lock);
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens dp->dp_mos_used_delta += used;
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens dp->dp_mos_compressed_delta += comp;
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens dp->dp_mos_uncompressed_delta += uncomp;
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens mutex_exit(&dp->dp_lock);
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens}
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensstatic int
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensdeadlist_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens{
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dsl_deadlist_t *dl = arg;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dsl_deadlist_insert(dl, bp, tx);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens return (0);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens}
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrensvoid
fa9e4066f08beec538e775443c5be79dd423fcabahrensdsl_pool_sync(dsl_pool_t *dp, uint64_t txg)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
c717a56157ae0e6fca6a1e3689ae1edc385716a3maybee zio_t *zio;
fa9e4066f08beec538e775443c5be79dd423fcabahrens dmu_tx_t *tx;
c717a56157ae0e6fca6a1e3689ae1edc385716a3maybee dsl_dir_t *dd;
c717a56157ae0e6fca6a1e3689ae1edc385716a3maybee dsl_dataset_t *ds;
503ad85c168c7992ccc310af845a581cff3c72b5Matthew Ahrens objset_t *mos = dp->dp_meta_objset;
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee hrtime_t start, write_time;
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee uint64_t data_written;
c717a56157ae0e6fca6a1e3689ae1edc385716a3maybee int err;
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens list_t synced_datasets;
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens list_create(&synced_datasets, sizeof (dsl_dataset_t),
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens offsetof(dsl_dataset_t, ds_synced_link));
fa9e4066f08beec538e775443c5be79dd423fcabahrens
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Ling /*
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Ling * We need to copy dp_space_towrite() before doing
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * dsl_sync_task_sync(), because
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Ling * dsl_dataset_snapshot_reserve_space() will increase
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Ling * dp_space_towrite but not actually write anything.
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Ling */
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Ling data_written = dp->dp_space_towrite[txg & TXG_MASK];
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Ling
fa9e4066f08beec538e775443c5be79dd423fcabahrens tx = dmu_tx_create_assigned(dp, txg);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee dp->dp_read_overhead = 0;
0fd90d51ca56bcfe45114c5f0b74d50ad612537fMark Maybee start = gethrtime();
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens
c717a56157ae0e6fca6a1e3689ae1edc385716a3maybee zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
c717a56157ae0e6fca6a1e3689ae1edc385716a3maybee while (ds = txg_list_remove(&dp->dp_dirty_datasets, txg)) {
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens /*
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens * We must not sync any non-MOS datasets twice, because
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens * we may have taken a snapshot of them. However, we
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens * may sync newly-created datasets on pass 2.
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens */
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens ASSERT(!list_link_active(&ds->ds_synced_link));
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens list_insert_tail(&synced_datasets, ds);
c717a56157ae0e6fca6a1e3689ae1edc385716a3maybee dsl_dataset_sync(ds, zio, tx);
c717a56157ae0e6fca6a1e3689ae1edc385716a3maybee }
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee DTRACE_PROBE(pool_sync__1setup);
c717a56157ae0e6fca6a1e3689ae1edc385716a3maybee err = zio_wait(zio);
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee write_time = gethrtime() - start;
c717a56157ae0e6fca6a1e3689ae1edc385716a3maybee ASSERT(err == 0);
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee DTRACE_PROBE(pool_sync__2rootzio);
c717a56157ae0e6fca6a1e3689ae1edc385716a3maybee
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens /*
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens * After the data blocks have been written (ensured by the zio_wait()
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens * above), update the user/group space accounting.
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens */
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens for (ds = list_head(&synced_datasets); ds;
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens ds = list_next(&synced_datasets, ds))
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaum dmu_objset_do_userquota_updates(ds->ds_objset, tx);
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens /*
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens * Sync the datasets again to push out the changes due to
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Ling * userspace updates. This must be done before we process the
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens * sync tasks, so that any snapshots will have the correct
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens * user accounting information (and we won't get confused
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens * about which blocks are part of the snapshot).
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens */
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens while (ds = txg_list_remove(&dp->dp_dirty_datasets, txg)) {
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens ASSERT(list_link_active(&ds->ds_synced_link));
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens dmu_buf_rele(ds->ds_dbuf, ds);
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens dsl_dataset_sync(ds, zio, tx);
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens }
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens err = zio_wait(zio);
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick /*
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens * Now that the datasets have been completely synced, we can
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens * clean up our in-memory structures accumulated while syncing:
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens *
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens * - move dead blocks from the pending deadlist to the on-disk deadlist
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens * - release hold from dsl_dataset_dirty()
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick */
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens while (ds = list_remove_head(&synced_datasets)) {
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens objset_t *os = ds->ds_objset;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bplist_iterate(&ds->ds_pending_deadlist,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens deadlist_enqueue_cb, &ds->ds_deadlist, tx);
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens ASSERT(!dmu_objset_is_dirty(os, txg));
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens dmu_buf_rele(ds->ds_dbuf, ds);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens }
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee start = gethrtime();
c717a56157ae0e6fca6a1e3689ae1edc385716a3maybee while (dd = txg_list_remove(&dp->dp_dirty_dirs, txg))
c717a56157ae0e6fca6a1e3689ae1edc385716a3maybee dsl_dir_sync(dd, tx);
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee write_time += gethrtime() - start;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens /*
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens * The MOS's space is accounted for in the pool/$MOS
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens * (dp_mos_dir). We can't modify the mos while we're syncing
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens * it, so we remember the deltas and apply them here.
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens */
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens if (dp->dp_mos_used_delta != 0 || dp->dp_mos_compressed_delta != 0 ||
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens dp->dp_mos_uncompressed_delta != 0) {
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens dsl_dir_diduse_space(dp->dp_mos_dir, DD_USED_HEAD,
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens dp->dp_mos_used_delta,
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens dp->dp_mos_compressed_delta,
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens dp->dp_mos_uncompressed_delta, tx);
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens dp->dp_mos_used_delta = 0;
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens dp->dp_mos_compressed_delta = 0;
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens dp->dp_mos_uncompressed_delta = 0;
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens }
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee start = gethrtime();
503ad85c168c7992ccc310af845a581cff3c72b5Matthew Ahrens if (list_head(&mos->os_dirty_dnodes[txg & TXG_MASK]) != NULL ||
503ad85c168c7992ccc310af845a581cff3c72b5Matthew Ahrens list_head(&mos->os_free_dnodes[txg & TXG_MASK]) != NULL) {
c717a56157ae0e6fca6a1e3689ae1edc385716a3maybee zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
503ad85c168c7992ccc310af845a581cff3c72b5Matthew Ahrens dmu_objset_sync(mos, zio, tx);
c717a56157ae0e6fca6a1e3689ae1edc385716a3maybee err = zio_wait(zio);
c717a56157ae0e6fca6a1e3689ae1edc385716a3maybee ASSERT(err == 0);
fa9e4066f08beec538e775443c5be79dd423fcabahrens dprintf_bp(&dp->dp_meta_rootbp, "meta objset rootbp is %s", "");
fa9e4066f08beec538e775443c5be79dd423fcabahrens spa_set_rootblkptr(dp->dp_spa, &dp->dp_meta_rootbp);
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee write_time += gethrtime() - start;
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee DTRACE_PROBE2(pool_sync__4io, hrtime_t, write_time,
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee hrtime_t, dp->dp_read_overhead);
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee write_time -= dp->dp_read_overhead;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens /*
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens * If we modify a dataset in the same txg that we want to destroy it,
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens * its dsl_dir's dd_dbuf will be dirty, and thus have a hold on it.
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens * dsl_dir_destroy_check() will fail if there are unexpected holds.
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens * Therefore, we want to sync the MOS (thus syncing the dd_dbuf
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens * and clearing the hold on it) before we process the sync_tasks.
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens * The MOS data dirtied by the sync_tasks will be synced on the next
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens * pass.
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens */
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens DTRACE_PROBE(pool_sync__3task);
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens if (!txg_list_empty(&dp->dp_sync_tasks, txg)) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_sync_task_t *dst;
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens /*
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens * No more sync tasks should have been added while we
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens * were syncing.
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens */
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens ASSERT(spa_sync_pass(dp->dp_spa) == 1);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens while (dst = txg_list_remove(&dp->dp_sync_tasks, txg))
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_sync_task_sync(dst, tx);
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens }
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens dmu_tx_commit(tx);
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee dp->dp_space_towrite[txg & TXG_MASK] = 0;
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee ASSERT(dp->dp_tempreserved[txg & TXG_MASK] == 0);
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee /*
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee * If the write limit max has not been explicitly set, set it
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee * to a fraction of available physical memory (default 1/8th).
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee * Note that we must inflate the limit because the spa
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee * inflates write sizes to account for data replication.
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee * Check this each sync phase to catch changing memory size.
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee */
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee if (physmem != old_physmem && zfs_write_limit_shift) {
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee mutex_enter(&zfs_write_limit_lock);
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee old_physmem = physmem;
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee zfs_write_limit_max = ptob(physmem) >> zfs_write_limit_shift;
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee zfs_write_limit_inflated = MAX(zfs_write_limit_min,
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee spa_get_asize(dp->dp_spa, zfs_write_limit_max));
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee mutex_exit(&zfs_write_limit_lock);
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee }
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee /*
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee * Attempt to keep the sync time consistent by adjusting the
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee * amount of write traffic allowed into each transaction group.
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee * Weight the throughput calculation towards the current value:
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee * thru = 3/4 old_thru + 1/4 new_thru
fb5dd802b9c6917629172c7c7fade7ee9d39cd71Lin Ling *
fb5dd802b9c6917629172c7c7fade7ee9d39cd71Lin Ling * Note: write_time is in nanosecs, so write_time/MICROSEC
fb5dd802b9c6917629172c7c7fade7ee9d39cd71Lin Ling * yields millisecs
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee */
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee ASSERT(zfs_write_limit_min > 0);
fb5dd802b9c6917629172c7c7fade7ee9d39cd71Lin Ling if (data_written > zfs_write_limit_min / 8 && write_time > MICROSEC) {
fb5dd802b9c6917629172c7c7fade7ee9d39cd71Lin Ling uint64_t throughput = data_written / (write_time / MICROSEC);
fb5dd802b9c6917629172c7c7fade7ee9d39cd71Lin Ling
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee if (dp->dp_throughput)
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee dp->dp_throughput = throughput / 4 +
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee 3 * dp->dp_throughput / 4;
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee else
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee dp->dp_throughput = throughput;
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee dp->dp_write_limit = MIN(zfs_write_limit_inflated,
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee MAX(zfs_write_limit_min,
fb5dd802b9c6917629172c7c7fade7ee9d39cd71Lin Ling dp->dp_throughput * zfs_txg_synctime_ms));
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee }
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrensvoid
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickdsl_pool_sync_done(dsl_pool_t *dp, uint64_t txg)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens zilog_t *zilog;
fa9e4066f08beec538e775443c5be79dd423fcabahrens dsl_dataset_t *ds;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens while (zilog = txg_list_remove(&dp->dp_dirty_zilogs, txg)) {
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens ds = dmu_objset_ds(zilog->zl_os);
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens zil_clean(zilog, txg);
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens ASSERT(!dmu_objset_is_dirty(zilog->zl_os, txg));
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens dmu_buf_rele(ds->ds_dbuf, zilog);
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(!dmu_objset_is_dirty(dp->dp_meta_objset, txg));
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
c717a56157ae0e6fca6a1e3689ae1edc385716a3maybee/*
c717a56157ae0e6fca6a1e3689ae1edc385716a3maybee * TRUE if the current thread is the tx_sync_thread or if we
c717a56157ae0e6fca6a1e3689ae1edc385716a3maybee * are being called from SPA context during pool initialization.
c717a56157ae0e6fca6a1e3689ae1edc385716a3maybee */
fa9e4066f08beec538e775443c5be79dd423fcabahrensint
fa9e4066f08beec538e775443c5be79dd423fcabahrensdsl_pool_sync_context(dsl_pool_t *dp)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (curthread == dp->dp_tx.tx_sync_thread ||
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden spa_is_initializing(dp->dp_spa));
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrensuint64_t
fa9e4066f08beec538e775443c5be79dd423fcabahrensdsl_pool_adjustedsize(dsl_pool_t *dp, boolean_t netfree)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens uint64_t space, resv;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens /*
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm * Reserve about 1.6% (1/64), or at least 32MB, for allocation
fa9e4066f08beec538e775443c5be79dd423fcabahrens * efficiency.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * XXX The intent log is not accounted for, so it must fit
fa9e4066f08beec538e775443c5be79dd423fcabahrens * within this slop.
fa9e4066f08beec538e775443c5be79dd423fcabahrens *
fa9e4066f08beec538e775443c5be79dd423fcabahrens * If we're trying to assess whether it's OK to do a free,
fa9e4066f08beec538e775443c5be79dd423fcabahrens * cut the reservation in half to allow forward progress
fa9e4066f08beec538e775443c5be79dd423fcabahrens * (e.g. make it possible to rm(1) files from a full pool).
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
485bbbf5450c6645352388d798251c1a89ef4c9cGeorge Wilson space = spa_get_dspace(dp->dp_spa);
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm resv = MAX(space >> 6, SPA_MINDEVSIZE >> 1);
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (netfree)
fa9e4066f08beec538e775443c5be79dd423fcabahrens resv >>= 1;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (space - resv);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybeeint
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybeedsl_pool_tempreserve_space(dsl_pool_t *dp, uint64_t space, dmu_tx_t *tx)
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee{
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee uint64_t reserved = 0;
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee uint64_t write_limit = (zfs_write_limit_override ?
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee zfs_write_limit_override : dp->dp_write_limit);
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee if (zfs_no_write_throttle) {
c5904d138f3bdf0762dbf452a43d5a5c387ea6a8eschrock atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK],
c5904d138f3bdf0762dbf452a43d5a5c387ea6a8eschrock space);
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee return (0);
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee }
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee /*
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee * Check to see if we have exceeded the maximum allowed IO for
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee * this transaction group. We can do this without locks since
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee * a little slop here is ok. Note that we do the reserved check
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee * with only half the requested reserve: this is because the
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee * reserve requests are worst-case, and we really don't want to
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee * throttle based off of worst-case estimates.
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee */
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee if (write_limit > 0) {
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee reserved = dp->dp_space_towrite[tx->tx_txg & TXG_MASK]
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee + dp->dp_tempreserved[tx->tx_txg & TXG_MASK] / 2;
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee if (reserved && reserved > write_limit)
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee return (ERESTART);
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee }
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK], space);
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee /*
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee * If this transaction group is over 7/8ths capacity, delay
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee * the caller 1 clock tick. This will slow down the "fill"
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee * rate until the sync process can catch up with us.
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee */
e8397a2be4690aefe43370aae2d4214c6778327egw if (reserved && reserved > (write_limit - (write_limit >> 3)))
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee txg_delay(dp, tx->tx_txg, 1);
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee return (0);
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee}
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybeevoid
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybeedsl_pool_tempreserve_clear(dsl_pool_t *dp, int64_t space, dmu_tx_t *tx)
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee{
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee ASSERT(dp->dp_tempreserved[tx->tx_txg & TXG_MASK] >= space);
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK], -space);
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee}
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybeevoid
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybeedsl_pool_memory_pressure(dsl_pool_t *dp)
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee{
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee uint64_t space_inuse = 0;
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee int i;
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee if (dp->dp_write_limit == zfs_write_limit_min)
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee return;
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee for (i = 0; i < TXG_SIZE; i++) {
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee space_inuse += dp->dp_space_towrite[i];
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee space_inuse += dp->dp_tempreserved[i];
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee }
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee dp->dp_write_limit = MAX(zfs_write_limit_min,
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee MIN(dp->dp_write_limit, space_inuse / 4));
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee}
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybeevoid
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybeedsl_pool_willuse_space(dsl_pool_t *dp, int64_t space, dmu_tx_t *tx)
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee{
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee if (space > 0) {
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee mutex_enter(&dp->dp_lock);
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee dp->dp_space_towrite[tx->tx_txg & TXG_MASK] += space;
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee mutex_exit(&dp->dp_lock);
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee }
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee}
088f389458728c464569a5506b58070254fa4f7dahrens
088f389458728c464569a5506b58070254fa4f7dahrens/* ARGSUSED */
088f389458728c464569a5506b58070254fa4f7dahrensstatic int
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensupgrade_clones_cb(dsl_pool_t *dp, dsl_dataset_t *hds, void *arg)
088f389458728c464569a5506b58070254fa4f7dahrens{
088f389458728c464569a5506b58070254fa4f7dahrens dmu_tx_t *tx = arg;
088f389458728c464569a5506b58070254fa4f7dahrens dsl_dataset_t *ds, *prev = NULL;
088f389458728c464569a5506b58070254fa4f7dahrens int err;
088f389458728c464569a5506b58070254fa4f7dahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens err = dsl_dataset_hold_obj(dp, hds->ds_object, FTAG, &ds);
088f389458728c464569a5506b58070254fa4f7dahrens if (err)
088f389458728c464569a5506b58070254fa4f7dahrens return (err);
088f389458728c464569a5506b58070254fa4f7dahrens
088f389458728c464569a5506b58070254fa4f7dahrens while (ds->ds_phys->ds_prev_snap_obj != 0) {
088f389458728c464569a5506b58070254fa4f7dahrens err = dsl_dataset_hold_obj(dp, ds->ds_phys->ds_prev_snap_obj,
088f389458728c464569a5506b58070254fa4f7dahrens FTAG, &prev);
088f389458728c464569a5506b58070254fa4f7dahrens if (err) {
088f389458728c464569a5506b58070254fa4f7dahrens dsl_dataset_rele(ds, FTAG);
088f389458728c464569a5506b58070254fa4f7dahrens return (err);
088f389458728c464569a5506b58070254fa4f7dahrens }
088f389458728c464569a5506b58070254fa4f7dahrens
088f389458728c464569a5506b58070254fa4f7dahrens if (prev->ds_phys->ds_next_snap_obj != ds->ds_object)
088f389458728c464569a5506b58070254fa4f7dahrens break;
088f389458728c464569a5506b58070254fa4f7dahrens dsl_dataset_rele(ds, FTAG);
088f389458728c464569a5506b58070254fa4f7dahrens ds = prev;
088f389458728c464569a5506b58070254fa4f7dahrens prev = NULL;
088f389458728c464569a5506b58070254fa4f7dahrens }
088f389458728c464569a5506b58070254fa4f7dahrens
088f389458728c464569a5506b58070254fa4f7dahrens if (prev == NULL) {
088f389458728c464569a5506b58070254fa4f7dahrens prev = dp->dp_origin_snap;
088f389458728c464569a5506b58070254fa4f7dahrens
088f389458728c464569a5506b58070254fa4f7dahrens /*
088f389458728c464569a5506b58070254fa4f7dahrens * The $ORIGIN can't have any data, or the accounting
088f389458728c464569a5506b58070254fa4f7dahrens * will be wrong.
088f389458728c464569a5506b58070254fa4f7dahrens */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT0(prev->ds_phys->ds_bp.blk_birth);
088f389458728c464569a5506b58070254fa4f7dahrens
088f389458728c464569a5506b58070254fa4f7dahrens /* The origin doesn't get attached to itself */
088f389458728c464569a5506b58070254fa4f7dahrens if (ds->ds_object == prev->ds_object) {
088f389458728c464569a5506b58070254fa4f7dahrens dsl_dataset_rele(ds, FTAG);
088f389458728c464569a5506b58070254fa4f7dahrens return (0);
088f389458728c464569a5506b58070254fa4f7dahrens }
088f389458728c464569a5506b58070254fa4f7dahrens
088f389458728c464569a5506b58070254fa4f7dahrens dmu_buf_will_dirty(ds->ds_dbuf, tx);
088f389458728c464569a5506b58070254fa4f7dahrens ds->ds_phys->ds_prev_snap_obj = prev->ds_object;
088f389458728c464569a5506b58070254fa4f7dahrens ds->ds_phys->ds_prev_snap_txg = prev->ds_phys->ds_creation_txg;
088f389458728c464569a5506b58070254fa4f7dahrens
088f389458728c464569a5506b58070254fa4f7dahrens dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
088f389458728c464569a5506b58070254fa4f7dahrens ds->ds_dir->dd_phys->dd_origin_obj = prev->ds_object;
088f389458728c464569a5506b58070254fa4f7dahrens
088f389458728c464569a5506b58070254fa4f7dahrens dmu_buf_will_dirty(prev->ds_dbuf, tx);
088f389458728c464569a5506b58070254fa4f7dahrens prev->ds_phys->ds_num_children++;
088f389458728c464569a5506b58070254fa4f7dahrens
088f389458728c464569a5506b58070254fa4f7dahrens if (ds->ds_phys->ds_next_snap_obj == 0) {
088f389458728c464569a5506b58070254fa4f7dahrens ASSERT(ds->ds_prev == NULL);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp,
088f389458728c464569a5506b58070254fa4f7dahrens ds->ds_phys->ds_prev_snap_obj, ds, &ds->ds_prev));
088f389458728c464569a5506b58070254fa4f7dahrens }
088f389458728c464569a5506b58070254fa4f7dahrens }
088f389458728c464569a5506b58070254fa4f7dahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT3U(ds->ds_dir->dd_phys->dd_origin_obj, ==, prev->ds_object);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT3U(ds->ds_phys->ds_prev_snap_obj, ==, prev->ds_object);
088f389458728c464569a5506b58070254fa4f7dahrens
088f389458728c464569a5506b58070254fa4f7dahrens if (prev->ds_phys->ds_next_clones_obj == 0) {
c33e334fd3eb2b3d91c4b9667d7a465b6924e8d3Matthew Ahrens dmu_buf_will_dirty(prev->ds_dbuf, tx);
088f389458728c464569a5506b58070254fa4f7dahrens prev->ds_phys->ds_next_clones_obj =
088f389458728c464569a5506b58070254fa4f7dahrens zap_create(dp->dp_meta_objset,
088f389458728c464569a5506b58070254fa4f7dahrens DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx);
088f389458728c464569a5506b58070254fa4f7dahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(zap_add_int(dp->dp_meta_objset,
088f389458728c464569a5506b58070254fa4f7dahrens prev->ds_phys->ds_next_clones_obj, ds->ds_object, tx));
088f389458728c464569a5506b58070254fa4f7dahrens
088f389458728c464569a5506b58070254fa4f7dahrens dsl_dataset_rele(ds, FTAG);
088f389458728c464569a5506b58070254fa4f7dahrens if (prev != dp->dp_origin_snap)
088f389458728c464569a5506b58070254fa4f7dahrens dsl_dataset_rele(prev, FTAG);
088f389458728c464569a5506b58070254fa4f7dahrens return (0);
088f389458728c464569a5506b58070254fa4f7dahrens}
088f389458728c464569a5506b58070254fa4f7dahrens
088f389458728c464569a5506b58070254fa4f7dahrensvoid
088f389458728c464569a5506b58070254fa4f7dahrensdsl_pool_upgrade_clones(dsl_pool_t *dp, dmu_tx_t *tx)
088f389458728c464569a5506b58070254fa4f7dahrens{
088f389458728c464569a5506b58070254fa4f7dahrens ASSERT(dmu_tx_is_syncing(tx));
088f389458728c464569a5506b58070254fa4f7dahrens ASSERT(dp->dp_origin_snap != NULL);
088f389458728c464569a5506b58070254fa4f7dahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dmu_objset_find_dp(dp, dp->dp_root_dir_obj, upgrade_clones_cb,
c33e334fd3eb2b3d91c4b9667d7a465b6924e8d3Matthew Ahrens tx, DS_FIND_CHILDREN));
088f389458728c464569a5506b58070254fa4f7dahrens}
088f389458728c464569a5506b58070254fa4f7dahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens/* ARGSUSED */
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensstatic int
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensupgrade_dir_clones_cb(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens{
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dmu_tx_t *tx = arg;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens objset_t *mos = dp->dp_meta_objset;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (ds->ds_dir->dd_phys->dd_origin_obj != 0) {
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dsl_dataset_t *origin;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens ds->ds_dir->dd_phys->dd_origin_obj, FTAG, &origin));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (origin->ds_dir->dd_phys->dd_clones == 0) {
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dmu_buf_will_dirty(origin->ds_dir->dd_dbuf, tx);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens origin->ds_dir->dd_phys->dd_clones = zap_create(mos,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens }
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(zap_add_int(dp->dp_meta_objset,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens origin->ds_dir->dd_phys->dd_clones, ds->ds_object, tx));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dsl_dataset_rele(origin, FTAG);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens }
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens return (0);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens}
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensvoid
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensdsl_pool_upgrade_dir_clones(dsl_pool_t *dp, dmu_tx_t *tx)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens{
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens ASSERT(dmu_tx_is_syncing(tx));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens uint64_t obj;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens (void) dsl_dir_create_sync(dp, dp->dp_root_dir, FREE_DIR_NAME, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dsl_pool_open_special_dir(dp,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens FREE_DIR_NAME, &dp->dp_free_dir));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens /*
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens * We can't use bpobj_alloc(), because spa_version() still
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens * returns the old version, and we need a new-version bpobj with
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens * subobj support. So call dmu_object_alloc() directly.
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens */
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens obj = dmu_object_alloc(dp->dp_meta_objset, DMU_OT_BPOBJ,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens SPA_MAXBLOCKSIZE, DMU_OT_BPOBJ_HDR, sizeof (bpobj_phys_t), tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(zap_add(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens DMU_POOL_FREE_BPOBJ, sizeof (uint64_t), 1, &obj, tx));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(bpobj_open(&dp->dp_free_bpobj, dp->dp_meta_objset, obj));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dmu_objset_find_dp(dp, dp->dp_root_dir_obj,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens upgrade_dir_clones_cb, tx, DS_FIND_CHILDREN));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens}
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
088f389458728c464569a5506b58070254fa4f7dahrensvoid
088f389458728c464569a5506b58070254fa4f7dahrensdsl_pool_create_origin(dsl_pool_t *dp, dmu_tx_t *tx)
088f389458728c464569a5506b58070254fa4f7dahrens{
088f389458728c464569a5506b58070254fa4f7dahrens uint64_t dsobj;
088f389458728c464569a5506b58070254fa4f7dahrens dsl_dataset_t *ds;
088f389458728c464569a5506b58070254fa4f7dahrens
088f389458728c464569a5506b58070254fa4f7dahrens ASSERT(dmu_tx_is_syncing(tx));
088f389458728c464569a5506b58070254fa4f7dahrens ASSERT(dp->dp_origin_snap == NULL);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT(rrw_held(&dp->dp_config_rwlock, RW_WRITER));
088f389458728c464569a5506b58070254fa4f7dahrens
088f389458728c464569a5506b58070254fa4f7dahrens /* create the origin dir, ds, & snap-ds */
088f389458728c464569a5506b58070254fa4f7dahrens dsobj = dsl_dataset_create_sync(dp->dp_root_dir, ORIGIN_DIR_NAME,
088f389458728c464569a5506b58070254fa4f7dahrens NULL, 0, kcred, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_snapshot_sync_impl(ds, ORIGIN_DIR_NAME, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, ds->ds_phys->ds_prev_snap_obj,
088f389458728c464569a5506b58070254fa4f7dahrens dp, &dp->dp_origin_snap));
088f389458728c464569a5506b58070254fa4f7dahrens dsl_dataset_rele(ds, FTAG);
088f389458728c464569a5506b58070254fa4f7dahrens}
9d3574bff0b382b89dd380c1e23d742ebc156772Neil Perrin
9d3574bff0b382b89dd380c1e23d742ebc156772Neil Perrintaskq_t *
9d3574bff0b382b89dd380c1e23d742ebc156772Neil Perrindsl_pool_vnrele_taskq(dsl_pool_t *dp)
9d3574bff0b382b89dd380c1e23d742ebc156772Neil Perrin{
9d3574bff0b382b89dd380c1e23d742ebc156772Neil Perrin return (dp->dp_vnrele_taskq);
9d3574bff0b382b89dd380c1e23d742ebc156772Neil Perrin}
ca45db4129beff691dc46576c328149443788af2Chris Kirby
ca45db4129beff691dc46576c328149443788af2Chris Kirby/*
ca45db4129beff691dc46576c328149443788af2Chris Kirby * Walk through the pool-wide zap object of temporary snapshot user holds
ca45db4129beff691dc46576c328149443788af2Chris Kirby * and release them.
ca45db4129beff691dc46576c328149443788af2Chris Kirby */
ca45db4129beff691dc46576c328149443788af2Chris Kirbyvoid
ca45db4129beff691dc46576c328149443788af2Chris Kirbydsl_pool_clean_tmp_userrefs(dsl_pool_t *dp)
ca45db4129beff691dc46576c328149443788af2Chris Kirby{
ca45db4129beff691dc46576c328149443788af2Chris Kirby zap_attribute_t za;
ca45db4129beff691dc46576c328149443788af2Chris Kirby zap_cursor_t zc;
ca45db4129beff691dc46576c328149443788af2Chris Kirby objset_t *mos = dp->dp_meta_objset;
ca45db4129beff691dc46576c328149443788af2Chris Kirby uint64_t zapobj = dp->dp_tmp_userrefs_obj;
ca45db4129beff691dc46576c328149443788af2Chris Kirby
ca45db4129beff691dc46576c328149443788af2Chris Kirby if (zapobj == 0)
ca45db4129beff691dc46576c328149443788af2Chris Kirby return;
ca45db4129beff691dc46576c328149443788af2Chris Kirby ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS);
ca45db4129beff691dc46576c328149443788af2Chris Kirby
ca45db4129beff691dc46576c328149443788af2Chris Kirby for (zap_cursor_init(&zc, mos, zapobj);
ca45db4129beff691dc46576c328149443788af2Chris Kirby zap_cursor_retrieve(&zc, &za) == 0;
ca45db4129beff691dc46576c328149443788af2Chris Kirby zap_cursor_advance(&zc)) {
ca45db4129beff691dc46576c328149443788af2Chris Kirby char *htag;
ca45db4129beff691dc46576c328149443788af2Chris Kirby uint64_t dsobj;
ca45db4129beff691dc46576c328149443788af2Chris Kirby
ca45db4129beff691dc46576c328149443788af2Chris Kirby htag = strchr(za.za_name, '-');
ca45db4129beff691dc46576c328149443788af2Chris Kirby *htag = '\0';
ca45db4129beff691dc46576c328149443788af2Chris Kirby ++htag;
ca45db4129beff691dc46576c328149443788af2Chris Kirby dsobj = strtonum(za.za_name, NULL);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_user_release_tmp(dp, dsobj, htag);
ca45db4129beff691dc46576c328149443788af2Chris Kirby }
ca45db4129beff691dc46576c328149443788af2Chris Kirby zap_cursor_fini(&zc);
ca45db4129beff691dc46576c328149443788af2Chris Kirby}
ca45db4129beff691dc46576c328149443788af2Chris Kirby
ca45db4129beff691dc46576c328149443788af2Chris Kirby/*
ca45db4129beff691dc46576c328149443788af2Chris Kirby * Create the pool-wide zap object for storing temporary snapshot holds.
ca45db4129beff691dc46576c328149443788af2Chris Kirby */
ca45db4129beff691dc46576c328149443788af2Chris Kirbyvoid
ca45db4129beff691dc46576c328149443788af2Chris Kirbydsl_pool_user_hold_create_obj(dsl_pool_t *dp, dmu_tx_t *tx)
ca45db4129beff691dc46576c328149443788af2Chris Kirby{
ca45db4129beff691dc46576c328149443788af2Chris Kirby objset_t *mos = dp->dp_meta_objset;
ca45db4129beff691dc46576c328149443788af2Chris Kirby
ca45db4129beff691dc46576c328149443788af2Chris Kirby ASSERT(dp->dp_tmp_userrefs_obj == 0);
ca45db4129beff691dc46576c328149443788af2Chris Kirby ASSERT(dmu_tx_is_syncing(tx));
ca45db4129beff691dc46576c328149443788af2Chris Kirby
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden dp->dp_tmp_userrefs_obj = zap_create_link(mos, DMU_OT_USERREFS,
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_TMP_USERREFS, tx);
ca45db4129beff691dc46576c328149443788af2Chris Kirby}
ca45db4129beff691dc46576c328149443788af2Chris Kirby
ca45db4129beff691dc46576c328149443788af2Chris Kirbystatic int
ca45db4129beff691dc46576c328149443788af2Chris Kirbydsl_pool_user_hold_rele_impl(dsl_pool_t *dp, uint64_t dsobj,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens const char *tag, uint64_t now, dmu_tx_t *tx, boolean_t holding)
ca45db4129beff691dc46576c328149443788af2Chris Kirby{
ca45db4129beff691dc46576c328149443788af2Chris Kirby objset_t *mos = dp->dp_meta_objset;
ca45db4129beff691dc46576c328149443788af2Chris Kirby uint64_t zapobj = dp->dp_tmp_userrefs_obj;
ca45db4129beff691dc46576c328149443788af2Chris Kirby char *name;
ca45db4129beff691dc46576c328149443788af2Chris Kirby int error;
ca45db4129beff691dc46576c328149443788af2Chris Kirby
ca45db4129beff691dc46576c328149443788af2Chris Kirby ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS);
ca45db4129beff691dc46576c328149443788af2Chris Kirby ASSERT(dmu_tx_is_syncing(tx));
ca45db4129beff691dc46576c328149443788af2Chris Kirby
ca45db4129beff691dc46576c328149443788af2Chris Kirby /*
ca45db4129beff691dc46576c328149443788af2Chris Kirby * If the pool was created prior to SPA_VERSION_USERREFS, the
ca45db4129beff691dc46576c328149443788af2Chris Kirby * zap object for temporary holds might not exist yet.
ca45db4129beff691dc46576c328149443788af2Chris Kirby */
ca45db4129beff691dc46576c328149443788af2Chris Kirby if (zapobj == 0) {
ca45db4129beff691dc46576c328149443788af2Chris Kirby if (holding) {
ca45db4129beff691dc46576c328149443788af2Chris Kirby dsl_pool_user_hold_create_obj(dp, tx);
ca45db4129beff691dc46576c328149443788af2Chris Kirby zapobj = dp->dp_tmp_userrefs_obj;
ca45db4129beff691dc46576c328149443788af2Chris Kirby } else {
ca45db4129beff691dc46576c328149443788af2Chris Kirby return (ENOENT);
ca45db4129beff691dc46576c328149443788af2Chris Kirby }
ca45db4129beff691dc46576c328149443788af2Chris Kirby }
ca45db4129beff691dc46576c328149443788af2Chris Kirby
ca45db4129beff691dc46576c328149443788af2Chris Kirby name = kmem_asprintf("%llx-%s", (u_longlong_t)dsobj, tag);
ca45db4129beff691dc46576c328149443788af2Chris Kirby if (holding)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens error = zap_add(mos, zapobj, name, 8, 1, &now, tx);
ca45db4129beff691dc46576c328149443788af2Chris Kirby else
ca45db4129beff691dc46576c328149443788af2Chris Kirby error = zap_remove(mos, zapobj, name, tx);
ca45db4129beff691dc46576c328149443788af2Chris Kirby strfree(name);
ca45db4129beff691dc46576c328149443788af2Chris Kirby
ca45db4129beff691dc46576c328149443788af2Chris Kirby return (error);
ca45db4129beff691dc46576c328149443788af2Chris Kirby}
ca45db4129beff691dc46576c328149443788af2Chris Kirby
ca45db4129beff691dc46576c328149443788af2Chris Kirby/*
ca45db4129beff691dc46576c328149443788af2Chris Kirby * Add a temporary hold for the given dataset object and tag.
ca45db4129beff691dc46576c328149443788af2Chris Kirby */
ca45db4129beff691dc46576c328149443788af2Chris Kirbyint
ca45db4129beff691dc46576c328149443788af2Chris Kirbydsl_pool_user_hold(dsl_pool_t *dp, uint64_t dsobj, const char *tag,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens uint64_t now, dmu_tx_t *tx)
ca45db4129beff691dc46576c328149443788af2Chris Kirby{
15508ac067f2fb55a439711838b971c02d42316fChris Kirby return (dsl_pool_user_hold_rele_impl(dp, dsobj, tag, now, tx, B_TRUE));
ca45db4129beff691dc46576c328149443788af2Chris Kirby}
ca45db4129beff691dc46576c328149443788af2Chris Kirby
ca45db4129beff691dc46576c328149443788af2Chris Kirby/*
ca45db4129beff691dc46576c328149443788af2Chris Kirby * Release a temporary hold for the given dataset object and tag.
ca45db4129beff691dc46576c328149443788af2Chris Kirby */
ca45db4129beff691dc46576c328149443788af2Chris Kirbyint
ca45db4129beff691dc46576c328149443788af2Chris Kirbydsl_pool_user_release(dsl_pool_t *dp, uint64_t dsobj, const char *tag,
ca45db4129beff691dc46576c328149443788af2Chris Kirby dmu_tx_t *tx)
ca45db4129beff691dc46576c328149443788af2Chris Kirby{
ca45db4129beff691dc46576c328149443788af2Chris Kirby return (dsl_pool_user_hold_rele_impl(dp, dsobj, tag, NULL,
ca45db4129beff691dc46576c328149443788af2Chris Kirby tx, B_FALSE));
ca45db4129beff691dc46576c328149443788af2Chris Kirby}
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens/*
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * DSL Pool Configuration Lock
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens *
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * The dp_config_rwlock protects against changes to DSL state (e.g. dataset
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * creation / destruction / rename / property setting). It must be held for
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * read to hold a dataset or dsl_dir. I.e. you must call
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * dsl_pool_config_enter() or dsl_pool_hold() before calling
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * dsl_{dataset,dir}_hold{_obj}. In most circumstances, the dp_config_rwlock
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * must be held continuously until all datasets and dsl_dirs are released.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens *
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * The only exception to this rule is that if a "long hold" is placed on
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * a dataset, then the dp_config_rwlock may be dropped while the dataset
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * is still held. The long hold will prevent the dataset from being
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * destroyed -- the destroy will fail with EBUSY. A long hold can be
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * obtained by calling dsl_dataset_long_hold(), or by "owning" a dataset
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * (by calling dsl_{dataset,objset}_{try}own{_obj}).
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens *
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * Legitimate long-holders (including owners) should be long-running, cancelable
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * tasks that should cause "zfs destroy" to fail. This includes DMU
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * consumers (i.e. a ZPL filesystem being mounted or ZVOL being open),
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * "zfs send", and "zfs diff". There are several other long-holders whose
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * uses are suboptimal (e.g. "zfs promote", and zil_suspend()).
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens *
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * The usual formula for long-holding would be:
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * dsl_pool_hold()
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * dsl_dataset_hold()
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * ... perform checks ...
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * dsl_dataset_long_hold()
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * dsl_pool_rele()
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * ... perform long-running task ...
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * dsl_dataset_long_rele()
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * dsl_dataset_rele()
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens *
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * Note that when the long hold is released, the dataset is still held but
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * the pool is not held. The dataset may change arbitrarily during this time
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * (e.g. it could be destroyed). Therefore you shouldn't do anything to the
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * dataset except release it.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens *
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * User-initiated operations (e.g. ioctls, zfs_ioc_*()) are either read-only
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * or modifying operations.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens *
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * Modifying operations should generally use dsl_sync_task(). The synctask
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * infrastructure enforces proper locking strategy with respect to the
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * dp_config_rwlock. See the comment above dsl_sync_task() for details.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens *
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * Read-only operations will manually hold the pool, then the dataset, obtain
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * information from the dataset, then release the pool and dataset.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * dmu_objset_{hold,rele}() are convenience routines that also do the pool
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * hold/rele.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensint
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_pool_hold(const char *name, void *tag, dsl_pool_t **dp)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens spa_t *spa;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens int error;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens error = spa_open(name, &spa, tag);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (error == 0) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens *dp = spa_get_dsl(spa);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_pool_config_enter(*dp, tag);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (error);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens}
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensvoid
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_pool_rele(dsl_pool_t *dp, void *tag)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_pool_config_exit(dp, tag);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens spa_close(dp->dp_spa, tag);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens}
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensvoid
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_pool_config_enter(dsl_pool_t *dp, void *tag)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /*
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * We use a "reentrant" reader-writer lock, but not reentrantly.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens *
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * The rrwlock can (with the track_all flag) track all reading threads,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * which is very useful for debugging which code path failed to release
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * the lock, and for verifying that the *current* thread does hold
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * the lock.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens *
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * (Unlike a rwlock, which knows that N threads hold it for
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * read, but not *which* threads, so rw_held(RW_READER) returns TRUE
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * if any thread holds it for read, even if this thread doesn't).
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT(!rrw_held(&dp->dp_config_rwlock, RW_READER));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens rrw_enter(&dp->dp_config_rwlock, RW_READER, tag);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens}
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensvoid
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_pool_config_exit(dsl_pool_t *dp, void *tag)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens rrw_exit(&dp->dp_config_rwlock, tag);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens}
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensboolean_t
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_pool_config_held(dsl_pool_t *dp)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (RRW_LOCK_HELD(&dp->dp_config_rwlock));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens}