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