xref: /illumos-gate/usr/src/uts/common/fs/zfs/zrlock.c (revision 260af64d)
1744947dcSTom Erickson /*
2744947dcSTom Erickson  * CDDL HEADER START
3744947dcSTom Erickson  *
4744947dcSTom Erickson  * The contents of this file are subject to the terms of the
5744947dcSTom Erickson  * Common Development and Distribution License (the "License").
6744947dcSTom Erickson  * You may not use this file except in compliance with the License.
7744947dcSTom Erickson  *
8744947dcSTom Erickson  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9744947dcSTom Erickson  * or http://www.opensolaris.org/os/licensing.
10744947dcSTom Erickson  * See the License for the specific language governing permissions
11744947dcSTom Erickson  * and limitations under the License.
12744947dcSTom Erickson  *
13744947dcSTom Erickson  * When distributing Covered Code, include this CDDL HEADER in each
14744947dcSTom Erickson  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15744947dcSTom Erickson  * If applicable, add the following below this CDDL HEADER, with the
16744947dcSTom Erickson  * fields enclosed by brackets "[]" replaced with your own identifying
17744947dcSTom Erickson  * information: Portions Copyright [yyyy] [name of copyright owner]
18744947dcSTom Erickson  *
19744947dcSTom Erickson  * CDDL HEADER END
20744947dcSTom Erickson  */
21744947dcSTom Erickson /*
22744947dcSTom Erickson  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
239a686fbcSPaul Dagnelie  * Copyright (c) 2014, 2015 by Delphix. All rights reserved.
24*260af64dSYouzhong Yang  * Copyright 2016 The MathWorks, Inc. All rights reserved.
25744947dcSTom Erickson  */
26744947dcSTom Erickson 
27744947dcSTom Erickson /*
28744947dcSTom Erickson  * A Zero Reference Lock (ZRL) is a reference count that can lock out new
29744947dcSTom Erickson  * references only when the count is zero and only without waiting if the count
30744947dcSTom Erickson  * is not already zero. It is similar to a read-write lock in that it allows
31744947dcSTom Erickson  * multiple readers and only a single writer, but it does not allow a writer to
32744947dcSTom Erickson  * block while waiting for readers to exit, and therefore the question of
33744947dcSTom Erickson  * reader/writer priority is moot (no WRWANT bit). Since the equivalent of
34744947dcSTom Erickson  * rw_enter(&lock, RW_WRITER) is disallowed and only tryenter() is allowed, it
35744947dcSTom Erickson  * is perfectly safe for the same reader to acquire the same lock multiple
36744947dcSTom Erickson  * times. The fact that a ZRL is reentrant for readers (through multiple calls
37744947dcSTom Erickson  * to zrl_add()) makes it convenient for determining whether something is
38744947dcSTom Erickson  * actively referenced without the fuss of flagging lock ownership across
39744947dcSTom Erickson  * function calls.
40744947dcSTom Erickson  */
41744947dcSTom Erickson #include <sys/zrlock.h>
42744947dcSTom Erickson 
43744947dcSTom Erickson /*
44744947dcSTom Erickson  * A ZRL can be locked only while there are zero references, so ZRL_LOCKED is
45744947dcSTom Erickson  * treated as zero references.
46744947dcSTom Erickson  */
478df17305SMatthew Ahrens #define	ZRL_LOCKED	-1
48744947dcSTom Erickson #define	ZRL_DESTROYED	-2
49744947dcSTom Erickson 
50744947dcSTom Erickson void
zrl_init(zrlock_t * zrl)51744947dcSTom Erickson zrl_init(zrlock_t *zrl)
52744947dcSTom Erickson {
53744947dcSTom Erickson 	mutex_init(&zrl->zr_mtx, NULL, MUTEX_DEFAULT, NULL);
54744947dcSTom Erickson 	zrl->zr_refcount = 0;
55744947dcSTom Erickson 	cv_init(&zrl->zr_cv, NULL, CV_DEFAULT, NULL);
56744947dcSTom Erickson #ifdef	ZFS_DEBUG
57744947dcSTom Erickson 	zrl->zr_owner = NULL;
58744947dcSTom Erickson 	zrl->zr_caller = NULL;
59744947dcSTom Erickson #endif
60744947dcSTom Erickson }
61744947dcSTom Erickson 
62744947dcSTom Erickson void
zrl_destroy(zrlock_t * zrl)63744947dcSTom Erickson zrl_destroy(zrlock_t *zrl)
64744947dcSTom Erickson {
658df17305SMatthew Ahrens 	ASSERT0(zrl->zr_refcount);
66744947dcSTom Erickson 
67744947dcSTom Erickson 	mutex_destroy(&zrl->zr_mtx);
68744947dcSTom Erickson 	zrl->zr_refcount = ZRL_DESTROYED;
69744947dcSTom Erickson 	cv_destroy(&zrl->zr_cv);
70744947dcSTom Erickson }
71744947dcSTom Erickson 
72744947dcSTom Erickson void
zrl_add_impl(zrlock_t * zrl,const char * zc)739a686fbcSPaul Dagnelie zrl_add_impl(zrlock_t *zrl, const char *zc)
74744947dcSTom Erickson {
75*260af64dSYouzhong Yang 	for (;;) {
76*260af64dSYouzhong Yang 		uint32_t n = (uint32_t)zrl->zr_refcount;
77*260af64dSYouzhong Yang 		while (n != ZRL_LOCKED) {
78*260af64dSYouzhong Yang 			uint32_t cas = atomic_cas_32(
79*260af64dSYouzhong Yang 			    (uint32_t *)&zrl->zr_refcount, n, n + 1);
80*260af64dSYouzhong Yang 			if (cas == n) {
81*260af64dSYouzhong Yang 				ASSERT3S((int32_t)n, >=, 0);
82744947dcSTom Erickson #ifdef	ZFS_DEBUG
83*260af64dSYouzhong Yang 				if (zrl->zr_owner == curthread) {
84*260af64dSYouzhong Yang 					DTRACE_PROBE2(zrlock__reentry,
85*260af64dSYouzhong Yang 					    zrlock_t *, zrl, uint32_t, n);
86*260af64dSYouzhong Yang 				}
87*260af64dSYouzhong Yang 				zrl->zr_owner = curthread;
88*260af64dSYouzhong Yang 				zrl->zr_caller = zc;
89744947dcSTom Erickson #endif
90*260af64dSYouzhong Yang 				return;
91*260af64dSYouzhong Yang 			}
92*260af64dSYouzhong Yang 			n = cas;
93744947dcSTom Erickson 		}
94744947dcSTom Erickson 
95*260af64dSYouzhong Yang 		mutex_enter(&zrl->zr_mtx);
96*260af64dSYouzhong Yang 		while (zrl->zr_refcount == ZRL_LOCKED) {
97*260af64dSYouzhong Yang 			cv_wait(&zrl->zr_cv, &zrl->zr_mtx);
98*260af64dSYouzhong Yang 		}
99*260af64dSYouzhong Yang 		mutex_exit(&zrl->zr_mtx);
100744947dcSTom Erickson 	}
101744947dcSTom Erickson }
102744947dcSTom Erickson 
103744947dcSTom Erickson void
zrl_remove(zrlock_t * zrl)104744947dcSTom Erickson zrl_remove(zrlock_t *zrl)
105744947dcSTom Erickson {
106744947dcSTom Erickson 	uint32_t n;
107744947dcSTom Erickson 
108744947dcSTom Erickson #ifdef	ZFS_DEBUG
109744947dcSTom Erickson 	if (zrl->zr_owner == curthread) {
110744947dcSTom Erickson 		zrl->zr_owner = NULL;
111744947dcSTom Erickson 		zrl->zr_caller = NULL;
112744947dcSTom Erickson 	}
113744947dcSTom Erickson #endif
1148df17305SMatthew Ahrens 	n = atomic_dec_32_nv((uint32_t *)&zrl->zr_refcount);
1158df17305SMatthew Ahrens 	ASSERT3S((int32_t)n, >=, 0);
116744947dcSTom Erickson }
117744947dcSTom Erickson 
118744947dcSTom Erickson int
zrl_tryenter(zrlock_t * zrl)119744947dcSTom Erickson zrl_tryenter(zrlock_t *zrl)
120744947dcSTom Erickson {
121744947dcSTom Erickson 	uint32_t n = (uint32_t)zrl->zr_refcount;
122744947dcSTom Erickson 
123744947dcSTom Erickson 	if (n == 0) {
124744947dcSTom Erickson 		uint32_t cas = atomic_cas_32(
125744947dcSTom Erickson 		    (uint32_t *)&zrl->zr_refcount, 0, ZRL_LOCKED);
126744947dcSTom Erickson 		if (cas == 0) {
127744947dcSTom Erickson #ifdef	ZFS_DEBUG
1288df17305SMatthew Ahrens 			ASSERT3P(zrl->zr_owner, ==, NULL);
129744947dcSTom Erickson 			zrl->zr_owner = curthread;
130744947dcSTom Erickson #endif
131744947dcSTom Erickson 			return (1);
132744947dcSTom Erickson 		}
133744947dcSTom Erickson 	}
134744947dcSTom Erickson 
1358df17305SMatthew Ahrens 	ASSERT3S((int32_t)n, >, ZRL_DESTROYED);
136744947dcSTom Erickson 
137744947dcSTom Erickson 	return (0);
138744947dcSTom Erickson }
139744947dcSTom Erickson 
140744947dcSTom Erickson void
zrl_exit(zrlock_t * zrl)141744947dcSTom Erickson zrl_exit(zrlock_t *zrl)
142744947dcSTom Erickson {
1438df17305SMatthew Ahrens 	ASSERT3S(zrl->zr_refcount, ==, ZRL_LOCKED);
144744947dcSTom Erickson 
145744947dcSTom Erickson 	mutex_enter(&zrl->zr_mtx);
146744947dcSTom Erickson #ifdef	ZFS_DEBUG
1478df17305SMatthew Ahrens 	ASSERT3P(zrl->zr_owner, ==, curthread);
148744947dcSTom Erickson 	zrl->zr_owner = NULL;
149744947dcSTom Erickson 	membar_producer();	/* make sure the owner store happens first */
150744947dcSTom Erickson #endif
151744947dcSTom Erickson 	zrl->zr_refcount = 0;
152744947dcSTom Erickson 	cv_broadcast(&zrl->zr_cv);
153744947dcSTom Erickson 	mutex_exit(&zrl->zr_mtx);
154744947dcSTom Erickson }
155744947dcSTom Erickson 
156744947dcSTom Erickson int
zrl_refcount(zrlock_t * zrl)157744947dcSTom Erickson zrl_refcount(zrlock_t *zrl)
158744947dcSTom Erickson {
1598df17305SMatthew Ahrens 	ASSERT3S(zrl->zr_refcount, >, ZRL_DESTROYED);
160744947dcSTom Erickson 
161744947dcSTom Erickson 	int n = (int)zrl->zr_refcount;
162744947dcSTom Erickson 	return (n <= 0 ? 0 : n);
163744947dcSTom Erickson }
164744947dcSTom Erickson 
165744947dcSTom Erickson int
zrl_is_zero(zrlock_t * zrl)166744947dcSTom Erickson zrl_is_zero(zrlock_t *zrl)
167744947dcSTom Erickson {
1688df17305SMatthew Ahrens 	ASSERT3S(zrl->zr_refcount, >, ZRL_DESTROYED);
169744947dcSTom Erickson 
170744947dcSTom Erickson 	return (zrl->zr_refcount <= 0);
171744947dcSTom Erickson }
172744947dcSTom Erickson 
173744947dcSTom Erickson int
zrl_is_locked(zrlock_t * zrl)174744947dcSTom Erickson zrl_is_locked(zrlock_t *zrl)
175744947dcSTom Erickson {
1768df17305SMatthew Ahrens 	ASSERT3S(zrl->zr_refcount, >, ZRL_DESTROYED);
177744947dcSTom Erickson 
178744947dcSTom Erickson 	return (zrl->zr_refcount == ZRL_LOCKED);
179744947dcSTom Erickson }
180744947dcSTom Erickson 
181744947dcSTom Erickson #ifdef	ZFS_DEBUG
182744947dcSTom Erickson kthread_t *
zrl_owner(zrlock_t * zrl)183744947dcSTom Erickson zrl_owner(zrlock_t *zrl)
184744947dcSTom Erickson {
185744947dcSTom Erickson 	return (zrl->zr_owner);
186744947dcSTom Erickson }
187744947dcSTom Erickson #endif
188