2016-09-10 02:56:02 +03:00
|
|
|
/*
|
2019-06-20 02:58:25 +03:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2016-09-10 02:56:02 +03:00
|
|
|
*
|
2019-06-20 02:58:25 +03:00
|
|
|
* This software may be used and distributed according to the terms of the
|
|
|
|
* GNU General Public License version 2.
|
2016-09-10 02:56:02 +03:00
|
|
|
*/
|
2019-10-11 15:26:59 +03:00
|
|
|
|
2018-03-29 06:42:21 +03:00
|
|
|
#include "eden/fs/inodes/Overlay.h"
|
|
|
|
|
2017-02-11 01:16:00 +03:00
|
|
|
#include <boost/filesystem.hpp>
|
2019-09-04 21:05:47 +03:00
|
|
|
#include <algorithm>
|
|
|
|
|
2016-09-10 02:56:02 +03:00
|
|
|
#include <folly/Exception.h>
|
2017-02-11 01:16:00 +03:00
|
|
|
#include <folly/File.h>
|
2016-09-10 02:56:02 +03:00
|
|
|
#include <folly/FileUtil.h>
|
2018-07-20 21:40:20 +03:00
|
|
|
#include <folly/Range.h>
|
2017-07-04 10:18:17 +03:00
|
|
|
#include <folly/io/Cursor.h>
|
|
|
|
#include <folly/io/IOBuf.h>
|
2018-05-01 07:20:51 +03:00
|
|
|
#include <folly/logging/xlog.h>
|
2016-09-10 02:56:02 +03:00
|
|
|
#include <thrift/lib/cpp2/protocol/Serializer.h>
|
2018-06-01 19:29:47 +03:00
|
|
|
#include "eden/fs/inodes/DirEntry.h"
|
2020-04-23 22:38:56 +03:00
|
|
|
#include "eden/fs/utils/Bug.h"
|
|
|
|
#include "eden/fs/utils/PathFuncs.h"
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include "eden/fs/inodes/InodeBase.h"
|
|
|
|
#include "eden/fs/inodes/TreeInode.h"
|
|
|
|
#include "eden/fs/win/utils/StringConv.h" // @manual
|
|
|
|
#include "eden/fs/win/utils/Stub.h" // @manual
|
|
|
|
|
|
|
|
#else
|
2018-05-22 20:46:24 +03:00
|
|
|
#include "eden/fs/inodes/InodeTable.h"
|
2019-11-07 05:21:39 +03:00
|
|
|
#include "eden/fs/inodes/OverlayFile.h"
|
2019-09-04 21:05:47 +03:00
|
|
|
#include "eden/fs/inodes/overlay/OverlayChecker.h"
|
2020-04-23 22:38:56 +03:00
|
|
|
#endif // !_WIN32
|
2016-09-10 02:56:02 +03:00
|
|
|
|
|
|
|
namespace facebook {
|
|
|
|
namespace eden {
|
|
|
|
|
2020-01-28 05:15:29 +03:00
|
|
|
namespace {
|
|
|
|
constexpr uint64_t ioCountMask = 0x7FFFFFFFFFFFFFFFull;
|
|
|
|
constexpr uint64_t ioClosedMask = 1ull << 63;
|
|
|
|
} // namespace
|
|
|
|
|
2019-03-07 07:30:45 +03:00
|
|
|
using folly::Unit;
|
2018-10-24 04:48:38 +03:00
|
|
|
using std::optional;
|
2016-09-10 02:56:02 +03:00
|
|
|
|
2019-11-07 05:21:39 +03:00
|
|
|
std::shared_ptr<Overlay> Overlay::create(AbsolutePathPiece localDir) {
|
|
|
|
struct MakeSharedEnabler : public Overlay {
|
|
|
|
explicit MakeSharedEnabler(AbsolutePathPiece localDir)
|
|
|
|
: Overlay(localDir) {}
|
|
|
|
};
|
|
|
|
return std::make_shared<MakeSharedEnabler>(localDir);
|
|
|
|
}
|
|
|
|
|
2020-04-23 22:38:56 +03:00
|
|
|
Overlay::Overlay(AbsolutePathPiece localDir) : backingOverlay_{localDir} {}
|
2018-04-25 04:18:35 +03:00
|
|
|
|
|
|
|
Overlay::~Overlay() {
|
2018-05-31 11:40:38 +03:00
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
2018-08-24 08:41:19 +03:00
|
|
|
void Overlay::close() {
|
2018-04-25 04:18:35 +03:00
|
|
|
CHECK_NE(std::this_thread::get_id(), gcThread_.get_id());
|
2018-05-31 11:40:38 +03:00
|
|
|
|
2019-03-07 07:30:45 +03:00
|
|
|
gcQueue_.lock()->stop = true;
|
|
|
|
gcCondVar_.notify_one();
|
|
|
|
if (gcThread_.joinable()) {
|
|
|
|
gcThread_.join();
|
|
|
|
}
|
|
|
|
|
2019-03-12 03:26:06 +03:00
|
|
|
// Make sure everything is shut down in reverse of construction order.
|
|
|
|
// Cleanup is not necessary if overlay was not initialized
|
2020-04-23 22:38:56 +03:00
|
|
|
if (!backingOverlay_.initialized()) {
|
2018-08-24 08:41:19 +03:00
|
|
|
return;
|
2018-05-31 11:40:38 +03:00
|
|
|
}
|
|
|
|
|
2019-03-12 03:26:06 +03:00
|
|
|
// Since we are closing the overlay, no other threads can still be using
|
|
|
|
// it. They must have used some external synchronization mechanism to
|
|
|
|
// ensure this, so it is okay for us to still use relaxed access to
|
|
|
|
// nextInodeNumber_.
|
|
|
|
std::optional<InodeNumber> optNextInodeNumber;
|
|
|
|
auto nextInodeNumber = nextInodeNumber_.load(std::memory_order_relaxed);
|
|
|
|
if (nextInodeNumber) {
|
|
|
|
optNextInodeNumber = InodeNumber{nextInodeNumber};
|
|
|
|
}
|
2018-06-01 04:32:34 +03:00
|
|
|
|
2019-11-11 21:53:50 +03:00
|
|
|
closeAndWaitForOutstandingIO();
|
2020-04-23 22:38:56 +03:00
|
|
|
#ifndef _WIN32
|
2018-05-31 11:40:38 +03:00
|
|
|
inodeMetadataTable_.reset();
|
2020-04-23 22:38:56 +03:00
|
|
|
#endif // !_WIN32
|
|
|
|
|
|
|
|
backingOverlay_.close(optNextInodeNumber);
|
2018-08-24 08:41:19 +03:00
|
|
|
}
|
2018-05-31 11:40:43 +03:00
|
|
|
|
2020-01-28 05:15:29 +03:00
|
|
|
bool Overlay::isClosed() {
|
|
|
|
return outstandingIORequests_.load(std::memory_order_acquire) & ioClosedMask;
|
|
|
|
}
|
|
|
|
|
2020-04-23 22:38:56 +03:00
|
|
|
#ifndef _WIN32
|
2019-11-11 21:53:50 +03:00
|
|
|
struct statfs Overlay::statFs() {
|
|
|
|
IORequest req{this};
|
2020-04-23 22:38:56 +03:00
|
|
|
return backingOverlay_.statFs();
|
2019-09-10 21:29:27 +03:00
|
|
|
}
|
2020-04-23 22:38:56 +03:00
|
|
|
#endif // !_WIN32
|
2019-09-10 21:29:27 +03:00
|
|
|
|
2019-03-12 03:26:06 +03:00
|
|
|
folly::SemiFuture<Unit> Overlay::initialize() {
|
|
|
|
// The initOverlay() call is potentially slow, so we want to avoid
|
|
|
|
// performing it in the current thread and blocking returning to our caller.
|
|
|
|
//
|
|
|
|
// We already spawn a separate thread for garbage collection. It's convenient
|
|
|
|
// to simply use this existing thread to perform the initialization logic
|
|
|
|
// before waiting for GC work to do.
|
|
|
|
auto [initPromise, initFuture] = folly::makePromiseContract<Unit>();
|
|
|
|
gcThread_ = std::thread([this, promise = std::move(initPromise)]() mutable {
|
|
|
|
try {
|
|
|
|
initOverlay();
|
|
|
|
} catch (std::exception& ex) {
|
|
|
|
XLOG(ERR) << "overlay initialization failed for "
|
2020-04-23 22:38:56 +03:00
|
|
|
<< backingOverlay_.getLocalDir() << ": " << ex.what();
|
2019-03-12 03:26:06 +03:00
|
|
|
promise.setException(
|
|
|
|
folly::exception_wrapper(std::current_exception(), ex));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
promise.setValue();
|
2020-04-23 22:38:56 +03:00
|
|
|
#ifndef _WIN32
|
|
|
|
// TODO: On Windows files are cached by the ProjectedFS. We need to clean
|
|
|
|
// the cached files while doing GC.
|
|
|
|
|
2019-03-12 03:26:06 +03:00
|
|
|
gcThread();
|
2020-04-23 22:38:56 +03:00
|
|
|
#endif
|
2019-03-12 03:26:06 +03:00
|
|
|
});
|
|
|
|
return std::move(initFuture);
|
|
|
|
}
|
2018-03-29 06:42:21 +03:00
|
|
|
|
2019-03-12 03:26:06 +03:00
|
|
|
void Overlay::initOverlay() {
|
2019-11-11 21:53:50 +03:00
|
|
|
IORequest req{this};
|
2020-04-23 22:38:56 +03:00
|
|
|
auto optNextInodeNumber = backingOverlay_.initOverlay(true);
|
2019-09-04 21:05:47 +03:00
|
|
|
if (!optNextInodeNumber.has_value()) {
|
2020-04-23 22:38:56 +03:00
|
|
|
#ifndef _WIN32
|
2019-09-04 21:05:47 +03:00
|
|
|
// If the next-inode-number data is missing it means that this overlay was
|
|
|
|
// not shut down cleanly the last time it was used. If this was caused by a
|
|
|
|
// hard system reboot this can sometimes cause corruption and/or missing
|
|
|
|
// data in some of the on-disk state.
|
|
|
|
//
|
|
|
|
// Use OverlayChecker to scan the overlay for any issues, and also compute
|
|
|
|
// correct next inode number as it does so.
|
2020-04-23 22:38:56 +03:00
|
|
|
XLOG(WARN) << "Overlay " << backingOverlay_.getLocalDir()
|
2019-09-04 21:05:47 +03:00
|
|
|
<< " was not shut down cleanly. Performing fsck scan.";
|
2020-04-23 22:38:56 +03:00
|
|
|
OverlayChecker checker(&backingOverlay_, std::nullopt);
|
2019-09-04 21:05:47 +03:00
|
|
|
checker.scanForErrors();
|
|
|
|
checker.repairErrors();
|
|
|
|
|
|
|
|
optNextInodeNumber = checker.getNextInodeNumber();
|
2020-04-23 22:38:56 +03:00
|
|
|
#else
|
|
|
|
// SqliteOverlay will always return the value of next Inode number, if we
|
|
|
|
// end up here - it's a bug.
|
|
|
|
EDEN_BUG() << "Sqlite Overlay is null value for NextInodeNumber";
|
|
|
|
#endif
|
2020-01-31 21:40:37 +03:00
|
|
|
} else {
|
|
|
|
hadCleanStartup_ = true;
|
2019-03-07 07:30:45 +03:00
|
|
|
}
|
2019-09-04 21:05:47 +03:00
|
|
|
nextInodeNumber_.store(optNextInodeNumber->get(), std::memory_order_relaxed);
|
2019-03-07 07:30:45 +03:00
|
|
|
|
2020-04-23 22:38:56 +03:00
|
|
|
#ifndef _WIN32
|
2018-05-22 20:46:24 +03:00
|
|
|
// Open after infoFile_'s lock is acquired because the InodeTable acquires
|
|
|
|
// its own lock, which should be released prior to infoFile_.
|
2020-04-23 22:38:56 +03:00
|
|
|
inodeMetadataTable_ =
|
|
|
|
InodeMetadataTable::open((backingOverlay_.getLocalDir() +
|
|
|
|
PathComponentPiece{FsOverlay::kMetadataFile})
|
|
|
|
.c_str());
|
|
|
|
#endif // !_WIN32
|
2018-06-14 20:07:36 +03:00
|
|
|
}
|
|
|
|
|
2018-05-31 11:40:43 +03:00
|
|
|
InodeNumber Overlay::allocateInodeNumber() {
|
|
|
|
// InodeNumber should generally be 64-bits wide, in which case it isn't even
|
|
|
|
// worth bothering to handle the case where nextInodeNumber_ wraps. We don't
|
|
|
|
// need to bother checking for conflicts with existing inode numbers since
|
|
|
|
// this can only happen if we wrap around. We don't currently support
|
|
|
|
// platforms with 32-bit inode numbers.
|
|
|
|
static_assert(
|
|
|
|
sizeof(nextInodeNumber_) == sizeof(InodeNumber),
|
|
|
|
"expected nextInodeNumber_ and InodeNumber to have the same size");
|
|
|
|
static_assert(
|
|
|
|
sizeof(InodeNumber) >= 8, "expected InodeNumber to be at least 64 bits");
|
|
|
|
|
|
|
|
// This could be a relaxed atomic operation. It doesn't matter on x86 but
|
|
|
|
// might on ARM.
|
|
|
|
auto previous = nextInodeNumber_++;
|
2020-04-23 22:38:56 +03:00
|
|
|
#ifdef _WIN32
|
|
|
|
backingOverlay_.updateUsedInodeNumber(previous);
|
|
|
|
#endif
|
2018-05-31 11:40:43 +03:00
|
|
|
DCHECK_NE(0, previous) << "allocateInodeNumber called before initialize";
|
|
|
|
return InodeNumber{previous};
|
2017-02-11 01:16:00 +03:00
|
|
|
}
|
|
|
|
|
2018-11-26 22:56:58 +03:00
|
|
|
optional<DirContents> Overlay::loadOverlayDir(InodeNumber inodeNumber) {
|
2019-11-11 21:53:50 +03:00
|
|
|
IORequest req{this};
|
2020-04-23 22:38:56 +03:00
|
|
|
auto dirData = backingOverlay_.loadOverlayDir(inodeNumber);
|
2018-10-24 04:48:38 +03:00
|
|
|
if (!dirData.has_value()) {
|
|
|
|
return std::nullopt;
|
2017-02-11 01:16:00 +03:00
|
|
|
}
|
|
|
|
const auto& dir = dirData.value();
|
2016-09-10 02:56:02 +03:00
|
|
|
|
2018-03-24 04:17:05 +03:00
|
|
|
bool shouldMigrateToNewFormat = false;
|
|
|
|
|
2018-06-01 19:29:47 +03:00
|
|
|
DirContents result;
|
2016-09-10 02:56:02 +03:00
|
|
|
for (auto& iter : dir.entries) {
|
|
|
|
const auto& name = iter.first;
|
|
|
|
const auto& value = iter.second;
|
|
|
|
|
2019-01-16 22:46:08 +03:00
|
|
|
bool isMaterialized =
|
|
|
|
!value.__isset.hash || value.hash_ref().value_unchecked().empty();
|
2018-03-24 04:17:05 +03:00
|
|
|
InodeNumber ino;
|
|
|
|
if (value.inodeNumber) {
|
|
|
|
ino = InodeNumber::fromThrift(value.inodeNumber);
|
|
|
|
} else {
|
2018-05-31 11:40:43 +03:00
|
|
|
ino = allocateInodeNumber();
|
2018-03-24 04:17:05 +03:00
|
|
|
shouldMigrateToNewFormat = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isMaterialized) {
|
2018-06-13 21:46:13 +03:00
|
|
|
result.emplace(PathComponentPiece{name}, value.mode, ino);
|
2017-02-11 01:16:00 +03:00
|
|
|
} else {
|
2019-01-16 22:46:08 +03:00
|
|
|
auto hash = Hash{folly::ByteRange{
|
|
|
|
folly::StringPiece{value.hash_ref().value_unchecked()}}};
|
2018-06-13 21:46:13 +03:00
|
|
|
result.emplace(PathComponentPiece{name}, value.mode, ino, hash);
|
2016-09-10 02:56:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-24 04:17:05 +03:00
|
|
|
if (shouldMigrateToNewFormat) {
|
2018-11-26 22:56:58 +03:00
|
|
|
saveOverlayDir(inodeNumber, result);
|
2018-03-24 04:17:05 +03:00
|
|
|
}
|
|
|
|
|
2018-11-26 22:56:58 +03:00
|
|
|
return std::move(result);
|
2016-09-10 02:56:02 +03:00
|
|
|
}
|
|
|
|
|
2018-11-26 22:56:58 +03:00
|
|
|
void Overlay::saveOverlayDir(InodeNumber inodeNumber, const DirContents& dir) {
|
2019-11-11 21:53:50 +03:00
|
|
|
IORequest req{this};
|
2018-06-01 04:32:34 +03:00
|
|
|
auto nextInodeNumber = nextInodeNumber_.load(std::memory_order_relaxed);
|
|
|
|
CHECK_LT(inodeNumber.get(), nextInodeNumber)
|
|
|
|
<< "saveOverlayDir called with unallocated inode number";
|
|
|
|
|
2017-07-26 05:52:59 +03:00
|
|
|
// TODO: T20282158 clean up access of child inode information.
|
|
|
|
//
|
2016-09-10 02:56:02 +03:00
|
|
|
// Translate the data to the thrift equivalents
|
|
|
|
overlay::OverlayDir odir;
|
|
|
|
|
2018-06-13 21:46:13 +03:00
|
|
|
for (auto& entIter : dir) {
|
2016-09-10 02:56:02 +03:00
|
|
|
const auto& entName = entIter.first;
|
2018-01-31 01:59:14 +03:00
|
|
|
const auto& ent = entIter.second;
|
2016-09-10 02:56:02 +03:00
|
|
|
|
2018-06-01 04:32:34 +03:00
|
|
|
CHECK_LT(ent.getInodeNumber().get(), nextInodeNumber)
|
|
|
|
<< "saveOverlayDir called with entry using unallocated inode number";
|
|
|
|
|
2016-09-10 02:56:02 +03:00
|
|
|
overlay::OverlayEntry oent;
|
2019-02-14 00:42:19 +03:00
|
|
|
// TODO: Eventually, we should merely serialize the child entry's dtype
|
|
|
|
// into the Overlay. But, as of now, it's possible to create an inode under
|
|
|
|
// a tree, serialize that tree into the overlay, then restart Eden. Since
|
|
|
|
// writing mode bits into the InodeMetadataTable only occurs when the inode
|
|
|
|
// is loaded, the initial mode bits must persist until the first load.
|
|
|
|
oent.mode = ent.getInitialMode();
|
2018-03-24 04:17:05 +03:00
|
|
|
oent.inodeNumber = ent.getInodeNumber().get();
|
|
|
|
bool isMaterialized = ent.isMaterialized();
|
|
|
|
if (!isMaterialized) {
|
2018-01-31 01:59:14 +03:00
|
|
|
auto entHash = ent.getHash();
|
2017-04-18 01:30:38 +03:00
|
|
|
auto bytes = entHash.getBytes();
|
2018-03-24 04:17:05 +03:00
|
|
|
oent.set_hash(std::string{reinterpret_cast<const char*>(bytes.data()),
|
|
|
|
bytes.size()});
|
2016-09-10 02:56:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
odir.entries.emplace(
|
|
|
|
std::make_pair(entName.stringPiece().str(), std::move(oent)));
|
|
|
|
}
|
|
|
|
|
2020-04-23 22:38:56 +03:00
|
|
|
backingOverlay_.saveOverlayDir(inodeNumber, odir);
|
2017-02-11 01:16:00 +03:00
|
|
|
}
|
2016-09-10 02:56:02 +03:00
|
|
|
|
2018-03-29 06:42:21 +03:00
|
|
|
void Overlay::removeOverlayData(InodeNumber inodeNumber) {
|
2019-11-11 21:53:50 +03:00
|
|
|
IORequest req{this};
|
2020-04-23 22:38:56 +03:00
|
|
|
|
|
|
|
#ifndef _WIN32
|
2018-05-22 20:46:24 +03:00
|
|
|
// TODO: batch request during GC
|
|
|
|
getInodeMetadataTable()->freeInode(inodeNumber);
|
2020-04-23 22:38:56 +03:00
|
|
|
backingOverlay_.removeOverlayFile(inodeNumber);
|
|
|
|
#else
|
|
|
|
backingOverlay_.removeOverlayData(inodeNumber);
|
|
|
|
#endif // !_WIN32
|
2017-02-11 01:16:00 +03:00
|
|
|
}
|
2016-09-10 02:56:02 +03:00
|
|
|
|
2020-04-23 22:38:56 +03:00
|
|
|
#ifndef _WIN32
|
2020-04-24 22:42:38 +03:00
|
|
|
void Overlay::recursivelyRemoveOverlayData(InodeNumber inodeNumber) {
|
2019-11-11 21:53:50 +03:00
|
|
|
IORequest req{this};
|
2020-04-23 22:38:56 +03:00
|
|
|
auto dirData = backingOverlay_.loadOverlayDir(inodeNumber);
|
2019-03-12 03:26:06 +03:00
|
|
|
|
2018-04-25 04:18:35 +03:00
|
|
|
// This inode's data must be removed from the overlay before
|
|
|
|
// recursivelyRemoveOverlayData returns to avoid a race condition if
|
|
|
|
// recursivelyRemoveOverlayData(I) is called immediately prior to
|
|
|
|
// saveOverlayDir(I). There's also no risk of violating our durability
|
|
|
|
// guarantees if the process dies after this call but before the thread could
|
|
|
|
// remove this data.
|
|
|
|
removeOverlayData(inodeNumber);
|
|
|
|
|
|
|
|
if (dirData) {
|
|
|
|
gcQueue_.lock()->queue.emplace_back(std::move(*dirData));
|
|
|
|
gcCondVar_.notify_one();
|
2018-04-06 20:47:24 +03:00
|
|
|
}
|
|
|
|
}
|
2020-04-24 22:42:38 +03:00
|
|
|
#endif
|
2018-04-06 20:47:24 +03:00
|
|
|
|
2020-04-23 22:38:56 +03:00
|
|
|
#ifndef _WIN32
|
2018-04-25 04:18:35 +03:00
|
|
|
folly::Future<folly::Unit> Overlay::flushPendingAsync() {
|
|
|
|
folly::Promise<folly::Unit> promise;
|
|
|
|
auto future = promise.getFuture();
|
|
|
|
gcQueue_.lock()->queue.emplace_back(std::move(promise));
|
|
|
|
gcCondVar_.notify_one();
|
|
|
|
return future;
|
|
|
|
}
|
2020-04-23 22:38:56 +03:00
|
|
|
#endif // !_WIN32
|
2018-04-25 04:18:35 +03:00
|
|
|
|
2018-04-06 20:47:24 +03:00
|
|
|
bool Overlay::hasOverlayData(InodeNumber inodeNumber) {
|
2019-11-11 21:53:50 +03:00
|
|
|
IORequest req{this};
|
2020-04-23 22:38:56 +03:00
|
|
|
return backingOverlay_.hasOverlayData(inodeNumber);
|
2016-09-10 02:56:02 +03:00
|
|
|
}
|
2017-07-14 03:11:36 +03:00
|
|
|
|
2020-04-23 22:38:56 +03:00
|
|
|
#ifndef _WIN32
|
2017-07-14 03:11:36 +03:00
|
|
|
// Helper function to open,validate,
|
|
|
|
// get file pointer of an overlay file
|
2019-11-07 05:21:39 +03:00
|
|
|
OverlayFile Overlay::openFile(
|
2018-03-28 22:54:24 +03:00
|
|
|
InodeNumber inodeNumber,
|
2018-11-26 22:56:58 +03:00
|
|
|
folly::StringPiece headerId) {
|
2019-11-11 21:53:50 +03:00
|
|
|
IORequest req{this};
|
2019-11-07 05:21:39 +03:00
|
|
|
return OverlayFile(
|
2020-04-23 22:38:56 +03:00
|
|
|
backingOverlay_.openFile(inodeNumber, headerId), weak_from_this());
|
2017-07-14 03:11:36 +03:00
|
|
|
}
|
|
|
|
|
2019-11-07 05:21:39 +03:00
|
|
|
OverlayFile Overlay::openFileNoVerify(InodeNumber inodeNumber) {
|
2019-11-11 21:53:50 +03:00
|
|
|
IORequest req{this};
|
2019-11-07 05:21:39 +03:00
|
|
|
return OverlayFile(
|
2020-04-23 22:38:56 +03:00
|
|
|
backingOverlay_.openFileNoVerify(inodeNumber), weak_from_this());
|
2018-03-28 22:54:24 +03:00
|
|
|
}
|
|
|
|
|
2019-11-07 05:21:39 +03:00
|
|
|
OverlayFile Overlay::createOverlayFile(
|
2018-03-29 06:42:21 +03:00
|
|
|
InodeNumber inodeNumber,
|
2019-03-12 03:26:06 +03:00
|
|
|
folly::ByteRange contents) {
|
2019-11-11 21:53:50 +03:00
|
|
|
IORequest req{this};
|
2018-06-01 04:32:34 +03:00
|
|
|
CHECK_LT(inodeNumber.get(), nextInodeNumber_.load(std::memory_order_relaxed))
|
|
|
|
<< "createOverlayFile called with unallocated inode number";
|
2019-11-07 05:21:39 +03:00
|
|
|
return OverlayFile(
|
2020-04-23 22:38:56 +03:00
|
|
|
backingOverlay_.createOverlayFile(inodeNumber, contents),
|
|
|
|
weak_from_this());
|
2017-07-14 03:11:36 +03:00
|
|
|
}
|
2017-08-05 06:14:18 +03:00
|
|
|
|
2019-11-07 05:21:39 +03:00
|
|
|
OverlayFile Overlay::createOverlayFile(
|
2018-03-28 22:54:24 +03:00
|
|
|
InodeNumber inodeNumber,
|
2019-03-12 03:26:06 +03:00
|
|
|
const folly::IOBuf& contents) {
|
2019-11-11 21:53:50 +03:00
|
|
|
IORequest req{this};
|
2019-03-12 03:26:06 +03:00
|
|
|
CHECK_LT(inodeNumber.get(), nextInodeNumber_.load(std::memory_order_relaxed))
|
|
|
|
<< "createOverlayFile called with unallocated inode number";
|
2019-11-07 05:21:39 +03:00
|
|
|
return OverlayFile(
|
2020-04-23 22:38:56 +03:00
|
|
|
backingOverlay_.createOverlayFile(inodeNumber, contents),
|
|
|
|
weak_from_this());
|
2018-03-28 22:54:24 +03:00
|
|
|
}
|
|
|
|
|
2020-04-23 22:38:56 +03:00
|
|
|
#endif // !_WIN32
|
|
|
|
|
2019-03-12 03:26:06 +03:00
|
|
|
InodeNumber Overlay::getMaxInodeNumber() {
|
|
|
|
auto ino = nextInodeNumber_.load(std::memory_order_relaxed);
|
|
|
|
CHECK_GT(ino, 1);
|
|
|
|
return InodeNumber{ino - 1};
|
2017-08-05 06:14:18 +03:00
|
|
|
}
|
2018-05-31 11:40:38 +03:00
|
|
|
|
2019-11-11 21:53:50 +03:00
|
|
|
bool Overlay::tryIncOutstandingIORequests() {
|
|
|
|
uint64_t currentOutstandingIO =
|
|
|
|
outstandingIORequests_.load(std::memory_order_seq_cst);
|
|
|
|
|
|
|
|
// Retry incrementing the IO count while we have not either successfully
|
|
|
|
// updated outstandingIORequests_ or closed the overlay
|
|
|
|
while (!(currentOutstandingIO & ioClosedMask)) {
|
|
|
|
// If not closed, currentOutstandingIO now holds what
|
|
|
|
// outstandingIORequests_ actually contained
|
|
|
|
if (outstandingIORequests_.compare_exchange_weak(
|
|
|
|
currentOutstandingIO,
|
|
|
|
currentOutstandingIO + 1,
|
|
|
|
std::memory_order_seq_cst)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have broken out of the above loop, the overlay is closed and we
|
|
|
|
// been unable to increment outstandingIORequests_.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Overlay::decOutstandingIORequests() {
|
|
|
|
uint64_t outstanding =
|
|
|
|
outstandingIORequests_.fetch_sub(1, std::memory_order_seq_cst);
|
|
|
|
XCHECK_NE(0ull, outstanding) << "Decremented too far!";
|
|
|
|
// If the overlay is closed and we just finished our last IO request (meaning
|
|
|
|
// the previous value of outstandingIORequests_ was 1), then wake the waiting
|
|
|
|
// thread.
|
|
|
|
if ((outstanding & ioClosedMask) && (outstanding & ioCountMask) == 1) {
|
|
|
|
lastOutstandingRequestIsComplete_.post();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Overlay::closeAndWaitForOutstandingIO() {
|
|
|
|
uint64_t outstanding =
|
|
|
|
outstandingIORequests_.fetch_or(ioClosedMask, std::memory_order_seq_cst);
|
|
|
|
|
|
|
|
// If we have outstanding IO requests, wait for them. This should not block if
|
|
|
|
// this baton has already been posted between the load in the fetch_or and
|
|
|
|
// this if statement.
|
|
|
|
if (outstanding & ioCountMask) {
|
|
|
|
lastOutstandingRequestIsComplete_.wait();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-23 22:38:56 +03:00
|
|
|
#ifndef _WIN32
|
|
|
|
// TODO: On Windows files are cached by the ProjectedFS. We need to clean that
|
|
|
|
// cache before doing GC.
|
|
|
|
|
2018-04-25 04:18:35 +03:00
|
|
|
void Overlay::gcThread() noexcept {
|
|
|
|
for (;;) {
|
|
|
|
std::vector<GCRequest> requests;
|
|
|
|
{
|
|
|
|
auto lock = gcQueue_.lock();
|
|
|
|
while (lock->queue.empty()) {
|
|
|
|
if (lock->stop) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
gcCondVar_.wait(lock.getUniqueLock());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
requests = std::move(lock->queue);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto& request : requests) {
|
|
|
|
try {
|
|
|
|
handleGCRequest(request);
|
|
|
|
} catch (const std::exception& e) {
|
|
|
|
XLOG(ERR) << "handleGCRequest should never throw, but it did: "
|
|
|
|
<< e.what();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Overlay::handleGCRequest(GCRequest& request) {
|
2019-11-11 21:53:50 +03:00
|
|
|
IORequest req{this};
|
2018-04-25 04:18:35 +03:00
|
|
|
if (request.flush) {
|
|
|
|
request.flush->setValue();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should only include inode numbers for trees.
|
|
|
|
std::queue<InodeNumber> queue;
|
|
|
|
|
|
|
|
// TODO: For better throughput on large tree collections, it might make
|
|
|
|
// sense to split this into two threads: one for traversing the tree and
|
|
|
|
// another that makes the actual unlink calls.
|
|
|
|
auto safeRemoveOverlayData = [&](InodeNumber inodeNumber) {
|
|
|
|
try {
|
|
|
|
removeOverlayData(inodeNumber);
|
|
|
|
} catch (const std::exception& e) {
|
|
|
|
XLOG(ERR) << "Failed to remove overlay data for inode " << inodeNumber
|
|
|
|
<< ": " << e.what();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
auto processDir = [&](const overlay::OverlayDir& dir) {
|
|
|
|
for (const auto& entry : dir.entries) {
|
|
|
|
const auto& value = entry.second;
|
|
|
|
if (!value.inodeNumber) {
|
|
|
|
// Legacy-only. All new Overlay trees have inode numbers for all
|
|
|
|
// children.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
auto ino = InodeNumber::fromThrift(value.inodeNumber);
|
|
|
|
|
|
|
|
if (S_ISDIR(value.mode)) {
|
|
|
|
queue.push(ino);
|
|
|
|
} else {
|
|
|
|
// No need to recurse, but delete any file at this inode. Note that,
|
|
|
|
// under normal operation, there should be nothing at this path
|
|
|
|
// because files are only written into the overlay if they're
|
|
|
|
// materialized.
|
|
|
|
safeRemoveOverlayData(ino);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
processDir(request.dir);
|
|
|
|
|
|
|
|
while (!queue.empty()) {
|
|
|
|
auto ino = queue.front();
|
|
|
|
queue.pop();
|
|
|
|
|
|
|
|
overlay::OverlayDir dir;
|
|
|
|
try {
|
2020-04-23 22:38:56 +03:00
|
|
|
auto dirData = backingOverlay_.loadOverlayDir(ino);
|
2018-10-24 04:48:38 +03:00
|
|
|
if (!dirData.has_value()) {
|
2019-12-20 05:46:01 +03:00
|
|
|
XLOG(DBG7) << "no dir data for inode " << ino;
|
2018-04-25 04:18:35 +03:00
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
dir = std::move(*dirData);
|
|
|
|
}
|
|
|
|
} catch (const std::exception& e) {
|
|
|
|
XLOG(ERR) << "While collecting, failed to load tree data for inode "
|
|
|
|
<< ino << ": " << e.what();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
safeRemoveOverlayData(ino);
|
|
|
|
processDir(dir);
|
|
|
|
}
|
|
|
|
}
|
2020-04-23 22:38:56 +03:00
|
|
|
#endif // !1
|
2018-07-20 21:40:20 +03:00
|
|
|
|
2017-11-04 01:58:04 +03:00
|
|
|
} // namespace eden
|
|
|
|
} // namespace facebook
|