Skip to content

Commit 9baeac3

Browse files
johnpgarryDarrick J. Wong
authored andcommitted
xfs: add xfs_file_dio_write_atomic()
Add xfs_file_dio_write_atomic() for dedicated handling of atomic writes. Now HW offload will not be required to support atomic writes and is an optional feature. CoW-based atomic writes will be supported with out-of-places write and atomic extent remapping. Either mode of operation may be used for an atomic write, depending on the circumstances. The preferred method is HW offload as it will be faster. If HW offload is not available then we always use the CoW-based method. If HW offload is available but not possible to use, then again we use the CoW-based method. If available, HW offload would not be possible for the write length exceeding the HW offload limit, the write spanning multiple extents, unaligned disk blocks, etc. Apart from the write exceeding the HW offload limit, other conditions for HW offload usage can only be detected in the iomap handling for the write. As such, we use a fallback method to issue the write if we detect in the ->iomap_begin() handler that HW offload is not possible. Special code -ENOPROTOOPT is returned from ->iomap_begin() to inform that HW offload is not possible. In other words, atomic writes are supported on any filesystem that can perform out of place write remapping atomically (i.e. reflink) up to some fairly large size. If the conditions are right (a single correctly aligned overwrite mapping) then the filesystem will use any available hardware support to avoid the filesystem metadata updates. Reviewed-by: Darrick J. Wong <[email protected]> Signed-off-by: Darrick J. Wong <[email protected]> Reviewed-by: Christoph Hellwig <[email protected]> Signed-off-by: John Garry <[email protected]>
1 parent b1e0917 commit 9baeac3

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

fs/xfs/xfs_file.c

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,72 @@ xfs_file_dio_write_zoned(
728728
return ret;
729729
}
730730

731+
/*
732+
* Handle block atomic writes
733+
*
734+
* Two methods of atomic writes are supported:
735+
* - REQ_ATOMIC-based, which would typically use some form of HW offload in the
736+
* disk
737+
* - COW-based, which uses a COW fork as a staging extent for data updates
738+
* before atomically updating extent mappings for the range being written
739+
*
740+
*/
741+
static noinline ssize_t
742+
xfs_file_dio_write_atomic(
743+
struct xfs_inode *ip,
744+
struct kiocb *iocb,
745+
struct iov_iter *from)
746+
{
747+
unsigned int iolock = XFS_IOLOCK_SHARED;
748+
ssize_t ret, ocount = iov_iter_count(from);
749+
const struct iomap_ops *dops;
750+
751+
/*
752+
* HW offload should be faster, so try that first if it is already
753+
* known that the write length is not too large.
754+
*/
755+
if (ocount > xfs_inode_buftarg(ip)->bt_bdev_awu_max)
756+
dops = &xfs_atomic_write_cow_iomap_ops;
757+
else
758+
dops = &xfs_direct_write_iomap_ops;
759+
760+
retry:
761+
ret = xfs_ilock_iocb_for_write(iocb, &iolock);
762+
if (ret)
763+
return ret;
764+
765+
ret = xfs_file_write_checks(iocb, from, &iolock, NULL);
766+
if (ret)
767+
goto out_unlock;
768+
769+
/* Demote similar to xfs_file_dio_write_aligned() */
770+
if (iolock == XFS_IOLOCK_EXCL) {
771+
xfs_ilock_demote(ip, XFS_IOLOCK_EXCL);
772+
iolock = XFS_IOLOCK_SHARED;
773+
}
774+
775+
trace_xfs_file_direct_write(iocb, from);
776+
ret = iomap_dio_rw(iocb, from, dops, &xfs_dio_write_ops,
777+
0, NULL, 0);
778+
779+
/*
780+
* The retry mechanism is based on the ->iomap_begin method returning
781+
* -ENOPROTOOPT, which would be when the REQ_ATOMIC-based write is not
782+
* possible. The REQ_ATOMIC-based method typically not be possible if
783+
* the write spans multiple extents or the disk blocks are misaligned.
784+
*/
785+
if (ret == -ENOPROTOOPT && dops == &xfs_direct_write_iomap_ops) {
786+
xfs_iunlock(ip, iolock);
787+
dops = &xfs_atomic_write_cow_iomap_ops;
788+
goto retry;
789+
}
790+
791+
out_unlock:
792+
if (iolock)
793+
xfs_iunlock(ip, iolock);
794+
return ret;
795+
}
796+
731797
/*
732798
* Handle block unaligned direct I/O writes
733799
*
@@ -843,6 +909,8 @@ xfs_file_dio_write(
843909
return xfs_file_dio_write_unaligned(ip, iocb, from);
844910
if (xfs_is_zoned_inode(ip))
845911
return xfs_file_dio_write_zoned(ip, iocb, from);
912+
if (iocb->ki_flags & IOCB_ATOMIC)
913+
return xfs_file_dio_write_atomic(ip, iocb, from);
846914
return xfs_file_dio_write_aligned(ip, iocb, from,
847915
&xfs_direct_write_iomap_ops, &xfs_dio_write_ops, NULL);
848916
}

0 commit comments

Comments
 (0)