[PATCH] Adjusted ngx_file_fs_size() workaround for XFS
Maxim Dounin
mdounin at mdounin.ru
Sat Dec 6 09:10:21 UTC 2025
# HG changeset patch
# User Maxim Dounin <mdounin at mdounin.ru>
# Date 1765007183 -10800
# Sat Dec 06 10:46:23 2025 +0300
# Node ID 3a7cbb9c60d5e058b41d2aae6e7890e89099e556
# Parent 234a79a752d46a21ecaccddfda660ca2cbfe15f4
Adjusted ngx_file_fs_size() workaround for XFS.
With XFS, creating a file might result in large preallocation being
reported in st_blocks as returned by fstat() till the file is closed,
resulting in incorrect cache size calculations and too aggressive cache
clearing based on max_size.
This was worked around in 7669:52b34c3f89b4 by introducing the arbitrary
sanity limit of eight blocks (8 * st_blksize) for the difference between
st_size and size in st_blocks. Unfortunately, it turns out that XFS
might return very large values in st_blksize as well, notably when using
the "largeio" mount option, which makes the limit ineffective.
As a workaround, the limit is changed to 32k instead, which is expected
to be large enough to cover real differences between st_size and size
in st_blocks, yet small enough to effectively suppress bogus st_blocks
values with preallocations. It also matches the previous limit in many
practical cases (with st_blksize 4096, previous limit was 32k).
Reported by Oleg Mamontov.
diff --git a/src/os/unix/ngx_files.h b/src/os/unix/ngx_files.h
--- a/src/os/unix/ngx_files.h
+++ b/src/os/unix/ngx_files.h
@@ -187,7 +187,7 @@ ngx_int_t ngx_set_file_time(u_char *name
#define ngx_file_size(sb) (sb)->st_size
#define ngx_file_fs_size(sb) \
(((sb)->st_blocks * 512 > (sb)->st_size \
- && (sb)->st_blocks * 512 < (sb)->st_size + 8 * (sb)->st_blksize) \
+ && (sb)->st_blocks * 512 < (sb)->st_size + 32768) \
? (sb)->st_blocks * 512 : (sb)->st_size)
#define ngx_file_mtime(sb) (sb)->st_mtime
#define ngx_file_uniq(sb) (sb)->st_ino
More information about the nginx-devel
mailing list