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.
43297f973a3543e7403ac27076490ab958a94b15George Wilson * Copyright (c) 2011, 2016 by Delphix. All rights reserved.
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * Copyright (c) 2013 Steven Hartland. All rights reserved.
bc9014e6a81272073b9854d9f65dd59e18d18c35Justin Gibbs * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
c3d26abc9ee97b4f60233556aadeb57e0bd30bb9Matthew Ahrens * Copyright (c) 2014 Integros [integros.com]
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
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens/*
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * ZFS Write Throttle
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * ------------------
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens *
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * ZFS must limit the rate of incoming writes to the rate at which it is able
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * to sync data modifications to the backend storage. Throttling by too much
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * creates an artificial limit; throttling by too little can only be sustained
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * for short periods and would lead to highly lumpy performance. On a per-pool
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * basis, ZFS tracks the amount of modified (dirty) data. As operations change
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * data, the amount of dirty data increases; as ZFS syncs out data, the amount
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * of dirty data decreases. When the amount of dirty data exceeds a
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * predetermined threshold further modifications are blocked until the amount
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * of dirty data decreases (as data is synced out).
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens *
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * The limit on dirty data is tunable, and should be adjusted according to
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * both the IO capacity and available memory of the system. The larger the
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * window, the more ZFS is able to aggregate and amortize metadata (and data)
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * changes. However, memory is a limited resource, and allowing for more dirty
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * data comes at the cost of keeping other useful data in memory (for example
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * ZFS data cached by the ARC).
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens *
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * Implementation
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens *
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * As buffers are modified dsl_pool_willuse_space() increments both the per-
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * txg (dp_dirty_pertxg[]) and poolwide (dp_dirty_total) accounting of
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * dirty space used; dsl_pool_dirty_space() decrements those values as data
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * is synced out from dsl_pool_sync(). While only the poolwide value is
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * relevant, the per-txg value is useful for debugging. The tunable
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * zfs_dirty_data_max determines the dirty space limit. Once that value is
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * exceeded, new writes are halted until space frees up.
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens *
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * The zfs_dirty_data_sync tunable dictates the threshold at which we
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * ensure that there is a txg syncing (see the comment in txg.c for a full
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * description of transaction group stages).
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens *
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * The IO scheduler uses both the dirty space limit and current amount of
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * dirty data as inputs. Those values affect the number of concurrent IOs ZFS
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * issues. See the comment in vdev_queue.c for details of the IO scheduler.
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens *
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * The delay is also calculated based on the amount of dirty data. See the
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * comment above dmu_tx_delay() for details.
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens */
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens/*
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * zfs_dirty_data_max will be set to zfs_dirty_data_max_percent% of all memory,
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * capped at zfs_dirty_data_max_max. It can also be overridden in /etc/system.
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens */
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrensuint64_t zfs_dirty_data_max;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrensuint64_t zfs_dirty_data_max_max = 4ULL * 1024 * 1024 * 1024;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrensint zfs_dirty_data_max_percent = 10;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens/*
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * If there is at least this much dirty data, push out a txg.
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens */
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrensuint64_t zfs_dirty_data_sync = 64 * 1024 * 1024;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens/*
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * Once there is this amount of dirty data, the dmu_tx_delay() will kick in
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * and delay each transaction.
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * This value should be >= zfs_vdev_async_write_active_max_dirty_percent.
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens */
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrensint zfs_delay_min_dirty_percent = 60;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens/*
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * This controls how quickly the delay approaches infinity.
d85a1e969ce08b3bc4a7cb3626a6e33a28b5caebMatthew Ahrens * Larger values cause it to delay more for a given amount of dirty data.
d85a1e969ce08b3bc4a7cb3626a6e33a28b5caebMatthew Ahrens * Therefore larger values will cause there to be less dirty data for a
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * given throughput.
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens *
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * For the smoothest delay, this value should be about 1 billion divided
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * by the maximum number of operations per second. This will smoothly
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * handle between 10x and 1/10th this number.
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens *
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * Note: zfs_delay_scale * zfs_dirty_data_max must be < 2^64, due to the
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * multiply in dmu_tx_delay().
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens */
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrensuint64_t zfs_delay_scale = 1000 * 1000 * 1000 / 2000;
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee
0689f76c08c5e553ff25ac43a852b56c430bb61eAdam Leventhalhrtime_t zfs_throttle_delay = MSEC2NSEC(10);
0689f76c08c5e553ff25ac43a852b56c430bb61eAdam Leventhalhrtime_t zfs_throttle_resolution = MSEC2NSEC(10);
0689f76c08c5e553ff25ac43a852b56c430bb61eAdam Leventhal
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,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dir_phys(dp->dp_root_dir)->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);
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);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens cv_init(&dp->dp_spaceavail_cv, NULL, CV_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;
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs err = dsl_dataset_hold_obj(dp,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dir_phys(dd)->dd_head_dataset_obj, FTAG, &ds);
8f63aa464994c24e12bd657bac93d1b2e2df2d1cLin Ling if (err == 0) {
8f63aa464994c24e12bd657bac93d1b2e2df2d1cLin Ling err = dsl_dataset_hold_obj(dp,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->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
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens /*
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens * Note: errors ignored, because the leak dir will not exist if we
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens * have not encountered a leak yet.
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens */
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens (void) dsl_pool_open_special_dir(dp, LEAK_DIR_NAME,
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens &dp->dp_leak_dir);
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens if (spa_feature_is_active(dp->dp_spa, 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
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens if (spa_feature_is_active(dp->dp_spa, 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 /*
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * Drop our references from dsl_pool_open().
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens *
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);
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens if (dp->dp_leak_dir)
7fd05ac4dec0c343d2f68f310d3718b715ecfbafMatthew Ahrens dsl_dir_rele(dp->dp_leak_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
244781f10dcd82684fd8163c016540667842f203Prakash Surya /*
244781f10dcd82684fd8163c016540667842f203Prakash Surya * We can't set retry to TRUE since we're explicitly specifying
244781f10dcd82684fd8163c016540667842f203Prakash Surya * a spa to flush. This is good enough; any missed buffers for
244781f10dcd82684fd8163c016540667842f203Prakash Surya * this spa won't cause trouble, and they'll eventually fall
244781f10dcd82684fd8163c016540667842f203Prakash Surya * out of the ARC just like any other unused buffer.
244781f10dcd82684fd8163c016540667842f203Prakash Surya */
244781f10dcd82684fd8163c016540667842f203Prakash Surya arc_flush(dp->dp_spa, FALSE);
244781f10dcd82684fd8163c016540667842f203Prakash Surya
fa9e4066f08beec538e775443c5be79dd423fcabahrens txg_fini(dp);
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Ling dsl_scan_fini(dp);
bc9014e6a81272073b9854d9f65dd59e18d18c35Justin Gibbs dmu_buf_user_evict_wait();
bc9014e6a81272073b9854d9f65dd59e18d18c35Justin Gibbs
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 */
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens obj = bpobj_alloc(dp->dp_meta_objset, SPA_OLD_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));
c166b69d29138aed7a415fe7cef698e54c6ae945Paul Dagnelie rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
503ad85c168c7992ccc310af845a581cff3c72b5Matthew Ahrens os = dmu_objset_create_impl(dp->dp_spa, ds,
088f389458728c464569a5506b58070254fa4f7dahrens dsl_dataset_get_blkptr(ds), DMU_OST_ZFS, tx);
c166b69d29138aed7a415fe7cef698e54c6ae945Paul Dagnelie rrw_exit(&ds->ds_bp_rwlock, FTAG);
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
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrensstatic void
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrensdsl_pool_sync_mos(dsl_pool_t *dp, dmu_tx_t *tx)
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens{
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens zio_t *zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens dmu_objset_sync(dp->dp_meta_objset, zio, tx);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens VERIFY0(zio_wait(zio));
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens dprintf_bp(&dp->dp_meta_rootbp, "meta objset rootbp is %s", "");
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens spa_set_rootblkptr(dp->dp_spa, &dp->dp_meta_rootbp);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens}
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrensstatic void
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrensdsl_pool_dirty_delta(dsl_pool_t *dp, int64_t delta)
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens{
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens ASSERT(MUTEX_HELD(&dp->dp_lock));
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens if (delta < 0)
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens ASSERT3U(-delta, <=, dp->dp_dirty_total);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens dp->dp_dirty_total += delta;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens /*
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * Note: we signal even when increasing dp_dirty_total.
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * This ensures forward progress -- each thread wakes the next waiter.
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens */
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens if (dp->dp_dirty_total <= zfs_dirty_data_max)
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens cv_signal(&dp->dp_spaceavail_cv);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens}
69962b5647e4a8b9b14998733b765925381b727eMatthew 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;
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
fa9e4066f08beec538e775443c5be79dd423fcabahrens tx = dmu_tx_create_assigned(dp, txg);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens /*
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * Write out all dirty blocks of dirty datasets.
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens */
c717a56157ae0e6fca6a1e3689ae1edc385716a3maybee zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens while ((ds = txg_list_remove(&dp->dp_dirty_datasets, txg)) != NULL) {
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 }
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens VERIFY0(zio_wait(zio));
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens /*
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * We have written all of the accounted dirty data, so our
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * dp_space_towrite should now be zero. However, some seldom-used
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * code paths do not adhere to this (e.g. dbuf_undirty(), also
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * rounding error in dbuf_write_physdone).
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * Shore up the accounting of any dirtied space now.
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens */
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens dsl_pool_undirty_space(dp, dp->dp_dirty_pertxg[txg & TXG_MASK], txg);
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 */
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens for (ds = list_head(&synced_datasets); ds != NULL;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens ds = list_next(&synced_datasets, ds)) {
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaum dmu_objset_do_userquota_updates(ds->ds_objset, tx);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens }
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);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens while ((ds = txg_list_remove(&dp->dp_dirty_datasets, txg)) != NULL) {
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 }
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens VERIFY0(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 */
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens while ((ds = list_remove_head(&synced_datasets)) != NULL) {
bfaed0b91e57062c38bc16b4f89db3c8f0052a9bAndriy Gapon dsl_dataset_sync_done(ds, tx);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens }
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens while ((dd = txg_list_remove(&dp->dp_dirty_dirs, txg)) != NULL) {
c717a56157ae0e6fca6a1e3689ae1edc385716a3maybee dsl_dir_sync(dd, tx);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens }
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
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) {
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens dsl_pool_sync_mos(dp, tx);
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
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 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 */
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens ASSERT3U(spa_sync_pass(dp->dp_spa), ==, 1);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens while ((dst = txg_list_remove(&dp->dp_sync_tasks, txg)) != NULL)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_sync_task_sync(dst, tx);
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens }
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens dmu_tx_commit(tx);
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens DTRACE_PROBE2(dsl_pool_sync__done, dsl_pool_t *dp, dp, uint64_t, txg);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrensvoid
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickdsl_pool_sync_done(dsl_pool_t *dp, uint64_t txg)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens zilog_t *zilog;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
43297f973a3543e7403ac27076490ab958a94b15George Wilson while (zilog = txg_list_head(&dp->dp_dirty_zilogs, txg)) {
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens dsl_dataset_t *ds = dmu_objset_ds(zilog->zl_os);
43297f973a3543e7403ac27076490ab958a94b15George Wilson /*
43297f973a3543e7403ac27076490ab958a94b15George Wilson * We don't remove the zilog from the dp_dirty_zilogs
43297f973a3543e7403ac27076490ab958a94b15George Wilson * list until after we've cleaned it. This ensures that
43297f973a3543e7403ac27076490ab958a94b15George Wilson * callers of zilog_is_dirty() receive an accurate
43297f973a3543e7403ac27076490ab958a94b15George Wilson * answer when they are racing with the spa sync thread.
43297f973a3543e7403ac27076490ab958a94b15George Wilson */
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens zil_clean(zilog, txg);
43297f973a3543e7403ac27076490ab958a94b15George Wilson (void) txg_list_remove_this(&dp->dp_dirty_zilogs, 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 /*
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);
c39f2c8cacf297ec37acd421f4c4fec127991995Christopher Siden resv = spa_get_slop_space(dp->dp_spa);
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (netfree)
fa9e4066f08beec538e775443c5be79dd423fcabahrens resv >>= 1;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (space - resv);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrensboolean_t
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrensdsl_pool_need_dirty_delay(dsl_pool_t *dp)
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee{
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens uint64_t delay_min_bytes =
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens zfs_dirty_data_max * zfs_delay_min_dirty_percent / 100;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens boolean_t rv;
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens mutex_enter(&dp->dp_lock);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens if (dp->dp_dirty_total > zfs_dirty_data_sync)
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens txg_kick(dp);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens rv = (dp->dp_dirty_total > delay_min_bytes);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens mutex_exit(&dp->dp_lock);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens return (rv);
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee}
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybeevoid
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrensdsl_pool_dirty_space(dsl_pool_t *dp, int64_t space, dmu_tx_t *tx)
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee{
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens if (space > 0) {
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens mutex_enter(&dp->dp_lock);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens dp->dp_dirty_pertxg[tx->tx_txg & TXG_MASK] += space;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens dsl_pool_dirty_delta(dp, space);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens mutex_exit(&dp->dp_lock);
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee }
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee}
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybeevoid
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrensdsl_pool_undirty_space(dsl_pool_t *dp, int64_t space, uint64_t txg)
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee{
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens ASSERT3S(space, >=, 0);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens if (space == 0)
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens return;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens mutex_enter(&dp->dp_lock);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens if (dp->dp_dirty_pertxg[txg & TXG_MASK] < space) {
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens /* XXX writing something we didn't dirty? */
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens space = dp->dp_dirty_pertxg[txg & TXG_MASK];
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee }
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens ASSERT3U(dp->dp_dirty_pertxg[txg & TXG_MASK], >=, space);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens dp->dp_dirty_pertxg[txg & TXG_MASK] -= space;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens ASSERT3U(dp->dp_dirty_total, >=, space);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens dsl_pool_dirty_delta(dp, -space);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens mutex_exit(&dp->dp_lock);
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
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs while (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) {
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs err = dsl_dataset_hold_obj(dp,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_obj, FTAG, &prev);
088f389458728c464569a5506b58070254fa4f7dahrens if (err) {
088f389458728c464569a5506b58070254fa4f7dahrens dsl_dataset_rele(ds, FTAG);
088f389458728c464569a5506b58070254fa4f7dahrens return (err);
088f389458728c464569a5506b58070254fa4f7dahrens }
088f389458728c464569a5506b58070254fa4f7dahrens
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs if (dsl_dataset_phys(prev)->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 */
c166b69d29138aed7a415fe7cef698e54c6ae945Paul Dagnelie rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ASSERT0(dsl_dataset_phys(prev)->ds_bp.blk_birth);
c166b69d29138aed7a415fe7cef698e54c6ae945Paul Dagnelie rrw_exit(&ds->ds_bp_rwlock, FTAG);
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);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_obj = prev->ds_object;
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_txg =
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(prev)->ds_creation_txg;
088f389458728c464569a5506b58070254fa4f7dahrens
088f389458728c464569a5506b58070254fa4f7dahrens dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dir_phys(ds->ds_dir)->dd_origin_obj = prev->ds_object;
088f389458728c464569a5506b58070254fa4f7dahrens
088f389458728c464569a5506b58070254fa4f7dahrens dmu_buf_will_dirty(prev->ds_dbuf, tx);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(prev)->ds_num_children++;
088f389458728c464569a5506b58070254fa4f7dahrens
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_next_snap_obj == 0) {
088f389458728c464569a5506b58070254fa4f7dahrens ASSERT(ds->ds_prev == NULL);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_obj,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ds, &ds->ds_prev));
088f389458728c464569a5506b58070254fa4f7dahrens }
088f389458728c464569a5506b58070254fa4f7dahrens }
088f389458728c464569a5506b58070254fa4f7dahrens
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ASSERT3U(dsl_dir_phys(ds->ds_dir)->dd_origin_obj, ==, prev->ds_object);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ASSERT3U(dsl_dataset_phys(ds)->ds_prev_snap_obj, ==, prev->ds_object);
088f389458728c464569a5506b58070254fa4f7dahrens
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs if (dsl_dataset_phys(prev)->ds_next_clones_obj == 0) {
c33e334fd3eb2b3d91c4b9667d7a465b6924e8d3Matthew Ahrens dmu_buf_will_dirty(prev->ds_dbuf, tx);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(prev)->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,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(prev)->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,
12380e1e701fda28c9e9f32d01cafb54af279eb5Arne Jansen tx, DS_FIND_CHILDREN | DS_FIND_SERIALIZE));
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
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs if (dsl_dir_phys(ds->ds_dir)->dd_origin_obj != 0) {
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dsl_dataset_t *origin;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dir_phys(ds->ds_dir)->dd_origin_obj, FTAG, &origin));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs if (dsl_dir_phys(origin->ds_dir)->dd_clones == 0) {
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dmu_buf_will_dirty(origin->ds_dir->dd_dbuf, tx);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dir_phys(origin->ds_dir)->dd_clones =
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs zap_create(mos, DMU_OT_DSL_CLONES, DMU_OT_NONE,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs 0, tx);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens }
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(zap_add_int(dp->dp_meta_objset,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dir_phys(origin->ds_dir)->dd_clones,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs 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,
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens SPA_OLD_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,
12380e1e701fda28c9e9f32d01cafb54af279eb5Arne Jansen upgrade_dir_clones_cb, tx, DS_FIND_CHILDREN | DS_FIND_SERIALIZE));
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);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs VERIFY0(dsl_dataset_hold_obj(dp, dsl_dataset_phys(ds)->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;
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland nvlist_t *holds;
ca45db4129beff691dc46576c328149443788af2Chris Kirby
ca45db4129beff691dc46576c328149443788af2Chris Kirby if (zapobj == 0)
ca45db4129beff691dc46576c328149443788af2Chris Kirby return;
ca45db4129beff691dc46576c328149443788af2Chris Kirby ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS);
ca45db4129beff691dc46576c328149443788af2Chris Kirby
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland holds = fnvlist_alloc();
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland
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;
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland nvlist_t *tags;
ca45db4129beff691dc46576c328149443788af2Chris Kirby
ca45db4129beff691dc46576c328149443788af2Chris Kirby htag = strchr(za.za_name, '-');
ca45db4129beff691dc46576c328149443788af2Chris Kirby *htag = '\0';
ca45db4129beff691dc46576c328149443788af2Chris Kirby ++htag;
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland if (nvlist_lookup_nvlist(holds, za.za_name, &tags) != 0) {
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland tags = fnvlist_alloc();
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland fnvlist_add_boolean(tags, htag);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland fnvlist_add_nvlist(holds, za.za_name, tags);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland fnvlist_free(tags);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland } else {
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland fnvlist_add_boolean(tags, htag);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland }
ca45db4129beff691dc46576c328149443788af2Chris Kirby }
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland dsl_dataset_user_release_tmp(dp, holds);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland fnvlist_free(holds);
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 {
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(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
1d3f896f5469c69c1339890ec3d68e9feddb0343Arne Jansenvoid
1d3f896f5469c69c1339890ec3d68e9feddb0343Arne Jansendsl_pool_config_enter_prio(dsl_pool_t *dp, void *tag)
1d3f896f5469c69c1339890ec3d68e9feddb0343Arne Jansen{
1d3f896f5469c69c1339890ec3d68e9feddb0343Arne Jansen ASSERT(!rrw_held(&dp->dp_config_rwlock, RW_READER));
1d3f896f5469c69c1339890ec3d68e9feddb0343Arne Jansen rrw_enter_read_prio(&dp->dp_config_rwlock, tag);
1d3f896f5469c69c1339890ec3d68e9feddb0343Arne Jansen}
1d3f896f5469c69c1339890ec3d68e9feddb0343Arne Jansen
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}
12380e1e701fda28c9e9f32d01cafb54af279eb5Arne Jansen
12380e1e701fda28c9e9f32d01cafb54af279eb5Arne Jansenboolean_t
12380e1e701fda28c9e9f32d01cafb54af279eb5Arne Jansendsl_pool_config_held_writer(dsl_pool_t *dp)
12380e1e701fda28c9e9f32d01cafb54af279eb5Arne Jansen{
12380e1e701fda28c9e9f32d01cafb54af279eb5Arne Jansen return (RRW_WRITE_HELD(&dp->dp_config_rwlock));
12380e1e701fda28c9e9f32d01cafb54af279eb5Arne Jansen}