@@ -594,15 +594,20 @@ new_segment_file(Segment, SegmentEntryCount, State = #qi{ segments = Segments })
594
594
case file :allocate (Fd , 0 , Size ) of
595
595
ok ->
596
596
ok ;
597
+ % % For filesystems using copy-on-write such as ZFS, file preallocation
598
+ % % does not make any sense. For instance, on FreeBSD+ZFS,
599
+ % % posix_fallocate(2) fails with `EINVAL'.
600
+ % %
601
+ % % FIXME: Filling the file with zeroes is counter-productive because
602
+ % % it eats free space for no benefits and even worsen situations where
603
+ % % the disk is short on free space. However we still do it because the
604
+ % % rest of the code assumes that the file is preallocated.
605
+ {error , einval } ->
606
+ fill_file_with_zeroes (Fd , Size );
597
607
% % On some platforms file:allocate is not supported (e.g. Windows).
598
608
% % In that case we fill the file with zeroes manually.
599
609
{error , enotsup } ->
600
- ok = file :write (Fd , <<0 :Size /unit :8 >>),
601
- {ok , 0 } = file :position (Fd , bof ),
602
- % % We do a full GC immediately after because we do not want
603
- % % to keep around the large binary we used to fill the file.
604
- _ = garbage_collect (),
605
- ok
610
+ fill_file_with_zeroes (Fd , Size )
606
611
end ,
607
612
% % We then write the segment file header. It contains
608
613
% % some useful info and some reserved bytes for future use.
@@ -620,6 +625,14 @@ new_segment_file(Segment, SegmentEntryCount, State = #qi{ segments = Segments })
620
625
State # qi { segments = Segments #{Segment => 1 },
621
626
fds = OpenFds #{Segment => Fd } }.
622
627
628
+ fill_file_with_zeroes (Fd , Size ) ->
629
+ ok = file :write (Fd , <<0 :Size /unit :8 >>),
630
+ {ok , 0 } = file :position (Fd , bof ),
631
+ % % We do a full GC immediately after because we do not want
632
+ % % to keep around the large binary we used to fill the file.
633
+ _ = garbage_collect (),
634
+ ok .
635
+
623
636
% % We try to keep the number of FDs open at 4 at a maximum.
624
637
% % Under normal circumstances we will end up with 1 or 2
625
638
% % open (roughly one for reading, one for writing, when
0 commit comments