From: Hengyu Liang <hengyul@cs.unc.edu>
To: slava@dubeyko.com
Cc: glaubitz@physik.fu-berlin.de, frank.li@vivo.com,
linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH 1/2] hfs: fall back to buffered I/O for direct writes past phys_size
Date: Fri, 30 Oct 2026 00:29:02 -0400 [thread overview]
Message-ID: <20261030042903.4160506-2-hengyul@cs.unc.edu> (raw)
In-Reply-To: <20261030042903.4160506-1-hengyul@cs.unc.edu>
hfs_direct_IO() passes direct writes to blockdev_direct_IO(), which
does not go through hfs_write_begin(). When such a write needs a new
block, hfs_get_block() only adds the block size to phys_size. phys_size
starts at i_size when the inode is read, which does not have to be
block aligned, so after the write phys_size is smaller than i_size.
However, at the last close, hfs_file_truncate() sees i_size larger than
phys_size and calls hfs_write_begin() at i_size, which zeroes the file
from phys_size up to i_size. As of now, part of the data that a direct
write has just written past the old end of the file is lost, and fsck
reports no error.
The issue can be reproduced with:
truncate -s 32M hfs.img
mkfs.hfsplus -h hfs.img
mount -t hfs -o loop hfs.img /mnt
printf 'A%.0s' $(seq 100) > /mnt/f
umount /mnt
mount -t hfs -o loop hfs.img /mnt
xfs_io -d -c 'pwrite -S 0x42 512 512' /mnt/f
umount /mnt
mount -t hfs -o loop hfs.img /mnt
od -An -tx1 -j 512 -N 512 /mnt/f
The last 412 bytes read back as zeros instead of 0x42.
This patch will make a direct write that ends past phys_size fall back
to a buffered write, as fat_direct_IO() and affs_direct_IO() do.
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Cc: stable@vger.kernel.org
Signed-off-by: Hengyu Liang <hengyul@cs.unc.edu>
---
fs/hfs/inode.c | 10 ++++++++++
1 file changed, 10 insertions(+)
diff --git a/fs/hfs/inode.c b/fs/hfs/inode.c
index d9abab2a1e61..d476a16f4ef9 100644
--- a/fs/hfs/inode.c
+++ b/fs/hfs/inode.c
@@ -139,6 +139,16 @@ static ssize_t hfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
size_t count = iov_iter_count(iter);
ssize_t ret;
+ /*
+ * blockdev_direct_IO() does not go through ->write_begin(), so it
+ * neither zeroes the file from phys_size up to the write nor keeps
+ * phys_size in step with i_size. Return 0 for a write that ends
+ * past phys_size, so that it falls back to a buffered write.
+ */
+ if (iov_iter_rw(iter) == WRITE &&
+ iocb->ki_pos + count > HFS_I(inode)->phys_size)
+ return 0;
+
ret = blockdev_direct_IO(iocb, inode, iter, hfs_get_block);
/*
--
2.53.0
next prev parent reply other threads:[~2026-10-30 4:29 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-30 4:29 [PATCH 0/2] " Hengyu Liang
2026-10-30 4:29 ` Hengyu Liang [this message]
2026-10-30 4:29 ` [PATCH 2/2] hfs: remove the dead grow path from hfs_file_truncate() Hengyu Liang
2026-10-31 8:59 ` [syzbot ci] Re: hfs: fall back to buffered I/O for direct writes past phys_size syzbot ci
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261030042903.4160506-2-hengyul@cs.unc.edu \
--to=hengyul@cs.unc.edu \
--cc=frank.li@vivo.com \
--cc=glaubitz@physik.fu-berlin.de \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=slava@dubeyko.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®