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