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