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