Skip to content

Commit 11ab319

Browse files
johnpgarryDarrick J. Wong
authored andcommitted
xfs: add large atomic writes checks in xfs_direct_write_iomap_begin()
For when large atomic writes (> 1x FS block) are supported, there will be various occasions when HW offload may not be possible. Such instances include: - unaligned extent mapping wrt write length - extent mappings which do not cover the full write, e.g. the write spans sparse or mixed-mapping extents - the write length is greater than HW offload can support - no hardware support at all In those cases, we need to fallback to the CoW-based atomic write mode. For this, report special code -ENOPROTOOPT to inform the caller that HW offload-based method is not possible. In addition to the occasions mentioned, if the write covers an unallocated range, we again judge that we need to rely on the CoW-based method when we would need to allocate anything more than 1x block. This is because if we allocate less blocks that is required for the write, then again HW offload-based method would not be possible. So we are taking a pessimistic approach to writes covering unallocated space. Reviewed-by: Darrick J. Wong <[email protected]> [djwong: various cleanups] Signed-off-by: Darrick J. Wong <[email protected]> Reviewed-by: Christoph Hellwig <[email protected]> Signed-off-by: John Garry <[email protected]>
1 parent bd1d2c2 commit 11ab319

File tree

1 file changed

+60
-2
lines changed

1 file changed

+60
-2
lines changed

fs/xfs/xfs_iomap.c

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,38 @@ imap_spans_range(
798798
return true;
799799
}
800800

801+
static bool
802+
xfs_bmap_hw_atomic_write_possible(
803+
struct xfs_inode *ip,
804+
struct xfs_bmbt_irec *imap,
805+
xfs_fileoff_t offset_fsb,
806+
xfs_fileoff_t end_fsb)
807+
{
808+
struct xfs_mount *mp = ip->i_mount;
809+
xfs_fsize_t len = XFS_FSB_TO_B(mp, end_fsb - offset_fsb);
810+
811+
/*
812+
* atomic writes are required to be naturally aligned for disk blocks,
813+
* which ensures that we adhere to block layer rules that we won't
814+
* straddle any boundary or violate write alignment requirement.
815+
*/
816+
if (!IS_ALIGNED(imap->br_startblock, imap->br_blockcount))
817+
return false;
818+
819+
/*
820+
* Spanning multiple extents would mean that multiple BIOs would be
821+
* issued, and so would lose atomicity required for REQ_ATOMIC-based
822+
* atomics.
823+
*/
824+
if (!imap_spans_range(imap, offset_fsb, end_fsb))
825+
return false;
826+
827+
/*
828+
* The ->iomap_begin caller should ensure this, but check anyway.
829+
*/
830+
return len <= xfs_inode_buftarg(ip)->bt_bdev_awu_max;
831+
}
832+
801833
static int
802834
xfs_direct_write_iomap_begin(
803835
struct inode *inode,
@@ -812,9 +844,11 @@ xfs_direct_write_iomap_begin(
812844
struct xfs_bmbt_irec imap, cmap;
813845
xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
814846
xfs_fileoff_t end_fsb = xfs_iomap_end_fsb(mp, offset, length);
847+
xfs_fileoff_t orig_end_fsb = end_fsb;
815848
int nimaps = 1, error = 0;
816849
bool shared = false;
817850
u16 iomap_flags = 0;
851+
bool needs_alloc;
818852
unsigned int lockmode;
819853
u64 seq;
820854

@@ -875,13 +909,37 @@ xfs_direct_write_iomap_begin(
875909
(flags & IOMAP_DIRECT) || IS_DAX(inode));
876910
if (error)
877911
goto out_unlock;
878-
if (shared)
912+
if (shared) {
913+
if ((flags & IOMAP_ATOMIC) &&
914+
!xfs_bmap_hw_atomic_write_possible(ip, &cmap,
915+
offset_fsb, end_fsb)) {
916+
error = -ENOPROTOOPT;
917+
goto out_unlock;
918+
}
879919
goto out_found_cow;
920+
}
880921
end_fsb = imap.br_startoff + imap.br_blockcount;
881922
length = XFS_FSB_TO_B(mp, end_fsb) - offset;
882923
}
883924

884-
if (imap_needs_alloc(inode, flags, &imap, nimaps))
925+
needs_alloc = imap_needs_alloc(inode, flags, &imap, nimaps);
926+
927+
if (flags & IOMAP_ATOMIC) {
928+
error = -ENOPROTOOPT;
929+
/*
930+
* If we allocate less than what is required for the write
931+
* then we may end up with multiple extents, which means that
932+
* REQ_ATOMIC-based cannot be used, so avoid this possibility.
933+
*/
934+
if (needs_alloc && orig_end_fsb - offset_fsb > 1)
935+
goto out_unlock;
936+
937+
if (!xfs_bmap_hw_atomic_write_possible(ip, &imap, offset_fsb,
938+
orig_end_fsb))
939+
goto out_unlock;
940+
}
941+
942+
if (needs_alloc)
885943
goto allocate_blocks;
886944

887945
/*

0 commit comments

Comments
 (0)