blob: 51a9c82f0f624b47d0993e12cea4e4664066518f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scottd8cc8902005-11-02 10:34:53 +11002 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc. All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
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
35struct getbmap;
36struct xfs_bmbt_irec;
37struct xfs_inode;
38struct xfs_mount;
39struct xfs_trans;
40
41/*
42 * List of extents to be free "later".
43 * The list is kept sorted on xbf_startblock.
44 */
45typedef 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 */
55typedef 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#define XFS_BMAPI_AFLAG(w) xfs_bmapi_aflag(w)
Nathan Scotta844f452005-11-02 14:38:42 +110081static inline int xfs_bmapi_aflag(int w)
82{
83 return (w == XFS_ATTR_FORK ? XFS_BMAPI_ATTRFORK : 0);
84}
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86/*
87 * Special values for xfs_bmbt_irec_t br_startblock field.
88 */
89#define DELAYSTARTBLOCK ((xfs_fsblock_t)-1LL)
90#define HOLESTARTBLOCK ((xfs_fsblock_t)-2LL)
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#define XFS_BMAP_INIT(flp,fbp) xfs_bmap_init(flp,fbp)
Nathan Scotta844f452005-11-02 14:38:42 +110093static inline void xfs_bmap_init(xfs_bmap_free_t *flp, xfs_fsblock_t *fbp)
94{
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 ((flp)->xbf_first = NULL, (flp)->xbf_count = 0, \
Nathan Scotta844f452005-11-02 14:38:42 +110096 (flp)->xbf_low = 0, *(fbp) = NULLFSBLOCK);
97}
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99/*
100 * Argument structure for xfs_bmap_alloc.
101 */
102typedef struct xfs_bmalloca {
103 xfs_fsblock_t firstblock; /* i/o first block allocated */
104 xfs_fsblock_t rval; /* starting block of new extent */
105 xfs_fileoff_t off; /* offset in file filling in */
106 struct xfs_trans *tp; /* transaction pointer */
107 struct xfs_inode *ip; /* incore inode pointer */
108 struct xfs_bmbt_irec *prevp; /* extent before the new one */
109 struct xfs_bmbt_irec *gotp; /* extent after, or delayed */
110 xfs_extlen_t alen; /* i/o length asked/allocated */
111 xfs_extlen_t total; /* total blocks needed for xaction */
112 xfs_extlen_t minlen; /* mininum allocation size (blocks) */
113 xfs_extlen_t minleft; /* amount must be left after alloc */
114 char eof; /* set if allocating past last extent */
115 char wasdel; /* replacing a delayed allocation */
116 char userdata;/* set if is user data */
117 char low; /* low on space, using seq'l ags */
118 char aeof; /* allocated space at eof */
119} xfs_bmalloca_t;
120
121#ifdef __KERNEL__
122
123#if defined(XFS_BMAP_TRACE)
124/*
125 * Trace operations for bmap extent tracing
126 */
127#define XFS_BMAP_KTRACE_DELETE 1
128#define XFS_BMAP_KTRACE_INSERT 2
129#define XFS_BMAP_KTRACE_PRE_UP 3
130#define XFS_BMAP_KTRACE_POST_UP 4
131
132#define XFS_BMAP_TRACE_SIZE 4096 /* size of global trace buffer */
133#define XFS_BMAP_KTRACE_SIZE 32 /* size of per-inode trace buffer */
134extern ktrace_t *xfs_bmap_trace_buf;
135
136/*
137 * Add bmap trace insert entries for all the contents of the extent list.
138 */
139void
140xfs_bmap_trace_exlist(
141 char *fname, /* function name */
142 struct xfs_inode *ip, /* incore inode pointer */
143 xfs_extnum_t cnt, /* count of entries in list */
144 int whichfork); /* data or attr fork */
145#else
146#define xfs_bmap_trace_exlist(f,ip,c,w)
147#endif
148
149/*
150 * Convert inode from non-attributed to attributed.
151 * Must not be in a transaction, ip must not be locked.
152 */
153int /* error code */
154xfs_bmap_add_attrfork(
155 struct xfs_inode *ip, /* incore inode pointer */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100156 int size, /* space needed for new attribute */
157 int rsvd); /* flag for reserved block allocation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159/*
160 * Add the extent to the list of extents to be free at transaction end.
161 * The list is maintained sorted (by block number).
162 */
163void
164xfs_bmap_add_free(
165 xfs_fsblock_t bno, /* fs block number of extent */
166 xfs_filblks_t len, /* length of extent */
167 xfs_bmap_free_t *flist, /* list of extents */
168 struct xfs_mount *mp); /* mount point structure */
169
170/*
171 * Routine to clean up the free list data structure when
172 * an error occurs during a transaction.
173 */
174void
175xfs_bmap_cancel(
176 xfs_bmap_free_t *flist); /* free list to clean up */
177
178/*
179 * Compute and fill in the value of the maximum depth of a bmap btree
180 * in this filesystem. Done once, during mount.
181 */
182void
183xfs_bmap_compute_maxlevels(
184 struct xfs_mount *mp, /* file system mount structure */
185 int whichfork); /* data or attr fork */
186
187/*
188 * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
189 * caller. Frees all the extents that need freeing, which must be done
190 * last due to locking considerations.
191 *
192 * Return 1 if the given transaction was committed and a new one allocated,
193 * and 0 otherwise.
194 */
195int /* error */
196xfs_bmap_finish(
197 struct xfs_trans **tp, /* transaction pointer addr */
198 xfs_bmap_free_t *flist, /* i/o: list extents to free */
199 xfs_fsblock_t firstblock, /* controlled a.g. for allocs */
200 int *committed); /* xact committed or not */
201
202/*
203 * Returns the file-relative block number of the first unused block in the file.
204 * This is the lowest-address hole if the file has holes, else the first block
205 * past the end of file.
206 */
207int /* error */
208xfs_bmap_first_unused(
209 struct xfs_trans *tp, /* transaction pointer */
210 struct xfs_inode *ip, /* incore inode */
211 xfs_extlen_t len, /* size of hole to find */
212 xfs_fileoff_t *unused, /* unused block num */
213 int whichfork); /* data or attr fork */
214
215/*
216 * Returns the file-relative block number of the last block + 1 before
217 * last_block (input value) in the file.
218 * This is not based on i_size, it is based on the extent list.
219 * Returns 0 for local files, as they do not have an extent list.
220 */
221int /* error */
222xfs_bmap_last_before(
223 struct xfs_trans *tp, /* transaction pointer */
224 struct xfs_inode *ip, /* incore inode */
225 xfs_fileoff_t *last_block, /* last block */
226 int whichfork); /* data or attr fork */
227
228/*
229 * Returns the file-relative block number of the first block past eof in
230 * the file. This is not based on i_size, it is based on the extent list.
231 * Returns 0 for local files, as they do not have an extent list.
232 */
233int /* error */
234xfs_bmap_last_offset(
235 struct xfs_trans *tp, /* transaction pointer */
236 struct xfs_inode *ip, /* incore inode */
237 xfs_fileoff_t *unused, /* last block num */
238 int whichfork); /* data or attr fork */
239
240/*
241 * Returns whether the selected fork of the inode has exactly one
242 * block or not. For the data fork we check this matches di_size,
243 * implying the file's range is 0..bsize-1.
244 */
245int
246xfs_bmap_one_block(
247 struct xfs_inode *ip, /* incore inode */
248 int whichfork); /* data or attr fork */
249
250/*
251 * Read in the extents to iu_extents.
252 * All inode fields are set up by caller, we just traverse the btree
253 * and copy the records in.
254 */
255int /* error */
256xfs_bmap_read_extents(
257 struct xfs_trans *tp, /* transaction pointer */
258 struct xfs_inode *ip, /* incore inode */
259 int whichfork); /* data or attr fork */
260
261/*
262 * Map file blocks to filesystem blocks.
263 * File range is given by the bno/len pair.
264 * Adds blocks to file if a write ("flags & XFS_BMAPI_WRITE" set)
265 * into a hole or past eof.
266 * Only allocates blocks from a single allocation group,
267 * to avoid locking problems.
268 * The returned value in "firstblock" from the first call in a transaction
269 * must be remembered and presented to subsequent calls in "firstblock".
270 * An upper bound for the number of blocks to be allocated is supplied to
271 * the first call in "total"; if no allocation group has that many free
272 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
273 */
274int /* error */
275xfs_bmapi(
276 struct xfs_trans *tp, /* transaction pointer */
277 struct xfs_inode *ip, /* incore inode */
278 xfs_fileoff_t bno, /* starting file offs. mapped */
279 xfs_filblks_t len, /* length to map in file */
280 int flags, /* XFS_BMAPI_... */
281 xfs_fsblock_t *firstblock, /* first allocated block
282 controls a.g. for allocs */
283 xfs_extlen_t total, /* total blocks needed */
284 struct xfs_bmbt_irec *mval, /* output: map values */
285 int *nmap, /* i/o: mval size/count */
286 xfs_bmap_free_t *flist); /* i/o: list extents to free */
287
288/*
289 * Map file blocks to filesystem blocks, simple version.
290 * One block only, read-only.
291 * For flags, only the XFS_BMAPI_ATTRFORK flag is examined.
292 * For the other flag values, the effect is as if XFS_BMAPI_METADATA
293 * was set and all the others were clear.
294 */
295int /* error */
296xfs_bmapi_single(
297 struct xfs_trans *tp, /* transaction pointer */
298 struct xfs_inode *ip, /* incore inode */
299 int whichfork, /* data or attr fork */
300 xfs_fsblock_t *fsb, /* output: mapped block */
301 xfs_fileoff_t bno); /* starting file offs. mapped */
302
303/*
304 * Unmap (remove) blocks from a file.
305 * If nexts is nonzero then the number of extents to remove is limited to
306 * that value. If not all extents in the block range can be removed then
307 * *done is set.
308 */
309int /* error */
310xfs_bunmapi(
311 struct xfs_trans *tp, /* transaction pointer */
312 struct xfs_inode *ip, /* incore inode */
313 xfs_fileoff_t bno, /* starting offset to unmap */
314 xfs_filblks_t len, /* length to unmap in file */
315 int flags, /* XFS_BMAPI_... */
316 xfs_extnum_t nexts, /* number of extents max */
317 xfs_fsblock_t *firstblock, /* first allocated block
318 controls a.g. for allocs */
319 xfs_bmap_free_t *flist, /* i/o: list extents to free */
320 int *done); /* set if not done yet */
321
322/*
323 * Fcntl interface to xfs_bmapi.
324 */
325int /* error code */
326xfs_getbmap(
327 bhv_desc_t *bdp, /* XFS behavior descriptor*/
328 struct getbmap *bmv, /* user bmap structure */
329 void __user *ap, /* pointer to user's array */
330 int iflags); /* interface flags */
331
332/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 * Check if the endoff is outside the last extent. If so the caller will grow
334 * the allocation to a stripe unit boundary
335 */
336int
337xfs_bmap_eof(
338 struct xfs_inode *ip,
339 xfs_fileoff_t endoff,
340 int whichfork,
341 int *eof);
342
343/*
344 * Count fsblocks of the given fork.
345 */
346int
347xfs_bmap_count_blocks(
348 xfs_trans_t *tp,
349 struct xfs_inode *ip,
350 int whichfork,
351 int *count);
352
353/*
354 * Check an extent list, which has just been read, for
355 * any bit in the extent flag field.
356 */
357int
358xfs_check_nostate_extents(
359 xfs_bmbt_rec_t *ep,
360 xfs_extnum_t num);
361
362#endif /* __KERNEL__ */
363
364#endif /* __XFS_BMAP_H__ */