dmu_objset.c revision 268647
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21/*
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
24 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
25 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
26 */
27
28/* Portions Copyright 2010 Robert Milkowski */
29
30#include <sys/cred.h>
31#include <sys/zfs_context.h>
32#include <sys/dmu_objset.h>
33#include <sys/dsl_dir.h>
34#include <sys/dsl_dataset.h>
35#include <sys/dsl_prop.h>
36#include <sys/dsl_pool.h>
37#include <sys/dsl_synctask.h>
38#include <sys/dsl_deleg.h>
39#include <sys/dnode.h>
40#include <sys/dbuf.h>
41#include <sys/zvol.h>
42#include <sys/dmu_tx.h>
43#include <sys/zap.h>
44#include <sys/zil.h>
45#include <sys/dmu_impl.h>
46#include <sys/zfs_ioctl.h>
47#include <sys/sa.h>
48#include <sys/zfs_onexit.h>
49#include <sys/dsl_destroy.h>
50
51/*
52 * Needed to close a window in dnode_move() that allows the objset to be freed
53 * before it can be safely accessed.
54 */
55krwlock_t os_lock;
56
57void
58dmu_objset_init(void)
59{
60	rw_init(&os_lock, NULL, RW_DEFAULT, NULL);
61}
62
63void
64dmu_objset_fini(void)
65{
66	rw_destroy(&os_lock);
67}
68
69spa_t *
70dmu_objset_spa(objset_t *os)
71{
72	return (os->os_spa);
73}
74
75zilog_t *
76dmu_objset_zil(objset_t *os)
77{
78	return (os->os_zil);
79}
80
81dsl_pool_t *
82dmu_objset_pool(objset_t *os)
83{
84	dsl_dataset_t *ds;
85
86	if ((ds = os->os_dsl_dataset) != NULL && ds->ds_dir)
87		return (ds->ds_dir->dd_pool);
88	else
89		return (spa_get_dsl(os->os_spa));
90}
91
92dsl_dataset_t *
93dmu_objset_ds(objset_t *os)
94{
95	return (os->os_dsl_dataset);
96}
97
98dmu_objset_type_t
99dmu_objset_type(objset_t *os)
100{
101	return (os->os_phys->os_type);
102}
103
104void
105dmu_objset_name(objset_t *os, char *buf)
106{
107	dsl_dataset_name(os->os_dsl_dataset, buf);
108}
109
110uint64_t
111dmu_objset_id(objset_t *os)
112{
113	dsl_dataset_t *ds = os->os_dsl_dataset;
114
115	return (ds ? ds->ds_object : 0);
116}
117
118zfs_sync_type_t
119dmu_objset_syncprop(objset_t *os)
120{
121	return (os->os_sync);
122}
123
124zfs_logbias_op_t
125dmu_objset_logbias(objset_t *os)
126{
127	return (os->os_logbias);
128}
129
130static void
131checksum_changed_cb(void *arg, uint64_t newval)
132{
133	objset_t *os = arg;
134
135	/*
136	 * Inheritance should have been done by now.
137	 */
138	ASSERT(newval != ZIO_CHECKSUM_INHERIT);
139
140	os->os_checksum = zio_checksum_select(newval, ZIO_CHECKSUM_ON_VALUE);
141}
142
143static void
144compression_changed_cb(void *arg, uint64_t newval)
145{
146	objset_t *os = arg;
147
148	/*
149	 * Inheritance and range checking should have been done by now.
150	 */
151	ASSERT(newval != ZIO_COMPRESS_INHERIT);
152
153	os->os_compress = zio_compress_select(newval, ZIO_COMPRESS_ON_VALUE);
154}
155
156static void
157copies_changed_cb(void *arg, uint64_t newval)
158{
159	objset_t *os = arg;
160
161	/*
162	 * Inheritance and range checking should have been done by now.
163	 */
164	ASSERT(newval > 0);
165	ASSERT(newval <= spa_max_replication(os->os_spa));
166
167	os->os_copies = newval;
168}
169
170static void
171dedup_changed_cb(void *arg, uint64_t newval)
172{
173	objset_t *os = arg;
174	spa_t *spa = os->os_spa;
175	enum zio_checksum checksum;
176
177	/*
178	 * Inheritance should have been done by now.
179	 */
180	ASSERT(newval != ZIO_CHECKSUM_INHERIT);
181
182	checksum = zio_checksum_dedup_select(spa, newval, ZIO_CHECKSUM_OFF);
183
184	os->os_dedup_checksum = checksum & ZIO_CHECKSUM_MASK;
185	os->os_dedup_verify = !!(checksum & ZIO_CHECKSUM_VERIFY);
186}
187
188static void
189primary_cache_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 == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
197	    newval == ZFS_CACHE_METADATA);
198
199	os->os_primary_cache = newval;
200}
201
202static void
203secondary_cache_changed_cb(void *arg, uint64_t newval)
204{
205	objset_t *os = arg;
206
207	/*
208	 * Inheritance and range checking should have been done by now.
209	 */
210	ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
211	    newval == ZFS_CACHE_METADATA);
212
213	os->os_secondary_cache = newval;
214}
215
216static void
217sync_changed_cb(void *arg, uint64_t newval)
218{
219	objset_t *os = arg;
220
221	/*
222	 * Inheritance and range checking should have been done by now.
223	 */
224	ASSERT(newval == ZFS_SYNC_STANDARD || newval == ZFS_SYNC_ALWAYS ||
225	    newval == ZFS_SYNC_DISABLED);
226
227	os->os_sync = newval;
228	if (os->os_zil)
229		zil_set_sync(os->os_zil, newval);
230}
231
232static void
233redundant_metadata_changed_cb(void *arg, uint64_t newval)
234{
235	objset_t *os = arg;
236
237	/*
238	 * Inheritance and range checking should have been done by now.
239	 */
240	ASSERT(newval == ZFS_REDUNDANT_METADATA_ALL ||
241	    newval == ZFS_REDUNDANT_METADATA_MOST);
242
243	os->os_redundant_metadata = newval;
244}
245
246static void
247logbias_changed_cb(void *arg, uint64_t newval)
248{
249	objset_t *os = arg;
250
251	ASSERT(newval == ZFS_LOGBIAS_LATENCY ||
252	    newval == ZFS_LOGBIAS_THROUGHPUT);
253	os->os_logbias = newval;
254	if (os->os_zil)
255		zil_set_logbias(os->os_zil, newval);
256}
257
258void
259dmu_objset_byteswap(void *buf, size_t size)
260{
261	objset_phys_t *osp = buf;
262
263	ASSERT(size == OBJSET_OLD_PHYS_SIZE || size == sizeof (objset_phys_t));
264	dnode_byteswap(&osp->os_meta_dnode);
265	byteswap_uint64_array(&osp->os_zil_header, sizeof (zil_header_t));
266	osp->os_type = BSWAP_64(osp->os_type);
267	osp->os_flags = BSWAP_64(osp->os_flags);
268	if (size == sizeof (objset_phys_t)) {
269		dnode_byteswap(&osp->os_userused_dnode);
270		dnode_byteswap(&osp->os_groupused_dnode);
271	}
272}
273
274int
275dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
276    objset_t **osp)
277{
278	objset_t *os;
279	int i, err;
280
281	ASSERT(ds == NULL || MUTEX_HELD(&ds->ds_opening_lock));
282
283	os = kmem_zalloc(sizeof (objset_t), KM_SLEEP);
284	os->os_dsl_dataset = ds;
285	os->os_spa = spa;
286	os->os_rootbp = bp;
287	if (!BP_IS_HOLE(os->os_rootbp)) {
288		uint32_t aflags = ARC_WAIT;
289		zbookmark_t zb;
290		SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET,
291		    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
292
293		if (DMU_OS_IS_L2CACHEABLE(os))
294			aflags |= ARC_L2CACHE;
295		if (DMU_OS_IS_L2COMPRESSIBLE(os))
296			aflags |= ARC_L2COMPRESS;
297
298		dprintf_bp(os->os_rootbp, "reading %s", "");
299		err = arc_read(NULL, spa, os->os_rootbp,
300		    arc_getbuf_func, &os->os_phys_buf,
301		    ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &aflags, &zb);
302		if (err != 0) {
303			kmem_free(os, sizeof (objset_t));
304			/* convert checksum errors into IO errors */
305			if (err == ECKSUM)
306				err = SET_ERROR(EIO);
307			return (err);
308		}
309
310		/* Increase the blocksize if we are permitted. */
311		if (spa_version(spa) >= SPA_VERSION_USERSPACE &&
312		    arc_buf_size(os->os_phys_buf) < sizeof (objset_phys_t)) {
313			arc_buf_t *buf = arc_buf_alloc(spa,
314			    sizeof (objset_phys_t), &os->os_phys_buf,
315			    ARC_BUFC_METADATA);
316			bzero(buf->b_data, sizeof (objset_phys_t));
317			bcopy(os->os_phys_buf->b_data, buf->b_data,
318			    arc_buf_size(os->os_phys_buf));
319			(void) arc_buf_remove_ref(os->os_phys_buf,
320			    &os->os_phys_buf);
321			os->os_phys_buf = buf;
322		}
323
324		os->os_phys = os->os_phys_buf->b_data;
325		os->os_flags = os->os_phys->os_flags;
326	} else {
327		int size = spa_version(spa) >= SPA_VERSION_USERSPACE ?
328		    sizeof (objset_phys_t) : OBJSET_OLD_PHYS_SIZE;
329		os->os_phys_buf = arc_buf_alloc(spa, size,
330		    &os->os_phys_buf, ARC_BUFC_METADATA);
331		os->os_phys = os->os_phys_buf->b_data;
332		bzero(os->os_phys, size);
333	}
334
335	/*
336	 * Note: the changed_cb will be called once before the register
337	 * func returns, thus changing the checksum/compression from the
338	 * default (fletcher2/off).  Snapshots don't need to know about
339	 * checksum/compression/copies.
340	 */
341	if (ds) {
342		err = dsl_prop_register(ds,
343		    zfs_prop_to_name(ZFS_PROP_PRIMARYCACHE),
344		    primary_cache_changed_cb, os);
345		if (err == 0) {
346			err = dsl_prop_register(ds,
347			    zfs_prop_to_name(ZFS_PROP_SECONDARYCACHE),
348			    secondary_cache_changed_cb, os);
349		}
350		if (!dsl_dataset_is_snapshot(ds)) {
351			if (err == 0) {
352				err = dsl_prop_register(ds,
353				    zfs_prop_to_name(ZFS_PROP_CHECKSUM),
354				    checksum_changed_cb, os);
355			}
356			if (err == 0) {
357				err = dsl_prop_register(ds,
358				    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
359				    compression_changed_cb, os);
360			}
361			if (err == 0) {
362				err = dsl_prop_register(ds,
363				    zfs_prop_to_name(ZFS_PROP_COPIES),
364				    copies_changed_cb, os);
365			}
366			if (err == 0) {
367				err = dsl_prop_register(ds,
368				    zfs_prop_to_name(ZFS_PROP_DEDUP),
369				    dedup_changed_cb, os);
370			}
371			if (err == 0) {
372				err = dsl_prop_register(ds,
373				    zfs_prop_to_name(ZFS_PROP_LOGBIAS),
374				    logbias_changed_cb, os);
375			}
376			if (err == 0) {
377				err = dsl_prop_register(ds,
378				    zfs_prop_to_name(ZFS_PROP_SYNC),
379				    sync_changed_cb, os);
380			}
381			if (err == 0) {
382				err = dsl_prop_register(ds,
383				    zfs_prop_to_name(
384				    ZFS_PROP_REDUNDANT_METADATA),
385				    redundant_metadata_changed_cb, os);
386			}
387		}
388		if (err != 0) {
389			VERIFY(arc_buf_remove_ref(os->os_phys_buf,
390			    &os->os_phys_buf));
391			kmem_free(os, sizeof (objset_t));
392			return (err);
393		}
394	} else if (ds == NULL) {
395		/* It's the meta-objset. */
396		os->os_checksum = ZIO_CHECKSUM_FLETCHER_4;
397		os->os_compress = ZIO_COMPRESS_LZJB;
398		os->os_copies = spa_max_replication(spa);
399		os->os_dedup_checksum = ZIO_CHECKSUM_OFF;
400		os->os_dedup_verify = B_FALSE;
401		os->os_logbias = ZFS_LOGBIAS_LATENCY;
402		os->os_sync = ZFS_SYNC_STANDARD;
403		os->os_primary_cache = ZFS_CACHE_ALL;
404		os->os_secondary_cache = ZFS_CACHE_ALL;
405	}
406
407	if (ds == NULL || !dsl_dataset_is_snapshot(ds))
408		os->os_zil_header = os->os_phys->os_zil_header;
409	os->os_zil = zil_alloc(os, &os->os_zil_header);
410
411	for (i = 0; i < TXG_SIZE; i++) {
412		list_create(&os->os_dirty_dnodes[i], sizeof (dnode_t),
413		    offsetof(dnode_t, dn_dirty_link[i]));
414		list_create(&os->os_free_dnodes[i], sizeof (dnode_t),
415		    offsetof(dnode_t, dn_dirty_link[i]));
416	}
417	list_create(&os->os_dnodes, sizeof (dnode_t),
418	    offsetof(dnode_t, dn_link));
419	list_create(&os->os_downgraded_dbufs, sizeof (dmu_buf_impl_t),
420	    offsetof(dmu_buf_impl_t, db_link));
421
422	mutex_init(&os->os_lock, NULL, MUTEX_DEFAULT, NULL);
423	mutex_init(&os->os_obj_lock, NULL, MUTEX_DEFAULT, NULL);
424	mutex_init(&os->os_user_ptr_lock, NULL, MUTEX_DEFAULT, NULL);
425
426	DMU_META_DNODE(os) = dnode_special_open(os,
427	    &os->os_phys->os_meta_dnode, DMU_META_DNODE_OBJECT,
428	    &os->os_meta_dnode);
429	if (arc_buf_size(os->os_phys_buf) >= sizeof (objset_phys_t)) {
430		DMU_USERUSED_DNODE(os) = dnode_special_open(os,
431		    &os->os_phys->os_userused_dnode, DMU_USERUSED_OBJECT,
432		    &os->os_userused_dnode);
433		DMU_GROUPUSED_DNODE(os) = dnode_special_open(os,
434		    &os->os_phys->os_groupused_dnode, DMU_GROUPUSED_OBJECT,
435		    &os->os_groupused_dnode);
436	}
437
438	/*
439	 * We should be the only thread trying to do this because we
440	 * have ds_opening_lock
441	 */
442	if (ds) {
443		mutex_enter(&ds->ds_lock);
444		ASSERT(ds->ds_objset == NULL);
445		ds->ds_objset = os;
446		mutex_exit(&ds->ds_lock);
447	}
448
449	*osp = os;
450	return (0);
451}
452
453int
454dmu_objset_from_ds(dsl_dataset_t *ds, objset_t **osp)
455{
456	int err = 0;
457
458	mutex_enter(&ds->ds_opening_lock);
459	*osp = ds->ds_objset;
460	if (*osp == NULL) {
461		err = dmu_objset_open_impl(dsl_dataset_get_spa(ds),
462		    ds, dsl_dataset_get_blkptr(ds), osp);
463	}
464	mutex_exit(&ds->ds_opening_lock);
465	return (err);
466}
467
468/*
469 * Holds the pool while the objset is held.  Therefore only one objset
470 * can be held at a time.
471 */
472int
473dmu_objset_hold(const char *name, void *tag, objset_t **osp)
474{
475	dsl_pool_t *dp;
476	dsl_dataset_t *ds;
477	int err;
478
479	err = dsl_pool_hold(name, tag, &dp);
480	if (err != 0)
481		return (err);
482	err = dsl_dataset_hold(dp, name, tag, &ds);
483	if (err != 0) {
484		dsl_pool_rele(dp, tag);
485		return (err);
486	}
487
488	err = dmu_objset_from_ds(ds, osp);
489	if (err != 0) {
490		dsl_dataset_rele(ds, tag);
491		dsl_pool_rele(dp, tag);
492	}
493
494	return (err);
495}
496
497/*
498 * dsl_pool must not be held when this is called.
499 * Upon successful return, there will be a longhold on the dataset,
500 * and the dsl_pool will not be held.
501 */
502int
503dmu_objset_own(const char *name, dmu_objset_type_t type,
504    boolean_t readonly, void *tag, objset_t **osp)
505{
506	dsl_pool_t *dp;
507	dsl_dataset_t *ds;
508	int err;
509
510	err = dsl_pool_hold(name, FTAG, &dp);
511	if (err != 0)
512		return (err);
513	err = dsl_dataset_own(dp, name, tag, &ds);
514	if (err != 0) {
515		dsl_pool_rele(dp, FTAG);
516		return (err);
517	}
518
519	err = dmu_objset_from_ds(ds, osp);
520	dsl_pool_rele(dp, FTAG);
521	if (err != 0) {
522		dsl_dataset_disown(ds, tag);
523	} else if (type != DMU_OST_ANY && type != (*osp)->os_phys->os_type) {
524		dsl_dataset_disown(ds, tag);
525		return (SET_ERROR(EINVAL));
526	} else if (!readonly && dsl_dataset_is_snapshot(ds)) {
527		dsl_dataset_disown(ds, tag);
528		return (SET_ERROR(EROFS));
529	}
530	return (err);
531}
532
533void
534dmu_objset_rele(objset_t *os, void *tag)
535{
536	dsl_pool_t *dp = dmu_objset_pool(os);
537	dsl_dataset_rele(os->os_dsl_dataset, tag);
538	dsl_pool_rele(dp, tag);
539}
540
541/*
542 * When we are called, os MUST refer to an objset associated with a dataset
543 * that is owned by 'tag'; that is, is held and long held by 'tag' and ds_owner
544 * == tag.  We will then release and reacquire ownership of the dataset while
545 * holding the pool config_rwlock to avoid intervening namespace or ownership
546 * changes may occur.
547 *
548 * This exists solely to accommodate zfs_ioc_userspace_upgrade()'s desire to
549 * release the hold on its dataset and acquire a new one on the dataset of the
550 * same name so that it can be partially torn down and reconstructed.
551 */
552void
553dmu_objset_refresh_ownership(objset_t *os, void *tag)
554{
555	dsl_pool_t *dp;
556	dsl_dataset_t *ds, *newds;
557	char name[MAXNAMELEN];
558
559	ds = os->os_dsl_dataset;
560	VERIFY3P(ds, !=, NULL);
561	VERIFY3P(ds->ds_owner, ==, tag);
562	VERIFY(dsl_dataset_long_held(ds));
563
564	dsl_dataset_name(ds, name);
565	dp = dmu_objset_pool(os);
566	dsl_pool_config_enter(dp, FTAG);
567	dmu_objset_disown(os, tag);
568	VERIFY0(dsl_dataset_own(dp, name, tag, &newds));
569	VERIFY3P(newds, ==, os->os_dsl_dataset);
570	dsl_pool_config_exit(dp, FTAG);
571}
572
573void
574dmu_objset_disown(objset_t *os, void *tag)
575{
576	dsl_dataset_disown(os->os_dsl_dataset, tag);
577}
578
579void
580dmu_objset_evict_dbufs(objset_t *os)
581{
582	dnode_t *dn;
583
584	mutex_enter(&os->os_lock);
585
586	/* process the mdn last, since the other dnodes have holds on it */
587	list_remove(&os->os_dnodes, DMU_META_DNODE(os));
588	list_insert_tail(&os->os_dnodes, DMU_META_DNODE(os));
589
590	/*
591	 * Find the first dnode with holds.  We have to do this dance
592	 * because dnode_add_ref() only works if you already have a
593	 * hold.  If there are no holds then it has no dbufs so OK to
594	 * skip.
595	 */
596	for (dn = list_head(&os->os_dnodes);
597	    dn && !dnode_add_ref(dn, FTAG);
598	    dn = list_next(&os->os_dnodes, dn))
599		continue;
600
601	while (dn) {
602		dnode_t *next_dn = dn;
603
604		do {
605			next_dn = list_next(&os->os_dnodes, next_dn);
606		} while (next_dn && !dnode_add_ref(next_dn, FTAG));
607
608		mutex_exit(&os->os_lock);
609		dnode_evict_dbufs(dn);
610		dnode_rele(dn, FTAG);
611		mutex_enter(&os->os_lock);
612		dn = next_dn;
613	}
614	mutex_exit(&os->os_lock);
615}
616
617void
618dmu_objset_evict(objset_t *os)
619{
620	dsl_dataset_t *ds = os->os_dsl_dataset;
621
622	for (int t = 0; t < TXG_SIZE; t++)
623		ASSERT(!dmu_objset_is_dirty(os, t));
624
625	if (ds) {
626		if (!dsl_dataset_is_snapshot(ds)) {
627			VERIFY0(dsl_prop_unregister(ds,
628			    zfs_prop_to_name(ZFS_PROP_CHECKSUM),
629			    checksum_changed_cb, os));
630			VERIFY0(dsl_prop_unregister(ds,
631			    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
632			    compression_changed_cb, os));
633			VERIFY0(dsl_prop_unregister(ds,
634			    zfs_prop_to_name(ZFS_PROP_COPIES),
635			    copies_changed_cb, os));
636			VERIFY0(dsl_prop_unregister(ds,
637			    zfs_prop_to_name(ZFS_PROP_DEDUP),
638			    dedup_changed_cb, os));
639			VERIFY0(dsl_prop_unregister(ds,
640			    zfs_prop_to_name(ZFS_PROP_LOGBIAS),
641			    logbias_changed_cb, os));
642			VERIFY0(dsl_prop_unregister(ds,
643			    zfs_prop_to_name(ZFS_PROP_SYNC),
644			    sync_changed_cb, os));
645			VERIFY0(dsl_prop_unregister(ds,
646			    zfs_prop_to_name(ZFS_PROP_REDUNDANT_METADATA),
647			    redundant_metadata_changed_cb, os));
648		}
649		VERIFY0(dsl_prop_unregister(ds,
650		    zfs_prop_to_name(ZFS_PROP_PRIMARYCACHE),
651		    primary_cache_changed_cb, os));
652		VERIFY0(dsl_prop_unregister(ds,
653		    zfs_prop_to_name(ZFS_PROP_SECONDARYCACHE),
654		    secondary_cache_changed_cb, os));
655	}
656
657	if (os->os_sa)
658		sa_tear_down(os);
659
660	dmu_objset_evict_dbufs(os);
661
662	dnode_special_close(&os->os_meta_dnode);
663	if (DMU_USERUSED_DNODE(os)) {
664		dnode_special_close(&os->os_userused_dnode);
665		dnode_special_close(&os->os_groupused_dnode);
666	}
667	zil_free(os->os_zil);
668
669	ASSERT3P(list_head(&os->os_dnodes), ==, NULL);
670
671	VERIFY(arc_buf_remove_ref(os->os_phys_buf, &os->os_phys_buf));
672
673	/*
674	 * This is a barrier to prevent the objset from going away in
675	 * dnode_move() until we can safely ensure that the objset is still in
676	 * use. We consider the objset valid before the barrier and invalid
677	 * after the barrier.
678	 */
679	rw_enter(&os_lock, RW_READER);
680	rw_exit(&os_lock);
681
682	mutex_destroy(&os->os_lock);
683	mutex_destroy(&os->os_obj_lock);
684	mutex_destroy(&os->os_user_ptr_lock);
685	kmem_free(os, sizeof (objset_t));
686}
687
688timestruc_t
689dmu_objset_snap_cmtime(objset_t *os)
690{
691	return (dsl_dir_snap_cmtime(os->os_dsl_dataset->ds_dir));
692}
693
694/* called from dsl for meta-objset */
695objset_t *
696dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
697    dmu_objset_type_t type, dmu_tx_t *tx)
698{
699	objset_t *os;
700	dnode_t *mdn;
701
702	ASSERT(dmu_tx_is_syncing(tx));
703
704	if (ds != NULL)
705		VERIFY0(dmu_objset_from_ds(ds, &os));
706	else
707		VERIFY0(dmu_objset_open_impl(spa, NULL, bp, &os));
708
709	mdn = DMU_META_DNODE(os);
710
711	dnode_allocate(mdn, DMU_OT_DNODE, 1 << DNODE_BLOCK_SHIFT,
712	    DN_MAX_INDBLKSHIFT, DMU_OT_NONE, 0, tx);
713
714	/*
715	 * We don't want to have to increase the meta-dnode's nlevels
716	 * later, because then we could do it in quescing context while
717	 * we are also accessing it in open context.
718	 *
719	 * This precaution is not necessary for the MOS (ds == NULL),
720	 * because the MOS is only updated in syncing context.
721	 * This is most fortunate: the MOS is the only objset that
722	 * needs to be synced multiple times as spa_sync() iterates
723	 * to convergence, so minimizing its dn_nlevels matters.
724	 */
725	if (ds != NULL) {
726		int levels = 1;
727
728		/*
729		 * Determine the number of levels necessary for the meta-dnode
730		 * to contain DN_MAX_OBJECT dnodes.
731		 */
732		while ((uint64_t)mdn->dn_nblkptr << (mdn->dn_datablkshift +
733		    (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) <
734		    DN_MAX_OBJECT * sizeof (dnode_phys_t))
735			levels++;
736
737		mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] =
738		    mdn->dn_nlevels = levels;
739	}
740
741	ASSERT(type != DMU_OST_NONE);
742	ASSERT(type != DMU_OST_ANY);
743	ASSERT(type < DMU_OST_NUMTYPES);
744	os->os_phys->os_type = type;
745	if (dmu_objset_userused_enabled(os)) {
746		os->os_phys->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
747		os->os_flags = os->os_phys->os_flags;
748	}
749
750	dsl_dataset_dirty(ds, tx);
751
752	return (os);
753}
754
755typedef struct dmu_objset_create_arg {
756	const char *doca_name;
757	cred_t *doca_cred;
758	void (*doca_userfunc)(objset_t *os, void *arg,
759	    cred_t *cr, dmu_tx_t *tx);
760	void *doca_userarg;
761	dmu_objset_type_t doca_type;
762	uint64_t doca_flags;
763} dmu_objset_create_arg_t;
764
765/*ARGSUSED*/
766static int
767dmu_objset_create_check(void *arg, dmu_tx_t *tx)
768{
769	dmu_objset_create_arg_t *doca = arg;
770	dsl_pool_t *dp = dmu_tx_pool(tx);
771	dsl_dir_t *pdd;
772	const char *tail;
773	int error;
774
775	if (strchr(doca->doca_name, '@') != NULL)
776		return (SET_ERROR(EINVAL));
777
778	error = dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail);
779	if (error != 0)
780		return (error);
781	if (tail == NULL) {
782		dsl_dir_rele(pdd, FTAG);
783		return (SET_ERROR(EEXIST));
784	}
785	error = dsl_fs_ss_limit_check(pdd, 1, ZFS_PROP_FILESYSTEM_LIMIT, NULL,
786	    doca->doca_cred);
787	dsl_dir_rele(pdd, FTAG);
788
789	return (error);
790}
791
792static void
793dmu_objset_create_sync(void *arg, dmu_tx_t *tx)
794{
795	dmu_objset_create_arg_t *doca = arg;
796	dsl_pool_t *dp = dmu_tx_pool(tx);
797	dsl_dir_t *pdd;
798	const char *tail;
799	dsl_dataset_t *ds;
800	uint64_t obj;
801	blkptr_t *bp;
802	objset_t *os;
803
804	VERIFY0(dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail));
805
806	obj = dsl_dataset_create_sync(pdd, tail, NULL, doca->doca_flags,
807	    doca->doca_cred, tx);
808
809	VERIFY0(dsl_dataset_hold_obj(pdd->dd_pool, obj, FTAG, &ds));
810	bp = dsl_dataset_get_blkptr(ds);
811	os = dmu_objset_create_impl(pdd->dd_pool->dp_spa,
812	    ds, bp, doca->doca_type, tx);
813
814	if (doca->doca_userfunc != NULL) {
815		doca->doca_userfunc(os, doca->doca_userarg,
816		    doca->doca_cred, tx);
817	}
818
819	spa_history_log_internal_ds(ds, "create", tx, "");
820	dsl_dataset_rele(ds, FTAG);
821	dsl_dir_rele(pdd, FTAG);
822}
823
824int
825dmu_objset_create(const char *name, dmu_objset_type_t type, uint64_t flags,
826    void (*func)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx), void *arg)
827{
828	dmu_objset_create_arg_t doca;
829
830	doca.doca_name = name;
831	doca.doca_cred = CRED();
832	doca.doca_flags = flags;
833	doca.doca_userfunc = func;
834	doca.doca_userarg = arg;
835	doca.doca_type = type;
836
837	return (dsl_sync_task(name,
838	    dmu_objset_create_check, dmu_objset_create_sync, &doca, 5));
839}
840
841typedef struct dmu_objset_clone_arg {
842	const char *doca_clone;
843	const char *doca_origin;
844	cred_t *doca_cred;
845} dmu_objset_clone_arg_t;
846
847/*ARGSUSED*/
848static int
849dmu_objset_clone_check(void *arg, dmu_tx_t *tx)
850{
851	dmu_objset_clone_arg_t *doca = arg;
852	dsl_dir_t *pdd;
853	const char *tail;
854	int error;
855	dsl_dataset_t *origin;
856	dsl_pool_t *dp = dmu_tx_pool(tx);
857
858	if (strchr(doca->doca_clone, '@') != NULL)
859		return (SET_ERROR(EINVAL));
860
861	error = dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail);
862	if (error != 0)
863		return (error);
864	if (tail == NULL) {
865		dsl_dir_rele(pdd, FTAG);
866		return (SET_ERROR(EEXIST));
867	}
868	/* You can't clone across pools. */
869	if (pdd->dd_pool != dp) {
870		dsl_dir_rele(pdd, FTAG);
871		return (SET_ERROR(EXDEV));
872	}
873	error = dsl_fs_ss_limit_check(pdd, 1, ZFS_PROP_FILESYSTEM_LIMIT, NULL,
874	    doca->doca_cred);
875	if (error != 0) {
876		dsl_dir_rele(pdd, FTAG);
877		return (SET_ERROR(EDQUOT));
878	}
879	dsl_dir_rele(pdd, FTAG);
880
881	error = dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin);
882	if (error != 0)
883		return (error);
884
885	/* You can't clone across pools. */
886	if (origin->ds_dir->dd_pool != dp) {
887		dsl_dataset_rele(origin, FTAG);
888		return (SET_ERROR(EXDEV));
889	}
890
891	/* You can only clone snapshots, not the head datasets. */
892	if (!dsl_dataset_is_snapshot(origin)) {
893		dsl_dataset_rele(origin, FTAG);
894		return (SET_ERROR(EINVAL));
895	}
896	dsl_dataset_rele(origin, FTAG);
897
898	return (0);
899}
900
901static void
902dmu_objset_clone_sync(void *arg, dmu_tx_t *tx)
903{
904	dmu_objset_clone_arg_t *doca = arg;
905	dsl_pool_t *dp = dmu_tx_pool(tx);
906	dsl_dir_t *pdd;
907	const char *tail;
908	dsl_dataset_t *origin, *ds;
909	uint64_t obj;
910	char namebuf[MAXNAMELEN];
911
912	VERIFY0(dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail));
913	VERIFY0(dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin));
914
915	obj = dsl_dataset_create_sync(pdd, tail, origin, 0,
916	    doca->doca_cred, tx);
917
918	VERIFY0(dsl_dataset_hold_obj(pdd->dd_pool, obj, FTAG, &ds));
919	dsl_dataset_name(origin, namebuf);
920	spa_history_log_internal_ds(ds, "clone", tx,
921	    "origin=%s (%llu)", namebuf, origin->ds_object);
922	dsl_dataset_rele(ds, FTAG);
923	dsl_dataset_rele(origin, FTAG);
924	dsl_dir_rele(pdd, FTAG);
925}
926
927int
928dmu_objset_clone(const char *clone, const char *origin)
929{
930	dmu_objset_clone_arg_t doca;
931
932	doca.doca_clone = clone;
933	doca.doca_origin = origin;
934	doca.doca_cred = CRED();
935
936	return (dsl_sync_task(clone,
937	    dmu_objset_clone_check, dmu_objset_clone_sync, &doca, 5));
938}
939
940int
941dmu_objset_snapshot_one(const char *fsname, const char *snapname)
942{
943	int err;
944	char *longsnap = kmem_asprintf("%s@%s", fsname, snapname);
945	nvlist_t *snaps = fnvlist_alloc();
946
947	fnvlist_add_boolean(snaps, longsnap);
948	strfree(longsnap);
949	err = dsl_dataset_snapshot(snaps, NULL, NULL);
950	fnvlist_free(snaps);
951	return (err);
952}
953
954static void
955dmu_objset_sync_dnodes(list_t *list, list_t *newlist, dmu_tx_t *tx)
956{
957	dnode_t *dn;
958
959	while (dn = list_head(list)) {
960		ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
961		ASSERT(dn->dn_dbuf->db_data_pending);
962		/*
963		 * Initialize dn_zio outside dnode_sync() because the
964		 * meta-dnode needs to set it ouside dnode_sync().
965		 */
966		dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio;
967		ASSERT(dn->dn_zio);
968
969		ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS);
970		list_remove(list, dn);
971
972		if (newlist) {
973			(void) dnode_add_ref(dn, newlist);
974			list_insert_tail(newlist, dn);
975		}
976
977		dnode_sync(dn, tx);
978	}
979}
980
981/* ARGSUSED */
982static void
983dmu_objset_write_ready(zio_t *zio, arc_buf_t *abuf, void *arg)
984{
985	blkptr_t *bp = zio->io_bp;
986	objset_t *os = arg;
987	dnode_phys_t *dnp = &os->os_phys->os_meta_dnode;
988
989	ASSERT3P(bp, ==, os->os_rootbp);
990	ASSERT3U(BP_GET_TYPE(bp), ==, DMU_OT_OBJSET);
991	ASSERT0(BP_GET_LEVEL(bp));
992
993	/*
994	 * Update rootbp fill count: it should be the number of objects
995	 * allocated in the object set (not counting the "special"
996	 * objects that are stored in the objset_phys_t -- the meta
997	 * dnode and user/group accounting objects).
998	 */
999	bp->blk_fill = 0;
1000	for (int i = 0; i < dnp->dn_nblkptr; i++)
1001		bp->blk_fill += dnp->dn_blkptr[i].blk_fill;
1002}
1003
1004/* ARGSUSED */
1005static void
1006dmu_objset_write_done(zio_t *zio, arc_buf_t *abuf, void *arg)
1007{
1008	blkptr_t *bp = zio->io_bp;
1009	blkptr_t *bp_orig = &zio->io_bp_orig;
1010	objset_t *os = arg;
1011
1012	if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
1013		ASSERT(BP_EQUAL(bp, bp_orig));
1014	} else {
1015		dsl_dataset_t *ds = os->os_dsl_dataset;
1016		dmu_tx_t *tx = os->os_synctx;
1017
1018		(void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
1019		dsl_dataset_block_born(ds, bp, tx);
1020	}
1021}
1022
1023/* called from dsl */
1024void
1025dmu_objset_sync(objset_t *os, zio_t *pio, dmu_tx_t *tx)
1026{
1027	int txgoff;
1028	zbookmark_t zb;
1029	zio_prop_t zp;
1030	zio_t *zio;
1031	list_t *list;
1032	list_t *newlist = NULL;
1033	dbuf_dirty_record_t *dr;
1034
1035	dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg);
1036
1037	ASSERT(dmu_tx_is_syncing(tx));
1038	/* XXX the write_done callback should really give us the tx... */
1039	os->os_synctx = tx;
1040
1041	if (os->os_dsl_dataset == NULL) {
1042		/*
1043		 * This is the MOS.  If we have upgraded,
1044		 * spa_max_replication() could change, so reset
1045		 * os_copies here.
1046		 */
1047		os->os_copies = spa_max_replication(os->os_spa);
1048	}
1049
1050	/*
1051	 * Create the root block IO
1052	 */
1053	SET_BOOKMARK(&zb, os->os_dsl_dataset ?
1054	    os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
1055	    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
1056	arc_release(os->os_phys_buf, &os->os_phys_buf);
1057
1058	dmu_write_policy(os, NULL, 0, 0, &zp);
1059
1060	zio = arc_write(pio, os->os_spa, tx->tx_txg,
1061	    os->os_rootbp, os->os_phys_buf, DMU_OS_IS_L2CACHEABLE(os),
1062	    DMU_OS_IS_L2COMPRESSIBLE(os), &zp, dmu_objset_write_ready,
1063	    NULL, dmu_objset_write_done, os, ZIO_PRIORITY_ASYNC_WRITE,
1064	    ZIO_FLAG_MUSTSUCCEED, &zb);
1065
1066	/*
1067	 * Sync special dnodes - the parent IO for the sync is the root block
1068	 */
1069	DMU_META_DNODE(os)->dn_zio = zio;
1070	dnode_sync(DMU_META_DNODE(os), tx);
1071
1072	os->os_phys->os_flags = os->os_flags;
1073
1074	if (DMU_USERUSED_DNODE(os) &&
1075	    DMU_USERUSED_DNODE(os)->dn_type != DMU_OT_NONE) {
1076		DMU_USERUSED_DNODE(os)->dn_zio = zio;
1077		dnode_sync(DMU_USERUSED_DNODE(os), tx);
1078		DMU_GROUPUSED_DNODE(os)->dn_zio = zio;
1079		dnode_sync(DMU_GROUPUSED_DNODE(os), tx);
1080	}
1081
1082	txgoff = tx->tx_txg & TXG_MASK;
1083
1084	if (dmu_objset_userused_enabled(os)) {
1085		newlist = &os->os_synced_dnodes;
1086		/*
1087		 * We must create the list here because it uses the
1088		 * dn_dirty_link[] of this txg.
1089		 */
1090		list_create(newlist, sizeof (dnode_t),
1091		    offsetof(dnode_t, dn_dirty_link[txgoff]));
1092	}
1093
1094	dmu_objset_sync_dnodes(&os->os_free_dnodes[txgoff], newlist, tx);
1095	dmu_objset_sync_dnodes(&os->os_dirty_dnodes[txgoff], newlist, tx);
1096
1097	list = &DMU_META_DNODE(os)->dn_dirty_records[txgoff];
1098	while (dr = list_head(list)) {
1099		ASSERT0(dr->dr_dbuf->db_level);
1100		list_remove(list, dr);
1101		if (dr->dr_zio)
1102			zio_nowait(dr->dr_zio);
1103	}
1104	/*
1105	 * Free intent log blocks up to this tx.
1106	 */
1107	zil_sync(os->os_zil, tx);
1108	os->os_phys->os_zil_header = os->os_zil_header;
1109	zio_nowait(zio);
1110}
1111
1112boolean_t
1113dmu_objset_is_dirty(objset_t *os, uint64_t txg)
1114{
1115	return (!list_is_empty(&os->os_dirty_dnodes[txg & TXG_MASK]) ||
1116	    !list_is_empty(&os->os_free_dnodes[txg & TXG_MASK]));
1117}
1118
1119static objset_used_cb_t *used_cbs[DMU_OST_NUMTYPES];
1120
1121void
1122dmu_objset_register_type(dmu_objset_type_t ost, objset_used_cb_t *cb)
1123{
1124	used_cbs[ost] = cb;
1125}
1126
1127boolean_t
1128dmu_objset_userused_enabled(objset_t *os)
1129{
1130	return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE &&
1131	    used_cbs[os->os_phys->os_type] != NULL &&
1132	    DMU_USERUSED_DNODE(os) != NULL);
1133}
1134
1135static void
1136do_userquota_update(objset_t *os, uint64_t used, uint64_t flags,
1137    uint64_t user, uint64_t group, boolean_t subtract, dmu_tx_t *tx)
1138{
1139	if ((flags & DNODE_FLAG_USERUSED_ACCOUNTED)) {
1140		int64_t delta = DNODE_SIZE + used;
1141		if (subtract)
1142			delta = -delta;
1143		VERIFY3U(0, ==, zap_increment_int(os, DMU_USERUSED_OBJECT,
1144		    user, delta, tx));
1145		VERIFY3U(0, ==, zap_increment_int(os, DMU_GROUPUSED_OBJECT,
1146		    group, delta, tx));
1147	}
1148}
1149
1150void
1151dmu_objset_do_userquota_updates(objset_t *os, dmu_tx_t *tx)
1152{
1153	dnode_t *dn;
1154	list_t *list = &os->os_synced_dnodes;
1155
1156	ASSERT(list_head(list) == NULL || dmu_objset_userused_enabled(os));
1157
1158	while (dn = list_head(list)) {
1159		int flags;
1160		ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object));
1161		ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE ||
1162		    dn->dn_phys->dn_flags &
1163		    DNODE_FLAG_USERUSED_ACCOUNTED);
1164
1165		/* Allocate the user/groupused objects if necessary. */
1166		if (DMU_USERUSED_DNODE(os)->dn_type == DMU_OT_NONE) {
1167			VERIFY(0 == zap_create_claim(os,
1168			    DMU_USERUSED_OBJECT,
1169			    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
1170			VERIFY(0 == zap_create_claim(os,
1171			    DMU_GROUPUSED_OBJECT,
1172			    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
1173		}
1174
1175		/*
1176		 * We intentionally modify the zap object even if the
1177		 * net delta is zero.  Otherwise
1178		 * the block of the zap obj could be shared between
1179		 * datasets but need to be different between them after
1180		 * a bprewrite.
1181		 */
1182
1183		flags = dn->dn_id_flags;
1184		ASSERT(flags);
1185		if (flags & DN_ID_OLD_EXIST)  {
1186			do_userquota_update(os, dn->dn_oldused, dn->dn_oldflags,
1187			    dn->dn_olduid, dn->dn_oldgid, B_TRUE, tx);
1188		}
1189		if (flags & DN_ID_NEW_EXIST) {
1190			do_userquota_update(os, DN_USED_BYTES(dn->dn_phys),
1191			    dn->dn_phys->dn_flags,  dn->dn_newuid,
1192			    dn->dn_newgid, B_FALSE, tx);
1193		}
1194
1195		mutex_enter(&dn->dn_mtx);
1196		dn->dn_oldused = 0;
1197		dn->dn_oldflags = 0;
1198		if (dn->dn_id_flags & DN_ID_NEW_EXIST) {
1199			dn->dn_olduid = dn->dn_newuid;
1200			dn->dn_oldgid = dn->dn_newgid;
1201			dn->dn_id_flags |= DN_ID_OLD_EXIST;
1202			if (dn->dn_bonuslen == 0)
1203				dn->dn_id_flags |= DN_ID_CHKED_SPILL;
1204			else
1205				dn->dn_id_flags |= DN_ID_CHKED_BONUS;
1206		}
1207		dn->dn_id_flags &= ~(DN_ID_NEW_EXIST);
1208		mutex_exit(&dn->dn_mtx);
1209
1210		list_remove(list, dn);
1211		dnode_rele(dn, list);
1212	}
1213}
1214
1215/*
1216 * Returns a pointer to data to find uid/gid from
1217 *
1218 * If a dirty record for transaction group that is syncing can't
1219 * be found then NULL is returned.  In the NULL case it is assumed
1220 * the uid/gid aren't changing.
1221 */
1222static void *
1223dmu_objset_userquota_find_data(dmu_buf_impl_t *db, dmu_tx_t *tx)
1224{
1225	dbuf_dirty_record_t *dr, **drp;
1226	void *data;
1227
1228	if (db->db_dirtycnt == 0)
1229		return (db->db.db_data);  /* Nothing is changing */
1230
1231	for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next)
1232		if (dr->dr_txg == tx->tx_txg)
1233			break;
1234
1235	if (dr == NULL) {
1236		data = NULL;
1237	} else {
1238		dnode_t *dn;
1239
1240		DB_DNODE_ENTER(dr->dr_dbuf);
1241		dn = DB_DNODE(dr->dr_dbuf);
1242
1243		if (dn->dn_bonuslen == 0 &&
1244		    dr->dr_dbuf->db_blkid == DMU_SPILL_BLKID)
1245			data = dr->dt.dl.dr_data->b_data;
1246		else
1247			data = dr->dt.dl.dr_data;
1248
1249		DB_DNODE_EXIT(dr->dr_dbuf);
1250	}
1251
1252	return (data);
1253}
1254
1255void
1256dmu_objset_userquota_get_ids(dnode_t *dn, boolean_t before, dmu_tx_t *tx)
1257{
1258	objset_t *os = dn->dn_objset;
1259	void *data = NULL;
1260	dmu_buf_impl_t *db = NULL;
1261	uint64_t *user = NULL;
1262	uint64_t *group = NULL;
1263	int flags = dn->dn_id_flags;
1264	int error;
1265	boolean_t have_spill = B_FALSE;
1266
1267	if (!dmu_objset_userused_enabled(dn->dn_objset))
1268		return;
1269
1270	if (before && (flags & (DN_ID_CHKED_BONUS|DN_ID_OLD_EXIST|
1271	    DN_ID_CHKED_SPILL)))
1272		return;
1273
1274	if (before && dn->dn_bonuslen != 0)
1275		data = DN_BONUS(dn->dn_phys);
1276	else if (!before && dn->dn_bonuslen != 0) {
1277		if (dn->dn_bonus) {
1278			db = dn->dn_bonus;
1279			mutex_enter(&db->db_mtx);
1280			data = dmu_objset_userquota_find_data(db, tx);
1281		} else {
1282			data = DN_BONUS(dn->dn_phys);
1283		}
1284	} else if (dn->dn_bonuslen == 0 && dn->dn_bonustype == DMU_OT_SA) {
1285			int rf = 0;
1286
1287			if (RW_WRITE_HELD(&dn->dn_struct_rwlock))
1288				rf |= DB_RF_HAVESTRUCT;
1289			error = dmu_spill_hold_by_dnode(dn,
1290			    rf | DB_RF_MUST_SUCCEED,
1291			    FTAG, (dmu_buf_t **)&db);
1292			ASSERT(error == 0);
1293			mutex_enter(&db->db_mtx);
1294			data = (before) ? db->db.db_data :
1295			    dmu_objset_userquota_find_data(db, tx);
1296			have_spill = B_TRUE;
1297	} else {
1298		mutex_enter(&dn->dn_mtx);
1299		dn->dn_id_flags |= DN_ID_CHKED_BONUS;
1300		mutex_exit(&dn->dn_mtx);
1301		return;
1302	}
1303
1304	if (before) {
1305		ASSERT(data);
1306		user = &dn->dn_olduid;
1307		group = &dn->dn_oldgid;
1308	} else if (data) {
1309		user = &dn->dn_newuid;
1310		group = &dn->dn_newgid;
1311	}
1312
1313	/*
1314	 * Must always call the callback in case the object
1315	 * type has changed and that type isn't an object type to track
1316	 */
1317	error = used_cbs[os->os_phys->os_type](dn->dn_bonustype, data,
1318	    user, group);
1319
1320	/*
1321	 * Preserve existing uid/gid when the callback can't determine
1322	 * what the new uid/gid are and the callback returned EEXIST.
1323	 * The EEXIST error tells us to just use the existing uid/gid.
1324	 * If we don't know what the old values are then just assign
1325	 * them to 0, since that is a new file  being created.
1326	 */
1327	if (!before && data == NULL && error == EEXIST) {
1328		if (flags & DN_ID_OLD_EXIST) {
1329			dn->dn_newuid = dn->dn_olduid;
1330			dn->dn_newgid = dn->dn_oldgid;
1331		} else {
1332			dn->dn_newuid = 0;
1333			dn->dn_newgid = 0;
1334		}
1335		error = 0;
1336	}
1337
1338	if (db)
1339		mutex_exit(&db->db_mtx);
1340
1341	mutex_enter(&dn->dn_mtx);
1342	if (error == 0 && before)
1343		dn->dn_id_flags |= DN_ID_OLD_EXIST;
1344	if (error == 0 && !before)
1345		dn->dn_id_flags |= DN_ID_NEW_EXIST;
1346
1347	if (have_spill) {
1348		dn->dn_id_flags |= DN_ID_CHKED_SPILL;
1349	} else {
1350		dn->dn_id_flags |= DN_ID_CHKED_BONUS;
1351	}
1352	mutex_exit(&dn->dn_mtx);
1353	if (have_spill)
1354		dmu_buf_rele((dmu_buf_t *)db, FTAG);
1355}
1356
1357boolean_t
1358dmu_objset_userspace_present(objset_t *os)
1359{
1360	return (os->os_phys->os_flags &
1361	    OBJSET_FLAG_USERACCOUNTING_COMPLETE);
1362}
1363
1364int
1365dmu_objset_userspace_upgrade(objset_t *os)
1366{
1367	uint64_t obj;
1368	int err = 0;
1369
1370	if (dmu_objset_userspace_present(os))
1371		return (0);
1372	if (!dmu_objset_userused_enabled(os))
1373		return (SET_ERROR(ENOTSUP));
1374	if (dmu_objset_is_snapshot(os))
1375		return (SET_ERROR(EINVAL));
1376
1377	/*
1378	 * We simply need to mark every object dirty, so that it will be
1379	 * synced out and now accounted.  If this is called
1380	 * concurrently, or if we already did some work before crashing,
1381	 * that's fine, since we track each object's accounted state
1382	 * independently.
1383	 */
1384
1385	for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
1386		dmu_tx_t *tx;
1387		dmu_buf_t *db;
1388		int objerr;
1389
1390		if (issig(JUSTLOOKING) && issig(FORREAL))
1391			return (SET_ERROR(EINTR));
1392
1393		objerr = dmu_bonus_hold(os, obj, FTAG, &db);
1394		if (objerr != 0)
1395			continue;
1396		tx = dmu_tx_create(os);
1397		dmu_tx_hold_bonus(tx, obj);
1398		objerr = dmu_tx_assign(tx, TXG_WAIT);
1399		if (objerr != 0) {
1400			dmu_tx_abort(tx);
1401			continue;
1402		}
1403		dmu_buf_will_dirty(db, tx);
1404		dmu_buf_rele(db, FTAG);
1405		dmu_tx_commit(tx);
1406	}
1407
1408	os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
1409	txg_wait_synced(dmu_objset_pool(os), 0);
1410	return (0);
1411}
1412
1413void
1414dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp,
1415    uint64_t *usedobjsp, uint64_t *availobjsp)
1416{
1417	dsl_dataset_space(os->os_dsl_dataset, refdbytesp, availbytesp,
1418	    usedobjsp, availobjsp);
1419}
1420
1421uint64_t
1422dmu_objset_fsid_guid(objset_t *os)
1423{
1424	return (dsl_dataset_fsid_guid(os->os_dsl_dataset));
1425}
1426
1427void
1428dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat)
1429{
1430	stat->dds_type = os->os_phys->os_type;
1431	if (os->os_dsl_dataset)
1432		dsl_dataset_fast_stat(os->os_dsl_dataset, stat);
1433}
1434
1435void
1436dmu_objset_stats(objset_t *os, nvlist_t *nv)
1437{
1438	ASSERT(os->os_dsl_dataset ||
1439	    os->os_phys->os_type == DMU_OST_META);
1440
1441	if (os->os_dsl_dataset != NULL)
1442		dsl_dataset_stats(os->os_dsl_dataset, nv);
1443
1444	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE,
1445	    os->os_phys->os_type);
1446	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERACCOUNTING,
1447	    dmu_objset_userspace_present(os));
1448}
1449
1450int
1451dmu_objset_is_snapshot(objset_t *os)
1452{
1453	if (os->os_dsl_dataset != NULL)
1454		return (dsl_dataset_is_snapshot(os->os_dsl_dataset));
1455	else
1456		return (B_FALSE);
1457}
1458
1459int
1460dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen,
1461    boolean_t *conflict)
1462{
1463	dsl_dataset_t *ds = os->os_dsl_dataset;
1464	uint64_t ignored;
1465
1466	if (ds->ds_phys->ds_snapnames_zapobj == 0)
1467		return (SET_ERROR(ENOENT));
1468
1469	return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset,
1470	    ds->ds_phys->ds_snapnames_zapobj, name, 8, 1, &ignored, MT_FIRST,
1471	    real, maxlen, conflict));
1472}
1473
1474int
1475dmu_snapshot_list_next(objset_t *os, int namelen, char *name,
1476    uint64_t *idp, uint64_t *offp, boolean_t *case_conflict)
1477{
1478	dsl_dataset_t *ds = os->os_dsl_dataset;
1479	zap_cursor_t cursor;
1480	zap_attribute_t attr;
1481
1482	ASSERT(dsl_pool_config_held(dmu_objset_pool(os)));
1483
1484	if (ds->ds_phys->ds_snapnames_zapobj == 0)
1485		return (SET_ERROR(ENOENT));
1486
1487	zap_cursor_init_serialized(&cursor,
1488	    ds->ds_dir->dd_pool->dp_meta_objset,
1489	    ds->ds_phys->ds_snapnames_zapobj, *offp);
1490
1491	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
1492		zap_cursor_fini(&cursor);
1493		return (SET_ERROR(ENOENT));
1494	}
1495
1496	if (strlen(attr.za_name) + 1 > namelen) {
1497		zap_cursor_fini(&cursor);
1498		return (SET_ERROR(ENAMETOOLONG));
1499	}
1500
1501	(void) strcpy(name, attr.za_name);
1502	if (idp)
1503		*idp = attr.za_first_integer;
1504	if (case_conflict)
1505		*case_conflict = attr.za_normalization_conflict;
1506	zap_cursor_advance(&cursor);
1507	*offp = zap_cursor_serialize(&cursor);
1508	zap_cursor_fini(&cursor);
1509
1510	return (0);
1511}
1512
1513int
1514dmu_dir_list_next(objset_t *os, int namelen, char *name,
1515    uint64_t *idp, uint64_t *offp)
1516{
1517	dsl_dir_t *dd = os->os_dsl_dataset->ds_dir;
1518	zap_cursor_t cursor;
1519	zap_attribute_t attr;
1520
1521	/* there is no next dir on a snapshot! */
1522	if (os->os_dsl_dataset->ds_object !=
1523	    dd->dd_phys->dd_head_dataset_obj)
1524		return (SET_ERROR(ENOENT));
1525
1526	zap_cursor_init_serialized(&cursor,
1527	    dd->dd_pool->dp_meta_objset,
1528	    dd->dd_phys->dd_child_dir_zapobj, *offp);
1529
1530	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
1531		zap_cursor_fini(&cursor);
1532		return (SET_ERROR(ENOENT));
1533	}
1534
1535	if (strlen(attr.za_name) + 1 > namelen) {
1536		zap_cursor_fini(&cursor);
1537		return (SET_ERROR(ENAMETOOLONG));
1538	}
1539
1540	(void) strcpy(name, attr.za_name);
1541	if (idp)
1542		*idp = attr.za_first_integer;
1543	zap_cursor_advance(&cursor);
1544	*offp = zap_cursor_serialize(&cursor);
1545	zap_cursor_fini(&cursor);
1546
1547	return (0);
1548}
1549
1550/*
1551 * Find objsets under and including ddobj, call func(ds) on each.
1552 */
1553int
1554dmu_objset_find_dp(dsl_pool_t *dp, uint64_t ddobj,
1555    int func(dsl_pool_t *, dsl_dataset_t *, void *), void *arg, int flags)
1556{
1557	dsl_dir_t *dd;
1558	dsl_dataset_t *ds;
1559	zap_cursor_t zc;
1560	zap_attribute_t *attr;
1561	uint64_t thisobj;
1562	int err;
1563
1564	ASSERT(dsl_pool_config_held(dp));
1565
1566	err = dsl_dir_hold_obj(dp, ddobj, NULL, FTAG, &dd);
1567	if (err != 0)
1568		return (err);
1569
1570	/* Don't visit hidden ($MOS & $ORIGIN) objsets. */
1571	if (dd->dd_myname[0] == '$') {
1572		dsl_dir_rele(dd, FTAG);
1573		return (0);
1574	}
1575
1576	thisobj = dd->dd_phys->dd_head_dataset_obj;
1577	attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
1578
1579	/*
1580	 * Iterate over all children.
1581	 */
1582	if (flags & DS_FIND_CHILDREN) {
1583		for (zap_cursor_init(&zc, dp->dp_meta_objset,
1584		    dd->dd_phys->dd_child_dir_zapobj);
1585		    zap_cursor_retrieve(&zc, attr) == 0;
1586		    (void) zap_cursor_advance(&zc)) {
1587			ASSERT3U(attr->za_integer_length, ==,
1588			    sizeof (uint64_t));
1589			ASSERT3U(attr->za_num_integers, ==, 1);
1590
1591			err = dmu_objset_find_dp(dp, attr->za_first_integer,
1592			    func, arg, flags);
1593			if (err != 0)
1594				break;
1595		}
1596		zap_cursor_fini(&zc);
1597
1598		if (err != 0) {
1599			dsl_dir_rele(dd, FTAG);
1600			kmem_free(attr, sizeof (zap_attribute_t));
1601			return (err);
1602		}
1603	}
1604
1605	/*
1606	 * Iterate over all snapshots.
1607	 */
1608	if (flags & DS_FIND_SNAPSHOTS) {
1609		dsl_dataset_t *ds;
1610		err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
1611
1612		if (err == 0) {
1613			uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
1614			dsl_dataset_rele(ds, FTAG);
1615
1616			for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
1617			    zap_cursor_retrieve(&zc, attr) == 0;
1618			    (void) zap_cursor_advance(&zc)) {
1619				ASSERT3U(attr->za_integer_length, ==,
1620				    sizeof (uint64_t));
1621				ASSERT3U(attr->za_num_integers, ==, 1);
1622
1623				err = dsl_dataset_hold_obj(dp,
1624				    attr->za_first_integer, FTAG, &ds);
1625				if (err != 0)
1626					break;
1627				err = func(dp, ds, arg);
1628				dsl_dataset_rele(ds, FTAG);
1629				if (err != 0)
1630					break;
1631			}
1632			zap_cursor_fini(&zc);
1633		}
1634	}
1635
1636	dsl_dir_rele(dd, FTAG);
1637	kmem_free(attr, sizeof (zap_attribute_t));
1638
1639	if (err != 0)
1640		return (err);
1641
1642	/*
1643	 * Apply to self.
1644	 */
1645	err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
1646	if (err != 0)
1647		return (err);
1648	err = func(dp, ds, arg);
1649	dsl_dataset_rele(ds, FTAG);
1650	return (err);
1651}
1652
1653/*
1654 * Find all objsets under name, and for each, call 'func(child_name, arg)'.
1655 * The dp_config_rwlock must not be held when this is called, and it
1656 * will not be held when the callback is called.
1657 * Therefore this function should only be used when the pool is not changing
1658 * (e.g. in syncing context), or the callback can deal with the possible races.
1659 */
1660static int
1661dmu_objset_find_impl(spa_t *spa, const char *name,
1662    int func(const char *, void *), void *arg, int flags)
1663{
1664	dsl_dir_t *dd;
1665	dsl_pool_t *dp = spa_get_dsl(spa);
1666	dsl_dataset_t *ds;
1667	zap_cursor_t zc;
1668	zap_attribute_t *attr;
1669	char *child;
1670	uint64_t thisobj;
1671	int err;
1672
1673	dsl_pool_config_enter(dp, FTAG);
1674
1675	err = dsl_dir_hold(dp, name, FTAG, &dd, NULL);
1676	if (err != 0) {
1677		dsl_pool_config_exit(dp, FTAG);
1678		return (err);
1679	}
1680
1681	/* Don't visit hidden ($MOS & $ORIGIN) objsets. */
1682	if (dd->dd_myname[0] == '$') {
1683		dsl_dir_rele(dd, FTAG);
1684		dsl_pool_config_exit(dp, FTAG);
1685		return (0);
1686	}
1687
1688	thisobj = dd->dd_phys->dd_head_dataset_obj;
1689	attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
1690
1691	/*
1692	 * Iterate over all children.
1693	 */
1694	if (flags & DS_FIND_CHILDREN) {
1695		for (zap_cursor_init(&zc, dp->dp_meta_objset,
1696		    dd->dd_phys->dd_child_dir_zapobj);
1697		    zap_cursor_retrieve(&zc, attr) == 0;
1698		    (void) zap_cursor_advance(&zc)) {
1699			ASSERT3U(attr->za_integer_length, ==,
1700			    sizeof (uint64_t));
1701			ASSERT3U(attr->za_num_integers, ==, 1);
1702
1703			child = kmem_asprintf("%s/%s", name, attr->za_name);
1704			dsl_pool_config_exit(dp, FTAG);
1705			err = dmu_objset_find_impl(spa, child,
1706			    func, arg, flags);
1707			dsl_pool_config_enter(dp, FTAG);
1708			strfree(child);
1709			if (err != 0)
1710				break;
1711		}
1712		zap_cursor_fini(&zc);
1713
1714		if (err != 0) {
1715			dsl_dir_rele(dd, FTAG);
1716			dsl_pool_config_exit(dp, FTAG);
1717			kmem_free(attr, sizeof (zap_attribute_t));
1718			return (err);
1719		}
1720	}
1721
1722	/*
1723	 * Iterate over all snapshots.
1724	 */
1725	if (flags & DS_FIND_SNAPSHOTS) {
1726		err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
1727
1728		if (err == 0) {
1729			uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
1730			dsl_dataset_rele(ds, FTAG);
1731
1732			for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
1733			    zap_cursor_retrieve(&zc, attr) == 0;
1734			    (void) zap_cursor_advance(&zc)) {
1735				ASSERT3U(attr->za_integer_length, ==,
1736				    sizeof (uint64_t));
1737				ASSERT3U(attr->za_num_integers, ==, 1);
1738
1739				child = kmem_asprintf("%s@%s",
1740				    name, attr->za_name);
1741				dsl_pool_config_exit(dp, FTAG);
1742				err = func(child, arg);
1743				dsl_pool_config_enter(dp, FTAG);
1744				strfree(child);
1745				if (err != 0)
1746					break;
1747			}
1748			zap_cursor_fini(&zc);
1749		}
1750	}
1751
1752	dsl_dir_rele(dd, FTAG);
1753	kmem_free(attr, sizeof (zap_attribute_t));
1754	dsl_pool_config_exit(dp, FTAG);
1755
1756	if (err != 0)
1757		return (err);
1758
1759	/* Apply to self. */
1760	return (func(name, arg));
1761}
1762
1763/*
1764 * See comment above dmu_objset_find_impl().
1765 */
1766int
1767dmu_objset_find(char *name, int func(const char *, void *), void *arg,
1768    int flags)
1769{
1770	spa_t *spa;
1771	int error;
1772
1773	error = spa_open(name, &spa, FTAG);
1774	if (error != 0)
1775		return (error);
1776	error = dmu_objset_find_impl(spa, name, func, arg, flags);
1777	spa_close(spa, FTAG);
1778	return (error);
1779}
1780
1781void
1782dmu_objset_set_user(objset_t *os, void *user_ptr)
1783{
1784	ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
1785	os->os_user_ptr = user_ptr;
1786}
1787
1788void *
1789dmu_objset_get_user(objset_t *os)
1790{
1791	ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
1792	return (os->os_user_ptr);
1793}
1794
1795/*
1796 * Determine name of filesystem, given name of snapshot.
1797 * buf must be at least MAXNAMELEN bytes
1798 */
1799int
1800dmu_fsname(const char *snapname, char *buf)
1801{
1802	char *atp = strchr(snapname, '@');
1803	if (atp == NULL)
1804		return (SET_ERROR(EINVAL));
1805	if (atp - snapname >= MAXNAMELEN)
1806		return (SET_ERROR(ENAMETOOLONG));
1807	(void) strlcpy(buf, snapname, atp - snapname + 1);
1808	return (0);
1809}
1810