xref: /openbmc/linux/fs/xfs/libxfs/xfs_bmap_btree.h (revision 2359ccdd)
1 /*
2  * Copyright (c) 2000,2002-2005 Silicon Graphics, Inc.
3  * All Rights Reserved.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it would be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write the Free Software Foundation,
16  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18 #ifndef __XFS_BMAP_BTREE_H__
19 #define __XFS_BMAP_BTREE_H__
20 
21 struct xfs_btree_cur;
22 struct xfs_btree_block;
23 struct xfs_mount;
24 struct xfs_inode;
25 struct xfs_trans;
26 
27 /*
28  * Btree block header size depends on a superblock flag.
29  */
30 #define XFS_BMBT_BLOCK_LEN(mp) \
31 	(xfs_sb_version_hascrc(&((mp)->m_sb)) ? \
32 		XFS_BTREE_LBLOCK_CRC_LEN : XFS_BTREE_LBLOCK_LEN)
33 
34 #define XFS_BMBT_REC_ADDR(mp, block, index) \
35 	((xfs_bmbt_rec_t *) \
36 		((char *)(block) + \
37 		 XFS_BMBT_BLOCK_LEN(mp) + \
38 		 ((index) - 1) * sizeof(xfs_bmbt_rec_t)))
39 
40 #define XFS_BMBT_KEY_ADDR(mp, block, index) \
41 	((xfs_bmbt_key_t *) \
42 		((char *)(block) + \
43 		 XFS_BMBT_BLOCK_LEN(mp) + \
44 		 ((index) - 1) * sizeof(xfs_bmbt_key_t)))
45 
46 #define XFS_BMBT_PTR_ADDR(mp, block, index, maxrecs) \
47 	((xfs_bmbt_ptr_t *) \
48 		((char *)(block) + \
49 		 XFS_BMBT_BLOCK_LEN(mp) + \
50 		 (maxrecs) * sizeof(xfs_bmbt_key_t) + \
51 		 ((index) - 1) * sizeof(xfs_bmbt_ptr_t)))
52 
53 #define XFS_BMDR_REC_ADDR(block, index) \
54 	((xfs_bmdr_rec_t *) \
55 		((char *)(block) + \
56 		 sizeof(struct xfs_bmdr_block) + \
57 	         ((index) - 1) * sizeof(xfs_bmdr_rec_t)))
58 
59 #define XFS_BMDR_KEY_ADDR(block, index) \
60 	((xfs_bmdr_key_t *) \
61 		((char *)(block) + \
62 		 sizeof(struct xfs_bmdr_block) + \
63 		 ((index) - 1) * sizeof(xfs_bmdr_key_t)))
64 
65 #define XFS_BMDR_PTR_ADDR(block, index, maxrecs) \
66 	((xfs_bmdr_ptr_t *) \
67 		((char *)(block) + \
68 		 sizeof(struct xfs_bmdr_block) + \
69 		 (maxrecs) * sizeof(xfs_bmdr_key_t) + \
70 		 ((index) - 1) * sizeof(xfs_bmdr_ptr_t)))
71 
72 /*
73  * These are to be used when we know the size of the block and
74  * we don't have a cursor.
75  */
76 #define XFS_BMAP_BROOT_PTR_ADDR(mp, bb, i, sz) \
77 	XFS_BMBT_PTR_ADDR(mp, bb, i, xfs_bmbt_maxrecs(mp, sz, 0))
78 
79 #define XFS_BMAP_BROOT_SPACE_CALC(mp, nrecs) \
80 	(int)(XFS_BMBT_BLOCK_LEN(mp) + \
81 	       ((nrecs) * (sizeof(xfs_bmbt_key_t) + sizeof(xfs_bmbt_ptr_t))))
82 
83 #define XFS_BMAP_BROOT_SPACE(mp, bb) \
84 	(XFS_BMAP_BROOT_SPACE_CALC(mp, be16_to_cpu((bb)->bb_numrecs)))
85 #define XFS_BMDR_SPACE_CALC(nrecs) \
86 	(int)(sizeof(xfs_bmdr_block_t) + \
87 	       ((nrecs) * (sizeof(xfs_bmbt_key_t) + sizeof(xfs_bmbt_ptr_t))))
88 #define XFS_BMAP_BMDR_SPACE(bb) \
89 	(XFS_BMDR_SPACE_CALC(be16_to_cpu((bb)->bb_numrecs)))
90 
91 /*
92  * Maximum number of bmap btree levels.
93  */
94 #define XFS_BM_MAXLEVELS(mp,w)		((mp)->m_bm_maxlevels[(w)])
95 
96 /*
97  * Prototypes for xfs_bmap.c to call.
98  */
99 extern void xfs_bmdr_to_bmbt(struct xfs_inode *, xfs_bmdr_block_t *, int,
100 			struct xfs_btree_block *, int);
101 
102 void xfs_bmbt_disk_set_all(struct xfs_bmbt_rec *r, struct xfs_bmbt_irec *s);
103 extern xfs_filblks_t xfs_bmbt_disk_get_blockcount(xfs_bmbt_rec_t *r);
104 extern xfs_fileoff_t xfs_bmbt_disk_get_startoff(xfs_bmbt_rec_t *r);
105 extern void xfs_bmbt_disk_get_all(xfs_bmbt_rec_t *r, xfs_bmbt_irec_t *s);
106 
107 extern void xfs_bmbt_to_bmdr(struct xfs_mount *, struct xfs_btree_block *, int,
108 			xfs_bmdr_block_t *, int);
109 
110 extern int xfs_bmbt_get_maxrecs(struct xfs_btree_cur *, int level);
111 extern int xfs_bmdr_maxrecs(int blocklen, int leaf);
112 extern int xfs_bmbt_maxrecs(struct xfs_mount *, int blocklen, int leaf);
113 
114 extern int xfs_bmbt_change_owner(struct xfs_trans *tp, struct xfs_inode *ip,
115 				 int whichfork, xfs_ino_t new_owner,
116 				 struct list_head *buffer_list);
117 
118 extern struct xfs_btree_cur *xfs_bmbt_init_cursor(struct xfs_mount *,
119 		struct xfs_trans *, struct xfs_inode *, int);
120 
121 #endif	/* __XFS_BMAP_BTREE_H__ */
122