xref: /titanic_51/usr/src/uts/common/fs/zfs/dsl_pool.c (revision d4ac42a1cd3016618a9ba0330862d410f0058f89)
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  */
24 
25 #include <sys/dsl_pool.h>
26 #include <sys/dsl_dataset.h>
27 #include <sys/dsl_prop.h>
28 #include <sys/dsl_dir.h>
29 #include <sys/dsl_synctask.h>
30 #include <sys/dsl_scan.h>
31 #include <sys/dnode.h>
32 #include <sys/dmu_tx.h>
33 #include <sys/dmu_objset.h>
34 #include <sys/arc.h>
35 #include <sys/zap.h>
36 #include <sys/zio.h>
37 #include <sys/zfs_context.h>
38 #include <sys/fs/zfs.h>
39 #include <sys/zfs_znode.h>
40 #include <sys/spa_impl.h>
41 
42 int zfs_no_write_throttle = 0;
43 int zfs_write_limit_shift = 3;			/* 1/8th of physical memory */
44 int zfs_txg_synctime_ms = 5000;		/* target millisecs to sync a txg */
45 
46 uint64_t zfs_write_limit_min = 32 << 20;	/* min write limit is 32MB */
47 uint64_t zfs_write_limit_max = 0;		/* max data payload per txg */
48 uint64_t zfs_write_limit_inflated = 0;
49 uint64_t zfs_write_limit_override = 0;
50 
51 kmutex_t zfs_write_limit_lock;
52 
53 static pgcnt_t old_physmem = 0;
54 
55 int
56 dsl_pool_open_special_dir(dsl_pool_t *dp, const char *name, dsl_dir_t **ddp)
57 {
58 	uint64_t obj;
59 	int err;
60 
61 	err = zap_lookup(dp->dp_meta_objset,
62 	    dp->dp_root_dir->dd_phys->dd_child_dir_zapobj,
63 	    name, sizeof (obj), 1, &obj);
64 	if (err)
65 		return (err);
66 
67 	return (dsl_dir_open_obj(dp, obj, name, dp, ddp));
68 }
69 
70 static dsl_pool_t *
71 dsl_pool_open_impl(spa_t *spa, uint64_t txg)
72 {
73 	dsl_pool_t *dp;
74 	blkptr_t *bp = spa_get_rootblkptr(spa);
75 
76 	dp = kmem_zalloc(sizeof (dsl_pool_t), KM_SLEEP);
77 	dp->dp_spa = spa;
78 	dp->dp_meta_rootbp = *bp;
79 	rw_init(&dp->dp_config_rwlock, NULL, RW_DEFAULT, NULL);
80 	dp->dp_write_limit = zfs_write_limit_min;
81 	txg_init(dp, txg);
82 
83 	txg_list_create(&dp->dp_dirty_datasets,
84 	    offsetof(dsl_dataset_t, ds_dirty_link));
85 	txg_list_create(&dp->dp_dirty_dirs,
86 	    offsetof(dsl_dir_t, dd_dirty_link));
87 	txg_list_create(&dp->dp_sync_tasks,
88 	    offsetof(dsl_sync_task_group_t, dstg_node));
89 	list_create(&dp->dp_synced_datasets, sizeof (dsl_dataset_t),
90 	    offsetof(dsl_dataset_t, ds_synced_link));
91 
92 	mutex_init(&dp->dp_lock, NULL, MUTEX_DEFAULT, NULL);
93 
94 	dp->dp_vnrele_taskq = taskq_create("zfs_vn_rele_taskq", 1, minclsyspri,
95 	    1, 4, 0);
96 
97 	return (dp);
98 }
99 
100 int
101 dsl_pool_open(spa_t *spa, uint64_t txg, dsl_pool_t **dpp)
102 {
103 	int err;
104 	dsl_pool_t *dp = dsl_pool_open_impl(spa, txg);
105 	dsl_dir_t *dd;
106 	dsl_dataset_t *ds;
107 
108 	rw_enter(&dp->dp_config_rwlock, RW_WRITER);
109 	err = dmu_objset_open_impl(spa, NULL, &dp->dp_meta_rootbp,
110 	    &dp->dp_meta_objset);
111 	if (err)
112 		goto out;
113 
114 	err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
115 	    DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1,
116 	    &dp->dp_root_dir_obj);
117 	if (err)
118 		goto out;
119 
120 	err = dsl_dir_open_obj(dp, dp->dp_root_dir_obj,
121 	    NULL, dp, &dp->dp_root_dir);
122 	if (err)
123 		goto out;
124 
125 	err = dsl_pool_open_special_dir(dp, MOS_DIR_NAME, &dp->dp_mos_dir);
126 	if (err)
127 		goto out;
128 
129 	if (spa_version(spa) >= SPA_VERSION_ORIGIN) {
130 		err = dsl_pool_open_special_dir(dp, ORIGIN_DIR_NAME, &dd);
131 		if (err)
132 			goto out;
133 		err = dsl_dataset_hold_obj(dp, dd->dd_phys->dd_head_dataset_obj,
134 		    FTAG, &ds);
135 		if (err == 0) {
136 			err = dsl_dataset_hold_obj(dp,
137 			    ds->ds_phys->ds_prev_snap_obj, dp,
138 			    &dp->dp_origin_snap);
139 			dsl_dataset_rele(ds, FTAG);
140 		}
141 		dsl_dir_close(dd, dp);
142 		if (err)
143 			goto out;
144 	}
145 
146 	err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
147 	    DMU_POOL_TMP_USERREFS, sizeof (uint64_t), 1,
148 	    &dp->dp_tmp_userrefs_obj);
149 	if (err == ENOENT)
150 		err = 0;
151 	if (err)
152 		goto out;
153 
154 	err = dsl_scan_init(dp, txg);
155 
156 out:
157 	rw_exit(&dp->dp_config_rwlock);
158 	if (err)
159 		dsl_pool_close(dp);
160 	else
161 		*dpp = dp;
162 
163 	return (err);
164 }
165 
166 void
167 dsl_pool_close(dsl_pool_t *dp)
168 {
169 	/* drop our references from dsl_pool_open() */
170 
171 	/*
172 	 * Since we held the origin_snap from "syncing" context (which
173 	 * includes pool-opening context), it actually only got a "ref"
174 	 * and not a hold, so just drop that here.
175 	 */
176 	if (dp->dp_origin_snap)
177 		dsl_dataset_drop_ref(dp->dp_origin_snap, dp);
178 	if (dp->dp_mos_dir)
179 		dsl_dir_close(dp->dp_mos_dir, dp);
180 	if (dp->dp_root_dir)
181 		dsl_dir_close(dp->dp_root_dir, dp);
182 
183 	/* undo the dmu_objset_open_impl(mos) from dsl_pool_open() */
184 	if (dp->dp_meta_objset)
185 		dmu_objset_evict(dp->dp_meta_objset);
186 
187 	txg_list_destroy(&dp->dp_dirty_datasets);
188 	txg_list_destroy(&dp->dp_sync_tasks);
189 	txg_list_destroy(&dp->dp_dirty_dirs);
190 	list_destroy(&dp->dp_synced_datasets);
191 
192 	arc_flush(dp->dp_spa);
193 	txg_fini(dp);
194 	dsl_scan_fini(dp);
195 	rw_destroy(&dp->dp_config_rwlock);
196 	mutex_destroy(&dp->dp_lock);
197 	taskq_destroy(dp->dp_vnrele_taskq);
198 	if (dp->dp_blkstats)
199 		kmem_free(dp->dp_blkstats, sizeof (zfs_all_blkstats_t));
200 	kmem_free(dp, sizeof (dsl_pool_t));
201 }
202 
203 dsl_pool_t *
204 dsl_pool_create(spa_t *spa, nvlist_t *zplprops, uint64_t txg)
205 {
206 	int err;
207 	dsl_pool_t *dp = dsl_pool_open_impl(spa, txg);
208 	dmu_tx_t *tx = dmu_tx_create_assigned(dp, txg);
209 	objset_t *os;
210 	dsl_dataset_t *ds;
211 	uint64_t dsobj;
212 
213 	/* create and open the MOS (meta-objset) */
214 	dp->dp_meta_objset = dmu_objset_create_impl(spa,
215 	    NULL, &dp->dp_meta_rootbp, DMU_OST_META, tx);
216 
217 	/* create the pool directory */
218 	err = zap_create_claim(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
219 	    DMU_OT_OBJECT_DIRECTORY, DMU_OT_NONE, 0, tx);
220 	ASSERT3U(err, ==, 0);
221 
222 	/* Initialize scan structures */
223 	VERIFY3U(0, ==, dsl_scan_init(dp, txg));
224 
225 	/* create and open the root dir */
226 	dp->dp_root_dir_obj = dsl_dir_create_sync(dp, NULL, NULL, tx);
227 	VERIFY(0 == dsl_dir_open_obj(dp, dp->dp_root_dir_obj,
228 	    NULL, dp, &dp->dp_root_dir));
229 
230 	/* create and open the meta-objset dir */
231 	(void) dsl_dir_create_sync(dp, dp->dp_root_dir, MOS_DIR_NAME, tx);
232 	VERIFY(0 == dsl_pool_open_special_dir(dp,
233 	    MOS_DIR_NAME, &dp->dp_mos_dir));
234 
235 	if (spa_version(spa) >= SPA_VERSION_DSL_SCRUB)
236 		dsl_pool_create_origin(dp, tx);
237 
238 	/* create the root dataset */
239 	dsobj = dsl_dataset_create_sync_dd(dp->dp_root_dir, NULL, 0, tx);
240 
241 	/* create the root objset */
242 	VERIFY(0 == dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
243 	os = dmu_objset_create_impl(dp->dp_spa, ds,
244 	    dsl_dataset_get_blkptr(ds), DMU_OST_ZFS, tx);
245 #ifdef _KERNEL
246 	zfs_create_fs(os, kcred, zplprops, tx);
247 #endif
248 	dsl_dataset_rele(ds, FTAG);
249 
250 	dmu_tx_commit(tx);
251 
252 	return (dp);
253 }
254 
255 void
256 dsl_pool_sync(dsl_pool_t *dp, uint64_t txg)
257 {
258 	zio_t *zio;
259 	dmu_tx_t *tx;
260 	dsl_dir_t *dd;
261 	dsl_dataset_t *ds;
262 	dsl_sync_task_group_t *dstg;
263 	objset_t *mos = dp->dp_meta_objset;
264 	hrtime_t start, write_time;
265 	uint64_t data_written;
266 	int err;
267 
268 	/*
269 	 * We need to copy dp_space_towrite() before doing
270 	 * dsl_sync_task_group_sync(), because
271 	 * dsl_dataset_snapshot_reserve_space() will increase
272 	 * dp_space_towrite but not actually write anything.
273 	 */
274 	data_written = dp->dp_space_towrite[txg & TXG_MASK];
275 
276 	tx = dmu_tx_create_assigned(dp, txg);
277 
278 	dp->dp_read_overhead = 0;
279 	start = gethrtime();
280 
281 	zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
282 	while (ds = txg_list_remove(&dp->dp_dirty_datasets, txg)) {
283 		/*
284 		 * We must not sync any non-MOS datasets twice, because
285 		 * we may have taken a snapshot of them.  However, we
286 		 * may sync newly-created datasets on pass 2.
287 		 */
288 		ASSERT(!list_link_active(&ds->ds_synced_link));
289 		list_insert_tail(&dp->dp_synced_datasets, ds);
290 		dsl_dataset_sync(ds, zio, tx);
291 	}
292 	DTRACE_PROBE(pool_sync__1setup);
293 	err = zio_wait(zio);
294 
295 	write_time = gethrtime() - start;
296 	ASSERT(err == 0);
297 	DTRACE_PROBE(pool_sync__2rootzio);
298 
299 	for (ds = list_head(&dp->dp_synced_datasets); ds;
300 	    ds = list_next(&dp->dp_synced_datasets, ds))
301 		dmu_objset_do_userquota_updates(ds->ds_objset, tx);
302 
303 	/*
304 	 * Sync the datasets again to push out the changes due to
305 	 * userspace updates.  This must be done before we process the
306 	 * sync tasks, because that could cause a snapshot of a dataset
307 	 * whose ds_bp will be rewritten when we do this 2nd sync.
308 	 */
309 	zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
310 	while (ds = txg_list_remove(&dp->dp_dirty_datasets, txg)) {
311 		ASSERT(list_link_active(&ds->ds_synced_link));
312 		dmu_buf_rele(ds->ds_dbuf, ds);
313 		dsl_dataset_sync(ds, zio, tx);
314 	}
315 	err = zio_wait(zio);
316 
317 	/*
318 	 * If anything was added to a deadlist during a zio done callback,
319 	 * it had to be put on the deferred queue.  Enqueue it for real now.
320 	 */
321 	for (ds = list_head(&dp->dp_synced_datasets); ds;
322 	    ds = list_next(&dp->dp_synced_datasets, ds))
323 		bplist_sync(&ds->ds_deadlist,
324 		    bplist_enqueue_cb, &ds->ds_deadlist, tx);
325 
326 	while (dstg = txg_list_remove(&dp->dp_sync_tasks, txg)) {
327 		/*
328 		 * No more sync tasks should have been added while we
329 		 * were syncing.
330 		 */
331 		ASSERT(spa_sync_pass(dp->dp_spa) == 1);
332 		dsl_sync_task_group_sync(dstg, tx);
333 	}
334 	DTRACE_PROBE(pool_sync__3task);
335 
336 	start = gethrtime();
337 	while (dd = txg_list_remove(&dp->dp_dirty_dirs, txg))
338 		dsl_dir_sync(dd, tx);
339 	write_time += gethrtime() - start;
340 
341 	start = gethrtime();
342 	if (list_head(&mos->os_dirty_dnodes[txg & TXG_MASK]) != NULL ||
343 	    list_head(&mos->os_free_dnodes[txg & TXG_MASK]) != NULL) {
344 		zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
345 		dmu_objset_sync(mos, zio, tx);
346 		err = zio_wait(zio);
347 		ASSERT(err == 0);
348 		dprintf_bp(&dp->dp_meta_rootbp, "meta objset rootbp is %s", "");
349 		spa_set_rootblkptr(dp->dp_spa, &dp->dp_meta_rootbp);
350 	}
351 	write_time += gethrtime() - start;
352 	DTRACE_PROBE2(pool_sync__4io, hrtime_t, write_time,
353 	    hrtime_t, dp->dp_read_overhead);
354 	write_time -= dp->dp_read_overhead;
355 
356 	dmu_tx_commit(tx);
357 
358 	dp->dp_space_towrite[txg & TXG_MASK] = 0;
359 	ASSERT(dp->dp_tempreserved[txg & TXG_MASK] == 0);
360 
361 	/*
362 	 * If the write limit max has not been explicitly set, set it
363 	 * to a fraction of available physical memory (default 1/8th).
364 	 * Note that we must inflate the limit because the spa
365 	 * inflates write sizes to account for data replication.
366 	 * Check this each sync phase to catch changing memory size.
367 	 */
368 	if (physmem != old_physmem && zfs_write_limit_shift) {
369 		mutex_enter(&zfs_write_limit_lock);
370 		old_physmem = physmem;
371 		zfs_write_limit_max = ptob(physmem) >> zfs_write_limit_shift;
372 		zfs_write_limit_inflated = MAX(zfs_write_limit_min,
373 		    spa_get_asize(dp->dp_spa, zfs_write_limit_max));
374 		mutex_exit(&zfs_write_limit_lock);
375 	}
376 
377 	/*
378 	 * Attempt to keep the sync time consistent by adjusting the
379 	 * amount of write traffic allowed into each transaction group.
380 	 * Weight the throughput calculation towards the current value:
381 	 * 	thru = 3/4 old_thru + 1/4 new_thru
382 	 *
383 	 * Note: write_time is in nanosecs, so write_time/MICROSEC
384 	 * yields millisecs
385 	 */
386 	ASSERT(zfs_write_limit_min > 0);
387 	if (data_written > zfs_write_limit_min / 8 && write_time > MICROSEC) {
388 		uint64_t throughput = data_written / (write_time / MICROSEC);
389 
390 		if (dp->dp_throughput)
391 			dp->dp_throughput = throughput / 4 +
392 			    3 * dp->dp_throughput / 4;
393 		else
394 			dp->dp_throughput = throughput;
395 		dp->dp_write_limit = MIN(zfs_write_limit_inflated,
396 		    MAX(zfs_write_limit_min,
397 		    dp->dp_throughput * zfs_txg_synctime_ms));
398 	}
399 }
400 
401 void
402 dsl_pool_sync_done(dsl_pool_t *dp, uint64_t txg)
403 {
404 	dsl_dataset_t *ds;
405 	objset_t *os;
406 
407 	while (ds = list_head(&dp->dp_synced_datasets)) {
408 		list_remove(&dp->dp_synced_datasets, ds);
409 		os = ds->ds_objset;
410 		zil_clean(os->os_zil);
411 		ASSERT(!dmu_objset_is_dirty(os, txg));
412 		dmu_buf_rele(ds->ds_dbuf, ds);
413 	}
414 	ASSERT(!dmu_objset_is_dirty(dp->dp_meta_objset, txg));
415 }
416 
417 /*
418  * TRUE if the current thread is the tx_sync_thread or if we
419  * are being called from SPA context during pool initialization.
420  */
421 int
422 dsl_pool_sync_context(dsl_pool_t *dp)
423 {
424 	return (curthread == dp->dp_tx.tx_sync_thread ||
425 	    spa_get_dsl(dp->dp_spa) == NULL);
426 }
427 
428 uint64_t
429 dsl_pool_adjustedsize(dsl_pool_t *dp, boolean_t netfree)
430 {
431 	uint64_t space, resv;
432 
433 	/*
434 	 * Reserve about 1.6% (1/64), or at least 32MB, for allocation
435 	 * efficiency.
436 	 * XXX The intent log is not accounted for, so it must fit
437 	 * within this slop.
438 	 *
439 	 * If we're trying to assess whether it's OK to do a free,
440 	 * cut the reservation in half to allow forward progress
441 	 * (e.g. make it possible to rm(1) files from a full pool).
442 	 */
443 	space = spa_get_dspace(dp->dp_spa);
444 	resv = MAX(space >> 6, SPA_MINDEVSIZE >> 1);
445 	if (netfree)
446 		resv >>= 1;
447 
448 	return (space - resv);
449 }
450 
451 int
452 dsl_pool_tempreserve_space(dsl_pool_t *dp, uint64_t space, dmu_tx_t *tx)
453 {
454 	uint64_t reserved = 0;
455 	uint64_t write_limit = (zfs_write_limit_override ?
456 	    zfs_write_limit_override : dp->dp_write_limit);
457 
458 	if (zfs_no_write_throttle) {
459 		atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK],
460 		    space);
461 		return (0);
462 	}
463 
464 	/*
465 	 * Check to see if we have exceeded the maximum allowed IO for
466 	 * this transaction group.  We can do this without locks since
467 	 * a little slop here is ok.  Note that we do the reserved check
468 	 * with only half the requested reserve: this is because the
469 	 * reserve requests are worst-case, and we really don't want to
470 	 * throttle based off of worst-case estimates.
471 	 */
472 	if (write_limit > 0) {
473 		reserved = dp->dp_space_towrite[tx->tx_txg & TXG_MASK]
474 		    + dp->dp_tempreserved[tx->tx_txg & TXG_MASK] / 2;
475 
476 		if (reserved && reserved > write_limit)
477 			return (ERESTART);
478 	}
479 
480 	atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK], space);
481 
482 	/*
483 	 * If this transaction group is over 7/8ths capacity, delay
484 	 * the caller 1 clock tick.  This will slow down the "fill"
485 	 * rate until the sync process can catch up with us.
486 	 */
487 	if (reserved && reserved > (write_limit - (write_limit >> 3)))
488 		txg_delay(dp, tx->tx_txg, 1);
489 
490 	return (0);
491 }
492 
493 void
494 dsl_pool_tempreserve_clear(dsl_pool_t *dp, int64_t space, dmu_tx_t *tx)
495 {
496 	ASSERT(dp->dp_tempreserved[tx->tx_txg & TXG_MASK] >= space);
497 	atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK], -space);
498 }
499 
500 void
501 dsl_pool_memory_pressure(dsl_pool_t *dp)
502 {
503 	uint64_t space_inuse = 0;
504 	int i;
505 
506 	if (dp->dp_write_limit == zfs_write_limit_min)
507 		return;
508 
509 	for (i = 0; i < TXG_SIZE; i++) {
510 		space_inuse += dp->dp_space_towrite[i];
511 		space_inuse += dp->dp_tempreserved[i];
512 	}
513 	dp->dp_write_limit = MAX(zfs_write_limit_min,
514 	    MIN(dp->dp_write_limit, space_inuse / 4));
515 }
516 
517 void
518 dsl_pool_willuse_space(dsl_pool_t *dp, int64_t space, dmu_tx_t *tx)
519 {
520 	if (space > 0) {
521 		mutex_enter(&dp->dp_lock);
522 		dp->dp_space_towrite[tx->tx_txg & TXG_MASK] += space;
523 		mutex_exit(&dp->dp_lock);
524 	}
525 }
526 
527 /* ARGSUSED */
528 static int
529 upgrade_clones_cb(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg)
530 {
531 	dmu_tx_t *tx = arg;
532 	dsl_dataset_t *ds, *prev = NULL;
533 	int err;
534 	dsl_pool_t *dp = spa_get_dsl(spa);
535 
536 	err = dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds);
537 	if (err)
538 		return (err);
539 
540 	while (ds->ds_phys->ds_prev_snap_obj != 0) {
541 		err = dsl_dataset_hold_obj(dp, ds->ds_phys->ds_prev_snap_obj,
542 		    FTAG, &prev);
543 		if (err) {
544 			dsl_dataset_rele(ds, FTAG);
545 			return (err);
546 		}
547 
548 		if (prev->ds_phys->ds_next_snap_obj != ds->ds_object)
549 			break;
550 		dsl_dataset_rele(ds, FTAG);
551 		ds = prev;
552 		prev = NULL;
553 	}
554 
555 	if (prev == NULL) {
556 		prev = dp->dp_origin_snap;
557 
558 		/*
559 		 * The $ORIGIN can't have any data, or the accounting
560 		 * will be wrong.
561 		 */
562 		ASSERT(prev->ds_phys->ds_bp.blk_birth == 0);
563 
564 		/* The origin doesn't get attached to itself */
565 		if (ds->ds_object == prev->ds_object) {
566 			dsl_dataset_rele(ds, FTAG);
567 			return (0);
568 		}
569 
570 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
571 		ds->ds_phys->ds_prev_snap_obj = prev->ds_object;
572 		ds->ds_phys->ds_prev_snap_txg = prev->ds_phys->ds_creation_txg;
573 
574 		dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
575 		ds->ds_dir->dd_phys->dd_origin_obj = prev->ds_object;
576 
577 		dmu_buf_will_dirty(prev->ds_dbuf, tx);
578 		prev->ds_phys->ds_num_children++;
579 
580 		if (ds->ds_phys->ds_next_snap_obj == 0) {
581 			ASSERT(ds->ds_prev == NULL);
582 			VERIFY(0 == dsl_dataset_hold_obj(dp,
583 			    ds->ds_phys->ds_prev_snap_obj, ds, &ds->ds_prev));
584 		}
585 	}
586 
587 	ASSERT(ds->ds_dir->dd_phys->dd_origin_obj == prev->ds_object);
588 	ASSERT(ds->ds_phys->ds_prev_snap_obj == prev->ds_object);
589 
590 	if (prev->ds_phys->ds_next_clones_obj == 0) {
591 		dmu_buf_will_dirty(prev->ds_dbuf, tx);
592 		prev->ds_phys->ds_next_clones_obj =
593 		    zap_create(dp->dp_meta_objset,
594 		    DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx);
595 	}
596 	VERIFY(0 == zap_add_int(dp->dp_meta_objset,
597 	    prev->ds_phys->ds_next_clones_obj, ds->ds_object, tx));
598 
599 	dsl_dataset_rele(ds, FTAG);
600 	if (prev != dp->dp_origin_snap)
601 		dsl_dataset_rele(prev, FTAG);
602 	return (0);
603 }
604 
605 void
606 dsl_pool_upgrade_clones(dsl_pool_t *dp, dmu_tx_t *tx)
607 {
608 	ASSERT(dmu_tx_is_syncing(tx));
609 	ASSERT(dp->dp_origin_snap != NULL);
610 
611 	VERIFY3U(0, ==, dmu_objset_find_spa(dp->dp_spa, NULL, upgrade_clones_cb,
612 	    tx, DS_FIND_CHILDREN));
613 }
614 
615 void
616 dsl_pool_create_origin(dsl_pool_t *dp, dmu_tx_t *tx)
617 {
618 	uint64_t dsobj;
619 	dsl_dataset_t *ds;
620 
621 	ASSERT(dmu_tx_is_syncing(tx));
622 	ASSERT(dp->dp_origin_snap == NULL);
623 
624 	/* create the origin dir, ds, & snap-ds */
625 	rw_enter(&dp->dp_config_rwlock, RW_WRITER);
626 	dsobj = dsl_dataset_create_sync(dp->dp_root_dir, ORIGIN_DIR_NAME,
627 	    NULL, 0, kcred, tx);
628 	VERIFY(0 == dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
629 	dsl_dataset_snapshot_sync(ds, ORIGIN_DIR_NAME, tx);
630 	VERIFY(0 == dsl_dataset_hold_obj(dp, ds->ds_phys->ds_prev_snap_obj,
631 	    dp, &dp->dp_origin_snap));
632 	dsl_dataset_rele(ds, FTAG);
633 	rw_exit(&dp->dp_config_rwlock);
634 }
635 
636 taskq_t *
637 dsl_pool_vnrele_taskq(dsl_pool_t *dp)
638 {
639 	return (dp->dp_vnrele_taskq);
640 }
641 
642 /*
643  * Walk through the pool-wide zap object of temporary snapshot user holds
644  * and release them.
645  */
646 void
647 dsl_pool_clean_tmp_userrefs(dsl_pool_t *dp)
648 {
649 	zap_attribute_t za;
650 	zap_cursor_t zc;
651 	objset_t *mos = dp->dp_meta_objset;
652 	uint64_t zapobj = dp->dp_tmp_userrefs_obj;
653 
654 	if (zapobj == 0)
655 		return;
656 	ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS);
657 
658 	for (zap_cursor_init(&zc, mos, zapobj);
659 	    zap_cursor_retrieve(&zc, &za) == 0;
660 	    zap_cursor_advance(&zc)) {
661 		char *htag;
662 		uint64_t dsobj;
663 
664 		htag = strchr(za.za_name, '-');
665 		*htag = '\0';
666 		++htag;
667 		dsobj = strtonum(za.za_name, NULL);
668 		(void) dsl_dataset_user_release_tmp(dp, dsobj, htag);
669 	}
670 	zap_cursor_fini(&zc);
671 }
672 
673 /*
674  * Create the pool-wide zap object for storing temporary snapshot holds.
675  */
676 void
677 dsl_pool_user_hold_create_obj(dsl_pool_t *dp, dmu_tx_t *tx)
678 {
679 	objset_t *mos = dp->dp_meta_objset;
680 
681 	ASSERT(dp->dp_tmp_userrefs_obj == 0);
682 	ASSERT(dmu_tx_is_syncing(tx));
683 
684 	dp->dp_tmp_userrefs_obj = zap_create(mos, DMU_OT_USERREFS,
685 	    DMU_OT_NONE, 0, tx);
686 
687 	VERIFY(zap_add(mos, DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_TMP_USERREFS,
688 	    sizeof (uint64_t), 1, &dp->dp_tmp_userrefs_obj, tx) == 0);
689 }
690 
691 static int
692 dsl_pool_user_hold_rele_impl(dsl_pool_t *dp, uint64_t dsobj,
693     const char *tag, uint64_t *now, dmu_tx_t *tx, boolean_t holding)
694 {
695 	objset_t *mos = dp->dp_meta_objset;
696 	uint64_t zapobj = dp->dp_tmp_userrefs_obj;
697 	char *name;
698 	int error;
699 
700 	ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS);
701 	ASSERT(dmu_tx_is_syncing(tx));
702 
703 	/*
704 	 * If the pool was created prior to SPA_VERSION_USERREFS, the
705 	 * zap object for temporary holds might not exist yet.
706 	 */
707 	if (zapobj == 0) {
708 		if (holding) {
709 			dsl_pool_user_hold_create_obj(dp, tx);
710 			zapobj = dp->dp_tmp_userrefs_obj;
711 		} else {
712 			return (ENOENT);
713 		}
714 	}
715 
716 	name = kmem_asprintf("%llx-%s", (u_longlong_t)dsobj, tag);
717 	if (holding)
718 		error = zap_add(mos, zapobj, name, 8, 1, now, tx);
719 	else
720 		error = zap_remove(mos, zapobj, name, tx);
721 	strfree(name);
722 
723 	return (error);
724 }
725 
726 /*
727  * Add a temporary hold for the given dataset object and tag.
728  */
729 int
730 dsl_pool_user_hold(dsl_pool_t *dp, uint64_t dsobj, const char *tag,
731     uint64_t *now, dmu_tx_t *tx)
732 {
733 	return (dsl_pool_user_hold_rele_impl(dp, dsobj, tag, now, tx, B_TRUE));
734 }
735 
736 /*
737  * Release a temporary hold for the given dataset object and tag.
738  */
739 int
740 dsl_pool_user_release(dsl_pool_t *dp, uint64_t dsobj, const char *tag,
741     dmu_tx_t *tx)
742 {
743 	return (dsl_pool_user_hold_rele_impl(dp, dsobj, tag, NULL,
744 	    tx, B_FALSE));
745 }
746