1fa9e4066Sahrens /*
2fa9e4066Sahrens * CDDL HEADER START
3fa9e4066Sahrens *
4fa9e4066Sahrens * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock * You may not use this file except in compliance with the License.
7fa9e4066Sahrens *
8fa9e4066Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens * See the License for the specific language governing permissions
11fa9e4066Sahrens * and limitations under the License.
12fa9e4066Sahrens *
13fa9e4066Sahrens * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens *
19fa9e4066Sahrens * CDDL HEADER END
20fa9e4066Sahrens */
21fa9e4066Sahrens /*
223f9d6ad7SLin Ling * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
232a104a52SAlex Reece * Copyright (c) 2011, 2014 by Delphix. All rights reserved.
24a7a845e4SSteven Hartland * Copyright (c) 2013 Steven Hartland. All rights reserved.
25bc9014e6SJustin Gibbs * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
26*c3d26abcSMatthew Ahrens * Copyright (c) 2014 Integros [integros.com]
27fa9e4066Sahrens */
28fa9e4066Sahrens
29fa9e4066Sahrens #include <sys/dsl_pool.h>
30fa9e4066Sahrens #include <sys/dsl_dataset.h>
313f9d6ad7SLin Ling #include <sys/dsl_prop.h>
32fa9e4066Sahrens #include <sys/dsl_dir.h>
331d452cf5Sahrens #include <sys/dsl_synctask.h>
343f9d6ad7SLin Ling #include <sys/dsl_scan.h>
353f9d6ad7SLin Ling #include <sys/dnode.h>
36fa9e4066Sahrens #include <sys/dmu_tx.h>
37fa9e4066Sahrens #include <sys/dmu_objset.h>
38fa9e4066Sahrens #include <sys/arc.h>
39fa9e4066Sahrens #include <sys/zap.h>
40c717a561Smaybee #include <sys/zio.h>
41fa9e4066Sahrens #include <sys/zfs_context.h>
42fa9e4066Sahrens #include <sys/fs/zfs.h>
43088f3894Sahrens #include <sys/zfs_znode.h>
44088f3894Sahrens #include <sys/spa_impl.h>
45cde58dbcSMatthew Ahrens #include <sys/dsl_deadlist.h>
46ad135b5dSChristopher Siden #include <sys/bptree.h>
47ad135b5dSChristopher Siden #include <sys/zfeature.h>
48ce636f8bSMatthew Ahrens #include <sys/zil_impl.h>
493b2aab18SMatthew Ahrens #include <sys/dsl_userhold.h>
50fa9e4066Sahrens
5169962b56SMatthew Ahrens /*
5269962b56SMatthew Ahrens * ZFS Write Throttle
5369962b56SMatthew Ahrens * ------------------
5469962b56SMatthew Ahrens *
5569962b56SMatthew Ahrens * ZFS must limit the rate of incoming writes to the rate at which it is able
5669962b56SMatthew Ahrens * to sync data modifications to the backend storage. Throttling by too much
5769962b56SMatthew Ahrens * creates an artificial limit; throttling by too little can only be sustained
5869962b56SMatthew Ahrens * for short periods and would lead to highly lumpy performance. On a per-pool
5969962b56SMatthew Ahrens * basis, ZFS tracks the amount of modified (dirty) data. As operations change
6069962b56SMatthew Ahrens * data, the amount of dirty data increases; as ZFS syncs out data, the amount
6169962b56SMatthew Ahrens * of dirty data decreases. When the amount of dirty data exceeds a
6269962b56SMatthew Ahrens * predetermined threshold further modifications are blocked until the amount
6369962b56SMatthew Ahrens * of dirty data decreases (as data is synced out).
6469962b56SMatthew Ahrens *
6569962b56SMatthew Ahrens * The limit on dirty data is tunable, and should be adjusted according to
6669962b56SMatthew Ahrens * both the IO capacity and available memory of the system. The larger the
6769962b56SMatthew Ahrens * window, the more ZFS is able to aggregate and amortize metadata (and data)
6869962b56SMatthew Ahrens * changes. However, memory is a limited resource, and allowing for more dirty
6969962b56SMatthew Ahrens * data comes at the cost of keeping other useful data in memory (for example
7069962b56SMatthew Ahrens * ZFS data cached by the ARC).
7169962b56SMatthew Ahrens *
7269962b56SMatthew Ahrens * Implementation
7369962b56SMatthew Ahrens *
7469962b56SMatthew Ahrens * As buffers are modified dsl_pool_willuse_space() increments both the per-
7569962b56SMatthew Ahrens * txg (dp_dirty_pertxg[]) and poolwide (dp_dirty_total) accounting of
7669962b56SMatthew Ahrens * dirty space used; dsl_pool_dirty_space() decrements those values as data
7769962b56SMatthew Ahrens * is synced out from dsl_pool_sync(). While only the poolwide value is
7869962b56SMatthew Ahrens * relevant, the per-txg value is useful for debugging. The tunable
7969962b56SMatthew Ahrens * zfs_dirty_data_max determines the dirty space limit. Once that value is
8069962b56SMatthew Ahrens * exceeded, new writes are halted until space frees up.
8169962b56SMatthew Ahrens *
8269962b56SMatthew Ahrens * The zfs_dirty_data_sync tunable dictates the threshold at which we
8369962b56SMatthew Ahrens * ensure that there is a txg syncing (see the comment in txg.c for a full
8469962b56SMatthew Ahrens * description of transaction group stages).
8569962b56SMatthew Ahrens *
8669962b56SMatthew Ahrens * The IO scheduler uses both the dirty space limit and current amount of
8769962b56SMatthew Ahrens * dirty data as inputs. Those values affect the number of concurrent IOs ZFS
8869962b56SMatthew Ahrens * issues. See the comment in vdev_queue.c for details of the IO scheduler.
8969962b56SMatthew Ahrens *
9069962b56SMatthew Ahrens * The delay is also calculated based on the amount of dirty data. See the
9169962b56SMatthew Ahrens * comment above dmu_tx_delay() for details.
9269962b56SMatthew Ahrens */
9305715f94SMark Maybee
9469962b56SMatthew Ahrens /*
9569962b56SMatthew Ahrens * zfs_dirty_data_max will be set to zfs_dirty_data_max_percent% of all memory,
9669962b56SMatthew Ahrens * capped at zfs_dirty_data_max_max. It can also be overridden in /etc/system.
9769962b56SMatthew Ahrens */
9869962b56SMatthew Ahrens uint64_t zfs_dirty_data_max;
9969962b56SMatthew Ahrens uint64_t zfs_dirty_data_max_max = 4ULL * 1024 * 1024 * 1024;
10069962b56SMatthew Ahrens int zfs_dirty_data_max_percent = 10;
1011ab7f2deSmaybee
10269962b56SMatthew Ahrens /*
10369962b56SMatthew Ahrens * If there is at least this much dirty data, push out a txg.
10469962b56SMatthew Ahrens */
10569962b56SMatthew Ahrens uint64_t zfs_dirty_data_sync = 64 * 1024 * 1024;
10605715f94SMark Maybee
10769962b56SMatthew Ahrens /*
10869962b56SMatthew Ahrens * Once there is this amount of dirty data, the dmu_tx_delay() will kick in
10969962b56SMatthew Ahrens * and delay each transaction.
11069962b56SMatthew Ahrens * This value should be >= zfs_vdev_async_write_active_max_dirty_percent.
11169962b56SMatthew Ahrens */
11269962b56SMatthew Ahrens int zfs_delay_min_dirty_percent = 60;
11369962b56SMatthew Ahrens
11469962b56SMatthew Ahrens /*
11569962b56SMatthew Ahrens * This controls how quickly the delay approaches infinity.
116d85a1e96SMatthew Ahrens * Larger values cause it to delay more for a given amount of dirty data.
117d85a1e96SMatthew Ahrens * Therefore larger values will cause there to be less dirty data for a
11869962b56SMatthew Ahrens * given throughput.
11969962b56SMatthew Ahrens *
12069962b56SMatthew Ahrens * For the smoothest delay, this value should be about 1 billion divided
12169962b56SMatthew Ahrens * by the maximum number of operations per second. This will smoothly
12269962b56SMatthew Ahrens * handle between 10x and 1/10th this number.
12369962b56SMatthew Ahrens *
12469962b56SMatthew Ahrens * Note: zfs_delay_scale * zfs_dirty_data_max must be < 2^64, due to the
12569962b56SMatthew Ahrens * multiply in dmu_tx_delay().
12669962b56SMatthew Ahrens */
12769962b56SMatthew Ahrens uint64_t zfs_delay_scale = 1000 * 1000 * 1000 / 2000;
12869962b56SMatthew Ahrens
12969962b56SMatthew Ahrens
1300689f76cSAdam Leventhal hrtime_t zfs_throttle_delay = MSEC2NSEC(10);
1310689f76cSAdam Leventhal hrtime_t zfs_throttle_resolution = MSEC2NSEC(10);
1320689f76cSAdam Leventhal
1333f9d6ad7SLin Ling int
dsl_pool_open_special_dir(dsl_pool_t * dp,const char * name,dsl_dir_t ** ddp)134088f3894Sahrens dsl_pool_open_special_dir(dsl_pool_t *dp, const char *name, dsl_dir_t **ddp)
135fa9e4066Sahrens {
136fa9e4066Sahrens uint64_t obj;
137fa9e4066Sahrens int err;
138fa9e4066Sahrens
139fa9e4066Sahrens err = zap_lookup(dp->dp_meta_objset,
140c1379625SJustin T. Gibbs dsl_dir_phys(dp->dp_root_dir)->dd_child_dir_zapobj,
141088f3894Sahrens name, sizeof (obj), 1, &obj);
142ea8dc4b6Seschrock if (err)
143ea8dc4b6Seschrock return (err);
144fa9e4066Sahrens
1453b2aab18SMatthew Ahrens return (dsl_dir_hold_obj(dp, obj, name, dp, ddp));
146fa9e4066Sahrens }
147fa9e4066Sahrens
148fa9e4066Sahrens static dsl_pool_t *
dsl_pool_open_impl(spa_t * spa,uint64_t txg)149fa9e4066Sahrens dsl_pool_open_impl(spa_t *spa, uint64_t txg)
150fa9e4066Sahrens {
151fa9e4066Sahrens dsl_pool_t *dp;
152fa9e4066Sahrens blkptr_t *bp = spa_get_rootblkptr(spa);
153fa9e4066Sahrens
154fa9e4066Sahrens dp = kmem_zalloc(sizeof (dsl_pool_t), KM_SLEEP);
155fa9e4066Sahrens dp->dp_spa = spa;
156fa9e4066Sahrens dp->dp_meta_rootbp = *bp;
1573b2aab18SMatthew Ahrens rrw_init(&dp->dp_config_rwlock, B_TRUE);
158fa9e4066Sahrens txg_init(dp, txg);
159fa9e4066Sahrens
160fa9e4066Sahrens txg_list_create(&dp->dp_dirty_datasets,
161fa9e4066Sahrens offsetof(dsl_dataset_t, ds_dirty_link));
162ce636f8bSMatthew Ahrens txg_list_create(&dp->dp_dirty_zilogs,
163ce636f8bSMatthew Ahrens offsetof(zilog_t, zl_dirty_link));
164fa9e4066Sahrens txg_list_create(&dp->dp_dirty_dirs,
165fa9e4066Sahrens offsetof(dsl_dir_t, dd_dirty_link));
1661d452cf5Sahrens txg_list_create(&dp->dp_sync_tasks,
1673b2aab18SMatthew Ahrens offsetof(dsl_sync_task_t, dst_node));
168fa9e4066Sahrens
1691ab7f2deSmaybee mutex_init(&dp->dp_lock, NULL, MUTEX_DEFAULT, NULL);
17069962b56SMatthew Ahrens cv_init(&dp->dp_spaceavail_cv, NULL, CV_DEFAULT, NULL);
1711ab7f2deSmaybee
1729d3574bfSNeil Perrin dp->dp_vnrele_taskq = taskq_create("zfs_vn_rele_taskq", 1, minclsyspri,
1739d3574bfSNeil Perrin 1, 4, 0);
1749d3574bfSNeil Perrin
175fa9e4066Sahrens return (dp);
176fa9e4066Sahrens }
177fa9e4066Sahrens
178ea8dc4b6Seschrock int
dsl_pool_init(spa_t * spa,uint64_t txg,dsl_pool_t ** dpp)179ad135b5dSChristopher Siden dsl_pool_init(spa_t *spa, uint64_t txg, dsl_pool_t **dpp)
180fa9e4066Sahrens {
181fa9e4066Sahrens int err;
182fa9e4066Sahrens dsl_pool_t *dp = dsl_pool_open_impl(spa, txg);
183ad135b5dSChristopher Siden
184ad135b5dSChristopher Siden err = dmu_objset_open_impl(spa, NULL, &dp->dp_meta_rootbp,
185ad135b5dSChristopher Siden &dp->dp_meta_objset);
186ad135b5dSChristopher Siden if (err != 0)
187ad135b5dSChristopher Siden dsl_pool_close(dp);
188ad135b5dSChristopher Siden else
189ad135b5dSChristopher Siden *dpp = dp;
190ad135b5dSChristopher Siden
191ad135b5dSChristopher Siden return (err);
192ad135b5dSChristopher Siden }
193ad135b5dSChristopher Siden
194ad135b5dSChristopher Siden int
dsl_pool_open(dsl_pool_t * dp)195ad135b5dSChristopher Siden dsl_pool_open(dsl_pool_t *dp)
196ad135b5dSChristopher Siden {
197ad135b5dSChristopher Siden int err;
198088f3894Sahrens dsl_dir_t *dd;
199088f3894Sahrens dsl_dataset_t *ds;
200cde58dbcSMatthew Ahrens uint64_t obj;
201fa9e4066Sahrens
2023b2aab18SMatthew Ahrens rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG);
203fa9e4066Sahrens err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
204fa9e4066Sahrens DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1,
205fa9e4066Sahrens &dp->dp_root_dir_obj);
206ea8dc4b6Seschrock if (err)
207ea8dc4b6Seschrock goto out;
208fa9e4066Sahrens
2093b2aab18SMatthew Ahrens err = dsl_dir_hold_obj(dp, dp->dp_root_dir_obj,
210ea8dc4b6Seschrock NULL, dp, &dp->dp_root_dir);
211ea8dc4b6Seschrock if (err)
212ea8dc4b6Seschrock goto out;
213ea8dc4b6Seschrock
214088f3894Sahrens err = dsl_pool_open_special_dir(dp, MOS_DIR_NAME, &dp->dp_mos_dir);
215ea8dc4b6Seschrock if (err)
216ea8dc4b6Seschrock goto out;
217ea8dc4b6Seschrock
218ad135b5dSChristopher Siden if (spa_version(dp->dp_spa) >= SPA_VERSION_ORIGIN) {
219088f3894Sahrens err = dsl_pool_open_special_dir(dp, ORIGIN_DIR_NAME, &dd);
220088f3894Sahrens if (err)
221088f3894Sahrens goto out;
222c1379625SJustin T. Gibbs err = dsl_dataset_hold_obj(dp,
223c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_head_dataset_obj, FTAG, &ds);
2248f63aa46SLin Ling if (err == 0) {
2258f63aa46SLin Ling err = dsl_dataset_hold_obj(dp,
226c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_obj, dp,
2278f63aa46SLin Ling &dp->dp_origin_snap);
228088f3894Sahrens dsl_dataset_rele(ds, FTAG);
2298f63aa46SLin Ling }
2303b2aab18SMatthew Ahrens dsl_dir_rele(dd, dp);
2318f63aa46SLin Ling if (err)
2328f63aa46SLin Ling goto out;
233088f3894Sahrens }
234088f3894Sahrens
235ad135b5dSChristopher Siden if (spa_version(dp->dp_spa) >= SPA_VERSION_DEADLISTS) {
236cde58dbcSMatthew Ahrens err = dsl_pool_open_special_dir(dp, FREE_DIR_NAME,
237cde58dbcSMatthew Ahrens &dp->dp_free_dir);
238cde58dbcSMatthew Ahrens if (err)
239cde58dbcSMatthew Ahrens goto out;
240cde58dbcSMatthew Ahrens
241cde58dbcSMatthew Ahrens err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
242cde58dbcSMatthew Ahrens DMU_POOL_FREE_BPOBJ, sizeof (uint64_t), 1, &obj);
243cde58dbcSMatthew Ahrens if (err)
244cde58dbcSMatthew Ahrens goto out;
2453b2aab18SMatthew Ahrens VERIFY0(bpobj_open(&dp->dp_free_bpobj,
246cde58dbcSMatthew Ahrens dp->dp_meta_objset, obj));
247cde58dbcSMatthew Ahrens }
248cde58dbcSMatthew Ahrens
2497fd05ac4SMatthew Ahrens /*
2507fd05ac4SMatthew Ahrens * Note: errors ignored, because the leak dir will not exist if we
2517fd05ac4SMatthew Ahrens * have not encountered a leak yet.
2527fd05ac4SMatthew Ahrens */
2537fd05ac4SMatthew Ahrens (void) dsl_pool_open_special_dir(dp, LEAK_DIR_NAME,
2547fd05ac4SMatthew Ahrens &dp->dp_leak_dir);
2557fd05ac4SMatthew Ahrens
2562acef22dSMatthew Ahrens if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_ASYNC_DESTROY)) {
257ad135b5dSChristopher Siden err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
258ad135b5dSChristopher Siden DMU_POOL_BPTREE_OBJ, sizeof (uint64_t), 1,
259ad135b5dSChristopher Siden &dp->dp_bptree_obj);
260ad135b5dSChristopher Siden if (err != 0)
261ad135b5dSChristopher Siden goto out;
262ad135b5dSChristopher Siden }
263ad135b5dSChristopher Siden
2642acef22dSMatthew Ahrens if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_EMPTY_BPOBJ)) {
265f1745736SMatthew Ahrens err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
266f1745736SMatthew Ahrens DMU_POOL_EMPTY_BPOBJ, sizeof (uint64_t), 1,
267f1745736SMatthew Ahrens &dp->dp_empty_bpobj);
268f1745736SMatthew Ahrens if (err != 0)
269f1745736SMatthew Ahrens goto out;
270f1745736SMatthew Ahrens }
271f1745736SMatthew Ahrens
272ca45db41SChris Kirby err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
273ca45db41SChris Kirby DMU_POOL_TMP_USERREFS, sizeof (uint64_t), 1,
274ca45db41SChris Kirby &dp->dp_tmp_userrefs_obj);
275ca45db41SChris Kirby if (err == ENOENT)
276ca45db41SChris Kirby err = 0;
277ca45db41SChris Kirby if (err)
278ca45db41SChris Kirby goto out;
279ca45db41SChris Kirby
280ad135b5dSChristopher Siden err = dsl_scan_init(dp, dp->dp_tx.tx_open_txg);
281088f3894Sahrens
282ea8dc4b6Seschrock out:
2833b2aab18SMatthew Ahrens rrw_exit(&dp->dp_config_rwlock, FTAG);
284ea8dc4b6Seschrock return (err);
285fa9e4066Sahrens }
286fa9e4066Sahrens
287fa9e4066Sahrens void
dsl_pool_close(dsl_pool_t * dp)288fa9e4066Sahrens dsl_pool_close(dsl_pool_t *dp)
289fa9e4066Sahrens {
290088f3894Sahrens /*
29169962b56SMatthew Ahrens * Drop our references from dsl_pool_open().
29269962b56SMatthew Ahrens *
293088f3894Sahrens * Since we held the origin_snap from "syncing" context (which
294088f3894Sahrens * includes pool-opening context), it actually only got a "ref"
295088f3894Sahrens * and not a hold, so just drop that here.
296088f3894Sahrens */
297088f3894Sahrens if (dp->dp_origin_snap)
2983b2aab18SMatthew Ahrens dsl_dataset_rele(dp->dp_origin_snap, dp);
299ea8dc4b6Seschrock if (dp->dp_mos_dir)
3003b2aab18SMatthew Ahrens dsl_dir_rele(dp->dp_mos_dir, dp);
301cde58dbcSMatthew Ahrens if (dp->dp_free_dir)
3023b2aab18SMatthew Ahrens dsl_dir_rele(dp->dp_free_dir, dp);
3037fd05ac4SMatthew Ahrens if (dp->dp_leak_dir)
3047fd05ac4SMatthew Ahrens dsl_dir_rele(dp->dp_leak_dir, dp);
305ea8dc4b6Seschrock if (dp->dp_root_dir)
3063b2aab18SMatthew Ahrens dsl_dir_rele(dp->dp_root_dir, dp);
307fa9e4066Sahrens
308cde58dbcSMatthew Ahrens bpobj_close(&dp->dp_free_bpobj);
309cde58dbcSMatthew Ahrens
310fa9e4066Sahrens /* undo the dmu_objset_open_impl(mos) from dsl_pool_open() */
311ea8dc4b6Seschrock if (dp->dp_meta_objset)
312503ad85cSMatthew Ahrens dmu_objset_evict(dp->dp_meta_objset);
313fa9e4066Sahrens
314fa9e4066Sahrens txg_list_destroy(&dp->dp_dirty_datasets);
315ce636f8bSMatthew Ahrens txg_list_destroy(&dp->dp_dirty_zilogs);
31654a91118SChris Kirby txg_list_destroy(&dp->dp_sync_tasks);
317fa9e4066Sahrens txg_list_destroy(&dp->dp_dirty_dirs);
318fa9e4066Sahrens
319244781f1SPrakash Surya /*
320244781f1SPrakash Surya * We can't set retry to TRUE since we're explicitly specifying
321244781f1SPrakash Surya * a spa to flush. This is good enough; any missed buffers for
322244781f1SPrakash Surya * this spa won't cause trouble, and they'll eventually fall
323244781f1SPrakash Surya * out of the ARC just like any other unused buffer.
324244781f1SPrakash Surya */
325244781f1SPrakash Surya arc_flush(dp->dp_spa, FALSE);
326244781f1SPrakash Surya
327fa9e4066Sahrens txg_fini(dp);
3283f9d6ad7SLin Ling dsl_scan_fini(dp);
329bc9014e6SJustin Gibbs dmu_buf_user_evict_wait();
330bc9014e6SJustin Gibbs
3313b2aab18SMatthew Ahrens rrw_destroy(&dp->dp_config_rwlock);
3321ab7f2deSmaybee mutex_destroy(&dp->dp_lock);
3339d3574bfSNeil Perrin taskq_destroy(dp->dp_vnrele_taskq);
33488b7b0f2SMatthew Ahrens if (dp->dp_blkstats)
33588b7b0f2SMatthew Ahrens kmem_free(dp->dp_blkstats, sizeof (zfs_all_blkstats_t));
336fa9e4066Sahrens kmem_free(dp, sizeof (dsl_pool_t));
337fa9e4066Sahrens }
338fa9e4066Sahrens
339fa9e4066Sahrens dsl_pool_t *
dsl_pool_create(spa_t * spa,nvlist_t * zplprops,uint64_t txg)3400a48a24eStimh dsl_pool_create(spa_t *spa, nvlist_t *zplprops, uint64_t txg)
341fa9e4066Sahrens {
342fa9e4066Sahrens int err;
343fa9e4066Sahrens dsl_pool_t *dp = dsl_pool_open_impl(spa, txg);
344fa9e4066Sahrens dmu_tx_t *tx = dmu_tx_create_assigned(dp, txg);
345503ad85cSMatthew Ahrens objset_t *os;
346088f3894Sahrens dsl_dataset_t *ds;
347cde58dbcSMatthew Ahrens uint64_t obj;
348088f3894Sahrens
3493b2aab18SMatthew Ahrens rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG);
3503b2aab18SMatthew Ahrens
351088f3894Sahrens /* create and open the MOS (meta-objset) */
352503ad85cSMatthew Ahrens dp->dp_meta_objset = dmu_objset_create_impl(spa,
353503ad85cSMatthew Ahrens NULL, &dp->dp_meta_rootbp, DMU_OST_META, tx);
354fa9e4066Sahrens
355fa9e4066Sahrens /* create the pool directory */
356fa9e4066Sahrens err = zap_create_claim(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
357fa9e4066Sahrens DMU_OT_OBJECT_DIRECTORY, DMU_OT_NONE, 0, tx);
358fb09f5aaSMadhav Suresh ASSERT0(err);
359fa9e4066Sahrens
3603f9d6ad7SLin Ling /* Initialize scan structures */
3613b2aab18SMatthew Ahrens VERIFY0(dsl_scan_init(dp, txg));
3623f9d6ad7SLin Ling
363fa9e4066Sahrens /* create and open the root dir */
364088f3894Sahrens dp->dp_root_dir_obj = dsl_dir_create_sync(dp, NULL, NULL, tx);
3653b2aab18SMatthew Ahrens VERIFY0(dsl_dir_hold_obj(dp, dp->dp_root_dir_obj,
366ea8dc4b6Seschrock NULL, dp, &dp->dp_root_dir));
367fa9e4066Sahrens
368fa9e4066Sahrens /* create and open the meta-objset dir */
369088f3894Sahrens (void) dsl_dir_create_sync(dp, dp->dp_root_dir, MOS_DIR_NAME, tx);
3703b2aab18SMatthew Ahrens VERIFY0(dsl_pool_open_special_dir(dp,
371088f3894Sahrens MOS_DIR_NAME, &dp->dp_mos_dir));
372088f3894Sahrens
373cde58dbcSMatthew Ahrens if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
374cde58dbcSMatthew Ahrens /* create and open the free dir */
375cde58dbcSMatthew Ahrens (void) dsl_dir_create_sync(dp, dp->dp_root_dir,
376cde58dbcSMatthew Ahrens FREE_DIR_NAME, tx);
3773b2aab18SMatthew Ahrens VERIFY0(dsl_pool_open_special_dir(dp,
378cde58dbcSMatthew Ahrens FREE_DIR_NAME, &dp->dp_free_dir));
379cde58dbcSMatthew Ahrens
380cde58dbcSMatthew Ahrens /* create and open the free_bplist */
381b5152584SMatthew Ahrens obj = bpobj_alloc(dp->dp_meta_objset, SPA_OLD_MAXBLOCKSIZE, tx);
382cde58dbcSMatthew Ahrens VERIFY(zap_add(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
383cde58dbcSMatthew Ahrens DMU_POOL_FREE_BPOBJ, sizeof (uint64_t), 1, &obj, tx) == 0);
3843b2aab18SMatthew Ahrens VERIFY0(bpobj_open(&dp->dp_free_bpobj,
385cde58dbcSMatthew Ahrens dp->dp_meta_objset, obj));
386cde58dbcSMatthew Ahrens }
387cde58dbcSMatthew Ahrens
388088f3894Sahrens if (spa_version(spa) >= SPA_VERSION_DSL_SCRUB)
389088f3894Sahrens dsl_pool_create_origin(dp, tx);
390088f3894Sahrens
391088f3894Sahrens /* create the root dataset */
392cde58dbcSMatthew Ahrens obj = dsl_dataset_create_sync_dd(dp->dp_root_dir, NULL, 0, tx);
393088f3894Sahrens
394088f3894Sahrens /* create the root objset */
3953b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, obj, FTAG, &ds));
396503ad85cSMatthew Ahrens os = dmu_objset_create_impl(dp->dp_spa, ds,
397088f3894Sahrens dsl_dataset_get_blkptr(ds), DMU_OST_ZFS, tx);
398088f3894Sahrens #ifdef _KERNEL
399503ad85cSMatthew Ahrens zfs_create_fs(os, kcred, zplprops, tx);
400088f3894Sahrens #endif
401088f3894Sahrens dsl_dataset_rele(ds, FTAG);
402fa9e4066Sahrens
403fa9e4066Sahrens dmu_tx_commit(tx);
404fa9e4066Sahrens
4053b2aab18SMatthew Ahrens rrw_exit(&dp->dp_config_rwlock, FTAG);
4063b2aab18SMatthew Ahrens
407fa9e4066Sahrens return (dp);
408fa9e4066Sahrens }
409fa9e4066Sahrens
410ce636f8bSMatthew Ahrens /*
411ce636f8bSMatthew Ahrens * Account for the meta-objset space in its placeholder dsl_dir.
412ce636f8bSMatthew Ahrens */
413ce636f8bSMatthew Ahrens void
dsl_pool_mos_diduse_space(dsl_pool_t * dp,int64_t used,int64_t comp,int64_t uncomp)414ce636f8bSMatthew Ahrens dsl_pool_mos_diduse_space(dsl_pool_t *dp,
415ce636f8bSMatthew Ahrens int64_t used, int64_t comp, int64_t uncomp)
416ce636f8bSMatthew Ahrens {
417ce636f8bSMatthew Ahrens ASSERT3U(comp, ==, uncomp); /* it's all metadata */
418ce636f8bSMatthew Ahrens mutex_enter(&dp->dp_lock);
419ce636f8bSMatthew Ahrens dp->dp_mos_used_delta += used;
420ce636f8bSMatthew Ahrens dp->dp_mos_compressed_delta += comp;
421ce636f8bSMatthew Ahrens dp->dp_mos_uncompressed_delta += uncomp;
422ce636f8bSMatthew Ahrens mutex_exit(&dp->dp_lock);
423ce636f8bSMatthew Ahrens }
424ce636f8bSMatthew Ahrens
425cde58dbcSMatthew Ahrens static int
deadlist_enqueue_cb(void * arg,const blkptr_t * bp,dmu_tx_t * tx)426cde58dbcSMatthew Ahrens deadlist_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
427cde58dbcSMatthew Ahrens {
428cde58dbcSMatthew Ahrens dsl_deadlist_t *dl = arg;
429cde58dbcSMatthew Ahrens dsl_deadlist_insert(dl, bp, tx);
430cde58dbcSMatthew Ahrens return (0);
431cde58dbcSMatthew Ahrens }
432cde58dbcSMatthew Ahrens
43369962b56SMatthew Ahrens static void
dsl_pool_sync_mos(dsl_pool_t * dp,dmu_tx_t * tx)43469962b56SMatthew Ahrens dsl_pool_sync_mos(dsl_pool_t *dp, dmu_tx_t *tx)
43569962b56SMatthew Ahrens {
43669962b56SMatthew Ahrens zio_t *zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
43769962b56SMatthew Ahrens dmu_objset_sync(dp->dp_meta_objset, zio, tx);
43869962b56SMatthew Ahrens VERIFY0(zio_wait(zio));
43969962b56SMatthew Ahrens dprintf_bp(&dp->dp_meta_rootbp, "meta objset rootbp is %s", "");
44069962b56SMatthew Ahrens spa_set_rootblkptr(dp->dp_spa, &dp->dp_meta_rootbp);
44169962b56SMatthew Ahrens }
44269962b56SMatthew Ahrens
44369962b56SMatthew Ahrens static void
dsl_pool_dirty_delta(dsl_pool_t * dp,int64_t delta)44469962b56SMatthew Ahrens dsl_pool_dirty_delta(dsl_pool_t *dp, int64_t delta)
44569962b56SMatthew Ahrens {
44669962b56SMatthew Ahrens ASSERT(MUTEX_HELD(&dp->dp_lock));
44769962b56SMatthew Ahrens
44869962b56SMatthew Ahrens if (delta < 0)
44969962b56SMatthew Ahrens ASSERT3U(-delta, <=, dp->dp_dirty_total);
45069962b56SMatthew Ahrens
45169962b56SMatthew Ahrens dp->dp_dirty_total += delta;
45269962b56SMatthew Ahrens
45369962b56SMatthew Ahrens /*
45469962b56SMatthew Ahrens * Note: we signal even when increasing dp_dirty_total.
45569962b56SMatthew Ahrens * This ensures forward progress -- each thread wakes the next waiter.
45669962b56SMatthew Ahrens */
45769962b56SMatthew Ahrens if (dp->dp_dirty_total <= zfs_dirty_data_max)
45869962b56SMatthew Ahrens cv_signal(&dp->dp_spaceavail_cv);
45969962b56SMatthew Ahrens }
46069962b56SMatthew Ahrens
461fa9e4066Sahrens void
dsl_pool_sync(dsl_pool_t * dp,uint64_t txg)462fa9e4066Sahrens dsl_pool_sync(dsl_pool_t *dp, uint64_t txg)
463fa9e4066Sahrens {
464c717a561Smaybee zio_t *zio;
465fa9e4066Sahrens dmu_tx_t *tx;
466fa9e4066Sahrens dsl_dir_t *dd;
467fa9e4066Sahrens dsl_dataset_t *ds;
468503ad85cSMatthew Ahrens objset_t *mos = dp->dp_meta_objset;
469ce636f8bSMatthew Ahrens list_t synced_datasets;
470ce636f8bSMatthew Ahrens
471ce636f8bSMatthew Ahrens list_create(&synced_datasets, sizeof (dsl_dataset_t),
472ce636f8bSMatthew Ahrens offsetof(dsl_dataset_t, ds_synced_link));
473fa9e4066Sahrens
474c717a561Smaybee tx = dmu_tx_create_assigned(dp, txg);
475c717a561Smaybee
47669962b56SMatthew Ahrens /*
47769962b56SMatthew Ahrens * Write out all dirty blocks of dirty datasets.
47869962b56SMatthew Ahrens */
479c717a561Smaybee zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
48069962b56SMatthew Ahrens while ((ds = txg_list_remove(&dp->dp_dirty_datasets, txg)) != NULL) {
48114843421SMatthew Ahrens /*
48214843421SMatthew Ahrens * We must not sync any non-MOS datasets twice, because
48314843421SMatthew Ahrens * we may have taken a snapshot of them. However, we
48414843421SMatthew Ahrens * may sync newly-created datasets on pass 2.
48514843421SMatthew Ahrens */
48614843421SMatthew Ahrens ASSERT(!list_link_active(&ds->ds_synced_link));
487ce636f8bSMatthew Ahrens list_insert_tail(&synced_datasets, ds);
488c717a561Smaybee dsl_dataset_sync(ds, zio, tx);
489fa9e4066Sahrens }
49069962b56SMatthew Ahrens VERIFY0(zio_wait(zio));
49114843421SMatthew Ahrens
49269962b56SMatthew Ahrens /*
49369962b56SMatthew Ahrens * We have written all of the accounted dirty data, so our
49469962b56SMatthew Ahrens * dp_space_towrite should now be zero. However, some seldom-used
49569962b56SMatthew Ahrens * code paths do not adhere to this (e.g. dbuf_undirty(), also
49669962b56SMatthew Ahrens * rounding error in dbuf_write_physdone).
49769962b56SMatthew Ahrens * Shore up the accounting of any dirtied space now.
49869962b56SMatthew Ahrens */
49969962b56SMatthew Ahrens dsl_pool_undirty_space(dp, dp->dp_dirty_pertxg[txg & TXG_MASK], txg);
500c717a561Smaybee
501ce636f8bSMatthew Ahrens /*
502ce636f8bSMatthew Ahrens * After the data blocks have been written (ensured by the zio_wait()
503ce636f8bSMatthew Ahrens * above), update the user/group space accounting.
504ce636f8bSMatthew Ahrens */
50569962b56SMatthew Ahrens for (ds = list_head(&synced_datasets); ds != NULL;
50669962b56SMatthew Ahrens ds = list_next(&synced_datasets, ds)) {
5070a586ceaSMark Shellenbaum dmu_objset_do_userquota_updates(ds->ds_objset, tx);
50869962b56SMatthew Ahrens }
50914843421SMatthew Ahrens
51014843421SMatthew Ahrens /*
51114843421SMatthew Ahrens * Sync the datasets again to push out the changes due to
5123f9d6ad7SLin Ling * userspace updates. This must be done before we process the
513ce636f8bSMatthew Ahrens * sync tasks, so that any snapshots will have the correct
514ce636f8bSMatthew Ahrens * user accounting information (and we won't get confused
515ce636f8bSMatthew Ahrens * about which blocks are part of the snapshot).
51614843421SMatthew Ahrens */
51714843421SMatthew Ahrens zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
51869962b56SMatthew Ahrens while ((ds = txg_list_remove(&dp->dp_dirty_datasets, txg)) != NULL) {
51914843421SMatthew Ahrens ASSERT(list_link_active(&ds->ds_synced_link));
52014843421SMatthew Ahrens dmu_buf_rele(ds->ds_dbuf, ds);
52114843421SMatthew Ahrens dsl_dataset_sync(ds, zio, tx);
52214843421SMatthew Ahrens }
52369962b56SMatthew Ahrens VERIFY0(zio_wait(zio));
52414843421SMatthew Ahrens
525b24ab676SJeff Bonwick /*
526ce636f8bSMatthew Ahrens * Now that the datasets have been completely synced, we can
527ce636f8bSMatthew Ahrens * clean up our in-memory structures accumulated while syncing:
528ce636f8bSMatthew Ahrens *
529ce636f8bSMatthew Ahrens * - move dead blocks from the pending deadlist to the on-disk deadlist
530ce636f8bSMatthew Ahrens * - release hold from dsl_dataset_dirty()
531b24ab676SJeff Bonwick */
53269962b56SMatthew Ahrens while ((ds = list_remove_head(&synced_datasets)) != NULL) {
533ce636f8bSMatthew Ahrens objset_t *os = ds->ds_objset;
534cde58dbcSMatthew Ahrens bplist_iterate(&ds->ds_pending_deadlist,
535cde58dbcSMatthew Ahrens deadlist_enqueue_cb, &ds->ds_deadlist, tx);
536ce636f8bSMatthew Ahrens ASSERT(!dmu_objset_is_dirty(os, txg));
537ce636f8bSMatthew Ahrens dmu_buf_rele(ds->ds_dbuf, ds);
538cde58dbcSMatthew Ahrens }
53969962b56SMatthew Ahrens while ((dd = txg_list_remove(&dp->dp_dirty_dirs, txg)) != NULL) {
540fa9e4066Sahrens dsl_dir_sync(dd, tx);
54169962b56SMatthew Ahrens }
542fa9e4066Sahrens
543ce636f8bSMatthew Ahrens /*
544ce636f8bSMatthew Ahrens * The MOS's space is accounted for in the pool/$MOS
545ce636f8bSMatthew Ahrens * (dp_mos_dir). We can't modify the mos while we're syncing
546ce636f8bSMatthew Ahrens * it, so we remember the deltas and apply them here.
547ce636f8bSMatthew Ahrens */
548ce636f8bSMatthew Ahrens if (dp->dp_mos_used_delta != 0 || dp->dp_mos_compressed_delta != 0 ||
549ce636f8bSMatthew Ahrens dp->dp_mos_uncompressed_delta != 0) {
550ce636f8bSMatthew Ahrens dsl_dir_diduse_space(dp->dp_mos_dir, DD_USED_HEAD,
551ce636f8bSMatthew Ahrens dp->dp_mos_used_delta,
552ce636f8bSMatthew Ahrens dp->dp_mos_compressed_delta,
553ce636f8bSMatthew Ahrens dp->dp_mos_uncompressed_delta, tx);
554ce636f8bSMatthew Ahrens dp->dp_mos_used_delta = 0;
555ce636f8bSMatthew Ahrens dp->dp_mos_compressed_delta = 0;
556ce636f8bSMatthew Ahrens dp->dp_mos_uncompressed_delta = 0;
557ce636f8bSMatthew Ahrens }
558ce636f8bSMatthew Ahrens
559503ad85cSMatthew Ahrens if (list_head(&mos->os_dirty_dnodes[txg & TXG_MASK]) != NULL ||
560503ad85cSMatthew Ahrens list_head(&mos->os_free_dnodes[txg & TXG_MASK]) != NULL) {
56169962b56SMatthew Ahrens dsl_pool_sync_mos(dp, tx);
562fa9e4066Sahrens }
563fa9e4066Sahrens
564ce636f8bSMatthew Ahrens /*
565ce636f8bSMatthew Ahrens * If we modify a dataset in the same txg that we want to destroy it,
566ce636f8bSMatthew Ahrens * its dsl_dir's dd_dbuf will be dirty, and thus have a hold on it.
567ce636f8bSMatthew Ahrens * dsl_dir_destroy_check() will fail if there are unexpected holds.
568ce636f8bSMatthew Ahrens * Therefore, we want to sync the MOS (thus syncing the dd_dbuf
569ce636f8bSMatthew Ahrens * and clearing the hold on it) before we process the sync_tasks.
570ce636f8bSMatthew Ahrens * The MOS data dirtied by the sync_tasks will be synced on the next
571ce636f8bSMatthew Ahrens * pass.
572ce636f8bSMatthew Ahrens */
573ce636f8bSMatthew Ahrens if (!txg_list_empty(&dp->dp_sync_tasks, txg)) {
5743b2aab18SMatthew Ahrens dsl_sync_task_t *dst;
575ce636f8bSMatthew Ahrens /*
576ce636f8bSMatthew Ahrens * No more sync tasks should have been added while we
577ce636f8bSMatthew Ahrens * were syncing.
578ce636f8bSMatthew Ahrens */
57969962b56SMatthew Ahrens ASSERT3U(spa_sync_pass(dp->dp_spa), ==, 1);
58069962b56SMatthew Ahrens while ((dst = txg_list_remove(&dp->dp_sync_tasks, txg)) != NULL)
5813b2aab18SMatthew Ahrens dsl_sync_task_sync(dst, tx);
582ce636f8bSMatthew Ahrens }
583ce636f8bSMatthew Ahrens
584fa9e4066Sahrens dmu_tx_commit(tx);
58505715f94SMark Maybee
58669962b56SMatthew Ahrens DTRACE_PROBE2(dsl_pool_sync__done, dsl_pool_t *dp, dp, uint64_t, txg);
587fa9e4066Sahrens }
588fa9e4066Sahrens
589fa9e4066Sahrens void
dsl_pool_sync_done(dsl_pool_t * dp,uint64_t txg)590b24ab676SJeff Bonwick dsl_pool_sync_done(dsl_pool_t *dp, uint64_t txg)
591fa9e4066Sahrens {
592ce636f8bSMatthew Ahrens zilog_t *zilog;
593fa9e4066Sahrens
594ce636f8bSMatthew Ahrens while (zilog = txg_list_remove(&dp->dp_dirty_zilogs, txg)) {
59569962b56SMatthew Ahrens dsl_dataset_t *ds = dmu_objset_ds(zilog->zl_os);
596ce636f8bSMatthew Ahrens zil_clean(zilog, txg);
597ce636f8bSMatthew Ahrens ASSERT(!dmu_objset_is_dirty(zilog->zl_os, txg));
598ce636f8bSMatthew Ahrens dmu_buf_rele(ds->ds_dbuf, zilog);
599fa9e4066Sahrens }
600b24ab676SJeff Bonwick ASSERT(!dmu_objset_is_dirty(dp->dp_meta_objset, txg));
601fa9e4066Sahrens }
602fa9e4066Sahrens
603c717a561Smaybee /*
604c717a561Smaybee * TRUE if the current thread is the tx_sync_thread or if we
605c717a561Smaybee * are being called from SPA context during pool initialization.
606c717a561Smaybee */
607fa9e4066Sahrens int
dsl_pool_sync_context(dsl_pool_t * dp)608fa9e4066Sahrens dsl_pool_sync_context(dsl_pool_t *dp)
609fa9e4066Sahrens {
610fa9e4066Sahrens return (curthread == dp->dp_tx.tx_sync_thread ||
611ad135b5dSChristopher Siden spa_is_initializing(dp->dp_spa));
612fa9e4066Sahrens }
613fa9e4066Sahrens
614fa9e4066Sahrens uint64_t
dsl_pool_adjustedsize(dsl_pool_t * dp,boolean_t netfree)615fa9e4066Sahrens dsl_pool_adjustedsize(dsl_pool_t *dp, boolean_t netfree)
616fa9e4066Sahrens {
617fa9e4066Sahrens uint64_t space, resv;
618fa9e4066Sahrens
619fa9e4066Sahrens /*
620fa9e4066Sahrens * If we're trying to assess whether it's OK to do a free,
621fa9e4066Sahrens * cut the reservation in half to allow forward progress
622fa9e4066Sahrens * (e.g. make it possible to rm(1) files from a full pool).
623fa9e4066Sahrens */
624485bbbf5SGeorge Wilson space = spa_get_dspace(dp->dp_spa);
625c39f2c8cSChristopher Siden resv = spa_get_slop_space(dp->dp_spa);
626fa9e4066Sahrens if (netfree)
627fa9e4066Sahrens resv >>= 1;
628fa9e4066Sahrens
629fa9e4066Sahrens return (space - resv);
630fa9e4066Sahrens }
6311ab7f2deSmaybee
63269962b56SMatthew Ahrens boolean_t
dsl_pool_need_dirty_delay(dsl_pool_t * dp)63369962b56SMatthew Ahrens dsl_pool_need_dirty_delay(dsl_pool_t *dp)
6341ab7f2deSmaybee {
63569962b56SMatthew Ahrens uint64_t delay_min_bytes =
63669962b56SMatthew Ahrens zfs_dirty_data_max * zfs_delay_min_dirty_percent / 100;
63769962b56SMatthew Ahrens boolean_t rv;
6381ab7f2deSmaybee
63969962b56SMatthew Ahrens mutex_enter(&dp->dp_lock);
64069962b56SMatthew Ahrens if (dp->dp_dirty_total > zfs_dirty_data_sync)
64169962b56SMatthew Ahrens txg_kick(dp);
64269962b56SMatthew Ahrens rv = (dp->dp_dirty_total > delay_min_bytes);
64369962b56SMatthew Ahrens mutex_exit(&dp->dp_lock);
64469962b56SMatthew Ahrens return (rv);
6451ab7f2deSmaybee }
6461ab7f2deSmaybee
6471ab7f2deSmaybee void
dsl_pool_dirty_space(dsl_pool_t * dp,int64_t space,dmu_tx_t * tx)64869962b56SMatthew Ahrens dsl_pool_dirty_space(dsl_pool_t *dp, int64_t space, dmu_tx_t *tx)
6491ab7f2deSmaybee {
6501ab7f2deSmaybee if (space > 0) {
6511ab7f2deSmaybee mutex_enter(&dp->dp_lock);
65269962b56SMatthew Ahrens dp->dp_dirty_pertxg[tx->tx_txg & TXG_MASK] += space;
65369962b56SMatthew Ahrens dsl_pool_dirty_delta(dp, space);
6541ab7f2deSmaybee mutex_exit(&dp->dp_lock);
6551ab7f2deSmaybee }
6561ab7f2deSmaybee }
657088f3894Sahrens
65869962b56SMatthew Ahrens void
dsl_pool_undirty_space(dsl_pool_t * dp,int64_t space,uint64_t txg)65969962b56SMatthew Ahrens dsl_pool_undirty_space(dsl_pool_t *dp, int64_t space, uint64_t txg)
66069962b56SMatthew Ahrens {
66169962b56SMatthew Ahrens ASSERT3S(space, >=, 0);
66269962b56SMatthew Ahrens if (space == 0)
66369962b56SMatthew Ahrens return;
66469962b56SMatthew Ahrens mutex_enter(&dp->dp_lock);
66569962b56SMatthew Ahrens if (dp->dp_dirty_pertxg[txg & TXG_MASK] < space) {
66669962b56SMatthew Ahrens /* XXX writing something we didn't dirty? */
66769962b56SMatthew Ahrens space = dp->dp_dirty_pertxg[txg & TXG_MASK];
66869962b56SMatthew Ahrens }
66969962b56SMatthew Ahrens ASSERT3U(dp->dp_dirty_pertxg[txg & TXG_MASK], >=, space);
67069962b56SMatthew Ahrens dp->dp_dirty_pertxg[txg & TXG_MASK] -= space;
67169962b56SMatthew Ahrens ASSERT3U(dp->dp_dirty_total, >=, space);
67269962b56SMatthew Ahrens dsl_pool_dirty_delta(dp, -space);
67369962b56SMatthew Ahrens mutex_exit(&dp->dp_lock);
67469962b56SMatthew Ahrens }
67569962b56SMatthew Ahrens
676088f3894Sahrens /* ARGSUSED */
677088f3894Sahrens static int
upgrade_clones_cb(dsl_pool_t * dp,dsl_dataset_t * hds,void * arg)6783b2aab18SMatthew Ahrens upgrade_clones_cb(dsl_pool_t *dp, dsl_dataset_t *hds, void *arg)
679088f3894Sahrens {
680088f3894Sahrens dmu_tx_t *tx = arg;
681088f3894Sahrens dsl_dataset_t *ds, *prev = NULL;
682088f3894Sahrens int err;
683088f3894Sahrens
6843b2aab18SMatthew Ahrens err = dsl_dataset_hold_obj(dp, hds->ds_object, FTAG, &ds);
685088f3894Sahrens if (err)
686088f3894Sahrens return (err);
687088f3894Sahrens
688c1379625SJustin T. Gibbs while (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) {
689c1379625SJustin T. Gibbs err = dsl_dataset_hold_obj(dp,
690c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_obj, FTAG, &prev);
691088f3894Sahrens if (err) {
692088f3894Sahrens dsl_dataset_rele(ds, FTAG);
693088f3894Sahrens return (err);
694088f3894Sahrens }
695088f3894Sahrens
696c1379625SJustin T. Gibbs if (dsl_dataset_phys(prev)->ds_next_snap_obj != ds->ds_object)
697088f3894Sahrens break;
698088f3894Sahrens dsl_dataset_rele(ds, FTAG);
699088f3894Sahrens ds = prev;
700088f3894Sahrens prev = NULL;
701088f3894Sahrens }
702088f3894Sahrens
703088f3894Sahrens if (prev == NULL) {
704088f3894Sahrens prev = dp->dp_origin_snap;
705088f3894Sahrens
706088f3894Sahrens /*
707088f3894Sahrens * The $ORIGIN can't have any data, or the accounting
708088f3894Sahrens * will be wrong.
709088f3894Sahrens */
710c1379625SJustin T. Gibbs ASSERT0(dsl_dataset_phys(prev)->ds_bp.blk_birth);
711088f3894Sahrens
712088f3894Sahrens /* The origin doesn't get attached to itself */
713088f3894Sahrens if (ds->ds_object == prev->ds_object) {
714088f3894Sahrens dsl_dataset_rele(ds, FTAG);
715088f3894Sahrens return (0);
716088f3894Sahrens }
717088f3894Sahrens
718088f3894Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx);
719c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_obj = prev->ds_object;
720c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_txg =
721c1379625SJustin T. Gibbs dsl_dataset_phys(prev)->ds_creation_txg;
722088f3894Sahrens
723088f3894Sahrens dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
724c1379625SJustin T. Gibbs dsl_dir_phys(ds->ds_dir)->dd_origin_obj = prev->ds_object;
725088f3894Sahrens
726088f3894Sahrens dmu_buf_will_dirty(prev->ds_dbuf, tx);
727c1379625SJustin T. Gibbs dsl_dataset_phys(prev)->ds_num_children++;
728088f3894Sahrens
729c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_next_snap_obj == 0) {
730088f3894Sahrens ASSERT(ds->ds_prev == NULL);
7313b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp,
732c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_obj,
733c1379625SJustin T. Gibbs ds, &ds->ds_prev));
734088f3894Sahrens }
735088f3894Sahrens }
736088f3894Sahrens
737c1379625SJustin T. Gibbs ASSERT3U(dsl_dir_phys(ds->ds_dir)->dd_origin_obj, ==, prev->ds_object);
738c1379625SJustin T. Gibbs ASSERT3U(dsl_dataset_phys(ds)->ds_prev_snap_obj, ==, prev->ds_object);
739088f3894Sahrens
740c1379625SJustin T. Gibbs if (dsl_dataset_phys(prev)->ds_next_clones_obj == 0) {
741c33e334fSMatthew Ahrens dmu_buf_will_dirty(prev->ds_dbuf, tx);
742c1379625SJustin T. Gibbs dsl_dataset_phys(prev)->ds_next_clones_obj =
743088f3894Sahrens zap_create(dp->dp_meta_objset,
744088f3894Sahrens DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx);
745088f3894Sahrens }
7463b2aab18SMatthew Ahrens VERIFY0(zap_add_int(dp->dp_meta_objset,
747c1379625SJustin T. Gibbs dsl_dataset_phys(prev)->ds_next_clones_obj, ds->ds_object, tx));
748088f3894Sahrens
749088f3894Sahrens dsl_dataset_rele(ds, FTAG);
750088f3894Sahrens if (prev != dp->dp_origin_snap)
751088f3894Sahrens dsl_dataset_rele(prev, FTAG);
752088f3894Sahrens return (0);
753088f3894Sahrens }
754088f3894Sahrens
755088f3894Sahrens void
dsl_pool_upgrade_clones(dsl_pool_t * dp,dmu_tx_t * tx)756088f3894Sahrens dsl_pool_upgrade_clones(dsl_pool_t *dp, dmu_tx_t *tx)
757088f3894Sahrens {
758088f3894Sahrens ASSERT(dmu_tx_is_syncing(tx));
759088f3894Sahrens ASSERT(dp->dp_origin_snap != NULL);
760088f3894Sahrens
7613b2aab18SMatthew Ahrens VERIFY0(dmu_objset_find_dp(dp, dp->dp_root_dir_obj, upgrade_clones_cb,
76212380e1eSArne Jansen tx, DS_FIND_CHILDREN | DS_FIND_SERIALIZE));
763088f3894Sahrens }
764088f3894Sahrens
765cde58dbcSMatthew Ahrens /* ARGSUSED */
766cde58dbcSMatthew Ahrens static int
upgrade_dir_clones_cb(dsl_pool_t * dp,dsl_dataset_t * ds,void * arg)7673b2aab18SMatthew Ahrens upgrade_dir_clones_cb(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg)
768cde58dbcSMatthew Ahrens {
769cde58dbcSMatthew Ahrens dmu_tx_t *tx = arg;
770cde58dbcSMatthew Ahrens objset_t *mos = dp->dp_meta_objset;
771cde58dbcSMatthew Ahrens
772c1379625SJustin T. Gibbs if (dsl_dir_phys(ds->ds_dir)->dd_origin_obj != 0) {
773cde58dbcSMatthew Ahrens dsl_dataset_t *origin;
774cde58dbcSMatthew Ahrens
7753b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp,
776c1379625SJustin T. Gibbs dsl_dir_phys(ds->ds_dir)->dd_origin_obj, FTAG, &origin));
777cde58dbcSMatthew Ahrens
778c1379625SJustin T. Gibbs if (dsl_dir_phys(origin->ds_dir)->dd_clones == 0) {
779cde58dbcSMatthew Ahrens dmu_buf_will_dirty(origin->ds_dir->dd_dbuf, tx);
780c1379625SJustin T. Gibbs dsl_dir_phys(origin->ds_dir)->dd_clones =
781c1379625SJustin T. Gibbs zap_create(mos, DMU_OT_DSL_CLONES, DMU_OT_NONE,
782c1379625SJustin T. Gibbs 0, tx);
783cde58dbcSMatthew Ahrens }
784cde58dbcSMatthew Ahrens
7853b2aab18SMatthew Ahrens VERIFY0(zap_add_int(dp->dp_meta_objset,
786c1379625SJustin T. Gibbs dsl_dir_phys(origin->ds_dir)->dd_clones,
787c1379625SJustin T. Gibbs ds->ds_object, tx));
788cde58dbcSMatthew Ahrens
789cde58dbcSMatthew Ahrens dsl_dataset_rele(origin, FTAG);
790cde58dbcSMatthew Ahrens }
791cde58dbcSMatthew Ahrens return (0);
792cde58dbcSMatthew Ahrens }
793cde58dbcSMatthew Ahrens
794cde58dbcSMatthew Ahrens void
dsl_pool_upgrade_dir_clones(dsl_pool_t * dp,dmu_tx_t * tx)795cde58dbcSMatthew Ahrens dsl_pool_upgrade_dir_clones(dsl_pool_t *dp, dmu_tx_t *tx)
796cde58dbcSMatthew Ahrens {
797cde58dbcSMatthew Ahrens ASSERT(dmu_tx_is_syncing(tx));
798cde58dbcSMatthew Ahrens uint64_t obj;
799cde58dbcSMatthew Ahrens
800cde58dbcSMatthew Ahrens (void) dsl_dir_create_sync(dp, dp->dp_root_dir, FREE_DIR_NAME, tx);
8013b2aab18SMatthew Ahrens VERIFY0(dsl_pool_open_special_dir(dp,
802cde58dbcSMatthew Ahrens FREE_DIR_NAME, &dp->dp_free_dir));
803cde58dbcSMatthew Ahrens
804cde58dbcSMatthew Ahrens /*
805cde58dbcSMatthew Ahrens * We can't use bpobj_alloc(), because spa_version() still
806cde58dbcSMatthew Ahrens * returns the old version, and we need a new-version bpobj with
807cde58dbcSMatthew Ahrens * subobj support. So call dmu_object_alloc() directly.
808cde58dbcSMatthew Ahrens */
809cde58dbcSMatthew Ahrens obj = dmu_object_alloc(dp->dp_meta_objset, DMU_OT_BPOBJ,
810b5152584SMatthew Ahrens SPA_OLD_MAXBLOCKSIZE, DMU_OT_BPOBJ_HDR, sizeof (bpobj_phys_t), tx);
8113b2aab18SMatthew Ahrens VERIFY0(zap_add(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
812cde58dbcSMatthew Ahrens DMU_POOL_FREE_BPOBJ, sizeof (uint64_t), 1, &obj, tx));
8133b2aab18SMatthew Ahrens VERIFY0(bpobj_open(&dp->dp_free_bpobj, dp->dp_meta_objset, obj));
814cde58dbcSMatthew Ahrens
8153b2aab18SMatthew Ahrens VERIFY0(dmu_objset_find_dp(dp, dp->dp_root_dir_obj,
81612380e1eSArne Jansen upgrade_dir_clones_cb, tx, DS_FIND_CHILDREN | DS_FIND_SERIALIZE));
817cde58dbcSMatthew Ahrens }
818cde58dbcSMatthew Ahrens
819088f3894Sahrens void
dsl_pool_create_origin(dsl_pool_t * dp,dmu_tx_t * tx)820088f3894Sahrens dsl_pool_create_origin(dsl_pool_t *dp, dmu_tx_t *tx)
821088f3894Sahrens {
822088f3894Sahrens uint64_t dsobj;
823088f3894Sahrens dsl_dataset_t *ds;
824088f3894Sahrens
825088f3894Sahrens ASSERT(dmu_tx_is_syncing(tx));
826088f3894Sahrens ASSERT(dp->dp_origin_snap == NULL);
8273b2aab18SMatthew Ahrens ASSERT(rrw_held(&dp->dp_config_rwlock, RW_WRITER));
828088f3894Sahrens
829088f3894Sahrens /* create the origin dir, ds, & snap-ds */
830088f3894Sahrens dsobj = dsl_dataset_create_sync(dp->dp_root_dir, ORIGIN_DIR_NAME,
831088f3894Sahrens NULL, 0, kcred, tx);
8323b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
8333b2aab18SMatthew Ahrens dsl_dataset_snapshot_sync_impl(ds, ORIGIN_DIR_NAME, tx);
834c1379625SJustin T. Gibbs VERIFY0(dsl_dataset_hold_obj(dp, dsl_dataset_phys(ds)->ds_prev_snap_obj,
835088f3894Sahrens dp, &dp->dp_origin_snap));
836088f3894Sahrens dsl_dataset_rele(ds, FTAG);
837088f3894Sahrens }
8389d3574bfSNeil Perrin
8399d3574bfSNeil Perrin taskq_t *
dsl_pool_vnrele_taskq(dsl_pool_t * dp)8409d3574bfSNeil Perrin dsl_pool_vnrele_taskq(dsl_pool_t *dp)
8419d3574bfSNeil Perrin {
8429d3574bfSNeil Perrin return (dp->dp_vnrele_taskq);
8439d3574bfSNeil Perrin }
844ca45db41SChris Kirby
845ca45db41SChris Kirby /*
846ca45db41SChris Kirby * Walk through the pool-wide zap object of temporary snapshot user holds
847ca45db41SChris Kirby * and release them.
848ca45db41SChris Kirby */
849ca45db41SChris Kirby void
dsl_pool_clean_tmp_userrefs(dsl_pool_t * dp)850ca45db41SChris Kirby dsl_pool_clean_tmp_userrefs(dsl_pool_t *dp)
851ca45db41SChris Kirby {
852ca45db41SChris Kirby zap_attribute_t za;
853ca45db41SChris Kirby zap_cursor_t zc;
854ca45db41SChris Kirby objset_t *mos = dp->dp_meta_objset;
855ca45db41SChris Kirby uint64_t zapobj = dp->dp_tmp_userrefs_obj;
856a7a845e4SSteven Hartland nvlist_t *holds;
857ca45db41SChris Kirby
858ca45db41SChris Kirby if (zapobj == 0)
859ca45db41SChris Kirby return;
860ca45db41SChris Kirby ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS);
861ca45db41SChris Kirby
862a7a845e4SSteven Hartland holds = fnvlist_alloc();
863a7a845e4SSteven Hartland
864ca45db41SChris Kirby for (zap_cursor_init(&zc, mos, zapobj);
865ca45db41SChris Kirby zap_cursor_retrieve(&zc, &za) == 0;
866ca45db41SChris Kirby zap_cursor_advance(&zc)) {
867ca45db41SChris Kirby char *htag;
868a7a845e4SSteven Hartland nvlist_t *tags;
869ca45db41SChris Kirby
870ca45db41SChris Kirby htag = strchr(za.za_name, '-');
871ca45db41SChris Kirby *htag = '\0';
872ca45db41SChris Kirby ++htag;
873a7a845e4SSteven Hartland if (nvlist_lookup_nvlist(holds, za.za_name, &tags) != 0) {
874a7a845e4SSteven Hartland tags = fnvlist_alloc();
875a7a845e4SSteven Hartland fnvlist_add_boolean(tags, htag);
876a7a845e4SSteven Hartland fnvlist_add_nvlist(holds, za.za_name, tags);
877a7a845e4SSteven Hartland fnvlist_free(tags);
878a7a845e4SSteven Hartland } else {
879a7a845e4SSteven Hartland fnvlist_add_boolean(tags, htag);
880ca45db41SChris Kirby }
881a7a845e4SSteven Hartland }
882a7a845e4SSteven Hartland dsl_dataset_user_release_tmp(dp, holds);
883a7a845e4SSteven Hartland fnvlist_free(holds);
884ca45db41SChris Kirby zap_cursor_fini(&zc);
885ca45db41SChris Kirby }
886ca45db41SChris Kirby
887ca45db41SChris Kirby /*
888ca45db41SChris Kirby * Create the pool-wide zap object for storing temporary snapshot holds.
889ca45db41SChris Kirby */
890ca45db41SChris Kirby void
dsl_pool_user_hold_create_obj(dsl_pool_t * dp,dmu_tx_t * tx)891ca45db41SChris Kirby dsl_pool_user_hold_create_obj(dsl_pool_t *dp, dmu_tx_t *tx)
892ca45db41SChris Kirby {
893ca45db41SChris Kirby objset_t *mos = dp->dp_meta_objset;
894ca45db41SChris Kirby
895ca45db41SChris Kirby ASSERT(dp->dp_tmp_userrefs_obj == 0);
896ca45db41SChris Kirby ASSERT(dmu_tx_is_syncing(tx));
897ca45db41SChris Kirby
898ad135b5dSChristopher Siden dp->dp_tmp_userrefs_obj = zap_create_link(mos, DMU_OT_USERREFS,
899ad135b5dSChristopher Siden DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_TMP_USERREFS, tx);
900ca45db41SChris Kirby }
901ca45db41SChris Kirby
902ca45db41SChris Kirby static int
dsl_pool_user_hold_rele_impl(dsl_pool_t * dp,uint64_t dsobj,const char * tag,uint64_t now,dmu_tx_t * tx,boolean_t holding)903ca45db41SChris Kirby dsl_pool_user_hold_rele_impl(dsl_pool_t *dp, uint64_t dsobj,
9043b2aab18SMatthew Ahrens const char *tag, uint64_t now, dmu_tx_t *tx, boolean_t holding)
905ca45db41SChris Kirby {
906ca45db41SChris Kirby objset_t *mos = dp->dp_meta_objset;
907ca45db41SChris Kirby uint64_t zapobj = dp->dp_tmp_userrefs_obj;
908ca45db41SChris Kirby char *name;
909ca45db41SChris Kirby int error;
910ca45db41SChris Kirby
911ca45db41SChris Kirby ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS);
912ca45db41SChris Kirby ASSERT(dmu_tx_is_syncing(tx));
913ca45db41SChris Kirby
914ca45db41SChris Kirby /*
915ca45db41SChris Kirby * If the pool was created prior to SPA_VERSION_USERREFS, the
916ca45db41SChris Kirby * zap object for temporary holds might not exist yet.
917ca45db41SChris Kirby */
918ca45db41SChris Kirby if (zapobj == 0) {
919ca45db41SChris Kirby if (holding) {
920ca45db41SChris Kirby dsl_pool_user_hold_create_obj(dp, tx);
921ca45db41SChris Kirby zapobj = dp->dp_tmp_userrefs_obj;
922ca45db41SChris Kirby } else {
923be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT));
924ca45db41SChris Kirby }
925ca45db41SChris Kirby }
926ca45db41SChris Kirby
927ca45db41SChris Kirby name = kmem_asprintf("%llx-%s", (u_longlong_t)dsobj, tag);
928ca45db41SChris Kirby if (holding)
9293b2aab18SMatthew Ahrens error = zap_add(mos, zapobj, name, 8, 1, &now, tx);
930ca45db41SChris Kirby else
931ca45db41SChris Kirby error = zap_remove(mos, zapobj, name, tx);
932ca45db41SChris Kirby strfree(name);
933ca45db41SChris Kirby
934ca45db41SChris Kirby return (error);
935ca45db41SChris Kirby }
936ca45db41SChris Kirby
937ca45db41SChris Kirby /*
938ca45db41SChris Kirby * Add a temporary hold for the given dataset object and tag.
939ca45db41SChris Kirby */
940ca45db41SChris Kirby int
dsl_pool_user_hold(dsl_pool_t * dp,uint64_t dsobj,const char * tag,uint64_t now,dmu_tx_t * tx)941ca45db41SChris Kirby dsl_pool_user_hold(dsl_pool_t *dp, uint64_t dsobj, const char *tag,
9423b2aab18SMatthew Ahrens uint64_t now, dmu_tx_t *tx)
943ca45db41SChris Kirby {
94415508ac0SChris Kirby return (dsl_pool_user_hold_rele_impl(dp, dsobj, tag, now, tx, B_TRUE));
945ca45db41SChris Kirby }
946ca45db41SChris Kirby
947ca45db41SChris Kirby /*
948ca45db41SChris Kirby * Release a temporary hold for the given dataset object and tag.
949ca45db41SChris Kirby */
950ca45db41SChris Kirby int
dsl_pool_user_release(dsl_pool_t * dp,uint64_t dsobj,const char * tag,dmu_tx_t * tx)951ca45db41SChris Kirby dsl_pool_user_release(dsl_pool_t *dp, uint64_t dsobj, const char *tag,
952ca45db41SChris Kirby dmu_tx_t *tx)
953ca45db41SChris Kirby {
954ca45db41SChris Kirby return (dsl_pool_user_hold_rele_impl(dp, dsobj, tag, NULL,
955ca45db41SChris Kirby tx, B_FALSE));
956ca45db41SChris Kirby }
9573b2aab18SMatthew Ahrens
9583b2aab18SMatthew Ahrens /*
9593b2aab18SMatthew Ahrens * DSL Pool Configuration Lock
9603b2aab18SMatthew Ahrens *
9613b2aab18SMatthew Ahrens * The dp_config_rwlock protects against changes to DSL state (e.g. dataset
9623b2aab18SMatthew Ahrens * creation / destruction / rename / property setting). It must be held for
9633b2aab18SMatthew Ahrens * read to hold a dataset or dsl_dir. I.e. you must call
9643b2aab18SMatthew Ahrens * dsl_pool_config_enter() or dsl_pool_hold() before calling
9653b2aab18SMatthew Ahrens * dsl_{dataset,dir}_hold{_obj}. In most circumstances, the dp_config_rwlock
9663b2aab18SMatthew Ahrens * must be held continuously until all datasets and dsl_dirs are released.
9673b2aab18SMatthew Ahrens *
9683b2aab18SMatthew Ahrens * The only exception to this rule is that if a "long hold" is placed on
9693b2aab18SMatthew Ahrens * a dataset, then the dp_config_rwlock may be dropped while the dataset
9703b2aab18SMatthew Ahrens * is still held. The long hold will prevent the dataset from being
9713b2aab18SMatthew Ahrens * destroyed -- the destroy will fail with EBUSY. A long hold can be
9723b2aab18SMatthew Ahrens * obtained by calling dsl_dataset_long_hold(), or by "owning" a dataset
9733b2aab18SMatthew Ahrens * (by calling dsl_{dataset,objset}_{try}own{_obj}).
9743b2aab18SMatthew Ahrens *
9753b2aab18SMatthew Ahrens * Legitimate long-holders (including owners) should be long-running, cancelable
9763b2aab18SMatthew Ahrens * tasks that should cause "zfs destroy" to fail. This includes DMU
9773b2aab18SMatthew Ahrens * consumers (i.e. a ZPL filesystem being mounted or ZVOL being open),
9783b2aab18SMatthew Ahrens * "zfs send", and "zfs diff". There are several other long-holders whose
9793b2aab18SMatthew Ahrens * uses are suboptimal (e.g. "zfs promote", and zil_suspend()).
9803b2aab18SMatthew Ahrens *
9813b2aab18SMatthew Ahrens * The usual formula for long-holding would be:
9823b2aab18SMatthew Ahrens * dsl_pool_hold()
9833b2aab18SMatthew Ahrens * dsl_dataset_hold()
9843b2aab18SMatthew Ahrens * ... perform checks ...
9853b2aab18SMatthew Ahrens * dsl_dataset_long_hold()
9863b2aab18SMatthew Ahrens * dsl_pool_rele()
9873b2aab18SMatthew Ahrens * ... perform long-running task ...
9883b2aab18SMatthew Ahrens * dsl_dataset_long_rele()
9893b2aab18SMatthew Ahrens * dsl_dataset_rele()
9903b2aab18SMatthew Ahrens *
9913b2aab18SMatthew Ahrens * Note that when the long hold is released, the dataset is still held but
9923b2aab18SMatthew Ahrens * the pool is not held. The dataset may change arbitrarily during this time
9933b2aab18SMatthew Ahrens * (e.g. it could be destroyed). Therefore you shouldn't do anything to the
9943b2aab18SMatthew Ahrens * dataset except release it.
9953b2aab18SMatthew Ahrens *
9963b2aab18SMatthew Ahrens * User-initiated operations (e.g. ioctls, zfs_ioc_*()) are either read-only
9973b2aab18SMatthew Ahrens * or modifying operations.
9983b2aab18SMatthew Ahrens *
9993b2aab18SMatthew Ahrens * Modifying operations should generally use dsl_sync_task(). The synctask
10003b2aab18SMatthew Ahrens * infrastructure enforces proper locking strategy with respect to the
10013b2aab18SMatthew Ahrens * dp_config_rwlock. See the comment above dsl_sync_task() for details.
10023b2aab18SMatthew Ahrens *
10033b2aab18SMatthew Ahrens * Read-only operations will manually hold the pool, then the dataset, obtain
10043b2aab18SMatthew Ahrens * information from the dataset, then release the pool and dataset.
10053b2aab18SMatthew Ahrens * dmu_objset_{hold,rele}() are convenience routines that also do the pool
10063b2aab18SMatthew Ahrens * hold/rele.
10073b2aab18SMatthew Ahrens */
10083b2aab18SMatthew Ahrens
10093b2aab18SMatthew Ahrens int
dsl_pool_hold(const char * name,void * tag,dsl_pool_t ** dp)10103b2aab18SMatthew Ahrens dsl_pool_hold(const char *name, void *tag, dsl_pool_t **dp)
10113b2aab18SMatthew Ahrens {
10123b2aab18SMatthew Ahrens spa_t *spa;
10133b2aab18SMatthew Ahrens int error;
10143b2aab18SMatthew Ahrens
10153b2aab18SMatthew Ahrens error = spa_open(name, &spa, tag);
10163b2aab18SMatthew Ahrens if (error == 0) {
10173b2aab18SMatthew Ahrens *dp = spa_get_dsl(spa);
10183b2aab18SMatthew Ahrens dsl_pool_config_enter(*dp, tag);
10193b2aab18SMatthew Ahrens }
10203b2aab18SMatthew Ahrens return (error);
10213b2aab18SMatthew Ahrens }
10223b2aab18SMatthew Ahrens
10233b2aab18SMatthew Ahrens void
dsl_pool_rele(dsl_pool_t * dp,void * tag)10243b2aab18SMatthew Ahrens dsl_pool_rele(dsl_pool_t *dp, void *tag)
10253b2aab18SMatthew Ahrens {
10263b2aab18SMatthew Ahrens dsl_pool_config_exit(dp, tag);
10273b2aab18SMatthew Ahrens spa_close(dp->dp_spa, tag);
10283b2aab18SMatthew Ahrens }
10293b2aab18SMatthew Ahrens
10303b2aab18SMatthew Ahrens void
dsl_pool_config_enter(dsl_pool_t * dp,void * tag)10313b2aab18SMatthew Ahrens dsl_pool_config_enter(dsl_pool_t *dp, void *tag)
10323b2aab18SMatthew Ahrens {
10333b2aab18SMatthew Ahrens /*
10343b2aab18SMatthew Ahrens * We use a "reentrant" reader-writer lock, but not reentrantly.
10353b2aab18SMatthew Ahrens *
10363b2aab18SMatthew Ahrens * The rrwlock can (with the track_all flag) track all reading threads,
10373b2aab18SMatthew Ahrens * which is very useful for debugging which code path failed to release
10383b2aab18SMatthew Ahrens * the lock, and for verifying that the *current* thread does hold
10393b2aab18SMatthew Ahrens * the lock.
10403b2aab18SMatthew Ahrens *
10413b2aab18SMatthew Ahrens * (Unlike a rwlock, which knows that N threads hold it for
10423b2aab18SMatthew Ahrens * read, but not *which* threads, so rw_held(RW_READER) returns TRUE
10433b2aab18SMatthew Ahrens * if any thread holds it for read, even if this thread doesn't).
10443b2aab18SMatthew Ahrens */
10453b2aab18SMatthew Ahrens ASSERT(!rrw_held(&dp->dp_config_rwlock, RW_READER));
10463b2aab18SMatthew Ahrens rrw_enter(&dp->dp_config_rwlock, RW_READER, tag);
10473b2aab18SMatthew Ahrens }
10483b2aab18SMatthew Ahrens
10493b2aab18SMatthew Ahrens void
dsl_pool_config_enter_prio(dsl_pool_t * dp,void * tag)10501d3f896fSArne Jansen dsl_pool_config_enter_prio(dsl_pool_t *dp, void *tag)
10511d3f896fSArne Jansen {
10521d3f896fSArne Jansen ASSERT(!rrw_held(&dp->dp_config_rwlock, RW_READER));
10531d3f896fSArne Jansen rrw_enter_read_prio(&dp->dp_config_rwlock, tag);
10541d3f896fSArne Jansen }
10551d3f896fSArne Jansen
10561d3f896fSArne Jansen void
dsl_pool_config_exit(dsl_pool_t * dp,void * tag)10573b2aab18SMatthew Ahrens dsl_pool_config_exit(dsl_pool_t *dp, void *tag)
10583b2aab18SMatthew Ahrens {
10593b2aab18SMatthew Ahrens rrw_exit(&dp->dp_config_rwlock, tag);
10603b2aab18SMatthew Ahrens }
10613b2aab18SMatthew Ahrens
10623b2aab18SMatthew Ahrens boolean_t
dsl_pool_config_held(dsl_pool_t * dp)10633b2aab18SMatthew Ahrens dsl_pool_config_held(dsl_pool_t *dp)
10643b2aab18SMatthew Ahrens {
10653b2aab18SMatthew Ahrens return (RRW_LOCK_HELD(&dp->dp_config_rwlock));
10663b2aab18SMatthew Ahrens }
106712380e1eSArne Jansen
106812380e1eSArne Jansen boolean_t
dsl_pool_config_held_writer(dsl_pool_t * dp)106912380e1eSArne Jansen dsl_pool_config_held_writer(dsl_pool_t *dp)
107012380e1eSArne Jansen {
107112380e1eSArne Jansen return (RRW_WRITE_HELD(&dp->dp_config_rwlock));
107212380e1eSArne Jansen }
1073