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