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