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.
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.
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]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2013 by Delphix. All rights reserved.
24 * Copyright (c) 2013 Steven Hartland. All rights reserved.
27 #include <sys/dsl_pool.h>
28 #include <sys/dsl_dataset.h>
29 #include <sys/dsl_prop.h>
30 #include <sys/dsl_dir.h>
31 #include <sys/dsl_synctask.h>
32 #include <sys/dsl_scan.h>
33 #include <sys/dnode.h>
34 #include <sys/dmu_tx.h>
35 #include <sys/dmu_objset.h>
39 #include <sys/zfs_context.h>
40 #include <sys/fs/zfs.h>
41 #include <sys/zfs_znode.h>
42 #include <sys/spa_impl.h>
43 #include <sys/dsl_deadlist.h>
44 #include <sys/bptree.h>
45 #include <sys/zfeature.h>
46 #include <sys/zil_impl.h>
47 #include <sys/dsl_userhold.h>
49 int zfs_no_write_throttle = 0;
50 int zfs_write_limit_shift = 3; /* 1/8th of physical memory */
51 int zfs_txg_synctime_ms = 1000; /* target millisecs to sync a txg */
53 uint64_t zfs_write_limit_min = 32 << 20; /* min write limit is 32MB */
54 uint64_t zfs_write_limit_max = 0; /* max data payload per txg */
55 uint64_t zfs_write_limit_inflated = 0;
56 uint64_t zfs_write_limit_override = 0;
58 kmutex_t zfs_write_limit_lock;
60 static pgcnt_t old_physmem = 0;
62 SYSCTL_DECL(_vfs_zfs);
63 TUNABLE_INT("vfs.zfs.no_write_throttle", &zfs_no_write_throttle);
64 SYSCTL_INT(_vfs_zfs, OID_AUTO, no_write_throttle, CTLFLAG_RDTUN,
65 &zfs_no_write_throttle, 0, "");
66 TUNABLE_INT("vfs.zfs.write_limit_shift", &zfs_write_limit_shift);
67 SYSCTL_INT(_vfs_zfs, OID_AUTO, write_limit_shift, CTLFLAG_RDTUN,
68 &zfs_write_limit_shift, 0, "2^N of physical memory");
69 SYSCTL_DECL(_vfs_zfs_txg);
70 TUNABLE_INT("vfs.zfs.txg.synctime_ms", &zfs_txg_synctime_ms);
71 SYSCTL_INT(_vfs_zfs_txg, OID_AUTO, synctime_ms, CTLFLAG_RDTUN,
72 &zfs_txg_synctime_ms, 0, "Target milliseconds to sync a txg");
74 TUNABLE_QUAD("vfs.zfs.write_limit_min", &zfs_write_limit_min);
75 SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, write_limit_min, CTLFLAG_RDTUN,
76 &zfs_write_limit_min, 0, "Minimum write limit");
77 TUNABLE_QUAD("vfs.zfs.write_limit_max", &zfs_write_limit_max);
78 SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, write_limit_max, CTLFLAG_RDTUN,
79 &zfs_write_limit_max, 0, "Maximum data payload per txg");
80 TUNABLE_QUAD("vfs.zfs.write_limit_inflated", &zfs_write_limit_inflated);
81 SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, write_limit_inflated, CTLFLAG_RDTUN,
82 &zfs_write_limit_inflated, 0, "Maximum size of the dynamic write limit");
83 TUNABLE_QUAD("vfs.zfs.write_limit_override", &zfs_write_limit_override);
84 SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, write_limit_override, CTLFLAG_RDTUN,
85 &zfs_write_limit_override, 0,
86 "Force a txg if dirty buffers exceed this value (bytes)");
89 dsl_pool_open_special_dir(dsl_pool_t *dp, const char *name, dsl_dir_t **ddp)
94 err = zap_lookup(dp->dp_meta_objset,
95 dp->dp_root_dir->dd_phys->dd_child_dir_zapobj,
96 name, sizeof (obj), 1, &obj);
100 return (dsl_dir_hold_obj(dp, obj, name, dp, ddp));
104 dsl_pool_open_impl(spa_t *spa, uint64_t txg)
107 blkptr_t *bp = spa_get_rootblkptr(spa);
109 dp = kmem_zalloc(sizeof (dsl_pool_t), KM_SLEEP);
111 dp->dp_meta_rootbp = *bp;
112 rrw_init(&dp->dp_config_rwlock, B_TRUE);
113 dp->dp_write_limit = zfs_write_limit_min;
116 txg_list_create(&dp->dp_dirty_datasets,
117 offsetof(dsl_dataset_t, ds_dirty_link));
118 txg_list_create(&dp->dp_dirty_zilogs,
119 offsetof(zilog_t, zl_dirty_link));
120 txg_list_create(&dp->dp_dirty_dirs,
121 offsetof(dsl_dir_t, dd_dirty_link));
122 txg_list_create(&dp->dp_sync_tasks,
123 offsetof(dsl_sync_task_t, dst_node));
125 mutex_init(&dp->dp_lock, NULL, MUTEX_DEFAULT, NULL);
127 dp->dp_vnrele_taskq = taskq_create("zfs_vn_rele_taskq", 1, minclsyspri,
134 dsl_pool_init(spa_t *spa, uint64_t txg, dsl_pool_t **dpp)
137 dsl_pool_t *dp = dsl_pool_open_impl(spa, txg);
139 err = dmu_objset_open_impl(spa, NULL, &dp->dp_meta_rootbp,
140 &dp->dp_meta_objset);
150 dsl_pool_open(dsl_pool_t *dp)
157 rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG);
158 err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
159 DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1,
160 &dp->dp_root_dir_obj);
164 err = dsl_dir_hold_obj(dp, dp->dp_root_dir_obj,
165 NULL, dp, &dp->dp_root_dir);
169 err = dsl_pool_open_special_dir(dp, MOS_DIR_NAME, &dp->dp_mos_dir);
173 if (spa_version(dp->dp_spa) >= SPA_VERSION_ORIGIN) {
174 err = dsl_pool_open_special_dir(dp, ORIGIN_DIR_NAME, &dd);
177 err = dsl_dataset_hold_obj(dp, dd->dd_phys->dd_head_dataset_obj,
180 err = dsl_dataset_hold_obj(dp,
181 ds->ds_phys->ds_prev_snap_obj, dp,
182 &dp->dp_origin_snap);
183 dsl_dataset_rele(ds, FTAG);
185 dsl_dir_rele(dd, dp);
190 if (spa_version(dp->dp_spa) >= SPA_VERSION_DEADLISTS) {
191 err = dsl_pool_open_special_dir(dp, FREE_DIR_NAME,
196 err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
197 DMU_POOL_FREE_BPOBJ, sizeof (uint64_t), 1, &obj);
200 VERIFY0(bpobj_open(&dp->dp_free_bpobj,
201 dp->dp_meta_objset, obj));
204 if (spa_feature_is_active(dp->dp_spa,
205 &spa_feature_table[SPA_FEATURE_ASYNC_DESTROY])) {
206 err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
207 DMU_POOL_BPTREE_OBJ, sizeof (uint64_t), 1,
213 if (spa_feature_is_active(dp->dp_spa,
214 &spa_feature_table[SPA_FEATURE_EMPTY_BPOBJ])) {
215 err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
216 DMU_POOL_EMPTY_BPOBJ, sizeof (uint64_t), 1,
217 &dp->dp_empty_bpobj);
222 err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
223 DMU_POOL_TMP_USERREFS, sizeof (uint64_t), 1,
224 &dp->dp_tmp_userrefs_obj);
230 err = dsl_scan_init(dp, dp->dp_tx.tx_open_txg);
233 rrw_exit(&dp->dp_config_rwlock, FTAG);
238 dsl_pool_close(dsl_pool_t *dp)
240 /* drop our references from dsl_pool_open() */
243 * Since we held the origin_snap from "syncing" context (which
244 * includes pool-opening context), it actually only got a "ref"
245 * and not a hold, so just drop that here.
247 if (dp->dp_origin_snap)
248 dsl_dataset_rele(dp->dp_origin_snap, dp);
250 dsl_dir_rele(dp->dp_mos_dir, dp);
252 dsl_dir_rele(dp->dp_free_dir, dp);
254 dsl_dir_rele(dp->dp_root_dir, dp);
256 bpobj_close(&dp->dp_free_bpobj);
258 /* undo the dmu_objset_open_impl(mos) from dsl_pool_open() */
259 if (dp->dp_meta_objset)
260 dmu_objset_evict(dp->dp_meta_objset);
262 txg_list_destroy(&dp->dp_dirty_datasets);
263 txg_list_destroy(&dp->dp_dirty_zilogs);
264 txg_list_destroy(&dp->dp_sync_tasks);
265 txg_list_destroy(&dp->dp_dirty_dirs);
267 arc_flush(dp->dp_spa);
270 rrw_destroy(&dp->dp_config_rwlock);
271 mutex_destroy(&dp->dp_lock);
272 taskq_destroy(dp->dp_vnrele_taskq);
274 kmem_free(dp->dp_blkstats, sizeof (zfs_all_blkstats_t));
275 kmem_free(dp, sizeof (dsl_pool_t));
279 dsl_pool_create(spa_t *spa, nvlist_t *zplprops, uint64_t txg)
282 dsl_pool_t *dp = dsl_pool_open_impl(spa, txg);
283 dmu_tx_t *tx = dmu_tx_create_assigned(dp, txg);
288 rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG);
290 /* create and open the MOS (meta-objset) */
291 dp->dp_meta_objset = dmu_objset_create_impl(spa,
292 NULL, &dp->dp_meta_rootbp, DMU_OST_META, tx);
294 /* create the pool directory */
295 err = zap_create_claim(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
296 DMU_OT_OBJECT_DIRECTORY, DMU_OT_NONE, 0, tx);
299 /* Initialize scan structures */
300 VERIFY0(dsl_scan_init(dp, txg));
302 /* create and open the root dir */
303 dp->dp_root_dir_obj = dsl_dir_create_sync(dp, NULL, NULL, tx);
304 VERIFY0(dsl_dir_hold_obj(dp, dp->dp_root_dir_obj,
305 NULL, dp, &dp->dp_root_dir));
307 /* create and open the meta-objset dir */
308 (void) dsl_dir_create_sync(dp, dp->dp_root_dir, MOS_DIR_NAME, tx);
309 VERIFY0(dsl_pool_open_special_dir(dp,
310 MOS_DIR_NAME, &dp->dp_mos_dir));
312 if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
313 /* create and open the free dir */
314 (void) dsl_dir_create_sync(dp, dp->dp_root_dir,
316 VERIFY0(dsl_pool_open_special_dir(dp,
317 FREE_DIR_NAME, &dp->dp_free_dir));
319 /* create and open the free_bplist */
320 obj = bpobj_alloc(dp->dp_meta_objset, SPA_MAXBLOCKSIZE, tx);
321 VERIFY(zap_add(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
322 DMU_POOL_FREE_BPOBJ, sizeof (uint64_t), 1, &obj, tx) == 0);
323 VERIFY0(bpobj_open(&dp->dp_free_bpobj,
324 dp->dp_meta_objset, obj));
327 if (spa_version(spa) >= SPA_VERSION_DSL_SCRUB)
328 dsl_pool_create_origin(dp, tx);
330 /* create the root dataset */
331 obj = dsl_dataset_create_sync_dd(dp->dp_root_dir, NULL, 0, tx);
333 /* create the root objset */
334 VERIFY0(dsl_dataset_hold_obj(dp, obj, FTAG, &ds));
335 os = dmu_objset_create_impl(dp->dp_spa, ds,
336 dsl_dataset_get_blkptr(ds), DMU_OST_ZFS, tx);
338 zfs_create_fs(os, kcred, zplprops, tx);
340 dsl_dataset_rele(ds, FTAG);
344 rrw_exit(&dp->dp_config_rwlock, FTAG);
350 * Account for the meta-objset space in its placeholder dsl_dir.
353 dsl_pool_mos_diduse_space(dsl_pool_t *dp,
354 int64_t used, int64_t comp, int64_t uncomp)
356 ASSERT3U(comp, ==, uncomp); /* it's all metadata */
357 mutex_enter(&dp->dp_lock);
358 dp->dp_mos_used_delta += used;
359 dp->dp_mos_compressed_delta += comp;
360 dp->dp_mos_uncompressed_delta += uncomp;
361 mutex_exit(&dp->dp_lock);
365 deadlist_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
367 dsl_deadlist_t *dl = arg;
368 dsl_deadlist_insert(dl, bp, tx);
373 dsl_pool_sync(dsl_pool_t *dp, uint64_t txg)
379 objset_t *mos = dp->dp_meta_objset;
380 hrtime_t start, write_time;
381 uint64_t data_written;
383 list_t synced_datasets;
385 list_create(&synced_datasets, sizeof (dsl_dataset_t),
386 offsetof(dsl_dataset_t, ds_synced_link));
389 * We need to copy dp_space_towrite() before doing
390 * dsl_sync_task_sync(), because
391 * dsl_dataset_snapshot_reserve_space() will increase
392 * dp_space_towrite but not actually write anything.
394 data_written = dp->dp_space_towrite[txg & TXG_MASK];
396 tx = dmu_tx_create_assigned(dp, txg);
398 dp->dp_read_overhead = 0;
401 zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
402 while (ds = txg_list_remove(&dp->dp_dirty_datasets, txg)) {
404 * We must not sync any non-MOS datasets twice, because
405 * we may have taken a snapshot of them. However, we
406 * may sync newly-created datasets on pass 2.
408 ASSERT(!list_link_active(&ds->ds_synced_link));
409 list_insert_tail(&synced_datasets, ds);
410 dsl_dataset_sync(ds, zio, tx);
412 DTRACE_PROBE(pool_sync__1setup);
415 write_time = gethrtime() - start;
417 DTRACE_PROBE(pool_sync__2rootzio);
420 * After the data blocks have been written (ensured by the zio_wait()
421 * above), update the user/group space accounting.
423 for (ds = list_head(&synced_datasets); ds;
424 ds = list_next(&synced_datasets, ds))
425 dmu_objset_do_userquota_updates(ds->ds_objset, tx);
428 * Sync the datasets again to push out the changes due to
429 * userspace updates. This must be done before we process the
430 * sync tasks, so that any snapshots will have the correct
431 * user accounting information (and we won't get confused
432 * about which blocks are part of the snapshot).
434 zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
435 while (ds = txg_list_remove(&dp->dp_dirty_datasets, txg)) {
436 ASSERT(list_link_active(&ds->ds_synced_link));
437 dmu_buf_rele(ds->ds_dbuf, ds);
438 dsl_dataset_sync(ds, zio, tx);
443 * Now that the datasets have been completely synced, we can
444 * clean up our in-memory structures accumulated while syncing:
446 * - move dead blocks from the pending deadlist to the on-disk deadlist
447 * - release hold from dsl_dataset_dirty()
449 while (ds = list_remove_head(&synced_datasets)) {
450 objset_t *os = ds->ds_objset;
451 bplist_iterate(&ds->ds_pending_deadlist,
452 deadlist_enqueue_cb, &ds->ds_deadlist, tx);
453 ASSERT(!dmu_objset_is_dirty(os, txg));
454 dmu_buf_rele(ds->ds_dbuf, ds);
458 while (dd = txg_list_remove(&dp->dp_dirty_dirs, txg))
459 dsl_dir_sync(dd, tx);
460 write_time += gethrtime() - start;
463 * The MOS's space is accounted for in the pool/$MOS
464 * (dp_mos_dir). We can't modify the mos while we're syncing
465 * it, so we remember the deltas and apply them here.
467 if (dp->dp_mos_used_delta != 0 || dp->dp_mos_compressed_delta != 0 ||
468 dp->dp_mos_uncompressed_delta != 0) {
469 dsl_dir_diduse_space(dp->dp_mos_dir, DD_USED_HEAD,
470 dp->dp_mos_used_delta,
471 dp->dp_mos_compressed_delta,
472 dp->dp_mos_uncompressed_delta, tx);
473 dp->dp_mos_used_delta = 0;
474 dp->dp_mos_compressed_delta = 0;
475 dp->dp_mos_uncompressed_delta = 0;
479 if (list_head(&mos->os_dirty_dnodes[txg & TXG_MASK]) != NULL ||
480 list_head(&mos->os_free_dnodes[txg & TXG_MASK]) != NULL) {
481 zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
482 dmu_objset_sync(mos, zio, tx);
485 dprintf_bp(&dp->dp_meta_rootbp, "meta objset rootbp is %s", "");
486 spa_set_rootblkptr(dp->dp_spa, &dp->dp_meta_rootbp);
488 write_time += gethrtime() - start;
489 DTRACE_PROBE2(pool_sync__4io, hrtime_t, write_time,
490 hrtime_t, dp->dp_read_overhead);
491 write_time -= dp->dp_read_overhead;
494 * If we modify a dataset in the same txg that we want to destroy it,
495 * its dsl_dir's dd_dbuf will be dirty, and thus have a hold on it.
496 * dsl_dir_destroy_check() will fail if there are unexpected holds.
497 * Therefore, we want to sync the MOS (thus syncing the dd_dbuf
498 * and clearing the hold on it) before we process the sync_tasks.
499 * The MOS data dirtied by the sync_tasks will be synced on the next
502 DTRACE_PROBE(pool_sync__3task);
503 if (!txg_list_empty(&dp->dp_sync_tasks, txg)) {
504 dsl_sync_task_t *dst;
506 * No more sync tasks should have been added while we
509 ASSERT(spa_sync_pass(dp->dp_spa) == 1);
510 while (dst = txg_list_remove(&dp->dp_sync_tasks, txg))
511 dsl_sync_task_sync(dst, tx);
516 dp->dp_space_towrite[txg & TXG_MASK] = 0;
517 ASSERT(dp->dp_tempreserved[txg & TXG_MASK] == 0);
520 * If the write limit max has not been explicitly set, set it
521 * to a fraction of available physical memory (default 1/8th).
522 * Note that we must inflate the limit because the spa
523 * inflates write sizes to account for data replication.
524 * Check this each sync phase to catch changing memory size.
526 if (physmem != old_physmem && zfs_write_limit_shift) {
527 mutex_enter(&zfs_write_limit_lock);
528 old_physmem = physmem;
529 zfs_write_limit_max = ptob(physmem) >> zfs_write_limit_shift;
530 zfs_write_limit_inflated = MAX(zfs_write_limit_min,
531 spa_get_asize(dp->dp_spa, zfs_write_limit_max));
532 mutex_exit(&zfs_write_limit_lock);
536 * Attempt to keep the sync time consistent by adjusting the
537 * amount of write traffic allowed into each transaction group.
538 * Weight the throughput calculation towards the current value:
539 * thru = 3/4 old_thru + 1/4 new_thru
541 * Note: write_time is in nanosecs, so write_time/MICROSEC
544 ASSERT(zfs_write_limit_min > 0);
545 if (data_written > zfs_write_limit_min / 8 && write_time > MICROSEC) {
546 uint64_t throughput = data_written / (write_time / MICROSEC);
548 if (dp->dp_throughput)
549 dp->dp_throughput = throughput / 4 +
550 3 * dp->dp_throughput / 4;
552 dp->dp_throughput = throughput;
553 dp->dp_write_limit = MIN(zfs_write_limit_inflated,
554 MAX(zfs_write_limit_min,
555 dp->dp_throughput * zfs_txg_synctime_ms));
560 dsl_pool_sync_done(dsl_pool_t *dp, uint64_t txg)
565 while (zilog = txg_list_remove(&dp->dp_dirty_zilogs, txg)) {
566 ds = dmu_objset_ds(zilog->zl_os);
567 zil_clean(zilog, txg);
568 ASSERT(!dmu_objset_is_dirty(zilog->zl_os, txg));
569 dmu_buf_rele(ds->ds_dbuf, zilog);
571 ASSERT(!dmu_objset_is_dirty(dp->dp_meta_objset, txg));
575 * TRUE if the current thread is the tx_sync_thread or if we
576 * are being called from SPA context during pool initialization.
579 dsl_pool_sync_context(dsl_pool_t *dp)
581 return (curthread == dp->dp_tx.tx_sync_thread ||
582 spa_is_initializing(dp->dp_spa));
586 dsl_pool_adjustedsize(dsl_pool_t *dp, boolean_t netfree)
588 uint64_t space, resv;
591 * Reserve about 1.6% (1/64), or at least 32MB, for allocation
593 * XXX The intent log is not accounted for, so it must fit
596 * If we're trying to assess whether it's OK to do a free,
597 * cut the reservation in half to allow forward progress
598 * (e.g. make it possible to rm(1) files from a full pool).
600 space = spa_get_dspace(dp->dp_spa);
601 resv = MAX(space >> 6, SPA_MINDEVSIZE >> 1);
605 return (space - resv);
609 dsl_pool_tempreserve_space(dsl_pool_t *dp, uint64_t space, dmu_tx_t *tx)
611 uint64_t reserved = 0;
612 uint64_t write_limit = (zfs_write_limit_override ?
613 zfs_write_limit_override : dp->dp_write_limit);
615 if (zfs_no_write_throttle) {
616 atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK],
622 * Check to see if we have exceeded the maximum allowed IO for
623 * this transaction group. We can do this without locks since
624 * a little slop here is ok. Note that we do the reserved check
625 * with only half the requested reserve: this is because the
626 * reserve requests are worst-case, and we really don't want to
627 * throttle based off of worst-case estimates.
629 if (write_limit > 0) {
630 reserved = dp->dp_space_towrite[tx->tx_txg & TXG_MASK]
631 + dp->dp_tempreserved[tx->tx_txg & TXG_MASK] / 2;
633 if (reserved && reserved > write_limit)
634 return (SET_ERROR(ERESTART));
637 atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK], space);
640 * If this transaction group is over 7/8ths capacity, delay
641 * the caller 1 clock tick. This will slow down the "fill"
642 * rate until the sync process can catch up with us.
644 if (reserved && reserved > (write_limit - (write_limit >> 3)))
645 txg_delay(dp, tx->tx_txg, 1);
651 dsl_pool_tempreserve_clear(dsl_pool_t *dp, int64_t space, dmu_tx_t *tx)
653 ASSERT(dp->dp_tempreserved[tx->tx_txg & TXG_MASK] >= space);
654 atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK], -space);
658 dsl_pool_memory_pressure(dsl_pool_t *dp)
660 uint64_t space_inuse = 0;
663 if (dp->dp_write_limit == zfs_write_limit_min)
666 for (i = 0; i < TXG_SIZE; i++) {
667 space_inuse += dp->dp_space_towrite[i];
668 space_inuse += dp->dp_tempreserved[i];
670 dp->dp_write_limit = MAX(zfs_write_limit_min,
671 MIN(dp->dp_write_limit, space_inuse / 4));
675 dsl_pool_willuse_space(dsl_pool_t *dp, int64_t space, dmu_tx_t *tx)
678 mutex_enter(&dp->dp_lock);
679 dp->dp_space_towrite[tx->tx_txg & TXG_MASK] += space;
680 mutex_exit(&dp->dp_lock);
686 upgrade_clones_cb(dsl_pool_t *dp, dsl_dataset_t *hds, void *arg)
689 dsl_dataset_t *ds, *prev = NULL;
692 err = dsl_dataset_hold_obj(dp, hds->ds_object, FTAG, &ds);
696 while (ds->ds_phys->ds_prev_snap_obj != 0) {
697 err = dsl_dataset_hold_obj(dp, ds->ds_phys->ds_prev_snap_obj,
700 dsl_dataset_rele(ds, FTAG);
704 if (prev->ds_phys->ds_next_snap_obj != ds->ds_object)
706 dsl_dataset_rele(ds, FTAG);
712 prev = dp->dp_origin_snap;
715 * The $ORIGIN can't have any data, or the accounting
718 ASSERT0(prev->ds_phys->ds_bp.blk_birth);
720 /* The origin doesn't get attached to itself */
721 if (ds->ds_object == prev->ds_object) {
722 dsl_dataset_rele(ds, FTAG);
726 dmu_buf_will_dirty(ds->ds_dbuf, tx);
727 ds->ds_phys->ds_prev_snap_obj = prev->ds_object;
728 ds->ds_phys->ds_prev_snap_txg = prev->ds_phys->ds_creation_txg;
730 dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
731 ds->ds_dir->dd_phys->dd_origin_obj = prev->ds_object;
733 dmu_buf_will_dirty(prev->ds_dbuf, tx);
734 prev->ds_phys->ds_num_children++;
736 if (ds->ds_phys->ds_next_snap_obj == 0) {
737 ASSERT(ds->ds_prev == NULL);
738 VERIFY0(dsl_dataset_hold_obj(dp,
739 ds->ds_phys->ds_prev_snap_obj, ds, &ds->ds_prev));
743 ASSERT3U(ds->ds_dir->dd_phys->dd_origin_obj, ==, prev->ds_object);
744 ASSERT3U(ds->ds_phys->ds_prev_snap_obj, ==, prev->ds_object);
746 if (prev->ds_phys->ds_next_clones_obj == 0) {
747 dmu_buf_will_dirty(prev->ds_dbuf, tx);
748 prev->ds_phys->ds_next_clones_obj =
749 zap_create(dp->dp_meta_objset,
750 DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx);
752 VERIFY0(zap_add_int(dp->dp_meta_objset,
753 prev->ds_phys->ds_next_clones_obj, ds->ds_object, tx));
755 dsl_dataset_rele(ds, FTAG);
756 if (prev != dp->dp_origin_snap)
757 dsl_dataset_rele(prev, FTAG);
762 dsl_pool_upgrade_clones(dsl_pool_t *dp, dmu_tx_t *tx)
764 ASSERT(dmu_tx_is_syncing(tx));
765 ASSERT(dp->dp_origin_snap != NULL);
767 VERIFY0(dmu_objset_find_dp(dp, dp->dp_root_dir_obj, upgrade_clones_cb,
768 tx, DS_FIND_CHILDREN));
773 upgrade_dir_clones_cb(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg)
776 objset_t *mos = dp->dp_meta_objset;
778 if (ds->ds_dir->dd_phys->dd_origin_obj != 0) {
779 dsl_dataset_t *origin;
781 VERIFY0(dsl_dataset_hold_obj(dp,
782 ds->ds_dir->dd_phys->dd_origin_obj, FTAG, &origin));
784 if (origin->ds_dir->dd_phys->dd_clones == 0) {
785 dmu_buf_will_dirty(origin->ds_dir->dd_dbuf, tx);
786 origin->ds_dir->dd_phys->dd_clones = zap_create(mos,
787 DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx);
790 VERIFY0(zap_add_int(dp->dp_meta_objset,
791 origin->ds_dir->dd_phys->dd_clones, ds->ds_object, tx));
793 dsl_dataset_rele(origin, FTAG);
799 dsl_pool_upgrade_dir_clones(dsl_pool_t *dp, dmu_tx_t *tx)
801 ASSERT(dmu_tx_is_syncing(tx));
804 (void) dsl_dir_create_sync(dp, dp->dp_root_dir, FREE_DIR_NAME, tx);
805 VERIFY0(dsl_pool_open_special_dir(dp,
806 FREE_DIR_NAME, &dp->dp_free_dir));
809 * We can't use bpobj_alloc(), because spa_version() still
810 * returns the old version, and we need a new-version bpobj with
811 * subobj support. So call dmu_object_alloc() directly.
813 obj = dmu_object_alloc(dp->dp_meta_objset, DMU_OT_BPOBJ,
814 SPA_MAXBLOCKSIZE, DMU_OT_BPOBJ_HDR, sizeof (bpobj_phys_t), tx);
815 VERIFY0(zap_add(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
816 DMU_POOL_FREE_BPOBJ, sizeof (uint64_t), 1, &obj, tx));
817 VERIFY0(bpobj_open(&dp->dp_free_bpobj, dp->dp_meta_objset, obj));
819 VERIFY0(dmu_objset_find_dp(dp, dp->dp_root_dir_obj,
820 upgrade_dir_clones_cb, tx, DS_FIND_CHILDREN));
824 dsl_pool_create_origin(dsl_pool_t *dp, dmu_tx_t *tx)
829 ASSERT(dmu_tx_is_syncing(tx));
830 ASSERT(dp->dp_origin_snap == NULL);
831 ASSERT(rrw_held(&dp->dp_config_rwlock, RW_WRITER));
833 /* create the origin dir, ds, & snap-ds */
834 dsobj = dsl_dataset_create_sync(dp->dp_root_dir, ORIGIN_DIR_NAME,
836 VERIFY0(dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
837 dsl_dataset_snapshot_sync_impl(ds, ORIGIN_DIR_NAME, tx);
838 VERIFY0(dsl_dataset_hold_obj(dp, ds->ds_phys->ds_prev_snap_obj,
839 dp, &dp->dp_origin_snap));
840 dsl_dataset_rele(ds, FTAG);
844 dsl_pool_vnrele_taskq(dsl_pool_t *dp)
846 return (dp->dp_vnrele_taskq);
850 * Walk through the pool-wide zap object of temporary snapshot user holds
854 dsl_pool_clean_tmp_userrefs(dsl_pool_t *dp)
858 objset_t *mos = dp->dp_meta_objset;
859 uint64_t zapobj = dp->dp_tmp_userrefs_obj;
864 ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS);
866 holds = fnvlist_alloc();
868 for (zap_cursor_init(&zc, mos, zapobj);
869 zap_cursor_retrieve(&zc, &za) == 0;
870 zap_cursor_advance(&zc)) {
874 htag = strchr(za.za_name, '-');
877 if (nvlist_lookup_nvlist(holds, za.za_name, &tags) != 0) {
878 tags = fnvlist_alloc();
879 fnvlist_add_boolean(tags, htag);
880 fnvlist_add_nvlist(holds, za.za_name, tags);
883 fnvlist_add_boolean(tags, htag);
886 dsl_dataset_user_release_tmp(dp, holds);
888 zap_cursor_fini(&zc);
892 * Create the pool-wide zap object for storing temporary snapshot holds.
895 dsl_pool_user_hold_create_obj(dsl_pool_t *dp, dmu_tx_t *tx)
897 objset_t *mos = dp->dp_meta_objset;
899 ASSERT(dp->dp_tmp_userrefs_obj == 0);
900 ASSERT(dmu_tx_is_syncing(tx));
902 dp->dp_tmp_userrefs_obj = zap_create_link(mos, DMU_OT_USERREFS,
903 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_TMP_USERREFS, tx);
907 dsl_pool_user_hold_rele_impl(dsl_pool_t *dp, uint64_t dsobj,
908 const char *tag, uint64_t now, dmu_tx_t *tx, boolean_t holding)
910 objset_t *mos = dp->dp_meta_objset;
911 uint64_t zapobj = dp->dp_tmp_userrefs_obj;
915 ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS);
916 ASSERT(dmu_tx_is_syncing(tx));
919 * If the pool was created prior to SPA_VERSION_USERREFS, the
920 * zap object for temporary holds might not exist yet.
924 dsl_pool_user_hold_create_obj(dp, tx);
925 zapobj = dp->dp_tmp_userrefs_obj;
927 return (SET_ERROR(ENOENT));
931 name = kmem_asprintf("%llx-%s", (u_longlong_t)dsobj, tag);
933 error = zap_add(mos, zapobj, name, 8, 1, &now, tx);
935 error = zap_remove(mos, zapobj, name, tx);
942 * Add a temporary hold for the given dataset object and tag.
945 dsl_pool_user_hold(dsl_pool_t *dp, uint64_t dsobj, const char *tag,
946 uint64_t now, dmu_tx_t *tx)
948 return (dsl_pool_user_hold_rele_impl(dp, dsobj, tag, now, tx, B_TRUE));
952 * Release a temporary hold for the given dataset object and tag.
955 dsl_pool_user_release(dsl_pool_t *dp, uint64_t dsobj, const char *tag,
958 return (dsl_pool_user_hold_rele_impl(dp, dsobj, tag, 0,
963 * DSL Pool Configuration Lock
965 * The dp_config_rwlock protects against changes to DSL state (e.g. dataset
966 * creation / destruction / rename / property setting). It must be held for
967 * read to hold a dataset or dsl_dir. I.e. you must call
968 * dsl_pool_config_enter() or dsl_pool_hold() before calling
969 * dsl_{dataset,dir}_hold{_obj}. In most circumstances, the dp_config_rwlock
970 * must be held continuously until all datasets and dsl_dirs are released.
972 * The only exception to this rule is that if a "long hold" is placed on
973 * a dataset, then the dp_config_rwlock may be dropped while the dataset
974 * is still held. The long hold will prevent the dataset from being
975 * destroyed -- the destroy will fail with EBUSY. A long hold can be
976 * obtained by calling dsl_dataset_long_hold(), or by "owning" a dataset
977 * (by calling dsl_{dataset,objset}_{try}own{_obj}).
979 * Legitimate long-holders (including owners) should be long-running, cancelable
980 * tasks that should cause "zfs destroy" to fail. This includes DMU
981 * consumers (i.e. a ZPL filesystem being mounted or ZVOL being open),
982 * "zfs send", and "zfs diff". There are several other long-holders whose
983 * uses are suboptimal (e.g. "zfs promote", and zil_suspend()).
985 * The usual formula for long-holding would be:
988 * ... perform checks ...
989 * dsl_dataset_long_hold()
991 * ... perform long-running task ...
992 * dsl_dataset_long_rele()
995 * Note that when the long hold is released, the dataset is still held but
996 * the pool is not held. The dataset may change arbitrarily during this time
997 * (e.g. it could be destroyed). Therefore you shouldn't do anything to the
998 * dataset except release it.
1000 * User-initiated operations (e.g. ioctls, zfs_ioc_*()) are either read-only
1001 * or modifying operations.
1003 * Modifying operations should generally use dsl_sync_task(). The synctask
1004 * infrastructure enforces proper locking strategy with respect to the
1005 * dp_config_rwlock. See the comment above dsl_sync_task() for details.
1007 * Read-only operations will manually hold the pool, then the dataset, obtain
1008 * information from the dataset, then release the pool and dataset.
1009 * dmu_objset_{hold,rele}() are convenience routines that also do the pool
1014 dsl_pool_hold(const char *name, void *tag, dsl_pool_t **dp)
1019 error = spa_open(name, &spa, tag);
1021 *dp = spa_get_dsl(spa);
1022 dsl_pool_config_enter(*dp, tag);
1028 dsl_pool_rele(dsl_pool_t *dp, void *tag)
1030 dsl_pool_config_exit(dp, tag);
1031 spa_close(dp->dp_spa, tag);
1035 dsl_pool_config_enter(dsl_pool_t *dp, void *tag)
1038 * We use a "reentrant" reader-writer lock, but not reentrantly.
1040 * The rrwlock can (with the track_all flag) track all reading threads,
1041 * which is very useful for debugging which code path failed to release
1042 * the lock, and for verifying that the *current* thread does hold
1045 * (Unlike a rwlock, which knows that N threads hold it for
1046 * read, but not *which* threads, so rw_held(RW_READER) returns TRUE
1047 * if any thread holds it for read, even if this thread doesn't).
1049 ASSERT(!rrw_held(&dp->dp_config_rwlock, RW_READER));
1050 rrw_enter(&dp->dp_config_rwlock, RW_READER, tag);
1054 dsl_pool_config_exit(dsl_pool_t *dp, void *tag)
1056 rrw_exit(&dp->dp_config_rwlock, tag);
1060 dsl_pool_config_held(dsl_pool_t *dp)
1062 return (RRW_LOCK_HELD(&dp->dp_config_rwlock));