Skip to content

Commit dad1b6c

Browse files
committed
Merge patch series "fsdax/xfs: unshare range fixes for 6.12"
Darrick J. Wong <[email protected]> says: This patchset fixes multiple data corruption bugs in the fallocate unshare range implementation for fsdax. * patches from https://lore.kernel.org/r/172796813251.1131942.12184885574609980777.stgit@frogsfrogsfrogs: fsdax: dax_unshare_iter needs to copy entire blocks fsdax: remove zeroing code from dax_unshare_iter iomap: share iomap_unshare_iter predicate code with fsdax xfs: don't allocate COW extents when unsharing a hole Link: https://lore.kernel.org/r/172796813251.1131942.12184885574609980777.stgit@frogsfrogsfrogs Signed-off-by: Christian Brauner <[email protected]>
2 parents 8cf0b93 + 5079380 commit dad1b6c

File tree

4 files changed

+46
-32
lines changed

4 files changed

+46
-32
lines changed

fs/dax.c

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,35 +1262,46 @@ static s64 dax_unshare_iter(struct iomap_iter *iter)
12621262
{
12631263
struct iomap *iomap = &iter->iomap;
12641264
const struct iomap *srcmap = iomap_iter_srcmap(iter);
1265-
loff_t pos = iter->pos;
1266-
loff_t length = iomap_length(iter);
1265+
loff_t copy_pos = iter->pos;
1266+
u64 copy_len = iomap_length(iter);
1267+
u32 mod;
12671268
int id = 0;
12681269
s64 ret = 0;
12691270
void *daddr = NULL, *saddr = NULL;
12701271

1271-
/* don't bother with blocks that are not shared to start with */
1272-
if (!(iomap->flags & IOMAP_F_SHARED))
1273-
return length;
1272+
if (!iomap_want_unshare_iter(iter))
1273+
return iomap_length(iter);
1274+
1275+
/*
1276+
* Extend the file range to be aligned to fsblock/pagesize, because
1277+
* we need to copy entire blocks, not just the byte range specified.
1278+
* Invalidate the mapping because we're about to CoW.
1279+
*/
1280+
mod = offset_in_page(copy_pos);
1281+
if (mod) {
1282+
copy_len += mod;
1283+
copy_pos -= mod;
1284+
}
1285+
1286+
mod = offset_in_page(copy_pos + copy_len);
1287+
if (mod)
1288+
copy_len += PAGE_SIZE - mod;
1289+
1290+
invalidate_inode_pages2_range(iter->inode->i_mapping,
1291+
copy_pos >> PAGE_SHIFT,
1292+
(copy_pos + copy_len - 1) >> PAGE_SHIFT);
12741293

12751294
id = dax_read_lock();
1276-
ret = dax_iomap_direct_access(iomap, pos, length, &daddr, NULL);
1295+
ret = dax_iomap_direct_access(iomap, copy_pos, copy_len, &daddr, NULL);
12771296
if (ret < 0)
12781297
goto out_unlock;
12791298

1280-
/* zero the distance if srcmap is HOLE or UNWRITTEN */
1281-
if (srcmap->flags & IOMAP_F_SHARED || srcmap->type == IOMAP_UNWRITTEN) {
1282-
memset(daddr, 0, length);
1283-
dax_flush(iomap->dax_dev, daddr, length);
1284-
ret = length;
1285-
goto out_unlock;
1286-
}
1287-
1288-
ret = dax_iomap_direct_access(srcmap, pos, length, &saddr, NULL);
1299+
ret = dax_iomap_direct_access(srcmap, copy_pos, copy_len, &saddr, NULL);
12891300
if (ret < 0)
12901301
goto out_unlock;
12911302

1292-
if (copy_mc_to_kernel(daddr, saddr, length) == 0)
1293-
ret = length;
1303+
if (copy_mc_to_kernel(daddr, saddr, copy_len) == 0)
1304+
ret = iomap_length(iter);
12941305
else
12951306
ret = -EIO;
12961307

fs/iomap/buffered-io.c

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,29 +1309,31 @@ void iomap_file_buffered_write_punch_delalloc(struct inode *inode,
13091309
}
13101310
EXPORT_SYMBOL_GPL(iomap_file_buffered_write_punch_delalloc);
13111311

1312-
static loff_t iomap_unshare_iter(struct iomap_iter *iter)
1312+
bool iomap_want_unshare_iter(const struct iomap_iter *iter)
13131313
{
1314-
struct iomap *iomap = &iter->iomap;
1315-
loff_t pos = iter->pos;
1316-
loff_t length = iomap_length(iter);
1317-
loff_t written = 0;
1318-
1319-
/* Don't bother with blocks that are not shared to start with. */
1320-
if (!(iomap->flags & IOMAP_F_SHARED))
1321-
return length;
1322-
13231314
/*
1324-
* Don't bother with delalloc reservations, holes or unwritten extents.
1315+
* Don't bother with blocks that are not shared to start with; or
1316+
* mappings that cannot be shared, such as inline data, delalloc
1317+
* reservations, holes or unwritten extents.
13251318
*
13261319
* Note that we use srcmap directly instead of iomap_iter_srcmap as
13271320
* unsharing requires providing a separate source map, and the presence
13281321
* of one is a good indicator that unsharing is needed, unlike
13291322
* IOMAP_F_SHARED which can be set for any data that goes into the COW
13301323
* fork for XFS.
13311324
*/
1332-
if (iter->srcmap.type == IOMAP_HOLE ||
1333-
iter->srcmap.type == IOMAP_DELALLOC ||
1334-
iter->srcmap.type == IOMAP_UNWRITTEN)
1325+
return (iter->iomap.flags & IOMAP_F_SHARED) &&
1326+
iter->srcmap.type == IOMAP_MAPPED;
1327+
}
1328+
1329+
static loff_t iomap_unshare_iter(struct iomap_iter *iter)
1330+
{
1331+
struct iomap *iomap = &iter->iomap;
1332+
loff_t pos = iter->pos;
1333+
loff_t length = iomap_length(iter);
1334+
loff_t written = 0;
1335+
1336+
if (!iomap_want_unshare_iter(iter))
13351337
return length;
13361338

13371339
do {

fs/xfs/xfs_iomap.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,7 @@ imap_needs_cow(
707707
return false;
708708

709709
/* when zeroing we don't have to COW holes or unwritten extents */
710-
if (flags & IOMAP_ZERO) {
710+
if (flags & (IOMAP_UNSHARE | IOMAP_ZERO)) {
711711
if (!nimaps ||
712712
imap->br_startblock == HOLESTARTBLOCK ||
713713
imap->br_state == XFS_EXT_UNWRITTEN)

include/linux/iomap.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ void iomap_invalidate_folio(struct folio *folio, size_t offset, size_t len);
267267
bool iomap_dirty_folio(struct address_space *mapping, struct folio *folio);
268268
int iomap_file_unshare(struct inode *inode, loff_t pos, loff_t len,
269269
const struct iomap_ops *ops);
270+
bool iomap_want_unshare_iter(const struct iomap_iter *iter);
270271
int iomap_zero_range(struct inode *inode, loff_t pos, loff_t len,
271272
bool *did_zero, const struct iomap_ops *ops);
272273
int iomap_truncate_page(struct inode *inode, loff_t pos, bool *did_zero,

0 commit comments

Comments
 (0)