Note about hashstore preallocation on XFS

[Below analysis is obtained with the help of Claude, written myself]

Starting with commit cb5706a released in v1.154.4, the node has started preallocating log files, defaulting to 64 MiB at a time. The general idea is to reduce fragmentation by allocating extra space for future writes. Notably, it does not ask for this extra space if the log file exceeds 1 GiB.

However, on XFS on Unraid which defaults to a dynamic allocsize, when a write exceeds the 1 GiB boundary by a couple of of bytes (e.g. the last write to a full log file) it decides to allocate another 1-2 GiB due to this dynamic scaling. After, the node closes the log file and opens a new one. One expects that XFS would recover the extra amount it allocated, but because of the preallocation XFS adds a XFS_DIFLAG_PREALLOC flag to the log file. Thus, XFS thinks the application will use the entire preallocated amount and never frees this.

As a result a 1 GiB log file can use as much as 3 GiB:

$ stat log-00000000000066f9-00000000
  Size: 1074388608   Blocks: 6291456    IO Block: 4096   regular file
Modify: 2026-08-30 23:29:19.775732498 -0700

$ du -h --apparent-size log-00000000000066f9-00000000   ->  1.1G
$ du -h                 log-00000000000066f9-00000000   ->  3.0G

$ xfs_io -r -c stat log-00000000000066f9-00000000
stat.size = 1074388608
stat.blocks = 6291456
fsxattr.xflags = 0x2 [-p---------------]     <- XFS_DIFLAG_PREALLOC, set by fallocate()
fsxattr.nextents = 3

$ xfs_bmap -vp log-00000000000066f9-00000000
 EXT: FILE-OFFSET         BLOCK-RANGE              AG AG-OFFSET                  TOTAL FLAGS
   0: [0..2097151]:       11309779136..11311876287  5 (572360936..574458087)   2097152 000000
   1: [2097152..2098415]: 23445811080..23445812343 10 (1970974680..1970975943)    1264 000000
   2: [2098416..6291455]: 23445812344..23450005383 10 (1970975944..1975168983) 4193040 010000
  • Extent 0: 1 GiB, the range preallocated by hashstore in 64 MiB steps, now written.
  • Extent 1: ~632 KiB, the tail of the last write that crossed max-log-size (1 GiB).
    Hashstore skips fallocate here because the next 64 MiB boundary is past 1 GiB.
  • Extent 2: ~2.0 GiB unwritten extent past EOF (flag 010000 = unwritten).

The node does track this nicely in terms of available, but does not count this allocated storage towards the used amount, so despite setting a 1 TiB free space buffer it’s been eating into it.

There are basically two simple fixes here for new writes:

  1. Disable preallocation by setting hashstore.store.prealloc-alignment: 0
  2. Set a allocsize mount flag

There is no need to do both, but you can. Then to recover the free space, I asked Claude to run fallocate --punch-hole on the files that have ballooned. For example see the bellow script:

punch-one-log.sh
#!/bin/bash
# punch-one-log.sh - release blocks allocated past EOF on one closed hashstore log (XFS).
# Usage: punch-one-log.sh <container-name> <path-to-log-file>
# Run on the host with the storagenode container STOPPED.
set -euo pipefail

CONTAINER=$1
F=$2
MAX_LOG_SIZE=1073741824   # hashstore.compaction.max-log-size (1 GiB default)

# --- safety checks ---------------------------------------------------------
# 1. node must be stopped, so nothing has the log open or appends to it
if [ "$(docker inspect -f '{{.State.Running}}' "$CONTAINER")" = "true" ]; then
  echo "refusing: $CONTAINER is running"; exit 1
fi

# 2. must be on XFS (punch semantics past EOF were only verified there)
[ "$(stat -f -c %T "$F")" = "xfs" ] || { echo "refusing: not on XFS"; exit 1; }

S=$(stat -c %s "$F")            # apparent size (bytes of real data)
B=$(stat -c %b "$F")            # allocated 512-byte blocks
ALLOC=$((B * 512))

# 3. only full (closed) logs - those are never appended to again
[ "$S" -ge "$MAX_LOG_SIZE" ] || { echo "skip: log not full ($S bytes)"; exit 0; }

# 4. punch start = first 4 KiB block boundary at/after EOF, so the block that
#    holds the last bytes of data is never touched
OFF=$(( (S + 4095) / 4096 * 4096 ))
LEN=$(( ALLOC - OFF ))
[ "$LEN" -gt 0 ] || { echo "skip: nothing allocated past EOF"; exit 0; }

MNT=$(stat -c %m "$F")
echo "file:          $F"
echo "size:          $S"
echo "allocated:     $ALLOC"
echo "punch:         offset=$OFF len=$LEN ($((LEN / 1048576)) MiB)"
echo "fs free before: $(df -B1 --output=avail "$MNT" | tail -1)"

# --- checksum, punch, checksum ---------------------------------------------
SUM1=$(sha256sum "$F" | cut -d' ' -f1)
echo "sha256 before: $SUM1"

# --punch-hole implies KEEP_SIZE: the file size is not changed
fallocate --punch-hole --offset "$OFF" --length "$LEN" "$F"
sync -f "$F"

SUM2=$(sha256sum "$F" | cut -d' ' -f1)
echo "sha256 after:  $SUM2"

# --- verify ----------------------------------------------------------------
S2=$(stat -c %s "$F")
[ "$S" = "$S2" ]       || { echo "!!! SIZE CHANGED ($S -> $S2)"; exit 2; }
[ "$SUM1" = "$SUM2" ]  || { echo "!!! CONTENT CHANGED"; exit 2; }

echo "allocated now: $(( $(stat -c %b "$F") * 512 ))"
echo "fs free after:  $(df -B1 --output=avail "$MNT" | tail -1)"
xfs_bmap -vp "$F"
echo "OK: content identical, $((LEN / 1048576)) MiB released"

This will also likely depend on how fragmented the logs are. On this particular node the log file was contiguous with a decent amount of free space, which led to large allocation. On another node that was was more fragmented, the last extent was significantly less than 1 GiB, so the additional allocation was less. This node I reclaimed about 30% of overallocation, but an older node that was close to full only around 7%. Here, there were more logs that were affected by the overallocation but the amount was less.

I also have a node on ZFS which this doesn’t occur, mostly as it is a CoW filesystem. The node’s preallocation here does nothing. No idea about ext4.

So this is caused by Unraid choosing to use XFS with a dynamic allocsize? If so, then mounting the XFS filesystem with a fixed allocsize (like 4k?) should stop it from creating the problem?

It’s true for Unraid and likely also true for standard XFS filesystem creation, that allocsize is not set to a fixed number so it just defaults to the dynamic behavior.

A fixed allocsize should fix it, say 64MiB, which matches the prealloc-alignment default. Fairly minimal overhead vs 1GiB. I have not tested myself as I just turned off preallocation.

I’m already doing this for an unrelated reason: I believe ext4’s delayed allocation is a strictly better option to preallocation.

This shall be the default. Software by default should not try to game the environment and assume its incompetency, especially cross platform software.

I wonder what other sticks is node showing in its wheels.

I’m guessing that if I do this, but don’t run the script to recover the free space, that over time the free space will be recovered as the logs are either filled or removed.

Cheers.