fa9e4066f08beec538e775443c5be79dd423fcabahrens/*
fa9e4066f08beec538e775443c5be79dd423fcabahrens * CDDL HEADER START
fa9e4066f08beec538e775443c5be79dd423fcabahrens *
fa9e4066f08beec538e775443c5be79dd423fcabahrens * The contents of this file are subject to the terms of the
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock * Common Development and Distribution License (the "License").
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock * You may not use this file except in compliance with the License.
fa9e4066f08beec538e775443c5be79dd423fcabahrens *
fa9e4066f08beec538e775443c5be79dd423fcabahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
fa9e4066f08beec538e775443c5be79dd423fcabahrens * or http://www.opensolaris.org/os/licensing.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * See the License for the specific language governing permissions
fa9e4066f08beec538e775443c5be79dd423fcabahrens * and limitations under the License.
fa9e4066f08beec538e775443c5be79dd423fcabahrens *
fa9e4066f08beec538e775443c5be79dd423fcabahrens * When distributing Covered Code, include this CDDL HEADER in each
fa9e4066f08beec538e775443c5be79dd423fcabahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * If applicable, add the following below this CDDL HEADER, with the
fa9e4066f08beec538e775443c5be79dd423fcabahrens * fields enclosed by brackets "[]" replaced with your own identifying
fa9e4066f08beec538e775443c5be79dd423fcabahrens * information: Portions Copyright [yyyy] [name of copyright owner]
fa9e4066f08beec538e775443c5be79dd423fcabahrens *
fa9e4066f08beec538e775443c5be79dd423fcabahrens * CDDL HEADER END
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
fa9e4066f08beec538e775443c5be79dd423fcabahrens/*
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Ling * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens#ifndef _SYS_REFCOUNT_H
fa9e4066f08beec538e775443c5be79dd423fcabahrens#define _SYS_REFCOUNT_H
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <sys/inttypes.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <sys/list.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <sys/zfs_context.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens#ifdef __cplusplus
fa9e4066f08beec538e775443c5be79dd423fcabahrensextern "C" {
fa9e4066f08beec538e775443c5be79dd423fcabahrens#endif
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens/*
fa9e4066f08beec538e775443c5be79dd423fcabahrens * If the reference is held only by the calling function and not any
fa9e4066f08beec538e775443c5be79dd423fcabahrens * particular object, use FTAG (which is a string) for the holder_tag.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Otherwise, use the object that holds the reference.
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock#define FTAG ((char *)__func__)
fa9e4066f08beec538e775443c5be79dd423fcabahrens
744947dc83c634d985ed3ad79ac9c5e28d1865fdTom Erickson#ifdef ZFS_DEBUG
fa9e4066f08beec538e775443c5be79dd423fcabahrenstypedef struct reference {
fa9e4066f08beec538e775443c5be79dd423fcabahrens list_node_t ref_link;
fa9e4066f08beec538e775443c5be79dd423fcabahrens void *ref_holder;
fa9e4066f08beec538e775443c5be79dd423fcabahrens uint64_t ref_number;
fa9e4066f08beec538e775443c5be79dd423fcabahrens uint8_t *ref_removed;
fa9e4066f08beec538e775443c5be79dd423fcabahrens} reference_t;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrenstypedef struct refcount {
fa9e4066f08beec538e775443c5be79dd423fcabahrens kmutex_t rc_mtx;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens boolean_t rc_tracked;
fa9e4066f08beec538e775443c5be79dd423fcabahrens list_t rc_list;
fa9e4066f08beec538e775443c5be79dd423fcabahrens list_t rc_removed;
28e4da25922bdfc5cba7ab29f47de911bbd78009Matthew Ahrens uint64_t rc_count;
28e4da25922bdfc5cba7ab29f47de911bbd78009Matthew Ahrens uint64_t rc_removed_count;
fa9e4066f08beec538e775443c5be79dd423fcabahrens} refcount_t;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens/* Note: refcount_t must be initialized with refcount_create[_untracked]() */
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrensvoid refcount_create(refcount_t *rc);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensvoid refcount_create_untracked(refcount_t *rc);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilsonvoid refcount_create_tracked(refcount_t *rc);
fa9e4066f08beec538e775443c5be79dd423fcabahrensvoid refcount_destroy(refcount_t *rc);
fa9e4066f08beec538e775443c5be79dd423fcabahrensvoid refcount_destroy_many(refcount_t *rc, uint64_t number);
fa9e4066f08beec538e775443c5be79dd423fcabahrensint refcount_is_zero(refcount_t *rc);
fa9e4066f08beec538e775443c5be79dd423fcabahrensint64_t refcount_count(refcount_t *rc);
fa9e4066f08beec538e775443c5be79dd423fcabahrensint64_t refcount_add(refcount_t *rc, void *holder_tag);
fa9e4066f08beec538e775443c5be79dd423fcabahrensint64_t refcount_remove(refcount_t *rc, void *holder_tag);
fa9e4066f08beec538e775443c5be79dd423fcabahrensint64_t refcount_add_many(refcount_t *rc, uint64_t number, void *holder_tag);
fa9e4066f08beec538e775443c5be79dd423fcabahrensint64_t refcount_remove_many(refcount_t *rc, uint64_t number, void *holder_tag);
744947dc83c634d985ed3ad79ac9c5e28d1865fdTom Ericksonvoid refcount_transfer(refcount_t *dst, refcount_t *src);
dcbf3bd6a1f1360fc1afcee9e22c6dcff7844bf2George Wilsonvoid refcount_transfer_ownership(refcount_t *, void *, void *);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilsonboolean_t refcount_held(refcount_t *, void *);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilsonboolean_t refcount_not_held(refcount_t *, void *);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrensvoid refcount_init(void);
fa9e4066f08beec538e775443c5be79dd423fcabahrensvoid refcount_fini(void);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
744947dc83c634d985ed3ad79ac9c5e28d1865fdTom Erickson#else /* ZFS_DEBUG */
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrenstypedef struct refcount {
fa9e4066f08beec538e775443c5be79dd423fcabahrens uint64_t rc_count;
fa9e4066f08beec538e775443c5be79dd423fcabahrens} refcount_t;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens#define refcount_create(rc) ((rc)->rc_count = 0)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens#define refcount_create_untracked(rc) ((rc)->rc_count = 0)
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson#define refcount_create_tracked(rc) ((rc)->rc_count = 0)
fa9e4066f08beec538e775443c5be79dd423fcabahrens#define refcount_destroy(rc) ((rc)->rc_count = 0)
fa9e4066f08beec538e775443c5be79dd423fcabahrens#define refcount_destroy_many(rc, number) ((rc)->rc_count = 0)
fa9e4066f08beec538e775443c5be79dd423fcabahrens#define refcount_is_zero(rc) ((rc)->rc_count == 0)
fa9e4066f08beec538e775443c5be79dd423fcabahrens#define refcount_count(rc) ((rc)->rc_count)
1a5e258f5471356ca102c7176637cdce45bac147Josef 'Jeff' Sipek#define refcount_add(rc, holder) atomic_inc_64_nv(&(rc)->rc_count)
1a5e258f5471356ca102c7176637cdce45bac147Josef 'Jeff' Sipek#define refcount_remove(rc, holder) atomic_dec_64_nv(&(rc)->rc_count)
fa9e4066f08beec538e775443c5be79dd423fcabahrens#define refcount_add_many(rc, number, holder) \
fa9e4066f08beec538e775443c5be79dd423fcabahrens atomic_add_64_nv(&(rc)->rc_count, number)
fa9e4066f08beec538e775443c5be79dd423fcabahrens#define refcount_remove_many(rc, number, holder) \
fa9e4066f08beec538e775443c5be79dd423fcabahrens atomic_add_64_nv(&(rc)->rc_count, -number)
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Ling#define refcount_transfer(dst, src) { \
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Ling uint64_t __tmp = (src)->rc_count; \
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Ling atomic_add_64(&(src)->rc_count, -__tmp); \
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Ling atomic_add_64(&(dst)->rc_count, __tmp); \
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Ling}
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel#define refcount_transfer_ownership(rc, current_holder, new_holder) (void)0
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson#define refcount_held(rc, holder) ((rc)->rc_count > 0)
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson#define refcount_not_held(rc, holder) (B_TRUE)
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens#define refcount_init()
fa9e4066f08beec538e775443c5be79dd423fcabahrens#define refcount_fini()
fa9e4066f08beec538e775443c5be79dd423fcabahrens
744947dc83c634d985ed3ad79ac9c5e28d1865fdTom Erickson#endif /* ZFS_DEBUG */
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens#ifdef __cplusplus
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens#endif
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens#endif /* _SYS_REFCOUNT_H */