# Investigating a FreeBSD/OpenZFS Panic

## Current conclusion

The original FreeBSD 15.1-RELEASE-p3 panic is confirmed physical-dnode
corruption. Object 815488 occupies slot 0 of dnode block 25484. Its in-core
dnode is coherent and points to the correct slot, but that slot begins with
two file-data block pointers instead of a dnode header.

**The writer and root cause remain unidentified.** Matching block pointers
in BRT records and a clone dirty record connect the affected data to block
cloning, but do not establish that cloning, BRT prefetch, or ARC sharing
caused the overwrite. The diagnostic patches are detectors, not a proven fix.

This account separates the original dump evidence, unsuccessful reproduction
attempts, and subsequent instrumentation. Detailed procedures remain in
[CRASH.md](CRASH.md), [DEBUGGING.md](DEBUGGING.md), and
[WRITER_KERNEL_PLAN.md](WRITER_KERNEL_PLAN.md).

## What the original dump establishes

The matching GENERIC-DEBUG kernel and vmcore show this panic:

```text
VERIFY(DMU_OT_IS_VALID(dn->dn_phys->dn_type)) failed

dnode_verify()
dnode_sync()
dmu_objset_sync_dnodes()
sync_dnodes_task()
taskq_run()
taskqueue_run_locked()
taskqueue_thread_loop()
```

The detecting thread is a ZFS sync taskq thread. These addresses and object
numbers apply to the original dump; they must not be reused for another crash.

| Item | Value |
|---|---|
| In-core dnode | `0xfffff8019e467000` |
| Object | `815488` |
| Physical slot / dbuf data | `0xfffffe00ff36c000` |
| Dnode-block dbuf | `0xfffff80277fe02f0` |
| Dbuf object / level / block ID | `0 / 0 / 25484` |
| Dbuf size / state | `16384 / DB_CACHED` |
| Physical / cached type | `88 (0x58) / DMU_OT_PLAIN_FILE_CONTENTS` |
| Dirty TXG | `1175694` |
| Pending dirty record | `0xfffff8044ff1ba48` |

A 16 KiB block contains 32 minimum-size dnode slots. Since
`25484 * 32 = 815488`, the affected object is slot 0. The expected slot
calculation equals `dn_phys` exactly. This rules out an obvious slot-index
error at the time of the dump; it does not prove the entire lifetime was valid.

Slot 0 starts with `58 00 00 00 00 00 00 00`. Slots 1, 2, and 31 look like
ordinary dnodes. For example, slot 1 starts with
`13 11 01 01 2c 00 00 0b`, describing plain-file data with one block pointer
and a 176-byte bonus. The damage appears localized rather than a replacement
of the entire 16 KiB block.

The cached dnode fields remain coherent: type, bonus length, block-pointer
count, levels, and indirect-block shift agree with an ordinary file dnode.
The dbuf, pending dirty record, and ARC data reference the same damaged memory.
The dirty record therefore does not provide a separate historical copy.

### The two block pointers

The first 256 bytes decode as two consecutive 128-byte `blkptr_t` values.
Copies match BPs retained in BRT tree entries.

| BP | Raw first-DVA words (decimal) | Birth TXG | Logical size | Physical size |
|---|---|---|---|---|
| 1 | `(88, 482663448)` | `1175483` | 128 KiB | 45056 bytes |
| 2 | `(24, 482663544)` | `1175483` | 128 KiB | 12288 bytes |

Both describe LZ4-compressed plain-file data. They are **block pointers held
by BRT entries**, not evidence that the slot contains BRT ZAP data.

The dbuf scan found no second live dbuf aliasing the target, and no other
readable metadata buffer containing the exact pair. The scan reported 63,392
missing metadata pages. Large indirect buffers were unavailable in this
minidump, so absence from the scan does not exclude an indirect block or
temporary BP array as the source.

### What those pointers reference

On September 27, both physical extents were read from vdev 0. Their Fletcher4
checksums matched the dump BPs, and each decompressed to 128 KiB. This confirms
that the bytes read matched the recorded block contents; it does not establish
every intervening write or allocation event.

Concatenating BP1 and BP2 produced a valid FreeBSD x86-64 relocatable ELF
object with debug information. Embedded strings identify:

```text
/home/wongk/projects/rgand/ripgrep
crates/ignore/src/walk.rs
crates/ignore/examples/walk.rs
rustc 1.96.1 (31fca3adb 2026-06-26)
```

BP1 contains the ELF header, code, and debug sections; BP2 continues with
relocations, symbols, and section tables. This strongly suggests consecutive
blocks of one temporary Rust build object. The dnode slot contains pointers
to those contents, not the ELF contents themselves.

Objset 99 maps to `zroot/home/wongk`, not the MOS. A pathname search found no
current name for object 815488. Its original pathname and the exact source
file identity remain unresolved.

## What the source supports

The reviewed source baseline is commit
`88e7371d9dc26f85dfc1b008cbe59ebc7e4a33da`. The diagnostic worktree is
`zfsdiag/src`; the separate checkout at `/home/wongk/freebsd-src` has local
changes that must be considered independently.

- The failing `dnode_verify()` observes damage before the relevant physical
  dnode updates in `dnode_sync()`. This establishes discovery order, not the
  TXG or thread that performed the overwrite.
- Allocation/free paths can clear physical slots. Zeroing alone does not
  explain two structured file-data BPs.
- `dbuf_write_ready()` stores a complete BP through
  `*db->db_blkptr = *bp`. A wrong destination could misplace one BP; explaining
  both adjacent BPs requires two writes or a larger copy.
- `dmu_brt_clone()` sets up clone dirty records and calls
  `brt_pending_add()`. In the reviewed clone path, `brt_prefetch()` is reached
  in open context. No demonstrated path connects that prefetch to overwriting
  a held dnode buffer.

## Working hypotheses

These are investigation priorities, not measured probabilities.

1. **Wrong metadata destination or BP-array copy.** The structured replacement
   bytes make BP stores and bulk BP copies concrete places to inspect. No
   captured store yet identifies either path as responsible.
2. **Buffer lifetime or ownership failure.** A stale dbuf/ARC reference or
   allocator reuse could explain coherent cached fields over damaged memory.
   Matching pointers at panic time do not establish their lifetime history.
3. **Unrelated kernel overwrite.** Earlier fd-table/mqueue crashes make a
   broader corruption mechanism worth considering, but no shared writer,
   allocation, or address has been demonstrated.
4. **On-disk corruption or hardware fault.** Still possible. The structured
   bytes and localized damage make a software write/copy worth prioritizing;
   they do not exclude these alternatives.

Block-clone handling could participate in either of the first two hypotheses.
The BRT matches alone do not justify treating BRT as the culprit.

### Workload and earlier panics

The crashed system had large fd-table growth and a Codex workload with about
30,000 vnode descriptors. Earlier exit/mqueue panics included invalid
`struct file *` values, one matching a Capsicum rights mask. Those are separate
observations, not proof of one common bug.

On another machine, the reported workload held about 202,000 vnode descriptors
to 185,600 unique files, alongside very high vnode churn. That machine used
`vfs.vnode.param.can_skip_requeue=1` as a runtime workaround for a VFS locking
bottleneck. The crashed machine did not have that setting enabled.

Contention could alter timing and expose a race, but its contribution to this
ZFS panic is unproven. Treat the userspace workload as a stressor. Neither
requeue skipping nor disabling block cloning is an established fix here.

## Superseded interpretations

Earlier notes overstated several leads. They are retained here briefly so
they are not mistaken for confirmed findings elsewhere in the investigation.

- **“BRT prefetch reused held ARC memory.”** Unsupported. No captured ownership
  transition proves it. The proposed `b_hold_count` guard was based on an
  unestablished safeguard, not a demonstrated defect in `arc_can_share()`.
  Claims that its sharing criteria were observed to succeed during the
  corrupting event are also unproven.
- **“Both dirty records are corrupt or mislinked.”** The cited addresses
  `0xfffff8044ff1ba70` and `0xfffff804368b2be8` are list-node addresses.
  The recorded list offset is 40 bytes; subtracting it yields record bases
  `0xfffff8044ff1ba48` and `0xfffff804368b2bc0`. Casting the nodes directly
  as records misreads their fields. The recheck found the clone record linked
  to its own dbuf. The claimed freed-memory evidence is not established by
  those casts. Dirty-record unions must also be decoded by record level.
- **“A ZIO at 0xfffff80440d75155 proves a corrupt write was queued.”** This
  candidate was not validated as a live ZIO and is not used as evidence.
  A single `io_bp_copy` is one BP, so the earlier claim that it contained
  both BPs also requires correction.
- **“Fill a pool above 90% to force the original DVAs.”** Occupancy does not
  reproduce specific allocation addresses. A fuller pool can be a separate
  VM stress condition, not a way to recreate the exact overwrite.

The list-node recheck is captured in
[investigation/diagnosis-recheck.txt](investigation/diagnosis-recheck.txt).
Historical notes, including parts of [TDD_PLAN.md](TDD_PLAN.md), still contain
superseded claims; use the corrections above when interpreting them.

## Reproduction attempts

Run reproducers only in `test-zfs2`, never on the production host.

| Date | Kernel / workload | Result |
|---|---|---|
| 2026-09-25 | Diagnostic GENERIC-DEBUG #6; 100 clone iterations | No panic; reported SHA256 matches |
| 2026-09-25 | Original crash kernel #1; single clone and 100 iterations | No panic; reported SHA256 matches |
| 2026-09-26 | 1,000 iterations of 128 KiB `copy_file_range()` | No panic; whole-file checksum comparison was unsuitable for the partial-copy test |

These attempts did not reproduce the original corruption or validate a fix.
The partial-copy test needs range/length-aware verification before its checksum
result can be interpreted. A separate VM fasttrap/Witness panic is not evidence
of reproducing the dnode overwrite.

Useful next workloads combine concurrent clones, ordinary writes, truncation,
deletion/reuse, and syncing. Change one condition at a time and record the exact
kernel/module identity. Prefetch and vnode tunables are secondary timing
experiments, not presumed causes.

## Diagnostic kernel: implementation and limits

The diagnostic builds use the #1 source baseline and GENERIC-DEBUG, with patches
saved under `zfsdiag/`. The intended boot directory is
`/boot/kernel.zfsdiag`, containing the kernel and `zfs.ko`.

The initial revisions added detailed dnode panic output, dnode-block scans at
read/dirty/write-ready boundaries, and BP destination checks. v3 added a
parent-buffer containment check, ownership checks, and a clone ring. The v4
candidate removes that ring and its correlation scan, narrows ownership checks
to physical dnode blocks, removes an overlapping verifier call, and captures
one first-failure snapshot. It has been built but not installed or booted.

Artifacts include:

- [Current patch](zfsdiag/dnode-write-diagnostic.patch).
- [Previous v2 patch](zfsdiag/dnode-write-diagnostic-v2.patch).
- [v4 build log](zfsdiag/build-v4.log).
- [Implementation and validation plan](WRITER_KERNEL_PLAN.md).

### Corrections applied in v4

The patch review identified unnecessary work and unreliable evidence handling:

- The clone ring and its lockless correlation scan were removed; clone
  correlation remains postmortem work.
- The added `DBUF_VERIFY()` was removed; the targeted BP checks remain.
- Ownership checking now returns immediately for non-dnode dbufs.
- One allocation-free in-memory snapshot retains the first failing context and
  slot bytes for kgdb.

Compilation alone does not establish production safety. Runtime overhead,
legitimate transitions, and snapshot retention need VM validation.

### What a diagnostic panic can tell us

Panic is appropriate when a validated invariant proves metadata ownership or
a write destination is wrong. It preserves a dump and stops that execution
path from proceeding. It does not guarantee that no damaged data was already
written.

A boundary check captures the **detecting stack**. Only a check at the offending
store, or sufficiently synchronized lifetime evidence, can identify the writer.
The current patch does not implement a continuous checksum history or capture
every BP write. Even a checksum transition would bracket the change rather
than automatically reveal its author.

A minidump may suffice if the needed records and buffers survive. Preserve the
exact kernel, modules, symbols, patches, crash messages, and workload notes.
Test record retention in the VM instead of assuming every relevant page is
present.

## Next steps

- [ ] Validate normal operations and diagnostic failures in the VM.
- [ ] Verify saved minidumps contain usable diagnostic records and symbols.
- [ ] Measure overhead and false positives before calling a revision production-safe.
- [ ] Repair partial-copy verification and run concurrent mixed VM workloads.
- [ ] Preserve future host dumps with exact build artifacts.
- [ ] Identify a specific bad write or lifetime transition before proposing a fix.

The immediate goal is a trustworthy earlier observation of the corruption.
That may be enough to identify the bug; it is not yet a promise that the next
panic will provide the exact writer instruction.
