2018-02-04 03:22:01 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
2018-01-11 06:30:31 +01:00
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
2018-02-04 03:22:01 +01:00
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
2018-01-11 06:30:31 +01:00
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
2018-02-04 03:22:01 +01:00
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
2018-01-11 06:30:31 +01:00
|
|
|
|
2019-05-03 15:09:58 +02:00
|
|
|
#include <RB_INC_FCNTL_H
|
2018-10-18 18:26:04 +02:00
|
|
|
#include <RB_INC_SYS_STAT_H
|
2019-01-02 03:35:09 +01:00
|
|
|
#include <RB_INC_SYS_STATFS_H
|
|
|
|
#include <RB_INC_SYS_STATVFS_H
|
2020-03-20 18:05:50 +01:00
|
|
|
#include <RB_INC_SYS_RESOURCE_H
|
2020-04-19 00:18:08 +02:00
|
|
|
#include <boost/filesystem.hpp>
|
2016-08-31 10:03:44 +02:00
|
|
|
|
2018-01-11 06:30:31 +01:00
|
|
|
#ifdef IRCD_USE_AIO
|
2019-01-23 22:20:38 +01:00
|
|
|
#include "fs_aio.h"
|
2018-01-11 06:30:31 +01:00
|
|
|
#endif
|
|
|
|
|
2019-08-11 07:06:08 +02:00
|
|
|
#ifdef IRCD_USE_IOU
|
|
|
|
#include "fs_iou.h"
|
|
|
|
#endif
|
|
|
|
|
2019-10-06 02:55:10 +02:00
|
|
|
// TODO: prevents use until io_uring support implemented
|
|
|
|
#undef IRCD_USE_IOU
|
|
|
|
|
2020-03-20 18:45:03 +01:00
|
|
|
namespace ircd::fs
|
|
|
|
{
|
|
|
|
extern conf::item<ulong> rlimit_nofile;
|
2020-05-21 02:56:02 +02:00
|
|
|
|
2020-04-19 03:05:55 +02:00
|
|
|
static void update_rlimit_nofile();
|
2020-03-20 18:45:03 +01:00
|
|
|
static void init_dump_info();
|
|
|
|
}
|
|
|
|
|
2020-05-21 02:56:02 +02:00
|
|
|
decltype(ircd::fs::log)
|
|
|
|
ircd::fs::log
|
|
|
|
{
|
|
|
|
"fs"
|
|
|
|
};
|
|
|
|
|
2020-03-20 18:45:03 +01:00
|
|
|
decltype(ircd::fs::rlimit_nofile)
|
|
|
|
ircd::fs::rlimit_nofile
|
|
|
|
{
|
2020-04-19 03:05:55 +02:00
|
|
|
{
|
|
|
|
{ "name", "ircd.fs.rlimit.nofile" },
|
|
|
|
{ "default", 65535L },
|
|
|
|
{ "persist", false },
|
|
|
|
},
|
|
|
|
update_rlimit_nofile
|
2020-03-20 18:45:03 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// init::init
|
|
|
|
//
|
|
|
|
|
2020-03-20 18:04:51 +01:00
|
|
|
ircd::fs::init::init()
|
2020-03-20 18:45:03 +01:00
|
|
|
{
|
|
|
|
init_dump_info();
|
|
|
|
}
|
|
|
|
|
2022-05-26 21:00:07 +02:00
|
|
|
[[gnu::cold]]
|
2020-03-20 18:45:03 +01:00
|
|
|
ircd::fs::init::~init()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::fs::init_dump_info()
|
2020-03-20 18:04:51 +01:00
|
|
|
{
|
|
|
|
const bool support_async
|
|
|
|
{
|
|
|
|
false || iou::system || aio::system
|
|
|
|
};
|
|
|
|
|
2020-06-07 09:56:43 +02:00
|
|
|
if(!support_async)
|
2020-03-20 18:04:51 +01:00
|
|
|
log::warning
|
|
|
|
{
|
|
|
|
log, "Support for asynchronous filesystem IO has not been"
|
|
|
|
" established. Filesystem IO is degraded to synchronous system calls."
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-03-20 18:45:03 +01:00
|
|
|
#if defined(HAVE_SYS_RESOURCE_H) && defined(RLIMIT_NOFILE)
|
|
|
|
void
|
2020-04-19 03:05:55 +02:00
|
|
|
ircd::fs::update_rlimit_nofile()
|
2020-03-20 18:45:03 +01:00
|
|
|
try
|
2020-03-20 18:04:51 +01:00
|
|
|
{
|
2020-05-11 02:45:01 +02:00
|
|
|
rlimit rlim[2];
|
2020-03-20 18:45:03 +01:00
|
|
|
syscall(getrlimit, RLIMIT_NOFILE, &rlim[0]);
|
2020-04-19 03:07:52 +02:00
|
|
|
|
2020-03-20 18:45:03 +01:00
|
|
|
rlim[1] = rlim[0];
|
2020-12-22 18:06:28 +01:00
|
|
|
rlim[1].rlim_cur = std::max(ulong(rlim[1].rlim_cur), ulong(fs::rlimit_nofile));
|
2020-03-20 18:45:03 +01:00
|
|
|
rlim[1].rlim_cur = std::min(rlim[1].rlim_cur, rlim[1].rlim_max);
|
2020-04-19 03:07:52 +02:00
|
|
|
if(rlim[0].rlim_cur == rlim[1].rlim_cur)
|
|
|
|
return;
|
|
|
|
|
2020-03-20 18:45:03 +01:00
|
|
|
syscall(setrlimit, RLIMIT_NOFILE, &rlim[1]);
|
|
|
|
log::info
|
|
|
|
{
|
|
|
|
log, "Raised resource limit for number of open files from %ld to %ld",
|
|
|
|
rlim[0].rlim_cur,
|
|
|
|
rlim[1].rlim_cur,
|
|
|
|
};
|
2020-03-20 18:04:51 +01:00
|
|
|
}
|
2020-03-20 18:45:03 +01:00
|
|
|
catch(const std::system_error &e)
|
|
|
|
{
|
|
|
|
log::warning
|
|
|
|
{
|
|
|
|
log, "Failed to raise resource limit for number of open files :%s",
|
|
|
|
e.what()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
void
|
|
|
|
ircd::fs::init_rlimit_nofile()
|
|
|
|
{
|
|
|
|
log::dwarning
|
|
|
|
{
|
|
|
|
log, "Cannot modify resource limit for number of open files."
|
|
|
|
};
|
|
|
|
}
|
|
|
|
#endif
|
2020-03-20 18:04:51 +01:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// fs/support.h
|
|
|
|
//
|
|
|
|
|
|
|
|
decltype(ircd::fs::support::pwritev2)
|
|
|
|
ircd::fs::support::pwritev2
|
2019-04-18 03:12:43 +02:00
|
|
|
{
|
|
|
|
#if defined(HAVE_PWRITEV2)
|
2019-08-07 00:49:24 +02:00
|
|
|
info::kernel_version[0] > 4 ||
|
|
|
|
(info::kernel_version[0] >= 4 && info::kernel_version[1] >= 6)
|
2019-04-18 03:12:43 +02:00
|
|
|
#else
|
|
|
|
false
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2020-03-20 18:04:51 +01:00
|
|
|
decltype(ircd::fs::support::preadv2)
|
|
|
|
ircd::fs::support::preadv2
|
2019-06-06 08:18:33 +02:00
|
|
|
{
|
|
|
|
#if defined(HAVE_PREADV2)
|
2019-08-07 00:49:24 +02:00
|
|
|
info::kernel_version[0] > 4 ||
|
|
|
|
(info::kernel_version[0] >= 4 && info::kernel_version[1] >= 6)
|
2019-06-06 08:18:33 +02:00
|
|
|
#else
|
|
|
|
false
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2020-03-20 18:04:51 +01:00
|
|
|
decltype(ircd::fs::support::sync)
|
|
|
|
ircd::fs::support::sync
|
2019-03-16 02:40:16 +01:00
|
|
|
{
|
2019-04-18 04:10:50 +02:00
|
|
|
#if defined(HAVE_PWRITEV2) && defined(RWF_SYNC)
|
2019-08-07 00:49:24 +02:00
|
|
|
info::kernel_version[0] > 4 ||
|
|
|
|
(info::kernel_version[0] >= 4 && info::kernel_version[1] >= 7)
|
2019-04-18 04:10:50 +02:00
|
|
|
#else
|
|
|
|
false
|
|
|
|
#endif
|
2019-03-16 02:40:16 +01:00
|
|
|
};
|
|
|
|
|
2020-03-20 18:04:51 +01:00
|
|
|
decltype(ircd::fs::support::dsync)
|
|
|
|
ircd::fs::support::dsync
|
2019-03-16 02:40:16 +01:00
|
|
|
{
|
2019-04-18 04:10:50 +02:00
|
|
|
#if defined(HAVE_PWRITEV2) && defined(RWF_DSYNC)
|
2019-08-07 00:49:24 +02:00
|
|
|
info::kernel_version[0] > 4 ||
|
|
|
|
(info::kernel_version[0] >= 4 && info::kernel_version[1] >= 7)
|
2019-04-18 04:10:50 +02:00
|
|
|
#else
|
|
|
|
false
|
|
|
|
#endif
|
2019-03-16 02:40:16 +01:00
|
|
|
};
|
|
|
|
|
2020-03-20 18:04:51 +01:00
|
|
|
decltype(ircd::fs::support::hipri)
|
|
|
|
ircd::fs::support::hipri
|
2019-03-16 02:40:16 +01:00
|
|
|
{
|
2019-04-18 04:10:50 +02:00
|
|
|
#if defined(HAVE_PWRITEV2) && defined(RWF_HIPRI)
|
2019-08-07 00:49:24 +02:00
|
|
|
info::kernel_version[0] > 4 ||
|
|
|
|
(info::kernel_version[0] >= 4 && info::kernel_version[1] >= 6)
|
2019-04-18 04:10:50 +02:00
|
|
|
#else
|
|
|
|
false
|
|
|
|
#endif
|
2019-03-16 02:40:16 +01:00
|
|
|
};
|
|
|
|
|
2020-03-20 18:04:51 +01:00
|
|
|
decltype(ircd::fs::support::nowait)
|
|
|
|
ircd::fs::support::nowait
|
2019-03-16 02:40:16 +01:00
|
|
|
{
|
2019-04-18 04:10:50 +02:00
|
|
|
#if defined(HAVE_PWRITEV2) && defined(RWF_NOWAIT)
|
2019-08-07 00:49:24 +02:00
|
|
|
info::kernel_version[0] > 4 ||
|
|
|
|
(info::kernel_version[0] >= 4 && info::kernel_version[1] >= 14)
|
2019-04-18 04:10:50 +02:00
|
|
|
#else
|
|
|
|
false
|
|
|
|
#endif
|
2019-03-16 02:40:16 +01:00
|
|
|
};
|
|
|
|
|
2020-03-20 18:04:51 +01:00
|
|
|
decltype(ircd::fs::support::append)
|
|
|
|
ircd::fs::support::append
|
2019-03-16 02:40:16 +01:00
|
|
|
{
|
2019-04-18 02:49:01 +02:00
|
|
|
#if defined(HAVE_PWRITEV2) && defined(RWF_APPEND)
|
2019-08-07 00:49:24 +02:00
|
|
|
info::kernel_version[0] > 4 ||
|
|
|
|
(info::kernel_version[0] >= 4 && info::kernel_version[1] >= 16)
|
2019-04-18 02:49:01 +02:00
|
|
|
#else
|
|
|
|
false
|
|
|
|
#endif
|
2019-03-16 02:40:16 +01:00
|
|
|
};
|
|
|
|
|
2020-03-20 18:04:51 +01:00
|
|
|
decltype(ircd::fs::support::rwh_write_life)
|
|
|
|
ircd::fs::support::rwh_write_life
|
2019-05-06 02:32:07 +02:00
|
|
|
{
|
|
|
|
#if defined(HAVE_FCNTL_H) && defined(F_SET_FILE_RW_HINT)
|
2022-03-20 19:35:39 +01:00
|
|
|
(info::kernel_version[0] > 4 ||
|
|
|
|
(info::kernel_version[0] >= 4 && info::kernel_version[1] >= 13))
|
|
|
|
&& !vg::active // not yet supported by valgrind
|
2019-05-06 02:32:07 +02:00
|
|
|
#else
|
|
|
|
false
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2020-03-20 18:04:51 +01:00
|
|
|
decltype(ircd::fs::support::rwf_write_life)
|
|
|
|
ircd::fs::support::rwf_write_life
|
2019-05-06 02:32:07 +02:00
|
|
|
{
|
|
|
|
#if defined(RWF_WRITE_LIFE_SHIFT)
|
|
|
|
false //TODO: XXX
|
|
|
|
#else
|
|
|
|
false
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2020-03-20 18:04:51 +01:00
|
|
|
decltype(ircd::fs::support::aio)
|
|
|
|
ircd::fs::support::aio
|
|
|
|
{
|
|
|
|
#ifdef IRCD_USE_AIO
|
|
|
|
true
|
|
|
|
#else
|
|
|
|
false
|
|
|
|
#endif
|
|
|
|
};
|
2018-08-12 20:56:53 +02:00
|
|
|
|
2020-03-20 18:04:51 +01:00
|
|
|
void
|
|
|
|
ircd::fs::support::dump_info()
|
2018-08-12 20:56:53 +02:00
|
|
|
{
|
2020-03-20 18:04:51 +01:00
|
|
|
#if defined(IRCD_USE_AIO) || defined(IRCD_USE_IOU)
|
|
|
|
const bool support_async {true};
|
|
|
|
#else
|
|
|
|
const bool support_async {false};
|
|
|
|
#endif
|
|
|
|
|
2020-10-10 14:17:26 +02:00
|
|
|
char support[128] {0};
|
|
|
|
const auto _append{[&support]
|
|
|
|
(const string_view &name, const bool &avail, const int &enable)
|
|
|
|
{
|
|
|
|
strlcat(support, fmt::bsprintf<64>
|
|
|
|
{
|
|
|
|
"%s:%c%s ",
|
|
|
|
name,
|
|
|
|
avail == true? 'y': 'n',
|
|
|
|
enable == true? "y": enable == false? "n": "",
|
|
|
|
});
|
|
|
|
}};
|
|
|
|
|
|
|
|
_append("async", support_async, -1);
|
|
|
|
_append("preadv2", preadv2, -1);
|
|
|
|
_append("pwritev2", pwritev2, -1);
|
|
|
|
_append("SYNC", sync, -1);
|
|
|
|
_append("DSYNC", dsync, -1);
|
|
|
|
_append("HIPRI", hipri, -1);
|
|
|
|
_append("NOWAIT", nowait, -1);
|
|
|
|
_append("APPEND", append, -1);
|
|
|
|
_append("RWH", rwh_write_life, -1);
|
|
|
|
_append("RWF", rwf_write_life, -1);
|
|
|
|
|
2020-03-20 18:04:51 +01:00
|
|
|
log::info
|
2020-03-16 19:09:25 +01:00
|
|
|
{
|
2020-10-10 14:17:26 +02:00
|
|
|
log, "VFS %s",
|
|
|
|
support
|
2020-03-16 19:09:25 +01:00
|
|
|
};
|
|
|
|
|
2020-03-20 18:04:51 +01:00
|
|
|
#ifdef RB_DEBUG
|
|
|
|
const unique_mutable_buffer buf
|
|
|
|
{
|
|
|
|
PATH_MAX_LEN + 1
|
|
|
|
};
|
|
|
|
|
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
log, "Current working directory: `%s'", cwd(buf)
|
|
|
|
};
|
|
|
|
#endif
|
2018-08-12 20:56:53 +02:00
|
|
|
}
|
|
|
|
|
2020-03-20 18:04:51 +01:00
|
|
|
bool
|
|
|
|
ircd::fs::support::fallocate(const string_view &path,
|
|
|
|
const write_opts &wopts)
|
|
|
|
try
|
2018-08-12 20:56:53 +02:00
|
|
|
{
|
2022-06-18 21:05:17 +02:00
|
|
|
const fs::fd fd
|
2020-03-20 18:04:51 +01:00
|
|
|
{
|
2022-06-18 21:05:17 +02:00
|
|
|
path, fd::opts
|
|
|
|
{
|
|
|
|
.mode = std::ios::out,
|
|
|
|
},
|
2020-03-20 18:04:51 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
fs::allocate(fd, info::page_size, wopts);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch(const std::system_error &e)
|
|
|
|
{
|
|
|
|
const auto &ec(e.code());
|
|
|
|
if(system_category(ec)) switch(ec.value())
|
|
|
|
{
|
|
|
|
case int(std::errc::invalid_argument):
|
|
|
|
case int(std::errc::operation_not_supported):
|
|
|
|
return false;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::fs::support::direct_io(const string_view &path)
|
|
|
|
try
|
|
|
|
{
|
2022-06-18 21:05:17 +02:00
|
|
|
fs::fd
|
|
|
|
{
|
|
|
|
path, fd::opts
|
|
|
|
{
|
|
|
|
.mode = std::ios::out,
|
|
|
|
.direct = true,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-03-20 18:04:51 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch(const std::system_error &e)
|
|
|
|
{
|
|
|
|
const auto &ec(e.code());
|
|
|
|
if(system_category(ec)) switch(ec.value())
|
|
|
|
{
|
|
|
|
case int(std::errc::invalid_argument):
|
|
|
|
return false;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw;
|
2018-08-12 20:56:53 +02:00
|
|
|
}
|
|
|
|
|
2020-03-20 18:54:14 +01:00
|
|
|
#if defined(HAVE_SYS_RESOURCE_H) && defined(RLIMIT_FSIZE)
|
|
|
|
size_t
|
|
|
|
ircd::fs::support::rlimit_fsize()
|
|
|
|
{
|
|
|
|
rlimit rlim;
|
|
|
|
syscall(getrlimit, RLIMIT_FSIZE, &rlim);
|
|
|
|
return rlim.rlim_cur;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
size_t
|
|
|
|
ircd::fs::support::rlimit_fize()
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-03-20 18:05:50 +01:00
|
|
|
#if defined(HAVE_SYS_RESOURCE_H) && defined(RLIMIT_NOFILE)
|
|
|
|
size_t
|
|
|
|
ircd::fs::support::rlimit_nofile()
|
|
|
|
{
|
|
|
|
rlimit rlim;
|
|
|
|
syscall(getrlimit, RLIMIT_NOFILE, &rlim);
|
|
|
|
return rlim.rlim_cur;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
size_t
|
|
|
|
ircd::fs::support::rlimit_nofile()
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-11-28 03:09:12 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// fs.h / misc
|
|
|
|
//
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::fs::mkdir(const string_view &path)
|
|
|
|
try
|
|
|
|
{
|
2019-01-25 19:35:39 +01:00
|
|
|
return filesystem::create_directories(_path(path));
|
2018-11-28 03:09:12 +01:00
|
|
|
}
|
|
|
|
catch(const filesystem::filesystem_error &e)
|
|
|
|
{
|
2018-12-16 05:12:39 +01:00
|
|
|
throw error{e};
|
2018-11-28 03:09:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::fs::remove(const string_view &path)
|
|
|
|
try
|
|
|
|
{
|
2020-06-09 04:32:12 +02:00
|
|
|
const prof::syscall_usage_warning message
|
|
|
|
{
|
|
|
|
"fs::remove(%s)", path
|
|
|
|
};
|
|
|
|
|
2019-01-25 19:35:39 +01:00
|
|
|
return filesystem::remove(_path(path));
|
2018-11-28 03:09:12 +01:00
|
|
|
}
|
|
|
|
catch(const filesystem::filesystem_error &e)
|
|
|
|
{
|
2018-12-16 05:12:39 +01:00
|
|
|
throw error{e};
|
2018-11-28 03:09:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::fs::remove(std::nothrow_t,
|
|
|
|
const string_view &path)
|
|
|
|
{
|
2020-06-09 04:32:12 +02:00
|
|
|
const prof::syscall_usage_warning message
|
|
|
|
{
|
|
|
|
"fs::remove(%s)", path
|
|
|
|
};
|
|
|
|
|
2018-11-28 03:09:12 +01:00
|
|
|
boost::system::error_code ec;
|
2019-01-25 19:35:39 +01:00
|
|
|
return filesystem::remove(_path(path), ec);
|
2018-11-28 03:09:12 +01:00
|
|
|
}
|
|
|
|
|
2018-12-30 00:27:58 +01:00
|
|
|
bool
|
2018-11-28 03:09:12 +01:00
|
|
|
ircd::fs::rename(const string_view &old,
|
|
|
|
const string_view &new_)
|
|
|
|
try
|
|
|
|
{
|
2020-06-09 04:32:12 +02:00
|
|
|
const prof::syscall_usage_warning message
|
|
|
|
{
|
|
|
|
"fs::rename(%s, %s)", old, new_
|
|
|
|
};
|
|
|
|
|
2019-01-25 19:35:39 +01:00
|
|
|
filesystem::rename(_path(old), _path(new_));
|
2018-12-30 00:27:58 +01:00
|
|
|
return true;
|
2018-11-28 03:09:12 +01:00
|
|
|
}
|
|
|
|
catch(const filesystem::filesystem_error &e)
|
|
|
|
{
|
2018-12-16 05:12:39 +01:00
|
|
|
throw error{e};
|
2018-11-28 03:09:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::fs::rename(std::nothrow_t,
|
|
|
|
const string_view &old,
|
|
|
|
const string_view &new_)
|
|
|
|
{
|
2020-06-09 04:32:12 +02:00
|
|
|
const prof::syscall_usage_warning message
|
|
|
|
{
|
|
|
|
"fs::rename(%s, %s)", old, new_
|
|
|
|
};
|
|
|
|
|
2018-11-28 03:09:12 +01:00
|
|
|
boost::system::error_code ec;
|
2019-01-25 19:35:39 +01:00
|
|
|
filesystem::rename(_path(old), _path(new_), ec);
|
2018-11-28 03:09:12 +01:00
|
|
|
return !ec;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string>
|
2018-12-30 00:27:58 +01:00
|
|
|
ircd::fs::ls_r(const string_view &path)
|
2018-11-28 03:09:12 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
const filesystem::recursive_directory_iterator end;
|
|
|
|
filesystem::recursive_directory_iterator it
|
|
|
|
{
|
2019-01-25 19:35:39 +01:00
|
|
|
_path(path)
|
2018-11-28 03:09:12 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<std::string> ret;
|
2019-03-01 02:53:01 +01:00
|
|
|
std::for_each(it, end, [&ret]
|
2018-11-28 03:09:12 +01:00
|
|
|
(const auto &ent)
|
|
|
|
{
|
2019-03-01 02:53:01 +01:00
|
|
|
ret.emplace_back(ent.path().string());
|
2018-11-28 03:09:12 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
catch(const filesystem::filesystem_error &e)
|
|
|
|
{
|
2018-12-16 05:12:39 +01:00
|
|
|
throw error{e};
|
2018-11-28 03:09:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string>
|
|
|
|
ircd::fs::ls(const string_view &path)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
static const filesystem::directory_iterator end;
|
|
|
|
filesystem::directory_iterator it
|
|
|
|
{
|
2019-01-25 19:35:39 +01:00
|
|
|
_path(path)
|
2018-11-28 03:09:12 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<std::string> ret;
|
2019-03-01 02:53:01 +01:00
|
|
|
std::for_each(it, end, [&ret]
|
2018-11-28 03:09:12 +01:00
|
|
|
(const auto &ent)
|
|
|
|
{
|
2019-03-01 02:53:01 +01:00
|
|
|
ret.emplace_back(ent.path().string());
|
2018-11-28 03:09:12 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
catch(const filesystem::filesystem_error &e)
|
|
|
|
{
|
2018-12-16 05:12:39 +01:00
|
|
|
throw error{e};
|
2018-11-28 03:09:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::fs::size(const string_view &path)
|
2018-12-12 22:16:12 +01:00
|
|
|
try
|
2018-11-28 03:09:12 +01:00
|
|
|
{
|
2019-01-25 19:35:39 +01:00
|
|
|
return filesystem::file_size(_path(path));
|
2018-11-28 03:09:12 +01:00
|
|
|
}
|
2018-12-12 22:16:12 +01:00
|
|
|
catch(const filesystem::filesystem_error &e)
|
|
|
|
{
|
|
|
|
throw error{e};
|
|
|
|
}
|
2018-11-28 03:09:12 +01:00
|
|
|
|
2020-10-20 07:22:33 +02:00
|
|
|
bool
|
|
|
|
ircd::fs::is_exec(const string_view &path)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return filesystem::status(_path(path)).permissions() & filesystem::owner_exe;
|
|
|
|
}
|
|
|
|
catch(const filesystem::filesystem_error &e)
|
|
|
|
{
|
|
|
|
throw error{e};
|
|
|
|
}
|
|
|
|
|
2018-11-28 03:09:12 +01:00
|
|
|
bool
|
|
|
|
ircd::fs::is_reg(const string_view &path)
|
|
|
|
try
|
|
|
|
{
|
2019-01-25 19:35:39 +01:00
|
|
|
return filesystem::is_regular_file(_path(path));
|
2018-11-28 03:09:12 +01:00
|
|
|
}
|
|
|
|
catch(const filesystem::filesystem_error &e)
|
|
|
|
{
|
|
|
|
throw error{e};
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::fs::is_dir(const string_view &path)
|
|
|
|
try
|
|
|
|
{
|
2019-01-25 19:35:39 +01:00
|
|
|
return filesystem::is_directory(_path(path));
|
2018-11-28 03:09:12 +01:00
|
|
|
}
|
|
|
|
catch(const filesystem::filesystem_error &e)
|
|
|
|
{
|
|
|
|
throw error{e};
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::fs::exists(const string_view &path)
|
|
|
|
try
|
|
|
|
{
|
2019-01-25 19:35:39 +01:00
|
|
|
return filesystem::exists(_path(path));
|
2018-11-28 03:09:12 +01:00
|
|
|
}
|
|
|
|
catch(const filesystem::filesystem_error &e)
|
|
|
|
{
|
|
|
|
throw error{e};
|
|
|
|
}
|
|
|
|
|
2018-03-23 23:37:01 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// fs/stdin.h
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::fs::stdin::readline(const mutable_buffer &buf)
|
2018-12-11 23:32:51 +01:00
|
|
|
try
|
2018-03-23 23:37:01 +01:00
|
|
|
{
|
|
|
|
boost::asio::posix::stream_descriptor fd
|
|
|
|
{
|
2018-10-17 14:12:10 +02:00
|
|
|
ios::get(), dup(STDIN_FILENO)
|
2018-03-23 23:37:01 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
boost::asio::streambuf sb
|
|
|
|
{
|
|
|
|
size(buf)
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto interruption{[&fd]
|
2018-12-23 01:21:27 +01:00
|
|
|
(ctx::ctx *const &interruptor)
|
2018-03-23 23:37:01 +01:00
|
|
|
{
|
|
|
|
fd.cancel();
|
|
|
|
}};
|
|
|
|
|
2018-12-23 05:55:52 +01:00
|
|
|
size_t len; continuation
|
2018-03-23 23:37:01 +01:00
|
|
|
{
|
2018-12-23 05:55:52 +01:00
|
|
|
continuation::asio_predicate, interruption, [&len, &fd, &sb]
|
|
|
|
(auto &yield)
|
2018-12-23 05:11:00 +01:00
|
|
|
{
|
2018-12-23 05:55:52 +01:00
|
|
|
len = boost::asio::async_read_until(fd, sb, '\n', yield);
|
|
|
|
}
|
2018-03-23 23:37:01 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
std::istream is{&sb};
|
|
|
|
is.get(data(buf), size(buf), '\n');
|
|
|
|
return string_view
|
|
|
|
{
|
|
|
|
data(buf), size_t(is.gcount())
|
|
|
|
};
|
|
|
|
}
|
2018-12-11 23:32:51 +01:00
|
|
|
catch(boost::system::system_error &e)
|
|
|
|
{
|
2019-04-11 16:50:48 +02:00
|
|
|
throw_system_error(e.code());
|
2018-12-11 23:32:51 +01:00
|
|
|
__builtin_unreachable();
|
|
|
|
}
|
2018-03-23 23:37:01 +01:00
|
|
|
|
2018-06-03 18:47:42 +02:00
|
|
|
//
|
|
|
|
// tty
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::fs::stdin::tty::tty()
|
|
|
|
:fd{[]
|
|
|
|
{
|
2020-12-21 15:43:15 +01:00
|
|
|
char buf[256];
|
2022-06-18 21:05:17 +02:00
|
|
|
sys::call(::ttyname_r, STDIN_FILENO, buf, sizeof(buf));
|
|
|
|
return fs::fd
|
2018-06-03 18:47:42 +02:00
|
|
|
{
|
2022-06-18 21:05:17 +02:00
|
|
|
string_view{buf}, fd::opts
|
|
|
|
{
|
|
|
|
.mode = std::ios_base::out
|
|
|
|
}
|
2018-06-03 18:47:42 +02:00
|
|
|
};
|
|
|
|
}()}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::fs::stdin::tty::write(const string_view &buf)
|
|
|
|
{
|
|
|
|
return syscall(::write, int(*this), buf.data(), buf.size());
|
|
|
|
}
|
|
|
|
|
2020-05-09 02:26:46 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// fs/select.h
|
|
|
|
//
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::fs::select(const vector_view<const fd> &fd_)
|
|
|
|
{
|
|
|
|
using asio::posix::stream_descriptor;
|
|
|
|
|
|
|
|
static ios::descriptor desc
|
|
|
|
{
|
|
|
|
"ircd::fs::select"
|
|
|
|
};
|
|
|
|
|
|
|
|
const size_t num(size(fd_));
|
|
|
|
std::optional<stream_descriptor> _fd[num];
|
|
|
|
const unwind release{[&_fd]
|
|
|
|
{
|
|
|
|
for(auto &fd : _fd)
|
|
|
|
if(fd)
|
|
|
|
fd->release();
|
|
|
|
}};
|
|
|
|
|
|
|
|
size_t ret(-1);
|
|
|
|
ctx::latch latch(num);
|
|
|
|
const auto callback{[&num, &_fd, &latch, &ret]
|
|
|
|
(const boost::system::error_code &ec, const auto &fd)
|
|
|
|
{
|
|
|
|
// The first successful callback is associated with an input fd
|
|
|
|
// and its array indice becomes the return value.
|
|
|
|
if(!ec && ret == size_t(-1))
|
|
|
|
{
|
|
|
|
const auto it
|
|
|
|
{
|
|
|
|
std::find_if(_fd, _fd + num, [&fd]
|
|
|
|
(const auto &_fd)
|
|
|
|
{
|
|
|
|
return _fd && std::addressof(*_fd) == std::addressof(*fd);
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
ret = std::distance(_fd, it);
|
|
|
|
assert(ret < num);
|
|
|
|
}
|
|
|
|
|
|
|
|
latch.count_down();
|
|
|
|
}};
|
|
|
|
|
|
|
|
for(size_t i(0); i < num; ++i)
|
|
|
|
{
|
|
|
|
// Allow a closed descriptor in the vector to be no-op.
|
|
|
|
if(!fd_[i])
|
|
|
|
{
|
|
|
|
latch.count_down();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
_fd[i] =
|
|
|
|
{
|
|
|
|
ios::get(), int(fd_[i])
|
|
|
|
};
|
|
|
|
|
|
|
|
auto handle
|
|
|
|
{
|
|
|
|
std::bind(callback, ph::_1, std::cref(_fd[i]))
|
|
|
|
};
|
|
|
|
|
|
|
|
_fd[i]->async_wait(stream_descriptor::wait_read, ios::handle(desc, std::move(handle)));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::exception_ptr eptr; try
|
|
|
|
{
|
|
|
|
latch.wait();
|
|
|
|
assert(ret < num);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
eptr = std::current_exception();
|
|
|
|
const ctx::exception_handler eh;
|
|
|
|
const ctx::uninterruptible::nothrow ui;
|
|
|
|
for(auto &fd : _fd)
|
|
|
|
fd->cancel();
|
|
|
|
|
|
|
|
latch.wait();
|
|
|
|
assert(eptr);
|
|
|
|
std::rethrow_exception(eptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-01-11 06:30:31 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2018-12-14 01:54:09 +01:00
|
|
|
// fs/sync.h
|
2018-01-11 06:30:31 +01:00
|
|
|
//
|
|
|
|
|
2018-12-14 01:54:09 +01:00
|
|
|
ircd::fs::sync_opts
|
|
|
|
const ircd::fs::sync_opts_default;
|
2018-01-11 06:30:31 +01:00
|
|
|
|
2018-12-14 03:03:17 +01:00
|
|
|
void
|
|
|
|
ircd::fs::sync(const fd &fd,
|
|
|
|
const off_t &offset,
|
|
|
|
const size_t &length,
|
|
|
|
const sync_opts &opts)
|
|
|
|
{
|
|
|
|
return sync(fd, opts);
|
|
|
|
}
|
|
|
|
|
2018-12-12 19:55:56 +01:00
|
|
|
void
|
|
|
|
ircd::fs::sync(const fd &fd,
|
2018-12-14 01:54:09 +01:00
|
|
|
const sync_opts &opts)
|
2018-12-12 19:55:56 +01:00
|
|
|
{
|
2019-04-06 21:13:16 +02:00
|
|
|
assert(opts.op == op::SYNC);
|
2020-03-07 00:19:05 +01:00
|
|
|
const prof::syscall_usage_warning message
|
2018-12-18 01:02:05 +01:00
|
|
|
{
|
|
|
|
"fs::sync(fd:%d)", int(fd)
|
|
|
|
};
|
|
|
|
|
2018-12-12 19:55:56 +01:00
|
|
|
#ifdef __linux__
|
|
|
|
syscall(::syncfs, fd);
|
|
|
|
#else
|
|
|
|
syscall(::sync);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-12-14 03:03:17 +01:00
|
|
|
void
|
|
|
|
ircd::fs::flush(const fd &fd,
|
|
|
|
const off_t &offset,
|
|
|
|
const size_t &length,
|
|
|
|
const sync_opts &opts)
|
|
|
|
{
|
|
|
|
return flush(fd, opts);
|
|
|
|
}
|
|
|
|
|
2018-08-19 07:58:43 +02:00
|
|
|
void
|
2018-12-14 02:46:08 +01:00
|
|
|
ircd::fs::flush(const fd &fd,
|
2018-12-14 01:54:09 +01:00
|
|
|
const sync_opts &opts)
|
2018-08-19 07:58:43 +02:00
|
|
|
{
|
2019-04-06 21:13:16 +02:00
|
|
|
assert(opts.op == op::SYNC);
|
2018-12-18 01:02:05 +01:00
|
|
|
|
2019-08-11 07:06:08 +02:00
|
|
|
#ifdef IRCD_USE_IOU
|
|
|
|
if(iou::system && opts.aio)
|
2021-01-22 04:57:51 +01:00
|
|
|
return void(iou::fsync(fd, opts));
|
2019-08-11 07:06:08 +02:00
|
|
|
#endif
|
|
|
|
|
2018-08-19 07:58:43 +02:00
|
|
|
#ifdef IRCD_USE_AIO
|
2018-12-29 00:02:02 +01:00
|
|
|
if(aio::system && opts.aio)
|
2018-12-14 02:46:08 +01:00
|
|
|
{
|
2020-03-20 18:04:51 +01:00
|
|
|
if(support::aio_fdsync && !opts.metadata)
|
2021-01-22 04:57:51 +01:00
|
|
|
return void(aio::fsync(fd, opts));
|
2018-08-19 07:58:43 +02:00
|
|
|
|
2021-01-22 04:57:51 +01:00
|
|
|
else if(support::aio_fsync && opts.metadata)
|
|
|
|
return void(aio::fsync(fd, opts));
|
2018-12-14 02:46:08 +01:00
|
|
|
}
|
2018-08-19 07:58:43 +02:00
|
|
|
#endif
|
2018-11-29 19:18:43 +01:00
|
|
|
|
2020-03-07 00:19:05 +01:00
|
|
|
const prof::syscall_usage_warning message
|
2019-04-26 15:19:48 +02:00
|
|
|
{
|
|
|
|
"fs::flush(fd:%d, {metadata:%b aio:%b:%b})",
|
|
|
|
int(fd),
|
|
|
|
opts.metadata,
|
|
|
|
opts.aio,
|
2020-03-20 18:04:51 +01:00
|
|
|
opts.metadata? support::aio_fsync : support::aio_fdsync
|
2019-04-26 15:19:48 +02:00
|
|
|
};
|
|
|
|
|
2018-12-14 02:46:08 +01:00
|
|
|
if(!opts.metadata)
|
|
|
|
return void(syscall(::fdatasync, fd));
|
|
|
|
|
2018-12-18 01:02:05 +01:00
|
|
|
return void(syscall(::fsync, fd));
|
2018-08-19 07:58:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2018-01-11 06:30:31 +01:00
|
|
|
//
|
2018-08-19 07:58:43 +02:00
|
|
|
// fs/read.h
|
2018-01-11 06:30:31 +01:00
|
|
|
//
|
|
|
|
|
2018-08-19 07:58:43 +02:00
|
|
|
ircd::fs::read_opts
|
|
|
|
const ircd::fs::read_opts_default
|
|
|
|
{};
|
|
|
|
|
2019-03-16 05:25:07 +01:00
|
|
|
size_t
|
2018-06-02 20:44:53 +02:00
|
|
|
ircd::fs::prefetch(const fd &fd,
|
|
|
|
const size_t &count,
|
|
|
|
const read_opts &opts)
|
2019-05-03 12:33:45 +02:00
|
|
|
{
|
2019-05-03 13:34:16 +02:00
|
|
|
#if defined(POSIX_FADV_WILLNEED)
|
2019-07-12 22:34:18 +02:00
|
|
|
return advise(fd, POSIX_FADV_WILLNEED, count, opts);
|
2019-05-03 13:34:16 +02:00
|
|
|
#else
|
|
|
|
return 0UL;
|
|
|
|
#endif
|
2019-05-03 12:33:45 +02:00
|
|
|
}
|
|
|
|
|
2019-04-21 01:59:08 +02:00
|
|
|
bool
|
2020-11-01 14:19:31 +01:00
|
|
|
ircd::fs::incore(const fd &fd,
|
|
|
|
const size_t &count,
|
|
|
|
const read_opts &opts)
|
2019-04-21 01:59:08 +02:00
|
|
|
{
|
2022-06-18 21:05:17 +02:00
|
|
|
const fs::map::opts map_opts
|
|
|
|
{
|
|
|
|
fs::opts
|
|
|
|
{
|
|
|
|
.offset = off_t(align(opts.offset, info::page_size)),
|
|
|
|
.blocking = false,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-04-21 01:59:08 +02:00
|
|
|
const size_t &map_size
|
|
|
|
{
|
|
|
|
count?: size(fd)
|
|
|
|
};
|
|
|
|
|
2020-08-24 17:56:23 +02:00
|
|
|
const size_t &map_pages
|
2019-04-21 01:59:08 +02:00
|
|
|
{
|
2020-08-24 17:56:23 +02:00
|
|
|
(map_size + info::page_size - 1) / info::page_size
|
2019-04-21 01:59:08 +02:00
|
|
|
};
|
|
|
|
|
2020-08-24 17:56:23 +02:00
|
|
|
assert(map_opts.offset % 4096 == 0);
|
|
|
|
const fs::map map
|
2019-05-03 13:33:48 +02:00
|
|
|
{
|
2020-08-24 17:56:23 +02:00
|
|
|
fd, map_opts, map_size
|
2019-05-03 13:33:48 +02:00
|
|
|
};
|
2019-04-21 01:59:08 +02:00
|
|
|
|
2020-11-01 14:19:31 +01:00
|
|
|
const size_t res
|
|
|
|
{
|
|
|
|
allocator::incore(map)
|
|
|
|
};
|
2019-05-03 13:33:48 +02:00
|
|
|
|
2020-11-01 14:19:31 +01:00
|
|
|
return res == map_size;
|
2019-04-21 01:59:08 +02:00
|
|
|
}
|
|
|
|
|
2018-05-30 11:13:06 +02:00
|
|
|
std::string
|
|
|
|
ircd::fs::read(const fd &fd,
|
|
|
|
const read_opts &opts)
|
|
|
|
{
|
|
|
|
return string(size(fd), [&fd, &opts]
|
|
|
|
(const mutable_buffer &buf)
|
|
|
|
{
|
|
|
|
return read(fd, buf, opts);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-11-29 02:26:25 +01:00
|
|
|
ircd::const_buffer
|
|
|
|
ircd::fs::read(const fd &fd,
|
|
|
|
const mutable_buffer &buf,
|
|
|
|
const read_opts &opts)
|
|
|
|
{
|
|
|
|
const mutable_buffers bufs
|
|
|
|
{
|
|
|
|
&buf, 1
|
|
|
|
};
|
|
|
|
|
|
|
|
return mutable_buffer
|
|
|
|
{
|
|
|
|
data(buf), read(fd, bufs, opts)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-05-07 04:14:20 +02:00
|
|
|
size_t
|
|
|
|
ircd::fs::read(const vector_view<read_op> &op)
|
|
|
|
{
|
|
|
|
// Use IOV_MAX as a sanity value for number of operations here
|
|
|
|
if(unlikely(op.size() > info::iov_max))
|
|
|
|
throw error
|
|
|
|
{
|
|
|
|
make_error_code(std::errc::invalid_argument),
|
|
|
|
"Read operation count:%zu exceeds max:%zu",
|
|
|
|
op.size(),
|
|
|
|
info::iov_max,
|
|
|
|
};
|
|
|
|
|
|
|
|
bool aio {true}, all {false};
|
|
|
|
for(size_t i(0); i < op.size(); ++i)
|
|
|
|
{
|
|
|
|
assert(op[i].opts);
|
|
|
|
assert(op[i].opts->aio);
|
|
|
|
|
|
|
|
// If any op isn't tolerant of less bytes actually read than they
|
|
|
|
// requested, they require us to perform the unix read loop, and
|
|
|
|
// that ruins things for everybody!
|
|
|
|
assert(!op[i].opts->all);
|
|
|
|
//all |= op[i].opts->all;
|
|
|
|
|
|
|
|
// If any op doesn't want AIO we have to fallback on sequential
|
|
|
|
// blocking reads for all ops.
|
|
|
|
assert(op[i].opts->aio);
|
|
|
|
//aio &= op[i].opts->aio;
|
|
|
|
|
|
|
|
// EINVAL for exceeding this system's IOV_MAX
|
|
|
|
if(unlikely(op[i].bufs.size() > info::iov_max))
|
|
|
|
throw error
|
|
|
|
{
|
|
|
|
make_error_code(std::errc::invalid_argument),
|
|
|
|
"op[%zu] :buffer count of %zu exceeds IOV_MAX of %zu",
|
|
|
|
i,
|
|
|
|
op[i].bufs.size(),
|
|
|
|
info::iov_max,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef IRCD_USE_AIO
|
|
|
|
if(likely(aio::system && aio && !all))
|
|
|
|
return aio::read(op);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Fallback to sequential read operations
|
|
|
|
size_t ret(0);
|
|
|
|
for(size_t i(0); i < op.size(); ++i) try
|
|
|
|
{
|
|
|
|
assert(op[i].fd);
|
|
|
|
assert(op[i].opts);
|
|
|
|
op[i].ret = read(*op[i].fd, op[i].bufs, *op[i].opts);
|
|
|
|
ret += op[i].ret;
|
|
|
|
}
|
|
|
|
catch(const std::system_error &)
|
|
|
|
{
|
|
|
|
op[i].eptr = std::current_exception();
|
|
|
|
op[i].ret = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-03-16 00:06:51 +01:00
|
|
|
namespace ircd::fs
|
|
|
|
{
|
|
|
|
static int flags(const read_opts &opts);
|
2019-06-06 08:18:33 +02:00
|
|
|
static size_t _read_preadv2(const fd &, const const_iovec_view &, const read_opts &);
|
|
|
|
static size_t _read_preadv(const fd &, const const_iovec_view &, const read_opts &);
|
2022-05-04 21:14:00 +02:00
|
|
|
static size_t _read(const fd &, const const_iovec_view &, const read_opts &);
|
2019-03-16 00:06:51 +01:00
|
|
|
}
|
|
|
|
|
2018-12-19 21:24:16 +01:00
|
|
|
/// Read from file descriptor fd into buffers. The number of bytes read into
|
|
|
|
/// the buffers is returned. By default (via read_opts.all) this call will
|
|
|
|
/// loop internally until the buffers are full or EOF. To allow for a partial
|
|
|
|
/// read(), disable read_opts.all. Note that to maintain alignments (i.e when
|
|
|
|
/// direct-io or for special files read_opts.all must be false). By default
|
|
|
|
/// (via read_opts.interruptible) this call can throw if the syscall was
|
|
|
|
/// interrupted before reading any bytes.
|
2018-12-21 22:19:34 +01:00
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wstack-usage="
|
2018-11-29 02:26:25 +01:00
|
|
|
size_t
|
2018-12-21 22:19:34 +01:00
|
|
|
__attribute__((stack_protect))
|
2018-05-30 11:13:06 +02:00
|
|
|
ircd::fs::read(const fd &fd,
|
2018-11-29 02:26:25 +01:00
|
|
|
const mutable_buffers &bufs,
|
2018-12-18 22:41:45 +01:00
|
|
|
const read_opts &opts_)
|
|
|
|
{
|
2018-12-21 22:19:34 +01:00
|
|
|
if(unlikely(bufs.size() > info::iov_max))
|
|
|
|
throw error
|
|
|
|
{
|
|
|
|
make_error_code(std::errc::invalid_argument),
|
|
|
|
"Buffer count of %zu exceeds IOV_MAX of %zu",
|
|
|
|
bufs.size(),
|
|
|
|
info::iov_max
|
|
|
|
};
|
|
|
|
|
2018-12-18 22:41:45 +01:00
|
|
|
size_t ret(0);
|
2018-12-21 19:28:51 +01:00
|
|
|
read_opts opts(opts_);
|
2018-12-21 22:19:34 +01:00
|
|
|
assert(bufs.size() <= info::iov_max);
|
2018-12-21 19:28:51 +01:00
|
|
|
struct ::iovec iovbuf[bufs.size()]; do
|
2018-12-18 22:41:45 +01:00
|
|
|
{
|
|
|
|
assert(opts.offset >= opts_.offset);
|
|
|
|
const size_t off(opts.offset - opts_.offset);
|
|
|
|
assert(off <= buffers::size(bufs));
|
|
|
|
assert(ret <= buffers::size(bufs));
|
2018-12-21 22:19:34 +01:00
|
|
|
const auto iov
|
|
|
|
{
|
|
|
|
make_iov({iovbuf, bufs.size()}, bufs, ret)
|
|
|
|
};
|
|
|
|
|
2019-04-21 06:24:26 +02:00
|
|
|
const size_t last
|
|
|
|
{
|
2022-05-04 21:14:00 +02:00
|
|
|
_read(fd, iov, opts)
|
2019-04-21 06:24:26 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if(!opts_.blocking && !last)
|
|
|
|
break;
|
|
|
|
|
|
|
|
ret += last;
|
2018-12-18 22:41:45 +01:00
|
|
|
if(!opts_.all)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if(off >= ret)
|
|
|
|
break;
|
|
|
|
|
|
|
|
opts.offset = opts_.offset + ret;
|
|
|
|
}
|
|
|
|
while(ret < buffers::size(bufs));
|
|
|
|
assert(opts.offset >= opts_.offset);
|
|
|
|
assert(ret <= buffers::size(bufs));
|
|
|
|
return ret;
|
|
|
|
}
|
2018-12-21 22:19:34 +01:00
|
|
|
#pragma GCC diagnostic pop
|
2018-12-18 22:41:45 +01:00
|
|
|
|
|
|
|
size_t
|
2022-05-04 21:14:00 +02:00
|
|
|
ircd::fs::_read(const fd &fd,
|
|
|
|
const const_iovec_view &iov,
|
|
|
|
const read_opts &opts)
|
2018-01-11 06:30:31 +01:00
|
|
|
{
|
2019-04-06 21:13:16 +02:00
|
|
|
assert(opts.op == op::READ);
|
|
|
|
|
2019-08-11 07:06:08 +02:00
|
|
|
#ifdef IRCD_USE_IOU
|
2019-10-10 10:32:42 +02:00
|
|
|
if(likely(iou::system && opts.aio))
|
2019-08-11 07:06:08 +02:00
|
|
|
return iou::read(fd, iov, opts);
|
|
|
|
#endif
|
|
|
|
|
2018-05-30 11:13:06 +02:00
|
|
|
#ifdef IRCD_USE_AIO
|
2019-10-10 10:32:42 +02:00
|
|
|
if(likely(aio::system && opts.aio))
|
2018-12-18 22:41:45 +01:00
|
|
|
return aio::read(fd, iov, opts);
|
2018-05-30 11:13:06 +02:00
|
|
|
#endif
|
2018-01-11 06:30:31 +01:00
|
|
|
|
2019-06-06 08:18:33 +02:00
|
|
|
#ifdef HAVE_PREADV2
|
2020-03-20 18:04:51 +01:00
|
|
|
return support::preadv2?
|
2019-06-06 08:18:33 +02:00
|
|
|
_read_preadv2(fd, iov, opts):
|
|
|
|
_read_preadv(fd, iov, opts);
|
|
|
|
#else
|
|
|
|
return _read_preadv(fd, iov, opts);
|
|
|
|
#endif
|
2019-03-14 03:33:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
2019-06-06 08:18:33 +02:00
|
|
|
ircd::fs::_read_preadv(const fd &fd,
|
|
|
|
const const_iovec_view &iov,
|
|
|
|
const read_opts &opts)
|
2019-03-14 03:33:58 +01:00
|
|
|
{
|
2019-04-21 03:23:52 +02:00
|
|
|
ssize_t ret; do
|
2019-03-14 03:33:58 +01:00
|
|
|
{
|
2019-06-06 08:18:33 +02:00
|
|
|
ret = ::preadv(int(fd), iov.data(), iov.size(), opts.offset);
|
2019-04-21 03:23:52 +02:00
|
|
|
}
|
|
|
|
while(!opts.interruptible && unlikely(ret == -1 && errno == EINTR));
|
|
|
|
|
|
|
|
static_assert(EAGAIN == EWOULDBLOCK);
|
2019-06-06 08:18:33 +02:00
|
|
|
if(unlikely(!opts.blocking && ret == -1 && errno == EAGAIN))
|
2019-04-21 03:23:52 +02:00
|
|
|
return 0UL;
|
|
|
|
|
|
|
|
if(unlikely(ret == -1))
|
|
|
|
throw std::system_error
|
|
|
|
{
|
|
|
|
errno, std::system_category()
|
|
|
|
};
|
2019-03-14 03:33:58 +01:00
|
|
|
|
2019-04-21 03:23:52 +02:00
|
|
|
return ret;
|
2019-03-14 03:33:58 +01:00
|
|
|
}
|
2019-06-06 08:18:33 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_PREADV2
|
2019-03-14 03:33:58 +01:00
|
|
|
size_t
|
2019-06-06 08:18:33 +02:00
|
|
|
ircd::fs::_read_preadv2(const fd &fd,
|
|
|
|
const const_iovec_view &iov,
|
|
|
|
const read_opts &opts)
|
2019-03-14 03:33:58 +01:00
|
|
|
{
|
2019-06-06 08:18:33 +02:00
|
|
|
const auto &flags_
|
|
|
|
{
|
|
|
|
flags(opts)
|
|
|
|
};
|
|
|
|
|
2019-04-21 03:23:52 +02:00
|
|
|
ssize_t ret; do
|
2018-12-18 23:17:38 +01:00
|
|
|
{
|
2019-06-06 08:18:33 +02:00
|
|
|
ret = ::preadv2(int(fd), iov.data(), iov.size(), opts.offset, flags_);
|
2019-04-21 03:23:52 +02:00
|
|
|
}
|
|
|
|
while(!opts.interruptible && unlikely(ret == -1 && errno == EINTR));
|
|
|
|
|
|
|
|
static_assert(EAGAIN == EWOULDBLOCK);
|
2019-06-06 08:18:33 +02:00
|
|
|
if(!opts.blocking && ret == -1 && errno == EAGAIN)
|
2019-04-21 03:23:52 +02:00
|
|
|
return 0UL;
|
2018-12-18 23:17:38 +01:00
|
|
|
|
2019-04-21 03:23:52 +02:00
|
|
|
if(unlikely(ret == -1))
|
|
|
|
throw std::system_error
|
|
|
|
{
|
|
|
|
errno, std::system_category()
|
|
|
|
};
|
|
|
|
|
|
|
|
return ret;
|
2018-05-30 11:13:06 +02:00
|
|
|
}
|
2019-06-06 08:18:33 +02:00
|
|
|
#endif HAVE_PREADV2
|
2018-01-11 06:30:31 +01:00
|
|
|
|
2019-03-16 00:06:51 +01:00
|
|
|
int
|
|
|
|
ircd::fs::flags(const read_opts &opts)
|
|
|
|
{
|
|
|
|
int ret{0};
|
|
|
|
|
|
|
|
#if defined(RWF_HIPRI)
|
2020-03-20 18:04:51 +01:00
|
|
|
if(support::hipri && reqprio(opts.priority) == reqprio(opts::highest_priority))
|
2019-03-16 00:06:51 +01:00
|
|
|
ret |= RWF_HIPRI;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(RWF_NOWAIT)
|
2020-03-20 18:04:51 +01:00
|
|
|
if(support::nowait && !opts.blocking)
|
2019-03-16 00:06:51 +01:00
|
|
|
ret |= RWF_NOWAIT;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-01-11 06:30:31 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// fs/write.h
|
|
|
|
//
|
|
|
|
|
2018-02-06 08:28:33 +01:00
|
|
|
ircd::fs::write_opts
|
|
|
|
const ircd::fs::write_opts_default
|
|
|
|
{};
|
2017-12-21 21:27:22 +01:00
|
|
|
|
2018-08-24 06:24:35 +02:00
|
|
|
void
|
|
|
|
ircd::fs::allocate(const fd &fd,
|
|
|
|
const size_t &size,
|
|
|
|
const write_opts &opts)
|
|
|
|
{
|
2019-04-06 21:13:16 +02:00
|
|
|
assert(opts.op == op::WRITE);
|
|
|
|
|
2018-08-24 07:35:01 +02:00
|
|
|
int mode{0};
|
2019-08-07 01:39:28 +02:00
|
|
|
|
|
|
|
#ifdef FALLOC_FL_KEEP_SIZE
|
2018-08-24 07:35:01 +02:00
|
|
|
mode |= opts.keep_size? FALLOC_FL_KEEP_SIZE : 0;
|
2019-08-07 01:39:28 +02:00
|
|
|
#else
|
|
|
|
if(opts.keep_size)
|
|
|
|
throw_system_error(std::errc::invalid_argument);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef FALLOC_FL_PUNCH_HOLE
|
|
|
|
mode |= opts.punch_hole? FALLOC_FL_PUNCH_HOLE : 0;
|
|
|
|
#else
|
|
|
|
if(opts.punch_hole)
|
|
|
|
throw_system_error(std::errc::invalid_argument);
|
|
|
|
#endif
|
|
|
|
|
2018-08-24 07:35:01 +02:00
|
|
|
syscall(::fallocate, fd, mode, opts.offset, size);
|
2018-08-24 06:24:35 +02:00
|
|
|
}
|
|
|
|
|
2018-08-24 06:24:19 +02:00
|
|
|
void
|
|
|
|
ircd::fs::truncate(const string_view &path,
|
|
|
|
const size_t &size,
|
|
|
|
const write_opts &opts)
|
|
|
|
{
|
2022-06-18 21:05:17 +02:00
|
|
|
const fs::fd fd
|
2018-08-24 06:24:19 +02:00
|
|
|
{
|
2022-06-18 21:05:17 +02:00
|
|
|
path, fd::opts
|
|
|
|
{
|
|
|
|
.mode = std::ios::out | std::ios::trunc,
|
|
|
|
},
|
2018-08-24 06:24:19 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return truncate(fd, size, opts);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::fs::truncate(const fd &fd,
|
|
|
|
const size_t &size,
|
|
|
|
const write_opts &opts)
|
|
|
|
{
|
2019-04-06 21:13:16 +02:00
|
|
|
assert(opts.op == op::WRITE);
|
2018-08-24 06:24:19 +02:00
|
|
|
syscall(::ftruncate, fd, size);
|
|
|
|
}
|
|
|
|
|
2018-08-24 06:23:42 +02:00
|
|
|
ircd::const_buffer
|
|
|
|
ircd::fs::overwrite(const string_view &path,
|
|
|
|
const const_buffer &buf,
|
|
|
|
const write_opts &opts)
|
|
|
|
{
|
2018-11-29 02:21:34 +01:00
|
|
|
const const_buffers bufs
|
2018-08-24 06:23:42 +02:00
|
|
|
{
|
2018-11-29 02:21:34 +01:00
|
|
|
&buf, 1
|
2018-08-24 06:23:42 +02:00
|
|
|
};
|
|
|
|
|
2018-11-29 02:21:34 +01:00
|
|
|
return const_buffer
|
|
|
|
{
|
|
|
|
data(buf), overwrite(path, bufs, opts)
|
|
|
|
};
|
2018-08-24 06:23:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::const_buffer
|
|
|
|
ircd::fs::overwrite(const fd &fd,
|
|
|
|
const const_buffer &buf,
|
|
|
|
const write_opts &opts)
|
|
|
|
{
|
2018-11-29 02:21:34 +01:00
|
|
|
const const_buffers bufs
|
|
|
|
{
|
|
|
|
&buf, 1
|
|
|
|
};
|
|
|
|
|
|
|
|
return const_buffer
|
|
|
|
{
|
|
|
|
data(buf), overwrite(fd, bufs, opts)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::fs::overwrite(const string_view &path,
|
|
|
|
const const_buffers &bufs,
|
|
|
|
const write_opts &opts)
|
|
|
|
{
|
2022-06-18 21:05:17 +02:00
|
|
|
const fs::fd fd
|
2018-11-29 02:21:34 +01:00
|
|
|
{
|
2022-06-18 21:05:17 +02:00
|
|
|
path, fd::opts
|
|
|
|
{
|
|
|
|
.mode = std::ios::out | std::ios::trunc,
|
|
|
|
},
|
2018-11-29 02:21:34 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
return overwrite(fd, bufs, opts);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::fs::overwrite(const fd &fd,
|
|
|
|
const const_buffers &bufs,
|
|
|
|
const write_opts &opts)
|
|
|
|
{
|
|
|
|
return write(fd, bufs, opts);
|
2018-08-24 06:23:42 +02:00
|
|
|
}
|
|
|
|
|
2019-04-18 02:49:01 +02:00
|
|
|
//
|
|
|
|
// append
|
|
|
|
//
|
|
|
|
|
2018-08-24 06:24:56 +02:00
|
|
|
ircd::const_buffer
|
|
|
|
ircd::fs::append(const string_view &path,
|
|
|
|
const const_buffer &buf,
|
|
|
|
const write_opts &opts)
|
|
|
|
{
|
2018-11-29 02:16:26 +01:00
|
|
|
const const_buffers bufs
|
2018-08-24 06:24:56 +02:00
|
|
|
{
|
2018-11-29 02:16:26 +01:00
|
|
|
&buf, 1
|
2018-08-24 06:24:56 +02:00
|
|
|
};
|
|
|
|
|
2018-11-29 02:16:26 +01:00
|
|
|
return const_buffer
|
|
|
|
{
|
|
|
|
data(buf), append(path, bufs, opts)
|
|
|
|
};
|
2018-08-24 06:24:56 +02:00
|
|
|
}
|
|
|
|
|
2018-05-30 11:40:20 +02:00
|
|
|
ircd::const_buffer
|
|
|
|
ircd::fs::append(const fd &fd,
|
|
|
|
const const_buffer &buf,
|
2018-11-29 02:16:26 +01:00
|
|
|
const write_opts &opts)
|
|
|
|
{
|
|
|
|
const const_buffers bufs
|
|
|
|
{
|
|
|
|
&buf, 1
|
|
|
|
};
|
|
|
|
|
|
|
|
return const_buffer
|
|
|
|
{
|
|
|
|
data(buf), append(fd, bufs, opts)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::fs::append(const string_view &path,
|
|
|
|
const const_buffers &bufs,
|
|
|
|
const write_opts &opts)
|
|
|
|
{
|
2022-06-18 21:05:17 +02:00
|
|
|
const fs::fd fd
|
2018-11-29 02:16:26 +01:00
|
|
|
{
|
2022-06-18 21:05:17 +02:00
|
|
|
path, fd::opts
|
|
|
|
{
|
|
|
|
.mode = std::ios::out | std::ios::app
|
|
|
|
},
|
2018-11-29 02:16:26 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
return append(fd, bufs, opts);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
2019-06-05 01:37:29 +02:00
|
|
|
ircd::fs::append(const fd &fd,
|
|
|
|
const const_buffers &bufs,
|
|
|
|
const write_opts &opts_)
|
2018-05-30 11:40:20 +02:00
|
|
|
{
|
|
|
|
auto opts(opts_);
|
2020-03-20 18:04:51 +01:00
|
|
|
if(support::pwritev2 && support::append)
|
2019-06-06 08:18:33 +02:00
|
|
|
opts.offset = -1;
|
|
|
|
else if(!opts.offset || opts.offset == -1)
|
2018-08-24 06:24:56 +02:00
|
|
|
opts.offset = syscall(::lseek, fd, 0, SEEK_END);
|
|
|
|
|
2018-11-29 02:16:26 +01:00
|
|
|
return write(fd, bufs, opts);
|
2018-05-30 11:40:20 +02:00
|
|
|
}
|
2019-04-18 02:49:01 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// write
|
|
|
|
//
|
2018-05-30 11:40:20 +02:00
|
|
|
|
2018-04-25 03:34:46 +02:00
|
|
|
ircd::const_buffer
|
2018-02-06 08:28:33 +01:00
|
|
|
ircd::fs::write(const string_view &path,
|
|
|
|
const const_buffer &buf,
|
|
|
|
const write_opts &opts)
|
2018-11-29 02:10:48 +01:00
|
|
|
{
|
|
|
|
const const_buffers bufs
|
|
|
|
{
|
|
|
|
&buf, 1
|
|
|
|
};
|
|
|
|
|
|
|
|
return const_buffer
|
|
|
|
{
|
|
|
|
data(buf), write(path, bufs, opts)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::const_buffer
|
|
|
|
ircd::fs::write(const fd &fd,
|
|
|
|
const const_buffer &buf,
|
|
|
|
const write_opts &opts)
|
|
|
|
{
|
|
|
|
const const_buffers bufs
|
|
|
|
{
|
|
|
|
&buf, 1
|
|
|
|
};
|
|
|
|
|
|
|
|
return const_buffer
|
|
|
|
{
|
|
|
|
data(buf), write(fd, bufs, opts)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::fs::write(const string_view &path,
|
|
|
|
const const_buffers &bufs,
|
|
|
|
const write_opts &opts)
|
2017-10-02 11:58:53 +02:00
|
|
|
{
|
2022-06-18 21:05:17 +02:00
|
|
|
const fs::fd fd
|
2018-05-30 11:24:35 +02:00
|
|
|
{
|
2022-06-18 21:05:17 +02:00
|
|
|
path, fd::opts
|
|
|
|
{
|
|
|
|
.mode = std::ios::out
|
|
|
|
},
|
2018-05-30 11:24:35 +02:00
|
|
|
};
|
2018-02-06 08:28:33 +01:00
|
|
|
|
2018-11-29 02:10:48 +01:00
|
|
|
return write(fd, bufs, opts);
|
2018-05-30 11:24:35 +02:00
|
|
|
}
|
2017-10-02 11:58:53 +02:00
|
|
|
|
2019-03-16 00:06:51 +01:00
|
|
|
namespace ircd::fs
|
|
|
|
{
|
|
|
|
static int flags(const write_opts &opts);
|
2019-06-06 08:18:33 +02:00
|
|
|
static size_t _write_pwritev2(const fd &, const const_iovec_view &, const write_opts &);
|
|
|
|
static size_t _write_pwritev(const fd &, const const_iovec_view &, const write_opts &);
|
2022-05-04 21:14:00 +02:00
|
|
|
static size_t _write(const fd &, const const_iovec_view &, const write_opts &);
|
2019-03-16 00:06:51 +01:00
|
|
|
}
|
|
|
|
|
2018-12-21 22:19:34 +01:00
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wstack-usage="
|
2018-11-29 02:10:48 +01:00
|
|
|
size_t
|
2018-12-21 22:19:34 +01:00
|
|
|
__attribute__((stack_protect))
|
2018-05-30 11:24:35 +02:00
|
|
|
ircd::fs::write(const fd &fd,
|
2018-11-29 02:10:48 +01:00
|
|
|
const const_buffers &bufs,
|
2018-12-18 23:01:05 +01:00
|
|
|
const write_opts &opts_)
|
2017-10-02 11:58:53 +02:00
|
|
|
{
|
2018-12-21 22:19:34 +01:00
|
|
|
if(unlikely(bufs.size() > info::iov_max))
|
|
|
|
throw error
|
|
|
|
{
|
|
|
|
make_error_code(std::errc::invalid_argument),
|
|
|
|
"Buffer count of %zu exceeds IOV_MAX of %zu",
|
|
|
|
bufs.size(),
|
|
|
|
info::iov_max
|
|
|
|
};
|
|
|
|
|
2019-03-14 03:33:58 +01:00
|
|
|
size_t off(0);
|
2018-12-18 23:01:05 +01:00
|
|
|
write_opts opts(opts_);
|
2018-12-21 22:19:34 +01:00
|
|
|
assert(bufs.size() <= info::iov_max);
|
2018-12-21 19:28:51 +01:00
|
|
|
struct ::iovec iovbuf[bufs.size()]; do
|
2018-12-18 23:01:05 +01:00
|
|
|
{
|
2018-12-21 22:19:34 +01:00
|
|
|
const auto iov
|
|
|
|
{
|
|
|
|
make_iov({iovbuf, bufs.size()}, bufs, off)
|
|
|
|
};
|
|
|
|
|
2019-04-21 06:24:26 +02:00
|
|
|
const size_t last
|
|
|
|
{
|
2022-05-04 21:14:00 +02:00
|
|
|
_write(fd, iov, opts)
|
2019-04-21 06:24:26 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
opts.offset += last;
|
2018-12-18 23:01:05 +01:00
|
|
|
assert(opts.offset >= opts_.offset);
|
|
|
|
off = opts.offset - opts_.offset;
|
2019-04-21 06:24:26 +02:00
|
|
|
if(!opts.blocking && !last)
|
|
|
|
break;
|
2018-12-18 23:01:05 +01:00
|
|
|
}
|
2019-03-14 03:33:58 +01:00
|
|
|
while(opts.all && opts_.offset >= 0 && off < buffers::size(bufs));
|
2018-12-18 23:01:05 +01:00
|
|
|
assert(opts.offset >= opts_.offset);
|
|
|
|
assert(ssize_t(off) == opts.offset - opts_.offset);
|
2019-04-21 03:23:52 +02:00
|
|
|
assert(!opts.all || !opts.blocking || off == buffers::size(bufs));
|
2018-12-18 23:01:05 +01:00
|
|
|
return off;
|
|
|
|
}
|
2018-12-21 22:19:34 +01:00
|
|
|
#pragma GCC diagnostic pop
|
2018-12-18 22:40:46 +01:00
|
|
|
|
2018-12-18 23:01:05 +01:00
|
|
|
size_t
|
2022-05-04 21:14:00 +02:00
|
|
|
ircd::fs::_write(const fd &fd,
|
|
|
|
const const_iovec_view &iov,
|
|
|
|
const write_opts &opts)
|
2018-12-18 23:01:05 +01:00
|
|
|
{
|
2019-04-06 21:13:16 +02:00
|
|
|
assert(opts.op == op::WRITE);
|
|
|
|
|
2019-08-11 07:06:08 +02:00
|
|
|
#ifdef IRCD_USE_IOU
|
2019-10-10 10:32:42 +02:00
|
|
|
if(likely(iou::system && opts.aio))
|
2019-08-11 07:06:08 +02:00
|
|
|
return iou::write(fd, iov, opts);
|
|
|
|
#endif
|
|
|
|
|
2018-05-30 11:24:35 +02:00
|
|
|
#ifdef IRCD_USE_AIO
|
2019-10-10 10:32:42 +02:00
|
|
|
if(likely(aio::system && opts.aio))
|
2018-12-18 22:40:46 +01:00
|
|
|
return aio::write(fd, iov, opts);
|
2019-06-06 08:18:33 +02:00
|
|
|
#endif
|
2018-02-06 08:28:33 +01:00
|
|
|
|
2019-06-06 08:18:33 +02:00
|
|
|
#ifdef HAVE_PWRITEV2
|
2020-03-20 18:04:51 +01:00
|
|
|
return support::pwritev2?
|
2019-06-06 08:18:33 +02:00
|
|
|
_write_pwritev2(fd, iov, opts):
|
|
|
|
_write_pwritev(fd, iov, opts);
|
|
|
|
#else
|
|
|
|
return _write_pwritev(fd, iov, opts);
|
|
|
|
#endif
|
2019-03-14 02:42:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
2019-06-06 08:18:33 +02:00
|
|
|
ircd::fs::_write_pwritev(const fd &fd,
|
|
|
|
const const_iovec_view &iov,
|
|
|
|
const write_opts &opts)
|
2019-03-14 02:42:16 +01:00
|
|
|
{
|
2019-04-21 03:23:52 +02:00
|
|
|
ssize_t ret; do
|
|
|
|
{
|
2019-06-06 08:18:33 +02:00
|
|
|
ret = ::pwritev(int(fd), iov.data(), iov.size(), opts.offset);
|
2019-04-21 03:23:52 +02:00
|
|
|
}
|
|
|
|
while(!opts.interruptible && unlikely(ret == -1 && errno == EINTR));
|
|
|
|
|
|
|
|
static_assert(EAGAIN == EWOULDBLOCK);
|
2019-06-06 08:18:33 +02:00
|
|
|
if(unlikely(!opts.blocking && ret == -1 && errno == EAGAIN))
|
2019-04-21 03:23:52 +02:00
|
|
|
return 0UL;
|
|
|
|
|
|
|
|
if(unlikely(ret == -1))
|
|
|
|
throw std::system_error
|
|
|
|
{
|
|
|
|
errno, std::system_category()
|
|
|
|
};
|
|
|
|
|
|
|
|
return ret;
|
2019-03-14 02:42:16 +01:00
|
|
|
}
|
2019-06-06 08:18:33 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_PWRITEV2
|
2019-03-14 02:42:16 +01:00
|
|
|
size_t
|
2019-06-06 08:18:33 +02:00
|
|
|
ircd::fs::_write_pwritev2(const fd &fd,
|
|
|
|
const const_iovec_view &iov,
|
|
|
|
const write_opts &opts)
|
2019-03-14 02:42:16 +01:00
|
|
|
{
|
2019-06-06 08:18:33 +02:00
|
|
|
// Manpages sez that when appending with RWF_APPEND, the offset has no
|
|
|
|
// effect on the write; but if the value of the offset is -1 then the
|
|
|
|
// fd's offset is updated, otherwise it is not.
|
|
|
|
const off_t &offset
|
|
|
|
{
|
|
|
|
opts.offset == -1 && !opts.update_offset? 0 : opts.offset
|
|
|
|
};
|
|
|
|
|
2019-06-05 01:37:29 +02:00
|
|
|
ssize_t ret; do
|
2019-04-18 03:12:43 +02:00
|
|
|
{
|
2019-08-16 08:35:19 +02:00
|
|
|
ret = ::pwritev2(int(fd), iov.data(), iov.size(), opts.offset, flags(opts));
|
2019-06-05 01:37:29 +02:00
|
|
|
}
|
|
|
|
while(!opts.interruptible && unlikely(ret == -1 && errno == EINTR));
|
|
|
|
|
|
|
|
static_assert(EAGAIN == EWOULDBLOCK);
|
2019-06-06 08:18:33 +02:00
|
|
|
if(!opts.blocking && ret == -1 && errno == EAGAIN)
|
2019-06-05 01:37:29 +02:00
|
|
|
return 0UL;
|
|
|
|
|
|
|
|
if(unlikely(ret == -1))
|
|
|
|
throw std::system_error
|
|
|
|
{
|
|
|
|
errno, std::system_category()
|
|
|
|
};
|
|
|
|
|
|
|
|
return ret;
|
2018-05-30 11:24:35 +02:00
|
|
|
}
|
2019-06-06 08:18:33 +02:00
|
|
|
#endif HAVE_PWRITEV2
|
2017-10-02 11:58:53 +02:00
|
|
|
|
2019-03-16 00:06:51 +01:00
|
|
|
int
|
|
|
|
ircd::fs::flags(const write_opts &opts)
|
|
|
|
{
|
|
|
|
int ret{0};
|
|
|
|
|
|
|
|
#if defined(RWF_APPEND)
|
2020-03-20 18:04:51 +01:00
|
|
|
assert(opts.offset >= 0 || support::append);
|
|
|
|
if(support::append && opts.offset == -1)
|
2019-03-16 00:06:51 +01:00
|
|
|
ret |= RWF_APPEND;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(RWF_HIPRI)
|
2020-03-20 18:04:51 +01:00
|
|
|
if(support::hipri && reqprio(opts.priority) == reqprio(opts::highest_priority))
|
2019-03-16 00:06:51 +01:00
|
|
|
ret |= RWF_HIPRI;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(RWF_NOWAIT)
|
2020-03-20 18:04:51 +01:00
|
|
|
if(support::nowait && !opts.blocking)
|
2019-03-16 00:06:51 +01:00
|
|
|
ret |= RWF_NOWAIT;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(RWF_DSYNC)
|
2020-03-20 18:04:51 +01:00
|
|
|
if(support::dsync && opts.sync && !opts.metadata)
|
2019-03-16 00:06:51 +01:00
|
|
|
ret |= RWF_DSYNC;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(RWF_SYNC)
|
2020-03-20 18:04:51 +01:00
|
|
|
if(support::sync && opts.sync && opts.metadata)
|
2019-03-16 00:06:51 +01:00
|
|
|
ret |= RWF_SYNC;
|
|
|
|
#endif
|
|
|
|
|
2019-05-06 02:48:02 +02:00
|
|
|
#ifdef RWF_WRITE_LIFE_SHIFT
|
2020-03-20 18:04:51 +01:00
|
|
|
if(support::rwf_write_life && opts.write_life)
|
2019-05-06 02:48:02 +02:00
|
|
|
ret |= (opts.write_life << (RWF_WRITE_LIFE_SHIFT));
|
|
|
|
#endif
|
|
|
|
|
2019-03-16 00:06:51 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-04-08 02:29:23 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// fs/wait.h
|
|
|
|
//
|
|
|
|
|
|
|
|
namespace ircd::fs
|
|
|
|
{
|
2019-08-06 01:15:56 +02:00
|
|
|
static constexpr asio::posix::stream_descriptor::wait_type translate(const ready &) noexcept
|
|
|
|
__attribute__((const));
|
2019-04-08 02:29:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
decltype(ircd::fs::wait_opts_default)
|
|
|
|
ircd::fs::wait_opts_default;
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::fs::wait(const fd &fd,
|
|
|
|
const wait_opts &opts)
|
|
|
|
{
|
|
|
|
assert(opts.op == op::WAIT);
|
|
|
|
|
|
|
|
const auto &wait_type
|
|
|
|
{
|
|
|
|
translate(opts.ready)
|
|
|
|
};
|
|
|
|
|
|
|
|
boost::asio::posix::stream_descriptor sd
|
|
|
|
{
|
|
|
|
ios::get(), int(fd)
|
|
|
|
};
|
|
|
|
|
|
|
|
const unwind release{[&sd]
|
|
|
|
{
|
|
|
|
sd.release();
|
|
|
|
}};
|
|
|
|
|
|
|
|
const auto interruption{[&sd]
|
|
|
|
(ctx::ctx *const &interruptor)
|
|
|
|
{
|
|
|
|
sd.cancel();
|
|
|
|
}};
|
|
|
|
|
|
|
|
boost::system::error_code ec; continuation
|
|
|
|
{
|
|
|
|
continuation::asio_predicate, interruption, [&wait_type, &sd, &ec]
|
|
|
|
(auto &yield)
|
|
|
|
{
|
|
|
|
sd.async_wait(wait_type, yield[ec]);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if(unlikely(ec))
|
|
|
|
throw_system_error(ec);
|
|
|
|
}
|
|
|
|
|
2019-08-06 01:15:56 +02:00
|
|
|
constexpr
|
2019-04-08 02:29:23 +02:00
|
|
|
boost::asio::posix::stream_descriptor::wait_type
|
|
|
|
ircd::fs::translate(const ready &ready)
|
2019-08-06 01:15:56 +02:00
|
|
|
noexcept
|
2019-04-08 02:29:23 +02:00
|
|
|
{
|
|
|
|
using wait_type = boost::asio::posix::stream_descriptor::wait_type;
|
|
|
|
|
|
|
|
switch(ready)
|
|
|
|
{
|
|
|
|
case ready::ANY:
|
|
|
|
return wait_type::wait_read | wait_type::wait_write | wait_type::wait_error;
|
|
|
|
|
|
|
|
case ready::READ:
|
|
|
|
return wait_type::wait_read;
|
|
|
|
|
|
|
|
case ready::WRITE:
|
|
|
|
return wait_type::wait_write;
|
|
|
|
|
|
|
|
case ready::ERROR:
|
|
|
|
default:
|
|
|
|
return wait_type::wait_error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::fs::reflect(const ready &ready)
|
|
|
|
{
|
|
|
|
switch(ready)
|
|
|
|
{
|
|
|
|
case ready::ANY: return "ANY";
|
|
|
|
case ready::READ: return "READ";
|
|
|
|
case ready::WRITE: return "WRITE";
|
|
|
|
case ready::ERROR: return "ERROR";
|
|
|
|
}
|
|
|
|
|
|
|
|
return "?????";
|
|
|
|
}
|
|
|
|
|
2018-05-30 10:38:20 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2018-11-28 03:09:12 +01:00
|
|
|
// fs/aio.h
|
|
|
|
//
|
|
|
|
|
2020-06-07 09:56:43 +02:00
|
|
|
decltype(ircd::fs::aio::MAX_EVENTS)
|
|
|
|
ircd::fs::aio::MAX_EVENTS
|
|
|
|
{
|
|
|
|
info::aio_max
|
|
|
|
};
|
|
|
|
|
2019-03-14 02:50:57 +01:00
|
|
|
decltype(ircd::fs::aio::MAX_REQPRIO)
|
|
|
|
ircd::fs::aio::MAX_REQPRIO
|
|
|
|
{
|
2019-08-11 07:06:08 +02:00
|
|
|
info::aio_reqprio_max
|
2019-03-14 02:50:57 +01:00
|
|
|
};
|
|
|
|
|
2019-08-11 07:06:08 +02:00
|
|
|
/// Conf item to control whether aio is enabled or bypassed.
|
2018-12-09 00:17:13 +01:00
|
|
|
decltype(ircd::fs::aio::enable)
|
|
|
|
ircd::fs::aio::enable
|
|
|
|
{
|
|
|
|
{ "name", "ircd.fs.aio.enable" },
|
|
|
|
{ "default", true },
|
2018-12-09 01:15:22 +01:00
|
|
|
{ "persist", false },
|
2018-12-09 00:17:13 +01:00
|
|
|
};
|
|
|
|
|
2018-12-02 01:06:33 +01:00
|
|
|
/// Global stats structure
|
|
|
|
decltype(ircd::fs::aio::stats)
|
|
|
|
ircd::fs::aio::stats;
|
|
|
|
|
2018-11-28 03:09:12 +01:00
|
|
|
/// Non-null when aio is available for use
|
2018-12-29 00:02:02 +01:00
|
|
|
decltype(ircd::fs::aio::system)
|
|
|
|
ircd::fs::aio::system;
|
2018-11-28 03:09:12 +01:00
|
|
|
|
|
|
|
//
|
|
|
|
// init
|
|
|
|
//
|
|
|
|
|
2019-10-06 03:04:13 +02:00
|
|
|
#ifndef IRCD_USE_AIO
|
2018-11-28 03:09:12 +01:00
|
|
|
ircd::fs::aio::init::init()
|
|
|
|
{
|
2019-08-11 04:16:34 +02:00
|
|
|
assert(!system);
|
2018-11-28 03:09:12 +01:00
|
|
|
}
|
2019-10-06 03:04:13 +02:00
|
|
|
#endif
|
2018-11-28 03:09:12 +01:00
|
|
|
|
2019-10-06 03:04:13 +02:00
|
|
|
#ifndef IRCD_USE_AIO
|
2022-05-26 21:00:07 +02:00
|
|
|
[[using gnu: weak, cold]]
|
2018-11-28 03:09:12 +01:00
|
|
|
ircd::fs::aio::init::~init()
|
|
|
|
noexcept
|
|
|
|
{
|
2019-08-11 04:16:34 +02:00
|
|
|
assert(!system);
|
2018-11-28 03:09:12 +01:00
|
|
|
}
|
2019-10-06 03:04:13 +02:00
|
|
|
#endif
|
2019-08-11 07:06:08 +02:00
|
|
|
|
2020-12-18 02:22:28 +01:00
|
|
|
//
|
|
|
|
// stats
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::fs::aio::stats::stats()
|
|
|
|
:value{0}
|
|
|
|
,items{0}
|
|
|
|
,requests
|
|
|
|
{
|
|
|
|
value + items++,
|
|
|
|
{
|
|
|
|
{ "name", "ircd.fs.aio.requests" },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
,complete
|
|
|
|
{
|
|
|
|
value + items++,
|
|
|
|
{
|
|
|
|
{ "name", "ircd.fs.aio.complete" },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
,submits
|
|
|
|
{
|
|
|
|
value + items++,
|
|
|
|
{
|
|
|
|
{ "name", "ircd.fs.aio.submits" },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
,chases
|
|
|
|
{
|
|
|
|
value + items++,
|
|
|
|
{
|
|
|
|
{ "name", "ircd.fs.aio.chases" },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
,handles
|
|
|
|
{
|
|
|
|
value + items++,
|
|
|
|
{
|
|
|
|
{ "name", "ircd.fs.aio.handles" },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
,events
|
|
|
|
{
|
|
|
|
value + items++,
|
|
|
|
{
|
|
|
|
{ "name", "ircd.fs.aio.events" },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
,cancel
|
|
|
|
{
|
|
|
|
value + items++,
|
|
|
|
{
|
|
|
|
{ "name", "ircd.fs.aio.cancel" },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
,errors
|
|
|
|
{
|
|
|
|
value + items++,
|
|
|
|
{
|
|
|
|
{ "name", "ircd.fs.aio.errors" },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
,reads
|
|
|
|
{
|
|
|
|
value + items++,
|
|
|
|
{
|
|
|
|
{ "name", "ircd.fs.aio.reads" },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
,writes
|
|
|
|
{
|
|
|
|
value + items++,
|
|
|
|
{
|
|
|
|
{ "name", "ircd.fs.aio.writes" },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
,stalls
|
|
|
|
{
|
|
|
|
value + items++,
|
|
|
|
{
|
|
|
|
{ "name", "ircd.fs.aio.stalls" },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
,bytes_requests
|
|
|
|
{
|
|
|
|
value + items++,
|
|
|
|
{
|
|
|
|
{ "name", "ircd.fs.aio.bytes.requests" },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
,bytes_complete
|
|
|
|
{
|
|
|
|
value + items++,
|
|
|
|
{
|
|
|
|
{ "name", "ircd.fs.aio.bytes.complete" },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
,bytes_errors
|
|
|
|
{
|
|
|
|
value + items++,
|
|
|
|
{
|
|
|
|
{ "name", "ircd.fs.aio.bytes.errors" },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
,bytes_cancel
|
|
|
|
{
|
|
|
|
value + items++,
|
|
|
|
{
|
|
|
|
{ "name", "ircd.fs.aio.bytes.cancel" },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
,bytes_read
|
|
|
|
{
|
|
|
|
value + items++,
|
|
|
|
{
|
|
|
|
{ "name", "ircd.fs.aio.bytes.read" },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
,bytes_write
|
|
|
|
{
|
|
|
|
value + items++,
|
|
|
|
{
|
|
|
|
{ "name", "ircd.fs.aio.bytes.write" },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
,cur_bytes_write
|
|
|
|
{
|
|
|
|
value + items++,
|
|
|
|
{
|
|
|
|
{ "name", "ircd.fs.aio.cur.bytes.write" },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
,cur_reads
|
|
|
|
{
|
|
|
|
value + items++,
|
|
|
|
{
|
|
|
|
{ "name", "ircd.fs.aio.cur.reads" },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
,cur_writes
|
|
|
|
{
|
|
|
|
value + items++,
|
|
|
|
{
|
|
|
|
{ "name", "ircd.fs.aio.cur.writes" },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
,cur_queued
|
|
|
|
{
|
|
|
|
value + items++,
|
|
|
|
{
|
|
|
|
{ "name", "ircd.fs.aio.cur.queued" },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
,cur_submits
|
|
|
|
{
|
|
|
|
value + items++,
|
|
|
|
{
|
|
|
|
{ "name", "ircd.fs.aio.cur.submits" },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
,max_requests
|
|
|
|
{
|
|
|
|
value + items++,
|
|
|
|
{
|
|
|
|
{ "name", "ircd.fs.aio.max.requests" },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
,max_reads
|
|
|
|
{
|
|
|
|
value + items++,
|
|
|
|
{
|
|
|
|
{ "name", "ircd.fs.aio.max.reads" },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
,max_writes
|
|
|
|
{
|
|
|
|
value + items++,
|
|
|
|
{
|
|
|
|
{ "name", "ircd.fs.aio.max.writes" },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
,max_queued
|
|
|
|
{
|
|
|
|
value + items++,
|
|
|
|
{
|
|
|
|
{ "name", "ircd.fs.aio.max.queued" },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
,max_submits
|
|
|
|
{
|
|
|
|
value + items++,
|
|
|
|
{
|
|
|
|
{ "name", "ircd.fs.aio.max.submits" },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
assert(items <= (sizeof(value) / sizeof(value[0])));
|
|
|
|
}
|
|
|
|
|
2019-08-11 07:06:08 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// fs/iou.h
|
|
|
|
//
|
|
|
|
|
|
|
|
decltype(ircd::fs::iou::support)
|
|
|
|
ircd::fs::iou::support
|
|
|
|
{
|
|
|
|
#ifdef IRCD_USE_IOU
|
|
|
|
info::kernel_version[0] > 5 ||
|
|
|
|
(info::kernel_version[0] >= 5 && info::kernel_version[1] >= 1)
|
|
|
|
#else
|
|
|
|
false
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Conf item to control whether iou is enabled or bypassed.
|
|
|
|
decltype(ircd::fs::iou::enable)
|
|
|
|
ircd::fs::iou::enable
|
|
|
|
{
|
|
|
|
{ "name", "ircd.fs.iou.enable" },
|
|
|
|
{ "default", false },
|
|
|
|
{ "persist", false },
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Global stats structure
|
|
|
|
decltype(ircd::fs::iou::stats)
|
2020-12-18 02:22:28 +01:00
|
|
|
ircd::fs::iou::stats
|
|
|
|
{
|
|
|
|
fs::aio::stats
|
|
|
|
};
|
2019-08-11 07:06:08 +02:00
|
|
|
|
|
|
|
/// Non-null when iou is available for use
|
|
|
|
decltype(ircd::fs::iou::system)
|
|
|
|
ircd::fs::iou::system;
|
|
|
|
|
|
|
|
//
|
|
|
|
// init
|
|
|
|
//
|
|
|
|
|
2019-10-06 03:04:13 +02:00
|
|
|
#ifndef IRCD_USE_IOU
|
|
|
|
[[gnu::weak]]
|
2019-08-11 07:06:08 +02:00
|
|
|
ircd::fs::iou::init::init()
|
|
|
|
{
|
|
|
|
assert(!system);
|
|
|
|
}
|
2019-10-06 03:04:13 +02:00
|
|
|
#endif
|
2019-08-11 07:06:08 +02:00
|
|
|
|
2019-10-06 03:04:13 +02:00
|
|
|
#ifndef IRCD_USE_IOU
|
|
|
|
[[gnu::weak]]
|
2019-08-11 07:06:08 +02:00
|
|
|
ircd::fs::iou::init::~init()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
assert(!system);
|
|
|
|
}
|
2019-10-06 03:04:13 +02:00
|
|
|
#endif
|
2018-11-28 03:09:12 +01:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2020-08-24 17:49:41 +02:00
|
|
|
//
|
|
|
|
// fs/map.h
|
|
|
|
//
|
|
|
|
|
|
|
|
namespace ircd::fs
|
|
|
|
{
|
|
|
|
static uint flags(const map::opts &);
|
|
|
|
static uint prot(const map::opts &);
|
|
|
|
}
|
|
|
|
|
2022-01-26 20:24:06 +01:00
|
|
|
size_t
|
|
|
|
ircd::fs::sync(const map &map,
|
|
|
|
const size_t &len,
|
|
|
|
const opts &opts)
|
|
|
|
{
|
|
|
|
const bool invalidate
|
|
|
|
{
|
|
|
|
false // XXX
|
|
|
|
};
|
|
|
|
|
|
|
|
const size_t offset
|
|
|
|
{
|
2022-02-24 23:35:58 +01:00
|
|
|
align(opts.offset, info::page_size)
|
2022-01-26 20:24:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const mutable_buffer buf
|
|
|
|
{
|
|
|
|
map + offset, len
|
|
|
|
};
|
|
|
|
|
|
|
|
return allocator::sync(buf, invalidate);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::fs::flush(const map &map,
|
|
|
|
const size_t &len,
|
|
|
|
const opts &opts)
|
|
|
|
{
|
|
|
|
const bool invalidate
|
|
|
|
{
|
|
|
|
false // XXX
|
|
|
|
};
|
|
|
|
|
|
|
|
const size_t offset
|
|
|
|
{
|
2022-02-24 23:35:58 +01:00
|
|
|
align(opts.offset, info::page_size)
|
2022-01-26 20:24:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const mutable_buffer buf
|
|
|
|
{
|
|
|
|
map + offset, len
|
|
|
|
};
|
|
|
|
|
|
|
|
return allocator::flush(buf, invalidate);
|
|
|
|
}
|
|
|
|
|
2020-09-07 20:08:54 +02:00
|
|
|
size_t
|
|
|
|
ircd::fs::evict(const map &map,
|
|
|
|
const size_t &len,
|
|
|
|
const opts &opts)
|
|
|
|
{
|
2021-05-02 22:21:43 +02:00
|
|
|
const size_t offset
|
|
|
|
{
|
2022-02-24 23:35:58 +01:00
|
|
|
align(opts.offset, info::page_size)
|
2021-05-02 22:21:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const mutable_buffer buf
|
|
|
|
{
|
|
|
|
map + offset, len
|
|
|
|
};
|
|
|
|
|
|
|
|
return allocator::evict(buf);
|
2020-09-07 20:08:54 +02:00
|
|
|
}
|
|
|
|
|
2020-09-13 09:24:04 +02:00
|
|
|
size_t
|
|
|
|
ircd::fs::prefetch(const map &map,
|
|
|
|
const size_t &len,
|
|
|
|
const opts &opts)
|
2020-09-07 20:08:54 +02:00
|
|
|
{
|
|
|
|
const size_t offset
|
|
|
|
{
|
2022-02-24 23:35:58 +01:00
|
|
|
align(opts.offset, info::page_size)
|
2020-09-07 20:08:54 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const mutable_buffer buf
|
|
|
|
{
|
|
|
|
map + offset, len
|
|
|
|
};
|
|
|
|
|
2021-05-02 22:21:43 +02:00
|
|
|
return allocator::prefetch(buf);
|
2020-09-07 20:08:54 +02:00
|
|
|
}
|
2021-05-02 22:21:43 +02:00
|
|
|
|
2020-09-07 20:08:54 +02:00
|
|
|
size_t
|
|
|
|
ircd::fs::advise(const map &map,
|
|
|
|
const int &advice,
|
|
|
|
const size_t &len,
|
|
|
|
const opts &opts)
|
|
|
|
{
|
|
|
|
const mutable_buffer buf
|
|
|
|
{
|
|
|
|
map + opts.offset, len
|
|
|
|
};
|
|
|
|
|
2021-05-02 22:21:43 +02:00
|
|
|
return allocator::advise(buf, advice);
|
2020-09-07 20:08:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// map::map
|
|
|
|
//
|
|
|
|
|
2021-03-10 07:05:43 +01:00
|
|
|
decltype(ircd::fs::map::default_opts)
|
|
|
|
ircd::fs::map::default_opts;
|
|
|
|
|
2020-08-24 17:49:41 +02:00
|
|
|
ircd::fs::map::map(const fd &fd,
|
2022-06-18 23:31:37 +02:00
|
|
|
const size_t size,
|
|
|
|
const opts &opts)
|
2022-02-24 02:10:20 +01:00
|
|
|
#if defined(HAVE_MMAP)
|
2020-08-24 17:49:41 +02:00
|
|
|
{
|
2022-03-05 21:22:20 +01:00
|
|
|
assert(size || int(fd) > -1);
|
2020-08-24 17:49:41 +02:00
|
|
|
const auto map_size
|
|
|
|
{
|
|
|
|
size?: fs::size(fd)
|
|
|
|
};
|
|
|
|
|
2022-02-24 02:10:20 +01:00
|
|
|
void *ptr
|
2020-08-24 17:49:41 +02:00
|
|
|
{
|
2022-02-24 02:10:20 +01:00
|
|
|
::mmap
|
|
|
|
(
|
|
|
|
nullptr,
|
|
|
|
map_size,
|
|
|
|
prot(opts),
|
2022-03-05 21:22:20 +01:00
|
|
|
flags(opts) | (int(fd) == -1? MAP_ANONYMOUS : 0),
|
2022-02-24 02:10:20 +01:00
|
|
|
int(fd),
|
|
|
|
opts.offset
|
|
|
|
)
|
2020-08-24 17:49:41 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if(unlikely(ptr == MAP_FAILED))
|
|
|
|
throw_system_error(errno);
|
|
|
|
|
2022-02-24 02:10:20 +01:00
|
|
|
#if defined(HAVE_MREMAP)
|
|
|
|
if(opts.alignment && !aligned(ptr, opts.alignment))
|
|
|
|
{
|
|
|
|
assert(opts.alignment > 1);
|
|
|
|
assert(opts.alignment > info::page_size);
|
|
|
|
ptr = ::mremap
|
|
|
|
(
|
|
|
|
ptr,
|
|
|
|
map_size,
|
|
|
|
map_size,
|
|
|
|
MREMAP_FIXED | MREMAP_MAYMOVE,
|
|
|
|
align_up(ptr, opts.alignment) + pad_to(map_size, opts.alignment)
|
|
|
|
);
|
|
|
|
|
|
|
|
if(unlikely(ptr == MAP_FAILED))
|
|
|
|
throw_system_error(errno);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
assert(aligned(ptr, opts.alignment));
|
|
|
|
assert(padded(map_size, opts.alignment));
|
|
|
|
static_cast<mutable_buffer &>(*this) =
|
2020-08-24 17:49:41 +02:00
|
|
|
{
|
|
|
|
reinterpret_cast<char *>(ptr),
|
|
|
|
map_size
|
|
|
|
};
|
2020-09-07 20:08:54 +02:00
|
|
|
|
|
|
|
const int advise
|
|
|
|
{
|
|
|
|
#if defined(HAVE_POSIX_MADVISE)
|
|
|
|
opts.random? POSIX_MADV_RANDOM:
|
|
|
|
opts.sequential? POSIX_MADV_SEQUENTIAL:
|
|
|
|
opts.dontneed? POSIX_MADV_DONTNEED:
|
|
|
|
#endif
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
|
|
|
if(advise)
|
|
|
|
fs::advise(*this, advise, map_size);
|
2020-08-24 17:49:41 +02:00
|
|
|
}
|
2022-02-24 02:10:20 +01:00
|
|
|
#else
|
|
|
|
#error "Missing mmap(2) on this platform."
|
|
|
|
#endif
|
2020-08-24 17:49:41 +02:00
|
|
|
|
|
|
|
ircd::fs::map::~map()
|
|
|
|
noexcept try
|
|
|
|
{
|
|
|
|
if(mutable_buffer::null())
|
|
|
|
return;
|
|
|
|
|
|
|
|
syscall(::munmap, data(*this), size(*this));
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::critical
|
|
|
|
{
|
|
|
|
log, "munmap(%p, %zu) :%s",
|
|
|
|
data(static_cast<mutable_buffer &>(*this)),
|
|
|
|
size(static_cast<mutable_buffer &>(*this)),
|
|
|
|
e.what(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::fs::map &
|
|
|
|
ircd::fs::map::operator=(map &&other)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
auto &ours
|
|
|
|
{
|
|
|
|
static_cast<mutable_buffer &>(*this)
|
|
|
|
};
|
|
|
|
|
|
|
|
auto &theirs
|
|
|
|
{
|
|
|
|
static_cast<mutable_buffer &>(other)
|
|
|
|
};
|
|
|
|
|
|
|
|
this->~map();
|
|
|
|
ours = theirs;
|
|
|
|
theirs = {};
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-09-07 20:08:54 +02:00
|
|
|
//
|
|
|
|
// util
|
|
|
|
//
|
|
|
|
|
2020-08-24 17:49:41 +02:00
|
|
|
uint
|
|
|
|
ircd::fs::prot(const map::opts &opts)
|
|
|
|
{
|
|
|
|
uint ret
|
|
|
|
{
|
|
|
|
PROT_NONE
|
|
|
|
};
|
|
|
|
|
|
|
|
if(opts.mode & std::ios::in)
|
|
|
|
ret |= PROT_READ;
|
|
|
|
|
|
|
|
if(opts.mode & std::ios::out)
|
|
|
|
ret |= PROT_WRITE;
|
|
|
|
|
|
|
|
assert(!opts.execute);
|
|
|
|
if((false) && opts.execute)
|
|
|
|
ret |= PROT_EXEC;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint
|
|
|
|
ircd::fs::flags(const map::opts &opts)
|
|
|
|
{
|
|
|
|
uint ret
|
|
|
|
{
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
|
|
|
if(opts.shared)
|
|
|
|
ret |= MAP_SHARED;
|
|
|
|
else
|
|
|
|
ret |= MAP_PRIVATE;
|
|
|
|
|
|
|
|
#if defined(MAP_NONBLOCK)
|
|
|
|
if(!opts.blocking)
|
|
|
|
ret |= MAP_NONBLOCK;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MAP_POPULATE)
|
|
|
|
if(opts.populate)
|
|
|
|
ret |= MAP_POPULATE;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MAP_NORESERVE)
|
|
|
|
if(!opts.reserve)
|
|
|
|
ret |= MAP_NORESERVE;
|
|
|
|
#endif
|
|
|
|
|
2021-03-11 22:36:39 +01:00
|
|
|
#if defined(MAP_LOCKED)
|
|
|
|
if(opts.locked)
|
|
|
|
ret |= MAP_LOCKED;
|
|
|
|
#endif
|
|
|
|
|
2021-03-11 22:37:33 +01:00
|
|
|
#if defined(MAP_HUGE_TLB) && defined(MAP_HUGE_2MB)
|
|
|
|
if(opts.huge2mb)
|
|
|
|
ret |= MAP_HUGETLB | MAP_HUGE_2MB;
|
|
|
|
#elif defined(MAP_HUGE_SHIFT)
|
|
|
|
if(opts.huge2mb)
|
|
|
|
ret |= (21 << MAP_HUGE_SHIFT);
|
|
|
|
#elif defined(MAP_HUGETLB)
|
|
|
|
if(opts.huge2mb)
|
|
|
|
ret |= MAP_HUGE_TLB
|
|
|
|
#else
|
|
|
|
#warning "MAP_HUGETLB (2MB) not supported"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MAP_HUGE_TLB) && defined(MAP_HUGE_1GB)
|
|
|
|
if(opts.huge1gb)
|
|
|
|
ret |= MAP_HUGE_1GB;
|
|
|
|
#elif defined(MAP_HUGE_SHIFT)
|
|
|
|
if(opts.huge1gb)
|
|
|
|
ret |= (30 << MAP_HUGE_SHIFT);
|
|
|
|
#elif defined(MAP_HUGE_TLB)
|
|
|
|
if(opts.huge1gb)
|
|
|
|
ret |= MAP_HUGE_TLB
|
|
|
|
#else
|
|
|
|
#warning "MAP_HUGETLB (1GB) not supported"
|
|
|
|
#endif
|
|
|
|
|
2020-08-24 17:49:41 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2018-11-28 03:09:12 +01:00
|
|
|
//
|
|
|
|
// fs/fd.h
|
|
|
|
//
|
2018-05-30 10:38:20 +02:00
|
|
|
|
2019-01-02 03:35:09 +01:00
|
|
|
namespace ircd::fs
|
|
|
|
{
|
2020-05-21 02:55:13 +02:00
|
|
|
static uint flags(const fd::opts &);
|
|
|
|
static uint flags(const std::ios::openmode &);
|
2022-06-18 21:05:17 +02:00
|
|
|
static fd::opts make(const fd::opts &);
|
2019-01-02 03:35:09 +01:00
|
|
|
static long pathconf(const fd &, const int &arg);
|
|
|
|
}
|
|
|
|
|
2018-12-09 00:17:13 +01:00
|
|
|
decltype(ircd::fs::fd::opts::direct_io_enable)
|
|
|
|
ircd::fs::fd::opts::direct_io_enable
|
|
|
|
{
|
|
|
|
{ "name", "ircd.fs.fd.direct_io_enable" },
|
|
|
|
{ "default", true },
|
2018-12-09 01:15:22 +01:00
|
|
|
{ "persist", false },
|
2018-12-09 00:17:13 +01:00
|
|
|
};
|
|
|
|
|
2019-07-12 22:34:18 +02:00
|
|
|
#if defined(POSIX_FADV_DONTNEED)
|
|
|
|
size_t
|
|
|
|
ircd::fs::evict(const fd &fd,
|
|
|
|
const size_t &count,
|
|
|
|
const opts &opts)
|
|
|
|
{
|
|
|
|
return advise(fd, POSIX_FADV_DONTNEED, count, opts);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#warning "POSIX_FADV_DONTNEED not available on this platform."
|
|
|
|
size_t
|
|
|
|
ircd::fs::evict(const fd &fd,
|
|
|
|
const size_t &count,
|
|
|
|
const opts &opts)
|
|
|
|
{
|
|
|
|
return 0UL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(HAVE_POSIX_FADVISE)
|
|
|
|
size_t
|
|
|
|
ircd::fs::advise(const fd &fd,
|
|
|
|
const int &advice,
|
|
|
|
const size_t &count,
|
|
|
|
const opts &opts)
|
|
|
|
{
|
|
|
|
static const size_t max_count
|
|
|
|
{
|
|
|
|
128_KiB
|
|
|
|
};
|
|
|
|
|
|
|
|
size_t i(0), off, cnt; do
|
|
|
|
{
|
|
|
|
off = opts.offset + max_count * i++;
|
|
|
|
cnt = std::min(opts.offset + count - off, max_count);
|
|
|
|
switch(const auto r(::posix_fadvise(fd, off, cnt, advice)); r)
|
|
|
|
{
|
|
|
|
case 0: break;
|
|
|
|
default: throw_system_error(r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while(off + cnt < opts.offset + count);
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#warning "posix_fadvise(2) not available for this compilation."
|
|
|
|
size_t
|
|
|
|
ircd::fs::advise(const fd &fd,
|
|
|
|
const int &advice,
|
|
|
|
const size_t &count,
|
|
|
|
const opts &opts)
|
|
|
|
{
|
2019-10-06 02:55:10 +02:00
|
|
|
return 0UL;
|
2019-07-12 22:34:18 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-05-04 04:53:05 +02:00
|
|
|
#if defined(HAVE_FCNTL_H) && defined(F_SET_FILE_RW_HINT)
|
|
|
|
void
|
|
|
|
ircd::fs::write_life(const fd &fd,
|
|
|
|
const uint64_t &hint)
|
|
|
|
{
|
2020-03-20 18:04:51 +01:00
|
|
|
if(!support::rwh_write_life)
|
2019-05-06 02:32:07 +02:00
|
|
|
return;
|
|
|
|
|
2019-05-04 04:53:05 +02:00
|
|
|
syscall(::fcntl, int(fd), F_SET_FILE_RW_HINT, &hint);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#warning "F_SET_FILE_RW_HINT not supported on platform."
|
|
|
|
void
|
|
|
|
ircd::fs::write_life(const fd &fd,
|
|
|
|
const uint64_t &hint)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(HAVE_FCNTL_H) && defined(F_GET_FILE_RW_HINT)
|
|
|
|
uint64_t
|
|
|
|
ircd::fs::write_life(const fd &fd)
|
2019-05-09 04:08:28 +02:00
|
|
|
noexcept try
|
2019-05-04 04:53:05 +02:00
|
|
|
{
|
|
|
|
uint64_t ret;
|
|
|
|
syscall(::fcntl, int(fd), F_GET_FILE_RW_HINT, &ret);
|
|
|
|
return ret;
|
|
|
|
}
|
2019-05-09 04:08:28 +02:00
|
|
|
catch(const std::system_error &e)
|
|
|
|
{
|
|
|
|
log::derror
|
|
|
|
{
|
|
|
|
log, "fcntl(F_GET_FILE_RW_HINT) fd:%d :%s",
|
|
|
|
int(fd),
|
|
|
|
e.what()
|
|
|
|
};
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2019-05-04 04:53:05 +02:00
|
|
|
#else
|
|
|
|
#warning "F_GET_FILE_RW_HINT not supported on platform."
|
|
|
|
uint64_t
|
|
|
|
ircd::fs::write_life(const fd &fd)
|
|
|
|
{
|
|
|
|
return 0UL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-01-02 03:35:09 +01:00
|
|
|
#ifdef HAVE_SYS_STAT_H
|
|
|
|
ulong
|
|
|
|
ircd::fs::device(const fd &fd)
|
|
|
|
{
|
|
|
|
struct stat st{0};
|
|
|
|
syscall(::fstat, fd, &st);
|
|
|
|
return st.st_dev;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
ulong
|
|
|
|
ircd::fs::device(const fd &fd)
|
|
|
|
{
|
|
|
|
static_assert
|
|
|
|
(
|
|
|
|
0, "Please implement this definition"
|
|
|
|
)
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_SYS_STATFS_H
|
|
|
|
ulong
|
2019-01-25 19:35:39 +01:00
|
|
|
ircd::fs::fstype(const fd &fd)
|
2019-01-02 03:35:09 +01:00
|
|
|
{
|
|
|
|
struct statfs f{0};
|
|
|
|
syscall(::fstatfs, fd, &f);
|
|
|
|
return f.f_type;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
ulong
|
2019-01-25 19:35:39 +01:00
|
|
|
ircd::fs::fstype(const fd &fd)
|
2019-01-02 03:35:09 +01:00
|
|
|
{
|
|
|
|
static_assert
|
|
|
|
(
|
|
|
|
0, "Please implement this definition"
|
|
|
|
)
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-10-18 18:28:10 +02:00
|
|
|
#ifdef __linux__
|
2018-08-29 04:31:58 +02:00
|
|
|
size_t
|
|
|
|
ircd::fs::block_size(const fd &fd)
|
|
|
|
{
|
2018-10-18 18:28:10 +02:00
|
|
|
return 512UL;
|
|
|
|
}
|
|
|
|
#elif defined(HAVE_SYS_STAT_H)
|
|
|
|
size_t
|
|
|
|
ircd::fs::block_size(const fd &fd)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
syscall(::fstat, fd, &st);
|
|
|
|
return st.st_blksize;
|
2018-08-29 04:31:58 +02:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
size_t
|
|
|
|
ircd::fs::block_size(const fd &fd)
|
|
|
|
{
|
|
|
|
return info::page_size;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-01-02 03:35:09 +01:00
|
|
|
long
|
|
|
|
ircd::fs::pathconf(const fd &fd,
|
|
|
|
const int &arg)
|
2018-08-21 07:44:39 +02:00
|
|
|
{
|
2019-01-02 03:35:09 +01:00
|
|
|
return syscall(::fpathconf, fd, arg);
|
2018-08-21 07:44:39 +02:00
|
|
|
}
|
|
|
|
|
2018-05-30 11:12:19 +02:00
|
|
|
size_t
|
|
|
|
ircd::fs::size(const fd &fd)
|
|
|
|
{
|
2018-08-24 06:25:05 +02:00
|
|
|
const off_t cur
|
|
|
|
{
|
|
|
|
syscall(::lseek, fd, 0, SEEK_CUR)
|
|
|
|
};
|
|
|
|
|
|
|
|
const off_t end
|
|
|
|
{
|
|
|
|
syscall(::lseek, fd, 0, SEEK_END)
|
|
|
|
};
|
|
|
|
|
|
|
|
syscall(::lseek, fd, cur, SEEK_SET);
|
|
|
|
return end;
|
|
|
|
}
|
2018-05-30 11:12:19 +02:00
|
|
|
|
2018-05-30 10:38:20 +02:00
|
|
|
//
|
|
|
|
// fd::fd
|
|
|
|
//
|
|
|
|
|
2019-04-01 08:57:35 +02:00
|
|
|
ircd::fs::fd::fd(const int &fdno)
|
2020-05-21 03:20:47 +02:00
|
|
|
:fdno
|
|
|
|
{
|
|
|
|
fdno
|
|
|
|
}
|
2019-04-01 08:57:35 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-05-30 10:38:20 +02:00
|
|
|
ircd::fs::fd::fd(const string_view &path)
|
2020-05-21 03:20:47 +02:00
|
|
|
:fd
|
|
|
|
{
|
|
|
|
path, opts{}
|
|
|
|
}
|
2018-05-30 10:38:20 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::fs::fd::fd(const string_view &path,
|
|
|
|
const opts &opts)
|
2020-05-21 03:20:47 +02:00
|
|
|
:fd
|
|
|
|
{
|
|
|
|
AT_FDCWD, path, opts
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::fs::fd::fd(const int &dirfd,
|
|
|
|
const string_view &path,
|
2022-06-18 21:05:17 +02:00
|
|
|
const opts &opts_)
|
2020-05-09 03:13:51 +02:00
|
|
|
try
|
2020-05-21 03:20:47 +02:00
|
|
|
:fdno
|
|
|
|
{
|
|
|
|
-1 // sentinel value for inert dtor
|
|
|
|
}
|
2018-05-30 10:38:20 +02:00
|
|
|
{
|
2020-05-24 23:43:42 +02:00
|
|
|
const unwind_exceptional dtor_on_error
|
|
|
|
{
|
|
|
|
[this] { this->~fd(); }
|
|
|
|
};
|
|
|
|
|
2022-06-18 21:05:17 +02:00
|
|
|
const auto opts
|
|
|
|
{
|
|
|
|
make(opts_)
|
|
|
|
};
|
|
|
|
|
2020-05-21 02:56:21 +02:00
|
|
|
const mode_t mode
|
2019-02-12 19:50:36 +01:00
|
|
|
{
|
2020-05-21 02:56:21 +02:00
|
|
|
mode_t(opts.mask)
|
2019-02-12 19:50:36 +01:00
|
|
|
};
|
|
|
|
|
2020-05-21 02:56:21 +02:00
|
|
|
const uint &flags
|
|
|
|
{
|
|
|
|
fs::flags(opts)
|
|
|
|
};
|
2018-05-30 13:12:27 +02:00
|
|
|
|
2020-05-21 02:56:21 +02:00
|
|
|
const int &advise
|
2020-05-10 03:34:50 +02:00
|
|
|
{
|
|
|
|
opts.direct?
|
|
|
|
0:
|
|
|
|
opts.random?
|
|
|
|
POSIX_FADV_RANDOM:
|
|
|
|
opts.sequential?
|
|
|
|
POSIX_FADV_SEQUENTIAL:
|
|
|
|
opts.dontneed?
|
|
|
|
POSIX_FADV_DONTNEED:
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
2020-05-21 02:56:21 +02:00
|
|
|
{
|
2020-05-24 23:43:42 +02:00
|
|
|
const prof::syscall_usage_warning message
|
|
|
|
{
|
|
|
|
"fs::fs::fd(): openat(2): %s", path
|
|
|
|
};
|
2020-05-21 02:56:21 +02:00
|
|
|
|
2020-05-24 23:43:42 +02:00
|
|
|
assert((flags & ~O_CREAT) || mode != 0);
|
|
|
|
fdno = syscall(::openat, dirfd, path_cstr(path), flags, mode);
|
|
|
|
}
|
2020-05-21 02:56:21 +02:00
|
|
|
|
2020-05-10 03:34:50 +02:00
|
|
|
if(advise)
|
|
|
|
fs::advise(*this, advise);
|
|
|
|
|
2018-05-30 10:38:20 +02:00
|
|
|
if(opts.ate)
|
|
|
|
syscall(::lseek, fdno, 0, SEEK_END);
|
|
|
|
}
|
2020-05-09 03:13:51 +02:00
|
|
|
catch(const std::system_error &e)
|
|
|
|
{
|
2022-06-18 21:05:17 +02:00
|
|
|
if(opts_.errlog)
|
2020-06-08 23:07:42 +02:00
|
|
|
log::derror
|
|
|
|
{
|
|
|
|
log, "`%s' :%s",
|
|
|
|
path,
|
|
|
|
e.what(),
|
|
|
|
};
|
2020-05-21 02:56:21 +02:00
|
|
|
|
|
|
|
throw;
|
2020-05-09 03:13:51 +02:00
|
|
|
}
|
2018-05-30 10:38:20 +02:00
|
|
|
|
|
|
|
ircd::fs::fd::fd(fd &&o)
|
|
|
|
noexcept
|
|
|
|
:fdno
|
|
|
|
{
|
|
|
|
std::move(o.fdno)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
o.fdno = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::fs::fd &
|
|
|
|
ircd::fs::fd::operator=(fd &&o)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
this->~fd();
|
|
|
|
fdno = std::move(o.fdno);
|
|
|
|
o.fdno = -1;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::fs::fd::~fd()
|
2019-06-24 07:17:49 +02:00
|
|
|
noexcept
|
2019-04-22 18:16:11 +02:00
|
|
|
{
|
2019-06-24 07:17:49 +02:00
|
|
|
if(likely(fdno >= 0)) try
|
2019-04-22 18:16:11 +02:00
|
|
|
{
|
2019-06-24 07:17:49 +02:00
|
|
|
syscall(::close, fdno);
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::critical
|
|
|
|
{
|
|
|
|
"Failed to close fd:%d :%s",
|
|
|
|
fdno,
|
|
|
|
e.what()
|
|
|
|
};
|
|
|
|
}
|
2019-04-22 18:16:11 +02:00
|
|
|
}
|
2018-05-30 10:38:20 +02:00
|
|
|
|
2019-04-22 17:55:42 +02:00
|
|
|
int
|
|
|
|
ircd::fs::fd::release()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
const int fdno(this->fdno);
|
|
|
|
this->fdno = -1;
|
|
|
|
return fdno;
|
|
|
|
}
|
|
|
|
|
2019-04-22 18:09:20 +02:00
|
|
|
ircd::fs::fd::opts
|
|
|
|
ircd::fs::fd::options()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
opts ret;
|
|
|
|
ret.flags = syscall(::fcntl, int(*this), F_GETFL, 0);
|
|
|
|
|
|
|
|
if((ret.flags & O_RDONLY) == O_RDONLY)
|
|
|
|
ret.mode = std::ios::in;
|
|
|
|
|
|
|
|
if((ret.flags & O_WRONLY) == O_WRONLY)
|
|
|
|
ret.mode = std::ios::out;
|
|
|
|
|
|
|
|
if((ret.flags & O_RDWR) == O_RDWR)
|
|
|
|
ret.mode = std::ios::in | std::ios::out;
|
|
|
|
|
2020-09-29 07:23:56 +02:00
|
|
|
if((ret.flags & O_TRUNC) == O_TRUNC)
|
|
|
|
ret.mode = std::ios::trunc;
|
|
|
|
|
2019-04-22 18:09:20 +02:00
|
|
|
ret.direct = ret.flags & O_DIRECT;
|
|
|
|
ret.cloexec = ret.flags & O_CLOEXEC;
|
2020-09-29 07:14:13 +02:00
|
|
|
ret.create = ret.flags & O_CREAT;
|
2019-04-22 18:09:20 +02:00
|
|
|
ret.blocking = ret.flags & O_NONBLOCK;
|
2020-09-29 07:30:11 +02:00
|
|
|
ret.exclusive = ret.flags & O_EXCL;
|
2019-04-22 18:09:20 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-06-18 21:05:17 +02:00
|
|
|
ircd::fs::fd::opts
|
|
|
|
ircd::fs::make(const fd::opts &opts)
|
|
|
|
{
|
|
|
|
fd::opts ret(opts);
|
|
|
|
if(!ret.flags)
|
|
|
|
ret.flags = fs::flags(ret);
|
|
|
|
|
|
|
|
if(!ret.mask && (ret.flags & O_CREAT))
|
|
|
|
ret.mask = S_IRUSR | S_IWUSR;
|
|
|
|
|
|
|
|
if(!ret.ate)
|
|
|
|
ret.ate = bool(ret.mode & std::ios::ate);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-05-21 02:55:13 +02:00
|
|
|
uint
|
|
|
|
ircd::fs::flags(const fd::opts &opts)
|
|
|
|
{
|
|
|
|
uint ret(opts.flags);
|
|
|
|
ret |= fs::flags(opts.mode);
|
|
|
|
ret |= opts.direct? O_DIRECT : 0UL;
|
|
|
|
ret |= opts.cloexec? O_CLOEXEC : 0UL;
|
2020-09-29 07:14:13 +02:00
|
|
|
ret |= opts.create? O_CREAT : 0UL;
|
2020-05-21 02:55:13 +02:00
|
|
|
ret |= !opts.blocking? O_NONBLOCK : 0UL;
|
2020-09-29 07:30:11 +02:00
|
|
|
ret |= opts.exclusive? O_EXCL : 0UL;
|
2020-05-21 02:55:13 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint
|
|
|
|
ircd::fs::flags(const std::ios::openmode &mode)
|
|
|
|
{
|
|
|
|
static const auto rdwr
|
|
|
|
{
|
|
|
|
std::ios::in | std::ios::out
|
|
|
|
};
|
|
|
|
|
|
|
|
uint ret{0};
|
|
|
|
if((mode & rdwr) == rdwr)
|
|
|
|
ret |= O_RDWR;
|
|
|
|
else if(mode & std::ios::out)
|
|
|
|
ret |= O_WRONLY;
|
|
|
|
else
|
|
|
|
ret |= O_RDONLY;
|
|
|
|
|
|
|
|
ret |= mode & std::ios::trunc? O_TRUNC : 0;
|
|
|
|
ret |= mode & std::ios::app? O_APPEND : 0;
|
|
|
|
ret |= ret & O_WRONLY? O_CREAT : 0;
|
|
|
|
ret |= ret & O_RDWR && ret & (O_TRUNC | O_APPEND)? O_CREAT : 0;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-12-30 05:02:22 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// fs/opts.h
|
|
|
|
//
|
|
|
|
|
|
|
|
decltype(ircd::fs::opts_default)
|
|
|
|
ircd::fs::opts_default
|
|
|
|
{};
|
|
|
|
|
2019-03-14 03:33:58 +01:00
|
|
|
decltype(ircd::fs::opts::highest_priority)
|
|
|
|
ircd::fs::opts::highest_priority
|
|
|
|
{
|
|
|
|
std::numeric_limits<decltype(priority)>::min()
|
|
|
|
};
|
|
|
|
|
2019-04-06 21:13:16 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// fs/op.h
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::fs::reflect(const op &op)
|
|
|
|
{
|
|
|
|
switch(op)
|
|
|
|
{
|
|
|
|
case op::NOOP: return "NOOP";
|
|
|
|
case op::READ: return "READ";
|
|
|
|
case op::WRITE: return "WRITE";
|
|
|
|
case op::SYNC: return "SYNC";
|
2019-04-08 02:29:23 +02:00
|
|
|
case op::WAIT: return "WAIT";
|
2019-04-06 21:13:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return "????";
|
|
|
|
}
|
|
|
|
|
2019-05-06 02:47:08 +02:00
|
|
|
#ifndef IRCD_USE_AIO
|
2019-10-06 03:04:13 +02:00
|
|
|
[[gnu::weak]]
|
2019-04-06 21:13:16 +02:00
|
|
|
ircd::fs::op
|
|
|
|
ircd::fs::aio::translate(const int &val)
|
|
|
|
{
|
|
|
|
return op::NOOP;
|
|
|
|
}
|
2019-05-06 02:47:08 +02:00
|
|
|
#endif
|
2019-04-06 21:13:16 +02:00
|
|
|
|
2019-08-11 07:06:08 +02:00
|
|
|
#ifndef IRCD_USE_IOU
|
2019-10-06 03:04:13 +02:00
|
|
|
[[gnu::weak]]
|
2019-08-11 07:06:08 +02:00
|
|
|
ircd::fs::op
|
|
|
|
ircd::fs::iou::translate(const int &val)
|
|
|
|
{
|
|
|
|
return op::NOOP;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-11-29 01:53:04 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// fs/iov.h
|
|
|
|
//
|
|
|
|
|
2018-12-02 00:33:30 +01:00
|
|
|
ircd::fs::const_iovec_view
|
|
|
|
ircd::fs::make_iov(const iovec_view &iov,
|
2018-12-18 21:28:28 +01:00
|
|
|
const mutable_buffers &bufs,
|
|
|
|
const size_t &offset)
|
2018-12-02 00:33:30 +01:00
|
|
|
{
|
2019-03-14 03:33:58 +01:00
|
|
|
assert(offset <= buffers::size(bufs));
|
2018-12-18 21:28:28 +01:00
|
|
|
const size_t max
|
|
|
|
{
|
|
|
|
std::min(iov.size(), bufs.size())
|
|
|
|
};
|
|
|
|
|
|
|
|
size_t i(0), off(0);
|
|
|
|
for(; i < max; off += size(bufs[i++]))
|
2019-03-16 21:49:04 +01:00
|
|
|
if(size(bufs[i]) >= offset - off)
|
2018-12-02 00:33:30 +01:00
|
|
|
{
|
2019-03-16 21:49:04 +01:00
|
|
|
assert(offset >= off);
|
2018-12-18 21:28:28 +01:00
|
|
|
off = offset - off;
|
|
|
|
break;
|
|
|
|
}
|
2018-12-02 00:33:30 +01:00
|
|
|
|
2019-03-16 21:49:04 +01:00
|
|
|
assert(i <= max);
|
2018-12-18 21:28:28 +01:00
|
|
|
if(i < max)
|
2018-12-02 00:33:30 +01:00
|
|
|
{
|
2018-12-18 21:28:28 +01:00
|
|
|
assert(off <= size(bufs[i]));
|
|
|
|
iov.at(i) =
|
|
|
|
{
|
|
|
|
data(bufs[i]) + off, size(bufs[i]) - off
|
|
|
|
};
|
|
|
|
|
|
|
|
for(++i; i < max; ++i)
|
|
|
|
iov.at(i) =
|
|
|
|
{
|
|
|
|
data(bufs[i]), size(bufs[i])
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-03-16 21:49:04 +01:00
|
|
|
assert(i <= max);
|
2018-12-18 21:28:28 +01:00
|
|
|
const const_iovec_view ret{iov.data(), i};
|
|
|
|
assert(bytes(ret) <= buffer::buffers::size(bufs));
|
|
|
|
return ret;
|
2018-12-02 00:33:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::fs::const_iovec_view
|
|
|
|
ircd::fs::make_iov(const iovec_view &iov,
|
2018-12-18 21:28:28 +01:00
|
|
|
const const_buffers &bufs,
|
|
|
|
const size_t &offset)
|
2018-12-02 00:33:30 +01:00
|
|
|
{
|
2019-03-14 03:33:58 +01:00
|
|
|
assert(offset <= buffers::size(bufs));
|
2018-12-18 21:28:28 +01:00
|
|
|
const size_t max
|
|
|
|
{
|
|
|
|
std::min(iov.size(), bufs.size())
|
|
|
|
};
|
|
|
|
|
|
|
|
size_t i(0), off(0);
|
|
|
|
for(; i < max; off += size(bufs[i++]))
|
2019-03-16 21:49:04 +01:00
|
|
|
if(size(bufs[i]) >= offset - off)
|
2018-12-18 21:28:28 +01:00
|
|
|
{
|
2019-03-16 21:49:04 +01:00
|
|
|
assert(offset >= off);
|
2018-12-18 21:28:28 +01:00
|
|
|
off = offset - off;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-03-16 21:49:04 +01:00
|
|
|
assert(i <= max);
|
2018-12-18 21:28:28 +01:00
|
|
|
if(i < max)
|
|
|
|
{
|
|
|
|
assert(off <= size(bufs[i]));
|
2018-12-02 00:33:30 +01:00
|
|
|
iov.at(i) =
|
2018-11-29 01:53:04 +01:00
|
|
|
{
|
2018-12-18 21:28:28 +01:00
|
|
|
const_cast<char *>(data(bufs[i])) + off, size(bufs[i]) - off
|
2018-11-29 01:53:04 +01:00
|
|
|
};
|
|
|
|
|
2018-12-18 21:28:28 +01:00
|
|
|
for(++i; i < max; ++i)
|
|
|
|
iov.at(i) =
|
|
|
|
{
|
|
|
|
const_cast<char *>(data(bufs[i])), size(bufs[i])
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-03-16 21:49:04 +01:00
|
|
|
assert(i <= max);
|
2018-12-18 21:28:28 +01:00
|
|
|
const const_iovec_view ret{iov.data(), i};
|
|
|
|
assert(bytes(ret) <= buffer::buffers::size(bufs));
|
|
|
|
return ret;
|
2018-11-29 01:53:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
2018-12-02 00:33:30 +01:00
|
|
|
ircd::fs::bytes(const const_iovec_view &iov)
|
2018-11-29 01:53:04 +01:00
|
|
|
{
|
|
|
|
return std::accumulate(begin(iov), end(iov), size_t(0), []
|
|
|
|
(auto ret, const auto &iov)
|
|
|
|
{
|
|
|
|
return ret += iov.iov_len;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-03-08 18:27:32 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2018-08-23 12:31:36 +02:00
|
|
|
//
|
|
|
|
// fs/error.h
|
|
|
|
//
|
|
|
|
|
2019-03-17 01:21:42 +01:00
|
|
|
std::string
|
|
|
|
ircd::string(const boost::filesystem::filesystem_error &e)
|
|
|
|
{
|
|
|
|
return ircd::string(512, [&e]
|
|
|
|
(const mutable_buffer &buf)
|
|
|
|
{
|
|
|
|
return string(buf, e);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::string(const mutable_buffer &buf,
|
|
|
|
const boost::filesystem::filesystem_error &e)
|
|
|
|
{
|
|
|
|
return fmt::sprintf
|
|
|
|
{
|
|
|
|
buf, "%s :%s", e.code().category().name(), e.what()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::system_error
|
|
|
|
ircd::make_system_error(const boost::filesystem::filesystem_error &e)
|
|
|
|
{
|
|
|
|
return std::system_error
|
|
|
|
{
|
|
|
|
make_error_code(e), e.what()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-08-23 12:31:36 +02:00
|
|
|
std::error_code
|
|
|
|
ircd::make_error_code(const boost::filesystem::filesystem_error &e)
|
|
|
|
{
|
|
|
|
const boost::system::error_code &ec
|
|
|
|
{
|
|
|
|
e.code()
|
|
|
|
};
|
|
|
|
|
|
|
|
return make_error_code(ec);
|
|
|
|
}
|
2018-12-16 05:12:39 +01:00
|
|
|
|
|
|
|
//
|
|
|
|
// error::error
|
|
|
|
//
|
|
|
|
|
2020-12-21 15:43:15 +01:00
|
|
|
decltype(ircd::fs::error::buf)
|
|
|
|
thread_local
|
2019-03-17 01:21:42 +01:00
|
|
|
ircd::fs::error::buf;
|
2018-12-16 05:12:39 +01:00
|
|
|
|
2019-03-17 01:21:42 +01:00
|
|
|
ircd::fs::error::error(const boost::filesystem::filesystem_error &e)
|
|
|
|
:std::system_error
|
2018-12-16 05:12:39 +01:00
|
|
|
{
|
2019-07-21 05:19:40 +02:00
|
|
|
make_system_error(e)
|
2018-12-16 05:12:39 +01:00
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
2018-12-30 00:27:58 +01:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Internal utils
|
|
|
|
//
|
|
|
|
|
2019-03-14 02:50:57 +01:00
|
|
|
/// Translate an ircd::fs opts priority integer to an AIO priority integer.
|
|
|
|
/// The ircd::fs priority integer is like a nice value. The AIO value is
|
|
|
|
/// positive [0, MAX_REQPRIO]. This function takes an ircd::fs value and
|
|
|
|
/// shifts it to the AIO value.
|
|
|
|
int
|
|
|
|
ircd::fs::reqprio(int input)
|
2019-08-06 01:15:56 +02:00
|
|
|
noexcept
|
2019-03-14 02:50:57 +01:00
|
|
|
{
|
|
|
|
const auto &max_reqprio
|
|
|
|
{
|
|
|
|
aio::MAX_REQPRIO
|
|
|
|
};
|
|
|
|
|
|
|
|
static const auto median
|
|
|
|
{
|
|
|
|
int(max_reqprio / 2)
|
|
|
|
};
|
|
|
|
|
|
|
|
input = std::max(input, 0 - median);
|
|
|
|
input = std::min(input, median);
|
|
|
|
input = max_reqprio - (input + median);
|
|
|
|
assert(input >= 0 && input <= int(max_reqprio));
|
|
|
|
return input;
|
|
|
|
}
|