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