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
|
|
|
*/
|
|
|
|
|
|
|
|
#include <fuse.h>
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <string.h>
|
2014-06-01 05:54:18 +08:00
|
|
|
#include <stdio.h>
|
2015-06-17 10:12:55 +08:00
|
|
|
#include <stdlib.h>
|
2015-09-28 18:39:16 +08:00
|
|
|
#include <unistd.h>
|
2014-05-13 00:41:46 +08:00
|
|
|
|
|
|
|
#include <string>
|
2014-06-01 05:54:18 +08:00
|
|
|
#include <vector>
|
2014-05-13 00:41:46 +08:00
|
|
|
#include <sstream>
|
2014-06-01 05:54:18 +08:00
|
|
|
#include <iostream>
|
2015-09-28 18:39:16 +08:00
|
|
|
#include <iomanip>
|
2014-05-13 00:41:46 +08:00
|
|
|
|
|
|
|
#include "config.hpp"
|
2018-10-08 10:28:58 +08:00
|
|
|
#include "errno.hpp"
|
2018-09-12 04:45:17 +08:00
|
|
|
#include "fs_glob.hpp"
|
2015-09-28 18:39:16 +08:00
|
|
|
#include "num.hpp"
|
2014-05-13 00:41:46 +08:00
|
|
|
#include "policy.hpp"
|
2015-09-28 18:39:16 +08:00
|
|
|
#include "str.hpp"
|
|
|
|
#include "version.hpp"
|
2014-05-13 00:41:46 +08:00
|
|
|
|
2014-06-01 05:54:18 +08:00
|
|
|
using std::string;
|
|
|
|
using std::vector;
|
2014-05-13 00:41:46 +08:00
|
|
|
using namespace mergerfs;
|
|
|
|
|
2015-09-28 18:39:16 +08:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
MERGERFS_OPT_HELP,
|
|
|
|
MERGERFS_OPT_VERSION
|
|
|
|
};
|
|
|
|
|
2015-03-12 06:14:05 +08:00
|
|
|
static
|
|
|
|
void
|
2015-07-22 02:19:17 +08:00
|
|
|
set_option(fuse_args &args,
|
2015-03-12 06:14:05 +08:00
|
|
|
const std::string &option_)
|
|
|
|
{
|
|
|
|
string option;
|
|
|
|
|
|
|
|
option = "-o" + option_;
|
|
|
|
|
|
|
|
fuse_opt_insert_arg(&args,1,option.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
void
|
2015-07-22 02:19:17 +08:00
|
|
|
set_kv_option(fuse_args &args,
|
2015-03-12 06:14:05 +08:00
|
|
|
const std::string &key,
|
|
|
|
const std::string &value)
|
|
|
|
{
|
|
|
|
std::string option;
|
|
|
|
|
|
|
|
option = key + '=' + value;
|
|
|
|
|
|
|
|
set_option(args,option);
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
void
|
2018-10-21 11:40:02 +08:00
|
|
|
set_fsname(fuse_args &args,
|
|
|
|
const Branches &branches_)
|
2015-03-12 06:14:05 +08:00
|
|
|
{
|
2018-10-21 11:40:02 +08:00
|
|
|
vector<string> branches;
|
|
|
|
|
|
|
|
branches_.to_paths(branches);
|
|
|
|
|
|
|
|
if(branches.size() > 0)
|
2015-03-12 06:14:05 +08:00
|
|
|
{
|
|
|
|
std::string fsname;
|
|
|
|
|
2018-10-21 11:40:02 +08:00
|
|
|
fsname = str::remove_common_prefix_and_join(branches,':');
|
2015-03-12 06:14:05 +08:00
|
|
|
|
|
|
|
set_kv_option(args,"fsname",fsname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
void
|
2015-07-22 02:19:17 +08:00
|
|
|
set_subtype(fuse_args &args)
|
2015-03-12 06:14:05 +08:00
|
|
|
{
|
|
|
|
set_kv_option(args,"subtype","mergerfs");
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
void
|
2015-07-22 02:19:17 +08:00
|
|
|
set_default_options(fuse_args &args)
|
2015-03-12 06:14:05 +08:00
|
|
|
{
|
2015-09-15 10:11:52 +08:00
|
|
|
set_option(args,"atomic_o_trunc");
|
2015-03-12 06:14:05 +08:00
|
|
|
set_option(args,"auto_cache");
|
2015-09-15 10:11:52 +08:00
|
|
|
set_option(args,"big_writes");
|
|
|
|
set_option(args,"default_permissions");
|
|
|
|
set_option(args,"splice_move");
|
|
|
|
set_option(args,"splice_read");
|
|
|
|
set_option(args,"splice_write");
|
2015-03-12 06:14:05 +08:00
|
|
|
}
|
|
|
|
|
2015-06-17 10:12:55 +08:00
|
|
|
static
|
|
|
|
int
|
2017-04-24 18:57:29 +08:00
|
|
|
parse_and_process(const std::string &value,
|
|
|
|
uint64_t &minfreespace)
|
2015-06-17 10:12:55 +08:00
|
|
|
{
|
2015-07-03 23:56:01 +08:00
|
|
|
int rv;
|
2015-06-17 10:12:55 +08:00
|
|
|
|
2016-07-11 00:44:29 +08:00
|
|
|
rv = num::to_uint64_t(value,minfreespace);
|
2015-07-03 23:56:01 +08:00
|
|
|
if(rv == -1)
|
|
|
|
return 1;
|
2015-06-17 10:12:55 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-09-16 06:49:18 +08:00
|
|
|
static
|
|
|
|
int
|
2017-04-24 18:57:29 +08:00
|
|
|
parse_and_process(const std::string &value,
|
|
|
|
time_t &time)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
rv = num::to_time_t(value,time);
|
|
|
|
if(rv == -1)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
int
|
|
|
|
parse_and_process(const std::string &value,
|
|
|
|
bool &boolean)
|
2015-09-16 06:49:18 +08:00
|
|
|
{
|
|
|
|
if(value == "false")
|
2017-02-19 02:45:43 +08:00
|
|
|
boolean = false;
|
2015-09-16 06:49:18 +08:00
|
|
|
else if(value == "true")
|
2017-02-19 02:45:43 +08:00
|
|
|
boolean = true;
|
2015-09-16 06:49:18 +08:00
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-10-08 10:28:58 +08:00
|
|
|
static
|
|
|
|
int
|
|
|
|
parse_and_process_errno(const std::string &value_,
|
|
|
|
int &errno_)
|
|
|
|
{
|
|
|
|
if(value_ == "passthrough")
|
|
|
|
errno_ = 0;
|
|
|
|
else if(value_ == "notsup")
|
|
|
|
errno_ = ENOTSUP;
|
|
|
|
else if(value_ == "noattr")
|
|
|
|
errno_ = ENOATTR;
|
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-03-12 06:14:05 +08:00
|
|
|
static
|
|
|
|
int
|
2015-07-22 02:19:17 +08:00
|
|
|
parse_and_process_arg(Config &config,
|
2015-03-12 06:14:05 +08:00
|
|
|
const std::string &arg,
|
2015-07-22 02:19:17 +08:00
|
|
|
fuse_args *outargs)
|
2015-03-12 06:14:05 +08:00
|
|
|
{
|
|
|
|
if(arg == "defaults")
|
2016-12-21 06:06:34 +08:00
|
|
|
return (set_default_options(*outargs),0);
|
|
|
|
else if(arg == "direct_io")
|
|
|
|
return (config.direct_io=true,1);
|
2015-03-12 06:14:05 +08:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-03-11 06:42:59 +08:00
|
|
|
static
|
|
|
|
int
|
2015-07-22 02:19:17 +08:00
|
|
|
parse_and_process_kv_arg(Config &config,
|
2015-03-11 06:42:59 +08:00
|
|
|
const std::string &key,
|
|
|
|
const std::string &value)
|
|
|
|
{
|
2015-06-17 10:12:55 +08:00
|
|
|
int rv = -1;
|
2015-03-11 06:42:59 +08:00
|
|
|
std::vector<std::string> keypart;
|
|
|
|
|
|
|
|
str::split(keypart,key,'.');
|
2015-06-17 10:12:55 +08:00
|
|
|
if(keypart.size() == 2)
|
|
|
|
{
|
|
|
|
if(keypart[0] == "func")
|
|
|
|
rv = config.set_func_policy(keypart[1],value);
|
|
|
|
else if(keypart[0] == "category")
|
|
|
|
rv = config.set_category_policy(keypart[1],value);
|
|
|
|
}
|
2015-03-12 06:14:05 +08:00
|
|
|
else
|
2015-06-17 10:12:55 +08:00
|
|
|
{
|
|
|
|
if(key == "minfreespace")
|
2017-04-24 18:57:29 +08:00
|
|
|
rv = parse_and_process(value,config.minfreespace);
|
2015-09-16 06:49:18 +08:00
|
|
|
else if(key == "moveonenospc")
|
2017-04-24 18:57:29 +08:00
|
|
|
rv = parse_and_process(value,config.moveonenospc);
|
2017-02-19 02:45:43 +08:00
|
|
|
else if(key == "dropcacheonclose")
|
2017-04-24 18:57:29 +08:00
|
|
|
rv = parse_and_process(value,config.dropcacheonclose);
|
|
|
|
else if(key == "symlinkify")
|
|
|
|
rv = parse_and_process(value,config.symlinkify);
|
|
|
|
else if(key == "symlinkify_timeout")
|
|
|
|
rv = parse_and_process(value,config.symlinkify_timeout);
|
2017-05-27 04:51:30 +08:00
|
|
|
else if(key == "nullrw")
|
|
|
|
rv = parse_and_process(value,config.nullrw);
|
2017-06-30 11:57:15 +08:00
|
|
|
else if(key == "ignorepponrename")
|
|
|
|
rv = parse_and_process(value,config.ignorepponrename);
|
2018-10-01 07:47:44 +08:00
|
|
|
else if(key == "security_capability")
|
|
|
|
rv = parse_and_process(value,config.security_capability);
|
2018-10-03 02:22:37 +08:00
|
|
|
else if(key == "link_cow")
|
|
|
|
rv = parse_and_process(value,config.link_cow);
|
2018-10-08 10:28:58 +08:00
|
|
|
else if(key == "xattr")
|
|
|
|
rv = parse_and_process_errno(value,config.xattr);
|
2015-06-17 10:12:55 +08:00
|
|
|
}
|
2015-03-11 06:42:59 +08:00
|
|
|
|
|
|
|
if(rv == -1)
|
|
|
|
rv = 1;
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2014-05-13 00:41:46 +08:00
|
|
|
static
|
|
|
|
int
|
2015-07-22 02:19:17 +08:00
|
|
|
process_opt(Config &config,
|
2015-03-12 06:14:05 +08:00
|
|
|
const std::string &arg,
|
2015-07-22 02:19:17 +08:00
|
|
|
fuse_args *outargs)
|
2014-05-13 00:41:46 +08:00
|
|
|
{
|
2015-03-12 06:14:05 +08:00
|
|
|
int rv;
|
2014-05-13 00:41:46 +08:00
|
|
|
std::vector<std::string> argvalue;
|
|
|
|
|
2014-08-04 03:50:28 +08:00
|
|
|
str::split(argvalue,arg,'=');
|
2014-05-13 00:41:46 +08:00
|
|
|
switch(argvalue.size())
|
|
|
|
{
|
2015-03-12 06:14:05 +08:00
|
|
|
case 1:
|
|
|
|
rv = parse_and_process_arg(config,argvalue[0],outargs);
|
|
|
|
break;
|
|
|
|
|
2014-05-13 00:41:46 +08:00
|
|
|
case 2:
|
2015-03-11 06:42:59 +08:00
|
|
|
rv = parse_and_process_kv_arg(config,argvalue[0],argvalue[1]);
|
2014-05-13 00:41:46 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2014-06-26 03:17:26 +08:00
|
|
|
rv = 1;
|
2014-05-13 00:41:46 +08:00
|
|
|
break;
|
|
|
|
};
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2014-06-01 05:54:18 +08:00
|
|
|
static
|
2014-08-04 03:50:28 +08:00
|
|
|
int
|
2018-10-21 11:40:02 +08:00
|
|
|
process_branches(const char *arg,
|
|
|
|
Config &config)
|
2014-06-01 05:54:18 +08:00
|
|
|
{
|
2018-10-21 11:40:02 +08:00
|
|
|
config.branches.set(arg);
|
2015-09-07 06:24:36 +08:00
|
|
|
|
2014-08-04 03:50:28 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2014-06-01 05:54:18 +08:00
|
|
|
|
2014-08-04 03:50:28 +08:00
|
|
|
static
|
|
|
|
int
|
2015-07-22 02:19:17 +08:00
|
|
|
process_destmounts(const char *arg,
|
|
|
|
Config &config)
|
2014-08-04 03:50:28 +08:00
|
|
|
{
|
|
|
|
config.destmount = arg;
|
2014-06-01 05:54:18 +08:00
|
|
|
|
2014-08-04 03:50:28 +08:00
|
|
|
return 1;
|
2014-06-01 05:54:18 +08:00
|
|
|
}
|
|
|
|
|
2015-09-28 18:39:16 +08:00
|
|
|
static
|
|
|
|
void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
std::cout <<
|
|
|
|
"Usage: mergerfs [options] <srcpaths> <destpath>\n"
|
|
|
|
"\n"
|
|
|
|
" -o [opt,...] mount options\n"
|
|
|
|
" -h --help print help\n"
|
|
|
|
" -v --version print version\n"
|
|
|
|
"\n"
|
|
|
|
"mergerfs options:\n"
|
|
|
|
" <srcpaths> ':' delimited list of directories. Supports\n"
|
|
|
|
" shell globbing (must be escaped in shell)\n"
|
2017-02-19 02:45:43 +08:00
|
|
|
" -o defaults Default FUSE options which seem to provide the\n"
|
2015-09-28 18:39:16 +08:00
|
|
|
" best performance: atomic_o_trunc, auto_cache,\n"
|
|
|
|
" big_writes, default_permissions, splice_read,\n"
|
|
|
|
" splice_write, splice_move\n"
|
2017-02-19 02:45:43 +08:00
|
|
|
" -o func.<f>=<p> Set function <f> to policy <p>\n"
|
|
|
|
" -o category.<c>=<p> Set functions in category <c> to <p>\n"
|
|
|
|
" -o direct_io Bypass additional caching, increases write\n"
|
|
|
|
" speeds at the cost of reads. Please read docs\n"
|
|
|
|
" for more details as there are tradeoffs.\n"
|
|
|
|
" -o use_ino Have mergerfs generate inode values rather than\n"
|
|
|
|
" autogenerated by libfuse. Suggested.\n"
|
|
|
|
" -o minfreespace=<int> minimum free space needed for certain policies.\n"
|
2018-10-01 07:47:44 +08:00
|
|
|
" default = 4G\n"
|
2017-04-24 18:57:29 +08:00
|
|
|
" -o moveonenospc=<bool> Try to move file to another drive when ENOSPC\n"
|
2018-10-01 07:47:44 +08:00
|
|
|
" on write. default = false\n"
|
2017-02-19 02:45:43 +08:00
|
|
|
" -o dropcacheonclose=<bool>\n"
|
2017-04-24 18:57:29 +08:00
|
|
|
" When a file is closed suggest to OS it drop\n"
|
2017-02-19 02:45:43 +08:00
|
|
|
" the file's cache. This is useful when direct_io\n"
|
2018-10-01 07:47:44 +08:00
|
|
|
" is disabled. default = false\n"
|
2017-04-24 18:57:29 +08:00
|
|
|
" -o symlinkify=<bool> Read-only files, after a timeout, will be turned\n"
|
|
|
|
" into symlinks. Read docs for limitations and\n"
|
2018-10-01 07:47:44 +08:00
|
|
|
" possible issues. default = false\n"
|
2017-04-24 18:57:29 +08:00
|
|
|
" -o symlinkify_timeout=<int>\n"
|
|
|
|
" timeout in seconds before will turn to symlinks.\n"
|
2018-10-01 07:47:44 +08:00
|
|
|
" default = 3600\n"
|
2017-05-27 04:51:30 +08:00
|
|
|
" -o nullrw=<bool> Disables reads and writes. For benchmarking.\n"
|
2018-10-03 02:22:37 +08:00
|
|
|
" default = false\n"
|
2017-06-30 11:57:15 +08:00
|
|
|
" -o ignorepponrename=<bool>\n"
|
|
|
|
" Ignore path preserving when performing renames\n"
|
2017-07-03 02:39:59 +08:00
|
|
|
" and links. default = false\n"
|
2018-10-08 10:28:58 +08:00
|
|
|
" -o link_cow=<bool> delink/clone file on open to simulate CoW.\n"
|
|
|
|
" default = false\n"
|
2018-10-01 07:47:44 +08:00
|
|
|
" -o security_capability=<bool>\n"
|
|
|
|
" When disabled return ENOATTR when the xattr\n"
|
|
|
|
" security.capability is queried. default = true\n"
|
2018-10-08 10:28:58 +08:00
|
|
|
" -o xattr=passthrough|noattr|notsup\n"
|
|
|
|
" Runtime control of xattrs. By default xattr\n"
|
|
|
|
" requests will pass through to the underlying\n"
|
|
|
|
" filesystems. notattr will short circuit as if\n"
|
|
|
|
" nothing exists. notsup will respond as if not\n"
|
|
|
|
" supported or disabled. default = passthrough\n"
|
2015-09-28 18:39:16 +08:00
|
|
|
<< std::endl;
|
|
|
|
}
|
|
|
|
|
2014-05-13 00:41:46 +08:00
|
|
|
static
|
|
|
|
int
|
2015-07-22 02:19:17 +08:00
|
|
|
option_processor(void *data,
|
|
|
|
const char *arg,
|
|
|
|
int key,
|
|
|
|
fuse_args *outargs)
|
2014-05-13 00:41:46 +08:00
|
|
|
{
|
2015-07-22 02:19:17 +08:00
|
|
|
int rv = 0;
|
|
|
|
Config &config = *(Config*)data;
|
2014-05-13 00:41:46 +08:00
|
|
|
|
|
|
|
switch(key)
|
|
|
|
{
|
|
|
|
case FUSE_OPT_KEY_OPT:
|
2015-03-12 06:14:05 +08:00
|
|
|
rv = process_opt(config,arg,outargs);
|
2014-05-13 00:41:46 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case FUSE_OPT_KEY_NONOPT:
|
2018-10-21 11:40:02 +08:00
|
|
|
rv = config.branches.empty() ?
|
|
|
|
process_branches(arg,config) :
|
2014-08-04 03:50:28 +08:00
|
|
|
process_destmounts(arg,config);
|
2014-05-13 00:41:46 +08:00
|
|
|
break;
|
|
|
|
|
2015-09-28 18:39:16 +08:00
|
|
|
case MERGERFS_OPT_HELP:
|
|
|
|
usage();
|
|
|
|
close(2);
|
|
|
|
dup(1);
|
|
|
|
fuse_opt_add_arg(outargs,"-ho");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MERGERFS_OPT_VERSION:
|
2018-03-10 09:48:36 +08:00
|
|
|
std::cout << "mergerfs version: "
|
|
|
|
<< (MERGERFS_VERSION[0] ? MERGERFS_VERSION : "unknown")
|
|
|
|
<< std::endl;
|
2015-09-28 18:39:16 +08:00
|
|
|
fuse_opt_add_arg(outargs,"--version");
|
|
|
|
break;
|
|
|
|
|
2014-05-13 00:41:46 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace mergerfs
|
|
|
|
{
|
|
|
|
namespace options
|
|
|
|
{
|
|
|
|
void
|
2015-07-22 02:19:17 +08:00
|
|
|
parse(fuse_args &args,
|
|
|
|
Config &config)
|
2014-05-13 00:41:46 +08:00
|
|
|
{
|
2015-09-28 18:39:16 +08:00
|
|
|
const struct fuse_opt opts[] =
|
|
|
|
{
|
|
|
|
FUSE_OPT_KEY("-h",MERGERFS_OPT_HELP),
|
|
|
|
FUSE_OPT_KEY("--help",MERGERFS_OPT_HELP),
|
|
|
|
FUSE_OPT_KEY("-v",MERGERFS_OPT_VERSION),
|
2016-02-24 19:36:33 +08:00
|
|
|
FUSE_OPT_KEY("-V",MERGERFS_OPT_VERSION),
|
2015-09-28 18:39:16 +08:00
|
|
|
FUSE_OPT_KEY("--version",MERGERFS_OPT_VERSION),
|
2015-10-13 07:51:57 +08:00
|
|
|
{NULL,-1U,0}
|
2015-09-28 18:39:16 +08:00
|
|
|
};
|
|
|
|
|
2014-06-01 05:54:18 +08:00
|
|
|
|
2014-05-13 00:41:46 +08:00
|
|
|
fuse_opt_parse(&args,
|
|
|
|
&config,
|
2015-09-28 18:39:16 +08:00
|
|
|
opts,
|
2014-05-13 00:41:46 +08:00
|
|
|
::option_processor);
|
2014-05-31 11:23:45 +08:00
|
|
|
|
2018-10-21 11:40:02 +08:00
|
|
|
set_fsname(args,config.branches);
|
2015-03-12 01:50:19 +08:00
|
|
|
set_subtype(args);
|
2014-05-13 00:41:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|