migrate ocfs2 DIO from buffer_head to iomap
Heming Zhao
reviewerv6 cover migrate 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 6628e7d4c6b2 (v7.4-rc1), and also merge Joseph's code and ideas from [1]:
V5 changelog:
- Support allocating multiple clusters at once in iomap_begin.
- Use iocb as a token to detect the unwritten extent list between iomap_begin and end_io.
- Squash patch [3/4] and [4/4] from v5 into a single patch (since v5 patch [4/4] consists mostly of code deletions).
[1]:
https://gitee.com/anolis/storage-cloud-kernel/commit/ea3fe75e2060118ba093e90b1cd34fa382914b29
[2]:
https://sashiko.dev/#/patchset/20261103135605.22943-1-heming.zhao%40suse.com
v1 patch set:
https://lore.kernel.org/ocfs2-devel/[email protected]/
v2 patch set:
https://lore.kernel.org/ocfs2-devel/[email protected]/
v3 patch set:
https://lore.kernel.org/ocfs2-devel/[email protected]/
v3 sashiko review:
https://sashiko.dev/#/patchset/20261018143319.22329-1-heming.zhao%40suse.com
v4 patch set:
https://lore.kernel.org/ocfs2-devel/[email protected]/
v4 sashiko review:
https://sashiko.dev/#/patchset/20261103135605.22943-1-heming.zhao%40suse.com
v5 patch set:
https://lore.kernel.org/ocfs2-devel/[email protected]/
v5 sashiko review:
https://sashiko.dev/#/patchset/20261105071930.16399-1-heming.zhao%40suse.com
--------------
# test report
This patch set has been tested on 7.4-rc1 kernel with the following test cases.
## ltp
runltp -f dio -d /mnt/ocfs2/
The failure test results:
(These failures are all minor issues. I'd like to check Sashiko's review comments first before fixing them.)
diotest4 3 TFAIL : diotest4.c:112: read allows odd count. returns 1: Success diotest4 4 TFAIL : diotest4.c:127: write allows odd count.returns 1: Success diotest4 5 TFAIL : diotest4.c:179: Odd count of read and write
diotest4 16 TFAIL : diotest4.c:112: read allows nonaligned buf. returns 4096: Success diotest4 17 TFAIL : diotest4.c:127: write allows nonaligned buf.returns 4096: Success diotest4 18 TFAIL : diotest4.c:179: read, write with non-aligned buffer
## xfstests
./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
OCFS2_MKFS_LOCAL_OPT="-b 2K -C 4K --fs-features=local --fs-feature-level=max-features"
Failures: generic/003 generic/056 generic/091 generic/094 generic/177 generic/211 generic/225 generic/263 generic/321 generic/322 generic/329 generic/331 generic/370 generic/376 generic/410 generic/424 generic/448 generic/467 generic/477 generic/481 generic/498 generic/510 generic/512 generic/513 generic/534 generic/537 generic/552 generic/557 generic/563 generic/578 generic/588 generic/607 generic/620 generic/630 generic/755 generic/760 generic/771 generic/777 generic/779 generic/784
Failed 40 of 632 tests
OCFS2_MKFS_LOCAL_OPT="-b 4K -C 4K --fs-features=local --fs-feature-level=max-features"
Failures: generic/003 generic/056 generic/177 generic/211 generic/321 generic/322 generic/329 generic/331 generic/370 generic/376 generic/420 generic/424 generic/448 generic/467 generic/477 generic/481 generic/498 generic/510 generic/512 generic/513 generic/534 generic/537 generic/552 generic/557 generic/563 generic/578 generic/588 generic/607 generic/620 generic/630 generic/755 generic/771 generic/777 generic/779 generic/784 Failed 35 of 632 tests
OCFS2_MKFS_LOCAL_OPT="-b 2K -C 8K --fs-features=local --fs-feature-level=max-features"
Failures: generic/003 generic/056 generic/070 generic/075 generic/076 generic/091 generic/094 generic/112 generic/177 generic/211 generic/225 generic/263 generic/319 generic/321 generic/322 generic/329 generic/331 generic/370 generic/376 generic/409 generic/410 generic/424 generic/448 generic/467 generic/477 generic/481 generic/498 generic/510 generic/512 generic/513 generic/534 generic/537 generic/552 generic/557 generic/563 generic/564 generic/578 generic/588 generic/607 generic/620 generic/630 generic/755 generic/759 generic/760 generic/771 generic/777 generic/779 generic/784 ocfs2/001
Failed 49 of 632 tests
## fio on single node
The test results are similar to v5.
---------------------
Heming Zhao (3):
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
fs/ocfs2/Kconfig | 2 +- fs/ocfs2/aops.c | 1532 +++++++++++++++++++++++++++++++++++----------- fs/ocfs2/aops.h | 41 +- fs/ocfs2/file.c | 220 ++++++- fs/ocfs2/mmap.c | 2 +- fs/ocfs2/ocfs2.h | 25 +- 6 files changed, 1404 insertions(+), 418 deletions(-)
Signature · 1 line
2.55.0
Heming Zhao
reviewerRFC PATCH v6 1/3 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 | 72 ++++++++++++++++++++++++++++++++++++++++++++++++ fs/ocfs2/aops.h | 2 ++ fs/ocfs2/ocfs2.h | 25 ++++++++++++++++- 3 files changed, 98 insertions(+), 1 deletion(-)
fs/ocfs2/aops.c +72 -0
@@ -126,6 +126,78 @@ static int ocfs2_lock_get_block(struct inode *inode, sector_t iblock,return ret; } +/* ocfs2 never allocates in this function */ +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; + +/* +* If we see this on a sparse file system, then a truncate has +* raced us and removed the cluster. We treat this as a hole +* and skip setting the iomap flags. The caller will set +* iomap->type to IOMAP_HOLE. +*/ +if (create && map->pblk ==0 &&ocfs2_sparse_alloc (osb)) + goto bail; + +/* Treat the unwritten extent as a hole for zeroing purposes. */ +if (p_blkno) { +if (ext_flags &OCFS2_EXT_UNWRITTEN ) { + map->flags |=OCFS2_MAP_UNWRITTEN ; + }else { + map->flags |=OCFS2_MAP_MAPPED ; + } + } + +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: +return err; +} +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/ocfs2.h +24 -1
@@ -514,7 +514,30 @@ struct ocfs2_superstruct ocfs2_filecheck_sysfs_entry osb_fc_ent; }; -#define OCFS2_SB (sb) ((struct ocfs2_super *)(sb)->s_fs_info) +/* +* Logical to physical block mapping, used by ocfs2_map_blocks() +* +* This structure is used to pass requests into ocfs2_map_blocks() +* as well as to store the information returned by ocfs2_map_blocks(). +* It takes less room on the stack than a struct buffer_head. +*/ +#define OCFS2_MAP_NEW BIT(BH_New) +#define OCFS2_MAP_MAPPED BIT(BH_Mapped) +#define OCFS2_MAP_UNWRITTEN BIT(BH_Unwritten) +#define OCFS2_MAP_FLAGS (OCFS2_MAP_NEW | OCFS2_MAP_MAPPED |\ + OCFS2_MAP_UNWRITTEN) + +struct ocfs2_map_block { +u64 pblk;/* physical block# */ +u64 lblk;/* logical block# */ +u64 len;/* number of block */ +unsigned int flags; +}; + +/* Flags used by ocfs2_map_blocks() */ +#define OCFS2_GET_BLOCKS_CREATE (0x0001) + +#define OCFS2_SB (sb) ((struct ocfs2_super *)(sb)->s_fs_info)/* Useful typedef for passing around journal access functions */ typedef int (*ocfs2_journal_access_func)(handle_t *handle,
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 v6 1/3] ocfs2: Add new ocfs2_map_blocks() to introduce iomap feature
Quoted 45 lines · On 11/15/26 23:53, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] ocfs2: misleading unwritten extent comment inocfs2_map_blocks()--commit
36724870d23a63ee30eadf9855ab6d1cebde1f9f
Author: Heming Zhao <[email protected]>ocfs2: Add new
ocfs2_map_blocks()to introduce iomap featureThis commit adds
ocfs2_map_blocks()along with structocfs2_map_blockand OCFS2_MAP_* flags to map logical blocks to physical blocks for iomap.Quoted 12 lines
fs/ocfs2/aops.c +4
-0@@ -126,6 +126,78 @@ static int ocfs2_lock_get_block(struct inode *inode, sector_t iblock,return ret; } +/* ocfs2 never allocates in this function */ +int ocfs2_map_blocks (struct inode *inode,struct ocfs2_map_block *map, +int flags) +{[ ... ]
Quoted 19 lines
fs/ocfs2/aops.c +19
-0+ map->pblk = p_blkno; + map->len = count; + +/* +* If we see this on a sparse file system, then a truncate has +* raced us and removed the cluster. We treat this as a hole +* and skip setting the iomap flags. The caller will set +* iomap->type to IOMAP_HOLE. +*/ +if (create && map->pblk ==0 &&ocfs2_sparse_alloc (osb)) + goto bail; + +/* Treat the unwritten extent as a hole for zeroing purposes. */ +if (p_blkno) { +if (ext_flags &OCFS2_EXT_UNWRITTEN ) { + map->flags |=OCFS2_MAP_UNWRITTEN ; + }else { + map->flags |=OCFS2_MAP_MAPPED ; + }
Quoted 9 lines
fs/ocfs2/aops.c
Quoted 1 line
fs/ocfs2/aops.c +1
-0+ }[Severity: Low]
This isn't a bug, but the comment "Treat the unwritten extent as a hole for zeroing purposes." appears to be copied over fromocfs2_get_block(), wheremap_bh()is skipped onOCFS2_EXT_UNWRITTENso thebuffer_headlayer sees an unmapped hole. Hereocfs2_map_blocks()recordsmap->pblk=p_blknoand setsOCFS2_MAP_UNWRITTENinstead of treating it as a hole.
will remove this comment line in the next version
Thanks,
Heming
Heming Zhao
reviewerRFC PATCH v6 2/3 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 | 97 ++++++++++++++++++++++++++++++++++++++++++++++-- fs/ocfs2/aops.h | 4 ++ fs/ocfs2/file.c | 60 +++++++++++++++++++++++++----- 4 files changed, 150 insertions(+), 12 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 +94 -3
134 lines not on this page · raw message
fs/ocfs2/aops.h +4 -0
@@ -7,6 +7,10 @@#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; int ocfs2_map_folio_blocks(struct folio *folio, u64 *p_blkno, struct inode *inode, unsigned int from,
fs/ocfs2/file.c +51 -9
95 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 v6 2/3] ocfs2: switch DIO read path from buffer_head to iomap
Quoted 48 lines · On 11/15/26 23:57, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] ocfs2: use of removed structiomap_opsbreaks build inocfs2_file_read_iter()
- [Medium] ocfs2: wrong check ofmap->pblkagainstpast_eofinocfs2_map_blocks()
--commit
f6ba1e8d47250dad525939255c96ed39f19c41f1
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 iomap, addingocfs2_iomap_begin()for block mapping and using the split__iomap_dio_rw()andiomap_dio_complete()APIs to manage clusterrw_locklifetime.Quoted 4 lines
fs/ocfs2/aops.c +0
-0[ ... ]
Quoted 14 lines
fs/ocfs2/aops.c +4
-1@@ -173,8 +190,11 @@ int ocfs2_map_blocks(struct inode *inode, struct ocfs2_map_block *map, } } + past_eof =ocfs2_blocks_for_bytes (inode->i_sb,i_size_read (inode)); +if (!ocfs2_sparse_alloc (osb)) { -if (map->pblk ==0 ) { +/* detected a hole (unallocated extent) in a file */ +if (map->pblk ==0 && map->pblk < past_eof) { err = -EIO ; mlog(ML_ERROR, "iblock = %llu p_blkno = %llu blkno=(%llu)\n",fs/ocfs2/aops.c int ocfs2_map_blocks(struct inode *inode, struct ocfs2_map_block *map, · line 213
Quoted 7 lines
fs/ocfs2/aops.c +0
-2@@ -189,8 +209,6 @@ int ocfs2_map_blocks(struct inode *inode, struct ocfs2_map_block *map, } } - past_eof =ocfs2_blocks_for_bytes (inode->i_sb,i_size_read (inode)); -if (create && (map->lblk >= past_eof)) map->flags |=OCFS2_MAP_NEW ;[Severity: Medium]
Should this checkmap->lblk<past_eofinstead ofmap->pblk<past_eof?
Quoted 6 lines
Since
map->pblk== 0 is already true here,map->pblk<past_eofis true for any non-empty file regardless of the logical offset. Because__iomap_dio_rw()does not clamp iomi.len todio->i_sizebefore iterating, a direct I/O read on a non-sparse filesystem that starts before EOF and spans into unallocated blocks at or past EOF will hit this branch and fail with-EIOinstead of returning a short read.
Hallucinating? The code here is actually "map->lblk < past_eof"
Quoted 13 lines
Quoted 5 lines
fs/ocfs2/aops.c +0
-0@@ -2534,6 +2552,79 @@ static ssize_t ocfs2_direct_IO(struct kiocb *iocb, struct iov_iter *iter) ocfs2_dio_end_io, 0); }[ ... ]
fs/ocfs2/aops.c static ssize_t ocfs2_direct_IO(struct kiocb *iocb, struct iov_iter *iter) · line 2609
Quoted 3 lines
fs/ocfs2/aops.c +3
-0+const struct iomap_ops ocfs2_iomap_ops = { + .iomap_begin = ocfs2_iomap_begin, +};[Severity: High]
Does definingocfs2_iomap_opsas structiomap_opsbreak the build?
Quoted 7 lines
Commit
82f677376a90("iomap: remove ->iomap_begin()/->iomap_end() legacy path") and commit9e8fc16dc6b4("iomap: passiomap_iter_next_fndirectly instead of structiomap_ops") removed structiomap_opsand changed__iomap_dio_rw()to take aniomap_iter_next_fncallback directly. This needsDEFINE_IOMAP_ITER_NEXT()andDECLARE_IOMAP_ITER_NEXT()(and passing the generated callback to__iomap_dio_rw()inocfs2_file_read_iter()).
will align the latest iomap code style in the next version.
Thanks,
Heming
Heming Zhao
reviewerRFC PATCH v6 3/3 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.
The buffer_head based ocfs2_dio_wr_get_block() is replaced by ocfs2_dio_wr_map_blocks(). Instead of one cluster per call, it now hands iomap a mapping of as many clusters as can be described by a single struct iomap: up to 16MB, stopped at the first cluster that is not of the same kind as the first one (hole, unwritten, written).
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).
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().
It is also impossible t pass The dwc to iomap_dio_ops.end_io, the iomap ->end_io callback cannot effectively distinguish between I/Os initiated by different threads concurrently. For example, when multiple DIO write threads operate on the same inode concurrently, the end_io callback without the dwc context may incorrectly delete an orphan inode created by another thread, leading to a crash. The fix is to move the orphan inode deletion from end_io into ocfs2_file_write_iter(). In ocfs2_file_write_iter(), inode_lock() serializes the write threads, and IOMAP_DIO_FORCE_WAIT ensures that the function only returns after the bio completes. This guarantees that i_size has already been updated when ocfs2_dio_del_orphan() is called.
iomap calls ->iomap_begin() once per mapping but ->end_io() once per io, and the latter only gets the kiocb, so the kiocb is used as the identity of the io: ocfs2_file_write_iter() passes it in iomap_iter.private, ocfs2_iomap_next() forwards it to ocfs2_dio_wr_map_blocks(), and each unwritten record stores it in ue_owner with the cluster range it covers.
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/Kconfig | 1 - fs/ocfs2/aops.c | 1445 +++++++++++++++++++++++++++++++++------------- fs/ocfs2/aops.h | 35 +- fs/ocfs2/file.c | 160 ++++- fs/ocfs2/mmap.c | 2 +- 5 files changed, 1197 insertions(+), 446 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 +1052 -393
1,816 lines not on this page · raw message
fs/ocfs2/aops.h +5 -30
@@ -11,6 +11,8 @@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,
@@ -38,7 +40,8 @@ typedef enum {int ocfs2_write_begin_nolock (struct address_space *mapping,loff_t pos,unsigned len,ocfs2_write_type_t type,struct folio **foliop,void **fsdata, -struct buffer_head *di_bh,struct folio *mmap_folio); +struct buffer_head *di_bh,struct folio *mmap_folio, +void *owner);int ocfs2_read_inline_data (struct inode *inode,struct folio *folio,struct buffer_head *di_bh);
@@ -46,36 +49,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_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 +139 -21
224 lines not on this page · raw message
fs/ocfs2/mmap.c +1 -1
@@ -90,7 +90,7 @@ static vm_fault_t __ocfs2_page_mkwrite(struct file *file, len = ((size -1 ) & ~PAGE_MASK ) +1 ; err =ocfs2_write_begin_nolock (mapping, pos, len,OCFS2_WRITE_MMAP , - &locked_folio, &fsdata, di_bh, folio); + &locked_folio, &fsdata, di_bh, folio,NULL );if (err) {if (err != -ENOSPC )mlog_errno (err);
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 v6 3/3] ocfs2: switch dio write path from buffer_head to iomap
Quoted 104 lines · On 11/16/26 00:39, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 9 potential issues to consider:
- [High] ocfs2: build failure from using removed structiomap_opsin__iomap_dio_rw()> - [High] ocfs2: unwritten cluster mapped asIOMAP_MAPPEDinocfs2_unwritten_check()
- [High] ocfs2: unaligned 16MB cap inocfs2_dio_wr_map_blocks()dirties DIO range
- [High] ocfs2: missingIOMAP_DIO_FORCE_WAITon sub-cluster extending async DIO writes
- [High] ocfs2:ocfs2_dio_del_orphan()leaks excess clusters and runs afterrw_unlock
- [High] ocfs2: brokenO_DIRECTalignment and buffered fallback inocfs2_file_write_iter()
- [Medium] ocfs2: quota freed for allocated clusters inocfs2_write_begin_nolock()
- [Medium] ocfs2: off-by-one cluster check on-ENOSPCinocfs2_dio_wr_map_blocks()
- [Medium] ocfs2: blocking and on-disk changes beforeIOMAP_NOWAITcheck in DIO write
--commit
295acb4171def4f98acfe868a1d80047646a8e1a
Author: Heming Zhao <[email protected]>ocfs2: switch dio write path from
buffer_headto iomapThis commit converts the OCFS2 direct I/O write path from legacy
buffer_headhelpers to iomap, replacingocfs2_dio_wr_get_block()with multi-cluster mapping inocfs2_dio_wr_map_blocks()and moving EOF zeroing and orphan handling intoocfs2_file_write_iter().Quoted 4 lines
fs/ocfs2/aops.c +0
-0[ ... ]
Quoted 34 lines
fs/ocfs2/aops.c +10
-14@@ -1380,77 +1716,180 @@ static void ocfs2_set_target_boundaries(struct ocfs2_super *osb,static void ocfs2_unwritten_check (struct inode *inode,struct ocfs2_write_ctxt *wc,struct ocfs2_write_cluster_desc *desc) {struct ocfs2_inode_info *oi =OCFS2_I (inode); -struct ocfs2_unwritten_extent *ue =NULL , *new =NULL ; -int ret =0 ; +struct ocfs2_unwritten_extent *ue;if (!desc->c_needs_zero) -return 0 ; +return ; -retry:spin_lock (&oi->ip_lock); -/* Needs not to zero no metter buffer or direct. The one who is zero -* the cluster is doing zero. And he will clear unwritten after all -* cluster io finished. */ -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; - } + ue =ocfs2_find_unwritten (oi, desc->c_cpos); +/* A hole (c_new is 1) can not be on the list, don't trust it if it is. */ +if (ue && !WARN_ON_ONCE (desc->c_new)) { +/* +* Needs not to zero no matter buffer or direct. The one who is +* zero the cluster is doing zero. And he will clear unwritten +* after all cluster io finished. +*/ fs/ocfs2/aops.c static void ocfs2_set_target_boundaries(struct ocfs2_super *osb, · line 1744
Quoted 10 lines
fs/ocfs2/aops.c +9
-0+ desc->c_needs_zero =0 ; + desc->c_clear_unwritten =0 ; +/* match its own entry */ +if (wc->w_type ==OCFS2_WRITE_DIRECT && wc->w_owner && + ue->ue_owner == wc->w_owner) + desc->c_dio_unwritten =1 ; + }else if (wc->w_type ==OCFS2_WRITE_DIRECT ) { +/* This direct write will doing zero. */ + desc->c_clear_unwritten =0 ; }[Severity: High]
Doesocfs2_unwritten_check()causeocfs2_dio_wr_map_blocks()to report an unwritten cluster asIOMAP_MAPPEDwhen another in-flight direct write owns the entry onoi->ip_unwritten_list?CPU0 (write #1, iocb1) CPU1 (write #2, iocb2)
ocfs2_dio_wr_map_blocks()
ocfs2_write_begin_nolock()
ocfs2_dio_add_unwritten()
adds ue (ue_owner= iocb1)
ocfs2_dio_wr_map_blocks()
ocfs2_unwritten_check()
ue->ue_owner!= iocb2c_needs_zero= 0c_dio_unwritten= 0
reportsOCFS2_MAP_MAPPEDiomap_dio_complete()-> returns to user
bio fails or stops short
ocfs2_dio_end_io()
frees ue without converting <-- write #2 data stays in unwritten extent
Quoted 7 lines
Because
c_needs_zeroandc_dio_unwrittenare both 0 for write #2,ocfs2_desc_dio_unwritten()returns false and write #2 does not record its own unwritten entry. If write #2 finishes first, reads see zeros until write #1 completes; if write #1 fails or completes short before that cluster, the extent is never marked written and write #2's data is lost (and sub-block writes in write #2 also skip iomap sub-block zeroing becauseIOMAP_F_NEWis not set).
agree. I have found a solution to fix it.
I will add retry logic in ocfs2_dio_wr_map_blocks() to wait for the unwritten list to be cleared by the owner (#1 CPU)
Quoted 31 lines
[ ... ]
Quoted 13 lines
fs/ocfs2/aops.c +7
-0@@ -1886,11 +2392,26 @@ int ocfs2_write_begin_nolock(struct address_space *mapping, ret =ocfs2_write_cluster_by_desc (mapping, data_ac, meta_ac, wc, pos, len); +if (wc->w_clusters_unused) { +/* A direct write got a shorter chunk than reserved. */ +dquot_free_space (inode, +ocfs2_clusters_to_bytes (osb->sb, wc->w_clusters_unused)); + clusters_to_alloc -= wc->w_clusters_unused; + wc->w_clusters_unused =0 ; + }if (ret) {mlog_errno (ret);fs/ocfs2/aops.c int ocfs2_write_begin_nolock(struct address_space *mapping, · line 2413
Quoted 10 lines
fs/ocfs2/aops.c +7
-0goto out_quota; } +if (type ==OCFS2_WRITE_DIRECT ) { + ret =ocfs2_dio_add_unwritten (inode, wc); +if (ret) { +mlog_errno (ret); + goto out_quota; + } + }[Severity: Medium]
Does jumping toout_quotahere free quota for clusters that were already allocated and attached to the inode?
Quoted 6 lines
Once
ocfs2_dio_alloc_clusters()succeeds insideocfs2_write_clusters_direct(), the allocated clusters have been added to the extent tree andi_clustersinwc->w_handle. Ifocfs2_prepare_cluster_folios()orocfs2_dio_add_unwritten()then fails,out_quotacallsdquot_free_space()for the remainingclusters_to_allocandout_commitcommits the transaction, underflowing the inode's quota reservation while keeping the allocated clusters on disk.
agree.
Quoted 61 lines
[ ... ]
Quoted 42 lines
fs/ocfs2/aops.c +12
-23@@ -2156,132 +2677,134 @@ static int ocfs2_write_end(const struct kiocb *iocb, +static int ocfs2_dio_wr_map_blocks (struct inode *inode, +struct ocfs2_map_block *map, +unsigned int flags,void *owner) {struct ocfs2_super *osb =OCFS2_SB (inode->i_sb);struct ocfs2_inode_info *oi =OCFS2_I (inode);struct ocfs2_write_ctxt *wc; -struct ocfs2_write_cluster_desc *desc =NULL ; -struct ocfs2_dio_write_ctxt *dwc =NULL ; +struct ocfs2_write_cluster_desc *desc; +struct ocfs2_map_block probe;struct buffer_head *di_bh =NULL ; -u64 p_blkno; -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 len, total_len = bh_result->b_size; -int ret =0 , first_get_block =0 ; - - len = osb->s_clustersize - (pos & (osb->s_clustersize -1 )); - len =min (total_len, len); - -/* -* bh_result->b_size is count in get_more_blocks 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------->| -*/ +unsigned int blkbits = inode->i_sb->s_blocksize_bits; +u64 bpc =ocfs2_clusters_to_blocks (inode->i_sb,1 ); +u64 lblk = map->lblk; +u64 eof_blk =ocfs2_blocks_for_bytes (inode->i_sb,i_size_read (inode)); +loff_t pos = lblk << blkbits; +loff_t len, end; +int ret =0 , unwritten; - if ((iblock <= endblk) &&fs/ocfs2/aops.c static int ocfs2_write_end(const struct kiocb *iocb, · line 2700
Quoted 10 lines
fs/ocfs2/aops.c +5
-4- ((iblock + ((len - 1) >> i_blkbits)) > endblk)) - len = (endblk - iblock +1 ) << i_blkbits; +/* Do not hand out a mapping which is over the end of the file. */ +if (lblk < eof_blk && lblk + map->len > eof_blk) + map->len = eof_blk - lblk; -mlog (0 ,"get block of %llu at %llu:%u req %u\n" , - inode->i_ino, pos, len, total_len); + map->len =min_t (u64, map->len, +max_t (u64,OCFS2_DIO_MAX_MAP_BYTES >> blkbits, bpc));[Severity: High]
When lblk is not cluster-aligned and the write exceeds 16MB, can cappingmap->lenatOCFS2_DIO_MAX_MAP_BYTES>> blkbits split the mapping in the middle of a cluster and corrupt the second half of that cluster?
Quoted 10 lines
If a direct write > 16MB starts at an unaligned block offset into a hole or unwritten extent below
i_size(for example, pos = 4KB with 64KB clusters), the first mapping ends at 16MB + 4KB, mid-way through a cluster.ocfs2_grab_folios_direct()then treats 16MB + 4KB as the end of the user write and zeroes and dirties the page-cache folios for the tail of that cluster ([16MB + 4KB, 16MB + 64KB)). Because__iomap_dio_rw()only invalidates the page cache before startingiomap_iter(), the next mapping writes directly to [16MB + 4KB, ...) on disk while dirty zero folios remain in the page cache and later overwrite the direct I/O data on writeback.
agree, will modify the map->len logic.
Quoted 23 lines
Quoted 7 lines
fs/ocfs2/aops.c +3
-1@@ -2290,95 +2813,149 @@ static int ocfs2_dio_wr_get_block(struct inode *inode, sector_t iblock, +again: ret =ocfs2_write_begin_nolock (inode->i_mapping, pos, len,OCFS2_WRITE_DIRECT ,NULL , - (void **)&wc, di_bh,NULL ); + (void **)&wc, di_bh,NULL , owner); + if (ret == -ENOSPC &&fs/ocfs2/aops.c static int ocfs2_dio_wr_get_block(struct inode *inode, sector_t iblock, · line 2827
Quoted 10 lines
fs/ocfs2/aops.c +10
-0+ ocfs2_clusters_for_bytes(inode->i_sb, pos + len) > + ocfs2_clusters_for_bytes(inode->i_sb, pos) + 1) { +/* +* There is not room for all of it, but maybe there is for +* some, take the first cluster. +*/ + len =min_t (loff_t, len, osb->s_clustersize - + (pos & (osb->s_clustersize -1 ))); + goto again; + }[Severity: Medium]
Should the starting cluster in this-ENOSPCretry check useocfs2_bytes_to_clusters(inode->i_sb, pos)instead ofocfs2_clusters_for_bytes(inode->i_sb, pos)?
Quoted 7 lines
ocfs2_clusters_for_bytes()rounds up to the next cluster boundary, so when pos is not cluster-aligned and [pos, pos + len) spans two clusters c and c + 1,ocfs2_clusters_for_bytes(inode->i_sb, pos)returns c + 1 andocfs2_clusters_for_bytes(inode->i_sb, pos + len)returns c + 2. The condition c + 2 > (c + 1) + 1 evaluates to false, skipping the single-cluster fallback and returning-ENOSPCprematurely.
will follow the suggestion
Quoted 4 lines
Also,
ocfs2_dio_wr_map_blocks()still has a leftover "//zhm: remove??" comment on theIOMAP_NOWAITcheck above, and the commit message refers toocfs2_dio_write_end_io()andocfs2_iomap_dio_end_io_write(), whereas the patch definesocfs2_dio_end_io()andocfs2_dio_end_io_write().
removed
Quoted 37 lines
[ ... ]
Quoted 14 lines
fs/ocfs2/aops.c +10
-0@@ -2604,10 +3170,95 @@ static int ocfs2_iomap_begin(struct inode *inode, loff_t offset, loff_t length,return 0 ; } +static int ocfs2_iomap_next (const struct iomap_iter *iter,struct iomap *iomap, +struct iomap *srcmap) +{ +int ret; + +if (iomap->length) { + ret =iomap_iter_continue (iter, iomap, srcmap,0 ); +if (ret <=0 ) +return ret; + }fs/ocfs2/aops.c static int ocfs2_iomap_begin(struct inode *inode, loff_t offset, loff_t length, · line 3191
Quoted 10 lines
fs/ocfs2/aops.c +7
-1+ + ret =ocfs2_iomap_begin (iter->inode, iter->pos, iter->len, iter->flags, + iomap, srcmap, iter->private); +return ret <0 ? ret :1 ; +} +const struct iomap_ops ocfs2_iomap_ops = { - .iomap_begin = ocfs2_iomap_begin, + .iomap_next = ocfs2_iomap_next, };[Severity: High]
Does this fail to build because structiomap_opsno longer exists ininclude/linux/iomap.h?
__iomap_dio_rw()takes aniomap_iter_next_fncallback directly as its third parameter:
include/linux/iomap.h:
structiomap_dio*__iomap_dio_rw(struct kiocb *iocb, structiov_iter*iter,
Quoted 7 lines
iomap_iter_next_fn next, const struct iomap_dio_ops *dops, unsigned int dio_flags, void *private, size_t done_before);Defining const struct
iomap_opsocfs2_iomap_opshere (and declaring it infs/ocfs2/aops.h) fails to compile on an incomplete type, and passing&ocfs2_iomap_opsto__iomap_dio_rw()inocfs2_file_write_iter()passes the wrong type instead ofocfs2_iomap_next.
The iomap APIs have changed during the dev of this patch set. I will address in the next version.
Quoted 22 lines
Quoted 5 lines
fs/ocfs2/file.c +0
-0[ ... ]
fs/ocfs2/file.c static bool ocfs2_should_use_dio(struct kiocb *iocb, struct iov_iter *iter, · line 2420
Quoted 9 lines
fs/ocfs2/file.c +5
-0@@ -2404,6 +2413,11 @@ static bool ocfs2_should_use_dio(struct kiocb *iocb, struct iov_iter *iter, struct inode *inode) {struct ocfs2_super *osb =OCFS2_SB (inode->i_sb); +unsigned int dio_align =bdev_logical_block_size (inode->i_sb->s_bdev); + +/* Fallback to buffered I/O if we do not alignment. */ +if (!IS_ALIGNED (iocb->ki_pos |iov_iter_alignment (iter), dio_align)) +return false;[Severity: High]
Won't returning false here cause misalignedO_DIRECTreads and writes to silently fall back to buffered I/O instead of returning-EINVAL?
Quoted 12 lines
In addition, when
ocfs2_file_write_iter()falls back to buffered I/O (either viaocfs2_should_use_dio()returning false or on-ENOTBLK):
- In the !ocfs2_should_use_dio() branch, clearingIOCB_DIRECTbefore calling__generic_file_write_iter()skipsdirect_write_fallback(), leaving dirty pages in the page cache without writeback or invalidation.
- On both fallback paths, localdirect_ioremains 1, so the post-write ((file->f_flags&O_DSYNC) && !direct_io) check is skipped andO_DSYNCwrites return without committing the journal.
- With coherency=buffered on a non-extending write,rw_levelis 0 (PR), so the buffered write fallback runs without the exclusive EX clusterrw_lock, and ifIOCB_NOWAITwas set, the fallback blocks on locks and transactions.
will move the misaligned logic from ocfs2_should_use_dio() to ocfs2_file_write_iter().
Quoted 55 lines
Quoted 22 lines
fs/ocfs2/file.c +15
-5@@ -2527,26 +2574,90 @@ static ssize_t ocfs2_file_write_iter(struct kiocb *iocb, -/* 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 (iocb->ki_pos >i_size_read (inode) && +fault_in_iov_iter_readable (from, count)) { + ret = -EFAULT ; + goto out; + } - written =__generic_file_write_iter (iocb, from); -/* buffered aio wouldn't have proper lock coverage today */ -BUG_ON (written == -EIOCBQUEUED && !direct_io); +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 +* 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 ) {fs/ocfs2/file.c static ssize_t ocfs2_file_write_iter(struct kiocb *iocb, · line 2599
Quoted 10 lines
fs/ocfs2/file.c +10
-0+mlog_errno (ret); + goto out; + } + orphaned =1 ; + dio_flags =IOMAP_DIO_FORCE_WAIT ; + } + + ret =ocfs2_dio_zero_eof (inode, iocb->ki_pos, count); +if (ret <0 ) + goto out;[Severity: High]
Can an asynchronous direct write that extendsi_sizewithin an already allocated cluster complete asynchronously and have its data zeroed by the next write?CPU0 (async write #1: 4KB..8KB) CPU1 (write #2: 8KB..12KB)
ocfs2_file_write_iter()
i_sizeis 4KB, cluster is 64KB
clusters_for_bytes(8KB)== 1
dio_flags= 0 (noFORCE_WAIT)
__iomap_dio_rw()->-EIOCBQUEUED
inode_unlock(inode)ocfs2_file_write_iter() inode_lock(inode) i_size is still 4KB ocfs2_dio_zero_eof(pos = 8KB) ocfs2_zero_tail() zeroes 4KB..8KB <-- clobbers write #1
s_dio_done_wq:
ocfs2_dio_end_io_write()
updatesi_sizeto 8KB
Quoted 5 lines
In
__iomap_dio_rw(), extending writes only setIOMAP_DIO_COMP_WORK, notwait_for_completion. Whencluster_size>block_sizeand a block-aligned async write extendsi_sizewithout crossing a cluster boundary,IOMAP_DIO_FORCE_WAITis not set, soinode_lock(inode)is dropped befores_dio_done_wqupdatesi_size.
agree, will add condition check in ocfs2_file_write_iter().
Quoted 10 lines
[Severity: Medium]
Does this block and modify on-disk metadata whenIOCB_NOWAITis set?
ocfs2_overwrite_io()inocfs2_prepare_inode_for_write()allows writes pasti_sizeinto the tail of an allocated cluster or into preallocated unwritten extents.ocfs2_file_write_iter()then runsfault_in_iov_iter_readable(),ocfs2_add_inode_to_orphan(), andocfs2_dio_zero_eof()— sleeping on page faults, cluster locks, and journal transactions — beforeocfs2_dio_wr_map_blocks()checksIOMAP_NOWAITand fails with-EAGAIN.
agree, also requires new condition check in ocfs2_file_write_iter().
Quoted 28 lines
[ ... ]
fs/ocfs2/file.c static ssize_t ocfs2_file_write_iter(struct kiocb *iocb, · line 2690
Quoted 10 lines
fs/ocfs2/file.c +6
-0@@ -2571,6 +2682,13 @@ static ssize_t ocfs2_file_write_iter(struct kiocb *iocb, } out: +/* +* IOMAP_DIO_FORCE_WAIT made the dio complete synchronously, so its +* end_io has already updated i_size. +*/ +if (orphaned) +ocfs2_dio_del_orphan (inode);[Severity: High]
If an extending direct write fails or completes short after allocating clusters beyondi_size, doesocfs2_dio_del_orphan()leave those excess clusters allocated pasti_size?
ocfs2_dio_zero_eof()andocfs2_dio_wr_map_blocks()allocate and commit clusters before bio submission or when pinning user pages (for instance, ifki_pos==i_sizeand the user buffer faults inbio_iov_iter_get_pages(), sincefault_in_iov_iter_readable()only runs whenki_pos>i_size). When the write fails or stops short,i_sizeis not extended to cover the new clusters, butocfs2_dio_del_orphan()removes the inode from the orphan directory and clearsOCFS2_DIO_ORPHANED_FLwithout truncating the blocks beyondi_size, leaking the allocated clusters and quota.
Quoted 5 lines
Additionally, for synchronous DIO completions,
ocfs2_dio_end_io()callsocfs2_rw_unlock()insideiomap_dio_complete()before returning here, soocfs2_dio_del_orphan()runs after the cluster-widerw_lockhas already been released.
agree. in my view, should restore the ocfs2_del_inode_from_orphan() in ocfs2_dio_end_io_write().
Thanks,
Heming