Git development
 help / color / mirror / Atom feed
From: Aman Ganapathy <amanganapathy@gmail.com>
To: git@vger.kernel.org
Cc: Patrick Steinhardt <ps@pks.im>, Karthik Nayak <karthik.188@gmail.com>
Subject: [PATCH] refs/files: sync symbolic refs to disk
Date: Fri,  4 Dec 2026 10:31:32 -0800	[thread overview]
Message-ID: <20261204183132.82882-1-amanganapathy@gmail.com> (raw)

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)


                 reply	other threads:[~2026-12-04 18:31 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261204183132.82882-1-amanganapathy@gmail.com \
    --to=amanganapathy@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=karthik.188@gmail.com \
    --cc=ps@pks.im \
    /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