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_format.h"
21#include "xfs_log_format.h"
22#include "xfs_shared.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_inode.h"
29#include "xfs_btree.h"
30#include "xfs_rmap.h"
31#include "xfs_alloc_btree.h"
32#include "xfs_alloc.h"
33#include "xfs_extent_busy.h"
34#include "xfs_error.h"
35#include "xfs_cksum.h"
36#include "xfs_trace.h"
37#include "xfs_trans.h"
38#include "xfs_buf_item.h"
39#include "xfs_log.h"
40#include "xfs_ag_resv.h"
41
42struct workqueue_struct *xfs_alloc_wq;
43
44#define XFS_ABSDIFF(a,b) (((a) <= (b)) ? ((b) - (a)) : ((a) - (b)))
45
46#define XFSA_FIXUP_BNO_OK 1
47#define XFSA_FIXUP_CNT_OK 2
48
49STATIC int xfs_alloc_ag_vextent_exact(xfs_alloc_arg_t *);
50STATIC int xfs_alloc_ag_vextent_near(xfs_alloc_arg_t *);
51STATIC int xfs_alloc_ag_vextent_size(xfs_alloc_arg_t *);
52STATIC int xfs_alloc_ag_vextent_small(xfs_alloc_arg_t *,
53 xfs_btree_cur_t *, xfs_agblock_t *, xfs_extlen_t *, int *);
54
55unsigned int
56xfs_refc_block(
57 struct xfs_mount *mp)
58{
59 if (xfs_sb_version_hasrmapbt(&mp->m_sb))
60 return XFS_RMAP_BLOCK(mp) + 1;
61 if (xfs_sb_version_hasfinobt(&mp->m_sb))
62 return XFS_FIBT_BLOCK(mp) + 1;
63 return XFS_IBT_BLOCK(mp) + 1;
64}
65
66xfs_extlen_t
67xfs_prealloc_blocks(
68 struct xfs_mount *mp)
69{
70 if (xfs_sb_version_hasreflink(&mp->m_sb))
71 return xfs_refc_block(mp) + 1;
72 if (xfs_sb_version_hasrmapbt(&mp->m_sb))
73 return XFS_RMAP_BLOCK(mp) + 1;
74 if (xfs_sb_version_hasfinobt(&mp->m_sb))
75 return XFS_FIBT_BLOCK(mp) + 1;
76 return XFS_IBT_BLOCK(mp) + 1;
77}
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94unsigned int
95xfs_alloc_set_aside(
96 struct xfs_mount *mp)
97{
98 unsigned int blocks;
99
100 blocks = 4 + (mp->m_sb.sb_agcount * XFS_ALLOC_AGFL_RESERVE);
101 return blocks;
102}
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118unsigned int
119xfs_alloc_ag_max_usable(
120 struct xfs_mount *mp)
121{
122 unsigned int blocks;
123
124 blocks = XFS_BB_TO_FSB(mp, XFS_FSS_TO_BB(mp, 4));
125 blocks += XFS_ALLOC_AGFL_RESERVE;
126 blocks += 3;
127 if (xfs_sb_version_hasfinobt(&mp->m_sb))
128 blocks++;
129 if (xfs_sb_version_hasrmapbt(&mp->m_sb))
130 blocks++;
131 if (xfs_sb_version_hasreflink(&mp->m_sb))
132 blocks++;
133
134 return mp->m_sb.sb_agblocks - blocks;
135}
136
137
138
139
140STATIC int
141xfs_alloc_lookup_eq(
142 struct xfs_btree_cur *cur,
143 xfs_agblock_t bno,
144 xfs_extlen_t len,
145 int *stat)
146{
147 cur->bc_rec.a.ar_startblock = bno;
148 cur->bc_rec.a.ar_blockcount = len;
149 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
150}
151
152
153
154
155
156int
157xfs_alloc_lookup_ge(
158 struct xfs_btree_cur *cur,
159 xfs_agblock_t bno,
160 xfs_extlen_t len,
161 int *stat)
162{
163 cur->bc_rec.a.ar_startblock = bno;
164 cur->bc_rec.a.ar_blockcount = len;
165 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
166}
167
168
169
170
171
172static int
173xfs_alloc_lookup_le(
174 struct xfs_btree_cur *cur,
175 xfs_agblock_t bno,
176 xfs_extlen_t len,
177 int *stat)
178{
179 cur->bc_rec.a.ar_startblock = bno;
180 cur->bc_rec.a.ar_blockcount = len;
181 return xfs_btree_lookup(cur, XFS_LOOKUP_LE, stat);
182}
183
184
185
186
187
188
189STATIC int
190xfs_alloc_update(
191 struct xfs_btree_cur *cur,
192 xfs_agblock_t bno,
193 xfs_extlen_t len)
194{
195 union xfs_btree_rec rec;
196
197 rec.alloc.ar_startblock = cpu_to_be32(bno);
198 rec.alloc.ar_blockcount = cpu_to_be32(len);
199 return xfs_btree_update(cur, &rec);
200}
201
202
203
204
205int
206xfs_alloc_get_rec(
207 struct xfs_btree_cur *cur,
208 xfs_agblock_t *bno,
209 xfs_extlen_t *len,
210 int *stat)
211{
212 union xfs_btree_rec *rec;
213 int error;
214
215 error = xfs_btree_get_rec(cur, &rec, stat);
216 if (!error && *stat == 1) {
217 *bno = be32_to_cpu(rec->alloc.ar_startblock);
218 *len = be32_to_cpu(rec->alloc.ar_blockcount);
219 }
220 return error;
221}
222
223
224
225
226
227STATIC void
228xfs_alloc_compute_aligned(
229 xfs_alloc_arg_t *args,
230 xfs_agblock_t foundbno,
231 xfs_extlen_t foundlen,
232 xfs_agblock_t *resbno,
233 xfs_extlen_t *reslen)
234{
235 xfs_agblock_t bno;
236 xfs_extlen_t len;
237 xfs_extlen_t diff;
238
239
240 xfs_extent_busy_trim(args, foundbno, foundlen, &bno, &len);
241
242
243
244
245
246 if (bno < args->min_agbno && bno + len > args->min_agbno) {
247 diff = args->min_agbno - bno;
248 if (len > diff) {
249 bno += diff;
250 len -= diff;
251 }
252 }
253
254 if (args->alignment > 1 && len >= args->minlen) {
255 xfs_agblock_t aligned_bno = roundup(bno, args->alignment);
256
257 diff = aligned_bno - bno;
258
259 *resbno = aligned_bno;
260 *reslen = diff >= len ? 0 : len - diff;
261 } else {
262 *resbno = bno;
263 *reslen = len;
264 }
265}
266
267
268
269
270
271STATIC xfs_extlen_t
272xfs_alloc_compute_diff(
273 xfs_agblock_t wantbno,
274 xfs_extlen_t wantlen,
275 xfs_extlen_t alignment,
276 int datatype,
277 xfs_agblock_t freebno,
278 xfs_extlen_t freelen,
279 xfs_agblock_t *newbnop)
280{
281 xfs_agblock_t freeend;
282 xfs_agblock_t newbno1;
283 xfs_agblock_t newbno2;
284 xfs_extlen_t newlen1=0;
285 xfs_extlen_t newlen2=0;
286 xfs_agblock_t wantend;
287 bool userdata = xfs_alloc_is_userdata(datatype);
288
289 ASSERT(freelen >= wantlen);
290 freeend = freebno + freelen;
291 wantend = wantbno + wantlen;
292
293
294
295
296
297
298
299 if (freebno >= wantbno || (userdata && freeend < wantend)) {
300 if ((newbno1 = roundup(freebno, alignment)) >= freeend)
301 newbno1 = NULLAGBLOCK;
302 } else if (freeend >= wantend && alignment > 1) {
303 newbno1 = roundup(wantbno, alignment);
304 newbno2 = newbno1 - alignment;
305 if (newbno1 >= freeend)
306 newbno1 = NULLAGBLOCK;
307 else
308 newlen1 = XFS_EXTLEN_MIN(wantlen, freeend - newbno1);
309 if (newbno2 < freebno)
310 newbno2 = NULLAGBLOCK;
311 else
312 newlen2 = XFS_EXTLEN_MIN(wantlen, freeend - newbno2);
313 if (newbno1 != NULLAGBLOCK && newbno2 != NULLAGBLOCK) {
314 if (newlen1 < newlen2 ||
315 (newlen1 == newlen2 &&
316 XFS_ABSDIFF(newbno1, wantbno) >
317 XFS_ABSDIFF(newbno2, wantbno)))
318 newbno1 = newbno2;
319 } else if (newbno2 != NULLAGBLOCK)
320 newbno1 = newbno2;
321 } else if (freeend >= wantend) {
322 newbno1 = wantbno;
323 } else if (alignment > 1) {
324 newbno1 = roundup(freeend - wantlen, alignment);
325 if (newbno1 > freeend - wantlen &&
326 newbno1 - alignment >= freebno)
327 newbno1 -= alignment;
328 else if (newbno1 >= freeend)
329 newbno1 = NULLAGBLOCK;
330 } else
331 newbno1 = freeend - wantlen;
332 *newbnop = newbno1;
333 return newbno1 == NULLAGBLOCK ? 0 : XFS_ABSDIFF(newbno1, wantbno);
334}
335
336
337
338
339
340
341
342STATIC void
343xfs_alloc_fix_len(
344 xfs_alloc_arg_t *args)
345{
346 xfs_extlen_t k;
347 xfs_extlen_t rlen;
348
349 ASSERT(args->mod < args->prod);
350 rlen = args->len;
351 ASSERT(rlen >= args->minlen);
352 ASSERT(rlen <= args->maxlen);
353 if (args->prod <= 1 || rlen < args->mod || rlen == args->maxlen ||
354 (args->mod == 0 && rlen < args->prod))
355 return;
356 k = rlen % args->prod;
357 if (k == args->mod)
358 return;
359 if (k > args->mod)
360 rlen = rlen - (k - args->mod);
361 else
362 rlen = rlen - args->prod + (args->mod - k);
363
364 if ((int)rlen < (int)args->minlen)
365 return;
366 ASSERT(rlen >= args->minlen && rlen <= args->maxlen);
367 ASSERT(rlen % args->prod == args->mod);
368 args->len = rlen;
369}
370
371
372
373
374
375STATIC int
376xfs_alloc_fix_minleft(
377 xfs_alloc_arg_t *args)
378{
379 xfs_agf_t *agf;
380 int diff;
381
382 if (args->minleft == 0)
383 return 1;
384 agf = XFS_BUF_TO_AGF(args->agbp);
385 diff = be32_to_cpu(agf->agf_freeblks)
386 - args->len - args->minleft;
387 if (diff >= 0)
388 return 1;
389 args->len += diff;
390
391 if ((int)args->len >= (int)args->minlen)
392 return 1;
393 args->agbno = NULLAGBLOCK;
394 return 0;
395}
396
397
398
399
400
401
402
403
404STATIC int
405xfs_alloc_fixup_trees(
406 xfs_btree_cur_t *cnt_cur,
407 xfs_btree_cur_t *bno_cur,
408 xfs_agblock_t fbno,
409 xfs_extlen_t flen,
410 xfs_agblock_t rbno,
411 xfs_extlen_t rlen,
412 int flags)
413{
414 int error;
415 int i;
416 xfs_agblock_t nfbno1;
417 xfs_agblock_t nfbno2;
418 xfs_extlen_t nflen1=0;
419 xfs_extlen_t nflen2=0;
420 struct xfs_mount *mp;
421
422 mp = cnt_cur->bc_mp;
423
424
425
426
427 if (flags & XFSA_FIXUP_CNT_OK) {
428#ifdef DEBUG
429 if ((error = xfs_alloc_get_rec(cnt_cur, &nfbno1, &nflen1, &i)))
430 return error;
431 XFS_WANT_CORRUPTED_RETURN(mp,
432 i == 1 && nfbno1 == fbno && nflen1 == flen);
433#endif
434 } else {
435 if ((error = xfs_alloc_lookup_eq(cnt_cur, fbno, flen, &i)))
436 return error;
437 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
438 }
439
440
441
442 if (flags & XFSA_FIXUP_BNO_OK) {
443#ifdef DEBUG
444 if ((error = xfs_alloc_get_rec(bno_cur, &nfbno1, &nflen1, &i)))
445 return error;
446 XFS_WANT_CORRUPTED_RETURN(mp,
447 i == 1 && nfbno1 == fbno && nflen1 == flen);
448#endif
449 } else {
450 if ((error = xfs_alloc_lookup_eq(bno_cur, fbno, flen, &i)))
451 return error;
452 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
453 }
454
455#ifdef DEBUG
456 if (bno_cur->bc_nlevels == 1 && cnt_cur->bc_nlevels == 1) {
457 struct xfs_btree_block *bnoblock;
458 struct xfs_btree_block *cntblock;
459
460 bnoblock = XFS_BUF_TO_BLOCK(bno_cur->bc_bufs[0]);
461 cntblock = XFS_BUF_TO_BLOCK(cnt_cur->bc_bufs[0]);
462
463 XFS_WANT_CORRUPTED_RETURN(mp,
464 bnoblock->bb_numrecs == cntblock->bb_numrecs);
465 }
466#endif
467
468
469
470
471
472
473 if (rbno == fbno && rlen == flen)
474 nfbno1 = nfbno2 = NULLAGBLOCK;
475 else if (rbno == fbno) {
476 nfbno1 = rbno + rlen;
477 nflen1 = flen - rlen;
478 nfbno2 = NULLAGBLOCK;
479 } else if (rbno + rlen == fbno + flen) {
480 nfbno1 = fbno;
481 nflen1 = flen - rlen;
482 nfbno2 = NULLAGBLOCK;
483 } else {
484 nfbno1 = fbno;
485 nflen1 = rbno - fbno;
486 nfbno2 = rbno + rlen;
487 nflen2 = (fbno + flen) - nfbno2;
488 }
489
490
491
492 if ((error = xfs_btree_delete(cnt_cur, &i)))
493 return error;
494 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
495
496
497
498 if (nfbno1 != NULLAGBLOCK) {
499 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno1, nflen1, &i)))
500 return error;
501 XFS_WANT_CORRUPTED_RETURN(mp, i == 0);
502 if ((error = xfs_btree_insert(cnt_cur, &i)))
503 return error;
504 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
505 }
506 if (nfbno2 != NULLAGBLOCK) {
507 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno2, nflen2, &i)))
508 return error;
509 XFS_WANT_CORRUPTED_RETURN(mp, i == 0);
510 if ((error = xfs_btree_insert(cnt_cur, &i)))
511 return error;
512 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
513 }
514
515
516
517 if (nfbno1 == NULLAGBLOCK) {
518
519
520
521 if ((error = xfs_btree_delete(bno_cur, &i)))
522 return error;
523 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
524 } else {
525
526
527
528 if ((error = xfs_alloc_update(bno_cur, nfbno1, nflen1)))
529 return error;
530 }
531 if (nfbno2 != NULLAGBLOCK) {
532
533
534
535 if ((error = xfs_alloc_lookup_eq(bno_cur, nfbno2, nflen2, &i)))
536 return error;
537 XFS_WANT_CORRUPTED_RETURN(mp, i == 0);
538 if ((error = xfs_btree_insert(bno_cur, &i)))
539 return error;
540 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
541 }
542 return 0;
543}
544
545static bool
546xfs_agfl_verify(
547 struct xfs_buf *bp)
548{
549 struct xfs_mount *mp = bp->b_target->bt_mount;
550 struct xfs_agfl *agfl = XFS_BUF_TO_AGFL(bp);
551 int i;
552
553 if (!uuid_equal(&agfl->agfl_uuid, &mp->m_sb.sb_meta_uuid))
554 return false;
555 if (be32_to_cpu(agfl->agfl_magicnum) != XFS_AGFL_MAGIC)
556 return false;
557
558
559
560
561
562
563 if (bp->b_pag && be32_to_cpu(agfl->agfl_seqno) != bp->b_pag->pag_agno)
564 return false;
565
566 for (i = 0; i < XFS_AGFL_SIZE(mp); i++) {
567 if (be32_to_cpu(agfl->agfl_bno[i]) != NULLAGBLOCK &&
568 be32_to_cpu(agfl->agfl_bno[i]) >= mp->m_sb.sb_agblocks)
569 return false;
570 }
571
572 return xfs_log_check_lsn(mp,
573 be64_to_cpu(XFS_BUF_TO_AGFL(bp)->agfl_lsn));
574}
575
576static void
577xfs_agfl_read_verify(
578 struct xfs_buf *bp)
579{
580 struct xfs_mount *mp = bp->b_target->bt_mount;
581
582
583
584
585
586
587
588 if (!xfs_sb_version_hascrc(&mp->m_sb))
589 return;
590
591 if (!xfs_buf_verify_cksum(bp, XFS_AGFL_CRC_OFF))
592 xfs_buf_ioerror(bp, -EFSBADCRC);
593 else if (!xfs_agfl_verify(bp))
594 xfs_buf_ioerror(bp, -EFSCORRUPTED);
595
596 if (bp->b_error)
597 xfs_verifier_error(bp);
598}
599
600static void
601xfs_agfl_write_verify(
602 struct xfs_buf *bp)
603{
604 struct xfs_mount *mp = bp->b_target->bt_mount;
605 struct xfs_buf_log_item *bip = bp->b_fspriv;
606
607
608 if (!xfs_sb_version_hascrc(&mp->m_sb))
609 return;
610
611 if (!xfs_agfl_verify(bp)) {
612 xfs_buf_ioerror(bp, -EFSCORRUPTED);
613 xfs_verifier_error(bp);
614 return;
615 }
616
617 if (bip)
618 XFS_BUF_TO_AGFL(bp)->agfl_lsn = cpu_to_be64(bip->bli_item.li_lsn);
619
620 xfs_buf_update_cksum(bp, XFS_AGFL_CRC_OFF);
621}
622
623const struct xfs_buf_ops xfs_agfl_buf_ops = {
624 .name = "xfs_agfl",
625 .verify_read = xfs_agfl_read_verify,
626 .verify_write = xfs_agfl_write_verify,
627};
628
629
630
631
632STATIC int
633xfs_alloc_read_agfl(
634 xfs_mount_t *mp,
635 xfs_trans_t *tp,
636 xfs_agnumber_t agno,
637 xfs_buf_t **bpp)
638{
639 xfs_buf_t *bp;
640 int error;
641
642 ASSERT(agno != NULLAGNUMBER);
643 error = xfs_trans_read_buf(
644 mp, tp, mp->m_ddev_targp,
645 XFS_AG_DADDR(mp, agno, XFS_AGFL_DADDR(mp)),
646 XFS_FSS_TO_BB(mp, 1), 0, &bp, &xfs_agfl_buf_ops);
647 if (error)
648 return error;
649 xfs_buf_set_ref(bp, XFS_AGFL_REF);
650 *bpp = bp;
651 return 0;
652}
653
654STATIC int
655xfs_alloc_update_counters(
656 struct xfs_trans *tp,
657 struct xfs_perag *pag,
658 struct xfs_buf *agbp,
659 long len)
660{
661 struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
662
663 pag->pagf_freeblks += len;
664 be32_add_cpu(&agf->agf_freeblks, len);
665
666 xfs_trans_agblocks_delta(tp, len);
667 if (unlikely(be32_to_cpu(agf->agf_freeblks) >
668 be32_to_cpu(agf->agf_length)))
669 return -EFSCORRUPTED;
670
671 xfs_alloc_log_agf(tp, agbp, XFS_AGF_FREEBLKS);
672 return 0;
673}
674
675
676
677
678
679
680
681
682
683
684
685
686
687STATIC int
688xfs_alloc_ag_vextent(
689 xfs_alloc_arg_t *args)
690{
691 int error=0;
692 xfs_extlen_t reservation;
693 xfs_extlen_t oldmax;
694
695 ASSERT(args->minlen > 0);
696 ASSERT(args->maxlen > 0);
697 ASSERT(args->minlen <= args->maxlen);
698 ASSERT(args->mod < args->prod);
699 ASSERT(args->alignment > 0);
700
701
702
703
704
705 oldmax = args->maxlen;
706 reservation = xfs_ag_resv_needed(args->pag, args->resv);
707 if (args->maxlen > args->pag->pagf_freeblks - reservation)
708 args->maxlen = args->pag->pagf_freeblks - reservation;
709 if (args->maxlen == 0) {
710 args->agbno = NULLAGBLOCK;
711 args->maxlen = oldmax;
712 return 0;
713 }
714
715
716
717
718 args->wasfromfl = 0;
719 switch (args->type) {
720 case XFS_ALLOCTYPE_THIS_AG:
721 error = xfs_alloc_ag_vextent_size(args);
722 break;
723 case XFS_ALLOCTYPE_NEAR_BNO:
724 error = xfs_alloc_ag_vextent_near(args);
725 break;
726 case XFS_ALLOCTYPE_THIS_BNO:
727 error = xfs_alloc_ag_vextent_exact(args);
728 break;
729 default:
730 ASSERT(0);
731
732 }
733
734 args->maxlen = oldmax;
735
736 if (error || args->agbno == NULLAGBLOCK)
737 return error;
738
739 ASSERT(args->len >= args->minlen);
740 ASSERT(args->len <= args->maxlen);
741 ASSERT(!args->wasfromfl || args->resv != XFS_AG_RESV_AGFL);
742 ASSERT(args->agbno % args->alignment == 0);
743
744
745 if (args->oinfo.oi_owner != XFS_RMAP_OWN_UNKNOWN) {
746 error = xfs_rmap_alloc(args->tp, args->agbp, args->agno,
747 args->agbno, args->len, &args->oinfo);
748 if (error)
749 return error;
750 }
751
752 if (!args->wasfromfl) {
753 error = xfs_alloc_update_counters(args->tp, args->pag,
754 args->agbp,
755 -((long)(args->len)));
756 if (error)
757 return error;
758
759 ASSERT(!xfs_extent_busy_search(args->mp, args->agno,
760 args->agbno, args->len));
761 }
762
763 xfs_ag_resv_alloc_extent(args->pag, args->resv, args);
764
765 XFS_STATS_INC(args->mp, xs_allocx);
766 XFS_STATS_ADD(args->mp, xs_allocb, args->len);
767 return error;
768}
769
770
771
772
773
774
775
776STATIC int
777xfs_alloc_ag_vextent_exact(
778 xfs_alloc_arg_t *args)
779{
780 xfs_btree_cur_t *bno_cur;
781 xfs_btree_cur_t *cnt_cur;
782 int error;
783 xfs_agblock_t fbno;
784 xfs_extlen_t flen;
785 xfs_agblock_t tbno;
786 xfs_extlen_t tlen;
787 xfs_agblock_t tend;
788 int i;
789
790 ASSERT(args->alignment == 1);
791
792
793
794
795 bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
796 args->agno, XFS_BTNUM_BNO);
797
798
799
800
801
802
803 error = xfs_alloc_lookup_le(bno_cur, args->agbno, args->minlen, &i);
804 if (error)
805 goto error0;
806 if (!i)
807 goto not_found;
808
809
810
811
812 error = xfs_alloc_get_rec(bno_cur, &fbno, &flen, &i);
813 if (error)
814 goto error0;
815 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
816 ASSERT(fbno <= args->agbno);
817
818
819
820
821 xfs_extent_busy_trim(args, fbno, flen, &tbno, &tlen);
822
823
824
825
826
827 if (tbno > args->agbno)
828 goto not_found;
829 if (tlen < args->minlen)
830 goto not_found;
831 tend = tbno + tlen;
832 if (tend < args->agbno + args->minlen)
833 goto not_found;
834
835
836
837
838
839
840
841 args->len = XFS_AGBLOCK_MIN(tend, args->agbno + args->maxlen)
842 - args->agbno;
843 xfs_alloc_fix_len(args);
844 if (!xfs_alloc_fix_minleft(args))
845 goto not_found;
846
847 ASSERT(args->agbno + args->len <= tend);
848
849
850
851
852
853 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
854 args->agno, XFS_BTNUM_CNT);
855 ASSERT(args->agbno + args->len <=
856 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
857 error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen, args->agbno,
858 args->len, XFSA_FIXUP_BNO_OK);
859 if (error) {
860 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
861 goto error0;
862 }
863
864 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
865 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
866
867 args->wasfromfl = 0;
868 trace_xfs_alloc_exact_done(args);
869 return 0;
870
871not_found:
872
873 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
874 args->agbno = NULLAGBLOCK;
875 trace_xfs_alloc_exact_notfound(args);
876 return 0;
877
878error0:
879 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
880 trace_xfs_alloc_exact_error(args);
881 return error;
882}
883
884
885
886
887
888STATIC int
889xfs_alloc_find_best_extent(
890 struct xfs_alloc_arg *args,
891 struct xfs_btree_cur **gcur,
892 struct xfs_btree_cur **scur,
893 xfs_agblock_t gdiff,
894 xfs_agblock_t *sbno,
895 xfs_extlen_t *slen,
896 xfs_agblock_t *sbnoa,
897 xfs_extlen_t *slena,
898 int dir)
899{
900 xfs_agblock_t new;
901 xfs_agblock_t sdiff;
902 int error;
903 int i;
904
905
906 if (!gdiff)
907 goto out_use_good;
908
909
910
911
912 do {
913 error = xfs_alloc_get_rec(*scur, sbno, slen, &i);
914 if (error)
915 goto error0;
916 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
917 xfs_alloc_compute_aligned(args, *sbno, *slen, sbnoa, slena);
918
919
920
921
922 if (!dir) {
923 if (*sbnoa > args->max_agbno)
924 goto out_use_good;
925 if (*sbnoa >= args->agbno + gdiff)
926 goto out_use_good;
927 } else {
928 if (*sbnoa < args->min_agbno)
929 goto out_use_good;
930 if (*sbnoa <= args->agbno - gdiff)
931 goto out_use_good;
932 }
933
934
935
936
937 if (*slena >= args->minlen) {
938 args->len = XFS_EXTLEN_MIN(*slena, args->maxlen);
939 xfs_alloc_fix_len(args);
940
941 sdiff = xfs_alloc_compute_diff(args->agbno, args->len,
942 args->alignment,
943 args->datatype, *sbnoa,
944 *slena, &new);
945
946
947
948
949 if (sdiff < gdiff)
950 goto out_use_search;
951 goto out_use_good;
952 }
953
954 if (!dir)
955 error = xfs_btree_increment(*scur, 0, &i);
956 else
957 error = xfs_btree_decrement(*scur, 0, &i);
958 if (error)
959 goto error0;
960 } while (i);
961
962out_use_good:
963 xfs_btree_del_cursor(*scur, XFS_BTREE_NOERROR);
964 *scur = NULL;
965 return 0;
966
967out_use_search:
968 xfs_btree_del_cursor(*gcur, XFS_BTREE_NOERROR);
969 *gcur = NULL;
970 return 0;
971
972error0:
973
974 return error;
975}
976
977
978
979
980
981
982
983STATIC int
984xfs_alloc_ag_vextent_near(
985 xfs_alloc_arg_t *args)
986{
987 xfs_btree_cur_t *bno_cur_gt;
988 xfs_btree_cur_t *bno_cur_lt;
989 xfs_btree_cur_t *cnt_cur;
990 xfs_agblock_t gtbno;
991 xfs_agblock_t gtbnoa;
992 xfs_extlen_t gtdiff;
993 xfs_extlen_t gtlen;
994 xfs_extlen_t gtlena;
995 xfs_agblock_t gtnew;
996 int error;
997 int i;
998 int j;
999 xfs_agblock_t ltbno;
1000 xfs_agblock_t ltbnoa;
1001 xfs_extlen_t ltdiff;
1002 xfs_extlen_t ltlen;
1003 xfs_extlen_t ltlena;
1004 xfs_agblock_t ltnew;
1005 xfs_extlen_t rlen;
1006 int forced = 0;
1007#ifdef DEBUG
1008
1009
1010
1011 int dofirst;
1012
1013 dofirst = prandom_u32() & 1;
1014#endif
1015
1016
1017 if (!args->min_agbno && !args->max_agbno)
1018 args->max_agbno = args->mp->m_sb.sb_agblocks - 1;
1019 ASSERT(args->min_agbno <= args->max_agbno);
1020
1021
1022 if (args->agbno < args->min_agbno)
1023 args->agbno = args->min_agbno;
1024 if (args->agbno > args->max_agbno)
1025 args->agbno = args->max_agbno;
1026
1027restart:
1028 bno_cur_lt = NULL;
1029 bno_cur_gt = NULL;
1030 ltlen = 0;
1031 gtlena = 0;
1032 ltlena = 0;
1033
1034
1035
1036
1037 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1038 args->agno, XFS_BTNUM_CNT);
1039
1040
1041
1042
1043 if ((error = xfs_alloc_lookup_ge(cnt_cur, 0, args->maxlen, &i)))
1044 goto error0;
1045
1046
1047
1048
1049 if (!i) {
1050 if ((error = xfs_alloc_ag_vextent_small(args, cnt_cur, <bno,
1051 <len, &i)))
1052 goto error0;
1053 if (i == 0 || ltlen == 0) {
1054 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1055 trace_xfs_alloc_near_noentry(args);
1056 return 0;
1057 }
1058 ASSERT(i == 1);
1059 }
1060 args->wasfromfl = 0;
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072 while (xfs_btree_islastblock(cnt_cur, 0)) {
1073 xfs_extlen_t bdiff;
1074 int besti=0;
1075 xfs_extlen_t blen=0;
1076 xfs_agblock_t bnew=0;
1077
1078#ifdef DEBUG
1079 if (dofirst)
1080 break;
1081#endif
1082
1083
1084
1085
1086
1087
1088 if (ltlen || args->alignment > 1) {
1089 cnt_cur->bc_ptrs[0] = 1;
1090 do {
1091 if ((error = xfs_alloc_get_rec(cnt_cur, <bno,
1092 <len, &i)))
1093 goto error0;
1094 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
1095 if (ltlen >= args->minlen)
1096 break;
1097 if ((error = xfs_btree_increment(cnt_cur, 0, &i)))
1098 goto error0;
1099 } while (i);
1100 ASSERT(ltlen >= args->minlen);
1101 if (!i)
1102 break;
1103 }
1104 i = cnt_cur->bc_ptrs[0];
1105 for (j = 1, blen = 0, bdiff = 0;
1106 !error && j && (blen < args->maxlen || bdiff > 0);
1107 error = xfs_btree_increment(cnt_cur, 0, &j)) {
1108
1109
1110
1111
1112 if ((error = xfs_alloc_get_rec(cnt_cur, <bno, <len, &i)))
1113 goto error0;
1114 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
1115 xfs_alloc_compute_aligned(args, ltbno, ltlen,
1116 <bnoa, <lena);
1117 if (ltlena < args->minlen)
1118 continue;
1119 if (ltbnoa < args->min_agbno || ltbnoa > args->max_agbno)
1120 continue;
1121 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1122 xfs_alloc_fix_len(args);
1123 ASSERT(args->len >= args->minlen);
1124 if (args->len < blen)
1125 continue;
1126 ltdiff = xfs_alloc_compute_diff(args->agbno, args->len,
1127 args->alignment, args->datatype, ltbnoa,
1128 ltlena, <new);
1129 if (ltnew != NULLAGBLOCK &&
1130 (args->len > blen || ltdiff < bdiff)) {
1131 bdiff = ltdiff;
1132 bnew = ltnew;
1133 blen = args->len;
1134 besti = cnt_cur->bc_ptrs[0];
1135 }
1136 }
1137
1138
1139
1140
1141 if (blen == 0)
1142 break;
1143
1144
1145
1146 cnt_cur->bc_ptrs[0] = besti;
1147 if ((error = xfs_alloc_get_rec(cnt_cur, <bno, <len, &i)))
1148 goto error0;
1149 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
1150 ASSERT(ltbno + ltlen <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
1151 args->len = blen;
1152 if (!xfs_alloc_fix_minleft(args)) {
1153 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1154 trace_xfs_alloc_near_nominleft(args);
1155 return 0;
1156 }
1157 blen = args->len;
1158
1159
1160
1161 args->agbno = bnew;
1162 ASSERT(bnew >= ltbno);
1163 ASSERT(bnew + blen <= ltbno + ltlen);
1164
1165
1166
1167 bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp,
1168 args->agbp, args->agno, XFS_BTNUM_BNO);
1169
1170
1171
1172 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno,
1173 ltlen, bnew, blen, XFSA_FIXUP_CNT_OK)))
1174 goto error0;
1175 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1176 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
1177
1178 trace_xfs_alloc_near_first(args);
1179 return 0;
1180 }
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196 bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1197 args->agno, XFS_BTNUM_BNO);
1198
1199
1200
1201 if ((error = xfs_alloc_lookup_le(bno_cur_lt, args->agbno, args->maxlen, &i)))
1202 goto error0;
1203 if (!i) {
1204
1205
1206
1207
1208 bno_cur_gt = bno_cur_lt;
1209 bno_cur_lt = NULL;
1210 }
1211
1212
1213
1214 else if ((error = xfs_btree_dup_cursor(bno_cur_lt, &bno_cur_gt)))
1215 goto error0;
1216
1217
1218
1219
1220 if ((error = xfs_btree_increment(bno_cur_gt, 0, &i)))
1221 goto error0;
1222 if (!i) {
1223
1224
1225
1226 xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_NOERROR);
1227 bno_cur_gt = NULL;
1228 }
1229
1230
1231
1232
1233
1234 do {
1235 if (bno_cur_lt) {
1236 if ((error = xfs_alloc_get_rec(bno_cur_lt, <bno, <len, &i)))
1237 goto error0;
1238 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
1239 xfs_alloc_compute_aligned(args, ltbno, ltlen,
1240 <bnoa, <lena);
1241 if (ltlena >= args->minlen && ltbnoa >= args->min_agbno)
1242 break;
1243 if ((error = xfs_btree_decrement(bno_cur_lt, 0, &i)))
1244 goto error0;
1245 if (!i || ltbnoa < args->min_agbno) {
1246 xfs_btree_del_cursor(bno_cur_lt,
1247 XFS_BTREE_NOERROR);
1248 bno_cur_lt = NULL;
1249 }
1250 }
1251 if (bno_cur_gt) {
1252 if ((error = xfs_alloc_get_rec(bno_cur_gt, >bno, >len, &i)))
1253 goto error0;
1254 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
1255 xfs_alloc_compute_aligned(args, gtbno, gtlen,
1256 >bnoa, >lena);
1257 if (gtlena >= args->minlen && gtbnoa <= args->max_agbno)
1258 break;
1259 if ((error = xfs_btree_increment(bno_cur_gt, 0, &i)))
1260 goto error0;
1261 if (!i || gtbnoa > args->max_agbno) {
1262 xfs_btree_del_cursor(bno_cur_gt,
1263 XFS_BTREE_NOERROR);
1264 bno_cur_gt = NULL;
1265 }
1266 }
1267 } while (bno_cur_lt || bno_cur_gt);
1268
1269
1270
1271
1272 if (bno_cur_lt && bno_cur_gt) {
1273 if (ltlena >= args->minlen) {
1274
1275
1276
1277 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1278 xfs_alloc_fix_len(args);
1279 ltdiff = xfs_alloc_compute_diff(args->agbno, args->len,
1280 args->alignment, args->datatype, ltbnoa,
1281 ltlena, <new);
1282
1283 error = xfs_alloc_find_best_extent(args,
1284 &bno_cur_lt, &bno_cur_gt,
1285 ltdiff, >bno, >len,
1286 >bnoa, >lena,
1287 0 );
1288 } else {
1289 ASSERT(gtlena >= args->minlen);
1290
1291
1292
1293
1294 args->len = XFS_EXTLEN_MIN(gtlena, args->maxlen);
1295 xfs_alloc_fix_len(args);
1296 gtdiff = xfs_alloc_compute_diff(args->agbno, args->len,
1297 args->alignment, args->datatype, gtbnoa,
1298 gtlena, >new);
1299
1300 error = xfs_alloc_find_best_extent(args,
1301 &bno_cur_gt, &bno_cur_lt,
1302 gtdiff, <bno, <len,
1303 <bnoa, <lena,
1304 1 );
1305 }
1306
1307 if (error)
1308 goto error0;
1309 }
1310
1311
1312
1313
1314 if (bno_cur_lt == NULL && bno_cur_gt == NULL) {
1315 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1316
1317 if (!forced++) {
1318 trace_xfs_alloc_near_busy(args);
1319 xfs_log_force(args->mp, XFS_LOG_SYNC);
1320 goto restart;
1321 }
1322 trace_xfs_alloc_size_neither(args);
1323 args->agbno = NULLAGBLOCK;
1324 return 0;
1325 }
1326
1327
1328
1329
1330
1331
1332
1333 if (bno_cur_gt) {
1334 bno_cur_lt = bno_cur_gt;
1335 bno_cur_gt = NULL;
1336 ltbno = gtbno;
1337 ltbnoa = gtbnoa;
1338 ltlen = gtlen;
1339 ltlena = gtlena;
1340 j = 1;
1341 } else
1342 j = 0;
1343
1344
1345
1346
1347 args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
1348 xfs_alloc_fix_len(args);
1349 if (!xfs_alloc_fix_minleft(args)) {
1350 trace_xfs_alloc_near_nominleft(args);
1351 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
1352 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1353 return 0;
1354 }
1355 rlen = args->len;
1356 (void)xfs_alloc_compute_diff(args->agbno, rlen, args->alignment,
1357 args->datatype, ltbnoa, ltlena, <new);
1358 ASSERT(ltnew >= ltbno);
1359 ASSERT(ltnew + rlen <= ltbnoa + ltlena);
1360 ASSERT(ltnew + rlen <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
1361 ASSERT(ltnew >= args->min_agbno && ltnew <= args->max_agbno);
1362 args->agbno = ltnew;
1363
1364 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno, ltlen,
1365 ltnew, rlen, XFSA_FIXUP_BNO_OK)))
1366 goto error0;
1367
1368 if (j)
1369 trace_xfs_alloc_near_greater(args);
1370 else
1371 trace_xfs_alloc_near_lesser(args);
1372
1373 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1374 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
1375 return 0;
1376
1377 error0:
1378 trace_xfs_alloc_near_error(args);
1379 if (cnt_cur != NULL)
1380 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1381 if (bno_cur_lt != NULL)
1382 xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_ERROR);
1383 if (bno_cur_gt != NULL)
1384 xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_ERROR);
1385 return error;
1386}
1387
1388
1389
1390
1391
1392
1393
1394STATIC int
1395xfs_alloc_ag_vextent_size(
1396 xfs_alloc_arg_t *args)
1397{
1398 xfs_btree_cur_t *bno_cur;
1399 xfs_btree_cur_t *cnt_cur;
1400 int error;
1401 xfs_agblock_t fbno;
1402 xfs_extlen_t flen;
1403 int i;
1404 xfs_agblock_t rbno;
1405 xfs_extlen_t rlen;
1406 int forced = 0;
1407
1408restart:
1409
1410
1411
1412 cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1413 args->agno, XFS_BTNUM_CNT);
1414 bno_cur = NULL;
1415
1416
1417
1418
1419 if ((error = xfs_alloc_lookup_ge(cnt_cur, 0,
1420 args->maxlen + args->alignment - 1, &i)))
1421 goto error0;
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431 if (!i || forced > 1) {
1432 error = xfs_alloc_ag_vextent_small(args, cnt_cur,
1433 &fbno, &flen, &i);
1434 if (error)
1435 goto error0;
1436 if (i == 0 || flen == 0) {
1437 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1438 trace_xfs_alloc_size_noentry(args);
1439 return 0;
1440 }
1441 ASSERT(i == 1);
1442 xfs_alloc_compute_aligned(args, fbno, flen, &rbno, &rlen);
1443 } else {
1444
1445
1446
1447
1448
1449
1450 for (;;) {
1451 error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen, &i);
1452 if (error)
1453 goto error0;
1454 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
1455
1456 xfs_alloc_compute_aligned(args, fbno, flen,
1457 &rbno, &rlen);
1458
1459 if (rlen >= args->maxlen)
1460 break;
1461
1462 error = xfs_btree_increment(cnt_cur, 0, &i);
1463 if (error)
1464 goto error0;
1465 if (i == 0) {
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476 xfs_btree_del_cursor(cnt_cur,
1477 XFS_BTREE_NOERROR);
1478 trace_xfs_alloc_size_busy(args);
1479 if (!forced++)
1480 xfs_log_force(args->mp, XFS_LOG_SYNC);
1481 goto restart;
1482 }
1483 }
1484 }
1485
1486
1487
1488
1489
1490
1491
1492 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
1493 XFS_WANT_CORRUPTED_GOTO(args->mp, rlen == 0 ||
1494 (rlen <= flen && rbno + rlen <= fbno + flen), error0);
1495 if (rlen < args->maxlen) {
1496 xfs_agblock_t bestfbno;
1497 xfs_extlen_t bestflen;
1498 xfs_agblock_t bestrbno;
1499 xfs_extlen_t bestrlen;
1500
1501 bestrlen = rlen;
1502 bestrbno = rbno;
1503 bestflen = flen;
1504 bestfbno = fbno;
1505 for (;;) {
1506 if ((error = xfs_btree_decrement(cnt_cur, 0, &i)))
1507 goto error0;
1508 if (i == 0)
1509 break;
1510 if ((error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen,
1511 &i)))
1512 goto error0;
1513 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
1514 if (flen < bestrlen)
1515 break;
1516 xfs_alloc_compute_aligned(args, fbno, flen,
1517 &rbno, &rlen);
1518 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
1519 XFS_WANT_CORRUPTED_GOTO(args->mp, rlen == 0 ||
1520 (rlen <= flen && rbno + rlen <= fbno + flen),
1521 error0);
1522 if (rlen > bestrlen) {
1523 bestrlen = rlen;
1524 bestrbno = rbno;
1525 bestflen = flen;
1526 bestfbno = fbno;
1527 if (rlen == args->maxlen)
1528 break;
1529 }
1530 }
1531 if ((error = xfs_alloc_lookup_eq(cnt_cur, bestfbno, bestflen,
1532 &i)))
1533 goto error0;
1534 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
1535 rlen = bestrlen;
1536 rbno = bestrbno;
1537 flen = bestflen;
1538 fbno = bestfbno;
1539 }
1540 args->wasfromfl = 0;
1541
1542
1543
1544 args->len = rlen;
1545 if (rlen < args->minlen) {
1546 if (!forced++) {
1547 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1548 trace_xfs_alloc_size_busy(args);
1549 xfs_log_force(args->mp, XFS_LOG_SYNC);
1550 goto restart;
1551 }
1552 goto out_nominleft;
1553 }
1554 xfs_alloc_fix_len(args);
1555
1556 if (!xfs_alloc_fix_minleft(args))
1557 goto out_nominleft;
1558 rlen = args->len;
1559 XFS_WANT_CORRUPTED_GOTO(args->mp, rlen <= flen, error0);
1560
1561
1562
1563 bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
1564 args->agno, XFS_BTNUM_BNO);
1565 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen,
1566 rbno, rlen, XFSA_FIXUP_CNT_OK)))
1567 goto error0;
1568 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1569 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1570 cnt_cur = bno_cur = NULL;
1571 args->len = rlen;
1572 args->agbno = rbno;
1573 XFS_WANT_CORRUPTED_GOTO(args->mp,
1574 args->agbno + args->len <=
1575 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
1576 error0);
1577 trace_xfs_alloc_size_done(args);
1578 return 0;
1579
1580error0:
1581 trace_xfs_alloc_size_error(args);
1582 if (cnt_cur)
1583 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1584 if (bno_cur)
1585 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
1586 return error;
1587
1588out_nominleft:
1589 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1590 trace_xfs_alloc_size_nominleft(args);
1591 args->agbno = NULLAGBLOCK;
1592 return 0;
1593}
1594
1595
1596
1597
1598
1599
1600STATIC int
1601xfs_alloc_ag_vextent_small(
1602 xfs_alloc_arg_t *args,
1603 xfs_btree_cur_t *ccur,
1604 xfs_agblock_t *fbnop,
1605 xfs_extlen_t *flenp,
1606 int *stat)
1607{
1608 struct xfs_owner_info oinfo;
1609 struct xfs_perag *pag;
1610 int error;
1611 xfs_agblock_t fbno;
1612 xfs_extlen_t flen;
1613 int i;
1614
1615 if ((error = xfs_btree_decrement(ccur, 0, &i)))
1616 goto error0;
1617 if (i) {
1618 if ((error = xfs_alloc_get_rec(ccur, &fbno, &flen, &i)))
1619 goto error0;
1620 XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
1621 }
1622
1623
1624
1625
1626
1627 else if (args->minlen == 1 && args->alignment == 1 &&
1628 args->resv != XFS_AG_RESV_AGFL &&
1629 (be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_flcount)
1630 > args->minleft)) {
1631 error = xfs_alloc_get_freelist(args->tp, args->agbp, &fbno, 0);
1632 if (error)
1633 goto error0;
1634 if (fbno != NULLAGBLOCK) {
1635 xfs_extent_busy_reuse(args->mp, args->agno, fbno, 1,
1636 xfs_alloc_allow_busy_reuse(args->datatype));
1637
1638 if (xfs_alloc_is_userdata(args->datatype)) {
1639 xfs_buf_t *bp;
1640
1641 bp = xfs_btree_get_bufs(args->mp, args->tp,
1642 args->agno, fbno, 0);
1643 xfs_trans_binval(args->tp, bp);
1644 }
1645 args->len = 1;
1646 args->agbno = fbno;
1647 XFS_WANT_CORRUPTED_GOTO(args->mp,
1648 args->agbno + args->len <=
1649 be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
1650 error0);
1651 args->wasfromfl = 1;
1652 trace_xfs_alloc_small_freelist(args);
1653
1654
1655
1656
1657
1658
1659
1660 xfs_rmap_ag_owner(&oinfo, XFS_RMAP_OWN_AG);
1661 error = xfs_rmap_free(args->tp, args->agbp, args->agno,
1662 fbno, 1, &oinfo);
1663 if (error)
1664 goto error0;
1665 pag = xfs_perag_get(args->mp, args->agno);
1666 xfs_ag_resv_free_extent(pag, XFS_AG_RESV_AGFL,
1667 args->tp, 1);
1668 xfs_perag_put(pag);
1669
1670 *stat = 0;
1671 return 0;
1672 }
1673
1674
1675
1676 else
1677 flen = 0;
1678 }
1679
1680
1681
1682 else {
1683 fbno = NULLAGBLOCK;
1684 flen = 0;
1685 }
1686
1687
1688
1689 if (flen < args->minlen) {
1690 args->agbno = NULLAGBLOCK;
1691 trace_xfs_alloc_small_notenough(args);
1692 flen = 0;
1693 }
1694 *fbnop = fbno;
1695 *flenp = flen;
1696 *stat = 1;
1697 trace_xfs_alloc_small_done(args);
1698 return 0;
1699
1700error0:
1701 trace_xfs_alloc_small_error(args);
1702 return error;
1703}
1704
1705
1706
1707
1708STATIC int
1709xfs_free_ag_extent(
1710 xfs_trans_t *tp,
1711 xfs_buf_t *agbp,
1712 xfs_agnumber_t agno,
1713 xfs_agblock_t bno,
1714 xfs_extlen_t len,
1715 struct xfs_owner_info *oinfo,
1716 enum xfs_ag_resv_type type)
1717{
1718 xfs_btree_cur_t *bno_cur;
1719 xfs_btree_cur_t *cnt_cur;
1720 int error;
1721 xfs_agblock_t gtbno;
1722 xfs_extlen_t gtlen;
1723 int haveleft;
1724 int haveright;
1725 int i;
1726 xfs_agblock_t ltbno;
1727 xfs_extlen_t ltlen;
1728 xfs_mount_t *mp;
1729 xfs_agblock_t nbno;
1730 xfs_extlen_t nlen;
1731 xfs_perag_t *pag;
1732
1733 bno_cur = cnt_cur = NULL;
1734 mp = tp->t_mountp;
1735
1736 if (oinfo->oi_owner != XFS_RMAP_OWN_UNKNOWN) {
1737 error = xfs_rmap_free(tp, agbp, agno, bno, len, oinfo);
1738 if (error)
1739 goto error0;
1740 }
1741
1742
1743
1744
1745 bno_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_BNO);
1746
1747
1748
1749
1750 if ((error = xfs_alloc_lookup_le(bno_cur, bno, len, &haveleft)))
1751 goto error0;
1752 if (haveleft) {
1753
1754
1755
1756 if ((error = xfs_alloc_get_rec(bno_cur, <bno, <len, &i)))
1757 goto error0;
1758 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
1759
1760
1761
1762 if (ltbno + ltlen < bno)
1763 haveleft = 0;
1764 else {
1765
1766
1767
1768
1769
1770 XFS_WANT_CORRUPTED_GOTO(mp,
1771 ltbno + ltlen <= bno, error0);
1772 }
1773 }
1774
1775
1776
1777
1778 if ((error = xfs_btree_increment(bno_cur, 0, &haveright)))
1779 goto error0;
1780 if (haveright) {
1781
1782
1783
1784 if ((error = xfs_alloc_get_rec(bno_cur, >bno, >len, &i)))
1785 goto error0;
1786 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
1787
1788
1789
1790 if (bno + len < gtbno)
1791 haveright = 0;
1792 else {
1793
1794
1795
1796
1797
1798 XFS_WANT_CORRUPTED_GOTO(mp, gtbno >= bno + len, error0);
1799 }
1800 }
1801
1802
1803
1804 cnt_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_CNT);
1805
1806
1807
1808
1809 if (haveleft && haveright) {
1810
1811
1812
1813 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
1814 goto error0;
1815 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
1816 if ((error = xfs_btree_delete(cnt_cur, &i)))
1817 goto error0;
1818 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
1819
1820
1821
1822 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
1823 goto error0;
1824 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
1825 if ((error = xfs_btree_delete(cnt_cur, &i)))
1826 goto error0;
1827 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
1828
1829
1830
1831 if ((error = xfs_btree_delete(bno_cur, &i)))
1832 goto error0;
1833 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
1834
1835
1836
1837 if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
1838 goto error0;
1839 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
1840#ifdef DEBUG
1841
1842
1843
1844
1845 {
1846 xfs_agblock_t xxbno;
1847 xfs_extlen_t xxlen;
1848
1849 if ((error = xfs_alloc_get_rec(bno_cur, &xxbno, &xxlen,
1850 &i)))
1851 goto error0;
1852 XFS_WANT_CORRUPTED_GOTO(mp,
1853 i == 1 && xxbno == ltbno && xxlen == ltlen,
1854 error0);
1855 }
1856#endif
1857
1858
1859
1860 nbno = ltbno;
1861 nlen = len + ltlen + gtlen;
1862 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1863 goto error0;
1864 }
1865
1866
1867
1868
1869 else if (haveleft) {
1870
1871
1872
1873 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
1874 goto error0;
1875 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
1876 if ((error = xfs_btree_delete(cnt_cur, &i)))
1877 goto error0;
1878 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
1879
1880
1881
1882
1883 if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
1884 goto error0;
1885 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
1886 nbno = ltbno;
1887 nlen = len + ltlen;
1888 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1889 goto error0;
1890 }
1891
1892
1893
1894
1895 else if (haveright) {
1896
1897
1898
1899 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
1900 goto error0;
1901 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
1902 if ((error = xfs_btree_delete(cnt_cur, &i)))
1903 goto error0;
1904 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
1905
1906
1907
1908
1909 nbno = bno;
1910 nlen = len + gtlen;
1911 if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
1912 goto error0;
1913 }
1914
1915
1916
1917
1918 else {
1919 nbno = bno;
1920 nlen = len;
1921 if ((error = xfs_btree_insert(bno_cur, &i)))
1922 goto error0;
1923 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
1924 }
1925 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
1926 bno_cur = NULL;
1927
1928
1929
1930 if ((error = xfs_alloc_lookup_eq(cnt_cur, nbno, nlen, &i)))
1931 goto error0;
1932 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, error0);
1933 if ((error = xfs_btree_insert(cnt_cur, &i)))
1934 goto error0;
1935 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
1936 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
1937 cnt_cur = NULL;
1938
1939
1940
1941
1942 pag = xfs_perag_get(mp, agno);
1943 error = xfs_alloc_update_counters(tp, pag, agbp, len);
1944 xfs_ag_resv_free_extent(pag, type, tp, len);
1945 xfs_perag_put(pag);
1946 if (error)
1947 goto error0;
1948
1949 XFS_STATS_INC(mp, xs_freex);
1950 XFS_STATS_ADD(mp, xs_freeb, len);
1951
1952 trace_xfs_free_extent(mp, agno, bno, len, type == XFS_AG_RESV_AGFL,
1953 haveleft, haveright);
1954
1955 return 0;
1956
1957 error0:
1958 trace_xfs_free_extent(mp, agno, bno, len, type == XFS_AG_RESV_AGFL,
1959 -1, -1);
1960 if (bno_cur)
1961 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
1962 if (cnt_cur)
1963 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
1964 return error;
1965}
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975void
1976xfs_alloc_compute_maxlevels(
1977 xfs_mount_t *mp)
1978{
1979 mp->m_ag_maxlevels = xfs_btree_compute_maxlevels(mp, mp->m_alloc_mnr,
1980 (mp->m_sb.sb_agblocks + 1) / 2);
1981}
1982
1983
1984
1985
1986
1987
1988
1989xfs_extlen_t
1990xfs_alloc_longest_free_extent(
1991 struct xfs_mount *mp,
1992 struct xfs_perag *pag,
1993 xfs_extlen_t need,
1994 xfs_extlen_t reserved)
1995{
1996 xfs_extlen_t delta = 0;
1997
1998
1999
2000
2001
2002 if (need > pag->pagf_flcount)
2003 delta = need - pag->pagf_flcount;
2004
2005
2006
2007
2008
2009
2010 if (pag->pagf_freeblks - pag->pagf_longest < reserved)
2011 delta += reserved - (pag->pagf_freeblks - pag->pagf_longest);
2012
2013
2014
2015
2016
2017 if (pag->pagf_longest > delta)
2018 return pag->pagf_longest - delta;
2019
2020
2021 return pag->pagf_flcount > 0 || pag->pagf_longest > 0;
2022}
2023
2024unsigned int
2025xfs_alloc_min_freelist(
2026 struct xfs_mount *mp,
2027 struct xfs_perag *pag)
2028{
2029 unsigned int min_free;
2030
2031
2032 min_free = min_t(unsigned int, pag->pagf_levels[XFS_BTNUM_BNOi] + 1,
2033 mp->m_ag_maxlevels);
2034
2035 min_free += min_t(unsigned int, pag->pagf_levels[XFS_BTNUM_CNTi] + 1,
2036 mp->m_ag_maxlevels);
2037
2038 if (xfs_sb_version_hasrmapbt(&mp->m_sb))
2039 min_free += min_t(unsigned int,
2040 pag->pagf_levels[XFS_BTNUM_RMAPi] + 1,
2041 mp->m_rmap_maxlevels);
2042
2043 return min_free;
2044}
2045
2046
2047
2048
2049
2050
2051
2052static bool
2053xfs_alloc_space_available(
2054 struct xfs_alloc_arg *args,
2055 xfs_extlen_t min_free,
2056 int flags)
2057{
2058 struct xfs_perag *pag = args->pag;
2059 xfs_extlen_t longest;
2060 xfs_extlen_t reservation;
2061 int available;
2062
2063 if (flags & XFS_ALLOC_FLAG_FREEING)
2064 return true;
2065
2066 reservation = xfs_ag_resv_needed(pag, args->resv);
2067
2068
2069 longest = xfs_alloc_longest_free_extent(args->mp, pag, min_free,
2070 reservation);
2071 if ((args->minlen + args->alignment + args->minalignslop - 1) > longest)
2072 return false;
2073
2074
2075 available = (int)(pag->pagf_freeblks + pag->pagf_flcount -
2076 reservation - min_free - args->total);
2077 if (available < (int)args->minleft || available <= 0)
2078 return false;
2079
2080 return true;
2081}
2082
2083
2084
2085
2086
2087int
2088xfs_alloc_fix_freelist(
2089 struct xfs_alloc_arg *args,
2090 int flags)
2091{
2092 struct xfs_mount *mp = args->mp;
2093 struct xfs_perag *pag = args->pag;
2094 struct xfs_trans *tp = args->tp;
2095 struct xfs_buf *agbp = NULL;
2096 struct xfs_buf *agflbp = NULL;
2097 struct xfs_alloc_arg targs;
2098 xfs_agblock_t bno;
2099 xfs_extlen_t need;
2100 int error = 0;
2101
2102 if (!pag->pagf_init) {
2103 error = xfs_alloc_read_agf(mp, tp, args->agno, flags, &agbp);
2104 if (error)
2105 goto out_no_agbp;
2106 if (!pag->pagf_init) {
2107 ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
2108 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
2109 goto out_agbp_relse;
2110 }
2111 }
2112
2113
2114
2115
2116
2117
2118 if (pag->pagf_metadata && xfs_alloc_is_userdata(args->datatype) &&
2119 (flags & XFS_ALLOC_FLAG_TRYLOCK)) {
2120 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
2121 goto out_agbp_relse;
2122 }
2123
2124 need = xfs_alloc_min_freelist(mp, pag);
2125 if (!xfs_alloc_space_available(args, need, flags))
2126 goto out_agbp_relse;
2127
2128
2129
2130
2131
2132 if (!agbp) {
2133 error = xfs_alloc_read_agf(mp, tp, args->agno, flags, &agbp);
2134 if (error)
2135 goto out_no_agbp;
2136 if (!agbp) {
2137 ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
2138 ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
2139 goto out_no_agbp;
2140 }
2141 }
2142
2143
2144 need = xfs_alloc_min_freelist(mp, pag);
2145 if (!xfs_alloc_space_available(args, need, flags))
2146 goto out_agbp_relse;
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172 memset(&targs, 0, sizeof(targs));
2173 if (flags & XFS_ALLOC_FLAG_NORMAP)
2174 xfs_rmap_skip_owner_update(&targs.oinfo);
2175 else
2176 xfs_rmap_ag_owner(&targs.oinfo, XFS_RMAP_OWN_AG);
2177 while (!(flags & XFS_ALLOC_FLAG_NOSHRINK) && pag->pagf_flcount > need) {
2178 struct xfs_buf *bp;
2179
2180 error = xfs_alloc_get_freelist(tp, agbp, &bno, 0);
2181 if (error)
2182 goto out_agbp_relse;
2183 error = xfs_free_ag_extent(tp, agbp, args->agno, bno, 1,
2184 &targs.oinfo, XFS_AG_RESV_AGFL);
2185 if (error)
2186 goto out_agbp_relse;
2187 bp = xfs_btree_get_bufs(mp, tp, args->agno, bno, 0);
2188 xfs_trans_binval(tp, bp);
2189 }
2190
2191 targs.tp = tp;
2192 targs.mp = mp;
2193 targs.agbp = agbp;
2194 targs.agno = args->agno;
2195 targs.alignment = targs.minlen = targs.prod = 1;
2196 targs.type = XFS_ALLOCTYPE_THIS_AG;
2197 targs.pag = pag;
2198 error = xfs_alloc_read_agfl(mp, tp, targs.agno, &agflbp);
2199 if (error)
2200 goto out_agbp_relse;
2201
2202
2203 while (pag->pagf_flcount < need) {
2204 targs.agbno = 0;
2205 targs.maxlen = need - pag->pagf_flcount;
2206 targs.resv = XFS_AG_RESV_AGFL;
2207
2208
2209 error = xfs_alloc_ag_vextent(&targs);
2210 if (error)
2211 goto out_agflbp_relse;
2212
2213
2214
2215
2216
2217
2218 if (targs.agbno == NULLAGBLOCK) {
2219 if (flags & XFS_ALLOC_FLAG_FREEING)
2220 break;
2221 goto out_agflbp_relse;
2222 }
2223
2224
2225
2226 for (bno = targs.agbno; bno < targs.agbno + targs.len; bno++) {
2227 error = xfs_alloc_put_freelist(tp, agbp,
2228 agflbp, bno, 0);
2229 if (error)
2230 goto out_agflbp_relse;
2231 }
2232 }
2233 xfs_trans_brelse(tp, agflbp);
2234 args->agbp = agbp;
2235 return 0;
2236
2237out_agflbp_relse:
2238 xfs_trans_brelse(tp, agflbp);
2239out_agbp_relse:
2240 if (agbp)
2241 xfs_trans_brelse(tp, agbp);
2242out_no_agbp:
2243 args->agbp = NULL;
2244 return error;
2245}
2246
2247
2248
2249
2250
2251int
2252xfs_alloc_get_freelist(
2253 xfs_trans_t *tp,
2254 xfs_buf_t *agbp,
2255 xfs_agblock_t *bnop,
2256 int btreeblk)
2257{
2258 xfs_agf_t *agf;
2259 xfs_buf_t *agflbp;
2260 xfs_agblock_t bno;
2261 __be32 *agfl_bno;
2262 int error;
2263 int logflags;
2264 xfs_mount_t *mp = tp->t_mountp;
2265 xfs_perag_t *pag;
2266
2267
2268
2269
2270 agf = XFS_BUF_TO_AGF(agbp);
2271 if (!agf->agf_flcount) {
2272 *bnop = NULLAGBLOCK;
2273 return 0;
2274 }
2275
2276
2277
2278 error = xfs_alloc_read_agfl(mp, tp, be32_to_cpu(agf->agf_seqno),
2279 &agflbp);
2280 if (error)
2281 return error;
2282
2283
2284
2285
2286
2287 agfl_bno = XFS_BUF_TO_AGFL_BNO(mp, agflbp);
2288 bno = be32_to_cpu(agfl_bno[be32_to_cpu(agf->agf_flfirst)]);
2289 be32_add_cpu(&agf->agf_flfirst, 1);
2290 xfs_trans_brelse(tp, agflbp);
2291 if (be32_to_cpu(agf->agf_flfirst) == XFS_AGFL_SIZE(mp))
2292 agf->agf_flfirst = 0;
2293
2294 pag = xfs_perag_get(mp, be32_to_cpu(agf->agf_seqno));
2295 be32_add_cpu(&agf->agf_flcount, -1);
2296 xfs_trans_agflist_delta(tp, -1);
2297 pag->pagf_flcount--;
2298 xfs_perag_put(pag);
2299
2300 logflags = XFS_AGF_FLFIRST | XFS_AGF_FLCOUNT;
2301 if (btreeblk) {
2302 be32_add_cpu(&agf->agf_btreeblks, 1);
2303 pag->pagf_btreeblks++;
2304 logflags |= XFS_AGF_BTREEBLKS;
2305 }
2306
2307 xfs_alloc_log_agf(tp, agbp, logflags);
2308 *bnop = bno;
2309
2310 return 0;
2311}
2312
2313
2314
2315
2316void
2317xfs_alloc_log_agf(
2318 xfs_trans_t *tp,
2319 xfs_buf_t *bp,
2320 int fields)
2321{
2322 int first;
2323 int last;
2324 static const short offsets[] = {
2325 offsetof(xfs_agf_t, agf_magicnum),
2326 offsetof(xfs_agf_t, agf_versionnum),
2327 offsetof(xfs_agf_t, agf_seqno),
2328 offsetof(xfs_agf_t, agf_length),
2329 offsetof(xfs_agf_t, agf_roots[0]),
2330 offsetof(xfs_agf_t, agf_levels[0]),
2331 offsetof(xfs_agf_t, agf_flfirst),
2332 offsetof(xfs_agf_t, agf_fllast),
2333 offsetof(xfs_agf_t, agf_flcount),
2334 offsetof(xfs_agf_t, agf_freeblks),
2335 offsetof(xfs_agf_t, agf_longest),
2336 offsetof(xfs_agf_t, agf_btreeblks),
2337 offsetof(xfs_agf_t, agf_uuid),
2338 offsetof(xfs_agf_t, agf_rmap_blocks),
2339 offsetof(xfs_agf_t, agf_refcount_blocks),
2340 offsetof(xfs_agf_t, agf_refcount_root),
2341 offsetof(xfs_agf_t, agf_refcount_level),
2342
2343 offsetof(xfs_agf_t, agf_spare64),
2344 sizeof(xfs_agf_t)
2345 };
2346
2347 trace_xfs_agf(tp->t_mountp, XFS_BUF_TO_AGF(bp), fields, _RET_IP_);
2348
2349 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_AGF_BUF);
2350
2351 xfs_btree_offsets(fields, offsets, XFS_AGF_NUM_BITS, &first, &last);
2352 xfs_trans_log_buf(tp, bp, (uint)first, (uint)last);
2353}
2354
2355
2356
2357
2358int
2359xfs_alloc_pagf_init(
2360 xfs_mount_t *mp,
2361 xfs_trans_t *tp,
2362 xfs_agnumber_t agno,
2363 int flags)
2364{
2365 xfs_buf_t *bp;
2366 int error;
2367
2368 if ((error = xfs_alloc_read_agf(mp, tp, agno, flags, &bp)))
2369 return error;
2370 if (bp)
2371 xfs_trans_brelse(tp, bp);
2372 return 0;
2373}
2374
2375
2376
2377
2378int
2379xfs_alloc_put_freelist(
2380 xfs_trans_t *tp,
2381 xfs_buf_t *agbp,
2382 xfs_buf_t *agflbp,
2383 xfs_agblock_t bno,
2384 int btreeblk)
2385{
2386 xfs_agf_t *agf;
2387 __be32 *blockp;
2388 int error;
2389 int logflags;
2390 xfs_mount_t *mp;
2391 xfs_perag_t *pag;
2392 __be32 *agfl_bno;
2393 int startoff;
2394
2395 agf = XFS_BUF_TO_AGF(agbp);
2396 mp = tp->t_mountp;
2397
2398 if (!agflbp && (error = xfs_alloc_read_agfl(mp, tp,
2399 be32_to_cpu(agf->agf_seqno), &agflbp)))
2400 return error;
2401 be32_add_cpu(&agf->agf_fllast, 1);
2402 if (be32_to_cpu(agf->agf_fllast) == XFS_AGFL_SIZE(mp))
2403 agf->agf_fllast = 0;
2404
2405 pag = xfs_perag_get(mp, be32_to_cpu(agf->agf_seqno));
2406 be32_add_cpu(&agf->agf_flcount, 1);
2407 xfs_trans_agflist_delta(tp, 1);
2408 pag->pagf_flcount++;
2409
2410 logflags = XFS_AGF_FLLAST | XFS_AGF_FLCOUNT;
2411 if (btreeblk) {
2412 be32_add_cpu(&agf->agf_btreeblks, -1);
2413 pag->pagf_btreeblks--;
2414 logflags |= XFS_AGF_BTREEBLKS;
2415 }
2416 xfs_perag_put(pag);
2417
2418 xfs_alloc_log_agf(tp, agbp, logflags);
2419
2420 ASSERT(be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp));
2421
2422 agfl_bno = XFS_BUF_TO_AGFL_BNO(mp, agflbp);
2423 blockp = &agfl_bno[be32_to_cpu(agf->agf_fllast)];
2424 *blockp = cpu_to_be32(bno);
2425 startoff = (char *)blockp - (char *)agflbp->b_addr;
2426
2427 xfs_alloc_log_agf(tp, agbp, logflags);
2428
2429 xfs_trans_buf_set_type(tp, agflbp, XFS_BLFT_AGFL_BUF);
2430 xfs_trans_log_buf(tp, agflbp, startoff,
2431 startoff + sizeof(xfs_agblock_t) - 1);
2432 return 0;
2433}
2434
2435static bool
2436xfs_agf_verify(
2437 struct xfs_mount *mp,
2438 struct xfs_buf *bp)
2439 {
2440 struct xfs_agf *agf = XFS_BUF_TO_AGF(bp);
2441
2442 if (xfs_sb_version_hascrc(&mp->m_sb)) {
2443 if (!uuid_equal(&agf->agf_uuid, &mp->m_sb.sb_meta_uuid))
2444 return false;
2445 if (!xfs_log_check_lsn(mp,
2446 be64_to_cpu(XFS_BUF_TO_AGF(bp)->agf_lsn)))
2447 return false;
2448 }
2449
2450 if (!(agf->agf_magicnum == cpu_to_be32(XFS_AGF_MAGIC) &&
2451 XFS_AGF_GOOD_VERSION(be32_to_cpu(agf->agf_versionnum)) &&
2452 be32_to_cpu(agf->agf_freeblks) <= be32_to_cpu(agf->agf_length) &&
2453 be32_to_cpu(agf->agf_flfirst) < XFS_AGFL_SIZE(mp) &&
2454 be32_to_cpu(agf->agf_fllast) < XFS_AGFL_SIZE(mp) &&
2455 be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp)))
2456 return false;
2457
2458 if (be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNO]) > XFS_BTREE_MAXLEVELS ||
2459 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNT]) > XFS_BTREE_MAXLEVELS)
2460 return false;
2461
2462 if (xfs_sb_version_hasrmapbt(&mp->m_sb) &&
2463 be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAP]) > XFS_BTREE_MAXLEVELS)
2464 return false;
2465
2466
2467
2468
2469
2470
2471
2472 if (bp->b_pag && be32_to_cpu(agf->agf_seqno) != bp->b_pag->pag_agno)
2473 return false;
2474
2475 if (xfs_sb_version_haslazysbcount(&mp->m_sb) &&
2476 be32_to_cpu(agf->agf_btreeblks) > be32_to_cpu(agf->agf_length))
2477 return false;
2478
2479 if (xfs_sb_version_hasreflink(&mp->m_sb) &&
2480 be32_to_cpu(agf->agf_refcount_level) > XFS_BTREE_MAXLEVELS)
2481 return false;
2482
2483 return true;;
2484
2485}
2486
2487static void
2488xfs_agf_read_verify(
2489 struct xfs_buf *bp)
2490{
2491 struct xfs_mount *mp = bp->b_target->bt_mount;
2492
2493 if (xfs_sb_version_hascrc(&mp->m_sb) &&
2494 !xfs_buf_verify_cksum(bp, XFS_AGF_CRC_OFF))
2495 xfs_buf_ioerror(bp, -EFSBADCRC);
2496 else if (XFS_TEST_ERROR(!xfs_agf_verify(mp, bp), mp,
2497 XFS_ERRTAG_ALLOC_READ_AGF,
2498 XFS_RANDOM_ALLOC_READ_AGF))
2499 xfs_buf_ioerror(bp, -EFSCORRUPTED);
2500
2501 if (bp->b_error)
2502 xfs_verifier_error(bp);
2503}
2504
2505static void
2506xfs_agf_write_verify(
2507 struct xfs_buf *bp)
2508{
2509 struct xfs_mount *mp = bp->b_target->bt_mount;
2510 struct xfs_buf_log_item *bip = bp->b_fspriv;
2511
2512 if (!xfs_agf_verify(mp, bp)) {
2513 xfs_buf_ioerror(bp, -EFSCORRUPTED);
2514 xfs_verifier_error(bp);
2515 return;
2516 }
2517
2518 if (!xfs_sb_version_hascrc(&mp->m_sb))
2519 return;
2520
2521 if (bip)
2522 XFS_BUF_TO_AGF(bp)->agf_lsn = cpu_to_be64(bip->bli_item.li_lsn);
2523
2524 xfs_buf_update_cksum(bp, XFS_AGF_CRC_OFF);
2525}
2526
2527const struct xfs_buf_ops xfs_agf_buf_ops = {
2528 .name = "xfs_agf",
2529 .verify_read = xfs_agf_read_verify,
2530 .verify_write = xfs_agf_write_verify,
2531};
2532
2533
2534
2535
2536int
2537xfs_read_agf(
2538 struct xfs_mount *mp,
2539 struct xfs_trans *tp,
2540 xfs_agnumber_t agno,
2541 int flags,
2542 struct xfs_buf **bpp)
2543{
2544 int error;
2545
2546 trace_xfs_read_agf(mp, agno);
2547
2548 ASSERT(agno != NULLAGNUMBER);
2549 error = xfs_trans_read_buf(
2550 mp, tp, mp->m_ddev_targp,
2551 XFS_AG_DADDR(mp, agno, XFS_AGF_DADDR(mp)),
2552 XFS_FSS_TO_BB(mp, 1), flags, bpp, &xfs_agf_buf_ops);
2553 if (error)
2554 return error;
2555 if (!*bpp)
2556 return 0;
2557
2558 ASSERT(!(*bpp)->b_error);
2559 xfs_buf_set_ref(*bpp, XFS_AGF_REF);
2560 return 0;
2561}
2562
2563
2564
2565
2566int
2567xfs_alloc_read_agf(
2568 struct xfs_mount *mp,
2569 struct xfs_trans *tp,
2570 xfs_agnumber_t agno,
2571 int flags,
2572 struct xfs_buf **bpp)
2573{
2574 struct xfs_agf *agf;
2575 struct xfs_perag *pag;
2576 int error;
2577
2578 trace_xfs_alloc_read_agf(mp, agno);
2579
2580 ASSERT(agno != NULLAGNUMBER);
2581 error = xfs_read_agf(mp, tp, agno,
2582 (flags & XFS_ALLOC_FLAG_TRYLOCK) ? XBF_TRYLOCK : 0,
2583 bpp);
2584 if (error)
2585 return error;
2586 if (!*bpp)
2587 return 0;
2588 ASSERT(!(*bpp)->b_error);
2589
2590 agf = XFS_BUF_TO_AGF(*bpp);
2591 pag = xfs_perag_get(mp, agno);
2592 if (!pag->pagf_init) {
2593 pag->pagf_freeblks = be32_to_cpu(agf->agf_freeblks);
2594 pag->pagf_btreeblks = be32_to_cpu(agf->agf_btreeblks);
2595 pag->pagf_flcount = be32_to_cpu(agf->agf_flcount);
2596 pag->pagf_longest = be32_to_cpu(agf->agf_longest);
2597 pag->pagf_levels[XFS_BTNUM_BNOi] =
2598 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]);
2599 pag->pagf_levels[XFS_BTNUM_CNTi] =
2600 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]);
2601 pag->pagf_levels[XFS_BTNUM_RMAPi] =
2602 be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAPi]);
2603 pag->pagf_refcount_level = be32_to_cpu(agf->agf_refcount_level);
2604 spin_lock_init(&pag->pagb_lock);
2605 pag->pagb_count = 0;
2606 pag->pagb_tree = RB_ROOT;
2607 pag->pagf_init = 1;
2608 }
2609#ifdef DEBUG
2610 else if (!XFS_FORCED_SHUTDOWN(mp)) {
2611 ASSERT(pag->pagf_freeblks == be32_to_cpu(agf->agf_freeblks));
2612 ASSERT(pag->pagf_btreeblks == be32_to_cpu(agf->agf_btreeblks));
2613 ASSERT(pag->pagf_flcount == be32_to_cpu(agf->agf_flcount));
2614 ASSERT(pag->pagf_longest == be32_to_cpu(agf->agf_longest));
2615 ASSERT(pag->pagf_levels[XFS_BTNUM_BNOi] ==
2616 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]));
2617 ASSERT(pag->pagf_levels[XFS_BTNUM_CNTi] ==
2618 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]));
2619 }
2620#endif
2621 xfs_perag_put(pag);
2622 return 0;
2623}
2624
2625
2626
2627
2628
2629
2630int
2631xfs_alloc_vextent(
2632 xfs_alloc_arg_t *args)
2633{
2634 xfs_agblock_t agsize;
2635 int error;
2636 int flags;
2637 xfs_extlen_t minleft;
2638 xfs_mount_t *mp;
2639 xfs_agnumber_t sagno;
2640 xfs_alloctype_t type;
2641 int bump_rotor = 0;
2642 int no_min = 0;
2643 xfs_agnumber_t rotorstep = xfs_rotorstep;
2644
2645 mp = args->mp;
2646 type = args->otype = args->type;
2647 args->agbno = NULLAGBLOCK;
2648
2649
2650
2651
2652
2653 agsize = mp->m_sb.sb_agblocks;
2654 if (args->maxlen > agsize)
2655 args->maxlen = agsize;
2656 if (args->alignment == 0)
2657 args->alignment = 1;
2658 ASSERT(XFS_FSB_TO_AGNO(mp, args->fsbno) < mp->m_sb.sb_agcount);
2659 ASSERT(XFS_FSB_TO_AGBNO(mp, args->fsbno) < agsize);
2660 ASSERT(args->minlen <= args->maxlen);
2661 ASSERT(args->minlen <= agsize);
2662 ASSERT(args->mod < args->prod);
2663 if (XFS_FSB_TO_AGNO(mp, args->fsbno) >= mp->m_sb.sb_agcount ||
2664 XFS_FSB_TO_AGBNO(mp, args->fsbno) >= agsize ||
2665 args->minlen > args->maxlen || args->minlen > agsize ||
2666 args->mod >= args->prod) {
2667 args->fsbno = NULLFSBLOCK;
2668 trace_xfs_alloc_vextent_badargs(args);
2669 return 0;
2670 }
2671 minleft = args->minleft;
2672
2673 switch (type) {
2674 case XFS_ALLOCTYPE_THIS_AG:
2675 case XFS_ALLOCTYPE_NEAR_BNO:
2676 case XFS_ALLOCTYPE_THIS_BNO:
2677
2678
2679
2680 args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2681 args->pag = xfs_perag_get(mp, args->agno);
2682 args->minleft = 0;
2683 error = xfs_alloc_fix_freelist(args, 0);
2684 args->minleft = minleft;
2685 if (error) {
2686 trace_xfs_alloc_vextent_nofix(args);
2687 goto error0;
2688 }
2689 if (!args->agbp) {
2690 trace_xfs_alloc_vextent_noagbp(args);
2691 break;
2692 }
2693 args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
2694 if ((error = xfs_alloc_ag_vextent(args)))
2695 goto error0;
2696 break;
2697 case XFS_ALLOCTYPE_START_BNO:
2698
2699
2700
2701
2702 if ((args->datatype & XFS_ALLOC_INITIAL_USER_DATA) &&
2703 (mp->m_flags & XFS_MOUNT_32BITINODES)) {
2704 args->fsbno = XFS_AGB_TO_FSB(mp,
2705 ((mp->m_agfrotor / rotorstep) %
2706 mp->m_sb.sb_agcount), 0);
2707 bump_rotor = 1;
2708 }
2709 args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
2710 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2711
2712 case XFS_ALLOCTYPE_ANY_AG:
2713 case XFS_ALLOCTYPE_START_AG:
2714 case XFS_ALLOCTYPE_FIRST_AG:
2715
2716
2717
2718 if (type == XFS_ALLOCTYPE_ANY_AG) {
2719
2720
2721
2722 args->agno = sagno = (mp->m_agfrotor / rotorstep) %
2723 mp->m_sb.sb_agcount;
2724 args->type = XFS_ALLOCTYPE_THIS_AG;
2725 flags = XFS_ALLOC_FLAG_TRYLOCK;
2726 } else if (type == XFS_ALLOCTYPE_FIRST_AG) {
2727
2728
2729
2730 args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2731 args->type = XFS_ALLOCTYPE_THIS_AG;
2732 sagno = 0;
2733 flags = 0;
2734 } else {
2735 if (type == XFS_ALLOCTYPE_START_AG)
2736 args->type = XFS_ALLOCTYPE_THIS_AG;
2737
2738
2739
2740 args->agno = sagno = XFS_FSB_TO_AGNO(mp, args->fsbno);
2741 flags = XFS_ALLOC_FLAG_TRYLOCK;
2742 }
2743
2744
2745
2746
2747 for (;;) {
2748 args->pag = xfs_perag_get(mp, args->agno);
2749 if (no_min) args->minleft = 0;
2750 error = xfs_alloc_fix_freelist(args, flags);
2751 args->minleft = minleft;
2752 if (error) {
2753 trace_xfs_alloc_vextent_nofix(args);
2754 goto error0;
2755 }
2756
2757
2758
2759 if (args->agbp) {
2760 if ((error = xfs_alloc_ag_vextent(args)))
2761 goto error0;
2762 break;
2763 }
2764
2765 trace_xfs_alloc_vextent_loopfailed(args);
2766
2767
2768
2769
2770 if (args->agno == sagno &&
2771 type == XFS_ALLOCTYPE_START_BNO)
2772 args->type = XFS_ALLOCTYPE_THIS_AG;
2773
2774
2775
2776
2777
2778
2779
2780 if (++(args->agno) == mp->m_sb.sb_agcount) {
2781 if (args->firstblock != NULLFSBLOCK)
2782 args->agno = sagno;
2783 else
2784 args->agno = 0;
2785 }
2786
2787
2788
2789
2790 if (args->agno == sagno) {
2791 if (no_min == 1) {
2792 args->agbno = NULLAGBLOCK;
2793 trace_xfs_alloc_vextent_allfailed(args);
2794 break;
2795 }
2796 if (flags == 0) {
2797 no_min = 1;
2798 } else {
2799 flags = 0;
2800 if (type == XFS_ALLOCTYPE_START_BNO) {
2801 args->agbno = XFS_FSB_TO_AGBNO(mp,
2802 args->fsbno);
2803 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2804 }
2805 }
2806 }
2807 xfs_perag_put(args->pag);
2808 }
2809 if (bump_rotor || (type == XFS_ALLOCTYPE_ANY_AG)) {
2810 if (args->agno == sagno)
2811 mp->m_agfrotor = (mp->m_agfrotor + 1) %
2812 (mp->m_sb.sb_agcount * rotorstep);
2813 else
2814 mp->m_agfrotor = (args->agno * rotorstep + 1) %
2815 (mp->m_sb.sb_agcount * rotorstep);
2816 }
2817 break;
2818 default:
2819 ASSERT(0);
2820
2821 }
2822 if (args->agbno == NULLAGBLOCK)
2823 args->fsbno = NULLFSBLOCK;
2824 else {
2825 args->fsbno = XFS_AGB_TO_FSB(mp, args->agno, args->agbno);
2826#ifdef DEBUG
2827 ASSERT(args->len >= args->minlen);
2828 ASSERT(args->len <= args->maxlen);
2829 ASSERT(args->agbno % args->alignment == 0);
2830 XFS_AG_CHECK_DADDR(mp, XFS_FSB_TO_DADDR(mp, args->fsbno),
2831 args->len);
2832#endif
2833
2834
2835 if (args->datatype & XFS_ALLOC_USERDATA_ZERO) {
2836 error = xfs_zero_extent(args->ip, args->fsbno, args->len);
2837 if (error)
2838 goto error0;
2839 }
2840
2841 }
2842 xfs_perag_put(args->pag);
2843 return 0;
2844error0:
2845 xfs_perag_put(args->pag);
2846 return error;
2847}
2848
2849
2850int
2851xfs_free_extent_fix_freelist(
2852 struct xfs_trans *tp,
2853 xfs_agnumber_t agno,
2854 struct xfs_buf **agbp)
2855{
2856 struct xfs_alloc_arg args;
2857 int error;
2858
2859 memset(&args, 0, sizeof(struct xfs_alloc_arg));
2860 args.tp = tp;
2861 args.mp = tp->t_mountp;
2862 args.agno = agno;
2863
2864
2865
2866
2867
2868 if (args.agno >= args.mp->m_sb.sb_agcount)
2869 return -EFSCORRUPTED;
2870
2871 args.pag = xfs_perag_get(args.mp, args.agno);
2872 ASSERT(args.pag);
2873
2874 error = xfs_alloc_fix_freelist(&args, XFS_ALLOC_FLAG_FREEING);
2875 if (error)
2876 goto out;
2877
2878 *agbp = args.agbp;
2879out:
2880 xfs_perag_put(args.pag);
2881 return error;
2882}
2883
2884
2885
2886
2887
2888
2889int
2890xfs_free_extent(
2891 struct xfs_trans *tp,
2892 xfs_fsblock_t bno,
2893 xfs_extlen_t len,
2894 struct xfs_owner_info *oinfo,
2895 enum xfs_ag_resv_type type)
2896{
2897 struct xfs_mount *mp = tp->t_mountp;
2898 struct xfs_buf *agbp;
2899 xfs_agnumber_t agno = XFS_FSB_TO_AGNO(mp, bno);
2900 xfs_agblock_t agbno = XFS_FSB_TO_AGBNO(mp, bno);
2901 int error;
2902
2903 ASSERT(len != 0);
2904 ASSERT(type != XFS_AG_RESV_AGFL);
2905
2906 if (XFS_TEST_ERROR(false, mp,
2907 XFS_ERRTAG_FREE_EXTENT,
2908 XFS_RANDOM_FREE_EXTENT))
2909 return -EIO;
2910
2911 error = xfs_free_extent_fix_freelist(tp, agno, &agbp);
2912 if (error)
2913 return error;
2914
2915 XFS_WANT_CORRUPTED_GOTO(mp, agbno < mp->m_sb.sb_agblocks, err);
2916
2917
2918 XFS_WANT_CORRUPTED_GOTO(mp,
2919 agbno + len <= be32_to_cpu(XFS_BUF_TO_AGF(agbp)->agf_length),
2920 err);
2921
2922 error = xfs_free_ag_extent(tp, agbp, agno, agbno, len, oinfo, type);
2923 if (error)
2924 goto err;
2925
2926 xfs_extent_busy_insert(tp, agno, agbno, len, 0);
2927 return 0;
2928
2929err:
2930 xfs_trans_brelse(tp, agbp);
2931 return error;
2932}
2933