9337 zfs get all is slow due to uncached metadata
[unleashed.git] / usr / src / uts / common / fs / zfs / dmu_objset.c
blobf345ed9383e503387585c61811bdf90ac64640fe
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
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.
33 /* Portions Copyright 2010 Robert Milkowski */
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>
59 * Needed to close a window in dnode_move() that allows the objset to be freed
60 * before it can be safely accessed.
62 krwlock_t os_lock;
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.
70 int dmu_find_threads = 0;
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.
77 int dmu_rescan_dnode_threshold = 131072;
79 static void dmu_objset_find_dp_cb(void *arg);
81 void
82 dmu_objset_init(void)
84 rw_init(&os_lock, NULL, RW_DEFAULT, NULL);
87 void
88 dmu_objset_fini(void)
90 rw_destroy(&os_lock);
93 spa_t *
94 dmu_objset_spa(objset_t *os)
96 return (os->os_spa);
99 zilog_t *
100 dmu_objset_zil(objset_t *os)
102 return (os->os_zil);
105 dsl_pool_t *
106 dmu_objset_pool(objset_t *os)
108 dsl_dataset_t *ds;
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));
116 dsl_dataset_t *
117 dmu_objset_ds(objset_t *os)
119 return (os->os_dsl_dataset);
122 dmu_objset_type_t
123 dmu_objset_type(objset_t *os)
125 return (os->os_phys->os_type);
128 void
129 dmu_objset_name(objset_t *os, char *buf)
131 dsl_dataset_name(os->os_dsl_dataset, buf);
134 uint64_t
135 dmu_objset_id(objset_t *os)
137 dsl_dataset_t *ds = os->os_dsl_dataset;
139 return (ds ? ds->ds_object : 0);
142 zfs_sync_type_t
143 dmu_objset_syncprop(objset_t *os)
145 return (os->os_sync);
148 zfs_logbias_op_t
149 dmu_objset_logbias(objset_t *os)
151 return (os->os_logbias);
154 static void
155 checksum_changed_cb(void *arg, uint64_t newval)
157 objset_t *os = arg;
160 * Inheritance should have been done by now.
162 ASSERT(newval != ZIO_CHECKSUM_INHERIT);
164 os->os_checksum = zio_checksum_select(newval, ZIO_CHECKSUM_ON_VALUE);
167 static void
168 compression_changed_cb(void *arg, uint64_t newval)
170 objset_t *os = arg;
173 * Inheritance and range checking should have been done by now.
175 ASSERT(newval != ZIO_COMPRESS_INHERIT);
177 os->os_compress = zio_compress_select(os->os_spa, newval,
178 ZIO_COMPRESS_ON);
181 static void
182 copies_changed_cb(void *arg, uint64_t newval)
184 objset_t *os = arg;
187 * Inheritance and range checking should have been done by now.
189 ASSERT(newval > 0);
190 ASSERT(newval <= spa_max_replication(os->os_spa));
192 os->os_copies = newval;
195 static void
196 dedup_changed_cb(void *arg, uint64_t newval)
198 objset_t *os = arg;
199 spa_t *spa = os->os_spa;
200 enum zio_checksum checksum;
203 * Inheritance should have been done by now.
205 ASSERT(newval != ZIO_CHECKSUM_INHERIT);
207 checksum = zio_checksum_dedup_select(spa, newval, ZIO_CHECKSUM_OFF);
209 os->os_dedup_checksum = checksum & ZIO_CHECKSUM_MASK;
210 os->os_dedup_verify = !!(checksum & ZIO_CHECKSUM_VERIFY);
213 static void
214 primary_cache_changed_cb(void *arg, uint64_t newval)
216 objset_t *os = arg;
219 * Inheritance and range checking should have been done by now.
221 ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
222 newval == ZFS_CACHE_METADATA);
224 os->os_primary_cache = newval;
227 static void
228 secondary_cache_changed_cb(void *arg, uint64_t newval)
230 objset_t *os = arg;
233 * Inheritance and range checking should have been done by now.
235 ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
236 newval == ZFS_CACHE_METADATA);
238 os->os_secondary_cache = newval;
241 static void
242 sync_changed_cb(void *arg, uint64_t newval)
244 objset_t *os = arg;
247 * Inheritance and range checking should have been done by now.
249 ASSERT(newval == ZFS_SYNC_STANDARD || newval == ZFS_SYNC_ALWAYS ||
250 newval == ZFS_SYNC_DISABLED);
252 os->os_sync = newval;
253 if (os->os_zil)
254 zil_set_sync(os->os_zil, newval);
257 static void
258 redundant_metadata_changed_cb(void *arg, uint64_t newval)
260 objset_t *os = arg;
263 * Inheritance and range checking should have been done by now.
265 ASSERT(newval == ZFS_REDUNDANT_METADATA_ALL ||
266 newval == ZFS_REDUNDANT_METADATA_MOST);
268 os->os_redundant_metadata = newval;
271 static void
272 logbias_changed_cb(void *arg, uint64_t newval)
274 objset_t *os = arg;
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);
283 static void
284 recordsize_changed_cb(void *arg, uint64_t newval)
286 objset_t *os = arg;
288 os->os_recordsize = newval;
291 void
292 dmu_objset_byteswap(void *buf, size_t size)
294 objset_phys_t *osp = buf;
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);
308 * The hash is a CRC-based hash of the objset_t pointer and the object number.
310 static uint64_t
311 dnode_hash(const objset_t *os, uint64_t obj)
313 uintptr_t osv = (uintptr_t)os;
314 uint64_t crc = -1ULL;
316 ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY);
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.
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];
326 crc ^= (osv>>14) ^ (obj>>24);
328 return (crc);
331 unsigned int
332 dnode_multilist_index_func(multilist_t *ml, void *obj)
334 dnode_t *dn = obj;
335 return (dnode_hash(dn->dn_objset, dn->dn_object) %
336 multilist_get_num_sublists(ml));
340 * Instantiates the objset_t in-memory structure corresponding to the
341 * objset_phys_t that's pointed to by the specified blkptr_t.
344 dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
345 objset_t **osp)
347 objset_t *os;
348 int i, err;
350 ASSERT(ds == NULL || MUTEX_HELD(&ds->ds_opening_lock));
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.
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);
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);
373 if (DMU_OS_IS_L2CACHEABLE(os))
374 aflags |= ARC_FLAG_L2CACHE;
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);
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;
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);
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.
417 if (ds != NULL) {
418 boolean_t needlock = B_FALSE;
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.
425 if (!dsl_pool_config_held(dmu_objset_pool(os))) {
426 needlock = B_TRUE;
427 dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
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);
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);
443 if (err == 0) {
444 err = dsl_prop_register(ds,
445 zfs_prop_to_name(ZFS_PROP_COMPRESSION),
446 compression_changed_cb, os);
448 if (err == 0) {
449 err = dsl_prop_register(ds,
450 zfs_prop_to_name(ZFS_PROP_COPIES),
451 copies_changed_cb, os);
453 if (err == 0) {
454 err = dsl_prop_register(ds,
455 zfs_prop_to_name(ZFS_PROP_DEDUP),
456 dedup_changed_cb, os);
458 if (err == 0) {
459 err = dsl_prop_register(ds,
460 zfs_prop_to_name(ZFS_PROP_LOGBIAS),
461 logbias_changed_cb, os);
463 if (err == 0) {
464 err = dsl_prop_register(ds,
465 zfs_prop_to_name(ZFS_PROP_SYNC),
466 sync_changed_cb, os);
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);
474 if (err == 0) {
475 err = dsl_prop_register(ds,
476 zfs_prop_to_name(ZFS_PROP_RECORDSIZE),
477 recordsize_changed_cb, os);
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);
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;
500 * These properties will be filled in by the logic in zfs_get_zplprop()
501 * when they are queried for the first time.
503 os->os_version = OBJSET_PROP_UNINITIALIZED;
504 os->os_normalization = OBJSET_PROP_UNINITIALIZED;
505 os->os_utf8only = OBJSET_PROP_UNINITIALIZED;
506 os->os_casesensitivity = OBJSET_PROP_UNINITIALIZED;
508 if (ds == NULL || !ds->ds_is_snapshot)
509 os->os_zil_header = os->os_phys->os_zil_header;
510 os->os_zil = zil_alloc(os, &os->os_zil_header);
512 for (i = 0; i < TXG_SIZE; i++) {
513 os->os_dirty_dnodes[i] = multilist_create(sizeof (dnode_t),
514 offsetof(dnode_t, dn_dirty_link[i]),
515 dnode_multilist_index_func);
517 list_create(&os->os_dnodes, sizeof (dnode_t),
518 offsetof(dnode_t, dn_link));
519 list_create(&os->os_downgraded_dbufs, sizeof (dmu_buf_impl_t),
520 offsetof(dmu_buf_impl_t, db_link));
522 mutex_init(&os->os_lock, NULL, MUTEX_DEFAULT, NULL);
523 mutex_init(&os->os_userused_lock, NULL, MUTEX_DEFAULT, NULL);
524 mutex_init(&os->os_obj_lock, NULL, MUTEX_DEFAULT, NULL);
525 mutex_init(&os->os_user_ptr_lock, NULL, MUTEX_DEFAULT, NULL);
527 dnode_special_open(os, &os->os_phys->os_meta_dnode,
528 DMU_META_DNODE_OBJECT, &os->os_meta_dnode);
529 if (arc_buf_size(os->os_phys_buf) >= sizeof (objset_phys_t)) {
530 dnode_special_open(os, &os->os_phys->os_userused_dnode,
531 DMU_USERUSED_OBJECT, &os->os_userused_dnode);
532 dnode_special_open(os, &os->os_phys->os_groupused_dnode,
533 DMU_GROUPUSED_OBJECT, &os->os_groupused_dnode);
536 *osp = os;
537 return (0);
541 dmu_objset_from_ds(dsl_dataset_t *ds, objset_t **osp)
543 int err = 0;
546 * We shouldn't be doing anything with dsl_dataset_t's unless the
547 * pool_config lock is held, or the dataset is long-held.
549 ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool) ||
550 dsl_dataset_long_held(ds));
552 mutex_enter(&ds->ds_opening_lock);
553 if (ds->ds_objset == NULL) {
554 objset_t *os;
555 rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
556 err = dmu_objset_open_impl(dsl_dataset_get_spa(ds),
557 ds, dsl_dataset_get_blkptr(ds), &os);
558 rrw_exit(&ds->ds_bp_rwlock, FTAG);
560 if (err == 0) {
561 mutex_enter(&ds->ds_lock);
562 ASSERT(ds->ds_objset == NULL);
563 ds->ds_objset = os;
564 mutex_exit(&ds->ds_lock);
567 *osp = ds->ds_objset;
568 mutex_exit(&ds->ds_opening_lock);
569 return (err);
573 * Holds the pool while the objset is held. Therefore only one objset
574 * can be held at a time.
577 dmu_objset_hold(const char *name, void *tag, objset_t **osp)
579 dsl_pool_t *dp;
580 dsl_dataset_t *ds;
581 int err;
583 err = dsl_pool_hold(name, tag, &dp);
584 if (err != 0)
585 return (err);
586 err = dsl_dataset_hold(dp, name, tag, &ds);
587 if (err != 0) {
588 dsl_pool_rele(dp, tag);
589 return (err);
592 err = dmu_objset_from_ds(ds, osp);
593 if (err != 0) {
594 dsl_dataset_rele(ds, tag);
595 dsl_pool_rele(dp, tag);
598 return (err);
601 static int
602 dmu_objset_own_impl(dsl_dataset_t *ds, dmu_objset_type_t type,
603 boolean_t readonly, void *tag, objset_t **osp)
605 int err;
607 err = dmu_objset_from_ds(ds, osp);
608 if (err != 0) {
609 dsl_dataset_disown(ds, tag);
610 } else if (type != DMU_OST_ANY && type != (*osp)->os_phys->os_type) {
611 dsl_dataset_disown(ds, tag);
612 return (SET_ERROR(EINVAL));
613 } else if (!readonly && dsl_dataset_is_snapshot(ds)) {
614 dsl_dataset_disown(ds, tag);
615 return (SET_ERROR(EROFS));
617 return (err);
621 * dsl_pool must not be held when this is called.
622 * Upon successful return, there will be a longhold on the dataset,
623 * and the dsl_pool will not be held.
626 dmu_objset_own(const char *name, dmu_objset_type_t type,
627 boolean_t readonly, void *tag, objset_t **osp)
629 dsl_pool_t *dp;
630 dsl_dataset_t *ds;
631 int err;
633 err = dsl_pool_hold(name, FTAG, &dp);
634 if (err != 0)
635 return (err);
636 err = dsl_dataset_own(dp, name, tag, &ds);
637 if (err != 0) {
638 dsl_pool_rele(dp, FTAG);
639 return (err);
641 err = dmu_objset_own_impl(ds, type, readonly, tag, osp);
642 dsl_pool_rele(dp, FTAG);
644 return (err);
648 dmu_objset_own_obj(dsl_pool_t *dp, uint64_t obj, dmu_objset_type_t type,
649 boolean_t readonly, void *tag, objset_t **osp)
651 dsl_dataset_t *ds;
652 int err;
654 err = dsl_dataset_own_obj(dp, obj, tag, &ds);
655 if (err != 0)
656 return (err);
658 return (dmu_objset_own_impl(ds, type, readonly, tag, osp));
661 void
662 dmu_objset_rele(objset_t *os, void *tag)
664 dsl_pool_t *dp = dmu_objset_pool(os);
665 dsl_dataset_rele(os->os_dsl_dataset, tag);
666 dsl_pool_rele(dp, tag);
670 * When we are called, os MUST refer to an objset associated with a dataset
671 * that is owned by 'tag'; that is, is held and long held by 'tag' and ds_owner
672 * == tag. We will then release and reacquire ownership of the dataset while
673 * holding the pool config_rwlock to avoid intervening namespace or ownership
674 * changes may occur.
676 * This exists solely to accommodate zfs_ioc_userspace_upgrade()'s desire to
677 * release the hold on its dataset and acquire a new one on the dataset of the
678 * same name so that it can be partially torn down and reconstructed.
680 void
681 dmu_objset_refresh_ownership(dsl_dataset_t *ds, dsl_dataset_t **newds,
682 void *tag)
684 dsl_pool_t *dp;
685 char name[ZFS_MAX_DATASET_NAME_LEN];
687 VERIFY3P(ds, !=, NULL);
688 VERIFY3P(ds->ds_owner, ==, tag);
689 VERIFY(dsl_dataset_long_held(ds));
691 dsl_dataset_name(ds, name);
692 dp = ds->ds_dir->dd_pool;
693 dsl_pool_config_enter(dp, FTAG);
694 dsl_dataset_disown(ds, tag);
695 VERIFY0(dsl_dataset_own(dp, name, tag, newds));
696 dsl_pool_config_exit(dp, FTAG);
699 void
700 dmu_objset_disown(objset_t *os, void *tag)
702 dsl_dataset_disown(os->os_dsl_dataset, tag);
705 void
706 dmu_objset_evict_dbufs(objset_t *os)
708 dnode_t dn_marker;
709 dnode_t *dn;
711 mutex_enter(&os->os_lock);
712 dn = list_head(&os->os_dnodes);
713 while (dn != NULL) {
715 * Skip dnodes without holds. We have to do this dance
716 * because dnode_add_ref() only works if there is already a
717 * hold. If the dnode has no holds, then it has no dbufs.
719 if (dnode_add_ref(dn, FTAG)) {
720 list_insert_after(&os->os_dnodes, dn, &dn_marker);
721 mutex_exit(&os->os_lock);
723 dnode_evict_dbufs(dn);
724 dnode_rele(dn, FTAG);
726 mutex_enter(&os->os_lock);
727 dn = list_next(&os->os_dnodes, &dn_marker);
728 list_remove(&os->os_dnodes, &dn_marker);
729 } else {
730 dn = list_next(&os->os_dnodes, dn);
733 mutex_exit(&os->os_lock);
735 if (DMU_USERUSED_DNODE(os) != NULL) {
736 dnode_evict_dbufs(DMU_GROUPUSED_DNODE(os));
737 dnode_evict_dbufs(DMU_USERUSED_DNODE(os));
739 dnode_evict_dbufs(DMU_META_DNODE(os));
743 * Objset eviction processing is split into into two pieces.
744 * The first marks the objset as evicting, evicts any dbufs that
745 * have a refcount of zero, and then queues up the objset for the
746 * second phase of eviction. Once os->os_dnodes has been cleared by
747 * dnode_buf_pageout()->dnode_destroy(), the second phase is executed.
748 * The second phase closes the special dnodes, dequeues the objset from
749 * the list of those undergoing eviction, and finally frees the objset.
751 * NOTE: Due to asynchronous eviction processing (invocation of
752 * dnode_buf_pageout()), it is possible for the meta dnode for the
753 * objset to have no holds even though os->os_dnodes is not empty.
755 void
756 dmu_objset_evict(objset_t *os)
758 dsl_dataset_t *ds = os->os_dsl_dataset;
760 for (int t = 0; t < TXG_SIZE; t++)
761 ASSERT(!dmu_objset_is_dirty(os, t));
763 if (ds)
764 dsl_prop_unregister_all(ds, os);
766 if (os->os_sa)
767 sa_tear_down(os);
769 dmu_objset_evict_dbufs(os);
771 mutex_enter(&os->os_lock);
772 spa_evicting_os_register(os->os_spa, os);
773 if (list_is_empty(&os->os_dnodes)) {
774 mutex_exit(&os->os_lock);
775 dmu_objset_evict_done(os);
776 } else {
777 mutex_exit(&os->os_lock);
781 void
782 dmu_objset_evict_done(objset_t *os)
784 ASSERT3P(list_head(&os->os_dnodes), ==, NULL);
786 dnode_special_close(&os->os_meta_dnode);
787 if (DMU_USERUSED_DNODE(os)) {
788 dnode_special_close(&os->os_userused_dnode);
789 dnode_special_close(&os->os_groupused_dnode);
791 zil_free(os->os_zil);
793 arc_buf_destroy(os->os_phys_buf, &os->os_phys_buf);
796 * This is a barrier to prevent the objset from going away in
797 * dnode_move() until we can safely ensure that the objset is still in
798 * use. We consider the objset valid before the barrier and invalid
799 * after the barrier.
801 rw_enter(&os_lock, RW_READER);
802 rw_exit(&os_lock);
804 mutex_destroy(&os->os_lock);
805 mutex_destroy(&os->os_userused_lock);
806 mutex_destroy(&os->os_obj_lock);
807 mutex_destroy(&os->os_user_ptr_lock);
808 for (int i = 0; i < TXG_SIZE; i++) {
809 multilist_destroy(os->os_dirty_dnodes[i]);
811 spa_evicting_os_deregister(os->os_spa, os);
812 kmem_free(os, sizeof (objset_t));
815 timestruc_t
816 dmu_objset_snap_cmtime(objset_t *os)
818 return (dsl_dir_snap_cmtime(os->os_dsl_dataset->ds_dir));
821 /* called from dsl for meta-objset */
822 objset_t *
823 dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
824 dmu_objset_type_t type, dmu_tx_t *tx)
826 objset_t *os;
827 dnode_t *mdn;
829 ASSERT(dmu_tx_is_syncing(tx));
831 if (ds != NULL)
832 VERIFY0(dmu_objset_from_ds(ds, &os));
833 else
834 VERIFY0(dmu_objset_open_impl(spa, NULL, bp, &os));
836 mdn = DMU_META_DNODE(os);
838 dnode_allocate(mdn, DMU_OT_DNODE, 1 << DNODE_BLOCK_SHIFT,
839 DN_MAX_INDBLKSHIFT, DMU_OT_NONE, 0, tx);
842 * We don't want to have to increase the meta-dnode's nlevels
843 * later, because then we could do it in quescing context while
844 * we are also accessing it in open context.
846 * This precaution is not necessary for the MOS (ds == NULL),
847 * because the MOS is only updated in syncing context.
848 * This is most fortunate: the MOS is the only objset that
849 * needs to be synced multiple times as spa_sync() iterates
850 * to convergence, so minimizing its dn_nlevels matters.
852 if (ds != NULL) {
853 int levels = 1;
856 * Determine the number of levels necessary for the meta-dnode
857 * to contain DN_MAX_OBJECT dnodes. Note that in order to
858 * ensure that we do not overflow 64 bits, there has to be
859 * a nlevels that gives us a number of blocks > DN_MAX_OBJECT
860 * but < 2^64. Therefore,
861 * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT) (10) must be
862 * less than (64 - log2(DN_MAX_OBJECT)) (16).
864 while ((uint64_t)mdn->dn_nblkptr <<
865 (mdn->dn_datablkshift - DNODE_SHIFT +
866 (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) <
867 DN_MAX_OBJECT)
868 levels++;
870 mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] =
871 mdn->dn_nlevels = levels;
874 ASSERT(type != DMU_OST_NONE);
875 ASSERT(type != DMU_OST_ANY);
876 ASSERT(type < DMU_OST_NUMTYPES);
877 os->os_phys->os_type = type;
878 if (dmu_objset_userused_enabled(os)) {
879 os->os_phys->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
880 os->os_flags = os->os_phys->os_flags;
883 dsl_dataset_dirty(ds, tx);
885 return (os);
888 typedef struct dmu_objset_create_arg {
889 const char *doca_name;
890 cred_t *doca_cred;
891 void (*doca_userfunc)(objset_t *os, void *arg,
892 cred_t *cr, dmu_tx_t *tx);
893 void *doca_userarg;
894 dmu_objset_type_t doca_type;
895 uint64_t doca_flags;
896 } dmu_objset_create_arg_t;
898 /*ARGSUSED*/
899 static int
900 dmu_objset_create_check(void *arg, dmu_tx_t *tx)
902 dmu_objset_create_arg_t *doca = arg;
903 dsl_pool_t *dp = dmu_tx_pool(tx);
904 dsl_dir_t *pdd;
905 const char *tail;
906 int error;
908 if (strchr(doca->doca_name, '@') != NULL)
909 return (SET_ERROR(EINVAL));
911 if (strlen(doca->doca_name) >= ZFS_MAX_DATASET_NAME_LEN)
912 return (SET_ERROR(ENAMETOOLONG));
914 error = dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail);
915 if (error != 0)
916 return (error);
917 if (tail == NULL) {
918 dsl_dir_rele(pdd, FTAG);
919 return (SET_ERROR(EEXIST));
921 error = dsl_fs_ss_limit_check(pdd, 1, ZFS_PROP_FILESYSTEM_LIMIT, NULL,
922 doca->doca_cred);
923 dsl_dir_rele(pdd, FTAG);
925 return (error);
928 static void
929 dmu_objset_create_sync(void *arg, dmu_tx_t *tx)
931 dmu_objset_create_arg_t *doca = arg;
932 dsl_pool_t *dp = dmu_tx_pool(tx);
933 dsl_dir_t *pdd;
934 const char *tail;
935 dsl_dataset_t *ds;
936 uint64_t obj;
937 blkptr_t *bp;
938 objset_t *os;
940 VERIFY0(dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail));
942 obj = dsl_dataset_create_sync(pdd, tail, NULL, doca->doca_flags,
943 doca->doca_cred, tx);
945 VERIFY0(dsl_dataset_hold_obj(pdd->dd_pool, obj, FTAG, &ds));
946 rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
947 bp = dsl_dataset_get_blkptr(ds);
948 os = dmu_objset_create_impl(pdd->dd_pool->dp_spa,
949 ds, bp, doca->doca_type, tx);
950 rrw_exit(&ds->ds_bp_rwlock, FTAG);
952 if (doca->doca_userfunc != NULL) {
953 doca->doca_userfunc(os, doca->doca_userarg,
954 doca->doca_cred, tx);
957 spa_history_log_internal_ds(ds, "create", tx, "");
958 dsl_dataset_rele(ds, FTAG);
959 dsl_dir_rele(pdd, FTAG);
963 dmu_objset_create(const char *name, dmu_objset_type_t type, uint64_t flags,
964 void (*func)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx), void *arg)
966 dmu_objset_create_arg_t doca;
968 doca.doca_name = name;
969 doca.doca_cred = CRED();
970 doca.doca_flags = flags;
971 doca.doca_userfunc = func;
972 doca.doca_userarg = arg;
973 doca.doca_type = type;
975 return (dsl_sync_task(name,
976 dmu_objset_create_check, dmu_objset_create_sync, &doca,
977 5, ZFS_SPACE_CHECK_NORMAL));
980 typedef struct dmu_objset_clone_arg {
981 const char *doca_clone;
982 const char *doca_origin;
983 cred_t *doca_cred;
984 } dmu_objset_clone_arg_t;
986 /*ARGSUSED*/
987 static int
988 dmu_objset_clone_check(void *arg, dmu_tx_t *tx)
990 dmu_objset_clone_arg_t *doca = arg;
991 dsl_dir_t *pdd;
992 const char *tail;
993 int error;
994 dsl_dataset_t *origin;
995 dsl_pool_t *dp = dmu_tx_pool(tx);
997 if (strchr(doca->doca_clone, '@') != NULL)
998 return (SET_ERROR(EINVAL));
1000 if (strlen(doca->doca_clone) >= ZFS_MAX_DATASET_NAME_LEN)
1001 return (SET_ERROR(ENAMETOOLONG));
1003 error = dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail);
1004 if (error != 0)
1005 return (error);
1006 if (tail == NULL) {
1007 dsl_dir_rele(pdd, FTAG);
1008 return (SET_ERROR(EEXIST));
1011 error = dsl_fs_ss_limit_check(pdd, 1, ZFS_PROP_FILESYSTEM_LIMIT, NULL,
1012 doca->doca_cred);
1013 if (error != 0) {
1014 dsl_dir_rele(pdd, FTAG);
1015 return (SET_ERROR(EDQUOT));
1017 dsl_dir_rele(pdd, FTAG);
1019 error = dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin);
1020 if (error != 0)
1021 return (error);
1023 /* You can only clone snapshots, not the head datasets. */
1024 if (!origin->ds_is_snapshot) {
1025 dsl_dataset_rele(origin, FTAG);
1026 return (SET_ERROR(EINVAL));
1028 dsl_dataset_rele(origin, FTAG);
1030 return (0);
1033 static void
1034 dmu_objset_clone_sync(void *arg, dmu_tx_t *tx)
1036 dmu_objset_clone_arg_t *doca = arg;
1037 dsl_pool_t *dp = dmu_tx_pool(tx);
1038 dsl_dir_t *pdd;
1039 const char *tail;
1040 dsl_dataset_t *origin, *ds;
1041 uint64_t obj;
1042 char namebuf[ZFS_MAX_DATASET_NAME_LEN];
1044 VERIFY0(dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail));
1045 VERIFY0(dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin));
1047 obj = dsl_dataset_create_sync(pdd, tail, origin, 0,
1048 doca->doca_cred, tx);
1050 VERIFY0(dsl_dataset_hold_obj(pdd->dd_pool, obj, FTAG, &ds));
1051 dsl_dataset_name(origin, namebuf);
1052 spa_history_log_internal_ds(ds, "clone", tx,
1053 "origin=%s (%llu)", namebuf, origin->ds_object);
1054 dsl_dataset_rele(ds, FTAG);
1055 dsl_dataset_rele(origin, FTAG);
1056 dsl_dir_rele(pdd, FTAG);
1060 dmu_objset_clone(const char *clone, const char *origin)
1062 dmu_objset_clone_arg_t doca;
1064 doca.doca_clone = clone;
1065 doca.doca_origin = origin;
1066 doca.doca_cred = CRED();
1068 return (dsl_sync_task(clone,
1069 dmu_objset_clone_check, dmu_objset_clone_sync, &doca,
1070 5, ZFS_SPACE_CHECK_NORMAL));
1073 static int
1074 dmu_objset_remap_indirects_impl(objset_t *os, uint64_t last_removed_txg)
1076 int error = 0;
1077 uint64_t object = 0;
1078 while ((error = dmu_object_next(os, &object, B_FALSE, 0)) == 0) {
1079 error = dmu_object_remap_indirects(os, object,
1080 last_removed_txg);
1082 * If the ZPL removed the object before we managed to dnode_hold
1083 * it, we would get an ENOENT. If the ZPL declares its intent
1084 * to remove the object (dnode_free) before we manage to
1085 * dnode_hold it, we would get an EEXIST. In either case, we
1086 * want to continue remapping the other objects in the objset;
1087 * in all other cases, we want to break early.
1089 if (error != 0 && error != ENOENT && error != EEXIST) {
1090 break;
1093 if (error == ESRCH) {
1094 error = 0;
1096 return (error);
1100 dmu_objset_remap_indirects(const char *fsname)
1102 int error = 0;
1103 objset_t *os = NULL;
1104 uint64_t last_removed_txg;
1105 uint64_t remap_start_txg;
1106 dsl_dir_t *dd;
1108 error = dmu_objset_hold(fsname, FTAG, &os);
1109 if (error != 0) {
1110 return (error);
1112 dd = dmu_objset_ds(os)->ds_dir;
1114 if (!spa_feature_is_enabled(dmu_objset_spa(os),
1115 SPA_FEATURE_OBSOLETE_COUNTS)) {
1116 dmu_objset_rele(os, FTAG);
1117 return (SET_ERROR(ENOTSUP));
1120 if (dsl_dataset_is_snapshot(dmu_objset_ds(os))) {
1121 dmu_objset_rele(os, FTAG);
1122 return (SET_ERROR(EINVAL));
1126 * If there has not been a removal, we're done.
1128 last_removed_txg = spa_get_last_removal_txg(dmu_objset_spa(os));
1129 if (last_removed_txg == -1ULL) {
1130 dmu_objset_rele(os, FTAG);
1131 return (0);
1135 * If we have remapped since the last removal, we're done.
1137 if (dsl_dir_is_zapified(dd)) {
1138 uint64_t last_remap_txg;
1139 if (zap_lookup(spa_meta_objset(dmu_objset_spa(os)),
1140 dd->dd_object, DD_FIELD_LAST_REMAP_TXG,
1141 sizeof (last_remap_txg), 1, &last_remap_txg) == 0 &&
1142 last_remap_txg > last_removed_txg) {
1143 dmu_objset_rele(os, FTAG);
1144 return (0);
1148 dsl_dataset_long_hold(dmu_objset_ds(os), FTAG);
1149 dsl_pool_rele(dmu_objset_pool(os), FTAG);
1151 remap_start_txg = spa_last_synced_txg(dmu_objset_spa(os));
1152 error = dmu_objset_remap_indirects_impl(os, last_removed_txg);
1153 if (error == 0) {
1155 * We update the last_remap_txg to be the start txg so that
1156 * we can guarantee that every block older than last_remap_txg
1157 * that can be remapped has been remapped.
1159 error = dsl_dir_update_last_remap_txg(dd, remap_start_txg);
1162 dsl_dataset_long_rele(dmu_objset_ds(os), FTAG);
1163 dsl_dataset_rele(dmu_objset_ds(os), FTAG);
1165 return (error);
1169 dmu_objset_snapshot_one(const char *fsname, const char *snapname)
1171 int err;
1172 char *longsnap = kmem_asprintf("%s@%s", fsname, snapname);
1173 nvlist_t *snaps = fnvlist_alloc();
1175 fnvlist_add_boolean(snaps, longsnap);
1176 strfree(longsnap);
1177 err = dsl_dataset_snapshot(snaps, NULL, NULL);
1178 fnvlist_free(snaps);
1179 return (err);
1182 static void
1183 dmu_objset_sync_dnodes(multilist_sublist_t *list, dmu_tx_t *tx)
1185 dnode_t *dn;
1187 while ((dn = multilist_sublist_head(list)) != NULL) {
1188 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
1189 ASSERT(dn->dn_dbuf->db_data_pending);
1191 * Initialize dn_zio outside dnode_sync() because the
1192 * meta-dnode needs to set it ouside dnode_sync().
1194 dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio;
1195 ASSERT(dn->dn_zio);
1197 ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS);
1198 multilist_sublist_remove(list, dn);
1200 multilist_t *newlist = dn->dn_objset->os_synced_dnodes;
1201 if (newlist != NULL) {
1202 (void) dnode_add_ref(dn, newlist);
1203 multilist_insert(newlist, dn);
1206 dnode_sync(dn, tx);
1210 /* ARGSUSED */
1211 static void
1212 dmu_objset_write_ready(zio_t *zio, arc_buf_t *abuf, void *arg)
1214 blkptr_t *bp = zio->io_bp;
1215 objset_t *os = arg;
1216 dnode_phys_t *dnp = &os->os_phys->os_meta_dnode;
1218 ASSERT(!BP_IS_EMBEDDED(bp));
1219 ASSERT3U(BP_GET_TYPE(bp), ==, DMU_OT_OBJSET);
1220 ASSERT0(BP_GET_LEVEL(bp));
1223 * Update rootbp fill count: it should be the number of objects
1224 * allocated in the object set (not counting the "special"
1225 * objects that are stored in the objset_phys_t -- the meta
1226 * dnode and user/group accounting objects).
1228 bp->blk_fill = 0;
1229 for (int i = 0; i < dnp->dn_nblkptr; i++)
1230 bp->blk_fill += BP_GET_FILL(&dnp->dn_blkptr[i]);
1231 if (os->os_dsl_dataset != NULL)
1232 rrw_enter(&os->os_dsl_dataset->ds_bp_rwlock, RW_WRITER, FTAG);
1233 *os->os_rootbp = *bp;
1234 if (os->os_dsl_dataset != NULL)
1235 rrw_exit(&os->os_dsl_dataset->ds_bp_rwlock, FTAG);
1238 /* ARGSUSED */
1239 static void
1240 dmu_objset_write_done(zio_t *zio, arc_buf_t *abuf, void *arg)
1242 blkptr_t *bp = zio->io_bp;
1243 blkptr_t *bp_orig = &zio->io_bp_orig;
1244 objset_t *os = arg;
1246 if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
1247 ASSERT(BP_EQUAL(bp, bp_orig));
1248 } else {
1249 dsl_dataset_t *ds = os->os_dsl_dataset;
1250 dmu_tx_t *tx = os->os_synctx;
1252 (void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
1253 dsl_dataset_block_born(ds, bp, tx);
1255 kmem_free(bp, sizeof (*bp));
1258 typedef struct sync_dnodes_arg {
1259 multilist_t *sda_list;
1260 int sda_sublist_idx;
1261 multilist_t *sda_newlist;
1262 dmu_tx_t *sda_tx;
1263 } sync_dnodes_arg_t;
1265 static void
1266 sync_dnodes_task(void *arg)
1268 sync_dnodes_arg_t *sda = arg;
1270 multilist_sublist_t *ms =
1271 multilist_sublist_lock(sda->sda_list, sda->sda_sublist_idx);
1273 dmu_objset_sync_dnodes(ms, sda->sda_tx);
1275 multilist_sublist_unlock(ms);
1277 kmem_free(sda, sizeof (*sda));
1281 /* called from dsl */
1282 void
1283 dmu_objset_sync(objset_t *os, zio_t *pio, dmu_tx_t *tx)
1285 int txgoff;
1286 zbookmark_phys_t zb;
1287 zio_prop_t zp;
1288 zio_t *zio;
1289 list_t *list;
1290 dbuf_dirty_record_t *dr;
1291 blkptr_t *blkptr_copy = kmem_alloc(sizeof (*os->os_rootbp), KM_SLEEP);
1292 *blkptr_copy = *os->os_rootbp;
1294 dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg);
1296 ASSERT(dmu_tx_is_syncing(tx));
1297 /* XXX the write_done callback should really give us the tx... */
1298 os->os_synctx = tx;
1300 if (os->os_dsl_dataset == NULL) {
1302 * This is the MOS. If we have upgraded,
1303 * spa_max_replication() could change, so reset
1304 * os_copies here.
1306 os->os_copies = spa_max_replication(os->os_spa);
1310 * Create the root block IO
1312 SET_BOOKMARK(&zb, os->os_dsl_dataset ?
1313 os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
1314 ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
1315 arc_release(os->os_phys_buf, &os->os_phys_buf);
1317 dmu_write_policy(os, NULL, 0, 0, &zp);
1319 zio = arc_write(pio, os->os_spa, tx->tx_txg,
1320 blkptr_copy, os->os_phys_buf, DMU_OS_IS_L2CACHEABLE(os),
1321 &zp, dmu_objset_write_ready, NULL, NULL, dmu_objset_write_done,
1322 os, ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
1325 * Sync special dnodes - the parent IO for the sync is the root block
1327 DMU_META_DNODE(os)->dn_zio = zio;
1328 dnode_sync(DMU_META_DNODE(os), tx);
1330 os->os_phys->os_flags = os->os_flags;
1332 if (DMU_USERUSED_DNODE(os) &&
1333 DMU_USERUSED_DNODE(os)->dn_type != DMU_OT_NONE) {
1334 DMU_USERUSED_DNODE(os)->dn_zio = zio;
1335 dnode_sync(DMU_USERUSED_DNODE(os), tx);
1336 DMU_GROUPUSED_DNODE(os)->dn_zio = zio;
1337 dnode_sync(DMU_GROUPUSED_DNODE(os), tx);
1340 txgoff = tx->tx_txg & TXG_MASK;
1342 if (dmu_objset_userused_enabled(os)) {
1344 * We must create the list here because it uses the
1345 * dn_dirty_link[] of this txg. But it may already
1346 * exist because we call dsl_dataset_sync() twice per txg.
1348 if (os->os_synced_dnodes == NULL) {
1349 os->os_synced_dnodes =
1350 multilist_create(sizeof (dnode_t),
1351 offsetof(dnode_t, dn_dirty_link[txgoff]),
1352 dnode_multilist_index_func);
1353 } else {
1354 ASSERT3U(os->os_synced_dnodes->ml_offset, ==,
1355 offsetof(dnode_t, dn_dirty_link[txgoff]));
1359 for (int i = 0;
1360 i < multilist_get_num_sublists(os->os_dirty_dnodes[txgoff]); i++) {
1361 sync_dnodes_arg_t *sda = kmem_alloc(sizeof (*sda), KM_SLEEP);
1362 sda->sda_list = os->os_dirty_dnodes[txgoff];
1363 sda->sda_sublist_idx = i;
1364 sda->sda_tx = tx;
1365 (void) taskq_dispatch(dmu_objset_pool(os)->dp_sync_taskq,
1366 sync_dnodes_task, sda, 0);
1367 /* callback frees sda */
1369 taskq_wait(dmu_objset_pool(os)->dp_sync_taskq);
1371 list = &DMU_META_DNODE(os)->dn_dirty_records[txgoff];
1372 while ((dr = list_head(list)) != NULL) {
1373 ASSERT0(dr->dr_dbuf->db_level);
1374 list_remove(list, dr);
1375 if (dr->dr_zio)
1376 zio_nowait(dr->dr_zio);
1379 /* Enable dnode backfill if enough objects have been freed. */
1380 if (os->os_freed_dnodes >= dmu_rescan_dnode_threshold) {
1381 os->os_rescan_dnodes = B_TRUE;
1382 os->os_freed_dnodes = 0;
1386 * Free intent log blocks up to this tx.
1388 zil_sync(os->os_zil, tx);
1389 os->os_phys->os_zil_header = os->os_zil_header;
1390 zio_nowait(zio);
1393 boolean_t
1394 dmu_objset_is_dirty(objset_t *os, uint64_t txg)
1396 return (!multilist_is_empty(os->os_dirty_dnodes[txg & TXG_MASK]));
1399 static objset_used_cb_t *used_cbs[DMU_OST_NUMTYPES];
1401 void
1402 dmu_objset_register_type(dmu_objset_type_t ost, objset_used_cb_t *cb)
1404 used_cbs[ost] = cb;
1407 boolean_t
1408 dmu_objset_userused_enabled(objset_t *os)
1410 return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE &&
1411 used_cbs[os->os_phys->os_type] != NULL &&
1412 DMU_USERUSED_DNODE(os) != NULL);
1415 typedef struct userquota_node {
1416 uint64_t uqn_id;
1417 int64_t uqn_delta;
1418 avl_node_t uqn_node;
1419 } userquota_node_t;
1421 typedef struct userquota_cache {
1422 avl_tree_t uqc_user_deltas;
1423 avl_tree_t uqc_group_deltas;
1424 } userquota_cache_t;
1426 static int
1427 userquota_compare(const void *l, const void *r)
1429 const userquota_node_t *luqn = l;
1430 const userquota_node_t *ruqn = r;
1432 if (luqn->uqn_id < ruqn->uqn_id)
1433 return (-1);
1434 if (luqn->uqn_id > ruqn->uqn_id)
1435 return (1);
1436 return (0);
1439 static void
1440 do_userquota_cacheflush(objset_t *os, userquota_cache_t *cache, dmu_tx_t *tx)
1442 void *cookie;
1443 userquota_node_t *uqn;
1445 ASSERT(dmu_tx_is_syncing(tx));
1447 cookie = NULL;
1448 while ((uqn = avl_destroy_nodes(&cache->uqc_user_deltas,
1449 &cookie)) != NULL) {
1451 * os_userused_lock protects against concurrent calls to
1452 * zap_increment_int(). It's needed because zap_increment_int()
1453 * is not thread-safe (i.e. not atomic).
1455 mutex_enter(&os->os_userused_lock);
1456 VERIFY0(zap_increment_int(os, DMU_USERUSED_OBJECT,
1457 uqn->uqn_id, uqn->uqn_delta, tx));
1458 mutex_exit(&os->os_userused_lock);
1459 kmem_free(uqn, sizeof (*uqn));
1461 avl_destroy(&cache->uqc_user_deltas);
1463 cookie = NULL;
1464 while ((uqn = avl_destroy_nodes(&cache->uqc_group_deltas,
1465 &cookie)) != NULL) {
1466 mutex_enter(&os->os_userused_lock);
1467 VERIFY0(zap_increment_int(os, DMU_GROUPUSED_OBJECT,
1468 uqn->uqn_id, uqn->uqn_delta, tx));
1469 mutex_exit(&os->os_userused_lock);
1470 kmem_free(uqn, sizeof (*uqn));
1472 avl_destroy(&cache->uqc_group_deltas);
1475 static void
1476 userquota_update_cache(avl_tree_t *avl, uint64_t id, int64_t delta)
1478 userquota_node_t search = { .uqn_id = id };
1479 avl_index_t idx;
1481 userquota_node_t *uqn = avl_find(avl, &search, &idx);
1482 if (uqn == NULL) {
1483 uqn = kmem_zalloc(sizeof (*uqn), KM_SLEEP);
1484 uqn->uqn_id = id;
1485 avl_insert(avl, uqn, idx);
1487 uqn->uqn_delta += delta;
1490 static void
1491 do_userquota_update(userquota_cache_t *cache, uint64_t used, uint64_t flags,
1492 uint64_t user, uint64_t group, boolean_t subtract)
1494 if ((flags & DNODE_FLAG_USERUSED_ACCOUNTED)) {
1495 int64_t delta = DNODE_SIZE + used;
1496 if (subtract)
1497 delta = -delta;
1499 userquota_update_cache(&cache->uqc_user_deltas, user, delta);
1500 userquota_update_cache(&cache->uqc_group_deltas, group, delta);
1504 typedef struct userquota_updates_arg {
1505 objset_t *uua_os;
1506 int uua_sublist_idx;
1507 dmu_tx_t *uua_tx;
1508 } userquota_updates_arg_t;
1510 static void
1511 userquota_updates_task(void *arg)
1513 userquota_updates_arg_t *uua = arg;
1514 objset_t *os = uua->uua_os;
1515 dmu_tx_t *tx = uua->uua_tx;
1516 dnode_t *dn;
1517 userquota_cache_t cache = { 0 };
1519 multilist_sublist_t *list =
1520 multilist_sublist_lock(os->os_synced_dnodes, uua->uua_sublist_idx);
1522 ASSERT(multilist_sublist_head(list) == NULL ||
1523 dmu_objset_userused_enabled(os));
1524 avl_create(&cache.uqc_user_deltas, userquota_compare,
1525 sizeof (userquota_node_t), offsetof(userquota_node_t, uqn_node));
1526 avl_create(&cache.uqc_group_deltas, userquota_compare,
1527 sizeof (userquota_node_t), offsetof(userquota_node_t, uqn_node));
1529 while ((dn = multilist_sublist_head(list)) != NULL) {
1530 int flags;
1531 ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object));
1532 ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE ||
1533 dn->dn_phys->dn_flags &
1534 DNODE_FLAG_USERUSED_ACCOUNTED);
1536 flags = dn->dn_id_flags;
1537 ASSERT(flags);
1538 if (flags & DN_ID_OLD_EXIST) {
1539 do_userquota_update(&cache,
1540 dn->dn_oldused, dn->dn_oldflags,
1541 dn->dn_olduid, dn->dn_oldgid, B_TRUE);
1543 if (flags & DN_ID_NEW_EXIST) {
1544 do_userquota_update(&cache,
1545 DN_USED_BYTES(dn->dn_phys),
1546 dn->dn_phys->dn_flags, dn->dn_newuid,
1547 dn->dn_newgid, B_FALSE);
1550 mutex_enter(&dn->dn_mtx);
1551 dn->dn_oldused = 0;
1552 dn->dn_oldflags = 0;
1553 if (dn->dn_id_flags & DN_ID_NEW_EXIST) {
1554 dn->dn_olduid = dn->dn_newuid;
1555 dn->dn_oldgid = dn->dn_newgid;
1556 dn->dn_id_flags |= DN_ID_OLD_EXIST;
1557 if (dn->dn_bonuslen == 0)
1558 dn->dn_id_flags |= DN_ID_CHKED_SPILL;
1559 else
1560 dn->dn_id_flags |= DN_ID_CHKED_BONUS;
1562 dn->dn_id_flags &= ~(DN_ID_NEW_EXIST);
1563 mutex_exit(&dn->dn_mtx);
1565 multilist_sublist_remove(list, dn);
1566 dnode_rele(dn, os->os_synced_dnodes);
1568 do_userquota_cacheflush(os, &cache, tx);
1569 multilist_sublist_unlock(list);
1570 kmem_free(uua, sizeof (*uua));
1573 void
1574 dmu_objset_do_userquota_updates(objset_t *os, dmu_tx_t *tx)
1576 if (!dmu_objset_userused_enabled(os))
1577 return;
1579 /* Allocate the user/groupused objects if necessary. */
1580 if (DMU_USERUSED_DNODE(os)->dn_type == DMU_OT_NONE) {
1581 VERIFY0(zap_create_claim(os,
1582 DMU_USERUSED_OBJECT,
1583 DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
1584 VERIFY0(zap_create_claim(os,
1585 DMU_GROUPUSED_OBJECT,
1586 DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
1589 for (int i = 0;
1590 i < multilist_get_num_sublists(os->os_synced_dnodes); i++) {
1591 userquota_updates_arg_t *uua =
1592 kmem_alloc(sizeof (*uua), KM_SLEEP);
1593 uua->uua_os = os;
1594 uua->uua_sublist_idx = i;
1595 uua->uua_tx = tx;
1596 /* note: caller does taskq_wait() */
1597 (void) taskq_dispatch(dmu_objset_pool(os)->dp_sync_taskq,
1598 userquota_updates_task, uua, 0);
1599 /* callback frees uua */
1604 * Returns a pointer to data to find uid/gid from
1606 * If a dirty record for transaction group that is syncing can't
1607 * be found then NULL is returned. In the NULL case it is assumed
1608 * the uid/gid aren't changing.
1610 static void *
1611 dmu_objset_userquota_find_data(dmu_buf_impl_t *db, dmu_tx_t *tx)
1613 dbuf_dirty_record_t *dr, **drp;
1614 void *data;
1616 if (db->db_dirtycnt == 0)
1617 return (db->db.db_data); /* Nothing is changing */
1619 for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next)
1620 if (dr->dr_txg == tx->tx_txg)
1621 break;
1623 if (dr == NULL) {
1624 data = NULL;
1625 } else {
1626 dnode_t *dn;
1628 DB_DNODE_ENTER(dr->dr_dbuf);
1629 dn = DB_DNODE(dr->dr_dbuf);
1631 if (dn->dn_bonuslen == 0 &&
1632 dr->dr_dbuf->db_blkid == DMU_SPILL_BLKID)
1633 data = dr->dt.dl.dr_data->b_data;
1634 else
1635 data = dr->dt.dl.dr_data;
1637 DB_DNODE_EXIT(dr->dr_dbuf);
1640 return (data);
1643 void
1644 dmu_objset_userquota_get_ids(dnode_t *dn, boolean_t before, dmu_tx_t *tx)
1646 objset_t *os = dn->dn_objset;
1647 void *data = NULL;
1648 dmu_buf_impl_t *db = NULL;
1649 uint64_t *user = NULL;
1650 uint64_t *group = NULL;
1651 int flags = dn->dn_id_flags;
1652 int error;
1653 boolean_t have_spill = B_FALSE;
1655 if (!dmu_objset_userused_enabled(dn->dn_objset))
1656 return;
1658 if (before && (flags & (DN_ID_CHKED_BONUS|DN_ID_OLD_EXIST|
1659 DN_ID_CHKED_SPILL)))
1660 return;
1662 if (before && dn->dn_bonuslen != 0)
1663 data = DN_BONUS(dn->dn_phys);
1664 else if (!before && dn->dn_bonuslen != 0) {
1665 if (dn->dn_bonus) {
1666 db = dn->dn_bonus;
1667 mutex_enter(&db->db_mtx);
1668 data = dmu_objset_userquota_find_data(db, tx);
1669 } else {
1670 data = DN_BONUS(dn->dn_phys);
1672 } else if (dn->dn_bonuslen == 0 && dn->dn_bonustype == DMU_OT_SA) {
1673 int rf = 0;
1675 if (RW_WRITE_HELD(&dn->dn_struct_rwlock))
1676 rf |= DB_RF_HAVESTRUCT;
1677 error = dmu_spill_hold_by_dnode(dn,
1678 rf | DB_RF_MUST_SUCCEED,
1679 FTAG, (dmu_buf_t **)&db);
1680 ASSERT(error == 0);
1681 mutex_enter(&db->db_mtx);
1682 data = (before) ? db->db.db_data :
1683 dmu_objset_userquota_find_data(db, tx);
1684 have_spill = B_TRUE;
1685 } else {
1686 mutex_enter(&dn->dn_mtx);
1687 dn->dn_id_flags |= DN_ID_CHKED_BONUS;
1688 mutex_exit(&dn->dn_mtx);
1689 return;
1692 if (before) {
1693 ASSERT(data);
1694 user = &dn->dn_olduid;
1695 group = &dn->dn_oldgid;
1696 } else if (data) {
1697 user = &dn->dn_newuid;
1698 group = &dn->dn_newgid;
1702 * Must always call the callback in case the object
1703 * type has changed and that type isn't an object type to track
1705 error = used_cbs[os->os_phys->os_type](dn->dn_bonustype, data,
1706 user, group);
1709 * Preserve existing uid/gid when the callback can't determine
1710 * what the new uid/gid are and the callback returned EEXIST.
1711 * The EEXIST error tells us to just use the existing uid/gid.
1712 * If we don't know what the old values are then just assign
1713 * them to 0, since that is a new file being created.
1715 if (!before && data == NULL && error == EEXIST) {
1716 if (flags & DN_ID_OLD_EXIST) {
1717 dn->dn_newuid = dn->dn_olduid;
1718 dn->dn_newgid = dn->dn_oldgid;
1719 } else {
1720 dn->dn_newuid = 0;
1721 dn->dn_newgid = 0;
1723 error = 0;
1726 if (db)
1727 mutex_exit(&db->db_mtx);
1729 mutex_enter(&dn->dn_mtx);
1730 if (error == 0 && before)
1731 dn->dn_id_flags |= DN_ID_OLD_EXIST;
1732 if (error == 0 && !before)
1733 dn->dn_id_flags |= DN_ID_NEW_EXIST;
1735 if (have_spill) {
1736 dn->dn_id_flags |= DN_ID_CHKED_SPILL;
1737 } else {
1738 dn->dn_id_flags |= DN_ID_CHKED_BONUS;
1740 mutex_exit(&dn->dn_mtx);
1741 if (have_spill)
1742 dmu_buf_rele((dmu_buf_t *)db, FTAG);
1745 boolean_t
1746 dmu_objset_userspace_present(objset_t *os)
1748 return (os->os_phys->os_flags &
1749 OBJSET_FLAG_USERACCOUNTING_COMPLETE);
1753 dmu_objset_userspace_upgrade(objset_t *os)
1755 uint64_t obj;
1756 int err = 0;
1758 if (dmu_objset_userspace_present(os))
1759 return (0);
1760 if (!dmu_objset_userused_enabled(os))
1761 return (SET_ERROR(ENOTSUP));
1762 if (dmu_objset_is_snapshot(os))
1763 return (SET_ERROR(EINVAL));
1766 * We simply need to mark every object dirty, so that it will be
1767 * synced out and now accounted. If this is called
1768 * concurrently, or if we already did some work before crashing,
1769 * that's fine, since we track each object's accounted state
1770 * independently.
1773 for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
1774 dmu_tx_t *tx;
1775 dmu_buf_t *db;
1776 int objerr;
1778 if (issig(JUSTLOOKING) && issig(FORREAL))
1779 return (SET_ERROR(EINTR));
1781 objerr = dmu_bonus_hold(os, obj, FTAG, &db);
1782 if (objerr != 0)
1783 continue;
1784 tx = dmu_tx_create(os);
1785 dmu_tx_hold_bonus(tx, obj);
1786 objerr = dmu_tx_assign(tx, TXG_WAIT);
1787 if (objerr != 0) {
1788 dmu_tx_abort(tx);
1789 continue;
1791 dmu_buf_will_dirty(db, tx);
1792 dmu_buf_rele(db, FTAG);
1793 dmu_tx_commit(tx);
1796 os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
1797 txg_wait_synced(dmu_objset_pool(os), 0);
1798 return (0);
1801 void
1802 dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp,
1803 uint64_t *usedobjsp, uint64_t *availobjsp)
1805 dsl_dataset_space(os->os_dsl_dataset, refdbytesp, availbytesp,
1806 usedobjsp, availobjsp);
1809 uint64_t
1810 dmu_objset_fsid_guid(objset_t *os)
1812 return (dsl_dataset_fsid_guid(os->os_dsl_dataset));
1815 void
1816 dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat)
1818 stat->dds_type = os->os_phys->os_type;
1819 if (os->os_dsl_dataset)
1820 dsl_dataset_fast_stat(os->os_dsl_dataset, stat);
1823 void
1824 dmu_objset_stats(objset_t *os, nvlist_t *nv)
1826 ASSERT(os->os_dsl_dataset ||
1827 os->os_phys->os_type == DMU_OST_META);
1829 if (os->os_dsl_dataset != NULL)
1830 dsl_dataset_stats(os->os_dsl_dataset, nv);
1832 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE,
1833 os->os_phys->os_type);
1834 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERACCOUNTING,
1835 dmu_objset_userspace_present(os));
1839 dmu_objset_is_snapshot(objset_t *os)
1841 if (os->os_dsl_dataset != NULL)
1842 return (os->os_dsl_dataset->ds_is_snapshot);
1843 else
1844 return (B_FALSE);
1848 dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen,
1849 boolean_t *conflict)
1851 dsl_dataset_t *ds = os->os_dsl_dataset;
1852 uint64_t ignored;
1854 if (dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0)
1855 return (SET_ERROR(ENOENT));
1857 return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset,
1858 dsl_dataset_phys(ds)->ds_snapnames_zapobj, name, 8, 1, &ignored,
1859 MT_NORMALIZE, real, maxlen, conflict));
1863 dmu_snapshot_list_next(objset_t *os, int namelen, char *name,
1864 uint64_t *idp, uint64_t *offp, boolean_t *case_conflict)
1866 dsl_dataset_t *ds = os->os_dsl_dataset;
1867 zap_cursor_t cursor;
1868 zap_attribute_t attr;
1870 ASSERT(dsl_pool_config_held(dmu_objset_pool(os)));
1872 if (dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0)
1873 return (SET_ERROR(ENOENT));
1875 zap_cursor_init_serialized(&cursor,
1876 ds->ds_dir->dd_pool->dp_meta_objset,
1877 dsl_dataset_phys(ds)->ds_snapnames_zapobj, *offp);
1879 if (zap_cursor_retrieve(&cursor, &attr) != 0) {
1880 zap_cursor_fini(&cursor);
1881 return (SET_ERROR(ENOENT));
1884 if (strlen(attr.za_name) + 1 > namelen) {
1885 zap_cursor_fini(&cursor);
1886 return (SET_ERROR(ENAMETOOLONG));
1889 (void) strcpy(name, attr.za_name);
1890 if (idp)
1891 *idp = attr.za_first_integer;
1892 if (case_conflict)
1893 *case_conflict = attr.za_normalization_conflict;
1894 zap_cursor_advance(&cursor);
1895 *offp = zap_cursor_serialize(&cursor);
1896 zap_cursor_fini(&cursor);
1898 return (0);
1902 dmu_dir_list_next(objset_t *os, int namelen, char *name,
1903 uint64_t *idp, uint64_t *offp)
1905 dsl_dir_t *dd = os->os_dsl_dataset->ds_dir;
1906 zap_cursor_t cursor;
1907 zap_attribute_t attr;
1909 /* there is no next dir on a snapshot! */
1910 if (os->os_dsl_dataset->ds_object !=
1911 dsl_dir_phys(dd)->dd_head_dataset_obj)
1912 return (SET_ERROR(ENOENT));
1914 zap_cursor_init_serialized(&cursor,
1915 dd->dd_pool->dp_meta_objset,
1916 dsl_dir_phys(dd)->dd_child_dir_zapobj, *offp);
1918 if (zap_cursor_retrieve(&cursor, &attr) != 0) {
1919 zap_cursor_fini(&cursor);
1920 return (SET_ERROR(ENOENT));
1923 if (strlen(attr.za_name) + 1 > namelen) {
1924 zap_cursor_fini(&cursor);
1925 return (SET_ERROR(ENAMETOOLONG));
1928 (void) strcpy(name, attr.za_name);
1929 if (idp)
1930 *idp = attr.za_first_integer;
1931 zap_cursor_advance(&cursor);
1932 *offp = zap_cursor_serialize(&cursor);
1933 zap_cursor_fini(&cursor);
1935 return (0);
1938 typedef struct dmu_objset_find_ctx {
1939 taskq_t *dc_tq;
1940 dsl_pool_t *dc_dp;
1941 uint64_t dc_ddobj;
1942 char *dc_ddname; /* last component of ddobj's name */
1943 int (*dc_func)(dsl_pool_t *, dsl_dataset_t *, void *);
1944 void *dc_arg;
1945 int dc_flags;
1946 kmutex_t *dc_error_lock;
1947 int *dc_error;
1948 } dmu_objset_find_ctx_t;
1950 static void
1951 dmu_objset_find_dp_impl(dmu_objset_find_ctx_t *dcp)
1953 dsl_pool_t *dp = dcp->dc_dp;
1954 dsl_dir_t *dd;
1955 dsl_dataset_t *ds;
1956 zap_cursor_t zc;
1957 zap_attribute_t *attr;
1958 uint64_t thisobj;
1959 int err = 0;
1961 /* don't process if there already was an error */
1962 if (*dcp->dc_error != 0)
1963 goto out;
1966 * Note: passing the name (dc_ddname) here is optional, but it
1967 * improves performance because we don't need to call
1968 * zap_value_search() to determine the name.
1970 err = dsl_dir_hold_obj(dp, dcp->dc_ddobj, dcp->dc_ddname, FTAG, &dd);
1971 if (err != 0)
1972 goto out;
1974 /* Don't visit hidden ($MOS & $ORIGIN) objsets. */
1975 if (dd->dd_myname[0] == '$') {
1976 dsl_dir_rele(dd, FTAG);
1977 goto out;
1980 thisobj = dsl_dir_phys(dd)->dd_head_dataset_obj;
1981 attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
1984 * Iterate over all children.
1986 if (dcp->dc_flags & DS_FIND_CHILDREN) {
1987 for (zap_cursor_init(&zc, dp->dp_meta_objset,
1988 dsl_dir_phys(dd)->dd_child_dir_zapobj);
1989 zap_cursor_retrieve(&zc, attr) == 0;
1990 (void) zap_cursor_advance(&zc)) {
1991 ASSERT3U(attr->za_integer_length, ==,
1992 sizeof (uint64_t));
1993 ASSERT3U(attr->za_num_integers, ==, 1);
1995 dmu_objset_find_ctx_t *child_dcp =
1996 kmem_alloc(sizeof (*child_dcp), KM_SLEEP);
1997 *child_dcp = *dcp;
1998 child_dcp->dc_ddobj = attr->za_first_integer;
1999 child_dcp->dc_ddname = spa_strdup(attr->za_name);
2000 if (dcp->dc_tq != NULL)
2001 (void) taskq_dispatch(dcp->dc_tq,
2002 dmu_objset_find_dp_cb, child_dcp, TQ_SLEEP);
2003 else
2004 dmu_objset_find_dp_impl(child_dcp);
2006 zap_cursor_fini(&zc);
2010 * Iterate over all snapshots.
2012 if (dcp->dc_flags & DS_FIND_SNAPSHOTS) {
2013 dsl_dataset_t *ds;
2014 err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
2016 if (err == 0) {
2017 uint64_t snapobj;
2019 snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
2020 dsl_dataset_rele(ds, FTAG);
2022 for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
2023 zap_cursor_retrieve(&zc, attr) == 0;
2024 (void) zap_cursor_advance(&zc)) {
2025 ASSERT3U(attr->za_integer_length, ==,
2026 sizeof (uint64_t));
2027 ASSERT3U(attr->za_num_integers, ==, 1);
2029 err = dsl_dataset_hold_obj(dp,
2030 attr->za_first_integer, FTAG, &ds);
2031 if (err != 0)
2032 break;
2033 err = dcp->dc_func(dp, ds, dcp->dc_arg);
2034 dsl_dataset_rele(ds, FTAG);
2035 if (err != 0)
2036 break;
2038 zap_cursor_fini(&zc);
2042 kmem_free(attr, sizeof (zap_attribute_t));
2044 if (err != 0) {
2045 dsl_dir_rele(dd, FTAG);
2046 goto out;
2050 * Apply to self.
2052 err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
2055 * Note: we hold the dir while calling dsl_dataset_hold_obj() so
2056 * that the dir will remain cached, and we won't have to re-instantiate
2057 * it (which could be expensive due to finding its name via
2058 * zap_value_search()).
2060 dsl_dir_rele(dd, FTAG);
2061 if (err != 0)
2062 goto out;
2063 err = dcp->dc_func(dp, ds, dcp->dc_arg);
2064 dsl_dataset_rele(ds, FTAG);
2066 out:
2067 if (err != 0) {
2068 mutex_enter(dcp->dc_error_lock);
2069 /* only keep first error */
2070 if (*dcp->dc_error == 0)
2071 *dcp->dc_error = err;
2072 mutex_exit(dcp->dc_error_lock);
2075 if (dcp->dc_ddname != NULL)
2076 spa_strfree(dcp->dc_ddname);
2077 kmem_free(dcp, sizeof (*dcp));
2080 static void
2081 dmu_objset_find_dp_cb(void *arg)
2083 dmu_objset_find_ctx_t *dcp = arg;
2084 dsl_pool_t *dp = dcp->dc_dp;
2087 * We need to get a pool_config_lock here, as there are several
2088 * asssert(pool_config_held) down the stack. Getting a lock via
2089 * dsl_pool_config_enter is risky, as it might be stalled by a
2090 * pending writer. This would deadlock, as the write lock can
2091 * only be granted when our parent thread gives up the lock.
2092 * The _prio interface gives us priority over a pending writer.
2094 dsl_pool_config_enter_prio(dp, FTAG);
2096 dmu_objset_find_dp_impl(dcp);
2098 dsl_pool_config_exit(dp, FTAG);
2102 * Find objsets under and including ddobj, call func(ds) on each.
2103 * The order for the enumeration is completely undefined.
2104 * func is called with dsl_pool_config held.
2107 dmu_objset_find_dp(dsl_pool_t *dp, uint64_t ddobj,
2108 int func(dsl_pool_t *, dsl_dataset_t *, void *), void *arg, int flags)
2110 int error = 0;
2111 taskq_t *tq = NULL;
2112 int ntasks;
2113 dmu_objset_find_ctx_t *dcp;
2114 kmutex_t err_lock;
2116 mutex_init(&err_lock, NULL, MUTEX_DEFAULT, NULL);
2117 dcp = kmem_alloc(sizeof (*dcp), KM_SLEEP);
2118 dcp->dc_tq = NULL;
2119 dcp->dc_dp = dp;
2120 dcp->dc_ddobj = ddobj;
2121 dcp->dc_ddname = NULL;
2122 dcp->dc_func = func;
2123 dcp->dc_arg = arg;
2124 dcp->dc_flags = flags;
2125 dcp->dc_error_lock = &err_lock;
2126 dcp->dc_error = &error;
2128 if ((flags & DS_FIND_SERIALIZE) || dsl_pool_config_held_writer(dp)) {
2130 * In case a write lock is held we can't make use of
2131 * parallelism, as down the stack of the worker threads
2132 * the lock is asserted via dsl_pool_config_held.
2133 * In case of a read lock this is solved by getting a read
2134 * lock in each worker thread, which isn't possible in case
2135 * of a writer lock. So we fall back to the synchronous path
2136 * here.
2137 * In the future it might be possible to get some magic into
2138 * dsl_pool_config_held in a way that it returns true for
2139 * the worker threads so that a single lock held from this
2140 * thread suffices. For now, stay single threaded.
2142 dmu_objset_find_dp_impl(dcp);
2143 mutex_destroy(&err_lock);
2145 return (error);
2148 ntasks = dmu_find_threads;
2149 if (ntasks == 0)
2150 ntasks = vdev_count_leaves(dp->dp_spa) * 4;
2151 tq = taskq_create("dmu_objset_find", ntasks, minclsyspri, ntasks,
2152 INT_MAX, 0);
2153 if (tq == NULL) {
2154 kmem_free(dcp, sizeof (*dcp));
2155 mutex_destroy(&err_lock);
2157 return (SET_ERROR(ENOMEM));
2159 dcp->dc_tq = tq;
2161 /* dcp will be freed by task */
2162 (void) taskq_dispatch(tq, dmu_objset_find_dp_cb, dcp, TQ_SLEEP);
2165 * PORTING: this code relies on the property of taskq_wait to wait
2166 * until no more tasks are queued and no more tasks are active. As
2167 * we always queue new tasks from within other tasks, task_wait
2168 * reliably waits for the full recursion to finish, even though we
2169 * enqueue new tasks after taskq_wait has been called.
2170 * On platforms other than illumos, taskq_wait may not have this
2171 * property.
2173 taskq_wait(tq);
2174 taskq_destroy(tq);
2175 mutex_destroy(&err_lock);
2177 return (error);
2181 * Find all objsets under name, and for each, call 'func(child_name, arg)'.
2182 * The dp_config_rwlock must not be held when this is called, and it
2183 * will not be held when the callback is called.
2184 * Therefore this function should only be used when the pool is not changing
2185 * (e.g. in syncing context), or the callback can deal with the possible races.
2187 static int
2188 dmu_objset_find_impl(spa_t *spa, const char *name,
2189 int func(const char *, void *), void *arg, int flags)
2191 dsl_dir_t *dd;
2192 dsl_pool_t *dp = spa_get_dsl(spa);
2193 dsl_dataset_t *ds;
2194 zap_cursor_t zc;
2195 zap_attribute_t *attr;
2196 char *child;
2197 uint64_t thisobj;
2198 int err;
2200 dsl_pool_config_enter(dp, FTAG);
2202 err = dsl_dir_hold(dp, name, FTAG, &dd, NULL);
2203 if (err != 0) {
2204 dsl_pool_config_exit(dp, FTAG);
2205 return (err);
2208 /* Don't visit hidden ($MOS & $ORIGIN) objsets. */
2209 if (dd->dd_myname[0] == '$') {
2210 dsl_dir_rele(dd, FTAG);
2211 dsl_pool_config_exit(dp, FTAG);
2212 return (0);
2215 thisobj = dsl_dir_phys(dd)->dd_head_dataset_obj;
2216 attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
2219 * Iterate over all children.
2221 if (flags & DS_FIND_CHILDREN) {
2222 for (zap_cursor_init(&zc, dp->dp_meta_objset,
2223 dsl_dir_phys(dd)->dd_child_dir_zapobj);
2224 zap_cursor_retrieve(&zc, attr) == 0;
2225 (void) zap_cursor_advance(&zc)) {
2226 ASSERT3U(attr->za_integer_length, ==,
2227 sizeof (uint64_t));
2228 ASSERT3U(attr->za_num_integers, ==, 1);
2230 child = kmem_asprintf("%s/%s", name, attr->za_name);
2231 dsl_pool_config_exit(dp, FTAG);
2232 err = dmu_objset_find_impl(spa, child,
2233 func, arg, flags);
2234 dsl_pool_config_enter(dp, FTAG);
2235 strfree(child);
2236 if (err != 0)
2237 break;
2239 zap_cursor_fini(&zc);
2241 if (err != 0) {
2242 dsl_dir_rele(dd, FTAG);
2243 dsl_pool_config_exit(dp, FTAG);
2244 kmem_free(attr, sizeof (zap_attribute_t));
2245 return (err);
2250 * Iterate over all snapshots.
2252 if (flags & DS_FIND_SNAPSHOTS) {
2253 err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
2255 if (err == 0) {
2256 uint64_t snapobj;
2258 snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
2259 dsl_dataset_rele(ds, FTAG);
2261 for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
2262 zap_cursor_retrieve(&zc, attr) == 0;
2263 (void) zap_cursor_advance(&zc)) {
2264 ASSERT3U(attr->za_integer_length, ==,
2265 sizeof (uint64_t));
2266 ASSERT3U(attr->za_num_integers, ==, 1);
2268 child = kmem_asprintf("%s@%s",
2269 name, attr->za_name);
2270 dsl_pool_config_exit(dp, FTAG);
2271 err = func(child, arg);
2272 dsl_pool_config_enter(dp, FTAG);
2273 strfree(child);
2274 if (err != 0)
2275 break;
2277 zap_cursor_fini(&zc);
2281 dsl_dir_rele(dd, FTAG);
2282 kmem_free(attr, sizeof (zap_attribute_t));
2283 dsl_pool_config_exit(dp, FTAG);
2285 if (err != 0)
2286 return (err);
2288 /* Apply to self. */
2289 return (func(name, arg));
2293 * See comment above dmu_objset_find_impl().
2296 dmu_objset_find(char *name, int func(const char *, void *), void *arg,
2297 int flags)
2299 spa_t *spa;
2300 int error;
2302 error = spa_open(name, &spa, FTAG);
2303 if (error != 0)
2304 return (error);
2305 error = dmu_objset_find_impl(spa, name, func, arg, flags);
2306 spa_close(spa, FTAG);
2307 return (error);
2310 void
2311 dmu_objset_set_user(objset_t *os, void *user_ptr)
2313 ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
2314 os->os_user_ptr = user_ptr;
2317 void *
2318 dmu_objset_get_user(objset_t *os)
2320 ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
2321 return (os->os_user_ptr);
2325 * Determine name of filesystem, given name of snapshot.
2326 * buf must be at least ZFS_MAX_DATASET_NAME_LEN bytes
2329 dmu_fsname(const char *snapname, char *buf)
2331 char *atp = strchr(snapname, '@');
2332 if (atp == NULL)
2333 return (SET_ERROR(EINVAL));
2334 if (atp - snapname >= ZFS_MAX_DATASET_NAME_LEN)
2335 return (SET_ERROR(ENAMETOOLONG));
2336 (void) strlcpy(buf, snapname, atp - snapname + 1);
2337 return (0);
2341 * Call when we think we're going to write/free space in open context to track
2342 * the amount of dirty data in the open txg, which is also the amount
2343 * of memory that can not be evicted until this txg syncs.
2345 void
2346 dmu_objset_willuse_space(objset_t *os, int64_t space, dmu_tx_t *tx)
2348 dsl_dataset_t *ds = os->os_dsl_dataset;
2349 int64_t aspace = spa_get_worst_case_asize(os->os_spa, space);
2351 if (ds != NULL) {
2352 dsl_dir_willuse_space(ds->ds_dir, aspace, tx);
2353 dsl_pool_dirty_space(dmu_tx_pool(tx), space, tx);