nfs-workarounds.c revision dec85d9856c33f427a06dda01e0e50de0bc8fa7d
/* Copyright (c) 2006-2007 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>
#ifdef __linux__
# define READ_CACHE_FLUSH_FCNTL
#endif
static int
void *context)
{
unsigned int i;
int ret;
t_push();
for (i = 1;; i++) {
break;
/* ESTALE: Some operating systems may fail with this if they
can't internally revalidating the NFS handle. It may also
happen if the parent directory has been deleted. If the
directory still exists, try reopening the file. */
break;
}
/* maybe it's gone or something else bad happened to
it. in any case we can't open the file, so fail
with the original ESTALE error and let our caller
handle it. */
break;
}
/* directory still exists, try reopening */
}
t_pop();
return ret;
}
struct nfs_safe_open_context {
int flags;
int fd;
};
{
}
{
struct nfs_safe_open_context ctx;
return -1;
}
{
}
{
}
{
}
{
}
{
/* ESTALE causes the OS to flush the attr cache */
return FALSE;
}
return TRUE;
}
return FALSE;
}
/* attr cache is flushed */
return TRUE;
}
}
return TRUE;
}
static void nfs_flush_chown_uid(const char *path)
{
/* ESTALE causes the OS to flush the attr cache */
return;
}
i_error("nfs_flush_chown_uid: stat(%s) failed: %m",
path);
return;
}
/* flush a negative cache entry. use effective UID to chown.
it probably doesn't really matter what UID is used, because
as long as we're not root we don't have permission to really
change it anyway */
}
/* attr cache is flushed */
return;
}
}
}
#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);
if (ret < 0) {
return;
}
if (old_lock_type == F_UNLCK) {
}
}
#endif
{
const char *p;
if (flush_dir) {
if (p == NULL)
nfs_flush_chown_uid(".");
else {
t_push();
t_pop();
}
}
}
{
}
int lock_type ATTR_UNUSED,
bool just_locked ATTR_UNUSED)
{
#ifdef READ_CACHE_FLUSH_FCNTL
if (!just_locked)
#else
/* FreeBSD, Solaris */
#endif
}