[PATCH v2] f2fs: fix to tag FIEMAP_EXTENT_DELALLOC in fiemap() for delay allocated extent

From: Chao Yu
Date: Sun Apr 09 2023 - 22:07:45 EST


xfstest generic/614 fails to run due to below reason:

generic/614 1s ... [not run] test requires delayed allocation buffered writes

The root cause is f2fs tags wrong fiemap flag for delay allocated
extent.

Quoted from fiemap ioctl manual from [1]:
FIEMAP_EXTENT_UNKNOWN
The location of this extent is currently unknown. This may
indicate the data is stored on an inaccessible volume or that
no storage has been allocated for the file yet.

FIEMAP_EXTENT_DELALLOC
This will also set FIEMAP_EXTENT_UNKNOWN.

Delayed allocation - while there is data for this extent, its
physical location has not been allocated yet.

FIEMAP_EXTENT_UNWRITTEN
Unwritten extent - the extent is allocated but its data has not
been initialized. This indicates the extent’s data will be all
zero if read through the filesystem but the contents are undefined
if read directly from the device.

[1] https://www.kernel.org/doc/html/latest/filesystems/fiemap.html

FIEMAP_EXTENT_UNWRITTEN means block address is preallocated, but w/o
been written any data, which status f2fs is not supported now, for all
NEW_ADDR block addresses, it means delay allocated blocks, so let's
tag FIEMAP_EXTENT_DELALLOC instead.

Testcase:
xfs_io -f -c 'pwrite 0 64k' /mnt/f2fs/file;
filefrag -v /mnt/f2fs/file

Output:
- Before
Filesystem type is: f2f52010
Fize of /mnt/f2fs/file is 65536 (16 blocks of 4096 bytes)
ext: logical_offset: physical_offset: length: expected: flags:
0: 0.. 15: 0.. 15: 16: last,unwritten,merged,eof
/mnt/f2fs/file: 1 extent found

- After:
Filesystem type is: f2f52010
File size of /mnt/f2fs/file is 65536 (16 blocks of 4096 bytes)
ext: logical_offset: physical_offset: length: expected: flags:
0: 0.. 15: 0.. 0: 0: last,unknown_loc,delalloc,eof
/mnt/f2fs/file: 1 extent found

Fixes: 7f63eb77af7b ("f2fs: report unwritten area in f2fs_fiemap")
Signed-off-by: Chao Yu <chao@xxxxxxxxxx>
---
v2:
- fix type and update description of fiemap manual in commit message.
fs/f2fs/data.c | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c
index 0a5832ec4046..becc7bdb7403 100644
--- a/fs/f2fs/data.c
+++ b/fs/f2fs/data.c
@@ -1999,7 +1999,10 @@ int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
}

if (size) {
- flags |= FIEMAP_EXTENT_MERGED;
+ if (flags & FIEMAP_EXTENT_DELALLOC)
+ phys = 0;
+ else
+ flags |= FIEMAP_EXTENT_MERGED;
if (IS_ENCRYPTED(inode))
flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;

@@ -2039,7 +2042,7 @@ int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
size += blks_to_bytes(inode, 1);
}
} else if (map.m_flags & F2FS_MAP_DELALLOC) {
- flags = FIEMAP_EXTENT_UNWRITTEN;
+ flags = FIEMAP_EXTENT_DELALLOC;
}

start_blk += bytes_to_blks(inode, size);
--
2.25.1