8023 Panic destroying a metaslab deferred range tree
[unleashed.git] / usr / src / uts / common / fs / zfs / zrlock.c
blobec333e54d2a83e17f4c798f7035b953166b3918d
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
22 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2014, 2015 by Delphix. All rights reserved.
24 * Copyright 2016 The MathWorks, Inc. All rights reserved.
28 * A Zero Reference Lock (ZRL) is a reference count that can lock out new
29 * references only when the count is zero and only without waiting if the count
30 * is not already zero. It is similar to a read-write lock in that it allows
31 * multiple readers and only a single writer, but it does not allow a writer to
32 * block while waiting for readers to exit, and therefore the question of
33 * reader/writer priority is moot (no WRWANT bit). Since the equivalent of
34 * rw_enter(&lock, RW_WRITER) is disallowed and only tryenter() is allowed, it
35 * is perfectly safe for the same reader to acquire the same lock multiple
36 * times. The fact that a ZRL is reentrant for readers (through multiple calls
37 * to zrl_add()) makes it convenient for determining whether something is
38 * actively referenced without the fuss of flagging lock ownership across
39 * function calls.
41 #include <sys/zrlock.h>
44 * A ZRL can be locked only while there are zero references, so ZRL_LOCKED is
45 * treated as zero references.
47 #define ZRL_LOCKED -1
48 #define ZRL_DESTROYED -2
50 void
51 zrl_init(zrlock_t *zrl)
53 mutex_init(&zrl->zr_mtx, NULL, MUTEX_DEFAULT, NULL);
54 zrl->zr_refcount = 0;
55 cv_init(&zrl->zr_cv, NULL, CV_DEFAULT, NULL);
56 #ifdef ZFS_DEBUG
57 zrl->zr_owner = NULL;
58 zrl->zr_caller = NULL;
59 #endif
62 void
63 zrl_destroy(zrlock_t *zrl)
65 ASSERT0(zrl->zr_refcount);
67 mutex_destroy(&zrl->zr_mtx);
68 zrl->zr_refcount = ZRL_DESTROYED;
69 cv_destroy(&zrl->zr_cv);
72 void
73 zrl_add_impl(zrlock_t *zrl, const char *zc)
75 for (;;) {
76 uint32_t n = (uint32_t)zrl->zr_refcount;
77 while (n != ZRL_LOCKED) {
78 uint32_t cas = atomic_cas_32(
79 (uint32_t *)&zrl->zr_refcount, n, n + 1);
80 if (cas == n) {
81 ASSERT3S((int32_t)n, >=, 0);
82 #ifdef ZFS_DEBUG
83 if (zrl->zr_owner == curthread) {
84 DTRACE_PROBE2(zrlock__reentry,
85 zrlock_t *, zrl, uint32_t, n);
87 zrl->zr_owner = curthread;
88 zrl->zr_caller = zc;
89 #endif
90 return;
92 n = cas;
95 mutex_enter(&zrl->zr_mtx);
96 while (zrl->zr_refcount == ZRL_LOCKED) {
97 cv_wait(&zrl->zr_cv, &zrl->zr_mtx);
99 mutex_exit(&zrl->zr_mtx);
103 void
104 zrl_remove(zrlock_t *zrl)
106 uint32_t n;
108 #ifdef ZFS_DEBUG
109 if (zrl->zr_owner == curthread) {
110 zrl->zr_owner = NULL;
111 zrl->zr_caller = NULL;
113 #endif
114 n = atomic_dec_32_nv((uint32_t *)&zrl->zr_refcount);
115 ASSERT3S((int32_t)n, >=, 0);
119 zrl_tryenter(zrlock_t *zrl)
121 uint32_t n = (uint32_t)zrl->zr_refcount;
123 if (n == 0) {
124 uint32_t cas = atomic_cas_32(
125 (uint32_t *)&zrl->zr_refcount, 0, ZRL_LOCKED);
126 if (cas == 0) {
127 #ifdef ZFS_DEBUG
128 ASSERT3P(zrl->zr_owner, ==, NULL);
129 zrl->zr_owner = curthread;
130 #endif
131 return (1);
135 ASSERT3S((int32_t)n, >, ZRL_DESTROYED);
137 return (0);
140 void
141 zrl_exit(zrlock_t *zrl)
143 ASSERT3S(zrl->zr_refcount, ==, ZRL_LOCKED);
145 mutex_enter(&zrl->zr_mtx);
146 #ifdef ZFS_DEBUG
147 ASSERT3P(zrl->zr_owner, ==, curthread);
148 zrl->zr_owner = NULL;
149 membar_producer(); /* make sure the owner store happens first */
150 #endif
151 zrl->zr_refcount = 0;
152 cv_broadcast(&zrl->zr_cv);
153 mutex_exit(&zrl->zr_mtx);
157 zrl_refcount(zrlock_t *zrl)
159 ASSERT3S(zrl->zr_refcount, >, ZRL_DESTROYED);
161 int n = (int)zrl->zr_refcount;
162 return (n <= 0 ? 0 : n);
166 zrl_is_zero(zrlock_t *zrl)
168 ASSERT3S(zrl->zr_refcount, >, ZRL_DESTROYED);
170 return (zrl->zr_refcount <= 0);
174 zrl_is_locked(zrlock_t *zrl)
176 ASSERT3S(zrl->zr_refcount, >, ZRL_DESTROYED);
178 return (zrl->zr_refcount == ZRL_LOCKED);
181 #ifdef ZFS_DEBUG
182 kthread_t *
183 zrl_owner(zrlock_t *zrl)
185 return (zrl->zr_owner);
187 #endif