Skip to content

Commit 1abcf26

Browse files
Dave Chinnerdjwong
authored andcommitted
xfs: move on-disk inode allocation out of xfs_ialloc()
So xfs_ialloc() will only address in-core inode allocation then, Also, rename xfs_ialloc() to xfs_dir_ialloc_init() in order to keep everything in xfs_inode.c under the same namespace. Reviewed-by: Darrick J. Wong <darrick.wong@oracle.com> Reviewed-by: Christoph Hellwig <hch@lst.de> Signed-off-by: Dave Chinner <dchinner@redhat.com> Signed-off-by: Gao Xiang <hsiangkao@redhat.com> Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
1 parent aececc9 commit 1abcf26

File tree

3 files changed

+86
-161
lines changed

3 files changed

+86
-161
lines changed

fs/xfs/xfs_inode.c

Lines changed: 70 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -761,68 +761,26 @@ xfs_inode_inherit_flags2(
761761
}
762762

763763
/*
764-
* Allocate an inode on disk and return a copy of its in-core version.
765-
* The in-core inode is locked exclusively. Set mode, nlink, and rdev
766-
* appropriately within the inode. The uid and gid for the inode are
767-
* set according to the contents of the given cred structure.
768-
*
769-
* Use xfs_dialloc() to allocate the on-disk inode. If xfs_dialloc()
770-
* has a free inode available, call xfs_iget() to obtain the in-core
771-
* version of the allocated inode. Finally, fill in the inode and
772-
* log its initial contents. In this case, ialloc_context would be
773-
* set to NULL.
774-
*
775-
* If xfs_dialloc() does not have an available inode, it will replenish
776-
* its supply by doing an allocation. Since we can only do one
777-
* allocation within a transaction without deadlocks, we must commit
778-
* the current transaction before returning the inode itself.
779-
* In this case, therefore, we will set ialloc_context and return.
780-
* The caller should then commit the current transaction, start a new
781-
* transaction, and call xfs_ialloc() again to actually get the inode.
782-
*
783-
* To ensure that some other process does not grab the inode that
784-
* was allocated during the first call to xfs_ialloc(), this routine
785-
* also returns the [locked] bp pointing to the head of the freelist
786-
* as ialloc_context. The caller should hold this buffer across
787-
* the commit and pass it back into this routine on the second call.
788-
*
789-
* If we are allocating quota inodes, we do not have a parent inode
790-
* to attach to or associate with (i.e. pip == NULL) because they
791-
* are not linked into the directory structure - they are attached
792-
* directly to the superblock - and so have no parent.
764+
* Initialise a newly allocated inode and return the in-core inode to the
765+
* caller locked exclusively.
793766
*/
794767
static int
795-
xfs_ialloc(
796-
xfs_trans_t *tp,
797-
xfs_inode_t *pip,
798-
umode_t mode,
799-
xfs_nlink_t nlink,
800-
dev_t rdev,
801-
prid_t prid,
802-
xfs_buf_t **ialloc_context,
803-
xfs_inode_t **ipp)
804-
{
805-
struct xfs_mount *mp = tp->t_mountp;
806-
xfs_ino_t ino;
807-
xfs_inode_t *ip;
808-
uint flags;
809-
int error;
810-
struct timespec64 tv;
811-
struct inode *inode;
812-
813-
/*
814-
* Call the space management code to pick
815-
* the on-disk inode to be allocated.
816-
*/
817-
error = xfs_dialloc(tp, pip ? pip->i_ino : 0, mode,
818-
ialloc_context, &ino);
819-
if (error)
820-
return error;
821-
if (*ialloc_context || ino == NULLFSINO) {
822-
*ipp = NULL;
823-
return 0;
824-
}
825-
ASSERT(*ialloc_context == NULL);
768+
xfs_init_new_inode(
769+
struct xfs_trans *tp,
770+
struct xfs_inode *pip,
771+
xfs_ino_t ino,
772+
umode_t mode,
773+
xfs_nlink_t nlink,
774+
dev_t rdev,
775+
prid_t prid,
776+
struct xfs_inode **ipp)
777+
{
778+
struct xfs_mount *mp = tp->t_mountp;
779+
struct xfs_inode *ip;
780+
unsigned int flags;
781+
int error;
782+
struct timespec64 tv;
783+
struct inode *inode;
826784

827785
/*
828786
* Protect against obviously corrupt allocation btree records. Later
@@ -837,14 +795,13 @@ xfs_ialloc(
837795
}
838796

839797
/*
840-
* Get the in-core inode with the lock held exclusively.
841-
* This is because we're setting fields here we need
842-
* to prevent others from looking at until we're done.
798+
* Get the in-core inode with the lock held exclusively to prevent
799+
* others from looking at until we're done.
843800
*/
844-
error = xfs_iget(mp, tp, ino, XFS_IGET_CREATE,
845-
XFS_ILOCK_EXCL, &ip);
801+
error = xfs_iget(mp, tp, ino, XFS_IGET_CREATE, XFS_ILOCK_EXCL, &ip);
846802
if (error)
847803
return error;
804+
848805
ASSERT(ip != NULL);
849806
inode = VFS_I(ip);
850807
inode->i_mode = mode;
@@ -932,108 +889,76 @@ xfs_ialloc(
932889
}
933890

934891
/*
935-
* Allocates a new inode from disk and return a pointer to the
936-
* incore copy. This routine will internally commit the current
937-
* transaction and allocate a new one if the Space Manager needed
938-
* to do an allocation to replenish the inode free-list.
939-
*
940-
* This routine is designed to be called from xfs_create and
941-
* xfs_create_dir.
892+
* Allocates a new inode from disk and return a pointer to the incore copy. This
893+
* routine will internally commit the current transaction and allocate a new one
894+
* if we needed to allocate more on-disk free inodes to perform the requested
895+
* operation.
942896
*
897+
* If we are allocating quota inodes, we do not have a parent inode to attach to
898+
* or associate with (i.e. dp == NULL) because they are not linked into the
899+
* directory structure - they are attached directly to the superblock - and so
900+
* have no parent.
943901
*/
944902
int
945903
xfs_dir_ialloc(
946-
xfs_trans_t **tpp, /* input: current transaction;
947-
output: may be a new transaction. */
948-
xfs_inode_t *dp, /* directory within whose allocate
949-
the inode. */
950-
umode_t mode,
951-
xfs_nlink_t nlink,
952-
dev_t rdev,
953-
prid_t prid, /* project id */
954-
xfs_inode_t **ipp) /* pointer to inode; it will be
955-
locked. */
956-
{
957-
xfs_trans_t *tp;
958-
xfs_inode_t *ip;
959-
xfs_buf_t *ialloc_context = NULL;
960-
int code;
961-
962-
tp = *tpp;
963-
ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
904+
struct xfs_trans **tpp,
905+
struct xfs_inode *dp,
906+
umode_t mode,
907+
xfs_nlink_t nlink,
908+
dev_t rdev,
909+
prid_t prid,
910+
struct xfs_inode **ipp)
911+
{
912+
struct xfs_buf *ialloc_context = NULL;
913+
xfs_ino_t parent_ino = dp ? dp->i_ino : 0;
914+
xfs_ino_t ino;
915+
int error;
964916

965-
/*
966-
* xfs_ialloc will return a pointer to an incore inode if
967-
* the Space Manager has an available inode on the free
968-
* list. Otherwise, it will do an allocation and replenish
969-
* the freelist. Since we can only do one allocation per
970-
* transaction without deadlocks, we will need to commit the
971-
* current transaction and start a new one. We will then
972-
* need to call xfs_ialloc again to get the inode.
973-
*
974-
* If xfs_ialloc did an allocation to replenish the freelist,
975-
* it returns the bp containing the head of the freelist as
976-
* ialloc_context. We will hold a lock on it across the
977-
* transaction commit so that no other process can steal
978-
* the inode(s) that we've just allocated.
979-
*/
980-
code = xfs_ialloc(tp, dp, mode, nlink, rdev, prid, &ialloc_context,
981-
&ip);
917+
ASSERT((*tpp)->t_flags & XFS_TRANS_PERM_LOG_RES);
982918

983919
/*
984-
* Return an error if we were unable to allocate a new inode.
985-
* This should only happen if we run out of space on disk or
986-
* encounter a disk error.
920+
* Call the space management code to pick the on-disk inode to be
921+
* allocated and replenish the freelist. Since we can only do one
922+
* allocation per transaction without deadlocks, we will need to
923+
* commit the current transaction and start a new one.
924+
* If xfs_dialloc did an allocation to replenish the freelist, it
925+
* returns the bp containing the head of the freelist as
926+
* ialloc_context. We will hold a lock on it across the transaction
927+
* commit so that no other process can steal the inode(s) that we've
928+
* just allocated.
987929
*/
988-
if (code) {
989-
*ipp = NULL;
990-
return code;
991-
}
992-
if (!ialloc_context && !ip) {
993-
*ipp = NULL;
994-
return -ENOSPC;
995-
}
930+
error = xfs_dialloc(*tpp, parent_ino, mode, &ialloc_context, &ino);
931+
if (error)
932+
return error;
996933

997934
/*
998935
* If the AGI buffer is non-NULL, then we were unable to get an
999936
* inode in one operation. We need to commit the current
1000-
* transaction and call xfs_ialloc() again. It is guaranteed
937+
* transaction and call xfs_dialloc() again. It is guaranteed
1001938
* to succeed the second time.
1002939
*/
1003940
if (ialloc_context) {
1004-
code = xfs_dialloc_roll(&tp, ialloc_context);
1005-
if (code) {
941+
error = xfs_dialloc_roll(tpp, ialloc_context);
942+
if (error) {
1006943
xfs_buf_relse(ialloc_context);
1007-
*tpp = tp;
1008-
*ipp = NULL;
1009-
return code;
944+
return error;
1010945
}
1011-
1012-
/*
1013-
* Call ialloc again. Since we've locked out all
1014-
* other allocations in this allocation group,
1015-
* this call should always succeed.
1016-
*/
1017-
code = xfs_ialloc(tp, dp, mode, nlink, rdev, prid,
1018-
&ialloc_context, &ip);
1019-
1020946
/*
1021-
* If we get an error at this point, return to the caller
1022-
* so that the current transaction can be aborted.
947+
* Call dialloc again. Since we've locked out all other
948+
* allocations in this allocation group, this call should
949+
* always succeed.
1023950
*/
1024-
if (code) {
1025-
*tpp = tp;
1026-
*ipp = NULL;
1027-
return code;
1028-
}
1029-
ASSERT(!ialloc_context && ip);
1030-
951+
error = xfs_dialloc(*tpp, parent_ino, mode, &ialloc_context,
952+
&ino);
953+
if (error)
954+
return error;
955+
ASSERT(!ialloc_context);
1031956
}
1032957

1033-
*ipp = ip;
1034-
*tpp = tp;
958+
if (ino == NULLFSINO)
959+
return -ENOSPC;
1035960

1036-
return 0;
961+
return xfs_init_new_inode(*tpp, dp, ino, mode, nlink, rdev, prid, ipp);
1037962
}
1038963

1039964
/*

fs/xfs/xfs_inode.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -407,9 +407,9 @@ void xfs_lock_two_inodes(struct xfs_inode *ip0, uint ip0_mode,
407407
xfs_extlen_t xfs_get_extsz_hint(struct xfs_inode *ip);
408408
xfs_extlen_t xfs_get_cowextsz_hint(struct xfs_inode *ip);
409409

410-
int xfs_dir_ialloc(struct xfs_trans **, struct xfs_inode *, umode_t,
411-
xfs_nlink_t, dev_t, prid_t,
412-
struct xfs_inode **);
410+
int xfs_dir_ialloc(struct xfs_trans **tpp, struct xfs_inode *dp, umode_t mode,
411+
xfs_nlink_t nlink, dev_t dev, prid_t prid,
412+
struct xfs_inode **ipp);
413413

414414
static inline int
415415
xfs_itruncate_extents(

fs/xfs/xfs_qm.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -737,15 +737,15 @@ xfs_qm_destroy_quotainfo(
737737
*/
738738
STATIC int
739739
xfs_qm_qino_alloc(
740-
xfs_mount_t *mp,
741-
xfs_inode_t **ip,
742-
uint flags)
740+
struct xfs_mount *mp,
741+
struct xfs_inode **ipp,
742+
unsigned int flags)
743743
{
744-
xfs_trans_t *tp;
745-
int error;
746-
bool need_alloc = true;
744+
struct xfs_trans *tp;
745+
int error;
746+
bool need_alloc = true;
747747

748-
*ip = NULL;
748+
*ipp = NULL;
749749
/*
750750
* With superblock that doesn't have separate pquotino, we
751751
* share an inode between gquota and pquota. If the on-disk
@@ -771,7 +771,7 @@ xfs_qm_qino_alloc(
771771
return -EFSCORRUPTED;
772772
}
773773
if (ino != NULLFSINO) {
774-
error = xfs_iget(mp, NULL, ino, 0, 0, ip);
774+
error = xfs_iget(mp, NULL, ino, 0, 0, ipp);
775775
if (error)
776776
return error;
777777
mp->m_sb.sb_gquotino = NULLFSINO;
@@ -787,7 +787,7 @@ xfs_qm_qino_alloc(
787787
return error;
788788

789789
if (need_alloc) {
790-
error = xfs_dir_ialloc(&tp, NULL, S_IFREG, 1, 0, 0, ip);
790+
error = xfs_dir_ialloc(&tp, NULL, S_IFREG, 1, 0, 0, ipp);
791791
if (error) {
792792
xfs_trans_cancel(tp);
793793
return error;
@@ -812,11 +812,11 @@ xfs_qm_qino_alloc(
812812
mp->m_sb.sb_qflags = mp->m_qflags & XFS_ALL_QUOTA_ACCT;
813813
}
814814
if (flags & XFS_QMOPT_UQUOTA)
815-
mp->m_sb.sb_uquotino = (*ip)->i_ino;
815+
mp->m_sb.sb_uquotino = (*ipp)->i_ino;
816816
else if (flags & XFS_QMOPT_GQUOTA)
817-
mp->m_sb.sb_gquotino = (*ip)->i_ino;
817+
mp->m_sb.sb_gquotino = (*ipp)->i_ino;
818818
else
819-
mp->m_sb.sb_pquotino = (*ip)->i_ino;
819+
mp->m_sb.sb_pquotino = (*ipp)->i_ino;
820820
spin_unlock(&mp->m_sb_lock);
821821
xfs_log_sb(tp);
822822

@@ -826,7 +826,7 @@ xfs_qm_qino_alloc(
826826
xfs_alert(mp, "%s failed (error %d)!", __func__, error);
827827
}
828828
if (need_alloc)
829-
xfs_finish_inode_setup(*ip);
829+
xfs_finish_inode_setup(*ipp);
830830
return error;
831831
}
832832

0 commit comments

Comments
 (0)