[XFS] simplify xfs_lookup
[deliverable/linux.git] / fs / xfs / linux-2.6 / mrlock.h
CommitLineData
1da177e4 1/*
72c93bcc 2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
7b718769 3 * All Rights Reserved.
1da177e4 4 *
7b718769
NS
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
1da177e4
LT
7 * published by the Free Software Foundation.
8 *
7b718769
NS
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
1da177e4 13 *
7b718769
NS
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1da177e4
LT
17 */
18#ifndef __XFS_SUPPORT_MRLOCK_H__
19#define __XFS_SUPPORT_MRLOCK_H__
20
21#include <linux/rwsem.h>
22
23enum { MR_NONE, MR_ACCESS, MR_UPDATE };
24
25typedef struct {
26 struct rw_semaphore mr_lock;
27 int mr_writer;
28} mrlock_t;
29
30#define mrinit(mrp, name) \
72c93bcc 31 do { (mrp)->mr_writer = 0; init_rwsem(&(mrp)->mr_lock); } while (0)
1da177e4
LT
32#define mrlock_init(mrp, t,n,s) mrinit(mrp, n)
33#define mrfree(mrp) do { } while (0)
1da177e4 34
a3227fb9 35static inline void mraccess(mrlock_t *mrp)
1da177e4
LT
36{
37 down_read(&mrp->mr_lock);
38}
39
a3227fb9 40static inline void mrupdate(mrlock_t *mrp)
1da177e4
LT
41{
42 down_write(&mrp->mr_lock);
43 mrp->mr_writer = 1;
44}
45
f7c66ce3
LM
46static inline void mraccess_nested(mrlock_t *mrp, int subclass)
47{
48 down_read_nested(&mrp->mr_lock, subclass);
49}
50
51static inline void mrupdate_nested(mrlock_t *mrp, int subclass)
52{
53 down_write_nested(&mrp->mr_lock, subclass);
54 mrp->mr_writer = 1;
55}
56
57
1da177e4
LT
58static inline int mrtryaccess(mrlock_t *mrp)
59{
60 return down_read_trylock(&mrp->mr_lock);
61}
62
63static inline int mrtryupdate(mrlock_t *mrp)
64{
65 if (!down_write_trylock(&mrp->mr_lock))
66 return 0;
67 mrp->mr_writer = 1;
68 return 1;
69}
70
71static inline void mrunlock(mrlock_t *mrp)
72{
73 if (mrp->mr_writer) {
74 mrp->mr_writer = 0;
75 up_write(&mrp->mr_lock);
76 } else {
77 up_read(&mrp->mr_lock);
78 }
79}
80
81static inline void mrdemote(mrlock_t *mrp)
82{
83 mrp->mr_writer = 0;
84 downgrade_write(&mrp->mr_lock);
85}
86
87#ifdef DEBUG
88/*
89 * Debug-only routine, without some platform-specific asm code, we can
90 * now only answer requests regarding whether we hold the lock for write
91 * (reader state is outside our visibility, we only track writer state).
c41564b5 92 * Note: means !ismrlocked would give false positives, so don't do that.
1da177e4
LT
93 */
94static inline int ismrlocked(mrlock_t *mrp, int type)
95{
96 if (mrp && type == MR_UPDATE)
97 return mrp->mr_writer;
98 return 1;
99}
100#endif
101
102#endif /* __XFS_SUPPORT_MRLOCK_H__ */
This page took 0.627264 seconds and 5 git commands to generate.