1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef _BCACHEFS_BACKPOINTERS_BACKGROUND_H 3 #define _BCACHEFS_BACKPOINTERS_BACKGROUND_H 4 5 #include "btree_cache.h" 6 #include "btree_iter.h" 7 #include "btree_update.h" 8 #include "buckets.h" 9 #include "error.h" 10 #include "super.h" 11 12 static inline u64 swab40(u64 x) 13 { 14 return (((x & 0x00000000ffULL) << 32)| 15 ((x & 0x000000ff00ULL) << 16)| 16 ((x & 0x0000ff0000ULL) >> 0)| 17 ((x & 0x00ff000000ULL) >> 16)| 18 ((x & 0xff00000000ULL) >> 32)); 19 } 20 21 int bch2_backpointer_validate(struct bch_fs *, struct bkey_s_c k, 22 struct bkey_validate_context); 23 void bch2_backpointer_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c); 24 void bch2_backpointer_swab(struct bkey_s); 25 26 #define bch2_bkey_ops_backpointer ((struct bkey_ops) { \ 27 .key_validate = bch2_backpointer_validate, \ 28 .val_to_text = bch2_backpointer_to_text, \ 29 .swab = bch2_backpointer_swab, \ 30 .min_val_size = 32, \ 31 }) 32 33 #define MAX_EXTENT_COMPRESS_RATIO_SHIFT 10 34 35 /* 36 * Convert from pos in backpointer btree to pos of corresponding bucket in alloc 37 * btree: 38 */ 39 static inline struct bpos bp_pos_to_bucket(const struct bch_dev *ca, struct bpos bp_pos) 40 { 41 u64 bucket_sector = bp_pos.offset >> MAX_EXTENT_COMPRESS_RATIO_SHIFT; 42 43 return POS(bp_pos.inode, sector_to_bucket(ca, bucket_sector)); 44 } 45 46 static inline struct bpos bp_pos_to_bucket_and_offset(const struct bch_dev *ca, struct bpos bp_pos, 47 u32 *bucket_offset) 48 { 49 u64 bucket_sector = bp_pos.offset >> MAX_EXTENT_COMPRESS_RATIO_SHIFT; 50 51 return POS(bp_pos.inode, sector_to_bucket_and_offset(ca, bucket_sector, bucket_offset)); 52 } 53 54 static inline bool bp_pos_to_bucket_nodev_noerror(struct bch_fs *c, struct bpos bp_pos, struct bpos *bucket) 55 { 56 rcu_read_lock(); 57 struct bch_dev *ca = bch2_dev_rcu_noerror(c, bp_pos.inode); 58 if (ca) 59 *bucket = bp_pos_to_bucket(ca, bp_pos); 60 rcu_read_unlock(); 61 return ca != NULL; 62 } 63 64 static inline struct bpos bucket_pos_to_bp_noerror(const struct bch_dev *ca, 65 struct bpos bucket, 66 u64 bucket_offset) 67 { 68 return POS(bucket.inode, 69 (bucket_to_sector(ca, bucket.offset) << 70 MAX_EXTENT_COMPRESS_RATIO_SHIFT) + bucket_offset); 71 } 72 73 /* 74 * Convert from pos in alloc btree + bucket offset to pos in backpointer btree: 75 */ 76 static inline struct bpos bucket_pos_to_bp(const struct bch_dev *ca, 77 struct bpos bucket, 78 u64 bucket_offset) 79 { 80 struct bpos ret = bucket_pos_to_bp_noerror(ca, bucket, bucket_offset); 81 EBUG_ON(!bkey_eq(bucket, bp_pos_to_bucket(ca, ret))); 82 return ret; 83 } 84 85 static inline struct bpos bucket_pos_to_bp_start(const struct bch_dev *ca, struct bpos bucket) 86 { 87 return bucket_pos_to_bp(ca, bucket, 0); 88 } 89 90 static inline struct bpos bucket_pos_to_bp_end(const struct bch_dev *ca, struct bpos bucket) 91 { 92 return bpos_nosnap_predecessor(bucket_pos_to_bp(ca, bpos_nosnap_successor(bucket), 0)); 93 } 94 95 int bch2_bucket_backpointer_mod_nowritebuffer(struct btree_trans *, 96 struct bkey_s_c, 97 struct bkey_i_backpointer *, 98 bool); 99 100 static inline int bch2_bucket_backpointer_mod(struct btree_trans *trans, 101 struct bkey_s_c orig_k, 102 struct bkey_i_backpointer *bp, 103 bool insert) 104 { 105 if (unlikely(bch2_backpointers_no_use_write_buffer)) 106 return bch2_bucket_backpointer_mod_nowritebuffer(trans, orig_k, bp, insert); 107 108 if (!insert) { 109 bp->k.type = KEY_TYPE_deleted; 110 set_bkey_val_u64s(&bp->k, 0); 111 } 112 113 return bch2_trans_update_buffered(trans, BTREE_ID_backpointers, &bp->k_i); 114 } 115 116 static inline enum bch_data_type bch2_bkey_ptr_data_type(struct bkey_s_c k, 117 struct extent_ptr_decoded p, 118 const union bch_extent_entry *entry) 119 { 120 switch (k.k->type) { 121 case KEY_TYPE_btree_ptr: 122 case KEY_TYPE_btree_ptr_v2: 123 return BCH_DATA_btree; 124 case KEY_TYPE_extent: 125 case KEY_TYPE_reflink_v: 126 return p.has_ec ? BCH_DATA_stripe : BCH_DATA_user; 127 case KEY_TYPE_stripe: { 128 const struct bch_extent_ptr *ptr = &entry->ptr; 129 struct bkey_s_c_stripe s = bkey_s_c_to_stripe(k); 130 131 BUG_ON(ptr < s.v->ptrs || 132 ptr >= s.v->ptrs + s.v->nr_blocks); 133 134 return ptr >= s.v->ptrs + s.v->nr_blocks - s.v->nr_redundant 135 ? BCH_DATA_parity 136 : BCH_DATA_user; 137 } 138 default: 139 BUG(); 140 } 141 } 142 143 static inline void bch2_extent_ptr_to_bp(struct bch_fs *c, 144 enum btree_id btree_id, unsigned level, 145 struct bkey_s_c k, struct extent_ptr_decoded p, 146 const union bch_extent_entry *entry, 147 struct bkey_i_backpointer *bp) 148 { 149 bkey_backpointer_init(&bp->k_i); 150 bp->k.p = POS(p.ptr.dev, ((u64) p.ptr.offset << MAX_EXTENT_COMPRESS_RATIO_SHIFT) + p.crc.offset); 151 bp->v = (struct bch_backpointer) { 152 .btree_id = btree_id, 153 .level = level, 154 .data_type = bch2_bkey_ptr_data_type(k, p, entry), 155 .bucket_gen = p.ptr.gen, 156 .bucket_len = ptr_disk_sectors(level ? btree_sectors(c) : k.k->size, p), 157 .pos = k.k->p, 158 }; 159 } 160 161 struct bkey_buf; 162 struct bkey_s_c bch2_backpointer_get_key(struct btree_trans *, struct bkey_s_c_backpointer, 163 struct btree_iter *, unsigned, struct bkey_buf *); 164 struct btree *bch2_backpointer_get_node(struct btree_trans *, struct bkey_s_c_backpointer, 165 struct btree_iter *, struct bkey_buf *); 166 167 int bch2_check_btree_backpointers(struct bch_fs *); 168 int bch2_check_extents_to_backpointers(struct bch_fs *); 169 int bch2_check_backpointers_to_extents(struct bch_fs *); 170 171 #endif /* _BCACHEFS_BACKPOINTERS_BACKGROUND_H */ 172