提交 1d83fbbb 编写于 作者: D Darrick J. Wong 提交者: Zheng Zengkai

xfs: refactor common transaction/inode/quota allocation idiom

mainline-inclusion
from mainline-v5.11-rc4
commit 3a1af6c3
category: bugfix
bugzilla: https://gitee.com/openeuler/kernel/issues/I4KIAO
CVE: NA

Reference: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=3a1af6c317d0a55524f39079183be107be4c1f39

-------------------------------------------------

Create a new helper xfs_trans_alloc_inode that allocates a transaction,
locks and joins an inode to it, and then reserves the appropriate amount
of quota against that transction.  Then replace all the open-coded
idioms with a single call to this helper.
Signed-off-by: NDarrick J. Wong <djwong@kernel.org>
Reviewed-by: NChristoph Hellwig <hch@lst.de>
Reviewed-by: NBrian Foster <bfoster@redhat.com>
Signed-off-by: NLihong Kou <koulihong@huawei.com>
Reviewed-by: NZhang Yi <yi.zhang@huawei.com>
Signed-off-by: NZheng Zengkai <zhengzengkai@huawei.com>
上级 9c990db4
...@@ -453,19 +453,11 @@ xfs_attr_set( ...@@ -453,19 +453,11 @@ xfs_attr_set(
* Root fork attributes can use reserved data blocks for this * Root fork attributes can use reserved data blocks for this
* operation if necessary * operation if necessary
*/ */
error = xfs_trans_alloc(mp, &tres, total, 0, error = xfs_trans_alloc_inode(dp, &tres, total, rsvd, &args->trans);
rsvd ? XFS_TRANS_RESERVE : 0, &args->trans);
if (error) if (error)
return error; return error;
xfs_ilock(dp, XFS_ILOCK_EXCL);
xfs_trans_ijoin(args->trans, dp, 0);
if (args->value) { if (args->value) {
error = xfs_trans_reserve_quota_nblks(args->trans, dp,
args->total, 0, rsvd);
if (error)
goto out_trans_cancel;
error = xfs_has_attr(args); error = xfs_has_attr(args);
if (error == -EEXIST && (args->attr_flags & XATTR_CREATE)) if (error == -EEXIST && (args->attr_flags & XATTR_CREATE))
goto out_trans_cancel; goto out_trans_cancel;
......
...@@ -1078,19 +1078,13 @@ xfs_bmap_add_attrfork( ...@@ -1078,19 +1078,13 @@ xfs_bmap_add_attrfork(
blks = XFS_ADDAFORK_SPACE_RES(mp); blks = XFS_ADDAFORK_SPACE_RES(mp);
error = xfs_trans_alloc(mp, &M_RES(mp)->tr_addafork, blks, 0, error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_addafork, blks,
rsvd ? XFS_TRANS_RESERVE : 0, &tp); rsvd, &tp);
if (error) if (error)
return error; return error;
xfs_ilock(ip, XFS_ILOCK_EXCL);
error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd);
if (error)
goto trans_cancel;
if (XFS_IFORK_Q(ip)) if (XFS_IFORK_Q(ip))
goto trans_cancel; goto trans_cancel;
xfs_trans_ijoin(tp, ip, 0);
xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
error = xfs_bmap_set_attrforkoff(ip, size, &version); error = xfs_bmap_set_attrforkoff(ip, size, &version);
if (error) if (error)
......
...@@ -868,18 +868,10 @@ xfs_unmap_extent( ...@@ -868,18 +868,10 @@ xfs_unmap_extent(
uint resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0); uint resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
int error; int error;
error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0, &tp); error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_write, resblks,
if (error) { false, &tp);
ASSERT(error == -ENOSPC || XFS_FORCED_SHUTDOWN(mp));
return error;
}
xfs_ilock(ip, XFS_ILOCK_EXCL);
error = xfs_trans_reserve_quota_nblks(tp, ip, resblks, 0, false);
if (error) if (error)
goto out_trans_cancel; return error;
xfs_trans_ijoin(tp, ip, 0);
error = xfs_bunmapi(tp, ip, startoffset_fsb, len_fsb, 0, 2, done); error = xfs_bunmapi(tp, ip, startoffset_fsb, len_fsb, 0, 2, done);
if (error) if (error)
......
...@@ -547,17 +547,11 @@ xfs_iomap_write_unwritten( ...@@ -547,17 +547,11 @@ xfs_iomap_write_unwritten(
* here as we might be asked to write out the same inode that we * here as we might be asked to write out the same inode that we
* complete here and might deadlock on the iolock. * complete here and might deadlock on the iolock.
*/ */
error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_write, resblks,
XFS_TRANS_RESERVE, &tp); true, &tp);
if (error) if (error)
return error; return error;
xfs_ilock(ip, XFS_ILOCK_EXCL);
xfs_trans_ijoin(tp, ip, 0);
error = xfs_trans_reserve_quota_nblks(tp, ip, resblks, 0, true);
if (error)
goto error_on_bmapi_transaction;
/* /*
* Modify the unwritten extent state of the buffer. * Modify the unwritten extent state of the buffer.
......
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
#include "xfs_trace.h" #include "xfs_trace.h"
#include "xfs_error.h" #include "xfs_error.h"
#include "xfs_defer.h" #include "xfs_defer.h"
#include "xfs_inode.h"
kmem_zone_t *xfs_trans_zone; kmem_zone_t *xfs_trans_zone;
...@@ -1020,3 +1021,50 @@ xfs_trans_roll( ...@@ -1020,3 +1021,50 @@ xfs_trans_roll(
tres.tr_logflags = XFS_TRANS_PERM_LOG_RES; tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
return xfs_trans_reserve(*tpp, &tres, 0, 0); return xfs_trans_reserve(*tpp, &tres, 0, 0);
} }
/*
* Allocate an transaction, lock and join the inode to it, and reserve quota.
*
* The caller must ensure that the on-disk dquots attached to this inode have
* already been allocated and initialized. The caller is responsible for
* releasing ILOCK_EXCL if a new transaction is returned.
*/
int
xfs_trans_alloc_inode(
struct xfs_inode *ip,
struct xfs_trans_res *resv,
unsigned int dblocks,
bool force,
struct xfs_trans **tpp)
{
struct xfs_trans *tp;
struct xfs_mount *mp = ip->i_mount;
int error;
error = xfs_trans_alloc(mp, resv, dblocks, 0,
force ? XFS_TRANS_RESERVE : 0, &tp);
if (error)
return error;
xfs_ilock(ip, XFS_ILOCK_EXCL);
xfs_trans_ijoin(tp, ip, 0);
error = xfs_qm_dqattach_locked(ip, false);
if (error) {
/* Caller should have allocated the dquots! */
ASSERT(error != -ENOENT);
goto out_cancel;
}
error = xfs_trans_reserve_quota_nblks(tp, ip, dblocks, 0, force);
if (error)
goto out_cancel;
*tpp = tp;
return 0;
out_cancel:
xfs_trans_cancel(tp);
xfs_iunlock(ip, XFS_ILOCK_EXCL);
return error;
}
...@@ -253,4 +253,7 @@ xfs_trans_item_relog( ...@@ -253,4 +253,7 @@ xfs_trans_item_relog(
return lip->li_ops->iop_relog(lip, tp); return lip->li_ops->iop_relog(lip, tp);
} }
int xfs_trans_alloc_inode(struct xfs_inode *ip, struct xfs_trans_res *resv,
unsigned int dblocks, bool force, struct xfs_trans **tpp);
#endif /* __XFS_TRANS_H__ */ #endif /* __XFS_TRANS_H__ */
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册