blob: b13569a6179b7235f315fe87f624e42336310c9e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11003 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
18#ifndef __XFS_BMAP_H__
19#define __XFS_BMAP_H__
20
21struct getbmap;
22struct xfs_bmbt_irec;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +110023struct xfs_ifork;
Linus Torvalds1da177e2005-04-16 15:20:36 -070024struct xfs_inode;
25struct xfs_mount;
26struct xfs_trans;
27
David Chinnera8272ce2007-11-23 16:28:09 +110028extern kmem_zone_t *xfs_bmap_free_item_zone;
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030/*
31 * List of extents to be free "later".
32 * The list is kept sorted on xbf_startblock.
33 */
34typedef 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.
Lachlan McIlroyb877e3d2008-06-27 13:33:03 +100043 *
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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 */
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 */
Lachlan McIlroyb877e3d2008-06-27 13:33:03 +100059 int xbf_low; /* alloc in low mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -070060} 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 */
Christoph Hellwigcd8b0bb2010-06-23 18:11:15 +100071#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 - */
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 /* combine contig. space */
Christoph Hellwigcd8b0bb2010-06-23 18:11:15 +100076#define XFS_BMAPI_CONTIG 0x100 /* must allocate only one extent */
77#define XFS_BMAPI_CONVERT 0x200 /* unwritten extent conversion - */
Nathan Scottdd9f4382006-01-11 15:28:28 +110078 /* need write cache flushing and no */
79 /* additional allocation alignments */
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000081#define XFS_BMAPI_FLAGS \
82 { XFS_BMAPI_WRITE, "WRITE" }, \
83 { XFS_BMAPI_DELAY, "DELAY" }, \
84 { XFS_BMAPI_ENTIRE, "ENTIRE" }, \
85 { XFS_BMAPI_METADATA, "METADATA" }, \
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000086 { XFS_BMAPI_ATTRFORK, "ATTRFORK" }, \
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000087 { 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
Nathan Scotta844f452005-11-02 14:38:42 +110094static inline int xfs_bmapi_aflag(int w)
95{
96 return (w == XFS_ATTR_FORK ? XFS_BMAPI_ATTRFORK : 0);
97}
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
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
Nathan Scotta844f452005-11-02 14:38:42 +1100105static inline void xfs_bmap_init(xfs_bmap_free_t *flp, xfs_fsblock_t *fbp)
106{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 ((flp)->xbf_first = NULL, (flp)->xbf_count = 0, \
Nathan Scotta844f452005-11-02 14:38:42 +1100108 (flp)->xbf_low = 0, *(fbp) = NULLFSBLOCK);
109}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111/*
112 * Argument structure for xfs_bmap_alloc.
113 */
114typedef 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 */
Malcolm Parsons9da096f2009-03-29 09:55:42 +0200124 xfs_extlen_t minlen; /* minimum allocation size (blocks) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 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 */
Nathan Scottdd9f4382006-01-11 15:28:28 +1100130 char aeof; /* allocated space at eof */
131 char conv; /* overwriting unwritten extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132} xfs_bmalloca_t;
133
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000134/*
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)
Christoph Hellwig6ef35542009-11-25 00:00:21 +0000145#define BMAP_ATTRFORK (1 << 8)
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000146
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000147#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" }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154/*
155 * Add bmap trace insert entries for all the contents of the extent list.
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000156 *
157 * Quite excessive tracing. Only do this for debug builds.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000159#if defined(__KERNEL) && defined(DEBUG)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160void
161xfs_bmap_trace_exlist(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 struct xfs_inode *ip, /* incore inode pointer */
163 xfs_extnum_t cnt, /* count of entries in list */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000164 int whichfork,
165 unsigned long caller_ip); /* data or attr fork */
Eric Sandeen3a59c942007-07-11 11:09:47 +1000166#define XFS_BMAP_TRACE_EXLIST(ip,c,w) \
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000167 xfs_bmap_trace_exlist(ip,c,w, _THIS_IP_)
168#else
Eric Sandeen3a59c942007-07-11 11:09:47 +1000169#define XFS_BMAP_TRACE_EXLIST(ip,c,w)
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000170#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172/*
173 * Convert inode from non-attributed to attributed.
174 * Must not be in a transaction, ip must not be locked.
175 */
176int /* error code */
177xfs_bmap_add_attrfork(
178 struct xfs_inode *ip, /* incore inode pointer */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100179 int size, /* space needed for new attribute */
180 int rsvd); /* flag for reserved block allocation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
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 */
186void
187xfs_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 */
197void
198xfs_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 */
205void
206xfs_bmap_compute_maxlevels(
207 struct xfs_mount *mp, /* file system mount structure */
208 int whichfork); /* data or attr fork */
209
210/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 * 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 */
215int /* error */
216xfs_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 */
229int /* error */
230xfs_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 */
241int /* error */
242xfs_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 */
253int
254xfs_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 */
263int /* error */
264xfs_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 */
282int /* error */
283xfs_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 */
Christoph Hellwigb4e91812010-06-23 18:11:15 +1000294 xfs_bmap_free_t *flist); /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
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 */
303int /* error */
304xfs_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 */
317int /* error */
318xfs_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/*
Barry Naujok847fff52008-10-30 17:05:38 +1100331 * Check an extent list, which has just been read, for
332 * any bit in the extent flag field.
333 */
334int
335xfs_check_nostate_extents(
336 struct xfs_ifork *ifp,
337 xfs_extnum_t idx,
338 xfs_extnum_t num);
339
Christoph Hellwig1a5902c2009-03-29 19:26:46 +0200340uint
341xfs_default_attroffset(
342 struct xfs_inode *ip);
343
Barry Naujok847fff52008-10-30 17:05:38 +1100344#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 */
354int /* error */
355xfs_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
Eric Sandeen8a7141a2008-11-28 14:23:35 +1100360/* bmap to userspace formatter - copy to user & advance pointer */
361typedef int (*xfs_bmap_format_t)(void **, struct getbmapx *, int *);
362
Barry Naujok847fff52008-10-30 17:05:38 +1100363/*
Eric Sandeen8a7141a2008-11-28 14:23:35 +1100364 * Get inode's extents as described in bmv, and format for output.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 */
366int /* error code */
367xfs_getbmap(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000368 xfs_inode_t *ip,
Eric Sandeen8a7141a2008-11-28 14:23:35 +1100369 struct getbmapx *bmv, /* user bmap structure */
370 xfs_bmap_format_t formatter, /* format to user */
371 void *arg); /* formatter arg */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 * Check if the endoff is outside the last extent. If so the caller will grow
375 * the allocation to a stripe unit boundary
376 */
377int
378xfs_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 */
387int
388xfs_bmap_count_blocks(
389 xfs_trans_t *tp,
390 struct xfs_inode *ip,
391 int whichfork,
392 int *count);
393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394#endif /* __KERNEL__ */
395
396#endif /* __XFS_BMAP_H__ */