18d59ecb2SHans Petter Selasky /*-
28d59ecb2SHans Petter Selasky * Copyright (c) 2010 Isilon Systems, Inc.
38d59ecb2SHans Petter Selasky * Copyright (c) 2010 iX Systems, Inc.
48d59ecb2SHans Petter Selasky * Copyright (c) 2010 Panasas, Inc.
5684bcfecSHans Petter Selasky * Copyright (c) 2013-2017 Mellanox Technologies, Ltd.
68d59ecb2SHans Petter Selasky * All rights reserved.
78d59ecb2SHans Petter Selasky *
88d59ecb2SHans Petter Selasky * Redistribution and use in source and binary forms, with or without
98d59ecb2SHans Petter Selasky * modification, are permitted provided that the following conditions
108d59ecb2SHans Petter Selasky * are met:
118d59ecb2SHans Petter Selasky * 1. Redistributions of source code must retain the above copyright
128d59ecb2SHans Petter Selasky * notice unmodified, this list of conditions, and the following
138d59ecb2SHans Petter Selasky * disclaimer.
148d59ecb2SHans Petter Selasky * 2. Redistributions in binary form must reproduce the above copyright
158d59ecb2SHans Petter Selasky * notice, this list of conditions and the following disclaimer in the
168d59ecb2SHans Petter Selasky * documentation and/or other materials provided with the distribution.
178d59ecb2SHans Petter Selasky *
188d59ecb2SHans Petter Selasky * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
198d59ecb2SHans Petter Selasky * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
208d59ecb2SHans Petter Selasky * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
218d59ecb2SHans Petter Selasky * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
228d59ecb2SHans Petter Selasky * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
238d59ecb2SHans Petter Selasky * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
248d59ecb2SHans Petter Selasky * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
258d59ecb2SHans Petter Selasky * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
268d59ecb2SHans Petter Selasky * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
278d59ecb2SHans Petter Selasky * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
288d59ecb2SHans Petter Selasky */
29307f78f3SVladimir Kondratyev #ifndef _LINUXKPI_LINUX_RWSEM_H_
30307f78f3SVladimir Kondratyev #define _LINUXKPI_LINUX_RWSEM_H_
318d59ecb2SHans Petter Selasky
328d59ecb2SHans Petter Selasky #include <sys/param.h>
338d59ecb2SHans Petter Selasky #include <sys/lock.h>
348d59ecb2SHans Petter Selasky #include <sys/sx.h>
351e3db1deSHans Petter Selasky #include <sys/libkern.h>
3619bf8ef5SHans Petter Selasky #include <sys/kernel.h>
378d59ecb2SHans Petter Selasky
388d59ecb2SHans Petter Selasky struct rw_semaphore {
398d59ecb2SHans Petter Selasky struct sx sx;
408d59ecb2SHans Petter Selasky };
418d59ecb2SHans Petter Selasky
428d59ecb2SHans Petter Selasky #define down_write(_rw) sx_xlock(&(_rw)->sx)
438d59ecb2SHans Petter Selasky #define up_write(_rw) sx_xunlock(&(_rw)->sx)
448d59ecb2SHans Petter Selasky #define down_read(_rw) sx_slock(&(_rw)->sx)
458d59ecb2SHans Petter Selasky #define up_read(_rw) sx_sunlock(&(_rw)->sx)
468d59ecb2SHans Petter Selasky #define down_read_trylock(_rw) !!sx_try_slock(&(_rw)->sx)
47f0b0f28fSJake Freeland #define down_read_killable(_rw) linux_down_read_killable(_rw)
488d59ecb2SHans Petter Selasky #define down_write_trylock(_rw) !!sx_try_xlock(&(_rw)->sx)
4994944062SHans Petter Selasky #define down_write_killable(_rw) linux_down_write_killable(_rw)
508d59ecb2SHans Petter Selasky #define downgrade_write(_rw) sx_downgrade(&(_rw)->sx)
518d59ecb2SHans Petter Selasky #define down_read_nested(_rw, _sc) down_read(_rw)
52684bcfecSHans Petter Selasky #define init_rwsem(_rw) linux_init_rwsem(_rw, rwsem_name("lnxrwsem"))
53f9413897SEmmanuel Vadot #define down_write_nest_lock(sem, _rw) down_write(_rw)
54684bcfecSHans Petter Selasky
55684bcfecSHans Petter Selasky #ifdef WITNESS_ALL
56684bcfecSHans Petter Selasky /* NOTE: the maximum WITNESS name is 64 chars */
57684bcfecSHans Petter Selasky #define __rwsem_name(name, file, line) \
58684bcfecSHans Petter Selasky (((const char *){file ":" #line "-" name}) + \
59684bcfecSHans Petter Selasky (sizeof(file) > 16 ? sizeof(file) - 16 : 0))
60684bcfecSHans Petter Selasky #else
61684bcfecSHans Petter Selasky #define __rwsem_name(name, file, line) name
62684bcfecSHans Petter Selasky #endif
63684bcfecSHans Petter Selasky #define _rwsem_name(...) __rwsem_name(__VA_ARGS__)
64684bcfecSHans Petter Selasky #define rwsem_name(name) _rwsem_name(name, __FILE__, __LINE__)
658d59ecb2SHans Petter Selasky
6619bf8ef5SHans Petter Selasky #define DECLARE_RWSEM(name) \
6719bf8ef5SHans Petter Selasky struct rw_semaphore name; \
6819bf8ef5SHans Petter Selasky static void name##_rwsem_init(void *arg) \
6919bf8ef5SHans Petter Selasky { \
70661a318cSHans Petter Selasky linux_init_rwsem(&name, rwsem_name(#name)); \
7119bf8ef5SHans Petter Selasky } \
72661a318cSHans Petter Selasky SYSINIT(name, SI_SUB_LOCK, SI_ORDER_SECOND, name##_rwsem_init, NULL)
7319bf8ef5SHans Petter Selasky
748d59ecb2SHans Petter Selasky static inline void
linux_init_rwsem(struct rw_semaphore * rw,const char * name)75684bcfecSHans Petter Selasky linux_init_rwsem(struct rw_semaphore *rw, const char *name)
768d59ecb2SHans Petter Selasky {
778d59ecb2SHans Petter Selasky
78684bcfecSHans Petter Selasky memset(rw, 0, sizeof(*rw));
79684bcfecSHans Petter Selasky sx_init_flags(&rw->sx, name, SX_NOWITNESS);
808d59ecb2SHans Petter Selasky }
818d59ecb2SHans Petter Selasky
82f0b0f28fSJake Freeland extern int linux_down_read_killable(struct rw_semaphore *);
8394944062SHans Petter Selasky extern int linux_down_write_killable(struct rw_semaphore *);
8494944062SHans Petter Selasky
85307f78f3SVladimir Kondratyev #endif /* _LINUXKPI_LINUX_RWSEM_H_ */
86