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