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