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