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