From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=115.124.30.118 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796119669; b=BKAB0IEwhOkWg1b0MNdUthXsCshjV+EsyEhnA79ch+mrw81fJ9/Y7MvkHWuXzVzVjBBy /Ds3lNMM2Zrhf0CIopuO6qFcdWSAxUGaSQSOpWS0qu78QB5C9l2YAHmmlWvRxP53LkzVm A4JPqjr/lRBaGbwqOOg3QiBjl7Gx5ocpsQg4azB2kDo4GV46tGPAaPcL+YKHeOF1Cw7Fo TyxeE2AI/LpZwcwDjCEVoOtLM4yk+/dDd8c2JAZrXj3pcNfL+dk882uQXWe3oKuNJIBJf bgrSFnaaEqeW4YO2xIOiaZjFiA0fisWcqbRTcG1vpRqUp9kNKCDoNFNKa1SHUJGN2nA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796119669; h=DMARC-Filter:DKIM-Signature:X-Alimail-AntiSpam:Received:From:To:Cc: Subject:Date:Message-Id:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=MvhkaAWoLV4M0oZ/tpsHBwaOiAGWIx6wofw5xwfHWPA=; b=crAZaQ8Uasu0VNF0Dx1Z+wU3bOeXOCucRKmt/ZWJCC1xLdgWIARTiJu9Oa1z85Tmfcyx 0X+2bVLwjN//b+Ybl/yLI5+2Hg9DRZnERyuBAncmjGFuhIEFRu2Us/3jxl/mDPd05OPBp srt8K2k/o4CE63ubkJOANhGRAWqGz7rrZ2peCmqtU1DwUBYV6geQNZ5bu8HA1LHeGJZSE f5nzKkNr7rXFBtw1SXmarVEq3YSudlTXJOlMoeAM8HaFA55e9bwTnyk2tOMwhEFjZX3Xo CZju2Ik/4v/vfc70BkwnzW4ELs6nxPvINcqL6C7g2nAkn3zLbuETl2k9Wm4SbopsCPA== 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=DVrlfSSb; arc=none smtp.remote-ip=115.124.30.118 Received: from out30-118.freemail.mail.aliyun.com (out30-118.freemail.mail.aliyun.com [115.124.30.118]) (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 55B0A606A332 for ; Tue, 1 Dec 2026 10:07:47 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 55B0A606A332 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=DVrlfSSb DKIM-Signature:v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1796119666; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=MvhkaAWoLV4M0oZ/tpsHBwaOiAGWIx6wofw5xwfHWPA=; b=DVrlfSSbo8Kny1O6aezVEyQG6UqAV/EuI9kyg+dlcL1imQhKxGJTL4CnAROV7GSXMdiywSkM7gf7J2tFZtAgGmyt1jQGxE3Dk/x8Ze2tE1CcTzc8+QLTJv1/cQnJfcF8OBZ4X7A31qCEljQQBT3kzB3dpF8koHHRqG3gYhDlwME= X-Alimail-AntiSpam:AC=PASS;BC=-1|-1;BR=01201311R601e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=maildocker-contentspam033045133197;MF=joseph.qi@linux.alibaba.com;NM=1;PH=DS;RN=8;SR=0;TI=SMTPD_---0XFzGSpn_1796119665; Received: from localhost(mailfrom:joseph.qi@linux.alibaba.com fp:SMTPD_---0XFzGSpn_1796119665 cluster:ay36) by smtp.aliyun-inc.com; Tue, 01 Dec 2026 18:07:45 +0800 From: Joseph Qi To: hch@lst.de, Heming Zhao Cc: Andrew Morton , Mark Fasheh , Joel Becker , ocfs2-devel@lists.linux.dev, linux-kernel@vger.kernel.org, Joseph Qi Subject: [RFC PATCH v14 10/10] ocfs2: make buffered writes wait for in-flight direct writes Date: Tue, 1 Dec 2026 18:07:24 +0800 Message-Id: <20261201100724.2909559-11-joseph.qi@linux.alibaba.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20261201100724.2909559-1-joseph.qi@linux.alibaba.com> References: <20261201100724.2909559-1-joseph.qi@linux.alibaba.com> 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 Content-Transfer-Encoding: 8bit ocfs2_unwritten_check() lets a buffered write skip zeroing and unwritten conversion for a cluster that an in-flight direct write has put on ip_unwritten_list, on the assumption that the dio will convert it. The dio only converts the range it actually completed, and converts nothing at all when it fails, so a dio which ends short or errors leaves the cluster unwritten while the buffered data sits in a dirty folio. Once that folio is written back and evicted the data reads back as zeroes. Wait for foreign entries on ip_unwritten_wq before taking any lock in ocfs2_write_begin(), the same way ocfs2_dio_wr_map_blocks() already does. The wait must be outside ip_alloc_sem and the inode cluster lock because ocfs2_dio_end_io_write() takes both to convert. Every caller holds i_rwsem and entries are only added under it, so the set can only shrink and no retry loop is needed. The folio is not locked yet at this point, so there is no ordering conflict with ocfs2_grab_folios_direct(). page_mkwrite is deliberately left alone. It holds no i_rwsem, so entries can appear between the wait and taking ip_alloc_sem, and it is reachable through get_user_pages() from bio_iov_iter_get_pages() when the dio source buffer is an mmap of the same file. Waiting there with no owner would match the dio's own entries, which are only removed at end_io, and deadlock against itself. The mmap window therefore remains open and needs a way to exclude the current task's own in-flight dio first. Fixes: 4506cfb6f8ca ("ocfs2: record UNWRITTEN extents when populate write desc") Signed-off-by: Joseph Qi --- fs/ocfs2/aops.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/fs/ocfs2/aops.c b/fs/ocfs2/aops.c index 81e374f1d4f77..a7634e92e72e8 100644 --- a/fs/ocfs2/aops.c +++ b/fs/ocfs2/aops.c @@ -2569,6 +2569,39 @@ int ocfs2_write_begin_nolock(struct address_space *mapping, return ret; } +/* + * Wait for direct writes still in flight on [pos, pos + len) to finish. + * A buffered write must not defer its zeroing to a dio which may never + * convert the cluster: if that dio ends short or fails, the cluster stays + * unwritten on disk and the buffered data reads back as zeroes. + * + * Must be called without ip_alloc_sem or the inode cluster lock held, + * because ocfs2_dio_end_io_write() takes both to convert. i_rwsem is held + * by every caller and all entries are added under it, so the set can only + * shrink while we wait and no retry is needed. + */ +static int ocfs2_wait_foreign_unwritten(struct inode *inode, loff_t pos, + unsigned int len) +{ + struct ocfs2_inode_info *oi = OCFS2_I(inode); + struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); + /* + * An extending write zeroes the gap from i_size, so wait on the + * cluster containing i_size too: a dio there may still convert it + * after ocfs2_zero_extend() skipped it as unwritten, exposing the + * unzeroed tail below the new i_size. + */ + u32 cstart = min_t(loff_t, pos, i_size_read(inode)) >> + osb->s_clustersize_bits; + u32 cend = ocfs2_clusters_for_bytes(inode->i_sb, pos + len); + + if (!ocfs2_dio_foreign_unwritten(oi, cstart, cend, NULL)) + return 0; + + return wait_event_killable(oi->ip_unwritten_wq, + !ocfs2_dio_foreign_unwritten(oi, cstart, cend, NULL)); +} + static int ocfs2_write_begin(const struct kiocb *iocb, struct address_space *mapping, loff_t pos, unsigned len, @@ -2578,6 +2611,10 @@ static int ocfs2_write_begin(const struct kiocb *iocb, struct buffer_head *di_bh = NULL; struct inode *inode = mapping->host; + ret = ocfs2_wait_foreign_unwritten(inode, pos, len); + if (ret) + return ret; + ret = ocfs2_inode_lock(inode, &di_bh, 1); if (ret) { mlog_errno(ret); -- 2.39.3