^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: GPL-2.0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2008 Oracle. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #ifndef BTRFS_LOCKING_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #define BTRFS_LOCKING_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/atomic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/wait.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/percpu_counter.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include "extent_io.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #define BTRFS_WRITE_LOCK 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #define BTRFS_READ_LOCK 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #define BTRFS_WRITE_LOCK_BLOCKING 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #define BTRFS_READ_LOCK_BLOCKING 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * We are limited in number of subclasses by MAX_LOCKDEP_SUBCLASSES, which at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * the time of this patch is 8, which is how many we use. Keep this in mind if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * you decide you want to add another subclass.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) enum btrfs_lock_nesting {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) BTRFS_NESTING_NORMAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * When we COW a block we are holding the lock on the original block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * and since our lockdep maps are rootid+level, this confuses lockdep
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * when we lock the newly allocated COW'd block. Handle this by having
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * a subclass for COW'ed blocks so that lockdep doesn't complain.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) BTRFS_NESTING_COW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * Oftentimes we need to lock adjacent nodes on the same level while
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * still holding the lock on the original node we searched to, such as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * for searching forward or for split/balance.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * Because of this we need to indicate to lockdep that this is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * acceptable by having a different subclass for each of these
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * operations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) BTRFS_NESTING_LEFT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) BTRFS_NESTING_RIGHT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * When splitting we will be holding a lock on the left/right node when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * we need to cow that node, thus we need a new set of subclasses for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * these two operations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) BTRFS_NESTING_LEFT_COW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) BTRFS_NESTING_RIGHT_COW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) * When splitting we may push nodes to the left or right, but still use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) * the subsequent nodes in our path, keeping our locks on those adjacent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) * blocks. Thus when we go to allocate a new split block we've already
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) * used up all of our available subclasses, so this subclass exists to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) * handle this case where we need to allocate a new split block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) BTRFS_NESTING_SPLIT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * When promoting a new block to a root we need to have a special
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * subclass so we don't confuse lockdep, as it will appear that we are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) * locking a higher level node before a lower level one. Copying also
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) * has this problem as it appears we're locking the same block again
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * when we make a snapshot of an existing root.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) BTRFS_NESTING_NEW_ROOT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) * We are limited to MAX_LOCKDEP_SUBLCLASSES number of subclasses, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) * add this in here and add a static_assert to keep us from going over
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) * the limit. As of this writing we're limited to 8, and we're
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) * definitely using 8, hence this check to keep us from messing up in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) * the future.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) BTRFS_NESTING_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) static_assert(BTRFS_NESTING_MAX <= MAX_LOCKDEP_SUBCLASSES,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) "too many lock subclasses defined");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) struct btrfs_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) void __btrfs_tree_lock(struct extent_buffer *eb, enum btrfs_lock_nesting nest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) void btrfs_tree_lock(struct extent_buffer *eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) void btrfs_tree_unlock(struct extent_buffer *eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) void __btrfs_tree_read_lock(struct extent_buffer *eb, enum btrfs_lock_nesting nest,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) bool recurse);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) void btrfs_tree_read_lock(struct extent_buffer *eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) void btrfs_tree_read_unlock(struct extent_buffer *eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) void btrfs_tree_read_unlock_blocking(struct extent_buffer *eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) void btrfs_set_lock_blocking_read(struct extent_buffer *eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) void btrfs_set_lock_blocking_write(struct extent_buffer *eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) int btrfs_try_tree_read_lock(struct extent_buffer *eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) int btrfs_try_tree_write_lock(struct extent_buffer *eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) int btrfs_tree_read_lock_atomic(struct extent_buffer *eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) struct extent_buffer *__btrfs_read_lock_root_node(struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) bool recurse);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) static inline struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) return __btrfs_read_lock_root_node(root, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) #ifdef CONFIG_BTRFS_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) static inline void btrfs_assert_tree_locked(struct extent_buffer *eb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) BUG_ON(!eb->write_locks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) static inline void btrfs_assert_tree_locked(struct extent_buffer *eb) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) void btrfs_set_path_blocking(struct btrfs_path *p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) void btrfs_unlock_up_safe(struct btrfs_path *path, int level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) static inline void btrfs_tree_unlock_rw(struct extent_buffer *eb, int rw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) if (rw == BTRFS_WRITE_LOCK || rw == BTRFS_WRITE_LOCK_BLOCKING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) btrfs_tree_unlock(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) else if (rw == BTRFS_READ_LOCK_BLOCKING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) btrfs_tree_read_unlock_blocking(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) else if (rw == BTRFS_READ_LOCK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) btrfs_tree_read_unlock(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) struct btrfs_drew_lock {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) atomic_t readers;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) struct percpu_counter writers;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) wait_queue_head_t pending_writers;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) wait_queue_head_t pending_readers;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) int btrfs_drew_lock_init(struct btrfs_drew_lock *lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) void btrfs_drew_lock_destroy(struct btrfs_drew_lock *lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) void btrfs_drew_write_lock(struct btrfs_drew_lock *lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) bool btrfs_drew_try_write_lock(struct btrfs_drew_lock *lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) void btrfs_drew_write_unlock(struct btrfs_drew_lock *lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) void btrfs_drew_read_lock(struct btrfs_drew_lock *lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) void btrfs_drew_read_unlock(struct btrfs_drew_lock *lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) #endif