Skip to contentSkip to conversation

only do a single COW fork lookup in writeback

PATCH In review Christoph Hellwig 3 participants 7 messages Sep 24, 2016 – Sep 27, 2016 XFS filesystem

lore
Share & tools
lore
#0 lore raw

I've got a bug report with a slightly older version of the reflink code, in which I get a bogus NULL xfs_bmbt_rec_host pointer back from xfs_iext_bno_to_ext in xfs_reflink_find_cow_mapping. I've not reproduced that bug myself yet, but what's clear from the report is that it's not just inefficient but also potentially dangerous to do the blind dereference in xfs_reflink_find_cow_mapping after we dropped the ilock from the previous xfs_reflink_find_cow_mapping call.

So just combine that two into one function, and then rewrite the COW writeback code to only do a single call in the second step. I think that also cleans up the writeback code quite a bit and makes it much easier to follow as well.

#1 lore raw

PATCH 1/2 xfs: kill xfs_reflink_is_cow_pending

Instead enhance xfs_reflink_find_cow_mapping to properly deal with the fact that we might not have a COW mapping at the offset, and just return false in this case.

This avoids code duplication, makes xfs_reflink_find_cow_mapping more robust, and last but not least halves the number of extent map lookups needed in COW writeback operations.

---
 fs/xfs/xfs_aops.c    | 28 ++++++++++++++++------------
 fs/xfs/xfs_reflink.c | 42 +++++++++++-------------------------------
 fs/xfs/xfs_reflink.h |  3 +--
 3 files changed, 28 insertions(+), 45 deletions(-)
fs/xfs/xfs_aops.c +16 -12
index d8ce18b..1933803 100644
@@ -379,10 +379,13 @@ xfs_map_blocks(
 	end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
 	offset_fsb = XFS_B_TO_FSBT(mp, offset);
 
-	if (type == XFS_IO_COW)
-		error = xfs_reflink_find_cow_mapping(ip, offset, imap,
-						     &need_alloc);
-	else {
+	if (type == XFS_IO_COW) {
+		if (!xfs_reflink_find_cow_mapping(ip, offset, imap,
+						     &need_alloc)) {
+			WARN_ON_ONCE(1);
+			return -EIO;
+		}
+	} else {
 		error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb,
 				       imap, &nimaps, bmapi_flags);
 		/*
@@ -712,13 +715,14 @@ xfs_is_cow_io(
 	struct xfs_inode	*ip,
 	xfs_off_t		offset)
 {
-	bool			is_cow;
+	struct xfs_bmbt_irec	imap;
+	bool			is_cow, need_alloc;
 
 	if (!xfs_sb_version_hasreflink(&ip->i_mount->m_sb))
 		return false;
 
 	xfs_ilock(ip, XFS_ILOCK_SHARED);
-	is_cow = xfs_reflink_is_cow_pending(ip, offset);
+	is_cow = xfs_reflink_find_cow_mapping(ip, offset, &imap, &need_alloc);
 	xfs_iunlock(ip, XFS_ILOCK_SHARED);
 
 	return is_cow;
@@ -1316,12 +1320,12 @@ __xfs_get_blocks(
 	end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + size);
 	offset_fsb = XFS_B_TO_FSBT(mp, offset);
 
-	if (create && direct)
-		is_cow = xfs_reflink_is_cow_pending(ip, offset);
-	if (is_cow)
-		error = xfs_reflink_find_cow_mapping(ip, offset, &imap,
-						     &need_alloc);
-	else {
+	if (create && direct) {
+		is_cow = xfs_reflink_find_cow_mapping(ip, offset, &imap,
+					&need_alloc);
+	}
+
+	if (!is_cow) {
 		error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb,
 					&imap, &nimaps, XFS_BMAPI_ENTIRE);
 		/*
fs/xfs/xfs_reflink.c +11 -31

75 lines not on this page · raw message

fs/xfs/xfs_reflink.h +1 -2
index 398e726..6519f19 100644
@@ -30,8 +30,7 @@ extern int xfs_reflink_reserve_cow_range(struct xfs_inode *ip,
 		xfs_fileoff_t offset_fsb, xfs_fileoff_t end_fsb);
 extern int xfs_reflink_allocate_cow_range(struct xfs_inode *ip, xfs_off_t pos,
 		xfs_off_t len);
-extern bool xfs_reflink_is_cow_pending(struct xfs_inode *ip, xfs_off_t offset);
-extern int xfs_reflink_find_cow_mapping(struct xfs_inode *ip, xfs_off_t offset,
+extern bool xfs_reflink_find_cow_mapping(struct xfs_inode *ip, xfs_off_t offset,
 		struct xfs_bmbt_irec *imap, bool *need_alloc);
 extern int xfs_reflink_trim_irec_to_next_cow(struct xfs_inode *ip,
 		xfs_fileoff_t offset_fsb, struct xfs_bmbt_irec *imap);
Signature · 1 line
2.1.4
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] xfs: kill xfs_reflink_is_cow_pending

Quoted 7 lines · Christoph Hellwig

Instead enhance xfs_reflink_find_cow_mapping to properly deal with the fact that we might not have a COW mapping at the offset, and just return false in this case.

This avoids code duplication, makes xfs_reflink_find_cow_mapping more robust, and last but not least halves the number of extent map lookups needed in COW writeback operations.

Hooray!

Quoted 15 lines
---
 fs/xfs/xfs_aops.c    | 28 ++++++++++++++++------------
 fs/xfs/xfs_reflink.c | 42 +++++++++++-------------------------------
 fs/xfs/xfs_reflink.h |  3 +--
 3 files changed, 28 insertions(+), 45 deletions(-)
fs/xfs/xfs_aops.c +0 -0
index d8ce18b..1933803 100644
@@ -379,10 +379,13 @@ xfs_map_blocks(
 	end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
 	offset_fsb = XFS_B_TO_FSBT(mp, offset);
 

fs/xfs/xfs_aops.c xfs_map_blocks( · line 387

Quoted 10 lines
fs/xfs/xfs_aops.c +6 -4
-	if (type == XFS_IO_COW)
-		error = xfs_reflink_find_cow_mapping(ip, offset, imap,
-						     &need_alloc);
-	else {
+	if (type == XFS_IO_COW) {
+		if (!xfs_reflink_find_cow_mapping(ip, offset, imap,
+						     &need_alloc)) {
+			WARN_ON_ONCE(1);
+			return -EIO;
+		}

I squinted at this for a second but then realized it goes away in the next patch anyway.

Quoted 114 lines
fs/xfs/xfs_aops.c +10 -8
+	} else {
 		error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb,
 				       imap, &nimaps, bmapi_flags);
 		/*
@@ -712,13 +715,14 @@ xfs_is_cow_io(
 	struct xfs_inode	*ip,
 	xfs_off_t		offset)
 {
-	bool			is_cow;
+	struct xfs_bmbt_irec	imap;
+	bool			is_cow, need_alloc;
 
 	if (!xfs_sb_version_hasreflink(&ip->i_mount->m_sb))
 		return false;
 
 	xfs_ilock(ip, XFS_ILOCK_SHARED);
-	is_cow = xfs_reflink_is_cow_pending(ip, offset);
+	is_cow = xfs_reflink_find_cow_mapping(ip, offset, &imap, &need_alloc);
 	xfs_iunlock(ip, XFS_ILOCK_SHARED);
 
 	return is_cow;
@@ -1316,12 +1320,12 @@ __xfs_get_blocks(
 	end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + size);
 	offset_fsb = XFS_B_TO_FSBT(mp, offset);
 
-	if (create && direct)
-		is_cow = xfs_reflink_is_cow_pending(ip, offset);
-	if (is_cow)
-		error = xfs_reflink_find_cow_mapping(ip, offset, &imap,
-						     &need_alloc);
-	else {
+	if (create && direct) {
+		is_cow = xfs_reflink_find_cow_mapping(ip, offset, &imap,
+					&need_alloc);
+	}
+
+	if (!is_cow) {
 		error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb,
 					&imap, &nimaps, XFS_BMAPI_ENTIRE);
 		/*
fs/xfs/xfs_reflink.c +10 -30
index 6f87b1e..a1ba7f5 100644
@@ -429,26 +429,31 @@ advloop:
 }
 
 /*
- * Determine if there's a CoW reservation at a byte offset of an inode.
+ * Find the CoW reservation (and whether or not it needs block allocation)
+ * for a given byte offset of a file.
  */
 bool
-xfs_reflink_is_cow_pending(
+xfs_reflink_find_cow_mapping(
 	struct xfs_inode		*ip,
-	xfs_off_t			offset)
+	xfs_off_t			offset,
+	struct xfs_bmbt_irec		*imap,
+	bool				*need_alloc)
 {
+	struct xfs_bmbt_irec		irec;
 	struct xfs_ifork		*ifp;
 	struct xfs_bmbt_rec_host	*gotp;
-	struct xfs_bmbt_irec		irec;
 	xfs_fileoff_t			bno;
 	xfs_extnum_t			idx;
 
+	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL | XFS_ILOCK_SHARED));
+
 	if (!xfs_is_reflink_inode(ip))
 		return false;
 
+	/* Find the extent in the CoW fork. */
 	ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
 	bno = XFS_B_TO_FSBT(ip->i_mount, offset);
 	gotp = xfs_iext_bno_to_ext(ifp, bno, &idx);
-
 	if (!gotp)
 		return false;
 
@@ -456,31 +461,6 @@ xfs_reflink_is_cow_pending(
 	if (bno >= irec.br_startoff + irec.br_blockcount ||
 	    bno < irec.br_startoff)
 		return false;
-	return true;
-}
-
-/*
- * Find the CoW reservation (and whether or not it needs block allocation)
- * for a given byte offset of a file.
- */
-int
-xfs_reflink_find_cow_mapping(
-	struct xfs_inode		*ip,
-	xfs_off_t			offset,
-	struct xfs_bmbt_irec		*imap,
-	bool				*need_alloc)
-{
-	struct xfs_bmbt_irec		irec;
-	struct xfs_ifork		*ifp;
-	struct xfs_bmbt_rec_host	*gotp;
-	xfs_fileoff_t			bno;
-	xfs_extnum_t			idx;
-
-	/* Find the extent in the CoW fork. */
-	ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
-	bno = XFS_B_TO_FSBT(ip->i_mount, offset);
-	gotp = xfs_iext_bno_to_ext(ifp, bno, &idx);
-	xfs_bmbt_get_all(gotp, &irec);
 
 	trace_xfs_reflink_find_cow_mapping(ip, offset, 1, XFS_IO_OVERWRITE,
 			&irec);

fs/xfs/xfs_reflink.c xfs_reflink_find_cow_mapping( · line 472

Quoted 5 lines
fs/xfs/xfs_reflink.c +1 -1
@@ -489,7 +469,7 @@ xfs_reflink_find_cow_mapping(
 	*imap = irec;
 	*need_alloc = !!(isnullstartblock(irec.br_startblock));
 
-	return 0;
+	return true;

Otherwise looks resonable enough, so

--D

Quoted 20 lines
fs/xfs/xfs_reflink.c +0 -0
 }
 
 /*
fs/xfs/xfs_reflink.h +1 -2
index 398e726..6519f19 100644
@@ -30,8 +30,7 @@ extern int xfs_reflink_reserve_cow_range(struct xfs_inode *ip,
 		xfs_fileoff_t offset_fsb, xfs_fileoff_t end_fsb);
 extern int xfs_reflink_allocate_cow_range(struct xfs_inode *ip, xfs_off_t pos,
 		xfs_off_t len);
-extern bool xfs_reflink_is_cow_pending(struct xfs_inode *ip, xfs_off_t offset);
-extern int xfs_reflink_find_cow_mapping(struct xfs_inode *ip, xfs_off_t offset,
+extern bool xfs_reflink_find_cow_mapping(struct xfs_inode *ip, xfs_off_t offset,
 		struct xfs_bmbt_irec *imap, bool *need_alloc);
 extern int xfs_reflink_trim_irec_to_next_cow(struct xfs_inode *ip,
 		xfs_fileoff_t offset_fsb, struct xfs_bmbt_irec *imap);
Signature · 1 line
2.1.4
#3 lore raw

PATCH 2/2 xfs: rewrite the COW writeback mapping code

In reply to Christoph Hellwig

Add a new xfs_map_cow helper that does a single consistent lookup in the COW fork extent map, and remove the existing COW handling code from xfs_map_blocks.

---
 fs/xfs/xfs_aops.c | 119 +++++++++++++++++++++++++++++++-----------------------
 1 file changed, 69 insertions(+), 50 deletions(-)
fs/xfs/xfs_aops.c +69 -50
index 1933803..2a3f4c1 100644
@@ -357,15 +357,11 @@ xfs_map_blocks(
 	int			error = 0;
 	int			bmapi_flags = XFS_BMAPI_ENTIRE;
 	int			nimaps = 1;
-	int			whichfork;
-	bool			need_alloc;
 
 	if (XFS_FORCED_SHUTDOWN(mp))
 		return -EIO;
 
-	whichfork = (type == XFS_IO_COW ? XFS_COW_FORK : XFS_DATA_FORK);
-	need_alloc = (type == XFS_IO_DELALLOC);
-
+	ASSERT(type != XFS_IO_COW);
 	if (type == XFS_IO_UNWRITTEN)
 		bmapi_flags |= XFS_BMAPI_IGSTATE;
 
@@ -378,36 +374,26 @@ xfs_map_blocks(
 		count = mp->m_super->s_maxbytes - offset;
 	end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
 	offset_fsb = XFS_B_TO_FSBT(mp, offset);
-
-	if (type == XFS_IO_COW) {
-		if (!xfs_reflink_find_cow_mapping(ip, offset, imap,
-						     &need_alloc)) {
-			WARN_ON_ONCE(1);
-			return -EIO;
-		}
-	} else {
-		error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb,
-				       imap, &nimaps, bmapi_flags);
-		/*
-		 * Truncate an overwrite extent if there's a pending CoW
-		 * reservation before the end of this extent.  This forces us
-		 * to come back to writepage to take care of the CoW.
-		 */
-		if (nimaps && type == XFS_IO_OVERWRITE)
-			xfs_reflink_trim_irec_to_next_cow(ip, offset_fsb, imap);
-	}
+	error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb,
+				imap, &nimaps, bmapi_flags);
+	/*
+	 * Truncate an overwrite extent if there's a pending CoW
+	 * reservation before the end of this extent.  This forces us
+	 * to come back to writepage to take care of the CoW.
+	 */
+	if (nimaps && type == XFS_IO_OVERWRITE)
+		xfs_reflink_trim_irec_to_next_cow(ip, offset_fsb, imap);
 	xfs_iunlock(ip, XFS_ILOCK_SHARED);
 
 	if (error)
 		return error;
 
-	if (need_alloc &&
+	if (type == XFS_IO_DELALLOC &&
 	    (!nimaps || isnullstartblock(imap->br_startblock))) {
-		error = xfs_iomap_write_allocate(ip, whichfork, offset,
+		error = xfs_iomap_write_allocate(ip, XFS_DATA_FORK, offset,
 				imap);
 		if (!error)
-			trace_xfs_map_blocks_alloc(ip, offset, count, type,
-					imap);
+			trace_xfs_map_blocks_alloc(ip, offset, count, type, imap);
 		return error;
 	}
 
@@ -707,27 +693,6 @@ xfs_check_page_type(
 	return false;
 }
 
-/*
- * Figure out if CoW is pending at this offset.
- */
-static bool
-xfs_is_cow_io(
-	struct xfs_inode	*ip,
-	xfs_off_t		offset)
-{
-	struct xfs_bmbt_irec	imap;
-	bool			is_cow, need_alloc;
-
-	if (!xfs_sb_version_hasreflink(&ip->i_mount->m_sb))
-		return false;
-
-	xfs_ilock(ip, XFS_ILOCK_SHARED);
-	is_cow = xfs_reflink_find_cow_mapping(ip, offset, &imap, &need_alloc);
-	xfs_iunlock(ip, XFS_ILOCK_SHARED);
-
-	return is_cow;
-}
-
 STATIC void
 xfs_vm_invalidatepage(
 	struct page		*page,
@@ -804,6 +769,56 @@ out_invalidate:
 	return;
 }
 
+static int
+xfs_map_cow(
+	struct xfs_writepage_ctx *wpc,
+	struct inode		*inode,
+	loff_t			offset,
+	unsigned int		*new_type)
+{
+	struct xfs_inode	*ip = XFS_I(inode);
+	struct xfs_bmbt_irec	imap;
+	bool			is_cow = false, need_alloc = false;
+	int			error;
+
+	/*
+	 * If we already have a valid COW mapping keep using it.
+	 */
+	if (wpc->io_type == XFS_IO_COW) {
+		wpc->imap_valid = xfs_imap_valid(inode, &wpc->imap, offset);
+		if (wpc->imap_valid) {
+			*new_type = XFS_IO_COW;
+			return 0;
+		}
+	}
+
+	/*
+	 * Else we need to check if there is a COW mapping at this offset.
+	 */
+	xfs_ilock(ip, XFS_ILOCK_SHARED);
+	is_cow = xfs_reflink_find_cow_mapping(ip, offset, &imap, &need_alloc);
+	xfs_iunlock(ip, XFS_ILOCK_SHARED);
+
+	if (!is_cow)
+		return 0;
+
+	/*
+	 * And if the COW mapping has a delayed extent here we need to
+	 * allocate real space for it now.
+	 */
+	if (need_alloc) {
+		error = xfs_iomap_write_allocate(ip, XFS_COW_FORK, offset,
+				&imap);
+		if (error)
+			return error;
+	}
+
+	wpc->io_type = *new_type = XFS_IO_COW;
+	wpc->imap_valid = true;
+	wpc->imap = imap;
+	return 0;
+}
+
 /*
  * We implement an immediate ioend submission policy here to avoid needing to
  * chain multiple ioends and hence nest mempool allocations which can violate
@@ -876,8 +891,12 @@ xfs_writepage_map(
 			continue;
 		}
 
-		if (xfs_is_cow_io(XFS_I(inode), offset))
-			new_type = XFS_IO_COW;
+		if (xfs_sb_version_hasreflink(&XFS_I(inode)->i_mount->m_sb)) {
+			error = xfs_map_cow(wpc, inode, offset, &new_type);
+			if (error)
+				goto out;
+		}
+
 		if (wpc->io_type != new_type) {
 			wpc->io_type = new_type;
 			wpc->imap_valid = false;
Signature · 1 line
2.1.4
Reviewed-by on v1 2/2
#4 lore raw

Re: [PATCH 2/2] xfs: rewrite the COW writeback mapping code

Quoted 165 lines · Christoph Hellwig

Add a new xfs_map_cow helper that does a single consistent lookup in the COW fork extent map, and remove the existing COW handling code from xfs_map_blocks.

---
 fs/xfs/xfs_aops.c | 119 +++++++++++++++++++++++++++++++-----------------------
 1 file changed, 69 insertions(+), 50 deletions(-)
fs/xfs/xfs_aops.c +63 -48
index 1933803..2a3f4c1 100644
@@ -357,15 +357,11 @@ xfs_map_blocks(
 	int			error = 0;
 	int			bmapi_flags = XFS_BMAPI_ENTIRE;
 	int			nimaps = 1;
-	int			whichfork;
-	bool			need_alloc;
 
 	if (XFS_FORCED_SHUTDOWN(mp))
 		return -EIO;
 
-	whichfork = (type == XFS_IO_COW ? XFS_COW_FORK : XFS_DATA_FORK);
-	need_alloc = (type == XFS_IO_DELALLOC);
-
+	ASSERT(type != XFS_IO_COW);
 	if (type == XFS_IO_UNWRITTEN)
 		bmapi_flags |= XFS_BMAPI_IGSTATE;
 
@@ -378,36 +374,26 @@ xfs_map_blocks(
 		count = mp->m_super->s_maxbytes - offset;
 	end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
 	offset_fsb = XFS_B_TO_FSBT(mp, offset);
-
-	if (type == XFS_IO_COW) {
-		if (!xfs_reflink_find_cow_mapping(ip, offset, imap,
-						     &need_alloc)) {
-			WARN_ON_ONCE(1);
-			return -EIO;
-		}
-	} else {
-		error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb,
-				       imap, &nimaps, bmapi_flags);
-		/*
-		 * Truncate an overwrite extent if there's a pending CoW
-		 * reservation before the end of this extent.  This forces us
-		 * to come back to writepage to take care of the CoW.
-		 */
-		if (nimaps && type == XFS_IO_OVERWRITE)
-			xfs_reflink_trim_irec_to_next_cow(ip, offset_fsb, imap);
-	}
+	error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb,
+				imap, &nimaps, bmapi_flags);
+	/*
+	 * Truncate an overwrite extent if there's a pending CoW
+	 * reservation before the end of this extent.  This forces us
+	 * to come back to writepage to take care of the CoW.
+	 */
+	if (nimaps && type == XFS_IO_OVERWRITE)
+		xfs_reflink_trim_irec_to_next_cow(ip, offset_fsb, imap);
 	xfs_iunlock(ip, XFS_ILOCK_SHARED);
 
 	if (error)
 		return error;
 
-	if (need_alloc &&
+	if (type == XFS_IO_DELALLOC &&
 	    (!nimaps || isnullstartblock(imap->br_startblock))) {
-		error = xfs_iomap_write_allocate(ip, whichfork, offset,
+		error = xfs_iomap_write_allocate(ip, XFS_DATA_FORK, offset,
 				imap);
 		if (!error)
-			trace_xfs_map_blocks_alloc(ip, offset, count, type,
-					imap);
+			trace_xfs_map_blocks_alloc(ip, offset, count, type, imap);
 		return error;
 	}
 
@@ -707,27 +693,6 @@ xfs_check_page_type(
 	return false;
 }
 
-/*
- * Figure out if CoW is pending at this offset.
- */
-static bool
-xfs_is_cow_io(
-	struct xfs_inode	*ip,
-	xfs_off_t		offset)
-{
-	struct xfs_bmbt_irec	imap;
-	bool			is_cow, need_alloc;
-
-	if (!xfs_sb_version_hasreflink(&ip->i_mount->m_sb))
-		return false;
-
-	xfs_ilock(ip, XFS_ILOCK_SHARED);
-	is_cow = xfs_reflink_find_cow_mapping(ip, offset, &imap, &need_alloc);
-	xfs_iunlock(ip, XFS_ILOCK_SHARED);
-
-	return is_cow;
-}
-
 STATIC void
 xfs_vm_invalidatepage(
 	struct page		*page,
@@ -804,6 +769,56 @@ out_invalidate:
 	return;
 }
 
+static int
+xfs_map_cow(
+	struct xfs_writepage_ctx *wpc,
+	struct inode		*inode,
+	loff_t			offset,
+	unsigned int		*new_type)
+{
+	struct xfs_inode	*ip = XFS_I(inode);
+	struct xfs_bmbt_irec	imap;
+	bool			is_cow = false, need_alloc = false;
+	int			error;
+
+	/*
+	 * If we already have a valid COW mapping keep using it.
+	 */
+	if (wpc->io_type == XFS_IO_COW) {
+		wpc->imap_valid = xfs_imap_valid(inode, &wpc->imap, offset);
+		if (wpc->imap_valid) {
+			*new_type = XFS_IO_COW;
+			return 0;
+		}
+	}
+
+	/*
+	 * Else we need to check if there is a COW mapping at this offset.
+	 */
+	xfs_ilock(ip, XFS_ILOCK_SHARED);
+	is_cow = xfs_reflink_find_cow_mapping(ip, offset, &imap, &need_alloc);
+	xfs_iunlock(ip, XFS_ILOCK_SHARED);
+
+	if (!is_cow)
+		return 0;
+
+	/*
+	 * And if the COW mapping has a delayed extent here we need to
+	 * allocate real space for it now.
+	 */
+	if (need_alloc) {
+		error = xfs_iomap_write_allocate(ip, XFS_COW_FORK, offset,
+				&imap);
+		if (error)
+			return error;
+	}
+
+	wpc->io_type = *new_type = XFS_IO_COW;
+	wpc->imap_valid = true;
+	wpc->imap = imap;
+	return 0;
+}
+
 /*
  * We implement an immediate ioend submission policy here to avoid needing to
  * chain multiple ioends and hence nest mempool allocations which can violate

fs/xfs/xfs_aops.c xfs_writepage_map( · line 894

Quoted 6 lines
fs/xfs/xfs_aops.c +1 -2
@@ -876,8 +891,12 @@ xfs_writepage_map(
 			continue;
 		}
 
-		if (xfs_is_cow_io(XFS_I(inode), offset))
-			new_type = XFS_IO_COW;
+		if (xfs_sb_version_hasreflink(&XFS_I(inode)->i_mount->m_sb)) {

This could be:

if (xfs_is_reflink_inode(XFS_I(inode))) {

since we don't have to care about COW mappings unless the inode also has shared extents. The code you got this from seems to have this bug too; I'll just fix this when I push the patch onto my stack.

--D

Quoted 11 lines
fs/xfs/xfs_aops.c +5 -0
+			error = xfs_map_cow(wpc, inode, offset, &new_type);
+			if (error)
+				goto out;
+		}
+
 		if (wpc->io_type != new_type) {
 			wpc->io_type = new_type;
 			wpc->imap_valid = false;
Signature · 1 line
2.1.4
#5 lore raw

Re: [PATCH 2/2] xfs: rewrite the COW writeback mapping code

Quoted 5 lines

if (xfs_is_reflink_inode(XFS_I(inode))) {

since we don't have to care about COW mappings unless the inode also has shared extents. The code you got this from seems to have this bug too; I'll just fix this when I push the patch onto my stack.

Indeed. I felt the check to be a bit odd, but for some reason didn't follow up on it.

#6 lore raw
In reply to Christoph Hellwig
Quoted 8 lines · Christoph Hellwig

I've got a bug report with a slightly older version of the reflink code, in which I get a bogus NULL xfs_bmbt_rec_host pointer back from xfs_iext_bno_to_ext in xfs_reflink_find_cow_mapping. I've not reproduced that bug myself yet, but what's clear from the report is that it's not just inefficient but also potentially dangerous to do the blind dereference in xfs_reflink_find_cow_mapping after we dropped the ilock from the previous xfs_reflink_find_cow_mapping call.

FYI, based on further analsys I suspect that a xfs_reflink_end_cow called from xfs_end_io cause the extent index to be invalid during the xfs_reflink_find_cow_mapping mapping, as that can easily shift the extent indices around and race with writeback elsewhere on the same file.