Lines Matching refs:b
38 void bch2_btree_node_io_unlock(struct btree *b) in bch2_btree_node_io_unlock() argument
40 EBUG_ON(!btree_node_write_in_flight(b)); in bch2_btree_node_io_unlock()
42 clear_btree_node_write_in_flight_inner(b); in bch2_btree_node_io_unlock()
43 clear_btree_node_write_in_flight(b); in bch2_btree_node_io_unlock()
44 wake_up_bit(&b->flags, BTREE_NODE_write_in_flight); in bch2_btree_node_io_unlock()
47 void bch2_btree_node_io_lock(struct btree *b) in bch2_btree_node_io_lock() argument
49 wait_on_bit_lock_io(&b->flags, BTREE_NODE_write_in_flight, in bch2_btree_node_io_lock()
53 void __bch2_btree_node_wait_on_read(struct btree *b) in __bch2_btree_node_wait_on_read() argument
55 wait_on_bit_io(&b->flags, BTREE_NODE_read_in_flight, in __bch2_btree_node_wait_on_read()
59 void __bch2_btree_node_wait_on_write(struct btree *b) in __bch2_btree_node_wait_on_write() argument
61 wait_on_bit_io(&b->flags, BTREE_NODE_write_in_flight, in __bch2_btree_node_wait_on_write()
65 void bch2_btree_node_wait_on_read(struct btree *b) in bch2_btree_node_wait_on_read() argument
67 wait_on_bit_io(&b->flags, BTREE_NODE_read_in_flight, in bch2_btree_node_wait_on_read()
71 void bch2_btree_node_wait_on_write(struct btree *b) in bch2_btree_node_wait_on_write() argument
73 wait_on_bit_io(&b->flags, BTREE_NODE_write_in_flight, in bch2_btree_node_wait_on_write()
77 static void verify_no_dups(struct btree *b, in verify_no_dups() argument
90 struct bkey l = bkey_unpack_key(b, p); in verify_no_dups()
91 struct bkey r = bkey_unpack_key(b, k); in verify_no_dups()
136 unsigned n = nr, a = nr / 2, b, c, d; in sort_bkey_ptrs() local
150 for (b = a; c = 2 * b + 1, (d = c + 1) < n;) in sort_bkey_ptrs()
151 b = bch2_bkey_cmp_packed(bt, in sort_bkey_ptrs()
155 b = c; in sort_bkey_ptrs()
157 while (b != a && in sort_bkey_ptrs()
160 ptrs[b]) >= 0) in sort_bkey_ptrs()
161 b = (b - 1) / 2; in sort_bkey_ptrs()
162 c = b; in sort_bkey_ptrs()
163 while (b != a) { in sort_bkey_ptrs()
164 b = (b - 1) / 2; in sort_bkey_ptrs()
165 swap(ptrs[b], ptrs[c]); in sort_bkey_ptrs()
170 static void bch2_sort_whiteouts(struct bch_fs *c, struct btree *b) in bch2_sort_whiteouts() argument
174 size_t bytes = b->whiteout_u64s * sizeof(u64); in bch2_sort_whiteouts()
176 if (!b->whiteout_u64s) in bch2_sort_whiteouts()
183 for (k = unwritten_whiteouts_start(b); in bch2_sort_whiteouts()
184 k != unwritten_whiteouts_end(b); in bch2_sort_whiteouts()
188 sort_bkey_ptrs(b, ptrs, ptrs_end - ptrs); in bch2_sort_whiteouts()
198 verify_no_dups(b, new_whiteouts, in bch2_sort_whiteouts()
199 (void *) ((u64 *) new_whiteouts + b->whiteout_u64s)); in bch2_sort_whiteouts()
201 memcpy_u64s(unwritten_whiteouts_start(b), in bch2_sort_whiteouts()
202 new_whiteouts, b->whiteout_u64s); in bch2_sort_whiteouts()
207 static bool should_compact_bset(struct btree *b, struct bset_tree *t, in should_compact_bset() argument
210 if (!bset_dead_u64s(b, t)) in should_compact_bset()
215 return should_compact_bset_lazy(b, t) || in should_compact_bset()
216 (compacting && !bset_written(b, bset(b, t))); in should_compact_bset()
224 static bool bch2_drop_whiteouts(struct btree *b, enum compact_mode mode) in bch2_drop_whiteouts() argument
228 for_each_bset(b, t) { in bch2_drop_whiteouts()
229 struct bset *i = bset(b, t); in bch2_drop_whiteouts()
233 if (t != b->set && !bset_written(b, i)) { in bch2_drop_whiteouts()
235 dst = max(write_block(b), in bch2_drop_whiteouts()
236 (void *) btree_bkey_last(b, t - 1)); in bch2_drop_whiteouts()
242 if (!should_compact_bset(b, t, ret, mode)) { in bch2_drop_whiteouts()
248 set_btree_bset(b, t, i); in bch2_drop_whiteouts()
253 start = btree_bkey_first(b, t); in bch2_drop_whiteouts()
254 end = btree_bkey_last(b, t); in bch2_drop_whiteouts()
259 set_btree_bset(b, t, i); in bch2_drop_whiteouts()
276 set_btree_bset_end(b, t); in bch2_drop_whiteouts()
277 bch2_bset_set_no_aux_tree(b, t); in bch2_drop_whiteouts()
281 bch2_verify_btree_nr_keys(b); in bch2_drop_whiteouts()
283 bch2_btree_build_aux_trees(b); in bch2_drop_whiteouts()
288 bool bch2_compact_whiteouts(struct bch_fs *c, struct btree *b, in bch2_compact_whiteouts() argument
291 return bch2_drop_whiteouts(b, mode); in bch2_compact_whiteouts()
294 static void btree_node_sort(struct bch_fs *c, struct btree *b, in btree_node_sort() argument
301 struct bset *start_bset = bset(b, &b->set[start_idx]); in btree_node_sort()
306 end_idx == b->nsets; in btree_node_sort()
308 sort_iter_stack_init(&sort_iter, b); in btree_node_sort()
310 for (t = b->set + start_idx; in btree_node_sort()
311 t < b->set + end_idx; in btree_node_sort()
313 u64s += le16_to_cpu(bset(b, t)->u64s); in btree_node_sort()
315 btree_bkey_first(b, t), in btree_node_sort()
316 btree_bkey_last(b, t)); in btree_node_sort()
320 ? btree_buf_bytes(b) in btree_node_sort()
338 for (t = b->set + start_idx; t < b->set + end_idx; t++) in btree_node_sort()
339 seq = max(seq, le64_to_cpu(bset(b, t)->journal_seq)); in btree_node_sort()
345 BUG_ON(bytes != btree_buf_bytes(b)); in btree_node_sort()
352 *out = *b->data; in btree_node_sort()
354 swap(out, b->data); in btree_node_sort()
355 set_btree_bset(b, b->set, &b->data->keys); in btree_node_sort()
364 b->nr.bset_u64s[start_idx] += in btree_node_sort()
365 b->nr.bset_u64s[i]; in btree_node_sort()
367 b->nsets -= shift; in btree_node_sort()
369 for (i = start_idx + 1; i < b->nsets; i++) { in btree_node_sort()
370 b->nr.bset_u64s[i] = b->nr.bset_u64s[i + shift]; in btree_node_sort()
371 b->set[i] = b->set[i + shift]; in btree_node_sort()
374 for (i = b->nsets; i < MAX_BSETS; i++) in btree_node_sort()
375 b->nr.bset_u64s[i] = 0; in btree_node_sort()
377 set_btree_bset_end(b, &b->set[start_idx]); in btree_node_sort()
378 bch2_bset_set_no_aux_tree(b, &b->set[start_idx]); in btree_node_sort()
382 bch2_verify_btree_nr_keys(b); in btree_node_sort()
421 static bool btree_node_compact(struct bch_fs *c, struct btree *b) in btree_node_compact() argument
427 unwritten_idx < b->nsets; in btree_node_compact()
429 if (!bset_written(b, bset(b, &b->set[unwritten_idx]))) in btree_node_compact()
432 if (b->nsets - unwritten_idx > 1) { in btree_node_compact()
433 btree_node_sort(c, b, unwritten_idx, b->nsets); in btree_node_compact()
438 btree_node_sort(c, b, 0, unwritten_idx); in btree_node_compact()
445 void bch2_btree_build_aux_trees(struct btree *b) in bch2_btree_build_aux_trees() argument
447 for_each_bset(b, t) in bch2_btree_build_aux_trees()
448 bch2_bset_build_aux_tree(b, t, in bch2_btree_build_aux_trees()
449 !bset_written(b, bset(b, t)) && in bch2_btree_build_aux_trees()
450 t == bset_tree_last(b)); in bch2_btree_build_aux_trees()
463 static inline bool should_compact_all(struct bch_fs *c, struct btree *b) in should_compact_all() argument
468 return bset_u64s(&b->set[1]) > 1U << mid_u64s_bits; in should_compact_all()
480 void bch2_btree_init_next(struct btree_trans *trans, struct btree *b) in bch2_btree_init_next() argument
486 EBUG_ON(!six_lock_counts(&b->c.lock).n[SIX_LOCK_write]); in bch2_btree_init_next()
487 BUG_ON(bset_written(b, bset(b, &b->set[1]))); in bch2_btree_init_next()
488 BUG_ON(btree_node_just_written(b)); in bch2_btree_init_next()
490 if (b->nsets == MAX_BSETS && in bch2_btree_init_next()
491 !btree_node_write_in_flight(b) && in bch2_btree_init_next()
492 should_compact_all(c, b)) { in bch2_btree_init_next()
493 bch2_btree_node_write(c, b, SIX_LOCK_write, in bch2_btree_init_next()
498 if (b->nsets == MAX_BSETS && in bch2_btree_init_next()
499 btree_node_compact(c, b)) in bch2_btree_init_next()
502 BUG_ON(b->nsets >= MAX_BSETS); in bch2_btree_init_next()
504 bne = want_new_bset(c, b); in bch2_btree_init_next()
506 bch2_bset_init_next(b, bne); in bch2_btree_init_next()
508 bch2_btree_build_aux_trees(b); in bch2_btree_init_next()
511 bch2_trans_node_reinit_iter(trans, b); in bch2_btree_init_next()
516 struct btree *b, struct bset *i, struct bkey_packed *k, in btree_err_msg() argument
526 bch2_btree_pos_to_text(out, c, b); in btree_err_msg()
531 b->written, btree_ptr_sectors_written(bkey_i_to_s_c(&b->key))); in btree_err_msg()
545 struct btree *b, in __btree_err() argument
557 btree_err_msg(&out, c, ca, b, i, k, b->written, write); in __btree_err()
613 #define btree_err(type, c, ca, b, i, k, _err_type, msg, ...) \ argument
615 int _ret = __btree_err(type, c, ca, b, i, k, write, have_retry, \
634 void bch2_btree_node_drop_keys_outside_node(struct btree *b) in bch2_btree_node_drop_keys_outside_node() argument
636 for_each_bset(b, t) { in bch2_btree_node_drop_keys_outside_node()
637 struct bset *i = bset(b, t); in bch2_btree_node_drop_keys_outside_node()
641 if (bkey_cmp_left_packed(b, k, &b->data->min_key) >= 0) in bch2_btree_node_drop_keys_outside_node()
650 set_btree_bset_end(b, t); in bch2_btree_node_drop_keys_outside_node()
654 if (bkey_cmp_left_packed(b, k, &b->data->max_key) > 0) in bch2_btree_node_drop_keys_outside_node()
659 set_btree_bset_end(b, t); in bch2_btree_node_drop_keys_outside_node()
667 bch2_bset_set_no_aux_tree(b, b->set); in bch2_btree_node_drop_keys_outside_node()
668 bch2_btree_build_aux_trees(b); in bch2_btree_node_drop_keys_outside_node()
669 b->nr = bch2_btree_node_count_keys(b); in bch2_btree_node_drop_keys_outside_node()
674 for_each_btree_node_key_unpack(b, k, &iter, &unpacked) { in bch2_btree_node_drop_keys_outside_node()
675 BUG_ON(bpos_lt(k.k->p, b->data->min_key)); in bch2_btree_node_drop_keys_outside_node()
676 BUG_ON(bpos_gt(k.k->p, b->data->max_key)); in bch2_btree_node_drop_keys_outside_node()
681 struct btree *b, struct bset *i, in validate_bset() argument
686 unsigned ptr_written = btree_ptr_sectors_written(bkey_i_to_s_c(&b->key)); in validate_bset()
693 c, ca, b, i, NULL, in validate_bset()
701 c, NULL, b, i, NULL, in validate_bset()
714 c, NULL, b, i, NULL, in validate_bset()
726 c, ca, b, i, NULL, in validate_bset()
733 c, ca, b, i, NULL, in validate_bset()
741 c, ca, b, i, NULL, in validate_bset()
747 c, ca, b, i, NULL, in validate_bset()
756 if (b->key.k.type == KEY_TYPE_btree_ptr_v2) { in validate_bset()
758 &bkey_i_to_btree_ptr_v2(&b->key)->v; in validate_bset()
763 c, ca, b, NULL, NULL, in validate_bset()
768 btree_err_on(BTREE_NODE_ID(bn) != b->c.btree_id, in validate_bset()
770 c, ca, b, i, NULL, in validate_bset()
774 btree_err_on(BTREE_NODE_LEVEL(bn) != b->c.level, in validate_bset()
776 c, ca, b, i, NULL, in validate_bset()
781 compat_btree_node(b->c.level, b->c.btree_id, version, in validate_bset()
784 if (b->key.k.type == KEY_TYPE_btree_ptr_v2) { in validate_bset()
786 &bkey_i_to_btree_ptr_v2(&b->key)->v; in validate_bset()
789 b->data->min_key = bp->min_key; in validate_bset()
790 b->data->max_key = b->key.k.p; in validate_bset()
793 btree_err_on(!bpos_eq(b->data->min_key, bp->min_key), in validate_bset()
795 c, ca, b, NULL, NULL, in validate_bset()
804 btree_err_on(!bpos_eq(bn->max_key, b->key.k.p), in validate_bset()
806 c, ca, b, i, NULL, in validate_bset()
813 compat_btree_node(b->c.level, b->c.btree_id, version, in validate_bset()
818 c, ca, b, i, NULL, in validate_bset()
825 compat_bformat(b->c.level, b->c.btree_id, version, in validate_bset()
835 static int bset_key_validate(struct bch_fs *c, struct btree *b, in bset_key_validate() argument
839 return __bch2_bkey_validate(c, k, btree_node_type(b), 0) ?: in bset_key_validate()
840 (!updated_range ? bch2_bkey_in_btree_node(c, b, k, 0) : 0) ?: in bset_key_validate()
844 static bool bkey_packed_valid(struct bch_fs *c, struct btree *b, in bkey_packed_valid() argument
853 if (!bkeyp_u64s_valid(&b->format, k)) in bkey_packed_valid()
857 struct bkey_s u = __bkey_disassemble(b, k, &tmp); in bkey_packed_valid()
858 return !__bch2_bkey_validate(c, u.s_c, btree_node_type(b), BCH_VALIDATE_silent); in bkey_packed_valid()
861 static int validate_bset_keys(struct bch_fs *c, struct btree *b, in validate_bset_keys() argument
868 bool updated_range = b->key.k.type == KEY_TYPE_btree_ptr_v2 && in validate_bset_keys()
869 BTREE_PTR_RANGE_UPDATED(&bkey_i_to_btree_ptr_v2(&b->key)->v); in validate_bset_keys()
880 c, NULL, b, i, k, in validate_bset_keys()
889 c, NULL, b, i, k, in validate_bset_keys()
894 if (btree_err_on(!bkeyp_u64s_valid(&b->format, k), in validate_bset_keys()
896 c, NULL, b, i, k, in validate_bset_keys()
899 bkeyp_key_u64s(&b->format, k), in validate_bset_keys()
900 U8_MAX - BKEY_U64s + bkeyp_key_u64s(&b->format, k))) in validate_bset_keys()
904 bch2_bkey_compat(b->c.level, b->c.btree_id, version, in validate_bset_keys()
906 &b->format, k); in validate_bset_keys()
908 u = __bkey_disassemble(b, k, &tmp); in validate_bset_keys()
910 ret = bset_key_validate(c, b, u.s_c, updated_range, write); in validate_bset_keys()
917 bch2_bkey_compat(b->c.level, b->c.btree_id, version, in validate_bset_keys()
919 &b->format, k); in validate_bset_keys()
921 if (prev && bkey_iter_cmp(b, prev, k) > 0) { in validate_bset_keys()
922 struct bkey up = bkey_unpack_key(b, prev); in validate_bset_keys()
931 c, NULL, b, i, k, in validate_bset_keys()
951 if (!bkey_packed_valid(c, b, i, (void *) ((u64 *) k + next_good_key))) { in validate_bset_keys()
955 if (bkey_packed_valid(c, b, i, (void *) ((u64 *) k + next_good_key))) in validate_bset_keys()
975 struct btree *b, bool have_retry, bool *saw_error) in bch2_btree_node_read_done() argument
983 bool updated_range = b->key.k.type == KEY_TYPE_btree_ptr_v2 && in bch2_btree_node_read_done()
984 BTREE_PTR_RANGE_UPDATED(&bkey_i_to_btree_ptr_v2(&b->key)->v); in bch2_btree_node_read_done()
986 unsigned ptr_written = btree_ptr_sectors_written(bkey_i_to_s_c(&b->key)); in bch2_btree_node_read_done()
992 b->version_ondisk = U16_MAX; in bch2_btree_node_read_done()
994 b->written = 0; in bch2_btree_node_read_done()
997 sort_iter_init(iter, b, (btree_blocks(c) + 1) * 2); in bch2_btree_node_read_done()
1001 c, ca, b, NULL, NULL, in bch2_btree_node_read_done()
1005 btree_err_on(le64_to_cpu(b->data->magic) != bset_magic(c), in bch2_btree_node_read_done()
1007 c, ca, b, NULL, NULL, in bch2_btree_node_read_done()
1010 bset_magic(c), le64_to_cpu(b->data->magic)); in bch2_btree_node_read_done()
1012 if (b->key.k.type == KEY_TYPE_btree_ptr_v2) { in bch2_btree_node_read_done()
1014 &bkey_i_to_btree_ptr_v2(&b->key)->v; in bch2_btree_node_read_done()
1016 bch2_bpos_to_text(&buf, b->data->min_key); in bch2_btree_node_read_done()
1018 bch2_bpos_to_text(&buf, b->data->max_key); in bch2_btree_node_read_done()
1020 btree_err_on(b->data->keys.seq != bp->seq, in bch2_btree_node_read_done()
1022 c, ca, b, NULL, NULL, in bch2_btree_node_read_done()
1026 bch2_btree_node_header_to_text(&buf, b->data), in bch2_btree_node_read_done()
1029 btree_err_on(!b->data->keys.seq, in bch2_btree_node_read_done()
1031 c, ca, b, NULL, NULL, in bch2_btree_node_read_done()
1035 bch2_btree_node_header_to_text(&buf, b->data), in bch2_btree_node_read_done()
1039 while (b->written < (ptr_written ?: btree_sectors(c))) { in bch2_btree_node_read_done()
1042 bool first = !b->written; in bch2_btree_node_read_done()
1045 if (!b->written) { in bch2_btree_node_read_done()
1046 i = &b->data->keys; in bch2_btree_node_read_done()
1050 c, ca, b, i, NULL, in bch2_btree_node_read_done()
1054 nonce = btree_nonce(i, b->written << 9); in bch2_btree_node_read_done()
1056 struct bch_csum csum = csum_vstruct(c, BSET_CSUM_TYPE(i), nonce, b->data); in bch2_btree_node_read_done()
1057 csum_bad = bch2_crc_cmp(b->data->csum, csum); in bch2_btree_node_read_done()
1063 c, ca, b, i, NULL, in bch2_btree_node_read_done()
1067 bch2_csum_err_msg(&buf, BSET_CSUM_TYPE(i), b->data->csum, csum), in bch2_btree_node_read_done()
1070 ret = bset_encrypt(c, i, b->written << 9); in bch2_btree_node_read_done()
1075 btree_err_on(btree_node_type_is_extents(btree_node_type(b)) && in bch2_btree_node_read_done()
1076 !BTREE_NODE_NEW_EXTENT_OVERWRITE(b->data), in bch2_btree_node_read_done()
1078 c, NULL, b, NULL, NULL, in bch2_btree_node_read_done()
1082 sectors = vstruct_sectors(b->data, c->block_bits); in bch2_btree_node_read_done()
1084 bne = write_block(b); in bch2_btree_node_read_done()
1087 if (i->seq != b->data->keys.seq) in bch2_btree_node_read_done()
1092 c, ca, b, i, NULL, in bch2_btree_node_read_done()
1096 nonce = btree_nonce(i, b->written << 9); in bch2_btree_node_read_done()
1104 c, ca, b, i, NULL, in bch2_btree_node_read_done()
1111 ret = bset_encrypt(c, i, b->written << 9); in bch2_btree_node_read_done()
1119 b->version_ondisk = min(b->version_ondisk, in bch2_btree_node_read_done()
1122 ret = validate_bset(c, ca, b, i, b->written, sectors, in bch2_btree_node_read_done()
1127 if (!b->written) in bch2_btree_node_read_done()
1128 btree_node_set_format(b, b->data->format); in bch2_btree_node_read_done()
1130 ret = validate_bset_keys(c, b, i, READ, have_retry, saw_error); in bch2_btree_node_read_done()
1142 c, ca, b, i, NULL, in bch2_btree_node_read_done()
1149 c, ca, b, i, NULL, in bch2_btree_node_read_done()
1153 b->written, b->written + sectors, ptr_written); in bch2_btree_node_read_done()
1155 b->written += sectors; in bch2_btree_node_read_done()
1168 btree_err_on(b->written < ptr_written, in bch2_btree_node_read_done()
1170 c, ca, b, NULL, NULL, in bch2_btree_node_read_done()
1173 ptr_written, b->written); in bch2_btree_node_read_done()
1175 for (bne = write_block(b); in bch2_btree_node_read_done()
1176 bset_byte_offset(b, bne) < btree_buf_bytes(b); in bch2_btree_node_read_done()
1178 btree_err_on(bne->keys.seq == b->data->keys.seq && in bch2_btree_node_read_done()
1183 c, ca, b, NULL, NULL, in bch2_btree_node_read_done()
1188 sorted = btree_bounce_alloc(c, btree_buf_bytes(b), &used_mempool); in bch2_btree_node_read_done()
1191 set_btree_bset(b, b->set, &b->data->keys); in bch2_btree_node_read_done()
1193 b->nr = bch2_key_sort_fix_overlapping(c, &sorted->keys, iter); in bch2_btree_node_read_done()
1194 memset((uint8_t *)(sorted + 1) + b->nr.live_u64s * sizeof(u64), 0, in bch2_btree_node_read_done()
1195 btree_buf_bytes(b) - in bch2_btree_node_read_done()
1197 b->nr.live_u64s * sizeof(u64)); in bch2_btree_node_read_done()
1200 *sorted = *b->data; in bch2_btree_node_read_done()
1202 swap(sorted, b->data); in bch2_btree_node_read_done()
1203 set_btree_bset(b, b->set, &b->data->keys); in bch2_btree_node_read_done()
1204 b->nsets = 1; in bch2_btree_node_read_done()
1205 b->data->keys.journal_seq = cpu_to_le64(max_journal_seq); in bch2_btree_node_read_done()
1207 BUG_ON(b->nr.live_u64s != u64s); in bch2_btree_node_read_done()
1209 btree_bounce_free(c, btree_buf_bytes(b), used_mempool, sorted); in bch2_btree_node_read_done()
1212 bch2_btree_node_drop_keys_outside_node(b); in bch2_btree_node_read_done()
1214 i = &b->data->keys; in bch2_btree_node_read_done()
1217 struct bkey_s u = __bkey_disassemble(b, k, &tmp); in bch2_btree_node_read_done()
1223 btree_keys_account_key_drop(&b->nr, 0, k); in bch2_btree_node_read_done()
1228 set_btree_bset_end(b, b->set); in bch2_btree_node_read_done()
1243 bch2_bset_build_aux_tree(b, b->set, false); in bch2_btree_node_read_done()
1245 set_needs_whiteout(btree_bset_first(b), true); in bch2_btree_node_read_done()
1247 btree_node_reset_sib_u64s(b); in bch2_btree_node_read_done()
1250 bkey_for_each_ptr(bch2_bkey_ptrs(bkey_i_to_s(&b->key)), ptr) { in bch2_btree_node_read_done()
1254 set_btree_node_need_rewrite(b); in bch2_btree_node_read_done()
1259 set_btree_node_need_rewrite(b); in bch2_btree_node_read_done()
1270 set_btree_node_read_error(b); in bch2_btree_node_read_done()
1271 bch2_btree_lost_data(c, b->c.btree_id); in bch2_btree_node_read_done()
1282 struct btree *b = rb->b; in btree_node_read_work() local
1298 bio->bi_iter.bi_size = btree_buf_bytes(b); in btree_node_read_work()
1308 bch2_btree_pos_to_text(&buf, c, b); in btree_node_read_work()
1319 bkey_i_to_s_c(&b->key), in btree_node_read_work()
1323 !bch2_btree_node_read_done(c, ca, b, can_retry, &saw_error)) { in btree_node_read_work()
1332 set_btree_node_read_error(b); in btree_node_read_work()
1333 bch2_btree_lost_data(c, b->c.btree_id); in btree_node_read_work()
1343 !btree_node_read_error(b) && in btree_node_read_work()
1346 bch2_bpos_to_text(&buf, b->key.k.p); in btree_node_read_work()
1348 __func__, bch2_btree_id_str(b->c.btree_id), b->c.level, buf.buf); in btree_node_read_work()
1350 bch2_btree_node_rewrite_async(c, b); in btree_node_read_work()
1354 clear_btree_node_read_in_flight(b); in btree_node_read_work()
1355 wake_up_bit(&b->flags, BTREE_NODE_read_in_flight); in btree_node_read_work()
1376 struct btree *b; member
1429 struct btree *b = ra->b; in CLOSURE_CALLBACK() local
1435 __le64 seq = b->key.k.type == KEY_TYPE_btree_ptr_v2 in CLOSURE_CALLBACK()
1436 ? bkey_i_to_btree_ptr_v2(&b->key)->v.seq : 0; in CLOSURE_CALLBACK()
1457 c, NULL, b, NULL, NULL, in CLOSURE_CALLBACK()
1463 c, NULL, b, NULL, NULL, in CLOSURE_CALLBACK()
1468 c, NULL, b, NULL, NULL, in CLOSURE_CALLBACK()
1529 memcpy(b->data, ra->buf[best], btree_buf_bytes(b)); in CLOSURE_CALLBACK()
1530 ret = bch2_btree_node_read_done(c, NULL, b, false, saw_error); in CLOSURE_CALLBACK()
1536 set_btree_node_read_error(b); in CLOSURE_CALLBACK()
1537 bch2_btree_lost_data(c, b->c.btree_id); in CLOSURE_CALLBACK()
1539 bch2_btree_node_rewrite_async(c, b); in CLOSURE_CALLBACK()
1550 clear_btree_node_read_in_flight(b); in CLOSURE_CALLBACK()
1551 wake_up_bit(&b->flags, BTREE_NODE_read_in_flight); in CLOSURE_CALLBACK()
1575 static int btree_node_read_all_replicas(struct bch_fs *c, struct btree *b, bool sync) in btree_node_read_all_replicas() argument
1577 struct bkey_s_c k = bkey_i_to_s_c(&b->key); in btree_node_read_all_replicas()
1590 ra->b = b; in btree_node_read_all_replicas()
1596 buf_pages(ra->buf[i], btree_buf_bytes(b)), in btree_node_read_all_replicas()
1608 rb->b = b; in btree_node_read_all_replicas()
1616 bch2_bio_map(&rb->bio, ra->buf[i], btree_buf_bytes(b)); in btree_node_read_all_replicas()
1643 void bch2_btree_node_read(struct btree_trans *trans, struct btree *b, in bch2_btree_node_read() argument
1653 trace_and_count(c, btree_node_read, trans, b); in bch2_btree_node_read()
1656 !btree_node_read_all_replicas(c, b, sync)) in bch2_btree_node_read()
1659 ret = bch2_bkey_pick_read_device(c, bkey_i_to_s_c(&b->key), in bch2_btree_node_read()
1666 bch2_btree_pos_to_text(&buf, c, b); in bch2_btree_node_read()
1673 set_btree_node_read_error(b); in bch2_btree_node_read()
1674 bch2_btree_lost_data(c, b->c.btree_id); in bch2_btree_node_read()
1675 clear_btree_node_read_in_flight(b); in bch2_btree_node_read()
1676 wake_up_bit(&b->flags, BTREE_NODE_read_in_flight); in bch2_btree_node_read()
1684 buf_pages(b->data, btree_buf_bytes(b)), in bch2_btree_node_read()
1690 rb->b = b; in bch2_btree_node_read()
1698 bch2_bio_map(bio, b->data, btree_buf_bytes(b)); in bch2_btree_node_read()
1727 struct btree *b; in __bch2_btree_root_read() local
1737 b = bch2_btree_node_mem_alloc(trans, level != 0); in __bch2_btree_root_read()
1740 BUG_ON(IS_ERR(b)); in __bch2_btree_root_read()
1742 bkey_copy(&b->key, k); in __bch2_btree_root_read()
1743 BUG_ON(bch2_btree_node_hash_insert(&c->btree_cache, b, level, id)); in __bch2_btree_root_read()
1745 set_btree_node_read_in_flight(b); in __bch2_btree_root_read()
1749 bch2_btree_node_read(trans, b, true); in __bch2_btree_root_read()
1751 if (btree_node_read_error(b)) { in __bch2_btree_root_read()
1753 bch2_btree_node_hash_remove(&c->btree_cache, b); in __bch2_btree_root_read()
1760 bch2_btree_set_root_for_read(c, b); in __bch2_btree_root_read()
1762 six_unlock_write(&b->c.lock); in __bch2_btree_root_read()
1763 six_unlock_intent(&b->c.lock); in __bch2_btree_root_read()
1774 static void bch2_btree_complete_write(struct bch_fs *c, struct btree *b, in bch2_btree_complete_write() argument
1779 old = READ_ONCE(b->will_make_reachable); in bch2_btree_complete_write()
1786 } while (!try_cmpxchg(&b->will_make_reachable, &old, new)); in bch2_btree_complete_write()
1794 static void __btree_node_write_done(struct bch_fs *c, struct btree *b) in __btree_node_write_done() argument
1796 struct btree_write *w = btree_prev_write(b); in __btree_node_write_done()
1800 bch2_btree_complete_write(c, b, w); in __btree_node_write_done()
1802 old = READ_ONCE(b->flags); in __btree_node_write_done()
1824 } while (!try_cmpxchg(&b->flags, &old, new)); in __btree_node_write_done()
1827 __bch2_btree_node_write(c, b, BTREE_WRITE_ALREADY_STARTED|type); in __btree_node_write_done()
1829 wake_up_bit(&b->flags, BTREE_NODE_write_in_flight); in __btree_node_write_done()
1832 static void btree_node_write_done(struct bch_fs *c, struct btree *b) in btree_node_write_done() argument
1836 btree_node_lock_nopath_nofail(trans, &b->c, SIX_LOCK_read); in btree_node_write_done()
1840 __btree_node_write_done(c, b); in btree_node_write_done()
1841 six_unlock_read(&b->c.lock); in btree_node_write_done()
1849 struct btree *b = wbio->wbio.bio.bi_private; in btree_node_write_work() local
1871 bch2_btree_node_update_key_get_iter(trans, b, &wbio->key, in btree_node_write_work()
1882 btree_node_write_done(c, b); in btree_node_write_work()
1885 set_btree_node_noevict(b); in btree_node_write_work()
1898 struct btree *b = wbio->bio.bi_private; in btree_node_write_endio() local
1924 clear_btree_node_write_in_flight_inner(b); in btree_node_write_endio()
1925 wake_up_bit(&b->flags, BTREE_NODE_write_in_flight_inner); in btree_node_write_endio()
1930 static int validate_bset_for_write(struct bch_fs *c, struct btree *b, in validate_bset_for_write() argument
1935 int ret = bch2_bkey_validate(c, bkey_i_to_s_c(&b->key), in validate_bset_for_write()
1942 ret = validate_bset_keys(c, b, i, WRITE, false, &saw_error) ?: in validate_bset_for_write()
1943 validate_bset(c, NULL, b, i, b->written, sectors, WRITE, false, &saw_error); in validate_bset_for_write()
1966 void __bch2_btree_node_write(struct bch_fs *c, struct btree *b, unsigned flags) in __bch2_btree_node_write() argument
1993 old = READ_ONCE(b->flags); in __bch2_btree_node_write()
2009 if (b->written && in __bch2_btree_node_write()
2026 } while (!try_cmpxchg_acquire(&b->flags, &old, new)); in __bch2_btree_node_write()
2031 BUG_ON((type == BTREE_WRITE_initial) != (b->written == 0)); in __bch2_btree_node_write()
2035 BUG_ON(btree_node_fake(b)); in __bch2_btree_node_write()
2036 BUG_ON((b->will_make_reachable != 0) != !b->written); in __bch2_btree_node_write()
2038 BUG_ON(b->written >= btree_sectors(c)); in __bch2_btree_node_write()
2039 BUG_ON(b->written & (block_sectors(c) - 1)); in __bch2_btree_node_write()
2040 BUG_ON(bset_written(b, btree_bset_last(b))); in __bch2_btree_node_write()
2041 BUG_ON(le64_to_cpu(b->data->magic) != bset_magic(c)); in __bch2_btree_node_write()
2042 BUG_ON(memcmp(&b->data->format, &b->format, sizeof(b->format))); in __bch2_btree_node_write()
2044 bch2_sort_whiteouts(c, b); in __bch2_btree_node_write()
2046 sort_iter_stack_init(&sort_iter, b); in __bch2_btree_node_write()
2048 bytes = !b->written in __bch2_btree_node_write()
2052 bytes += b->whiteout_u64s * sizeof(u64); in __bch2_btree_node_write()
2054 for_each_bset(b, t) { in __bch2_btree_node_write()
2055 i = bset(b, t); in __bch2_btree_node_write()
2057 if (bset_written(b, i)) in __bch2_btree_node_write()
2062 btree_bkey_first(b, t), in __bch2_btree_node_write()
2063 btree_bkey_last(b, t)); in __bch2_btree_node_write()
2067 BUG_ON(b->written && !seq); in __bch2_btree_node_write()
2077 if (!b->written) { in __bch2_btree_node_write()
2079 *bn = *b->data; in __bch2_btree_node_write()
2083 bne->keys = b->data->keys; in __bch2_btree_node_write()
2091 unwritten_whiteouts_start(b), in __bch2_btree_node_write()
2092 unwritten_whiteouts_end(b)); in __bch2_btree_node_write()
2098 b->whiteout_u64s = 0; in __bch2_btree_node_write()
2100 BUG_ON(!b->written && i->u64s != b->data->keys.u64s); in __bch2_btree_node_write()
2105 if (b->written && !i->u64s) in __bch2_btree_node_write()
2111 if (!b->written && in __bch2_btree_node_write()
2112 b->key.k.type == KEY_TYPE_btree_ptr_v2) in __bch2_btree_node_write()
2113 BUG_ON(btree_ptr_sectors_written(bkey_i_to_s_c(&b->key)) != sectors_to_write); in __bch2_btree_node_write()
2118 BUG_ON(b->written + sectors_to_write > btree_sectors(c)); in __bch2_btree_node_write()
2120 BUG_ON(i->seq != b->data->keys.seq); in __bch2_btree_node_write()
2123 SET_BSET_OFFSET(i, b->written); in __bch2_btree_node_write()
2135 validate_bset_for_write(c, b, i, sectors_to_write)) in __bch2_btree_node_write()
2138 ret = bset_encrypt(c, i, b->written << 9); in __bch2_btree_node_write()
2143 nonce = btree_nonce(i, b->written << 9); in __bch2_btree_node_write()
2152 validate_bset_for_write(c, b, i, sectors_to_write)) in __bch2_btree_node_write()
2177 trace_and_count(c, btree_node_write, b, bytes_to_write, sectors_to_write); in __bch2_btree_node_write()
2188 wbio->sector_offset = b->written; in __bch2_btree_node_write()
2191 wbio->wbio.first_btree_write = !b->written; in __bch2_btree_node_write()
2193 wbio->wbio.bio.bi_private = b; in __bch2_btree_node_write()
2197 bkey_copy(&wbio->key, &b->key); in __bch2_btree_node_write()
2199 b->written += sectors_to_write; in __bch2_btree_node_write()
2203 cpu_to_le16(b->written); in __bch2_btree_node_write()
2212 set_btree_node_noevict(b); in __bch2_btree_node_write()
2213 b->written += sectors_to_write; in __bch2_btree_node_write()
2216 __btree_node_write_done(c, b); in __bch2_btree_node_write()
2222 bool bch2_btree_post_write_cleanup(struct bch_fs *c, struct btree *b) in bch2_btree_post_write_cleanup() argument
2227 if (!btree_node_just_written(b)) in bch2_btree_post_write_cleanup()
2230 BUG_ON(b->whiteout_u64s); in bch2_btree_post_write_cleanup()
2232 clear_btree_node_just_written(b); in bch2_btree_post_write_cleanup()
2247 if (b->nsets > 1) { in bch2_btree_post_write_cleanup()
2248 btree_node_sort(c, b, 0, b->nsets); in bch2_btree_post_write_cleanup()
2251 invalidated_iter = bch2_drop_whiteouts(b, COMPACT_ALL); in bch2_btree_post_write_cleanup()
2254 for_each_bset(b, t) in bch2_btree_post_write_cleanup()
2255 set_needs_whiteout(bset(b, t), true); in bch2_btree_post_write_cleanup()
2257 bch2_btree_verify(c, b); in bch2_btree_post_write_cleanup()
2263 BUG_ON((void *) btree_bkey_last(b, bset_tree_last(b)) > write_block(b)); in bch2_btree_post_write_cleanup()
2265 bne = want_new_bset(c, b); in bch2_btree_post_write_cleanup()
2267 bch2_bset_init_next(b, bne); in bch2_btree_post_write_cleanup()
2269 bch2_btree_build_aux_trees(b); in bch2_btree_post_write_cleanup()
2277 void bch2_btree_node_write(struct bch_fs *c, struct btree *b, in bch2_btree_node_write() argument
2283 six_lock_tryupgrade(&b->c.lock))) { in bch2_btree_node_write()
2284 __bch2_btree_node_write(c, b, flags); in bch2_btree_node_write()
2287 if (btree_node_just_written(b) && in bch2_btree_node_write()
2288 six_trylock_write(&b->c.lock)) { in bch2_btree_node_write()
2289 bch2_btree_post_write_cleanup(c, b); in bch2_btree_node_write()
2290 six_unlock_write(&b->c.lock); in bch2_btree_node_write()
2294 six_lock_downgrade(&b->c.lock); in bch2_btree_node_write()
2296 __bch2_btree_node_write(c, b, flags); in bch2_btree_node_write()
2298 btree_node_just_written(b)) in bch2_btree_node_write()
2299 bch2_btree_post_write_cleanup(c, b); in bch2_btree_node_write()
2307 struct btree *b; in __bch2_btree_flush_all() local
2312 for_each_cached_btree(b, c, tbl, i, pos) in __bch2_btree_flush_all()
2313 if (test_bit(flag, &b->flags)) { in __bch2_btree_flush_all()
2315 wait_on_bit_io(&b->flags, flag, TASK_UNINTERRUPTIBLE); in __bch2_btree_flush_all()