nfs-workarounds.c revision f9bda37757fcd418a55fe036acc182443c7a901f
/* Copyright (c) 2006-2008 Dovecot authors, see the included COPYING file */
/*
These tests were done with various Linux 2.6 kernels, FreeBSD 6.2 and
Solaris 8 and 10.
Attribute cache is usually flushed with chown()ing or fchown()ing the file.
The safest way would be to use uid=-1 gid=-1, but this doesn't work with
Linux (it does with FreeBSD 6.2 and Solaris). So we'll first get the
file's owner and use it. As long as we're not root the file's owner can't
change accidentally. If would be possible to also use chmod()/fchmod(), but
that's riskier since it could actually cause an unwanted change.
Write cache can be flushed with fdatasync(). It's all we need, but other
tested alternatives are: fcntl locking (Linux 2.6, Solaris),
fchown() (Solaris) and dup()+close() (Linux 2.6, Solaris).
Read cache flushing is more problematic. There's no universal way to do it.
The working methods are:
Linux 2.6: fcntl(), O_DIRECT
Solaris: fchown(), fcntl(), dup()+close()
FreeBSD 6.2: fchown()
fchown() can be easily used for Solaris and FreeBSD, but Linux requires
playing with locks. O_DIRECT requires CONFIG_NFS_DIRECTIO to be enabled, so
we can't always use it.
*/
#include "lib.h"
#include "nfs-workarounds.h"
#include <fcntl.h>
#include <unistd.h>
# define READ_CACHE_FLUSH_FCNTL
#endif
#if defined(__FreeBSD__) || defined(__sun)
# define ATTRCACHE_FLUSH_CHOWN_UID_1
#endif
static void nfs_flush_file_handle_cache_parent_dir(const char *path);
static int
void *context)
{
unsigned int i;
int ret;
for (i = 1;; i++) {
break;
/* ESTALE: Some operating systems may fail with this if they
can't internally revalidate the NFS file handle. Flush the
file handle and try again */
}
return ret;
}
struct nfs_safe_open_context {
int flags;
int fd;
};
{
}
{
struct nfs_safe_open_context ctx;
return -1;
}
{
}
{
}
{
}
{
}
{
if (!links1) {
return -1;
}
#ifndef __FreeBSD__
return 0;
#endif
/* FreeBSD at least up to v6.2 converts EEXIST errors to
success. */
return -1;
/* We don't know if it succeeded or failed. stat() to make sure. */
return -1;
return -1;
}
return 0;
}
static void nfs_flush_chown_uid(const char *path)
{
#ifdef ATTRCACHE_FLUSH_CHOWN_UID_1
#else
else {
/* ESTALE causes the OS to flush the attr cache */
return;
}
return;
}
return;
}
#endif
/* attr cache is flushed */
return;
}
return;
}
}
}
#ifdef __FreeBSD__
{
#ifndef ATTRCACHE_FLUSH_CHOWN_UID_1
return FALSE;
i_error("nfs_flush_attr_cache_fchown: fstat(%s) failed: %m",
path);
return TRUE;
}
#endif
return FALSE;
/* attr cache is flushed */
return TRUE;
}
i_error("nfs_flush_attr_cache_fd_locked: fchown(%s) failed: %m",
path);
}
return TRUE;
}
#endif
#ifdef READ_CACHE_FLUSH_FCNTL
{
int ret;
/* If the file was already locked, we'll just get the same lock
again. It should succeed just fine. If was was unlocked, we'll
have to get a lock and then unlock it. Linux 2.6 flushes read cache
alarm(60);
alarm(0);
return;
}
}
#endif
void nfs_flush_attr_cache_unlocked(const char *path)
{
int fd;
/* Try to flush the attribute cache the nice way first. */
if (fd != -1)
/* this already flushed the cache */
} else {
/* most likely ENOENT, which means a negative cache hit.
flush the file handles for its parent directory. */
}
}
void nfs_flush_attr_cache_maybe_locked(const char *path)
{
}
int fd ATTR_UNUSED)
{
#ifdef __FreeBSD__
/* FreeBSD doesn't flush attribute cache with fcntl(), so we have
to do it ourself. */
#else
/* Linux and Solaris are fine. */
return TRUE;
#endif
}
static bool
{
#ifdef __linux__
/* chown()ing parent is the safest way to handle this */
#else
/* rmdir() is the only choice with FreeBSD and Solaris */
i_warning("nfs_flush_file_handle_cache_dir: "
"rmdir(%s) unexpectedly "
"removed the dir. recreated.", path);
} else {
i_warning("nfs_flush_file_handle_cache_dir: "
"rmdir(%s) unexpectedly "
"removed the dir. mkdir() failed: %m", path);
}
/* expected failures */
return FALSE;
/* Solaris gives this if we're trying to rmdir() the current
directory. Work around this by temporarily changing the
current directory to the parent directory. */
int cur_dir_fd;
bool ret;
if (cur_dir_fd == -1) {
i_error("open(.) failed for: %m");
return TRUE;
}
i_error("nfs_flush_file_handle_cache_dir: "
"getcwd() failed");
(void)close(cur_dir_fd);
return TRUE;
}
if (p != NULL)
*p = '\0';
else {
p[0] = '/';
p[1] = '\0';
}
i_error("nfs_flush_file_handle_cache_dir: "
"chdir() failed");
}
if (fchdir(cur_dir_fd) < 0)
i_error("fchdir() failed: %m");
(void)close(cur_dir_fd);
return ret;
} else {
i_error("nfs_flush_file_handle_cache_dir: "
"rmdir(%s) failed: %m", path);
}
#endif
return TRUE;
}
static void nfs_flush_file_handle_cache_parent_dir(const char *path)
{
const char *p;
if (p == NULL)
else T_FRAME(
);
}
void nfs_flush_file_handle_cache(const char *path)
{
}
int fd ATTR_UNUSED)
{
#ifdef READ_CACHE_FLUSH_FCNTL
/* already flushed when fcntl() was called */
#else
/* we can only hope that underlying filesystem uses micro/nanosecond
resolution so that attribute cache flushing notices mtime changes */
#endif
}
{
#ifdef READ_CACHE_FLUSH_FCNTL
#else
#endif
}