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