Skip to content

Commit

Permalink
Merge pull request #352 from trapexit/directio
Browse files Browse the repository at this point in the history
use different read and write functions when using direct_io
  • Loading branch information
trapexit authored Dec 20, 2016
2 parents 437067b + 1aa76a5 commit 10a9918
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 19 deletions.
1 change: 1 addition & 0 deletions src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ namespace mergerfs
srcmountslock(),
minfreespace(MINFREESPACE_DEFAULT),
moveonenospc(false),
direct_io(false),
POLICYINIT(access),
POLICYINIT(chmod),
POLICYINIT(chown),
Expand Down
1 change: 1 addition & 0 deletions src/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ namespace mergerfs
mutable pthread_rwlock_t srcmountslock;
uint64_t minfreespace;
bool moveonenospc;
bool direct_io;

public:
const Policy *policies[FuseFunc::Enum::END];
Expand Down
13 changes: 9 additions & 4 deletions src/mergerfs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ namespace local

static
void
get_fuse_operations(struct fuse_operations &ops)
get_fuse_operations(struct fuse_operations &ops,
const bool direct_io)
{
#if FLAG_NOPATH
ops.flag_nopath = false;
Expand Down Expand Up @@ -115,7 +116,9 @@ namespace local
ops.open = mergerfs::fuse::open;
ops.opendir = mergerfs::fuse::opendir;
ops.poll = NULL;
ops.read = mergerfs::fuse::read;
ops.read = direct_io ?
mergerfs::fuse::read_direct_io :
mergerfs::fuse::read;
#if READ_BUF
ops.read_buf = mergerfs::fuse::read_buf;
#endif
Expand All @@ -133,7 +136,9 @@ namespace local
ops.unlink = mergerfs::fuse::unlink;
ops.utime = NULL; /* deprecated; use utimens() */
ops.utimens = mergerfs::fuse::utimens;
ops.write = mergerfs::fuse::write;
ops.write = direct_io ?
mergerfs::fuse::write_direct_io :
mergerfs::fuse::write;
#if WRITE_BUF
ops.write_buf = mergerfs::fuse::write_buf;
#endif
Expand Down Expand Up @@ -172,7 +177,7 @@ namespace mergerfs
mergerfs::options::parse(args,config);

local::setup_resources();
local::get_fuse_operations(ops);
local::get_fuse_operations(ops,config.direct_io);

return fuse_main(args.argc,
args.argv,
Expand Down
7 changes: 3 additions & 4 deletions src/option_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,9 @@ parse_and_process_arg(Config &config,
fuse_args *outargs)
{
if(arg == "defaults")
{
set_default_options(*outargs);
return 0;
}
return (set_default_options(*outargs),0);
else if(arg == "direct_io")
return (config.direct_io=true,1);

return 1;
}
Expand Down
41 changes: 36 additions & 5 deletions src/read.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "fs_base_read.hpp"

static
inline
int
_read(const int fd,
void *buf,
Expand All @@ -34,8 +35,29 @@ _read(const int fd,
int rv;

rv = fs::pread(fd,buf,count,offset);
if(rv == -1)
return -errno;
if(rv == 0)
return 0;

return ((rv == -1) ? -errno : rv);
return count;
}

static
inline
int
_read_direct_io(const int fd,
void *buf,
const size_t count,
const off_t offset)
{
int rv;

rv = fs::pread(fd,buf,count,offset);
if(rv == -1)
return -errno;

return rv;
}

namespace mergerfs
Expand All @@ -51,10 +73,19 @@ namespace mergerfs
{
FileInfo *fi = reinterpret_cast<FileInfo*>(ffi->fh);

return _read(fi->fd,
buf,
count,
offset);
return _read(fi->fd,buf,count,offset);
}

int
read_direct_io(const char *fusepath,
char *buf,
size_t count,
off_t offset,
fuse_file_info *ffi)
{
FileInfo *fi = reinterpret_cast<FileInfo*>(ffi->fh);

return _read_direct_io(fi->fd,buf,count,offset);
}
}
}
7 changes: 7 additions & 0 deletions src/read.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,12 @@ namespace mergerfs
size_t count,
off_t offset,
fuse_file_info *fi);

int
read_direct_io(const char *fusepath,
char *buf,
size_t count,
off_t offset,
fuse_file_info *fi);
}
}
64 changes: 58 additions & 6 deletions src/write.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
#include "rwlock.hpp"
#include "ugid.hpp"

using namespace mergerfs;

typedef int (*WriteFunc)(const int,const void*,const size_t,const off_t);

static
bool
_out_of_space(const int error)
Expand All @@ -33,6 +37,7 @@ _out_of_space(const int error)
}

static
inline
int
_write(const int fd,
const void *buf,
Expand All @@ -42,25 +47,52 @@ _write(const int fd,
int rv;

rv = fs::pwrite(fd,buf,count,offset);
if(rv == -1)
return -errno;
if(rv == 0)
return 0;

return ((rv == -1) ? -errno : rv);
return count;
}

static
inline
int
_write_direct_io(const int fd,
const void *buf,
const size_t count,
const off_t offset)
{
int rv;

rv = fs::pwrite(fd,buf,count,offset);
if(rv == -1)
return -errno;

return rv;
}




namespace mergerfs
{
namespace fuse
{
static
inline
int
write(const char *fusepath,
write(WriteFunc func,
const char *fusepath,
const char *buf,
size_t count,
off_t offset,
const size_t count,
const off_t offset,
fuse_file_info *ffi)
{
int rv;
FileInfo* fi = reinterpret_cast<FileInfo*>(ffi->fh);

rv = _write(fi->fd,buf,count,offset);
rv = func(fi->fd,buf,count,offset);
if(_out_of_space(-rv))
{
const fuse_context *fc = fuse_get_context();
Expand All @@ -75,11 +107,31 @@ namespace mergerfs
if(rv == -1)
return -ENOSPC;

rv = _write(fi->fd,buf,count,offset);
rv = func(fi->fd,buf,count,offset);
}
}

return rv;
}

int
write(const char *fusepath,
const char *buf,
size_t count,
off_t offset,
fuse_file_info *ffi)
{
return write(_write,fusepath,buf,count,offset,ffi);
}

int
write_direct_io(const char *fusepath,
const char *buf,
size_t count,
off_t offset,
fuse_file_info *ffi)
{
return write(_write_direct_io,fusepath,buf,count,offset,ffi);
}
}
}
7 changes: 7 additions & 0 deletions src/write.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,12 @@ namespace mergerfs
size_t count,
off_t offset,
fuse_file_info *fi);

int
write_direct_io(const char *fusepath,
const char *buf,
size_t count,
off_t offset,
fuse_file_info *fi);
}
}

0 comments on commit 10a9918

Please sign in to comment.