udisks icon indicating copy to clipboard operation
udisks copied to clipboard

Mount exfat filesystems with the sync option by default

Open kotarou3 opened this issue 2 years ago • 3 comments
trafficstars

Removable drives are frequently formatted with the vfat and exfat filesystems

Currently vfat filesystems are mounted with the flush option by default: https://github.com/storaged-project/udisks/blob/060c9492d616f5b5287e834b50297e414c52f166/data/builtin_mount_options.conf#L4 This results in the write buffer being flushed frequently, so writes of large files end up being throttled to the disk's write speed (rather than RAM), resulting in accurate write speed indications in userspace and a speedy unmount

Flushing the write buffer frequently is especially important for removable drives with slow write speeds because:

  • The user may "yank" the drive without unmounting, resulting in data loss if the write buffer isn't empty
  • The user will have an indication of when the write will actually complete, and is likely to refrain from "yanking" until then
  • Even if the drive is unmounted, the user might not wait for an unknown-length unmount (potentially ranging from minutes to an hour for sufficiently large buffers and sufficiently slow write speeds) to complete before "yanking"

Unfortunately, exfat doesn't support the flush option at the moment, so the closest would be sync, which flushes every write. So please consider updating the default mount options on exfat to use the sync mount option

kotarou3 avatar Aug 28 '23 05:08 kotarou3

I'm not totally convinced to use sync by default, it's very expensive. Of couse the current status is not good either.

As @vojtechtrefny pointed out, there might be a way to limit the dirty buffer for a particular device - see /sys/class/bdi/<bdi>/max_bytes. However, this feature has only been added in kernel 6.2 (the exfat filesystem driver was added in 5.7): https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?id=00df7d51263b46ed93f7572e2d09579746f7b1eb https://lore.kernel.org/all/[email protected]/

tbzatek avatar Feb 05 '24 13:02 tbzatek

Came across this ticket looking for a way to get an usb drive with exfat to behave a bit more sane.

I wrote this udev rule to set the max_bytes to a more conservative value when an usb drive with exfat filesystem is added. I set it to the equivalent of 40MiB, that can probably be tweaked a bit.

# /etc/udev/rules.d/99-usb-exfat-max-bytes.rules

# Set usb drives with exfat to ~40MiB max_bytes so that they start syncing earlier
SUBSYSTEM=="block", ENV{ID_BUS}=="usb", ENV{ID_FS_TYPE}=="exfat", ENV{DEVTYPE}=="partition", ACTION=="add", \
    RUN+="/bin/sh -c 'echo 41943040 > /sys/class/bdi/%E{ID_PART_ENTRY_DISK}/max_bytes'"

(make sure to reload your udev rules, sudo udevadm control --reload && sudo udevadm trigger or systemctl restart systemd-udev-trigger)

Perhaps some find it useful.

trijpstra-fourlights avatar Feb 03 '25 08:02 trijpstra-fourlights

Perhaps some find it useful.

Very nice and useful indeed! I would like to see such support in UDisks for removable devices, for the moment I just have no clear idea how it should look like. To some degree a kind of writeback_buffer can be considered a drive configuration parameter besides power management for example. The value should probably take an actual average write speed of the drive in account for a sane buffer size.

tbzatek avatar Feb 03 '25 17:02 tbzatek