]> git.proxmox.com Git - mirror_spl-debian.git/blame - include/sys/rwlock.h
Imported Upstream version 0.6.5.9
[mirror_spl-debian.git] / include / sys / rwlock.h
CommitLineData
716154c5
BB
1/*****************************************************************************\
2 * Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.
3 * Copyright (C) 2007 The Regents of the University of California.
4 * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
5 * Written by Brian Behlendorf <behlendorf1@llnl.gov>.
715f6251 6 * UCRL-CODE-235197
7 *
716154c5 8 * This file is part of the SPL, Solaris Porting Layer.
3d6af2dd 9 * For details, see <http://zfsonlinux.org/>.
716154c5
BB
10 *
11 * The SPL is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
715f6251 15 *
716154c5 16 * The SPL is distributed in the hope that it will be useful, but WITHOUT
715f6251 17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 * for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
716154c5
BB
22 * with the SPL. If not, see <http://www.gnu.org/licenses/>.
23\*****************************************************************************/
715f6251 24
09b414e8 25#ifndef _SPL_RWLOCK_H
e8b31e84 26#define _SPL_RWLOCK_H
f1ca4da6 27
f4b37741 28#include <sys/types.h>
d28db80f 29#include <linux/rwsem.h>
46aa7b39 30#include <linux/rwsem_compat.h>
f1ca4da6 31
32typedef enum {
e811949a
BB
33 RW_DRIVER = 2,
34 RW_DEFAULT = 4
f1ca4da6 35} krw_type_t;
36
37typedef enum {
d28db80f 38 RW_NONE = 0,
e811949a
BB
39 RW_WRITER = 1,
40 RW_READER = 2
f1ca4da6 41} krw_t;
42
d28db80f
BB
43typedef struct {
44 struct rw_semaphore rw_rwlock;
45 kthread_t *rw_owner;
46} krwlock_t;
f1ca4da6 47
d28db80f
BB
48#define SEM(rwp) ((struct rw_semaphore *)(rwp))
49
d28db80f
BB
50static inline void
51spl_rw_set_owner(krwlock_t *rwp)
52{
53 unsigned long flags;
54
588d9004 55 spl_rwsem_lock_irqsave(&SEM(rwp)->wait_lock, flags);
d28db80f 56 rwp->rw_owner = current;
588d9004 57 spl_rwsem_unlock_irqrestore(&SEM(rwp)->wait_lock, flags);
d28db80f
BB
58}
59
60static inline void
61spl_rw_clear_owner(krwlock_t *rwp)
62{
63 unsigned long flags;
64
588d9004 65 spl_rwsem_lock_irqsave(&SEM(rwp)->wait_lock, flags);
d28db80f 66 rwp->rw_owner = NULL;
588d9004 67 spl_rwsem_unlock_irqrestore(&SEM(rwp)->wait_lock, flags);
d28db80f
BB
68}
69
70static inline kthread_t *
71rw_owner(krwlock_t *rwp)
72{
73 unsigned long flags;
74 kthread_t *owner;
75
588d9004 76 spl_rwsem_lock_irqsave(&SEM(rwp)->wait_lock, flags);
46aa7b39 77 owner = rwp->rw_owner;
588d9004 78 spl_rwsem_unlock_irqrestore(&SEM(rwp)->wait_lock, flags);
d28db80f
BB
79
80 return owner;
81}
82
83static inline int
84RW_READ_HELD(krwlock_t *rwp)
85{
ac9cc135
AX
86 /*
87 * Linux 4.8 will set owner to 1 when read held instead of leave it
88 * NULL. So we check whether owner <= 1.
89 */
90 return (spl_rwsem_is_locked(SEM(rwp)) &&
91 (unsigned long)rw_owner(rwp) <= 1);
d28db80f
BB
92}
93
94static inline int
95RW_WRITE_HELD(krwlock_t *rwp)
96{
f6188ddd 97 return (spl_rwsem_is_locked(SEM(rwp)) && rw_owner(rwp) == current);
d28db80f
BB
98}
99
100static inline int
101RW_LOCK_HELD(krwlock_t *rwp)
102{
46aa7b39 103 return spl_rwsem_is_locked(SEM(rwp));
d28db80f
BB
104}
105
106/*
107 * The following functions must be a #define and not static inline.
108 * This ensures that the native linux semaphore functions (down/up)
109 * will be correctly located in the users code which is important
110 * for the built in kernel lock analysis tools
111 */
112#define rw_init(rwp, name, type, arg) \
113({ \
0e77fc11
BB
114 static struct lock_class_key __key; \
115 \
116 __init_rwsem(SEM(rwp), #rwp, &__key); \
d28db80f 117 spl_rw_clear_owner(rwp); \
e811949a 118})
f1ca4da6 119
d28db80f
BB
120#define rw_destroy(rwp) \
121({ \
122 VERIFY(!RW_LOCK_HELD(rwp)); \
e811949a 123})
ed61a7d0 124
d28db80f
BB
125#define rw_tryenter(rwp, rw) \
126({ \
127 int _rc_ = 0; \
128 \
129 switch (rw) { \
130 case RW_READER: \
131 _rc_ = down_read_trylock(SEM(rwp)); \
132 break; \
133 case RW_WRITER: \
134 if ((_rc_ = down_write_trylock(SEM(rwp)))) \
135 spl_rw_set_owner(rwp); \
136 break; \
137 default: \
55abb092 138 VERIFY(0); \
d28db80f
BB
139 } \
140 _rc_; \
e8b31e84 141})
e8b31e84 142
d28db80f
BB
143#define rw_enter(rwp, rw) \
144({ \
145 switch (rw) { \
146 case RW_READER: \
147 down_read(SEM(rwp)); \
148 break; \
149 case RW_WRITER: \
150 down_write(SEM(rwp)); \
151 spl_rw_set_owner(rwp); \
152 break; \
153 default: \
55abb092 154 VERIFY(0); \
d28db80f 155 } \
e811949a 156})
f1ca4da6 157
d28db80f
BB
158#define rw_exit(rwp) \
159({ \
160 if (RW_WRITE_HELD(rwp)) { \
161 spl_rw_clear_owner(rwp); \
162 up_write(SEM(rwp)); \
163 } else { \
164 ASSERT(RW_READ_HELD(rwp)); \
165 up_read(SEM(rwp)); \
166 } \
167})
168
169#define rw_downgrade(rwp) \
170({ \
171 spl_rw_clear_owner(rwp); \
172 downgrade_write(SEM(rwp)); \
173})
174
32f5faff 175/*
0f836a62
AX
176 * This implementation of rw_tryupgrade() behaves slightly differently
177 * from its counterparts on other platforms. It drops the RW_READER lock
178 * and then acquires the RW_WRITER lock leaving a small window where no
179 * lock is held. On other platforms the lock is never released during
180 * the upgrade process. This is necessary under Linux because the kernel
181 * does not provide an upgrade function.
32f5faff 182 */
0f836a62
AX
183#define rw_tryupgrade(rwp) \
184({ \
185 int _rc_ = 0; \
186 \
187 if (RW_WRITE_HELD(rwp)) { \
188 _rc_ = 1; \
189 } else { \
190 if ((_rc_ = rwsem_tryupgrade(SEM(rwp)))) \
191 spl_rw_set_owner(rwp); \
192 } \
193 _rc_; \
d28db80f
BB
194})
195
196int spl_rw_init(void);
197void spl_rw_fini(void);
198
e8b31e84 199#endif /* _SPL_RWLOCK_H */