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