Whamcloud - gitweb
LU-969 debug: reduce stack usage
[fs/lustre-release.git] / libcfs / include / libcfs / linux / linux-lock.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * libcfs/include/libcfs/linux/linux-lock.h
37  *
38  * Basic library routines.
39  */
40
41 #ifndef __LIBCFS_LINUX_CFS_LOCK_H__
42 #define __LIBCFS_LINUX_CFS_LOCK_H__
43
44 #ifndef __LIBCFS_LIBCFS_H__
45 #error Do not #include this file directly. #include <libcfs/libcfs.h> instead
46 #endif
47
48 #ifndef __KERNEL__
49 #error This include is only for kernel use.
50 #endif
51
52 #include <linux/smp_lock.h>
53 #include <linux/mutex.h>
54
55 /*
56  * IMPORTANT !!!!!!!!
57  *
58  * All locks' declaration are not guaranteed to be initialized,
59  * Althought some of they are initialized in Linux. All locks
60  * declared by CFS_DECL_* should be initialized explicitly.
61  */
62
63 /*
64  * spin_lock "implementation" (use Linux kernel's primitives)
65  *
66  * - spin_lock_init(x)
67  * - spin_lock(x)
68  * - spin_lock_bh(x)
69  * - spin_lock_bh_init(x)
70  * - spin_unlock(x)
71  * - spin_unlock_bh(x)
72  * - spin_trylock(x)
73  * - spin_is_locked(x)
74  *
75  * - spin_lock_irq(x)
76  * - spin_lock_irqsave(x, f)
77  * - spin_unlock_irqrestore(x, f)
78  * - read_lock_irqsave(lock, f)
79  * - write_lock_irqsave(lock, f)
80  * - write_unlock_irqrestore(lock, f)
81  *
82  * - SPIN_LOCK_UNLOCKED
83  */
84
85 /*
86  * spinlock "implementation"
87  */
88
89 typedef spinlock_t cfs_spinlock_t;
90
91 #define cfs_spin_lock_init(lock)             spin_lock_init(lock)
92 #define cfs_spin_lock(lock)                  spin_lock(lock)
93 #define cfs_spin_lock_bh(lock)               spin_lock_bh(lock)
94 #define cfs_spin_lock_bh_init(lock)          spin_lock_bh_init(lock)
95 #define cfs_spin_unlock(lock)                spin_unlock(lock)
96 #define cfs_spin_unlock_bh(lock)             spin_unlock_bh(lock)
97 #define cfs_spin_trylock(lock)               spin_trylock(lock)
98 #define cfs_spin_is_locked(lock)             spin_is_locked(lock)
99
100 #define cfs_spin_lock_irq(lock)              spin_lock_irq(lock)
101 #define cfs_spin_unlock_irq(lock)            spin_unlock_irq(lock)
102 #define cfs_read_lock_irqsave(lock, f)       read_lock_irqsave(lock, f)
103 #define cfs_write_lock_irqsave(lock, f)      write_lock_irqsave(lock, f)
104 #define cfs_write_unlock_irqrestore(lock, f) write_unlock_irqrestore(lock, f)
105 #define cfs_spin_lock_irqsave(lock, f)       spin_lock_irqsave(lock, f)
106 #define cfs_spin_unlock_irqrestore(lock, f)  spin_unlock_irqrestore(lock, f)
107
108 #define CFS_SPIN_LOCK_UNLOCKED               SPIN_LOCK_UNLOCKED
109
110 /*
111  * rw_semaphore "implementation" (use Linux kernel's primitives)
112  *
113  * - sema_init(x)
114  * - init_rwsem(x)
115  * - down_read(x)
116  * - up_read(x)
117  * - down_write(x)
118  * - up_write(x)
119  */
120 typedef struct rw_semaphore cfs_rw_semaphore_t;
121
122 #define cfs_sema_init(s, val)     sema_init(s, val)
123 #define cfs_init_rwsem(s)         init_rwsem(s)
124 #define cfs_down_read(s)          down_read(s)
125 #define cfs_down_read_trylock(s)  down_read_trylock(s)
126 #define cfs_up_read(s)            up_read(s)
127 #define cfs_down_write(s)         down_write(s)
128 #define cfs_down_write_trylock(s) down_write_trylock(s)
129 #define cfs_up_write(s)           up_write(s)
130
131 #define cfs_fini_rwsem(s)         do {} while(0)
132
133 #define CFS_DECLARE_RWSEM(name)   DECLARE_RWSEM(name)
134
135 /*
136  * semaphore "implementation" (use Linux kernel's primitives)
137  */
138 typedef struct semaphore      cfs_semaphore_t;
139
140 /*
141  * rwlock_t "implementation" (use Linux kernel's primitives)
142  *
143  * - rwlock_init(x)
144  * - read_lock(x)
145  * - read_unlock(x)
146  * - write_lock(x)
147  * - write_unlock(x)
148  * - write_lock_bh(x)
149  * - write_unlock_bh(x)
150  *
151  * - RW_LOCK_UNLOCKED
152  */
153 typedef rwlock_t cfs_rwlock_t;
154
155 #define cfs_rwlock_init(lock)                  rwlock_init(lock)
156 #define cfs_read_lock(lock)                    read_lock(lock)
157 #define cfs_read_unlock(lock)                  read_unlock(lock)
158 #define cfs_read_unlock_irqrestore(lock,flags) \
159         read_unlock_irqrestore(lock, flags)
160 #define cfs_write_lock(lock)                   write_lock(lock)
161 #define cfs_write_unlock(lock)                 write_unlock(lock)
162 #define cfs_write_lock_bh(lock)                write_lock_bh(lock)
163 #define cfs_write_unlock_bh(lock)              write_unlock_bh(lock)
164
165 #define CFS_RW_LOCK_UNLOCKED                   RW_LOCK_UNLOCKED
166
167 /*
168  * completion "implementation" (use Linux kernel's primitives)
169  *
170  * - DECLARE_COMPLETION(work)
171  * - INIT_COMPLETION(c)
172  * - COMPLETION_INITIALIZER(work)
173  * - init_completion(c)
174  * - complete(c)
175  * - wait_for_completion(c)
176  * - wait_for_completion_interruptible(c)
177  * - fini_completion(c)
178  */
179 typedef struct completion cfs_completion_t;
180
181 #define CFS_DECLARE_COMPLETION(work)             DECLARE_COMPLETION(work)
182 #define CFS_INIT_COMPLETION(c)                   INIT_COMPLETION(c)
183 #define CFS_COMPLETION_INITIALIZER(work)         COMPLETION_INITIALIZER(work)
184 #define cfs_init_completion(c)                   init_completion(c)
185 #define cfs_complete(c)                          complete(c)
186 #define cfs_wait_for_completion(c)               wait_for_completion(c)
187 #define cfs_wait_for_completion_interruptible(c) \
188         wait_for_completion_interruptible(c)
189 #define cfs_complete_and_exit(c, code)           complete_and_exit(c, code)
190 #define cfs_fini_completion(c)                   do { } while (0)
191
192 /*
193  * mutex "implementation" (use Linux kernel's primitives)
194  *
195  * - DECLARE_MUTEX(name)
196  * - mutex_init(x)
197  * - init_mutex(x)
198  * - init_mutex_locked(x)
199  * - init_MUTEX_LOCKED(x)
200  * - mutex_up(x)
201  * - mutex_down(x)
202  * - up(x)
203  * - down(x)
204  * - mutex_down_trylock(x)
205  * - mutex_lock(x)
206  * - mutex_unlock(x)
207  */
208 typedef struct mutex cfs_mutex_t;
209
210 #define CFS_DEFINE_MUTEX(name)             DEFINE_MUTEX(name)
211 #define CFS_DECLARE_MUTEX(name)            DECLARE_MUTEX(name)
212
213 #define cfs_mutex_init(x)                   mutex_init(x)
214 #define cfs_init_mutex(x)                   init_MUTEX(x)
215 #define cfs_init_mutex_locked(x)            init_MUTEX_LOCKED(x)
216 #define cfs_mutex_up(x)                     up(x)
217 #define cfs_mutex_down(x)                   down(x)
218 #define cfs_up(x)                           up(x)
219 #define cfs_down(x)                         down(x)
220 #define cfs_down_interruptible(x)           down_interruptible(x)
221 #define cfs_mutex_down_trylock(x)           down_trylock(x)
222 #define cfs_mutex_lock(x)                   mutex_lock(x)
223 #define cfs_mutex_unlock(x)                 mutex_unlock(x)
224 #define cfs_mutex_trylock(x)                mutex_trylock(x)
225
226 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,16)
227
228 /**************************************************************************
229  *
230  * Mutex interface from newer Linux kernels.
231  *
232  * this augments compatibility interface from include/linux/mutex.h
233  *
234  **************************************************************************/
235
236 static inline void cfs_mutex_destroy(cfs_mutex_t *lock)
237 {
238 }
239
240 /*
241  * This is for use in assertions _only_, i.e., this function should always
242  * return 1.
243  *
244  * \retval 1 mutex is locked.
245  *
246  * \retval 0 mutex is not locked. This should never happen.
247  */
248 static inline int cfs_mutex_is_locked(cfs_mutex_t *lock)
249 {
250         return 1;
251 }
252 #else /* LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16) */
253 #define cfs_mutex_destroy(x)    mutex_destroy(x)
254 #define cfs_mutex_is_locked(x)  mutex_is_locked(x)
255 #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2,6,16) */
256
257 /*
258  * Kernel locking primitives
259  *
260  * - lock_kernel
261  * - unlock_kernel
262  */
263 #define cfs_lock_kernel()      lock_kernel()
264 #define cfs_unlock_kernel()    unlock_kernel()
265
266 #ifndef lockdep_set_class
267
268 /**************************************************************************
269  *
270  * Lockdep "implementation". Also see liblustre.h
271  *
272  **************************************************************************/
273
274 typedef struct cfs_lock_class_key {
275         ;
276 } cfs_lock_class_key_t;
277
278 #define cfs_lockdep_set_class(lock, key) \
279         do { (void)sizeof (lock);(void)sizeof (key); } while (0)
280 /* This has to be a macro, so that `subclass' can be undefined in kernels that
281  * do not support lockdep. */
282
283
284 static inline void cfs_lockdep_off(void)
285 {
286 }
287
288 static inline void cfs_lockdep_on(void)
289 {
290 }
291 #else
292 typedef struct lock_class_key cfs_lock_class_key_t;
293
294 #define cfs_lockdep_set_class(lock, key) lockdep_set_class(lock, key)
295 #define cfs_lockdep_off()                lockdep_off()
296 #define cfs_lockdep_on()                 lockdep_on()
297 #endif /* lockdep_set_class */
298
299 #ifndef CONFIG_DEBUG_LOCK_ALLOC
300 #ifndef mutex_lock_nested
301 #define cfs_mutex_lock_nested(mutex, subclass) mutex_lock(mutex)
302 #else
303 #define cfs_mutex_lock_nested(mutex, subclass) \
304         mutex_lock_nested(mutex, subclass)
305 #endif
306
307 #ifndef spin_lock_nested
308 #define cfs_spin_lock_nested(lock, subclass) spin_lock(lock)
309 #else
310 #define cfs_spin_lock_nested(lock, subclass) spin_lock_nested(lock, subclass)
311 #endif
312
313 #ifndef down_read_nested
314 #define cfs_down_read_nested(lock, subclass) down_read(lock)
315 #else
316 #define cfs_down_read_nested(lock, subclass) down_read_nested(lock, subclass)
317 #endif
318
319 #ifndef down_write_nested
320 #define cfs_down_write_nested(lock, subclass) down_write(lock)
321 #else
322 #define cfs_down_write_nested(lock, subclass) down_write_nested(lock, subclass)
323 #endif
324 #else /* CONFIG_DEBUG_LOCK_ALLOC is defined */
325 #define cfs_mutex_lock_nested(mutex, subclass) \
326         mutex_lock_nested(mutex, subclass)
327 #define cfs_spin_lock_nested(lock, subclass) spin_lock_nested(lock, subclass)
328 #define cfs_down_read_nested(lock, subclass) down_read_nested(lock, subclass)
329 #define cfs_down_write_nested(lock, subclass) down_write_nested(lock, subclass)
330 #endif /* CONFIG_DEBUG_LOCK_ALLOC */
331
332
333 #endif /* __LIBCFS_LINUX_CFS_LOCK_H__ */