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