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