No space on Btrfs although there is plenty of free disk space

With Nixos or Nixpkgs on a Btrfs partition I regularly run into the issue that Btrfs cannot create files on the partition anymore although there is still enough free disk space. E.g. I have currently a 250 GB partition for NixOS and df tells that 50 GB are free.

I have read about Btrfs and Balancing and metadata vs. data chunks.

I still wonder however whether I can solve the issue once and for all. Can I pass some mount options to prevent the problem from occuring in the future? I exclusively encounter this problem on Btrfs partitions holding /nix/store. So it seems Nixos uses a filesystem in a way that Btrfs developers did not expect.

Freespace reporting is a known problem on BTRFS since basically forever, at least 5ish years ago they still claimed it wasn’t possible on COW systems to report free space properly (ZFS got that one right by the way).

Your best bet is to balance on a schedule.

1 Like

you can have the balance done automatically when required by enabling dynamic reclaim:

        systemd.services.btrfs-set-dynamic-reclaim = {
          description = "set dynamic reclain on all btrfs filesystems";
          wantedBy = [ "multi-user.target" ];
          script = /* bash */ ''
          shopt -s failglob
          for root in /sys/fs/btrfs/*-*/allocation/data; do
            echo 1 > "$root/dynamic_reclaim"
            echo 1 > "$root/periodic_reclaim"
          done
          '';
          serviceConfig = {
            Type = "oneshot";
            PrivateNetwork = true;
            ProtectHome = true;
            ProtectSystem = true;
            ReadWritePaths = [ "/sys/fs/btrfs" ];
          };
        };

it will trigger approximately when there is less than 10G unallocated (which is different than 10G free). The main downside is that when you have less than 10G free for a long time, the disk is constantly working due to the frequent automated balances.

2 Likes