2014-05-13 00:41:46 +08:00
|
|
|
/*
|
2016-01-15 05:50:22 +08:00
|
|
|
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.
|
|
|
|
*/
|
2014-05-13 00:41:46 +08:00
|
|
|
|
2024-01-24 11:18:09 +08:00
|
|
|
#include "toml.hpp"
|
|
|
|
|
2023-03-01 12:10:36 +08:00
|
|
|
#include "fs_readahead.hpp"
|
2022-12-31 07:03:13 +08:00
|
|
|
#include "fs_wait_for_mount.hpp"
|
|
|
|
#include "syslog.hpp"
|
|
|
|
|
2019-01-25 20:03:46 +08:00
|
|
|
#include "fs_path.hpp"
|
2023-02-02 12:05:45 +08:00
|
|
|
#include "fs_umount2.hpp"
|
2019-01-25 20:03:46 +08:00
|
|
|
#include "mergerfs.hpp"
|
|
|
|
#include "option_parser.hpp"
|
2023-02-12 01:11:45 +08:00
|
|
|
#include "procfs_get_name.hpp"
|
2019-01-25 20:03:46 +08:00
|
|
|
#include "resources.hpp"
|
2020-12-20 04:06:08 +08:00
|
|
|
#include "strvec.hpp"
|
2023-08-14 11:55:04 +08:00
|
|
|
#include "gidcache.hpp"
|
2019-01-25 20:03:46 +08:00
|
|
|
|
|
|
|
#include "fuse_access.hpp"
|
2021-08-25 09:24:51 +08:00
|
|
|
#include "fuse_bmap.hpp"
|
2019-01-25 20:03:46 +08:00
|
|
|
#include "fuse_chmod.hpp"
|
|
|
|
#include "fuse_chown.hpp"
|
2019-05-26 02:25:57 +08:00
|
|
|
#include "fuse_copy_file_range.hpp"
|
2019-01-25 20:03:46 +08:00
|
|
|
#include "fuse_create.hpp"
|
|
|
|
#include "fuse_destroy.hpp"
|
|
|
|
#include "fuse_fallocate.hpp"
|
2019-03-04 12:26:20 +08:00
|
|
|
#include "fuse_fchmod.hpp"
|
|
|
|
#include "fuse_fchown.hpp"
|
2019-01-25 20:03:46 +08:00
|
|
|
#include "fuse_fgetattr.hpp"
|
|
|
|
#include "fuse_flock.hpp"
|
|
|
|
#include "fuse_flush.hpp"
|
2019-03-04 12:26:20 +08:00
|
|
|
#include "fuse_free_hide.hpp"
|
2019-01-25 20:03:46 +08:00
|
|
|
#include "fuse_fsync.hpp"
|
|
|
|
#include "fuse_fsyncdir.hpp"
|
|
|
|
#include "fuse_ftruncate.hpp"
|
2019-03-04 12:26:20 +08:00
|
|
|
#include "fuse_futimens.hpp"
|
2019-01-25 20:03:46 +08:00
|
|
|
#include "fuse_getattr.hpp"
|
|
|
|
#include "fuse_getxattr.hpp"
|
|
|
|
#include "fuse_init.hpp"
|
|
|
|
#include "fuse_ioctl.hpp"
|
|
|
|
#include "fuse_link.hpp"
|
|
|
|
#include "fuse_listxattr.hpp"
|
2021-08-25 09:24:51 +08:00
|
|
|
#include "fuse_lock.hpp"
|
2019-01-25 20:03:46 +08:00
|
|
|
#include "fuse_mkdir.hpp"
|
|
|
|
#include "fuse_mknod.hpp"
|
|
|
|
#include "fuse_open.hpp"
|
|
|
|
#include "fuse_opendir.hpp"
|
2021-08-25 09:24:51 +08:00
|
|
|
#include "fuse_poll.hpp"
|
2019-03-04 12:26:20 +08:00
|
|
|
#include "fuse_prepare_hide.hpp"
|
2023-02-27 04:16:18 +08:00
|
|
|
#include "fuse_read.hpp"
|
2019-01-25 20:03:46 +08:00
|
|
|
#include "fuse_readdir.hpp"
|
2019-06-14 20:59:04 +08:00
|
|
|
#include "fuse_readdir_plus.hpp"
|
2019-01-25 20:03:46 +08:00
|
|
|
#include "fuse_readlink.hpp"
|
|
|
|
#include "fuse_release.hpp"
|
|
|
|
#include "fuse_releasedir.hpp"
|
2023-08-12 14:50:37 +08:00
|
|
|
#include "fuse_removemapping.hpp"
|
2019-01-25 20:03:46 +08:00
|
|
|
#include "fuse_removexattr.hpp"
|
|
|
|
#include "fuse_rename.hpp"
|
|
|
|
#include "fuse_rmdir.hpp"
|
2023-08-12 14:50:37 +08:00
|
|
|
#include "fuse_setupmapping.hpp"
|
2019-01-25 20:03:46 +08:00
|
|
|
#include "fuse_setxattr.hpp"
|
|
|
|
#include "fuse_statfs.hpp"
|
|
|
|
#include "fuse_symlink.hpp"
|
2023-08-12 14:50:37 +08:00
|
|
|
#include "fuse_syncfs.hpp"
|
|
|
|
#include "fuse_tmpfile.hpp"
|
2019-01-25 20:03:46 +08:00
|
|
|
#include "fuse_truncate.hpp"
|
|
|
|
#include "fuse_unlink.hpp"
|
|
|
|
#include "fuse_utimens.hpp"
|
2022-11-13 11:26:26 +08:00
|
|
|
#include "fuse_write.hpp"
|
2019-01-25 20:03:46 +08:00
|
|
|
|
2020-12-20 04:06:08 +08:00
|
|
|
#include "fuse.h"
|
2014-05-13 00:41:46 +08:00
|
|
|
|
2023-04-24 11:33:48 +08:00
|
|
|
#include <csignal>
|
2014-05-13 00:41:46 +08:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <iostream>
|
|
|
|
|
2019-01-25 20:03:46 +08:00
|
|
|
#include <string.h>
|
2015-01-28 07:25:20 +08:00
|
|
|
|
2020-12-20 04:06:08 +08:00
|
|
|
|
2019-01-25 20:03:46 +08:00
|
|
|
namespace l
|
2014-05-13 00:41:46 +08:00
|
|
|
{
|
2015-01-28 05:08:13 +08:00
|
|
|
static
|
|
|
|
void
|
2020-08-17 06:22:25 +08:00
|
|
|
get_fuse_operations(struct fuse_operations &ops_,
|
|
|
|
const bool nullrw_)
|
2015-01-28 05:08:13 +08:00
|
|
|
{
|
2020-08-17 06:22:25 +08:00
|
|
|
ops_.access = FUSE::access;
|
2021-08-25 09:24:51 +08:00
|
|
|
ops_.bmap = FUSE::bmap;
|
2020-08-17 06:22:25 +08:00
|
|
|
ops_.chmod = FUSE::chmod;
|
|
|
|
ops_.chown = FUSE::chown;
|
|
|
|
ops_.copy_file_range = FUSE::copy_file_range;
|
|
|
|
ops_.create = FUSE::create;
|
|
|
|
ops_.destroy = FUSE::destroy;
|
|
|
|
ops_.fallocate = FUSE::fallocate;
|
|
|
|
ops_.fchmod = FUSE::fchmod;
|
|
|
|
ops_.fchown = FUSE::fchown;
|
|
|
|
ops_.fgetattr = FUSE::fgetattr;
|
2021-08-25 09:24:51 +08:00
|
|
|
ops_.flock = FUSE::flock;
|
2020-08-17 06:22:25 +08:00
|
|
|
ops_.flush = FUSE::flush;
|
|
|
|
ops_.free_hide = FUSE::free_hide;
|
|
|
|
ops_.fsync = FUSE::fsync;
|
|
|
|
ops_.fsyncdir = FUSE::fsyncdir;
|
|
|
|
ops_.ftruncate = FUSE::ftruncate;
|
|
|
|
ops_.futimens = FUSE::futimens;
|
|
|
|
ops_.getattr = FUSE::getattr;
|
|
|
|
ops_.getxattr = FUSE::getxattr;
|
|
|
|
ops_.init = FUSE::init;
|
|
|
|
ops_.ioctl = FUSE::ioctl;
|
|
|
|
ops_.link = FUSE::link;
|
|
|
|
ops_.listxattr = FUSE::listxattr;
|
2021-08-25 09:24:51 +08:00
|
|
|
ops_.lock = FUSE::lock;
|
2020-08-17 06:22:25 +08:00
|
|
|
ops_.mkdir = FUSE::mkdir;
|
|
|
|
ops_.mknod = FUSE::mknod;
|
|
|
|
ops_.open = FUSE::open;
|
|
|
|
ops_.opendir = FUSE::opendir;
|
2021-08-25 09:24:51 +08:00
|
|
|
ops_.poll = FUSE::poll;;
|
2020-08-17 06:22:25 +08:00
|
|
|
ops_.prepare_hide = FUSE::prepare_hide;
|
2023-02-27 04:16:18 +08:00
|
|
|
ops_.read = (nullrw_ ? FUSE::read_null : FUSE::read);
|
2020-08-17 06:22:25 +08:00
|
|
|
ops_.readdir = FUSE::readdir;
|
|
|
|
ops_.readdir_plus = FUSE::readdir_plus;
|
|
|
|
ops_.readlink = FUSE::readlink;
|
|
|
|
ops_.release = FUSE::release;
|
|
|
|
ops_.releasedir = FUSE::releasedir;
|
2023-08-12 14:50:37 +08:00
|
|
|
ops_.removemapping = FUSE::removemapping;
|
2020-08-17 06:22:25 +08:00
|
|
|
ops_.removexattr = FUSE::removexattr;
|
|
|
|
ops_.rename = FUSE::rename;
|
|
|
|
ops_.rmdir = FUSE::rmdir;
|
2023-08-12 14:50:37 +08:00
|
|
|
ops_.setupmapping = FUSE::setupmapping;
|
2020-08-17 06:22:25 +08:00
|
|
|
ops_.setxattr = FUSE::setxattr;
|
|
|
|
ops_.statfs = FUSE::statfs;
|
|
|
|
ops_.symlink = FUSE::symlink;
|
2023-08-12 14:50:37 +08:00
|
|
|
ops_.syncfs = FUSE::syncfs;
|
|
|
|
ops_.tmpfile = FUSE::tmpfile;
|
2020-08-17 06:22:25 +08:00
|
|
|
ops_.truncate = FUSE::truncate;
|
|
|
|
ops_.unlink = FUSE::unlink;
|
|
|
|
ops_.utimens = FUSE::utimens;
|
2022-11-13 11:26:26 +08:00
|
|
|
ops_.write = (nullrw_ ? FUSE::write_null : FUSE::write);
|
2014-05-13 00:41:46 +08:00
|
|
|
|
2015-01-28 05:08:13 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
void
|
2023-01-26 12:38:31 +08:00
|
|
|
setup_resources(const int scheduling_priority_)
|
2015-01-28 05:08:13 +08:00
|
|
|
{
|
|
|
|
std::srand(time(NULL));
|
2019-01-25 20:03:46 +08:00
|
|
|
resources::reset_umask();
|
|
|
|
resources::maxout_rlimit_nofile();
|
|
|
|
resources::maxout_rlimit_fsize();
|
2023-01-26 12:38:31 +08:00
|
|
|
resources::setpriority(scheduling_priority_);
|
2015-01-28 05:08:13 +08:00
|
|
|
}
|
2014-05-13 00:41:46 +08:00
|
|
|
|
2022-12-31 07:03:13 +08:00
|
|
|
static
|
|
|
|
void
|
|
|
|
wait_for_mount(const Config::Read &cfg_)
|
|
|
|
{
|
|
|
|
fs::PathVector paths;
|
|
|
|
std::chrono::milliseconds timeout;
|
|
|
|
|
|
|
|
paths = cfg_->branches->to_paths();
|
|
|
|
|
|
|
|
syslog_info("Waiting %u seconds for branches to mount",
|
|
|
|
(uint64_t)cfg_->branches_mount_timeout);
|
|
|
|
|
|
|
|
timeout = std::chrono::milliseconds(cfg_->branches_mount_timeout * 1000);
|
2023-02-02 12:05:45 +08:00
|
|
|
fs::wait_for_mount((std::string)cfg_->mountpoint,
|
2022-12-31 07:03:13 +08:00
|
|
|
paths,
|
2023-03-27 12:07:17 +08:00
|
|
|
timeout);
|
2022-12-31 07:03:13 +08:00
|
|
|
}
|
|
|
|
|
2023-02-02 12:05:45 +08:00
|
|
|
static
|
|
|
|
void
|
|
|
|
lazy_umount(const std::string target_)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
rv = fs::umount_lazy(target_);
|
|
|
|
switch(rv)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
syslog_notice("%s has been successfully lazily unmounted",
|
|
|
|
target_.c_str());
|
|
|
|
break;
|
|
|
|
case -EINVAL:
|
|
|
|
syslog_notice("%s was not a mount point needing to be unmounted",
|
|
|
|
target_.c_str());
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
syslog_error("Error unmounting %s: %d - %s",
|
|
|
|
target_.c_str(),
|
|
|
|
-rv,
|
|
|
|
strerror(-rv));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-24 11:33:48 +08:00
|
|
|
static
|
|
|
|
void
|
|
|
|
usr1_signal_handler(int signal_)
|
|
|
|
{
|
|
|
|
syslog_info("Received SIGUSR1 - invalidating all nodes");
|
|
|
|
fuse_invalidate_all_nodes();
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
void
|
|
|
|
usr2_signal_handler(int signal_)
|
|
|
|
{
|
|
|
|
syslog_info("Received SIGUSR2 - triggering thorough gc");
|
|
|
|
fuse_gc();
|
2023-08-14 11:55:04 +08:00
|
|
|
GIDCache::invalidate_all_caches();
|
2023-04-24 11:33:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
void
|
|
|
|
setup_signal_handlers()
|
|
|
|
{
|
|
|
|
std::signal(SIGUSR1,l::usr1_signal_handler);
|
|
|
|
std::signal(SIGUSR2,l::usr2_signal_handler);
|
|
|
|
}
|
|
|
|
|
2023-10-22 13:43:19 +08:00
|
|
|
static
|
|
|
|
void
|
|
|
|
warn_if_not_root()
|
|
|
|
{
|
|
|
|
uid_t uid;
|
|
|
|
|
|
|
|
uid = geteuid();
|
|
|
|
if(uid == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
char const *s = "mergerfs is not running as root and may not work correctly\n";
|
|
|
|
fprintf(stderr,"warning: %s",s);
|
|
|
|
syslog_warning(s);
|
|
|
|
}
|
|
|
|
|
2014-05-13 00:41:46 +08:00
|
|
|
int
|
2019-01-25 20:03:46 +08:00
|
|
|
main(const int argc_,
|
|
|
|
char **argv_)
|
2014-05-13 00:41:46 +08:00
|
|
|
{
|
2023-10-29 14:44:18 +08:00
|
|
|
int rv;
|
2020-12-20 04:06:08 +08:00
|
|
|
Config::Read cfg;
|
|
|
|
Config::ErrVec errs;
|
|
|
|
fuse_args args;
|
|
|
|
fuse_operations ops;
|
2019-09-24 22:27:46 +08:00
|
|
|
|
2022-12-31 07:03:13 +08:00
|
|
|
syslog_open();
|
|
|
|
|
2023-10-22 13:43:19 +08:00
|
|
|
l::warn_if_not_root();
|
|
|
|
|
2019-09-24 22:27:46 +08:00
|
|
|
memset(&ops,0,sizeof(fuse_operations));
|
2014-05-13 00:41:46 +08:00
|
|
|
|
2019-01-25 20:03:46 +08:00
|
|
|
args.argc = argc_;
|
|
|
|
args.argv = argv_;
|
2015-01-28 05:08:13 +08:00
|
|
|
args.allocated = 0;
|
2014-05-13 00:41:46 +08:00
|
|
|
|
2020-12-20 04:06:08 +08:00
|
|
|
options::parse(&args,&errs);
|
2019-09-24 22:27:46 +08:00
|
|
|
if(errs.size())
|
2022-07-10 04:06:18 +08:00
|
|
|
{
|
|
|
|
std::cerr << errs << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
2015-01-28 05:08:13 +08:00
|
|
|
|
2022-12-31 07:03:13 +08:00
|
|
|
if(cfg->branches_mount_timeout > 0)
|
|
|
|
l::wait_for_mount(cfg);
|
|
|
|
|
2023-01-26 12:38:31 +08:00
|
|
|
l::setup_resources(cfg->scheduling_priority);
|
2023-04-24 11:33:48 +08:00
|
|
|
l::setup_signal_handlers();
|
2020-12-20 04:06:08 +08:00
|
|
|
l::get_fuse_operations(ops,cfg->nullrw);
|
2014-05-13 00:41:46 +08:00
|
|
|
|
2023-02-02 12:05:45 +08:00
|
|
|
if(cfg->lazy_umount_mountpoint)
|
|
|
|
l::lazy_umount(cfg->mountpoint);
|
|
|
|
|
2023-02-12 01:11:45 +08:00
|
|
|
procfs::init();
|
|
|
|
|
2023-10-29 14:44:18 +08:00
|
|
|
rv = fuse_main(args.argc,
|
|
|
|
args.argv,
|
|
|
|
&ops);
|
|
|
|
|
|
|
|
syslog_info("exiting main loop with return code %d",rv);
|
|
|
|
|
|
|
|
return rv;
|
2014-05-13 00:41:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2020-08-17 06:22:25 +08:00
|
|
|
main(int argc_,
|
|
|
|
char **argv_)
|
2014-05-13 00:41:46 +08:00
|
|
|
{
|
2024-01-24 11:19:38 +08:00
|
|
|
auto data = toml::parse("config.toml");
|
|
|
|
|
2024-01-26 12:03:41 +08:00
|
|
|
// std::cout << data << '\n';
|
2024-01-26 12:10:14 +08:00
|
|
|
Branches2 b(data["branches"]);
|
2024-01-24 11:18:25 +08:00
|
|
|
|
2024-01-24 11:19:38 +08:00
|
|
|
|
|
|
|
return 0;
|
2024-01-24 11:18:25 +08:00
|
|
|
|
2020-08-17 06:22:25 +08:00
|
|
|
return l::main(argc_,argv_);
|
2014-05-13 00:41:46 +08:00
|
|
|
}
|