From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=115.124.30.119 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796222126; b=Pe2xoc9zA/C9dY9BVgoTeTG+vM8wKn6MEqTNiI02vjz5TWiTJEdMv37h9Q65MkzQ0RG3 M+hr8JT7U5OfJfccqYLaAOBzDVMd0Pk32sGcRGGLz6mZQnsBFQ18sxdimtZ7o53rOaQyH yOJO8XN6OsJqd0SZS7GkB0KAyfKbpI6MW8YIqnWeA2mQk2Yd0H1vm/ieNZbwdPgO2ZafA m2mjtF/fCOuq5vvwG1xCpi3r2DmyrLpVYFwwRBHKX4ClcinK7yLVjgNPhXi/ZDlmqdP5V g7ZPUBJrmUt+hdNlBJZ0+I5nJqTzHaRzDTLotRSAOoNotGSN7DvukGewrF0HsnaxdJA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796222126; 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=kB0WuubI6qhwVRzADivmKz7F2X7rdl0F32Okteoy0TY=; b=aBX4SpQ8yqf1eZfhUH7HINf/TBRyMxgT+fFEEEmI7Xz3JRRnu5z63zHCkm0XS4O+w8VO IsxwjQL1a8rsjPYytbua0AOjZbe+fvk3Pad/sMaO3o5YvNnu1OWzXrYECtv3TG1Enpzyt UNj6Na7f8VjMZ3Eqmc/IHZVrPbte5C+AEDn9aXL12fq8hWNO4vKncpZ4mQAWyVZ0mUpLZ kjHOoWmjfxjrsF+ccIyPSscOwm9SLFidtTNFlDVVKlDdK4W1/LckAkV7bFVYr6VK75LEp zURdblqMp0lEmhp9vN5dlfmmq36O+ACB/Av7sPUGwrRLTTfMbMsBISonL3p9wrEnxiw== 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="EPIDl/R1"; arc=none smtp.remote-ip=115.124.30.119 Received: from out30-119.freemail.mail.aliyun.com (out30-119.freemail.mail.aliyun.com [115.124.30.119]) (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 DBE57606A69C for ; Wed, 2 Dec 2026 14:35:25 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org DBE57606A69C 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=EPIDl/R1 DKIM-Signature:v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1796222117; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=kB0WuubI6qhwVRzADivmKz7F2X7rdl0F32Okteoy0TY=; b=EPIDl/R1kgfWy5ICI+I6ekaNLY22r3SWptdDgbgxMBbXZN6HHoSshyQ4QCV5DNGhOmFYWXsy4rAzR7BZPb+PBGgujzgJLSRgS/GITOMsBiogKv+aFACPB6uwUWxmycNl/1YnleK5C4s/M74QDOQ1mtkODp3iSlq3bAMb+TZpKzE= X-Alimail-AntiSpam:AC=PASS;BC=-1|-1;BR=01201311R461e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=maildocker-contentspam033037009110;MF=joseph.qi@linux.alibaba.com;NM=1;PH=DS;RN=8;SR=0;TI=SMTPD_---0XGBGYuR_1796222116; Received: from localhost(mailfrom:joseph.qi@linux.alibaba.com fp:SMTPD_---0XGBGYuR_1796222116 cluster:ay36) by smtp.aliyun-inc.com; Wed, 02 Dec 2026 22:35:17 +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 v15 08/10] ocfs2: keep sub-page overwrites of written blocks on direct I/O Date: Wed, 2 Dec 2026 22:35:05 +0800 Message-Id: <20261202143507.1754776-9-joseph.qi@linux.alibaba.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20261202143507.1754776-1-joseph.qi@linux.alibaba.com> References: <20261202143507.1754776-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_should_use_dio() demotes every non-page-aligned write to buffered I/O when i_blocksize(inode) < PAGE_SIZE. The zeroing hazard it guards against only exists when a partial block at either end is a hole or an unwritten extent, or the write extends the file; a pure in-place overwrite of already-written blocks needs no zeroing at all. On 16K or 64K page kernels every ocfs2 file system has blocksize < PAGE_SIZE, so all sub-page O_DIRECT overwrites were taking an exclusive rw lock, failing IOCB_NOWAIT and going through page-cache writeback. Probe the partial blocks at both ends with a no-create lookup and demote only when one of them is not already mapped and written, or the write extends i_size. Do this only at the second ocfs2_should_use_dio() call site, after generic_write_checks() has resolved O_APPEND and clamped the length and ocfs2_prepare_inode_for_write() has refreshed the inode. At the first site ki_pos and count are not final, so the range cannot be probed there; leaving it undemoted is safe because the buffered path upgrades the rw lock later. That second call site is added here. A write demoted after generic_write_checks() has already been handed a PR rw lock by the first evaluation, and the buffered write it falls into needs EX, so it goes through ocfs2_dio_upgrade_rw_lock() like the other fallback paths. The rw lock is held across the probe, so no other node can punch or truncate the range underneath it. Signed-off-by: Joseph Qi --- fs/ocfs2/aops.c | 52 +++++++++++++++++++++++++++++++++ fs/ocfs2/aops.h | 2 ++ fs/ocfs2/file.c | 77 +++++++++++++++++++++++++++++++++++++++---------- 3 files changed, 116 insertions(+), 15 deletions(-) diff --git a/fs/ocfs2/aops.c b/fs/ocfs2/aops.c index 8d903c55749a9..c7b76842b0fd6 100644 --- a/fs/ocfs2/aops.c +++ b/fs/ocfs2/aops.c @@ -2841,6 +2841,58 @@ int ocfs2_dio_zero_eof(struct inode *inode, loff_t pos, size_t count) return ret; } +/* + * A direct write which does not cover whole pages leaves the rest of the + * page untouched, and iomap only zeroes the padding up to the file system + * block size. That is safe only when the partial blocks at both ends are + * already allocated and written; a hole or an unwritten extent there would + * expose stale data once the cluster is marked written. Let buffered I/O + * handle those. + * + * Called with the rw lock held, so no other node can punch or truncate the + * range underneath us. + */ +bool ocfs2_dio_subpage_needs_buffered(struct inode *inode, loff_t pos, + size_t count, bool nowait) +{ + unsigned int blkbits = inode->i_blkbits; + struct ocfs2_map_block map; + int ret; + + if (i_blocksize(inode) >= PAGE_SIZE) + return false; + if (IS_ALIGNED(pos, PAGE_SIZE) && IS_ALIGNED(pos + count, PAGE_SIZE)) + return false; + if (pos + count > i_size_read(inode)) + return true; + + /* + * The write does not cover whole pages, so the head block can be + * unwritten even when pos is page aligned (count < PAGE_SIZE): probe + * it regardless of alignment, and the tail block too when it is a + * different block. An unwritten end block means the cluster is + * unwritten and a sub-page direct write would expose its unzeroed + * blocks once it is converted. + */ + map.lblk = pos >> blkbits; + map.len = 1; + map.flags = 0; + ret = ocfs2_lock_map_blocks(inode, &map, 0, nowait); + if (ret || !(map.flags & OCFS2_MAP_MAPPED)) + return true; + + if (((pos + count - 1) >> blkbits) != (pos >> blkbits)) { + map.lblk = (pos + count - 1) >> blkbits; + map.len = 1; + map.flags = 0; + ret = ocfs2_lock_map_blocks(inode, &map, 0, nowait); + if (ret || !(map.flags & OCFS2_MAP_MAPPED)) + return true; + } + + return false; +} + /* * The most a single mapping handed out for a direct write spans. * It bounds the memory of the write context, one descriptor per diff --git a/fs/ocfs2/aops.h b/fs/ocfs2/aops.h index 7336df15ca3a0..38a977938223a 100644 --- a/fs/ocfs2/aops.h +++ b/fs/ocfs2/aops.h @@ -51,6 +51,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_dio_zero_eof(struct inode *inode, loff_t pos, size_t count); +bool ocfs2_dio_subpage_needs_buffered(struct inode *inode, loff_t pos, + size_t count, bool nowait); void ocfs2_dio_clear_orphan(struct inode *inode, loff_t end); int ocfs2_map_blocks(struct inode *inode, struct ocfs2_map_block *map, int flags); diff --git a/fs/ocfs2/file.c b/fs/ocfs2/file.c index a96fb831b8a98..8515ec83a3dbe 100644 --- a/fs/ocfs2/file.c +++ b/fs/ocfs2/file.c @@ -2425,21 +2425,6 @@ 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. - * - * 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; } @@ -2583,6 +2568,68 @@ static ssize_t ocfs2_file_write_iter(struct kiocb *iocb, goto out; } + /* + * generic_write_checks() has moved ki_pos to i_size for O_APPEND and + * clamped the length, and ocfs2_prepare_inode_for_write() has + * refreshed i_size and the inode flags under the cluster lock. The + * decisions taken before them can be stale, take them again with what + * the I/O is actually going to use. + */ + if (direct_io) { + use_dio = ocfs2_should_use_dio(iocb, from, inode); + + /* + * Only now are ki_pos and the length final, so this is where + * a sub-page write can be checked against the blocks it will + * actually touch. A pure overwrite of written blocks stays + * direct; anything needing zeroing goes buffered. + */ + if (use_dio && iov_iter_rw(from) == WRITE && + ocfs2_dio_subpage_needs_buffered(inode, iocb->ki_pos, + iov_iter_count(from), + nowait)) + use_dio = false; + + /* + * A demoted NOWAIT write cannot fall back to buffered I/O; + * reject it here, before kiocb_modified() dirties the inode. + */ + if (nowait && !use_dio) { + ret = -EAGAIN; + goto out; + } + + /* An extending write has to hold the rw lock EX */ + if (use_dio && rw_level == 0 && !nowait && + iocb->ki_pos + count > i_size_read(inode)) { + ret = ocfs2_dio_upgrade_rw_lock(inode, &rw_level); + if (ret) + goto out; + /* + * The upgrade dropped the rw lock, so another node may + * have reflinked, punched or extended the range since + * ocfs2_prepare_inode_for_write() ran. Refresh the + * inode and extent cache and redo any CoW before the + * direct write maps blocks without the inode lock. + */ + ret = ocfs2_prepare_inode_for_write(file, iocb->ki_pos, + count, 1); + if (ret < 0) { + if (ret != -EAGAIN) + mlog_errno(ret); + goto out; + } + /* + * The inode may have changed across the lock drop, e.g. + * another node truncated it and wrote inline data; take + * the direct/buffered decision again so an inline-data + * inode falls back to buffered I/O instead of failing + * the direct mapping with -ERANGE. + */ + use_dio = ocfs2_should_use_dio(iocb, from, inode); + } + } + ret = kiocb_modified(iocb); if (ret < 0) goto out; -- 2.39.3