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