Skip to content

Commit 3a1af6c

Browse files
author
Darrick J. Wong
committed
xfs: refactor common transaction/inode/quota allocation idiom
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: Darrick J. Wong <[email protected]> Reviewed-by: Christoph Hellwig <[email protected]> Reviewed-by: Brian Foster <[email protected]>
1 parent 02b7ee4 commit 3a1af6c

File tree

6 files changed

+59
-38
lines changed

6 files changed

+59
-38
lines changed

fs/xfs/libxfs/xfs_attr.c

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -458,14 +458,10 @@ xfs_attr_set(
458458
* Root fork attributes can use reserved data blocks for this
459459
* operation if necessary
460460
*/
461-
error = xfs_trans_alloc(mp, &tres, total, 0,
462-
rsvd ? XFS_TRANS_RESERVE : 0, &args->trans);
461+
error = xfs_trans_alloc_inode(dp, &tres, total, rsvd, &args->trans);
463462
if (error)
464463
return error;
465464

466-
xfs_ilock(dp, XFS_ILOCK_EXCL);
467-
xfs_trans_ijoin(args->trans, dp, 0);
468-
469465
if (args->value || xfs_inode_hasattr(dp)) {
470466
error = xfs_iext_count_may_overflow(dp, XFS_ATTR_FORK,
471467
XFS_IEXT_ATTR_MANIP_CNT(rmt_blks));
@@ -474,11 +470,6 @@ xfs_attr_set(
474470
}
475471

476472
if (args->value) {
477-
error = xfs_trans_reserve_quota_nblks(args->trans, dp,
478-
args->total, 0, rsvd);
479-
if (error)
480-
goto out_trans_cancel;
481-
482473
error = xfs_has_attr(args);
483474
if (error == -EEXIST && (args->attr_flags & XATTR_CREATE))
484475
goto out_trans_cancel;

fs/xfs/libxfs/xfs_bmap.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,19 +1079,13 @@ xfs_bmap_add_attrfork(
10791079

10801080
blks = XFS_ADDAFORK_SPACE_RES(mp);
10811081

1082-
error = xfs_trans_alloc(mp, &M_RES(mp)->tr_addafork, blks, 0,
1083-
rsvd ? XFS_TRANS_RESERVE : 0, &tp);
1082+
error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_addafork, blks,
1083+
rsvd, &tp);
10841084
if (error)
10851085
return error;
1086-
1087-
xfs_ilock(ip, XFS_ILOCK_EXCL);
1088-
error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd);
1089-
if (error)
1090-
goto trans_cancel;
10911086
if (XFS_IFORK_Q(ip))
10921087
goto trans_cancel;
10931088

1094-
xfs_trans_ijoin(tp, ip, 0);
10951089
xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
10961090
error = xfs_bmap_set_attrforkoff(ip, size, &version);
10971091
if (error)

fs/xfs/xfs_bmap_util.c

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -873,18 +873,10 @@ xfs_unmap_extent(
873873
uint resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
874874
int error;
875875

876-
error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0, &tp);
877-
if (error) {
878-
ASSERT(error == -ENOSPC || XFS_FORCED_SHUTDOWN(mp));
879-
return error;
880-
}
881-
882-
xfs_ilock(ip, XFS_ILOCK_EXCL);
883-
error = xfs_trans_reserve_quota_nblks(tp, ip, resblks, 0, false);
876+
error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_write, resblks,
877+
false, &tp);
884878
if (error)
885-
goto out_trans_cancel;
886-
887-
xfs_trans_ijoin(tp, ip, 0);
879+
return error;
888880

889881
error = xfs_iext_count_may_overflow(ip, XFS_DATA_FORK,
890882
XFS_IEXT_PUNCH_HOLE_CNT);

fs/xfs/xfs_iomap.c

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -552,18 +552,11 @@ xfs_iomap_write_unwritten(
552552
* here as we might be asked to write out the same inode that we
553553
* complete here and might deadlock on the iolock.
554554
*/
555-
error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0,
556-
XFS_TRANS_RESERVE, &tp);
555+
error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_write, resblks,
556+
true, &tp);
557557
if (error)
558558
return error;
559559

560-
xfs_ilock(ip, XFS_ILOCK_EXCL);
561-
xfs_trans_ijoin(tp, ip, 0);
562-
563-
error = xfs_trans_reserve_quota_nblks(tp, ip, resblks, 0, true);
564-
if (error)
565-
goto error_on_bmapi_transaction;
566-
567560
error = xfs_iext_count_may_overflow(ip, XFS_DATA_FORK,
568561
XFS_IEXT_WRITE_UNWRITTEN_CNT);
569562
if (error)

fs/xfs/xfs_trans.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "xfs_trace.h"
2121
#include "xfs_error.h"
2222
#include "xfs_defer.h"
23+
#include "xfs_inode.h"
2324

2425
kmem_zone_t *xfs_trans_zone;
2526

@@ -1024,3 +1025,50 @@ xfs_trans_roll(
10241025
tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
10251026
return xfs_trans_reserve(*tpp, &tres, 0, 0);
10261027
}
1028+
1029+
/*
1030+
* Allocate an transaction, lock and join the inode to it, and reserve quota.
1031+
*
1032+
* The caller must ensure that the on-disk dquots attached to this inode have
1033+
* already been allocated and initialized. The caller is responsible for
1034+
* releasing ILOCK_EXCL if a new transaction is returned.
1035+
*/
1036+
int
1037+
xfs_trans_alloc_inode(
1038+
struct xfs_inode *ip,
1039+
struct xfs_trans_res *resv,
1040+
unsigned int dblocks,
1041+
bool force,
1042+
struct xfs_trans **tpp)
1043+
{
1044+
struct xfs_trans *tp;
1045+
struct xfs_mount *mp = ip->i_mount;
1046+
int error;
1047+
1048+
error = xfs_trans_alloc(mp, resv, dblocks, 0,
1049+
force ? XFS_TRANS_RESERVE : 0, &tp);
1050+
if (error)
1051+
return error;
1052+
1053+
xfs_ilock(ip, XFS_ILOCK_EXCL);
1054+
xfs_trans_ijoin(tp, ip, 0);
1055+
1056+
error = xfs_qm_dqattach_locked(ip, false);
1057+
if (error) {
1058+
/* Caller should have allocated the dquots! */
1059+
ASSERT(error != -ENOENT);
1060+
goto out_cancel;
1061+
}
1062+
1063+
error = xfs_trans_reserve_quota_nblks(tp, ip, dblocks, 0, force);
1064+
if (error)
1065+
goto out_cancel;
1066+
1067+
*tpp = tp;
1068+
return 0;
1069+
1070+
out_cancel:
1071+
xfs_trans_cancel(tp);
1072+
xfs_iunlock(ip, XFS_ILOCK_EXCL);
1073+
return error;
1074+
}

fs/xfs/xfs_trans.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,4 +268,7 @@ xfs_trans_item_relog(
268268
return lip->li_ops->iop_relog(lip, tp);
269269
}
270270

271+
int xfs_trans_alloc_inode(struct xfs_inode *ip, struct xfs_trans_res *resv,
272+
unsigned int dblocks, bool force, struct xfs_trans **tpp);
273+
271274
#endif /* __XFS_TRANS_H__ */

0 commit comments

Comments
 (0)