BTRFS file corruption on SanDisk SD cards (potential fix)

Just making a note for anyone else running Raspbian 11 Bullseye: I was experiencing repeated crashes and found lots of btrfs “mmc_erase” errors in syslog. Long story short it appears there is a lowlevel issue with btrfs discard and some SD cards. The fix that appears (so far) to work is to add “nodiscard” to the btrfs options in fstab:

proc            /proc           proc    defaults          0       0
PARTUUID=123xyzpdq-01  /boot/efi           vfat    defaults          0       2
PARTUUID=123xyzpdq-02  /               btrfs    defaults,noatime,nodiscard  0       1

Ref: [PATCH 5.10 77/79] mmc: core: Add SD card quirk for broken discard - Greg Kroah-Hartman
Ref: btrfs file corruption with pci-e sd card reader with discard mount option(likely a driver bug?) : btrfs

For future reference, see #2 of Troubleshooting General Boot Issues

SD cards should have TRIM since a decade again. It’s not new technology. Usually fake SD cards use different controllers than the manufacturer’s original and those controllers are optimized for cost so do not implement the spec. Tier 1 manufacturer controllers will definitely have the mmc discard capability.