1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2000-2006 Silicon Graphics, Inc.
4  * All Rights Reserved.
5  */
6 #ifndef __XFS_SUPPORT_MRLOCK_H__
7 #define __XFS_SUPPORT_MRLOCK_H__
8 
9 #include <linux/rwsem.h>
10 
11 typedef struct {
12 	struct rw_semaphore	mr_lock;
13 #if defined(DEBUG) || defined(XFS_WARN)
14 	int			mr_writer;
15 #endif
16 } mrlock_t;
17 
18 #if defined(DEBUG) || defined(XFS_WARN)
19 #define mrinit(mrp, name)	\
20 	do { (mrp)->mr_writer = 0; init_rwsem(&(mrp)->mr_lock); } while (0)
21 #else
22 #define mrinit(mrp, name)	\
23 	do { init_rwsem(&(mrp)->mr_lock); } while (0)
24 #endif
25 
26 #define mrlock_init(mrp, t,n,s)	mrinit(mrp, n)
27 #define mrfree(mrp)		do { } while (0)
28 
mraccess_nested(mrlock_t * mrp,int subclass)29 static inline void mraccess_nested(mrlock_t *mrp, int subclass)
30 {
31 	down_read_nested(&mrp->mr_lock, subclass);
32 }
33 
mrupdate_nested(mrlock_t * mrp,int subclass)34 static inline void mrupdate_nested(mrlock_t *mrp, int subclass)
35 {
36 	down_write_nested(&mrp->mr_lock, subclass);
37 #if defined(DEBUG) || defined(XFS_WARN)
38 	mrp->mr_writer = 1;
39 #endif
40 }
41 
mrtryaccess(mrlock_t * mrp)42 static inline int mrtryaccess(mrlock_t *mrp)
43 {
44 	return down_read_trylock(&mrp->mr_lock);
45 }
46 
mrtryupdate(mrlock_t * mrp)47 static inline int mrtryupdate(mrlock_t *mrp)
48 {
49 	if (!down_write_trylock(&mrp->mr_lock))
50 		return 0;
51 #if defined(DEBUG) || defined(XFS_WARN)
52 	mrp->mr_writer = 1;
53 #endif
54 	return 1;
55 }
56 
mrunlock_excl(mrlock_t * mrp)57 static inline void mrunlock_excl(mrlock_t *mrp)
58 {
59 #if defined(DEBUG) || defined(XFS_WARN)
60 	mrp->mr_writer = 0;
61 #endif
62 	up_write(&mrp->mr_lock);
63 }
64 
mrunlock_shared(mrlock_t * mrp)65 static inline void mrunlock_shared(mrlock_t *mrp)
66 {
67 	up_read(&mrp->mr_lock);
68 }
69 
mrdemote(mrlock_t * mrp)70 static inline void mrdemote(mrlock_t *mrp)
71 {
72 #if defined(DEBUG) || defined(XFS_WARN)
73 	mrp->mr_writer = 0;
74 #endif
75 	downgrade_write(&mrp->mr_lock);
76 }
77 
78 #endif /* __XFS_SUPPORT_MRLOCK_H__ */
79