dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / kernel / fs / zfs / dmu_objset.c
blob6bf61854b7e99ea9a82ea274c94bfc56a7826c97
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>
58 * Needed to close a window in dnode_move() that allows the objset to be freed
59 * before it can be safely accessed.
61 krwlock_t os_lock;
64 * Tunable to overwrite the maximum number of threads for the parallization
65 * of dmu_objset_find_dp, needed to speed up the import of pools with many
66 * datasets.
67 * Default is 4 times the number of leaf vdevs.
69 int dmu_find_threads = 0;
72 * Backfill lower metadnode objects after this many have been freed.
73 * Backfilling negatively impacts object creation rates, so only do it
74 * if there are enough holes to fill.
76 int dmu_rescan_dnode_threshold = 131072;
78 static void dmu_objset_find_dp_cb(void *arg);
80 void
81 dmu_objset_init(void)
83 rw_init(&os_lock, NULL, RW_DEFAULT, NULL);
86 void
87 dmu_objset_fini(void)
89 rw_destroy(&os_lock);
92 spa_t *
93 dmu_objset_spa(objset_t *os)
95 return (os->os_spa);
98 zilog_t *
99 dmu_objset_zil(objset_t *os)
101 return (os->os_zil);
104 dsl_pool_t *
105 dmu_objset_pool(objset_t *os)
107 dsl_dataset_t *ds;
109 if ((ds = os->os_dsl_dataset) != NULL && ds->ds_dir)
110 return (ds->ds_dir->dd_pool);
111 else
112 return (spa_get_dsl(os->os_spa));
115 dsl_dataset_t *
116 dmu_objset_ds(objset_t *os)
118 return (os->os_dsl_dataset);
121 dmu_objset_type_t
122 dmu_objset_type(objset_t *os)
124 return (os->os_phys->os_type);
127 void
128 dmu_objset_name(objset_t *os, char *buf)
130 dsl_dataset_name(os->os_dsl_dataset, buf);
133 uint64_t
134 dmu_objset_id(objset_t *os)
136 dsl_dataset_t *ds = os->os_dsl_dataset;
138 return (ds ? ds->ds_object : 0);
141 zfs_sync_type_t
142 dmu_objset_syncprop(objset_t *os)
144 return (os->os_sync);
147 zfs_logbias_op_t
148 dmu_objset_logbias(objset_t *os)
150 return (os->os_logbias);
153 static void
154 checksum_changed_cb(void *arg, uint64_t newval)
156 objset_t *os = arg;
159 * Inheritance should have been done by now.
161 ASSERT(newval != ZIO_CHECKSUM_INHERIT);
163 os->os_checksum = zio_checksum_select(newval, ZIO_CHECKSUM_ON_VALUE);
166 static void
167 compression_changed_cb(void *arg, uint64_t newval)
169 objset_t *os = arg;
172 * Inheritance and range checking should have been done by now.
174 ASSERT(newval != ZIO_COMPRESS_INHERIT);
176 os->os_compress = zio_compress_select(os->os_spa, newval,
177 ZIO_COMPRESS_ON);
180 static void
181 copies_changed_cb(void *arg, uint64_t newval)
183 objset_t *os = arg;
186 * Inheritance and range checking should have been done by now.
188 ASSERT(newval > 0);
189 ASSERT(newval <= spa_max_replication(os->os_spa));
191 os->os_copies = newval;
194 static void
195 dedup_changed_cb(void *arg, uint64_t newval)
197 objset_t *os = arg;
198 spa_t *spa = os->os_spa;
199 enum zio_checksum checksum;
202 * Inheritance should have been done by now.
204 ASSERT(newval != ZIO_CHECKSUM_INHERIT);
206 checksum = zio_checksum_dedup_select(spa, newval, ZIO_CHECKSUM_OFF);
208 os->os_dedup_checksum = checksum & ZIO_CHECKSUM_MASK;
209 os->os_dedup_verify = !!(checksum & ZIO_CHECKSUM_VERIFY);
212 static void
213 primary_cache_changed_cb(void *arg, uint64_t newval)
215 objset_t *os = arg;
218 * Inheritance and range checking should have been done by now.
220 ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
221 newval == ZFS_CACHE_METADATA);
223 os->os_primary_cache = newval;
226 static void
227 secondary_cache_changed_cb(void *arg, uint64_t newval)
229 objset_t *os = arg;
232 * Inheritance and range checking should have been done by now.
234 ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
235 newval == ZFS_CACHE_METADATA);
237 os->os_secondary_cache = newval;
240 static void
241 sync_changed_cb(void *arg, uint64_t newval)
243 objset_t *os = arg;
246 * Inheritance and range checking should have been done by now.
248 ASSERT(newval == ZFS_SYNC_STANDARD || newval == ZFS_SYNC_ALWAYS ||
249 newval == ZFS_SYNC_DISABLED);
251 os->os_sync = newval;
252 if (os->os_zil)
253 zil_set_sync(os->os_zil, newval);
256 static void
257 redundant_metadata_changed_cb(void *arg, uint64_t newval)
259 objset_t *os = arg;
262 * Inheritance and range checking should have been done by now.
264 ASSERT(newval == ZFS_REDUNDANT_METADATA_ALL ||
265 newval == ZFS_REDUNDANT_METADATA_MOST);
267 os->os_redundant_metadata = newval;
270 static void
271 logbias_changed_cb(void *arg, uint64_t newval)
273 objset_t *os = arg;
275 ASSERT(newval == ZFS_LOGBIAS_LATENCY ||
276 newval == ZFS_LOGBIAS_THROUGHPUT);
277 os->os_logbias = newval;
278 if (os->os_zil)
279 zil_set_logbias(os->os_zil, newval);
282 static void
283 recordsize_changed_cb(void *arg, uint64_t newval)
285 objset_t *os = arg;
287 os->os_recordsize = newval;
290 void
291 dmu_objset_byteswap(void *buf, size_t size)
293 objset_phys_t *osp = buf;
295 ASSERT(size == OBJSET_OLD_PHYS_SIZE || size == sizeof (objset_phys_t));
296 dnode_byteswap(&osp->os_meta_dnode);
297 byteswap_uint64_array(&osp->os_zil_header, sizeof (zil_header_t));
298 osp->os_type = BSWAP_64(osp->os_type);
299 osp->os_flags = BSWAP_64(osp->os_flags);
300 if (size == sizeof (objset_phys_t)) {
301 dnode_byteswap(&osp->os_userused_dnode);
302 dnode_byteswap(&osp->os_groupused_dnode);
307 * The hash is a CRC-based hash of the objset_t pointer and the object number.
309 static uint64_t
310 dnode_hash(const objset_t *os, uint64_t obj)
312 uintptr_t osv = (uintptr_t)os;
313 uint64_t crc = -1ULL;
315 ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY);
317 * The low 6 bits of the pointer don't have much entropy, because
318 * the objset_t is larger than 2^6 bytes long.
320 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (osv >> 6)) & 0xFF];
321 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 0)) & 0xFF];
322 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 8)) & 0xFF];
323 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 16)) & 0xFF];
325 crc ^= (osv>>14) ^ (obj>>24);
327 return (crc);
330 unsigned int
331 dnode_multilist_index_func(multilist_t *ml, void *obj)
333 dnode_t *dn = obj;
334 return (dnode_hash(dn->dn_objset, dn->dn_object) %
335 multilist_get_num_sublists(ml));
339 * Instantiates the objset_t in-memory structure corresponding to the
340 * objset_phys_t that's pointed to by the specified blkptr_t.
343 dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
344 objset_t **osp)
346 objset_t *os;
347 int i, err;
349 ASSERT(ds == NULL || MUTEX_HELD(&ds->ds_opening_lock));
351 os = kmem_zalloc(sizeof (objset_t), KM_SLEEP);
352 os->os_dsl_dataset = ds;
353 os->os_spa = spa;
354 os->os_rootbp = bp;
355 if (!BP_IS_HOLE(os->os_rootbp)) {
356 arc_flags_t aflags = ARC_FLAG_WAIT;
357 zbookmark_phys_t zb;
358 SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET,
359 ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
361 if (DMU_OS_IS_L2CACHEABLE(os))
362 aflags |= ARC_FLAG_L2CACHE;
364 dprintf_bp(os->os_rootbp, "reading %s", "");
365 err = arc_read(NULL, spa, os->os_rootbp,
366 arc_getbuf_func, &os->os_phys_buf,
367 ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &aflags, &zb);
368 if (err != 0) {
369 kmem_free(os, sizeof (objset_t));
370 /* convert checksum errors into IO errors */
371 if (err == ECKSUM)
372 err = SET_ERROR(EIO);
373 return (err);
376 /* Increase the blocksize if we are permitted. */
377 if (spa_version(spa) >= SPA_VERSION_USERSPACE &&
378 arc_buf_size(os->os_phys_buf) < sizeof (objset_phys_t)) {
379 arc_buf_t *buf = arc_alloc_buf(spa, &os->os_phys_buf,
380 ARC_BUFC_METADATA, sizeof (objset_phys_t));
381 bzero(buf->b_data, sizeof (objset_phys_t));
382 bcopy(os->os_phys_buf->b_data, buf->b_data,
383 arc_buf_size(os->os_phys_buf));
384 arc_buf_destroy(os->os_phys_buf, &os->os_phys_buf);
385 os->os_phys_buf = buf;
388 os->os_phys = os->os_phys_buf->b_data;
389 os->os_flags = os->os_phys->os_flags;
390 } else {
391 int size = spa_version(spa) >= SPA_VERSION_USERSPACE ?
392 sizeof (objset_phys_t) : OBJSET_OLD_PHYS_SIZE;
393 os->os_phys_buf = arc_alloc_buf(spa, &os->os_phys_buf,
394 ARC_BUFC_METADATA, size);
395 os->os_phys = os->os_phys_buf->b_data;
396 bzero(os->os_phys, size);
400 * Note: the changed_cb will be called once before the register
401 * func returns, thus changing the checksum/compression from the
402 * default (fletcher2/off). Snapshots don't need to know about
403 * checksum/compression/copies.
405 if (ds != NULL) {
406 boolean_t needlock = B_FALSE;
409 * Note: it's valid to open the objset if the dataset is
410 * long-held, in which case the pool_config lock will not
411 * be held.
413 if (!dsl_pool_config_held(dmu_objset_pool(os))) {
414 needlock = B_TRUE;
415 dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
417 err = dsl_prop_register(ds,
418 zfs_prop_to_name(ZFS_PROP_PRIMARYCACHE),
419 primary_cache_changed_cb, os);
420 if (err == 0) {
421 err = dsl_prop_register(ds,
422 zfs_prop_to_name(ZFS_PROP_SECONDARYCACHE),
423 secondary_cache_changed_cb, os);
425 if (!ds->ds_is_snapshot) {
426 if (err == 0) {
427 err = dsl_prop_register(ds,
428 zfs_prop_to_name(ZFS_PROP_CHECKSUM),
429 checksum_changed_cb, os);
431 if (err == 0) {
432 err = dsl_prop_register(ds,
433 zfs_prop_to_name(ZFS_PROP_COMPRESSION),
434 compression_changed_cb, os);
436 if (err == 0) {
437 err = dsl_prop_register(ds,
438 zfs_prop_to_name(ZFS_PROP_COPIES),
439 copies_changed_cb, os);
441 if (err == 0) {
442 err = dsl_prop_register(ds,
443 zfs_prop_to_name(ZFS_PROP_DEDUP),
444 dedup_changed_cb, os);
446 if (err == 0) {
447 err = dsl_prop_register(ds,
448 zfs_prop_to_name(ZFS_PROP_LOGBIAS),
449 logbias_changed_cb, os);
451 if (err == 0) {
452 err = dsl_prop_register(ds,
453 zfs_prop_to_name(ZFS_PROP_SYNC),
454 sync_changed_cb, os);
456 if (err == 0) {
457 err = dsl_prop_register(ds,
458 zfs_prop_to_name(
459 ZFS_PROP_REDUNDANT_METADATA),
460 redundant_metadata_changed_cb, os);
462 if (err == 0) {
463 err = dsl_prop_register(ds,
464 zfs_prop_to_name(ZFS_PROP_RECORDSIZE),
465 recordsize_changed_cb, os);
468 if (needlock)
469 dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
470 if (err != 0) {
471 arc_buf_destroy(os->os_phys_buf, &os->os_phys_buf);
472 kmem_free(os, sizeof (objset_t));
473 return (err);
475 } else {
476 /* It's the meta-objset. */
477 os->os_checksum = ZIO_CHECKSUM_FLETCHER_4;
478 os->os_compress = ZIO_COMPRESS_ON;
479 os->os_copies = spa_max_replication(spa);
480 os->os_dedup_checksum = ZIO_CHECKSUM_OFF;
481 os->os_dedup_verify = B_FALSE;
482 os->os_logbias = ZFS_LOGBIAS_LATENCY;
483 os->os_sync = ZFS_SYNC_STANDARD;
484 os->os_primary_cache = ZFS_CACHE_ALL;
485 os->os_secondary_cache = ZFS_CACHE_ALL;
488 if (ds == NULL || !ds->ds_is_snapshot)
489 os->os_zil_header = os->os_phys->os_zil_header;
490 os->os_zil = zil_alloc(os, &os->os_zil_header);
492 for (i = 0; i < TXG_SIZE; i++) {
493 os->os_dirty_dnodes[i] = multilist_create(sizeof (dnode_t),
494 offsetof(dnode_t, dn_dirty_link[i]),
495 dnode_multilist_index_func);
497 list_create(&os->os_dnodes, sizeof (dnode_t),
498 offsetof(dnode_t, dn_link));
499 list_create(&os->os_downgraded_dbufs, sizeof (dmu_buf_impl_t),
500 offsetof(dmu_buf_impl_t, db_link));
502 mutex_init(&os->os_lock, NULL, MUTEX_DEFAULT, NULL);
503 mutex_init(&os->os_userused_lock, NULL, MUTEX_DEFAULT, NULL);
504 mutex_init(&os->os_obj_lock, NULL, MUTEX_DEFAULT, NULL);
505 mutex_init(&os->os_user_ptr_lock, NULL, MUTEX_DEFAULT, NULL);
507 dnode_special_open(os, &os->os_phys->os_meta_dnode,
508 DMU_META_DNODE_OBJECT, &os->os_meta_dnode);
509 if (arc_buf_size(os->os_phys_buf) >= sizeof (objset_phys_t)) {
510 dnode_special_open(os, &os->os_phys->os_userused_dnode,
511 DMU_USERUSED_OBJECT, &os->os_userused_dnode);
512 dnode_special_open(os, &os->os_phys->os_groupused_dnode,
513 DMU_GROUPUSED_OBJECT, &os->os_groupused_dnode);
516 *osp = os;
517 return (0);
521 dmu_objset_from_ds(dsl_dataset_t *ds, objset_t **osp)
523 int err = 0;
526 * We shouldn't be doing anything with dsl_dataset_t's unless the
527 * pool_config lock is held, or the dataset is long-held.
529 ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool) ||
530 dsl_dataset_long_held(ds));
532 mutex_enter(&ds->ds_opening_lock);
533 if (ds->ds_objset == NULL) {
534 objset_t *os;
535 rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
536 err = dmu_objset_open_impl(dsl_dataset_get_spa(ds),
537 ds, dsl_dataset_get_blkptr(ds), &os);
538 rrw_exit(&ds->ds_bp_rwlock, FTAG);
540 if (err == 0) {
541 mutex_enter(&ds->ds_lock);
542 ASSERT(ds->ds_objset == NULL);
543 ds->ds_objset = os;
544 mutex_exit(&ds->ds_lock);
547 *osp = ds->ds_objset;
548 mutex_exit(&ds->ds_opening_lock);
549 return (err);
553 * Holds the pool while the objset is held. Therefore only one objset
554 * can be held at a time.
557 dmu_objset_hold(const char *name, void *tag, objset_t **osp)
559 dsl_pool_t *dp;
560 dsl_dataset_t *ds;
561 int err;
563 err = dsl_pool_hold(name, tag, &dp);
564 if (err != 0)
565 return (err);
566 err = dsl_dataset_hold(dp, name, tag, &ds);
567 if (err != 0) {
568 dsl_pool_rele(dp, tag);
569 return (err);
572 err = dmu_objset_from_ds(ds, osp);
573 if (err != 0) {
574 dsl_dataset_rele(ds, tag);
575 dsl_pool_rele(dp, tag);
578 return (err);
581 static int
582 dmu_objset_own_impl(dsl_dataset_t *ds, dmu_objset_type_t type,
583 boolean_t readonly, void *tag, objset_t **osp)
585 int err;
587 err = dmu_objset_from_ds(ds, osp);
588 if (err != 0) {
589 dsl_dataset_disown(ds, tag);
590 } else if (type != DMU_OST_ANY && type != (*osp)->os_phys->os_type) {
591 dsl_dataset_disown(ds, tag);
592 return (SET_ERROR(EINVAL));
593 } else if (!readonly && dsl_dataset_is_snapshot(ds)) {
594 dsl_dataset_disown(ds, tag);
595 return (SET_ERROR(EROFS));
597 return (err);
601 * dsl_pool must not be held when this is called.
602 * Upon successful return, there will be a longhold on the dataset,
603 * and the dsl_pool will not be held.
606 dmu_objset_own(const char *name, dmu_objset_type_t type,
607 boolean_t readonly, void *tag, objset_t **osp)
609 dsl_pool_t *dp;
610 dsl_dataset_t *ds;
611 int err;
613 err = dsl_pool_hold(name, FTAG, &dp);
614 if (err != 0)
615 return (err);
616 err = dsl_dataset_own(dp, name, tag, &ds);
617 if (err != 0) {
618 dsl_pool_rele(dp, FTAG);
619 return (err);
621 err = dmu_objset_own_impl(ds, type, readonly, tag, osp);
622 dsl_pool_rele(dp, FTAG);
624 return (err);
628 dmu_objset_own_obj(dsl_pool_t *dp, uint64_t obj, dmu_objset_type_t type,
629 boolean_t readonly, void *tag, objset_t **osp)
631 dsl_dataset_t *ds;
632 int err;
634 err = dsl_dataset_own_obj(dp, obj, tag, &ds);
635 if (err != 0)
636 return (err);
638 return (dmu_objset_own_impl(ds, type, readonly, tag, osp));
641 void
642 dmu_objset_rele(objset_t *os, void *tag)
644 dsl_pool_t *dp = dmu_objset_pool(os);
645 dsl_dataset_rele(os->os_dsl_dataset, tag);
646 dsl_pool_rele(dp, tag);
650 * When we are called, os MUST refer to an objset associated with a dataset
651 * that is owned by 'tag'; that is, is held and long held by 'tag' and ds_owner
652 * == tag. We will then release and reacquire ownership of the dataset while
653 * holding the pool config_rwlock to avoid intervening namespace or ownership
654 * changes may occur.
656 * This exists solely to accommodate zfs_ioc_userspace_upgrade()'s desire to
657 * release the hold on its dataset and acquire a new one on the dataset of the
658 * same name so that it can be partially torn down and reconstructed.
660 void
661 dmu_objset_refresh_ownership(objset_t *os, void *tag)
663 dsl_pool_t *dp;
664 dsl_dataset_t *ds, *newds;
665 char name[ZFS_MAX_DATASET_NAME_LEN];
667 ds = os->os_dsl_dataset;
668 VERIFY3P(ds, !=, NULL);
669 VERIFY3P(ds->ds_owner, ==, tag);
670 VERIFY(dsl_dataset_long_held(ds));
672 dsl_dataset_name(ds, name);
673 dp = dmu_objset_pool(os);
674 dsl_pool_config_enter(dp, FTAG);
675 dmu_objset_disown(os, tag);
676 VERIFY0(dsl_dataset_own(dp, name, tag, &newds));
677 VERIFY3P(newds, ==, os->os_dsl_dataset);
678 dsl_pool_config_exit(dp, FTAG);
681 void
682 dmu_objset_disown(objset_t *os, void *tag)
684 dsl_dataset_disown(os->os_dsl_dataset, tag);
687 void
688 dmu_objset_evict_dbufs(objset_t *os)
690 dnode_t dn_marker;
691 dnode_t *dn;
693 mutex_enter(&os->os_lock);
694 dn = list_head(&os->os_dnodes);
695 while (dn != NULL) {
697 * Skip dnodes without holds. We have to do this dance
698 * because dnode_add_ref() only works if there is already a
699 * hold. If the dnode has no holds, then it has no dbufs.
701 if (dnode_add_ref(dn, FTAG)) {
702 list_insert_after(&os->os_dnodes, dn, &dn_marker);
703 mutex_exit(&os->os_lock);
705 dnode_evict_dbufs(dn);
706 dnode_rele(dn, FTAG);
708 mutex_enter(&os->os_lock);
709 dn = list_next(&os->os_dnodes, &dn_marker);
710 list_remove(&os->os_dnodes, &dn_marker);
711 } else {
712 dn = list_next(&os->os_dnodes, dn);
715 mutex_exit(&os->os_lock);
717 if (DMU_USERUSED_DNODE(os) != NULL) {
718 dnode_evict_dbufs(DMU_GROUPUSED_DNODE(os));
719 dnode_evict_dbufs(DMU_USERUSED_DNODE(os));
721 dnode_evict_dbufs(DMU_META_DNODE(os));
725 * Objset eviction processing is split into into two pieces.
726 * The first marks the objset as evicting, evicts any dbufs that
727 * have a refcount of zero, and then queues up the objset for the
728 * second phase of eviction. Once os->os_dnodes has been cleared by
729 * dnode_buf_pageout()->dnode_destroy(), the second phase is executed.
730 * The second phase closes the special dnodes, dequeues the objset from
731 * the list of those undergoing eviction, and finally frees the objset.
733 * NOTE: Due to asynchronous eviction processing (invocation of
734 * dnode_buf_pageout()), it is possible for the meta dnode for the
735 * objset to have no holds even though os->os_dnodes is not empty.
737 void
738 dmu_objset_evict(objset_t *os)
740 dsl_dataset_t *ds = os->os_dsl_dataset;
742 for (int t = 0; t < TXG_SIZE; t++)
743 ASSERT(!dmu_objset_is_dirty(os, t));
745 if (ds)
746 dsl_prop_unregister_all(ds, os);
748 if (os->os_sa)
749 sa_tear_down(os);
751 dmu_objset_evict_dbufs(os);
753 mutex_enter(&os->os_lock);
754 spa_evicting_os_register(os->os_spa, os);
755 if (list_is_empty(&os->os_dnodes)) {
756 mutex_exit(&os->os_lock);
757 dmu_objset_evict_done(os);
758 } else {
759 mutex_exit(&os->os_lock);
763 void
764 dmu_objset_evict_done(objset_t *os)
766 ASSERT3P(list_head(&os->os_dnodes), ==, NULL);
768 dnode_special_close(&os->os_meta_dnode);
769 if (DMU_USERUSED_DNODE(os)) {
770 dnode_special_close(&os->os_userused_dnode);
771 dnode_special_close(&os->os_groupused_dnode);
773 zil_free(os->os_zil);
775 arc_buf_destroy(os->os_phys_buf, &os->os_phys_buf);
778 * This is a barrier to prevent the objset from going away in
779 * dnode_move() until we can safely ensure that the objset is still in
780 * use. We consider the objset valid before the barrier and invalid
781 * after the barrier.
783 rw_enter(&os_lock, RW_READER);
784 rw_exit(&os_lock);
786 mutex_destroy(&os->os_lock);
787 mutex_destroy(&os->os_userused_lock);
788 mutex_destroy(&os->os_obj_lock);
789 mutex_destroy(&os->os_user_ptr_lock);
790 for (int i = 0; i < TXG_SIZE; i++) {
791 multilist_destroy(os->os_dirty_dnodes[i]);
793 spa_evicting_os_deregister(os->os_spa, os);
794 kmem_free(os, sizeof (objset_t));
797 timestruc_t
798 dmu_objset_snap_cmtime(objset_t *os)
800 return (dsl_dir_snap_cmtime(os->os_dsl_dataset->ds_dir));
803 /* called from dsl for meta-objset */
804 objset_t *
805 dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
806 dmu_objset_type_t type, dmu_tx_t *tx)
808 objset_t *os;
809 dnode_t *mdn;
811 ASSERT(dmu_tx_is_syncing(tx));
813 if (ds != NULL)
814 VERIFY0(dmu_objset_from_ds(ds, &os));
815 else
816 VERIFY0(dmu_objset_open_impl(spa, NULL, bp, &os));
818 mdn = DMU_META_DNODE(os);
820 dnode_allocate(mdn, DMU_OT_DNODE, 1 << DNODE_BLOCK_SHIFT,
821 DN_MAX_INDBLKSHIFT, DMU_OT_NONE, 0, tx);
824 * We don't want to have to increase the meta-dnode's nlevels
825 * later, because then we could do it in quescing context while
826 * we are also accessing it in open context.
828 * This precaution is not necessary for the MOS (ds == NULL),
829 * because the MOS is only updated in syncing context.
830 * This is most fortunate: the MOS is the only objset that
831 * needs to be synced multiple times as spa_sync() iterates
832 * to convergence, so minimizing its dn_nlevels matters.
834 if (ds != NULL) {
835 int levels = 1;
838 * Determine the number of levels necessary for the meta-dnode
839 * to contain DN_MAX_OBJECT dnodes. Note that in order to
840 * ensure that we do not overflow 64 bits, there has to be
841 * a nlevels that gives us a number of blocks > DN_MAX_OBJECT
842 * but < 2^64. Therefore,
843 * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT) (10) must be
844 * less than (64 - log2(DN_MAX_OBJECT)) (16).
846 while ((uint64_t)mdn->dn_nblkptr <<
847 (mdn->dn_datablkshift - DNODE_SHIFT +
848 (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) <
849 DN_MAX_OBJECT)
850 levels++;
852 mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] =
853 mdn->dn_nlevels = levels;
856 ASSERT(type != DMU_OST_NONE);
857 ASSERT(type != DMU_OST_ANY);
858 ASSERT(type < DMU_OST_NUMTYPES);
859 os->os_phys->os_type = type;
860 if (dmu_objset_userused_enabled(os)) {
861 os->os_phys->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
862 os->os_flags = os->os_phys->os_flags;
865 dsl_dataset_dirty(ds, tx);
867 return (os);
870 typedef struct dmu_objset_create_arg {
871 const char *doca_name;
872 cred_t *doca_cred;
873 void (*doca_userfunc)(objset_t *os, void *arg,
874 cred_t *cr, dmu_tx_t *tx);
875 void *doca_userarg;
876 dmu_objset_type_t doca_type;
877 uint64_t doca_flags;
878 } dmu_objset_create_arg_t;
880 /*ARGSUSED*/
881 static int
882 dmu_objset_create_check(void *arg, dmu_tx_t *tx)
884 dmu_objset_create_arg_t *doca = arg;
885 dsl_pool_t *dp = dmu_tx_pool(tx);
886 dsl_dir_t *pdd;
887 const char *tail;
888 int error;
890 if (strchr(doca->doca_name, '@') != NULL)
891 return (SET_ERROR(EINVAL));
893 if (strlen(doca->doca_name) >= ZFS_MAX_DATASET_NAME_LEN)
894 return (SET_ERROR(ENAMETOOLONG));
896 error = dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail);
897 if (error != 0)
898 return (error);
899 if (tail == NULL) {
900 dsl_dir_rele(pdd, FTAG);
901 return (SET_ERROR(EEXIST));
903 error = dsl_fs_ss_limit_check(pdd, 1, ZFS_PROP_FILESYSTEM_LIMIT, NULL,
904 doca->doca_cred);
905 dsl_dir_rele(pdd, FTAG);
907 return (error);
910 static void
911 dmu_objset_create_sync(void *arg, dmu_tx_t *tx)
913 dmu_objset_create_arg_t *doca = arg;
914 dsl_pool_t *dp = dmu_tx_pool(tx);
915 dsl_dir_t *pdd;
916 const char *tail;
917 dsl_dataset_t *ds;
918 uint64_t obj;
919 blkptr_t *bp;
920 objset_t *os;
922 VERIFY0(dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail));
924 obj = dsl_dataset_create_sync(pdd, tail, NULL, doca->doca_flags,
925 doca->doca_cred, tx);
927 VERIFY0(dsl_dataset_hold_obj(pdd->dd_pool, obj, FTAG, &ds));
928 rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
929 bp = dsl_dataset_get_blkptr(ds);
930 os = dmu_objset_create_impl(pdd->dd_pool->dp_spa,
931 ds, bp, doca->doca_type, tx);
932 rrw_exit(&ds->ds_bp_rwlock, FTAG);
934 if (doca->doca_userfunc != NULL) {
935 doca->doca_userfunc(os, doca->doca_userarg,
936 doca->doca_cred, tx);
939 spa_history_log_internal_ds(ds, "create", tx, "");
940 dsl_dataset_rele(ds, FTAG);
941 dsl_dir_rele(pdd, FTAG);
945 dmu_objset_create(const char *name, dmu_objset_type_t type, uint64_t flags,
946 void (*func)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx), void *arg)
948 dmu_objset_create_arg_t doca;
950 doca.doca_name = name;
951 doca.doca_cred = CRED();
952 doca.doca_flags = flags;
953 doca.doca_userfunc = func;
954 doca.doca_userarg = arg;
955 doca.doca_type = type;
957 return (dsl_sync_task(name,
958 dmu_objset_create_check, dmu_objset_create_sync, &doca,
959 5, ZFS_SPACE_CHECK_NORMAL));
962 typedef struct dmu_objset_clone_arg {
963 const char *doca_clone;
964 const char *doca_origin;
965 cred_t *doca_cred;
966 } dmu_objset_clone_arg_t;
968 /*ARGSUSED*/
969 static int
970 dmu_objset_clone_check(void *arg, dmu_tx_t *tx)
972 dmu_objset_clone_arg_t *doca = arg;
973 dsl_dir_t *pdd;
974 const char *tail;
975 int error;
976 dsl_dataset_t *origin;
977 dsl_pool_t *dp = dmu_tx_pool(tx);
979 if (strchr(doca->doca_clone, '@') != NULL)
980 return (SET_ERROR(EINVAL));
982 if (strlen(doca->doca_clone) >= ZFS_MAX_DATASET_NAME_LEN)
983 return (SET_ERROR(ENAMETOOLONG));
985 error = dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail);
986 if (error != 0)
987 return (error);
988 if (tail == NULL) {
989 dsl_dir_rele(pdd, FTAG);
990 return (SET_ERROR(EEXIST));
993 error = dsl_fs_ss_limit_check(pdd, 1, ZFS_PROP_FILESYSTEM_LIMIT, NULL,
994 doca->doca_cred);
995 if (error != 0) {
996 dsl_dir_rele(pdd, FTAG);
997 return (SET_ERROR(EDQUOT));
999 dsl_dir_rele(pdd, FTAG);
1001 error = dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin);
1002 if (error != 0)
1003 return (error);
1005 /* You can only clone snapshots, not the head datasets. */
1006 if (!origin->ds_is_snapshot) {
1007 dsl_dataset_rele(origin, FTAG);
1008 return (SET_ERROR(EINVAL));
1010 dsl_dataset_rele(origin, FTAG);
1012 return (0);
1015 static void
1016 dmu_objset_clone_sync(void *arg, dmu_tx_t *tx)
1018 dmu_objset_clone_arg_t *doca = arg;
1019 dsl_pool_t *dp = dmu_tx_pool(tx);
1020 dsl_dir_t *pdd;
1021 const char *tail;
1022 dsl_dataset_t *origin, *ds;
1023 uint64_t obj;
1024 char namebuf[ZFS_MAX_DATASET_NAME_LEN];
1026 VERIFY0(dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail));
1027 VERIFY0(dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin));
1029 obj = dsl_dataset_create_sync(pdd, tail, origin, 0,
1030 doca->doca_cred, tx);
1032 VERIFY0(dsl_dataset_hold_obj(pdd->dd_pool, obj, FTAG, &ds));
1033 dsl_dataset_name(origin, namebuf);
1034 spa_history_log_internal_ds(ds, "clone", tx,
1035 "origin=%s (%llu)", namebuf, origin->ds_object);
1036 dsl_dataset_rele(ds, FTAG);
1037 dsl_dataset_rele(origin, FTAG);
1038 dsl_dir_rele(pdd, FTAG);
1042 dmu_objset_clone(const char *clone, const char *origin)
1044 dmu_objset_clone_arg_t doca;
1046 doca.doca_clone = clone;
1047 doca.doca_origin = origin;
1048 doca.doca_cred = CRED();
1050 return (dsl_sync_task(clone,
1051 dmu_objset_clone_check, dmu_objset_clone_sync, &doca,
1052 5, ZFS_SPACE_CHECK_NORMAL));
1056 dmu_objset_snapshot_one(const char *fsname, const char *snapname)
1058 int err;
1059 char *longsnap = kmem_asprintf("%s@%s", fsname, snapname);
1060 nvlist_t *snaps = fnvlist_alloc();
1062 fnvlist_add_boolean(snaps, longsnap);
1063 strfree(longsnap);
1064 err = dsl_dataset_snapshot(snaps, NULL, NULL);
1065 fnvlist_free(snaps);
1066 return (err);
1069 static void
1070 dmu_objset_sync_dnodes(multilist_sublist_t *list, dmu_tx_t *tx)
1072 dnode_t *dn;
1074 while ((dn = multilist_sublist_head(list)) != NULL) {
1075 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
1076 ASSERT(dn->dn_dbuf->db_data_pending);
1078 * Initialize dn_zio outside dnode_sync() because the
1079 * meta-dnode needs to set it ouside dnode_sync().
1081 dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio;
1082 ASSERT(dn->dn_zio);
1084 ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS);
1085 multilist_sublist_remove(list, dn);
1087 multilist_t *newlist = dn->dn_objset->os_synced_dnodes;
1088 if (newlist != NULL) {
1089 (void) dnode_add_ref(dn, newlist);
1090 multilist_insert(newlist, dn);
1093 dnode_sync(dn, tx);
1097 /* ARGSUSED */
1098 static void
1099 dmu_objset_write_ready(zio_t *zio, arc_buf_t *abuf, void *arg)
1101 blkptr_t *bp = zio->io_bp;
1102 objset_t *os = arg;
1103 dnode_phys_t *dnp = &os->os_phys->os_meta_dnode;
1105 ASSERT(!BP_IS_EMBEDDED(bp));
1106 ASSERT3U(BP_GET_TYPE(bp), ==, DMU_OT_OBJSET);
1107 ASSERT0(BP_GET_LEVEL(bp));
1110 * Update rootbp fill count: it should be the number of objects
1111 * allocated in the object set (not counting the "special"
1112 * objects that are stored in the objset_phys_t -- the meta
1113 * dnode and user/group accounting objects).
1115 bp->blk_fill = 0;
1116 for (int i = 0; i < dnp->dn_nblkptr; i++)
1117 bp->blk_fill += BP_GET_FILL(&dnp->dn_blkptr[i]);
1118 if (os->os_dsl_dataset != NULL)
1119 rrw_enter(&os->os_dsl_dataset->ds_bp_rwlock, RW_WRITER, FTAG);
1120 *os->os_rootbp = *bp;
1121 if (os->os_dsl_dataset != NULL)
1122 rrw_exit(&os->os_dsl_dataset->ds_bp_rwlock, FTAG);
1125 /* ARGSUSED */
1126 static void
1127 dmu_objset_write_done(zio_t *zio, arc_buf_t *abuf, void *arg)
1129 blkptr_t *bp = zio->io_bp;
1130 blkptr_t *bp_orig = &zio->io_bp_orig;
1131 objset_t *os = arg;
1133 if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
1134 ASSERT(BP_EQUAL(bp, bp_orig));
1135 } else {
1136 dsl_dataset_t *ds = os->os_dsl_dataset;
1137 dmu_tx_t *tx = os->os_synctx;
1139 (void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
1140 dsl_dataset_block_born(ds, bp, tx);
1142 kmem_free(bp, sizeof (*bp));
1145 typedef struct sync_dnodes_arg {
1146 multilist_t *sda_list;
1147 int sda_sublist_idx;
1148 multilist_t *sda_newlist;
1149 dmu_tx_t *sda_tx;
1150 } sync_dnodes_arg_t;
1152 static void
1153 sync_dnodes_task(void *arg)
1155 sync_dnodes_arg_t *sda = arg;
1157 multilist_sublist_t *ms =
1158 multilist_sublist_lock(sda->sda_list, sda->sda_sublist_idx);
1160 dmu_objset_sync_dnodes(ms, sda->sda_tx);
1162 multilist_sublist_unlock(ms);
1164 kmem_free(sda, sizeof (*sda));
1168 /* called from dsl */
1169 void
1170 dmu_objset_sync(objset_t *os, zio_t *pio, dmu_tx_t *tx)
1172 int txgoff;
1173 zbookmark_phys_t zb;
1174 zio_prop_t zp;
1175 zio_t *zio;
1176 list_t *list;
1177 dbuf_dirty_record_t *dr;
1178 blkptr_t *blkptr_copy = kmem_alloc(sizeof (*os->os_rootbp), KM_SLEEP);
1179 *blkptr_copy = *os->os_rootbp;
1181 dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg);
1183 ASSERT(dmu_tx_is_syncing(tx));
1184 /* XXX the write_done callback should really give us the tx... */
1185 os->os_synctx = tx;
1187 if (os->os_dsl_dataset == NULL) {
1189 * This is the MOS. If we have upgraded,
1190 * spa_max_replication() could change, so reset
1191 * os_copies here.
1193 os->os_copies = spa_max_replication(os->os_spa);
1197 * Create the root block IO
1199 SET_BOOKMARK(&zb, os->os_dsl_dataset ?
1200 os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
1201 ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
1202 arc_release(os->os_phys_buf, &os->os_phys_buf);
1204 dmu_write_policy(os, NULL, 0, 0, &zp);
1206 zio = arc_write(pio, os->os_spa, tx->tx_txg,
1207 blkptr_copy, os->os_phys_buf, DMU_OS_IS_L2CACHEABLE(os),
1208 &zp, dmu_objset_write_ready, NULL, NULL, dmu_objset_write_done,
1209 os, ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
1212 * Sync special dnodes - the parent IO for the sync is the root block
1214 DMU_META_DNODE(os)->dn_zio = zio;
1215 dnode_sync(DMU_META_DNODE(os), tx);
1217 os->os_phys->os_flags = os->os_flags;
1219 if (DMU_USERUSED_DNODE(os) &&
1220 DMU_USERUSED_DNODE(os)->dn_type != DMU_OT_NONE) {
1221 DMU_USERUSED_DNODE(os)->dn_zio = zio;
1222 dnode_sync(DMU_USERUSED_DNODE(os), tx);
1223 DMU_GROUPUSED_DNODE(os)->dn_zio = zio;
1224 dnode_sync(DMU_GROUPUSED_DNODE(os), tx);
1227 txgoff = tx->tx_txg & TXG_MASK;
1229 if (dmu_objset_userused_enabled(os)) {
1231 * We must create the list here because it uses the
1232 * dn_dirty_link[] of this txg. But it may already
1233 * exist because we call dsl_dataset_sync() twice per txg.
1235 if (os->os_synced_dnodes == NULL) {
1236 os->os_synced_dnodes =
1237 multilist_create(sizeof (dnode_t),
1238 offsetof(dnode_t, dn_dirty_link[txgoff]),
1239 dnode_multilist_index_func);
1240 } else {
1241 ASSERT3U(os->os_synced_dnodes->ml_offset, ==,
1242 offsetof(dnode_t, dn_dirty_link[txgoff]));
1246 for (int i = 0;
1247 i < multilist_get_num_sublists(os->os_dirty_dnodes[txgoff]); i++) {
1248 sync_dnodes_arg_t *sda = kmem_alloc(sizeof (*sda), KM_SLEEP);
1249 sda->sda_list = os->os_dirty_dnodes[txgoff];
1250 sda->sda_sublist_idx = i;
1251 sda->sda_tx = tx;
1252 (void) taskq_dispatch(dmu_objset_pool(os)->dp_sync_taskq,
1253 sync_dnodes_task, sda, 0);
1254 /* callback frees sda */
1256 taskq_wait(dmu_objset_pool(os)->dp_sync_taskq);
1258 list = &DMU_META_DNODE(os)->dn_dirty_records[txgoff];
1259 while ((dr = list_head(list)) != NULL) {
1260 ASSERT0(dr->dr_dbuf->db_level);
1261 list_remove(list, dr);
1262 if (dr->dr_zio)
1263 zio_nowait(dr->dr_zio);
1266 /* Enable dnode backfill if enough objects have been freed. */
1267 if (os->os_freed_dnodes >= dmu_rescan_dnode_threshold) {
1268 os->os_rescan_dnodes = B_TRUE;
1269 os->os_freed_dnodes = 0;
1273 * Free intent log blocks up to this tx.
1275 zil_sync(os->os_zil, tx);
1276 os->os_phys->os_zil_header = os->os_zil_header;
1277 zio_nowait(zio);
1280 boolean_t
1281 dmu_objset_is_dirty(objset_t *os, uint64_t txg)
1283 return (!multilist_is_empty(os->os_dirty_dnodes[txg & TXG_MASK]));
1286 static objset_used_cb_t *used_cbs[DMU_OST_NUMTYPES];
1288 void
1289 dmu_objset_register_type(dmu_objset_type_t ost, objset_used_cb_t *cb)
1291 used_cbs[ost] = cb;
1294 boolean_t
1295 dmu_objset_userused_enabled(objset_t *os)
1297 return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE &&
1298 used_cbs[os->os_phys->os_type] != NULL &&
1299 DMU_USERUSED_DNODE(os) != NULL);
1302 typedef struct userquota_node {
1303 uint64_t uqn_id;
1304 int64_t uqn_delta;
1305 avl_node_t uqn_node;
1306 } userquota_node_t;
1308 typedef struct userquota_cache {
1309 avl_tree_t uqc_user_deltas;
1310 avl_tree_t uqc_group_deltas;
1311 } userquota_cache_t;
1313 static int
1314 userquota_compare(const void *l, const void *r)
1316 const userquota_node_t *luqn = l;
1317 const userquota_node_t *ruqn = r;
1319 if (luqn->uqn_id < ruqn->uqn_id)
1320 return (-1);
1321 if (luqn->uqn_id > ruqn->uqn_id)
1322 return (1);
1323 return (0);
1326 static void
1327 do_userquota_cacheflush(objset_t *os, userquota_cache_t *cache, dmu_tx_t *tx)
1329 void *cookie;
1330 userquota_node_t *uqn;
1332 ASSERT(dmu_tx_is_syncing(tx));
1334 cookie = NULL;
1335 while ((uqn = avl_destroy_nodes(&cache->uqc_user_deltas,
1336 &cookie)) != NULL) {
1338 * os_userused_lock protects against concurrent calls to
1339 * zap_increment_int(). It's needed because zap_increment_int()
1340 * is not thread-safe (i.e. not atomic).
1342 mutex_enter(&os->os_userused_lock);
1343 VERIFY0(zap_increment_int(os, DMU_USERUSED_OBJECT,
1344 uqn->uqn_id, uqn->uqn_delta, tx));
1345 mutex_exit(&os->os_userused_lock);
1346 kmem_free(uqn, sizeof (*uqn));
1348 avl_destroy(&cache->uqc_user_deltas);
1350 cookie = NULL;
1351 while ((uqn = avl_destroy_nodes(&cache->uqc_group_deltas,
1352 &cookie)) != NULL) {
1353 mutex_enter(&os->os_userused_lock);
1354 VERIFY0(zap_increment_int(os, DMU_GROUPUSED_OBJECT,
1355 uqn->uqn_id, uqn->uqn_delta, tx));
1356 mutex_exit(&os->os_userused_lock);
1357 kmem_free(uqn, sizeof (*uqn));
1359 avl_destroy(&cache->uqc_group_deltas);
1362 static void
1363 userquota_update_cache(avl_tree_t *avl, uint64_t id, int64_t delta)
1365 userquota_node_t search = { .uqn_id = id };
1366 avl_index_t idx;
1368 userquota_node_t *uqn = avl_find(avl, &search, &idx);
1369 if (uqn == NULL) {
1370 uqn = kmem_zalloc(sizeof (*uqn), KM_SLEEP);
1371 uqn->uqn_id = id;
1372 avl_insert(avl, uqn, idx);
1374 uqn->uqn_delta += delta;
1377 static void
1378 do_userquota_update(userquota_cache_t *cache, uint64_t used, uint64_t flags,
1379 uint64_t user, uint64_t group, boolean_t subtract)
1381 if ((flags & DNODE_FLAG_USERUSED_ACCOUNTED)) {
1382 int64_t delta = DNODE_SIZE + used;
1383 if (subtract)
1384 delta = -delta;
1386 userquota_update_cache(&cache->uqc_user_deltas, user, delta);
1387 userquota_update_cache(&cache->uqc_group_deltas, group, delta);
1391 typedef struct userquota_updates_arg {
1392 objset_t *uua_os;
1393 int uua_sublist_idx;
1394 dmu_tx_t *uua_tx;
1395 } userquota_updates_arg_t;
1397 static void
1398 userquota_updates_task(void *arg)
1400 userquota_updates_arg_t *uua = arg;
1401 objset_t *os = uua->uua_os;
1402 dmu_tx_t *tx = uua->uua_tx;
1403 dnode_t *dn;
1404 userquota_cache_t cache = { 0 };
1406 multilist_sublist_t *list =
1407 multilist_sublist_lock(os->os_synced_dnodes, uua->uua_sublist_idx);
1409 ASSERT(multilist_sublist_head(list) == NULL ||
1410 dmu_objset_userused_enabled(os));
1411 avl_create(&cache.uqc_user_deltas, userquota_compare,
1412 sizeof (userquota_node_t), offsetof(userquota_node_t, uqn_node));
1413 avl_create(&cache.uqc_group_deltas, userquota_compare,
1414 sizeof (userquota_node_t), offsetof(userquota_node_t, uqn_node));
1416 while ((dn = multilist_sublist_head(list)) != NULL) {
1417 int flags;
1418 ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object));
1419 ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE ||
1420 dn->dn_phys->dn_flags &
1421 DNODE_FLAG_USERUSED_ACCOUNTED);
1423 flags = dn->dn_id_flags;
1424 ASSERT(flags);
1425 if (flags & DN_ID_OLD_EXIST) {
1426 do_userquota_update(&cache,
1427 dn->dn_oldused, dn->dn_oldflags,
1428 dn->dn_olduid, dn->dn_oldgid, B_TRUE);
1430 if (flags & DN_ID_NEW_EXIST) {
1431 do_userquota_update(&cache,
1432 DN_USED_BYTES(dn->dn_phys),
1433 dn->dn_phys->dn_flags, dn->dn_newuid,
1434 dn->dn_newgid, B_FALSE);
1437 mutex_enter(&dn->dn_mtx);
1438 dn->dn_oldused = 0;
1439 dn->dn_oldflags = 0;
1440 if (dn->dn_id_flags & DN_ID_NEW_EXIST) {
1441 dn->dn_olduid = dn->dn_newuid;
1442 dn->dn_oldgid = dn->dn_newgid;
1443 dn->dn_id_flags |= DN_ID_OLD_EXIST;
1444 if (dn->dn_bonuslen == 0)
1445 dn->dn_id_flags |= DN_ID_CHKED_SPILL;
1446 else
1447 dn->dn_id_flags |= DN_ID_CHKED_BONUS;
1449 dn->dn_id_flags &= ~(DN_ID_NEW_EXIST);
1450 mutex_exit(&dn->dn_mtx);
1452 multilist_sublist_remove(list, dn);
1453 dnode_rele(dn, os->os_synced_dnodes);
1455 do_userquota_cacheflush(os, &cache, tx);
1456 multilist_sublist_unlock(list);
1457 kmem_free(uua, sizeof (*uua));
1460 void
1461 dmu_objset_do_userquota_updates(objset_t *os, dmu_tx_t *tx)
1463 if (!dmu_objset_userused_enabled(os))
1464 return;
1466 /* Allocate the user/groupused objects if necessary. */
1467 if (DMU_USERUSED_DNODE(os)->dn_type == DMU_OT_NONE) {
1468 VERIFY0(zap_create_claim(os,
1469 DMU_USERUSED_OBJECT,
1470 DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
1471 VERIFY0(zap_create_claim(os,
1472 DMU_GROUPUSED_OBJECT,
1473 DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
1476 for (int i = 0;
1477 i < multilist_get_num_sublists(os->os_synced_dnodes); i++) {
1478 userquota_updates_arg_t *uua =
1479 kmem_alloc(sizeof (*uua), KM_SLEEP);
1480 uua->uua_os = os;
1481 uua->uua_sublist_idx = i;
1482 uua->uua_tx = tx;
1483 /* note: caller does taskq_wait() */
1484 (void) taskq_dispatch(dmu_objset_pool(os)->dp_sync_taskq,
1485 userquota_updates_task, uua, 0);
1486 /* callback frees uua */
1491 * Returns a pointer to data to find uid/gid from
1493 * If a dirty record for transaction group that is syncing can't
1494 * be found then NULL is returned. In the NULL case it is assumed
1495 * the uid/gid aren't changing.
1497 static void *
1498 dmu_objset_userquota_find_data(dmu_buf_impl_t *db, dmu_tx_t *tx)
1500 dbuf_dirty_record_t *dr, **drp;
1501 void *data;
1503 if (db->db_dirtycnt == 0)
1504 return (db->db.db_data); /* Nothing is changing */
1506 for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next)
1507 if (dr->dr_txg == tx->tx_txg)
1508 break;
1510 if (dr == NULL) {
1511 data = NULL;
1512 } else {
1513 dnode_t *dn;
1515 DB_DNODE_ENTER(dr->dr_dbuf);
1516 dn = DB_DNODE(dr->dr_dbuf);
1518 if (dn->dn_bonuslen == 0 &&
1519 dr->dr_dbuf->db_blkid == DMU_SPILL_BLKID)
1520 data = dr->dt.dl.dr_data->b_data;
1521 else
1522 data = dr->dt.dl.dr_data;
1524 DB_DNODE_EXIT(dr->dr_dbuf);
1527 return (data);
1530 void
1531 dmu_objset_userquota_get_ids(dnode_t *dn, boolean_t before, dmu_tx_t *tx)
1533 objset_t *os = dn->dn_objset;
1534 void *data = NULL;
1535 dmu_buf_impl_t *db = NULL;
1536 uint64_t *user = NULL;
1537 uint64_t *group = NULL;
1538 int flags = dn->dn_id_flags;
1539 int error;
1540 boolean_t have_spill = B_FALSE;
1542 if (!dmu_objset_userused_enabled(dn->dn_objset))
1543 return;
1545 if (before && (flags & (DN_ID_CHKED_BONUS|DN_ID_OLD_EXIST|
1546 DN_ID_CHKED_SPILL)))
1547 return;
1549 if (before && dn->dn_bonuslen != 0)
1550 data = DN_BONUS(dn->dn_phys);
1551 else if (!before && dn->dn_bonuslen != 0) {
1552 if (dn->dn_bonus) {
1553 db = dn->dn_bonus;
1554 mutex_enter(&db->db_mtx);
1555 data = dmu_objset_userquota_find_data(db, tx);
1556 } else {
1557 data = DN_BONUS(dn->dn_phys);
1559 } else if (dn->dn_bonuslen == 0 && dn->dn_bonustype == DMU_OT_SA) {
1560 int rf = 0;
1562 if (RW_WRITE_HELD(&dn->dn_struct_rwlock))
1563 rf |= DB_RF_HAVESTRUCT;
1564 error = dmu_spill_hold_by_dnode(dn,
1565 rf | DB_RF_MUST_SUCCEED,
1566 FTAG, (dmu_buf_t **)&db);
1567 ASSERT(error == 0);
1568 mutex_enter(&db->db_mtx);
1569 data = (before) ? db->db.db_data :
1570 dmu_objset_userquota_find_data(db, tx);
1571 have_spill = B_TRUE;
1572 } else {
1573 mutex_enter(&dn->dn_mtx);
1574 dn->dn_id_flags |= DN_ID_CHKED_BONUS;
1575 mutex_exit(&dn->dn_mtx);
1576 return;
1579 if (before) {
1580 ASSERT(data);
1581 user = &dn->dn_olduid;
1582 group = &dn->dn_oldgid;
1583 } else if (data) {
1584 user = &dn->dn_newuid;
1585 group = &dn->dn_newgid;
1589 * Must always call the callback in case the object
1590 * type has changed and that type isn't an object type to track
1592 error = used_cbs[os->os_phys->os_type](dn->dn_bonustype, data,
1593 user, group);
1596 * Preserve existing uid/gid when the callback can't determine
1597 * what the new uid/gid are and the callback returned EEXIST.
1598 * The EEXIST error tells us to just use the existing uid/gid.
1599 * If we don't know what the old values are then just assign
1600 * them to 0, since that is a new file being created.
1602 if (!before && data == NULL && error == EEXIST) {
1603 if (flags & DN_ID_OLD_EXIST) {
1604 dn->dn_newuid = dn->dn_olduid;
1605 dn->dn_newgid = dn->dn_oldgid;
1606 } else {
1607 dn->dn_newuid = 0;
1608 dn->dn_newgid = 0;
1610 error = 0;
1613 if (db)
1614 mutex_exit(&db->db_mtx);
1616 mutex_enter(&dn->dn_mtx);
1617 if (error == 0 && before)
1618 dn->dn_id_flags |= DN_ID_OLD_EXIST;
1619 if (error == 0 && !before)
1620 dn->dn_id_flags |= DN_ID_NEW_EXIST;
1622 if (have_spill) {
1623 dn->dn_id_flags |= DN_ID_CHKED_SPILL;
1624 } else {
1625 dn->dn_id_flags |= DN_ID_CHKED_BONUS;
1627 mutex_exit(&dn->dn_mtx);
1628 if (have_spill)
1629 dmu_buf_rele((dmu_buf_t *)db, FTAG);
1632 boolean_t
1633 dmu_objset_userspace_present(objset_t *os)
1635 return (os->os_phys->os_flags &
1636 OBJSET_FLAG_USERACCOUNTING_COMPLETE);
1640 dmu_objset_userspace_upgrade(objset_t *os)
1642 uint64_t obj;
1643 int err = 0;
1645 if (dmu_objset_userspace_present(os))
1646 return (0);
1647 if (!dmu_objset_userused_enabled(os))
1648 return (SET_ERROR(ENOTSUP));
1649 if (dmu_objset_is_snapshot(os))
1650 return (SET_ERROR(EINVAL));
1653 * We simply need to mark every object dirty, so that it will be
1654 * synced out and now accounted. If this is called
1655 * concurrently, or if we already did some work before crashing,
1656 * that's fine, since we track each object's accounted state
1657 * independently.
1660 for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
1661 dmu_tx_t *tx;
1662 dmu_buf_t *db;
1663 int objerr;
1665 if (issig(JUSTLOOKING) && issig(FORREAL))
1666 return (SET_ERROR(EINTR));
1668 objerr = dmu_bonus_hold(os, obj, FTAG, &db);
1669 if (objerr != 0)
1670 continue;
1671 tx = dmu_tx_create(os);
1672 dmu_tx_hold_bonus(tx, obj);
1673 objerr = dmu_tx_assign(tx, TXG_WAIT);
1674 if (objerr != 0) {
1675 dmu_tx_abort(tx);
1676 continue;
1678 dmu_buf_will_dirty(db, tx);
1679 dmu_buf_rele(db, FTAG);
1680 dmu_tx_commit(tx);
1683 os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
1684 txg_wait_synced(dmu_objset_pool(os), 0);
1685 return (0);
1688 void
1689 dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp,
1690 uint64_t *usedobjsp, uint64_t *availobjsp)
1692 dsl_dataset_space(os->os_dsl_dataset, refdbytesp, availbytesp,
1693 usedobjsp, availobjsp);
1696 uint64_t
1697 dmu_objset_fsid_guid(objset_t *os)
1699 return (dsl_dataset_fsid_guid(os->os_dsl_dataset));
1702 void
1703 dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat)
1705 stat->dds_type = os->os_phys->os_type;
1706 if (os->os_dsl_dataset)
1707 dsl_dataset_fast_stat(os->os_dsl_dataset, stat);
1710 void
1711 dmu_objset_stats(objset_t *os, nvlist_t *nv)
1713 ASSERT(os->os_dsl_dataset ||
1714 os->os_phys->os_type == DMU_OST_META);
1716 if (os->os_dsl_dataset != NULL)
1717 dsl_dataset_stats(os->os_dsl_dataset, nv);
1719 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE,
1720 os->os_phys->os_type);
1721 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERACCOUNTING,
1722 dmu_objset_userspace_present(os));
1726 dmu_objset_is_snapshot(objset_t *os)
1728 if (os->os_dsl_dataset != NULL)
1729 return (os->os_dsl_dataset->ds_is_snapshot);
1730 else
1731 return (B_FALSE);
1735 dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen,
1736 boolean_t *conflict)
1738 dsl_dataset_t *ds = os->os_dsl_dataset;
1739 uint64_t ignored;
1741 if (dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0)
1742 return (SET_ERROR(ENOENT));
1744 return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset,
1745 dsl_dataset_phys(ds)->ds_snapnames_zapobj, name, 8, 1, &ignored,
1746 MT_NORMALIZE, real, maxlen, conflict));
1750 dmu_snapshot_list_next(objset_t *os, int namelen, char *name,
1751 uint64_t *idp, uint64_t *offp, boolean_t *case_conflict)
1753 dsl_dataset_t *ds = os->os_dsl_dataset;
1754 zap_cursor_t cursor;
1755 zap_attribute_t attr;
1757 ASSERT(dsl_pool_config_held(dmu_objset_pool(os)));
1759 if (dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0)
1760 return (SET_ERROR(ENOENT));
1762 zap_cursor_init_serialized(&cursor,
1763 ds->ds_dir->dd_pool->dp_meta_objset,
1764 dsl_dataset_phys(ds)->ds_snapnames_zapobj, *offp);
1766 if (zap_cursor_retrieve(&cursor, &attr) != 0) {
1767 zap_cursor_fini(&cursor);
1768 return (SET_ERROR(ENOENT));
1771 if (strlen(attr.za_name) + 1 > namelen) {
1772 zap_cursor_fini(&cursor);
1773 return (SET_ERROR(ENAMETOOLONG));
1776 (void) strcpy(name, attr.za_name);
1777 if (idp)
1778 *idp = attr.za_first_integer;
1779 if (case_conflict)
1780 *case_conflict = attr.za_normalization_conflict;
1781 zap_cursor_advance(&cursor);
1782 *offp = zap_cursor_serialize(&cursor);
1783 zap_cursor_fini(&cursor);
1785 return (0);
1789 dmu_dir_list_next(objset_t *os, int namelen, char *name,
1790 uint64_t *idp, uint64_t *offp)
1792 dsl_dir_t *dd = os->os_dsl_dataset->ds_dir;
1793 zap_cursor_t cursor;
1794 zap_attribute_t attr;
1796 /* there is no next dir on a snapshot! */
1797 if (os->os_dsl_dataset->ds_object !=
1798 dsl_dir_phys(dd)->dd_head_dataset_obj)
1799 return (SET_ERROR(ENOENT));
1801 zap_cursor_init_serialized(&cursor,
1802 dd->dd_pool->dp_meta_objset,
1803 dsl_dir_phys(dd)->dd_child_dir_zapobj, *offp);
1805 if (zap_cursor_retrieve(&cursor, &attr) != 0) {
1806 zap_cursor_fini(&cursor);
1807 return (SET_ERROR(ENOENT));
1810 if (strlen(attr.za_name) + 1 > namelen) {
1811 zap_cursor_fini(&cursor);
1812 return (SET_ERROR(ENAMETOOLONG));
1815 (void) strcpy(name, attr.za_name);
1816 if (idp)
1817 *idp = attr.za_first_integer;
1818 zap_cursor_advance(&cursor);
1819 *offp = zap_cursor_serialize(&cursor);
1820 zap_cursor_fini(&cursor);
1822 return (0);
1825 typedef struct dmu_objset_find_ctx {
1826 taskq_t *dc_tq;
1827 dsl_pool_t *dc_dp;
1828 uint64_t dc_ddobj;
1829 char *dc_ddname; /* last component of ddobj's name */
1830 int (*dc_func)(dsl_pool_t *, dsl_dataset_t *, void *);
1831 void *dc_arg;
1832 int dc_flags;
1833 kmutex_t *dc_error_lock;
1834 int *dc_error;
1835 } dmu_objset_find_ctx_t;
1837 static void
1838 dmu_objset_find_dp_impl(dmu_objset_find_ctx_t *dcp)
1840 dsl_pool_t *dp = dcp->dc_dp;
1841 dsl_dir_t *dd;
1842 dsl_dataset_t *ds;
1843 zap_cursor_t zc;
1844 zap_attribute_t *attr;
1845 uint64_t thisobj;
1846 int err = 0;
1848 /* don't process if there already was an error */
1849 if (*dcp->dc_error != 0)
1850 goto out;
1853 * Note: passing the name (dc_ddname) here is optional, but it
1854 * improves performance because we don't need to call
1855 * zap_value_search() to determine the name.
1857 err = dsl_dir_hold_obj(dp, dcp->dc_ddobj, dcp->dc_ddname, FTAG, &dd);
1858 if (err != 0)
1859 goto out;
1861 /* Don't visit hidden ($MOS & $ORIGIN) objsets. */
1862 if (dd->dd_myname[0] == '$') {
1863 dsl_dir_rele(dd, FTAG);
1864 goto out;
1867 thisobj = dsl_dir_phys(dd)->dd_head_dataset_obj;
1868 attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
1871 * Iterate over all children.
1873 if (dcp->dc_flags & DS_FIND_CHILDREN) {
1874 for (zap_cursor_init(&zc, dp->dp_meta_objset,
1875 dsl_dir_phys(dd)->dd_child_dir_zapobj);
1876 zap_cursor_retrieve(&zc, attr) == 0;
1877 (void) zap_cursor_advance(&zc)) {
1878 ASSERT3U(attr->za_integer_length, ==,
1879 sizeof (uint64_t));
1880 ASSERT3U(attr->za_num_integers, ==, 1);
1882 dmu_objset_find_ctx_t *child_dcp =
1883 kmem_alloc(sizeof (*child_dcp), KM_SLEEP);
1884 *child_dcp = *dcp;
1885 child_dcp->dc_ddobj = attr->za_first_integer;
1886 child_dcp->dc_ddname = spa_strdup(attr->za_name);
1887 if (dcp->dc_tq != NULL)
1888 (void) taskq_dispatch(dcp->dc_tq,
1889 dmu_objset_find_dp_cb, child_dcp, TQ_SLEEP);
1890 else
1891 dmu_objset_find_dp_impl(child_dcp);
1893 zap_cursor_fini(&zc);
1897 * Iterate over all snapshots.
1899 if (dcp->dc_flags & DS_FIND_SNAPSHOTS) {
1900 dsl_dataset_t *ds;
1901 err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
1903 if (err == 0) {
1904 uint64_t snapobj;
1906 snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
1907 dsl_dataset_rele(ds, FTAG);
1909 for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
1910 zap_cursor_retrieve(&zc, attr) == 0;
1911 (void) zap_cursor_advance(&zc)) {
1912 ASSERT3U(attr->za_integer_length, ==,
1913 sizeof (uint64_t));
1914 ASSERT3U(attr->za_num_integers, ==, 1);
1916 err = dsl_dataset_hold_obj(dp,
1917 attr->za_first_integer, FTAG, &ds);
1918 if (err != 0)
1919 break;
1920 err = dcp->dc_func(dp, ds, dcp->dc_arg);
1921 dsl_dataset_rele(ds, FTAG);
1922 if (err != 0)
1923 break;
1925 zap_cursor_fini(&zc);
1929 kmem_free(attr, sizeof (zap_attribute_t));
1931 if (err != 0) {
1932 dsl_dir_rele(dd, FTAG);
1933 goto out;
1937 * Apply to self.
1939 err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
1942 * Note: we hold the dir while calling dsl_dataset_hold_obj() so
1943 * that the dir will remain cached, and we won't have to re-instantiate
1944 * it (which could be expensive due to finding its name via
1945 * zap_value_search()).
1947 dsl_dir_rele(dd, FTAG);
1948 if (err != 0)
1949 goto out;
1950 err = dcp->dc_func(dp, ds, dcp->dc_arg);
1951 dsl_dataset_rele(ds, FTAG);
1953 out:
1954 if (err != 0) {
1955 mutex_enter(dcp->dc_error_lock);
1956 /* only keep first error */
1957 if (*dcp->dc_error == 0)
1958 *dcp->dc_error = err;
1959 mutex_exit(dcp->dc_error_lock);
1962 if (dcp->dc_ddname != NULL)
1963 spa_strfree(dcp->dc_ddname);
1964 kmem_free(dcp, sizeof (*dcp));
1967 static void
1968 dmu_objset_find_dp_cb(void *arg)
1970 dmu_objset_find_ctx_t *dcp = arg;
1971 dsl_pool_t *dp = dcp->dc_dp;
1974 * We need to get a pool_config_lock here, as there are several
1975 * asssert(pool_config_held) down the stack. Getting a lock via
1976 * dsl_pool_config_enter is risky, as it might be stalled by a
1977 * pending writer. This would deadlock, as the write lock can
1978 * only be granted when our parent thread gives up the lock.
1979 * The _prio interface gives us priority over a pending writer.
1981 dsl_pool_config_enter_prio(dp, FTAG);
1983 dmu_objset_find_dp_impl(dcp);
1985 dsl_pool_config_exit(dp, FTAG);
1989 * Find objsets under and including ddobj, call func(ds) on each.
1990 * The order for the enumeration is completely undefined.
1991 * func is called with dsl_pool_config held.
1994 dmu_objset_find_dp(dsl_pool_t *dp, uint64_t ddobj,
1995 int func(dsl_pool_t *, dsl_dataset_t *, void *), void *arg, int flags)
1997 int error = 0;
1998 taskq_t *tq = NULL;
1999 int ntasks;
2000 dmu_objset_find_ctx_t *dcp;
2001 kmutex_t err_lock;
2003 mutex_init(&err_lock, NULL, MUTEX_DEFAULT, NULL);
2004 dcp = kmem_alloc(sizeof (*dcp), KM_SLEEP);
2005 dcp->dc_tq = NULL;
2006 dcp->dc_dp = dp;
2007 dcp->dc_ddobj = ddobj;
2008 dcp->dc_ddname = NULL;
2009 dcp->dc_func = func;
2010 dcp->dc_arg = arg;
2011 dcp->dc_flags = flags;
2012 dcp->dc_error_lock = &err_lock;
2013 dcp->dc_error = &error;
2015 if ((flags & DS_FIND_SERIALIZE) || dsl_pool_config_held_writer(dp)) {
2017 * In case a write lock is held we can't make use of
2018 * parallelism, as down the stack of the worker threads
2019 * the lock is asserted via dsl_pool_config_held.
2020 * In case of a read lock this is solved by getting a read
2021 * lock in each worker thread, which isn't possible in case
2022 * of a writer lock. So we fall back to the synchronous path
2023 * here.
2024 * In the future it might be possible to get some magic into
2025 * dsl_pool_config_held in a way that it returns true for
2026 * the worker threads so that a single lock held from this
2027 * thread suffices. For now, stay single threaded.
2029 dmu_objset_find_dp_impl(dcp);
2030 mutex_destroy(&err_lock);
2032 return (error);
2035 ntasks = dmu_find_threads;
2036 if (ntasks == 0)
2037 ntasks = vdev_count_leaves(dp->dp_spa) * 4;
2038 tq = taskq_create("dmu_objset_find", ntasks, minclsyspri, ntasks,
2039 INT_MAX, 0);
2040 if (tq == NULL) {
2041 kmem_free(dcp, sizeof (*dcp));
2042 mutex_destroy(&err_lock);
2044 return (SET_ERROR(ENOMEM));
2046 dcp->dc_tq = tq;
2048 /* dcp will be freed by task */
2049 (void) taskq_dispatch(tq, dmu_objset_find_dp_cb, dcp, TQ_SLEEP);
2052 * PORTING: this code relies on the property of taskq_wait to wait
2053 * until no more tasks are queued and no more tasks are active. As
2054 * we always queue new tasks from within other tasks, task_wait
2055 * reliably waits for the full recursion to finish, even though we
2056 * enqueue new tasks after taskq_wait has been called.
2057 * On platforms other than illumos, taskq_wait may not have this
2058 * property.
2060 taskq_wait(tq);
2061 taskq_destroy(tq);
2062 mutex_destroy(&err_lock);
2064 return (error);
2068 * Find all objsets under name, and for each, call 'func(child_name, arg)'.
2069 * The dp_config_rwlock must not be held when this is called, and it
2070 * will not be held when the callback is called.
2071 * Therefore this function should only be used when the pool is not changing
2072 * (e.g. in syncing context), or the callback can deal with the possible races.
2074 static int
2075 dmu_objset_find_impl(spa_t *spa, const char *name,
2076 int func(const char *, void *), void *arg, int flags)
2078 dsl_dir_t *dd;
2079 dsl_pool_t *dp = spa_get_dsl(spa);
2080 dsl_dataset_t *ds;
2081 zap_cursor_t zc;
2082 zap_attribute_t *attr;
2083 char *child;
2084 uint64_t thisobj;
2085 int err;
2087 dsl_pool_config_enter(dp, FTAG);
2089 err = dsl_dir_hold(dp, name, FTAG, &dd, NULL);
2090 if (err != 0) {
2091 dsl_pool_config_exit(dp, FTAG);
2092 return (err);
2095 /* Don't visit hidden ($MOS & $ORIGIN) objsets. */
2096 if (dd->dd_myname[0] == '$') {
2097 dsl_dir_rele(dd, FTAG);
2098 dsl_pool_config_exit(dp, FTAG);
2099 return (0);
2102 thisobj = dsl_dir_phys(dd)->dd_head_dataset_obj;
2103 attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
2106 * Iterate over all children.
2108 if (flags & DS_FIND_CHILDREN) {
2109 for (zap_cursor_init(&zc, dp->dp_meta_objset,
2110 dsl_dir_phys(dd)->dd_child_dir_zapobj);
2111 zap_cursor_retrieve(&zc, attr) == 0;
2112 (void) zap_cursor_advance(&zc)) {
2113 ASSERT3U(attr->za_integer_length, ==,
2114 sizeof (uint64_t));
2115 ASSERT3U(attr->za_num_integers, ==, 1);
2117 child = kmem_asprintf("%s/%s", name, attr->za_name);
2118 dsl_pool_config_exit(dp, FTAG);
2119 err = dmu_objset_find_impl(spa, child,
2120 func, arg, flags);
2121 dsl_pool_config_enter(dp, FTAG);
2122 strfree(child);
2123 if (err != 0)
2124 break;
2126 zap_cursor_fini(&zc);
2128 if (err != 0) {
2129 dsl_dir_rele(dd, FTAG);
2130 dsl_pool_config_exit(dp, FTAG);
2131 kmem_free(attr, sizeof (zap_attribute_t));
2132 return (err);
2137 * Iterate over all snapshots.
2139 if (flags & DS_FIND_SNAPSHOTS) {
2140 err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
2142 if (err == 0) {
2143 uint64_t snapobj;
2145 snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
2146 dsl_dataset_rele(ds, FTAG);
2148 for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
2149 zap_cursor_retrieve(&zc, attr) == 0;
2150 (void) zap_cursor_advance(&zc)) {
2151 ASSERT3U(attr->za_integer_length, ==,
2152 sizeof (uint64_t));
2153 ASSERT3U(attr->za_num_integers, ==, 1);
2155 child = kmem_asprintf("%s@%s",
2156 name, attr->za_name);
2157 dsl_pool_config_exit(dp, FTAG);
2158 err = func(child, arg);
2159 dsl_pool_config_enter(dp, FTAG);
2160 strfree(child);
2161 if (err != 0)
2162 break;
2164 zap_cursor_fini(&zc);
2168 dsl_dir_rele(dd, FTAG);
2169 kmem_free(attr, sizeof (zap_attribute_t));
2170 dsl_pool_config_exit(dp, FTAG);
2172 if (err != 0)
2173 return (err);
2175 /* Apply to self. */
2176 return (func(name, arg));
2180 * See comment above dmu_objset_find_impl().
2183 dmu_objset_find(char *name, int func(const char *, void *), void *arg,
2184 int flags)
2186 spa_t *spa;
2187 int error;
2189 error = spa_open(name, &spa, FTAG);
2190 if (error != 0)
2191 return (error);
2192 error = dmu_objset_find_impl(spa, name, func, arg, flags);
2193 spa_close(spa, FTAG);
2194 return (error);
2197 void
2198 dmu_objset_set_user(objset_t *os, void *user_ptr)
2200 ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
2201 os->os_user_ptr = user_ptr;
2204 void *
2205 dmu_objset_get_user(objset_t *os)
2207 ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
2208 return (os->os_user_ptr);
2212 * Determine name of filesystem, given name of snapshot.
2213 * buf must be at least ZFS_MAX_DATASET_NAME_LEN bytes
2216 dmu_fsname(const char *snapname, char *buf)
2218 char *atp = strchr(snapname, '@');
2219 if (atp == NULL)
2220 return (SET_ERROR(EINVAL));
2221 if (atp - snapname >= ZFS_MAX_DATASET_NAME_LEN)
2222 return (SET_ERROR(ENAMETOOLONG));
2223 (void) strlcpy(buf, snapname, atp - snapname + 1);
2224 return (0);
2228 * Call when we think we're going to write/free space in open context to track
2229 * the amount of dirty data in the open txg, which is also the amount
2230 * of memory that can not be evicted until this txg syncs.
2232 void
2233 dmu_objset_willuse_space(objset_t *os, int64_t space, dmu_tx_t *tx)
2235 dsl_dataset_t *ds = os->os_dsl_dataset;
2236 int64_t aspace = spa_get_worst_case_asize(os->os_spa, space);
2238 if (ds != NULL) {
2239 dsl_dir_willuse_space(ds->ds_dir, aspace, tx);
2240 dsl_pool_dirty_space(dmu_tx_pool(tx), space, tx);