xref: /illumos-gate/usr/src/uts/common/fs/zfs/dmu_objset.c (revision 08f1bbed5edd2a2e9c8be7b7424c32e67c2f3f2c)
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 /*
23  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright (c) 2012, 2017 by Delphix. All rights reserved.
25  * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
26  * Copyright (c) 2013, Joyent, Inc. All rights reserved.
27  * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
28  * Copyright (c) 2015, STRATO AG, Inc. All rights reserved.
29  * Copyright (c) 2014 Integros [integros.com]
30  * Copyright 2017 Nexenta Systems, Inc.
31  */
32 
33 /* Portions Copyright 2010 Robert Milkowski */
34 
35 #include <sys/cred.h>
36 #include <sys/zfs_context.h>
37 #include <sys/dmu_objset.h>
38 #include <sys/dsl_dir.h>
39 #include <sys/dsl_dataset.h>
40 #include <sys/dsl_prop.h>
41 #include <sys/dsl_pool.h>
42 #include <sys/dsl_synctask.h>
43 #include <sys/dsl_deleg.h>
44 #include <sys/dnode.h>
45 #include <sys/dbuf.h>
46 #include <sys/zvol.h>
47 #include <sys/dmu_tx.h>
48 #include <sys/zap.h>
49 #include <sys/zil.h>
50 #include <sys/dmu_impl.h>
51 #include <sys/zfs_ioctl.h>
52 #include <sys/sa.h>
53 #include <sys/zfs_onexit.h>
54 #include <sys/dsl_destroy.h>
55 #include <sys/vdev.h>
56 #include <sys/zfeature.h>
57 #include "zfs_namecheck.h"
58 
59 /*
60  * Needed to close a window in dnode_move() that allows the objset to be freed
61  * before it can be safely accessed.
62  */
63 krwlock_t os_lock;
64 
65 /*
66  * Tunable to overwrite the maximum number of threads for the parallization
67  * of dmu_objset_find_dp, needed to speed up the import of pools with many
68  * datasets.
69  * Default is 4 times the number of leaf vdevs.
70  */
71 int dmu_find_threads = 0;
72 
73 /*
74  * Backfill lower metadnode objects after this many have been freed.
75  * Backfilling negatively impacts object creation rates, so only do it
76  * if there are enough holes to fill.
77  */
78 int dmu_rescan_dnode_threshold = 131072;
79 
80 static void dmu_objset_find_dp_cb(void *arg);
81 
82 void
83 dmu_objset_init(void)
84 {
85 	rw_init(&os_lock, NULL, RW_DEFAULT, NULL);
86 }
87 
88 void
89 dmu_objset_fini(void)
90 {
91 	rw_destroy(&os_lock);
92 }
93 
94 spa_t *
95 dmu_objset_spa(objset_t *os)
96 {
97 	return (os->os_spa);
98 }
99 
100 zilog_t *
101 dmu_objset_zil(objset_t *os)
102 {
103 	return (os->os_zil);
104 }
105 
106 dsl_pool_t *
107 dmu_objset_pool(objset_t *os)
108 {
109 	dsl_dataset_t *ds;
110 
111 	if ((ds = os->os_dsl_dataset) != NULL && ds->ds_dir)
112 		return (ds->ds_dir->dd_pool);
113 	else
114 		return (spa_get_dsl(os->os_spa));
115 }
116 
117 dsl_dataset_t *
118 dmu_objset_ds(objset_t *os)
119 {
120 	return (os->os_dsl_dataset);
121 }
122 
123 dmu_objset_type_t
124 dmu_objset_type(objset_t *os)
125 {
126 	return (os->os_phys->os_type);
127 }
128 
129 void
130 dmu_objset_name(objset_t *os, char *buf)
131 {
132 	dsl_dataset_name(os->os_dsl_dataset, buf);
133 }
134 
135 uint64_t
136 dmu_objset_id(objset_t *os)
137 {
138 	dsl_dataset_t *ds = os->os_dsl_dataset;
139 
140 	return (ds ? ds->ds_object : 0);
141 }
142 
143 uint64_t
144 dmu_objset_dnodesize(objset_t *os)
145 {
146 	return (os->os_dnodesize);
147 }
148 
149 zfs_sync_type_t
150 dmu_objset_syncprop(objset_t *os)
151 {
152 	return (os->os_sync);
153 }
154 
155 zfs_logbias_op_t
156 dmu_objset_logbias(objset_t *os)
157 {
158 	return (os->os_logbias);
159 }
160 
161 static void
162 checksum_changed_cb(void *arg, uint64_t newval)
163 {
164 	objset_t *os = arg;
165 
166 	/*
167 	 * Inheritance should have been done by now.
168 	 */
169 	ASSERT(newval != ZIO_CHECKSUM_INHERIT);
170 
171 	os->os_checksum = zio_checksum_select(newval, ZIO_CHECKSUM_ON_VALUE);
172 }
173 
174 static void
175 compression_changed_cb(void *arg, uint64_t newval)
176 {
177 	objset_t *os = arg;
178 
179 	/*
180 	 * Inheritance and range checking should have been done by now.
181 	 */
182 	ASSERT(newval != ZIO_COMPRESS_INHERIT);
183 
184 	os->os_compress = zio_compress_select(os->os_spa, newval,
185 	    ZIO_COMPRESS_ON);
186 }
187 
188 static void
189 copies_changed_cb(void *arg, uint64_t newval)
190 {
191 	objset_t *os = arg;
192 
193 	/*
194 	 * Inheritance and range checking should have been done by now.
195 	 */
196 	ASSERT(newval > 0);
197 	ASSERT(newval <= spa_max_replication(os->os_spa));
198 
199 	os->os_copies = newval;
200 }
201 
202 static void
203 dedup_changed_cb(void *arg, uint64_t newval)
204 {
205 	objset_t *os = arg;
206 	spa_t *spa = os->os_spa;
207 	enum zio_checksum checksum;
208 
209 	/*
210 	 * Inheritance should have been done by now.
211 	 */
212 	ASSERT(newval != ZIO_CHECKSUM_INHERIT);
213 
214 	checksum = zio_checksum_dedup_select(spa, newval, ZIO_CHECKSUM_OFF);
215 
216 	os->os_dedup_checksum = checksum & ZIO_CHECKSUM_MASK;
217 	os->os_dedup_verify = !!(checksum & ZIO_CHECKSUM_VERIFY);
218 }
219 
220 static void
221 primary_cache_changed_cb(void *arg, uint64_t newval)
222 {
223 	objset_t *os = arg;
224 
225 	/*
226 	 * Inheritance and range checking should have been done by now.
227 	 */
228 	ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
229 	    newval == ZFS_CACHE_METADATA);
230 
231 	os->os_primary_cache = newval;
232 }
233 
234 static void
235 secondary_cache_changed_cb(void *arg, uint64_t newval)
236 {
237 	objset_t *os = arg;
238 
239 	/*
240 	 * Inheritance and range checking should have been done by now.
241 	 */
242 	ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
243 	    newval == ZFS_CACHE_METADATA);
244 
245 	os->os_secondary_cache = newval;
246 }
247 
248 static void
249 sync_changed_cb(void *arg, uint64_t newval)
250 {
251 	objset_t *os = arg;
252 
253 	/*
254 	 * Inheritance and range checking should have been done by now.
255 	 */
256 	ASSERT(newval == ZFS_SYNC_STANDARD || newval == ZFS_SYNC_ALWAYS ||
257 	    newval == ZFS_SYNC_DISABLED);
258 
259 	os->os_sync = newval;
260 	if (os->os_zil)
261 		zil_set_sync(os->os_zil, newval);
262 }
263 
264 static void
265 redundant_metadata_changed_cb(void *arg, uint64_t newval)
266 {
267 	objset_t *os = arg;
268 
269 	/*
270 	 * Inheritance and range checking should have been done by now.
271 	 */
272 	ASSERT(newval == ZFS_REDUNDANT_METADATA_ALL ||
273 	    newval == ZFS_REDUNDANT_METADATA_MOST);
274 
275 	os->os_redundant_metadata = newval;
276 }
277 
278 static void
279 dnodesize_changed_cb(void *arg, uint64_t newval)
280 {
281 	objset_t *os = arg;
282 
283 	switch (newval) {
284 	case ZFS_DNSIZE_LEGACY:
285 		os->os_dnodesize = DNODE_MIN_SIZE;
286 		break;
287 	case ZFS_DNSIZE_AUTO:
288 		/*
289 		 * Choose a dnode size that will work well for most
290 		 * workloads if the user specified "auto". Future code
291 		 * improvements could dynamically select a dnode size
292 		 * based on observed workload patterns.
293 		 */
294 		os->os_dnodesize = DNODE_MIN_SIZE * 2;
295 		break;
296 	case ZFS_DNSIZE_1K:
297 	case ZFS_DNSIZE_2K:
298 	case ZFS_DNSIZE_4K:
299 	case ZFS_DNSIZE_8K:
300 	case ZFS_DNSIZE_16K:
301 		os->os_dnodesize = newval;
302 		break;
303 	}
304 }
305 
306 static void
307 logbias_changed_cb(void *arg, uint64_t newval)
308 {
309 	objset_t *os = arg;
310 
311 	ASSERT(newval == ZFS_LOGBIAS_LATENCY ||
312 	    newval == ZFS_LOGBIAS_THROUGHPUT);
313 	os->os_logbias = newval;
314 	if (os->os_zil)
315 		zil_set_logbias(os->os_zil, newval);
316 }
317 
318 static void
319 recordsize_changed_cb(void *arg, uint64_t newval)
320 {
321 	objset_t *os = arg;
322 
323 	os->os_recordsize = newval;
324 }
325 
326 void
327 dmu_objset_byteswap(void *buf, size_t size)
328 {
329 	objset_phys_t *osp = buf;
330 
331 	ASSERT(size == OBJSET_OLD_PHYS_SIZE || size == sizeof (objset_phys_t));
332 	dnode_byteswap(&osp->os_meta_dnode);
333 	byteswap_uint64_array(&osp->os_zil_header, sizeof (zil_header_t));
334 	osp->os_type = BSWAP_64(osp->os_type);
335 	osp->os_flags = BSWAP_64(osp->os_flags);
336 	if (size == sizeof (objset_phys_t)) {
337 		dnode_byteswap(&osp->os_userused_dnode);
338 		dnode_byteswap(&osp->os_groupused_dnode);
339 	}
340 }
341 
342 /*
343  * The hash is a CRC-based hash of the objset_t pointer and the object number.
344  */
345 static uint64_t
346 dnode_hash(const objset_t *os, uint64_t obj)
347 {
348 	uintptr_t osv = (uintptr_t)os;
349 	uint64_t crc = -1ULL;
350 
351 	ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY);
352 	/*
353 	 * The low 6 bits of the pointer don't have much entropy, because
354 	 * the objset_t is larger than 2^6 bytes long.
355 	 */
356 	crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (osv >> 6)) & 0xFF];
357 	crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 0)) & 0xFF];
358 	crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 8)) & 0xFF];
359 	crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 16)) & 0xFF];
360 
361 	crc ^= (osv>>14) ^ (obj>>24);
362 
363 	return (crc);
364 }
365 
366 unsigned int
367 dnode_multilist_index_func(multilist_t *ml, void *obj)
368 {
369 	dnode_t *dn = obj;
370 	return (dnode_hash(dn->dn_objset, dn->dn_object) %
371 	    multilist_get_num_sublists(ml));
372 }
373 
374 /*
375  * Instantiates the objset_t in-memory structure corresponding to the
376  * objset_phys_t that's pointed to by the specified blkptr_t.
377  */
378 int
379 dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
380     objset_t **osp)
381 {
382 	objset_t *os;
383 	int i, err;
384 
385 	ASSERT(ds == NULL || MUTEX_HELD(&ds->ds_opening_lock));
386 
387 	/*
388 	 * The $ORIGIN dataset (if it exists) doesn't have an associated
389 	 * objset, so there's no reason to open it. The $ORIGIN dataset
390 	 * will not exist on pools older than SPA_VERSION_ORIGIN.
391 	 */
392 	if (ds != NULL && spa_get_dsl(spa) != NULL &&
393 	    spa_get_dsl(spa)->dp_origin_snap != NULL) {
394 		ASSERT3P(ds->ds_dir, !=,
395 		    spa_get_dsl(spa)->dp_origin_snap->ds_dir);
396 	}
397 
398 	os = kmem_zalloc(sizeof (objset_t), KM_SLEEP);
399 	os->os_dsl_dataset = ds;
400 	os->os_spa = spa;
401 	os->os_rootbp = bp;
402 	if (!BP_IS_HOLE(os->os_rootbp)) {
403 		arc_flags_t aflags = ARC_FLAG_WAIT;
404 		zbookmark_phys_t zb;
405 		SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET,
406 		    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
407 
408 		if (DMU_OS_IS_L2CACHEABLE(os))
409 			aflags |= ARC_FLAG_L2CACHE;
410 
411 		dprintf_bp(os->os_rootbp, "reading %s", "");
412 		err = arc_read(NULL, spa, os->os_rootbp,
413 		    arc_getbuf_func, &os->os_phys_buf,
414 		    ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &aflags, &zb);
415 		if (err != 0) {
416 			kmem_free(os, sizeof (objset_t));
417 			/* convert checksum errors into IO errors */
418 			if (err == ECKSUM)
419 				err = SET_ERROR(EIO);
420 			return (err);
421 		}
422 
423 		/* Increase the blocksize if we are permitted. */
424 		if (spa_version(spa) >= SPA_VERSION_USERSPACE &&
425 		    arc_buf_size(os->os_phys_buf) < sizeof (objset_phys_t)) {
426 			arc_buf_t *buf = arc_alloc_buf(spa, &os->os_phys_buf,
427 			    ARC_BUFC_METADATA, sizeof (objset_phys_t));
428 			bzero(buf->b_data, sizeof (objset_phys_t));
429 			bcopy(os->os_phys_buf->b_data, buf->b_data,
430 			    arc_buf_size(os->os_phys_buf));
431 			arc_buf_destroy(os->os_phys_buf, &os->os_phys_buf);
432 			os->os_phys_buf = buf;
433 		}
434 
435 		os->os_phys = os->os_phys_buf->b_data;
436 		os->os_flags = os->os_phys->os_flags;
437 	} else {
438 		int size = spa_version(spa) >= SPA_VERSION_USERSPACE ?
439 		    sizeof (objset_phys_t) : OBJSET_OLD_PHYS_SIZE;
440 		os->os_phys_buf = arc_alloc_buf(spa, &os->os_phys_buf,
441 		    ARC_BUFC_METADATA, size);
442 		os->os_phys = os->os_phys_buf->b_data;
443 		bzero(os->os_phys, size);
444 	}
445 
446 	/*
447 	 * Note: the changed_cb will be called once before the register
448 	 * func returns, thus changing the checksum/compression from the
449 	 * default (fletcher2/off).  Snapshots don't need to know about
450 	 * checksum/compression/copies.
451 	 */
452 	if (ds != NULL) {
453 		boolean_t needlock = B_FALSE;
454 
455 		/*
456 		 * Note: it's valid to open the objset if the dataset is
457 		 * long-held, in which case the pool_config lock will not
458 		 * be held.
459 		 */
460 		if (!dsl_pool_config_held(dmu_objset_pool(os))) {
461 			needlock = B_TRUE;
462 			dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
463 		}
464 		err = dsl_prop_register(ds,
465 		    zfs_prop_to_name(ZFS_PROP_PRIMARYCACHE),
466 		    primary_cache_changed_cb, os);
467 		if (err == 0) {
468 			err = dsl_prop_register(ds,
469 			    zfs_prop_to_name(ZFS_PROP_SECONDARYCACHE),
470 			    secondary_cache_changed_cb, os);
471 		}
472 		if (!ds->ds_is_snapshot) {
473 			if (err == 0) {
474 				err = dsl_prop_register(ds,
475 				    zfs_prop_to_name(ZFS_PROP_CHECKSUM),
476 				    checksum_changed_cb, os);
477 			}
478 			if (err == 0) {
479 				err = dsl_prop_register(ds,
480 				    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
481 				    compression_changed_cb, os);
482 			}
483 			if (err == 0) {
484 				err = dsl_prop_register(ds,
485 				    zfs_prop_to_name(ZFS_PROP_COPIES),
486 				    copies_changed_cb, os);
487 			}
488 			if (err == 0) {
489 				err = dsl_prop_register(ds,
490 				    zfs_prop_to_name(ZFS_PROP_DEDUP),
491 				    dedup_changed_cb, os);
492 			}
493 			if (err == 0) {
494 				err = dsl_prop_register(ds,
495 				    zfs_prop_to_name(ZFS_PROP_LOGBIAS),
496 				    logbias_changed_cb, os);
497 			}
498 			if (err == 0) {
499 				err = dsl_prop_register(ds,
500 				    zfs_prop_to_name(ZFS_PROP_SYNC),
501 				    sync_changed_cb, os);
502 			}
503 			if (err == 0) {
504 				err = dsl_prop_register(ds,
505 				    zfs_prop_to_name(
506 				    ZFS_PROP_REDUNDANT_METADATA),
507 				    redundant_metadata_changed_cb, os);
508 			}
509 			if (err == 0) {
510 				err = dsl_prop_register(ds,
511 				    zfs_prop_to_name(ZFS_PROP_RECORDSIZE),
512 				    recordsize_changed_cb, os);
513 			}
514 			if (err == 0) {
515 				err = dsl_prop_register(ds,
516 				    zfs_prop_to_name(ZFS_PROP_DNODESIZE),
517 				    dnodesize_changed_cb, os);
518 			}
519 		}
520 		if (needlock)
521 			dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
522 		if (err != 0) {
523 			arc_buf_destroy(os->os_phys_buf, &os->os_phys_buf);
524 			kmem_free(os, sizeof (objset_t));
525 			return (err);
526 		}
527 	} else {
528 		/* It's the meta-objset. */
529 		os->os_checksum = ZIO_CHECKSUM_FLETCHER_4;
530 		os->os_compress = ZIO_COMPRESS_ON;
531 		os->os_copies = spa_max_replication(spa);
532 		os->os_dedup_checksum = ZIO_CHECKSUM_OFF;
533 		os->os_dedup_verify = B_FALSE;
534 		os->os_logbias = ZFS_LOGBIAS_LATENCY;
535 		os->os_sync = ZFS_SYNC_STANDARD;
536 		os->os_primary_cache = ZFS_CACHE_ALL;
537 		os->os_secondary_cache = ZFS_CACHE_ALL;
538 		os->os_dnodesize = DNODE_MIN_SIZE;
539 	}
540 	/*
541 	 * These properties will be filled in by the logic in zfs_get_zplprop()
542 	 * when they are queried for the first time.
543 	 */
544 	os->os_version = OBJSET_PROP_UNINITIALIZED;
545 	os->os_normalization = OBJSET_PROP_UNINITIALIZED;
546 	os->os_utf8only = OBJSET_PROP_UNINITIALIZED;
547 	os->os_casesensitivity = OBJSET_PROP_UNINITIALIZED;
548 
549 	if (ds == NULL || !ds->ds_is_snapshot)
550 		os->os_zil_header = os->os_phys->os_zil_header;
551 	os->os_zil = zil_alloc(os, &os->os_zil_header);
552 
553 	for (i = 0; i < TXG_SIZE; i++) {
554 		os->os_dirty_dnodes[i] = multilist_create(sizeof (dnode_t),
555 		    offsetof(dnode_t, dn_dirty_link[i]),
556 		    dnode_multilist_index_func);
557 	}
558 	list_create(&os->os_dnodes, sizeof (dnode_t),
559 	    offsetof(dnode_t, dn_link));
560 	list_create(&os->os_downgraded_dbufs, sizeof (dmu_buf_impl_t),
561 	    offsetof(dmu_buf_impl_t, db_link));
562 
563 	mutex_init(&os->os_lock, NULL, MUTEX_DEFAULT, NULL);
564 	mutex_init(&os->os_userused_lock, NULL, MUTEX_DEFAULT, NULL);
565 	mutex_init(&os->os_obj_lock, NULL, MUTEX_DEFAULT, NULL);
566 	mutex_init(&os->os_user_ptr_lock, NULL, MUTEX_DEFAULT, NULL);
567 	os->os_obj_next_percpu_len = boot_ncpus;
568 	os->os_obj_next_percpu = kmem_zalloc(os->os_obj_next_percpu_len *
569 	    sizeof (os->os_obj_next_percpu[0]), KM_SLEEP);
570 
571 	dnode_special_open(os, &os->os_phys->os_meta_dnode,
572 	    DMU_META_DNODE_OBJECT, &os->os_meta_dnode);
573 	if (arc_buf_size(os->os_phys_buf) >= sizeof (objset_phys_t)) {
574 		dnode_special_open(os, &os->os_phys->os_userused_dnode,
575 		    DMU_USERUSED_OBJECT, &os->os_userused_dnode);
576 		dnode_special_open(os, &os->os_phys->os_groupused_dnode,
577 		    DMU_GROUPUSED_OBJECT, &os->os_groupused_dnode);
578 	}
579 
580 	*osp = os;
581 	return (0);
582 }
583 
584 int
585 dmu_objset_from_ds(dsl_dataset_t *ds, objset_t **osp)
586 {
587 	int err = 0;
588 
589 	/*
590 	 * We shouldn't be doing anything with dsl_dataset_t's unless the
591 	 * pool_config lock is held, or the dataset is long-held.
592 	 */
593 	ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool) ||
594 	    dsl_dataset_long_held(ds));
595 
596 	mutex_enter(&ds->ds_opening_lock);
597 	if (ds->ds_objset == NULL) {
598 		objset_t *os;
599 		rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
600 		err = dmu_objset_open_impl(dsl_dataset_get_spa(ds),
601 		    ds, dsl_dataset_get_blkptr(ds), &os);
602 		rrw_exit(&ds->ds_bp_rwlock, FTAG);
603 
604 		if (err == 0) {
605 			mutex_enter(&ds->ds_lock);
606 			ASSERT(ds->ds_objset == NULL);
607 			ds->ds_objset = os;
608 			mutex_exit(&ds->ds_lock);
609 		}
610 	}
611 	*osp = ds->ds_objset;
612 	mutex_exit(&ds->ds_opening_lock);
613 	return (err);
614 }
615 
616 /*
617  * Holds the pool while the objset is held.  Therefore only one objset
618  * can be held at a time.
619  */
620 int
621 dmu_objset_hold(const char *name, void *tag, objset_t **osp)
622 {
623 	dsl_pool_t *dp;
624 	dsl_dataset_t *ds;
625 	int err;
626 
627 	err = dsl_pool_hold(name, tag, &dp);
628 	if (err != 0)
629 		return (err);
630 	err = dsl_dataset_hold(dp, name, tag, &ds);
631 	if (err != 0) {
632 		dsl_pool_rele(dp, tag);
633 		return (err);
634 	}
635 
636 	err = dmu_objset_from_ds(ds, osp);
637 	if (err != 0) {
638 		dsl_dataset_rele(ds, tag);
639 		dsl_pool_rele(dp, tag);
640 	}
641 
642 	return (err);
643 }
644 
645 static int
646 dmu_objset_own_impl(dsl_dataset_t *ds, dmu_objset_type_t type,
647     boolean_t readonly, void *tag, objset_t **osp)
648 {
649 	int err;
650 
651 	err = dmu_objset_from_ds(ds, osp);
652 	if (err != 0) {
653 		dsl_dataset_disown(ds, tag);
654 	} else if (type != DMU_OST_ANY && type != (*osp)->os_phys->os_type) {
655 		dsl_dataset_disown(ds, tag);
656 		return (SET_ERROR(EINVAL));
657 	} else if (!readonly && dsl_dataset_is_snapshot(ds)) {
658 		dsl_dataset_disown(ds, tag);
659 		return (SET_ERROR(EROFS));
660 	}
661 	return (err);
662 }
663 
664 /*
665  * dsl_pool must not be held when this is called.
666  * Upon successful return, there will be a longhold on the dataset,
667  * and the dsl_pool will not be held.
668  */
669 int
670 dmu_objset_own(const char *name, dmu_objset_type_t type,
671     boolean_t readonly, void *tag, objset_t **osp)
672 {
673 	dsl_pool_t *dp;
674 	dsl_dataset_t *ds;
675 	int err;
676 
677 	err = dsl_pool_hold(name, FTAG, &dp);
678 	if (err != 0)
679 		return (err);
680 	err = dsl_dataset_own(dp, name, tag, &ds);
681 	if (err != 0) {
682 		dsl_pool_rele(dp, FTAG);
683 		return (err);
684 	}
685 	err = dmu_objset_own_impl(ds, type, readonly, tag, osp);
686 	dsl_pool_rele(dp, FTAG);
687 
688 	return (err);
689 }
690 
691 int
692 dmu_objset_own_obj(dsl_pool_t *dp, uint64_t obj, dmu_objset_type_t type,
693     boolean_t readonly, void *tag, objset_t **osp)
694 {
695 	dsl_dataset_t *ds;
696 	int err;
697 
698 	err = dsl_dataset_own_obj(dp, obj, tag, &ds);
699 	if (err != 0)
700 		return (err);
701 
702 	return (dmu_objset_own_impl(ds, type, readonly, tag, osp));
703 }
704 
705 void
706 dmu_objset_rele(objset_t *os, void *tag)
707 {
708 	dsl_pool_t *dp = dmu_objset_pool(os);
709 	dsl_dataset_rele(os->os_dsl_dataset, tag);
710 	dsl_pool_rele(dp, tag);
711 }
712 
713 /*
714  * When we are called, os MUST refer to an objset associated with a dataset
715  * that is owned by 'tag'; that is, is held and long held by 'tag' and ds_owner
716  * == tag.  We will then release and reacquire ownership of the dataset while
717  * holding the pool config_rwlock to avoid intervening namespace or ownership
718  * changes may occur.
719  *
720  * This exists solely to accommodate zfs_ioc_userspace_upgrade()'s desire to
721  * release the hold on its dataset and acquire a new one on the dataset of the
722  * same name so that it can be partially torn down and reconstructed.
723  */
724 void
725 dmu_objset_refresh_ownership(dsl_dataset_t *ds, dsl_dataset_t **newds,
726     void *tag)
727 {
728 	dsl_pool_t *dp;
729 	char name[ZFS_MAX_DATASET_NAME_LEN];
730 
731 	VERIFY3P(ds, !=, NULL);
732 	VERIFY3P(ds->ds_owner, ==, tag);
733 	VERIFY(dsl_dataset_long_held(ds));
734 
735 	dsl_dataset_name(ds, name);
736 	dp = ds->ds_dir->dd_pool;
737 	dsl_pool_config_enter(dp, FTAG);
738 	dsl_dataset_disown(ds, tag);
739 	VERIFY0(dsl_dataset_own(dp, name, tag, newds));
740 	dsl_pool_config_exit(dp, FTAG);
741 }
742 
743 void
744 dmu_objset_disown(objset_t *os, void *tag)
745 {
746 	dsl_dataset_disown(os->os_dsl_dataset, tag);
747 }
748 
749 void
750 dmu_objset_evict_dbufs(objset_t *os)
751 {
752 	dnode_t dn_marker;
753 	dnode_t *dn;
754 
755 	mutex_enter(&os->os_lock);
756 	dn = list_head(&os->os_dnodes);
757 	while (dn != NULL) {
758 		/*
759 		 * Skip dnodes without holds.  We have to do this dance
760 		 * because dnode_add_ref() only works if there is already a
761 		 * hold.  If the dnode has no holds, then it has no dbufs.
762 		 */
763 		if (dnode_add_ref(dn, FTAG)) {
764 			list_insert_after(&os->os_dnodes, dn, &dn_marker);
765 			mutex_exit(&os->os_lock);
766 
767 			dnode_evict_dbufs(dn);
768 			dnode_rele(dn, FTAG);
769 
770 			mutex_enter(&os->os_lock);
771 			dn = list_next(&os->os_dnodes, &dn_marker);
772 			list_remove(&os->os_dnodes, &dn_marker);
773 		} else {
774 			dn = list_next(&os->os_dnodes, dn);
775 		}
776 	}
777 	mutex_exit(&os->os_lock);
778 
779 	if (DMU_USERUSED_DNODE(os) != NULL) {
780 		dnode_evict_dbufs(DMU_GROUPUSED_DNODE(os));
781 		dnode_evict_dbufs(DMU_USERUSED_DNODE(os));
782 	}
783 	dnode_evict_dbufs(DMU_META_DNODE(os));
784 }
785 
786 /*
787  * Objset eviction processing is split into into two pieces.
788  * The first marks the objset as evicting, evicts any dbufs that
789  * have a refcount of zero, and then queues up the objset for the
790  * second phase of eviction.  Once os->os_dnodes has been cleared by
791  * dnode_buf_pageout()->dnode_destroy(), the second phase is executed.
792  * The second phase closes the special dnodes, dequeues the objset from
793  * the list of those undergoing eviction, and finally frees the objset.
794  *
795  * NOTE: Due to asynchronous eviction processing (invocation of
796  *       dnode_buf_pageout()), it is possible for the meta dnode for the
797  *       objset to have no holds even though os->os_dnodes is not empty.
798  */
799 void
800 dmu_objset_evict(objset_t *os)
801 {
802 	dsl_dataset_t *ds = os->os_dsl_dataset;
803 
804 	for (int t = 0; t < TXG_SIZE; t++)
805 		ASSERT(!dmu_objset_is_dirty(os, t));
806 
807 	if (ds)
808 		dsl_prop_unregister_all(ds, os);
809 
810 	if (os->os_sa)
811 		sa_tear_down(os);
812 
813 	dmu_objset_evict_dbufs(os);
814 
815 	mutex_enter(&os->os_lock);
816 	spa_evicting_os_register(os->os_spa, os);
817 	if (list_is_empty(&os->os_dnodes)) {
818 		mutex_exit(&os->os_lock);
819 		dmu_objset_evict_done(os);
820 	} else {
821 		mutex_exit(&os->os_lock);
822 	}
823 }
824 
825 void
826 dmu_objset_evict_done(objset_t *os)
827 {
828 	ASSERT3P(list_head(&os->os_dnodes), ==, NULL);
829 
830 	dnode_special_close(&os->os_meta_dnode);
831 	if (DMU_USERUSED_DNODE(os)) {
832 		dnode_special_close(&os->os_userused_dnode);
833 		dnode_special_close(&os->os_groupused_dnode);
834 	}
835 	zil_free(os->os_zil);
836 
837 	arc_buf_destroy(os->os_phys_buf, &os->os_phys_buf);
838 
839 	/*
840 	 * This is a barrier to prevent the objset from going away in
841 	 * dnode_move() until we can safely ensure that the objset is still in
842 	 * use. We consider the objset valid before the barrier and invalid
843 	 * after the barrier.
844 	 */
845 	rw_enter(&os_lock, RW_READER);
846 	rw_exit(&os_lock);
847 
848 	kmem_free(os->os_obj_next_percpu,
849 	    os->os_obj_next_percpu_len * sizeof (os->os_obj_next_percpu[0]));
850 
851 	mutex_destroy(&os->os_lock);
852 	mutex_destroy(&os->os_userused_lock);
853 	mutex_destroy(&os->os_obj_lock);
854 	mutex_destroy(&os->os_user_ptr_lock);
855 	for (int i = 0; i < TXG_SIZE; i++) {
856 		multilist_destroy(os->os_dirty_dnodes[i]);
857 	}
858 	spa_evicting_os_deregister(os->os_spa, os);
859 	kmem_free(os, sizeof (objset_t));
860 }
861 
862 timestruc_t
863 dmu_objset_snap_cmtime(objset_t *os)
864 {
865 	return (dsl_dir_snap_cmtime(os->os_dsl_dataset->ds_dir));
866 }
867 
868 /* called from dsl for meta-objset */
869 objset_t *
870 dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
871     dmu_objset_type_t type, dmu_tx_t *tx)
872 {
873 	objset_t *os;
874 	dnode_t *mdn;
875 
876 	ASSERT(dmu_tx_is_syncing(tx));
877 
878 	if (ds != NULL)
879 		VERIFY0(dmu_objset_from_ds(ds, &os));
880 	else
881 		VERIFY0(dmu_objset_open_impl(spa, NULL, bp, &os));
882 
883 	mdn = DMU_META_DNODE(os);
884 
885 	dnode_allocate(mdn, DMU_OT_DNODE, DNODE_BLOCK_SIZE, DN_MAX_INDBLKSHIFT,
886 	    DMU_OT_NONE, 0, DNODE_MIN_SLOTS, tx);
887 
888 	/*
889 	 * We don't want to have to increase the meta-dnode's nlevels
890 	 * later, because then we could do it in quescing context while
891 	 * we are also accessing it in open context.
892 	 *
893 	 * This precaution is not necessary for the MOS (ds == NULL),
894 	 * because the MOS is only updated in syncing context.
895 	 * This is most fortunate: the MOS is the only objset that
896 	 * needs to be synced multiple times as spa_sync() iterates
897 	 * to convergence, so minimizing its dn_nlevels matters.
898 	 */
899 	if (ds != NULL) {
900 		int levels = 1;
901 
902 		/*
903 		 * Determine the number of levels necessary for the meta-dnode
904 		 * to contain DN_MAX_OBJECT dnodes.  Note that in order to
905 		 * ensure that we do not overflow 64 bits, there has to be
906 		 * a nlevels that gives us a number of blocks > DN_MAX_OBJECT
907 		 * but < 2^64.  Therefore,
908 		 * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT) (10) must be
909 		 * less than (64 - log2(DN_MAX_OBJECT)) (16).
910 		 */
911 		while ((uint64_t)mdn->dn_nblkptr <<
912 		    (mdn->dn_datablkshift - DNODE_SHIFT +
913 		    (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) <
914 		    DN_MAX_OBJECT)
915 			levels++;
916 
917 		mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] =
918 		    mdn->dn_nlevels = levels;
919 	}
920 
921 	ASSERT(type != DMU_OST_NONE);
922 	ASSERT(type != DMU_OST_ANY);
923 	ASSERT(type < DMU_OST_NUMTYPES);
924 	os->os_phys->os_type = type;
925 	if (dmu_objset_userused_enabled(os)) {
926 		os->os_phys->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
927 		os->os_flags = os->os_phys->os_flags;
928 	}
929 
930 	dsl_dataset_dirty(ds, tx);
931 
932 	return (os);
933 }
934 
935 typedef struct dmu_objset_create_arg {
936 	const char *doca_name;
937 	cred_t *doca_cred;
938 	void (*doca_userfunc)(objset_t *os, void *arg,
939 	    cred_t *cr, dmu_tx_t *tx);
940 	void *doca_userarg;
941 	dmu_objset_type_t doca_type;
942 	uint64_t doca_flags;
943 } dmu_objset_create_arg_t;
944 
945 /*ARGSUSED*/
946 static int
947 dmu_objset_create_check(void *arg, dmu_tx_t *tx)
948 {
949 	dmu_objset_create_arg_t *doca = arg;
950 	dsl_pool_t *dp = dmu_tx_pool(tx);
951 	dsl_dir_t *pdd;
952 	const char *tail;
953 	int error;
954 
955 	if (strchr(doca->doca_name, '@') != NULL)
956 		return (SET_ERROR(EINVAL));
957 
958 	if (strlen(doca->doca_name) >= ZFS_MAX_DATASET_NAME_LEN)
959 		return (SET_ERROR(ENAMETOOLONG));
960 
961 	if (dataset_nestcheck(doca->doca_name) != 0)
962 		return (SET_ERROR(ENAMETOOLONG));
963 
964 	error = dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail);
965 	if (error != 0)
966 		return (error);
967 	if (tail == NULL) {
968 		dsl_dir_rele(pdd, FTAG);
969 		return (SET_ERROR(EEXIST));
970 	}
971 	error = dsl_fs_ss_limit_check(pdd, 1, ZFS_PROP_FILESYSTEM_LIMIT, NULL,
972 	    doca->doca_cred);
973 	dsl_dir_rele(pdd, FTAG);
974 
975 	return (error);
976 }
977 
978 static void
979 dmu_objset_create_sync(void *arg, dmu_tx_t *tx)
980 {
981 	dmu_objset_create_arg_t *doca = arg;
982 	dsl_pool_t *dp = dmu_tx_pool(tx);
983 	dsl_dir_t *pdd;
984 	const char *tail;
985 	dsl_dataset_t *ds;
986 	uint64_t obj;
987 	blkptr_t *bp;
988 	objset_t *os;
989 
990 	VERIFY0(dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail));
991 
992 	obj = dsl_dataset_create_sync(pdd, tail, NULL, doca->doca_flags,
993 	    doca->doca_cred, tx);
994 
995 	VERIFY0(dsl_dataset_hold_obj(pdd->dd_pool, obj, FTAG, &ds));
996 	rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
997 	bp = dsl_dataset_get_blkptr(ds);
998 	os = dmu_objset_create_impl(pdd->dd_pool->dp_spa,
999 	    ds, bp, doca->doca_type, tx);
1000 	rrw_exit(&ds->ds_bp_rwlock, FTAG);
1001 
1002 	if (doca->doca_userfunc != NULL) {
1003 		doca->doca_userfunc(os, doca->doca_userarg,
1004 		    doca->doca_cred, tx);
1005 	}
1006 
1007 	spa_history_log_internal_ds(ds, "create", tx, "");
1008 	dsl_dataset_rele(ds, FTAG);
1009 	dsl_dir_rele(pdd, FTAG);
1010 }
1011 
1012 int
1013 dmu_objset_create(const char *name, dmu_objset_type_t type, uint64_t flags,
1014     void (*func)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx), void *arg)
1015 {
1016 	dmu_objset_create_arg_t doca;
1017 
1018 	doca.doca_name = name;
1019 	doca.doca_cred = CRED();
1020 	doca.doca_flags = flags;
1021 	doca.doca_userfunc = func;
1022 	doca.doca_userarg = arg;
1023 	doca.doca_type = type;
1024 
1025 	return (dsl_sync_task(name,
1026 	    dmu_objset_create_check, dmu_objset_create_sync, &doca,
1027 	    5, ZFS_SPACE_CHECK_NORMAL));
1028 }
1029 
1030 typedef struct dmu_objset_clone_arg {
1031 	const char *doca_clone;
1032 	const char *doca_origin;
1033 	cred_t *doca_cred;
1034 } dmu_objset_clone_arg_t;
1035 
1036 /*ARGSUSED*/
1037 static int
1038 dmu_objset_clone_check(void *arg, dmu_tx_t *tx)
1039 {
1040 	dmu_objset_clone_arg_t *doca = arg;
1041 	dsl_dir_t *pdd;
1042 	const char *tail;
1043 	int error;
1044 	dsl_dataset_t *origin;
1045 	dsl_pool_t *dp = dmu_tx_pool(tx);
1046 
1047 	if (strchr(doca->doca_clone, '@') != NULL)
1048 		return (SET_ERROR(EINVAL));
1049 
1050 	if (strlen(doca->doca_clone) >= ZFS_MAX_DATASET_NAME_LEN)
1051 		return (SET_ERROR(ENAMETOOLONG));
1052 
1053 	error = dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail);
1054 	if (error != 0)
1055 		return (error);
1056 	if (tail == NULL) {
1057 		dsl_dir_rele(pdd, FTAG);
1058 		return (SET_ERROR(EEXIST));
1059 	}
1060 
1061 	error = dsl_fs_ss_limit_check(pdd, 1, ZFS_PROP_FILESYSTEM_LIMIT, NULL,
1062 	    doca->doca_cred);
1063 	if (error != 0) {
1064 		dsl_dir_rele(pdd, FTAG);
1065 		return (SET_ERROR(EDQUOT));
1066 	}
1067 	dsl_dir_rele(pdd, FTAG);
1068 
1069 	error = dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin);
1070 	if (error != 0)
1071 		return (error);
1072 
1073 	/* You can only clone snapshots, not the head datasets. */
1074 	if (!origin->ds_is_snapshot) {
1075 		dsl_dataset_rele(origin, FTAG);
1076 		return (SET_ERROR(EINVAL));
1077 	}
1078 	dsl_dataset_rele(origin, FTAG);
1079 
1080 	return (0);
1081 }
1082 
1083 static void
1084 dmu_objset_clone_sync(void *arg, dmu_tx_t *tx)
1085 {
1086 	dmu_objset_clone_arg_t *doca = arg;
1087 	dsl_pool_t *dp = dmu_tx_pool(tx);
1088 	dsl_dir_t *pdd;
1089 	const char *tail;
1090 	dsl_dataset_t *origin, *ds;
1091 	uint64_t obj;
1092 	char namebuf[ZFS_MAX_DATASET_NAME_LEN];
1093 
1094 	VERIFY0(dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail));
1095 	VERIFY0(dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin));
1096 
1097 	obj = dsl_dataset_create_sync(pdd, tail, origin, 0,
1098 	    doca->doca_cred, tx);
1099 
1100 	VERIFY0(dsl_dataset_hold_obj(pdd->dd_pool, obj, FTAG, &ds));
1101 	dsl_dataset_name(origin, namebuf);
1102 	spa_history_log_internal_ds(ds, "clone", tx,
1103 	    "origin=%s (%llu)", namebuf, origin->ds_object);
1104 	dsl_dataset_rele(ds, FTAG);
1105 	dsl_dataset_rele(origin, FTAG);
1106 	dsl_dir_rele(pdd, FTAG);
1107 }
1108 
1109 int
1110 dmu_objset_clone(const char *clone, const char *origin)
1111 {
1112 	dmu_objset_clone_arg_t doca;
1113 
1114 	doca.doca_clone = clone;
1115 	doca.doca_origin = origin;
1116 	doca.doca_cred = CRED();
1117 
1118 	return (dsl_sync_task(clone,
1119 	    dmu_objset_clone_check, dmu_objset_clone_sync, &doca,
1120 	    5, ZFS_SPACE_CHECK_NORMAL));
1121 }
1122 
1123 static int
1124 dmu_objset_remap_indirects_impl(objset_t *os, uint64_t last_removed_txg)
1125 {
1126 	int error = 0;
1127 	uint64_t object = 0;
1128 	while ((error = dmu_object_next(os, &object, B_FALSE, 0)) == 0) {
1129 		error = dmu_object_remap_indirects(os, object,
1130 		    last_removed_txg);
1131 		/*
1132 		 * If the ZPL removed the object before we managed to dnode_hold
1133 		 * it, we would get an ENOENT. If the ZPL declares its intent
1134 		 * to remove the object (dnode_free) before we manage to
1135 		 * dnode_hold it, we would get an EEXIST. In either case, we
1136 		 * want to continue remapping the other objects in the objset;
1137 		 * in all other cases, we want to break early.
1138 		 */
1139 		if (error != 0 && error != ENOENT && error != EEXIST) {
1140 			break;
1141 		}
1142 	}
1143 	if (error == ESRCH) {
1144 		error = 0;
1145 	}
1146 	return (error);
1147 }
1148 
1149 int
1150 dmu_objset_remap_indirects(const char *fsname)
1151 {
1152 	int error = 0;
1153 	objset_t *os = NULL;
1154 	uint64_t last_removed_txg;
1155 	uint64_t remap_start_txg;
1156 	dsl_dir_t *dd;
1157 
1158 	error = dmu_objset_hold(fsname, FTAG, &os);
1159 	if (error != 0) {
1160 		return (error);
1161 	}
1162 	dd = dmu_objset_ds(os)->ds_dir;
1163 
1164 	if (!spa_feature_is_enabled(dmu_objset_spa(os),
1165 	    SPA_FEATURE_OBSOLETE_COUNTS)) {
1166 		dmu_objset_rele(os, FTAG);
1167 		return (SET_ERROR(ENOTSUP));
1168 	}
1169 
1170 	if (dsl_dataset_is_snapshot(dmu_objset_ds(os))) {
1171 		dmu_objset_rele(os, FTAG);
1172 		return (SET_ERROR(EINVAL));
1173 	}
1174 
1175 	/*
1176 	 * If there has not been a removal, we're done.
1177 	 */
1178 	last_removed_txg = spa_get_last_removal_txg(dmu_objset_spa(os));
1179 	if (last_removed_txg == -1ULL) {
1180 		dmu_objset_rele(os, FTAG);
1181 		return (0);
1182 	}
1183 
1184 	/*
1185 	 * If we have remapped since the last removal, we're done.
1186 	 */
1187 	if (dsl_dir_is_zapified(dd)) {
1188 		uint64_t last_remap_txg;
1189 		if (zap_lookup(spa_meta_objset(dmu_objset_spa(os)),
1190 		    dd->dd_object, DD_FIELD_LAST_REMAP_TXG,
1191 		    sizeof (last_remap_txg), 1, &last_remap_txg) == 0 &&
1192 		    last_remap_txg > last_removed_txg) {
1193 			dmu_objset_rele(os, FTAG);
1194 			return (0);
1195 		}
1196 	}
1197 
1198 	dsl_dataset_long_hold(dmu_objset_ds(os), FTAG);
1199 	dsl_pool_rele(dmu_objset_pool(os), FTAG);
1200 
1201 	remap_start_txg = spa_last_synced_txg(dmu_objset_spa(os));
1202 	error = dmu_objset_remap_indirects_impl(os, last_removed_txg);
1203 	if (error == 0) {
1204 		/*
1205 		 * We update the last_remap_txg to be the start txg so that
1206 		 * we can guarantee that every block older than last_remap_txg
1207 		 * that can be remapped has been remapped.
1208 		 */
1209 		error = dsl_dir_update_last_remap_txg(dd, remap_start_txg);
1210 	}
1211 
1212 	dsl_dataset_long_rele(dmu_objset_ds(os), FTAG);
1213 	dsl_dataset_rele(dmu_objset_ds(os), FTAG);
1214 
1215 	return (error);
1216 }
1217 
1218 int
1219 dmu_objset_snapshot_one(const char *fsname, const char *snapname)
1220 {
1221 	int err;
1222 	char *longsnap = kmem_asprintf("%s@%s", fsname, snapname);
1223 	nvlist_t *snaps = fnvlist_alloc();
1224 
1225 	fnvlist_add_boolean(snaps, longsnap);
1226 	strfree(longsnap);
1227 	err = dsl_dataset_snapshot(snaps, NULL, NULL);
1228 	fnvlist_free(snaps);
1229 	return (err);
1230 }
1231 
1232 static void
1233 dmu_objset_sync_dnodes(multilist_sublist_t *list, dmu_tx_t *tx)
1234 {
1235 	dnode_t *dn;
1236 
1237 	while ((dn = multilist_sublist_head(list)) != NULL) {
1238 		ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
1239 		ASSERT(dn->dn_dbuf->db_data_pending);
1240 		/*
1241 		 * Initialize dn_zio outside dnode_sync() because the
1242 		 * meta-dnode needs to set it ouside dnode_sync().
1243 		 */
1244 		dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio;
1245 		ASSERT(dn->dn_zio);
1246 
1247 		ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS);
1248 		multilist_sublist_remove(list, dn);
1249 
1250 		multilist_t *newlist = dn->dn_objset->os_synced_dnodes;
1251 		if (newlist != NULL) {
1252 			(void) dnode_add_ref(dn, newlist);
1253 			multilist_insert(newlist, dn);
1254 		}
1255 
1256 		dnode_sync(dn, tx);
1257 	}
1258 }
1259 
1260 /* ARGSUSED */
1261 static void
1262 dmu_objset_write_ready(zio_t *zio, arc_buf_t *abuf, void *arg)
1263 {
1264 	blkptr_t *bp = zio->io_bp;
1265 	objset_t *os = arg;
1266 	dnode_phys_t *dnp = &os->os_phys->os_meta_dnode;
1267 
1268 	ASSERT(!BP_IS_EMBEDDED(bp));
1269 	ASSERT3U(BP_GET_TYPE(bp), ==, DMU_OT_OBJSET);
1270 	ASSERT0(BP_GET_LEVEL(bp));
1271 
1272 	/*
1273 	 * Update rootbp fill count: it should be the number of objects
1274 	 * allocated in the object set (not counting the "special"
1275 	 * objects that are stored in the objset_phys_t -- the meta
1276 	 * dnode and user/group accounting objects).
1277 	 */
1278 	bp->blk_fill = 0;
1279 	for (int i = 0; i < dnp->dn_nblkptr; i++)
1280 		bp->blk_fill += BP_GET_FILL(&dnp->dn_blkptr[i]);
1281 	if (os->os_dsl_dataset != NULL)
1282 		rrw_enter(&os->os_dsl_dataset->ds_bp_rwlock, RW_WRITER, FTAG);
1283 	*os->os_rootbp = *bp;
1284 	if (os->os_dsl_dataset != NULL)
1285 		rrw_exit(&os->os_dsl_dataset->ds_bp_rwlock, FTAG);
1286 }
1287 
1288 /* ARGSUSED */
1289 static void
1290 dmu_objset_write_done(zio_t *zio, arc_buf_t *abuf, void *arg)
1291 {
1292 	blkptr_t *bp = zio->io_bp;
1293 	blkptr_t *bp_orig = &zio->io_bp_orig;
1294 	objset_t *os = arg;
1295 
1296 	if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
1297 		ASSERT(BP_EQUAL(bp, bp_orig));
1298 	} else {
1299 		dsl_dataset_t *ds = os->os_dsl_dataset;
1300 		dmu_tx_t *tx = os->os_synctx;
1301 
1302 		(void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
1303 		dsl_dataset_block_born(ds, bp, tx);
1304 	}
1305 	kmem_free(bp, sizeof (*bp));
1306 }
1307 
1308 typedef struct sync_dnodes_arg {
1309 	multilist_t *sda_list;
1310 	int sda_sublist_idx;
1311 	multilist_t *sda_newlist;
1312 	dmu_tx_t *sda_tx;
1313 } sync_dnodes_arg_t;
1314 
1315 static void
1316 sync_dnodes_task(void *arg)
1317 {
1318 	sync_dnodes_arg_t *sda = arg;
1319 
1320 	multilist_sublist_t *ms =
1321 	    multilist_sublist_lock(sda->sda_list, sda->sda_sublist_idx);
1322 
1323 	dmu_objset_sync_dnodes(ms, sda->sda_tx);
1324 
1325 	multilist_sublist_unlock(ms);
1326 
1327 	kmem_free(sda, sizeof (*sda));
1328 }
1329 
1330 
1331 /* called from dsl */
1332 void
1333 dmu_objset_sync(objset_t *os, zio_t *pio, dmu_tx_t *tx)
1334 {
1335 	int txgoff;
1336 	zbookmark_phys_t zb;
1337 	zio_prop_t zp;
1338 	zio_t *zio;
1339 	list_t *list;
1340 	dbuf_dirty_record_t *dr;
1341 	blkptr_t *blkptr_copy = kmem_alloc(sizeof (*os->os_rootbp), KM_SLEEP);
1342 	*blkptr_copy = *os->os_rootbp;
1343 
1344 	dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg);
1345 
1346 	ASSERT(dmu_tx_is_syncing(tx));
1347 	/* XXX the write_done callback should really give us the tx... */
1348 	os->os_synctx = tx;
1349 
1350 	if (os->os_dsl_dataset == NULL) {
1351 		/*
1352 		 * This is the MOS.  If we have upgraded,
1353 		 * spa_max_replication() could change, so reset
1354 		 * os_copies here.
1355 		 */
1356 		os->os_copies = spa_max_replication(os->os_spa);
1357 	}
1358 
1359 	/*
1360 	 * Create the root block IO
1361 	 */
1362 	SET_BOOKMARK(&zb, os->os_dsl_dataset ?
1363 	    os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
1364 	    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
1365 	arc_release(os->os_phys_buf, &os->os_phys_buf);
1366 
1367 	dmu_write_policy(os, NULL, 0, 0, &zp);
1368 
1369 	zio = arc_write(pio, os->os_spa, tx->tx_txg,
1370 	    blkptr_copy, os->os_phys_buf, DMU_OS_IS_L2CACHEABLE(os),
1371 	    &zp, dmu_objset_write_ready, NULL, NULL, dmu_objset_write_done,
1372 	    os, ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
1373 
1374 	/*
1375 	 * Sync special dnodes - the parent IO for the sync is the root block
1376 	 */
1377 	DMU_META_DNODE(os)->dn_zio = zio;
1378 	dnode_sync(DMU_META_DNODE(os), tx);
1379 
1380 	os->os_phys->os_flags = os->os_flags;
1381 
1382 	if (DMU_USERUSED_DNODE(os) &&
1383 	    DMU_USERUSED_DNODE(os)->dn_type != DMU_OT_NONE) {
1384 		DMU_USERUSED_DNODE(os)->dn_zio = zio;
1385 		dnode_sync(DMU_USERUSED_DNODE(os), tx);
1386 		DMU_GROUPUSED_DNODE(os)->dn_zio = zio;
1387 		dnode_sync(DMU_GROUPUSED_DNODE(os), tx);
1388 	}
1389 
1390 	txgoff = tx->tx_txg & TXG_MASK;
1391 
1392 	if (dmu_objset_userused_enabled(os)) {
1393 		/*
1394 		 * We must create the list here because it uses the
1395 		 * dn_dirty_link[] of this txg.  But it may already
1396 		 * exist because we call dsl_dataset_sync() twice per txg.
1397 		 */
1398 		if (os->os_synced_dnodes == NULL) {
1399 			os->os_synced_dnodes =
1400 			    multilist_create(sizeof (dnode_t),
1401 			    offsetof(dnode_t, dn_dirty_link[txgoff]),
1402 			    dnode_multilist_index_func);
1403 		} else {
1404 			ASSERT3U(os->os_synced_dnodes->ml_offset, ==,
1405 			    offsetof(dnode_t, dn_dirty_link[txgoff]));
1406 		}
1407 	}
1408 
1409 	for (int i = 0;
1410 	    i < multilist_get_num_sublists(os->os_dirty_dnodes[txgoff]); i++) {
1411 		sync_dnodes_arg_t *sda = kmem_alloc(sizeof (*sda), KM_SLEEP);
1412 		sda->sda_list = os->os_dirty_dnodes[txgoff];
1413 		sda->sda_sublist_idx = i;
1414 		sda->sda_tx = tx;
1415 		(void) taskq_dispatch(dmu_objset_pool(os)->dp_sync_taskq,
1416 		    sync_dnodes_task, sda, 0);
1417 		/* callback frees sda */
1418 	}
1419 	taskq_wait(dmu_objset_pool(os)->dp_sync_taskq);
1420 
1421 	list = &DMU_META_DNODE(os)->dn_dirty_records[txgoff];
1422 	while ((dr = list_head(list)) != NULL) {
1423 		ASSERT0(dr->dr_dbuf->db_level);
1424 		list_remove(list, dr);
1425 		if (dr->dr_zio)
1426 			zio_nowait(dr->dr_zio);
1427 	}
1428 
1429 	/* Enable dnode backfill if enough objects have been freed. */
1430 	if (os->os_freed_dnodes >= dmu_rescan_dnode_threshold) {
1431 		os->os_rescan_dnodes = B_TRUE;
1432 		os->os_freed_dnodes = 0;
1433 	}
1434 
1435 	/*
1436 	 * Free intent log blocks up to this tx.
1437 	 */
1438 	zil_sync(os->os_zil, tx);
1439 	os->os_phys->os_zil_header = os->os_zil_header;
1440 	zio_nowait(zio);
1441 }
1442 
1443 boolean_t
1444 dmu_objset_is_dirty(objset_t *os, uint64_t txg)
1445 {
1446 	return (!multilist_is_empty(os->os_dirty_dnodes[txg & TXG_MASK]));
1447 }
1448 
1449 static objset_used_cb_t *used_cbs[DMU_OST_NUMTYPES];
1450 
1451 void
1452 dmu_objset_register_type(dmu_objset_type_t ost, objset_used_cb_t *cb)
1453 {
1454 	used_cbs[ost] = cb;
1455 }
1456 
1457 boolean_t
1458 dmu_objset_userused_enabled(objset_t *os)
1459 {
1460 	return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE &&
1461 	    used_cbs[os->os_phys->os_type] != NULL &&
1462 	    DMU_USERUSED_DNODE(os) != NULL);
1463 }
1464 
1465 typedef struct userquota_node {
1466 	uint64_t uqn_id;
1467 	int64_t uqn_delta;
1468 	avl_node_t uqn_node;
1469 } userquota_node_t;
1470 
1471 typedef struct userquota_cache {
1472 	avl_tree_t uqc_user_deltas;
1473 	avl_tree_t uqc_group_deltas;
1474 } userquota_cache_t;
1475 
1476 static int
1477 userquota_compare(const void *l, const void *r)
1478 {
1479 	const userquota_node_t *luqn = l;
1480 	const userquota_node_t *ruqn = r;
1481 
1482 	if (luqn->uqn_id < ruqn->uqn_id)
1483 		return (-1);
1484 	if (luqn->uqn_id > ruqn->uqn_id)
1485 		return (1);
1486 	return (0);
1487 }
1488 
1489 static void
1490 do_userquota_cacheflush(objset_t *os, userquota_cache_t *cache, dmu_tx_t *tx)
1491 {
1492 	void *cookie;
1493 	userquota_node_t *uqn;
1494 
1495 	ASSERT(dmu_tx_is_syncing(tx));
1496 
1497 	cookie = NULL;
1498 	while ((uqn = avl_destroy_nodes(&cache->uqc_user_deltas,
1499 	    &cookie)) != NULL) {
1500 		/*
1501 		 * os_userused_lock protects against concurrent calls to
1502 		 * zap_increment_int().  It's needed because zap_increment_int()
1503 		 * is not thread-safe (i.e. not atomic).
1504 		 */
1505 		mutex_enter(&os->os_userused_lock);
1506 		VERIFY0(zap_increment_int(os, DMU_USERUSED_OBJECT,
1507 		    uqn->uqn_id, uqn->uqn_delta, tx));
1508 		mutex_exit(&os->os_userused_lock);
1509 		kmem_free(uqn, sizeof (*uqn));
1510 	}
1511 	avl_destroy(&cache->uqc_user_deltas);
1512 
1513 	cookie = NULL;
1514 	while ((uqn = avl_destroy_nodes(&cache->uqc_group_deltas,
1515 	    &cookie)) != NULL) {
1516 		mutex_enter(&os->os_userused_lock);
1517 		VERIFY0(zap_increment_int(os, DMU_GROUPUSED_OBJECT,
1518 		    uqn->uqn_id, uqn->uqn_delta, tx));
1519 		mutex_exit(&os->os_userused_lock);
1520 		kmem_free(uqn, sizeof (*uqn));
1521 	}
1522 	avl_destroy(&cache->uqc_group_deltas);
1523 }
1524 
1525 static void
1526 userquota_update_cache(avl_tree_t *avl, uint64_t id, int64_t delta)
1527 {
1528 	userquota_node_t search = { .uqn_id = id };
1529 	avl_index_t idx;
1530 
1531 	userquota_node_t *uqn = avl_find(avl, &search, &idx);
1532 	if (uqn == NULL) {
1533 		uqn = kmem_zalloc(sizeof (*uqn), KM_SLEEP);
1534 		uqn->uqn_id = id;
1535 		avl_insert(avl, uqn, idx);
1536 	}
1537 	uqn->uqn_delta += delta;
1538 }
1539 
1540 static void
1541 do_userquota_update(userquota_cache_t *cache, uint64_t used, uint64_t flags,
1542     uint64_t user, uint64_t group, boolean_t subtract)
1543 {
1544 	if ((flags & DNODE_FLAG_USERUSED_ACCOUNTED)) {
1545 		int64_t delta = DNODE_MIN_SIZE + used;
1546 		if (subtract)
1547 			delta = -delta;
1548 
1549 		userquota_update_cache(&cache->uqc_user_deltas, user, delta);
1550 		userquota_update_cache(&cache->uqc_group_deltas, group, delta);
1551 	}
1552 }
1553 
1554 typedef struct userquota_updates_arg {
1555 	objset_t *uua_os;
1556 	int uua_sublist_idx;
1557 	dmu_tx_t *uua_tx;
1558 } userquota_updates_arg_t;
1559 
1560 static void
1561 userquota_updates_task(void *arg)
1562 {
1563 	userquota_updates_arg_t *uua = arg;
1564 	objset_t *os = uua->uua_os;
1565 	dmu_tx_t *tx = uua->uua_tx;
1566 	dnode_t *dn;
1567 	userquota_cache_t cache = { 0 };
1568 
1569 	multilist_sublist_t *list =
1570 	    multilist_sublist_lock(os->os_synced_dnodes, uua->uua_sublist_idx);
1571 
1572 	ASSERT(multilist_sublist_head(list) == NULL ||
1573 	    dmu_objset_userused_enabled(os));
1574 	avl_create(&cache.uqc_user_deltas, userquota_compare,
1575 	    sizeof (userquota_node_t), offsetof(userquota_node_t, uqn_node));
1576 	avl_create(&cache.uqc_group_deltas, userquota_compare,
1577 	    sizeof (userquota_node_t), offsetof(userquota_node_t, uqn_node));
1578 
1579 	while ((dn = multilist_sublist_head(list)) != NULL) {
1580 		int flags;
1581 		ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object));
1582 		ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE ||
1583 		    dn->dn_phys->dn_flags &
1584 		    DNODE_FLAG_USERUSED_ACCOUNTED);
1585 
1586 		flags = dn->dn_id_flags;
1587 		ASSERT(flags);
1588 		if (flags & DN_ID_OLD_EXIST)  {
1589 			do_userquota_update(&cache,
1590 			    dn->dn_oldused, dn->dn_oldflags,
1591 			    dn->dn_olduid, dn->dn_oldgid, B_TRUE);
1592 		}
1593 		if (flags & DN_ID_NEW_EXIST) {
1594 			do_userquota_update(&cache,
1595 			    DN_USED_BYTES(dn->dn_phys),
1596 			    dn->dn_phys->dn_flags,  dn->dn_newuid,
1597 			    dn->dn_newgid, B_FALSE);
1598 		}
1599 
1600 		mutex_enter(&dn->dn_mtx);
1601 		dn->dn_oldused = 0;
1602 		dn->dn_oldflags = 0;
1603 		if (dn->dn_id_flags & DN_ID_NEW_EXIST) {
1604 			dn->dn_olduid = dn->dn_newuid;
1605 			dn->dn_oldgid = dn->dn_newgid;
1606 			dn->dn_id_flags |= DN_ID_OLD_EXIST;
1607 			if (dn->dn_bonuslen == 0)
1608 				dn->dn_id_flags |= DN_ID_CHKED_SPILL;
1609 			else
1610 				dn->dn_id_flags |= DN_ID_CHKED_BONUS;
1611 		}
1612 		dn->dn_id_flags &= ~(DN_ID_NEW_EXIST);
1613 		mutex_exit(&dn->dn_mtx);
1614 
1615 		multilist_sublist_remove(list, dn);
1616 		dnode_rele(dn, os->os_synced_dnodes);
1617 	}
1618 	do_userquota_cacheflush(os, &cache, tx);
1619 	multilist_sublist_unlock(list);
1620 	kmem_free(uua, sizeof (*uua));
1621 }
1622 
1623 void
1624 dmu_objset_do_userquota_updates(objset_t *os, dmu_tx_t *tx)
1625 {
1626 	if (!dmu_objset_userused_enabled(os))
1627 		return;
1628 
1629 	/* Allocate the user/groupused objects if necessary. */
1630 	if (DMU_USERUSED_DNODE(os)->dn_type == DMU_OT_NONE) {
1631 		VERIFY0(zap_create_claim(os,
1632 		    DMU_USERUSED_OBJECT,
1633 		    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
1634 		VERIFY0(zap_create_claim(os,
1635 		    DMU_GROUPUSED_OBJECT,
1636 		    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
1637 	}
1638 
1639 	for (int i = 0;
1640 	    i < multilist_get_num_sublists(os->os_synced_dnodes); i++) {
1641 		userquota_updates_arg_t *uua =
1642 		    kmem_alloc(sizeof (*uua), KM_SLEEP);
1643 		uua->uua_os = os;
1644 		uua->uua_sublist_idx = i;
1645 		uua->uua_tx = tx;
1646 		/* note: caller does taskq_wait() */
1647 		(void) taskq_dispatch(dmu_objset_pool(os)->dp_sync_taskq,
1648 		    userquota_updates_task, uua, 0);
1649 		/* callback frees uua */
1650 	}
1651 }
1652 
1653 /*
1654  * Returns a pointer to data to find uid/gid from
1655  *
1656  * If a dirty record for transaction group that is syncing can't
1657  * be found then NULL is returned.  In the NULL case it is assumed
1658  * the uid/gid aren't changing.
1659  */
1660 static void *
1661 dmu_objset_userquota_find_data(dmu_buf_impl_t *db, dmu_tx_t *tx)
1662 {
1663 	dbuf_dirty_record_t *dr, **drp;
1664 	void *data;
1665 
1666 	if (db->db_dirtycnt == 0)
1667 		return (db->db.db_data);  /* Nothing is changing */
1668 
1669 	for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next)
1670 		if (dr->dr_txg == tx->tx_txg)
1671 			break;
1672 
1673 	if (dr == NULL) {
1674 		data = NULL;
1675 	} else {
1676 		dnode_t *dn;
1677 
1678 		DB_DNODE_ENTER(dr->dr_dbuf);
1679 		dn = DB_DNODE(dr->dr_dbuf);
1680 
1681 		if (dn->dn_bonuslen == 0 &&
1682 		    dr->dr_dbuf->db_blkid == DMU_SPILL_BLKID)
1683 			data = dr->dt.dl.dr_data->b_data;
1684 		else
1685 			data = dr->dt.dl.dr_data;
1686 
1687 		DB_DNODE_EXIT(dr->dr_dbuf);
1688 	}
1689 
1690 	return (data);
1691 }
1692 
1693 void
1694 dmu_objset_userquota_get_ids(dnode_t *dn, boolean_t before, dmu_tx_t *tx)
1695 {
1696 	objset_t *os = dn->dn_objset;
1697 	void *data = NULL;
1698 	dmu_buf_impl_t *db = NULL;
1699 	uint64_t *user = NULL;
1700 	uint64_t *group = NULL;
1701 	int flags = dn->dn_id_flags;
1702 	int error;
1703 	boolean_t have_spill = B_FALSE;
1704 
1705 	if (!dmu_objset_userused_enabled(dn->dn_objset))
1706 		return;
1707 
1708 	if (before && (flags & (DN_ID_CHKED_BONUS|DN_ID_OLD_EXIST|
1709 	    DN_ID_CHKED_SPILL)))
1710 		return;
1711 
1712 	if (before && dn->dn_bonuslen != 0)
1713 		data = DN_BONUS(dn->dn_phys);
1714 	else if (!before && dn->dn_bonuslen != 0) {
1715 		if (dn->dn_bonus) {
1716 			db = dn->dn_bonus;
1717 			mutex_enter(&db->db_mtx);
1718 			data = dmu_objset_userquota_find_data(db, tx);
1719 		} else {
1720 			data = DN_BONUS(dn->dn_phys);
1721 		}
1722 	} else if (dn->dn_bonuslen == 0 && dn->dn_bonustype == DMU_OT_SA) {
1723 			int rf = 0;
1724 
1725 			if (RW_WRITE_HELD(&dn->dn_struct_rwlock))
1726 				rf |= DB_RF_HAVESTRUCT;
1727 			error = dmu_spill_hold_by_dnode(dn,
1728 			    rf | DB_RF_MUST_SUCCEED,
1729 			    FTAG, (dmu_buf_t **)&db);
1730 			ASSERT(error == 0);
1731 			mutex_enter(&db->db_mtx);
1732 			data = (before) ? db->db.db_data :
1733 			    dmu_objset_userquota_find_data(db, tx);
1734 			have_spill = B_TRUE;
1735 	} else {
1736 		mutex_enter(&dn->dn_mtx);
1737 		dn->dn_id_flags |= DN_ID_CHKED_BONUS;
1738 		mutex_exit(&dn->dn_mtx);
1739 		return;
1740 	}
1741 
1742 	if (before) {
1743 		ASSERT(data);
1744 		user = &dn->dn_olduid;
1745 		group = &dn->dn_oldgid;
1746 	} else if (data) {
1747 		user = &dn->dn_newuid;
1748 		group = &dn->dn_newgid;
1749 	}
1750 
1751 	/*
1752 	 * Must always call the callback in case the object
1753 	 * type has changed and that type isn't an object type to track
1754 	 */
1755 	error = used_cbs[os->os_phys->os_type](dn->dn_bonustype, data,
1756 	    user, group);
1757 
1758 	/*
1759 	 * Preserve existing uid/gid when the callback can't determine
1760 	 * what the new uid/gid are and the callback returned EEXIST.
1761 	 * The EEXIST error tells us to just use the existing uid/gid.
1762 	 * If we don't know what the old values are then just assign
1763 	 * them to 0, since that is a new file  being created.
1764 	 */
1765 	if (!before && data == NULL && error == EEXIST) {
1766 		if (flags & DN_ID_OLD_EXIST) {
1767 			dn->dn_newuid = dn->dn_olduid;
1768 			dn->dn_newgid = dn->dn_oldgid;
1769 		} else {
1770 			dn->dn_newuid = 0;
1771 			dn->dn_newgid = 0;
1772 		}
1773 		error = 0;
1774 	}
1775 
1776 	if (db)
1777 		mutex_exit(&db->db_mtx);
1778 
1779 	mutex_enter(&dn->dn_mtx);
1780 	if (error == 0 && before)
1781 		dn->dn_id_flags |= DN_ID_OLD_EXIST;
1782 	if (error == 0 && !before)
1783 		dn->dn_id_flags |= DN_ID_NEW_EXIST;
1784 
1785 	if (have_spill) {
1786 		dn->dn_id_flags |= DN_ID_CHKED_SPILL;
1787 	} else {
1788 		dn->dn_id_flags |= DN_ID_CHKED_BONUS;
1789 	}
1790 	mutex_exit(&dn->dn_mtx);
1791 	if (have_spill)
1792 		dmu_buf_rele((dmu_buf_t *)db, FTAG);
1793 }
1794 
1795 boolean_t
1796 dmu_objset_userspace_present(objset_t *os)
1797 {
1798 	return (os->os_phys->os_flags &
1799 	    OBJSET_FLAG_USERACCOUNTING_COMPLETE);
1800 }
1801 
1802 int
1803 dmu_objset_userspace_upgrade(objset_t *os)
1804 {
1805 	uint64_t obj;
1806 	int err = 0;
1807 
1808 	if (dmu_objset_userspace_present(os))
1809 		return (0);
1810 	if (!dmu_objset_userused_enabled(os))
1811 		return (SET_ERROR(ENOTSUP));
1812 	if (dmu_objset_is_snapshot(os))
1813 		return (SET_ERROR(EINVAL));
1814 
1815 	/*
1816 	 * We simply need to mark every object dirty, so that it will be
1817 	 * synced out and now accounted.  If this is called
1818 	 * concurrently, or if we already did some work before crashing,
1819 	 * that's fine, since we track each object's accounted state
1820 	 * independently.
1821 	 */
1822 
1823 	for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
1824 		dmu_tx_t *tx;
1825 		dmu_buf_t *db;
1826 		int objerr;
1827 
1828 		if (issig(JUSTLOOKING) && issig(FORREAL))
1829 			return (SET_ERROR(EINTR));
1830 
1831 		objerr = dmu_bonus_hold(os, obj, FTAG, &db);
1832 		if (objerr != 0)
1833 			continue;
1834 		tx = dmu_tx_create(os);
1835 		dmu_tx_hold_bonus(tx, obj);
1836 		objerr = dmu_tx_assign(tx, TXG_WAIT);
1837 		if (objerr != 0) {
1838 			dmu_tx_abort(tx);
1839 			continue;
1840 		}
1841 		dmu_buf_will_dirty(db, tx);
1842 		dmu_buf_rele(db, FTAG);
1843 		dmu_tx_commit(tx);
1844 	}
1845 
1846 	os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
1847 	txg_wait_synced(dmu_objset_pool(os), 0);
1848 	return (0);
1849 }
1850 
1851 void
1852 dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp,
1853     uint64_t *usedobjsp, uint64_t *availobjsp)
1854 {
1855 	dsl_dataset_space(os->os_dsl_dataset, refdbytesp, availbytesp,
1856 	    usedobjsp, availobjsp);
1857 }
1858 
1859 uint64_t
1860 dmu_objset_fsid_guid(objset_t *os)
1861 {
1862 	return (dsl_dataset_fsid_guid(os->os_dsl_dataset));
1863 }
1864 
1865 void
1866 dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat)
1867 {
1868 	stat->dds_type = os->os_phys->os_type;
1869 	if (os->os_dsl_dataset)
1870 		dsl_dataset_fast_stat(os->os_dsl_dataset, stat);
1871 }
1872 
1873 void
1874 dmu_objset_stats(objset_t *os, nvlist_t *nv)
1875 {
1876 	ASSERT(os->os_dsl_dataset ||
1877 	    os->os_phys->os_type == DMU_OST_META);
1878 
1879 	if (os->os_dsl_dataset != NULL)
1880 		dsl_dataset_stats(os->os_dsl_dataset, nv);
1881 
1882 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE,
1883 	    os->os_phys->os_type);
1884 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERACCOUNTING,
1885 	    dmu_objset_userspace_present(os));
1886 }
1887 
1888 int
1889 dmu_objset_is_snapshot(objset_t *os)
1890 {
1891 	if (os->os_dsl_dataset != NULL)
1892 		return (os->os_dsl_dataset->ds_is_snapshot);
1893 	else
1894 		return (B_FALSE);
1895 }
1896 
1897 int
1898 dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen,
1899     boolean_t *conflict)
1900 {
1901 	dsl_dataset_t *ds = os->os_dsl_dataset;
1902 	uint64_t ignored;
1903 
1904 	if (dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0)
1905 		return (SET_ERROR(ENOENT));
1906 
1907 	return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset,
1908 	    dsl_dataset_phys(ds)->ds_snapnames_zapobj, name, 8, 1, &ignored,
1909 	    MT_NORMALIZE, real, maxlen, conflict));
1910 }
1911 
1912 int
1913 dmu_snapshot_list_next(objset_t *os, int namelen, char *name,
1914     uint64_t *idp, uint64_t *offp, boolean_t *case_conflict)
1915 {
1916 	dsl_dataset_t *ds = os->os_dsl_dataset;
1917 	zap_cursor_t cursor;
1918 	zap_attribute_t attr;
1919 
1920 	ASSERT(dsl_pool_config_held(dmu_objset_pool(os)));
1921 
1922 	if (dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0)
1923 		return (SET_ERROR(ENOENT));
1924 
1925 	zap_cursor_init_serialized(&cursor,
1926 	    ds->ds_dir->dd_pool->dp_meta_objset,
1927 	    dsl_dataset_phys(ds)->ds_snapnames_zapobj, *offp);
1928 
1929 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
1930 		zap_cursor_fini(&cursor);
1931 		return (SET_ERROR(ENOENT));
1932 	}
1933 
1934 	if (strlen(attr.za_name) + 1 > namelen) {
1935 		zap_cursor_fini(&cursor);
1936 		return (SET_ERROR(ENAMETOOLONG));
1937 	}
1938 
1939 	(void) strcpy(name, attr.za_name);
1940 	if (idp)
1941 		*idp = attr.za_first_integer;
1942 	if (case_conflict)
1943 		*case_conflict = attr.za_normalization_conflict;
1944 	zap_cursor_advance(&cursor);
1945 	*offp = zap_cursor_serialize(&cursor);
1946 	zap_cursor_fini(&cursor);
1947 
1948 	return (0);
1949 }
1950 
1951 int
1952 dmu_dir_list_next(objset_t *os, int namelen, char *name,
1953     uint64_t *idp, uint64_t *offp)
1954 {
1955 	dsl_dir_t *dd = os->os_dsl_dataset->ds_dir;
1956 	zap_cursor_t cursor;
1957 	zap_attribute_t attr;
1958 
1959 	/* there is no next dir on a snapshot! */
1960 	if (os->os_dsl_dataset->ds_object !=
1961 	    dsl_dir_phys(dd)->dd_head_dataset_obj)
1962 		return (SET_ERROR(ENOENT));
1963 
1964 	zap_cursor_init_serialized(&cursor,
1965 	    dd->dd_pool->dp_meta_objset,
1966 	    dsl_dir_phys(dd)->dd_child_dir_zapobj, *offp);
1967 
1968 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
1969 		zap_cursor_fini(&cursor);
1970 		return (SET_ERROR(ENOENT));
1971 	}
1972 
1973 	if (strlen(attr.za_name) + 1 > namelen) {
1974 		zap_cursor_fini(&cursor);
1975 		return (SET_ERROR(ENAMETOOLONG));
1976 	}
1977 
1978 	(void) strcpy(name, attr.za_name);
1979 	if (idp)
1980 		*idp = attr.za_first_integer;
1981 	zap_cursor_advance(&cursor);
1982 	*offp = zap_cursor_serialize(&cursor);
1983 	zap_cursor_fini(&cursor);
1984 
1985 	return (0);
1986 }
1987 
1988 typedef struct dmu_objset_find_ctx {
1989 	taskq_t		*dc_tq;
1990 	dsl_pool_t	*dc_dp;
1991 	uint64_t	dc_ddobj;
1992 	char		*dc_ddname; /* last component of ddobj's name */
1993 	int		(*dc_func)(dsl_pool_t *, dsl_dataset_t *, void *);
1994 	void		*dc_arg;
1995 	int		dc_flags;
1996 	kmutex_t	*dc_error_lock;
1997 	int		*dc_error;
1998 } dmu_objset_find_ctx_t;
1999 
2000 static void
2001 dmu_objset_find_dp_impl(dmu_objset_find_ctx_t *dcp)
2002 {
2003 	dsl_pool_t *dp = dcp->dc_dp;
2004 	dsl_dir_t *dd;
2005 	dsl_dataset_t *ds;
2006 	zap_cursor_t zc;
2007 	zap_attribute_t *attr;
2008 	uint64_t thisobj;
2009 	int err = 0;
2010 
2011 	/* don't process if there already was an error */
2012 	if (*dcp->dc_error != 0)
2013 		goto out;
2014 
2015 	/*
2016 	 * Note: passing the name (dc_ddname) here is optional, but it
2017 	 * improves performance because we don't need to call
2018 	 * zap_value_search() to determine the name.
2019 	 */
2020 	err = dsl_dir_hold_obj(dp, dcp->dc_ddobj, dcp->dc_ddname, FTAG, &dd);
2021 	if (err != 0)
2022 		goto out;
2023 
2024 	/* Don't visit hidden ($MOS & $ORIGIN) objsets. */
2025 	if (dd->dd_myname[0] == '$') {
2026 		dsl_dir_rele(dd, FTAG);
2027 		goto out;
2028 	}
2029 
2030 	thisobj = dsl_dir_phys(dd)->dd_head_dataset_obj;
2031 	attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
2032 
2033 	/*
2034 	 * Iterate over all children.
2035 	 */
2036 	if (dcp->dc_flags & DS_FIND_CHILDREN) {
2037 		for (zap_cursor_init(&zc, dp->dp_meta_objset,
2038 		    dsl_dir_phys(dd)->dd_child_dir_zapobj);
2039 		    zap_cursor_retrieve(&zc, attr) == 0;
2040 		    (void) zap_cursor_advance(&zc)) {
2041 			ASSERT3U(attr->za_integer_length, ==,
2042 			    sizeof (uint64_t));
2043 			ASSERT3U(attr->za_num_integers, ==, 1);
2044 
2045 			dmu_objset_find_ctx_t *child_dcp =
2046 			    kmem_alloc(sizeof (*child_dcp), KM_SLEEP);
2047 			*child_dcp = *dcp;
2048 			child_dcp->dc_ddobj = attr->za_first_integer;
2049 			child_dcp->dc_ddname = spa_strdup(attr->za_name);
2050 			if (dcp->dc_tq != NULL)
2051 				(void) taskq_dispatch(dcp->dc_tq,
2052 				    dmu_objset_find_dp_cb, child_dcp, TQ_SLEEP);
2053 			else
2054 				dmu_objset_find_dp_impl(child_dcp);
2055 		}
2056 		zap_cursor_fini(&zc);
2057 	}
2058 
2059 	/*
2060 	 * Iterate over all snapshots.
2061 	 */
2062 	if (dcp->dc_flags & DS_FIND_SNAPSHOTS) {
2063 		dsl_dataset_t *ds;
2064 		err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
2065 
2066 		if (err == 0) {
2067 			uint64_t snapobj;
2068 
2069 			snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
2070 			dsl_dataset_rele(ds, FTAG);
2071 
2072 			for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
2073 			    zap_cursor_retrieve(&zc, attr) == 0;
2074 			    (void) zap_cursor_advance(&zc)) {
2075 				ASSERT3U(attr->za_integer_length, ==,
2076 				    sizeof (uint64_t));
2077 				ASSERT3U(attr->za_num_integers, ==, 1);
2078 
2079 				err = dsl_dataset_hold_obj(dp,
2080 				    attr->za_first_integer, FTAG, &ds);
2081 				if (err != 0)
2082 					break;
2083 				err = dcp->dc_func(dp, ds, dcp->dc_arg);
2084 				dsl_dataset_rele(ds, FTAG);
2085 				if (err != 0)
2086 					break;
2087 			}
2088 			zap_cursor_fini(&zc);
2089 		}
2090 	}
2091 
2092 	kmem_free(attr, sizeof (zap_attribute_t));
2093 
2094 	if (err != 0) {
2095 		dsl_dir_rele(dd, FTAG);
2096 		goto out;
2097 	}
2098 
2099 	/*
2100 	 * Apply to self.
2101 	 */
2102 	err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
2103 
2104 	/*
2105 	 * Note: we hold the dir while calling dsl_dataset_hold_obj() so
2106 	 * that the dir will remain cached, and we won't have to re-instantiate
2107 	 * it (which could be expensive due to finding its name via
2108 	 * zap_value_search()).
2109 	 */
2110 	dsl_dir_rele(dd, FTAG);
2111 	if (err != 0)
2112 		goto out;
2113 	err = dcp->dc_func(dp, ds, dcp->dc_arg);
2114 	dsl_dataset_rele(ds, FTAG);
2115 
2116 out:
2117 	if (err != 0) {
2118 		mutex_enter(dcp->dc_error_lock);
2119 		/* only keep first error */
2120 		if (*dcp->dc_error == 0)
2121 			*dcp->dc_error = err;
2122 		mutex_exit(dcp->dc_error_lock);
2123 	}
2124 
2125 	if (dcp->dc_ddname != NULL)
2126 		spa_strfree(dcp->dc_ddname);
2127 	kmem_free(dcp, sizeof (*dcp));
2128 }
2129 
2130 static void
2131 dmu_objset_find_dp_cb(void *arg)
2132 {
2133 	dmu_objset_find_ctx_t *dcp = arg;
2134 	dsl_pool_t *dp = dcp->dc_dp;
2135 
2136 	/*
2137 	 * We need to get a pool_config_lock here, as there are several
2138 	 * asssert(pool_config_held) down the stack. Getting a lock via
2139 	 * dsl_pool_config_enter is risky, as it might be stalled by a
2140 	 * pending writer. This would deadlock, as the write lock can
2141 	 * only be granted when our parent thread gives up the lock.
2142 	 * The _prio interface gives us priority over a pending writer.
2143 	 */
2144 	dsl_pool_config_enter_prio(dp, FTAG);
2145 
2146 	dmu_objset_find_dp_impl(dcp);
2147 
2148 	dsl_pool_config_exit(dp, FTAG);
2149 }
2150 
2151 /*
2152  * Find objsets under and including ddobj, call func(ds) on each.
2153  * The order for the enumeration is completely undefined.
2154  * func is called with dsl_pool_config held.
2155  */
2156 int
2157 dmu_objset_find_dp(dsl_pool_t *dp, uint64_t ddobj,
2158     int func(dsl_pool_t *, dsl_dataset_t *, void *), void *arg, int flags)
2159 {
2160 	int error = 0;
2161 	taskq_t *tq = NULL;
2162 	int ntasks;
2163 	dmu_objset_find_ctx_t *dcp;
2164 	kmutex_t err_lock;
2165 
2166 	mutex_init(&err_lock, NULL, MUTEX_DEFAULT, NULL);
2167 	dcp = kmem_alloc(sizeof (*dcp), KM_SLEEP);
2168 	dcp->dc_tq = NULL;
2169 	dcp->dc_dp = dp;
2170 	dcp->dc_ddobj = ddobj;
2171 	dcp->dc_ddname = NULL;
2172 	dcp->dc_func = func;
2173 	dcp->dc_arg = arg;
2174 	dcp->dc_flags = flags;
2175 	dcp->dc_error_lock = &err_lock;
2176 	dcp->dc_error = &error;
2177 
2178 	if ((flags & DS_FIND_SERIALIZE) || dsl_pool_config_held_writer(dp)) {
2179 		/*
2180 		 * In case a write lock is held we can't make use of
2181 		 * parallelism, as down the stack of the worker threads
2182 		 * the lock is asserted via dsl_pool_config_held.
2183 		 * In case of a read lock this is solved by getting a read
2184 		 * lock in each worker thread, which isn't possible in case
2185 		 * of a writer lock. So we fall back to the synchronous path
2186 		 * here.
2187 		 * In the future it might be possible to get some magic into
2188 		 * dsl_pool_config_held in a way that it returns true for
2189 		 * the worker threads so that a single lock held from this
2190 		 * thread suffices. For now, stay single threaded.
2191 		 */
2192 		dmu_objset_find_dp_impl(dcp);
2193 		mutex_destroy(&err_lock);
2194 
2195 		return (error);
2196 	}
2197 
2198 	ntasks = dmu_find_threads;
2199 	if (ntasks == 0)
2200 		ntasks = vdev_count_leaves(dp->dp_spa) * 4;
2201 	tq = taskq_create("dmu_objset_find", ntasks, minclsyspri, ntasks,
2202 	    INT_MAX, 0);
2203 	if (tq == NULL) {
2204 		kmem_free(dcp, sizeof (*dcp));
2205 		mutex_destroy(&err_lock);
2206 
2207 		return (SET_ERROR(ENOMEM));
2208 	}
2209 	dcp->dc_tq = tq;
2210 
2211 	/* dcp will be freed by task */
2212 	(void) taskq_dispatch(tq, dmu_objset_find_dp_cb, dcp, TQ_SLEEP);
2213 
2214 	/*
2215 	 * PORTING: this code relies on the property of taskq_wait to wait
2216 	 * until no more tasks are queued and no more tasks are active. As
2217 	 * we always queue new tasks from within other tasks, task_wait
2218 	 * reliably waits for the full recursion to finish, even though we
2219 	 * enqueue new tasks after taskq_wait has been called.
2220 	 * On platforms other than illumos, taskq_wait may not have this
2221 	 * property.
2222 	 */
2223 	taskq_wait(tq);
2224 	taskq_destroy(tq);
2225 	mutex_destroy(&err_lock);
2226 
2227 	return (error);
2228 }
2229 
2230 /*
2231  * Find all objsets under name, and for each, call 'func(child_name, arg)'.
2232  * The dp_config_rwlock must not be held when this is called, and it
2233  * will not be held when the callback is called.
2234  * Therefore this function should only be used when the pool is not changing
2235  * (e.g. in syncing context), or the callback can deal with the possible races.
2236  */
2237 static int
2238 dmu_objset_find_impl(spa_t *spa, const char *name,
2239     int func(const char *, void *), void *arg, int flags)
2240 {
2241 	dsl_dir_t *dd;
2242 	dsl_pool_t *dp = spa_get_dsl(spa);
2243 	dsl_dataset_t *ds;
2244 	zap_cursor_t zc;
2245 	zap_attribute_t *attr;
2246 	char *child;
2247 	uint64_t thisobj;
2248 	int err;
2249 
2250 	dsl_pool_config_enter(dp, FTAG);
2251 
2252 	err = dsl_dir_hold(dp, name, FTAG, &dd, NULL);
2253 	if (err != 0) {
2254 		dsl_pool_config_exit(dp, FTAG);
2255 		return (err);
2256 	}
2257 
2258 	/* Don't visit hidden ($MOS & $ORIGIN) objsets. */
2259 	if (dd->dd_myname[0] == '$') {
2260 		dsl_dir_rele(dd, FTAG);
2261 		dsl_pool_config_exit(dp, FTAG);
2262 		return (0);
2263 	}
2264 
2265 	thisobj = dsl_dir_phys(dd)->dd_head_dataset_obj;
2266 	attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
2267 
2268 	/*
2269 	 * Iterate over all children.
2270 	 */
2271 	if (flags & DS_FIND_CHILDREN) {
2272 		for (zap_cursor_init(&zc, dp->dp_meta_objset,
2273 		    dsl_dir_phys(dd)->dd_child_dir_zapobj);
2274 		    zap_cursor_retrieve(&zc, attr) == 0;
2275 		    (void) zap_cursor_advance(&zc)) {
2276 			ASSERT3U(attr->za_integer_length, ==,
2277 			    sizeof (uint64_t));
2278 			ASSERT3U(attr->za_num_integers, ==, 1);
2279 
2280 			child = kmem_asprintf("%s/%s", name, attr->za_name);
2281 			dsl_pool_config_exit(dp, FTAG);
2282 			err = dmu_objset_find_impl(spa, child,
2283 			    func, arg, flags);
2284 			dsl_pool_config_enter(dp, FTAG);
2285 			strfree(child);
2286 			if (err != 0)
2287 				break;
2288 		}
2289 		zap_cursor_fini(&zc);
2290 
2291 		if (err != 0) {
2292 			dsl_dir_rele(dd, FTAG);
2293 			dsl_pool_config_exit(dp, FTAG);
2294 			kmem_free(attr, sizeof (zap_attribute_t));
2295 			return (err);
2296 		}
2297 	}
2298 
2299 	/*
2300 	 * Iterate over all snapshots.
2301 	 */
2302 	if (flags & DS_FIND_SNAPSHOTS) {
2303 		err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
2304 
2305 		if (err == 0) {
2306 			uint64_t snapobj;
2307 
2308 			snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
2309 			dsl_dataset_rele(ds, FTAG);
2310 
2311 			for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
2312 			    zap_cursor_retrieve(&zc, attr) == 0;
2313 			    (void) zap_cursor_advance(&zc)) {
2314 				ASSERT3U(attr->za_integer_length, ==,
2315 				    sizeof (uint64_t));
2316 				ASSERT3U(attr->za_num_integers, ==, 1);
2317 
2318 				child = kmem_asprintf("%s@%s",
2319 				    name, attr->za_name);
2320 				dsl_pool_config_exit(dp, FTAG);
2321 				err = func(child, arg);
2322 				dsl_pool_config_enter(dp, FTAG);
2323 				strfree(child);
2324 				if (err != 0)
2325 					break;
2326 			}
2327 			zap_cursor_fini(&zc);
2328 		}
2329 	}
2330 
2331 	dsl_dir_rele(dd, FTAG);
2332 	kmem_free(attr, sizeof (zap_attribute_t));
2333 	dsl_pool_config_exit(dp, FTAG);
2334 
2335 	if (err != 0)
2336 		return (err);
2337 
2338 	/* Apply to self. */
2339 	return (func(name, arg));
2340 }
2341 
2342 /*
2343  * See comment above dmu_objset_find_impl().
2344  */
2345 int
2346 dmu_objset_find(char *name, int func(const char *, void *), void *arg,
2347     int flags)
2348 {
2349 	spa_t *spa;
2350 	int error;
2351 
2352 	error = spa_open(name, &spa, FTAG);
2353 	if (error != 0)
2354 		return (error);
2355 	error = dmu_objset_find_impl(spa, name, func, arg, flags);
2356 	spa_close(spa, FTAG);
2357 	return (error);
2358 }
2359 
2360 void
2361 dmu_objset_set_user(objset_t *os, void *user_ptr)
2362 {
2363 	ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
2364 	os->os_user_ptr = user_ptr;
2365 }
2366 
2367 void *
2368 dmu_objset_get_user(objset_t *os)
2369 {
2370 	ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
2371 	return (os->os_user_ptr);
2372 }
2373 
2374 /*
2375  * Determine name of filesystem, given name of snapshot.
2376  * buf must be at least ZFS_MAX_DATASET_NAME_LEN bytes
2377  */
2378 int
2379 dmu_fsname(const char *snapname, char *buf)
2380 {
2381 	char *atp = strchr(snapname, '@');
2382 	if (atp == NULL)
2383 		return (SET_ERROR(EINVAL));
2384 	if (atp - snapname >= ZFS_MAX_DATASET_NAME_LEN)
2385 		return (SET_ERROR(ENAMETOOLONG));
2386 	(void) strlcpy(buf, snapname, atp - snapname + 1);
2387 	return (0);
2388 }
2389 
2390 /*
2391  * Call when we think we're going to write/free space in open context to track
2392  * the amount of dirty data in the open txg, which is also the amount
2393  * of memory that can not be evicted until this txg syncs.
2394  */
2395 void
2396 dmu_objset_willuse_space(objset_t *os, int64_t space, dmu_tx_t *tx)
2397 {
2398 	dsl_dataset_t *ds = os->os_dsl_dataset;
2399 	int64_t aspace = spa_get_worst_case_asize(os->os_spa, space);
2400 
2401 	if (ds != NULL) {
2402 		dsl_dir_willuse_space(ds->ds_dir, aspace, tx);
2403 		dsl_pool_dirty_space(dmu_tx_pool(tx), space, tx);
2404 	}
2405 }
2406