1
2
3
4
5
6#include "xfs.h"
7#include "xfs_fs.h"
8#include "xfs_shared.h"
9#include "xfs_format.h"
10#include "xfs_log_format.h"
11#include "xfs_trans_resv.h"
12#include "xfs_bit.h"
13#include "xfs_sb.h"
14#include "xfs_mount.h"
15#include "xfs_inode.h"
16#include "xfs_btree.h"
17#include "xfs_ialloc.h"
18#include "xfs_ialloc_btree.h"
19#include "xfs_alloc.h"
20#include "xfs_errortag.h"
21#include "xfs_error.h"
22#include "xfs_bmap.h"
23#include "xfs_trans.h"
24#include "xfs_buf_item.h"
25#include "xfs_icreate_item.h"
26#include "xfs_icache.h"
27#include "xfs_trace.h"
28#include "xfs_log.h"
29#include "xfs_rmap.h"
30
31
32
33
34int
35xfs_inobt_lookup(
36 struct xfs_btree_cur *cur,
37 xfs_agino_t ino,
38 xfs_lookup_t dir,
39 int *stat)
40{
41 cur->bc_rec.i.ir_startino = ino;
42 cur->bc_rec.i.ir_holemask = 0;
43 cur->bc_rec.i.ir_count = 0;
44 cur->bc_rec.i.ir_freecount = 0;
45 cur->bc_rec.i.ir_free = 0;
46 return xfs_btree_lookup(cur, dir, stat);
47}
48
49
50
51
52
53STATIC int
54xfs_inobt_update(
55 struct xfs_btree_cur *cur,
56 xfs_inobt_rec_incore_t *irec)
57{
58 union xfs_btree_rec rec;
59
60 rec.inobt.ir_startino = cpu_to_be32(irec->ir_startino);
61 if (xfs_sb_version_hassparseinodes(&cur->bc_mp->m_sb)) {
62 rec.inobt.ir_u.sp.ir_holemask = cpu_to_be16(irec->ir_holemask);
63 rec.inobt.ir_u.sp.ir_count = irec->ir_count;
64 rec.inobt.ir_u.sp.ir_freecount = irec->ir_freecount;
65 } else {
66
67 rec.inobt.ir_u.f.ir_freecount = cpu_to_be32(irec->ir_freecount);
68 }
69 rec.inobt.ir_free = cpu_to_be64(irec->ir_free);
70 return xfs_btree_update(cur, &rec);
71}
72
73
74void
75xfs_inobt_btrec_to_irec(
76 struct xfs_mount *mp,
77 union xfs_btree_rec *rec,
78 struct xfs_inobt_rec_incore *irec)
79{
80 irec->ir_startino = be32_to_cpu(rec->inobt.ir_startino);
81 if (xfs_sb_version_hassparseinodes(&mp->m_sb)) {
82 irec->ir_holemask = be16_to_cpu(rec->inobt.ir_u.sp.ir_holemask);
83 irec->ir_count = rec->inobt.ir_u.sp.ir_count;
84 irec->ir_freecount = rec->inobt.ir_u.sp.ir_freecount;
85 } else {
86
87
88
89
90 irec->ir_holemask = XFS_INOBT_HOLEMASK_FULL;
91 irec->ir_count = XFS_INODES_PER_CHUNK;
92 irec->ir_freecount =
93 be32_to_cpu(rec->inobt.ir_u.f.ir_freecount);
94 }
95 irec->ir_free = be64_to_cpu(rec->inobt.ir_free);
96}
97
98
99
100
101int
102xfs_inobt_get_rec(
103 struct xfs_btree_cur *cur,
104 struct xfs_inobt_rec_incore *irec,
105 int *stat)
106{
107 struct xfs_mount *mp = cur->bc_mp;
108 xfs_agnumber_t agno = cur->bc_ag.agno;
109 union xfs_btree_rec *rec;
110 int error;
111 uint64_t realfree;
112
113 error = xfs_btree_get_rec(cur, &rec, stat);
114 if (error || *stat == 0)
115 return error;
116
117 xfs_inobt_btrec_to_irec(mp, rec, irec);
118
119 if (!xfs_verify_agino(mp, agno, irec->ir_startino))
120 goto out_bad_rec;
121 if (irec->ir_count < XFS_INODES_PER_HOLEMASK_BIT ||
122 irec->ir_count > XFS_INODES_PER_CHUNK)
123 goto out_bad_rec;
124 if (irec->ir_freecount > XFS_INODES_PER_CHUNK)
125 goto out_bad_rec;
126
127
128 if (!xfs_inobt_issparse(irec->ir_holemask))
129 realfree = irec->ir_free;
130 else
131 realfree = irec->ir_free & xfs_inobt_irec_to_allocmask(irec);
132 if (hweight64(realfree) != irec->ir_freecount)
133 goto out_bad_rec;
134
135 return 0;
136
137out_bad_rec:
138 xfs_warn(mp,
139 "%s Inode BTree record corruption in AG %d detected!",
140 cur->bc_btnum == XFS_BTNUM_INO ? "Used" : "Free", agno);
141 xfs_warn(mp,
142"start inode 0x%x, count 0x%x, free 0x%x freemask 0x%llx, holemask 0x%x",
143 irec->ir_startino, irec->ir_count, irec->ir_freecount,
144 irec->ir_free, irec->ir_holemask);
145 return -EFSCORRUPTED;
146}
147
148
149
150
151int
152xfs_inobt_insert_rec(
153 struct xfs_btree_cur *cur,
154 uint16_t holemask,
155 uint8_t count,
156 int32_t freecount,
157 xfs_inofree_t free,
158 int *stat)
159{
160 cur->bc_rec.i.ir_holemask = holemask;
161 cur->bc_rec.i.ir_count = count;
162 cur->bc_rec.i.ir_freecount = freecount;
163 cur->bc_rec.i.ir_free = free;
164 return xfs_btree_insert(cur, stat);
165}
166
167
168
169
170STATIC int
171xfs_inobt_insert(
172 struct xfs_mount *mp,
173 struct xfs_trans *tp,
174 struct xfs_buf *agbp,
175 xfs_agino_t newino,
176 xfs_agino_t newlen,
177 xfs_btnum_t btnum)
178{
179 struct xfs_btree_cur *cur;
180 struct xfs_agi *agi = agbp->b_addr;
181 xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno);
182 xfs_agino_t thisino;
183 int i;
184 int error;
185
186 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, btnum);
187
188 for (thisino = newino;
189 thisino < newino + newlen;
190 thisino += XFS_INODES_PER_CHUNK) {
191 error = xfs_inobt_lookup(cur, thisino, XFS_LOOKUP_EQ, &i);
192 if (error) {
193 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
194 return error;
195 }
196 ASSERT(i == 0);
197
198 error = xfs_inobt_insert_rec(cur, XFS_INOBT_HOLEMASK_FULL,
199 XFS_INODES_PER_CHUNK,
200 XFS_INODES_PER_CHUNK,
201 XFS_INOBT_ALL_FREE, &i);
202 if (error) {
203 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
204 return error;
205 }
206 ASSERT(i == 1);
207 }
208
209 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
210
211 return 0;
212}
213
214
215
216
217#ifdef DEBUG
218STATIC int
219xfs_check_agi_freecount(
220 struct xfs_btree_cur *cur,
221 struct xfs_agi *agi)
222{
223 if (cur->bc_nlevels == 1) {
224 xfs_inobt_rec_incore_t rec;
225 int freecount = 0;
226 int error;
227 int i;
228
229 error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
230 if (error)
231 return error;
232
233 do {
234 error = xfs_inobt_get_rec(cur, &rec, &i);
235 if (error)
236 return error;
237
238 if (i) {
239 freecount += rec.ir_freecount;
240 error = xfs_btree_increment(cur, 0, &i);
241 if (error)
242 return error;
243 }
244 } while (i == 1);
245
246 if (!XFS_FORCED_SHUTDOWN(cur->bc_mp))
247 ASSERT(freecount == be32_to_cpu(agi->agi_freecount));
248 }
249 return 0;
250}
251#else
252#define xfs_check_agi_freecount(cur, agi) 0
253#endif
254
255
256
257
258
259
260
261int
262xfs_ialloc_inode_init(
263 struct xfs_mount *mp,
264 struct xfs_trans *tp,
265 struct list_head *buffer_list,
266 int icount,
267 xfs_agnumber_t agno,
268 xfs_agblock_t agbno,
269 xfs_agblock_t length,
270 unsigned int gen)
271{
272 struct xfs_buf *fbuf;
273 struct xfs_dinode *free;
274 int nbufs;
275 int version;
276 int i, j;
277 xfs_daddr_t d;
278 xfs_ino_t ino = 0;
279 int error;
280
281
282
283
284
285
286 nbufs = length / M_IGEO(mp)->blocks_per_cluster;
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307 if (xfs_sb_version_has_v3inode(&mp->m_sb)) {
308 version = 3;
309 ino = XFS_AGINO_TO_INO(mp, agno, XFS_AGB_TO_AGINO(mp, agbno));
310
311
312
313
314
315
316
317
318
319 if (tp)
320 xfs_icreate_log(tp, agno, agbno, icount,
321 mp->m_sb.sb_inodesize, length, gen);
322 } else
323 version = 2;
324
325 for (j = 0; j < nbufs; j++) {
326
327
328
329 d = XFS_AGB_TO_DADDR(mp, agno, agbno +
330 (j * M_IGEO(mp)->blocks_per_cluster));
331 error = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
332 mp->m_bsize * M_IGEO(mp)->blocks_per_cluster,
333 XBF_UNMAPPED, &fbuf);
334 if (error)
335 return error;
336
337
338 fbuf->b_ops = &xfs_inode_buf_ops;
339 xfs_buf_zero(fbuf, 0, BBTOB(fbuf->b_length));
340 for (i = 0; i < M_IGEO(mp)->inodes_per_cluster; i++) {
341 int ioffset = i << mp->m_sb.sb_inodelog;
342 uint isize = XFS_DINODE_SIZE(&mp->m_sb);
343
344 free = xfs_make_iptr(mp, fbuf, i);
345 free->di_magic = cpu_to_be16(XFS_DINODE_MAGIC);
346 free->di_version = version;
347 free->di_gen = cpu_to_be32(gen);
348 free->di_next_unlinked = cpu_to_be32(NULLAGINO);
349
350 if (version == 3) {
351 free->di_ino = cpu_to_be64(ino);
352 ino++;
353 uuid_copy(&free->di_uuid,
354 &mp->m_sb.sb_meta_uuid);
355 xfs_dinode_calc_crc(mp, free);
356 } else if (tp) {
357
358 xfs_trans_log_buf(tp, fbuf, ioffset,
359 ioffset + isize - 1);
360 }
361 }
362
363 if (tp) {
364
365
366
367
368
369
370
371
372 xfs_trans_inode_alloc_buf(tp, fbuf);
373 if (version == 3) {
374
375
376
377
378
379
380 xfs_trans_ordered_buf(tp, fbuf);
381 }
382 } else {
383 fbuf->b_flags |= XBF_DONE;
384 xfs_buf_delwri_queue(fbuf, buffer_list);
385 xfs_buf_relse(fbuf);
386 }
387 }
388 return 0;
389}
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414STATIC void
415xfs_align_sparse_ino(
416 struct xfs_mount *mp,
417 xfs_agino_t *startino,
418 uint16_t *allocmask)
419{
420 xfs_agblock_t agbno;
421 xfs_agblock_t mod;
422 int offset;
423
424 agbno = XFS_AGINO_TO_AGBNO(mp, *startino);
425 mod = agbno % mp->m_sb.sb_inoalignmt;
426 if (!mod)
427 return;
428
429
430 offset = XFS_AGB_TO_AGINO(mp, mod);
431 *startino -= offset;
432
433
434
435
436
437
438 *allocmask <<= offset / XFS_INODES_PER_HOLEMASK_BIT;
439}
440
441
442
443
444
445
446STATIC bool
447__xfs_inobt_can_merge(
448 struct xfs_inobt_rec_incore *trec,
449 struct xfs_inobt_rec_incore *srec)
450{
451 uint64_t talloc;
452 uint64_t salloc;
453
454
455 if (trec->ir_startino != srec->ir_startino)
456 return false;
457
458
459 if (!xfs_inobt_issparse(trec->ir_holemask) ||
460 !xfs_inobt_issparse(srec->ir_holemask))
461 return false;
462
463
464 if (!trec->ir_count || !srec->ir_count)
465 return false;
466
467
468 if (trec->ir_count + srec->ir_count > XFS_INODES_PER_CHUNK)
469 return false;
470
471
472 talloc = xfs_inobt_irec_to_allocmask(trec);
473 salloc = xfs_inobt_irec_to_allocmask(srec);
474 if (talloc & salloc)
475 return false;
476
477 return true;
478}
479
480
481
482
483
484STATIC void
485__xfs_inobt_rec_merge(
486 struct xfs_inobt_rec_incore *trec,
487 struct xfs_inobt_rec_incore *srec)
488{
489 ASSERT(trec->ir_startino == srec->ir_startino);
490
491
492 trec->ir_count += srec->ir_count;
493 trec->ir_freecount += srec->ir_freecount;
494
495
496
497
498
499 trec->ir_holemask &= srec->ir_holemask;
500 trec->ir_free &= srec->ir_free;
501}
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518STATIC int
519xfs_inobt_insert_sprec(
520 struct xfs_mount *mp,
521 struct xfs_trans *tp,
522 struct xfs_buf *agbp,
523 int btnum,
524 struct xfs_inobt_rec_incore *nrec,
525 bool merge)
526{
527 struct xfs_btree_cur *cur;
528 struct xfs_agi *agi = agbp->b_addr;
529 xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno);
530 int error;
531 int i;
532 struct xfs_inobt_rec_incore rec;
533
534 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, btnum);
535
536
537 error = xfs_inobt_lookup(cur, nrec->ir_startino, XFS_LOOKUP_EQ, &i);
538 if (error)
539 goto error;
540
541 if (i == 0) {
542 error = xfs_inobt_insert_rec(cur, nrec->ir_holemask,
543 nrec->ir_count, nrec->ir_freecount,
544 nrec->ir_free, &i);
545 if (error)
546 goto error;
547 if (XFS_IS_CORRUPT(mp, i != 1)) {
548 error = -EFSCORRUPTED;
549 goto error;
550 }
551
552 goto out;
553 }
554
555
556
557
558
559 if (merge) {
560 error = xfs_inobt_get_rec(cur, &rec, &i);
561 if (error)
562 goto error;
563 if (XFS_IS_CORRUPT(mp, i != 1)) {
564 error = -EFSCORRUPTED;
565 goto error;
566 }
567 if (XFS_IS_CORRUPT(mp, rec.ir_startino != nrec->ir_startino)) {
568 error = -EFSCORRUPTED;
569 goto error;
570 }
571
572
573
574
575
576 if (XFS_IS_CORRUPT(mp, !__xfs_inobt_can_merge(nrec, &rec))) {
577 error = -EFSCORRUPTED;
578 goto error;
579 }
580
581 trace_xfs_irec_merge_pre(mp, agno, rec.ir_startino,
582 rec.ir_holemask, nrec->ir_startino,
583 nrec->ir_holemask);
584
585
586 __xfs_inobt_rec_merge(nrec, &rec);
587
588 trace_xfs_irec_merge_post(mp, agno, nrec->ir_startino,
589 nrec->ir_holemask);
590
591 error = xfs_inobt_rec_check_count(mp, nrec);
592 if (error)
593 goto error;
594 }
595
596 error = xfs_inobt_update(cur, nrec);
597 if (error)
598 goto error;
599
600out:
601 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
602 return 0;
603error:
604 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
605 return error;
606}
607
608
609
610
611
612
613STATIC int
614xfs_ialloc_ag_alloc(
615 struct xfs_trans *tp,
616 struct xfs_buf *agbp)
617{
618 struct xfs_agi *agi;
619 struct xfs_alloc_arg args;
620 xfs_agnumber_t agno;
621 int error;
622 xfs_agino_t newino;
623 xfs_agino_t newlen;
624 int isaligned = 0;
625
626
627 uint16_t allocmask = (uint16_t) -1;
628 struct xfs_inobt_rec_incore rec;
629 struct xfs_perag *pag;
630 struct xfs_ino_geometry *igeo = M_IGEO(tp->t_mountp);
631 int do_sparse = 0;
632
633 memset(&args, 0, sizeof(args));
634 args.tp = tp;
635 args.mp = tp->t_mountp;
636 args.fsbno = NULLFSBLOCK;
637 args.oinfo = XFS_RMAP_OINFO_INODES;
638
639#ifdef DEBUG
640
641 if (xfs_sb_version_hassparseinodes(&tp->t_mountp->m_sb) &&
642 igeo->ialloc_min_blks < igeo->ialloc_blks)
643 do_sparse = prandom_u32() & 1;
644#endif
645
646
647
648
649
650 newlen = igeo->ialloc_inos;
651 if (igeo->maxicount &&
652 percpu_counter_read_positive(&args.mp->m_icount) + newlen >
653 igeo->maxicount)
654 return -ENOSPC;
655 args.minlen = args.maxlen = igeo->ialloc_blks;
656
657
658
659
660
661 agi = agbp->b_addr;
662 newino = be32_to_cpu(agi->agi_newino);
663 agno = be32_to_cpu(agi->agi_seqno);
664 args.agbno = XFS_AGINO_TO_AGBNO(args.mp, newino) +
665 igeo->ialloc_blks;
666 if (do_sparse)
667 goto sparse_alloc;
668 if (likely(newino != NULLAGINO &&
669 (args.agbno < be32_to_cpu(agi->agi_length)))) {
670 args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
671 args.type = XFS_ALLOCTYPE_THIS_BNO;
672 args.prod = 1;
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687 args.alignment = 1;
688 args.minalignslop = igeo->cluster_align - 1;
689
690
691 args.minleft = igeo->inobt_maxlevels;
692 if ((error = xfs_alloc_vextent(&args)))
693 return error;
694
695
696
697
698
699
700
701
702
703
704
705 args.minalignslop = 0;
706 }
707
708 if (unlikely(args.fsbno == NULLFSBLOCK)) {
709
710
711
712
713
714
715
716
717 isaligned = 0;
718 if (igeo->ialloc_align) {
719 ASSERT(!(args.mp->m_flags & XFS_MOUNT_NOALIGN));
720 args.alignment = args.mp->m_dalign;
721 isaligned = 1;
722 } else
723 args.alignment = igeo->cluster_align;
724
725
726
727
728
729 args.agbno = be32_to_cpu(agi->agi_root);
730 args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
731
732
733
734 args.type = XFS_ALLOCTYPE_NEAR_BNO;
735 args.prod = 1;
736
737
738
739 args.minleft = igeo->inobt_maxlevels;
740 if ((error = xfs_alloc_vextent(&args)))
741 return error;
742 }
743
744
745
746
747
748 if (isaligned && args.fsbno == NULLFSBLOCK) {
749 args.type = XFS_ALLOCTYPE_NEAR_BNO;
750 args.agbno = be32_to_cpu(agi->agi_root);
751 args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
752 args.alignment = igeo->cluster_align;
753 if ((error = xfs_alloc_vextent(&args)))
754 return error;
755 }
756
757
758
759
760
761 if (xfs_sb_version_hassparseinodes(&args.mp->m_sb) &&
762 igeo->ialloc_min_blks < igeo->ialloc_blks &&
763 args.fsbno == NULLFSBLOCK) {
764sparse_alloc:
765 args.type = XFS_ALLOCTYPE_NEAR_BNO;
766 args.agbno = be32_to_cpu(agi->agi_root);
767 args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
768 args.alignment = args.mp->m_sb.sb_spino_align;
769 args.prod = 1;
770
771 args.minlen = igeo->ialloc_min_blks;
772 args.maxlen = args.minlen;
773
774
775
776
777
778
779
780
781
782
783
784 args.min_agbno = args.mp->m_sb.sb_inoalignmt;
785 args.max_agbno = round_down(args.mp->m_sb.sb_agblocks,
786 args.mp->m_sb.sb_inoalignmt) -
787 igeo->ialloc_blks;
788
789 error = xfs_alloc_vextent(&args);
790 if (error)
791 return error;
792
793 newlen = XFS_AGB_TO_AGINO(args.mp, args.len);
794 ASSERT(newlen <= XFS_INODES_PER_CHUNK);
795 allocmask = (1 << (newlen / XFS_INODES_PER_HOLEMASK_BIT)) - 1;
796 }
797
798 if (args.fsbno == NULLFSBLOCK)
799 return 1;
800
801 ASSERT(args.len == args.minlen);
802
803
804
805
806
807
808
809
810
811
812 error = xfs_ialloc_inode_init(args.mp, tp, NULL, newlen, agno,
813 args.agbno, args.len, prandom_u32());
814
815 if (error)
816 return error;
817
818
819
820 newino = XFS_AGB_TO_AGINO(args.mp, args.agbno);
821
822 if (xfs_inobt_issparse(~allocmask)) {
823
824
825
826 xfs_align_sparse_ino(args.mp, &newino, &allocmask);
827
828 rec.ir_startino = newino;
829 rec.ir_holemask = ~allocmask;
830 rec.ir_count = newlen;
831 rec.ir_freecount = newlen;
832 rec.ir_free = XFS_INOBT_ALL_FREE;
833
834
835
836
837
838
839 error = xfs_inobt_insert_sprec(args.mp, tp, agbp, XFS_BTNUM_INO,
840 &rec, true);
841 if (error == -EFSCORRUPTED) {
842 xfs_alert(args.mp,
843 "invalid sparse inode record: ino 0x%llx holemask 0x%x count %u",
844 XFS_AGINO_TO_INO(args.mp, agno,
845 rec.ir_startino),
846 rec.ir_holemask, rec.ir_count);
847 xfs_force_shutdown(args.mp, SHUTDOWN_CORRUPT_INCORE);
848 }
849 if (error)
850 return error;
851
852
853
854
855
856
857
858
859
860
861
862
863 if (xfs_sb_version_hasfinobt(&args.mp->m_sb)) {
864 error = xfs_inobt_insert_sprec(args.mp, tp, agbp,
865 XFS_BTNUM_FINO, &rec,
866 false);
867 if (error)
868 return error;
869 }
870 } else {
871
872 error = xfs_inobt_insert(args.mp, tp, agbp, newino, newlen,
873 XFS_BTNUM_INO);
874 if (error)
875 return error;
876
877 if (xfs_sb_version_hasfinobt(&args.mp->m_sb)) {
878 error = xfs_inobt_insert(args.mp, tp, agbp, newino,
879 newlen, XFS_BTNUM_FINO);
880 if (error)
881 return error;
882 }
883 }
884
885
886
887
888 be32_add_cpu(&agi->agi_count, newlen);
889 be32_add_cpu(&agi->agi_freecount, newlen);
890 pag = agbp->b_pag;
891 pag->pagi_freecount += newlen;
892 pag->pagi_count += newlen;
893 agi->agi_newino = cpu_to_be32(newino);
894
895
896
897
898 xfs_ialloc_log_agi(tp, agbp,
899 XFS_AGI_COUNT | XFS_AGI_FREECOUNT | XFS_AGI_NEWINO);
900
901
902
903 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, (long)newlen);
904 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, (long)newlen);
905 return 0;
906}
907
908STATIC xfs_agnumber_t
909xfs_ialloc_next_ag(
910 xfs_mount_t *mp)
911{
912 xfs_agnumber_t agno;
913
914 spin_lock(&mp->m_agirotor_lock);
915 agno = mp->m_agirotor;
916 if (++mp->m_agirotor >= mp->m_maxagi)
917 mp->m_agirotor = 0;
918 spin_unlock(&mp->m_agirotor_lock);
919
920 return agno;
921}
922
923
924
925
926
927STATIC xfs_agnumber_t
928xfs_ialloc_ag_select(
929 xfs_trans_t *tp,
930 xfs_ino_t parent,
931 umode_t mode)
932{
933 xfs_agnumber_t agcount;
934 xfs_agnumber_t agno;
935 int flags;
936 xfs_extlen_t ineed;
937 xfs_extlen_t longest = 0;
938 xfs_mount_t *mp;
939 int needspace;
940 xfs_perag_t *pag;
941 xfs_agnumber_t pagno;
942 int error;
943
944
945
946
947
948 needspace = S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode);
949 mp = tp->t_mountp;
950 agcount = mp->m_maxagi;
951 if (S_ISDIR(mode))
952 pagno = xfs_ialloc_next_ag(mp);
953 else {
954 pagno = XFS_INO_TO_AGNO(mp, parent);
955 if (pagno >= agcount)
956 pagno = 0;
957 }
958
959 ASSERT(pagno < agcount);
960
961
962
963
964
965
966
967
968 agno = pagno;
969 flags = XFS_ALLOC_FLAG_TRYLOCK;
970 for (;;) {
971 pag = xfs_perag_get(mp, agno);
972 if (!pag->pagi_inodeok) {
973 xfs_ialloc_next_ag(mp);
974 goto nextag;
975 }
976
977 if (!pag->pagi_init) {
978 error = xfs_ialloc_pagi_init(mp, tp, agno);
979 if (error)
980 goto nextag;
981 }
982
983 if (pag->pagi_freecount) {
984 xfs_perag_put(pag);
985 return agno;
986 }
987
988 if (!pag->pagf_init) {
989 error = xfs_alloc_pagf_init(mp, tp, agno, flags);
990 if (error)
991 goto nextag;
992 }
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010 ineed = M_IGEO(mp)->ialloc_min_blks;
1011 if (flags && ineed > 1)
1012 ineed += M_IGEO(mp)->cluster_align;
1013 longest = pag->pagf_longest;
1014 if (!longest)
1015 longest = pag->pagf_flcount > 0;
1016
1017 if (pag->pagf_freeblks >= needspace + ineed &&
1018 longest >= ineed) {
1019 xfs_perag_put(pag);
1020 return agno;
1021 }
1022nextag:
1023 xfs_perag_put(pag);
1024
1025
1026
1027
1028 if (XFS_FORCED_SHUTDOWN(mp))
1029 return NULLAGNUMBER;
1030 agno++;
1031 if (agno >= agcount)
1032 agno = 0;
1033 if (agno == pagno) {
1034 if (flags == 0)
1035 return NULLAGNUMBER;
1036 flags = 0;
1037 }
1038 }
1039}
1040
1041
1042
1043
1044STATIC int
1045xfs_ialloc_next_rec(
1046 struct xfs_btree_cur *cur,
1047 xfs_inobt_rec_incore_t *rec,
1048 int *done,
1049 int left)
1050{
1051 int error;
1052 int i;
1053
1054 if (left)
1055 error = xfs_btree_decrement(cur, 0, &i);
1056 else
1057 error = xfs_btree_increment(cur, 0, &i);
1058
1059 if (error)
1060 return error;
1061 *done = !i;
1062 if (i) {
1063 error = xfs_inobt_get_rec(cur, rec, &i);
1064 if (error)
1065 return error;
1066 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1))
1067 return -EFSCORRUPTED;
1068 }
1069
1070 return 0;
1071}
1072
1073STATIC int
1074xfs_ialloc_get_rec(
1075 struct xfs_btree_cur *cur,
1076 xfs_agino_t agino,
1077 xfs_inobt_rec_incore_t *rec,
1078 int *done)
1079{
1080 int error;
1081 int i;
1082
1083 error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_EQ, &i);
1084 if (error)
1085 return error;
1086 *done = !i;
1087 if (i) {
1088 error = xfs_inobt_get_rec(cur, rec, &i);
1089 if (error)
1090 return error;
1091 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1))
1092 return -EFSCORRUPTED;
1093 }
1094
1095 return 0;
1096}
1097
1098
1099
1100
1101
1102
1103STATIC int
1104xfs_inobt_first_free_inode(
1105 struct xfs_inobt_rec_incore *rec)
1106{
1107 xfs_inofree_t realfree;
1108
1109
1110 if (!xfs_inobt_issparse(rec->ir_holemask))
1111 return xfs_lowbit64(rec->ir_free);
1112
1113 realfree = xfs_inobt_irec_to_allocmask(rec);
1114 realfree &= rec->ir_free;
1115
1116 return xfs_lowbit64(realfree);
1117}
1118
1119
1120
1121
1122STATIC int
1123xfs_dialloc_ag_inobt(
1124 struct xfs_trans *tp,
1125 struct xfs_buf *agbp,
1126 xfs_ino_t parent,
1127 xfs_ino_t *inop)
1128{
1129 struct xfs_mount *mp = tp->t_mountp;
1130 struct xfs_agi *agi = agbp->b_addr;
1131 xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno);
1132 xfs_agnumber_t pagno = XFS_INO_TO_AGNO(mp, parent);
1133 xfs_agino_t pagino = XFS_INO_TO_AGINO(mp, parent);
1134 struct xfs_perag *pag = agbp->b_pag;
1135 struct xfs_btree_cur *cur, *tcur;
1136 struct xfs_inobt_rec_incore rec, trec;
1137 xfs_ino_t ino;
1138 int error;
1139 int offset;
1140 int i, j;
1141 int searchdistance = 10;
1142
1143 ASSERT(pag->pagi_init);
1144 ASSERT(pag->pagi_inodeok);
1145 ASSERT(pag->pagi_freecount > 0);
1146
1147 restart_pagno:
1148 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO);
1149
1150
1151
1152
1153 if (!pagino)
1154 pagino = be32_to_cpu(agi->agi_newino);
1155
1156 error = xfs_check_agi_freecount(cur, agi);
1157 if (error)
1158 goto error0;
1159
1160
1161
1162
1163 if (pagno == agno) {
1164 int doneleft;
1165 int doneright;
1166
1167 error = xfs_inobt_lookup(cur, pagino, XFS_LOOKUP_LE, &i);
1168 if (error)
1169 goto error0;
1170 if (XFS_IS_CORRUPT(mp, i != 1)) {
1171 error = -EFSCORRUPTED;
1172 goto error0;
1173 }
1174
1175 error = xfs_inobt_get_rec(cur, &rec, &j);
1176 if (error)
1177 goto error0;
1178 if (XFS_IS_CORRUPT(mp, j != 1)) {
1179 error = -EFSCORRUPTED;
1180 goto error0;
1181 }
1182
1183 if (rec.ir_freecount > 0) {
1184
1185
1186
1187
1188 goto alloc_inode;
1189 }
1190
1191
1192
1193
1194
1195
1196
1197 error = xfs_btree_dup_cursor(cur, &tcur);
1198 if (error)
1199 goto error0;
1200
1201
1202
1203
1204 if (pagino != NULLAGINO &&
1205 pag->pagl_pagino == pagino &&
1206 pag->pagl_leftrec != NULLAGINO &&
1207 pag->pagl_rightrec != NULLAGINO) {
1208 error = xfs_ialloc_get_rec(tcur, pag->pagl_leftrec,
1209 &trec, &doneleft);
1210 if (error)
1211 goto error1;
1212
1213 error = xfs_ialloc_get_rec(cur, pag->pagl_rightrec,
1214 &rec, &doneright);
1215 if (error)
1216 goto error1;
1217 } else {
1218
1219 error = xfs_ialloc_next_rec(tcur, &trec, &doneleft, 1);
1220 if (error)
1221 goto error1;
1222
1223
1224 error = xfs_ialloc_next_rec(cur, &rec, &doneright, 0);
1225 if (error)
1226 goto error1;
1227 }
1228
1229
1230
1231
1232 while (--searchdistance > 0 && (!doneleft || !doneright)) {
1233 int useleft;
1234
1235
1236 if (!doneleft && !doneright) {
1237 useleft = pagino -
1238 (trec.ir_startino + XFS_INODES_PER_CHUNK - 1) <
1239 rec.ir_startino - pagino;
1240 } else {
1241 useleft = !doneleft;
1242 }
1243
1244
1245 if (useleft && trec.ir_freecount) {
1246 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1247 cur = tcur;
1248
1249 pag->pagl_leftrec = trec.ir_startino;
1250 pag->pagl_rightrec = rec.ir_startino;
1251 pag->pagl_pagino = pagino;
1252 rec = trec;
1253 goto alloc_inode;
1254 }
1255
1256
1257 if (!useleft && rec.ir_freecount) {
1258 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
1259
1260 pag->pagl_leftrec = trec.ir_startino;
1261 pag->pagl_rightrec = rec.ir_startino;
1262 pag->pagl_pagino = pagino;
1263 goto alloc_inode;
1264 }
1265
1266
1267 if (useleft) {
1268 error = xfs_ialloc_next_rec(tcur, &trec,
1269 &doneleft, 1);
1270 } else {
1271 error = xfs_ialloc_next_rec(cur, &rec,
1272 &doneright, 0);
1273 }
1274 if (error)
1275 goto error1;
1276 }
1277
1278 if (searchdistance <= 0) {
1279
1280
1281
1282
1283 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
1284 pag->pagl_leftrec = trec.ir_startino;
1285 pag->pagl_rightrec = rec.ir_startino;
1286 pag->pagl_pagino = pagino;
1287
1288 } else {
1289
1290
1291
1292
1293
1294
1295
1296 pag->pagl_pagino = NULLAGINO;
1297 pag->pagl_leftrec = NULLAGINO;
1298 pag->pagl_rightrec = NULLAGINO;
1299 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
1300 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1301 goto restart_pagno;
1302 }
1303 }
1304
1305
1306
1307
1308
1309 if (agi->agi_newino != cpu_to_be32(NULLAGINO)) {
1310 error = xfs_inobt_lookup(cur, be32_to_cpu(agi->agi_newino),
1311 XFS_LOOKUP_EQ, &i);
1312 if (error)
1313 goto error0;
1314
1315 if (i == 1) {
1316 error = xfs_inobt_get_rec(cur, &rec, &j);
1317 if (error)
1318 goto error0;
1319
1320 if (j == 1 && rec.ir_freecount > 0) {
1321
1322
1323
1324
1325 goto alloc_inode;
1326 }
1327 }
1328 }
1329
1330
1331
1332
1333 error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
1334 if (error)
1335 goto error0;
1336 if (XFS_IS_CORRUPT(mp, i != 1)) {
1337 error = -EFSCORRUPTED;
1338 goto error0;
1339 }
1340
1341 for (;;) {
1342 error = xfs_inobt_get_rec(cur, &rec, &i);
1343 if (error)
1344 goto error0;
1345 if (XFS_IS_CORRUPT(mp, i != 1)) {
1346 error = -EFSCORRUPTED;
1347 goto error0;
1348 }
1349 if (rec.ir_freecount > 0)
1350 break;
1351 error = xfs_btree_increment(cur, 0, &i);
1352 if (error)
1353 goto error0;
1354 if (XFS_IS_CORRUPT(mp, i != 1)) {
1355 error = -EFSCORRUPTED;
1356 goto error0;
1357 }
1358 }
1359
1360alloc_inode:
1361 offset = xfs_inobt_first_free_inode(&rec);
1362 ASSERT(offset >= 0);
1363 ASSERT(offset < XFS_INODES_PER_CHUNK);
1364 ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
1365 XFS_INODES_PER_CHUNK) == 0);
1366 ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset);
1367 rec.ir_free &= ~XFS_INOBT_MASK(offset);
1368 rec.ir_freecount--;
1369 error = xfs_inobt_update(cur, &rec);
1370 if (error)
1371 goto error0;
1372 be32_add_cpu(&agi->agi_freecount, -1);
1373 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
1374 pag->pagi_freecount--;
1375
1376 error = xfs_check_agi_freecount(cur, agi);
1377 if (error)
1378 goto error0;
1379
1380 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1381 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
1382 *inop = ino;
1383 return 0;
1384error1:
1385 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
1386error0:
1387 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1388 return error;
1389}
1390
1391
1392
1393
1394
1395STATIC int
1396xfs_dialloc_ag_finobt_near(
1397 xfs_agino_t pagino,
1398 struct xfs_btree_cur **ocur,
1399 struct xfs_inobt_rec_incore *rec)
1400{
1401 struct xfs_btree_cur *lcur = *ocur;
1402 struct xfs_btree_cur *rcur;
1403 struct xfs_inobt_rec_incore rrec;
1404 int error;
1405 int i, j;
1406
1407 error = xfs_inobt_lookup(lcur, pagino, XFS_LOOKUP_LE, &i);
1408 if (error)
1409 return error;
1410
1411 if (i == 1) {
1412 error = xfs_inobt_get_rec(lcur, rec, &i);
1413 if (error)
1414 return error;
1415 if (XFS_IS_CORRUPT(lcur->bc_mp, i != 1))
1416 return -EFSCORRUPTED;
1417
1418
1419
1420
1421
1422
1423 if (pagino >= rec->ir_startino &&
1424 pagino < (rec->ir_startino + XFS_INODES_PER_CHUNK))
1425 return 0;
1426 }
1427
1428 error = xfs_btree_dup_cursor(lcur, &rcur);
1429 if (error)
1430 return error;
1431
1432 error = xfs_inobt_lookup(rcur, pagino, XFS_LOOKUP_GE, &j);
1433 if (error)
1434 goto error_rcur;
1435 if (j == 1) {
1436 error = xfs_inobt_get_rec(rcur, &rrec, &j);
1437 if (error)
1438 goto error_rcur;
1439 if (XFS_IS_CORRUPT(lcur->bc_mp, j != 1)) {
1440 error = -EFSCORRUPTED;
1441 goto error_rcur;
1442 }
1443 }
1444
1445 if (XFS_IS_CORRUPT(lcur->bc_mp, i != 1 && j != 1)) {
1446 error = -EFSCORRUPTED;
1447 goto error_rcur;
1448 }
1449 if (i == 1 && j == 1) {
1450
1451
1452
1453
1454 if ((pagino - rec->ir_startino + XFS_INODES_PER_CHUNK - 1) >
1455 (rrec.ir_startino - pagino)) {
1456 *rec = rrec;
1457 xfs_btree_del_cursor(lcur, XFS_BTREE_NOERROR);
1458 *ocur = rcur;
1459 } else {
1460 xfs_btree_del_cursor(rcur, XFS_BTREE_NOERROR);
1461 }
1462 } else if (j == 1) {
1463
1464 *rec = rrec;
1465 xfs_btree_del_cursor(lcur, XFS_BTREE_NOERROR);
1466 *ocur = rcur;
1467 } else if (i == 1) {
1468
1469 xfs_btree_del_cursor(rcur, XFS_BTREE_NOERROR);
1470 }
1471
1472 return 0;
1473
1474error_rcur:
1475 xfs_btree_del_cursor(rcur, XFS_BTREE_ERROR);
1476 return error;
1477}
1478
1479
1480
1481
1482
1483STATIC int
1484xfs_dialloc_ag_finobt_newino(
1485 struct xfs_agi *agi,
1486 struct xfs_btree_cur *cur,
1487 struct xfs_inobt_rec_incore *rec)
1488{
1489 int error;
1490 int i;
1491
1492 if (agi->agi_newino != cpu_to_be32(NULLAGINO)) {
1493 error = xfs_inobt_lookup(cur, be32_to_cpu(agi->agi_newino),
1494 XFS_LOOKUP_EQ, &i);
1495 if (error)
1496 return error;
1497 if (i == 1) {
1498 error = xfs_inobt_get_rec(cur, rec, &i);
1499 if (error)
1500 return error;
1501 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1))
1502 return -EFSCORRUPTED;
1503 return 0;
1504 }
1505 }
1506
1507
1508
1509
1510 error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
1511 if (error)
1512 return error;
1513 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1))
1514 return -EFSCORRUPTED;
1515
1516 error = xfs_inobt_get_rec(cur, rec, &i);
1517 if (error)
1518 return error;
1519 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1))
1520 return -EFSCORRUPTED;
1521
1522 return 0;
1523}
1524
1525
1526
1527
1528
1529STATIC int
1530xfs_dialloc_ag_update_inobt(
1531 struct xfs_btree_cur *cur,
1532 struct xfs_inobt_rec_incore *frec,
1533 int offset)
1534{
1535 struct xfs_inobt_rec_incore rec;
1536 int error;
1537 int i;
1538
1539 error = xfs_inobt_lookup(cur, frec->ir_startino, XFS_LOOKUP_EQ, &i);
1540 if (error)
1541 return error;
1542 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1))
1543 return -EFSCORRUPTED;
1544
1545 error = xfs_inobt_get_rec(cur, &rec, &i);
1546 if (error)
1547 return error;
1548 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1))
1549 return -EFSCORRUPTED;
1550 ASSERT((XFS_AGINO_TO_OFFSET(cur->bc_mp, rec.ir_startino) %
1551 XFS_INODES_PER_CHUNK) == 0);
1552
1553 rec.ir_free &= ~XFS_INOBT_MASK(offset);
1554 rec.ir_freecount--;
1555
1556 if (XFS_IS_CORRUPT(cur->bc_mp,
1557 rec.ir_free != frec->ir_free ||
1558 rec.ir_freecount != frec->ir_freecount))
1559 return -EFSCORRUPTED;
1560
1561 return xfs_inobt_update(cur, &rec);
1562}
1563
1564
1565
1566
1567
1568
1569
1570
1571int
1572xfs_dialloc_ag(
1573 struct xfs_trans *tp,
1574 struct xfs_buf *agbp,
1575 xfs_ino_t parent,
1576 xfs_ino_t *inop)
1577{
1578 struct xfs_mount *mp = tp->t_mountp;
1579 struct xfs_agi *agi = agbp->b_addr;
1580 xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno);
1581 xfs_agnumber_t pagno = XFS_INO_TO_AGNO(mp, parent);
1582 xfs_agino_t pagino = XFS_INO_TO_AGINO(mp, parent);
1583 struct xfs_btree_cur *cur;
1584 struct xfs_btree_cur *icur;
1585 struct xfs_inobt_rec_incore rec;
1586 xfs_ino_t ino;
1587 int error;
1588 int offset;
1589 int i;
1590
1591 if (!xfs_sb_version_hasfinobt(&mp->m_sb))
1592 return xfs_dialloc_ag_inobt(tp, agbp, parent, inop);
1593
1594
1595
1596
1597
1598 if (!pagino)
1599 pagino = be32_to_cpu(agi->agi_newino);
1600
1601 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_FINO);
1602
1603 error = xfs_check_agi_freecount(cur, agi);
1604 if (error)
1605 goto error_cur;
1606
1607
1608
1609
1610
1611
1612 if (agno == pagno)
1613 error = xfs_dialloc_ag_finobt_near(pagino, &cur, &rec);
1614 else
1615 error = xfs_dialloc_ag_finobt_newino(agi, cur, &rec);
1616 if (error)
1617 goto error_cur;
1618
1619 offset = xfs_inobt_first_free_inode(&rec);
1620 ASSERT(offset >= 0);
1621 ASSERT(offset < XFS_INODES_PER_CHUNK);
1622 ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
1623 XFS_INODES_PER_CHUNK) == 0);
1624 ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset);
1625
1626
1627
1628
1629 rec.ir_free &= ~XFS_INOBT_MASK(offset);
1630 rec.ir_freecount--;
1631 if (rec.ir_freecount)
1632 error = xfs_inobt_update(cur, &rec);
1633 else
1634 error = xfs_btree_delete(cur, &i);
1635 if (error)
1636 goto error_cur;
1637
1638
1639
1640
1641
1642
1643
1644 icur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO);
1645
1646 error = xfs_check_agi_freecount(icur, agi);
1647 if (error)
1648 goto error_icur;
1649
1650 error = xfs_dialloc_ag_update_inobt(icur, &rec, offset);
1651 if (error)
1652 goto error_icur;
1653
1654
1655
1656
1657
1658 be32_add_cpu(&agi->agi_freecount, -1);
1659 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
1660 agbp->b_pag->pagi_freecount--;
1661
1662 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
1663
1664 error = xfs_check_agi_freecount(icur, agi);
1665 if (error)
1666 goto error_icur;
1667 error = xfs_check_agi_freecount(cur, agi);
1668 if (error)
1669 goto error_icur;
1670
1671 xfs_btree_del_cursor(icur, XFS_BTREE_NOERROR);
1672 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1673 *inop = ino;
1674 return 0;
1675
1676error_icur:
1677 xfs_btree_del_cursor(icur, XFS_BTREE_ERROR);
1678error_cur:
1679 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1680 return error;
1681}
1682
1683static int
1684xfs_dialloc_roll(
1685 struct xfs_trans **tpp,
1686 struct xfs_buf *agibp)
1687{
1688 struct xfs_trans *tp = *tpp;
1689 struct xfs_dquot_acct *dqinfo;
1690 int error;
1691
1692
1693
1694
1695
1696 xfs_trans_bhold(tp, agibp);
1697
1698
1699
1700
1701
1702
1703 dqinfo = tp->t_dqinfo;
1704 tp->t_dqinfo = NULL;
1705
1706 error = xfs_trans_roll(&tp);
1707
1708
1709 tp->t_dqinfo = dqinfo;
1710
1711 *tpp = tp;
1712 if (error)
1713 return error;
1714 xfs_trans_bjoin(tp, agibp);
1715 return 0;
1716}
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729int
1730xfs_dialloc_select_ag(
1731 struct xfs_trans **tpp,
1732 xfs_ino_t parent,
1733 umode_t mode,
1734 struct xfs_buf **IO_agbp)
1735{
1736 struct xfs_mount *mp = (*tpp)->t_mountp;
1737 struct xfs_buf *agbp;
1738 xfs_agnumber_t agno;
1739 int error;
1740 bool noroom = false;
1741 xfs_agnumber_t start_agno;
1742 struct xfs_perag *pag;
1743 struct xfs_ino_geometry *igeo = M_IGEO(mp);
1744 bool okalloc = true;
1745
1746 *IO_agbp = NULL;
1747
1748
1749
1750
1751
1752 start_agno = xfs_ialloc_ag_select(*tpp, parent, mode);
1753 if (start_agno == NULLAGNUMBER)
1754 return 0;
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764 if (igeo->maxicount &&
1765 percpu_counter_read_positive(&mp->m_icount) + igeo->ialloc_inos
1766 > igeo->maxicount) {
1767 noroom = true;
1768 okalloc = false;
1769 }
1770
1771
1772
1773
1774
1775
1776 agno = start_agno;
1777 for (;;) {
1778 pag = xfs_perag_get(mp, agno);
1779 if (!pag->pagi_inodeok) {
1780 xfs_ialloc_next_ag(mp);
1781 goto nextag;
1782 }
1783
1784 if (!pag->pagi_init) {
1785 error = xfs_ialloc_pagi_init(mp, *tpp, agno);
1786 if (error)
1787 break;
1788 }
1789
1790
1791
1792
1793 if (!pag->pagi_freecount && !okalloc)
1794 goto nextag;
1795
1796
1797
1798
1799
1800 error = xfs_ialloc_read_agi(mp, *tpp, agno, &agbp);
1801 if (error)
1802 break;
1803
1804 if (pag->pagi_freecount) {
1805 xfs_perag_put(pag);
1806 goto found_ag;
1807 }
1808
1809 if (!okalloc)
1810 goto nextag_relse_buffer;
1811
1812 error = xfs_ialloc_ag_alloc(*tpp, agbp);
1813 if (error < 0) {
1814 xfs_trans_brelse(*tpp, agbp);
1815
1816 if (error == -ENOSPC)
1817 error = 0;
1818 break;
1819 }
1820
1821 if (error == 0) {
1822
1823
1824
1825
1826
1827 ASSERT(pag->pagi_freecount > 0);
1828 xfs_perag_put(pag);
1829
1830 error = xfs_dialloc_roll(tpp, agbp);
1831 if (error) {
1832 xfs_buf_relse(agbp);
1833 return error;
1834 }
1835 goto found_ag;
1836 }
1837
1838nextag_relse_buffer:
1839 xfs_trans_brelse(*tpp, agbp);
1840nextag:
1841 xfs_perag_put(pag);
1842 if (++agno == mp->m_sb.sb_agcount)
1843 agno = 0;
1844 if (agno == start_agno)
1845 return noroom ? -ENOSPC : 0;
1846 }
1847
1848 xfs_perag_put(pag);
1849 return error;
1850found_ag:
1851 *IO_agbp = agbp;
1852 return 0;
1853}
1854
1855
1856
1857
1858
1859
1860STATIC void
1861xfs_difree_inode_chunk(
1862 struct xfs_trans *tp,
1863 xfs_agnumber_t agno,
1864 struct xfs_inobt_rec_incore *rec)
1865{
1866 struct xfs_mount *mp = tp->t_mountp;
1867 xfs_agblock_t sagbno = XFS_AGINO_TO_AGBNO(mp,
1868 rec->ir_startino);
1869 int startidx, endidx;
1870 int nextbit;
1871 xfs_agblock_t agbno;
1872 int contigblk;
1873 DECLARE_BITMAP(holemask, XFS_INOBT_HOLEMASK_BITS);
1874
1875 if (!xfs_inobt_issparse(rec->ir_holemask)) {
1876
1877 xfs_bmap_add_free(tp, XFS_AGB_TO_FSB(mp, agno, sagbno),
1878 M_IGEO(mp)->ialloc_blks,
1879 &XFS_RMAP_OINFO_INODES);
1880 return;
1881 }
1882
1883
1884 ASSERT(sizeof(rec->ir_holemask) <= sizeof(holemask[0]));
1885 holemask[0] = rec->ir_holemask;
1886
1887
1888
1889
1890
1891
1892
1893 startidx = endidx = find_first_zero_bit(holemask,
1894 XFS_INOBT_HOLEMASK_BITS);
1895 nextbit = startidx + 1;
1896 while (startidx < XFS_INOBT_HOLEMASK_BITS) {
1897 nextbit = find_next_zero_bit(holemask, XFS_INOBT_HOLEMASK_BITS,
1898 nextbit);
1899
1900
1901
1902
1903 if (nextbit != XFS_INOBT_HOLEMASK_BITS &&
1904 nextbit == endidx + 1) {
1905 endidx = nextbit;
1906 goto next;
1907 }
1908
1909
1910
1911
1912
1913
1914 agbno = sagbno + (startidx * XFS_INODES_PER_HOLEMASK_BIT) /
1915 mp->m_sb.sb_inopblock;
1916 contigblk = ((endidx - startidx + 1) *
1917 XFS_INODES_PER_HOLEMASK_BIT) /
1918 mp->m_sb.sb_inopblock;
1919
1920 ASSERT(agbno % mp->m_sb.sb_spino_align == 0);
1921 ASSERT(contigblk % mp->m_sb.sb_spino_align == 0);
1922 xfs_bmap_add_free(tp, XFS_AGB_TO_FSB(mp, agno, agbno),
1923 contigblk, &XFS_RMAP_OINFO_INODES);
1924
1925
1926 startidx = endidx = nextbit;
1927
1928next:
1929 nextbit++;
1930 }
1931}
1932
1933STATIC int
1934xfs_difree_inobt(
1935 struct xfs_mount *mp,
1936 struct xfs_trans *tp,
1937 struct xfs_buf *agbp,
1938 xfs_agino_t agino,
1939 struct xfs_icluster *xic,
1940 struct xfs_inobt_rec_incore *orec)
1941{
1942 struct xfs_agi *agi = agbp->b_addr;
1943 xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno);
1944 struct xfs_btree_cur *cur;
1945 struct xfs_inobt_rec_incore rec;
1946 int ilen;
1947 int error;
1948 int i;
1949 int off;
1950
1951 ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC));
1952 ASSERT(XFS_AGINO_TO_AGBNO(mp, agino) < be32_to_cpu(agi->agi_length));
1953
1954
1955
1956
1957 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO);
1958
1959 error = xfs_check_agi_freecount(cur, agi);
1960 if (error)
1961 goto error0;
1962
1963
1964
1965
1966 if ((error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i))) {
1967 xfs_warn(mp, "%s: xfs_inobt_lookup() returned error %d.",
1968 __func__, error);
1969 goto error0;
1970 }
1971 if (XFS_IS_CORRUPT(mp, i != 1)) {
1972 error = -EFSCORRUPTED;
1973 goto error0;
1974 }
1975 error = xfs_inobt_get_rec(cur, &rec, &i);
1976 if (error) {
1977 xfs_warn(mp, "%s: xfs_inobt_get_rec() returned error %d.",
1978 __func__, error);
1979 goto error0;
1980 }
1981 if (XFS_IS_CORRUPT(mp, i != 1)) {
1982 error = -EFSCORRUPTED;
1983 goto error0;
1984 }
1985
1986
1987
1988 off = agino - rec.ir_startino;
1989 ASSERT(off >= 0 && off < XFS_INODES_PER_CHUNK);
1990 ASSERT(!(rec.ir_free & XFS_INOBT_MASK(off)));
1991
1992
1993
1994 rec.ir_free |= XFS_INOBT_MASK(off);
1995 rec.ir_freecount++;
1996
1997
1998
1999
2000
2001
2002 if (!(mp->m_flags & XFS_MOUNT_IKEEP) &&
2003 rec.ir_free == XFS_INOBT_ALL_FREE &&
2004 mp->m_sb.sb_inopblock <= XFS_INODES_PER_CHUNK) {
2005 struct xfs_perag *pag = agbp->b_pag;
2006
2007 xic->deleted = true;
2008 xic->first_ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino);
2009 xic->alloc = xfs_inobt_irec_to_allocmask(&rec);
2010
2011
2012
2013
2014
2015
2016 ilen = rec.ir_freecount;
2017 be32_add_cpu(&agi->agi_count, -ilen);
2018 be32_add_cpu(&agi->agi_freecount, -(ilen - 1));
2019 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_COUNT | XFS_AGI_FREECOUNT);
2020 pag->pagi_freecount -= ilen - 1;
2021 pag->pagi_count -= ilen;
2022 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, -ilen);
2023 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -(ilen - 1));
2024
2025 if ((error = xfs_btree_delete(cur, &i))) {
2026 xfs_warn(mp, "%s: xfs_btree_delete returned error %d.",
2027 __func__, error);
2028 goto error0;
2029 }
2030
2031 xfs_difree_inode_chunk(tp, agno, &rec);
2032 } else {
2033 xic->deleted = false;
2034
2035 error = xfs_inobt_update(cur, &rec);
2036 if (error) {
2037 xfs_warn(mp, "%s: xfs_inobt_update returned error %d.",
2038 __func__, error);
2039 goto error0;
2040 }
2041
2042
2043
2044
2045 be32_add_cpu(&agi->agi_freecount, 1);
2046 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
2047 agbp->b_pag->pagi_freecount++;
2048 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, 1);
2049 }
2050
2051 error = xfs_check_agi_freecount(cur, agi);
2052 if (error)
2053 goto error0;
2054
2055 *orec = rec;
2056 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
2057 return 0;
2058
2059error0:
2060 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
2061 return error;
2062}
2063
2064
2065
2066
2067STATIC int
2068xfs_difree_finobt(
2069 struct xfs_mount *mp,
2070 struct xfs_trans *tp,
2071 struct xfs_buf *agbp,
2072 xfs_agino_t agino,
2073 struct xfs_inobt_rec_incore *ibtrec)
2074{
2075 struct xfs_agi *agi = agbp->b_addr;
2076 xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno);
2077 struct xfs_btree_cur *cur;
2078 struct xfs_inobt_rec_incore rec;
2079 int offset = agino - ibtrec->ir_startino;
2080 int error;
2081 int i;
2082
2083 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_FINO);
2084
2085 error = xfs_inobt_lookup(cur, ibtrec->ir_startino, XFS_LOOKUP_EQ, &i);
2086 if (error)
2087 goto error;
2088 if (i == 0) {
2089
2090
2091
2092
2093
2094 if (XFS_IS_CORRUPT(mp, ibtrec->ir_freecount != 1)) {
2095 error = -EFSCORRUPTED;
2096 goto error;
2097 }
2098
2099 error = xfs_inobt_insert_rec(cur, ibtrec->ir_holemask,
2100 ibtrec->ir_count,
2101 ibtrec->ir_freecount,
2102 ibtrec->ir_free, &i);
2103 if (error)
2104 goto error;
2105 ASSERT(i == 1);
2106
2107 goto out;
2108 }
2109
2110
2111
2112
2113
2114
2115
2116
2117 error = xfs_inobt_get_rec(cur, &rec, &i);
2118 if (error)
2119 goto error;
2120 if (XFS_IS_CORRUPT(mp, i != 1)) {
2121 error = -EFSCORRUPTED;
2122 goto error;
2123 }
2124
2125 rec.ir_free |= XFS_INOBT_MASK(offset);
2126 rec.ir_freecount++;
2127
2128 if (XFS_IS_CORRUPT(mp,
2129 rec.ir_free != ibtrec->ir_free ||
2130 rec.ir_freecount != ibtrec->ir_freecount)) {
2131 error = -EFSCORRUPTED;
2132 goto error;
2133 }
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147 if (rec.ir_free == XFS_INOBT_ALL_FREE &&
2148 mp->m_sb.sb_inopblock <= XFS_INODES_PER_CHUNK &&
2149 !(mp->m_flags & XFS_MOUNT_IKEEP)) {
2150 error = xfs_btree_delete(cur, &i);
2151 if (error)
2152 goto error;
2153 ASSERT(i == 1);
2154 } else {
2155 error = xfs_inobt_update(cur, &rec);
2156 if (error)
2157 goto error;
2158 }
2159
2160out:
2161 error = xfs_check_agi_freecount(cur, agi);
2162 if (error)
2163 goto error;
2164
2165 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
2166 return 0;
2167
2168error:
2169 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
2170 return error;
2171}
2172
2173
2174
2175
2176
2177
2178
2179int
2180xfs_difree(
2181 struct xfs_trans *tp,
2182 xfs_ino_t inode,
2183 struct xfs_icluster *xic)
2184{
2185
2186 xfs_agblock_t agbno;
2187 struct xfs_buf *agbp;
2188 xfs_agino_t agino;
2189 xfs_agnumber_t agno;
2190 int error;
2191 struct xfs_mount *mp;
2192 struct xfs_inobt_rec_incore rec;
2193
2194 mp = tp->t_mountp;
2195
2196
2197
2198
2199 agno = XFS_INO_TO_AGNO(mp, inode);
2200 if (agno >= mp->m_sb.sb_agcount) {
2201 xfs_warn(mp, "%s: agno >= mp->m_sb.sb_agcount (%d >= %d).",
2202 __func__, agno, mp->m_sb.sb_agcount);
2203 ASSERT(0);
2204 return -EINVAL;
2205 }
2206 agino = XFS_INO_TO_AGINO(mp, inode);
2207 if (inode != XFS_AGINO_TO_INO(mp, agno, agino)) {
2208 xfs_warn(mp, "%s: inode != XFS_AGINO_TO_INO() (%llu != %llu).",
2209 __func__, (unsigned long long)inode,
2210 (unsigned long long)XFS_AGINO_TO_INO(mp, agno, agino));
2211 ASSERT(0);
2212 return -EINVAL;
2213 }
2214 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
2215 if (agbno >= mp->m_sb.sb_agblocks) {
2216 xfs_warn(mp, "%s: agbno >= mp->m_sb.sb_agblocks (%d >= %d).",
2217 __func__, agbno, mp->m_sb.sb_agblocks);
2218 ASSERT(0);
2219 return -EINVAL;
2220 }
2221
2222
2223
2224 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
2225 if (error) {
2226 xfs_warn(mp, "%s: xfs_ialloc_read_agi() returned error %d.",
2227 __func__, error);
2228 return error;
2229 }
2230
2231
2232
2233
2234 error = xfs_difree_inobt(mp, tp, agbp, agino, xic, &rec);
2235 if (error)
2236 goto error0;
2237
2238
2239
2240
2241 if (xfs_sb_version_hasfinobt(&mp->m_sb)) {
2242 error = xfs_difree_finobt(mp, tp, agbp, agino, &rec);
2243 if (error)
2244 goto error0;
2245 }
2246
2247 return 0;
2248
2249error0:
2250 return error;
2251}
2252
2253STATIC int
2254xfs_imap_lookup(
2255 struct xfs_mount *mp,
2256 struct xfs_trans *tp,
2257 xfs_agnumber_t agno,
2258 xfs_agino_t agino,
2259 xfs_agblock_t agbno,
2260 xfs_agblock_t *chunk_agbno,
2261 xfs_agblock_t *offset_agbno,
2262 int flags)
2263{
2264 struct xfs_inobt_rec_incore rec;
2265 struct xfs_btree_cur *cur;
2266 struct xfs_buf *agbp;
2267 int error;
2268 int i;
2269
2270 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
2271 if (error) {
2272 xfs_alert(mp,
2273 "%s: xfs_ialloc_read_agi() returned error %d, agno %d",
2274 __func__, error, agno);
2275 return error;
2276 }
2277
2278
2279
2280
2281
2282
2283
2284 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO);
2285 error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i);
2286 if (!error) {
2287 if (i)
2288 error = xfs_inobt_get_rec(cur, &rec, &i);
2289 if (!error && i == 0)
2290 error = -EINVAL;
2291 }
2292
2293 xfs_trans_brelse(tp, agbp);
2294 xfs_btree_del_cursor(cur, error);
2295 if (error)
2296 return error;
2297
2298
2299 if (rec.ir_startino > agino ||
2300 rec.ir_startino + M_IGEO(mp)->ialloc_inos <= agino)
2301 return -EINVAL;
2302
2303
2304 if ((flags & XFS_IGET_UNTRUSTED) &&
2305 (rec.ir_free & XFS_INOBT_MASK(agino - rec.ir_startino)))
2306 return -EINVAL;
2307
2308 *chunk_agbno = XFS_AGINO_TO_AGBNO(mp, rec.ir_startino);
2309 *offset_agbno = agbno - *chunk_agbno;
2310 return 0;
2311}
2312
2313
2314
2315
2316int
2317xfs_imap(
2318 xfs_mount_t *mp,
2319 xfs_trans_t *tp,
2320 xfs_ino_t ino,
2321 struct xfs_imap *imap,
2322 uint flags)
2323{
2324 xfs_agblock_t agbno;
2325 xfs_agino_t agino;
2326 xfs_agnumber_t agno;
2327 xfs_agblock_t chunk_agbno;
2328 xfs_agblock_t cluster_agbno;
2329 int error;
2330 int offset;
2331 xfs_agblock_t offset_agbno;
2332
2333 ASSERT(ino != NULLFSINO);
2334
2335
2336
2337
2338 agno = XFS_INO_TO_AGNO(mp, ino);
2339 agino = XFS_INO_TO_AGINO(mp, ino);
2340 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
2341 if (agno >= mp->m_sb.sb_agcount || agbno >= mp->m_sb.sb_agblocks ||
2342 ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
2343#ifdef DEBUG
2344
2345
2346
2347
2348 if (flags & XFS_IGET_UNTRUSTED)
2349 return -EINVAL;
2350 if (agno >= mp->m_sb.sb_agcount) {
2351 xfs_alert(mp,
2352 "%s: agno (%d) >= mp->m_sb.sb_agcount (%d)",
2353 __func__, agno, mp->m_sb.sb_agcount);
2354 }
2355 if (agbno >= mp->m_sb.sb_agblocks) {
2356 xfs_alert(mp,
2357 "%s: agbno (0x%llx) >= mp->m_sb.sb_agblocks (0x%lx)",
2358 __func__, (unsigned long long)agbno,
2359 (unsigned long)mp->m_sb.sb_agblocks);
2360 }
2361 if (ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
2362 xfs_alert(mp,
2363 "%s: ino (0x%llx) != XFS_AGINO_TO_INO() (0x%llx)",
2364 __func__, ino,
2365 XFS_AGINO_TO_INO(mp, agno, agino));
2366 }
2367 xfs_stack_trace();
2368#endif
2369 return -EINVAL;
2370 }
2371
2372
2373
2374
2375
2376
2377
2378
2379 if (flags & XFS_IGET_UNTRUSTED) {
2380 error = xfs_imap_lookup(mp, tp, agno, agino, agbno,
2381 &chunk_agbno, &offset_agbno, flags);
2382 if (error)
2383 return error;
2384 goto out_map;
2385 }
2386
2387
2388
2389
2390
2391 if (M_IGEO(mp)->blocks_per_cluster == 1) {
2392 offset = XFS_INO_TO_OFFSET(mp, ino);
2393 ASSERT(offset < mp->m_sb.sb_inopblock);
2394
2395 imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, agbno);
2396 imap->im_len = XFS_FSB_TO_BB(mp, 1);
2397 imap->im_boffset = (unsigned short)(offset <<
2398 mp->m_sb.sb_inodelog);
2399 return 0;
2400 }
2401
2402
2403
2404
2405
2406
2407 if (M_IGEO(mp)->inoalign_mask) {
2408 offset_agbno = agbno & M_IGEO(mp)->inoalign_mask;
2409 chunk_agbno = agbno - offset_agbno;
2410 } else {
2411 error = xfs_imap_lookup(mp, tp, agno, agino, agbno,
2412 &chunk_agbno, &offset_agbno, flags);
2413 if (error)
2414 return error;
2415 }
2416
2417out_map:
2418 ASSERT(agbno >= chunk_agbno);
2419 cluster_agbno = chunk_agbno +
2420 ((offset_agbno / M_IGEO(mp)->blocks_per_cluster) *
2421 M_IGEO(mp)->blocks_per_cluster);
2422 offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
2423 XFS_INO_TO_OFFSET(mp, ino);
2424
2425 imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, cluster_agbno);
2426 imap->im_len = XFS_FSB_TO_BB(mp, M_IGEO(mp)->blocks_per_cluster);
2427 imap->im_boffset = (unsigned short)(offset << mp->m_sb.sb_inodelog);
2428
2429
2430
2431
2432
2433
2434
2435 if ((imap->im_blkno + imap->im_len) >
2436 XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)) {
2437 xfs_alert(mp,
2438 "%s: (im_blkno (0x%llx) + im_len (0x%llx)) > sb_dblocks (0x%llx)",
2439 __func__, (unsigned long long) imap->im_blkno,
2440 (unsigned long long) imap->im_len,
2441 XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks));
2442 return -EINVAL;
2443 }
2444 return 0;
2445}
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459void
2460xfs_ialloc_log_agi(
2461 xfs_trans_t *tp,
2462 struct xfs_buf *bp,
2463 int fields)
2464{
2465 int first;
2466 int last;
2467 static const short offsets[] = {
2468
2469 offsetof(xfs_agi_t, agi_magicnum),
2470 offsetof(xfs_agi_t, agi_versionnum),
2471 offsetof(xfs_agi_t, agi_seqno),
2472 offsetof(xfs_agi_t, agi_length),
2473 offsetof(xfs_agi_t, agi_count),
2474 offsetof(xfs_agi_t, agi_root),
2475 offsetof(xfs_agi_t, agi_level),
2476 offsetof(xfs_agi_t, agi_freecount),
2477 offsetof(xfs_agi_t, agi_newino),
2478 offsetof(xfs_agi_t, agi_dirino),
2479 offsetof(xfs_agi_t, agi_unlinked),
2480 offsetof(xfs_agi_t, agi_free_root),
2481 offsetof(xfs_agi_t, agi_free_level),
2482 offsetof(xfs_agi_t, agi_iblocks),
2483 sizeof(xfs_agi_t)
2484 };
2485#ifdef DEBUG
2486 struct xfs_agi *agi = bp->b_addr;
2487
2488 ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC));
2489#endif
2490
2491
2492
2493
2494
2495
2496 if (fields & XFS_AGI_ALL_BITS_R1) {
2497 xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS_R1,
2498 &first, &last);
2499 xfs_trans_log_buf(tp, bp, first, last);
2500 }
2501
2502
2503
2504
2505
2506 fields &= ~XFS_AGI_ALL_BITS_R1;
2507 if (fields) {
2508 xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS_R2,
2509 &first, &last);
2510 xfs_trans_log_buf(tp, bp, first, last);
2511 }
2512}
2513
2514static xfs_failaddr_t
2515xfs_agi_verify(
2516 struct xfs_buf *bp)
2517{
2518 struct xfs_mount *mp = bp->b_mount;
2519 struct xfs_agi *agi = bp->b_addr;
2520 int i;
2521
2522 if (xfs_sb_version_hascrc(&mp->m_sb)) {
2523 if (!uuid_equal(&agi->agi_uuid, &mp->m_sb.sb_meta_uuid))
2524 return __this_address;
2525 if (!xfs_log_check_lsn(mp, be64_to_cpu(agi->agi_lsn)))
2526 return __this_address;
2527 }
2528
2529
2530
2531
2532 if (!xfs_verify_magic(bp, agi->agi_magicnum))
2533 return __this_address;
2534 if (!XFS_AGI_GOOD_VERSION(be32_to_cpu(agi->agi_versionnum)))
2535 return __this_address;
2536
2537 if (be32_to_cpu(agi->agi_level) < 1 ||
2538 be32_to_cpu(agi->agi_level) > M_IGEO(mp)->inobt_maxlevels)
2539 return __this_address;
2540
2541 if (xfs_sb_version_hasfinobt(&mp->m_sb) &&
2542 (be32_to_cpu(agi->agi_free_level) < 1 ||
2543 be32_to_cpu(agi->agi_free_level) > M_IGEO(mp)->inobt_maxlevels))
2544 return __this_address;
2545
2546
2547
2548
2549
2550
2551
2552 if (bp->b_pag && be32_to_cpu(agi->agi_seqno) != bp->b_pag->pag_agno)
2553 return __this_address;
2554
2555 for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++) {
2556 if (agi->agi_unlinked[i] == cpu_to_be32(NULLAGINO))
2557 continue;
2558 if (!xfs_verify_ino(mp, be32_to_cpu(agi->agi_unlinked[i])))
2559 return __this_address;
2560 }
2561
2562 return NULL;
2563}
2564
2565static void
2566xfs_agi_read_verify(
2567 struct xfs_buf *bp)
2568{
2569 struct xfs_mount *mp = bp->b_mount;
2570 xfs_failaddr_t fa;
2571
2572 if (xfs_sb_version_hascrc(&mp->m_sb) &&
2573 !xfs_buf_verify_cksum(bp, XFS_AGI_CRC_OFF))
2574 xfs_verifier_error(bp, -EFSBADCRC, __this_address);
2575 else {
2576 fa = xfs_agi_verify(bp);
2577 if (XFS_TEST_ERROR(fa, mp, XFS_ERRTAG_IALLOC_READ_AGI))
2578 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
2579 }
2580}
2581
2582static void
2583xfs_agi_write_verify(
2584 struct xfs_buf *bp)
2585{
2586 struct xfs_mount *mp = bp->b_mount;
2587 struct xfs_buf_log_item *bip = bp->b_log_item;
2588 struct xfs_agi *agi = bp->b_addr;
2589 xfs_failaddr_t fa;
2590
2591 fa = xfs_agi_verify(bp);
2592 if (fa) {
2593 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
2594 return;
2595 }
2596
2597 if (!xfs_sb_version_hascrc(&mp->m_sb))
2598 return;
2599
2600 if (bip)
2601 agi->agi_lsn = cpu_to_be64(bip->bli_item.li_lsn);
2602 xfs_buf_update_cksum(bp, XFS_AGI_CRC_OFF);
2603}
2604
2605const struct xfs_buf_ops xfs_agi_buf_ops = {
2606 .name = "xfs_agi",
2607 .magic = { cpu_to_be32(XFS_AGI_MAGIC), cpu_to_be32(XFS_AGI_MAGIC) },
2608 .verify_read = xfs_agi_read_verify,
2609 .verify_write = xfs_agi_write_verify,
2610 .verify_struct = xfs_agi_verify,
2611};
2612
2613
2614
2615
2616int
2617xfs_read_agi(
2618 struct xfs_mount *mp,
2619 struct xfs_trans *tp,
2620 xfs_agnumber_t agno,
2621 struct xfs_buf **bpp)
2622{
2623 int error;
2624
2625 trace_xfs_read_agi(mp, agno);
2626
2627 ASSERT(agno != NULLAGNUMBER);
2628 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
2629 XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)),
2630 XFS_FSS_TO_BB(mp, 1), 0, bpp, &xfs_agi_buf_ops);
2631 if (error)
2632 return error;
2633 if (tp)
2634 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_AGI_BUF);
2635
2636 xfs_buf_set_ref(*bpp, XFS_AGI_REF);
2637 return 0;
2638}
2639
2640int
2641xfs_ialloc_read_agi(
2642 struct xfs_mount *mp,
2643 struct xfs_trans *tp,
2644 xfs_agnumber_t agno,
2645 struct xfs_buf **bpp)
2646{
2647 struct xfs_agi *agi;
2648 struct xfs_perag *pag;
2649 int error;
2650
2651 trace_xfs_ialloc_read_agi(mp, agno);
2652
2653 error = xfs_read_agi(mp, tp, agno, bpp);
2654 if (error)
2655 return error;
2656
2657 agi = (*bpp)->b_addr;
2658 pag = (*bpp)->b_pag;
2659 if (!pag->pagi_init) {
2660 pag->pagi_freecount = be32_to_cpu(agi->agi_freecount);
2661 pag->pagi_count = be32_to_cpu(agi->agi_count);
2662 pag->pagi_init = 1;
2663 }
2664
2665
2666
2667
2668
2669 ASSERT(pag->pagi_freecount == be32_to_cpu(agi->agi_freecount) ||
2670 XFS_FORCED_SHUTDOWN(mp));
2671 return 0;
2672}
2673
2674
2675
2676
2677int
2678xfs_ialloc_pagi_init(
2679 xfs_mount_t *mp,
2680 xfs_trans_t *tp,
2681 xfs_agnumber_t agno)
2682{
2683 struct xfs_buf *bp = NULL;
2684 int error;
2685
2686 error = xfs_ialloc_read_agi(mp, tp, agno, &bp);
2687 if (error)
2688 return error;
2689 if (bp)
2690 xfs_trans_brelse(tp, bp);
2691 return 0;
2692}
2693
2694
2695int
2696xfs_ialloc_has_inode_record(
2697 struct xfs_btree_cur *cur,
2698 xfs_agino_t low,
2699 xfs_agino_t high,
2700 bool *exists)
2701{
2702 struct xfs_inobt_rec_incore irec;
2703 xfs_agino_t agino;
2704 uint16_t holemask;
2705 int has_record;
2706 int i;
2707 int error;
2708
2709 *exists = false;
2710 error = xfs_inobt_lookup(cur, low, XFS_LOOKUP_LE, &has_record);
2711 while (error == 0 && has_record) {
2712 error = xfs_inobt_get_rec(cur, &irec, &has_record);
2713 if (error || irec.ir_startino > high)
2714 break;
2715
2716 agino = irec.ir_startino;
2717 holemask = irec.ir_holemask;
2718 for (i = 0; i < XFS_INOBT_HOLEMASK_BITS; holemask >>= 1,
2719 i++, agino += XFS_INODES_PER_HOLEMASK_BIT) {
2720 if (holemask & 1)
2721 continue;
2722 if (agino + XFS_INODES_PER_HOLEMASK_BIT > low &&
2723 agino <= high) {
2724 *exists = true;
2725 return 0;
2726 }
2727 }
2728
2729 error = xfs_btree_increment(cur, 0, &has_record);
2730 }
2731 return error;
2732}
2733
2734
2735int
2736xfs_ialloc_has_inodes_at_extent(
2737 struct xfs_btree_cur *cur,
2738 xfs_agblock_t bno,
2739 xfs_extlen_t len,
2740 bool *exists)
2741{
2742 xfs_agino_t low;
2743 xfs_agino_t high;
2744
2745 low = XFS_AGB_TO_AGINO(cur->bc_mp, bno);
2746 high = XFS_AGB_TO_AGINO(cur->bc_mp, bno + len) - 1;
2747
2748 return xfs_ialloc_has_inode_record(cur, low, high, exists);
2749}
2750
2751struct xfs_ialloc_count_inodes {
2752 xfs_agino_t count;
2753 xfs_agino_t freecount;
2754};
2755
2756
2757STATIC int
2758xfs_ialloc_count_inodes_rec(
2759 struct xfs_btree_cur *cur,
2760 union xfs_btree_rec *rec,
2761 void *priv)
2762{
2763 struct xfs_inobt_rec_incore irec;
2764 struct xfs_ialloc_count_inodes *ci = priv;
2765
2766 xfs_inobt_btrec_to_irec(cur->bc_mp, rec, &irec);
2767 ci->count += irec.ir_count;
2768 ci->freecount += irec.ir_freecount;
2769
2770 return 0;
2771}
2772
2773
2774int
2775xfs_ialloc_count_inodes(
2776 struct xfs_btree_cur *cur,
2777 xfs_agino_t *count,
2778 xfs_agino_t *freecount)
2779{
2780 struct xfs_ialloc_count_inodes ci = {0};
2781 int error;
2782
2783 ASSERT(cur->bc_btnum == XFS_BTNUM_INO);
2784 error = xfs_btree_query_all(cur, xfs_ialloc_count_inodes_rec, &ci);
2785 if (error)
2786 return error;
2787
2788 *count = ci.count;
2789 *freecount = ci.freecount;
2790 return 0;
2791}
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807void
2808xfs_ialloc_setup_geometry(
2809 struct xfs_mount *mp)
2810{
2811 struct xfs_sb *sbp = &mp->m_sb;
2812 struct xfs_ino_geometry *igeo = M_IGEO(mp);
2813 uint64_t icount;
2814 uint inodes;
2815
2816 igeo->new_diflags2 = 0;
2817 if (xfs_sb_version_hasbigtime(&mp->m_sb))
2818 igeo->new_diflags2 |= XFS_DIFLAG2_BIGTIME;
2819
2820
2821 igeo->agino_log = sbp->sb_inopblog + sbp->sb_agblklog;
2822 igeo->inobt_mxr[0] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 1);
2823 igeo->inobt_mxr[1] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 0);
2824 igeo->inobt_mnr[0] = igeo->inobt_mxr[0] / 2;
2825 igeo->inobt_mnr[1] = igeo->inobt_mxr[1] / 2;
2826
2827 igeo->ialloc_inos = max_t(uint16_t, XFS_INODES_PER_CHUNK,
2828 sbp->sb_inopblock);
2829 igeo->ialloc_blks = igeo->ialloc_inos >> sbp->sb_inopblog;
2830
2831 if (sbp->sb_spino_align)
2832 igeo->ialloc_min_blks = sbp->sb_spino_align;
2833 else
2834 igeo->ialloc_min_blks = igeo->ialloc_blks;
2835
2836
2837 inodes = (1LL << XFS_INO_AGINO_BITS(mp)) >> XFS_INODES_PER_CHUNK_LOG;
2838 igeo->inobt_maxlevels = xfs_btree_compute_maxlevels(igeo->inobt_mnr,
2839 inodes);
2840
2841
2842
2843
2844
2845
2846
2847 if (sbp->sb_imax_pct && igeo->ialloc_blks) {
2848
2849
2850
2851
2852 icount = sbp->sb_dblocks * sbp->sb_imax_pct;
2853 do_div(icount, 100);
2854 do_div(icount, igeo->ialloc_blks);
2855 igeo->maxicount = XFS_FSB_TO_INO(mp,
2856 icount * igeo->ialloc_blks);
2857 } else {
2858 igeo->maxicount = 0;
2859 }
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871 igeo->inode_cluster_size_raw = XFS_INODE_BIG_CLUSTER_SIZE;
2872 if (xfs_sb_version_has_v3inode(&mp->m_sb)) {
2873 int new_size = igeo->inode_cluster_size_raw;
2874
2875 new_size *= mp->m_sb.sb_inodesize / XFS_DINODE_MIN_SIZE;
2876 if (mp->m_sb.sb_inoalignmt >= XFS_B_TO_FSBT(mp, new_size))
2877 igeo->inode_cluster_size_raw = new_size;
2878 }
2879
2880
2881 if (igeo->inode_cluster_size_raw > mp->m_sb.sb_blocksize)
2882 igeo->blocks_per_cluster = XFS_B_TO_FSBT(mp,
2883 igeo->inode_cluster_size_raw);
2884 else
2885 igeo->blocks_per_cluster = 1;
2886 igeo->inode_cluster_size = XFS_FSB_TO_B(mp, igeo->blocks_per_cluster);
2887 igeo->inodes_per_cluster = XFS_FSB_TO_INO(mp, igeo->blocks_per_cluster);
2888
2889
2890 if (xfs_sb_version_hasalign(&mp->m_sb) &&
2891 mp->m_sb.sb_inoalignmt >= igeo->blocks_per_cluster)
2892 igeo->cluster_align = mp->m_sb.sb_inoalignmt;
2893 else
2894 igeo->cluster_align = 1;
2895 igeo->inoalign_mask = igeo->cluster_align - 1;
2896 igeo->cluster_align_inodes = XFS_FSB_TO_INO(mp, igeo->cluster_align);
2897
2898
2899
2900
2901
2902 if (mp->m_dalign && igeo->inoalign_mask &&
2903 !(mp->m_dalign & igeo->inoalign_mask))
2904 igeo->ialloc_align = mp->m_dalign;
2905 else
2906 igeo->ialloc_align = 0;
2907}
2908
2909
2910xfs_ino_t
2911xfs_ialloc_calc_rootino(
2912 struct xfs_mount *mp,
2913 int sunit)
2914{
2915 struct xfs_ino_geometry *igeo = M_IGEO(mp);
2916 xfs_agblock_t first_bno;
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926 first_bno = howmany(4 * mp->m_sb.sb_sectsize, mp->m_sb.sb_blocksize);
2927
2928
2929 first_bno += 2;
2930
2931
2932 first_bno += 1;
2933
2934
2935 first_bno += xfs_alloc_min_freelist(mp, NULL);
2936
2937
2938 if (xfs_sb_version_hasfinobt(&mp->m_sb))
2939 first_bno++;
2940
2941
2942 if (xfs_sb_version_hasrmapbt(&mp->m_sb))
2943 first_bno++;
2944
2945
2946 if (xfs_sb_version_hasreflink(&mp->m_sb))
2947 first_bno++;
2948
2949
2950
2951
2952
2953
2954
2955
2956 if (mp->m_sb.sb_logstart &&
2957 XFS_FSB_TO_AGNO(mp, mp->m_sb.sb_logstart) == 0)
2958 first_bno += mp->m_sb.sb_logblocks;
2959
2960
2961
2962
2963
2964 if (xfs_sb_version_hasdalign(&mp->m_sb) && igeo->ialloc_align > 0)
2965 first_bno = roundup(first_bno, sunit);
2966 else if (xfs_sb_version_hasalign(&mp->m_sb) &&
2967 mp->m_sb.sb_inoalignmt > 1)
2968 first_bno = roundup(first_bno, mp->m_sb.sb_inoalignmt);
2969
2970 return XFS_AGINO_TO_INO(mp, 0, XFS_AGB_TO_AGINO(mp, first_bno));
2971}
2972