Add warning for external consumers of dmu_tx_callback_register
[zfs.git] / include / sys / zfs_refcount.h
blob77965a0aa58029f335f11cc6928b5099e4e54288
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 https://opensource.org/licenses/CDDL-1.0.
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
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
26 #ifndef _SYS_ZFS_REFCOUNT_H
27 #define _SYS_ZFS_REFCOUNT_H
29 #include <sys/inttypes.h>
30 #include <sys/avl.h>
31 #include <sys/list.h>
32 #include <sys/zfs_context.h>
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
39 * If the reference is held only by the calling function and not any
40 * particular object, use FTAG (which is a string) for the holder_tag.
41 * Otherwise, use the object that holds the reference.
43 #define FTAG ((char *)(uintptr_t)__func__)
45 #ifdef ZFS_DEBUG
46 typedef struct reference {
47 union {
48 avl_node_t a;
49 list_node_t l;
50 } ref_link;
51 const void *ref_holder;
52 uint64_t ref_number;
53 boolean_t ref_search;
54 } reference_t;
56 typedef struct refcount {
57 uint64_t rc_count;
58 kmutex_t rc_mtx;
59 avl_tree_t rc_tree;
60 list_t rc_removed;
61 uint_t rc_removed_count;
62 boolean_t rc_tracked;
63 } zfs_refcount_t;
66 * Note: zfs_refcount_t must be initialized with
67 * refcount_create[_untracked]()
70 void zfs_refcount_create(zfs_refcount_t *);
71 void zfs_refcount_create_untracked(zfs_refcount_t *);
72 void zfs_refcount_create_tracked(zfs_refcount_t *);
73 void zfs_refcount_destroy(zfs_refcount_t *);
74 void zfs_refcount_destroy_many(zfs_refcount_t *, uint64_t);
75 int zfs_refcount_is_zero(zfs_refcount_t *);
76 int64_t zfs_refcount_count(zfs_refcount_t *);
77 int64_t zfs_refcount_add(zfs_refcount_t *, const void *);
78 int64_t zfs_refcount_remove(zfs_refcount_t *, const void *);
80 * Note that (add|remove)_many adds/removes one reference with "number" N,
81 * _not_ N references with "number" 1, which is what (add|remove)_few does,
82 * or what vanilla zfs_refcount_(add|remove) called N times would do.
84 * Attempting to remove a reference with number N when none exists is a
85 * panic on debug kernels with reference_tracking enabled.
87 void zfs_refcount_add_few(zfs_refcount_t *, uint64_t, const void *);
88 void zfs_refcount_remove_few(zfs_refcount_t *, uint64_t, const void *);
89 int64_t zfs_refcount_add_many(zfs_refcount_t *, uint64_t, const void *);
90 int64_t zfs_refcount_remove_many(zfs_refcount_t *, uint64_t, const void *);
91 void zfs_refcount_transfer(zfs_refcount_t *, zfs_refcount_t *);
92 void zfs_refcount_transfer_ownership(zfs_refcount_t *, const void *,
93 const void *);
94 void zfs_refcount_transfer_ownership_many(zfs_refcount_t *, uint64_t,
95 const void *, const void *);
96 boolean_t zfs_refcount_held(zfs_refcount_t *, const void *);
97 boolean_t zfs_refcount_not_held(zfs_refcount_t *, const void *);
99 void zfs_refcount_init(void);
100 void zfs_refcount_fini(void);
102 #else /* ZFS_DEBUG */
104 typedef struct refcount {
105 uint64_t rc_count;
106 } zfs_refcount_t;
108 #define zfs_refcount_create(rc) ((rc)->rc_count = 0)
109 #define zfs_refcount_create_untracked(rc) ((rc)->rc_count = 0)
110 #define zfs_refcount_create_tracked(rc) ((rc)->rc_count = 0)
111 #define zfs_refcount_destroy(rc) ((rc)->rc_count = 0)
112 #define zfs_refcount_destroy_many(rc, number) ((rc)->rc_count = 0)
113 #define zfs_refcount_is_zero(rc) (zfs_refcount_count(rc) == 0)
114 #define zfs_refcount_count(rc) atomic_load_64(&(rc)->rc_count)
115 #define zfs_refcount_add(rc, holder) atomic_inc_64_nv(&(rc)->rc_count)
116 #define zfs_refcount_remove(rc, holder) atomic_dec_64_nv(&(rc)->rc_count)
117 #define zfs_refcount_add_few(rc, number, holder) \
118 atomic_add_64(&(rc)->rc_count, number)
119 #define zfs_refcount_remove_few(rc, number, holder) \
120 atomic_add_64(&(rc)->rc_count, -number)
121 #define zfs_refcount_add_many(rc, number, holder) \
122 atomic_add_64_nv(&(rc)->rc_count, number)
123 #define zfs_refcount_remove_many(rc, number, holder) \
124 atomic_add_64_nv(&(rc)->rc_count, -number)
125 #define zfs_refcount_transfer(dst, src) { \
126 uint64_t __tmp = zfs_refcount_count(src); \
127 atomic_add_64(&(src)->rc_count, -__tmp); \
128 atomic_add_64(&(dst)->rc_count, __tmp); \
130 #define zfs_refcount_transfer_ownership(rc, ch, nh) ((void)0)
131 #define zfs_refcount_transfer_ownership_many(rc, nr, ch, nh) ((void)0)
132 #define zfs_refcount_held(rc, holder) (zfs_refcount_count(rc) > 0)
133 #define zfs_refcount_not_held(rc, holder) (B_TRUE)
135 #define zfs_refcount_init()
136 #define zfs_refcount_fini()
138 #endif /* ZFS_DEBUG */
140 #ifdef __cplusplus
142 #endif
144 #endif /* _SYS_REFCOUNT_H */