162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (c) 2000,2002-2005 Silicon Graphics, Inc.
462306a36Sopenharmony_ci * All Rights Reserved.
562306a36Sopenharmony_ci */
662306a36Sopenharmony_ci#ifndef __XFS_BMAP_BTREE_H__
762306a36Sopenharmony_ci#define __XFS_BMAP_BTREE_H__
862306a36Sopenharmony_ci
962306a36Sopenharmony_cistruct xfs_btree_cur;
1062306a36Sopenharmony_cistruct xfs_btree_block;
1162306a36Sopenharmony_cistruct xfs_mount;
1262306a36Sopenharmony_cistruct xfs_inode;
1362306a36Sopenharmony_cistruct xfs_trans;
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ci/*
1662306a36Sopenharmony_ci * Btree block header size depends on a superblock flag.
1762306a36Sopenharmony_ci */
1862306a36Sopenharmony_ci#define XFS_BMBT_BLOCK_LEN(mp) \
1962306a36Sopenharmony_ci	(xfs_has_crc(((mp))) ? \
2062306a36Sopenharmony_ci		XFS_BTREE_LBLOCK_CRC_LEN : XFS_BTREE_LBLOCK_LEN)
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ci#define XFS_BMBT_REC_ADDR(mp, block, index) \
2362306a36Sopenharmony_ci	((xfs_bmbt_rec_t *) \
2462306a36Sopenharmony_ci		((char *)(block) + \
2562306a36Sopenharmony_ci		 XFS_BMBT_BLOCK_LEN(mp) + \
2662306a36Sopenharmony_ci		 ((index) - 1) * sizeof(xfs_bmbt_rec_t)))
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci#define XFS_BMBT_KEY_ADDR(mp, block, index) \
2962306a36Sopenharmony_ci	((xfs_bmbt_key_t *) \
3062306a36Sopenharmony_ci		((char *)(block) + \
3162306a36Sopenharmony_ci		 XFS_BMBT_BLOCK_LEN(mp) + \
3262306a36Sopenharmony_ci		 ((index) - 1) * sizeof(xfs_bmbt_key_t)))
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci#define XFS_BMBT_PTR_ADDR(mp, block, index, maxrecs) \
3562306a36Sopenharmony_ci	((xfs_bmbt_ptr_t *) \
3662306a36Sopenharmony_ci		((char *)(block) + \
3762306a36Sopenharmony_ci		 XFS_BMBT_BLOCK_LEN(mp) + \
3862306a36Sopenharmony_ci		 (maxrecs) * sizeof(xfs_bmbt_key_t) + \
3962306a36Sopenharmony_ci		 ((index) - 1) * sizeof(xfs_bmbt_ptr_t)))
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci#define XFS_BMDR_REC_ADDR(block, index) \
4262306a36Sopenharmony_ci	((xfs_bmdr_rec_t *) \
4362306a36Sopenharmony_ci		((char *)(block) + \
4462306a36Sopenharmony_ci		 sizeof(struct xfs_bmdr_block) + \
4562306a36Sopenharmony_ci	         ((index) - 1) * sizeof(xfs_bmdr_rec_t)))
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ci#define XFS_BMDR_KEY_ADDR(block, index) \
4862306a36Sopenharmony_ci	((xfs_bmdr_key_t *) \
4962306a36Sopenharmony_ci		((char *)(block) + \
5062306a36Sopenharmony_ci		 sizeof(struct xfs_bmdr_block) + \
5162306a36Sopenharmony_ci		 ((index) - 1) * sizeof(xfs_bmdr_key_t)))
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_ci#define XFS_BMDR_PTR_ADDR(block, index, maxrecs) \
5462306a36Sopenharmony_ci	((xfs_bmdr_ptr_t *) \
5562306a36Sopenharmony_ci		((char *)(block) + \
5662306a36Sopenharmony_ci		 sizeof(struct xfs_bmdr_block) + \
5762306a36Sopenharmony_ci		 (maxrecs) * sizeof(xfs_bmdr_key_t) + \
5862306a36Sopenharmony_ci		 ((index) - 1) * sizeof(xfs_bmdr_ptr_t)))
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_ci/*
6162306a36Sopenharmony_ci * These are to be used when we know the size of the block and
6262306a36Sopenharmony_ci * we don't have a cursor.
6362306a36Sopenharmony_ci */
6462306a36Sopenharmony_ci#define XFS_BMAP_BROOT_PTR_ADDR(mp, bb, i, sz) \
6562306a36Sopenharmony_ci	XFS_BMBT_PTR_ADDR(mp, bb, i, xfs_bmbt_maxrecs(mp, sz, 0))
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ci#define XFS_BMAP_BROOT_SPACE_CALC(mp, nrecs) \
6862306a36Sopenharmony_ci	(int)(XFS_BMBT_BLOCK_LEN(mp) + \
6962306a36Sopenharmony_ci	       ((nrecs) * (sizeof(xfs_bmbt_key_t) + sizeof(xfs_bmbt_ptr_t))))
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ci#define XFS_BMAP_BROOT_SPACE(mp, bb) \
7262306a36Sopenharmony_ci	(XFS_BMAP_BROOT_SPACE_CALC(mp, be16_to_cpu((bb)->bb_numrecs)))
7362306a36Sopenharmony_ci#define XFS_BMDR_SPACE_CALC(nrecs) \
7462306a36Sopenharmony_ci	(int)(sizeof(xfs_bmdr_block_t) + \
7562306a36Sopenharmony_ci	       ((nrecs) * (sizeof(xfs_bmbt_key_t) + sizeof(xfs_bmbt_ptr_t))))
7662306a36Sopenharmony_ci#define XFS_BMAP_BMDR_SPACE(bb) \
7762306a36Sopenharmony_ci	(XFS_BMDR_SPACE_CALC(be16_to_cpu((bb)->bb_numrecs)))
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ci/*
8062306a36Sopenharmony_ci * Maximum number of bmap btree levels.
8162306a36Sopenharmony_ci */
8262306a36Sopenharmony_ci#define XFS_BM_MAXLEVELS(mp,w)		((mp)->m_bm_maxlevels[(w)])
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ci/*
8562306a36Sopenharmony_ci * Prototypes for xfs_bmap.c to call.
8662306a36Sopenharmony_ci */
8762306a36Sopenharmony_ciextern void xfs_bmdr_to_bmbt(struct xfs_inode *, xfs_bmdr_block_t *, int,
8862306a36Sopenharmony_ci			struct xfs_btree_block *, int);
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_civoid xfs_bmbt_disk_set_all(struct xfs_bmbt_rec *r, struct xfs_bmbt_irec *s);
9162306a36Sopenharmony_ciextern xfs_filblks_t xfs_bmbt_disk_get_blockcount(const struct xfs_bmbt_rec *r);
9262306a36Sopenharmony_ciextern xfs_fileoff_t xfs_bmbt_disk_get_startoff(const struct xfs_bmbt_rec *r);
9362306a36Sopenharmony_civoid xfs_bmbt_disk_get_all(const struct xfs_bmbt_rec *r,
9462306a36Sopenharmony_ci		struct xfs_bmbt_irec *s);
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_ciextern void xfs_bmbt_to_bmdr(struct xfs_mount *, struct xfs_btree_block *, int,
9762306a36Sopenharmony_ci			xfs_bmdr_block_t *, int);
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_ciextern int xfs_bmbt_get_maxrecs(struct xfs_btree_cur *, int level);
10062306a36Sopenharmony_ciextern int xfs_bmdr_maxrecs(int blocklen, int leaf);
10162306a36Sopenharmony_ciextern int xfs_bmbt_maxrecs(struct xfs_mount *, int blocklen, int leaf);
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_ciextern int xfs_bmbt_change_owner(struct xfs_trans *tp, struct xfs_inode *ip,
10462306a36Sopenharmony_ci				 int whichfork, xfs_ino_t new_owner,
10562306a36Sopenharmony_ci				 struct list_head *buffer_list);
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_ciextern struct xfs_btree_cur *xfs_bmbt_init_cursor(struct xfs_mount *,
10862306a36Sopenharmony_ci		struct xfs_trans *, struct xfs_inode *, int);
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ciextern unsigned long long xfs_bmbt_calc_size(struct xfs_mount *mp,
11162306a36Sopenharmony_ci		unsigned long long len);
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_ciunsigned int xfs_bmbt_maxlevels_ondisk(void);
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ciint __init xfs_bmbt_init_cur_cache(void);
11662306a36Sopenharmony_civoid xfs_bmbt_destroy_cur_cache(void);
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_ci#endif	/* __XFS_BMAP_BTREE_H__ */
119