blob: 79155eec341bff4b6718a0b50d6ec60475c329f9 [file] [log] [blame]
Dave Chinner0b61f8a2018-06-05 19:42:14 -07001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Ingo Molnar72c93bc2006-06-09 14:57:01 +10003 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11004 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
6#ifndef __XFS_SUPPORT_MRLOCK_H__
7#define __XFS_SUPPORT_MRLOCK_H__
8
9#include <linux/rwsem.h>
10
Linus Torvalds1da177e2005-04-16 15:20:36 -070011typedef struct {
12 struct rw_semaphore mr_lock;
Dave Chinner742ae1e2013-04-30 21:39:34 +100013#if defined(DEBUG) || defined(XFS_WARN)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 int mr_writer;
Christoph Hellwig579aa9c2008-04-22 17:34:00 +100015#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070016} mrlock_t;
17
Dave Chinner742ae1e2013-04-30 21:39:34 +100018#if defined(DEBUG) || defined(XFS_WARN)
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#define mrinit(mrp, name) \
Ingo Molnar72c93bc2006-06-09 14:57:01 +100020 do { (mrp)->mr_writer = 0; init_rwsem(&(mrp)->mr_lock); } while (0)
Christoph Hellwig579aa9c2008-04-22 17:34:00 +100021#else
22#define mrinit(mrp, name) \
23 do { init_rwsem(&(mrp)->mr_lock); } while (0)
24#endif
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#define mrlock_init(mrp, t,n,s) mrinit(mrp, n)
27#define mrfree(mrp) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +100029static inline void mraccess_nested(mrlock_t *mrp, int subclass)
30{
31 down_read_nested(&mrp->mr_lock, subclass);
32}
33
34static inline void mrupdate_nested(mrlock_t *mrp, int subclass)
35{
36 down_write_nested(&mrp->mr_lock, subclass);
Dave Chinner742ae1e2013-04-30 21:39:34 +100037#if defined(DEBUG) || defined(XFS_WARN)
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +100038 mrp->mr_writer = 1;
Christoph Hellwig579aa9c2008-04-22 17:34:00 +100039#endif
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +100040}
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042static inline int mrtryaccess(mrlock_t *mrp)
43{
44 return down_read_trylock(&mrp->mr_lock);
45}
46
47static inline int mrtryupdate(mrlock_t *mrp)
48{
49 if (!down_write_trylock(&mrp->mr_lock))
50 return 0;
Dave Chinner742ae1e2013-04-30 21:39:34 +100051#if defined(DEBUG) || defined(XFS_WARN)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 mrp->mr_writer = 1;
Christoph Hellwig579aa9c2008-04-22 17:34:00 +100053#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 return 1;
55}
56
Christoph Hellwig579aa9c2008-04-22 17:34:00 +100057static inline void mrunlock_excl(mrlock_t *mrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
Dave Chinner742ae1e2013-04-30 21:39:34 +100059#if defined(DEBUG) || defined(XFS_WARN)
Christoph Hellwig579aa9c2008-04-22 17:34:00 +100060 mrp->mr_writer = 0;
61#endif
62 up_write(&mrp->mr_lock);
63}
64
65static inline void mrunlock_shared(mrlock_t *mrp)
66{
67 up_read(&mrp->mr_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068}
69
70static inline void mrdemote(mrlock_t *mrp)
71{
Dave Chinner742ae1e2013-04-30 21:39:34 +100072#if defined(DEBUG) || defined(XFS_WARN)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 mrp->mr_writer = 0;
Christoph Hellwig579aa9c2008-04-22 17:34:00 +100074#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 downgrade_write(&mrp->mr_lock);
76}
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#endif /* __XFS_SUPPORT_MRLOCK_H__ */