failover file being read when read fails. #342

This commit is contained in:
Antonio SJ Musumeci 2016-12-09 17:15:13 -05:00
parent 437067b5e0
commit 7e53ef0c5a
7 changed files with 261 additions and 17 deletions

View File

@ -60,7 +60,7 @@ _create_core(const string &existingpath,
if(rv == -1) if(rv == -1)
return -errno; return -errno;
fh = reinterpret_cast<uint64_t>(new FileInfo(rv)); fh = reinterpret_cast<uint64_t>(new FileInfo(rv,flags));
return 0; return 0;
} }

View File

@ -20,13 +20,16 @@
class FileInfo class FileInfo
{ {
public: public:
FileInfo(int _fd) : FileInfo(const int _fd,
fd(_fd) const int _flags) :
fd(_fd),
flags(_flags)
{ {
} }
public: public:
int fd; int fd;
int flags;
}; };
#endif #endif

View File

@ -38,14 +38,22 @@ namespace fs
base += suffix; base += suffix;
} }
inline
void
make(const string &base,
const char *suffix,
string &output)
{
output = base + suffix;
}
inline inline
void void
make(const string *base, make(const string *base,
const char *suffix, const char *suffix,
string &output) string &output)
{ {
output = *base; output = *base + suffix;
output += suffix;
} }
} }
}; };

49
src/mutex.hpp Normal file
View File

@ -0,0 +1,49 @@
/*
Copyright (c) 2016, Antonio SJ Musumeci <trapexit@spawn.link>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#ifndef __MUTEX_HPP__
#define __MUTEX_HPP__
#include <pthread.h>
namespace mergerfs
{
namespace mutex
{
class Guard
{
public:
Guard(pthread_mutex_t &lock)
: _lock(&lock)
{
pthread_mutex_lock(_lock);
}
~Guard()
{
pthread_mutex_unlock(_lock);
}
private:
Guard();
private:
pthread_mutex_t *_lock;
};
}
}
#endif

View File

@ -49,7 +49,7 @@ _open_core(const string *basepath,
if(fd == -1) if(fd == -1)
return -errno; return -errno;
fh = reinterpret_cast<uint64_t>(new FileInfo(fd)); fh = reinterpret_cast<uint64_t>(new FileInfo(fd,flags));
return 0; return 0;
} }

View File

@ -17,12 +17,22 @@
#include <fuse.h> #include <fuse.h>
#include <string.h> #include <string.h>
#include <pthread.h>
#include <string> #include <string>
#include "config.hpp"
#include "errno.hpp" #include "errno.hpp"
#include "fileinfo.hpp" #include "fileinfo.hpp"
#include "fs_base_close.hpp"
#include "fs_base_open.hpp"
#include "fs_base_read.hpp" #include "fs_base_read.hpp"
#include "fs_path.hpp"
#include "mutex.hpp"
#include "rwlock.hpp"
#include "ugid.hpp"
using namespace mergerfs;
static static
int int
@ -38,6 +48,76 @@ _read(const int fd,
return ((rv == -1) ? -errno : rv); return ((rv == -1) ? -errno : rv);
} }
static
bool
_can_failover(const int error,
const int flags)
{
return (((error == -EIO) ||
(error == -ENOTCONN)) &&
!(flags & (O_CREAT|O_TRUNC)));
}
static
int
_failover_read_loop(const std::vector<std::string> &basepaths,
const char *fusepath,
char *buf,
const size_t count,
const off_t offset,
FileInfo *fi,
const int error)
{
int fd;
int rv;
std::string fullpath;
rv = fs::pread(fi->fd,buf,count,offset);
if(rv >= 0)
return rv;
for(size_t i = 0, ei = basepaths.size(); i != ei; i++)
{
fs::path::make(basepaths[i],fusepath,fullpath);
fd = fs::open(fullpath,fi->flags);
if(fd == -1)
continue;
rv = fs::pread(fd,buf,count,offset);
if(rv >= 0)
{
fs::close(fi->fd);
fi->fd = fd;
return rv;
}
fs::close(fd);
}
return error;
}
static
int
_failover_read(const char *fusepath,
char *buf,
const size_t count,
const off_t offset,
FileInfo *fi,
const int error)
{
static pthread_mutex_t failover_lock = PTHREAD_MUTEX_INITIALIZER;
const fuse_context *fc = fuse_get_context();
const Config &config = Config::get(fc);
const ugid::Set ugid(fc->uid,fc->gid);
const rwlock::ReadGuard read_guard(&config.srcmountslock);
const mutex::Guard failover_guard(failover_lock);
return _failover_read_loop(config.srcmounts,fusepath,buf,count,offset,fi,error);
}
namespace mergerfs namespace mergerfs
{ {
namespace fuse namespace fuse
@ -49,12 +129,14 @@ namespace mergerfs
off_t offset, off_t offset,
fuse_file_info *ffi) fuse_file_info *ffi)
{ {
int rv;
FileInfo *fi = reinterpret_cast<FileInfo*>(ffi->fh); FileInfo *fi = reinterpret_cast<FileInfo*>(ffi->fh);
return _read(fi->fd, rv = _read(fi->fd,buf,count,offset);
buf, if(_can_failover(rv,fi->flags))
count, rv = _failover_read(fusepath,buf,count,offset,fi,rv);
offset);
return rv;
} }
} }
} }

View File

@ -16,13 +16,26 @@
#if READ_BUF #if READ_BUF
#include <string>
#include <fuse.h> #include <fuse.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <pthread.h>
#include "config.hpp"
#include "errno.hpp" #include "errno.hpp"
#include "fileinfo.hpp" #include "fileinfo.hpp"
#include "fs_base_close.hpp"
#include "fs_base_open.hpp"
#include "fs_base_read.hpp"
#include "fs_path.hpp"
#include "mutex.hpp"
#include "rwlock.hpp"
#include "ugid.hpp"
using namespace mergerfs;
typedef struct fuse_bufvec fuse_bufvec; typedef struct fuse_bufvec fuse_bufvec;
@ -33,7 +46,9 @@ _read_buf(const int fd,
const size_t size, const size_t size,
const off_t offset) const off_t offset)
{ {
int rv;
fuse_bufvec *src; fuse_bufvec *src;
void *buf;
src = (fuse_bufvec*)malloc(sizeof(fuse_bufvec)); src = (fuse_bufvec*)malloc(sizeof(fuse_bufvec));
if(src == NULL) if(src == NULL)
@ -41,15 +56,100 @@ _read_buf(const int fd,
*src = FUSE_BUFVEC_INIT(size); *src = FUSE_BUFVEC_INIT(size);
src->buf->flags = (fuse_buf_flags)(FUSE_BUF_IS_FD|FUSE_BUF_FD_SEEK|FUSE_BUF_FD_RETRY); buf = malloc(size);
src->buf->fd = fd; if(buf == NULL)
src->buf->pos = offset; return -ENOMEM;
rv = fs::pread(fd,buf,size,offset);
if(rv == -1)
return -errno;
src->buf->mem = buf;
src->buf->size = rv;
*bufp = src; *bufp = src;
return 0; return 0;
} }
static
bool
_can_failover(const int error,
const int flags)
{
return (((error == -EIO) ||
(error == -ENOTCONN)) &&
!(flags & (O_CREAT|O_TRUNC)));
}
static
int
_open_file_and_check(const std::string &fullpath,
const int flags)
{
int fd;
int rv;
fd = fs::open(fullpath,flags);
if(fd == -1)
return -1;
rv = fs::pread(fd,&rv,1,0);
if(rv == -1)
return (fs::close(fd),-1);
return fd;
}
static
int
_failover_read_buf_loop(const std::vector<std::string> &basepaths,
const char *fusepath,
fuse_bufvec **bufp,
const size_t size,
const off_t offset,
FileInfo *fi)
{
int fd;
std::string fullpath;
for(size_t i = 0, ei = basepaths.size(); i != ei; i++)
{
fs::path::make(&basepaths[i],fusepath,fullpath);
fd = _open_file_and_check(fullpath,fi->flags);
if(fd == -1)
continue;
fs::close(fi->fd);
fi->fd = fd;
return _read_buf(fd,bufp,size,offset);
}
return -EIO;
}
static
int
_failover_read_buf(const char *fusepath,
fuse_bufvec **bufp,
const size_t size,
const off_t offset,
FileInfo *fi)
{
static pthread_mutex_t failover_lock = PTHREAD_MUTEX_INITIALIZER;
const fuse_context *fc = fuse_get_context();
const Config &config = Config::get(fc);
const ugid::Set ugid(fc->uid,fc->gid);
const rwlock::ReadGuard read_guard(&config.srcmountslock);
const mutex::Guard failover_guard(failover_lock);
return _failover_read_buf_loop(config.srcmounts,fusepath,bufp,size,offset,fi);
}
namespace mergerfs namespace mergerfs
{ {
namespace fuse namespace fuse
@ -61,12 +161,14 @@ namespace mergerfs
off_t offset, off_t offset,
fuse_file_info *ffi) fuse_file_info *ffi)
{ {
int rv;
FileInfo *fi = reinterpret_cast<FileInfo*>(ffi->fh); FileInfo *fi = reinterpret_cast<FileInfo*>(ffi->fh);
return _read_buf(fi->fd, rv = _read_buf(fi->fd,bufp,size,offset);
bufp, if(_can_failover(rv,fi->flags))
size, rv = _failover_read_buf(fusepath,bufp,size,offset,fi);
offset);
return rv;
} }
} }
} }