locking.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (C) 2008 Oracle. All rights reserved.
  4. */
  5. #ifndef BTRFS_LOCKING_H
  6. #define BTRFS_LOCKING_H
  7. #include <linux/atomic.h>
  8. #include <linux/wait.h>
  9. #include <linux/lockdep.h>
  10. #include <linux/percpu_counter.h>
  11. #include "extent_io.h"
  12. struct extent_buffer;
  13. struct btrfs_path;
  14. struct btrfs_root;
  15. #define BTRFS_WRITE_LOCK 1
  16. #define BTRFS_READ_LOCK 2
  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. * When we COW a block we are holding the lock on the original block,
  26. * and since our lockdep maps are rootid+level, this confuses lockdep
  27. * when we lock the newly allocated COW'd block. Handle this by having
  28. * a subclass for COW'ed blocks so that lockdep doesn't complain.
  29. */
  30. BTRFS_NESTING_COW,
  31. /*
  32. * Oftentimes we need to lock adjacent nodes on the same level while
  33. * still holding the lock on the original node we searched to, such as
  34. * for searching forward or for split/balance.
  35. *
  36. * Because of this we need to indicate to lockdep that this is
  37. * acceptable by having a different subclass for each of these
  38. * operations.
  39. */
  40. BTRFS_NESTING_LEFT,
  41. BTRFS_NESTING_RIGHT,
  42. /*
  43. * When splitting we will be holding a lock on the left/right node when
  44. * we need to cow that node, thus we need a new set of subclasses for
  45. * these two operations.
  46. */
  47. BTRFS_NESTING_LEFT_COW,
  48. BTRFS_NESTING_RIGHT_COW,
  49. /*
  50. * When splitting we may push nodes to the left or right, but still use
  51. * the subsequent nodes in our path, keeping our locks on those adjacent
  52. * blocks. Thus when we go to allocate a new split block we've already
  53. * used up all of our available subclasses, so this subclass exists to
  54. * handle this case where we need to allocate a new split block.
  55. */
  56. BTRFS_NESTING_SPLIT,
  57. /*
  58. * When promoting a new block to a root we need to have a special
  59. * subclass so we don't confuse lockdep, as it will appear that we are
  60. * locking a higher level node before a lower level one. Copying also
  61. * has this problem as it appears we're locking the same block again
  62. * when we make a snapshot of an existing root.
  63. */
  64. BTRFS_NESTING_NEW_ROOT,
  65. /*
  66. * We are limited to MAX_LOCKDEP_SUBLCLASSES number of subclasses, so
  67. * add this in here and add a static_assert to keep us from going over
  68. * the limit. As of this writing we're limited to 8, and we're
  69. * definitely using 8, hence this check to keep us from messing up in
  70. * the future.
  71. */
  72. BTRFS_NESTING_MAX,
  73. };
  74. enum btrfs_lockdep_trans_states {
  75. BTRFS_LOCKDEP_TRANS_COMMIT_PREP,
  76. BTRFS_LOCKDEP_TRANS_UNBLOCKED,
  77. BTRFS_LOCKDEP_TRANS_SUPER_COMMITTED,
  78. BTRFS_LOCKDEP_TRANS_COMPLETED,
  79. };
  80. /*
  81. * Lockdep annotation for wait events.
  82. *
  83. * @owner: The struct where the lockdep map is defined
  84. * @lock: The lockdep map corresponding to a wait event
  85. *
  86. * This macro is used to annotate a wait event. In this case a thread acquires
  87. * the lockdep map as writer (exclusive lock) because it has to block until all
  88. * the threads that hold the lock as readers signal the condition for the wait
  89. * event and release their locks.
  90. */
  91. #define btrfs_might_wait_for_event(owner, lock) \
  92. do { \
  93. rwsem_acquire(&owner->lock##_map, 0, 0, _THIS_IP_); \
  94. rwsem_release(&owner->lock##_map, _THIS_IP_); \
  95. } while (0)
  96. /*
  97. * Protection for the resource/condition of a wait event.
  98. *
  99. * @owner: The struct where the lockdep map is defined
  100. * @lock: The lockdep map corresponding to a wait event
  101. *
  102. * Many threads can modify the condition for the wait event at the same time
  103. * and signal the threads that block on the wait event. The threads that modify
  104. * the condition and do the signaling acquire the lock as readers (shared
  105. * lock).
  106. */
  107. #define btrfs_lockdep_acquire(owner, lock) \
  108. rwsem_acquire_read(&owner->lock##_map, 0, 0, _THIS_IP_)
  109. /*
  110. * Used after signaling the condition for a wait event to release the lockdep
  111. * map held by a reader thread.
  112. */
  113. #define btrfs_lockdep_release(owner, lock) \
  114. rwsem_release(&owner->lock##_map, _THIS_IP_)
  115. /*
  116. * Macros for the transaction states wait events, similar to the generic wait
  117. * event macros.
  118. */
  119. #define btrfs_might_wait_for_state(owner, i) \
  120. do { \
  121. rwsem_acquire(&owner->btrfs_state_change_map[i], 0, 0, _THIS_IP_); \
  122. rwsem_release(&owner->btrfs_state_change_map[i], _THIS_IP_); \
  123. } while (0)
  124. #define btrfs_trans_state_lockdep_acquire(owner, i) \
  125. rwsem_acquire_read(&owner->btrfs_state_change_map[i], 0, 0, _THIS_IP_)
  126. #define btrfs_trans_state_lockdep_release(owner, i) \
  127. rwsem_release(&owner->btrfs_state_change_map[i], _THIS_IP_)
  128. /* Initialization of the lockdep map */
  129. #define btrfs_lockdep_init_map(owner, lock) \
  130. do { \
  131. static struct lock_class_key lock##_key; \
  132. lockdep_init_map(&owner->lock##_map, #lock, &lock##_key, 0); \
  133. } while (0)
  134. /* Initialization of the transaction states lockdep maps. */
  135. #define btrfs_state_lockdep_init_map(owner, lock, state) \
  136. do { \
  137. static struct lock_class_key lock##_key; \
  138. lockdep_init_map(&owner->btrfs_state_change_map[state], #lock, \
  139. &lock##_key, 0); \
  140. } while (0)
  141. static_assert(BTRFS_NESTING_MAX <= MAX_LOCKDEP_SUBCLASSES,
  142. "too many lock subclasses defined");
  143. void btrfs_tree_lock_nested(struct extent_buffer *eb, enum btrfs_lock_nesting nest);
  144. static inline void btrfs_tree_lock(struct extent_buffer *eb)
  145. {
  146. btrfs_tree_lock_nested(eb, BTRFS_NESTING_NORMAL);
  147. }
  148. void btrfs_tree_unlock(struct extent_buffer *eb);
  149. void btrfs_tree_read_lock_nested(struct extent_buffer *eb, enum btrfs_lock_nesting nest);
  150. static inline void btrfs_tree_read_lock(struct extent_buffer *eb)
  151. {
  152. btrfs_tree_read_lock_nested(eb, BTRFS_NESTING_NORMAL);
  153. }
  154. void btrfs_tree_read_unlock(struct extent_buffer *eb);
  155. int btrfs_try_tree_read_lock(struct extent_buffer *eb);
  156. int btrfs_try_tree_write_lock(struct extent_buffer *eb);
  157. struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root);
  158. struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root);
  159. struct extent_buffer *btrfs_try_read_lock_root_node(struct btrfs_root *root);
  160. #ifdef CONFIG_BTRFS_DEBUG
  161. static inline void btrfs_assert_tree_write_locked(struct extent_buffer *eb)
  162. {
  163. lockdep_assert_held_write(&eb->lock);
  164. }
  165. #else
  166. static inline void btrfs_assert_tree_write_locked(struct extent_buffer *eb) { }
  167. #endif
  168. void btrfs_unlock_up_safe(struct btrfs_path *path, int level);
  169. static inline void btrfs_tree_unlock_rw(struct extent_buffer *eb, int rw)
  170. {
  171. if (rw == BTRFS_WRITE_LOCK)
  172. btrfs_tree_unlock(eb);
  173. else if (rw == BTRFS_READ_LOCK)
  174. btrfs_tree_read_unlock(eb);
  175. else
  176. BUG();
  177. }
  178. struct btrfs_drew_lock {
  179. atomic_t readers;
  180. atomic_t writers;
  181. wait_queue_head_t pending_writers;
  182. wait_queue_head_t pending_readers;
  183. };
  184. void btrfs_drew_lock_init(struct btrfs_drew_lock *lock);
  185. void btrfs_drew_write_lock(struct btrfs_drew_lock *lock);
  186. bool btrfs_drew_try_write_lock(struct btrfs_drew_lock *lock);
  187. void btrfs_drew_write_unlock(struct btrfs_drew_lock *lock);
  188. void btrfs_drew_read_lock(struct btrfs_drew_lock *lock);
  189. void btrfs_drew_read_unlock(struct btrfs_drew_lock *lock);
  190. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  191. void btrfs_set_buffer_lockdep_class(u64 objectid, struct extent_buffer *eb, int level);
  192. void btrfs_maybe_reset_lockdep_class(struct btrfs_root *root, struct extent_buffer *eb);
  193. #else
  194. static inline void btrfs_set_buffer_lockdep_class(u64 objectid,
  195. struct extent_buffer *eb, int level)
  196. {
  197. }
  198. static inline void btrfs_maybe_reset_lockdep_class(struct btrfs_root *root,
  199. struct extent_buffer *eb)
  200. {
  201. }
  202. #endif
  203. #endif