migrate ocfs2 DIO from buffer_head to iomap
Heming Zhao
reviewerv11 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]:
V11 changelog:
patch 1/3: no change
patch 2/3: no change except add "reviewed-by" tag patch 3/3:
- based on Joseph's review comments
- modify ocfs2_lock_map_blocks() and ocfs2_dio_zero_eof()
- based on sashiko's review comments
- add TODO comments in ocfs2_should_use_dio()
--------------
# 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 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/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 39 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/070 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/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 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/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 44 of 632 tests
## fio
no test
------
[1]:
https://gitee.com/anolis/storage-cloud-kernel/commit/ea3fe75e2060118ba093e90b1cd34fa382914b29
[2]:
https://sashiko.dev/#/patchset/20261103135605.22943-1-heming.zhao%40suse.com
[3]:
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
v8 patch set:
https://lore.kernel.org/ocfs2-devel/[email protected]/
v8 sashiko review:
https://sashiko.dev/#/patchset/20261119064029.11910-1-heming.zhao%40suse.com
v9 patch set:
https://lore.kernel.org/ocfs2-devel/[email protected]/
v9 sashiko review:
https://sashiko.dev/#/patchset/20261120060014.12087-1-heming.zhao%40suse.com
v10 patch set:
https://lore.kernel.org/ocfs2-devel/[email protected]/
v10 sashiko review:
https://sashiko.dev/#/patchset/20261124045501.10281-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 | 1705 ++++++++++++++++++++++++++++++++--------- fs/ocfs2/aops.h | 43 +- fs/ocfs2/extent_map.c | 34 +- fs/ocfs2/extent_map.h | 3 + fs/ocfs2/file.c | 345 ++++++++- fs/ocfs2/inode.h | 9 + fs/ocfs2/mmap.c | 2 +- fs/ocfs2/ocfs2.h | 25 +- fs/ocfs2/super.c | 3 + 10 files changed, 1741 insertions(+), 430 deletions(-)
Signature · 1 line
2.55.0
Heming Zhao
reviewerRFC PATCH v11 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 bridge 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 v11 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 e.g.: ocfs2_iomap_dio_ops_r_pr ocfs2_iomap_dio_ops_w_pr for PRMODE or ocfs2_iomap_dio_ops_w_ex for EXMODE. Also note that the ops_w_{pr|ex} are defined in the next patch.
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.
--- 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 v11 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().
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 | 1616 +++++++++++++++++++++++++++++++---------- fs/ocfs2/aops.h | 36 +- fs/ocfs2/extent_map.c | 34 +- fs/ocfs2/extent_map.h | 3 + fs/ocfs2/file.c | 285 +++++++- fs/ocfs2/inode.h | 9 + fs/ocfs2/mmap.c | 2 +- fs/ocfs2/super.c | 3 + 9 files changed, 1528 insertions(+), 461 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 +1223 -393
2,014 lines not on this page · raw message
fs/ocfs2/aops.h +6 -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,9 @@ 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); +void ocfs2_dio_clear_orphan (struct inode *inode);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 +257 -28
361 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 +3 -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);
@@ -1640,6 +1641,8 @@ static void ocfs2_inode_init_once(void *data) oi->ip_blkno =0ULL ; oi->ip_clusters =0 ; oi->ip_next_orphan =NULL ; + oi->ip_dio_orphan_pid =0 ; + oi->ip_dio_orphan_end =0ULL ;ocfs2_resv_init_once (&oi->ip_la_data_resv);
Signature · 1 line
2.55.0
A message that is not in the archive (sent off-list or not yet received): [email protected]
Joseph Qi
maintainerRe: [RFC PATCH v11 3/3] ocfs2: switch dio write path from buffer_head to iomap
Hi,
Quoted 7 lines · On 11/25/26 12:11 AM, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 7 potential issues to consider:
- [High] ocfs2: missingIOMAP_F_DIRTYinocfs2_iomap_begin()skipsO_DSYNCJBD2 commit
- [High] ocfs2: partial DIO failure inocfs2_dio_end_io()skips unwritten conversion
- [High] ocfs2: buffered and mmap writes do not wait in
Quoted 8 lines
ocfs2_unwritten_check()
- [High] ocfs2: orphan entry and block leak on failed or short DIO write
- [Medium] ocfs2: preallocated extents truncated inocfs2_dio_clear_orphan_locked()
- [Medium] ocfs2: per-cluster kvcalloc andip_lockscan inocfs2_alloc_write_ctxt()
- [Medium] ocfs2:ocfs2_should_use_dio()forces buffered I/O for sub-page DIO overwrites
Seems the above Sashiko review comments are mostly new.
Quoted 186 lines
--
commit
b5b57d5ded817baa5742865c157d167ab85796b4
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 handling unwritten extent conversion andi_sizeupdates inocfs2_dio_end_io().Quoted 4 lines
fs/ocfs2/aops.c +0
-0[ ... ]
Quoted 20 lines
fs/ocfs2/aops.c +12
-0@@ -914,10 +967,46 @@ static int ocfs2_alloc_write_ctxt(struct ocfs2_write_ctxt **wcp,if (!wc)return -ENOMEM ; + wc->w_desc = wc->w_desc_inline; + wc->w_folios = wc->w_folios_inline; + wc->w_owner = owner; wc->w_cpos = pos >> osb->s_clustersize_bits; wc->w_first_new_cpos =UINT_MAX ; cend = (pos + len -1 ) >> osb->s_clustersize_bits; wc->w_clen = cend - wc->w_cpos +1 ; + +/* +* Only a direct write can span more clusters than fit in a page. +* It needs the folios of the first and of the last cluster at most, +* see ocfs2_grab_folios_for_write(). +*/ +if (wc->w_clen >ARRAY_SIZE (wc->w_desc_inline)) { +if (WARN_ON_ONCE (type !=OCFS2_WRITE_DIRECT )) { +ocfs2_release_write_ctxt (wc);fs/ocfs2/aops.c static int ocfs2_alloc_write_ctxt(struct ocfs2_write_ctxt **wcp, · line 995
Quoted 10 lines
fs/ocfs2/aops.c +10
-0+return -EINVAL ; + } + wc->w_desc =kvcalloc (wc->w_clen,sizeof (*wc->w_desc), +GFP_NOFS ); +if (!wc->w_desc) { + wc->w_desc = wc->w_desc_inline; +ocfs2_release_write_ctxt (wc); +return -ENOMEM ; + } + }[Severity: Medium]
Does a direct write need to allocate oneocfs2_write_cluster_descper cluster inwc->w_descand callocfs2_unwritten_check()on every cluster?Before calling
ocfs2_write_begin_nolock(),ocfs2_dio_wr_map_blocks()already clampsmap->lenwithocfs2_map_blocks()to a single contiguous extent record or hole. With 4KB clusters and a 16MB mapping (OCFS2_DIO_MAX_MAP_BYTES),wc->w_clenis 4096, so every mapping call allocates and frees a 96KB (order-5) array underocfs2_inode_lock()andip_alloc_sem, andocfs2_populate_write_desc()takesoi->ip_lock4096 times to scanoi->ip_unwritten_list(even for holes wherec_newis 1).[ ... ]
Quoted 28 lines
fs/ocfs2/aops.c +25
-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); + 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 ; +/* 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 +/* +* Not ours. A direct write never gets here, it waits fs/ocfs2/aops.c static void ocfs2_set_target_boundaries(struct ocfs2_super *osb, · line 1809
Quoted 10 lines
fs/ocfs2/aops.c +8
-0+* for the entries of the other dios before it maps, +* see ocfs2_dio_wr_map_blocks(). +*/ +WARN_ON_ONCE (wc->w_type ==OCFS2_WRITE_DIRECT ); + }else if (wc->w_type ==OCFS2_WRITE_DIRECT ) { +/* This direct write will doing zero. */ + desc->c_clear_unwritten =0 ; }spin_unlock (&oi->ip_lock); +}[Severity: High]
What happens when a buffered write or mmappage_mkwritefault hits a cluster that an in-flight direct write has placed onoi->ip_unwritten_list?Unlike
ocfs2_dio_wr_map_blocks(), which waits onoi->ip_unwritten_wqfor foreign unwritten entries to finish, buffered and mmap writes cleardesc->c_needs_zeroanddesc->c_clear_unwrittenhere and proceed assuming the direct write has zeroed the cluster and will convert it to written:1. If the in-flight direct write later fails (for example with
-EFAULTon a subsequent mapping) or ends short before this cluster,ocfs2_dio_end_io()skips conversion of that cluster, whileocfs2_write_cluster()also skippedocfs2_mark_extent_written()becausec_clear_unwrittenwas 0. Once the dirty page cache folio is written back and evicted, the cluster remains markedOCFS2_EXT_UNWRITTENon disk and subsequent reads return zeroes instead of the written data.2. When
cluster_size>PAGE_SIZE,ocfs2_grab_folios_direct()only zeroes folios outside the direct write's [target_index,end_index] up tomax(user_pos + user_len, i_size). If a concurrent buffered write extendsi_sizefurther within the same unwritten cluster, or does a partial-page write inside [target_index,end_index] where new = 0 causesocfs2_map_folio_blocks()to issuebh_read_nowait()on the unwritten block, unzeroed disk blocks are exposed once the direct write marks the cluster written.[ ... ]
Quoted 5 lines
fs/ocfs2/aops.c +5
-0+static void ocfs2_dio_clear_orphan_locked (struct inode *inode, +struct buffer_head *di_bh) +{ +struct ocfs2_super *osb =OCFS2_SB (inode->i_sb); +struct ocfs2_inode_info *oi =OCFS2_I (inode);fs/ocfs2/aops.c static int ocfs2_dio_wr_get_block(struct inode *inode, sector_t iblock, · line 3067
Quoted 10 lines
fs/ocfs2/aops.c +10
-0+struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; +loff_t end =READ_ONCE (oi->ip_dio_orphan_end); +int ret =0 ; + +/* +* The write has not reached the end it asked for. Free whatever it +* allocated beyond i_size before the orphan entry. +*/ +if (i_size_read (inode) < end) + ret =ocfs2_truncate_file (inode, di_bh,i_size_read (inode));[Severity: Medium]
As noted in the TODO comment above this function, if a file has unwritten extents preallocated beyondi_sizeviafallocate(FALLOC_FL_KEEP_SIZE), won't any extending direct write that ends short, fails before submitting I/O, or bounces to buffered I/O and writes short truncate away all pre-existing preallocated clusters beyondi_size?In addition, because
ocfs2_file_write_iter()checksocfs2_clusters_for_bytes(..., iocb->ki_pos + count)againsti_size_read()rather than against the inode's allocated clusters, every extending direct write into space already preallocated withFALLOC_FL_KEEP_SIZEunnecessarily adds and removes the inode in the slot's global orphan directory.[ ... ]
Quoted 9 lines
fs/ocfs2/aops.c +3
-1@@ -2567,10 +3311,11 @@ static int ocfs2_iomap_begin(struct inode *inode, loff_t offset, loff_t length, map.flags =0 ;if (flags &IOMAP_WRITE ) { -return -EOPNOTSUPP ; + ret =ocfs2_dio_wr_map_blocks (inode, &map, flags, owner); + }else { + ret =ocfs2_lock_map_blocks (inode, &map,0 , flags &IOMAP_NOWAIT ); }[ ... ]
fs/ocfs2/aops.c
Quoted 5 lines
fs/ocfs2/aops.c +0
-0if (map.flags &OCFS2_MAP_NEW ) iomap->flags |=IOMAP_F_NEW ;return 0 ; }[Severity: High]
Shouldocfs2_iomap_begin()setIOMAP_F_DIRTYwhen the write extendsi_sizeor when the inode has uncommitted metadata transactions in JBD2?
Quoted 12 lines
For
O_DSYNC/RWF_DSYNCdirect writes,ocfs2_file_write_iter()skips its post-writejbd2_journal_force_commit()whenuse_diois true and relies on iomap. Whenocfs2_dio_wr_map_blocks()returns an already-allocated extent (IOMAP_MAPPEDwithout IOMAP_F_NEW)—for example when extending an unalignedi_sizewithin an already-allocated tail block (lblk <eof_blk), or afterocfs2_prepare_inode_for_write()performed refcount CoW—and the block device supportsREQ_FUA, iomap sees neitherIOMAP_F_NEWnorIOMAP_F_DIRTY, issues aREQ_FUAbio, clearsIOMAP_DIO_NEED_SYNC, and skipsgeneric_write_sync()(ocfs2_sync_file()). The write then returns success to userspace while the JBD2 transaction updatingi_sizeor extent metadata is still uncommitted.
Seems we have to do it like:
if ((flags & IOMAP_WRITE) && offset + length > i_size_read(inode)) iomap->flags |= IOMAP_F_DIRTY;
Quoted 258 lines
[ ... ]
Quoted 21 lines
fs/ocfs2/aops.c +21
-0+static int ocfs2_dio_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; +bool recorded = error || (flags &IOMAP_DIO_UNWRITTEN ); +LIST_HEAD (unwritten); +int ret =0 ; + +if (recorded) +ocfs2_dio_collect_unwritten (inode, iocb, &unwritten); + +if (error) +mlog_ratelimited (ML_ERROR ,"Direct IO failed, bytes = %lld errno:%d" , + (long long )size, error); + +/* bounced back to buffered I/O, leave the rw_lock held */ +if (size ==0 && error ==0 ) { +ocfs2_free_unwritten_list (inode, &unwritten); +return 0 ; + }fs/ocfs2/aops.c int ocfs2_iomap_next(const struct iomap_iter *iter, struct iomap *iomap, · line 3414
Quoted 10 lines
fs/ocfs2/aops.c +10
-0+ +if (size && error ==0 && + (!list_empty (&unwritten) || + (offset + size >i_size_read (inode)))) { + ret =ocfs2_dio_end_io_write (inode, offset, size, &unwritten); +if (ret <0 ) +mlog_errno (ret); + } + +ocfs2_free_unwritten_list (inode, &unwritten);[Severity: High]
What happens if a multi-mapping direct write to unwritten or newly allocated clusters completes some I/O (size > 0) and then fails on a later mapping or bio (error != 0), for example with-ENOSPCinocfs2_dio_wr_map_blocks()or-EFAULTinbio_iov_iter_get_pages()?During
ocfs2_dio_wr_map_blocks(), any unaligned head or tail folios in the page cache for the first and last clusters of the earlier mapping were already zeroed, mapped to the cluster's physical blocks, and marked dirty and uptodate viablock_commit_write(). Because error != 0,ocfs2_dio_end_io()skipsocfs2_dio_end_io_write()and discards the unwritten list, andiomap_dio_complete()skipskiocb_invalidate_post_direct_write(). That leaves dirty, mapped page cache buffers pointing at an extent that is still markedOCFS2_EXT_UNWRITTENon disk.Quoted 4 lines
fs/ocfs2/file.c +0
-0[ ... ]
Quoted 11 lines
fs/ocfs2/file.c +7
-0@@ -2417,9 +2426,52 @@ static bool ocfs2_should_use_dio(struct kiocb *iocb, struct iov_iter *iter, !ocfs2_supports_append_dio(osb))return false; +/* +* A direct write which does not cover whole pages leaves the rest of +* the page untouched. iomap only zeroes the padding up to the file +* system block size, so on a file system with a block size smaller +* than the page size the untouched blocks would expose stale data +* once the cluster is marked written. Let buffered I/O handle it. +* fs/ocfs2/file.c static bool ocfs2_should_use_dio(struct kiocb *iocb, struct iov_iter *iter, · line 2445
Quoted 10 lines
fs/ocfs2/file.c +8
-0+* TODO: Avoid unnecessarily demoting in-place sub-page overwrites +* to buffered I/O when i_blocksize(inode) < PAGE_SIZE. +*/ +if (iov_iter_rw (iter) ==WRITE &&i_blocksize (inode) <PAGE_SIZE && + (!IS_ALIGNED (iocb->ki_pos,PAGE_SIZE ) || + !IS_ALIGNED (iocb->ki_pos + iter->count,PAGE_SIZE ))) +return false; +return true; }[Severity: Medium]
Wheni_blocksize(inode)<PAGE_SIZE(including all OCFS2 filesystems on 16KB or 64KBPAGE_SIZEkernels), doesn't checking page alignment inocfs2_should_use_dio()demote all sub-pageO_DIRECToverwrites of already-written blocks to synchronous buffered I/O?Because
use_diobecomes false before block mapping is checked, pure in-place overwrites take an exclusive EX clusterrw_lockinstead of a shared PR lock under coherency=buffered, rejectIOCB_NOWAITwrites with-EAGAIN, and fall back togeneric_perform_write()and page-cache writeback/invalidation. Returning-ENOTBLKfromocfs2_dio_wr_map_blocks()only when the fast-path overwrite check misses would keep in-place sub-page overwrites on the direct I/O path.[ ... ]
Quoted 93 lines
fs/ocfs2/file.c +92
-0+if (use_dio) { +if (!IS_ALIGNED (iocb->ki_pos |iov_iter_alignment (from), + dio_align)) { + ret = -EINVAL ; + goto out; + } +/* +* Extending the file requires cluster locks and +* transactions, which IOCB_NOWAIT forbids. +*/ +if (nowait && + ((iocb->ki_pos + count) >i_size_read (inode))) { + ret = -EAGAIN ; + goto out; + } + +if (iocb->ki_pos >i_size_read (inode) && +fault_in_iov_iter_readable (from, count)) { + ret = -EFAULT ; + goto out; + } + +if ((iocb->ki_pos + count) >i_size_read (inode)) { +/* +* 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; + } +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; + +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 ; + }[ ... ]
Quoted 16 lines
out:
fs/ocfs2/file.c +11
-1+/* +* IOMAP_DIO_FORCE_WAIT made an extending dio complete synchronously, +* so its end_io has already updated i_size. +*/ if (saved_ki_complete)xchg (&iocb->ki_complete, saved_ki_complete); -if (rw_level !=-1 ) +if (rw_level !=-1 ) { +/* +* The orphan entry this write added is still there when no io +* was issued at all, or when the write bailed out before +* submitting it. The conversion in ocfs2_dio_end_io_write() +* has removed it on the success path; a failed io is left to +* the recovery. Remove it here for the former, while the fs/ocfs2/file.c static ssize_t ocfs2_file_write_iter(struct kiocb *iocb, · line 2807
Quoted 10 lines
fs/ocfs2/file.c +9
-0+* rw_lock is still held +*/ +ocfs2_dio_clear_orphan (inode);ocfs2_rw_unlock (inode, rw_level); + } + +/* The marker must not outlive this write. */ +if (READ_ONCE (OCFS2_I (inode)->ip_dio_orphan_pid) == +task_pid_nr (current)) +WRITE_ONCE (OCFS2_I (inode)->ip_dio_orphan_pid,0 );[Severity: High]
Does an extending direct write that fails inside__iomap_dio_rw()afterinode_dio_begin()(or ends short at or belowi_sizewithout unwritten extents) leave the inode orphaned and any newly allocated clusters beyondi_sizeleaked?For example, when appending at
ki_pos==i_sizewith an unmapped user buffer,fault_in_iov_iter_readable()is skipped becauseki_pos>i_sizeis false.ocfs2_add_inode_to_orphan()adds the dio- orphan entry and setsOCFS2_DIO_ORPHANED_FLon disk, and inside__iomap_dio_rw(),ocfs2_dio_wr_map_blocks()allocates unwritten clusters beforebio_iov_iter_get_pages()fails with-EFAULT. BecauseIOMAP_DIO_FORCE_WAITis set,__iomap_dio_rw()returns the dio pointer andiomap_dio_complete()runsocfs2_dio_end_io(), which skipsocfs2_dio_end_io_write()on error, unlocksrw_lock, and returns-EFAULT.ocfs2_file_write_iter()then setsrw_level= -1, so at out: it skipsocfs2_dio_clear_orphan()and resetsip_dio_orphan_pidto 0.The same happens if an extending write starting below
i_sizeon an already-written prefix stops short at or beforei_size(wherelist_empty(&unwritten)and offset + size <=i_size_read(inode), soocfs2_dio_end_io()skipsocfs2_dio_end_io_write()). WithOCFS2_DIO_ORPHANED_FLleft set on the dinode, subsequent unlink cannot wipe the inode inocfs2_delete_inode():fs/ocfs2/inode.c:ocfs2_delete_inode() { ... if (unlikely(di->i_flags & cpu_to_le32(OCFS2_DIO_ORPHANED_FL))) { ocfs2_cleanup_delete_inode(inode, 0); goto bail_unlock_inode; } ... }Because background and clean-mount orphan scans skip dio- entries (
ORPHAN_NO_NEED_TRUNCATE), the unlinked inode, its orphan entry, and the clusters allocated beyondi_sizeremain on disk until an unclean recovery or another extending direct write on the same inode triggers orphan cleanup.
Heming Zhao
reviewerRe: [RFC PATCH v11 3/3] ocfs2: switch dio write path from buffer_head to iomap
Quoted 19 lines · Joseph Qi
Hi,
Quoted 7 lines · On 11/25/26 12:11 AM, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 7 potential issues to consider:
- [High] ocfs2: missingIOMAP_F_DIRTYinocfs2_iomap_begin()skipsO_DSYNCJBD2 commit
- [High] ocfs2: partial DIO failure inocfs2_dio_end_io()skips unwritten conversion
- [High] ocfs2: buffered and mmap writes do not wait inQuoted 8 lines
ocfs2_unwritten_check()
- [High] ocfs2: orphan entry and block leak on failed or short DIO write
- [Medium] ocfs2: preallocated extents truncated inocfs2_dio_clear_orphan_locked()
- [Medium] ocfs2: per-cluster kvcalloc andip_lockscan inocfs2_alloc_write_ctxt()
- [Medium] ocfs2:ocfs2_should_use_dio()forces buffered I/O for sub-page DIO overwrites
Quoted 63 lines
Seems the above Sashiko review comments are mostly new. >
Quoted 62 lines
--
commit
b5b57d5ded817baa5742865c157d167ab85796b4
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 handling unwritten extent conversion andi_sizeupdates inocfs2_dio_end_io().Quoted 4 lines
fs/ocfs2/aops.c +0
-0[ ... ]
Quoted 20 lines
fs/ocfs2/aops.c +12
-0@@ -914,10 +967,46 @@ static int ocfs2_alloc_write_ctxt(struct ocfs2_write_ctxt **wcp,if (!wc)return -ENOMEM ; + wc->w_desc = wc->w_desc_inline; + wc->w_folios = wc->w_folios_inline; + wc->w_owner = owner; wc->w_cpos = pos >> osb->s_clustersize_bits; wc->w_first_new_cpos =UINT_MAX ; cend = (pos + len -1 ) >> osb->s_clustersize_bits; wc->w_clen = cend - wc->w_cpos +1 ; + +/* +* Only a direct write can span more clusters than fit in a page. +* It needs the folios of the first and of the last cluster at most, +* see ocfs2_grab_folios_for_write(). +*/ +if (wc->w_clen >ARRAY_SIZE (wc->w_desc_inline)) { +if (WARN_ON_ONCE (type !=OCFS2_WRITE_DIRECT )) { +ocfs2_release_write_ctxt (wc);fs/ocfs2/aops.c static int ocfs2_alloc_write_ctxt(struct ocfs2_write_ctxt **wcp, · line 995
Quoted 10 lines
fs/ocfs2/aops.c +10
-0+return -EINVAL ; + } + wc->w_desc =kvcalloc (wc->w_clen,sizeof (*wc->w_desc), +GFP_NOFS ); +if (!wc->w_desc) { + wc->w_desc = wc->w_desc_inline; +ocfs2_release_write_ctxt (wc); +return -ENOMEM ; + } + }[Severity: Medium]
Does a direct write need to allocate oneocfs2_write_cluster_descper cluster inwc->w_descand callocfs2_unwritten_check()on every cluster?Before calling
ocfs2_write_begin_nolock(),ocfs2_dio_wr_map_blocks()already clampsmap->lenwithocfs2_map_blocks()to a single contiguous extent record or hole. With 4KB clusters and a 16MB mapping (OCFS2_DIO_MAX_MAP_BYTES),wc->w_clenis 4096, so every mapping call allocates and frees a 96KB (order-5) array underocfs2_inode_lock()andip_alloc_sem, andocfs2_populate_write_desc()takesoi->ip_lock4096 times to scanoi->ip_unwritten_list(even for holes wherec_newis 1).
This is performance issue. The fio results show that the patched code achieves the same speed/bandwidth as the original code.
Let's do it in another patch set.
Quoted 59 lines
Quoted 59 lines
[ ... ]
Quoted 28 lines
fs/ocfs2/aops.c +25
-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); + 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 ; +/* 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 +/* +* Not ours. A direct write never gets here, it waits fs/ocfs2/aops.c static void ocfs2_set_target_boundaries(struct ocfs2_super *osb, · line 1809
Quoted 10 lines
fs/ocfs2/aops.c +8
-0+* for the entries of the other dios before it maps, +* see ocfs2_dio_wr_map_blocks(). +*/ +WARN_ON_ONCE (wc->w_type ==OCFS2_WRITE_DIRECT ); + }else if (wc->w_type ==OCFS2_WRITE_DIRECT ) { +/* This direct write will doing zero. */ + desc->c_clear_unwritten =0 ; }spin_unlock (&oi->ip_lock); +}[Severity: High]
What happens when a buffered write or mmappage_mkwritefault hits a cluster that an in-flight direct write has placed onoi->ip_unwritten_list?Unlike
ocfs2_dio_wr_map_blocks(), which waits onoi->ip_unwritten_wqfor foreign unwritten entries to finish, buffered and mmap writes cleardesc->c_needs_zeroanddesc->c_clear_unwrittenhere and proceed assuming the direct write has zeroed the cluster and will convert it to written:1. If the in-flight direct write later fails (for example with
-EFAULTon a subsequent mapping) or ends short before this cluster,ocfs2_dio_end_io()skips conversion of that cluster, whileocfs2_write_cluster()also skippedocfs2_mark_extent_written()becausec_clear_unwrittenwas 0. Once the dirty page cache folio is written back and evicted, the cluster remains markedOCFS2_EXT_UNWRITTENon disk and subsequent reads return zeroes instead of the written data.
Quoted 9 lines
Quoted 9 lines
2. When
cluster_size>PAGE_SIZE,ocfs2_grab_folios_direct()only zeroes folios outside the direct write's [target_index,end_index] up tomax(user_pos + user_len, i_size). If a concurrent buffered write extendsi_sizefurther within the same unwritten cluster, or does a partial-page write inside [target_index,end_index] where new = 0 causesocfs2_map_folio_blocks()to issuebh_read_nowait()on the unwritten block, unzeroed disk blocks are exposed once the direct write marks the cluster written.
I will ignore above issue.
This issue predates this patch and is left unfixed. Additionally, triggering it would require concurrent DIO and buffered I/O or mmap writes on the same inode, which is not a realistic scenario in production env.
Quoted 25 lines
Quoted 25 lines
[ ... ]
Quoted 5 lines
fs/ocfs2/aops.c +5
-0+static void ocfs2_dio_clear_orphan_locked (struct inode *inode, +struct buffer_head *di_bh) +{ +struct ocfs2_super *osb =OCFS2_SB (inode->i_sb); +struct ocfs2_inode_info *oi =OCFS2_I (inode);fs/ocfs2/aops.c static int ocfs2_dio_wr_get_block(struct inode *inode, sector_t iblock, · line 3067
Quoted 10 lines
fs/ocfs2/aops.c +10
-0+struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; +loff_t end =READ_ONCE (oi->ip_dio_orphan_end); +int ret =0 ; + +/* +* The write has not reached the end it asked for. Free whatever it +* allocated beyond i_size before the orphan entry. +*/ +if (i_size_read (inode) < end) + ret =ocfs2_truncate_file (inode, di_bh,i_size_read (inode));[Severity: Medium]
As noted in the TODO comment above this function, if a file has unwritten extents preallocated beyondi_sizeviafallocate(FALLOC_FL_KEEP_SIZE), won't any extending direct write that ends short, fails before submitting I/O, or bounces to buffered I/O and writes short truncate away all pre-existing preallocated clusters beyondi_size?
Quoted 6 lines
Quoted 6 lines
In addition, because
ocfs2_file_write_iter()checksocfs2_clusters_for_bytes(..., iocb->ki_pos + count)againsti_size_read()rather than against the inode's allocated clusters, every extending direct write into space already preallocated withFALLOC_FL_KEEP_SIZEunnecessarily adds and removes the inode in the slot's global orphan directory.
I already added a "TODO" tag in the comments and will ignore this issue. btw, this issue already existed before this patch set.
Quoted 38 lines
Quoted 25 lines
[ ... ]
Quoted 9 lines
fs/ocfs2/aops.c +3
-1@@ -2567,10 +3311,11 @@ static int ocfs2_iomap_begin(struct inode *inode, loff_t offset, loff_t length, map.flags =0 ;if (flags &IOMAP_WRITE ) { -return -EOPNOTSUPP ; + ret =ocfs2_dio_wr_map_blocks (inode, &map, flags, owner); + }else { + ret =ocfs2_lock_map_blocks (inode, &map,0 , flags &IOMAP_NOWAIT ); }[ ... ]
Quoted 5 lines
if (map.flags & OCFS2_MAP_NEW) iomap->flags |= IOMAP_F_NEW; return 0; }[Severity: High]
Shouldocfs2_iomap_begin()setIOMAP_F_DIRTYwhen the write extendsi_sizeor when the inode has uncommitted metadata transactions in JBD2?Quoted 12 lines
For
O_DSYNC/RWF_DSYNCdirect writes,ocfs2_file_write_iter()skips its post-writejbd2_journal_force_commit()whenuse_diois true and relies on iomap. Whenocfs2_dio_wr_map_blocks()returns an already-allocated extent (IOMAP_MAPPEDwithout IOMAP_F_NEW)—for example when extending an unalignedi_sizewithin an already-allocated tail block (lblk <eof_blk), or afterocfs2_prepare_inode_for_write()performed refcount CoW—and the block device supportsREQ_FUA, iomap sees neitherIOMAP_F_NEWnorIOMAP_F_DIRTY, issues aREQ_FUAbio, clearsIOMAP_DIO_NEED_SYNC, and skipsgeneric_write_sync()(ocfs2_sync_file()). The write then returns success to userspace while the JBD2 transaction updatingi_sizeor extent metadata is still uncommitted.
Quoted 5 lines
Seems we have to do it like:
if ((flags & IOMAP_WRITE) && offset + length > i_size_read(inode)) iomap->flags |= IOMAP_F_DIRTY;
My fix (copied from xfs and ext4):
@@ -3311,6 +3311,15 @@ static int ocfs2_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
map.flags = 0;
if (flags & IOMAP_WRITE) {
+ /*
+ * Writes that span EOF might trigger an IO size update on
+ * completion, so consider them to be dirty for the purposes
+ * of O_DSYNC even if there is no other metadata changes
+ * pending or have been made here.
+ */
+ if (offset + length > i_size_read(inode))
+ iomap->flags |= IOMAP_F_DIRTY;
+
ret = ocfs2_dio_wr_map_blocks(inode, &map, flags, owner);
} else {
ret = ocfs2_lock_map_blocks(inode, &map, 0, flags & IOMAP_NOWAIT);Quoted 40 lines
Quoted 40 lines
[ ... ]
Quoted 21 lines
fs/ocfs2/aops.c +21
-0+static int ocfs2_dio_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; +bool recorded = error || (flags &IOMAP_DIO_UNWRITTEN ); +LIST_HEAD (unwritten); +int ret =0 ; + +if (recorded) +ocfs2_dio_collect_unwritten (inode, iocb, &unwritten); + +if (error) +mlog_ratelimited (ML_ERROR ,"Direct IO failed, bytes = %lld errno:%d" , + (long long )size, error); + +/* bounced back to buffered I/O, leave the rw_lock held */ +if (size ==0 && error ==0 ) { +ocfs2_free_unwritten_list (inode, &unwritten); +return 0 ; + }fs/ocfs2/aops.c int ocfs2_iomap_next(const struct iomap_iter *iter, struct iomap *iomap, · line 3414
Quoted 10 lines
fs/ocfs2/aops.c +10
-0+ +if (size && error ==0 && + (!list_empty (&unwritten) || + (offset + size >i_size_read (inode)))) { + ret =ocfs2_dio_end_io_write (inode, offset, size, &unwritten); +if (ret <0 ) +mlog_errno (ret); + } + +ocfs2_free_unwritten_list (inode, &unwritten);[Severity: High]
What happens if a multi-mapping direct write to unwritten or newly allocated clusters completes some I/O (size > 0) and then fails on a later mapping or bio (error != 0), for example with-ENOSPCinocfs2_dio_wr_map_blocks()or-EFAULTinbio_iov_iter_get_pages()?
Quoted 9 lines
Quoted 9 lines
During
ocfs2_dio_wr_map_blocks(), any unaligned head or tail folios in the page cache for the first and last clusters of the earlier mapping were already zeroed, mapped to the cluster's physical blocks, and marked dirty and uptodate viablock_commit_write(). Because error != 0,ocfs2_dio_end_io()skipsocfs2_dio_end_io_write()and discards the unwritten list, andiomap_dio_complete()skipskiocb_invalidate_post_direct_write(). That leaves dirty, mapped page cache buffers pointing at an extent that is still markedOCFS2_EXT_UNWRITTENon disk.
I will try to fix this issue in next version.
the idea is to first write back the dirty cache pages to disk, then invalidate/drop the clean (written-back) pages.
btw, this issue is also existing in buffer_head path.
the old ocfs2_dio_end_io() just calls ocfs2_dio_free_write_ctx() when "bytes <= 0", which also doesn't release the dirty pages.
Quoted 36 lines
Quoted 36 lines
Quoted 5 lines
fs/ocfs2/file.c +0
-0[ ... ]
Quoted 11 lines
fs/ocfs2/file.c +7
-0@@ -2417,9 +2426,52 @@ static bool ocfs2_should_use_dio(struct kiocb *iocb, struct iov_iter *iter, !ocfs2_supports_append_dio(osb))return false; +/* +* A direct write which does not cover whole pages leaves the rest of +* the page untouched. iomap only zeroes the padding up to the file +* system block size, so on a file system with a block size smaller +* than the page size the untouched blocks would expose stale data +* once the cluster is marked written. Let buffered I/O handle it. +* fs/ocfs2/file.c static bool ocfs2_should_use_dio(struct kiocb *iocb, struct iov_iter *iter, · line 2445
Quoted 10 lines
fs/ocfs2/file.c +8
-0+* TODO: Avoid unnecessarily demoting in-place sub-page overwrites +* to buffered I/O when i_blocksize(inode) < PAGE_SIZE. +*/ +if (iov_iter_rw (iter) ==WRITE &&i_blocksize (inode) <PAGE_SIZE && + (!IS_ALIGNED (iocb->ki_pos,PAGE_SIZE ) || + !IS_ALIGNED (iocb->ki_pos + iter->count,PAGE_SIZE ))) +return false; +return true; }[Severity: Medium]
Wheni_blocksize(inode)<PAGE_SIZE(including all OCFS2 filesystems on 16KB or 64KBPAGE_SIZEkernels), doesn't checking page alignment inocfs2_should_use_dio()demote all sub-pageO_DIRECToverwrites of already-written blocks to synchronous buffered I/O?
Quoted 8 lines
Quoted 8 lines
Because
use_diobecomes false before block mapping is checked, pure in-place overwrites take an exclusive EX clusterrw_lockinstead of a shared PR lock under coherency=buffered, rejectIOCB_NOWAITwrites with-EAGAIN, and fall back togeneric_perform_write()and page-cache writeback/invalidation. Returning-ENOTBLKfromocfs2_dio_wr_map_blocks()only when the fast-path overwrite check misses would keep in-place sub-page overwrites on the direct I/O path.
I already added a "TODO" tag in the comments and will ignore this issue.
Quoted 159 lines
Quoted 159 lines
[ ... ]
Quoted 93 lines
fs/ocfs2/file.c +92
-0+if (use_dio) { +if (!IS_ALIGNED (iocb->ki_pos |iov_iter_alignment (from), + dio_align)) { + ret = -EINVAL ; + goto out; + } +/* +* Extending the file requires cluster locks and +* transactions, which IOCB_NOWAIT forbids. +*/ +if (nowait && + ((iocb->ki_pos + count) >i_size_read (inode))) { + ret = -EAGAIN ; + goto out; + } + +if (iocb->ki_pos >i_size_read (inode) && +fault_in_iov_iter_readable (from, count)) { + ret = -EFAULT ; + goto out; + } + +if ((iocb->ki_pos + count) >i_size_read (inode)) { +/* +* 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; + } +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; + +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 ; + }[ ... ]
Quoted 16 lines
out:
fs/ocfs2/file.c +11
-1+/* +* IOMAP_DIO_FORCE_WAIT made an extending dio complete synchronously, +* so its end_io has already updated i_size. +*/ if (saved_ki_complete)xchg (&iocb->ki_complete, saved_ki_complete); -if (rw_level !=-1 ) +if (rw_level !=-1 ) { +/* +* The orphan entry this write added is still there when no io +* was issued at all, or when the write bailed out before +* submitting it. The conversion in ocfs2_dio_end_io_write() +* has removed it on the success path; a failed io is left to +* the recovery. Remove it here for the former, while the fs/ocfs2/file.c static ssize_t ocfs2_file_write_iter(struct kiocb *iocb, · line 2807
Quoted 10 lines
fs/ocfs2/file.c +9
-0+* rw_lock is still held +*/ +ocfs2_dio_clear_orphan (inode);ocfs2_rw_unlock (inode, rw_level); + } + +/* The marker must not outlive this write. */ +if (READ_ONCE (OCFS2_I (inode)->ip_dio_orphan_pid) == +task_pid_nr (current)) +WRITE_ONCE (OCFS2_I (inode)->ip_dio_orphan_pid,0 );[Severity: High]
Does an extending direct write that fails inside__iomap_dio_rw()afterinode_dio_begin()(or ends short at or belowi_sizewithout unwritten extents) leave the inode orphaned and any newly allocated clusters beyondi_sizeleaked?For example, when appending at
ki_pos==i_sizewith an unmapped user buffer,fault_in_iov_iter_readable()is skipped becauseki_pos>i_sizeis false.ocfs2_add_inode_to_orphan()adds the dio- orphan entry and setsOCFS2_DIO_ORPHANED_FLon disk, and inside__iomap_dio_rw(),ocfs2_dio_wr_map_blocks()allocates unwritten clusters beforebio_iov_iter_get_pages()fails with-EFAULT. BecauseIOMAP_DIO_FORCE_WAITis set,__iomap_dio_rw()returns the dio pointer andiomap_dio_complete()runsocfs2_dio_end_io(), which skipsocfs2_dio_end_io_write()on error, unlocksrw_lock, and returns-EFAULT.ocfs2_file_write_iter()then setsrw_level= -1, so at out: it skipsocfs2_dio_clear_orphan()and resetsip_dio_orphan_pidto 0.The same happens if an extending write starting below
i_sizeon an already-written prefix stops short at or beforei_size(wherelist_empty(&unwritten)and offset + size <=i_size_read(inode), soocfs2_dio_end_io()skipsocfs2_dio_end_io_write()). WithOCFS2_DIO_ORPHANED_FLleft set on the dinode, subsequent unlink cannot wipe the inode inocfs2_delete_inode():fs/ocfs2/inode.c:ocfs2_delete_inode() { ... if (unlikely(di->i_flags & cpu_to_le32(OCFS2_DIO_ORPHANED_FL))) { ocfs2_cleanup_delete_inode(inode, 0); goto bail_unlock_inode; } ... }
Quoted 7 lines
Quoted 6 lines
Because background and clean-mount orphan scans skip dio- entries (
ORPHAN_NO_NEED_TRUNCATE), the unlinked inode, its orphan entry, and the clusters allocated beyondi_sizeremain on disk until an unclean recovery or another extending direct write on the same inode triggers orphan cleanup.
Don't agree. For above scenario, the patch keeps old logic. There are two additional places where the orphaned inode is removed:
- On the next write to the inode, ocfs2_add_inode_to_orphan() will check OCFS2_DIO_ORPHANED_FL and remove the orphaned entry.
- The recovery path will call ocfs2_recover_orphans() to do the removal.
Thanks,
Heming