1fa9e4066Sahrens /*
2fa9e4066Sahrens * CDDL HEADER START
3fa9e4066Sahrens *
4fa9e4066Sahrens * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock * You may not use this file except in compliance with the License.
7fa9e4066Sahrens *
8fa9e4066Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens * See the License for the specific language governing permissions
11fa9e4066Sahrens * and limitations under the License.
12fa9e4066Sahrens *
13fa9e4066Sahrens * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens *
19fa9e4066Sahrens * CDDL HEADER END
20fa9e4066Sahrens */
21fa9e4066Sahrens /*
2206e0070dSMark Shellenbaum * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23*675fc291SMatthew Ahrens * Copyright (c) 2012, 2016 by Delphix. All rights reserved.
24aad02571SSaso Kiselkov * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
25bc93cffdSJerry Jelinek * Copyright (c) 2013, Joyent, Inc. All rights reserved.
260521c5ebSJustin Gibbs * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
27c7fed717SAlexander Eremin * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
28416c5cd3SArne Jansen * Copyright (c) 2015, STRATO AG, Inc. All rights reserved.
29fa9e4066Sahrens */
30fa9e4066Sahrens
3155da60b9SMark J Musante /* Portions Copyright 2010 Robert Milkowski */
3255da60b9SMark J Musante
33ecd6cf80Smarks #include <sys/cred.h>
34fa9e4066Sahrens #include <sys/zfs_context.h>
35fa9e4066Sahrens #include <sys/dmu_objset.h>
36fa9e4066Sahrens #include <sys/dsl_dir.h>
37fa9e4066Sahrens #include <sys/dsl_dataset.h>
38fa9e4066Sahrens #include <sys/dsl_prop.h>
39fa9e4066Sahrens #include <sys/dsl_pool.h>
401d452cf5Sahrens #include <sys/dsl_synctask.h>
41ecd6cf80Smarks #include <sys/dsl_deleg.h>
42fa9e4066Sahrens #include <sys/dnode.h>
43fa9e4066Sahrens #include <sys/dbuf.h>
44a2eea2e1Sahrens #include <sys/zvol.h>
45fa9e4066Sahrens #include <sys/dmu_tx.h>
46fa9e4066Sahrens #include <sys/zap.h>
47fa9e4066Sahrens #include <sys/zil.h>
48fa9e4066Sahrens #include <sys/dmu_impl.h>
49ecd6cf80Smarks #include <sys/zfs_ioctl.h>
500a586ceaSMark Shellenbaum #include <sys/sa.h>
5199d5e173STim Haley #include <sys/zfs_onexit.h>
523b2aab18SMatthew Ahrens #include <sys/dsl_destroy.h>
53416c5cd3SArne Jansen #include <sys/vdev.h>
54fa9e4066Sahrens
55744947dcSTom Erickson /*
56744947dcSTom Erickson * Needed to close a window in dnode_move() that allows the objset to be freed
57744947dcSTom Erickson * before it can be safely accessed.
58744947dcSTom Erickson */
59744947dcSTom Erickson krwlock_t os_lock;
60744947dcSTom Erickson
61416c5cd3SArne Jansen /*
62416c5cd3SArne Jansen * Tunable to overwrite the maximum number of threads for the parallization
63416c5cd3SArne Jansen * of dmu_objset_find_dp, needed to speed up the import of pools with many
64416c5cd3SArne Jansen * datasets.
65416c5cd3SArne Jansen * Default is 4 times the number of leaf vdevs.
66416c5cd3SArne Jansen */
67416c5cd3SArne Jansen int dmu_find_threads = 0;
68416c5cd3SArne Jansen
69416c5cd3SArne Jansen static void dmu_objset_find_dp_cb(void *arg);
70416c5cd3SArne Jansen
71744947dcSTom Erickson void
dmu_objset_init(void)72744947dcSTom Erickson dmu_objset_init(void)
73744947dcSTom Erickson {
74744947dcSTom Erickson rw_init(&os_lock, NULL, RW_DEFAULT, NULL);
75744947dcSTom Erickson }
76744947dcSTom Erickson
77744947dcSTom Erickson void
dmu_objset_fini(void)78744947dcSTom Erickson dmu_objset_fini(void)
79744947dcSTom Erickson {
80744947dcSTom Erickson rw_destroy(&os_lock);
81744947dcSTom Erickson }
82744947dcSTom Erickson
83fa9e4066Sahrens spa_t *
dmu_objset_spa(objset_t * os)84fa9e4066Sahrens dmu_objset_spa(objset_t *os)
85fa9e4066Sahrens {
86503ad85cSMatthew Ahrens return (os->os_spa);
87fa9e4066Sahrens }
88fa9e4066Sahrens
89fa9e4066Sahrens zilog_t *
dmu_objset_zil(objset_t * os)90fa9e4066Sahrens dmu_objset_zil(objset_t *os)
91fa9e4066Sahrens {
92503ad85cSMatthew Ahrens return (os->os_zil);
93fa9e4066Sahrens }
94fa9e4066Sahrens
95fa9e4066Sahrens dsl_pool_t *
dmu_objset_pool(objset_t * os)96fa9e4066Sahrens dmu_objset_pool(objset_t *os)
97fa9e4066Sahrens {
98fa9e4066Sahrens dsl_dataset_t *ds;
99fa9e4066Sahrens
100503ad85cSMatthew Ahrens if ((ds = os->os_dsl_dataset) != NULL && ds->ds_dir)
101fa9e4066Sahrens return (ds->ds_dir->dd_pool);
102fa9e4066Sahrens else
103503ad85cSMatthew Ahrens return (spa_get_dsl(os->os_spa));
104fa9e4066Sahrens }
105fa9e4066Sahrens
106fa9e4066Sahrens dsl_dataset_t *
dmu_objset_ds(objset_t * os)107fa9e4066Sahrens dmu_objset_ds(objset_t *os)
108fa9e4066Sahrens {
109503ad85cSMatthew Ahrens return (os->os_dsl_dataset);
110fa9e4066Sahrens }
111fa9e4066Sahrens
112fa9e4066Sahrens dmu_objset_type_t
dmu_objset_type(objset_t * os)113fa9e4066Sahrens dmu_objset_type(objset_t *os)
114fa9e4066Sahrens {
115503ad85cSMatthew Ahrens return (os->os_phys->os_type);
116fa9e4066Sahrens }
117fa9e4066Sahrens
118fa9e4066Sahrens void
dmu_objset_name(objset_t * os,char * buf)119fa9e4066Sahrens dmu_objset_name(objset_t *os, char *buf)
120fa9e4066Sahrens {
121503ad85cSMatthew Ahrens dsl_dataset_name(os->os_dsl_dataset, buf);
122fa9e4066Sahrens }
123fa9e4066Sahrens
124fa9e4066Sahrens uint64_t
dmu_objset_id(objset_t * os)125fa9e4066Sahrens dmu_objset_id(objset_t *os)
126fa9e4066Sahrens {
127503ad85cSMatthew Ahrens dsl_dataset_t *ds = os->os_dsl_dataset;
128fa9e4066Sahrens
129fa9e4066Sahrens return (ds ? ds->ds_object : 0);
130fa9e4066Sahrens }
131fa9e4066Sahrens
132469a3d62SMatthew Ahrens zfs_sync_type_t
dmu_objset_syncprop(objset_t * os)13355da60b9SMark J Musante dmu_objset_syncprop(objset_t *os)
13455da60b9SMark J Musante {
13555da60b9SMark J Musante return (os->os_sync);
13655da60b9SMark J Musante }
13755da60b9SMark J Musante
138469a3d62SMatthew Ahrens zfs_logbias_op_t
dmu_objset_logbias(objset_t * os)139e09fa4daSNeil Perrin dmu_objset_logbias(objset_t *os)
140e09fa4daSNeil Perrin {
141e09fa4daSNeil Perrin return (os->os_logbias);
142e09fa4daSNeil Perrin }
143e09fa4daSNeil Perrin
144fa9e4066Sahrens static void
checksum_changed_cb(void * arg,uint64_t newval)145fa9e4066Sahrens checksum_changed_cb(void *arg, uint64_t newval)
146fa9e4066Sahrens {
147503ad85cSMatthew Ahrens objset_t *os = arg;
148fa9e4066Sahrens
149fa9e4066Sahrens /*
150fa9e4066Sahrens * Inheritance should have been done by now.
151fa9e4066Sahrens */
152fa9e4066Sahrens ASSERT(newval != ZIO_CHECKSUM_INHERIT);
153fa9e4066Sahrens
154503ad85cSMatthew Ahrens os->os_checksum = zio_checksum_select(newval, ZIO_CHECKSUM_ON_VALUE);
155fa9e4066Sahrens }
156fa9e4066Sahrens
157fa9e4066Sahrens static void
compression_changed_cb(void * arg,uint64_t newval)158fa9e4066Sahrens compression_changed_cb(void *arg, uint64_t newval)
159fa9e4066Sahrens {
160503ad85cSMatthew Ahrens objset_t *os = arg;
161fa9e4066Sahrens
162fa9e4066Sahrens /*
163fa9e4066Sahrens * Inheritance and range checking should have been done by now.
164fa9e4066Sahrens */
165fa9e4066Sahrens ASSERT(newval != ZIO_COMPRESS_INHERIT);
166fa9e4066Sahrens
1674454ba50SJustin T. Gibbs os->os_compress = zio_compress_select(os->os_spa, newval,
1684454ba50SJustin T. Gibbs ZIO_COMPRESS_ON);
169fa9e4066Sahrens }
170fa9e4066Sahrens
171d0ad202dSahrens static void
copies_changed_cb(void * arg,uint64_t newval)172d0ad202dSahrens copies_changed_cb(void *arg, uint64_t newval)
173d0ad202dSahrens {
174503ad85cSMatthew Ahrens objset_t *os = arg;
175d0ad202dSahrens
176d0ad202dSahrens /*
177d0ad202dSahrens * Inheritance and range checking should have been done by now.
178d0ad202dSahrens */
179d0ad202dSahrens ASSERT(newval > 0);
180503ad85cSMatthew Ahrens ASSERT(newval <= spa_max_replication(os->os_spa));
181d0ad202dSahrens
182503ad85cSMatthew Ahrens os->os_copies = newval;
183d0ad202dSahrens }
184d0ad202dSahrens
1853baa08fcSek110237 static void
dedup_changed_cb(void * arg,uint64_t newval)186b24ab676SJeff Bonwick dedup_changed_cb(void *arg, uint64_t newval)
187b24ab676SJeff Bonwick {
188b24ab676SJeff Bonwick objset_t *os = arg;
189b24ab676SJeff Bonwick spa_t *spa = os->os_spa;
190b24ab676SJeff Bonwick enum zio_checksum checksum;
191b24ab676SJeff Bonwick
192b24ab676SJeff Bonwick /*
193b24ab676SJeff Bonwick * Inheritance should have been done by now.
194b24ab676SJeff Bonwick */
195b24ab676SJeff Bonwick ASSERT(newval != ZIO_CHECKSUM_INHERIT);
196b24ab676SJeff Bonwick
197b24ab676SJeff Bonwick checksum = zio_checksum_dedup_select(spa, newval, ZIO_CHECKSUM_OFF);
198b24ab676SJeff Bonwick
199b24ab676SJeff Bonwick os->os_dedup_checksum = checksum & ZIO_CHECKSUM_MASK;
200b24ab676SJeff Bonwick os->os_dedup_verify = !!(checksum & ZIO_CHECKSUM_VERIFY);
201b24ab676SJeff Bonwick }
202b24ab676SJeff Bonwick
203b24ab676SJeff Bonwick static void
primary_cache_changed_cb(void * arg,uint64_t newval)2043baa08fcSek110237 primary_cache_changed_cb(void *arg, uint64_t newval)
2053baa08fcSek110237 {
206503ad85cSMatthew Ahrens objset_t *os = arg;
2073baa08fcSek110237
2083baa08fcSek110237 /*
2093baa08fcSek110237 * Inheritance and range checking should have been done by now.
2103baa08fcSek110237 */
2113baa08fcSek110237 ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
2123baa08fcSek110237 newval == ZFS_CACHE_METADATA);
2133baa08fcSek110237
214503ad85cSMatthew Ahrens os->os_primary_cache = newval;
2153baa08fcSek110237 }
2163baa08fcSek110237
2173baa08fcSek110237 static void
secondary_cache_changed_cb(void * arg,uint64_t newval)2183baa08fcSek110237 secondary_cache_changed_cb(void *arg, uint64_t newval)
2193baa08fcSek110237 {
220503ad85cSMatthew Ahrens objset_t *os = arg;
2213baa08fcSek110237
2223baa08fcSek110237 /*
2233baa08fcSek110237 * Inheritance and range checking should have been done by now.
2243baa08fcSek110237 */
2253baa08fcSek110237 ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
2263baa08fcSek110237 newval == ZFS_CACHE_METADATA);
2273baa08fcSek110237
228503ad85cSMatthew Ahrens os->os_secondary_cache = newval;
2293baa08fcSek110237 }
2303baa08fcSek110237
231e09fa4daSNeil Perrin static void
sync_changed_cb(void * arg,uint64_t newval)23255da60b9SMark J Musante sync_changed_cb(void *arg, uint64_t newval)
23355da60b9SMark J Musante {
23455da60b9SMark J Musante objset_t *os = arg;
23555da60b9SMark J Musante
23655da60b9SMark J Musante /*
23755da60b9SMark J Musante * Inheritance and range checking should have been done by now.
23855da60b9SMark J Musante */
23955da60b9SMark J Musante ASSERT(newval == ZFS_SYNC_STANDARD || newval == ZFS_SYNC_ALWAYS ||
24055da60b9SMark J Musante newval == ZFS_SYNC_DISABLED);
24155da60b9SMark J Musante
24255da60b9SMark J Musante os->os_sync = newval;
24355da60b9SMark J Musante if (os->os_zil)
24455da60b9SMark J Musante zil_set_sync(os->os_zil, newval);
24555da60b9SMark J Musante }
24655da60b9SMark J Musante
24755da60b9SMark J Musante static void
redundant_metadata_changed_cb(void * arg,uint64_t newval)248469a3d62SMatthew Ahrens redundant_metadata_changed_cb(void *arg, uint64_t newval)
249469a3d62SMatthew Ahrens {
250469a3d62SMatthew Ahrens objset_t *os = arg;
251469a3d62SMatthew Ahrens
252469a3d62SMatthew Ahrens /*
253469a3d62SMatthew Ahrens * Inheritance and range checking should have been done by now.
254469a3d62SMatthew Ahrens */
255469a3d62SMatthew Ahrens ASSERT(newval == ZFS_REDUNDANT_METADATA_ALL ||
256469a3d62SMatthew Ahrens newval == ZFS_REDUNDANT_METADATA_MOST);
257469a3d62SMatthew Ahrens
258469a3d62SMatthew Ahrens os->os_redundant_metadata = newval;
259469a3d62SMatthew Ahrens }
260469a3d62SMatthew Ahrens
261469a3d62SMatthew Ahrens static void
logbias_changed_cb(void * arg,uint64_t newval)262e09fa4daSNeil Perrin logbias_changed_cb(void *arg, uint64_t newval)
263e09fa4daSNeil Perrin {
264e09fa4daSNeil Perrin objset_t *os = arg;
265e09fa4daSNeil Perrin
266e09fa4daSNeil Perrin ASSERT(newval == ZFS_LOGBIAS_LATENCY ||
267e09fa4daSNeil Perrin newval == ZFS_LOGBIAS_THROUGHPUT);
268e09fa4daSNeil Perrin os->os_logbias = newval;
269e09fa4daSNeil Perrin if (os->os_zil)
270e09fa4daSNeil Perrin zil_set_logbias(os->os_zil, newval);
271e09fa4daSNeil Perrin }
272e09fa4daSNeil Perrin
273d1a98260SMatthew Ahrens static void
recordsize_changed_cb(void * arg,uint64_t newval)274d1a98260SMatthew Ahrens recordsize_changed_cb(void *arg, uint64_t newval)
275d1a98260SMatthew Ahrens {
276d1a98260SMatthew Ahrens objset_t *os = arg;
277d1a98260SMatthew Ahrens
278d1a98260SMatthew Ahrens os->os_recordsize = newval;
279d1a98260SMatthew Ahrens }
280d1a98260SMatthew Ahrens
281fa9e4066Sahrens void
dmu_objset_byteswap(void * buf,size_t size)282fa9e4066Sahrens dmu_objset_byteswap(void *buf, size_t size)
283fa9e4066Sahrens {
284fa9e4066Sahrens objset_phys_t *osp = buf;
285fa9e4066Sahrens
28614843421SMatthew Ahrens ASSERT(size == OBJSET_OLD_PHYS_SIZE || size == sizeof (objset_phys_t));
287fa9e4066Sahrens dnode_byteswap(&osp->os_meta_dnode);
288fa9e4066Sahrens byteswap_uint64_array(&osp->os_zil_header, sizeof (zil_header_t));
289fa9e4066Sahrens osp->os_type = BSWAP_64(osp->os_type);
29014843421SMatthew Ahrens osp->os_flags = BSWAP_64(osp->os_flags);
29114843421SMatthew Ahrens if (size == sizeof (objset_phys_t)) {
29214843421SMatthew Ahrens dnode_byteswap(&osp->os_userused_dnode);
29314843421SMatthew Ahrens dnode_byteswap(&osp->os_groupused_dnode);
29414843421SMatthew Ahrens }
295fa9e4066Sahrens }
296fa9e4066Sahrens
297ea8dc4b6Seschrock int
dmu_objset_open_impl(spa_t * spa,dsl_dataset_t * ds,blkptr_t * bp,objset_t ** osp)298ea8dc4b6Seschrock dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
299503ad85cSMatthew Ahrens objset_t **osp)
300fa9e4066Sahrens {
301503ad85cSMatthew Ahrens objset_t *os;
302088f3894Sahrens int i, err;
303fa9e4066Sahrens
30491ebeef5Sahrens ASSERT(ds == NULL || MUTEX_HELD(&ds->ds_opening_lock));
30591ebeef5Sahrens
306503ad85cSMatthew Ahrens os = kmem_zalloc(sizeof (objset_t), KM_SLEEP);
307503ad85cSMatthew Ahrens os->os_dsl_dataset = ds;
308503ad85cSMatthew Ahrens os->os_spa = spa;
309503ad85cSMatthew Ahrens os->os_rootbp = bp;
310503ad85cSMatthew Ahrens if (!BP_IS_HOLE(os->os_rootbp)) {
31153c4e354SGeorge Wilson arc_flags_t aflags = ARC_FLAG_WAIT;
31205d95d03SMatthew Ahrens zbookmark_phys_t zb;
313b24ab676SJeff Bonwick SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET,
314b24ab676SJeff Bonwick ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
315b24ab676SJeff Bonwick
316503ad85cSMatthew Ahrens if (DMU_OS_IS_L2CACHEABLE(os))
31753c4e354SGeorge Wilson aflags |= ARC_FLAG_L2CACHE;
318aad02571SSaso Kiselkov if (DMU_OS_IS_L2COMPRESSIBLE(os))
31953c4e354SGeorge Wilson aflags |= ARC_FLAG_L2COMPRESS;
320ea8dc4b6Seschrock
321503ad85cSMatthew Ahrens dprintf_bp(os->os_rootbp, "reading %s", "");
3221b912ec7SGeorge Wilson err = arc_read(NULL, spa, os->os_rootbp,
323503ad85cSMatthew Ahrens arc_getbuf_func, &os->os_phys_buf,
32413506d1eSmaybee ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &aflags, &zb);
3253b2aab18SMatthew Ahrens if (err != 0) {
326503ad85cSMatthew Ahrens kmem_free(os, sizeof (objset_t));
327b87f3af3Sperrin /* convert checksum errors into IO errors */
328b87f3af3Sperrin if (err == ECKSUM)
329be6fd75aSMatthew Ahrens err = SET_ERROR(EIO);
330ea8dc4b6Seschrock return (err);
331ea8dc4b6Seschrock }
33214843421SMatthew Ahrens
33314843421SMatthew Ahrens /* Increase the blocksize if we are permitted. */
33414843421SMatthew Ahrens if (spa_version(spa) >= SPA_VERSION_USERSPACE &&
335503ad85cSMatthew Ahrens arc_buf_size(os->os_phys_buf) < sizeof (objset_phys_t)) {
33614843421SMatthew Ahrens arc_buf_t *buf = arc_buf_alloc(spa,
337503ad85cSMatthew Ahrens sizeof (objset_phys_t), &os->os_phys_buf,
33814843421SMatthew Ahrens ARC_BUFC_METADATA);
33914843421SMatthew Ahrens bzero(buf->b_data, sizeof (objset_phys_t));
340503ad85cSMatthew Ahrens bcopy(os->os_phys_buf->b_data, buf->b_data,
341503ad85cSMatthew Ahrens arc_buf_size(os->os_phys_buf));
342503ad85cSMatthew Ahrens (void) arc_buf_remove_ref(os->os_phys_buf,
343503ad85cSMatthew Ahrens &os->os_phys_buf);
344503ad85cSMatthew Ahrens os->os_phys_buf = buf;
34514843421SMatthew Ahrens }
34614843421SMatthew Ahrens
347503ad85cSMatthew Ahrens os->os_phys = os->os_phys_buf->b_data;
348503ad85cSMatthew Ahrens os->os_flags = os->os_phys->os_flags;
349fa9e4066Sahrens } else {
35014843421SMatthew Ahrens int size = spa_version(spa) >= SPA_VERSION_USERSPACE ?
35114843421SMatthew Ahrens sizeof (objset_phys_t) : OBJSET_OLD_PHYS_SIZE;
352503ad85cSMatthew Ahrens os->os_phys_buf = arc_buf_alloc(spa, size,
353503ad85cSMatthew Ahrens &os->os_phys_buf, ARC_BUFC_METADATA);
354503ad85cSMatthew Ahrens os->os_phys = os->os_phys_buf->b_data;
355503ad85cSMatthew Ahrens bzero(os->os_phys, size);
356fa9e4066Sahrens }
357fa9e4066Sahrens
358fa9e4066Sahrens /*
359fa9e4066Sahrens * Note: the changed_cb will be called once before the register
360fa9e4066Sahrens * func returns, thus changing the checksum/compression from the
3613baa08fcSek110237 * default (fletcher2/off). Snapshots don't need to know about
3623baa08fcSek110237 * checksum/compression/copies.
363fa9e4066Sahrens */
364e54e0be9SMatthew Ahrens if (ds != NULL) {
3658429b235SMatthew Ahrens boolean_t needlock = B_FALSE;
3668429b235SMatthew Ahrens
3678429b235SMatthew Ahrens /*
3688429b235SMatthew Ahrens * Note: it's valid to open the objset if the dataset is
3698429b235SMatthew Ahrens * long-held, in which case the pool_config lock will not
3708429b235SMatthew Ahrens * be held.
3718429b235SMatthew Ahrens */
3728429b235SMatthew Ahrens if (!dsl_pool_config_held(dmu_objset_pool(os))) {
3738429b235SMatthew Ahrens needlock = B_TRUE;
3748429b235SMatthew Ahrens dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
3758429b235SMatthew Ahrens }
3763b2aab18SMatthew Ahrens err = dsl_prop_register(ds,
3773b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_PRIMARYCACHE),
378503ad85cSMatthew Ahrens primary_cache_changed_cb, os);
3793b2aab18SMatthew Ahrens if (err == 0) {
3803b2aab18SMatthew Ahrens err = dsl_prop_register(ds,
3813b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_SECONDARYCACHE),
382503ad85cSMatthew Ahrens secondary_cache_changed_cb, os);
3833b2aab18SMatthew Ahrens }
3840521c5ebSJustin Gibbs if (!ds->ds_is_snapshot) {
3853b2aab18SMatthew Ahrens if (err == 0) {
3863b2aab18SMatthew Ahrens err = dsl_prop_register(ds,
3873b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_CHECKSUM),
388503ad85cSMatthew Ahrens checksum_changed_cb, os);
3893b2aab18SMatthew Ahrens }
3903b2aab18SMatthew Ahrens if (err == 0) {
3913b2aab18SMatthew Ahrens err = dsl_prop_register(ds,
3923b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_COMPRESSION),
393503ad85cSMatthew Ahrens compression_changed_cb, os);
3943b2aab18SMatthew Ahrens }
3953b2aab18SMatthew Ahrens if (err == 0) {
3963b2aab18SMatthew Ahrens err = dsl_prop_register(ds,
3973b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_COPIES),
398503ad85cSMatthew Ahrens copies_changed_cb, os);
3993b2aab18SMatthew Ahrens }
4003b2aab18SMatthew Ahrens if (err == 0) {
4013b2aab18SMatthew Ahrens err = dsl_prop_register(ds,
4023b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_DEDUP),
403b24ab676SJeff Bonwick dedup_changed_cb, os);
4043b2aab18SMatthew Ahrens }
4053b2aab18SMatthew Ahrens if (err == 0) {
4063b2aab18SMatthew Ahrens err = dsl_prop_register(ds,
4073b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_LOGBIAS),
408e09fa4daSNeil Perrin logbias_changed_cb, os);
4093b2aab18SMatthew Ahrens }
4103b2aab18SMatthew Ahrens if (err == 0) {
4113b2aab18SMatthew Ahrens err = dsl_prop_register(ds,
4123b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_SYNC),
41355da60b9SMark J Musante sync_changed_cb, os);
4143baa08fcSek110237 }
415469a3d62SMatthew Ahrens if (err == 0) {
416469a3d62SMatthew Ahrens err = dsl_prop_register(ds,
417469a3d62SMatthew Ahrens zfs_prop_to_name(
418469a3d62SMatthew Ahrens ZFS_PROP_REDUNDANT_METADATA),
419469a3d62SMatthew Ahrens redundant_metadata_changed_cb, os);
420469a3d62SMatthew Ahrens }
421d1a98260SMatthew Ahrens if (err == 0) {
422d1a98260SMatthew Ahrens err = dsl_prop_register(ds,
423d1a98260SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_RECORDSIZE),
424d1a98260SMatthew Ahrens recordsize_changed_cb, os);
425d1a98260SMatthew Ahrens }
4263b2aab18SMatthew Ahrens }
4278429b235SMatthew Ahrens if (needlock)
4288429b235SMatthew Ahrens dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
4293b2aab18SMatthew Ahrens if (err != 0) {
430503ad85cSMatthew Ahrens VERIFY(arc_buf_remove_ref(os->os_phys_buf,
4313b2aab18SMatthew Ahrens &os->os_phys_buf));
432503ad85cSMatthew Ahrens kmem_free(os, sizeof (objset_t));
433ea8dc4b6Seschrock return (err);
434ea8dc4b6Seschrock }
435e54e0be9SMatthew Ahrens } else {
436fa9e4066Sahrens /* It's the meta-objset. */
437503ad85cSMatthew Ahrens os->os_checksum = ZIO_CHECKSUM_FLETCHER_4;
4384454ba50SJustin T. Gibbs os->os_compress = ZIO_COMPRESS_ON;
439503ad85cSMatthew Ahrens os->os_copies = spa_max_replication(spa);
440b24ab676SJeff Bonwick os->os_dedup_checksum = ZIO_CHECKSUM_OFF;
441469a3d62SMatthew Ahrens os->os_dedup_verify = B_FALSE;
442469a3d62SMatthew Ahrens os->os_logbias = ZFS_LOGBIAS_LATENCY;
443469a3d62SMatthew Ahrens os->os_sync = ZFS_SYNC_STANDARD;
444503ad85cSMatthew Ahrens os->os_primary_cache = ZFS_CACHE_ALL;
445503ad85cSMatthew Ahrens os->os_secondary_cache = ZFS_CACHE_ALL;
446fa9e4066Sahrens }
447fa9e4066Sahrens
4480521c5ebSJustin Gibbs if (ds == NULL || !ds->ds_is_snapshot)
449503ad85cSMatthew Ahrens os->os_zil_header = os->os_phys->os_zil_header;
450503ad85cSMatthew Ahrens os->os_zil = zil_alloc(os, &os->os_zil_header);
451fa9e4066Sahrens
452fa9e4066Sahrens for (i = 0; i < TXG_SIZE; i++) {
453503ad85cSMatthew Ahrens list_create(&os->os_dirty_dnodes[i], sizeof (dnode_t),
454fa9e4066Sahrens offsetof(dnode_t, dn_dirty_link[i]));
455503ad85cSMatthew Ahrens list_create(&os->os_free_dnodes[i], sizeof (dnode_t),
456fa9e4066Sahrens offsetof(dnode_t, dn_dirty_link[i]));
457fa9e4066Sahrens }
458503ad85cSMatthew Ahrens list_create(&os->os_dnodes, sizeof (dnode_t),
459fa9e4066Sahrens offsetof(dnode_t, dn_link));
460503ad85cSMatthew Ahrens list_create(&os->os_downgraded_dbufs, sizeof (dmu_buf_impl_t),
461fa9e4066Sahrens offsetof(dmu_buf_impl_t, db_link));
462fa9e4066Sahrens
463503ad85cSMatthew Ahrens mutex_init(&os->os_lock, NULL, MUTEX_DEFAULT, NULL);
464503ad85cSMatthew Ahrens mutex_init(&os->os_obj_lock, NULL, MUTEX_DEFAULT, NULL);
465503ad85cSMatthew Ahrens mutex_init(&os->os_user_ptr_lock, NULL, MUTEX_DEFAULT, NULL);
4665ad82045Snd150628
4670521c5ebSJustin Gibbs dnode_special_open(os, &os->os_phys->os_meta_dnode,
4680521c5ebSJustin Gibbs DMU_META_DNODE_OBJECT, &os->os_meta_dnode);
469503ad85cSMatthew Ahrens if (arc_buf_size(os->os_phys_buf) >= sizeof (objset_phys_t)) {
4700521c5ebSJustin Gibbs dnode_special_open(os, &os->os_phys->os_userused_dnode,
4710521c5ebSJustin Gibbs DMU_USERUSED_OBJECT, &os->os_userused_dnode);
4720521c5ebSJustin Gibbs dnode_special_open(os, &os->os_phys->os_groupused_dnode,
4730521c5ebSJustin Gibbs DMU_GROUPUSED_OBJECT, &os->os_groupused_dnode);
47414843421SMatthew Ahrens }
475fa9e4066Sahrens
476503ad85cSMatthew Ahrens *osp = os;
4773cb34c60Sahrens return (0);
4783cb34c60Sahrens }
4793cb34c60Sahrens
4803cb34c60Sahrens int
dmu_objset_from_ds(dsl_dataset_t * ds,objset_t ** osp)481503ad85cSMatthew Ahrens dmu_objset_from_ds(dsl_dataset_t *ds, objset_t **osp)
4823cb34c60Sahrens {
483503ad85cSMatthew Ahrens int err = 0;
4843cb34c60Sahrens
4858429b235SMatthew Ahrens /*
4868429b235SMatthew Ahrens * We shouldn't be doing anything with dsl_dataset_t's unless the
4878429b235SMatthew Ahrens * pool_config lock is held, or the dataset is long-held.
4888429b235SMatthew Ahrens */
4898429b235SMatthew Ahrens ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool) ||
4908429b235SMatthew Ahrens dsl_dataset_long_held(ds));
4918429b235SMatthew Ahrens
492503ad85cSMatthew Ahrens mutex_enter(&ds->ds_opening_lock);
493e54e0be9SMatthew Ahrens if (ds->ds_objset == NULL) {
494e54e0be9SMatthew Ahrens objset_t *os;
495503ad85cSMatthew Ahrens err = dmu_objset_open_impl(dsl_dataset_get_spa(ds),
496e54e0be9SMatthew Ahrens ds, dsl_dataset_get_blkptr(ds), &os);
497e54e0be9SMatthew Ahrens
498e54e0be9SMatthew Ahrens if (err == 0) {
499e54e0be9SMatthew Ahrens mutex_enter(&ds->ds_lock);
500e54e0be9SMatthew Ahrens ASSERT(ds->ds_objset == NULL);
501e54e0be9SMatthew Ahrens ds->ds_objset = os;
502e54e0be9SMatthew Ahrens mutex_exit(&ds->ds_lock);
503503ad85cSMatthew Ahrens }
504e54e0be9SMatthew Ahrens }
505e54e0be9SMatthew Ahrens *osp = ds->ds_objset;
506503ad85cSMatthew Ahrens mutex_exit(&ds->ds_opening_lock);
5073cb34c60Sahrens return (err);
5083cb34c60Sahrens }
5093cb34c60Sahrens
5103b2aab18SMatthew Ahrens /*
5113b2aab18SMatthew Ahrens * Holds the pool while the objset is held. Therefore only one objset
5123b2aab18SMatthew Ahrens * can be held at a time.
5133b2aab18SMatthew Ahrens */
514fa9e4066Sahrens int
dmu_objset_hold(const char * name,void * tag,objset_t ** osp)515503ad85cSMatthew Ahrens dmu_objset_hold(const char *name, void *tag, objset_t **osp)
516fa9e4066Sahrens {
5173b2aab18SMatthew Ahrens dsl_pool_t *dp;
518f18faf3fSek110237 dsl_dataset_t *ds;
519f18faf3fSek110237 int err;
520fa9e4066Sahrens
5213b2aab18SMatthew Ahrens err = dsl_pool_hold(name, tag, &dp);
5223b2aab18SMatthew Ahrens if (err != 0)
523503ad85cSMatthew Ahrens return (err);
5243b2aab18SMatthew Ahrens err = dsl_dataset_hold(dp, name, tag, &ds);
5253b2aab18SMatthew Ahrens if (err != 0) {
5263b2aab18SMatthew Ahrens dsl_pool_rele(dp, tag);
5273b2aab18SMatthew Ahrens return (err);
5283b2aab18SMatthew Ahrens }
5293cb34c60Sahrens
530503ad85cSMatthew Ahrens err = dmu_objset_from_ds(ds, osp);
5313b2aab18SMatthew Ahrens if (err != 0) {
532503ad85cSMatthew Ahrens dsl_dataset_rele(ds, tag);
5333b2aab18SMatthew Ahrens dsl_pool_rele(dp, tag);
5343b2aab18SMatthew Ahrens }
535503ad85cSMatthew Ahrens
536fa9e4066Sahrens return (err);
537fa9e4066Sahrens }
538fa9e4066Sahrens
539416c5cd3SArne Jansen static int
dmu_objset_own_impl(dsl_dataset_t * ds,dmu_objset_type_t type,boolean_t readonly,void * tag,objset_t ** osp)540416c5cd3SArne Jansen dmu_objset_own_impl(dsl_dataset_t *ds, dmu_objset_type_t type,
541416c5cd3SArne Jansen boolean_t readonly, void *tag, objset_t **osp)
542416c5cd3SArne Jansen {
543416c5cd3SArne Jansen int err;
544416c5cd3SArne Jansen
545416c5cd3SArne Jansen err = dmu_objset_from_ds(ds, osp);
546416c5cd3SArne Jansen if (err != 0) {
547416c5cd3SArne Jansen dsl_dataset_disown(ds, tag);
548416c5cd3SArne Jansen } else if (type != DMU_OST_ANY && type != (*osp)->os_phys->os_type) {
549416c5cd3SArne Jansen dsl_dataset_disown(ds, tag);
550416c5cd3SArne Jansen return (SET_ERROR(EINVAL));
551416c5cd3SArne Jansen } else if (!readonly && dsl_dataset_is_snapshot(ds)) {
552416c5cd3SArne Jansen dsl_dataset_disown(ds, tag);
553416c5cd3SArne Jansen return (SET_ERROR(EROFS));
554416c5cd3SArne Jansen }
555416c5cd3SArne Jansen return (err);
556416c5cd3SArne Jansen }
557416c5cd3SArne Jansen
5583b2aab18SMatthew Ahrens /*
5593b2aab18SMatthew Ahrens * dsl_pool must not be held when this is called.
5603b2aab18SMatthew Ahrens * Upon successful return, there will be a longhold on the dataset,
5613b2aab18SMatthew Ahrens * and the dsl_pool will not be held.
5623b2aab18SMatthew Ahrens */
563503ad85cSMatthew Ahrens int
dmu_objset_own(const char * name,dmu_objset_type_t type,boolean_t readonly,void * tag,objset_t ** osp)564503ad85cSMatthew Ahrens dmu_objset_own(const char *name, dmu_objset_type_t type,
565503ad85cSMatthew Ahrens boolean_t readonly, void *tag, objset_t **osp)
566503ad85cSMatthew Ahrens {
5673b2aab18SMatthew Ahrens dsl_pool_t *dp;
568503ad85cSMatthew Ahrens dsl_dataset_t *ds;
569503ad85cSMatthew Ahrens int err;
570503ad85cSMatthew Ahrens
5713b2aab18SMatthew Ahrens err = dsl_pool_hold(name, FTAG, &dp);
5723b2aab18SMatthew Ahrens if (err != 0)
573503ad85cSMatthew Ahrens return (err);
5743b2aab18SMatthew Ahrens err = dsl_dataset_own(dp, name, tag, &ds);
5753b2aab18SMatthew Ahrens if (err != 0) {
5763b2aab18SMatthew Ahrens dsl_pool_rele(dp, FTAG);
5773b2aab18SMatthew Ahrens return (err);
5783b2aab18SMatthew Ahrens }
579416c5cd3SArne Jansen err = dmu_objset_own_impl(ds, type, readonly, tag, osp);
5803b2aab18SMatthew Ahrens dsl_pool_rele(dp, FTAG);
581416c5cd3SArne Jansen
5823cb34c60Sahrens return (err);
583fa9e4066Sahrens }
584fa9e4066Sahrens
585416c5cd3SArne Jansen 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)586416c5cd3SArne Jansen dmu_objset_own_obj(dsl_pool_t *dp, uint64_t obj, dmu_objset_type_t type,
587416c5cd3SArne Jansen boolean_t readonly, void *tag, objset_t **osp)
588416c5cd3SArne Jansen {
589416c5cd3SArne Jansen dsl_dataset_t *ds;
590416c5cd3SArne Jansen int err;
591416c5cd3SArne Jansen
592416c5cd3SArne Jansen err = dsl_dataset_own_obj(dp, obj, tag, &ds);
593416c5cd3SArne Jansen if (err != 0)
594416c5cd3SArne Jansen return (err);
595416c5cd3SArne Jansen
596416c5cd3SArne Jansen return (dmu_objset_own_impl(ds, type, readonly, tag, osp));
597416c5cd3SArne Jansen }
598416c5cd3SArne Jansen
599fa9e4066Sahrens void
dmu_objset_rele(objset_t * os,void * tag)600503ad85cSMatthew Ahrens dmu_objset_rele(objset_t *os, void *tag)
601fa9e4066Sahrens {
6023b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_objset_pool(os);
603503ad85cSMatthew Ahrens dsl_dataset_rele(os->os_dsl_dataset, tag);
6043b2aab18SMatthew Ahrens dsl_pool_rele(dp, tag);
605503ad85cSMatthew Ahrens }
606745cd3c5Smaybee
60791948b51SKeith M Wesolowski /*
60891948b51SKeith M Wesolowski * When we are called, os MUST refer to an objset associated with a dataset
60991948b51SKeith M Wesolowski * that is owned by 'tag'; that is, is held and long held by 'tag' and ds_owner
61091948b51SKeith M Wesolowski * == tag. We will then release and reacquire ownership of the dataset while
61191948b51SKeith M Wesolowski * holding the pool config_rwlock to avoid intervening namespace or ownership
61291948b51SKeith M Wesolowski * changes may occur.
61391948b51SKeith M Wesolowski *
61491948b51SKeith M Wesolowski * This exists solely to accommodate zfs_ioc_userspace_upgrade()'s desire to
61591948b51SKeith M Wesolowski * release the hold on its dataset and acquire a new one on the dataset of the
61691948b51SKeith M Wesolowski * same name so that it can be partially torn down and reconstructed.
61791948b51SKeith M Wesolowski */
61891948b51SKeith M Wesolowski void
dmu_objset_refresh_ownership(objset_t * os,void * tag)61991948b51SKeith M Wesolowski dmu_objset_refresh_ownership(objset_t *os, void *tag)
62091948b51SKeith M Wesolowski {
62191948b51SKeith M Wesolowski dsl_pool_t *dp;
62291948b51SKeith M Wesolowski dsl_dataset_t *ds, *newds;
623*675fc291SMatthew Ahrens char name[ZFS_MAX_DATASET_NAME_LEN];
62491948b51SKeith M Wesolowski
62591948b51SKeith M Wesolowski ds = os->os_dsl_dataset;
62691948b51SKeith M Wesolowski VERIFY3P(ds, !=, NULL);
62791948b51SKeith M Wesolowski VERIFY3P(ds->ds_owner, ==, tag);
62891948b51SKeith M Wesolowski VERIFY(dsl_dataset_long_held(ds));
62991948b51SKeith M Wesolowski
63091948b51SKeith M Wesolowski dsl_dataset_name(ds, name);
63191948b51SKeith M Wesolowski dp = dmu_objset_pool(os);
63291948b51SKeith M Wesolowski dsl_pool_config_enter(dp, FTAG);
63391948b51SKeith M Wesolowski dmu_objset_disown(os, tag);
63491948b51SKeith M Wesolowski VERIFY0(dsl_dataset_own(dp, name, tag, &newds));
63591948b51SKeith M Wesolowski VERIFY3P(newds, ==, os->os_dsl_dataset);
63691948b51SKeith M Wesolowski dsl_pool_config_exit(dp, FTAG);
63791948b51SKeith M Wesolowski }
63891948b51SKeith M Wesolowski
639503ad85cSMatthew Ahrens void
dmu_objset_disown(objset_t * os,void * tag)640503ad85cSMatthew Ahrens dmu_objset_disown(objset_t *os, void *tag)
641503ad85cSMatthew Ahrens {
642503ad85cSMatthew Ahrens dsl_dataset_disown(os->os_dsl_dataset, tag);
643fa9e4066Sahrens }
644fa9e4066Sahrens
6453b2aab18SMatthew Ahrens void
dmu_objset_evict_dbufs(objset_t * os)6461934e92fSmaybee dmu_objset_evict_dbufs(objset_t *os)
647ea8dc4b6Seschrock {
6480521c5ebSJustin Gibbs dnode_t dn_marker;
649ea8dc4b6Seschrock dnode_t *dn;
650c543ec06Sahrens
651503ad85cSMatthew Ahrens mutex_enter(&os->os_lock);
6520521c5ebSJustin Gibbs dn = list_head(&os->os_dnodes);
6530521c5ebSJustin Gibbs while (dn != NULL) {
654ea8dc4b6Seschrock /*
6550521c5ebSJustin Gibbs * Skip dnodes without holds. We have to do this dance
6560521c5ebSJustin Gibbs * because dnode_add_ref() only works if there is already a
6570521c5ebSJustin Gibbs * hold. If the dnode has no holds, then it has no dbufs.
658ea8dc4b6Seschrock */
6590521c5ebSJustin Gibbs if (dnode_add_ref(dn, FTAG)) {
6600521c5ebSJustin Gibbs list_insert_after(&os->os_dnodes, dn, &dn_marker);
661503ad85cSMatthew Ahrens mutex_exit(&os->os_lock);
6620521c5ebSJustin Gibbs
6631934e92fSmaybee dnode_evict_dbufs(dn);
664ea8dc4b6Seschrock dnode_rele(dn, FTAG);
6650521c5ebSJustin Gibbs
666503ad85cSMatthew Ahrens mutex_enter(&os->os_lock);
6670521c5ebSJustin Gibbs dn = list_next(&os->os_dnodes, &dn_marker);
6680521c5ebSJustin Gibbs list_remove(&os->os_dnodes, &dn_marker);
6690521c5ebSJustin Gibbs } else {
6700521c5ebSJustin Gibbs dn = list_next(&os->os_dnodes, dn);
6710521c5ebSJustin Gibbs }
672ea8dc4b6Seschrock }
673503ad85cSMatthew Ahrens mutex_exit(&os->os_lock);
6740521c5ebSJustin Gibbs
6750521c5ebSJustin Gibbs if (DMU_USERUSED_DNODE(os) != NULL) {
6760521c5ebSJustin Gibbs dnode_evict_dbufs(DMU_GROUPUSED_DNODE(os));
6770521c5ebSJustin Gibbs dnode_evict_dbufs(DMU_USERUSED_DNODE(os));
6780521c5ebSJustin Gibbs }
6790521c5ebSJustin Gibbs dnode_evict_dbufs(DMU_META_DNODE(os));
680ea8dc4b6Seschrock }
681ea8dc4b6Seschrock
6820521c5ebSJustin Gibbs /*
6830521c5ebSJustin Gibbs * Objset eviction processing is split into into two pieces.
6840521c5ebSJustin Gibbs * The first marks the objset as evicting, evicts any dbufs that
6850521c5ebSJustin Gibbs * have a refcount of zero, and then queues up the objset for the
6860521c5ebSJustin Gibbs * second phase of eviction. Once os->os_dnodes has been cleared by
6870521c5ebSJustin Gibbs * dnode_buf_pageout()->dnode_destroy(), the second phase is executed.
6880521c5ebSJustin Gibbs * The second phase closes the special dnodes, dequeues the objset from
6890521c5ebSJustin Gibbs * the list of those undergoing eviction, and finally frees the objset.
6900521c5ebSJustin Gibbs *
6910521c5ebSJustin Gibbs * NOTE: Due to asynchronous eviction processing (invocation of
6920521c5ebSJustin Gibbs * dnode_buf_pageout()), it is possible for the meta dnode for the
6930521c5ebSJustin Gibbs * objset to have no holds even though os->os_dnodes is not empty.
6940521c5ebSJustin Gibbs */
695ea8dc4b6Seschrock void
dmu_objset_evict(objset_t * os)696503ad85cSMatthew Ahrens dmu_objset_evict(objset_t *os)
697fa9e4066Sahrens {
698503ad85cSMatthew Ahrens dsl_dataset_t *ds = os->os_dsl_dataset;
699fa9e4066Sahrens
700b24ab676SJeff Bonwick for (int t = 0; t < TXG_SIZE; t++)
701b24ab676SJeff Bonwick ASSERT(!dmu_objset_is_dirty(os, t));
702fa9e4066Sahrens
7033baa08fcSek110237 if (ds) {
7040521c5ebSJustin Gibbs if (!ds->ds_is_snapshot) {
7053b2aab18SMatthew Ahrens VERIFY0(dsl_prop_unregister(ds,
7063b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_CHECKSUM),
707503ad85cSMatthew Ahrens checksum_changed_cb, os));
7083b2aab18SMatthew Ahrens VERIFY0(dsl_prop_unregister(ds,
7093b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_COMPRESSION),
710503ad85cSMatthew Ahrens compression_changed_cb, os));
7113b2aab18SMatthew Ahrens VERIFY0(dsl_prop_unregister(ds,
7123b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_COPIES),
713503ad85cSMatthew Ahrens copies_changed_cb, os));
7143b2aab18SMatthew Ahrens VERIFY0(dsl_prop_unregister(ds,
7153b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_DEDUP),
716b24ab676SJeff Bonwick dedup_changed_cb, os));
7173b2aab18SMatthew Ahrens VERIFY0(dsl_prop_unregister(ds,
7183b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_LOGBIAS),
719e09fa4daSNeil Perrin logbias_changed_cb, os));
7203b2aab18SMatthew Ahrens VERIFY0(dsl_prop_unregister(ds,
7213b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_SYNC),
72255da60b9SMark J Musante sync_changed_cb, os));
723469a3d62SMatthew Ahrens VERIFY0(dsl_prop_unregister(ds,
724469a3d62SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_REDUNDANT_METADATA),
725469a3d62SMatthew Ahrens redundant_metadata_changed_cb, os));
726d1a98260SMatthew Ahrens VERIFY0(dsl_prop_unregister(ds,
727d1a98260SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_RECORDSIZE),
728d1a98260SMatthew Ahrens recordsize_changed_cb, os));
729fa9e4066Sahrens }
7303b2aab18SMatthew Ahrens VERIFY0(dsl_prop_unregister(ds,
7313b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_PRIMARYCACHE),
732503ad85cSMatthew Ahrens primary_cache_changed_cb, os));
7333b2aab18SMatthew Ahrens VERIFY0(dsl_prop_unregister(ds,
7343b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_SECONDARYCACHE),
735503ad85cSMatthew Ahrens secondary_cache_changed_cb, os));
7363baa08fcSek110237 }
737fa9e4066Sahrens
7380a586ceaSMark Shellenbaum if (os->os_sa)
7390a586ceaSMark Shellenbaum sa_tear_down(os);
7400a586ceaSMark Shellenbaum
7413b2aab18SMatthew Ahrens dmu_objset_evict_dbufs(os);
742ea8dc4b6Seschrock
7430521c5ebSJustin Gibbs mutex_enter(&os->os_lock);
7440521c5ebSJustin Gibbs spa_evicting_os_register(os->os_spa, os);
7450521c5ebSJustin Gibbs if (list_is_empty(&os->os_dnodes)) {
7460521c5ebSJustin Gibbs mutex_exit(&os->os_lock);
7470521c5ebSJustin Gibbs dmu_objset_evict_done(os);
7480521c5ebSJustin Gibbs } else {
7490521c5ebSJustin Gibbs mutex_exit(&os->os_lock);
7500521c5ebSJustin Gibbs }
7510521c5ebSJustin Gibbs }
7520521c5ebSJustin Gibbs
7530521c5ebSJustin Gibbs void
dmu_objset_evict_done(objset_t * os)7540521c5ebSJustin Gibbs dmu_objset_evict_done(objset_t *os)
7550521c5ebSJustin Gibbs {
7560521c5ebSJustin Gibbs ASSERT3P(list_head(&os->os_dnodes), ==, NULL);
7570521c5ebSJustin Gibbs
758744947dcSTom Erickson dnode_special_close(&os->os_meta_dnode);
759744947dcSTom Erickson if (DMU_USERUSED_DNODE(os)) {
760744947dcSTom Erickson dnode_special_close(&os->os_userused_dnode);
761744947dcSTom Erickson dnode_special_close(&os->os_groupused_dnode);
76214843421SMatthew Ahrens }
763503ad85cSMatthew Ahrens zil_free(os->os_zil);
764fa9e4066Sahrens
7653b2aab18SMatthew Ahrens VERIFY(arc_buf_remove_ref(os->os_phys_buf, &os->os_phys_buf));
766744947dcSTom Erickson
767744947dcSTom Erickson /*
768744947dcSTom Erickson * This is a barrier to prevent the objset from going away in
769744947dcSTom Erickson * dnode_move() until we can safely ensure that the objset is still in
770744947dcSTom Erickson * use. We consider the objset valid before the barrier and invalid
771744947dcSTom Erickson * after the barrier.
772744947dcSTom Erickson */
773744947dcSTom Erickson rw_enter(&os_lock, RW_READER);
774744947dcSTom Erickson rw_exit(&os_lock);
775744947dcSTom Erickson
776503ad85cSMatthew Ahrens mutex_destroy(&os->os_lock);
777503ad85cSMatthew Ahrens mutex_destroy(&os->os_obj_lock);
778503ad85cSMatthew Ahrens mutex_destroy(&os->os_user_ptr_lock);
7790521c5ebSJustin Gibbs spa_evicting_os_deregister(os->os_spa, os);
780503ad85cSMatthew Ahrens kmem_free(os, sizeof (objset_t));
781fa9e4066Sahrens }
782fa9e4066Sahrens
78371eb0538SChris Kirby timestruc_t
dmu_objset_snap_cmtime(objset_t * os)78471eb0538SChris Kirby dmu_objset_snap_cmtime(objset_t *os)
78571eb0538SChris Kirby {
78671eb0538SChris Kirby return (dsl_dir_snap_cmtime(os->os_dsl_dataset->ds_dir));
78771eb0538SChris Kirby }
78871eb0538SChris Kirby
789fa9e4066Sahrens /* called from dsl for meta-objset */
790503ad85cSMatthew Ahrens 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)791c717a561Smaybee dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
792c717a561Smaybee dmu_objset_type_t type, dmu_tx_t *tx)
793fa9e4066Sahrens {
794503ad85cSMatthew Ahrens objset_t *os;
795fa9e4066Sahrens dnode_t *mdn;
796fa9e4066Sahrens
797fa9e4066Sahrens ASSERT(dmu_tx_is_syncing(tx));
7983b2aab18SMatthew Ahrens
799feaa74e4SMark Maybee if (ds != NULL)
8003b2aab18SMatthew Ahrens VERIFY0(dmu_objset_from_ds(ds, &os));
801feaa74e4SMark Maybee else
8023b2aab18SMatthew Ahrens VERIFY0(dmu_objset_open_impl(spa, NULL, bp, &os));
803feaa74e4SMark Maybee
804744947dcSTom Erickson mdn = DMU_META_DNODE(os);
805fa9e4066Sahrens
806fa9e4066Sahrens dnode_allocate(mdn, DMU_OT_DNODE, 1 << DNODE_BLOCK_SHIFT,
807fa9e4066Sahrens DN_MAX_INDBLKSHIFT, DMU_OT_NONE, 0, tx);
808fa9e4066Sahrens
809fa9e4066Sahrens /*
810fa9e4066Sahrens * We don't want to have to increase the meta-dnode's nlevels
811fa9e4066Sahrens * later, because then we could do it in quescing context while
812fa9e4066Sahrens * we are also accessing it in open context.
813fa9e4066Sahrens *
814fa9e4066Sahrens * This precaution is not necessary for the MOS (ds == NULL),
815fa9e4066Sahrens * because the MOS is only updated in syncing context.
816fa9e4066Sahrens * This is most fortunate: the MOS is the only objset that
817fa9e4066Sahrens * needs to be synced multiple times as spa_sync() iterates
818fa9e4066Sahrens * to convergence, so minimizing its dn_nlevels matters.
819fa9e4066Sahrens */
820ea8dc4b6Seschrock if (ds != NULL) {
821ea8dc4b6Seschrock int levels = 1;
822ea8dc4b6Seschrock
823ea8dc4b6Seschrock /*
824ea8dc4b6Seschrock * Determine the number of levels necessary for the meta-dnode
825ea8dc4b6Seschrock * to contain DN_MAX_OBJECT dnodes.
826ea8dc4b6Seschrock */
827ea8dc4b6Seschrock while ((uint64_t)mdn->dn_nblkptr << (mdn->dn_datablkshift +
828ea8dc4b6Seschrock (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) <
829ea8dc4b6Seschrock DN_MAX_OBJECT * sizeof (dnode_phys_t))
830ea8dc4b6Seschrock levels++;
831ea8dc4b6Seschrock
832fa9e4066Sahrens mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] =
833ea8dc4b6Seschrock mdn->dn_nlevels = levels;
834ea8dc4b6Seschrock }
835fa9e4066Sahrens
836fa9e4066Sahrens ASSERT(type != DMU_OST_NONE);
837fa9e4066Sahrens ASSERT(type != DMU_OST_ANY);
838fa9e4066Sahrens ASSERT(type < DMU_OST_NUMTYPES);
839503ad85cSMatthew Ahrens os->os_phys->os_type = type;
840503ad85cSMatthew Ahrens if (dmu_objset_userused_enabled(os)) {
841503ad85cSMatthew Ahrens os->os_phys->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
842503ad85cSMatthew Ahrens os->os_flags = os->os_phys->os_flags;
84314843421SMatthew Ahrens }
844fa9e4066Sahrens
845fa9e4066Sahrens dsl_dataset_dirty(ds, tx);
846fa9e4066Sahrens
847503ad85cSMatthew Ahrens return (os);
848fa9e4066Sahrens }
849fa9e4066Sahrens
8503b2aab18SMatthew Ahrens typedef struct dmu_objset_create_arg {
8513b2aab18SMatthew Ahrens const char *doca_name;
8523b2aab18SMatthew Ahrens cred_t *doca_cred;
8533b2aab18SMatthew Ahrens void (*doca_userfunc)(objset_t *os, void *arg,
8543b2aab18SMatthew Ahrens cred_t *cr, dmu_tx_t *tx);
8553b2aab18SMatthew Ahrens void *doca_userarg;
8563b2aab18SMatthew Ahrens dmu_objset_type_t doca_type;
8573b2aab18SMatthew Ahrens uint64_t doca_flags;
8583b2aab18SMatthew Ahrens } dmu_objset_create_arg_t;
859fa9e4066Sahrens
8601d452cf5Sahrens /*ARGSUSED*/
861fa9e4066Sahrens static int
dmu_objset_create_check(void * arg,dmu_tx_t * tx)8623b2aab18SMatthew Ahrens dmu_objset_create_check(void *arg, dmu_tx_t *tx)
863fa9e4066Sahrens {
8643b2aab18SMatthew Ahrens dmu_objset_create_arg_t *doca = arg;
8653b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx);
8663b2aab18SMatthew Ahrens dsl_dir_t *pdd;
8673b2aab18SMatthew Ahrens const char *tail;
8683b2aab18SMatthew Ahrens int error;
8691d452cf5Sahrens
8703b2aab18SMatthew Ahrens if (strchr(doca->doca_name, '@') != NULL)
871be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
8723b2aab18SMatthew Ahrens
873*675fc291SMatthew Ahrens if (strlen(doca->doca_name) >= ZFS_MAX_DATASET_NAME_LEN)
874*675fc291SMatthew Ahrens return (SET_ERROR(ENAMETOOLONG));
875*675fc291SMatthew Ahrens
8763b2aab18SMatthew Ahrens error = dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail);
8773b2aab18SMatthew Ahrens if (error != 0)
8783b2aab18SMatthew Ahrens return (error);
8793b2aab18SMatthew Ahrens if (tail == NULL) {
8803b2aab18SMatthew Ahrens dsl_dir_rele(pdd, FTAG);
881be6fd75aSMatthew Ahrens return (SET_ERROR(EEXIST));
8821d452cf5Sahrens }
883bc93cffdSJerry Jelinek error = dsl_fs_ss_limit_check(pdd, 1, ZFS_PROP_FILESYSTEM_LIMIT, NULL,
884bc93cffdSJerry Jelinek doca->doca_cred);
8853b2aab18SMatthew Ahrens dsl_dir_rele(pdd, FTAG);
886ecd6cf80Smarks
887bc93cffdSJerry Jelinek return (error);
8881d452cf5Sahrens }
8891d452cf5Sahrens
8901d452cf5Sahrens static void
dmu_objset_create_sync(void * arg,dmu_tx_t * tx)8913b2aab18SMatthew Ahrens dmu_objset_create_sync(void *arg, dmu_tx_t *tx)
8921d452cf5Sahrens {
8933b2aab18SMatthew Ahrens dmu_objset_create_arg_t *doca = arg;
8943b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx);
8953b2aab18SMatthew Ahrens dsl_dir_t *pdd;
8963b2aab18SMatthew Ahrens const char *tail;
8974445fffbSMatthew Ahrens dsl_dataset_t *ds;
8983b2aab18SMatthew Ahrens uint64_t obj;
8994445fffbSMatthew Ahrens blkptr_t *bp;
9003b2aab18SMatthew Ahrens objset_t *os;
901fa9e4066Sahrens
9023b2aab18SMatthew Ahrens VERIFY0(dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail));
903fa9e4066Sahrens
9043b2aab18SMatthew Ahrens obj = dsl_dataset_create_sync(pdd, tail, NULL, doca->doca_flags,
9053b2aab18SMatthew Ahrens doca->doca_cred, tx);
906fa9e4066Sahrens
9073b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(pdd->dd_pool, obj, FTAG, &ds));
908ae46e4c7SMatthew Ahrens bp = dsl_dataset_get_blkptr(ds);
9093b2aab18SMatthew Ahrens os = dmu_objset_create_impl(pdd->dd_pool->dp_spa,
9103b2aab18SMatthew Ahrens ds, bp, doca->doca_type, tx);
911fa9e4066Sahrens
9123b2aab18SMatthew Ahrens if (doca->doca_userfunc != NULL) {
9133b2aab18SMatthew Ahrens doca->doca_userfunc(os, doca->doca_userarg,
9143b2aab18SMatthew Ahrens doca->doca_cred, tx);
915feaa74e4SMark Maybee }
916ecd6cf80Smarks
9174445fffbSMatthew Ahrens spa_history_log_internal_ds(ds, "create", tx, "");
9184445fffbSMatthew Ahrens dsl_dataset_rele(ds, FTAG);
9193b2aab18SMatthew Ahrens dsl_dir_rele(pdd, FTAG);
920fa9e4066Sahrens }
921fa9e4066Sahrens
922fa9e4066Sahrens 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)923ae46e4c7SMatthew Ahrens dmu_objset_create(const char *name, dmu_objset_type_t type, uint64_t flags,
924ecd6cf80Smarks void (*func)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx), void *arg)
925fa9e4066Sahrens {
9263b2aab18SMatthew Ahrens dmu_objset_create_arg_t doca;
927fa9e4066Sahrens
9283b2aab18SMatthew Ahrens doca.doca_name = name;
9293b2aab18SMatthew Ahrens doca.doca_cred = CRED();
9303b2aab18SMatthew Ahrens doca.doca_flags = flags;
9313b2aab18SMatthew Ahrens doca.doca_userfunc = func;
9323b2aab18SMatthew Ahrens doca.doca_userarg = arg;
9333b2aab18SMatthew Ahrens doca.doca_type = type;
9343b2aab18SMatthew Ahrens
9353b2aab18SMatthew Ahrens return (dsl_sync_task(name,
93643d5e60eSMatthew Ahrens dmu_objset_create_check, dmu_objset_create_sync, &doca,
93743d5e60eSMatthew Ahrens 5, ZFS_SPACE_CHECK_NORMAL));
938fa9e4066Sahrens }
939fa9e4066Sahrens
9403b2aab18SMatthew Ahrens typedef struct dmu_objset_clone_arg {
9413b2aab18SMatthew Ahrens const char *doca_clone;
9423b2aab18SMatthew Ahrens const char *doca_origin;
9433b2aab18SMatthew Ahrens cred_t *doca_cred;
9443b2aab18SMatthew Ahrens } dmu_objset_clone_arg_t;
945ecd6cf80Smarks
9463b2aab18SMatthew Ahrens /*ARGSUSED*/
947ea2f5b9eSMatthew Ahrens static int
dmu_objset_clone_check(void * arg,dmu_tx_t * tx)9483b2aab18SMatthew Ahrens dmu_objset_clone_check(void *arg, dmu_tx_t *tx)
949ea2f5b9eSMatthew Ahrens {
9503b2aab18SMatthew Ahrens dmu_objset_clone_arg_t *doca = arg;
9513b2aab18SMatthew Ahrens dsl_dir_t *pdd;
9523b2aab18SMatthew Ahrens const char *tail;
95399d5e173STim Haley int error;
9543b2aab18SMatthew Ahrens dsl_dataset_t *origin;
9553b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx);
956ea2f5b9eSMatthew Ahrens
9573b2aab18SMatthew Ahrens if (strchr(doca->doca_clone, '@') != NULL)
958be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
959ea2f5b9eSMatthew Ahrens
960*675fc291SMatthew Ahrens if (strlen(doca->doca_clone) >= ZFS_MAX_DATASET_NAME_LEN)
961*675fc291SMatthew Ahrens return (SET_ERROR(ENAMETOOLONG));
962*675fc291SMatthew Ahrens
9633b2aab18SMatthew Ahrens error = dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail);
9643b2aab18SMatthew Ahrens if (error != 0)
96599d5e173STim Haley return (error);
9663b2aab18SMatthew Ahrens if (tail == NULL) {
9673b2aab18SMatthew Ahrens dsl_dir_rele(pdd, FTAG);
968be6fd75aSMatthew Ahrens return (SET_ERROR(EEXIST));
96999d5e173STim Haley }
970c7fed717SAlexander Eremin
971bc93cffdSJerry Jelinek error = dsl_fs_ss_limit_check(pdd, 1, ZFS_PROP_FILESYSTEM_LIMIT, NULL,
972bc93cffdSJerry Jelinek doca->doca_cred);
973bc93cffdSJerry Jelinek if (error != 0) {
974bc93cffdSJerry Jelinek dsl_dir_rele(pdd, FTAG);
975bc93cffdSJerry Jelinek return (SET_ERROR(EDQUOT));
976bc93cffdSJerry Jelinek }
9773b2aab18SMatthew Ahrens dsl_dir_rele(pdd, FTAG);
9783b2aab18SMatthew Ahrens
9793b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin);
9803b2aab18SMatthew Ahrens if (error != 0)
98199d5e173STim Haley return (error);
9823b2aab18SMatthew Ahrens
9833b2aab18SMatthew Ahrens /* You can only clone snapshots, not the head datasets. */
9840521c5ebSJustin Gibbs if (!origin->ds_is_snapshot) {
9853b2aab18SMatthew Ahrens dsl_dataset_rele(origin, FTAG);
986be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
9873b2aab18SMatthew Ahrens }
9883b2aab18SMatthew Ahrens dsl_dataset_rele(origin, FTAG);
9893b2aab18SMatthew Ahrens
9903b2aab18SMatthew Ahrens return (0);
991ea2f5b9eSMatthew Ahrens }
992ea2f5b9eSMatthew Ahrens
993ea2f5b9eSMatthew Ahrens static void
dmu_objset_clone_sync(void * arg,dmu_tx_t * tx)9943b2aab18SMatthew Ahrens dmu_objset_clone_sync(void *arg, dmu_tx_t *tx)
995ea2f5b9eSMatthew Ahrens {
9963b2aab18SMatthew Ahrens dmu_objset_clone_arg_t *doca = arg;
9973b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx);
9983b2aab18SMatthew Ahrens dsl_dir_t *pdd;
9993b2aab18SMatthew Ahrens const char *tail;
10003b2aab18SMatthew Ahrens dsl_dataset_t *origin, *ds;
10013b2aab18SMatthew Ahrens uint64_t obj;
1002*675fc291SMatthew Ahrens char namebuf[ZFS_MAX_DATASET_NAME_LEN];
1003ea2f5b9eSMatthew Ahrens
10043b2aab18SMatthew Ahrens VERIFY0(dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail));
10053b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin));
1006ea2f5b9eSMatthew Ahrens
10073b2aab18SMatthew Ahrens obj = dsl_dataset_create_sync(pdd, tail, origin, 0,
10083b2aab18SMatthew Ahrens doca->doca_cred, tx);
10093b2aab18SMatthew Ahrens
10103b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(pdd->dd_pool, obj, FTAG, &ds));
10113b2aab18SMatthew Ahrens dsl_dataset_name(origin, namebuf);
10123b2aab18SMatthew Ahrens spa_history_log_internal_ds(ds, "clone", tx,
10133b2aab18SMatthew Ahrens "origin=%s (%llu)", namebuf, origin->ds_object);
10143b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG);
10153b2aab18SMatthew Ahrens dsl_dataset_rele(origin, FTAG);
10163b2aab18SMatthew Ahrens dsl_dir_rele(pdd, FTAG);
101792241e0bSTom Erickson }
101899d5e173STim Haley
10191d452cf5Sahrens int
dmu_objset_clone(const char * clone,const char * origin)10203b2aab18SMatthew Ahrens dmu_objset_clone(const char *clone, const char *origin)
10211d452cf5Sahrens {
10223b2aab18SMatthew Ahrens dmu_objset_clone_arg_t doca;
10234445fffbSMatthew Ahrens
10243b2aab18SMatthew Ahrens doca.doca_clone = clone;
10253b2aab18SMatthew Ahrens doca.doca_origin = origin;
10263b2aab18SMatthew Ahrens doca.doca_cred = CRED();
10274445fffbSMatthew Ahrens
10283b2aab18SMatthew Ahrens return (dsl_sync_task(clone,
102943d5e60eSMatthew Ahrens dmu_objset_clone_check, dmu_objset_clone_sync, &doca,
103043d5e60eSMatthew Ahrens 5, ZFS_SPACE_CHECK_NORMAL));
10314445fffbSMatthew Ahrens }
10324445fffbSMatthew Ahrens
10334445fffbSMatthew Ahrens int
dmu_objset_snapshot_one(const char * fsname,const char * snapname)10344445fffbSMatthew Ahrens dmu_objset_snapshot_one(const char *fsname, const char *snapname)
10354445fffbSMatthew Ahrens {
10364445fffbSMatthew Ahrens int err;
10374445fffbSMatthew Ahrens char *longsnap = kmem_asprintf("%s@%s", fsname, snapname);
10384445fffbSMatthew Ahrens nvlist_t *snaps = fnvlist_alloc();
10394445fffbSMatthew Ahrens
10404445fffbSMatthew Ahrens fnvlist_add_boolean(snaps, longsnap);
10414445fffbSMatthew Ahrens strfree(longsnap);
10423b2aab18SMatthew Ahrens err = dsl_dataset_snapshot(snaps, NULL, NULL);
10433b2aab18SMatthew Ahrens fnvlist_free(snaps);
10444445fffbSMatthew Ahrens return (err);
10454445fffbSMatthew Ahrens }
10464445fffbSMatthew Ahrens
1047fa9e4066Sahrens static void
dmu_objset_sync_dnodes(list_t * list,list_t * newlist,dmu_tx_t * tx)104814843421SMatthew Ahrens dmu_objset_sync_dnodes(list_t *list, list_t *newlist, dmu_tx_t *tx)
1049fa9e4066Sahrens {
1050c717a561Smaybee dnode_t *dn;
1051fa9e4066Sahrens
1052c717a561Smaybee while (dn = list_head(list)) {
1053c717a561Smaybee ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
1054c717a561Smaybee ASSERT(dn->dn_dbuf->db_data_pending);
1055fa9e4066Sahrens /*
105614843421SMatthew Ahrens * Initialize dn_zio outside dnode_sync() because the
105714843421SMatthew Ahrens * meta-dnode needs to set it ouside dnode_sync().
1058fa9e4066Sahrens */
1059c717a561Smaybee dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio;
1060c717a561Smaybee ASSERT(dn->dn_zio);
1061c717a561Smaybee
1062c717a561Smaybee ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS);
1063c717a561Smaybee list_remove(list, dn);
106414843421SMatthew Ahrens
106514843421SMatthew Ahrens if (newlist) {
106614843421SMatthew Ahrens (void) dnode_add_ref(dn, newlist);
106714843421SMatthew Ahrens list_insert_tail(newlist, dn);
106814843421SMatthew Ahrens }
106914843421SMatthew Ahrens
1070c717a561Smaybee dnode_sync(dn, tx);
1071fa9e4066Sahrens }
1072fa9e4066Sahrens }
1073faafa6e3Sahrens
1074c717a561Smaybee /* ARGSUSED */
1075c717a561Smaybee static void
dmu_objset_write_ready(zio_t * zio,arc_buf_t * abuf,void * arg)1076b24ab676SJeff Bonwick dmu_objset_write_ready(zio_t *zio, arc_buf_t *abuf, void *arg)
1077c717a561Smaybee {
1078e14bb325SJeff Bonwick blkptr_t *bp = zio->io_bp;
1079503ad85cSMatthew Ahrens objset_t *os = arg;
1080c717a561Smaybee dnode_phys_t *dnp = &os->os_phys->os_meta_dnode;
1081faafa6e3Sahrens
1082e54e0be9SMatthew Ahrens ASSERT(!BP_IS_EMBEDDED(bp));
10833b2aab18SMatthew Ahrens ASSERT3P(bp, ==, os->os_rootbp);
10843b2aab18SMatthew Ahrens ASSERT3U(BP_GET_TYPE(bp), ==, DMU_OT_OBJSET);
10853b2aab18SMatthew Ahrens ASSERT0(BP_GET_LEVEL(bp));
10860a4e9518Sgw25295
1087c717a561Smaybee /*
108814843421SMatthew Ahrens * Update rootbp fill count: it should be the number of objects
108914843421SMatthew Ahrens * allocated in the object set (not counting the "special"
109014843421SMatthew Ahrens * objects that are stored in the objset_phys_t -- the meta
109114843421SMatthew Ahrens * dnode and user/group accounting objects).
1092c717a561Smaybee */
109314843421SMatthew Ahrens bp->blk_fill = 0;
1094e14bb325SJeff Bonwick for (int i = 0; i < dnp->dn_nblkptr; i++)
1095e54e0be9SMatthew Ahrens bp->blk_fill += BP_GET_FILL(&dnp->dn_blkptr[i]);
1096b24ab676SJeff Bonwick }
1097b24ab676SJeff Bonwick
1098b24ab676SJeff Bonwick /* ARGSUSED */
1099b24ab676SJeff Bonwick static void
dmu_objset_write_done(zio_t * zio,arc_buf_t * abuf,void * arg)1100b24ab676SJeff Bonwick dmu_objset_write_done(zio_t *zio, arc_buf_t *abuf, void *arg)
1101b24ab676SJeff Bonwick {
1102b24ab676SJeff Bonwick blkptr_t *bp = zio->io_bp;
1103b24ab676SJeff Bonwick blkptr_t *bp_orig = &zio->io_bp_orig;
1104b24ab676SJeff Bonwick objset_t *os = arg;
11050a4e9518Sgw25295
1106e14bb325SJeff Bonwick if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
1107b24ab676SJeff Bonwick ASSERT(BP_EQUAL(bp, bp_orig));
1108e14bb325SJeff Bonwick } else {
1109b24ab676SJeff Bonwick dsl_dataset_t *ds = os->os_dsl_dataset;
1110b24ab676SJeff Bonwick dmu_tx_t *tx = os->os_synctx;
1111b24ab676SJeff Bonwick
1112b24ab676SJeff Bonwick (void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
1113b24ab676SJeff Bonwick dsl_dataset_block_born(ds, bp, tx);
11140a4e9518Sgw25295 }
1115fa9e4066Sahrens }
1116fa9e4066Sahrens
1117fa9e4066Sahrens /* called from dsl */
1118fa9e4066Sahrens void
dmu_objset_sync(objset_t * os,zio_t * pio,dmu_tx_t * tx)1119503ad85cSMatthew Ahrens dmu_objset_sync(objset_t *os, zio_t *pio, dmu_tx_t *tx)
1120fa9e4066Sahrens {
1121fa9e4066Sahrens int txgoff;
112205d95d03SMatthew Ahrens zbookmark_phys_t zb;
1123b24ab676SJeff Bonwick zio_prop_t zp;
1124c717a561Smaybee zio_t *zio;
1125c717a561Smaybee list_t *list;
112614843421SMatthew Ahrens list_t *newlist = NULL;
1127c717a561Smaybee dbuf_dirty_record_t *dr;
1128fa9e4066Sahrens
1129fa9e4066Sahrens dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg);
1130fa9e4066Sahrens
1131c717a561Smaybee ASSERT(dmu_tx_is_syncing(tx));
1132c717a561Smaybee /* XXX the write_done callback should really give us the tx... */
1133c717a561Smaybee os->os_synctx = tx;
1134fa9e4066Sahrens
113587bd5c1eSahrens if (os->os_dsl_dataset == NULL) {
113687bd5c1eSahrens /*
113787bd5c1eSahrens * This is the MOS. If we have upgraded,
113887bd5c1eSahrens * spa_max_replication() could change, so reset
113987bd5c1eSahrens * os_copies here.
114087bd5c1eSahrens */
114187bd5c1eSahrens os->os_copies = spa_max_replication(os->os_spa);
114287bd5c1eSahrens }
114387bd5c1eSahrens
1144fa9e4066Sahrens /*
1145c717a561Smaybee * Create the root block IO
1146fa9e4066Sahrens */
1147b24ab676SJeff Bonwick SET_BOOKMARK(&zb, os->os_dsl_dataset ?
1148b24ab676SJeff Bonwick os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
1149b24ab676SJeff Bonwick ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
11501b912ec7SGeorge Wilson arc_release(os->os_phys_buf, &os->os_phys_buf);
1151b24ab676SJeff Bonwick
1152b24ab676SJeff Bonwick dmu_write_policy(os, NULL, 0, 0, &zp);
1153b24ab676SJeff Bonwick
1154b24ab676SJeff Bonwick zio = arc_write(pio, os->os_spa, tx->tx_txg,
1155aad02571SSaso Kiselkov os->os_rootbp, os->os_phys_buf, DMU_OS_IS_L2CACHEABLE(os),
1156aad02571SSaso Kiselkov DMU_OS_IS_L2COMPRESSIBLE(os), &zp, dmu_objset_write_ready,
115769962b56SMatthew Ahrens NULL, dmu_objset_write_done, os, ZIO_PRIORITY_ASYNC_WRITE,
1158aad02571SSaso Kiselkov ZIO_FLAG_MUSTSUCCEED, &zb);
1159fa9e4066Sahrens
1160c717a561Smaybee /*
116114843421SMatthew Ahrens * Sync special dnodes - the parent IO for the sync is the root block
1162c717a561Smaybee */
1163744947dcSTom Erickson DMU_META_DNODE(os)->dn_zio = zio;
1164744947dcSTom Erickson dnode_sync(DMU_META_DNODE(os), tx);
1165fa9e4066Sahrens
116614843421SMatthew Ahrens os->os_phys->os_flags = os->os_flags;
116714843421SMatthew Ahrens
1168744947dcSTom Erickson if (DMU_USERUSED_DNODE(os) &&
1169744947dcSTom Erickson DMU_USERUSED_DNODE(os)->dn_type != DMU_OT_NONE) {
1170744947dcSTom Erickson DMU_USERUSED_DNODE(os)->dn_zio = zio;
1171744947dcSTom Erickson dnode_sync(DMU_USERUSED_DNODE(os), tx);
1172744947dcSTom Erickson DMU_GROUPUSED_DNODE(os)->dn_zio = zio;
1173744947dcSTom Erickson dnode_sync(DMU_GROUPUSED_DNODE(os), tx);
117414843421SMatthew Ahrens }
117514843421SMatthew Ahrens
1176c717a561Smaybee txgoff = tx->tx_txg & TXG_MASK;
1177c717a561Smaybee
117814843421SMatthew Ahrens if (dmu_objset_userused_enabled(os)) {
117914843421SMatthew Ahrens newlist = &os->os_synced_dnodes;
118014843421SMatthew Ahrens /*
118114843421SMatthew Ahrens * We must create the list here because it uses the
118214843421SMatthew Ahrens * dn_dirty_link[] of this txg.
118314843421SMatthew Ahrens */
118414843421SMatthew Ahrens list_create(newlist, sizeof (dnode_t),
118514843421SMatthew Ahrens offsetof(dnode_t, dn_dirty_link[txgoff]));
118614843421SMatthew Ahrens }
118714843421SMatthew Ahrens
118814843421SMatthew Ahrens dmu_objset_sync_dnodes(&os->os_free_dnodes[txgoff], newlist, tx);
118914843421SMatthew Ahrens dmu_objset_sync_dnodes(&os->os_dirty_dnodes[txgoff], newlist, tx);
1190c717a561Smaybee
1191744947dcSTom Erickson list = &DMU_META_DNODE(os)->dn_dirty_records[txgoff];
1192c717a561Smaybee while (dr = list_head(list)) {
11933b2aab18SMatthew Ahrens ASSERT0(dr->dr_dbuf->db_level);
1194c717a561Smaybee list_remove(list, dr);
1195c717a561Smaybee if (dr->dr_zio)
1196c717a561Smaybee zio_nowait(dr->dr_zio);
1197c717a561Smaybee }
1198c717a561Smaybee /*
1199c717a561Smaybee * Free intent log blocks up to this tx.
1200c717a561Smaybee */
1201c717a561Smaybee zil_sync(os->os_zil, tx);
1202088f3894Sahrens os->os_phys->os_zil_header = os->os_zil_header;
1203c717a561Smaybee zio_nowait(zio);
1204fa9e4066Sahrens }
1205fa9e4066Sahrens
1206b24ab676SJeff Bonwick boolean_t
dmu_objset_is_dirty(objset_t * os,uint64_t txg)1207b24ab676SJeff Bonwick dmu_objset_is_dirty(objset_t *os, uint64_t txg)
1208b24ab676SJeff Bonwick {
1209b24ab676SJeff Bonwick return (!list_is_empty(&os->os_dirty_dnodes[txg & TXG_MASK]) ||
1210b24ab676SJeff Bonwick !list_is_empty(&os->os_free_dnodes[txg & TXG_MASK]));
1211b24ab676SJeff Bonwick }
1212b24ab676SJeff Bonwick
1213744947dcSTom Erickson static objset_used_cb_t *used_cbs[DMU_OST_NUMTYPES];
121414843421SMatthew Ahrens
121514843421SMatthew Ahrens void
dmu_objset_register_type(dmu_objset_type_t ost,objset_used_cb_t * cb)121614843421SMatthew Ahrens dmu_objset_register_type(dmu_objset_type_t ost, objset_used_cb_t *cb)
121714843421SMatthew Ahrens {
121814843421SMatthew Ahrens used_cbs[ost] = cb;
121914843421SMatthew Ahrens }
122014843421SMatthew Ahrens
122114843421SMatthew Ahrens boolean_t
dmu_objset_userused_enabled(objset_t * os)1222503ad85cSMatthew Ahrens dmu_objset_userused_enabled(objset_t *os)
122314843421SMatthew Ahrens {
122414843421SMatthew Ahrens return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE &&
1225744947dcSTom Erickson used_cbs[os->os_phys->os_type] != NULL &&
1226744947dcSTom Erickson DMU_USERUSED_DNODE(os) != NULL);
122714843421SMatthew Ahrens }
122814843421SMatthew Ahrens
12299966ca11SMatthew Ahrens static void
do_userquota_update(objset_t * os,uint64_t used,uint64_t flags,uint64_t user,uint64_t group,boolean_t subtract,dmu_tx_t * tx)12300a586ceaSMark Shellenbaum do_userquota_update(objset_t *os, uint64_t used, uint64_t flags,
12310a586ceaSMark Shellenbaum uint64_t user, uint64_t group, boolean_t subtract, dmu_tx_t *tx)
12329966ca11SMatthew Ahrens {
12330a586ceaSMark Shellenbaum if ((flags & DNODE_FLAG_USERUSED_ACCOUNTED)) {
12340a586ceaSMark Shellenbaum int64_t delta = DNODE_SIZE + used;
12359966ca11SMatthew Ahrens if (subtract)
12369966ca11SMatthew Ahrens delta = -delta;
1237b420f3adSRichard Lowe VERIFY3U(0, ==, zap_increment_int(os, DMU_USERUSED_OBJECT,
12389966ca11SMatthew Ahrens user, delta, tx));
1239b420f3adSRichard Lowe VERIFY3U(0, ==, zap_increment_int(os, DMU_GROUPUSED_OBJECT,
12409966ca11SMatthew Ahrens group, delta, tx));
12419966ca11SMatthew Ahrens }
12429966ca11SMatthew Ahrens }
12439966ca11SMatthew Ahrens
124414843421SMatthew Ahrens void
dmu_objset_do_userquota_updates(objset_t * os,dmu_tx_t * tx)12450a586ceaSMark Shellenbaum dmu_objset_do_userquota_updates(objset_t *os, dmu_tx_t *tx)
124614843421SMatthew Ahrens {
124714843421SMatthew Ahrens dnode_t *dn;
124814843421SMatthew Ahrens list_t *list = &os->os_synced_dnodes;
124914843421SMatthew Ahrens
125014843421SMatthew Ahrens ASSERT(list_head(list) == NULL || dmu_objset_userused_enabled(os));
125114843421SMatthew Ahrens
125214843421SMatthew Ahrens while (dn = list_head(list)) {
12531d8ccc7bSMark Shellenbaum int flags;
125414843421SMatthew Ahrens ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object));
125514843421SMatthew Ahrens ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE ||
125614843421SMatthew Ahrens dn->dn_phys->dn_flags &
125714843421SMatthew Ahrens DNODE_FLAG_USERUSED_ACCOUNTED);
125814843421SMatthew Ahrens
125914843421SMatthew Ahrens /* Allocate the user/groupused objects if necessary. */
1260744947dcSTom Erickson if (DMU_USERUSED_DNODE(os)->dn_type == DMU_OT_NONE) {
1261503ad85cSMatthew Ahrens VERIFY(0 == zap_create_claim(os,
126214843421SMatthew Ahrens DMU_USERUSED_OBJECT,
126314843421SMatthew Ahrens DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
1264503ad85cSMatthew Ahrens VERIFY(0 == zap_create_claim(os,
126514843421SMatthew Ahrens DMU_GROUPUSED_OBJECT,
126614843421SMatthew Ahrens DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
126714843421SMatthew Ahrens }
126814843421SMatthew Ahrens
126914843421SMatthew Ahrens /*
12709966ca11SMatthew Ahrens * We intentionally modify the zap object even if the
12710a586ceaSMark Shellenbaum * net delta is zero. Otherwise
12729966ca11SMatthew Ahrens * the block of the zap obj could be shared between
12739966ca11SMatthew Ahrens * datasets but need to be different between them after
12749966ca11SMatthew Ahrens * a bprewrite.
127514843421SMatthew Ahrens */
127614843421SMatthew Ahrens
12771d8ccc7bSMark Shellenbaum flags = dn->dn_id_flags;
12781d8ccc7bSMark Shellenbaum ASSERT(flags);
12791d8ccc7bSMark Shellenbaum if (flags & DN_ID_OLD_EXIST) {
12800a586ceaSMark Shellenbaum do_userquota_update(os, dn->dn_oldused, dn->dn_oldflags,
12810a586ceaSMark Shellenbaum dn->dn_olduid, dn->dn_oldgid, B_TRUE, tx);
12820a586ceaSMark Shellenbaum }
12831d8ccc7bSMark Shellenbaum if (flags & DN_ID_NEW_EXIST) {
12840a586ceaSMark Shellenbaum do_userquota_update(os, DN_USED_BYTES(dn->dn_phys),
12850a586ceaSMark Shellenbaum dn->dn_phys->dn_flags, dn->dn_newuid,
12860a586ceaSMark Shellenbaum dn->dn_newgid, B_FALSE, tx);
12870a586ceaSMark Shellenbaum }
12880a586ceaSMark Shellenbaum
12891d8ccc7bSMark Shellenbaum mutex_enter(&dn->dn_mtx);
12900a586ceaSMark Shellenbaum dn->dn_oldused = 0;
12910a586ceaSMark Shellenbaum dn->dn_oldflags = 0;
12920a586ceaSMark Shellenbaum if (dn->dn_id_flags & DN_ID_NEW_EXIST) {
12930a586ceaSMark Shellenbaum dn->dn_olduid = dn->dn_newuid;
12940a586ceaSMark Shellenbaum dn->dn_oldgid = dn->dn_newgid;
12950a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_OLD_EXIST;
12960a586ceaSMark Shellenbaum if (dn->dn_bonuslen == 0)
12970a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_CHKED_SPILL;
12980a586ceaSMark Shellenbaum else
12990a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_CHKED_BONUS;
13000a586ceaSMark Shellenbaum }
130128d97a71SMark Shellenbaum dn->dn_id_flags &= ~(DN_ID_NEW_EXIST);
130214843421SMatthew Ahrens mutex_exit(&dn->dn_mtx);
130314843421SMatthew Ahrens
130414843421SMatthew Ahrens list_remove(list, dn);
130514843421SMatthew Ahrens dnode_rele(dn, list);
130614843421SMatthew Ahrens }
130714843421SMatthew Ahrens }
130814843421SMatthew Ahrens
130906e0070dSMark Shellenbaum /*
131006e0070dSMark Shellenbaum * Returns a pointer to data to find uid/gid from
131106e0070dSMark Shellenbaum *
131206e0070dSMark Shellenbaum * If a dirty record for transaction group that is syncing can't
131306e0070dSMark Shellenbaum * be found then NULL is returned. In the NULL case it is assumed
131406e0070dSMark Shellenbaum * the uid/gid aren't changing.
131506e0070dSMark Shellenbaum */
131606e0070dSMark Shellenbaum static void *
dmu_objset_userquota_find_data(dmu_buf_impl_t * db,dmu_tx_t * tx)131706e0070dSMark Shellenbaum dmu_objset_userquota_find_data(dmu_buf_impl_t *db, dmu_tx_t *tx)
131806e0070dSMark Shellenbaum {
131906e0070dSMark Shellenbaum dbuf_dirty_record_t *dr, **drp;
132006e0070dSMark Shellenbaum void *data;
132106e0070dSMark Shellenbaum
132206e0070dSMark Shellenbaum if (db->db_dirtycnt == 0)
132306e0070dSMark Shellenbaum return (db->db.db_data); /* Nothing is changing */
132406e0070dSMark Shellenbaum
132506e0070dSMark Shellenbaum for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next)
132606e0070dSMark Shellenbaum if (dr->dr_txg == tx->tx_txg)
132706e0070dSMark Shellenbaum break;
132806e0070dSMark Shellenbaum
1329744947dcSTom Erickson if (dr == NULL) {
133006e0070dSMark Shellenbaum data = NULL;
1331744947dcSTom Erickson } else {
1332744947dcSTom Erickson dnode_t *dn;
1333744947dcSTom Erickson
1334744947dcSTom Erickson DB_DNODE_ENTER(dr->dr_dbuf);
1335744947dcSTom Erickson dn = DB_DNODE(dr->dr_dbuf);
1336744947dcSTom Erickson
1337744947dcSTom Erickson if (dn->dn_bonuslen == 0 &&
133806e0070dSMark Shellenbaum dr->dr_dbuf->db_blkid == DMU_SPILL_BLKID)
133906e0070dSMark Shellenbaum data = dr->dt.dl.dr_data->b_data;
134006e0070dSMark Shellenbaum else
134106e0070dSMark Shellenbaum data = dr->dt.dl.dr_data;
1342744947dcSTom Erickson
1343744947dcSTom Erickson DB_DNODE_EXIT(dr->dr_dbuf);
1344744947dcSTom Erickson }
1345744947dcSTom Erickson
134606e0070dSMark Shellenbaum return (data);
134706e0070dSMark Shellenbaum }
134806e0070dSMark Shellenbaum
13490a586ceaSMark Shellenbaum void
dmu_objset_userquota_get_ids(dnode_t * dn,boolean_t before,dmu_tx_t * tx)135006e0070dSMark Shellenbaum dmu_objset_userquota_get_ids(dnode_t *dn, boolean_t before, dmu_tx_t *tx)
13510a586ceaSMark Shellenbaum {
13520a586ceaSMark Shellenbaum objset_t *os = dn->dn_objset;
13530a586ceaSMark Shellenbaum void *data = NULL;
135406e0070dSMark Shellenbaum dmu_buf_impl_t *db = NULL;
1355d5285caeSGeorge Wilson uint64_t *user = NULL;
1356d5285caeSGeorge Wilson uint64_t *group = NULL;
13570a586ceaSMark Shellenbaum int flags = dn->dn_id_flags;
13580a586ceaSMark Shellenbaum int error;
135906e0070dSMark Shellenbaum boolean_t have_spill = B_FALSE;
13600a586ceaSMark Shellenbaum
13610a586ceaSMark Shellenbaum if (!dmu_objset_userused_enabled(dn->dn_objset))
13620a586ceaSMark Shellenbaum return;
13630a586ceaSMark Shellenbaum
13640a586ceaSMark Shellenbaum if (before && (flags & (DN_ID_CHKED_BONUS|DN_ID_OLD_EXIST|
13650a586ceaSMark Shellenbaum DN_ID_CHKED_SPILL)))
13660a586ceaSMark Shellenbaum return;
13670a586ceaSMark Shellenbaum
13680a586ceaSMark Shellenbaum if (before && dn->dn_bonuslen != 0)
13690a586ceaSMark Shellenbaum data = DN_BONUS(dn->dn_phys);
137006e0070dSMark Shellenbaum else if (!before && dn->dn_bonuslen != 0) {
137106e0070dSMark Shellenbaum if (dn->dn_bonus) {
137206e0070dSMark Shellenbaum db = dn->dn_bonus;
137306e0070dSMark Shellenbaum mutex_enter(&db->db_mtx);
137406e0070dSMark Shellenbaum data = dmu_objset_userquota_find_data(db, tx);
137506e0070dSMark Shellenbaum } else {
137606e0070dSMark Shellenbaum data = DN_BONUS(dn->dn_phys);
137706e0070dSMark Shellenbaum }
137806e0070dSMark Shellenbaum } else if (dn->dn_bonuslen == 0 && dn->dn_bonustype == DMU_OT_SA) {
13790a586ceaSMark Shellenbaum int rf = 0;
13800a586ceaSMark Shellenbaum
13810a586ceaSMark Shellenbaum if (RW_WRITE_HELD(&dn->dn_struct_rwlock))
13820a586ceaSMark Shellenbaum rf |= DB_RF_HAVESTRUCT;
13831d8ccc7bSMark Shellenbaum error = dmu_spill_hold_by_dnode(dn,
13841d8ccc7bSMark Shellenbaum rf | DB_RF_MUST_SUCCEED,
138506e0070dSMark Shellenbaum FTAG, (dmu_buf_t **)&db);
13860a586ceaSMark Shellenbaum ASSERT(error == 0);
138706e0070dSMark Shellenbaum mutex_enter(&db->db_mtx);
138806e0070dSMark Shellenbaum data = (before) ? db->db.db_data :
138906e0070dSMark Shellenbaum dmu_objset_userquota_find_data(db, tx);
139006e0070dSMark Shellenbaum have_spill = B_TRUE;
13910a586ceaSMark Shellenbaum } else {
13920a586ceaSMark Shellenbaum mutex_enter(&dn->dn_mtx);
13930a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_CHKED_BONUS;
13940a586ceaSMark Shellenbaum mutex_exit(&dn->dn_mtx);
13950a586ceaSMark Shellenbaum return;
13960a586ceaSMark Shellenbaum }
13970a586ceaSMark Shellenbaum
13980a586ceaSMark Shellenbaum if (before) {
139906e0070dSMark Shellenbaum ASSERT(data);
14000a586ceaSMark Shellenbaum user = &dn->dn_olduid;
14010a586ceaSMark Shellenbaum group = &dn->dn_oldgid;
140206e0070dSMark Shellenbaum } else if (data) {
14030a586ceaSMark Shellenbaum user = &dn->dn_newuid;
14040a586ceaSMark Shellenbaum group = &dn->dn_newgid;
14050a586ceaSMark Shellenbaum }
14060a586ceaSMark Shellenbaum
140706e0070dSMark Shellenbaum /*
140806e0070dSMark Shellenbaum * Must always call the callback in case the object
140906e0070dSMark Shellenbaum * type has changed and that type isn't an object type to track
141006e0070dSMark Shellenbaum */
14110a586ceaSMark Shellenbaum error = used_cbs[os->os_phys->os_type](dn->dn_bonustype, data,
14120a586ceaSMark Shellenbaum user, group);
14130a586ceaSMark Shellenbaum
141406e0070dSMark Shellenbaum /*
141506e0070dSMark Shellenbaum * Preserve existing uid/gid when the callback can't determine
141606e0070dSMark Shellenbaum * what the new uid/gid are and the callback returned EEXIST.
141706e0070dSMark Shellenbaum * The EEXIST error tells us to just use the existing uid/gid.
141806e0070dSMark Shellenbaum * If we don't know what the old values are then just assign
141906e0070dSMark Shellenbaum * them to 0, since that is a new file being created.
142006e0070dSMark Shellenbaum */
142106e0070dSMark Shellenbaum if (!before && data == NULL && error == EEXIST) {
142206e0070dSMark Shellenbaum if (flags & DN_ID_OLD_EXIST) {
142306e0070dSMark Shellenbaum dn->dn_newuid = dn->dn_olduid;
142406e0070dSMark Shellenbaum dn->dn_newgid = dn->dn_oldgid;
142506e0070dSMark Shellenbaum } else {
142606e0070dSMark Shellenbaum dn->dn_newuid = 0;
142706e0070dSMark Shellenbaum dn->dn_newgid = 0;
142806e0070dSMark Shellenbaum }
142906e0070dSMark Shellenbaum error = 0;
143006e0070dSMark Shellenbaum }
143106e0070dSMark Shellenbaum
143206e0070dSMark Shellenbaum if (db)
143306e0070dSMark Shellenbaum mutex_exit(&db->db_mtx);
143406e0070dSMark Shellenbaum
14350a586ceaSMark Shellenbaum mutex_enter(&dn->dn_mtx);
14360a586ceaSMark Shellenbaum if (error == 0 && before)
14370a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_OLD_EXIST;
14380a586ceaSMark Shellenbaum if (error == 0 && !before)
14390a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_NEW_EXIST;
14400a586ceaSMark Shellenbaum
144106e0070dSMark Shellenbaum if (have_spill) {
14420a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_CHKED_SPILL;
14430a586ceaSMark Shellenbaum } else {
14440a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_CHKED_BONUS;
14450a586ceaSMark Shellenbaum }
14460a586ceaSMark Shellenbaum mutex_exit(&dn->dn_mtx);
144706e0070dSMark Shellenbaum if (have_spill)
144806e0070dSMark Shellenbaum dmu_buf_rele((dmu_buf_t *)db, FTAG);
14490a586ceaSMark Shellenbaum }
14500a586ceaSMark Shellenbaum
145114843421SMatthew Ahrens boolean_t
dmu_objset_userspace_present(objset_t * os)145214843421SMatthew Ahrens dmu_objset_userspace_present(objset_t *os)
145314843421SMatthew Ahrens {
1454503ad85cSMatthew Ahrens return (os->os_phys->os_flags &
145514843421SMatthew Ahrens OBJSET_FLAG_USERACCOUNTING_COMPLETE);
145614843421SMatthew Ahrens }
145714843421SMatthew Ahrens
145814843421SMatthew Ahrens int
dmu_objset_userspace_upgrade(objset_t * os)145914843421SMatthew Ahrens dmu_objset_userspace_upgrade(objset_t *os)
146014843421SMatthew Ahrens {
146114843421SMatthew Ahrens uint64_t obj;
146214843421SMatthew Ahrens int err = 0;
146314843421SMatthew Ahrens
146414843421SMatthew Ahrens if (dmu_objset_userspace_present(os))
146514843421SMatthew Ahrens return (0);
1466503ad85cSMatthew Ahrens if (!dmu_objset_userused_enabled(os))
1467be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP));
146814843421SMatthew Ahrens if (dmu_objset_is_snapshot(os))
1469be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
147014843421SMatthew Ahrens
147114843421SMatthew Ahrens /*
147214843421SMatthew Ahrens * We simply need to mark every object dirty, so that it will be
147314843421SMatthew Ahrens * synced out and now accounted. If this is called
147414843421SMatthew Ahrens * concurrently, or if we already did some work before crashing,
147514843421SMatthew Ahrens * that's fine, since we track each object's accounted state
147614843421SMatthew Ahrens * independently.
147714843421SMatthew Ahrens */
147814843421SMatthew Ahrens
147914843421SMatthew Ahrens for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
148068857716SLin Ling dmu_tx_t *tx;
148114843421SMatthew Ahrens dmu_buf_t *db;
148214843421SMatthew Ahrens int objerr;
148314843421SMatthew Ahrens
148414843421SMatthew Ahrens if (issig(JUSTLOOKING) && issig(FORREAL))
1485be6fd75aSMatthew Ahrens return (SET_ERROR(EINTR));
148614843421SMatthew Ahrens
148714843421SMatthew Ahrens objerr = dmu_bonus_hold(os, obj, FTAG, &db);
14883b2aab18SMatthew Ahrens if (objerr != 0)
148914843421SMatthew Ahrens continue;
149068857716SLin Ling tx = dmu_tx_create(os);
149114843421SMatthew Ahrens dmu_tx_hold_bonus(tx, obj);
149214843421SMatthew Ahrens objerr = dmu_tx_assign(tx, TXG_WAIT);
14933b2aab18SMatthew Ahrens if (objerr != 0) {
149414843421SMatthew Ahrens dmu_tx_abort(tx);
149514843421SMatthew Ahrens continue;
149614843421SMatthew Ahrens }
149714843421SMatthew Ahrens dmu_buf_will_dirty(db, tx);
149814843421SMatthew Ahrens dmu_buf_rele(db, FTAG);
149914843421SMatthew Ahrens dmu_tx_commit(tx);
150014843421SMatthew Ahrens }
150114843421SMatthew Ahrens
1502503ad85cSMatthew Ahrens os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
150314843421SMatthew Ahrens txg_wait_synced(dmu_objset_pool(os), 0);
150414843421SMatthew Ahrens return (0);
150514843421SMatthew Ahrens }
150614843421SMatthew Ahrens
1507fa9e4066Sahrens void
dmu_objset_space(objset_t * os,uint64_t * refdbytesp,uint64_t * availbytesp,uint64_t * usedobjsp,uint64_t * availobjsp)1508a2eea2e1Sahrens dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp,
1509a2eea2e1Sahrens uint64_t *usedobjsp, uint64_t *availobjsp)
1510fa9e4066Sahrens {
1511503ad85cSMatthew Ahrens dsl_dataset_space(os->os_dsl_dataset, refdbytesp, availbytesp,
1512a2eea2e1Sahrens usedobjsp, availobjsp);
1513fa9e4066Sahrens }
1514a2eea2e1Sahrens
1515a2eea2e1Sahrens uint64_t
dmu_objset_fsid_guid(objset_t * os)1516a2eea2e1Sahrens dmu_objset_fsid_guid(objset_t *os)
1517a2eea2e1Sahrens {
1518503ad85cSMatthew Ahrens return (dsl_dataset_fsid_guid(os->os_dsl_dataset));
1519a2eea2e1Sahrens }
1520a2eea2e1Sahrens
1521a2eea2e1Sahrens void
dmu_objset_fast_stat(objset_t * os,dmu_objset_stats_t * stat)1522a2eea2e1Sahrens dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat)
1523a2eea2e1Sahrens {
1524503ad85cSMatthew Ahrens stat->dds_type = os->os_phys->os_type;
1525503ad85cSMatthew Ahrens if (os->os_dsl_dataset)
1526503ad85cSMatthew Ahrens dsl_dataset_fast_stat(os->os_dsl_dataset, stat);
1527a2eea2e1Sahrens }
1528a2eea2e1Sahrens
1529a2eea2e1Sahrens void
dmu_objset_stats(objset_t * os,nvlist_t * nv)1530a2eea2e1Sahrens dmu_objset_stats(objset_t *os, nvlist_t *nv)
1531a2eea2e1Sahrens {
1532503ad85cSMatthew Ahrens ASSERT(os->os_dsl_dataset ||
1533503ad85cSMatthew Ahrens os->os_phys->os_type == DMU_OST_META);
1534a2eea2e1Sahrens
1535503ad85cSMatthew Ahrens if (os->os_dsl_dataset != NULL)
1536503ad85cSMatthew Ahrens dsl_dataset_stats(os->os_dsl_dataset, nv);
1537a2eea2e1Sahrens
1538a2eea2e1Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE,
1539503ad85cSMatthew Ahrens os->os_phys->os_type);
154014843421SMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERACCOUNTING,
154114843421SMatthew Ahrens dmu_objset_userspace_present(os));
1542fa9e4066Sahrens }
1543fa9e4066Sahrens
1544fa9e4066Sahrens int
dmu_objset_is_snapshot(objset_t * os)1545fa9e4066Sahrens dmu_objset_is_snapshot(objset_t *os)
1546fa9e4066Sahrens {
1547503ad85cSMatthew Ahrens if (os->os_dsl_dataset != NULL)
15480521c5ebSJustin Gibbs return (os->os_dsl_dataset->ds_is_snapshot);
1549fa9e4066Sahrens else
1550fa9e4066Sahrens return (B_FALSE);
1551fa9e4066Sahrens }
1552fa9e4066Sahrens
1553fa9e4066Sahrens int
dmu_snapshot_realname(objset_t * os,char * name,char * real,int maxlen,boolean_t * conflict)1554ab04eb8eStimh dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen,
1555ab04eb8eStimh boolean_t *conflict)
1556ab04eb8eStimh {
1557503ad85cSMatthew Ahrens dsl_dataset_t *ds = os->os_dsl_dataset;
1558ab04eb8eStimh uint64_t ignored;
1559ab04eb8eStimh
15608d62b223SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0)
1561be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT));
1562ab04eb8eStimh
1563ab04eb8eStimh return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset,
15648d62b223SJustin T. Gibbs dsl_dataset_phys(ds)->ds_snapnames_zapobj, name, 8, 1, &ignored,
15658d62b223SJustin T. Gibbs MT_FIRST, real, maxlen, conflict));
1566ab04eb8eStimh }
1567ab04eb8eStimh
1568ab04eb8eStimh int
dmu_snapshot_list_next(objset_t * os,int namelen,char * name,uint64_t * idp,uint64_t * offp,boolean_t * case_conflict)1569fa9e4066Sahrens dmu_snapshot_list_next(objset_t *os, int namelen, char *name,
1570b38f0970Sck153898 uint64_t *idp, uint64_t *offp, boolean_t *case_conflict)
1571fa9e4066Sahrens {
1572503ad85cSMatthew Ahrens dsl_dataset_t *ds = os->os_dsl_dataset;
1573fa9e4066Sahrens zap_cursor_t cursor;
1574fa9e4066Sahrens zap_attribute_t attr;
1575fa9e4066Sahrens
15763b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dmu_objset_pool(os)));
15773b2aab18SMatthew Ahrens
15788d62b223SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0)
1579be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT));
1580fa9e4066Sahrens
1581fa9e4066Sahrens zap_cursor_init_serialized(&cursor,
1582fa9e4066Sahrens ds->ds_dir->dd_pool->dp_meta_objset,
15838d62b223SJustin T. Gibbs dsl_dataset_phys(ds)->ds_snapnames_zapobj, *offp);
1584fa9e4066Sahrens
158587e5029aSahrens if (zap_cursor_retrieve(&cursor, &attr) != 0) {
158687e5029aSahrens zap_cursor_fini(&cursor);
1587be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT));
158887e5029aSahrens }
1589fa9e4066Sahrens
159087e5029aSahrens if (strlen(attr.za_name) + 1 > namelen) {
159187e5029aSahrens zap_cursor_fini(&cursor);
1592be6fd75aSMatthew Ahrens return (SET_ERROR(ENAMETOOLONG));
159387e5029aSahrens }
1594fa9e4066Sahrens
1595fa9e4066Sahrens (void) strcpy(name, attr.za_name);
159687e5029aSahrens if (idp)
159787e5029aSahrens *idp = attr.za_first_integer;
1598b38f0970Sck153898 if (case_conflict)
1599b38f0970Sck153898 *case_conflict = attr.za_normalization_conflict;
1600fa9e4066Sahrens zap_cursor_advance(&cursor);
1601fa9e4066Sahrens *offp = zap_cursor_serialize(&cursor);
160287e5029aSahrens zap_cursor_fini(&cursor);
160387e5029aSahrens
160487e5029aSahrens return (0);
160587e5029aSahrens }
160687e5029aSahrens
160787e5029aSahrens int
dmu_dir_list_next(objset_t * os,int namelen,char * name,uint64_t * idp,uint64_t * offp)160887e5029aSahrens dmu_dir_list_next(objset_t *os, int namelen, char *name,
160987e5029aSahrens uint64_t *idp, uint64_t *offp)
161087e5029aSahrens {
1611503ad85cSMatthew Ahrens dsl_dir_t *dd = os->os_dsl_dataset->ds_dir;
161287e5029aSahrens zap_cursor_t cursor;
161387e5029aSahrens zap_attribute_t attr;
161487e5029aSahrens
161587e5029aSahrens /* there is no next dir on a snapshot! */
1616503ad85cSMatthew Ahrens if (os->os_dsl_dataset->ds_object !=
16178d62b223SJustin T. Gibbs dsl_dir_phys(dd)->dd_head_dataset_obj)
1618be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT));
161987e5029aSahrens
162087e5029aSahrens zap_cursor_init_serialized(&cursor,
162187e5029aSahrens dd->dd_pool->dp_meta_objset,
16228d62b223SJustin T. Gibbs dsl_dir_phys(dd)->dd_child_dir_zapobj, *offp);
162387e5029aSahrens
162487e5029aSahrens if (zap_cursor_retrieve(&cursor, &attr) != 0) {
162587e5029aSahrens zap_cursor_fini(&cursor);
1626be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT));
162787e5029aSahrens }
162887e5029aSahrens
162987e5029aSahrens if (strlen(attr.za_name) + 1 > namelen) {
163087e5029aSahrens zap_cursor_fini(&cursor);
1631be6fd75aSMatthew Ahrens return (SET_ERROR(ENAMETOOLONG));
163287e5029aSahrens }
163387e5029aSahrens
163487e5029aSahrens (void) strcpy(name, attr.za_name);
163587e5029aSahrens if (idp)
163687e5029aSahrens *idp = attr.za_first_integer;
163787e5029aSahrens zap_cursor_advance(&cursor);
163887e5029aSahrens *offp = zap_cursor_serialize(&cursor);
163987e5029aSahrens zap_cursor_fini(&cursor);
1640fa9e4066Sahrens
1641fa9e4066Sahrens return (0);
1642fa9e4066Sahrens }
1643fa9e4066Sahrens
1644416c5cd3SArne Jansen typedef struct dmu_objset_find_ctx {
1645416c5cd3SArne Jansen taskq_t *dc_tq;
1646416c5cd3SArne Jansen dsl_pool_t *dc_dp;
1647416c5cd3SArne Jansen uint64_t dc_ddobj;
1648416c5cd3SArne Jansen int (*dc_func)(dsl_pool_t *, dsl_dataset_t *, void *);
1649416c5cd3SArne Jansen void *dc_arg;
1650416c5cd3SArne Jansen int dc_flags;
1651416c5cd3SArne Jansen kmutex_t *dc_error_lock;
1652416c5cd3SArne Jansen int *dc_error;
1653416c5cd3SArne Jansen } dmu_objset_find_ctx_t;
1654416c5cd3SArne Jansen
1655416c5cd3SArne Jansen static void
dmu_objset_find_dp_impl(dmu_objset_find_ctx_t * dcp)1656416c5cd3SArne Jansen dmu_objset_find_dp_impl(dmu_objset_find_ctx_t *dcp)
1657088f3894Sahrens {
1658416c5cd3SArne Jansen dsl_pool_t *dp = dcp->dc_dp;
1659416c5cd3SArne Jansen dmu_objset_find_ctx_t *child_dcp;
1660fa9e4066Sahrens dsl_dir_t *dd;
1661088f3894Sahrens dsl_dataset_t *ds;
1662fa9e4066Sahrens zap_cursor_t zc;
1663b7661cccSmmusante zap_attribute_t *attr;
1664088f3894Sahrens uint64_t thisobj;
1665416c5cd3SArne Jansen int err = 0;
1666fa9e4066Sahrens
1667416c5cd3SArne Jansen /* don't process if there already was an error */
1668416c5cd3SArne Jansen if (*dcp->dc_error != 0)
1669416c5cd3SArne Jansen goto out;
16703b2aab18SMatthew Ahrens
1671416c5cd3SArne Jansen err = dsl_dir_hold_obj(dp, dcp->dc_ddobj, NULL, FTAG, &dd);
16723b2aab18SMatthew Ahrens if (err != 0)
1673416c5cd3SArne Jansen goto out;
1674fa9e4066Sahrens
1675088f3894Sahrens /* Don't visit hidden ($MOS & $ORIGIN) objsets. */
1676088f3894Sahrens if (dd->dd_myname[0] == '$') {
16773b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG);
1678416c5cd3SArne Jansen goto out;
1679088f3894Sahrens }
1680088f3894Sahrens
16818d62b223SJustin T. Gibbs thisobj = dsl_dir_phys(dd)->dd_head_dataset_obj;
1682b7661cccSmmusante attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
1683fa9e4066Sahrens
1684fa9e4066Sahrens /*
1685fa9e4066Sahrens * Iterate over all children.
1686fa9e4066Sahrens */
1687416c5cd3SArne Jansen if (dcp->dc_flags & DS_FIND_CHILDREN) {
1688088f3894Sahrens for (zap_cursor_init(&zc, dp->dp_meta_objset,
16898d62b223SJustin T. Gibbs dsl_dir_phys(dd)->dd_child_dir_zapobj);
1690b7661cccSmmusante zap_cursor_retrieve(&zc, attr) == 0;
1691fa9e4066Sahrens (void) zap_cursor_advance(&zc)) {
16923b2aab18SMatthew Ahrens ASSERT3U(attr->za_integer_length, ==,
16933b2aab18SMatthew Ahrens sizeof (uint64_t));
16943b2aab18SMatthew Ahrens ASSERT3U(attr->za_num_integers, ==, 1);
1695fa9e4066Sahrens
1696416c5cd3SArne Jansen child_dcp = kmem_alloc(sizeof (*child_dcp), KM_SLEEP);
1697416c5cd3SArne Jansen *child_dcp = *dcp;
1698416c5cd3SArne Jansen child_dcp->dc_ddobj = attr->za_first_integer;
1699416c5cd3SArne Jansen if (dcp->dc_tq != NULL)
1700416c5cd3SArne Jansen (void) taskq_dispatch(dcp->dc_tq,
1701416c5cd3SArne Jansen dmu_objset_find_dp_cb, child_dcp, TQ_SLEEP);
1702416c5cd3SArne Jansen else
1703416c5cd3SArne Jansen dmu_objset_find_dp_impl(child_dcp);
1704fa9e4066Sahrens }
170587e5029aSahrens zap_cursor_fini(&zc);
17060b69c2f0Sahrens }
1707fa9e4066Sahrens
1708fa9e4066Sahrens /*
1709fa9e4066Sahrens * Iterate over all snapshots.
1710fa9e4066Sahrens */
1711416c5cd3SArne Jansen if (dcp->dc_flags & DS_FIND_SNAPSHOTS) {
17123b2aab18SMatthew Ahrens dsl_dataset_t *ds;
1713088f3894Sahrens err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
1714fa9e4066Sahrens
1715088f3894Sahrens if (err == 0) {
17168d62b223SJustin T. Gibbs uint64_t snapobj;
17178d62b223SJustin T. Gibbs
17188d62b223SJustin T. Gibbs snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
1719088f3894Sahrens dsl_dataset_rele(ds, FTAG);
1720fa9e4066Sahrens
1721088f3894Sahrens for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
1722b7661cccSmmusante zap_cursor_retrieve(&zc, attr) == 0;
1723fa9e4066Sahrens (void) zap_cursor_advance(&zc)) {
17243b2aab18SMatthew Ahrens ASSERT3U(attr->za_integer_length, ==,
1725088f3894Sahrens sizeof (uint64_t));
17263b2aab18SMatthew Ahrens ASSERT3U(attr->za_num_integers, ==, 1);
1727fa9e4066Sahrens
17283b2aab18SMatthew Ahrens err = dsl_dataset_hold_obj(dp,
17293b2aab18SMatthew Ahrens attr->za_first_integer, FTAG, &ds);
17303b2aab18SMatthew Ahrens if (err != 0)
17313b2aab18SMatthew Ahrens break;
1732416c5cd3SArne Jansen err = dcp->dc_func(dp, ds, dcp->dc_arg);
17333b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG);
17343b2aab18SMatthew Ahrens if (err != 0)
17351d452cf5Sahrens break;
1736fa9e4066Sahrens }
173787e5029aSahrens zap_cursor_fini(&zc);
1738fa9e4066Sahrens }
1739088f3894Sahrens }
1740fa9e4066Sahrens
17413b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG);
1742b7661cccSmmusante kmem_free(attr, sizeof (zap_attribute_t));
1743fa9e4066Sahrens
17443b2aab18SMatthew Ahrens if (err != 0)
1745416c5cd3SArne Jansen goto out;
17461d452cf5Sahrens
1747fa9e4066Sahrens /*
17483b2aab18SMatthew Ahrens * Apply to self.
1749fa9e4066Sahrens */
17503b2aab18SMatthew Ahrens err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
17513b2aab18SMatthew Ahrens if (err != 0)
1752416c5cd3SArne Jansen goto out;
1753416c5cd3SArne Jansen err = dcp->dc_func(dp, ds, dcp->dc_arg);
17543b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG);
1755416c5cd3SArne Jansen
1756416c5cd3SArne Jansen out:
1757416c5cd3SArne Jansen if (err != 0) {
1758416c5cd3SArne Jansen mutex_enter(dcp->dc_error_lock);
1759416c5cd3SArne Jansen /* only keep first error */
1760416c5cd3SArne Jansen if (*dcp->dc_error == 0)
1761416c5cd3SArne Jansen *dcp->dc_error = err;
1762416c5cd3SArne Jansen mutex_exit(dcp->dc_error_lock);
1763416c5cd3SArne Jansen }
1764416c5cd3SArne Jansen
1765416c5cd3SArne Jansen kmem_free(dcp, sizeof (*dcp));
1766416c5cd3SArne Jansen }
1767416c5cd3SArne Jansen
1768416c5cd3SArne Jansen static void
dmu_objset_find_dp_cb(void * arg)1769416c5cd3SArne Jansen dmu_objset_find_dp_cb(void *arg)
1770416c5cd3SArne Jansen {
1771416c5cd3SArne Jansen dmu_objset_find_ctx_t *dcp = arg;
1772416c5cd3SArne Jansen dsl_pool_t *dp = dcp->dc_dp;
1773416c5cd3SArne Jansen
17748f72ee6dSArne Jansen /*
17758f72ee6dSArne Jansen * We need to get a pool_config_lock here, as there are several
17768f72ee6dSArne Jansen * asssert(pool_config_held) down the stack. Getting a lock via
17778f72ee6dSArne Jansen * dsl_pool_config_enter is risky, as it might be stalled by a
17788f72ee6dSArne Jansen * pending writer. This would deadlock, as the write lock can
17798f72ee6dSArne Jansen * only be granted when our parent thread gives up the lock.
17808f72ee6dSArne Jansen * The _prio interface gives us priority over a pending writer.
17818f72ee6dSArne Jansen */
17828f72ee6dSArne Jansen dsl_pool_config_enter_prio(dp, FTAG);
1783416c5cd3SArne Jansen
1784416c5cd3SArne Jansen dmu_objset_find_dp_impl(dcp);
1785416c5cd3SArne Jansen
1786416c5cd3SArne Jansen dsl_pool_config_exit(dp, FTAG);
1787416c5cd3SArne Jansen }
1788416c5cd3SArne Jansen
1789416c5cd3SArne Jansen /*
1790416c5cd3SArne Jansen * Find objsets under and including ddobj, call func(ds) on each.
1791416c5cd3SArne Jansen * The order for the enumeration is completely undefined.
1792416c5cd3SArne Jansen * func is called with dsl_pool_config held.
1793416c5cd3SArne Jansen */
1794416c5cd3SArne Jansen 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)1795416c5cd3SArne Jansen dmu_objset_find_dp(dsl_pool_t *dp, uint64_t ddobj,
1796416c5cd3SArne Jansen int func(dsl_pool_t *, dsl_dataset_t *, void *), void *arg, int flags)
1797416c5cd3SArne Jansen {
1798416c5cd3SArne Jansen int error = 0;
1799416c5cd3SArne Jansen taskq_t *tq = NULL;
1800416c5cd3SArne Jansen int ntasks;
1801416c5cd3SArne Jansen dmu_objset_find_ctx_t *dcp;
1802416c5cd3SArne Jansen kmutex_t err_lock;
1803416c5cd3SArne Jansen
1804416c5cd3SArne Jansen mutex_init(&err_lock, NULL, MUTEX_DEFAULT, NULL);
1805416c5cd3SArne Jansen dcp = kmem_alloc(sizeof (*dcp), KM_SLEEP);
1806416c5cd3SArne Jansen dcp->dc_tq = NULL;
1807416c5cd3SArne Jansen dcp->dc_dp = dp;
1808416c5cd3SArne Jansen dcp->dc_ddobj = ddobj;
1809416c5cd3SArne Jansen dcp->dc_func = func;
1810416c5cd3SArne Jansen dcp->dc_arg = arg;
1811416c5cd3SArne Jansen dcp->dc_flags = flags;
1812416c5cd3SArne Jansen dcp->dc_error_lock = &err_lock;
1813416c5cd3SArne Jansen dcp->dc_error = &error;
1814416c5cd3SArne Jansen
1815416c5cd3SArne Jansen if ((flags & DS_FIND_SERIALIZE) || dsl_pool_config_held_writer(dp)) {
1816416c5cd3SArne Jansen /*
1817416c5cd3SArne Jansen * In case a write lock is held we can't make use of
1818416c5cd3SArne Jansen * parallelism, as down the stack of the worker threads
1819416c5cd3SArne Jansen * the lock is asserted via dsl_pool_config_held.
1820416c5cd3SArne Jansen * In case of a read lock this is solved by getting a read
1821416c5cd3SArne Jansen * lock in each worker thread, which isn't possible in case
1822416c5cd3SArne Jansen * of a writer lock. So we fall back to the synchronous path
1823416c5cd3SArne Jansen * here.
1824416c5cd3SArne Jansen * In the future it might be possible to get some magic into
1825416c5cd3SArne Jansen * dsl_pool_config_held in a way that it returns true for
1826416c5cd3SArne Jansen * the worker threads so that a single lock held from this
1827416c5cd3SArne Jansen * thread suffices. For now, stay single threaded.
1828416c5cd3SArne Jansen */
1829416c5cd3SArne Jansen dmu_objset_find_dp_impl(dcp);
1830416c5cd3SArne Jansen
1831416c5cd3SArne Jansen return (error);
1832416c5cd3SArne Jansen }
1833416c5cd3SArne Jansen
1834416c5cd3SArne Jansen ntasks = dmu_find_threads;
1835416c5cd3SArne Jansen if (ntasks == 0)
1836416c5cd3SArne Jansen ntasks = vdev_count_leaves(dp->dp_spa) * 4;
1837416c5cd3SArne Jansen tq = taskq_create("dmu_objset_find", ntasks, minclsyspri, ntasks,
1838416c5cd3SArne Jansen INT_MAX, 0);
1839416c5cd3SArne Jansen if (tq == NULL) {
1840416c5cd3SArne Jansen kmem_free(dcp, sizeof (*dcp));
1841416c5cd3SArne Jansen return (SET_ERROR(ENOMEM));
1842416c5cd3SArne Jansen }
1843416c5cd3SArne Jansen dcp->dc_tq = tq;
1844416c5cd3SArne Jansen
1845416c5cd3SArne Jansen /* dcp will be freed by task */
1846416c5cd3SArne Jansen (void) taskq_dispatch(tq, dmu_objset_find_dp_cb, dcp, TQ_SLEEP);
1847416c5cd3SArne Jansen
1848416c5cd3SArne Jansen /*
1849416c5cd3SArne Jansen * PORTING: this code relies on the property of taskq_wait to wait
1850416c5cd3SArne Jansen * until no more tasks are queued and no more tasks are active. As
1851416c5cd3SArne Jansen * we always queue new tasks from within other tasks, task_wait
1852416c5cd3SArne Jansen * reliably waits for the full recursion to finish, even though we
1853416c5cd3SArne Jansen * enqueue new tasks after taskq_wait has been called.
1854416c5cd3SArne Jansen * On platforms other than illumos, taskq_wait may not have this
1855416c5cd3SArne Jansen * property.
1856416c5cd3SArne Jansen */
1857416c5cd3SArne Jansen taskq_wait(tq);
1858416c5cd3SArne Jansen taskq_destroy(tq);
1859416c5cd3SArne Jansen mutex_destroy(&err_lock);
1860416c5cd3SArne Jansen
1861416c5cd3SArne Jansen return (error);
1862fa9e4066Sahrens }
1863f18faf3fSek110237
18643b2aab18SMatthew Ahrens /*
18653b2aab18SMatthew Ahrens * Find all objsets under name, and for each, call 'func(child_name, arg)'.
18663b2aab18SMatthew Ahrens * The dp_config_rwlock must not be held when this is called, and it
18673b2aab18SMatthew Ahrens * will not be held when the callback is called.
18683b2aab18SMatthew Ahrens * Therefore this function should only be used when the pool is not changing
18693b2aab18SMatthew Ahrens * (e.g. in syncing context), or the callback can deal with the possible races.
18703b2aab18SMatthew Ahrens */
18713b2aab18SMatthew Ahrens static int
dmu_objset_find_impl(spa_t * spa,const char * name,int func (const char *,void *),void * arg,int flags)18723b2aab18SMatthew Ahrens dmu_objset_find_impl(spa_t *spa, const char *name,
18733b2aab18SMatthew Ahrens int func(const char *, void *), void *arg, int flags)
18747f73c863SRich Morris {
18753b2aab18SMatthew Ahrens dsl_dir_t *dd;
18763b2aab18SMatthew Ahrens dsl_pool_t *dp = spa_get_dsl(spa);
18777f73c863SRich Morris dsl_dataset_t *ds;
18783b2aab18SMatthew Ahrens zap_cursor_t zc;
18793b2aab18SMatthew Ahrens zap_attribute_t *attr;
18803b2aab18SMatthew Ahrens char *child;
18813b2aab18SMatthew Ahrens uint64_t thisobj;
18823b2aab18SMatthew Ahrens int err;
18837f73c863SRich Morris
18843b2aab18SMatthew Ahrens dsl_pool_config_enter(dp, FTAG);
18853b2aab18SMatthew Ahrens
18863b2aab18SMatthew Ahrens err = dsl_dir_hold(dp, name, FTAG, &dd, NULL);
18873b2aab18SMatthew Ahrens if (err != 0) {
18883b2aab18SMatthew Ahrens dsl_pool_config_exit(dp, FTAG);
18893b2aab18SMatthew Ahrens return (err);
18903b2aab18SMatthew Ahrens }
18913b2aab18SMatthew Ahrens
18923b2aab18SMatthew Ahrens /* Don't visit hidden ($MOS & $ORIGIN) objsets. */
18933b2aab18SMatthew Ahrens if (dd->dd_myname[0] == '$') {
18943b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG);
18953b2aab18SMatthew Ahrens dsl_pool_config_exit(dp, FTAG);
18967f73c863SRich Morris return (0);
18977f73c863SRich Morris }
18987f73c863SRich Morris
18998d62b223SJustin T. Gibbs thisobj = dsl_dir_phys(dd)->dd_head_dataset_obj;
19003b2aab18SMatthew Ahrens attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
19013b2aab18SMatthew Ahrens
19023b2aab18SMatthew Ahrens /*
19033b2aab18SMatthew Ahrens * Iterate over all children.
19043b2aab18SMatthew Ahrens */
19053b2aab18SMatthew Ahrens if (flags & DS_FIND_CHILDREN) {
19063b2aab18SMatthew Ahrens for (zap_cursor_init(&zc, dp->dp_meta_objset,
19078d62b223SJustin T. Gibbs dsl_dir_phys(dd)->dd_child_dir_zapobj);
19083b2aab18SMatthew Ahrens zap_cursor_retrieve(&zc, attr) == 0;
19093b2aab18SMatthew Ahrens (void) zap_cursor_advance(&zc)) {
19103b2aab18SMatthew Ahrens ASSERT3U(attr->za_integer_length, ==,
19113b2aab18SMatthew Ahrens sizeof (uint64_t));
19123b2aab18SMatthew Ahrens ASSERT3U(attr->za_num_integers, ==, 1);
19133b2aab18SMatthew Ahrens
19143b2aab18SMatthew Ahrens child = kmem_asprintf("%s/%s", name, attr->za_name);
19153b2aab18SMatthew Ahrens dsl_pool_config_exit(dp, FTAG);
19163b2aab18SMatthew Ahrens err = dmu_objset_find_impl(spa, child,
19173b2aab18SMatthew Ahrens func, arg, flags);
19183b2aab18SMatthew Ahrens dsl_pool_config_enter(dp, FTAG);
19193b2aab18SMatthew Ahrens strfree(child);
19203b2aab18SMatthew Ahrens if (err != 0)
19213b2aab18SMatthew Ahrens break;
19223b2aab18SMatthew Ahrens }
19233b2aab18SMatthew Ahrens zap_cursor_fini(&zc);
19243b2aab18SMatthew Ahrens
19253b2aab18SMatthew Ahrens if (err != 0) {
19263b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG);
19273b2aab18SMatthew Ahrens dsl_pool_config_exit(dp, FTAG);
19283b2aab18SMatthew Ahrens kmem_free(attr, sizeof (zap_attribute_t));
19293b2aab18SMatthew Ahrens return (err);
19303b2aab18SMatthew Ahrens }
19313b2aab18SMatthew Ahrens }
19323b2aab18SMatthew Ahrens
19333b2aab18SMatthew Ahrens /*
19343b2aab18SMatthew Ahrens * Iterate over all snapshots.
19353b2aab18SMatthew Ahrens */
19363b2aab18SMatthew Ahrens if (flags & DS_FIND_SNAPSHOTS) {
19373b2aab18SMatthew Ahrens err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
19383b2aab18SMatthew Ahrens
19393b2aab18SMatthew Ahrens if (err == 0) {
19408d62b223SJustin T. Gibbs uint64_t snapobj;
19418d62b223SJustin T. Gibbs
19428d62b223SJustin T. Gibbs snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
19437f73c863SRich Morris dsl_dataset_rele(ds, FTAG);
19443b2aab18SMatthew Ahrens
19453b2aab18SMatthew Ahrens for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
19463b2aab18SMatthew Ahrens zap_cursor_retrieve(&zc, attr) == 0;
19473b2aab18SMatthew Ahrens (void) zap_cursor_advance(&zc)) {
19483b2aab18SMatthew Ahrens ASSERT3U(attr->za_integer_length, ==,
19493b2aab18SMatthew Ahrens sizeof (uint64_t));
19503b2aab18SMatthew Ahrens ASSERT3U(attr->za_num_integers, ==, 1);
19513b2aab18SMatthew Ahrens
19523b2aab18SMatthew Ahrens child = kmem_asprintf("%s@%s",
19533b2aab18SMatthew Ahrens name, attr->za_name);
19543b2aab18SMatthew Ahrens dsl_pool_config_exit(dp, FTAG);
19553b2aab18SMatthew Ahrens err = func(child, arg);
19563b2aab18SMatthew Ahrens dsl_pool_config_enter(dp, FTAG);
19573b2aab18SMatthew Ahrens strfree(child);
19583b2aab18SMatthew Ahrens if (err != 0)
19593b2aab18SMatthew Ahrens break;
19603b2aab18SMatthew Ahrens }
19613b2aab18SMatthew Ahrens zap_cursor_fini(&zc);
19623b2aab18SMatthew Ahrens }
19633b2aab18SMatthew Ahrens }
19643b2aab18SMatthew Ahrens
19653b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG);
19663b2aab18SMatthew Ahrens kmem_free(attr, sizeof (zap_attribute_t));
19673b2aab18SMatthew Ahrens dsl_pool_config_exit(dp, FTAG);
19683b2aab18SMatthew Ahrens
19693b2aab18SMatthew Ahrens if (err != 0)
19703b2aab18SMatthew Ahrens return (err);
19713b2aab18SMatthew Ahrens
19723b2aab18SMatthew Ahrens /* Apply to self. */
19733b2aab18SMatthew Ahrens return (func(name, arg));
19743b2aab18SMatthew Ahrens }
19753b2aab18SMatthew Ahrens
19763b2aab18SMatthew Ahrens /*
19773b2aab18SMatthew Ahrens * See comment above dmu_objset_find_impl().
19783b2aab18SMatthew Ahrens */
19793b2aab18SMatthew Ahrens int
dmu_objset_find(char * name,int func (const char *,void *),void * arg,int flags)19803b2aab18SMatthew Ahrens dmu_objset_find(char *name, int func(const char *, void *), void *arg,
19813b2aab18SMatthew Ahrens int flags)
19823b2aab18SMatthew Ahrens {
19833b2aab18SMatthew Ahrens spa_t *spa;
19843b2aab18SMatthew Ahrens int error;
19853b2aab18SMatthew Ahrens
19863b2aab18SMatthew Ahrens error = spa_open(name, &spa, FTAG);
19873b2aab18SMatthew Ahrens if (error != 0)
19883b2aab18SMatthew Ahrens return (error);
19893b2aab18SMatthew Ahrens error = dmu_objset_find_impl(spa, name, func, arg, flags);
19903b2aab18SMatthew Ahrens spa_close(spa, FTAG);
19913b2aab18SMatthew Ahrens return (error);
19927f73c863SRich Morris }
19937f73c863SRich Morris
1994f18faf3fSek110237 void
dmu_objset_set_user(objset_t * os,void * user_ptr)1995f18faf3fSek110237 dmu_objset_set_user(objset_t *os, void *user_ptr)
1996f18faf3fSek110237 {
1997503ad85cSMatthew Ahrens ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
1998503ad85cSMatthew Ahrens os->os_user_ptr = user_ptr;
1999f18faf3fSek110237 }
2000f18faf3fSek110237
2001f18faf3fSek110237 void *
dmu_objset_get_user(objset_t * os)2002f18faf3fSek110237 dmu_objset_get_user(objset_t *os)
2003f18faf3fSek110237 {
2004503ad85cSMatthew Ahrens ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
2005503ad85cSMatthew Ahrens return (os->os_user_ptr);
2006f18faf3fSek110237 }
20073b2aab18SMatthew Ahrens
20083b2aab18SMatthew Ahrens /*
20093b2aab18SMatthew Ahrens * Determine name of filesystem, given name of snapshot.
2010*675fc291SMatthew Ahrens * buf must be at least ZFS_MAX_DATASET_NAME_LEN bytes
20113b2aab18SMatthew Ahrens */
20123b2aab18SMatthew Ahrens int
dmu_fsname(const char * snapname,char * buf)20133b2aab18SMatthew Ahrens dmu_fsname(const char *snapname, char *buf)
20143b2aab18SMatthew Ahrens {
20153b2aab18SMatthew Ahrens char *atp = strchr(snapname, '@');
20163b2aab18SMatthew Ahrens if (atp == NULL)
2017be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
2018*675fc291SMatthew Ahrens if (atp - snapname >= ZFS_MAX_DATASET_NAME_LEN)
2019be6fd75aSMatthew Ahrens return (SET_ERROR(ENAMETOOLONG));
20203b2aab18SMatthew Ahrens (void) strlcpy(buf, snapname, atp - snapname + 1);
20213b2aab18SMatthew Ahrens return (0);
20223b2aab18SMatthew Ahrens }
2023