[nginx] Adjusted ngx_file_fs_size() workaround for XFS.

Maxim Dounin mdounin at mdounin.ru
Sun Dec 14 02:59:57 UTC 2025


details:   http://freenginx.org/hg/nginx/rev/f5ce4ebcec7e
branches:  
changeset: 9449:f5ce4ebcec7e
user:      Maxim Dounin <mdounin at mdounin.ru>
date:      Sun Dec 14 02:36:34 2025 +0300
description:
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.

diffstat:

 src/os/unix/ngx_files.h |  2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

diffs (12 lines):

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