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