xref: /illumos-gate/usr/src/uts/common/fs/zfs/dmu_objset.c (revision aa02ea01)
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 		/*
1251 		 * If we are not doing useraccounting (os_synced_dnodes == NULL)
1252 		 * we are done with this dnode for this txg. Unset dn_dirty_txg
1253 		 * if later txgs aren't dirtying it so that future holders do
1254 		 * not get a stale value. Otherwise, we will do this in
1255 		 * userquota_updates_task() when processing has completely
1256 		 * finished for this txg.
1257 		 */
1258 		multilist_t *newlist = dn->dn_objset->os_synced_dnodes;
1259 		if (newlist != NULL) {
1260 			(void) dnode_add_ref(dn, newlist);
1261 			multilist_insert(newlist, dn);
1262 		} else {
1263 			mutex_enter(&dn->dn_mtx);
1264 			if (dn->dn_dirty_txg == tx->tx_txg)
1265 				dn->dn_dirty_txg = 0;
1266 			mutex_exit(&dn->dn_mtx);
1267 		}
1268 
1269 		dnode_sync(dn, tx);
1270 	}
1271 }
1272 
1273 /* ARGSUSED */
1274 static void
1275 dmu_objset_write_ready(zio_t *zio, arc_buf_t *abuf, void *arg)
1276 {
1277 	blkptr_t *bp = zio->io_bp;
1278 	objset_t *os = arg;
1279 	dnode_phys_t *dnp = &os->os_phys->os_meta_dnode;
1280 
1281 	ASSERT(!BP_IS_EMBEDDED(bp));
1282 	ASSERT3U(BP_GET_TYPE(bp), ==, DMU_OT_OBJSET);
1283 	ASSERT0(BP_GET_LEVEL(bp));
1284 
1285 	/*
1286 	 * Update rootbp fill count: it should be the number of objects
1287 	 * allocated in the object set (not counting the "special"
1288 	 * objects that are stored in the objset_phys_t -- the meta
1289 	 * dnode and user/group accounting objects).
1290 	 */
1291 	bp->blk_fill = 0;
1292 	for (int i = 0; i < dnp->dn_nblkptr; i++)
1293 		bp->blk_fill += BP_GET_FILL(&dnp->dn_blkptr[i]);
1294 	if (os->os_dsl_dataset != NULL)
1295 		rrw_enter(&os->os_dsl_dataset->ds_bp_rwlock, RW_WRITER, FTAG);
1296 	*os->os_rootbp = *bp;
1297 	if (os->os_dsl_dataset != NULL)
1298 		rrw_exit(&os->os_dsl_dataset->ds_bp_rwlock, FTAG);
1299 }
1300 
1301 /* ARGSUSED */
1302 static void
1303 dmu_objset_write_done(zio_t *zio, arc_buf_t *abuf, void *arg)
1304 {
1305 	blkptr_t *bp = zio->io_bp;
1306 	blkptr_t *bp_orig = &zio->io_bp_orig;
1307 	objset_t *os = arg;
1308 
1309 	if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
1310 		ASSERT(BP_EQUAL(bp, bp_orig));
1311 	} else {
1312 		dsl_dataset_t *ds = os->os_dsl_dataset;
1313 		dmu_tx_t *tx = os->os_synctx;
1314 
1315 		(void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
1316 		dsl_dataset_block_born(ds, bp, tx);
1317 	}
1318 	kmem_free(bp, sizeof (*bp));
1319 }
1320 
1321 typedef struct sync_dnodes_arg {
1322 	multilist_t *sda_list;
1323 	int sda_sublist_idx;
1324 	multilist_t *sda_newlist;
1325 	dmu_tx_t *sda_tx;
1326 } sync_dnodes_arg_t;
1327 
1328 static void
1329 sync_dnodes_task(void *arg)
1330 {
1331 	sync_dnodes_arg_t *sda = arg;
1332 
1333 	multilist_sublist_t *ms =
1334 	    multilist_sublist_lock(sda->sda_list, sda->sda_sublist_idx);
1335 
1336 	dmu_objset_sync_dnodes(ms, sda->sda_tx);
1337 
1338 	multilist_sublist_unlock(ms);
1339 
1340 	kmem_free(sda, sizeof (*sda));
1341 }
1342 
1343 
1344 /* called from dsl */
1345 void
1346 dmu_objset_sync(objset_t *os, zio_t *pio, dmu_tx_t *tx)
1347 {
1348 	int txgoff;
1349 	zbookmark_phys_t zb;
1350 	zio_prop_t zp;
1351 	zio_t *zio;
1352 	list_t *list;
1353 	dbuf_dirty_record_t *dr;
1354 	blkptr_t *blkptr_copy = kmem_alloc(sizeof (*os->os_rootbp), KM_SLEEP);
1355 	*blkptr_copy = *os->os_rootbp;
1356 
1357 	dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg);
1358 
1359 	ASSERT(dmu_tx_is_syncing(tx));
1360 	/* XXX the write_done callback should really give us the tx... */
1361 	os->os_synctx = tx;
1362 
1363 	if (os->os_dsl_dataset == NULL) {
1364 		/*
1365 		 * This is the MOS.  If we have upgraded,
1366 		 * spa_max_replication() could change, so reset
1367 		 * os_copies here.
1368 		 */
1369 		os->os_copies = spa_max_replication(os->os_spa);
1370 	}
1371 
1372 	/*
1373 	 * Create the root block IO
1374 	 */
1375 	SET_BOOKMARK(&zb, os->os_dsl_dataset ?
1376 	    os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
1377 	    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
1378 	arc_release(os->os_phys_buf, &os->os_phys_buf);
1379 
1380 	dmu_write_policy(os, NULL, 0, 0, &zp);
1381 
1382 	zio = arc_write(pio, os->os_spa, tx->tx_txg,
1383 	    blkptr_copy, os->os_phys_buf, DMU_OS_IS_L2CACHEABLE(os),
1384 	    &zp, dmu_objset_write_ready, NULL, NULL, dmu_objset_write_done,
1385 	    os, ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
1386 
1387 	/*
1388 	 * Sync special dnodes - the parent IO for the sync is the root block
1389 	 */
1390 	DMU_META_DNODE(os)->dn_zio = zio;
1391 	dnode_sync(DMU_META_DNODE(os), tx);
1392 
1393 	os->os_phys->os_flags = os->os_flags;
1394 
1395 	if (DMU_USERUSED_DNODE(os) &&
1396 	    DMU_USERUSED_DNODE(os)->dn_type != DMU_OT_NONE) {
1397 		DMU_USERUSED_DNODE(os)->dn_zio = zio;
1398 		dnode_sync(DMU_USERUSED_DNODE(os), tx);
1399 		DMU_GROUPUSED_DNODE(os)->dn_zio = zio;
1400 		dnode_sync(DMU_GROUPUSED_DNODE(os), tx);
1401 	}
1402 
1403 	txgoff = tx->tx_txg & TXG_MASK;
1404 
1405 	if (dmu_objset_userused_enabled(os)) {
1406 		/*
1407 		 * We must create the list here because it uses the
1408 		 * dn_dirty_link[] of this txg.  But it may already
1409 		 * exist because we call dsl_dataset_sync() twice per txg.
1410 		 */
1411 		if (os->os_synced_dnodes == NULL) {
1412 			os->os_synced_dnodes =
1413 			    multilist_create(sizeof (dnode_t),
1414 			    offsetof(dnode_t, dn_dirty_link[txgoff]),
1415 			    dnode_multilist_index_func);
1416 		} else {
1417 			ASSERT3U(os->os_synced_dnodes->ml_offset, ==,
1418 			    offsetof(dnode_t, dn_dirty_link[txgoff]));
1419 		}
1420 	}
1421 
1422 	for (int i = 0;
1423 	    i < multilist_get_num_sublists(os->os_dirty_dnodes[txgoff]); i++) {
1424 		sync_dnodes_arg_t *sda = kmem_alloc(sizeof (*sda), KM_SLEEP);
1425 		sda->sda_list = os->os_dirty_dnodes[txgoff];
1426 		sda->sda_sublist_idx = i;
1427 		sda->sda_tx = tx;
1428 		(void) taskq_dispatch(dmu_objset_pool(os)->dp_sync_taskq,
1429 		    sync_dnodes_task, sda, 0);
1430 		/* callback frees sda */
1431 	}
1432 	taskq_wait(dmu_objset_pool(os)->dp_sync_taskq);
1433 
1434 	list = &DMU_META_DNODE(os)->dn_dirty_records[txgoff];
1435 	while ((dr = list_head(list)) != NULL) {
1436 		ASSERT0(dr->dr_dbuf->db_level);
1437 		list_remove(list, dr);
1438 		if (dr->dr_zio)
1439 			zio_nowait(dr->dr_zio);
1440 	}
1441 
1442 	/* Enable dnode backfill if enough objects have been freed. */
1443 	if (os->os_freed_dnodes >= dmu_rescan_dnode_threshold) {
1444 		os->os_rescan_dnodes = B_TRUE;
1445 		os->os_freed_dnodes = 0;
1446 	}
1447 
1448 	/*
1449 	 * Free intent log blocks up to this tx.
1450 	 */
1451 	zil_sync(os->os_zil, tx);
1452 	os->os_phys->os_zil_header = os->os_zil_header;
1453 	zio_nowait(zio);
1454 }
1455 
1456 boolean_t
1457 dmu_objset_is_dirty(objset_t *os, uint64_t txg)
1458 {
1459 	return (!multilist_is_empty(os->os_dirty_dnodes[txg & TXG_MASK]));
1460 }
1461 
1462 static objset_used_cb_t *used_cbs[DMU_OST_NUMTYPES];
1463 
1464 void
1465 dmu_objset_register_type(dmu_objset_type_t ost, objset_used_cb_t *cb)
1466 {
1467 	used_cbs[ost] = cb;
1468 }
1469 
1470 boolean_t
1471 dmu_objset_userused_enabled(objset_t *os)
1472 {
1473 	return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE &&
1474 	    used_cbs[os->os_phys->os_type] != NULL &&
1475 	    DMU_USERUSED_DNODE(os) != NULL);
1476 }
1477 
1478 typedef struct userquota_node {
1479 	uint64_t uqn_id;
1480 	int64_t uqn_delta;
1481 	avl_node_t uqn_node;
1482 } userquota_node_t;
1483 
1484 typedef struct userquota_cache {
1485 	avl_tree_t uqc_user_deltas;
1486 	avl_tree_t uqc_group_deltas;
1487 } userquota_cache_t;
1488 
1489 static int
1490 userquota_compare(const void *l, const void *r)
1491 {
1492 	const userquota_node_t *luqn = l;
1493 	const userquota_node_t *ruqn = r;
1494 
1495 	if (luqn->uqn_id < ruqn->uqn_id)
1496 		return (-1);
1497 	if (luqn->uqn_id > ruqn->uqn_id)
1498 		return (1);
1499 	return (0);
1500 }
1501 
1502 static void
1503 do_userquota_cacheflush(objset_t *os, userquota_cache_t *cache, dmu_tx_t *tx)
1504 {
1505 	void *cookie;
1506 	userquota_node_t *uqn;
1507 
1508 	ASSERT(dmu_tx_is_syncing(tx));
1509 
1510 	cookie = NULL;
1511 	while ((uqn = avl_destroy_nodes(&cache->uqc_user_deltas,
1512 	    &cookie)) != NULL) {
1513 		/*
1514 		 * os_userused_lock protects against concurrent calls to
1515 		 * zap_increment_int().  It's needed because zap_increment_int()
1516 		 * is not thread-safe (i.e. not atomic).
1517 		 */
1518 		mutex_enter(&os->os_userused_lock);
1519 		VERIFY0(zap_increment_int(os, DMU_USERUSED_OBJECT,
1520 		    uqn->uqn_id, uqn->uqn_delta, tx));
1521 		mutex_exit(&os->os_userused_lock);
1522 		kmem_free(uqn, sizeof (*uqn));
1523 	}
1524 	avl_destroy(&cache->uqc_user_deltas);
1525 
1526 	cookie = NULL;
1527 	while ((uqn = avl_destroy_nodes(&cache->uqc_group_deltas,
1528 	    &cookie)) != NULL) {
1529 		mutex_enter(&os->os_userused_lock);
1530 		VERIFY0(zap_increment_int(os, DMU_GROUPUSED_OBJECT,
1531 		    uqn->uqn_id, uqn->uqn_delta, tx));
1532 		mutex_exit(&os->os_userused_lock);
1533 		kmem_free(uqn, sizeof (*uqn));
1534 	}
1535 	avl_destroy(&cache->uqc_group_deltas);
1536 }
1537 
1538 static void
1539 userquota_update_cache(avl_tree_t *avl, uint64_t id, int64_t delta)
1540 {
1541 	userquota_node_t search = { .uqn_id = id };
1542 	avl_index_t idx;
1543 
1544 	userquota_node_t *uqn = avl_find(avl, &search, &idx);
1545 	if (uqn == NULL) {
1546 		uqn = kmem_zalloc(sizeof (*uqn), KM_SLEEP);
1547 		uqn->uqn_id = id;
1548 		avl_insert(avl, uqn, idx);
1549 	}
1550 	uqn->uqn_delta += delta;
1551 }
1552 
1553 static void
1554 do_userquota_update(userquota_cache_t *cache, uint64_t used, uint64_t flags,
1555     uint64_t user, uint64_t group, boolean_t subtract)
1556 {
1557 	if ((flags & DNODE_FLAG_USERUSED_ACCOUNTED)) {
1558 		int64_t delta = DNODE_MIN_SIZE + used;
1559 		if (subtract)
1560 			delta = -delta;
1561 
1562 		userquota_update_cache(&cache->uqc_user_deltas, user, delta);
1563 		userquota_update_cache(&cache->uqc_group_deltas, group, delta);
1564 	}
1565 }
1566 
1567 typedef struct userquota_updates_arg {
1568 	objset_t *uua_os;
1569 	int uua_sublist_idx;
1570 	dmu_tx_t *uua_tx;
1571 } userquota_updates_arg_t;
1572 
1573 static void
1574 userquota_updates_task(void *arg)
1575 {
1576 	userquota_updates_arg_t *uua = arg;
1577 	objset_t *os = uua->uua_os;
1578 	dmu_tx_t *tx = uua->uua_tx;
1579 	dnode_t *dn;
1580 	userquota_cache_t cache = { 0 };
1581 
1582 	multilist_sublist_t *list =
1583 	    multilist_sublist_lock(os->os_synced_dnodes, uua->uua_sublist_idx);
1584 
1585 	ASSERT(multilist_sublist_head(list) == NULL ||
1586 	    dmu_objset_userused_enabled(os));
1587 	avl_create(&cache.uqc_user_deltas, userquota_compare,
1588 	    sizeof (userquota_node_t), offsetof(userquota_node_t, uqn_node));
1589 	avl_create(&cache.uqc_group_deltas, userquota_compare,
1590 	    sizeof (userquota_node_t), offsetof(userquota_node_t, uqn_node));
1591 
1592 	while ((dn = multilist_sublist_head(list)) != NULL) {
1593 		int flags;
1594 		ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object));
1595 		ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE ||
1596 		    dn->dn_phys->dn_flags &
1597 		    DNODE_FLAG_USERUSED_ACCOUNTED);
1598 
1599 		flags = dn->dn_id_flags;
1600 		ASSERT(flags);
1601 		if (flags & DN_ID_OLD_EXIST)  {
1602 			do_userquota_update(&cache,
1603 			    dn->dn_oldused, dn->dn_oldflags,
1604 			    dn->dn_olduid, dn->dn_oldgid, B_TRUE);
1605 		}
1606 		if (flags & DN_ID_NEW_EXIST) {
1607 			do_userquota_update(&cache,
1608 			    DN_USED_BYTES(dn->dn_phys),
1609 			    dn->dn_phys->dn_flags,  dn->dn_newuid,
1610 			    dn->dn_newgid, B_FALSE);
1611 		}
1612 
1613 		mutex_enter(&dn->dn_mtx);
1614 		dn->dn_oldused = 0;
1615 		dn->dn_oldflags = 0;
1616 		if (dn->dn_id_flags & DN_ID_NEW_EXIST) {
1617 			dn->dn_olduid = dn->dn_newuid;
1618 			dn->dn_oldgid = dn->dn_newgid;
1619 			dn->dn_id_flags |= DN_ID_OLD_EXIST;
1620 			if (dn->dn_bonuslen == 0)
1621 				dn->dn_id_flags |= DN_ID_CHKED_SPILL;
1622 			else
1623 				dn->dn_id_flags |= DN_ID_CHKED_BONUS;
1624 		}
1625 		dn->dn_id_flags &= ~(DN_ID_NEW_EXIST);
1626 		if (dn->dn_dirty_txg == spa_syncing_txg(os->os_spa))
1627 			dn->dn_dirty_txg = 0;
1628 		mutex_exit(&dn->dn_mtx);
1629 
1630 		multilist_sublist_remove(list, dn);
1631 		dnode_rele(dn, os->os_synced_dnodes);
1632 	}
1633 	do_userquota_cacheflush(os, &cache, tx);
1634 	multilist_sublist_unlock(list);
1635 	kmem_free(uua, sizeof (*uua));
1636 }
1637 
1638 void
1639 dmu_objset_do_userquota_updates(objset_t *os, dmu_tx_t *tx)
1640 {
1641 	if (!dmu_objset_userused_enabled(os))
1642 		return;
1643 
1644 	/* Allocate the user/groupused objects if necessary. */
1645 	if (DMU_USERUSED_DNODE(os)->dn_type == DMU_OT_NONE) {
1646 		VERIFY0(zap_create_claim(os,
1647 		    DMU_USERUSED_OBJECT,
1648 		    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
1649 		VERIFY0(zap_create_claim(os,
1650 		    DMU_GROUPUSED_OBJECT,
1651 		    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
1652 	}
1653 
1654 	for (int i = 0;
1655 	    i < multilist_get_num_sublists(os->os_synced_dnodes); i++) {
1656 		userquota_updates_arg_t *uua =
1657 		    kmem_alloc(sizeof (*uua), KM_SLEEP);
1658 		uua->uua_os = os;
1659 		uua->uua_sublist_idx = i;
1660 		uua->uua_tx = tx;
1661 		/* note: caller does taskq_wait() */
1662 		(void) taskq_dispatch(dmu_objset_pool(os)->dp_sync_taskq,
1663 		    userquota_updates_task, uua, 0);
1664 		/* callback frees uua */
1665 	}
1666 }
1667 
1668 /*
1669  * Returns a pointer to data to find uid/gid from
1670  *
1671  * If a dirty record for transaction group that is syncing can't
1672  * be found then NULL is returned.  In the NULL case it is assumed
1673  * the uid/gid aren't changing.
1674  */
1675 static void *
1676 dmu_objset_userquota_find_data(dmu_buf_impl_t *db, dmu_tx_t *tx)
1677 {
1678 	dbuf_dirty_record_t *dr, **drp;
1679 	void *data;
1680 
1681 	if (db->db_dirtycnt == 0)
1682 		return (db->db.db_data);  /* Nothing is changing */
1683 
1684 	for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next)
1685 		if (dr->dr_txg == tx->tx_txg)
1686 			break;
1687 
1688 	if (dr == NULL) {
1689 		data = NULL;
1690 	} else {
1691 		dnode_t *dn;
1692 
1693 		DB_DNODE_ENTER(dr->dr_dbuf);
1694 		dn = DB_DNODE(dr->dr_dbuf);
1695 
1696 		if (dn->dn_bonuslen == 0 &&
1697 		    dr->dr_dbuf->db_blkid == DMU_SPILL_BLKID)
1698 			data = dr->dt.dl.dr_data->b_data;
1699 		else
1700 			data = dr->dt.dl.dr_data;
1701 
1702 		DB_DNODE_EXIT(dr->dr_dbuf);
1703 	}
1704 
1705 	return (data);
1706 }
1707 
1708 void
1709 dmu_objset_userquota_get_ids(dnode_t *dn, boolean_t before, dmu_tx_t *tx)
1710 {
1711 	objset_t *os = dn->dn_objset;
1712 	void *data = NULL;
1713 	dmu_buf_impl_t *db = NULL;
1714 	uint64_t *user = NULL;
1715 	uint64_t *group = NULL;
1716 	int flags = dn->dn_id_flags;
1717 	int error;
1718 	boolean_t have_spill = B_FALSE;
1719 
1720 	if (!dmu_objset_userused_enabled(dn->dn_objset))
1721 		return;
1722 
1723 	if (before && (flags & (DN_ID_CHKED_BONUS|DN_ID_OLD_EXIST|
1724 	    DN_ID_CHKED_SPILL)))
1725 		return;
1726 
1727 	if (before && dn->dn_bonuslen != 0)
1728 		data = DN_BONUS(dn->dn_phys);
1729 	else if (!before && dn->dn_bonuslen != 0) {
1730 		if (dn->dn_bonus) {
1731 			db = dn->dn_bonus;
1732 			mutex_enter(&db->db_mtx);
1733 			data = dmu_objset_userquota_find_data(db, tx);
1734 		} else {
1735 			data = DN_BONUS(dn->dn_phys);
1736 		}
1737 	} else if (dn->dn_bonuslen == 0 && dn->dn_bonustype == DMU_OT_SA) {
1738 			int rf = 0;
1739 
1740 			if (RW_WRITE_HELD(&dn->dn_struct_rwlock))
1741 				rf |= DB_RF_HAVESTRUCT;
1742 			error = dmu_spill_hold_by_dnode(dn,
1743 			    rf | DB_RF_MUST_SUCCEED,
1744 			    FTAG, (dmu_buf_t **)&db);
1745 			ASSERT(error == 0);
1746 			mutex_enter(&db->db_mtx);
1747 			data = (before) ? db->db.db_data :
1748 			    dmu_objset_userquota_find_data(db, tx);
1749 			have_spill = B_TRUE;
1750 	} else {
1751 		mutex_enter(&dn->dn_mtx);
1752 		dn->dn_id_flags |= DN_ID_CHKED_BONUS;
1753 		mutex_exit(&dn->dn_mtx);
1754 		return;
1755 	}
1756 
1757 	if (before) {
1758 		ASSERT(data);
1759 		user = &dn->dn_olduid;
1760 		group = &dn->dn_oldgid;
1761 	} else if (data) {
1762 		user = &dn->dn_newuid;
1763 		group = &dn->dn_newgid;
1764 	}
1765 
1766 	/*
1767 	 * Must always call the callback in case the object
1768 	 * type has changed and that type isn't an object type to track
1769 	 */
1770 	error = used_cbs[os->os_phys->os_type](dn->dn_bonustype, data,
1771 	    user, group);
1772 
1773 	/*
1774 	 * Preserve existing uid/gid when the callback can't determine
1775 	 * what the new uid/gid are and the callback returned EEXIST.
1776 	 * The EEXIST error tells us to just use the existing uid/gid.
1777 	 * If we don't know what the old values are then just assign
1778 	 * them to 0, since that is a new file  being created.
1779 	 */
1780 	if (!before && data == NULL && error == EEXIST) {
1781 		if (flags & DN_ID_OLD_EXIST) {
1782 			dn->dn_newuid = dn->dn_olduid;
1783 			dn->dn_newgid = dn->dn_oldgid;
1784 		} else {
1785 			dn->dn_newuid = 0;
1786 			dn->dn_newgid = 0;
1787 		}
1788 		error = 0;
1789 	}
1790 
1791 	if (db)
1792 		mutex_exit(&db->db_mtx);
1793 
1794 	mutex_enter(&dn->dn_mtx);
1795 	if (error == 0 && before)
1796 		dn->dn_id_flags |= DN_ID_OLD_EXIST;
1797 	if (error == 0 && !before)
1798 		dn->dn_id_flags |= DN_ID_NEW_EXIST;
1799 
1800 	if (have_spill) {
1801 		dn->dn_id_flags |= DN_ID_CHKED_SPILL;
1802 	} else {
1803 		dn->dn_id_flags |= DN_ID_CHKED_BONUS;
1804 	}
1805 	mutex_exit(&dn->dn_mtx);
1806 	if (have_spill)
1807 		dmu_buf_rele((dmu_buf_t *)db, FTAG);
1808 }
1809 
1810 boolean_t
1811 dmu_objset_userspace_present(objset_t *os)
1812 {
1813 	return (os->os_phys->os_flags &
1814 	    OBJSET_FLAG_USERACCOUNTING_COMPLETE);
1815 }
1816 
1817 int
1818 dmu_objset_userspace_upgrade(objset_t *os)
1819 {
1820 	uint64_t obj;
1821 	int err = 0;
1822 
1823 	if (dmu_objset_userspace_present(os))
1824 		return (0);
1825 	if (!dmu_objset_userused_enabled(os))
1826 		return (SET_ERROR(ENOTSUP));
1827 	if (dmu_objset_is_snapshot(os))
1828 		return (SET_ERROR(EINVAL));
1829 
1830 	/*
1831 	 * We simply need to mark every object dirty, so that it will be
1832 	 * synced out and now accounted.  If this is called
1833 	 * concurrently, or if we already did some work before crashing,
1834 	 * that's fine, since we track each object's accounted state
1835 	 * independently.
1836 	 */
1837 
1838 	for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
1839 		dmu_tx_t *tx;
1840 		dmu_buf_t *db;
1841 		int objerr;
1842 
1843 		if (issig(JUSTLOOKING) && issig(FORREAL))
1844 			return (SET_ERROR(EINTR));
1845 
1846 		objerr = dmu_bonus_hold(os, obj, FTAG, &db);
1847 		if (objerr != 0)
1848 			continue;
1849 		tx = dmu_tx_create(os);
1850 		dmu_tx_hold_bonus(tx, obj);
1851 		objerr = dmu_tx_assign(tx, TXG_WAIT);
1852 		if (objerr != 0) {
1853 			dmu_tx_abort(tx);
1854 			continue;
1855 		}
1856 		dmu_buf_will_dirty(db, tx);
1857 		dmu_buf_rele(db, FTAG);
1858 		dmu_tx_commit(tx);
1859 	}
1860 
1861 	os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
1862 	txg_wait_synced(dmu_objset_pool(os), 0);
1863 	return (0);
1864 }
1865 
1866 void
1867 dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp,
1868     uint64_t *usedobjsp, uint64_t *availobjsp)
1869 {
1870 	dsl_dataset_space(os->os_dsl_dataset, refdbytesp, availbytesp,
1871 	    usedobjsp, availobjsp);
1872 }
1873 
1874 uint64_t
1875 dmu_objset_fsid_guid(objset_t *os)
1876 {
1877 	return (dsl_dataset_fsid_guid(os->os_dsl_dataset));
1878 }
1879 
1880 void
1881 dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat)
1882 {
1883 	stat->dds_type = os->os_phys->os_type;
1884 	if (os->os_dsl_dataset)
1885 		dsl_dataset_fast_stat(os->os_dsl_dataset, stat);
1886 }
1887 
1888 void
1889 dmu_objset_stats(objset_t *os, nvlist_t *nv)
1890 {
1891 	ASSERT(os->os_dsl_dataset ||
1892 	    os->os_phys->os_type == DMU_OST_META);
1893 
1894 	if (os->os_dsl_dataset != NULL)
1895 		dsl_dataset_stats(os->os_dsl_dataset, nv);
1896 
1897 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE,
1898 	    os->os_phys->os_type);
1899 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERACCOUNTING,
1900 	    dmu_objset_userspace_present(os));
1901 }
1902 
1903 int
1904 dmu_objset_is_snapshot(objset_t *os)
1905 {
1906 	if (os->os_dsl_dataset != NULL)
1907 		return (os->os_dsl_dataset->ds_is_snapshot);
1908 	else
1909 		return (B_FALSE);
1910 }
1911 
1912 int
1913 dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen,
1914     boolean_t *conflict)
1915 {
1916 	dsl_dataset_t *ds = os->os_dsl_dataset;
1917 	uint64_t ignored;
1918 
1919 	if (dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0)
1920 		return (SET_ERROR(ENOENT));
1921 
1922 	return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset,
1923 	    dsl_dataset_phys(ds)->ds_snapnames_zapobj, name, 8, 1, &ignored,
1924 	    MT_NORMALIZE, real, maxlen, conflict));
1925 }
1926 
1927 int
1928 dmu_snapshot_list_next(objset_t *os, int namelen, char *name,
1929     uint64_t *idp, uint64_t *offp, boolean_t *case_conflict)
1930 {
1931 	dsl_dataset_t *ds = os->os_dsl_dataset;
1932 	zap_cursor_t cursor;
1933 	zap_attribute_t attr;
1934 
1935 	ASSERT(dsl_pool_config_held(dmu_objset_pool(os)));
1936 
1937 	if (dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0)
1938 		return (SET_ERROR(ENOENT));
1939 
1940 	zap_cursor_init_serialized(&cursor,
1941 	    ds->ds_dir->dd_pool->dp_meta_objset,
1942 	    dsl_dataset_phys(ds)->ds_snapnames_zapobj, *offp);
1943 
1944 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
1945 		zap_cursor_fini(&cursor);
1946 		return (SET_ERROR(ENOENT));
1947 	}
1948 
1949 	if (strlen(attr.za_name) + 1 > namelen) {
1950 		zap_cursor_fini(&cursor);
1951 		return (SET_ERROR(ENAMETOOLONG));
1952 	}
1953 
1954 	(void) strcpy(name, attr.za_name);
1955 	if (idp)
1956 		*idp = attr.za_first_integer;
1957 	if (case_conflict)
1958 		*case_conflict = attr.za_normalization_conflict;
1959 	zap_cursor_advance(&cursor);
1960 	*offp = zap_cursor_serialize(&cursor);
1961 	zap_cursor_fini(&cursor);
1962 
1963 	return (0);
1964 }
1965 
1966 int
1967 dmu_dir_list_next(objset_t *os, int namelen, char *name,
1968     uint64_t *idp, uint64_t *offp)
1969 {
1970 	dsl_dir_t *dd = os->os_dsl_dataset->ds_dir;
1971 	zap_cursor_t cursor;
1972 	zap_attribute_t attr;
1973 
1974 	/* there is no next dir on a snapshot! */
1975 	if (os->os_dsl_dataset->ds_object !=
1976 	    dsl_dir_phys(dd)->dd_head_dataset_obj)
1977 		return (SET_ERROR(ENOENT));
1978 
1979 	zap_cursor_init_serialized(&cursor,
1980 	    dd->dd_pool->dp_meta_objset,
1981 	    dsl_dir_phys(dd)->dd_child_dir_zapobj, *offp);
1982 
1983 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
1984 		zap_cursor_fini(&cursor);
1985 		return (SET_ERROR(ENOENT));
1986 	}
1987 
1988 	if (strlen(attr.za_name) + 1 > namelen) {
1989 		zap_cursor_fini(&cursor);
1990 		return (SET_ERROR(ENAMETOOLONG));
1991 	}
1992 
1993 	(void) strcpy(name, attr.za_name);
1994 	if (idp)
1995 		*idp = attr.za_first_integer;
1996 	zap_cursor_advance(&cursor);
1997 	*offp = zap_cursor_serialize(&cursor);
1998 	zap_cursor_fini(&cursor);
1999 
2000 	return (0);
2001 }
2002 
2003 typedef struct dmu_objset_find_ctx {
2004 	taskq_t		*dc_tq;
2005 	dsl_pool_t	*dc_dp;
2006 	uint64_t	dc_ddobj;
2007 	char		*dc_ddname; /* last component of ddobj's name */
2008 	int		(*dc_func)(dsl_pool_t *, dsl_dataset_t *, void *);
2009 	void		*dc_arg;
2010 	int		dc_flags;
2011 	kmutex_t	*dc_error_lock;
2012 	int		*dc_error;
2013 } dmu_objset_find_ctx_t;
2014 
2015 static void
2016 dmu_objset_find_dp_impl(dmu_objset_find_ctx_t *dcp)
2017 {
2018 	dsl_pool_t *dp = dcp->dc_dp;
2019 	dsl_dir_t *dd;
2020 	dsl_dataset_t *ds;
2021 	zap_cursor_t zc;
2022 	zap_attribute_t *attr;
2023 	uint64_t thisobj;
2024 	int err = 0;
2025 
2026 	/* don't process if there already was an error */
2027 	if (*dcp->dc_error != 0)
2028 		goto out;
2029 
2030 	/*
2031 	 * Note: passing the name (dc_ddname) here is optional, but it
2032 	 * improves performance because we don't need to call
2033 	 * zap_value_search() to determine the name.
2034 	 */
2035 	err = dsl_dir_hold_obj(dp, dcp->dc_ddobj, dcp->dc_ddname, FTAG, &dd);
2036 	if (err != 0)
2037 		goto out;
2038 
2039 	/* Don't visit hidden ($MOS & $ORIGIN) objsets. */
2040 	if (dd->dd_myname[0] == '$') {
2041 		dsl_dir_rele(dd, FTAG);
2042 		goto out;
2043 	}
2044 
2045 	thisobj = dsl_dir_phys(dd)->dd_head_dataset_obj;
2046 	attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
2047 
2048 	/*
2049 	 * Iterate over all children.
2050 	 */
2051 	if (dcp->dc_flags & DS_FIND_CHILDREN) {
2052 		for (zap_cursor_init(&zc, dp->dp_meta_objset,
2053 		    dsl_dir_phys(dd)->dd_child_dir_zapobj);
2054 		    zap_cursor_retrieve(&zc, attr) == 0;
2055 		    (void) zap_cursor_advance(&zc)) {
2056 			ASSERT3U(attr->za_integer_length, ==,
2057 			    sizeof (uint64_t));
2058 			ASSERT3U(attr->za_num_integers, ==, 1);
2059 
2060 			dmu_objset_find_ctx_t *child_dcp =
2061 			    kmem_alloc(sizeof (*child_dcp), KM_SLEEP);
2062 			*child_dcp = *dcp;
2063 			child_dcp->dc_ddobj = attr->za_first_integer;
2064 			child_dcp->dc_ddname = spa_strdup(attr->za_name);
2065 			if (dcp->dc_tq != NULL)
2066 				(void) taskq_dispatch(dcp->dc_tq,
2067 				    dmu_objset_find_dp_cb, child_dcp, TQ_SLEEP);
2068 			else
2069 				dmu_objset_find_dp_impl(child_dcp);
2070 		}
2071 		zap_cursor_fini(&zc);
2072 	}
2073 
2074 	/*
2075 	 * Iterate over all snapshots.
2076 	 */
2077 	if (dcp->dc_flags & DS_FIND_SNAPSHOTS) {
2078 		dsl_dataset_t *ds;
2079 		err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
2080 
2081 		if (err == 0) {
2082 			uint64_t snapobj;
2083 
2084 			snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
2085 			dsl_dataset_rele(ds, FTAG);
2086 
2087 			for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
2088 			    zap_cursor_retrieve(&zc, attr) == 0;
2089 			    (void) zap_cursor_advance(&zc)) {
2090 				ASSERT3U(attr->za_integer_length, ==,
2091 				    sizeof (uint64_t));
2092 				ASSERT3U(attr->za_num_integers, ==, 1);
2093 
2094 				err = dsl_dataset_hold_obj(dp,
2095 				    attr->za_first_integer, FTAG, &ds);
2096 				if (err != 0)
2097 					break;
2098 				err = dcp->dc_func(dp, ds, dcp->dc_arg);
2099 				dsl_dataset_rele(ds, FTAG);
2100 				if (err != 0)
2101 					break;
2102 			}
2103 			zap_cursor_fini(&zc);
2104 		}
2105 	}
2106 
2107 	kmem_free(attr, sizeof (zap_attribute_t));
2108 
2109 	if (err != 0) {
2110 		dsl_dir_rele(dd, FTAG);
2111 		goto out;
2112 	}
2113 
2114 	/*
2115 	 * Apply to self.
2116 	 */
2117 	err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
2118 
2119 	/*
2120 	 * Note: we hold the dir while calling dsl_dataset_hold_obj() so
2121 	 * that the dir will remain cached, and we won't have to re-instantiate
2122 	 * it (which could be expensive due to finding its name via
2123 	 * zap_value_search()).
2124 	 */
2125 	dsl_dir_rele(dd, FTAG);
2126 	if (err != 0)
2127 		goto out;
2128 	err = dcp->dc_func(dp, ds, dcp->dc_arg);
2129 	dsl_dataset_rele(ds, FTAG);
2130 
2131 out:
2132 	if (err != 0) {
2133 		mutex_enter(dcp->dc_error_lock);
2134 		/* only keep first error */
2135 		if (*dcp->dc_error == 0)
2136 			*dcp->dc_error = err;
2137 		mutex_exit(dcp->dc_error_lock);
2138 	}
2139 
2140 	if (dcp->dc_ddname != NULL)
2141 		spa_strfree(dcp->dc_ddname);
2142 	kmem_free(dcp, sizeof (*dcp));
2143 }
2144 
2145 static void
2146 dmu_objset_find_dp_cb(void *arg)
2147 {
2148 	dmu_objset_find_ctx_t *dcp = arg;
2149 	dsl_pool_t *dp = dcp->dc_dp;
2150 
2151 	/*
2152 	 * We need to get a pool_config_lock here, as there are several
2153 	 * asssert(pool_config_held) down the stack. Getting a lock via
2154 	 * dsl_pool_config_enter is risky, as it might be stalled by a
2155 	 * pending writer. This would deadlock, as the write lock can
2156 	 * only be granted when our parent thread gives up the lock.
2157 	 * The _prio interface gives us priority over a pending writer.
2158 	 */
2159 	dsl_pool_config_enter_prio(dp, FTAG);
2160 
2161 	dmu_objset_find_dp_impl(dcp);
2162 
2163 	dsl_pool_config_exit(dp, FTAG);
2164 }
2165 
2166 /*
2167  * Find objsets under and including ddobj, call func(ds) on each.
2168  * The order for the enumeration is completely undefined.
2169  * func is called with dsl_pool_config held.
2170  */
2171 int
2172 dmu_objset_find_dp(dsl_pool_t *dp, uint64_t ddobj,
2173     int func(dsl_pool_t *, dsl_dataset_t *, void *), void *arg, int flags)
2174 {
2175 	int error = 0;
2176 	taskq_t *tq = NULL;
2177 	int ntasks;
2178 	dmu_objset_find_ctx_t *dcp;
2179 	kmutex_t err_lock;
2180 
2181 	mutex_init(&err_lock, NULL, MUTEX_DEFAULT, NULL);
2182 	dcp = kmem_alloc(sizeof (*dcp), KM_SLEEP);
2183 	dcp->dc_tq = NULL;
2184 	dcp->dc_dp = dp;
2185 	dcp->dc_ddobj = ddobj;
2186 	dcp->dc_ddname = NULL;
2187 	dcp->dc_func = func;
2188 	dcp->dc_arg = arg;
2189 	dcp->dc_flags = flags;
2190 	dcp->dc_error_lock = &err_lock;
2191 	dcp->dc_error = &error;
2192 
2193 	if ((flags & DS_FIND_SERIALIZE) || dsl_pool_config_held_writer(dp)) {
2194 		/*
2195 		 * In case a write lock is held we can't make use of
2196 		 * parallelism, as down the stack of the worker threads
2197 		 * the lock is asserted via dsl_pool_config_held.
2198 		 * In case of a read lock this is solved by getting a read
2199 		 * lock in each worker thread, which isn't possible in case
2200 		 * of a writer lock. So we fall back to the synchronous path
2201 		 * here.
2202 		 * In the future it might be possible to get some magic into
2203 		 * dsl_pool_config_held in a way that it returns true for
2204 		 * the worker threads so that a single lock held from this
2205 		 * thread suffices. For now, stay single threaded.
2206 		 */
2207 		dmu_objset_find_dp_impl(dcp);
2208 		mutex_destroy(&err_lock);
2209 
2210 		return (error);
2211 	}
2212 
2213 	ntasks = dmu_find_threads;
2214 	if (ntasks == 0)
2215 		ntasks = vdev_count_leaves(dp->dp_spa) * 4;
2216 	tq = taskq_create("dmu_objset_find", ntasks, minclsyspri, ntasks,
2217 	    INT_MAX, 0);
2218 	if (tq == NULL) {
2219 		kmem_free(dcp, sizeof (*dcp));
2220 		mutex_destroy(&err_lock);
2221 
2222 		return (SET_ERROR(ENOMEM));
2223 	}
2224 	dcp->dc_tq = tq;
2225 
2226 	/* dcp will be freed by task */
2227 	(void) taskq_dispatch(tq, dmu_objset_find_dp_cb, dcp, TQ_SLEEP);
2228 
2229 	/*
2230 	 * PORTING: this code relies on the property of taskq_wait to wait
2231 	 * until no more tasks are queued and no more tasks are active. As
2232 	 * we always queue new tasks from within other tasks, task_wait
2233 	 * reliably waits for the full recursion to finish, even though we
2234 	 * enqueue new tasks after taskq_wait has been called.
2235 	 * On platforms other than illumos, taskq_wait may not have this
2236 	 * property.
2237 	 */
2238 	taskq_wait(tq);
2239 	taskq_destroy(tq);
2240 	mutex_destroy(&err_lock);
2241 
2242 	return (error);
2243 }
2244 
2245 /*
2246  * Find all objsets under name, and for each, call 'func(child_name, arg)'.
2247  * The dp_config_rwlock must not be held when this is called, and it
2248  * will not be held when the callback is called.
2249  * Therefore this function should only be used when the pool is not changing
2250  * (e.g. in syncing context), or the callback can deal with the possible races.
2251  */
2252 static int
2253 dmu_objset_find_impl(spa_t *spa, const char *name,
2254     int func(const char *, void *), void *arg, int flags)
2255 {
2256 	dsl_dir_t *dd;
2257 	dsl_pool_t *dp = spa_get_dsl(spa);
2258 	dsl_dataset_t *ds;
2259 	zap_cursor_t zc;
2260 	zap_attribute_t *attr;
2261 	char *child;
2262 	uint64_t thisobj;
2263 	int err;
2264 
2265 	dsl_pool_config_enter(dp, FTAG);
2266 
2267 	err = dsl_dir_hold(dp, name, FTAG, &dd, NULL);
2268 	if (err != 0) {
2269 		dsl_pool_config_exit(dp, FTAG);
2270 		return (err);
2271 	}
2272 
2273 	/* Don't visit hidden ($MOS & $ORIGIN) objsets. */
2274 	if (dd->dd_myname[0] == '$') {
2275 		dsl_dir_rele(dd, FTAG);
2276 		dsl_pool_config_exit(dp, FTAG);
2277 		return (0);
2278 	}
2279 
2280 	thisobj = dsl_dir_phys(dd)->dd_head_dataset_obj;
2281 	attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
2282 
2283 	/*
2284 	 * Iterate over all children.
2285 	 */
2286 	if (flags & DS_FIND_CHILDREN) {
2287 		for (zap_cursor_init(&zc, dp->dp_meta_objset,
2288 		    dsl_dir_phys(dd)->dd_child_dir_zapobj);
2289 		    zap_cursor_retrieve(&zc, attr) == 0;
2290 		    (void) zap_cursor_advance(&zc)) {
2291 			ASSERT3U(attr->za_integer_length, ==,
2292 			    sizeof (uint64_t));
2293 			ASSERT3U(attr->za_num_integers, ==, 1);
2294 
2295 			child = kmem_asprintf("%s/%s", name, attr->za_name);
2296 			dsl_pool_config_exit(dp, FTAG);
2297 			err = dmu_objset_find_impl(spa, child,
2298 			    func, arg, flags);
2299 			dsl_pool_config_enter(dp, FTAG);
2300 			strfree(child);
2301 			if (err != 0)
2302 				break;
2303 		}
2304 		zap_cursor_fini(&zc);
2305 
2306 		if (err != 0) {
2307 			dsl_dir_rele(dd, FTAG);
2308 			dsl_pool_config_exit(dp, FTAG);
2309 			kmem_free(attr, sizeof (zap_attribute_t));
2310 			return (err);
2311 		}
2312 	}
2313 
2314 	/*
2315 	 * Iterate over all snapshots.
2316 	 */
2317 	if (flags & DS_FIND_SNAPSHOTS) {
2318 		err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
2319 
2320 		if (err == 0) {
2321 			uint64_t snapobj;
2322 
2323 			snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
2324 			dsl_dataset_rele(ds, FTAG);
2325 
2326 			for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
2327 			    zap_cursor_retrieve(&zc, attr) == 0;
2328 			    (void) zap_cursor_advance(&zc)) {
2329 				ASSERT3U(attr->za_integer_length, ==,
2330 				    sizeof (uint64_t));
2331 				ASSERT3U(attr->za_num_integers, ==, 1);
2332 
2333 				child = kmem_asprintf("%s@%s",
2334 				    name, attr->za_name);
2335 				dsl_pool_config_exit(dp, FTAG);
2336 				err = func(child, arg);
2337 				dsl_pool_config_enter(dp, FTAG);
2338 				strfree(child);
2339 				if (err != 0)
2340 					break;
2341 			}
2342 			zap_cursor_fini(&zc);
2343 		}
2344 	}
2345 
2346 	dsl_dir_rele(dd, FTAG);
2347 	kmem_free(attr, sizeof (zap_attribute_t));
2348 	dsl_pool_config_exit(dp, FTAG);
2349 
2350 	if (err != 0)
2351 		return (err);
2352 
2353 	/* Apply to self. */
2354 	return (func(name, arg));
2355 }
2356 
2357 /*
2358  * See comment above dmu_objset_find_impl().
2359  */
2360 int
2361 dmu_objset_find(char *name, int func(const char *, void *), void *arg,
2362     int flags)
2363 {
2364 	spa_t *spa;
2365 	int error;
2366 
2367 	error = spa_open(name, &spa, FTAG);
2368 	if (error != 0)
2369 		return (error);
2370 	error = dmu_objset_find_impl(spa, name, func, arg, flags);
2371 	spa_close(spa, FTAG);
2372 	return (error);
2373 }
2374 
2375 void
2376 dmu_objset_set_user(objset_t *os, void *user_ptr)
2377 {
2378 	ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
2379 	os->os_user_ptr = user_ptr;
2380 }
2381 
2382 void *
2383 dmu_objset_get_user(objset_t *os)
2384 {
2385 	ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
2386 	return (os->os_user_ptr);
2387 }
2388 
2389 /*
2390  * Determine name of filesystem, given name of snapshot.
2391  * buf must be at least ZFS_MAX_DATASET_NAME_LEN bytes
2392  */
2393 int
2394 dmu_fsname(const char *snapname, char *buf)
2395 {
2396 	char *atp = strchr(snapname, '@');
2397 	if (atp == NULL)
2398 		return (SET_ERROR(EINVAL));
2399 	if (atp - snapname >= ZFS_MAX_DATASET_NAME_LEN)
2400 		return (SET_ERROR(ENAMETOOLONG));
2401 	(void) strlcpy(buf, snapname, atp - snapname + 1);
2402 	return (0);
2403 }
2404 
2405 /*
2406  * Call when we think we're going to write/free space in open context to track
2407  * the amount of dirty data in the open txg, which is also the amount
2408  * of memory that can not be evicted until this txg syncs.
2409  */
2410 void
2411 dmu_objset_willuse_space(objset_t *os, int64_t space, dmu_tx_t *tx)
2412 {
2413 	dsl_dataset_t *ds = os->os_dsl_dataset;
2414 	int64_t aspace = spa_get_worst_case_asize(os->os_spa, space);
2415 
2416 	if (ds != NULL) {
2417 		dsl_dir_willuse_space(ds->ds_dir, aspace, tx);
2418 		dsl_pool_dirty_space(dmu_tx_pool(tx), space, tx);
2419 	}
2420 }
2421