1 /*
2  * Copyright (c) 2000-2003 Silicon Graphics, Inc.  All Rights Reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of version 2 of the GNU General Public License as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it would be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  *
12  * Further, this software is distributed without any warranty that it is
13  * free of the rightful claim of any third person regarding infringement
14  * or the like.  Any license provided herein, whether implied or
15  * otherwise, applies only to this software file.  Patent licenses, if
16  * any, provided herein do not apply to combinations of this program with
17  * other software, or any other product whatsoever.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write the Free Software Foundation, Inc., 59
21  * Temple Place - Suite 330, Boston MA 02111-1307, USA.
22  *
23  * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24  * Mountain View, CA  94043, or:
25  *
26  * http://www.sgi.com
27  *
28  * For further information regarding this notice, see:
29  *
30  * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31  */
32 #ifndef __XFS_BMAP_H__
33 #define	__XFS_BMAP_H__
34 
35 struct getbmap;
36 struct xfs_bmbt_irec;
37 struct xfs_inode;
38 struct xfs_mount;
39 struct xfs_trans;
40 
41 /*
42  * List of extents to be free "later".
43  * The list is kept sorted on xbf_startblock.
44  */
45 typedef struct xfs_bmap_free_item
46 {
47 	xfs_fsblock_t		xbfi_startblock;/* starting fs block number */
48 	xfs_extlen_t		xbfi_blockcount;/* number of blocks in extent */
49 	struct xfs_bmap_free_item *xbfi_next;	/* link to next entry */
50 } xfs_bmap_free_item_t;
51 
52 /*
53  * Header for free extent list.
54  */
55 typedef	struct xfs_bmap_free
56 {
57 	xfs_bmap_free_item_t	*xbf_first;	/* list of to-be-free extents */
58 	int			xbf_count;	/* count of items on list */
59 	int			xbf_low;	/* kludge: alloc in low mode */
60 } xfs_bmap_free_t;
61 
62 #define	XFS_BMAP_MAX_NMAP	4
63 
64 /*
65  * Flags for xfs_bmapi
66  */
67 #define	XFS_BMAPI_WRITE		0x001	/* write operation: allocate space */
68 #define XFS_BMAPI_DELAY		0x002	/* delayed write operation */
69 #define XFS_BMAPI_ENTIRE	0x004	/* return entire extent, not trimmed */
70 #define XFS_BMAPI_METADATA	0x008	/* mapping metadata not user data */
71 #define XFS_BMAPI_EXACT		0x010	/* allocate only to spec'd bounds */
72 #define XFS_BMAPI_ATTRFORK	0x020	/* use attribute fork not data */
73 #define XFS_BMAPI_ASYNC		0x040	/* bunmapi xactions can be async */
74 #define XFS_BMAPI_RSVBLOCKS	0x080	/* OK to alloc. reserved data blocks */
75 #define	XFS_BMAPI_PREALLOC	0x100	/* preallocation op: unwritten space */
76 #define	XFS_BMAPI_IGSTATE	0x200	/* Ignore state - */
77 					/* combine contig. space */
78 #define	XFS_BMAPI_CONTIG	0x400	/* must allocate only one extent */
79 
80 #if XFS_WANT_FUNCS || (XFS_WANT_SPACE && XFSSO_XFS_BMAPI_AFLAG)
81 int xfs_bmapi_aflag(int w);
82 #define	XFS_BMAPI_AFLAG(w)	xfs_bmapi_aflag(w)
83 #else
84 #define	XFS_BMAPI_AFLAG(w)	((w) == XFS_ATTR_FORK ? XFS_BMAPI_ATTRFORK : 0)
85 #endif
86 
87 /*
88  * Special values for xfs_bmbt_irec_t br_startblock field.
89  */
90 #define	DELAYSTARTBLOCK		((xfs_fsblock_t)-1LL)
91 #define	HOLESTARTBLOCK		((xfs_fsblock_t)-2LL)
92 
93 #if XFS_WANT_FUNCS || (XFS_WANT_SPACE && XFSSO_XFS_BMAP_INIT)
94 void xfs_bmap_init(xfs_bmap_free_t *flp, xfs_fsblock_t *fbp);
95 #define	XFS_BMAP_INIT(flp,fbp)	xfs_bmap_init(flp,fbp)
96 #else
97 #define	XFS_BMAP_INIT(flp,fbp)	\
98 	((flp)->xbf_first = NULL, (flp)->xbf_count = 0, \
99 	 (flp)->xbf_low = 0, *(fbp) = NULLFSBLOCK)
100 #endif
101 
102 /*
103  * Argument structure for xfs_bmap_alloc.
104  */
105 typedef struct xfs_bmalloca {
106 	xfs_fsblock_t		firstblock; /* i/o first block allocated */
107 	xfs_fsblock_t		rval;	/* starting block of new extent */
108 	xfs_fileoff_t		off;	/* offset in file filling in */
109 	struct xfs_trans	*tp;	/* transaction pointer */
110 	struct xfs_inode	*ip;	/* incore inode pointer */
111 	struct xfs_bmbt_irec	*prevp;	/* extent before the new one */
112 	struct xfs_bmbt_irec	*gotp;	/* extent after, or delayed */
113 	xfs_extlen_t		alen;	/* i/o length asked/allocated */
114 	xfs_extlen_t		total;	/* total blocks needed for xaction */
115 	xfs_extlen_t		minlen;	/* mininum allocation size (blocks) */
116 	xfs_extlen_t		minleft; /* amount must be left after alloc */
117 	char			eof;	/* set if allocating past last extent */
118 	char			wasdel;	/* replacing a delayed allocation */
119 	char			userdata;/* set if is user data */
120 	char			low;	/* low on space, using seq'l ags */
121 	char			aeof;   /* allocated space at eof */
122 } xfs_bmalloca_t;
123 
124 #ifdef __KERNEL__
125 
126 #if defined(XFS_BMAP_TRACE)
127 /*
128  * Trace operations for bmap extent tracing
129  */
130 #define	XFS_BMAP_KTRACE_DELETE	1
131 #define	XFS_BMAP_KTRACE_INSERT	2
132 #define	XFS_BMAP_KTRACE_PRE_UP	3
133 #define	XFS_BMAP_KTRACE_POST_UP	4
134 
135 #define	XFS_BMAP_TRACE_SIZE	4096	/* size of global trace buffer */
136 #define	XFS_BMAP_KTRACE_SIZE	32	/* size of per-inode trace buffer */
137 extern ktrace_t	*xfs_bmap_trace_buf;
138 
139 /*
140  * Add bmap trace insert entries for all the contents of the extent list.
141  */
142 void
143 xfs_bmap_trace_exlist(
144 	char			*fname,		/* function name */
145 	struct xfs_inode	*ip,		/* incore inode pointer */
146 	xfs_extnum_t		cnt,		/* count of entries in list */
147 	int			whichfork);	/* data or attr fork */
148 #else
149 #define	xfs_bmap_trace_exlist(f,ip,c,w)
150 #endif
151 
152 /*
153  * Convert inode from non-attributed to attributed.
154  * Must not be in a transaction, ip must not be locked.
155  */
156 int					/* error code */
157 xfs_bmap_add_attrfork(
158 	struct xfs_inode	*ip,	/* incore inode pointer */
159 	int					rsvd);	/* flag for reserved block allocation */
160 
161 /*
162  * Add the extent to the list of extents to be free at transaction end.
163  * The list is maintained sorted (by block number).
164  */
165 void
166 xfs_bmap_add_free(
167 	xfs_fsblock_t		bno,		/* fs block number of extent */
168 	xfs_filblks_t		len,		/* length of extent */
169 	xfs_bmap_free_t		*flist,		/* list of extents */
170 	struct xfs_mount	*mp);		/* mount point structure */
171 
172 /*
173  * Routine to clean up the free list data structure when
174  * an error occurs during a transaction.
175  */
176 void
177 xfs_bmap_cancel(
178 	xfs_bmap_free_t		*flist);	/* free list to clean up */
179 
180 /*
181  * Compute and fill in the value of the maximum depth of a bmap btree
182  * in this filesystem.  Done once, during mount.
183  */
184 void
185 xfs_bmap_compute_maxlevels(
186 	struct xfs_mount	*mp,	/* file system mount structure */
187 	int			whichfork);	/* data or attr fork */
188 
189 /*
190  * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
191  * caller.  Frees all the extents that need freeing, which must be done
192  * last due to locking considerations.
193  *
194  * Return 1 if the given transaction was committed and a new one allocated,
195  * and 0 otherwise.
196  */
197 int						/* error */
198 xfs_bmap_finish(
199 	struct xfs_trans	**tp,		/* transaction pointer addr */
200 	xfs_bmap_free_t		*flist,		/* i/o: list extents to free */
201 	xfs_fsblock_t		firstblock,	/* controlled a.g. for allocs */
202 	int			*committed);	/* xact committed or not */
203 
204 /*
205  * Returns the file-relative block number of the first unused block in the file.
206  * This is the lowest-address hole if the file has holes, else the first block
207  * past the end of file.
208  */
209 int						/* error */
210 xfs_bmap_first_unused(
211 	struct xfs_trans	*tp,		/* transaction pointer */
212 	struct xfs_inode	*ip,		/* incore inode */
213 	xfs_extlen_t		len,		/* size of hole to find */
214 	xfs_fileoff_t		*unused,	/* unused block num */
215 	int			whichfork);	/* data or attr fork */
216 
217 /*
218  * Returns the file-relative block number of the last block + 1 before
219  * last_block (input value) in the file.
220  * This is not based on i_size, it is based on the extent list.
221  * Returns 0 for local files, as they do not have an extent list.
222  */
223 int						/* error */
224 xfs_bmap_last_before(
225 	struct xfs_trans	*tp,		/* transaction pointer */
226 	struct xfs_inode	*ip,		/* incore inode */
227 	xfs_fileoff_t		*last_block,	/* last block */
228 	int			whichfork);	/* data or attr fork */
229 
230 /*
231  * Returns the file-relative block number of the first block past eof in
232  * the file.  This is not based on i_size, it is based on the extent list.
233  * Returns 0 for local files, as they do not have an extent list.
234  */
235 int						/* error */
236 xfs_bmap_last_offset(
237 	struct xfs_trans	*tp,		/* transaction pointer */
238 	struct xfs_inode	*ip,		/* incore inode */
239 	xfs_fileoff_t		*unused,	/* last block num */
240 	int			whichfork);	/* data or attr fork */
241 
242 /*
243  * Returns whether the selected fork of the inode has exactly one
244  * block or not.  For the data fork we check this matches di_size,
245  * implying the file's range is 0..bsize-1.
246  */
247 int
248 xfs_bmap_one_block(
249 	struct xfs_inode	*ip,		/* incore inode */
250 	int			whichfork);	/* data or attr fork */
251 
252 /*
253  * Read in the extents to iu_extents.
254  * All inode fields are set up by caller, we just traverse the btree
255  * and copy the records in.
256  */
257 int						/* error */
258 xfs_bmap_read_extents(
259 	struct xfs_trans	*tp,		/* transaction pointer */
260 	struct xfs_inode	*ip,		/* incore inode */
261 	int			whichfork);	/* data or attr fork */
262 
263 /*
264  * Map file blocks to filesystem blocks.
265  * File range is given by the bno/len pair.
266  * Adds blocks to file if a write ("flags & XFS_BMAPI_WRITE" set)
267  * into a hole or past eof.
268  * Only allocates blocks from a single allocation group,
269  * to avoid locking problems.
270  * The returned value in "firstblock" from the first call in a transaction
271  * must be remembered and presented to subsequent calls in "firstblock".
272  * An upper bound for the number of blocks to be allocated is supplied to
273  * the first call in "total"; if no allocation group has that many free
274  * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
275  */
276 int						/* error */
277 xfs_bmapi(
278 	struct xfs_trans	*tp,		/* transaction pointer */
279 	struct xfs_inode	*ip,		/* incore inode */
280 	xfs_fileoff_t		bno,		/* starting file offs. mapped */
281 	xfs_filblks_t		len,		/* length to map in file */
282 	int			flags,		/* XFS_BMAPI_... */
283 	xfs_fsblock_t		*firstblock,	/* first allocated block
284 						   controls a.g. for allocs */
285 	xfs_extlen_t		total,		/* total blocks needed */
286 	struct xfs_bmbt_irec	*mval,		/* output: map values */
287 	int			*nmap,		/* i/o: mval size/count */
288 	xfs_bmap_free_t		*flist);	/* i/o: list extents to free */
289 
290 /*
291  * Map file blocks to filesystem blocks, simple version.
292  * One block only, read-only.
293  * For flags, only the XFS_BMAPI_ATTRFORK flag is examined.
294  * For the other flag values, the effect is as if XFS_BMAPI_METADATA
295  * was set and all the others were clear.
296  */
297 int						/* error */
298 xfs_bmapi_single(
299 	struct xfs_trans	*tp,		/* transaction pointer */
300 	struct xfs_inode	*ip,		/* incore inode */
301 	int			whichfork,	/* data or attr fork */
302 	xfs_fsblock_t		*fsb,		/* output: mapped block */
303 	xfs_fileoff_t		bno);		/* starting file offs. mapped */
304 
305 /*
306  * Unmap (remove) blocks from a file.
307  * If nexts is nonzero then the number of extents to remove is limited to
308  * that value.  If not all extents in the block range can be removed then
309  * *done is set.
310  */
311 int						/* error */
312 xfs_bunmapi(
313 	struct xfs_trans	*tp,		/* transaction pointer */
314 	struct xfs_inode	*ip,		/* incore inode */
315 	xfs_fileoff_t		bno,		/* starting offset to unmap */
316 	xfs_filblks_t		len,		/* length to unmap in file */
317 	int			flags,		/* XFS_BMAPI_... */
318 	xfs_extnum_t		nexts,		/* number of extents max */
319 	xfs_fsblock_t		*firstblock,	/* first allocated block
320 						   controls a.g. for allocs */
321 	xfs_bmap_free_t		*flist,		/* i/o: list extents to free */
322 	int			*done);		/* set if not done yet */
323 
324 /*
325  * Fcntl interface to xfs_bmapi.
326  */
327 int						/* error code */
328 xfs_getbmap(
329 	bhv_desc_t		*bdp,		/* XFS behavior descriptor*/
330 	struct getbmap		*bmv,		/* user bmap structure */
331 	void			__user *ap,	/* pointer to user's array */
332 	int			iflags);	/* interface flags */
333 
334 /*
335  * Check the last inode extent to determine whether this allocation will result
336  * in blocks being allocated at the end of the file. When we allocate new data
337  * blocks at the end of the file which do not start at the previous data block,
338  * we will try to align the new blocks at stripe unit boundaries.
339  */
340 int
341 xfs_bmap_isaeof(
342 	struct xfs_inode	*ip,
343 	xfs_fileoff_t		off,
344 	int			whichfork,
345 	char			*aeof);
346 
347 /*
348  * Check if the endoff is outside the last extent. If so the caller will grow
349  * the allocation to a stripe unit boundary
350  */
351 int
352 xfs_bmap_eof(
353 	struct xfs_inode        *ip,
354 	xfs_fileoff_t           endoff,
355 	int                     whichfork,
356 	int                     *eof);
357 
358 /*
359  * Count fsblocks of the given fork.
360  */
361 int
362 xfs_bmap_count_blocks(
363 	xfs_trans_t		*tp,
364 	struct xfs_inode	*ip,
365 	int			whichfork,
366 	int			*count);
367 
368 /*
369  * Check an extent list, which has just been read, for
370  * any bit in the extent flag field.
371  */
372 int
373 xfs_check_nostate_extents(
374 	xfs_bmbt_rec_t		*ep,
375 	xfs_extnum_t		num);
376 
377 #endif	/* __KERNEL__ */
378 
379 #endif	/* __XFS_BMAP_H__ */
380