xref: /illumos-gate/usr/src/uts/common/fs/zfs/sys/refcount.h (revision eb633035c80613ec93d62f90482837adaaf21a0a)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21fa9e4066Sahrens /*
223f9d6ad7SLin Ling  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
230f7643c7SGeorge Wilson  * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
24fa9e4066Sahrens  */
25fa9e4066Sahrens 
26fa9e4066Sahrens #ifndef	_SYS_REFCOUNT_H
27fa9e4066Sahrens #define	_SYS_REFCOUNT_H
28fa9e4066Sahrens 
29fa9e4066Sahrens #include <sys/inttypes.h>
30fa9e4066Sahrens #include <sys/list.h>
31fa9e4066Sahrens #include <sys/zfs_context.h>
32fa9e4066Sahrens 
33fa9e4066Sahrens #ifdef	__cplusplus
34fa9e4066Sahrens extern "C" {
35fa9e4066Sahrens #endif
36fa9e4066Sahrens 
37fa9e4066Sahrens /*
38fa9e4066Sahrens  * If the reference is held only by the calling function and not any
39fa9e4066Sahrens  * particular object, use FTAG (which is a string) for the holder_tag.
40fa9e4066Sahrens  * Otherwise, use the object that holds the reference.
41fa9e4066Sahrens  */
423f7978d0SAlan Somers #define	FTAG ((char *)(uintptr_t)__func__)
43fa9e4066Sahrens 
44744947dcSTom Erickson #ifdef	ZFS_DEBUG
45fa9e4066Sahrens typedef struct reference {
46fa9e4066Sahrens 	list_node_t ref_link;
47fa9e4066Sahrens 	void *ref_holder;
48fa9e4066Sahrens 	uint64_t ref_number;
49fa9e4066Sahrens 	uint8_t *ref_removed;
50fa9e4066Sahrens } reference_t;
51fa9e4066Sahrens 
52fa9e4066Sahrens typedef struct refcount {
53fa9e4066Sahrens 	kmutex_t rc_mtx;
543b2aab18SMatthew Ahrens 	boolean_t rc_tracked;
55fa9e4066Sahrens 	list_t rc_list;
56fa9e4066Sahrens 	list_t rc_removed;
5728e4da25SMatthew Ahrens 	uint64_t rc_count;
5828e4da25SMatthew Ahrens 	uint64_t rc_removed_count;
59e914ace2STim Schumacher } zfs_refcount_t;
60e914ace2STim Schumacher 
61e914ace2STim Schumacher /*
62e914ace2STim Schumacher  * Note: zfs_refcount_t must be initialized with
63e914ace2STim Schumacher  * refcount_create[_untracked]()
64e914ace2STim Schumacher  */
65e914ace2STim Schumacher 
66e914ace2STim Schumacher void zfs_refcount_create(zfs_refcount_t *);
67e914ace2STim Schumacher void zfs_refcount_create_untracked(zfs_refcount_t *);
68e914ace2STim Schumacher void zfs_refcount_create_tracked(zfs_refcount_t *);
69e914ace2STim Schumacher void zfs_refcount_destroy(zfs_refcount_t *);
70e914ace2STim Schumacher void zfs_refcount_destroy_many(zfs_refcount_t *, uint64_t);
71e914ace2STim Schumacher int zfs_refcount_is_zero(zfs_refcount_t *);
72e914ace2STim Schumacher int64_t zfs_refcount_count(zfs_refcount_t *);
73e914ace2STim Schumacher int64_t zfs_refcount_add(zfs_refcount_t *, void *);
74e914ace2STim Schumacher int64_t zfs_refcount_remove(zfs_refcount_t *, void *);
75e914ace2STim Schumacher int64_t zfs_refcount_add_many(zfs_refcount_t *, uint64_t, void *);
76e914ace2STim Schumacher int64_t zfs_refcount_remove_many(zfs_refcount_t *, uint64_t, void *);
77e914ace2STim Schumacher void zfs_refcount_transfer(zfs_refcount_t *, zfs_refcount_t *);
78e914ace2STim Schumacher void zfs_refcount_transfer_ownership(zfs_refcount_t *, void *, void *);
79*eb633035STom Caputi void zfs_refcount_transfer_ownership_many(zfs_refcount_t *, uint64_t,
80*eb633035STom Caputi     void *, void *);
81e914ace2STim Schumacher boolean_t zfs_refcount_held(zfs_refcount_t *, void *);
82e914ace2STim Schumacher boolean_t zfs_refcount_not_held(zfs_refcount_t *, void *);
83e914ace2STim Schumacher 
84e914ace2STim Schumacher void zfs_refcount_init(void);
85e914ace2STim Schumacher void zfs_refcount_fini(void);
86fa9e4066Sahrens 
87744947dcSTom Erickson #else	/* ZFS_DEBUG */
88fa9e4066Sahrens 
89fa9e4066Sahrens typedef struct refcount {
90fa9e4066Sahrens 	uint64_t rc_count;
91e914ace2STim Schumacher } zfs_refcount_t;
92e914ace2STim Schumacher 
93e914ace2STim Schumacher #define	zfs_refcount_create(rc) ((rc)->rc_count = 0)
94e914ace2STim Schumacher #define	zfs_refcount_create_untracked(rc) ((rc)->rc_count = 0)
95e914ace2STim Schumacher #define	zfs_refcount_create_tracked(rc) ((rc)->rc_count = 0)
96e914ace2STim Schumacher #define	zfs_refcount_destroy(rc) ((rc)->rc_count = 0)
97e914ace2STim Schumacher #define	zfs_refcount_destroy_many(rc, number) ((rc)->rc_count = 0)
98e914ace2STim Schumacher #define	zfs_refcount_is_zero(rc) ((rc)->rc_count == 0)
99e914ace2STim Schumacher #define	zfs_refcount_count(rc) ((rc)->rc_count)
100e914ace2STim Schumacher #define	zfs_refcount_add(rc, holder) atomic_inc_64_nv(&(rc)->rc_count)
101e914ace2STim Schumacher #define	zfs_refcount_remove(rc, holder) atomic_dec_64_nv(&(rc)->rc_count)
102e914ace2STim Schumacher #define	zfs_refcount_add_many(rc, number, holder) \
103fa9e4066Sahrens 	atomic_add_64_nv(&(rc)->rc_count, number)
104e914ace2STim Schumacher #define	zfs_refcount_remove_many(rc, number, holder) \
105fa9e4066Sahrens 	atomic_add_64_nv(&(rc)->rc_count, -number)
106e914ace2STim Schumacher #define	zfs_refcount_transfer(dst, src) { \
1073f9d6ad7SLin Ling 	uint64_t __tmp = (src)->rc_count; \
1083f9d6ad7SLin Ling 	atomic_add_64(&(src)->rc_count, -__tmp); \
1093f9d6ad7SLin Ling 	atomic_add_64(&(dst)->rc_count, __tmp); \
1103f9d6ad7SLin Ling }
111e914ace2STim Schumacher #define	zfs_refcount_transfer_ownership(rc, current_holder, new_holder)	(void)0
112*eb633035STom Caputi #define	zfs_refcount_transfer_ownership_many(rc, nr, ch, nh)	((void)0)
113e914ace2STim Schumacher #define	zfs_refcount_held(rc, holder)		((rc)->rc_count > 0)
114e914ace2STim Schumacher #define	zfs_refcount_not_held(rc, holder)		(B_TRUE)
115fa9e4066Sahrens 
116e914ace2STim Schumacher #define	zfs_refcount_init()
117e914ace2STim Schumacher #define	zfs_refcount_fini()
118fa9e4066Sahrens 
119744947dcSTom Erickson #endif	/* ZFS_DEBUG */
120fa9e4066Sahrens 
121fa9e4066Sahrens #ifdef	__cplusplus
122fa9e4066Sahrens }
123fa9e4066Sahrens #endif
124fa9e4066Sahrens 
125fa9e4066Sahrens #endif /* _SYS_REFCOUNT_H */
126