From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=115.124.30.131 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796288123; b=iuW8r4dLXpHx/obqe6JiOHdFadaYNTxUeb/h9lvOa0K8pw9c7ARJMjzxkn8cDIr0QL0e xMBRInNX/mXp90GihgcTx5vOpqCoExHYIZFJ/L1IA1shiwU3iJAqNtwd+JyZeYGPhIt6+ EHIrgbmGjO1xbVgYraKZLZz05qpclKAm0N9rmizf6W38/KcqS0fWqPf9HJq8nz6NRDQQW +iTnxsiLiIjNCGsc2KfcKYtc5j5qZK7GDl3pBHB12LzXV6y2UIWrqfi+RsEF+mwH8JPWV BrlghP1n/WmyKINuPsE8DOsR2h1ea1wDAK2ooxNP9VMQ4Co3pVTnx7igyAzrUafqLXw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796288123; h=DMARC-Filter:DKIM-Signature:X-Alimail-AntiSpam:Received:Message-ID: Date:MIME-Version:User-Agent:Subject:To:Cc:References:From: In-Reply-To:Content-Type:Content-Transfer-Encoding; bh=CUnlWXJm7endCG6BSkHZfVF6k0WatrDEqso44kn/z8I=; b=tefeYoRNfwzWEWD80NCSiMsKejrRO3AxrgYGVow7HwOEKThrbxpiqRqzRb8TLgRXoLss bmguhDRZCj3OOBOouQfqqRpl1U2SOzMppECvrjkBsS9I96VXRJFjERoqjhqYDoMu/txFL re5i+HX2u0N290OxlZTkEdGDfyiuddCheUOZx6viYfSoodiUHgkDaYLLW2U1HR9YZtGJh JuL+O/TEluqKS6DgxF32fT06gARvcuVCDumxSL5OHMFXhVqtaxkqE96Bpi1k3xGfjTvni DYpfP+1TS3oQbKngAkIifWhTlNCTVfk225P8OtXmVdf3LAZZZw8792OYi0l+N1lsLUQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; dkim=pass header.d=linux.alibaba.com header.i=@linux.alibaba.com header.a=rsa-sha256 header.s=default header.b=Rz2l1ehY; arc=none smtp.remote-ip=115.124.30.131 Received: from out30-131.freemail.mail.aliyun.com (out30-131.freemail.mail.aliyun.com [115.124.30.131]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 620B8606A322 for ; Thu, 3 Dec 2026 08:55:22 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 620B8606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.a=rsa-sha256 header.s=default header.b=Rz2l1ehY DKIM-Signature:v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1796288114; h=Message-ID:Date:MIME-Version:Subject:To:From:Content-Type; bh=CUnlWXJm7endCG6BSkHZfVF6k0WatrDEqso44kn/z8I=; b=Rz2l1ehYMTfHeG0TFr5gOyHqNYQA8rEpeCCrih0OaGbhVy86XvE5f/AGvPW6BZpsnvrhjBDTMEBNyOcBDY8XHEI9uqzRaoWBXfmnPiVZswdqdb0FWMqkqavWQguw2jj4Gfylkj+Mnv4Q7MTNY2ps0Qsbh1DzgTVfeeZN3iy7Tt4= X-Alimail-AntiSpam:AC=PASS;BC=-1|-1;BR=01201311R171e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=maildocker-contentspam033065142238;MF=joseph.qi@linux.alibaba.com;NM=1;PH=DS;RN=7;SR=0;TI=SMTPD_---0XGDlCLj_1796288113; Received: from 30.221.129.217(mailfrom:joseph.qi@linux.alibaba.com fp:SMTPD_---0XGDlCLj_1796288113 cluster:ay36) by smtp.aliyun-inc.com; Thu, 03 Dec 2026 16:55:13 +0800 Message-ID: Date: Thu, 3 Dec 2026 16:55:13 +0800 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [RFC PATCH v15 07/10] ocfs2: switch dio write path from buffer_head to iomap To: Heming Zhao Cc: Andrew Morton , Mark Fasheh , Joel Becker , ocfs2-devel@lists.linux.dev, linux-kernel@vger.kernel.org, hch@lst.de References: <20261202143507.1754776-1-joseph.qi@linux.alibaba.com> <20261202143507.1754776-8-joseph.qi@linux.alibaba.com> From: Joseph Qi In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On 12/3/26 3:49 PM, Heming Zhao wrote: > On 12/2/26 22:35, Joseph Qi wrote: >> From: Heming Zhao >> >> Replace ocfs2_dio_wr_get_block() with ocfs2_dio_wr_map_blocks(), which >> 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.  ocfs2_iomap_begin() serves writes through it, >> and ocfs2_dio_end_io() converts the unwritten extents the write recorded >> and updates the inode size. >> >> In the buffer_head path, ocfs2_dio_wr_get_block() stores dwc in >> bh->b_private, which lets it detect the first call for an I/O chunk and >> invoke ocfs2_add_inode_to_orphan() to protect the extent allocation. >> iomap provides no similar private field, so ocfs2_add_inode_to_orphan() >> moves to ocfs2_file_write_iter(). >> >> It is also impossible to pass the dwc to iomap_dio_ops.end_io: the >> callback cannot distinguish between I/Os initiated by different threads >> concurrently, and without that context it could delete an orphan entry >> created by another thread.  The orphan removal therefore moves to >> ocfs2_file_write_iter() as well, where inode_lock() serializes the writers >> and IOMAP_DIO_FORCE_WAIT makes an extending write complete before the >> function returns, so i_size is already updated when the entry is removed. >> >> iomap calls ->iomap_begin() once per mapping but ->end_io() once per I/O, >> and the latter only gets the kiocb, so the kiocb is the identity of the >> I/O: 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.  ocfs2_dio_collect_unwritten() >> finds the records of one I/O by that owner. >> >> A direct write waits for unwritten records belonging to another dio before >> it takes the re-write fast path, not after.  A completion which fails part >> way through its conversion has already made some clusters written and >> dropped ip_alloc_sem and the inode lock, but it still owns the records and >> is about to write the zeroed head/tail buffers over those clusters.  The >> fast path would see them as MAPPED and hand them out, and the new data >> would be overwritten by those zeroes.  The records are only removed after >> that cleanup, so waiting first excludes the whole window.  Under >> IOMAP_NOWAIT the wait becomes -EAGAIN. >> >> Unlike ocfs2_dio_wr_get_block(), the zeroing of the gap between i_size and >> the write is done once per dio by ocfs2_dio_zero_eof() before the io is >> issued.  Zeroing it per mapping would zero blocks that earlier mappings of >> this same dio have already written, because i_size is only updated at >> completion; that made xfstests generic/538 fail. >> >> On failure only the first and the last cluster of the touched range are >> cleaned up, through ocfs2_dio_cleanup_clusters(): those are the only >> clusters ocfs2_grab_folios_direct() fills folio windows for, and the >> clusters in between can hold dirty folios of other writes sharing the >> cluster.  ocfs2_write_range_buffers() writes the buffers directly when some >> clusters may already have been converted, because generic writeback >> refuses to submit folios beyond i_size. >> >> ocfs2_dio_clear_orphan_locked() only removes the orphan entry.  Clusters >> beyond i_size are left alone: there is no on-disk way to tell what this >> write allocated from unwritten extents preallocated with >> fallocate(FALLOC_FL_KEEP_SIZE), so truncating to i_size would free the >> preallocated space as well.  They stay until a later truncate or recovery. >> The in-memory marker is only dropped once the entry is really gone, so a >> failed write leaves it for the next extending write to recover, which >> ocfs2_add_inode_to_orphan() already does before adding its own. >> >> The pid marker is a strict ownership token: every path that leaves the >> entry behind zeroes ip_dio_orphan_pid, so a later write by the same task >> cannot match it and remove the entry without the truncate recovery. >> ocfs2_dio_clear_orphan() only removes the entry once i_size covers the >> range the write meant to write, or nothing is allocated beyond i_size; >> otherwise it leaves the entry for recovery and gives up ownership. >> >> Writes that span EOF may trigger an i_size update at completion, and >> within i_size there can still be uncommitted metadata such as the CoW done >> by ocfs2_prepare_inode_for_write().  Both set IOMAP_F_DIRTY so iomap cannot >> take the FUA-only fast path, which would clear IOMAP_DIO_NEED_SYNC and skip >> generic_write_sync(), leaving that metadata uncommitted for O_DSYNC. >> >> With the write path on iomap, ocfs2 no longer needs LEGACY_DIRECT_IO. >> >> A buffered write needs the rw lock EX, so every path that falls back to >> generic_perform_write() upgrades first: the -ENOTBLK / zero-length bounce >> inside the direct branch as well as the demoted direct branch.  Under >> coherency=buffered a PR lock would let another node write the same range >> directly while this node does a read-modify-write in the page cache. >> ocfs2_dio_upgrade_rw_lock() drops PR and takes EX rather than asking for >> one upgrade, which can deadlock in the cluster lock manager when two nodes >> upgrade at once. >> >> ip_dio_orphan_pid is reset in ocfs2_alloc_inode(), not only in the slab >> constructor.  The slab object is reused for a new inode, and a stale pid >> that happens to be reused by another task would let an unrelated write >> match the marker and delete a crash-left orphan entry without the truncate >> recovery, leaking the clusters beyond i_size. >> >> Clearing the orphan entry takes the inode cluster lock, which blocks, so >> IOCB_NOWAIT does not go there.  The marker stays for a later write or for >> the recovery. >> >> ocfs2_free_write_ctxt() runs the cached deallocs after unlocking the >> folios, like ocfs2_write_end_nolock() does.  Without it the error paths >> that free a write context lose the extents it had cached for deletion. >> >> A write that hits an error does not convert its unwritten extents. >> iomap keeps dio->size counting bios that failed at completion, and a >> short write can leave the tail of the last cluster unzeroed, so >> converting [offset, offset + size) would mark uninitialized disk blocks >> written and expose stale data.  Leave the extents unwritten, drop the >> zeroed folios and return the error, as ext4 and xfs do; the extents read >> back as zeroes until a later write converts them. >> >> Co-developed-by: Joseph Qi >> Signed-off-by: Joseph Qi >> Signed-off-by: Heming Zhao >> --- >>   fs/ocfs2/Kconfig |    1 - >>   fs/ocfs2/aops.c  | 1062 +++++++++++++++++++++++++++++++++------------- >>   fs/ocfs2/aops.h  |   33 +- >>   fs/ocfs2/file.c  |  275 ++++++++++-- >>   fs/ocfs2/inode.h |    8 +- >>   fs/ocfs2/super.c |    2 + >>   6 files changed, 1032 insertions(+), 349 deletions(-) >> >> diff --git a/fs/ocfs2/Kconfig b/fs/ocfs2/Kconfig >> index bf1678a5eb01e..74bc2008ef0bf 100644 >> --- a/fs/ocfs2/Kconfig >> +++ b/fs/ocfs2/Kconfig >> @@ -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 >> diff --git a/fs/ocfs2/aops.c b/fs/ocfs2/aops.c >> index 4b76be5909689..8d903c55749a9 100644 >> --- a/fs/ocfs2/aops.c >> +++ b/fs/ocfs2/aops.c >> @@ -951,6 +951,8 @@ static void ocfs2_free_write_ctxt(struct inode *inode, >>   ... ... >> + >> +/* >> + * Undo the orphan dir entry an extending direct write has added. >> + * >> + * Clusters beyond i_size are left alone.  There is no on-disk way to tell >> + * what this write allocated from unwritten extents preallocated with >> + * fallocate(FALLOC_FL_KEEP_SIZE), so truncating to i_size would free the >> + * preallocated space as well.  They stay until a later truncate or recovery. >> + * >> + * TODO: ocfs2_recover_orphans() still truncates to i_size, so dead-slot >> + * recovery can still free FALLOC_FL_KEEP_SIZE extents.  That predates the >> + * iomap conversion. >> + */ >> +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); >> +    struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; >> +    int ret = 0; >> + >> +    if (di->i_flags & cpu_to_le32(OCFS2_DIO_ORPHANED_FL)) >> +        ret = ocfs2_del_inode_from_orphan(osb, inode, di_bh, 0, 0); >> +    if (ret < 0) { >> +        mlog_errno(ret); /* leave the entry to the recovery */ >> +        WRITE_ONCE(oi->ip_dio_orphan_pid, 0); >> +        return; >> +    } > > Regardless of the "ret" value, the code clears oi->ip_dio_orphan_pid. > I suggest removing the above WRITE_ONCE() and return. > You mean re-use the below clear, right? Then we can't return directly here. So how about the following nit: if (di->i_flags & cpu_to_le32(OCFS2_DIO_ORPHANED_FL)) { int ret = ocfs2_del_inode_from_orphan(osb, inode, di_bh, 0, 0); if (ret < 0) mlog_errno(ret); /* leave the entry to the recovery */ } /* * Always give up ownership: if the entry survived it is left for the * next extending write's truncate recovery, see * ocfs2_add_inode_to_orphan(). */ WRITE_ONCE(oi->ip_dio_orphan_pid, 0); >> + >> +    /* >> +     * Only drop the marker once the entry is gone.  Keeping it while the >> +     * entry survives lets the next extending write recover it, see >> +     * ocfs2_add_inode_to_orphan(). >> +     */ > > This comment also needs updating if we drop above codes. > > Thanks, > Heming > >> +    WRITE_ONCE(oi->ip_dio_orphan_pid, 0); >> +} >> + >> +/* >> + * Does nothing when this task is not the one which added the entry. >> + * The rw_lock has to be held.  @end is the end of the range this write >> + * meant to write, 0 when it added no orphan entry. >> + */ >> +void ocfs2_dio_clear_orphan(struct inode *inode, loff_t end) >> ... ...