18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (c) 2000,2002-2005 Silicon Graphics, Inc.
48c2ecf20Sopenharmony_ci * All Rights Reserved.
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci#ifndef __XFS_BMAP_BTREE_H__
78c2ecf20Sopenharmony_ci#define __XFS_BMAP_BTREE_H__
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_cistruct xfs_btree_cur;
108c2ecf20Sopenharmony_cistruct xfs_btree_block;
118c2ecf20Sopenharmony_cistruct xfs_mount;
128c2ecf20Sopenharmony_cistruct xfs_inode;
138c2ecf20Sopenharmony_cistruct xfs_trans;
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci/*
168c2ecf20Sopenharmony_ci * Btree block header size depends on a superblock flag.
178c2ecf20Sopenharmony_ci */
188c2ecf20Sopenharmony_ci#define XFS_BMBT_BLOCK_LEN(mp) \
198c2ecf20Sopenharmony_ci	(xfs_sb_version_hascrc(&((mp)->m_sb)) ? \
208c2ecf20Sopenharmony_ci		XFS_BTREE_LBLOCK_CRC_LEN : XFS_BTREE_LBLOCK_LEN)
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#define XFS_BMBT_REC_ADDR(mp, block, index) \
238c2ecf20Sopenharmony_ci	((xfs_bmbt_rec_t *) \
248c2ecf20Sopenharmony_ci		((char *)(block) + \
258c2ecf20Sopenharmony_ci		 XFS_BMBT_BLOCK_LEN(mp) + \
268c2ecf20Sopenharmony_ci		 ((index) - 1) * sizeof(xfs_bmbt_rec_t)))
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci#define XFS_BMBT_KEY_ADDR(mp, block, index) \
298c2ecf20Sopenharmony_ci	((xfs_bmbt_key_t *) \
308c2ecf20Sopenharmony_ci		((char *)(block) + \
318c2ecf20Sopenharmony_ci		 XFS_BMBT_BLOCK_LEN(mp) + \
328c2ecf20Sopenharmony_ci		 ((index) - 1) * sizeof(xfs_bmbt_key_t)))
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci#define XFS_BMBT_PTR_ADDR(mp, block, index, maxrecs) \
358c2ecf20Sopenharmony_ci	((xfs_bmbt_ptr_t *) \
368c2ecf20Sopenharmony_ci		((char *)(block) + \
378c2ecf20Sopenharmony_ci		 XFS_BMBT_BLOCK_LEN(mp) + \
388c2ecf20Sopenharmony_ci		 (maxrecs) * sizeof(xfs_bmbt_key_t) + \
398c2ecf20Sopenharmony_ci		 ((index) - 1) * sizeof(xfs_bmbt_ptr_t)))
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci#define XFS_BMDR_REC_ADDR(block, index) \
428c2ecf20Sopenharmony_ci	((xfs_bmdr_rec_t *) \
438c2ecf20Sopenharmony_ci		((char *)(block) + \
448c2ecf20Sopenharmony_ci		 sizeof(struct xfs_bmdr_block) + \
458c2ecf20Sopenharmony_ci	         ((index) - 1) * sizeof(xfs_bmdr_rec_t)))
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci#define XFS_BMDR_KEY_ADDR(block, index) \
488c2ecf20Sopenharmony_ci	((xfs_bmdr_key_t *) \
498c2ecf20Sopenharmony_ci		((char *)(block) + \
508c2ecf20Sopenharmony_ci		 sizeof(struct xfs_bmdr_block) + \
518c2ecf20Sopenharmony_ci		 ((index) - 1) * sizeof(xfs_bmdr_key_t)))
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci#define XFS_BMDR_PTR_ADDR(block, index, maxrecs) \
548c2ecf20Sopenharmony_ci	((xfs_bmdr_ptr_t *) \
558c2ecf20Sopenharmony_ci		((char *)(block) + \
568c2ecf20Sopenharmony_ci		 sizeof(struct xfs_bmdr_block) + \
578c2ecf20Sopenharmony_ci		 (maxrecs) * sizeof(xfs_bmdr_key_t) + \
588c2ecf20Sopenharmony_ci		 ((index) - 1) * sizeof(xfs_bmdr_ptr_t)))
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci/*
618c2ecf20Sopenharmony_ci * These are to be used when we know the size of the block and
628c2ecf20Sopenharmony_ci * we don't have a cursor.
638c2ecf20Sopenharmony_ci */
648c2ecf20Sopenharmony_ci#define XFS_BMAP_BROOT_PTR_ADDR(mp, bb, i, sz) \
658c2ecf20Sopenharmony_ci	XFS_BMBT_PTR_ADDR(mp, bb, i, xfs_bmbt_maxrecs(mp, sz, 0))
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci#define XFS_BMAP_BROOT_SPACE_CALC(mp, nrecs) \
688c2ecf20Sopenharmony_ci	(int)(XFS_BMBT_BLOCK_LEN(mp) + \
698c2ecf20Sopenharmony_ci	       ((nrecs) * (sizeof(xfs_bmbt_key_t) + sizeof(xfs_bmbt_ptr_t))))
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci#define XFS_BMAP_BROOT_SPACE(mp, bb) \
728c2ecf20Sopenharmony_ci	(XFS_BMAP_BROOT_SPACE_CALC(mp, be16_to_cpu((bb)->bb_numrecs)))
738c2ecf20Sopenharmony_ci#define XFS_BMDR_SPACE_CALC(nrecs) \
748c2ecf20Sopenharmony_ci	(int)(sizeof(xfs_bmdr_block_t) + \
758c2ecf20Sopenharmony_ci	       ((nrecs) * (sizeof(xfs_bmbt_key_t) + sizeof(xfs_bmbt_ptr_t))))
768c2ecf20Sopenharmony_ci#define XFS_BMAP_BMDR_SPACE(bb) \
778c2ecf20Sopenharmony_ci	(XFS_BMDR_SPACE_CALC(be16_to_cpu((bb)->bb_numrecs)))
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci/*
808c2ecf20Sopenharmony_ci * Maximum number of bmap btree levels.
818c2ecf20Sopenharmony_ci */
828c2ecf20Sopenharmony_ci#define XFS_BM_MAXLEVELS(mp,w)		((mp)->m_bm_maxlevels[(w)])
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci/*
858c2ecf20Sopenharmony_ci * Prototypes for xfs_bmap.c to call.
868c2ecf20Sopenharmony_ci */
878c2ecf20Sopenharmony_ciextern void xfs_bmdr_to_bmbt(struct xfs_inode *, xfs_bmdr_block_t *, int,
888c2ecf20Sopenharmony_ci			struct xfs_btree_block *, int);
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_civoid xfs_bmbt_disk_set_all(struct xfs_bmbt_rec *r, struct xfs_bmbt_irec *s);
918c2ecf20Sopenharmony_ciextern xfs_filblks_t xfs_bmbt_disk_get_blockcount(xfs_bmbt_rec_t *r);
928c2ecf20Sopenharmony_ciextern xfs_fileoff_t xfs_bmbt_disk_get_startoff(xfs_bmbt_rec_t *r);
938c2ecf20Sopenharmony_ciextern void xfs_bmbt_disk_get_all(xfs_bmbt_rec_t *r, xfs_bmbt_irec_t *s);
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ciextern void xfs_bmbt_to_bmdr(struct xfs_mount *, struct xfs_btree_block *, int,
968c2ecf20Sopenharmony_ci			xfs_bmdr_block_t *, int);
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ciextern int xfs_bmbt_get_maxrecs(struct xfs_btree_cur *, int level);
998c2ecf20Sopenharmony_ciextern int xfs_bmdr_maxrecs(int blocklen, int leaf);
1008c2ecf20Sopenharmony_ciextern int xfs_bmbt_maxrecs(struct xfs_mount *, int blocklen, int leaf);
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ciextern int xfs_bmbt_change_owner(struct xfs_trans *tp, struct xfs_inode *ip,
1038c2ecf20Sopenharmony_ci				 int whichfork, xfs_ino_t new_owner,
1048c2ecf20Sopenharmony_ci				 struct list_head *buffer_list);
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ciextern struct xfs_btree_cur *xfs_bmbt_init_cursor(struct xfs_mount *,
1078c2ecf20Sopenharmony_ci		struct xfs_trans *, struct xfs_inode *, int);
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ciextern unsigned long long xfs_bmbt_calc_size(struct xfs_mount *mp,
1108c2ecf20Sopenharmony_ci		unsigned long long len);
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci#endif	/* __XFS_BMAP_BTREE_H__ */
113