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 9 * or http://www.opensolaris.org/os/licensing. 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 /* 22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23 * Copyright (c) 2011, 2017 by Delphix. All rights reserved. 24 * Copyright (c) 2013 Steven Hartland. All rights reserved. 25 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved. 26 * Copyright (c) 2014 Integros [integros.com] 27 * Copyright 2016 Nexenta Systems, Inc. All rights reserved. 28 */ 29 30 #include <sys/dsl_pool.h> 31 #include <sys/dsl_dataset.h> 32 #include <sys/dsl_prop.h> 33 #include <sys/dsl_dir.h> 34 #include <sys/dsl_synctask.h> 35 #include <sys/dsl_scan.h> 36 #include <sys/dnode.h> 37 #include <sys/dmu_tx.h> 38 #include <sys/dmu_objset.h> 39 #include <sys/arc.h> 40 #include <sys/zap.h> 41 #include <sys/zio.h> 42 #include <sys/zfs_context.h> 43 #include <sys/fs/zfs.h> 44 #include <sys/zfs_znode.h> 45 #include <sys/spa_impl.h> 46 #include <sys/dsl_deadlist.h> 47 #include <sys/vdev_impl.h> 48 #include <sys/metaslab_impl.h> 49 #include <sys/bptree.h> 50 #include <sys/zfeature.h> 51 #include <sys/zil_impl.h> 52 #include <sys/dsl_userhold.h> 53 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 * 85 * The zfs_dirty_data_sync tunable dictates the threshold at which we 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 in /etc/system. 100 */ 101 uint64_t zfs_dirty_data_max; 102 uint64_t zfs_dirty_data_max_max = 4ULL * 1024 * 1024 * 1024; 103 int zfs_dirty_data_max_percent = 10; 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 uint64_t zfs_dirty_data_sync_pct = 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 int 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 * This determines the number of threads used by the dp_sync_taskq. 136 */ 137 int zfs_sync_taskq_batch_pct = 75; 138 139 /* 140 * These tunables determine the behavior of how zil_itxg_clean() is 141 * called via zil_clean() in the context of spa_sync(). When an itxg 142 * list needs to be cleaned, TQ_NOSLEEP will be used when dispatching. 143 * If the dispatch fails, the call to zil_itxg_clean() will occur 144 * synchronously in the context of spa_sync(), which can negatively 145 * impact the performance of spa_sync() (e.g. in the case of the itxg 146 * list having a large number of itxs that needs to be cleaned). 147 * 148 * Thus, these tunables can be used to manipulate the behavior of the 149 * taskq used by zil_clean(); they determine the number of taskq entries 150 * that are pre-populated when the taskq is first created (via the 151 * "zfs_zil_clean_taskq_minalloc" tunable) and the maximum number of 152 * taskq entries that are cached after an on-demand allocation (via the 153 * "zfs_zil_clean_taskq_maxalloc"). 154 * 155 * The idea being, we want to try reasonably hard to ensure there will 156 * already be a taskq entry pre-allocated by the time that it is needed 157 * by zil_clean(). This way, we can avoid the possibility of an 158 * on-demand allocation of a new taskq entry from failing, which would 159 * result in zil_itxg_clean() being called synchronously from zil_clean() 160 * (which can adversely affect performance of spa_sync()). 161 * 162 * Additionally, the number of threads used by the taskq can be 163 * configured via the "zfs_zil_clean_taskq_nthr_pct" tunable. 164 */ 165 int zfs_zil_clean_taskq_nthr_pct = 100; 166 int zfs_zil_clean_taskq_minalloc = 1024; 167 int zfs_zil_clean_taskq_maxalloc = 1024 * 1024; 168 169 int 170 dsl_pool_open_special_dir(dsl_pool_t *dp, const char *name, dsl_dir_t **ddp) 171 { 172 uint64_t obj; 173 int err; 174 175 err = zap_lookup(dp->dp_meta_objset, 176 dsl_dir_phys(dp->dp_root_dir)->dd_child_dir_zapobj, 177 name, sizeof (obj), 1, &obj); 178 if (err) 179 return (err); 180 181 return (dsl_dir_hold_obj(dp, obj, name, dp, ddp)); 182 } 183 184 static dsl_pool_t * 185 dsl_pool_open_impl(spa_t *spa, uint64_t txg) 186 { 187 dsl_pool_t *dp; 188 blkptr_t *bp = spa_get_rootblkptr(spa); 189 190 dp = kmem_zalloc(sizeof (dsl_pool_t), KM_SLEEP); 191 dp->dp_spa = spa; 192 dp->dp_meta_rootbp = *bp; 193 rrw_init(&dp->dp_config_rwlock, B_TRUE); 194 txg_init(dp, txg); 195 196 txg_list_create(&dp->dp_dirty_datasets, spa, 197 offsetof(dsl_dataset_t, ds_dirty_link)); 198 txg_list_create(&dp->dp_dirty_zilogs, spa, 199 offsetof(zilog_t, zl_dirty_link)); 200 txg_list_create(&dp->dp_dirty_dirs, spa, 201 offsetof(dsl_dir_t, dd_dirty_link)); 202 txg_list_create(&dp->dp_sync_tasks, spa, 203 offsetof(dsl_sync_task_t, dst_node)); 204 txg_list_create(&dp->dp_early_sync_tasks, spa, 205 offsetof(dsl_sync_task_t, dst_node)); 206 207 dp->dp_sync_taskq = taskq_create("dp_sync_taskq", 208 zfs_sync_taskq_batch_pct, minclsyspri, 1, INT_MAX, 209 TASKQ_THREADS_CPU_PCT); 210 211 dp->dp_zil_clean_taskq = taskq_create("dp_zil_clean_taskq", 212 zfs_zil_clean_taskq_nthr_pct, minclsyspri, 213 zfs_zil_clean_taskq_minalloc, 214 zfs_zil_clean_taskq_maxalloc, 215 TASKQ_PREPOPULATE | TASKQ_THREADS_CPU_PCT); 216 217 mutex_init(&dp->dp_lock, NULL, MUTEX_DEFAULT, NULL); 218 cv_init(&dp->dp_spaceavail_cv, NULL, CV_DEFAULT, NULL); 219 220 dp->dp_vnrele_taskq = taskq_create("zfs_vn_rele_taskq", 1, minclsyspri, 221 1, 4, 0); 222 223 return (dp); 224 } 225 226 int 227 dsl_pool_init(spa_t *spa, uint64_t txg, dsl_pool_t **dpp) 228 { 229 int err; 230 dsl_pool_t *dp = dsl_pool_open_impl(spa, txg); 231 232 err = dmu_objset_open_impl(spa, NULL, &dp->dp_meta_rootbp, 233 &dp->dp_meta_objset); 234 if (err != 0) 235 dsl_pool_close(dp); 236 else 237 *dpp = dp; 238 239 return (err); 240 } 241 242 int 243 dsl_pool_open(dsl_pool_t *dp) 244 { 245 int err; 246 dsl_dir_t *dd; 247 dsl_dataset_t *ds; 248 uint64_t obj; 249 250 rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG); 251 err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 252 DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1, 253 &dp->dp_root_dir_obj); 254 if (err) 255 goto out; 256 257 err = dsl_dir_hold_obj(dp, dp->dp_root_dir_obj, 258 NULL, dp, &dp->dp_root_dir); 259 if (err) 260 goto out; 261 262 err = dsl_pool_open_special_dir(dp, MOS_DIR_NAME, &dp->dp_mos_dir); 263 if (err) 264 goto out; 265 266 if (spa_version(dp->dp_spa) >= SPA_VERSION_ORIGIN) { 267 err = dsl_pool_open_special_dir(dp, ORIGIN_DIR_NAME, &dd); 268 if (err) 269 goto out; 270 err = dsl_dataset_hold_obj(dp, 271 dsl_dir_phys(dd)->dd_head_dataset_obj, FTAG, &ds); 272 if (err == 0) { 273 err = dsl_dataset_hold_obj(dp, 274 dsl_dataset_phys(ds)->ds_prev_snap_obj, dp, 275 &dp->dp_origin_snap); 276 dsl_dataset_rele(ds, FTAG); 277 } 278 dsl_dir_rele(dd, dp); 279 if (err) 280 goto out; 281 } 282 283 if (spa_version(dp->dp_spa) >= SPA_VERSION_DEADLISTS) { 284 err = dsl_pool_open_special_dir(dp, FREE_DIR_NAME, 285 &dp->dp_free_dir); 286 if (err) 287 goto out; 288 289 err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 290 DMU_POOL_FREE_BPOBJ, sizeof (uint64_t), 1, &obj); 291 if (err) 292 goto out; 293 VERIFY0(bpobj_open(&dp->dp_free_bpobj, 294 dp->dp_meta_objset, obj)); 295 } 296 297 if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_OBSOLETE_COUNTS)) { 298 err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 299 DMU_POOL_OBSOLETE_BPOBJ, sizeof (uint64_t), 1, &obj); 300 if (err == 0) { 301 VERIFY0(bpobj_open(&dp->dp_obsolete_bpobj, 302 dp->dp_meta_objset, obj)); 303 } else if (err == ENOENT) { 304 /* 305 * We might not have created the remap bpobj yet. 306 */ 307 err = 0; 308 } else { 309 goto out; 310 } 311 } 312 313 /* 314 * Note: errors ignored, because the these special dirs, used for 315 * space accounting, are only created on demand. 316 */ 317 (void) dsl_pool_open_special_dir(dp, LEAK_DIR_NAME, 318 &dp->dp_leak_dir); 319 320 if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_ASYNC_DESTROY)) { 321 err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 322 DMU_POOL_BPTREE_OBJ, sizeof (uint64_t), 1, 323 &dp->dp_bptree_obj); 324 if (err != 0) 325 goto out; 326 } 327 328 if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_EMPTY_BPOBJ)) { 329 err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 330 DMU_POOL_EMPTY_BPOBJ, sizeof (uint64_t), 1, 331 &dp->dp_empty_bpobj); 332 if (err != 0) 333 goto out; 334 } 335 336 err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 337 DMU_POOL_TMP_USERREFS, sizeof (uint64_t), 1, 338 &dp->dp_tmp_userrefs_obj); 339 if (err == ENOENT) 340 err = 0; 341 if (err) 342 goto out; 343 344 err = dsl_scan_init(dp, dp->dp_tx.tx_open_txg); 345 346 out: 347 rrw_exit(&dp->dp_config_rwlock, FTAG); 348 return (err); 349 } 350 351 void 352 dsl_pool_close(dsl_pool_t *dp) 353 { 354 /* 355 * Drop our references from dsl_pool_open(). 356 * 357 * Since we held the origin_snap from "syncing" context (which 358 * includes pool-opening context), it actually only got a "ref" 359 * and not a hold, so just drop that here. 360 */ 361 if (dp->dp_origin_snap != NULL) 362 dsl_dataset_rele(dp->dp_origin_snap, dp); 363 if (dp->dp_mos_dir != NULL) 364 dsl_dir_rele(dp->dp_mos_dir, dp); 365 if (dp->dp_free_dir != NULL) 366 dsl_dir_rele(dp->dp_free_dir, dp); 367 if (dp->dp_leak_dir != NULL) 368 dsl_dir_rele(dp->dp_leak_dir, dp); 369 if (dp->dp_root_dir != NULL) 370 dsl_dir_rele(dp->dp_root_dir, dp); 371 372 bpobj_close(&dp->dp_free_bpobj); 373 bpobj_close(&dp->dp_obsolete_bpobj); 374 375 /* undo the dmu_objset_open_impl(mos) from dsl_pool_open() */ 376 if (dp->dp_meta_objset != NULL) 377 dmu_objset_evict(dp->dp_meta_objset); 378 379 txg_list_destroy(&dp->dp_dirty_datasets); 380 txg_list_destroy(&dp->dp_dirty_zilogs); 381 txg_list_destroy(&dp->dp_sync_tasks); 382 txg_list_destroy(&dp->dp_early_sync_tasks); 383 txg_list_destroy(&dp->dp_dirty_dirs); 384 385 taskq_destroy(dp->dp_zil_clean_taskq); 386 taskq_destroy(dp->dp_sync_taskq); 387 388 /* 389 * We can't set retry to TRUE since we're explicitly specifying 390 * a spa to flush. This is good enough; any missed buffers for 391 * this spa won't cause trouble, and they'll eventually fall 392 * out of the ARC just like any other unused buffer. 393 */ 394 arc_flush(dp->dp_spa, FALSE); 395 396 txg_fini(dp); 397 dsl_scan_fini(dp); 398 dmu_buf_user_evict_wait(); 399 400 rrw_destroy(&dp->dp_config_rwlock); 401 mutex_destroy(&dp->dp_lock); 402 taskq_destroy(dp->dp_vnrele_taskq); 403 if (dp->dp_blkstats != NULL) 404 kmem_free(dp->dp_blkstats, sizeof (zfs_all_blkstats_t)); 405 kmem_free(dp, sizeof (dsl_pool_t)); 406 } 407 408 void 409 dsl_pool_create_obsolete_bpobj(dsl_pool_t *dp, dmu_tx_t *tx) 410 { 411 uint64_t obj; 412 /* 413 * Currently, we only create the obsolete_bpobj where there are 414 * indirect vdevs with referenced mappings. 415 */ 416 ASSERT(spa_feature_is_active(dp->dp_spa, SPA_FEATURE_DEVICE_REMOVAL)); 417 /* create and open the obsolete_bpobj */ 418 obj = bpobj_alloc(dp->dp_meta_objset, SPA_OLD_MAXBLOCKSIZE, tx); 419 VERIFY0(bpobj_open(&dp->dp_obsolete_bpobj, dp->dp_meta_objset, obj)); 420 VERIFY0(zap_add(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 421 DMU_POOL_OBSOLETE_BPOBJ, sizeof (uint64_t), 1, &obj, tx)); 422 spa_feature_incr(dp->dp_spa, SPA_FEATURE_OBSOLETE_COUNTS, tx); 423 } 424 425 void 426 dsl_pool_destroy_obsolete_bpobj(dsl_pool_t *dp, dmu_tx_t *tx) 427 { 428 spa_feature_decr(dp->dp_spa, SPA_FEATURE_OBSOLETE_COUNTS, tx); 429 VERIFY0(zap_remove(dp->dp_meta_objset, 430 DMU_POOL_DIRECTORY_OBJECT, 431 DMU_POOL_OBSOLETE_BPOBJ, tx)); 432 bpobj_free(dp->dp_meta_objset, 433 dp->dp_obsolete_bpobj.bpo_object, tx); 434 bpobj_close(&dp->dp_obsolete_bpobj); 435 } 436 437 dsl_pool_t * 438 dsl_pool_create(spa_t *spa, nvlist_t *zplprops, uint64_t txg) 439 { 440 int err; 441 dsl_pool_t *dp = dsl_pool_open_impl(spa, txg); 442 dmu_tx_t *tx = dmu_tx_create_assigned(dp, txg); 443 dsl_dataset_t *ds; 444 uint64_t obj; 445 446 rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG); 447 448 /* create and open the MOS (meta-objset) */ 449 dp->dp_meta_objset = dmu_objset_create_impl(spa, 450 NULL, &dp->dp_meta_rootbp, DMU_OST_META, tx); 451 452 /* create the pool directory */ 453 err = zap_create_claim(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 454 DMU_OT_OBJECT_DIRECTORY, DMU_OT_NONE, 0, tx); 455 ASSERT0(err); 456 457 /* Initialize scan structures */ 458 VERIFY0(dsl_scan_init(dp, txg)); 459 460 /* create and open the root dir */ 461 dp->dp_root_dir_obj = dsl_dir_create_sync(dp, NULL, NULL, tx); 462 VERIFY0(dsl_dir_hold_obj(dp, dp->dp_root_dir_obj, 463 NULL, dp, &dp->dp_root_dir)); 464 465 /* create and open the meta-objset dir */ 466 (void) dsl_dir_create_sync(dp, dp->dp_root_dir, MOS_DIR_NAME, tx); 467 VERIFY0(dsl_pool_open_special_dir(dp, 468 MOS_DIR_NAME, &dp->dp_mos_dir)); 469 470 if (spa_version(spa) >= SPA_VERSION_DEADLISTS) { 471 /* create and open the free dir */ 472 (void) dsl_dir_create_sync(dp, dp->dp_root_dir, 473 FREE_DIR_NAME, tx); 474 VERIFY0(dsl_pool_open_special_dir(dp, 475 FREE_DIR_NAME, &dp->dp_free_dir)); 476 477 /* create and open the free_bplist */ 478 obj = bpobj_alloc(dp->dp_meta_objset, SPA_OLD_MAXBLOCKSIZE, tx); 479 VERIFY(zap_add(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 480 DMU_POOL_FREE_BPOBJ, sizeof (uint64_t), 1, &obj, tx) == 0); 481 VERIFY0(bpobj_open(&dp->dp_free_bpobj, 482 dp->dp_meta_objset, obj)); 483 } 484 485 if (spa_version(spa) >= SPA_VERSION_DSL_SCRUB) 486 dsl_pool_create_origin(dp, tx); 487 488 /* create the root dataset */ 489 obj = dsl_dataset_create_sync_dd(dp->dp_root_dir, NULL, 0, tx); 490 491 /* create the root objset */ 492 VERIFY0(dsl_dataset_hold_obj(dp, obj, FTAG, &ds)); 493 #ifdef _KERNEL 494 { 495 objset_t *os; 496 rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG); 497 os = dmu_objset_create_impl(dp->dp_spa, ds, 498 dsl_dataset_get_blkptr(ds), DMU_OST_ZFS, tx); 499 rrw_exit(&ds->ds_bp_rwlock, FTAG); 500 zfs_create_fs(os, kcred, zplprops, tx); 501 } 502 #endif 503 dsl_dataset_rele(ds, FTAG); 504 505 dmu_tx_commit(tx); 506 507 rrw_exit(&dp->dp_config_rwlock, FTAG); 508 509 return (dp); 510 } 511 512 /* 513 * Account for the meta-objset space in its placeholder dsl_dir. 514 */ 515 void 516 dsl_pool_mos_diduse_space(dsl_pool_t *dp, 517 int64_t used, int64_t comp, int64_t uncomp) 518 { 519 ASSERT3U(comp, ==, uncomp); /* it's all metadata */ 520 mutex_enter(&dp->dp_lock); 521 dp->dp_mos_used_delta += used; 522 dp->dp_mos_compressed_delta += comp; 523 dp->dp_mos_uncompressed_delta += uncomp; 524 mutex_exit(&dp->dp_lock); 525 } 526 527 static void 528 dsl_pool_sync_mos(dsl_pool_t *dp, dmu_tx_t *tx) 529 { 530 zio_t *zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED); 531 dmu_objset_sync(dp->dp_meta_objset, zio, tx); 532 VERIFY0(zio_wait(zio)); 533 dprintf_bp(&dp->dp_meta_rootbp, "meta objset rootbp is %s", ""); 534 spa_set_rootblkptr(dp->dp_spa, &dp->dp_meta_rootbp); 535 } 536 537 static void 538 dsl_pool_dirty_delta(dsl_pool_t *dp, int64_t delta) 539 { 540 ASSERT(MUTEX_HELD(&dp->dp_lock)); 541 542 if (delta < 0) 543 ASSERT3U(-delta, <=, dp->dp_dirty_total); 544 545 dp->dp_dirty_total += delta; 546 547 /* 548 * Note: we signal even when increasing dp_dirty_total. 549 * This ensures forward progress -- each thread wakes the next waiter. 550 */ 551 if (dp->dp_dirty_total < zfs_dirty_data_max) 552 cv_signal(&dp->dp_spaceavail_cv); 553 } 554 555 static boolean_t 556 dsl_early_sync_task_verify(dsl_pool_t *dp, uint64_t txg) 557 { 558 spa_t *spa = dp->dp_spa; 559 vdev_t *rvd = spa->spa_root_vdev; 560 561 for (uint64_t c = 0; c < rvd->vdev_children; c++) { 562 vdev_t *vd = rvd->vdev_child[c]; 563 txg_list_t *tl = &vd->vdev_ms_list; 564 metaslab_t *ms; 565 566 for (ms = txg_list_head(tl, TXG_CLEAN(txg)); ms; 567 ms = txg_list_next(tl, ms, TXG_CLEAN(txg))) { 568 VERIFY(range_tree_is_empty(ms->ms_freeing)); 569 VERIFY(range_tree_is_empty(ms->ms_checkpointing)); 570 } 571 } 572 573 return (B_TRUE); 574 } 575 576 void 577 dsl_pool_sync(dsl_pool_t *dp, uint64_t txg) 578 { 579 zio_t *zio; 580 dmu_tx_t *tx; 581 dsl_dir_t *dd; 582 dsl_dataset_t *ds; 583 objset_t *mos = dp->dp_meta_objset; 584 list_t synced_datasets; 585 586 list_create(&synced_datasets, sizeof (dsl_dataset_t), 587 offsetof(dsl_dataset_t, ds_synced_link)); 588 589 tx = dmu_tx_create_assigned(dp, txg); 590 591 /* 592 * Run all early sync tasks before writing out any dirty blocks. 593 * For more info on early sync tasks see block comment in 594 * dsl_early_sync_task(). 595 */ 596 if (!txg_list_empty(&dp->dp_early_sync_tasks, txg)) { 597 dsl_sync_task_t *dst; 598 599 ASSERT3U(spa_sync_pass(dp->dp_spa), ==, 1); 600 while ((dst = 601 txg_list_remove(&dp->dp_early_sync_tasks, txg)) != NULL) { 602 ASSERT(dsl_early_sync_task_verify(dp, txg)); 603 dsl_sync_task_sync(dst, tx); 604 } 605 ASSERT(dsl_early_sync_task_verify(dp, txg)); 606 } 607 608 /* 609 * Write out all dirty blocks of dirty datasets. 610 */ 611 zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED); 612 while ((ds = txg_list_remove(&dp->dp_dirty_datasets, txg)) != NULL) { 613 /* 614 * We must not sync any non-MOS datasets twice, because 615 * we may have taken a snapshot of them. However, we 616 * may sync newly-created datasets on pass 2. 617 */ 618 ASSERT(!list_link_active(&ds->ds_synced_link)); 619 list_insert_tail(&synced_datasets, ds); 620 dsl_dataset_sync(ds, zio, tx); 621 } 622 VERIFY0(zio_wait(zio)); 623 624 /* 625 * We have written all of the accounted dirty data, so our 626 * dp_space_towrite should now be zero. However, some seldom-used 627 * code paths do not adhere to this (e.g. dbuf_undirty(), also 628 * rounding error in dbuf_write_physdone). 629 * Shore up the accounting of any dirtied space now. 630 */ 631 dsl_pool_undirty_space(dp, dp->dp_dirty_pertxg[txg & TXG_MASK], txg); 632 633 /* 634 * Update the long range free counter after 635 * we're done syncing user data 636 */ 637 mutex_enter(&dp->dp_lock); 638 ASSERT(spa_sync_pass(dp->dp_spa) == 1 || 639 dp->dp_long_free_dirty_pertxg[txg & TXG_MASK] == 0); 640 dp->dp_long_free_dirty_pertxg[txg & TXG_MASK] = 0; 641 mutex_exit(&dp->dp_lock); 642 643 /* 644 * After the data blocks have been written (ensured by the zio_wait() 645 * above), update the user/group space accounting. This happens 646 * in tasks dispatched to dp_sync_taskq, so wait for them before 647 * continuing. 648 */ 649 for (ds = list_head(&synced_datasets); ds != NULL; 650 ds = list_next(&synced_datasets, ds)) { 651 dmu_objset_do_userquota_updates(ds->ds_objset, tx); 652 } 653 taskq_wait(dp->dp_sync_taskq); 654 655 /* 656 * Sync the datasets again to push out the changes due to 657 * userspace updates. This must be done before we process the 658 * sync tasks, so that any snapshots will have the correct 659 * user accounting information (and we won't get confused 660 * about which blocks are part of the snapshot). 661 */ 662 zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED); 663 while ((ds = txg_list_remove(&dp->dp_dirty_datasets, txg)) != NULL) { 664 ASSERT(list_link_active(&ds->ds_synced_link)); 665 dmu_buf_rele(ds->ds_dbuf, ds); 666 dsl_dataset_sync(ds, zio, tx); 667 } 668 VERIFY0(zio_wait(zio)); 669 670 /* 671 * Now that the datasets have been completely synced, we can 672 * clean up our in-memory structures accumulated while syncing: 673 * 674 * - move dead blocks from the pending deadlist to the on-disk deadlist 675 * - release hold from dsl_dataset_dirty() 676 */ 677 while ((ds = list_remove_head(&synced_datasets)) != NULL) { 678 dsl_dataset_sync_done(ds, tx); 679 } 680 while ((dd = txg_list_remove(&dp->dp_dirty_dirs, txg)) != NULL) { 681 dsl_dir_sync(dd, tx); 682 } 683 684 /* 685 * The MOS's space is accounted for in the pool/$MOS 686 * (dp_mos_dir). We can't modify the mos while we're syncing 687 * it, so we remember the deltas and apply them here. 688 */ 689 if (dp->dp_mos_used_delta != 0 || dp->dp_mos_compressed_delta != 0 || 690 dp->dp_mos_uncompressed_delta != 0) { 691 dsl_dir_diduse_space(dp->dp_mos_dir, DD_USED_HEAD, 692 dp->dp_mos_used_delta, 693 dp->dp_mos_compressed_delta, 694 dp->dp_mos_uncompressed_delta, tx); 695 dp->dp_mos_used_delta = 0; 696 dp->dp_mos_compressed_delta = 0; 697 dp->dp_mos_uncompressed_delta = 0; 698 } 699 700 if (!multilist_is_empty(mos->os_dirty_dnodes[txg & TXG_MASK])) { 701 dsl_pool_sync_mos(dp, tx); 702 } 703 704 /* 705 * If we modify a dataset in the same txg that we want to destroy it, 706 * its dsl_dir's dd_dbuf will be dirty, and thus have a hold on it. 707 * dsl_dir_destroy_check() will fail if there are unexpected holds. 708 * Therefore, we want to sync the MOS (thus syncing the dd_dbuf 709 * and clearing the hold on it) before we process the sync_tasks. 710 * The MOS data dirtied by the sync_tasks will be synced on the next 711 * pass. 712 */ 713 if (!txg_list_empty(&dp->dp_sync_tasks, txg)) { 714 dsl_sync_task_t *dst; 715 /* 716 * No more sync tasks should have been added while we 717 * were syncing. 718 */ 719 ASSERT3U(spa_sync_pass(dp->dp_spa), ==, 1); 720 while ((dst = txg_list_remove(&dp->dp_sync_tasks, txg)) != NULL) 721 dsl_sync_task_sync(dst, tx); 722 } 723 724 dmu_tx_commit(tx); 725 726 DTRACE_PROBE2(dsl_pool_sync__done, dsl_pool_t *dp, dp, uint64_t, txg); 727 } 728 729 void 730 dsl_pool_sync_done(dsl_pool_t *dp, uint64_t txg) 731 { 732 zilog_t *zilog; 733 734 while (zilog = txg_list_head(&dp->dp_dirty_zilogs, txg)) { 735 dsl_dataset_t *ds = dmu_objset_ds(zilog->zl_os); 736 /* 737 * We don't remove the zilog from the dp_dirty_zilogs 738 * list until after we've cleaned it. This ensures that 739 * callers of zilog_is_dirty() receive an accurate 740 * answer when they are racing with the spa sync thread. 741 */ 742 zil_clean(zilog, txg); 743 (void) txg_list_remove_this(&dp->dp_dirty_zilogs, zilog, txg); 744 ASSERT(!dmu_objset_is_dirty(zilog->zl_os, txg)); 745 dmu_buf_rele(ds->ds_dbuf, zilog); 746 } 747 ASSERT(!dmu_objset_is_dirty(dp->dp_meta_objset, txg)); 748 } 749 750 /* 751 * TRUE if the current thread is the tx_sync_thread or if we 752 * are being called from SPA context during pool initialization. 753 */ 754 int 755 dsl_pool_sync_context(dsl_pool_t *dp) 756 { 757 return (curthread == dp->dp_tx.tx_sync_thread || 758 spa_is_initializing(dp->dp_spa) || 759 taskq_member(dp->dp_sync_taskq, curthread)); 760 } 761 762 /* 763 * This function returns the amount of allocatable space in the pool 764 * minus whatever space is currently reserved by ZFS for specific 765 * purposes. Specifically: 766 * 767 * 1] Any reserved SLOP space 768 * 2] Any space used by the checkpoint 769 * 3] Any space used for deferred frees 770 * 771 * The latter 2 are especially important because they are needed to 772 * rectify the SPA's and DMU's different understanding of how much space 773 * is used. Now the DMU is aware of that extra space tracked by the SPA 774 * without having to maintain a separate special dir (e.g similar to 775 * $MOS, $FREEING, and $LEAKED). 776 * 777 * Note: By deferred frees here, we mean the frees that were deferred 778 * in spa_sync() after sync pass 1 (spa_deferred_bpobj), and not the 779 * segments placed in ms_defer trees during metaslab_sync_done(). 780 */ 781 uint64_t 782 dsl_pool_adjustedsize(dsl_pool_t *dp, zfs_space_check_t slop_policy) 783 { 784 spa_t *spa = dp->dp_spa; 785 uint64_t space, resv, adjustedsize; 786 uint64_t spa_deferred_frees = 787 spa->spa_deferred_bpobj.bpo_phys->bpo_bytes; 788 789 space = spa_get_dspace(spa) 790 - spa_get_checkpoint_space(spa) - spa_deferred_frees; 791 resv = spa_get_slop_space(spa); 792 793 switch (slop_policy) { 794 case ZFS_SPACE_CHECK_NORMAL: 795 break; 796 case ZFS_SPACE_CHECK_RESERVED: 797 resv >>= 1; 798 break; 799 case ZFS_SPACE_CHECK_EXTRA_RESERVED: 800 resv >>= 2; 801 break; 802 case ZFS_SPACE_CHECK_NONE: 803 resv = 0; 804 break; 805 default: 806 panic("invalid slop policy value: %d", slop_policy); 807 break; 808 } 809 adjustedsize = (space >= resv) ? (space - resv) : 0; 810 811 return (adjustedsize); 812 } 813 814 uint64_t 815 dsl_pool_unreserved_space(dsl_pool_t *dp, zfs_space_check_t slop_policy) 816 { 817 uint64_t poolsize = dsl_pool_adjustedsize(dp, slop_policy); 818 uint64_t deferred = 819 metaslab_class_get_deferred(spa_normal_class(dp->dp_spa)); 820 uint64_t quota = (poolsize >= deferred) ? (poolsize - deferred) : 0; 821 return (quota); 822 } 823 824 boolean_t 825 dsl_pool_need_dirty_delay(dsl_pool_t *dp) 826 { 827 uint64_t delay_min_bytes = 828 zfs_dirty_data_max * zfs_delay_min_dirty_percent / 100; 829 uint64_t dirty_min_bytes = 830 zfs_dirty_data_max * zfs_dirty_data_sync_pct / 100; 831 boolean_t rv; 832 833 mutex_enter(&dp->dp_lock); 834 if (dp->dp_dirty_total > dirty_min_bytes) 835 txg_kick(dp); 836 rv = (dp->dp_dirty_total > delay_min_bytes); 837 mutex_exit(&dp->dp_lock); 838 return (rv); 839 } 840 841 void 842 dsl_pool_dirty_space(dsl_pool_t *dp, int64_t space, dmu_tx_t *tx) 843 { 844 if (space > 0) { 845 mutex_enter(&dp->dp_lock); 846 dp->dp_dirty_pertxg[tx->tx_txg & TXG_MASK] += space; 847 dsl_pool_dirty_delta(dp, space); 848 mutex_exit(&dp->dp_lock); 849 } 850 } 851 852 void 853 dsl_pool_undirty_space(dsl_pool_t *dp, int64_t space, uint64_t txg) 854 { 855 ASSERT3S(space, >=, 0); 856 if (space == 0) 857 return; 858 mutex_enter(&dp->dp_lock); 859 if (dp->dp_dirty_pertxg[txg & TXG_MASK] < space) { 860 /* XXX writing something we didn't dirty? */ 861 space = dp->dp_dirty_pertxg[txg & TXG_MASK]; 862 } 863 ASSERT3U(dp->dp_dirty_pertxg[txg & TXG_MASK], >=, space); 864 dp->dp_dirty_pertxg[txg & TXG_MASK] -= space; 865 ASSERT3U(dp->dp_dirty_total, >=, space); 866 dsl_pool_dirty_delta(dp, -space); 867 mutex_exit(&dp->dp_lock); 868 } 869 870 /* ARGSUSED */ 871 static int 872 upgrade_clones_cb(dsl_pool_t *dp, dsl_dataset_t *hds, void *arg) 873 { 874 dmu_tx_t *tx = arg; 875 dsl_dataset_t *ds, *prev = NULL; 876 int err; 877 878 err = dsl_dataset_hold_obj(dp, hds->ds_object, FTAG, &ds); 879 if (err) 880 return (err); 881 882 while (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) { 883 err = dsl_dataset_hold_obj(dp, 884 dsl_dataset_phys(ds)->ds_prev_snap_obj, FTAG, &prev); 885 if (err) { 886 dsl_dataset_rele(ds, FTAG); 887 return (err); 888 } 889 890 if (dsl_dataset_phys(prev)->ds_next_snap_obj != ds->ds_object) 891 break; 892 dsl_dataset_rele(ds, FTAG); 893 ds = prev; 894 prev = NULL; 895 } 896 897 if (prev == NULL) { 898 prev = dp->dp_origin_snap; 899 900 /* 901 * The $ORIGIN can't have any data, or the accounting 902 * will be wrong. 903 */ 904 rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG); 905 ASSERT0(dsl_dataset_phys(prev)->ds_bp.blk_birth); 906 rrw_exit(&ds->ds_bp_rwlock, FTAG); 907 908 /* The origin doesn't get attached to itself */ 909 if (ds->ds_object == prev->ds_object) { 910 dsl_dataset_rele(ds, FTAG); 911 return (0); 912 } 913 914 dmu_buf_will_dirty(ds->ds_dbuf, tx); 915 dsl_dataset_phys(ds)->ds_prev_snap_obj = prev->ds_object; 916 dsl_dataset_phys(ds)->ds_prev_snap_txg = 917 dsl_dataset_phys(prev)->ds_creation_txg; 918 919 dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx); 920 dsl_dir_phys(ds->ds_dir)->dd_origin_obj = prev->ds_object; 921 922 dmu_buf_will_dirty(prev->ds_dbuf, tx); 923 dsl_dataset_phys(prev)->ds_num_children++; 924 925 if (dsl_dataset_phys(ds)->ds_next_snap_obj == 0) { 926 ASSERT(ds->ds_prev == NULL); 927 VERIFY0(dsl_dataset_hold_obj(dp, 928 dsl_dataset_phys(ds)->ds_prev_snap_obj, 929 ds, &ds->ds_prev)); 930 } 931 } 932 933 ASSERT3U(dsl_dir_phys(ds->ds_dir)->dd_origin_obj, ==, prev->ds_object); 934 ASSERT3U(dsl_dataset_phys(ds)->ds_prev_snap_obj, ==, prev->ds_object); 935 936 if (dsl_dataset_phys(prev)->ds_next_clones_obj == 0) { 937 dmu_buf_will_dirty(prev->ds_dbuf, tx); 938 dsl_dataset_phys(prev)->ds_next_clones_obj = 939 zap_create(dp->dp_meta_objset, 940 DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx); 941 } 942 VERIFY0(zap_add_int(dp->dp_meta_objset, 943 dsl_dataset_phys(prev)->ds_next_clones_obj, ds->ds_object, tx)); 944 945 dsl_dataset_rele(ds, FTAG); 946 if (prev != dp->dp_origin_snap) 947 dsl_dataset_rele(prev, FTAG); 948 return (0); 949 } 950 951 void 952 dsl_pool_upgrade_clones(dsl_pool_t *dp, dmu_tx_t *tx) 953 { 954 ASSERT(dmu_tx_is_syncing(tx)); 955 ASSERT(dp->dp_origin_snap != NULL); 956 957 VERIFY0(dmu_objset_find_dp(dp, dp->dp_root_dir_obj, upgrade_clones_cb, 958 tx, DS_FIND_CHILDREN | DS_FIND_SERIALIZE)); 959 } 960 961 /* ARGSUSED */ 962 static int 963 upgrade_dir_clones_cb(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg) 964 { 965 dmu_tx_t *tx = arg; 966 objset_t *mos = dp->dp_meta_objset; 967 968 if (dsl_dir_phys(ds->ds_dir)->dd_origin_obj != 0) { 969 dsl_dataset_t *origin; 970 971 VERIFY0(dsl_dataset_hold_obj(dp, 972 dsl_dir_phys(ds->ds_dir)->dd_origin_obj, FTAG, &origin)); 973 974 if (dsl_dir_phys(origin->ds_dir)->dd_clones == 0) { 975 dmu_buf_will_dirty(origin->ds_dir->dd_dbuf, tx); 976 dsl_dir_phys(origin->ds_dir)->dd_clones = 977 zap_create(mos, DMU_OT_DSL_CLONES, DMU_OT_NONE, 978 0, tx); 979 } 980 981 VERIFY0(zap_add_int(dp->dp_meta_objset, 982 dsl_dir_phys(origin->ds_dir)->dd_clones, 983 ds->ds_object, tx)); 984 985 dsl_dataset_rele(origin, FTAG); 986 } 987 return (0); 988 } 989 990 void 991 dsl_pool_upgrade_dir_clones(dsl_pool_t *dp, dmu_tx_t *tx) 992 { 993 ASSERT(dmu_tx_is_syncing(tx)); 994 uint64_t obj; 995 996 (void) dsl_dir_create_sync(dp, dp->dp_root_dir, FREE_DIR_NAME, tx); 997 VERIFY0(dsl_pool_open_special_dir(dp, 998 FREE_DIR_NAME, &dp->dp_free_dir)); 999 1000 /* 1001 * We can't use bpobj_alloc(), because spa_version() still 1002 * returns the old version, and we need a new-version bpobj with 1003 * subobj support. So call dmu_object_alloc() directly. 1004 */ 1005 obj = dmu_object_alloc(dp->dp_meta_objset, DMU_OT_BPOBJ, 1006 SPA_OLD_MAXBLOCKSIZE, DMU_OT_BPOBJ_HDR, sizeof (bpobj_phys_t), tx); 1007 VERIFY0(zap_add(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 1008 DMU_POOL_FREE_BPOBJ, sizeof (uint64_t), 1, &obj, tx)); 1009 VERIFY0(bpobj_open(&dp->dp_free_bpobj, dp->dp_meta_objset, obj)); 1010 1011 VERIFY0(dmu_objset_find_dp(dp, dp->dp_root_dir_obj, 1012 upgrade_dir_clones_cb, tx, DS_FIND_CHILDREN | DS_FIND_SERIALIZE)); 1013 } 1014 1015 void 1016 dsl_pool_create_origin(dsl_pool_t *dp, dmu_tx_t *tx) 1017 { 1018 uint64_t dsobj; 1019 dsl_dataset_t *ds; 1020 1021 ASSERT(dmu_tx_is_syncing(tx)); 1022 ASSERT(dp->dp_origin_snap == NULL); 1023 ASSERT(rrw_held(&dp->dp_config_rwlock, RW_WRITER)); 1024 1025 /* create the origin dir, ds, & snap-ds */ 1026 dsobj = dsl_dataset_create_sync(dp->dp_root_dir, ORIGIN_DIR_NAME, 1027 NULL, 0, kcred, tx); 1028 VERIFY0(dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds)); 1029 dsl_dataset_snapshot_sync_impl(ds, ORIGIN_DIR_NAME, tx); 1030 VERIFY0(dsl_dataset_hold_obj(dp, dsl_dataset_phys(ds)->ds_prev_snap_obj, 1031 dp, &dp->dp_origin_snap)); 1032 dsl_dataset_rele(ds, FTAG); 1033 } 1034 1035 taskq_t * 1036 dsl_pool_vnrele_taskq(dsl_pool_t *dp) 1037 { 1038 return (dp->dp_vnrele_taskq); 1039 } 1040 1041 /* 1042 * Walk through the pool-wide zap object of temporary snapshot user holds 1043 * and release them. 1044 */ 1045 void 1046 dsl_pool_clean_tmp_userrefs(dsl_pool_t *dp) 1047 { 1048 zap_attribute_t za; 1049 zap_cursor_t zc; 1050 objset_t *mos = dp->dp_meta_objset; 1051 uint64_t zapobj = dp->dp_tmp_userrefs_obj; 1052 nvlist_t *holds; 1053 1054 if (zapobj == 0) 1055 return; 1056 ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS); 1057 1058 holds = fnvlist_alloc(); 1059 1060 for (zap_cursor_init(&zc, mos, zapobj); 1061 zap_cursor_retrieve(&zc, &za) == 0; 1062 zap_cursor_advance(&zc)) { 1063 char *htag; 1064 nvlist_t *tags; 1065 1066 htag = strchr(za.za_name, '-'); 1067 *htag = '\0'; 1068 ++htag; 1069 if (nvlist_lookup_nvlist(holds, za.za_name, &tags) != 0) { 1070 tags = fnvlist_alloc(); 1071 fnvlist_add_boolean(tags, htag); 1072 fnvlist_add_nvlist(holds, za.za_name, tags); 1073 fnvlist_free(tags); 1074 } else { 1075 fnvlist_add_boolean(tags, htag); 1076 } 1077 } 1078 dsl_dataset_user_release_tmp(dp, holds); 1079 fnvlist_free(holds); 1080 zap_cursor_fini(&zc); 1081 } 1082 1083 /* 1084 * Create the pool-wide zap object for storing temporary snapshot holds. 1085 */ 1086 void 1087 dsl_pool_user_hold_create_obj(dsl_pool_t *dp, dmu_tx_t *tx) 1088 { 1089 objset_t *mos = dp->dp_meta_objset; 1090 1091 ASSERT(dp->dp_tmp_userrefs_obj == 0); 1092 ASSERT(dmu_tx_is_syncing(tx)); 1093 1094 dp->dp_tmp_userrefs_obj = zap_create_link(mos, DMU_OT_USERREFS, 1095 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_TMP_USERREFS, tx); 1096 } 1097 1098 static int 1099 dsl_pool_user_hold_rele_impl(dsl_pool_t *dp, uint64_t dsobj, 1100 const char *tag, uint64_t now, dmu_tx_t *tx, boolean_t holding) 1101 { 1102 objset_t *mos = dp->dp_meta_objset; 1103 uint64_t zapobj = dp->dp_tmp_userrefs_obj; 1104 char *name; 1105 int error; 1106 1107 ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS); 1108 ASSERT(dmu_tx_is_syncing(tx)); 1109 1110 /* 1111 * If the pool was created prior to SPA_VERSION_USERREFS, the 1112 * zap object for temporary holds might not exist yet. 1113 */ 1114 if (zapobj == 0) { 1115 if (holding) { 1116 dsl_pool_user_hold_create_obj(dp, tx); 1117 zapobj = dp->dp_tmp_userrefs_obj; 1118 } else { 1119 return (SET_ERROR(ENOENT)); 1120 } 1121 } 1122 1123 name = kmem_asprintf("%llx-%s", (u_longlong_t)dsobj, tag); 1124 if (holding) 1125 error = zap_add(mos, zapobj, name, 8, 1, &now, tx); 1126 else 1127 error = zap_remove(mos, zapobj, name, tx); 1128 strfree(name); 1129 1130 return (error); 1131 } 1132 1133 /* 1134 * Add a temporary hold for the given dataset object and tag. 1135 */ 1136 int 1137 dsl_pool_user_hold(dsl_pool_t *dp, uint64_t dsobj, const char *tag, 1138 uint64_t now, dmu_tx_t *tx) 1139 { 1140 return (dsl_pool_user_hold_rele_impl(dp, dsobj, tag, now, tx, B_TRUE)); 1141 } 1142 1143 /* 1144 * Release a temporary hold for the given dataset object and tag. 1145 */ 1146 int 1147 dsl_pool_user_release(dsl_pool_t *dp, uint64_t dsobj, const char *tag, 1148 dmu_tx_t *tx) 1149 { 1150 return (dsl_pool_user_hold_rele_impl(dp, dsobj, tag, NULL, 1151 tx, B_FALSE)); 1152 } 1153 1154 /* 1155 * DSL Pool Configuration Lock 1156 * 1157 * The dp_config_rwlock protects against changes to DSL state (e.g. dataset 1158 * creation / destruction / rename / property setting). It must be held for 1159 * read to hold a dataset or dsl_dir. I.e. you must call 1160 * dsl_pool_config_enter() or dsl_pool_hold() before calling 1161 * dsl_{dataset,dir}_hold{_obj}. In most circumstances, the dp_config_rwlock 1162 * must be held continuously until all datasets and dsl_dirs are released. 1163 * 1164 * The only exception to this rule is that if a "long hold" is placed on 1165 * a dataset, then the dp_config_rwlock may be dropped while the dataset 1166 * is still held. The long hold will prevent the dataset from being 1167 * destroyed -- the destroy will fail with EBUSY. A long hold can be 1168 * obtained by calling dsl_dataset_long_hold(), or by "owning" a dataset 1169 * (by calling dsl_{dataset,objset}_{try}own{_obj}). 1170 * 1171 * Legitimate long-holders (including owners) should be long-running, cancelable 1172 * tasks that should cause "zfs destroy" to fail. This includes DMU 1173 * consumers (i.e. a ZPL filesystem being mounted or ZVOL being open), 1174 * "zfs send", and "zfs diff". There are several other long-holders whose 1175 * uses are suboptimal (e.g. "zfs promote", and zil_suspend()). 1176 * 1177 * The usual formula for long-holding would be: 1178 * dsl_pool_hold() 1179 * dsl_dataset_hold() 1180 * ... perform checks ... 1181 * dsl_dataset_long_hold() 1182 * dsl_pool_rele() 1183 * ... perform long-running task ... 1184 * dsl_dataset_long_rele() 1185 * dsl_dataset_rele() 1186 * 1187 * Note that when the long hold is released, the dataset is still held but 1188 * the pool is not held. The dataset may change arbitrarily during this time 1189 * (e.g. it could be destroyed). Therefore you shouldn't do anything to the 1190 * dataset except release it. 1191 * 1192 * User-initiated operations (e.g. ioctls, zfs_ioc_*()) are either read-only 1193 * or modifying operations. 1194 * 1195 * Modifying operations should generally use dsl_sync_task(). The synctask 1196 * infrastructure enforces proper locking strategy with respect to the 1197 * dp_config_rwlock. See the comment above dsl_sync_task() for details. 1198 * 1199 * Read-only operations will manually hold the pool, then the dataset, obtain 1200 * information from the dataset, then release the pool and dataset. 1201 * dmu_objset_{hold,rele}() are convenience routines that also do the pool 1202 * hold/rele. 1203 */ 1204 1205 int 1206 dsl_pool_hold(const char *name, void *tag, dsl_pool_t **dp) 1207 { 1208 spa_t *spa; 1209 int error; 1210 1211 error = spa_open(name, &spa, tag); 1212 if (error == 0) { 1213 *dp = spa_get_dsl(spa); 1214 dsl_pool_config_enter(*dp, tag); 1215 } 1216 return (error); 1217 } 1218 1219 void 1220 dsl_pool_rele(dsl_pool_t *dp, void *tag) 1221 { 1222 dsl_pool_config_exit(dp, tag); 1223 spa_close(dp->dp_spa, tag); 1224 } 1225 1226 void 1227 dsl_pool_config_enter(dsl_pool_t *dp, void *tag) 1228 { 1229 /* 1230 * We use a "reentrant" reader-writer lock, but not reentrantly. 1231 * 1232 * The rrwlock can (with the track_all flag) track all reading threads, 1233 * which is very useful for debugging which code path failed to release 1234 * the lock, and for verifying that the *current* thread does hold 1235 * the lock. 1236 * 1237 * (Unlike a rwlock, which knows that N threads hold it for 1238 * read, but not *which* threads, so rw_held(RW_READER) returns TRUE 1239 * if any thread holds it for read, even if this thread doesn't). 1240 */ 1241 ASSERT(!rrw_held(&dp->dp_config_rwlock, RW_READER)); 1242 rrw_enter(&dp->dp_config_rwlock, RW_READER, tag); 1243 } 1244 1245 void 1246 dsl_pool_config_enter_prio(dsl_pool_t *dp, void *tag) 1247 { 1248 ASSERT(!rrw_held(&dp->dp_config_rwlock, RW_READER)); 1249 rrw_enter_read_prio(&dp->dp_config_rwlock, tag); 1250 } 1251 1252 void 1253 dsl_pool_config_exit(dsl_pool_t *dp, void *tag) 1254 { 1255 rrw_exit(&dp->dp_config_rwlock, tag); 1256 } 1257 1258 boolean_t 1259 dsl_pool_config_held(dsl_pool_t *dp) 1260 { 1261 return (RRW_LOCK_HELD(&dp->dp_config_rwlock)); 1262 } 1263 1264 boolean_t 1265 dsl_pool_config_held_writer(dsl_pool_t *dp) 1266 { 1267 return (RRW_WRITE_HELD(&dp->dp_config_rwlock)); 1268 } 1269