Git development
 help / color / mirror / Atom feed
* [PATCH] refs/files: sync symbolic refs to disk
@ 2026-12-04 18:31 Aman Ganapathy
  0 siblings, 0 replies; only message in thread
From: Aman Ganapathy @ 2026-12-04 18:31 UTC (permalink / raw)
  To: git; +Cc: Patrick Steinhardt, Karthik Nayak

With core.fsync=reference, write_ref_to_lockfile() syncs a loose ref to
disk before it is renamed into place. Symbolic refs like HEAD are
written by create_symref_lock() instead, which never syncs them.
gitglossary(7) describes a symbolic ref as a kind of ref, so a user who
sets core.fsync=reference would expect them to be synced as well.

On XFS, a crash shortly after "git checkout" or "git clone" with
core.fsync=all can leave ".git/HEAD" empty. Git then no longer
recognizes the repository and every command fails with "fatal: not a
git repository", which is misleading since only HEAD is broken.

Fix this by flushing and syncing the lock file in create_symref_lock(),
the same way the packed-refs file is synced.

Signed-off-by: Aman Ganapathy <amanganapathy@gmail.com>
---
I ran into this while testing crashes with core.fsync=all. Here's a
small repro (needs root and xfsprogs):

    truncate -s 300M /tmp/disk.img && mkfs.xfs -q /tmp/disk.img
    mount -o loop /tmp/disk.img /mnt && cd /mnt
    git init -q repo && cd repo
    git config core.fsync all
    git commit -q --allow-empty -m one && git branch other && sync
    git checkout -q other
    git commit -q --allow-empty -m two
    xfs_io -x -c shutdown /mnt
    cd / && umount /mnt && mount -o loop /tmp/disk.img /mnt
    git -C /mnt/repo status   # "fatal: not a git repository"

.git/HEAD is 0 bytes after the remount. With the patch it still says
"ref: refs/heads/other". On ext4 HEAD survived, I think because
auto_da_alloc flushes the data when a file is renamed over another one.

 refs/files-backend.c        |  4 +++-
 t/t0600-reffiles-backend.sh | 29 +++++++++++++++++++++++++++++
 2 files changed, 32 insertions(+), 1 deletion(-)

diff --git a/refs/files-backend.c b/refs/files-backend.c
index 1bdb16c0e7..394c85d9ad 100644
--- a/refs/files-backend.c
+++ b/refs/files-backend.c
@@ -2204,7 +2204,9 @@ static int create_symref_lock(struct ref_lock *lock, const char *target,
 		return -1;
 	}
 
-	if (fprintf(get_lock_file_fp(&lock->lk), "ref: %s\n", target) < 0) {
+	if (fprintf(get_lock_file_fp(&lock->lk), "ref: %s\n", target) < 0 ||
+	    fflush(get_lock_file_fp(&lock->lk)) ||
+	    fsync_component(FSYNC_COMPONENT_REFERENCE, get_lock_file_fd(&lock->lk)) < 0) {
 		strbuf_addf(err, "unable to write to %s: %s",
 			     get_lock_file_path(&lock->lk), strerror(errno));
 		return -1;
diff --git a/t/t0600-reffiles-backend.sh b/t/t0600-reffiles-backend.sh
index 26385aab7d..1aceb19bfb 100755
--- a/t/t0600-reffiles-backend.sh
+++ b/t/t0600-reffiles-backend.sh
@@ -580,4 +580,33 @@ test_expect_success 'deleting root and packed refs in one transaction requires p
 	)
 '
 
+check_fsync_events () {
+	local trace="$1" &&
+	shift &&
+
+	cat >expect &&
+	sed -n \
+		-e '/^{"event":"counter",.*"category":"fsync",/ {
+			s/.*"category":"fsync",//;
+			s/}$//;
+			p;
+		}' \
+		<"$trace" >actual &&
+	test_cmp expect actual
+}
+
+test_expect_success 'symref updates are synced' '
+	test_when_finished "rm -rf repo trace2.txt" &&
+	git init repo &&
+	test_commit -C repo initial &&
+
+	GIT_TRACE2_EVENT="$(pwd)/trace2.txt" \
+	GIT_TEST_FSYNC=true \
+		git -C repo -c core.fsync=reference \
+		-c core.fsyncMethod=fsync symbolic-ref HEAD refs/heads/other &&
+	check_fsync_events trace2.txt <<-EOF
+	"name":"hardware-flush","count":1
+	EOF
+'
+
 test_done
-- 
2.39.5 (Apple Git-154)


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-12-04 18:31 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 18:31 [PATCH] refs/files: sync symbolic refs to disk Aman Ganapathy

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox