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