2022-03-18 08:51:27 +08:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0 */
|
|
|
|
#ifndef _BCACHEFS_BACKPOINTERS_BACKGROUND_H
|
|
|
|
#define _BCACHEFS_BACKPOINTERS_BACKGROUND_H
|
|
|
|
|
2024-01-17 02:29:59 +08:00
|
|
|
#include "btree_cache.h"
|
2022-03-18 08:51:27 +08:00
|
|
|
#include "btree_iter.h"
|
|
|
|
#include "btree_update.h"
|
|
|
|
#include "buckets.h"
|
2024-05-01 04:50:28 +08:00
|
|
|
#include "error.h"
|
2022-03-18 08:51:27 +08:00
|
|
|
#include "super.h"
|
|
|
|
|
2023-11-03 21:09:38 +08:00
|
|
|
static inline u64 swab40(u64 x)
|
|
|
|
{
|
|
|
|
return (((x & 0x00000000ffULL) << 32)|
|
|
|
|
((x & 0x000000ff00ULL) << 16)|
|
|
|
|
((x & 0x0000ff0000ULL) >> 0)|
|
|
|
|
((x & 0x00ff000000ULL) >> 16)|
|
|
|
|
((x & 0xff00000000ULL) >> 32));
|
|
|
|
}
|
|
|
|
|
2023-10-25 08:44:36 +08:00
|
|
|
int bch2_backpointer_invalid(struct bch_fs *, struct bkey_s_c k,
|
2024-05-09 06:40:42 +08:00
|
|
|
enum bch_validate_flags, struct printbuf *);
|
2022-03-18 08:51:27 +08:00
|
|
|
void bch2_backpointer_to_text(struct printbuf *, const struct bch_backpointer *);
|
|
|
|
void bch2_backpointer_k_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
|
|
|
|
void bch2_backpointer_swab(struct bkey_s);
|
|
|
|
|
|
|
|
#define bch2_bkey_ops_backpointer ((struct bkey_ops) { \
|
|
|
|
.key_invalid = bch2_backpointer_invalid, \
|
|
|
|
.val_to_text = bch2_backpointer_k_to_text, \
|
|
|
|
.swab = bch2_backpointer_swab, \
|
2023-04-30 01:24:18 +08:00
|
|
|
.min_val_size = 32, \
|
2022-03-18 08:51:27 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
#define MAX_EXTENT_COMPRESS_RATIO_SHIFT 10
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert from pos in backpointer btree to pos of corresponding bucket in alloc
|
|
|
|
* btree:
|
|
|
|
*/
|
2024-05-01 04:50:28 +08:00
|
|
|
static inline struct bpos bp_pos_to_bucket(const struct bch_dev *ca, struct bpos bp_pos)
|
2022-03-18 08:51:27 +08:00
|
|
|
{
|
|
|
|
u64 bucket_sector = bp_pos.offset >> MAX_EXTENT_COMPRESS_RATIO_SHIFT;
|
|
|
|
|
|
|
|
return POS(bp_pos.inode, sector_to_bucket(ca, bucket_sector));
|
|
|
|
}
|
|
|
|
|
2024-05-01 04:50:28 +08:00
|
|
|
static inline bool bp_pos_to_bucket_nodev_noerror(struct bch_fs *c, struct bpos bp_pos, struct bpos *bucket)
|
|
|
|
{
|
|
|
|
rcu_read_lock();
|
|
|
|
struct bch_dev *ca = bch2_dev_rcu(c, bp_pos.inode);
|
|
|
|
if (ca)
|
|
|
|
*bucket = bp_pos_to_bucket(ca, bp_pos);
|
|
|
|
rcu_read_unlock();
|
|
|
|
return ca != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool bp_pos_to_bucket_nodev(struct bch_fs *c, struct bpos bp_pos, struct bpos *bucket)
|
|
|
|
{
|
|
|
|
return !bch2_fs_inconsistent_on(!bp_pos_to_bucket_nodev_noerror(c, bp_pos, bucket),
|
|
|
|
c, "backpointer for missing device %llu", bp_pos.inode);
|
|
|
|
}
|
|
|
|
|
2024-05-03 23:06:54 +08:00
|
|
|
static inline struct bpos bucket_pos_to_bp_noerror(const struct bch_dev *ca,
|
|
|
|
struct bpos bucket,
|
|
|
|
u64 bucket_offset)
|
|
|
|
{
|
|
|
|
return POS(bucket.inode,
|
|
|
|
(bucket_to_sector(ca, bucket.offset) <<
|
|
|
|
MAX_EXTENT_COMPRESS_RATIO_SHIFT) + bucket_offset);
|
|
|
|
}
|
|
|
|
|
2022-03-18 08:51:27 +08:00
|
|
|
/*
|
|
|
|
* Convert from pos in alloc btree + bucket offset to pos in backpointer btree:
|
|
|
|
*/
|
2024-05-01 04:50:28 +08:00
|
|
|
static inline struct bpos bucket_pos_to_bp(const struct bch_dev *ca,
|
2022-03-18 08:51:27 +08:00
|
|
|
struct bpos bucket,
|
|
|
|
u64 bucket_offset)
|
|
|
|
{
|
2024-05-03 23:06:54 +08:00
|
|
|
struct bpos ret = bucket_pos_to_bp_noerror(ca, bucket, bucket_offset);
|
2024-05-01 04:50:28 +08:00
|
|
|
EBUG_ON(!bkey_eq(bucket, bp_pos_to_bucket(ca, ret)));
|
2022-03-18 08:51:27 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2024-05-01 04:50:28 +08:00
|
|
|
int bch2_bucket_backpointer_mod_nowritebuffer(struct btree_trans *, struct bch_dev *,
|
|
|
|
struct bpos bucket, struct bch_backpointer, struct bkey_s_c, bool);
|
2022-03-18 08:51:27 +08:00
|
|
|
|
|
|
|
static inline int bch2_bucket_backpointer_mod(struct btree_trans *trans,
|
2024-05-01 04:50:28 +08:00
|
|
|
struct bch_dev *ca,
|
2023-04-01 04:24:45 +08:00
|
|
|
struct bpos bucket,
|
2022-03-18 08:51:27 +08:00
|
|
|
struct bch_backpointer bp,
|
|
|
|
struct bkey_s_c orig_k,
|
|
|
|
bool insert)
|
|
|
|
{
|
2023-11-27 09:18:16 +08:00
|
|
|
if (unlikely(bch2_backpointers_no_use_write_buffer))
|
2024-05-01 04:50:28 +08:00
|
|
|
return bch2_bucket_backpointer_mod_nowritebuffer(trans, ca, bucket, bp, orig_k, insert);
|
2022-03-18 08:51:27 +08:00
|
|
|
|
2023-11-27 09:18:16 +08:00
|
|
|
struct bkey_i_backpointer bp_k;
|
2022-03-18 08:51:27 +08:00
|
|
|
|
2023-11-27 09:18:16 +08:00
|
|
|
bkey_backpointer_init(&bp_k.k_i);
|
2024-05-01 04:50:28 +08:00
|
|
|
bp_k.k.p = bucket_pos_to_bp(ca, bucket, bp.bucket_offset);
|
2023-11-27 09:18:16 +08:00
|
|
|
bp_k.v = bp;
|
2022-03-18 08:51:27 +08:00
|
|
|
|
|
|
|
if (!insert) {
|
2023-11-27 09:18:16 +08:00
|
|
|
bp_k.k.type = KEY_TYPE_deleted;
|
|
|
|
set_bkey_val_u64s(&bp_k.k, 0);
|
2022-03-18 08:51:27 +08:00
|
|
|
}
|
|
|
|
|
2023-11-27 09:18:16 +08:00
|
|
|
return bch2_trans_update_buffered(trans, BTREE_ID_backpointers, &bp_k.k_i);
|
2022-03-18 08:51:27 +08:00
|
|
|
}
|
|
|
|
|
2024-03-26 07:26:05 +08:00
|
|
|
static inline enum bch_data_type bch2_bkey_ptr_data_type(struct bkey_s_c k,
|
|
|
|
struct extent_ptr_decoded p,
|
|
|
|
const union bch_extent_entry *entry)
|
2023-03-02 10:47:07 +08:00
|
|
|
{
|
2024-03-26 07:26:05 +08:00
|
|
|
switch (k.k->type) {
|
|
|
|
case KEY_TYPE_btree_ptr:
|
|
|
|
case KEY_TYPE_btree_ptr_v2:
|
|
|
|
return BCH_DATA_btree;
|
|
|
|
case KEY_TYPE_extent:
|
|
|
|
case KEY_TYPE_reflink_v:
|
|
|
|
return p.has_ec ? BCH_DATA_stripe : BCH_DATA_user;
|
|
|
|
case KEY_TYPE_stripe: {
|
|
|
|
const struct bch_extent_ptr *ptr = &entry->ptr;
|
|
|
|
struct bkey_s_c_stripe s = bkey_s_c_to_stripe(k);
|
|
|
|
|
|
|
|
BUG_ON(ptr < s.v->ptrs ||
|
|
|
|
ptr >= s.v->ptrs + s.v->nr_blocks);
|
|
|
|
|
|
|
|
return ptr >= s.v->ptrs + s.v->nr_blocks - s.v->nr_redundant
|
|
|
|
? BCH_DATA_parity
|
|
|
|
: BCH_DATA_user;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
BUG();
|
|
|
|
}
|
2023-03-02 10:47:07 +08:00
|
|
|
}
|
|
|
|
|
2024-05-01 07:34:28 +08:00
|
|
|
static inline void bch2_extent_ptr_to_bp(struct bch_fs *c, struct bch_dev *ca,
|
2022-03-18 08:51:27 +08:00
|
|
|
enum btree_id btree_id, unsigned level,
|
|
|
|
struct bkey_s_c k, struct extent_ptr_decoded p,
|
2024-03-26 07:26:05 +08:00
|
|
|
const union bch_extent_entry *entry,
|
2022-03-18 08:51:27 +08:00
|
|
|
struct bpos *bucket_pos, struct bch_backpointer *bp)
|
|
|
|
{
|
2024-03-26 07:26:05 +08:00
|
|
|
enum bch_data_type data_type = bch2_bkey_ptr_data_type(k, p, entry);
|
2022-03-18 08:51:27 +08:00
|
|
|
s64 sectors = level ? btree_sectors(c) : k.k->size;
|
|
|
|
u32 bucket_offset;
|
|
|
|
|
2024-05-01 07:34:28 +08:00
|
|
|
*bucket_pos = PTR_BUCKET_POS_OFFSET(ca, &p.ptr, &bucket_offset);
|
2022-03-18 08:51:27 +08:00
|
|
|
*bp = (struct bch_backpointer) {
|
|
|
|
.btree_id = btree_id,
|
|
|
|
.level = level,
|
|
|
|
.data_type = data_type,
|
|
|
|
.bucket_offset = ((u64) bucket_offset << MAX_EXTENT_COMPRESS_RATIO_SHIFT) +
|
|
|
|
p.crc.offset,
|
|
|
|
.bucket_len = ptr_disk_sectors(sectors, p),
|
|
|
|
.pos = k.k->p,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-05-01 04:50:28 +08:00
|
|
|
int bch2_get_next_backpointer(struct btree_trans *, struct bch_dev *ca, struct bpos, int,
|
2023-04-01 04:24:45 +08:00
|
|
|
struct bpos *, struct bch_backpointer *, unsigned);
|
2022-03-18 08:51:27 +08:00
|
|
|
struct bkey_s_c bch2_backpointer_get_key(struct btree_trans *, struct btree_iter *,
|
2023-04-01 04:24:45 +08:00
|
|
|
struct bpos, struct bch_backpointer,
|
2023-03-30 01:10:36 +08:00
|
|
|
unsigned);
|
2022-03-18 08:51:27 +08:00
|
|
|
struct btree *bch2_backpointer_get_node(struct btree_trans *, struct btree_iter *,
|
2023-04-01 04:24:45 +08:00
|
|
|
struct bpos, struct bch_backpointer);
|
2022-03-18 08:51:27 +08:00
|
|
|
|
|
|
|
int bch2_check_btree_backpointers(struct bch_fs *);
|
|
|
|
int bch2_check_extents_to_backpointers(struct bch_fs *);
|
|
|
|
int bch2_check_backpointers_to_extents(struct bch_fs *);
|
|
|
|
|
|
|
|
#endif /* _BCACHEFS_BACKPOINTERS_BACKGROUND_H */
|