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