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