migrate ocfs2 DIO from buffer_head to iomap
Heming Zhao
reviewerv8 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. It is based on commit aebb72744764 (v7.4-rc3), and also incorporates Joseph's code and ideas from [1]:
V8 changelog:
Address sashiko's review comments (all changes are in patch [3/3]):
1. move del_orphan from ocfs2_file_write_iter() to ocfs2_dio_end_io_write().
2. Fix several edge-case handling problems in the DIO write path.
3. adjust dio fallback logic in ocfs2_file_write_iter() to ensure dirty pages are properly written back or invalidated.
4. Note that some issues reported by sashiko are pre-existing problems and won't be fixed in this patch set.
--------------
# test report
This patch set has been tested on 7.4-rc3 kernel
## ltp
runltp -f dio -d /mnt/ocfs2/
results: passed
## 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 8K --fs-features=local --fs-feature-level=max-features"
Failures: generic/003 generic/056 generic/075 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/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 45 of 632 tests
## fio
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=38.5k, BW=4808MiB/s
after patch: write: IOPS=39.3k, BW=4911MiB/s
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=51.7k, BW=6461MiB/s
after patch: read: IOPS=50.0k, BW=6253MiB/s
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: write: IOPS=27.3k, BW=107MiB/s
after patch: write: IOPS=26.9k, BW=105MiB/s
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: read: IOPS=372k, BW=1455MiB/s
after patch: read: IOPS=350k, BW=1368MiB/s
------
[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
v6 patch set:
https://lore.kernel.org/ocfs2-devel/[email protected]/
v6 sashiko review:
https://sashiko.dev/#/patchset/20261115154013.9379-1-heming.zhao%40suse.com
v7 patch set:
https://lore.kernel.org/ocfs2-devel/[email protected]/
v7 sashiko review:
- https://sashiko.dev/#/patchset/20261117150053.18417-1-heming.zhao%40suse.com
- https://sashiko.dev/#/patchset/20261118002050.9019-1-heming.zhao%40suse.com
-----------------------
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 | 1661 ++++++++++++++++++++++++++++++++--------- fs/ocfs2/aops.h | 42 +- fs/ocfs2/extent_map.c | 34 +- fs/ocfs2/extent_map.h | 3 + fs/ocfs2/file.c | 226 +++++- fs/ocfs2/inode.h | 9 + fs/ocfs2/mmap.c | 2 +- fs/ocfs2/ocfs2.h | 25 +- fs/ocfs2/super.c | 1 + 10 files changed, 1577 insertions(+), 428 deletions(-)
Signature · 1 line
2.55.0
Heming Zhao
reviewerRFC PATCH v8 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 | 70 ++++++++++++++++++++++++++++++++++++++++++++++++ fs/ocfs2/aops.h | 2 ++ fs/ocfs2/ocfs2.h | 25 ++++++++++++++++- 3 files changed, 96 insertions(+), 1 deletion(-)
fs/ocfs2/aops.c +70 -0
@@ -126,6 +126,76 @@ 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; + +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
Heming Zhao
reviewerRFC PATCH v8 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 | 107 +++++++++++++++++++++++++++++++++++++++++++++-- fs/ocfs2/aops.h | 5 +++ fs/ocfs2/file.c | 60 ++++++++++++++++++++++---- 4 files changed, 161 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 +104 -3
138 lines not on this page · raw message
fs/ocfs2/aops.h +5 -0
@@ -7,6 +7,11 @@#define OCFS2_AOPS_H #include <linux/fs.h> +#include <linux/iomap.h> + +extern const struct iomap_dio_ops ocfs2_iomap_dio_ops_r_pr; +int ocfs2_iomap_next (const struct iomap_iter *iter,struct iomap *iomap, +struct iomap *srcmap); 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
Heming Zhao
reviewerRFC PATCH v8 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_end_io() and ocfs2_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().
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 | 1572 ++++++++++++++++++++++++++++++----------- fs/ocfs2/aops.h | 35 +- fs/ocfs2/extent_map.c | 34 +- fs/ocfs2/extent_map.h | 3 + fs/ocfs2/file.c | 166 ++++- fs/ocfs2/inode.h | 9 + fs/ocfs2/mmap.c | 2 +- fs/ocfs2/super.c | 1 + 9 files changed, 1364 insertions(+), 459 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 +1176 -396
1,962 lines not on this page · raw message
fs/ocfs2/aops.h +5 -30
@@ -10,6 +10,8 @@#include <linux/iomap.h> 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_iomap_next (const struct iomap_iter *iter,struct iomap *iomap,struct iomap *srcmap);
@@ -39,7 +41,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);
@@ -47,36 +50,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/extent_map.c +26 -8
@@ -602,9 +602,9 @@ int ocfs2_xattr_get_clusters(struct inode *inode, u32 v_cluster,return ret; } -int ocfs2_get_clusters (struct inode *inode,u32 v_cluster, -u32 *p_cluster,u32 *num_clusters, -unsigned int *extent_flags) +static int __ocfs2_get_clusters (struct inode *inode,u32 v_cluster, +u32 *p_cluster,u32 *num_clusters, +unsigned int *extent_flags,bool cache) {int ret;unsigned int hole_len, flags =0 ;
@@ -617,10 +617,12 @@ int ocfs2_get_clusters(struct inode *inode, u32 v_cluster, goto out; } - ret =ocfs2_extent_map_lookup (inode, v_cluster, p_cluster, - num_clusters, extent_flags); -if (ret ==0 ) - goto out; +if (cache) { + ret =ocfs2_extent_map_lookup (inode, v_cluster, p_cluster, + num_clusters, extent_flags); +if (ret ==0 ) + goto out; + } ret =ocfs2_read_inode_block (inode, &di_bh);if (ret) {
@@ -650,7 +652,8 @@ int ocfs2_get_clusters(struct inode *inode, u32 v_cluster, p_cluster, num_clusters); flags = rec.e_flags; -ocfs2_extent_map_insert_rec (inode, &rec); +if (cache) +ocfs2_extent_map_insert_rec (inode, &rec); }if (extent_flags)
@@ -661,6 +664,21 @@ int ocfs2_get_clusters(struct inode *inode, u32 v_cluster,return ret; } +int ocfs2_get_clusters (struct inode *inode,u32 v_cluster,u32 *p_cluster, +u32 *num_clusters,unsigned int *extent_flags) +{ +return __ocfs2_get_clusters (inode, v_cluster, p_cluster, num_clusters, + extent_flags, true); +} + +int ocfs2_get_clusters_uncached (struct inode *inode,u32 v_cluster, +u32 *p_cluster,u32 *num_clusters, +unsigned int *extent_flags) +{ +return __ocfs2_get_clusters (inode, v_cluster, p_cluster, num_clusters, + extent_flags, false); +} +/* * This expects alloc_sem to be held. The allocation cannot change at * all while the map is in the process of being updated.
fs/ocfs2/extent_map.h +3 -0
@@ -32,6 +32,9 @@ void ocfs2_extent_map_insert_rec(struct inode *inode,int ocfs2_get_clusters (struct inode *inode,u32 v_cluster,u32 *p_cluster,u32 *num_clusters,unsigned int *extent_flags); +int ocfs2_get_clusters_uncached (struct inode *inode,u32 v_cluster, +u32 *p_cluster,u32 *num_clusters, +unsigned int *extent_flags);int ocfs2_extent_map_get_blocks (struct inode *inode,u64 v_blkno,u64 *p_blkno,u64 *ret_count,unsigned int *extent_flags);
fs/ocfs2/file.c +143 -23
221 lines not on this page · raw message
fs/ocfs2/inode.h +9 -0
@@ -40,6 +40,15 @@ struct ocfs2_inode_info/* Record unwritten extents during direct io. */ struct list_head ip_unwritten_list; +/* +* Woken when entries leave ip_unwritten_list. A direct write waits +* on it for the entries of the other dios, see +* ocfs2_dio_wr_map_blocks(). +*/ +wait_queue_head_t ip_unwritten_wq; + +pid_t ip_dio_orphan_pid; +loff_t ip_dio_orphan_end;/* protected by recovery_lock. */ struct inode *ip_next_orphan;
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);
fs/ocfs2/super.c +1 -0
@@ -1632,6 +1632,7 @@ static void ocfs2_inode_init_once(void *data)ocfs2_extent_map_init (&oi->vfs_inode);INIT_LIST_HEAD (&oi->ip_io_markers);INIT_LIST_HEAD (&oi->ip_unwritten_list); +init_waitqueue_head (&oi->ip_unwritten_wq); oi->ip_dir_start_lookup =0 ;init_rwsem (&oi->ip_alloc_sem);init_rwsem (&oi->ip_xattr_sem);
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 v8 3/3] ocfs2: switch dio write path from buffer_head to iomap
Quoted 102 lines · On 11/19/26 15:17, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 8 potential issues to consider:
- [High] ocfs2: data corruption inocfs2_unwritten_check()racing with in-flight DIO
- [High] ocfs2: unzeroed sub-page blocks inocfs2_grab_folios_direct()on unwritten DIO
- [High] ocfs2: leak of orphan state and post-EOF clusters on DIO write failure
- [High] ocfs2: skippedO_DSYNCcommit and bad lock mode on DIO write fallback
- [High] ocfs2: missingrw_lockupgrade after reloadingappend_write
- [Medium] ocfs2: u32 cluster truncation inocfs2_dio_wr_map_blocks()cap_endnears_maxbytes
- [Medium] ocfs2: uninitializedip_dio_orphan_pidinocfs2_inode_init_once()
- [Medium] ocfs2: off-by-oneappend_writecheck rejectsRWF_NOWAITat EOF
--commit
8112d6f9133d337b4bfbd643ba1711c27d614d85
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, mapping multi-cluster extents inocfs2_dio_wr_map_blocks()and converting unwritten extents upon I/O completion inocfs2_dio_end_io_write().Quoted 4 lines
fs/ocfs2/aops.c +0
-0[ ... ]
Quoted 53 lines
fs/ocfs2/aops.c +48
-1@@ -1084,7 +1167,85 @@ static int ocfs2_prepare_folio_for_write(struct inode *inode, u64 *p_blkno, }/* -* This function will only grab one clusters worth of pages. +* Grab the folios a direct write has to take care of, w_folios is laid out as +* two windows of one cluster's worth of pages each: +* +* [0, ppc) the pages of the first cluster of the write +* [ppc, 2ppc) the pages of the last cluster of the write +* +* A direct write goes to the disk directly, so the pages inside of the user +* range are not touched and stay NULL. The clusters in the middle of the +* write are covered by the user range entirely and need nothing at all. Only +* the head cluster before the user range and the tail cluster after it can have +* pages which are not going to be overwritten, those must be zeroed through the +* page cache if the cluster is new or unwritten (c_needs_zero). +* +* If the write is made of just one cluster, there is only the first window. +* +* Like for the buffered write, nothing past the end of the file is needed, the +* tail is then cut at max(user range end, i_size). +*/ +static int ocfs2_grab_folios_direct (struct address_space *mapping, +struct ocfs2_write_ctxt *wc,loff_t user_pos,unsigned user_len) +{ +struct inode *inode = mapping->host; +unsigned int ppc =ocfs2_pages_per_cluster (inode->i_sb); +unsigned long target_index = user_pos >>PAGE_SHIFT ; +unsigned long end_index = (user_pos + user_len -1 ) >>PAGE_SHIFT ; +loff_t last_byte =max_t (loff_t, user_pos + user_len, +i_size_read (inode)); +unsigned long last_index = ((last_byte -1 ) >>PAGE_SHIFT ) +1 ; +unsigned int nr_win = wc->w_clen >1 ?2 :1 ; +int ret =0 , win, i; + + wc->w_num_folios =0 ; + +for (win =0 ; win < nr_win; win++) { +struct ocfs2_write_cluster_desc *desc = + win ? &wc->w_desc[wc->w_clen -1 ] : &wc->w_desc[0 ]; +unsigned long start; + +if (!desc->c_needs_zero) +continue ; + + start =ocfs2_align_clusters_to_page_index (inode->i_sb, + desc->c_cpos); + +for (i =0 ; i < ppc; i++) { +unsigned long index = start + i; +struct folio *folio; +fs/ocfs2/aops.c static int ocfs2_prepare_folio_for_write(struct inode *inode, u64 *p_blkno, · line 1227
Quoted 10 lines
fs/ocfs2/aops.c +10
-0+if (index >= last_index) +break ; + +/* Track the slots in use, for the unlock/commit loops. */ + wc->w_num_folios = max_t(unsigned int, wc->w_num_folios, + win * ppc + i +1 ); + +/* The direct io target has no page cache page here. */ +if (index >= target_index && index <= end_index) +continue ;[Severity: High]
Does skipping every folio in [target_index,end_index] leave unwritten sub-page blocks unzeroed whens_blocksize<PAGE_SIZE?
Quoted 8 lines
iomap_dio_zero()only zeroes sub-block padding up to the filesystem block size, not up toPAGE_SIZE. When a direct write to a newly allocated or unwritten cluster is block-aligned but does not cover an entire page (for example, writing a 1KB block into a 4KB cluster/page), bothocfs2_dio_need_zero_folios()andocfs2_grab_folios_direct()skip the page at target_index/end_index, andocfs2_dio_end_io_write()then marks the whole cluster written, exposing stale on-disk data in the unwritten blocks of that page.
will switch buffered IO for above case.
Quoted 47 lines
[ ... ]
Quoted 1 line
fs/ocfs2/aops.c +0
-0@@ -1376,76 +1717,212 @@ static void ocfs2_set_target_boundaries(struct ocfs2_super *osb,[ ... ]
Quoted 11 lines
fs/ocfs2/aops.c +11
-0+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; + +if (!desc->c_needs_zero) +return ; + +spin_lock (&oi->ip_lock);fs/ocfs2/aops.c static void ocfs2_set_target_boundaries(struct ocfs2_super *osb, · line 1788
Quoted 10 lines
fs/ocfs2/aops.c +10
-0+ 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. +*/ + desc->c_needs_zero =0 ; + desc->c_clear_unwritten =0 ;[Severity: High]
Can a buffered write or mmap page fault lose data when it hits a cluster onip_unwritten_listowned by an in-flight direct write?CPU0 (async DIO write) CPU1 (mmap fault / buffered write)
ocfs2_file_write_iter()
__iomap_dio_rw()ocfs2_dio_wr_map_blocks() ocfs2_write_begin_nolock() ocfs2_dio_add_unwritten() up_write(&oi->ip_alloc_sem) ocfs2_inode_unlock() __ocfs2_page_mkwrite() ocfs2_write_begin_nolock() ocfs2_unwritten_check() desc->c_needs_zero = 0 desc->c_clear_unwritten = 0 /* bio fails or ends short */ ocfs2_dio_end_io() /* skips conversion */ /* extent stays OCFS2_EXT_UNWRITTEN */
Quoted 11 lines
ocfs2_dio_wr_map_blocks()now maps up to 16MB of unwritten clusters at once and only converts the clusters covered by the completed I/O inocfs2_dio_end_io_write()if the DIO succeeds, which is why direct writes wait onip_unwritten_wqviaocfs2_dio_foreign_unwritten(). Buffered and mmap writes do not wait: they clearc_clear_unwrittenandc_needs_zero, so if the in-flight DIO fails (for example,-EFAULTon a later user page) or completes short, the extent is never marked written on disk and reads back as zeroes once the folio is evicted; in addition, a partial-page buffered write to a folio inside the DIO range thatocfs2_grab_folios_direct()did not zero will read raw uninitialized blocks from the unwritten extent.
I have answered in previous sashiko review comments. This is an existing issue, won't be fixed in this patch set.
Quoted 66 lines
[ ... ]
Quoted 1 line
fs/ocfs2/aops.c +0
-0@@ -2151,130 +2727,178 @@ static int ocfs2_write_end(const struct kiocb *iocb,[ ... ]
Quoted 48 lines
fs/ocfs2/aops.c +34
-10+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; +struct ocfs2_map_block probe; +struct buffer_head *di_bh =NULL ; +unsigned int blkbits = inode->i_sb->s_blocksize_bits; +u64 bpc =ocfs2_clusters_to_blocks (inode->i_sb,1 ); +u64 lblk = map->lblk; +u64 req_len = map->len; +u64 eof_blk, cap_end; +loff_t pos = lblk << blkbits; +loff_t len, end; +u32 cstart, cend; +int ret =0 , unwritten; + +retry: + map->len = req_len; + map->flags =0 ; + eof_blk =ocfs2_blocks_for_bytes (inode->i_sb,i_size_read (inode)); - len = osb->s_clustersize - (pos & (osb->s_clustersize -1 )); - len =min (total_len, len); +/* 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; + map->len =min_t (u64, map->len, +max_t (u64,OCFS2_DIO_MAX_MAP_BYTES >> blkbits, bpc));/* -* 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------->| +* Cap the size of a mapping, but not in the middle of a cluster. The +* rest of that cluster, if it is a hole or unwritten, is left to the +* next mapping to write. The folios this one would zero there are the +* very ones the next one writes directly to the disk, and they would +* overwrite that data when they are written back. */ fs/ocfs2/aops.c static int ocfs2_write_end(const struct kiocb *iocb, · line 2849
Quoted 10 lines
fs/ocfs2/aops.c +3
-7- -if ((iblock <= endblk) && - ((iblock + ((len -1 ) >> i_blkbits)) > endblk)) - len = (endblk - iblock +1 ) << i_blkbits; - -mlog (0 ,"get block of %llu at %llu:%u req %u\n" , - inode->i_ino, pos, len, total_len); + cap_end =ocfs2_clusters_to_blocks (inode->i_sb, + (pos +OCFS2_DIO_MAX_MAP_BYTES ) >> osb->s_clustersize_bits); + map->len =min_t (u64, map->len, cap_end - lblk);[Severity: Medium]
Can the cluster number passed toocfs2_clusters_to_blocks()overflow u32 when pos is withinOCFS2_DIO_MAX_MAP_BYTESofs_maxbytes?
Quoted 7 lines
On 64-bit systems
s_maxbytesisUINT_MAXclusters, so when pos is within the last 16MB of the maximum file offset, (pos +OCFS2_DIO_MAX_MAP_BYTES) >>osb->s_clustersize_bitsreaches0x100000000ULLand truncates in the u32 parameter ofocfs2_clusters_to_blocks().cap_end- lblk then underflows u64, bypassing the cluster-boundary cap and allowing a non-cluster-aligned 16MB mapping to end mid-cluster.
agree, will fix in the next version.
Quoted 31 lines
Quoted 4 lines
fs/ocfs2/file.c +0
-0[ ... ]
Quoted 11 lines
fs/ocfs2/file.c +5
-2@@ -2527,31 +2541,133 @@ 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)) { + use_dio = true; +/* if we do not alignment. */ +if (!IS_ALIGNED (iocb->ki_pos |iov_iter_alignment (from), + dio_align)) {fs/ocfs2/file.c static ssize_t ocfs2_file_write_iter(struct kiocb *iocb, · line 2555
Quoted 10 lines
fs/ocfs2/file.c +6
-3+ ret = -EINVAL ; + goto out; + } - written =__generic_file_write_iter (iocb, from); -/* buffered aio wouldn't have proper lock coverage today */ -BUG_ON (written == -EIOCBQUEUED && !direct_io); +/* reload value after inode_lock() and cluster lock */ + append_write = ((iocb->ki_pos + count) >= +i_size_read (inode) ?1 :0 );[Severity: High]
Ifappend_writechanges from 0 to 1 when reloaded here, doesrw_levelremain 0 (PR mode) instead of upgrading the clusterrw_lockto EX?
Quoted 8 lines
On a coherency=buffered mount,
rw_levelis computed and locked beforegeneric_write_checks()updatesiocb->ki_posfor O_APPEND/RWF_APPEND and beforeocfs2_prepare_inode_for_write()refreshesi_sizeunder the cluster lock. AnO_APPENDwrite starting at offset 0 with count <i_sizetherefore acquiresip_rw_lockresin PR mode, reloadsappend_write= 1 here, and proceeds to modify the orphan directory, zero EOF, allocate blocks, updatei_size, and unlock viaocfs2_iomap_dio_ops_w_prunder a shared PR lock.
no idea about the above suggestion. this is an existing issue.
Quoted 15 lines
fs/ocfs2/file.c static ssize_t ocfs2_file_write_iter(struct kiocb *iocb, · line 2566
Quoted 11 lines
fs/ocfs2/file.c +11
-0+ +/* +* Extending the file means the gap between i_size and the +* write is zeroed below, against the i_size we have now, and +* that the next writer has to see the new size. That takes +* cluster locks and a transaction, which IOCB_NOWAIT forbids. +*/ +if (nowait && append_write) { + ret = -EAGAIN ; + goto out; + }[Severity: Medium]
Shouldappend_writetest >i_size_read(inode)rather than >=?
Quoted 6 lines
When
iocb->ki_pos+ count ==i_size_read(inode), the write is a non-extending overwrite up to EOF, butappend_writeis 1. That causesRWF_NOWAIToverwrites ending at EOF to fail with-EAGAINand forces asynchronous overwrites ending at EOF into synchronous completion viaIOMAP_DIO_FORCE_WAIT.
agree. will fix in the next version.
Quoted 46 lines
Quoted 30 lines
fs/ocfs2/file.c +30
-0+ +if (iocb->ki_pos >i_size_read (inode) && +fault_in_iov_iter_readable (from, count)) { + ret = -EFAULT ; + goto out; + } + +if (append_write) { +/* +* Complete before i_rwsem is dropped: i_size is only +* updated by ->end_io(), and the next writer would +* zero the gap between the size it still sees and its +* own write, over the data this one has just written. +*/ + dio_flags |=IOMAP_DIO_FORCE_WAIT ; + +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 ) { +mlog_errno (ret); + goto out; + }fs/ocfs2/file.c static ssize_t ocfs2_file_write_iter(struct kiocb *iocb, · line 2606
Quoted 10 lines
fs/ocfs2/file.c +10
-0+WRITE_ONCE (OCFS2_I (inode)->ip_dio_orphan_pid, +task_pid_nr (current)); +WRITE_ONCE (OCFS2_I (inode)->ip_dio_orphan_end, + iocb->ki_pos + count); + } + } + + ret =ocfs2_dio_zero_eof (inode, iocb->ki_pos, count); +if (ret <0 ) + goto out;[Severity: High]
Does an extending direct write that fails or falls back to buffered I/O afterocfs2_add_inode_to_orphan()leak the orphan directory entry and any clusters allocated beyondi_size?
Quoted 12 lines
ocfs2_dio_clear_orphan_locked()is only called fromocfs2_dio_end_io_write(), andocfs2_dio_end_io()only callsocfs2_dio_end_io_write()when size != 0 && error == 0 && (!list_empty(&unwritten) || offset + size >i_size_read(inode)). Ifocfs2_dio_zero_eof()fails, if__iomap_dio_rw()returns-ENOTBLKbefore starting I/O, or if__iomap_dio_rw()fails afterocfs2_dio_wr_map_blocks()has allocated clusters beyondi_size(for example, with-EFAULTwhen pinning an unmapped user buffer on an append write atki_pos==i_size),ocfs2_dio_clear_orphan_locked()never runs. The excess post-EOF clusters are not truncated,ip_dio_orphan_pidandip_dio_orphan_endremain set on the inode, andOCFS2_DIO_ORPHANED_FLstays set on disk so unlinking the file skips wiping the inode inocfs2_delete_inode().
I will do a partly fix: add orphan inode clearing in the rw_lock release path. Other cases still follow the old logic, which removes the orphan inode in the next call to ocfs2_add_inode_to_orphan().
Quoted 71 lines
Quoted 56 lines
fs/ocfs2/file.c +55
-0+ +const struct iomap_dio_ops *dops = rw_level ? + &ocfs2_iomap_dio_ops_w_ex : &ocfs2_iomap_dio_ops_w_pr; +struct iomap_dio *dio; + +/* +* The kiocb is the owner of the unwritten clusters which +* ->iomap_begin() records for ->end_io() to convert, see +* ocfs2_iomap_next(). +*/ + dio =__iomap_dio_rw (iocb, from, ocfs2_iomap_next, + dops, dio_flags, iocb,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 ) + written =0 ; +if (written ==0 &&iov_iter_count (from)) { + written =direct_write_fallback (iocb, from,0 , +generic_perform_write (iocb, from)); + } + }else if (direct_io) { + written =direct_write_fallback (iocb, from,0 , +generic_perform_write (iocb, from)); + }else { + written =__generic_file_write_iter (iocb, from); }fs/ocfs2/file.c static ssize_t ocfs2_file_write_iter(struct kiocb *iocb, · line 2670
Quoted 10 lines
fs/ocfs2/file.c +4
-2+/* buffered aio wouldn't have proper lock coverage today */ +BUG_ON (written == -EIOCBQUEUED && !direct_io); +if (unlikely (written <=0 )) goto out; -if (((file->f_flags &O_DSYNC ) && !direct_io) || -IS_SYNC (inode)) { +if (((file->f_flags &O_DSYNC ) && !use_dio) ||IS_SYNC (inode)) {[Severity: High]
Does falling back togeneric_perform_write()after__iomap_dio_rw()returns-ENOTBLKleaveuse_dioset to true and skip theO_DSYNCjournal commit?
Quoted 8 lines
direct_write_fallback()writes and invalidates the page cache range, but it does not commit the JBD2 journal. Becauseuse_diois still true after the-ENOTBLKfallback, theO_DSYNCblock here is skipped andjbd2_journal_force_commit()is never called before returning to userspace. In addition, bothdirect_write_fallback()branches rungeneric_perform_write()while holding only a PR clusterrw_lock(rw_level==
0) on coherency=buffered non-extending writes, and without rejectingIOCB_NOWAITbefore entering blocking buffered write paths.
agree, will change in the next version.
btw, this is an existing issue. The old code also doesn't upgrade PR=>EX when switching from DIO to buffered IO.
Quoted 18 lines
fs/ocfs2/super.c static void ocfs2_inode_init_once(void *data) · line 1651
Quoted 13 lines
fs/ocfs2/super.c +1
-0@@ -1645,6 +1645,7 @@ static void ocfs2_inode_init_once(void *data)ocfs2_extent_map_init (&oi->vfs_inode);INIT_LIST_HEAD (&oi->ip_io_markers);INIT_LIST_HEAD (&oi->ip_unwritten_list); +init_waitqueue_head (&oi->ip_unwritten_wq); oi->ip_dir_start_lookup =0 ;init_rwsem (&oi->ip_alloc_sem);init_rwsem (&oi->ip_xattr_sem);[Severity: Medium]
Areoi->ip_dio_orphan_pidandoi->ip_dio_orphan_endleft uninitialized when an inode is allocated fromocfs2_inode_cachep?
Quoted 7 lines
Neither
ocfs2_inode_init_once()norocfs2_alloc_inode()initializes them, andocfs2_dio_end_io_write()unconditionally checksREAD_ONCE(oi->ip_dio_orphan_pid)==task_pid_nr(current). On a non-orphaned direct write that converts an unwritten extent, an uninitialized slab value matching current's PID will invokeocfs2_dio_clear_orphan_locked()with a garbageip_dio_orphan_endand truncate preallocated blocks beyondi_size.
will follow above suggestion.
Thanks,
Heming