From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=115.124.30.110 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796481623; b=U14tWEcREmqDCOX+Dtt6+oUfeVafZWGtHdpFuz5bft4rzfc2WW5CTEHnoUx5kR+049CJ g9wbbufcikMssmRGn36vcnh22mPg8wBEQF5rC+Jl7dg9/hq/VgHGRNP0IKtUzQ3AdjltY 8smm0eu6dZktNfMxU9zP144eKzAcHB0Mw//0BXfLoM8tKZsTVpn0CepuiUCYRb8AR5pM/ R+ij6v0DrpJtRtsVIqIB6Ds/6FFWU6sLQR9f411Be4WxSWCCwQ9SPAAYCDPtN7+1MQfNK SD8yW1CsH+iglXETSN4Q4gXxivOfDccEpcaxL6OZxlRS3IgtdiazQeADp7Z9ozaxvUw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796481623; 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=Gs1uzJ1ajvLpQ04Rw7rBhn6uobLH/0brY9nwUgTSeg4=; b=C7Gdz/iR797vZ18j+LW2e1Tkrot1dl+CWbe5ICW9b5/dRLxdLXKnRRbQJW4cT3eMQB99 64tOTmuvBQDM/XrYwcjcRVOOZCOtUT4GksnEPPEtnWTnu2J9raRfFey0Rt9l+HQUP5XJ2 VGLeJDzFcTO0Z5wwpNiDOG1w8uLW+QHnCW+9cmHyCDT1m9BATem1Xg6Ziv620FTxKE3Ft PBVlpUk+Bt0NmIrhdy0lSB22fbQpSDI8tLYT8i2SP3vxi9JG7OQzWErwCFnzfyK+JUaKy YUY+I1Fhf1l0ughkiinJs/NPR4bf4fkrRyITXAD8FLVvM3nnfBw7JhCaLxJvgwHzJBQ== 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=qWKzFajw; arc=none smtp.remote-ip=115.124.30.110 Received: from out30-110.freemail.mail.aliyun.com (out30-110.freemail.mail.aliyun.com [115.124.30.110]) (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 8C64A606A6B1 for ; Sat, 5 Dec 2026 14:40:21 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 8C64A606A6B1 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=qWKzFajw DKIM-Signature:v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1796481612; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=Gs1uzJ1ajvLpQ04Rw7rBhn6uobLH/0brY9nwUgTSeg4=; b=qWKzFajwAuWcI7peD+1TU211PlP+Hkvc+y3i+KUAZ4V4Ub7wwVqZ1PppbFOaaEHU9wfQ8k11ekQV9e2uYSsBepRt6a84BqmeYqLqV4LcvuryeWTaWLK0+4oEU2YvDnNsI30in7Be8OS38W2F1ZjaY3eTvvC99ly0Yq7JwwMAFIQ= X-Alimail-AntiSpam:AC=PASS;BC=-1|-1;BR=01201311R711e4;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=8;SR=0;TI=SMTPD_---0XGJscL4_1796481610; Received: from localhost(mailfrom:joseph.qi@linux.alibaba.com fp:SMTPD_---0XGJscL4_1796481610 cluster:ay36) by smtp.aliyun-inc.com; Sat, 05 Dec 2026 22:40:11 +0800 From: Joseph Qi To: Andrew Morton , Heming Zhao , hch@lst.de Cc: Mark Fasheh , Joel Becker , ocfs2-devel@lists.linux.dev, linux-kernel@vger.kernel.org, Joseph Qi Subject: [PATCH v18 10/10] ocfs2: make buffered writes wait for in-flight direct writes Date: Sat, 5 Dec 2026 22:39:59 +0800 Message-Id: <20261205143959.704982-11-joseph.qi@linux.alibaba.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20261205143959.704982-1-joseph.qi@linux.alibaba.com> References: <20261205143959.704982-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(). The same race exists in the gap between i_size and an extending write: ocfs2_zero_extend() skips unwritten clusters, so an in-flight dio there could later convert a cluster whose tail past i_size was never zeroed, exposing stale blocks once this write extends i_size past it. ocfs2_dio_zero_eof() therefore waits for foreign entries in [i_size, pos) before taking the inode lock, and ocfs2_wait_foreign_unwritten() widens cstart down to i_size so an extending buffered write waits on the cluster containing i_size too. 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 holds the inode cluster lock and ip_alloc_sem that ocfs2_dio_end_io_write() needs to convert, so a wait there would deadlock against the completion of any in-flight dio on the range. It is also reachable through get_user_pages() from bio_iov_iter_get_pages() when a direct read's destination buffer is an mmap of the same file. The mmap window therefore remains open. Fixes: 4506cfb6f8ca ("ocfs2: record UNWRITTEN extents when populate write desc") Signed-off-by: Joseph Qi --- fs/ocfs2/aops.c | 79 ++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 71 insertions(+), 8 deletions(-) diff --git a/fs/ocfs2/aops.c b/fs/ocfs2/aops.c index 7b074c7439412..bba1d4de1ce21 100644 --- a/fs/ocfs2/aops.c +++ b/fs/ocfs2/aops.c @@ -1808,12 +1808,14 @@ static bool ocfs2_dio_foreign_unwritten(struct ocfs2_inode_info *oi, * write might get shortened afterwards. * * TODO: - * Buffered writes or mmap page_mkwrite faults clear c_needs_zero and - * c_clear_unwritten, relying on the in-flight direct write to zero the cluster - * and convert the extent. If the direct write fails after - * ocfs2_dio_wr_map_blocks() drops ip_alloc_sem and ocfs2_inode_lock() before - * user pages are pinned or bios are submitted or rw_lock, they will lose data - * or expose uninitialized disk blocks. + * mmap page_mkwrite faults clear c_needs_zero and c_clear_unwritten, relying + * on the in-flight direct write to zero the cluster and convert the extent. + * If the direct write fails after ocfs2_dio_wr_map_blocks() drops ip_alloc_sem + * and ocfs2_inode_lock() before user pages are pinned or bios are submitted or + * rw_lock, they will lose data or expose uninitialized disk blocks. Buffered + * writes wait for the dio first, see ocfs2_wait_foreign_unwritten(), but + * page_mkwrite holds the inode cluster lock and ip_alloc_sem the conversion + * needs, so it cannot wait there. */ static void ocfs2_unwritten_check_locked(struct inode *inode, struct ocfs2_write_ctxt *wc, @@ -2628,6 +2630,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, @@ -2637,6 +2672,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); @@ -2977,6 +3016,7 @@ static int ocfs2_write_end(const struct kiocb *iocb, int ocfs2_dio_zero_eof(struct inode *inode, loff_t pos, size_t count) { struct ocfs2_inode_info *oi = OCFS2_I(inode); + struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); struct buffer_head *di_bh = NULL; int ret; loff_t isize = i_size_read(inode); @@ -2989,9 +3029,32 @@ int ocfs2_dio_zero_eof(struct inode *inode, loff_t pos, size_t count) * volume there is nothing to do here and no need to take the cluster * lock. The non-sparse path below always has allocation work left. */ - if (ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)) && pos <= isize) + if (ocfs2_sparse_alloc(osb) && pos <= isize) return 0; + /* + * An in-flight dio may own an unwritten cluster in the gap [i_size, + * pos). ocfs2_zero_extend() skips unwritten clusters, so that dio + * would later convert the cluster to written with its tail past + * i_size never zeroed, exposing stale blocks once this write extends + * i_size beyond it. Wait for those dios to finish first: afterwards + * the cluster is either written (and gets zeroed here) or unwritten + * and unowned (and stays unwritten, reading as zeroes). i_rwsem is + * held, so no new dio records clusters while we wait and the set only + * shrinks. Wait before taking the inode lock and ip_alloc_sem, which + * ->end_io() needs to convert. + */ + if (pos > isize) { + u32 cstart = isize >> osb->s_clustersize_bits; + u32 cend = ocfs2_clusters_for_bytes(inode->i_sb, pos); + + ret = wait_event_killable(oi->ip_unwritten_wq, + !ocfs2_dio_foreign_unwritten(oi, cstart, cend, + NULL)); + if (ret) + return ret; + } + ret = ocfs2_inode_lock(inode, &di_bh, 1); if (ret) { mlog_errno(ret); @@ -2999,7 +3062,7 @@ int ocfs2_dio_zero_eof(struct inode *inode, loff_t pos, size_t count) } down_write(&oi->ip_alloc_sem); - if (ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb))) + if (ocfs2_sparse_alloc(osb)) ret = ocfs2_zero_tail(inode, di_bh, pos); else ret = ocfs2_expand_nonsparse_inode(inode, di_bh, pos, count, -- 2.39.3