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; 59*e914ace2STim Schumacher } zfs_refcount_t; 60*e914ace2STim Schumacher 61*e914ace2STim Schumacher /* 62*e914ace2STim Schumacher * Note: zfs_refcount_t must be initialized with 63*e914ace2STim Schumacher * refcount_create[_untracked]() 64*e914ace2STim Schumacher */ 65*e914ace2STim Schumacher 66*e914ace2STim Schumacher void zfs_refcount_create(zfs_refcount_t *); 67*e914ace2STim Schumacher void zfs_refcount_create_untracked(zfs_refcount_t *); 68*e914ace2STim Schumacher void zfs_refcount_create_tracked(zfs_refcount_t *); 69*e914ace2STim Schumacher void zfs_refcount_destroy(zfs_refcount_t *); 70*e914ace2STim Schumacher void zfs_refcount_destroy_many(zfs_refcount_t *, uint64_t); 71*e914ace2STim Schumacher int zfs_refcount_is_zero(zfs_refcount_t *); 72*e914ace2STim Schumacher int64_t zfs_refcount_count(zfs_refcount_t *); 73*e914ace2STim Schumacher int64_t zfs_refcount_add(zfs_refcount_t *, void *); 74*e914ace2STim Schumacher int64_t zfs_refcount_remove(zfs_refcount_t *, void *); 75*e914ace2STim Schumacher int64_t zfs_refcount_add_many(zfs_refcount_t *, uint64_t, void *); 76*e914ace2STim Schumacher int64_t zfs_refcount_remove_many(zfs_refcount_t *, uint64_t, void *); 77*e914ace2STim Schumacher void zfs_refcount_transfer(zfs_refcount_t *, zfs_refcount_t *); 78*e914ace2STim Schumacher void zfs_refcount_transfer_ownership(zfs_refcount_t *, void *, void *); 79*e914ace2STim Schumacher boolean_t zfs_refcount_held(zfs_refcount_t *, void *); 80*e914ace2STim Schumacher boolean_t zfs_refcount_not_held(zfs_refcount_t *, void *); 81*e914ace2STim Schumacher 82*e914ace2STim Schumacher void zfs_refcount_init(void); 83*e914ace2STim Schumacher void zfs_refcount_fini(void); 84fa9e4066Sahrens 85744947dcSTom Erickson #else /* ZFS_DEBUG */ 86fa9e4066Sahrens 87fa9e4066Sahrens typedef struct refcount { 88fa9e4066Sahrens uint64_t rc_count; 89*e914ace2STim Schumacher } zfs_refcount_t; 90*e914ace2STim Schumacher 91*e914ace2STim Schumacher #define zfs_refcount_create(rc) ((rc)->rc_count = 0) 92*e914ace2STim Schumacher #define zfs_refcount_create_untracked(rc) ((rc)->rc_count = 0) 93*e914ace2STim Schumacher #define zfs_refcount_create_tracked(rc) ((rc)->rc_count = 0) 94*e914ace2STim Schumacher #define zfs_refcount_destroy(rc) ((rc)->rc_count = 0) 95*e914ace2STim Schumacher #define zfs_refcount_destroy_many(rc, number) ((rc)->rc_count = 0) 96*e914ace2STim Schumacher #define zfs_refcount_is_zero(rc) ((rc)->rc_count == 0) 97*e914ace2STim Schumacher #define zfs_refcount_count(rc) ((rc)->rc_count) 98*e914ace2STim Schumacher #define zfs_refcount_add(rc, holder) atomic_inc_64_nv(&(rc)->rc_count) 99*e914ace2STim Schumacher #define zfs_refcount_remove(rc, holder) atomic_dec_64_nv(&(rc)->rc_count) 100*e914ace2STim Schumacher #define zfs_refcount_add_many(rc, number, holder) \ 101fa9e4066Sahrens atomic_add_64_nv(&(rc)->rc_count, number) 102*e914ace2STim Schumacher #define zfs_refcount_remove_many(rc, number, holder) \ 103fa9e4066Sahrens atomic_add_64_nv(&(rc)->rc_count, -number) 104*e914ace2STim Schumacher #define zfs_refcount_transfer(dst, src) { \ 1053f9d6ad7SLin Ling uint64_t __tmp = (src)->rc_count; \ 1063f9d6ad7SLin Ling atomic_add_64(&(src)->rc_count, -__tmp); \ 1073f9d6ad7SLin Ling atomic_add_64(&(dst)->rc_count, __tmp); \ 1083f9d6ad7SLin Ling } 109*e914ace2STim Schumacher #define zfs_refcount_transfer_ownership(rc, current_holder, new_holder) (void)0 110*e914ace2STim Schumacher #define zfs_refcount_held(rc, holder) ((rc)->rc_count > 0) 111*e914ace2STim Schumacher #define zfs_refcount_not_held(rc, holder) (B_TRUE) 112fa9e4066Sahrens 113*e914ace2STim Schumacher #define zfs_refcount_init() 114*e914ace2STim Schumacher #define zfs_refcount_fini() 115fa9e4066Sahrens 116744947dcSTom Erickson #endif /* ZFS_DEBUG */ 117fa9e4066Sahrens 118fa9e4066Sahrens #ifdef __cplusplus 119fa9e4066Sahrens } 120fa9e4066Sahrens #endif 121fa9e4066Sahrens 122fa9e4066Sahrens #endif /* _SYS_REFCOUNT_H */ 123