]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/btree_io.h
Update bcachefs sources to f026e4e024
[bcachefs-tools-debian] / libbcachefs / btree_io.h
1 #ifndef _BCACHE_BTREE_IO_H
2 #define _BCACHE_BTREE_IO_H
3
4 struct bch_fs;
5 struct btree_write;
6 struct btree;
7 struct btree_iter;
8
9 static inline void btree_node_io_unlock(struct btree *b)
10 {
11         EBUG_ON(!btree_node_write_in_flight(b));
12         clear_btree_node_write_in_flight(b);
13         wake_up_bit(&b->flags, BTREE_NODE_write_in_flight);
14 }
15
16 static inline void btree_node_io_lock(struct btree *b)
17 {
18         wait_on_bit_lock_io(&b->flags, BTREE_NODE_write_in_flight,
19                             TASK_UNINTERRUPTIBLE);
20 }
21
22 static inline void btree_node_wait_on_io(struct btree *b)
23 {
24         wait_on_bit_io(&b->flags, BTREE_NODE_write_in_flight,
25                        TASK_UNINTERRUPTIBLE);
26 }
27
28 static inline bool btree_node_may_write(struct btree *b)
29 {
30         return list_empty_careful(&b->write_blocked) &&
31                 list_empty_careful(&b->reachable);
32 }
33
34 enum compact_mode {
35         COMPACT_LAZY,
36         COMPACT_WRITTEN,
37         COMPACT_WRITTEN_NO_WRITE_LOCK,
38 };
39
40 bool __bch2_compact_whiteouts(struct bch_fs *, struct btree *, enum compact_mode);
41
42 static inline bool bch2_maybe_compact_whiteouts(struct bch_fs *c, struct btree *b)
43 {
44         struct bset_tree *t;
45
46         for_each_bset(b, t) {
47                 unsigned live_u64s = b->nr.bset_u64s[t - b->set];
48                 unsigned bset_u64s = le16_to_cpu(bset(b, t)->u64s);
49
50                 if (live_u64s * 4 < bset_u64s * 3)
51                         goto compact;
52         }
53
54         return false;
55 compact:
56         return __bch2_compact_whiteouts(c, b, COMPACT_LAZY);
57 }
58
59 void bch2_btree_sort_into(struct bch_fs *, struct btree *, struct btree *);
60
61 void bch2_btree_build_aux_trees(struct btree *);
62 void bch2_btree_init_next(struct bch_fs *, struct btree *,
63                          struct btree_iter *);
64
65 void bch2_btree_node_read_done(struct bch_fs *, struct btree *,
66                               struct bch_dev *, const struct bch_extent_ptr *);
67 void bch2_btree_node_read(struct bch_fs *, struct btree *);
68 int bch2_btree_root_read(struct bch_fs *, enum btree_id,
69                         const struct bkey_i *, unsigned);
70
71 void bch2_btree_complete_write(struct bch_fs *, struct btree *,
72                               struct btree_write *);
73
74 void __bch2_btree_node_write(struct bch_fs *, struct btree *,
75                             struct closure *, enum six_lock_type);
76 bool bch2_btree_post_write_cleanup(struct bch_fs *, struct btree *);
77
78 void bch2_btree_node_write(struct bch_fs *, struct btree *,
79                           struct closure *, enum six_lock_type);
80
81 #define bch2_btree_node_write_dirty(_c, _b, _cl, cond)                  \
82 do {                                                                    \
83         while ((_b)->written && btree_node_dirty(_b) && (cond)) {       \
84                 set_btree_node_need_write(_b);                          \
85                                                                         \
86                 if (!btree_node_may_write(_b))                          \
87                         break;                                          \
88                                                                         \
89                 if (!btree_node_write_in_flight(_b)) {                  \
90                         bch2_btree_node_write(_c, _b, _cl, SIX_LOCK_read);\
91                         break;                                          \
92                 }                                                       \
93                                                                         \
94                 six_unlock_read(&(_b)->lock);                           \
95                 btree_node_wait_on_io(_b);                              \
96                 six_lock_read(&(_b)->lock);                             \
97         }                                                               \
98 } while (0)
99
100 void bch2_btree_verify_flushed(struct bch_fs *);
101
102 #endif /* _BCACHE_BTREE_IO_H */