dsl_pool.c revision 88b7b0f29b20b808b9e06071885b1d6a3ddb6328
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/*
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Use is subject to license terms.
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <sys/dsl_pool.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <sys/dsl_dataset.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <sys/dsl_dir.h>
1d452cf5123cb6ac0a013a4dbd4dcceeb0da314dahrens#include <sys/dsl_synctask.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>
fa9e4066f08beec538e775443c5be79dd423fcabahrens
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybeeint zfs_no_write_throttle = 0;
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybeeint zfs_write_limit_shift = 3; /* 1/8th of physical memory */
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybeeint zfs_txg_synctime = 5; /* target secs 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
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrockstatic int
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
088f389458728c464569a5506b58070254fa4f7dahrens return (dsl_dir_open_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;
5ad820458efd0fdb914baff9c1447c22b819fa23nd rw_init(&dp->dp_config_rwlock, NULL, RW_DEFAULT, NULL);
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));
fa9e4066f08beec538e775443c5be79dd423fcabahrens txg_list_create(&dp->dp_dirty_dirs,
fa9e4066f08beec538e775443c5be79dd423fcabahrens offsetof(dsl_dir_t, dd_dirty_link));
1d452cf5123cb6ac0a013a4dbd4dcceeb0da314dahrens txg_list_create(&dp->dp_sync_tasks,
1d452cf5123cb6ac0a013a4dbd4dcceeb0da314dahrens offsetof(dsl_sync_task_group_t, dstg_node));
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens list_create(&dp->dp_synced_datasets, sizeof (dsl_dataset_t),
fa9e4066f08beec538e775443c5be79dd423fcabahrens offsetof(dsl_dataset_t, ds_synced_link));
fa9e4066f08beec538e775443c5be79dd423fcabahrens
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee mutex_init(&dp->dp_lock, NULL, MUTEX_DEFAULT, NULL);
088f389458728c464569a5506b58070254fa4f7dahrens mutex_init(&dp->dp_scrub_cancel_lock, NULL, MUTEX_DEFAULT, NULL);
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (dp);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrockint
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrockdsl_pool_open(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);
088f389458728c464569a5506b58070254fa4f7dahrens dsl_dir_t *dd;
088f389458728c464569a5506b58070254fa4f7dahrens dsl_dataset_t *ds;
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock objset_impl_t *osi;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
088f389458728c464569a5506b58070254fa4f7dahrens rw_enter(&dp->dp_config_rwlock, RW_WRITER);
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock err = dmu_objset_open_impl(spa, NULL, &dp->dp_meta_rootbp, &osi);
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock if (err)
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock goto out;
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock dp->dp_meta_objset = &osi->os;
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock
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
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock err = dsl_dir_open_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
088f389458728c464569a5506b58070254fa4f7dahrens if (spa_version(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);
088f389458728c464569a5506b58070254fa4f7dahrens if (err)
088f389458728c464569a5506b58070254fa4f7dahrens goto out;
088f389458728c464569a5506b58070254fa4f7dahrens err = dsl_dataset_hold_obj(dp, ds->ds_phys->ds_prev_snap_obj,
088f389458728c464569a5506b58070254fa4f7dahrens dp, &dp->dp_origin_snap);
088f389458728c464569a5506b58070254fa4f7dahrens if (err)
088f389458728c464569a5506b58070254fa4f7dahrens goto out;
088f389458728c464569a5506b58070254fa4f7dahrens dsl_dataset_rele(ds, FTAG);
088f389458728c464569a5506b58070254fa4f7dahrens dsl_dir_close(dd, dp);
088f389458728c464569a5506b58070254fa4f7dahrens }
088f389458728c464569a5506b58070254fa4f7dahrens
088f389458728c464569a5506b58070254fa4f7dahrens /* get scrub status */
088f389458728c464569a5506b58070254fa4f7dahrens err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
088f389458728c464569a5506b58070254fa4f7dahrens DMU_POOL_SCRUB_FUNC, sizeof (uint32_t), 1,
088f389458728c464569a5506b58070254fa4f7dahrens &dp->dp_scrub_func);
088f389458728c464569a5506b58070254fa4f7dahrens if (err == 0) {
088f389458728c464569a5506b58070254fa4f7dahrens err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
088f389458728c464569a5506b58070254fa4f7dahrens DMU_POOL_SCRUB_QUEUE, sizeof (uint64_t), 1,
088f389458728c464569a5506b58070254fa4f7dahrens &dp->dp_scrub_queue_obj);
088f389458728c464569a5506b58070254fa4f7dahrens if (err)
088f389458728c464569a5506b58070254fa4f7dahrens goto out;
088f389458728c464569a5506b58070254fa4f7dahrens err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
088f389458728c464569a5506b58070254fa4f7dahrens DMU_POOL_SCRUB_MIN_TXG, sizeof (uint64_t), 1,
088f389458728c464569a5506b58070254fa4f7dahrens &dp->dp_scrub_min_txg);
088f389458728c464569a5506b58070254fa4f7dahrens if (err)
088f389458728c464569a5506b58070254fa4f7dahrens goto out;
088f389458728c464569a5506b58070254fa4f7dahrens err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
088f389458728c464569a5506b58070254fa4f7dahrens DMU_POOL_SCRUB_MAX_TXG, sizeof (uint64_t), 1,
088f389458728c464569a5506b58070254fa4f7dahrens &dp->dp_scrub_max_txg);
088f389458728c464569a5506b58070254fa4f7dahrens if (err)
088f389458728c464569a5506b58070254fa4f7dahrens goto out;
088f389458728c464569a5506b58070254fa4f7dahrens err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
088f389458728c464569a5506b58070254fa4f7dahrens DMU_POOL_SCRUB_BOOKMARK, sizeof (uint64_t), 4,
088f389458728c464569a5506b58070254fa4f7dahrens &dp->dp_scrub_bookmark);
088f389458728c464569a5506b58070254fa4f7dahrens if (err)
088f389458728c464569a5506b58070254fa4f7dahrens goto out;
088f389458728c464569a5506b58070254fa4f7dahrens err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
088f389458728c464569a5506b58070254fa4f7dahrens DMU_POOL_SCRUB_ERRORS, sizeof (uint64_t), 1,
088f389458728c464569a5506b58070254fa4f7dahrens &spa->spa_scrub_errors);
088f389458728c464569a5506b58070254fa4f7dahrens if (err)
088f389458728c464569a5506b58070254fa4f7dahrens goto out;
088f389458728c464569a5506b58070254fa4f7dahrens if (spa_version(spa) < SPA_VERSION_DSL_SCRUB) {
088f389458728c464569a5506b58070254fa4f7dahrens /*
088f389458728c464569a5506b58070254fa4f7dahrens * A new-type scrub was in progress on an old
088f389458728c464569a5506b58070254fa4f7dahrens * pool. Restart from the beginning, since the
088f389458728c464569a5506b58070254fa4f7dahrens * old software may have changed the pool in the
088f389458728c464569a5506b58070254fa4f7dahrens * meantime.
088f389458728c464569a5506b58070254fa4f7dahrens */
088f389458728c464569a5506b58070254fa4f7dahrens dsl_pool_scrub_restart(dp);
088f389458728c464569a5506b58070254fa4f7dahrens }
088f389458728c464569a5506b58070254fa4f7dahrens } else {
088f389458728c464569a5506b58070254fa4f7dahrens /*
088f389458728c464569a5506b58070254fa4f7dahrens * It's OK if there is no scrub in progress (and if
088f389458728c464569a5506b58070254fa4f7dahrens * there was an I/O error, ignore it).
088f389458728c464569a5506b58070254fa4f7dahrens */
088f389458728c464569a5506b58070254fa4f7dahrens err = 0;
088f389458728c464569a5506b58070254fa4f7dahrens }
088f389458728c464569a5506b58070254fa4f7dahrens
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrockout:
fa9e4066f08beec538e775443c5be79dd423fcabahrens rw_exit(&dp->dp_config_rwlock);
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock if (err)
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock dsl_pool_close(dp);
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock else
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock *dpp = dp;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
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)
088f389458728c464569a5506b58070254fa4f7dahrens dsl_dataset_drop_ref(dp->dp_origin_snap, dp);
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock if (dp->dp_mos_dir)
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock dsl_dir_close(dp->dp_mos_dir, dp);
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock if (dp->dp_root_dir)
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock dsl_dir_close(dp->dp_root_dir, dp);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens /* undo the dmu_objset_open_impl(mos) from dsl_pool_open() */
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock if (dp->dp_meta_objset)
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock dmu_objset_evict(NULL, dp->dp_meta_objset->os);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens txg_list_destroy(&dp->dp_dirty_datasets);
fa9e4066f08beec538e775443c5be79dd423fcabahrens txg_list_destroy(&dp->dp_dirty_dirs);
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens list_destroy(&dp->dp_synced_datasets);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
874395d5f8cae2b9cd2d1fcbfcfe963a0c23966dmaybee arc_flush(dp->dp_spa);
fa9e4066f08beec538e775443c5be79dd423fcabahrens txg_fini(dp);
5ad820458efd0fdb914baff9c1447c22b819fa23nd rw_destroy(&dp->dp_config_rwlock);
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee mutex_destroy(&dp->dp_lock);
088f389458728c464569a5506b58070254fa4f7dahrens mutex_destroy(&dp->dp_scrub_cancel_lock);
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);
088f389458728c464569a5506b58070254fa4f7dahrens objset_impl_t *osip;
088f389458728c464569a5506b58070254fa4f7dahrens dsl_dataset_t *ds;
088f389458728c464569a5506b58070254fa4f7dahrens uint64_t dsobj;
088f389458728c464569a5506b58070254fa4f7dahrens
088f389458728c464569a5506b58070254fa4f7dahrens /* create and open the MOS (meta-objset) */
fa9e4066f08beec538e775443c5be79dd423fcabahrens dp->dp_meta_objset = &dmu_objset_create_impl(spa,
c717a56157ae0e6fca6a1e3689ae1edc385716a3maybee NULL, &dp->dp_meta_rootbp, DMU_OST_META, tx)->os;
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);
fa9e4066f08beec538e775443c5be79dd423fcabahrens ASSERT3U(err, ==, 0);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens /* create and open the root dir */
088f389458728c464569a5506b58070254fa4f7dahrens dp->dp_root_dir_obj = dsl_dir_create_sync(dp, NULL, NULL, tx);
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock VERIFY(0 == dsl_dir_open_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);
088f389458728c464569a5506b58070254fa4f7dahrens VERIFY(0 == dsl_pool_open_special_dir(dp,
088f389458728c464569a5506b58070254fa4f7dahrens MOS_DIR_NAME, &dp->dp_mos_dir));
088f389458728c464569a5506b58070254fa4f7dahrens
088f389458728c464569a5506b58070254fa4f7dahrens if (spa_version(spa) >= SPA_VERSION_DSL_SCRUB)
088f389458728c464569a5506b58070254fa4f7dahrens dsl_pool_create_origin(dp, tx);
088f389458728c464569a5506b58070254fa4f7dahrens
088f389458728c464569a5506b58070254fa4f7dahrens /* create the root dataset */
088f389458728c464569a5506b58070254fa4f7dahrens dsobj = dsl_dataset_create_sync_dd(dp->dp_root_dir, NULL, 0, tx);
088f389458728c464569a5506b58070254fa4f7dahrens
088f389458728c464569a5506b58070254fa4f7dahrens /* create the root objset */
088f389458728c464569a5506b58070254fa4f7dahrens VERIFY(0 == dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
088f389458728c464569a5506b58070254fa4f7dahrens osip = dmu_objset_create_impl(dp->dp_spa, ds,
088f389458728c464569a5506b58070254fa4f7dahrens dsl_dataset_get_blkptr(ds), DMU_OST_ZFS, tx);
088f389458728c464569a5506b58070254fa4f7dahrens#ifdef _KERNEL
0a48a24e663a04e34e2ed4e55390ad96f178dbeatimh zfs_create_fs(&osip->os, kcred, zplprops, tx);
088f389458728c464569a5506b58070254fa4f7dahrens#endif
088f389458728c464569a5506b58070254fa4f7dahrens dsl_dataset_rele(ds, FTAG);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens dmu_tx_commit(tx);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (dp);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
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;
c717a56157ae0e6fca6a1e3689ae1edc385716a3maybee dsl_sync_task_group_t *dstg;
fa9e4066f08beec538e775443c5be79dd423fcabahrens objset_impl_t *mosi = dp->dp_meta_objset->os;
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee hrtime_t start, write_time;
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee uint64_t data_written;
c717a56157ae0e6fca6a1e3689ae1edc385716a3maybee int err;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens tx = dmu_tx_create_assigned(dp, txg);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee dp->dp_read_overhead = 0;
c717a56157ae0e6fca6a1e3689ae1edc385716a3maybee zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
c717a56157ae0e6fca6a1e3689ae1edc385716a3maybee while (ds = txg_list_remove(&dp->dp_dirty_datasets, txg)) {
c717a56157ae0e6fca6a1e3689ae1edc385716a3maybee if (!list_link_active(&ds->ds_synced_link))
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens list_insert_tail(&dp->dp_synced_datasets, ds);
af2c4821c0a23e873f2a63bca4145080aa2183e3maybee else
af2c4821c0a23e873f2a63bca4145080aa2183e3maybee dmu_buf_rele(ds->ds_dbuf, ds);
c717a56157ae0e6fca6a1e3689ae1edc385716a3maybee dsl_dataset_sync(ds, zio, tx);
c717a56157ae0e6fca6a1e3689ae1edc385716a3maybee }
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee DTRACE_PROBE(pool_sync__1setup);
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee start = gethrtime();
c717a56157ae0e6fca6a1e3689ae1edc385716a3maybee err = zio_wait(zio);
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee write_time = gethrtime() - start;
c717a56157ae0e6fca6a1e3689ae1edc385716a3maybee ASSERT(err == 0);
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee DTRACE_PROBE(pool_sync__2rootzio);
c717a56157ae0e6fca6a1e3689ae1edc385716a3maybee
c717a56157ae0e6fca6a1e3689ae1edc385716a3maybee while (dstg = txg_list_remove(&dp->dp_sync_tasks, txg))
c717a56157ae0e6fca6a1e3689ae1edc385716a3maybee dsl_sync_task_group_sync(dstg, tx);
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee DTRACE_PROBE(pool_sync__3task);
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee
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
088f389458728c464569a5506b58070254fa4f7dahrens if (spa_sync_pass(dp->dp_spa) == 1)
088f389458728c464569a5506b58070254fa4f7dahrens dsl_pool_scrub_sync(dp, tx);
088f389458728c464569a5506b58070254fa4f7dahrens
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee start = gethrtime();
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (list_head(&mosi->os_dirty_dnodes[txg & TXG_MASK]) != NULL ||
fa9e4066f08beec538e775443c5be79dd423fcabahrens list_head(&mosi->os_free_dnodes[txg & TXG_MASK]) != NULL) {
c717a56157ae0e6fca6a1e3689ae1edc385716a3maybee zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
c717a56157ae0e6fca6a1e3689ae1edc385716a3maybee dmu_objset_sync(mosi, 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
fa9e4066f08beec538e775443c5be79dd423fcabahrens dmu_tx_commit(tx);
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee data_written = dp->dp_space_towrite[txg & TXG_MASK];
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
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee */
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee ASSERT(zfs_write_limit_min > 0);
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee if (data_written > zfs_write_limit_min / 8 && write_time > 0) {
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee uint64_t throughput = (data_written * NANOSEC) / write_time;
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,
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee dp->dp_throughput * zfs_txg_synctime));
05715f945c5c007fc4bb6a4e7cf4a749c9b30038Mark Maybee }
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrensvoid
fa9e4066f08beec538e775443c5be79dd423fcabahrensdsl_pool_zil_clean(dsl_pool_t *dp)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens dsl_dataset_t *ds;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens while (ds = list_head(&dp->dp_synced_datasets)) {
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens list_remove(&dp->dp_synced_datasets, ds);
fa9e4066f08beec538e775443c5be79dd423fcabahrens ASSERT(ds->ds_user_ptr != NULL);
fa9e4066f08beec538e775443c5be79dd423fcabahrens zil_clean(((objset_impl_t *)ds->ds_user_ptr)->os_zil);
af2c4821c0a23e873f2a63bca4145080aa2183e3maybee dmu_buf_rele(ds->ds_dbuf, ds);
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
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 ||
c717a56157ae0e6fca6a1e3689ae1edc385716a3maybee spa_get_dsl(dp->dp_spa) == NULL);
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 */
99653d4ee642c6528e88224f12409a5f23060994eschrock 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
088f389458728c464569a5506b58070254fa4f7dahrensupgrade_clones_cb(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg)
088f389458728c464569a5506b58070254fa4f7dahrens{
088f389458728c464569a5506b58070254fa4f7dahrens dmu_tx_t *tx = arg;
088f389458728c464569a5506b58070254fa4f7dahrens dsl_dataset_t *ds, *prev = NULL;
088f389458728c464569a5506b58070254fa4f7dahrens int err;
088f389458728c464569a5506b58070254fa4f7dahrens dsl_pool_t *dp = spa_get_dsl(spa);
088f389458728c464569a5506b58070254fa4f7dahrens
088f389458728c464569a5506b58070254fa4f7dahrens err = dsl_dataset_hold_obj(dp, dsobj, 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 */
088f389458728c464569a5506b58070254fa4f7dahrens ASSERT(prev->ds_phys->ds_bp.blk_birth == 0);
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);
088f389458728c464569a5506b58070254fa4f7dahrens VERIFY(0 == dsl_dataset_hold_obj(dp,
088f389458728c464569a5506b58070254fa4f7dahrens ds->ds_phys->ds_prev_snap_obj, ds, &ds->ds_prev));
088f389458728c464569a5506b58070254fa4f7dahrens }
088f389458728c464569a5506b58070254fa4f7dahrens }
088f389458728c464569a5506b58070254fa4f7dahrens
088f389458728c464569a5506b58070254fa4f7dahrens ASSERT(ds->ds_dir->dd_phys->dd_origin_obj == prev->ds_object);
088f389458728c464569a5506b58070254fa4f7dahrens ASSERT(ds->ds_phys->ds_prev_snap_obj == prev->ds_object);
088f389458728c464569a5506b58070254fa4f7dahrens
088f389458728c464569a5506b58070254fa4f7dahrens if (prev->ds_phys->ds_next_clones_obj == 0) {
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 }
088f389458728c464569a5506b58070254fa4f7dahrens VERIFY(0 == 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
088f389458728c464569a5506b58070254fa4f7dahrens (void) dmu_objset_find_spa(dp->dp_spa, NULL, upgrade_clones_cb,
088f389458728c464569a5506b58070254fa4f7dahrens tx, DS_FIND_CHILDREN);
088f389458728c464569a5506b58070254fa4f7dahrens}
088f389458728c464569a5506b58070254fa4f7dahrens
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);
088f389458728c464569a5506b58070254fa4f7dahrens
088f389458728c464569a5506b58070254fa4f7dahrens /* create the origin dir, ds, & snap-ds */
088f389458728c464569a5506b58070254fa4f7dahrens rw_enter(&dp->dp_config_rwlock, RW_WRITER);
088f389458728c464569a5506b58070254fa4f7dahrens dsobj = dsl_dataset_create_sync(dp->dp_root_dir, ORIGIN_DIR_NAME,
088f389458728c464569a5506b58070254fa4f7dahrens NULL, 0, kcred, tx);
088f389458728c464569a5506b58070254fa4f7dahrens VERIFY(0 == dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
088f389458728c464569a5506b58070254fa4f7dahrens dsl_dataset_snapshot_sync(ds, ORIGIN_DIR_NAME, kcred, tx);
088f389458728c464569a5506b58070254fa4f7dahrens VERIFY(0 == 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 rw_exit(&dp->dp_config_rwlock);
088f389458728c464569a5506b58070254fa4f7dahrens}