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