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