dsl_pool.c revision 69962b5647e4a8b9b14998733b765925381b727e
fa9e4066f08beec538e775443c5be79dd423fcabahrens * CDDL HEADER START
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 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
fa9e4066f08beec538e775443c5be79dd423fcabahrens * See the License for the specific language governing permissions
fa9e4066f08beec538e775443c5be79dd423fcabahrens * and limitations under the License.
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 * CDDL HEADER END
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Ling * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens * Copyright (c) 2013 by Delphix. All rights reserved.
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * Copyright (c) 2013 Steven Hartland. All rights reserved.
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * ZFS Write Throttle
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 * 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 * Implementation
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 * 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 * 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 * 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 * 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 Ahrensuint64_t zfs_dirty_data_max_max = 4ULL * 1024 * 1024 * 1024;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * If there is at least this much dirty data, push out a txg.
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrensuint64_t zfs_dirty_data_sync = 64 * 1024 * 1024;
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 * This controls how quickly the delay approaches infinity.
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * Larger values cause it to delay less for a given amount of dirty data.
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * Therefore larger values will cause there to be more dirty data for a
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * given throughput.
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 * Note: zfs_delay_scale * zfs_dirty_data_max must be < 2^64, due to the
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * multiply in dmu_tx_delay().
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrensuint64_t zfs_delay_scale = 1000 * 1000 * 1000 / 2000;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * XXX someday maybe turn these into #defines, and you have to tune it on a
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * per-pool basis using zfs.conf.
0689f76c08c5e553ff25ac43a852b56c430bb61eAdam Leventhalhrtime_t zfs_throttle_resolution = MSEC2NSEC(10);
088f389458728c464569a5506b58070254fa4f7dahrensdsl_pool_open_special_dir(dsl_pool_t *dp, const char *name, dsl_dir_t **ddp)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (dsl_dir_hold_obj(dp, obj, name, dp, ddp));
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens cv_init(&dp->dp_spaceavail_cv, NULL, CV_DEFAULT, NULL);
9d3574bff0b382b89dd380c1e23d742ebc156772Neil Perrin dp->dp_vnrele_taskq = taskq_create("zfs_vn_rele_taskq", 1, minclsyspri,
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Sidendsl_pool_init(spa_t *spa, uint64_t txg, dsl_pool_t **dpp)
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden err = dmu_objset_open_impl(spa, NULL, &dp->dp_meta_rootbp,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG);
fa9e4066f08beec538e775443c5be79dd423fcabahrens err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens err = dsl_dir_hold_obj(dp, dp->dp_root_dir_obj,
088f389458728c464569a5506b58070254fa4f7dahrens err = dsl_pool_open_special_dir(dp, MOS_DIR_NAME, &dp->dp_mos_dir);
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden if (spa_version(dp->dp_spa) >= SPA_VERSION_ORIGIN) {
088f389458728c464569a5506b58070254fa4f7dahrens err = dsl_pool_open_special_dir(dp, ORIGIN_DIR_NAME, &dd);
088f389458728c464569a5506b58070254fa4f7dahrens err = dsl_dataset_hold_obj(dp, dd->dd_phys->dd_head_dataset_obj,
8f63aa464994c24e12bd657bac93d1b2e2df2d1cLin Ling if (err == 0) {
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 err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens DMU_POOL_FREE_BPOBJ, sizeof (uint64_t), 1, &obj);
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden &spa_feature_table[SPA_FEATURE_ASYNC_DESTROY])) {
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden DMU_POOL_BPTREE_OBJ, sizeof (uint64_t), 1,
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens &spa_feature_table[SPA_FEATURE_EMPTY_BPOBJ])) {
f17457368189aa911f774c38c1f21875a568bdcaMatthew Ahrens err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
ca45db4129beff691dc46576c328149443788af2Chris Kirby err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden err = dsl_scan_init(dp, dp->dp_tx.tx_open_txg);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * Drop our references from dsl_pool_open().
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.
fa9e4066f08beec538e775443c5be79dd423fcabahrens /* undo the dmu_objset_open_impl(mos) from dsl_pool_open() */
88b7b0f29b20b808b9e06071885b1d6a3ddb6328Matthew Ahrens kmem_free(dp->dp_blkstats, sizeof (zfs_all_blkstats_t));
0a48a24e663a04e34e2ed4e55390ad96f178dbeatimhdsl_pool_create(spa_t *spa, nvlist_t *zplprops, uint64_t txg)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG);
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 /* create the pool directory */
fa9e4066f08beec538e775443c5be79dd423fcabahrens err = zap_create_claim(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Ling /* Initialize scan structures */
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,
fa9e4066f08beec538e775443c5be79dd423fcabahrens /* create and open the meta-objset dir */
088f389458728c464569a5506b58070254fa4f7dahrens (void) dsl_dir_create_sync(dp, dp->dp_root_dir, MOS_DIR_NAME, tx);
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 /* create and open the free_bplist */
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens obj = bpobj_alloc(dp->dp_meta_objset, SPA_MAXBLOCKSIZE, tx);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens VERIFY(zap_add(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens DMU_POOL_FREE_BPOBJ, sizeof (uint64_t), 1, &obj, tx) == 0);
088f389458728c464569a5506b58070254fa4f7dahrens /* create the root dataset */
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens obj = dsl_dataset_create_sync_dd(dp->dp_root_dir, NULL, 0, tx);
088f389458728c464569a5506b58070254fa4f7dahrens /* create the root objset */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, obj, FTAG, &ds));
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens * Account for the meta-objset space in its placeholder dsl_dir.
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens ASSERT3U(comp, ==, uncomp); /* it's all metadata */
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensdeadlist_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrensdsl_pool_sync_mos(dsl_pool_t *dp, dmu_tx_t *tx)
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 dprintf_bp(&dp->dp_meta_rootbp, "meta objset rootbp is %s", "");
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens spa_set_rootblkptr(dp->dp_spa, &dp->dp_meta_rootbp);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrensdsl_pool_dirty_delta(dsl_pool_t *dp, int64_t delta)
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 if (dp->dp_dirty_total <= zfs_dirty_data_max)
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens list_create(&synced_datasets, sizeof (dsl_dataset_t),
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * Write out all dirty blocks of dirty datasets.
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 * 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 ASSERT(!list_link_active(&ds->ds_synced_link));
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 dsl_pool_undirty_space(dp, dp->dp_dirty_pertxg[txg & TXG_MASK], txg);
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens * After the data blocks have been written (ensured by the zio_wait()
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens * above), update the user/group space accounting.
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens for (ds = list_head(&synced_datasets); ds != NULL;
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaum dmu_objset_do_userquota_updates(ds->ds_objset, tx);
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 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));
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 * - move dead blocks from the pending deadlist to the on-disk deadlist
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens * - release hold from dsl_dataset_dirty()
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens while ((ds = list_remove_head(&synced_datasets)) != NULL) {
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens while ((dd = txg_list_remove(&dp->dp_dirty_dirs, txg)) != NULL) {
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 if (dp->dp_mos_used_delta != 0 || dp->dp_mos_compressed_delta != 0 ||
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens dsl_dir_diduse_space(dp->dp_mos_dir, DD_USED_HEAD,
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) {
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 if (!txg_list_empty(&dp->dp_sync_tasks, txg)) {
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens * No more sync tasks should have been added while we
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens * were syncing.
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens while ((dst = txg_list_remove(&dp->dp_sync_tasks, txg)) != NULL)
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens DTRACE_PROBE2(dsl_pool_sync__done, dsl_pool_t *dp, dp, uint64_t, txg);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickdsl_pool_sync_done(dsl_pool_t *dp, uint64_t txg)
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens while (zilog = txg_list_remove(&dp->dp_dirty_zilogs, txg)) {
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens dsl_dataset_t *ds = dmu_objset_ds(zilog->zl_os);
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens ASSERT(!dmu_objset_is_dirty(zilog->zl_os, txg));
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(!dmu_objset_is_dirty(dp->dp_meta_objset, txg));
c717a56157ae0e6fca6a1e3689ae1edc385716a3maybee * TRUE if the current thread is the tx_sync_thread or if we
c717a56157ae0e6fca6a1e3689ae1edc385716a3maybee * are being called from SPA context during pool initialization.
fa9e4066f08beec538e775443c5be79dd423fcabahrensdsl_pool_adjustedsize(dsl_pool_t *dp, boolean_t netfree)
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 * 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).
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens zfs_dirty_data_max * zfs_delay_min_dirty_percent / 100;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens if (dp->dp_dirty_total > zfs_dirty_data_sync)
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrensdsl_pool_dirty_space(dsl_pool_t *dp, int64_t space, dmu_tx_t *tx)
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens dp->dp_dirty_pertxg[tx->tx_txg & TXG_MASK] += space;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrensdsl_pool_undirty_space(dsl_pool_t *dp, int64_t space, uint64_t txg)
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens if (dp->dp_dirty_pertxg[txg & TXG_MASK] < space) {
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens /* XXX writing something we didn't dirty? */
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens ASSERT3U(dp->dp_dirty_pertxg[txg & TXG_MASK], >=, space);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens dp->dp_dirty_pertxg[txg & TXG_MASK] -= space;
088f389458728c464569a5506b58070254fa4f7dahrens/* ARGSUSED */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensupgrade_clones_cb(dsl_pool_t *dp, dsl_dataset_t *hds, void *arg)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens err = dsl_dataset_hold_obj(dp, hds->ds_object, FTAG, &ds);
088f389458728c464569a5506b58070254fa4f7dahrens err = dsl_dataset_hold_obj(dp, ds->ds_phys->ds_prev_snap_obj,
088f389458728c464569a5506b58070254fa4f7dahrens if (prev->ds_phys->ds_next_snap_obj != ds->ds_object)
088f389458728c464569a5506b58070254fa4f7dahrens * The $ORIGIN can't have any data, or the accounting
088f389458728c464569a5506b58070254fa4f7dahrens * will be wrong.
088f389458728c464569a5506b58070254fa4f7dahrens /* The origin doesn't get attached to itself */
088f389458728c464569a5506b58070254fa4f7dahrens return (0);
088f389458728c464569a5506b58070254fa4f7dahrens ds->ds_phys->ds_prev_snap_txg = prev->ds_phys->ds_creation_txg;
088f389458728c464569a5506b58070254fa4f7dahrens ds->ds_dir->dd_phys->dd_origin_obj = prev->ds_object;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT3U(ds->ds_dir->dd_phys->dd_origin_obj, ==, prev->ds_object);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT3U(ds->ds_phys->ds_prev_snap_obj, ==, prev->ds_object);
088f389458728c464569a5506b58070254fa4f7dahrens prev->ds_phys->ds_next_clones_obj, ds->ds_object, tx));
088f389458728c464569a5506b58070254fa4f7dahrens return (0);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dmu_objset_find_dp(dp, dp->dp_root_dir_obj, upgrade_clones_cb,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens/* ARGSUSED */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensupgrade_dir_clones_cb(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (ds->ds_dir->dd_phys->dd_origin_obj != 0) {
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens ds->ds_dir->dd_phys->dd_origin_obj, FTAG, &origin));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (origin->ds_dir->dd_phys->dd_clones == 0) {
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dmu_buf_will_dirty(origin->ds_dir->dd_dbuf, tx);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens origin->ds_dir->dd_phys->dd_clones = zap_create(mos,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens origin->ds_dir->dd_phys->dd_clones, ds->ds_object, tx));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrensdsl_pool_upgrade_dir_clones(dsl_pool_t *dp, dmu_tx_t *tx)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens (void) dsl_dir_create_sync(dp, dp->dp_root_dir, FREE_DIR_NAME, tx);
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 obj = dmu_object_alloc(dp->dp_meta_objset, DMU_OT_BPOBJ,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens SPA_MAXBLOCKSIZE, DMU_OT_BPOBJ_HDR, sizeof (bpobj_phys_t), tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(zap_add(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens DMU_POOL_FREE_BPOBJ, sizeof (uint64_t), 1, &obj, tx));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(bpobj_open(&dp->dp_free_bpobj, dp->dp_meta_objset, obj));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dmu_objset_find_dp(dp, dp->dp_root_dir_obj,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens upgrade_dir_clones_cb, tx, DS_FIND_CHILDREN));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT(rrw_held(&dp->dp_config_rwlock, RW_WRITER));
088f389458728c464569a5506b58070254fa4f7dahrens /* create the origin dir, ds, & snap-ds */
088f389458728c464569a5506b58070254fa4f7dahrens dsobj = dsl_dataset_create_sync(dp->dp_root_dir, ORIGIN_DIR_NAME,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_snapshot_sync_impl(ds, ORIGIN_DIR_NAME, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, ds->ds_phys->ds_prev_snap_obj,
ca45db4129beff691dc46576c328149443788af2Chris Kirby * Walk through the pool-wide zap object of temporary snapshot user holds
ca45db4129beff691dc46576c328149443788af2Chris Kirby * and release them.
ca45db4129beff691dc46576c328149443788af2Chris Kirby ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland if (nvlist_lookup_nvlist(holds, za.za_name, &tags) != 0) {
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland fnvlist_add_nvlist(holds, za.za_name, tags);
ca45db4129beff691dc46576c328149443788af2Chris Kirby * Create the pool-wide zap object for storing temporary snapshot holds.
ca45db4129beff691dc46576c328149443788af2Chris Kirbydsl_pool_user_hold_create_obj(dsl_pool_t *dp, dmu_tx_t *tx)
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 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 ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS);
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 name = kmem_asprintf("%llx-%s", (u_longlong_t)dsobj, tag);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens error = zap_add(mos, zapobj, name, 8, 1, &now, tx);
ca45db4129beff691dc46576c328149443788af2Chris Kirby * Add a temporary hold for the given dataset object and tag.
ca45db4129beff691dc46576c328149443788af2Chris Kirbydsl_pool_user_hold(dsl_pool_t *dp, uint64_t dsobj, const char *tag,
15508ac067f2fb55a439711838b971c02d42316fChris Kirby return (dsl_pool_user_hold_rele_impl(dp, dsobj, tag, now, tx, B_TRUE));
ca45db4129beff691dc46576c328149443788af2Chris Kirby * Release a temporary hold for the given dataset object and tag.
ca45db4129beff691dc46576c328149443788af2Chris Kirbydsl_pool_user_release(dsl_pool_t *dp, uint64_t dsobj, const char *tag,
ca45db4129beff691dc46576c328149443788af2Chris Kirby return (dsl_pool_user_hold_rele_impl(dp, dsobj, tag, NULL,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * DSL Pool Configuration Lock
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 * 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 * 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 * 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 * 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 * User-initiated operations (e.g. ioctls, zfs_ioc_*()) are either read-only
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * or modifying operations.
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 * 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 Ahrensdsl_pool_hold(const char *name, void *tag, dsl_pool_t **dp)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_pool_config_enter(dsl_pool_t *dp, void *tag)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * We use a "reentrant" reader-writer lock, but not reentrantly.
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 * (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 ASSERT(!rrw_held(&dp->dp_config_rwlock, RW_READER));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens rrw_enter(&dp->dp_config_rwlock, RW_READER, tag);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_pool_config_exit(dsl_pool_t *dp, void *tag)