Fix read/write behavior and return value depending on direct_io

Also add parallel direct write option for 6.2+ kernels.
This commit is contained in:
Antonio SJ Musumeci 2023-06-18 00:31:59 -05:00
parent 82430e1079
commit 6a14a10e6c
19 changed files with 531 additions and 158 deletions

View file

@ -17,8 +17,11 @@
#include "config.hpp"
#include "errno.hpp"
#include "fileinfo.hpp"
#include "fs_close.hpp"
#include "fs_dup2.hpp"
#include "fs_movefile.hpp"
#include "fs_write.hpp"
#include "fs_pwrite.hpp"
#include "fs_pwriten.hpp"
#include "ugid.hpp"
#include "fuse.h"
@ -36,37 +39,22 @@ namespace l
{
static
bool
out_of_space(const int error_)
out_of_space(const ssize_t error_)
{
return ((error_ == ENOSPC) ||
(error_ == EDQUOT));
return ((error_ == -ENOSPC) ||
(error_ == -EDQUOT));
}
static
int
write(const int fd_,
const void *buf_,
const size_t count_,
const off_t offset_)
move_and_pwrite(const char *buf_,
const size_t count_,
const off_t offset_,
FileInfo *fi_,
int err_)
{
int rv;
rv = fs::pwrite(fd_,buf_,count_,offset_);
if(rv == -1)
return -errno;
return rv;
}
static
int
move_and_write(const char *buf_,
const size_t count_,
const off_t offset_,
FileInfo *fi_,
int err_)
{
int rv;
int err;
ssize_t rv;
Config::Read cfg;
if(cfg->moveonenospc.enabled == false)
@ -79,9 +67,101 @@ namespace l
if(rv < 0)
return err_;
fi_->fd = rv;
err = fs::dup2(rv,fi_->fd);
fs::close(rv);
if(err < 0)
return err;
return l::write(fi_->fd,buf_,count_,offset_);
return fs::pwrite(fi_->fd,buf_,count_,offset_);
}
static
int
move_and_pwriten(char const *buf_,
size_t const count_,
off_t const offset_,
FileInfo *fi_,
ssize_t const err_,
ssize_t const written_)
{
int err;
ssize_t rv;
Config::Read cfg;
if(cfg->moveonenospc.enabled == false)
return err_;
rv = fs::movefile_as_root(cfg->moveonenospc.policy,
cfg->branches,
fi_->fusepath,
fi_->fd);
if(rv < 0)
return err_;
err = fs::dup2(rv,fi_->fd);
fs::close(rv);
if(err < 0)
return err;
rv = fs::pwriten(fi_->fd,
buf_ + written_,
count_ - written_,
offset_ + written_,
&err);
if(err < 0)
return err;
return (rv + written_);
}
// When in direct_io mode write's return value should match that of
// the operation.
// 0 on EOF
// N bytes written (short writes included)
// -errno on error
// See libfuse/include/fuse.h for more details
static
int
write_direct_io(const char *buf_,
const size_t count_,
const off_t offset_,
FileInfo *fi_)
{
ssize_t rv;
rv = fs::pwrite(fi_->fd,buf_,count_,offset_);
if(l::out_of_space(rv))
rv = l::move_and_pwrite(buf_,count_,offset_,fi_,rv);
return rv;
}
// When not in direct_io mode write's return value is more complex.
// 0 or less than `count` on EOF
// `count` on non-errors
// -errno on error
// See libfuse/include/fuse.h for more details
static
int
write_cached(const char *buf_,
const size_t count_,
const off_t offset_,
FileInfo *fi_)
{
int err;
ssize_t rv;
rv = fs::pwriten(fi_->fd,buf_,count_,offset_,&err);
if(rv == (ssize_t)count_)
return count_;
if(rv == 0)
return 0;
if(err && !l::out_of_space(err))
return err;
rv = l::move_and_pwriten(buf_,count_,offset_,fi_,err,rv);
return rv;
}
static
@ -91,16 +171,25 @@ namespace l
const size_t count_,
const off_t offset_)
{
int rv;
FileInfo* fi;
FileInfo *fi;
fi = reinterpret_cast<FileInfo*>(ffi_->fh);
rv = l::write(fi->fd,buf_,count_,offset_);
if(l::out_of_space(-rv))
rv = l::move_and_write(buf_,count_,offset_,fi,rv);
// Concurrent writes can only happen if:
// 1) writeback-cache is enabled and using page caching
// 2) parallel_direct_writes is enabled and file has `direct_io=true`
// Will look into selectively locking in the future
// A reader/writer lock would probably be the best option given
// the expense of the write itself in comparison. Alternatively,
// could change the move file behavior to use a known target file
// and have threads use O_EXCL and back off and wait for the
// transfer to complete before retrying.
std::lock_guard<std::mutex> guard(fi->mutex);
return rv;
if(fi->direct_io)
return l::write_direct_io(buf_,count_,offset_,fi);
return l::write_cached(buf_,count_,offset_,fi);
}
}