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