blob: e3c92d19e54079e5ffc950adf8974c35eee0703e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Ingo Molnar72c93bc2006-06-09 14:57:01 +10002 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11003 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
18#ifndef __XFS_SUPPORT_MRLOCK_H__
19#define __XFS_SUPPORT_MRLOCK_H__
20
21#include <linux/rwsem.h>
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023typedef struct {
24 struct rw_semaphore mr_lock;
Dave Chinner742ae1e2013-04-30 21:39:34 +100025#if defined(DEBUG) || defined(XFS_WARN)
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 int mr_writer;
Christoph Hellwig579aa9c2008-04-22 17:34:00 +100027#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070028} mrlock_t;
29
Dave Chinner742ae1e2013-04-30 21:39:34 +100030#if defined(DEBUG) || defined(XFS_WARN)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#define mrinit(mrp, name) \
Ingo Molnar72c93bc2006-06-09 14:57:01 +100032 do { (mrp)->mr_writer = 0; init_rwsem(&(mrp)->mr_lock); } while (0)
Christoph Hellwig579aa9c2008-04-22 17:34:00 +100033#else
34#define mrinit(mrp, name) \
35 do { init_rwsem(&(mrp)->mr_lock); } while (0)
36#endif
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#define mrlock_init(mrp, t,n,s) mrinit(mrp, n)
39#define mrfree(mrp) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +100041static inline void mraccess_nested(mrlock_t *mrp, int subclass)
42{
43 down_read_nested(&mrp->mr_lock, subclass);
44}
45
46static inline void mrupdate_nested(mrlock_t *mrp, int subclass)
47{
48 down_write_nested(&mrp->mr_lock, subclass);
Dave Chinner742ae1e2013-04-30 21:39:34 +100049#if defined(DEBUG) || defined(XFS_WARN)
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +100050 mrp->mr_writer = 1;
Christoph Hellwig579aa9c2008-04-22 17:34:00 +100051#endif
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +100052}
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054static inline int mrtryaccess(mrlock_t *mrp)
55{
56 return down_read_trylock(&mrp->mr_lock);
57}
58
59static inline int mrtryupdate(mrlock_t *mrp)
60{
61 if (!down_write_trylock(&mrp->mr_lock))
62 return 0;
Dave Chinner742ae1e2013-04-30 21:39:34 +100063#if defined(DEBUG) || defined(XFS_WARN)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 mrp->mr_writer = 1;
Christoph Hellwig579aa9c2008-04-22 17:34:00 +100065#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 return 1;
67}
68
Christoph Hellwig579aa9c2008-04-22 17:34:00 +100069static inline void mrunlock_excl(mrlock_t *mrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
Dave Chinner742ae1e2013-04-30 21:39:34 +100071#if defined(DEBUG) || defined(XFS_WARN)
Christoph Hellwig579aa9c2008-04-22 17:34:00 +100072 mrp->mr_writer = 0;
73#endif
74 up_write(&mrp->mr_lock);
75}
76
77static inline void mrunlock_shared(mrlock_t *mrp)
78{
79 up_read(&mrp->mr_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
81
82static inline void mrdemote(mrlock_t *mrp)
83{
Dave Chinner742ae1e2013-04-30 21:39:34 +100084#if defined(DEBUG) || defined(XFS_WARN)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 mrp->mr_writer = 0;
Christoph Hellwig579aa9c2008-04-22 17:34:00 +100086#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 downgrade_write(&mrp->mr_lock);
88}
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090#endif /* __XFS_SUPPORT_MRLOCK_H__ */