Dave Chinner | 0b61f8a | 2018-06-05 19:42:14 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Ingo Molnar | 72c93bc | 2006-06-09 14:57:01 +1000 | [diff] [blame] | 3 | * Copyright (c) 2000-2006 Silicon Graphics, Inc. |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 4 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | */ |
| 6 | #ifndef __XFS_SUPPORT_MRLOCK_H__ |
| 7 | #define __XFS_SUPPORT_MRLOCK_H__ |
| 8 | |
| 9 | #include <linux/rwsem.h> |
| 10 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | typedef struct { |
| 12 | struct rw_semaphore mr_lock; |
Dave Chinner | 742ae1e | 2013-04-30 21:39:34 +1000 | [diff] [blame] | 13 | #if defined(DEBUG) || defined(XFS_WARN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | int mr_writer; |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 15 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | } mrlock_t; |
| 17 | |
Dave Chinner | 742ae1e | 2013-04-30 21:39:34 +1000 | [diff] [blame] | 18 | #if defined(DEBUG) || defined(XFS_WARN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #define mrinit(mrp, name) \ |
Ingo Molnar | 72c93bc | 2006-06-09 14:57:01 +1000 | [diff] [blame] | 20 | do { (mrp)->mr_writer = 0; init_rwsem(&(mrp)->mr_lock); } while (0) |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 21 | #else |
| 22 | #define mrinit(mrp, name) \ |
| 23 | do { init_rwsem(&(mrp)->mr_lock); } while (0) |
| 24 | #endif |
| 25 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #define mrlock_init(mrp, t,n,s) mrinit(mrp, n) |
| 27 | #define mrfree(mrp) do { } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
Lachlan McIlroy | f7c66ce | 2007-05-08 13:50:19 +1000 | [diff] [blame] | 29 | static inline void mraccess_nested(mrlock_t *mrp, int subclass) |
| 30 | { |
| 31 | down_read_nested(&mrp->mr_lock, subclass); |
| 32 | } |
| 33 | |
| 34 | static inline void mrupdate_nested(mrlock_t *mrp, int subclass) |
| 35 | { |
| 36 | down_write_nested(&mrp->mr_lock, subclass); |
Dave Chinner | 742ae1e | 2013-04-30 21:39:34 +1000 | [diff] [blame] | 37 | #if defined(DEBUG) || defined(XFS_WARN) |
Lachlan McIlroy | f7c66ce | 2007-05-08 13:50:19 +1000 | [diff] [blame] | 38 | mrp->mr_writer = 1; |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 39 | #endif |
Lachlan McIlroy | f7c66ce | 2007-05-08 13:50:19 +1000 | [diff] [blame] | 40 | } |
| 41 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | static inline int mrtryaccess(mrlock_t *mrp) |
| 43 | { |
| 44 | return down_read_trylock(&mrp->mr_lock); |
| 45 | } |
| 46 | |
| 47 | static inline int mrtryupdate(mrlock_t *mrp) |
| 48 | { |
| 49 | if (!down_write_trylock(&mrp->mr_lock)) |
| 50 | return 0; |
Dave Chinner | 742ae1e | 2013-04-30 21:39:34 +1000 | [diff] [blame] | 51 | #if defined(DEBUG) || defined(XFS_WARN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | mrp->mr_writer = 1; |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 53 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | return 1; |
| 55 | } |
| 56 | |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 57 | static inline void mrunlock_excl(mrlock_t *mrp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | { |
Dave Chinner | 742ae1e | 2013-04-30 21:39:34 +1000 | [diff] [blame] | 59 | #if defined(DEBUG) || defined(XFS_WARN) |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 60 | mrp->mr_writer = 0; |
| 61 | #endif |
| 62 | up_write(&mrp->mr_lock); |
| 63 | } |
| 64 | |
| 65 | static inline void mrunlock_shared(mrlock_t *mrp) |
| 66 | { |
| 67 | up_read(&mrp->mr_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | static inline void mrdemote(mrlock_t *mrp) |
| 71 | { |
Dave Chinner | 742ae1e | 2013-04-30 21:39:34 +1000 | [diff] [blame] | 72 | #if defined(DEBUG) || defined(XFS_WARN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | mrp->mr_writer = 0; |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 74 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | downgrade_write(&mrp->mr_lock); |
| 76 | } |
| 77 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | #endif /* __XFS_SUPPORT_MRLOCK_H__ */ |