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