1508578f2SNishad Kamdar/* SPDX-License-Identifier: GPL-2.0 */
21da177e4SLinus Torvalds/*
37b718769SNathan Scott * Copyright (c) 2000,2002-2005 Silicon Graphics, Inc.
47b718769SNathan Scott * All Rights Reserved.
51da177e4SLinus Torvalds */
61da177e4SLinus Torvalds#ifndef __XFS_BMAP_BTREE_H__
71da177e4SLinus Torvalds#define __XFS_BMAP_BTREE_H__
81da177e4SLinus Torvalds
91da177e4SLinus Torvaldsstruct xfs_btree_cur;
10136341b4SChristoph Hellwigstruct xfs_btree_block;
111da177e4SLinus Torvaldsstruct xfs_mount;
121da177e4SLinus Torvaldsstruct xfs_inode;
13561f7d17SChristoph Hellwigstruct xfs_trans;
141da177e4SLinus Torvalds
157cc95a82SChristoph Hellwig/*
167cc95a82SChristoph Hellwig * Btree block header size depends on a superblock flag.
177cc95a82SChristoph Hellwig */
18ee1a47abSChristoph Hellwig#define XFS_BMBT_BLOCK_LEN(mp) \
1938c26bfdSDave Chinner	(xfs_has_crc(((mp))) ? \
20ee1a47abSChristoph Hellwig		XFS_BTREE_LBLOCK_CRC_LEN : XFS_BTREE_LBLOCK_LEN)
211da177e4SLinus Torvalds
22136341b4SChristoph Hellwig#define XFS_BMBT_REC_ADDR(mp, block, index) \
23136341b4SChristoph Hellwig	((xfs_bmbt_rec_t *) \
24136341b4SChristoph Hellwig		((char *)(block) + \
257cc95a82SChristoph Hellwig		 XFS_BMBT_BLOCK_LEN(mp) + \
26136341b4SChristoph Hellwig		 ((index) - 1) * sizeof(xfs_bmbt_rec_t)))
27136341b4SChristoph Hellwig
28136341b4SChristoph Hellwig#define XFS_BMBT_KEY_ADDR(mp, block, index) \
29136341b4SChristoph Hellwig	((xfs_bmbt_key_t *) \
30136341b4SChristoph Hellwig		((char *)(block) + \
317cc95a82SChristoph Hellwig		 XFS_BMBT_BLOCK_LEN(mp) + \
32136341b4SChristoph Hellwig		 ((index) - 1) * sizeof(xfs_bmbt_key_t)))
33136341b4SChristoph Hellwig
34136341b4SChristoph Hellwig#define XFS_BMBT_PTR_ADDR(mp, block, index, maxrecs) \
35136341b4SChristoph Hellwig	((xfs_bmbt_ptr_t *) \
36136341b4SChristoph Hellwig		((char *)(block) + \
377cc95a82SChristoph Hellwig		 XFS_BMBT_BLOCK_LEN(mp) + \
38136341b4SChristoph Hellwig		 (maxrecs) * sizeof(xfs_bmbt_key_t) + \
39136341b4SChristoph Hellwig		 ((index) - 1) * sizeof(xfs_bmbt_ptr_t)))
40136341b4SChristoph Hellwig
41136341b4SChristoph Hellwig#define XFS_BMDR_REC_ADDR(block, index) \
42136341b4SChristoph Hellwig	((xfs_bmdr_rec_t *) \
43136341b4SChristoph Hellwig		((char *)(block) + \
44136341b4SChristoph Hellwig		 sizeof(struct xfs_bmdr_block) + \
45136341b4SChristoph Hellwig	         ((index) - 1) * sizeof(xfs_bmdr_rec_t)))
46136341b4SChristoph Hellwig
47136341b4SChristoph Hellwig#define XFS_BMDR_KEY_ADDR(block, index) \
48136341b4SChristoph Hellwig	((xfs_bmdr_key_t *) \
49136341b4SChristoph Hellwig		((char *)(block) + \
50136341b4SChristoph Hellwig		 sizeof(struct xfs_bmdr_block) + \
51136341b4SChristoph Hellwig		 ((index) - 1) * sizeof(xfs_bmdr_key_t)))
52136341b4SChristoph Hellwig
53136341b4SChristoph Hellwig#define XFS_BMDR_PTR_ADDR(block, index, maxrecs) \
54136341b4SChristoph Hellwig	((xfs_bmdr_ptr_t *) \
55136341b4SChristoph Hellwig		((char *)(block) + \
56136341b4SChristoph Hellwig		 sizeof(struct xfs_bmdr_block) + \
57136341b4SChristoph Hellwig		 (maxrecs) * sizeof(xfs_bmdr_key_t) + \
58136341b4SChristoph Hellwig		 ((index) - 1) * sizeof(xfs_bmdr_ptr_t)))
591da177e4SLinus Torvalds
601da177e4SLinus Torvalds/*
611da177e4SLinus Torvalds * These are to be used when we know the size of the block and
621da177e4SLinus Torvalds * we don't have a cursor.
631da177e4SLinus Torvalds */
64136341b4SChristoph Hellwig#define XFS_BMAP_BROOT_PTR_ADDR(mp, bb, i, sz) \
65136341b4SChristoph Hellwig	XFS_BMBT_PTR_ADDR(mp, bb, i, xfs_bmbt_maxrecs(mp, sz, 0))
661da177e4SLinus Torvalds
67ee1a47abSChristoph Hellwig#define XFS_BMAP_BROOT_SPACE_CALC(mp, nrecs) \
68ee1a47abSChristoph Hellwig	(int)(XFS_BMBT_BLOCK_LEN(mp) + \
69a844f451SNathan Scott	       ((nrecs) * (sizeof(xfs_bmbt_key_t) + sizeof(xfs_bmbt_ptr_t))))
70a844f451SNathan Scott
71ee1a47abSChristoph Hellwig#define XFS_BMAP_BROOT_SPACE(mp, bb) \
72ee1a47abSChristoph Hellwig	(XFS_BMAP_BROOT_SPACE_CALC(mp, be16_to_cpu((bb)->bb_numrecs)))
73a844f451SNathan Scott#define XFS_BMDR_SPACE_CALC(nrecs) \
74a844f451SNathan Scott	(int)(sizeof(xfs_bmdr_block_t) + \
75a844f451SNathan Scott	       ((nrecs) * (sizeof(xfs_bmbt_key_t) + sizeof(xfs_bmbt_ptr_t))))
76427d9fe2SEric Sandeen#define XFS_BMAP_BMDR_SPACE(bb) \
77427d9fe2SEric Sandeen	(XFS_BMDR_SPACE_CALC(be16_to_cpu((bb)->bb_numrecs)))
781da177e4SLinus Torvalds
791da177e4SLinus Torvalds/*
801da177e4SLinus Torvalds * Maximum number of bmap btree levels.
811da177e4SLinus Torvalds */
82a844f451SNathan Scott#define XFS_BM_MAXLEVELS(mp,w)		((mp)->m_bm_maxlevels[(w)])
831da177e4SLinus Torvalds
841da177e4SLinus Torvalds/*
851da177e4SLinus Torvalds * Prototypes for xfs_bmap.c to call.
861da177e4SLinus Torvalds */
87ee1a47abSChristoph Hellwigextern void xfs_bmdr_to_bmbt(struct xfs_inode *, xfs_bmdr_block_t *, int,
887cc95a82SChristoph Hellwig			struct xfs_btree_block *, int);
891da177e4SLinus Torvalds
90a67d00a5SChristoph Hellwigvoid xfs_bmbt_disk_set_all(struct xfs_bmbt_rec *r, struct xfs_bmbt_irec *s);
9123825cd1SDarrick J. Wongextern xfs_filblks_t xfs_bmbt_disk_get_blockcount(const struct xfs_bmbt_rec *r);
9223825cd1SDarrick J. Wongextern xfs_fileoff_t xfs_bmbt_disk_get_startoff(const struct xfs_bmbt_rec *r);
9322ece4e8SDarrick J. Wongvoid xfs_bmbt_disk_get_all(const struct xfs_bmbt_rec *r,
9422ece4e8SDarrick J. Wong		struct xfs_bmbt_irec *s);
951da177e4SLinus Torvalds
967cc95a82SChristoph Hellwigextern void xfs_bmbt_to_bmdr(struct xfs_mount *, struct xfs_btree_block *, int,
9760197e8dSChristoph Hellwig			xfs_bmdr_block_t *, int);
9860197e8dSChristoph Hellwig
9960197e8dSChristoph Hellwigextern int xfs_bmbt_get_maxrecs(struct xfs_btree_cur *, int level);
100152d93b7SEric Sandeenextern int xfs_bmdr_maxrecs(int blocklen, int leaf);
10160197e8dSChristoph Hellwigextern int xfs_bmbt_maxrecs(struct xfs_mount *, int blocklen, int leaf);
1021da177e4SLinus Torvalds
10321b5c978SDave Chinnerextern int xfs_bmbt_change_owner(struct xfs_trans *tp, struct xfs_inode *ip,
104638f4416SDave Chinner				 int whichfork, xfs_ino_t new_owner,
105638f4416SDave Chinner				 struct list_head *buffer_list);
10621b5c978SDave Chinner
107561f7d17SChristoph Hellwigextern struct xfs_btree_cur *xfs_bmbt_init_cursor(struct xfs_mount *,
108561f7d17SChristoph Hellwig		struct xfs_trans *, struct xfs_inode *, int);
109561f7d17SChristoph Hellwig
11014861c47SDarrick J. Wongextern unsigned long long xfs_bmbt_calc_size(struct xfs_mount *mp,
11114861c47SDarrick J. Wong		unsigned long long len);
11214861c47SDarrick J. Wong
1130ed5f735SDarrick J. Wongunsigned int xfs_bmbt_maxlevels_ondisk(void);
1140ed5f735SDarrick J. Wong
1159fa47bdcSDarrick J. Wongint __init xfs_bmbt_init_cur_cache(void);
1169fa47bdcSDarrick J. Wongvoid xfs_bmbt_destroy_cur_cache(void);
1179fa47bdcSDarrick J. Wong
1181da177e4SLinus Torvalds#endif	/* __XFS_BMAP_BTREE_H__ */
119