xref: /illumos-gate/usr/src/uts/common/fs/zfs/dsl_pool.c (revision 524b24f9)
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 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <sys/dsl_pool.h>
27 #include <sys/dsl_dataset.h>
28 #include <sys/dsl_dir.h>
29 #include <sys/dsl_synctask.h>
30 #include <sys/dmu_tx.h>
31 #include <sys/dmu_objset.h>
32 #include <sys/arc.h>
33 #include <sys/zap.h>
34 #include <sys/zio.h>
35 #include <sys/zfs_context.h>
36 #include <sys/fs/zfs.h>
37 #include <sys/zfs_znode.h>
38 #include <sys/spa_impl.h>
39 
40 int zfs_no_write_throttle = 0;
41 int zfs_write_limit_shift = 3;			/* 1/8th of physical memory */
42 int zfs_txg_synctime = 5;			/* target secs to sync a txg */
43 
44 uint64_t zfs_write_limit_min = 32 << 20;	/* min write limit is 32MB */
45 uint64_t zfs_write_limit_max = 0;		/* max data payload per txg */
46 uint64_t zfs_write_limit_inflated = 0;
47 uint64_t zfs_write_limit_override = 0;
48 
49 kmutex_t zfs_write_limit_lock;
50 
51 static pgcnt_t old_physmem = 0;
52 
53 static int
54 dsl_pool_open_special_dir(dsl_pool_t *dp, const char *name, dsl_dir_t **ddp)
55 {
56 	uint64_t obj;
57 	int err;
58 
59 	err = zap_lookup(dp->dp_meta_objset,
60 	    dp->dp_root_dir->dd_phys->dd_child_dir_zapobj,
61 	    name, sizeof (obj), 1, &obj);
62 	if (err)
63 		return (err);
64 
65 	return (dsl_dir_open_obj(dp, obj, name, dp, ddp));
66 }
67 
68 static dsl_pool_t *
69 dsl_pool_open_impl(spa_t *spa, uint64_t txg)
70 {
71 	dsl_pool_t *dp;
72 	blkptr_t *bp = spa_get_rootblkptr(spa);
73 
74 	dp = kmem_zalloc(sizeof (dsl_pool_t), KM_SLEEP);
75 	dp->dp_spa = spa;
76 	dp->dp_meta_rootbp = *bp;
77 	rw_init(&dp->dp_config_rwlock, NULL, RW_DEFAULT, NULL);
78 	dp->dp_write_limit = zfs_write_limit_min;
79 	txg_init(dp, txg);
80 
81 	txg_list_create(&dp->dp_dirty_datasets,
82 	    offsetof(dsl_dataset_t, ds_dirty_link));
83 	txg_list_create(&dp->dp_dirty_dirs,
84 	    offsetof(dsl_dir_t, dd_dirty_link));
85 	txg_list_create(&dp->dp_sync_tasks,
86 	    offsetof(dsl_sync_task_group_t, dstg_node));
87 	list_create(&dp->dp_synced_datasets, sizeof (dsl_dataset_t),
88 	    offsetof(dsl_dataset_t, ds_synced_link));
89 
90 	mutex_init(&dp->dp_lock, NULL, MUTEX_DEFAULT, NULL);
91 	mutex_init(&dp->dp_scrub_cancel_lock, NULL, MUTEX_DEFAULT, NULL);
92 
93 	return (dp);
94 }
95 
96 int
97 dsl_pool_open(spa_t *spa, uint64_t txg, dsl_pool_t **dpp)
98 {
99 	int err;
100 	dsl_pool_t *dp = dsl_pool_open_impl(spa, txg);
101 	dsl_dir_t *dd;
102 	dsl_dataset_t *ds;
103 	objset_impl_t *osi;
104 
105 	rw_enter(&dp->dp_config_rwlock, RW_WRITER);
106 	err = dmu_objset_open_impl(spa, NULL, &dp->dp_meta_rootbp, &osi);
107 	if (err)
108 		goto out;
109 	dp->dp_meta_objset = &osi->os;
110 
111 	err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
112 	    DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1,
113 	    &dp->dp_root_dir_obj);
114 	if (err)
115 		goto out;
116 
117 	err = dsl_dir_open_obj(dp, dp->dp_root_dir_obj,
118 	    NULL, dp, &dp->dp_root_dir);
119 	if (err)
120 		goto out;
121 
122 	err = dsl_pool_open_special_dir(dp, MOS_DIR_NAME, &dp->dp_mos_dir);
123 	if (err)
124 		goto out;
125 
126 	if (spa_version(spa) >= SPA_VERSION_ORIGIN) {
127 		err = dsl_pool_open_special_dir(dp, ORIGIN_DIR_NAME, &dd);
128 		if (err)
129 			goto out;
130 		err = dsl_dataset_hold_obj(dp, dd->dd_phys->dd_head_dataset_obj,
131 		    FTAG, &ds);
132 		if (err == 0) {
133 			err = dsl_dataset_hold_obj(dp,
134 			    ds->ds_phys->ds_prev_snap_obj, dp,
135 			    &dp->dp_origin_snap);
136 			dsl_dataset_rele(ds, FTAG);
137 		}
138 		dsl_dir_close(dd, dp);
139 		if (err)
140 			goto out;
141 	}
142 
143 	/* get scrub status */
144 	err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
145 	    DMU_POOL_SCRUB_FUNC, sizeof (uint32_t), 1,
146 	    &dp->dp_scrub_func);
147 	if (err == 0) {
148 		err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
149 		    DMU_POOL_SCRUB_QUEUE, sizeof (uint64_t), 1,
150 		    &dp->dp_scrub_queue_obj);
151 		if (err)
152 			goto out;
153 		err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
154 		    DMU_POOL_SCRUB_MIN_TXG, sizeof (uint64_t), 1,
155 		    &dp->dp_scrub_min_txg);
156 		if (err)
157 			goto out;
158 		err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
159 		    DMU_POOL_SCRUB_MAX_TXG, sizeof (uint64_t), 1,
160 		    &dp->dp_scrub_max_txg);
161 		if (err)
162 			goto out;
163 		err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
164 		    DMU_POOL_SCRUB_BOOKMARK, sizeof (uint64_t), 4,
165 		    &dp->dp_scrub_bookmark);
166 		if (err)
167 			goto out;
168 		err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
169 		    DMU_POOL_SCRUB_ERRORS, sizeof (uint64_t), 1,
170 		    &spa->spa_scrub_errors);
171 		if (err)
172 			goto out;
173 		if (spa_version(spa) < SPA_VERSION_DSL_SCRUB) {
174 			/*
175 			 * A new-type scrub was in progress on an old
176 			 * pool.  Restart from the beginning, since the
177 			 * old software may have changed the pool in the
178 			 * meantime.
179 			 */
180 			dsl_pool_scrub_restart(dp);
181 		}
182 	} else {
183 		/*
184 		 * It's OK if there is no scrub in progress (and if
185 		 * there was an I/O error, ignore it).
186 		 */
187 		err = 0;
188 	}
189 
190 out:
191 	rw_exit(&dp->dp_config_rwlock);
192 	if (err)
193 		dsl_pool_close(dp);
194 	else
195 		*dpp = dp;
196 
197 	return (err);
198 }
199 
200 void
201 dsl_pool_close(dsl_pool_t *dp)
202 {
203 	/* drop our references from dsl_pool_open() */
204 
205 	/*
206 	 * Since we held the origin_snap from "syncing" context (which
207 	 * includes pool-opening context), it actually only got a "ref"
208 	 * and not a hold, so just drop that here.
209 	 */
210 	if (dp->dp_origin_snap)
211 		dsl_dataset_drop_ref(dp->dp_origin_snap, dp);
212 	if (dp->dp_mos_dir)
213 		dsl_dir_close(dp->dp_mos_dir, dp);
214 	if (dp->dp_root_dir)
215 		dsl_dir_close(dp->dp_root_dir, dp);
216 
217 	/* undo the dmu_objset_open_impl(mos) from dsl_pool_open() */
218 	if (dp->dp_meta_objset)
219 		dmu_objset_evict(NULL, dp->dp_meta_objset->os);
220 
221 	txg_list_destroy(&dp->dp_dirty_datasets);
222 	txg_list_destroy(&dp->dp_dirty_dirs);
223 	list_destroy(&dp->dp_synced_datasets);
224 
225 	arc_flush(dp->dp_spa);
226 	txg_fini(dp);
227 	rw_destroy(&dp->dp_config_rwlock);
228 	mutex_destroy(&dp->dp_lock);
229 	mutex_destroy(&dp->dp_scrub_cancel_lock);
230 	if (dp->dp_blkstats)
231 		kmem_free(dp->dp_blkstats, sizeof (zfs_all_blkstats_t));
232 	kmem_free(dp, sizeof (dsl_pool_t));
233 }
234 
235 dsl_pool_t *
236 dsl_pool_create(spa_t *spa, nvlist_t *zplprops, uint64_t txg)
237 {
238 	int err;
239 	dsl_pool_t *dp = dsl_pool_open_impl(spa, txg);
240 	dmu_tx_t *tx = dmu_tx_create_assigned(dp, txg);
241 	objset_impl_t *osip;
242 	dsl_dataset_t *ds;
243 	uint64_t dsobj;
244 
245 	/* create and open the MOS (meta-objset) */
246 	dp->dp_meta_objset = &dmu_objset_create_impl(spa,
247 	    NULL, &dp->dp_meta_rootbp, DMU_OST_META, tx)->os;
248 
249 	/* create the pool directory */
250 	err = zap_create_claim(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
251 	    DMU_OT_OBJECT_DIRECTORY, DMU_OT_NONE, 0, tx);
252 	ASSERT3U(err, ==, 0);
253 
254 	/* create and open the root dir */
255 	dp->dp_root_dir_obj = dsl_dir_create_sync(dp, NULL, NULL, tx);
256 	VERIFY(0 == dsl_dir_open_obj(dp, dp->dp_root_dir_obj,
257 	    NULL, dp, &dp->dp_root_dir));
258 
259 	/* create and open the meta-objset dir */
260 	(void) dsl_dir_create_sync(dp, dp->dp_root_dir, MOS_DIR_NAME, tx);
261 	VERIFY(0 == dsl_pool_open_special_dir(dp,
262 	    MOS_DIR_NAME, &dp->dp_mos_dir));
263 
264 	if (spa_version(spa) >= SPA_VERSION_DSL_SCRUB)
265 		dsl_pool_create_origin(dp, tx);
266 
267 	/* create the root dataset */
268 	dsobj = dsl_dataset_create_sync_dd(dp->dp_root_dir, NULL, 0, tx);
269 
270 	/* create the root objset */
271 	VERIFY(0 == dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
272 	osip = dmu_objset_create_impl(dp->dp_spa, ds,
273 	    dsl_dataset_get_blkptr(ds), DMU_OST_ZFS, tx);
274 #ifdef _KERNEL
275 	zfs_create_fs(&osip->os, kcred, zplprops, tx);
276 #endif
277 	dsl_dataset_rele(ds, FTAG);
278 
279 	dmu_tx_commit(tx);
280 
281 	return (dp);
282 }
283 
284 void
285 dsl_pool_sync(dsl_pool_t *dp, uint64_t txg)
286 {
287 	zio_t *zio;
288 	dmu_tx_t *tx;
289 	dsl_dir_t *dd;
290 	dsl_dataset_t *ds;
291 	dsl_sync_task_group_t *dstg;
292 	objset_impl_t *mosi = dp->dp_meta_objset->os;
293 	hrtime_t start, write_time;
294 	uint64_t data_written;
295 	int err;
296 
297 	tx = dmu_tx_create_assigned(dp, txg);
298 
299 	dp->dp_read_overhead = 0;
300 	zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
301 	while (ds = txg_list_remove(&dp->dp_dirty_datasets, txg)) {
302 		if (!list_link_active(&ds->ds_synced_link))
303 			list_insert_tail(&dp->dp_synced_datasets, ds);
304 		else
305 			dmu_buf_rele(ds->ds_dbuf, ds);
306 		dsl_dataset_sync(ds, zio, tx);
307 	}
308 	DTRACE_PROBE(pool_sync__1setup);
309 
310 	start = gethrtime();
311 	err = zio_wait(zio);
312 	write_time = gethrtime() - start;
313 	ASSERT(err == 0);
314 	DTRACE_PROBE(pool_sync__2rootzio);
315 
316 	while (dstg = txg_list_remove(&dp->dp_sync_tasks, txg))
317 		dsl_sync_task_group_sync(dstg, tx);
318 	DTRACE_PROBE(pool_sync__3task);
319 
320 	start = gethrtime();
321 	while (dd = txg_list_remove(&dp->dp_dirty_dirs, txg))
322 		dsl_dir_sync(dd, tx);
323 	write_time += gethrtime() - start;
324 
325 	if (spa_sync_pass(dp->dp_spa) == 1)
326 		dsl_pool_scrub_sync(dp, tx);
327 
328 	start = gethrtime();
329 	if (list_head(&mosi->os_dirty_dnodes[txg & TXG_MASK]) != NULL ||
330 	    list_head(&mosi->os_free_dnodes[txg & TXG_MASK]) != NULL) {
331 		zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
332 		dmu_objset_sync(mosi, zio, tx);
333 		err = zio_wait(zio);
334 		ASSERT(err == 0);
335 		dprintf_bp(&dp->dp_meta_rootbp, "meta objset rootbp is %s", "");
336 		spa_set_rootblkptr(dp->dp_spa, &dp->dp_meta_rootbp);
337 	}
338 	write_time += gethrtime() - start;
339 	DTRACE_PROBE2(pool_sync__4io, hrtime_t, write_time,
340 	    hrtime_t, dp->dp_read_overhead);
341 	write_time -= dp->dp_read_overhead;
342 
343 	dmu_tx_commit(tx);
344 
345 	data_written = dp->dp_space_towrite[txg & TXG_MASK];
346 	dp->dp_space_towrite[txg & TXG_MASK] = 0;
347 	ASSERT(dp->dp_tempreserved[txg & TXG_MASK] == 0);
348 
349 	/*
350 	 * If the write limit max has not been explicitly set, set it
351 	 * to a fraction of available physical memory (default 1/8th).
352 	 * Note that we must inflate the limit because the spa
353 	 * inflates write sizes to account for data replication.
354 	 * Check this each sync phase to catch changing memory size.
355 	 */
356 	if (physmem != old_physmem && zfs_write_limit_shift) {
357 		mutex_enter(&zfs_write_limit_lock);
358 		old_physmem = physmem;
359 		zfs_write_limit_max = ptob(physmem) >> zfs_write_limit_shift;
360 		zfs_write_limit_inflated = MAX(zfs_write_limit_min,
361 		    spa_get_asize(dp->dp_spa, zfs_write_limit_max));
362 		mutex_exit(&zfs_write_limit_lock);
363 	}
364 
365 	/*
366 	 * Attempt to keep the sync time consistent by adjusting the
367 	 * amount of write traffic allowed into each transaction group.
368 	 * Weight the throughput calculation towards the current value:
369 	 * 	thru = 3/4 old_thru + 1/4 new_thru
370 	 */
371 	ASSERT(zfs_write_limit_min > 0);
372 	if (data_written > zfs_write_limit_min / 8 && write_time > 0) {
373 		uint64_t throughput = (data_written * NANOSEC) / write_time;
374 		if (dp->dp_throughput)
375 			dp->dp_throughput = throughput / 4 +
376 			    3 * dp->dp_throughput / 4;
377 		else
378 			dp->dp_throughput = throughput;
379 		dp->dp_write_limit = MIN(zfs_write_limit_inflated,
380 		    MAX(zfs_write_limit_min,
381 		    dp->dp_throughput * zfs_txg_synctime));
382 	}
383 }
384 
385 void
386 dsl_pool_zil_clean(dsl_pool_t *dp)
387 {
388 	dsl_dataset_t *ds;
389 
390 	while (ds = list_head(&dp->dp_synced_datasets)) {
391 		list_remove(&dp->dp_synced_datasets, ds);
392 		ASSERT(ds->ds_user_ptr != NULL);
393 		zil_clean(((objset_impl_t *)ds->ds_user_ptr)->os_zil);
394 		dmu_buf_rele(ds->ds_dbuf, ds);
395 	}
396 }
397 
398 /*
399  * TRUE if the current thread is the tx_sync_thread or if we
400  * are being called from SPA context during pool initialization.
401  */
402 int
403 dsl_pool_sync_context(dsl_pool_t *dp)
404 {
405 	return (curthread == dp->dp_tx.tx_sync_thread ||
406 	    spa_get_dsl(dp->dp_spa) == NULL);
407 }
408 
409 uint64_t
410 dsl_pool_adjustedsize(dsl_pool_t *dp, boolean_t netfree)
411 {
412 	uint64_t space, resv;
413 
414 	/*
415 	 * Reserve about 1.6% (1/64), or at least 32MB, for allocation
416 	 * efficiency.
417 	 * XXX The intent log is not accounted for, so it must fit
418 	 * within this slop.
419 	 *
420 	 * If we're trying to assess whether it's OK to do a free,
421 	 * cut the reservation in half to allow forward progress
422 	 * (e.g. make it possible to rm(1) files from a full pool).
423 	 */
424 	space = spa_get_dspace(dp->dp_spa);
425 	resv = MAX(space >> 6, SPA_MINDEVSIZE >> 1);
426 	if (netfree)
427 		resv >>= 1;
428 
429 	return (space - resv);
430 }
431 
432 int
433 dsl_pool_tempreserve_space(dsl_pool_t *dp, uint64_t space, dmu_tx_t *tx)
434 {
435 	uint64_t reserved = 0;
436 	uint64_t write_limit = (zfs_write_limit_override ?
437 	    zfs_write_limit_override : dp->dp_write_limit);
438 
439 	if (zfs_no_write_throttle) {
440 		atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK],
441 		    space);
442 		return (0);
443 	}
444 
445 	/*
446 	 * Check to see if we have exceeded the maximum allowed IO for
447 	 * this transaction group.  We can do this without locks since
448 	 * a little slop here is ok.  Note that we do the reserved check
449 	 * with only half the requested reserve: this is because the
450 	 * reserve requests are worst-case, and we really don't want to
451 	 * throttle based off of worst-case estimates.
452 	 */
453 	if (write_limit > 0) {
454 		reserved = dp->dp_space_towrite[tx->tx_txg & TXG_MASK]
455 		    + dp->dp_tempreserved[tx->tx_txg & TXG_MASK] / 2;
456 
457 		if (reserved && reserved > write_limit)
458 			return (ERESTART);
459 	}
460 
461 	atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK], space);
462 
463 	/*
464 	 * If this transaction group is over 7/8ths capacity, delay
465 	 * the caller 1 clock tick.  This will slow down the "fill"
466 	 * rate until the sync process can catch up with us.
467 	 */
468 	if (reserved && reserved > (write_limit - (write_limit >> 3)))
469 		txg_delay(dp, tx->tx_txg, 1);
470 
471 	return (0);
472 }
473 
474 void
475 dsl_pool_tempreserve_clear(dsl_pool_t *dp, int64_t space, dmu_tx_t *tx)
476 {
477 	ASSERT(dp->dp_tempreserved[tx->tx_txg & TXG_MASK] >= space);
478 	atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK], -space);
479 }
480 
481 void
482 dsl_pool_memory_pressure(dsl_pool_t *dp)
483 {
484 	uint64_t space_inuse = 0;
485 	int i;
486 
487 	if (dp->dp_write_limit == zfs_write_limit_min)
488 		return;
489 
490 	for (i = 0; i < TXG_SIZE; i++) {
491 		space_inuse += dp->dp_space_towrite[i];
492 		space_inuse += dp->dp_tempreserved[i];
493 	}
494 	dp->dp_write_limit = MAX(zfs_write_limit_min,
495 	    MIN(dp->dp_write_limit, space_inuse / 4));
496 }
497 
498 void
499 dsl_pool_willuse_space(dsl_pool_t *dp, int64_t space, dmu_tx_t *tx)
500 {
501 	if (space > 0) {
502 		mutex_enter(&dp->dp_lock);
503 		dp->dp_space_towrite[tx->tx_txg & TXG_MASK] += space;
504 		mutex_exit(&dp->dp_lock);
505 	}
506 }
507 
508 /* ARGSUSED */
509 static int
510 upgrade_clones_cb(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg)
511 {
512 	dmu_tx_t *tx = arg;
513 	dsl_dataset_t *ds, *prev = NULL;
514 	int err;
515 	dsl_pool_t *dp = spa_get_dsl(spa);
516 
517 	err = dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds);
518 	if (err)
519 		return (err);
520 
521 	while (ds->ds_phys->ds_prev_snap_obj != 0) {
522 		err = dsl_dataset_hold_obj(dp, ds->ds_phys->ds_prev_snap_obj,
523 		    FTAG, &prev);
524 		if (err) {
525 			dsl_dataset_rele(ds, FTAG);
526 			return (err);
527 		}
528 
529 		if (prev->ds_phys->ds_next_snap_obj != ds->ds_object)
530 			break;
531 		dsl_dataset_rele(ds, FTAG);
532 		ds = prev;
533 		prev = NULL;
534 	}
535 
536 	if (prev == NULL) {
537 		prev = dp->dp_origin_snap;
538 
539 		/*
540 		 * The $ORIGIN can't have any data, or the accounting
541 		 * will be wrong.
542 		 */
543 		ASSERT(prev->ds_phys->ds_bp.blk_birth == 0);
544 
545 		/* The origin doesn't get attached to itself */
546 		if (ds->ds_object == prev->ds_object) {
547 			dsl_dataset_rele(ds, FTAG);
548 			return (0);
549 		}
550 
551 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
552 		ds->ds_phys->ds_prev_snap_obj = prev->ds_object;
553 		ds->ds_phys->ds_prev_snap_txg = prev->ds_phys->ds_creation_txg;
554 
555 		dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
556 		ds->ds_dir->dd_phys->dd_origin_obj = prev->ds_object;
557 
558 		dmu_buf_will_dirty(prev->ds_dbuf, tx);
559 		prev->ds_phys->ds_num_children++;
560 
561 		if (ds->ds_phys->ds_next_snap_obj == 0) {
562 			ASSERT(ds->ds_prev == NULL);
563 			VERIFY(0 == dsl_dataset_hold_obj(dp,
564 			    ds->ds_phys->ds_prev_snap_obj, ds, &ds->ds_prev));
565 		}
566 	}
567 
568 	ASSERT(ds->ds_dir->dd_phys->dd_origin_obj == prev->ds_object);
569 	ASSERT(ds->ds_phys->ds_prev_snap_obj == prev->ds_object);
570 
571 	if (prev->ds_phys->ds_next_clones_obj == 0) {
572 		prev->ds_phys->ds_next_clones_obj =
573 		    zap_create(dp->dp_meta_objset,
574 		    DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx);
575 	}
576 	VERIFY(0 == zap_add_int(dp->dp_meta_objset,
577 	    prev->ds_phys->ds_next_clones_obj, ds->ds_object, tx));
578 
579 	dsl_dataset_rele(ds, FTAG);
580 	if (prev != dp->dp_origin_snap)
581 		dsl_dataset_rele(prev, FTAG);
582 	return (0);
583 }
584 
585 void
586 dsl_pool_upgrade_clones(dsl_pool_t *dp, dmu_tx_t *tx)
587 {
588 	ASSERT(dmu_tx_is_syncing(tx));
589 	ASSERT(dp->dp_origin_snap != NULL);
590 
591 	(void) dmu_objset_find_spa(dp->dp_spa, NULL, upgrade_clones_cb,
592 	    tx, DS_FIND_CHILDREN);
593 }
594 
595 void
596 dsl_pool_create_origin(dsl_pool_t *dp, dmu_tx_t *tx)
597 {
598 	uint64_t dsobj;
599 	dsl_dataset_t *ds;
600 
601 	ASSERT(dmu_tx_is_syncing(tx));
602 	ASSERT(dp->dp_origin_snap == NULL);
603 
604 	/* create the origin dir, ds, & snap-ds */
605 	rw_enter(&dp->dp_config_rwlock, RW_WRITER);
606 	dsobj = dsl_dataset_create_sync(dp->dp_root_dir, ORIGIN_DIR_NAME,
607 	    NULL, 0, kcred, tx);
608 	VERIFY(0 == dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
609 	dsl_dataset_snapshot_sync(ds, ORIGIN_DIR_NAME, kcred, tx);
610 	VERIFY(0 == dsl_dataset_hold_obj(dp, ds->ds_phys->ds_prev_snap_obj,
611 	    dp, &dp->dp_origin_snap));
612 	dsl_dataset_rele(ds, FTAG);
613 	rw_exit(&dp->dp_config_rwlock);
614 }
615