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