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