]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/xfs/xfs_bmap.c
xfs: pass bmalloca to xfs_bmap_add_extent_delay_real
[mirror_ubuntu-artful-kernel.git] / fs / xfs / xfs_bmap.c
CommitLineData
1da177e4 1/*
3e57ecf6 2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
7b718769 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 17 */
1da177e4 18#include "xfs.h"
a844f451 19#include "xfs_fs.h"
1da177e4 20#include "xfs_types.h"
a844f451 21#include "xfs_bit.h"
1da177e4 22#include "xfs_log.h"
a844f451 23#include "xfs_inum.h"
1da177e4
LT
24#include "xfs_trans.h"
25#include "xfs_sb.h"
26#include "xfs_ag.h"
1da177e4 27#include "xfs_dir2.h"
a844f451 28#include "xfs_da_btree.h"
1da177e4 29#include "xfs_bmap_btree.h"
a844f451 30#include "xfs_alloc_btree.h"
1da177e4 31#include "xfs_ialloc_btree.h"
1da177e4 32#include "xfs_dinode.h"
1da177e4 33#include "xfs_inode.h"
a844f451 34#include "xfs_btree.h"
a844f451 35#include "xfs_mount.h"
1da177e4 36#include "xfs_itable.h"
a844f451 37#include "xfs_inode_item.h"
1da177e4
LT
38#include "xfs_extfree_item.h"
39#include "xfs_alloc.h"
40#include "xfs_bmap.h"
41#include "xfs_rtalloc.h"
42#include "xfs_error.h"
d8cc890d 43#include "xfs_attr_leaf.h"
1da177e4
LT
44#include "xfs_rw.h"
45#include "xfs_quota.h"
46#include "xfs_trans_space.h"
47#include "xfs_buf_item.h"
2a82b8be 48#include "xfs_filestream.h"
739bfb2a 49#include "xfs_vnodeops.h"
0b1b213f 50#include "xfs_trace.h"
1da177e4
LT
51
52
1da177e4
LT
53kmem_zone_t *xfs_bmap_free_item_zone;
54
55/*
56 * Prototypes for internal bmap routines.
57 */
58
a5bd606b
CH
59#ifdef DEBUG
60STATIC void
61xfs_bmap_check_leaf_extents(
62 struct xfs_btree_cur *cur,
63 struct xfs_inode *ip,
64 int whichfork);
65#else
66#define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
67#endif
68
1da177e4
LT
69
70/*
71 * Called from xfs_bmap_add_attrfork to handle extents format files.
72 */
73STATIC int /* error */
74xfs_bmap_add_attrfork_extents(
75 xfs_trans_t *tp, /* transaction pointer */
76 xfs_inode_t *ip, /* incore inode pointer */
77 xfs_fsblock_t *firstblock, /* first block allocated */
78 xfs_bmap_free_t *flist, /* blocks to free at commit */
79 int *flags); /* inode logging flags */
80
81/*
82 * Called from xfs_bmap_add_attrfork to handle local format files.
83 */
84STATIC int /* error */
85xfs_bmap_add_attrfork_local(
86 xfs_trans_t *tp, /* transaction pointer */
87 xfs_inode_t *ip, /* incore inode pointer */
88 xfs_fsblock_t *firstblock, /* first block allocated */
89 xfs_bmap_free_t *flist, /* blocks to free at commit */
90 int *flags); /* inode logging flags */
91
1da177e4
LT
92/*
93 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
94 * It figures out where to ask the underlying allocator to put the new extent.
95 */
96STATIC int /* error */
97xfs_bmap_alloc(
98 xfs_bmalloca_t *ap); /* bmap alloc argument struct */
99
100/*
101 * Transform a btree format file with only one leaf node, where the
102 * extents list will fit in the inode, into an extents format file.
4eea22f0 103 * Since the file extents are already in-core, all we have to do is
1da177e4
LT
104 * give up the space for the btree root and pitch the leaf block.
105 */
106STATIC int /* error */
107xfs_bmap_btree_to_extents(
108 xfs_trans_t *tp, /* transaction pointer */
109 xfs_inode_t *ip, /* incore inode pointer */
110 xfs_btree_cur_t *cur, /* btree cursor */
111 int *logflagsp, /* inode logging flags */
112 int whichfork); /* data or attr fork */
113
1da177e4
LT
114/*
115 * Remove the entry "free" from the free item list. Prev points to the
116 * previous entry, unless "free" is the head of the list.
117 */
118STATIC void
119xfs_bmap_del_free(
120 xfs_bmap_free_t *flist, /* free item list header */
121 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
122 xfs_bmap_free_item_t *free); /* list item to be freed */
123
1da177e4
LT
124/*
125 * Convert an extents-format file into a btree-format file.
126 * The new file will have a root block (in the inode) and a single child block.
127 */
128STATIC int /* error */
129xfs_bmap_extents_to_btree(
130 xfs_trans_t *tp, /* transaction pointer */
131 xfs_inode_t *ip, /* incore inode pointer */
132 xfs_fsblock_t *firstblock, /* first-block-allocated */
133 xfs_bmap_free_t *flist, /* blocks freed in xaction */
134 xfs_btree_cur_t **curp, /* cursor returned to caller */
135 int wasdel, /* converting a delayed alloc */
136 int *logflagsp, /* inode logging flags */
137 int whichfork); /* data or attr fork */
138
1da177e4
LT
139/*
140 * Convert a local file to an extents file.
141 * This code is sort of bogus, since the file data needs to get
142 * logged so it won't be lost. The bmap-level manipulations are ok, though.
143 */
144STATIC int /* error */
145xfs_bmap_local_to_extents(
146 xfs_trans_t *tp, /* transaction pointer */
147 xfs_inode_t *ip, /* incore inode pointer */
148 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
149 xfs_extlen_t total, /* total blocks needed by transaction */
150 int *logflagsp, /* inode logging flags */
151 int whichfork); /* data or attr fork */
152
153/*
154 * Search the extents list for the inode, for the extent containing bno.
155 * If bno lies in a hole, point to the next entry. If bno lies past eof,
156 * *eofp will be set, and *prevp will contain the last entry (null if none).
157 * Else, *lastxp will be set to the index of the found
158 * entry; *gotp will contain the entry.
159 */
a6f64d4a 160STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
1da177e4
LT
161xfs_bmap_search_extents(
162 xfs_inode_t *ip, /* incore inode pointer */
163 xfs_fileoff_t bno, /* block number searched for */
164 int whichfork, /* data or attr fork */
165 int *eofp, /* out: end of file found */
166 xfs_extnum_t *lastxp, /* out: last extent index */
167 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
168 xfs_bmbt_irec_t *prevp); /* out: previous extent entry found */
169
1da177e4
LT
170/*
171 * Compute the worst-case number of indirect blocks that will be used
172 * for ip's delayed extent of length "len".
173 */
174STATIC xfs_filblks_t
175xfs_bmap_worst_indlen(
176 xfs_inode_t *ip, /* incore inode pointer */
177 xfs_filblks_t len); /* delayed extent length */
178
179#ifdef DEBUG
180/*
181 * Perform various validation checks on the values being returned
182 * from xfs_bmapi().
183 */
184STATIC void
185xfs_bmap_validate_ret(
186 xfs_fileoff_t bno,
187 xfs_filblks_t len,
188 int flags,
189 xfs_bmbt_irec_t *mval,
190 int nmap,
191 int ret_nmap);
192#else
193#define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
194#endif /* DEBUG */
195
1da177e4
LT
196STATIC int
197xfs_bmap_count_tree(
198 xfs_mount_t *mp,
199 xfs_trans_t *tp,
4eea22f0 200 xfs_ifork_t *ifp,
1da177e4
LT
201 xfs_fsblock_t blockno,
202 int levelin,
203 int *count);
204
c94312de 205STATIC void
1da177e4 206xfs_bmap_count_leaves(
4eea22f0
MK
207 xfs_ifork_t *ifp,
208 xfs_extnum_t idx,
1da177e4
LT
209 int numrecs,
210 int *count);
211
c94312de 212STATIC void
91e11088 213xfs_bmap_disk_count_leaves(
136341b4 214 struct xfs_mount *mp,
7cc95a82 215 struct xfs_btree_block *block,
91e11088
YL
216 int numrecs,
217 int *count);
218
1da177e4
LT
219/*
220 * Bmap internal routines.
221 */
222
fe033cc8
CH
223STATIC int /* error */
224xfs_bmbt_lookup_eq(
225 struct xfs_btree_cur *cur,
226 xfs_fileoff_t off,
227 xfs_fsblock_t bno,
228 xfs_filblks_t len,
229 int *stat) /* success/failure */
230{
231 cur->bc_rec.b.br_startoff = off;
232 cur->bc_rec.b.br_startblock = bno;
233 cur->bc_rec.b.br_blockcount = len;
234 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
235}
236
237STATIC int /* error */
238xfs_bmbt_lookup_ge(
239 struct xfs_btree_cur *cur,
240 xfs_fileoff_t off,
241 xfs_fsblock_t bno,
242 xfs_filblks_t len,
243 int *stat) /* success/failure */
244{
245 cur->bc_rec.b.br_startoff = off;
246 cur->bc_rec.b.br_startblock = bno;
247 cur->bc_rec.b.br_blockcount = len;
248 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
249}
250
278d0ca1
CH
251/*
252* Update the record referred to by cur to the value given
253 * by [off, bno, len, state].
254 * This either works (return 0) or gets an EFSCORRUPTED error.
255 */
256STATIC int
257xfs_bmbt_update(
258 struct xfs_btree_cur *cur,
259 xfs_fileoff_t off,
260 xfs_fsblock_t bno,
261 xfs_filblks_t len,
262 xfs_exntst_t state)
263{
264 union xfs_btree_rec rec;
265
266 xfs_bmbt_disk_set_allf(&rec.bmbt, off, bno, len, state);
267 return xfs_btree_update(cur, &rec);
268}
fe033cc8 269
1da177e4
LT
270/*
271 * Called from xfs_bmap_add_attrfork to handle btree format files.
272 */
273STATIC int /* error */
274xfs_bmap_add_attrfork_btree(
275 xfs_trans_t *tp, /* transaction pointer */
276 xfs_inode_t *ip, /* incore inode pointer */
277 xfs_fsblock_t *firstblock, /* first block allocated */
278 xfs_bmap_free_t *flist, /* blocks to free at commit */
279 int *flags) /* inode logging flags */
280{
281 xfs_btree_cur_t *cur; /* btree cursor */
282 int error; /* error return value */
283 xfs_mount_t *mp; /* file system mount struct */
284 int stat; /* newroot status */
285
286 mp = ip->i_mount;
287 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
288 *flags |= XFS_ILOG_DBROOT;
289 else {
561f7d17 290 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
1da177e4
LT
291 cur->bc_private.b.flist = flist;
292 cur->bc_private.b.firstblock = *firstblock;
293 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
294 goto error0;
6bd8fc8a
LM
295 /* must be at least one entry */
296 XFS_WANT_CORRUPTED_GOTO(stat == 1, error0);
ea77b0a6 297 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
1da177e4
LT
298 goto error0;
299 if (stat == 0) {
300 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
301 return XFS_ERROR(ENOSPC);
302 }
303 *firstblock = cur->bc_private.b.firstblock;
304 cur->bc_private.b.allocated = 0;
305 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
306 }
307 return 0;
308error0:
309 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
310 return error;
311}
312
313/*
314 * Called from xfs_bmap_add_attrfork to handle extents format files.
315 */
316STATIC int /* error */
317xfs_bmap_add_attrfork_extents(
318 xfs_trans_t *tp, /* transaction pointer */
319 xfs_inode_t *ip, /* incore inode pointer */
320 xfs_fsblock_t *firstblock, /* first block allocated */
321 xfs_bmap_free_t *flist, /* blocks to free at commit */
322 int *flags) /* inode logging flags */
323{
324 xfs_btree_cur_t *cur; /* bmap btree cursor */
325 int error; /* error return value */
326
327 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
328 return 0;
329 cur = NULL;
330 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0,
331 flags, XFS_DATA_FORK);
332 if (cur) {
333 cur->bc_private.b.allocated = 0;
334 xfs_btree_del_cursor(cur,
335 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
336 }
337 return error;
338}
339
340/*
341 * Called from xfs_bmap_add_attrfork to handle local format files.
342 */
343STATIC int /* error */
344xfs_bmap_add_attrfork_local(
345 xfs_trans_t *tp, /* transaction pointer */
346 xfs_inode_t *ip, /* incore inode pointer */
347 xfs_fsblock_t *firstblock, /* first block allocated */
348 xfs_bmap_free_t *flist, /* blocks to free at commit */
349 int *flags) /* inode logging flags */
350{
351 xfs_da_args_t dargs; /* args for dir/attr code */
352 int error; /* error return value */
353 xfs_mount_t *mp; /* mount structure pointer */
354
355 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
356 return 0;
abbede1b 357 if (S_ISDIR(ip->i_d.di_mode)) {
1da177e4
LT
358 mp = ip->i_mount;
359 memset(&dargs, 0, sizeof(dargs));
360 dargs.dp = ip;
361 dargs.firstblock = firstblock;
362 dargs.flist = flist;
363 dargs.total = mp->m_dirblkfsbs;
364 dargs.whichfork = XFS_DATA_FORK;
365 dargs.trans = tp;
f6c2d1fa 366 error = xfs_dir2_sf_to_block(&dargs);
1da177e4
LT
367 } else
368 error = xfs_bmap_local_to_extents(tp, ip, firstblock, 1, flags,
369 XFS_DATA_FORK);
370 return error;
371}
372
373/*
a5bd606b 374 * Convert a delayed allocation to a real allocation.
1da177e4
LT
375 */
376STATIC int /* error */
377xfs_bmap_add_extent_delay_real(
572a4cf0 378 struct xfs_bmalloca *bma)
1da177e4 379{
572a4cf0 380 struct xfs_bmbt_irec *new = &bma->got;
1da177e4 381 int diff; /* temp value */
a6f64d4a 382 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
1da177e4 383 int error; /* error return value */
1da177e4 384 int i; /* temp state */
4eea22f0 385 xfs_ifork_t *ifp; /* inode fork pointer */
1da177e4
LT
386 xfs_fileoff_t new_endoff; /* end offset of new entry */
387 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
388 /* left is 0, right is 1, prev is 2 */
389 int rval=0; /* return value (logging flags) */
390 int state = 0;/* state bits, accessed thru macros */
a5bd606b
CH
391 xfs_filblks_t da_new; /* new count del alloc blocks used */
392 xfs_filblks_t da_old; /* old count del alloc blocks used */
393 xfs_filblks_t temp=0; /* value for da_new calculations */
394 xfs_filblks_t temp2=0;/* value for da_new calculations */
1da177e4 395 int tmp_rval; /* partial logging flags */
1da177e4 396
572a4cf0 397 ifp = XFS_IFORK_PTR(bma->ip, XFS_DATA_FORK);
a5bd606b 398
572a4cf0
CH
399 ASSERT(bma->idx >= 0);
400 ASSERT(bma->idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
a5bd606b 401 ASSERT(!isnullstartblock(new->br_startblock));
572a4cf0
CH
402 ASSERT(!bma->cur ||
403 (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
a5bd606b
CH
404
405 XFS_STATS_INC(xs_add_exlist);
406
1da177e4
LT
407#define LEFT r[0]
408#define RIGHT r[1]
409#define PREV r[2]
1da177e4
LT
410
411 /*
412 * Set up a bunch of variables to make the tests simpler.
413 */
572a4cf0 414 ep = xfs_iext_get_ext(ifp, bma->idx);
1da177e4
LT
415 xfs_bmbt_get_all(ep, &PREV);
416 new_endoff = new->br_startoff + new->br_blockcount;
417 ASSERT(PREV.br_startoff <= new->br_startoff);
418 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
7574aa92 419
a5bd606b
CH
420 da_old = startblockval(PREV.br_startblock);
421 da_new = 0;
422
1da177e4
LT
423 /*
424 * Set flags determining what part of the previous delayed allocation
425 * extent is being replaced by a real allocation.
426 */
7574aa92
CH
427 if (PREV.br_startoff == new->br_startoff)
428 state |= BMAP_LEFT_FILLING;
429 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
430 state |= BMAP_RIGHT_FILLING;
431
1da177e4
LT
432 /*
433 * Check and set flags if this segment has a left neighbor.
434 * Don't set contiguous if the combined extent would be too large.
435 */
572a4cf0 436 if (bma->idx > 0) {
7574aa92 437 state |= BMAP_LEFT_VALID;
572a4cf0 438 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &LEFT);
7574aa92
CH
439
440 if (isnullstartblock(LEFT.br_startblock))
441 state |= BMAP_LEFT_DELAY;
1da177e4 442 }
7574aa92
CH
443
444 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
445 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
446 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
447 LEFT.br_state == new->br_state &&
448 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
449 state |= BMAP_LEFT_CONTIG;
450
1da177e4
LT
451 /*
452 * Check and set flags if this segment has a right neighbor.
453 * Don't set contiguous if the combined extent would be too large.
454 * Also check for all-three-contiguous being too large.
455 */
572a4cf0 456 if (bma->idx < bma->ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
7574aa92 457 state |= BMAP_RIGHT_VALID;
572a4cf0 458 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx + 1), &RIGHT);
7574aa92
CH
459
460 if (isnullstartblock(RIGHT.br_startblock))
461 state |= BMAP_RIGHT_DELAY;
1da177e4 462 }
7574aa92
CH
463
464 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
465 new_endoff == RIGHT.br_startoff &&
466 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
467 new->br_state == RIGHT.br_state &&
468 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
469 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
470 BMAP_RIGHT_FILLING)) !=
471 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
472 BMAP_RIGHT_FILLING) ||
473 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
474 <= MAXEXTLEN))
475 state |= BMAP_RIGHT_CONTIG;
476
1da177e4
LT
477 error = 0;
478 /*
479 * Switch out based on the FILLING and CONTIG state bits.
480 */
7574aa92
CH
481 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
482 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
483 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
484 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1da177e4
LT
485 /*
486 * Filling in all of a previously delayed allocation extent.
487 * The left and right neighbors are both contiguous with new.
488 */
572a4cf0
CH
489 bma->idx--;
490 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
491 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
1da177e4
LT
492 LEFT.br_blockcount + PREV.br_blockcount +
493 RIGHT.br_blockcount);
572a4cf0 494 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
0b1b213f 495
572a4cf0
CH
496 xfs_iext_remove(bma->ip, bma->idx + 1, 2, state);
497 bma->ip->i_d.di_nextents--;
498 if (bma->cur == NULL)
1da177e4
LT
499 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
500 else {
501 rval = XFS_ILOG_CORE;
572a4cf0 502 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
1da177e4 503 RIGHT.br_startblock,
572a4cf0
CH
504 RIGHT.br_blockcount, &i);
505 if (error)
1da177e4 506 goto done;
6bd8fc8a 507 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
572a4cf0
CH
508 error = xfs_btree_delete(bma->cur, &i);
509 if (error)
1da177e4 510 goto done;
6bd8fc8a 511 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
572a4cf0
CH
512 error = xfs_btree_decrement(bma->cur, 0, &i);
513 if (error)
1da177e4 514 goto done;
6bd8fc8a 515 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
572a4cf0 516 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
1da177e4
LT
517 LEFT.br_startblock,
518 LEFT.br_blockcount +
519 PREV.br_blockcount +
572a4cf0
CH
520 RIGHT.br_blockcount, LEFT.br_state);
521 if (error)
1da177e4
LT
522 goto done;
523 }
1da177e4
LT
524 break;
525
7574aa92 526 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1da177e4
LT
527 /*
528 * Filling in all of a previously delayed allocation extent.
529 * The left neighbor is contiguous, the right is not.
530 */
572a4cf0 531 bma->idx--;
ec90c556 532
572a4cf0
CH
533 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
534 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
1da177e4 535 LEFT.br_blockcount + PREV.br_blockcount);
572a4cf0 536 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
0b1b213f 537
572a4cf0
CH
538 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
539 if (bma->cur == NULL)
1da177e4
LT
540 rval = XFS_ILOG_DEXT;
541 else {
542 rval = 0;
572a4cf0 543 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
1da177e4 544 LEFT.br_startblock, LEFT.br_blockcount,
572a4cf0
CH
545 &i);
546 if (error)
1da177e4 547 goto done;
6bd8fc8a 548 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
572a4cf0 549 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
1da177e4
LT
550 LEFT.br_startblock,
551 LEFT.br_blockcount +
572a4cf0
CH
552 PREV.br_blockcount, LEFT.br_state);
553 if (error)
1da177e4
LT
554 goto done;
555 }
1da177e4
LT
556 break;
557
7574aa92 558 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1da177e4
LT
559 /*
560 * Filling in all of a previously delayed allocation extent.
561 * The right neighbor is contiguous, the left is not.
562 */
572a4cf0 563 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1da177e4
LT
564 xfs_bmbt_set_startblock(ep, new->br_startblock);
565 xfs_bmbt_set_blockcount(ep,
566 PREV.br_blockcount + RIGHT.br_blockcount);
572a4cf0 567 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
0b1b213f 568
572a4cf0
CH
569 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
570 if (bma->cur == NULL)
1da177e4
LT
571 rval = XFS_ILOG_DEXT;
572 else {
573 rval = 0;
572a4cf0 574 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
1da177e4 575 RIGHT.br_startblock,
572a4cf0
CH
576 RIGHT.br_blockcount, &i);
577 if (error)
1da177e4 578 goto done;
6bd8fc8a 579 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
572a4cf0 580 error = xfs_bmbt_update(bma->cur, PREV.br_startoff,
1da177e4
LT
581 new->br_startblock,
582 PREV.br_blockcount +
572a4cf0
CH
583 RIGHT.br_blockcount, PREV.br_state);
584 if (error)
1da177e4
LT
585 goto done;
586 }
1da177e4
LT
587 break;
588
7574aa92 589 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
1da177e4
LT
590 /*
591 * Filling in all of a previously delayed allocation extent.
592 * Neither the left nor right neighbors are contiguous with
593 * the new one.
594 */
572a4cf0 595 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1da177e4 596 xfs_bmbt_set_startblock(ep, new->br_startblock);
572a4cf0 597 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
0b1b213f 598
572a4cf0
CH
599 bma->ip->i_d.di_nextents++;
600 if (bma->cur == NULL)
1da177e4
LT
601 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
602 else {
603 rval = XFS_ILOG_CORE;
572a4cf0 604 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
1da177e4 605 new->br_startblock, new->br_blockcount,
572a4cf0
CH
606 &i);
607 if (error)
1da177e4 608 goto done;
6bd8fc8a 609 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
572a4cf0
CH
610 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
611 error = xfs_btree_insert(bma->cur, &i);
612 if (error)
1da177e4 613 goto done;
6bd8fc8a 614 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1da177e4 615 }
1da177e4
LT
616 break;
617
7574aa92 618 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
1da177e4
LT
619 /*
620 * Filling in the first part of a previous delayed allocation.
621 * The left neighbor is contiguous.
622 */
572a4cf0
CH
623 trace_xfs_bmap_pre_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
624 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx - 1),
1da177e4
LT
625 LEFT.br_blockcount + new->br_blockcount);
626 xfs_bmbt_set_startoff(ep,
627 PREV.br_startoff + new->br_blockcount);
572a4cf0 628 trace_xfs_bmap_post_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
0b1b213f 629
1da177e4 630 temp = PREV.br_blockcount - new->br_blockcount;
572a4cf0 631 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1da177e4 632 xfs_bmbt_set_blockcount(ep, temp);
572a4cf0 633 if (bma->cur == NULL)
1da177e4
LT
634 rval = XFS_ILOG_DEXT;
635 else {
636 rval = 0;
572a4cf0 637 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
1da177e4 638 LEFT.br_startblock, LEFT.br_blockcount,
572a4cf0
CH
639 &i);
640 if (error)
1da177e4 641 goto done;
6bd8fc8a 642 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
572a4cf0 643 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
1da177e4
LT
644 LEFT.br_startblock,
645 LEFT.br_blockcount +
646 new->br_blockcount,
572a4cf0
CH
647 LEFT.br_state);
648 if (error)
1da177e4
LT
649 goto done;
650 }
572a4cf0 651 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
9d87c319 652 startblockval(PREV.br_startblock));
a5bd606b 653 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
572a4cf0 654 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
ec90c556 655
572a4cf0 656 bma->idx--;
1da177e4
LT
657 break;
658
7574aa92 659 case BMAP_LEFT_FILLING:
1da177e4
LT
660 /*
661 * Filling in the first part of a previous delayed allocation.
662 * The left neighbor is not contiguous.
663 */
572a4cf0 664 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1da177e4
LT
665 xfs_bmbt_set_startoff(ep, new_endoff);
666 temp = PREV.br_blockcount - new->br_blockcount;
667 xfs_bmbt_set_blockcount(ep, temp);
572a4cf0
CH
668 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
669 bma->ip->i_d.di_nextents++;
670 if (bma->cur == NULL)
1da177e4
LT
671 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
672 else {
673 rval = XFS_ILOG_CORE;
572a4cf0 674 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
1da177e4 675 new->br_startblock, new->br_blockcount,
572a4cf0
CH
676 &i);
677 if (error)
1da177e4 678 goto done;
6bd8fc8a 679 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
572a4cf0
CH
680 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
681 error = xfs_btree_insert(bma->cur, &i);
682 if (error)
1da177e4 683 goto done;
6bd8fc8a 684 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1da177e4 685 }
572a4cf0
CH
686 if (bma->ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
687 bma->ip->i_d.di_nextents > bma->ip->i_df.if_ext_max) {
688 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
689 bma->firstblock, bma->flist,
690 &bma->cur, 1, &tmp_rval, XFS_DATA_FORK);
1da177e4
LT
691 rval |= tmp_rval;
692 if (error)
693 goto done;
694 }
572a4cf0 695 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
9d87c319 696 startblockval(PREV.br_startblock) -
572a4cf0
CH
697 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
698 ep = xfs_iext_get_ext(ifp, bma->idx + 1);
a5bd606b 699 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
572a4cf0 700 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
1da177e4
LT
701 break;
702
7574aa92 703 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1da177e4
LT
704 /*
705 * Filling in the last part of a previous delayed allocation.
706 * The right neighbor is contiguous with the new allocation.
707 */
708 temp = PREV.br_blockcount - new->br_blockcount;
572a4cf0 709 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
1da177e4 710 xfs_bmbt_set_blockcount(ep, temp);
572a4cf0 711 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx + 1),
4eea22f0 712 new->br_startoff, new->br_startblock,
1da177e4
LT
713 new->br_blockcount + RIGHT.br_blockcount,
714 RIGHT.br_state);
572a4cf0
CH
715 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
716 if (bma->cur == NULL)
1da177e4
LT
717 rval = XFS_ILOG_DEXT;
718 else {
719 rval = 0;
572a4cf0 720 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
1da177e4 721 RIGHT.br_startblock,
572a4cf0
CH
722 RIGHT.br_blockcount, &i);
723 if (error)
1da177e4 724 goto done;
6bd8fc8a 725 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
572a4cf0 726 error = xfs_bmbt_update(bma->cur, new->br_startoff,
1da177e4
LT
727 new->br_startblock,
728 new->br_blockcount +
729 RIGHT.br_blockcount,
572a4cf0
CH
730 RIGHT.br_state);
731 if (error)
1da177e4
LT
732 goto done;
733 }
ec90c556 734
572a4cf0 735 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
9d87c319 736 startblockval(PREV.br_startblock));
572a4cf0 737 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
a5bd606b 738 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
572a4cf0 739 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
ec90c556 740
572a4cf0 741 bma->idx++;
1da177e4
LT
742 break;
743
7574aa92 744 case BMAP_RIGHT_FILLING:
1da177e4
LT
745 /*
746 * Filling in the last part of a previous delayed allocation.
747 * The right neighbor is not contiguous.
748 */
749 temp = PREV.br_blockcount - new->br_blockcount;
572a4cf0 750 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1da177e4 751 xfs_bmbt_set_blockcount(ep, temp);
572a4cf0
CH
752 xfs_iext_insert(bma->ip, bma->idx + 1, 1, new, state);
753 bma->ip->i_d.di_nextents++;
754 if (bma->cur == NULL)
1da177e4
LT
755 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
756 else {
757 rval = XFS_ILOG_CORE;
572a4cf0 758 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
1da177e4 759 new->br_startblock, new->br_blockcount,
572a4cf0
CH
760 &i);
761 if (error)
1da177e4 762 goto done;
6bd8fc8a 763 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
572a4cf0
CH
764 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
765 error = xfs_btree_insert(bma->cur, &i);
766 if (error)
1da177e4 767 goto done;
6bd8fc8a 768 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1da177e4 769 }
572a4cf0
CH
770 if (bma->ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
771 bma->ip->i_d.di_nextents > bma->ip->i_df.if_ext_max) {
772 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
773 bma->firstblock, bma->flist, &bma->cur, 1,
774 &tmp_rval, XFS_DATA_FORK);
1da177e4
LT
775 rval |= tmp_rval;
776 if (error)
777 goto done;
778 }
572a4cf0 779 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
9d87c319 780 startblockval(PREV.br_startblock) -
572a4cf0
CH
781 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
782 ep = xfs_iext_get_ext(ifp, bma->idx);
a5bd606b 783 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
572a4cf0 784 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
ec90c556 785
572a4cf0 786 bma->idx++;
1da177e4
LT
787 break;
788
789 case 0:
790 /*
791 * Filling in the middle part of a previous delayed allocation.
792 * Contiguity is impossible here.
793 * This case is avoided almost all the time.
24446fc6 794 *
795 * We start with a delayed allocation:
796 *
797 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
798 * PREV @ idx
799 *
800 * and we are allocating:
801 * +rrrrrrrrrrrrrrrrr+
802 * new
803 *
804 * and we set it up for insertion as:
805 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
806 * new
807 * PREV @ idx LEFT RIGHT
808 * inserted at idx + 1
1da177e4
LT
809 */
810 temp = new->br_startoff - PREV.br_startoff;
1da177e4 811 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
572a4cf0 812 trace_xfs_bmap_pre_update(bma->ip, bma->idx, 0, _THIS_IP_);
24446fc6 813 xfs_bmbt_set_blockcount(ep, temp); /* truncate PREV */
814 LEFT = *new;
815 RIGHT.br_state = PREV.br_state;
816 RIGHT.br_startblock = nullstartblock(
572a4cf0 817 (int)xfs_bmap_worst_indlen(bma->ip, temp2));
24446fc6 818 RIGHT.br_startoff = new_endoff;
819 RIGHT.br_blockcount = temp2;
820 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
572a4cf0
CH
821 xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state);
822 bma->ip->i_d.di_nextents++;
823 if (bma->cur == NULL)
1da177e4
LT
824 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
825 else {
826 rval = XFS_ILOG_CORE;
572a4cf0 827 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
1da177e4 828 new->br_startblock, new->br_blockcount,
572a4cf0
CH
829 &i);
830 if (error)
1da177e4 831 goto done;
6bd8fc8a 832 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
572a4cf0
CH
833 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
834 error = xfs_btree_insert(bma->cur, &i);
835 if (error)
1da177e4 836 goto done;
6bd8fc8a 837 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1da177e4 838 }
572a4cf0
CH
839 if (bma->ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
840 bma->ip->i_d.di_nextents > bma->ip->i_df.if_ext_max) {
841 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
842 bma->firstblock, bma->flist, &bma->cur,
843 1, &tmp_rval, XFS_DATA_FORK);
1da177e4
LT
844 rval |= tmp_rval;
845 if (error)
846 goto done;
847 }
572a4cf0
CH
848 temp = xfs_bmap_worst_indlen(bma->ip, temp);
849 temp2 = xfs_bmap_worst_indlen(bma->ip, temp2);
9d87c319 850 diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) -
572a4cf0 851 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
b9b984d7 852 if (diff > 0) {
572a4cf0 853 error = xfs_icsb_modify_counters(bma->ip->i_mount,
b9b984d7
CH
854 XFS_SBS_FDBLOCKS,
855 -((int64_t)diff), 0);
856 ASSERT(!error);
857 if (error)
858 goto done;
1da177e4 859 }
b9b984d7 860
572a4cf0 861 ep = xfs_iext_get_ext(ifp, bma->idx);
9d87c319 862 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
572a4cf0
CH
863 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
864 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
865 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, bma->idx + 2),
9d87c319 866 nullstartblock((int)temp2));
572a4cf0 867 trace_xfs_bmap_post_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
ec90c556 868
572a4cf0 869 bma->idx++;
a5bd606b 870 da_new = temp + temp2;
1da177e4
LT
871 break;
872
7574aa92
CH
873 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
874 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
875 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
876 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
877 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
878 case BMAP_LEFT_CONTIG:
879 case BMAP_RIGHT_CONTIG:
1da177e4
LT
880 /*
881 * These cases are all impossible.
882 */
883 ASSERT(0);
884 }
a5bd606b
CH
885
886 /* convert to a btree if necessary */
572a4cf0
CH
887 if (XFS_IFORK_FORMAT(bma->ip, XFS_DATA_FORK) == XFS_DINODE_FMT_EXTENTS &&
888 XFS_IFORK_NEXTENTS(bma->ip, XFS_DATA_FORK) > ifp->if_ext_max) {
a5bd606b
CH
889 int tmp_logflags; /* partial log flag return val */
890
572a4cf0
CH
891 ASSERT(bma->cur == NULL);
892 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
893 bma->firstblock, bma->flist, &bma->cur,
a5bd606b 894 da_old > 0, &tmp_logflags, XFS_DATA_FORK);
572a4cf0 895 bma->logflags |= tmp_logflags;
a5bd606b
CH
896 if (error)
897 goto done;
898 }
899
900 /* adjust for changes in reserved delayed indirect blocks */
901 if (da_old || da_new) {
902 temp = da_new;
572a4cf0
CH
903 if (bma->cur)
904 temp += bma->cur->bc_private.b.allocated;
a5bd606b
CH
905 ASSERT(temp <= da_old);
906 if (temp < da_old)
572a4cf0
CH
907 xfs_icsb_modify_counters(bma->ip->i_mount,
908 XFS_SBS_FDBLOCKS,
909 (int64_t)(da_old - temp), 0);
a5bd606b
CH
910 }
911
912 /* clear out the allocated field, done with it now in any case. */
572a4cf0
CH
913 if (bma->cur)
914 bma->cur->bc_private.b.allocated = 0;
915
916 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, XFS_DATA_FORK);
1da177e4 917done:
572a4cf0 918 bma->logflags |= rval;
1da177e4
LT
919 return error;
920#undef LEFT
921#undef RIGHT
922#undef PREV
1da177e4
LT
923}
924
925/*
a5bd606b 926 * Convert an unwritten allocation to a real allocation or vice versa.
1da177e4
LT
927 */
928STATIC int /* error */
929xfs_bmap_add_extent_unwritten_real(
a5bd606b 930 struct xfs_trans *tp,
1da177e4 931 xfs_inode_t *ip, /* incore inode pointer */
ec90c556 932 xfs_extnum_t *idx, /* extent number to update/insert */
1da177e4 933 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
4eea22f0 934 xfs_bmbt_irec_t *new, /* new data to add to file extents */
a5bd606b
CH
935 xfs_fsblock_t *first, /* pointer to firstblock variable */
936 xfs_bmap_free_t *flist, /* list of extents to be freed */
b4e9181e 937 int *logflagsp) /* inode logging flags */
1da177e4 938{
1da177e4 939 xfs_btree_cur_t *cur; /* btree cursor */
a6f64d4a 940 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
1da177e4 941 int error; /* error return value */
1da177e4 942 int i; /* temp state */
4eea22f0 943 xfs_ifork_t *ifp; /* inode fork pointer */
1da177e4
LT
944 xfs_fileoff_t new_endoff; /* end offset of new entry */
945 xfs_exntst_t newext; /* new extent state */
946 xfs_exntst_t oldext; /* old extent state */
947 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
948 /* left is 0, right is 1, prev is 2 */
949 int rval=0; /* return value (logging flags) */
950 int state = 0;/* state bits, accessed thru macros */
1da177e4 951
a5bd606b
CH
952 *logflagsp = 0;
953
954 cur = *curp;
955 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
956
957 ASSERT(*idx >= 0);
958 ASSERT(*idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
959 ASSERT(!isnullstartblock(new->br_startblock));
960
961 XFS_STATS_INC(xs_add_exlist);
962
1da177e4
LT
963#define LEFT r[0]
964#define RIGHT r[1]
965#define PREV r[2]
a5bd606b 966
1da177e4
LT
967 /*
968 * Set up a bunch of variables to make the tests simpler.
969 */
970 error = 0;
ec90c556 971 ep = xfs_iext_get_ext(ifp, *idx);
1da177e4
LT
972 xfs_bmbt_get_all(ep, &PREV);
973 newext = new->br_state;
974 oldext = (newext == XFS_EXT_UNWRITTEN) ?
975 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
976 ASSERT(PREV.br_state == oldext);
977 new_endoff = new->br_startoff + new->br_blockcount;
978 ASSERT(PREV.br_startoff <= new->br_startoff);
979 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
7574aa92 980
1da177e4
LT
981 /*
982 * Set flags determining what part of the previous oldext allocation
983 * extent is being replaced by a newext allocation.
984 */
7574aa92
CH
985 if (PREV.br_startoff == new->br_startoff)
986 state |= BMAP_LEFT_FILLING;
987 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
988 state |= BMAP_RIGHT_FILLING;
989
1da177e4
LT
990 /*
991 * Check and set flags if this segment has a left neighbor.
992 * Don't set contiguous if the combined extent would be too large.
993 */
ec90c556 994 if (*idx > 0) {
7574aa92 995 state |= BMAP_LEFT_VALID;
ec90c556 996 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT);
7574aa92
CH
997
998 if (isnullstartblock(LEFT.br_startblock))
999 state |= BMAP_LEFT_DELAY;
1da177e4 1000 }
7574aa92
CH
1001
1002 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1003 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1004 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1005 LEFT.br_state == newext &&
1006 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1007 state |= BMAP_LEFT_CONTIG;
1008
1da177e4
LT
1009 /*
1010 * Check and set flags if this segment has a right neighbor.
1011 * Don't set contiguous if the combined extent would be too large.
1012 * Also check for all-three-contiguous being too large.
1013 */
ec90c556 1014 if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
7574aa92 1015 state |= BMAP_RIGHT_VALID;
ec90c556 1016 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT);
7574aa92
CH
1017 if (isnullstartblock(RIGHT.br_startblock))
1018 state |= BMAP_RIGHT_DELAY;
1da177e4 1019 }
7574aa92
CH
1020
1021 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1022 new_endoff == RIGHT.br_startoff &&
1023 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1024 newext == RIGHT.br_state &&
1025 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1026 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1027 BMAP_RIGHT_FILLING)) !=
1028 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1029 BMAP_RIGHT_FILLING) ||
1030 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1031 <= MAXEXTLEN))
1032 state |= BMAP_RIGHT_CONTIG;
1033
1da177e4
LT
1034 /*
1035 * Switch out based on the FILLING and CONTIG state bits.
1036 */
7574aa92
CH
1037 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1038 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1039 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1040 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1da177e4
LT
1041 /*
1042 * Setting all of a previous oldext extent to newext.
1043 * The left and right neighbors are both contiguous with new.
1044 */
ec90c556
CH
1045 --*idx;
1046
1047 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1048 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
1da177e4
LT
1049 LEFT.br_blockcount + PREV.br_blockcount +
1050 RIGHT.br_blockcount);
ec90c556 1051 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
0b1b213f 1052
ec90c556 1053 xfs_iext_remove(ip, *idx + 1, 2, state);
1da177e4
LT
1054 ip->i_d.di_nextents -= 2;
1055 if (cur == NULL)
1056 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1057 else {
1058 rval = XFS_ILOG_CORE;
1059 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1060 RIGHT.br_startblock,
1061 RIGHT.br_blockcount, &i)))
1062 goto done;
6bd8fc8a 1063 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
91cca5df 1064 if ((error = xfs_btree_delete(cur, &i)))
1da177e4 1065 goto done;
6bd8fc8a 1066 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
8df4da4a 1067 if ((error = xfs_btree_decrement(cur, 0, &i)))
1da177e4 1068 goto done;
6bd8fc8a 1069 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
91cca5df 1070 if ((error = xfs_btree_delete(cur, &i)))
1da177e4 1071 goto done;
6bd8fc8a 1072 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
8df4da4a 1073 if ((error = xfs_btree_decrement(cur, 0, &i)))
1da177e4 1074 goto done;
6bd8fc8a 1075 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1da177e4
LT
1076 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1077 LEFT.br_startblock,
1078 LEFT.br_blockcount + PREV.br_blockcount +
1079 RIGHT.br_blockcount, LEFT.br_state)))
1080 goto done;
1081 }
1082 break;
1083
7574aa92 1084 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1da177e4
LT
1085 /*
1086 * Setting all of a previous oldext extent to newext.
1087 * The left neighbor is contiguous, the right is not.
1088 */
ec90c556
CH
1089 --*idx;
1090
1091 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1092 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
1da177e4 1093 LEFT.br_blockcount + PREV.br_blockcount);
ec90c556 1094 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
0b1b213f 1095
ec90c556 1096 xfs_iext_remove(ip, *idx + 1, 1, state);
1da177e4
LT
1097 ip->i_d.di_nextents--;
1098 if (cur == NULL)
1099 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1100 else {
1101 rval = XFS_ILOG_CORE;
1102 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1103 PREV.br_startblock, PREV.br_blockcount,
1104 &i)))
1105 goto done;
6bd8fc8a 1106 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
91cca5df 1107 if ((error = xfs_btree_delete(cur, &i)))
1da177e4 1108 goto done;
6bd8fc8a 1109 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
8df4da4a 1110 if ((error = xfs_btree_decrement(cur, 0, &i)))
1da177e4 1111 goto done;
6bd8fc8a 1112 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1da177e4
LT
1113 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1114 LEFT.br_startblock,
1115 LEFT.br_blockcount + PREV.br_blockcount,
1116 LEFT.br_state)))
1117 goto done;
1118 }
1119 break;
1120
7574aa92 1121 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1da177e4
LT
1122 /*
1123 * Setting all of a previous oldext extent to newext.
1124 * The right neighbor is contiguous, the left is not.
1125 */
ec90c556 1126 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1da177e4
LT
1127 xfs_bmbt_set_blockcount(ep,
1128 PREV.br_blockcount + RIGHT.br_blockcount);
1129 xfs_bmbt_set_state(ep, newext);
ec90c556
CH
1130 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1131 xfs_iext_remove(ip, *idx + 1, 1, state);
1da177e4
LT
1132 ip->i_d.di_nextents--;
1133 if (cur == NULL)
1134 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1135 else {
1136 rval = XFS_ILOG_CORE;
1137 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1138 RIGHT.br_startblock,
1139 RIGHT.br_blockcount, &i)))
1140 goto done;
6bd8fc8a 1141 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
91cca5df 1142 if ((error = xfs_btree_delete(cur, &i)))
1da177e4 1143 goto done;
6bd8fc8a 1144 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
8df4da4a 1145 if ((error = xfs_btree_decrement(cur, 0, &i)))
1da177e4 1146 goto done;
6bd8fc8a 1147 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1da177e4
LT
1148 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1149 new->br_startblock,
1150 new->br_blockcount + RIGHT.br_blockcount,
1151 newext)))
1152 goto done;
1153 }
1154 break;
1155
7574aa92 1156 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
1da177e4
LT
1157 /*
1158 * Setting all of a previous oldext extent to newext.
1159 * Neither the left nor right neighbors are contiguous with
1160 * the new one.
1161 */
ec90c556 1162 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1da177e4 1163 xfs_bmbt_set_state(ep, newext);
ec90c556 1164 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
0b1b213f 1165
1da177e4
LT
1166 if (cur == NULL)
1167 rval = XFS_ILOG_DEXT;
1168 else {
1169 rval = 0;
1170 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1171 new->br_startblock, new->br_blockcount,
1172 &i)))
1173 goto done;
6bd8fc8a 1174 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1da177e4
LT
1175 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1176 new->br_startblock, new->br_blockcount,
1177 newext)))
1178 goto done;
1179 }
1180 break;
1181
7574aa92 1182 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
1da177e4
LT
1183 /*
1184 * Setting the first part of a previous oldext extent to newext.
1185 * The left neighbor is contiguous.
1186 */
ec90c556
CH
1187 trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_);
1188 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1),
1da177e4
LT
1189 LEFT.br_blockcount + new->br_blockcount);
1190 xfs_bmbt_set_startoff(ep,
1191 PREV.br_startoff + new->br_blockcount);
ec90c556 1192 trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_);
0b1b213f 1193
ec90c556 1194 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1da177e4
LT
1195 xfs_bmbt_set_startblock(ep,
1196 new->br_startblock + new->br_blockcount);
1197 xfs_bmbt_set_blockcount(ep,
1198 PREV.br_blockcount - new->br_blockcount);
ec90c556
CH
1199 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1200
1201 --*idx;
0b1b213f 1202
1da177e4
LT
1203 if (cur == NULL)
1204 rval = XFS_ILOG_DEXT;
1205 else {
1206 rval = 0;
1207 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1208 PREV.br_startblock, PREV.br_blockcount,
1209 &i)))
1210 goto done;
6bd8fc8a 1211 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1da177e4
LT
1212 if ((error = xfs_bmbt_update(cur,
1213 PREV.br_startoff + new->br_blockcount,
1214 PREV.br_startblock + new->br_blockcount,
1215 PREV.br_blockcount - new->br_blockcount,
1216 oldext)))
1217 goto done;
8df4da4a 1218 if ((error = xfs_btree_decrement(cur, 0, &i)))
1da177e4
LT
1219 goto done;
1220 if (xfs_bmbt_update(cur, LEFT.br_startoff,
1221 LEFT.br_startblock,
1222 LEFT.br_blockcount + new->br_blockcount,
1223 LEFT.br_state))
1224 goto done;
1225 }
1226 break;
1227
7574aa92 1228 case BMAP_LEFT_FILLING:
1da177e4
LT
1229 /*
1230 * Setting the first part of a previous oldext extent to newext.
1231 * The left neighbor is not contiguous.
1232 */
ec90c556 1233 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1da177e4
LT
1234 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
1235 xfs_bmbt_set_startoff(ep, new_endoff);
1236 xfs_bmbt_set_blockcount(ep,
1237 PREV.br_blockcount - new->br_blockcount);
1238 xfs_bmbt_set_startblock(ep,
1239 new->br_startblock + new->br_blockcount);
ec90c556 1240 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
0b1b213f 1241
ec90c556 1242 xfs_iext_insert(ip, *idx, 1, new, state);
1da177e4
LT
1243 ip->i_d.di_nextents++;
1244 if (cur == NULL)
1245 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1246 else {
1247 rval = XFS_ILOG_CORE;
1248 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1249 PREV.br_startblock, PREV.br_blockcount,
1250 &i)))
1251 goto done;
6bd8fc8a 1252 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1da177e4
LT
1253 if ((error = xfs_bmbt_update(cur,
1254 PREV.br_startoff + new->br_blockcount,
1255 PREV.br_startblock + new->br_blockcount,
1256 PREV.br_blockcount - new->br_blockcount,
1257 oldext)))
1258 goto done;
1259 cur->bc_rec.b = *new;
4b22a571 1260 if ((error = xfs_btree_insert(cur, &i)))
1da177e4 1261 goto done;
6bd8fc8a 1262 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1da177e4
LT
1263 }
1264 break;
1265
7574aa92 1266 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1da177e4
LT
1267 /*
1268 * Setting the last part of a previous oldext extent to newext.
1269 * The right neighbor is contiguous with the new allocation.
1270 */
ec90c556 1271 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1da177e4
LT
1272 xfs_bmbt_set_blockcount(ep,
1273 PREV.br_blockcount - new->br_blockcount);
ec90c556
CH
1274 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1275
1276 ++*idx;
1277
1278 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1279 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
4eea22f0 1280 new->br_startoff, new->br_startblock,
1da177e4 1281 new->br_blockcount + RIGHT.br_blockcount, newext);
ec90c556 1282 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
0b1b213f 1283
1da177e4
LT
1284 if (cur == NULL)
1285 rval = XFS_ILOG_DEXT;
1286 else {
1287 rval = 0;
1288 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1289 PREV.br_startblock,
1290 PREV.br_blockcount, &i)))
1291 goto done;
6bd8fc8a 1292 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1da177e4
LT
1293 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1294 PREV.br_startblock,
1295 PREV.br_blockcount - new->br_blockcount,
1296 oldext)))
1297 goto done;
637aa50f 1298 if ((error = xfs_btree_increment(cur, 0, &i)))
1da177e4
LT
1299 goto done;
1300 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1301 new->br_startblock,
1302 new->br_blockcount + RIGHT.br_blockcount,
1303 newext)))
1304 goto done;
1305 }
1306 break;
1307
7574aa92 1308 case BMAP_RIGHT_FILLING:
1da177e4
LT
1309 /*
1310 * Setting the last part of a previous oldext extent to newext.
1311 * The right neighbor is not contiguous.
1312 */
ec90c556 1313 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1da177e4
LT
1314 xfs_bmbt_set_blockcount(ep,
1315 PREV.br_blockcount - new->br_blockcount);
ec90c556
CH
1316 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1317
1318 ++*idx;
1319 xfs_iext_insert(ip, *idx, 1, new, state);
0b1b213f 1320
1da177e4
LT
1321 ip->i_d.di_nextents++;
1322 if (cur == NULL)
1323 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1324 else {
1325 rval = XFS_ILOG_CORE;
1326 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1327 PREV.br_startblock, PREV.br_blockcount,
1328 &i)))
1329 goto done;
6bd8fc8a 1330 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1da177e4
LT
1331 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1332 PREV.br_startblock,
1333 PREV.br_blockcount - new->br_blockcount,
1334 oldext)))
1335 goto done;
1336 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1337 new->br_startblock, new->br_blockcount,
1338 &i)))
1339 goto done;
6bd8fc8a 1340 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1da177e4 1341 cur->bc_rec.b.br_state = XFS_EXT_NORM;
4b22a571 1342 if ((error = xfs_btree_insert(cur, &i)))
1da177e4 1343 goto done;
6bd8fc8a 1344 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1da177e4
LT
1345 }
1346 break;
1347
1348 case 0:
1349 /*
1350 * Setting the middle part of a previous oldext extent to
1351 * newext. Contiguity is impossible here.
1352 * One extent becomes three extents.
1353 */
ec90c556 1354 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1da177e4
LT
1355 xfs_bmbt_set_blockcount(ep,
1356 new->br_startoff - PREV.br_startoff);
ec90c556 1357 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
0b1b213f 1358
1da177e4
LT
1359 r[0] = *new;
1360 r[1].br_startoff = new_endoff;
1361 r[1].br_blockcount =
1362 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1363 r[1].br_startblock = new->br_startblock + new->br_blockcount;
1364 r[1].br_state = oldext;
ec90c556
CH
1365
1366 ++*idx;
1367 xfs_iext_insert(ip, *idx, 2, &r[0], state);
1368
1da177e4
LT
1369 ip->i_d.di_nextents += 2;
1370 if (cur == NULL)
1371 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1372 else {
1373 rval = XFS_ILOG_CORE;
1374 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1375 PREV.br_startblock, PREV.br_blockcount,
1376 &i)))
1377 goto done;
6bd8fc8a 1378 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1da177e4
LT
1379 /* new right extent - oldext */
1380 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
1381 r[1].br_startblock, r[1].br_blockcount,
1382 r[1].br_state)))
1383 goto done;
1384 /* new left extent - oldext */
1da177e4 1385 cur->bc_rec.b = PREV;
6a617dd2
TS
1386 cur->bc_rec.b.br_blockcount =
1387 new->br_startoff - PREV.br_startoff;
4b22a571 1388 if ((error = xfs_btree_insert(cur, &i)))
1da177e4 1389 goto done;
6bd8fc8a 1390 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
ddea2d52
LM
1391 /*
1392 * Reset the cursor to the position of the new extent
1393 * we are about to insert as we can't trust it after
1394 * the previous insert.
1395 */
1396 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1397 new->br_startblock, new->br_blockcount,
1398 &i)))
1da177e4 1399 goto done;
ddea2d52 1400 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1da177e4 1401 /* new middle extent - newext */
ddea2d52 1402 cur->bc_rec.b.br_state = new->br_state;
4b22a571 1403 if ((error = xfs_btree_insert(cur, &i)))
1da177e4 1404 goto done;
6bd8fc8a 1405 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1da177e4
LT
1406 }
1407 break;
1408
7574aa92
CH
1409 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1410 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1411 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1412 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1413 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1414 case BMAP_LEFT_CONTIG:
1415 case BMAP_RIGHT_CONTIG:
1da177e4
LT
1416 /*
1417 * These cases are all impossible.
1418 */
1419 ASSERT(0);
1420 }
a5bd606b
CH
1421
1422 /* convert to a btree if necessary */
1423 if (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) == XFS_DINODE_FMT_EXTENTS &&
1424 XFS_IFORK_NEXTENTS(ip, XFS_DATA_FORK) > ifp->if_ext_max) {
1425 int tmp_logflags; /* partial log flag return val */
1426
1427 ASSERT(cur == NULL);
1428 error = xfs_bmap_extents_to_btree(tp, ip, first, flist, &cur,
1429 0, &tmp_logflags, XFS_DATA_FORK);
1430 *logflagsp |= tmp_logflags;
1431 if (error)
1432 goto done;
1433 }
1434
1435 /* clear out the allocated field, done with it now in any case. */
1436 if (cur) {
1437 cur->bc_private.b.allocated = 0;
1438 *curp = cur;
1439 }
1440
1441 xfs_bmap_check_leaf_extents(*curp, ip, XFS_DATA_FORK);
1da177e4 1442done:
a5bd606b 1443 *logflagsp |= rval;
1da177e4
LT
1444 return error;
1445#undef LEFT
1446#undef RIGHT
1447#undef PREV
1da177e4
LT
1448}
1449
1450/*
1fd044d9 1451 * Convert a hole to a delayed allocation.
1da177e4 1452 */
1fd044d9 1453STATIC void
1da177e4
LT
1454xfs_bmap_add_extent_hole_delay(
1455 xfs_inode_t *ip, /* incore inode pointer */
ec90c556 1456 xfs_extnum_t *idx, /* extent number to update/insert */
1fd044d9 1457 xfs_bmbt_irec_t *new) /* new data to add to file extents */
1da177e4 1458{
4eea22f0 1459 xfs_ifork_t *ifp; /* inode fork pointer */
1da177e4
LT
1460 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1461 xfs_filblks_t newlen=0; /* new indirect size */
1462 xfs_filblks_t oldlen=0; /* old indirect size */
1463 xfs_bmbt_irec_t right; /* right neighbor extent entry */
1464 int state; /* state bits, accessed thru macros */
3e57ecf6 1465 xfs_filblks_t temp=0; /* temp for indirect calculations */
1da177e4 1466
4eea22f0 1467 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1da177e4 1468 state = 0;
9d87c319 1469 ASSERT(isnullstartblock(new->br_startblock));
7574aa92 1470
1da177e4
LT
1471 /*
1472 * Check and set flags if this segment has a left neighbor
1473 */
ec90c556 1474 if (*idx > 0) {
7574aa92 1475 state |= BMAP_LEFT_VALID;
ec90c556 1476 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left);
7574aa92
CH
1477
1478 if (isnullstartblock(left.br_startblock))
1479 state |= BMAP_LEFT_DELAY;
1da177e4 1480 }
7574aa92 1481
1da177e4
LT
1482 /*
1483 * Check and set flags if the current (right) segment exists.
1484 * If it doesn't exist, we're converting the hole at end-of-file.
1485 */
ec90c556 1486 if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
7574aa92 1487 state |= BMAP_RIGHT_VALID;
2f2b3220 1488 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right);
7574aa92
CH
1489
1490 if (isnullstartblock(right.br_startblock))
1491 state |= BMAP_RIGHT_DELAY;
1da177e4 1492 }
7574aa92 1493
1da177e4
LT
1494 /*
1495 * Set contiguity flags on the left and right neighbors.
1496 * Don't let extents get too large, even if the pieces are contiguous.
1497 */
7574aa92
CH
1498 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
1499 left.br_startoff + left.br_blockcount == new->br_startoff &&
1500 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1501 state |= BMAP_LEFT_CONTIG;
1502
1503 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
1504 new->br_startoff + new->br_blockcount == right.br_startoff &&
1505 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1506 (!(state & BMAP_LEFT_CONTIG) ||
1507 (left.br_blockcount + new->br_blockcount +
1508 right.br_blockcount <= MAXEXTLEN)))
1509 state |= BMAP_RIGHT_CONTIG;
1510
1da177e4
LT
1511 /*
1512 * Switch out based on the contiguity flags.
1513 */
7574aa92
CH
1514 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
1515 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1da177e4
LT
1516 /*
1517 * New allocation is contiguous with delayed allocations
1518 * on the left and on the right.
4eea22f0 1519 * Merge all three into a single extent record.
1da177e4 1520 */
ec90c556 1521 --*idx;
1da177e4
LT
1522 temp = left.br_blockcount + new->br_blockcount +
1523 right.br_blockcount;
0b1b213f 1524
ec90c556
CH
1525 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1526 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
9d87c319
ES
1527 oldlen = startblockval(left.br_startblock) +
1528 startblockval(new->br_startblock) +
1529 startblockval(right.br_startblock);
1da177e4 1530 newlen = xfs_bmap_worst_indlen(ip, temp);
ec90c556 1531 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
9d87c319 1532 nullstartblock((int)newlen));
ec90c556 1533 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
0b1b213f 1534
ec90c556 1535 xfs_iext_remove(ip, *idx + 1, 1, state);
1da177e4
LT
1536 break;
1537
7574aa92 1538 case BMAP_LEFT_CONTIG:
1da177e4
LT
1539 /*
1540 * New allocation is contiguous with a delayed allocation
1541 * on the left.
1542 * Merge the new allocation with the left neighbor.
1543 */
ec90c556 1544 --*idx;
1da177e4 1545 temp = left.br_blockcount + new->br_blockcount;
ec90c556
CH
1546
1547 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1548 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
9d87c319
ES
1549 oldlen = startblockval(left.br_startblock) +
1550 startblockval(new->br_startblock);
1da177e4 1551 newlen = xfs_bmap_worst_indlen(ip, temp);
ec90c556 1552 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
9d87c319 1553 nullstartblock((int)newlen));
ec90c556 1554 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1da177e4
LT
1555 break;
1556
7574aa92 1557 case BMAP_RIGHT_CONTIG:
1da177e4
LT
1558 /*
1559 * New allocation is contiguous with a delayed allocation
1560 * on the right.
1561 * Merge the new allocation with the right neighbor.
1562 */
ec90c556 1563 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1da177e4 1564 temp = new->br_blockcount + right.br_blockcount;
9d87c319
ES
1565 oldlen = startblockval(new->br_startblock) +
1566 startblockval(right.br_startblock);
1da177e4 1567 newlen = xfs_bmap_worst_indlen(ip, temp);
2f2b3220
CH
1568 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
1569 new->br_startoff,
9d87c319 1570 nullstartblock((int)newlen), temp, right.br_state);
ec90c556 1571 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1da177e4
LT
1572 break;
1573
1574 case 0:
1575 /*
1576 * New allocation is not contiguous with another
1577 * delayed allocation.
1578 * Insert a new entry.
1579 */
1580 oldlen = newlen = 0;
ec90c556 1581 xfs_iext_insert(ip, *idx, 1, new, state);
1da177e4
LT
1582 break;
1583 }
1584 if (oldlen != newlen) {
1585 ASSERT(oldlen > newlen);
96540c78 1586 xfs_icsb_modify_counters(ip->i_mount, XFS_SBS_FDBLOCKS,
54893273 1587 (int64_t)(oldlen - newlen), 0);
1da177e4
LT
1588 /*
1589 * Nothing to do for disk quota accounting here.
1590 */
1591 }
1da177e4
LT
1592}
1593
1594/*
a5bd606b 1595 * Convert a hole to a real allocation.
1da177e4
LT
1596 */
1597STATIC int /* error */
1598xfs_bmap_add_extent_hole_real(
a5bd606b 1599 struct xfs_trans *tp,
1da177e4 1600 xfs_inode_t *ip, /* incore inode pointer */
ec90c556 1601 xfs_extnum_t *idx, /* extent number to update/insert */
a5bd606b 1602 xfs_btree_cur_t **curp, /* if null, not a btree */
4eea22f0 1603 xfs_bmbt_irec_t *new, /* new data to add to file extents */
a5bd606b
CH
1604 xfs_fsblock_t *first, /* pointer to firstblock variable */
1605 xfs_bmap_free_t *flist, /* list of extents to be freed */
1da177e4
LT
1606 int *logflagsp, /* inode logging flags */
1607 int whichfork) /* data or attr fork */
1608{
1da177e4 1609 int error; /* error return value */
1da177e4 1610 int i; /* temp state */
a5bd606b 1611 xfs_btree_cur_t *cur; /* if null, not a btree */
1da177e4
LT
1612 xfs_ifork_t *ifp; /* inode fork pointer */
1613 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1614 xfs_bmbt_irec_t right; /* right neighbor extent entry */
3e57ecf6 1615 int rval=0; /* return value (logging flags) */
1da177e4 1616 int state; /* state bits, accessed thru macros */
1da177e4 1617
a5bd606b
CH
1618 *logflagsp = 0;
1619
1da177e4 1620 ifp = XFS_IFORK_PTR(ip, whichfork);
a5bd606b
CH
1621 cur = *curp;
1622
1623 ASSERT(*idx >= 0);
1624 ASSERT(*idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
1625 ASSERT(!isnullstartblock(new->br_startblock));
1626 ASSERT(!cur || !(cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
1627
1628 XFS_STATS_INC(xs_add_exlist);
7574aa92 1629
a5bd606b 1630 state = 0;
6ef35544
CH
1631 if (whichfork == XFS_ATTR_FORK)
1632 state |= BMAP_ATTRFORK;
1633
1da177e4
LT
1634 /*
1635 * Check and set flags if this segment has a left neighbor.
1636 */
ec90c556 1637 if (*idx > 0) {
7574aa92 1638 state |= BMAP_LEFT_VALID;
ec90c556 1639 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left);
7574aa92
CH
1640 if (isnullstartblock(left.br_startblock))
1641 state |= BMAP_LEFT_DELAY;
1da177e4 1642 }
7574aa92 1643
1da177e4
LT
1644 /*
1645 * Check and set flags if this segment has a current value.
1646 * Not true if we're inserting into the "hole" at eof.
1647 */
ec90c556 1648 if (*idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
7574aa92 1649 state |= BMAP_RIGHT_VALID;
2f2b3220 1650 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right);
7574aa92
CH
1651 if (isnullstartblock(right.br_startblock))
1652 state |= BMAP_RIGHT_DELAY;
1da177e4 1653 }
7574aa92 1654
1da177e4
LT
1655 /*
1656 * We're inserting a real allocation between "left" and "right".
1657 * Set the contiguity flags. Don't let extents get too large.
1658 */
7574aa92
CH
1659 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1660 left.br_startoff + left.br_blockcount == new->br_startoff &&
1661 left.br_startblock + left.br_blockcount == new->br_startblock &&
1662 left.br_state == new->br_state &&
1663 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1664 state |= BMAP_LEFT_CONTIG;
1665
1666 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1667 new->br_startoff + new->br_blockcount == right.br_startoff &&
1668 new->br_startblock + new->br_blockcount == right.br_startblock &&
1669 new->br_state == right.br_state &&
1670 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1671 (!(state & BMAP_LEFT_CONTIG) ||
1672 left.br_blockcount + new->br_blockcount +
1673 right.br_blockcount <= MAXEXTLEN))
1674 state |= BMAP_RIGHT_CONTIG;
1da177e4 1675
3e57ecf6 1676 error = 0;
1da177e4
LT
1677 /*
1678 * Select which case we're in here, and implement it.
1679 */
7574aa92
CH
1680 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
1681 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1da177e4
LT
1682 /*
1683 * New allocation is contiguous with real allocations on the
1684 * left and on the right.
4eea22f0 1685 * Merge all three into a single extent record.
1da177e4 1686 */
ec90c556
CH
1687 --*idx;
1688 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1689 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
1da177e4
LT
1690 left.br_blockcount + new->br_blockcount +
1691 right.br_blockcount);
ec90c556
CH
1692 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1693
1694 xfs_iext_remove(ip, *idx + 1, 1, state);
0b1b213f 1695
1da177e4
LT
1696 XFS_IFORK_NEXT_SET(ip, whichfork,
1697 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
1698 if (cur == NULL) {
9d87c319 1699 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
3e57ecf6
OW
1700 } else {
1701 rval = XFS_ILOG_CORE;
1702 if ((error = xfs_bmbt_lookup_eq(cur,
1703 right.br_startoff,
1704 right.br_startblock,
1705 right.br_blockcount, &i)))
1706 goto done;
6bd8fc8a 1707 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
91cca5df 1708 if ((error = xfs_btree_delete(cur, &i)))
3e57ecf6 1709 goto done;
6bd8fc8a 1710 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
8df4da4a 1711 if ((error = xfs_btree_decrement(cur, 0, &i)))
3e57ecf6 1712 goto done;
6bd8fc8a 1713 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
3e57ecf6
OW
1714 if ((error = xfs_bmbt_update(cur, left.br_startoff,
1715 left.br_startblock,
1716 left.br_blockcount +
1717 new->br_blockcount +
1718 right.br_blockcount,
1719 left.br_state)))
1720 goto done;
1da177e4 1721 }
3e57ecf6 1722 break;
1da177e4 1723
7574aa92 1724 case BMAP_LEFT_CONTIG:
1da177e4
LT
1725 /*
1726 * New allocation is contiguous with a real allocation
1727 * on the left.
1728 * Merge the new allocation with the left neighbor.
1729 */
ec90c556
CH
1730 --*idx;
1731 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1732 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
1da177e4 1733 left.br_blockcount + new->br_blockcount);
ec90c556 1734 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
0b1b213f 1735
1da177e4 1736 if (cur == NULL) {
9d87c319 1737 rval = xfs_ilog_fext(whichfork);
3e57ecf6
OW
1738 } else {
1739 rval = 0;
1740 if ((error = xfs_bmbt_lookup_eq(cur,
1741 left.br_startoff,
1742 left.br_startblock,
1743 left.br_blockcount, &i)))
1744 goto done;
6bd8fc8a 1745 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
3e57ecf6
OW
1746 if ((error = xfs_bmbt_update(cur, left.br_startoff,
1747 left.br_startblock,
1748 left.br_blockcount +
1749 new->br_blockcount,
1750 left.br_state)))
1751 goto done;
1da177e4 1752 }
3e57ecf6 1753 break;
1da177e4 1754
7574aa92 1755 case BMAP_RIGHT_CONTIG:
1da177e4
LT
1756 /*
1757 * New allocation is contiguous with a real allocation
1758 * on the right.
1759 * Merge the new allocation with the right neighbor.
1760 */
ec90c556 1761 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2f2b3220
CH
1762 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
1763 new->br_startoff, new->br_startblock,
1da177e4
LT
1764 new->br_blockcount + right.br_blockcount,
1765 right.br_state);
ec90c556 1766 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
0b1b213f 1767
1da177e4 1768 if (cur == NULL) {
9d87c319 1769 rval = xfs_ilog_fext(whichfork);
3e57ecf6
OW
1770 } else {
1771 rval = 0;
1772 if ((error = xfs_bmbt_lookup_eq(cur,
1773 right.br_startoff,
1774 right.br_startblock,
1775 right.br_blockcount, &i)))
1776 goto done;
6bd8fc8a 1777 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
3e57ecf6
OW
1778 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1779 new->br_startblock,
1780 new->br_blockcount +
1781 right.br_blockcount,
1782 right.br_state)))
1783 goto done;
1da177e4 1784 }
3e57ecf6 1785 break;
1da177e4
LT
1786
1787 case 0:
1788 /*
1789 * New allocation is not contiguous with another
1790 * real allocation.
1791 * Insert a new entry.
1792 */
ec90c556 1793 xfs_iext_insert(ip, *idx, 1, new, state);
1da177e4
LT
1794 XFS_IFORK_NEXT_SET(ip, whichfork,
1795 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
1796 if (cur == NULL) {
9d87c319 1797 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
3e57ecf6
OW
1798 } else {
1799 rval = XFS_ILOG_CORE;
1800 if ((error = xfs_bmbt_lookup_eq(cur,
1801 new->br_startoff,
1802 new->br_startblock,
1803 new->br_blockcount, &i)))
1804 goto done;
6bd8fc8a 1805 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
3e57ecf6 1806 cur->bc_rec.b.br_state = new->br_state;
4b22a571 1807 if ((error = xfs_btree_insert(cur, &i)))
3e57ecf6 1808 goto done;
6bd8fc8a 1809 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1da177e4 1810 }
3e57ecf6 1811 break;
1da177e4 1812 }
a5bd606b
CH
1813
1814 /* convert to a btree if necessary */
1815 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
1816 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
1817 int tmp_logflags; /* partial log flag return val */
1818
1819 ASSERT(cur == NULL);
1820 error = xfs_bmap_extents_to_btree(tp, ip, first,
1821 flist, &cur, 0, &tmp_logflags, whichfork);
1822 *logflagsp |= tmp_logflags;
1823 if (error)
1824 goto done;
1825 }
1826
1827 /* clear out the allocated field, done with it now in any case. */
1828 if (cur) {
1829 cur->bc_private.b.allocated = 0;
1830 *curp = cur;
1831 }
1832 xfs_bmap_check_leaf_extents(cur, ip, whichfork);
3e57ecf6 1833done:
a5bd606b 1834 *logflagsp |= rval;
3e57ecf6 1835 return error;
1da177e4
LT
1836}
1837
dd9f438e
NS
1838/*
1839 * Adjust the size of the new extent based on di_extsize and rt extsize.
1840 */
1841STATIC int
1842xfs_bmap_extsize_align(
1843 xfs_mount_t *mp,
1844 xfs_bmbt_irec_t *gotp, /* next extent pointer */
1845 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
1846 xfs_extlen_t extsz, /* align to this extent size */
1847 int rt, /* is this a realtime inode? */
1848 int eof, /* is extent at end-of-file? */
1849 int delay, /* creating delalloc extent? */
1850 int convert, /* overwriting unwritten extent? */
1851 xfs_fileoff_t *offp, /* in/out: aligned offset */
1852 xfs_extlen_t *lenp) /* in/out: aligned length */
1853{
1854 xfs_fileoff_t orig_off; /* original offset */
1855 xfs_extlen_t orig_alen; /* original length */
1856 xfs_fileoff_t orig_end; /* original off+len */
1857 xfs_fileoff_t nexto; /* next file offset */
1858 xfs_fileoff_t prevo; /* previous file offset */
1859 xfs_fileoff_t align_off; /* temp for offset */
1860 xfs_extlen_t align_alen; /* temp for length */
1861 xfs_extlen_t temp; /* temp for calculations */
1862
1863 if (convert)
1864 return 0;
1865
1866 orig_off = align_off = *offp;
1867 orig_alen = align_alen = *lenp;
1868 orig_end = orig_off + orig_alen;
1869
1870 /*
1871 * If this request overlaps an existing extent, then don't
1872 * attempt to perform any additional alignment.
1873 */
1874 if (!delay && !eof &&
1875 (orig_off >= gotp->br_startoff) &&
1876 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
1877 return 0;
1878 }
1879
1880 /*
1881 * If the file offset is unaligned vs. the extent size
1882 * we need to align it. This will be possible unless
1883 * the file was previously written with a kernel that didn't
1884 * perform this alignment, or if a truncate shot us in the
1885 * foot.
1886 */
1887 temp = do_mod(orig_off, extsz);
1888 if (temp) {
1889 align_alen += temp;
1890 align_off -= temp;
1891 }
1892 /*
1893 * Same adjustment for the end of the requested area.
1894 */
1895 if ((temp = (align_alen % extsz))) {
1896 align_alen += extsz - temp;
1897 }
1898 /*
1899 * If the previous block overlaps with this proposed allocation
1900 * then move the start forward without adjusting the length.
1901 */
1902 if (prevp->br_startoff != NULLFILEOFF) {
1903 if (prevp->br_startblock == HOLESTARTBLOCK)
1904 prevo = prevp->br_startoff;
1905 else
1906 prevo = prevp->br_startoff + prevp->br_blockcount;
1907 } else
1908 prevo = 0;
1909 if (align_off != orig_off && align_off < prevo)
1910 align_off = prevo;
1911 /*
1912 * If the next block overlaps with this proposed allocation
1913 * then move the start back without adjusting the length,
1914 * but not before offset 0.
1915 * This may of course make the start overlap previous block,
1916 * and if we hit the offset 0 limit then the next block
1917 * can still overlap too.
1918 */
1919 if (!eof && gotp->br_startoff != NULLFILEOFF) {
1920 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
1921 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
1922 nexto = gotp->br_startoff + gotp->br_blockcount;
1923 else
1924 nexto = gotp->br_startoff;
1925 } else
1926 nexto = NULLFILEOFF;
1927 if (!eof &&
1928 align_off + align_alen != orig_end &&
1929 align_off + align_alen > nexto)
1930 align_off = nexto > align_alen ? nexto - align_alen : 0;
1931 /*
1932 * If we're now overlapping the next or previous extent that
1933 * means we can't fit an extsz piece in this hole. Just move
1934 * the start forward to the first valid spot and set
1935 * the length so we hit the end.
1936 */
1937 if (align_off != orig_off && align_off < prevo)
1938 align_off = prevo;
1939 if (align_off + align_alen != orig_end &&
1940 align_off + align_alen > nexto &&
1941 nexto != NULLFILEOFF) {
1942 ASSERT(nexto > prevo);
1943 align_alen = nexto - align_off;
1944 }
1945
1946 /*
1947 * If realtime, and the result isn't a multiple of the realtime
1948 * extent size we need to remove blocks until it is.
1949 */
1950 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
1951 /*
1952 * We're not covering the original request, or
1953 * we won't be able to once we fix the length.
1954 */
1955 if (orig_off < align_off ||
1956 orig_end > align_off + align_alen ||
1957 align_alen - temp < orig_alen)
1958 return XFS_ERROR(EINVAL);
1959 /*
1960 * Try to fix it by moving the start up.
1961 */
1962 if (align_off + temp <= orig_off) {
1963 align_alen -= temp;
1964 align_off += temp;
1965 }
1966 /*
1967 * Try to fix it by moving the end in.
1968 */
1969 else if (align_off + align_alen - temp >= orig_end)
1970 align_alen -= temp;
1971 /*
1972 * Set the start to the minimum then trim the length.
1973 */
1974 else {
1975 align_alen -= orig_off - align_off;
1976 align_off = orig_off;
1977 align_alen -= align_alen % mp->m_sb.sb_rextsize;
1978 }
1979 /*
1980 * Result doesn't cover the request, fail it.
1981 */
1982 if (orig_off < align_off || orig_end > align_off + align_alen)
1983 return XFS_ERROR(EINVAL);
1984 } else {
1985 ASSERT(orig_off >= align_off);
1986 ASSERT(orig_end <= align_off + align_alen);
1987 }
1988
1989#ifdef DEBUG
1990 if (!eof && gotp->br_startoff != NULLFILEOFF)
1991 ASSERT(align_off + align_alen <= gotp->br_startoff);
1992 if (prevp->br_startoff != NULLFILEOFF)
1993 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
1994#endif
1995
1996 *lenp = align_alen;
1997 *offp = align_off;
1998 return 0;
1999}
2000
1da177e4
LT
2001#define XFS_ALLOC_GAP_UNITS 4
2002
c2b1cba6 2003STATIC void
a365bdd5 2004xfs_bmap_adjacent(
1da177e4
LT
2005 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2006{
2007 xfs_fsblock_t adjust; /* adjustment to block numbers */
1da177e4
LT
2008 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2009 xfs_mount_t *mp; /* mount point structure */
2010 int nullfb; /* true if ap->firstblock isn't set */
2011 int rt; /* true if inode is realtime */
1da177e4
LT
2012
2013#define ISVALID(x,y) \
2014 (rt ? \
2015 (x) < mp->m_sb.sb_rblocks : \
2016 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
2017 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
2018 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
2019
1da177e4 2020 mp = ap->ip->i_mount;
0937e0fd 2021 nullfb = *ap->firstblock == NULLFSBLOCK;
1da177e4 2022 rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata;
0937e0fd 2023 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
1da177e4
LT
2024 /*
2025 * If allocating at eof, and there's a previous real block,
9da096fd 2026 * try to use its last block as our starting point.
1da177e4 2027 */
baf41a52
DC
2028 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
2029 !isnullstartblock(ap->prev.br_startblock) &&
2030 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
2031 ap->prev.br_startblock)) {
3a75667e 2032 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
1da177e4
LT
2033 /*
2034 * Adjust for the gap between prevp and us.
2035 */
3a75667e 2036 adjust = ap->offset -
baf41a52 2037 (ap->prev.br_startoff + ap->prev.br_blockcount);
1da177e4 2038 if (adjust &&
3a75667e
DC
2039 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
2040 ap->blkno += adjust;
1da177e4
LT
2041 }
2042 /*
2043 * If not at eof, then compare the two neighbor blocks.
2044 * Figure out whether either one gives us a good starting point,
2045 * and pick the better one.
2046 */
2047 else if (!ap->eof) {
2048 xfs_fsblock_t gotbno; /* right side block number */
2049 xfs_fsblock_t gotdiff=0; /* right side difference */
2050 xfs_fsblock_t prevbno; /* left side block number */
2051 xfs_fsblock_t prevdiff=0; /* left side difference */
2052
2053 /*
2054 * If there's a previous (left) block, select a requested
2055 * start block based on it.
2056 */
baf41a52
DC
2057 if (ap->prev.br_startoff != NULLFILEOFF &&
2058 !isnullstartblock(ap->prev.br_startblock) &&
2059 (prevbno = ap->prev.br_startblock +
2060 ap->prev.br_blockcount) &&
2061 ISVALID(prevbno, ap->prev.br_startblock)) {
1da177e4
LT
2062 /*
2063 * Calculate gap to end of previous block.
2064 */
3a75667e 2065 adjust = prevdiff = ap->offset -
baf41a52
DC
2066 (ap->prev.br_startoff +
2067 ap->prev.br_blockcount);
1da177e4
LT
2068 /*
2069 * Figure the startblock based on the previous block's
2070 * end and the gap size.
2071 * Heuristic!
2072 * If the gap is large relative to the piece we're
2073 * allocating, or using it gives us an invalid block
2074 * number, then just use the end of the previous block.
2075 */
3a75667e 2076 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
1da177e4 2077 ISVALID(prevbno + prevdiff,
baf41a52 2078 ap->prev.br_startblock))
1da177e4
LT
2079 prevbno += adjust;
2080 else
2081 prevdiff += adjust;
2082 /*
2083 * If the firstblock forbids it, can't use it,
2084 * must use default.
2085 */
2086 if (!rt && !nullfb &&
2087 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
2088 prevbno = NULLFSBLOCK;
2089 }
2090 /*
2091 * No previous block or can't follow it, just default.
2092 */
2093 else
2094 prevbno = NULLFSBLOCK;
2095 /*
2096 * If there's a following (right) block, select a requested
2097 * start block based on it.
2098 */
baf41a52 2099 if (!isnullstartblock(ap->got.br_startblock)) {
1da177e4
LT
2100 /*
2101 * Calculate gap to start of next block.
2102 */
3a75667e 2103 adjust = gotdiff = ap->got.br_startoff - ap->offset;
1da177e4
LT
2104 /*
2105 * Figure the startblock based on the next block's
2106 * start and the gap size.
2107 */
baf41a52 2108 gotbno = ap->got.br_startblock;
1da177e4
LT
2109 /*
2110 * Heuristic!
2111 * If the gap is large relative to the piece we're
2112 * allocating, or using it gives us an invalid block
2113 * number, then just use the start of the next block
2114 * offset by our length.
2115 */
3a75667e 2116 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
1da177e4
LT
2117 ISVALID(gotbno - gotdiff, gotbno))
2118 gotbno -= adjust;
3a75667e
DC
2119 else if (ISVALID(gotbno - ap->length, gotbno)) {
2120 gotbno -= ap->length;
2121 gotdiff += adjust - ap->length;
1da177e4
LT
2122 } else
2123 gotdiff += adjust;
2124 /*
2125 * If the firstblock forbids it, can't use it,
2126 * must use default.
2127 */
2128 if (!rt && !nullfb &&
2129 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
2130 gotbno = NULLFSBLOCK;
2131 }
2132 /*
2133 * No next block, just default.
2134 */
2135 else
2136 gotbno = NULLFSBLOCK;
2137 /*
2138 * If both valid, pick the better one, else the only good
3a75667e 2139 * one, else ap->blkno is already set (to 0 or the inode block).
1da177e4
LT
2140 */
2141 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
3a75667e 2142 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
1da177e4 2143 else if (prevbno != NULLFSBLOCK)
3a75667e 2144 ap->blkno = prevbno;
1da177e4 2145 else if (gotbno != NULLFSBLOCK)
3a75667e 2146 ap->blkno = gotbno;
1da177e4 2147 }
a365bdd5 2148#undef ISVALID
a365bdd5
NS
2149}
2150
2151STATIC int
2152xfs_bmap_rtalloc(
2153 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2154{
2155 xfs_alloctype_t atype = 0; /* type for allocation routines */
2156 int error; /* error return value */
2157 xfs_mount_t *mp; /* mount point structure */
2158 xfs_extlen_t prod = 0; /* product factor for allocators */
2159 xfs_extlen_t ralen = 0; /* realtime allocation length */
2160 xfs_extlen_t align; /* minimum allocation alignment */
a365bdd5
NS
2161 xfs_rtblock_t rtb;
2162
2163 mp = ap->ip->i_mount;
957d0ebe 2164 align = xfs_get_extsz_hint(ap->ip);
a365bdd5 2165 prod = align / mp->m_sb.sb_rextsize;
baf41a52 2166 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
a365bdd5 2167 align, 1, ap->eof, 0,
3a75667e 2168 ap->conv, &ap->offset, &ap->length);
a365bdd5
NS
2169 if (error)
2170 return error;
3a75667e
DC
2171 ASSERT(ap->length);
2172 ASSERT(ap->length % mp->m_sb.sb_rextsize == 0);
a365bdd5
NS
2173
2174 /*
2175 * If the offset & length are not perfectly aligned
2176 * then kill prod, it will just get us in trouble.
2177 */
3a75667e 2178 if (do_mod(ap->offset, align) || ap->length % align)
a365bdd5
NS
2179 prod = 1;
2180 /*
2181 * Set ralen to be the actual requested length in rtextents.
2182 */
3a75667e 2183 ralen = ap->length / mp->m_sb.sb_rextsize;
a365bdd5
NS
2184 /*
2185 * If the old value was close enough to MAXEXTLEN that
2186 * we rounded up to it, cut it back so it's valid again.
2187 * Note that if it's a really large request (bigger than
2188 * MAXEXTLEN), we don't hear about that number, and can't
2189 * adjust the starting point to match it.
2190 */
2191 if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN)
2192 ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
04e99455
CH
2193
2194 /*
2195 * Lock out other modifications to the RT bitmap inode.
2196 */
1050c71e
CH
2197 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL);
2198 xfs_trans_ijoin_ref(ap->tp, mp->m_rbmip, XFS_ILOCK_EXCL);
04e99455 2199
a365bdd5
NS
2200 /*
2201 * If it's an allocation to an empty file at offset 0,
2202 * pick an extent that will space things out in the rt area.
2203 */
3a75667e 2204 if (ap->eof && ap->offset == 0) {
0892ccd6
AM
2205 xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */
2206
a365bdd5
NS
2207 error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
2208 if (error)
2209 return error;
3a75667e 2210 ap->blkno = rtx * mp->m_sb.sb_rextsize;
a365bdd5 2211 } else {
3a75667e 2212 ap->blkno = 0;
a365bdd5
NS
2213 }
2214
2215 xfs_bmap_adjacent(ap);
2216
2217 /*
2218 * Realtime allocation, done through xfs_rtallocate_extent.
2219 */
3a75667e
DC
2220 atype = ap->blkno == 0 ? XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO;
2221 do_div(ap->blkno, mp->m_sb.sb_rextsize);
2222 rtb = ap->blkno;
2223 ap->length = ralen;
2224 if ((error = xfs_rtallocate_extent(ap->tp, ap->blkno, 1, ap->length,
a365bdd5
NS
2225 &ralen, atype, ap->wasdel, prod, &rtb)))
2226 return error;
2227 if (rtb == NULLFSBLOCK && prod > 1 &&
3a75667e
DC
2228 (error = xfs_rtallocate_extent(ap->tp, ap->blkno, 1,
2229 ap->length, &ralen, atype,
a365bdd5
NS
2230 ap->wasdel, 1, &rtb)))
2231 return error;
3a75667e
DC
2232 ap->blkno = rtb;
2233 if (ap->blkno != NULLFSBLOCK) {
2234 ap->blkno *= mp->m_sb.sb_rextsize;
a365bdd5 2235 ralen *= mp->m_sb.sb_rextsize;
3a75667e 2236 ap->length = ralen;
a365bdd5
NS
2237 ap->ip->i_d.di_nblocks += ralen;
2238 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2239 if (ap->wasdel)
2240 ap->ip->i_delayed_blks -= ralen;
2241 /*
2242 * Adjust the disk quota also. This was reserved
2243 * earlier.
2244 */
7d095257 2245 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
a365bdd5
NS
2246 ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
2247 XFS_TRANS_DQ_RTBCOUNT, (long) ralen);
2248 } else {
3a75667e 2249 ap->length = 0;
a365bdd5
NS
2250 }
2251 return 0;
2252}
2253
c467c049
CH
2254STATIC int
2255xfs_bmap_btalloc_nullfb(
2256 struct xfs_bmalloca *ap,
2257 struct xfs_alloc_arg *args,
2258 xfs_extlen_t *blen)
2259{
2260 struct xfs_mount *mp = ap->ip->i_mount;
2261 struct xfs_perag *pag;
2262 xfs_agnumber_t ag, startag;
2263 int notinit = 0;
2264 int error;
2265
2266 if (ap->userdata && xfs_inode_is_filestream(ap->ip))
2267 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2268 else
2269 args->type = XFS_ALLOCTYPE_START_BNO;
2270 args->total = ap->total;
2271
2272 /*
2273 * Search for an allocation group with a single extent large enough
2274 * for the request. If one isn't found, then adjust the minimum
2275 * allocation size to the largest space found.
2276 */
2277 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
2278 if (startag == NULLAGNUMBER)
2279 startag = ag = 0;
2280
2281 pag = xfs_perag_get(mp, ag);
14b064ce 2282 while (*blen < args->maxlen) {
c467c049
CH
2283 if (!pag->pagf_init) {
2284 error = xfs_alloc_pagf_init(mp, args->tp, ag,
2285 XFS_ALLOC_FLAG_TRYLOCK);
2286 if (error) {
2287 xfs_perag_put(pag);
2288 return error;
2289 }
2290 }
2291
2292 /*
2293 * See xfs_alloc_fix_freelist...
2294 */
2295 if (pag->pagf_init) {
2296 xfs_extlen_t longest;
2297 longest = xfs_alloc_longest_free_extent(mp, pag);
2298 if (*blen < longest)
2299 *blen = longest;
2300 } else
2301 notinit = 1;
2302
2303 if (xfs_inode_is_filestream(ap->ip)) {
14b064ce 2304 if (*blen >= args->maxlen)
c467c049
CH
2305 break;
2306
2307 if (ap->userdata) {
2308 /*
2309 * If startag is an invalid AG, we've
2310 * come here once before and
2311 * xfs_filestream_new_ag picked the
2312 * best currently available.
2313 *
2314 * Don't continue looping, since we
2315 * could loop forever.
2316 */
2317 if (startag == NULLAGNUMBER)
2318 break;
2319
2320 error = xfs_filestream_new_ag(ap, &ag);
2321 xfs_perag_put(pag);
2322 if (error)
2323 return error;
2324
2325 /* loop again to set 'blen'*/
2326 startag = NULLAGNUMBER;
2327 pag = xfs_perag_get(mp, ag);
2328 continue;
2329 }
2330 }
2331 if (++ag == mp->m_sb.sb_agcount)
2332 ag = 0;
2333 if (ag == startag)
2334 break;
2335 xfs_perag_put(pag);
2336 pag = xfs_perag_get(mp, ag);
2337 }
2338 xfs_perag_put(pag);
2339
2340 /*
2341 * Since the above loop did a BUF_TRYLOCK, it is
2342 * possible that there is space for this request.
2343 */
2344 if (notinit || *blen < ap->minlen)
2345 args->minlen = ap->minlen;
2346 /*
2347 * If the best seen length is less than the request
2348 * length, use the best as the minimum.
2349 */
14b064ce 2350 else if (*blen < args->maxlen)
c467c049
CH
2351 args->minlen = *blen;
2352 /*
14b064ce 2353 * Otherwise we've seen an extent as big as maxlen,
c467c049
CH
2354 * use that as the minimum.
2355 */
2356 else
14b064ce 2357 args->minlen = args->maxlen;
c467c049
CH
2358
2359 /*
2360 * set the failure fallback case to look in the selected
2361 * AG as the stream may have moved.
2362 */
2363 if (xfs_inode_is_filestream(ap->ip))
3a75667e 2364 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
c467c049
CH
2365
2366 return 0;
2367}
2368
a365bdd5
NS
2369STATIC int
2370xfs_bmap_btalloc(
2371 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2372{
2373 xfs_mount_t *mp; /* mount point structure */
2374 xfs_alloctype_t atype = 0; /* type for allocation routines */
2375 xfs_extlen_t align; /* minimum allocation alignment */
a365bdd5 2376 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
c467c049 2377 xfs_agnumber_t ag;
a365bdd5
NS
2378 xfs_alloc_arg_t args;
2379 xfs_extlen_t blen;
a365bdd5 2380 xfs_extlen_t nextminlen = 0;
a365bdd5
NS
2381 int nullfb; /* true if ap->firstblock isn't set */
2382 int isaligned;
a365bdd5
NS
2383 int tryagain;
2384 int error;
2385
2386 mp = ap->ip->i_mount;
957d0ebe 2387 align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0;
a365bdd5 2388 if (unlikely(align)) {
baf41a52 2389 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
a365bdd5 2390 align, 0, ap->eof, 0, ap->conv,
3a75667e 2391 &ap->offset, &ap->length);
a365bdd5 2392 ASSERT(!error);
3a75667e 2393 ASSERT(ap->length);
a365bdd5 2394 }
0937e0fd
DC
2395 nullfb = *ap->firstblock == NULLFSBLOCK;
2396 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
2a82b8be
DC
2397 if (nullfb) {
2398 if (ap->userdata && xfs_inode_is_filestream(ap->ip)) {
2399 ag = xfs_filestream_lookup_ag(ap->ip);
2400 ag = (ag != NULLAGNUMBER) ? ag : 0;
3a75667e 2401 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
2a82b8be 2402 } else {
3a75667e 2403 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
2a82b8be
DC
2404 }
2405 } else
3a75667e 2406 ap->blkno = *ap->firstblock;
a365bdd5
NS
2407
2408 xfs_bmap_adjacent(ap);
2409
1da177e4 2410 /*
3a75667e 2411 * If allowed, use ap->blkno; otherwise must use firstblock since
1da177e4
LT
2412 * it's in the right allocation group.
2413 */
3a75667e 2414 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
1da177e4
LT
2415 ;
2416 else
3a75667e 2417 ap->blkno = *ap->firstblock;
1da177e4 2418 /*
a365bdd5 2419 * Normal allocation, done through xfs_alloc_vextent.
1da177e4 2420 */
a365bdd5
NS
2421 tryagain = isaligned = 0;
2422 args.tp = ap->tp;
2423 args.mp = mp;
3a75667e 2424 args.fsbno = ap->blkno;
14b064ce
DC
2425
2426 /* Trim the allocation back to the maximum an AG can fit. */
3a75667e 2427 args.maxlen = MIN(ap->length, XFS_ALLOC_AG_MAX_USABLE(mp));
0937e0fd 2428 args.firstblock = *ap->firstblock;
a365bdd5
NS
2429 blen = 0;
2430 if (nullfb) {
c467c049
CH
2431 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
2432 if (error)
2433 return error;
0937e0fd 2434 } else if (ap->flist->xbf_low) {
2a82b8be
DC
2435 if (xfs_inode_is_filestream(ap->ip))
2436 args.type = XFS_ALLOCTYPE_FIRST_AG;
2437 else
2438 args.type = XFS_ALLOCTYPE_START_BNO;
a365bdd5
NS
2439 args.total = args.minlen = ap->minlen;
2440 } else {
2441 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2442 args.total = ap->total;
2443 args.minlen = ap->minlen;
2444 }
957d0ebe
DC
2445 /* apply extent size hints if obtained earlier */
2446 if (unlikely(align)) {
2447 args.prod = align;
3a75667e 2448 if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod)))
a365bdd5 2449 args.mod = (xfs_extlen_t)(args.prod - args.mod);
e6a4b37f 2450 } else if (mp->m_sb.sb_blocksize >= PAGE_CACHE_SIZE) {
a365bdd5
NS
2451 args.prod = 1;
2452 args.mod = 0;
2453 } else {
e6a4b37f 2454 args.prod = PAGE_CACHE_SIZE >> mp->m_sb.sb_blocklog;
3a75667e 2455 if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod))))
a365bdd5 2456 args.mod = (xfs_extlen_t)(args.prod - args.mod);
1da177e4
LT
2457 }
2458 /*
a365bdd5
NS
2459 * If we are not low on available data blocks, and the
2460 * underlying logical volume manager is a stripe, and
2461 * the file offset is zero then try to allocate data
2462 * blocks on stripe unit boundary.
2463 * NOTE: ap->aeof is only set if the allocation length
2464 * is >= the stripe unit and the allocation offset is
2465 * at the end of file.
1da177e4 2466 */
0937e0fd 2467 if (!ap->flist->xbf_low && ap->aeof) {
3a75667e 2468 if (!ap->offset) {
a365bdd5
NS
2469 args.alignment = mp->m_dalign;
2470 atype = args.type;
2471 isaligned = 1;
1da177e4 2472 /*
a365bdd5 2473 * Adjust for alignment
1da177e4 2474 */
14b064ce 2475 if (blen > args.alignment && blen <= args.maxlen)
a365bdd5
NS
2476 args.minlen = blen - args.alignment;
2477 args.minalignslop = 0;
2478 } else {
1da177e4 2479 /*
a365bdd5
NS
2480 * First try an exact bno allocation.
2481 * If it fails then do a near or start bno
2482 * allocation with alignment turned on.
1da177e4 2483 */
a365bdd5
NS
2484 atype = args.type;
2485 tryagain = 1;
2486 args.type = XFS_ALLOCTYPE_THIS_BNO;
2487 args.alignment = 1;
1da177e4 2488 /*
a365bdd5
NS
2489 * Compute the minlen+alignment for the
2490 * next case. Set slop so that the value
2491 * of minlen+alignment+slop doesn't go up
2492 * between the calls.
1da177e4 2493 */
14b064ce 2494 if (blen > mp->m_dalign && blen <= args.maxlen)
a365bdd5 2495 nextminlen = blen - mp->m_dalign;
1da177e4 2496 else
a365bdd5
NS
2497 nextminlen = args.minlen;
2498 if (nextminlen + mp->m_dalign > args.minlen + 1)
2499 args.minalignslop =
2500 nextminlen + mp->m_dalign -
2501 args.minlen - 1;
2502 else
2503 args.minalignslop = 0;
1da177e4 2504 }
a365bdd5
NS
2505 } else {
2506 args.alignment = 1;
2507 args.minalignslop = 0;
2508 }
2509 args.minleft = ap->minleft;
2510 args.wasdel = ap->wasdel;
2511 args.isfl = 0;
2512 args.userdata = ap->userdata;
2513 if ((error = xfs_alloc_vextent(&args)))
2514 return error;
2515 if (tryagain && args.fsbno == NULLFSBLOCK) {
1da177e4 2516 /*
a365bdd5
NS
2517 * Exact allocation failed. Now try with alignment
2518 * turned on.
1da177e4 2519 */
a365bdd5 2520 args.type = atype;
3a75667e 2521 args.fsbno = ap->blkno;
a365bdd5
NS
2522 args.alignment = mp->m_dalign;
2523 args.minlen = nextminlen;
2524 args.minalignslop = 0;
2525 isaligned = 1;
1da177e4
LT
2526 if ((error = xfs_alloc_vextent(&args)))
2527 return error;
a365bdd5
NS
2528 }
2529 if (isaligned && args.fsbno == NULLFSBLOCK) {
2530 /*
2531 * allocation failed, so turn off alignment and
2532 * try again.
2533 */
2534 args.type = atype;
3a75667e 2535 args.fsbno = ap->blkno;
a365bdd5
NS
2536 args.alignment = 0;
2537 if ((error = xfs_alloc_vextent(&args)))
2538 return error;
2539 }
2540 if (args.fsbno == NULLFSBLOCK && nullfb &&
2541 args.minlen > ap->minlen) {
2542 args.minlen = ap->minlen;
2543 args.type = XFS_ALLOCTYPE_START_BNO;
3a75667e 2544 args.fsbno = ap->blkno;
a365bdd5
NS
2545 if ((error = xfs_alloc_vextent(&args)))
2546 return error;
2547 }
2548 if (args.fsbno == NULLFSBLOCK && nullfb) {
2549 args.fsbno = 0;
2550 args.type = XFS_ALLOCTYPE_FIRST_AG;
2551 args.total = ap->minlen;
2552 args.minleft = 0;
2553 if ((error = xfs_alloc_vextent(&args)))
2554 return error;
0937e0fd 2555 ap->flist->xbf_low = 1;
a365bdd5
NS
2556 }
2557 if (args.fsbno != NULLFSBLOCK) {
0937e0fd
DC
2558 /*
2559 * check the allocation happened at the same or higher AG than
2560 * the first block that was allocated.
2561 */
2562 ASSERT(*ap->firstblock == NULLFSBLOCK ||
2563 XFS_FSB_TO_AGNO(mp, *ap->firstblock) ==
2564 XFS_FSB_TO_AGNO(mp, args.fsbno) ||
2565 (ap->flist->xbf_low &&
2566 XFS_FSB_TO_AGNO(mp, *ap->firstblock) <
2567 XFS_FSB_TO_AGNO(mp, args.fsbno)));
2568
3a75667e 2569 ap->blkno = args.fsbno;
0937e0fd
DC
2570 if (*ap->firstblock == NULLFSBLOCK)
2571 *ap->firstblock = args.fsbno;
a365bdd5 2572 ASSERT(nullfb || fb_agno == args.agno ||
0937e0fd 2573 (ap->flist->xbf_low && fb_agno < args.agno));
3a75667e 2574 ap->length = args.len;
a365bdd5
NS
2575 ap->ip->i_d.di_nblocks += args.len;
2576 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2577 if (ap->wasdel)
2578 ap->ip->i_delayed_blks -= args.len;
2579 /*
2580 * Adjust the disk quota also. This was reserved
2581 * earlier.
2582 */
7d095257 2583 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
a365bdd5
NS
2584 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
2585 XFS_TRANS_DQ_BCOUNT,
2586 (long) args.len);
2587 } else {
3a75667e
DC
2588 ap->blkno = NULLFSBLOCK;
2589 ap->length = 0;
1da177e4
LT
2590 }
2591 return 0;
a365bdd5
NS
2592}
2593
2594/*
2595 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
2596 * It figures out where to ask the underlying allocator to put the new extent.
2597 */
2598STATIC int
2599xfs_bmap_alloc(
2600 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2601{
71ddabb9 2602 if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata)
a365bdd5
NS
2603 return xfs_bmap_rtalloc(ap);
2604 return xfs_bmap_btalloc(ap);
1da177e4
LT
2605}
2606
2607/*
2608 * Transform a btree format file with only one leaf node, where the
2609 * extents list will fit in the inode, into an extents format file.
4eea22f0 2610 * Since the file extents are already in-core, all we have to do is
1da177e4
LT
2611 * give up the space for the btree root and pitch the leaf block.
2612 */
2613STATIC int /* error */
2614xfs_bmap_btree_to_extents(
2615 xfs_trans_t *tp, /* transaction pointer */
2616 xfs_inode_t *ip, /* incore inode pointer */
2617 xfs_btree_cur_t *cur, /* btree cursor */
2618 int *logflagsp, /* inode logging flags */
2619 int whichfork) /* data or attr fork */
2620{
2621 /* REFERENCED */
7cc95a82 2622 struct xfs_btree_block *cblock;/* child btree block */
1da177e4
LT
2623 xfs_fsblock_t cbno; /* child block number */
2624 xfs_buf_t *cbp; /* child block's buffer */
2625 int error; /* error return value */
2626 xfs_ifork_t *ifp; /* inode fork data */
2627 xfs_mount_t *mp; /* mount point structure */
576039cf 2628 __be64 *pp; /* ptr to block address */
7cc95a82 2629 struct xfs_btree_block *rblock;/* root btree block */
1da177e4 2630
60197e8d 2631 mp = ip->i_mount;
1da177e4
LT
2632 ifp = XFS_IFORK_PTR(ip, whichfork);
2633 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
2634 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
2635 rblock = ifp->if_broot;
16259e7d
CH
2636 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
2637 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
60197e8d
CH
2638 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
2639 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
576039cf 2640 cbno = be64_to_cpu(*pp);
1da177e4
LT
2641 *logflagsp = 0;
2642#ifdef DEBUG
576039cf 2643 if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
1da177e4
LT
2644 return error;
2645#endif
1da177e4
LT
2646 if ((error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp,
2647 XFS_BMAP_BTREE_REF)))
2648 return error;
7cc95a82
CH
2649 cblock = XFS_BUF_TO_BLOCK(cbp);
2650 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
1da177e4
LT
2651 return error;
2652 xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp);
2653 ip->i_d.di_nblocks--;
7d095257 2654 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
1da177e4
LT
2655 xfs_trans_binval(tp, cbp);
2656 if (cur->bc_bufs[0] == cbp)
2657 cur->bc_bufs[0] = NULL;
2658 xfs_iroot_realloc(ip, -1, whichfork);
2659 ASSERT(ifp->if_broot == NULL);
2660 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
2661 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
9d87c319 2662 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
1da177e4
LT
2663 return 0;
2664}
2665
2666/*
4eea22f0 2667 * Called by xfs_bmapi to update file extent records and the btree
1da177e4
LT
2668 * after removing space (or undoing a delayed allocation).
2669 */
2670STATIC int /* error */
2671xfs_bmap_del_extent(
2672 xfs_inode_t *ip, /* incore inode pointer */
2673 xfs_trans_t *tp, /* current transaction pointer */
ec90c556 2674 xfs_extnum_t *idx, /* extent number to update/delete */
1da177e4
LT
2675 xfs_bmap_free_t *flist, /* list of extents to be freed */
2676 xfs_btree_cur_t *cur, /* if null, not a btree */
4eea22f0 2677 xfs_bmbt_irec_t *del, /* data to remove from extents */
1da177e4 2678 int *logflagsp, /* inode logging flags */
54893273 2679 int whichfork) /* data or attr fork */
1da177e4
LT
2680{
2681 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
2682 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
2683 xfs_fsblock_t del_endblock=0; /* first block past del */
2684 xfs_fileoff_t del_endoff; /* first offset past del */
2685 int delay; /* current block is delayed allocated */
2686 int do_fx; /* free extent at end of routine */
a6f64d4a 2687 xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
1da177e4
LT
2688 int error; /* error return value */
2689 int flags; /* inode logging flags */
1da177e4
LT
2690 xfs_bmbt_irec_t got; /* current extent entry */
2691 xfs_fileoff_t got_endoff; /* first offset past got */
2692 int i; /* temp state */
2693 xfs_ifork_t *ifp; /* inode fork pointer */
2694 xfs_mount_t *mp; /* mount structure */
2695 xfs_filblks_t nblks; /* quota/sb block count */
2696 xfs_bmbt_irec_t new; /* new record to be inserted */
2697 /* REFERENCED */
1da177e4
LT
2698 uint qfield; /* quota field to update */
2699 xfs_filblks_t temp; /* for indirect length calculations */
2700 xfs_filblks_t temp2; /* for indirect length calculations */
0b1b213f 2701 int state = 0;
1da177e4
LT
2702
2703 XFS_STATS_INC(xs_del_exlist);
0b1b213f
CH
2704
2705 if (whichfork == XFS_ATTR_FORK)
2706 state |= BMAP_ATTRFORK;
2707
1da177e4
LT
2708 mp = ip->i_mount;
2709 ifp = XFS_IFORK_PTR(ip, whichfork);
ec90c556 2710 ASSERT((*idx >= 0) && (*idx < ifp->if_bytes /
4eea22f0 2711 (uint)sizeof(xfs_bmbt_rec_t)));
1da177e4 2712 ASSERT(del->br_blockcount > 0);
ec90c556 2713 ep = xfs_iext_get_ext(ifp, *idx);
1da177e4
LT
2714 xfs_bmbt_get_all(ep, &got);
2715 ASSERT(got.br_startoff <= del->br_startoff);
2716 del_endoff = del->br_startoff + del->br_blockcount;
2717 got_endoff = got.br_startoff + got.br_blockcount;
2718 ASSERT(got_endoff >= del_endoff);
9d87c319
ES
2719 delay = isnullstartblock(got.br_startblock);
2720 ASSERT(isnullstartblock(del->br_startblock) == delay);
1da177e4
LT
2721 flags = 0;
2722 qfield = 0;
2723 error = 0;
2724 /*
2725 * If deleting a real allocation, must free up the disk space.
2726 */
2727 if (!delay) {
2728 flags = XFS_ILOG_CORE;
2729 /*
2730 * Realtime allocation. Free it and record di_nblocks update.
2731 */
71ddabb9 2732 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
1da177e4
LT
2733 xfs_fsblock_t bno;
2734 xfs_filblks_t len;
2735
2736 ASSERT(do_mod(del->br_blockcount,
2737 mp->m_sb.sb_rextsize) == 0);
2738 ASSERT(do_mod(del->br_startblock,
2739 mp->m_sb.sb_rextsize) == 0);
2740 bno = del->br_startblock;
2741 len = del->br_blockcount;
2742 do_div(bno, mp->m_sb.sb_rextsize);
2743 do_div(len, mp->m_sb.sb_rextsize);
f3ca8738
CH
2744 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
2745 if (error)
1da177e4
LT
2746 goto done;
2747 do_fx = 0;
2748 nblks = len * mp->m_sb.sb_rextsize;
2749 qfield = XFS_TRANS_DQ_RTBCOUNT;
2750 }
2751 /*
2752 * Ordinary allocation.
2753 */
2754 else {
2755 do_fx = 1;
2756 nblks = del->br_blockcount;
2757 qfield = XFS_TRANS_DQ_BCOUNT;
2758 }
2759 /*
2760 * Set up del_endblock and cur for later.
2761 */
2762 del_endblock = del->br_startblock + del->br_blockcount;
2763 if (cur) {
2764 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
2765 got.br_startblock, got.br_blockcount,
2766 &i)))
2767 goto done;
6bd8fc8a 2768 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1da177e4
LT
2769 }
2770 da_old = da_new = 0;
2771 } else {
9d87c319 2772 da_old = startblockval(got.br_startblock);
1da177e4
LT
2773 da_new = 0;
2774 nblks = 0;
2775 do_fx = 0;
2776 }
2777 /*
2778 * Set flag value to use in switch statement.
2779 * Left-contig is 2, right-contig is 1.
2780 */
2781 switch (((got.br_startoff == del->br_startoff) << 1) |
2782 (got_endoff == del_endoff)) {
2783 case 3:
2784 /*
2785 * Matches the whole extent. Delete the entry.
2786 */
ec90c556 2787 xfs_iext_remove(ip, *idx, 1,
6ef35544 2788 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
233eebb9 2789 --*idx;
1da177e4
LT
2790 if (delay)
2791 break;
233eebb9 2792
1da177e4
LT
2793 XFS_IFORK_NEXT_SET(ip, whichfork,
2794 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2795 flags |= XFS_ILOG_CORE;
2796 if (!cur) {
9d87c319 2797 flags |= xfs_ilog_fext(whichfork);
1da177e4
LT
2798 break;
2799 }
91cca5df 2800 if ((error = xfs_btree_delete(cur, &i)))
1da177e4 2801 goto done;
6bd8fc8a 2802 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1da177e4
LT
2803 break;
2804
2805 case 2:
2806 /*
2807 * Deleting the first part of the extent.
2808 */
ec90c556 2809 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1da177e4
LT
2810 xfs_bmbt_set_startoff(ep, del_endoff);
2811 temp = got.br_blockcount - del->br_blockcount;
2812 xfs_bmbt_set_blockcount(ep, temp);
1da177e4
LT
2813 if (delay) {
2814 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2815 da_old);
9d87c319 2816 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
ec90c556 2817 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1da177e4
LT
2818 da_new = temp;
2819 break;
2820 }
2821 xfs_bmbt_set_startblock(ep, del_endblock);
ec90c556 2822 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1da177e4 2823 if (!cur) {
9d87c319 2824 flags |= xfs_ilog_fext(whichfork);
1da177e4
LT
2825 break;
2826 }
2827 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
2828 got.br_blockcount - del->br_blockcount,
2829 got.br_state)))
2830 goto done;
2831 break;
2832
2833 case 1:
2834 /*
2835 * Deleting the last part of the extent.
2836 */
2837 temp = got.br_blockcount - del->br_blockcount;
ec90c556 2838 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1da177e4 2839 xfs_bmbt_set_blockcount(ep, temp);
1da177e4
LT
2840 if (delay) {
2841 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2842 da_old);
9d87c319 2843 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
ec90c556 2844 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1da177e4
LT
2845 da_new = temp;
2846 break;
2847 }
ec90c556 2848 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1da177e4 2849 if (!cur) {
9d87c319 2850 flags |= xfs_ilog_fext(whichfork);
1da177e4
LT
2851 break;
2852 }
2853 if ((error = xfs_bmbt_update(cur, got.br_startoff,
2854 got.br_startblock,
2855 got.br_blockcount - del->br_blockcount,
2856 got.br_state)))
2857 goto done;
2858 break;
2859
2860 case 0:
2861 /*
2862 * Deleting the middle of the extent.
2863 */
2864 temp = del->br_startoff - got.br_startoff;
ec90c556 2865 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1da177e4
LT
2866 xfs_bmbt_set_blockcount(ep, temp);
2867 new.br_startoff = del_endoff;
2868 temp2 = got_endoff - del_endoff;
2869 new.br_blockcount = temp2;
2870 new.br_state = got.br_state;
2871 if (!delay) {
2872 new.br_startblock = del_endblock;
2873 flags |= XFS_ILOG_CORE;
2874 if (cur) {
2875 if ((error = xfs_bmbt_update(cur,
2876 got.br_startoff,
2877 got.br_startblock, temp,
2878 got.br_state)))
2879 goto done;
637aa50f 2880 if ((error = xfs_btree_increment(cur, 0, &i)))
1da177e4
LT
2881 goto done;
2882 cur->bc_rec.b = new;
4b22a571 2883 error = xfs_btree_insert(cur, &i);
1da177e4
LT
2884 if (error && error != ENOSPC)
2885 goto done;
2886 /*
2887 * If get no-space back from btree insert,
2888 * it tried a split, and we have a zero
2889 * block reservation.
2890 * Fix up our state and return the error.
2891 */
2892 if (error == ENOSPC) {
2893 /*
2894 * Reset the cursor, don't trust
2895 * it after any insert operation.
2896 */
2897 if ((error = xfs_bmbt_lookup_eq(cur,
2898 got.br_startoff,
2899 got.br_startblock,
2900 temp, &i)))
2901 goto done;
6bd8fc8a 2902 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1da177e4
LT
2903 /*
2904 * Update the btree record back
2905 * to the original value.
2906 */
2907 if ((error = xfs_bmbt_update(cur,
2908 got.br_startoff,
2909 got.br_startblock,
2910 got.br_blockcount,
2911 got.br_state)))
2912 goto done;
2913 /*
2914 * Reset the extent record back
2915 * to the original value.
2916 */
2917 xfs_bmbt_set_blockcount(ep,
2918 got.br_blockcount);
2919 flags = 0;
2920 error = XFS_ERROR(ENOSPC);
2921 goto done;
2922 }
6bd8fc8a 2923 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1da177e4 2924 } else
9d87c319 2925 flags |= xfs_ilog_fext(whichfork);
1da177e4
LT
2926 XFS_IFORK_NEXT_SET(ip, whichfork,
2927 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2928 } else {
2929 ASSERT(whichfork == XFS_DATA_FORK);
2930 temp = xfs_bmap_worst_indlen(ip, temp);
9d87c319 2931 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
1da177e4 2932 temp2 = xfs_bmap_worst_indlen(ip, temp2);
9d87c319 2933 new.br_startblock = nullstartblock((int)temp2);
1da177e4
LT
2934 da_new = temp + temp2;
2935 while (da_new > da_old) {
2936 if (temp) {
2937 temp--;
2938 da_new--;
2939 xfs_bmbt_set_startblock(ep,
9d87c319 2940 nullstartblock((int)temp));
1da177e4
LT
2941 }
2942 if (da_new == da_old)
2943 break;
2944 if (temp2) {
2945 temp2--;
2946 da_new--;
2947 new.br_startblock =
9d87c319 2948 nullstartblock((int)temp2);
1da177e4
LT
2949 }
2950 }
2951 }
ec90c556
CH
2952 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2953 xfs_iext_insert(ip, *idx + 1, 1, &new, state);
2954 ++*idx;
1da177e4
LT
2955 break;
2956 }
2957 /*
2958 * If we need to, add to list of extents to delete.
2959 */
2960 if (do_fx)
2961 xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist,
2962 mp);
2963 /*
2964 * Adjust inode # blocks in the file.
2965 */
2966 if (nblks)
2967 ip->i_d.di_nblocks -= nblks;
2968 /*
2969 * Adjust quota data.
2970 */
2971 if (qfield)
7d095257 2972 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
1da177e4
LT
2973
2974 /*
2975 * Account for change in delayed indirect blocks.
2976 * Nothing to do for disk quota accounting here.
2977 */
2978 ASSERT(da_old >= da_new);
96540c78
CH
2979 if (da_old > da_new) {
2980 xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
54893273 2981 (int64_t)(da_old - da_new), 0);
96540c78 2982 }
1da177e4
LT
2983done:
2984 *logflagsp = flags;
2985 return error;
2986}
2987
2988/*
2989 * Remove the entry "free" from the free item list. Prev points to the
2990 * previous entry, unless "free" is the head of the list.
2991 */
2992STATIC void
2993xfs_bmap_del_free(
2994 xfs_bmap_free_t *flist, /* free item list header */
2995 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
2996 xfs_bmap_free_item_t *free) /* list item to be freed */
2997{
2998 if (prev)
2999 prev->xbfi_next = free->xbfi_next;
3000 else
3001 flist->xbf_first = free->xbfi_next;
3002 flist->xbf_count--;
3003 kmem_zone_free(xfs_bmap_free_item_zone, free);
3004}
3005
1da177e4
LT
3006/*
3007 * Convert an extents-format file into a btree-format file.
3008 * The new file will have a root block (in the inode) and a single child block.
3009 */
3010STATIC int /* error */
3011xfs_bmap_extents_to_btree(
3012 xfs_trans_t *tp, /* transaction pointer */
3013 xfs_inode_t *ip, /* incore inode pointer */
3014 xfs_fsblock_t *firstblock, /* first-block-allocated */
3015 xfs_bmap_free_t *flist, /* blocks freed in xaction */
3016 xfs_btree_cur_t **curp, /* cursor returned to caller */
3017 int wasdel, /* converting a delayed alloc */
3018 int *logflagsp, /* inode logging flags */
3019 int whichfork) /* data or attr fork */
3020{
7cc95a82 3021 struct xfs_btree_block *ablock; /* allocated (child) bt block */
1da177e4
LT
3022 xfs_buf_t *abp; /* buffer for ablock */
3023 xfs_alloc_arg_t args; /* allocation arguments */
3024 xfs_bmbt_rec_t *arp; /* child record pointer */
7cc95a82 3025 struct xfs_btree_block *block; /* btree root block */
1da177e4 3026 xfs_btree_cur_t *cur; /* bmap btree cursor */
a6f64d4a 3027 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
1da177e4 3028 int error; /* error return value */
4eea22f0 3029 xfs_extnum_t i, cnt; /* extent record index */
1da177e4
LT
3030 xfs_ifork_t *ifp; /* inode fork pointer */
3031 xfs_bmbt_key_t *kp; /* root block key pointer */
3032 xfs_mount_t *mp; /* mount structure */
4eea22f0 3033 xfs_extnum_t nextents; /* number of file extents */
1da177e4
LT
3034 xfs_bmbt_ptr_t *pp; /* root block address pointer */
3035
3036 ifp = XFS_IFORK_PTR(ip, whichfork);
3037 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
3038 ASSERT(ifp->if_ext_max ==
3039 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
3040 /*
3041 * Make space in the inode incore.
3042 */
3043 xfs_iroot_realloc(ip, 1, whichfork);
3044 ifp->if_flags |= XFS_IFBROOT;
7cc95a82 3045
1da177e4
LT
3046 /*
3047 * Fill in the root.
3048 */
3049 block = ifp->if_broot;
16259e7d
CH
3050 block->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
3051 block->bb_level = cpu_to_be16(1);
3052 block->bb_numrecs = cpu_to_be16(1);
7cc95a82
CH
3053 block->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
3054 block->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
3055
1da177e4
LT
3056 /*
3057 * Need a cursor. Can't allocate until bb_level is filled in.
3058 */
3059 mp = ip->i_mount;
561f7d17 3060 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
1da177e4
LT
3061 cur->bc_private.b.firstblock = *firstblock;
3062 cur->bc_private.b.flist = flist;
3063 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
3064 /*
3065 * Convert to a btree with two levels, one record in root.
3066 */
3067 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
3068 args.tp = tp;
3069 args.mp = mp;
d210a28c 3070 args.firstblock = *firstblock;
1da177e4
LT
3071 if (*firstblock == NULLFSBLOCK) {
3072 args.type = XFS_ALLOCTYPE_START_BNO;
3073 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
3074 } else if (flist->xbf_low) {
3075 args.type = XFS_ALLOCTYPE_START_BNO;
3076 args.fsbno = *firstblock;
3077 } else {
3078 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3079 args.fsbno = *firstblock;
3080 }
3081 args.minlen = args.maxlen = args.prod = 1;
3082 args.total = args.minleft = args.alignment = args.mod = args.isfl =
3083 args.minalignslop = 0;
3084 args.wasdel = wasdel;
3085 *logflagsp = 0;
3086 if ((error = xfs_alloc_vextent(&args))) {
3087 xfs_iroot_realloc(ip, -1, whichfork);
3088 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
3089 return error;
3090 }
3091 /*
3092 * Allocation can't fail, the space was reserved.
3093 */
3094 ASSERT(args.fsbno != NULLFSBLOCK);
3095 ASSERT(*firstblock == NULLFSBLOCK ||
3096 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
3097 (flist->xbf_low &&
3098 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
3099 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
3100 cur->bc_private.b.allocated++;
3101 ip->i_d.di_nblocks++;
7d095257 3102 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
1da177e4
LT
3103 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
3104 /*
3105 * Fill in the child block.
3106 */
7cc95a82 3107 ablock = XFS_BUF_TO_BLOCK(abp);
16259e7d 3108 ablock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
1da177e4 3109 ablock->bb_level = 0;
7cc95a82
CH
3110 ablock->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
3111 ablock->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
136341b4 3112 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
1da177e4 3113 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4eea22f0
MK
3114 for (cnt = i = 0; i < nextents; i++) {
3115 ep = xfs_iext_get_ext(ifp, i);
9d87c319 3116 if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
cd8b0a97
CH
3117 arp->l0 = cpu_to_be64(ep->l0);
3118 arp->l1 = cpu_to_be64(ep->l1);
1da177e4
LT
3119 arp++; cnt++;
3120 }
3121 }
16259e7d 3122 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
7cc95a82
CH
3123 xfs_btree_set_numrecs(ablock, cnt);
3124
1da177e4
LT
3125 /*
3126 * Fill in the root key and pointer.
3127 */
136341b4
CH
3128 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
3129 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
8801bb99 3130 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
136341b4
CH
3131 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
3132 be16_to_cpu(block->bb_level)));
576039cf 3133 *pp = cpu_to_be64(args.fsbno);
136341b4 3134
1da177e4
LT
3135 /*
3136 * Do all this logging at the end so that
3137 * the root is at the right level.
3138 */
fd6bcc5b
CH
3139 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
3140 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
1da177e4
LT
3141 ASSERT(*curp == NULL);
3142 *curp = cur;
9d87c319 3143 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
1da177e4
LT
3144 return 0;
3145}
3146
1a5902c5
CH
3147/*
3148 * Calculate the default attribute fork offset for newly created inodes.
3149 */
3150uint
3151xfs_default_attroffset(
3152 struct xfs_inode *ip)
3153{
3154 struct xfs_mount *mp = ip->i_mount;
3155 uint offset;
3156
3157 if (mp->m_sb.sb_inodesize == 256) {
3158 offset = XFS_LITINO(mp) -
3159 XFS_BMDR_SPACE_CALC(MINABTPTRS);
3160 } else {
3161 offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
3162 }
3163
3164 ASSERT(offset < XFS_LITINO(mp));
3165 return offset;
3166}
3167
d8cc890d
NS
3168/*
3169 * Helper routine to reset inode di_forkoff field when switching
3170 * attribute fork from local to extent format - we reset it where
3171 * possible to make space available for inline data fork extents.
3172 */
3173STATIC void
3174xfs_bmap_forkoff_reset(
3175 xfs_mount_t *mp,
3176 xfs_inode_t *ip,
3177 int whichfork)
3178{
3179 if (whichfork == XFS_ATTR_FORK &&
1a5902c5
CH
3180 ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
3181 ip->i_d.di_format != XFS_DINODE_FMT_UUID &&
3182 ip->i_d.di_format != XFS_DINODE_FMT_BTREE) {
3183 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
3184
3185 if (dfl_forkoff > ip->i_d.di_forkoff) {
3186 ip->i_d.di_forkoff = dfl_forkoff;
3187 ip->i_df.if_ext_max =
3188 XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t);
3189 ip->i_afp->if_ext_max =
3190 XFS_IFORK_ASIZE(ip) / sizeof(xfs_bmbt_rec_t);
3191 }
d8cc890d
NS
3192 }
3193}
3194
1da177e4
LT
3195/*
3196 * Convert a local file to an extents file.
3197 * This code is out of bounds for data forks of regular files,
3198 * since the file data needs to get logged so things will stay consistent.
3199 * (The bmap-level manipulations are ok, though).
3200 */
3201STATIC int /* error */
3202xfs_bmap_local_to_extents(
3203 xfs_trans_t *tp, /* transaction pointer */
3204 xfs_inode_t *ip, /* incore inode pointer */
3205 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
3206 xfs_extlen_t total, /* total blocks needed by transaction */
3207 int *logflagsp, /* inode logging flags */
3208 int whichfork) /* data or attr fork */
3209{
3210 int error; /* error return value */
3211 int flags; /* logging flags returned */
1da177e4
LT
3212 xfs_ifork_t *ifp; /* inode fork pointer */
3213
3214 /*
3215 * We don't want to deal with the case of keeping inode data inline yet.
3216 * So sending the data fork of a regular inode is invalid.
3217 */
abbede1b 3218 ASSERT(!(S_ISREG(ip->i_d.di_mode) && whichfork == XFS_DATA_FORK));
1da177e4
LT
3219 ifp = XFS_IFORK_PTR(ip, whichfork);
3220 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
3221 flags = 0;
3222 error = 0;
3223 if (ifp->if_bytes) {
3224 xfs_alloc_arg_t args; /* allocation arguments */
4eea22f0 3225 xfs_buf_t *bp; /* buffer for extent block */
a6f64d4a 3226 xfs_bmbt_rec_host_t *ep;/* extent record pointer */
1da177e4
LT
3227
3228 args.tp = tp;
3229 args.mp = ip->i_mount;
d210a28c 3230 args.firstblock = *firstblock;
f020b67f
MK
3231 ASSERT((ifp->if_flags &
3232 (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == XFS_IFINLINE);
1da177e4
LT
3233 /*
3234 * Allocate a block. We know we need only one, since the
3235 * file currently fits in an inode.
3236 */
3237 if (*firstblock == NULLFSBLOCK) {
3238 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
3239 args.type = XFS_ALLOCTYPE_START_BNO;
3240 } else {
3241 args.fsbno = *firstblock;
3242 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3243 }
3244 args.total = total;
3245 args.mod = args.minleft = args.alignment = args.wasdel =
3246 args.isfl = args.minalignslop = 0;
3247 args.minlen = args.maxlen = args.prod = 1;
3248 if ((error = xfs_alloc_vextent(&args)))
3249 goto done;
3250 /*
3251 * Can't fail, the space was reserved.
3252 */
3253 ASSERT(args.fsbno != NULLFSBLOCK);
3254 ASSERT(args.len == 1);
3255 *firstblock = args.fsbno;
3256 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
62926044 3257 memcpy(bp->b_addr, ifp->if_u1.if_data, ifp->if_bytes);
1da177e4 3258 xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
d8cc890d 3259 xfs_bmap_forkoff_reset(args.mp, ip, whichfork);
1da177e4 3260 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
4eea22f0
MK
3261 xfs_iext_add(ifp, 0, 1);
3262 ep = xfs_iext_get_ext(ifp, 0);
1da177e4 3263 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
0b1b213f
CH
3264 trace_xfs_bmap_post_update(ip, 0,
3265 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0,
3266 _THIS_IP_);
1da177e4
LT
3267 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
3268 ip->i_d.di_nblocks = 1;
7d095257 3269 xfs_trans_mod_dquot_byino(tp, ip,
1da177e4 3270 XFS_TRANS_DQ_BCOUNT, 1L);
9d87c319 3271 flags |= xfs_ilog_fext(whichfork);
d8cc890d 3272 } else {
1da177e4 3273 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
d8cc890d
NS
3274 xfs_bmap_forkoff_reset(ip->i_mount, ip, whichfork);
3275 }
1da177e4
LT
3276 ifp->if_flags &= ~XFS_IFINLINE;
3277 ifp->if_flags |= XFS_IFEXTENTS;
3278 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
3279 flags |= XFS_ILOG_CORE;
3280done:
3281 *logflagsp = flags;
3282 return error;
3283}
3284
0293ce3a 3285/*
8867bc9b
MK
3286 * Search the extent records for the entry containing block bno.
3287 * If bno lies in a hole, point to the next entry. If bno lies
3288 * past eof, *eofp will be set, and *prevp will contain the last
3289 * entry (null if none). Else, *lastxp will be set to the index
3290 * of the found entry; *gotp will contain the entry.
0293ce3a 3291 */
d96f8f89 3292STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
0293ce3a
MK
3293xfs_bmap_search_multi_extents(
3294 xfs_ifork_t *ifp, /* inode fork pointer */
3295 xfs_fileoff_t bno, /* block number searched for */
3296 int *eofp, /* out: end of file found */
3297 xfs_extnum_t *lastxp, /* out: last extent index */
3298 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3299 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3300{
a6f64d4a 3301 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
0293ce3a 3302 xfs_extnum_t lastx; /* last extent index */
0293ce3a
MK
3303
3304 /*
8867bc9b
MK
3305 * Initialize the extent entry structure to catch access to
3306 * uninitialized br_startblock field.
0293ce3a 3307 */
8867bc9b
MK
3308 gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL;
3309 gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL;
3310 gotp->br_state = XFS_EXT_INVALID;
3311#if XFS_BIG_BLKNOS
3312 gotp->br_startblock = 0xffffa5a5a5a5a5a5LL;
3313#else
3314 gotp->br_startblock = 0xffffa5a5;
3315#endif
3316 prevp->br_startoff = NULLFILEOFF;
3317
3318 ep = xfs_iext_bno_to_ext(ifp, bno, &lastx);
3319 if (lastx > 0) {
3320 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp);
0293ce3a 3321 }
8867bc9b
MK
3322 if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
3323 xfs_bmbt_get_all(ep, gotp);
3324 *eofp = 0;
3325 } else {
3326 if (lastx > 0) {
3327 *gotp = *prevp;
3328 }
3329 *eofp = 1;
3330 ep = NULL;
0293ce3a 3331 }
8867bc9b 3332 *lastxp = lastx;
0293ce3a
MK
3333 return ep;
3334}
3335
1da177e4
LT
3336/*
3337 * Search the extents list for the inode, for the extent containing bno.
3338 * If bno lies in a hole, point to the next entry. If bno lies past eof,
3339 * *eofp will be set, and *prevp will contain the last entry (null if none).
3340 * Else, *lastxp will be set to the index of the found
3341 * entry; *gotp will contain the entry.
3342 */
a6f64d4a 3343STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
1da177e4
LT
3344xfs_bmap_search_extents(
3345 xfs_inode_t *ip, /* incore inode pointer */
3346 xfs_fileoff_t bno, /* block number searched for */
572d95f4 3347 int fork, /* data or attr fork */
1da177e4
LT
3348 int *eofp, /* out: end of file found */
3349 xfs_extnum_t *lastxp, /* out: last extent index */
3350 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3351 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3352{
3353 xfs_ifork_t *ifp; /* inode fork pointer */
a6f64d4a 3354 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
1da177e4
LT
3355
3356 XFS_STATS_INC(xs_look_exlist);
572d95f4 3357 ifp = XFS_IFORK_PTR(ip, fork);
1da177e4 3358
0293ce3a
MK
3359 ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
3360
572d95f4
NS
3361 if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) &&
3362 !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) {
6a19d939 3363 xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO,
572d95f4
NS
3364 "Access to block zero in inode %llu "
3365 "start_block: %llx start_off: %llx "
3366 "blkcnt: %llx extent-state: %x lastx: %x\n",
3367 (unsigned long long)ip->i_ino,
3ddb8fa9
NS
3368 (unsigned long long)gotp->br_startblock,
3369 (unsigned long long)gotp->br_startoff,
3370 (unsigned long long)gotp->br_blockcount,
572d95f4
NS
3371 gotp->br_state, *lastxp);
3372 *lastxp = NULLEXTNUM;
3373 *eofp = 1;
3374 return NULL;
3375 }
3376 return ep;
1da177e4
LT
3377}
3378
1da177e4
LT
3379/*
3380 * Compute the worst-case number of indirect blocks that will be used
3381 * for ip's delayed extent of length "len".
3382 */
3383STATIC xfs_filblks_t
3384xfs_bmap_worst_indlen(
3385 xfs_inode_t *ip, /* incore inode pointer */
3386 xfs_filblks_t len) /* delayed extent length */
3387{
3388 int level; /* btree level number */
3389 int maxrecs; /* maximum record count at this level */
3390 xfs_mount_t *mp; /* mount structure */
3391 xfs_filblks_t rval; /* return value */
3392
3393 mp = ip->i_mount;
3394 maxrecs = mp->m_bmap_dmxr[0];
3395 for (level = 0, rval = 0;
3396 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
3397 level++) {
3398 len += maxrecs - 1;
3399 do_div(len, maxrecs);
3400 rval += len;
3401 if (len == 1)
3402 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
3403 level - 1;
3404 if (level == 0)
3405 maxrecs = mp->m_bmap_dmxr[1];
3406 }
3407 return rval;
3408}
3409
1da177e4
LT
3410/*
3411 * Convert inode from non-attributed to attributed.
3412 * Must not be in a transaction, ip must not be locked.
3413 */
3414int /* error code */
3415xfs_bmap_add_attrfork(
3416 xfs_inode_t *ip, /* incore inode pointer */
d8cc890d
NS
3417 int size, /* space new attribute needs */
3418 int rsvd) /* xact may use reserved blks */
1da177e4 3419{
1da177e4 3420 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
4eea22f0 3421 xfs_bmap_free_t flist; /* freed extent records */
1da177e4 3422 xfs_mount_t *mp; /* mount structure */
1da177e4 3423 xfs_trans_t *tp; /* transaction pointer */
d8cc890d
NS
3424 int blks; /* space reservation */
3425 int version = 1; /* superblock attr version */
3426 int committed; /* xaction was committed */
3427 int logflags; /* logging flags */
3428 int error; /* error return value */
1da177e4 3429
d8cc890d 3430 ASSERT(XFS_IFORK_Q(ip) == 0);
1da177e4
LT
3431 ASSERT(ip->i_df.if_ext_max ==
3432 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
d8cc890d 3433
1da177e4
LT
3434 mp = ip->i_mount;
3435 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
3436 tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK);
3437 blks = XFS_ADDAFORK_SPACE_RES(mp);
3438 if (rsvd)
3439 tp->t_flags |= XFS_TRANS_RESERVE;
3440 if ((error = xfs_trans_reserve(tp, blks, XFS_ADDAFORK_LOG_RES(mp), 0,
3441 XFS_TRANS_PERM_LOG_RES, XFS_ADDAFORK_LOG_COUNT)))
3442 goto error0;
3443 xfs_ilock(ip, XFS_ILOCK_EXCL);
7d095257 3444 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
1da177e4
LT
3445 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
3446 XFS_QMOPT_RES_REGBLKS);
3447 if (error) {
3448 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3449 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES);
3450 return error;
3451 }
3452 if (XFS_IFORK_Q(ip))
3453 goto error1;
3454 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
3455 /*
3456 * For inodes coming from pre-6.2 filesystems.
3457 */
3458 ASSERT(ip->i_d.di_aformat == 0);
3459 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
3460 }
3461 ASSERT(ip->i_d.di_anextents == 0);
898621d5
CH
3462
3463 xfs_trans_ijoin_ref(tp, ip, XFS_ILOCK_EXCL);
1da177e4 3464 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
898621d5 3465
1da177e4
LT
3466 switch (ip->i_d.di_format) {
3467 case XFS_DINODE_FMT_DEV:
3468 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
3469 break;
3470 case XFS_DINODE_FMT_UUID:
3471 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
3472 break;
3473 case XFS_DINODE_FMT_LOCAL:
3474 case XFS_DINODE_FMT_EXTENTS:
3475 case XFS_DINODE_FMT_BTREE:
d8cc890d
NS
3476 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
3477 if (!ip->i_d.di_forkoff)
1a5902c5 3478 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
13059ff0 3479 else if (mp->m_flags & XFS_MOUNT_ATTR2)
d8cc890d 3480 version = 2;
1da177e4
LT
3481 break;
3482 default:
3483 ASSERT(0);
3484 error = XFS_ERROR(EINVAL);
3485 goto error1;
3486 }
3487 ip->i_df.if_ext_max =
3488 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
3489 ASSERT(ip->i_afp == NULL);
3490 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
3491 ip->i_afp->if_ext_max =
3492 XFS_IFORK_ASIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
3493 ip->i_afp->if_flags = XFS_IFEXTENTS;
3494 logflags = 0;
9d87c319 3495 xfs_bmap_init(&flist, &firstblock);
1da177e4
LT
3496 switch (ip->i_d.di_format) {
3497 case XFS_DINODE_FMT_LOCAL:
3498 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist,
3499 &logflags);
3500 break;
3501 case XFS_DINODE_FMT_EXTENTS:
3502 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
3503 &flist, &logflags);
3504 break;
3505 case XFS_DINODE_FMT_BTREE:
3506 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist,
3507 &logflags);
3508 break;
3509 default:
3510 error = 0;
3511 break;
3512 }
3513 if (logflags)
3514 xfs_trans_log_inode(tp, ip, logflags);
3515 if (error)
3516 goto error2;
62118709
ES
3517 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
3518 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
da087bad
NS
3519 __int64_t sbfields = 0;
3520
3685c2a1 3521 spin_lock(&mp->m_sb_lock);
62118709
ES
3522 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
3523 xfs_sb_version_addattr(&mp->m_sb);
da087bad 3524 sbfields |= XFS_SB_VERSIONNUM;
d8cc890d 3525 }
62118709
ES
3526 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
3527 xfs_sb_version_addattr2(&mp->m_sb);
da087bad 3528 sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
d8cc890d 3529 }
da087bad 3530 if (sbfields) {
3685c2a1 3531 spin_unlock(&mp->m_sb_lock);
da087bad 3532 xfs_mod_sb(tp, sbfields);
1da177e4 3533 } else
3685c2a1 3534 spin_unlock(&mp->m_sb_lock);
1da177e4 3535 }
f7c99b6f 3536 if ((error = xfs_bmap_finish(&tp, &flist, &committed)))
1da177e4 3537 goto error2;
713bf88b 3538 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1da177e4
LT
3539 ASSERT(ip->i_df.if_ext_max ==
3540 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
3541 return error;
3542error2:
3543 xfs_bmap_cancel(&flist);
3544error1:
1da177e4
LT
3545 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3546error0:
3547 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
3548 ASSERT(ip->i_df.if_ext_max ==
3549 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
3550 return error;
3551}
3552
3553/*
3554 * Add the extent to the list of extents to be free at transaction end.
3555 * The list is maintained sorted (by block number).
3556 */
3557/* ARGSUSED */
3558void
3559xfs_bmap_add_free(
3560 xfs_fsblock_t bno, /* fs block number of extent */
3561 xfs_filblks_t len, /* length of extent */
3562 xfs_bmap_free_t *flist, /* list of extents */
3563 xfs_mount_t *mp) /* mount point structure */
3564{
3565 xfs_bmap_free_item_t *cur; /* current (next) element */
3566 xfs_bmap_free_item_t *new; /* new element */
3567 xfs_bmap_free_item_t *prev; /* previous element */
3568#ifdef DEBUG
3569 xfs_agnumber_t agno;
3570 xfs_agblock_t agbno;
3571
3572 ASSERT(bno != NULLFSBLOCK);
3573 ASSERT(len > 0);
3574 ASSERT(len <= MAXEXTLEN);
9d87c319 3575 ASSERT(!isnullstartblock(bno));
1da177e4
LT
3576 agno = XFS_FSB_TO_AGNO(mp, bno);
3577 agbno = XFS_FSB_TO_AGBNO(mp, bno);
3578 ASSERT(agno < mp->m_sb.sb_agcount);
3579 ASSERT(agbno < mp->m_sb.sb_agblocks);
3580 ASSERT(len < mp->m_sb.sb_agblocks);
3581 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
3582#endif
3583 ASSERT(xfs_bmap_free_item_zone != NULL);
3584 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
3585 new->xbfi_startblock = bno;
3586 new->xbfi_blockcount = (xfs_extlen_t)len;
3587 for (prev = NULL, cur = flist->xbf_first;
3588 cur != NULL;
3589 prev = cur, cur = cur->xbfi_next) {
3590 if (cur->xbfi_startblock >= bno)
3591 break;
3592 }
3593 if (prev)
3594 prev->xbfi_next = new;
3595 else
3596 flist->xbf_first = new;
3597 new->xbfi_next = cur;
3598 flist->xbf_count++;
3599}
3600
3601/*
3602 * Compute and fill in the value of the maximum depth of a bmap btree
3603 * in this filesystem. Done once, during mount.
3604 */
3605void
3606xfs_bmap_compute_maxlevels(
3607 xfs_mount_t *mp, /* file system mount structure */
3608 int whichfork) /* data or attr fork */
3609{
3610 int level; /* btree level */
3611 uint maxblocks; /* max blocks at this level */
3612 uint maxleafents; /* max leaf entries possible */
3613 int maxrootrecs; /* max records in root block */
3614 int minleafrecs; /* min records in leaf block */
3615 int minnoderecs; /* min records in node block */
3616 int sz; /* root block size */
3617
3618 /*
3619 * The maximum number of extents in a file, hence the maximum
3620 * number of leaf entries, is controlled by the type of di_nextents
3621 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
3622 * (a signed 16-bit number, xfs_aextnum_t).
6d1337b2
TS
3623 *
3624 * Note that we can no longer assume that if we are in ATTR1 that
1a5902c5
CH
3625 * the fork offset of all the inodes will be
3626 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
3627 * with ATTR2 and then mounted back with ATTR1, keeping the
3628 * di_forkoff's fixed but probably at various positions. Therefore,
3629 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
3630 * of a minimum size available.
1da177e4 3631 */
d8cc890d
NS
3632 if (whichfork == XFS_DATA_FORK) {
3633 maxleafents = MAXEXTNUM;
6d1337b2 3634 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
d8cc890d
NS
3635 } else {
3636 maxleafents = MAXAEXTNUM;
6d1337b2 3637 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
d8cc890d 3638 }
60197e8d 3639 maxrootrecs = xfs_bmdr_maxrecs(mp, sz, 0);
1da177e4
LT
3640 minleafrecs = mp->m_bmap_dmnr[0];
3641 minnoderecs = mp->m_bmap_dmnr[1];
1da177e4
LT
3642 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
3643 for (level = 1; maxblocks > 1; level++) {
3644 if (maxblocks <= maxrootrecs)
3645 maxblocks = 1;
3646 else
3647 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
3648 }
3649 mp->m_bm_maxlevels[whichfork] = level;
3650}
3651
3652/*
3653 * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
3654 * caller. Frees all the extents that need freeing, which must be done
3655 * last due to locking considerations. We never free any extents in
859f57ca 3656 * the first transaction.
1da177e4
LT
3657 *
3658 * Return 1 if the given transaction was committed and a new one
3659 * started, and 0 otherwise in the committed parameter.
3660 */
1da177e4
LT
3661int /* error */
3662xfs_bmap_finish(
3663 xfs_trans_t **tp, /* transaction pointer addr */
3664 xfs_bmap_free_t *flist, /* i/o: list extents to free */
1da177e4
LT
3665 int *committed) /* xact committed or not */
3666{
3667 xfs_efd_log_item_t *efd; /* extent free data */
3668 xfs_efi_log_item_t *efi; /* extent free intention */
3669 int error; /* error return value */
4eea22f0 3670 xfs_bmap_free_item_t *free; /* free extent item */
1da177e4
LT
3671 unsigned int logres; /* new log reservation */
3672 unsigned int logcount; /* new log count */
3673 xfs_mount_t *mp; /* filesystem mount structure */
3674 xfs_bmap_free_item_t *next; /* next item on free list */
3675 xfs_trans_t *ntp; /* new transaction pointer */
3676
3677 ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
3678 if (flist->xbf_count == 0) {
3679 *committed = 0;
3680 return 0;
3681 }
3682 ntp = *tp;
3683 efi = xfs_trans_get_efi(ntp, flist->xbf_count);
3684 for (free = flist->xbf_first; free; free = free->xbfi_next)
3685 xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock,
3686 free->xbfi_blockcount);
3687 logres = ntp->t_log_res;
3688 logcount = ntp->t_log_count;
3689 ntp = xfs_trans_dup(*tp);
1c72bf90 3690 error = xfs_trans_commit(*tp, 0);
1da177e4
LT
3691 *tp = ntp;
3692 *committed = 1;
3693 /*
3694 * We have a new transaction, so we should return committed=1,
3695 * even though we're returning an error.
3696 */
cc09c0dc 3697 if (error)
1da177e4 3698 return error;
cc09c0dc
DC
3699
3700 /*
3701 * transaction commit worked ok so we can drop the extra ticket
3702 * reference that we gained in xfs_trans_dup()
3703 */
3704 xfs_log_ticket_put(ntp->t_ticket);
3705
1da177e4
LT
3706 if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES,
3707 logcount)))
3708 return error;
3709 efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count);
3710 for (free = flist->xbf_first; free != NULL; free = next) {
3711 next = free->xbfi_next;
3712 if ((error = xfs_free_extent(ntp, free->xbfi_startblock,
3713 free->xbfi_blockcount))) {
3714 /*
3715 * The bmap free list will be cleaned up at a
3716 * higher level. The EFI will be canceled when
3717 * this transaction is aborted.
3718 * Need to force shutdown here to make sure it
3719 * happens, since this transaction may not be
3720 * dirty yet.
3721 */
3722 mp = ntp->t_mountp;
3723 if (!XFS_FORCED_SHUTDOWN(mp))
3724 xfs_force_shutdown(mp,
3725 (error == EFSCORRUPTED) ?
7d04a335
NS
3726 SHUTDOWN_CORRUPT_INCORE :
3727 SHUTDOWN_META_IO_ERROR);
1da177e4
LT
3728 return error;
3729 }
3730 xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock,
3731 free->xbfi_blockcount);
3732 xfs_bmap_del_free(flist, NULL, free);
3733 }
3734 return 0;
3735}
3736
3737/*
3738 * Free up any items left in the list.
3739 */
3740void
3741xfs_bmap_cancel(
3742 xfs_bmap_free_t *flist) /* list of bmap_free_items */
3743{
3744 xfs_bmap_free_item_t *free; /* free list item */
3745 xfs_bmap_free_item_t *next;
3746
3747 if (flist->xbf_count == 0)
3748 return;
3749 ASSERT(flist->xbf_first != NULL);
3750 for (free = flist->xbf_first; free; free = next) {
3751 next = free->xbfi_next;
3752 xfs_bmap_del_free(flist, NULL, free);
3753 }
3754 ASSERT(flist->xbf_count == 0);
3755}
3756
3757/*
3758 * Returns the file-relative block number of the first unused block(s)
3759 * in the file with at least "len" logically contiguous blocks free.
3760 * This is the lowest-address hole if the file has holes, else the first block
3761 * past the end of file.
3762 * Return 0 if the file is currently local (in-inode).
3763 */
3764int /* error */
3765xfs_bmap_first_unused(
3766 xfs_trans_t *tp, /* transaction pointer */
3767 xfs_inode_t *ip, /* incore inode */
3768 xfs_extlen_t len, /* size of hole to find */
3769 xfs_fileoff_t *first_unused, /* unused block */
3770 int whichfork) /* data or attr fork */
3771{
1da177e4 3772 int error; /* error return value */
4eea22f0 3773 int idx; /* extent record index */
1da177e4
LT
3774 xfs_ifork_t *ifp; /* inode fork pointer */
3775 xfs_fileoff_t lastaddr; /* last block number seen */
3776 xfs_fileoff_t lowest; /* lowest useful block */
3777 xfs_fileoff_t max; /* starting useful block */
3778 xfs_fileoff_t off; /* offset for this block */
3779 xfs_extnum_t nextents; /* number of extent entries */
3780
3781 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
3782 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
3783 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
3784 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
3785 *first_unused = 0;
3786 return 0;
3787 }
3788 ifp = XFS_IFORK_PTR(ip, whichfork);
3789 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
3790 (error = xfs_iread_extents(tp, ip, whichfork)))
3791 return error;
3792 lowest = *first_unused;
3793 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4eea22f0 3794 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
a6f64d4a 3795 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
1da177e4
LT
3796 off = xfs_bmbt_get_startoff(ep);
3797 /*
3798 * See if the hole before this extent will work.
3799 */
3800 if (off >= lowest + len && off - max >= len) {
3801 *first_unused = max;
3802 return 0;
3803 }
3804 lastaddr = off + xfs_bmbt_get_blockcount(ep);
3805 max = XFS_FILEOFF_MAX(lastaddr, lowest);
3806 }
3807 *first_unused = max;
3808 return 0;
3809}
3810
3811/*
3812 * Returns the file-relative block number of the last block + 1 before
3813 * last_block (input value) in the file.
4eea22f0
MK
3814 * This is not based on i_size, it is based on the extent records.
3815 * Returns 0 for local files, as they do not have extent records.
1da177e4
LT
3816 */
3817int /* error */
3818xfs_bmap_last_before(
3819 xfs_trans_t *tp, /* transaction pointer */
3820 xfs_inode_t *ip, /* incore inode */
3821 xfs_fileoff_t *last_block, /* last block */
3822 int whichfork) /* data or attr fork */
3823{
3824 xfs_fileoff_t bno; /* input file offset */
3825 int eof; /* hit end of file */
a6f64d4a 3826 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
1da177e4
LT
3827 int error; /* error return value */
3828 xfs_bmbt_irec_t got; /* current extent value */
3829 xfs_ifork_t *ifp; /* inode fork pointer */
3830 xfs_extnum_t lastx; /* last extent used */
3831 xfs_bmbt_irec_t prev; /* previous extent value */
3832
3833 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
3834 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
3835 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
3836 return XFS_ERROR(EIO);
3837 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
3838 *last_block = 0;
3839 return 0;
3840 }
3841 ifp = XFS_IFORK_PTR(ip, whichfork);
3842 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
3843 (error = xfs_iread_extents(tp, ip, whichfork)))
3844 return error;
3845 bno = *last_block - 1;
3846 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
3847 &prev);
3848 if (eof || xfs_bmbt_get_startoff(ep) > bno) {
3849 if (prev.br_startoff == NULLFILEOFF)
3850 *last_block = 0;
3851 else
3852 *last_block = prev.br_startoff + prev.br_blockcount;
3853 }
3854 /*
3855 * Otherwise *last_block is already the right answer.
3856 */
3857 return 0;
3858}
3859
27a3f8f2
CH
3860STATIC int
3861xfs_bmap_last_extent(
3862 struct xfs_trans *tp,
3863 struct xfs_inode *ip,
3864 int whichfork,
3865 struct xfs_bmbt_irec *rec,
3866 int *is_empty)
3867{
3868 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
3869 int error;
3870 int nextents;
3871
3872 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
3873 error = xfs_iread_extents(tp, ip, whichfork);
3874 if (error)
3875 return error;
3876 }
3877
3878 nextents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
3879 if (nextents == 0) {
3880 *is_empty = 1;
3881 return 0;
3882 }
3883
3884 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, nextents - 1), rec);
3885 *is_empty = 0;
3886 return 0;
3887}
3888
3889/*
3890 * Check the last inode extent to determine whether this allocation will result
3891 * in blocks being allocated at the end of the file. When we allocate new data
3892 * blocks at the end of the file which do not start at the previous data block,
3893 * we will try to align the new blocks at stripe unit boundaries.
3894 *
1b16447b
DC
3895 * Returns 0 in bma->aeof if the file (fork) is empty as any new write will be
3896 * at, or past the EOF.
27a3f8f2
CH
3897 */
3898STATIC int
3899xfs_bmap_isaeof(
1b16447b
DC
3900 struct xfs_bmalloca *bma,
3901 int whichfork)
27a3f8f2
CH
3902{
3903 struct xfs_bmbt_irec rec;
3904 int is_empty;
3905 int error;
3906
1b16447b
DC
3907 bma->aeof = 0;
3908 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
3909 &is_empty);
27a3f8f2
CH
3910 if (error || is_empty)
3911 return error;
3912
3913 /*
3914 * Check if we are allocation or past the last extent, or at least into
3915 * the last delayed allocated extent.
3916 */
3a75667e
DC
3917 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
3918 (bma->offset >= rec.br_startoff &&
1b16447b 3919 isnullstartblock(rec.br_startblock));
27a3f8f2
CH
3920 return 0;
3921}
3922
3923/*
3924 * Check if the endoff is outside the last extent. If so the caller will grow
3925 * the allocation to a stripe unit boundary. All offsets are considered outside
3926 * the end of file for an empty fork, so 1 is returned in *eof in that case.
3927 */
3928int
3929xfs_bmap_eof(
3930 struct xfs_inode *ip,
3931 xfs_fileoff_t endoff,
3932 int whichfork,
3933 int *eof)
3934{
3935 struct xfs_bmbt_irec rec;
3936 int error;
3937
3938 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, eof);
3939 if (error || *eof)
3940 return error;
3941
3942 *eof = endoff >= rec.br_startoff + rec.br_blockcount;
3943 return 0;
3944}
3945
1da177e4
LT
3946/*
3947 * Returns the file-relative block number of the first block past eof in
4eea22f0
MK
3948 * the file. This is not based on i_size, it is based on the extent records.
3949 * Returns 0 for local files, as they do not have extent records.
1da177e4 3950 */
27a3f8f2 3951int
1da177e4 3952xfs_bmap_last_offset(
27a3f8f2
CH
3953 struct xfs_trans *tp,
3954 struct xfs_inode *ip,
3955 xfs_fileoff_t *last_block,
3956 int whichfork)
1da177e4 3957{
27a3f8f2
CH
3958 struct xfs_bmbt_irec rec;
3959 int is_empty;
3960 int error;
3961
3962 *last_block = 0;
3963
3964 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
3965 return 0;
1da177e4
LT
3966
3967 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
27a3f8f2 3968 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1da177e4 3969 return XFS_ERROR(EIO);
27a3f8f2
CH
3970
3971 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
3972 if (error || is_empty)
1da177e4 3973 return error;
27a3f8f2
CH
3974
3975 *last_block = rec.br_startoff + rec.br_blockcount;
1da177e4
LT
3976 return 0;
3977}
3978
3979/*
3980 * Returns whether the selected fork of the inode has exactly one
3981 * block or not. For the data fork we check this matches di_size,
3982 * implying the file's range is 0..bsize-1.
3983 */
3984int /* 1=>1 block, 0=>otherwise */
3985xfs_bmap_one_block(
3986 xfs_inode_t *ip, /* incore inode */
3987 int whichfork) /* data or attr fork */
3988{
a6f64d4a 3989 xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
1da177e4
LT
3990 xfs_ifork_t *ifp; /* inode fork pointer */
3991 int rval; /* return value */
3992 xfs_bmbt_irec_t s; /* internal version of extent */
3993
3994#ifndef DEBUG
ba87ea69 3995 if (whichfork == XFS_DATA_FORK) {
abbede1b 3996 return S_ISREG(ip->i_d.di_mode) ?
ba87ea69
LM
3997 (ip->i_size == ip->i_mount->m_sb.sb_blocksize) :
3998 (ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize);
3999 }
1da177e4
LT
4000#endif /* !DEBUG */
4001 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
4002 return 0;
4003 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4004 return 0;
4005 ifp = XFS_IFORK_PTR(ip, whichfork);
4006 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
4eea22f0 4007 ep = xfs_iext_get_ext(ifp, 0);
1da177e4
LT
4008 xfs_bmbt_get_all(ep, &s);
4009 rval = s.br_startoff == 0 && s.br_blockcount == 1;
4010 if (rval && whichfork == XFS_DATA_FORK)
ba87ea69 4011 ASSERT(ip->i_size == ip->i_mount->m_sb.sb_blocksize);
1da177e4
LT
4012 return rval;
4013}
4014
4e8938fe
CH
4015STATIC int
4016xfs_bmap_sanity_check(
4017 struct xfs_mount *mp,
4018 struct xfs_buf *bp,
4019 int level)
4020{
4021 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
4022
69ef921b 4023 if (block->bb_magic != cpu_to_be32(XFS_BMAP_MAGIC) ||
4e8938fe
CH
4024 be16_to_cpu(block->bb_level) != level ||
4025 be16_to_cpu(block->bb_numrecs) == 0 ||
4026 be16_to_cpu(block->bb_numrecs) > mp->m_bmap_dmxr[level != 0])
4027 return 0;
4028 return 1;
4029}
4030
1da177e4
LT
4031/*
4032 * Read in the extents to if_extents.
4033 * All inode fields are set up by caller, we just traverse the btree
4034 * and copy the records in. If the file system cannot contain unwritten
4035 * extents, the records are checked for no "state" flags.
4036 */
4037int /* error */
4038xfs_bmap_read_extents(
4039 xfs_trans_t *tp, /* transaction pointer */
4040 xfs_inode_t *ip, /* incore inode */
4041 int whichfork) /* data or attr fork */
4042{
7cc95a82 4043 struct xfs_btree_block *block; /* current btree block */
1da177e4
LT
4044 xfs_fsblock_t bno; /* block # of "block" */
4045 xfs_buf_t *bp; /* buffer for "block" */
4046 int error; /* error return value */
4047 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
1da177e4
LT
4048 xfs_extnum_t i, j; /* index into the extents list */
4049 xfs_ifork_t *ifp; /* fork structure */
4050 int level; /* btree level, for checking */
4051 xfs_mount_t *mp; /* file system mount structure */
576039cf 4052 __be64 *pp; /* pointer to block address */
1da177e4
LT
4053 /* REFERENCED */
4054 xfs_extnum_t room; /* number of entries there's room for */
1da177e4
LT
4055
4056 bno = NULLFSBLOCK;
4057 mp = ip->i_mount;
4058 ifp = XFS_IFORK_PTR(ip, whichfork);
4059 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
4060 XFS_EXTFMT_INODE(ip);
4061 block = ifp->if_broot;
4062 /*
4063 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
4064 */
16259e7d
CH
4065 level = be16_to_cpu(block->bb_level);
4066 ASSERT(level > 0);
60197e8d 4067 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
576039cf
CH
4068 bno = be64_to_cpu(*pp);
4069 ASSERT(bno != NULLDFSBNO);
4070 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
4071 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
1da177e4
LT
4072 /*
4073 * Go down the tree until leaf level is reached, following the first
4074 * pointer (leftmost) at each level.
4075 */
4076 while (level-- > 0) {
4077 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4078 XFS_BMAP_BTREE_REF)))
4079 return error;
7cc95a82 4080 block = XFS_BUF_TO_BLOCK(bp);
1da177e4 4081 XFS_WANT_CORRUPTED_GOTO(
4e8938fe 4082 xfs_bmap_sanity_check(mp, bp, level),
1da177e4
LT
4083 error0);
4084 if (level == 0)
4085 break;
136341b4 4086 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
576039cf
CH
4087 bno = be64_to_cpu(*pp);
4088 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
1da177e4
LT
4089 xfs_trans_brelse(tp, bp);
4090 }
4091 /*
4092 * Here with bp and block set to the leftmost leaf node in the tree.
4093 */
4eea22f0 4094 room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1da177e4
LT
4095 i = 0;
4096 /*
4eea22f0 4097 * Loop over all leaf nodes. Copy information to the extent records.
1da177e4
LT
4098 */
4099 for (;;) {
a6f64d4a 4100 xfs_bmbt_rec_t *frp;
1da177e4
LT
4101 xfs_fsblock_t nextbno;
4102 xfs_extnum_t num_recs;
4eea22f0 4103 xfs_extnum_t start;
1da177e4 4104
7cc95a82 4105 num_recs = xfs_btree_get_numrecs(block);
1da177e4
LT
4106 if (unlikely(i + num_recs > room)) {
4107 ASSERT(i + num_recs <= room);
65333b4c 4108 xfs_warn(ip->i_mount,
3762ec6b 4109 "corrupt dinode %Lu, (btree extents).",
1da177e4 4110 (unsigned long long) ip->i_ino);
65333b4c
DC
4111 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
4112 XFS_ERRLEVEL_LOW, ip->i_mount, block);
1da177e4
LT
4113 goto error0;
4114 }
4115 XFS_WANT_CORRUPTED_GOTO(
4e8938fe 4116 xfs_bmap_sanity_check(mp, bp, 0),
1da177e4
LT
4117 error0);
4118 /*
4119 * Read-ahead the next leaf block, if any.
4120 */
7cc95a82 4121 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
1da177e4
LT
4122 if (nextbno != NULLFSBLOCK)
4123 xfs_btree_reada_bufl(mp, nextbno, 1);
4124 /*
4eea22f0 4125 * Copy records into the extent records.
1da177e4 4126 */
136341b4 4127 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
4eea22f0
MK
4128 start = i;
4129 for (j = 0; j < num_recs; j++, i++, frp++) {
a6f64d4a 4130 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
cd8b0a97
CH
4131 trp->l0 = be64_to_cpu(frp->l0);
4132 trp->l1 = be64_to_cpu(frp->l1);
1da177e4
LT
4133 }
4134 if (exntf == XFS_EXTFMT_NOSTATE) {
4135 /*
4136 * Check all attribute bmap btree records and
4137 * any "older" data bmap btree records for a
4138 * set bit in the "extent flag" position.
4139 */
4eea22f0
MK
4140 if (unlikely(xfs_check_nostate_extents(ifp,
4141 start, num_recs))) {
1da177e4
LT
4142 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
4143 XFS_ERRLEVEL_LOW,
4144 ip->i_mount);
4145 goto error0;
4146 }
4147 }
1da177e4
LT
4148 xfs_trans_brelse(tp, bp);
4149 bno = nextbno;
4150 /*
4151 * If we've reached the end, stop.
4152 */
4153 if (bno == NULLFSBLOCK)
4154 break;
4155 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4156 XFS_BMAP_BTREE_REF)))
4157 return error;
7cc95a82 4158 block = XFS_BUF_TO_BLOCK(bp);
1da177e4 4159 }
4eea22f0 4160 ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
1da177e4 4161 ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
3a59c94c 4162 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
1da177e4
LT
4163 return 0;
4164error0:
4165 xfs_trans_brelse(tp, bp);
4166 return XFS_ERROR(EFSCORRUPTED);
4167}
4168
0b1b213f 4169#ifdef DEBUG
1da177e4 4170/*
4eea22f0 4171 * Add bmap trace insert entries for all the contents of the extent records.
1da177e4
LT
4172 */
4173void
4174xfs_bmap_trace_exlist(
1da177e4
LT
4175 xfs_inode_t *ip, /* incore inode pointer */
4176 xfs_extnum_t cnt, /* count of entries in the list */
0b1b213f
CH
4177 int whichfork, /* data or attr fork */
4178 unsigned long caller_ip)
1da177e4 4179{
4eea22f0 4180 xfs_extnum_t idx; /* extent record index */
1da177e4 4181 xfs_ifork_t *ifp; /* inode fork pointer */
0b1b213f
CH
4182 int state = 0;
4183
4184 if (whichfork == XFS_ATTR_FORK)
4185 state |= BMAP_ATTRFORK;
1da177e4
LT
4186
4187 ifp = XFS_IFORK_PTR(ip, whichfork);
4eea22f0 4188 ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
0b1b213f
CH
4189 for (idx = 0; idx < cnt; idx++)
4190 trace_xfs_extlist(ip, idx, whichfork, caller_ip);
1da177e4 4191}
1da177e4 4192
1da177e4
LT
4193/*
4194 * Validate that the bmbt_irecs being returned from bmapi are valid
4195 * given the callers original parameters. Specifically check the
4196 * ranges of the returned irecs to ensure that they only extent beyond
4197 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
4198 */
4199STATIC void
4200xfs_bmap_validate_ret(
4201 xfs_fileoff_t bno,
4202 xfs_filblks_t len,
4203 int flags,
4204 xfs_bmbt_irec_t *mval,
4205 int nmap,
4206 int ret_nmap)
4207{
4208 int i; /* index to map values */
4209
4210 ASSERT(ret_nmap <= nmap);
4211
4212 for (i = 0; i < ret_nmap; i++) {
4213 ASSERT(mval[i].br_blockcount > 0);
4214 if (!(flags & XFS_BMAPI_ENTIRE)) {
4215 ASSERT(mval[i].br_startoff >= bno);
4216 ASSERT(mval[i].br_blockcount <= len);
4217 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
4218 bno + len);
4219 } else {
4220 ASSERT(mval[i].br_startoff < bno + len);
4221 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
4222 bno);
4223 }
4224 ASSERT(i == 0 ||
4225 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
4226 mval[i].br_startoff);
c0dc7828
DC
4227 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
4228 mval[i].br_startblock != HOLESTARTBLOCK);
1da177e4
LT
4229 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
4230 mval[i].br_state == XFS_EXT_UNWRITTEN);
4231 }
4232}
4233#endif /* DEBUG */
4234
4235
aef9a895
DC
4236/*
4237 * Trim the returned map to the required bounds
4238 */
4239STATIC void
4240xfs_bmapi_trim_map(
4241 struct xfs_bmbt_irec *mval,
4242 struct xfs_bmbt_irec *got,
4243 xfs_fileoff_t *bno,
4244 xfs_filblks_t len,
4245 xfs_fileoff_t obno,
4246 xfs_fileoff_t end,
4247 int n,
4248 int flags)
4249{
4250 if ((flags & XFS_BMAPI_ENTIRE) ||
4251 got->br_startoff + got->br_blockcount <= obno) {
4252 *mval = *got;
4253 if (isnullstartblock(got->br_startblock))
4254 mval->br_startblock = DELAYSTARTBLOCK;
4255 return;
4256 }
4257
4258 if (obno > *bno)
4259 *bno = obno;
4260 ASSERT((*bno >= obno) || (n == 0));
4261 ASSERT(*bno < end);
4262 mval->br_startoff = *bno;
4263 if (isnullstartblock(got->br_startblock))
4264 mval->br_startblock = DELAYSTARTBLOCK;
4265 else
4266 mval->br_startblock = got->br_startblock +
4267 (*bno - got->br_startoff);
4268 /*
4269 * Return the minimum of what we got and what we asked for for
4270 * the length. We can use the len variable here because it is
4271 * modified below and we could have been there before coming
4272 * here if the first part of the allocation didn't overlap what
4273 * was asked for.
4274 */
4275 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
4276 got->br_blockcount - (*bno - got->br_startoff));
4277 mval->br_state = got->br_state;
4278 ASSERT(mval->br_blockcount <= len);
4279 return;
4280}
4281
4282/*
4283 * Update and validate the extent map to return
4284 */
4285STATIC void
4286xfs_bmapi_update_map(
4287 struct xfs_bmbt_irec **map,
4288 xfs_fileoff_t *bno,
4289 xfs_filblks_t *len,
4290 xfs_fileoff_t obno,
4291 xfs_fileoff_t end,
4292 int *n,
4293 int flags)
4294{
4295 xfs_bmbt_irec_t *mval = *map;
4296
4297 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
4298 ((mval->br_startoff + mval->br_blockcount) <= end));
4299 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
4300 (mval->br_startoff < obno));
4301
4302 *bno = mval->br_startoff + mval->br_blockcount;
4303 *len = end - *bno;
4304 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
4305 /* update previous map with new information */
4306 ASSERT(mval->br_startblock == mval[-1].br_startblock);
4307 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
4308 ASSERT(mval->br_state == mval[-1].br_state);
4309 mval[-1].br_blockcount = mval->br_blockcount;
4310 mval[-1].br_state = mval->br_state;
4311 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
4312 mval[-1].br_startblock != DELAYSTARTBLOCK &&
4313 mval[-1].br_startblock != HOLESTARTBLOCK &&
4314 mval->br_startblock == mval[-1].br_startblock +
4315 mval[-1].br_blockcount &&
4316 ((flags & XFS_BMAPI_IGSTATE) ||
4317 mval[-1].br_state == mval->br_state)) {
4318 ASSERT(mval->br_startoff ==
4319 mval[-1].br_startoff + mval[-1].br_blockcount);
4320 mval[-1].br_blockcount += mval->br_blockcount;
4321 } else if (*n > 0 &&
4322 mval->br_startblock == DELAYSTARTBLOCK &&
4323 mval[-1].br_startblock == DELAYSTARTBLOCK &&
4324 mval->br_startoff ==
4325 mval[-1].br_startoff + mval[-1].br_blockcount) {
4326 mval[-1].br_blockcount += mval->br_blockcount;
4327 mval[-1].br_state = mval->br_state;
4328 } else if (!((*n == 0) &&
4329 ((mval->br_startoff + mval->br_blockcount) <=
4330 obno))) {
4331 mval++;
4332 (*n)++;
4333 }
4334 *map = mval;
4335}
4336
5c8ed202
DC
4337/*
4338 * Map file blocks to filesystem blocks without allocation.
4339 */
4340int
4341xfs_bmapi_read(
4342 struct xfs_inode *ip,
4343 xfs_fileoff_t bno,
4344 xfs_filblks_t len,
4345 struct xfs_bmbt_irec *mval,
4346 int *nmap,
4347 int flags)
4348{
4349 struct xfs_mount *mp = ip->i_mount;
4350 struct xfs_ifork *ifp;
4351 struct xfs_bmbt_irec got;
4352 struct xfs_bmbt_irec prev;
4353 xfs_fileoff_t obno;
4354 xfs_fileoff_t end;
4355 xfs_extnum_t lastx;
4356 int error;
4357 int eof;
4358 int n = 0;
4359 int whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4360 XFS_ATTR_FORK : XFS_DATA_FORK;
4361
4362 ASSERT(*nmap >= 1);
4363 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
4364 XFS_BMAPI_IGSTATE)));
4365
4366 if (unlikely(XFS_TEST_ERROR(
4367 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4368 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
4369 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4370 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
4371 return XFS_ERROR(EFSCORRUPTED);
4372 }
4373
4374 if (XFS_FORCED_SHUTDOWN(mp))
4375 return XFS_ERROR(EIO);
4376
4377 XFS_STATS_INC(xs_blk_mapr);
4378
4379 ifp = XFS_IFORK_PTR(ip, whichfork);
4380 ASSERT(ifp->if_ext_max ==
4381 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
4382
4383 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4384 error = xfs_iread_extents(NULL, ip, whichfork);
4385 if (error)
4386 return error;
4387 }
4388
4389 xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, &prev);
4390 end = bno + len;
4391 obno = bno;
4392
4393 while (bno < end && n < *nmap) {
4394 /* Reading past eof, act as though there's a hole up to end. */
4395 if (eof)
4396 got.br_startoff = end;
4397 if (got.br_startoff > bno) {
4398 /* Reading in a hole. */
4399 mval->br_startoff = bno;
4400 mval->br_startblock = HOLESTARTBLOCK;
4401 mval->br_blockcount =
4402 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
4403 mval->br_state = XFS_EXT_NORM;
4404 bno += mval->br_blockcount;
4405 len -= mval->br_blockcount;
4406 mval++;
4407 n++;
4408 continue;
4409 }
4410
4411 /* set up the extent map to return. */
4412 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4413 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4414
4415 /* If we're done, stop now. */
4416 if (bno >= end || n >= *nmap)
4417 break;
4418
4419 /* Else go on to the next record. */
4420 if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t))
4421 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got);
4422 else
4423 eof = 1;
4424 }
4425 *nmap = n;
4426 return 0;
4427}
4428
b64dfe4e
CH
4429STATIC int
4430xfs_bmapi_reserve_delalloc(
4431 struct xfs_inode *ip,
4432 xfs_fileoff_t aoff,
4433 xfs_filblks_t len,
4434 struct xfs_bmbt_irec *got,
4435 struct xfs_bmbt_irec *prev,
4436 xfs_extnum_t *lastx,
4437 int eof)
4438{
4439 struct xfs_mount *mp = ip->i_mount;
4440 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4441 xfs_extlen_t alen;
4442 xfs_extlen_t indlen;
b64dfe4e
CH
4443 char rt = XFS_IS_REALTIME_INODE(ip);
4444 xfs_extlen_t extsz;
4445 int error;
4446
4447 alen = XFS_FILBLKS_MIN(len, MAXEXTLEN);
4448 if (!eof)
4449 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
4450
4451 /* Figure out the extent size, adjust alen */
4452 extsz = xfs_get_extsz_hint(ip);
4453 if (extsz) {
4454 /*
4455 * Make sure we don't exceed a single extent length when we
4456 * align the extent by reducing length we are going to
4457 * allocate by the maximum amount extent size aligment may
4458 * require.
4459 */
4460 alen = XFS_FILBLKS_MIN(len, MAXEXTLEN - (2 * extsz - 1));
4461 error = xfs_bmap_extsize_align(mp, got, prev, extsz, rt, eof,
4462 1, 0, &aoff, &alen);
4463 ASSERT(!error);
4464 }
4465
4466 if (rt)
4467 extsz = alen / mp->m_sb.sb_rextsize;
4468
4469 /*
4470 * Make a transaction-less quota reservation for delayed allocation
4471 * blocks. This number gets adjusted later. We return if we haven't
4472 * allocated blocks already inside this loop.
4473 */
4474 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
4475 rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4476 if (error)
4477 return error;
4478
4479 /*
4480 * Split changing sb for alen and indlen since they could be coming
4481 * from different places.
4482 */
4483 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
4484 ASSERT(indlen > 0);
4485
4486 if (rt) {
4487 error = xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
4488 -((int64_t)extsz), 0);
4489 } else {
4490 error = xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
4491 -((int64_t)alen), 0);
4492 }
4493
4494 if (error)
4495 goto out_unreserve_quota;
4496
4497 error = xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
4498 -((int64_t)indlen), 0);
4499 if (error)
4500 goto out_unreserve_blocks;
4501
4502
4503 ip->i_delayed_blks += alen;
4504
4505 got->br_startoff = aoff;
4506 got->br_startblock = nullstartblock(indlen);
4507 got->br_blockcount = alen;
4508 got->br_state = XFS_EXT_NORM;
1fd044d9 4509 xfs_bmap_add_extent_hole_delay(ip, lastx, got);
b64dfe4e
CH
4510
4511 /*
1fd044d9
CH
4512 * Update our extent pointer, given that xfs_bmap_add_extent_hole_delay
4513 * might have merged it into one of the neighbouring ones.
b64dfe4e
CH
4514 */
4515 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *lastx), got);
4516
4517 ASSERT(got->br_startoff <= aoff);
4518 ASSERT(got->br_startoff + got->br_blockcount >= aoff + alen);
4519 ASSERT(isnullstartblock(got->br_startblock));
4520 ASSERT(got->br_state == XFS_EXT_NORM);
4521 return 0;
4522
4523out_unreserve_blocks:
4524 if (rt)
4525 xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS, extsz, 0);
4526 else
4527 xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS, alen, 0);
4528out_unreserve_quota:
4529 if (XFS_IS_QUOTA_ON(mp))
4530 xfs_trans_unreserve_quota_nblks(NULL, ip, alen, 0, rt ?
4531 XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4532 return error;
4533}
4534
4403280a
CH
4535/*
4536 * Map file blocks to filesystem blocks, adding delayed allocations as needed.
4537 */
4538int
4539xfs_bmapi_delay(
4540 struct xfs_inode *ip, /* incore inode */
4541 xfs_fileoff_t bno, /* starting file offs. mapped */
4542 xfs_filblks_t len, /* length to map in file */
4543 struct xfs_bmbt_irec *mval, /* output: map values */
4544 int *nmap, /* i/o: mval size/count */
4545 int flags) /* XFS_BMAPI_... */
4546{
4547 struct xfs_mount *mp = ip->i_mount;
4548 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4549 struct xfs_bmbt_irec got; /* current file extent record */
4550 struct xfs_bmbt_irec prev; /* previous file extent record */
4551 xfs_fileoff_t obno; /* old block number (offset) */
4552 xfs_fileoff_t end; /* end of mapped file region */
4553 xfs_extnum_t lastx; /* last useful extent number */
4554 int eof; /* we've hit the end of extents */
4555 int n = 0; /* current extent index */
4556 int error = 0;
4557
4558 ASSERT(*nmap >= 1);
4559 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4560 ASSERT(!(flags & ~XFS_BMAPI_ENTIRE));
4561
4562 if (unlikely(XFS_TEST_ERROR(
4563 (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS &&
4564 XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE),
4565 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4566 XFS_ERROR_REPORT("xfs_bmapi_delay", XFS_ERRLEVEL_LOW, mp);
4567 return XFS_ERROR(EFSCORRUPTED);
4568 }
4569
4570 if (XFS_FORCED_SHUTDOWN(mp))
4571 return XFS_ERROR(EIO);
4572
4573 XFS_STATS_INC(xs_blk_mapw);
4574
4575 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4576 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
4577 if (error)
4578 return error;
4579 }
4580
4581 xfs_bmap_search_extents(ip, bno, XFS_DATA_FORK, &eof, &lastx, &got, &prev);
4582 end = bno + len;
4583 obno = bno;
4584
4585 while (bno < end && n < *nmap) {
4586 if (eof || got.br_startoff > bno) {
4587 error = xfs_bmapi_reserve_delalloc(ip, bno, len, &got,
4588 &prev, &lastx, eof);
4589 if (error) {
4590 if (n == 0) {
4591 *nmap = 0;
4592 return error;
4593 }
4594 break;
4595 }
4596 }
4597
4598 /* set up the extent map to return. */
4599 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4600 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4601
4602 /* If we're done, stop now. */
4603 if (bno >= end || n >= *nmap)
4604 break;
4605
4606 /* Else go on to the next record. */
4607 prev = got;
4608 if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t))
4609 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got);
4610 else
4611 eof = 1;
4612 }
4613
4614 *nmap = n;
4615 return 0;
4616}
4617
4618
7e47a4ef
DC
4619STATIC int
4620xfs_bmapi_allocate(
4621 struct xfs_bmalloca *bma,
c315c90b 4622 int flags)
7e47a4ef
DC
4623{
4624 struct xfs_mount *mp = bma->ip->i_mount;
4625 int whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4626 XFS_ATTR_FORK : XFS_DATA_FORK;
4627 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
c315c90b 4628 int tmp_logflags = 0;
7e47a4ef
DC
4629 int error;
4630 int rt;
4631
4632 rt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(bma->ip);
4633
4634 /*
4635 * For the wasdelay case, we could also just allocate the stuff asked
4636 * for in this bmap call but that wouldn't be as good.
4637 */
4638 if (bma->wasdel) {
963c30cf
DC
4639 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4640 bma->offset = bma->got.br_startoff;
e0c3da5d
DC
4641 if (bma->idx != NULLEXTNUM && bma->idx) {
4642 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1),
baf41a52 4643 &bma->prev);
7e47a4ef
DC
4644 }
4645 } else {
963c30cf 4646 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
7e47a4ef 4647 if (!bma->eof)
963c30cf 4648 bma->length = XFS_FILBLKS_MIN(bma->length,
3a75667e 4649 bma->got.br_startoff - bma->offset);
7e47a4ef
DC
4650 }
4651
4652 /*
4653 * Indicate if this is the first user data in the file, or just any
4654 * user data.
4655 */
4656 if (!(flags & XFS_BMAPI_METADATA)) {
963c30cf 4657 bma->userdata = (bma->offset == 0) ?
7e47a4ef
DC
4658 XFS_ALLOC_INITIAL_USER_DATA : XFS_ALLOC_USERDATA;
4659 }
4660
963c30cf 4661 bma->minlen = (flags & XFS_BMAPI_CONTIG) ? bma->length : 1;
7e47a4ef
DC
4662
4663 /*
4664 * Only want to do the alignment at the eof if it is userdata and
4665 * allocation length is larger than a stripe unit.
4666 */
963c30cf 4667 if (mp->m_dalign && bma->length >= mp->m_dalign &&
7e47a4ef 4668 !(flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
1b16447b 4669 error = xfs_bmap_isaeof(bma, whichfork);
7e47a4ef
DC
4670 if (error)
4671 return error;
4672 }
4673
4674 error = xfs_bmap_alloc(bma);
4675 if (error)
4676 return error;
4677
0937e0fd
DC
4678 if (bma->flist->xbf_low)
4679 bma->minleft = 0;
29c8d17a
DC
4680 if (bma->cur)
4681 bma->cur->bc_private.b.firstblock = *bma->firstblock;
963c30cf 4682 if (bma->blkno == NULLFSBLOCK)
7e47a4ef 4683 return 0;
29c8d17a
DC
4684 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4685 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4686 bma->cur->bc_private.b.firstblock = *bma->firstblock;
4687 bma->cur->bc_private.b.flist = bma->flist;
7e47a4ef
DC
4688 }
4689 /*
4690 * Bump the number of extents we've allocated
4691 * in this call.
4692 */
e0c3da5d 4693 bma->nallocs++;
7e47a4ef 4694
29c8d17a
DC
4695 if (bma->cur)
4696 bma->cur->bc_private.b.flags =
7e47a4ef
DC
4697 bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
4698
963c30cf
DC
4699 bma->got.br_startoff = bma->offset;
4700 bma->got.br_startblock = bma->blkno;
4701 bma->got.br_blockcount = bma->length;
baf41a52 4702 bma->got.br_state = XFS_EXT_NORM;
7e47a4ef
DC
4703
4704 /*
4705 * A wasdelay extent has been initialized, so shouldn't be flagged
4706 * as unwritten.
4707 */
4708 if (!bma->wasdel && (flags & XFS_BMAPI_PREALLOC) &&
4709 xfs_sb_version_hasextflgbit(&mp->m_sb))
baf41a52 4710 bma->got.br_state = XFS_EXT_UNWRITTEN;
7e47a4ef 4711
a5bd606b 4712 if (bma->wasdel) {
572a4cf0 4713 error = xfs_bmap_add_extent_delay_real(bma);
a5bd606b 4714 } else {
e0c3da5d
DC
4715 error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip,
4716 &bma->idx, &bma->cur, &bma->got,
c315c90b 4717 bma->firstblock, bma->flist, &tmp_logflags,
e0c3da5d 4718 whichfork);
a5bd606b
CH
4719 }
4720
c315c90b 4721 bma->logflags |= tmp_logflags;
7e47a4ef
DC
4722 if (error)
4723 return error;
4724
4725 /*
a5bd606b
CH
4726 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4727 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4728 * the neighbouring ones.
7e47a4ef 4729 */
e0c3da5d 4730 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
7e47a4ef 4731
963c30cf
DC
4732 ASSERT(bma->got.br_startoff <= bma->offset);
4733 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4734 bma->offset + bma->length);
baf41a52
DC
4735 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4736 bma->got.br_state == XFS_EXT_UNWRITTEN);
7e47a4ef
DC
4737 return 0;
4738}
4739
b447fe5a
DC
4740STATIC int
4741xfs_bmapi_convert_unwritten(
4742 struct xfs_bmalloca *bma,
4743 struct xfs_bmbt_irec *mval,
4744 xfs_filblks_t len,
c315c90b 4745 int flags)
b447fe5a
DC
4746{
4747 int whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4748 XFS_ATTR_FORK : XFS_DATA_FORK;
4749 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
c315c90b 4750 int tmp_logflags = 0;
b447fe5a
DC
4751 int error;
4752
b447fe5a
DC
4753 /* check if we need to do unwritten->real conversion */
4754 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4755 (flags & XFS_BMAPI_PREALLOC))
4756 return 0;
4757
4758 /* check if we need to do real->unwritten conversion */
4759 if (mval->br_state == XFS_EXT_NORM &&
4760 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4761 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4762 return 0;
4763
4764 /*
4765 * Modify (by adding) the state flag, if writing.
4766 */
4767 ASSERT(mval->br_blockcount <= len);
29c8d17a
DC
4768 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4769 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
b447fe5a 4770 bma->ip, whichfork);
29c8d17a
DC
4771 bma->cur->bc_private.b.firstblock = *bma->firstblock;
4772 bma->cur->bc_private.b.flist = bma->flist;
b447fe5a
DC
4773 }
4774 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4775 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4776
e0c3da5d 4777 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, &bma->idx,
c315c90b
CH
4778 &bma->cur, mval, bma->firstblock, bma->flist,
4779 &tmp_logflags);
4780 bma->logflags |= tmp_logflags;
b447fe5a
DC
4781 if (error)
4782 return error;
4783
4784 /*
a5bd606b
CH
4785 * Update our extent pointer, given that
4786 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4787 * of the neighbouring ones.
b447fe5a 4788 */
e0c3da5d 4789 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
b447fe5a
DC
4790
4791 /*
4792 * We may have combined previously unwritten space with written space,
4793 * so generate another request.
4794 */
4795 if (mval->br_blockcount < len)
4796 return EAGAIN;
4797 return 0;
4798}
4799
1da177e4 4800/*
c0dc7828
DC
4801 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4802 * extent state if necessary. Details behaviour is controlled by the flags
4803 * parameter. Only allocates blocks from a single allocation group, to avoid
4804 * locking problems.
4805 *
1da177e4
LT
4806 * The returned value in "firstblock" from the first call in a transaction
4807 * must be remembered and presented to subsequent calls in "firstblock".
4808 * An upper bound for the number of blocks to be allocated is supplied to
4809 * the first call in "total"; if no allocation group has that many free
4810 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4811 */
c0dc7828
DC
4812int
4813xfs_bmapi_write(
4814 struct xfs_trans *tp, /* transaction pointer */
4815 struct xfs_inode *ip, /* incore inode */
4816 xfs_fileoff_t bno, /* starting file offs. mapped */
4817 xfs_filblks_t len, /* length to map in file */
4818 int flags, /* XFS_BMAPI_... */
4819 xfs_fsblock_t *firstblock, /* first allocated block
4820 controls a.g. for allocs */
4821 xfs_extlen_t total, /* total blocks needed */
4822 struct xfs_bmbt_irec *mval, /* output: map values */
4823 int *nmap, /* i/o: mval size/count */
4824 struct xfs_bmap_free *flist) /* i/o: list extents to free */
1da177e4 4825{
c0dc7828
DC
4826 struct xfs_mount *mp = ip->i_mount;
4827 struct xfs_ifork *ifp;
4828 struct xfs_bmalloca bma = { 0 }; /* args for xfs_bmap_alloc */
c0dc7828
DC
4829 xfs_fileoff_t end; /* end of mapped file region */
4830 int eof; /* after the end of extents */
4831 int error; /* error return */
c0dc7828 4832 int n; /* current extent index */
c0dc7828 4833 xfs_fileoff_t obno; /* old block number (offset) */
c0dc7828
DC
4834 int whichfork; /* data or attr fork */
4835 char inhole; /* current location is hole in file */
4836 char wasdelay; /* old extent was delayed */
4837
1da177e4 4838#ifdef DEBUG
c0dc7828
DC
4839 xfs_fileoff_t orig_bno; /* original block number value */
4840 int orig_flags; /* original flags arg value */
4841 xfs_filblks_t orig_len; /* original value of len arg */
4842 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4843 int orig_nmap; /* original value of *nmap */
1da177e4
LT
4844
4845 orig_bno = bno;
4846 orig_len = len;
4847 orig_flags = flags;
4848 orig_mval = mval;
4849 orig_nmap = *nmap;
4850#endif
c0dc7828 4851
1da177e4 4852 ASSERT(*nmap >= 1);
c0dc7828
DC
4853 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4854 ASSERT(!(flags & XFS_BMAPI_IGSTATE));
4855 ASSERT(tp != NULL);
4856
1da177e4
LT
4857 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4858 XFS_ATTR_FORK : XFS_DATA_FORK;
c0dc7828 4859
1da177e4
LT
4860 if (unlikely(XFS_TEST_ERROR(
4861 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4862 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4863 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL),
4864 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
c0dc7828 4865 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
1da177e4
LT
4866 return XFS_ERROR(EFSCORRUPTED);
4867 }
c0dc7828 4868
1da177e4
LT
4869 if (XFS_FORCED_SHUTDOWN(mp))
4870 return XFS_ERROR(EIO);
c0dc7828 4871
1da177e4
LT
4872 ifp = XFS_IFORK_PTR(ip, whichfork);
4873 ASSERT(ifp->if_ext_max ==
4874 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
c0dc7828
DC
4875
4876 XFS_STATS_INC(xs_blk_mapw);
4877
1da177e4 4878 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
c0dc7828 4879 error = xfs_bmap_local_to_extents(tp, ip, firstblock, total,
c315c90b 4880 &bma.logflags, whichfork);
c0dc7828 4881 if (error)
1da177e4
LT
4882 goto error0;
4883 }
c0dc7828
DC
4884
4885 if (*firstblock == NULLFSBLOCK) {
1da177e4 4886 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
0937e0fd 4887 bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
1da177e4 4888 else
0937e0fd 4889 bma.minleft = 1;
c0dc7828 4890 } else {
0937e0fd 4891 bma.minleft = 0;
c0dc7828
DC
4892 }
4893
4894 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4895 error = xfs_iread_extents(tp, ip, whichfork);
4896 if (error)
4897 goto error0;
4898 }
4899
e0c3da5d 4900 xfs_bmap_search_extents(ip, bno, whichfork, &eof, &bma.idx, &bma.got,
baf41a52 4901 &bma.prev);
1da177e4
LT
4902 n = 0;
4903 end = bno + len;
4904 obno = bno;
7e47a4ef
DC
4905
4906 bma.tp = tp;
4907 bma.ip = ip;
7e47a4ef
DC
4908 bma.total = total;
4909 bma.userdata = 0;
0937e0fd
DC
4910 bma.flist = flist;
4911 bma.firstblock = firstblock;
b4e9181e 4912
1da177e4 4913 while (bno < end && n < *nmap) {
baf41a52
DC
4914 inhole = eof || bma.got.br_startoff > bno;
4915 wasdelay = !inhole && isnullstartblock(bma.got.br_startblock);
c0dc7828 4916
1da177e4
LT
4917 /*
4918 * First, deal with the hole before the allocated space
4919 * that we found, if any.
4920 */
c0dc7828 4921 if (inhole || wasdelay) {
7e47a4ef
DC
4922 bma.eof = eof;
4923 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4924 bma.wasdel = wasdelay;
3a75667e
DC
4925 bma.length = len;
4926 bma.offset = bno;
7e47a4ef 4927
c315c90b 4928 error = xfs_bmapi_allocate(&bma, flags);
1da177e4
LT
4929 if (error)
4930 goto error0;
3a75667e 4931 if (bma.blkno == NULLFSBLOCK)
7e47a4ef 4932 break;
1da177e4 4933 }
4403280a 4934
aef9a895 4935 /* Deal with the allocated space we found. */
baf41a52
DC
4936 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4937 end, n, flags);
1da177e4 4938
b447fe5a 4939 /* Execute unwritten extent conversion if necessary */
c315c90b 4940 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
c0dc7828
DC
4941 if (error == EAGAIN)
4942 continue;
4943 if (error)
4944 goto error0;
1da177e4 4945
aef9a895
DC
4946 /* update the extent map to return */
4947 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4948
1da177e4
LT
4949 /*
4950 * If we're done, stop now. Stop when we've allocated
4951 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4952 * the transaction may get too big.
4953 */
e0c3da5d 4954 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
1da177e4 4955 break;
c0dc7828
DC
4956
4957 /* Else go on to the next record. */
baf41a52 4958 bma.prev = bma.got;
e0c3da5d
DC
4959 if (++bma.idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t)) {
4960 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma.idx),
4961 &bma.got);
4962 } else
5690f921 4963 eof = 1;
1da177e4 4964 }
1da177e4 4965 *nmap = n;
c0dc7828 4966
1da177e4
LT
4967 /*
4968 * Transform from btree to extents, give it cur.
4969 */
c0dc7828 4970 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
1da177e4 4971 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
c315c90b
CH
4972 int tmp_logflags = 0;
4973
29c8d17a
DC
4974 ASSERT(bma.cur);
4975 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur,
1da177e4 4976 &tmp_logflags, whichfork);
c315c90b 4977 bma.logflags |= tmp_logflags;
1da177e4
LT
4978 if (error)
4979 goto error0;
4980 }
4981 ASSERT(ifp->if_ext_max ==
4982 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
4983 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
4984 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max);
4985 error = 0;
1da177e4
LT
4986error0:
4987 /*
4988 * Log everything. Do this after conversion, there's no point in
4eea22f0 4989 * logging the extent records if we've converted to btree format.
1da177e4 4990 */
c315c90b 4991 if ((bma.logflags & xfs_ilog_fext(whichfork)) &&
1da177e4 4992 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
c315c90b
CH
4993 bma.logflags &= ~xfs_ilog_fext(whichfork);
4994 else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) &&
1da177e4 4995 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
c315c90b 4996 bma.logflags &= ~xfs_ilog_fbroot(whichfork);
1da177e4
LT
4997 /*
4998 * Log whatever the flags say, even if error. Otherwise we might miss
4999 * detecting a case where the data is changed, there's an error,
5000 * and it's not logged so we don't shutdown when we should.
5001 */
c315c90b
CH
5002 if (bma.logflags)
5003 xfs_trans_log_inode(tp, ip, bma.logflags);
c0dc7828 5004
29c8d17a 5005 if (bma.cur) {
1da177e4
LT
5006 if (!error) {
5007 ASSERT(*firstblock == NULLFSBLOCK ||
5008 XFS_FSB_TO_AGNO(mp, *firstblock) ==
5009 XFS_FSB_TO_AGNO(mp,
29c8d17a 5010 bma.cur->bc_private.b.firstblock) ||
1da177e4
LT
5011 (flist->xbf_low &&
5012 XFS_FSB_TO_AGNO(mp, *firstblock) <
5013 XFS_FSB_TO_AGNO(mp,
29c8d17a
DC
5014 bma.cur->bc_private.b.firstblock)));
5015 *firstblock = bma.cur->bc_private.b.firstblock;
1da177e4 5016 }
29c8d17a 5017 xfs_btree_del_cursor(bma.cur,
1da177e4
LT
5018 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5019 }
5020 if (!error)
5021 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
5022 orig_nmap, *nmap);
5023 return error;
5024}
5025
1da177e4
LT
5026/*
5027 * Unmap (remove) blocks from a file.
5028 * If nexts is nonzero then the number of extents to remove is limited to
5029 * that value. If not all extents in the block range can be removed then
5030 * *done is set.
5031 */
5032int /* error */
5033xfs_bunmapi(
5034 xfs_trans_t *tp, /* transaction pointer */
5035 struct xfs_inode *ip, /* incore inode */
5036 xfs_fileoff_t bno, /* starting offset to unmap */
5037 xfs_filblks_t len, /* length to unmap in file */
5038 int flags, /* misc flags */
5039 xfs_extnum_t nexts, /* number of extents max */
5040 xfs_fsblock_t *firstblock, /* first allocated block
5041 controls a.g. for allocs */
5042 xfs_bmap_free_t *flist, /* i/o: list extents to free */
5043 int *done) /* set if not done yet */
5044{
5045 xfs_btree_cur_t *cur; /* bmap btree cursor */
5046 xfs_bmbt_irec_t del; /* extent being deleted */
5047 int eof; /* is deleting at eof */
a6f64d4a 5048 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
1da177e4
LT
5049 int error; /* error return value */
5050 xfs_extnum_t extno; /* extent number in list */
4eea22f0 5051 xfs_bmbt_irec_t got; /* current extent record */
1da177e4
LT
5052 xfs_ifork_t *ifp; /* inode fork pointer */
5053 int isrt; /* freeing in rt area */
5054 xfs_extnum_t lastx; /* last extent index used */
5055 int logflags; /* transaction logging flags */
5056 xfs_extlen_t mod; /* rt extent offset */
5057 xfs_mount_t *mp; /* mount structure */
4eea22f0
MK
5058 xfs_extnum_t nextents; /* number of file extents */
5059 xfs_bmbt_irec_t prev; /* previous extent record */
1da177e4
LT
5060 xfs_fileoff_t start; /* first file offset deleted */
5061 int tmp_logflags; /* partial logging flags */
5062 int wasdel; /* was a delayed alloc extent */
5063 int whichfork; /* data or attribute fork */
1da177e4
LT
5064 xfs_fsblock_t sum;
5065
0b1b213f
CH
5066 trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_);
5067
1da177e4
LT
5068 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
5069 XFS_ATTR_FORK : XFS_DATA_FORK;
5070 ifp = XFS_IFORK_PTR(ip, whichfork);
5071 if (unlikely(
5072 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5073 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5074 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5075 ip->i_mount);
5076 return XFS_ERROR(EFSCORRUPTED);
5077 }
5078 mp = ip->i_mount;
5079 if (XFS_FORCED_SHUTDOWN(mp))
5080 return XFS_ERROR(EIO);
54893273 5081
1da177e4
LT
5082 ASSERT(len > 0);
5083 ASSERT(nexts >= 0);
5084 ASSERT(ifp->if_ext_max ==
5085 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5086 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5087 (error = xfs_iread_extents(tp, ip, whichfork)))
5088 return error;
5089 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5090 if (nextents == 0) {
5091 *done = 1;
5092 return 0;
5093 }
5094 XFS_STATS_INC(xs_blk_unmap);
dd9f438e 5095 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
1da177e4
LT
5096 start = bno;
5097 bno = start + len - 1;
5098 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5099 &prev);
b4e9181e 5100
1da177e4
LT
5101 /*
5102 * Check to see if the given block number is past the end of the
5103 * file, back up to the last block if so...
5104 */
5105 if (eof) {
4eea22f0 5106 ep = xfs_iext_get_ext(ifp, --lastx);
1da177e4
LT
5107 xfs_bmbt_get_all(ep, &got);
5108 bno = got.br_startoff + got.br_blockcount - 1;
5109 }
5110 logflags = 0;
5111 if (ifp->if_flags & XFS_IFBROOT) {
5112 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
561f7d17 5113 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
1da177e4
LT
5114 cur->bc_private.b.firstblock = *firstblock;
5115 cur->bc_private.b.flist = flist;
5116 cur->bc_private.b.flags = 0;
5117 } else
5118 cur = NULL;
5119 extno = 0;
5120 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5121 (nexts == 0 || extno < nexts)) {
5122 /*
5123 * Is the found extent after a hole in which bno lives?
5124 * Just back up to the previous extent, if so.
5125 */
5126 if (got.br_startoff > bno) {
5127 if (--lastx < 0)
5128 break;
4eea22f0 5129 ep = xfs_iext_get_ext(ifp, lastx);
1da177e4
LT
5130 xfs_bmbt_get_all(ep, &got);
5131 }
5132 /*
5133 * Is the last block of this extent before the range
5134 * we're supposed to delete? If so, we're done.
5135 */
5136 bno = XFS_FILEOFF_MIN(bno,
5137 got.br_startoff + got.br_blockcount - 1);
5138 if (bno < start)
5139 break;
5140 /*
5141 * Then deal with the (possibly delayed) allocated space
5142 * we found.
5143 */
5144 ASSERT(ep != NULL);
5145 del = got;
9d87c319 5146 wasdel = isnullstartblock(del.br_startblock);
1da177e4
LT
5147 if (got.br_startoff < start) {
5148 del.br_startoff = start;
5149 del.br_blockcount -= start - got.br_startoff;
5150 if (!wasdel)
5151 del.br_startblock += start - got.br_startoff;
5152 }
5153 if (del.br_startoff + del.br_blockcount > bno + 1)
5154 del.br_blockcount = bno + 1 - del.br_startoff;
5155 sum = del.br_startblock + del.br_blockcount;
5156 if (isrt &&
5157 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5158 /*
5159 * Realtime extent not lined up at the end.
5160 * The extent could have been split into written
5161 * and unwritten pieces, or we could just be
5162 * unmapping part of it. But we can't really
5163 * get rid of part of a realtime extent.
5164 */
5165 if (del.br_state == XFS_EXT_UNWRITTEN ||
62118709 5166 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
1da177e4
LT
5167 /*
5168 * This piece is unwritten, or we're not
5169 * using unwritten extents. Skip over it.
5170 */
5171 ASSERT(bno >= mod);
5172 bno -= mod > del.br_blockcount ?
5173 del.br_blockcount : mod;
5174 if (bno < got.br_startoff) {
5175 if (--lastx >= 0)
4eea22f0
MK
5176 xfs_bmbt_get_all(xfs_iext_get_ext(
5177 ifp, lastx), &got);
1da177e4
LT
5178 }
5179 continue;
5180 }
5181 /*
5182 * It's written, turn it unwritten.
5183 * This is better than zeroing it.
5184 */
5185 ASSERT(del.br_state == XFS_EXT_NORM);
5186 ASSERT(xfs_trans_get_block_res(tp) > 0);
5187 /*
5188 * If this spans a realtime extent boundary,
5189 * chop it back to the start of the one we end at.
5190 */
5191 if (del.br_blockcount > mod) {
5192 del.br_startoff += del.br_blockcount - mod;
5193 del.br_startblock += del.br_blockcount - mod;
5194 del.br_blockcount = mod;
5195 }
5196 del.br_state = XFS_EXT_UNWRITTEN;
a5bd606b
CH
5197 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
5198 &lastx, &cur, &del, firstblock, flist,
5199 &logflags);
1da177e4
LT
5200 if (error)
5201 goto error0;
5202 goto nodelete;
5203 }
5204 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5205 /*
5206 * Realtime extent is lined up at the end but not
5207 * at the front. We'll get rid of full extents if
5208 * we can.
5209 */
5210 mod = mp->m_sb.sb_rextsize - mod;
5211 if (del.br_blockcount > mod) {
5212 del.br_blockcount -= mod;
5213 del.br_startoff += mod;
5214 del.br_startblock += mod;
5215 } else if ((del.br_startoff == start &&
5216 (del.br_state == XFS_EXT_UNWRITTEN ||
5217 xfs_trans_get_block_res(tp) == 0)) ||
62118709 5218 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
1da177e4
LT
5219 /*
5220 * Can't make it unwritten. There isn't
5221 * a full extent here so just skip it.
5222 */
5223 ASSERT(bno >= del.br_blockcount);
5224 bno -= del.br_blockcount;
f1c63b73
CH
5225 if (got.br_startoff > bno) {
5226 if (--lastx >= 0) {
5227 ep = xfs_iext_get_ext(ifp,
5228 lastx);
5229 xfs_bmbt_get_all(ep, &got);
5230 }
1da177e4
LT
5231 }
5232 continue;
5233 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5234 /*
5235 * This one is already unwritten.
5236 * It must have a written left neighbor.
5237 * Unwrite the killed part of that one and
5238 * try again.
5239 */
5240 ASSERT(lastx > 0);
4eea22f0
MK
5241 xfs_bmbt_get_all(xfs_iext_get_ext(ifp,
5242 lastx - 1), &prev);
1da177e4 5243 ASSERT(prev.br_state == XFS_EXT_NORM);
9d87c319 5244 ASSERT(!isnullstartblock(prev.br_startblock));
1da177e4
LT
5245 ASSERT(del.br_startblock ==
5246 prev.br_startblock + prev.br_blockcount);
5247 if (prev.br_startoff < start) {
5248 mod = start - prev.br_startoff;
5249 prev.br_blockcount -= mod;
5250 prev.br_startblock += mod;
5251 prev.br_startoff = start;
5252 }
5253 prev.br_state = XFS_EXT_UNWRITTEN;
ec90c556 5254 lastx--;
a5bd606b
CH
5255 error = xfs_bmap_add_extent_unwritten_real(tp,
5256 ip, &lastx, &cur, &prev,
5257 firstblock, flist, &logflags);
1da177e4
LT
5258 if (error)
5259 goto error0;
5260 goto nodelete;
5261 } else {
5262 ASSERT(del.br_state == XFS_EXT_NORM);
5263 del.br_state = XFS_EXT_UNWRITTEN;
a5bd606b
CH
5264 error = xfs_bmap_add_extent_unwritten_real(tp,
5265 ip, &lastx, &cur, &del,
5266 firstblock, flist, &logflags);
1da177e4
LT
5267 if (error)
5268 goto error0;
5269 goto nodelete;
5270 }
5271 }
5272 if (wasdel) {
9d87c319 5273 ASSERT(startblockval(del.br_startblock) > 0);
dd9f438e 5274 /* Update realtime/data freespace, unreserve quota */
06d10dd9
NS
5275 if (isrt) {
5276 xfs_filblks_t rtexts;
5277
5278 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5279 do_div(rtexts, mp->m_sb.sb_rextsize);
5280 xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
54893273 5281 (int64_t)rtexts, 0);
7d095257
CH
5282 (void)xfs_trans_reserve_quota_nblks(NULL,
5283 ip, -((long)del.br_blockcount), 0,
06d10dd9
NS
5284 XFS_QMOPT_RES_RTBLKS);
5285 } else {
96540c78 5286 xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
54893273 5287 (int64_t)del.br_blockcount, 0);
7d095257
CH
5288 (void)xfs_trans_reserve_quota_nblks(NULL,
5289 ip, -((long)del.br_blockcount), 0,
1da177e4 5290 XFS_QMOPT_RES_REGBLKS);
06d10dd9 5291 }
1da177e4
LT
5292 ip->i_delayed_blks -= del.br_blockcount;
5293 if (cur)
5294 cur->bc_private.b.flags |=
5295 XFS_BTCUR_BPRV_WASDEL;
5296 } else if (cur)
5297 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5298 /*
5299 * If it's the case where the directory code is running
5300 * with no block reservation, and the deleted block is in
5301 * the middle of its extent, and the resulting insert
5302 * of an extent would cause transformation to btree format,
5303 * then reject it. The calling code will then swap
5304 * blocks around instead.
5305 * We have to do this now, rather than waiting for the
5306 * conversion to btree format, since the transaction
5307 * will be dirty.
5308 */
5309 if (!wasdel && xfs_trans_get_block_res(tp) == 0 &&
5310 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5311 XFS_IFORK_NEXTENTS(ip, whichfork) >= ifp->if_ext_max &&
5312 del.br_startoff > got.br_startoff &&
5313 del.br_startoff + del.br_blockcount <
5314 got.br_startoff + got.br_blockcount) {
5315 error = XFS_ERROR(ENOSPC);
5316 goto error0;
5317 }
ec90c556 5318 error = xfs_bmap_del_extent(ip, tp, &lastx, flist, cur, &del,
54893273 5319 &tmp_logflags, whichfork);
1da177e4
LT
5320 logflags |= tmp_logflags;
5321 if (error)
5322 goto error0;
5323 bno = del.br_startoff - 1;
5324nodelete:
1da177e4
LT
5325 /*
5326 * If not done go on to the next (previous) record.
1da177e4 5327 */
1da177e4 5328 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
00239acf
CH
5329 if (lastx >= 0) {
5330 ep = xfs_iext_get_ext(ifp, lastx);
5331 if (xfs_bmbt_get_startoff(ep) > bno) {
5332 if (--lastx >= 0)
5333 ep = xfs_iext_get_ext(ifp,
5334 lastx);
5335 }
1da177e4 5336 xfs_bmbt_get_all(ep, &got);
00239acf 5337 }
1da177e4
LT
5338 extno++;
5339 }
5340 }
1da177e4
LT
5341 *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
5342 ASSERT(ifp->if_ext_max ==
5343 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5344 /*
5345 * Convert to a btree if necessary.
5346 */
5347 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5348 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
5349 ASSERT(cur == NULL);
5350 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist,
5351 &cur, 0, &tmp_logflags, whichfork);
5352 logflags |= tmp_logflags;
5353 if (error)
5354 goto error0;
5355 }
5356 /*
5357 * transform from btree to extents, give it cur
5358 */
5359 else if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
5360 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
5361 ASSERT(cur != NULL);
5362 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5363 whichfork);
5364 logflags |= tmp_logflags;
5365 if (error)
5366 goto error0;
5367 }
5368 /*
5369 * transform from extents to local?
5370 */
5371 ASSERT(ifp->if_ext_max ==
5372 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5373 error = 0;
5374error0:
5375 /*
5376 * Log everything. Do this after conversion, there's no point in
4eea22f0 5377 * logging the extent records if we've converted to btree format.
1da177e4 5378 */
9d87c319 5379 if ((logflags & xfs_ilog_fext(whichfork)) &&
1da177e4 5380 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
9d87c319
ES
5381 logflags &= ~xfs_ilog_fext(whichfork);
5382 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
1da177e4 5383 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
9d87c319 5384 logflags &= ~xfs_ilog_fbroot(whichfork);
1da177e4
LT
5385 /*
5386 * Log inode even in the error case, if the transaction
5387 * is dirty we'll need to shut down the filesystem.
5388 */
5389 if (logflags)
5390 xfs_trans_log_inode(tp, ip, logflags);
5391 if (cur) {
5392 if (!error) {
5393 *firstblock = cur->bc_private.b.firstblock;
5394 cur->bc_private.b.allocated = 0;
5395 }
5396 xfs_btree_del_cursor(cur,
5397 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5398 }
5399 return error;
5400}
5401
3bacbcd8
VA
5402/*
5403 * returns 1 for success, 0 if we failed to map the extent.
5404 */
5405STATIC int
5406xfs_getbmapx_fix_eof_hole(
5407 xfs_inode_t *ip, /* xfs incore inode pointer */
8a7141a8 5408 struct getbmapx *out, /* output structure */
3bacbcd8 5409 int prealloced, /* this is a file with
8a7141a8 5410 * preallocated data space */
3bacbcd8
VA
5411 __int64_t end, /* last block requested */
5412 xfs_fsblock_t startblock)
5413{
5414 __int64_t fixlen;
5415 xfs_mount_t *mp; /* file system mount point */
5af317c9
ES
5416 xfs_ifork_t *ifp; /* inode fork pointer */
5417 xfs_extnum_t lastx; /* last extent pointer */
5418 xfs_fileoff_t fileblock;
3bacbcd8
VA
5419
5420 if (startblock == HOLESTARTBLOCK) {
5421 mp = ip->i_mount;
5422 out->bmv_block = -1;
5423 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, ip->i_size));
5424 fixlen -= out->bmv_offset;
5425 if (prealloced && out->bmv_offset + out->bmv_length == end) {
5426 /* Came to hole at EOF. Trim it. */
5427 if (fixlen <= 0)
5428 return 0;
5429 out->bmv_length = fixlen;
5430 }
5431 } else {
5af317c9
ES
5432 if (startblock == DELAYSTARTBLOCK)
5433 out->bmv_block = -2;
5434 else
9d87c319 5435 out->bmv_block = xfs_fsb_to_db(ip, startblock);
5af317c9
ES
5436 fileblock = XFS_BB_TO_FSB(ip->i_mount, out->bmv_offset);
5437 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
5438 if (xfs_iext_bno_to_ext(ifp, fileblock, &lastx) &&
5439 (lastx == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))-1))
5440 out->bmv_oflags |= BMV_OF_LAST;
3bacbcd8
VA
5441 }
5442
5443 return 1;
5444}
5445
1da177e4 5446/*
8a7141a8
ES
5447 * Get inode's extents as described in bmv, and format for output.
5448 * Calls formatter to fill the user's buffer until all extents
5449 * are mapped, until the passed-in bmv->bmv_count slots have
5450 * been filled, or until the formatter short-circuits the loop,
5451 * if it is tracking filled-in extents on its own.
1da177e4
LT
5452 */
5453int /* error code */
5454xfs_getbmap(
993386c1 5455 xfs_inode_t *ip,
8a7141a8
ES
5456 struct getbmapx *bmv, /* user bmap structure */
5457 xfs_bmap_format_t formatter, /* format to user */
5458 void *arg) /* formatter arg */
1da177e4
LT
5459{
5460 __int64_t bmvend; /* last block requested */
4be4a00f 5461 int error = 0; /* return value */
1da177e4
LT
5462 __int64_t fixlen; /* length for -1 case */
5463 int i; /* extent number */
1da177e4
LT
5464 int lock; /* lock state */
5465 xfs_bmbt_irec_t *map; /* buffer for user's data */
5466 xfs_mount_t *mp; /* file system mount point */
5467 int nex; /* # of user extents can do */
5468 int nexleft; /* # of user extents left */
5469 int subnex; /* # of bmapi's can do */
5470 int nmap; /* number of map entries */
6321e3ed 5471 struct getbmapx *out; /* output structure */
1da177e4
LT
5472 int whichfork; /* data or attr fork */
5473 int prealloced; /* this is a file with
5474 * preallocated data space */
8a7141a8 5475 int iflags; /* interface flags */
1da177e4 5476 int bmapi_flags; /* flags for xfs_bmapi */
6321e3ed 5477 int cur_ext = 0;
1da177e4 5478
1da177e4 5479 mp = ip->i_mount;
8a7141a8 5480 iflags = bmv->bmv_iflags;
8a7141a8 5481 whichfork = iflags & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK;
1da177e4 5482
1da177e4
LT
5483 if (whichfork == XFS_ATTR_FORK) {
5484 if (XFS_IFORK_Q(ip)) {
5485 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS &&
5486 ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE &&
5487 ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
5488 return XFS_ERROR(EINVAL);
5489 } else if (unlikely(
5490 ip->i_d.di_aformat != 0 &&
5491 ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) {
5492 XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW,
5493 ip->i_mount);
5494 return XFS_ERROR(EFSCORRUPTED);
5495 }
4be4a00f
CH
5496
5497 prealloced = 0;
5498 fixlen = 1LL << 32;
5499 } else {
4be4a00f
CH
5500 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
5501 ip->i_d.di_format != XFS_DINODE_FMT_BTREE &&
5502 ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
5503 return XFS_ERROR(EINVAL);
5504
957d0ebe 5505 if (xfs_get_extsz_hint(ip) ||
dd9f438e 5506 ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){
1da177e4
LT
5507 prealloced = 1;
5508 fixlen = XFS_MAXIOFFSET(mp);
5509 } else {
5510 prealloced = 0;
ba87ea69 5511 fixlen = ip->i_size;
1da177e4 5512 }
1da177e4
LT
5513 }
5514
5515 if (bmv->bmv_length == -1) {
5516 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen));
4be4a00f
CH
5517 bmv->bmv_length =
5518 max_t(__int64_t, fixlen - bmv->bmv_offset, 0);
5519 } else if (bmv->bmv_length == 0) {
1da177e4
LT
5520 bmv->bmv_entries = 0;
5521 return 0;
4be4a00f
CH
5522 } else if (bmv->bmv_length < 0) {
5523 return XFS_ERROR(EINVAL);
1da177e4 5524 }
4be4a00f 5525
1da177e4
LT
5526 nex = bmv->bmv_count - 1;
5527 if (nex <= 0)
5528 return XFS_ERROR(EINVAL);
5529 bmvend = bmv->bmv_offset + bmv->bmv_length;
5530
6321e3ed
CH
5531
5532 if (bmv->bmv_count > ULONG_MAX / sizeof(struct getbmapx))
5533 return XFS_ERROR(ENOMEM);
5534 out = kmem_zalloc(bmv->bmv_count * sizeof(struct getbmapx), KM_MAYFAIL);
5535 if (!out)
5536 return XFS_ERROR(ENOMEM);
5537
1da177e4 5538 xfs_ilock(ip, XFS_IOLOCK_SHARED);
4be4a00f
CH
5539 if (whichfork == XFS_DATA_FORK && !(iflags & BMV_IF_DELALLOC)) {
5540 if (ip->i_delayed_blks || ip->i_size > ip->i_d.di_size) {
5541 error = xfs_flush_pages(ip, 0, -1, 0, FI_REMAPF);
5542 if (error)
5543 goto out_unlock_iolock;
e12070a5 5544 }
309c8480
DC
5545 /*
5546 * even after flushing the inode, there can still be delalloc
5547 * blocks on the inode beyond EOF due to speculative
5548 * preallocation. These are not removed until the release
5549 * function is called or the inode is inactivated. Hence we
5550 * cannot assert here that ip->i_delayed_blks == 0.
5551 */
4be4a00f 5552 }
1da177e4
LT
5553
5554 lock = xfs_ilock_map_shared(ip);
5555
5556 /*
5557 * Don't let nex be bigger than the number of extents
5558 * we can have assuming alternating holes and real extents.
5559 */
5560 if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1)
5561 nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1;
5562
4be4a00f
CH
5563 bmapi_flags = xfs_bmapi_aflag(whichfork);
5564 if (!(iflags & BMV_IF_PREALLOC))
5565 bmapi_flags |= XFS_BMAPI_IGSTATE;
1da177e4
LT
5566
5567 /*
5568 * Allocate enough space to handle "subnex" maps at a time.
5569 */
4be4a00f 5570 error = ENOMEM;
1da177e4 5571 subnex = 16;
ca35dcd6 5572 map = kmem_alloc(subnex * sizeof(*map), KM_MAYFAIL | KM_NOFS);
4be4a00f
CH
5573 if (!map)
5574 goto out_unlock_ilock;
1da177e4
LT
5575
5576 bmv->bmv_entries = 0;
5577
4be4a00f
CH
5578 if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0 &&
5579 (whichfork == XFS_ATTR_FORK || !(iflags & BMV_IF_DELALLOC))) {
5580 error = 0;
5581 goto out_free_map;
1da177e4
LT
5582 }
5583
5584 nexleft = nex;
5585
5586 do {
5587 nmap = (nexleft > subnex) ? subnex : nexleft;
5c8ed202
DC
5588 error = xfs_bmapi_read(ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset),
5589 XFS_BB_TO_FSB(mp, bmv->bmv_length),
5590 map, &nmap, bmapi_flags);
1da177e4 5591 if (error)
4be4a00f 5592 goto out_free_map;
1da177e4
LT
5593 ASSERT(nmap <= subnex);
5594
5595 for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) {
6321e3ed 5596 out[cur_ext].bmv_oflags = 0;
5af317c9 5597 if (map[i].br_state == XFS_EXT_UNWRITTEN)
6321e3ed 5598 out[cur_ext].bmv_oflags |= BMV_OF_PREALLOC;
5af317c9 5599 else if (map[i].br_startblock == DELAYSTARTBLOCK)
6321e3ed
CH
5600 out[cur_ext].bmv_oflags |= BMV_OF_DELALLOC;
5601 out[cur_ext].bmv_offset =
5602 XFS_FSB_TO_BB(mp, map[i].br_startoff);
5603 out[cur_ext].bmv_length =
5604 XFS_FSB_TO_BB(mp, map[i].br_blockcount);
5605 out[cur_ext].bmv_unused1 = 0;
5606 out[cur_ext].bmv_unused2 = 0;
5af317c9
ES
5607 ASSERT(((iflags & BMV_IF_DELALLOC) != 0) ||
5608 (map[i].br_startblock != DELAYSTARTBLOCK));
9af0a70c 5609 if (map[i].br_startblock == HOLESTARTBLOCK &&
3bacbcd8
VA
5610 whichfork == XFS_ATTR_FORK) {
5611 /* came to the end of attribute fork */
6321e3ed 5612 out[cur_ext].bmv_oflags |= BMV_OF_LAST;
4be4a00f 5613 goto out_free_map;
1da177e4 5614 }
4be4a00f 5615
6321e3ed
CH
5616 if (!xfs_getbmapx_fix_eof_hole(ip, &out[cur_ext],
5617 prealloced, bmvend,
5618 map[i].br_startblock))
4be4a00f
CH
5619 goto out_free_map;
5620
4be4a00f 5621 bmv->bmv_offset =
6321e3ed
CH
5622 out[cur_ext].bmv_offset +
5623 out[cur_ext].bmv_length;
4be4a00f
CH
5624 bmv->bmv_length =
5625 max_t(__int64_t, 0, bmvend - bmv->bmv_offset);
9af25465
TM
5626
5627 /*
5628 * In case we don't want to return the hole,
5629 * don't increase cur_ext so that we can reuse
5630 * it in the next loop.
5631 */
5632 if ((iflags & BMV_IF_NO_HOLES) &&
5633 map[i].br_startblock == HOLESTARTBLOCK) {
5634 memset(&out[cur_ext], 0, sizeof(out[cur_ext]));
5635 continue;
5636 }
5637
5638 nexleft--;
4be4a00f 5639 bmv->bmv_entries++;
6321e3ed 5640 cur_ext++;
1da177e4
LT
5641 }
5642 } while (nmap && nexleft && bmv->bmv_length);
5643
4be4a00f
CH
5644 out_free_map:
5645 kmem_free(map);
5646 out_unlock_ilock:
1da177e4 5647 xfs_iunlock_map_shared(ip, lock);
4be4a00f 5648 out_unlock_iolock:
1da177e4 5649 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
6321e3ed
CH
5650
5651 for (i = 0; i < cur_ext; i++) {
5652 int full = 0; /* user array is full */
5653
5654 /* format results & advance arg */
5655 error = formatter(&arg, &out[i], &full);
5656 if (error || full)
5657 break;
5658 }
5659
7747a0b0 5660 kmem_free(out);
1da177e4
LT
5661 return error;
5662}
5663
1da177e4 5664#ifdef DEBUG
ecd7f082 5665STATIC struct xfs_buf *
1da177e4 5666xfs_bmap_get_bp(
ecd7f082 5667 struct xfs_btree_cur *cur,
1da177e4
LT
5668 xfs_fsblock_t bno)
5669{
ecd7f082
CH
5670 struct xfs_log_item_desc *lidp;
5671 int i;
1da177e4
LT
5672
5673 if (!cur)
ecd7f082
CH
5674 return NULL;
5675
5676 for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
5677 if (!cur->bc_bufs[i])
5678 break;
5679 if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno)
5680 return cur->bc_bufs[i];
1da177e4 5681 }
1da177e4 5682
ecd7f082
CH
5683 /* Chase down all the log items to see if the bp is there */
5684 list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) {
e98c414f 5685 struct xfs_buf_log_item *bip;
ecd7f082
CH
5686 bip = (struct xfs_buf_log_item *)lidp->lid_item;
5687 if (bip->bli_item.li_type == XFS_LI_BUF &&
5688 XFS_BUF_ADDR(bip->bli_buf) == bno)
5689 return bip->bli_buf;
1da177e4 5690 }
e98c414f 5691
ecd7f082 5692 return NULL;
1da177e4
LT
5693}
5694
3180e66d 5695STATIC void
1da177e4 5696xfs_check_block(
7cc95a82 5697 struct xfs_btree_block *block,
1da177e4
LT
5698 xfs_mount_t *mp,
5699 int root,
5700 short sz)
5701{
5702 int i, j, dmxr;
576039cf 5703 __be64 *pp, *thispa; /* pointer to block address */
1da177e4
LT
5704 xfs_bmbt_key_t *prevp, *keyp;
5705
16259e7d 5706 ASSERT(be16_to_cpu(block->bb_level) > 0);
1da177e4
LT
5707
5708 prevp = NULL;
7cc95a82 5709 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
1da177e4 5710 dmxr = mp->m_bmap_dmxr[0];
136341b4 5711 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
1da177e4
LT
5712
5713 if (prevp) {
4a26e66e
CH
5714 ASSERT(be64_to_cpu(prevp->br_startoff) <
5715 be64_to_cpu(keyp->br_startoff));
1da177e4
LT
5716 }
5717 prevp = keyp;
5718
5719 /*
5720 * Compare the block numbers to see if there are dups.
5721 */
136341b4 5722 if (root)
60197e8d 5723 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
136341b4
CH
5724 else
5725 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
5726
16259e7d 5727 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
136341b4 5728 if (root)
60197e8d 5729 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
136341b4
CH
5730 else
5731 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
576039cf 5732 if (*thispa == *pp) {
0b932ccc 5733 xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld",
34a622b2 5734 __func__, j, i,
576039cf 5735 (unsigned long long)be64_to_cpu(*thispa));
1da177e4 5736 panic("%s: ptrs are equal in node\n",
34a622b2 5737 __func__);
1da177e4
LT
5738 }
5739 }
5740 }
5741}
5742
5743/*
5744 * Check that the extents for the inode ip are in the right order in all
5745 * btree leaves.
5746 */
5747
5748STATIC void
5749xfs_bmap_check_leaf_extents(
5750 xfs_btree_cur_t *cur, /* btree cursor or null */
5751 xfs_inode_t *ip, /* incore inode pointer */
5752 int whichfork) /* data or attr fork */
5753{
7cc95a82 5754 struct xfs_btree_block *block; /* current btree block */
1da177e4
LT
5755 xfs_fsblock_t bno; /* block # of "block" */
5756 xfs_buf_t *bp; /* buffer for "block" */
5757 int error; /* error return value */
4eea22f0 5758 xfs_extnum_t i=0, j; /* index into the extents list */
1da177e4
LT
5759 xfs_ifork_t *ifp; /* fork structure */
5760 int level; /* btree level, for checking */
5761 xfs_mount_t *mp; /* file system mount structure */
576039cf 5762 __be64 *pp; /* pointer to block address */
4eea22f0 5763 xfs_bmbt_rec_t *ep; /* pointer to current extent */
2abdb8c8 5764 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
4eea22f0 5765 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
1da177e4
LT
5766 int bp_release = 0;
5767
5768 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
5769 return;
5770 }
5771
5772 bno = NULLFSBLOCK;
5773 mp = ip->i_mount;
5774 ifp = XFS_IFORK_PTR(ip, whichfork);
5775 block = ifp->if_broot;
5776 /*
5777 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
5778 */
16259e7d
CH
5779 level = be16_to_cpu(block->bb_level);
5780 ASSERT(level > 0);
1da177e4 5781 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
60197e8d 5782 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
576039cf
CH
5783 bno = be64_to_cpu(*pp);
5784
5785 ASSERT(bno != NULLDFSBNO);
5786 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
5787 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
5788
1da177e4
LT
5789 /*
5790 * Go down the tree until leaf level is reached, following the first
5791 * pointer (leftmost) at each level.
5792 */
5793 while (level-- > 0) {
5794 /* See if buf is in cur first */
5795 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
5796 if (bp) {
5797 bp_release = 0;
5798 } else {
5799 bp_release = 1;
5800 }
5801 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
5802 XFS_BMAP_BTREE_REF)))
5803 goto error_norelse;
7cc95a82 5804 block = XFS_BUF_TO_BLOCK(bp);
1da177e4 5805 XFS_WANT_CORRUPTED_GOTO(
4e8938fe 5806 xfs_bmap_sanity_check(mp, bp, level),
1da177e4
LT
5807 error0);
5808 if (level == 0)
5809 break;
5810
5811 /*
5812 * Check this block for basic sanity (increasing keys and
5813 * no duplicate blocks).
5814 */
5815
5816 xfs_check_block(block, mp, 0, 0);
136341b4 5817 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
576039cf
CH
5818 bno = be64_to_cpu(*pp);
5819 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
1da177e4
LT
5820 if (bp_release) {
5821 bp_release = 0;
5822 xfs_trans_brelse(NULL, bp);
5823 }
5824 }
5825
5826 /*
5827 * Here with bp and block set to the leftmost leaf node in the tree.
5828 */
5829 i = 0;
5830
5831 /*
5832 * Loop over all leaf nodes checking that all extents are in the right order.
5833 */
1da177e4 5834 for (;;) {
1da177e4
LT
5835 xfs_fsblock_t nextbno;
5836 xfs_extnum_t num_recs;
5837
5838
7cc95a82 5839 num_recs = xfs_btree_get_numrecs(block);
1da177e4
LT
5840
5841 /*
5842 * Read-ahead the next leaf block, if any.
5843 */
5844
7cc95a82 5845 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
1da177e4
LT
5846
5847 /*
5848 * Check all the extents to make sure they are OK.
5849 * If we had a previous block, the last entry should
5850 * conform with the first entry in this one.
5851 */
5852
136341b4 5853 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
2abdb8c8 5854 if (i) {
4a26e66e
CH
5855 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
5856 xfs_bmbt_disk_get_blockcount(&last) <=
5857 xfs_bmbt_disk_get_startoff(ep));
2abdb8c8 5858 }
4eea22f0 5859 for (j = 1; j < num_recs; j++) {
136341b4 5860 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
4a26e66e
CH
5861 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
5862 xfs_bmbt_disk_get_blockcount(ep) <=
5863 xfs_bmbt_disk_get_startoff(nextp));
4eea22f0 5864 ep = nextp;
1da177e4 5865 }
1da177e4 5866
2abdb8c8 5867 last = *ep;
1da177e4
LT
5868 i += num_recs;
5869 if (bp_release) {
5870 bp_release = 0;
5871 xfs_trans_brelse(NULL, bp);
5872 }
5873 bno = nextbno;
5874 /*
5875 * If we've reached the end, stop.
5876 */
5877 if (bno == NULLFSBLOCK)
5878 break;
5879
5880 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
5881 if (bp) {
5882 bp_release = 0;
5883 } else {
5884 bp_release = 1;
5885 }
5886 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
5887 XFS_BMAP_BTREE_REF)))
5888 goto error_norelse;
7cc95a82 5889 block = XFS_BUF_TO_BLOCK(bp);
1da177e4
LT
5890 }
5891 if (bp_release) {
5892 bp_release = 0;
5893 xfs_trans_brelse(NULL, bp);
5894 }
5895 return;
5896
5897error0:
0b932ccc 5898 xfs_warn(mp, "%s: at error0", __func__);
1da177e4
LT
5899 if (bp_release)
5900 xfs_trans_brelse(NULL, bp);
5901error_norelse:
0b932ccc 5902 xfs_warn(mp, "%s: BAD after btree leaves for %d extents",
34a622b2
HH
5903 __func__, i);
5904 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
1da177e4
LT
5905 return;
5906}
5907#endif
5908
5909/*
5910 * Count fsblocks of the given fork.
5911 */
5912int /* error */
5913xfs_bmap_count_blocks(
5914 xfs_trans_t *tp, /* transaction pointer */
5915 xfs_inode_t *ip, /* incore inode */
5916 int whichfork, /* data or attr fork */
5917 int *count) /* out: count of blocks */
5918{
7cc95a82 5919 struct xfs_btree_block *block; /* current btree block */
1da177e4
LT
5920 xfs_fsblock_t bno; /* block # of "block" */
5921 xfs_ifork_t *ifp; /* fork structure */
5922 int level; /* btree level, for checking */
5923 xfs_mount_t *mp; /* file system mount structure */
576039cf 5924 __be64 *pp; /* pointer to block address */
1da177e4
LT
5925
5926 bno = NULLFSBLOCK;
5927 mp = ip->i_mount;
5928 ifp = XFS_IFORK_PTR(ip, whichfork);
5929 if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) {
c94312de 5930 xfs_bmap_count_leaves(ifp, 0,
1da177e4 5931 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t),
c94312de 5932 count);
1da177e4
LT
5933 return 0;
5934 }
5935
5936 /*
5937 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
5938 */
5939 block = ifp->if_broot;
16259e7d
CH
5940 level = be16_to_cpu(block->bb_level);
5941 ASSERT(level > 0);
60197e8d 5942 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
576039cf
CH
5943 bno = be64_to_cpu(*pp);
5944 ASSERT(bno != NULLDFSBNO);
5945 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
5946 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
1da177e4 5947
4eea22f0 5948 if (unlikely(xfs_bmap_count_tree(mp, tp, ifp, bno, level, count) < 0)) {
1da177e4
LT
5949 XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW,
5950 mp);
5951 return XFS_ERROR(EFSCORRUPTED);
5952 }
5953
5954 return 0;
5955}
5956
5957/*
5958 * Recursively walks each level of a btree
5959 * to count total fsblocks is use.
5960 */
a8272ce0 5961STATIC int /* error */
1da177e4
LT
5962xfs_bmap_count_tree(
5963 xfs_mount_t *mp, /* file system mount point */
5964 xfs_trans_t *tp, /* transaction pointer */
4eea22f0 5965 xfs_ifork_t *ifp, /* inode fork pointer */
1da177e4
LT
5966 xfs_fsblock_t blockno, /* file system block number */
5967 int levelin, /* level in btree */
5968 int *count) /* Count of blocks */
5969{
5970 int error;
5971 xfs_buf_t *bp, *nbp;
5972 int level = levelin;
576039cf 5973 __be64 *pp;
1da177e4
LT
5974 xfs_fsblock_t bno = blockno;
5975 xfs_fsblock_t nextbno;
7cc95a82 5976 struct xfs_btree_block *block, *nextblock;
1da177e4 5977 int numrecs;
1da177e4
LT
5978
5979 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF)))
5980 return error;
5981 *count += 1;
7cc95a82 5982 block = XFS_BUF_TO_BLOCK(bp);
1da177e4
LT
5983
5984 if (--level) {
9da096fd 5985 /* Not at node above leaves, count this level of nodes */
7cc95a82 5986 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
1da177e4
LT
5987 while (nextbno != NULLFSBLOCK) {
5988 if ((error = xfs_btree_read_bufl(mp, tp, nextbno,
5989 0, &nbp, XFS_BMAP_BTREE_REF)))
5990 return error;
5991 *count += 1;
7cc95a82
CH
5992 nextblock = XFS_BUF_TO_BLOCK(nbp);
5993 nextbno = be64_to_cpu(nextblock->bb_u.l.bb_rightsib);
1da177e4
LT
5994 xfs_trans_brelse(tp, nbp);
5995 }
5996
5997 /* Dive to the next level */
136341b4 5998 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
576039cf 5999 bno = be64_to_cpu(*pp);
1da177e4 6000 if (unlikely((error =
4eea22f0 6001 xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) {
1da177e4
LT
6002 xfs_trans_brelse(tp, bp);
6003 XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
6004 XFS_ERRLEVEL_LOW, mp);
6005 return XFS_ERROR(EFSCORRUPTED);
6006 }
6007 xfs_trans_brelse(tp, bp);
6008 } else {
6009 /* count all level 1 nodes and their leaves */
6010 for (;;) {
7cc95a82 6011 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
16259e7d 6012 numrecs = be16_to_cpu(block->bb_numrecs);
136341b4 6013 xfs_bmap_disk_count_leaves(mp, block, numrecs, count);
1da177e4
LT
6014 xfs_trans_brelse(tp, bp);
6015 if (nextbno == NULLFSBLOCK)
6016 break;
6017 bno = nextbno;
6018 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
6019 XFS_BMAP_BTREE_REF)))
6020 return error;
6021 *count += 1;
7cc95a82 6022 block = XFS_BUF_TO_BLOCK(bp);
1da177e4
LT
6023 }
6024 }
6025 return 0;
6026}
6027
6028/*
4eea22f0 6029 * Count leaf blocks given a range of extent records.
1da177e4 6030 */
c94312de 6031STATIC void
1da177e4 6032xfs_bmap_count_leaves(
4eea22f0
MK
6033 xfs_ifork_t *ifp,
6034 xfs_extnum_t idx,
1da177e4
LT
6035 int numrecs,
6036 int *count)
6037{
6038 int b;
6039
4eea22f0 6040 for (b = 0; b < numrecs; b++) {
a6f64d4a 6041 xfs_bmbt_rec_host_t *frp = xfs_iext_get_ext(ifp, idx + b);
91e11088 6042 *count += xfs_bmbt_get_blockcount(frp);
4eea22f0 6043 }
91e11088
YL
6044}
6045
6046/*
4eea22f0
MK
6047 * Count leaf blocks given a range of extent records originally
6048 * in btree format.
91e11088 6049 */
c94312de 6050STATIC void
91e11088 6051xfs_bmap_disk_count_leaves(
136341b4 6052 struct xfs_mount *mp,
7cc95a82 6053 struct xfs_btree_block *block,
91e11088
YL
6054 int numrecs,
6055 int *count)
6056{
6057 int b;
4eea22f0 6058 xfs_bmbt_rec_t *frp;
91e11088 6059
4eea22f0 6060 for (b = 1; b <= numrecs; b++) {
136341b4 6061 frp = XFS_BMBT_REC_ADDR(mp, block, b);
1da177e4 6062 *count += xfs_bmbt_disk_get_blockcount(frp);
4eea22f0 6063 }
1da177e4 6064}
c726de44
DC
6065
6066/*
6067 * dead simple method of punching delalyed allocation blocks from a range in
6068 * the inode. Walks a block at a time so will be slow, but is only executed in
6069 * rare error cases so the overhead is not critical. This will alays punch out
6070 * both the start and end blocks, even if the ranges only partially overlap
6071 * them, so it is up to the caller to ensure that partial blocks are not
6072 * passed in.
6073 */
6074int
6075xfs_bmap_punch_delalloc_range(
6076 struct xfs_inode *ip,
6077 xfs_fileoff_t start_fsb,
6078 xfs_fileoff_t length)
6079{
6080 xfs_fileoff_t remaining = length;
6081 int error = 0;
6082
6083 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
6084
6085 do {
6086 int done;
6087 xfs_bmbt_irec_t imap;
6088 int nimaps = 1;
6089 xfs_fsblock_t firstblock;
6090 xfs_bmap_free_t flist;
6091
6092 /*
6093 * Map the range first and check that it is a delalloc extent
6094 * before trying to unmap the range. Otherwise we will be
6095 * trying to remove a real extent (which requires a
6096 * transaction) or a hole, which is probably a bad idea...
6097 */
5c8ed202
DC
6098 error = xfs_bmapi_read(ip, start_fsb, 1, &imap, &nimaps,
6099 XFS_BMAPI_ENTIRE);
c726de44
DC
6100
6101 if (error) {
6102 /* something screwed, just bail */
6103 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
53487786 6104 xfs_alert(ip->i_mount,
c726de44
DC
6105 "Failed delalloc mapping lookup ino %lld fsb %lld.",
6106 ip->i_ino, start_fsb);
6107 }
6108 break;
6109 }
6110 if (!nimaps) {
6111 /* nothing there */
6112 goto next_block;
6113 }
6114 if (imap.br_startblock != DELAYSTARTBLOCK) {
6115 /* been converted, ignore */
6116 goto next_block;
6117 }
6118 WARN_ON(imap.br_blockcount == 0);
6119
6120 /*
6121 * Note: while we initialise the firstblock/flist pair, they
6122 * should never be used because blocks should never be
6123 * allocated or freed for a delalloc extent and hence we need
6124 * don't cancel or finish them after the xfs_bunmapi() call.
6125 */
6126 xfs_bmap_init(&flist, &firstblock);
6127 error = xfs_bunmapi(NULL, ip, start_fsb, 1, 0, 1, &firstblock,
6128 &flist, &done);
6129 if (error)
6130 break;
6131
6132 ASSERT(!flist.xbf_count && !flist.xbf_first);
6133next_block:
6134 start_fsb++;
6135 remaining--;
6136 } while(remaining > 0);
6137
6138 return error;
6139}