Handle the case of posix_fadvise() returning ENOENT

This commit is contained in:
Andrea Mazzoleni 2026-04-04 20:02:30 +02:00
parent de598080c4
commit 35b5c46851
4 changed files with 81 additions and 9 deletions

View file

@ -14,6 +14,63 @@ int exit_success = 0;
int exit_failure = 1;
int exit_sync_needed = 2;
#if HAVE_POSIX_FADVISE
/**
* Wrapper around posix_fadvise() that handles ENOENT by converting it to
* ENOSYS.
*
* Why can posix_fadvise() return ENOENT?
*
* According to POSIX and the Linux kernel's standard fadvise64 implementation
* (mm/fadvise.c), posix_fadvise() should only return EBADF (bad file
* descriptor), ESPIPE (fd refers to a pipe or FIFO), or EINVAL (invalid
* advice value or arguments). ENOENT is never generated by the kernel's
* generic path.
*
* However, the VFS layer allows individual filesystems to provide their own
* .fadvise() method, which is called instead of (or in addition to) the
* generic implementation. Certain filesystem drivers can return ENOENT in
* this method for reasons that are completely unrelated to the file not
* existing (the file is clearly open, we have a valid fd):
*
* - NFS: A stale NFS inode can be revalidated against the server while
* fadvise is being processed. If the server-side file has been removed or
* renamed, the revalidation discovers the inode is gone and returns
* -ENOENT, even though the client still holds an open file descriptor.
*
* - FUSE-based filesystems (mergerfs, sshfs, rclone, etc.): The FUSE daemon
* receives the fadvise request and may forward it to an underlying backend.
* If that backend cannot find the file (e.g., the backing path has changed,
* the FUSE daemon is partially initialised, or the daemon translates the
* request and the target layer returns an error), the daemon may return
* ENOENT to the kernel.
*
* - Overlayfs (used by Docker, Podman, LXC): The overlayfs .fadvise()
* implementation delegates to the lower or upper layer. If the lookup
* through the overlay stack fails to find the real inode (e.g., the upper
* directory entry is absent and the lower copy is in a weird state), the
* delegation can return ENOENT.
*
* - Other network or virtual filesystems: Any out-of-tree or in-tree
* filesystem that implements .fadvise() and performs a path or inode
* lookup as part of processing the hint can propagate ENOENT if that
* internal lookup fails.
*
* Because posix_fadvise() is always an advisory hint, it never affects
* correctness, only performance, it is safe to treat unexpected errors,
* including ENOENT, as "not supported" (ENOSYS) and continue.
*/
int posix_fadvise_wrapper(int fd, off_t offset, off_t len, int advice)
{
int ret = posix_fadvise(fd, offset, len, advice);
if (ret == ENOENT)
return ENOSYS;
return ret;
}
#endif
int open_noatime(const char* file, int flags)
{
#ifdef O_NOATIME
@ -2054,7 +2111,11 @@ static int devup(dev_t device, const char* name)
#if HAVE_POSIX_FADVISE
/* clear cache */
ret = posix_fadvise(f, offset, 4096, POSIX_FADV_DONTNEED);
ret = posix_fadvise_wrapper(f, offset, 4096, POSIX_FADV_DONTNEED);
if (ret == ENOSYS) {
/* call is not supported */
ret = 0;
}
if (ret != 0) {
/* LCOV_EXCL_START */
close(f);