Writing the NTFS journal the way Windows does
Falconry Labs · 26 November 2026
A USB drive gets pulled out. Sometimes it is a mistake, sometimes a loose cable, sometimes a dead battery. Whether the file system survives that is decided long before the cable moves, by how the driver orders its writes. This is a write-up of how we handled it in Falconry Drive, our NTFS driver for macOS 27. It runs on Apple's FSKit; the engine is based on Apple's open-source ntfs-168, with our own journal, repair tool and FSKit module.
Why the journal matters on a removable drive
NTFS updates several structures for one logical change. Creating a file touches an MFT record, a directory index block and allocation bitmaps. If power disappears between those writes, the structures disagree. Without a journal, the volume stays marked dirty and needs a full check before anyone can trust it.
NTFS keeps a metadata journal, $LogFile, for exactly this. Our goal is simple to state: a drive written on the Mac goes back into Windows, and Windows treats it as its own, with no “dirty, run chkdsk” after an unplug.
What $LogFile contains
The file has two layers. The lower one, LFS, is a generic log service. It starts with two restart pages (RSTR) holding the current LSN, the clients, and flags: one flag says the volume was closed cleanly, and a restart area without it means “in use”. After them come 4 KiB log pages (RCRD) with fixups, filled with records. A record can continue across pages.
An LSN is not a counter. It packs a sequence number into the high bits and the record's file offset divided by 8 into the low bits, so the log can wrap around and the order stays unambiguous.
On top sits NTFS's own record format. A client record names a redo operation, an undo operation, a target (an entry in the open attribute table), the clusters the target covers, and the data. Two operations carry most of the weight for us:
redo 0x02 InitializeFileRecordSegment
copy a whole MFT record image over the record on disk;
skipped if the record on disk already has an LSN >= this one
redo 0x08 UpdateNonresidentValue
copy bytes into a page of a non-resident attribute
(index block, bitmap…); no LSN check, replayed in log order
end ForgetTransaction (0x1b): the transaction is complete
The images in the log are the in-memory form, before the update-sequence fixups that are applied when a page goes to disk. Every page record also needs an open-attribute-table entry for its target and the LCNs of the clusters it covers; a record without LCNs is never redone.
Replay it, reset it, or write it
A driver has three options. It can replay what Windows left and then work without logging. It can reset the log to empty, which makes the volume look clean but gives up crash recovery. Or it can write real log records as it changes metadata. The first two are easier. We chose the third, because the first two leave a drive that was pulled on the Mac without the protection Windows gives it.
The risk is on the Windows side. A volume pulled from the Mac and plugged into Windows is replayed by Windows. A wrong record would corrupt it there, so the log has to be something Windows replays by design.
What we learned from how Windows behaves
Our rule is that when we doubt something about the journal, we look at how Windows' own NTFS driver does it before writing code. We studied its behaviour on a Windows 11 build and checked the findings against logs Windows itself wrote. Three results shaped the writer.
Windows logs whole images where it can
Windows itself logs a new or reused MFT record as a whole 1 KiB image with a Noop undo, and a new index block as a whole block. That is the same shape as the 0x02 and 0x08 operations above, so a writer that logs whole images is speaking a dialect Windows already uses.
Write-ahead, everywhere
Before the cache manager writes a dirty page, the log is flushed up to that page's newest LSN. Windows ends a transaction with ForgetTransaction, and the restart area's base LSN, the oldest record still needed, is the minimum of the checkpoint start, the oldest LSN of any still-dirty page and the oldest open transaction. Pages that are still dirty hold the base back.
We kept both rules. A metadata block whose newest change is not logged must not reach its home location. Where a block is written while a newer change is not yet logged, the write carries an older logged image, and the newer state stays dirty in the cache. A block that is logged but may not be home yet is tracked in a dirty block table, and the base LSN never moves past it.
A full log is never an excuse to drop a commit
In Windows, a record that does not fit raises a log-full status before anything is written. The operation is rolled back, a checkpoint frees space, and the request runs again. The clean flag is only set by a clean checkpoint after a full flush. Our notes are blunt that our own log-full handling is not yet this protocol. We first try to get logged work home and checkpoint again; only if that fails do we fall back to marking the volume dirty. That fallback is a documented residual risk, and we do not claim to match Windows there.
Where we differ on purpose
Windows opens a transaction per operation. We group completed operations. A short pause lets running operations finish, then the dirty set is copied. At that point the metadata is consistent, because no operation is half done, so the snapshot is logged as one transaction: OpenNonresidentAttribute for attributes not yet open, then the MFT record images, then index and bitmap chunks of up to 4 KiB, then ForgetTransaction. A commit is requested about once a second, or sooner when the pool is half full.
operations run ─► quiet point ─► copy the dirty metadata ─► one transaction in $LogFile:
OpenNonresidentAttribute attributes not yet open
MFT record images redo 0x02, whole 1 KiB records
index and bitmap chunks redo 0x08, up to 4 KiB each
ForgetTransaction end: the transaction is complete
The consequence is useful for recovery: replaying any prefix of committed snapshots gives a consistent volume, and in the normal case no undo is needed. Windows replays such a log fine, because every record is one it knows.
When the cable comes out
A commit must be all or nothing. In an early test, a pull in the middle of a large commit left its first log pages on disk, because full pages had already gone out as they filled. Both our replay and Windows' redid half a transaction.
The fix is an ordering rule in the log writer. It holds back the page containing the header of the first not-yet-durable record. It writes every later page, issues a barrier, then writes that page, so an unfinished commit is invisible to a reader that follows records one by one.
one commit = records R1…R5 on log pages P1…P4
P1 holds the header of R1, the first record not yet durable: the gate
1 write P2, P3, P4 and the tail copy P1 stays in memory
2 barrier the device must finish step 1 first
3 write P1 the gate
4 sync
cable out before step 3: a reader finds no R1 header in P1 and stops
there; the whole commit is invisible
cable out after step 3: R1…R5 are all on disk; the whole commit replays
Windows also taught us about the tail. When it saw newer, complete log pages after the last record reachable from the restart area, it logged corruption events and replayed nothing. The same pages marked as one write group with the missing first page are read as a torn write, with no event. So we mark the pages after the gate as one write group, the way Windows writes a flush.
After a pull, the next mount sees a restart area marked in use. On the Mac, the helper replays the journal before mounting read-write. On a Windows PC, Windows replays it itself.
How we judge correctness
Windows chkdsk on a real Windows machine is the judge, together with our own checker, fsck_peregrine. Other drivers are not judges. When our fsck and Windows disagree, Windows is right.
The first proof was hand-made. Before any driver code existed, we appended one transaction of whole images to the clean log of a Windows-formatted image and attached it to Windows. We built two cases: a small one (a file in an existing directory, 7 MFT records, 5 index blocks, 18 log records) and a large one (a new directory of 60 files with a new index allocation, a 300 KB file, a rename and a delete; 96 records across 40 log pages). Windows replayed both silently, with no NTFS events, and chkdsk found no problems. The new files, the 60 names, the rename and the delete were all there.
We then ran the real writer against Windows: continuing Windows' own log, a log wrapped twice over 22,871 filler transactions, and new logs in both LFS 1.1 and 2.0 formats. All replayed or mounted without events, and chkdsk was clean. The first LFS 2.0 run lost its last page because our tail copy was in the wrong place; Windows ignored it, we fixed the placement, and the rerun was clean.
A false alarm, and who was right
One overnight round produced an image that our fsck called broken: a directory index with a root pointing at a node that held only an end marker, which pointed to a leaf. Windows replayed the journal and ran chkdsk on the same image, with no complaint and no NTFS events. It was a legal intermediate state. We stopped trying to repair it. A checker that is stricter than Windows will eventually “fix” valid volumes, and that is a risk of its own.
The checkpoint that started in the wrong place
Our fuzzy checkpoint, which does not need every block home, first used a separate 0x38-byte start record. On a volume written that way, Windows logged event 55 and replayed nothing. Starting the checkpoint at the last record already logged fixed it, and both our replay and Windows’ then redid the transaction from before the checkpoint, with chkdsk clean. We found this by asking Windows, not by reading our own code.
Testing the pull itself
Our unplug tests clone a Windows-made image, run a workload, kill the disk image process at a random moment and check the result: replay, then fsck -n, then the dirty flags. A repair after a failed replay does not count as a pass. File contents are checked against a manifest: a file whose fsync completed must be intact, and a deleted file must not reappear. Tests on real drives need a physical disconnect, because an image or a software unplug does not lose the device's volatile cache.
Limits
A file being written can be incomplete
The journal covers metadata. Non-resident file data is written separately. A file that was still being written when the cable came out can be shorter than intended, or end in zeros. The volume around it stays consistent. To keep stale bytes of freed clusters from showing up, the driver keeps the valid-data length in the on-disk record behind the in-memory value until data is synced. Closing a file is not an fsync; fsync waits for the journal commit, and F_FULLFSYNC also flushes the device cache.
Hibernated and Fast Startup volumes stay read-only
When Windows hibernates, or shuts down with Fast Startup, it later resumes from a memory image and does not re-read NTFS metadata. Anything the Mac wrote under that image is invisible to the cached state, and the next Windows write uses stale LSNs, bitmaps and MFT cache.
We tested this on a USB data disk: the Mac wrote after Windows hibernated, then Windows resumed. The Mac's files were invisible at first, and read-only chkdsk reported an incorrect MFT bitmap and volume bitmap. After a Fast Startup boot, Windows could not open two of the Mac's files and logged corruption events. So we do not write, replay or repair a volume that is hibernated. It mounts read-only, and the user can discard the hibernation, which zeroes the first page of hiberfil.sys and loses the saved Windows session. The Mac sees hiberfil.sys only on a system volume; a separate data disk cannot be told apart from the volume alone.
The rule that keeps us honest
Every change we make falls into one of four classes. In-memory changes are free. Lazy states that NTFS itself allows are fine if measured and chkdsk stays silent. Intermediate states under the dirty flag are allowed only if our fsck repairs them the way chkdsk would. Anything chkdsk calls an error on a clean volume is never allowed, because that is where NTFS ends and our own format begins.
Try it on your own drives.
Falconry Drive reads and writes NTFS on macOS 27 with Apple silicon. 14 days of full read and write, free.