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_mount.h"
28#include "xfs_bmap_btree.h"
29#include "xfs_alloc_btree.h"
30#include "xfs_ialloc_btree.h"
31#include "xfs_dinode.h"
32#include "xfs_inode.h"
33#include "xfs_btree.h"
34#include "xfs_ialloc.h"
35#include "xfs_alloc.h"
36#include "xfs_rtalloc.h"
37#include "xfs_error.h"
38#include "xfs_bmap.h"
39#include "xfs_cksum.h"
40#include "xfs_buf_item.h"
41#include "xfs_icreate_item.h"
42
43
44
45
46
47static inline int
48xfs_ialloc_cluster_alignment(
49 xfs_alloc_arg_t *args)
50{
51 if (xfs_sb_version_hasalign(&args->mp->m_sb) &&
52 args->mp->m_sb.sb_inoalignmt >=
53 XFS_B_TO_FSBT(args->mp, XFS_INODE_CLUSTER_SIZE(args->mp)))
54 return args->mp->m_sb.sb_inoalignmt;
55 return 1;
56}
57
58
59
60
61int
62xfs_inobt_lookup(
63 struct xfs_btree_cur *cur,
64 xfs_agino_t ino,
65 xfs_lookup_t dir,
66 int *stat)
67{
68 cur->bc_rec.i.ir_startino = ino;
69 cur->bc_rec.i.ir_freecount = 0;
70 cur->bc_rec.i.ir_free = 0;
71 return xfs_btree_lookup(cur, dir, stat);
72}
73
74
75
76
77
78STATIC int
79xfs_inobt_update(
80 struct xfs_btree_cur *cur,
81 xfs_inobt_rec_incore_t *irec)
82{
83 union xfs_btree_rec rec;
84
85 rec.inobt.ir_startino = cpu_to_be32(irec->ir_startino);
86 rec.inobt.ir_freecount = cpu_to_be32(irec->ir_freecount);
87 rec.inobt.ir_free = cpu_to_be64(irec->ir_free);
88 return xfs_btree_update(cur, &rec);
89}
90
91
92
93
94int
95xfs_inobt_get_rec(
96 struct xfs_btree_cur *cur,
97 xfs_inobt_rec_incore_t *irec,
98 int *stat)
99{
100 union xfs_btree_rec *rec;
101 int error;
102
103 error = xfs_btree_get_rec(cur, &rec, stat);
104 if (!error && *stat == 1) {
105 irec->ir_startino = be32_to_cpu(rec->inobt.ir_startino);
106 irec->ir_freecount = be32_to_cpu(rec->inobt.ir_freecount);
107 irec->ir_free = be64_to_cpu(rec->inobt.ir_free);
108 }
109 return error;
110}
111
112
113
114
115#ifdef DEBUG
116STATIC int
117xfs_check_agi_freecount(
118 struct xfs_btree_cur *cur,
119 struct xfs_agi *agi)
120{
121 if (cur->bc_nlevels == 1) {
122 xfs_inobt_rec_incore_t rec;
123 int freecount = 0;
124 int error;
125 int i;
126
127 error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
128 if (error)
129 return error;
130
131 do {
132 error = xfs_inobt_get_rec(cur, &rec, &i);
133 if (error)
134 return error;
135
136 if (i) {
137 freecount += rec.ir_freecount;
138 error = xfs_btree_increment(cur, 0, &i);
139 if (error)
140 return error;
141 }
142 } while (i == 1);
143
144 if (!XFS_FORCED_SHUTDOWN(cur->bc_mp))
145 ASSERT(freecount == be32_to_cpu(agi->agi_freecount));
146 }
147 return 0;
148}
149#else
150#define xfs_check_agi_freecount(cur, agi) 0
151#endif
152
153
154
155
156
157
158
159int
160xfs_ialloc_inode_init(
161 struct xfs_mount *mp,
162 struct xfs_trans *tp,
163 struct list_head *buffer_list,
164 xfs_agnumber_t agno,
165 xfs_agblock_t agbno,
166 xfs_agblock_t length,
167 unsigned int gen)
168{
169 struct xfs_buf *fbuf;
170 struct xfs_dinode *free;
171 int blks_per_cluster, nbufs, ninodes;
172 int version;
173 int i, j;
174 xfs_daddr_t d;
175 xfs_ino_t ino = 0;
176
177
178
179
180
181
182 if (mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) {
183 blks_per_cluster = 1;
184 nbufs = length;
185 ninodes = mp->m_sb.sb_inopblock;
186 } else {
187 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) /
188 mp->m_sb.sb_blocksize;
189 nbufs = length / blks_per_cluster;
190 ninodes = blks_per_cluster * mp->m_sb.sb_inopblock;
191 }
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212 if (xfs_sb_version_hascrc(&mp->m_sb)) {
213 version = 3;
214 ino = XFS_AGINO_TO_INO(mp, agno,
215 XFS_OFFBNO_TO_AGINO(mp, agbno, 0));
216
217
218
219
220
221
222
223
224
225 if (tp)
226 xfs_icreate_log(tp, agno, agbno, XFS_IALLOC_INODES(mp),
227 mp->m_sb.sb_inodesize, length, gen);
228 } else if (xfs_sb_version_hasnlink(&mp->m_sb))
229 version = 2;
230 else
231 version = 1;
232
233 for (j = 0; j < nbufs; j++) {
234
235
236
237 d = XFS_AGB_TO_DADDR(mp, agno, agbno + (j * blks_per_cluster));
238 fbuf = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
239 mp->m_bsize * blks_per_cluster,
240 XBF_UNMAPPED);
241 if (!fbuf)
242 return ENOMEM;
243
244
245 fbuf->b_ops = &xfs_inode_buf_ops;
246 xfs_buf_zero(fbuf, 0, BBTOB(fbuf->b_length));
247 for (i = 0; i < ninodes; i++) {
248 int ioffset = i << mp->m_sb.sb_inodelog;
249 uint isize = xfs_dinode_size(version);
250
251 free = xfs_make_iptr(mp, fbuf, i);
252 free->di_magic = cpu_to_be16(XFS_DINODE_MAGIC);
253 free->di_version = version;
254 free->di_gen = cpu_to_be32(gen);
255 free->di_next_unlinked = cpu_to_be32(NULLAGINO);
256
257 if (version == 3) {
258 free->di_ino = cpu_to_be64(ino);
259 ino++;
260 uuid_copy(&free->di_uuid, &mp->m_sb.sb_uuid);
261 xfs_dinode_calc_crc(mp, free);
262 } else if (tp) {
263
264 xfs_trans_log_buf(tp, fbuf, ioffset,
265 ioffset + isize - 1);
266 }
267 }
268
269 if (tp) {
270
271
272
273
274
275
276
277
278 xfs_trans_inode_alloc_buf(tp, fbuf);
279 if (version == 3) {
280
281
282
283
284
285
286 xfs_trans_ordered_buf(tp, fbuf);
287 xfs_trans_log_buf(tp, fbuf, 0,
288 BBTOB(fbuf->b_length) - 1);
289 }
290 } else {
291 fbuf->b_flags |= XBF_DONE;
292 xfs_buf_delwri_queue(fbuf, buffer_list);
293 xfs_buf_relse(fbuf);
294 }
295 }
296 return 0;
297}
298
299
300
301
302
303STATIC int
304xfs_ialloc_ag_alloc(
305 xfs_trans_t *tp,
306 xfs_buf_t *agbp,
307 int *alloc)
308{
309 xfs_agi_t *agi;
310 xfs_alloc_arg_t args;
311 xfs_btree_cur_t *cur;
312 xfs_agnumber_t agno;
313 int error;
314 int i;
315 xfs_agino_t newino;
316 xfs_agino_t newlen;
317 xfs_agino_t thisino;
318 int isaligned = 0;
319
320 struct xfs_perag *pag;
321
322 memset(&args, 0, sizeof(args));
323 args.tp = tp;
324 args.mp = tp->t_mountp;
325
326
327
328
329
330 newlen = XFS_IALLOC_INODES(args.mp);
331 if (args.mp->m_maxicount &&
332 args.mp->m_sb.sb_icount + newlen > args.mp->m_maxicount)
333 return XFS_ERROR(ENOSPC);
334 args.minlen = args.maxlen = XFS_IALLOC_BLOCKS(args.mp);
335
336
337
338
339
340 agi = XFS_BUF_TO_AGI(agbp);
341 newino = be32_to_cpu(agi->agi_newino);
342 agno = be32_to_cpu(agi->agi_seqno);
343 args.agbno = XFS_AGINO_TO_AGBNO(args.mp, newino) +
344 XFS_IALLOC_BLOCKS(args.mp);
345 if (likely(newino != NULLAGINO &&
346 (args.agbno < be32_to_cpu(agi->agi_length)))) {
347 args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
348 args.type = XFS_ALLOCTYPE_THIS_BNO;
349 args.prod = 1;
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364 args.alignment = 1;
365 args.minalignslop = xfs_ialloc_cluster_alignment(&args) - 1;
366
367
368 args.minleft = args.mp->m_in_maxlevels - 1;
369 if ((error = xfs_alloc_vextent(&args)))
370 return error;
371 } else
372 args.fsbno = NULLFSBLOCK;
373
374 if (unlikely(args.fsbno == NULLFSBLOCK)) {
375
376
377
378
379
380
381
382
383 isaligned = 0;
384 if (args.mp->m_sinoalign) {
385 ASSERT(!(args.mp->m_flags & XFS_MOUNT_NOALIGN));
386 args.alignment = args.mp->m_dalign;
387 isaligned = 1;
388 } else
389 args.alignment = xfs_ialloc_cluster_alignment(&args);
390
391
392
393
394
395 args.agbno = be32_to_cpu(agi->agi_root);
396 args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
397
398
399
400 args.type = XFS_ALLOCTYPE_NEAR_BNO;
401 args.prod = 1;
402
403
404
405 args.minleft = args.mp->m_in_maxlevels - 1;
406 if ((error = xfs_alloc_vextent(&args)))
407 return error;
408 }
409
410
411
412
413
414 if (isaligned && args.fsbno == NULLFSBLOCK) {
415 args.type = XFS_ALLOCTYPE_NEAR_BNO;
416 args.agbno = be32_to_cpu(agi->agi_root);
417 args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
418 args.alignment = xfs_ialloc_cluster_alignment(&args);
419 if ((error = xfs_alloc_vextent(&args)))
420 return error;
421 }
422
423 if (args.fsbno == NULLFSBLOCK) {
424 *alloc = 0;
425 return 0;
426 }
427 ASSERT(args.len == args.minlen);
428
429
430
431
432
433
434
435
436
437
438 error = xfs_ialloc_inode_init(args.mp, tp, NULL, agno, args.agbno,
439 args.len, prandom_u32());
440
441 if (error)
442 return error;
443
444
445
446 newino = XFS_OFFBNO_TO_AGINO(args.mp, args.agbno, 0);
447 be32_add_cpu(&agi->agi_count, newlen);
448 be32_add_cpu(&agi->agi_freecount, newlen);
449 pag = xfs_perag_get(args.mp, agno);
450 pag->pagi_freecount += newlen;
451 xfs_perag_put(pag);
452 agi->agi_newino = cpu_to_be32(newino);
453
454
455
456
457 cur = xfs_inobt_init_cursor(args.mp, tp, agbp, agno);
458 for (thisino = newino;
459 thisino < newino + newlen;
460 thisino += XFS_INODES_PER_CHUNK) {
461 cur->bc_rec.i.ir_startino = thisino;
462 cur->bc_rec.i.ir_freecount = XFS_INODES_PER_CHUNK;
463 cur->bc_rec.i.ir_free = XFS_INOBT_ALL_FREE;
464 error = xfs_btree_lookup(cur, XFS_LOOKUP_EQ, &i);
465 if (error) {
466 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
467 return error;
468 }
469 ASSERT(i == 0);
470 error = xfs_btree_insert(cur, &i);
471 if (error) {
472 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
473 return error;
474 }
475 ASSERT(i == 1);
476 }
477 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
478
479
480
481 xfs_ialloc_log_agi(tp, agbp,
482 XFS_AGI_COUNT | XFS_AGI_FREECOUNT | XFS_AGI_NEWINO);
483
484
485
486 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, (long)newlen);
487 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, (long)newlen);
488 *alloc = 1;
489 return 0;
490}
491
492STATIC xfs_agnumber_t
493xfs_ialloc_next_ag(
494 xfs_mount_t *mp)
495{
496 xfs_agnumber_t agno;
497
498 spin_lock(&mp->m_agirotor_lock);
499 agno = mp->m_agirotor;
500 if (++mp->m_agirotor >= mp->m_maxagi)
501 mp->m_agirotor = 0;
502 spin_unlock(&mp->m_agirotor_lock);
503
504 return agno;
505}
506
507
508
509
510
511STATIC xfs_agnumber_t
512xfs_ialloc_ag_select(
513 xfs_trans_t *tp,
514 xfs_ino_t parent,
515 umode_t mode,
516 int okalloc)
517{
518 xfs_agnumber_t agcount;
519 xfs_agnumber_t agno;
520 int flags;
521 xfs_extlen_t ineed;
522 xfs_extlen_t longest = 0;
523 xfs_mount_t *mp;
524 int needspace;
525 xfs_perag_t *pag;
526 xfs_agnumber_t pagno;
527 int error;
528
529
530
531
532
533 needspace = S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode);
534 mp = tp->t_mountp;
535 agcount = mp->m_maxagi;
536 if (S_ISDIR(mode))
537 pagno = xfs_ialloc_next_ag(mp);
538 else {
539 pagno = XFS_INO_TO_AGNO(mp, parent);
540 if (pagno >= agcount)
541 pagno = 0;
542 }
543
544 ASSERT(pagno < agcount);
545
546
547
548
549
550
551
552
553 agno = pagno;
554 flags = XFS_ALLOC_FLAG_TRYLOCK;
555 for (;;) {
556 pag = xfs_perag_get(mp, agno);
557 if (!pag->pagi_inodeok) {
558 xfs_ialloc_next_ag(mp);
559 goto nextag;
560 }
561
562 if (!pag->pagi_init) {
563 error = xfs_ialloc_pagi_init(mp, tp, agno);
564 if (error)
565 goto nextag;
566 }
567
568 if (pag->pagi_freecount) {
569 xfs_perag_put(pag);
570 return agno;
571 }
572
573 if (!okalloc)
574 goto nextag;
575
576 if (!pag->pagf_init) {
577 error = xfs_alloc_pagf_init(mp, tp, agno, flags);
578 if (error)
579 goto nextag;
580 }
581
582
583
584
585
586 ineed = XFS_IALLOC_BLOCKS(mp);
587 longest = pag->pagf_longest;
588 if (!longest)
589 longest = pag->pagf_flcount > 0;
590
591 if (pag->pagf_freeblks >= needspace + ineed &&
592 longest >= ineed) {
593 xfs_perag_put(pag);
594 return agno;
595 }
596nextag:
597 xfs_perag_put(pag);
598
599
600
601
602 if (XFS_FORCED_SHUTDOWN(mp))
603 return NULLAGNUMBER;
604 agno++;
605 if (agno >= agcount)
606 agno = 0;
607 if (agno == pagno) {
608 if (flags == 0)
609 return NULLAGNUMBER;
610 flags = 0;
611 }
612 }
613}
614
615
616
617
618STATIC int
619xfs_ialloc_next_rec(
620 struct xfs_btree_cur *cur,
621 xfs_inobt_rec_incore_t *rec,
622 int *done,
623 int left)
624{
625 int error;
626 int i;
627
628 if (left)
629 error = xfs_btree_decrement(cur, 0, &i);
630 else
631 error = xfs_btree_increment(cur, 0, &i);
632
633 if (error)
634 return error;
635 *done = !i;
636 if (i) {
637 error = xfs_inobt_get_rec(cur, rec, &i);
638 if (error)
639 return error;
640 XFS_WANT_CORRUPTED_RETURN(i == 1);
641 }
642
643 return 0;
644}
645
646STATIC int
647xfs_ialloc_get_rec(
648 struct xfs_btree_cur *cur,
649 xfs_agino_t agino,
650 xfs_inobt_rec_incore_t *rec,
651 int *done)
652{
653 int error;
654 int i;
655
656 error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_EQ, &i);
657 if (error)
658 return error;
659 *done = !i;
660 if (i) {
661 error = xfs_inobt_get_rec(cur, rec, &i);
662 if (error)
663 return error;
664 XFS_WANT_CORRUPTED_RETURN(i == 1);
665 }
666
667 return 0;
668}
669
670
671
672
673
674
675
676STATIC int
677xfs_dialloc_ag(
678 struct xfs_trans *tp,
679 struct xfs_buf *agbp,
680 xfs_ino_t parent,
681 xfs_ino_t *inop)
682{
683 struct xfs_mount *mp = tp->t_mountp;
684 struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
685 xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno);
686 xfs_agnumber_t pagno = XFS_INO_TO_AGNO(mp, parent);
687 xfs_agino_t pagino = XFS_INO_TO_AGINO(mp, parent);
688 struct xfs_perag *pag;
689 struct xfs_btree_cur *cur, *tcur;
690 struct xfs_inobt_rec_incore rec, trec;
691 xfs_ino_t ino;
692 int error;
693 int offset;
694 int i, j;
695
696 pag = xfs_perag_get(mp, agno);
697
698 ASSERT(pag->pagi_init);
699 ASSERT(pag->pagi_inodeok);
700 ASSERT(pag->pagi_freecount > 0);
701
702 restart_pagno:
703 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno);
704
705
706
707
708 if (!pagino)
709 pagino = be32_to_cpu(agi->agi_newino);
710
711 error = xfs_check_agi_freecount(cur, agi);
712 if (error)
713 goto error0;
714
715
716
717
718 if (pagno == agno) {
719 int doneleft;
720 int doneright;
721 int searchdistance = 10;
722
723 error = xfs_inobt_lookup(cur, pagino, XFS_LOOKUP_LE, &i);
724 if (error)
725 goto error0;
726 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
727
728 error = xfs_inobt_get_rec(cur, &rec, &j);
729 if (error)
730 goto error0;
731 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
732
733 if (rec.ir_freecount > 0) {
734
735
736
737
738 goto alloc_inode;
739 }
740
741
742
743
744
745
746
747 error = xfs_btree_dup_cursor(cur, &tcur);
748 if (error)
749 goto error0;
750
751
752
753
754 if (pagino != NULLAGINO &&
755 pag->pagl_pagino == pagino &&
756 pag->pagl_leftrec != NULLAGINO &&
757 pag->pagl_rightrec != NULLAGINO) {
758 error = xfs_ialloc_get_rec(tcur, pag->pagl_leftrec,
759 &trec, &doneleft);
760 if (error)
761 goto error1;
762
763 error = xfs_ialloc_get_rec(cur, pag->pagl_rightrec,
764 &rec, &doneright);
765 if (error)
766 goto error1;
767 } else {
768
769 error = xfs_ialloc_next_rec(tcur, &trec, &doneleft, 1);
770 if (error)
771 goto error1;
772
773
774 error = xfs_ialloc_next_rec(cur, &rec, &doneright, 0);
775 if (error)
776 goto error1;
777 }
778
779
780
781
782 while (!doneleft || !doneright) {
783 int useleft;
784
785 if (!--searchdistance) {
786
787
788
789
790 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
791 pag->pagl_leftrec = trec.ir_startino;
792 pag->pagl_rightrec = rec.ir_startino;
793 pag->pagl_pagino = pagino;
794 goto newino;
795 }
796
797
798 if (!doneleft && !doneright) {
799 useleft = pagino -
800 (trec.ir_startino + XFS_INODES_PER_CHUNK - 1) <
801 rec.ir_startino - pagino;
802 } else {
803 useleft = !doneleft;
804 }
805
806
807 if (useleft && trec.ir_freecount) {
808 rec = trec;
809 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
810 cur = tcur;
811
812 pag->pagl_leftrec = trec.ir_startino;
813 pag->pagl_rightrec = rec.ir_startino;
814 pag->pagl_pagino = pagino;
815 goto alloc_inode;
816 }
817
818
819 if (!useleft && rec.ir_freecount) {
820 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
821
822 pag->pagl_leftrec = trec.ir_startino;
823 pag->pagl_rightrec = rec.ir_startino;
824 pag->pagl_pagino = pagino;
825 goto alloc_inode;
826 }
827
828
829 if (useleft) {
830 error = xfs_ialloc_next_rec(tcur, &trec,
831 &doneleft, 1);
832 } else {
833 error = xfs_ialloc_next_rec(cur, &rec,
834 &doneright, 0);
835 }
836 if (error)
837 goto error1;
838 }
839
840
841
842
843
844
845
846
847 pag->pagl_pagino = NULLAGINO;
848 pag->pagl_leftrec = NULLAGINO;
849 pag->pagl_rightrec = NULLAGINO;
850 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
851 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
852 goto restart_pagno;
853 }
854
855
856
857
858
859newino:
860 if (agi->agi_newino != cpu_to_be32(NULLAGINO)) {
861 error = xfs_inobt_lookup(cur, be32_to_cpu(agi->agi_newino),
862 XFS_LOOKUP_EQ, &i);
863 if (error)
864 goto error0;
865
866 if (i == 1) {
867 error = xfs_inobt_get_rec(cur, &rec, &j);
868 if (error)
869 goto error0;
870
871 if (j == 1 && rec.ir_freecount > 0) {
872
873
874
875
876 goto alloc_inode;
877 }
878 }
879 }
880
881
882
883
884 error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
885 if (error)
886 goto error0;
887 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
888
889 for (;;) {
890 error = xfs_inobt_get_rec(cur, &rec, &i);
891 if (error)
892 goto error0;
893 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
894 if (rec.ir_freecount > 0)
895 break;
896 error = xfs_btree_increment(cur, 0, &i);
897 if (error)
898 goto error0;
899 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
900 }
901
902alloc_inode:
903 offset = xfs_lowbit64(rec.ir_free);
904 ASSERT(offset >= 0);
905 ASSERT(offset < XFS_INODES_PER_CHUNK);
906 ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
907 XFS_INODES_PER_CHUNK) == 0);
908 ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset);
909 rec.ir_free &= ~XFS_INOBT_MASK(offset);
910 rec.ir_freecount--;
911 error = xfs_inobt_update(cur, &rec);
912 if (error)
913 goto error0;
914 be32_add_cpu(&agi->agi_freecount, -1);
915 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
916 pag->pagi_freecount--;
917
918 error = xfs_check_agi_freecount(cur, agi);
919 if (error)
920 goto error0;
921
922 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
923 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
924 xfs_perag_put(pag);
925 *inop = ino;
926 return 0;
927error1:
928 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
929error0:
930 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
931 xfs_perag_put(pag);
932 return error;
933}
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956int
957xfs_dialloc(
958 struct xfs_trans *tp,
959 xfs_ino_t parent,
960 umode_t mode,
961 int okalloc,
962 struct xfs_buf **IO_agbp,
963 xfs_ino_t *inop)
964{
965 struct xfs_mount *mp = tp->t_mountp;
966 struct xfs_buf *agbp;
967 xfs_agnumber_t agno;
968 int error;
969 int ialloced;
970 int noroom = 0;
971 xfs_agnumber_t start_agno;
972 struct xfs_perag *pag;
973
974 if (*IO_agbp) {
975
976
977
978
979
980 agbp = *IO_agbp;
981 goto out_alloc;
982 }
983
984
985
986
987
988 start_agno = xfs_ialloc_ag_select(tp, parent, mode, okalloc);
989 if (start_agno == NULLAGNUMBER) {
990 *inop = NULLFSINO;
991 return 0;
992 }
993
994
995
996
997
998
999 if (mp->m_maxicount &&
1000 mp->m_sb.sb_icount + XFS_IALLOC_INODES(mp) > mp->m_maxicount) {
1001 noroom = 1;
1002 okalloc = 0;
1003 }
1004
1005
1006
1007
1008
1009
1010 agno = start_agno;
1011 for (;;) {
1012 pag = xfs_perag_get(mp, agno);
1013 if (!pag->pagi_inodeok) {
1014 xfs_ialloc_next_ag(mp);
1015 goto nextag;
1016 }
1017
1018 if (!pag->pagi_init) {
1019 error = xfs_ialloc_pagi_init(mp, tp, agno);
1020 if (error)
1021 goto out_error;
1022 }
1023
1024
1025
1026
1027 if (!pag->pagi_freecount && !okalloc)
1028 goto nextag;
1029
1030
1031
1032
1033
1034 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1035 if (error)
1036 goto out_error;
1037
1038 if (pag->pagi_freecount) {
1039 xfs_perag_put(pag);
1040 goto out_alloc;
1041 }
1042
1043 if (!okalloc)
1044 goto nextag_relse_buffer;
1045
1046
1047 error = xfs_ialloc_ag_alloc(tp, agbp, &ialloced);
1048 if (error) {
1049 xfs_trans_brelse(tp, agbp);
1050
1051 if (error != ENOSPC)
1052 goto out_error;
1053
1054 xfs_perag_put(pag);
1055 *inop = NULLFSINO;
1056 return 0;
1057 }
1058
1059 if (ialloced) {
1060
1061
1062
1063
1064
1065
1066 ASSERT(pag->pagi_freecount > 0);
1067 xfs_perag_put(pag);
1068
1069 *IO_agbp = agbp;
1070 *inop = NULLFSINO;
1071 return 0;
1072 }
1073
1074nextag_relse_buffer:
1075 xfs_trans_brelse(tp, agbp);
1076nextag:
1077 xfs_perag_put(pag);
1078 if (++agno == mp->m_sb.sb_agcount)
1079 agno = 0;
1080 if (agno == start_agno) {
1081 *inop = NULLFSINO;
1082 return noroom ? ENOSPC : 0;
1083 }
1084 }
1085
1086out_alloc:
1087 *IO_agbp = NULL;
1088 return xfs_dialloc_ag(tp, agbp, parent, inop);
1089out_error:
1090 xfs_perag_put(pag);
1091 return XFS_ERROR(error);
1092}
1093
1094
1095
1096
1097
1098
1099
1100int
1101xfs_difree(
1102 xfs_trans_t *tp,
1103 xfs_ino_t inode,
1104 xfs_bmap_free_t *flist,
1105 int *delete,
1106 xfs_ino_t *first_ino)
1107{
1108
1109 xfs_agblock_t agbno;
1110 xfs_buf_t *agbp;
1111 xfs_agino_t agino;
1112 xfs_agnumber_t agno;
1113 xfs_agi_t *agi;
1114 xfs_btree_cur_t *cur;
1115 int error;
1116 int i;
1117 int ilen;
1118 xfs_mount_t *mp;
1119 int off;
1120 xfs_inobt_rec_incore_t rec;
1121 struct xfs_perag *pag;
1122
1123 mp = tp->t_mountp;
1124
1125
1126
1127
1128 agno = XFS_INO_TO_AGNO(mp, inode);
1129 if (agno >= mp->m_sb.sb_agcount) {
1130 xfs_warn(mp, "%s: agno >= mp->m_sb.sb_agcount (%d >= %d).",
1131 __func__, agno, mp->m_sb.sb_agcount);
1132 ASSERT(0);
1133 return XFS_ERROR(EINVAL);
1134 }
1135 agino = XFS_INO_TO_AGINO(mp, inode);
1136 if (inode != XFS_AGINO_TO_INO(mp, agno, agino)) {
1137 xfs_warn(mp, "%s: inode != XFS_AGINO_TO_INO() (%llu != %llu).",
1138 __func__, (unsigned long long)inode,
1139 (unsigned long long)XFS_AGINO_TO_INO(mp, agno, agino));
1140 ASSERT(0);
1141 return XFS_ERROR(EINVAL);
1142 }
1143 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
1144 if (agbno >= mp->m_sb.sb_agblocks) {
1145 xfs_warn(mp, "%s: agbno >= mp->m_sb.sb_agblocks (%d >= %d).",
1146 __func__, agbno, mp->m_sb.sb_agblocks);
1147 ASSERT(0);
1148 return XFS_ERROR(EINVAL);
1149 }
1150
1151
1152
1153 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1154 if (error) {
1155 xfs_warn(mp, "%s: xfs_ialloc_read_agi() returned error %d.",
1156 __func__, error);
1157 return error;
1158 }
1159 agi = XFS_BUF_TO_AGI(agbp);
1160 ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC));
1161 ASSERT(agbno < be32_to_cpu(agi->agi_length));
1162
1163
1164
1165 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno);
1166
1167 error = xfs_check_agi_freecount(cur, agi);
1168 if (error)
1169 goto error0;
1170
1171
1172
1173
1174 if ((error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i))) {
1175 xfs_warn(mp, "%s: xfs_inobt_lookup() returned error %d.",
1176 __func__, error);
1177 goto error0;
1178 }
1179 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1180 error = xfs_inobt_get_rec(cur, &rec, &i);
1181 if (error) {
1182 xfs_warn(mp, "%s: xfs_inobt_get_rec() returned error %d.",
1183 __func__, error);
1184 goto error0;
1185 }
1186 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1187
1188
1189
1190 off = agino - rec.ir_startino;
1191 ASSERT(off >= 0 && off < XFS_INODES_PER_CHUNK);
1192 ASSERT(!(rec.ir_free & XFS_INOBT_MASK(off)));
1193
1194
1195
1196 rec.ir_free |= XFS_INOBT_MASK(off);
1197 rec.ir_freecount++;
1198
1199
1200
1201
1202 if (!(mp->m_flags & XFS_MOUNT_IKEEP) &&
1203 (rec.ir_freecount == XFS_IALLOC_INODES(mp))) {
1204
1205 *delete = 1;
1206 *first_ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino);
1207
1208
1209
1210
1211
1212
1213 ilen = XFS_IALLOC_INODES(mp);
1214 be32_add_cpu(&agi->agi_count, -ilen);
1215 be32_add_cpu(&agi->agi_freecount, -(ilen - 1));
1216 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_COUNT | XFS_AGI_FREECOUNT);
1217 pag = xfs_perag_get(mp, agno);
1218 pag->pagi_freecount -= ilen - 1;
1219 xfs_perag_put(pag);
1220 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, -ilen);
1221 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -(ilen - 1));
1222
1223 if ((error = xfs_btree_delete(cur, &i))) {
1224 xfs_warn(mp, "%s: xfs_btree_delete returned error %d.",
1225 __func__, error);
1226 goto error0;
1227 }
1228
1229 xfs_bmap_add_free(XFS_AGB_TO_FSB(mp,
1230 agno, XFS_INO_TO_AGBNO(mp,rec.ir_startino)),
1231 XFS_IALLOC_BLOCKS(mp), flist, mp);
1232 } else {
1233 *delete = 0;
1234
1235 error = xfs_inobt_update(cur, &rec);
1236 if (error) {
1237 xfs_warn(mp, "%s: xfs_inobt_update returned error %d.",
1238 __func__, error);
1239 goto error0;
1240 }
1241
1242
1243
1244
1245 be32_add_cpu(&agi->agi_freecount, 1);
1246 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
1247 pag = xfs_perag_get(mp, agno);
1248 pag->pagi_freecount++;
1249 xfs_perag_put(pag);
1250 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, 1);
1251 }
1252
1253 error = xfs_check_agi_freecount(cur, agi);
1254 if (error)
1255 goto error0;
1256
1257 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1258 return 0;
1259
1260error0:
1261 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1262 return error;
1263}
1264
1265STATIC int
1266xfs_imap_lookup(
1267 struct xfs_mount *mp,
1268 struct xfs_trans *tp,
1269 xfs_agnumber_t agno,
1270 xfs_agino_t agino,
1271 xfs_agblock_t agbno,
1272 xfs_agblock_t *chunk_agbno,
1273 xfs_agblock_t *offset_agbno,
1274 int flags)
1275{
1276 struct xfs_inobt_rec_incore rec;
1277 struct xfs_btree_cur *cur;
1278 struct xfs_buf *agbp;
1279 int error;
1280 int i;
1281
1282 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1283 if (error) {
1284 xfs_alert(mp,
1285 "%s: xfs_ialloc_read_agi() returned error %d, agno %d",
1286 __func__, error, agno);
1287 return error;
1288 }
1289
1290
1291
1292
1293
1294
1295
1296 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno);
1297 error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i);
1298 if (!error) {
1299 if (i)
1300 error = xfs_inobt_get_rec(cur, &rec, &i);
1301 if (!error && i == 0)
1302 error = EINVAL;
1303 }
1304
1305 xfs_trans_brelse(tp, agbp);
1306 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1307 if (error)
1308 return error;
1309
1310
1311 if (rec.ir_startino > agino ||
1312 rec.ir_startino + XFS_IALLOC_INODES(mp) <= agino)
1313 return EINVAL;
1314
1315
1316 if ((flags & XFS_IGET_UNTRUSTED) &&
1317 (rec.ir_free & XFS_INOBT_MASK(agino - rec.ir_startino)))
1318 return EINVAL;
1319
1320 *chunk_agbno = XFS_AGINO_TO_AGBNO(mp, rec.ir_startino);
1321 *offset_agbno = agbno - *chunk_agbno;
1322 return 0;
1323}
1324
1325
1326
1327
1328int
1329xfs_imap(
1330 xfs_mount_t *mp,
1331 xfs_trans_t *tp,
1332 xfs_ino_t ino,
1333 struct xfs_imap *imap,
1334 uint flags)
1335{
1336 xfs_agblock_t agbno;
1337 xfs_agino_t agino;
1338 xfs_agnumber_t agno;
1339 int blks_per_cluster;
1340 xfs_agblock_t chunk_agbno;
1341 xfs_agblock_t cluster_agbno;
1342 int error;
1343 int offset;
1344 int offset_agbno;
1345
1346 ASSERT(ino != NULLFSINO);
1347
1348
1349
1350
1351 agno = XFS_INO_TO_AGNO(mp, ino);
1352 agino = XFS_INO_TO_AGINO(mp, ino);
1353 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
1354 if (agno >= mp->m_sb.sb_agcount || agbno >= mp->m_sb.sb_agblocks ||
1355 ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
1356#ifdef DEBUG
1357
1358
1359
1360
1361 if (flags & XFS_IGET_UNTRUSTED)
1362 return XFS_ERROR(EINVAL);
1363 if (agno >= mp->m_sb.sb_agcount) {
1364 xfs_alert(mp,
1365 "%s: agno (%d) >= mp->m_sb.sb_agcount (%d)",
1366 __func__, agno, mp->m_sb.sb_agcount);
1367 }
1368 if (agbno >= mp->m_sb.sb_agblocks) {
1369 xfs_alert(mp,
1370 "%s: agbno (0x%llx) >= mp->m_sb.sb_agblocks (0x%lx)",
1371 __func__, (unsigned long long)agbno,
1372 (unsigned long)mp->m_sb.sb_agblocks);
1373 }
1374 if (ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
1375 xfs_alert(mp,
1376 "%s: ino (0x%llx) != XFS_AGINO_TO_INO() (0x%llx)",
1377 __func__, ino,
1378 XFS_AGINO_TO_INO(mp, agno, agino));
1379 }
1380 xfs_stack_trace();
1381#endif
1382 return XFS_ERROR(EINVAL);
1383 }
1384
1385 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_blocklog;
1386
1387
1388
1389
1390
1391
1392
1393
1394 if (flags & XFS_IGET_UNTRUSTED) {
1395 error = xfs_imap_lookup(mp, tp, agno, agino, agbno,
1396 &chunk_agbno, &offset_agbno, flags);
1397 if (error)
1398 return error;
1399 goto out_map;
1400 }
1401
1402
1403
1404
1405
1406 if (XFS_INODE_CLUSTER_SIZE(mp) <= mp->m_sb.sb_blocksize) {
1407 offset = XFS_INO_TO_OFFSET(mp, ino);
1408 ASSERT(offset < mp->m_sb.sb_inopblock);
1409
1410 imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, agbno);
1411 imap->im_len = XFS_FSB_TO_BB(mp, 1);
1412 imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog);
1413 return 0;
1414 }
1415
1416
1417
1418
1419
1420
1421 if (mp->m_inoalign_mask) {
1422 offset_agbno = agbno & mp->m_inoalign_mask;
1423 chunk_agbno = agbno - offset_agbno;
1424 } else {
1425 error = xfs_imap_lookup(mp, tp, agno, agino, agbno,
1426 &chunk_agbno, &offset_agbno, flags);
1427 if (error)
1428 return error;
1429 }
1430
1431out_map:
1432 ASSERT(agbno >= chunk_agbno);
1433 cluster_agbno = chunk_agbno +
1434 ((offset_agbno / blks_per_cluster) * blks_per_cluster);
1435 offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
1436 XFS_INO_TO_OFFSET(mp, ino);
1437
1438 imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, cluster_agbno);
1439 imap->im_len = XFS_FSB_TO_BB(mp, blks_per_cluster);
1440 imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog);
1441
1442
1443
1444
1445
1446
1447
1448 if ((imap->im_blkno + imap->im_len) >
1449 XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)) {
1450 xfs_alert(mp,
1451 "%s: (im_blkno (0x%llx) + im_len (0x%llx)) > sb_dblocks (0x%llx)",
1452 __func__, (unsigned long long) imap->im_blkno,
1453 (unsigned long long) imap->im_len,
1454 XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks));
1455 return XFS_ERROR(EINVAL);
1456 }
1457 return 0;
1458}
1459
1460
1461
1462
1463void
1464xfs_ialloc_compute_maxlevels(
1465 xfs_mount_t *mp)
1466{
1467 int level;
1468 uint maxblocks;
1469 uint maxleafents;
1470 int minleafrecs;
1471 int minnoderecs;
1472
1473 maxleafents = (1LL << XFS_INO_AGINO_BITS(mp)) >>
1474 XFS_INODES_PER_CHUNK_LOG;
1475 minleafrecs = mp->m_alloc_mnr[0];
1476 minnoderecs = mp->m_alloc_mnr[1];
1477 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
1478 for (level = 1; maxblocks > 1; level++)
1479 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
1480 mp->m_in_maxlevels = level;
1481}
1482
1483
1484
1485
1486void
1487xfs_ialloc_log_agi(
1488 xfs_trans_t *tp,
1489 xfs_buf_t *bp,
1490 int fields)
1491{
1492 int first;
1493 int last;
1494 static const short offsets[] = {
1495
1496 offsetof(xfs_agi_t, agi_magicnum),
1497 offsetof(xfs_agi_t, agi_versionnum),
1498 offsetof(xfs_agi_t, agi_seqno),
1499 offsetof(xfs_agi_t, agi_length),
1500 offsetof(xfs_agi_t, agi_count),
1501 offsetof(xfs_agi_t, agi_root),
1502 offsetof(xfs_agi_t, agi_level),
1503 offsetof(xfs_agi_t, agi_freecount),
1504 offsetof(xfs_agi_t, agi_newino),
1505 offsetof(xfs_agi_t, agi_dirino),
1506 offsetof(xfs_agi_t, agi_unlinked),
1507 sizeof(xfs_agi_t)
1508 };
1509#ifdef DEBUG
1510 xfs_agi_t *agi;
1511
1512 agi = XFS_BUF_TO_AGI(bp);
1513 ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC));
1514#endif
1515
1516
1517
1518 xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS, &first, &last);
1519
1520
1521
1522 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_AGI_BUF);
1523 xfs_trans_log_buf(tp, bp, first, last);
1524}
1525
1526#ifdef DEBUG
1527STATIC void
1528xfs_check_agi_unlinked(
1529 struct xfs_agi *agi)
1530{
1531 int i;
1532
1533 for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++)
1534 ASSERT(agi->agi_unlinked[i]);
1535}
1536#else
1537#define xfs_check_agi_unlinked(agi)
1538#endif
1539
1540static bool
1541xfs_agi_verify(
1542 struct xfs_buf *bp)
1543{
1544 struct xfs_mount *mp = bp->b_target->bt_mount;
1545 struct xfs_agi *agi = XFS_BUF_TO_AGI(bp);
1546
1547 if (xfs_sb_version_hascrc(&mp->m_sb) &&
1548 !uuid_equal(&agi->agi_uuid, &mp->m_sb.sb_uuid))
1549 return false;
1550
1551
1552
1553 if (agi->agi_magicnum != cpu_to_be32(XFS_AGI_MAGIC))
1554 return false;
1555 if (!XFS_AGI_GOOD_VERSION(be32_to_cpu(agi->agi_versionnum)))
1556 return false;
1557
1558
1559
1560
1561
1562
1563
1564 if (bp->b_pag && be32_to_cpu(agi->agi_seqno) != bp->b_pag->pag_agno)
1565 return false;
1566
1567 xfs_check_agi_unlinked(agi);
1568 return true;
1569}
1570
1571static void
1572xfs_agi_read_verify(
1573 struct xfs_buf *bp)
1574{
1575 struct xfs_mount *mp = bp->b_target->bt_mount;
1576 int agi_ok = 1;
1577
1578 if (xfs_sb_version_hascrc(&mp->m_sb))
1579 agi_ok = xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length),
1580 offsetof(struct xfs_agi, agi_crc));
1581 agi_ok = agi_ok && xfs_agi_verify(bp);
1582
1583 if (unlikely(XFS_TEST_ERROR(!agi_ok, mp, XFS_ERRTAG_IALLOC_READ_AGI,
1584 XFS_RANDOM_IALLOC_READ_AGI))) {
1585 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
1586 xfs_buf_ioerror(bp, EFSCORRUPTED);
1587 }
1588}
1589
1590static void
1591xfs_agi_write_verify(
1592 struct xfs_buf *bp)
1593{
1594 struct xfs_mount *mp = bp->b_target->bt_mount;
1595 struct xfs_buf_log_item *bip = bp->b_fspriv;
1596
1597 if (!xfs_agi_verify(bp)) {
1598 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
1599 xfs_buf_ioerror(bp, EFSCORRUPTED);
1600 return;
1601 }
1602
1603 if (!xfs_sb_version_hascrc(&mp->m_sb))
1604 return;
1605
1606 if (bip)
1607 XFS_BUF_TO_AGI(bp)->agi_lsn = cpu_to_be64(bip->bli_item.li_lsn);
1608 xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length),
1609 offsetof(struct xfs_agi, agi_crc));
1610}
1611
1612const struct xfs_buf_ops xfs_agi_buf_ops = {
1613 .verify_read = xfs_agi_read_verify,
1614 .verify_write = xfs_agi_write_verify,
1615};
1616
1617
1618
1619
1620int
1621xfs_read_agi(
1622 struct xfs_mount *mp,
1623 struct xfs_trans *tp,
1624 xfs_agnumber_t agno,
1625 struct xfs_buf **bpp)
1626{
1627 int error;
1628
1629 ASSERT(agno != NULLAGNUMBER);
1630
1631 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
1632 XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)),
1633 XFS_FSS_TO_BB(mp, 1), 0, bpp, &xfs_agi_buf_ops);
1634 if (error)
1635 return error;
1636
1637 ASSERT(!xfs_buf_geterror(*bpp));
1638 xfs_buf_set_ref(*bpp, XFS_AGI_REF);
1639 return 0;
1640}
1641
1642int
1643xfs_ialloc_read_agi(
1644 struct xfs_mount *mp,
1645 struct xfs_trans *tp,
1646 xfs_agnumber_t agno,
1647 struct xfs_buf **bpp)
1648{
1649 struct xfs_agi *agi;
1650 struct xfs_perag *pag;
1651 int error;
1652
1653 error = xfs_read_agi(mp, tp, agno, bpp);
1654 if (error)
1655 return error;
1656
1657 agi = XFS_BUF_TO_AGI(*bpp);
1658 pag = xfs_perag_get(mp, agno);
1659 if (!pag->pagi_init) {
1660 pag->pagi_freecount = be32_to_cpu(agi->agi_freecount);
1661 pag->pagi_count = be32_to_cpu(agi->agi_count);
1662 pag->pagi_init = 1;
1663 }
1664
1665
1666
1667
1668
1669 ASSERT(pag->pagi_freecount == be32_to_cpu(agi->agi_freecount) ||
1670 XFS_FORCED_SHUTDOWN(mp));
1671 xfs_perag_put(pag);
1672 return 0;
1673}
1674
1675
1676
1677
1678int
1679xfs_ialloc_pagi_init(
1680 xfs_mount_t *mp,
1681 xfs_trans_t *tp,
1682 xfs_agnumber_t agno)
1683{
1684 xfs_buf_t *bp = NULL;
1685 int error;
1686
1687 error = xfs_ialloc_read_agi(mp, tp, agno, &bp);
1688 if (error)
1689 return error;
1690 if (bp)
1691 xfs_trans_brelse(tp, bp);
1692 return 0;
1693}
1694