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