]>
Commit | Line | Data |
---|---|---|
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 | |
32 | typedef enum { | |
e811949a BB |
33 | RW_DRIVER = 2, |
34 | RW_DEFAULT = 4 | |
f1ca4da6 | 35 | } krw_type_t; |
36 | ||
37 | typedef enum { | |
d28db80f | 38 | RW_NONE = 0, |
e811949a BB |
39 | RW_WRITER = 1, |
40 | RW_READER = 2 | |
f1ca4da6 | 41 | } krw_t; |
42 | ||
a00b3eb5 CC |
43 | /* |
44 | * If CONFIG_RWSEM_SPIN_ON_OWNER is defined, rw_semaphore will have an owner | |
45 | * field, so we don't need our own. | |
46 | */ | |
d28db80f BB |
47 | typedef struct { |
48 | struct rw_semaphore rw_rwlock; | |
a00b3eb5 | 49 | #ifndef CONFIG_RWSEM_SPIN_ON_OWNER |
d28db80f | 50 | kthread_t *rw_owner; |
a00b3eb5 | 51 | #endif |
d28db80f | 52 | } krwlock_t; |
f1ca4da6 | 53 | |
a00b3eb5 | 54 | #define SEM(rwp) (&(rwp)->rw_rwlock) |
d28db80f | 55 | |
d28db80f BB |
56 | static inline void |
57 | spl_rw_set_owner(krwlock_t *rwp) | |
58 | { | |
a00b3eb5 CC |
59 | /* |
60 | * If CONFIG_RWSEM_SPIN_ON_OWNER is defined, down_write, up_write, | |
61 | * downgrade_write and __init_rwsem will set/clear owner for us. | |
62 | */ | |
63 | #ifndef CONFIG_RWSEM_SPIN_ON_OWNER | |
4f8e643a | 64 | rwp->rw_owner = current; |
a00b3eb5 | 65 | #endif |
d28db80f BB |
66 | } |
67 | ||
68 | static inline void | |
69 | spl_rw_clear_owner(krwlock_t *rwp) | |
70 | { | |
a00b3eb5 | 71 | #ifndef CONFIG_RWSEM_SPIN_ON_OWNER |
4f8e643a | 72 | rwp->rw_owner = NULL; |
a00b3eb5 | 73 | #endif |
d28db80f BB |
74 | } |
75 | ||
76 | static inline kthread_t * | |
77 | rw_owner(krwlock_t *rwp) | |
78 | { | |
a00b3eb5 CC |
79 | #ifdef CONFIG_RWSEM_SPIN_ON_OWNER |
80 | return SEM(rwp)->owner; | |
81 | #else | |
4f8e643a | 82 | return rwp->rw_owner; |
a00b3eb5 | 83 | #endif |
d28db80f BB |
84 | } |
85 | ||
86 | static inline int | |
87 | RW_READ_HELD(krwlock_t *rwp) | |
88 | { | |
86c16c59 | 89 | return (spl_rwsem_is_locked(SEM(rwp)) && rw_owner(rwp) == NULL); |
d28db80f BB |
90 | } |
91 | ||
92 | static inline int | |
93 | RW_WRITE_HELD(krwlock_t *rwp) | |
94 | { | |
4f8e643a | 95 | return (rw_owner(rwp) == current); |
d28db80f BB |
96 | } |
97 | ||
98 | static inline int | |
99 | RW_LOCK_HELD(krwlock_t *rwp) | |
100 | { | |
46aa7b39 | 101 | return spl_rwsem_is_locked(SEM(rwp)); |
d28db80f BB |
102 | } |
103 | ||
104 | /* | |
105 | * The following functions must be a #define and not static inline. | |
106 | * This ensures that the native linux semaphore functions (down/up) | |
107 | * will be correctly located in the users code which is important | |
108 | * for the built in kernel lock analysis tools | |
109 | */ | |
110 | #define rw_init(rwp, name, type, arg) \ | |
111 | ({ \ | |
0e77fc11 BB |
112 | static struct lock_class_key __key; \ |
113 | \ | |
114 | __init_rwsem(SEM(rwp), #rwp, &__key); \ | |
d28db80f | 115 | spl_rw_clear_owner(rwp); \ |
e811949a | 116 | }) |
f1ca4da6 | 117 | |
d28db80f BB |
118 | #define rw_destroy(rwp) \ |
119 | ({ \ | |
120 | VERIFY(!RW_LOCK_HELD(rwp)); \ | |
e811949a | 121 | }) |
ed61a7d0 | 122 | |
d28db80f BB |
123 | #define rw_tryenter(rwp, rw) \ |
124 | ({ \ | |
125 | int _rc_ = 0; \ | |
126 | \ | |
127 | switch (rw) { \ | |
128 | case RW_READER: \ | |
129 | _rc_ = down_read_trylock(SEM(rwp)); \ | |
130 | break; \ | |
131 | case RW_WRITER: \ | |
132 | if ((_rc_ = down_write_trylock(SEM(rwp)))) \ | |
133 | spl_rw_set_owner(rwp); \ | |
134 | break; \ | |
135 | default: \ | |
55abb092 | 136 | VERIFY(0); \ |
d28db80f BB |
137 | } \ |
138 | _rc_; \ | |
e8b31e84 | 139 | }) |
e8b31e84 | 140 | |
d28db80f BB |
141 | #define rw_enter(rwp, rw) \ |
142 | ({ \ | |
143 | switch (rw) { \ | |
144 | case RW_READER: \ | |
145 | down_read(SEM(rwp)); \ | |
146 | break; \ | |
147 | case RW_WRITER: \ | |
148 | down_write(SEM(rwp)); \ | |
149 | spl_rw_set_owner(rwp); \ | |
150 | break; \ | |
151 | default: \ | |
55abb092 | 152 | VERIFY(0); \ |
d28db80f | 153 | } \ |
e811949a | 154 | }) |
f1ca4da6 | 155 | |
d28db80f BB |
156 | #define rw_exit(rwp) \ |
157 | ({ \ | |
158 | if (RW_WRITE_HELD(rwp)) { \ | |
159 | spl_rw_clear_owner(rwp); \ | |
160 | up_write(SEM(rwp)); \ | |
161 | } else { \ | |
162 | ASSERT(RW_READ_HELD(rwp)); \ | |
163 | up_read(SEM(rwp)); \ | |
164 | } \ | |
165 | }) | |
166 | ||
167 | #define rw_downgrade(rwp) \ | |
168 | ({ \ | |
169 | spl_rw_clear_owner(rwp); \ | |
170 | downgrade_write(SEM(rwp)); \ | |
171 | }) | |
172 | ||
ef6c1368 | 173 | #if defined(CONFIG_RWSEM_GENERIC_SPINLOCK) |
a00b3eb5 CC |
174 | #ifdef CONFIG_RWSEM_SPIN_ON_OWNER |
175 | #error spinlock rwsem should not have spin on owner | |
176 | #endif | |
32f5faff BB |
177 | /* |
178 | * For the generic implementations of rw-semaphores the following is | |
179 | * true. If your semaphore implementation internally represents the | |
180 | * semaphore state differently then special case handling is required. | |
181 | * - if activity/count is 0 then there are no active readers or writers | |
182 | * - if activity/count is +ve then that is the number of active readers | |
183 | * - if activity/count is -1 then there is one active writer | |
184 | */ | |
185 | ||
186 | extern void __up_read_locked(struct rw_semaphore *); | |
187 | extern int __down_write_trylock_locked(struct rw_semaphore *); | |
188 | ||
d28db80f BB |
189 | #define rw_tryupgrade(rwp) \ |
190 | ({ \ | |
191 | unsigned long _flags_; \ | |
192 | int _rc_ = 0; \ | |
193 | \ | |
588d9004 | 194 | spl_rwsem_lock_irqsave(&SEM(rwp)->wait_lock, _flags_); \ |
32f5faff BB |
195 | if ((list_empty(&SEM(rwp)->wait_list)) && \ |
196 | (SEM(rwp)->activity == 1)) { \ | |
197 | __up_read_locked(SEM(rwp)); \ | |
198 | VERIFY(_rc_ = __down_write_trylock_locked(SEM(rwp))); \ | |
d28db80f BB |
199 | (rwp)->rw_owner = current; \ |
200 | } \ | |
588d9004 | 201 | spl_rwsem_unlock_irqrestore(&SEM(rwp)->wait_lock, _flags_); \ |
d28db80f BB |
202 | _rc_; \ |
203 | }) | |
ef6c1368 BB |
204 | #else |
205 | /* | |
32f5faff BB |
206 | * rw_tryupgrade() can be implemented correctly but for each supported |
207 | * arch we will need a custom implementation. For the x86 implementation | |
208 | * it looks like a custom cmpxchg() to atomically check and promote the | |
209 | * rwsem would be safe. For now that's not worth the trouble so in this | |
210 | * case rw_tryupgrade() has just been disabled. | |
ef6c1368 BB |
211 | */ |
212 | #define rw_tryupgrade(rwp) ({ 0; }) | |
213 | #endif | |
d28db80f BB |
214 | |
215 | int spl_rw_init(void); | |
216 | void spl_rw_fini(void); | |
217 | ||
e8b31e84 | 218 | #endif /* _SPL_RWLOCK_H */ |