1168404Spjd/*
2168404Spjd * CDDL HEADER START
3168404Spjd *
4168404Spjd * The contents of this file are subject to the terms of the
5168404Spjd * Common Development and Distribution License (the "License").
6168404Spjd * You may not use this file except in compliance with the License.
7168404Spjd *
8168404Spjd * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9168404Spjd * or http://www.opensolaris.org/os/licensing.
10168404Spjd * See the License for the specific language governing permissions
11168404Spjd * and limitations under the License.
12168404Spjd *
13168404Spjd * When distributing Covered Code, include this CDDL HEADER in each
14168404Spjd * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15168404Spjd * If applicable, add the following below this CDDL HEADER, with the
16168404Spjd * fields enclosed by brackets "[]" replaced with your own identifying
17168404Spjd * information: Portions Copyright [yyyy] [name of copyright owner]
18168404Spjd *
19168404Spjd * CDDL HEADER END
20168404Spjd */
21168404Spjd/*
22219089Spjd * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23307277Smav * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
24168404Spjd */
25168404Spjd
26168404Spjd#ifndef	_SYS_REFCOUNT_H
27168404Spjd#define	_SYS_REFCOUNT_H
28168404Spjd
29185029Spjd#include <sys/cdefs.h>
30185029Spjd#include <sys/types.h>
31179310Spjd#include_next <sys/refcount.h>
32168404Spjd#include <sys/list.h>
33168404Spjd#include <sys/zfs_context.h>
34168404Spjd
35168404Spjd#ifdef	__cplusplus
36168404Spjdextern "C" {
37168404Spjd#endif
38168404Spjd
39168404Spjd/*
40168404Spjd * If the reference is held only by the calling function and not any
41168404Spjd * particular object, use FTAG (which is a string) for the holder_tag.
42168404Spjd * Otherwise, use the object that holds the reference.
43168404Spjd */
44331399Smav#define	FTAG ((char *)(uintptr_t)__func__)
45168404Spjd
46219089Spjd#ifdef	ZFS_DEBUG
47168404Spjdtypedef struct reference {
48168404Spjd	list_node_t ref_link;
49168404Spjd	void *ref_holder;
50168404Spjd	uint64_t ref_number;
51168404Spjd	uint8_t *ref_removed;
52168404Spjd} reference_t;
53168404Spjd
54168404Spjdtypedef struct refcount {
55168404Spjd	kmutex_t rc_mtx;
56248571Smm	boolean_t rc_tracked;
57168404Spjd	list_t rc_list;
58168404Spjd	list_t rc_removed;
59246651Smm	uint64_t rc_count;
60246651Smm	uint64_t rc_removed_count;
61168404Spjd} refcount_t;
62168404Spjd
63248571Smm/* Note: refcount_t must be initialized with refcount_create[_untracked]() */
64168404Spjd
65168404Spjdvoid refcount_create(refcount_t *rc);
66248571Smmvoid refcount_create_untracked(refcount_t *rc);
67307277Smavvoid refcount_create_tracked(refcount_t *rc);
68168404Spjdvoid refcount_destroy(refcount_t *rc);
69168404Spjdvoid refcount_destroy_many(refcount_t *rc, uint64_t number);
70168404Spjdint refcount_is_zero(refcount_t *rc);
71168404Spjdint64_t refcount_count(refcount_t *rc);
72168404Spjdint64_t refcount_add(refcount_t *rc, void *holder_tag);
73168404Spjdint64_t refcount_remove(refcount_t *rc, void *holder_tag);
74168404Spjdint64_t refcount_add_many(refcount_t *rc, uint64_t number, void *holder_tag);
75168404Spjdint64_t refcount_remove_many(refcount_t *rc, uint64_t number, void *holder_tag);
76219089Spjdvoid refcount_transfer(refcount_t *dst, refcount_t *src);
77307265Smavvoid refcount_transfer_ownership(refcount_t *, void *, void *);
78307277Smavboolean_t refcount_held(refcount_t *, void *);
79307277Smavboolean_t refcount_not_held(refcount_t *, void *);
80168404Spjd
81179310Spjdvoid refcount_sysinit(void);
82168404Spjdvoid refcount_fini(void);
83168404Spjd
84219089Spjd#else	/* ZFS_DEBUG */
85168404Spjd
86168404Spjdtypedef struct refcount {
87168404Spjd	uint64_t rc_count;
88168404Spjd} refcount_t;
89168404Spjd
90168404Spjd#define	refcount_create(rc) ((rc)->rc_count = 0)
91248571Smm#define	refcount_create_untracked(rc) ((rc)->rc_count = 0)
92307277Smav#define	refcount_create_tracked(rc) ((rc)->rc_count = 0)
93168404Spjd#define	refcount_destroy(rc) ((rc)->rc_count = 0)
94168404Spjd#define	refcount_destroy_many(rc, number) ((rc)->rc_count = 0)
95168404Spjd#define	refcount_is_zero(rc) ((rc)->rc_count == 0)
96168404Spjd#define	refcount_count(rc) ((rc)->rc_count)
97270247Sdelphij#define	refcount_add(rc, holder) atomic_inc_64_nv(&(rc)->rc_count)
98270247Sdelphij#define	refcount_remove(rc, holder) atomic_dec_64_nv(&(rc)->rc_count)
99168404Spjd#define	refcount_add_many(rc, number, holder) \
100168404Spjd	atomic_add_64_nv(&(rc)->rc_count, number)
101168404Spjd#define	refcount_remove_many(rc, number, holder) \
102168404Spjd	atomic_add_64_nv(&(rc)->rc_count, -number)
103219089Spjd#define	refcount_transfer(dst, src) { \
104219089Spjd	uint64_t __tmp = (src)->rc_count; \
105219089Spjd	atomic_add_64(&(src)->rc_count, -__tmp); \
106219089Spjd	atomic_add_64(&(dst)->rc_count, __tmp); \
107219089Spjd}
108321535Smav#define	refcount_transfer_ownership(rc, current_holder, new_holder)	(void)0
109307277Smav#define	refcount_held(rc, holder)		((rc)->rc_count > 0)
110307277Smav#define	refcount_not_held(rc, holder)		(B_TRUE)
111168404Spjd
112179310Spjd#define	refcount_sysinit()
113168404Spjd#define	refcount_fini()
114168404Spjd
115219089Spjd#endif	/* ZFS_DEBUG */
116168404Spjd
117168404Spjd#ifdef	__cplusplus
118168404Spjd}
119168404Spjd#endif
120168404Spjd
121168404Spjd#endif /* _SYS_REFCOUNT_H */
122