xref: /linux/fs/bcachefs/backpointers.h (revision 8e07e0e3964ca4e23ce7b68e2096fe660a888942)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_BACKPOINTERS_BACKGROUND_H
3 #define _BCACHEFS_BACKPOINTERS_BACKGROUND_H
4 
5 #include "btree_iter.h"
6 #include "btree_update.h"
7 #include "buckets.h"
8 #include "super.h"
9 
10 static inline u64 swab40(u64 x)
11 {
12 	return (((x & 0x00000000ffULL) << 32)|
13 		((x & 0x000000ff00ULL) << 16)|
14 		((x & 0x0000ff0000ULL) >>  0)|
15 		((x & 0x00ff000000ULL) >> 16)|
16 		((x & 0xff00000000ULL) >> 32));
17 }
18 
19 int bch2_backpointer_invalid(struct bch_fs *, struct bkey_s_c k,
20 			     enum bkey_invalid_flags, struct printbuf *);
21 void bch2_backpointer_to_text(struct printbuf *, const struct bch_backpointer *);
22 void bch2_backpointer_k_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
23 void bch2_backpointer_swab(struct bkey_s);
24 
25 #define bch2_bkey_ops_backpointer ((struct bkey_ops) {	\
26 	.key_invalid	= bch2_backpointer_invalid,	\
27 	.val_to_text	= bch2_backpointer_k_to_text,	\
28 	.swab		= bch2_backpointer_swab,	\
29 	.min_val_size	= 32,				\
30 })
31 
32 #define MAX_EXTENT_COMPRESS_RATIO_SHIFT		10
33 
34 /*
35  * Convert from pos in backpointer btree to pos of corresponding bucket in alloc
36  * btree:
37  */
38 static inline struct bpos bp_pos_to_bucket(const struct bch_fs *c,
39 					   struct bpos bp_pos)
40 {
41 	struct bch_dev *ca = bch_dev_bkey_exists(c, bp_pos.inode);
42 	u64 bucket_sector = bp_pos.offset >> MAX_EXTENT_COMPRESS_RATIO_SHIFT;
43 
44 	return POS(bp_pos.inode, sector_to_bucket(ca, bucket_sector));
45 }
46 
47 /*
48  * Convert from pos in alloc btree + bucket offset to pos in backpointer btree:
49  */
50 static inline struct bpos bucket_pos_to_bp(const struct bch_fs *c,
51 					   struct bpos bucket,
52 					   u64 bucket_offset)
53 {
54 	struct bch_dev *ca = bch_dev_bkey_exists(c, bucket.inode);
55 	struct bpos ret;
56 
57 	ret = POS(bucket.inode,
58 		  (bucket_to_sector(ca, bucket.offset) <<
59 		   MAX_EXTENT_COMPRESS_RATIO_SHIFT) + bucket_offset);
60 
61 	EBUG_ON(!bkey_eq(bucket, bp_pos_to_bucket(c, ret)));
62 
63 	return ret;
64 }
65 
66 int bch2_bucket_backpointer_mod_nowritebuffer(struct btree_trans *, struct bkey_i_backpointer *,
67 				struct bch_backpointer, struct bkey_s_c, bool);
68 
69 static inline int bch2_bucket_backpointer_mod(struct btree_trans *trans,
70 				struct bpos bucket,
71 				struct bch_backpointer bp,
72 				struct bkey_s_c orig_k,
73 				bool insert)
74 {
75 	struct bch_fs *c = trans->c;
76 	struct bkey_i_backpointer *bp_k;
77 	int ret;
78 
79 	bp_k = bch2_trans_kmalloc_nomemzero(trans, sizeof(struct bkey_i_backpointer));
80 	ret = PTR_ERR_OR_ZERO(bp_k);
81 	if (ret)
82 		return ret;
83 
84 	bkey_backpointer_init(&bp_k->k_i);
85 	bp_k->k.p = bucket_pos_to_bp(c, bucket, bp.bucket_offset);
86 	bp_k->v = bp;
87 
88 	if (!insert) {
89 		bp_k->k.type = KEY_TYPE_deleted;
90 		set_bkey_val_u64s(&bp_k->k, 0);
91 	}
92 
93 	if (unlikely(bch2_backpointers_no_use_write_buffer))
94 		return bch2_bucket_backpointer_mod_nowritebuffer(trans, bp_k, bp, orig_k, insert);
95 
96 	return bch2_trans_update_buffered(trans, BTREE_ID_backpointers, &bp_k->k_i);
97 }
98 
99 static inline enum bch_data_type bkey_ptr_data_type(enum btree_id btree_id, unsigned level,
100 						    struct bkey_s_c k, struct extent_ptr_decoded p)
101 {
102 	return  level		? BCH_DATA_btree :
103 		p.has_ec	? BCH_DATA_stripe :
104 				  BCH_DATA_user;
105 }
106 
107 static inline void bch2_extent_ptr_to_bp(struct bch_fs *c,
108 			   enum btree_id btree_id, unsigned level,
109 			   struct bkey_s_c k, struct extent_ptr_decoded p,
110 			   struct bpos *bucket_pos, struct bch_backpointer *bp)
111 {
112 	enum bch_data_type data_type = bkey_ptr_data_type(btree_id, level, k, p);
113 	s64 sectors = level ? btree_sectors(c) : k.k->size;
114 	u32 bucket_offset;
115 
116 	*bucket_pos = PTR_BUCKET_POS_OFFSET(c, &p.ptr, &bucket_offset);
117 	*bp = (struct bch_backpointer) {
118 		.btree_id	= btree_id,
119 		.level		= level,
120 		.data_type	= data_type,
121 		.bucket_offset	= ((u64) bucket_offset << MAX_EXTENT_COMPRESS_RATIO_SHIFT) +
122 			p.crc.offset,
123 		.bucket_len	= ptr_disk_sectors(sectors, p),
124 		.pos		= k.k->p,
125 	};
126 }
127 
128 int bch2_get_next_backpointer(struct btree_trans *, struct bpos, int,
129 			      struct bpos *, struct bch_backpointer *, unsigned);
130 struct bkey_s_c bch2_backpointer_get_key(struct btree_trans *, struct btree_iter *,
131 					 struct bpos, struct bch_backpointer,
132 					 unsigned);
133 struct btree *bch2_backpointer_get_node(struct btree_trans *, struct btree_iter *,
134 					struct bpos, struct bch_backpointer);
135 
136 int bch2_check_btree_backpointers(struct bch_fs *);
137 int bch2_check_extents_to_backpointers(struct bch_fs *);
138 int bch2_check_backpointers_to_extents(struct bch_fs *);
139 
140 #endif /* _BCACHEFS_BACKPOINTERS_BACKGROUND_H */
141