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) 2012 by Delphix. All rights reserved. 24 */ 25 26 #include <sys/dsl_pool.h> 27 #include <sys/dsl_dataset.h> 28 #include <sys/dsl_prop.h> 29 #include <sys/dsl_dir.h> 30 #include <sys/dsl_synctask.h> 31 #include <sys/dsl_scan.h> 32 #include <sys/dnode.h> 33 #include <sys/dmu_tx.h> 34 #include <sys/dmu_objset.h> 35 #include <sys/arc.h> 36 #include <sys/zap.h> 37 #include <sys/zio.h> 38 #include <sys/zfs_context.h> 39 #include <sys/fs/zfs.h> 40 #include <sys/zfs_znode.h> 41 #include <sys/spa_impl.h> 42 #include <sys/dsl_deadlist.h> 43 #include <sys/bptree.h> 44 #include <sys/zfeature.h> 45 #include <sys/zil_impl.h> 46 47 int zfs_no_write_throttle = 0; 48 int zfs_write_limit_shift = 3; /* 1/8th of physical memory */ 49 int zfs_txg_synctime_ms = 1000; /* target millisecs to sync a txg */ 50 51 uint64_t zfs_write_limit_min = 32 << 20; /* min write limit is 32MB */ 52 uint64_t zfs_write_limit_max = 0; /* max data payload per txg */ 53 uint64_t zfs_write_limit_inflated = 0; 54 uint64_t zfs_write_limit_override = 0; 55 56 kmutex_t zfs_write_limit_lock; 57 58 static pgcnt_t old_physmem = 0; 59 60 int 61 dsl_pool_open_special_dir(dsl_pool_t *dp, const char *name, dsl_dir_t **ddp) 62 { 63 uint64_t obj; 64 int err; 65 66 err = zap_lookup(dp->dp_meta_objset, 67 dp->dp_root_dir->dd_phys->dd_child_dir_zapobj, 68 name, sizeof (obj), 1, &obj); 69 if (err) 70 return (err); 71 72 return (dsl_dir_open_obj(dp, obj, name, dp, ddp)); 73 } 74 75 static dsl_pool_t * 76 dsl_pool_open_impl(spa_t *spa, uint64_t txg) 77 { 78 dsl_pool_t *dp; 79 blkptr_t *bp = spa_get_rootblkptr(spa); 80 81 dp = kmem_zalloc(sizeof (dsl_pool_t), KM_SLEEP); 82 dp->dp_spa = spa; 83 dp->dp_meta_rootbp = *bp; 84 rw_init(&dp->dp_config_rwlock, NULL, RW_DEFAULT, NULL); 85 dp->dp_write_limit = zfs_write_limit_min; 86 txg_init(dp, txg); 87 88 txg_list_create(&dp->dp_dirty_datasets, 89 offsetof(dsl_dataset_t, ds_dirty_link)); 90 txg_list_create(&dp->dp_dirty_zilogs, 91 offsetof(zilog_t, zl_dirty_link)); 92 txg_list_create(&dp->dp_dirty_dirs, 93 offsetof(dsl_dir_t, dd_dirty_link)); 94 txg_list_create(&dp->dp_sync_tasks, 95 offsetof(dsl_sync_task_group_t, dstg_node)); 96 97 mutex_init(&dp->dp_lock, NULL, MUTEX_DEFAULT, NULL); 98 99 dp->dp_vnrele_taskq = taskq_create("zfs_vn_rele_taskq", 1, minclsyspri, 100 1, 4, 0); 101 102 return (dp); 103 } 104 105 int 106 dsl_pool_init(spa_t *spa, uint64_t txg, dsl_pool_t **dpp) 107 { 108 int err; 109 dsl_pool_t *dp = dsl_pool_open_impl(spa, txg); 110 111 err = dmu_objset_open_impl(spa, NULL, &dp->dp_meta_rootbp, 112 &dp->dp_meta_objset); 113 if (err != 0) 114 dsl_pool_close(dp); 115 else 116 *dpp = dp; 117 118 return (err); 119 } 120 121 int 122 dsl_pool_open(dsl_pool_t *dp) 123 { 124 int err; 125 dsl_dir_t *dd; 126 dsl_dataset_t *ds; 127 uint64_t obj; 128 129 rw_enter(&dp->dp_config_rwlock, RW_WRITER); 130 err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 131 DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1, 132 &dp->dp_root_dir_obj); 133 if (err) 134 goto out; 135 136 err = dsl_dir_open_obj(dp, dp->dp_root_dir_obj, 137 NULL, dp, &dp->dp_root_dir); 138 if (err) 139 goto out; 140 141 err = dsl_pool_open_special_dir(dp, MOS_DIR_NAME, &dp->dp_mos_dir); 142 if (err) 143 goto out; 144 145 if (spa_version(dp->dp_spa) >= SPA_VERSION_ORIGIN) { 146 err = dsl_pool_open_special_dir(dp, ORIGIN_DIR_NAME, &dd); 147 if (err) 148 goto out; 149 err = dsl_dataset_hold_obj(dp, dd->dd_phys->dd_head_dataset_obj, 150 FTAG, &ds); 151 if (err == 0) { 152 err = dsl_dataset_hold_obj(dp, 153 ds->ds_phys->ds_prev_snap_obj, dp, 154 &dp->dp_origin_snap); 155 dsl_dataset_rele(ds, FTAG); 156 } 157 dsl_dir_close(dd, dp); 158 if (err) 159 goto out; 160 } 161 162 if (spa_version(dp->dp_spa) >= SPA_VERSION_DEADLISTS) { 163 err = dsl_pool_open_special_dir(dp, FREE_DIR_NAME, 164 &dp->dp_free_dir); 165 if (err) 166 goto out; 167 168 err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 169 DMU_POOL_FREE_BPOBJ, sizeof (uint64_t), 1, &obj); 170 if (err) 171 goto out; 172 VERIFY3U(0, ==, bpobj_open(&dp->dp_free_bpobj, 173 dp->dp_meta_objset, obj)); 174 } 175 176 if (spa_feature_is_active(dp->dp_spa, 177 &spa_feature_table[SPA_FEATURE_ASYNC_DESTROY])) { 178 err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 179 DMU_POOL_BPTREE_OBJ, sizeof (uint64_t), 1, 180 &dp->dp_bptree_obj); 181 if (err != 0) 182 goto out; 183 } 184 185 if (spa_feature_is_active(dp->dp_spa, 186 &spa_feature_table[SPA_FEATURE_EMPTY_BPOBJ])) { 187 err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 188 DMU_POOL_EMPTY_BPOBJ, sizeof (uint64_t), 1, 189 &dp->dp_empty_bpobj); 190 if (err != 0) 191 goto out; 192 } 193 194 err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 195 DMU_POOL_TMP_USERREFS, sizeof (uint64_t), 1, 196 &dp->dp_tmp_userrefs_obj); 197 if (err == ENOENT) 198 err = 0; 199 if (err) 200 goto out; 201 202 err = dsl_scan_init(dp, dp->dp_tx.tx_open_txg); 203 204 out: 205 rw_exit(&dp->dp_config_rwlock); 206 return (err); 207 } 208 209 void 210 dsl_pool_close(dsl_pool_t *dp) 211 { 212 /* drop our references from dsl_pool_open() */ 213 214 /* 215 * Since we held the origin_snap from "syncing" context (which 216 * includes pool-opening context), it actually only got a "ref" 217 * and not a hold, so just drop that here. 218 */ 219 if (dp->dp_origin_snap) 220 dsl_dataset_drop_ref(dp->dp_origin_snap, dp); 221 if (dp->dp_mos_dir) 222 dsl_dir_close(dp->dp_mos_dir, dp); 223 if (dp->dp_free_dir) 224 dsl_dir_close(dp->dp_free_dir, dp); 225 if (dp->dp_root_dir) 226 dsl_dir_close(dp->dp_root_dir, dp); 227 228 bpobj_close(&dp->dp_free_bpobj); 229 230 /* undo the dmu_objset_open_impl(mos) from dsl_pool_open() */ 231 if (dp->dp_meta_objset) 232 dmu_objset_evict(dp->dp_meta_objset); 233 234 txg_list_destroy(&dp->dp_dirty_datasets); 235 txg_list_destroy(&dp->dp_dirty_zilogs); 236 txg_list_destroy(&dp->dp_sync_tasks); 237 txg_list_destroy(&dp->dp_dirty_dirs); 238 239 arc_flush(dp->dp_spa); 240 txg_fini(dp); 241 dsl_scan_fini(dp); 242 rw_destroy(&dp->dp_config_rwlock); 243 mutex_destroy(&dp->dp_lock); 244 taskq_destroy(dp->dp_vnrele_taskq); 245 if (dp->dp_blkstats) 246 kmem_free(dp->dp_blkstats, sizeof (zfs_all_blkstats_t)); 247 kmem_free(dp, sizeof (dsl_pool_t)); 248 } 249 250 dsl_pool_t * 251 dsl_pool_create(spa_t *spa, nvlist_t *zplprops, uint64_t txg) 252 { 253 int err; 254 dsl_pool_t *dp = dsl_pool_open_impl(spa, txg); 255 dmu_tx_t *tx = dmu_tx_create_assigned(dp, txg); 256 objset_t *os; 257 dsl_dataset_t *ds; 258 uint64_t obj; 259 260 /* create and open the MOS (meta-objset) */ 261 dp->dp_meta_objset = dmu_objset_create_impl(spa, 262 NULL, &dp->dp_meta_rootbp, DMU_OST_META, tx); 263 264 /* create the pool directory */ 265 err = zap_create_claim(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 266 DMU_OT_OBJECT_DIRECTORY, DMU_OT_NONE, 0, tx); 267 ASSERT0(err); 268 269 /* Initialize scan structures */ 270 VERIFY3U(0, ==, dsl_scan_init(dp, txg)); 271 272 /* create and open the root dir */ 273 dp->dp_root_dir_obj = dsl_dir_create_sync(dp, NULL, NULL, tx); 274 VERIFY(0 == dsl_dir_open_obj(dp, dp->dp_root_dir_obj, 275 NULL, dp, &dp->dp_root_dir)); 276 277 /* create and open the meta-objset dir */ 278 (void) dsl_dir_create_sync(dp, dp->dp_root_dir, MOS_DIR_NAME, tx); 279 VERIFY(0 == dsl_pool_open_special_dir(dp, 280 MOS_DIR_NAME, &dp->dp_mos_dir)); 281 282 if (spa_version(spa) >= SPA_VERSION_DEADLISTS) { 283 /* create and open the free dir */ 284 (void) dsl_dir_create_sync(dp, dp->dp_root_dir, 285 FREE_DIR_NAME, tx); 286 VERIFY(0 == dsl_pool_open_special_dir(dp, 287 FREE_DIR_NAME, &dp->dp_free_dir)); 288 289 /* create and open the free_bplist */ 290 obj = bpobj_alloc(dp->dp_meta_objset, SPA_MAXBLOCKSIZE, tx); 291 VERIFY(zap_add(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 292 DMU_POOL_FREE_BPOBJ, sizeof (uint64_t), 1, &obj, tx) == 0); 293 VERIFY3U(0, ==, bpobj_open(&dp->dp_free_bpobj, 294 dp->dp_meta_objset, obj)); 295 } 296 297 if (spa_version(spa) >= SPA_VERSION_DSL_SCRUB) 298 dsl_pool_create_origin(dp, tx); 299 300 /* create the root dataset */ 301 obj = dsl_dataset_create_sync_dd(dp->dp_root_dir, NULL, 0, tx); 302 303 /* create the root objset */ 304 VERIFY(0 == dsl_dataset_hold_obj(dp, obj, FTAG, &ds)); 305 os = dmu_objset_create_impl(dp->dp_spa, ds, 306 dsl_dataset_get_blkptr(ds), DMU_OST_ZFS, tx); 307 #ifdef _KERNEL 308 zfs_create_fs(os, kcred, zplprops, tx); 309 #endif 310 dsl_dataset_rele(ds, FTAG); 311 312 dmu_tx_commit(tx); 313 314 return (dp); 315 } 316 317 /* 318 * Account for the meta-objset space in its placeholder dsl_dir. 319 */ 320 void 321 dsl_pool_mos_diduse_space(dsl_pool_t *dp, 322 int64_t used, int64_t comp, int64_t uncomp) 323 { 324 ASSERT3U(comp, ==, uncomp); /* it's all metadata */ 325 mutex_enter(&dp->dp_lock); 326 dp->dp_mos_used_delta += used; 327 dp->dp_mos_compressed_delta += comp; 328 dp->dp_mos_uncompressed_delta += uncomp; 329 mutex_exit(&dp->dp_lock); 330 } 331 332 static int 333 deadlist_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 334 { 335 dsl_deadlist_t *dl = arg; 336 dsl_pool_t *dp = dmu_objset_pool(dl->dl_os); 337 rw_enter(&dp->dp_config_rwlock, RW_READER); 338 dsl_deadlist_insert(dl, bp, tx); 339 rw_exit(&dp->dp_config_rwlock); 340 return (0); 341 } 342 343 void 344 dsl_pool_sync(dsl_pool_t *dp, uint64_t txg) 345 { 346 zio_t *zio; 347 dmu_tx_t *tx; 348 dsl_dir_t *dd; 349 dsl_dataset_t *ds; 350 objset_t *mos = dp->dp_meta_objset; 351 hrtime_t start, write_time; 352 uint64_t data_written; 353 int err; 354 list_t synced_datasets; 355 356 list_create(&synced_datasets, sizeof (dsl_dataset_t), 357 offsetof(dsl_dataset_t, ds_synced_link)); 358 359 /* 360 * We need to copy dp_space_towrite() before doing 361 * dsl_sync_task_group_sync(), because 362 * dsl_dataset_snapshot_reserve_space() will increase 363 * dp_space_towrite but not actually write anything. 364 */ 365 data_written = dp->dp_space_towrite[txg & TXG_MASK]; 366 367 tx = dmu_tx_create_assigned(dp, txg); 368 369 dp->dp_read_overhead = 0; 370 start = gethrtime(); 371 372 zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED); 373 while (ds = txg_list_remove(&dp->dp_dirty_datasets, txg)) { 374 /* 375 * We must not sync any non-MOS datasets twice, because 376 * we may have taken a snapshot of them. However, we 377 * may sync newly-created datasets on pass 2. 378 */ 379 ASSERT(!list_link_active(&ds->ds_synced_link)); 380 list_insert_tail(&synced_datasets, ds); 381 dsl_dataset_sync(ds, zio, tx); 382 } 383 DTRACE_PROBE(pool_sync__1setup); 384 err = zio_wait(zio); 385 386 write_time = gethrtime() - start; 387 ASSERT(err == 0); 388 DTRACE_PROBE(pool_sync__2rootzio); 389 390 /* 391 * After the data blocks have been written (ensured by the zio_wait() 392 * above), update the user/group space accounting. 393 */ 394 for (ds = list_head(&synced_datasets); ds; 395 ds = list_next(&synced_datasets, ds)) 396 dmu_objset_do_userquota_updates(ds->ds_objset, tx); 397 398 /* 399 * Sync the datasets again to push out the changes due to 400 * userspace updates. This must be done before we process the 401 * sync tasks, so that any snapshots will have the correct 402 * user accounting information (and we won't get confused 403 * about which blocks are part of the snapshot). 404 */ 405 zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED); 406 while (ds = txg_list_remove(&dp->dp_dirty_datasets, txg)) { 407 ASSERT(list_link_active(&ds->ds_synced_link)); 408 dmu_buf_rele(ds->ds_dbuf, ds); 409 dsl_dataset_sync(ds, zio, tx); 410 } 411 err = zio_wait(zio); 412 413 /* 414 * Now that the datasets have been completely synced, we can 415 * clean up our in-memory structures accumulated while syncing: 416 * 417 * - move dead blocks from the pending deadlist to the on-disk deadlist 418 * - clean up zil records 419 * - release hold from dsl_dataset_dirty() 420 */ 421 while (ds = list_remove_head(&synced_datasets)) { 422 objset_t *os = ds->ds_objset; 423 bplist_iterate(&ds->ds_pending_deadlist, 424 deadlist_enqueue_cb, &ds->ds_deadlist, tx); 425 ASSERT(!dmu_objset_is_dirty(os, txg)); 426 dmu_buf_rele(ds->ds_dbuf, ds); 427 } 428 429 start = gethrtime(); 430 while (dd = txg_list_remove(&dp->dp_dirty_dirs, txg)) 431 dsl_dir_sync(dd, tx); 432 write_time += gethrtime() - start; 433 434 /* 435 * The MOS's space is accounted for in the pool/$MOS 436 * (dp_mos_dir). We can't modify the mos while we're syncing 437 * it, so we remember the deltas and apply them here. 438 */ 439 if (dp->dp_mos_used_delta != 0 || dp->dp_mos_compressed_delta != 0 || 440 dp->dp_mos_uncompressed_delta != 0) { 441 dsl_dir_diduse_space(dp->dp_mos_dir, DD_USED_HEAD, 442 dp->dp_mos_used_delta, 443 dp->dp_mos_compressed_delta, 444 dp->dp_mos_uncompressed_delta, tx); 445 dp->dp_mos_used_delta = 0; 446 dp->dp_mos_compressed_delta = 0; 447 dp->dp_mos_uncompressed_delta = 0; 448 } 449 450 start = gethrtime(); 451 if (list_head(&mos->os_dirty_dnodes[txg & TXG_MASK]) != NULL || 452 list_head(&mos->os_free_dnodes[txg & TXG_MASK]) != NULL) { 453 zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED); 454 dmu_objset_sync(mos, zio, tx); 455 err = zio_wait(zio); 456 ASSERT(err == 0); 457 dprintf_bp(&dp->dp_meta_rootbp, "meta objset rootbp is %s", ""); 458 spa_set_rootblkptr(dp->dp_spa, &dp->dp_meta_rootbp); 459 } 460 write_time += gethrtime() - start; 461 DTRACE_PROBE2(pool_sync__4io, hrtime_t, write_time, 462 hrtime_t, dp->dp_read_overhead); 463 write_time -= dp->dp_read_overhead; 464 465 /* 466 * If we modify a dataset in the same txg that we want to destroy it, 467 * its dsl_dir's dd_dbuf will be dirty, and thus have a hold on it. 468 * dsl_dir_destroy_check() will fail if there are unexpected holds. 469 * Therefore, we want to sync the MOS (thus syncing the dd_dbuf 470 * and clearing the hold on it) before we process the sync_tasks. 471 * The MOS data dirtied by the sync_tasks will be synced on the next 472 * pass. 473 */ 474 DTRACE_PROBE(pool_sync__3task); 475 if (!txg_list_empty(&dp->dp_sync_tasks, txg)) { 476 dsl_sync_task_group_t *dstg; 477 /* 478 * No more sync tasks should have been added while we 479 * were syncing. 480 */ 481 ASSERT(spa_sync_pass(dp->dp_spa) == 1); 482 while (dstg = txg_list_remove(&dp->dp_sync_tasks, txg)) 483 dsl_sync_task_group_sync(dstg, tx); 484 } 485 486 dmu_tx_commit(tx); 487 488 dp->dp_space_towrite[txg & TXG_MASK] = 0; 489 ASSERT(dp->dp_tempreserved[txg & TXG_MASK] == 0); 490 491 /* 492 * If the write limit max has not been explicitly set, set it 493 * to a fraction of available physical memory (default 1/8th). 494 * Note that we must inflate the limit because the spa 495 * inflates write sizes to account for data replication. 496 * Check this each sync phase to catch changing memory size. 497 */ 498 if (physmem != old_physmem && zfs_write_limit_shift) { 499 mutex_enter(&zfs_write_limit_lock); 500 old_physmem = physmem; 501 zfs_write_limit_max = ptob(physmem) >> zfs_write_limit_shift; 502 zfs_write_limit_inflated = MAX(zfs_write_limit_min, 503 spa_get_asize(dp->dp_spa, zfs_write_limit_max)); 504 mutex_exit(&zfs_write_limit_lock); 505 } 506 507 /* 508 * Attempt to keep the sync time consistent by adjusting the 509 * amount of write traffic allowed into each transaction group. 510 * Weight the throughput calculation towards the current value: 511 * thru = 3/4 old_thru + 1/4 new_thru 512 * 513 * Note: write_time is in nanosecs, so write_time/MICROSEC 514 * yields millisecs 515 */ 516 ASSERT(zfs_write_limit_min > 0); 517 if (data_written > zfs_write_limit_min / 8 && write_time > MICROSEC) { 518 uint64_t throughput = data_written / (write_time / MICROSEC); 519 520 if (dp->dp_throughput) 521 dp->dp_throughput = throughput / 4 + 522 3 * dp->dp_throughput / 4; 523 else 524 dp->dp_throughput = throughput; 525 dp->dp_write_limit = MIN(zfs_write_limit_inflated, 526 MAX(zfs_write_limit_min, 527 dp->dp_throughput * zfs_txg_synctime_ms)); 528 } 529 } 530 531 void 532 dsl_pool_sync_done(dsl_pool_t *dp, uint64_t txg) 533 { 534 zilog_t *zilog; 535 dsl_dataset_t *ds; 536 537 while (zilog = txg_list_remove(&dp->dp_dirty_zilogs, txg)) { 538 ds = dmu_objset_ds(zilog->zl_os); 539 zil_clean(zilog, txg); 540 ASSERT(!dmu_objset_is_dirty(zilog->zl_os, txg)); 541 dmu_buf_rele(ds->ds_dbuf, zilog); 542 } 543 ASSERT(!dmu_objset_is_dirty(dp->dp_meta_objset, txg)); 544 } 545 546 /* 547 * TRUE if the current thread is the tx_sync_thread or if we 548 * are being called from SPA context during pool initialization. 549 */ 550 int 551 dsl_pool_sync_context(dsl_pool_t *dp) 552 { 553 return (curthread == dp->dp_tx.tx_sync_thread || 554 spa_is_initializing(dp->dp_spa)); 555 } 556 557 uint64_t 558 dsl_pool_adjustedsize(dsl_pool_t *dp, boolean_t netfree) 559 { 560 uint64_t space, resv; 561 562 /* 563 * Reserve about 1.6% (1/64), or at least 32MB, for allocation 564 * efficiency. 565 * XXX The intent log is not accounted for, so it must fit 566 * within this slop. 567 * 568 * If we're trying to assess whether it's OK to do a free, 569 * cut the reservation in half to allow forward progress 570 * (e.g. make it possible to rm(1) files from a full pool). 571 */ 572 space = spa_get_dspace(dp->dp_spa); 573 resv = MAX(space >> 6, SPA_MINDEVSIZE >> 1); 574 if (netfree) 575 resv >>= 1; 576 577 return (space - resv); 578 } 579 580 int 581 dsl_pool_tempreserve_space(dsl_pool_t *dp, uint64_t space, dmu_tx_t *tx) 582 { 583 uint64_t reserved = 0; 584 uint64_t write_limit = (zfs_write_limit_override ? 585 zfs_write_limit_override : dp->dp_write_limit); 586 587 if (zfs_no_write_throttle) { 588 atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK], 589 space); 590 return (0); 591 } 592 593 /* 594 * Check to see if we have exceeded the maximum allowed IO for 595 * this transaction group. We can do this without locks since 596 * a little slop here is ok. Note that we do the reserved check 597 * with only half the requested reserve: this is because the 598 * reserve requests are worst-case, and we really don't want to 599 * throttle based off of worst-case estimates. 600 */ 601 if (write_limit > 0) { 602 reserved = dp->dp_space_towrite[tx->tx_txg & TXG_MASK] 603 + dp->dp_tempreserved[tx->tx_txg & TXG_MASK] / 2; 604 605 if (reserved && reserved > write_limit) 606 return (ERESTART); 607 } 608 609 atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK], space); 610 611 /* 612 * If this transaction group is over 7/8ths capacity, delay 613 * the caller 1 clock tick. This will slow down the "fill" 614 * rate until the sync process can catch up with us. 615 */ 616 if (reserved && reserved > (write_limit - (write_limit >> 3))) 617 txg_delay(dp, tx->tx_txg, 1); 618 619 return (0); 620 } 621 622 void 623 dsl_pool_tempreserve_clear(dsl_pool_t *dp, int64_t space, dmu_tx_t *tx) 624 { 625 ASSERT(dp->dp_tempreserved[tx->tx_txg & TXG_MASK] >= space); 626 atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK], -space); 627 } 628 629 void 630 dsl_pool_memory_pressure(dsl_pool_t *dp) 631 { 632 uint64_t space_inuse = 0; 633 int i; 634 635 if (dp->dp_write_limit == zfs_write_limit_min) 636 return; 637 638 for (i = 0; i < TXG_SIZE; i++) { 639 space_inuse += dp->dp_space_towrite[i]; 640 space_inuse += dp->dp_tempreserved[i]; 641 } 642 dp->dp_write_limit = MAX(zfs_write_limit_min, 643 MIN(dp->dp_write_limit, space_inuse / 4)); 644 } 645 646 void 647 dsl_pool_willuse_space(dsl_pool_t *dp, int64_t space, dmu_tx_t *tx) 648 { 649 if (space > 0) { 650 mutex_enter(&dp->dp_lock); 651 dp->dp_space_towrite[tx->tx_txg & TXG_MASK] += space; 652 mutex_exit(&dp->dp_lock); 653 } 654 } 655 656 /* ARGSUSED */ 657 static int 658 upgrade_clones_cb(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg) 659 { 660 dmu_tx_t *tx = arg; 661 dsl_dataset_t *ds, *prev = NULL; 662 int err; 663 dsl_pool_t *dp = spa_get_dsl(spa); 664 665 err = dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds); 666 if (err) 667 return (err); 668 669 while (ds->ds_phys->ds_prev_snap_obj != 0) { 670 err = dsl_dataset_hold_obj(dp, ds->ds_phys->ds_prev_snap_obj, 671 FTAG, &prev); 672 if (err) { 673 dsl_dataset_rele(ds, FTAG); 674 return (err); 675 } 676 677 if (prev->ds_phys->ds_next_snap_obj != ds->ds_object) 678 break; 679 dsl_dataset_rele(ds, FTAG); 680 ds = prev; 681 prev = NULL; 682 } 683 684 if (prev == NULL) { 685 prev = dp->dp_origin_snap; 686 687 /* 688 * The $ORIGIN can't have any data, or the accounting 689 * will be wrong. 690 */ 691 ASSERT(prev->ds_phys->ds_bp.blk_birth == 0); 692 693 /* The origin doesn't get attached to itself */ 694 if (ds->ds_object == prev->ds_object) { 695 dsl_dataset_rele(ds, FTAG); 696 return (0); 697 } 698 699 dmu_buf_will_dirty(ds->ds_dbuf, tx); 700 ds->ds_phys->ds_prev_snap_obj = prev->ds_object; 701 ds->ds_phys->ds_prev_snap_txg = prev->ds_phys->ds_creation_txg; 702 703 dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx); 704 ds->ds_dir->dd_phys->dd_origin_obj = prev->ds_object; 705 706 dmu_buf_will_dirty(prev->ds_dbuf, tx); 707 prev->ds_phys->ds_num_children++; 708 709 if (ds->ds_phys->ds_next_snap_obj == 0) { 710 ASSERT(ds->ds_prev == NULL); 711 VERIFY(0 == dsl_dataset_hold_obj(dp, 712 ds->ds_phys->ds_prev_snap_obj, ds, &ds->ds_prev)); 713 } 714 } 715 716 ASSERT(ds->ds_dir->dd_phys->dd_origin_obj == prev->ds_object); 717 ASSERT(ds->ds_phys->ds_prev_snap_obj == prev->ds_object); 718 719 if (prev->ds_phys->ds_next_clones_obj == 0) { 720 dmu_buf_will_dirty(prev->ds_dbuf, tx); 721 prev->ds_phys->ds_next_clones_obj = 722 zap_create(dp->dp_meta_objset, 723 DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx); 724 } 725 VERIFY(0 == zap_add_int(dp->dp_meta_objset, 726 prev->ds_phys->ds_next_clones_obj, ds->ds_object, tx)); 727 728 dsl_dataset_rele(ds, FTAG); 729 if (prev != dp->dp_origin_snap) 730 dsl_dataset_rele(prev, FTAG); 731 return (0); 732 } 733 734 void 735 dsl_pool_upgrade_clones(dsl_pool_t *dp, dmu_tx_t *tx) 736 { 737 ASSERT(dmu_tx_is_syncing(tx)); 738 ASSERT(dp->dp_origin_snap != NULL); 739 740 VERIFY3U(0, ==, dmu_objset_find_spa(dp->dp_spa, NULL, upgrade_clones_cb, 741 tx, DS_FIND_CHILDREN)); 742 } 743 744 /* ARGSUSED */ 745 static int 746 upgrade_dir_clones_cb(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg) 747 { 748 dmu_tx_t *tx = arg; 749 dsl_dataset_t *ds; 750 dsl_pool_t *dp = spa_get_dsl(spa); 751 objset_t *mos = dp->dp_meta_objset; 752 753 VERIFY3U(0, ==, dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds)); 754 755 if (ds->ds_dir->dd_phys->dd_origin_obj) { 756 dsl_dataset_t *origin; 757 758 VERIFY3U(0, ==, dsl_dataset_hold_obj(dp, 759 ds->ds_dir->dd_phys->dd_origin_obj, FTAG, &origin)); 760 761 if (origin->ds_dir->dd_phys->dd_clones == 0) { 762 dmu_buf_will_dirty(origin->ds_dir->dd_dbuf, tx); 763 origin->ds_dir->dd_phys->dd_clones = zap_create(mos, 764 DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx); 765 } 766 767 VERIFY3U(0, ==, zap_add_int(dp->dp_meta_objset, 768 origin->ds_dir->dd_phys->dd_clones, dsobj, tx)); 769 770 dsl_dataset_rele(origin, FTAG); 771 } 772 773 dsl_dataset_rele(ds, FTAG); 774 return (0); 775 } 776 777 void 778 dsl_pool_upgrade_dir_clones(dsl_pool_t *dp, dmu_tx_t *tx) 779 { 780 ASSERT(dmu_tx_is_syncing(tx)); 781 uint64_t obj; 782 783 (void) dsl_dir_create_sync(dp, dp->dp_root_dir, FREE_DIR_NAME, tx); 784 VERIFY(0 == dsl_pool_open_special_dir(dp, 785 FREE_DIR_NAME, &dp->dp_free_dir)); 786 787 /* 788 * We can't use bpobj_alloc(), because spa_version() still 789 * returns the old version, and we need a new-version bpobj with 790 * subobj support. So call dmu_object_alloc() directly. 791 */ 792 obj = dmu_object_alloc(dp->dp_meta_objset, DMU_OT_BPOBJ, 793 SPA_MAXBLOCKSIZE, DMU_OT_BPOBJ_HDR, sizeof (bpobj_phys_t), tx); 794 VERIFY3U(0, ==, zap_add(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 795 DMU_POOL_FREE_BPOBJ, sizeof (uint64_t), 1, &obj, tx)); 796 VERIFY3U(0, ==, bpobj_open(&dp->dp_free_bpobj, 797 dp->dp_meta_objset, obj)); 798 799 VERIFY3U(0, ==, dmu_objset_find_spa(dp->dp_spa, NULL, 800 upgrade_dir_clones_cb, tx, DS_FIND_CHILDREN)); 801 } 802 803 void 804 dsl_pool_create_origin(dsl_pool_t *dp, dmu_tx_t *tx) 805 { 806 uint64_t dsobj; 807 dsl_dataset_t *ds; 808 809 ASSERT(dmu_tx_is_syncing(tx)); 810 ASSERT(dp->dp_origin_snap == NULL); 811 812 /* create the origin dir, ds, & snap-ds */ 813 rw_enter(&dp->dp_config_rwlock, RW_WRITER); 814 dsobj = dsl_dataset_create_sync(dp->dp_root_dir, ORIGIN_DIR_NAME, 815 NULL, 0, kcred, tx); 816 VERIFY(0 == dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds)); 817 dsl_dataset_snapshot_sync(ds, ORIGIN_DIR_NAME, tx); 818 VERIFY(0 == dsl_dataset_hold_obj(dp, ds->ds_phys->ds_prev_snap_obj, 819 dp, &dp->dp_origin_snap)); 820 dsl_dataset_rele(ds, FTAG); 821 rw_exit(&dp->dp_config_rwlock); 822 } 823 824 taskq_t * 825 dsl_pool_vnrele_taskq(dsl_pool_t *dp) 826 { 827 return (dp->dp_vnrele_taskq); 828 } 829 830 /* 831 * Walk through the pool-wide zap object of temporary snapshot user holds 832 * and release them. 833 */ 834 void 835 dsl_pool_clean_tmp_userrefs(dsl_pool_t *dp) 836 { 837 zap_attribute_t za; 838 zap_cursor_t zc; 839 objset_t *mos = dp->dp_meta_objset; 840 uint64_t zapobj = dp->dp_tmp_userrefs_obj; 841 842 if (zapobj == 0) 843 return; 844 ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS); 845 846 for (zap_cursor_init(&zc, mos, zapobj); 847 zap_cursor_retrieve(&zc, &za) == 0; 848 zap_cursor_advance(&zc)) { 849 char *htag; 850 uint64_t dsobj; 851 852 htag = strchr(za.za_name, '-'); 853 *htag = '\0'; 854 ++htag; 855 dsobj = strtonum(za.za_name, NULL); 856 (void) dsl_dataset_user_release_tmp(dp, dsobj, htag, B_FALSE); 857 } 858 zap_cursor_fini(&zc); 859 } 860 861 /* 862 * Create the pool-wide zap object for storing temporary snapshot holds. 863 */ 864 void 865 dsl_pool_user_hold_create_obj(dsl_pool_t *dp, dmu_tx_t *tx) 866 { 867 objset_t *mos = dp->dp_meta_objset; 868 869 ASSERT(dp->dp_tmp_userrefs_obj == 0); 870 ASSERT(dmu_tx_is_syncing(tx)); 871 872 dp->dp_tmp_userrefs_obj = zap_create_link(mos, DMU_OT_USERREFS, 873 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_TMP_USERREFS, tx); 874 } 875 876 static int 877 dsl_pool_user_hold_rele_impl(dsl_pool_t *dp, uint64_t dsobj, 878 const char *tag, uint64_t *now, dmu_tx_t *tx, boolean_t holding) 879 { 880 objset_t *mos = dp->dp_meta_objset; 881 uint64_t zapobj = dp->dp_tmp_userrefs_obj; 882 char *name; 883 int error; 884 885 ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS); 886 ASSERT(dmu_tx_is_syncing(tx)); 887 888 /* 889 * If the pool was created prior to SPA_VERSION_USERREFS, the 890 * zap object for temporary holds might not exist yet. 891 */ 892 if (zapobj == 0) { 893 if (holding) { 894 dsl_pool_user_hold_create_obj(dp, tx); 895 zapobj = dp->dp_tmp_userrefs_obj; 896 } else { 897 return (ENOENT); 898 } 899 } 900 901 name = kmem_asprintf("%llx-%s", (u_longlong_t)dsobj, tag); 902 if (holding) 903 error = zap_add(mos, zapobj, name, 8, 1, now, tx); 904 else 905 error = zap_remove(mos, zapobj, name, tx); 906 strfree(name); 907 908 return (error); 909 } 910 911 /* 912 * Add a temporary hold for the given dataset object and tag. 913 */ 914 int 915 dsl_pool_user_hold(dsl_pool_t *dp, uint64_t dsobj, const char *tag, 916 uint64_t *now, dmu_tx_t *tx) 917 { 918 return (dsl_pool_user_hold_rele_impl(dp, dsobj, tag, now, tx, B_TRUE)); 919 } 920 921 /* 922 * Release a temporary hold for the given dataset object and tag. 923 */ 924 int 925 dsl_pool_user_release(dsl_pool_t *dp, uint64_t dsobj, const char *tag, 926 dmu_tx_t *tx) 927 { 928 return (dsl_pool_user_hold_rele_impl(dp, dsobj, tag, NULL, 929 tx, B_FALSE)); 930 } 931