Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tj/percpu
[pandora-kernel.git] / fs / xfs / xfs_bmap.h
1 /*
2  * Copyright (c) 2000-2006 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_ifork;
24 struct xfs_inode;
25 struct xfs_mount;
26 struct xfs_trans;
27
28 extern kmem_zone_t      *xfs_bmap_free_item_zone;
29
30 /*
31  * List of extents to be free "later".
32  * The list is kept sorted on xbf_startblock.
33  */
34 typedef struct xfs_bmap_free_item
35 {
36         xfs_fsblock_t           xbfi_startblock;/* starting fs block number */
37         xfs_extlen_t            xbfi_blockcount;/* number of blocks in extent */
38         struct xfs_bmap_free_item *xbfi_next;   /* link to next entry */
39 } xfs_bmap_free_item_t;
40
41 /*
42  * Header for free extent list.
43  *
44  * xbf_low is used by the allocator to activate the lowspace algorithm -
45  * when free space is running low the extent allocator may choose to
46  * allocate an extent from an AG without leaving sufficient space for
47  * a btree split when inserting the new extent.  In this case the allocator
48  * will enable the lowspace algorithm which is supposed to allow further
49  * allocations (such as btree splits and newroots) to allocate from
50  * sequential AGs.  In order to avoid locking AGs out of order the lowspace
51  * algorithm will start searching for free space from AG 0.  If the correct
52  * transaction reservations have been made then this algorithm will eventually
53  * find all the space it needs.
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;        /* 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_ATTRFORK      0x010   /* use attribute fork not data */
72 #define XFS_BMAPI_RSVBLOCKS     0x020   /* OK to alloc. reserved data blocks */
73 #define XFS_BMAPI_PREALLOC      0x040   /* preallocation op: unwritten space */
74 #define XFS_BMAPI_IGSTATE       0x080   /* Ignore state - */
75                                         /* combine contig. space */
76 #define XFS_BMAPI_CONTIG        0x100   /* must allocate only one extent */
77 #define XFS_BMAPI_CONVERT       0x200   /* unwritten extent conversion - */
78                                         /* need write cache flushing and no */
79                                         /* additional allocation alignments */
80
81 #define XFS_BMAPI_FLAGS \
82         { XFS_BMAPI_WRITE,      "WRITE" }, \
83         { XFS_BMAPI_DELAY,      "DELAY" }, \
84         { XFS_BMAPI_ENTIRE,     "ENTIRE" }, \
85         { XFS_BMAPI_METADATA,   "METADATA" }, \
86         { XFS_BMAPI_ATTRFORK,   "ATTRFORK" }, \
87         { XFS_BMAPI_RSVBLOCKS,  "RSVBLOCKS" }, \
88         { XFS_BMAPI_PREALLOC,   "PREALLOC" }, \
89         { XFS_BMAPI_IGSTATE,    "IGSTATE" }, \
90         { XFS_BMAPI_CONTIG,     "CONTIG" }, \
91         { XFS_BMAPI_CONVERT,    "CONVERT" }
92
93
94 static inline int xfs_bmapi_aflag(int w)
95 {
96         return (w == XFS_ATTR_FORK ? XFS_BMAPI_ATTRFORK : 0);
97 }
98
99 /*
100  * Special values for xfs_bmbt_irec_t br_startblock field.
101  */
102 #define DELAYSTARTBLOCK         ((xfs_fsblock_t)-1LL)
103 #define HOLESTARTBLOCK          ((xfs_fsblock_t)-2LL)
104
105 static inline void xfs_bmap_init(xfs_bmap_free_t *flp, xfs_fsblock_t *fbp)
106 {
107         ((flp)->xbf_first = NULL, (flp)->xbf_count = 0, \
108                 (flp)->xbf_low = 0, *(fbp) = NULLFSBLOCK);
109 }
110
111 /*
112  * Argument structure for xfs_bmap_alloc.
113  */
114 typedef struct xfs_bmalloca {
115         xfs_fsblock_t           firstblock; /* i/o first block allocated */
116         xfs_fsblock_t           rval;   /* starting block of new extent */
117         xfs_fileoff_t           off;    /* offset in file filling in */
118         struct xfs_trans        *tp;    /* transaction pointer */
119         struct xfs_inode        *ip;    /* incore inode pointer */
120         struct xfs_bmbt_irec    *prevp; /* extent before the new one */
121         struct xfs_bmbt_irec    *gotp;  /* extent after, or delayed */
122         xfs_extlen_t            alen;   /* i/o length asked/allocated */
123         xfs_extlen_t            total;  /* total blocks needed for xaction */
124         xfs_extlen_t            minlen; /* minimum allocation size (blocks) */
125         xfs_extlen_t            minleft; /* amount must be left after alloc */
126         char                    eof;    /* set if allocating past last extent */
127         char                    wasdel; /* replacing a delayed allocation */
128         char                    userdata;/* set if is user data */
129         char                    low;    /* low on space, using seq'l ags */
130         char                    aeof;   /* allocated space at eof */
131         char                    conv;   /* overwriting unwritten extents */
132 } xfs_bmalloca_t;
133
134 /*
135  * Flags for xfs_bmap_add_extent*.
136  */
137 #define BMAP_LEFT_CONTIG        (1 << 0)
138 #define BMAP_RIGHT_CONTIG       (1 << 1)
139 #define BMAP_LEFT_FILLING       (1 << 2)
140 #define BMAP_RIGHT_FILLING      (1 << 3)
141 #define BMAP_LEFT_DELAY         (1 << 4)
142 #define BMAP_RIGHT_DELAY        (1 << 5)
143 #define BMAP_LEFT_VALID         (1 << 6)
144 #define BMAP_RIGHT_VALID        (1 << 7)
145 #define BMAP_ATTRFORK           (1 << 8)
146
147 #define XFS_BMAP_EXT_FLAGS \
148         { BMAP_LEFT_CONTIG,     "LC" }, \
149         { BMAP_RIGHT_CONTIG,    "RC" }, \
150         { BMAP_LEFT_FILLING,    "LF" }, \
151         { BMAP_RIGHT_FILLING,   "RF" }, \
152         { BMAP_ATTRFORK,        "ATTR" }
153
154 /*
155  * Add bmap trace insert entries for all the contents of the extent list.
156  *
157  * Quite excessive tracing.  Only do this for debug builds.
158  */
159 #if defined(__KERNEL) && defined(DEBUG)
160 void
161 xfs_bmap_trace_exlist(
162         struct xfs_inode        *ip,            /* incore inode pointer */
163         xfs_extnum_t            cnt,            /* count of entries in list */
164         int                     whichfork,
165         unsigned long           caller_ip);     /* data or attr fork */
166 #define XFS_BMAP_TRACE_EXLIST(ip,c,w)   \
167         xfs_bmap_trace_exlist(ip,c,w, _THIS_IP_)
168 #else
169 #define XFS_BMAP_TRACE_EXLIST(ip,c,w)
170 #endif
171
172 /*
173  * Convert inode from non-attributed to attributed.
174  * Must not be in a transaction, ip must not be locked.
175  */
176 int                                     /* error code */
177 xfs_bmap_add_attrfork(
178         struct xfs_inode        *ip,    /* incore inode pointer */
179         int                     size,   /* space needed for new attribute */
180         int                     rsvd);  /* flag for reserved block allocation */
181
182 /*
183  * Add the extent to the list of extents to be free at transaction end.
184  * The list is maintained sorted (by block number).
185  */
186 void
187 xfs_bmap_add_free(
188         xfs_fsblock_t           bno,            /* fs block number of extent */
189         xfs_filblks_t           len,            /* length of extent */
190         xfs_bmap_free_t         *flist,         /* list of extents */
191         struct xfs_mount        *mp);           /* mount point structure */
192
193 /*
194  * Routine to clean up the free list data structure when
195  * an error occurs during a transaction.
196  */
197 void
198 xfs_bmap_cancel(
199         xfs_bmap_free_t         *flist);        /* free list to clean up */
200
201 /*
202  * Compute and fill in the value of the maximum depth of a bmap btree
203  * in this filesystem.  Done once, during mount.
204  */
205 void
206 xfs_bmap_compute_maxlevels(
207         struct xfs_mount        *mp,    /* file system mount structure */
208         int                     whichfork);     /* data or attr fork */
209
210 /*
211  * Returns the file-relative block number of the first unused block in the file.
212  * This is the lowest-address hole if the file has holes, else the first block
213  * past the end of file.
214  */
215 int                                             /* error */
216 xfs_bmap_first_unused(
217         struct xfs_trans        *tp,            /* transaction pointer */
218         struct xfs_inode        *ip,            /* incore inode */
219         xfs_extlen_t            len,            /* size of hole to find */
220         xfs_fileoff_t           *unused,        /* unused block num */
221         int                     whichfork);     /* data or attr fork */
222
223 /*
224  * Returns the file-relative block number of the last block + 1 before
225  * last_block (input value) in the file.
226  * This is not based on i_size, it is based on the extent list.
227  * Returns 0 for local files, as they do not have an extent list.
228  */
229 int                                             /* error */
230 xfs_bmap_last_before(
231         struct xfs_trans        *tp,            /* transaction pointer */
232         struct xfs_inode        *ip,            /* incore inode */
233         xfs_fileoff_t           *last_block,    /* last block */
234         int                     whichfork);     /* data or attr fork */
235
236 /*
237  * Returns the file-relative block number of the first block past eof in
238  * the file.  This is not based on i_size, it is based on the extent list.
239  * Returns 0 for local files, as they do not have an extent list.
240  */
241 int                                             /* error */
242 xfs_bmap_last_offset(
243         struct xfs_trans        *tp,            /* transaction pointer */
244         struct xfs_inode        *ip,            /* incore inode */
245         xfs_fileoff_t           *unused,        /* last block num */
246         int                     whichfork);     /* data or attr fork */
247
248 /*
249  * Returns whether the selected fork of the inode has exactly one
250  * block or not.  For the data fork we check this matches di_size,
251  * implying the file's range is 0..bsize-1.
252  */
253 int
254 xfs_bmap_one_block(
255         struct xfs_inode        *ip,            /* incore inode */
256         int                     whichfork);     /* data or attr fork */
257
258 /*
259  * Read in the extents to iu_extents.
260  * All inode fields are set up by caller, we just traverse the btree
261  * and copy the records in.
262  */
263 int                                             /* error */
264 xfs_bmap_read_extents(
265         struct xfs_trans        *tp,            /* transaction pointer */
266         struct xfs_inode        *ip,            /* incore inode */
267         int                     whichfork);     /* data or attr fork */
268
269 /*
270  * Map file blocks to filesystem blocks.
271  * File range is given by the bno/len pair.
272  * Adds blocks to file if a write ("flags & XFS_BMAPI_WRITE" set)
273  * into a hole or past eof.
274  * Only allocates blocks from a single allocation group,
275  * to avoid locking problems.
276  * The returned value in "firstblock" from the first call in a transaction
277  * must be remembered and presented to subsequent calls in "firstblock".
278  * An upper bound for the number of blocks to be allocated is supplied to
279  * the first call in "total"; if no allocation group has that many free
280  * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
281  */
282 int                                             /* error */
283 xfs_bmapi(
284         struct xfs_trans        *tp,            /* transaction pointer */
285         struct xfs_inode        *ip,            /* incore inode */
286         xfs_fileoff_t           bno,            /* starting file offs. mapped */
287         xfs_filblks_t           len,            /* length to map in file */
288         int                     flags,          /* XFS_BMAPI_... */
289         xfs_fsblock_t           *firstblock,    /* first allocated block
290                                                    controls a.g. for allocs */
291         xfs_extlen_t            total,          /* total blocks needed */
292         struct xfs_bmbt_irec    *mval,          /* output: map values */
293         int                     *nmap,          /* i/o: mval size/count */
294         xfs_bmap_free_t         *flist);        /* i/o: list extents to free */
295
296 /*
297  * Map file blocks to filesystem blocks, simple version.
298  * One block only, read-only.
299  * For flags, only the XFS_BMAPI_ATTRFORK flag is examined.
300  * For the other flag values, the effect is as if XFS_BMAPI_METADATA
301  * was set and all the others were clear.
302  */
303 int                                             /* error */
304 xfs_bmapi_single(
305         struct xfs_trans        *tp,            /* transaction pointer */
306         struct xfs_inode        *ip,            /* incore inode */
307         int                     whichfork,      /* data or attr fork */
308         xfs_fsblock_t           *fsb,           /* output: mapped block */
309         xfs_fileoff_t           bno);           /* starting file offs. mapped */
310
311 /*
312  * Unmap (remove) blocks from a file.
313  * If nexts is nonzero then the number of extents to remove is limited to
314  * that value.  If not all extents in the block range can be removed then
315  * *done is set.
316  */
317 int                                             /* error */
318 xfs_bunmapi(
319         struct xfs_trans        *tp,            /* transaction pointer */
320         struct xfs_inode        *ip,            /* incore inode */
321         xfs_fileoff_t           bno,            /* starting offset to unmap */
322         xfs_filblks_t           len,            /* length to unmap in file */
323         int                     flags,          /* XFS_BMAPI_... */
324         xfs_extnum_t            nexts,          /* number of extents max */
325         xfs_fsblock_t           *firstblock,    /* first allocated block
326                                                    controls a.g. for allocs */
327         xfs_bmap_free_t         *flist,         /* i/o: list extents to free */
328         int                     *done);         /* set if not done yet */
329
330 /*
331  * Check an extent list, which has just been read, for
332  * any bit in the extent flag field.
333  */
334 int
335 xfs_check_nostate_extents(
336         struct xfs_ifork        *ifp,
337         xfs_extnum_t            idx,
338         xfs_extnum_t            num);
339
340 uint
341 xfs_default_attroffset(
342         struct xfs_inode        *ip);
343
344 #ifdef __KERNEL__
345
346 /*
347  * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
348  * caller.  Frees all the extents that need freeing, which must be done
349  * last due to locking considerations.
350  *
351  * Return 1 if the given transaction was committed and a new one allocated,
352  * and 0 otherwise.
353  */
354 int                                             /* error */
355 xfs_bmap_finish(
356         struct xfs_trans        **tp,           /* transaction pointer addr */
357         xfs_bmap_free_t         *flist,         /* i/o: list extents to free */
358         int                     *committed);    /* xact committed or not */
359
360 /* bmap to userspace formatter - copy to user & advance pointer */
361 typedef int (*xfs_bmap_format_t)(void **, struct getbmapx *, int *);
362
363 /*
364  * Get inode's extents as described in bmv, and format for output.
365  */
366 int                                             /* error code */
367 xfs_getbmap(
368         xfs_inode_t             *ip,
369         struct getbmapx         *bmv,           /* user bmap structure */
370         xfs_bmap_format_t       formatter,      /* format to user */
371         void                    *arg);          /* formatter arg */
372
373 /*
374  * Check if the endoff is outside the last extent. If so the caller will grow
375  * the allocation to a stripe unit boundary
376  */
377 int
378 xfs_bmap_eof(
379         struct xfs_inode        *ip,
380         xfs_fileoff_t           endoff,
381         int                     whichfork,
382         int                     *eof);
383
384 /*
385  * Count fsblocks of the given fork.
386  */
387 int
388 xfs_bmap_count_blocks(
389         xfs_trans_t             *tp,
390         struct xfs_inode        *ip,
391         int                     whichfork,
392         int                     *count);
393
394 #endif  /* __KERNEL__ */
395
396 #endif  /* __XFS_BMAP_H__ */