1 // SPDX-License-Identifier: CDDL-1.0 2 /* 3 * CDDL HEADER START 4 * 5 * The contents of this file are subject to the terms of the 6 * Common Development and Distribution License (the "License"). 7 * You may not use this file except in compliance with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or https://opensource.org/licenses/CDDL-1.0. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 24 * Copyright (c) 2012, 2015 by Delphix. All rights reserved. 25 */ 26 27 #ifndef _SYS_ZFS_REFCOUNT_H 28 #define _SYS_ZFS_REFCOUNT_H 29 30 #include <sys/inttypes.h> 31 #include <sys/avl.h> 32 #include <sys/list.h> 33 #include <sys/zfs_context.h> 34 35 #ifdef __cplusplus 36 extern "C" { 37 #endif 38 39 /* 40 * If the reference is held only by the calling function and not any 41 * particular object, use FTAG (which is a string) for the holder_tag. 42 * Otherwise, use the object that holds the reference. 43 */ 44 #define FTAG ((char *)(uintptr_t)__func__) 45 46 #ifdef ZFS_DEBUG 47 typedef struct reference { 48 union { 49 avl_node_t a; 50 list_node_t l; 51 } ref_link; 52 const void *ref_holder; 53 uint64_t ref_number; 54 boolean_t ref_search; 55 } reference_t; 56 57 typedef struct refcount { 58 uint64_t rc_count; 59 kmutex_t rc_mtx; 60 avl_tree_t rc_tree; 61 list_t rc_removed; 62 uint_t rc_removed_count; 63 boolean_t rc_tracked; 64 } zfs_refcount_t; 65 66 /* 67 * Note: zfs_refcount_t must be initialized with 68 * refcount_create[_untracked]() 69 */ 70 71 void zfs_refcount_create(zfs_refcount_t *); 72 void zfs_refcount_create_untracked(zfs_refcount_t *); 73 void zfs_refcount_create_tracked(zfs_refcount_t *); 74 void zfs_refcount_destroy(zfs_refcount_t *); 75 void zfs_refcount_destroy_many(zfs_refcount_t *, uint64_t); 76 int zfs_refcount_is_zero(zfs_refcount_t *); 77 int64_t zfs_refcount_count(zfs_refcount_t *); 78 int64_t zfs_refcount_add(zfs_refcount_t *, const void *); 79 int64_t zfs_refcount_remove(zfs_refcount_t *, const void *); 80 /* 81 * Note that (add|remove)_many adds/removes one reference with "number" N, 82 * _not_ N references with "number" 1, which is what (add|remove)_few does, 83 * or what vanilla zfs_refcount_(add|remove) called N times would do. 84 * 85 * Attempting to remove a reference with number N when none exists is a 86 * panic on debug kernels with reference_tracking enabled. 87 */ 88 void zfs_refcount_add_few(zfs_refcount_t *, uint64_t, const void *); 89 void zfs_refcount_remove_few(zfs_refcount_t *, uint64_t, const void *); 90 int64_t zfs_refcount_add_many(zfs_refcount_t *, uint64_t, const void *); 91 int64_t zfs_refcount_remove_many(zfs_refcount_t *, uint64_t, const void *); 92 void zfs_refcount_transfer(zfs_refcount_t *, zfs_refcount_t *); 93 void zfs_refcount_transfer_ownership(zfs_refcount_t *, const void *, 94 const void *); 95 void zfs_refcount_transfer_ownership_many(zfs_refcount_t *, uint64_t, 96 const void *, const void *); 97 boolean_t zfs_refcount_held(zfs_refcount_t *, const void *); 98 boolean_t zfs_refcount_not_held(zfs_refcount_t *, const void *); 99 100 void zfs_refcount_init(void); 101 void zfs_refcount_fini(void); 102 103 #else /* ZFS_DEBUG */ 104 105 typedef struct refcount { 106 uint64_t rc_count; 107 } zfs_refcount_t; 108 109 #define zfs_refcount_create(rc) ((rc)->rc_count = 0) 110 #define zfs_refcount_create_untracked(rc) ((rc)->rc_count = 0) 111 #define zfs_refcount_create_tracked(rc) ((rc)->rc_count = 0) 112 #define zfs_refcount_destroy(rc) ((rc)->rc_count = 0) 113 #define zfs_refcount_destroy_many(rc, number) ((rc)->rc_count = 0) 114 #define zfs_refcount_is_zero(rc) (zfs_refcount_count(rc) == 0) 115 #define zfs_refcount_count(rc) atomic_load_64(&(rc)->rc_count) 116 #define zfs_refcount_add(rc, holder) atomic_inc_64_nv(&(rc)->rc_count) 117 #define zfs_refcount_remove(rc, holder) atomic_dec_64_nv(&(rc)->rc_count) 118 #define zfs_refcount_add_few(rc, number, holder) \ 119 atomic_add_64(&(rc)->rc_count, number) 120 #define zfs_refcount_remove_few(rc, number, holder) \ 121 atomic_add_64(&(rc)->rc_count, -number) 122 #define zfs_refcount_add_many(rc, number, holder) \ 123 atomic_add_64_nv(&(rc)->rc_count, number) 124 #define zfs_refcount_remove_many(rc, number, holder) \ 125 atomic_add_64_nv(&(rc)->rc_count, -number) 126 #define zfs_refcount_transfer(dst, src) { \ 127 uint64_t __tmp = zfs_refcount_count(src); \ 128 atomic_add_64(&(src)->rc_count, -__tmp); \ 129 atomic_add_64(&(dst)->rc_count, __tmp); \ 130 } 131 #define zfs_refcount_transfer_ownership(rc, ch, nh) ((void)0) 132 #define zfs_refcount_transfer_ownership_many(rc, nr, ch, nh) ((void)0) 133 #define zfs_refcount_held(rc, holder) (zfs_refcount_count(rc) > 0) 134 #define zfs_refcount_not_held(rc, holder) (B_TRUE) 135 136 #define zfs_refcount_init() 137 #define zfs_refcount_fini() 138 139 #endif /* ZFS_DEBUG */ 140 141 #ifdef __cplusplus 142 } 143 #endif 144 145 #endif /* _SYS_REFCOUNT_H */ 146