diff options
author | Christoph Hellwig <hch@infradead.org> | 2012-07-04 10:54:46 -0400 |
---|---|---|
committer | Ben Myers <bpm@sgi.com> | 2012-07-29 16:56:49 -0400 |
commit | f2ecc5e453134a13c3b2b0f2cac52ab2d5c540d7 (patch) | |
tree | 58fdbdc513a494048fb66c424be6c3a07c645bc0 /fs | |
parent | 824c313139c2ce678011bf11c4823a0c99651c1f (diff) |
xfs: split xfs_dialloc
Move the actual allocation once we have selected an allocation group into a
separate helper, and make xfs_dialloc a wrapper around it.
Signed-off-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Dave Chinner <dchinner@redhat.com>
Reviewed-by: Ben Myers <bpm@sgi.com>
Signed-off-by: Ben Myers <bpm@sgi.com>
Diffstat (limited to 'fs')
-rw-r--r-- | fs/xfs/xfs_ialloc.c | 349 |
1 files changed, 174 insertions, 175 deletions
diff --git a/fs/xfs/xfs_ialloc.c b/fs/xfs/xfs_ialloc.c index 30b816d1f7e0..a124b9f88aae 100644 --- a/fs/xfs/xfs_ialloc.c +++ b/fs/xfs/xfs_ialloc.c | |||
@@ -607,188 +607,35 @@ xfs_ialloc_get_rec( | |||
607 | } | 607 | } |
608 | 608 | ||
609 | /* | 609 | /* |
610 | * Visible inode allocation functions. | 610 | * Allocate an inode. |
611 | */ | ||
612 | |||
613 | /* | ||
614 | * Allocate an inode on disk. | ||
615 | * Mode is used to tell whether the new inode will need space, and whether | ||
616 | * it is a directory. | ||
617 | * | 611 | * |
618 | * The arguments IO_agbp and alloc_done are defined to work within | 612 | * The caller selected an AG for us, and made sure that free inodes are |
619 | * the constraint of one allocation per transaction. | 613 | * available. |
620 | * xfs_dialloc() is designed to be called twice if it has to do an | ||
621 | * allocation to make more free inodes. On the first call, | ||
622 | * IO_agbp should be set to NULL. If an inode is available, | ||
623 | * i.e., xfs_dialloc() did not need to do an allocation, an inode | ||
624 | * number is returned. In this case, IO_agbp would be set to the | ||
625 | * current ag_buf and alloc_done set to false. | ||
626 | * If an allocation needed to be done, xfs_dialloc would return | ||
627 | * the current ag_buf in IO_agbp and set alloc_done to true. | ||
628 | * The caller should then commit the current transaction, allocate a new | ||
629 | * transaction, and call xfs_dialloc() again, passing in the previous | ||
630 | * value of IO_agbp. IO_agbp should be held across the transactions. | ||
631 | * Since the agbp is locked across the two calls, the second call is | ||
632 | * guaranteed to have a free inode available. | ||
633 | * | ||
634 | * Once we successfully pick an inode its number is returned and the | ||
635 | * on-disk data structures are updated. The inode itself is not read | ||
636 | * in, since doing so would break ordering constraints with xfs_reclaim. | ||
637 | */ | 614 | */ |
638 | int | 615 | STATIC int |
639 | xfs_dialloc( | 616 | xfs_dialloc_ag( |
640 | xfs_trans_t *tp, /* transaction pointer */ | 617 | struct xfs_trans *tp, |
641 | xfs_ino_t parent, /* parent inode (directory) */ | 618 | struct xfs_buf *agbp, |
642 | umode_t mode, /* mode bits for new inode */ | 619 | xfs_ino_t parent, |
643 | int okalloc, /* ok to allocate more space */ | 620 | xfs_ino_t *inop) |
644 | xfs_buf_t **IO_agbp, /* in/out ag header's buffer */ | ||
645 | boolean_t *alloc_done, /* true if we needed to replenish | ||
646 | inode freelist */ | ||
647 | xfs_ino_t *inop) /* inode number allocated */ | ||
648 | { | 621 | { |
649 | xfs_agnumber_t agcount; /* number of allocation groups */ | 622 | struct xfs_mount *mp = tp->t_mountp; |
650 | xfs_buf_t *agbp; /* allocation group header's buffer */ | 623 | struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp); |
651 | xfs_agnumber_t agno; /* allocation group number */ | 624 | xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno); |
652 | xfs_agi_t *agi; /* allocation group header structure */ | 625 | xfs_agnumber_t pagno = XFS_INO_TO_AGNO(mp, parent); |
653 | xfs_btree_cur_t *cur; /* inode allocation btree cursor */ | 626 | xfs_agino_t pagino = XFS_INO_TO_AGINO(mp, parent); |
654 | int error; /* error return value */ | 627 | struct xfs_perag *pag; |
655 | int i; /* result code */ | 628 | struct xfs_btree_cur *cur, *tcur; |
656 | int ialloced; /* inode allocation status */ | 629 | struct xfs_inobt_rec_incore rec, trec; |
657 | int noroom = 0; /* no space for inode blk allocation */ | 630 | xfs_ino_t ino; |
658 | xfs_ino_t ino; /* fs-relative inode to be returned */ | 631 | int error; |
659 | /* REFERENCED */ | 632 | int offset; |
660 | int j; /* result code */ | 633 | int i, j; |
661 | xfs_mount_t *mp; /* file system mount structure */ | ||
662 | int offset; /* index of inode in chunk */ | ||
663 | xfs_agino_t pagino; /* parent's AG relative inode # */ | ||
664 | xfs_agnumber_t pagno; /* parent's AG number */ | ||
665 | xfs_inobt_rec_incore_t rec; /* inode allocation record */ | ||
666 | xfs_agnumber_t tagno; /* testing allocation group number */ | ||
667 | xfs_btree_cur_t *tcur; /* temp cursor */ | ||
668 | xfs_inobt_rec_incore_t trec; /* temp inode allocation record */ | ||
669 | struct xfs_perag *pag; | ||
670 | |||
671 | |||
672 | if (*IO_agbp == NULL) { | ||
673 | /* | ||
674 | * We do not have an agbp, so select an initial allocation | ||
675 | * group for inode allocation. | ||
676 | */ | ||
677 | agbp = xfs_ialloc_ag_select(tp, parent, mode, okalloc); | ||
678 | /* | ||
679 | * Couldn't find an allocation group satisfying the | ||
680 | * criteria, give up. | ||
681 | */ | ||
682 | if (!agbp) { | ||
683 | *inop = NULLFSINO; | ||
684 | return 0; | ||
685 | } | ||
686 | agi = XFS_BUF_TO_AGI(agbp); | ||
687 | ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC)); | ||
688 | } else { | ||
689 | /* | ||
690 | * Continue where we left off before. In this case, we | ||
691 | * know that the allocation group has free inodes. | ||
692 | */ | ||
693 | agbp = *IO_agbp; | ||
694 | agi = XFS_BUF_TO_AGI(agbp); | ||
695 | ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC)); | ||
696 | ASSERT(be32_to_cpu(agi->agi_freecount) > 0); | ||
697 | } | ||
698 | mp = tp->t_mountp; | ||
699 | agcount = mp->m_sb.sb_agcount; | ||
700 | agno = be32_to_cpu(agi->agi_seqno); | ||
701 | tagno = agno; | ||
702 | pagno = XFS_INO_TO_AGNO(mp, parent); | ||
703 | pagino = XFS_INO_TO_AGINO(mp, parent); | ||
704 | |||
705 | /* | ||
706 | * If we have already hit the ceiling of inode blocks then clear | ||
707 | * okalloc so we scan all available agi structures for a free | ||
708 | * inode. | ||
709 | */ | ||
710 | |||
711 | if (mp->m_maxicount && | ||
712 | mp->m_sb.sb_icount + XFS_IALLOC_INODES(mp) > mp->m_maxicount) { | ||
713 | noroom = 1; | ||
714 | okalloc = 0; | ||
715 | } | ||
716 | 634 | ||
717 | /* | ||
718 | * Loop until we find an allocation group that either has free inodes | ||
719 | * or in which we can allocate some inodes. Iterate through the | ||
720 | * allocation groups upward, wrapping at the end. | ||
721 | */ | ||
722 | *alloc_done = B_FALSE; | ||
723 | while (!agi->agi_freecount) { | ||
724 | /* | ||
725 | * Don't do anything if we're not supposed to allocate | ||
726 | * any blocks, just go on to the next ag. | ||
727 | */ | ||
728 | if (okalloc) { | ||
729 | /* | ||
730 | * Try to allocate some new inodes in the allocation | ||
731 | * group. | ||
732 | */ | ||
733 | if ((error = xfs_ialloc_ag_alloc(tp, agbp, &ialloced))) { | ||
734 | xfs_trans_brelse(tp, agbp); | ||
735 | if (error == ENOSPC) { | ||
736 | *inop = NULLFSINO; | ||
737 | return 0; | ||
738 | } else | ||
739 | return error; | ||
740 | } | ||
741 | if (ialloced) { | ||
742 | /* | ||
743 | * We successfully allocated some inodes, return | ||
744 | * the current context to the caller so that it | ||
745 | * can commit the current transaction and call | ||
746 | * us again where we left off. | ||
747 | */ | ||
748 | ASSERT(be32_to_cpu(agi->agi_freecount) > 0); | ||
749 | *alloc_done = B_TRUE; | ||
750 | *IO_agbp = agbp; | ||
751 | *inop = NULLFSINO; | ||
752 | return 0; | ||
753 | } | ||
754 | } | ||
755 | /* | ||
756 | * If it failed, give up on this ag. | ||
757 | */ | ||
758 | xfs_trans_brelse(tp, agbp); | ||
759 | /* | ||
760 | * Go on to the next ag: get its ag header. | ||
761 | */ | ||
762 | nextag: | ||
763 | if (++tagno == agcount) | ||
764 | tagno = 0; | ||
765 | if (tagno == agno) { | ||
766 | *inop = NULLFSINO; | ||
767 | return noroom ? ENOSPC : 0; | ||
768 | } | ||
769 | pag = xfs_perag_get(mp, tagno); | ||
770 | if (pag->pagi_inodeok == 0) { | ||
771 | xfs_perag_put(pag); | ||
772 | goto nextag; | ||
773 | } | ||
774 | error = xfs_ialloc_read_agi(mp, tp, tagno, &agbp); | ||
775 | xfs_perag_put(pag); | ||
776 | if (error) | ||
777 | goto nextag; | ||
778 | agi = XFS_BUF_TO_AGI(agbp); | ||
779 | ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC)); | ||
780 | } | ||
781 | /* | ||
782 | * Here with an allocation group that has a free inode. | ||
783 | * Reset agno since we may have chosen a new ag in the | ||
784 | * loop above. | ||
785 | */ | ||
786 | agno = tagno; | ||
787 | *IO_agbp = NULL; | ||
788 | pag = xfs_perag_get(mp, agno); | 635 | pag = xfs_perag_get(mp, agno); |
789 | 636 | ||
790 | restart_pagno: | 637 | restart_pagno: |
791 | cur = xfs_inobt_init_cursor(mp, tp, agbp, be32_to_cpu(agi->agi_seqno)); | 638 | cur = xfs_inobt_init_cursor(mp, tp, agbp, agno); |
792 | /* | 639 | /* |
793 | * If pagino is 0 (this is the root inode allocation) use newino. | 640 | * If pagino is 0 (this is the root inode allocation) use newino. |
794 | * This must work because we've just allocated some. | 641 | * This must work because we've just allocated some. |
@@ -1021,6 +868,158 @@ error0: | |||
1021 | } | 868 | } |
1022 | 869 | ||
1023 | /* | 870 | /* |
871 | * Allocate an inode on disk. | ||
872 | * | ||
873 | * Mode is used to tell whether the new inode will need space, and whether it | ||
874 | * is a directory. | ||
875 | * | ||
876 | * This function is designed to be called twice if it has to do an allocation | ||
877 | * to make more free inodes. On the first call, *IO_agbp should be set to NULL. | ||
878 | * If an inode is available without having to performn an allocation, an inode | ||
879 | * number is returned. In this case, *IO_agbp would be NULL. If an allocation | ||
880 | * needes to be done, xfs_dialloc would return the current AGI buffer in | ||
881 | * *IO_agbp. The caller should then commit the current transaction, allocate a | ||
882 | * new transaction, and call xfs_dialloc() again, passing in the previous value | ||
883 | * of *IO_agbp. IO_agbp should be held across the transactions. Since the AGI | ||
884 | * buffer is locked across the two calls, the second call is guaranteed to have | ||
885 | * a free inode available. | ||
886 | * | ||
887 | * Once we successfully pick an inode its number is returned and the on-disk | ||
888 | * data structures are updated. The inode itself is not read in, since doing so | ||
889 | * would break ordering constraints with xfs_reclaim. | ||
890 | */ | ||
891 | int | ||
892 | xfs_dialloc( | ||
893 | struct xfs_trans *tp, | ||
894 | xfs_ino_t parent, | ||
895 | umode_t mode, | ||
896 | int okalloc, | ||
897 | struct xfs_buf **IO_agbp, | ||
898 | boolean_t *alloc_done, | ||
899 | xfs_ino_t *inop) | ||
900 | { | ||
901 | struct xfs_buf *agbp; | ||
902 | xfs_agnumber_t agno; | ||
903 | struct xfs_agi *agi; | ||
904 | int error; | ||
905 | int ialloced; | ||
906 | int noroom = 0; | ||
907 | struct xfs_mount *mp; | ||
908 | xfs_agnumber_t tagno; | ||
909 | struct xfs_perag *pag; | ||
910 | |||
911 | if (*IO_agbp == NULL) { | ||
912 | /* | ||
913 | * We do not have an agbp, so select an initial allocation | ||
914 | * group for inode allocation. | ||
915 | */ | ||
916 | agbp = xfs_ialloc_ag_select(tp, parent, mode, okalloc); | ||
917 | /* | ||
918 | * Couldn't find an allocation group satisfying the | ||
919 | * criteria, give up. | ||
920 | */ | ||
921 | if (!agbp) { | ||
922 | *inop = NULLFSINO; | ||
923 | return 0; | ||
924 | } | ||
925 | agi = XFS_BUF_TO_AGI(agbp); | ||
926 | ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC)); | ||
927 | } else { | ||
928 | /* | ||
929 | * Continue where we left off before. In this case, we | ||
930 | * know that the allocation group has free inodes. | ||
931 | */ | ||
932 | agbp = *IO_agbp; | ||
933 | agi = XFS_BUF_TO_AGI(agbp); | ||
934 | ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC)); | ||
935 | ASSERT(be32_to_cpu(agi->agi_freecount) > 0); | ||
936 | } | ||
937 | mp = tp->t_mountp; | ||
938 | agno = be32_to_cpu(agi->agi_seqno); | ||
939 | tagno = agno; | ||
940 | |||
941 | /* | ||
942 | * If we have already hit the ceiling of inode blocks then clear | ||
943 | * okalloc so we scan all available agi structures for a free | ||
944 | * inode. | ||
945 | */ | ||
946 | |||
947 | if (mp->m_maxicount && | ||
948 | mp->m_sb.sb_icount + XFS_IALLOC_INODES(mp) > mp->m_maxicount) { | ||
949 | noroom = 1; | ||
950 | okalloc = 0; | ||
951 | } | ||
952 | |||
953 | /* | ||
954 | * Loop until we find an allocation group that either has free inodes | ||
955 | * or in which we can allocate some inodes. Iterate through the | ||
956 | * allocation groups upward, wrapping at the end. | ||
957 | */ | ||
958 | *alloc_done = B_FALSE; | ||
959 | while (!agi->agi_freecount) { | ||
960 | /* | ||
961 | * Don't do anything if we're not supposed to allocate | ||
962 | * any blocks, just go on to the next ag. | ||
963 | */ | ||
964 | if (okalloc) { | ||
965 | /* | ||
966 | * Try to allocate some new inodes in the allocation | ||
967 | * group. | ||
968 | */ | ||
969 | if ((error = xfs_ialloc_ag_alloc(tp, agbp, &ialloced))) { | ||
970 | xfs_trans_brelse(tp, agbp); | ||
971 | if (error == ENOSPC) { | ||
972 | *inop = NULLFSINO; | ||
973 | return 0; | ||
974 | } else | ||
975 | return error; | ||
976 | } | ||
977 | if (ialloced) { | ||
978 | /* | ||
979 | * We successfully allocated some inodes, return | ||
980 | * the current context to the caller so that it | ||
981 | * can commit the current transaction and call | ||
982 | * us again where we left off. | ||
983 | */ | ||
984 | ASSERT(be32_to_cpu(agi->agi_freecount) > 0); | ||
985 | *alloc_done = B_TRUE; | ||
986 | *IO_agbp = agbp; | ||
987 | *inop = NULLFSINO; | ||
988 | return 0; | ||
989 | } | ||
990 | } | ||
991 | /* | ||
992 | * If it failed, give up on this ag. | ||
993 | */ | ||
994 | xfs_trans_brelse(tp, agbp); | ||
995 | /* | ||
996 | * Go on to the next ag: get its ag header. | ||
997 | */ | ||
998 | nextag: | ||
999 | if (++tagno == mp->m_sb.sb_agcount) | ||
1000 | tagno = 0; | ||
1001 | if (tagno == agno) { | ||
1002 | *inop = NULLFSINO; | ||
1003 | return noroom ? ENOSPC : 0; | ||
1004 | } | ||
1005 | pag = xfs_perag_get(mp, tagno); | ||
1006 | if (pag->pagi_inodeok == 0) { | ||
1007 | xfs_perag_put(pag); | ||
1008 | goto nextag; | ||
1009 | } | ||
1010 | error = xfs_ialloc_read_agi(mp, tp, tagno, &agbp); | ||
1011 | xfs_perag_put(pag); | ||
1012 | if (error) | ||
1013 | goto nextag; | ||
1014 | agi = XFS_BUF_TO_AGI(agbp); | ||
1015 | ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC)); | ||
1016 | } | ||
1017 | |||
1018 | *IO_agbp = NULL; | ||
1019 | return xfs_dialloc_ag(tp, agbp, parent, inop); | ||
1020 | } | ||
1021 | |||
1022 | /* | ||
1024 | * Free disk inode. Carefully avoids touching the incore inode, all | 1023 | * Free disk inode. Carefully avoids touching the incore inode, all |
1025 | * manipulations incore are the caller's responsibility. | 1024 | * manipulations incore are the caller's responsibility. |
1026 | * The on-disk inode is not changed by this operation, only the | 1025 | * The on-disk inode is not changed by this operation, only the |