^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: GPL-2.0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * All Rights Reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #ifndef __XFS_BTREE_H__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #define __XFS_BTREE_H__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) struct xfs_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) struct xfs_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) struct xfs_mount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) struct xfs_trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) struct xfs_ifork;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) extern kmem_zone_t *xfs_btree_cur_zone;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * Generic key, ptr and record wrapper structures.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * These are disk format structures, and are converted where necessary
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * by the btree specific code that needs to interpret them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) union xfs_btree_ptr {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) __be32 s; /* short form ptr */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) __be64 l; /* long form ptr */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * The in-core btree key. Overlapping btrees actually store two keys
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * per pointer, so we reserve enough memory to hold both. The __*bigkey
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * items should never be accessed directly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) union xfs_btree_key {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) struct xfs_bmbt_key bmbt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) xfs_bmdr_key_t bmbr; /* bmbt root block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) xfs_alloc_key_t alloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) struct xfs_inobt_key inobt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) struct xfs_rmap_key rmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) struct xfs_rmap_key __rmap_bigkey[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) struct xfs_refcount_key refc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) union xfs_btree_rec {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) struct xfs_bmbt_rec bmbt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) xfs_bmdr_rec_t bmbr; /* bmbt root block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) struct xfs_alloc_rec alloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct xfs_inobt_rec inobt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct xfs_rmap_rec rmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) struct xfs_refcount_rec refc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * This nonsense is to make -wlint happy.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) #define XFS_LOOKUP_EQ ((xfs_lookup_t)XFS_LOOKUP_EQi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) #define XFS_LOOKUP_LE ((xfs_lookup_t)XFS_LOOKUP_LEi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) #define XFS_LOOKUP_GE ((xfs_lookup_t)XFS_LOOKUP_GEi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) #define XFS_BTNUM_BNO ((xfs_btnum_t)XFS_BTNUM_BNOi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) #define XFS_BTNUM_CNT ((xfs_btnum_t)XFS_BTNUM_CNTi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) #define XFS_BTNUM_BMAP ((xfs_btnum_t)XFS_BTNUM_BMAPi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) #define XFS_BTNUM_INO ((xfs_btnum_t)XFS_BTNUM_INOi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) #define XFS_BTNUM_FINO ((xfs_btnum_t)XFS_BTNUM_FINOi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) #define XFS_BTNUM_RMAP ((xfs_btnum_t)XFS_BTNUM_RMAPi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) #define XFS_BTNUM_REFC ((xfs_btnum_t)XFS_BTNUM_REFCi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) uint32_t xfs_btree_magic(int crc, xfs_btnum_t btnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) * For logging record fields.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) #define XFS_BB_MAGIC (1 << 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) #define XFS_BB_LEVEL (1 << 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) #define XFS_BB_NUMRECS (1 << 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) #define XFS_BB_LEFTSIB (1 << 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) #define XFS_BB_RIGHTSIB (1 << 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) #define XFS_BB_BLKNO (1 << 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) #define XFS_BB_LSN (1 << 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) #define XFS_BB_UUID (1 << 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) #define XFS_BB_OWNER (1 << 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) #define XFS_BB_NUM_BITS 5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) #define XFS_BB_ALL_BITS ((1 << XFS_BB_NUM_BITS) - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) #define XFS_BB_NUM_BITS_CRC 9
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) #define XFS_BB_ALL_BITS_CRC ((1 << XFS_BB_NUM_BITS_CRC) - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) * Generic stats interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) #define XFS_BTREE_STATS_INC(cur, stat) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) XFS_STATS_INC_OFF((cur)->bc_mp, (cur)->bc_statoff + __XBTS_ ## stat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) #define XFS_BTREE_STATS_ADD(cur, stat, val) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) XFS_STATS_ADD_OFF((cur)->bc_mp, (cur)->bc_statoff + __XBTS_ ## stat, val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) #define XFS_BTREE_MAXLEVELS 9 /* max of all btrees */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) struct xfs_btree_ops {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) /* size of the key and record structures */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) size_t key_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) size_t rec_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) /* cursor operations */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) struct xfs_btree_cur *(*dup_cursor)(struct xfs_btree_cur *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) void (*update_cursor)(struct xfs_btree_cur *src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) struct xfs_btree_cur *dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) /* update btree root pointer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) void (*set_root)(struct xfs_btree_cur *cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) union xfs_btree_ptr *nptr, int level_change);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) /* block allocation / freeing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) int (*alloc_block)(struct xfs_btree_cur *cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) union xfs_btree_ptr *start_bno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) union xfs_btree_ptr *new_bno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) int *stat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) int (*free_block)(struct xfs_btree_cur *cur, struct xfs_buf *bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) /* update last record information */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) void (*update_lastrec)(struct xfs_btree_cur *cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) struct xfs_btree_block *block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) union xfs_btree_rec *rec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) int ptr, int reason);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) /* records in block/level */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) int (*get_minrecs)(struct xfs_btree_cur *cur, int level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) int (*get_maxrecs)(struct xfs_btree_cur *cur, int level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) /* records on disk. Matter for the root in inode case. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) int (*get_dmaxrecs)(struct xfs_btree_cur *cur, int level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) /* init values of btree structures */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) void (*init_key_from_rec)(union xfs_btree_key *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) union xfs_btree_rec *rec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) void (*init_rec_from_cur)(struct xfs_btree_cur *cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) union xfs_btree_rec *rec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) void (*init_ptr_from_cur)(struct xfs_btree_cur *cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) union xfs_btree_ptr *ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) void (*init_high_key_from_rec)(union xfs_btree_key *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) union xfs_btree_rec *rec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) /* difference between key value and cursor value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) int64_t (*key_diff)(struct xfs_btree_cur *cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) union xfs_btree_key *key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) * Difference between key2 and key1 -- positive if key1 > key2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) * negative if key1 < key2, and zero if equal.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) int64_t (*diff_two_keys)(struct xfs_btree_cur *cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) union xfs_btree_key *key1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) union xfs_btree_key *key2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) const struct xfs_buf_ops *buf_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) /* check that k1 is lower than k2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) int (*keys_inorder)(struct xfs_btree_cur *cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) union xfs_btree_key *k1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) union xfs_btree_key *k2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) /* check that r1 is lower than r2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) int (*recs_inorder)(struct xfs_btree_cur *cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) union xfs_btree_rec *r1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) union xfs_btree_rec *r2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) * Reasons for the update_lastrec method to be called.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) #define LASTREC_UPDATE 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) #define LASTREC_INSREC 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) #define LASTREC_DELREC 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) union xfs_btree_irec {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) struct xfs_alloc_rec_incore a;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) struct xfs_bmbt_irec b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) struct xfs_inobt_rec_incore i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) struct xfs_rmap_irec r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) struct xfs_refcount_irec rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) /* Per-AG btree information. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) struct xfs_btree_cur_ag {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) struct xfs_buf *agbp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) struct xbtree_afakeroot *afake; /* for staging cursor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) xfs_agnumber_t agno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) unsigned long nr_ops; /* # record updates */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) int shape_changes; /* # of extent splits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) } refc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) bool active; /* allocation cursor state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) } abt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) /* Btree-in-inode cursor information */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) struct xfs_btree_cur_ino {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) struct xfs_inode *ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) struct xbtree_ifakeroot *ifake; /* for staging cursor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) int allocated;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) short forksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) char whichfork;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) char flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) /* We are converting a delalloc reservation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) #define XFS_BTCUR_BMBT_WASDEL (1 << 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) /* For extent swap, ignore owner check in verifier */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) #define XFS_BTCUR_BMBT_INVALID_OWNER (1 << 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) * Btree cursor structure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) * This collects all information needed by the btree code in one place.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) typedef struct xfs_btree_cur
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) struct xfs_trans *bc_tp; /* transaction we're in, if any */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) struct xfs_mount *bc_mp; /* file system mount struct */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) const struct xfs_btree_ops *bc_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) uint bc_flags; /* btree features - below */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) union xfs_btree_irec bc_rec; /* current insert/search record value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) struct xfs_buf *bc_bufs[XFS_BTREE_MAXLEVELS]; /* buf ptr per level */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) int bc_ptrs[XFS_BTREE_MAXLEVELS]; /* key/record # */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) uint8_t bc_ra[XFS_BTREE_MAXLEVELS]; /* readahead bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) #define XFS_BTCUR_LEFTRA 1 /* left sibling has been read-ahead */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) #define XFS_BTCUR_RIGHTRA 2 /* right sibling has been read-ahead */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) uint8_t bc_nlevels; /* number of levels in the tree */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) uint8_t bc_blocklog; /* log2(blocksize) of btree blocks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) xfs_btnum_t bc_btnum; /* identifies which btree type */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) int bc_statoff; /* offset of btre stats array */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) struct xfs_btree_cur_ag bc_ag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) struct xfs_btree_cur_ino bc_ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) } xfs_btree_cur_t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) /* cursor flags */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) #define XFS_BTREE_LONG_PTRS (1<<0) /* pointers are 64bits long */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) #define XFS_BTREE_ROOT_IN_INODE (1<<1) /* root may be variable size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) #define XFS_BTREE_LASTREC_UPDATE (1<<2) /* track last rec externally */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) #define XFS_BTREE_CRC_BLOCKS (1<<3) /* uses extended btree blocks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) #define XFS_BTREE_OVERLAPPING (1<<4) /* overlapping intervals */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) * The root of this btree is a fakeroot structure so that we can stage a btree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) * rebuild without leaving it accessible via primary metadata. The ops struct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) * is dynamically allocated and must be freed when the cursor is deleted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) #define XFS_BTREE_STAGING (1<<5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) #define XFS_BTREE_NOERROR 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) #define XFS_BTREE_ERROR 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) * Convert from buffer to btree block header.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) #define XFS_BUF_TO_BLOCK(bp) ((struct xfs_btree_block *)((bp)->b_addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) * Internal long and short btree block checks. They return NULL if the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) * block is ok or the address of the failed check otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) xfs_failaddr_t __xfs_btree_check_lblock(struct xfs_btree_cur *cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) struct xfs_btree_block *block, int level, struct xfs_buf *bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) xfs_failaddr_t __xfs_btree_check_sblock(struct xfs_btree_cur *cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) struct xfs_btree_block *block, int level, struct xfs_buf *bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) * Check that block header is ok.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) xfs_btree_check_block(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) struct xfs_btree_cur *cur, /* btree cursor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) struct xfs_btree_block *block, /* generic btree block pointer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) int level, /* level of the btree block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) struct xfs_buf *bp); /* buffer containing block, if any */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) * Check that (long) pointer is ok.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) bool /* error (0 or EFSCORRUPTED) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) xfs_btree_check_lptr(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) struct xfs_btree_cur *cur, /* btree cursor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) xfs_fsblock_t fsbno, /* btree block disk address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) int level); /* btree block level */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) * Check that (short) pointer is ok.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) bool /* error (0 or EFSCORRUPTED) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) xfs_btree_check_sptr(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) struct xfs_btree_cur *cur, /* btree cursor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) xfs_agblock_t agbno, /* btree block disk address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) int level); /* btree block level */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) * Delete the btree cursor.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) xfs_btree_del_cursor(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) xfs_btree_cur_t *cur, /* btree cursor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) int error); /* del because of error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) * Duplicate the btree cursor.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) * Allocate a new one, copy the record, re-get the buffers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) int /* error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) xfs_btree_dup_cursor(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) xfs_btree_cur_t *cur, /* input cursor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) xfs_btree_cur_t **ncur);/* output cursor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) * Compute first and last byte offsets for the fields given.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) * Interprets the offsets table, which contains struct field offsets.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) xfs_btree_offsets(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) int64_t fields, /* bitmask of fields */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) const short *offsets,/* table of field offsets */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) int nbits, /* number of bits to inspect */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) int *first, /* output: first byte offset */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) int *last); /* output: last byte offset */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) * Get a buffer for the block, return it read in.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) * Long-form addressing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) int /* error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) xfs_btree_read_bufl(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) struct xfs_mount *mp, /* file system mount point */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) struct xfs_trans *tp, /* transaction pointer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) xfs_fsblock_t fsbno, /* file system block number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) struct xfs_buf **bpp, /* buffer for fsbno */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) int refval, /* ref count value for buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) const struct xfs_buf_ops *ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) * Read-ahead the block, don't wait for it, don't return a buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) * Long-form addressing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) void /* error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) xfs_btree_reada_bufl(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) struct xfs_mount *mp, /* file system mount point */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) xfs_fsblock_t fsbno, /* file system block number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) xfs_extlen_t count, /* count of filesystem blocks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) const struct xfs_buf_ops *ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) * Read-ahead the block, don't wait for it, don't return a buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) * Short-form addressing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) void /* error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) xfs_btree_reada_bufs(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) struct xfs_mount *mp, /* file system mount point */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) xfs_agnumber_t agno, /* allocation group number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) xfs_agblock_t agbno, /* allocation group block number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) xfs_extlen_t count, /* count of filesystem blocks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) const struct xfs_buf_ops *ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) * Initialise a new btree block header
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) xfs_btree_init_block(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) struct xfs_mount *mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) struct xfs_buf *bp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) xfs_btnum_t btnum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) __u16 level,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) __u16 numrecs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) __u64 owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) xfs_btree_init_block_int(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) struct xfs_mount *mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) struct xfs_btree_block *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) xfs_daddr_t blkno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) xfs_btnum_t btnum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) __u16 level,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) __u16 numrecs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) __u64 owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) unsigned int flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) * Common btree core entry points.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) int xfs_btree_increment(struct xfs_btree_cur *, int, int *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) int xfs_btree_decrement(struct xfs_btree_cur *, int, int *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) int xfs_btree_lookup(struct xfs_btree_cur *, xfs_lookup_t, int *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) int xfs_btree_update(struct xfs_btree_cur *, union xfs_btree_rec *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) int xfs_btree_new_iroot(struct xfs_btree_cur *, int *, int *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) int xfs_btree_insert(struct xfs_btree_cur *, int *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) int xfs_btree_delete(struct xfs_btree_cur *, int *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) int xfs_btree_get_rec(struct xfs_btree_cur *, union xfs_btree_rec **, int *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) int xfs_btree_change_owner(struct xfs_btree_cur *cur, uint64_t new_owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) struct list_head *buffer_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) * btree block CRC helpers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) void xfs_btree_lblock_calc_crc(struct xfs_buf *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) bool xfs_btree_lblock_verify_crc(struct xfs_buf *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) void xfs_btree_sblock_calc_crc(struct xfs_buf *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) bool xfs_btree_sblock_verify_crc(struct xfs_buf *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) * Internal btree helpers also used by xfs_bmap.c.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) void xfs_btree_log_block(struct xfs_btree_cur *, struct xfs_buf *, int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) void xfs_btree_log_recs(struct xfs_btree_cur *, struct xfs_buf *, int, int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) * Helpers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) static inline int xfs_btree_get_numrecs(struct xfs_btree_block *block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) return be16_to_cpu(block->bb_numrecs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) static inline void xfs_btree_set_numrecs(struct xfs_btree_block *block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) uint16_t numrecs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) block->bb_numrecs = cpu_to_be16(numrecs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) static inline int xfs_btree_get_level(struct xfs_btree_block *block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) return be16_to_cpu(block->bb_level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) * Min and max functions for extlen, agblock, fileoff, and filblks types.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) #define XFS_EXTLEN_MIN(a,b) min_t(xfs_extlen_t, (a), (b))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) #define XFS_EXTLEN_MAX(a,b) max_t(xfs_extlen_t, (a), (b))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) #define XFS_AGBLOCK_MIN(a,b) min_t(xfs_agblock_t, (a), (b))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) #define XFS_AGBLOCK_MAX(a,b) max_t(xfs_agblock_t, (a), (b))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) #define XFS_FILEOFF_MIN(a,b) min_t(xfs_fileoff_t, (a), (b))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) #define XFS_FILEOFF_MAX(a,b) max_t(xfs_fileoff_t, (a), (b))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) #define XFS_FILBLKS_MIN(a,b) min_t(xfs_filblks_t, (a), (b))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) #define XFS_FILBLKS_MAX(a,b) max_t(xfs_filblks_t, (a), (b))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) xfs_failaddr_t xfs_btree_sblock_v5hdr_verify(struct xfs_buf *bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) xfs_failaddr_t xfs_btree_sblock_verify(struct xfs_buf *bp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) unsigned int max_recs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) xfs_failaddr_t xfs_btree_lblock_v5hdr_verify(struct xfs_buf *bp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) uint64_t owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) xfs_failaddr_t xfs_btree_lblock_verify(struct xfs_buf *bp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) unsigned int max_recs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) uint xfs_btree_compute_maxlevels(uint *limits, unsigned long len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) unsigned long long xfs_btree_calc_size(uint *limits, unsigned long long len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) * Return codes for the query range iterator function are 0 to continue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) * iterating, and non-zero to stop iterating. Any non-zero value will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) * passed up to the _query_range caller. The special value -ECANCELED can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) * used to stop iteration, because _query_range never generates that error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) * code on its own.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) typedef int (*xfs_btree_query_range_fn)(struct xfs_btree_cur *cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) union xfs_btree_rec *rec, void *priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) int xfs_btree_query_range(struct xfs_btree_cur *cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) union xfs_btree_irec *low_rec, union xfs_btree_irec *high_rec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) xfs_btree_query_range_fn fn, void *priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) int xfs_btree_query_all(struct xfs_btree_cur *cur, xfs_btree_query_range_fn fn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) void *priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) typedef int (*xfs_btree_visit_blocks_fn)(struct xfs_btree_cur *cur, int level,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) void *data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) /* Visit record blocks. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) #define XFS_BTREE_VISIT_RECORDS (1 << 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) /* Visit leaf blocks. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) #define XFS_BTREE_VISIT_LEAVES (1 << 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) /* Visit all blocks. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) #define XFS_BTREE_VISIT_ALL (XFS_BTREE_VISIT_RECORDS | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) XFS_BTREE_VISIT_LEAVES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) int xfs_btree_visit_blocks(struct xfs_btree_cur *cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) xfs_btree_visit_blocks_fn fn, unsigned int flags, void *data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) int xfs_btree_count_blocks(struct xfs_btree_cur *cur, xfs_extlen_t *blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) union xfs_btree_rec *xfs_btree_rec_addr(struct xfs_btree_cur *cur, int n,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) struct xfs_btree_block *block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) union xfs_btree_key *xfs_btree_key_addr(struct xfs_btree_cur *cur, int n,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) struct xfs_btree_block *block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) union xfs_btree_key *xfs_btree_high_key_addr(struct xfs_btree_cur *cur, int n,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) struct xfs_btree_block *block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) union xfs_btree_ptr *xfs_btree_ptr_addr(struct xfs_btree_cur *cur, int n,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) struct xfs_btree_block *block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) int xfs_btree_lookup_get_block(struct xfs_btree_cur *cur, int level,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) union xfs_btree_ptr *pp, struct xfs_btree_block **blkp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) struct xfs_btree_block *xfs_btree_get_block(struct xfs_btree_cur *cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) int level, struct xfs_buf **bpp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) bool xfs_btree_ptr_is_null(struct xfs_btree_cur *cur, union xfs_btree_ptr *ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) int64_t xfs_btree_diff_two_ptrs(struct xfs_btree_cur *cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) const union xfs_btree_ptr *a,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) const union xfs_btree_ptr *b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) void xfs_btree_get_sibling(struct xfs_btree_cur *cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) struct xfs_btree_block *block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) union xfs_btree_ptr *ptr, int lr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) void xfs_btree_get_keys(struct xfs_btree_cur *cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) struct xfs_btree_block *block, union xfs_btree_key *key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) union xfs_btree_key *xfs_btree_high_key_from_key(struct xfs_btree_cur *cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) union xfs_btree_key *key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) int xfs_btree_has_record(struct xfs_btree_cur *cur, union xfs_btree_irec *low,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) union xfs_btree_irec *high, bool *exists);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) bool xfs_btree_has_more_records(struct xfs_btree_cur *cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) struct xfs_ifork *xfs_btree_ifork_ptr(struct xfs_btree_cur *cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) /* Does this cursor point to the last block in the given level? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) static inline bool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) xfs_btree_islastblock(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) xfs_btree_cur_t *cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) int level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) struct xfs_btree_block *block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) struct xfs_buf *bp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) block = xfs_btree_get_block(cur, level, &bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) ASSERT(block && xfs_btree_check_block(cur, block, level, bp) == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) return block->bb_u.l.bb_rightsib == cpu_to_be64(NULLFSBLOCK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) return block->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) void xfs_btree_set_ptr_null(struct xfs_btree_cur *cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) union xfs_btree_ptr *ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) int xfs_btree_get_buf_block(struct xfs_btree_cur *cur, union xfs_btree_ptr *ptr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) struct xfs_btree_block **block, struct xfs_buf **bpp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) void xfs_btree_set_sibling(struct xfs_btree_cur *cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) struct xfs_btree_block *block, union xfs_btree_ptr *ptr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) int lr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) void xfs_btree_init_block_cur(struct xfs_btree_cur *cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) struct xfs_buf *bp, int level, int numrecs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) void xfs_btree_copy_ptrs(struct xfs_btree_cur *cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) union xfs_btree_ptr *dst_ptr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) const union xfs_btree_ptr *src_ptr, int numptrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) void xfs_btree_copy_keys(struct xfs_btree_cur *cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) union xfs_btree_key *dst_key, union xfs_btree_key *src_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) int numkeys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) #endif /* __XFS_BTREE_H__ */