]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - zfs/include/sys/refcount.h
UBUNTU: SAUCE: (noup) Update spl to 0.6.5.9-1ubuntu1, zfs to 0.6.5.9-5ubuntu5
[mirror_ubuntu-bionic-kernel.git] / zfs / include / sys / refcount.h
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 http://www.opensolaris.org/os/licensing.
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 */
24
25 #ifndef _SYS_REFCOUNT_H
26 #define _SYS_REFCOUNT_H
27
28 #include <sys/inttypes.h>
29 #include <sys/list.h>
30 #include <sys/zfs_context.h>
31
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35
36 /*
37 * If the reference is held only by the calling function and not any
38 * particular object, use FTAG (which is a string) for the holder_tag.
39 * Otherwise, use the object that holds the reference.
40 */
41 #define FTAG ((char *)__func__)
42
43 /*
44 * Starting with 4.11, torvalds/linux@f405df5, the linux kernel defines a
45 * refcount_t type of its own. The macro below effectively changes references
46 * in the ZFS code from refcount_t to zfs_refcount_t at compile time, so that
47 * existing code need not be altered, reducing conflicts when landing openZFS
48 * patches.
49 */
50
51 #define refcount_t zfs_refcount_t
52 #define refcount_add zfs_refcount_add
53
54 #ifdef ZFS_DEBUG
55 typedef struct reference {
56 list_node_t ref_link;
57 void *ref_holder;
58 uint64_t ref_number;
59 uint8_t *ref_removed;
60 } reference_t;
61
62 typedef struct refcount {
63 kmutex_t rc_mtx;
64 boolean_t rc_tracked;
65 list_t rc_list;
66 list_t rc_removed;
67 int64_t rc_count;
68 int64_t rc_removed_count;
69 } zfs_refcount_t;
70
71 /* Note: refcount_t must be initialized with refcount_create[_untracked]() */
72
73 void refcount_create(refcount_t *rc);
74 void refcount_create_untracked(refcount_t *rc);
75 void refcount_destroy(refcount_t *rc);
76 void refcount_destroy_many(refcount_t *rc, uint64_t number);
77 int refcount_is_zero(refcount_t *rc);
78 int64_t refcount_count(refcount_t *rc);
79 int64_t zfs_refcount_add(refcount_t *rc, void *holder_tag);
80 int64_t refcount_remove(refcount_t *rc, void *holder_tag);
81 int64_t refcount_add_many(refcount_t *rc, uint64_t number, void *holder_tag);
82 int64_t refcount_remove_many(refcount_t *rc, uint64_t number, void *holder_tag);
83 void refcount_transfer(refcount_t *dst, refcount_t *src);
84
85 void refcount_init(void);
86 void refcount_fini(void);
87
88 #else /* ZFS_DEBUG */
89
90 typedef struct refcount {
91 uint64_t rc_count;
92 } refcount_t;
93
94 #define refcount_create(rc) ((rc)->rc_count = 0)
95 #define refcount_create_untracked(rc) ((rc)->rc_count = 0)
96 #define refcount_destroy(rc) ((rc)->rc_count = 0)
97 #define refcount_destroy_many(rc, number) ((rc)->rc_count = 0)
98 #define refcount_is_zero(rc) ((rc)->rc_count == 0)
99 #define refcount_count(rc) ((rc)->rc_count)
100 #define zfs_refcount_add(rc, holder) atomic_add_64_nv(&(rc)->rc_count, 1)
101 #define refcount_remove(rc, holder) atomic_add_64_nv(&(rc)->rc_count, -1)
102 #define refcount_add_many(rc, number, holder) \
103 atomic_add_64_nv(&(rc)->rc_count, number)
104 #define refcount_remove_many(rc, number, holder) \
105 atomic_add_64_nv(&(rc)->rc_count, -number)
106 #define refcount_transfer(dst, src) { \
107 uint64_t __tmp = (src)->rc_count; \
108 atomic_add_64(&(src)->rc_count, -__tmp); \
109 atomic_add_64(&(dst)->rc_count, __tmp); \
110 }
111
112 #define refcount_init()
113 #define refcount_fini()
114
115 #endif /* ZFS_DEBUG */
116
117 #ifdef __cplusplus
118 }
119 #endif
120
121 #endif /* _SYS_REFCOUNT_H */