1
2
3
4
5
6#ifndef BTRFS_LOCKING_H
7#define BTRFS_LOCKING_H
8
9#include <linux/atomic.h>
10#include <linux/wait.h>
11#include <linux/percpu_counter.h>
12#include "extent_io.h"
13
14#define BTRFS_WRITE_LOCK 1
15#define BTRFS_READ_LOCK 2
16
17
18
19
20
21
22enum btrfs_lock_nesting {
23 BTRFS_NESTING_NORMAL,
24
25
26
27
28
29
30
31 BTRFS_NESTING_COW,
32
33
34
35
36
37
38
39
40
41
42 BTRFS_NESTING_LEFT,
43 BTRFS_NESTING_RIGHT,
44
45
46
47
48
49
50 BTRFS_NESTING_LEFT_COW,
51 BTRFS_NESTING_RIGHT_COW,
52
53
54
55
56
57
58
59
60 BTRFS_NESTING_SPLIT,
61
62
63
64
65
66
67
68
69 BTRFS_NESTING_NEW_ROOT,
70
71
72
73
74
75
76
77
78 BTRFS_NESTING_MAX,
79};
80
81static_assert(BTRFS_NESTING_MAX <= MAX_LOCKDEP_SUBCLASSES,
82 "too many lock subclasses defined");
83
84struct btrfs_path;
85
86void __btrfs_tree_lock(struct extent_buffer *eb, enum btrfs_lock_nesting nest);
87void btrfs_tree_lock(struct extent_buffer *eb);
88void btrfs_tree_unlock(struct extent_buffer *eb);
89
90void __btrfs_tree_read_lock(struct extent_buffer *eb, enum btrfs_lock_nesting nest);
91void btrfs_tree_read_lock(struct extent_buffer *eb);
92void btrfs_tree_read_unlock(struct extent_buffer *eb);
93int btrfs_try_tree_read_lock(struct extent_buffer *eb);
94int btrfs_try_tree_write_lock(struct extent_buffer *eb);
95struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root);
96struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root);
97
98#ifdef CONFIG_BTRFS_DEBUG
99static inline void btrfs_assert_tree_locked(struct extent_buffer *eb) {
100 lockdep_assert_held(&eb->lock);
101}
102#else
103static inline void btrfs_assert_tree_locked(struct extent_buffer *eb) { }
104#endif
105
106void btrfs_unlock_up_safe(struct btrfs_path *path, int level);
107
108static inline void btrfs_tree_unlock_rw(struct extent_buffer *eb, int rw)
109{
110 if (rw == BTRFS_WRITE_LOCK)
111 btrfs_tree_unlock(eb);
112 else if (rw == BTRFS_READ_LOCK)
113 btrfs_tree_read_unlock(eb);
114 else
115 BUG();
116}
117
118struct btrfs_drew_lock {
119 atomic_t readers;
120 struct percpu_counter writers;
121 wait_queue_head_t pending_writers;
122 wait_queue_head_t pending_readers;
123};
124
125int btrfs_drew_lock_init(struct btrfs_drew_lock *lock);
126void btrfs_drew_lock_destroy(struct btrfs_drew_lock *lock);
127void btrfs_drew_write_lock(struct btrfs_drew_lock *lock);
128bool btrfs_drew_try_write_lock(struct btrfs_drew_lock *lock);
129void btrfs_drew_write_unlock(struct btrfs_drew_lock *lock);
130void btrfs_drew_read_lock(struct btrfs_drew_lock *lock);
131void btrfs_drew_read_unlock(struct btrfs_drew_lock *lock);
132
133#endif
134