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