1 /* SPDX-License-Identifier: GPL-2.0 */ 1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef _BCACHEFS_BKEY_H 2 #ifndef _BCACHEFS_BKEY_H 3 #define _BCACHEFS_BKEY_H 3 #define _BCACHEFS_BKEY_H 4 4 5 #include <linux/bug.h> 5 #include <linux/bug.h> 6 #include "bcachefs_format.h" 6 #include "bcachefs_format.h" 7 #include "bkey_types.h" 7 #include "bkey_types.h" 8 #include "btree_types.h" 8 #include "btree_types.h" 9 #include "util.h" 9 #include "util.h" 10 #include "vstructs.h" 10 #include "vstructs.h" 11 11 12 enum bch_validate_flags { 12 enum bch_validate_flags { 13 BCH_VALIDATE_write = BIT( !! 13 BCH_VALIDATE_write = (1U << 0), 14 BCH_VALIDATE_commit = BIT( !! 14 BCH_VALIDATE_commit = (1U << 1), 15 BCH_VALIDATE_journal = BIT( !! 15 BCH_VALIDATE_journal = (1U << 2), 16 BCH_VALIDATE_silent = BIT( << 17 }; 16 }; 18 17 19 #if 0 18 #if 0 20 19 21 /* 20 /* 22 * compiled unpack functions are disabled, pen 21 * compiled unpack functions are disabled, pending a new interface for 23 * dynamically allocating executable memory: 22 * dynamically allocating executable memory: 24 */ 23 */ 25 24 26 #ifdef CONFIG_X86_64 25 #ifdef CONFIG_X86_64 27 #define HAVE_BCACHEFS_COMPILED_UNPACK 1 26 #define HAVE_BCACHEFS_COMPILED_UNPACK 1 28 #endif 27 #endif 29 #endif 28 #endif 30 29 31 void bch2_bkey_packed_to_binary_text(struct pr 30 void bch2_bkey_packed_to_binary_text(struct printbuf *, 32 const str 31 const struct bkey_format *, 33 const str 32 const struct bkey_packed *); 34 33 35 enum bkey_lr_packed { 34 enum bkey_lr_packed { 36 BKEY_PACKED_BOTH, 35 BKEY_PACKED_BOTH, 37 BKEY_PACKED_RIGHT, 36 BKEY_PACKED_RIGHT, 38 BKEY_PACKED_LEFT, 37 BKEY_PACKED_LEFT, 39 BKEY_PACKED_NONE, 38 BKEY_PACKED_NONE, 40 }; 39 }; 41 40 42 #define bkey_lr_packed(_l, _r) 41 #define bkey_lr_packed(_l, _r) \ 43 ((_l)->format + ((_r)->format << 1)) 42 ((_l)->format + ((_r)->format << 1)) 44 43 45 static inline void bkey_p_copy(struct bkey_pac 44 static inline void bkey_p_copy(struct bkey_packed *dst, const struct bkey_packed *src) 46 { 45 { 47 memcpy_u64s_small(dst, src, src->u64s) 46 memcpy_u64s_small(dst, src, src->u64s); 48 } 47 } 49 48 50 static inline void bkey_copy(struct bkey_i *ds 49 static inline void bkey_copy(struct bkey_i *dst, const struct bkey_i *src) 51 { 50 { 52 memcpy_u64s_small(dst, src, src->k.u64 51 memcpy_u64s_small(dst, src, src->k.u64s); 53 } 52 } 54 53 55 struct btree; 54 struct btree; 56 55 57 __pure 56 __pure 58 unsigned bch2_bkey_greatest_differing_bit(cons 57 unsigned bch2_bkey_greatest_differing_bit(const struct btree *, 59 cons 58 const struct bkey_packed *, 60 cons 59 const struct bkey_packed *); 61 __pure 60 __pure 62 unsigned bch2_bkey_ffs(const struct btree *, c 61 unsigned bch2_bkey_ffs(const struct btree *, const struct bkey_packed *); 63 62 64 __pure 63 __pure 65 int __bch2_bkey_cmp_packed_format_checked(cons 64 int __bch2_bkey_cmp_packed_format_checked(const struct bkey_packed *, 66 const str 65 const struct bkey_packed *, 67 const str 66 const struct btree *); 68 67 69 __pure 68 __pure 70 int __bch2_bkey_cmp_left_packed_format_checked 69 int __bch2_bkey_cmp_left_packed_format_checked(const struct btree *, 71 cons 70 const struct bkey_packed *, 72 cons 71 const struct bpos *); 73 72 74 __pure 73 __pure 75 int bch2_bkey_cmp_packed(const struct btree *, 74 int bch2_bkey_cmp_packed(const struct btree *, 76 const struct bkey_pac 75 const struct bkey_packed *, 77 const struct bkey_pac 76 const struct bkey_packed *); 78 77 79 __pure 78 __pure 80 int __bch2_bkey_cmp_left_packed(const struct b 79 int __bch2_bkey_cmp_left_packed(const struct btree *, 81 const struct b 80 const struct bkey_packed *, 82 const struct b 81 const struct bpos *); 83 82 84 static inline __pure 83 static inline __pure 85 int bkey_cmp_left_packed(const struct btree *b 84 int bkey_cmp_left_packed(const struct btree *b, 86 const struct bkey_pac 85 const struct bkey_packed *l, const struct bpos *r) 87 { 86 { 88 return __bch2_bkey_cmp_left_packed(b, 87 return __bch2_bkey_cmp_left_packed(b, l, r); 89 } 88 } 90 89 91 /* 90 /* 92 * The compiler generates better code when we 91 * The compiler generates better code when we pass bpos by ref, but it's often 93 * enough terribly convenient to pass it by va 92 * enough terribly convenient to pass it by val... as much as I hate c++, const 94 * ref would be nice here: 93 * ref would be nice here: 95 */ 94 */ 96 __pure __flatten 95 __pure __flatten 97 static inline int bkey_cmp_left_packed_byval(c 96 static inline int bkey_cmp_left_packed_byval(const struct btree *b, 98 c 97 const struct bkey_packed *l, 99 s 98 struct bpos r) 100 { 99 { 101 return bkey_cmp_left_packed(b, l, &r); 100 return bkey_cmp_left_packed(b, l, &r); 102 } 101 } 103 102 104 static __always_inline bool bpos_eq(struct bpo 103 static __always_inline bool bpos_eq(struct bpos l, struct bpos r) 105 { 104 { 106 return !((l.inode ^ r.inode) | 105 return !((l.inode ^ r.inode) | 107 (l.offset ^ r.offset) | 106 (l.offset ^ r.offset) | 108 (l.snapshot ^ r.snapshot)) 107 (l.snapshot ^ r.snapshot)); 109 } 108 } 110 109 111 static __always_inline bool bpos_lt(struct bpo 110 static __always_inline bool bpos_lt(struct bpos l, struct bpos r) 112 { 111 { 113 return l.inode != r.inode ? l.inode < 112 return l.inode != r.inode ? l.inode < r.inode : 114 l.offset != r.offset ? l.offse 113 l.offset != r.offset ? l.offset < r.offset : 115 l.snapshot != r.snapshot ? l.s 114 l.snapshot != r.snapshot ? l.snapshot < r.snapshot : false; 116 } 115 } 117 116 118 static __always_inline bool bpos_le(struct bpo 117 static __always_inline bool bpos_le(struct bpos l, struct bpos r) 119 { 118 { 120 return l.inode != r.inode ? l.inode < 119 return l.inode != r.inode ? l.inode < r.inode : 121 l.offset != r.offset ? l.offse 120 l.offset != r.offset ? l.offset < r.offset : 122 l.snapshot != r.snapshot ? l.s 121 l.snapshot != r.snapshot ? l.snapshot < r.snapshot : true; 123 } 122 } 124 123 125 static __always_inline bool bpos_gt(struct bpo 124 static __always_inline bool bpos_gt(struct bpos l, struct bpos r) 126 { 125 { 127 return bpos_lt(r, l); 126 return bpos_lt(r, l); 128 } 127 } 129 128 130 static __always_inline bool bpos_ge(struct bpo 129 static __always_inline bool bpos_ge(struct bpos l, struct bpos r) 131 { 130 { 132 return bpos_le(r, l); 131 return bpos_le(r, l); 133 } 132 } 134 133 135 static __always_inline int bpos_cmp(struct bpo 134 static __always_inline int bpos_cmp(struct bpos l, struct bpos r) 136 { 135 { 137 return cmp_int(l.inode, r.inode) ? 136 return cmp_int(l.inode, r.inode) ?: 138 cmp_int(l.offset, r.offset) 137 cmp_int(l.offset, r.offset) ?: 139 cmp_int(l.snapshot, r.snapshot 138 cmp_int(l.snapshot, r.snapshot); 140 } 139 } 141 140 142 static inline struct bpos bpos_min(struct bpos 141 static inline struct bpos bpos_min(struct bpos l, struct bpos r) 143 { 142 { 144 return bpos_lt(l, r) ? l : r; 143 return bpos_lt(l, r) ? l : r; 145 } 144 } 146 145 147 static inline struct bpos bpos_max(struct bpos 146 static inline struct bpos bpos_max(struct bpos l, struct bpos r) 148 { 147 { 149 return bpos_gt(l, r) ? l : r; 148 return bpos_gt(l, r) ? l : r; 150 } 149 } 151 150 152 static __always_inline bool bkey_eq(struct bpo 151 static __always_inline bool bkey_eq(struct bpos l, struct bpos r) 153 { 152 { 154 return !((l.inode ^ r.inode) | 153 return !((l.inode ^ r.inode) | 155 (l.offset ^ r.offset)); 154 (l.offset ^ r.offset)); 156 } 155 } 157 156 158 static __always_inline bool bkey_lt(struct bpo 157 static __always_inline bool bkey_lt(struct bpos l, struct bpos r) 159 { 158 { 160 return l.inode != r.inode 159 return l.inode != r.inode 161 ? l.inode < r.inode 160 ? l.inode < r.inode 162 : l.offset < r.offset; 161 : l.offset < r.offset; 163 } 162 } 164 163 165 static __always_inline bool bkey_le(struct bpo 164 static __always_inline bool bkey_le(struct bpos l, struct bpos r) 166 { 165 { 167 return l.inode != r.inode 166 return l.inode != r.inode 168 ? l.inode < r.inode 167 ? l.inode < r.inode 169 : l.offset <= r.offset; 168 : l.offset <= r.offset; 170 } 169 } 171 170 172 static __always_inline bool bkey_gt(struct bpo 171 static __always_inline bool bkey_gt(struct bpos l, struct bpos r) 173 { 172 { 174 return bkey_lt(r, l); 173 return bkey_lt(r, l); 175 } 174 } 176 175 177 static __always_inline bool bkey_ge(struct bpo 176 static __always_inline bool bkey_ge(struct bpos l, struct bpos r) 178 { 177 { 179 return bkey_le(r, l); 178 return bkey_le(r, l); 180 } 179 } 181 180 182 static __always_inline int bkey_cmp(struct bpo 181 static __always_inline int bkey_cmp(struct bpos l, struct bpos r) 183 { 182 { 184 return cmp_int(l.inode, r.inode) ? 183 return cmp_int(l.inode, r.inode) ?: 185 cmp_int(l.offset, r.offset); 184 cmp_int(l.offset, r.offset); 186 } 185 } 187 186 188 static inline struct bpos bkey_min(struct bpos 187 static inline struct bpos bkey_min(struct bpos l, struct bpos r) 189 { 188 { 190 return bkey_lt(l, r) ? l : r; 189 return bkey_lt(l, r) ? l : r; 191 } 190 } 192 191 193 static inline struct bpos bkey_max(struct bpos 192 static inline struct bpos bkey_max(struct bpos l, struct bpos r) 194 { 193 { 195 return bkey_gt(l, r) ? l : r; 194 return bkey_gt(l, r) ? l : r; 196 } 195 } 197 196 198 static inline bool bkey_and_val_eq(struct bkey 197 static inline bool bkey_and_val_eq(struct bkey_s_c l, struct bkey_s_c r) 199 { 198 { 200 return bpos_eq(l.k->p, r.k->p) && 199 return bpos_eq(l.k->p, r.k->p) && 201 bkey_bytes(l.k) == bkey_bytes( 200 bkey_bytes(l.k) == bkey_bytes(r.k) && 202 !memcmp(l.v, r.v, bkey_val_byt 201 !memcmp(l.v, r.v, bkey_val_bytes(l.k)); 203 } 202 } 204 203 205 void bch2_bpos_swab(struct bpos *); 204 void bch2_bpos_swab(struct bpos *); 206 void bch2_bkey_swab_key(const struct bkey_form 205 void bch2_bkey_swab_key(const struct bkey_format *, struct bkey_packed *); 207 206 208 static __always_inline int bversion_cmp(struct 207 static __always_inline int bversion_cmp(struct bversion l, struct bversion r) 209 { 208 { 210 return cmp_int(l.hi, r.hi) ?: 209 return cmp_int(l.hi, r.hi) ?: 211 cmp_int(l.lo, r.lo); 210 cmp_int(l.lo, r.lo); 212 } 211 } 213 212 214 #define ZERO_VERSION ((struct bversion) { . 213 #define ZERO_VERSION ((struct bversion) { .hi = 0, .lo = 0 }) 215 #define MAX_VERSION ((struct bversion) { . 214 #define MAX_VERSION ((struct bversion) { .hi = ~0, .lo = ~0ULL }) 216 215 217 static __always_inline bool bversion_zero(stru !! 216 static __always_inline int bversion_zero(struct bversion v) 218 { 217 { 219 return bversion_cmp(v, ZERO_VERSION) = !! 218 return !bversion_cmp(v, ZERO_VERSION); 220 } 219 } 221 220 222 #ifdef CONFIG_BCACHEFS_DEBUG 221 #ifdef CONFIG_BCACHEFS_DEBUG 223 /* statement expressions confusing unlikely()? 222 /* statement expressions confusing unlikely()? */ 224 #define bkey_packed(_k) 223 #define bkey_packed(_k) \ 225 ({ EBUG_ON((_k)->format > KEY_FORMAT_C 224 ({ EBUG_ON((_k)->format > KEY_FORMAT_CURRENT); \ 226 (_k)->format != KEY_FORMAT_CURRENT; } 225 (_k)->format != KEY_FORMAT_CURRENT; }) 227 #else 226 #else 228 #define bkey_packed(_k) ((_k)->format 227 #define bkey_packed(_k) ((_k)->format != KEY_FORMAT_CURRENT) 229 #endif 228 #endif 230 229 231 /* 230 /* 232 * It's safe to treat an unpacked bkey as a pa 231 * It's safe to treat an unpacked bkey as a packed one, but not the reverse 233 */ 232 */ 234 static inline struct bkey_packed *bkey_to_pack 233 static inline struct bkey_packed *bkey_to_packed(struct bkey_i *k) 235 { 234 { 236 return (struct bkey_packed *) k; 235 return (struct bkey_packed *) k; 237 } 236 } 238 237 239 static inline const struct bkey_packed *bkey_t 238 static inline const struct bkey_packed *bkey_to_packed_c(const struct bkey_i *k) 240 { 239 { 241 return (const struct bkey_packed *) k; 240 return (const struct bkey_packed *) k; 242 } 241 } 243 242 244 static inline struct bkey_i *packed_to_bkey(st 243 static inline struct bkey_i *packed_to_bkey(struct bkey_packed *k) 245 { 244 { 246 return bkey_packed(k) ? NULL : (struct 245 return bkey_packed(k) ? NULL : (struct bkey_i *) k; 247 } 246 } 248 247 249 static inline const struct bkey *packed_to_bke 248 static inline const struct bkey *packed_to_bkey_c(const struct bkey_packed *k) 250 { 249 { 251 return bkey_packed(k) ? NULL : (const 250 return bkey_packed(k) ? NULL : (const struct bkey *) k; 252 } 251 } 253 252 254 static inline unsigned bkey_format_key_bits(co 253 static inline unsigned bkey_format_key_bits(const struct bkey_format *format) 255 { 254 { 256 return format->bits_per_field[BKEY_FIE 255 return format->bits_per_field[BKEY_FIELD_INODE] + 257 format->bits_per_field[BKEY_FI 256 format->bits_per_field[BKEY_FIELD_OFFSET] + 258 format->bits_per_field[BKEY_FI 257 format->bits_per_field[BKEY_FIELD_SNAPSHOT]; 259 } 258 } 260 259 261 static inline struct bpos bpos_successor(struc 260 static inline struct bpos bpos_successor(struct bpos p) 262 { 261 { 263 if (!++p.snapshot && 262 if (!++p.snapshot && 264 !++p.offset && 263 !++p.offset && 265 !++p.inode) 264 !++p.inode) 266 BUG(); 265 BUG(); 267 266 268 return p; 267 return p; 269 } 268 } 270 269 271 static inline struct bpos bpos_predecessor(str 270 static inline struct bpos bpos_predecessor(struct bpos p) 272 { 271 { 273 if (!p.snapshot-- && 272 if (!p.snapshot-- && 274 !p.offset-- && 273 !p.offset-- && 275 !p.inode--) 274 !p.inode--) 276 BUG(); 275 BUG(); 277 276 278 return p; 277 return p; 279 } 278 } 280 279 281 static inline struct bpos bpos_nosnap_successo 280 static inline struct bpos bpos_nosnap_successor(struct bpos p) 282 { 281 { 283 p.snapshot = 0; 282 p.snapshot = 0; 284 283 285 if (!++p.offset && 284 if (!++p.offset && 286 !++p.inode) 285 !++p.inode) 287 BUG(); 286 BUG(); 288 287 289 return p; 288 return p; 290 } 289 } 291 290 292 static inline struct bpos bpos_nosnap_predeces 291 static inline struct bpos bpos_nosnap_predecessor(struct bpos p) 293 { 292 { 294 p.snapshot = 0; 293 p.snapshot = 0; 295 294 296 if (!p.offset-- && 295 if (!p.offset-- && 297 !p.inode--) 296 !p.inode--) 298 BUG(); 297 BUG(); 299 298 300 return p; 299 return p; 301 } 300 } 302 301 303 static inline u64 bkey_start_offset(const stru 302 static inline u64 bkey_start_offset(const struct bkey *k) 304 { 303 { 305 return k->p.offset - k->size; 304 return k->p.offset - k->size; 306 } 305 } 307 306 308 static inline struct bpos bkey_start_pos(const 307 static inline struct bpos bkey_start_pos(const struct bkey *k) 309 { 308 { 310 return (struct bpos) { 309 return (struct bpos) { 311 .inode = k->p.inode, 310 .inode = k->p.inode, 312 .offset = bkey_start_o 311 .offset = bkey_start_offset(k), 313 .snapshot = k->p.snapsho 312 .snapshot = k->p.snapshot, 314 }; 313 }; 315 } 314 } 316 315 317 /* Packed helpers */ 316 /* Packed helpers */ 318 317 319 static inline unsigned bkeyp_key_u64s(const st 318 static inline unsigned bkeyp_key_u64s(const struct bkey_format *format, 320 const st 319 const struct bkey_packed *k) 321 { 320 { 322 return bkey_packed(k) ? format->key_u6 321 return bkey_packed(k) ? format->key_u64s : BKEY_U64s; 323 } 322 } 324 323 325 static inline bool bkeyp_u64s_valid(const stru 324 static inline bool bkeyp_u64s_valid(const struct bkey_format *f, 326 const stru 325 const struct bkey_packed *k) 327 { 326 { 328 return ((unsigned) k->u64s - bkeyp_key 327 return ((unsigned) k->u64s - bkeyp_key_u64s(f, k) <= U8_MAX - BKEY_U64s); 329 } 328 } 330 329 331 static inline unsigned bkeyp_key_bytes(const s 330 static inline unsigned bkeyp_key_bytes(const struct bkey_format *format, 332 const s 331 const struct bkey_packed *k) 333 { 332 { 334 return bkeyp_key_u64s(format, k) * siz 333 return bkeyp_key_u64s(format, k) * sizeof(u64); 335 } 334 } 336 335 337 static inline unsigned bkeyp_val_u64s(const st 336 static inline unsigned bkeyp_val_u64s(const struct bkey_format *format, 338 const st 337 const struct bkey_packed *k) 339 { 338 { 340 return k->u64s - bkeyp_key_u64s(format 339 return k->u64s - bkeyp_key_u64s(format, k); 341 } 340 } 342 341 343 static inline size_t bkeyp_val_bytes(const str 342 static inline size_t bkeyp_val_bytes(const struct bkey_format *format, 344 const str 343 const struct bkey_packed *k) 345 { 344 { 346 return bkeyp_val_u64s(format, k) * siz 345 return bkeyp_val_u64s(format, k) * sizeof(u64); 347 } 346 } 348 347 349 static inline void set_bkeyp_val_u64s(const st 348 static inline void set_bkeyp_val_u64s(const struct bkey_format *format, 350 struct b 349 struct bkey_packed *k, unsigned val_u64s) 351 { 350 { 352 k->u64s = bkeyp_key_u64s(format, k) + 351 k->u64s = bkeyp_key_u64s(format, k) + val_u64s; 353 } 352 } 354 353 355 #define bkeyp_val(_format, _k) 354 #define bkeyp_val(_format, _k) \ 356 ((struct bch_val *) ((u64 *) (_k)->_d 355 ((struct bch_val *) ((u64 *) (_k)->_data + bkeyp_key_u64s(_format, _k))) 357 356 358 extern const struct bkey_format bch2_bkey_form 357 extern const struct bkey_format bch2_bkey_format_current; 359 358 360 bool bch2_bkey_transform(const struct bkey_for 359 bool bch2_bkey_transform(const struct bkey_format *, 361 struct bkey_packed *, 360 struct bkey_packed *, 362 const struct bkey_for 361 const struct bkey_format *, 363 const struct bkey_pac 362 const struct bkey_packed *); 364 363 365 struct bkey __bch2_bkey_unpack_key(const struc 364 struct bkey __bch2_bkey_unpack_key(const struct bkey_format *, 366 const struc 365 const struct bkey_packed *); 367 366 368 #ifndef HAVE_BCACHEFS_COMPILED_UNPACK 367 #ifndef HAVE_BCACHEFS_COMPILED_UNPACK 369 struct bpos __bkey_unpack_pos(const struct bke 368 struct bpos __bkey_unpack_pos(const struct bkey_format *, 370 const struct bke 369 const struct bkey_packed *); 371 #endif 370 #endif 372 371 373 bool bch2_bkey_pack_key(struct bkey_packed *, 372 bool bch2_bkey_pack_key(struct bkey_packed *, const struct bkey *, 374 const struct bkey_format *) 373 const struct bkey_format *); 375 374 376 enum bkey_pack_pos_ret { 375 enum bkey_pack_pos_ret { 377 BKEY_PACK_POS_EXACT, 376 BKEY_PACK_POS_EXACT, 378 BKEY_PACK_POS_SMALLER, 377 BKEY_PACK_POS_SMALLER, 379 BKEY_PACK_POS_FAIL, 378 BKEY_PACK_POS_FAIL, 380 }; 379 }; 381 380 382 enum bkey_pack_pos_ret bch2_bkey_pack_pos_loss 381 enum bkey_pack_pos_ret bch2_bkey_pack_pos_lossy(struct bkey_packed *, struct bpos, 383 con 382 const struct btree *); 384 383 385 static inline bool bkey_pack_pos(struct bkey_p 384 static inline bool bkey_pack_pos(struct bkey_packed *out, struct bpos in, 386 const struct 385 const struct btree *b) 387 { 386 { 388 return bch2_bkey_pack_pos_lossy(out, i 387 return bch2_bkey_pack_pos_lossy(out, in, b) == BKEY_PACK_POS_EXACT; 389 } 388 } 390 389 391 void bch2_bkey_unpack(const struct btree *, st 390 void bch2_bkey_unpack(const struct btree *, struct bkey_i *, 392 const struct bkey_packed *); 391 const struct bkey_packed *); 393 bool bch2_bkey_pack(struct bkey_packed *, cons 392 bool bch2_bkey_pack(struct bkey_packed *, const struct bkey_i *, 394 const struct bkey_format *); 393 const struct bkey_format *); 395 394 396 typedef void (*compiled_unpack_fn)(struct bkey 395 typedef void (*compiled_unpack_fn)(struct bkey *, const struct bkey_packed *); 397 396 398 static inline void 397 static inline void 399 __bkey_unpack_key_format_checked(const struct 398 __bkey_unpack_key_format_checked(const struct btree *b, 400 struct bkey *ds 399 struct bkey *dst, 401 const struct bk 400 const struct bkey_packed *src) 402 { 401 { 403 if (IS_ENABLED(HAVE_BCACHEFS_COMPILED_ 402 if (IS_ENABLED(HAVE_BCACHEFS_COMPILED_UNPACK)) { 404 compiled_unpack_fn unpack_fn = 403 compiled_unpack_fn unpack_fn = b->aux_data; 405 unpack_fn(dst, src); 404 unpack_fn(dst, src); 406 405 407 if (IS_ENABLED(CONFIG_BCACHEFS 406 if (IS_ENABLED(CONFIG_BCACHEFS_DEBUG) && 408 bch2_expensive_debug_check 407 bch2_expensive_debug_checks) { 409 struct bkey dst2 = __b 408 struct bkey dst2 = __bch2_bkey_unpack_key(&b->format, src); 410 409 411 BUG_ON(memcmp(dst, &ds 410 BUG_ON(memcmp(dst, &dst2, sizeof(*dst))); 412 } 411 } 413 } else { 412 } else { 414 *dst = __bch2_bkey_unpack_key( 413 *dst = __bch2_bkey_unpack_key(&b->format, src); 415 } 414 } 416 } 415 } 417 416 418 static inline struct bkey 417 static inline struct bkey 419 bkey_unpack_key_format_checked(const struct bt 418 bkey_unpack_key_format_checked(const struct btree *b, 420 const struct bk 419 const struct bkey_packed *src) 421 { 420 { 422 struct bkey dst; 421 struct bkey dst; 423 422 424 __bkey_unpack_key_format_checked(b, &d 423 __bkey_unpack_key_format_checked(b, &dst, src); 425 return dst; 424 return dst; 426 } 425 } 427 426 428 static inline void __bkey_unpack_key(const str 427 static inline void __bkey_unpack_key(const struct btree *b, 429 struct bk 428 struct bkey *dst, 430 const str 429 const struct bkey_packed *src) 431 { 430 { 432 if (likely(bkey_packed(src))) 431 if (likely(bkey_packed(src))) 433 __bkey_unpack_key_format_check 432 __bkey_unpack_key_format_checked(b, dst, src); 434 else 433 else 435 *dst = *packed_to_bkey_c(src); 434 *dst = *packed_to_bkey_c(src); 436 } 435 } 437 436 438 /** 437 /** 439 * bkey_unpack_key -- unpack just the key, not 438 * bkey_unpack_key -- unpack just the key, not the value 440 */ 439 */ 441 static inline struct bkey bkey_unpack_key(cons 440 static inline struct bkey bkey_unpack_key(const struct btree *b, 442 cons 441 const struct bkey_packed *src) 443 { 442 { 444 return likely(bkey_packed(src)) 443 return likely(bkey_packed(src)) 445 ? bkey_unpack_key_format_check 444 ? bkey_unpack_key_format_checked(b, src) 446 : *packed_to_bkey_c(src); 445 : *packed_to_bkey_c(src); 447 } 446 } 448 447 449 static inline struct bpos 448 static inline struct bpos 450 bkey_unpack_pos_format_checked(const struct bt 449 bkey_unpack_pos_format_checked(const struct btree *b, 451 const struct bk 450 const struct bkey_packed *src) 452 { 451 { 453 #ifdef HAVE_BCACHEFS_COMPILED_UNPACK 452 #ifdef HAVE_BCACHEFS_COMPILED_UNPACK 454 return bkey_unpack_key_format_checked( 453 return bkey_unpack_key_format_checked(b, src).p; 455 #else 454 #else 456 return __bkey_unpack_pos(&b->format, s 455 return __bkey_unpack_pos(&b->format, src); 457 #endif 456 #endif 458 } 457 } 459 458 460 static inline struct bpos bkey_unpack_pos(cons 459 static inline struct bpos bkey_unpack_pos(const struct btree *b, 461 cons 460 const struct bkey_packed *src) 462 { 461 { 463 return likely(bkey_packed(src)) 462 return likely(bkey_packed(src)) 464 ? bkey_unpack_pos_format_check 463 ? bkey_unpack_pos_format_checked(b, src) 465 : packed_to_bkey_c(src)->p; 464 : packed_to_bkey_c(src)->p; 466 } 465 } 467 466 468 /* Disassembled bkeys */ 467 /* Disassembled bkeys */ 469 468 470 static inline struct bkey_s_c bkey_disassemble 469 static inline struct bkey_s_c bkey_disassemble(const struct btree *b, 471 470 const struct bkey_packed *k, 472 471 struct bkey *u) 473 { 472 { 474 __bkey_unpack_key(b, u, k); 473 __bkey_unpack_key(b, u, k); 475 474 476 return (struct bkey_s_c) { u, bkeyp_va 475 return (struct bkey_s_c) { u, bkeyp_val(&b->format, k), }; 477 } 476 } 478 477 479 /* non const version: */ 478 /* non const version: */ 480 static inline struct bkey_s __bkey_disassemble 479 static inline struct bkey_s __bkey_disassemble(const struct btree *b, 481 480 struct bkey_packed *k, 482 481 struct bkey *u) 483 { 482 { 484 __bkey_unpack_key(b, u, k); 483 __bkey_unpack_key(b, u, k); 485 484 486 return (struct bkey_s) { .k = u, .v = 485 return (struct bkey_s) { .k = u, .v = bkeyp_val(&b->format, k), }; 487 } 486 } 488 487 489 static inline u64 bkey_field_max(const struct 488 static inline u64 bkey_field_max(const struct bkey_format *f, 490 enum bch_bkey 489 enum bch_bkey_fields nr) 491 { 490 { 492 return f->bits_per_field[nr] < 64 491 return f->bits_per_field[nr] < 64 493 ? (le64_to_cpu(f->field_offset 492 ? (le64_to_cpu(f->field_offset[nr]) + 494 ~(~0ULL << f->bits_per_fiel 493 ~(~0ULL << f->bits_per_field[nr])) 495 : U64_MAX; 494 : U64_MAX; 496 } 495 } 497 496 498 #ifdef HAVE_BCACHEFS_COMPILED_UNPACK 497 #ifdef HAVE_BCACHEFS_COMPILED_UNPACK 499 498 500 int bch2_compile_bkey_format(const struct bkey 499 int bch2_compile_bkey_format(const struct bkey_format *, void *); 501 500 502 #else 501 #else 503 502 504 static inline int bch2_compile_bkey_format(con 503 static inline int bch2_compile_bkey_format(const struct bkey_format *format, 505 void 504 void *out) { return 0; } 506 505 507 #endif 506 #endif 508 507 509 static inline void bkey_reassemble(struct bkey 508 static inline void bkey_reassemble(struct bkey_i *dst, 510 struct bkey 509 struct bkey_s_c src) 511 { 510 { 512 dst->k = *src.k; 511 dst->k = *src.k; 513 memcpy_u64s_small(&dst->v, src.v, bkey 512 memcpy_u64s_small(&dst->v, src.v, bkey_val_u64s(src.k)); 514 } 513 } 515 514 516 /* byte order helpers */ 515 /* byte order helpers */ 517 516 518 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ 517 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ 519 518 520 static inline unsigned high_word_offset(const 519 static inline unsigned high_word_offset(const struct bkey_format *f) 521 { 520 { 522 return f->key_u64s - 1; 521 return f->key_u64s - 1; 523 } 522 } 524 523 525 #define high_bit_offset 0 524 #define high_bit_offset 0 526 #define nth_word(p, n) ((p) - (n)) 525 #define nth_word(p, n) ((p) - (n)) 527 526 528 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ 527 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ 529 528 530 static inline unsigned high_word_offset(const 529 static inline unsigned high_word_offset(const struct bkey_format *f) 531 { 530 { 532 return 0; 531 return 0; 533 } 532 } 534 533 535 #define high_bit_offset KEY_PACKED_BIT 534 #define high_bit_offset KEY_PACKED_BITS_START 536 #define nth_word(p, n) ((p) + (n)) 535 #define nth_word(p, n) ((p) + (n)) 537 536 538 #else 537 #else 539 #error edit for your odd byteorder. 538 #error edit for your odd byteorder. 540 #endif 539 #endif 541 540 542 #define high_word(f, k) ((u64 *) (k)-> 541 #define high_word(f, k) ((u64 *) (k)->_data + high_word_offset(f)) 543 #define next_word(p) nth_word(p, 1) 542 #define next_word(p) nth_word(p, 1) 544 #define prev_word(p) nth_word(p, -1 543 #define prev_word(p) nth_word(p, -1) 545 544 546 #ifdef CONFIG_BCACHEFS_DEBUG 545 #ifdef CONFIG_BCACHEFS_DEBUG 547 void bch2_bkey_pack_test(void); 546 void bch2_bkey_pack_test(void); 548 #else 547 #else 549 static inline void bch2_bkey_pack_test(void) { 548 static inline void bch2_bkey_pack_test(void) {} 550 #endif 549 #endif 551 550 552 #define bkey_fields() 551 #define bkey_fields() \ 553 x(BKEY_FIELD_INODE, p.inod 552 x(BKEY_FIELD_INODE, p.inode) \ 554 x(BKEY_FIELD_OFFSET, p.offs 553 x(BKEY_FIELD_OFFSET, p.offset) \ 555 x(BKEY_FIELD_SNAPSHOT, p.snap 554 x(BKEY_FIELD_SNAPSHOT, p.snapshot) \ 556 x(BKEY_FIELD_SIZE, size) 555 x(BKEY_FIELD_SIZE, size) \ 557 x(BKEY_FIELD_VERSION_HI, bversi !! 556 x(BKEY_FIELD_VERSION_HI, version.hi) \ 558 x(BKEY_FIELD_VERSION_LO, bversi !! 557 x(BKEY_FIELD_VERSION_LO, version.lo) 559 558 560 struct bkey_format_state { 559 struct bkey_format_state { 561 u64 field_min[BKEY_NR_FIELDS]; 560 u64 field_min[BKEY_NR_FIELDS]; 562 u64 field_max[BKEY_NR_FIELDS]; 561 u64 field_max[BKEY_NR_FIELDS]; 563 }; 562 }; 564 563 565 void bch2_bkey_format_init(struct bkey_format_ 564 void bch2_bkey_format_init(struct bkey_format_state *); 566 565 567 static inline void __bkey_format_add(struct bk 566 static inline void __bkey_format_add(struct bkey_format_state *s, unsigned field, u64 v) 568 { 567 { 569 s->field_min[field] = min(s->field_min 568 s->field_min[field] = min(s->field_min[field], v); 570 s->field_max[field] = max(s->field_max 569 s->field_max[field] = max(s->field_max[field], v); 571 } 570 } 572 571 573 /* 572 /* 574 * Changes @format so that @k can be successfu 573 * Changes @format so that @k can be successfully packed with @format 575 */ 574 */ 576 static inline void bch2_bkey_format_add_key(st 575 static inline void bch2_bkey_format_add_key(struct bkey_format_state *s, const struct bkey *k) 577 { 576 { 578 #define x(id, field) __bkey_format_add(s, id, 577 #define x(id, field) __bkey_format_add(s, id, k->field); 579 bkey_fields() 578 bkey_fields() 580 #undef x 579 #undef x 581 } 580 } 582 581 583 void bch2_bkey_format_add_pos(struct bkey_form 582 void bch2_bkey_format_add_pos(struct bkey_format_state *, struct bpos); 584 struct bkey_format bch2_bkey_format_done(struc 583 struct bkey_format bch2_bkey_format_done(struct bkey_format_state *); 585 584 586 static inline bool bch2_bkey_format_field_over 585 static inline bool bch2_bkey_format_field_overflows(struct bkey_format *f, unsigned i) 587 { 586 { 588 unsigned f_bits = f->bits_per_field[i] 587 unsigned f_bits = f->bits_per_field[i]; 589 unsigned unpacked_bits = bch2_bkey_for 588 unsigned unpacked_bits = bch2_bkey_format_current.bits_per_field[i]; 590 u64 unpacked_mask = ~((~0ULL << 1) << 589 u64 unpacked_mask = ~((~0ULL << 1) << (unpacked_bits - 1)); 591 u64 field_offset = le64_to_cpu(f->fiel 590 u64 field_offset = le64_to_cpu(f->field_offset[i]); 592 591 593 if (f_bits > unpacked_bits) 592 if (f_bits > unpacked_bits) 594 return true; 593 return true; 595 594 596 if ((f_bits == unpacked_bits) && field 595 if ((f_bits == unpacked_bits) && field_offset) 597 return true; 596 return true; 598 597 599 u64 f_mask = f_bits 598 u64 f_mask = f_bits 600 ? ~((~0ULL << (f_bits - 1)) << 599 ? ~((~0ULL << (f_bits - 1)) << 1) 601 : 0; 600 : 0; 602 601 603 if (((field_offset + f_mask) & unpacke 602 if (((field_offset + f_mask) & unpacked_mask) < field_offset) 604 return true; 603 return true; 605 return false; 604 return false; 606 } 605 } 607 606 608 int bch2_bkey_format_invalid(struct bch_fs *, 607 int bch2_bkey_format_invalid(struct bch_fs *, struct bkey_format *, 609 enum bch_validate 608 enum bch_validate_flags, struct printbuf *); 610 void bch2_bkey_format_to_text(struct printbuf 609 void bch2_bkey_format_to_text(struct printbuf *, const struct bkey_format *); 611 610 612 #endif /* _BCACHEFS_BKEY_H */ 611 #endif /* _BCACHEFS_BKEY_H */ 613 612
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.