Skip to content

Commit 17fa6a5

Browse files
committed
Merge tag 'vfs-6.12-rc6.iomap' of gitolite.kernel.org:pub/scm/linux/kernel/git/vfs/vfs
Pull iomap fixes from Christian Brauner: "Fixes for iomap to prevent data corruption bugs in the fallocate unshare range implementation of fsdax and a small cleanup to turn iomap_want_unshare_iter() into an inline function" * tag 'vfs-6.12-rc6.iomap' of gitolite.kernel.org:pub/scm/linux/kernel/git/vfs/vfs: iomap: turn iomap_want_unshare_iter into an inline function 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
2 parents d56239a + 6db3885 commit 17fa6a5

File tree

4 files changed

+49
-34
lines changed

4 files changed

+49
-34
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: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,22 +1277,7 @@ static loff_t iomap_unshare_iter(struct iomap_iter *iter)
12771277
loff_t length = iomap_length(iter);
12781278
loff_t written = 0;
12791279

1280-
/* Don't bother with blocks that are not shared to start with. */
1281-
if (!(iomap->flags & IOMAP_F_SHARED))
1282-
return length;
1283-
1284-
/*
1285-
* Don't bother with delalloc reservations, holes or unwritten extents.
1286-
*
1287-
* Note that we use srcmap directly instead of iomap_iter_srcmap as
1288-
* unsharing requires providing a separate source map, and the presence
1289-
* of one is a good indicator that unsharing is needed, unlike
1290-
* IOMAP_F_SHARED which can be set for any data that goes into the COW
1291-
* fork for XFS.
1292-
*/
1293-
if (iter->srcmap.type == IOMAP_HOLE ||
1294-
iter->srcmap.type == IOMAP_DELALLOC ||
1295-
iter->srcmap.type == IOMAP_UNWRITTEN)
1280+
if (!iomap_want_unshare_iter(iter))
12961281
return length;
12971282

12981283
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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,25 @@ static inline loff_t iomap_last_written_block(struct inode *inode, loff_t pos,
270270
return round_up(pos + written, i_blocksize(inode));
271271
}
272272

273+
/*
274+
* Check if the range needs to be unshared for a FALLOC_FL_UNSHARE_RANGE
275+
* operation.
276+
*
277+
* Don't bother with blocks that are not shared to start with; or mappings that
278+
* cannot be shared, such as inline data, delalloc reservations, holes or
279+
* unwritten extents.
280+
*
281+
* Note that we use srcmap directly instead of iomap_iter_srcmap as unsharing
282+
* requires providing a separate source map, and the presence of one is a good
283+
* indicator that unsharing is needed, unlike IOMAP_F_SHARED which can be set
284+
* for any data that goes into the COW fork for XFS.
285+
*/
286+
static inline bool iomap_want_unshare_iter(const struct iomap_iter *iter)
287+
{
288+
return (iter->iomap.flags & IOMAP_F_SHARED) &&
289+
iter->srcmap.type == IOMAP_MAPPED;
290+
}
291+
273292
ssize_t iomap_file_buffered_write(struct kiocb *iocb, struct iov_iter *from,
274293
const struct iomap_ops *ops, void *private);
275294
int iomap_read_folio(struct folio *folio, const struct iomap_ops *ops);

0 commit comments

Comments
 (0)