From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796491222; b=EQFUPZPy2PEjeVG+d7djNhiSlCKsUq28oWvjJKt6r8q4z6PXftP5iijC01EUHzdcuGZy pQPb0fNh+J43Fgty+POC88+eld/bOIzjz+KNshHbbilC4fDjntDOrUrxpbQ61CGkUg8op oMqMaHUj3RqqHAtpj1WRWc1sVHBeSFN4nrfvKOXsF7XHbtbKDMVunZBx2ORiI9WYlZSZU fVOrIdMnV2GCwwvyXa4o49N85i0juZVKX2jTGZAzi/lmZjUMWicvBBDE3QIQAqVIsLNSV VGQRqnpp/CmahObH4ayNwyUlNowuLASnpSRflvoYfuGohPoRHJfNYizt7cMMNNeq8cg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796491222; h=Received:DKIM-Signature:Date:To:From:In-Reply-To:References:Subject: Message-Id; bh=FjKItgde9xsMKyK8vBNqrGDUJFQoHCOksWCgQ21aLpM=; b=sikcPEvR8A+bNMdsvKOF8VotO5rDwOEMt0BWBYoVEjygQJxfgymmyFOKBQW9YxJ7+vdL oqmyiphhf6NFjpl1iLdxVjAI0Y7x4l5NXqd3sNamLlGsRP5QtHxlzK9ul2rdvizFZLXFh Cicgt6L3sptMJEc/riMHcTi2QHMVeX2BOXsx1k9FAMB/hKAjsqTX8nF9CtVI3GIN8wW+K aT3fPB2f7MA4eiQmvj3y5gPSqSWN9y6uh+FO4RNA80h1OOmzJ+aTmqcgS/GXV/oZ3I0dk DQI3Ug8fJyXdvt2Xj4lNHSQO6Wq4DtsgPmO4iz5/6FNaAWMkOI4k/XSx5s9tAlIysoQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linux-foundation.org header.i=@linux-foundation.org header.a=rsa-sha256 header.s=korg header.b=RJcOzwY2; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 91388606A31F for ; Sat, 5 Dec 2026 17:20:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.a=rsa-sha256 header.s=korg header.b=RJcOzwY2 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 09C2E1F0092B; Sat, 5 Dec 2026 17:20:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=korg; t=1796491222; bh=FjKItgde9xsMKyK8vBNqrGDUJFQoHCOksWCgQ21aLpM=; h=Date:To:From:In-Reply-To:References:Subject; b=RJcOzwY22sQ2XkojXBNQgU5diVouIEoZTPB7UsViRPEzY1ZFxfSCCqk1xo6XprHJ6 6BpY7d8DBXp5RT13GMqPvxzKyP+FXFoUbrM9CJhfOdOJmN24PtwUOR/3welnNRoGRw q3vac3RSybtq5A5KVQSjq8exTmK8zjyRPavAWLmc= Date: Sat, 05 Dec 2026 09:20:21 -0800 To: mm-commits@vger.kernel.org,mark@fasheh.com,jlbec@evilplan.org, heming.zhao@suse.com,hch@lst.de,joseph.qi@linux.alibaba.com, akpm@linux-foundation.org From: Andrew Morton In-Reply-To: <20261205143959.704982-9-joseph.qi@linux.alibaba.com> References: <20261205143959.704982-9-joseph.qi@linux.alibaba.com> Subject: + ocfs2-keep-sub-page-overwrites-of-written-blocks-on-direct-i-o.patch added to mm-nonmm-unstable branch Message-Id: <20261205172022.09C2E1F0092B@smtp.kernel.org> X-Mailing-List: mm-commits@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list The patch titled Subject: ocfs2: keep sub-page overwrites of written blocks on direct I/O has been added to the -mm mm-nonmm-unstable branch. Its filename is ocfs2-keep-sub-page-overwrites-of-written-blocks-on-direct-i-o.patch This patch will shortly appear at https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/ocfs2-keep-sub-page-overwrites-of-written-blocks-on-direct-i-o.patch This patch will later appear in the mm-nonmm-unstable branch at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm Before you just go and hit "reply", please: a) Consider who else should be cc'ed b) Prefer to cc a suitable mailing list as well c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's *** Remember to use Documentation/process/submit-checklist.rst when testing your code *** The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm and is updated there most days ------------------------------------------------------ From: Joseph Qi Subject: ocfs2: keep sub-page overwrites of written blocks on direct I/O Date: Sat, 05 Dec 2026 22:39:57 +0800 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. Link: https://patch.msgid.link/20261205143959.704982-9-joseph.qi@linux.alibaba.com Signed-off-by: Joseph Qi Signed-off-by: Andrew Morton Cc: Heming Zhao Cc: Christoph Hellwig Cc: Mark Fasheh Cc: Joel Becker fs/ocfs2/aops.c | 57 +++++++++++++++++++++++++++++ fs/ocfs2/aops.h | 2 + fs/ocfs2/file.c | 89 ++++++++++++++++++++++++++++++++++++---------- 3 files changed, 129 insertions(+), 19 deletions(-) --- a/fs/ocfs2/aops.c~ocfs2-keep-sub-page-overwrites-of-written-blocks-on-direct-i-o +++ a/fs/ocfs2/aops.c @@ -2904,6 +2904,63 @@ int ocfs2_dio_zero_eof(struct inode *ino } /* + * 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. + */ +int 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 0; + if (IS_ALIGNED(pos, PAGE_SIZE) && IS_ALIGNED(pos + count, PAGE_SIZE)) + return 0; + if (pos + count > i_size_read(inode)) + return 1; + + /* + * 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. A lookup error is returned as-is so + * the caller fails the write rather than masking it as a demotion. + */ + map.lblk = pos >> blkbits; + map.len = 1; + map.flags = 0; + ret = ocfs2_lock_map_blocks(inode, &map, 0, nowait); + if (ret) + return ret; + if (!(map.flags & OCFS2_MAP_MAPPED)) + return 1; + + 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) + return ret; + if (!(map.flags & OCFS2_MAP_MAPPED)) + return 1; + } + + return 0; +} + +/* * The most a single mapping handed out for a direct write spans. * It bounds the memory of the write context, one descriptor per * cluster. --- a/fs/ocfs2/aops.h~ocfs2-keep-sub-page-overwrites-of-written-blocks-on-direct-i-o +++ a/fs/ocfs2/aops.h @@ -51,6 +51,8 @@ int ocfs2_size_fits_inline_data(struct b 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); +int 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); --- a/fs/ocfs2/file.c~ocfs2-keep-sub-page-overwrites-of-written-blocks-on-direct-i-o +++ a/fs/ocfs2/file.c @@ -2425,21 +2425,6 @@ static bool ocfs2_should_use_dio(struct !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; } @@ -2597,6 +2582,76 @@ static ssize_t ocfs2_file_write_iter(str } /* + * 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) { + ret = ocfs2_dio_subpage_needs_buffered(inode, + iocb->ki_pos, iov_iter_count(from), + nowait); + if (ret < 0) { + if (ret != -EAGAIN) + mlog_errno(ret); + goto out; + } + if (ret) + use_dio = false; + } + + /* + * A demoted NOWAIT write cannot fall back to buffered I/O; + * reject it here, before kiocb_modified() dirties the inode. + * The device alignment was already checked above. + */ + 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); + } + } + + /* * Validate the direct write before kiocb_modified() so a rejected * write does not dirty the inode or strip capabilities. Extending * the file needs cluster locks and transactions, which IOCB_NOWAIT @@ -2728,10 +2783,6 @@ static ssize_t ocfs2_file_write_iter(str use_dio = false; } } else if (direct_io) { - if (nowait) { - ret = -EAGAIN; - goto out; - } ret = ocfs2_dio_upgrade_rw_lock(inode, &rw_level); if (ret) goto out; _ Patches currently in -mm which might be from joseph.qi@linux.alibaba.com are ocfs2-exempt-orphaned-dinodes-from-the-zero-nlink-check.patch ocfs2-abstract-the-zero-nlink-or-mode-dinode-check.patch ocfs2-abstract-the-dinode-i_blkno-mismatch-check.patch ocfs2-abstract-the-dinode-ocfs2_valid_fl-check.patch ocfs2-abstract-the-dinode-fs_generation-mismatch-check.patch ocfs2-abstract-the-dinode-suballoc-slot-checks.patch ocfs2-abstract-the-dinode-suballoc-bit-check.patch ocfs2-abstract-the-dinode-orphaned-slot-check.patch ocfs2-abstract-the-dinode-dio-orphaned-slot-check.patch ocfs2-abstract-the-inline-data-dinode-guard-and-cluster-check.patch ocfs2-abstract-the-inline-data-id_count-bound-check.patch ocfs2-abstract-the-inline-data-i_size-bound-check.patch ocfs2-abstract-the-fast-symlink-dinode-guard-and-size-check.patch ocfs2-abstract-the-fast-symlink-nul-termination-check.patch ocfs2-abstract-the-chain-list-dinode-guard-and-count-check.patch ocfs2-abstract-the-chain-list-next-free-record-check.patch ocfs2-abstract-the-chain-list-bits-per-cluster-check.patch ocfs2-abstract-the-dinode-refcount-location-check.patch ocfs2-abstract-the-zero-extent-list-count-check.patch ocfs2-abstract-the-oversized-extent-list-count-check.patch ocfs2-abstract-the-extent-list-next-free-record-check.patch ocfs2-report-unrepairable-corruption-from-online-filecheck-fix.patch ocfs2-check-dinode-nlink-and-mode-in-online-filecheck.patch ocfs2-check-dinode-suballoc-slot-in-online-filecheck.patch ocfs2-check-dinode-suballoc-bit-in-online-filecheck.patch ocfs2-check-dinode-orphaned-slots-in-online-filecheck.patch ocfs2-check-inline-data-dinodes-in-online-filecheck.patch ocfs2-check-fast-symlink-dinodes-in-online-filecheck.patch ocfs2-check-chain-list-dinodes-in-online-filecheck.patch ocfs2-check-dinode-refcount-location-in-online-filecheck.patch ocfs2-check-extent-list-dinodes-in-online-filecheck.patch ocfs2-dont-bug_on-when-a-group-extend-leaves-too-few-free-bits.patch ocfs2-validate-chain-allocator-clusters-per-group.patch ocfs2-check-chain-allocator-clusters-per-group-in-online-filecheck.patch ocfs2-keep-sub-page-overwrites-of-written-blocks-on-direct-i-o.patch ocfs2-write-the-dio-zeroed-head-tail-buffers-out-before-completion.patch ocfs2-make-buffered-writes-wait-for-in-flight-direct-writes.patch