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