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