restructure readdir, add readdir_plus

Does not enable READDIR_AUTO. Might add in the future.
This commit is contained in:
Antonio SJ Musumeci 2019-06-14 08:59:04 -04:00
parent 10e8bd9fa3
commit d119807adb
29 changed files with 1081 additions and 364 deletions

View file

@ -53,6 +53,7 @@ Config::Config()
cache_readdir(false),
async_read(true),
writeback_cache(false),
readdirplus(false),
cache_files(CacheFiles::LIBFUSE),
fuse_msg_size(FUSE_MAX_MAX_PAGES),
POLICYINIT(access),

View file

@ -113,6 +113,7 @@ public:
bool cache_readdir;
bool async_read;
bool writeback_cache;
bool readdirplus;
CacheFiles cache_files;
uint16_t fuse_msg_size;

53
src/fs_base_fstatat.hpp Normal file
View file

@ -0,0 +1,53 @@
/*
ISC License
Copyright (c) 2019, 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.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
namespace fs
{
static
inline
int
fstatat(const int dirfd,
const char *pathname,
struct stat *statbuf,
const int flags)
{
return ::fstatat(dirfd,
pathname,
statbuf,
flags);
}
static
inline
int
fstatat_nofollow(const int dirfd,
const char *pathname,
struct stat *statbuf)
{
return fs::fstatat(dirfd,
pathname,
statbuf,
AT_SYMLINK_NOFOLLOW);
}
}

View file

@ -30,18 +30,26 @@ namespace fs
static const uint64_t MAGIC = 0x7472617065786974;
inline
void
recompute(struct stat *st_)
uint64_t
recompute(ino_t ino_,
dev_t dev_)
{
uint64_t buf[5];
buf[0] = st_->st_ino;
buf[1] = st_->st_dev;
buf[0] = ino_;
buf[1] = dev_;
buf[2] = buf[0] ^ buf[1];
buf[3] = buf[0] & buf[1];
buf[4] = buf[0] | buf[1];
st_->st_ino = fasthash64(&buf[0],sizeof(buf),MAGIC);
return fasthash64(&buf[0],sizeof(buf),MAGIC);
}
inline
void
recompute(struct stat *st_)
{
st_->st_ino = recompute(st_->st_ino,st_->st_dev);
}
}
}

View file

@ -95,6 +95,8 @@ namespace FUSE
l::want_if_capable(conn_,FUSE_CAP_DONT_MASK);
l::want_if_capable(conn_,FUSE_CAP_IOCTL_DIR);
l::want_if_capable(conn_,FUSE_CAP_PARALLEL_DIROPS);
l::want_if_capable(conn_,FUSE_CAP_READDIR_PLUS,&c.readdirplus);
//l::want_if_capable(conn_,FUSE_CAP_READDIR_PLUS_AUTO);
l::want_if_capable(conn_,FUSE_CAP_POSIX_ACL,&c.posix_acl);
l::want_if_capable(conn_,FUSE_CAP_WRITEBACK_CACHE,&c.writeback_cache);
l::want_if_capable_max_pages(conn_,c);

View file

@ -32,27 +32,27 @@
#include "ugid.hpp"
#include <fuse.h>
#include <fuse_dirents.h>
#include <string>
#include <vector>
#include <dirent.h>
using std::string;
using std::vector;
#define NO_OFFSET 0
namespace l
{
static
int
readdir(const Branches &branches_,
const char *dirname_,
void *buf_,
const fuse_fill_dir_t filler_)
readdir(const Branches &branches_,
const char *dirname_,
fuse_dirents_t *buf_)
{
dev_t dev;
HashSet names;
string basepath;
struct stat st = {0};
for(size_t i = 0, ei = branches_.size(); i != ei; i++)
{
@ -66,24 +66,21 @@ namespace l
if(!dh)
continue;
dirfd = fs::dirfd(dh);
st.st_dev = fs::devid(dirfd);
if(st.st_dev == (dev_t)-1)
st.st_dev = i;
dirfd = fs::dirfd(dh);
dev = fs::devid(dirfd);
if(dev == (dev_t)-1)
dev = i;
rv = 0;
for(struct dirent *de = fs::readdir(dh); de && !rv; de = fs::readdir(dh))
{
rv = names.put(de->d_name);
rv = names.put(de->d_name,_D_EXACT_NAMLEN(de));
if(rv == 0)
continue;
st.st_ino = de->d_ino;
st.st_mode = DTTOIF(de->d_type);
de->d_ino = fs::inode::recompute(de->d_ino,dev);
fs::inode::recompute(&st);
rv = filler_(buf_,de->d_name,&st,NO_OFFSET);
rv = fuse_dirents_add(buf_,de);
if(rv)
return (fs::closedir(dh),-ENOMEM);
}
@ -98,11 +95,8 @@ namespace l
namespace FUSE
{
int
readdir(const char *fusepath_,
void *buf_,
fuse_fill_dir_t filler_,
off_t offset_,
fuse_file_info *ffi_)
readdir(fuse_file_info *ffi_,
fuse_dirents_t *buf_)
{
DirInfo *di = reinterpret_cast<DirInfo*>(ffi_->fh);
const fuse_context *fc = fuse_get_context();
@ -112,7 +106,6 @@ namespace FUSE
return l::readdir(config.branches,
di->fusepath.c_str(),
buf_,
filler_);
buf_);
}
}

View file

@ -1,5 +1,5 @@
/*
Copyright (c) 2016, Antonio SJ Musumeci <trapexit@spawn.link>
Copyright (c) 2019, 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
@ -18,16 +18,9 @@
#include <fuse.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
namespace FUSE
{
int
readdir(const char *fusepath_,
void *buf_,
fuse_fill_dir_t filler_,
off_t offset_,
fuse_file_info *ffi_);
readdir(fuse_file_info *ffi_,
fuse_dirents_t *buf_);
}

138
src/fuse_readdir_plus.cpp Normal file
View file

@ -0,0 +1,138 @@
/*
Copyright (c) 2019, 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.
*/
#define _DEFAULT_SOURCE
#include "config.hpp"
#include "dirinfo.hpp"
#include "errno.hpp"
#include "fs_base_closedir.hpp"
#include "fs_base_dirfd.hpp"
#include "fs_base_opendir.hpp"
#include "fs_base_readdir.hpp"
#include "fs_base_fstatat.hpp"
#include "fs_base_stat.hpp"
#include "fs_devid.hpp"
#include "fs_inode.hpp"
#include "fs_path.hpp"
#include "hashset.hpp"
#include "rwlock.hpp"
#include "ugid.hpp"
#include <fuse.h>
#include <fuse_dirents.h>
#include <string>
#include <vector>
#include <dirent.h>
using std::string;
using std::vector;
#define NO_OFFSET 0
static
int
dot_or_dotdot(const char *s_)
{
return ((s_[0] == '.') &&
((s_[1] == '\0') ||
((s_[1] == '.') && (s_[2] == '\0'))));
}
namespace l
{
static
int
readdir_plus(const Branches &branches_,
const char *dirname_,
fuse_dirents_t *buf_)
{
dev_t dev;
HashSet names;
string basepath;
struct stat st;
fuse_entry_t entry;
entry.nodeid = 0;
entry.generation = 0;
entry.entry_valid = 1;
entry.attr_valid = 1;
entry.entry_valid_nsec = 0;
entry.attr_valid_nsec = 0;
for(size_t i = 0, ei = branches_.size(); i != ei; i++)
{
int rv;
int dirfd;
DIR *dh;
basepath = fs::path::make(&branches_[i].path,dirname_);
dh = fs::opendir(basepath);
if(!dh)
continue;
dirfd = fs::dirfd(dh);
dev = fs::devid(dirfd);
if(dev == (dev_t)-1)
dev = i;
rv = 0;
for(struct dirent *de = fs::readdir(dh); de && !rv; de = fs::readdir(dh))
{
rv = names.put(de->d_name,_D_EXACT_NAMLEN(de));
if(rv == 0)
continue;
rv = fs::fstatat_nofollow(dirfd,de->d_name,&st);
if(rv == -1)
memset(&st,0,sizeof(st));
de->d_ino = fs::inode::recompute(de->d_ino,dev);
st.st_ino = fs::inode::recompute(st.st_ino,dev);
rv = fuse_dirents_add_plus(buf_,de,&entry,&st);
if(rv)
return (fs::closedir(dh),-ENOMEM);
}
fs::closedir(dh);
}
return 0;
}
}
namespace FUSE
{
int
readdir_plus(fuse_file_info *ffi_,
fuse_dirents_t *buf_)
{
DirInfo *di = reinterpret_cast<DirInfo*>(ffi_->fh);
const fuse_context *fc = fuse_get_context();
const Config &config = Config::get(fc);
const ugid::Set ugid(fc->uid,fc->gid);
const rwlock::ReadGuard readlock(&config.branches_lock);
return l::readdir_plus(config.branches,
di->fusepath.c_str(),
buf_);
}
}

30
src/fuse_readdir_plus.hpp Normal file
View file

@ -0,0 +1,30 @@
/*
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.
*/
#pragma once
#include <fuse.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
namespace FUSE
{
int
readdir_plus(fuse_file_info *ffi_,
fuse_dirents_t *buf_);
}

View file

@ -38,13 +38,14 @@ public:
inline
int
put(const char *str_)
put(const char *str_,
const uint64_t len_)
{
int rv;
uint64_t h;
khint_t key;
h = fasthash64(str_,strlen(str_),0x7472617065786974);
h = fasthash64(str_,len_,0x7472617065786974);
key = kh_put(hashset,_set,h,&rv);
if(rv == 0)

View file

@ -50,6 +50,7 @@
#include "fuse_read.hpp"
#include "fuse_read_buf.hpp"
#include "fuse_readdir.hpp"
#include "fuse_readdir_plus.hpp"
#include "fuse_readlink.hpp"
#include "fuse_release.hpp"
#include "fuse_releasedir.hpp"
@ -102,7 +103,6 @@ namespace l
ops.ftruncate = FUSE::ftruncate;
ops.futimens = FUSE::futimens;
ops.getattr = FUSE::getattr;
ops.getdir = NULL; /* deprecated; use readdir */
ops.getxattr = FUSE::getxattr;
ops.init = FUSE::init;
ops.ioctl = FUSE::ioctl;
@ -118,6 +118,7 @@ namespace l
ops.read = (nullrw ? FUSE::read_null : FUSE::read);
ops.read_buf = (nullrw ? NULL : FUSE::read_buf);
ops.readdir = FUSE::readdir;
ops.readdir_plus = FUSE::readdir_plus;
ops.readlink = FUSE::readlink;
ops.release = FUSE::release;
ops.releasedir = FUSE::releasedir;

View file

@ -310,6 +310,8 @@ parse_and_process_arg(Config &config,
return 0;
else if(arg == "big_writes")
return 0;
else if(arg == "readdir_ino")
return 0;
return 1;
}
@ -373,6 +375,8 @@ parse_and_process_kv_arg(Config &config,
rv = parse_and_process(value,config.auto_cache);
else if(key == "async_read")
rv = parse_and_process(value,config.async_read);
else if(key == "readdirplus")
rv = parse_and_process(value,config.readdirplus);
else if(key == "max_write")
rv = 0;
else if(key == "fuse_msg_size")