David Sterba | 9888c34 | 2018-04-03 19:16:55 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2008 Oracle. All rights reserved. |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4 | */ |
| 5 | |
David Sterba | 9888c34 | 2018-04-03 19:16:55 +0200 | [diff] [blame] | 6 | #ifndef BTRFS_LOCKING_H |
| 7 | #define BTRFS_LOCKING_H |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 8 | |
Nikolay Borisov | 2992df7 | 2020-01-30 14:59:44 +0200 | [diff] [blame] | 9 | #include <linux/atomic.h> |
| 10 | #include <linux/wait.h> |
| 11 | #include <linux/percpu_counter.h> |
David Sterba | 31f6e76 | 2019-09-24 18:44:24 +0200 | [diff] [blame] | 12 | #include "extent_io.h" |
| 13 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 14 | #define BTRFS_WRITE_LOCK 1 |
| 15 | #define BTRFS_READ_LOCK 2 |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 16 | |
Josef Bacik | fd7ba1c | 2020-08-20 11:46:02 -0400 | [diff] [blame] | 17 | /* |
| 18 | * We are limited in number of subclasses by MAX_LOCKDEP_SUBCLASSES, which at |
| 19 | * the time of this patch is 8, which is how many we use. Keep this in mind if |
| 20 | * you decide you want to add another subclass. |
| 21 | */ |
| 22 | enum btrfs_lock_nesting { |
| 23 | BTRFS_NESTING_NORMAL, |
| 24 | |
| 25 | /* |
Josef Bacik | 9631e4c | 2020-08-20 11:46:03 -0400 | [diff] [blame] | 26 | * When we COW a block we are holding the lock on the original block, |
| 27 | * and since our lockdep maps are rootid+level, this confuses lockdep |
| 28 | * when we lock the newly allocated COW'd block. Handle this by having |
| 29 | * a subclass for COW'ed blocks so that lockdep doesn't complain. |
| 30 | */ |
| 31 | BTRFS_NESTING_COW, |
| 32 | |
| 33 | /* |
Josef Bacik | bf77467 | 2020-08-20 11:46:04 -0400 | [diff] [blame] | 34 | * Oftentimes we need to lock adjacent nodes on the same level while |
| 35 | * still holding the lock on the original node we searched to, such as |
| 36 | * for searching forward or for split/balance. |
| 37 | * |
| 38 | * Because of this we need to indicate to lockdep that this is |
| 39 | * acceptable by having a different subclass for each of these |
| 40 | * operations. |
| 41 | */ |
| 42 | BTRFS_NESTING_LEFT, |
| 43 | BTRFS_NESTING_RIGHT, |
| 44 | |
| 45 | /* |
Josef Bacik | bf59a5a | 2020-08-20 11:46:05 -0400 | [diff] [blame] | 46 | * When splitting we will be holding a lock on the left/right node when |
| 47 | * we need to cow that node, thus we need a new set of subclasses for |
| 48 | * these two operations. |
| 49 | */ |
| 50 | BTRFS_NESTING_LEFT_COW, |
| 51 | BTRFS_NESTING_RIGHT_COW, |
| 52 | |
| 53 | /* |
Josef Bacik | 4dff97e | 2020-08-20 11:46:06 -0400 | [diff] [blame] | 54 | * When splitting we may push nodes to the left or right, but still use |
| 55 | * the subsequent nodes in our path, keeping our locks on those adjacent |
| 56 | * blocks. Thus when we go to allocate a new split block we've already |
| 57 | * used up all of our available subclasses, so this subclass exists to |
| 58 | * handle this case where we need to allocate a new split block. |
| 59 | */ |
| 60 | BTRFS_NESTING_SPLIT, |
| 61 | |
| 62 | /* |
Josef Bacik | cf6f34a | 2020-08-20 11:46:07 -0400 | [diff] [blame] | 63 | * When promoting a new block to a root we need to have a special |
| 64 | * subclass so we don't confuse lockdep, as it will appear that we are |
| 65 | * locking a higher level node before a lower level one. Copying also |
| 66 | * has this problem as it appears we're locking the same block again |
| 67 | * when we make a snapshot of an existing root. |
| 68 | */ |
| 69 | BTRFS_NESTING_NEW_ROOT, |
| 70 | |
| 71 | /* |
Josef Bacik | fd7ba1c | 2020-08-20 11:46:02 -0400 | [diff] [blame] | 72 | * We are limited to MAX_LOCKDEP_SUBLCLASSES number of subclasses, so |
| 73 | * add this in here and add a static_assert to keep us from going over |
| 74 | * the limit. As of this writing we're limited to 8, and we're |
| 75 | * definitely using 8, hence this check to keep us from messing up in |
| 76 | * the future. |
| 77 | */ |
| 78 | BTRFS_NESTING_MAX, |
| 79 | }; |
| 80 | |
| 81 | static_assert(BTRFS_NESTING_MAX <= MAX_LOCKDEP_SUBCLASSES, |
| 82 | "too many lock subclasses defined"); |
| 83 | |
Nikolay Borisov | 2992df7 | 2020-01-30 14:59:44 +0200 | [diff] [blame] | 84 | struct btrfs_path; |
| 85 | |
Josef Bacik | fd7ba1c | 2020-08-20 11:46:02 -0400 | [diff] [blame] | 86 | void __btrfs_tree_lock(struct extent_buffer *eb, enum btrfs_lock_nesting nest); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 87 | void btrfs_tree_lock(struct extent_buffer *eb); |
| 88 | void btrfs_tree_unlock(struct extent_buffer *eb); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 89 | |
Josef Bacik | 0ecae6f | 2020-11-06 16:27:35 -0500 | [diff] [blame] | 90 | void __btrfs_tree_read_lock(struct extent_buffer *eb, enum btrfs_lock_nesting nest); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 91 | void btrfs_tree_read_lock(struct extent_buffer *eb); |
| 92 | void btrfs_tree_read_unlock(struct extent_buffer *eb); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 93 | int btrfs_try_tree_read_lock(struct extent_buffer *eb); |
| 94 | int btrfs_try_tree_write_lock(struct extent_buffer *eb); |
Josef Bacik | 5189941 | 2020-08-20 11:46:01 -0400 | [diff] [blame] | 95 | struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root); |
Josef Bacik | 1bb9659 | 2020-11-06 16:27:33 -0500 | [diff] [blame] | 96 | struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root); |
Chris Mason | f82c458 | 2014-11-19 10:25:09 -0800 | [diff] [blame] | 97 | |
David Sterba | 31f6e76 | 2019-09-24 18:44:24 +0200 | [diff] [blame] | 98 | #ifdef CONFIG_BTRFS_DEBUG |
Filipe Manana | 49d0c64 | 2021-09-22 10:36:45 +0100 | [diff] [blame] | 99 | static inline void btrfs_assert_tree_write_locked(struct extent_buffer *eb) |
| 100 | { |
| 101 | lockdep_assert_held_write(&eb->lock); |
David Sterba | 31f6e76 | 2019-09-24 18:44:24 +0200 | [diff] [blame] | 102 | } |
| 103 | #else |
Filipe Manana | 49d0c64 | 2021-09-22 10:36:45 +0100 | [diff] [blame] | 104 | static inline void btrfs_assert_tree_write_locked(struct extent_buffer *eb) { } |
David Sterba | 31f6e76 | 2019-09-24 18:44:24 +0200 | [diff] [blame] | 105 | #endif |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 106 | |
David Sterba | 1f95ec0 | 2019-09-24 19:17:17 +0200 | [diff] [blame] | 107 | void btrfs_unlock_up_safe(struct btrfs_path *path, int level); |
David Sterba | ed2b1d3 | 2019-09-24 19:17:17 +0200 | [diff] [blame] | 108 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 109 | static inline void btrfs_tree_unlock_rw(struct extent_buffer *eb, int rw) |
| 110 | { |
Josef Bacik | ac5887c | 2020-08-20 11:46:10 -0400 | [diff] [blame] | 111 | if (rw == BTRFS_WRITE_LOCK) |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 112 | btrfs_tree_unlock(eb); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 113 | else if (rw == BTRFS_READ_LOCK) |
| 114 | btrfs_tree_read_unlock(eb); |
| 115 | else |
| 116 | BUG(); |
| 117 | } |
| 118 | |
Nikolay Borisov | 2992df7 | 2020-01-30 14:59:44 +0200 | [diff] [blame] | 119 | struct btrfs_drew_lock { |
| 120 | atomic_t readers; |
| 121 | struct percpu_counter writers; |
| 122 | wait_queue_head_t pending_writers; |
| 123 | wait_queue_head_t pending_readers; |
| 124 | }; |
| 125 | |
| 126 | int btrfs_drew_lock_init(struct btrfs_drew_lock *lock); |
| 127 | void btrfs_drew_lock_destroy(struct btrfs_drew_lock *lock); |
| 128 | void btrfs_drew_write_lock(struct btrfs_drew_lock *lock); |
| 129 | bool btrfs_drew_try_write_lock(struct btrfs_drew_lock *lock); |
| 130 | void btrfs_drew_write_unlock(struct btrfs_drew_lock *lock); |
| 131 | void btrfs_drew_read_lock(struct btrfs_drew_lock *lock); |
| 132 | void btrfs_drew_read_unlock(struct btrfs_drew_lock *lock); |
| 133 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 134 | #endif |