migrates ocfs2 DIO from buffer_head to iomap
Heming Zhao
reviewerv3 cover migrates ocfs2 DIO from buffer_head to iomap
This patch set migrates ocfs2 Direct I/O (DIO) from buffer_head mode to iomap mode. And the code base is commit 58717b2a1365d.
Changelog:
v3:
patch 1/4
revise the commit log
for ocfs2_map_blocks():
- removed S_ISLNK() handling, as S_ISLNK inodes are already handled by the existing code path:
ocfs2_read_folio => block_read_full_folio() => get_block (ocfs2_get_block)
- replace "return -EIO" with "return err" at the end of this func
patch 2/4
revise the commit log based on hch's comments.
in ocfs2_iomap_begin(), replace the "/* todo */" comment under the IOMAP_WRITE case with "return -EOPNOTSUPP". revise comments in ocfs2_iomap_dio_end_io()
remove the unused "bail" label in ocfs2_dio_end_io_r_pr()
patch 3/4
revise the commit log based on hch's comments.
remove the unused var max_blocks in ocfs2_iomap_dio_end_io_write().
followed Joseph's comment, directly set map.lblk in ocfs2_iomap_dio_end_io_write().
in ocfs2_dio_wr_map_blocks():
- replace BUG_ON(p_blkno == 0) with "ret = -EIO, mlog_errno() and goto unlock".
(the legacy code was copied from ocfs2_dio_wr_get_block())
- adjust arithmetic for map->len by substracting the offset and using min_t() to cap the length within limits.
- adjust error handling style for the return value of ocfs2_write_end_nolock().
introduce a new function, ocfs2_dio_zero_eof().
patch 4/4
Set f_mode with FMODE_CAN_ODIRECT only for non-inline files, per Joseph's comments.
v2:
- Merge Joseph's code and ideas from [1]:
- Add FMODE_CAN_ODIRECT to f_mode in ocfs2_file_open()
- Change iomap_ops and iomap_dio_ops API declarations from ocfs2.h to aops.h
- Switch from iomap_dio_rw to __iomap_dio_rw + iomap_dio_complete
- Change "struct iomap_dio_ops" from read/write style to PR/EX locking style. With this approach, we completely eliminate the need to define any iocb->ki_flags bits.
- Remove alignment code from ocfs2_should_use_dio(), because iomap internally already handles this.
- Add patch 4 to remove unused functions and the CONFIG_LEGACY_DIRECT_IO Kconfig option.
- Add "Co-developed-by: Joseph Qi <[email protected]>"
[1]:
https://gitee.com/anolis/storage-cloud-kernel/commit/ea3fe75e2060118ba093e90b1cd34fa382914b29
v1:
https://lore.kernel.org/ocfs2-devel/[email protected]/
v2:
https://lore.kernel.org/ocfs2-devel/[email protected]/
--------------
# test report
This patch set has been tested on 7.3.0-rc7 kernel with the following test cases.
## ltp
runltp -f dio -d /mnt/ocfs2/
The test results: passed
## xfstests
format cmd: mkfs.ocfs2 -M local --fs-feature-level=max-features /dev/vde
./check -g quick -T -b -s ocfs2 -e generic/081 \
-e generic/266 -e generic/272 -e generic/281 \
-e generic/338 -e generic/347 -e generic/361 \
-e generic/628 -e generic/629 -e generic/648 -e generic/650
The test results are the same before and after the patch set.
Failures: generic/003 generic/091 generic/094 generic/177 generic/211 generic/225 generic/263 generic/322 generic/329 generic/331 generic/370 generic/376 generic/424 generic/448 generic/467 generic/477 generic/481 generic/510 generic/513 generic/537 generic/552 generic/563 generic/578 generic/607 generic/620 generic/630 generic/755 generic/760 generic/777 generic/784
Failed 30 of 631 tests
(following is 7.2.0 kernel test result)
Failures: generic/003 generic/322 generic/329 generic/331 generic/370 generic/376 generic/420 generic/424 generic/448 generic/449 generic/467 generic/477 generic/510 generic/513 generic/537 generic/552 generic/563 generic/578 generic/607 generic/620 generic/630 generic/755 generic/777 generic/784
Failed 24 of 632 tests
## fio on single node
(this is v2 test result, v3 test result is similar)
fio --direct=1 --iodepth=32 --rw=write --ioengine=libaio --bs=128k --size=1g \
--numjobs=4 --runtime=100 --group_reporting --filename=/mnt/ocfs2/write.1g --name=tst
- before patch : write: IOPS=18.8k, BW=4708MiB/s (4937MB/s)(4096MiB/870msec)
- applied patch: write: IOPS=39.6k, BW=4953MiB/s (5193MB/s)(4096MiB/827msec)
- overwrite (don't remove write.1g, run above fio command) speed seems same.
fio --direct=1 --iodepth=32 --rw=read --ioengine=libaio --bs=128k --size=1g \
--numjobs=4 --runtime=100 --group_reporting --filename=/mnt/ocfs2/write.1g --name=tst
- before patch : read: IOPS=52.4k, BW=6554MiB/s (6872MB/s)(4096MiB/625msec)
- applied patch: read: IOPS=52.2k, BW=6522MiB/s (6839MB/s)(4096MiB/628msec)
fio --direct=1 --iodepth=32 --rw=randwrite --ioengine=libaio --bs=4k --size=1g \
--numjobs=4 --runtime=100 --group_reporting --filename=/mnt/ocfs2/write.1g --name=tst
- before patch
- creating write: IOPS=20.9k, BW=81.7MiB/s (85.6MB/s)(4096MiB/50159msec)
- overwriting write.1g: write: IOPS=281k, BW=1097MiB/s (1151MB/s)(4096MiB/3733msec)
- applied patch
- creating write: IOPS=21.0k, BW=82.0MiB/s (86.0MB/s)(4096MiB/49938msec)
- overwriting write.1g: write: IOPS=329k, BW=1286MiB/s (1348MB/s)(4096MiB/3185msec)
fio --direct=1 --iodepth=32 --rw=randread --ioengine=libaio --bs=4k --size=1g \
--numjobs=4 --runtime=100 --group_reporting --filename=/mnt/ocfs2/write.1g --name=tst
- before patch : read: IOPS=372k, BW=1452MiB/s (1522MB/s)(4096MiB/2821msec)
- applied patch: read: IOPS=369k, BW=1441MiB/s (1511MB/s)(4096MiB/2842msec)
## fio on two nodes
### write in different folder
node1 /root/fio/diff/ocfs2-w.sh:
Quoted 2 lines
fio --direct=1 --iodepth=32 --rw=write --ioengine=libaio --bs=128k --size=2g \
--numjobs=2 --runtime=100 --group_reporting --filename=/mnt/ocfs2/node1/write.2g --name=tst
node2 /root/fio/diff/ocfs2-w.sh:
Quoted 2 lines
fio --direct=1 --iodepth=32 --rw=write --ioengine=libaio --bs=128k --size=2g \
--numjobs=2 --runtime=100 --group_reporting --filename=/mnt/ocfs2/node2/write.2g --name=tst
node1 /root/fio/diff/ocfs2-randw.sh:
Quoted 2 lines
fio --direct=1 --iodepth=32 --rw=randwrite --ioengine=libaio --bs=4k --size=2g \
--numjobs=2 --runtime=100 --group_reporting --filename=/mnt/ocfs2/node1/write.2g --name=tst
node2 /root/fio/diff/ocfs2-randw.sh:
Quoted 2 lines
fio --direct=1 --iodepth=32 --rw=randwrite --ioengine=libaio --bs=4k --size=2g \
--numjobs=2 --runtime=100 --group_reporting --filename=/mnt/ocfs2/node2/write.2g --name=tst
run on both node,
pdsh -w ssh:tw2510-[1-2] /root/fio/diff/ocfs2-w.sh
- before patch : write: IOPS=19.3k, BW=2417MiB/s (2534MB/s)(4096MiB/1695msec)
- applied patch: write: IOPS=20.8k, BW=2599MiB/s (2725MB/s)(4096MiB/1576msec) pdsh -w ssh:tw2510-[1-2] /root/fio/diff/ocfs2-randw.sh
- before patch : write: IOPS=3742, BW=14.6MiB/s (15.3MB/s)(1462MiB/100007msec)
- applied patch: write: IOPS=3765, BW=14.7MiB/s (15.4MB/s)(1471MiB/100010msec)
### write in same file
node1 & node2: (/root/fio/share/ocfs2-w.sh)
fio --direct=1 --iodepth=32 --rw=write --ioengine=libaio --bs=128k --size=2g \
--numjobs=2 --runtime=100 --group_reporting --filename=/mnt/ocfs2/share/write.2g --name=tst
node1 & node2: (/root/fio/share/ocfs2-randw.sh)
fio --direct=1 --iodepth=32 --rw=randwrite --ioengine=libaio --bs=4k --size=2g \
--numjobs=2 --runtime=100 --group_reporting --filename=/mnt/ocfs2/share/write.2g --name=tst
run on both node
pdsh -w ssh:tw2510-[1-2] /root/fio/share/ocfs2-w.sh
- before patch : write: IOPS=2052, BW=257MiB/s (269MB/s)(4096MiB/15967msec)
- applied patch: write: IOPS=2558, BW=320MiB/s (335MB/s)(4096MiB/12808msec) pdsh -w ssh:tw2510-[1-2] /root/fio/share/ocfs2-randw.sh
- before patch : write: IOPS=3588, BW=14.0MiB/s (14.7MB/s)(1402MiB/100001msec)
- applied patch: write: IOPS=3590, BW=14.0MiB/s (14.7MB/s)(1403MiB/100001msec)
Heming Zhao (4):
ocfs2: Add new ocfs2_map_blocks() to introduce iomap feature
ocfs2: switch DIO read path from buffer_head to iomap
ocfs2: switch dio write path from buffer_head to iomap
ocfs2: remove legacy blockdev direct-IO path APIs
fs/ocfs2/Kconfig | 2 +- fs/ocfs2/aops.c | 689 ++++++++++++++++++++++++-------------- fs/ocfs2/aops.h | 38 +-- fs/ocfs2/buffer_head_io.c | 19 +- fs/ocfs2/file.c | 168 ++++++++-- fs/ocfs2/ocfs2.h | 45 ++- fs/ocfs2/ocfs2_fs.h | 3 + 7 files changed, 634 insertions(+), 330 deletions(-)
Signature · 1 line
2.55.0
Heming Zhao
reviewerRFC PATCH v3 1/4 ocfs2: Add new ocfs2_map_blocks() to introduce iomap feature
As part of migrating OCFS2 DIO read/write code paths towards the modern and performant iomap framework, here introduces ocfs2_map_blocks() to replace the legacy, high-overhead buffer_head based block mapping routines.
In addition, OCFS2_MAP_* flags are defined to gridge OCFS2 internal block mapping flags with the iomap API requirements.
Subsequent iomap integration patches will build on this core block mapping routine. The implementation draws inspiration from ext4_map_blocks().
--- fs/ocfs2/aops.c | 83 +++++++++++++++++++++++++++++++++++++++ fs/ocfs2/aops.h | 2 + fs/ocfs2/buffer_head_io.c | 12 ------ fs/ocfs2/ocfs2.h | 45 ++++++++++++++++++++- 4 files changed, 129 insertions(+), 13 deletions(-)
fs/ocfs2/aops.c +83 -0
@@ -126,6 +126,89 @@ static int ocfs2_lock_get_block(struct inode *inode, sector_t iblock,return ret; } +int ocfs2_map_blocks (struct inode *inode,struct ocfs2_map_block *map, +int flags) +{ +int err =0 ; +unsigned int ext_flags; +u64 max_blocks = map->len; +u64 p_blkno, count, past_eof; +struct ocfs2_super *osb =OCFS2_SB (inode->i_sb); +int create = flags &OCFS2_GET_BLOCKS_CREATE ; + +if (OCFS2_I (inode)->ip_flags &OCFS2_INODE_SYSTEM_FILE ) +mlog (ML_NOTICE ,"map_block on system inode 0x%p (%llu)\n" , + inode, inode->i_ino); + + err =ocfs2_extent_map_get_blocks (inode, map->lblk, &p_blkno, &count, + &ext_flags); +if (err) { +mlog (ML_ERROR ,"get_blocks() failed, inode: 0x%p, " +"block: %llu\n" , inode, map->lblk); + goto bail; + } + +if (max_blocks < count) + count = max_blocks; + + map->pblk = p_blkno; + map->len = count; + +/* +* ocfs2 never allocates in this function - the only time we +* need to use MAP_NEW is when we're extending i_size on a file +* system which doesn't support holes, in which case MAP_NEW +* allows __block_write_begin() to zero. +* +* If we see this on a sparse file system, then a truncate has +* raced us and removed the cluster. In this case, we clear +* the buffers dirty and uptodate bits and let the buffer code +* ignore it as a hole. +*/ +if (create && map->pblk ==0 &&ocfs2_sparse_alloc (osb)) { + map->flags &= ~(OCFS2_MAP_DIRTY |OCFS2_MAP_UPTODATE ); + goto bail; + } + +if (p_blkno) { +if (ext_flags &OCFS2_EXT_UNWRITTEN ) { + map->flags |=OCFS2_MAP_UNWRITTEN ; + }else if (!(ext_flags &OCFS2_EXT_UNWRITTEN )) { +/* Treat the unwritten extent as a hole for zeroing purposes. */ + map->flags |=OCFS2_MAP_MAPPED ; + }else { +/* TODO: handling other OCFS2_MAP_XX in the future */ + } + } + +if (!ocfs2_sparse_alloc (osb)) { +if (map->pblk ==0 ) { + err = -EIO ; +mlog (ML_ERROR , +"iblock = %llu p_blkno = %llu blkno=(%llu)\n" , + (unsigned long long )map->lblk, + (unsigned long long )map->pblk, + (unsigned long long )OCFS2_I (inode)->ip_blkno); +mlog (ML_ERROR ,"Size %llu, clusters %u\n" , + (unsigned long long )i_size_read (inode), +OCFS2_I (inode)->ip_clusters); +dump_stack (); + goto bail; + } + } + + past_eof =ocfs2_blocks_for_bytes (inode->i_sb,i_size_read (inode)); + +if (create && (map->lblk >= past_eof)) + map->flags |=OCFS2_MAP_NEW ; + +bail: +if (err <0 ) +return err; +else +return map->len; +} +int ocfs2_get_block (struct inode *inode,sector_t iblock,struct buffer_head *bh_result,int create) {
fs/ocfs2/aops.h +2 -0
@@ -42,6 +42,8 @@ int ocfs2_size_fits_inline_data(struct buffer_head *di_bh, u64 new_size);int ocfs2_get_block (struct inode *inode,sector_t iblock,struct buffer_head *bh_result,int create); +int ocfs2_map_blocks (struct inode *inode,struct ocfs2_map_block *map, +int flags);/* all ocfs2_dio_end_io()'s fault */ #define ocfs2_iocb_is_rw_locked (iocb) \ test_bit(0, (unsigned long *)&iocb->private)
fs/ocfs2/buffer_head_io.c +0 -12
@@ -23,18 +23,6 @@#include "buffer_head_io.h" #include "ocfs2_trace.h" -/* -* Bits on bh->b_state used by ocfs2. -* -* These MUST be after the JBD2 bits. Hence, we use BH_JBDPrivateStart. -*/ -enum ocfs2_state_bits { - BH_NeedsValidate = BH_JBDPrivateStart, -}; - -/* Expand the magic b_state functions */ -BUFFER_FNS (NeedsValidate, needs_validate); -int ocfs2_write_block (struct ocfs2_super *osb,struct buffer_head *bh,struct ocfs2_caching_info *ci) {
fs/ocfs2/ocfs2.h +44 -1
51 lines not on this page · raw message
Signature · 1 line
2.55.0
A message that is not in the archive (sent off-list or not yet received): [email protected]
Heming Zhao
reviewerRe: [RFC PATCH v3 1/4] ocfs2: Add new ocfs2_map_blocks() to introduce iomap feature
Quoted 82 lines · On 10/18/26 22:46, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] ocfs2:ocfs2_map_blocks()truncates 64-bitmap->lento signed int
- [Medium] ocfs2:ocfs2_map_blocks()leaves stalemap->flagsand omits UNWRITTEN mask
- [Low] ocfs2: tautological unwritten check and dead else branch inocfs2_map_blocks()--commit
bbd9a5b12f2907ae3d2ebf7b854a6e0542a61540
Author: Heming Zhao <[email protected]>ocfs2: Add new
ocfs2_map_blocks()to introduce iomap featureThis commit introduces
ocfs2_map_blocks(), structocfs2_map_block, and the OCFS2_MAP_* flags in preparation for converting OCFS2 direct I/O to the iomap framework.Quoted 52 lines
fs/ocfs2/aops.c +44
-0@@ -126,6 +126,89 @@ static int ocfs2_lock_get_block(struct inode *inode, sector_t iblock,return ret; } +int ocfs2_map_blocks (struct inode *inode,struct ocfs2_map_block *map, +int flags) +{ +int err =0 ; +unsigned int ext_flags; +u64 max_blocks = map->len; +u64 p_blkno, count, past_eof; +struct ocfs2_super *osb =OCFS2_SB (inode->i_sb); +int create = flags &OCFS2_GET_BLOCKS_CREATE ; + +if (OCFS2_I (inode)->ip_flags &OCFS2_INODE_SYSTEM_FILE ) +mlog (ML_NOTICE ,"map_block on system inode 0x%p (%llu)\n" , + inode, inode->i_ino); + + err =ocfs2_extent_map_get_blocks (inode, map->lblk, &p_blkno, &count, + &ext_flags); +if (err) { +mlog (ML_ERROR ,"get_blocks() failed, inode: 0x%p, " +"block: %llu\n" , inode, map->lblk); + goto bail; + } + +if (max_blocks < count) + count = max_blocks; + + map->pblk = p_blkno; + map->len = count; + +/* +* ocfs2 never allocates in this function - the only time we +* need to use MAP_NEW is when we're extending i_size on a file +* system which doesn't support holes, in which case MAP_NEW +* allows __block_write_begin() to zero. +* +* If we see this on a sparse file system, then a truncate has +* raced us and removed the cluster. In this case, we clear +* the buffers dirty and uptodate bits and let the buffer code +* ignore it as a hole. +*/ +if (create && map->pblk ==0 &&ocfs2_sparse_alloc (osb)) { + map->flags &= ~(OCFS2_MAP_DIRTY |OCFS2_MAP_UPTODATE ); + goto bail; + } +fs/ocfs2/aops.c static int ocfs2_lock_get_block(struct inode *inode, sector_t iblock, · line 182
Quoted 10 lines
fs/ocfs2/aops.c +10
-0+if (p_blkno) { +if (ext_flags &OCFS2_EXT_UNWRITTEN ) { + map->flags |=OCFS2_MAP_UNWRITTEN ; + }else if (!(ext_flags &OCFS2_EXT_UNWRITTEN )) { +/* Treat the unwritten extent as a hole for zeroing purposes. */ + map->flags |=OCFS2_MAP_MAPPED ; + }else { +/* TODO: handling other OCFS2_MAP_XX in the future */ + } + }[Severity: Medium]
Shouldocfs2_map_blocks()clearmap->flagson entry (similar to howext4_map_blocks()initializesmap->m_flags= 0)?
Quoted 9 lines
Right now,
ocfs2_map_blocks()only modifiesmap->flagsvia bitwise OR (or clearsOCFS2_MAP_DIRTYandOCFS2_MAP_UPTODATEon a sparse hole with create). If a caller does not zeromap->flagsbefore callingocfs2_map_blocks(), or reuses the same structocfs2_map_blockacross consecutive calls (for example, in the subsequent patch whereocfs2_iomap_begin()callsocfs2_map_blocks()on the overwrite fast path and then resets only map.len before callingocfs2_iomap_alloc()->ocfs2_dio_wr_map_blocks()->ocfs2_map_blocks()), stale flags likeOCFS2_MAP_MAPPEDorOCFS2_MAP_UNWRITTENwill remain set even if the next lookup hits a hole or a different extent type.
accept the above suggestion.
Quoted 7 lines
[Severity: Low]
In thep_blknoblock above, the else-if condition checks !(ext_flags &OCFS2_EXT_UNWRITTEN), which is the exact negation of the preceding if condition. Does this make the final else branch unreachable dead code?
I will remove the else branch in next version.
Quoted 5 lines
Also, the comment "Treat the unwritten extent as a hole for zeroing purposes" appears to be carried over from
ocfs2_get_block(), where unwritten extents were left unmapped, whereas here unwritten extents are mapped withOCFS2_MAP_UNWRITTENin the first branch and written extents getOCFS2_MAP_MAPPEDin the second branch.
disagree. The logic in ocfs2_map_blocks() is the same as in ocfs2_get_block():
unwritten extents are marked as mapped.
Quoted 49 lines
Quoted 19 lines
fs/ocfs2/aops.c +18
-0+ +if (!ocfs2_sparse_alloc (osb)) { +if (map->pblk ==0 ) { + err = -EIO ; +mlog (ML_ERROR , +"iblock = %llu p_blkno = %llu blkno=(%llu)\n" , + (unsigned long long )map->lblk, + (unsigned long long )map->pblk, + (unsigned long long )OCFS2_I (inode)->ip_blkno); +mlog (ML_ERROR ,"Size %llu, clusters %u\n" , + (unsigned long long )i_size_read (inode), +OCFS2_I (inode)->ip_clusters); +dump_stack (); + goto bail; + } + } + + past_eof =ocfs2_blocks_for_bytes (inode->i_sb,i_size_read (inode));fs/ocfs2/aops.c static int ocfs2_lock_get_block(struct inode *inode, sector_t iblock, · line 210
Quoted 10 lines
fs/ocfs2/aops.c +10
-0+ +if (create && (map->lblk >= past_eof)) + map->flags |=OCFS2_MAP_NEW ; + +bail: +if (err <0 ) +return err; +else +return map->len; +}[Severity: High]
Can returningmap->lenfromocfs2_map_blocks()overflow or truncate the 64-bit block count into a negative signed int?In struct
ocfs2_map_block, len is a u64, whileocfs2_map_blocks()returns a 32-bit signed int without clamping count ormax_blockstoINT_MAX(unlikeext4_map_blocks()).When a caller such as
ocfs2_iomap_begin()(introduced in the next patch of this series, which bounds block numbers toOCFS2_MAX_LOGICAL_BLOCK=0xfffffffeand sets map.len up to0xffffffff) passesmap->len>INT_MAXand queries a hole at or past the end of existing extents:
ocfs2_map_blocks()
ocfs2_extent_map_get_blocks()ocfs2_get_clusters() ocfs2_figure_hole_clusters() *num_clusters = UINT_MAX - v_cluster;
Quoted 5 lines
ocfs2_extent_map_get_blocks()convertsnum_clustersinto a 64-bit block count that can be >=0x80000000. Becauseocfs2_map_blocks()only bounds count by u64max_blocks=map->len, returningmap->lenas an int turns values in 0x80000000..0xffffffff into negative numbers and truncates values >= 2^32, causing callers checking ret < 0 to treat valid mappings as errors.
Agree. I will change ocfs2_map_blocks() to return the "err" (negative value on failure, 0 on success), and have callers use input parameter map->len to check the write length.
Quoted 19 lines
Quoted 5 lines
fs/ocfs2/ocfs2.h +0
-0[ ... ]
Quoted 11 lines
fs/ocfs2/ocfs2.h +11
-0+#define OCFS2_MAP_NEW BIT(BH_New) +#define OCFS2_MAP_MAPPED BIT(BH_Mapped) +#define OCFS2_MAP_UNWRITTEN BIT(BH_Unwritten) +/* useless? #define OCFS2_MAP_BOUNDARY BIT(BH_Boundary) */ +/* useless? #define OCFS2_MAP_DELAYED BIT(BH_Delay) */ +#define OCFS2_MAP_DIRTY BIT(BH_Dirty) +#define OCFS2_MAP_UPTODATE BIT(BH_Uptodate) +#define OCFS2_MAP_NEEDS_VALIDATE BIT(BH_NeedsValidate) +#define OCFS2_MAP_DEFER_COMPLETION BIT(BH_Defer_Completion) +#define OCFS2_MAP_FLAGS (OCFS2_MAP_NEW | OCFS2_MAP_MAPPED |\ + OCFS2_MAP_DIRTY | OCFS2_MAP_UPTODATE |\
Quoted 8 lines
fs/ocfs2/ocfs2.h struct ocfs2_super · line 543
Quoted 2 lines
fs/ocfs2/ocfs2.h +2
-0+OCFS2_MAP_NEEDS_VALIDATE |\ + OCFS2_MAP_DEFER_COMPLETION)[Severity: Medium]
ShouldOCFS2_MAP_FLAGSalso includeOCFS2_MAP_UNWRITTEN?OCFS2_MAP_UNWRITTENis defined just above and set byocfs2_map_blocks(), so omitting it fromOCFS2_MAP_FLAGSwill drop the unwritten state whenever flags are masked withOCFS2_MAP_FLAGS.
Agree. Btw, currently no function uses OCFS2_MAP_FLAGS.
- Heming
Quoted 17 lines
[ ... ]
Quoted 6 lines
fs/ocfs2/ocfs2.h +6
-0+struct ocfs2_map_block { +u64 pblk;/* physical block# */ +u64 lblk;/* logical block# */ +u64 len;/* number of block */ +unsigned int flags; +};[ ... ]
Quoted 4 lines
Signature · 2 lines
-- 2.51.2
Heming Zhao
reviewerRFC PATCH v3 2/4 ocfs2: switch DIO read path from buffer_head to iomap
In reply to Heming ZhaoMigrates the DIO read path from the legacy buffer_head infrastructure to the modern and more efficient iomap framework.
Under the current code, ocfs2 holds a cluster lock during DIO via ocfs2_iocb_set_rw_locked(), and releases the cluster lock in ocfs2_dio_end_io() via ocfs2_iocb_clear_rw_locked(). The lock level is stashed in iocb->private.
However, iomap DIO owns that field (it stores a bio there for polled I/O and unconditionally clears it before calling ->end_io). Any side-channel state packed into iocb->private is corrupted, which trips the end_io lock-state check.
Instead, encode the unlock level by passing distinct iomap_dio_ops methods (ocfs2_iomap_dio_ops_pr for PRMODE or ocfs2_iomap_dio_ops_ex for EXMODE). Additionally, the *_iter() functions use the split __iomap_dio_rw() + iomap_dio_complete() API so they can determine unambiguously, from the return value, whether the completion handler already dropped the lock (real I/O, sync or -EIOCBQUEUED) or whether the caller must drop it (no I/O issued, or buffered fallback).
The new function ocfs2_iomap_begin() performs a similar role to ocfs2_get_block(). While ocfs2_get_block() retrieves only one block per call, ocfs2_iomap_begin() can handle multiple blocks at once. Limit the map offset and length in ocfs2_iomap_begin() to OCFS2_MAX_LOGICAL_BLOCK to avoid overflow issues.
--- fs/ocfs2/Kconfig | 1 + fs/ocfs2/aops.c | 111 ++++++++++++++++++++++++++++++++++++++++++++ fs/ocfs2/file.c | 57 +++++++++++++++++++---- fs/ocfs2/ocfs2.h | 3 ++ fs/ocfs2/ocfs2_fs.h | 3 ++ 5 files changed, 165 insertions(+), 10 deletions(-)
fs/ocfs2/Kconfig +1 -0
@@ -7,6 +7,7 @@ config OCFS2_FS select CRC32 select QUOTA select QUOTA_TREE + select FS_IOMAP select FS_POSIX_ACL select LEGACY_DIRECT_IO help
fs/ocfs2/aops.c +111 -0
123 lines not on this page · raw message
fs/ocfs2/file.c +47 -10
98 lines not on this page · raw message
fs/ocfs2/ocfs2.h +3 -0
@@ -549,6 +549,9 @@ struct ocfs2_map_block {unsigned int flags; }; +extern const struct iomap_ops ocfs2_iomap_ops; +extern const struct iomap_dio_ops ocfs2_iomap_dio_ops_r_pr; +/* Flags used by ocfs2_map_blocks() */ #define OCFS2_GET_BLOCKS_CREATE (0x0001)
fs/ocfs2/ocfs2_fs.h +3 -0
@@ -314,6 +314,9 @@*/ #define OCFS2_CLUSTER_O2CB_GLOBAL_HEARTBEAT (0x01) +/* Max logical block we can support */ +#define OCFS2_MAX_LOGICAL_BLOCK (0xFFFFFFFE) +struct ocfs2_system_inode_info {char *si_name;int si_iflags;
Signature · 1 line
2.55.0
A message that is not in the archive (sent off-list or not yet received): [email protected]
Heming Zhao
reviewerRe: [RFC PATCH v3 2/4] ocfs2: switch DIO read path from buffer_head to iomap
Quoted 81 lines · On 10/18/26 23:03, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] ocfs2:ocfs2_iomap_begin()caps logical blocks at 32 bits and overflows int
- [Medium] ocfs2:ocfs2_file_read_iter()treats DIO EOF return as buffered fallback --commit
0ce3e0976541f87f07073b3eedf67a03235e8f09
Author: Heming Zhao <[email protected]>ocfs2: switch DIO read path from
buffer_headto iomapThis commit migrates the ocfs2 direct I/O read path from
buffer_headto the iomap framework, introducingocfs2_iomap_begin()and handling clusterrw_lockunlocking throughiomap_dio_opsand__iomap_dio_rw().Quoted 35 lines
fs/ocfs2/aops.c +20
-0@@ -4,6 +4,7 @@*/ #include <linux/fs.h> +#include <linux/iomap.h> #include <linux/slab.h> #include <linux/highmem.h> #include <linux/pagemap.h> @@ -2545,6 +2546,116 @@ static ssize_t ocfs2_direct_IO(struct kiocb *iocb, struct iov_iter *iter) ocfs2_dio_end_io, 0); } +static int ocfs2_iomap_begin (struct inode *inode,loff_t offset,loff_t length, +unsigned int flags,struct iomap *iomap,struct iomap *srcmap) +{ +int ret; +struct ocfs2_map_block map; +struct ocfs2_inode_info *oi =OCFS2_I (inode); +u8 blkbits = inode->i_blkbits; + +if ((offset >> blkbits) >OCFS2_MAX_LOGICAL_BLOCK ) +return -EINVAL ; + +/* +* Calculate the first and last logical blocks respectively. +*/ + map.lblk = offset >> blkbits; + map.len =min_t (loff_t, (offset + length -1 ) >> blkbits, +OCFS2_MAX_LOGICAL_BLOCK ) - map.lblk +1 ; + map.flags =0 ; +fs/ocfs2/aops.c static ssize_t ocfs2_direct_IO(struct kiocb *iocb, struct iov_iter *iter) · line 2577
Quoted 10 lines
fs/ocfs2/aops.c +10
-0+if (flags &IOMAP_WRITE ) { +return -EOPNOTSUPP ; + }else { +down_read (&oi->ip_alloc_sem); + ret =ocfs2_map_blocks (inode, &map,0 ); +up_read (&oi->ip_alloc_sem); + } + +if (ret <0 ) +return ret;[Severity: High]
Can this cap onOCFS2_MAX_LOGICAL_BLOCK(0xFFFFFFFE) reject valid large-file offsets and overflow the signed int return value ofocfs2_map_blocks()?In ocfs2, on-disk extent offsets (
e_cpos) and inode cluster counts (ip_clusters) are 32-bit cluster numbers, while block offsets are 64-bit:fs/ocfs2/super.c:ocfs2_max_file_offset() { unsigned int bytes = 1 << cbits; unsigned int trim = bytes; unsigned int bitshift = 32; /* * i_size and all block offsets in ocfs2 are always 64 bits * wide. i_clusters is 32 bits, in cluster-sized units. So on * 64 bit platforms, cluster size will be the limiting factor. */ ... return (((unsigned long long)bytes) << bitshift) - trim; }
Quoted 6 lines
Whenever the cluster size is larger than the block size (cbits > bbits), valid logical block numbers exceed
0xFFFFFFFE(for example, offsets at or above 16 TiB with 4 KiB blocks and 8 KiB to 1 MiB clusters). With this check,O_DIRECTreads viaocfs2_file_read_iter()->__iomap_dio_rw()->iomap_iter()->ocfs2_iomap_begin()fail with-EINVALat offsets that previously worked and still work for buffered reads.
the max file size is 16TB(0x1000,0000,0000).OCFS2_MAX_LOGICAL_BLOCK represents the block number, the max value is 0xFFFF,FFFF in theory.
I will change the value of OCFS2_MAX_LOGICAL_BLOCK from 0xFFFFFFFE to 0xFFFF,FFFF.
Quoted 2 lines
Additionally,
ocfs2_map_blocks()returns u64map->lenas a signed 32-bit int:
This issue was mentioned in [1/4] patch review comments. I will fix it in the next version.
Quoted 99 lines
fs/ocfs2/aops.c:ocfs2_map_blocks() { ... bail: if (err < 0) return err; else return map->len; }Because map.len can be up to
0xFFFFFFFF(andocfs2_figure_hole_clusters()returnsUINT_MAX-v_clusterfor holes at EOF), a mapped or hole range of0x80000000blocks or more will truncate to a negative int return value in ret and causeocfs2_iomap_begin()to fail with a bogus error code.[ ... ]
Quoted 17 lines
fs/ocfs2/aops.c +17
-0+static int ocfs2_iomap_dio_end_io (struct kiocb *iocb,ssize_t size,int error, +int level) +{ +struct inode *inode =file_inode (iocb->ki_filp); + +/* +* When iomap_dio_complete() calls us with size==0 and error==0, it +* means the mapping bounced back to buffered I/O. In that case leave +* the rw_lock ocfs2_file_{read,write}_iter() keeps it for the buffered +* retry and releases it in its own cleanup path. +*/ +if (size ==0 && error ==0 ) +return 0 ; + +ocfs2_rw_unlock (inode, level); +return error; +}[ ... ]
Quoted 4 lines
fs/ocfs2/file.c +0
-0[ ... ]
Quoted 22 lines
fs/ocfs2/file.c +17
-1@@ -2598,17 +2610,42 @@ static ssize_t ocfs2_file_read_iter(struct kiocb *iocb, }ocfs2_inode_unlock (inode, lock_level); - ret =generic_file_read_iter (iocb, to); +if (direct_io &&ocfs2_should_use_dio (iocb, to, inode)) { +struct iomap_dio *dio; + + dio =__iomap_dio_rw (iocb, to, &ocfs2_iomap_ops, + &ocfs2_iomap_dio_ops_r_pr,0 ,NULL ,0 ); +if (dio ==NULL ) { +/* No I/O issued; rw_lock still held. */ + ret =0 ; + }else if (IS_ERR (dio)) { + ret =PTR_ERR (dio); +if (ret == -EIOCBQUEUED ) + rw_level =-1 ; + }else { + ret =iomap_dio_complete (dio); +if (ret !=0 ) + rw_level =-1 ; + }fs/ocfs2/file.c static ssize_t ocfs2_file_read_iter(struct kiocb *iocb, · line 2639
Quoted 10 lines
fs/ocfs2/file.c +10
-0+/* +* A 0 result means the mapping bounced us back to buffered I/O +* (e.g. inline data); the rw_lock is still held. Clear +* IOCB_DIRECT so generic_file_read_iter() takes the buffered +* path rather than re-entering direct I/O. +*/ +if (ret ==0 ) { + iocb->ki_flags &= ~IOCB_DIRECT ; + ret =generic_file_read_iter (iocb, to); + }[Severity: Medium]
When anO_DIRECTread starts at or past EOF, won't this treat the 0 return as a buffered I/O fallback and callgeneric_file_read_iter()withIOCB_DIRECTcleared?In
__iomap_dio_rw(), a read at or pastdio->i_sizejumps toout_free_diowith ret == 0 and returns NULL:fs/iomap/direct-io.c:__iomap_dio_rw() { ... if (iov_iter_rw(iter) == READ) { if (iomi.pos >= dio->i_size) goto out_free_dio; ... }Here, dio == NULL sets ret = 0, and the if (ret == 0) block then clears
IOCB_DIRECTand falls back togeneric_file_read_iter()->filemap_read(). If a concurrent writer extendsi_sizeafter__iomap_dio_rw()sampleddio->i_size,filemap_read()will perform a buffered read on theO_DIRECTdescriptor, populating the page cache and bypassing DIO alignment checks.
Quoted 4 lines
Also, since
ocfs2_should_use_dio()already filters out inline data before calling__iomap_dio_rw()andocfs2_iomap_begin()never returns-ENOTBLKon reads, is there any case on the read path where__iomap_dio_rw()oriomap_dio_complete()actually bounces back to buffered I/O?
The buffer_head path has the same issue. I suggest addressing it in another thread.
@joseph,
There is no action to clear IOCB_DIRECT in the buffer_head path, so I think the code below can be written more simply.
from
+ if (ret == 0) {
+ iocb->ki_flags &= ~IOCB_DIRECT;
+ ret = generic_file_read_iter(iocb, to);
+ }
to:
+ if (ret == 0)
+ ret = filemap_read(iocb, to, 0);
Thanks,
Heming
Quoted 8 lines
Quoted 5 lines
fs/ocfs2/file.c +4
-0+ }else { + iocb->ki_flags &= ~IOCB_DIRECT ; + ret =generic_file_read_iter (iocb, to); + }[ ... ]
Heming Zhao
reviewerRFC PATCH v3 3/4 ocfs2: switch dio write path from buffer_head to iomap
In reply to Heming ZhaoConverts OCFS2's DIO write path from the legacy buffer_head infrastructure to the modern iomap framework.
Key modifications and designs are as follows:
Iomap can handle more than one cluster per call. Refactor 'struct ocfs2_write_ctxt' to use a flexible array 'w_desc[]' instead of a fixed-size array (which was 1 for 4K page on x86_64). Dynamically allocate the context based on the write length ('w_clen') in 'ocfs2_alloc_write_ctxt()'.
Introduce 'ocfs2_dio_wr_map_blocks()' to allocate and map direct write blocks. Define OCFS2_DIO_WR_MAX_MAP_BYTES to limit mappings to 1 MiB chunk per call, restricting allocation granularity to prevent resource exhaustion during huge asynchronous sequential writes.
Implement ocfs2_dio_write_end_io() and ocfs2_iomap_dio_end_io_write() to handle IO completion. They convert UNWRITTEN extents, updates inode size (EOF), and deletes the inode from the orphan directory if it was appended.
In the buffer_head path, ocfs2_dio_wr_get_block() stores dwc in bh->b_private, enabling it to detect the first call for an IO chunk and invoke ocfs2_add_inode_to_orphan() to protect the extent allocation. However, iomap doesn't provide a similar private field. Therefore, moves ocfs2_add_inode_to_orphan() from ocfs2_dio_wr_map_blocks(the buffer_head counterpart to ocfs2_dio_wr_get_block()) to ocfs2_file_write_iter().
Doesn't like ocfs2_dio_wr_get_block(), move the zero operations from ocfs2_dio_wr_map_blocks() to ocfs2_file_write_iter(). If do the zeroing in ocfs2_dio_wr_map_blocks() would zero blocks that earlier mappings of this same dio have already written, as i_size is only updated at dio completion. this will makes xfstests generic/538 failure.
--- fs/ocfs2/aops.c | 426 ++++++++++++++++++++++++++++++++++++-- fs/ocfs2/aops.h | 1 + fs/ocfs2/buffer_head_io.c | 7 +- fs/ocfs2/file.c | 96 +++++++-- fs/ocfs2/ocfs2.h | 2 + 5 files changed, 498 insertions(+), 34 deletions(-)
fs/ocfs2/aops.c +414 -12
@@ -753,6 +753,8 @@ int ocfs2_map_folio_blocks(struct folio *folio, u64 *p_blkno,#define OCFS2_MAX_CTXT_PAGES (OCFS2_MAX_CLUSTERSIZE / PAGE_SIZE)#endif +#define OCFS2_DIO_WR_MAX_MAP_BYTES (1U << 20)/* 1 MiB */ +#define OCFS2_MAX_CLUSTERS_PER_PAGE (PAGE_SIZE / OCFS2_MIN_CLUSTERSIZE)struct ocfs2_unwritten_extent {
@@ -788,8 +790,6 @@ struct ocfs2_write_ctxt {/* Type of caller. Must be one of buffer, mmap, direct. */ ocfs2_write_type_t w_type; -struct ocfs2_write_cluster_desc w_desc[OCFS2_MAX_CLUSTERS_PER_PAGE ]; -/* * This is true if page_size > cluster_size. *
@@ -839,6 +839,8 @@ struct ocfs2_write_ctxt {struct list_head w_unwritten_list;unsigned int w_unwritten_count; + +struct ocfs2_write_cluster_desc w_desc[]; };void ocfs2_unlock_and_free_folios (struct folio **folios,int num_folios)
@@ -906,17 +908,24 @@ static int ocfs2_alloc_write_ctxt(struct ocfs2_write_ctxt **wcp, unsigned len, ocfs2_write_type_t type, struct buffer_head *di_bh) { - u32 cend; + u32 w_cpos, cend, w_clen;struct ocfs2_write_ctxt *wc; +size_t alloc_size; + + w_cpos = pos >> osb->s_clustersize_bits; + cend = (pos + len -1 ) >> osb->s_clustersize_bits; + w_clen = cend - w_cpos +1 ; - wc = kzalloc_obj(struct ocfs2_write_ctxt, GFP_NOFS); + alloc_size =sizeof (struct ocfs2_write_ctxt ) + + w_clen *sizeof (struct ocfs2_write_cluster_desc ); + + wc =kzalloc (alloc_size,GFP_NOFS );if (!wc)return -ENOMEM ; - wc->w_cpos = pos >> osb->s_clustersize_bits; + wc->w_cpos = w_cpos; wc->w_first_new_cpos =UINT_MAX ; - cend = (pos + len -1 ) >> osb->s_clustersize_bits; - wc->w_clen = cend - wc->w_cpos +1 ; + wc->w_clen = w_clen;get_bh (di_bh); wc->w_di_bh = di_bh; wc->w_type = type;
@@ -2207,7 +2216,7 @@ static int ocfs2_dio_wr_get_block(struct inode *inode, sector_t iblock,struct ocfs2_write_cluster_desc *desc =NULL ;struct ocfs2_dio_write_ctxt *dwc =NULL ;struct buffer_head *di_bh =NULL ; -u64 p_blkno; +u64 p_blkno =0 ;unsigned int i_blkbits = inode->i_sb->s_blocksize_bits;loff_t pos = iblock << i_blkbits;sector_t endblk = (i_size_read (inode) -1 ) >> i_blkbits;
@@ -2347,6 +2356,183 @@ static int ocfs2_dio_wr_get_block(struct inode *inode, sector_t iblock,return ret; } +/* +* Prepare the area between i_size and a direct write that starts or ends +* beyond it. Must be called once per dio, before any io is issued, with +* i_rwsem and the rw lock held. +*/ +int ocfs2_dio_zero_eof (struct inode *inode,loff_t pos,size_t count) +{ +struct ocfs2_inode_info *oi =OCFS2_I (inode); +struct buffer_head *di_bh =NULL ; +int ret; + +if (pos + count <=i_size_read (inode)) +return 0 ; + + ret =ocfs2_inode_lock (inode, &di_bh,1 ); +if (ret) { +mlog_errno (ret); +return ret; + } + +down_write (&oi->ip_alloc_sem); +if (ocfs2_sparse_alloc (OCFS2_SB (inode->i_sb))) + ret =ocfs2_zero_tail (inode, di_bh, pos); +else + ret =ocfs2_expand_nonsparse_inode (inode, di_bh, pos, count, +NULL ); +if (ret <0 ) +mlog_errno (ret); +up_write (&oi->ip_alloc_sem); + +ocfs2_inode_unlock (inode,1 ); +brelse (di_bh); +return ret; +} + +/* copy from ocfs2_dio_wr_get_block */ +static int ocfs2_dio_wr_map_blocks (struct inode *inode, +struct ocfs2_map_block *map,int create) +{ +struct ocfs2_inode_info *oi =OCFS2_I (inode); +struct ocfs2_write_ctxt *wc; +struct ocfs2_write_cluster_desc *desc =NULL ; +struct buffer_head *di_bh =NULL ; +u64 p_blkno, bpc =ocfs2_clusters_to_blocks (inode->i_sb,1 ); +sector_t iblock = map->lblk; +unsigned int i_blkbits = inode->i_sb->s_blocksize_bits; +loff_t pos = iblock << i_blkbits; +sector_t endblk = (i_size_read (inode) -1 ) >> i_blkbits; +unsigned int total_len = map->len << i_blkbits; +unsigned int map_len; +unsigned int cont_clusters =1 ; +int ret =0 , idx; + +/* +* map->len is count in ocfs2_iomap_begin according to write +* "pos" and "end", we need map twice to return different buffer state: +* 1. area in file size, not set NEW; +* 2. area out file size, set NEW. +* +* iblock endblk +* |--------|---------|---------|--------- +* |<-------area in file------->| +*/ +if ((iblock <= endblk) && + ((iblock + ((total_len -1 ) >> i_blkbits)) > endblk)) + total_len = (endblk - iblock +1 ) << i_blkbits; + +/* +* Bound how much we allocate/prepare (and journal) per call. map->len +* below still reports only the physically-contiguous prefix to iomap; +* iomap calls us again for the remainder. +*/ + map_len =min (total_len,OCFS2_DIO_WR_MAX_MAP_BYTES ); + +mlog (0 ,"get block of %llu at %llu:%u req %u\n" , + inode->i_ino, pos, map_len, total_len); + +/* +* Because we need to change file size in ocfs2_iomap_dio_end_io_write(), +* or we may need to add it to orphan dir. So can not fall to fast path +* while file size will be changed. +*/ +if (pos + total_len <=i_size_read (inode)) { +/* This is the fast path for re-write. */ + ret =ocfs2_map_blocks (inode, map,OCFS2_GET_BLOCKS_CREATE ); +if ((map->flags &OCFS2_MAP_MAPPED ) && + !(map->flags &OCFS2_MAP_NEW ) && ret ==0 ) + goto out; + +/* Clear state set by ocfs2_map_blocks. */ + map->flags =0 ; + } + + ret =ocfs2_inode_lock (inode, &di_bh,1 ); +if (ret) { +mlog_errno (ret); + goto out; + } + +down_write (&oi->ip_alloc_sem); + +/* +* Note, Zeroing the gap between i_size and the write is done +* once per dio by ocfs2_dio_zero_eof() before the io is issued. +*/ + ret =ocfs2_write_begin_nolock (inode->i_mapping, pos, map_len, +OCFS2_WRITE_DIRECT ,NULL , + (void **)&wc, di_bh,NULL ); +if (ret) { +mlog_errno (ret); + goto unlock; + } + + desc = &wc->w_desc[0 ]; + + p_blkno =ocfs2_clusters_to_blocks (inode->i_sb, desc->c_phys); +if (p_blkno ==0 ) { + ret = -EIO ; +mlog_errno (ret); + goto unlock; + } + p_blkno += iblock & (bpc -1 ); + +/* +* Calculate physically contiguous clusters by checking descriptor arrays. +* This strictly prevents map->len from expanding past non-contiguous physical bounds. +*/ +for (idx =1 ; idx < wc->w_clen; idx++) { +if (wc->w_desc[idx].c_phys == wc->w_desc[idx -1 ].c_phys +1 && + wc->w_desc[idx].c_needs_zero == wc->w_desc[idx -1 ].c_needs_zero) { + cont_clusters++; + }else { +break ; + } + } + map->pblk = p_blkno; +/* +* Only report the mapping as UNWRITTEN when the cluster is newly +* allocated or was already unwritten (desc->c_needs_zero). Reporting +* an already-written cluster as UNWRITTEN makes the iomap direct-io +* core zero the sub-block head/tail padding, which destroys existing +* data when a sub-block write lands inside a written cluster. +*/ + map->flags |=OCFS2_MAP_MAPPED ; +if (desc->c_needs_zero) + map->flags |=OCFS2_MAP_UNWRITTEN ; +/* subtract the offset */ + map->len = cont_clusters * bpc - (iblock & (bpc -1 )); + map->len =min_t (u64, map->len, map_len >> i_blkbits); + +if (desc->c_needs_zero) + map->flags |=OCFS2_MAP_NEW ; + +if (iblock > endblk) + map->flags |=OCFS2_MAP_NEW ; + + map->flags |=OCFS2_MAP_DEFER_COMPLETION ; + +ocfs2_free_unwritten_list (inode, &wc->w_unwritten_list); +/* Here returns only two types: map_len (success), errno (failure). */ + ret =ocfs2_write_end_nolock (inode->i_mapping, pos, map_len, map_len, wc); +if (ret <0 ) { +mlog_errno (ret); + goto unlock; + } +WARN_ON_ONCE (ret != map_len); + ret =0 ; + +unlock: +up_write (&oi->ip_alloc_sem); +ocfs2_inode_unlock (inode,1 ); +brelse (di_bh); + +out: +return ret; +} + static int ocfs2_dio_end_io_write(struct inode *inode, struct ocfs2_dio_write_ctxt *dwc, loff_t offset,
@@ -2546,6 +2732,12 @@ static ssize_t ocfs2_direct_IO(struct kiocb *iocb, struct iov_iter *iter) ocfs2_dio_end_io, 0); } +static int ocfs2_iomap_alloc (struct inode *inode,struct ocfs2_map_block *map, +unsigned int flags) +{ +return ocfs2_dio_wr_map_blocks (inode, map,1 ); +} +static int ocfs2_iomap_begin (struct inode *inode,loff_t offset,loff_t length,unsigned int flags,struct iomap *iomap,struct iomap *srcmap) {
@@ -2553,6 +2745,7 @@ static int ocfs2_iomap_begin(struct inode *inode, loff_t offset, loff_t length,struct ocfs2_map_block map;struct ocfs2_inode_info *oi =OCFS2_I (inode);u8 blkbits = inode->i_blkbits; +unsigned int orig_mlen;if ((offset >> blkbits) >OCFS2_MAX_LOGICAL_BLOCK )return -EINVAL ;
@@ -2564,9 +2757,32 @@ static int ocfs2_iomap_begin(struct inode *inode, loff_t offset, loff_t length, map.len =min_t (loff_t, (offset + length -1 ) >> blkbits,OCFS2_MAX_LOGICAL_BLOCK ) - map.lblk +1 ; map.flags =0 ; + orig_mlen = map.len;if (flags &IOMAP_WRITE ) { -return -EOPNOTSUPP ; +/* +* We check here if the blocks are already allocated, then we +* don't need to start a journal txn and we can directly return +* the mapping information. This could boost performance +* especially in multi-threaded overwrite requests. +*/ +if (offset + length <=i_size_read (inode)) { +down_read (&oi->ip_alloc_sem); + ret =ocfs2_map_blocks (inode, &map,0 ); +up_read (&oi->ip_alloc_sem); +/* +* For atomic writes the entire requested length should +* be mapped. +*/ +if (map.flags &OCFS2_MAP_MAPPED ) { +if ((!(flags &IOMAP_ATOMIC ) && ret >0 ) || + (flags &IOMAP_ATOMIC && ret >= orig_mlen)) { + goto out; + } + } + map.len = orig_mlen; + } + ret =ocfs2_iomap_alloc (inode, &map, flags); }else {down_read (&oi->ip_alloc_sem); ret =ocfs2_map_blocks (inode, &map,0 );
@@ -2576,6 +2792,7 @@ static int ocfs2_iomap_begin(struct inode *inode, loff_t offset, loff_t length,if (ret <0 )return ret; +out:/* * Before returning to iomap, let's ensure the allocated mapping * covers the entire requested length for atomic writes.
@@ -2641,8 +2858,8 @@ static int ocfs2_iomap_dio_end_io(struct kiocb *iocb, ssize_t size, int error,return error; } -static int ocfs2_dio_end_io_r_pr (struct kiocb *iocb,ssize_t size, -int error,unsigned int flags) +static int ocfs2_iomap_dio_end_io_r_pr (struct kiocb *iocb,ssize_t size, +int error,unsigned int flags) {if (error) mlog_ratelimited(ML_ERROR, "Direct IO failed, bytes = %lld errno:%d",
@@ -2654,8 +2871,193 @@ static int ocfs2_dio_end_io_r_pr(struct kiocb *iocb, ssize_t size, }const struct iomap_dio_ops ocfs2_iomap_dio_ops_r_pr = { - .end_io = ocfs2_dio_end_io_r_pr, + .end_io = ocfs2_iomap_dio_end_io_r_pr, +}; + +/* copy from ocfs2_dio_end_io_write */ +static int ocfs2_iomap_dio_end_io_write (struct inode *inode, +loff_t offset, +ssize_t bytes) +{ +struct ocfs2_cached_dealloc_ctxt dealloc; +struct ocfs2_extent_tree et; +struct ocfs2_super *osb =OCFS2_SB (inode->i_sb); +struct ocfs2_inode_info *oi =OCFS2_I (inode); +struct buffer_head *di_bh =NULL ; +struct ocfs2_dinode *di; +struct ocfs2_alloc_context *data_ac =NULL ; +struct ocfs2_alloc_context *meta_ac =NULL ; +handle_t *handle =NULL ; +loff_t end = offset + bytes; +int ret =0 , credits =0 ; +struct ocfs2_map_block map; +unsigned int blkbits = inode->i_blkbits; +unsigned int ue_cpos =0 , ue_phys =0 , ue_len =0 ; +unsigned int curr_lblk, end_lblk; + + map.lblk = offset >> blkbits; + +ocfs2_init_dealloc_ctxt (&dealloc); + + ret =ocfs2_inode_lock (inode, &di_bh,1 ); +if (ret <0 ) { +mlog_errno (ret); + goto out; + } + +down_write (&oi->ip_alloc_sem); + + di = (struct ocfs2_dinode *)di_bh->b_data; + +ocfs2_init_dinode_extent_tree (&et,INODE_CACHE (inode), di_bh); + +/* Attach dealloc with extent tree in case that we may reuse extents +* which are already unlinked from current extent tree due to extent +* rotation and merging. +*/ + et.et_dealloc = &dealloc; + + ret =ocfs2_lock_allocators (inode, &et,0 , (bytes >> osb->s_clustersize_bits)*2 , + &data_ac, &meta_ac); +if (ret) { +mlog_errno (ret); + goto unlock; + } + + credits =ocfs2_calc_extend_credits (inode->i_sb, &di->id2.i_list); + + handle =ocfs2_start_trans (osb, credits); +if (IS_ERR (handle)) { + ret =PTR_ERR (handle); +mlog_errno (ret); + goto unlock; + } + ret =ocfs2_journal_access_di (handle,INODE_CACHE (inode), di_bh, +OCFS2_JOURNAL_ACCESS_WRITE ); +if (ret) { +mlog_errno (ret); + goto commit; + } + + curr_lblk = offset >> blkbits; +/* +* Round the end up so the final partial block (sub-block direct I/O) +* is included; otherwise the last, partially-written cluster is left +* unwritten and reads back as zero. +*/ + end_lblk = (offset + bytes + (1 << blkbits) -1 ) >> blkbits; +while (ret >=0 && curr_lblk < end_lblk) { +memset (&map,0 ,sizeof (map)); + map.lblk = curr_lblk; + map.len = end_lblk - curr_lblk; + + ret =ocfs2_assure_trans_credits (handle, credits); +if (ret <0 ) { +mlog_errno (ret); +break ; + } + + ret =ocfs2_map_blocks (inode, &map,0 ); +if (ret <0 ) { +mlog_errno (ret); +break ; + } +if (ret ==0 ) +break ; + +if (map.flags &OCFS2_MAP_UNWRITTEN ) { + ue_cpos =ocfs2_blocks_to_clusters (inode->i_sb, map.lblk); + ue_phys =ocfs2_blocks_to_clusters (inode->i_sb, map.pblk); + ue_len =ocfs2_clusters_for_blocks (inode->i_sb, + map.lblk + map.len) - ue_cpos; + + ret =ocfs2_mark_extent_written (inode, &et, handle, + ue_cpos, ue_len, + ue_phys, + meta_ac, &dealloc); +if (ret <0 ) { +mlog_errno (ret); +break ; + } + } + curr_lblk += map.len; + } + +if (end >i_size_read (inode)) { + ret =ocfs2_set_inode_size (handle, inode, di_bh, end); +if (ret <0 ) +mlog_errno (ret); + } + +commit: +ocfs2_commit_trans (osb, handle); + +unlock: +up_write (&oi->ip_alloc_sem); + +if (data_ac) +ocfs2_free_alloc_context (data_ac); +if (meta_ac) +ocfs2_free_alloc_context (meta_ac); + +down_write (&oi->ip_alloc_sem); +if (!ret && (di->i_flags &cpu_to_le32 (OCFS2_DIO_ORPHANED_FL ))) { + ret =ocfs2_del_inode_from_orphan (osb, inode, di_bh,0 ,0 ); +if (ret <0 ) +mlog_errno (ret); + } +up_write (&oi->ip_alloc_sem); +ocfs2_inode_unlock (inode,1 ); +brelse (di_bh); + +out: +ocfs2_run_deallocs (osb, &dealloc); +return ret; +} + +static int ocfs2_dio_write_end_io (struct kiocb *iocb,ssize_t size, +int error,unsigned int flags,int level) +{ +struct inode *inode =file_inode (iocb->ki_filp); +loff_t offset = iocb->ki_pos; +int ret =0 ; + +if (error) +mlog_ratelimited (ML_ERROR ,"Direct IO failed, bytes = %lld errno:%d" , + (long long )size, error); + +if (size && ((flags &IOMAP_DIO_UNWRITTEN ) || + (offset + size >i_size_read (inode)))) { + ret =ocfs2_iomap_dio_end_io_write (inode, offset, size); +if (ret <0 ) +mlog_errno (ret); + } + + error =ocfs2_iomap_dio_end_io (iocb, size, error, level); + +return error; +} + +static int ocfs2_iomap_dio_end_io_w_pr (struct kiocb *iocb,ssize_t size, +int error,unsigned int flags) +{ +return ocfs2_dio_write_end_io (iocb, size, error, flags,0 ); +} + +static int ocfs2_iomap_dio_end_io_w_ex (struct kiocb *iocb,ssize_t size, +int error,unsigned int flags) +{ +return ocfs2_dio_write_end_io (iocb, size, error, flags,1 ); +} + +const struct iomap_dio_ops ocfs2_iomap_dio_ops_w_pr = { + .end_io = ocfs2_iomap_dio_end_io_w_pr, +}; + +const struct iomap_dio_ops ocfs2_iomap_dio_ops_w_ex = { + .end_io = ocfs2_iomap_dio_end_io_w_ex, }; +const struct address_space_operations ocfs2_aops = { .dirty_folio = block_dirty_folio, .read_folio = ocfs2_read_folio,
fs/ocfs2/aops.h +1 -0
@@ -42,6 +42,7 @@ int ocfs2_size_fits_inline_data(struct buffer_head *di_bh, u64 new_size);int ocfs2_get_block (struct inode *inode,sector_t iblock,struct buffer_head *bh_result,int create); +int ocfs2_dio_zero_eof (struct inode *inode,loff_t pos,size_t count);int ocfs2_map_blocks (struct inode *inode,struct ocfs2_map_block *map,int flags);/* all ocfs2_dio_end_io()'s fault */
fs/ocfs2/buffer_head_io.c +3 -4
@@ -332,6 +332,8 @@ int ocfs2_read_blocks(struct ocfs2_caching_info *ci, u64 block, int nr,wait_on_buffer (bh);put_bh (bh); bhs[i] =NULL ; + }else if (bh &&buffer_uptodate (bh)) { +clear_buffer_uptodate (bh); }continue ; }
@@ -360,11 +362,8 @@ int ocfs2_read_blocks(struct ocfs2_caching_info *ci, u64 block, int nr,BUG_ON (buffer_jbd (bh));clear_buffer_needs_validate (bh); status =validate (sb, bh); -if (status) { -if (buffer_uptodate (bh)) -clear_buffer_uptodate (bh); +if (status) goto read_failure; - } } }
fs/ocfs2/file.c +78 -18
136 lines not on this page · raw message
fs/ocfs2/ocfs2.h +2 -0
@@ -551,6 +551,8 @@ struct ocfs2_map_block {extern const struct iomap_ops ocfs2_iomap_ops;extern const struct iomap_dio_ops ocfs2_iomap_dio_ops_r_pr; +extern const struct iomap_dio_ops ocfs2_iomap_dio_ops_w_pr; +extern const struct iomap_dio_ops ocfs2_iomap_dio_ops_w_ex;/* Flags used by ocfs2_map_blocks() */ #define OCFS2_GET_BLOCKS_CREATE (0x0001)
Signature · 1 line
2.55.0
A message that is not in the archive (sent off-list or not yet received): [email protected]
Heming Zhao
reviewerRe: [RFC PATCH v3 3/4] ocfs2: switch dio write path from buffer_head to iomap
Quoted 97 lines · On 10/18/26 23:16, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 11 potential issue(s) to consider:
- [Critical] ocfs2: multi-clustermap_leninocfs2_dio_wr_map_blocks()breaks write context
- [Critical] ocfs2: prematureocfs2_free_unwritten_list()inocfs2_dio_wr_map_blocks()
- [Critical] ocfs2: broken error handling and bio failure check inocfs2_dio_write_end_io()
- [High] ocfs2:extents_to_splitundercount inocfs2_iomap_dio_end_io_write()
- [High] ocfs2: orphan entry leak on DIO write error and fallback paths
- [High] ocfs2: broken locking and sync on DIO fallback inocfs2_file_write_iter()
- [High] ocfs2: broken fast path and endblk underflow inocfs2_dio_wr_map_blocks()
- [High] ocfs2: accidental revert ofbuffer_headvalidation fix inocfs2_read_blocks()
- [High] ocfs2:ip_alloc_semlock inversion inocfs2_iomap_dio_end_io_write()
- [High] ocfs2: missingIOMAP_DIO_FORCE_WAITon intra-cluster extending AIO DIO writes
- [Medium] ocfs2: missingfile_remove_privs()andfile_update_time()in iomap DIO write --commit
e9c307eccfa1367c7b156cd785a2734d2998abc3
Author: Heming Zhao <[email protected]>ocfs2: switch dio write path from
buffer_headto iomapThis commit converts the OCFS2 direct I/O write path from the legacy
buffer_headinfrastructure to the iomap framework, refactoringocfs2_write_ctxtto use a flexible array for multi-cluster mappings and introducing iomap block mapping and completion handlers.Quoted 4 lines
fs/ocfs2/aops.c +0
-0[ ... ]
Quoted 1 line
fs/ocfs2/aops.c +0
-0@@ -2347,6 +2356,183 @@ static int ocfs2_dio_wr_get_block(struct inode *inode, sector_t iblock,[ ... ]
Quoted 47 lines
fs/ocfs2/aops.c +47
-0+/* copy from ocfs2_dio_wr_get_block */ +static int ocfs2_dio_wr_map_blocks (struct inode *inode, +struct ocfs2_map_block *map,int create) +{ +struct ocfs2_inode_info *oi =OCFS2_I (inode); +struct ocfs2_write_ctxt *wc; +struct ocfs2_write_cluster_desc *desc =NULL ; +struct buffer_head *di_bh =NULL ; +u64 p_blkno, bpc =ocfs2_clusters_to_blocks (inode->i_sb,1 ); +sector_t iblock = map->lblk; +unsigned int i_blkbits = inode->i_sb->s_blocksize_bits; +loff_t pos = iblock << i_blkbits; +sector_t endblk = (i_size_read (inode) -1 ) >> i_blkbits; +unsigned int total_len = map->len << i_blkbits; +unsigned int map_len; +unsigned int cont_clusters =1 ; +int ret =0 , idx; + +/* +* map->len is count in ocfs2_iomap_begin according to write +* "pos" and "end", we need map twice to return different buffer state: +* 1. area in file size, not set NEW; +* 2. area out file size, set NEW. +* +* iblock endblk +* |--------|---------|---------|--------- +* |<-------area in file------->| +*/ +if ((iblock <= endblk) && + ((iblock + ((total_len -1 ) >> i_blkbits)) > endblk)) + total_len = (endblk - iblock +1 ) << i_blkbits; + +/* +* Bound how much we allocate/prepare (and journal) per call. map->len +* below still reports only the physically-contiguous prefix to iomap; +* iomap calls us again for the remainder. +*/ + map_len =min (total_len,OCFS2_DIO_WR_MAX_MAP_BYTES ); + +mlog (0 ,"get block of %llu at %llu:%u req %u\n" , + inode->i_ino, pos, map_len, total_len); + +/* +* Because we need to change file size in ocfs2_iomap_dio_end_io_write(), +* or we may need to add it to orphan dir. So can not fall to fast path +* while file size will be changed. +*/ fs/ocfs2/aops.c static int ocfs2_dio_wr_get_block(struct inode *inode, sector_t iblock, · line 2450
Quoted 10 lines
fs/ocfs2/aops.c +10
-0+if (pos + total_len <=i_size_read (inode)) { +/* This is the fast path for re-write. */ + ret =ocfs2_map_blocks (inode, map,OCFS2_GET_BLOCKS_CREATE ); +if ((map->flags &OCFS2_MAP_MAPPED ) && + !(map->flags &OCFS2_MAP_NEW ) && ret ==0 ) + goto out; + +/* Clear state set by ocfs2_map_blocks. */ + map->flags =0 ; + }[Severity: High]
Are there a few issues with the rewrite fast path and endblk calculation inocfs2_dio_wr_map_blocks()?
Quoted 5 lines
First,
ocfs2_map_blocks()is called here without holdingoi->ip_alloc_sem(unlikeocfs2_dio_wr_get_block(), which calledocfs2_lock_get_block()to acquiredown_read(&oi->ip_alloc_sem), and unlikeocfs2_iomap_begin()), even thoughocfs2_map_blocks()->ocfs2_extent_map_get_blocks()requiresip_alloc_semto be held.
I agree, I overlooked the locking issue. I will add an ocfs2_lock_map_blocks() (similar to ocfs2_lock_get_block()) in next version.
Quoted 5 lines
Second,
ocfs2_map_blocks()returnsmap->len(> 0) on success rather than 0, so checking ret == 0 whenOCFS2_MAP_MAPPEDis set is never true. When it falls through to the slow path,map->lenhas also been mutated without being clamped tototal_len>>i_blkbitsor restored.
I will change the return value style of ocfs2_map_blocks() based on [1/4] review comments. this issue will no longer exist.
Quoted 10 lines
Third, when
i_size_read(inode)== 0, computing:sector_t endblk = (i_size_read(inode) - 1) >> i_blkbits;underflows the unsigned
sector_ttoULLONG_MAX. As a result, the later check (iblock > endblk) is false for an empty file, and on a non-sparse filesystem whereocfs2_dio_zero_eof()pre-allocated written blocks viaocfs2_expand_nonsparse_inode()(leavingdesc->c_needs_zero== 0),OCFS2_MAP_NEW(and thusIOMAP_F_NEW) will not be set for sub-block writes.
In my view, i_size can never be 0 here. inline inode won't enter the DIO iomap path.
Quoted 65 lines
Quoted 11 lines
fs/ocfs2/aops.c +10
-0+ + ret =ocfs2_inode_lock (inode, &di_bh,1 ); +if (ret) { +mlog_errno (ret); + goto out; + } + +down_write (&oi->ip_alloc_sem); + +/* fs/ocfs2/aops.c static int ocfs2_dio_wr_get_block(struct inode *inode, sector_t iblock, · line 2470
Quoted 10 lines
fs/ocfs2/aops.c +10
-0+* Note, Zeroing the gap between i_size and the write is done +* once per dio by ocfs2_dio_zero_eof() before the io is issued. +*/ + ret =ocfs2_write_begin_nolock (inode->i_mapping, pos, map_len, +OCFS2_WRITE_DIRECT ,NULL , + (void **)&wc, di_bh,NULL ); +if (ret) { +mlog_errno (ret); + goto unlock; + }[Severity: Critical]
Can passing a multi-clustermap_len(up toOCFS2_DIO_WR_MAX_MAP_BYTES, 1 MiB) toocfs2_write_begin_nolock()trigger aBUG_ON()or expose stale disk data whenwc->w_clen> 1?Previously,
ocfs2_dio_wr_get_block()clamped each call to at most one cluster:len = osb->s_clustersize - (pos & (osb->s_clustersize - 1)); len = min(total_len, len);With
map_lenup to 1 MiB,ocfs2_alloc_write_ctxt()setswc->w_clen> 1, but the downstream helpers inocfs2_write_begin_nolock()still assume a single cluster:1. When
osb->s_clustersize>PAGE_SIZE,ocfs2_grab_folios_for_write()only grabs page-cache folios for one cluster starting at cpos =wc->w_cpos:if (new) { wc->w_num_folios = ocfs2_pages_per_cluster(inode->i_sb); start = ocfs2_align_clusters_to_page_index(inode->i_sb, cpos);If pos is not cluster-aligned and
cluster_of_pagesis set, wc->w_folios[] is populated with folios from the first cluster whilewc->w_target_foliois NULL (forOCFS2_WRITE_DIRECT). Thenocfs2_write_cluster_by_desc()callsocfs2_write_cluster()for every cluster 0 ..wc->w_clen- 1, re-iterating over the same wc->w_folios[] array. If any cluster in the range hasc_needs_zero== 0 (e.g. a write crossing an unwritten cluster and an already written cluster),ocfs2_prepare_folio_for_write()is called with new == 0 and hitsBUG_ON(!new):} else { BUG_ON(!new);2. Also when
osb->s_clustersize>PAGE_SIZE, if a multi-cluster allocating DIO write ends partway through the final cluster (wc->w_cpos+wc->w_clen- 1),ocfs2_grab_folios_for_write()never grabs or zeroes the trailing pages of that last cluster because it only indexes pages inwc->w_cpos. Whenocfs2_iomap_dio_end_io_write()later marks the whole last cluster written, those unzeroed trailing pages expose stale on-disk data. In addition,cluster_of_pagesinocfs2_write_begin_nolock()only checks wc->w_desc[0].c_needs_zero || wc->w_desc[wc->w_clen - 1].c_needs_zero, missing intermediate clusters whenwc->w_clen> 2.
Quoted 10 lines
3. Even when
osb->s_clustersize==PAGE_SIZE(4 KiB),wc->w_clencan be up to 256 clusters.ocfs2_write_begin_nolock()reserves metadata inmeta_acfor only a single tree growth viaocfs2_lock_allocators(), and starts a transaction with credits for one extent operation, butocfs2_write_cluster_by_desc()callsocfs2_add_inode_data(..., 1, ...)in a loop up to 256 times. If fragmented allocations split/grow the extent tree more than once across that loop,ocfs2_add_clusters_in_btree()can exhaustmeta_acand return-EAGAIN, hittingmlog_bug_on_msg(ret == -EAGAIN, ...)inocfs2_write_cluster().
I agree. For the initial fix, I propose taking the simplest approach and ignoring I/O performance overhead. We can just use one-cluster-size allocation in the slow path. We can start a separate patch thread later to implement the multi-cluster map_len feature.
Quoted 79 lines
Quoted 39 lines
fs/ocfs2/aops.c +39
-0+ + desc = &wc->w_desc[0 ]; + + p_blkno =ocfs2_clusters_to_blocks (inode->i_sb, desc->c_phys); +if (p_blkno ==0 ) { + ret = -EIO ; +mlog_errno (ret); + goto unlock; + } + p_blkno += iblock & (bpc -1 ); + +/* +* Calculate physically contiguous clusters by checking descriptor arrays. +* This strictly prevents map->len from expanding past non-contiguous physical bounds. +*/ +for (idx =1 ; idx < wc->w_clen; idx++) { +if (wc->w_desc[idx].c_phys == wc->w_desc[idx -1 ].c_phys +1 && + wc->w_desc[idx].c_needs_zero == wc->w_desc[idx -1 ].c_needs_zero) { + cont_clusters++; + }else { +break ; + } + } + map->pblk = p_blkno; +/* +* Only report the mapping as UNWRITTEN when the cluster is newly +* allocated or was already unwritten (desc->c_needs_zero). Reporting +* an already-written cluster as UNWRITTEN makes the iomap direct-io +* core zero the sub-block head/tail padding, which destroys existing +* data when a sub-block write lands inside a written cluster. +*/ + map->flags |=OCFS2_MAP_MAPPED ; +if (desc->c_needs_zero) + map->flags |=OCFS2_MAP_UNWRITTEN ; +/* subtract the offset */ + map->len = cont_clusters * bpc - (iblock & (bpc -1 )); + map->len =min_t (u64, map->len, map_len >> i_blkbits); + +if (desc->c_needs_zero)fs/ocfs2/aops.c static int ocfs2_dio_wr_get_block(struct inode *inode, sector_t iblock, · line 2519
Quoted 10 lines
fs/ocfs2/aops.c +10
-0+ map->flags |=OCFS2_MAP_NEW ; + +if (iblock > endblk) + map->flags |=OCFS2_MAP_NEW ; + + map->flags |=OCFS2_MAP_DEFER_COMPLETION ; + +ocfs2_free_unwritten_list (inode, &wc->w_unwritten_list); +/* Here returns only two types: map_len (success), errno (failure). */ + ret =ocfs2_write_end_nolock (inode->i_mapping, pos, map_len, map_len, wc);[Severity: Critical]
Does callingocfs2_free_unwritten_list(inode, &wc->w_unwritten_list)here inocfs2_dio_wr_map_blocks()before the DIO completes cause a subsequent mapping of the same cluster to zero out blocks from the first mapping whens_clustersize>PAGE_SIZE?During
ocfs2_write_begin_nolock(),ocfs2_populate_write_desc()->ocfs2_unwritten_check()adds unwritten clusters tooi->ip_unwritten_listso that another mapping of the same cluster before DIO completion sees the entry onoi->ip_unwritten_listand clearsdesc->c_needs_zero:list_for_each_entry(ue, &oi->ip_unwritten_list, ue_ip_node) { if (desc->c_cpos == ue->ue_cpos) { BUG_ON(desc->c_new); desc->c_needs_zero = 0; desc->c_clear_unwritten = 0; goto unlock; } }Because
ocfs2_dio_wr_map_blocks()removes the cluster fromoi->ip_unwritten_listimmediately while the on-disk extent still hasOCFS2_EXT_UNWRITTENset, ifocfs2_dio_wr_map_blocks()is called a second time for the same cluster (for example when a write crosses endblk mid-cluster andtotal_lenis split at endblk, whencont_clusters<wc->w_clen, when a > 1 MiB write is not cluster-aligned, or during concurrent DIO writes under coherency=buffered),ocfs2_unwritten_check()will not find the cluster onoi->ip_unwritten_listand will leavedesc->c_needs_zero= 1.
Quoted 5 lines
ocfs2_grab_folios_for_write()will then grab page-cache folios for the prefix of the cluster ([start,target_index- 1]), zero them, and dirty their buffers inocfs2_write_end_nolock(), whichkiocb_invalidate_post_direct_write()flushes to disk on DIO completion, overwriting the first mapping's data with zeros.
I plan to use the .submit_io and .end_io methods in struct iomap_dio_ops. This way, OCFS2 can pass a private structure to the end_io callback, just like in the buffer_head path.
Quoted 87 lines
Quoted 16 lines
fs/ocfs2/aops.c +15
-0+if (ret <0 ) { +mlog_errno (ret); + goto unlock; + } +WARN_ON_ONCE (ret != map_len); + ret =0 ; + +unlock: +up_write (&oi->ip_alloc_sem); +ocfs2_inode_unlock (inode,1 ); +brelse (di_bh); + +out: +return ret; +}[ ... ]
Quoted 43 lines
fs/ocfs2/aops.c +38
-1@@ -2654,8 +2871,193 @@ static int ocfs2_dio_end_io_r_pr(struct kiocb *iocb, ssize_t size, }const struct iomap_dio_ops ocfs2_iomap_dio_ops_r_pr = { - .end_io = ocfs2_dio_end_io_r_pr, + .end_io = ocfs2_iomap_dio_end_io_r_pr, +}; + +/* copy from ocfs2_dio_end_io_write */ +static int ocfs2_iomap_dio_end_io_write (struct inode *inode, +loff_t offset, +ssize_t bytes) +{ +struct ocfs2_cached_dealloc_ctxt dealloc; +struct ocfs2_extent_tree et; +struct ocfs2_super *osb =OCFS2_SB (inode->i_sb); +struct ocfs2_inode_info *oi =OCFS2_I (inode); +struct buffer_head *di_bh =NULL ; +struct ocfs2_dinode *di; +struct ocfs2_alloc_context *data_ac =NULL ; +struct ocfs2_alloc_context *meta_ac =NULL ; +handle_t *handle =NULL ; +loff_t end = offset + bytes; +int ret =0 , credits =0 ; +struct ocfs2_map_block map; +unsigned int blkbits = inode->i_blkbits; +unsigned int ue_cpos =0 , ue_phys =0 , ue_len =0 ; +unsigned int curr_lblk, end_lblk; + + map.lblk = offset >> blkbits; + +ocfs2_init_dealloc_ctxt (&dealloc); + + ret =ocfs2_inode_lock (inode, &di_bh,1 ); +if (ret <0 ) { +mlog_errno (ret); + goto out; + } + +down_write (&oi->ip_alloc_sem); + + di = (struct ocfs2_dinode *)di_bh->b_data; +fs/ocfs2/aops.c static int ocfs2_dio_end_io_r_pr(struct kiocb *iocb, ssize_t size, · line 2921
Quoted 10 lines
fs/ocfs2/aops.c +10
-0+ocfs2_init_dinode_extent_tree (&et,INODE_CACHE (inode), di_bh); + +/* Attach dealloc with extent tree in case that we may reuse extents +* which are already unlinked from current extent tree due to extent +* rotation and merging. +*/ + et.et_dealloc = &dealloc; + + ret =ocfs2_lock_allocators (inode, &et,0 , (bytes >> osb->s_clustersize_bits)*2 , + &data_ac, &meta_ac);[Severity: High]
When bytes <osb->s_clustersize(for example, a 4 KiB DIO write on a filesystem with 8 KiB..1 MiB clusters, or a 512-byte write with 4 KiB clusters), doesn't (bytes >>osb->s_clustersize_bits) * 2 evaluate to 0 (and similarly undercount when an unaligned write straddles a cluster boundary)?In
ocfs2_lock_allocators(), whenextents_to_splitis 0,max_recs_neededis 0. If the extent list currently hasnum_free_extents== 1, the check:if (!num_free_extents || (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed)) { ret = ocfs2_reserve_new_metadata(osb, et->et_root_el, meta_ac);
Quoted 5 lines
skips reserving
meta_ac. Ifocfs2_mark_extent_written()then splits the middle of an unwritten extent into three records (requiring two free records),ocfs2_grow_tree()will hit:BUG_ON(meta_ac == NULL && ocfs2_is_dealloc_empty(et));
As mentioned in my previous reply, if we implement the .submit_io and .end_io methods, this issue can be resolved in the same way as ocfs2_dio_end_io_write() in the buffer_head path
Quoted 3 lines
Also, should the extent conversion loop below batch transactions using
OCFS2_DIO_MARK_EXTENT_BATCHlikeocfs2_dio_end_io_write()does?
agree. will add in next version.
Quoted 99 lines
Quoted 79 lines
fs/ocfs2/aops.c +78
-0+if (ret) { +mlog_errno (ret); + goto unlock; + } + + credits =ocfs2_calc_extend_credits (inode->i_sb, &di->id2.i_list); + + handle =ocfs2_start_trans (osb, credits); +if (IS_ERR (handle)) { + ret =PTR_ERR (handle); +mlog_errno (ret); + goto unlock; + } + ret =ocfs2_journal_access_di (handle,INODE_CACHE (inode), di_bh, +OCFS2_JOURNAL_ACCESS_WRITE ); +if (ret) { +mlog_errno (ret); + goto commit; + } + + curr_lblk = offset >> blkbits; +/* +* Round the end up so the final partial block (sub-block direct I/O) +* is included; otherwise the last, partially-written cluster is left +* unwritten and reads back as zero. +*/ + end_lblk = (offset + bytes + (1 << blkbits) -1 ) >> blkbits; +while (ret >=0 && curr_lblk < end_lblk) { +memset (&map,0 ,sizeof (map)); + map.lblk = curr_lblk; + map.len = end_lblk - curr_lblk; + + ret =ocfs2_assure_trans_credits (handle, credits); +if (ret <0 ) { +mlog_errno (ret); +break ; + } + + ret =ocfs2_map_blocks (inode, &map,0 ); +if (ret <0 ) { +mlog_errno (ret); +break ; + } +if (ret ==0 ) +break ; + +if (map.flags &OCFS2_MAP_UNWRITTEN ) { + ue_cpos =ocfs2_blocks_to_clusters (inode->i_sb, map.lblk); + ue_phys =ocfs2_blocks_to_clusters (inode->i_sb, map.pblk); + ue_len =ocfs2_clusters_for_blocks (inode->i_sb, + map.lblk + map.len) - ue_cpos; + + ret =ocfs2_mark_extent_written (inode, &et, handle, + ue_cpos, ue_len, + ue_phys, + meta_ac, &dealloc); +if (ret <0 ) { +mlog_errno (ret); +break ; + } + } + curr_lblk += map.len; + } + +if (end >i_size_read (inode)) { + ret =ocfs2_set_inode_size (handle, inode, di_bh, end); +if (ret <0 ) +mlog_errno (ret); + } + +commit: +ocfs2_commit_trans (osb, handle); + +unlock: +up_write (&oi->ip_alloc_sem); + +if (data_ac) +ocfs2_free_alloc_context (data_ac);fs/ocfs2/aops.c static int ocfs2_dio_end_io_r_pr(struct kiocb *iocb, ssize_t size, · line 3009
Quoted 10 lines
fs/ocfs2/aops.c +10
-0+if (meta_ac) +ocfs2_free_alloc_context (meta_ac); + +down_write (&oi->ip_alloc_sem); +if (!ret && (di->i_flags &cpu_to_le32 (OCFS2_DIO_ORPHANED_FL ))) { + ret =ocfs2_del_inode_from_orphan (osb, inode, di_bh,0 ,0 ); +if (ret <0 ) +mlog_errno (ret); + } +up_write (&oi->ip_alloc_sem);[Severity: High]
Doesn't re-acquiringdown_write(&oi->ip_alloc_sem)aroundocfs2_del_inode_from_orphan()invert the lock order and trigger a recursive lockdep splat?In
ocfs2_init_locked_inode(), all non-quota inodes (including regular files andORPHAN_DIR_SYSTEM_INODE) share the lockdep class&ocfs2_file_ip_alloc_sem_keyforip_alloc_sem.
Quoted 11 lines
Inside
ocfs2_del_inode_from_orphan(), the code takesinode_lock_nested(orphan_dir_inode, I_MUTEX_NONDIR2)and thenocfs2_orphan_del()->ocfs2_find_entry()->ocfs2_read_virt_blocks()->ocfs2_extent_map_get_blocks()acquires OCFS2_I(orphan_dir_inode)->ip_alloc_sem. Normally orphan directory operations acquireorphan_dir_inode->i_rwsembeforeip_alloc_sem, whereas holdingoi->ip_alloc_semhere acquiresip_alloc_sembeforeorphan_dir_inode->i_rwsem(and then nests a secondip_alloc_semof the same lockdep class). Notice thatocfs2_dio_end_io_write()calledocfs2_del_inode_from_orphan()afterup_write(&oi->ip_alloc_sem)without re-takingip_alloc_sem.
I will follow commit d647c5b2fbf81.
Quoted 42 lines
Quoted 20 lines
fs/ocfs2/aops.c +20
-0+ocfs2_inode_unlock (inode,1 ); +brelse (di_bh); + +out: +ocfs2_run_deallocs (osb, &dealloc); +return ret; +} + +static int ocfs2_dio_write_end_io (struct kiocb *iocb,ssize_t size, +int error,unsigned int flags,int level) +{ +struct inode *inode =file_inode (iocb->ki_filp); +loff_t offset = iocb->ki_pos; +int ret =0 ; + +if (error) +mlog_ratelimited (ML_ERROR ,"Direct IO failed, bytes = %lld errno:%d" , + (long long )size, error); + + if (size && ((flags & IOMAP_DIO_UNWRITTEN) ||fs/ocfs2/aops.c static int ocfs2_dio_end_io_r_pr(struct kiocb *iocb, ssize_t size, · line 3039
Quoted 10 lines
fs/ocfs2/aops.c +10
-0+ (offset + size > i_size_read(inode)))) { + ret =ocfs2_iomap_dio_end_io_write (inode, offset, size); +if (ret <0 ) +mlog_errno (ret); + } + + error =ocfs2_iomap_dio_end_io (iocb, size, error, level); + +return error; +}[Severity: Critical]
What happens inocfs2_dio_write_end_io()when a write bio fails or whenocfs2_iomap_dio_end_io_write()returns an error?In
iomap_dio_bio_iter(),dio->sizeis incremented as bios are submitted, so when a bio fails on completion,iomap_dio_complete()callsend_iowith size > 0 and error < 0. Becauseocfs2_dio_write_end_io()does not check !error before callingocfs2_iomap_dio_end_io_write(), a failed write will still markOCFS2_EXT_UNWRITTENextents as written and extendi_size, exposing stale uninitialized disk blocks to userspace.
Quoted 3 lines
Conversely, when error == 0 and
ocfs2_iomap_dio_end_io_write()fails with ret < 0, ret is logged withmlog_errno(ret)but never propagated to error, soocfs2_dio_write_end_io()returns 0 and the write syscall reports success.
I will follow ocfs2_dio_end_io() code logic to change the error handling of ocfs2_dio_write_end_io() , return 'ret' not input parameter 'error' to caller.
Quoted 4 lines
Also, inside
ocfs2_iomap_dio_end_io_write(), if the while loop breaks out with ret < 0, execution falls through to:
Quoted 8 lines
if (end > i_size_read(inode)) { ret = ocfs2_set_inode_size(handle, inode, di_bh, end); if (ret < 0) mlog_errno(ret); }which extends
i_sizeover unconverted extents, overwrites the negative ret with 0, and removes the inode from the orphan directory.
will change the code base on commit d647c5b2fbf81
Quoted 32 lines
Quoted 17 lines
fs/ocfs2/buffer_head_io.c +2
-0@@ -334,6 +334,8 @@ int ocfs2_read_blocks(struct ocfs2_caching_info *ci, u64 block, int nr,wait_on_buffer (bh);put_bh (bh); bhs[i] =NULL ; + }else if (bh &&buffer_uptodate (bh)) { +clear_buffer_uptodate (bh); }continue ; }@@ -362,11 +364,8 @@ int ocfs2_read_blocks(struct ocfs2_caching_info *ci, u64 block, int nr,BUG_ON (buffer_jbd (bh));clear_buffer_needs_validate (bh);fs/ocfs2/buffer_head_io.c int ocfs2_read_blocks(struct ocfs2_caching_info *ci, u64 block, int nr, · line 371
Quoted 10 lines
fs/ocfs2/buffer_head_io.c +1
-4status =validate (sb, bh); -if (status) { -if (buffer_uptodate (bh)) -clear_buffer_uptodate (bh); +if (status) goto read_failure; - } } }[Severity: High]
Was this change toocfs2_read_blocks()infs/ocfs2/buffer_head_io.cincluded by mistake?
Quoted 6 lines
It appears to revert commit
6371a07148ee("ocfs2: fix buffer head management inocfs2_read_blocks()"). Whennew_bhis true andvalidate(sb, bh)fails, jumping toread_failurewithout clearingBH_Uptodateenters the if (new_bh&& bh) branch and skips the else-if branch, leaving the failed buffer head markedBH_UptodatewithBH_NeedsValidatecleared in the block device cache.
This is accident revert operation. I will restore the commit 6371a07148ee code.
Quoted 35 lines
Quoted 5 lines
fs/ocfs2/file.c +0
-0[ ... ]
Quoted 11 lines
fs/ocfs2/file.c +5
-2@@ -2489,26 +2497,78 @@ static ssize_t ocfs2_file_write_iter(struct kiocb *iocb, saved_ki_complete =xchg (&iocb->ki_complete,NULL ); } -/* communicate with ocfs2_dio_end_io */ -ocfs2_iocb_set_rw_locked (iocb, rw_level); +if (direct_io &&ocfs2_should_use_dio (iocb, from, inode)) { +if (ocfs2_clusters_for_bytes (inode->i_sb, iocb->ki_pos + count) > +ocfs2_clusters_for_bytes (inode->i_sb,i_size_read (inode))) { +/* +* when we are going to alloc extents beyond file size, add the fs/ocfs2/file.c static ssize_t ocfs2_file_write_iter(struct kiocb *iocb, · line 2514
Quoted 10 lines
fs/ocfs2/file.c +10
-0+* inode to orphan dir, so we can recall those spaces when +* system crashed during write. +*/ + ret =ocfs2_add_inode_to_orphan (osb, inode); +if (ret <0 ) { +mlog_errno (ret); + goto out; + } + dio_flags =IOMAP_DIO_FORCE_WAIT ; + }[Severity: High]
ShouldIOMAP_DIO_FORCE_WAITalso be set when an AIO DIO write extendsi_sizewithin an already-allocated cluster (whereiocb->ki_pos+ count >i_size_read(inode), even ifocfs2_clusters_for_bytes()does not increase)?
Quoted 8 lines
When
s_clustersize>s_blocksize, a block-aligned extending AIO DIO write that stays within the last allocated cluster leavesdio_flagsas 0, so__iomap_dio_rw()submits the bio asynchronously and returns-EIOCBQUEUED.ocfs2_file_write_iter()then dropsinode_unlock(inode)whilei_sizehas not yet been updated on this node (andocfs2_rw_lock()does not serialize same-node writers once the node holds EX). A subsequent write on the same node can then observe the stalei_sizeingeneric_write_checks()orocfs2_dio_zero_eof()and overwrite or zero the in-flight AIO DIO range.
The code logic here matches the buffer_head path. If there's a real bug, we can fix it in a separate patch.
Quoted 9 lines
[Severity: Medium]
Also, in this new iomap DIO write branch inocfs2_file_write_iter(),__generic_file_write_iter()is no longer called, so neitherfile_remove_privs()norfile_update_time()(orkiocb_modified()) is invoked. Sinceocfs2_iomap_dio_end_io_write()only callsocfs2_set_inode_size()when end >i_size_read(inode), won't non-extendingO_DIRECTwrites skip clearing SUID/SGID/file capabilities (when not already handled byocfs2_prepare_inode_for_write()) and never update mtime/ctime?
will add kiocb_modified() in ocfs2_file_write_iter() on next verion.
Quoted 80 lines
Quoted 58 lines
fs/ocfs2/file.c +41
-14+ + ret =ocfs2_dio_zero_eof (inode, iocb->ki_pos, count); +if (ret <0 ) + goto out; - written =__generic_file_write_iter (iocb, from); -/* buffered aio wouldn't have proper lock coverage today */ -BUG_ON (written == -EIOCBQUEUED && !direct_io); +const struct iomap_dio_ops *dops = rw_level ? + &ocfs2_iomap_dio_ops_w_ex : &ocfs2_iomap_dio_ops_w_pr; +struct iomap_dio *dio; -/* -* deep in g_f_a_w_n()->ocfs2_direct_IO we pass in a ocfs2_dio_end_io -* function pointer which is called when o_direct io completes so that -* it can unlock our rw lock. -* Unfortunately there are error cases which call end_io and others -* that don't. so we don't have to unlock the rw_lock if either an -* async dio is going to do it in the future or an end_io after an -* error has already done it. -*/ -if ((written == -EIOCBQUEUED ) || (!ocfs2_iocb_is_rw_locked (iocb))) { - rw_level =-1 ; + dio =__iomap_dio_rw (iocb, from, &ocfs2_iomap_ops, + dops, dio_flags,NULL ,0 ); +if (dio ==NULL ) { +/* No I/O issued; rw_lock still held. */ + written =0 ; + }else if (IS_ERR (dio)) { + written =PTR_ERR (dio); +/* +* -EIOCBQUEUED: async I/O; end_io will drop the lock on +* completion. Any other error means no I/O started and +* end_io was not called, so the cleanup path below drops +* the lock. +*/ +if (written == -EIOCBQUEUED ) + rw_level =-1 ; + }else { +/* +* Synchronous completion. iomap_dio_complete() runs +* end_io, which drops the lock unless the mapping +* bounced us back to buffered I/O (returns 0, lock +* retained). +*/ + written =iomap_dio_complete (dio); +if (written !=0 ) + rw_level =-1 ; + } +/* +* iomap_dio_rw() returns -ENOTBLK when it could not invalidate +* the page cache for the DIO range (e.g. racing buffered/mmap +* I/O on the same file, as exercised by generic/095). Fall back +* to a buffered write for the remaining data, the same way +* ext4/xfs do, instead of leaking -ENOTBLK to userspace. +*/ +if (written == -ENOTBLK )fs/ocfs2/file.c static ssize_t ocfs2_file_write_iter(struct kiocb *iocb, · line 2567
Quoted 10 lines
fs/ocfs2/file.c +9
-0+ written =0 ; +if (written >=0 &&iov_iter_count (from)) { + iocb->ki_flags &= ~IOCB_DIRECT ; + buffered =__generic_file_write_iter (iocb, from); + written =direct_write_fallback (iocb, from, written, buffered); + } + }else { + iocb->ki_flags &= ~IOCB_DIRECT ; + written =__generic_file_write_iter (iocb, from); }[Severity: High]
Does this orphan handling inocfs2_file_write_iter()leak the orphan directory entry (and any blocks allocated beyondi_size) on error or fallback paths?
ocfs2_add_inode_to_orphan()setsOCFS2_DIO_ORPHANED_FLon disk beforeocfs2_dio_zero_eof()and__iomap_dio_rw(), butocfs2_del_inode_from_orphan()is only called fromocfs2_iomap_dio_end_io_write()when:if (size && ((flags & IOMAP_DIO_UNWRITTEN) || (offset + size > i_size_read(inode))))
Quoted 8 lines
If
ocfs2_dio_zero_eof()fails, if__iomap_dio_rw()fails before runningend_io(such as-ENOTBLKfalling back to buffered I/O,-EAGAIN, or-ENOMEM), ifend_ioruns with size == 0 (such as-EINVALor-EFAULTafterocfs2_dio_wr_map_blocks()already allocated blocks beyondi_size), or if a short DIO write completes without exceedingi_sizeor settingIOMAP_DIO_UNWRITTEN,ocfs2_del_inode_from_orphan()is never called. LeavingOCFS2_DIO_ORPHANED_FLset on disk also causesocfs2_delete_inode()to skip wiping the inode when the file is later unlinked.
If we implement the .submit_io and .end_io methods, orphan handling would effectively mirror the buffer_head path, which might make this issue go away entirely.
Quoted 21 lines
[Severity: High]
Are there also locking and sync issues when falling back to buffered I/O inocfs2_file_write_iter()?1. If
iomap_dio_complete(dio)returns a partial write (written > 0) withiov_iter_count(from)> 0,ocfs2_dio_write_end_io()has already droppedocfs2_rw_unlock()andrw_levelis set to -1, so the subsequent__generic_file_write_iter(iocb, from)runs without holdingocfs2_rw_lockat all. And on a-ENOTBLKfallback (written == 0) for an overwrite under coherency=buffered,rw_levelis 0 (PR), so the buffered write runs under a sharedrw_lockinstead of EX.2. In the else branch (when
ocfs2_should_use_dio()returns false), clearingiocb->ki_flags&= ~IOCB_DIRECT before calling__generic_file_write_iter()prevents__generic_file_write_iter()from callingdirect_write_fallback()to write back and invalidate the page cache.3. In both fallback paths,
IOCB_DIRECTis cleared oniocb->ki_flagswhile the local variabledirect_ioremains 1, so the subsequent sync check:
Quoted 6 lines
if (((file->f_flags & O_DSYNC) && !direct_io) || IS_SYNC(inode)) {skips
jbd2_journal_force_commit()(andfilemap_fdatawrite_range()/filemap_fdatawait_range()on the !ocfs2_should_use_dio() path) forO_DSYNCwrites that fell back to buffered I/O.
will use generic_perform_write() to replace __generic_file_write_iter(), and don't clear IOCB_DIRECT in iocb->ki_flags.
Implementing the .submit_io and .end_io methods will take some time, but I hope to finish it this week.
Thanks,
Heming
Quoted 10 lines
Quoted 7 lines
fs/ocfs2/file.c +3
-0+/* buffered aio wouldn't have proper lock coverage today */ +BUG_ON (written == -EIOCBQUEUED && !direct_io); +if (unlikely (written <=0 )) goto out;[ ... ]
Heming Zhao
reviewerRFC PATCH v3 4/4 ocfs2: remove legacy blockdev direct-IO path APIs
In reply to Heming ZhaoSince the DIO path already migrated from buffer_head mode (by using a_ops->direct_IO method) to iomap mode, the VFS no longer sets FMODE_CAN_ODIRECT automatically, so ocfs2_file_open() sets it explicitly.
The now-unused blockdev get_block/end_io machinery (ocfs2_lock_get_block(), ocfs2_dio_wr_get_block(), ocfs2_dio_end_io(), ocfs2_dio_end_io_write(), and the ocfs2_dio_write_ctxt helpers) is removed, and OCFS2_FS Kconfig removes LEGACY_DIRECT_IO.
--- fs/ocfs2/Kconfig | 1 - fs/ocfs2/aops.c | 411 ----------------------------------------------- fs/ocfs2/aops.h | 35 +--- fs/ocfs2/file.c | 15 +- fs/ocfs2/ocfs2.h | 5 - 5 files changed, 18 insertions(+), 449 deletions(-)
fs/ocfs2/Kconfig +0 -1
@@ -9,7 +9,6 @@ config OCFS2_FS select QUOTA_TREE select FS_IOMAP select FS_POSIX_ACL - select LEGACY_DIRECT_IO help OCFS2 is a general purpose extent based shared disk cluster file system with many similarities to ext3. It supports 64 bit inode
fs/ocfs2/aops.c +0 -411
441 lines not on this page · raw message
fs/ocfs2/aops.h +6 -29
@@ -7,6 +7,12 @@#define OCFS2_AOPS_H #include <linux/fs.h> +#include <linux/iomap.h> + +extern const struct iomap_ops ocfs2_iomap_ops; +extern const struct iomap_dio_ops ocfs2_iomap_dio_ops_r_pr; +extern const struct iomap_dio_ops ocfs2_iomap_dio_ops_w_pr; +extern const struct iomap_dio_ops ocfs2_iomap_dio_ops_w_ex; int ocfs2_map_folio_blocks(struct folio *folio, u64 *p_blkno, struct inode *inode, unsigned int from,
@@ -45,34 +51,5 @@ int ocfs2_get_block(struct inode *inode, sector_t iblock,int ocfs2_dio_zero_eof (struct inode *inode,loff_t pos,size_t count);int ocfs2_map_blocks (struct inode *inode,struct ocfs2_map_block *map,int flags); -/* all ocfs2_dio_end_io()'s fault */ -#define ocfs2_iocb_is_rw_locked (iocb) \ - test_bit(0, (unsigned long *)&iocb->private) -static inline void ocfs2_iocb_set_rw_locked (struct kiocb *iocb,int level) -{ -set_bit (0 , (unsigned long *)&iocb->private); -if (level) -set_bit (1 , (unsigned long *)&iocb->private); -else -clear_bit (1 , (unsigned long *)&iocb->private); -} - -/* -* Using a named enum representing lock types in terms of #N bit stored in -* iocb->private, which is going to be used for communication between -* ocfs2_dio_end_io() and ocfs2_file_write/read_iter(). -*/ -enum ocfs2_iocb_lock_bits { -OCFS2_IOCB_RW_LOCK =0 , -OCFS2_IOCB_RW_LOCK_LEVEL , -OCFS2_IOCB_NUM_LOCKS -}; - -#define ocfs2_iocb_init_rw_locked (iocb) \ - (iocb->private = NULL) -#define ocfs2_iocb_clear_rw_locked (iocb) \ - clear_bit(OCFS2_IOCB_RW_LOCK, (unsigned long *)&iocb->private) -#define ocfs2_iocb_rw_locked_level (iocb) \ - test_bit(OCFS2_IOCB_RW_LOCK_LEVEL, (unsigned long *)&iocb->private)#endif /* OCFS2_FILE_H */
fs/ocfs2/file.c +12 -3
@@ -129,6 +129,15 @@ static int ocfs2_file_open(struct inode *inode, struct file *file) file->f_mode |=FMODE_NOWAIT ; +/* +* Direct I/O is served through iomap_dio_rw() from +* ocfs2_file_{read,write}_iter() rather than an a_ops->direct_IO +* method, so advertise O_DIRECT capability explicitly here. +*/ +if (!(OCFS2_I (inode)->ip_dyn_features &OCFS2_INLINE_DATA_FL )) + file->f_mode |=FMODE_CAN_ODIRECT ; + + leave:return status; }
@@ -1171,9 +1180,9 @@ int ocfs2_setattr(struct mnt_idmap *idmap, struct dentry *dentry, size_change =S_ISREG (inode->i_mode) && attr->ia_valid &ATTR_SIZE ;if (size_change) {/* -* Here we should wait dio to finish before inode lock -* to avoid a deadlock between ocfs2_setattr() and -* ocfs2_dio_end_io_write() +* Here we should wait for in-flight direct I/O to finish +* before taking the inode lock, to avoid a deadlock between +* ocfs2_setattr() and direct I/O completion. */ inode_dio_wait (inode);
fs/ocfs2/ocfs2.h +0 -5
@@ -549,11 +549,6 @@ struct ocfs2_map_block {unsigned int flags; }; -extern const struct iomap_ops ocfs2_iomap_ops; -extern const struct iomap_dio_ops ocfs2_iomap_dio_ops_r_pr; -extern const struct iomap_dio_ops ocfs2_iomap_dio_ops_w_pr; -extern const struct iomap_dio_ops ocfs2_iomap_dio_ops_w_ex; -/* Flags used by ocfs2_map_blocks() */ #define OCFS2_GET_BLOCKS_CREATE (0x0001)
Signature · 1 line
2.55.0
A message that is not in the archive (sent off-list or not yet received): [email protected]
Heming Zhao
reviewerRe: [RFC PATCH v3 4/4] ocfs2: remove legacy blockdev direct-IO path APIs
Quoted 33 lines · On 10/18/26 22:44, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] ocfs2:ocfs2_file_open()skipsFMODE_CAN_ODIRECTon inline-data inodes
- [Low] ocfs2: unusedOCFS2_DIO_MARK_EXTENT_BATCHmacro infs/ocfs2/aops.c--commit
c44e1e979977c124de60573ae7e8ca56edb710ca
Author: Heming Zhao <[email protected]>ocfs2: remove legacy blockdev direct-IO path APIs
This commit removes the legacy
buffer_headdirect-IO callbacks andKconfigselection from ocfs2, and explicitly setsFMODE_CAN_ODIRECTinocfs2_file_open()now that direct I/O is handled via iomap.Quoted 4 lines
fs/ocfs2/aops.c +0
-0[ ... ]
Quoted 9 lines
fs/ocfs2/aops.c +0
-5@@ -2533,205 +2322,6 @@ static int ocfs2_dio_wr_map_blocks(struct inode *inode,return ret; } -static int ocfs2_dio_end_io_write (struct inode *inode, -struct ocfs2_dio_write_ctxt *dwc, -loff_t offset, -ssize_t bytes) -{[ ... ]
Quoted 11 lines
fs/ocfs2/aops.c static int ocfs2_dio_wr_map_blocks(struct inode *inode, · line 2623
Quoted 6 lines
fs/ocfs2/aops.c +0
-6-if (++batch ==OCFS2_DIO_MARK_EXTENT_BATCH ) { -ocfs2_commit_trans (osb, handle); - handle =NULL ; - batch =0 ; - } - }[Severity: Low]
Withocfs2_dio_end_io_write()removed, is theOCFS2_DIO_MARK_EXTENT_BATCHmacro definition earlier infs/ocfs2/aops.cnow unused, and should it be removed as well?
I will follow the same handling as in commit d647c5b2fbf81 by using OCFS2_DIO_MARK_EXTENT_BATCH in ocfs2_iomap_dio_end_io_write().
Quoted 63 lines
[ ... ]
Quoted 10 lines
fs/ocfs2/file.c +2
-0@@ -129,6 +129,15 @@ static int ocfs2_file_open(struct inode *inode, struct file *file) file->f_mode |=FMODE_NOWAIT ; +/* +* Direct I/O is served through iomap_dio_rw() from fs/ocfs2/file.c static int ocfs2_file_open(struct inode *inode, struct file *file) · line 143
Quoted 10 lines
fs/ocfs2/file.c +7
-0+* ocfs2_file_{read,write}_iter() rather than an a_ops->direct_IO +* method, so advertise O_DIRECT capability explicitly here. +*/ +if (!(OCFS2_I (inode)->ip_dyn_features &OCFS2_INLINE_DATA_FL )) + file->f_mode |=FMODE_CAN_ODIRECT ; + + leave:return status; }[Severity: High]
Shouldocfs2_file_open()setFMODE_CAN_ODIRECTunconditionally here rather than skipping it whenOCFS2_INLINE_DATA_FLis set?Previously,
ocfs2_aopshad .direct_IO set, sodo_dentry_open()always setFMODE_CAN_ODIRECTon regular files regardless ofOCFS2_INLINE_DATA_FL. The read and write iterators checkocfs2_should_use_dio()under the cluster lock and fall back to buffered I/O when the file has inline data:fs/ocfs2/file.c:ocfs2_should_use_dio() { if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) return false; }Skipping
FMODE_CAN_ODIRECTinocfs2_file_open()appears to cause several regressions on filesystems with inline-data enabled:1. Opening an existing inline-data file with
O_DIRECTnow fails with-EINVALindo_dentry_open()instead of succeeding and falling back to buffered I/O (or converting to extents on write).2.
OCFS2_INLINE_DATA_FLis mutable and is cleared dynamically inocfs2_convert_inline_data_to_extents()when a file grows beyond inline capacity. If an inline-data file is opened withoutO_DIRECTand later converted to extents,file->f_modestill lacksFMODE_CAN_ODIRECT, so a subsequentfcntl(fd, F_SETFL, O_DIRECT)insetfl()orRWF_DIRECTI/O will fail with-EINVAL.3.
ocfs2_file_open()readsip_dyn_featureswithout holdingoi->ip_lockor the cluster lock (ocfs2_inode_lock()), so a node with a stale cachedOCFS2_INLINE_DATA_FLflag can rejectopen(..., O_DIRECT)with-EINVALeven after another node has already converted the inode to extents.4. Earlier in
ocfs2_file_open(),OCFS2_INODE_OPEN_DIRECTis set beforedo_dentry_open()checksFMODE_CAN_ODIRECT:if (mode &
O_DIRECT)
oi->ip_flags|=OCFS2_INODE_OPEN_DIRECT;
Quoted 7 lines
If an inline-data file already has another open fd (
oi->ip_open_count> 0) when anO_DIRECTopen fails indo_dentry_open(),ocfs2_file_release()will not clearOCFS2_INODE_OPEN_DIRECTbecause --oi->ip_open_count is still non-zero.[ ... ]
I prefer the style of setting FMODE_CAN_ODIRECT unconditionally. This is also v2 code style.
Thanks,
Heming
Heming Zhao
reviewerHello list,
When using the format command "mkfs.ocfs2 -b 2K -C 8K --fs-features=local \
--fs-feature-level=max-features /dev/<disk>" to run xfstests,
I encountered some new hang issues. These issues are from the iomap write path. I will withdraw the v3 patch, and after fixing all the issues, I will send v4.
Thanks,
Heming
Quoted 188 lines · Heming Zhao
This patch set migrates ocfs2 Direct I/O (DIO) from
buffer_headmode to iomap mode. And the code base is commit58717b2a1365d.Changelog:
v3:
patch 1/4
revise the commit log
forocfs2_map_blocks():
- removedS_ISLNK()handling, asS_ISLNKinodes are already handled by the existing code path:
ocfs2_read_folio=>block_read_full_folio()=>get_block(ocfs2_get_block)
- replace "return-EIO" with "return err" at the end of this funcpatch 2/4
revise the commit log based on hch's comments.
inocfs2_iomap_begin(), replace the "/* todo */" comment under theIOMAP_WRITEcase with "return-EOPNOTSUPP". revise comments inocfs2_iomap_dio_end_io()
remove the unused "bail" label inocfs2_dio_end_io_r_pr()patch 3/4
revise the commit log based on hch's comments.
remove the unused varmax_blocksinocfs2_iomap_dio_end_io_write().
followed Joseph's comment, directly set map.lblk inocfs2_iomap_dio_end_io_write().
inocfs2_dio_wr_map_blocks():
- replaceBUG_ON(p_blkno == 0)with "ret =-EIO,mlog_errno()and goto unlock".
(the legacy code was copied fromocfs2_dio_wr_get_block())
- adjust arithmetic formap->lenby substracting the offset and usingmin_t()to cap the length within limits.
- adjust error handling style for the return value ofocfs2_write_end_nolock().
introduce a new function,ocfs2_dio_zero_eof().patch 4/4
Setf_modewithFMODE_CAN_ODIRECTonly for non-inline files, per Joseph's comments.v2:
- Merge Joseph's code and ideas from [1]:
- AddFMODE_CAN_ODIRECTtof_modeinocfs2_file_open()
- Changeiomap_opsandiomap_dio_opsAPI declarations fromocfs2.htoaops.h
- Switch fromiomap_dio_rwto__iomap_dio_rw+iomap_dio_complete
- Change "structiomap_dio_ops" from read/write style to PR/EX locking style. With this approach, we completely eliminate the need to define anyiocb->ki_flagsbits.
- Remove alignment code fromocfs2_should_use_dio(), because iomap internally already handles this.
- Add patch 4 to remove unused functions and theCONFIG_LEGACY_DIRECT_IOKconfigoption.
- Add "Co-developed-by: Joseph Qi <[email protected]>"[1]:
https://gitee.com/anolis/storage-cloud-kernel/commit/ea3fe75e2060118ba093e90b1cd34fa382914b29v1:
https://lore.kernel.org/ocfs2-devel/[email protected]/
v2:
https://lore.kernel.org/ocfs2-devel/[email protected]/--------------
# test reportThis patch set has been tested on 7.3.0-rc7 kernel with the following test cases.
## ltp
runltp -f dio -d
/mnt/ocfs2/The test results: passed
## xfstests
format cmd: mkfs.ocfs2 -M local --fs-feature-level=max-features
/dev/vde./check -g quick -T -b -s ocfs2 -e generic/081 \
(quoted diff) +0
-3-e generic/266 -e generic/272 -e generic/281 \ -e generic/338 -e generic/347 -e generic/361 \ -e generic/628 -e generic/629 -e generic/648 -e generic/650The test results are the same before and after the patch set.
Failures: generic/003 generic/091 generic/094 generic/177 generic/211 generic/225 generic/263 generic/322 generic/329 generic/331 generic/370 generic/376 generic/424 generic/448 generic/467 generic/477 generic/481 generic/510 generic/513 generic/537 generic/552 generic/563 generic/578 generic/607 generic/620 generic/630 generic/755 generic/760 generic/777 generic/784
Failed 30 of 631 tests(following is 7.2.0 kernel test result)
Failures: generic/003 generic/322 generic/329 generic/331 generic/370 generic/376 generic/420 generic/424 generic/448 generic/449 generic/467 generic/477 generic/510 generic/513 generic/537 generic/552 generic/563 generic/578 generic/607 generic/620 generic/630 generic/755 generic/777 generic/784
Failed 24 of 632 tests## fio on single node
(this is v2 test result, v3 test result is similar)
fio --direct=1 --iodepth=32 --rw=write --ioengine=libaio --bs=128k --size=1g \
--numjobs=4 --runtime=100 --group_reporting --filename=/mnt/ocfs2/write.1g --name=tst
- before patch : write: IOPS=18.8k, BW=4708MiB/s (4937MB/s)(4096MiB/870msec)
- applied patch: write: IOPS=39.6k, BW=4953MiB/s (5193MB/s)(4096MiB/827msec)
- overwrite (don't remove write.1g, run above fio command) speed seems same.fio --direct=1 --iodepth=32 --rw=read --ioengine=libaio --bs=128k --size=1g \
--numjobs=4 --runtime=100 --group_reporting --filename=/mnt/ocfs2/write.1g --name=tst
- before patch : read: IOPS=52.4k, BW=6554MiB/s (6872MB/s)(4096MiB/625msec)
- applied patch: read: IOPS=52.2k, BW=6522MiB/s (6839MB/s)(4096MiB/628msec)fio --direct=1 --iodepth=32 --rw=randwrite --ioengine=libaio --bs=4k --size=1g \
--numjobs=4 --runtime=100 --group_reporting --filename=/mnt/ocfs2/write.1g --name=tst
- before patch
- creating write: IOPS=20.9k, BW=81.7MiB/s (85.6MB/s)(4096MiB/50159msec)
- overwriting write.1g: write: IOPS=281k, BW=1097MiB/s (1151MB/s)(4096MiB/3733msec)
- applied patch
- creating write: IOPS=21.0k, BW=82.0MiB/s (86.0MB/s)(4096MiB/49938msec)
- overwriting write.1g: write: IOPS=329k, BW=1286MiB/s (1348MB/s)(4096MiB/3185msec)fio --direct=1 --iodepth=32 --rw=randread --ioengine=libaio --bs=4k --size=1g \
--numjobs=4 --runtime=100 --group_reporting --filename=/mnt/ocfs2/write.1g --name=tst
- before patch : read: IOPS=372k, BW=1452MiB/s (1522MB/s)(4096MiB/2821msec)
- applied patch: read: IOPS=369k, BW=1441MiB/s (1511MB/s)(4096MiB/2842msec)## fio on two nodes
### write in different folder
node1
/root/fio/diff/ocfs2-w.sh:Quoted 2 lines
fio --direct=1 --iodepth=32 --rw=write --ioengine=libaio --bs=128k --size=2g \
--numjobs=2 --runtime=100 --group_reporting --filename=/mnt/ocfs2/node1/write.2g --name=tstnode2
/root/fio/diff/ocfs2-w.sh:Quoted 2 lines
fio --direct=1 --iodepth=32 --rw=write --ioengine=libaio --bs=128k --size=2g \
--numjobs=2 --runtime=100 --group_reporting --filename=/mnt/ocfs2/node2/write.2g --name=tstnode1
/root/fio/diff/ocfs2-randw.sh:Quoted 2 lines
fio --direct=1 --iodepth=32 --rw=randwrite --ioengine=libaio --bs=4k --size=2g \
--numjobs=2 --runtime=100 --group_reporting --filename=/mnt/ocfs2/node1/write.2g --name=tstnode2
/root/fio/diff/ocfs2-randw.sh:Quoted 2 lines
fio --direct=1 --iodepth=32 --rw=randwrite --ioengine=libaio --bs=4k --size=2g \
--numjobs=2 --runtime=100 --group_reporting --filename=/mnt/ocfs2/node2/write.2g --name=tstrun on both node,
pdsh -w ssh:tw2510-[1-2]/root/fio/diff/ocfs2-w.sh
- before patch : write: IOPS=19.3k, BW=2417MiB/s (2534MB/s)(4096MiB/1695msec)
- applied patch: write: IOPS=20.8k, BW=2599MiB/s (2725MB/s)(4096MiB/1576msec) pdsh -w ssh:tw2510-[1-2]/root/fio/diff/ocfs2-randw.sh
- before patch : write: IOPS=3742, BW=14.6MiB/s (15.3MB/s)(1462MiB/100007msec)
- applied patch: write: IOPS=3765, BW=14.7MiB/s (15.4MB/s)(1471MiB/100010msec)### write in same file
node1 & node2: (
/root/fio/share/ocfs2-w.sh)
fio --direct=1 --iodepth=32 --rw=write --ioengine=libaio --bs=128k --size=2g \
--numjobs=2 --runtime=100 --group_reporting --filename=/mnt/ocfs2/share/write.2g --name=tstnode1 & node2: (
/root/fio/share/ocfs2-randw.sh)
fio --direct=1 --iodepth=32 --rw=randwrite --ioengine=libaio --bs=4k --size=2g \
--numjobs=2 --runtime=100 --group_reporting --filename=/mnt/ocfs2/share/write.2g --name=tstrun on both node
pdsh -w ssh:tw2510-[1-2]/root/fio/share/ocfs2-w.sh
- before patch : write: IOPS=2052, BW=257MiB/s (269MB/s)(4096MiB/15967msec)
- applied patch: write: IOPS=2558, BW=320MiB/s (335MB/s)(4096MiB/12808msec) pdsh -w ssh:tw2510-[1-2]/root/fio/share/ocfs2-randw.sh
- before patch : write: IOPS=3588, BW=14.0MiB/s (14.7MB/s)(1402MiB/100001msec)
- applied patch: write: IOPS=3590, BW=14.0MiB/s (14.7MB/s)(1403MiB/100001msec)Heming Zhao (4):
ocfs2: Add newocfs2_map_blocks()to introduce iomap feature
ocfs2: switch DIO read path frombuffer_headto iomap
ocfs2: switch dio write path frombuffer_headto iomap
ocfs2: remove legacy blockdev direct-IO path APIsfs/ocfs2/Kconfig | 2 +- fs/ocfs2/aops.c | 689 ++++++++++++++++++++++++-------------- fs/ocfs2/aops.h | 38 +-- fs/ocfs2/buffer_head_io.c | 19 +- fs/ocfs2/file.c | 168 ++++++++-- fs/ocfs2/ocfs2.h | 45 ++- fs/ocfs2/ocfs2_fs.h | 3 + 7 files changed, 634 insertions(+), 330 deletions(-)