]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/btree_io.h
Update bcachefs sources to 4837f82ee1 bcachefs: Use cached iterators for alloc btree
[bcachefs-tools-debian] / libbcachefs / btree_io.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_BTREE_IO_H
3 #define _BCACHEFS_BTREE_IO_H
4
5 #include "bkey_methods.h"
6 #include "bset.h"
7 #include "btree_locking.h"
8 #include "extents.h"
9 #include "io_types.h"
10
11 struct bch_fs;
12 struct btree_write;
13 struct btree;
14 struct btree_iter;
15
16 struct btree_read_bio {
17         struct bch_fs           *c;
18         u64                     start_time;
19         unsigned                have_ioref:1;
20         struct extent_ptr_decoded       pick;
21         struct work_struct      work;
22         struct bio              bio;
23 };
24
25 struct btree_write_bio {
26         void                    *data;
27         struct work_struct      work;
28         struct bch_write_bio    wbio;
29 };
30
31 static inline void btree_node_io_unlock(struct btree *b)
32 {
33         EBUG_ON(!btree_node_write_in_flight(b));
34         clear_btree_node_write_in_flight(b);
35         wake_up_bit(&b->flags, BTREE_NODE_write_in_flight);
36 }
37
38 static inline void btree_node_io_lock(struct btree *b)
39 {
40         wait_on_bit_lock_io(&b->flags, BTREE_NODE_write_in_flight,
41                             TASK_UNINTERRUPTIBLE);
42 }
43
44 static inline void btree_node_wait_on_io(struct btree *b)
45 {
46         wait_on_bit_io(&b->flags, BTREE_NODE_write_in_flight,
47                        TASK_UNINTERRUPTIBLE);
48 }
49
50 static inline bool btree_node_may_write(struct btree *b)
51 {
52         return list_empty_careful(&b->write_blocked) &&
53                 (!b->written || !b->will_make_reachable);
54 }
55
56 enum compact_mode {
57         COMPACT_LAZY,
58         COMPACT_ALL,
59 };
60
61 bool bch2_compact_whiteouts(struct bch_fs *, struct btree *,
62                             enum compact_mode);
63
64 static inline bool should_compact_bset_lazy(struct btree *b,
65                                             struct bset_tree *t)
66 {
67         unsigned total_u64s = bset_u64s(t);
68         unsigned dead_u64s = bset_dead_u64s(b, t);
69
70         return dead_u64s > 64 && dead_u64s * 3 > total_u64s;
71 }
72
73 static inline bool bch2_maybe_compact_whiteouts(struct bch_fs *c, struct btree *b)
74 {
75         struct bset_tree *t;
76
77         for_each_bset(b, t)
78                 if (should_compact_bset_lazy(b, t))
79                         return bch2_compact_whiteouts(c, b, COMPACT_LAZY);
80
81         return false;
82 }
83
84 void bch2_btree_sort_into(struct bch_fs *, struct btree *, struct btree *);
85
86 void bch2_btree_build_aux_trees(struct btree *);
87 void bch2_btree_init_next(struct bch_fs *, struct btree *,
88                          struct btree_iter *);
89
90 int bch2_btree_node_read_done(struct bch_fs *, struct btree *, bool);
91 void bch2_btree_node_read(struct bch_fs *, struct btree *, bool);
92 int bch2_btree_root_read(struct bch_fs *, enum btree_id,
93                          const struct bkey_i *, unsigned);
94
95 void bch2_btree_complete_write(struct bch_fs *, struct btree *,
96                               struct btree_write *);
97 void bch2_btree_write_error_work(struct work_struct *);
98
99 void __bch2_btree_node_write(struct bch_fs *, struct btree *,
100                             enum six_lock_type);
101 bool bch2_btree_post_write_cleanup(struct bch_fs *, struct btree *);
102
103 void bch2_btree_node_write(struct bch_fs *, struct btree *,
104                           enum six_lock_type);
105
106 static inline void btree_node_write_if_need(struct bch_fs *c, struct btree *b,
107                                             enum six_lock_type lock_held)
108 {
109         while (b->written &&
110                btree_node_need_write(b) &&
111                btree_node_may_write(b)) {
112                 if (!btree_node_write_in_flight(b)) {
113                         bch2_btree_node_write(c, b, lock_held);
114                         break;
115                 }
116
117                 six_unlock_type(&b->c.lock, lock_held);
118                 btree_node_wait_on_io(b);
119                 btree_node_lock_type(c, b, lock_held);
120         }
121 }
122
123 #define bch2_btree_node_write_cond(_c, _b, cond)                        \
124 do {                                                                    \
125         unsigned long old, new, v = READ_ONCE((_b)->flags);             \
126                                                                         \
127         do {                                                            \
128                 old = new = v;                                          \
129                                                                         \
130                 if (!(old & (1 << BTREE_NODE_dirty)) || !(cond))        \
131                         break;                                          \
132                                                                         \
133                 new |= (1 << BTREE_NODE_need_write);                    \
134         } while ((v = cmpxchg(&(_b)->flags, old, new)) != old);         \
135                                                                         \
136         btree_node_write_if_need(_c, _b, SIX_LOCK_read);                \
137 } while (0)
138
139 void bch2_btree_flush_all_reads(struct bch_fs *);
140 void bch2_btree_flush_all_writes(struct bch_fs *);
141 void bch2_btree_verify_flushed(struct bch_fs *);
142 ssize_t bch2_dirty_btree_nodes_print(struct bch_fs *, char *);
143
144 static inline void compat_bformat(unsigned level, enum btree_id btree_id,
145                                  unsigned version, unsigned big_endian,
146                                  int write, struct bkey_format *f)
147 {
148         if (version < bcachefs_metadata_version_inode_btree_change &&
149             btree_id == BTREE_ID_INODES) {
150                 swap(f->bits_per_field[BKEY_FIELD_INODE],
151                      f->bits_per_field[BKEY_FIELD_OFFSET]);
152                 swap(f->field_offset[BKEY_FIELD_INODE],
153                      f->field_offset[BKEY_FIELD_OFFSET]);
154         }
155 }
156
157 static inline void compat_bpos(unsigned level, enum btree_id btree_id,
158                                unsigned version, unsigned big_endian,
159                                int write, struct bpos *p)
160 {
161         if (big_endian != CPU_BIG_ENDIAN)
162                 bch2_bpos_swab(p);
163
164         if (version < bcachefs_metadata_version_inode_btree_change &&
165             btree_id == BTREE_ID_INODES)
166                 swap(p->inode, p->offset);
167 }
168
169 static inline void compat_btree_node(unsigned level, enum btree_id btree_id,
170                                      unsigned version, unsigned big_endian,
171                                      int write,
172                                      struct btree_node *bn)
173 {
174         if (version < bcachefs_metadata_version_inode_btree_change &&
175             btree_node_type_is_extents(btree_id) &&
176             bkey_cmp(bn->min_key, POS_MIN) &&
177             write)
178                 bn->min_key = bkey_predecessor(bn->min_key);
179
180         compat_bpos(level, btree_id, version, big_endian, write, &bn->min_key);
181         compat_bpos(level, btree_id, version, big_endian, write, &bn->max_key);
182
183         if (version < bcachefs_metadata_version_inode_btree_change &&
184             btree_node_type_is_extents(btree_id) &&
185             bkey_cmp(bn->min_key, POS_MIN) &&
186             !write)
187                 bn->min_key = bkey_successor(bn->min_key);
188 }
189
190 #endif /* _BCACHEFS_BTREE_IO_H */