1 /*
2  * Taken from Linux kernel tree (linux/fs/xfs)
3  *
4  * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
5  * All Rights Reserved.
6  *
7  * Copyright (c) 2012 Paulo Alcantara <pcacjr@zytor.com>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it would be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write the Free Software Foundation,
20  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21  */
22 
23 #ifndef XFS_AG_H_
24 #define	XFS_AG_H_
25 
26 #include "xfs_types.h"
27 
28 /*
29  * Allocation group header
30  * This is divided into three structures, placed in sequential 512-byte
31  * buffers after a copy of the superblock (also in a 512-byte buffer).
32  */
33 
34 typedef uint32_t xfs_agino_t;
35 
36 struct xfs_buf;
37 struct xfs_mount;
38 struct xfs_trans;
39 
40 #define	XFS_AGF_MAGIC	"XAGF"
41 #define	XFS_AGF_VERSION	1
42 #define	XFS_AGI_VERSION	1
43 
44 #define	XFS_AGF_GOOD_VERSION(v)	((v) == XFS_AGF_VERSION)
45 #define	XFS_AGI_GOOD_VERSION(v)	((v) == XFS_AGI_VERSION)
46 
47 /*
48  * Btree number 0 is bno, 1 is cnt.  This value gives the size of the
49  * arrays below.
50  */
51 #define	XFS_BTNUM_AGF	((int)XFS_BTNUM_CNTi + 1)
52 
53 /*
54  * The second word of agf_levels in the first a.g. overlaps the EFS
55  * superblock's magic number.  Since the magic numbers valid for EFS
56  * are > 64k, our value cannot be confused for an EFS superblock's.
57  */
58 
59 typedef struct xfs_agf {
60 	/*
61 	 * Common allocation group header information
62 	 */
63 	uint32_t		agf_magicnum;	/* magic number == XFS_AGF_MAGIC */
64 	uint32_t		agf_versionnum;	/* header version == XFS_AGF_VERSION */
65 	uint32_t		agf_seqno;	/* sequence # starting from 0 */
66 	uint32_t		agf_length;	/* size in blocks of a.g. */
67 	/*
68 	 * Freespace information
69 	 */
70 	uint32_t		agf_roots[XFS_BTNUM_AGF];	/* root blocks */
71 	uint32_t		agf_spare0;	/* spare field */
72 	uint32_t		agf_levels[XFS_BTNUM_AGF];	/* btree levels */
73 	uint32_t		agf_spare1;	/* spare field */
74 	uint32_t		agf_flfirst;	/* first freelist block's index */
75 	uint32_t		agf_fllast;	/* last freelist block's index */
76 	uint32_t		agf_flcount;	/* count of blocks in freelist */
77 	uint32_t		agf_freeblks;	/* total free blocks */
78 	uint32_t		agf_longest;	/* longest free space */
79 	uint32_t		agf_btreeblks;	/* # of blocks held in AGF btrees */
80 } xfs_agf_t;
81 
82 #define	XFS_AGF_MAGICNUM	0x00000001
83 #define	XFS_AGF_VERSIONNUM	0x00000002
84 #define	XFS_AGF_SEQNO		0x00000004
85 #define	XFS_AGF_LENGTH		0x00000008
86 #define	XFS_AGF_ROOTS		0x00000010
87 #define	XFS_AGF_LEVELS		0x00000020
88 #define	XFS_AGF_FLFIRST		0x00000040
89 #define	XFS_AGF_FLLAST		0x00000080
90 #define	XFS_AGF_FLCOUNT		0x00000100
91 #define	XFS_AGF_FREEBLKS	0x00000200
92 #define	XFS_AGF_LONGEST		0x00000400
93 #define	XFS_AGF_BTREEBLKS	0x00000800
94 #define	XFS_AGF_NUM_BITS	12
95 #define	XFS_AGF_ALL_BITS	((1 << XFS_AGF_NUM_BITS) - 1)
96 
97 #define XFS_AGF_FLAGS \
98 	{ XFS_AGF_MAGICNUM,	"MAGICNUM" }, \
99 	{ XFS_AGF_VERSIONNUM,	"VERSIONNUM" }, \
100 	{ XFS_AGF_SEQNO,	"SEQNO" }, \
101 	{ XFS_AGF_LENGTH,	"LENGTH" }, \
102 	{ XFS_AGF_ROOTS,	"ROOTS" }, \
103 	{ XFS_AGF_LEVELS,	"LEVELS" }, \
104 	{ XFS_AGF_FLFIRST,	"FLFIRST" }, \
105 	{ XFS_AGF_FLLAST,	"FLLAST" }, \
106 	{ XFS_AGF_FLCOUNT,	"FLCOUNT" }, \
107 	{ XFS_AGF_FREEBLKS,	"FREEBLKS" }, \
108 	{ XFS_AGF_LONGEST,	"LONGEST" }, \
109 	{ XFS_AGF_BTREEBLKS,	"BTREEBLKS" }
110 
111 /* disk block (xfs_daddr_t) in the AG */
112 #define XFS_AGF_DADDR(mp)	((xfs_daddr_t)(1 << (mp)->m_sectbb_log))
113 #define	XFS_AGF_BLOCK(mp)	XFS_HDR_BLOCK(mp, XFS_AGF_DADDR(mp))
114 #define	XFS_BUF_TO_AGF(bp)	((xfs_agf_t *)((bp)->b_addr))
115 
116 extern int xfs_read_agf(struct xfs_mount *mp, struct xfs_trans *tp,
117 			xfs_agnumber_t agno, int flags, struct xfs_buf **bpp);
118 
119 /*
120  * Size of the unlinked inode hash table in the agi.
121  */
122 #define	XFS_AGI_UNLINKED_BUCKETS	64
123 
124 #define	XFS_AGI_MAGICNUM	0x00000001
125 #define	XFS_AGI_VERSIONNUM	0x00000002
126 #define	XFS_AGI_SEQNO		0x00000004
127 #define	XFS_AGI_LENGTH		0x00000008
128 #define	XFS_AGI_COUNT		0x00000010
129 #define	XFS_AGI_ROOT		0x00000020
130 #define	XFS_AGI_LEVEL		0x00000040
131 #define	XFS_AGI_FREECOUNT	0x00000080
132 #define	XFS_AGI_NEWINO		0x00000100
133 #define	XFS_AGI_DIRINO		0x00000200
134 #define	XFS_AGI_UNLINKED	0x00000400
135 #define	XFS_AGI_NUM_BITS	11
136 #define	XFS_AGI_ALL_BITS	((1 << XFS_AGI_NUM_BITS) - 1)
137 
138 /* disk block (xfs_daddr_t) in the AG */
139 #define XFS_AGI_DADDR(mp)	((xfs_daddr_t)(2 << (mp)->m_sectbb_log))
140 #define	XFS_AGI_BLOCK(mp)	XFS_HDR_BLOCK(mp, XFS_AGI_DADDR(mp))
141 #define	XFS_BUF_TO_AGI(bp)	((xfs_agi_t *)((bp)->b_addr))
142 
143 extern int xfs_read_agi(struct xfs_mount *mp, struct xfs_trans *tp,
144 				xfs_agnumber_t agno, struct xfs_buf **bpp);
145 
146 /*
147  * The third a.g. block contains the a.g. freelist, an array
148  * of block pointers to blocks owned by the allocation btree code.
149  */
150 #define XFS_AGFL_DADDR(mp)	((xfs_daddr_t)(3 << (mp)->m_sectbb_log))
151 #define	XFS_AGFL_BLOCK(mp)	XFS_HDR_BLOCK(mp, XFS_AGFL_DADDR(mp))
152 #define XFS_AGFL_SIZE(mp)	((mp)->m_sb.sb_sectsize / sizeof(xfs_agblock_t))
153 #define	XFS_BUF_TO_AGFL(bp)	((xfs_agfl_t *)((bp)->b_addr))
154 
155 typedef struct xfs_agfl {
156 	uint32_t		agfl_bno[1];	/* actually XFS_AGFL_SIZE(mp) */
157 } xfs_agfl_t;
158 
159 /*
160  * tags for inode radix tree
161  */
162 #define XFS_ICI_NO_TAG		(-1)	/* special flag for an untagged lookup
163 					   in xfs_inode_ag_iterator */
164 #define XFS_ICI_RECLAIM_TAG	0	/* inode is to be reclaimed */
165 
166 #define	XFS_AG_MAXLEVELS(mp)		((mp)->m_ag_maxlevels)
167 #define	XFS_MIN_FREELIST_RAW(bl,cl,mp)	\
168 	(MIN(bl + 1, XFS_AG_MAXLEVELS(mp)) + MIN(cl + 1, XFS_AG_MAXLEVELS(mp)))
169 #define	XFS_MIN_FREELIST(a,mp)		\
170 	(XFS_MIN_FREELIST_RAW(		\
171 		be32_to_cpu((a)->agf_levels[XFS_BTNUM_BNOi]), \
172 		be32_to_cpu((a)->agf_levels[XFS_BTNUM_CNTi]), mp))
173 #define	XFS_MIN_FREELIST_PAG(pag,mp)	\
174 	(XFS_MIN_FREELIST_RAW(		\
175 		(unsigned int)(pag)->pagf_levels[XFS_BTNUM_BNOi], \
176 		(unsigned int)(pag)->pagf_levels[XFS_BTNUM_CNTi], mp))
177 
178 /*
179  * For checking for bad ranges of xfs_daddr_t's, covering multiple
180  * allocation groups or a single xfs_daddr_t that's a superblock copy.
181  */
182 #define	XFS_AG_CHECK_DADDR(mp,d,len)	\
183 	((len) == 1 ? \
184 	    ASSERT((d) == XFS_SB_DADDR || \
185 		   xfs_daddr_to_agbno(mp, d) != XFS_SB_DADDR) : \
186 	    ASSERT(xfs_daddr_to_agno(mp, d) == \
187 		   xfs_daddr_to_agno(mp, (d) + (len) - 1)))
188 
189 #endif	/* XFS_AG_H_ */
190