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