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