From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=115.124.30.111 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796458788; b=SRNPjXT61bBx3JHLw0QjGdBxOpv3ja01i7DGbHRIKMN2gSAdUoehxQIkd+rl9QQs8ene yGE0PpgfWq+KTKT79Hr/oRquSvjCEE3COt27aL9L01WSyMmfOJsq5eFMBPSL4tMLjh83k OnxL70mT9uzwU3mOcYC2sAlufIEz7kE1jIu1LUJguQrNzbVfzkkyu8duX3OM8TCQuLMSY MVkhlE9+e/HsZvFBZoJehSBDlnFe4gMYJcX+7IjXH3X7ZuQSY5AY3wCAmPpxO/n9QdU6r 4eJyQ4wy71kt3X00UU2OZfd5UoOMNHT7ShtzXlYZaGAU1TptXl3pU/qnFYlLY3PyrMQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796458788; 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=Ztk5fF2yPigFhrIq7XHUPft3fk16JqRHRpkRnH1M/24=; b=qZQ1LfXU+AQDidiEqkDCcNkBg8UFZk24PX3XoLBjRv4miifCnPpQnnQrjS5kcN1ihxO5 Ujvjlj8IP0DQkdOEpWNJG09/JNziGaU8PJIzSTPqNLvRhfZtp4RmyPHqGkkZW16C8oHZ/ Eii/p/fMaZksDVIm0pI60X/Ife9ZevmnzJdPoT81Jbceowf8v7/FrXbsPKxbzgZxBIJk1 79BsH960MtGFViBtbk9F001e7TZ4/vFPRVG3RFOd373tkzueOyJ/jXB+/WO8RsuX77549 /g3rUPh/AO8GNToXNxBhqOkik7MFd9kB/VcZ3V1z4BVjFzO1ARS+tn6CxnwbLKwOdpw== 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=pwdxGgsC; arc=none smtp.remote-ip=115.124.30.111 Received: from out30-111.freemail.mail.aliyun.com (out30-111.freemail.mail.aliyun.com [115.124.30.111]) (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 80ABF606A6B2 for ; Sat, 5 Dec 2026 08:19:47 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 80ABF606A6B2 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=pwdxGgsC DKIM-Signature:v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1796458774; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=Ztk5fF2yPigFhrIq7XHUPft3fk16JqRHRpkRnH1M/24=; b=pwdxGgsCn2uJvZ0k4xayMI3FpAXOuEifkJ6bWo/DyrZ5udR6DNBGF1qJb5fpUr1qHrjmbAEQovtckJvmeSejv6QLbGCiHuYC+6ZvQgN8H2g9XG+TwrcOM7ZQqQdkYOJ7n2h16heC6J8OBYJl+znQ1gTvvgXu6rjbBiyEkGGgg3s= X-Alimail-AntiSpam:AC=PASS;BC=-1|-1;BR=01201311R131e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=maildocker-contentspam033037026112;MF=joseph.qi@linux.alibaba.com;NM=1;PH=DS;RN=8;SR=0;TI=SMTPD_---0XGJEcnW_1796458773; Received: from localhost(mailfrom:joseph.qi@linux.alibaba.com fp:SMTPD_---0XGJEcnW_1796458773 cluster:ay36) by smtp.aliyun-inc.com; Sat, 05 Dec 2026 16:19:34 +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 v17 09/10] ocfs2: write the dio zeroed head/tail buffers out before completion Date: Sat, 5 Dec 2026 16:19:22 +0800 Message-Id: <20261205081923.4097763-10-joseph.qi@linux.alibaba.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20261205081923.4097763-1-joseph.qi@linux.alibaba.com> References: <20261205081923.4097763-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 Preparing a direct write of a new or unwritten cluster zeroes the head and tail folios outside the user range and leaves them dirty in the page cache, outside the range iomap invalidates. They can be wholly beyond i_size until ocfs2_dio_end_io_write() updates it at completion. Writeback will not submit such buffers. ocfs2_writepages() uses mpage_writepages(), which for a folio at or past i_size falls through to block_write_full_folio(), and __block_write_full_folio() does: if (block > last_block) { clear_buffer_dirty(bh); set_buffer_uptodate(bh); } So if writeback runs before the conversion, the zeroes are discarded without ever reaching the disk, the folio is left clean, and the cluster exposes stale blocks once ocfs2_dio_end_io_write() marks it written. A short write leaves the zeroed tail folios of the unconverted trailing clusters dirty in the same way. ocfs2_dio_cleanup_pages(..., true) cannot fix this: it only runs when the conversion fails, and even then the buffers may already have been cleaned or the folio reclaimed, so ocfs2_write_range_buffers() finds nothing to write. The zeroes have to reach the disk while the clusters are still ours and the folios are still locked. Write them in ocfs2_write_end_nolock() for a direct write, right after block_commit_write() has marked them dirty. Submit every buffer first and wait in a second pass, so a cluster spanning hundreds of folios does not cost one round trip per folio. submit_bh()/wait_on_buffer() bypass the i_size check, which is the point. The folio lock keeps the buffers alive and excludes any other io on them, so no extra buffer reference is taken and a write error seen in the wait pass can only come from the submit pass. ip_alloc_sem, the inode cluster lock and an open journal handle are held; the data io needs no journal progress, so there is no cycle with a commit that is blocked on our handle and waiting for the folio lock. IOMAP_NOWAIT never reaches here, ocfs2_dio_wr_map_blocks() returns -EAGAIN before the allocation path. This makes the zeroing io synchronous in the mapping path instead of deferring it to background writeback. That io was always required; iomap issues its own block-size padding the same way through iomap_dio_zero(). Signed-off-by: Joseph Qi --- fs/ocfs2/aops.c | 110 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) diff --git a/fs/ocfs2/aops.c b/fs/ocfs2/aops.c index f0c38f39e500f..f8807260ab172 100644 --- a/fs/ocfs2/aops.c +++ b/fs/ocfs2/aops.c @@ -2693,6 +2693,103 @@ static void ocfs2_write_end_inline(struct inode *inode, loff_t pos, le16_to_cpu(di->i_dyn_features)); } +/* + * The head/tail folios a direct write zeroed are outside the range iomap + * invalidates, and they can be wholly beyond i_size until ->end_io() updates + * it. Writeback will not submit such buffers: __block_write_full_folio() + * clears their dirty bit without writing them, so the zeroes would never + * reach the disk and the cluster would expose stale blocks once + * ocfs2_dio_end_io_write() converts it. Flushing at completion is too late, + * by then the buffers may already have been cleaned or the folio reclaimed. + * Write them here, while the folios are locked and the clusters are ours. + * + * The folio lock keeps the buffers alive and excludes any other io on them, + * so no extra buffer reference is needed and a write error seen in the second + * pass can only come from the first. + */ +static int ocfs2_dio_write_zero_folios(struct ocfs2_write_ctxt *wc) +{ + int i, ret = 0; + + /* Submit them all before waiting, a cluster can span many folios. */ + for (i = 0; i < wc->w_num_folios; i++) { + struct folio *folio = wc->w_folios[i]; + struct buffer_head *bh, *head; + + if (!folio) + continue; + head = folio_buffers(folio); + if (!head) + continue; + + /* + * Tag the folio for writeback before submitting its buffers, + * as __block_write_full_folio() does. clear_dirty_for_io() + * drops PG_dirty and write-protects any mmap so a concurrent + * write re-dirties it; start_writeback() then clears the + * PAGECACHE_TAG_DIRTY that block_commit_write() left and sets + * PAGECACHE_TAG_WRITEBACK. Without the pairing the dirty tag + * survives, so a RWF_NOWAIT direct read keeps seeing the folio + * as needing writeback and writeback never clears it. Wait out + * any earlier writeback first: start_writeback() BUGs on a + * folio already under writeback. + */ + folio_wait_writeback(folio); + folio_clear_dirty_for_io(folio); + folio_start_writeback(folio); + + bh = head; + do { + lock_buffer(bh); + /* + * Clear a stale error bit from an earlier writeback; no + * write of this operation has completed yet, so only the + * wait pass below may latch an error into ret. + */ + clear_buffer_write_io_error(bh); + if (!buffer_dirty(bh)) { + unlock_buffer(bh); + goto next; + } + if (!buffer_mapped(bh) || !buffer_uptodate(bh) || + buffer_delay(bh) || buffer_unwritten(bh)) { + unlock_buffer(bh); + if (!ret) + ret = -EIO; + goto next; + } + clear_buffer_dirty(bh); + bh_submit(bh, REQ_OP_WRITE | REQ_SYNC, bh_end_write); +next: + bh = bh->b_this_page; + } while (bh != head); + } + + for (i = 0; i < wc->w_num_folios; i++) { + struct folio *folio = wc->w_folios[i]; + struct buffer_head *bh, *head; + + if (!folio) + continue; + head = folio_buffers(folio); + if (!head) + continue; + + bh = head; + do { + wait_on_buffer(bh); + if (buffer_write_io_error(bh) && !ret) + ret = -EIO; + bh = bh->b_this_page; + } while (bh != head); + + /* Every buffer is on disk; end the writeback started above. */ + folio_end_writeback(folio); + } + + return ret; +} + int ocfs2_write_end_nolock(struct address_space *mapping, loff_t pos, unsigned len, unsigned copied, void *fsdata) { @@ -2797,6 +2894,19 @@ int ocfs2_write_end_nolock(struct address_space *mapping, loff_t pos, } } + if (wc->w_type == OCFS2_WRITE_DIRECT) { + ret = ocfs2_dio_write_zero_folios(wc); + if (ret) { + copied = ret; + /* + * On failure out_write_size still runs: the allocation + * is committed below regardless, so refresh i_blocks and + * record the fsync transaction; a first-mapping failure + * has no end_io to do it later. + */ + } + } + out_write_size: /* Direct io do not update i_size here. */ if (wc->w_type != OCFS2_WRITE_DIRECT) { -- 2.39.3