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_ALLOC_H__
19 #define	__XFS_ALLOC_H__
20 
21 struct xfs_buf;
22 struct xfs_btree_cur;
23 struct xfs_mount;
24 struct xfs_perag;
25 struct xfs_trans;
26 struct xfs_busy_extent;
27 
28 /*
29  * Freespace allocation types.  Argument to xfs_alloc_[v]extent.
30  */
31 #define XFS_ALLOCTYPE_ANY_AG	0x01	/* allocate anywhere, use rotor */
32 #define XFS_ALLOCTYPE_FIRST_AG	0x02	/* ... start at ag 0 */
33 #define XFS_ALLOCTYPE_START_AG	0x04	/* anywhere, start in this a.g. */
34 #define XFS_ALLOCTYPE_THIS_AG	0x08	/* anywhere in this a.g. */
35 #define XFS_ALLOCTYPE_START_BNO	0x10	/* near this block else anywhere */
36 #define XFS_ALLOCTYPE_NEAR_BNO	0x20	/* in this a.g. and near this block */
37 #define XFS_ALLOCTYPE_THIS_BNO	0x40	/* at exactly this block */
38 
39 /* this should become an enum again when the tracing code is fixed */
40 typedef unsigned int xfs_alloctype_t;
41 
42 #define XFS_ALLOC_TYPES \
43 	{ XFS_ALLOCTYPE_ANY_AG,		"ANY_AG" }, \
44 	{ XFS_ALLOCTYPE_FIRST_AG,	"FIRST_AG" }, \
45 	{ XFS_ALLOCTYPE_START_AG,	"START_AG" }, \
46 	{ XFS_ALLOCTYPE_THIS_AG,	"THIS_AG" }, \
47 	{ XFS_ALLOCTYPE_START_BNO,	"START_BNO" }, \
48 	{ XFS_ALLOCTYPE_NEAR_BNO,	"NEAR_BNO" }, \
49 	{ XFS_ALLOCTYPE_THIS_BNO,	"THIS_BNO" }
50 
51 /*
52  * Flags for xfs_alloc_fix_freelist.
53  */
54 #define	XFS_ALLOC_FLAG_TRYLOCK	0x00000001  /* use trylock for buffer locking */
55 #define	XFS_ALLOC_FLAG_FREEING	0x00000002  /* indicate caller is freeing extents*/
56 
57 /*
58  * In order to avoid ENOSPC-related deadlock caused by
59  * out-of-order locking of AGF buffer (PV 947395), we place
60  * constraints on the relationship among actual allocations for
61  * data blocks, freelist blocks, and potential file data bmap
62  * btree blocks. However, these restrictions may result in no
63  * actual space allocated for a delayed extent, for example, a data
64  * block in a certain AG is allocated but there is no additional
65  * block for the additional bmap btree block due to a split of the
66  * bmap btree of the file. The result of this may lead to an
67  * infinite loop in xfssyncd when the file gets flushed to disk and
68  * all delayed extents need to be actually allocated. To get around
69  * this, we explicitly set aside a few blocks which will not be
70  * reserved in delayed allocation. Considering the minimum number of
71  * needed freelist blocks is 4 fsbs _per AG_, a potential split of file's bmap
72  * btree requires 1 fsb, so we set the number of set-aside blocks
73  * to 4 + 4*agcount.
74  */
75 #define XFS_ALLOC_SET_ASIDE(mp)  (4 + ((mp)->m_sb.sb_agcount * 4))
76 
77 /*
78  * When deciding how much space to allocate out of an AG, we limit the
79  * allocation maximum size to the size the AG. However, we cannot use all the
80  * blocks in the AG - some are permanently used by metadata. These
81  * blocks are generally:
82  *	- the AG superblock, AGF, AGI and AGFL
83  *	- the AGF (bno and cnt) and AGI btree root blocks
84  *	- 4 blocks on the AGFL according to XFS_ALLOC_SET_ASIDE() limits
85  *
86  * The AG headers are sector sized, so the amount of space they take up is
87  * dependent on filesystem geometry. The others are all single blocks.
88  */
89 #define XFS_ALLOC_AG_MAX_USABLE(mp)	\
90 	((mp)->m_sb.sb_agblocks - XFS_BB_TO_FSB(mp, XFS_FSS_TO_BB(mp, 4)) - 7)
91 
92 
93 /*
94  * Argument structure for xfs_alloc routines.
95  * This is turned into a structure to avoid having 20 arguments passed
96  * down several levels of the stack.
97  */
98 typedef struct xfs_alloc_arg {
99 	struct xfs_trans *tp;		/* transaction pointer */
100 	struct xfs_mount *mp;		/* file system mount point */
101 	struct xfs_buf	*agbp;		/* buffer for a.g. freelist header */
102 	struct xfs_perag *pag;		/* per-ag struct for this agno */
103 	xfs_fsblock_t	fsbno;		/* file system block number */
104 	xfs_agnumber_t	agno;		/* allocation group number */
105 	xfs_agblock_t	agbno;		/* allocation group-relative block # */
106 	xfs_extlen_t	minlen;		/* minimum size of extent */
107 	xfs_extlen_t	maxlen;		/* maximum size of extent */
108 	xfs_extlen_t	mod;		/* mod value for extent size */
109 	xfs_extlen_t	prod;		/* prod value for extent size */
110 	xfs_extlen_t	minleft;	/* min blocks must be left after us */
111 	xfs_extlen_t	total;		/* total blocks needed in xaction */
112 	xfs_extlen_t	alignment;	/* align answer to multiple of this */
113 	xfs_extlen_t	minalignslop;	/* slop for minlen+alignment calcs */
114 	xfs_extlen_t	len;		/* output: actual size of extent */
115 	xfs_alloctype_t	type;		/* allocation type XFS_ALLOCTYPE_... */
116 	xfs_alloctype_t	otype;		/* original allocation type */
117 	char		wasdel;		/* set if allocation was prev delayed */
118 	char		wasfromfl;	/* set if allocation is from freelist */
119 	char		isfl;		/* set if is freelist blocks - !acctg */
120 	char		userdata;	/* set if this is user data */
121 	xfs_fsblock_t	firstblock;	/* io first block allocated */
122 } xfs_alloc_arg_t;
123 
124 /*
125  * Defines for userdata
126  */
127 #define XFS_ALLOC_USERDATA		1	/* allocation is for user data*/
128 #define XFS_ALLOC_INITIAL_USER_DATA	2	/* special case start of file */
129 
130 /*
131  * Find the length of the longest extent in an AG.
132  */
133 xfs_extlen_t
134 xfs_alloc_longest_free_extent(struct xfs_mount *mp,
135 		struct xfs_perag *pag);
136 
137 #ifdef __KERNEL__
138 void
139 xfs_alloc_busy_insert(struct xfs_trans *tp, xfs_agnumber_t agno,
140 	xfs_agblock_t bno, xfs_extlen_t len);
141 
142 void
143 xfs_alloc_busy_clear(struct xfs_mount *mp, struct xfs_busy_extent *busyp);
144 
145 int
146 xfs_alloc_busy_search(struct xfs_mount *mp, xfs_agnumber_t agno,
147 	xfs_agblock_t bno, xfs_extlen_t len);
148 #endif	/* __KERNEL__ */
149 
150 /*
151  * Compute and fill in value of m_ag_maxlevels.
152  */
153 void
154 xfs_alloc_compute_maxlevels(
155 	struct xfs_mount	*mp);	/* file system mount structure */
156 
157 /*
158  * Get a block from the freelist.
159  * Returns with the buffer for the block gotten.
160  */
161 int				/* error */
162 xfs_alloc_get_freelist(
163 	struct xfs_trans *tp,	/* transaction pointer */
164 	struct xfs_buf	*agbp,	/* buffer containing the agf structure */
165 	xfs_agblock_t	*bnop,	/* block address retrieved from freelist */
166 	int		btreeblk); /* destination is a AGF btree */
167 
168 /*
169  * Log the given fields from the agf structure.
170  */
171 void
172 xfs_alloc_log_agf(
173 	struct xfs_trans *tp,	/* transaction pointer */
174 	struct xfs_buf	*bp,	/* buffer for a.g. freelist header */
175 	int		fields);/* mask of fields to be logged (XFS_AGF_...) */
176 
177 /*
178  * Interface for inode allocation to force the pag data to be initialized.
179  */
180 int				/* error */
181 xfs_alloc_pagf_init(
182 	struct xfs_mount *mp,	/* file system mount structure */
183 	struct xfs_trans *tp,	/* transaction pointer */
184 	xfs_agnumber_t	agno,	/* allocation group number */
185 	int		flags);	/* XFS_ALLOC_FLAGS_... */
186 
187 /*
188  * Put the block on the freelist for the allocation group.
189  */
190 int				/* error */
191 xfs_alloc_put_freelist(
192 	struct xfs_trans *tp,	/* transaction pointer */
193 	struct xfs_buf	*agbp,	/* buffer for a.g. freelist header */
194 	struct xfs_buf	*agflbp,/* buffer for a.g. free block array */
195 	xfs_agblock_t	bno,	/* block being freed */
196 	int		btreeblk); /* owner was a AGF btree */
197 
198 /*
199  * Read in the allocation group header (free/alloc section).
200  */
201 int					/* error  */
202 xfs_alloc_read_agf(
203 	struct xfs_mount *mp,		/* mount point structure */
204 	struct xfs_trans *tp,		/* transaction pointer */
205 	xfs_agnumber_t	agno,		/* allocation group number */
206 	int		flags,		/* XFS_ALLOC_FLAG_... */
207 	struct xfs_buf	**bpp);		/* buffer for the ag freelist header */
208 
209 /*
210  * Allocate an extent (variable-size).
211  */
212 int				/* error */
213 xfs_alloc_vextent(
214 	xfs_alloc_arg_t	*args);	/* allocation argument structure */
215 
216 /*
217  * Free an extent.
218  */
219 int				/* error */
220 xfs_free_extent(
221 	struct xfs_trans *tp,	/* transaction pointer */
222 	xfs_fsblock_t	bno,	/* starting block number of extent */
223 	xfs_extlen_t	len);	/* length of extent */
224 
225 int					/* error */
226 xfs_alloc_lookup_le(
227 	struct xfs_btree_cur	*cur,	/* btree cursor */
228 	xfs_agblock_t		bno,	/* starting block of extent */
229 	xfs_extlen_t		len,	/* length of extent */
230 	int			*stat);	/* success/failure */
231 
232 int					/* error */
233 xfs_alloc_get_rec(
234 	struct xfs_btree_cur	*cur,	/* btree cursor */
235 	xfs_agblock_t		*bno,	/* output: starting block of extent */
236 	xfs_extlen_t		*len,	/* output: length of extent */
237 	int			*stat);	/* output: success/failure */
238 
239 #endif	/* __XFS_ALLOC_H__ */
240