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/bptree.h> 48 #include <sys/zfeature.h> 49 #include <sys/zil_impl.h> 50 #include <sys/dsl_userhold.h> 51 52 /* 53 * ZFS Write Throttle 54 * ------------------ 55 * 56 * ZFS must limit the rate of incoming writes to the rate at which it is able 57 * to sync data modifications to the backend storage. Throttling by too much 58 * creates an artificial limit; throttling by too little can only be sustained 59 * for short periods and would lead to highly lumpy performance. On a per-pool 60 * basis, ZFS tracks the amount of modified (dirty) data. As operations change 61 * data, the amount of dirty data increases; as ZFS syncs out data, the amount 62 * of dirty data decreases. When the amount of dirty data exceeds a 63 * predetermined threshold further modifications are blocked until the amount 64 * of dirty data decreases (as data is synced out). 65 * 66 * The limit on dirty data is tunable, and should be adjusted according to 67 * both the IO capacity and available memory of the system. The larger the 68 * window, the more ZFS is able to aggregate and amortize metadata (and data) 69 * changes. However, memory is a limited resource, and allowing for more dirty 70 * data comes at the cost of keeping other useful data in memory (for example 71 * ZFS data cached by the ARC). 72 * 73 * Implementation 74 * 75 * As buffers are modified dsl_pool_willuse_space() increments both the per- 76 * txg (dp_dirty_pertxg[]) and poolwide (dp_dirty_total) accounting of 77 * dirty space used; dsl_pool_dirty_space() decrements those values as data 78 * is synced out from dsl_pool_sync(). While only the poolwide value is 79 * relevant, the per-txg value is useful for debugging. The tunable 80 * zfs_dirty_data_max determines the dirty space limit. Once that value is 81 * exceeded, new writes are halted until space frees up. 82 * 83 * The zfs_dirty_data_sync tunable dictates the threshold at which we 84 * ensure that there is a txg syncing (see the comment in txg.c for a full 85 * description of transaction group stages). 86 * 87 * The IO scheduler uses both the dirty space limit and current amount of 88 * dirty data as inputs. Those values affect the number of concurrent IOs ZFS 89 * issues. See the comment in vdev_queue.c for details of the IO scheduler. 90 * 91 * The delay is also calculated based on the amount of dirty data. See the 92 * comment above dmu_tx_delay() for details. 93 */ 94 95 /* 96 * zfs_dirty_data_max will be set to zfs_dirty_data_max_percent% of all memory, 97 * capped at zfs_dirty_data_max_max. It can also be overridden in /etc/system. 98 */ 99 uint64_t zfs_dirty_data_max; 100 uint64_t zfs_dirty_data_max_max = 4ULL * 1024 * 1024 * 1024; 101 int zfs_dirty_data_max_percent = 10; 102 103 /* 104 * If there is at least this much dirty data, push out a txg. 105 */ 106 uint64_t zfs_dirty_data_sync = 64 * 1024 * 1024; 107 108 /* 109 * Once there is this amount of dirty data, the dmu_tx_delay() will kick in 110 * and delay each transaction. 111 * This value should be >= zfs_vdev_async_write_active_max_dirty_percent. 112 */ 113 int zfs_delay_min_dirty_percent = 60; 114 115 /* 116 * This controls how quickly the delay approaches infinity. 117 * Larger values cause it to delay more for a given amount of dirty data. 118 * Therefore larger values will cause there to be less dirty data for a 119 * given throughput. 120 * 121 * For the smoothest delay, this value should be about 1 billion divided 122 * by the maximum number of operations per second. This will smoothly 123 * handle between 10x and 1/10th this number. 124 * 125 * Note: zfs_delay_scale * zfs_dirty_data_max must be < 2^64, due to the 126 * multiply in dmu_tx_delay(). 127 */ 128 uint64_t zfs_delay_scale = 1000 * 1000 * 1000 / 2000; 129 130 /* 131 * This determines the number of threads used by the dp_sync_taskq. 132 */ 133 int zfs_sync_taskq_batch_pct = 75; 134 135 int 136 dsl_pool_open_special_dir(dsl_pool_t *dp, const char *name, dsl_dir_t **ddp) 137 { 138 uint64_t obj; 139 int err; 140 141 err = zap_lookup(dp->dp_meta_objset, 142 dsl_dir_phys(dp->dp_root_dir)->dd_child_dir_zapobj, 143 name, sizeof (obj), 1, &obj); 144 if (err) 145 return (err); 146 147 return (dsl_dir_hold_obj(dp, obj, name, dp, ddp)); 148 } 149 150 static dsl_pool_t * 151 dsl_pool_open_impl(spa_t *spa, uint64_t txg) 152 { 153 dsl_pool_t *dp; 154 blkptr_t *bp = spa_get_rootblkptr(spa); 155 156 dp = kmem_zalloc(sizeof (dsl_pool_t), KM_SLEEP); 157 dp->dp_spa = spa; 158 dp->dp_meta_rootbp = *bp; 159 rrw_init(&dp->dp_config_rwlock, B_TRUE); 160 txg_init(dp, txg); 161 162 txg_list_create(&dp->dp_dirty_datasets, spa, 163 offsetof(dsl_dataset_t, ds_dirty_link)); 164 txg_list_create(&dp->dp_dirty_zilogs, spa, 165 offsetof(zilog_t, zl_dirty_link)); 166 txg_list_create(&dp->dp_dirty_dirs, spa, 167 offsetof(dsl_dir_t, dd_dirty_link)); 168 txg_list_create(&dp->dp_sync_tasks, spa, 169 offsetof(dsl_sync_task_t, dst_node)); 170 171 dp->dp_sync_taskq = taskq_create("dp_sync_taskq", 172 zfs_sync_taskq_batch_pct, minclsyspri, 1, INT_MAX, 173 TASKQ_THREADS_CPU_PCT); 174 175 mutex_init(&dp->dp_lock, NULL, MUTEX_DEFAULT, NULL); 176 cv_init(&dp->dp_spaceavail_cv, NULL, CV_DEFAULT, NULL); 177 178 dp->dp_vnrele_taskq = taskq_create("zfs_vn_rele_taskq", 1, minclsyspri, 179 1, 4, 0); 180 181 return (dp); 182 } 183 184 int 185 dsl_pool_init(spa_t *spa, uint64_t txg, dsl_pool_t **dpp) 186 { 187 int err; 188 dsl_pool_t *dp = dsl_pool_open_impl(spa, txg); 189 190 err = dmu_objset_open_impl(spa, NULL, &dp->dp_meta_rootbp, 191 &dp->dp_meta_objset); 192 if (err != 0) 193 dsl_pool_close(dp); 194 else 195 *dpp = dp; 196 197 return (err); 198 } 199 200 int 201 dsl_pool_open(dsl_pool_t *dp) 202 { 203 int err; 204 dsl_dir_t *dd; 205 dsl_dataset_t *ds; 206 uint64_t obj; 207 208 rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG); 209 err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 210 DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1, 211 &dp->dp_root_dir_obj); 212 if (err) 213 goto out; 214 215 err = dsl_dir_hold_obj(dp, dp->dp_root_dir_obj, 216 NULL, dp, &dp->dp_root_dir); 217 if (err) 218 goto out; 219 220 err = dsl_pool_open_special_dir(dp, MOS_DIR_NAME, &dp->dp_mos_dir); 221 if (err) 222 goto out; 223 224 if (spa_version(dp->dp_spa) >= SPA_VERSION_ORIGIN) { 225 err = dsl_pool_open_special_dir(dp, ORIGIN_DIR_NAME, &dd); 226 if (err) 227 goto out; 228 err = dsl_dataset_hold_obj(dp, 229 dsl_dir_phys(dd)->dd_head_dataset_obj, FTAG, &ds); 230 if (err == 0) { 231 err = dsl_dataset_hold_obj(dp, 232 dsl_dataset_phys(ds)->ds_prev_snap_obj, dp, 233 &dp->dp_origin_snap); 234 dsl_dataset_rele(ds, FTAG); 235 } 236 dsl_dir_rele(dd, dp); 237 if (err) 238 goto out; 239 } 240 241 if (spa_version(dp->dp_spa) >= SPA_VERSION_DEADLISTS) { 242 err = dsl_pool_open_special_dir(dp, FREE_DIR_NAME, 243 &dp->dp_free_dir); 244 if (err) 245 goto out; 246 247 err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 248 DMU_POOL_FREE_BPOBJ, sizeof (uint64_t), 1, &obj); 249 if (err) 250 goto out; 251 VERIFY0(bpobj_open(&dp->dp_free_bpobj, 252 dp->dp_meta_objset, obj)); 253 } 254 255 /* 256 * Note: errors ignored, because the leak dir will not exist if we 257 * have not encountered a leak yet. 258 */ 259 (void) dsl_pool_open_special_dir(dp, LEAK_DIR_NAME, 260 &dp->dp_leak_dir); 261 262 if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_ASYNC_DESTROY)) { 263 err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 264 DMU_POOL_BPTREE_OBJ, sizeof (uint64_t), 1, 265 &dp->dp_bptree_obj); 266 if (err != 0) 267 goto out; 268 } 269 270 if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_EMPTY_BPOBJ)) { 271 err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 272 DMU_POOL_EMPTY_BPOBJ, sizeof (uint64_t), 1, 273 &dp->dp_empty_bpobj); 274 if (err != 0) 275 goto out; 276 } 277 278 err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 279 DMU_POOL_TMP_USERREFS, sizeof (uint64_t), 1, 280 &dp->dp_tmp_userrefs_obj); 281 if (err == ENOENT) 282 err = 0; 283 if (err) 284 goto out; 285 286 err = dsl_scan_init(dp, dp->dp_tx.tx_open_txg); 287 288 out: 289 rrw_exit(&dp->dp_config_rwlock, FTAG); 290 return (err); 291 } 292 293 void 294 dsl_pool_close(dsl_pool_t *dp) 295 { 296 /* 297 * Drop our references from dsl_pool_open(). 298 * 299 * Since we held the origin_snap from "syncing" context (which 300 * includes pool-opening context), it actually only got a "ref" 301 * and not a hold, so just drop that here. 302 */ 303 if (dp->dp_origin_snap) 304 dsl_dataset_rele(dp->dp_origin_snap, dp); 305 if (dp->dp_mos_dir) 306 dsl_dir_rele(dp->dp_mos_dir, dp); 307 if (dp->dp_free_dir) 308 dsl_dir_rele(dp->dp_free_dir, dp); 309 if (dp->dp_leak_dir) 310 dsl_dir_rele(dp->dp_leak_dir, dp); 311 if (dp->dp_root_dir) 312 dsl_dir_rele(dp->dp_root_dir, dp); 313 314 bpobj_close(&dp->dp_free_bpobj); 315 316 /* undo the dmu_objset_open_impl(mos) from dsl_pool_open() */ 317 if (dp->dp_meta_objset) 318 dmu_objset_evict(dp->dp_meta_objset); 319 320 txg_list_destroy(&dp->dp_dirty_datasets); 321 txg_list_destroy(&dp->dp_dirty_zilogs); 322 txg_list_destroy(&dp->dp_sync_tasks); 323 txg_list_destroy(&dp->dp_dirty_dirs); 324 325 taskq_destroy(dp->dp_sync_taskq); 326 327 /* 328 * We can't set retry to TRUE since we're explicitly specifying 329 * a spa to flush. This is good enough; any missed buffers for 330 * this spa won't cause trouble, and they'll eventually fall 331 * out of the ARC just like any other unused buffer. 332 */ 333 arc_flush(dp->dp_spa, FALSE); 334 335 txg_fini(dp); 336 dsl_scan_fini(dp); 337 dmu_buf_user_evict_wait(); 338 339 rrw_destroy(&dp->dp_config_rwlock); 340 mutex_destroy(&dp->dp_lock); 341 taskq_destroy(dp->dp_vnrele_taskq); 342 if (dp->dp_blkstats) 343 kmem_free(dp->dp_blkstats, sizeof (zfs_all_blkstats_t)); 344 kmem_free(dp, sizeof (dsl_pool_t)); 345 } 346 347 dsl_pool_t * 348 dsl_pool_create(spa_t *spa, nvlist_t *zplprops, uint64_t txg) 349 { 350 int err; 351 dsl_pool_t *dp = dsl_pool_open_impl(spa, txg); 352 dmu_tx_t *tx = dmu_tx_create_assigned(dp, txg); 353 objset_t *os; 354 dsl_dataset_t *ds; 355 uint64_t obj; 356 357 rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG); 358 359 /* create and open the MOS (meta-objset) */ 360 dp->dp_meta_objset = dmu_objset_create_impl(spa, 361 NULL, &dp->dp_meta_rootbp, DMU_OST_META, tx); 362 363 /* create the pool directory */ 364 err = zap_create_claim(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 365 DMU_OT_OBJECT_DIRECTORY, DMU_OT_NONE, 0, tx); 366 ASSERT0(err); 367 368 /* Initialize scan structures */ 369 VERIFY0(dsl_scan_init(dp, txg)); 370 371 /* create and open the root dir */ 372 dp->dp_root_dir_obj = dsl_dir_create_sync(dp, NULL, NULL, tx); 373 VERIFY0(dsl_dir_hold_obj(dp, dp->dp_root_dir_obj, 374 NULL, dp, &dp->dp_root_dir)); 375 376 /* create and open the meta-objset dir */ 377 (void) dsl_dir_create_sync(dp, dp->dp_root_dir, MOS_DIR_NAME, tx); 378 VERIFY0(dsl_pool_open_special_dir(dp, 379 MOS_DIR_NAME, &dp->dp_mos_dir)); 380 381 if (spa_version(spa) >= SPA_VERSION_DEADLISTS) { 382 /* create and open the free dir */ 383 (void) dsl_dir_create_sync(dp, dp->dp_root_dir, 384 FREE_DIR_NAME, tx); 385 VERIFY0(dsl_pool_open_special_dir(dp, 386 FREE_DIR_NAME, &dp->dp_free_dir)); 387 388 /* create and open the free_bplist */ 389 obj = bpobj_alloc(dp->dp_meta_objset, SPA_OLD_MAXBLOCKSIZE, tx); 390 VERIFY(zap_add(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 391 DMU_POOL_FREE_BPOBJ, sizeof (uint64_t), 1, &obj, tx) == 0); 392 VERIFY0(bpobj_open(&dp->dp_free_bpobj, 393 dp->dp_meta_objset, obj)); 394 } 395 396 if (spa_version(spa) >= SPA_VERSION_DSL_SCRUB) 397 dsl_pool_create_origin(dp, tx); 398 399 /* create the root dataset */ 400 obj = dsl_dataset_create_sync_dd(dp->dp_root_dir, NULL, 0, tx); 401 402 /* create the root objset */ 403 VERIFY0(dsl_dataset_hold_obj(dp, obj, FTAG, &ds)); 404 rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG); 405 os = dmu_objset_create_impl(dp->dp_spa, ds, 406 dsl_dataset_get_blkptr(ds), DMU_OST_ZFS, tx); 407 rrw_exit(&ds->ds_bp_rwlock, FTAG); 408 #ifdef _KERNEL 409 zfs_create_fs(os, kcred, zplprops, tx); 410 #endif 411 dsl_dataset_rele(ds, FTAG); 412 413 dmu_tx_commit(tx); 414 415 rrw_exit(&dp->dp_config_rwlock, FTAG); 416 417 return (dp); 418 } 419 420 /* 421 * Account for the meta-objset space in its placeholder dsl_dir. 422 */ 423 void 424 dsl_pool_mos_diduse_space(dsl_pool_t *dp, 425 int64_t used, int64_t comp, int64_t uncomp) 426 { 427 ASSERT3U(comp, ==, uncomp); /* it's all metadata */ 428 mutex_enter(&dp->dp_lock); 429 dp->dp_mos_used_delta += used; 430 dp->dp_mos_compressed_delta += comp; 431 dp->dp_mos_uncompressed_delta += uncomp; 432 mutex_exit(&dp->dp_lock); 433 } 434 435 static void 436 dsl_pool_sync_mos(dsl_pool_t *dp, dmu_tx_t *tx) 437 { 438 zio_t *zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED); 439 dmu_objset_sync(dp->dp_meta_objset, zio, tx); 440 VERIFY0(zio_wait(zio)); 441 dprintf_bp(&dp->dp_meta_rootbp, "meta objset rootbp is %s", ""); 442 spa_set_rootblkptr(dp->dp_spa, &dp->dp_meta_rootbp); 443 } 444 445 static void 446 dsl_pool_dirty_delta(dsl_pool_t *dp, int64_t delta) 447 { 448 ASSERT(MUTEX_HELD(&dp->dp_lock)); 449 450 if (delta < 0) 451 ASSERT3U(-delta, <=, dp->dp_dirty_total); 452 453 dp->dp_dirty_total += delta; 454 455 /* 456 * Note: we signal even when increasing dp_dirty_total. 457 * This ensures forward progress -- each thread wakes the next waiter. 458 */ 459 if (dp->dp_dirty_total < zfs_dirty_data_max) 460 cv_signal(&dp->dp_spaceavail_cv); 461 } 462 463 void 464 dsl_pool_sync(dsl_pool_t *dp, uint64_t txg) 465 { 466 zio_t *zio; 467 dmu_tx_t *tx; 468 dsl_dir_t *dd; 469 dsl_dataset_t *ds; 470 objset_t *mos = dp->dp_meta_objset; 471 list_t synced_datasets; 472 473 list_create(&synced_datasets, sizeof (dsl_dataset_t), 474 offsetof(dsl_dataset_t, ds_synced_link)); 475 476 tx = dmu_tx_create_assigned(dp, txg); 477 478 /* 479 * Write out all dirty blocks of dirty datasets. 480 */ 481 zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED); 482 while ((ds = txg_list_remove(&dp->dp_dirty_datasets, txg)) != NULL) { 483 /* 484 * We must not sync any non-MOS datasets twice, because 485 * we may have taken a snapshot of them. However, we 486 * may sync newly-created datasets on pass 2. 487 */ 488 ASSERT(!list_link_active(&ds->ds_synced_link)); 489 list_insert_tail(&synced_datasets, ds); 490 dsl_dataset_sync(ds, zio, tx); 491 } 492 VERIFY0(zio_wait(zio)); 493 494 /* 495 * We have written all of the accounted dirty data, so our 496 * dp_space_towrite should now be zero. However, some seldom-used 497 * code paths do not adhere to this (e.g. dbuf_undirty(), also 498 * rounding error in dbuf_write_physdone). 499 * Shore up the accounting of any dirtied space now. 500 */ 501 dsl_pool_undirty_space(dp, dp->dp_dirty_pertxg[txg & TXG_MASK], txg); 502 503 /* 504 * Update the long range free counter after 505 * we're done syncing user data 506 */ 507 mutex_enter(&dp->dp_lock); 508 ASSERT(spa_sync_pass(dp->dp_spa) == 1 || 509 dp->dp_long_free_dirty_pertxg[txg & TXG_MASK] == 0); 510 dp->dp_long_free_dirty_pertxg[txg & TXG_MASK] = 0; 511 mutex_exit(&dp->dp_lock); 512 513 /* 514 * After the data blocks have been written (ensured by the zio_wait() 515 * above), update the user/group space accounting. This happens 516 * in tasks dispatched to dp_sync_taskq, so wait for them before 517 * continuing. 518 */ 519 for (ds = list_head(&synced_datasets); ds != NULL; 520 ds = list_next(&synced_datasets, ds)) { 521 dmu_objset_do_userquota_updates(ds->ds_objset, tx); 522 } 523 taskq_wait(dp->dp_sync_taskq); 524 525 /* 526 * Sync the datasets again to push out the changes due to 527 * userspace updates. This must be done before we process the 528 * sync tasks, so that any snapshots will have the correct 529 * user accounting information (and we won't get confused 530 * about which blocks are part of the snapshot). 531 */ 532 zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED); 533 while ((ds = txg_list_remove(&dp->dp_dirty_datasets, txg)) != NULL) { 534 ASSERT(list_link_active(&ds->ds_synced_link)); 535 dmu_buf_rele(ds->ds_dbuf, ds); 536 dsl_dataset_sync(ds, zio, tx); 537 } 538 VERIFY0(zio_wait(zio)); 539 540 /* 541 * Now that the datasets have been completely synced, we can 542 * clean up our in-memory structures accumulated while syncing: 543 * 544 * - move dead blocks from the pending deadlist to the on-disk deadlist 545 * - release hold from dsl_dataset_dirty() 546 */ 547 while ((ds = list_remove_head(&synced_datasets)) != NULL) { 548 dsl_dataset_sync_done(ds, tx); 549 } 550 while ((dd = txg_list_remove(&dp->dp_dirty_dirs, txg)) != NULL) { 551 dsl_dir_sync(dd, tx); 552 } 553 554 /* 555 * The MOS's space is accounted for in the pool/$MOS 556 * (dp_mos_dir). We can't modify the mos while we're syncing 557 * it, so we remember the deltas and apply them here. 558 */ 559 if (dp->dp_mos_used_delta != 0 || dp->dp_mos_compressed_delta != 0 || 560 dp->dp_mos_uncompressed_delta != 0) { 561 dsl_dir_diduse_space(dp->dp_mos_dir, DD_USED_HEAD, 562 dp->dp_mos_used_delta, 563 dp->dp_mos_compressed_delta, 564 dp->dp_mos_uncompressed_delta, tx); 565 dp->dp_mos_used_delta = 0; 566 dp->dp_mos_compressed_delta = 0; 567 dp->dp_mos_uncompressed_delta = 0; 568 } 569 570 if (!multilist_is_empty(mos->os_dirty_dnodes[txg & TXG_MASK])) { 571 dsl_pool_sync_mos(dp, tx); 572 } 573 574 /* 575 * If we modify a dataset in the same txg that we want to destroy it, 576 * its dsl_dir's dd_dbuf will be dirty, and thus have a hold on it. 577 * dsl_dir_destroy_check() will fail if there are unexpected holds. 578 * Therefore, we want to sync the MOS (thus syncing the dd_dbuf 579 * and clearing the hold on it) before we process the sync_tasks. 580 * The MOS data dirtied by the sync_tasks will be synced on the next 581 * pass. 582 */ 583 if (!txg_list_empty(&dp->dp_sync_tasks, txg)) { 584 dsl_sync_task_t *dst; 585 /* 586 * No more sync tasks should have been added while we 587 * were syncing. 588 */ 589 ASSERT3U(spa_sync_pass(dp->dp_spa), ==, 1); 590 while ((dst = txg_list_remove(&dp->dp_sync_tasks, txg)) != NULL) 591 dsl_sync_task_sync(dst, tx); 592 } 593 594 dmu_tx_commit(tx); 595 596 DTRACE_PROBE2(dsl_pool_sync__done, dsl_pool_t *dp, dp, uint64_t, txg); 597 } 598 599 void 600 dsl_pool_sync_done(dsl_pool_t *dp, uint64_t txg) 601 { 602 zilog_t *zilog; 603 604 while (zilog = txg_list_head(&dp->dp_dirty_zilogs, txg)) { 605 dsl_dataset_t *ds = dmu_objset_ds(zilog->zl_os); 606 /* 607 * We don't remove the zilog from the dp_dirty_zilogs 608 * list until after we've cleaned it. This ensures that 609 * callers of zilog_is_dirty() receive an accurate 610 * answer when they are racing with the spa sync thread. 611 */ 612 zil_clean(zilog, txg); 613 (void) txg_list_remove_this(&dp->dp_dirty_zilogs, zilog, txg); 614 ASSERT(!dmu_objset_is_dirty(zilog->zl_os, txg)); 615 dmu_buf_rele(ds->ds_dbuf, zilog); 616 } 617 ASSERT(!dmu_objset_is_dirty(dp->dp_meta_objset, txg)); 618 } 619 620 /* 621 * TRUE if the current thread is the tx_sync_thread or if we 622 * are being called from SPA context during pool initialization. 623 */ 624 int 625 dsl_pool_sync_context(dsl_pool_t *dp) 626 { 627 return (curthread == dp->dp_tx.tx_sync_thread || 628 spa_is_initializing(dp->dp_spa) || 629 taskq_member(dp->dp_sync_taskq, curthread)); 630 } 631 632 uint64_t 633 dsl_pool_adjustedsize(dsl_pool_t *dp, boolean_t netfree) 634 { 635 uint64_t space, resv; 636 637 /* 638 * If we're trying to assess whether it's OK to do a free, 639 * cut the reservation in half to allow forward progress 640 * (e.g. make it possible to rm(1) files from a full pool). 641 */ 642 space = spa_get_dspace(dp->dp_spa); 643 resv = spa_get_slop_space(dp->dp_spa); 644 if (netfree) 645 resv >>= 1; 646 647 return (space - resv); 648 } 649 650 boolean_t 651 dsl_pool_need_dirty_delay(dsl_pool_t *dp) 652 { 653 uint64_t delay_min_bytes = 654 zfs_dirty_data_max * zfs_delay_min_dirty_percent / 100; 655 boolean_t rv; 656 657 mutex_enter(&dp->dp_lock); 658 if (dp->dp_dirty_total > zfs_dirty_data_sync) 659 txg_kick(dp); 660 rv = (dp->dp_dirty_total > delay_min_bytes); 661 mutex_exit(&dp->dp_lock); 662 return (rv); 663 } 664 665 void 666 dsl_pool_dirty_space(dsl_pool_t *dp, int64_t space, dmu_tx_t *tx) 667 { 668 if (space > 0) { 669 mutex_enter(&dp->dp_lock); 670 dp->dp_dirty_pertxg[tx->tx_txg & TXG_MASK] += space; 671 dsl_pool_dirty_delta(dp, space); 672 mutex_exit(&dp->dp_lock); 673 } 674 } 675 676 void 677 dsl_pool_undirty_space(dsl_pool_t *dp, int64_t space, uint64_t txg) 678 { 679 ASSERT3S(space, >=, 0); 680 if (space == 0) 681 return; 682 mutex_enter(&dp->dp_lock); 683 if (dp->dp_dirty_pertxg[txg & TXG_MASK] < space) { 684 /* XXX writing something we didn't dirty? */ 685 space = dp->dp_dirty_pertxg[txg & TXG_MASK]; 686 } 687 ASSERT3U(dp->dp_dirty_pertxg[txg & TXG_MASK], >=, space); 688 dp->dp_dirty_pertxg[txg & TXG_MASK] -= space; 689 ASSERT3U(dp->dp_dirty_total, >=, space); 690 dsl_pool_dirty_delta(dp, -space); 691 mutex_exit(&dp->dp_lock); 692 } 693 694 /* ARGSUSED */ 695 static int 696 upgrade_clones_cb(dsl_pool_t *dp, dsl_dataset_t *hds, void *arg) 697 { 698 dmu_tx_t *tx = arg; 699 dsl_dataset_t *ds, *prev = NULL; 700 int err; 701 702 err = dsl_dataset_hold_obj(dp, hds->ds_object, FTAG, &ds); 703 if (err) 704 return (err); 705 706 while (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) { 707 err = dsl_dataset_hold_obj(dp, 708 dsl_dataset_phys(ds)->ds_prev_snap_obj, FTAG, &prev); 709 if (err) { 710 dsl_dataset_rele(ds, FTAG); 711 return (err); 712 } 713 714 if (dsl_dataset_phys(prev)->ds_next_snap_obj != ds->ds_object) 715 break; 716 dsl_dataset_rele(ds, FTAG); 717 ds = prev; 718 prev = NULL; 719 } 720 721 if (prev == NULL) { 722 prev = dp->dp_origin_snap; 723 724 /* 725 * The $ORIGIN can't have any data, or the accounting 726 * will be wrong. 727 */ 728 rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG); 729 ASSERT0(dsl_dataset_phys(prev)->ds_bp.blk_birth); 730 rrw_exit(&ds->ds_bp_rwlock, FTAG); 731 732 /* The origin doesn't get attached to itself */ 733 if (ds->ds_object == prev->ds_object) { 734 dsl_dataset_rele(ds, FTAG); 735 return (0); 736 } 737 738 dmu_buf_will_dirty(ds->ds_dbuf, tx); 739 dsl_dataset_phys(ds)->ds_prev_snap_obj = prev->ds_object; 740 dsl_dataset_phys(ds)->ds_prev_snap_txg = 741 dsl_dataset_phys(prev)->ds_creation_txg; 742 743 dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx); 744 dsl_dir_phys(ds->ds_dir)->dd_origin_obj = prev->ds_object; 745 746 dmu_buf_will_dirty(prev->ds_dbuf, tx); 747 dsl_dataset_phys(prev)->ds_num_children++; 748 749 if (dsl_dataset_phys(ds)->ds_next_snap_obj == 0) { 750 ASSERT(ds->ds_prev == NULL); 751 VERIFY0(dsl_dataset_hold_obj(dp, 752 dsl_dataset_phys(ds)->ds_prev_snap_obj, 753 ds, &ds->ds_prev)); 754 } 755 } 756 757 ASSERT3U(dsl_dir_phys(ds->ds_dir)->dd_origin_obj, ==, prev->ds_object); 758 ASSERT3U(dsl_dataset_phys(ds)->ds_prev_snap_obj, ==, prev->ds_object); 759 760 if (dsl_dataset_phys(prev)->ds_next_clones_obj == 0) { 761 dmu_buf_will_dirty(prev->ds_dbuf, tx); 762 dsl_dataset_phys(prev)->ds_next_clones_obj = 763 zap_create(dp->dp_meta_objset, 764 DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx); 765 } 766 VERIFY0(zap_add_int(dp->dp_meta_objset, 767 dsl_dataset_phys(prev)->ds_next_clones_obj, ds->ds_object, tx)); 768 769 dsl_dataset_rele(ds, FTAG); 770 if (prev != dp->dp_origin_snap) 771 dsl_dataset_rele(prev, FTAG); 772 return (0); 773 } 774 775 void 776 dsl_pool_upgrade_clones(dsl_pool_t *dp, dmu_tx_t *tx) 777 { 778 ASSERT(dmu_tx_is_syncing(tx)); 779 ASSERT(dp->dp_origin_snap != NULL); 780 781 VERIFY0(dmu_objset_find_dp(dp, dp->dp_root_dir_obj, upgrade_clones_cb, 782 tx, DS_FIND_CHILDREN | DS_FIND_SERIALIZE)); 783 } 784 785 /* ARGSUSED */ 786 static int 787 upgrade_dir_clones_cb(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg) 788 { 789 dmu_tx_t *tx = arg; 790 objset_t *mos = dp->dp_meta_objset; 791 792 if (dsl_dir_phys(ds->ds_dir)->dd_origin_obj != 0) { 793 dsl_dataset_t *origin; 794 795 VERIFY0(dsl_dataset_hold_obj(dp, 796 dsl_dir_phys(ds->ds_dir)->dd_origin_obj, FTAG, &origin)); 797 798 if (dsl_dir_phys(origin->ds_dir)->dd_clones == 0) { 799 dmu_buf_will_dirty(origin->ds_dir->dd_dbuf, tx); 800 dsl_dir_phys(origin->ds_dir)->dd_clones = 801 zap_create(mos, DMU_OT_DSL_CLONES, DMU_OT_NONE, 802 0, tx); 803 } 804 805 VERIFY0(zap_add_int(dp->dp_meta_objset, 806 dsl_dir_phys(origin->ds_dir)->dd_clones, 807 ds->ds_object, tx)); 808 809 dsl_dataset_rele(origin, FTAG); 810 } 811 return (0); 812 } 813 814 void 815 dsl_pool_upgrade_dir_clones(dsl_pool_t *dp, dmu_tx_t *tx) 816 { 817 ASSERT(dmu_tx_is_syncing(tx)); 818 uint64_t obj; 819 820 (void) dsl_dir_create_sync(dp, dp->dp_root_dir, FREE_DIR_NAME, tx); 821 VERIFY0(dsl_pool_open_special_dir(dp, 822 FREE_DIR_NAME, &dp->dp_free_dir)); 823 824 /* 825 * We can't use bpobj_alloc(), because spa_version() still 826 * returns the old version, and we need a new-version bpobj with 827 * subobj support. So call dmu_object_alloc() directly. 828 */ 829 obj = dmu_object_alloc(dp->dp_meta_objset, DMU_OT_BPOBJ, 830 SPA_OLD_MAXBLOCKSIZE, DMU_OT_BPOBJ_HDR, sizeof (bpobj_phys_t), tx); 831 VERIFY0(zap_add(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 832 DMU_POOL_FREE_BPOBJ, sizeof (uint64_t), 1, &obj, tx)); 833 VERIFY0(bpobj_open(&dp->dp_free_bpobj, dp->dp_meta_objset, obj)); 834 835 VERIFY0(dmu_objset_find_dp(dp, dp->dp_root_dir_obj, 836 upgrade_dir_clones_cb, tx, DS_FIND_CHILDREN | DS_FIND_SERIALIZE)); 837 } 838 839 void 840 dsl_pool_create_origin(dsl_pool_t *dp, dmu_tx_t *tx) 841 { 842 uint64_t dsobj; 843 dsl_dataset_t *ds; 844 845 ASSERT(dmu_tx_is_syncing(tx)); 846 ASSERT(dp->dp_origin_snap == NULL); 847 ASSERT(rrw_held(&dp->dp_config_rwlock, RW_WRITER)); 848 849 /* create the origin dir, ds, & snap-ds */ 850 dsobj = dsl_dataset_create_sync(dp->dp_root_dir, ORIGIN_DIR_NAME, 851 NULL, 0, kcred, tx); 852 VERIFY0(dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds)); 853 dsl_dataset_snapshot_sync_impl(ds, ORIGIN_DIR_NAME, tx); 854 VERIFY0(dsl_dataset_hold_obj(dp, dsl_dataset_phys(ds)->ds_prev_snap_obj, 855 dp, &dp->dp_origin_snap)); 856 dsl_dataset_rele(ds, FTAG); 857 } 858 859 taskq_t * 860 dsl_pool_vnrele_taskq(dsl_pool_t *dp) 861 { 862 return (dp->dp_vnrele_taskq); 863 } 864 865 /* 866 * Walk through the pool-wide zap object of temporary snapshot user holds 867 * and release them. 868 */ 869 void 870 dsl_pool_clean_tmp_userrefs(dsl_pool_t *dp) 871 { 872 zap_attribute_t za; 873 zap_cursor_t zc; 874 objset_t *mos = dp->dp_meta_objset; 875 uint64_t zapobj = dp->dp_tmp_userrefs_obj; 876 nvlist_t *holds; 877 878 if (zapobj == 0) 879 return; 880 ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS); 881 882 holds = fnvlist_alloc(); 883 884 for (zap_cursor_init(&zc, mos, zapobj); 885 zap_cursor_retrieve(&zc, &za) == 0; 886 zap_cursor_advance(&zc)) { 887 char *htag; 888 nvlist_t *tags; 889 890 htag = strchr(za.za_name, '-'); 891 *htag = '\0'; 892 ++htag; 893 if (nvlist_lookup_nvlist(holds, za.za_name, &tags) != 0) { 894 tags = fnvlist_alloc(); 895 fnvlist_add_boolean(tags, htag); 896 fnvlist_add_nvlist(holds, za.za_name, tags); 897 fnvlist_free(tags); 898 } else { 899 fnvlist_add_boolean(tags, htag); 900 } 901 } 902 dsl_dataset_user_release_tmp(dp, holds); 903 fnvlist_free(holds); 904 zap_cursor_fini(&zc); 905 } 906 907 /* 908 * Create the pool-wide zap object for storing temporary snapshot holds. 909 */ 910 void 911 dsl_pool_user_hold_create_obj(dsl_pool_t *dp, dmu_tx_t *tx) 912 { 913 objset_t *mos = dp->dp_meta_objset; 914 915 ASSERT(dp->dp_tmp_userrefs_obj == 0); 916 ASSERT(dmu_tx_is_syncing(tx)); 917 918 dp->dp_tmp_userrefs_obj = zap_create_link(mos, DMU_OT_USERREFS, 919 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_TMP_USERREFS, tx); 920 } 921 922 static int 923 dsl_pool_user_hold_rele_impl(dsl_pool_t *dp, uint64_t dsobj, 924 const char *tag, uint64_t now, dmu_tx_t *tx, boolean_t holding) 925 { 926 objset_t *mos = dp->dp_meta_objset; 927 uint64_t zapobj = dp->dp_tmp_userrefs_obj; 928 char *name; 929 int error; 930 931 ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS); 932 ASSERT(dmu_tx_is_syncing(tx)); 933 934 /* 935 * If the pool was created prior to SPA_VERSION_USERREFS, the 936 * zap object for temporary holds might not exist yet. 937 */ 938 if (zapobj == 0) { 939 if (holding) { 940 dsl_pool_user_hold_create_obj(dp, tx); 941 zapobj = dp->dp_tmp_userrefs_obj; 942 } else { 943 return (SET_ERROR(ENOENT)); 944 } 945 } 946 947 name = kmem_asprintf("%llx-%s", (u_longlong_t)dsobj, tag); 948 if (holding) 949 error = zap_add(mos, zapobj, name, 8, 1, &now, tx); 950 else 951 error = zap_remove(mos, zapobj, name, tx); 952 strfree(name); 953 954 return (error); 955 } 956 957 /* 958 * Add a temporary hold for the given dataset object and tag. 959 */ 960 int 961 dsl_pool_user_hold(dsl_pool_t *dp, uint64_t dsobj, const char *tag, 962 uint64_t now, dmu_tx_t *tx) 963 { 964 return (dsl_pool_user_hold_rele_impl(dp, dsobj, tag, now, tx, B_TRUE)); 965 } 966 967 /* 968 * Release a temporary hold for the given dataset object and tag. 969 */ 970 int 971 dsl_pool_user_release(dsl_pool_t *dp, uint64_t dsobj, const char *tag, 972 dmu_tx_t *tx) 973 { 974 return (dsl_pool_user_hold_rele_impl(dp, dsobj, tag, NULL, 975 tx, B_FALSE)); 976 } 977 978 /* 979 * DSL Pool Configuration Lock 980 * 981 * The dp_config_rwlock protects against changes to DSL state (e.g. dataset 982 * creation / destruction / rename / property setting). It must be held for 983 * read to hold a dataset or dsl_dir. I.e. you must call 984 * dsl_pool_config_enter() or dsl_pool_hold() before calling 985 * dsl_{dataset,dir}_hold{_obj}. In most circumstances, the dp_config_rwlock 986 * must be held continuously until all datasets and dsl_dirs are released. 987 * 988 * The only exception to this rule is that if a "long hold" is placed on 989 * a dataset, then the dp_config_rwlock may be dropped while the dataset 990 * is still held. The long hold will prevent the dataset from being 991 * destroyed -- the destroy will fail with EBUSY. A long hold can be 992 * obtained by calling dsl_dataset_long_hold(), or by "owning" a dataset 993 * (by calling dsl_{dataset,objset}_{try}own{_obj}). 994 * 995 * Legitimate long-holders (including owners) should be long-running, cancelable 996 * tasks that should cause "zfs destroy" to fail. This includes DMU 997 * consumers (i.e. a ZPL filesystem being mounted or ZVOL being open), 998 * "zfs send", and "zfs diff". There are several other long-holders whose 999 * uses are suboptimal (e.g. "zfs promote", and zil_suspend()). 1000 * 1001 * The usual formula for long-holding would be: 1002 * dsl_pool_hold() 1003 * dsl_dataset_hold() 1004 * ... perform checks ... 1005 * dsl_dataset_long_hold() 1006 * dsl_pool_rele() 1007 * ... perform long-running task ... 1008 * dsl_dataset_long_rele() 1009 * dsl_dataset_rele() 1010 * 1011 * Note that when the long hold is released, the dataset is still held but 1012 * the pool is not held. The dataset may change arbitrarily during this time 1013 * (e.g. it could be destroyed). Therefore you shouldn't do anything to the 1014 * dataset except release it. 1015 * 1016 * User-initiated operations (e.g. ioctls, zfs_ioc_*()) are either read-only 1017 * or modifying operations. 1018 * 1019 * Modifying operations should generally use dsl_sync_task(). The synctask 1020 * infrastructure enforces proper locking strategy with respect to the 1021 * dp_config_rwlock. See the comment above dsl_sync_task() for details. 1022 * 1023 * Read-only operations will manually hold the pool, then the dataset, obtain 1024 * information from the dataset, then release the pool and dataset. 1025 * dmu_objset_{hold,rele}() are convenience routines that also do the pool 1026 * hold/rele. 1027 */ 1028 1029 int 1030 dsl_pool_hold(const char *name, void *tag, dsl_pool_t **dp) 1031 { 1032 spa_t *spa; 1033 int error; 1034 1035 error = spa_open(name, &spa, tag); 1036 if (error == 0) { 1037 *dp = spa_get_dsl(spa); 1038 dsl_pool_config_enter(*dp, tag); 1039 } 1040 return (error); 1041 } 1042 1043 void 1044 dsl_pool_rele(dsl_pool_t *dp, void *tag) 1045 { 1046 dsl_pool_config_exit(dp, tag); 1047 spa_close(dp->dp_spa, tag); 1048 } 1049 1050 void 1051 dsl_pool_config_enter(dsl_pool_t *dp, void *tag) 1052 { 1053 /* 1054 * We use a "reentrant" reader-writer lock, but not reentrantly. 1055 * 1056 * The rrwlock can (with the track_all flag) track all reading threads, 1057 * which is very useful for debugging which code path failed to release 1058 * the lock, and for verifying that the *current* thread does hold 1059 * the lock. 1060 * 1061 * (Unlike a rwlock, which knows that N threads hold it for 1062 * read, but not *which* threads, so rw_held(RW_READER) returns TRUE 1063 * if any thread holds it for read, even if this thread doesn't). 1064 */ 1065 ASSERT(!rrw_held(&dp->dp_config_rwlock, RW_READER)); 1066 rrw_enter(&dp->dp_config_rwlock, RW_READER, tag); 1067 } 1068 1069 void 1070 dsl_pool_config_enter_prio(dsl_pool_t *dp, void *tag) 1071 { 1072 ASSERT(!rrw_held(&dp->dp_config_rwlock, RW_READER)); 1073 rrw_enter_read_prio(&dp->dp_config_rwlock, tag); 1074 } 1075 1076 void 1077 dsl_pool_config_exit(dsl_pool_t *dp, void *tag) 1078 { 1079 rrw_exit(&dp->dp_config_rwlock, tag); 1080 } 1081 1082 boolean_t 1083 dsl_pool_config_held(dsl_pool_t *dp) 1084 { 1085 return (RRW_LOCK_HELD(&dp->dp_config_rwlock)); 1086 } 1087 1088 boolean_t 1089 dsl_pool_config_held_writer(dsl_pool_t *dp) 1090 { 1091 return (RRW_WRITE_HELD(&dp->dp_config_rwlock)); 1092 } 1093