]>
Commit | Line | Data |
---|---|---|
34dc7c2f BB |
1 | /* |
2 | * CDDL HEADER START | |
3 | * | |
4 | * The contents of this file are subject to the terms of the | |
5 | * Common Development and Distribution License (the "License"). | |
6 | * You may not use this file except in compliance with the License. | |
7 | * | |
8 | * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE | |
1d3ba0bf | 9 | * or https://opensource.org/licenses/CDDL-1.0. |
34dc7c2f BB |
10 | * See the License for the specific language governing permissions |
11 | * and limitations under the License. | |
12 | * | |
13 | * When distributing Covered Code, include this CDDL HEADER in each | |
14 | * file and include the License file at usr/src/OPENSOLARIS.LICENSE. | |
15 | * If applicable, add the following below this CDDL HEADER, with the | |
16 | * fields enclosed by brackets "[]" replaced with your own identifying | |
17 | * information: Portions Copyright [yyyy] [name of copyright owner] | |
18 | * | |
19 | * CDDL HEADER END | |
20 | */ | |
21 | /* | |
428870ff | 22 | * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. |
ba67d821 | 23 | * Copyright (c) 2011, 2020 by Delphix. All rights reserved. |
95fd54a1 | 24 | * Copyright (c) 2013 Steven Hartland. All rights reserved. |
0c66c32d | 25 | * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved. |
539d33c7 | 26 | * Copyright 2016 Nexenta Systems, Inc. All rights reserved. |
34dc7c2f BB |
27 | */ |
28 | ||
34dc7c2f BB |
29 | #include <sys/dsl_pool.h> |
30 | #include <sys/dsl_dataset.h> | |
428870ff | 31 | #include <sys/dsl_prop.h> |
34dc7c2f BB |
32 | #include <sys/dsl_dir.h> |
33 | #include <sys/dsl_synctask.h> | |
428870ff BB |
34 | #include <sys/dsl_scan.h> |
35 | #include <sys/dnode.h> | |
34dc7c2f BB |
36 | #include <sys/dmu_tx.h> |
37 | #include <sys/dmu_objset.h> | |
38 | #include <sys/arc.h> | |
39 | #include <sys/zap.h> | |
40 | #include <sys/zio.h> | |
41 | #include <sys/zfs_context.h> | |
42 | #include <sys/fs/zfs.h> | |
b128c09f BB |
43 | #include <sys/zfs_znode.h> |
44 | #include <sys/spa_impl.h> | |
d2734cce SD |
45 | #include <sys/vdev_impl.h> |
46 | #include <sys/metaslab_impl.h> | |
9ae529ec CS |
47 | #include <sys/bptree.h> |
48 | #include <sys/zfeature.h> | |
29809a6c | 49 | #include <sys/zil_impl.h> |
13fe0198 | 50 | #include <sys/dsl_userhold.h> |
e5d1c27e | 51 | #include <sys/trace_zfs.h> |
379ca9cf | 52 | #include <sys/mmp.h> |
34dc7c2f | 53 | |
e8b96c60 MA |
54 | /* |
55 | * ZFS Write Throttle | |
56 | * ------------------ | |
57 | * | |
58 | * ZFS must limit the rate of incoming writes to the rate at which it is able | |
59 | * to sync data modifications to the backend storage. Throttling by too much | |
60 | * creates an artificial limit; throttling by too little can only be sustained | |
61 | * for short periods and would lead to highly lumpy performance. On a per-pool | |
62 | * basis, ZFS tracks the amount of modified (dirty) data. As operations change | |
63 | * data, the amount of dirty data increases; as ZFS syncs out data, the amount | |
64 | * of dirty data decreases. When the amount of dirty data exceeds a | |
65 | * predetermined threshold further modifications are blocked until the amount | |
66 | * of dirty data decreases (as data is synced out). | |
67 | * | |
68 | * The limit on dirty data is tunable, and should be adjusted according to | |
69 | * both the IO capacity and available memory of the system. The larger the | |
70 | * window, the more ZFS is able to aggregate and amortize metadata (and data) | |
71 | * changes. However, memory is a limited resource, and allowing for more dirty | |
72 | * data comes at the cost of keeping other useful data in memory (for example | |
73 | * ZFS data cached by the ARC). | |
74 | * | |
75 | * Implementation | |
76 | * | |
77 | * As buffers are modified dsl_pool_willuse_space() increments both the per- | |
78 | * txg (dp_dirty_pertxg[]) and poolwide (dp_dirty_total) accounting of | |
79 | * dirty space used; dsl_pool_dirty_space() decrements those values as data | |
80 | * is synced out from dsl_pool_sync(). While only the poolwide value is | |
81 | * relevant, the per-txg value is useful for debugging. The tunable | |
82 | * zfs_dirty_data_max determines the dirty space limit. Once that value is | |
83 | * exceeded, new writes are halted until space frees up. | |
84 | * | |
00f198de | 85 | * The zfs_dirty_data_sync_percent tunable dictates the threshold at which we |
e8b96c60 MA |
86 | * ensure that there is a txg syncing (see the comment in txg.c for a full |
87 | * description of transaction group stages). | |
88 | * | |
89 | * The IO scheduler uses both the dirty space limit and current amount of | |
90 | * dirty data as inputs. Those values affect the number of concurrent IOs ZFS | |
91 | * issues. See the comment in vdev_queue.c for details of the IO scheduler. | |
92 | * | |
93 | * The delay is also calculated based on the amount of dirty data. See the | |
94 | * comment above dmu_tx_delay() for details. | |
95 | */ | |
96 | ||
97 | /* | |
98 | * zfs_dirty_data_max will be set to zfs_dirty_data_max_percent% of all memory, | |
99 | * capped at zfs_dirty_data_max_max. It can also be overridden with a module | |
100 | * parameter. | |
101 | */ | |
ab8d9c17 RY |
102 | uint64_t zfs_dirty_data_max = 0; |
103 | uint64_t zfs_dirty_data_max_max = 0; | |
fdc2d303 RY |
104 | uint_t zfs_dirty_data_max_percent = 10; |
105 | uint_t zfs_dirty_data_max_max_percent = 25; | |
b128c09f | 106 | |
a7bd20e3 | 107 | /* |
84d0a03f AM |
108 | * The upper limit of TX_WRITE log data. Write operations are throttled |
109 | * when approaching the limit until log data is cleared out after txg sync. | |
a7bd20e3 KJ |
110 | * It only counts TX_WRITE log with WR_COPIED or WR_NEED_COPY. |
111 | */ | |
ab8d9c17 | 112 | uint64_t zfs_wrlog_data_max = 0; |
a7bd20e3 | 113 | |
e8b96c60 | 114 | /* |
dfbe2675 MA |
115 | * If there's at least this much dirty data (as a percentage of |
116 | * zfs_dirty_data_max), push out a txg. This should be less than | |
117 | * zfs_vdev_async_write_active_min_dirty_percent. | |
e8b96c60 | 118 | */ |
fdc2d303 | 119 | static uint_t zfs_dirty_data_sync_percent = 20; |
34dc7c2f | 120 | |
e8b96c60 MA |
121 | /* |
122 | * Once there is this amount of dirty data, the dmu_tx_delay() will kick in | |
123 | * and delay each transaction. | |
124 | * This value should be >= zfs_vdev_async_write_active_max_dirty_percent. | |
125 | */ | |
fdc2d303 | 126 | uint_t zfs_delay_min_dirty_percent = 60; |
b128c09f | 127 | |
e8b96c60 MA |
128 | /* |
129 | * This controls how quickly the delay approaches infinity. | |
130 | * Larger values cause it to delay more for a given amount of dirty data. | |
131 | * Therefore larger values will cause there to be less dirty data for a | |
132 | * given throughput. | |
133 | * | |
134 | * For the smoothest delay, this value should be about 1 billion divided | |
135 | * by the maximum number of operations per second. This will smoothly | |
136 | * handle between 10x and 1/10th this number. | |
137 | * | |
138 | * Note: zfs_delay_scale * zfs_dirty_data_max must be < 2^64, due to the | |
139 | * multiply in dmu_tx_delay(). | |
140 | */ | |
ab8d9c17 | 141 | uint64_t zfs_delay_scale = 1000 * 1000 * 1000 / 2000; |
b128c09f | 142 | |
a032ac4b BB |
143 | /* |
144 | * These tunables determine the behavior of how zil_itxg_clean() is | |
145 | * called via zil_clean() in the context of spa_sync(). When an itxg | |
146 | * list needs to be cleaned, TQ_NOSLEEP will be used when dispatching. | |
147 | * If the dispatch fails, the call to zil_itxg_clean() will occur | |
148 | * synchronously in the context of spa_sync(), which can negatively | |
149 | * impact the performance of spa_sync() (e.g. in the case of the itxg | |
150 | * list having a large number of itxs that needs to be cleaned). | |
151 | * | |
152 | * Thus, these tunables can be used to manipulate the behavior of the | |
153 | * taskq used by zil_clean(); they determine the number of taskq entries | |
154 | * that are pre-populated when the taskq is first created (via the | |
155 | * "zfs_zil_clean_taskq_minalloc" tunable) and the maximum number of | |
156 | * taskq entries that are cached after an on-demand allocation (via the | |
157 | * "zfs_zil_clean_taskq_maxalloc"). | |
158 | * | |
159 | * The idea being, we want to try reasonably hard to ensure there will | |
160 | * already be a taskq entry pre-allocated by the time that it is needed | |
161 | * by zil_clean(). This way, we can avoid the possibility of an | |
162 | * on-demand allocation of a new taskq entry from failing, which would | |
163 | * result in zil_itxg_clean() being called synchronously from zil_clean() | |
164 | * (which can adversely affect performance of spa_sync()). | |
165 | * | |
166 | * Additionally, the number of threads used by the taskq can be | |
167 | * configured via the "zfs_zil_clean_taskq_nthr_pct" tunable. | |
168 | */ | |
18168da7 AZ |
169 | static int zfs_zil_clean_taskq_nthr_pct = 100; |
170 | static int zfs_zil_clean_taskq_minalloc = 1024; | |
171 | static int zfs_zil_clean_taskq_maxalloc = 1024 * 1024; | |
a032ac4b | 172 | |
428870ff | 173 | int |
b128c09f | 174 | dsl_pool_open_special_dir(dsl_pool_t *dp, const char *name, dsl_dir_t **ddp) |
34dc7c2f BB |
175 | { |
176 | uint64_t obj; | |
177 | int err; | |
178 | ||
179 | err = zap_lookup(dp->dp_meta_objset, | |
d683ddbb | 180 | dsl_dir_phys(dp->dp_root_dir)->dd_child_dir_zapobj, |
b128c09f | 181 | name, sizeof (obj), 1, &obj); |
34dc7c2f BB |
182 | if (err) |
183 | return (err); | |
184 | ||
13fe0198 | 185 | return (dsl_dir_hold_obj(dp, obj, name, dp, ddp)); |
34dc7c2f BB |
186 | } |
187 | ||
188 | static dsl_pool_t * | |
189 | dsl_pool_open_impl(spa_t *spa, uint64_t txg) | |
190 | { | |
191 | dsl_pool_t *dp; | |
192 | blkptr_t *bp = spa_get_rootblkptr(spa); | |
34dc7c2f BB |
193 | |
194 | dp = kmem_zalloc(sizeof (dsl_pool_t), KM_SLEEP); | |
195 | dp->dp_spa = spa; | |
196 | dp->dp_meta_rootbp = *bp; | |
13fe0198 | 197 | rrw_init(&dp->dp_config_rwlock, B_TRUE); |
34dc7c2f | 198 | txg_init(dp, txg); |
379ca9cf | 199 | mmp_init(spa); |
34dc7c2f | 200 | |
4747a7d3 | 201 | txg_list_create(&dp->dp_dirty_datasets, spa, |
34dc7c2f | 202 | offsetof(dsl_dataset_t, ds_dirty_link)); |
4747a7d3 | 203 | txg_list_create(&dp->dp_dirty_zilogs, spa, |
29809a6c | 204 | offsetof(zilog_t, zl_dirty_link)); |
4747a7d3 | 205 | txg_list_create(&dp->dp_dirty_dirs, spa, |
34dc7c2f | 206 | offsetof(dsl_dir_t, dd_dirty_link)); |
4747a7d3 | 207 | txg_list_create(&dp->dp_sync_tasks, spa, |
13fe0198 | 208 | offsetof(dsl_sync_task_t, dst_node)); |
d2734cce SD |
209 | txg_list_create(&dp->dp_early_sync_tasks, spa, |
210 | offsetof(dsl_sync_task_t, dst_node)); | |
34dc7c2f | 211 | |
3bd4df38 | 212 | dp->dp_sync_taskq = spa_sync_tq_create(spa, "dp_sync_taskq"); |
64fc7762 | 213 | |
a032ac4b BB |
214 | dp->dp_zil_clean_taskq = taskq_create("dp_zil_clean_taskq", |
215 | zfs_zil_clean_taskq_nthr_pct, minclsyspri, | |
216 | zfs_zil_clean_taskq_minalloc, | |
217 | zfs_zil_clean_taskq_maxalloc, | |
218 | TASKQ_PREPOPULATE | TASKQ_THREADS_CPU_PCT); | |
219 | ||
34dc7c2f | 220 | mutex_init(&dp->dp_lock, NULL, MUTEX_DEFAULT, NULL); |
e8b96c60 | 221 | cv_init(&dp->dp_spaceavail_cv, NULL, CV_DEFAULT, NULL); |
34dc7c2f | 222 | |
a7bd20e3 KJ |
223 | aggsum_init(&dp->dp_wrlog_total, 0); |
224 | for (int i = 0; i < TXG_SIZE; i++) { | |
225 | aggsum_init(&dp->dp_wrlog_pertxg[i], 0); | |
226 | } | |
227 | ||
60a4c7d2 PD |
228 | dp->dp_zrele_taskq = taskq_create("z_zrele", 100, defclsyspri, |
229 | boot_ncpus * 8, INT_MAX, TASKQ_PREPOPULATE | TASKQ_DYNAMIC | | |
230 | TASKQ_THREADS_CPU_PCT); | |
dcec0a12 | 231 | dp->dp_unlinked_drain_taskq = taskq_create("z_unlinked_drain", |
60a4c7d2 PD |
232 | 100, defclsyspri, boot_ncpus, INT_MAX, |
233 | TASKQ_PREPOPULATE | TASKQ_DYNAMIC | TASKQ_THREADS_CPU_PCT); | |
9babb374 | 234 | |
34dc7c2f BB |
235 | return (dp); |
236 | } | |
237 | ||
238 | int | |
9ae529ec | 239 | dsl_pool_init(spa_t *spa, uint64_t txg, dsl_pool_t **dpp) |
34dc7c2f BB |
240 | { |
241 | int err; | |
242 | dsl_pool_t *dp = dsl_pool_open_impl(spa, txg); | |
9ae529ec | 243 | |
b7faa7aa G |
244 | /* |
245 | * Initialize the caller's dsl_pool_t structure before we actually open | |
246 | * the meta objset. This is done because a self-healing write zio may | |
247 | * be issued as part of dmu_objset_open_impl() and the spa needs its | |
248 | * dsl_pool_t initialized in order to handle the write. | |
249 | */ | |
250 | *dpp = dp; | |
251 | ||
9ae529ec CS |
252 | err = dmu_objset_open_impl(spa, NULL, &dp->dp_meta_rootbp, |
253 | &dp->dp_meta_objset); | |
b7faa7aa | 254 | if (err != 0) { |
9ae529ec | 255 | dsl_pool_close(dp); |
b7faa7aa G |
256 | *dpp = NULL; |
257 | } | |
9ae529ec CS |
258 | |
259 | return (err); | |
260 | } | |
261 | ||
262 | int | |
263 | dsl_pool_open(dsl_pool_t *dp) | |
264 | { | |
265 | int err; | |
b128c09f BB |
266 | dsl_dir_t *dd; |
267 | dsl_dataset_t *ds; | |
428870ff | 268 | uint64_t obj; |
34dc7c2f | 269 | |
13fe0198 | 270 | rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG); |
34dc7c2f BB |
271 | err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, |
272 | DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1, | |
273 | &dp->dp_root_dir_obj); | |
274 | if (err) | |
275 | goto out; | |
276 | ||
13fe0198 | 277 | err = dsl_dir_hold_obj(dp, dp->dp_root_dir_obj, |
34dc7c2f BB |
278 | NULL, dp, &dp->dp_root_dir); |
279 | if (err) | |
280 | goto out; | |
281 | ||
b128c09f | 282 | err = dsl_pool_open_special_dir(dp, MOS_DIR_NAME, &dp->dp_mos_dir); |
34dc7c2f BB |
283 | if (err) |
284 | goto out; | |
285 | ||
9ae529ec | 286 | if (spa_version(dp->dp_spa) >= SPA_VERSION_ORIGIN) { |
b128c09f BB |
287 | err = dsl_pool_open_special_dir(dp, ORIGIN_DIR_NAME, &dd); |
288 | if (err) | |
289 | goto out; | |
d683ddbb JG |
290 | err = dsl_dataset_hold_obj(dp, |
291 | dsl_dir_phys(dd)->dd_head_dataset_obj, FTAG, &ds); | |
9babb374 BB |
292 | if (err == 0) { |
293 | err = dsl_dataset_hold_obj(dp, | |
d683ddbb | 294 | dsl_dataset_phys(ds)->ds_prev_snap_obj, dp, |
9babb374 BB |
295 | &dp->dp_origin_snap); |
296 | dsl_dataset_rele(ds, FTAG); | |
297 | } | |
13fe0198 | 298 | dsl_dir_rele(dd, dp); |
b128c09f BB |
299 | if (err) |
300 | goto out; | |
b128c09f BB |
301 | } |
302 | ||
9ae529ec | 303 | if (spa_version(dp->dp_spa) >= SPA_VERSION_DEADLISTS) { |
428870ff BB |
304 | err = dsl_pool_open_special_dir(dp, FREE_DIR_NAME, |
305 | &dp->dp_free_dir); | |
b128c09f BB |
306 | if (err) |
307 | goto out; | |
428870ff | 308 | |
b128c09f | 309 | err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, |
428870ff | 310 | DMU_POOL_FREE_BPOBJ, sizeof (uint64_t), 1, &obj); |
b128c09f BB |
311 | if (err) |
312 | goto out; | |
13fe0198 | 313 | VERIFY0(bpobj_open(&dp->dp_free_bpobj, |
428870ff | 314 | dp->dp_meta_objset, obj)); |
b128c09f BB |
315 | } |
316 | ||
a1d477c2 MA |
317 | if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_OBSOLETE_COUNTS)) { |
318 | err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, | |
319 | DMU_POOL_OBSOLETE_BPOBJ, sizeof (uint64_t), 1, &obj); | |
320 | if (err == 0) { | |
321 | VERIFY0(bpobj_open(&dp->dp_obsolete_bpobj, | |
322 | dp->dp_meta_objset, obj)); | |
323 | } else if (err == ENOENT) { | |
324 | /* | |
325 | * We might not have created the remap bpobj yet. | |
326 | */ | |
a1d477c2 MA |
327 | } else { |
328 | goto out; | |
329 | } | |
330 | } | |
331 | ||
fbeddd60 | 332 | /* |
a1d477c2 MA |
333 | * Note: errors ignored, because the these special dirs, used for |
334 | * space accounting, are only created on demand. | |
fbeddd60 MA |
335 | */ |
336 | (void) dsl_pool_open_special_dir(dp, LEAK_DIR_NAME, | |
337 | &dp->dp_leak_dir); | |
338 | ||
fa86b5db | 339 | if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_ASYNC_DESTROY)) { |
9ae529ec CS |
340 | err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, |
341 | DMU_POOL_BPTREE_OBJ, sizeof (uint64_t), 1, | |
342 | &dp->dp_bptree_obj); | |
343 | if (err != 0) | |
344 | goto out; | |
345 | } | |
346 | ||
fa86b5db | 347 | if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_EMPTY_BPOBJ)) { |
753c3839 MA |
348 | err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, |
349 | DMU_POOL_EMPTY_BPOBJ, sizeof (uint64_t), 1, | |
350 | &dp->dp_empty_bpobj); | |
351 | if (err != 0) | |
352 | goto out; | |
353 | } | |
354 | ||
428870ff BB |
355 | err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, |
356 | DMU_POOL_TMP_USERREFS, sizeof (uint64_t), 1, | |
357 | &dp->dp_tmp_userrefs_obj); | |
358 | if (err == ENOENT) | |
359 | err = 0; | |
360 | if (err) | |
361 | goto out; | |
362 | ||
9ae529ec | 363 | err = dsl_scan_init(dp, dp->dp_tx.tx_open_txg); |
428870ff | 364 | |
34dc7c2f | 365 | out: |
13fe0198 | 366 | rrw_exit(&dp->dp_config_rwlock, FTAG); |
34dc7c2f BB |
367 | return (err); |
368 | } | |
369 | ||
370 | void | |
371 | dsl_pool_close(dsl_pool_t *dp) | |
372 | { | |
b128c09f | 373 | /* |
e8b96c60 MA |
374 | * Drop our references from dsl_pool_open(). |
375 | * | |
b128c09f BB |
376 | * Since we held the origin_snap from "syncing" context (which |
377 | * includes pool-opening context), it actually only got a "ref" | |
378 | * and not a hold, so just drop that here. | |
379 | */ | |
a1d477c2 | 380 | if (dp->dp_origin_snap != NULL) |
13fe0198 | 381 | dsl_dataset_rele(dp->dp_origin_snap, dp); |
a1d477c2 | 382 | if (dp->dp_mos_dir != NULL) |
13fe0198 | 383 | dsl_dir_rele(dp->dp_mos_dir, dp); |
a1d477c2 | 384 | if (dp->dp_free_dir != NULL) |
13fe0198 | 385 | dsl_dir_rele(dp->dp_free_dir, dp); |
a1d477c2 | 386 | if (dp->dp_leak_dir != NULL) |
fbeddd60 | 387 | dsl_dir_rele(dp->dp_leak_dir, dp); |
a1d477c2 | 388 | if (dp->dp_root_dir != NULL) |
13fe0198 | 389 | dsl_dir_rele(dp->dp_root_dir, dp); |
34dc7c2f | 390 | |
428870ff | 391 | bpobj_close(&dp->dp_free_bpobj); |
a1d477c2 | 392 | bpobj_close(&dp->dp_obsolete_bpobj); |
428870ff | 393 | |
34dc7c2f | 394 | /* undo the dmu_objset_open_impl(mos) from dsl_pool_open() */ |
a1d477c2 | 395 | if (dp->dp_meta_objset != NULL) |
428870ff | 396 | dmu_objset_evict(dp->dp_meta_objset); |
34dc7c2f BB |
397 | |
398 | txg_list_destroy(&dp->dp_dirty_datasets); | |
29809a6c | 399 | txg_list_destroy(&dp->dp_dirty_zilogs); |
428870ff | 400 | txg_list_destroy(&dp->dp_sync_tasks); |
d2734cce | 401 | txg_list_destroy(&dp->dp_early_sync_tasks); |
34dc7c2f | 402 | txg_list_destroy(&dp->dp_dirty_dirs); |
34dc7c2f | 403 | |
a032ac4b | 404 | taskq_destroy(dp->dp_zil_clean_taskq); |
3bd4df38 | 405 | spa_sync_tq_destroy(dp->dp_spa); |
64fc7762 | 406 | |
ca0bf58d PS |
407 | /* |
408 | * We can't set retry to TRUE since we're explicitly specifying | |
409 | * a spa to flush. This is good enough; any missed buffers for | |
410 | * this spa won't cause trouble, and they'll eventually fall | |
411 | * out of the ARC just like any other unused buffer. | |
412 | */ | |
413 | arc_flush(dp->dp_spa, FALSE); | |
414 | ||
379ca9cf | 415 | mmp_fini(dp->dp_spa); |
34dc7c2f | 416 | txg_fini(dp); |
428870ff | 417 | dsl_scan_fini(dp); |
0c66c32d JG |
418 | dmu_buf_user_evict_wait(); |
419 | ||
13fe0198 | 420 | rrw_destroy(&dp->dp_config_rwlock); |
34dc7c2f | 421 | mutex_destroy(&dp->dp_lock); |
c17486b2 | 422 | cv_destroy(&dp->dp_spaceavail_cv); |
a7bd20e3 KJ |
423 | |
424 | ASSERT0(aggsum_value(&dp->dp_wrlog_total)); | |
425 | aggsum_fini(&dp->dp_wrlog_total); | |
426 | for (int i = 0; i < TXG_SIZE; i++) { | |
427 | ASSERT0(aggsum_value(&dp->dp_wrlog_pertxg[i])); | |
428 | aggsum_fini(&dp->dp_wrlog_pertxg[i]); | |
429 | } | |
430 | ||
dcec0a12 | 431 | taskq_destroy(dp->dp_unlinked_drain_taskq); |
657ce253 | 432 | taskq_destroy(dp->dp_zrele_taskq); |
82732299 | 433 | if (dp->dp_blkstats != NULL) |
79c76d5b | 434 | vmem_free(dp->dp_blkstats, sizeof (zfs_all_blkstats_t)); |
34dc7c2f BB |
435 | kmem_free(dp, sizeof (dsl_pool_t)); |
436 | } | |
437 | ||
a1d477c2 MA |
438 | void |
439 | dsl_pool_create_obsolete_bpobj(dsl_pool_t *dp, dmu_tx_t *tx) | |
440 | { | |
441 | uint64_t obj; | |
442 | /* | |
443 | * Currently, we only create the obsolete_bpobj where there are | |
444 | * indirect vdevs with referenced mappings. | |
445 | */ | |
446 | ASSERT(spa_feature_is_active(dp->dp_spa, SPA_FEATURE_DEVICE_REMOVAL)); | |
447 | /* create and open the obsolete_bpobj */ | |
448 | obj = bpobj_alloc(dp->dp_meta_objset, SPA_OLD_MAXBLOCKSIZE, tx); | |
449 | VERIFY0(bpobj_open(&dp->dp_obsolete_bpobj, dp->dp_meta_objset, obj)); | |
450 | VERIFY0(zap_add(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, | |
451 | DMU_POOL_OBSOLETE_BPOBJ, sizeof (uint64_t), 1, &obj, tx)); | |
452 | spa_feature_incr(dp->dp_spa, SPA_FEATURE_OBSOLETE_COUNTS, tx); | |
453 | } | |
454 | ||
455 | void | |
456 | dsl_pool_destroy_obsolete_bpobj(dsl_pool_t *dp, dmu_tx_t *tx) | |
457 | { | |
458 | spa_feature_decr(dp->dp_spa, SPA_FEATURE_OBSOLETE_COUNTS, tx); | |
459 | VERIFY0(zap_remove(dp->dp_meta_objset, | |
460 | DMU_POOL_DIRECTORY_OBJECT, | |
461 | DMU_POOL_OBSOLETE_BPOBJ, tx)); | |
462 | bpobj_free(dp->dp_meta_objset, | |
463 | dp->dp_obsolete_bpobj.bpo_object, tx); | |
464 | bpobj_close(&dp->dp_obsolete_bpobj); | |
465 | } | |
466 | ||
34dc7c2f | 467 | dsl_pool_t * |
14e4e3cb AZ |
468 | dsl_pool_create(spa_t *spa, nvlist_t *zplprops __attribute__((unused)), |
469 | dsl_crypto_params_t *dcp, uint64_t txg) | |
34dc7c2f BB |
470 | { |
471 | int err; | |
472 | dsl_pool_t *dp = dsl_pool_open_impl(spa, txg); | |
473 | dmu_tx_t *tx = dmu_tx_create_assigned(dp, txg); | |
0a108631 | 474 | #ifdef _KERNEL |
475 | objset_t *os; | |
476 | #else | |
477 | objset_t *os __attribute__((unused)); | |
478 | #endif | |
b128c09f | 479 | dsl_dataset_t *ds; |
428870ff | 480 | uint64_t obj; |
b128c09f | 481 | |
13fe0198 MA |
482 | rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG); |
483 | ||
b128c09f | 484 | /* create and open the MOS (meta-objset) */ |
428870ff BB |
485 | dp->dp_meta_objset = dmu_objset_create_impl(spa, |
486 | NULL, &dp->dp_meta_rootbp, DMU_OST_META, tx); | |
b5256303 | 487 | spa->spa_meta_objset = dp->dp_meta_objset; |
34dc7c2f BB |
488 | |
489 | /* create the pool directory */ | |
490 | err = zap_create_claim(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, | |
491 | DMU_OT_OBJECT_DIRECTORY, DMU_OT_NONE, 0, tx); | |
c99c9001 | 492 | ASSERT0(err); |
34dc7c2f | 493 | |
428870ff | 494 | /* Initialize scan structures */ |
13fe0198 | 495 | VERIFY0(dsl_scan_init(dp, txg)); |
428870ff | 496 | |
34dc7c2f | 497 | /* create and open the root dir */ |
b128c09f | 498 | dp->dp_root_dir_obj = dsl_dir_create_sync(dp, NULL, NULL, tx); |
13fe0198 | 499 | VERIFY0(dsl_dir_hold_obj(dp, dp->dp_root_dir_obj, |
34dc7c2f BB |
500 | NULL, dp, &dp->dp_root_dir)); |
501 | ||
502 | /* create and open the meta-objset dir */ | |
b128c09f | 503 | (void) dsl_dir_create_sync(dp, dp->dp_root_dir, MOS_DIR_NAME, tx); |
13fe0198 | 504 | VERIFY0(dsl_pool_open_special_dir(dp, |
b128c09f BB |
505 | MOS_DIR_NAME, &dp->dp_mos_dir)); |
506 | ||
428870ff BB |
507 | if (spa_version(spa) >= SPA_VERSION_DEADLISTS) { |
508 | /* create and open the free dir */ | |
509 | (void) dsl_dir_create_sync(dp, dp->dp_root_dir, | |
510 | FREE_DIR_NAME, tx); | |
13fe0198 | 511 | VERIFY0(dsl_pool_open_special_dir(dp, |
428870ff BB |
512 | FREE_DIR_NAME, &dp->dp_free_dir)); |
513 | ||
514 | /* create and open the free_bplist */ | |
f1512ee6 | 515 | obj = bpobj_alloc(dp->dp_meta_objset, SPA_OLD_MAXBLOCKSIZE, tx); |
428870ff BB |
516 | VERIFY(zap_add(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, |
517 | DMU_POOL_FREE_BPOBJ, sizeof (uint64_t), 1, &obj, tx) == 0); | |
13fe0198 | 518 | VERIFY0(bpobj_open(&dp->dp_free_bpobj, |
428870ff BB |
519 | dp->dp_meta_objset, obj)); |
520 | } | |
521 | ||
b128c09f BB |
522 | if (spa_version(spa) >= SPA_VERSION_DSL_SCRUB) |
523 | dsl_pool_create_origin(dp, tx); | |
524 | ||
b5256303 TC |
525 | /* |
526 | * Some features may be needed when creating the root dataset, so we | |
527 | * create the feature objects here. | |
528 | */ | |
529 | if (spa_version(spa) >= SPA_VERSION_FEATURES) | |
530 | spa_feature_create_zap_objects(spa, tx); | |
531 | ||
532 | if (dcp != NULL && dcp->cp_crypt != ZIO_CRYPT_OFF && | |
533 | dcp->cp_crypt != ZIO_CRYPT_INHERIT) | |
534 | spa_feature_enable(spa, SPA_FEATURE_ENCRYPTION, tx); | |
535 | ||
b128c09f | 536 | /* create the root dataset */ |
b5256303 | 537 | obj = dsl_dataset_create_sync_dd(dp->dp_root_dir, NULL, dcp, 0, tx); |
b128c09f BB |
538 | |
539 | /* create the root objset */ | |
52ce99dd TC |
540 | VERIFY0(dsl_dataset_hold_obj_flags(dp, obj, |
541 | DS_HOLD_FLAG_DECRYPT, FTAG, &ds)); | |
0a108631 | 542 | rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG); |
543 | os = dmu_objset_create_impl(dp->dp_spa, ds, | |
544 | dsl_dataset_get_blkptr(ds), DMU_OST_ZFS, tx); | |
545 | rrw_exit(&ds->ds_bp_rwlock, FTAG); | |
b128c09f | 546 | #ifdef _KERNEL |
0a108631 | 547 | zfs_create_fs(os, kcred, zplprops, tx); |
b128c09f | 548 | #endif |
52ce99dd | 549 | dsl_dataset_rele_flags(ds, DS_HOLD_FLAG_DECRYPT, FTAG); |
34dc7c2f BB |
550 | |
551 | dmu_tx_commit(tx); | |
552 | ||
13fe0198 MA |
553 | rrw_exit(&dp->dp_config_rwlock, FTAG); |
554 | ||
34dc7c2f BB |
555 | return (dp); |
556 | } | |
557 | ||
29809a6c MA |
558 | /* |
559 | * Account for the meta-objset space in its placeholder dsl_dir. | |
560 | */ | |
561 | void | |
562 | dsl_pool_mos_diduse_space(dsl_pool_t *dp, | |
563 | int64_t used, int64_t comp, int64_t uncomp) | |
564 | { | |
565 | ASSERT3U(comp, ==, uncomp); /* it's all metadata */ | |
566 | mutex_enter(&dp->dp_lock); | |
567 | dp->dp_mos_used_delta += used; | |
568 | dp->dp_mos_compressed_delta += comp; | |
569 | dp->dp_mos_uncompressed_delta += uncomp; | |
570 | mutex_exit(&dp->dp_lock); | |
571 | } | |
572 | ||
e8b96c60 MA |
573 | static void |
574 | dsl_pool_sync_mos(dsl_pool_t *dp, dmu_tx_t *tx) | |
575 | { | |
576 | zio_t *zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED); | |
577 | dmu_objset_sync(dp->dp_meta_objset, zio, tx); | |
578 | VERIFY0(zio_wait(zio)); | |
ba67d821 MA |
579 | dmu_objset_sync_done(dp->dp_meta_objset, tx); |
580 | taskq_wait(dp->dp_sync_taskq); | |
ffdf019c | 581 | multilist_destroy(&dp->dp_meta_objset->os_synced_dnodes); |
ba67d821 | 582 | |
e8b96c60 MA |
583 | dprintf_bp(&dp->dp_meta_rootbp, "meta objset rootbp is %s", ""); |
584 | spa_set_rootblkptr(dp->dp_spa, &dp->dp_meta_rootbp); | |
585 | } | |
586 | ||
587 | static void | |
588 | dsl_pool_dirty_delta(dsl_pool_t *dp, int64_t delta) | |
589 | { | |
590 | ASSERT(MUTEX_HELD(&dp->dp_lock)); | |
591 | ||
592 | if (delta < 0) | |
593 | ASSERT3U(-delta, <=, dp->dp_dirty_total); | |
594 | ||
595 | dp->dp_dirty_total += delta; | |
596 | ||
597 | /* | |
598 | * Note: we signal even when increasing dp_dirty_total. | |
599 | * This ensures forward progress -- each thread wakes the next waiter. | |
600 | */ | |
c0c8cc7b | 601 | if (dp->dp_dirty_total < zfs_dirty_data_max) |
e8b96c60 MA |
602 | cv_signal(&dp->dp_spaceavail_cv); |
603 | } | |
604 | ||
a7bd20e3 KJ |
605 | void |
606 | dsl_pool_wrlog_count(dsl_pool_t *dp, int64_t size, uint64_t txg) | |
607 | { | |
608 | ASSERT3S(size, >=, 0); | |
609 | ||
610 | aggsum_add(&dp->dp_wrlog_pertxg[txg & TXG_MASK], size); | |
611 | aggsum_add(&dp->dp_wrlog_total, size); | |
612 | ||
613 | /* Choose a value slightly bigger than min dirty sync bytes */ | |
614 | uint64_t sync_min = | |
84d0a03f | 615 | zfs_wrlog_data_max * (zfs_dirty_data_sync_percent + 10) / 200; |
a7bd20e3 KJ |
616 | if (aggsum_compare(&dp->dp_wrlog_pertxg[txg & TXG_MASK], sync_min) > 0) |
617 | txg_kick(dp, txg); | |
618 | } | |
619 | ||
620 | boolean_t | |
84d0a03f | 621 | dsl_pool_need_wrlog_delay(dsl_pool_t *dp) |
a7bd20e3 | 622 | { |
84d0a03f AM |
623 | uint64_t delay_min_bytes = |
624 | zfs_wrlog_data_max * zfs_delay_min_dirty_percent / 100; | |
625 | ||
626 | return (aggsum_compare(&dp->dp_wrlog_total, delay_min_bytes) > 0); | |
a7bd20e3 KJ |
627 | } |
628 | ||
629 | static void | |
630 | dsl_pool_wrlog_clear(dsl_pool_t *dp, uint64_t txg) | |
631 | { | |
632 | int64_t delta; | |
633 | delta = -(int64_t)aggsum_value(&dp->dp_wrlog_pertxg[txg & TXG_MASK]); | |
634 | aggsum_add(&dp->dp_wrlog_pertxg[txg & TXG_MASK], delta); | |
635 | aggsum_add(&dp->dp_wrlog_total, delta); | |
84d0a03f AM |
636 | /* Compact per-CPU sums after the big change. */ |
637 | (void) aggsum_value(&dp->dp_wrlog_pertxg[txg & TXG_MASK]); | |
638 | (void) aggsum_value(&dp->dp_wrlog_total); | |
a7bd20e3 KJ |
639 | } |
640 | ||
d2734cce SD |
641 | #ifdef ZFS_DEBUG |
642 | static boolean_t | |
643 | dsl_early_sync_task_verify(dsl_pool_t *dp, uint64_t txg) | |
644 | { | |
645 | spa_t *spa = dp->dp_spa; | |
646 | vdev_t *rvd = spa->spa_root_vdev; | |
647 | ||
648 | for (uint64_t c = 0; c < rvd->vdev_children; c++) { | |
649 | vdev_t *vd = rvd->vdev_child[c]; | |
650 | txg_list_t *tl = &vd->vdev_ms_list; | |
651 | metaslab_t *ms; | |
652 | ||
653 | for (ms = txg_list_head(tl, TXG_CLEAN(txg)); ms; | |
654 | ms = txg_list_next(tl, ms, TXG_CLEAN(txg))) { | |
655 | VERIFY(range_tree_is_empty(ms->ms_freeing)); | |
656 | VERIFY(range_tree_is_empty(ms->ms_checkpointing)); | |
657 | } | |
658 | } | |
659 | ||
660 | return (B_TRUE); | |
661 | } | |
89495a42 AZ |
662 | #else |
663 | #define dsl_early_sync_task_verify(dp, txg) \ | |
664 | ((void) sizeof (dp), (void) sizeof (txg), B_TRUE) | |
d2734cce SD |
665 | #endif |
666 | ||
34dc7c2f BB |
667 | void |
668 | dsl_pool_sync(dsl_pool_t *dp, uint64_t txg) | |
669 | { | |
3bd4df38 | 670 | zio_t *rio; /* root zio for all dirty dataset syncs */ |
34dc7c2f BB |
671 | dmu_tx_t *tx; |
672 | dsl_dir_t *dd; | |
673 | dsl_dataset_t *ds; | |
428870ff | 674 | objset_t *mos = dp->dp_meta_objset; |
29809a6c MA |
675 | list_t synced_datasets; |
676 | ||
677 | list_create(&synced_datasets, sizeof (dsl_dataset_t), | |
678 | offsetof(dsl_dataset_t, ds_synced_link)); | |
34dc7c2f BB |
679 | |
680 | tx = dmu_tx_create_assigned(dp, txg); | |
681 | ||
d2734cce SD |
682 | /* |
683 | * Run all early sync tasks before writing out any dirty blocks. | |
684 | * For more info on early sync tasks see block comment in | |
685 | * dsl_early_sync_task(). | |
686 | */ | |
687 | if (!txg_list_empty(&dp->dp_early_sync_tasks, txg)) { | |
688 | dsl_sync_task_t *dst; | |
689 | ||
690 | ASSERT3U(spa_sync_pass(dp->dp_spa), ==, 1); | |
691 | while ((dst = | |
692 | txg_list_remove(&dp->dp_early_sync_tasks, txg)) != NULL) { | |
693 | ASSERT(dsl_early_sync_task_verify(dp, txg)); | |
694 | dsl_sync_task_sync(dst, tx); | |
695 | } | |
696 | ASSERT(dsl_early_sync_task_verify(dp, txg)); | |
697 | } | |
698 | ||
e8b96c60 | 699 | /* |
3bd4df38 EN |
700 | * Write out all dirty blocks of dirty datasets. Note, this could |
701 | * create a very large (+10k) zio tree. | |
e8b96c60 | 702 | */ |
3bd4df38 | 703 | rio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED); |
e8b96c60 | 704 | while ((ds = txg_list_remove(&dp->dp_dirty_datasets, txg)) != NULL) { |
9babb374 BB |
705 | /* |
706 | * We must not sync any non-MOS datasets twice, because | |
707 | * we may have taken a snapshot of them. However, we | |
708 | * may sync newly-created datasets on pass 2. | |
709 | */ | |
710 | ASSERT(!list_link_active(&ds->ds_synced_link)); | |
29809a6c | 711 | list_insert_tail(&synced_datasets, ds); |
3bd4df38 | 712 | dsl_dataset_sync(ds, rio, tx); |
34dc7c2f | 713 | } |
3bd4df38 | 714 | VERIFY0(zio_wait(rio)); |
9babb374 | 715 | |
539d33c7 GM |
716 | /* |
717 | * Update the long range free counter after | |
718 | * we're done syncing user data | |
719 | */ | |
720 | mutex_enter(&dp->dp_lock); | |
721 | ASSERT(spa_sync_pass(dp->dp_spa) == 1 || | |
722 | dp->dp_long_free_dirty_pertxg[txg & TXG_MASK] == 0); | |
723 | dp->dp_long_free_dirty_pertxg[txg & TXG_MASK] = 0; | |
724 | mutex_exit(&dp->dp_lock); | |
725 | ||
29809a6c MA |
726 | /* |
727 | * After the data blocks have been written (ensured by the zio_wait() | |
9c5167d1 | 728 | * above), update the user/group/project space accounting. This happens |
64fc7762 MA |
729 | * in tasks dispatched to dp_sync_taskq, so wait for them before |
730 | * continuing. | |
29809a6c | 731 | */ |
e8b96c60 MA |
732 | for (ds = list_head(&synced_datasets); ds != NULL; |
733 | ds = list_next(&synced_datasets, ds)) { | |
ba67d821 | 734 | dmu_objset_sync_done(ds->ds_objset, tx); |
e8b96c60 | 735 | } |
64fc7762 | 736 | taskq_wait(dp->dp_sync_taskq); |
9babb374 BB |
737 | |
738 | /* | |
739 | * Sync the datasets again to push out the changes due to | |
428870ff | 740 | * userspace updates. This must be done before we process the |
29809a6c MA |
741 | * sync tasks, so that any snapshots will have the correct |
742 | * user accounting information (and we won't get confused | |
743 | * about which blocks are part of the snapshot). | |
9babb374 | 744 | */ |
3bd4df38 | 745 | rio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED); |
e8b96c60 | 746 | while ((ds = txg_list_remove(&dp->dp_dirty_datasets, txg)) != NULL) { |
52ce99dd TC |
747 | objset_t *os = ds->ds_objset; |
748 | ||
9babb374 BB |
749 | ASSERT(list_link_active(&ds->ds_synced_link)); |
750 | dmu_buf_rele(ds->ds_dbuf, ds); | |
3bd4df38 | 751 | dsl_dataset_sync(ds, rio, tx); |
52ce99dd TC |
752 | |
753 | /* | |
754 | * Release any key mappings created by calls to | |
755 | * dsl_dataset_dirty() from the userquota accounting | |
756 | * code paths. | |
757 | */ | |
758 | if (os->os_encrypted && !os->os_raw_receive && | |
759 | !os->os_next_write_raw[txg & TXG_MASK]) { | |
760 | ASSERT3P(ds->ds_key_mapping, !=, NULL); | |
761 | key_mapping_rele(dp->dp_spa, ds->ds_key_mapping, ds); | |
762 | } | |
9babb374 | 763 | } |
3bd4df38 | 764 | VERIFY0(zio_wait(rio)); |
9babb374 | 765 | |
428870ff | 766 | /* |
29809a6c MA |
767 | * Now that the datasets have been completely synced, we can |
768 | * clean up our in-memory structures accumulated while syncing: | |
769 | * | |
37f03da8 SH |
770 | * - move dead blocks from the pending deadlist and livelists |
771 | * to the on-disk versions | |
29809a6c | 772 | * - release hold from dsl_dataset_dirty() |
52ce99dd | 773 | * - release key mapping hold from dsl_dataset_dirty() |
428870ff | 774 | */ |
e8b96c60 | 775 | while ((ds = list_remove_head(&synced_datasets)) != NULL) { |
52ce99dd TC |
776 | objset_t *os = ds->ds_objset; |
777 | ||
778 | if (os->os_encrypted && !os->os_raw_receive && | |
779 | !os->os_next_write_raw[txg & TXG_MASK]) { | |
780 | ASSERT3P(ds->ds_key_mapping, !=, NULL); | |
781 | key_mapping_rele(dp->dp_spa, ds->ds_key_mapping, ds); | |
782 | } | |
783 | ||
0efd9791 | 784 | dsl_dataset_sync_done(ds, tx); |
d816bc5e | 785 | dmu_buf_rele(ds->ds_dbuf, ds); |
428870ff BB |
786 | } |
787 | ||
e8b96c60 | 788 | while ((dd = txg_list_remove(&dp->dp_dirty_dirs, txg)) != NULL) { |
34dc7c2f | 789 | dsl_dir_sync(dd, tx); |
e8b96c60 | 790 | } |
b128c09f | 791 | |
29809a6c MA |
792 | /* |
793 | * The MOS's space is accounted for in the pool/$MOS | |
794 | * (dp_mos_dir). We can't modify the mos while we're syncing | |
795 | * it, so we remember the deltas and apply them here. | |
796 | */ | |
797 | if (dp->dp_mos_used_delta != 0 || dp->dp_mos_compressed_delta != 0 || | |
798 | dp->dp_mos_uncompressed_delta != 0) { | |
799 | dsl_dir_diduse_space(dp->dp_mos_dir, DD_USED_HEAD, | |
800 | dp->dp_mos_used_delta, | |
801 | dp->dp_mos_compressed_delta, | |
802 | dp->dp_mos_uncompressed_delta, tx); | |
803 | dp->dp_mos_used_delta = 0; | |
804 | dp->dp_mos_compressed_delta = 0; | |
805 | dp->dp_mos_uncompressed_delta = 0; | |
806 | } | |
807 | ||
93e28d66 | 808 | if (dmu_objset_is_dirty(mos, txg)) { |
e8b96c60 | 809 | dsl_pool_sync_mos(dp, tx); |
34dc7c2f BB |
810 | } |
811 | ||
0f8ff49e SD |
812 | /* |
813 | * We have written all of the accounted dirty data, so our | |
814 | * dp_space_towrite should now be zero. However, some seldom-used | |
815 | * code paths do not adhere to this (e.g. dbuf_undirty()). Shore up | |
816 | * the accounting of any dirtied space now. | |
817 | * | |
818 | * Note that, besides any dirty data from datasets, the amount of | |
819 | * dirty data in the MOS is also accounted by the pool. Therefore, | |
820 | * we want to do this cleanup after dsl_pool_sync_mos() so we don't | |
821 | * attempt to update the accounting for the same dirty data twice. | |
822 | * (i.e. at this point we only update the accounting for the space | |
823 | * that we know that we "leaked"). | |
824 | */ | |
825 | dsl_pool_undirty_space(dp, dp->dp_dirty_pertxg[txg & TXG_MASK], txg); | |
826 | ||
29809a6c MA |
827 | /* |
828 | * If we modify a dataset in the same txg that we want to destroy it, | |
829 | * its dsl_dir's dd_dbuf will be dirty, and thus have a hold on it. | |
830 | * dsl_dir_destroy_check() will fail if there are unexpected holds. | |
831 | * Therefore, we want to sync the MOS (thus syncing the dd_dbuf | |
832 | * and clearing the hold on it) before we process the sync_tasks. | |
833 | * The MOS data dirtied by the sync_tasks will be synced on the next | |
834 | * pass. | |
835 | */ | |
29809a6c | 836 | if (!txg_list_empty(&dp->dp_sync_tasks, txg)) { |
13fe0198 | 837 | dsl_sync_task_t *dst; |
29809a6c MA |
838 | /* |
839 | * No more sync tasks should have been added while we | |
840 | * were syncing. | |
841 | */ | |
e8b96c60 MA |
842 | ASSERT3U(spa_sync_pass(dp->dp_spa), ==, 1); |
843 | while ((dst = txg_list_remove(&dp->dp_sync_tasks, txg)) != NULL) | |
13fe0198 | 844 | dsl_sync_task_sync(dst, tx); |
29809a6c MA |
845 | } |
846 | ||
34dc7c2f | 847 | dmu_tx_commit(tx); |
b128c09f | 848 | |
e8b96c60 | 849 | DTRACE_PROBE2(dsl_pool_sync__done, dsl_pool_t *dp, dp, uint64_t, txg); |
34dc7c2f BB |
850 | } |
851 | ||
852 | void | |
428870ff | 853 | dsl_pool_sync_done(dsl_pool_t *dp, uint64_t txg) |
34dc7c2f | 854 | { |
29809a6c | 855 | zilog_t *zilog; |
34dc7c2f | 856 | |
55922e73 | 857 | while ((zilog = txg_list_head(&dp->dp_dirty_zilogs, txg))) { |
e8b96c60 | 858 | dsl_dataset_t *ds = dmu_objset_ds(zilog->zl_os); |
55922e73 GW |
859 | /* |
860 | * We don't remove the zilog from the dp_dirty_zilogs | |
861 | * list until after we've cleaned it. This ensures that | |
862 | * callers of zilog_is_dirty() receive an accurate | |
863 | * answer when they are racing with the spa sync thread. | |
864 | */ | |
29809a6c | 865 | zil_clean(zilog, txg); |
55922e73 | 866 | (void) txg_list_remove_this(&dp->dp_dirty_zilogs, zilog, txg); |
29809a6c MA |
867 | ASSERT(!dmu_objset_is_dirty(zilog->zl_os, txg)); |
868 | dmu_buf_rele(ds->ds_dbuf, zilog); | |
34dc7c2f | 869 | } |
a7bd20e3 KJ |
870 | |
871 | dsl_pool_wrlog_clear(dp, txg); | |
872 | ||
428870ff | 873 | ASSERT(!dmu_objset_is_dirty(dp->dp_meta_objset, txg)); |
34dc7c2f BB |
874 | } |
875 | ||
876 | /* | |
877 | * TRUE if the current thread is the tx_sync_thread or if we | |
878 | * are being called from SPA context during pool initialization. | |
879 | */ | |
880 | int | |
881 | dsl_pool_sync_context(dsl_pool_t *dp) | |
882 | { | |
883 | return (curthread == dp->dp_tx.tx_sync_thread || | |
64fc7762 MA |
884 | spa_is_initializing(dp->dp_spa) || |
885 | taskq_member(dp->dp_sync_taskq, curthread)); | |
34dc7c2f BB |
886 | } |
887 | ||
d2734cce SD |
888 | /* |
889 | * This function returns the amount of allocatable space in the pool | |
890 | * minus whatever space is currently reserved by ZFS for specific | |
891 | * purposes. Specifically: | |
892 | * | |
893 | * 1] Any reserved SLOP space | |
894 | * 2] Any space used by the checkpoint | |
895 | * 3] Any space used for deferred frees | |
896 | * | |
897 | * The latter 2 are especially important because they are needed to | |
898 | * rectify the SPA's and DMU's different understanding of how much space | |
899 | * is used. Now the DMU is aware of that extra space tracked by the SPA | |
900 | * without having to maintain a separate special dir (e.g similar to | |
901 | * $MOS, $FREEING, and $LEAKED). | |
902 | * | |
903 | * Note: By deferred frees here, we mean the frees that were deferred | |
904 | * in spa_sync() after sync pass 1 (spa_deferred_bpobj), and not the | |
905 | * segments placed in ms_defer trees during metaslab_sync_done(). | |
906 | */ | |
34dc7c2f | 907 | uint64_t |
d2734cce | 908 | dsl_pool_adjustedsize(dsl_pool_t *dp, zfs_space_check_t slop_policy) |
34dc7c2f | 909 | { |
d2734cce SD |
910 | spa_t *spa = dp->dp_spa; |
911 | uint64_t space, resv, adjustedsize; | |
912 | uint64_t spa_deferred_frees = | |
913 | spa->spa_deferred_bpobj.bpo_phys->bpo_bytes; | |
914 | ||
915 | space = spa_get_dspace(spa) | |
916 | - spa_get_checkpoint_space(spa) - spa_deferred_frees; | |
917 | resv = spa_get_slop_space(spa); | |
918 | ||
919 | switch (slop_policy) { | |
920 | case ZFS_SPACE_CHECK_NORMAL: | |
921 | break; | |
922 | case ZFS_SPACE_CHECK_RESERVED: | |
34dc7c2f | 923 | resv >>= 1; |
d2734cce SD |
924 | break; |
925 | case ZFS_SPACE_CHECK_EXTRA_RESERVED: | |
926 | resv >>= 2; | |
927 | break; | |
928 | case ZFS_SPACE_CHECK_NONE: | |
929 | resv = 0; | |
930 | break; | |
931 | default: | |
932 | panic("invalid slop policy value: %d", slop_policy); | |
933 | break; | |
934 | } | |
935 | adjustedsize = (space >= resv) ? (space - resv) : 0; | |
34dc7c2f | 936 | |
d2734cce SD |
937 | return (adjustedsize); |
938 | } | |
939 | ||
940 | uint64_t | |
941 | dsl_pool_unreserved_space(dsl_pool_t *dp, zfs_space_check_t slop_policy) | |
942 | { | |
943 | uint64_t poolsize = dsl_pool_adjustedsize(dp, slop_policy); | |
944 | uint64_t deferred = | |
945 | metaslab_class_get_deferred(spa_normal_class(dp->dp_spa)); | |
946 | uint64_t quota = (poolsize >= deferred) ? (poolsize - deferred) : 0; | |
947 | return (quota); | |
34dc7c2f BB |
948 | } |
949 | ||
6df43169 BB |
950 | uint64_t |
951 | dsl_pool_deferred_space(dsl_pool_t *dp) | |
952 | { | |
953 | return (metaslab_class_get_deferred(spa_normal_class(dp->dp_spa))); | |
954 | } | |
955 | ||
e8b96c60 MA |
956 | boolean_t |
957 | dsl_pool_need_dirty_delay(dsl_pool_t *dp) | |
34dc7c2f | 958 | { |
e8b96c60 MA |
959 | uint64_t delay_min_bytes = |
960 | zfs_dirty_data_max * zfs_delay_min_dirty_percent / 100; | |
34dc7c2f | 961 | |
4fbc5249 AM |
962 | /* |
963 | * We are not taking the dp_lock here and few other places, since torn | |
964 | * reads are unlikely: on 64-bit systems due to register size and on | |
965 | * 32-bit due to memory constraints. Pool-wide locks in hot path may | |
966 | * be too expensive, while we do not need a precise result here. | |
967 | */ | |
968 | return (dp->dp_dirty_total > delay_min_bytes); | |
34dc7c2f BB |
969 | } |
970 | ||
50e09edd KJ |
971 | static boolean_t |
972 | dsl_pool_need_dirty_sync(dsl_pool_t *dp, uint64_t txg) | |
973 | { | |
50e09edd KJ |
974 | uint64_t dirty_min_bytes = |
975 | zfs_dirty_data_max * zfs_dirty_data_sync_percent / 100; | |
976 | uint64_t dirty = dp->dp_dirty_pertxg[txg & TXG_MASK]; | |
977 | ||
978 | return (dirty > dirty_min_bytes); | |
979 | } | |
980 | ||
34dc7c2f | 981 | void |
e8b96c60 | 982 | dsl_pool_dirty_space(dsl_pool_t *dp, int64_t space, dmu_tx_t *tx) |
34dc7c2f | 983 | { |
e8b96c60 MA |
984 | if (space > 0) { |
985 | mutex_enter(&dp->dp_lock); | |
986 | dp->dp_dirty_pertxg[tx->tx_txg & TXG_MASK] += space; | |
987 | dsl_pool_dirty_delta(dp, space); | |
50e09edd KJ |
988 | boolean_t needsync = !dmu_tx_is_syncing(tx) && |
989 | dsl_pool_need_dirty_sync(dp, tx->tx_txg); | |
e8b96c60 | 990 | mutex_exit(&dp->dp_lock); |
50e09edd KJ |
991 | |
992 | if (needsync) | |
993 | txg_kick(dp, tx->tx_txg); | |
e8b96c60 | 994 | } |
34dc7c2f BB |
995 | } |
996 | ||
997 | void | |
e8b96c60 | 998 | dsl_pool_undirty_space(dsl_pool_t *dp, int64_t space, uint64_t txg) |
34dc7c2f | 999 | { |
e8b96c60 MA |
1000 | ASSERT3S(space, >=, 0); |
1001 | if (space == 0) | |
34dc7c2f BB |
1002 | return; |
1003 | ||
e8b96c60 MA |
1004 | mutex_enter(&dp->dp_lock); |
1005 | if (dp->dp_dirty_pertxg[txg & TXG_MASK] < space) { | |
1006 | /* XXX writing something we didn't dirty? */ | |
1007 | space = dp->dp_dirty_pertxg[txg & TXG_MASK]; | |
34dc7c2f | 1008 | } |
e8b96c60 MA |
1009 | ASSERT3U(dp->dp_dirty_pertxg[txg & TXG_MASK], >=, space); |
1010 | dp->dp_dirty_pertxg[txg & TXG_MASK] -= space; | |
1011 | ASSERT3U(dp->dp_dirty_total, >=, space); | |
1012 | dsl_pool_dirty_delta(dp, -space); | |
1013 | mutex_exit(&dp->dp_lock); | |
34dc7c2f | 1014 | } |
b128c09f | 1015 | |
b128c09f | 1016 | static int |
13fe0198 | 1017 | upgrade_clones_cb(dsl_pool_t *dp, dsl_dataset_t *hds, void *arg) |
b128c09f BB |
1018 | { |
1019 | dmu_tx_t *tx = arg; | |
1020 | dsl_dataset_t *ds, *prev = NULL; | |
1021 | int err; | |
b128c09f | 1022 | |
13fe0198 | 1023 | err = dsl_dataset_hold_obj(dp, hds->ds_object, FTAG, &ds); |
b128c09f BB |
1024 | if (err) |
1025 | return (err); | |
1026 | ||
d683ddbb JG |
1027 | while (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) { |
1028 | err = dsl_dataset_hold_obj(dp, | |
1029 | dsl_dataset_phys(ds)->ds_prev_snap_obj, FTAG, &prev); | |
b128c09f BB |
1030 | if (err) { |
1031 | dsl_dataset_rele(ds, FTAG); | |
1032 | return (err); | |
1033 | } | |
1034 | ||
d683ddbb | 1035 | if (dsl_dataset_phys(prev)->ds_next_snap_obj != ds->ds_object) |
b128c09f BB |
1036 | break; |
1037 | dsl_dataset_rele(ds, FTAG); | |
1038 | ds = prev; | |
1039 | prev = NULL; | |
1040 | } | |
1041 | ||
1042 | if (prev == NULL) { | |
1043 | prev = dp->dp_origin_snap; | |
1044 | ||
1045 | /* | |
1046 | * The $ORIGIN can't have any data, or the accounting | |
1047 | * will be wrong. | |
1048 | */ | |
cc9bb3e5 | 1049 | rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG); |
d683ddbb | 1050 | ASSERT0(dsl_dataset_phys(prev)->ds_bp.blk_birth); |
cc9bb3e5 | 1051 | rrw_exit(&ds->ds_bp_rwlock, FTAG); |
b128c09f BB |
1052 | |
1053 | /* The origin doesn't get attached to itself */ | |
1054 | if (ds->ds_object == prev->ds_object) { | |
1055 | dsl_dataset_rele(ds, FTAG); | |
1056 | return (0); | |
1057 | } | |
1058 | ||
1059 | dmu_buf_will_dirty(ds->ds_dbuf, tx); | |
d683ddbb JG |
1060 | dsl_dataset_phys(ds)->ds_prev_snap_obj = prev->ds_object; |
1061 | dsl_dataset_phys(ds)->ds_prev_snap_txg = | |
1062 | dsl_dataset_phys(prev)->ds_creation_txg; | |
b128c09f BB |
1063 | |
1064 | dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx); | |
d683ddbb | 1065 | dsl_dir_phys(ds->ds_dir)->dd_origin_obj = prev->ds_object; |
b128c09f BB |
1066 | |
1067 | dmu_buf_will_dirty(prev->ds_dbuf, tx); | |
d683ddbb | 1068 | dsl_dataset_phys(prev)->ds_num_children++; |
b128c09f | 1069 | |
d683ddbb | 1070 | if (dsl_dataset_phys(ds)->ds_next_snap_obj == 0) { |
b128c09f | 1071 | ASSERT(ds->ds_prev == NULL); |
13fe0198 | 1072 | VERIFY0(dsl_dataset_hold_obj(dp, |
d683ddbb JG |
1073 | dsl_dataset_phys(ds)->ds_prev_snap_obj, |
1074 | ds, &ds->ds_prev)); | |
b128c09f BB |
1075 | } |
1076 | } | |
1077 | ||
d683ddbb JG |
1078 | ASSERT3U(dsl_dir_phys(ds->ds_dir)->dd_origin_obj, ==, prev->ds_object); |
1079 | ASSERT3U(dsl_dataset_phys(ds)->ds_prev_snap_obj, ==, prev->ds_object); | |
b128c09f | 1080 | |
d683ddbb | 1081 | if (dsl_dataset_phys(prev)->ds_next_clones_obj == 0) { |
428870ff | 1082 | dmu_buf_will_dirty(prev->ds_dbuf, tx); |
d683ddbb | 1083 | dsl_dataset_phys(prev)->ds_next_clones_obj = |
b128c09f BB |
1084 | zap_create(dp->dp_meta_objset, |
1085 | DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx); | |
1086 | } | |
13fe0198 | 1087 | VERIFY0(zap_add_int(dp->dp_meta_objset, |
d683ddbb | 1088 | dsl_dataset_phys(prev)->ds_next_clones_obj, ds->ds_object, tx)); |
b128c09f BB |
1089 | |
1090 | dsl_dataset_rele(ds, FTAG); | |
1091 | if (prev != dp->dp_origin_snap) | |
1092 | dsl_dataset_rele(prev, FTAG); | |
1093 | return (0); | |
1094 | } | |
1095 | ||
1096 | void | |
1097 | dsl_pool_upgrade_clones(dsl_pool_t *dp, dmu_tx_t *tx) | |
1098 | { | |
1099 | ASSERT(dmu_tx_is_syncing(tx)); | |
1100 | ASSERT(dp->dp_origin_snap != NULL); | |
1101 | ||
13fe0198 | 1102 | VERIFY0(dmu_objset_find_dp(dp, dp->dp_root_dir_obj, upgrade_clones_cb, |
9c43027b | 1103 | tx, DS_FIND_CHILDREN | DS_FIND_SERIALIZE)); |
428870ff BB |
1104 | } |
1105 | ||
428870ff | 1106 | static int |
13fe0198 | 1107 | upgrade_dir_clones_cb(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg) |
428870ff BB |
1108 | { |
1109 | dmu_tx_t *tx = arg; | |
428870ff BB |
1110 | objset_t *mos = dp->dp_meta_objset; |
1111 | ||
d683ddbb | 1112 | if (dsl_dir_phys(ds->ds_dir)->dd_origin_obj != 0) { |
428870ff BB |
1113 | dsl_dataset_t *origin; |
1114 | ||
13fe0198 | 1115 | VERIFY0(dsl_dataset_hold_obj(dp, |
d683ddbb | 1116 | dsl_dir_phys(ds->ds_dir)->dd_origin_obj, FTAG, &origin)); |
428870ff | 1117 | |
d683ddbb | 1118 | if (dsl_dir_phys(origin->ds_dir)->dd_clones == 0) { |
428870ff | 1119 | dmu_buf_will_dirty(origin->ds_dir->dd_dbuf, tx); |
d683ddbb JG |
1120 | dsl_dir_phys(origin->ds_dir)->dd_clones = |
1121 | zap_create(mos, DMU_OT_DSL_CLONES, DMU_OT_NONE, | |
1122 | 0, tx); | |
428870ff BB |
1123 | } |
1124 | ||
13fe0198 | 1125 | VERIFY0(zap_add_int(dp->dp_meta_objset, |
d683ddbb JG |
1126 | dsl_dir_phys(origin->ds_dir)->dd_clones, |
1127 | ds->ds_object, tx)); | |
428870ff BB |
1128 | |
1129 | dsl_dataset_rele(origin, FTAG); | |
1130 | } | |
428870ff BB |
1131 | return (0); |
1132 | } | |
1133 | ||
1134 | void | |
1135 | dsl_pool_upgrade_dir_clones(dsl_pool_t *dp, dmu_tx_t *tx) | |
1136 | { | |
428870ff BB |
1137 | uint64_t obj; |
1138 | ||
d6320ddb BB |
1139 | ASSERT(dmu_tx_is_syncing(tx)); |
1140 | ||
428870ff | 1141 | (void) dsl_dir_create_sync(dp, dp->dp_root_dir, FREE_DIR_NAME, tx); |
13fe0198 | 1142 | VERIFY0(dsl_pool_open_special_dir(dp, |
428870ff BB |
1143 | FREE_DIR_NAME, &dp->dp_free_dir)); |
1144 | ||
1145 | /* | |
1146 | * We can't use bpobj_alloc(), because spa_version() still | |
1147 | * returns the old version, and we need a new-version bpobj with | |
1148 | * subobj support. So call dmu_object_alloc() directly. | |
1149 | */ | |
1150 | obj = dmu_object_alloc(dp->dp_meta_objset, DMU_OT_BPOBJ, | |
f1512ee6 | 1151 | SPA_OLD_MAXBLOCKSIZE, DMU_OT_BPOBJ_HDR, sizeof (bpobj_phys_t), tx); |
13fe0198 | 1152 | VERIFY0(zap_add(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, |
428870ff | 1153 | DMU_POOL_FREE_BPOBJ, sizeof (uint64_t), 1, &obj, tx)); |
13fe0198 | 1154 | VERIFY0(bpobj_open(&dp->dp_free_bpobj, dp->dp_meta_objset, obj)); |
428870ff | 1155 | |
13fe0198 | 1156 | VERIFY0(dmu_objset_find_dp(dp, dp->dp_root_dir_obj, |
9c43027b | 1157 | upgrade_dir_clones_cb, tx, DS_FIND_CHILDREN | DS_FIND_SERIALIZE)); |
b128c09f BB |
1158 | } |
1159 | ||
1160 | void | |
1161 | dsl_pool_create_origin(dsl_pool_t *dp, dmu_tx_t *tx) | |
1162 | { | |
1163 | uint64_t dsobj; | |
1164 | dsl_dataset_t *ds; | |
1165 | ||
1166 | ASSERT(dmu_tx_is_syncing(tx)); | |
1167 | ASSERT(dp->dp_origin_snap == NULL); | |
13fe0198 | 1168 | ASSERT(rrw_held(&dp->dp_config_rwlock, RW_WRITER)); |
b128c09f BB |
1169 | |
1170 | /* create the origin dir, ds, & snap-ds */ | |
b128c09f | 1171 | dsobj = dsl_dataset_create_sync(dp->dp_root_dir, ORIGIN_DIR_NAME, |
b5256303 | 1172 | NULL, 0, kcred, NULL, tx); |
13fe0198 MA |
1173 | VERIFY0(dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds)); |
1174 | dsl_dataset_snapshot_sync_impl(ds, ORIGIN_DIR_NAME, tx); | |
d683ddbb | 1175 | VERIFY0(dsl_dataset_hold_obj(dp, dsl_dataset_phys(ds)->ds_prev_snap_obj, |
b128c09f BB |
1176 | dp, &dp->dp_origin_snap)); |
1177 | dsl_dataset_rele(ds, FTAG); | |
b128c09f | 1178 | } |
9babb374 BB |
1179 | |
1180 | taskq_t * | |
657ce253 | 1181 | dsl_pool_zrele_taskq(dsl_pool_t *dp) |
9babb374 | 1182 | { |
657ce253 | 1183 | return (dp->dp_zrele_taskq); |
9babb374 | 1184 | } |
428870ff | 1185 | |
dcec0a12 AP |
1186 | taskq_t * |
1187 | dsl_pool_unlinked_drain_taskq(dsl_pool_t *dp) | |
1188 | { | |
1189 | return (dp->dp_unlinked_drain_taskq); | |
1190 | } | |
1191 | ||
428870ff BB |
1192 | /* |
1193 | * Walk through the pool-wide zap object of temporary snapshot user holds | |
1194 | * and release them. | |
1195 | */ | |
1196 | void | |
1197 | dsl_pool_clean_tmp_userrefs(dsl_pool_t *dp) | |
1198 | { | |
1199 | zap_attribute_t za; | |
1200 | zap_cursor_t zc; | |
1201 | objset_t *mos = dp->dp_meta_objset; | |
1202 | uint64_t zapobj = dp->dp_tmp_userrefs_obj; | |
95fd54a1 | 1203 | nvlist_t *holds; |
428870ff BB |
1204 | |
1205 | if (zapobj == 0) | |
1206 | return; | |
1207 | ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS); | |
1208 | ||
95fd54a1 SH |
1209 | holds = fnvlist_alloc(); |
1210 | ||
428870ff BB |
1211 | for (zap_cursor_init(&zc, mos, zapobj); |
1212 | zap_cursor_retrieve(&zc, &za) == 0; | |
1213 | zap_cursor_advance(&zc)) { | |
1214 | char *htag; | |
95fd54a1 | 1215 | nvlist_t *tags; |
428870ff BB |
1216 | |
1217 | htag = strchr(za.za_name, '-'); | |
1218 | *htag = '\0'; | |
1219 | ++htag; | |
95fd54a1 SH |
1220 | if (nvlist_lookup_nvlist(holds, za.za_name, &tags) != 0) { |
1221 | tags = fnvlist_alloc(); | |
1222 | fnvlist_add_boolean(tags, htag); | |
1223 | fnvlist_add_nvlist(holds, za.za_name, tags); | |
1224 | fnvlist_free(tags); | |
1225 | } else { | |
1226 | fnvlist_add_boolean(tags, htag); | |
1227 | } | |
428870ff | 1228 | } |
95fd54a1 SH |
1229 | dsl_dataset_user_release_tmp(dp, holds); |
1230 | fnvlist_free(holds); | |
428870ff BB |
1231 | zap_cursor_fini(&zc); |
1232 | } | |
1233 | ||
1234 | /* | |
1235 | * Create the pool-wide zap object for storing temporary snapshot holds. | |
1236 | */ | |
65c7cc49 | 1237 | static void |
428870ff BB |
1238 | dsl_pool_user_hold_create_obj(dsl_pool_t *dp, dmu_tx_t *tx) |
1239 | { | |
1240 | objset_t *mos = dp->dp_meta_objset; | |
1241 | ||
1242 | ASSERT(dp->dp_tmp_userrefs_obj == 0); | |
1243 | ASSERT(dmu_tx_is_syncing(tx)); | |
1244 | ||
9ae529ec CS |
1245 | dp->dp_tmp_userrefs_obj = zap_create_link(mos, DMU_OT_USERREFS, |
1246 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_TMP_USERREFS, tx); | |
428870ff BB |
1247 | } |
1248 | ||
1249 | static int | |
1250 | dsl_pool_user_hold_rele_impl(dsl_pool_t *dp, uint64_t dsobj, | |
13fe0198 | 1251 | const char *tag, uint64_t now, dmu_tx_t *tx, boolean_t holding) |
428870ff BB |
1252 | { |
1253 | objset_t *mos = dp->dp_meta_objset; | |
1254 | uint64_t zapobj = dp->dp_tmp_userrefs_obj; | |
1255 | char *name; | |
1256 | int error; | |
1257 | ||
1258 | ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS); | |
1259 | ASSERT(dmu_tx_is_syncing(tx)); | |
1260 | ||
1261 | /* | |
1262 | * If the pool was created prior to SPA_VERSION_USERREFS, the | |
1263 | * zap object for temporary holds might not exist yet. | |
1264 | */ | |
1265 | if (zapobj == 0) { | |
1266 | if (holding) { | |
1267 | dsl_pool_user_hold_create_obj(dp, tx); | |
1268 | zapobj = dp->dp_tmp_userrefs_obj; | |
1269 | } else { | |
2e528b49 | 1270 | return (SET_ERROR(ENOENT)); |
428870ff BB |
1271 | } |
1272 | } | |
1273 | ||
1274 | name = kmem_asprintf("%llx-%s", (u_longlong_t)dsobj, tag); | |
1275 | if (holding) | |
13fe0198 | 1276 | error = zap_add(mos, zapobj, name, 8, 1, &now, tx); |
428870ff BB |
1277 | else |
1278 | error = zap_remove(mos, zapobj, name, tx); | |
e4f5fa12 | 1279 | kmem_strfree(name); |
428870ff BB |
1280 | |
1281 | return (error); | |
1282 | } | |
1283 | ||
1284 | /* | |
1285 | * Add a temporary hold for the given dataset object and tag. | |
1286 | */ | |
1287 | int | |
1288 | dsl_pool_user_hold(dsl_pool_t *dp, uint64_t dsobj, const char *tag, | |
13fe0198 | 1289 | uint64_t now, dmu_tx_t *tx) |
428870ff BB |
1290 | { |
1291 | return (dsl_pool_user_hold_rele_impl(dp, dsobj, tag, now, tx, B_TRUE)); | |
1292 | } | |
1293 | ||
1294 | /* | |
1295 | * Release a temporary hold for the given dataset object and tag. | |
1296 | */ | |
1297 | int | |
1298 | dsl_pool_user_release(dsl_pool_t *dp, uint64_t dsobj, const char *tag, | |
1299 | dmu_tx_t *tx) | |
1300 | { | |
13fe0198 | 1301 | return (dsl_pool_user_hold_rele_impl(dp, dsobj, tag, 0, |
428870ff BB |
1302 | tx, B_FALSE)); |
1303 | } | |
c409e464 | 1304 | |
13fe0198 MA |
1305 | /* |
1306 | * DSL Pool Configuration Lock | |
1307 | * | |
1308 | * The dp_config_rwlock protects against changes to DSL state (e.g. dataset | |
1309 | * creation / destruction / rename / property setting). It must be held for | |
1310 | * read to hold a dataset or dsl_dir. I.e. you must call | |
1311 | * dsl_pool_config_enter() or dsl_pool_hold() before calling | |
1312 | * dsl_{dataset,dir}_hold{_obj}. In most circumstances, the dp_config_rwlock | |
1313 | * must be held continuously until all datasets and dsl_dirs are released. | |
1314 | * | |
1315 | * The only exception to this rule is that if a "long hold" is placed on | |
1316 | * a dataset, then the dp_config_rwlock may be dropped while the dataset | |
1317 | * is still held. The long hold will prevent the dataset from being | |
1318 | * destroyed -- the destroy will fail with EBUSY. A long hold can be | |
1319 | * obtained by calling dsl_dataset_long_hold(), or by "owning" a dataset | |
1320 | * (by calling dsl_{dataset,objset}_{try}own{_obj}). | |
1321 | * | |
1322 | * Legitimate long-holders (including owners) should be long-running, cancelable | |
1323 | * tasks that should cause "zfs destroy" to fail. This includes DMU | |
1324 | * consumers (i.e. a ZPL filesystem being mounted or ZVOL being open), | |
1325 | * "zfs send", and "zfs diff". There are several other long-holders whose | |
1326 | * uses are suboptimal (e.g. "zfs promote", and zil_suspend()). | |
1327 | * | |
1328 | * The usual formula for long-holding would be: | |
1329 | * dsl_pool_hold() | |
1330 | * dsl_dataset_hold() | |
1331 | * ... perform checks ... | |
1332 | * dsl_dataset_long_hold() | |
1333 | * dsl_pool_rele() | |
1334 | * ... perform long-running task ... | |
1335 | * dsl_dataset_long_rele() | |
1336 | * dsl_dataset_rele() | |
1337 | * | |
1338 | * Note that when the long hold is released, the dataset is still held but | |
1339 | * the pool is not held. The dataset may change arbitrarily during this time | |
1340 | * (e.g. it could be destroyed). Therefore you shouldn't do anything to the | |
1341 | * dataset except release it. | |
1342 | * | |
49c482fd CS |
1343 | * Operations generally fall somewhere into the following taxonomy: |
1344 | * | |
1345 | * Read-Only Modifying | |
1346 | * | |
1347 | * Dataset Layer / MOS zfs get zfs destroy | |
1348 | * | |
1349 | * Individual Dataset read() write() | |
1350 | * | |
1351 | * | |
1352 | * Dataset Layer Operations | |
13fe0198 MA |
1353 | * |
1354 | * Modifying operations should generally use dsl_sync_task(). The synctask | |
1355 | * infrastructure enforces proper locking strategy with respect to the | |
1356 | * dp_config_rwlock. See the comment above dsl_sync_task() for details. | |
1357 | * | |
1358 | * Read-only operations will manually hold the pool, then the dataset, obtain | |
1359 | * information from the dataset, then release the pool and dataset. | |
1360 | * dmu_objset_{hold,rele}() are convenience routines that also do the pool | |
1361 | * hold/rele. | |
49c482fd CS |
1362 | * |
1363 | * | |
1364 | * Operations On Individual Datasets | |
1365 | * | |
1366 | * Objects _within_ an objset should only be modified by the current 'owner' | |
1367 | * of the objset to prevent incorrect concurrent modification. Thus, use | |
1368 | * {dmu_objset,dsl_dataset}_own to mark some entity as the current owner, | |
1369 | * and fail with EBUSY if there is already an owner. The owner can then | |
1370 | * implement its own locking strategy, independent of the dataset layer's | |
1371 | * locking infrastructure. | |
1372 | * (E.g., the ZPL has its own set of locks to control concurrency. A regular | |
1373 | * vnop will not reach into the dataset layer). | |
1374 | * | |
1375 | * Ideally, objects would also only be read by the objset’s owner, so that we | |
1376 | * don’t observe state mid-modification. | |
1377 | * (E.g. the ZPL is creating a new object and linking it into a directory; if | |
1378 | * you don’t coordinate with the ZPL to hold ZPL-level locks, you could see an | |
1379 | * intermediate state. The ioctl level violates this but in pretty benign | |
1380 | * ways, e.g. reading the zpl props object.) | |
13fe0198 MA |
1381 | */ |
1382 | ||
1383 | int | |
a926aab9 | 1384 | dsl_pool_hold(const char *name, const void *tag, dsl_pool_t **dp) |
13fe0198 MA |
1385 | { |
1386 | spa_t *spa; | |
1387 | int error; | |
1388 | ||
1389 | error = spa_open(name, &spa, tag); | |
1390 | if (error == 0) { | |
1391 | *dp = spa_get_dsl(spa); | |
1392 | dsl_pool_config_enter(*dp, tag); | |
1393 | } | |
1394 | return (error); | |
1395 | } | |
1396 | ||
1397 | void | |
a926aab9 | 1398 | dsl_pool_rele(dsl_pool_t *dp, const void *tag) |
13fe0198 MA |
1399 | { |
1400 | dsl_pool_config_exit(dp, tag); | |
1401 | spa_close(dp->dp_spa, tag); | |
1402 | } | |
1403 | ||
1404 | void | |
a926aab9 | 1405 | dsl_pool_config_enter(dsl_pool_t *dp, const void *tag) |
13fe0198 MA |
1406 | { |
1407 | /* | |
1408 | * We use a "reentrant" reader-writer lock, but not reentrantly. | |
1409 | * | |
1410 | * The rrwlock can (with the track_all flag) track all reading threads, | |
1411 | * which is very useful for debugging which code path failed to release | |
1412 | * the lock, and for verifying that the *current* thread does hold | |
1413 | * the lock. | |
1414 | * | |
1415 | * (Unlike a rwlock, which knows that N threads hold it for | |
1416 | * read, but not *which* threads, so rw_held(RW_READER) returns TRUE | |
1417 | * if any thread holds it for read, even if this thread doesn't). | |
1418 | */ | |
1419 | ASSERT(!rrw_held(&dp->dp_config_rwlock, RW_READER)); | |
1420 | rrw_enter(&dp->dp_config_rwlock, RW_READER, tag); | |
1421 | } | |
1422 | ||
5e8cd5d1 | 1423 | void |
a926aab9 | 1424 | dsl_pool_config_enter_prio(dsl_pool_t *dp, const void *tag) |
5e8cd5d1 AJ |
1425 | { |
1426 | ASSERT(!rrw_held(&dp->dp_config_rwlock, RW_READER)); | |
1427 | rrw_enter_read_prio(&dp->dp_config_rwlock, tag); | |
1428 | } | |
1429 | ||
13fe0198 | 1430 | void |
a926aab9 | 1431 | dsl_pool_config_exit(dsl_pool_t *dp, const void *tag) |
13fe0198 MA |
1432 | { |
1433 | rrw_exit(&dp->dp_config_rwlock, tag); | |
1434 | } | |
1435 | ||
1436 | boolean_t | |
1437 | dsl_pool_config_held(dsl_pool_t *dp) | |
1438 | { | |
1439 | return (RRW_LOCK_HELD(&dp->dp_config_rwlock)); | |
1440 | } | |
1441 | ||
9c43027b AJ |
1442 | boolean_t |
1443 | dsl_pool_config_held_writer(dsl_pool_t *dp) | |
1444 | { | |
1445 | return (RRW_WRITE_HELD(&dp->dp_config_rwlock)); | |
1446 | } | |
1447 | ||
40a806df NB |
1448 | EXPORT_SYMBOL(dsl_pool_config_enter); |
1449 | EXPORT_SYMBOL(dsl_pool_config_exit); | |
1450 | ||
d1d7e268 | 1451 | /* zfs_dirty_data_max_percent only applied at module load in arc_init(). */ |
fdc2d303 | 1452 | ZFS_MODULE_PARAM(zfs, zfs_, dirty_data_max_percent, UINT, ZMOD_RD, |
03fdcb9a | 1453 | "Max percent of RAM allowed to be dirty"); |
c409e464 | 1454 | |
d1d7e268 | 1455 | /* zfs_dirty_data_max_max_percent only applied at module load in arc_init(). */ |
fdc2d303 | 1456 | ZFS_MODULE_PARAM(zfs, zfs_, dirty_data_max_max_percent, UINT, ZMOD_RD, |
d1d7e268 | 1457 | "zfs_dirty_data_max upper bound as % of RAM"); |
c409e464 | 1458 | |
fdc2d303 | 1459 | ZFS_MODULE_PARAM(zfs, zfs_, delay_min_dirty_percent, UINT, ZMOD_RW, |
03fdcb9a | 1460 | "Transaction delay threshold"); |
c409e464 | 1461 | |
ab8d9c17 | 1462 | ZFS_MODULE_PARAM(zfs, zfs_, dirty_data_max, U64, ZMOD_RW, |
03fdcb9a | 1463 | "Determines the dirty space limit"); |
c409e464 | 1464 | |
ab8d9c17 | 1465 | ZFS_MODULE_PARAM(zfs, zfs_, wrlog_data_max, U64, ZMOD_RW, |
a7bd20e3 KJ |
1466 | "The size limit of write-transaction zil log data"); |
1467 | ||
d1d7e268 | 1468 | /* zfs_dirty_data_max_max only applied at module load in arc_init(). */ |
ab8d9c17 | 1469 | ZFS_MODULE_PARAM(zfs, zfs_, dirty_data_max_max, U64, ZMOD_RD, |
d1d7e268 | 1470 | "zfs_dirty_data_max upper bound in bytes"); |
c409e464 | 1471 | |
fdc2d303 | 1472 | ZFS_MODULE_PARAM(zfs, zfs_, dirty_data_sync_percent, UINT, ZMOD_RW, |
03fdcb9a | 1473 | "Dirty data txg sync threshold as a percentage of zfs_dirty_data_max"); |
c409e464 | 1474 | |
ab8d9c17 | 1475 | ZFS_MODULE_PARAM(zfs, zfs_, delay_scale, U64, ZMOD_RW, |
03fdcb9a | 1476 | "How quickly delay approaches infinity"); |
64fc7762 | 1477 | |
03fdcb9a MM |
1478 | ZFS_MODULE_PARAM(zfs_zil, zfs_zil_, clean_taskq_nthr_pct, INT, ZMOD_RW, |
1479 | "Max percent of CPUs that are used per dp_sync_taskq"); | |
a032ac4b | 1480 | |
03fdcb9a MM |
1481 | ZFS_MODULE_PARAM(zfs_zil, zfs_zil_, clean_taskq_minalloc, INT, ZMOD_RW, |
1482 | "Number of taskq entries that are pre-populated"); | |
a032ac4b | 1483 | |
03fdcb9a MM |
1484 | ZFS_MODULE_PARAM(zfs_zil, zfs_zil_, clean_taskq_maxalloc, INT, ZMOD_RW, |
1485 | "Max number of taskq entries that are cached"); |