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*40a5c998SMatthew Ahrens * Copyright (c) 2012, 2016 by Delphix. All rights reserved.
24aad02571SSaso Kiselkov * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
25a2afb611SJerry Jelinek * Copyright (c) 2013, Joyent, Inc. All rights reserved.
26bc9014e6SJustin Gibbs * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
2703b1c297SAlexander Eremin * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
2812380e1eSArne Jansen * Copyright (c) 2015, STRATO AG, Inc. All rights reserved.
29c3d26abcSMatthew Ahrens * Copyright (c) 2014 Integros [integros.com]
30fa9e4066Sahrens */
31fa9e4066Sahrens
3255da60b9SMark J Musante /* Portions Copyright 2010 Robert Milkowski */
3355da60b9SMark J Musante
34ecd6cf80Smarks #include <sys/cred.h>
35fa9e4066Sahrens #include <sys/zfs_context.h>
36fa9e4066Sahrens #include <sys/dmu_objset.h>
37fa9e4066Sahrens #include <sys/dsl_dir.h>
38fa9e4066Sahrens #include <sys/dsl_dataset.h>
39fa9e4066Sahrens #include <sys/dsl_prop.h>
40fa9e4066Sahrens #include <sys/dsl_pool.h>
411d452cf5Sahrens #include <sys/dsl_synctask.h>
42ecd6cf80Smarks #include <sys/dsl_deleg.h>
43fa9e4066Sahrens #include <sys/dnode.h>
44fa9e4066Sahrens #include <sys/dbuf.h>
45a2eea2e1Sahrens #include <sys/zvol.h>
46fa9e4066Sahrens #include <sys/dmu_tx.h>
47fa9e4066Sahrens #include <sys/zap.h>
48fa9e4066Sahrens #include <sys/zil.h>
49fa9e4066Sahrens #include <sys/dmu_impl.h>
50ecd6cf80Smarks #include <sys/zfs_ioctl.h>
510a586ceaSMark Shellenbaum #include <sys/sa.h>
5299d5e173STim Haley #include <sys/zfs_onexit.h>
533b2aab18SMatthew Ahrens #include <sys/dsl_destroy.h>
5412380e1eSArne Jansen #include <sys/vdev.h>
55fa9e4066Sahrens
56744947dcSTom Erickson /*
57744947dcSTom Erickson * Needed to close a window in dnode_move() that allows the objset to be freed
58744947dcSTom Erickson * before it can be safely accessed.
59744947dcSTom Erickson */
60744947dcSTom Erickson krwlock_t os_lock;
61744947dcSTom Erickson
6212380e1eSArne Jansen /*
6312380e1eSArne Jansen * Tunable to overwrite the maximum number of threads for the parallization
6412380e1eSArne Jansen * of dmu_objset_find_dp, needed to speed up the import of pools with many
6512380e1eSArne Jansen * datasets.
6612380e1eSArne Jansen * Default is 4 times the number of leaf vdevs.
6712380e1eSArne Jansen */
6812380e1eSArne Jansen int dmu_find_threads = 0;
6912380e1eSArne Jansen
7012380e1eSArne Jansen static void dmu_objset_find_dp_cb(void *arg);
7112380e1eSArne Jansen
72744947dcSTom Erickson void
dmu_objset_init(void)73744947dcSTom Erickson dmu_objset_init(void)
74744947dcSTom Erickson {
75744947dcSTom Erickson rw_init(&os_lock, NULL, RW_DEFAULT, NULL);
76744947dcSTom Erickson }
77744947dcSTom Erickson
78744947dcSTom Erickson void
dmu_objset_fini(void)79744947dcSTom Erickson dmu_objset_fini(void)
80744947dcSTom Erickson {
81744947dcSTom Erickson rw_destroy(&os_lock);
82744947dcSTom Erickson }
83744947dcSTom Erickson
84fa9e4066Sahrens spa_t *
dmu_objset_spa(objset_t * os)85fa9e4066Sahrens dmu_objset_spa(objset_t *os)
86fa9e4066Sahrens {
87503ad85cSMatthew Ahrens return (os->os_spa);
88fa9e4066Sahrens }
89fa9e4066Sahrens
90fa9e4066Sahrens zilog_t *
dmu_objset_zil(objset_t * os)91fa9e4066Sahrens dmu_objset_zil(objset_t *os)
92fa9e4066Sahrens {
93503ad85cSMatthew Ahrens return (os->os_zil);
94fa9e4066Sahrens }
95fa9e4066Sahrens
96fa9e4066Sahrens dsl_pool_t *
dmu_objset_pool(objset_t * os)97fa9e4066Sahrens dmu_objset_pool(objset_t *os)
98fa9e4066Sahrens {
99fa9e4066Sahrens dsl_dataset_t *ds;
100fa9e4066Sahrens
101503ad85cSMatthew Ahrens if ((ds = os->os_dsl_dataset) != NULL && ds->ds_dir)
102fa9e4066Sahrens return (ds->ds_dir->dd_pool);
103fa9e4066Sahrens else
104503ad85cSMatthew Ahrens return (spa_get_dsl(os->os_spa));
105fa9e4066Sahrens }
106fa9e4066Sahrens
107fa9e4066Sahrens dsl_dataset_t *
dmu_objset_ds(objset_t * os)108fa9e4066Sahrens dmu_objset_ds(objset_t *os)
109fa9e4066Sahrens {
110503ad85cSMatthew Ahrens return (os->os_dsl_dataset);
111fa9e4066Sahrens }
112fa9e4066Sahrens
113fa9e4066Sahrens dmu_objset_type_t
dmu_objset_type(objset_t * os)114fa9e4066Sahrens dmu_objset_type(objset_t *os)
115fa9e4066Sahrens {
116503ad85cSMatthew Ahrens return (os->os_phys->os_type);
117fa9e4066Sahrens }
118fa9e4066Sahrens
119fa9e4066Sahrens void
dmu_objset_name(objset_t * os,char * buf)120fa9e4066Sahrens dmu_objset_name(objset_t *os, char *buf)
121fa9e4066Sahrens {
122503ad85cSMatthew Ahrens dsl_dataset_name(os->os_dsl_dataset, buf);
123fa9e4066Sahrens }
124fa9e4066Sahrens
125fa9e4066Sahrens uint64_t
dmu_objset_id(objset_t * os)126fa9e4066Sahrens dmu_objset_id(objset_t *os)
127fa9e4066Sahrens {
128503ad85cSMatthew Ahrens dsl_dataset_t *ds = os->os_dsl_dataset;
129fa9e4066Sahrens
130fa9e4066Sahrens return (ds ? ds->ds_object : 0);
131fa9e4066Sahrens }
132fa9e4066Sahrens
133edf345e6SMatthew Ahrens zfs_sync_type_t
dmu_objset_syncprop(objset_t * os)13455da60b9SMark J Musante dmu_objset_syncprop(objset_t *os)
13555da60b9SMark J Musante {
13655da60b9SMark J Musante return (os->os_sync);
13755da60b9SMark J Musante }
13855da60b9SMark J Musante
139edf345e6SMatthew Ahrens zfs_logbias_op_t
dmu_objset_logbias(objset_t * os)140e09fa4daSNeil Perrin dmu_objset_logbias(objset_t *os)
141e09fa4daSNeil Perrin {
142e09fa4daSNeil Perrin return (os->os_logbias);
143e09fa4daSNeil Perrin }
144e09fa4daSNeil Perrin
145fa9e4066Sahrens static void
checksum_changed_cb(void * arg,uint64_t newval)146fa9e4066Sahrens checksum_changed_cb(void *arg, uint64_t newval)
147fa9e4066Sahrens {
148503ad85cSMatthew Ahrens objset_t *os = arg;
149fa9e4066Sahrens
150fa9e4066Sahrens /*
151fa9e4066Sahrens * Inheritance should have been done by now.
152fa9e4066Sahrens */
153fa9e4066Sahrens ASSERT(newval != ZIO_CHECKSUM_INHERIT);
154fa9e4066Sahrens
155503ad85cSMatthew Ahrens os->os_checksum = zio_checksum_select(newval, ZIO_CHECKSUM_ON_VALUE);
156fa9e4066Sahrens }
157fa9e4066Sahrens
158fa9e4066Sahrens static void
compression_changed_cb(void * arg,uint64_t newval)159fa9e4066Sahrens compression_changed_cb(void *arg, uint64_t newval)
160fa9e4066Sahrens {
161503ad85cSMatthew Ahrens objset_t *os = arg;
162fa9e4066Sahrens
163fa9e4066Sahrens /*
164fa9e4066Sahrens * Inheritance and range checking should have been done by now.
165fa9e4066Sahrens */
166fa9e4066Sahrens ASSERT(newval != ZIO_COMPRESS_INHERIT);
167fa9e4066Sahrens
168db1741f5SJustin T. Gibbs os->os_compress = zio_compress_select(os->os_spa, newval,
169db1741f5SJustin T. Gibbs ZIO_COMPRESS_ON);
170fa9e4066Sahrens }
171fa9e4066Sahrens
172d0ad202dSahrens static void
copies_changed_cb(void * arg,uint64_t newval)173d0ad202dSahrens copies_changed_cb(void *arg, uint64_t newval)
174d0ad202dSahrens {
175503ad85cSMatthew Ahrens objset_t *os = arg;
176d0ad202dSahrens
177d0ad202dSahrens /*
178d0ad202dSahrens * Inheritance and range checking should have been done by now.
179d0ad202dSahrens */
180d0ad202dSahrens ASSERT(newval > 0);
181503ad85cSMatthew Ahrens ASSERT(newval <= spa_max_replication(os->os_spa));
182d0ad202dSahrens
183503ad85cSMatthew Ahrens os->os_copies = newval;
184d0ad202dSahrens }
185d0ad202dSahrens
1863baa08fcSek110237 static void
dedup_changed_cb(void * arg,uint64_t newval)187b24ab676SJeff Bonwick dedup_changed_cb(void *arg, uint64_t newval)
188b24ab676SJeff Bonwick {
189b24ab676SJeff Bonwick objset_t *os = arg;
190b24ab676SJeff Bonwick spa_t *spa = os->os_spa;
191b24ab676SJeff Bonwick enum zio_checksum checksum;
192b24ab676SJeff Bonwick
193b24ab676SJeff Bonwick /*
194b24ab676SJeff Bonwick * Inheritance should have been done by now.
195b24ab676SJeff Bonwick */
196b24ab676SJeff Bonwick ASSERT(newval != ZIO_CHECKSUM_INHERIT);
197b24ab676SJeff Bonwick
198b24ab676SJeff Bonwick checksum = zio_checksum_dedup_select(spa, newval, ZIO_CHECKSUM_OFF);
199b24ab676SJeff Bonwick
200b24ab676SJeff Bonwick os->os_dedup_checksum = checksum & ZIO_CHECKSUM_MASK;
201b24ab676SJeff Bonwick os->os_dedup_verify = !!(checksum & ZIO_CHECKSUM_VERIFY);
202b24ab676SJeff Bonwick }
203b24ab676SJeff Bonwick
204b24ab676SJeff Bonwick static void
primary_cache_changed_cb(void * arg,uint64_t newval)2053baa08fcSek110237 primary_cache_changed_cb(void *arg, uint64_t newval)
2063baa08fcSek110237 {
207503ad85cSMatthew Ahrens objset_t *os = arg;
2083baa08fcSek110237
2093baa08fcSek110237 /*
2103baa08fcSek110237 * Inheritance and range checking should have been done by now.
2113baa08fcSek110237 */
2123baa08fcSek110237 ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
2133baa08fcSek110237 newval == ZFS_CACHE_METADATA);
2143baa08fcSek110237
215503ad85cSMatthew Ahrens os->os_primary_cache = newval;
2163baa08fcSek110237 }
2173baa08fcSek110237
2183baa08fcSek110237 static void
secondary_cache_changed_cb(void * arg,uint64_t newval)2193baa08fcSek110237 secondary_cache_changed_cb(void *arg, uint64_t newval)
2203baa08fcSek110237 {
221503ad85cSMatthew Ahrens objset_t *os = arg;
2223baa08fcSek110237
2233baa08fcSek110237 /*
2243baa08fcSek110237 * Inheritance and range checking should have been done by now.
2253baa08fcSek110237 */
2263baa08fcSek110237 ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
2273baa08fcSek110237 newval == ZFS_CACHE_METADATA);
2283baa08fcSek110237
229503ad85cSMatthew Ahrens os->os_secondary_cache = newval;
2303baa08fcSek110237 }
2313baa08fcSek110237
232e09fa4daSNeil Perrin static void
sync_changed_cb(void * arg,uint64_t newval)23355da60b9SMark J Musante sync_changed_cb(void *arg, uint64_t newval)
23455da60b9SMark J Musante {
23555da60b9SMark J Musante objset_t *os = arg;
23655da60b9SMark J Musante
23755da60b9SMark J Musante /*
23855da60b9SMark J Musante * Inheritance and range checking should have been done by now.
23955da60b9SMark J Musante */
24055da60b9SMark J Musante ASSERT(newval == ZFS_SYNC_STANDARD || newval == ZFS_SYNC_ALWAYS ||
24155da60b9SMark J Musante newval == ZFS_SYNC_DISABLED);
24255da60b9SMark J Musante
24355da60b9SMark J Musante os->os_sync = newval;
24455da60b9SMark J Musante if (os->os_zil)
24555da60b9SMark J Musante zil_set_sync(os->os_zil, newval);
24655da60b9SMark J Musante }
24755da60b9SMark J Musante
24855da60b9SMark J Musante static void
redundant_metadata_changed_cb(void * arg,uint64_t newval)249edf345e6SMatthew Ahrens redundant_metadata_changed_cb(void *arg, uint64_t newval)
250edf345e6SMatthew Ahrens {
251edf345e6SMatthew Ahrens objset_t *os = arg;
252edf345e6SMatthew Ahrens
253edf345e6SMatthew Ahrens /*
254edf345e6SMatthew Ahrens * Inheritance and range checking should have been done by now.
255edf345e6SMatthew Ahrens */
256edf345e6SMatthew Ahrens ASSERT(newval == ZFS_REDUNDANT_METADATA_ALL ||
257edf345e6SMatthew Ahrens newval == ZFS_REDUNDANT_METADATA_MOST);
258edf345e6SMatthew Ahrens
259edf345e6SMatthew Ahrens os->os_redundant_metadata = newval;
260edf345e6SMatthew Ahrens }
261edf345e6SMatthew Ahrens
262edf345e6SMatthew Ahrens static void
logbias_changed_cb(void * arg,uint64_t newval)263e09fa4daSNeil Perrin logbias_changed_cb(void *arg, uint64_t newval)
264e09fa4daSNeil Perrin {
265e09fa4daSNeil Perrin objset_t *os = arg;
266e09fa4daSNeil Perrin
267e09fa4daSNeil Perrin ASSERT(newval == ZFS_LOGBIAS_LATENCY ||
268e09fa4daSNeil Perrin newval == ZFS_LOGBIAS_THROUGHPUT);
269e09fa4daSNeil Perrin os->os_logbias = newval;
270e09fa4daSNeil Perrin if (os->os_zil)
271e09fa4daSNeil Perrin zil_set_logbias(os->os_zil, newval);
272e09fa4daSNeil Perrin }
273e09fa4daSNeil Perrin
274b5152584SMatthew Ahrens static void
recordsize_changed_cb(void * arg,uint64_t newval)275b5152584SMatthew Ahrens recordsize_changed_cb(void *arg, uint64_t newval)
276b5152584SMatthew Ahrens {
277b5152584SMatthew Ahrens objset_t *os = arg;
278b5152584SMatthew Ahrens
279b5152584SMatthew Ahrens os->os_recordsize = newval;
280b5152584SMatthew Ahrens }
281b5152584SMatthew Ahrens
282fa9e4066Sahrens void
dmu_objset_byteswap(void * buf,size_t size)283fa9e4066Sahrens dmu_objset_byteswap(void *buf, size_t size)
284fa9e4066Sahrens {
285fa9e4066Sahrens objset_phys_t *osp = buf;
286fa9e4066Sahrens
28714843421SMatthew Ahrens ASSERT(size == OBJSET_OLD_PHYS_SIZE || size == sizeof (objset_phys_t));
288fa9e4066Sahrens dnode_byteswap(&osp->os_meta_dnode);
289fa9e4066Sahrens byteswap_uint64_array(&osp->os_zil_header, sizeof (zil_header_t));
290fa9e4066Sahrens osp->os_type = BSWAP_64(osp->os_type);
29114843421SMatthew Ahrens osp->os_flags = BSWAP_64(osp->os_flags);
29214843421SMatthew Ahrens if (size == sizeof (objset_phys_t)) {
29314843421SMatthew Ahrens dnode_byteswap(&osp->os_userused_dnode);
29414843421SMatthew Ahrens dnode_byteswap(&osp->os_groupused_dnode);
29514843421SMatthew Ahrens }
296fa9e4066Sahrens }
297fa9e4066Sahrens
298ea8dc4b6Seschrock int
dmu_objset_open_impl(spa_t * spa,dsl_dataset_t * ds,blkptr_t * bp,objset_t ** osp)299ea8dc4b6Seschrock dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
300503ad85cSMatthew Ahrens objset_t **osp)
301fa9e4066Sahrens {
302503ad85cSMatthew Ahrens objset_t *os;
303088f3894Sahrens int i, err;
304fa9e4066Sahrens
30591ebeef5Sahrens ASSERT(ds == NULL || MUTEX_HELD(&ds->ds_opening_lock));
30691ebeef5Sahrens
307503ad85cSMatthew Ahrens os = kmem_zalloc(sizeof (objset_t), KM_SLEEP);
308503ad85cSMatthew Ahrens os->os_dsl_dataset = ds;
309503ad85cSMatthew Ahrens os->os_spa = spa;
310503ad85cSMatthew Ahrens os->os_rootbp = bp;
311503ad85cSMatthew Ahrens if (!BP_IS_HOLE(os->os_rootbp)) {
3127adb730bSGeorge Wilson arc_flags_t aflags = ARC_FLAG_WAIT;
3137802d7bfSMatthew Ahrens zbookmark_phys_t zb;
314b24ab676SJeff Bonwick SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET,
315b24ab676SJeff Bonwick ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
316b24ab676SJeff Bonwick
317503ad85cSMatthew Ahrens if (DMU_OS_IS_L2CACHEABLE(os))
3187adb730bSGeorge Wilson aflags |= ARC_FLAG_L2CACHE;
319aad02571SSaso Kiselkov if (DMU_OS_IS_L2COMPRESSIBLE(os))
3207adb730bSGeorge Wilson aflags |= ARC_FLAG_L2COMPRESS;
321ea8dc4b6Seschrock
322503ad85cSMatthew Ahrens dprintf_bp(os->os_rootbp, "reading %s", "");
3231b912ec7SGeorge Wilson err = arc_read(NULL, spa, os->os_rootbp,
324503ad85cSMatthew Ahrens arc_getbuf_func, &os->os_phys_buf,
32513506d1eSmaybee ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &aflags, &zb);
3263b2aab18SMatthew Ahrens if (err != 0) {
327503ad85cSMatthew Ahrens kmem_free(os, sizeof (objset_t));
328b87f3af3Sperrin /* convert checksum errors into IO errors */
329b87f3af3Sperrin if (err == ECKSUM)
330be6fd75aSMatthew Ahrens err = SET_ERROR(EIO);
331ea8dc4b6Seschrock return (err);
332ea8dc4b6Seschrock }
33314843421SMatthew Ahrens
33414843421SMatthew Ahrens /* Increase the blocksize if we are permitted. */
33514843421SMatthew Ahrens if (spa_version(spa) >= SPA_VERSION_USERSPACE &&
336503ad85cSMatthew Ahrens arc_buf_size(os->os_phys_buf) < sizeof (objset_phys_t)) {
33714843421SMatthew Ahrens arc_buf_t *buf = arc_buf_alloc(spa,
338503ad85cSMatthew Ahrens sizeof (objset_phys_t), &os->os_phys_buf,
33914843421SMatthew Ahrens ARC_BUFC_METADATA);
34014843421SMatthew Ahrens bzero(buf->b_data, sizeof (objset_phys_t));
341503ad85cSMatthew Ahrens bcopy(os->os_phys_buf->b_data, buf->b_data,
342503ad85cSMatthew Ahrens arc_buf_size(os->os_phys_buf));
343503ad85cSMatthew Ahrens (void) arc_buf_remove_ref(os->os_phys_buf,
344503ad85cSMatthew Ahrens &os->os_phys_buf);
345503ad85cSMatthew Ahrens os->os_phys_buf = buf;
34614843421SMatthew Ahrens }
34714843421SMatthew Ahrens
348503ad85cSMatthew Ahrens os->os_phys = os->os_phys_buf->b_data;
349503ad85cSMatthew Ahrens os->os_flags = os->os_phys->os_flags;
350fa9e4066Sahrens } else {
35114843421SMatthew Ahrens int size = spa_version(spa) >= SPA_VERSION_USERSPACE ?
35214843421SMatthew Ahrens sizeof (objset_phys_t) : OBJSET_OLD_PHYS_SIZE;
353503ad85cSMatthew Ahrens os->os_phys_buf = arc_buf_alloc(spa, size,
354503ad85cSMatthew Ahrens &os->os_phys_buf, ARC_BUFC_METADATA);
355503ad85cSMatthew Ahrens os->os_phys = os->os_phys_buf->b_data;
356503ad85cSMatthew Ahrens bzero(os->os_phys, size);
357fa9e4066Sahrens }
358fa9e4066Sahrens
359fa9e4066Sahrens /*
360fa9e4066Sahrens * Note: the changed_cb will be called once before the register
361fa9e4066Sahrens * func returns, thus changing the checksum/compression from the
3623baa08fcSek110237 * default (fletcher2/off). Snapshots don't need to know about
3633baa08fcSek110237 * checksum/compression/copies.
364fa9e4066Sahrens */
3655d7b4d43SMatthew Ahrens if (ds != NULL) {
3669c3fd121SMatthew Ahrens boolean_t needlock = B_FALSE;
3679c3fd121SMatthew Ahrens
3689c3fd121SMatthew Ahrens /*
3699c3fd121SMatthew Ahrens * Note: it's valid to open the objset if the dataset is
3709c3fd121SMatthew Ahrens * long-held, in which case the pool_config lock will not
3719c3fd121SMatthew Ahrens * be held.
3729c3fd121SMatthew Ahrens */
3739c3fd121SMatthew Ahrens if (!dsl_pool_config_held(dmu_objset_pool(os))) {
3749c3fd121SMatthew Ahrens needlock = B_TRUE;
3759c3fd121SMatthew Ahrens dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
3769c3fd121SMatthew Ahrens }
3773b2aab18SMatthew Ahrens err = dsl_prop_register(ds,
3783b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_PRIMARYCACHE),
379503ad85cSMatthew Ahrens primary_cache_changed_cb, os);
3803b2aab18SMatthew Ahrens if (err == 0) {
3813b2aab18SMatthew Ahrens err = dsl_prop_register(ds,
3823b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_SECONDARYCACHE),
383503ad85cSMatthew Ahrens secondary_cache_changed_cb, os);
3843b2aab18SMatthew Ahrens }
385bc9014e6SJustin Gibbs if (!ds->ds_is_snapshot) {
3863b2aab18SMatthew Ahrens if (err == 0) {
3873b2aab18SMatthew Ahrens err = dsl_prop_register(ds,
3883b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_CHECKSUM),
389503ad85cSMatthew Ahrens checksum_changed_cb, os);
3903b2aab18SMatthew Ahrens }
3913b2aab18SMatthew Ahrens if (err == 0) {
3923b2aab18SMatthew Ahrens err = dsl_prop_register(ds,
3933b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_COMPRESSION),
394503ad85cSMatthew Ahrens compression_changed_cb, os);
3953b2aab18SMatthew Ahrens }
3963b2aab18SMatthew Ahrens if (err == 0) {
3973b2aab18SMatthew Ahrens err = dsl_prop_register(ds,
3983b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_COPIES),
399503ad85cSMatthew Ahrens copies_changed_cb, os);
4003b2aab18SMatthew Ahrens }
4013b2aab18SMatthew Ahrens if (err == 0) {
4023b2aab18SMatthew Ahrens err = dsl_prop_register(ds,
4033b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_DEDUP),
404b24ab676SJeff Bonwick dedup_changed_cb, os);
4053b2aab18SMatthew Ahrens }
4063b2aab18SMatthew Ahrens if (err == 0) {
4073b2aab18SMatthew Ahrens err = dsl_prop_register(ds,
4083b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_LOGBIAS),
409e09fa4daSNeil Perrin logbias_changed_cb, os);
4103b2aab18SMatthew Ahrens }
4113b2aab18SMatthew Ahrens if (err == 0) {
4123b2aab18SMatthew Ahrens err = dsl_prop_register(ds,
4133b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_SYNC),
41455da60b9SMark J Musante sync_changed_cb, os);
4153baa08fcSek110237 }
416edf345e6SMatthew Ahrens if (err == 0) {
417edf345e6SMatthew Ahrens err = dsl_prop_register(ds,
418edf345e6SMatthew Ahrens zfs_prop_to_name(
419edf345e6SMatthew Ahrens ZFS_PROP_REDUNDANT_METADATA),
420edf345e6SMatthew Ahrens redundant_metadata_changed_cb, os);
421edf345e6SMatthew Ahrens }
422b5152584SMatthew Ahrens if (err == 0) {
423b5152584SMatthew Ahrens err = dsl_prop_register(ds,
424b5152584SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_RECORDSIZE),
425b5152584SMatthew Ahrens recordsize_changed_cb, os);
426b5152584SMatthew Ahrens }
4273b2aab18SMatthew Ahrens }
4289c3fd121SMatthew Ahrens if (needlock)
4299c3fd121SMatthew Ahrens dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
4303b2aab18SMatthew Ahrens if (err != 0) {
431503ad85cSMatthew Ahrens VERIFY(arc_buf_remove_ref(os->os_phys_buf,
4323b2aab18SMatthew Ahrens &os->os_phys_buf));
433503ad85cSMatthew Ahrens kmem_free(os, sizeof (objset_t));
434ea8dc4b6Seschrock return (err);
435ea8dc4b6Seschrock }
4365d7b4d43SMatthew Ahrens } else {
437fa9e4066Sahrens /* It's the meta-objset. */
438503ad85cSMatthew Ahrens os->os_checksum = ZIO_CHECKSUM_FLETCHER_4;
439db1741f5SJustin T. Gibbs os->os_compress = ZIO_COMPRESS_ON;
440503ad85cSMatthew Ahrens os->os_copies = spa_max_replication(spa);
441b24ab676SJeff Bonwick os->os_dedup_checksum = ZIO_CHECKSUM_OFF;
442edf345e6SMatthew Ahrens os->os_dedup_verify = B_FALSE;
443edf345e6SMatthew Ahrens os->os_logbias = ZFS_LOGBIAS_LATENCY;
444edf345e6SMatthew Ahrens os->os_sync = ZFS_SYNC_STANDARD;
445503ad85cSMatthew Ahrens os->os_primary_cache = ZFS_CACHE_ALL;
446503ad85cSMatthew Ahrens os->os_secondary_cache = ZFS_CACHE_ALL;
447fa9e4066Sahrens }
448fa9e4066Sahrens
449bc9014e6SJustin Gibbs if (ds == NULL || !ds->ds_is_snapshot)
450503ad85cSMatthew Ahrens os->os_zil_header = os->os_phys->os_zil_header;
451503ad85cSMatthew Ahrens os->os_zil = zil_alloc(os, &os->os_zil_header);
452fa9e4066Sahrens
453fa9e4066Sahrens for (i = 0; i < TXG_SIZE; i++) {
454503ad85cSMatthew Ahrens list_create(&os->os_dirty_dnodes[i], sizeof (dnode_t),
455fa9e4066Sahrens offsetof(dnode_t, dn_dirty_link[i]));
456503ad85cSMatthew Ahrens list_create(&os->os_free_dnodes[i], sizeof (dnode_t),
457fa9e4066Sahrens offsetof(dnode_t, dn_dirty_link[i]));
458fa9e4066Sahrens }
459503ad85cSMatthew Ahrens list_create(&os->os_dnodes, sizeof (dnode_t),
460fa9e4066Sahrens offsetof(dnode_t, dn_link));
461503ad85cSMatthew Ahrens list_create(&os->os_downgraded_dbufs, sizeof (dmu_buf_impl_t),
462fa9e4066Sahrens offsetof(dmu_buf_impl_t, db_link));
463fa9e4066Sahrens
464503ad85cSMatthew Ahrens mutex_init(&os->os_lock, NULL, MUTEX_DEFAULT, NULL);
465503ad85cSMatthew Ahrens mutex_init(&os->os_obj_lock, NULL, MUTEX_DEFAULT, NULL);
466503ad85cSMatthew Ahrens mutex_init(&os->os_user_ptr_lock, NULL, MUTEX_DEFAULT, NULL);
4675ad82045Snd150628
468bc9014e6SJustin Gibbs dnode_special_open(os, &os->os_phys->os_meta_dnode,
469bc9014e6SJustin Gibbs DMU_META_DNODE_OBJECT, &os->os_meta_dnode);
470503ad85cSMatthew Ahrens if (arc_buf_size(os->os_phys_buf) >= sizeof (objset_phys_t)) {
471bc9014e6SJustin Gibbs dnode_special_open(os, &os->os_phys->os_userused_dnode,
472bc9014e6SJustin Gibbs DMU_USERUSED_OBJECT, &os->os_userused_dnode);
473bc9014e6SJustin Gibbs dnode_special_open(os, &os->os_phys->os_groupused_dnode,
474bc9014e6SJustin Gibbs DMU_GROUPUSED_OBJECT, &os->os_groupused_dnode);
47514843421SMatthew Ahrens }
476fa9e4066Sahrens
477503ad85cSMatthew Ahrens *osp = os;
4783cb34c60Sahrens return (0);
4793cb34c60Sahrens }
4803cb34c60Sahrens
4813cb34c60Sahrens int
dmu_objset_from_ds(dsl_dataset_t * ds,objset_t ** osp)482503ad85cSMatthew Ahrens dmu_objset_from_ds(dsl_dataset_t *ds, objset_t **osp)
4833cb34c60Sahrens {
484503ad85cSMatthew Ahrens int err = 0;
4853cb34c60Sahrens
4869c3fd121SMatthew Ahrens /*
4879c3fd121SMatthew Ahrens * We shouldn't be doing anything with dsl_dataset_t's unless the
4889c3fd121SMatthew Ahrens * pool_config lock is held, or the dataset is long-held.
4899c3fd121SMatthew Ahrens */
4909c3fd121SMatthew Ahrens ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool) ||
4919c3fd121SMatthew Ahrens dsl_dataset_long_held(ds));
4929c3fd121SMatthew Ahrens
493503ad85cSMatthew Ahrens mutex_enter(&ds->ds_opening_lock);
4945d7b4d43SMatthew Ahrens if (ds->ds_objset == NULL) {
4955d7b4d43SMatthew Ahrens objset_t *os;
496503ad85cSMatthew Ahrens err = dmu_objset_open_impl(dsl_dataset_get_spa(ds),
4975d7b4d43SMatthew Ahrens ds, dsl_dataset_get_blkptr(ds), &os);
4985d7b4d43SMatthew Ahrens
4995d7b4d43SMatthew Ahrens if (err == 0) {
5005d7b4d43SMatthew Ahrens mutex_enter(&ds->ds_lock);
5015d7b4d43SMatthew Ahrens ASSERT(ds->ds_objset == NULL);
5025d7b4d43SMatthew Ahrens ds->ds_objset = os;
5035d7b4d43SMatthew Ahrens mutex_exit(&ds->ds_lock);
504503ad85cSMatthew Ahrens }
5055d7b4d43SMatthew Ahrens }
5065d7b4d43SMatthew Ahrens *osp = ds->ds_objset;
507503ad85cSMatthew Ahrens mutex_exit(&ds->ds_opening_lock);
5083cb34c60Sahrens return (err);
5093cb34c60Sahrens }
5103cb34c60Sahrens
5113b2aab18SMatthew Ahrens /*
5123b2aab18SMatthew Ahrens * Holds the pool while the objset is held. Therefore only one objset
5133b2aab18SMatthew Ahrens * can be held at a time.
5143b2aab18SMatthew Ahrens */
515fa9e4066Sahrens int
dmu_objset_hold(const char * name,void * tag,objset_t ** osp)516503ad85cSMatthew Ahrens dmu_objset_hold(const char *name, void *tag, objset_t **osp)
517fa9e4066Sahrens {
5183b2aab18SMatthew Ahrens dsl_pool_t *dp;
519f18faf3fSek110237 dsl_dataset_t *ds;
520f18faf3fSek110237 int err;
521fa9e4066Sahrens
5223b2aab18SMatthew Ahrens err = dsl_pool_hold(name, tag, &dp);
5233b2aab18SMatthew Ahrens if (err != 0)
524503ad85cSMatthew Ahrens return (err);
5253b2aab18SMatthew Ahrens err = dsl_dataset_hold(dp, name, tag, &ds);
5263b2aab18SMatthew Ahrens if (err != 0) {
5273b2aab18SMatthew Ahrens dsl_pool_rele(dp, tag);
5283b2aab18SMatthew Ahrens return (err);
5293b2aab18SMatthew Ahrens }
5303cb34c60Sahrens
531503ad85cSMatthew Ahrens err = dmu_objset_from_ds(ds, osp);
5323b2aab18SMatthew Ahrens if (err != 0) {
533503ad85cSMatthew Ahrens dsl_dataset_rele(ds, tag);
5343b2aab18SMatthew Ahrens dsl_pool_rele(dp, tag);
5353b2aab18SMatthew Ahrens }
536503ad85cSMatthew Ahrens
537fa9e4066Sahrens return (err);
538fa9e4066Sahrens }
539fa9e4066Sahrens
54012380e1eSArne Jansen static int
dmu_objset_own_impl(dsl_dataset_t * ds,dmu_objset_type_t type,boolean_t readonly,void * tag,objset_t ** osp)54112380e1eSArne Jansen dmu_objset_own_impl(dsl_dataset_t *ds, dmu_objset_type_t type,
54212380e1eSArne Jansen boolean_t readonly, void *tag, objset_t **osp)
54312380e1eSArne Jansen {
54412380e1eSArne Jansen int err;
54512380e1eSArne Jansen
54612380e1eSArne Jansen err = dmu_objset_from_ds(ds, osp);
54712380e1eSArne Jansen if (err != 0) {
54812380e1eSArne Jansen dsl_dataset_disown(ds, tag);
54912380e1eSArne Jansen } else if (type != DMU_OST_ANY && type != (*osp)->os_phys->os_type) {
55012380e1eSArne Jansen dsl_dataset_disown(ds, tag);
55112380e1eSArne Jansen return (SET_ERROR(EINVAL));
55212380e1eSArne Jansen } else if (!readonly && dsl_dataset_is_snapshot(ds)) {
55312380e1eSArne Jansen dsl_dataset_disown(ds, tag);
55412380e1eSArne Jansen return (SET_ERROR(EROFS));
55512380e1eSArne Jansen }
55612380e1eSArne Jansen return (err);
55712380e1eSArne Jansen }
55812380e1eSArne Jansen
5593b2aab18SMatthew Ahrens /*
5603b2aab18SMatthew Ahrens * dsl_pool must not be held when this is called.
5613b2aab18SMatthew Ahrens * Upon successful return, there will be a longhold on the dataset,
5623b2aab18SMatthew Ahrens * and the dsl_pool will not be held.
5633b2aab18SMatthew Ahrens */
564503ad85cSMatthew Ahrens int
dmu_objset_own(const char * name,dmu_objset_type_t type,boolean_t readonly,void * tag,objset_t ** osp)565503ad85cSMatthew Ahrens dmu_objset_own(const char *name, dmu_objset_type_t type,
566503ad85cSMatthew Ahrens boolean_t readonly, void *tag, objset_t **osp)
567503ad85cSMatthew Ahrens {
5683b2aab18SMatthew Ahrens dsl_pool_t *dp;
569503ad85cSMatthew Ahrens dsl_dataset_t *ds;
570503ad85cSMatthew Ahrens int err;
571503ad85cSMatthew Ahrens
5723b2aab18SMatthew Ahrens err = dsl_pool_hold(name, FTAG, &dp);
5733b2aab18SMatthew Ahrens if (err != 0)
574503ad85cSMatthew Ahrens return (err);
5753b2aab18SMatthew Ahrens err = dsl_dataset_own(dp, name, tag, &ds);
5763b2aab18SMatthew Ahrens if (err != 0) {
5773b2aab18SMatthew Ahrens dsl_pool_rele(dp, FTAG);
5783b2aab18SMatthew Ahrens return (err);
5793b2aab18SMatthew Ahrens }
58012380e1eSArne Jansen err = dmu_objset_own_impl(ds, type, readonly, tag, osp);
5813b2aab18SMatthew Ahrens dsl_pool_rele(dp, FTAG);
58212380e1eSArne Jansen
5833cb34c60Sahrens return (err);
584fa9e4066Sahrens }
585fa9e4066Sahrens
58612380e1eSArne 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)58712380e1eSArne Jansen dmu_objset_own_obj(dsl_pool_t *dp, uint64_t obj, dmu_objset_type_t type,
58812380e1eSArne Jansen boolean_t readonly, void *tag, objset_t **osp)
58912380e1eSArne Jansen {
59012380e1eSArne Jansen dsl_dataset_t *ds;
59112380e1eSArne Jansen int err;
59212380e1eSArne Jansen
59312380e1eSArne Jansen err = dsl_dataset_own_obj(dp, obj, tag, &ds);
59412380e1eSArne Jansen if (err != 0)
59512380e1eSArne Jansen return (err);
59612380e1eSArne Jansen
59712380e1eSArne Jansen return (dmu_objset_own_impl(ds, type, readonly, tag, osp));
59812380e1eSArne Jansen }
59912380e1eSArne Jansen
600fa9e4066Sahrens void
dmu_objset_rele(objset_t * os,void * tag)601503ad85cSMatthew Ahrens dmu_objset_rele(objset_t *os, void *tag)
602fa9e4066Sahrens {
6033b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_objset_pool(os);
604503ad85cSMatthew Ahrens dsl_dataset_rele(os->os_dsl_dataset, tag);
6053b2aab18SMatthew Ahrens dsl_pool_rele(dp, tag);
606503ad85cSMatthew Ahrens }
607745cd3c5Smaybee
60891948b51SKeith M Wesolowski /*
60991948b51SKeith M Wesolowski * When we are called, os MUST refer to an objset associated with a dataset
61091948b51SKeith M Wesolowski * that is owned by 'tag'; that is, is held and long held by 'tag' and ds_owner
61191948b51SKeith M Wesolowski * == tag. We will then release and reacquire ownership of the dataset while
61291948b51SKeith M Wesolowski * holding the pool config_rwlock to avoid intervening namespace or ownership
61391948b51SKeith M Wesolowski * changes may occur.
61491948b51SKeith M Wesolowski *
61591948b51SKeith M Wesolowski * This exists solely to accommodate zfs_ioc_userspace_upgrade()'s desire to
61691948b51SKeith M Wesolowski * release the hold on its dataset and acquire a new one on the dataset of the
61791948b51SKeith M Wesolowski * same name so that it can be partially torn down and reconstructed.
61891948b51SKeith M Wesolowski */
61991948b51SKeith M Wesolowski void
dmu_objset_refresh_ownership(objset_t * os,void * tag)62091948b51SKeith M Wesolowski dmu_objset_refresh_ownership(objset_t *os, void *tag)
62191948b51SKeith M Wesolowski {
62291948b51SKeith M Wesolowski dsl_pool_t *dp;
62391948b51SKeith M Wesolowski dsl_dataset_t *ds, *newds;
624*40a5c998SMatthew Ahrens char name[ZFS_MAX_DATASET_NAME_LEN];
62591948b51SKeith M Wesolowski
62691948b51SKeith M Wesolowski ds = os->os_dsl_dataset;
62791948b51SKeith M Wesolowski VERIFY3P(ds, !=, NULL);
62891948b51SKeith M Wesolowski VERIFY3P(ds->ds_owner, ==, tag);
62991948b51SKeith M Wesolowski VERIFY(dsl_dataset_long_held(ds));
63091948b51SKeith M Wesolowski
63191948b51SKeith M Wesolowski dsl_dataset_name(ds, name);
63291948b51SKeith M Wesolowski dp = dmu_objset_pool(os);
63391948b51SKeith M Wesolowski dsl_pool_config_enter(dp, FTAG);
63491948b51SKeith M Wesolowski dmu_objset_disown(os, tag);
63591948b51SKeith M Wesolowski VERIFY0(dsl_dataset_own(dp, name, tag, &newds));
63691948b51SKeith M Wesolowski VERIFY3P(newds, ==, os->os_dsl_dataset);
63791948b51SKeith M Wesolowski dsl_pool_config_exit(dp, FTAG);
63891948b51SKeith M Wesolowski }
63991948b51SKeith M Wesolowski
640503ad85cSMatthew Ahrens void
dmu_objset_disown(objset_t * os,void * tag)641503ad85cSMatthew Ahrens dmu_objset_disown(objset_t *os, void *tag)
642503ad85cSMatthew Ahrens {
643503ad85cSMatthew Ahrens dsl_dataset_disown(os->os_dsl_dataset, tag);
644fa9e4066Sahrens }
645fa9e4066Sahrens
6463b2aab18SMatthew Ahrens void
dmu_objset_evict_dbufs(objset_t * os)6471934e92fSmaybee dmu_objset_evict_dbufs(objset_t *os)
648ea8dc4b6Seschrock {
649bc9014e6SJustin Gibbs dnode_t dn_marker;
650ea8dc4b6Seschrock dnode_t *dn;
651c543ec06Sahrens
652503ad85cSMatthew Ahrens mutex_enter(&os->os_lock);
653bc9014e6SJustin Gibbs dn = list_head(&os->os_dnodes);
654bc9014e6SJustin Gibbs while (dn != NULL) {
655ea8dc4b6Seschrock /*
656bc9014e6SJustin Gibbs * Skip dnodes without holds. We have to do this dance
657bc9014e6SJustin Gibbs * because dnode_add_ref() only works if there is already a
658bc9014e6SJustin Gibbs * hold. If the dnode has no holds, then it has no dbufs.
659ea8dc4b6Seschrock */
660bc9014e6SJustin Gibbs if (dnode_add_ref(dn, FTAG)) {
661bc9014e6SJustin Gibbs list_insert_after(&os->os_dnodes, dn, &dn_marker);
662503ad85cSMatthew Ahrens mutex_exit(&os->os_lock);
663bc9014e6SJustin Gibbs
6641934e92fSmaybee dnode_evict_dbufs(dn);
665ea8dc4b6Seschrock dnode_rele(dn, FTAG);
666bc9014e6SJustin Gibbs
667503ad85cSMatthew Ahrens mutex_enter(&os->os_lock);
668bc9014e6SJustin Gibbs dn = list_next(&os->os_dnodes, &dn_marker);
669bc9014e6SJustin Gibbs list_remove(&os->os_dnodes, &dn_marker);
670bc9014e6SJustin Gibbs } else {
671bc9014e6SJustin Gibbs dn = list_next(&os->os_dnodes, dn);
672bc9014e6SJustin Gibbs }
673ea8dc4b6Seschrock }
674503ad85cSMatthew Ahrens mutex_exit(&os->os_lock);
675bc9014e6SJustin Gibbs
676bc9014e6SJustin Gibbs if (DMU_USERUSED_DNODE(os) != NULL) {
677bc9014e6SJustin Gibbs dnode_evict_dbufs(DMU_GROUPUSED_DNODE(os));
678bc9014e6SJustin Gibbs dnode_evict_dbufs(DMU_USERUSED_DNODE(os));
679bc9014e6SJustin Gibbs }
680bc9014e6SJustin Gibbs dnode_evict_dbufs(DMU_META_DNODE(os));
681ea8dc4b6Seschrock }
682ea8dc4b6Seschrock
683bc9014e6SJustin Gibbs /*
684bc9014e6SJustin Gibbs * Objset eviction processing is split into into two pieces.
685bc9014e6SJustin Gibbs * The first marks the objset as evicting, evicts any dbufs that
686bc9014e6SJustin Gibbs * have a refcount of zero, and then queues up the objset for the
687bc9014e6SJustin Gibbs * second phase of eviction. Once os->os_dnodes has been cleared by
688bc9014e6SJustin Gibbs * dnode_buf_pageout()->dnode_destroy(), the second phase is executed.
689bc9014e6SJustin Gibbs * The second phase closes the special dnodes, dequeues the objset from
690bc9014e6SJustin Gibbs * the list of those undergoing eviction, and finally frees the objset.
691bc9014e6SJustin Gibbs *
692bc9014e6SJustin Gibbs * NOTE: Due to asynchronous eviction processing (invocation of
693bc9014e6SJustin Gibbs * dnode_buf_pageout()), it is possible for the meta dnode for the
694bc9014e6SJustin Gibbs * objset to have no holds even though os->os_dnodes is not empty.
695bc9014e6SJustin Gibbs */
696ea8dc4b6Seschrock void
dmu_objset_evict(objset_t * os)697503ad85cSMatthew Ahrens dmu_objset_evict(objset_t *os)
698fa9e4066Sahrens {
699503ad85cSMatthew Ahrens dsl_dataset_t *ds = os->os_dsl_dataset;
700fa9e4066Sahrens
701b24ab676SJeff Bonwick for (int t = 0; t < TXG_SIZE; t++)
702b24ab676SJeff Bonwick ASSERT(!dmu_objset_is_dirty(os, t));
703fa9e4066Sahrens
70403bad06fSJustin Gibbs if (ds)
70503bad06fSJustin Gibbs dsl_prop_unregister_all(ds, os);
706fa9e4066Sahrens
7070a586ceaSMark Shellenbaum if (os->os_sa)
7080a586ceaSMark Shellenbaum sa_tear_down(os);
7090a586ceaSMark Shellenbaum
7103b2aab18SMatthew Ahrens dmu_objset_evict_dbufs(os);
711ea8dc4b6Seschrock
712bc9014e6SJustin Gibbs mutex_enter(&os->os_lock);
713bc9014e6SJustin Gibbs spa_evicting_os_register(os->os_spa, os);
714bc9014e6SJustin Gibbs if (list_is_empty(&os->os_dnodes)) {
715bc9014e6SJustin Gibbs mutex_exit(&os->os_lock);
716bc9014e6SJustin Gibbs dmu_objset_evict_done(os);
717bc9014e6SJustin Gibbs } else {
718bc9014e6SJustin Gibbs mutex_exit(&os->os_lock);
719bc9014e6SJustin Gibbs }
720bc9014e6SJustin Gibbs }
721bc9014e6SJustin Gibbs
722bc9014e6SJustin Gibbs void
dmu_objset_evict_done(objset_t * os)723bc9014e6SJustin Gibbs dmu_objset_evict_done(objset_t *os)
724bc9014e6SJustin Gibbs {
725bc9014e6SJustin Gibbs ASSERT3P(list_head(&os->os_dnodes), ==, NULL);
726bc9014e6SJustin Gibbs
727744947dcSTom Erickson dnode_special_close(&os->os_meta_dnode);
728744947dcSTom Erickson if (DMU_USERUSED_DNODE(os)) {
729744947dcSTom Erickson dnode_special_close(&os->os_userused_dnode);
730744947dcSTom Erickson dnode_special_close(&os->os_groupused_dnode);
73114843421SMatthew Ahrens }
732503ad85cSMatthew Ahrens zil_free(os->os_zil);
733fa9e4066Sahrens
7343b2aab18SMatthew Ahrens VERIFY(arc_buf_remove_ref(os->os_phys_buf, &os->os_phys_buf));
735744947dcSTom Erickson
736744947dcSTom Erickson /*
737744947dcSTom Erickson * This is a barrier to prevent the objset from going away in
738744947dcSTom Erickson * dnode_move() until we can safely ensure that the objset is still in
739744947dcSTom Erickson * use. We consider the objset valid before the barrier and invalid
740744947dcSTom Erickson * after the barrier.
741744947dcSTom Erickson */
742744947dcSTom Erickson rw_enter(&os_lock, RW_READER);
743744947dcSTom Erickson rw_exit(&os_lock);
744744947dcSTom Erickson
745503ad85cSMatthew Ahrens mutex_destroy(&os->os_lock);
746503ad85cSMatthew Ahrens mutex_destroy(&os->os_obj_lock);
747503ad85cSMatthew Ahrens mutex_destroy(&os->os_user_ptr_lock);
748bc9014e6SJustin Gibbs spa_evicting_os_deregister(os->os_spa, os);
749503ad85cSMatthew Ahrens kmem_free(os, sizeof (objset_t));
750fa9e4066Sahrens }
751fa9e4066Sahrens
75271eb0538SChris Kirby timestruc_t
dmu_objset_snap_cmtime(objset_t * os)75371eb0538SChris Kirby dmu_objset_snap_cmtime(objset_t *os)
75471eb0538SChris Kirby {
75571eb0538SChris Kirby return (dsl_dir_snap_cmtime(os->os_dsl_dataset->ds_dir));
75671eb0538SChris Kirby }
75771eb0538SChris Kirby
758fa9e4066Sahrens /* called from dsl for meta-objset */
759503ad85cSMatthew 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)760c717a561Smaybee dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
761c717a561Smaybee dmu_objset_type_t type, dmu_tx_t *tx)
762fa9e4066Sahrens {
763503ad85cSMatthew Ahrens objset_t *os;
764fa9e4066Sahrens dnode_t *mdn;
765fa9e4066Sahrens
766fa9e4066Sahrens ASSERT(dmu_tx_is_syncing(tx));
7673b2aab18SMatthew Ahrens
768feaa74e4SMark Maybee if (ds != NULL)
7693b2aab18SMatthew Ahrens VERIFY0(dmu_objset_from_ds(ds, &os));
770feaa74e4SMark Maybee else
7713b2aab18SMatthew Ahrens VERIFY0(dmu_objset_open_impl(spa, NULL, bp, &os));
772feaa74e4SMark Maybee
773744947dcSTom Erickson mdn = DMU_META_DNODE(os);
774fa9e4066Sahrens
775fa9e4066Sahrens dnode_allocate(mdn, DMU_OT_DNODE, 1 << DNODE_BLOCK_SHIFT,
776fa9e4066Sahrens DN_MAX_INDBLKSHIFT, DMU_OT_NONE, 0, tx);
777fa9e4066Sahrens
778fa9e4066Sahrens /*
779fa9e4066Sahrens * We don't want to have to increase the meta-dnode's nlevels
780fa9e4066Sahrens * later, because then we could do it in quescing context while
781fa9e4066Sahrens * we are also accessing it in open context.
782fa9e4066Sahrens *
783fa9e4066Sahrens * This precaution is not necessary for the MOS (ds == NULL),
784fa9e4066Sahrens * because the MOS is only updated in syncing context.
785fa9e4066Sahrens * This is most fortunate: the MOS is the only objset that
786fa9e4066Sahrens * needs to be synced multiple times as spa_sync() iterates
787fa9e4066Sahrens * to convergence, so minimizing its dn_nlevels matters.
788fa9e4066Sahrens */
789ea8dc4b6Seschrock if (ds != NULL) {
790ea8dc4b6Seschrock int levels = 1;
791ea8dc4b6Seschrock
792ea8dc4b6Seschrock /*
793ea8dc4b6Seschrock * Determine the number of levels necessary for the meta-dnode
794ea8dc4b6Seschrock * to contain DN_MAX_OBJECT dnodes.
795ea8dc4b6Seschrock */
796ea8dc4b6Seschrock while ((uint64_t)mdn->dn_nblkptr << (mdn->dn_datablkshift +
797ea8dc4b6Seschrock (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) <
798ea8dc4b6Seschrock DN_MAX_OBJECT * sizeof (dnode_phys_t))
799ea8dc4b6Seschrock levels++;
800ea8dc4b6Seschrock
801fa9e4066Sahrens mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] =
802ea8dc4b6Seschrock mdn->dn_nlevels = levels;
803ea8dc4b6Seschrock }
804fa9e4066Sahrens
805fa9e4066Sahrens ASSERT(type != DMU_OST_NONE);
806fa9e4066Sahrens ASSERT(type != DMU_OST_ANY);
807fa9e4066Sahrens ASSERT(type < DMU_OST_NUMTYPES);
808503ad85cSMatthew Ahrens os->os_phys->os_type = type;
809503ad85cSMatthew Ahrens if (dmu_objset_userused_enabled(os)) {
810503ad85cSMatthew Ahrens os->os_phys->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
811503ad85cSMatthew Ahrens os->os_flags = os->os_phys->os_flags;
81214843421SMatthew Ahrens }
813fa9e4066Sahrens
814fa9e4066Sahrens dsl_dataset_dirty(ds, tx);
815fa9e4066Sahrens
816503ad85cSMatthew Ahrens return (os);
817fa9e4066Sahrens }
818fa9e4066Sahrens
8193b2aab18SMatthew Ahrens typedef struct dmu_objset_create_arg {
8203b2aab18SMatthew Ahrens const char *doca_name;
8213b2aab18SMatthew Ahrens cred_t *doca_cred;
8223b2aab18SMatthew Ahrens void (*doca_userfunc)(objset_t *os, void *arg,
8233b2aab18SMatthew Ahrens cred_t *cr, dmu_tx_t *tx);
8243b2aab18SMatthew Ahrens void *doca_userarg;
8253b2aab18SMatthew Ahrens dmu_objset_type_t doca_type;
8263b2aab18SMatthew Ahrens uint64_t doca_flags;
8273b2aab18SMatthew Ahrens } dmu_objset_create_arg_t;
828fa9e4066Sahrens
8291d452cf5Sahrens /*ARGSUSED*/
830fa9e4066Sahrens static int
dmu_objset_create_check(void * arg,dmu_tx_t * tx)8313b2aab18SMatthew Ahrens dmu_objset_create_check(void *arg, dmu_tx_t *tx)
832fa9e4066Sahrens {
8333b2aab18SMatthew Ahrens dmu_objset_create_arg_t *doca = arg;
8343b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx);
8353b2aab18SMatthew Ahrens dsl_dir_t *pdd;
8363b2aab18SMatthew Ahrens const char *tail;
8373b2aab18SMatthew Ahrens int error;
8381d452cf5Sahrens
8393b2aab18SMatthew Ahrens if (strchr(doca->doca_name, '@') != NULL)
840be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
8413b2aab18SMatthew Ahrens
842*40a5c998SMatthew Ahrens if (strlen(doca->doca_name) >= ZFS_MAX_DATASET_NAME_LEN)
843*40a5c998SMatthew Ahrens return (SET_ERROR(ENAMETOOLONG));
844*40a5c998SMatthew Ahrens
8453b2aab18SMatthew Ahrens error = dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail);
8463b2aab18SMatthew Ahrens if (error != 0)
8473b2aab18SMatthew Ahrens return (error);
8483b2aab18SMatthew Ahrens if (tail == NULL) {
8493b2aab18SMatthew Ahrens dsl_dir_rele(pdd, FTAG);
850be6fd75aSMatthew Ahrens return (SET_ERROR(EEXIST));
8511d452cf5Sahrens }
852a2afb611SJerry Jelinek error = dsl_fs_ss_limit_check(pdd, 1, ZFS_PROP_FILESYSTEM_LIMIT, NULL,
853a2afb611SJerry Jelinek doca->doca_cred);
8543b2aab18SMatthew Ahrens dsl_dir_rele(pdd, FTAG);
855ecd6cf80Smarks
856a2afb611SJerry Jelinek return (error);
8571d452cf5Sahrens }
8581d452cf5Sahrens
8591d452cf5Sahrens static void
dmu_objset_create_sync(void * arg,dmu_tx_t * tx)8603b2aab18SMatthew Ahrens dmu_objset_create_sync(void *arg, dmu_tx_t *tx)
8611d452cf5Sahrens {
8623b2aab18SMatthew Ahrens dmu_objset_create_arg_t *doca = arg;
8633b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx);
8643b2aab18SMatthew Ahrens dsl_dir_t *pdd;
8653b2aab18SMatthew Ahrens const char *tail;
8664445fffbSMatthew Ahrens dsl_dataset_t *ds;
8673b2aab18SMatthew Ahrens uint64_t obj;
8684445fffbSMatthew Ahrens blkptr_t *bp;
8693b2aab18SMatthew Ahrens objset_t *os;
870fa9e4066Sahrens
8713b2aab18SMatthew Ahrens VERIFY0(dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail));
872fa9e4066Sahrens
8733b2aab18SMatthew Ahrens obj = dsl_dataset_create_sync(pdd, tail, NULL, doca->doca_flags,
8743b2aab18SMatthew Ahrens doca->doca_cred, tx);
875fa9e4066Sahrens
8763b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(pdd->dd_pool, obj, FTAG, &ds));
877ae46e4c7SMatthew Ahrens bp = dsl_dataset_get_blkptr(ds);
8783b2aab18SMatthew Ahrens os = dmu_objset_create_impl(pdd->dd_pool->dp_spa,
8793b2aab18SMatthew Ahrens ds, bp, doca->doca_type, tx);
880fa9e4066Sahrens
8813b2aab18SMatthew Ahrens if (doca->doca_userfunc != NULL) {
8823b2aab18SMatthew Ahrens doca->doca_userfunc(os, doca->doca_userarg,
8833b2aab18SMatthew Ahrens doca->doca_cred, tx);
884feaa74e4SMark Maybee }
885ecd6cf80Smarks
8864445fffbSMatthew Ahrens spa_history_log_internal_ds(ds, "create", tx, "");
8874445fffbSMatthew Ahrens dsl_dataset_rele(ds, FTAG);
8883b2aab18SMatthew Ahrens dsl_dir_rele(pdd, FTAG);
889fa9e4066Sahrens }
890fa9e4066Sahrens
891fa9e4066Sahrens 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)892ae46e4c7SMatthew Ahrens dmu_objset_create(const char *name, dmu_objset_type_t type, uint64_t flags,
893ecd6cf80Smarks void (*func)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx), void *arg)
894fa9e4066Sahrens {
8953b2aab18SMatthew Ahrens dmu_objset_create_arg_t doca;
896fa9e4066Sahrens
8973b2aab18SMatthew Ahrens doca.doca_name = name;
8983b2aab18SMatthew Ahrens doca.doca_cred = CRED();
8993b2aab18SMatthew Ahrens doca.doca_flags = flags;
9003b2aab18SMatthew Ahrens doca.doca_userfunc = func;
9013b2aab18SMatthew Ahrens doca.doca_userarg = arg;
9023b2aab18SMatthew Ahrens doca.doca_type = type;
9033b2aab18SMatthew Ahrens
9043b2aab18SMatthew Ahrens return (dsl_sync_task(name,
9057d46dc6cSMatthew Ahrens dmu_objset_create_check, dmu_objset_create_sync, &doca,
9067d46dc6cSMatthew Ahrens 5, ZFS_SPACE_CHECK_NORMAL));
907fa9e4066Sahrens }
908fa9e4066Sahrens
9093b2aab18SMatthew Ahrens typedef struct dmu_objset_clone_arg {
9103b2aab18SMatthew Ahrens const char *doca_clone;
9113b2aab18SMatthew Ahrens const char *doca_origin;
9123b2aab18SMatthew Ahrens cred_t *doca_cred;
9133b2aab18SMatthew Ahrens } dmu_objset_clone_arg_t;
914ecd6cf80Smarks
9153b2aab18SMatthew Ahrens /*ARGSUSED*/
916ea2f5b9eSMatthew Ahrens static int
dmu_objset_clone_check(void * arg,dmu_tx_t * tx)9173b2aab18SMatthew Ahrens dmu_objset_clone_check(void *arg, dmu_tx_t *tx)
918ea2f5b9eSMatthew Ahrens {
9193b2aab18SMatthew Ahrens dmu_objset_clone_arg_t *doca = arg;
9203b2aab18SMatthew Ahrens dsl_dir_t *pdd;
9213b2aab18SMatthew Ahrens const char *tail;
92299d5e173STim Haley int error;
9233b2aab18SMatthew Ahrens dsl_dataset_t *origin;
9243b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx);
925ea2f5b9eSMatthew Ahrens
9263b2aab18SMatthew Ahrens if (strchr(doca->doca_clone, '@') != NULL)
927be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
928ea2f5b9eSMatthew Ahrens
929*40a5c998SMatthew Ahrens if (strlen(doca->doca_clone) >= ZFS_MAX_DATASET_NAME_LEN)
930*40a5c998SMatthew Ahrens return (SET_ERROR(ENAMETOOLONG));
931*40a5c998SMatthew Ahrens
9323b2aab18SMatthew Ahrens error = dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail);
9333b2aab18SMatthew Ahrens if (error != 0)
93499d5e173STim Haley return (error);
9353b2aab18SMatthew Ahrens if (tail == NULL) {
9363b2aab18SMatthew Ahrens dsl_dir_rele(pdd, FTAG);
937be6fd75aSMatthew Ahrens return (SET_ERROR(EEXIST));
93899d5e173STim Haley }
93903b1c297SAlexander Eremin
940a2afb611SJerry Jelinek error = dsl_fs_ss_limit_check(pdd, 1, ZFS_PROP_FILESYSTEM_LIMIT, NULL,
941a2afb611SJerry Jelinek doca->doca_cred);
942a2afb611SJerry Jelinek if (error != 0) {
943a2afb611SJerry Jelinek dsl_dir_rele(pdd, FTAG);
944a2afb611SJerry Jelinek return (SET_ERROR(EDQUOT));
945a2afb611SJerry Jelinek }
9463b2aab18SMatthew Ahrens dsl_dir_rele(pdd, FTAG);
9473b2aab18SMatthew Ahrens
9483b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin);
9493b2aab18SMatthew Ahrens if (error != 0)
95099d5e173STim Haley return (error);
9513b2aab18SMatthew Ahrens
9523b2aab18SMatthew Ahrens /* You can only clone snapshots, not the head datasets. */
953bc9014e6SJustin Gibbs if (!origin->ds_is_snapshot) {
9543b2aab18SMatthew Ahrens dsl_dataset_rele(origin, FTAG);
955be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
9563b2aab18SMatthew Ahrens }
9573b2aab18SMatthew Ahrens dsl_dataset_rele(origin, FTAG);
9583b2aab18SMatthew Ahrens
9593b2aab18SMatthew Ahrens return (0);
960ea2f5b9eSMatthew Ahrens }
961ea2f5b9eSMatthew Ahrens
962ea2f5b9eSMatthew Ahrens static void
dmu_objset_clone_sync(void * arg,dmu_tx_t * tx)9633b2aab18SMatthew Ahrens dmu_objset_clone_sync(void *arg, dmu_tx_t *tx)
964ea2f5b9eSMatthew Ahrens {
9653b2aab18SMatthew Ahrens dmu_objset_clone_arg_t *doca = arg;
9663b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx);
9673b2aab18SMatthew Ahrens dsl_dir_t *pdd;
9683b2aab18SMatthew Ahrens const char *tail;
9693b2aab18SMatthew Ahrens dsl_dataset_t *origin, *ds;
9703b2aab18SMatthew Ahrens uint64_t obj;
971*40a5c998SMatthew Ahrens char namebuf[ZFS_MAX_DATASET_NAME_LEN];
972ea2f5b9eSMatthew Ahrens
9733b2aab18SMatthew Ahrens VERIFY0(dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail));
9743b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin));
975ea2f5b9eSMatthew Ahrens
9763b2aab18SMatthew Ahrens obj = dsl_dataset_create_sync(pdd, tail, origin, 0,
9773b2aab18SMatthew Ahrens doca->doca_cred, tx);
9783b2aab18SMatthew Ahrens
9793b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(pdd->dd_pool, obj, FTAG, &ds));
9803b2aab18SMatthew Ahrens dsl_dataset_name(origin, namebuf);
9813b2aab18SMatthew Ahrens spa_history_log_internal_ds(ds, "clone", tx,
9823b2aab18SMatthew Ahrens "origin=%s (%llu)", namebuf, origin->ds_object);
9833b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG);
9843b2aab18SMatthew Ahrens dsl_dataset_rele(origin, FTAG);
9853b2aab18SMatthew Ahrens dsl_dir_rele(pdd, FTAG);
98692241e0bSTom Erickson }
98799d5e173STim Haley
9881d452cf5Sahrens int
dmu_objset_clone(const char * clone,const char * origin)9893b2aab18SMatthew Ahrens dmu_objset_clone(const char *clone, const char *origin)
9901d452cf5Sahrens {
9913b2aab18SMatthew Ahrens dmu_objset_clone_arg_t doca;
9924445fffbSMatthew Ahrens
9933b2aab18SMatthew Ahrens doca.doca_clone = clone;
9943b2aab18SMatthew Ahrens doca.doca_origin = origin;
9953b2aab18SMatthew Ahrens doca.doca_cred = CRED();
9964445fffbSMatthew Ahrens
9973b2aab18SMatthew Ahrens return (dsl_sync_task(clone,
9987d46dc6cSMatthew Ahrens dmu_objset_clone_check, dmu_objset_clone_sync, &doca,
9997d46dc6cSMatthew Ahrens 5, ZFS_SPACE_CHECK_NORMAL));
10004445fffbSMatthew Ahrens }
10014445fffbSMatthew Ahrens
10024445fffbSMatthew Ahrens int
dmu_objset_snapshot_one(const char * fsname,const char * snapname)10034445fffbSMatthew Ahrens dmu_objset_snapshot_one(const char *fsname, const char *snapname)
10044445fffbSMatthew Ahrens {
10054445fffbSMatthew Ahrens int err;
10064445fffbSMatthew Ahrens char *longsnap = kmem_asprintf("%s@%s", fsname, snapname);
10074445fffbSMatthew Ahrens nvlist_t *snaps = fnvlist_alloc();
10084445fffbSMatthew Ahrens
10094445fffbSMatthew Ahrens fnvlist_add_boolean(snaps, longsnap);
10104445fffbSMatthew Ahrens strfree(longsnap);
10113b2aab18SMatthew Ahrens err = dsl_dataset_snapshot(snaps, NULL, NULL);
10123b2aab18SMatthew Ahrens fnvlist_free(snaps);
10134445fffbSMatthew Ahrens return (err);
10144445fffbSMatthew Ahrens }
10154445fffbSMatthew Ahrens
1016fa9e4066Sahrens static void
dmu_objset_sync_dnodes(list_t * list,list_t * newlist,dmu_tx_t * tx)101714843421SMatthew Ahrens dmu_objset_sync_dnodes(list_t *list, list_t *newlist, dmu_tx_t *tx)
1018fa9e4066Sahrens {
1019c717a561Smaybee dnode_t *dn;
1020fa9e4066Sahrens
1021c717a561Smaybee while (dn = list_head(list)) {
1022c717a561Smaybee ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
1023c717a561Smaybee ASSERT(dn->dn_dbuf->db_data_pending);
1024fa9e4066Sahrens /*
102514843421SMatthew Ahrens * Initialize dn_zio outside dnode_sync() because the
102614843421SMatthew Ahrens * meta-dnode needs to set it ouside dnode_sync().
1027fa9e4066Sahrens */
1028c717a561Smaybee dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio;
1029c717a561Smaybee ASSERT(dn->dn_zio);
1030c717a561Smaybee
1031c717a561Smaybee ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS);
1032c717a561Smaybee list_remove(list, dn);
103314843421SMatthew Ahrens
103414843421SMatthew Ahrens if (newlist) {
103514843421SMatthew Ahrens (void) dnode_add_ref(dn, newlist);
103614843421SMatthew Ahrens list_insert_tail(newlist, dn);
103714843421SMatthew Ahrens }
103814843421SMatthew Ahrens
1039c717a561Smaybee dnode_sync(dn, tx);
1040fa9e4066Sahrens }
1041fa9e4066Sahrens }
1042faafa6e3Sahrens
1043c717a561Smaybee /* ARGSUSED */
1044c717a561Smaybee static void
dmu_objset_write_ready(zio_t * zio,arc_buf_t * abuf,void * arg)1045b24ab676SJeff Bonwick dmu_objset_write_ready(zio_t *zio, arc_buf_t *abuf, void *arg)
1046c717a561Smaybee {
1047e14bb325SJeff Bonwick blkptr_t *bp = zio->io_bp;
1048503ad85cSMatthew Ahrens objset_t *os = arg;
1049c717a561Smaybee dnode_phys_t *dnp = &os->os_phys->os_meta_dnode;
1050faafa6e3Sahrens
10515d7b4d43SMatthew Ahrens ASSERT(!BP_IS_EMBEDDED(bp));
10523b2aab18SMatthew Ahrens ASSERT3P(bp, ==, os->os_rootbp);
10533b2aab18SMatthew Ahrens ASSERT3U(BP_GET_TYPE(bp), ==, DMU_OT_OBJSET);
10543b2aab18SMatthew Ahrens ASSERT0(BP_GET_LEVEL(bp));
10550a4e9518Sgw25295
1056c717a561Smaybee /*
105714843421SMatthew Ahrens * Update rootbp fill count: it should be the number of objects
105814843421SMatthew Ahrens * allocated in the object set (not counting the "special"
105914843421SMatthew Ahrens * objects that are stored in the objset_phys_t -- the meta
106014843421SMatthew Ahrens * dnode and user/group accounting objects).
1061c717a561Smaybee */
106214843421SMatthew Ahrens bp->blk_fill = 0;
1063e14bb325SJeff Bonwick for (int i = 0; i < dnp->dn_nblkptr; i++)
10645d7b4d43SMatthew Ahrens bp->blk_fill += BP_GET_FILL(&dnp->dn_blkptr[i]);
1065b24ab676SJeff Bonwick }
1066b24ab676SJeff Bonwick
1067b24ab676SJeff Bonwick /* ARGSUSED */
1068b24ab676SJeff Bonwick static void
dmu_objset_write_done(zio_t * zio,arc_buf_t * abuf,void * arg)1069b24ab676SJeff Bonwick dmu_objset_write_done(zio_t *zio, arc_buf_t *abuf, void *arg)
1070b24ab676SJeff Bonwick {
1071b24ab676SJeff Bonwick blkptr_t *bp = zio->io_bp;
1072b24ab676SJeff Bonwick blkptr_t *bp_orig = &zio->io_bp_orig;
1073b24ab676SJeff Bonwick objset_t *os = arg;
10740a4e9518Sgw25295
1075e14bb325SJeff Bonwick if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
1076b24ab676SJeff Bonwick ASSERT(BP_EQUAL(bp, bp_orig));
1077e14bb325SJeff Bonwick } else {
1078b24ab676SJeff Bonwick dsl_dataset_t *ds = os->os_dsl_dataset;
1079b24ab676SJeff Bonwick dmu_tx_t *tx = os->os_synctx;
1080b24ab676SJeff Bonwick
1081b24ab676SJeff Bonwick (void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
1082b24ab676SJeff Bonwick dsl_dataset_block_born(ds, bp, tx);
10830a4e9518Sgw25295 }
1084fa9e4066Sahrens }
1085fa9e4066Sahrens
1086fa9e4066Sahrens /* called from dsl */
1087fa9e4066Sahrens void
dmu_objset_sync(objset_t * os,zio_t * pio,dmu_tx_t * tx)1088503ad85cSMatthew Ahrens dmu_objset_sync(objset_t *os, zio_t *pio, dmu_tx_t *tx)
1089fa9e4066Sahrens {
1090fa9e4066Sahrens int txgoff;
10917802d7bfSMatthew Ahrens zbookmark_phys_t zb;
1092b24ab676SJeff Bonwick zio_prop_t zp;
1093c717a561Smaybee zio_t *zio;
1094c717a561Smaybee list_t *list;
109514843421SMatthew Ahrens list_t *newlist = NULL;
1096c717a561Smaybee dbuf_dirty_record_t *dr;
1097fa9e4066Sahrens
1098fa9e4066Sahrens dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg);
1099fa9e4066Sahrens
1100c717a561Smaybee ASSERT(dmu_tx_is_syncing(tx));
1101c717a561Smaybee /* XXX the write_done callback should really give us the tx... */
1102c717a561Smaybee os->os_synctx = tx;
1103fa9e4066Sahrens
110487bd5c1eSahrens if (os->os_dsl_dataset == NULL) {
110587bd5c1eSahrens /*
110687bd5c1eSahrens * This is the MOS. If we have upgraded,
110787bd5c1eSahrens * spa_max_replication() could change, so reset
110887bd5c1eSahrens * os_copies here.
110987bd5c1eSahrens */
111087bd5c1eSahrens os->os_copies = spa_max_replication(os->os_spa);
111187bd5c1eSahrens }
111287bd5c1eSahrens
1113fa9e4066Sahrens /*
1114c717a561Smaybee * Create the root block IO
1115fa9e4066Sahrens */
1116b24ab676SJeff Bonwick SET_BOOKMARK(&zb, os->os_dsl_dataset ?
1117b24ab676SJeff Bonwick os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
1118b24ab676SJeff Bonwick ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
11191b912ec7SGeorge Wilson arc_release(os->os_phys_buf, &os->os_phys_buf);
1120b24ab676SJeff Bonwick
1121b24ab676SJeff Bonwick dmu_write_policy(os, NULL, 0, 0, &zp);
1122b24ab676SJeff Bonwick
1123b24ab676SJeff Bonwick zio = arc_write(pio, os->os_spa, tx->tx_txg,
1124aad02571SSaso Kiselkov os->os_rootbp, os->os_phys_buf, DMU_OS_IS_L2CACHEABLE(os),
1125aad02571SSaso Kiselkov DMU_OS_IS_L2COMPRESSIBLE(os), &zp, dmu_objset_write_ready,
112669962b56SMatthew Ahrens NULL, dmu_objset_write_done, os, ZIO_PRIORITY_ASYNC_WRITE,
1127aad02571SSaso Kiselkov ZIO_FLAG_MUSTSUCCEED, &zb);
1128fa9e4066Sahrens
1129c717a561Smaybee /*
113014843421SMatthew Ahrens * Sync special dnodes - the parent IO for the sync is the root block
1131c717a561Smaybee */
1132744947dcSTom Erickson DMU_META_DNODE(os)->dn_zio = zio;
1133744947dcSTom Erickson dnode_sync(DMU_META_DNODE(os), tx);
1134fa9e4066Sahrens
113514843421SMatthew Ahrens os->os_phys->os_flags = os->os_flags;
113614843421SMatthew Ahrens
1137744947dcSTom Erickson if (DMU_USERUSED_DNODE(os) &&
1138744947dcSTom Erickson DMU_USERUSED_DNODE(os)->dn_type != DMU_OT_NONE) {
1139744947dcSTom Erickson DMU_USERUSED_DNODE(os)->dn_zio = zio;
1140744947dcSTom Erickson dnode_sync(DMU_USERUSED_DNODE(os), tx);
1141744947dcSTom Erickson DMU_GROUPUSED_DNODE(os)->dn_zio = zio;
1142744947dcSTom Erickson dnode_sync(DMU_GROUPUSED_DNODE(os), tx);
114314843421SMatthew Ahrens }
114414843421SMatthew Ahrens
1145c717a561Smaybee txgoff = tx->tx_txg & TXG_MASK;
1146c717a561Smaybee
114714843421SMatthew Ahrens if (dmu_objset_userused_enabled(os)) {
114814843421SMatthew Ahrens newlist = &os->os_synced_dnodes;
114914843421SMatthew Ahrens /*
115014843421SMatthew Ahrens * We must create the list here because it uses the
115114843421SMatthew Ahrens * dn_dirty_link[] of this txg.
115214843421SMatthew Ahrens */
115314843421SMatthew Ahrens list_create(newlist, sizeof (dnode_t),
115414843421SMatthew Ahrens offsetof(dnode_t, dn_dirty_link[txgoff]));
115514843421SMatthew Ahrens }
115614843421SMatthew Ahrens
115714843421SMatthew Ahrens dmu_objset_sync_dnodes(&os->os_free_dnodes[txgoff], newlist, tx);
115814843421SMatthew Ahrens dmu_objset_sync_dnodes(&os->os_dirty_dnodes[txgoff], newlist, tx);
1159c717a561Smaybee
1160744947dcSTom Erickson list = &DMU_META_DNODE(os)->dn_dirty_records[txgoff];
1161c717a561Smaybee while (dr = list_head(list)) {
11623b2aab18SMatthew Ahrens ASSERT0(dr->dr_dbuf->db_level);
1163c717a561Smaybee list_remove(list, dr);
1164c717a561Smaybee if (dr->dr_zio)
1165c717a561Smaybee zio_nowait(dr->dr_zio);
1166c717a561Smaybee }
1167c717a561Smaybee /*
1168c717a561Smaybee * Free intent log blocks up to this tx.
1169c717a561Smaybee */
1170c717a561Smaybee zil_sync(os->os_zil, tx);
1171088f3894Sahrens os->os_phys->os_zil_header = os->os_zil_header;
1172c717a561Smaybee zio_nowait(zio);
1173fa9e4066Sahrens }
1174fa9e4066Sahrens
1175b24ab676SJeff Bonwick boolean_t
dmu_objset_is_dirty(objset_t * os,uint64_t txg)1176b24ab676SJeff Bonwick dmu_objset_is_dirty(objset_t *os, uint64_t txg)
1177b24ab676SJeff Bonwick {
1178b24ab676SJeff Bonwick return (!list_is_empty(&os->os_dirty_dnodes[txg & TXG_MASK]) ||
1179b24ab676SJeff Bonwick !list_is_empty(&os->os_free_dnodes[txg & TXG_MASK]));
1180b24ab676SJeff Bonwick }
1181b24ab676SJeff Bonwick
1182744947dcSTom Erickson static objset_used_cb_t *used_cbs[DMU_OST_NUMTYPES];
118314843421SMatthew Ahrens
118414843421SMatthew Ahrens void
dmu_objset_register_type(dmu_objset_type_t ost,objset_used_cb_t * cb)118514843421SMatthew Ahrens dmu_objset_register_type(dmu_objset_type_t ost, objset_used_cb_t *cb)
118614843421SMatthew Ahrens {
118714843421SMatthew Ahrens used_cbs[ost] = cb;
118814843421SMatthew Ahrens }
118914843421SMatthew Ahrens
119014843421SMatthew Ahrens boolean_t
dmu_objset_userused_enabled(objset_t * os)1191503ad85cSMatthew Ahrens dmu_objset_userused_enabled(objset_t *os)
119214843421SMatthew Ahrens {
119314843421SMatthew Ahrens return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE &&
1194744947dcSTom Erickson used_cbs[os->os_phys->os_type] != NULL &&
1195744947dcSTom Erickson DMU_USERUSED_DNODE(os) != NULL);
119614843421SMatthew Ahrens }
119714843421SMatthew Ahrens
11989966ca11SMatthew 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)11990a586ceaSMark Shellenbaum do_userquota_update(objset_t *os, uint64_t used, uint64_t flags,
12000a586ceaSMark Shellenbaum uint64_t user, uint64_t group, boolean_t subtract, dmu_tx_t *tx)
12019966ca11SMatthew Ahrens {
12020a586ceaSMark Shellenbaum if ((flags & DNODE_FLAG_USERUSED_ACCOUNTED)) {
12030a586ceaSMark Shellenbaum int64_t delta = DNODE_SIZE + used;
12049966ca11SMatthew Ahrens if (subtract)
12059966ca11SMatthew Ahrens delta = -delta;
1206b420f3adSRichard Lowe VERIFY3U(0, ==, zap_increment_int(os, DMU_USERUSED_OBJECT,
12079966ca11SMatthew Ahrens user, delta, tx));
1208b420f3adSRichard Lowe VERIFY3U(0, ==, zap_increment_int(os, DMU_GROUPUSED_OBJECT,
12099966ca11SMatthew Ahrens group, delta, tx));
12109966ca11SMatthew Ahrens }
12119966ca11SMatthew Ahrens }
12129966ca11SMatthew Ahrens
121314843421SMatthew Ahrens void
dmu_objset_do_userquota_updates(objset_t * os,dmu_tx_t * tx)12140a586ceaSMark Shellenbaum dmu_objset_do_userquota_updates(objset_t *os, dmu_tx_t *tx)
121514843421SMatthew Ahrens {
121614843421SMatthew Ahrens dnode_t *dn;
121714843421SMatthew Ahrens list_t *list = &os->os_synced_dnodes;
121814843421SMatthew Ahrens
121914843421SMatthew Ahrens ASSERT(list_head(list) == NULL || dmu_objset_userused_enabled(os));
122014843421SMatthew Ahrens
122114843421SMatthew Ahrens while (dn = list_head(list)) {
12221d8ccc7bSMark Shellenbaum int flags;
122314843421SMatthew Ahrens ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object));
122414843421SMatthew Ahrens ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE ||
122514843421SMatthew Ahrens dn->dn_phys->dn_flags &
122614843421SMatthew Ahrens DNODE_FLAG_USERUSED_ACCOUNTED);
122714843421SMatthew Ahrens
122814843421SMatthew Ahrens /* Allocate the user/groupused objects if necessary. */
1229744947dcSTom Erickson if (DMU_USERUSED_DNODE(os)->dn_type == DMU_OT_NONE) {
1230503ad85cSMatthew Ahrens VERIFY(0 == zap_create_claim(os,
123114843421SMatthew Ahrens DMU_USERUSED_OBJECT,
123214843421SMatthew Ahrens DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
1233503ad85cSMatthew Ahrens VERIFY(0 == zap_create_claim(os,
123414843421SMatthew Ahrens DMU_GROUPUSED_OBJECT,
123514843421SMatthew Ahrens DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
123614843421SMatthew Ahrens }
123714843421SMatthew Ahrens
123814843421SMatthew Ahrens /*
12399966ca11SMatthew Ahrens * We intentionally modify the zap object even if the
12400a586ceaSMark Shellenbaum * net delta is zero. Otherwise
12419966ca11SMatthew Ahrens * the block of the zap obj could be shared between
12429966ca11SMatthew Ahrens * datasets but need to be different between them after
12439966ca11SMatthew Ahrens * a bprewrite.
124414843421SMatthew Ahrens */
124514843421SMatthew Ahrens
12461d8ccc7bSMark Shellenbaum flags = dn->dn_id_flags;
12471d8ccc7bSMark Shellenbaum ASSERT(flags);
12481d8ccc7bSMark Shellenbaum if (flags & DN_ID_OLD_EXIST) {
12490a586ceaSMark Shellenbaum do_userquota_update(os, dn->dn_oldused, dn->dn_oldflags,
12500a586ceaSMark Shellenbaum dn->dn_olduid, dn->dn_oldgid, B_TRUE, tx);
12510a586ceaSMark Shellenbaum }
12521d8ccc7bSMark Shellenbaum if (flags & DN_ID_NEW_EXIST) {
12530a586ceaSMark Shellenbaum do_userquota_update(os, DN_USED_BYTES(dn->dn_phys),
12540a586ceaSMark Shellenbaum dn->dn_phys->dn_flags, dn->dn_newuid,
12550a586ceaSMark Shellenbaum dn->dn_newgid, B_FALSE, tx);
12560a586ceaSMark Shellenbaum }
12570a586ceaSMark Shellenbaum
12581d8ccc7bSMark Shellenbaum mutex_enter(&dn->dn_mtx);
12590a586ceaSMark Shellenbaum dn->dn_oldused = 0;
12600a586ceaSMark Shellenbaum dn->dn_oldflags = 0;
12610a586ceaSMark Shellenbaum if (dn->dn_id_flags & DN_ID_NEW_EXIST) {
12620a586ceaSMark Shellenbaum dn->dn_olduid = dn->dn_newuid;
12630a586ceaSMark Shellenbaum dn->dn_oldgid = dn->dn_newgid;
12640a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_OLD_EXIST;
12650a586ceaSMark Shellenbaum if (dn->dn_bonuslen == 0)
12660a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_CHKED_SPILL;
12670a586ceaSMark Shellenbaum else
12680a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_CHKED_BONUS;
12690a586ceaSMark Shellenbaum }
127028d97a71SMark Shellenbaum dn->dn_id_flags &= ~(DN_ID_NEW_EXIST);
127114843421SMatthew Ahrens mutex_exit(&dn->dn_mtx);
127214843421SMatthew Ahrens
127314843421SMatthew Ahrens list_remove(list, dn);
127414843421SMatthew Ahrens dnode_rele(dn, list);
127514843421SMatthew Ahrens }
127614843421SMatthew Ahrens }
127714843421SMatthew Ahrens
127806e0070dSMark Shellenbaum /*
127906e0070dSMark Shellenbaum * Returns a pointer to data to find uid/gid from
128006e0070dSMark Shellenbaum *
128106e0070dSMark Shellenbaum * If a dirty record for transaction group that is syncing can't
128206e0070dSMark Shellenbaum * be found then NULL is returned. In the NULL case it is assumed
128306e0070dSMark Shellenbaum * the uid/gid aren't changing.
128406e0070dSMark Shellenbaum */
128506e0070dSMark Shellenbaum static void *
dmu_objset_userquota_find_data(dmu_buf_impl_t * db,dmu_tx_t * tx)128606e0070dSMark Shellenbaum dmu_objset_userquota_find_data(dmu_buf_impl_t *db, dmu_tx_t *tx)
128706e0070dSMark Shellenbaum {
128806e0070dSMark Shellenbaum dbuf_dirty_record_t *dr, **drp;
128906e0070dSMark Shellenbaum void *data;
129006e0070dSMark Shellenbaum
129106e0070dSMark Shellenbaum if (db->db_dirtycnt == 0)
129206e0070dSMark Shellenbaum return (db->db.db_data); /* Nothing is changing */
129306e0070dSMark Shellenbaum
129406e0070dSMark Shellenbaum for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next)
129506e0070dSMark Shellenbaum if (dr->dr_txg == tx->tx_txg)
129606e0070dSMark Shellenbaum break;
129706e0070dSMark Shellenbaum
1298744947dcSTom Erickson if (dr == NULL) {
129906e0070dSMark Shellenbaum data = NULL;
1300744947dcSTom Erickson } else {
1301744947dcSTom Erickson dnode_t *dn;
1302744947dcSTom Erickson
1303744947dcSTom Erickson DB_DNODE_ENTER(dr->dr_dbuf);
1304744947dcSTom Erickson dn = DB_DNODE(dr->dr_dbuf);
1305744947dcSTom Erickson
1306744947dcSTom Erickson if (dn->dn_bonuslen == 0 &&
130706e0070dSMark Shellenbaum dr->dr_dbuf->db_blkid == DMU_SPILL_BLKID)
130806e0070dSMark Shellenbaum data = dr->dt.dl.dr_data->b_data;
130906e0070dSMark Shellenbaum else
131006e0070dSMark Shellenbaum data = dr->dt.dl.dr_data;
1311744947dcSTom Erickson
1312744947dcSTom Erickson DB_DNODE_EXIT(dr->dr_dbuf);
1313744947dcSTom Erickson }
1314744947dcSTom Erickson
131506e0070dSMark Shellenbaum return (data);
131606e0070dSMark Shellenbaum }
131706e0070dSMark Shellenbaum
13180a586ceaSMark Shellenbaum void
dmu_objset_userquota_get_ids(dnode_t * dn,boolean_t before,dmu_tx_t * tx)131906e0070dSMark Shellenbaum dmu_objset_userquota_get_ids(dnode_t *dn, boolean_t before, dmu_tx_t *tx)
13200a586ceaSMark Shellenbaum {
13210a586ceaSMark Shellenbaum objset_t *os = dn->dn_objset;
13220a586ceaSMark Shellenbaum void *data = NULL;
132306e0070dSMark Shellenbaum dmu_buf_impl_t *db = NULL;
1324d5285caeSGeorge Wilson uint64_t *user = NULL;
1325d5285caeSGeorge Wilson uint64_t *group = NULL;
13260a586ceaSMark Shellenbaum int flags = dn->dn_id_flags;
13270a586ceaSMark Shellenbaum int error;
132806e0070dSMark Shellenbaum boolean_t have_spill = B_FALSE;
13290a586ceaSMark Shellenbaum
13300a586ceaSMark Shellenbaum if (!dmu_objset_userused_enabled(dn->dn_objset))
13310a586ceaSMark Shellenbaum return;
13320a586ceaSMark Shellenbaum
13330a586ceaSMark Shellenbaum if (before && (flags & (DN_ID_CHKED_BONUS|DN_ID_OLD_EXIST|
13340a586ceaSMark Shellenbaum DN_ID_CHKED_SPILL)))
13350a586ceaSMark Shellenbaum return;
13360a586ceaSMark Shellenbaum
13370a586ceaSMark Shellenbaum if (before && dn->dn_bonuslen != 0)
13380a586ceaSMark Shellenbaum data = DN_BONUS(dn->dn_phys);
133906e0070dSMark Shellenbaum else if (!before && dn->dn_bonuslen != 0) {
134006e0070dSMark Shellenbaum if (dn->dn_bonus) {
134106e0070dSMark Shellenbaum db = dn->dn_bonus;
134206e0070dSMark Shellenbaum mutex_enter(&db->db_mtx);
134306e0070dSMark Shellenbaum data = dmu_objset_userquota_find_data(db, tx);
134406e0070dSMark Shellenbaum } else {
134506e0070dSMark Shellenbaum data = DN_BONUS(dn->dn_phys);
134606e0070dSMark Shellenbaum }
134706e0070dSMark Shellenbaum } else if (dn->dn_bonuslen == 0 && dn->dn_bonustype == DMU_OT_SA) {
13480a586ceaSMark Shellenbaum int rf = 0;
13490a586ceaSMark Shellenbaum
13500a586ceaSMark Shellenbaum if (RW_WRITE_HELD(&dn->dn_struct_rwlock))
13510a586ceaSMark Shellenbaum rf |= DB_RF_HAVESTRUCT;
13521d8ccc7bSMark Shellenbaum error = dmu_spill_hold_by_dnode(dn,
13531d8ccc7bSMark Shellenbaum rf | DB_RF_MUST_SUCCEED,
135406e0070dSMark Shellenbaum FTAG, (dmu_buf_t **)&db);
13550a586ceaSMark Shellenbaum ASSERT(error == 0);
135606e0070dSMark Shellenbaum mutex_enter(&db->db_mtx);
135706e0070dSMark Shellenbaum data = (before) ? db->db.db_data :
135806e0070dSMark Shellenbaum dmu_objset_userquota_find_data(db, tx);
135906e0070dSMark Shellenbaum have_spill = B_TRUE;
13600a586ceaSMark Shellenbaum } else {
13610a586ceaSMark Shellenbaum mutex_enter(&dn->dn_mtx);
13620a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_CHKED_BONUS;
13630a586ceaSMark Shellenbaum mutex_exit(&dn->dn_mtx);
13640a586ceaSMark Shellenbaum return;
13650a586ceaSMark Shellenbaum }
13660a586ceaSMark Shellenbaum
13670a586ceaSMark Shellenbaum if (before) {
136806e0070dSMark Shellenbaum ASSERT(data);
13690a586ceaSMark Shellenbaum user = &dn->dn_olduid;
13700a586ceaSMark Shellenbaum group = &dn->dn_oldgid;
137106e0070dSMark Shellenbaum } else if (data) {
13720a586ceaSMark Shellenbaum user = &dn->dn_newuid;
13730a586ceaSMark Shellenbaum group = &dn->dn_newgid;
13740a586ceaSMark Shellenbaum }
13750a586ceaSMark Shellenbaum
137606e0070dSMark Shellenbaum /*
137706e0070dSMark Shellenbaum * Must always call the callback in case the object
137806e0070dSMark Shellenbaum * type has changed and that type isn't an object type to track
137906e0070dSMark Shellenbaum */
13800a586ceaSMark Shellenbaum error = used_cbs[os->os_phys->os_type](dn->dn_bonustype, data,
13810a586ceaSMark Shellenbaum user, group);
13820a586ceaSMark Shellenbaum
138306e0070dSMark Shellenbaum /*
138406e0070dSMark Shellenbaum * Preserve existing uid/gid when the callback can't determine
138506e0070dSMark Shellenbaum * what the new uid/gid are and the callback returned EEXIST.
138606e0070dSMark Shellenbaum * The EEXIST error tells us to just use the existing uid/gid.
138706e0070dSMark Shellenbaum * If we don't know what the old values are then just assign
138806e0070dSMark Shellenbaum * them to 0, since that is a new file being created.
138906e0070dSMark Shellenbaum */
139006e0070dSMark Shellenbaum if (!before && data == NULL && error == EEXIST) {
139106e0070dSMark Shellenbaum if (flags & DN_ID_OLD_EXIST) {
139206e0070dSMark Shellenbaum dn->dn_newuid = dn->dn_olduid;
139306e0070dSMark Shellenbaum dn->dn_newgid = dn->dn_oldgid;
139406e0070dSMark Shellenbaum } else {
139506e0070dSMark Shellenbaum dn->dn_newuid = 0;
139606e0070dSMark Shellenbaum dn->dn_newgid = 0;
139706e0070dSMark Shellenbaum }
139806e0070dSMark Shellenbaum error = 0;
139906e0070dSMark Shellenbaum }
140006e0070dSMark Shellenbaum
140106e0070dSMark Shellenbaum if (db)
140206e0070dSMark Shellenbaum mutex_exit(&db->db_mtx);
140306e0070dSMark Shellenbaum
14040a586ceaSMark Shellenbaum mutex_enter(&dn->dn_mtx);
14050a586ceaSMark Shellenbaum if (error == 0 && before)
14060a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_OLD_EXIST;
14070a586ceaSMark Shellenbaum if (error == 0 && !before)
14080a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_NEW_EXIST;
14090a586ceaSMark Shellenbaum
141006e0070dSMark Shellenbaum if (have_spill) {
14110a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_CHKED_SPILL;
14120a586ceaSMark Shellenbaum } else {
14130a586ceaSMark Shellenbaum dn->dn_id_flags |= DN_ID_CHKED_BONUS;
14140a586ceaSMark Shellenbaum }
14150a586ceaSMark Shellenbaum mutex_exit(&dn->dn_mtx);
141606e0070dSMark Shellenbaum if (have_spill)
141706e0070dSMark Shellenbaum dmu_buf_rele((dmu_buf_t *)db, FTAG);
14180a586ceaSMark Shellenbaum }
14190a586ceaSMark Shellenbaum
142014843421SMatthew Ahrens boolean_t
dmu_objset_userspace_present(objset_t * os)142114843421SMatthew Ahrens dmu_objset_userspace_present(objset_t *os)
142214843421SMatthew Ahrens {
1423503ad85cSMatthew Ahrens return (os->os_phys->os_flags &
142414843421SMatthew Ahrens OBJSET_FLAG_USERACCOUNTING_COMPLETE);
142514843421SMatthew Ahrens }
142614843421SMatthew Ahrens
142714843421SMatthew Ahrens int
dmu_objset_userspace_upgrade(objset_t * os)142814843421SMatthew Ahrens dmu_objset_userspace_upgrade(objset_t *os)
142914843421SMatthew Ahrens {
143014843421SMatthew Ahrens uint64_t obj;
143114843421SMatthew Ahrens int err = 0;
143214843421SMatthew Ahrens
143314843421SMatthew Ahrens if (dmu_objset_userspace_present(os))
143414843421SMatthew Ahrens return (0);
1435503ad85cSMatthew Ahrens if (!dmu_objset_userused_enabled(os))
1436be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP));
143714843421SMatthew Ahrens if (dmu_objset_is_snapshot(os))
1438be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
143914843421SMatthew Ahrens
144014843421SMatthew Ahrens /*
144114843421SMatthew Ahrens * We simply need to mark every object dirty, so that it will be
144214843421SMatthew Ahrens * synced out and now accounted. If this is called
144314843421SMatthew Ahrens * concurrently, or if we already did some work before crashing,
144414843421SMatthew Ahrens * that's fine, since we track each object's accounted state
144514843421SMatthew Ahrens * independently.
144614843421SMatthew Ahrens */
144714843421SMatthew Ahrens
144814843421SMatthew Ahrens for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
144968857716SLin Ling dmu_tx_t *tx;
145014843421SMatthew Ahrens dmu_buf_t *db;
145114843421SMatthew Ahrens int objerr;
145214843421SMatthew Ahrens
145314843421SMatthew Ahrens if (issig(JUSTLOOKING) && issig(FORREAL))
1454be6fd75aSMatthew Ahrens return (SET_ERROR(EINTR));
145514843421SMatthew Ahrens
145614843421SMatthew Ahrens objerr = dmu_bonus_hold(os, obj, FTAG, &db);
14573b2aab18SMatthew Ahrens if (objerr != 0)
145814843421SMatthew Ahrens continue;
145968857716SLin Ling tx = dmu_tx_create(os);
146014843421SMatthew Ahrens dmu_tx_hold_bonus(tx, obj);
146114843421SMatthew Ahrens objerr = dmu_tx_assign(tx, TXG_WAIT);
14623b2aab18SMatthew Ahrens if (objerr != 0) {
146314843421SMatthew Ahrens dmu_tx_abort(tx);
146414843421SMatthew Ahrens continue;
146514843421SMatthew Ahrens }
146614843421SMatthew Ahrens dmu_buf_will_dirty(db, tx);
146714843421SMatthew Ahrens dmu_buf_rele(db, FTAG);
146814843421SMatthew Ahrens dmu_tx_commit(tx);
146914843421SMatthew Ahrens }
147014843421SMatthew Ahrens
1471503ad85cSMatthew Ahrens os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
147214843421SMatthew Ahrens txg_wait_synced(dmu_objset_pool(os), 0);
147314843421SMatthew Ahrens return (0);
147414843421SMatthew Ahrens }
147514843421SMatthew Ahrens
1476fa9e4066Sahrens void
dmu_objset_space(objset_t * os,uint64_t * refdbytesp,uint64_t * availbytesp,uint64_t * usedobjsp,uint64_t * availobjsp)1477a2eea2e1Sahrens dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp,
1478a2eea2e1Sahrens uint64_t *usedobjsp, uint64_t *availobjsp)
1479fa9e4066Sahrens {
1480503ad85cSMatthew Ahrens dsl_dataset_space(os->os_dsl_dataset, refdbytesp, availbytesp,
1481a2eea2e1Sahrens usedobjsp, availobjsp);
1482fa9e4066Sahrens }
1483a2eea2e1Sahrens
1484a2eea2e1Sahrens uint64_t
dmu_objset_fsid_guid(objset_t * os)1485a2eea2e1Sahrens dmu_objset_fsid_guid(objset_t *os)
1486a2eea2e1Sahrens {
1487503ad85cSMatthew Ahrens return (dsl_dataset_fsid_guid(os->os_dsl_dataset));
1488a2eea2e1Sahrens }
1489a2eea2e1Sahrens
1490a2eea2e1Sahrens void
dmu_objset_fast_stat(objset_t * os,dmu_objset_stats_t * stat)1491a2eea2e1Sahrens dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat)
1492a2eea2e1Sahrens {
1493503ad85cSMatthew Ahrens stat->dds_type = os->os_phys->os_type;
1494503ad85cSMatthew Ahrens if (os->os_dsl_dataset)
1495503ad85cSMatthew Ahrens dsl_dataset_fast_stat(os->os_dsl_dataset, stat);
1496a2eea2e1Sahrens }
1497a2eea2e1Sahrens
1498a2eea2e1Sahrens void
dmu_objset_stats(objset_t * os,nvlist_t * nv)1499a2eea2e1Sahrens dmu_objset_stats(objset_t *os, nvlist_t *nv)
1500a2eea2e1Sahrens {
1501503ad85cSMatthew Ahrens ASSERT(os->os_dsl_dataset ||
1502503ad85cSMatthew Ahrens os->os_phys->os_type == DMU_OST_META);
1503a2eea2e1Sahrens
1504503ad85cSMatthew Ahrens if (os->os_dsl_dataset != NULL)
1505503ad85cSMatthew Ahrens dsl_dataset_stats(os->os_dsl_dataset, nv);
1506a2eea2e1Sahrens
1507a2eea2e1Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE,
1508503ad85cSMatthew Ahrens os->os_phys->os_type);
150914843421SMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERACCOUNTING,
151014843421SMatthew Ahrens dmu_objset_userspace_present(os));
1511fa9e4066Sahrens }
1512fa9e4066Sahrens
1513fa9e4066Sahrens int
dmu_objset_is_snapshot(objset_t * os)1514fa9e4066Sahrens dmu_objset_is_snapshot(objset_t *os)
1515fa9e4066Sahrens {
1516503ad85cSMatthew Ahrens if (os->os_dsl_dataset != NULL)
1517bc9014e6SJustin Gibbs return (os->os_dsl_dataset->ds_is_snapshot);
1518fa9e4066Sahrens else
1519fa9e4066Sahrens return (B_FALSE);
1520fa9e4066Sahrens }
1521fa9e4066Sahrens
1522fa9e4066Sahrens int
dmu_snapshot_realname(objset_t * os,char * name,char * real,int maxlen,boolean_t * conflict)1523ab04eb8eStimh dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen,
1524ab04eb8eStimh boolean_t *conflict)
1525ab04eb8eStimh {
1526503ad85cSMatthew Ahrens dsl_dataset_t *ds = os->os_dsl_dataset;
1527ab04eb8eStimh uint64_t ignored;
1528ab04eb8eStimh
1529c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0)
1530be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT));
1531ab04eb8eStimh
1532ab04eb8eStimh return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset,
1533c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_snapnames_zapobj, name, 8, 1, &ignored,
1534c1379625SJustin T. Gibbs MT_FIRST, real, maxlen, conflict));
1535ab04eb8eStimh }
1536ab04eb8eStimh
1537ab04eb8eStimh int
dmu_snapshot_list_next(objset_t * os,int namelen,char * name,uint64_t * idp,uint64_t * offp,boolean_t * case_conflict)1538fa9e4066Sahrens dmu_snapshot_list_next(objset_t *os, int namelen, char *name,
1539b38f0970Sck153898 uint64_t *idp, uint64_t *offp, boolean_t *case_conflict)
1540fa9e4066Sahrens {
1541503ad85cSMatthew Ahrens dsl_dataset_t *ds = os->os_dsl_dataset;
1542fa9e4066Sahrens zap_cursor_t cursor;
1543fa9e4066Sahrens zap_attribute_t attr;
1544fa9e4066Sahrens
15453b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dmu_objset_pool(os)));
15463b2aab18SMatthew Ahrens
1547c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0)
1548be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT));
1549fa9e4066Sahrens
1550fa9e4066Sahrens zap_cursor_init_serialized(&cursor,
1551fa9e4066Sahrens ds->ds_dir->dd_pool->dp_meta_objset,
1552c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_snapnames_zapobj, *offp);
1553fa9e4066Sahrens
155487e5029aSahrens if (zap_cursor_retrieve(&cursor, &attr) != 0) {
155587e5029aSahrens zap_cursor_fini(&cursor);
1556be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT));
155787e5029aSahrens }
1558fa9e4066Sahrens
155987e5029aSahrens if (strlen(attr.za_name) + 1 > namelen) {
156087e5029aSahrens zap_cursor_fini(&cursor);
1561be6fd75aSMatthew Ahrens return (SET_ERROR(ENAMETOOLONG));
156287e5029aSahrens }
1563fa9e4066Sahrens
1564fa9e4066Sahrens (void) strcpy(name, attr.za_name);
156587e5029aSahrens if (idp)
156687e5029aSahrens *idp = attr.za_first_integer;
1567b38f0970Sck153898 if (case_conflict)
1568b38f0970Sck153898 *case_conflict = attr.za_normalization_conflict;
1569fa9e4066Sahrens zap_cursor_advance(&cursor);
1570fa9e4066Sahrens *offp = zap_cursor_serialize(&cursor);
157187e5029aSahrens zap_cursor_fini(&cursor);
157287e5029aSahrens
157387e5029aSahrens return (0);
157487e5029aSahrens }
157587e5029aSahrens
157687e5029aSahrens int
dmu_dir_list_next(objset_t * os,int namelen,char * name,uint64_t * idp,uint64_t * offp)157787e5029aSahrens dmu_dir_list_next(objset_t *os, int namelen, char *name,
157887e5029aSahrens uint64_t *idp, uint64_t *offp)
157987e5029aSahrens {
1580503ad85cSMatthew Ahrens dsl_dir_t *dd = os->os_dsl_dataset->ds_dir;
158187e5029aSahrens zap_cursor_t cursor;
158287e5029aSahrens zap_attribute_t attr;
158387e5029aSahrens
158487e5029aSahrens /* there is no next dir on a snapshot! */
1585503ad85cSMatthew Ahrens if (os->os_dsl_dataset->ds_object !=
1586c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_head_dataset_obj)
1587be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT));
158887e5029aSahrens
158987e5029aSahrens zap_cursor_init_serialized(&cursor,
159087e5029aSahrens dd->dd_pool->dp_meta_objset,
1591c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_child_dir_zapobj, *offp);
159287e5029aSahrens
159387e5029aSahrens if (zap_cursor_retrieve(&cursor, &attr) != 0) {
159487e5029aSahrens zap_cursor_fini(&cursor);
1595be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT));
159687e5029aSahrens }
159787e5029aSahrens
159887e5029aSahrens if (strlen(attr.za_name) + 1 > namelen) {
159987e5029aSahrens zap_cursor_fini(&cursor);
1600be6fd75aSMatthew Ahrens return (SET_ERROR(ENAMETOOLONG));
160187e5029aSahrens }
160287e5029aSahrens
160387e5029aSahrens (void) strcpy(name, attr.za_name);
160487e5029aSahrens if (idp)
160587e5029aSahrens *idp = attr.za_first_integer;
160687e5029aSahrens zap_cursor_advance(&cursor);
160787e5029aSahrens *offp = zap_cursor_serialize(&cursor);
160887e5029aSahrens zap_cursor_fini(&cursor);
1609fa9e4066Sahrens
1610fa9e4066Sahrens return (0);
1611fa9e4066Sahrens }
1612fa9e4066Sahrens
161312380e1eSArne Jansen typedef struct dmu_objset_find_ctx {
161412380e1eSArne Jansen taskq_t *dc_tq;
161512380e1eSArne Jansen dsl_pool_t *dc_dp;
161612380e1eSArne Jansen uint64_t dc_ddobj;
161712380e1eSArne Jansen int (*dc_func)(dsl_pool_t *, dsl_dataset_t *, void *);
161812380e1eSArne Jansen void *dc_arg;
161912380e1eSArne Jansen int dc_flags;
162012380e1eSArne Jansen kmutex_t *dc_error_lock;
162112380e1eSArne Jansen int *dc_error;
162212380e1eSArne Jansen } dmu_objset_find_ctx_t;
162312380e1eSArne Jansen
162412380e1eSArne Jansen static void
dmu_objset_find_dp_impl(dmu_objset_find_ctx_t * dcp)162512380e1eSArne Jansen dmu_objset_find_dp_impl(dmu_objset_find_ctx_t *dcp)
1626088f3894Sahrens {
162712380e1eSArne Jansen dsl_pool_t *dp = dcp->dc_dp;
162812380e1eSArne Jansen dmu_objset_find_ctx_t *child_dcp;
1629fa9e4066Sahrens dsl_dir_t *dd;
1630088f3894Sahrens dsl_dataset_t *ds;
1631fa9e4066Sahrens zap_cursor_t zc;
1632b7661cccSmmusante zap_attribute_t *attr;
1633088f3894Sahrens uint64_t thisobj;
163412380e1eSArne Jansen int err = 0;
1635fa9e4066Sahrens
163612380e1eSArne Jansen /* don't process if there already was an error */
163712380e1eSArne Jansen if (*dcp->dc_error != 0)
163812380e1eSArne Jansen goto out;
16393b2aab18SMatthew Ahrens
164012380e1eSArne Jansen err = dsl_dir_hold_obj(dp, dcp->dc_ddobj, NULL, FTAG, &dd);
16413b2aab18SMatthew Ahrens if (err != 0)
164212380e1eSArne Jansen goto out;
1643fa9e4066Sahrens
1644088f3894Sahrens /* Don't visit hidden ($MOS & $ORIGIN) objsets. */
1645088f3894Sahrens if (dd->dd_myname[0] == '$') {
16463b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG);
164712380e1eSArne Jansen goto out;
1648088f3894Sahrens }
1649088f3894Sahrens
1650c1379625SJustin T. Gibbs thisobj = dsl_dir_phys(dd)->dd_head_dataset_obj;
1651b7661cccSmmusante attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
1652fa9e4066Sahrens
1653fa9e4066Sahrens /*
1654fa9e4066Sahrens * Iterate over all children.
1655fa9e4066Sahrens */
165612380e1eSArne Jansen if (dcp->dc_flags & DS_FIND_CHILDREN) {
1657088f3894Sahrens for (zap_cursor_init(&zc, dp->dp_meta_objset,
1658c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_child_dir_zapobj);
1659b7661cccSmmusante zap_cursor_retrieve(&zc, attr) == 0;
1660fa9e4066Sahrens (void) zap_cursor_advance(&zc)) {
16613b2aab18SMatthew Ahrens ASSERT3U(attr->za_integer_length, ==,
16623b2aab18SMatthew Ahrens sizeof (uint64_t));
16633b2aab18SMatthew Ahrens ASSERT3U(attr->za_num_integers, ==, 1);
1664fa9e4066Sahrens
166512380e1eSArne Jansen child_dcp = kmem_alloc(sizeof (*child_dcp), KM_SLEEP);
166612380e1eSArne Jansen *child_dcp = *dcp;
166712380e1eSArne Jansen child_dcp->dc_ddobj = attr->za_first_integer;
166812380e1eSArne Jansen if (dcp->dc_tq != NULL)
166912380e1eSArne Jansen (void) taskq_dispatch(dcp->dc_tq,
167012380e1eSArne Jansen dmu_objset_find_dp_cb, child_dcp, TQ_SLEEP);
167112380e1eSArne Jansen else
167212380e1eSArne Jansen dmu_objset_find_dp_impl(child_dcp);
1673fa9e4066Sahrens }
167487e5029aSahrens zap_cursor_fini(&zc);
16750b69c2f0Sahrens }
1676fa9e4066Sahrens
1677fa9e4066Sahrens /*
1678fa9e4066Sahrens * Iterate over all snapshots.
1679fa9e4066Sahrens */
168012380e1eSArne Jansen if (dcp->dc_flags & DS_FIND_SNAPSHOTS) {
16813b2aab18SMatthew Ahrens dsl_dataset_t *ds;
1682088f3894Sahrens err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
1683fa9e4066Sahrens
1684088f3894Sahrens if (err == 0) {
1685c1379625SJustin T. Gibbs uint64_t snapobj;
1686c1379625SJustin T. Gibbs
1687c1379625SJustin T. Gibbs snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
1688088f3894Sahrens dsl_dataset_rele(ds, FTAG);
1689fa9e4066Sahrens
1690088f3894Sahrens for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
1691b7661cccSmmusante zap_cursor_retrieve(&zc, attr) == 0;
1692fa9e4066Sahrens (void) zap_cursor_advance(&zc)) {
16933b2aab18SMatthew Ahrens ASSERT3U(attr->za_integer_length, ==,
1694088f3894Sahrens sizeof (uint64_t));
16953b2aab18SMatthew Ahrens ASSERT3U(attr->za_num_integers, ==, 1);
1696fa9e4066Sahrens
16973b2aab18SMatthew Ahrens err = dsl_dataset_hold_obj(dp,
16983b2aab18SMatthew Ahrens attr->za_first_integer, FTAG, &ds);
16993b2aab18SMatthew Ahrens if (err != 0)
17003b2aab18SMatthew Ahrens break;
170112380e1eSArne Jansen err = dcp->dc_func(dp, ds, dcp->dc_arg);
17023b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG);
17033b2aab18SMatthew Ahrens if (err != 0)
17041d452cf5Sahrens break;
1705fa9e4066Sahrens }
170687e5029aSahrens zap_cursor_fini(&zc);
1707fa9e4066Sahrens }
1708088f3894Sahrens }
1709fa9e4066Sahrens
17103b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG);
1711b7661cccSmmusante kmem_free(attr, sizeof (zap_attribute_t));
1712fa9e4066Sahrens
17133b2aab18SMatthew Ahrens if (err != 0)
171412380e1eSArne Jansen goto out;
17151d452cf5Sahrens
1716fa9e4066Sahrens /*
17173b2aab18SMatthew Ahrens * Apply to self.
1718fa9e4066Sahrens */
17193b2aab18SMatthew Ahrens err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
17203b2aab18SMatthew Ahrens if (err != 0)
172112380e1eSArne Jansen goto out;
172212380e1eSArne Jansen err = dcp->dc_func(dp, ds, dcp->dc_arg);
17233b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG);
172412380e1eSArne Jansen
172512380e1eSArne Jansen out:
172612380e1eSArne Jansen if (err != 0) {
172712380e1eSArne Jansen mutex_enter(dcp->dc_error_lock);
172812380e1eSArne Jansen /* only keep first error */
172912380e1eSArne Jansen if (*dcp->dc_error == 0)
173012380e1eSArne Jansen *dcp->dc_error = err;
173112380e1eSArne Jansen mutex_exit(dcp->dc_error_lock);
173212380e1eSArne Jansen }
173312380e1eSArne Jansen
173412380e1eSArne Jansen kmem_free(dcp, sizeof (*dcp));
173512380e1eSArne Jansen }
173612380e1eSArne Jansen
173712380e1eSArne Jansen static void
dmu_objset_find_dp_cb(void * arg)173812380e1eSArne Jansen dmu_objset_find_dp_cb(void *arg)
173912380e1eSArne Jansen {
174012380e1eSArne Jansen dmu_objset_find_ctx_t *dcp = arg;
174112380e1eSArne Jansen dsl_pool_t *dp = dcp->dc_dp;
174212380e1eSArne Jansen
17431d3f896fSArne Jansen /*
17441d3f896fSArne Jansen * We need to get a pool_config_lock here, as there are several
17451d3f896fSArne Jansen * asssert(pool_config_held) down the stack. Getting a lock via
17461d3f896fSArne Jansen * dsl_pool_config_enter is risky, as it might be stalled by a
17471d3f896fSArne Jansen * pending writer. This would deadlock, as the write lock can
17481d3f896fSArne Jansen * only be granted when our parent thread gives up the lock.
17491d3f896fSArne Jansen * The _prio interface gives us priority over a pending writer.
17501d3f896fSArne Jansen */
17511d3f896fSArne Jansen dsl_pool_config_enter_prio(dp, FTAG);
175212380e1eSArne Jansen
175312380e1eSArne Jansen dmu_objset_find_dp_impl(dcp);
175412380e1eSArne Jansen
175512380e1eSArne Jansen dsl_pool_config_exit(dp, FTAG);
175612380e1eSArne Jansen }
175712380e1eSArne Jansen
175812380e1eSArne Jansen /*
175912380e1eSArne Jansen * Find objsets under and including ddobj, call func(ds) on each.
176012380e1eSArne Jansen * The order for the enumeration is completely undefined.
176112380e1eSArne Jansen * func is called with dsl_pool_config held.
176212380e1eSArne Jansen */
176312380e1eSArne 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)176412380e1eSArne Jansen dmu_objset_find_dp(dsl_pool_t *dp, uint64_t ddobj,
176512380e1eSArne Jansen int func(dsl_pool_t *, dsl_dataset_t *, void *), void *arg, int flags)
176612380e1eSArne Jansen {
176712380e1eSArne Jansen int error = 0;
176812380e1eSArne Jansen taskq_t *tq = NULL;
176912380e1eSArne Jansen int ntasks;
177012380e1eSArne Jansen dmu_objset_find_ctx_t *dcp;
177112380e1eSArne Jansen kmutex_t err_lock;
177212380e1eSArne Jansen
177312380e1eSArne Jansen mutex_init(&err_lock, NULL, MUTEX_DEFAULT, NULL);
177412380e1eSArne Jansen dcp = kmem_alloc(sizeof (*dcp), KM_SLEEP);
177512380e1eSArne Jansen dcp->dc_tq = NULL;
177612380e1eSArne Jansen dcp->dc_dp = dp;
177712380e1eSArne Jansen dcp->dc_ddobj = ddobj;
177812380e1eSArne Jansen dcp->dc_func = func;
177912380e1eSArne Jansen dcp->dc_arg = arg;
178012380e1eSArne Jansen dcp->dc_flags = flags;
178112380e1eSArne Jansen dcp->dc_error_lock = &err_lock;
178212380e1eSArne Jansen dcp->dc_error = &error;
178312380e1eSArne Jansen
178412380e1eSArne Jansen if ((flags & DS_FIND_SERIALIZE) || dsl_pool_config_held_writer(dp)) {
178512380e1eSArne Jansen /*
178612380e1eSArne Jansen * In case a write lock is held we can't make use of
178712380e1eSArne Jansen * parallelism, as down the stack of the worker threads
178812380e1eSArne Jansen * the lock is asserted via dsl_pool_config_held.
178912380e1eSArne Jansen * In case of a read lock this is solved by getting a read
179012380e1eSArne Jansen * lock in each worker thread, which isn't possible in case
179112380e1eSArne Jansen * of a writer lock. So we fall back to the synchronous path
179212380e1eSArne Jansen * here.
179312380e1eSArne Jansen * In the future it might be possible to get some magic into
179412380e1eSArne Jansen * dsl_pool_config_held in a way that it returns true for
179512380e1eSArne Jansen * the worker threads so that a single lock held from this
179612380e1eSArne Jansen * thread suffices. For now, stay single threaded.
179712380e1eSArne Jansen */
179812380e1eSArne Jansen dmu_objset_find_dp_impl(dcp);
17992bad2258SSteven Hartland mutex_destroy(&err_lock);
180012380e1eSArne Jansen
180112380e1eSArne Jansen return (error);
180212380e1eSArne Jansen }
180312380e1eSArne Jansen
180412380e1eSArne Jansen ntasks = dmu_find_threads;
180512380e1eSArne Jansen if (ntasks == 0)
180612380e1eSArne Jansen ntasks = vdev_count_leaves(dp->dp_spa) * 4;
180712380e1eSArne Jansen tq = taskq_create("dmu_objset_find", ntasks, minclsyspri, ntasks,
180812380e1eSArne Jansen INT_MAX, 0);
180912380e1eSArne Jansen if (tq == NULL) {
181012380e1eSArne Jansen kmem_free(dcp, sizeof (*dcp));
18112bad2258SSteven Hartland mutex_destroy(&err_lock);
18122bad2258SSteven Hartland
181312380e1eSArne Jansen return (SET_ERROR(ENOMEM));
181412380e1eSArne Jansen }
181512380e1eSArne Jansen dcp->dc_tq = tq;
181612380e1eSArne Jansen
181712380e1eSArne Jansen /* dcp will be freed by task */
181812380e1eSArne Jansen (void) taskq_dispatch(tq, dmu_objset_find_dp_cb, dcp, TQ_SLEEP);
181912380e1eSArne Jansen
182012380e1eSArne Jansen /*
182112380e1eSArne Jansen * PORTING: this code relies on the property of taskq_wait to wait
182212380e1eSArne Jansen * until no more tasks are queued and no more tasks are active. As
182312380e1eSArne Jansen * we always queue new tasks from within other tasks, task_wait
182412380e1eSArne Jansen * reliably waits for the full recursion to finish, even though we
182512380e1eSArne Jansen * enqueue new tasks after taskq_wait has been called.
182612380e1eSArne Jansen * On platforms other than illumos, taskq_wait may not have this
182712380e1eSArne Jansen * property.
182812380e1eSArne Jansen */
182912380e1eSArne Jansen taskq_wait(tq);
183012380e1eSArne Jansen taskq_destroy(tq);
183112380e1eSArne Jansen mutex_destroy(&err_lock);
183212380e1eSArne Jansen
183312380e1eSArne Jansen return (error);
1834fa9e4066Sahrens }
1835f18faf3fSek110237
18363b2aab18SMatthew Ahrens /*
18373b2aab18SMatthew Ahrens * Find all objsets under name, and for each, call 'func(child_name, arg)'.
18383b2aab18SMatthew Ahrens * The dp_config_rwlock must not be held when this is called, and it
18393b2aab18SMatthew Ahrens * will not be held when the callback is called.
18403b2aab18SMatthew Ahrens * Therefore this function should only be used when the pool is not changing
18413b2aab18SMatthew Ahrens * (e.g. in syncing context), or the callback can deal with the possible races.
18423b2aab18SMatthew Ahrens */
18433b2aab18SMatthew Ahrens static int
dmu_objset_find_impl(spa_t * spa,const char * name,int func (const char *,void *),void * arg,int flags)18443b2aab18SMatthew Ahrens dmu_objset_find_impl(spa_t *spa, const char *name,
18453b2aab18SMatthew Ahrens int func(const char *, void *), void *arg, int flags)
18467f73c863SRich Morris {
18473b2aab18SMatthew Ahrens dsl_dir_t *dd;
18483b2aab18SMatthew Ahrens dsl_pool_t *dp = spa_get_dsl(spa);
18497f73c863SRich Morris dsl_dataset_t *ds;
18503b2aab18SMatthew Ahrens zap_cursor_t zc;
18513b2aab18SMatthew Ahrens zap_attribute_t *attr;
18523b2aab18SMatthew Ahrens char *child;
18533b2aab18SMatthew Ahrens uint64_t thisobj;
18543b2aab18SMatthew Ahrens int err;
18557f73c863SRich Morris
18563b2aab18SMatthew Ahrens dsl_pool_config_enter(dp, FTAG);
18573b2aab18SMatthew Ahrens
18583b2aab18SMatthew Ahrens err = dsl_dir_hold(dp, name, FTAG, &dd, NULL);
18593b2aab18SMatthew Ahrens if (err != 0) {
18603b2aab18SMatthew Ahrens dsl_pool_config_exit(dp, FTAG);
18613b2aab18SMatthew Ahrens return (err);
18623b2aab18SMatthew Ahrens }
18633b2aab18SMatthew Ahrens
18643b2aab18SMatthew Ahrens /* Don't visit hidden ($MOS & $ORIGIN) objsets. */
18653b2aab18SMatthew Ahrens if (dd->dd_myname[0] == '$') {
18663b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG);
18673b2aab18SMatthew Ahrens dsl_pool_config_exit(dp, FTAG);
18687f73c863SRich Morris return (0);
18697f73c863SRich Morris }
18707f73c863SRich Morris
1871c1379625SJustin T. Gibbs thisobj = dsl_dir_phys(dd)->dd_head_dataset_obj;
18723b2aab18SMatthew Ahrens attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
18733b2aab18SMatthew Ahrens
18743b2aab18SMatthew Ahrens /*
18753b2aab18SMatthew Ahrens * Iterate over all children.
18763b2aab18SMatthew Ahrens */
18773b2aab18SMatthew Ahrens if (flags & DS_FIND_CHILDREN) {
18783b2aab18SMatthew Ahrens for (zap_cursor_init(&zc, dp->dp_meta_objset,
1879c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_child_dir_zapobj);
18803b2aab18SMatthew Ahrens zap_cursor_retrieve(&zc, attr) == 0;
18813b2aab18SMatthew Ahrens (void) zap_cursor_advance(&zc)) {
18823b2aab18SMatthew Ahrens ASSERT3U(attr->za_integer_length, ==,
18833b2aab18SMatthew Ahrens sizeof (uint64_t));
18843b2aab18SMatthew Ahrens ASSERT3U(attr->za_num_integers, ==, 1);
18853b2aab18SMatthew Ahrens
18863b2aab18SMatthew Ahrens child = kmem_asprintf("%s/%s", name, attr->za_name);
18873b2aab18SMatthew Ahrens dsl_pool_config_exit(dp, FTAG);
18883b2aab18SMatthew Ahrens err = dmu_objset_find_impl(spa, child,
18893b2aab18SMatthew Ahrens func, arg, flags);
18903b2aab18SMatthew Ahrens dsl_pool_config_enter(dp, FTAG);
18913b2aab18SMatthew Ahrens strfree(child);
18923b2aab18SMatthew Ahrens if (err != 0)
18933b2aab18SMatthew Ahrens break;
18943b2aab18SMatthew Ahrens }
18953b2aab18SMatthew Ahrens zap_cursor_fini(&zc);
18963b2aab18SMatthew Ahrens
18973b2aab18SMatthew Ahrens if (err != 0) {
18983b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG);
18993b2aab18SMatthew Ahrens dsl_pool_config_exit(dp, FTAG);
19003b2aab18SMatthew Ahrens kmem_free(attr, sizeof (zap_attribute_t));
19013b2aab18SMatthew Ahrens return (err);
19023b2aab18SMatthew Ahrens }
19033b2aab18SMatthew Ahrens }
19043b2aab18SMatthew Ahrens
19053b2aab18SMatthew Ahrens /*
19063b2aab18SMatthew Ahrens * Iterate over all snapshots.
19073b2aab18SMatthew Ahrens */
19083b2aab18SMatthew Ahrens if (flags & DS_FIND_SNAPSHOTS) {
19093b2aab18SMatthew Ahrens err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
19103b2aab18SMatthew Ahrens
19113b2aab18SMatthew Ahrens if (err == 0) {
1912c1379625SJustin T. Gibbs uint64_t snapobj;
1913c1379625SJustin T. Gibbs
1914c1379625SJustin T. Gibbs snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
19157f73c863SRich Morris dsl_dataset_rele(ds, FTAG);
19163b2aab18SMatthew Ahrens
19173b2aab18SMatthew Ahrens for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
19183b2aab18SMatthew Ahrens zap_cursor_retrieve(&zc, attr) == 0;
19193b2aab18SMatthew Ahrens (void) zap_cursor_advance(&zc)) {
19203b2aab18SMatthew Ahrens ASSERT3U(attr->za_integer_length, ==,
19213b2aab18SMatthew Ahrens sizeof (uint64_t));
19223b2aab18SMatthew Ahrens ASSERT3U(attr->za_num_integers, ==, 1);
19233b2aab18SMatthew Ahrens
19243b2aab18SMatthew Ahrens child = kmem_asprintf("%s@%s",
19253b2aab18SMatthew Ahrens name, attr->za_name);
19263b2aab18SMatthew Ahrens dsl_pool_config_exit(dp, FTAG);
19273b2aab18SMatthew Ahrens err = func(child, arg);
19283b2aab18SMatthew Ahrens dsl_pool_config_enter(dp, FTAG);
19293b2aab18SMatthew Ahrens strfree(child);
19303b2aab18SMatthew Ahrens if (err != 0)
19313b2aab18SMatthew Ahrens break;
19323b2aab18SMatthew Ahrens }
19333b2aab18SMatthew Ahrens zap_cursor_fini(&zc);
19343b2aab18SMatthew Ahrens }
19353b2aab18SMatthew Ahrens }
19363b2aab18SMatthew Ahrens
19373b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG);
19383b2aab18SMatthew Ahrens kmem_free(attr, sizeof (zap_attribute_t));
19393b2aab18SMatthew Ahrens dsl_pool_config_exit(dp, FTAG);
19403b2aab18SMatthew Ahrens
19413b2aab18SMatthew Ahrens if (err != 0)
19423b2aab18SMatthew Ahrens return (err);
19433b2aab18SMatthew Ahrens
19443b2aab18SMatthew Ahrens /* Apply to self. */
19453b2aab18SMatthew Ahrens return (func(name, arg));
19463b2aab18SMatthew Ahrens }
19473b2aab18SMatthew Ahrens
19483b2aab18SMatthew Ahrens /*
19493b2aab18SMatthew Ahrens * See comment above dmu_objset_find_impl().
19503b2aab18SMatthew Ahrens */
19513b2aab18SMatthew Ahrens int
dmu_objset_find(char * name,int func (const char *,void *),void * arg,int flags)19523b2aab18SMatthew Ahrens dmu_objset_find(char *name, int func(const char *, void *), void *arg,
19533b2aab18SMatthew Ahrens int flags)
19543b2aab18SMatthew Ahrens {
19553b2aab18SMatthew Ahrens spa_t *spa;
19563b2aab18SMatthew Ahrens int error;
19573b2aab18SMatthew Ahrens
19583b2aab18SMatthew Ahrens error = spa_open(name, &spa, FTAG);
19593b2aab18SMatthew Ahrens if (error != 0)
19603b2aab18SMatthew Ahrens return (error);
19613b2aab18SMatthew Ahrens error = dmu_objset_find_impl(spa, name, func, arg, flags);
19623b2aab18SMatthew Ahrens spa_close(spa, FTAG);
19633b2aab18SMatthew Ahrens return (error);
19647f73c863SRich Morris }
19657f73c863SRich Morris
1966f18faf3fSek110237 void
dmu_objset_set_user(objset_t * os,void * user_ptr)1967f18faf3fSek110237 dmu_objset_set_user(objset_t *os, void *user_ptr)
1968f18faf3fSek110237 {
1969503ad85cSMatthew Ahrens ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
1970503ad85cSMatthew Ahrens os->os_user_ptr = user_ptr;
1971f18faf3fSek110237 }
1972f18faf3fSek110237
1973f18faf3fSek110237 void *
dmu_objset_get_user(objset_t * os)1974f18faf3fSek110237 dmu_objset_get_user(objset_t *os)
1975f18faf3fSek110237 {
1976503ad85cSMatthew Ahrens ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
1977503ad85cSMatthew Ahrens return (os->os_user_ptr);
1978f18faf3fSek110237 }
19793b2aab18SMatthew Ahrens
19803b2aab18SMatthew Ahrens /*
19813b2aab18SMatthew Ahrens * Determine name of filesystem, given name of snapshot.
1982*40a5c998SMatthew Ahrens * buf must be at least ZFS_MAX_DATASET_NAME_LEN bytes
19833b2aab18SMatthew Ahrens */
19843b2aab18SMatthew Ahrens int
dmu_fsname(const char * snapname,char * buf)19853b2aab18SMatthew Ahrens dmu_fsname(const char *snapname, char *buf)
19863b2aab18SMatthew Ahrens {
19873b2aab18SMatthew Ahrens char *atp = strchr(snapname, '@');
19883b2aab18SMatthew Ahrens if (atp == NULL)
1989be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
1990*40a5c998SMatthew Ahrens if (atp - snapname >= ZFS_MAX_DATASET_NAME_LEN)
1991be6fd75aSMatthew Ahrens return (SET_ERROR(ENAMETOOLONG));
19923b2aab18SMatthew Ahrens (void) strlcpy(buf, snapname, atp - snapname + 1);
19933b2aab18SMatthew Ahrens return (0);
19943b2aab18SMatthew Ahrens }
1995