xref: /titanic_50/usr/src/uts/common/fs/zfs/dsl_pool.c (revision 452fab4bfe0f4a0322a258885624366426683ae8)
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 	 *  - release hold from dsl_dataset_dirty()
419 	 */
420 	while (ds = list_remove_head(&synced_datasets)) {
421 		objset_t *os = ds->ds_objset;
422 		bplist_iterate(&ds->ds_pending_deadlist,
423 		    deadlist_enqueue_cb, &ds->ds_deadlist, tx);
424 		ASSERT(!dmu_objset_is_dirty(os, txg));
425 		dmu_buf_rele(ds->ds_dbuf, ds);
426 	}
427 
428 	start = gethrtime();
429 	while (dd = txg_list_remove(&dp->dp_dirty_dirs, txg))
430 		dsl_dir_sync(dd, tx);
431 	write_time += gethrtime() - start;
432 
433 	/*
434 	 * The MOS's space is accounted for in the pool/$MOS
435 	 * (dp_mos_dir).  We can't modify the mos while we're syncing
436 	 * it, so we remember the deltas and apply them here.
437 	 */
438 	if (dp->dp_mos_used_delta != 0 || dp->dp_mos_compressed_delta != 0 ||
439 	    dp->dp_mos_uncompressed_delta != 0) {
440 		dsl_dir_diduse_space(dp->dp_mos_dir, DD_USED_HEAD,
441 		    dp->dp_mos_used_delta,
442 		    dp->dp_mos_compressed_delta,
443 		    dp->dp_mos_uncompressed_delta, tx);
444 		dp->dp_mos_used_delta = 0;
445 		dp->dp_mos_compressed_delta = 0;
446 		dp->dp_mos_uncompressed_delta = 0;
447 	}
448 
449 	start = gethrtime();
450 	if (list_head(&mos->os_dirty_dnodes[txg & TXG_MASK]) != NULL ||
451 	    list_head(&mos->os_free_dnodes[txg & TXG_MASK]) != NULL) {
452 		zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
453 		dmu_objset_sync(mos, zio, tx);
454 		err = zio_wait(zio);
455 		ASSERT(err == 0);
456 		dprintf_bp(&dp->dp_meta_rootbp, "meta objset rootbp is %s", "");
457 		spa_set_rootblkptr(dp->dp_spa, &dp->dp_meta_rootbp);
458 	}
459 	write_time += gethrtime() - start;
460 	DTRACE_PROBE2(pool_sync__4io, hrtime_t, write_time,
461 	    hrtime_t, dp->dp_read_overhead);
462 	write_time -= dp->dp_read_overhead;
463 
464 	/*
465 	 * If we modify a dataset in the same txg that we want to destroy it,
466 	 * its dsl_dir's dd_dbuf will be dirty, and thus have a hold on it.
467 	 * dsl_dir_destroy_check() will fail if there are unexpected holds.
468 	 * Therefore, we want to sync the MOS (thus syncing the dd_dbuf
469 	 * and clearing the hold on it) before we process the sync_tasks.
470 	 * The MOS data dirtied by the sync_tasks will be synced on the next
471 	 * pass.
472 	 */
473 	DTRACE_PROBE(pool_sync__3task);
474 	if (!txg_list_empty(&dp->dp_sync_tasks, txg)) {
475 		dsl_sync_task_group_t *dstg;
476 		/*
477 		 * No more sync tasks should have been added while we
478 		 * were syncing.
479 		 */
480 		ASSERT(spa_sync_pass(dp->dp_spa) == 1);
481 		while (dstg = txg_list_remove(&dp->dp_sync_tasks, txg))
482 			dsl_sync_task_group_sync(dstg, tx);
483 	}
484 
485 	dmu_tx_commit(tx);
486 
487 	dp->dp_space_towrite[txg & TXG_MASK] = 0;
488 	ASSERT(dp->dp_tempreserved[txg & TXG_MASK] == 0);
489 
490 	/*
491 	 * If the write limit max has not been explicitly set, set it
492 	 * to a fraction of available physical memory (default 1/8th).
493 	 * Note that we must inflate the limit because the spa
494 	 * inflates write sizes to account for data replication.
495 	 * Check this each sync phase to catch changing memory size.
496 	 */
497 	if (physmem != old_physmem && zfs_write_limit_shift) {
498 		mutex_enter(&zfs_write_limit_lock);
499 		old_physmem = physmem;
500 		zfs_write_limit_max = ptob(physmem) >> zfs_write_limit_shift;
501 		zfs_write_limit_inflated = MAX(zfs_write_limit_min,
502 		    spa_get_asize(dp->dp_spa, zfs_write_limit_max));
503 		mutex_exit(&zfs_write_limit_lock);
504 	}
505 
506 	/*
507 	 * Attempt to keep the sync time consistent by adjusting the
508 	 * amount of write traffic allowed into each transaction group.
509 	 * Weight the throughput calculation towards the current value:
510 	 * 	thru = 3/4 old_thru + 1/4 new_thru
511 	 *
512 	 * Note: write_time is in nanosecs, so write_time/MICROSEC
513 	 * yields millisecs
514 	 */
515 	ASSERT(zfs_write_limit_min > 0);
516 	if (data_written > zfs_write_limit_min / 8 && write_time > MICROSEC) {
517 		uint64_t throughput = data_written / (write_time / MICROSEC);
518 
519 		if (dp->dp_throughput)
520 			dp->dp_throughput = throughput / 4 +
521 			    3 * dp->dp_throughput / 4;
522 		else
523 			dp->dp_throughput = throughput;
524 		dp->dp_write_limit = MIN(zfs_write_limit_inflated,
525 		    MAX(zfs_write_limit_min,
526 		    dp->dp_throughput * zfs_txg_synctime_ms));
527 	}
528 }
529 
530 void
531 dsl_pool_sync_done(dsl_pool_t *dp, uint64_t txg)
532 {
533 	zilog_t *zilog;
534 	dsl_dataset_t *ds;
535 
536 	while (zilog = txg_list_remove(&dp->dp_dirty_zilogs, txg)) {
537 		ds = dmu_objset_ds(zilog->zl_os);
538 		zil_clean(zilog, txg);
539 		ASSERT(!dmu_objset_is_dirty(zilog->zl_os, txg));
540 		dmu_buf_rele(ds->ds_dbuf, zilog);
541 	}
542 	ASSERT(!dmu_objset_is_dirty(dp->dp_meta_objset, txg));
543 }
544 
545 /*
546  * TRUE if the current thread is the tx_sync_thread or if we
547  * are being called from SPA context during pool initialization.
548  */
549 int
550 dsl_pool_sync_context(dsl_pool_t *dp)
551 {
552 	return (curthread == dp->dp_tx.tx_sync_thread ||
553 	    spa_is_initializing(dp->dp_spa));
554 }
555 
556 uint64_t
557 dsl_pool_adjustedsize(dsl_pool_t *dp, boolean_t netfree)
558 {
559 	uint64_t space, resv;
560 
561 	/*
562 	 * Reserve about 1.6% (1/64), or at least 32MB, for allocation
563 	 * efficiency.
564 	 * XXX The intent log is not accounted for, so it must fit
565 	 * within this slop.
566 	 *
567 	 * If we're trying to assess whether it's OK to do a free,
568 	 * cut the reservation in half to allow forward progress
569 	 * (e.g. make it possible to rm(1) files from a full pool).
570 	 */
571 	space = spa_get_dspace(dp->dp_spa);
572 	resv = MAX(space >> 6, SPA_MINDEVSIZE >> 1);
573 	if (netfree)
574 		resv >>= 1;
575 
576 	return (space - resv);
577 }
578 
579 int
580 dsl_pool_tempreserve_space(dsl_pool_t *dp, uint64_t space, dmu_tx_t *tx)
581 {
582 	uint64_t reserved = 0;
583 	uint64_t write_limit = (zfs_write_limit_override ?
584 	    zfs_write_limit_override : dp->dp_write_limit);
585 
586 	if (zfs_no_write_throttle) {
587 		atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK],
588 		    space);
589 		return (0);
590 	}
591 
592 	/*
593 	 * Check to see if we have exceeded the maximum allowed IO for
594 	 * this transaction group.  We can do this without locks since
595 	 * a little slop here is ok.  Note that we do the reserved check
596 	 * with only half the requested reserve: this is because the
597 	 * reserve requests are worst-case, and we really don't want to
598 	 * throttle based off of worst-case estimates.
599 	 */
600 	if (write_limit > 0) {
601 		reserved = dp->dp_space_towrite[tx->tx_txg & TXG_MASK]
602 		    + dp->dp_tempreserved[tx->tx_txg & TXG_MASK] / 2;
603 
604 		if (reserved && reserved > write_limit)
605 			return (ERESTART);
606 	}
607 
608 	atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK], space);
609 
610 	/*
611 	 * If this transaction group is over 7/8ths capacity, delay
612 	 * the caller 1 clock tick.  This will slow down the "fill"
613 	 * rate until the sync process can catch up with us.
614 	 */
615 	if (reserved && reserved > (write_limit - (write_limit >> 3)))
616 		txg_delay(dp, tx->tx_txg, 1);
617 
618 	return (0);
619 }
620 
621 void
622 dsl_pool_tempreserve_clear(dsl_pool_t *dp, int64_t space, dmu_tx_t *tx)
623 {
624 	ASSERT(dp->dp_tempreserved[tx->tx_txg & TXG_MASK] >= space);
625 	atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK], -space);
626 }
627 
628 void
629 dsl_pool_memory_pressure(dsl_pool_t *dp)
630 {
631 	uint64_t space_inuse = 0;
632 	int i;
633 
634 	if (dp->dp_write_limit == zfs_write_limit_min)
635 		return;
636 
637 	for (i = 0; i < TXG_SIZE; i++) {
638 		space_inuse += dp->dp_space_towrite[i];
639 		space_inuse += dp->dp_tempreserved[i];
640 	}
641 	dp->dp_write_limit = MAX(zfs_write_limit_min,
642 	    MIN(dp->dp_write_limit, space_inuse / 4));
643 }
644 
645 void
646 dsl_pool_willuse_space(dsl_pool_t *dp, int64_t space, dmu_tx_t *tx)
647 {
648 	if (space > 0) {
649 		mutex_enter(&dp->dp_lock);
650 		dp->dp_space_towrite[tx->tx_txg & TXG_MASK] += space;
651 		mutex_exit(&dp->dp_lock);
652 	}
653 }
654 
655 /* ARGSUSED */
656 static int
657 upgrade_clones_cb(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg)
658 {
659 	dmu_tx_t *tx = arg;
660 	dsl_dataset_t *ds, *prev = NULL;
661 	int err;
662 	dsl_pool_t *dp = spa_get_dsl(spa);
663 
664 	err = dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds);
665 	if (err)
666 		return (err);
667 
668 	while (ds->ds_phys->ds_prev_snap_obj != 0) {
669 		err = dsl_dataset_hold_obj(dp, ds->ds_phys->ds_prev_snap_obj,
670 		    FTAG, &prev);
671 		if (err) {
672 			dsl_dataset_rele(ds, FTAG);
673 			return (err);
674 		}
675 
676 		if (prev->ds_phys->ds_next_snap_obj != ds->ds_object)
677 			break;
678 		dsl_dataset_rele(ds, FTAG);
679 		ds = prev;
680 		prev = NULL;
681 	}
682 
683 	if (prev == NULL) {
684 		prev = dp->dp_origin_snap;
685 
686 		/*
687 		 * The $ORIGIN can't have any data, or the accounting
688 		 * will be wrong.
689 		 */
690 		ASSERT(prev->ds_phys->ds_bp.blk_birth == 0);
691 
692 		/* The origin doesn't get attached to itself */
693 		if (ds->ds_object == prev->ds_object) {
694 			dsl_dataset_rele(ds, FTAG);
695 			return (0);
696 		}
697 
698 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
699 		ds->ds_phys->ds_prev_snap_obj = prev->ds_object;
700 		ds->ds_phys->ds_prev_snap_txg = prev->ds_phys->ds_creation_txg;
701 
702 		dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
703 		ds->ds_dir->dd_phys->dd_origin_obj = prev->ds_object;
704 
705 		dmu_buf_will_dirty(prev->ds_dbuf, tx);
706 		prev->ds_phys->ds_num_children++;
707 
708 		if (ds->ds_phys->ds_next_snap_obj == 0) {
709 			ASSERT(ds->ds_prev == NULL);
710 			VERIFY(0 == dsl_dataset_hold_obj(dp,
711 			    ds->ds_phys->ds_prev_snap_obj, ds, &ds->ds_prev));
712 		}
713 	}
714 
715 	ASSERT(ds->ds_dir->dd_phys->dd_origin_obj == prev->ds_object);
716 	ASSERT(ds->ds_phys->ds_prev_snap_obj == prev->ds_object);
717 
718 	if (prev->ds_phys->ds_next_clones_obj == 0) {
719 		dmu_buf_will_dirty(prev->ds_dbuf, tx);
720 		prev->ds_phys->ds_next_clones_obj =
721 		    zap_create(dp->dp_meta_objset,
722 		    DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx);
723 	}
724 	VERIFY(0 == zap_add_int(dp->dp_meta_objset,
725 	    prev->ds_phys->ds_next_clones_obj, ds->ds_object, tx));
726 
727 	dsl_dataset_rele(ds, FTAG);
728 	if (prev != dp->dp_origin_snap)
729 		dsl_dataset_rele(prev, FTAG);
730 	return (0);
731 }
732 
733 void
734 dsl_pool_upgrade_clones(dsl_pool_t *dp, dmu_tx_t *tx)
735 {
736 	ASSERT(dmu_tx_is_syncing(tx));
737 	ASSERT(dp->dp_origin_snap != NULL);
738 
739 	VERIFY3U(0, ==, dmu_objset_find_spa(dp->dp_spa, NULL, upgrade_clones_cb,
740 	    tx, DS_FIND_CHILDREN));
741 }
742 
743 /* ARGSUSED */
744 static int
745 upgrade_dir_clones_cb(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg)
746 {
747 	dmu_tx_t *tx = arg;
748 	dsl_dataset_t *ds;
749 	dsl_pool_t *dp = spa_get_dsl(spa);
750 	objset_t *mos = dp->dp_meta_objset;
751 
752 	VERIFY3U(0, ==, dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
753 
754 	if (ds->ds_dir->dd_phys->dd_origin_obj) {
755 		dsl_dataset_t *origin;
756 
757 		VERIFY3U(0, ==, dsl_dataset_hold_obj(dp,
758 		    ds->ds_dir->dd_phys->dd_origin_obj, FTAG, &origin));
759 
760 		if (origin->ds_dir->dd_phys->dd_clones == 0) {
761 			dmu_buf_will_dirty(origin->ds_dir->dd_dbuf, tx);
762 			origin->ds_dir->dd_phys->dd_clones = zap_create(mos,
763 			    DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx);
764 		}
765 
766 		VERIFY3U(0, ==, zap_add_int(dp->dp_meta_objset,
767 		    origin->ds_dir->dd_phys->dd_clones, dsobj, tx));
768 
769 		dsl_dataset_rele(origin, FTAG);
770 	}
771 
772 	dsl_dataset_rele(ds, FTAG);
773 	return (0);
774 }
775 
776 void
777 dsl_pool_upgrade_dir_clones(dsl_pool_t *dp, dmu_tx_t *tx)
778 {
779 	ASSERT(dmu_tx_is_syncing(tx));
780 	uint64_t obj;
781 
782 	(void) dsl_dir_create_sync(dp, dp->dp_root_dir, FREE_DIR_NAME, tx);
783 	VERIFY(0 == dsl_pool_open_special_dir(dp,
784 	    FREE_DIR_NAME, &dp->dp_free_dir));
785 
786 	/*
787 	 * We can't use bpobj_alloc(), because spa_version() still
788 	 * returns the old version, and we need a new-version bpobj with
789 	 * subobj support.  So call dmu_object_alloc() directly.
790 	 */
791 	obj = dmu_object_alloc(dp->dp_meta_objset, DMU_OT_BPOBJ,
792 	    SPA_MAXBLOCKSIZE, DMU_OT_BPOBJ_HDR, sizeof (bpobj_phys_t), tx);
793 	VERIFY3U(0, ==, zap_add(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
794 	    DMU_POOL_FREE_BPOBJ, sizeof (uint64_t), 1, &obj, tx));
795 	VERIFY3U(0, ==, bpobj_open(&dp->dp_free_bpobj,
796 	    dp->dp_meta_objset, obj));
797 
798 	VERIFY3U(0, ==, dmu_objset_find_spa(dp->dp_spa, NULL,
799 	    upgrade_dir_clones_cb, tx, DS_FIND_CHILDREN));
800 }
801 
802 void
803 dsl_pool_create_origin(dsl_pool_t *dp, dmu_tx_t *tx)
804 {
805 	uint64_t dsobj;
806 	dsl_dataset_t *ds;
807 
808 	ASSERT(dmu_tx_is_syncing(tx));
809 	ASSERT(dp->dp_origin_snap == NULL);
810 
811 	/* create the origin dir, ds, & snap-ds */
812 	rw_enter(&dp->dp_config_rwlock, RW_WRITER);
813 	dsobj = dsl_dataset_create_sync(dp->dp_root_dir, ORIGIN_DIR_NAME,
814 	    NULL, 0, kcred, tx);
815 	VERIFY(0 == dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
816 	dsl_dataset_snapshot_sync(ds, ORIGIN_DIR_NAME, tx);
817 	VERIFY(0 == dsl_dataset_hold_obj(dp, ds->ds_phys->ds_prev_snap_obj,
818 	    dp, &dp->dp_origin_snap));
819 	dsl_dataset_rele(ds, FTAG);
820 	rw_exit(&dp->dp_config_rwlock);
821 }
822 
823 taskq_t *
824 dsl_pool_vnrele_taskq(dsl_pool_t *dp)
825 {
826 	return (dp->dp_vnrele_taskq);
827 }
828 
829 /*
830  * Walk through the pool-wide zap object of temporary snapshot user holds
831  * and release them.
832  */
833 void
834 dsl_pool_clean_tmp_userrefs(dsl_pool_t *dp)
835 {
836 	zap_attribute_t za;
837 	zap_cursor_t zc;
838 	objset_t *mos = dp->dp_meta_objset;
839 	uint64_t zapobj = dp->dp_tmp_userrefs_obj;
840 
841 	if (zapobj == 0)
842 		return;
843 	ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS);
844 
845 	for (zap_cursor_init(&zc, mos, zapobj);
846 	    zap_cursor_retrieve(&zc, &za) == 0;
847 	    zap_cursor_advance(&zc)) {
848 		char *htag;
849 		uint64_t dsobj;
850 
851 		htag = strchr(za.za_name, '-');
852 		*htag = '\0';
853 		++htag;
854 		dsobj = strtonum(za.za_name, NULL);
855 		(void) dsl_dataset_user_release_tmp(dp, dsobj, htag, B_FALSE);
856 	}
857 	zap_cursor_fini(&zc);
858 }
859 
860 /*
861  * Create the pool-wide zap object for storing temporary snapshot holds.
862  */
863 void
864 dsl_pool_user_hold_create_obj(dsl_pool_t *dp, dmu_tx_t *tx)
865 {
866 	objset_t *mos = dp->dp_meta_objset;
867 
868 	ASSERT(dp->dp_tmp_userrefs_obj == 0);
869 	ASSERT(dmu_tx_is_syncing(tx));
870 
871 	dp->dp_tmp_userrefs_obj = zap_create_link(mos, DMU_OT_USERREFS,
872 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_TMP_USERREFS, tx);
873 }
874 
875 static int
876 dsl_pool_user_hold_rele_impl(dsl_pool_t *dp, uint64_t dsobj,
877     const char *tag, uint64_t *now, dmu_tx_t *tx, boolean_t holding)
878 {
879 	objset_t *mos = dp->dp_meta_objset;
880 	uint64_t zapobj = dp->dp_tmp_userrefs_obj;
881 	char *name;
882 	int error;
883 
884 	ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS);
885 	ASSERT(dmu_tx_is_syncing(tx));
886 
887 	/*
888 	 * If the pool was created prior to SPA_VERSION_USERREFS, the
889 	 * zap object for temporary holds might not exist yet.
890 	 */
891 	if (zapobj == 0) {
892 		if (holding) {
893 			dsl_pool_user_hold_create_obj(dp, tx);
894 			zapobj = dp->dp_tmp_userrefs_obj;
895 		} else {
896 			return (ENOENT);
897 		}
898 	}
899 
900 	name = kmem_asprintf("%llx-%s", (u_longlong_t)dsobj, tag);
901 	if (holding)
902 		error = zap_add(mos, zapobj, name, 8, 1, now, tx);
903 	else
904 		error = zap_remove(mos, zapobj, name, tx);
905 	strfree(name);
906 
907 	return (error);
908 }
909 
910 /*
911  * Add a temporary hold for the given dataset object and tag.
912  */
913 int
914 dsl_pool_user_hold(dsl_pool_t *dp, uint64_t dsobj, const char *tag,
915     uint64_t *now, dmu_tx_t *tx)
916 {
917 	return (dsl_pool_user_hold_rele_impl(dp, dsobj, tag, now, tx, B_TRUE));
918 }
919 
920 /*
921  * Release a temporary hold for the given dataset object and tag.
922  */
923 int
924 dsl_pool_user_release(dsl_pool_t *dp, uint64_t dsobj, const char *tag,
925     dmu_tx_t *tx)
926 {
927 	return (dsl_pool_user_hold_rele_impl(dp, dsobj, tag, NULL,
928 	    tx, B_FALSE));
929 }
930